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