blob: ce4610316377f5c6c07280e5b49f7b85f027dee3 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* Kernel thread helper functions.
3 * Copyright (C) 2004 IBM Corporation, Rusty Russell.
Christoph Hellwig9bf5b9eb2020-06-10 18:41:59 -07004 * Copyright (C) 2009 Red Hat, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Eric W. Biederman73c27992007-05-09 02:34:32 -07006 * Creation is done via kthreadd, so that we get a clean environment
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * even if we're invoked from userspace (think modprobe, hotplug cpu,
8 * etc.).
9 */
Ingo Molnarae7e81c2017-02-01 18:07:51 +010010#include <uapi/linux/sched/types.h>
Christoph Hellwig9bf5b9eb2020-06-10 18:41:59 -070011#include <linux/mm.h>
12#include <linux/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/sched.h>
Christoph Hellwig9bf5b9eb2020-06-10 18:41:59 -070014#include <linux/sched/mm.h>
Ingo Molnar29930022017-02-08 18:51:36 +010015#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kthread.h>
17#include <linux/completion.h>
18#include <linux/err.h>
Suren Baghdasaryan8af0c182019-05-14 15:41:12 -070019#include <linux/cgroup.h>
Miao Xie58568d22009-06-16 15:31:49 -070020#include <linux/cpuset.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/unistd.h>
22#include <linux/file.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040023#include <linux/export.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080024#include <linux/mutex.h>
Tejun Heob56c0d82010-06-29 10:07:09 +020025#include <linux/slab.h>
26#include <linux/freezer.h>
Al Viroa74fb732012-10-10 21:28:25 -040027#include <linux/ptrace.h>
Tejun Heocd42d552013-04-30 15:27:21 -070028#include <linux/uaccess.h>
Anshuman Khandual98fa15f2019-03-05 15:42:58 -080029#include <linux/numa.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040030#include <trace/events/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Christoph Hellwig9bf5b9eb2020-06-10 18:41:59 -070032
Eric W. Biederman73c27992007-05-09 02:34:32 -070033static DEFINE_SPINLOCK(kthread_create_lock);
34static LIST_HEAD(kthread_create_list);
35struct task_struct *kthreadd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37struct kthread_create_info
38{
Eric W. Biederman73c27992007-05-09 02:34:32 -070039 /* Information passed to kthread() from kthreadd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 int (*threadfn)(void *data);
41 void *data;
Eric Dumazet207205a2011-03-22 16:30:44 -070042 int node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Eric W. Biederman73c27992007-05-09 02:34:32 -070044 /* Result passed back to kthread_create() from kthreadd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 struct task_struct *result;
Tetsuo Handa786235ee2013-11-12 15:06:45 -080046 struct completion *done;
David Howells65f27f32006-11-22 14:55:48 +000047
Eric W. Biederman73c27992007-05-09 02:34:32 -070048 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Oleg Nesterov63706172009-06-17 16:27:45 -070051struct kthread {
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000052 unsigned long flags;
53 unsigned int cpu;
Tejun Heo82805ab2010-06-29 10:07:09 +020054 void *data;
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000055 struct completion parked;
Oleg Nesterov63706172009-06-17 16:27:45 -070056 struct completion exited;
Shaohua Li0b508bc2017-09-26 11:02:12 -070057#ifdef CONFIG_BLK_CGROUP
Shaohua Li05e3db92017-09-14 14:02:04 -070058 struct cgroup_subsys_state *blkcg_css;
59#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000062enum KTHREAD_BITS {
63 KTHREAD_IS_PER_CPU = 0,
64 KTHREAD_SHOULD_STOP,
65 KTHREAD_SHOULD_PARK,
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000066};
67
Oleg Nesterov1da5c462016-11-29 18:50:57 +010068static inline void set_kthread_struct(void *kthread)
69{
70 /*
71 * We abuse ->set_child_tid to avoid the new member and because it
72 * can't be wrongly copied by copy_process(). We also rely on fact
73 * that the caller can't exec, so PF_KTHREAD can't be cleared.
74 */
75 current->set_child_tid = (__force void __user *)kthread;
76}
Oleg Nesterov4ecdafc2013-04-29 15:05:01 -070077
78static inline struct kthread *to_kthread(struct task_struct *k)
79{
Oleg Nesterov1da5c462016-11-29 18:50:57 +010080 WARN_ON(!(k->flags & PF_KTHREAD));
81 return (__force void *)k->set_child_tid;
Oleg Nesterov4ecdafc2013-04-29 15:05:01 -070082}
83
Oleg Nesterov1da5c462016-11-29 18:50:57 +010084void free_kthread_struct(struct task_struct *k)
85{
Shaohua Li05e3db92017-09-14 14:02:04 -070086 struct kthread *kthread;
87
Oleg Nesterov1da5c462016-11-29 18:50:57 +010088 /*
89 * Can be NULL if this kthread was created by kernel_thread()
90 * or if kmalloc() in kthread() failed.
91 */
Shaohua Li05e3db92017-09-14 14:02:04 -070092 kthread = to_kthread(k);
Shaohua Li0b508bc2017-09-26 11:02:12 -070093#ifdef CONFIG_BLK_CGROUP
Shaohua Li05e3db92017-09-14 14:02:04 -070094 WARN_ON_ONCE(kthread && kthread->blkcg_css);
95#endif
96 kfree(kthread);
Oleg Nesterov1da5c462016-11-29 18:50:57 +010097}
98
Randy Dunlap9e37bd32006-06-25 05:49:19 -070099/**
100 * kthread_should_stop - should this kthread return now?
101 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800102 * When someone calls kthread_stop() on your kthread, it will be woken
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700103 * and this will return true. You should then return, and your return
104 * value will be passed through to kthread_stop().
105 */
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000106bool kthread_should_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000108 return test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110EXPORT_SYMBOL(kthread_should_stop);
111
Matthias Kaehlcke01218052019-01-28 15:46:24 -0800112bool __kthread_should_park(struct task_struct *k)
113{
114 return test_bit(KTHREAD_SHOULD_PARK, &to_kthread(k)->flags);
115}
116EXPORT_SYMBOL_GPL(__kthread_should_park);
117
Tejun Heo82805ab2010-06-29 10:07:09 +0200118/**
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000119 * kthread_should_park - should this kthread park now?
120 *
121 * When someone calls kthread_park() on your kthread, it will be woken
122 * and this will return true. You should then do the necessary
123 * cleanup and call kthread_parkme()
124 *
125 * Similar to kthread_should_stop(), but this keeps the thread alive
126 * and in a park position. kthread_unpark() "restarts" the thread and
127 * calls the thread function again.
128 */
129bool kthread_should_park(void)
130{
Matthias Kaehlcke01218052019-01-28 15:46:24 -0800131 return __kthread_should_park(current);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000132}
David Kershner18896452015-08-06 15:46:45 -0700133EXPORT_SYMBOL_GPL(kthread_should_park);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000134
135/**
Tejun Heo8a32c442011-11-21 12:32:23 -0800136 * kthread_freezable_should_stop - should this freezable kthread return now?
137 * @was_frozen: optional out parameter, indicates whether %current was frozen
138 *
139 * kthread_should_stop() for freezable kthreads, which will enter
140 * refrigerator if necessary. This function is safe from kthread_stop() /
141 * freezer deadlock and freezable kthreads should use this function instead
142 * of calling try_to_freeze() directly.
143 */
144bool kthread_freezable_should_stop(bool *was_frozen)
145{
146 bool frozen = false;
147
148 might_sleep();
149
150 if (unlikely(freezing(current)))
151 frozen = __refrigerator(true);
152
153 if (was_frozen)
154 *was_frozen = frozen;
155
156 return kthread_should_stop();
157}
158EXPORT_SYMBOL_GPL(kthread_freezable_should_stop);
159
160/**
Tejun Heo82805ab2010-06-29 10:07:09 +0200161 * kthread_data - return data value specified on kthread creation
162 * @task: kthread task in question
163 *
164 * Return the data value specified when kthread @task was created.
165 * The caller is responsible for ensuring the validity of @task when
166 * calling this function.
167 */
168void *kthread_data(struct task_struct *task)
169{
170 return to_kthread(task)->data;
171}
172
Tejun Heocd42d552013-04-30 15:27:21 -0700173/**
Petr Mladeke7005912016-10-11 13:55:17 -0700174 * kthread_probe_data - speculative version of kthread_data()
Tejun Heocd42d552013-04-30 15:27:21 -0700175 * @task: possible kthread task in question
176 *
177 * @task could be a kthread task. Return the data value specified when it
178 * was created if accessible. If @task isn't a kthread task or its data is
179 * inaccessible for any reason, %NULL is returned. This function requires
180 * that @task itself is safe to dereference.
181 */
Petr Mladeke7005912016-10-11 13:55:17 -0700182void *kthread_probe_data(struct task_struct *task)
Tejun Heocd42d552013-04-30 15:27:21 -0700183{
184 struct kthread *kthread = to_kthread(task);
185 void *data = NULL;
186
187 probe_kernel_read(&data, &kthread->data, sizeof(data));
188 return data;
189}
190
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000191static void __kthread_parkme(struct kthread *self)
192{
Peter Zijlstra741a76b2018-04-30 14:50:22 +0200193 for (;;) {
Peter Zijlstra1cef1152018-06-07 11:45:49 +0200194 /*
195 * TASK_PARKED is a special state; we must serialize against
196 * possible pending wakeups to avoid store-store collisions on
197 * task->state.
198 *
199 * Such a collision might possibly result in the task state
200 * changin from TASK_PARKED and us failing the
201 * wait_task_inactive() in kthread_park().
202 */
203 set_special_state(TASK_PARKED);
Peter Zijlstra741a76b2018-04-30 14:50:22 +0200204 if (!test_bit(KTHREAD_SHOULD_PARK, &self->flags))
205 break;
Peter Zijlstra1cef1152018-06-07 11:45:49 +0200206
Liang Chen26c72952020-03-06 15:01:33 +0800207 /*
208 * Thread is going to call schedule(), do not preempt it,
209 * or the caller of kthread_park() may spend more time in
210 * wait_task_inactive().
211 */
212 preempt_disable();
Peter Zijlstraf83ee192018-06-07 10:55:56 +0200213 complete(&self->parked);
Liang Chen26c72952020-03-06 15:01:33 +0800214 schedule_preempt_disabled();
215 preempt_enable();
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000216 }
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000217 __set_current_state(TASK_RUNNING);
218}
219
220void kthread_parkme(void)
221{
222 __kthread_parkme(to_kthread(current));
223}
David Kershner18896452015-08-06 15:46:45 -0700224EXPORT_SYMBOL_GPL(kthread_parkme);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static int kthread(void *_create)
227{
Eric W. Biederman73c27992007-05-09 02:34:32 -0700228 /* Copy data: it's on kthread's stack */
Oleg Nesterov63706172009-06-17 16:27:45 -0700229 struct kthread_create_info *create = _create;
230 int (*threadfn)(void *data) = create->threadfn;
231 void *data = create->data;
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800232 struct completion *done;
Oleg Nesterov1da5c462016-11-29 18:50:57 +0100233 struct kthread *self;
Oleg Nesterov63706172009-06-17 16:27:45 -0700234 int ret;
235
Shaohua Lie10237c2017-11-07 11:09:50 -0800236 self = kzalloc(sizeof(*self), GFP_KERNEL);
Oleg Nesterov1da5c462016-11-29 18:50:57 +0100237 set_kthread_struct(self);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800239 /* If user was SIGKILLed, I release the structure. */
240 done = xchg(&create->done, NULL);
241 if (!done) {
242 kfree(create);
243 do_exit(-EINTR);
244 }
Oleg Nesterov1da5c462016-11-29 18:50:57 +0100245
246 if (!self) {
247 create->result = ERR_PTR(-ENOMEM);
248 complete(done);
249 do_exit(-ENOMEM);
250 }
251
Oleg Nesterov1da5c462016-11-29 18:50:57 +0100252 self->data = data;
253 init_completion(&self->exited);
254 init_completion(&self->parked);
255 current->vfork_done = &self->exited;
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /* OK, tell user we're spawned, wait for stop or wakeup */
Oleg Nesterova076e4b2007-05-23 13:57:27 -0700258 __set_current_state(TASK_UNINTERRUPTIBLE);
Vitaliy Gusev3217ab92009-04-09 09:50:35 -0600259 create->result = current;
Liang Chen26c72952020-03-06 15:01:33 +0800260 /*
261 * Thread is going to call schedule(), do not preempt it,
262 * or the creator may spend more time in wait_task_inactive().
263 */
264 preempt_disable();
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800265 complete(done);
Liang Chen26c72952020-03-06 15:01:33 +0800266 schedule_preempt_disabled();
267 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Oleg Nesterov63706172009-06-17 16:27:45 -0700269 ret = -EINTR;
Oleg Nesterov1da5c462016-11-29 18:50:57 +0100270 if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) {
Tejun Heo77f88792017-03-16 16:54:24 -0400271 cgroup_kthread_ready();
Oleg Nesterov1da5c462016-11-29 18:50:57 +0100272 __kthread_parkme(self);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000273 ret = threadfn(data);
274 }
Oleg Nesterov63706172009-06-17 16:27:45 -0700275 do_exit(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Eric Dumazet207205a2011-03-22 16:30:44 -0700278/* called from do_fork() to get node information for about to be created task */
279int tsk_fork_get_node(struct task_struct *tsk)
280{
281#ifdef CONFIG_NUMA
282 if (tsk == kthreadd_task)
283 return tsk->pref_node_fork;
284#endif
Nishanth Aravamudan81c98862014-04-03 14:46:25 -0700285 return NUMA_NO_NODE;
Eric Dumazet207205a2011-03-22 16:30:44 -0700286}
287
Eric W. Biederman73c27992007-05-09 02:34:32 -0700288static void create_kthread(struct kthread_create_info *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 int pid;
291
Eric Dumazet207205a2011-03-22 16:30:44 -0700292#ifdef CONFIG_NUMA
293 current->pref_node_fork = create->node;
294#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* We want our own signal handler (we take no signals by default). */
296 pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
Oleg Nesterovcdd140b2009-06-17 16:27:43 -0700297 if (pid < 0) {
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800298 /* If user was SIGKILLed, I release the structure. */
299 struct completion *done = xchg(&create->done, NULL);
300
301 if (!done) {
302 kfree(create);
303 return;
304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 create->result = ERR_PTR(pid);
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800306 complete(done);
Oleg Nesterovcdd140b2009-06-17 16:27:43 -0700307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Nicolas Ioossc0b942a2016-12-12 16:40:39 -0800310static __printf(4, 0)
311struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data),
Petr Mladek255451e2016-10-11 13:55:27 -0700312 void *data, int node,
313 const char namefmt[],
314 va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800316 DECLARE_COMPLETION_ONSTACK(done);
317 struct task_struct *task;
318 struct kthread_create_info *create = kmalloc(sizeof(*create),
319 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800321 if (!create)
322 return ERR_PTR(-ENOMEM);
323 create->threadfn = threadfn;
324 create->data = data;
325 create->node = node;
326 create->done = &done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Eric W. Biederman73c27992007-05-09 02:34:32 -0700328 spin_lock(&kthread_create_lock);
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800329 list_add_tail(&create->list, &kthread_create_list);
Eric W. Biederman73c27992007-05-09 02:34:32 -0700330 spin_unlock(&kthread_create_lock);
331
Dmitry Adamushkocbd9b672008-04-29 00:59:23 -0700332 wake_up_process(kthreadd_task);
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800333 /*
334 * Wait for completion in killable state, for I might be chosen by
335 * the OOM killer while kthreadd is trying to allocate memory for
336 * new kernel thread.
337 */
338 if (unlikely(wait_for_completion_killable(&done))) {
339 /*
340 * If I was SIGKILLed before kthreadd (or new kernel thread)
341 * calls complete(), leave the cleanup of this structure to
342 * that thread.
343 */
344 if (xchg(&create->done, NULL))
Tetsuo Handa8fe69292014-06-04 16:05:36 -0700345 return ERR_PTR(-EINTR);
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800346 /*
347 * kthreadd (or new kernel thread) will call complete()
348 * shortly.
349 */
350 wait_for_completion(&done);
351 }
352 task = create->result;
353 if (!IS_ERR(task)) {
Peter Zijlstrac9b5f502011-01-07 13:41:40 +0100354 static const struct sched_param param = { .sched_priority = 0 };
Snild Dolkow3e536e22018-07-26 09:15:39 +0200355 char name[TASK_COMM_LEN];
Oleg Nesterov1c993152009-04-09 09:50:36 -0600356
Snild Dolkow3e536e22018-07-26 09:15:39 +0200357 /*
358 * task is already visible to other tasks, so updating
359 * COMM must be protected.
360 */
361 vsnprintf(name, sizeof(name), namefmt, args);
362 set_task_comm(task, name);
Oleg Nesterov1c993152009-04-09 09:50:36 -0600363 /*
364 * root may have changed our (kthreadd's) priority or CPU mask.
365 * The kernel thread should not inherit these properties.
366 */
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800367 sched_setscheduler_nocheck(task, SCHED_NORMAL, &param);
368 set_cpus_allowed_ptr(task, cpu_all_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Tetsuo Handa786235ee2013-11-12 15:06:45 -0800370 kfree(create);
371 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
Petr Mladek255451e2016-10-11 13:55:27 -0700373
374/**
375 * kthread_create_on_node - create a kthread.
376 * @threadfn: the function to run until signal_pending(current).
377 * @data: data ptr for @threadfn.
378 * @node: task and thread structures for the thread are allocated on this node
379 * @namefmt: printf-style name for the thread.
380 *
381 * Description: This helper function creates and names a kernel
382 * thread. The thread will be stopped: use wake_up_process() to start
383 * it. See also kthread_run(). The new thread has SCHED_NORMAL policy and
384 * is affine to all CPUs.
385 *
386 * If thread is going to be bound on a particular cpu, give its node
387 * in @node, to get NUMA affinity for kthread stack, or else give NUMA_NO_NODE.
388 * When woken, the thread will run @threadfn() with @data as its
389 * argument. @threadfn() can either call do_exit() directly if it is a
390 * standalone thread for which no one will call kthread_stop(), or
391 * return when 'kthread_should_stop()' is true (which means
392 * kthread_stop() has been called). The return value should be zero
393 * or a negative error number; it will be passed to kthread_stop().
394 *
395 * Returns a task_struct or ERR_PTR(-ENOMEM) or ERR_PTR(-EINTR).
396 */
397struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
398 void *data, int node,
399 const char namefmt[],
400 ...)
401{
402 struct task_struct *task;
403 va_list args;
404
405 va_start(args, namefmt);
406 task = __kthread_create_on_node(threadfn, data, node, namefmt, args);
407 va_end(args);
408
409 return task;
410}
Eric Dumazet207205a2011-03-22 16:30:44 -0700411EXPORT_SYMBOL(kthread_create_on_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Peter Zijlstra25834c72015-05-15 17:43:34 +0200413static void __kthread_bind_mask(struct task_struct *p, const struct cpumask *mask, long state)
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000414{
Peter Zijlstra25834c72015-05-15 17:43:34 +0200415 unsigned long flags;
416
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200417 if (!wait_task_inactive(p, state)) {
418 WARN_ON(1);
419 return;
420 }
Peter Zijlstra25834c72015-05-15 17:43:34 +0200421
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000422 /* It's safe because the task is inactive. */
Peter Zijlstra25834c72015-05-15 17:43:34 +0200423 raw_spin_lock_irqsave(&p->pi_lock, flags);
424 do_set_cpus_allowed(p, mask);
Tejun Heo14a40ff2013-03-19 13:45:20 -0700425 p->flags |= PF_NO_SETAFFINITY;
Peter Zijlstra25834c72015-05-15 17:43:34 +0200426 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
427}
428
429static void __kthread_bind(struct task_struct *p, unsigned int cpu, long state)
430{
431 __kthread_bind_mask(p, cpumask_of(cpu), state);
432}
433
434void kthread_bind_mask(struct task_struct *p, const struct cpumask *mask)
435{
436 __kthread_bind_mask(p, mask, TASK_UNINTERRUPTIBLE);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000437}
438
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700439/**
Peter Zijlstra881232b2009-12-16 18:04:39 +0100440 * kthread_bind - bind a just-created kthread to a cpu.
441 * @p: thread created by kthread_create().
442 * @cpu: cpu (might not be online, must be possible) for @k to run on.
443 *
444 * Description: This function is equivalent to set_cpus_allowed(),
445 * except that @cpu doesn't need to be online, and the thread must be
446 * stopped (i.e., just returned from kthread_create()).
447 */
448void kthread_bind(struct task_struct *p, unsigned int cpu)
449{
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200450 __kthread_bind(p, cpu, TASK_UNINTERRUPTIBLE);
Peter Zijlstra881232b2009-12-16 18:04:39 +0100451}
452EXPORT_SYMBOL(kthread_bind);
453
454/**
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000455 * kthread_create_on_cpu - Create a cpu bound kthread
456 * @threadfn: the function to run until signal_pending(current).
457 * @data: data ptr for @threadfn.
458 * @cpu: The cpu on which the thread should be bound,
459 * @namefmt: printf-style name for the thread. Format is restricted
460 * to "name.*%u". Code fills in cpu number.
461 *
462 * Description: This helper function creates and names a kernel thread
463 * The thread will be woken and put into park mode.
464 */
465struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
466 void *data, unsigned int cpu,
467 const char *namefmt)
468{
469 struct task_struct *p;
470
Nishanth Aravamudan10922832014-10-09 15:26:18 -0700471 p = kthread_create_on_node(threadfn, data, cpu_to_node(cpu), namefmt,
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000472 cpu);
473 if (IS_ERR(p))
474 return p;
Petr Mladeka65d4092016-10-11 13:55:23 -0700475 kthread_bind(p, cpu);
476 /* CPU hotplug need to bind once again when unparking the thread. */
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000477 set_bit(KTHREAD_IS_PER_CPU, &to_kthread(p)->flags);
478 to_kthread(p)->cpu = cpu;
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000479 return p;
480}
481
Oleg Nesterovcf380a42016-11-29 18:51:07 +0100482/**
483 * kthread_unpark - unpark a thread created by kthread_create().
484 * @k: thread created by kthread_create().
485 *
486 * Sets kthread_should_park() for @k to return false, wakes it, and
487 * waits for it to return. If the thread is marked percpu then its
488 * bound to the cpu again.
489 */
490void kthread_unpark(struct task_struct *k)
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200491{
Oleg Nesterovcf380a42016-11-29 18:51:07 +0100492 struct kthread *kthread = to_kthread(k);
493
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200494 /*
Peter Zijlstra85f1abe2018-05-01 18:14:45 +0200495 * Newly created kthread was parked when the CPU was offline.
496 * The binding was lost and we need to set it again.
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200497 */
Peter Zijlstra85f1abe2018-05-01 18:14:45 +0200498 if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags))
499 __kthread_bind(k, kthread->cpu, TASK_PARKED);
500
501 clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
Peter Zijlstra1cef1152018-06-07 11:45:49 +0200502 /*
503 * __kthread_parkme() will either see !SHOULD_PARK or get the wakeup.
504 */
Peter Zijlstra85f1abe2018-05-01 18:14:45 +0200505 wake_up_state(k, TASK_PARKED);
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200506}
David Kershner18896452015-08-06 15:46:45 -0700507EXPORT_SYMBOL_GPL(kthread_unpark);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000508
509/**
510 * kthread_park - park a thread created by kthread_create().
511 * @k: thread created by kthread_create().
512 *
513 * Sets kthread_should_park() for @k to return true, wakes it, and
514 * waits for it to return. This can also be called after kthread_create()
515 * instead of calling wake_up_process(): the thread will park without
516 * calling threadfn().
517 *
518 * Returns 0 if the thread is parked, -ENOSYS if the thread exited.
519 * If called by the kthread itself just the park bit is set.
520 */
521int kthread_park(struct task_struct *k)
522{
Oleg Nesterovcf380a42016-11-29 18:51:07 +0100523 struct kthread *kthread = to_kthread(k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000524
Oleg Nesterovcf380a42016-11-29 18:51:07 +0100525 if (WARN_ON(k->flags & PF_EXITING))
526 return -ENOSYS;
527
Peter Zijlstraf83ee192018-06-07 10:55:56 +0200528 if (WARN_ON_ONCE(test_bit(KTHREAD_SHOULD_PARK, &kthread->flags)))
529 return -EBUSY;
530
Peter Zijlstra85f1abe2018-05-01 18:14:45 +0200531 set_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
532 if (k != current) {
533 wake_up_process(k);
Peter Zijlstra1cef1152018-06-07 11:45:49 +0200534 /*
535 * Wait for __kthread_parkme() to complete(), this means we
536 * _will_ have TASK_PARKED and are about to call schedule().
537 */
Peter Zijlstra85f1abe2018-05-01 18:14:45 +0200538 wait_for_completion(&kthread->parked);
Peter Zijlstra1cef1152018-06-07 11:45:49 +0200539 /*
540 * Now wait for that schedule() to complete and the task to
541 * get scheduled out.
542 */
543 WARN_ON_ONCE(!wait_task_inactive(k, TASK_PARKED));
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000544 }
Oleg Nesterovcf380a42016-11-29 18:51:07 +0100545
546 return 0;
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000547}
David Kershner18896452015-08-06 15:46:45 -0700548EXPORT_SYMBOL_GPL(kthread_park);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000549
550/**
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700551 * kthread_stop - stop a thread created by kthread_create().
552 * @k: thread created by kthread_create().
553 *
554 * Sets kthread_should_stop() for @k to return true, wakes it, and
Oleg Nesterov9ae26022009-06-19 02:51:13 +0200555 * waits for it to exit. This can also be called after kthread_create()
556 * instead of calling wake_up_process(): the thread will exit without
557 * calling threadfn().
558 *
559 * If threadfn() may call do_exit() itself, the caller must ensure
560 * task_struct can't go away.
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700561 *
562 * Returns the result of threadfn(), or %-EINTR if wake_up_process()
563 * was never called.
564 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565int kthread_stop(struct task_struct *k)
566{
Oleg Nesterovb5c54422013-04-29 15:05:12 -0700567 struct kthread *kthread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 int ret;
569
Oleg Nesterov63706172009-06-17 16:27:45 -0700570 trace_sched_kthread_stop(k);
Oleg Nesterovb5c54422013-04-29 15:05:12 -0700571
572 get_task_struct(k);
Oleg Nesterovefb29fb2016-11-29 18:51:03 +0100573 kthread = to_kthread(k);
574 set_bit(KTHREAD_SHOULD_STOP, &kthread->flags);
Oleg Nesterovcf380a42016-11-29 18:51:07 +0100575 kthread_unpark(k);
Oleg Nesterovefb29fb2016-11-29 18:51:03 +0100576 wake_up_process(k);
577 wait_for_completion(&kthread->exited);
Oleg Nesterov63706172009-06-17 16:27:45 -0700578 ret = k->exit_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 put_task_struct(k);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400580
Oleg Nesterovb5c54422013-04-29 15:05:12 -0700581 trace_sched_kthread_stop_ret(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return ret;
583}
Adrian Bunk52e92e52006-07-14 00:24:05 -0700584EXPORT_SYMBOL(kthread_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Satyam Sharmae804a4a2007-07-31 00:39:16 -0700586int kthreadd(void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Eric W. Biederman73c27992007-05-09 02:34:32 -0700588 struct task_struct *tsk = current;
Eric W. Biederman73c27992007-05-09 02:34:32 -0700589
Satyam Sharmae804a4a2007-07-31 00:39:16 -0700590 /* Setup a clean context for our children to inherit. */
Eric W. Biederman73c27992007-05-09 02:34:32 -0700591 set_task_comm(tsk, "kthreadd");
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700592 ignore_signals(tsk);
Rusty Russell1a2142a2009-03-30 22:05:10 -0600593 set_cpus_allowed_ptr(tsk, cpu_all_mask);
Lai Jiangshanaee4faa2012-12-12 13:51:39 -0800594 set_mems_allowed(node_states[N_MEMORY]);
Eric W. Biederman73c27992007-05-09 02:34:32 -0700595
Tejun Heo34b087e2011-11-23 09:28:17 -0800596 current->flags |= PF_NOFREEZE;
Tejun Heo77f88792017-03-16 16:54:24 -0400597 cgroup_init_kthreadd();
Eric W. Biederman73c27992007-05-09 02:34:32 -0700598
599 for (;;) {
600 set_current_state(TASK_INTERRUPTIBLE);
601 if (list_empty(&kthread_create_list))
602 schedule();
603 __set_current_state(TASK_RUNNING);
604
605 spin_lock(&kthread_create_lock);
606 while (!list_empty(&kthread_create_list)) {
607 struct kthread_create_info *create;
608
609 create = list_entry(kthread_create_list.next,
610 struct kthread_create_info, list);
611 list_del_init(&create->list);
612 spin_unlock(&kthread_create_lock);
613
614 create_kthread(create);
615
616 spin_lock(&kthread_create_lock);
617 }
618 spin_unlock(&kthread_create_lock);
619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 return 0;
622}
Tejun Heob56c0d82010-06-29 10:07:09 +0200623
Petr Mladek39891442016-10-11 13:55:20 -0700624void __kthread_init_worker(struct kthread_worker *worker,
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100625 const char *name,
626 struct lock_class_key *key)
627{
Petr Mladekdbf52682016-10-11 13:55:50 -0700628 memset(worker, 0, sizeof(struct kthread_worker));
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +0100629 raw_spin_lock_init(&worker->lock);
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100630 lockdep_set_class_and_name(&worker->lock, key, name);
631 INIT_LIST_HEAD(&worker->work_list);
Petr Mladek22597dc2016-10-11 13:55:40 -0700632 INIT_LIST_HEAD(&worker->delayed_work_list);
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100633}
Petr Mladek39891442016-10-11 13:55:20 -0700634EXPORT_SYMBOL_GPL(__kthread_init_worker);
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100635
Tejun Heob56c0d82010-06-29 10:07:09 +0200636/**
637 * kthread_worker_fn - kthread function to process kthread_worker
638 * @worker_ptr: pointer to initialized kthread_worker
639 *
Petr Mladekfbae2d42016-10-11 13:55:30 -0700640 * This function implements the main cycle of kthread worker. It processes
641 * work_list until it is stopped with kthread_stop(). It sleeps when the queue
642 * is empty.
Tejun Heob56c0d82010-06-29 10:07:09 +0200643 *
Petr Mladekfbae2d42016-10-11 13:55:30 -0700644 * The works are not allowed to keep any locks, disable preemption or interrupts
645 * when they finish. There is defined a safe point for freezing when one work
646 * finishes and before a new one is started.
Petr Mladek8197b3d42016-10-11 13:55:36 -0700647 *
648 * Also the works must not be handled by more than one worker at the same time,
649 * see also kthread_queue_work().
Tejun Heob56c0d82010-06-29 10:07:09 +0200650 */
651int kthread_worker_fn(void *worker_ptr)
652{
653 struct kthread_worker *worker = worker_ptr;
654 struct kthread_work *work;
655
Petr Mladekfbae2d42016-10-11 13:55:30 -0700656 /*
657 * FIXME: Update the check and remove the assignment when all kthread
658 * worker users are created using kthread_create_worker*() functions.
659 */
660 WARN_ON(worker->task && worker->task != current);
Tejun Heob56c0d82010-06-29 10:07:09 +0200661 worker->task = current;
Petr Mladekdbf52682016-10-11 13:55:50 -0700662
663 if (worker->flags & KTW_FREEZABLE)
664 set_freezable();
665
Tejun Heob56c0d82010-06-29 10:07:09 +0200666repeat:
667 set_current_state(TASK_INTERRUPTIBLE); /* mb paired w/ kthread_stop */
668
669 if (kthread_should_stop()) {
670 __set_current_state(TASK_RUNNING);
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +0100671 raw_spin_lock_irq(&worker->lock);
Tejun Heob56c0d82010-06-29 10:07:09 +0200672 worker->task = NULL;
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +0100673 raw_spin_unlock_irq(&worker->lock);
Tejun Heob56c0d82010-06-29 10:07:09 +0200674 return 0;
675 }
676
677 work = NULL;
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +0100678 raw_spin_lock_irq(&worker->lock);
Tejun Heob56c0d82010-06-29 10:07:09 +0200679 if (!list_empty(&worker->work_list)) {
680 work = list_first_entry(&worker->work_list,
681 struct kthread_work, node);
682 list_del_init(&work->node);
683 }
Tejun Heo46f3d972012-07-19 13:52:53 -0700684 worker->current_work = work;
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +0100685 raw_spin_unlock_irq(&worker->lock);
Tejun Heob56c0d82010-06-29 10:07:09 +0200686
687 if (work) {
688 __set_current_state(TASK_RUNNING);
689 work->func(work);
Tejun Heob56c0d82010-06-29 10:07:09 +0200690 } else if (!freezing(current))
691 schedule();
692
693 try_to_freeze();
Shaohua Li22cf8bc2017-08-31 16:15:23 -0700694 cond_resched();
Tejun Heob56c0d82010-06-29 10:07:09 +0200695 goto repeat;
696}
697EXPORT_SYMBOL_GPL(kthread_worker_fn);
698
Nicolas Ioossc0b942a2016-12-12 16:40:39 -0800699static __printf(3, 0) struct kthread_worker *
Petr Mladekdbf52682016-10-11 13:55:50 -0700700__kthread_create_worker(int cpu, unsigned int flags,
701 const char namefmt[], va_list args)
Petr Mladekfbae2d42016-10-11 13:55:30 -0700702{
703 struct kthread_worker *worker;
704 struct task_struct *task;
Anshuman Khandual98fa15f2019-03-05 15:42:58 -0800705 int node = NUMA_NO_NODE;
Petr Mladekfbae2d42016-10-11 13:55:30 -0700706
707 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
708 if (!worker)
709 return ERR_PTR(-ENOMEM);
710
711 kthread_init_worker(worker);
712
Oleg Nesterov8fb9dcb2016-11-29 18:51:10 +0100713 if (cpu >= 0)
714 node = cpu_to_node(cpu);
Petr Mladekfbae2d42016-10-11 13:55:30 -0700715
Oleg Nesterov8fb9dcb2016-11-29 18:51:10 +0100716 task = __kthread_create_on_node(kthread_worker_fn, worker,
717 node, namefmt, args);
Petr Mladekfbae2d42016-10-11 13:55:30 -0700718 if (IS_ERR(task))
719 goto fail_task;
720
Oleg Nesterov8fb9dcb2016-11-29 18:51:10 +0100721 if (cpu >= 0)
722 kthread_bind(task, cpu);
723
Petr Mladekdbf52682016-10-11 13:55:50 -0700724 worker->flags = flags;
Petr Mladekfbae2d42016-10-11 13:55:30 -0700725 worker->task = task;
726 wake_up_process(task);
727 return worker;
728
729fail_task:
730 kfree(worker);
731 return ERR_CAST(task);
732}
733
734/**
735 * kthread_create_worker - create a kthread worker
Petr Mladekdbf52682016-10-11 13:55:50 -0700736 * @flags: flags modifying the default behavior of the worker
Petr Mladekfbae2d42016-10-11 13:55:30 -0700737 * @namefmt: printf-style name for the kthread worker (task).
738 *
739 * Returns a pointer to the allocated worker on success, ERR_PTR(-ENOMEM)
740 * when the needed structures could not get allocated, and ERR_PTR(-EINTR)
741 * when the worker was SIGKILLed.
742 */
743struct kthread_worker *
Petr Mladekdbf52682016-10-11 13:55:50 -0700744kthread_create_worker(unsigned int flags, const char namefmt[], ...)
Petr Mladekfbae2d42016-10-11 13:55:30 -0700745{
746 struct kthread_worker *worker;
747 va_list args;
748
749 va_start(args, namefmt);
Petr Mladekdbf52682016-10-11 13:55:50 -0700750 worker = __kthread_create_worker(-1, flags, namefmt, args);
Petr Mladekfbae2d42016-10-11 13:55:30 -0700751 va_end(args);
752
753 return worker;
754}
755EXPORT_SYMBOL(kthread_create_worker);
756
757/**
758 * kthread_create_worker_on_cpu - create a kthread worker and bind it
759 * it to a given CPU and the associated NUMA node.
760 * @cpu: CPU number
Petr Mladekdbf52682016-10-11 13:55:50 -0700761 * @flags: flags modifying the default behavior of the worker
Petr Mladekfbae2d42016-10-11 13:55:30 -0700762 * @namefmt: printf-style name for the kthread worker (task).
763 *
764 * Use a valid CPU number if you want to bind the kthread worker
765 * to the given CPU and the associated NUMA node.
766 *
767 * A good practice is to add the cpu number also into the worker name.
768 * For example, use kthread_create_worker_on_cpu(cpu, "helper/%d", cpu).
769 *
770 * Returns a pointer to the allocated worker on success, ERR_PTR(-ENOMEM)
771 * when the needed structures could not get allocated, and ERR_PTR(-EINTR)
772 * when the worker was SIGKILLed.
773 */
774struct kthread_worker *
Petr Mladekdbf52682016-10-11 13:55:50 -0700775kthread_create_worker_on_cpu(int cpu, unsigned int flags,
776 const char namefmt[], ...)
Petr Mladekfbae2d42016-10-11 13:55:30 -0700777{
778 struct kthread_worker *worker;
779 va_list args;
780
781 va_start(args, namefmt);
Petr Mladekdbf52682016-10-11 13:55:50 -0700782 worker = __kthread_create_worker(cpu, flags, namefmt, args);
Petr Mladekfbae2d42016-10-11 13:55:30 -0700783 va_end(args);
784
785 return worker;
786}
787EXPORT_SYMBOL(kthread_create_worker_on_cpu);
788
Petr Mladek37be45d2016-10-11 13:55:43 -0700789/*
790 * Returns true when the work could not be queued at the moment.
791 * It happens when it is already pending in a worker list
792 * or when it is being cancelled.
793 */
794static inline bool queuing_blocked(struct kthread_worker *worker,
795 struct kthread_work *work)
796{
797 lockdep_assert_held(&worker->lock);
798
799 return !list_empty(&work->node) || work->canceling;
800}
801
Petr Mladek8197b3d42016-10-11 13:55:36 -0700802static void kthread_insert_work_sanity_check(struct kthread_worker *worker,
803 struct kthread_work *work)
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700804{
805 lockdep_assert_held(&worker->lock);
Petr Mladek8197b3d42016-10-11 13:55:36 -0700806 WARN_ON_ONCE(!list_empty(&work->node));
807 /* Do not use a work with >1 worker, see kthread_queue_work() */
808 WARN_ON_ONCE(work->worker && work->worker != worker);
809}
810
811/* insert @work before @pos in @worker */
812static void kthread_insert_work(struct kthread_worker *worker,
813 struct kthread_work *work,
814 struct list_head *pos)
815{
816 kthread_insert_work_sanity_check(worker, work);
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700817
818 list_add_tail(&work->node, pos);
Tejun Heo46f3d972012-07-19 13:52:53 -0700819 work->worker = worker;
Lai Jiangshaned1403e2014-07-26 12:03:59 +0800820 if (!worker->current_work && likely(worker->task))
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700821 wake_up_process(worker->task);
822}
823
Tejun Heob56c0d82010-06-29 10:07:09 +0200824/**
Petr Mladek39891442016-10-11 13:55:20 -0700825 * kthread_queue_work - queue a kthread_work
Tejun Heob56c0d82010-06-29 10:07:09 +0200826 * @worker: target kthread_worker
827 * @work: kthread_work to queue
828 *
829 * Queue @work to work processor @task for async execution. @task
830 * must have been created with kthread_worker_create(). Returns %true
831 * if @work was successfully queued, %false if it was already pending.
Petr Mladek8197b3d42016-10-11 13:55:36 -0700832 *
833 * Reinitialize the work if it needs to be used by another worker.
834 * For example, when the worker was stopped and started again.
Tejun Heob56c0d82010-06-29 10:07:09 +0200835 */
Petr Mladek39891442016-10-11 13:55:20 -0700836bool kthread_queue_work(struct kthread_worker *worker,
Tejun Heob56c0d82010-06-29 10:07:09 +0200837 struct kthread_work *work)
838{
839 bool ret = false;
840 unsigned long flags;
841
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +0100842 raw_spin_lock_irqsave(&worker->lock, flags);
Petr Mladek37be45d2016-10-11 13:55:43 -0700843 if (!queuing_blocked(worker, work)) {
Petr Mladek39891442016-10-11 13:55:20 -0700844 kthread_insert_work(worker, work, &worker->work_list);
Tejun Heob56c0d82010-06-29 10:07:09 +0200845 ret = true;
846 }
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +0100847 raw_spin_unlock_irqrestore(&worker->lock, flags);
Tejun Heob56c0d82010-06-29 10:07:09 +0200848 return ret;
849}
Petr Mladek39891442016-10-11 13:55:20 -0700850EXPORT_SYMBOL_GPL(kthread_queue_work);
Tejun Heob56c0d82010-06-29 10:07:09 +0200851
Petr Mladek22597dc2016-10-11 13:55:40 -0700852/**
853 * kthread_delayed_work_timer_fn - callback that queues the associated kthread
854 * delayed work when the timer expires.
Kees Cookfe5c3b62017-10-04 16:27:06 -0700855 * @t: pointer to the expired timer
Petr Mladek22597dc2016-10-11 13:55:40 -0700856 *
857 * The format of the function is defined by struct timer_list.
858 * It should have been called from irqsafe timer with irq already off.
859 */
Kees Cookfe5c3b62017-10-04 16:27:06 -0700860void kthread_delayed_work_timer_fn(struct timer_list *t)
Petr Mladek22597dc2016-10-11 13:55:40 -0700861{
Kees Cookfe5c3b62017-10-04 16:27:06 -0700862 struct kthread_delayed_work *dwork = from_timer(dwork, t, timer);
Petr Mladek22597dc2016-10-11 13:55:40 -0700863 struct kthread_work *work = &dwork->work;
864 struct kthread_worker *worker = work->worker;
Sebastian Andrzej Siewiorad014232019-02-12 17:25:54 +0100865 unsigned long flags;
Petr Mladek22597dc2016-10-11 13:55:40 -0700866
867 /*
868 * This might happen when a pending work is reinitialized.
869 * It means that it is used a wrong way.
870 */
871 if (WARN_ON_ONCE(!worker))
872 return;
873
Sebastian Andrzej Siewiorad014232019-02-12 17:25:54 +0100874 raw_spin_lock_irqsave(&worker->lock, flags);
Petr Mladek22597dc2016-10-11 13:55:40 -0700875 /* Work must not be used with >1 worker, see kthread_queue_work(). */
876 WARN_ON_ONCE(work->worker != worker);
877
878 /* Move the work from worker->delayed_work_list. */
879 WARN_ON_ONCE(list_empty(&work->node));
880 list_del_init(&work->node);
881 kthread_insert_work(worker, work, &worker->work_list);
882
Sebastian Andrzej Siewiorad014232019-02-12 17:25:54 +0100883 raw_spin_unlock_irqrestore(&worker->lock, flags);
Petr Mladek22597dc2016-10-11 13:55:40 -0700884}
885EXPORT_SYMBOL(kthread_delayed_work_timer_fn);
886
Ben Dooksbc88f852019-10-16 12:24:58 +0100887static void __kthread_queue_delayed_work(struct kthread_worker *worker,
888 struct kthread_delayed_work *dwork,
889 unsigned long delay)
Petr Mladek22597dc2016-10-11 13:55:40 -0700890{
891 struct timer_list *timer = &dwork->timer;
892 struct kthread_work *work = &dwork->work;
893
Kees Cook841b86f2017-10-23 09:40:42 +0200894 WARN_ON_ONCE(timer->function != kthread_delayed_work_timer_fn);
Petr Mladek22597dc2016-10-11 13:55:40 -0700895
896 /*
897 * If @delay is 0, queue @dwork->work immediately. This is for
898 * both optimization and correctness. The earliest @timer can
899 * expire is on the closest next tick and delayed_work users depend
900 * on that there's no such delay when @delay is 0.
901 */
902 if (!delay) {
903 kthread_insert_work(worker, work, &worker->work_list);
904 return;
905 }
906
907 /* Be paranoid and try to detect possible races already now. */
908 kthread_insert_work_sanity_check(worker, work);
909
910 list_add(&work->node, &worker->delayed_work_list);
911 work->worker = worker;
Petr Mladek22597dc2016-10-11 13:55:40 -0700912 timer->expires = jiffies + delay;
913 add_timer(timer);
914}
915
916/**
917 * kthread_queue_delayed_work - queue the associated kthread work
918 * after a delay.
919 * @worker: target kthread_worker
920 * @dwork: kthread_delayed_work to queue
921 * @delay: number of jiffies to wait before queuing
922 *
923 * If the work has not been pending it starts a timer that will queue
924 * the work after the given @delay. If @delay is zero, it queues the
925 * work immediately.
926 *
927 * Return: %false if the @work has already been pending. It means that
928 * either the timer was running or the work was queued. It returns %true
929 * otherwise.
930 */
931bool kthread_queue_delayed_work(struct kthread_worker *worker,
932 struct kthread_delayed_work *dwork,
933 unsigned long delay)
934{
935 struct kthread_work *work = &dwork->work;
936 unsigned long flags;
937 bool ret = false;
938
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +0100939 raw_spin_lock_irqsave(&worker->lock, flags);
Petr Mladek22597dc2016-10-11 13:55:40 -0700940
Petr Mladek37be45d2016-10-11 13:55:43 -0700941 if (!queuing_blocked(worker, work)) {
Petr Mladek22597dc2016-10-11 13:55:40 -0700942 __kthread_queue_delayed_work(worker, dwork, delay);
943 ret = true;
944 }
945
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +0100946 raw_spin_unlock_irqrestore(&worker->lock, flags);
Petr Mladek22597dc2016-10-11 13:55:40 -0700947 return ret;
948}
949EXPORT_SYMBOL_GPL(kthread_queue_delayed_work);
950
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700951struct kthread_flush_work {
952 struct kthread_work work;
953 struct completion done;
954};
955
956static void kthread_flush_work_fn(struct kthread_work *work)
957{
958 struct kthread_flush_work *fwork =
959 container_of(work, struct kthread_flush_work, work);
960 complete(&fwork->done);
961}
962
Tejun Heob56c0d82010-06-29 10:07:09 +0200963/**
Petr Mladek39891442016-10-11 13:55:20 -0700964 * kthread_flush_work - flush a kthread_work
Tejun Heob56c0d82010-06-29 10:07:09 +0200965 * @work: work to flush
966 *
967 * If @work is queued or executing, wait for it to finish execution.
968 */
Petr Mladek39891442016-10-11 13:55:20 -0700969void kthread_flush_work(struct kthread_work *work)
Tejun Heob56c0d82010-06-29 10:07:09 +0200970{
Tejun Heo46f3d972012-07-19 13:52:53 -0700971 struct kthread_flush_work fwork = {
972 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
973 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
974 };
975 struct kthread_worker *worker;
976 bool noop = false;
Tejun Heob56c0d82010-06-29 10:07:09 +0200977
Tejun Heo46f3d972012-07-19 13:52:53 -0700978 worker = work->worker;
979 if (!worker)
980 return;
Tejun Heob56c0d82010-06-29 10:07:09 +0200981
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +0100982 raw_spin_lock_irq(&worker->lock);
Petr Mladek8197b3d42016-10-11 13:55:36 -0700983 /* Work must not be used with >1 worker, see kthread_queue_work(). */
984 WARN_ON_ONCE(work->worker != worker);
Tejun Heob56c0d82010-06-29 10:07:09 +0200985
Tejun Heo46f3d972012-07-19 13:52:53 -0700986 if (!list_empty(&work->node))
Petr Mladek39891442016-10-11 13:55:20 -0700987 kthread_insert_work(worker, &fwork.work, work->node.next);
Tejun Heo46f3d972012-07-19 13:52:53 -0700988 else if (worker->current_work == work)
Petr Mladek39891442016-10-11 13:55:20 -0700989 kthread_insert_work(worker, &fwork.work,
990 worker->work_list.next);
Tejun Heo46f3d972012-07-19 13:52:53 -0700991 else
992 noop = true;
Tejun Heob56c0d82010-06-29 10:07:09 +0200993
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +0100994 raw_spin_unlock_irq(&worker->lock);
Tejun Heo46f3d972012-07-19 13:52:53 -0700995
996 if (!noop)
997 wait_for_completion(&fwork.done);
Tejun Heob56c0d82010-06-29 10:07:09 +0200998}
Petr Mladek39891442016-10-11 13:55:20 -0700999EXPORT_SYMBOL_GPL(kthread_flush_work);
Tejun Heob56c0d82010-06-29 10:07:09 +02001000
Petr Mladek37be45d2016-10-11 13:55:43 -07001001/*
1002 * This function removes the work from the worker queue. Also it makes sure
1003 * that it won't get queued later via the delayed work's timer.
1004 *
1005 * The work might still be in use when this function finishes. See the
1006 * current_work proceed by the worker.
1007 *
1008 * Return: %true if @work was pending and successfully canceled,
1009 * %false if @work was not pending
1010 */
1011static bool __kthread_cancel_work(struct kthread_work *work, bool is_dwork,
1012 unsigned long *flags)
1013{
1014 /* Try to cancel the timer if exists. */
1015 if (is_dwork) {
1016 struct kthread_delayed_work *dwork =
1017 container_of(work, struct kthread_delayed_work, work);
1018 struct kthread_worker *worker = work->worker;
1019
1020 /*
1021 * del_timer_sync() must be called to make sure that the timer
1022 * callback is not running. The lock must be temporary released
1023 * to avoid a deadlock with the callback. In the meantime,
1024 * any queuing is blocked by setting the canceling counter.
1025 */
1026 work->canceling++;
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +01001027 raw_spin_unlock_irqrestore(&worker->lock, *flags);
Petr Mladek37be45d2016-10-11 13:55:43 -07001028 del_timer_sync(&dwork->timer);
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +01001029 raw_spin_lock_irqsave(&worker->lock, *flags);
Petr Mladek37be45d2016-10-11 13:55:43 -07001030 work->canceling--;
1031 }
1032
1033 /*
1034 * Try to remove the work from a worker list. It might either
1035 * be from worker->work_list or from worker->delayed_work_list.
1036 */
1037 if (!list_empty(&work->node)) {
1038 list_del_init(&work->node);
1039 return true;
1040 }
1041
1042 return false;
1043}
1044
Petr Mladek9a6b06c2016-10-11 13:55:46 -07001045/**
1046 * kthread_mod_delayed_work - modify delay of or queue a kthread delayed work
1047 * @worker: kthread worker to use
1048 * @dwork: kthread delayed work to queue
1049 * @delay: number of jiffies to wait before queuing
1050 *
1051 * If @dwork is idle, equivalent to kthread_queue_delayed_work(). Otherwise,
1052 * modify @dwork's timer so that it expires after @delay. If @delay is zero,
1053 * @work is guaranteed to be queued immediately.
1054 *
1055 * Return: %true if @dwork was pending and its timer was modified,
1056 * %false otherwise.
1057 *
1058 * A special case is when the work is being canceled in parallel.
1059 * It might be caused either by the real kthread_cancel_delayed_work_sync()
1060 * or yet another kthread_mod_delayed_work() call. We let the other command
1061 * win and return %false here. The caller is supposed to synchronize these
1062 * operations a reasonable way.
1063 *
1064 * This function is safe to call from any context including IRQ handler.
1065 * See __kthread_cancel_work() and kthread_delayed_work_timer_fn()
1066 * for details.
1067 */
1068bool kthread_mod_delayed_work(struct kthread_worker *worker,
1069 struct kthread_delayed_work *dwork,
1070 unsigned long delay)
1071{
1072 struct kthread_work *work = &dwork->work;
1073 unsigned long flags;
1074 int ret = false;
1075
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +01001076 raw_spin_lock_irqsave(&worker->lock, flags);
Petr Mladek9a6b06c2016-10-11 13:55:46 -07001077
1078 /* Do not bother with canceling when never queued. */
1079 if (!work->worker)
1080 goto fast_queue;
1081
1082 /* Work must not be used with >1 worker, see kthread_queue_work() */
1083 WARN_ON_ONCE(work->worker != worker);
1084
1085 /* Do not fight with another command that is canceling this work. */
1086 if (work->canceling)
1087 goto out;
1088
1089 ret = __kthread_cancel_work(work, true, &flags);
1090fast_queue:
1091 __kthread_queue_delayed_work(worker, dwork, delay);
1092out:
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +01001093 raw_spin_unlock_irqrestore(&worker->lock, flags);
Petr Mladek9a6b06c2016-10-11 13:55:46 -07001094 return ret;
1095}
1096EXPORT_SYMBOL_GPL(kthread_mod_delayed_work);
1097
Petr Mladek37be45d2016-10-11 13:55:43 -07001098static bool __kthread_cancel_work_sync(struct kthread_work *work, bool is_dwork)
1099{
1100 struct kthread_worker *worker = work->worker;
1101 unsigned long flags;
1102 int ret = false;
1103
1104 if (!worker)
1105 goto out;
1106
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +01001107 raw_spin_lock_irqsave(&worker->lock, flags);
Petr Mladek37be45d2016-10-11 13:55:43 -07001108 /* Work must not be used with >1 worker, see kthread_queue_work(). */
1109 WARN_ON_ONCE(work->worker != worker);
1110
1111 ret = __kthread_cancel_work(work, is_dwork, &flags);
1112
1113 if (worker->current_work != work)
1114 goto out_fast;
1115
1116 /*
1117 * The work is in progress and we need to wait with the lock released.
1118 * In the meantime, block any queuing by setting the canceling counter.
1119 */
1120 work->canceling++;
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +01001121 raw_spin_unlock_irqrestore(&worker->lock, flags);
Petr Mladek37be45d2016-10-11 13:55:43 -07001122 kthread_flush_work(work);
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +01001123 raw_spin_lock_irqsave(&worker->lock, flags);
Petr Mladek37be45d2016-10-11 13:55:43 -07001124 work->canceling--;
1125
1126out_fast:
Julia Cartwrightfe99a4f2019-02-12 17:25:53 +01001127 raw_spin_unlock_irqrestore(&worker->lock, flags);
Petr Mladek37be45d2016-10-11 13:55:43 -07001128out:
1129 return ret;
1130}
1131
1132/**
1133 * kthread_cancel_work_sync - cancel a kthread work and wait for it to finish
1134 * @work: the kthread work to cancel
1135 *
1136 * Cancel @work and wait for its execution to finish. This function
1137 * can be used even if the work re-queues itself. On return from this
1138 * function, @work is guaranteed to be not pending or executing on any CPU.
1139 *
1140 * kthread_cancel_work_sync(&delayed_work->work) must not be used for
1141 * delayed_work's. Use kthread_cancel_delayed_work_sync() instead.
1142 *
1143 * The caller must ensure that the worker on which @work was last
1144 * queued can't be destroyed before this function returns.
1145 *
1146 * Return: %true if @work was pending, %false otherwise.
1147 */
1148bool kthread_cancel_work_sync(struct kthread_work *work)
1149{
1150 return __kthread_cancel_work_sync(work, false);
1151}
1152EXPORT_SYMBOL_GPL(kthread_cancel_work_sync);
1153
1154/**
1155 * kthread_cancel_delayed_work_sync - cancel a kthread delayed work and
1156 * wait for it to finish.
1157 * @dwork: the kthread delayed work to cancel
1158 *
1159 * This is kthread_cancel_work_sync() for delayed works.
1160 *
1161 * Return: %true if @dwork was pending, %false otherwise.
1162 */
1163bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *dwork)
1164{
1165 return __kthread_cancel_work_sync(&dwork->work, true);
1166}
1167EXPORT_SYMBOL_GPL(kthread_cancel_delayed_work_sync);
1168
Tejun Heob56c0d82010-06-29 10:07:09 +02001169/**
Petr Mladek39891442016-10-11 13:55:20 -07001170 * kthread_flush_worker - flush all current works on a kthread_worker
Tejun Heob56c0d82010-06-29 10:07:09 +02001171 * @worker: worker to flush
1172 *
1173 * Wait until all currently executing or pending works on @worker are
1174 * finished.
1175 */
Petr Mladek39891442016-10-11 13:55:20 -07001176void kthread_flush_worker(struct kthread_worker *worker)
Tejun Heob56c0d82010-06-29 10:07:09 +02001177{
1178 struct kthread_flush_work fwork = {
1179 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
1180 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
1181 };
1182
Petr Mladek39891442016-10-11 13:55:20 -07001183 kthread_queue_work(worker, &fwork.work);
Tejun Heob56c0d82010-06-29 10:07:09 +02001184 wait_for_completion(&fwork.done);
1185}
Petr Mladek39891442016-10-11 13:55:20 -07001186EXPORT_SYMBOL_GPL(kthread_flush_worker);
Petr Mladek35033fe2016-10-11 13:55:33 -07001187
1188/**
1189 * kthread_destroy_worker - destroy a kthread worker
1190 * @worker: worker to be destroyed
1191 *
1192 * Flush and destroy @worker. The simple flush is enough because the kthread
1193 * worker API is used only in trivial scenarios. There are no multi-step state
1194 * machines needed.
1195 */
1196void kthread_destroy_worker(struct kthread_worker *worker)
1197{
1198 struct task_struct *task;
1199
1200 task = worker->task;
1201 if (WARN_ON(!task))
1202 return;
1203
1204 kthread_flush_worker(worker);
1205 kthread_stop(task);
1206 WARN_ON(!list_empty(&worker->work_list));
1207 kfree(worker);
1208}
1209EXPORT_SYMBOL(kthread_destroy_worker);
Shaohua Li05e3db92017-09-14 14:02:04 -07001210
Christoph Hellwig9bf5b9eb2020-06-10 18:41:59 -07001211/*
1212 * use_mm
1213 * Makes the calling kernel thread take on the specified
1214 * mm context.
1215 * (Note: this routine is intended to be called only
1216 * from a kernel thread context)
1217 */
1218void use_mm(struct mm_struct *mm)
1219{
1220 struct mm_struct *active_mm;
1221 struct task_struct *tsk = current;
1222
1223 task_lock(tsk);
1224 active_mm = tsk->active_mm;
1225 if (active_mm != mm) {
1226 mmgrab(mm);
1227 tsk->active_mm = mm;
1228 }
1229 tsk->mm = mm;
1230 switch_mm(active_mm, mm, tsk);
1231 task_unlock(tsk);
1232#ifdef finish_arch_post_lock_switch
1233 finish_arch_post_lock_switch();
1234#endif
1235
1236 if (active_mm != mm)
1237 mmdrop(active_mm);
1238}
1239EXPORT_SYMBOL_GPL(use_mm);
1240
1241/*
1242 * unuse_mm
1243 * Reverses the effect of use_mm, i.e. releases the
1244 * specified mm context which was earlier taken on
1245 * by the calling kernel thread
1246 * (Note: this routine is intended to be called only
1247 * from a kernel thread context)
1248 */
1249void unuse_mm(struct mm_struct *mm)
1250{
1251 struct task_struct *tsk = current;
1252
1253 task_lock(tsk);
1254 sync_mm_rss(mm);
1255 tsk->mm = NULL;
1256 /* active_mm is still 'mm' */
1257 enter_lazy_tlb(mm, tsk);
1258 task_unlock(tsk);
1259}
1260EXPORT_SYMBOL_GPL(unuse_mm);
1261
Shaohua Li0b508bc2017-09-26 11:02:12 -07001262#ifdef CONFIG_BLK_CGROUP
Shaohua Li05e3db92017-09-14 14:02:04 -07001263/**
1264 * kthread_associate_blkcg - associate blkcg to current kthread
1265 * @css: the cgroup info
1266 *
1267 * Current thread must be a kthread. The thread is running jobs on behalf of
1268 * other threads. In some cases, we expect the jobs attach cgroup info of
1269 * original threads instead of that of current thread. This function stores
1270 * original thread's cgroup info in current kthread context for later
1271 * retrieval.
1272 */
1273void kthread_associate_blkcg(struct cgroup_subsys_state *css)
1274{
1275 struct kthread *kthread;
1276
1277 if (!(current->flags & PF_KTHREAD))
1278 return;
1279 kthread = to_kthread(current);
1280 if (!kthread)
1281 return;
1282
1283 if (kthread->blkcg_css) {
1284 css_put(kthread->blkcg_css);
1285 kthread->blkcg_css = NULL;
1286 }
1287 if (css) {
1288 css_get(css);
1289 kthread->blkcg_css = css;
1290 }
1291}
1292EXPORT_SYMBOL(kthread_associate_blkcg);
1293
1294/**
1295 * kthread_blkcg - get associated blkcg css of current kthread
1296 *
1297 * Current thread must be a kthread.
1298 */
1299struct cgroup_subsys_state *kthread_blkcg(void)
1300{
1301 struct kthread *kthread;
1302
1303 if (current->flags & PF_KTHREAD) {
1304 kthread = to_kthread(current);
1305 if (kthread)
1306 return kthread->blkcg_css;
1307 }
1308 return NULL;
1309}
1310EXPORT_SYMBOL(kthread_blkcg);
1311#endif