1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Kernel thread helper functions.
3 * Copyright (C) 2004 IBM Corporation, Rusty Russell.
4 * Copyright (C) 2009 Red Hat, Inc.
6 * Creation is done via kthreadd, so that we get a clean environment
7 * even if we're invoked from userspace (think modprobe, hotplug cpu,
10 #include <uapi/linux/sched/types.h>
12 #include <linux/mmu_context.h>
13 #include <linux/sched.h>
14 #include <linux/sched/mm.h>
15 #include <linux/sched/task.h>
16 #include <linux/kthread.h>
17 #include <linux/completion.h>
18 #include <linux/err.h>
19 #include <linux/cgroup.h>
20 #include <linux/cpuset.h>
21 #include <linux/unistd.h>
22 #include <linux/file.h>
23 #include <linux/export.h>
24 #include <linux/mutex.h>
25 #include <linux/slab.h>
26 #include <linux/freezer.h>
27 #include <linux/ptrace.h>
28 #include <linux/uaccess.h>
29 #include <linux/numa.h>
30 #include <linux/sched/isolation.h>
31 #include <trace/events/sched.h>
34 static DEFINE_SPINLOCK(kthread_create_lock);
35 static LIST_HEAD(kthread_create_list);
36 struct task_struct *kthreadd_task;
38 struct kthread_create_info
40 /* Information passed to kthread() from kthreadd. */
42 int (*threadfn)(void *data);
46 /* Result passed back to kthread_create() from kthreadd. */
47 struct task_struct *result;
48 struct completion *done;
50 struct list_head list;
57 int (*threadfn)(void *);
59 struct completion parked;
60 struct completion exited;
61 #ifdef CONFIG_BLK_CGROUP
62 struct cgroup_subsys_state *blkcg_css;
64 /* To store the full name if task comm is truncated. */
69 KTHREAD_IS_PER_CPU = 0,
74 static inline struct kthread *to_kthread(struct task_struct *k)
76 WARN_ON(!(k->flags & PF_KTHREAD));
77 return k->worker_private;
81 * Variant of to_kthread() that doesn't assume @p is a kthread.
83 * Per construction; when:
85 * (p->flags & PF_KTHREAD) && p->worker_private
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
91 static inline struct kthread *__to_kthread(struct task_struct *p)
93 void *kthread = p->worker_private;
94 if (kthread && !(p->flags & PF_KTHREAD))
99 void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk)
101 struct kthread *kthread = to_kthread(tsk);
103 if (!kthread || !kthread->full_name) {
104 __get_task_comm(buf, buf_size, tsk);
108 strscpy_pad(buf, kthread->full_name, buf_size);
111 bool set_kthread_struct(struct task_struct *p)
113 struct kthread *kthread;
115 if (WARN_ON_ONCE(to_kthread(p)))
118 kthread = kzalloc(sizeof(*kthread), GFP_KERNEL);
122 init_completion(&kthread->exited);
123 init_completion(&kthread->parked);
124 p->vfork_done = &kthread->exited;
126 p->worker_private = kthread;
130 void free_kthread_struct(struct task_struct *k)
132 struct kthread *kthread;
135 * Can be NULL if kmalloc() in set_kthread_struct() failed.
137 kthread = to_kthread(k);
141 #ifdef CONFIG_BLK_CGROUP
142 WARN_ON_ONCE(kthread->blkcg_css);
144 k->worker_private = NULL;
145 kfree(kthread->full_name);
150 * kthread_should_stop - should this kthread return now?
152 * When someone calls kthread_stop() on your kthread, it will be woken
153 * and this will return true. You should then return, and your return
154 * value will be passed through to kthread_stop().
156 bool kthread_should_stop(void)
158 return test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags);
160 EXPORT_SYMBOL(kthread_should_stop);
162 bool __kthread_should_park(struct task_struct *k)
164 return test_bit(KTHREAD_SHOULD_PARK, &to_kthread(k)->flags);
166 EXPORT_SYMBOL_GPL(__kthread_should_park);
169 * kthread_should_park - should this kthread park now?
171 * When someone calls kthread_park() on your kthread, it will be woken
172 * and this will return true. You should then do the necessary
173 * cleanup and call kthread_parkme()
175 * Similar to kthread_should_stop(), but this keeps the thread alive
176 * and in a park position. kthread_unpark() "restarts" the thread and
177 * calls the thread function again.
179 bool kthread_should_park(void)
181 return __kthread_should_park(current);
183 EXPORT_SYMBOL_GPL(kthread_should_park);
186 * kthread_freezable_should_stop - should this freezable kthread return now?
187 * @was_frozen: optional out parameter, indicates whether %current was frozen
189 * kthread_should_stop() for freezable kthreads, which will enter
190 * refrigerator if necessary. This function is safe from kthread_stop() /
191 * freezer deadlock and freezable kthreads should use this function instead
192 * of calling try_to_freeze() directly.
194 bool kthread_freezable_should_stop(bool *was_frozen)
200 if (unlikely(freezing(current)))
201 frozen = __refrigerator(true);
204 *was_frozen = frozen;
206 return kthread_should_stop();
208 EXPORT_SYMBOL_GPL(kthread_freezable_should_stop);
211 * kthread_func - return the function specified on kthread creation
212 * @task: kthread task in question
214 * Returns NULL if the task is not a kthread.
216 void *kthread_func(struct task_struct *task)
218 struct kthread *kthread = __to_kthread(task);
220 return kthread->threadfn;
223 EXPORT_SYMBOL_GPL(kthread_func);
226 * kthread_data - return data value specified on kthread creation
227 * @task: kthread task in question
229 * Return the data value specified when kthread @task was created.
230 * The caller is responsible for ensuring the validity of @task when
231 * calling this function.
233 void *kthread_data(struct task_struct *task)
235 return to_kthread(task)->data;
237 EXPORT_SYMBOL_GPL(kthread_data);
240 * kthread_probe_data - speculative version of kthread_data()
241 * @task: possible kthread task in question
243 * @task could be a kthread task. Return the data value specified when it
244 * was created if accessible. If @task isn't a kthread task or its data is
245 * inaccessible for any reason, %NULL is returned. This function requires
246 * that @task itself is safe to dereference.
248 void *kthread_probe_data(struct task_struct *task)
250 struct kthread *kthread = __to_kthread(task);
254 copy_from_kernel_nofault(&data, &kthread->data, sizeof(data));
258 static void __kthread_parkme(struct kthread *self)
262 * TASK_PARKED is a special state; we must serialize against
263 * possible pending wakeups to avoid store-store collisions on
266 * Such a collision might possibly result in the task state
267 * changin from TASK_PARKED and us failing the
268 * wait_task_inactive() in kthread_park().
270 set_special_state(TASK_PARKED);
271 if (!test_bit(KTHREAD_SHOULD_PARK, &self->flags))
275 * Thread is going to call schedule(), do not preempt it,
276 * or the caller of kthread_park() may spend more time in
277 * wait_task_inactive().
280 complete(&self->parked);
281 schedule_preempt_disabled();
284 __set_current_state(TASK_RUNNING);
287 void kthread_parkme(void)
289 __kthread_parkme(to_kthread(current));
291 EXPORT_SYMBOL_GPL(kthread_parkme);
294 * kthread_exit - Cause the current kthread return @result to kthread_stop().
295 * @result: The integer value to return to kthread_stop().
297 * While kthread_exit can be called directly, it exists so that
298 * functions which do some additional work in non-modular code such as
299 * module_put_and_kthread_exit can be implemented.
303 void __noreturn kthread_exit(long result)
305 struct kthread *kthread = to_kthread(current);
306 kthread->result = result;
311 * kthread_complete_and_exit - Exit the current kthread.
312 * @comp: Completion to complete
313 * @code: The integer value to return to kthread_stop().
315 * If present complete @comp and the reuturn code to kthread_stop().
317 * A kernel thread whose module may be removed after the completion of
318 * @comp can use this function exit safely.
322 void __noreturn kthread_complete_and_exit(struct completion *comp, long code)
329 EXPORT_SYMBOL(kthread_complete_and_exit);
331 static int kthread(void *_create)
333 static const struct sched_param param = { .sched_priority = 0 };
334 /* Copy data: it's on kthread's stack */
335 struct kthread_create_info *create = _create;
336 int (*threadfn)(void *data) = create->threadfn;
337 void *data = create->data;
338 struct completion *done;
339 struct kthread *self;
342 self = to_kthread(current);
344 /* Release the structure when caller killed by a fatal signal. */
345 done = xchg(&create->done, NULL);
347 kfree(create->full_name);
349 kthread_exit(-EINTR);
352 self->full_name = create->full_name;
353 self->threadfn = threadfn;
357 * The new thread inherited kthreadd's priority and CPU mask. Reset
358 * back to default in case they have been changed.
360 sched_setscheduler_nocheck(current, SCHED_NORMAL, ¶m);
361 set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_TYPE_KTHREAD));
363 /* OK, tell user we're spawned, wait for stop or wakeup */
364 __set_current_state(TASK_UNINTERRUPTIBLE);
365 create->result = current;
367 * Thread is going to call schedule(), do not preempt it,
368 * or the creator may spend more time in wait_task_inactive().
372 schedule_preempt_disabled();
376 if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) {
377 cgroup_kthread_ready();
378 __kthread_parkme(self);
379 ret = threadfn(data);
384 /* called from kernel_clone() to get node information for about to be created task */
385 int tsk_fork_get_node(struct task_struct *tsk)
388 if (tsk == kthreadd_task)
389 return tsk->pref_node_fork;
394 static void create_kthread(struct kthread_create_info *create)
399 current->pref_node_fork = create->node;
401 /* We want our own signal handler (we take no signals by default). */
402 pid = kernel_thread(kthread, create, create->full_name,
403 CLONE_FS | CLONE_FILES | SIGCHLD);
405 /* Release the structure when caller killed by a fatal signal. */
406 struct completion *done = xchg(&create->done, NULL);
408 kfree(create->full_name);
413 create->result = ERR_PTR(pid);
418 static __printf(4, 0)
419 struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data),
420 void *data, int node,
421 const char namefmt[],
424 DECLARE_COMPLETION_ONSTACK(done);
425 struct task_struct *task;
426 struct kthread_create_info *create = kmalloc(sizeof(*create),
430 return ERR_PTR(-ENOMEM);
431 create->threadfn = threadfn;
434 create->done = &done;
435 create->full_name = kvasprintf(GFP_KERNEL, namefmt, args);
436 if (!create->full_name) {
437 task = ERR_PTR(-ENOMEM);
441 spin_lock(&kthread_create_lock);
442 list_add_tail(&create->list, &kthread_create_list);
443 spin_unlock(&kthread_create_lock);
445 wake_up_process(kthreadd_task);
447 * Wait for completion in killable state, for I might be chosen by
448 * the OOM killer while kthreadd is trying to allocate memory for
451 if (unlikely(wait_for_completion_killable(&done))) {
453 * If I was killed by a fatal signal before kthreadd (or new
454 * kernel thread) calls complete(), leave the cleanup of this
455 * structure to that thread.
457 if (xchg(&create->done, NULL))
458 return ERR_PTR(-EINTR);
460 * kthreadd (or new kernel thread) will call complete()
463 wait_for_completion(&done);
465 task = create->result;
472 * kthread_create_on_node - create a kthread.
473 * @threadfn: the function to run until signal_pending(current).
474 * @data: data ptr for @threadfn.
475 * @node: task and thread structures for the thread are allocated on this node
476 * @namefmt: printf-style name for the thread.
478 * Description: This helper function creates and names a kernel
479 * thread. The thread will be stopped: use wake_up_process() to start
480 * it. See also kthread_run(). The new thread has SCHED_NORMAL policy and
481 * is affine to all CPUs.
483 * If thread is going to be bound on a particular cpu, give its node
484 * in @node, to get NUMA affinity for kthread stack, or else give NUMA_NO_NODE.
485 * When woken, the thread will run @threadfn() with @data as its
486 * argument. @threadfn() can either return directly if it is a
487 * standalone thread for which no one will call kthread_stop(), or
488 * return when 'kthread_should_stop()' is true (which means
489 * kthread_stop() has been called). The return value should be zero
490 * or a negative error number; it will be passed to kthread_stop().
492 * Returns a task_struct or ERR_PTR(-ENOMEM) or ERR_PTR(-EINTR).
494 struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
495 void *data, int node,
496 const char namefmt[],
499 struct task_struct *task;
502 va_start(args, namefmt);
503 task = __kthread_create_on_node(threadfn, data, node, namefmt, args);
508 EXPORT_SYMBOL(kthread_create_on_node);
510 static void __kthread_bind_mask(struct task_struct *p, const struct cpumask *mask, unsigned int state)
514 if (!wait_task_inactive(p, state)) {
519 /* It's safe because the task is inactive. */
520 raw_spin_lock_irqsave(&p->pi_lock, flags);
521 do_set_cpus_allowed(p, mask);
522 p->flags |= PF_NO_SETAFFINITY;
523 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
526 static void __kthread_bind(struct task_struct *p, unsigned int cpu, unsigned int state)
528 __kthread_bind_mask(p, cpumask_of(cpu), state);
531 void kthread_bind_mask(struct task_struct *p, const struct cpumask *mask)
533 __kthread_bind_mask(p, mask, TASK_UNINTERRUPTIBLE);
537 * kthread_bind - bind a just-created kthread to a cpu.
538 * @p: thread created by kthread_create().
539 * @cpu: cpu (might not be online, must be possible) for @k to run on.
541 * Description: This function is equivalent to set_cpus_allowed(),
542 * except that @cpu doesn't need to be online, and the thread must be
543 * stopped (i.e., just returned from kthread_create()).
545 void kthread_bind(struct task_struct *p, unsigned int cpu)
547 __kthread_bind(p, cpu, TASK_UNINTERRUPTIBLE);
549 EXPORT_SYMBOL(kthread_bind);
552 * kthread_create_on_cpu - Create a cpu bound kthread
553 * @threadfn: the function to run until signal_pending(current).
554 * @data: data ptr for @threadfn.
555 * @cpu: The cpu on which the thread should be bound,
556 * @namefmt: printf-style name for the thread. Format is restricted
557 * to "name.*%u". Code fills in cpu number.
559 * Description: This helper function creates and names a kernel thread
561 struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
562 void *data, unsigned int cpu,
565 struct task_struct *p;
567 p = kthread_create_on_node(threadfn, data, cpu_to_node(cpu), namefmt,
571 kthread_bind(p, cpu);
572 /* CPU hotplug need to bind once again when unparking the thread. */
573 to_kthread(p)->cpu = cpu;
576 EXPORT_SYMBOL(kthread_create_on_cpu);
578 void kthread_set_per_cpu(struct task_struct *k, int cpu)
580 struct kthread *kthread = to_kthread(k);
584 WARN_ON_ONCE(!(k->flags & PF_NO_SETAFFINITY));
587 clear_bit(KTHREAD_IS_PER_CPU, &kthread->flags);
592 set_bit(KTHREAD_IS_PER_CPU, &kthread->flags);
595 bool kthread_is_per_cpu(struct task_struct *p)
597 struct kthread *kthread = __to_kthread(p);
601 return test_bit(KTHREAD_IS_PER_CPU, &kthread->flags);
605 * kthread_unpark - unpark a thread created by kthread_create().
606 * @k: thread created by kthread_create().
608 * Sets kthread_should_park() for @k to return false, wakes it, and
609 * waits for it to return. If the thread is marked percpu then its
610 * bound to the cpu again.
612 void kthread_unpark(struct task_struct *k)
614 struct kthread *kthread = to_kthread(k);
617 * Newly created kthread was parked when the CPU was offline.
618 * The binding was lost and we need to set it again.
620 if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags))
621 __kthread_bind(k, kthread->cpu, TASK_PARKED);
623 clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
625 * __kthread_parkme() will either see !SHOULD_PARK or get the wakeup.
627 wake_up_state(k, TASK_PARKED);
629 EXPORT_SYMBOL_GPL(kthread_unpark);
632 * kthread_park - park a thread created by kthread_create().
633 * @k: thread created by kthread_create().
635 * Sets kthread_should_park() for @k to return true, wakes it, and
636 * waits for it to return. This can also be called after kthread_create()
637 * instead of calling wake_up_process(): the thread will park without
638 * calling threadfn().
640 * Returns 0 if the thread is parked, -ENOSYS if the thread exited.
641 * If called by the kthread itself just the park bit is set.
643 int kthread_park(struct task_struct *k)
645 struct kthread *kthread = to_kthread(k);
647 if (WARN_ON(k->flags & PF_EXITING))
650 if (WARN_ON_ONCE(test_bit(KTHREAD_SHOULD_PARK, &kthread->flags)))
653 set_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
657 * Wait for __kthread_parkme() to complete(), this means we
658 * _will_ have TASK_PARKED and are about to call schedule().
660 wait_for_completion(&kthread->parked);
662 * Now wait for that schedule() to complete and the task to
665 WARN_ON_ONCE(!wait_task_inactive(k, TASK_PARKED));
670 EXPORT_SYMBOL_GPL(kthread_park);
673 * kthread_stop - stop a thread created by kthread_create().
674 * @k: thread created by kthread_create().
676 * Sets kthread_should_stop() for @k to return true, wakes it, and
677 * waits for it to exit. This can also be called after kthread_create()
678 * instead of calling wake_up_process(): the thread will exit without
679 * calling threadfn().
681 * If threadfn() may call kthread_exit() itself, the caller must ensure
682 * task_struct can't go away.
684 * Returns the result of threadfn(), or %-EINTR if wake_up_process()
687 int kthread_stop(struct task_struct *k)
689 struct kthread *kthread;
692 trace_sched_kthread_stop(k);
695 kthread = to_kthread(k);
696 set_bit(KTHREAD_SHOULD_STOP, &kthread->flags);
698 set_tsk_thread_flag(k, TIF_NOTIFY_SIGNAL);
700 wait_for_completion(&kthread->exited);
701 ret = kthread->result;
704 trace_sched_kthread_stop_ret(ret);
707 EXPORT_SYMBOL(kthread_stop);
709 int kthreadd(void *unused)
711 struct task_struct *tsk = current;
713 /* Setup a clean context for our children to inherit. */
714 set_task_comm(tsk, "kthreadd");
716 set_cpus_allowed_ptr(tsk, housekeeping_cpumask(HK_TYPE_KTHREAD));
717 set_mems_allowed(node_states[N_MEMORY]);
719 current->flags |= PF_NOFREEZE;
720 cgroup_init_kthreadd();
723 set_current_state(TASK_INTERRUPTIBLE);
724 if (list_empty(&kthread_create_list))
726 __set_current_state(TASK_RUNNING);
728 spin_lock(&kthread_create_lock);
729 while (!list_empty(&kthread_create_list)) {
730 struct kthread_create_info *create;
732 create = list_entry(kthread_create_list.next,
733 struct kthread_create_info, list);
734 list_del_init(&create->list);
735 spin_unlock(&kthread_create_lock);
737 create_kthread(create);
739 spin_lock(&kthread_create_lock);
741 spin_unlock(&kthread_create_lock);
747 void __kthread_init_worker(struct kthread_worker *worker,
749 struct lock_class_key *key)
751 memset(worker, 0, sizeof(struct kthread_worker));
752 raw_spin_lock_init(&worker->lock);
753 lockdep_set_class_and_name(&worker->lock, key, name);
754 INIT_LIST_HEAD(&worker->work_list);
755 INIT_LIST_HEAD(&worker->delayed_work_list);
757 EXPORT_SYMBOL_GPL(__kthread_init_worker);
760 * kthread_worker_fn - kthread function to process kthread_worker
761 * @worker_ptr: pointer to initialized kthread_worker
763 * This function implements the main cycle of kthread worker. It processes
764 * work_list until it is stopped with kthread_stop(). It sleeps when the queue
767 * The works are not allowed to keep any locks, disable preemption or interrupts
768 * when they finish. There is defined a safe point for freezing when one work
769 * finishes and before a new one is started.
771 * Also the works must not be handled by more than one worker at the same time,
772 * see also kthread_queue_work().
774 int kthread_worker_fn(void *worker_ptr)
776 struct kthread_worker *worker = worker_ptr;
777 struct kthread_work *work;
780 * FIXME: Update the check and remove the assignment when all kthread
781 * worker users are created using kthread_create_worker*() functions.
783 WARN_ON(worker->task && worker->task != current);
784 worker->task = current;
786 if (worker->flags & KTW_FREEZABLE)
790 set_current_state(TASK_INTERRUPTIBLE); /* mb paired w/ kthread_stop */
792 if (kthread_should_stop()) {
793 __set_current_state(TASK_RUNNING);
794 raw_spin_lock_irq(&worker->lock);
796 raw_spin_unlock_irq(&worker->lock);
801 raw_spin_lock_irq(&worker->lock);
802 if (!list_empty(&worker->work_list)) {
803 work = list_first_entry(&worker->work_list,
804 struct kthread_work, node);
805 list_del_init(&work->node);
807 worker->current_work = work;
808 raw_spin_unlock_irq(&worker->lock);
811 kthread_work_func_t func = work->func;
812 __set_current_state(TASK_RUNNING);
813 trace_sched_kthread_work_execute_start(work);
816 * Avoid dereferencing work after this point. The trace
817 * event only cares about the address.
819 trace_sched_kthread_work_execute_end(work, func);
820 } else if (!freezing(current))
827 EXPORT_SYMBOL_GPL(kthread_worker_fn);
829 static __printf(3, 0) struct kthread_worker *
830 __kthread_create_worker(int cpu, unsigned int flags,
831 const char namefmt[], va_list args)
833 struct kthread_worker *worker;
834 struct task_struct *task;
835 int node = NUMA_NO_NODE;
837 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
839 return ERR_PTR(-ENOMEM);
841 kthread_init_worker(worker);
844 node = cpu_to_node(cpu);
846 task = __kthread_create_on_node(kthread_worker_fn, worker,
847 node, namefmt, args);
852 kthread_bind(task, cpu);
854 worker->flags = flags;
856 wake_up_process(task);
861 return ERR_CAST(task);
865 * kthread_create_worker - create a kthread worker
866 * @flags: flags modifying the default behavior of the worker
867 * @namefmt: printf-style name for the kthread worker (task).
869 * Returns a pointer to the allocated worker on success, ERR_PTR(-ENOMEM)
870 * when the needed structures could not get allocated, and ERR_PTR(-EINTR)
871 * when the caller was killed by a fatal signal.
873 struct kthread_worker *
874 kthread_create_worker(unsigned int flags, const char namefmt[], ...)
876 struct kthread_worker *worker;
879 va_start(args, namefmt);
880 worker = __kthread_create_worker(-1, flags, namefmt, args);
885 EXPORT_SYMBOL(kthread_create_worker);
888 * kthread_create_worker_on_cpu - create a kthread worker and bind it
889 * to a given CPU and the associated NUMA node.
891 * @flags: flags modifying the default behavior of the worker
892 * @namefmt: printf-style name for the kthread worker (task).
894 * Use a valid CPU number if you want to bind the kthread worker
895 * to the given CPU and the associated NUMA node.
897 * A good practice is to add the cpu number also into the worker name.
898 * For example, use kthread_create_worker_on_cpu(cpu, "helper/%d", cpu).
901 * The kthread worker API is simple and generic. It just provides a way
902 * to create, use, and destroy workers.
904 * It is up to the API user how to handle CPU hotplug. They have to decide
905 * how to handle pending work items, prevent queuing new ones, and
906 * restore the functionality when the CPU goes off and on. There are a
909 * - CPU affinity gets lost when it is scheduled on an offline CPU.
911 * - The worker might not exist when the CPU was off when the user
912 * created the workers.
914 * Good practice is to implement two CPU hotplug callbacks and to
915 * destroy/create the worker when the CPU goes down/up.
918 * The pointer to the allocated worker on success, ERR_PTR(-ENOMEM)
919 * when the needed structures could not get allocated, and ERR_PTR(-EINTR)
920 * when the caller was killed by a fatal signal.
922 struct kthread_worker *
923 kthread_create_worker_on_cpu(int cpu, unsigned int flags,
924 const char namefmt[], ...)
926 struct kthread_worker *worker;
929 va_start(args, namefmt);
930 worker = __kthread_create_worker(cpu, flags, namefmt, args);
935 EXPORT_SYMBOL(kthread_create_worker_on_cpu);
938 * Returns true when the work could not be queued at the moment.
939 * It happens when it is already pending in a worker list
940 * or when it is being cancelled.
942 static inline bool queuing_blocked(struct kthread_worker *worker,
943 struct kthread_work *work)
945 lockdep_assert_held(&worker->lock);
947 return !list_empty(&work->node) || work->canceling;
950 static void kthread_insert_work_sanity_check(struct kthread_worker *worker,
951 struct kthread_work *work)
953 lockdep_assert_held(&worker->lock);
954 WARN_ON_ONCE(!list_empty(&work->node));
955 /* Do not use a work with >1 worker, see kthread_queue_work() */
956 WARN_ON_ONCE(work->worker && work->worker != worker);
959 /* insert @work before @pos in @worker */
960 static void kthread_insert_work(struct kthread_worker *worker,
961 struct kthread_work *work,
962 struct list_head *pos)
964 kthread_insert_work_sanity_check(worker, work);
966 trace_sched_kthread_work_queue_work(worker, work);
968 list_add_tail(&work->node, pos);
969 work->worker = worker;
970 if (!worker->current_work && likely(worker->task))
971 wake_up_process(worker->task);
975 * kthread_queue_work - queue a kthread_work
976 * @worker: target kthread_worker
977 * @work: kthread_work to queue
979 * Queue @work to work processor @task for async execution. @task
980 * must have been created with kthread_worker_create(). Returns %true
981 * if @work was successfully queued, %false if it was already pending.
983 * Reinitialize the work if it needs to be used by another worker.
984 * For example, when the worker was stopped and started again.
986 bool kthread_queue_work(struct kthread_worker *worker,
987 struct kthread_work *work)
992 raw_spin_lock_irqsave(&worker->lock, flags);
993 if (!queuing_blocked(worker, work)) {
994 kthread_insert_work(worker, work, &worker->work_list);
997 raw_spin_unlock_irqrestore(&worker->lock, flags);
1000 EXPORT_SYMBOL_GPL(kthread_queue_work);
1003 * kthread_delayed_work_timer_fn - callback that queues the associated kthread
1004 * delayed work when the timer expires.
1005 * @t: pointer to the expired timer
1007 * The format of the function is defined by struct timer_list.
1008 * It should have been called from irqsafe timer with irq already off.
1010 void kthread_delayed_work_timer_fn(struct timer_list *t)
1012 struct kthread_delayed_work *dwork = from_timer(dwork, t, timer);
1013 struct kthread_work *work = &dwork->work;
1014 struct kthread_worker *worker = work->worker;
1015 unsigned long flags;
1018 * This might happen when a pending work is reinitialized.
1019 * It means that it is used a wrong way.
1021 if (WARN_ON_ONCE(!worker))
1024 raw_spin_lock_irqsave(&worker->lock, flags);
1025 /* Work must not be used with >1 worker, see kthread_queue_work(). */
1026 WARN_ON_ONCE(work->worker != worker);
1028 /* Move the work from worker->delayed_work_list. */
1029 WARN_ON_ONCE(list_empty(&work->node));
1030 list_del_init(&work->node);
1031 if (!work->canceling)
1032 kthread_insert_work(worker, work, &worker->work_list);
1034 raw_spin_unlock_irqrestore(&worker->lock, flags);
1036 EXPORT_SYMBOL(kthread_delayed_work_timer_fn);
1038 static void __kthread_queue_delayed_work(struct kthread_worker *worker,
1039 struct kthread_delayed_work *dwork,
1040 unsigned long delay)
1042 struct timer_list *timer = &dwork->timer;
1043 struct kthread_work *work = &dwork->work;
1045 WARN_ON_ONCE(timer->function != kthread_delayed_work_timer_fn);
1048 * If @delay is 0, queue @dwork->work immediately. This is for
1049 * both optimization and correctness. The earliest @timer can
1050 * expire is on the closest next tick and delayed_work users depend
1051 * on that there's no such delay when @delay is 0.
1054 kthread_insert_work(worker, work, &worker->work_list);
1058 /* Be paranoid and try to detect possible races already now. */
1059 kthread_insert_work_sanity_check(worker, work);
1061 list_add(&work->node, &worker->delayed_work_list);
1062 work->worker = worker;
1063 timer->expires = jiffies + delay;
1068 * kthread_queue_delayed_work - queue the associated kthread work
1070 * @worker: target kthread_worker
1071 * @dwork: kthread_delayed_work to queue
1072 * @delay: number of jiffies to wait before queuing
1074 * If the work has not been pending it starts a timer that will queue
1075 * the work after the given @delay. If @delay is zero, it queues the
1078 * Return: %false if the @work has already been pending. It means that
1079 * either the timer was running or the work was queued. It returns %true
1082 bool kthread_queue_delayed_work(struct kthread_worker *worker,
1083 struct kthread_delayed_work *dwork,
1084 unsigned long delay)
1086 struct kthread_work *work = &dwork->work;
1087 unsigned long flags;
1090 raw_spin_lock_irqsave(&worker->lock, flags);
1092 if (!queuing_blocked(worker, work)) {
1093 __kthread_queue_delayed_work(worker, dwork, delay);
1097 raw_spin_unlock_irqrestore(&worker->lock, flags);
1100 EXPORT_SYMBOL_GPL(kthread_queue_delayed_work);
1102 struct kthread_flush_work {
1103 struct kthread_work work;
1104 struct completion done;
1107 static void kthread_flush_work_fn(struct kthread_work *work)
1109 struct kthread_flush_work *fwork =
1110 container_of(work, struct kthread_flush_work, work);
1111 complete(&fwork->done);
1115 * kthread_flush_work - flush a kthread_work
1116 * @work: work to flush
1118 * If @work is queued or executing, wait for it to finish execution.
1120 void kthread_flush_work(struct kthread_work *work)
1122 struct kthread_flush_work fwork = {
1123 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
1124 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
1126 struct kthread_worker *worker;
1129 worker = work->worker;
1133 raw_spin_lock_irq(&worker->lock);
1134 /* Work must not be used with >1 worker, see kthread_queue_work(). */
1135 WARN_ON_ONCE(work->worker != worker);
1137 if (!list_empty(&work->node))
1138 kthread_insert_work(worker, &fwork.work, work->node.next);
1139 else if (worker->current_work == work)
1140 kthread_insert_work(worker, &fwork.work,
1141 worker->work_list.next);
1145 raw_spin_unlock_irq(&worker->lock);
1148 wait_for_completion(&fwork.done);
1150 EXPORT_SYMBOL_GPL(kthread_flush_work);
1153 * Make sure that the timer is neither set nor running and could
1154 * not manipulate the work list_head any longer.
1156 * The function is called under worker->lock. The lock is temporary
1157 * released but the timer can't be set again in the meantime.
1159 static void kthread_cancel_delayed_work_timer(struct kthread_work *work,
1160 unsigned long *flags)
1162 struct kthread_delayed_work *dwork =
1163 container_of(work, struct kthread_delayed_work, work);
1164 struct kthread_worker *worker = work->worker;
1167 * del_timer_sync() must be called to make sure that the timer
1168 * callback is not running. The lock must be temporary released
1169 * to avoid a deadlock with the callback. In the meantime,
1170 * any queuing is blocked by setting the canceling counter.
1173 raw_spin_unlock_irqrestore(&worker->lock, *flags);
1174 del_timer_sync(&dwork->timer);
1175 raw_spin_lock_irqsave(&worker->lock, *flags);
1180 * This function removes the work from the worker queue.
1182 * It is called under worker->lock. The caller must make sure that
1183 * the timer used by delayed work is not running, e.g. by calling
1184 * kthread_cancel_delayed_work_timer().
1186 * The work might still be in use when this function finishes. See the
1187 * current_work proceed by the worker.
1189 * Return: %true if @work was pending and successfully canceled,
1190 * %false if @work was not pending
1192 static bool __kthread_cancel_work(struct kthread_work *work)
1195 * Try to remove the work from a worker list. It might either
1196 * be from worker->work_list or from worker->delayed_work_list.
1198 if (!list_empty(&work->node)) {
1199 list_del_init(&work->node);
1207 * kthread_mod_delayed_work - modify delay of or queue a kthread delayed work
1208 * @worker: kthread worker to use
1209 * @dwork: kthread delayed work to queue
1210 * @delay: number of jiffies to wait before queuing
1212 * If @dwork is idle, equivalent to kthread_queue_delayed_work(). Otherwise,
1213 * modify @dwork's timer so that it expires after @delay. If @delay is zero,
1214 * @work is guaranteed to be queued immediately.
1216 * Return: %false if @dwork was idle and queued, %true otherwise.
1218 * A special case is when the work is being canceled in parallel.
1219 * It might be caused either by the real kthread_cancel_delayed_work_sync()
1220 * or yet another kthread_mod_delayed_work() call. We let the other command
1221 * win and return %true here. The return value can be used for reference
1222 * counting and the number of queued works stays the same. Anyway, the caller
1223 * is supposed to synchronize these operations a reasonable way.
1225 * This function is safe to call from any context including IRQ handler.
1226 * See __kthread_cancel_work() and kthread_delayed_work_timer_fn()
1229 bool kthread_mod_delayed_work(struct kthread_worker *worker,
1230 struct kthread_delayed_work *dwork,
1231 unsigned long delay)
1233 struct kthread_work *work = &dwork->work;
1234 unsigned long flags;
1237 raw_spin_lock_irqsave(&worker->lock, flags);
1239 /* Do not bother with canceling when never queued. */
1240 if (!work->worker) {
1245 /* Work must not be used with >1 worker, see kthread_queue_work() */
1246 WARN_ON_ONCE(work->worker != worker);
1249 * Temporary cancel the work but do not fight with another command
1250 * that is canceling the work as well.
1252 * It is a bit tricky because of possible races with another
1253 * mod_delayed_work() and cancel_delayed_work() callers.
1255 * The timer must be canceled first because worker->lock is released
1256 * when doing so. But the work can be removed from the queue (list)
1257 * only when it can be queued again so that the return value can
1258 * be used for reference counting.
1260 kthread_cancel_delayed_work_timer(work, &flags);
1261 if (work->canceling) {
1262 /* The number of works in the queue does not change. */
1266 ret = __kthread_cancel_work(work);
1269 __kthread_queue_delayed_work(worker, dwork, delay);
1271 raw_spin_unlock_irqrestore(&worker->lock, flags);
1274 EXPORT_SYMBOL_GPL(kthread_mod_delayed_work);
1276 static bool __kthread_cancel_work_sync(struct kthread_work *work, bool is_dwork)
1278 struct kthread_worker *worker = work->worker;
1279 unsigned long flags;
1285 raw_spin_lock_irqsave(&worker->lock, flags);
1286 /* Work must not be used with >1 worker, see kthread_queue_work(). */
1287 WARN_ON_ONCE(work->worker != worker);
1290 kthread_cancel_delayed_work_timer(work, &flags);
1292 ret = __kthread_cancel_work(work);
1294 if (worker->current_work != work)
1298 * The work is in progress and we need to wait with the lock released.
1299 * In the meantime, block any queuing by setting the canceling counter.
1302 raw_spin_unlock_irqrestore(&worker->lock, flags);
1303 kthread_flush_work(work);
1304 raw_spin_lock_irqsave(&worker->lock, flags);
1308 raw_spin_unlock_irqrestore(&worker->lock, flags);
1314 * kthread_cancel_work_sync - cancel a kthread work and wait for it to finish
1315 * @work: the kthread work to cancel
1317 * Cancel @work and wait for its execution to finish. This function
1318 * can be used even if the work re-queues itself. On return from this
1319 * function, @work is guaranteed to be not pending or executing on any CPU.
1321 * kthread_cancel_work_sync(&delayed_work->work) must not be used for
1322 * delayed_work's. Use kthread_cancel_delayed_work_sync() instead.
1324 * The caller must ensure that the worker on which @work was last
1325 * queued can't be destroyed before this function returns.
1327 * Return: %true if @work was pending, %false otherwise.
1329 bool kthread_cancel_work_sync(struct kthread_work *work)
1331 return __kthread_cancel_work_sync(work, false);
1333 EXPORT_SYMBOL_GPL(kthread_cancel_work_sync);
1336 * kthread_cancel_delayed_work_sync - cancel a kthread delayed work and
1337 * wait for it to finish.
1338 * @dwork: the kthread delayed work to cancel
1340 * This is kthread_cancel_work_sync() for delayed works.
1342 * Return: %true if @dwork was pending, %false otherwise.
1344 bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *dwork)
1346 return __kthread_cancel_work_sync(&dwork->work, true);
1348 EXPORT_SYMBOL_GPL(kthread_cancel_delayed_work_sync);
1351 * kthread_flush_worker - flush all current works on a kthread_worker
1352 * @worker: worker to flush
1354 * Wait until all currently executing or pending works on @worker are
1357 void kthread_flush_worker(struct kthread_worker *worker)
1359 struct kthread_flush_work fwork = {
1360 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
1361 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
1364 kthread_queue_work(worker, &fwork.work);
1365 wait_for_completion(&fwork.done);
1367 EXPORT_SYMBOL_GPL(kthread_flush_worker);
1370 * kthread_destroy_worker - destroy a kthread worker
1371 * @worker: worker to be destroyed
1373 * Flush and destroy @worker. The simple flush is enough because the kthread
1374 * worker API is used only in trivial scenarios. There are no multi-step state
1377 * Note that this function is not responsible for handling delayed work, so
1378 * caller should be responsible for queuing or canceling all delayed work items
1379 * before invoke this function.
1381 void kthread_destroy_worker(struct kthread_worker *worker)
1383 struct task_struct *task;
1385 task = worker->task;
1389 kthread_flush_worker(worker);
1391 WARN_ON(!list_empty(&worker->delayed_work_list));
1392 WARN_ON(!list_empty(&worker->work_list));
1395 EXPORT_SYMBOL(kthread_destroy_worker);
1398 * kthread_use_mm - make the calling kthread operate on an address space
1399 * @mm: address space to operate on
1401 void kthread_use_mm(struct mm_struct *mm)
1403 struct mm_struct *active_mm;
1404 struct task_struct *tsk = current;
1406 WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD));
1407 WARN_ON_ONCE(tsk->mm);
1410 * It is possible for mm to be the same as tsk->active_mm, but
1411 * we must still mmgrab(mm) and mmdrop_lazy_tlb(active_mm),
1412 * because these references are not equivalent.
1417 /* Hold off tlb flush IPIs while switching mm's */
1418 local_irq_disable();
1419 active_mm = tsk->active_mm;
1420 tsk->active_mm = mm;
1422 membarrier_update_current_mm(mm);
1423 switch_mm_irqs_off(active_mm, mm, tsk);
1426 #ifdef finish_arch_post_lock_switch
1427 finish_arch_post_lock_switch();
1431 * When a kthread starts operating on an address space, the loop
1432 * in membarrier_{private,global}_expedited() may not observe
1433 * that tsk->mm, and not issue an IPI. Membarrier requires a
1434 * memory barrier after storing to tsk->mm, before accessing
1435 * user-space memory. A full memory barrier for membarrier
1436 * {PRIVATE,GLOBAL}_EXPEDITED is implicitly provided by
1437 * mmdrop_lazy_tlb().
1439 mmdrop_lazy_tlb(active_mm);
1441 EXPORT_SYMBOL_GPL(kthread_use_mm);
1444 * kthread_unuse_mm - reverse the effect of kthread_use_mm()
1445 * @mm: address space to operate on
1447 void kthread_unuse_mm(struct mm_struct *mm)
1449 struct task_struct *tsk = current;
1451 WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD));
1452 WARN_ON_ONCE(!tsk->mm);
1456 * When a kthread stops operating on an address space, the loop
1457 * in membarrier_{private,global}_expedited() may not observe
1458 * that tsk->mm, and not issue an IPI. Membarrier requires a
1459 * memory barrier after accessing user-space memory, before
1462 smp_mb__after_spinlock();
1464 local_irq_disable();
1466 membarrier_update_current_mm(NULL);
1467 mmgrab_lazy_tlb(mm);
1468 /* active_mm is still 'mm' */
1469 enter_lazy_tlb(mm, tsk);
1475 EXPORT_SYMBOL_GPL(kthread_unuse_mm);
1477 #ifdef CONFIG_BLK_CGROUP
1479 * kthread_associate_blkcg - associate blkcg to current kthread
1480 * @css: the cgroup info
1482 * Current thread must be a kthread. The thread is running jobs on behalf of
1483 * other threads. In some cases, we expect the jobs attach cgroup info of
1484 * original threads instead of that of current thread. This function stores
1485 * original thread's cgroup info in current kthread context for later
1488 void kthread_associate_blkcg(struct cgroup_subsys_state *css)
1490 struct kthread *kthread;
1492 if (!(current->flags & PF_KTHREAD))
1494 kthread = to_kthread(current);
1498 if (kthread->blkcg_css) {
1499 css_put(kthread->blkcg_css);
1500 kthread->blkcg_css = NULL;
1504 kthread->blkcg_css = css;
1507 EXPORT_SYMBOL(kthread_associate_blkcg);
1510 * kthread_blkcg - get associated blkcg css of current kthread
1512 * Current thread must be a kthread.
1514 struct cgroup_subsys_state *kthread_blkcg(void)
1516 struct kthread *kthread;
1518 if (current->flags & PF_KTHREAD) {
1519 kthread = to_kthread(current);
1521 return kthread->blkcg_css;