diff options
author | Rafael J. Wysocki <rjw@sisk.pl> | 2008-06-11 16:04:29 -0400 |
---|---|---|
committer | Andi Kleen <andi@basil.nowhere.org> | 2008-07-16 17:27:03 -0400 |
commit | ebb12db51f6c13b30752fcf506baad4c617b153c (patch) | |
tree | 55c08b82463ffd5b78a8bc4a4748261b02391593 | |
parent | d20a4dca47d2cd027ed58a13f91b424affd1f449 (diff) |
Freezer: Introduce PF_FREEZER_NOSIG
The freezer currently attempts to distinguish kernel threads from
user space tasks by checking if their mm pointer is unset and it
does not send fake signals to kernel threads. However, there are
kernel threads, mostly related to networking, that behave like
user space tasks and may want to be sent a fake signal to be frozen.
Introduce the new process flag PF_FREEZER_NOSIG that will be set
by default for all kernel threads and make the freezer only send
fake signals to the tasks having PF_FREEZER_NOSIG unset. Provide
the set_freezable_with_signal() function to be called by the kernel
threads that want to be sent a fake signal for freezing.
This patch should not change the freezer's observable behavior.
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Signed-off-by: Andi Kleen <ak@linux.intel.com>
Acked-by: Pavel Machek <pavel@suse.cz>
Signed-off-by: Len Brown <len.brown@intel.com>
-rw-r--r-- | include/linux/freezer.h | 10 | ||||
-rw-r--r-- | include/linux/sched.h | 1 | ||||
-rw-r--r-- | kernel/kthread.c | 2 | ||||
-rw-r--r-- | kernel/power/process.c | 97 |
4 files changed, 54 insertions, 56 deletions
diff --git a/include/linux/freezer.h b/include/linux/freezer.h index 08934995c7ab..deddeedf3257 100644 --- a/include/linux/freezer.h +++ b/include/linux/freezer.h | |||
@@ -128,6 +128,15 @@ static inline void set_freezable(void) | |||
128 | } | 128 | } |
129 | 129 | ||
130 | /* | 130 | /* |
131 | * Tell the freezer that the current task should be frozen by it and that it | ||
132 | * should send a fake signal to the task to freeze it. | ||
133 | */ | ||
134 | static inline void set_freezable_with_signal(void) | ||
135 | { | ||
136 | current->flags &= ~(PF_NOFREEZE | PF_FREEZER_NOSIG); | ||
137 | } | ||
138 | |||
139 | /* | ||
131 | * Freezer-friendly wrappers around wait_event_interruptible() and | 140 | * Freezer-friendly wrappers around wait_event_interruptible() and |
132 | * wait_event_interruptible_timeout(), originally defined in <linux/wait.h> | 141 | * wait_event_interruptible_timeout(), originally defined in <linux/wait.h> |
133 | */ | 142 | */ |
@@ -174,6 +183,7 @@ static inline void freezer_do_not_count(void) {} | |||
174 | static inline void freezer_count(void) {} | 183 | static inline void freezer_count(void) {} |
175 | static inline int freezer_should_skip(struct task_struct *p) { return 0; } | 184 | static inline int freezer_should_skip(struct task_struct *p) { return 0; } |
176 | static inline void set_freezable(void) {} | 185 | static inline void set_freezable(void) {} |
186 | static inline void set_freezable_with_signal(void) {} | ||
177 | 187 | ||
178 | #define wait_event_freezable(wq, condition) \ | 188 | #define wait_event_freezable(wq, condition) \ |
179 | wait_event_interruptible(wq, condition) | 189 | wait_event_interruptible(wq, condition) |
diff --git a/include/linux/sched.h b/include/linux/sched.h index 21349173d148..ba2f859c6e4f 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h | |||
@@ -1494,6 +1494,7 @@ static inline void put_task_struct(struct task_struct *t) | |||
1494 | #define PF_MEMPOLICY 0x10000000 /* Non-default NUMA mempolicy */ | 1494 | #define PF_MEMPOLICY 0x10000000 /* Non-default NUMA mempolicy */ |
1495 | #define PF_MUTEX_TESTER 0x20000000 /* Thread belongs to the rt mutex tester */ | 1495 | #define PF_MUTEX_TESTER 0x20000000 /* Thread belongs to the rt mutex tester */ |
1496 | #define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezeable */ | 1496 | #define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezeable */ |
1497 | #define PF_FREEZER_NOSIG 0x80000000 /* Freezer won't send signals to it */ | ||
1497 | 1498 | ||
1498 | /* | 1499 | /* |
1499 | * Only the _current_ task can read/write to tsk->flags, but other | 1500 | * Only the _current_ task can read/write to tsk->flags, but other |
diff --git a/kernel/kthread.c b/kernel/kthread.c index 97747cdd37c9..ac3fb7326641 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c | |||
@@ -235,7 +235,7 @@ int kthreadd(void *unused) | |||
235 | set_user_nice(tsk, KTHREAD_NICE_LEVEL); | 235 | set_user_nice(tsk, KTHREAD_NICE_LEVEL); |
236 | set_cpus_allowed(tsk, CPU_MASK_ALL); | 236 | set_cpus_allowed(tsk, CPU_MASK_ALL); |
237 | 237 | ||
238 | current->flags |= PF_NOFREEZE; | 238 | current->flags |= PF_NOFREEZE | PF_FREEZER_NOSIG; |
239 | 239 | ||
240 | for (;;) { | 240 | for (;;) { |
241 | set_current_state(TASK_INTERRUPTIBLE); | 241 | set_current_state(TASK_INTERRUPTIBLE); |
diff --git a/kernel/power/process.c b/kernel/power/process.c index f1d0b345c9ba..5fb87652f214 100644 --- a/kernel/power/process.c +++ b/kernel/power/process.c | |||
@@ -19,9 +19,6 @@ | |||
19 | */ | 19 | */ |
20 | #define TIMEOUT (20 * HZ) | 20 | #define TIMEOUT (20 * HZ) |
21 | 21 | ||
22 | #define FREEZER_KERNEL_THREADS 0 | ||
23 | #define FREEZER_USER_SPACE 1 | ||
24 | |||
25 | static inline int freezeable(struct task_struct * p) | 22 | static inline int freezeable(struct task_struct * p) |
26 | { | 23 | { |
27 | if ((p == current) || | 24 | if ((p == current) || |
@@ -84,63 +81,53 @@ static void fake_signal_wake_up(struct task_struct *p) | |||
84 | spin_unlock_irqrestore(&p->sighand->siglock, flags); | 81 | spin_unlock_irqrestore(&p->sighand->siglock, flags); |
85 | } | 82 | } |
86 | 83 | ||
87 | static int has_mm(struct task_struct *p) | 84 | static inline bool should_send_signal(struct task_struct *p) |
88 | { | 85 | { |
89 | return (p->mm && !(p->flags & PF_BORROWED_MM)); | 86 | return !(p->flags & PF_FREEZER_NOSIG); |
90 | } | 87 | } |
91 | 88 | ||
92 | /** | 89 | /** |
93 | * freeze_task - send a freeze request to given task | 90 | * freeze_task - send a freeze request to given task |
94 | * @p: task to send the request to | 91 | * @p: task to send the request to |
95 | * @with_mm_only: if set, the request will only be sent if the task has its | 92 | * @sig_only: if set, the request will only be sent if the task has the |
96 | * own mm | 93 | * PF_FREEZER_NOSIG flag unset |
97 | * Return value: 0, if @with_mm_only is set and the task has no mm of its | 94 | * Return value: 'false', if @sig_only is set and the task has |
98 | * own or the task is frozen, 1, otherwise | 95 | * PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise |
99 | * | 96 | * |
100 | * The freeze request is sent by seting the tasks's TIF_FREEZE flag and | 97 | * The freeze request is sent by setting the tasks's TIF_FREEZE flag and |
101 | * either sending a fake signal to it or waking it up, depending on whether | 98 | * either sending a fake signal to it or waking it up, depending on whether |
102 | * or not it has its own mm (ie. it is a user land task). If @with_mm_only | 99 | * or not it has PF_FREEZER_NOSIG set. If @sig_only is set and the task |
103 | * is set and the task has no mm of its own (ie. it is a kernel thread), | 100 | * has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its |
104 | * its TIF_FREEZE flag should not be set. | 101 | * TIF_FREEZE flag will not be set. |
105 | * | ||
106 | * The task_lock() is necessary to prevent races with exit_mm() or | ||
107 | * use_mm()/unuse_mm() from occuring. | ||
108 | */ | 102 | */ |
109 | static int freeze_task(struct task_struct *p, int with_mm_only) | 103 | static bool freeze_task(struct task_struct *p, bool sig_only) |
110 | { | 104 | { |
111 | int ret = 1; | 105 | /* |
106 | * We first check if the task is freezing and next if it has already | ||
107 | * been frozen to avoid the race with frozen_process() which first marks | ||
108 | * the task as frozen and next clears its TIF_FREEZE. | ||
109 | */ | ||
110 | if (!freezing(p)) { | ||
111 | rmb(); | ||
112 | if (frozen(p)) | ||
113 | return false; | ||
112 | 114 | ||
113 | task_lock(p); | 115 | if (!sig_only || should_send_signal(p)) |
114 | if (freezing(p)) { | 116 | set_freeze_flag(p); |
115 | if (has_mm(p)) { | 117 | else |
116 | if (!signal_pending(p)) | 118 | return false; |
117 | fake_signal_wake_up(p); | 119 | } |
118 | } else { | 120 | |
119 | if (with_mm_only) | 121 | if (should_send_signal(p)) { |
120 | ret = 0; | 122 | if (!signal_pending(p)) |
121 | else | 123 | fake_signal_wake_up(p); |
122 | wake_up_state(p, TASK_INTERRUPTIBLE); | 124 | } else if (sig_only) { |
123 | } | 125 | return false; |
124 | } else { | 126 | } else { |
125 | rmb(); | 127 | wake_up_state(p, TASK_INTERRUPTIBLE); |
126 | if (frozen(p)) { | ||
127 | ret = 0; | ||
128 | } else { | ||
129 | if (has_mm(p)) { | ||
130 | set_freeze_flag(p); | ||
131 | fake_signal_wake_up(p); | ||
132 | } else { | ||
133 | if (with_mm_only) { | ||
134 | ret = 0; | ||
135 | } else { | ||
136 | set_freeze_flag(p); | ||
137 | wake_up_state(p, TASK_INTERRUPTIBLE); | ||
138 | } | ||
139 | } | ||
140 | } | ||
141 | } | 128 | } |
142 | task_unlock(p); | 129 | |
143 | return ret; | 130 | return true; |
144 | } | 131 | } |
145 | 132 | ||
146 | static void cancel_freezing(struct task_struct *p) | 133 | static void cancel_freezing(struct task_struct *p) |
@@ -156,7 +143,7 @@ static void cancel_freezing(struct task_struct *p) | |||
156 | } | 143 | } |
157 | } | 144 | } |
158 | 145 | ||
159 | static int try_to_freeze_tasks(int freeze_user_space) | 146 | static int try_to_freeze_tasks(bool sig_only) |
160 | { | 147 | { |
161 | struct task_struct *g, *p; | 148 | struct task_struct *g, *p; |
162 | unsigned long end_time; | 149 | unsigned long end_time; |
@@ -175,7 +162,7 @@ static int try_to_freeze_tasks(int freeze_user_space) | |||
175 | if (frozen(p) || !freezeable(p)) | 162 | if (frozen(p) || !freezeable(p)) |
176 | continue; | 163 | continue; |
177 | 164 | ||
178 | if (!freeze_task(p, freeze_user_space)) | 165 | if (!freeze_task(p, sig_only)) |
179 | continue; | 166 | continue; |
180 | 167 | ||
181 | /* | 168 | /* |
@@ -235,13 +222,13 @@ int freeze_processes(void) | |||
235 | int error; | 222 | int error; |
236 | 223 | ||
237 | printk("Freezing user space processes ... "); | 224 | printk("Freezing user space processes ... "); |
238 | error = try_to_freeze_tasks(FREEZER_USER_SPACE); | 225 | error = try_to_freeze_tasks(true); |
239 | if (error) | 226 | if (error) |
240 | goto Exit; | 227 | goto Exit; |
241 | printk("done.\n"); | 228 | printk("done.\n"); |
242 | 229 | ||
243 | printk("Freezing remaining freezable tasks ... "); | 230 | printk("Freezing remaining freezable tasks ... "); |
244 | error = try_to_freeze_tasks(FREEZER_KERNEL_THREADS); | 231 | error = try_to_freeze_tasks(false); |
245 | if (error) | 232 | if (error) |
246 | goto Exit; | 233 | goto Exit; |
247 | printk("done."); | 234 | printk("done."); |
@@ -251,7 +238,7 @@ int freeze_processes(void) | |||
251 | return error; | 238 | return error; |
252 | } | 239 | } |
253 | 240 | ||
254 | static void thaw_tasks(int thaw_user_space) | 241 | static void thaw_tasks(bool nosig_only) |
255 | { | 242 | { |
256 | struct task_struct *g, *p; | 243 | struct task_struct *g, *p; |
257 | 244 | ||
@@ -260,7 +247,7 @@ static void thaw_tasks(int thaw_user_space) | |||
260 | if (!freezeable(p)) | 247 | if (!freezeable(p)) |
261 | continue; | 248 | continue; |
262 | 249 | ||
263 | if (!p->mm == thaw_user_space) | 250 | if (nosig_only && should_send_signal(p)) |
264 | continue; | 251 | continue; |
265 | 252 | ||
266 | thaw_process(p); | 253 | thaw_process(p); |
@@ -271,8 +258,8 @@ static void thaw_tasks(int thaw_user_space) | |||
271 | void thaw_processes(void) | 258 | void thaw_processes(void) |
272 | { | 259 | { |
273 | printk("Restarting tasks ... "); | 260 | printk("Restarting tasks ... "); |
274 | thaw_tasks(FREEZER_KERNEL_THREADS); | 261 | thaw_tasks(true); |
275 | thaw_tasks(FREEZER_USER_SPACE); | 262 | thaw_tasks(false); |
276 | schedule(); | 263 | schedule(); |
277 | printk("done.\n"); | 264 | printk("done.\n"); |
278 | } | 265 | } |