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