blob: ac6849ee3057ba1fa577dd40da8a2e377416c802 [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>
Tejun Heo3144d812017-03-16 16:54:24 -040021#include <linux/cgroup.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040022#include <trace/events/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Eric W. Biederman73c27992007-05-09 02:34:32 -070024static DEFINE_SPINLOCK(kthread_create_lock);
25static LIST_HEAD(kthread_create_list);
26struct task_struct *kthreadd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28struct kthread_create_info
29{
Eric W. Biederman73c27992007-05-09 02:34:32 -070030 /* Information passed to kthread() from kthreadd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 int (*threadfn)(void *data);
32 void *data;
Eric Dumazet207205a2011-03-22 16:30:44 -070033 int node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Eric W. Biederman73c27992007-05-09 02:34:32 -070035 /* Result passed back to kthread_create() from kthreadd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 struct task_struct *result;
Tetsuo Handa786235ee2013-11-12 15:06:45 -080037 struct completion *done;
David Howells65f27f32006-11-22 14:55:48 +000038
Eric W. Biederman73c27992007-05-09 02:34:32 -070039 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040};
41
Oleg Nesterov63706172009-06-17 16:27:45 -070042struct kthread {
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000043 unsigned long flags;
44 unsigned int cpu;
Tejun Heo82805ab2010-06-29 10:07:09 +020045 void *data;
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000046 struct completion parked;
Oleg Nesterov63706172009-06-17 16:27:45 -070047 struct completion exited;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000050enum KTHREAD_BITS {
51 KTHREAD_IS_PER_CPU = 0,
52 KTHREAD_SHOULD_STOP,
53 KTHREAD_SHOULD_PARK,
54 KTHREAD_IS_PARKED,
55};
56
Oleg Nesterov4ecdafc2013-04-29 15:05:01 -070057#define __to_kthread(vfork) \
58 container_of(vfork, struct kthread, exited)
59
60static inline struct kthread *to_kthread(struct task_struct *k)
61{
62 return __to_kthread(k->vfork_done);
63}
64
65static struct kthread *to_live_kthread(struct task_struct *k)
66{
67 struct completion *vfork = ACCESS_ONCE(k->vfork_done);
68 if (likely(vfork))
69 return __to_kthread(vfork);
70 return NULL;
71}
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Randy Dunlap9e37bd32006-06-25 05:49:19 -070073/**
74 * kthread_should_stop - should this kthread return now?
75 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080076 * When someone calls kthread_stop() on your kthread, it will be woken
Randy Dunlap9e37bd32006-06-25 05:49:19 -070077 * and this will return true. You should then return, and your return
78 * value will be passed through to kthread_stop().
79 */
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000080bool kthread_should_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000082 return test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84EXPORT_SYMBOL(kthread_should_stop);
85
Tejun Heo82805ab2010-06-29 10:07:09 +020086/**
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000087 * kthread_should_park - should this kthread park now?
88 *
89 * When someone calls kthread_park() on your kthread, it will be woken
90 * and this will return true. You should then do the necessary
91 * cleanup and call kthread_parkme()
92 *
93 * Similar to kthread_should_stop(), but this keeps the thread alive
94 * and in a park position. kthread_unpark() "restarts" the thread and
95 * calls the thread function again.
96 */
97bool kthread_should_park(void)
98{
99 return test_bit(KTHREAD_SHOULD_PARK, &to_kthread(current)->flags);
100}
David Kershner18896452015-08-06 15:46:45 -0700101EXPORT_SYMBOL_GPL(kthread_should_park);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000102
103/**
Tejun Heo8a32c442011-11-21 12:32:23 -0800104 * kthread_freezable_should_stop - should this freezable kthread return now?
105 * @was_frozen: optional out parameter, indicates whether %current was frozen
106 *
107 * kthread_should_stop() for freezable kthreads, which will enter
108 * refrigerator if necessary. This function is safe from kthread_stop() /
109 * freezer deadlock and freezable kthreads should use this function instead
110 * of calling try_to_freeze() directly.
111 */
112bool kthread_freezable_should_stop(bool *was_frozen)
113{
114 bool frozen = false;
115
116 might_sleep();
117
118 if (unlikely(freezing(current)))
119 frozen = __refrigerator(true);
120
121 if (was_frozen)
122 *was_frozen = frozen;
123
124 return kthread_should_stop();
125}
126EXPORT_SYMBOL_GPL(kthread_freezable_should_stop);
127
128/**
Tejun Heo82805ab2010-06-29 10:07:09 +0200129 * kthread_data - return data value specified on kthread creation
130 * @task: kthread task in question
131 *
132 * Return the data value specified when kthread @task was created.
133 * The caller is responsible for ensuring the validity of @task when
134 * calling this function.
135 */
136void *kthread_data(struct task_struct *task)
137{
138 return to_kthread(task)->data;
139}
140
Tejun Heocd42d552013-04-30 15:27:21 -0700141/**
142 * probe_kthread_data - speculative version of kthread_data()
143 * @task: possible kthread task in question
144 *
145 * @task could be a kthread task. Return the data value specified when it
146 * was created if accessible. If @task isn't a kthread task or its data is
147 * inaccessible for any reason, %NULL is returned. This function requires
148 * that @task itself is safe to dereference.
149 */
150void *probe_kthread_data(struct task_struct *task)
151{
152 struct kthread *kthread = to_kthread(task);
153 void *data = NULL;
154
155 probe_kernel_read(&data, &kthread->data, sizeof(data));
156 return data;
157}
158
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000159static void __kthread_parkme(struct kthread *self)
160{
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200161 __set_current_state(TASK_PARKED);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000162 while (test_bit(KTHREAD_SHOULD_PARK, &self->flags)) {
163 if (!test_and_set_bit(KTHREAD_IS_PARKED, &self->flags))
164 complete(&self->parked);
165 schedule();
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200166 __set_current_state(TASK_PARKED);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000167 }
168 clear_bit(KTHREAD_IS_PARKED, &self->flags);
169 __set_current_state(TASK_RUNNING);
170}
171
172void kthread_parkme(void)
173{
174 __kthread_parkme(to_kthread(current));
175}
David Kershner18896452015-08-06 15:46:45 -0700176EXPORT_SYMBOL_GPL(kthread_parkme);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static int kthread(void *_create)
179{
Eric W. Biederman73c27992007-05-09 02:34:32 -0700180 /* Copy data: it's on kthread's stack */
Oleg Nesterov63706172009-06-17 16:27:45 -0700181 struct kthread_create_info *create = _create;
182 int (*threadfn)(void *data) = create->threadfn;
183 void *data = create->data;
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800184 struct completion *done;
Oleg Nesterov63706172009-06-17 16:27:45 -0700185 struct kthread self;
186 int ret;
187
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000188 self.flags = 0;
Tejun Heo82805ab2010-06-29 10:07:09 +0200189 self.data = data;
Oleg Nesterov63706172009-06-17 16:27:45 -0700190 init_completion(&self.exited);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000191 init_completion(&self.parked);
Oleg Nesterov63706172009-06-17 16:27:45 -0700192 current->vfork_done = &self.exited;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800194 /* If user was SIGKILLed, I release the structure. */
195 done = xchg(&create->done, NULL);
196 if (!done) {
197 kfree(create);
198 do_exit(-EINTR);
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* OK, tell user we're spawned, wait for stop or wakeup */
Oleg Nesterova076e4b2007-05-23 13:57:27 -0700201 __set_current_state(TASK_UNINTERRUPTIBLE);
Vitaliy Gusev3217ab92009-04-09 09:50:35 -0600202 create->result = current;
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800203 complete(done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 schedule();
205
Oleg Nesterov63706172009-06-17 16:27:45 -0700206 ret = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000208 if (!test_bit(KTHREAD_SHOULD_STOP, &self.flags)) {
Tejun Heo3144d812017-03-16 16:54:24 -0400209 cgroup_kthread_ready();
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000210 __kthread_parkme(&self);
211 ret = threadfn(data);
212 }
Oleg Nesterov63706172009-06-17 16:27:45 -0700213 /* we can't just return, we must preserve "self" on stack */
214 do_exit(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Eric Dumazet207205a2011-03-22 16:30:44 -0700217/* called from do_fork() to get node information for about to be created task */
218int tsk_fork_get_node(struct task_struct *tsk)
219{
220#ifdef CONFIG_NUMA
221 if (tsk == kthreadd_task)
222 return tsk->pref_node_fork;
223#endif
Nishanth Aravamudan81c98862014-04-03 14:46:25 -0700224 return NUMA_NO_NODE;
Eric Dumazet207205a2011-03-22 16:30:44 -0700225}
226
Eric W. Biederman73c27992007-05-09 02:34:32 -0700227static void create_kthread(struct kthread_create_info *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 int pid;
230
Eric Dumazet207205a2011-03-22 16:30:44 -0700231#ifdef CONFIG_NUMA
232 current->pref_node_fork = create->node;
233#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* We want our own signal handler (we take no signals by default). */
235 pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
Oleg Nesterovcdd140b2009-06-17 16:27:43 -0700236 if (pid < 0) {
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800237 /* If user was SIGKILLed, I release the structure. */
238 struct completion *done = xchg(&create->done, NULL);
239
240 if (!done) {
241 kfree(create);
242 return;
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 create->result = ERR_PTR(pid);
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800245 complete(done);
Oleg Nesterovcdd140b2009-06-17 16:27:43 -0700246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700249/**
Eric Dumazet207205a2011-03-22 16:30:44 -0700250 * kthread_create_on_node - create a kthread.
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700251 * @threadfn: the function to run until signal_pending(current).
252 * @data: data ptr for @threadfn.
Andrew Mortone9f06982015-09-04 15:42:42 -0700253 * @node: task and thread structures for the thread are allocated on this node
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700254 * @namefmt: printf-style name for the thread.
255 *
256 * Description: This helper function creates and names a kernel
257 * thread. The thread will be stopped: use wake_up_process() to start
Andrew Mortone9f06982015-09-04 15:42:42 -0700258 * it. See also kthread_run(). The new thread has SCHED_NORMAL policy and
259 * is affine to all CPUs.
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700260 *
Eric Dumazet207205a2011-03-22 16:30:44 -0700261 * If thread is going to be bound on a particular cpu, give its node
Andrew Mortone9f06982015-09-04 15:42:42 -0700262 * in @node, to get NUMA affinity for kthread stack, or else give NUMA_NO_NODE.
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700263 * When woken, the thread will run @threadfn() with @data as its
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800264 * argument. @threadfn() can either call do_exit() directly if it is a
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300265 * standalone thread for which no one will call kthread_stop(), or
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700266 * return when 'kthread_should_stop()' is true (which means
267 * kthread_stop() has been called). The return value should be zero
268 * or a negative error number; it will be passed to kthread_stop().
269 *
Tetsuo Handa8fe69292014-06-04 16:05:36 -0700270 * Returns a task_struct or ERR_PTR(-ENOMEM) or ERR_PTR(-EINTR).
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700271 */
Eric Dumazet207205a2011-03-22 16:30:44 -0700272struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000273 void *data, int node,
Eric Dumazet207205a2011-03-22 16:30:44 -0700274 const char namefmt[],
275 ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800277 DECLARE_COMPLETION_ONSTACK(done);
278 struct task_struct *task;
279 struct kthread_create_info *create = kmalloc(sizeof(*create),
280 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800282 if (!create)
283 return ERR_PTR(-ENOMEM);
284 create->threadfn = threadfn;
285 create->data = data;
286 create->node = node;
287 create->done = &done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Eric W. Biederman73c27992007-05-09 02:34:32 -0700289 spin_lock(&kthread_create_lock);
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800290 list_add_tail(&create->list, &kthread_create_list);
Eric W. Biederman73c27992007-05-09 02:34:32 -0700291 spin_unlock(&kthread_create_lock);
292
Dmitry Adamushkocbd9b672008-04-29 00:59:23 -0700293 wake_up_process(kthreadd_task);
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800294 /*
295 * Wait for completion in killable state, for I might be chosen by
296 * the OOM killer while kthreadd is trying to allocate memory for
297 * new kernel thread.
298 */
299 if (unlikely(wait_for_completion_killable(&done))) {
300 /*
301 * If I was SIGKILLed before kthreadd (or new kernel thread)
302 * calls complete(), leave the cleanup of this structure to
303 * that thread.
304 */
305 if (xchg(&create->done, NULL))
Tetsuo Handa8fe69292014-06-04 16:05:36 -0700306 return ERR_PTR(-EINTR);
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800307 /*
308 * kthreadd (or new kernel thread) will call complete()
309 * shortly.
310 */
311 wait_for_completion(&done);
312 }
313 task = create->result;
314 if (!IS_ERR(task)) {
Peter Zijlstrac9b5f502011-01-07 13:41:40 +0100315 static const struct sched_param param = { .sched_priority = 0 };
Snild Dolkowf6db3502018-07-26 09:15:39 +0200316 char name[TASK_COMM_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 va_list args;
Oleg Nesterov1c993152009-04-09 09:50:36 -0600318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 va_start(args, namefmt);
Snild Dolkowf6db3502018-07-26 09:15:39 +0200320 /*
321 * task is already visible to other tasks, so updating
322 * COMM must be protected.
323 */
324 vsnprintf(name, sizeof(name), namefmt, args);
325 set_task_comm(task, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 va_end(args);
Oleg Nesterov1c993152009-04-09 09:50:36 -0600327 /*
328 * root may have changed our (kthreadd's) priority or CPU mask.
329 * The kernel thread should not inherit these properties.
330 */
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800331 sched_setscheduler_nocheck(task, SCHED_NORMAL, &param);
332 set_cpus_allowed_ptr(task, cpu_all_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800334 kfree(create);
335 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
Eric Dumazet207205a2011-03-22 16:30:44 -0700337EXPORT_SYMBOL(kthread_create_on_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Peter Zijlstra25834c72015-05-15 17:43:34 +0200339static void __kthread_bind_mask(struct task_struct *p, const struct cpumask *mask, long state)
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000340{
Peter Zijlstra25834c72015-05-15 17:43:34 +0200341 unsigned long flags;
342
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200343 if (!wait_task_inactive(p, state)) {
344 WARN_ON(1);
345 return;
346 }
Peter Zijlstra25834c72015-05-15 17:43:34 +0200347
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000348 /* It's safe because the task is inactive. */
Peter Zijlstra25834c72015-05-15 17:43:34 +0200349 raw_spin_lock_irqsave(&p->pi_lock, flags);
350 do_set_cpus_allowed(p, mask);
Tejun Heo14a40ff2013-03-19 13:45:20 -0700351 p->flags |= PF_NO_SETAFFINITY;
Peter Zijlstra25834c72015-05-15 17:43:34 +0200352 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
353}
354
355static void __kthread_bind(struct task_struct *p, unsigned int cpu, long state)
356{
357 __kthread_bind_mask(p, cpumask_of(cpu), state);
358}
359
360void kthread_bind_mask(struct task_struct *p, const struct cpumask *mask)
361{
362 __kthread_bind_mask(p, mask, TASK_UNINTERRUPTIBLE);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000363}
364
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700365/**
Peter Zijlstra881232b2009-12-16 18:04:39 +0100366 * kthread_bind - bind a just-created kthread to a cpu.
367 * @p: thread created by kthread_create().
368 * @cpu: cpu (might not be online, must be possible) for @k to run on.
369 *
370 * Description: This function is equivalent to set_cpus_allowed(),
371 * except that @cpu doesn't need to be online, and the thread must be
372 * stopped (i.e., just returned from kthread_create()).
373 */
374void kthread_bind(struct task_struct *p, unsigned int cpu)
375{
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200376 __kthread_bind(p, cpu, TASK_UNINTERRUPTIBLE);
Peter Zijlstra881232b2009-12-16 18:04:39 +0100377}
378EXPORT_SYMBOL(kthread_bind);
379
380/**
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000381 * kthread_create_on_cpu - Create a cpu bound kthread
382 * @threadfn: the function to run until signal_pending(current).
383 * @data: data ptr for @threadfn.
384 * @cpu: The cpu on which the thread should be bound,
385 * @namefmt: printf-style name for the thread. Format is restricted
386 * to "name.*%u". Code fills in cpu number.
387 *
388 * Description: This helper function creates and names a kernel thread
389 * The thread will be woken and put into park mode.
390 */
391struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
392 void *data, unsigned int cpu,
393 const char *namefmt)
394{
395 struct task_struct *p;
396
Nishanth Aravamudan10922832014-10-09 15:26:18 -0700397 p = kthread_create_on_node(threadfn, data, cpu_to_node(cpu), namefmt,
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000398 cpu);
399 if (IS_ERR(p))
400 return p;
401 set_bit(KTHREAD_IS_PER_CPU, &to_kthread(p)->flags);
402 to_kthread(p)->cpu = cpu;
403 /* Park the thread to get it out of TASK_UNINTERRUPTIBLE state */
404 kthread_park(p);
405 return p;
406}
407
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200408static void __kthread_unpark(struct task_struct *k, struct kthread *kthread)
409{
410 clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
411 /*
412 * We clear the IS_PARKED bit here as we don't wait
413 * until the task has left the park code. So if we'd
414 * park before that happens we'd see the IS_PARKED bit
415 * which might be about to be cleared.
416 */
417 if (test_and_clear_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
418 if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags))
419 __kthread_bind(k, kthread->cpu, TASK_PARKED);
420 wake_up_state(k, TASK_PARKED);
421 }
422}
423
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000424/**
425 * kthread_unpark - unpark a thread created by kthread_create().
426 * @k: thread created by kthread_create().
427 *
428 * Sets kthread_should_park() for @k to return false, wakes it, and
429 * waits for it to return. If the thread is marked percpu then its
430 * bound to the cpu again.
431 */
432void kthread_unpark(struct task_struct *k)
433{
Oleg Nesterovb5c54422013-04-29 15:05:12 -0700434 struct kthread *kthread = to_live_kthread(k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000435
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200436 if (kthread)
437 __kthread_unpark(k, kthread);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000438}
David Kershner18896452015-08-06 15:46:45 -0700439EXPORT_SYMBOL_GPL(kthread_unpark);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000440
441/**
442 * kthread_park - park a thread created by kthread_create().
443 * @k: thread created by kthread_create().
444 *
445 * Sets kthread_should_park() for @k to return true, wakes it, and
446 * waits for it to return. This can also be called after kthread_create()
447 * instead of calling wake_up_process(): the thread will park without
448 * calling threadfn().
449 *
450 * Returns 0 if the thread is parked, -ENOSYS if the thread exited.
451 * If called by the kthread itself just the park bit is set.
452 */
453int kthread_park(struct task_struct *k)
454{
Oleg Nesterovb5c54422013-04-29 15:05:12 -0700455 struct kthread *kthread = to_live_kthread(k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000456 int ret = -ENOSYS;
457
458 if (kthread) {
459 if (!test_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
460 set_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
461 if (k != current) {
462 wake_up_process(k);
463 wait_for_completion(&kthread->parked);
464 }
465 }
466 ret = 0;
467 }
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000468 return ret;
469}
David Kershner18896452015-08-06 15:46:45 -0700470EXPORT_SYMBOL_GPL(kthread_park);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000471
472/**
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700473 * kthread_stop - stop a thread created by kthread_create().
474 * @k: thread created by kthread_create().
475 *
476 * Sets kthread_should_stop() for @k to return true, wakes it, and
Oleg Nesterov9ae26022009-06-19 02:51:13 +0200477 * waits for it to exit. This can also be called after kthread_create()
478 * instead of calling wake_up_process(): the thread will exit without
479 * calling threadfn().
480 *
481 * If threadfn() may call do_exit() itself, the caller must ensure
482 * task_struct can't go away.
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700483 *
484 * Returns the result of threadfn(), or %-EINTR if wake_up_process()
485 * was never called.
486 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487int kthread_stop(struct task_struct *k)
488{
Oleg Nesterovb5c54422013-04-29 15:05:12 -0700489 struct kthread *kthread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int ret;
491
Oleg Nesterov63706172009-06-17 16:27:45 -0700492 trace_sched_kthread_stop(k);
Oleg Nesterovb5c54422013-04-29 15:05:12 -0700493
494 get_task_struct(k);
495 kthread = to_live_kthread(k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000496 if (kthread) {
497 set_bit(KTHREAD_SHOULD_STOP, &kthread->flags);
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200498 __kthread_unpark(k, kthread);
Oleg Nesterov63706172009-06-17 16:27:45 -0700499 wake_up_process(k);
500 wait_for_completion(&kthread->exited);
501 }
502 ret = k->exit_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 put_task_struct(k);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400504
Oleg Nesterovb5c54422013-04-29 15:05:12 -0700505 trace_sched_kthread_stop_ret(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return ret;
507}
Adrian Bunk52e92e52006-07-14 00:24:05 -0700508EXPORT_SYMBOL(kthread_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Satyam Sharmae804a4a2007-07-31 00:39:16 -0700510int kthreadd(void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Eric W. Biederman73c27992007-05-09 02:34:32 -0700512 struct task_struct *tsk = current;
Eric W. Biederman73c27992007-05-09 02:34:32 -0700513
Satyam Sharmae804a4a2007-07-31 00:39:16 -0700514 /* Setup a clean context for our children to inherit. */
Eric W. Biederman73c27992007-05-09 02:34:32 -0700515 set_task_comm(tsk, "kthreadd");
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700516 ignore_signals(tsk);
Rusty Russell1a2142a2009-03-30 22:05:10 -0600517 set_cpus_allowed_ptr(tsk, cpu_all_mask);
Lai Jiangshanaee4faa2012-12-12 13:51:39 -0800518 set_mems_allowed(node_states[N_MEMORY]);
Eric W. Biederman73c27992007-05-09 02:34:32 -0700519
Tejun Heo34b087e2011-11-23 09:28:17 -0800520 current->flags |= PF_NOFREEZE;
Tejun Heo3144d812017-03-16 16:54:24 -0400521 cgroup_init_kthreadd();
Eric W. Biederman73c27992007-05-09 02:34:32 -0700522
523 for (;;) {
524 set_current_state(TASK_INTERRUPTIBLE);
525 if (list_empty(&kthread_create_list))
526 schedule();
527 __set_current_state(TASK_RUNNING);
528
529 spin_lock(&kthread_create_lock);
530 while (!list_empty(&kthread_create_list)) {
531 struct kthread_create_info *create;
532
533 create = list_entry(kthread_create_list.next,
534 struct kthread_create_info, list);
535 list_del_init(&create->list);
536 spin_unlock(&kthread_create_lock);
537
538 create_kthread(create);
539
540 spin_lock(&kthread_create_lock);
541 }
542 spin_unlock(&kthread_create_lock);
543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 return 0;
546}
Tejun Heob56c0d82010-06-29 10:07:09 +0200547
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100548void __init_kthread_worker(struct kthread_worker *worker,
549 const char *name,
550 struct lock_class_key *key)
551{
552 spin_lock_init(&worker->lock);
553 lockdep_set_class_and_name(&worker->lock, key, name);
554 INIT_LIST_HEAD(&worker->work_list);
555 worker->task = NULL;
556}
557EXPORT_SYMBOL_GPL(__init_kthread_worker);
558
Tejun Heob56c0d82010-06-29 10:07:09 +0200559/**
560 * kthread_worker_fn - kthread function to process kthread_worker
561 * @worker_ptr: pointer to initialized kthread_worker
562 *
563 * This function can be used as @threadfn to kthread_create() or
564 * kthread_run() with @worker_ptr argument pointing to an initialized
565 * kthread_worker. The started kthread will process work_list until
566 * the it is stopped with kthread_stop(). A kthread can also call
567 * this function directly after extra initialization.
568 *
569 * Different kthreads can be used for the same kthread_worker as long
570 * as there's only one kthread attached to it at any given time. A
571 * kthread_worker without an attached kthread simply collects queued
572 * kthread_works.
573 */
574int kthread_worker_fn(void *worker_ptr)
575{
576 struct kthread_worker *worker = worker_ptr;
577 struct kthread_work *work;
578
579 WARN_ON(worker->task);
580 worker->task = current;
581repeat:
582 set_current_state(TASK_INTERRUPTIBLE); /* mb paired w/ kthread_stop */
583
584 if (kthread_should_stop()) {
585 __set_current_state(TASK_RUNNING);
586 spin_lock_irq(&worker->lock);
587 worker->task = NULL;
588 spin_unlock_irq(&worker->lock);
589 return 0;
590 }
591
592 work = NULL;
593 spin_lock_irq(&worker->lock);
594 if (!list_empty(&worker->work_list)) {
595 work = list_first_entry(&worker->work_list,
596 struct kthread_work, node);
597 list_del_init(&work->node);
598 }
Tejun Heo46f3d972012-07-19 13:52:53 -0700599 worker->current_work = work;
Tejun Heob56c0d82010-06-29 10:07:09 +0200600 spin_unlock_irq(&worker->lock);
601
602 if (work) {
603 __set_current_state(TASK_RUNNING);
604 work->func(work);
Tejun Heob56c0d82010-06-29 10:07:09 +0200605 } else if (!freezing(current))
606 schedule();
607
608 try_to_freeze();
609 goto repeat;
610}
611EXPORT_SYMBOL_GPL(kthread_worker_fn);
612
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700613/* insert @work before @pos in @worker */
614static void insert_kthread_work(struct kthread_worker *worker,
615 struct kthread_work *work,
616 struct list_head *pos)
617{
618 lockdep_assert_held(&worker->lock);
619
620 list_add_tail(&work->node, pos);
Tejun Heo46f3d972012-07-19 13:52:53 -0700621 work->worker = worker;
Lai Jiangshaned1403e2014-07-26 12:03:59 +0800622 if (!worker->current_work && likely(worker->task))
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700623 wake_up_process(worker->task);
624}
625
Tejun Heob56c0d82010-06-29 10:07:09 +0200626/**
627 * queue_kthread_work - queue a kthread_work
628 * @worker: target kthread_worker
629 * @work: kthread_work to queue
630 *
631 * Queue @work to work processor @task for async execution. @task
632 * must have been created with kthread_worker_create(). Returns %true
633 * if @work was successfully queued, %false if it was already pending.
634 */
635bool queue_kthread_work(struct kthread_worker *worker,
636 struct kthread_work *work)
637{
638 bool ret = false;
639 unsigned long flags;
640
641 spin_lock_irqsave(&worker->lock, flags);
642 if (list_empty(&work->node)) {
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700643 insert_kthread_work(worker, work, &worker->work_list);
Tejun Heob56c0d82010-06-29 10:07:09 +0200644 ret = true;
645 }
646 spin_unlock_irqrestore(&worker->lock, flags);
647 return ret;
648}
649EXPORT_SYMBOL_GPL(queue_kthread_work);
650
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700651struct kthread_flush_work {
652 struct kthread_work work;
653 struct completion done;
654};
655
656static void kthread_flush_work_fn(struct kthread_work *work)
657{
658 struct kthread_flush_work *fwork =
659 container_of(work, struct kthread_flush_work, work);
660 complete(&fwork->done);
661}
662
Tejun Heob56c0d82010-06-29 10:07:09 +0200663/**
664 * flush_kthread_work - flush a kthread_work
665 * @work: work to flush
666 *
667 * If @work is queued or executing, wait for it to finish execution.
668 */
669void flush_kthread_work(struct kthread_work *work)
670{
Tejun Heo46f3d972012-07-19 13:52:53 -0700671 struct kthread_flush_work fwork = {
672 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
673 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
674 };
675 struct kthread_worker *worker;
676 bool noop = false;
Tejun Heob56c0d82010-06-29 10:07:09 +0200677
Tejun Heo46f3d972012-07-19 13:52:53 -0700678retry:
679 worker = work->worker;
680 if (!worker)
681 return;
Tejun Heob56c0d82010-06-29 10:07:09 +0200682
Tejun Heo46f3d972012-07-19 13:52:53 -0700683 spin_lock_irq(&worker->lock);
684 if (work->worker != worker) {
685 spin_unlock_irq(&worker->lock);
686 goto retry;
687 }
Tejun Heob56c0d82010-06-29 10:07:09 +0200688
Tejun Heo46f3d972012-07-19 13:52:53 -0700689 if (!list_empty(&work->node))
690 insert_kthread_work(worker, &fwork.work, work->node.next);
691 else if (worker->current_work == work)
692 insert_kthread_work(worker, &fwork.work, worker->work_list.next);
693 else
694 noop = true;
Tejun Heob56c0d82010-06-29 10:07:09 +0200695
Tejun Heo46f3d972012-07-19 13:52:53 -0700696 spin_unlock_irq(&worker->lock);
697
698 if (!noop)
699 wait_for_completion(&fwork.done);
Tejun Heob56c0d82010-06-29 10:07:09 +0200700}
701EXPORT_SYMBOL_GPL(flush_kthread_work);
702
Tejun Heob56c0d82010-06-29 10:07:09 +0200703/**
704 * flush_kthread_worker - flush all current works on a kthread_worker
705 * @worker: worker to flush
706 *
707 * Wait until all currently executing or pending works on @worker are
708 * finished.
709 */
710void flush_kthread_worker(struct kthread_worker *worker)
711{
712 struct kthread_flush_work fwork = {
713 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
714 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
715 };
716
717 queue_kthread_work(worker, &fwork.work);
718 wait_for_completion(&fwork.done);
719}
720EXPORT_SYMBOL_GPL(flush_kthread_worker);