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