Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 1 | /* Freezer declarations */ |
| 2 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 3 | #ifndef FREEZER_H_INCLUDED |
| 4 | #define FREEZER_H_INCLUDED |
| 5 | |
Randy Dunlap | 5c543ef | 2006-12-10 02:18:58 -0800 | [diff] [blame] | 6 | #include <linux/sched.h> |
| 7 | |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 8 | #ifdef CONFIG_PM |
| 9 | /* |
| 10 | * Check if a process has been frozen |
| 11 | */ |
| 12 | static inline int frozen(struct task_struct *p) |
| 13 | { |
| 14 | return p->flags & PF_FROZEN; |
| 15 | } |
| 16 | |
| 17 | /* |
| 18 | * Check if there is a request to freeze a process |
| 19 | */ |
| 20 | static inline int freezing(struct task_struct *p) |
| 21 | { |
Rafael J. Wysocki | 8a102ee | 2006-12-13 00:34:30 -0800 | [diff] [blame] | 22 | return test_tsk_thread_flag(p, TIF_FREEZE); |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | /* |
| 26 | * Request that a process be frozen |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 27 | */ |
| 28 | static inline void freeze(struct task_struct *p) |
| 29 | { |
Rafael J. Wysocki | 8a102ee | 2006-12-13 00:34:30 -0800 | [diff] [blame] | 30 | set_tsk_thread_flag(p, TIF_FREEZE); |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | /* |
| 34 | * Sometimes we may need to cancel the previous 'freeze' request |
| 35 | */ |
| 36 | static inline void do_not_freeze(struct task_struct *p) |
| 37 | { |
Rafael J. Wysocki | 8a102ee | 2006-12-13 00:34:30 -0800 | [diff] [blame] | 38 | clear_tsk_thread_flag(p, TIF_FREEZE); |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /* |
| 42 | * Wake up a frozen process |
Rafael J. Wysocki | 33e1c28 | 2007-05-23 13:57:24 -0700 | [diff] [blame] | 43 | * |
| 44 | * task_lock() is taken to prevent the race with refrigerator() which may |
| 45 | * occur if the freezing of tasks fails. Namely, without the lock, if the |
| 46 | * freezing of tasks failed, thaw_tasks() might have run before a task in |
| 47 | * refrigerator() could call frozen_process(), in which case the task would be |
| 48 | * frozen and no one would thaw it. |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 49 | */ |
| 50 | static inline int thaw_process(struct task_struct *p) |
| 51 | { |
Rafael J. Wysocki | 33e1c28 | 2007-05-23 13:57:24 -0700 | [diff] [blame] | 52 | task_lock(p); |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 53 | if (frozen(p)) { |
| 54 | p->flags &= ~PF_FROZEN; |
Rafael J. Wysocki | 33e1c28 | 2007-05-23 13:57:24 -0700 | [diff] [blame] | 55 | task_unlock(p); |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 56 | wake_up_process(p); |
| 57 | return 1; |
| 58 | } |
Rafael J. Wysocki | 33e1c28 | 2007-05-23 13:57:24 -0700 | [diff] [blame] | 59 | clear_tsk_thread_flag(p, TIF_FREEZE); |
| 60 | task_unlock(p); |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 64 | extern void refrigerator(void); |
| 65 | extern int freeze_processes(void); |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame] | 66 | extern void thaw_processes(void); |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 67 | |
| 68 | static inline int try_to_freeze(void) |
| 69 | { |
| 70 | if (freezing(current)) { |
| 71 | refrigerator(); |
| 72 | return 1; |
| 73 | } else |
| 74 | return 0; |
| 75 | } |
Nigel Cunningham | ff39593 | 2006-12-06 20:34:28 -0800 | [diff] [blame] | 76 | |
Rafael J. Wysocki | ba96a0c | 2007-05-23 13:57:25 -0700 | [diff] [blame] | 77 | /* |
| 78 | * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it |
| 79 | * calls wait_for_completion(&vfork) and reset right after it returns from this |
| 80 | * function. Next, the parent should call try_to_freeze() to freeze itself |
| 81 | * appropriately in case the child has exited before the freezing of tasks is |
| 82 | * complete. However, we don't want kernel threads to be frozen in unexpected |
| 83 | * places, so we allow them to block freeze_processes() instead or to set |
| 84 | * PF_NOFREEZE if needed and PF_FREEZER_SKIP is only set for userland vfork |
| 85 | * parents. Fortunately, in the ____call_usermodehelper() case the parent won't |
| 86 | * really block freeze_processes(), since ____call_usermodehelper() (the child) |
| 87 | * does a little before exec/exit and it can't be frozen before waking up the |
| 88 | * parent. |
| 89 | */ |
| 90 | |
| 91 | /* |
| 92 | * If the current task is a user space one, tell the freezer not to count it as |
| 93 | * freezable. |
| 94 | */ |
| 95 | static inline void freezer_do_not_count(void) |
| 96 | { |
| 97 | if (current->mm) |
| 98 | current->flags |= PF_FREEZER_SKIP; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * If the current task is a user space one, tell the freezer to count it as |
| 103 | * freezable again and try to freeze it. |
| 104 | */ |
| 105 | static inline void freezer_count(void) |
| 106 | { |
| 107 | if (current->mm) { |
| 108 | current->flags &= ~PF_FREEZER_SKIP; |
| 109 | try_to_freeze(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Check if the task should be counted as freezeable by the freezer |
| 115 | */ |
| 116 | static inline int freezer_should_skip(struct task_struct *p) |
| 117 | { |
| 118 | return !!(p->flags & PF_FREEZER_SKIP); |
| 119 | } |
Nigel Cunningham | ff39593 | 2006-12-06 20:34:28 -0800 | [diff] [blame] | 120 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 121 | /* |
| 122 | * Tell the freezer that the current task should be frozen by it |
| 123 | */ |
| 124 | static inline void set_freezable(void) |
| 125 | { |
| 126 | current->flags &= ~PF_NOFREEZE; |
| 127 | } |
| 128 | |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 129 | #else |
| 130 | static inline int frozen(struct task_struct *p) { return 0; } |
| 131 | static inline int freezing(struct task_struct *p) { return 0; } |
| 132 | static inline void freeze(struct task_struct *p) { BUG(); } |
| 133 | static inline int thaw_process(struct task_struct *p) { return 1; } |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 134 | |
| 135 | static inline void refrigerator(void) {} |
| 136 | static inline int freeze_processes(void) { BUG(); return 0; } |
| 137 | static inline void thaw_processes(void) {} |
| 138 | |
| 139 | static inline int try_to_freeze(void) { return 0; } |
| 140 | |
Rafael J. Wysocki | ba96a0c | 2007-05-23 13:57:25 -0700 | [diff] [blame] | 141 | static inline void freezer_do_not_count(void) {} |
| 142 | static inline void freezer_count(void) {} |
| 143 | static inline int freezer_should_skip(struct task_struct *p) { return 0; } |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 144 | static inline void set_freezable(void) {} |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 145 | #endif |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 146 | |
| 147 | #endif /* FREEZER_H_INCLUDED */ |