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