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