1 /* Kernel thread helper functions.
2 * Copyright (C) 2004 IBM Corporation, Rusty Russell.
4 * Creation is done via kthreadd, so that we get a clean environment
5 * even if we're invoked from userspace (think modprobe, hotplug cpu,
8 #include <linux/sched.h>
9 #include <linux/kthread.h>
10 #include <linux/completion.h>
11 #include <linux/err.h>
12 #include <linux/cpuset.h>
13 #include <linux/unistd.h>
14 #include <linux/file.h>
15 #include <linux/export.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18 #include <linux/freezer.h>
19 #include <linux/ptrace.h>
20 #include <linux/uaccess.h>
21 #include <linux/cgroup.h>
22 #include <trace/events/sched.h>
24 static DEFINE_SPINLOCK(kthread_create_lock
);
25 static LIST_HEAD(kthread_create_list
);
26 struct task_struct
*kthreadd_task
;
28 struct kthread_create_info
30 /* Information passed to kthread() from kthreadd. */
31 int (*threadfn
)(void *data
);
35 /* Result passed back to kthread_create() from kthreadd. */
36 struct task_struct
*result
;
37 struct completion
*done
;
39 struct list_head list
;
46 struct completion parked
;
47 struct completion exited
;
51 KTHREAD_IS_PER_CPU
= 0,
57 static inline void set_kthread_struct(void *kthread
)
60 * We abuse ->set_child_tid to avoid the new member and because it
61 * can't be wrongly copied by copy_process(). We also rely on fact
62 * that the caller can't exec, so PF_KTHREAD can't be cleared.
64 current
->set_child_tid
= (__force
void __user
*)kthread
;
67 static inline struct kthread
*to_kthread(struct task_struct
*k
)
69 WARN_ON(!(k
->flags
& PF_KTHREAD
));
70 return (__force
void *)k
->set_child_tid
;
73 void free_kthread_struct(struct task_struct
*k
)
76 * Can be NULL if this kthread was created by kernel_thread()
77 * or if kmalloc() in kthread() failed.
83 * kthread_should_stop - should this kthread return now?
85 * When someone calls kthread_stop() on your kthread, it will be woken
86 * and this will return true. You should then return, and your return
87 * value will be passed through to kthread_stop().
89 bool kthread_should_stop(void)
91 return test_bit(KTHREAD_SHOULD_STOP
, &to_kthread(current
)->flags
);
93 EXPORT_SYMBOL(kthread_should_stop
);
96 * kthread_should_park - should this kthread park now?
98 * When someone calls kthread_park() on your kthread, it will be woken
99 * and this will return true. You should then do the necessary
100 * cleanup and call kthread_parkme()
102 * Similar to kthread_should_stop(), but this keeps the thread alive
103 * and in a park position. kthread_unpark() "restarts" the thread and
104 * calls the thread function again.
106 bool kthread_should_park(void)
108 return test_bit(KTHREAD_SHOULD_PARK
, &to_kthread(current
)->flags
);
110 EXPORT_SYMBOL_GPL(kthread_should_park
);
113 * kthread_freezable_should_stop - should this freezable kthread return now?
114 * @was_frozen: optional out parameter, indicates whether %current was frozen
116 * kthread_should_stop() for freezable kthreads, which will enter
117 * refrigerator if necessary. This function is safe from kthread_stop() /
118 * freezer deadlock and freezable kthreads should use this function instead
119 * of calling try_to_freeze() directly.
121 bool kthread_freezable_should_stop(bool *was_frozen
)
127 if (unlikely(freezing(current
)))
128 frozen
= __refrigerator(true);
131 *was_frozen
= frozen
;
133 return kthread_should_stop();
135 EXPORT_SYMBOL_GPL(kthread_freezable_should_stop
);
138 * kthread_data - return data value specified on kthread creation
139 * @task: kthread task in question
141 * Return the data value specified when kthread @task was created.
142 * The caller is responsible for ensuring the validity of @task when
143 * calling this function.
145 void *kthread_data(struct task_struct
*task
)
147 return to_kthread(task
)->data
;
151 * kthread_probe_data - speculative version of kthread_data()
152 * @task: possible kthread task in question
154 * @task could be a kthread task. Return the data value specified when it
155 * was created if accessible. If @task isn't a kthread task or its data is
156 * inaccessible for any reason, %NULL is returned. This function requires
157 * that @task itself is safe to dereference.
159 void *kthread_probe_data(struct task_struct
*task
)
161 struct kthread
*kthread
= to_kthread(task
);
164 probe_kernel_read(&data
, &kthread
->data
, sizeof(data
));
168 static void __kthread_parkme(struct kthread
*self
)
170 __set_current_state(TASK_PARKED
);
171 while (test_bit(KTHREAD_SHOULD_PARK
, &self
->flags
)) {
172 if (!test_and_set_bit(KTHREAD_IS_PARKED
, &self
->flags
))
173 complete(&self
->parked
);
175 __set_current_state(TASK_PARKED
);
177 clear_bit(KTHREAD_IS_PARKED
, &self
->flags
);
178 __set_current_state(TASK_RUNNING
);
181 void kthread_parkme(void)
183 __kthread_parkme(to_kthread(current
));
185 EXPORT_SYMBOL_GPL(kthread_parkme
);
187 static int kthread(void *_create
)
189 /* Copy data: it's on kthread's stack */
190 struct kthread_create_info
*create
= _create
;
191 int (*threadfn
)(void *data
) = create
->threadfn
;
192 void *data
= create
->data
;
193 struct completion
*done
;
194 struct kthread
*self
;
197 self
= kmalloc(sizeof(*self
), GFP_KERNEL
);
198 set_kthread_struct(self
);
200 /* If user was SIGKILLed, I release the structure. */
201 done
= xchg(&create
->done
, NULL
);
208 create
->result
= ERR_PTR(-ENOMEM
);
215 init_completion(&self
->exited
);
216 init_completion(&self
->parked
);
217 current
->vfork_done
= &self
->exited
;
219 /* OK, tell user we're spawned, wait for stop or wakeup */
220 __set_current_state(TASK_UNINTERRUPTIBLE
);
221 create
->result
= current
;
226 if (!test_bit(KTHREAD_SHOULD_STOP
, &self
->flags
)) {
227 cgroup_kthread_ready();
228 __kthread_parkme(self
);
229 ret
= threadfn(data
);
234 /* called from do_fork() to get node information for about to be created task */
235 int tsk_fork_get_node(struct task_struct
*tsk
)
238 if (tsk
== kthreadd_task
)
239 return tsk
->pref_node_fork
;
244 static void create_kthread(struct kthread_create_info
*create
)
249 current
->pref_node_fork
= create
->node
;
251 /* We want our own signal handler (we take no signals by default). */
252 pid
= kernel_thread(kthread
, create
, CLONE_FS
| CLONE_FILES
| SIGCHLD
);
254 /* If user was SIGKILLed, I release the structure. */
255 struct completion
*done
= xchg(&create
->done
, NULL
);
261 create
->result
= ERR_PTR(pid
);
266 static __printf(4, 0)
267 struct task_struct
*__kthread_create_on_node(int (*threadfn
)(void *data
),
268 void *data
, int node
,
269 const char namefmt
[],
272 DECLARE_COMPLETION_ONSTACK(done
);
273 struct task_struct
*task
;
274 struct kthread_create_info
*create
= kmalloc(sizeof(*create
),
278 return ERR_PTR(-ENOMEM
);
279 create
->threadfn
= threadfn
;
282 create
->done
= &done
;
284 spin_lock(&kthread_create_lock
);
285 list_add_tail(&create
->list
, &kthread_create_list
);
286 spin_unlock(&kthread_create_lock
);
288 wake_up_process(kthreadd_task
);
290 * Wait for completion in killable state, for I might be chosen by
291 * the OOM killer while kthreadd is trying to allocate memory for
294 if (unlikely(wait_for_completion_killable(&done
))) {
298 * I got SIGKILL, but wait for 10 more seconds for completion
299 * unless chosen by the OOM killer. This delay is there as a
300 * workaround for boot failure caused by SIGKILL upon device
301 * driver initialization timeout.
303 while (i
++ < 10 && !test_tsk_thread_flag(current
, TIF_MEMDIE
))
304 if (wait_for_completion_timeout(&done
, HZ
))
307 * If I was SIGKILLed before kthreadd (or new kernel thread)
308 * calls complete(), leave the cleanup of this structure to
311 if (xchg(&create
->done
, NULL
))
312 return ERR_PTR(-EINTR
);
314 * kthreadd (or new kernel thread) will call complete()
317 wait_for_completion(&done
);
320 task
= create
->result
;
322 static const struct sched_param param
= { .sched_priority
= 0 };
324 vsnprintf(task
->comm
, sizeof(task
->comm
), namefmt
, args
);
326 * root may have changed our (kthreadd's) priority or CPU mask.
327 * The kernel thread should not inherit these properties.
329 sched_setscheduler_nocheck(task
, SCHED_NORMAL
, ¶m
);
330 set_cpus_allowed_ptr(task
, cpu_all_mask
);
337 * kthread_create_on_node - create a kthread.
338 * @threadfn: the function to run until signal_pending(current).
339 * @data: data ptr for @threadfn.
340 * @node: task and thread structures for the thread are allocated on this node
341 * @namefmt: printf-style name for the thread.
343 * Description: This helper function creates and names a kernel
344 * thread. The thread will be stopped: use wake_up_process() to start
345 * it. See also kthread_run(). The new thread has SCHED_NORMAL policy and
346 * is affine to all CPUs.
348 * If thread is going to be bound on a particular cpu, give its node
349 * in @node, to get NUMA affinity for kthread stack, or else give NUMA_NO_NODE.
350 * When woken, the thread will run @threadfn() with @data as its
351 * argument. @threadfn() can either call do_exit() directly if it is a
352 * standalone thread for which no one will call kthread_stop(), or
353 * return when 'kthread_should_stop()' is true (which means
354 * kthread_stop() has been called). The return value should be zero
355 * or a negative error number; it will be passed to kthread_stop().
357 * Returns a task_struct or ERR_PTR(-ENOMEM) or ERR_PTR(-EINTR).
359 struct task_struct
*kthread_create_on_node(int (*threadfn
)(void *data
),
360 void *data
, int node
,
361 const char namefmt
[],
364 struct task_struct
*task
;
367 va_start(args
, namefmt
);
368 task
= __kthread_create_on_node(threadfn
, data
, node
, namefmt
, args
);
373 EXPORT_SYMBOL(kthread_create_on_node
);
375 static void __kthread_bind_mask(struct task_struct
*p
, const struct cpumask
*mask
, long state
)
379 if (!wait_task_inactive(p
, state
)) {
384 /* It's safe because the task is inactive. */
385 raw_spin_lock_irqsave(&p
->pi_lock
, flags
);
386 do_set_cpus_allowed(p
, mask
);
387 p
->flags
|= PF_NO_SETAFFINITY
;
388 raw_spin_unlock_irqrestore(&p
->pi_lock
, flags
);
391 static void __kthread_bind(struct task_struct
*p
, unsigned int cpu
, long state
)
393 __kthread_bind_mask(p
, cpumask_of(cpu
), state
);
396 void kthread_bind_mask(struct task_struct
*p
, const struct cpumask
*mask
)
398 __kthread_bind_mask(p
, mask
, TASK_UNINTERRUPTIBLE
);
402 * kthread_bind - bind a just-created kthread to a cpu.
403 * @p: thread created by kthread_create().
404 * @cpu: cpu (might not be online, must be possible) for @k to run on.
406 * Description: This function is equivalent to set_cpus_allowed(),
407 * except that @cpu doesn't need to be online, and the thread must be
408 * stopped (i.e., just returned from kthread_create()).
410 void kthread_bind(struct task_struct
*p
, unsigned int cpu
)
412 __kthread_bind(p
, cpu
, TASK_UNINTERRUPTIBLE
);
414 EXPORT_SYMBOL(kthread_bind
);
417 * kthread_create_on_cpu - Create a cpu bound kthread
418 * @threadfn: the function to run until signal_pending(current).
419 * @data: data ptr for @threadfn.
420 * @cpu: The cpu on which the thread should be bound,
421 * @namefmt: printf-style name for the thread. Format is restricted
422 * to "name.*%u". Code fills in cpu number.
424 * Description: This helper function creates and names a kernel thread
425 * The thread will be woken and put into park mode.
427 struct task_struct
*kthread_create_on_cpu(int (*threadfn
)(void *data
),
428 void *data
, unsigned int cpu
,
431 struct task_struct
*p
;
433 p
= kthread_create_on_node(threadfn
, data
, cpu_to_node(cpu
), namefmt
,
437 kthread_bind(p
, cpu
);
438 /* CPU hotplug need to bind once again when unparking the thread. */
439 set_bit(KTHREAD_IS_PER_CPU
, &to_kthread(p
)->flags
);
440 to_kthread(p
)->cpu
= cpu
;
445 * kthread_unpark - unpark a thread created by kthread_create().
446 * @k: thread created by kthread_create().
448 * Sets kthread_should_park() for @k to return false, wakes it, and
449 * waits for it to return. If the thread is marked percpu then its
450 * bound to the cpu again.
452 void kthread_unpark(struct task_struct
*k
)
454 struct kthread
*kthread
= to_kthread(k
);
456 clear_bit(KTHREAD_SHOULD_PARK
, &kthread
->flags
);
458 * We clear the IS_PARKED bit here as we don't wait
459 * until the task has left the park code. So if we'd
460 * park before that happens we'd see the IS_PARKED bit
461 * which might be about to be cleared.
463 if (test_and_clear_bit(KTHREAD_IS_PARKED
, &kthread
->flags
)) {
465 * Newly created kthread was parked when the CPU was offline.
466 * The binding was lost and we need to set it again.
468 if (test_bit(KTHREAD_IS_PER_CPU
, &kthread
->flags
))
469 __kthread_bind(k
, kthread
->cpu
, TASK_PARKED
);
470 wake_up_state(k
, TASK_PARKED
);
473 EXPORT_SYMBOL_GPL(kthread_unpark
);
476 * kthread_park - park a thread created by kthread_create().
477 * @k: thread created by kthread_create().
479 * Sets kthread_should_park() for @k to return true, wakes it, and
480 * waits for it to return. This can also be called after kthread_create()
481 * instead of calling wake_up_process(): the thread will park without
482 * calling threadfn().
484 * Returns 0 if the thread is parked, -ENOSYS if the thread exited.
485 * If called by the kthread itself just the park bit is set.
487 int kthread_park(struct task_struct
*k
)
489 struct kthread
*kthread
= to_kthread(k
);
491 if (WARN_ON(k
->flags
& PF_EXITING
))
494 if (!test_bit(KTHREAD_IS_PARKED
, &kthread
->flags
)) {
495 set_bit(KTHREAD_SHOULD_PARK
, &kthread
->flags
);
498 wait_for_completion(&kthread
->parked
);
504 EXPORT_SYMBOL_GPL(kthread_park
);
507 * kthread_stop - stop a thread created by kthread_create().
508 * @k: thread created by kthread_create().
510 * Sets kthread_should_stop() for @k to return true, wakes it, and
511 * waits for it to exit. This can also be called after kthread_create()
512 * instead of calling wake_up_process(): the thread will exit without
513 * calling threadfn().
515 * If threadfn() may call do_exit() itself, the caller must ensure
516 * task_struct can't go away.
518 * Returns the result of threadfn(), or %-EINTR if wake_up_process()
521 int kthread_stop(struct task_struct
*k
)
523 struct kthread
*kthread
;
526 trace_sched_kthread_stop(k
);
529 kthread
= to_kthread(k
);
530 set_bit(KTHREAD_SHOULD_STOP
, &kthread
->flags
);
533 wait_for_completion(&kthread
->exited
);
537 trace_sched_kthread_stop_ret(ret
);
540 EXPORT_SYMBOL(kthread_stop
);
542 int kthreadd(void *unused
)
544 struct task_struct
*tsk
= current
;
546 /* Setup a clean context for our children to inherit. */
547 set_task_comm(tsk
, "kthreadd");
549 set_cpus_allowed_ptr(tsk
, cpu_all_mask
);
550 set_mems_allowed(node_states
[N_MEMORY
]);
552 current
->flags
|= PF_NOFREEZE
;
553 cgroup_init_kthreadd();
556 set_current_state(TASK_INTERRUPTIBLE
);
557 if (list_empty(&kthread_create_list
))
559 __set_current_state(TASK_RUNNING
);
561 spin_lock(&kthread_create_lock
);
562 while (!list_empty(&kthread_create_list
)) {
563 struct kthread_create_info
*create
;
565 create
= list_entry(kthread_create_list
.next
,
566 struct kthread_create_info
, list
);
567 list_del_init(&create
->list
);
568 spin_unlock(&kthread_create_lock
);
570 create_kthread(create
);
572 spin_lock(&kthread_create_lock
);
574 spin_unlock(&kthread_create_lock
);
580 void __kthread_init_worker(struct kthread_worker
*worker
,
582 struct lock_class_key
*key
)
584 memset(worker
, 0, sizeof(struct kthread_worker
));
585 spin_lock_init(&worker
->lock
);
586 lockdep_set_class_and_name(&worker
->lock
, key
, name
);
587 INIT_LIST_HEAD(&worker
->work_list
);
588 INIT_LIST_HEAD(&worker
->delayed_work_list
);
590 EXPORT_SYMBOL_GPL(__kthread_init_worker
);
593 * kthread_worker_fn - kthread function to process kthread_worker
594 * @worker_ptr: pointer to initialized kthread_worker
596 * This function implements the main cycle of kthread worker. It processes
597 * work_list until it is stopped with kthread_stop(). It sleeps when the queue
600 * The works are not allowed to keep any locks, disable preemption or interrupts
601 * when they finish. There is defined a safe point for freezing when one work
602 * finishes and before a new one is started.
604 * Also the works must not be handled by more than one worker at the same time,
605 * see also kthread_queue_work().
607 int kthread_worker_fn(void *worker_ptr
)
609 struct kthread_worker
*worker
= worker_ptr
;
610 struct kthread_work
*work
;
613 * FIXME: Update the check and remove the assignment when all kthread
614 * worker users are created using kthread_create_worker*() functions.
616 WARN_ON(worker
->task
&& worker
->task
!= current
);
617 worker
->task
= current
;
619 if (worker
->flags
& KTW_FREEZABLE
)
623 set_current_state(TASK_INTERRUPTIBLE
); /* mb paired w/ kthread_stop */
625 if (kthread_should_stop()) {
626 __set_current_state(TASK_RUNNING
);
627 spin_lock_irq(&worker
->lock
);
629 spin_unlock_irq(&worker
->lock
);
634 spin_lock_irq(&worker
->lock
);
635 if (!list_empty(&worker
->work_list
)) {
636 work
= list_first_entry(&worker
->work_list
,
637 struct kthread_work
, node
);
638 list_del_init(&work
->node
);
640 worker
->current_work
= work
;
641 spin_unlock_irq(&worker
->lock
);
644 __set_current_state(TASK_RUNNING
);
646 } else if (!freezing(current
))
652 EXPORT_SYMBOL_GPL(kthread_worker_fn
);
654 static __printf(3, 0) struct kthread_worker
*
655 __kthread_create_worker(int cpu
, unsigned int flags
,
656 const char namefmt
[], va_list args
)
658 struct kthread_worker
*worker
;
659 struct task_struct
*task
;
662 worker
= kzalloc(sizeof(*worker
), GFP_KERNEL
);
664 return ERR_PTR(-ENOMEM
);
666 kthread_init_worker(worker
);
669 node
= cpu_to_node(cpu
);
671 task
= __kthread_create_on_node(kthread_worker_fn
, worker
,
672 node
, namefmt
, args
);
677 kthread_bind(task
, cpu
);
679 worker
->flags
= flags
;
681 wake_up_process(task
);
686 return ERR_CAST(task
);
690 * kthread_create_worker - create a kthread worker
691 * @flags: flags modifying the default behavior of the worker
692 * @namefmt: printf-style name for the kthread worker (task).
694 * Returns a pointer to the allocated worker on success, ERR_PTR(-ENOMEM)
695 * when the needed structures could not get allocated, and ERR_PTR(-EINTR)
696 * when the worker was SIGKILLed.
698 struct kthread_worker
*
699 kthread_create_worker(unsigned int flags
, const char namefmt
[], ...)
701 struct kthread_worker
*worker
;
704 va_start(args
, namefmt
);
705 worker
= __kthread_create_worker(-1, flags
, namefmt
, args
);
710 EXPORT_SYMBOL(kthread_create_worker
);
713 * kthread_create_worker_on_cpu - create a kthread worker and bind it
714 * it to a given CPU and the associated NUMA node.
716 * @flags: flags modifying the default behavior of the worker
717 * @namefmt: printf-style name for the kthread worker (task).
719 * Use a valid CPU number if you want to bind the kthread worker
720 * to the given CPU and the associated NUMA node.
722 * A good practice is to add the cpu number also into the worker name.
723 * For example, use kthread_create_worker_on_cpu(cpu, "helper/%d", cpu).
725 * Returns a pointer to the allocated worker on success, ERR_PTR(-ENOMEM)
726 * when the needed structures could not get allocated, and ERR_PTR(-EINTR)
727 * when the worker was SIGKILLed.
729 struct kthread_worker
*
730 kthread_create_worker_on_cpu(int cpu
, unsigned int flags
,
731 const char namefmt
[], ...)
733 struct kthread_worker
*worker
;
736 va_start(args
, namefmt
);
737 worker
= __kthread_create_worker(cpu
, flags
, namefmt
, args
);
742 EXPORT_SYMBOL(kthread_create_worker_on_cpu
);
745 * Returns true when the work could not be queued at the moment.
746 * It happens when it is already pending in a worker list
747 * or when it is being cancelled.
749 static inline bool queuing_blocked(struct kthread_worker
*worker
,
750 struct kthread_work
*work
)
752 lockdep_assert_held(&worker
->lock
);
754 return !list_empty(&work
->node
) || work
->canceling
;
757 static void kthread_insert_work_sanity_check(struct kthread_worker
*worker
,
758 struct kthread_work
*work
)
760 lockdep_assert_held(&worker
->lock
);
761 WARN_ON_ONCE(!list_empty(&work
->node
));
762 /* Do not use a work with >1 worker, see kthread_queue_work() */
763 WARN_ON_ONCE(work
->worker
&& work
->worker
!= worker
);
766 /* insert @work before @pos in @worker */
767 static void kthread_insert_work(struct kthread_worker
*worker
,
768 struct kthread_work
*work
,
769 struct list_head
*pos
)
771 kthread_insert_work_sanity_check(worker
, work
);
773 list_add_tail(&work
->node
, pos
);
774 work
->worker
= worker
;
775 if (!worker
->current_work
&& likely(worker
->task
))
776 wake_up_process(worker
->task
);
780 * kthread_queue_work - queue a kthread_work
781 * @worker: target kthread_worker
782 * @work: kthread_work to queue
784 * Queue @work to work processor @task for async execution. @task
785 * must have been created with kthread_worker_create(). Returns %true
786 * if @work was successfully queued, %false if it was already pending.
788 * Reinitialize the work if it needs to be used by another worker.
789 * For example, when the worker was stopped and started again.
791 bool kthread_queue_work(struct kthread_worker
*worker
,
792 struct kthread_work
*work
)
797 spin_lock_irqsave(&worker
->lock
, flags
);
798 if (!queuing_blocked(worker
, work
)) {
799 kthread_insert_work(worker
, work
, &worker
->work_list
);
802 spin_unlock_irqrestore(&worker
->lock
, flags
);
805 EXPORT_SYMBOL_GPL(kthread_queue_work
);
808 * kthread_delayed_work_timer_fn - callback that queues the associated kthread
809 * delayed work when the timer expires.
810 * @__data: pointer to the data associated with the timer
812 * The format of the function is defined by struct timer_list.
813 * It should have been called from irqsafe timer with irq already off.
815 void kthread_delayed_work_timer_fn(unsigned long __data
)
817 struct kthread_delayed_work
*dwork
=
818 (struct kthread_delayed_work
*)__data
;
819 struct kthread_work
*work
= &dwork
->work
;
820 struct kthread_worker
*worker
= work
->worker
;
823 * This might happen when a pending work is reinitialized.
824 * It means that it is used a wrong way.
826 if (WARN_ON_ONCE(!worker
))
829 spin_lock(&worker
->lock
);
830 /* Work must not be used with >1 worker, see kthread_queue_work(). */
831 WARN_ON_ONCE(work
->worker
!= worker
);
833 /* Move the work from worker->delayed_work_list. */
834 WARN_ON_ONCE(list_empty(&work
->node
));
835 list_del_init(&work
->node
);
836 kthread_insert_work(worker
, work
, &worker
->work_list
);
838 spin_unlock(&worker
->lock
);
840 EXPORT_SYMBOL(kthread_delayed_work_timer_fn
);
842 void __kthread_queue_delayed_work(struct kthread_worker
*worker
,
843 struct kthread_delayed_work
*dwork
,
846 struct timer_list
*timer
= &dwork
->timer
;
847 struct kthread_work
*work
= &dwork
->work
;
849 WARN_ON_ONCE(timer
->function
!= kthread_delayed_work_timer_fn
||
850 timer
->data
!= (unsigned long)dwork
);
853 * If @delay is 0, queue @dwork->work immediately. This is for
854 * both optimization and correctness. The earliest @timer can
855 * expire is on the closest next tick and delayed_work users depend
856 * on that there's no such delay when @delay is 0.
859 kthread_insert_work(worker
, work
, &worker
->work_list
);
863 /* Be paranoid and try to detect possible races already now. */
864 kthread_insert_work_sanity_check(worker
, work
);
866 list_add(&work
->node
, &worker
->delayed_work_list
);
867 work
->worker
= worker
;
868 timer_stats_timer_set_start_info(&dwork
->timer
);
869 timer
->expires
= jiffies
+ delay
;
874 * kthread_queue_delayed_work - queue the associated kthread work
876 * @worker: target kthread_worker
877 * @dwork: kthread_delayed_work to queue
878 * @delay: number of jiffies to wait before queuing
880 * If the work has not been pending it starts a timer that will queue
881 * the work after the given @delay. If @delay is zero, it queues the
884 * Return: %false if the @work has already been pending. It means that
885 * either the timer was running or the work was queued. It returns %true
888 bool kthread_queue_delayed_work(struct kthread_worker
*worker
,
889 struct kthread_delayed_work
*dwork
,
892 struct kthread_work
*work
= &dwork
->work
;
896 spin_lock_irqsave(&worker
->lock
, flags
);
898 if (!queuing_blocked(worker
, work
)) {
899 __kthread_queue_delayed_work(worker
, dwork
, delay
);
903 spin_unlock_irqrestore(&worker
->lock
, flags
);
906 EXPORT_SYMBOL_GPL(kthread_queue_delayed_work
);
908 struct kthread_flush_work
{
909 struct kthread_work work
;
910 struct completion done
;
913 static void kthread_flush_work_fn(struct kthread_work
*work
)
915 struct kthread_flush_work
*fwork
=
916 container_of(work
, struct kthread_flush_work
, work
);
917 complete(&fwork
->done
);
921 * kthread_flush_work - flush a kthread_work
922 * @work: work to flush
924 * If @work is queued or executing, wait for it to finish execution.
926 void kthread_flush_work(struct kthread_work
*work
)
928 struct kthread_flush_work fwork
= {
929 KTHREAD_WORK_INIT(fwork
.work
, kthread_flush_work_fn
),
930 COMPLETION_INITIALIZER_ONSTACK(fwork
.done
),
932 struct kthread_worker
*worker
;
935 worker
= work
->worker
;
939 spin_lock_irq(&worker
->lock
);
940 /* Work must not be used with >1 worker, see kthread_queue_work(). */
941 WARN_ON_ONCE(work
->worker
!= worker
);
943 if (!list_empty(&work
->node
))
944 kthread_insert_work(worker
, &fwork
.work
, work
->node
.next
);
945 else if (worker
->current_work
== work
)
946 kthread_insert_work(worker
, &fwork
.work
,
947 worker
->work_list
.next
);
951 spin_unlock_irq(&worker
->lock
);
954 wait_for_completion(&fwork
.done
);
956 EXPORT_SYMBOL_GPL(kthread_flush_work
);
959 * This function removes the work from the worker queue. Also it makes sure
960 * that it won't get queued later via the delayed work's timer.
962 * The work might still be in use when this function finishes. See the
963 * current_work proceed by the worker.
965 * Return: %true if @work was pending and successfully canceled,
966 * %false if @work was not pending
968 static bool __kthread_cancel_work(struct kthread_work
*work
, bool is_dwork
,
969 unsigned long *flags
)
971 /* Try to cancel the timer if exists. */
973 struct kthread_delayed_work
*dwork
=
974 container_of(work
, struct kthread_delayed_work
, work
);
975 struct kthread_worker
*worker
= work
->worker
;
978 * del_timer_sync() must be called to make sure that the timer
979 * callback is not running. The lock must be temporary released
980 * to avoid a deadlock with the callback. In the meantime,
981 * any queuing is blocked by setting the canceling counter.
984 spin_unlock_irqrestore(&worker
->lock
, *flags
);
985 del_timer_sync(&dwork
->timer
);
986 spin_lock_irqsave(&worker
->lock
, *flags
);
991 * Try to remove the work from a worker list. It might either
992 * be from worker->work_list or from worker->delayed_work_list.
994 if (!list_empty(&work
->node
)) {
995 list_del_init(&work
->node
);
1003 * kthread_mod_delayed_work - modify delay of or queue a kthread delayed work
1004 * @worker: kthread worker to use
1005 * @dwork: kthread delayed work to queue
1006 * @delay: number of jiffies to wait before queuing
1008 * If @dwork is idle, equivalent to kthread_queue_delayed_work(). Otherwise,
1009 * modify @dwork's timer so that it expires after @delay. If @delay is zero,
1010 * @work is guaranteed to be queued immediately.
1012 * Return: %true if @dwork was pending and its timer was modified,
1015 * A special case is when the work is being canceled in parallel.
1016 * It might be caused either by the real kthread_cancel_delayed_work_sync()
1017 * or yet another kthread_mod_delayed_work() call. We let the other command
1018 * win and return %false here. The caller is supposed to synchronize these
1019 * operations a reasonable way.
1021 * This function is safe to call from any context including IRQ handler.
1022 * See __kthread_cancel_work() and kthread_delayed_work_timer_fn()
1025 bool kthread_mod_delayed_work(struct kthread_worker
*worker
,
1026 struct kthread_delayed_work
*dwork
,
1027 unsigned long delay
)
1029 struct kthread_work
*work
= &dwork
->work
;
1030 unsigned long flags
;
1033 spin_lock_irqsave(&worker
->lock
, flags
);
1035 /* Do not bother with canceling when never queued. */
1039 /* Work must not be used with >1 worker, see kthread_queue_work() */
1040 WARN_ON_ONCE(work
->worker
!= worker
);
1042 /* Do not fight with another command that is canceling this work. */
1043 if (work
->canceling
)
1046 ret
= __kthread_cancel_work(work
, true, &flags
);
1048 __kthread_queue_delayed_work(worker
, dwork
, delay
);
1050 spin_unlock_irqrestore(&worker
->lock
, flags
);
1053 EXPORT_SYMBOL_GPL(kthread_mod_delayed_work
);
1055 static bool __kthread_cancel_work_sync(struct kthread_work
*work
, bool is_dwork
)
1057 struct kthread_worker
*worker
= work
->worker
;
1058 unsigned long flags
;
1064 spin_lock_irqsave(&worker
->lock
, flags
);
1065 /* Work must not be used with >1 worker, see kthread_queue_work(). */
1066 WARN_ON_ONCE(work
->worker
!= worker
);
1068 ret
= __kthread_cancel_work(work
, is_dwork
, &flags
);
1070 if (worker
->current_work
!= work
)
1074 * The work is in progress and we need to wait with the lock released.
1075 * In the meantime, block any queuing by setting the canceling counter.
1078 spin_unlock_irqrestore(&worker
->lock
, flags
);
1079 kthread_flush_work(work
);
1080 spin_lock_irqsave(&worker
->lock
, flags
);
1084 spin_unlock_irqrestore(&worker
->lock
, flags
);
1090 * kthread_cancel_work_sync - cancel a kthread work and wait for it to finish
1091 * @work: the kthread work to cancel
1093 * Cancel @work and wait for its execution to finish. This function
1094 * can be used even if the work re-queues itself. On return from this
1095 * function, @work is guaranteed to be not pending or executing on any CPU.
1097 * kthread_cancel_work_sync(&delayed_work->work) must not be used for
1098 * delayed_work's. Use kthread_cancel_delayed_work_sync() instead.
1100 * The caller must ensure that the worker on which @work was last
1101 * queued can't be destroyed before this function returns.
1103 * Return: %true if @work was pending, %false otherwise.
1105 bool kthread_cancel_work_sync(struct kthread_work
*work
)
1107 return __kthread_cancel_work_sync(work
, false);
1109 EXPORT_SYMBOL_GPL(kthread_cancel_work_sync
);
1112 * kthread_cancel_delayed_work_sync - cancel a kthread delayed work and
1113 * wait for it to finish.
1114 * @dwork: the kthread delayed work to cancel
1116 * This is kthread_cancel_work_sync() for delayed works.
1118 * Return: %true if @dwork was pending, %false otherwise.
1120 bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work
*dwork
)
1122 return __kthread_cancel_work_sync(&dwork
->work
, true);
1124 EXPORT_SYMBOL_GPL(kthread_cancel_delayed_work_sync
);
1127 * kthread_flush_worker - flush all current works on a kthread_worker
1128 * @worker: worker to flush
1130 * Wait until all currently executing or pending works on @worker are
1133 void kthread_flush_worker(struct kthread_worker
*worker
)
1135 struct kthread_flush_work fwork
= {
1136 KTHREAD_WORK_INIT(fwork
.work
, kthread_flush_work_fn
),
1137 COMPLETION_INITIALIZER_ONSTACK(fwork
.done
),
1140 kthread_queue_work(worker
, &fwork
.work
);
1141 wait_for_completion(&fwork
.done
);
1143 EXPORT_SYMBOL_GPL(kthread_flush_worker
);
1146 * kthread_destroy_worker - destroy a kthread worker
1147 * @worker: worker to be destroyed
1149 * Flush and destroy @worker. The simple flush is enough because the kthread
1150 * worker API is used only in trivial scenarios. There are no multi-step state
1153 void kthread_destroy_worker(struct kthread_worker
*worker
)
1155 struct task_struct
*task
;
1157 task
= worker
->task
;
1161 kthread_flush_worker(worker
);
1163 WARN_ON(!list_empty(&worker
->work_list
));
1166 EXPORT_SYMBOL(kthread_destroy_worker
);