blob: 4dcbc8b5d6b6bda4a38b571f83c03fea76998400 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Kernel thread helper functions.
2 * Copyright (C) 2004 IBM Corporation, Rusty Russell.
3 *
Eric W. Biederman73c27992007-05-09 02:34:32 -07004 * Creation is done via kthreadd, so that we get a clean environment
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * even if we're invoked from userspace (think modprobe, hotplug cpu,
6 * etc.).
7 */
8#include <linux/sched.h>
9#include <linux/kthread.h>
10#include <linux/completion.h>
11#include <linux/err.h>
Miao Xie58568d22009-06-16 15:31:49 -070012#include <linux/cpuset.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/unistd.h>
14#include <linux/file.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040015#include <linux/export.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080016#include <linux/mutex.h>
Tejun Heob56c0d82010-06-29 10:07:09 +020017#include <linux/slab.h>
18#include <linux/freezer.h>
Al Viroa74fb732012-10-10 21:28:25 -040019#include <linux/ptrace.h>
Tejun Heocd42d552013-04-30 15:27:21 -070020#include <linux/uaccess.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040021#include <trace/events/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Eric W. Biederman73c27992007-05-09 02:34:32 -070023static DEFINE_SPINLOCK(kthread_create_lock);
24static LIST_HEAD(kthread_create_list);
25struct task_struct *kthreadd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27struct kthread_create_info
28{
Eric W. Biederman73c27992007-05-09 02:34:32 -070029 /* Information passed to kthread() from kthreadd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 int (*threadfn)(void *data);
31 void *data;
Eric Dumazet207205a2011-03-22 16:30:44 -070032 int node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Eric W. Biederman73c27992007-05-09 02:34:32 -070034 /* Result passed back to kthread_create() from kthreadd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 struct task_struct *result;
Tetsuo Handa786235ee2013-11-12 15:06:45 -080036 struct completion *done;
David Howells65f27f32006-11-22 14:55:48 +000037
Eric W. Biederman73c27992007-05-09 02:34:32 -070038 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
Oleg Nesterov63706172009-06-17 16:27:45 -070041struct kthread {
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000042 unsigned long flags;
43 unsigned int cpu;
Tejun Heo82805ab2010-06-29 10:07:09 +020044 void *data;
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000045 struct completion parked;
Oleg Nesterov63706172009-06-17 16:27:45 -070046 struct completion exited;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000049enum KTHREAD_BITS {
50 KTHREAD_IS_PER_CPU = 0,
51 KTHREAD_SHOULD_STOP,
52 KTHREAD_SHOULD_PARK,
53 KTHREAD_IS_PARKED,
54};
55
Oleg Nesterov1da5c462016-11-29 18:50:57 +010056static inline void set_kthread_struct(void *kthread)
57{
58 /*
59 * We abuse ->set_child_tid to avoid the new member and because it
60 * can't be wrongly copied by copy_process(). We also rely on fact
61 * that the caller can't exec, so PF_KTHREAD can't be cleared.
62 */
63 current->set_child_tid = (__force void __user *)kthread;
64}
Oleg Nesterov4ecdafc2013-04-29 15:05:01 -070065
66static inline struct kthread *to_kthread(struct task_struct *k)
67{
Oleg Nesterov1da5c462016-11-29 18:50:57 +010068 WARN_ON(!(k->flags & PF_KTHREAD));
69 return (__force void *)k->set_child_tid;
Oleg Nesterov4ecdafc2013-04-29 15:05:01 -070070}
71
Oleg Nesterov1da5c462016-11-29 18:50:57 +010072void free_kthread_struct(struct task_struct *k)
73{
74 /*
75 * Can be NULL if this kthread was created by kernel_thread()
76 * or if kmalloc() in kthread() failed.
77 */
78 kfree(to_kthread(k));
79}
80
81#define __to_kthread(vfork) \
82 container_of(vfork, struct kthread, exited)
83
84/*
85 * TODO: kill it and use to_kthread(). But we still need the users
86 * like kthread_stop() which has to sync with the exiting kthread.
87 */
Oleg Nesterov4ecdafc2013-04-29 15:05:01 -070088static struct kthread *to_live_kthread(struct task_struct *k)
89{
90 struct completion *vfork = ACCESS_ONCE(k->vfork_done);
Oleg Nesteroveff96622016-11-29 18:51:00 +010091 if (likely(vfork))
Oleg Nesterov4ecdafc2013-04-29 15:05:01 -070092 return __to_kthread(vfork);
93 return NULL;
94}
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Randy Dunlap9e37bd32006-06-25 05:49:19 -070096/**
97 * kthread_should_stop - should this kthread return now?
98 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080099 * When someone calls kthread_stop() on your kthread, it will be woken
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700100 * and this will return true. You should then return, and your return
101 * value will be passed through to kthread_stop().
102 */
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000103bool kthread_should_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000105 return test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107EXPORT_SYMBOL(kthread_should_stop);
108
Tejun Heo82805ab2010-06-29 10:07:09 +0200109/**
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000110 * kthread_should_park - should this kthread park now?
111 *
112 * When someone calls kthread_park() on your kthread, it will be woken
113 * and this will return true. You should then do the necessary
114 * cleanup and call kthread_parkme()
115 *
116 * Similar to kthread_should_stop(), but this keeps the thread alive
117 * and in a park position. kthread_unpark() "restarts" the thread and
118 * calls the thread function again.
119 */
120bool kthread_should_park(void)
121{
122 return test_bit(KTHREAD_SHOULD_PARK, &to_kthread(current)->flags);
123}
David Kershner18896452015-08-06 15:46:45 -0700124EXPORT_SYMBOL_GPL(kthread_should_park);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000125
126/**
Tejun Heo8a32c442011-11-21 12:32:23 -0800127 * kthread_freezable_should_stop - should this freezable kthread return now?
128 * @was_frozen: optional out parameter, indicates whether %current was frozen
129 *
130 * kthread_should_stop() for freezable kthreads, which will enter
131 * refrigerator if necessary. This function is safe from kthread_stop() /
132 * freezer deadlock and freezable kthreads should use this function instead
133 * of calling try_to_freeze() directly.
134 */
135bool kthread_freezable_should_stop(bool *was_frozen)
136{
137 bool frozen = false;
138
139 might_sleep();
140
141 if (unlikely(freezing(current)))
142 frozen = __refrigerator(true);
143
144 if (was_frozen)
145 *was_frozen = frozen;
146
147 return kthread_should_stop();
148}
149EXPORT_SYMBOL_GPL(kthread_freezable_should_stop);
150
151/**
Tejun Heo82805ab2010-06-29 10:07:09 +0200152 * kthread_data - return data value specified on kthread creation
153 * @task: kthread task in question
154 *
155 * Return the data value specified when kthread @task was created.
156 * The caller is responsible for ensuring the validity of @task when
157 * calling this function.
158 */
159void *kthread_data(struct task_struct *task)
160{
161 return to_kthread(task)->data;
162}
163
Tejun Heocd42d552013-04-30 15:27:21 -0700164/**
Petr Mladeke7005912016-10-11 13:55:17 -0700165 * kthread_probe_data - speculative version of kthread_data()
Tejun Heocd42d552013-04-30 15:27:21 -0700166 * @task: possible kthread task in question
167 *
168 * @task could be a kthread task. Return the data value specified when it
169 * was created if accessible. If @task isn't a kthread task or its data is
170 * inaccessible for any reason, %NULL is returned. This function requires
171 * that @task itself is safe to dereference.
172 */
Petr Mladeke7005912016-10-11 13:55:17 -0700173void *kthread_probe_data(struct task_struct *task)
Tejun Heocd42d552013-04-30 15:27:21 -0700174{
175 struct kthread *kthread = to_kthread(task);
176 void *data = NULL;
177
178 probe_kernel_read(&data, &kthread->data, sizeof(data));
179 return data;
180}
181
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000182static void __kthread_parkme(struct kthread *self)
183{
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200184 __set_current_state(TASK_PARKED);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000185 while (test_bit(KTHREAD_SHOULD_PARK, &self->flags)) {
186 if (!test_and_set_bit(KTHREAD_IS_PARKED, &self->flags))
187 complete(&self->parked);
188 schedule();
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200189 __set_current_state(TASK_PARKED);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000190 }
191 clear_bit(KTHREAD_IS_PARKED, &self->flags);
192 __set_current_state(TASK_RUNNING);
193}
194
195void kthread_parkme(void)
196{
197 __kthread_parkme(to_kthread(current));
198}
David Kershner18896452015-08-06 15:46:45 -0700199EXPORT_SYMBOL_GPL(kthread_parkme);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int kthread(void *_create)
202{
Eric W. Biederman73c27992007-05-09 02:34:32 -0700203 /* Copy data: it's on kthread's stack */
Oleg Nesterov63706172009-06-17 16:27:45 -0700204 struct kthread_create_info *create = _create;
205 int (*threadfn)(void *data) = create->threadfn;
206 void *data = create->data;
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800207 struct completion *done;
Oleg Nesterov1da5c462016-11-29 18:50:57 +0100208 struct kthread *self;
Oleg Nesterov63706172009-06-17 16:27:45 -0700209 int ret;
210
Oleg Nesterov1da5c462016-11-29 18:50:57 +0100211 self = kmalloc(sizeof(*self), GFP_KERNEL);
212 set_kthread_struct(self);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800214 /* If user was SIGKILLed, I release the structure. */
215 done = xchg(&create->done, NULL);
216 if (!done) {
217 kfree(create);
218 do_exit(-EINTR);
219 }
Oleg Nesterov1da5c462016-11-29 18:50:57 +0100220
221 if (!self) {
222 create->result = ERR_PTR(-ENOMEM);
223 complete(done);
224 do_exit(-ENOMEM);
225 }
226
227 self->flags = 0;
228 self->data = data;
229 init_completion(&self->exited);
230 init_completion(&self->parked);
231 current->vfork_done = &self->exited;
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* OK, tell user we're spawned, wait for stop or wakeup */
Oleg Nesterova076e4b2007-05-23 13:57:27 -0700234 __set_current_state(TASK_UNINTERRUPTIBLE);
Vitaliy Gusev3217ab92009-04-09 09:50:35 -0600235 create->result = current;
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800236 complete(done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 schedule();
238
Oleg Nesterov63706172009-06-17 16:27:45 -0700239 ret = -EINTR;
Oleg Nesterov1da5c462016-11-29 18:50:57 +0100240 if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) {
241 __kthread_parkme(self);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000242 ret = threadfn(data);
243 }
Oleg Nesterov63706172009-06-17 16:27:45 -0700244 do_exit(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Eric Dumazet207205a2011-03-22 16:30:44 -0700247/* called from do_fork() to get node information for about to be created task */
248int tsk_fork_get_node(struct task_struct *tsk)
249{
250#ifdef CONFIG_NUMA
251 if (tsk == kthreadd_task)
252 return tsk->pref_node_fork;
253#endif
Nishanth Aravamudan81c98862014-04-03 14:46:25 -0700254 return NUMA_NO_NODE;
Eric Dumazet207205a2011-03-22 16:30:44 -0700255}
256
Eric W. Biederman73c27992007-05-09 02:34:32 -0700257static void create_kthread(struct kthread_create_info *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 int pid;
260
Eric Dumazet207205a2011-03-22 16:30:44 -0700261#ifdef CONFIG_NUMA
262 current->pref_node_fork = create->node;
263#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /* We want our own signal handler (we take no signals by default). */
265 pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
Oleg Nesterovcdd140b2009-06-17 16:27:43 -0700266 if (pid < 0) {
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800267 /* If user was SIGKILLed, I release the structure. */
268 struct completion *done = xchg(&create->done, NULL);
269
270 if (!done) {
271 kfree(create);
272 return;
273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 create->result = ERR_PTR(pid);
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800275 complete(done);
Oleg Nesterovcdd140b2009-06-17 16:27:43 -0700276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Petr Mladek255451e2016-10-11 13:55:27 -0700279static struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data),
280 void *data, int node,
281 const char namefmt[],
282 va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800284 DECLARE_COMPLETION_ONSTACK(done);
285 struct task_struct *task;
286 struct kthread_create_info *create = kmalloc(sizeof(*create),
287 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800289 if (!create)
290 return ERR_PTR(-ENOMEM);
291 create->threadfn = threadfn;
292 create->data = data;
293 create->node = node;
294 create->done = &done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Eric W. Biederman73c27992007-05-09 02:34:32 -0700296 spin_lock(&kthread_create_lock);
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800297 list_add_tail(&create->list, &kthread_create_list);
Eric W. Biederman73c27992007-05-09 02:34:32 -0700298 spin_unlock(&kthread_create_lock);
299
Dmitry Adamushkocbd9b672008-04-29 00:59:23 -0700300 wake_up_process(kthreadd_task);
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800301 /*
302 * Wait for completion in killable state, for I might be chosen by
303 * the OOM killer while kthreadd is trying to allocate memory for
304 * new kernel thread.
305 */
306 if (unlikely(wait_for_completion_killable(&done))) {
307 /*
308 * If I was SIGKILLed before kthreadd (or new kernel thread)
309 * calls complete(), leave the cleanup of this structure to
310 * that thread.
311 */
312 if (xchg(&create->done, NULL))
Tetsuo Handa8fe69292014-06-04 16:05:36 -0700313 return ERR_PTR(-EINTR);
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800314 /*
315 * kthreadd (or new kernel thread) will call complete()
316 * shortly.
317 */
318 wait_for_completion(&done);
319 }
320 task = create->result;
321 if (!IS_ERR(task)) {
Peter Zijlstrac9b5f502011-01-07 13:41:40 +0100322 static const struct sched_param param = { .sched_priority = 0 };
Oleg Nesterov1c993152009-04-09 09:50:36 -0600323
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800324 vsnprintf(task->comm, sizeof(task->comm), namefmt, args);
Oleg Nesterov1c993152009-04-09 09:50:36 -0600325 /*
326 * root may have changed our (kthreadd's) priority or CPU mask.
327 * The kernel thread should not inherit these properties.
328 */
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800329 sched_setscheduler_nocheck(task, SCHED_NORMAL, &param);
330 set_cpus_allowed_ptr(task, cpu_all_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800332 kfree(create);
333 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
Petr Mladek255451e2016-10-11 13:55:27 -0700335
336/**
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.
342 *
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.
347 *
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().
356 *
357 * Returns a task_struct or ERR_PTR(-ENOMEM) or ERR_PTR(-EINTR).
358 */
359struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
360 void *data, int node,
361 const char namefmt[],
362 ...)
363{
364 struct task_struct *task;
365 va_list args;
366
367 va_start(args, namefmt);
368 task = __kthread_create_on_node(threadfn, data, node, namefmt, args);
369 va_end(args);
370
371 return task;
372}
Eric Dumazet207205a2011-03-22 16:30:44 -0700373EXPORT_SYMBOL(kthread_create_on_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Peter Zijlstra25834c72015-05-15 17:43:34 +0200375static void __kthread_bind_mask(struct task_struct *p, const struct cpumask *mask, long state)
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000376{
Peter Zijlstra25834c72015-05-15 17:43:34 +0200377 unsigned long flags;
378
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200379 if (!wait_task_inactive(p, state)) {
380 WARN_ON(1);
381 return;
382 }
Peter Zijlstra25834c72015-05-15 17:43:34 +0200383
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000384 /* It's safe because the task is inactive. */
Peter Zijlstra25834c72015-05-15 17:43:34 +0200385 raw_spin_lock_irqsave(&p->pi_lock, flags);
386 do_set_cpus_allowed(p, mask);
Tejun Heo14a40ff2013-03-19 13:45:20 -0700387 p->flags |= PF_NO_SETAFFINITY;
Peter Zijlstra25834c72015-05-15 17:43:34 +0200388 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
389}
390
391static void __kthread_bind(struct task_struct *p, unsigned int cpu, long state)
392{
393 __kthread_bind_mask(p, cpumask_of(cpu), state);
394}
395
396void kthread_bind_mask(struct task_struct *p, const struct cpumask *mask)
397{
398 __kthread_bind_mask(p, mask, TASK_UNINTERRUPTIBLE);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000399}
400
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700401/**
Peter Zijlstra881232b2009-12-16 18:04:39 +0100402 * 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.
405 *
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()).
409 */
410void kthread_bind(struct task_struct *p, unsigned int cpu)
411{
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200412 __kthread_bind(p, cpu, TASK_UNINTERRUPTIBLE);
Peter Zijlstra881232b2009-12-16 18:04:39 +0100413}
414EXPORT_SYMBOL(kthread_bind);
415
416/**
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000417 * 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.
423 *
424 * Description: This helper function creates and names a kernel thread
425 * The thread will be woken and put into park mode.
426 */
427struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
428 void *data, unsigned int cpu,
429 const char *namefmt)
430{
431 struct task_struct *p;
432
Nishanth Aravamudan10922832014-10-09 15:26:18 -0700433 p = kthread_create_on_node(threadfn, data, cpu_to_node(cpu), namefmt,
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000434 cpu);
435 if (IS_ERR(p))
436 return p;
Petr Mladeka65d4092016-10-11 13:55:23 -0700437 kthread_bind(p, cpu);
438 /* CPU hotplug need to bind once again when unparking the thread. */
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000439 set_bit(KTHREAD_IS_PER_CPU, &to_kthread(p)->flags);
440 to_kthread(p)->cpu = cpu;
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000441 return p;
442}
443
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200444static void __kthread_unpark(struct task_struct *k, struct kthread *kthread)
445{
446 clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
447 /*
448 * We clear the IS_PARKED bit here as we don't wait
449 * until the task has left the park code. So if we'd
450 * park before that happens we'd see the IS_PARKED bit
451 * which might be about to be cleared.
452 */
453 if (test_and_clear_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
Petr Mladeka65d4092016-10-11 13:55:23 -0700454 /*
455 * Newly created kthread was parked when the CPU was offline.
456 * The binding was lost and we need to set it again.
457 */
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200458 if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags))
459 __kthread_bind(k, kthread->cpu, TASK_PARKED);
460 wake_up_state(k, TASK_PARKED);
461 }
462}
463
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000464/**
465 * kthread_unpark - unpark a thread created by kthread_create().
466 * @k: thread created by kthread_create().
467 *
468 * Sets kthread_should_park() for @k to return false, wakes it, and
469 * waits for it to return. If the thread is marked percpu then its
470 * bound to the cpu again.
471 */
472void kthread_unpark(struct task_struct *k)
473{
Oleg Nesterovb5c54422013-04-29 15:05:12 -0700474 struct kthread *kthread = to_live_kthread(k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000475
Oleg Nesteroveff96622016-11-29 18:51:00 +0100476 if (kthread)
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200477 __kthread_unpark(k, kthread);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000478}
David Kershner18896452015-08-06 15:46:45 -0700479EXPORT_SYMBOL_GPL(kthread_unpark);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000480
481/**
482 * kthread_park - park a thread created by kthread_create().
483 * @k: thread created by kthread_create().
484 *
485 * Sets kthread_should_park() for @k to return true, wakes it, and
486 * waits for it to return. This can also be called after kthread_create()
487 * instead of calling wake_up_process(): the thread will park without
488 * calling threadfn().
489 *
490 * Returns 0 if the thread is parked, -ENOSYS if the thread exited.
491 * If called by the kthread itself just the park bit is set.
492 */
493int kthread_park(struct task_struct *k)
494{
Oleg Nesterovb5c54422013-04-29 15:05:12 -0700495 struct kthread *kthread = to_live_kthread(k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000496 int ret = -ENOSYS;
497
498 if (kthread) {
499 if (!test_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
500 set_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
501 if (k != current) {
502 wake_up_process(k);
503 wait_for_completion(&kthread->parked);
504 }
505 }
506 ret = 0;
507 }
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000508 return ret;
509}
David Kershner18896452015-08-06 15:46:45 -0700510EXPORT_SYMBOL_GPL(kthread_park);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000511
512/**
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700513 * kthread_stop - stop a thread created by kthread_create().
514 * @k: thread created by kthread_create().
515 *
516 * Sets kthread_should_stop() for @k to return true, wakes it, and
Oleg Nesterov9ae26022009-06-19 02:51:13 +0200517 * waits for it to exit. This can also be called after kthread_create()
518 * instead of calling wake_up_process(): the thread will exit without
519 * calling threadfn().
520 *
521 * If threadfn() may call do_exit() itself, the caller must ensure
522 * task_struct can't go away.
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700523 *
524 * Returns the result of threadfn(), or %-EINTR if wake_up_process()
525 * was never called.
526 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527int kthread_stop(struct task_struct *k)
528{
Oleg Nesterovb5c54422013-04-29 15:05:12 -0700529 struct kthread *kthread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 int ret;
531
Oleg Nesterov63706172009-06-17 16:27:45 -0700532 trace_sched_kthread_stop(k);
Oleg Nesterovb5c54422013-04-29 15:05:12 -0700533
534 get_task_struct(k);
Oleg Nesterovefb29fb2016-11-29 18:51:03 +0100535 kthread = to_kthread(k);
536 set_bit(KTHREAD_SHOULD_STOP, &kthread->flags);
537 __kthread_unpark(k, kthread);
538 wake_up_process(k);
539 wait_for_completion(&kthread->exited);
Oleg Nesterov63706172009-06-17 16:27:45 -0700540 ret = k->exit_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 put_task_struct(k);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400542
Oleg Nesterovb5c54422013-04-29 15:05:12 -0700543 trace_sched_kthread_stop_ret(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return ret;
545}
Adrian Bunk52e92e52006-07-14 00:24:05 -0700546EXPORT_SYMBOL(kthread_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Satyam Sharmae804a4a2007-07-31 00:39:16 -0700548int kthreadd(void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Eric W. Biederman73c27992007-05-09 02:34:32 -0700550 struct task_struct *tsk = current;
Eric W. Biederman73c27992007-05-09 02:34:32 -0700551
Satyam Sharmae804a4a2007-07-31 00:39:16 -0700552 /* Setup a clean context for our children to inherit. */
Eric W. Biederman73c27992007-05-09 02:34:32 -0700553 set_task_comm(tsk, "kthreadd");
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700554 ignore_signals(tsk);
Rusty Russell1a2142a2009-03-30 22:05:10 -0600555 set_cpus_allowed_ptr(tsk, cpu_all_mask);
Lai Jiangshanaee4faa2012-12-12 13:51:39 -0800556 set_mems_allowed(node_states[N_MEMORY]);
Eric W. Biederman73c27992007-05-09 02:34:32 -0700557
Tejun Heo34b087e2011-11-23 09:28:17 -0800558 current->flags |= PF_NOFREEZE;
Eric W. Biederman73c27992007-05-09 02:34:32 -0700559
560 for (;;) {
561 set_current_state(TASK_INTERRUPTIBLE);
562 if (list_empty(&kthread_create_list))
563 schedule();
564 __set_current_state(TASK_RUNNING);
565
566 spin_lock(&kthread_create_lock);
567 while (!list_empty(&kthread_create_list)) {
568 struct kthread_create_info *create;
569
570 create = list_entry(kthread_create_list.next,
571 struct kthread_create_info, list);
572 list_del_init(&create->list);
573 spin_unlock(&kthread_create_lock);
574
575 create_kthread(create);
576
577 spin_lock(&kthread_create_lock);
578 }
579 spin_unlock(&kthread_create_lock);
580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 return 0;
583}
Tejun Heob56c0d82010-06-29 10:07:09 +0200584
Petr Mladek39891442016-10-11 13:55:20 -0700585void __kthread_init_worker(struct kthread_worker *worker,
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100586 const char *name,
587 struct lock_class_key *key)
588{
Petr Mladekdbf52682016-10-11 13:55:50 -0700589 memset(worker, 0, sizeof(struct kthread_worker));
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100590 spin_lock_init(&worker->lock);
591 lockdep_set_class_and_name(&worker->lock, key, name);
592 INIT_LIST_HEAD(&worker->work_list);
Petr Mladek22597dc2016-10-11 13:55:40 -0700593 INIT_LIST_HEAD(&worker->delayed_work_list);
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100594}
Petr Mladek39891442016-10-11 13:55:20 -0700595EXPORT_SYMBOL_GPL(__kthread_init_worker);
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100596
Tejun Heob56c0d82010-06-29 10:07:09 +0200597/**
598 * kthread_worker_fn - kthread function to process kthread_worker
599 * @worker_ptr: pointer to initialized kthread_worker
600 *
Petr Mladekfbae2d42016-10-11 13:55:30 -0700601 * This function implements the main cycle of kthread worker. It processes
602 * work_list until it is stopped with kthread_stop(). It sleeps when the queue
603 * is empty.
Tejun Heob56c0d82010-06-29 10:07:09 +0200604 *
Petr Mladekfbae2d42016-10-11 13:55:30 -0700605 * The works are not allowed to keep any locks, disable preemption or interrupts
606 * when they finish. There is defined a safe point for freezing when one work
607 * finishes and before a new one is started.
Petr Mladek8197b3d42016-10-11 13:55:36 -0700608 *
609 * Also the works must not be handled by more than one worker at the same time,
610 * see also kthread_queue_work().
Tejun Heob56c0d82010-06-29 10:07:09 +0200611 */
612int kthread_worker_fn(void *worker_ptr)
613{
614 struct kthread_worker *worker = worker_ptr;
615 struct kthread_work *work;
616
Petr Mladekfbae2d42016-10-11 13:55:30 -0700617 /*
618 * FIXME: Update the check and remove the assignment when all kthread
619 * worker users are created using kthread_create_worker*() functions.
620 */
621 WARN_ON(worker->task && worker->task != current);
Tejun Heob56c0d82010-06-29 10:07:09 +0200622 worker->task = current;
Petr Mladekdbf52682016-10-11 13:55:50 -0700623
624 if (worker->flags & KTW_FREEZABLE)
625 set_freezable();
626
Tejun Heob56c0d82010-06-29 10:07:09 +0200627repeat:
628 set_current_state(TASK_INTERRUPTIBLE); /* mb paired w/ kthread_stop */
629
630 if (kthread_should_stop()) {
631 __set_current_state(TASK_RUNNING);
632 spin_lock_irq(&worker->lock);
633 worker->task = NULL;
634 spin_unlock_irq(&worker->lock);
635 return 0;
636 }
637
638 work = NULL;
639 spin_lock_irq(&worker->lock);
640 if (!list_empty(&worker->work_list)) {
641 work = list_first_entry(&worker->work_list,
642 struct kthread_work, node);
643 list_del_init(&work->node);
644 }
Tejun Heo46f3d972012-07-19 13:52:53 -0700645 worker->current_work = work;
Tejun Heob56c0d82010-06-29 10:07:09 +0200646 spin_unlock_irq(&worker->lock);
647
648 if (work) {
649 __set_current_state(TASK_RUNNING);
650 work->func(work);
Tejun Heob56c0d82010-06-29 10:07:09 +0200651 } else if (!freezing(current))
652 schedule();
653
654 try_to_freeze();
655 goto repeat;
656}
657EXPORT_SYMBOL_GPL(kthread_worker_fn);
658
Petr Mladekfbae2d42016-10-11 13:55:30 -0700659static struct kthread_worker *
Petr Mladekdbf52682016-10-11 13:55:50 -0700660__kthread_create_worker(int cpu, unsigned int flags,
661 const char namefmt[], va_list args)
Petr Mladekfbae2d42016-10-11 13:55:30 -0700662{
663 struct kthread_worker *worker;
664 struct task_struct *task;
665
666 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
667 if (!worker)
668 return ERR_PTR(-ENOMEM);
669
670 kthread_init_worker(worker);
671
672 if (cpu >= 0) {
673 char name[TASK_COMM_LEN];
674
675 /*
676 * kthread_create_worker_on_cpu() allows to pass a generic
677 * namefmt in compare with kthread_create_on_cpu. We need
678 * to format it here.
679 */
680 vsnprintf(name, sizeof(name), namefmt, args);
681 task = kthread_create_on_cpu(kthread_worker_fn, worker,
682 cpu, name);
683 } else {
684 task = __kthread_create_on_node(kthread_worker_fn, worker,
685 -1, namefmt, args);
686 }
687
688 if (IS_ERR(task))
689 goto fail_task;
690
Petr Mladekdbf52682016-10-11 13:55:50 -0700691 worker->flags = flags;
Petr Mladekfbae2d42016-10-11 13:55:30 -0700692 worker->task = task;
693 wake_up_process(task);
694 return worker;
695
696fail_task:
697 kfree(worker);
698 return ERR_CAST(task);
699}
700
701/**
702 * kthread_create_worker - create a kthread worker
Petr Mladekdbf52682016-10-11 13:55:50 -0700703 * @flags: flags modifying the default behavior of the worker
Petr Mladekfbae2d42016-10-11 13:55:30 -0700704 * @namefmt: printf-style name for the kthread worker (task).
705 *
706 * Returns a pointer to the allocated worker on success, ERR_PTR(-ENOMEM)
707 * when the needed structures could not get allocated, and ERR_PTR(-EINTR)
708 * when the worker was SIGKILLed.
709 */
710struct kthread_worker *
Petr Mladekdbf52682016-10-11 13:55:50 -0700711kthread_create_worker(unsigned int flags, const char namefmt[], ...)
Petr Mladekfbae2d42016-10-11 13:55:30 -0700712{
713 struct kthread_worker *worker;
714 va_list args;
715
716 va_start(args, namefmt);
Petr Mladekdbf52682016-10-11 13:55:50 -0700717 worker = __kthread_create_worker(-1, flags, namefmt, args);
Petr Mladekfbae2d42016-10-11 13:55:30 -0700718 va_end(args);
719
720 return worker;
721}
722EXPORT_SYMBOL(kthread_create_worker);
723
724/**
725 * kthread_create_worker_on_cpu - create a kthread worker and bind it
726 * it to a given CPU and the associated NUMA node.
727 * @cpu: CPU number
Petr Mladekdbf52682016-10-11 13:55:50 -0700728 * @flags: flags modifying the default behavior of the worker
Petr Mladekfbae2d42016-10-11 13:55:30 -0700729 * @namefmt: printf-style name for the kthread worker (task).
730 *
731 * Use a valid CPU number if you want to bind the kthread worker
732 * to the given CPU and the associated NUMA node.
733 *
734 * A good practice is to add the cpu number also into the worker name.
735 * For example, use kthread_create_worker_on_cpu(cpu, "helper/%d", cpu).
736 *
737 * Returns a pointer to the allocated worker on success, ERR_PTR(-ENOMEM)
738 * when the needed structures could not get allocated, and ERR_PTR(-EINTR)
739 * when the worker was SIGKILLed.
740 */
741struct kthread_worker *
Petr Mladekdbf52682016-10-11 13:55:50 -0700742kthread_create_worker_on_cpu(int cpu, unsigned int flags,
743 const char namefmt[], ...)
Petr Mladekfbae2d42016-10-11 13:55:30 -0700744{
745 struct kthread_worker *worker;
746 va_list args;
747
748 va_start(args, namefmt);
Petr Mladekdbf52682016-10-11 13:55:50 -0700749 worker = __kthread_create_worker(cpu, flags, namefmt, args);
Petr Mladekfbae2d42016-10-11 13:55:30 -0700750 va_end(args);
751
752 return worker;
753}
754EXPORT_SYMBOL(kthread_create_worker_on_cpu);
755
Petr Mladek37be45d2016-10-11 13:55:43 -0700756/*
757 * Returns true when the work could not be queued at the moment.
758 * It happens when it is already pending in a worker list
759 * or when it is being cancelled.
760 */
761static inline bool queuing_blocked(struct kthread_worker *worker,
762 struct kthread_work *work)
763{
764 lockdep_assert_held(&worker->lock);
765
766 return !list_empty(&work->node) || work->canceling;
767}
768
Petr Mladek8197b3d42016-10-11 13:55:36 -0700769static void kthread_insert_work_sanity_check(struct kthread_worker *worker,
770 struct kthread_work *work)
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700771{
772 lockdep_assert_held(&worker->lock);
Petr Mladek8197b3d42016-10-11 13:55:36 -0700773 WARN_ON_ONCE(!list_empty(&work->node));
774 /* Do not use a work with >1 worker, see kthread_queue_work() */
775 WARN_ON_ONCE(work->worker && work->worker != worker);
776}
777
778/* insert @work before @pos in @worker */
779static void kthread_insert_work(struct kthread_worker *worker,
780 struct kthread_work *work,
781 struct list_head *pos)
782{
783 kthread_insert_work_sanity_check(worker, work);
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700784
785 list_add_tail(&work->node, pos);
Tejun Heo46f3d972012-07-19 13:52:53 -0700786 work->worker = worker;
Lai Jiangshaned1403e2014-07-26 12:03:59 +0800787 if (!worker->current_work && likely(worker->task))
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700788 wake_up_process(worker->task);
789}
790
Tejun Heob56c0d82010-06-29 10:07:09 +0200791/**
Petr Mladek39891442016-10-11 13:55:20 -0700792 * kthread_queue_work - queue a kthread_work
Tejun Heob56c0d82010-06-29 10:07:09 +0200793 * @worker: target kthread_worker
794 * @work: kthread_work to queue
795 *
796 * Queue @work to work processor @task for async execution. @task
797 * must have been created with kthread_worker_create(). Returns %true
798 * if @work was successfully queued, %false if it was already pending.
Petr Mladek8197b3d42016-10-11 13:55:36 -0700799 *
800 * Reinitialize the work if it needs to be used by another worker.
801 * For example, when the worker was stopped and started again.
Tejun Heob56c0d82010-06-29 10:07:09 +0200802 */
Petr Mladek39891442016-10-11 13:55:20 -0700803bool kthread_queue_work(struct kthread_worker *worker,
Tejun Heob56c0d82010-06-29 10:07:09 +0200804 struct kthread_work *work)
805{
806 bool ret = false;
807 unsigned long flags;
808
809 spin_lock_irqsave(&worker->lock, flags);
Petr Mladek37be45d2016-10-11 13:55:43 -0700810 if (!queuing_blocked(worker, work)) {
Petr Mladek39891442016-10-11 13:55:20 -0700811 kthread_insert_work(worker, work, &worker->work_list);
Tejun Heob56c0d82010-06-29 10:07:09 +0200812 ret = true;
813 }
814 spin_unlock_irqrestore(&worker->lock, flags);
815 return ret;
816}
Petr Mladek39891442016-10-11 13:55:20 -0700817EXPORT_SYMBOL_GPL(kthread_queue_work);
Tejun Heob56c0d82010-06-29 10:07:09 +0200818
Petr Mladek22597dc2016-10-11 13:55:40 -0700819/**
820 * kthread_delayed_work_timer_fn - callback that queues the associated kthread
821 * delayed work when the timer expires.
822 * @__data: pointer to the data associated with the timer
823 *
824 * The format of the function is defined by struct timer_list.
825 * It should have been called from irqsafe timer with irq already off.
826 */
827void kthread_delayed_work_timer_fn(unsigned long __data)
828{
829 struct kthread_delayed_work *dwork =
830 (struct kthread_delayed_work *)__data;
831 struct kthread_work *work = &dwork->work;
832 struct kthread_worker *worker = work->worker;
833
834 /*
835 * This might happen when a pending work is reinitialized.
836 * It means that it is used a wrong way.
837 */
838 if (WARN_ON_ONCE(!worker))
839 return;
840
841 spin_lock(&worker->lock);
842 /* Work must not be used with >1 worker, see kthread_queue_work(). */
843 WARN_ON_ONCE(work->worker != worker);
844
845 /* Move the work from worker->delayed_work_list. */
846 WARN_ON_ONCE(list_empty(&work->node));
847 list_del_init(&work->node);
848 kthread_insert_work(worker, work, &worker->work_list);
849
850 spin_unlock(&worker->lock);
851}
852EXPORT_SYMBOL(kthread_delayed_work_timer_fn);
853
854void __kthread_queue_delayed_work(struct kthread_worker *worker,
855 struct kthread_delayed_work *dwork,
856 unsigned long delay)
857{
858 struct timer_list *timer = &dwork->timer;
859 struct kthread_work *work = &dwork->work;
860
861 WARN_ON_ONCE(timer->function != kthread_delayed_work_timer_fn ||
862 timer->data != (unsigned long)dwork);
863
864 /*
865 * If @delay is 0, queue @dwork->work immediately. This is for
866 * both optimization and correctness. The earliest @timer can
867 * expire is on the closest next tick and delayed_work users depend
868 * on that there's no such delay when @delay is 0.
869 */
870 if (!delay) {
871 kthread_insert_work(worker, work, &worker->work_list);
872 return;
873 }
874
875 /* Be paranoid and try to detect possible races already now. */
876 kthread_insert_work_sanity_check(worker, work);
877
878 list_add(&work->node, &worker->delayed_work_list);
879 work->worker = worker;
880 timer_stats_timer_set_start_info(&dwork->timer);
881 timer->expires = jiffies + delay;
882 add_timer(timer);
883}
884
885/**
886 * kthread_queue_delayed_work - queue the associated kthread work
887 * after a delay.
888 * @worker: target kthread_worker
889 * @dwork: kthread_delayed_work to queue
890 * @delay: number of jiffies to wait before queuing
891 *
892 * If the work has not been pending it starts a timer that will queue
893 * the work after the given @delay. If @delay is zero, it queues the
894 * work immediately.
895 *
896 * Return: %false if the @work has already been pending. It means that
897 * either the timer was running or the work was queued. It returns %true
898 * otherwise.
899 */
900bool kthread_queue_delayed_work(struct kthread_worker *worker,
901 struct kthread_delayed_work *dwork,
902 unsigned long delay)
903{
904 struct kthread_work *work = &dwork->work;
905 unsigned long flags;
906 bool ret = false;
907
908 spin_lock_irqsave(&worker->lock, flags);
909
Petr Mladek37be45d2016-10-11 13:55:43 -0700910 if (!queuing_blocked(worker, work)) {
Petr Mladek22597dc2016-10-11 13:55:40 -0700911 __kthread_queue_delayed_work(worker, dwork, delay);
912 ret = true;
913 }
914
915 spin_unlock_irqrestore(&worker->lock, flags);
916 return ret;
917}
918EXPORT_SYMBOL_GPL(kthread_queue_delayed_work);
919
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700920struct kthread_flush_work {
921 struct kthread_work work;
922 struct completion done;
923};
924
925static void kthread_flush_work_fn(struct kthread_work *work)
926{
927 struct kthread_flush_work *fwork =
928 container_of(work, struct kthread_flush_work, work);
929 complete(&fwork->done);
930}
931
Tejun Heob56c0d82010-06-29 10:07:09 +0200932/**
Petr Mladek39891442016-10-11 13:55:20 -0700933 * kthread_flush_work - flush a kthread_work
Tejun Heob56c0d82010-06-29 10:07:09 +0200934 * @work: work to flush
935 *
936 * If @work is queued or executing, wait for it to finish execution.
937 */
Petr Mladek39891442016-10-11 13:55:20 -0700938void kthread_flush_work(struct kthread_work *work)
Tejun Heob56c0d82010-06-29 10:07:09 +0200939{
Tejun Heo46f3d972012-07-19 13:52:53 -0700940 struct kthread_flush_work fwork = {
941 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
942 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
943 };
944 struct kthread_worker *worker;
945 bool noop = false;
Tejun Heob56c0d82010-06-29 10:07:09 +0200946
Tejun Heo46f3d972012-07-19 13:52:53 -0700947 worker = work->worker;
948 if (!worker)
949 return;
Tejun Heob56c0d82010-06-29 10:07:09 +0200950
Tejun Heo46f3d972012-07-19 13:52:53 -0700951 spin_lock_irq(&worker->lock);
Petr Mladek8197b3d42016-10-11 13:55:36 -0700952 /* Work must not be used with >1 worker, see kthread_queue_work(). */
953 WARN_ON_ONCE(work->worker != worker);
Tejun Heob56c0d82010-06-29 10:07:09 +0200954
Tejun Heo46f3d972012-07-19 13:52:53 -0700955 if (!list_empty(&work->node))
Petr Mladek39891442016-10-11 13:55:20 -0700956 kthread_insert_work(worker, &fwork.work, work->node.next);
Tejun Heo46f3d972012-07-19 13:52:53 -0700957 else if (worker->current_work == work)
Petr Mladek39891442016-10-11 13:55:20 -0700958 kthread_insert_work(worker, &fwork.work,
959 worker->work_list.next);
Tejun Heo46f3d972012-07-19 13:52:53 -0700960 else
961 noop = true;
Tejun Heob56c0d82010-06-29 10:07:09 +0200962
Tejun Heo46f3d972012-07-19 13:52:53 -0700963 spin_unlock_irq(&worker->lock);
964
965 if (!noop)
966 wait_for_completion(&fwork.done);
Tejun Heob56c0d82010-06-29 10:07:09 +0200967}
Petr Mladek39891442016-10-11 13:55:20 -0700968EXPORT_SYMBOL_GPL(kthread_flush_work);
Tejun Heob56c0d82010-06-29 10:07:09 +0200969
Petr Mladek37be45d2016-10-11 13:55:43 -0700970/*
971 * This function removes the work from the worker queue. Also it makes sure
972 * that it won't get queued later via the delayed work's timer.
973 *
974 * The work might still be in use when this function finishes. See the
975 * current_work proceed by the worker.
976 *
977 * Return: %true if @work was pending and successfully canceled,
978 * %false if @work was not pending
979 */
980static bool __kthread_cancel_work(struct kthread_work *work, bool is_dwork,
981 unsigned long *flags)
982{
983 /* Try to cancel the timer if exists. */
984 if (is_dwork) {
985 struct kthread_delayed_work *dwork =
986 container_of(work, struct kthread_delayed_work, work);
987 struct kthread_worker *worker = work->worker;
988
989 /*
990 * del_timer_sync() must be called to make sure that the timer
991 * callback is not running. The lock must be temporary released
992 * to avoid a deadlock with the callback. In the meantime,
993 * any queuing is blocked by setting the canceling counter.
994 */
995 work->canceling++;
996 spin_unlock_irqrestore(&worker->lock, *flags);
997 del_timer_sync(&dwork->timer);
998 spin_lock_irqsave(&worker->lock, *flags);
999 work->canceling--;
1000 }
1001
1002 /*
1003 * Try to remove the work from a worker list. It might either
1004 * be from worker->work_list or from worker->delayed_work_list.
1005 */
1006 if (!list_empty(&work->node)) {
1007 list_del_init(&work->node);
1008 return true;
1009 }
1010
1011 return false;
1012}
1013
Petr Mladek9a6b06c2016-10-11 13:55:46 -07001014/**
1015 * kthread_mod_delayed_work - modify delay of or queue a kthread delayed work
1016 * @worker: kthread worker to use
1017 * @dwork: kthread delayed work to queue
1018 * @delay: number of jiffies to wait before queuing
1019 *
1020 * If @dwork is idle, equivalent to kthread_queue_delayed_work(). Otherwise,
1021 * modify @dwork's timer so that it expires after @delay. If @delay is zero,
1022 * @work is guaranteed to be queued immediately.
1023 *
1024 * Return: %true if @dwork was pending and its timer was modified,
1025 * %false otherwise.
1026 *
1027 * A special case is when the work is being canceled in parallel.
1028 * It might be caused either by the real kthread_cancel_delayed_work_sync()
1029 * or yet another kthread_mod_delayed_work() call. We let the other command
1030 * win and return %false here. The caller is supposed to synchronize these
1031 * operations a reasonable way.
1032 *
1033 * This function is safe to call from any context including IRQ handler.
1034 * See __kthread_cancel_work() and kthread_delayed_work_timer_fn()
1035 * for details.
1036 */
1037bool kthread_mod_delayed_work(struct kthread_worker *worker,
1038 struct kthread_delayed_work *dwork,
1039 unsigned long delay)
1040{
1041 struct kthread_work *work = &dwork->work;
1042 unsigned long flags;
1043 int ret = false;
1044
1045 spin_lock_irqsave(&worker->lock, flags);
1046
1047 /* Do not bother with canceling when never queued. */
1048 if (!work->worker)
1049 goto fast_queue;
1050
1051 /* Work must not be used with >1 worker, see kthread_queue_work() */
1052 WARN_ON_ONCE(work->worker != worker);
1053
1054 /* Do not fight with another command that is canceling this work. */
1055 if (work->canceling)
1056 goto out;
1057
1058 ret = __kthread_cancel_work(work, true, &flags);
1059fast_queue:
1060 __kthread_queue_delayed_work(worker, dwork, delay);
1061out:
1062 spin_unlock_irqrestore(&worker->lock, flags);
1063 return ret;
1064}
1065EXPORT_SYMBOL_GPL(kthread_mod_delayed_work);
1066
Petr Mladek37be45d2016-10-11 13:55:43 -07001067static bool __kthread_cancel_work_sync(struct kthread_work *work, bool is_dwork)
1068{
1069 struct kthread_worker *worker = work->worker;
1070 unsigned long flags;
1071 int ret = false;
1072
1073 if (!worker)
1074 goto out;
1075
1076 spin_lock_irqsave(&worker->lock, flags);
1077 /* Work must not be used with >1 worker, see kthread_queue_work(). */
1078 WARN_ON_ONCE(work->worker != worker);
1079
1080 ret = __kthread_cancel_work(work, is_dwork, &flags);
1081
1082 if (worker->current_work != work)
1083 goto out_fast;
1084
1085 /*
1086 * The work is in progress and we need to wait with the lock released.
1087 * In the meantime, block any queuing by setting the canceling counter.
1088 */
1089 work->canceling++;
1090 spin_unlock_irqrestore(&worker->lock, flags);
1091 kthread_flush_work(work);
1092 spin_lock_irqsave(&worker->lock, flags);
1093 work->canceling--;
1094
1095out_fast:
1096 spin_unlock_irqrestore(&worker->lock, flags);
1097out:
1098 return ret;
1099}
1100
1101/**
1102 * kthread_cancel_work_sync - cancel a kthread work and wait for it to finish
1103 * @work: the kthread work to cancel
1104 *
1105 * Cancel @work and wait for its execution to finish. This function
1106 * can be used even if the work re-queues itself. On return from this
1107 * function, @work is guaranteed to be not pending or executing on any CPU.
1108 *
1109 * kthread_cancel_work_sync(&delayed_work->work) must not be used for
1110 * delayed_work's. Use kthread_cancel_delayed_work_sync() instead.
1111 *
1112 * The caller must ensure that the worker on which @work was last
1113 * queued can't be destroyed before this function returns.
1114 *
1115 * Return: %true if @work was pending, %false otherwise.
1116 */
1117bool kthread_cancel_work_sync(struct kthread_work *work)
1118{
1119 return __kthread_cancel_work_sync(work, false);
1120}
1121EXPORT_SYMBOL_GPL(kthread_cancel_work_sync);
1122
1123/**
1124 * kthread_cancel_delayed_work_sync - cancel a kthread delayed work and
1125 * wait for it to finish.
1126 * @dwork: the kthread delayed work to cancel
1127 *
1128 * This is kthread_cancel_work_sync() for delayed works.
1129 *
1130 * Return: %true if @dwork was pending, %false otherwise.
1131 */
1132bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *dwork)
1133{
1134 return __kthread_cancel_work_sync(&dwork->work, true);
1135}
1136EXPORT_SYMBOL_GPL(kthread_cancel_delayed_work_sync);
1137
Tejun Heob56c0d82010-06-29 10:07:09 +02001138/**
Petr Mladek39891442016-10-11 13:55:20 -07001139 * kthread_flush_worker - flush all current works on a kthread_worker
Tejun Heob56c0d82010-06-29 10:07:09 +02001140 * @worker: worker to flush
1141 *
1142 * Wait until all currently executing or pending works on @worker are
1143 * finished.
1144 */
Petr Mladek39891442016-10-11 13:55:20 -07001145void kthread_flush_worker(struct kthread_worker *worker)
Tejun Heob56c0d82010-06-29 10:07:09 +02001146{
1147 struct kthread_flush_work fwork = {
1148 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
1149 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
1150 };
1151
Petr Mladek39891442016-10-11 13:55:20 -07001152 kthread_queue_work(worker, &fwork.work);
Tejun Heob56c0d82010-06-29 10:07:09 +02001153 wait_for_completion(&fwork.done);
1154}
Petr Mladek39891442016-10-11 13:55:20 -07001155EXPORT_SYMBOL_GPL(kthread_flush_worker);
Petr Mladek35033fe2016-10-11 13:55:33 -07001156
1157/**
1158 * kthread_destroy_worker - destroy a kthread worker
1159 * @worker: worker to be destroyed
1160 *
1161 * Flush and destroy @worker. The simple flush is enough because the kthread
1162 * worker API is used only in trivial scenarios. There are no multi-step state
1163 * machines needed.
1164 */
1165void kthread_destroy_worker(struct kthread_worker *worker)
1166{
1167 struct task_struct *task;
1168
1169 task = worker->task;
1170 if (WARN_ON(!task))
1171 return;
1172
1173 kthread_flush_worker(worker);
1174 kthread_stop(task);
1175 WARN_ON(!list_empty(&worker->work_list));
1176 kfree(worker);
1177}
1178EXPORT_SYMBOL(kthread_destroy_worker);