Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Kernel thread helper functions. |
2 | * Copyright (C) 2004 IBM Corporation, Rusty Russell. | |
3 | * | |
73c27992 | 4 | * Creation is done via kthreadd, so that we get a clean environment |
1da177e4 LT |
5 | * even if we're invoked from userspace (think modprobe, hotplug cpu, |
6 | * etc.). | |
7 | */ | |
8 | #include <linux/sched.h> | |
9 | #include <linux/kthread.h> | |
10 | #include <linux/completion.h> | |
11 | #include <linux/err.h> | |
58568d2a | 12 | #include <linux/cpuset.h> |
1da177e4 LT |
13 | #include <linux/unistd.h> |
14 | #include <linux/file.h> | |
9984de1a | 15 | #include <linux/export.h> |
97d1f15b | 16 | #include <linux/mutex.h> |
b56c0d89 TH |
17 | #include <linux/slab.h> |
18 | #include <linux/freezer.h> | |
a74fb73c | 19 | #include <linux/ptrace.h> |
cd42d559 | 20 | #include <linux/uaccess.h> |
ad8d75ff | 21 | #include <trace/events/sched.h> |
1da177e4 | 22 | |
73c27992 EB |
23 | static DEFINE_SPINLOCK(kthread_create_lock); |
24 | static LIST_HEAD(kthread_create_list); | |
25 | struct task_struct *kthreadd_task; | |
1da177e4 LT |
26 | |
27 | struct kthread_create_info | |
28 | { | |
73c27992 | 29 | /* Information passed to kthread() from kthreadd. */ |
1da177e4 LT |
30 | int (*threadfn)(void *data); |
31 | void *data; | |
207205a2 | 32 | int node; |
1da177e4 | 33 | |
73c27992 | 34 | /* Result passed back to kthread_create() from kthreadd. */ |
1da177e4 | 35 | struct task_struct *result; |
786235ee | 36 | struct completion *done; |
65f27f38 | 37 | |
73c27992 | 38 | struct list_head list; |
1da177e4 LT |
39 | }; |
40 | ||
63706172 | 41 | struct kthread { |
2a1d4460 TG |
42 | unsigned long flags; |
43 | unsigned int cpu; | |
82805ab7 | 44 | void *data; |
2a1d4460 | 45 | struct completion parked; |
63706172 | 46 | struct completion exited; |
1da177e4 LT |
47 | }; |
48 | ||
2a1d4460 TG |
49 | enum KTHREAD_BITS { |
50 | KTHREAD_IS_PER_CPU = 0, | |
51 | KTHREAD_SHOULD_STOP, | |
52 | KTHREAD_SHOULD_PARK, | |
53 | KTHREAD_IS_PARKED, | |
54 | }; | |
55 | ||
1da5c46f ON |
56 | static inline void set_kthread_struct(void *kthread) |
57 | { | |
58 | /* | |
59 | * We abuse ->set_child_tid to avoid the new member and because it | |
60 | * can't be wrongly copied by copy_process(). We also rely on fact | |
61 | * that the caller can't exec, so PF_KTHREAD can't be cleared. | |
62 | */ | |
63 | current->set_child_tid = (__force void __user *)kthread; | |
64 | } | |
4ecdafc8 ON |
65 | |
66 | static inline struct kthread *to_kthread(struct task_struct *k) | |
67 | { | |
1da5c46f ON |
68 | WARN_ON(!(k->flags & PF_KTHREAD)); |
69 | return (__force void *)k->set_child_tid; | |
4ecdafc8 ON |
70 | } |
71 | ||
1da5c46f ON |
72 | void free_kthread_struct(struct task_struct *k) |
73 | { | |
74 | /* | |
75 | * Can be NULL if this kthread was created by kernel_thread() | |
76 | * or if kmalloc() in kthread() failed. | |
77 | */ | |
78 | kfree(to_kthread(k)); | |
79 | } | |
80 | ||
81 | #define __to_kthread(vfork) \ | |
82 | container_of(vfork, struct kthread, exited) | |
83 | ||
84 | /* | |
85 | * TODO: kill it and use to_kthread(). But we still need the users | |
86 | * like kthread_stop() which has to sync with the exiting kthread. | |
87 | */ | |
4ecdafc8 ON |
88 | static struct kthread *to_live_kthread(struct task_struct *k) |
89 | { | |
90 | struct completion *vfork = ACCESS_ONCE(k->vfork_done); | |
eff96625 | 91 | if (likely(vfork)) |
4ecdafc8 ON |
92 | return __to_kthread(vfork); |
93 | return NULL; | |
94 | } | |
1da177e4 | 95 | |
9e37bd30 RD |
96 | /** |
97 | * kthread_should_stop - should this kthread return now? | |
98 | * | |
72fd4a35 | 99 | * When someone calls kthread_stop() on your kthread, it will be woken |
9e37bd30 RD |
100 | * and this will return true. You should then return, and your return |
101 | * value will be passed through to kthread_stop(). | |
102 | */ | |
2a1d4460 | 103 | bool kthread_should_stop(void) |
1da177e4 | 104 | { |
2a1d4460 | 105 | return test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags); |
1da177e4 LT |
106 | } |
107 | EXPORT_SYMBOL(kthread_should_stop); | |
108 | ||
2a1d4460 TG |
109 | /** |
110 | * kthread_should_park - should this kthread park now? | |
111 | * | |
112 | * When someone calls kthread_park() on your kthread, it will be woken | |
113 | * and this will return true. You should then do the necessary | |
114 | * cleanup and call kthread_parkme() | |
115 | * | |
116 | * Similar to kthread_should_stop(), but this keeps the thread alive | |
117 | * and in a park position. kthread_unpark() "restarts" the thread and | |
118 | * calls the thread function again. | |
119 | */ | |
120 | bool kthread_should_park(void) | |
121 | { | |
122 | return test_bit(KTHREAD_SHOULD_PARK, &to_kthread(current)->flags); | |
123 | } | |
18896451 | 124 | EXPORT_SYMBOL_GPL(kthread_should_park); |
2a1d4460 | 125 | |
8a32c441 TH |
126 | /** |
127 | * kthread_freezable_should_stop - should this freezable kthread return now? | |
128 | * @was_frozen: optional out parameter, indicates whether %current was frozen | |
129 | * | |
130 | * kthread_should_stop() for freezable kthreads, which will enter | |
131 | * refrigerator if necessary. This function is safe from kthread_stop() / | |
132 | * freezer deadlock and freezable kthreads should use this function instead | |
133 | * of calling try_to_freeze() directly. | |
134 | */ | |
135 | bool kthread_freezable_should_stop(bool *was_frozen) | |
136 | { | |
137 | bool frozen = false; | |
138 | ||
139 | might_sleep(); | |
140 | ||
141 | if (unlikely(freezing(current))) | |
142 | frozen = __refrigerator(true); | |
143 | ||
144 | if (was_frozen) | |
145 | *was_frozen = frozen; | |
146 | ||
147 | return kthread_should_stop(); | |
148 | } | |
149 | EXPORT_SYMBOL_GPL(kthread_freezable_should_stop); | |
150 | ||
82805ab7 TH |
151 | /** |
152 | * kthread_data - return data value specified on kthread creation | |
153 | * @task: kthread task in question | |
154 | * | |
155 | * Return the data value specified when kthread @task was created. | |
156 | * The caller is responsible for ensuring the validity of @task when | |
157 | * calling this function. | |
158 | */ | |
159 | void *kthread_data(struct task_struct *task) | |
160 | { | |
161 | return to_kthread(task)->data; | |
162 | } | |
163 | ||
cd42d559 | 164 | /** |
e700591a | 165 | * kthread_probe_data - speculative version of kthread_data() |
cd42d559 TH |
166 | * @task: possible kthread task in question |
167 | * | |
168 | * @task could be a kthread task. Return the data value specified when it | |
169 | * was created if accessible. If @task isn't a kthread task or its data is | |
170 | * inaccessible for any reason, %NULL is returned. This function requires | |
171 | * that @task itself is safe to dereference. | |
172 | */ | |
e700591a | 173 | void *kthread_probe_data(struct task_struct *task) |
cd42d559 TH |
174 | { |
175 | struct kthread *kthread = to_kthread(task); | |
176 | void *data = NULL; | |
177 | ||
178 | probe_kernel_read(&data, &kthread->data, sizeof(data)); | |
179 | return data; | |
180 | } | |
181 | ||
2a1d4460 TG |
182 | static void __kthread_parkme(struct kthread *self) |
183 | { | |
f2530dc7 | 184 | __set_current_state(TASK_PARKED); |
2a1d4460 TG |
185 | while (test_bit(KTHREAD_SHOULD_PARK, &self->flags)) { |
186 | if (!test_and_set_bit(KTHREAD_IS_PARKED, &self->flags)) | |
187 | complete(&self->parked); | |
188 | schedule(); | |
f2530dc7 | 189 | __set_current_state(TASK_PARKED); |
2a1d4460 TG |
190 | } |
191 | clear_bit(KTHREAD_IS_PARKED, &self->flags); | |
192 | __set_current_state(TASK_RUNNING); | |
193 | } | |
194 | ||
195 | void kthread_parkme(void) | |
196 | { | |
197 | __kthread_parkme(to_kthread(current)); | |
198 | } | |
18896451 | 199 | EXPORT_SYMBOL_GPL(kthread_parkme); |
2a1d4460 | 200 | |
1da177e4 LT |
201 | static int kthread(void *_create) |
202 | { | |
63706172 | 203 | /* Copy data: it's on kthread's stack */ |
1da177e4 | 204 | struct kthread_create_info *create = _create; |
63706172 ON |
205 | int (*threadfn)(void *data) = create->threadfn; |
206 | void *data = create->data; | |
786235ee | 207 | struct completion *done; |
1da5c46f | 208 | struct kthread *self; |
63706172 | 209 | int ret; |
1da177e4 | 210 | |
1da5c46f ON |
211 | self = kmalloc(sizeof(*self), GFP_KERNEL); |
212 | set_kthread_struct(self); | |
1da177e4 | 213 | |
786235ee TH |
214 | /* If user was SIGKILLed, I release the structure. */ |
215 | done = xchg(&create->done, NULL); | |
216 | if (!done) { | |
217 | kfree(create); | |
218 | do_exit(-EINTR); | |
219 | } | |
1da5c46f ON |
220 | |
221 | if (!self) { | |
222 | create->result = ERR_PTR(-ENOMEM); | |
223 | complete(done); | |
224 | do_exit(-ENOMEM); | |
225 | } | |
226 | ||
227 | self->flags = 0; | |
228 | self->data = data; | |
229 | init_completion(&self->exited); | |
230 | init_completion(&self->parked); | |
231 | current->vfork_done = &self->exited; | |
232 | ||
1da177e4 | 233 | /* OK, tell user we're spawned, wait for stop or wakeup */ |
a076e4bc | 234 | __set_current_state(TASK_UNINTERRUPTIBLE); |
3217ab97 | 235 | create->result = current; |
786235ee | 236 | complete(done); |
1da177e4 LT |
237 | schedule(); |
238 | ||
63706172 | 239 | ret = -EINTR; |
1da5c46f ON |
240 | if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) { |
241 | __kthread_parkme(self); | |
2a1d4460 TG |
242 | ret = threadfn(data); |
243 | } | |
63706172 | 244 | do_exit(ret); |
1da177e4 LT |
245 | } |
246 | ||
207205a2 ED |
247 | /* called from do_fork() to get node information for about to be created task */ |
248 | int tsk_fork_get_node(struct task_struct *tsk) | |
249 | { | |
250 | #ifdef CONFIG_NUMA | |
251 | if (tsk == kthreadd_task) | |
252 | return tsk->pref_node_fork; | |
253 | #endif | |
81c98869 | 254 | return NUMA_NO_NODE; |
207205a2 ED |
255 | } |
256 | ||
73c27992 | 257 | static void create_kthread(struct kthread_create_info *create) |
1da177e4 | 258 | { |
1da177e4 LT |
259 | int pid; |
260 | ||
207205a2 ED |
261 | #ifdef CONFIG_NUMA |
262 | current->pref_node_fork = create->node; | |
263 | #endif | |
1da177e4 LT |
264 | /* We want our own signal handler (we take no signals by default). */ |
265 | pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD); | |
cdd140bd | 266 | if (pid < 0) { |
786235ee TH |
267 | /* If user was SIGKILLed, I release the structure. */ |
268 | struct completion *done = xchg(&create->done, NULL); | |
269 | ||
270 | if (!done) { | |
271 | kfree(create); | |
272 | return; | |
273 | } | |
1da177e4 | 274 | create->result = ERR_PTR(pid); |
786235ee | 275 | complete(done); |
cdd140bd | 276 | } |
1da177e4 LT |
277 | } |
278 | ||
255451e4 PM |
279 | static struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data), |
280 | void *data, int node, | |
281 | const char namefmt[], | |
282 | va_list args) | |
1da177e4 | 283 | { |
786235ee TH |
284 | DECLARE_COMPLETION_ONSTACK(done); |
285 | struct task_struct *task; | |
286 | struct kthread_create_info *create = kmalloc(sizeof(*create), | |
287 | GFP_KERNEL); | |
288 | ||
289 | if (!create) | |
290 | return ERR_PTR(-ENOMEM); | |
291 | create->threadfn = threadfn; | |
292 | create->data = data; | |
293 | create->node = node; | |
294 | create->done = &done; | |
73c27992 EB |
295 | |
296 | spin_lock(&kthread_create_lock); | |
786235ee | 297 | list_add_tail(&create->list, &kthread_create_list); |
73c27992 EB |
298 | spin_unlock(&kthread_create_lock); |
299 | ||
cbd9b67b | 300 | wake_up_process(kthreadd_task); |
786235ee TH |
301 | /* |
302 | * Wait for completion in killable state, for I might be chosen by | |
303 | * the OOM killer while kthreadd is trying to allocate memory for | |
304 | * new kernel thread. | |
305 | */ | |
306 | if (unlikely(wait_for_completion_killable(&done))) { | |
307 | /* | |
308 | * If I was SIGKILLed before kthreadd (or new kernel thread) | |
309 | * calls complete(), leave the cleanup of this structure to | |
310 | * that thread. | |
311 | */ | |
312 | if (xchg(&create->done, NULL)) | |
8fe6929c | 313 | return ERR_PTR(-EINTR); |
786235ee TH |
314 | /* |
315 | * kthreadd (or new kernel thread) will call complete() | |
316 | * shortly. | |
317 | */ | |
318 | wait_for_completion(&done); | |
319 | } | |
320 | task = create->result; | |
321 | if (!IS_ERR(task)) { | |
c9b5f501 | 322 | static const struct sched_param param = { .sched_priority = 0 }; |
1c99315b | 323 | |
786235ee | 324 | vsnprintf(task->comm, sizeof(task->comm), namefmt, args); |
1c99315b ON |
325 | /* |
326 | * root may have changed our (kthreadd's) priority or CPU mask. | |
327 | * The kernel thread should not inherit these properties. | |
328 | */ | |
786235ee TH |
329 | sched_setscheduler_nocheck(task, SCHED_NORMAL, ¶m); |
330 | set_cpus_allowed_ptr(task, cpu_all_mask); | |
1da177e4 | 331 | } |
786235ee TH |
332 | kfree(create); |
333 | return task; | |
1da177e4 | 334 | } |
255451e4 PM |
335 | |
336 | /** | |
337 | * kthread_create_on_node - create a kthread. | |
338 | * @threadfn: the function to run until signal_pending(current). | |
339 | * @data: data ptr for @threadfn. | |
340 | * @node: task and thread structures for the thread are allocated on this node | |
341 | * @namefmt: printf-style name for the thread. | |
342 | * | |
343 | * Description: This helper function creates and names a kernel | |
344 | * thread. The thread will be stopped: use wake_up_process() to start | |
345 | * it. See also kthread_run(). The new thread has SCHED_NORMAL policy and | |
346 | * is affine to all CPUs. | |
347 | * | |
348 | * If thread is going to be bound on a particular cpu, give its node | |
349 | * in @node, to get NUMA affinity for kthread stack, or else give NUMA_NO_NODE. | |
350 | * When woken, the thread will run @threadfn() with @data as its | |
351 | * argument. @threadfn() can either call do_exit() directly if it is a | |
352 | * standalone thread for which no one will call kthread_stop(), or | |
353 | * return when 'kthread_should_stop()' is true (which means | |
354 | * kthread_stop() has been called). The return value should be zero | |
355 | * or a negative error number; it will be passed to kthread_stop(). | |
356 | * | |
357 | * Returns a task_struct or ERR_PTR(-ENOMEM) or ERR_PTR(-EINTR). | |
358 | */ | |
359 | struct task_struct *kthread_create_on_node(int (*threadfn)(void *data), | |
360 | void *data, int node, | |
361 | const char namefmt[], | |
362 | ...) | |
363 | { | |
364 | struct task_struct *task; | |
365 | va_list args; | |
366 | ||
367 | va_start(args, namefmt); | |
368 | task = __kthread_create_on_node(threadfn, data, node, namefmt, args); | |
369 | va_end(args); | |
370 | ||
371 | return task; | |
372 | } | |
207205a2 | 373 | EXPORT_SYMBOL(kthread_create_on_node); |
1da177e4 | 374 | |
25834c73 | 375 | static void __kthread_bind_mask(struct task_struct *p, const struct cpumask *mask, long state) |
2a1d4460 | 376 | { |
25834c73 PZ |
377 | unsigned long flags; |
378 | ||
f2530dc7 TG |
379 | if (!wait_task_inactive(p, state)) { |
380 | WARN_ON(1); | |
381 | return; | |
382 | } | |
25834c73 | 383 | |
2a1d4460 | 384 | /* It's safe because the task is inactive. */ |
25834c73 PZ |
385 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
386 | do_set_cpus_allowed(p, mask); | |
14a40ffc | 387 | p->flags |= PF_NO_SETAFFINITY; |
25834c73 PZ |
388 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
389 | } | |
390 | ||
391 | static void __kthread_bind(struct task_struct *p, unsigned int cpu, long state) | |
392 | { | |
393 | __kthread_bind_mask(p, cpumask_of(cpu), state); | |
394 | } | |
395 | ||
396 | void kthread_bind_mask(struct task_struct *p, const struct cpumask *mask) | |
397 | { | |
398 | __kthread_bind_mask(p, mask, TASK_UNINTERRUPTIBLE); | |
2a1d4460 TG |
399 | } |
400 | ||
881232b7 PZ |
401 | /** |
402 | * kthread_bind - bind a just-created kthread to a cpu. | |
403 | * @p: thread created by kthread_create(). | |
404 | * @cpu: cpu (might not be online, must be possible) for @k to run on. | |
405 | * | |
406 | * Description: This function is equivalent to set_cpus_allowed(), | |
407 | * except that @cpu doesn't need to be online, and the thread must be | |
408 | * stopped (i.e., just returned from kthread_create()). | |
409 | */ | |
410 | void kthread_bind(struct task_struct *p, unsigned int cpu) | |
411 | { | |
f2530dc7 | 412 | __kthread_bind(p, cpu, TASK_UNINTERRUPTIBLE); |
881232b7 PZ |
413 | } |
414 | EXPORT_SYMBOL(kthread_bind); | |
415 | ||
2a1d4460 TG |
416 | /** |
417 | * kthread_create_on_cpu - Create a cpu bound kthread | |
418 | * @threadfn: the function to run until signal_pending(current). | |
419 | * @data: data ptr for @threadfn. | |
420 | * @cpu: The cpu on which the thread should be bound, | |
421 | * @namefmt: printf-style name for the thread. Format is restricted | |
422 | * to "name.*%u". Code fills in cpu number. | |
423 | * | |
424 | * Description: This helper function creates and names a kernel thread | |
425 | * The thread will be woken and put into park mode. | |
426 | */ | |
427 | struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data), | |
428 | void *data, unsigned int cpu, | |
429 | const char *namefmt) | |
430 | { | |
431 | struct task_struct *p; | |
432 | ||
10922838 | 433 | p = kthread_create_on_node(threadfn, data, cpu_to_node(cpu), namefmt, |
2a1d4460 TG |
434 | cpu); |
435 | if (IS_ERR(p)) | |
436 | return p; | |
a65d4096 PM |
437 | kthread_bind(p, cpu); |
438 | /* CPU hotplug need to bind once again when unparking the thread. */ | |
2a1d4460 TG |
439 | set_bit(KTHREAD_IS_PER_CPU, &to_kthread(p)->flags); |
440 | to_kthread(p)->cpu = cpu; | |
2a1d4460 TG |
441 | return p; |
442 | } | |
443 | ||
f2530dc7 TG |
444 | static void __kthread_unpark(struct task_struct *k, struct kthread *kthread) |
445 | { | |
446 | clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags); | |
447 | /* | |
448 | * We clear the IS_PARKED bit here as we don't wait | |
449 | * until the task has left the park code. So if we'd | |
450 | * park before that happens we'd see the IS_PARKED bit | |
451 | * which might be about to be cleared. | |
452 | */ | |
453 | if (test_and_clear_bit(KTHREAD_IS_PARKED, &kthread->flags)) { | |
a65d4096 PM |
454 | /* |
455 | * Newly created kthread was parked when the CPU was offline. | |
456 | * The binding was lost and we need to set it again. | |
457 | */ | |
f2530dc7 TG |
458 | if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags)) |
459 | __kthread_bind(k, kthread->cpu, TASK_PARKED); | |
460 | wake_up_state(k, TASK_PARKED); | |
461 | } | |
462 | } | |
463 | ||
2a1d4460 TG |
464 | /** |
465 | * kthread_unpark - unpark a thread created by kthread_create(). | |
466 | * @k: thread created by kthread_create(). | |
467 | * | |
468 | * Sets kthread_should_park() for @k to return false, wakes it, and | |
469 | * waits for it to return. If the thread is marked percpu then its | |
470 | * bound to the cpu again. | |
471 | */ | |
472 | void kthread_unpark(struct task_struct *k) | |
473 | { | |
b5c5442b | 474 | struct kthread *kthread = to_live_kthread(k); |
2a1d4460 | 475 | |
eff96625 | 476 | if (kthread) |
f2530dc7 | 477 | __kthread_unpark(k, kthread); |
2a1d4460 | 478 | } |
18896451 | 479 | EXPORT_SYMBOL_GPL(kthread_unpark); |
2a1d4460 TG |
480 | |
481 | /** | |
482 | * kthread_park - park a thread created by kthread_create(). | |
483 | * @k: thread created by kthread_create(). | |
484 | * | |
485 | * Sets kthread_should_park() for @k to return true, wakes it, and | |
486 | * waits for it to return. This can also be called after kthread_create() | |
487 | * instead of calling wake_up_process(): the thread will park without | |
488 | * calling threadfn(). | |
489 | * | |
490 | * Returns 0 if the thread is parked, -ENOSYS if the thread exited. | |
491 | * If called by the kthread itself just the park bit is set. | |
492 | */ | |
493 | int kthread_park(struct task_struct *k) | |
494 | { | |
b5c5442b | 495 | struct kthread *kthread = to_live_kthread(k); |
2a1d4460 TG |
496 | int ret = -ENOSYS; |
497 | ||
498 | if (kthread) { | |
499 | if (!test_bit(KTHREAD_IS_PARKED, &kthread->flags)) { | |
500 | set_bit(KTHREAD_SHOULD_PARK, &kthread->flags); | |
501 | if (k != current) { | |
502 | wake_up_process(k); | |
503 | wait_for_completion(&kthread->parked); | |
504 | } | |
505 | } | |
506 | ret = 0; | |
507 | } | |
2a1d4460 TG |
508 | return ret; |
509 | } | |
18896451 | 510 | EXPORT_SYMBOL_GPL(kthread_park); |
2a1d4460 | 511 | |
9e37bd30 RD |
512 | /** |
513 | * kthread_stop - stop a thread created by kthread_create(). | |
514 | * @k: thread created by kthread_create(). | |
515 | * | |
516 | * Sets kthread_should_stop() for @k to return true, wakes it, and | |
9ae26027 ON |
517 | * waits for it to exit. This can also be called after kthread_create() |
518 | * instead of calling wake_up_process(): the thread will exit without | |
519 | * calling threadfn(). | |
520 | * | |
521 | * If threadfn() may call do_exit() itself, the caller must ensure | |
522 | * task_struct can't go away. | |
9e37bd30 RD |
523 | * |
524 | * Returns the result of threadfn(), or %-EINTR if wake_up_process() | |
525 | * was never called. | |
526 | */ | |
1da177e4 LT |
527 | int kthread_stop(struct task_struct *k) |
528 | { | |
b5c5442b | 529 | struct kthread *kthread; |
1da177e4 LT |
530 | int ret; |
531 | ||
0a16b607 | 532 | trace_sched_kthread_stop(k); |
b5c5442b ON |
533 | |
534 | get_task_struct(k); | |
535 | kthread = to_live_kthread(k); | |
2a1d4460 TG |
536 | if (kthread) { |
537 | set_bit(KTHREAD_SHOULD_STOP, &kthread->flags); | |
f2530dc7 | 538 | __kthread_unpark(k, kthread); |
63706172 ON |
539 | wake_up_process(k); |
540 | wait_for_completion(&kthread->exited); | |
541 | } | |
542 | ret = k->exit_code; | |
1da177e4 | 543 | put_task_struct(k); |
0a16b607 | 544 | |
b5c5442b | 545 | trace_sched_kthread_stop_ret(ret); |
1da177e4 LT |
546 | return ret; |
547 | } | |
52e92e57 | 548 | EXPORT_SYMBOL(kthread_stop); |
1da177e4 | 549 | |
e804a4a4 | 550 | int kthreadd(void *unused) |
1da177e4 | 551 | { |
73c27992 | 552 | struct task_struct *tsk = current; |
1da177e4 | 553 | |
e804a4a4 | 554 | /* Setup a clean context for our children to inherit. */ |
73c27992 | 555 | set_task_comm(tsk, "kthreadd"); |
10ab825b | 556 | ignore_signals(tsk); |
1a2142af | 557 | set_cpus_allowed_ptr(tsk, cpu_all_mask); |
aee4faa4 | 558 | set_mems_allowed(node_states[N_MEMORY]); |
73c27992 | 559 | |
34b087e4 | 560 | current->flags |= PF_NOFREEZE; |
73c27992 EB |
561 | |
562 | for (;;) { | |
563 | set_current_state(TASK_INTERRUPTIBLE); | |
564 | if (list_empty(&kthread_create_list)) | |
565 | schedule(); | |
566 | __set_current_state(TASK_RUNNING); | |
567 | ||
568 | spin_lock(&kthread_create_lock); | |
569 | while (!list_empty(&kthread_create_list)) { | |
570 | struct kthread_create_info *create; | |
571 | ||
572 | create = list_entry(kthread_create_list.next, | |
573 | struct kthread_create_info, list); | |
574 | list_del_init(&create->list); | |
575 | spin_unlock(&kthread_create_lock); | |
576 | ||
577 | create_kthread(create); | |
578 | ||
579 | spin_lock(&kthread_create_lock); | |
580 | } | |
581 | spin_unlock(&kthread_create_lock); | |
582 | } | |
583 | ||
584 | return 0; | |
585 | } | |
b56c0d89 | 586 | |
3989144f | 587 | void __kthread_init_worker(struct kthread_worker *worker, |
4f32e9b1 YZ |
588 | const char *name, |
589 | struct lock_class_key *key) | |
590 | { | |
dbf52682 | 591 | memset(worker, 0, sizeof(struct kthread_worker)); |
4f32e9b1 YZ |
592 | spin_lock_init(&worker->lock); |
593 | lockdep_set_class_and_name(&worker->lock, key, name); | |
594 | INIT_LIST_HEAD(&worker->work_list); | |
22597dc3 | 595 | INIT_LIST_HEAD(&worker->delayed_work_list); |
4f32e9b1 | 596 | } |
3989144f | 597 | EXPORT_SYMBOL_GPL(__kthread_init_worker); |
4f32e9b1 | 598 | |
b56c0d89 TH |
599 | /** |
600 | * kthread_worker_fn - kthread function to process kthread_worker | |
601 | * @worker_ptr: pointer to initialized kthread_worker | |
602 | * | |
fbae2d44 PM |
603 | * This function implements the main cycle of kthread worker. It processes |
604 | * work_list until it is stopped with kthread_stop(). It sleeps when the queue | |
605 | * is empty. | |
b56c0d89 | 606 | * |
fbae2d44 PM |
607 | * The works are not allowed to keep any locks, disable preemption or interrupts |
608 | * when they finish. There is defined a safe point for freezing when one work | |
609 | * finishes and before a new one is started. | |
8197b3d4 PM |
610 | * |
611 | * Also the works must not be handled by more than one worker at the same time, | |
612 | * see also kthread_queue_work(). | |
b56c0d89 TH |
613 | */ |
614 | int kthread_worker_fn(void *worker_ptr) | |
615 | { | |
616 | struct kthread_worker *worker = worker_ptr; | |
617 | struct kthread_work *work; | |
618 | ||
fbae2d44 PM |
619 | /* |
620 | * FIXME: Update the check and remove the assignment when all kthread | |
621 | * worker users are created using kthread_create_worker*() functions. | |
622 | */ | |
623 | WARN_ON(worker->task && worker->task != current); | |
b56c0d89 | 624 | worker->task = current; |
dbf52682 PM |
625 | |
626 | if (worker->flags & KTW_FREEZABLE) | |
627 | set_freezable(); | |
628 | ||
b56c0d89 TH |
629 | repeat: |
630 | set_current_state(TASK_INTERRUPTIBLE); /* mb paired w/ kthread_stop */ | |
631 | ||
632 | if (kthread_should_stop()) { | |
633 | __set_current_state(TASK_RUNNING); | |
634 | spin_lock_irq(&worker->lock); | |
635 | worker->task = NULL; | |
636 | spin_unlock_irq(&worker->lock); | |
637 | return 0; | |
638 | } | |
639 | ||
640 | work = NULL; | |
641 | spin_lock_irq(&worker->lock); | |
642 | if (!list_empty(&worker->work_list)) { | |
643 | work = list_first_entry(&worker->work_list, | |
644 | struct kthread_work, node); | |
645 | list_del_init(&work->node); | |
646 | } | |
46f3d976 | 647 | worker->current_work = work; |
b56c0d89 TH |
648 | spin_unlock_irq(&worker->lock); |
649 | ||
650 | if (work) { | |
651 | __set_current_state(TASK_RUNNING); | |
652 | work->func(work); | |
b56c0d89 TH |
653 | } else if (!freezing(current)) |
654 | schedule(); | |
655 | ||
656 | try_to_freeze(); | |
657 | goto repeat; | |
658 | } | |
659 | EXPORT_SYMBOL_GPL(kthread_worker_fn); | |
660 | ||
fbae2d44 | 661 | static struct kthread_worker * |
dbf52682 PM |
662 | __kthread_create_worker(int cpu, unsigned int flags, |
663 | const char namefmt[], va_list args) | |
fbae2d44 PM |
664 | { |
665 | struct kthread_worker *worker; | |
666 | struct task_struct *task; | |
667 | ||
668 | worker = kzalloc(sizeof(*worker), GFP_KERNEL); | |
669 | if (!worker) | |
670 | return ERR_PTR(-ENOMEM); | |
671 | ||
672 | kthread_init_worker(worker); | |
673 | ||
674 | if (cpu >= 0) { | |
675 | char name[TASK_COMM_LEN]; | |
676 | ||
677 | /* | |
678 | * kthread_create_worker_on_cpu() allows to pass a generic | |
679 | * namefmt in compare with kthread_create_on_cpu. We need | |
680 | * to format it here. | |
681 | */ | |
682 | vsnprintf(name, sizeof(name), namefmt, args); | |
683 | task = kthread_create_on_cpu(kthread_worker_fn, worker, | |
684 | cpu, name); | |
685 | } else { | |
686 | task = __kthread_create_on_node(kthread_worker_fn, worker, | |
687 | -1, namefmt, args); | |
688 | } | |
689 | ||
690 | if (IS_ERR(task)) | |
691 | goto fail_task; | |
692 | ||
dbf52682 | 693 | worker->flags = flags; |
fbae2d44 PM |
694 | worker->task = task; |
695 | wake_up_process(task); | |
696 | return worker; | |
697 | ||
698 | fail_task: | |
699 | kfree(worker); | |
700 | return ERR_CAST(task); | |
701 | } | |
702 | ||
703 | /** | |
704 | * kthread_create_worker - create a kthread worker | |
dbf52682 | 705 | * @flags: flags modifying the default behavior of the worker |
fbae2d44 PM |
706 | * @namefmt: printf-style name for the kthread worker (task). |
707 | * | |
708 | * Returns a pointer to the allocated worker on success, ERR_PTR(-ENOMEM) | |
709 | * when the needed structures could not get allocated, and ERR_PTR(-EINTR) | |
710 | * when the worker was SIGKILLed. | |
711 | */ | |
712 | struct kthread_worker * | |
dbf52682 | 713 | kthread_create_worker(unsigned int flags, const char namefmt[], ...) |
fbae2d44 PM |
714 | { |
715 | struct kthread_worker *worker; | |
716 | va_list args; | |
717 | ||
718 | va_start(args, namefmt); | |
dbf52682 | 719 | worker = __kthread_create_worker(-1, flags, namefmt, args); |
fbae2d44 PM |
720 | va_end(args); |
721 | ||
722 | return worker; | |
723 | } | |
724 | EXPORT_SYMBOL(kthread_create_worker); | |
725 | ||
726 | /** | |
727 | * kthread_create_worker_on_cpu - create a kthread worker and bind it | |
728 | * it to a given CPU and the associated NUMA node. | |
729 | * @cpu: CPU number | |
dbf52682 | 730 | * @flags: flags modifying the default behavior of the worker |
fbae2d44 PM |
731 | * @namefmt: printf-style name for the kthread worker (task). |
732 | * | |
733 | * Use a valid CPU number if you want to bind the kthread worker | |
734 | * to the given CPU and the associated NUMA node. | |
735 | * | |
736 | * A good practice is to add the cpu number also into the worker name. | |
737 | * For example, use kthread_create_worker_on_cpu(cpu, "helper/%d", cpu). | |
738 | * | |
739 | * Returns a pointer to the allocated worker on success, ERR_PTR(-ENOMEM) | |
740 | * when the needed structures could not get allocated, and ERR_PTR(-EINTR) | |
741 | * when the worker was SIGKILLed. | |
742 | */ | |
743 | struct kthread_worker * | |
dbf52682 PM |
744 | kthread_create_worker_on_cpu(int cpu, unsigned int flags, |
745 | const char namefmt[], ...) | |
fbae2d44 PM |
746 | { |
747 | struct kthread_worker *worker; | |
748 | va_list args; | |
749 | ||
750 | va_start(args, namefmt); | |
dbf52682 | 751 | worker = __kthread_create_worker(cpu, flags, namefmt, args); |
fbae2d44 PM |
752 | va_end(args); |
753 | ||
754 | return worker; | |
755 | } | |
756 | EXPORT_SYMBOL(kthread_create_worker_on_cpu); | |
757 | ||
37be45d4 PM |
758 | /* |
759 | * Returns true when the work could not be queued at the moment. | |
760 | * It happens when it is already pending in a worker list | |
761 | * or when it is being cancelled. | |
762 | */ | |
763 | static inline bool queuing_blocked(struct kthread_worker *worker, | |
764 | struct kthread_work *work) | |
765 | { | |
766 | lockdep_assert_held(&worker->lock); | |
767 | ||
768 | return !list_empty(&work->node) || work->canceling; | |
769 | } | |
770 | ||
8197b3d4 PM |
771 | static void kthread_insert_work_sanity_check(struct kthread_worker *worker, |
772 | struct kthread_work *work) | |
773 | { | |
774 | lockdep_assert_held(&worker->lock); | |
775 | WARN_ON_ONCE(!list_empty(&work->node)); | |
776 | /* Do not use a work with >1 worker, see kthread_queue_work() */ | |
777 | WARN_ON_ONCE(work->worker && work->worker != worker); | |
778 | } | |
779 | ||
9a2e03d8 | 780 | /* insert @work before @pos in @worker */ |
3989144f | 781 | static void kthread_insert_work(struct kthread_worker *worker, |
8197b3d4 PM |
782 | struct kthread_work *work, |
783 | struct list_head *pos) | |
9a2e03d8 | 784 | { |
8197b3d4 | 785 | kthread_insert_work_sanity_check(worker, work); |
9a2e03d8 TH |
786 | |
787 | list_add_tail(&work->node, pos); | |
46f3d976 | 788 | work->worker = worker; |
ed1403ec | 789 | if (!worker->current_work && likely(worker->task)) |
9a2e03d8 TH |
790 | wake_up_process(worker->task); |
791 | } | |
792 | ||
b56c0d89 | 793 | /** |
3989144f | 794 | * kthread_queue_work - queue a kthread_work |
b56c0d89 TH |
795 | * @worker: target kthread_worker |
796 | * @work: kthread_work to queue | |
797 | * | |
798 | * Queue @work to work processor @task for async execution. @task | |
799 | * must have been created with kthread_worker_create(). Returns %true | |
800 | * if @work was successfully queued, %false if it was already pending. | |
8197b3d4 PM |
801 | * |
802 | * Reinitialize the work if it needs to be used by another worker. | |
803 | * For example, when the worker was stopped and started again. | |
b56c0d89 | 804 | */ |
3989144f | 805 | bool kthread_queue_work(struct kthread_worker *worker, |
b56c0d89 TH |
806 | struct kthread_work *work) |
807 | { | |
808 | bool ret = false; | |
809 | unsigned long flags; | |
810 | ||
811 | spin_lock_irqsave(&worker->lock, flags); | |
37be45d4 | 812 | if (!queuing_blocked(worker, work)) { |
3989144f | 813 | kthread_insert_work(worker, work, &worker->work_list); |
b56c0d89 TH |
814 | ret = true; |
815 | } | |
816 | spin_unlock_irqrestore(&worker->lock, flags); | |
817 | return ret; | |
818 | } | |
3989144f | 819 | EXPORT_SYMBOL_GPL(kthread_queue_work); |
b56c0d89 | 820 | |
22597dc3 PM |
821 | /** |
822 | * kthread_delayed_work_timer_fn - callback that queues the associated kthread | |
823 | * delayed work when the timer expires. | |
824 | * @__data: pointer to the data associated with the timer | |
825 | * | |
826 | * The format of the function is defined by struct timer_list. | |
827 | * It should have been called from irqsafe timer with irq already off. | |
828 | */ | |
829 | void kthread_delayed_work_timer_fn(unsigned long __data) | |
830 | { | |
831 | struct kthread_delayed_work *dwork = | |
832 | (struct kthread_delayed_work *)__data; | |
833 | struct kthread_work *work = &dwork->work; | |
834 | struct kthread_worker *worker = work->worker; | |
835 | ||
836 | /* | |
837 | * This might happen when a pending work is reinitialized. | |
838 | * It means that it is used a wrong way. | |
839 | */ | |
840 | if (WARN_ON_ONCE(!worker)) | |
841 | return; | |
842 | ||
843 | spin_lock(&worker->lock); | |
844 | /* Work must not be used with >1 worker, see kthread_queue_work(). */ | |
845 | WARN_ON_ONCE(work->worker != worker); | |
846 | ||
847 | /* Move the work from worker->delayed_work_list. */ | |
848 | WARN_ON_ONCE(list_empty(&work->node)); | |
849 | list_del_init(&work->node); | |
850 | kthread_insert_work(worker, work, &worker->work_list); | |
851 | ||
852 | spin_unlock(&worker->lock); | |
853 | } | |
854 | EXPORT_SYMBOL(kthread_delayed_work_timer_fn); | |
855 | ||
856 | void __kthread_queue_delayed_work(struct kthread_worker *worker, | |
857 | struct kthread_delayed_work *dwork, | |
858 | unsigned long delay) | |
859 | { | |
860 | struct timer_list *timer = &dwork->timer; | |
861 | struct kthread_work *work = &dwork->work; | |
862 | ||
863 | WARN_ON_ONCE(timer->function != kthread_delayed_work_timer_fn || | |
864 | timer->data != (unsigned long)dwork); | |
865 | ||
866 | /* | |
867 | * If @delay is 0, queue @dwork->work immediately. This is for | |
868 | * both optimization and correctness. The earliest @timer can | |
869 | * expire is on the closest next tick and delayed_work users depend | |
870 | * on that there's no such delay when @delay is 0. | |
871 | */ | |
872 | if (!delay) { | |
873 | kthread_insert_work(worker, work, &worker->work_list); | |
874 | return; | |
875 | } | |
876 | ||
877 | /* Be paranoid and try to detect possible races already now. */ | |
878 | kthread_insert_work_sanity_check(worker, work); | |
879 | ||
880 | list_add(&work->node, &worker->delayed_work_list); | |
881 | work->worker = worker; | |
882 | timer_stats_timer_set_start_info(&dwork->timer); | |
883 | timer->expires = jiffies + delay; | |
884 | add_timer(timer); | |
885 | } | |
886 | ||
887 | /** | |
888 | * kthread_queue_delayed_work - queue the associated kthread work | |
889 | * after a delay. | |
890 | * @worker: target kthread_worker | |
891 | * @dwork: kthread_delayed_work to queue | |
892 | * @delay: number of jiffies to wait before queuing | |
893 | * | |
894 | * If the work has not been pending it starts a timer that will queue | |
895 | * the work after the given @delay. If @delay is zero, it queues the | |
896 | * work immediately. | |
897 | * | |
898 | * Return: %false if the @work has already been pending. It means that | |
899 | * either the timer was running or the work was queued. It returns %true | |
900 | * otherwise. | |
901 | */ | |
902 | bool kthread_queue_delayed_work(struct kthread_worker *worker, | |
903 | struct kthread_delayed_work *dwork, | |
904 | unsigned long delay) | |
905 | { | |
906 | struct kthread_work *work = &dwork->work; | |
907 | unsigned long flags; | |
908 | bool ret = false; | |
909 | ||
910 | spin_lock_irqsave(&worker->lock, flags); | |
911 | ||
37be45d4 | 912 | if (!queuing_blocked(worker, work)) { |
22597dc3 PM |
913 | __kthread_queue_delayed_work(worker, dwork, delay); |
914 | ret = true; | |
915 | } | |
916 | ||
917 | spin_unlock_irqrestore(&worker->lock, flags); | |
918 | return ret; | |
919 | } | |
920 | EXPORT_SYMBOL_GPL(kthread_queue_delayed_work); | |
921 | ||
9a2e03d8 TH |
922 | struct kthread_flush_work { |
923 | struct kthread_work work; | |
924 | struct completion done; | |
925 | }; | |
926 | ||
927 | static void kthread_flush_work_fn(struct kthread_work *work) | |
928 | { | |
929 | struct kthread_flush_work *fwork = | |
930 | container_of(work, struct kthread_flush_work, work); | |
931 | complete(&fwork->done); | |
932 | } | |
933 | ||
b56c0d89 | 934 | /** |
3989144f | 935 | * kthread_flush_work - flush a kthread_work |
b56c0d89 TH |
936 | * @work: work to flush |
937 | * | |
938 | * If @work is queued or executing, wait for it to finish execution. | |
939 | */ | |
3989144f | 940 | void kthread_flush_work(struct kthread_work *work) |
b56c0d89 | 941 | { |
46f3d976 TH |
942 | struct kthread_flush_work fwork = { |
943 | KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn), | |
944 | COMPLETION_INITIALIZER_ONSTACK(fwork.done), | |
945 | }; | |
946 | struct kthread_worker *worker; | |
947 | bool noop = false; | |
948 | ||
46f3d976 TH |
949 | worker = work->worker; |
950 | if (!worker) | |
951 | return; | |
b56c0d89 | 952 | |
46f3d976 | 953 | spin_lock_irq(&worker->lock); |
8197b3d4 PM |
954 | /* Work must not be used with >1 worker, see kthread_queue_work(). */ |
955 | WARN_ON_ONCE(work->worker != worker); | |
b56c0d89 | 956 | |
46f3d976 | 957 | if (!list_empty(&work->node)) |
3989144f | 958 | kthread_insert_work(worker, &fwork.work, work->node.next); |
46f3d976 | 959 | else if (worker->current_work == work) |
3989144f PM |
960 | kthread_insert_work(worker, &fwork.work, |
961 | worker->work_list.next); | |
46f3d976 TH |
962 | else |
963 | noop = true; | |
b56c0d89 | 964 | |
46f3d976 | 965 | spin_unlock_irq(&worker->lock); |
b56c0d89 | 966 | |
46f3d976 TH |
967 | if (!noop) |
968 | wait_for_completion(&fwork.done); | |
b56c0d89 | 969 | } |
3989144f | 970 | EXPORT_SYMBOL_GPL(kthread_flush_work); |
b56c0d89 | 971 | |
37be45d4 PM |
972 | /* |
973 | * This function removes the work from the worker queue. Also it makes sure | |
974 | * that it won't get queued later via the delayed work's timer. | |
975 | * | |
976 | * The work might still be in use when this function finishes. See the | |
977 | * current_work proceed by the worker. | |
978 | * | |
979 | * Return: %true if @work was pending and successfully canceled, | |
980 | * %false if @work was not pending | |
981 | */ | |
982 | static bool __kthread_cancel_work(struct kthread_work *work, bool is_dwork, | |
983 | unsigned long *flags) | |
984 | { | |
985 | /* Try to cancel the timer if exists. */ | |
986 | if (is_dwork) { | |
987 | struct kthread_delayed_work *dwork = | |
988 | container_of(work, struct kthread_delayed_work, work); | |
989 | struct kthread_worker *worker = work->worker; | |
990 | ||
991 | /* | |
992 | * del_timer_sync() must be called to make sure that the timer | |
993 | * callback is not running. The lock must be temporary released | |
994 | * to avoid a deadlock with the callback. In the meantime, | |
995 | * any queuing is blocked by setting the canceling counter. | |
996 | */ | |
997 | work->canceling++; | |
998 | spin_unlock_irqrestore(&worker->lock, *flags); | |
999 | del_timer_sync(&dwork->timer); | |
1000 | spin_lock_irqsave(&worker->lock, *flags); | |
1001 | work->canceling--; | |
1002 | } | |
1003 | ||
1004 | /* | |
1005 | * Try to remove the work from a worker list. It might either | |
1006 | * be from worker->work_list or from worker->delayed_work_list. | |
1007 | */ | |
1008 | if (!list_empty(&work->node)) { | |
1009 | list_del_init(&work->node); | |
1010 | return true; | |
1011 | } | |
1012 | ||
1013 | return false; | |
1014 | } | |
1015 | ||
9a6b06c8 PM |
1016 | /** |
1017 | * kthread_mod_delayed_work - modify delay of or queue a kthread delayed work | |
1018 | * @worker: kthread worker to use | |
1019 | * @dwork: kthread delayed work to queue | |
1020 | * @delay: number of jiffies to wait before queuing | |
1021 | * | |
1022 | * If @dwork is idle, equivalent to kthread_queue_delayed_work(). Otherwise, | |
1023 | * modify @dwork's timer so that it expires after @delay. If @delay is zero, | |
1024 | * @work is guaranteed to be queued immediately. | |
1025 | * | |
1026 | * Return: %true if @dwork was pending and its timer was modified, | |
1027 | * %false otherwise. | |
1028 | * | |
1029 | * A special case is when the work is being canceled in parallel. | |
1030 | * It might be caused either by the real kthread_cancel_delayed_work_sync() | |
1031 | * or yet another kthread_mod_delayed_work() call. We let the other command | |
1032 | * win and return %false here. The caller is supposed to synchronize these | |
1033 | * operations a reasonable way. | |
1034 | * | |
1035 | * This function is safe to call from any context including IRQ handler. | |
1036 | * See __kthread_cancel_work() and kthread_delayed_work_timer_fn() | |
1037 | * for details. | |
1038 | */ | |
1039 | bool kthread_mod_delayed_work(struct kthread_worker *worker, | |
1040 | struct kthread_delayed_work *dwork, | |
1041 | unsigned long delay) | |
1042 | { | |
1043 | struct kthread_work *work = &dwork->work; | |
1044 | unsigned long flags; | |
1045 | int ret = false; | |
1046 | ||
1047 | spin_lock_irqsave(&worker->lock, flags); | |
1048 | ||
1049 | /* Do not bother with canceling when never queued. */ | |
1050 | if (!work->worker) | |
1051 | goto fast_queue; | |
1052 | ||
1053 | /* Work must not be used with >1 worker, see kthread_queue_work() */ | |
1054 | WARN_ON_ONCE(work->worker != worker); | |
1055 | ||
1056 | /* Do not fight with another command that is canceling this work. */ | |
1057 | if (work->canceling) | |
1058 | goto out; | |
1059 | ||
1060 | ret = __kthread_cancel_work(work, true, &flags); | |
1061 | fast_queue: | |
1062 | __kthread_queue_delayed_work(worker, dwork, delay); | |
1063 | out: | |
1064 | spin_unlock_irqrestore(&worker->lock, flags); | |
1065 | return ret; | |
1066 | } | |
1067 | EXPORT_SYMBOL_GPL(kthread_mod_delayed_work); | |
1068 | ||
37be45d4 PM |
1069 | static bool __kthread_cancel_work_sync(struct kthread_work *work, bool is_dwork) |
1070 | { | |
1071 | struct kthread_worker *worker = work->worker; | |
1072 | unsigned long flags; | |
1073 | int ret = false; | |
1074 | ||
1075 | if (!worker) | |
1076 | goto out; | |
1077 | ||
1078 | spin_lock_irqsave(&worker->lock, flags); | |
1079 | /* Work must not be used with >1 worker, see kthread_queue_work(). */ | |
1080 | WARN_ON_ONCE(work->worker != worker); | |
1081 | ||
1082 | ret = __kthread_cancel_work(work, is_dwork, &flags); | |
1083 | ||
1084 | if (worker->current_work != work) | |
1085 | goto out_fast; | |
1086 | ||
1087 | /* | |
1088 | * The work is in progress and we need to wait with the lock released. | |
1089 | * In the meantime, block any queuing by setting the canceling counter. | |
1090 | */ | |
1091 | work->canceling++; | |
1092 | spin_unlock_irqrestore(&worker->lock, flags); | |
1093 | kthread_flush_work(work); | |
1094 | spin_lock_irqsave(&worker->lock, flags); | |
1095 | work->canceling--; | |
1096 | ||
1097 | out_fast: | |
1098 | spin_unlock_irqrestore(&worker->lock, flags); | |
1099 | out: | |
1100 | return ret; | |
1101 | } | |
1102 | ||
1103 | /** | |
1104 | * kthread_cancel_work_sync - cancel a kthread work and wait for it to finish | |
1105 | * @work: the kthread work to cancel | |
1106 | * | |
1107 | * Cancel @work and wait for its execution to finish. This function | |
1108 | * can be used even if the work re-queues itself. On return from this | |
1109 | * function, @work is guaranteed to be not pending or executing on any CPU. | |
1110 | * | |
1111 | * kthread_cancel_work_sync(&delayed_work->work) must not be used for | |
1112 | * delayed_work's. Use kthread_cancel_delayed_work_sync() instead. | |
1113 | * | |
1114 | * The caller must ensure that the worker on which @work was last | |
1115 | * queued can't be destroyed before this function returns. | |
1116 | * | |
1117 | * Return: %true if @work was pending, %false otherwise. | |
1118 | */ | |
1119 | bool kthread_cancel_work_sync(struct kthread_work *work) | |
1120 | { | |
1121 | return __kthread_cancel_work_sync(work, false); | |
1122 | } | |
1123 | EXPORT_SYMBOL_GPL(kthread_cancel_work_sync); | |
1124 | ||
1125 | /** | |
1126 | * kthread_cancel_delayed_work_sync - cancel a kthread delayed work and | |
1127 | * wait for it to finish. | |
1128 | * @dwork: the kthread delayed work to cancel | |
1129 | * | |
1130 | * This is kthread_cancel_work_sync() for delayed works. | |
1131 | * | |
1132 | * Return: %true if @dwork was pending, %false otherwise. | |
1133 | */ | |
1134 | bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *dwork) | |
1135 | { | |
1136 | return __kthread_cancel_work_sync(&dwork->work, true); | |
1137 | } | |
1138 | EXPORT_SYMBOL_GPL(kthread_cancel_delayed_work_sync); | |
1139 | ||
b56c0d89 | 1140 | /** |
3989144f | 1141 | * kthread_flush_worker - flush all current works on a kthread_worker |
b56c0d89 TH |
1142 | * @worker: worker to flush |
1143 | * | |
1144 | * Wait until all currently executing or pending works on @worker are | |
1145 | * finished. | |
1146 | */ | |
3989144f | 1147 | void kthread_flush_worker(struct kthread_worker *worker) |
b56c0d89 TH |
1148 | { |
1149 | struct kthread_flush_work fwork = { | |
1150 | KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn), | |
1151 | COMPLETION_INITIALIZER_ONSTACK(fwork.done), | |
1152 | }; | |
1153 | ||
3989144f | 1154 | kthread_queue_work(worker, &fwork.work); |
b56c0d89 TH |
1155 | wait_for_completion(&fwork.done); |
1156 | } | |
3989144f | 1157 | EXPORT_SYMBOL_GPL(kthread_flush_worker); |
35033fe9 PM |
1158 | |
1159 | /** | |
1160 | * kthread_destroy_worker - destroy a kthread worker | |
1161 | * @worker: worker to be destroyed | |
1162 | * | |
1163 | * Flush and destroy @worker. The simple flush is enough because the kthread | |
1164 | * worker API is used only in trivial scenarios. There are no multi-step state | |
1165 | * machines needed. | |
1166 | */ | |
1167 | void kthread_destroy_worker(struct kthread_worker *worker) | |
1168 | { | |
1169 | struct task_struct *task; | |
1170 | ||
1171 | task = worker->task; | |
1172 | if (WARN_ON(!task)) | |
1173 | return; | |
1174 | ||
1175 | kthread_flush_worker(worker); | |
1176 | kthread_stop(task); | |
1177 | WARN_ON(!list_empty(&worker->work_list)); | |
1178 | kfree(worker); | |
1179 | } | |
1180 | EXPORT_SYMBOL(kthread_destroy_worker); |