xref: /linux-6.15/kernel/kthread.c (revision bbda86e9)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /* Kernel thread helper functions.
31da177e4SLinus Torvalds  *   Copyright (C) 2004 IBM Corporation, Rusty Russell.
49bf5b9ebSChristoph Hellwig  *   Copyright (C) 2009 Red Hat, Inc.
51da177e4SLinus Torvalds  *
673c27992SEric W. Biederman  * Creation is done via kthreadd, so that we get a clean environment
71da177e4SLinus Torvalds  * even if we're invoked from userspace (think modprobe, hotplug cpu,
81da177e4SLinus Torvalds  * etc.).
91da177e4SLinus Torvalds  */
10ae7e81c0SIngo Molnar #include <uapi/linux/sched/types.h>
119bf5b9ebSChristoph Hellwig #include <linux/mm.h>
129bf5b9ebSChristoph Hellwig #include <linux/mmu_context.h>
131da177e4SLinus Torvalds #include <linux/sched.h>
149bf5b9ebSChristoph Hellwig #include <linux/sched/mm.h>
1529930025SIngo Molnar #include <linux/sched/task.h>
161da177e4SLinus Torvalds #include <linux/kthread.h>
171da177e4SLinus Torvalds #include <linux/completion.h>
181da177e4SLinus Torvalds #include <linux/err.h>
198af0c18aSSuren Baghdasaryan #include <linux/cgroup.h>
2058568d2aSMiao Xie #include <linux/cpuset.h>
211da177e4SLinus Torvalds #include <linux/unistd.h>
221da177e4SLinus Torvalds #include <linux/file.h>
239984de1aSPaul Gortmaker #include <linux/export.h>
2497d1f15bSArjan van de Ven #include <linux/mutex.h>
25b56c0d89STejun Heo #include <linux/slab.h>
26b56c0d89STejun Heo #include <linux/freezer.h>
27a74fb73cSAl Viro #include <linux/ptrace.h>
28cd42d559STejun Heo #include <linux/uaccess.h>
2998fa15f3SAnshuman Khandual #include <linux/numa.h>
309cc5b865SMarcelo Tosatti #include <linux/sched/isolation.h>
31ad8d75ffSSteven Rostedt #include <trace/events/sched.h>
321da177e4SLinus Torvalds 
339bf5b9ebSChristoph Hellwig 
3473c27992SEric W. Biederman static DEFINE_SPINLOCK(kthread_create_lock);
3573c27992SEric W. Biederman static LIST_HEAD(kthread_create_list);
3673c27992SEric W. Biederman struct task_struct *kthreadd_task;
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds struct kthread_create_info
391da177e4SLinus Torvalds {
4073c27992SEric W. Biederman 	/* Information passed to kthread() from kthreadd. */
411da177e4SLinus Torvalds 	int (*threadfn)(void *data);
421da177e4SLinus Torvalds 	void *data;
43207205a2SEric Dumazet 	int node;
441da177e4SLinus Torvalds 
4573c27992SEric W. Biederman 	/* Result passed back to kthread_create() from kthreadd. */
461da177e4SLinus Torvalds 	struct task_struct *result;
47786235eeSTetsuo Handa 	struct completion *done;
4865f27f38SDavid Howells 
4973c27992SEric W. Biederman 	struct list_head list;
501da177e4SLinus Torvalds };
511da177e4SLinus Torvalds 
5263706172SOleg Nesterov struct kthread {
532a1d4460SThomas Gleixner 	unsigned long flags;
542a1d4460SThomas Gleixner 	unsigned int cpu;
5552782c92SJ. Bruce Fields 	int (*threadfn)(void *);
5682805ab7STejun Heo 	void *data;
5737c54f9bSChristoph Hellwig 	mm_segment_t oldfs;
582a1d4460SThomas Gleixner 	struct completion parked;
5963706172SOleg Nesterov 	struct completion exited;
600b508bc9SShaohua Li #ifdef CONFIG_BLK_CGROUP
6105e3db95SShaohua Li 	struct cgroup_subsys_state *blkcg_css;
6205e3db95SShaohua Li #endif
631da177e4SLinus Torvalds };
641da177e4SLinus Torvalds 
652a1d4460SThomas Gleixner enum KTHREAD_BITS {
662a1d4460SThomas Gleixner 	KTHREAD_IS_PER_CPU = 0,
672a1d4460SThomas Gleixner 	KTHREAD_SHOULD_STOP,
682a1d4460SThomas Gleixner 	KTHREAD_SHOULD_PARK,
692a1d4460SThomas Gleixner };
702a1d4460SThomas Gleixner 
714ecdafc8SOleg Nesterov static inline struct kthread *to_kthread(struct task_struct *k)
724ecdafc8SOleg Nesterov {
731da5c46fSOleg Nesterov 	WARN_ON(!(k->flags & PF_KTHREAD));
741da5c46fSOleg Nesterov 	return (__force void *)k->set_child_tid;
754ecdafc8SOleg Nesterov }
764ecdafc8SOleg Nesterov 
773a7956e2SPeter Zijlstra /*
783a7956e2SPeter Zijlstra  * Variant of to_kthread() that doesn't assume @p is a kthread.
793a7956e2SPeter Zijlstra  *
803a7956e2SPeter Zijlstra  * Per construction; when:
813a7956e2SPeter Zijlstra  *
823a7956e2SPeter Zijlstra  *   (p->flags & PF_KTHREAD) && p->set_child_tid
833a7956e2SPeter Zijlstra  *
843a7956e2SPeter Zijlstra  * the task is both a kthread and struct kthread is persistent. However
853a7956e2SPeter Zijlstra  * PF_KTHREAD on it's own is not, kernel_thread() can exec() (See umh.c and
863a7956e2SPeter Zijlstra  * begin_new_exec()).
873a7956e2SPeter Zijlstra  */
883a7956e2SPeter Zijlstra static inline struct kthread *__to_kthread(struct task_struct *p)
893a7956e2SPeter Zijlstra {
903a7956e2SPeter Zijlstra 	void *kthread = (__force void *)p->set_child_tid;
913a7956e2SPeter Zijlstra 	if (kthread && !(p->flags & PF_KTHREAD))
923a7956e2SPeter Zijlstra 		kthread = NULL;
933a7956e2SPeter Zijlstra 	return kthread;
943a7956e2SPeter Zijlstra }
953a7956e2SPeter Zijlstra 
9600b89fe0SValentin Schneider void set_kthread_struct(struct task_struct *p)
9700b89fe0SValentin Schneider {
9800b89fe0SValentin Schneider 	struct kthread *kthread;
9900b89fe0SValentin Schneider 
10000b89fe0SValentin Schneider 	if (__to_kthread(p))
10100b89fe0SValentin Schneider 		return;
10200b89fe0SValentin Schneider 
10300b89fe0SValentin Schneider 	kthread = kzalloc(sizeof(*kthread), GFP_KERNEL);
10400b89fe0SValentin Schneider 	/*
10500b89fe0SValentin Schneider 	 * We abuse ->set_child_tid to avoid the new member and because it
10600b89fe0SValentin Schneider 	 * can't be wrongly copied by copy_process(). We also rely on fact
10700b89fe0SValentin Schneider 	 * that the caller can't exec, so PF_KTHREAD can't be cleared.
10800b89fe0SValentin Schneider 	 */
10900b89fe0SValentin Schneider 	p->set_child_tid = (__force void __user *)kthread;
11000b89fe0SValentin Schneider }
11100b89fe0SValentin Schneider 
1121da5c46fSOleg Nesterov void free_kthread_struct(struct task_struct *k)
1131da5c46fSOleg Nesterov {
11405e3db95SShaohua Li 	struct kthread *kthread;
11505e3db95SShaohua Li 
1161da5c46fSOleg Nesterov 	/*
1171da5c46fSOleg Nesterov 	 * Can be NULL if this kthread was created by kernel_thread()
1181da5c46fSOleg Nesterov 	 * or if kmalloc() in kthread() failed.
1191da5c46fSOleg Nesterov 	 */
12005e3db95SShaohua Li 	kthread = to_kthread(k);
1210b508bc9SShaohua Li #ifdef CONFIG_BLK_CGROUP
12205e3db95SShaohua Li 	WARN_ON_ONCE(kthread && kthread->blkcg_css);
12305e3db95SShaohua Li #endif
12405e3db95SShaohua Li 	kfree(kthread);
1251da5c46fSOleg Nesterov }
1261da5c46fSOleg Nesterov 
1279e37bd30SRandy Dunlap /**
1289e37bd30SRandy Dunlap  * kthread_should_stop - should this kthread return now?
1299e37bd30SRandy Dunlap  *
13072fd4a35SRobert P. J. Day  * When someone calls kthread_stop() on your kthread, it will be woken
1319e37bd30SRandy Dunlap  * and this will return true.  You should then return, and your return
1329e37bd30SRandy Dunlap  * value will be passed through to kthread_stop().
1339e37bd30SRandy Dunlap  */
1342a1d4460SThomas Gleixner bool kthread_should_stop(void)
1351da177e4SLinus Torvalds {
1362a1d4460SThomas Gleixner 	return test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags);
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds EXPORT_SYMBOL(kthread_should_stop);
1391da177e4SLinus Torvalds 
1400121805dSMatthias Kaehlcke bool __kthread_should_park(struct task_struct *k)
1410121805dSMatthias Kaehlcke {
1420121805dSMatthias Kaehlcke 	return test_bit(KTHREAD_SHOULD_PARK, &to_kthread(k)->flags);
1430121805dSMatthias Kaehlcke }
1440121805dSMatthias Kaehlcke EXPORT_SYMBOL_GPL(__kthread_should_park);
1450121805dSMatthias Kaehlcke 
14682805ab7STejun Heo /**
1472a1d4460SThomas Gleixner  * kthread_should_park - should this kthread park now?
1482a1d4460SThomas Gleixner  *
1492a1d4460SThomas Gleixner  * When someone calls kthread_park() on your kthread, it will be woken
1502a1d4460SThomas Gleixner  * and this will return true.  You should then do the necessary
1512a1d4460SThomas Gleixner  * cleanup and call kthread_parkme()
1522a1d4460SThomas Gleixner  *
1532a1d4460SThomas Gleixner  * Similar to kthread_should_stop(), but this keeps the thread alive
1542a1d4460SThomas Gleixner  * and in a park position. kthread_unpark() "restarts" the thread and
1552a1d4460SThomas Gleixner  * calls the thread function again.
1562a1d4460SThomas Gleixner  */
1572a1d4460SThomas Gleixner bool kthread_should_park(void)
1582a1d4460SThomas Gleixner {
1590121805dSMatthias Kaehlcke 	return __kthread_should_park(current);
1602a1d4460SThomas Gleixner }
16118896451SDavid Kershner EXPORT_SYMBOL_GPL(kthread_should_park);
1622a1d4460SThomas Gleixner 
1632a1d4460SThomas Gleixner /**
1648a32c441STejun Heo  * kthread_freezable_should_stop - should this freezable kthread return now?
1658a32c441STejun Heo  * @was_frozen: optional out parameter, indicates whether %current was frozen
1668a32c441STejun Heo  *
1678a32c441STejun Heo  * kthread_should_stop() for freezable kthreads, which will enter
1688a32c441STejun Heo  * refrigerator if necessary.  This function is safe from kthread_stop() /
1698a32c441STejun Heo  * freezer deadlock and freezable kthreads should use this function instead
1708a32c441STejun Heo  * of calling try_to_freeze() directly.
1718a32c441STejun Heo  */
1728a32c441STejun Heo bool kthread_freezable_should_stop(bool *was_frozen)
1738a32c441STejun Heo {
1748a32c441STejun Heo 	bool frozen = false;
1758a32c441STejun Heo 
1768a32c441STejun Heo 	might_sleep();
1778a32c441STejun Heo 
1788a32c441STejun Heo 	if (unlikely(freezing(current)))
1798a32c441STejun Heo 		frozen = __refrigerator(true);
1808a32c441STejun Heo 
1818a32c441STejun Heo 	if (was_frozen)
1828a32c441STejun Heo 		*was_frozen = frozen;
1838a32c441STejun Heo 
1848a32c441STejun Heo 	return kthread_should_stop();
1858a32c441STejun Heo }
1868a32c441STejun Heo EXPORT_SYMBOL_GPL(kthread_freezable_should_stop);
1878a32c441STejun Heo 
1888a32c441STejun Heo /**
18952782c92SJ. Bruce Fields  * kthread_func - return the function specified on kthread creation
19052782c92SJ. Bruce Fields  * @task: kthread task in question
19152782c92SJ. Bruce Fields  *
19252782c92SJ. Bruce Fields  * Returns NULL if the task is not a kthread.
19352782c92SJ. Bruce Fields  */
19452782c92SJ. Bruce Fields void *kthread_func(struct task_struct *task)
19552782c92SJ. Bruce Fields {
1963a7956e2SPeter Zijlstra 	struct kthread *kthread = __to_kthread(task);
1973a7956e2SPeter Zijlstra 	if (kthread)
1983a7956e2SPeter Zijlstra 		return kthread->threadfn;
19952782c92SJ. Bruce Fields 	return NULL;
20052782c92SJ. Bruce Fields }
20152782c92SJ. Bruce Fields EXPORT_SYMBOL_GPL(kthread_func);
20252782c92SJ. Bruce Fields 
20352782c92SJ. Bruce Fields /**
20482805ab7STejun Heo  * kthread_data - return data value specified on kthread creation
20582805ab7STejun Heo  * @task: kthread task in question
20682805ab7STejun Heo  *
20782805ab7STejun Heo  * Return the data value specified when kthread @task was created.
20882805ab7STejun Heo  * The caller is responsible for ensuring the validity of @task when
20982805ab7STejun Heo  * calling this function.
21082805ab7STejun Heo  */
21182805ab7STejun Heo void *kthread_data(struct task_struct *task)
21282805ab7STejun Heo {
21382805ab7STejun Heo 	return to_kthread(task)->data;
21482805ab7STejun Heo }
21552782c92SJ. Bruce Fields EXPORT_SYMBOL_GPL(kthread_data);
21682805ab7STejun Heo 
217cd42d559STejun Heo /**
218e700591aSPetr Mladek  * kthread_probe_data - speculative version of kthread_data()
219cd42d559STejun Heo  * @task: possible kthread task in question
220cd42d559STejun Heo  *
221cd42d559STejun Heo  * @task could be a kthread task.  Return the data value specified when it
222cd42d559STejun Heo  * was created if accessible.  If @task isn't a kthread task or its data is
223cd42d559STejun Heo  * inaccessible for any reason, %NULL is returned.  This function requires
224cd42d559STejun Heo  * that @task itself is safe to dereference.
225cd42d559STejun Heo  */
226e700591aSPetr Mladek void *kthread_probe_data(struct task_struct *task)
227cd42d559STejun Heo {
2283a7956e2SPeter Zijlstra 	struct kthread *kthread = __to_kthread(task);
229cd42d559STejun Heo 	void *data = NULL;
230cd42d559STejun Heo 
2313a7956e2SPeter Zijlstra 	if (kthread)
232fe557319SChristoph Hellwig 		copy_from_kernel_nofault(&data, &kthread->data, sizeof(data));
233cd42d559STejun Heo 	return data;
234cd42d559STejun Heo }
235cd42d559STejun Heo 
2362a1d4460SThomas Gleixner static void __kthread_parkme(struct kthread *self)
2372a1d4460SThomas Gleixner {
238741a76b3SPeter Zijlstra 	for (;;) {
2391cef1150SPeter Zijlstra 		/*
2401cef1150SPeter Zijlstra 		 * TASK_PARKED is a special state; we must serialize against
2411cef1150SPeter Zijlstra 		 * possible pending wakeups to avoid store-store collisions on
2421cef1150SPeter Zijlstra 		 * task->state.
2431cef1150SPeter Zijlstra 		 *
2441cef1150SPeter Zijlstra 		 * Such a collision might possibly result in the task state
2451cef1150SPeter Zijlstra 		 * changin from TASK_PARKED and us failing the
2461cef1150SPeter Zijlstra 		 * wait_task_inactive() in kthread_park().
2471cef1150SPeter Zijlstra 		 */
2481cef1150SPeter Zijlstra 		set_special_state(TASK_PARKED);
249741a76b3SPeter Zijlstra 		if (!test_bit(KTHREAD_SHOULD_PARK, &self->flags))
250741a76b3SPeter Zijlstra 			break;
2511cef1150SPeter Zijlstra 
25226c7295bSLiang Chen 		/*
25326c7295bSLiang Chen 		 * Thread is going to call schedule(), do not preempt it,
25426c7295bSLiang Chen 		 * or the caller of kthread_park() may spend more time in
25526c7295bSLiang Chen 		 * wait_task_inactive().
25626c7295bSLiang Chen 		 */
25726c7295bSLiang Chen 		preempt_disable();
258f83ee19bSPeter Zijlstra 		complete(&self->parked);
25926c7295bSLiang Chen 		schedule_preempt_disabled();
26026c7295bSLiang Chen 		preempt_enable();
2612a1d4460SThomas Gleixner 	}
2622a1d4460SThomas Gleixner 	__set_current_state(TASK_RUNNING);
2632a1d4460SThomas Gleixner }
2642a1d4460SThomas Gleixner 
2652a1d4460SThomas Gleixner void kthread_parkme(void)
2662a1d4460SThomas Gleixner {
2672a1d4460SThomas Gleixner 	__kthread_parkme(to_kthread(current));
2682a1d4460SThomas Gleixner }
26918896451SDavid Kershner EXPORT_SYMBOL_GPL(kthread_parkme);
2702a1d4460SThomas Gleixner 
271*bbda86e9SEric W. Biederman /**
272*bbda86e9SEric W. Biederman  * kthread_exit - Cause the current kthread return @result to kthread_stop().
273*bbda86e9SEric W. Biederman  * @result: The integer value to return to kthread_stop().
274*bbda86e9SEric W. Biederman  *
275*bbda86e9SEric W. Biederman  * While kthread_exit can be called directly, it exists so that
276*bbda86e9SEric W. Biederman  * functions which do some additional work in non-modular code such as
277*bbda86e9SEric W. Biederman  * module_put_and_kthread_exit can be implemented.
278*bbda86e9SEric W. Biederman  *
279*bbda86e9SEric W. Biederman  * Does not return.
280*bbda86e9SEric W. Biederman  */
281*bbda86e9SEric W. Biederman void __noreturn kthread_exit(long result)
282*bbda86e9SEric W. Biederman {
283*bbda86e9SEric W. Biederman 	do_exit(result);
284*bbda86e9SEric W. Biederman }
285*bbda86e9SEric W. Biederman 
2861da177e4SLinus Torvalds static int kthread(void *_create)
2871da177e4SLinus Torvalds {
2881a7243caSSebastian Andrzej Siewior 	static const struct sched_param param = { .sched_priority = 0 };
28973c27992SEric W. Biederman 	/* Copy data: it's on kthread's stack */
29063706172SOleg Nesterov 	struct kthread_create_info *create = _create;
29163706172SOleg Nesterov 	int (*threadfn)(void *data) = create->threadfn;
29263706172SOleg Nesterov 	void *data = create->data;
293786235eeSTetsuo Handa 	struct completion *done;
2941da5c46fSOleg Nesterov 	struct kthread *self;
29563706172SOleg Nesterov 	int ret;
29663706172SOleg Nesterov 
29700b89fe0SValentin Schneider 	set_kthread_struct(current);
29800b89fe0SValentin Schneider 	self = to_kthread(current);
2991da177e4SLinus Torvalds 
300786235eeSTetsuo Handa 	/* If user was SIGKILLed, I release the structure. */
301786235eeSTetsuo Handa 	done = xchg(&create->done, NULL);
302786235eeSTetsuo Handa 	if (!done) {
303786235eeSTetsuo Handa 		kfree(create);
304*bbda86e9SEric W. Biederman 		kthread_exit(-EINTR);
305786235eeSTetsuo Handa 	}
3061da5c46fSOleg Nesterov 
3071da5c46fSOleg Nesterov 	if (!self) {
3081da5c46fSOleg Nesterov 		create->result = ERR_PTR(-ENOMEM);
3091da5c46fSOleg Nesterov 		complete(done);
310*bbda86e9SEric W. Biederman 		kthread_exit(-ENOMEM);
3111da5c46fSOleg Nesterov 	}
3121da5c46fSOleg Nesterov 
31352782c92SJ. Bruce Fields 	self->threadfn = threadfn;
3141da5c46fSOleg Nesterov 	self->data = data;
3151da5c46fSOleg Nesterov 	init_completion(&self->exited);
3161da5c46fSOleg Nesterov 	init_completion(&self->parked);
3171da5c46fSOleg Nesterov 	current->vfork_done = &self->exited;
3181da5c46fSOleg Nesterov 
3191a7243caSSebastian Andrzej Siewior 	/*
3201a7243caSSebastian Andrzej Siewior 	 * The new thread inherited kthreadd's priority and CPU mask. Reset
3211a7243caSSebastian Andrzej Siewior 	 * back to default in case they have been changed.
3221a7243caSSebastian Andrzej Siewior 	 */
3231a7243caSSebastian Andrzej Siewior 	sched_setscheduler_nocheck(current, SCHED_NORMAL, &param);
3241a7243caSSebastian Andrzej Siewior 	set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_KTHREAD));
3251a7243caSSebastian Andrzej Siewior 
3261da177e4SLinus Torvalds 	/* OK, tell user we're spawned, wait for stop or wakeup */
327a076e4bcSOleg Nesterov 	__set_current_state(TASK_UNINTERRUPTIBLE);
3283217ab97SVitaliy Gusev 	create->result = current;
32926c7295bSLiang Chen 	/*
33026c7295bSLiang Chen 	 * Thread is going to call schedule(), do not preempt it,
33126c7295bSLiang Chen 	 * or the creator may spend more time in wait_task_inactive().
33226c7295bSLiang Chen 	 */
33326c7295bSLiang Chen 	preempt_disable();
334786235eeSTetsuo Handa 	complete(done);
33526c7295bSLiang Chen 	schedule_preempt_disabled();
33626c7295bSLiang Chen 	preempt_enable();
3371da177e4SLinus Torvalds 
33863706172SOleg Nesterov 	ret = -EINTR;
3391da5c46fSOleg Nesterov 	if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) {
34077f88796STejun Heo 		cgroup_kthread_ready();
3411da5c46fSOleg Nesterov 		__kthread_parkme(self);
3422a1d4460SThomas Gleixner 		ret = threadfn(data);
3432a1d4460SThomas Gleixner 	}
344*bbda86e9SEric W. Biederman 	kthread_exit(ret);
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
347cb5021caSYanfei Xu /* called from kernel_clone() to get node information for about to be created task */
348207205a2SEric Dumazet int tsk_fork_get_node(struct task_struct *tsk)
349207205a2SEric Dumazet {
350207205a2SEric Dumazet #ifdef CONFIG_NUMA
351207205a2SEric Dumazet 	if (tsk == kthreadd_task)
352207205a2SEric Dumazet 		return tsk->pref_node_fork;
353207205a2SEric Dumazet #endif
35481c98869SNishanth Aravamudan 	return NUMA_NO_NODE;
355207205a2SEric Dumazet }
356207205a2SEric Dumazet 
35773c27992SEric W. Biederman static void create_kthread(struct kthread_create_info *create)
3581da177e4SLinus Torvalds {
3591da177e4SLinus Torvalds 	int pid;
3601da177e4SLinus Torvalds 
361207205a2SEric Dumazet #ifdef CONFIG_NUMA
362207205a2SEric Dumazet 	current->pref_node_fork = create->node;
363207205a2SEric Dumazet #endif
3641da177e4SLinus Torvalds 	/* We want our own signal handler (we take no signals by default). */
3651da177e4SLinus Torvalds 	pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
366cdd140bdSOleg Nesterov 	if (pid < 0) {
367786235eeSTetsuo Handa 		/* If user was SIGKILLed, I release the structure. */
368786235eeSTetsuo Handa 		struct completion *done = xchg(&create->done, NULL);
369786235eeSTetsuo Handa 
370786235eeSTetsuo Handa 		if (!done) {
371786235eeSTetsuo Handa 			kfree(create);
372786235eeSTetsuo Handa 			return;
373786235eeSTetsuo Handa 		}
3741da177e4SLinus Torvalds 		create->result = ERR_PTR(pid);
375786235eeSTetsuo Handa 		complete(done);
3761da177e4SLinus Torvalds 	}
377cdd140bdSOleg Nesterov }
3781da177e4SLinus Torvalds 
379c0b942a7SNicolas Iooss static __printf(4, 0)
380c0b942a7SNicolas Iooss struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data),
3812a1d4460SThomas Gleixner 						    void *data, int node,
3821da177e4SLinus Torvalds 						    const char namefmt[],
383255451e4SPetr Mladek 						    va_list args)
3841da177e4SLinus Torvalds {
385786235eeSTetsuo Handa 	DECLARE_COMPLETION_ONSTACK(done);
386786235eeSTetsuo Handa 	struct task_struct *task;
387786235eeSTetsuo Handa 	struct kthread_create_info *create = kmalloc(sizeof(*create),
388786235eeSTetsuo Handa 						     GFP_KERNEL);
3891da177e4SLinus Torvalds 
390786235eeSTetsuo Handa 	if (!create)
391786235eeSTetsuo Handa 		return ERR_PTR(-ENOMEM);
392786235eeSTetsuo Handa 	create->threadfn = threadfn;
393786235eeSTetsuo Handa 	create->data = data;
394786235eeSTetsuo Handa 	create->node = node;
395786235eeSTetsuo Handa 	create->done = &done;
3961da177e4SLinus Torvalds 
39773c27992SEric W. Biederman 	spin_lock(&kthread_create_lock);
398786235eeSTetsuo Handa 	list_add_tail(&create->list, &kthread_create_list);
39973c27992SEric W. Biederman 	spin_unlock(&kthread_create_lock);
40073c27992SEric W. Biederman 
401cbd9b67bSDmitry Adamushko 	wake_up_process(kthreadd_task);
402786235eeSTetsuo Handa 	/*
403786235eeSTetsuo Handa 	 * Wait for completion in killable state, for I might be chosen by
404786235eeSTetsuo Handa 	 * the OOM killer while kthreadd is trying to allocate memory for
405786235eeSTetsuo Handa 	 * new kernel thread.
406786235eeSTetsuo Handa 	 */
407786235eeSTetsuo Handa 	if (unlikely(wait_for_completion_killable(&done))) {
408786235eeSTetsuo Handa 		/*
409786235eeSTetsuo Handa 		 * If I was SIGKILLed before kthreadd (or new kernel thread)
410786235eeSTetsuo Handa 		 * calls complete(), leave the cleanup of this structure to
411786235eeSTetsuo Handa 		 * that thread.
412786235eeSTetsuo Handa 		 */
413786235eeSTetsuo Handa 		if (xchg(&create->done, NULL))
4148fe6929cSTetsuo Handa 			return ERR_PTR(-EINTR);
415786235eeSTetsuo Handa 		/*
416786235eeSTetsuo Handa 		 * kthreadd (or new kernel thread) will call complete()
417786235eeSTetsuo Handa 		 * shortly.
418786235eeSTetsuo Handa 		 */
419786235eeSTetsuo Handa 		wait_for_completion(&done);
420786235eeSTetsuo Handa 	}
421786235eeSTetsuo Handa 	task = create->result;
422786235eeSTetsuo Handa 	if (!IS_ERR(task)) {
4233e536e22SSnild Dolkow 		char name[TASK_COMM_LEN];
4241c99315bSOleg Nesterov 
4253e536e22SSnild Dolkow 		/*
4263e536e22SSnild Dolkow 		 * task is already visible to other tasks, so updating
4273e536e22SSnild Dolkow 		 * COMM must be protected.
4283e536e22SSnild Dolkow 		 */
4293e536e22SSnild Dolkow 		vsnprintf(name, sizeof(name), namefmt, args);
4303e536e22SSnild Dolkow 		set_task_comm(task, name);
4311da177e4SLinus Torvalds 	}
432786235eeSTetsuo Handa 	kfree(create);
433786235eeSTetsuo Handa 	return task;
4341da177e4SLinus Torvalds }
435255451e4SPetr Mladek 
436255451e4SPetr Mladek /**
437255451e4SPetr Mladek  * kthread_create_on_node - create a kthread.
438255451e4SPetr Mladek  * @threadfn: the function to run until signal_pending(current).
439255451e4SPetr Mladek  * @data: data ptr for @threadfn.
440255451e4SPetr Mladek  * @node: task and thread structures for the thread are allocated on this node
441255451e4SPetr Mladek  * @namefmt: printf-style name for the thread.
442255451e4SPetr Mladek  *
443255451e4SPetr Mladek  * Description: This helper function creates and names a kernel
444255451e4SPetr Mladek  * thread.  The thread will be stopped: use wake_up_process() to start
445255451e4SPetr Mladek  * it.  See also kthread_run().  The new thread has SCHED_NORMAL policy and
446255451e4SPetr Mladek  * is affine to all CPUs.
447255451e4SPetr Mladek  *
448255451e4SPetr Mladek  * If thread is going to be bound on a particular cpu, give its node
449255451e4SPetr Mladek  * in @node, to get NUMA affinity for kthread stack, or else give NUMA_NO_NODE.
450255451e4SPetr Mladek  * When woken, the thread will run @threadfn() with @data as its
451111e7049SEric W. Biederman  * argument. @threadfn() can either return directly if it is a
452255451e4SPetr Mladek  * standalone thread for which no one will call kthread_stop(), or
453255451e4SPetr Mladek  * return when 'kthread_should_stop()' is true (which means
454255451e4SPetr Mladek  * kthread_stop() has been called).  The return value should be zero
455255451e4SPetr Mladek  * or a negative error number; it will be passed to kthread_stop().
456255451e4SPetr Mladek  *
457255451e4SPetr Mladek  * Returns a task_struct or ERR_PTR(-ENOMEM) or ERR_PTR(-EINTR).
458255451e4SPetr Mladek  */
459255451e4SPetr Mladek struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
460255451e4SPetr Mladek 					   void *data, int node,
461255451e4SPetr Mladek 					   const char namefmt[],
462255451e4SPetr Mladek 					   ...)
463255451e4SPetr Mladek {
464255451e4SPetr Mladek 	struct task_struct *task;
465255451e4SPetr Mladek 	va_list args;
466255451e4SPetr Mladek 
467255451e4SPetr Mladek 	va_start(args, namefmt);
468255451e4SPetr Mladek 	task = __kthread_create_on_node(threadfn, data, node, namefmt, args);
469255451e4SPetr Mladek 	va_end(args);
470255451e4SPetr Mladek 
471255451e4SPetr Mladek 	return task;
472255451e4SPetr Mladek }
473207205a2SEric Dumazet EXPORT_SYMBOL(kthread_create_on_node);
4741da177e4SLinus Torvalds 
4752f064a59SPeter Zijlstra static void __kthread_bind_mask(struct task_struct *p, const struct cpumask *mask, unsigned int state)
4762a1d4460SThomas Gleixner {
47725834c73SPeter Zijlstra 	unsigned long flags;
47825834c73SPeter Zijlstra 
479f2530dc7SThomas Gleixner 	if (!wait_task_inactive(p, state)) {
480f2530dc7SThomas Gleixner 		WARN_ON(1);
481f2530dc7SThomas Gleixner 		return;
482f2530dc7SThomas Gleixner 	}
48325834c73SPeter Zijlstra 
4842a1d4460SThomas Gleixner 	/* It's safe because the task is inactive. */
48525834c73SPeter Zijlstra 	raw_spin_lock_irqsave(&p->pi_lock, flags);
48625834c73SPeter Zijlstra 	do_set_cpus_allowed(p, mask);
48714a40ffcSTejun Heo 	p->flags |= PF_NO_SETAFFINITY;
48825834c73SPeter Zijlstra 	raw_spin_unlock_irqrestore(&p->pi_lock, flags);
48925834c73SPeter Zijlstra }
49025834c73SPeter Zijlstra 
4912f064a59SPeter Zijlstra static void __kthread_bind(struct task_struct *p, unsigned int cpu, unsigned int state)
49225834c73SPeter Zijlstra {
49325834c73SPeter Zijlstra 	__kthread_bind_mask(p, cpumask_of(cpu), state);
49425834c73SPeter Zijlstra }
49525834c73SPeter Zijlstra 
49625834c73SPeter Zijlstra void kthread_bind_mask(struct task_struct *p, const struct cpumask *mask)
49725834c73SPeter Zijlstra {
49825834c73SPeter Zijlstra 	__kthread_bind_mask(p, mask, TASK_UNINTERRUPTIBLE);
4992a1d4460SThomas Gleixner }
5002a1d4460SThomas Gleixner 
5019e37bd30SRandy Dunlap /**
502881232b7SPeter Zijlstra  * kthread_bind - bind a just-created kthread to a cpu.
503881232b7SPeter Zijlstra  * @p: thread created by kthread_create().
504881232b7SPeter Zijlstra  * @cpu: cpu (might not be online, must be possible) for @k to run on.
505881232b7SPeter Zijlstra  *
506881232b7SPeter Zijlstra  * Description: This function is equivalent to set_cpus_allowed(),
507881232b7SPeter Zijlstra  * except that @cpu doesn't need to be online, and the thread must be
508881232b7SPeter Zijlstra  * stopped (i.e., just returned from kthread_create()).
509881232b7SPeter Zijlstra  */
510881232b7SPeter Zijlstra void kthread_bind(struct task_struct *p, unsigned int cpu)
511881232b7SPeter Zijlstra {
512f2530dc7SThomas Gleixner 	__kthread_bind(p, cpu, TASK_UNINTERRUPTIBLE);
513881232b7SPeter Zijlstra }
514881232b7SPeter Zijlstra EXPORT_SYMBOL(kthread_bind);
515881232b7SPeter Zijlstra 
516881232b7SPeter Zijlstra /**
5172a1d4460SThomas Gleixner  * kthread_create_on_cpu - Create a cpu bound kthread
5182a1d4460SThomas Gleixner  * @threadfn: the function to run until signal_pending(current).
5192a1d4460SThomas Gleixner  * @data: data ptr for @threadfn.
5202a1d4460SThomas Gleixner  * @cpu: The cpu on which the thread should be bound,
5212a1d4460SThomas Gleixner  * @namefmt: printf-style name for the thread. Format is restricted
5222a1d4460SThomas Gleixner  *	     to "name.*%u". Code fills in cpu number.
5232a1d4460SThomas Gleixner  *
5242a1d4460SThomas Gleixner  * Description: This helper function creates and names a kernel thread
5252a1d4460SThomas Gleixner  */
5262a1d4460SThomas Gleixner struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
5272a1d4460SThomas Gleixner 					  void *data, unsigned int cpu,
5282a1d4460SThomas Gleixner 					  const char *namefmt)
5292a1d4460SThomas Gleixner {
5302a1d4460SThomas Gleixner 	struct task_struct *p;
5312a1d4460SThomas Gleixner 
53210922838SNishanth Aravamudan 	p = kthread_create_on_node(threadfn, data, cpu_to_node(cpu), namefmt,
5332a1d4460SThomas Gleixner 				   cpu);
5342a1d4460SThomas Gleixner 	if (IS_ERR(p))
5352a1d4460SThomas Gleixner 		return p;
536a65d4096SPetr Mladek 	kthread_bind(p, cpu);
537a65d4096SPetr Mladek 	/* CPU hotplug need to bind once again when unparking the thread. */
5382a1d4460SThomas Gleixner 	to_kthread(p)->cpu = cpu;
5392a1d4460SThomas Gleixner 	return p;
5402a1d4460SThomas Gleixner }
5412a1d4460SThomas Gleixner 
542ac687e6eSPeter Zijlstra void kthread_set_per_cpu(struct task_struct *k, int cpu)
543ac687e6eSPeter Zijlstra {
544ac687e6eSPeter Zijlstra 	struct kthread *kthread = to_kthread(k);
545ac687e6eSPeter Zijlstra 	if (!kthread)
546ac687e6eSPeter Zijlstra 		return;
547ac687e6eSPeter Zijlstra 
548ac687e6eSPeter Zijlstra 	WARN_ON_ONCE(!(k->flags & PF_NO_SETAFFINITY));
549ac687e6eSPeter Zijlstra 
550ac687e6eSPeter Zijlstra 	if (cpu < 0) {
551ac687e6eSPeter Zijlstra 		clear_bit(KTHREAD_IS_PER_CPU, &kthread->flags);
552ac687e6eSPeter Zijlstra 		return;
553ac687e6eSPeter Zijlstra 	}
554ac687e6eSPeter Zijlstra 
555ac687e6eSPeter Zijlstra 	kthread->cpu = cpu;
556ac687e6eSPeter Zijlstra 	set_bit(KTHREAD_IS_PER_CPU, &kthread->flags);
557ac687e6eSPeter Zijlstra }
558ac687e6eSPeter Zijlstra 
5593a7956e2SPeter Zijlstra bool kthread_is_per_cpu(struct task_struct *p)
560ac687e6eSPeter Zijlstra {
5613a7956e2SPeter Zijlstra 	struct kthread *kthread = __to_kthread(p);
562ac687e6eSPeter Zijlstra 	if (!kthread)
563ac687e6eSPeter Zijlstra 		return false;
564ac687e6eSPeter Zijlstra 
565ac687e6eSPeter Zijlstra 	return test_bit(KTHREAD_IS_PER_CPU, &kthread->flags);
566ac687e6eSPeter Zijlstra }
567ac687e6eSPeter Zijlstra 
568cf380a4aSOleg Nesterov /**
569cf380a4aSOleg Nesterov  * kthread_unpark - unpark a thread created by kthread_create().
570cf380a4aSOleg Nesterov  * @k:		thread created by kthread_create().
571cf380a4aSOleg Nesterov  *
572cf380a4aSOleg Nesterov  * Sets kthread_should_park() for @k to return false, wakes it, and
573cf380a4aSOleg Nesterov  * waits for it to return. If the thread is marked percpu then its
574cf380a4aSOleg Nesterov  * bound to the cpu again.
575cf380a4aSOleg Nesterov  */
576cf380a4aSOleg Nesterov void kthread_unpark(struct task_struct *k)
577f2530dc7SThomas Gleixner {
578cf380a4aSOleg Nesterov 	struct kthread *kthread = to_kthread(k);
579cf380a4aSOleg Nesterov 
580a65d4096SPetr Mladek 	/*
581a65d4096SPetr Mladek 	 * Newly created kthread was parked when the CPU was offline.
582a65d4096SPetr Mladek 	 * The binding was lost and we need to set it again.
583a65d4096SPetr Mladek 	 */
584f2530dc7SThomas Gleixner 	if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags))
585f2530dc7SThomas Gleixner 		__kthread_bind(k, kthread->cpu, TASK_PARKED);
58685f1abe0SPeter Zijlstra 
58785f1abe0SPeter Zijlstra 	clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
5881cef1150SPeter Zijlstra 	/*
5891cef1150SPeter Zijlstra 	 * __kthread_parkme() will either see !SHOULD_PARK or get the wakeup.
5901cef1150SPeter Zijlstra 	 */
591f2530dc7SThomas Gleixner 	wake_up_state(k, TASK_PARKED);
592f2530dc7SThomas Gleixner }
59318896451SDavid Kershner EXPORT_SYMBOL_GPL(kthread_unpark);
5942a1d4460SThomas Gleixner 
5952a1d4460SThomas Gleixner /**
5962a1d4460SThomas Gleixner  * kthread_park - park a thread created by kthread_create().
5972a1d4460SThomas Gleixner  * @k: thread created by kthread_create().
5982a1d4460SThomas Gleixner  *
5992a1d4460SThomas Gleixner  * Sets kthread_should_park() for @k to return true, wakes it, and
6002a1d4460SThomas Gleixner  * waits for it to return. This can also be called after kthread_create()
6012a1d4460SThomas Gleixner  * instead of calling wake_up_process(): the thread will park without
6022a1d4460SThomas Gleixner  * calling threadfn().
6032a1d4460SThomas Gleixner  *
6042a1d4460SThomas Gleixner  * Returns 0 if the thread is parked, -ENOSYS if the thread exited.
6052a1d4460SThomas Gleixner  * If called by the kthread itself just the park bit is set.
6062a1d4460SThomas Gleixner  */
6072a1d4460SThomas Gleixner int kthread_park(struct task_struct *k)
6082a1d4460SThomas Gleixner {
609cf380a4aSOleg Nesterov 	struct kthread *kthread = to_kthread(k);
6102a1d4460SThomas Gleixner 
611cf380a4aSOleg Nesterov 	if (WARN_ON(k->flags & PF_EXITING))
612cf380a4aSOleg Nesterov 		return -ENOSYS;
613cf380a4aSOleg Nesterov 
614f83ee19bSPeter Zijlstra 	if (WARN_ON_ONCE(test_bit(KTHREAD_SHOULD_PARK, &kthread->flags)))
615f83ee19bSPeter Zijlstra 		return -EBUSY;
616f83ee19bSPeter Zijlstra 
6172a1d4460SThomas Gleixner 	set_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
6182a1d4460SThomas Gleixner 	if (k != current) {
6192a1d4460SThomas Gleixner 		wake_up_process(k);
6201cef1150SPeter Zijlstra 		/*
6211cef1150SPeter Zijlstra 		 * Wait for __kthread_parkme() to complete(), this means we
6221cef1150SPeter Zijlstra 		 * _will_ have TASK_PARKED and are about to call schedule().
6231cef1150SPeter Zijlstra 		 */
6242a1d4460SThomas Gleixner 		wait_for_completion(&kthread->parked);
6251cef1150SPeter Zijlstra 		/*
6261cef1150SPeter Zijlstra 		 * Now wait for that schedule() to complete and the task to
6271cef1150SPeter Zijlstra 		 * get scheduled out.
6281cef1150SPeter Zijlstra 		 */
6291cef1150SPeter Zijlstra 		WARN_ON_ONCE(!wait_task_inactive(k, TASK_PARKED));
6302a1d4460SThomas Gleixner 	}
631cf380a4aSOleg Nesterov 
632cf380a4aSOleg Nesterov 	return 0;
6332a1d4460SThomas Gleixner }
63418896451SDavid Kershner EXPORT_SYMBOL_GPL(kthread_park);
6352a1d4460SThomas Gleixner 
6362a1d4460SThomas Gleixner /**
6379e37bd30SRandy Dunlap  * kthread_stop - stop a thread created by kthread_create().
6389e37bd30SRandy Dunlap  * @k: thread created by kthread_create().
6399e37bd30SRandy Dunlap  *
6409e37bd30SRandy Dunlap  * Sets kthread_should_stop() for @k to return true, wakes it, and
6419ae26027SOleg Nesterov  * waits for it to exit. This can also be called after kthread_create()
6429ae26027SOleg Nesterov  * instead of calling wake_up_process(): the thread will exit without
6439ae26027SOleg Nesterov  * calling threadfn().
6449ae26027SOleg Nesterov  *
645*bbda86e9SEric W. Biederman  * If threadfn() may call kthread_exit() itself, the caller must ensure
6469ae26027SOleg Nesterov  * task_struct can't go away.
6479e37bd30SRandy Dunlap  *
6489e37bd30SRandy Dunlap  * Returns the result of threadfn(), or %-EINTR if wake_up_process()
6499e37bd30SRandy Dunlap  * was never called.
6509e37bd30SRandy Dunlap  */
6511da177e4SLinus Torvalds int kthread_stop(struct task_struct *k)
6521da177e4SLinus Torvalds {
653b5c5442bSOleg Nesterov 	struct kthread *kthread;
6541da177e4SLinus Torvalds 	int ret;
6551da177e4SLinus Torvalds 
65663706172SOleg Nesterov 	trace_sched_kthread_stop(k);
657b5c5442bSOleg Nesterov 
658b5c5442bSOleg Nesterov 	get_task_struct(k);
659efb29fbfSOleg Nesterov 	kthread = to_kthread(k);
6602a1d4460SThomas Gleixner 	set_bit(KTHREAD_SHOULD_STOP, &kthread->flags);
661cf380a4aSOleg Nesterov 	kthread_unpark(k);
6621da177e4SLinus Torvalds 	wake_up_process(k);
66363706172SOleg Nesterov 	wait_for_completion(&kthread->exited);
66463706172SOleg Nesterov 	ret = k->exit_code;
6651da177e4SLinus Torvalds 	put_task_struct(k);
6660a16b607SMathieu Desnoyers 
667b5c5442bSOleg Nesterov 	trace_sched_kthread_stop_ret(ret);
6681da177e4SLinus Torvalds 	return ret;
6691da177e4SLinus Torvalds }
67052e92e57SAdrian Bunk EXPORT_SYMBOL(kthread_stop);
6711da177e4SLinus Torvalds 
672e804a4a4SSatyam Sharma int kthreadd(void *unused)
6731da177e4SLinus Torvalds {
67473c27992SEric W. Biederman 	struct task_struct *tsk = current;
67573c27992SEric W. Biederman 
676e804a4a4SSatyam Sharma 	/* Setup a clean context for our children to inherit. */
67773c27992SEric W. Biederman 	set_task_comm(tsk, "kthreadd");
67810ab825bSOleg Nesterov 	ignore_signals(tsk);
6799cc5b865SMarcelo Tosatti 	set_cpus_allowed_ptr(tsk, housekeeping_cpumask(HK_FLAG_KTHREAD));
680aee4faa4SLai Jiangshan 	set_mems_allowed(node_states[N_MEMORY]);
68173c27992SEric W. Biederman 
68234b087e4STejun Heo 	current->flags |= PF_NOFREEZE;
68377f88796STejun Heo 	cgroup_init_kthreadd();
68473c27992SEric W. Biederman 
68573c27992SEric W. Biederman 	for (;;) {
68673c27992SEric W. Biederman 		set_current_state(TASK_INTERRUPTIBLE);
68773c27992SEric W. Biederman 		if (list_empty(&kthread_create_list))
68873c27992SEric W. Biederman 			schedule();
68973c27992SEric W. Biederman 		__set_current_state(TASK_RUNNING);
69073c27992SEric W. Biederman 
69173c27992SEric W. Biederman 		spin_lock(&kthread_create_lock);
69273c27992SEric W. Biederman 		while (!list_empty(&kthread_create_list)) {
69373c27992SEric W. Biederman 			struct kthread_create_info *create;
69473c27992SEric W. Biederman 
69573c27992SEric W. Biederman 			create = list_entry(kthread_create_list.next,
69673c27992SEric W. Biederman 					    struct kthread_create_info, list);
69773c27992SEric W. Biederman 			list_del_init(&create->list);
69873c27992SEric W. Biederman 			spin_unlock(&kthread_create_lock);
69973c27992SEric W. Biederman 
70073c27992SEric W. Biederman 			create_kthread(create);
70173c27992SEric W. Biederman 
70273c27992SEric W. Biederman 			spin_lock(&kthread_create_lock);
70373c27992SEric W. Biederman 		}
70473c27992SEric W. Biederman 		spin_unlock(&kthread_create_lock);
70573c27992SEric W. Biederman 	}
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	return 0;
7081da177e4SLinus Torvalds }
709b56c0d89STejun Heo 
7103989144fSPetr Mladek void __kthread_init_worker(struct kthread_worker *worker,
7114f32e9b1SYong Zhang 				const char *name,
7124f32e9b1SYong Zhang 				struct lock_class_key *key)
7134f32e9b1SYong Zhang {
714dbf52682SPetr Mladek 	memset(worker, 0, sizeof(struct kthread_worker));
715fe99a4f4SJulia Cartwright 	raw_spin_lock_init(&worker->lock);
7164f32e9b1SYong Zhang 	lockdep_set_class_and_name(&worker->lock, key, name);
7174f32e9b1SYong Zhang 	INIT_LIST_HEAD(&worker->work_list);
71822597dc3SPetr Mladek 	INIT_LIST_HEAD(&worker->delayed_work_list);
7194f32e9b1SYong Zhang }
7203989144fSPetr Mladek EXPORT_SYMBOL_GPL(__kthread_init_worker);
7214f32e9b1SYong Zhang 
722b56c0d89STejun Heo /**
723b56c0d89STejun Heo  * kthread_worker_fn - kthread function to process kthread_worker
724b56c0d89STejun Heo  * @worker_ptr: pointer to initialized kthread_worker
725b56c0d89STejun Heo  *
726fbae2d44SPetr Mladek  * This function implements the main cycle of kthread worker. It processes
727fbae2d44SPetr Mladek  * work_list until it is stopped with kthread_stop(). It sleeps when the queue
728fbae2d44SPetr Mladek  * is empty.
729b56c0d89STejun Heo  *
730fbae2d44SPetr Mladek  * The works are not allowed to keep any locks, disable preemption or interrupts
731fbae2d44SPetr Mladek  * when they finish. There is defined a safe point for freezing when one work
732fbae2d44SPetr Mladek  * finishes and before a new one is started.
7338197b3d4SPetr Mladek  *
7348197b3d4SPetr Mladek  * Also the works must not be handled by more than one worker at the same time,
7358197b3d4SPetr Mladek  * see also kthread_queue_work().
736b56c0d89STejun Heo  */
737b56c0d89STejun Heo int kthread_worker_fn(void *worker_ptr)
738b56c0d89STejun Heo {
739b56c0d89STejun Heo 	struct kthread_worker *worker = worker_ptr;
740b56c0d89STejun Heo 	struct kthread_work *work;
741b56c0d89STejun Heo 
742fbae2d44SPetr Mladek 	/*
743fbae2d44SPetr Mladek 	 * FIXME: Update the check and remove the assignment when all kthread
744fbae2d44SPetr Mladek 	 * worker users are created using kthread_create_worker*() functions.
745fbae2d44SPetr Mladek 	 */
746fbae2d44SPetr Mladek 	WARN_ON(worker->task && worker->task != current);
747b56c0d89STejun Heo 	worker->task = current;
748dbf52682SPetr Mladek 
749dbf52682SPetr Mladek 	if (worker->flags & KTW_FREEZABLE)
750dbf52682SPetr Mladek 		set_freezable();
751dbf52682SPetr Mladek 
752b56c0d89STejun Heo repeat:
753b56c0d89STejun Heo 	set_current_state(TASK_INTERRUPTIBLE);	/* mb paired w/ kthread_stop */
754b56c0d89STejun Heo 
755b56c0d89STejun Heo 	if (kthread_should_stop()) {
756b56c0d89STejun Heo 		__set_current_state(TASK_RUNNING);
757fe99a4f4SJulia Cartwright 		raw_spin_lock_irq(&worker->lock);
758b56c0d89STejun Heo 		worker->task = NULL;
759fe99a4f4SJulia Cartwright 		raw_spin_unlock_irq(&worker->lock);
760b56c0d89STejun Heo 		return 0;
761b56c0d89STejun Heo 	}
762b56c0d89STejun Heo 
763b56c0d89STejun Heo 	work = NULL;
764fe99a4f4SJulia Cartwright 	raw_spin_lock_irq(&worker->lock);
765b56c0d89STejun Heo 	if (!list_empty(&worker->work_list)) {
766b56c0d89STejun Heo 		work = list_first_entry(&worker->work_list,
767b56c0d89STejun Heo 					struct kthread_work, node);
768b56c0d89STejun Heo 		list_del_init(&work->node);
769b56c0d89STejun Heo 	}
77046f3d976STejun Heo 	worker->current_work = work;
771fe99a4f4SJulia Cartwright 	raw_spin_unlock_irq(&worker->lock);
772b56c0d89STejun Heo 
773b56c0d89STejun Heo 	if (work) {
774f630c7c6SRob Clark 		kthread_work_func_t func = work->func;
775b56c0d89STejun Heo 		__set_current_state(TASK_RUNNING);
776f630c7c6SRob Clark 		trace_sched_kthread_work_execute_start(work);
777b56c0d89STejun Heo 		work->func(work);
778f630c7c6SRob Clark 		/*
779f630c7c6SRob Clark 		 * Avoid dereferencing work after this point.  The trace
780f630c7c6SRob Clark 		 * event only cares about the address.
781f630c7c6SRob Clark 		 */
782f630c7c6SRob Clark 		trace_sched_kthread_work_execute_end(work, func);
783b56c0d89STejun Heo 	} else if (!freezing(current))
784b56c0d89STejun Heo 		schedule();
785b56c0d89STejun Heo 
786b56c0d89STejun Heo 	try_to_freeze();
78722cf8bc6SShaohua Li 	cond_resched();
788b56c0d89STejun Heo 	goto repeat;
789b56c0d89STejun Heo }
790b56c0d89STejun Heo EXPORT_SYMBOL_GPL(kthread_worker_fn);
791b56c0d89STejun Heo 
792c0b942a7SNicolas Iooss static __printf(3, 0) struct kthread_worker *
793dbf52682SPetr Mladek __kthread_create_worker(int cpu, unsigned int flags,
794dbf52682SPetr Mladek 			const char namefmt[], va_list args)
795fbae2d44SPetr Mladek {
796fbae2d44SPetr Mladek 	struct kthread_worker *worker;
797fbae2d44SPetr Mladek 	struct task_struct *task;
79898fa15f3SAnshuman Khandual 	int node = NUMA_NO_NODE;
799fbae2d44SPetr Mladek 
800fbae2d44SPetr Mladek 	worker = kzalloc(sizeof(*worker), GFP_KERNEL);
801fbae2d44SPetr Mladek 	if (!worker)
802fbae2d44SPetr Mladek 		return ERR_PTR(-ENOMEM);
803fbae2d44SPetr Mladek 
804fbae2d44SPetr Mladek 	kthread_init_worker(worker);
805fbae2d44SPetr Mladek 
8068fb9dcbdSOleg Nesterov 	if (cpu >= 0)
8078fb9dcbdSOleg Nesterov 		node = cpu_to_node(cpu);
808fbae2d44SPetr Mladek 
809fbae2d44SPetr Mladek 	task = __kthread_create_on_node(kthread_worker_fn, worker,
8108fb9dcbdSOleg Nesterov 						node, namefmt, args);
811fbae2d44SPetr Mladek 	if (IS_ERR(task))
812fbae2d44SPetr Mladek 		goto fail_task;
813fbae2d44SPetr Mladek 
8148fb9dcbdSOleg Nesterov 	if (cpu >= 0)
8158fb9dcbdSOleg Nesterov 		kthread_bind(task, cpu);
8168fb9dcbdSOleg Nesterov 
817dbf52682SPetr Mladek 	worker->flags = flags;
818fbae2d44SPetr Mladek 	worker->task = task;
819fbae2d44SPetr Mladek 	wake_up_process(task);
820fbae2d44SPetr Mladek 	return worker;
821fbae2d44SPetr Mladek 
822fbae2d44SPetr Mladek fail_task:
823fbae2d44SPetr Mladek 	kfree(worker);
824fbae2d44SPetr Mladek 	return ERR_CAST(task);
825fbae2d44SPetr Mladek }
826fbae2d44SPetr Mladek 
827fbae2d44SPetr Mladek /**
828fbae2d44SPetr Mladek  * kthread_create_worker - create a kthread worker
829dbf52682SPetr Mladek  * @flags: flags modifying the default behavior of the worker
830fbae2d44SPetr Mladek  * @namefmt: printf-style name for the kthread worker (task).
831fbae2d44SPetr Mladek  *
832fbae2d44SPetr Mladek  * Returns a pointer to the allocated worker on success, ERR_PTR(-ENOMEM)
833fbae2d44SPetr Mladek  * when the needed structures could not get allocated, and ERR_PTR(-EINTR)
834fbae2d44SPetr Mladek  * when the worker was SIGKILLed.
835fbae2d44SPetr Mladek  */
836fbae2d44SPetr Mladek struct kthread_worker *
837dbf52682SPetr Mladek kthread_create_worker(unsigned int flags, const char namefmt[], ...)
838fbae2d44SPetr Mladek {
839fbae2d44SPetr Mladek 	struct kthread_worker *worker;
840fbae2d44SPetr Mladek 	va_list args;
841fbae2d44SPetr Mladek 
842fbae2d44SPetr Mladek 	va_start(args, namefmt);
843dbf52682SPetr Mladek 	worker = __kthread_create_worker(-1, flags, namefmt, args);
844fbae2d44SPetr Mladek 	va_end(args);
845fbae2d44SPetr Mladek 
846fbae2d44SPetr Mladek 	return worker;
847fbae2d44SPetr Mladek }
848fbae2d44SPetr Mladek EXPORT_SYMBOL(kthread_create_worker);
849fbae2d44SPetr Mladek 
850fbae2d44SPetr Mladek /**
851fbae2d44SPetr Mladek  * kthread_create_worker_on_cpu - create a kthread worker and bind it
8527b7b8a2cSRandy Dunlap  *	to a given CPU and the associated NUMA node.
853fbae2d44SPetr Mladek  * @cpu: CPU number
854dbf52682SPetr Mladek  * @flags: flags modifying the default behavior of the worker
855fbae2d44SPetr Mladek  * @namefmt: printf-style name for the kthread worker (task).
856fbae2d44SPetr Mladek  *
857fbae2d44SPetr Mladek  * Use a valid CPU number if you want to bind the kthread worker
858fbae2d44SPetr Mladek  * to the given CPU and the associated NUMA node.
859fbae2d44SPetr Mladek  *
860fbae2d44SPetr Mladek  * A good practice is to add the cpu number also into the worker name.
861fbae2d44SPetr Mladek  * For example, use kthread_create_worker_on_cpu(cpu, "helper/%d", cpu).
862fbae2d44SPetr Mladek  *
863ebb2bdceSPetr Mladek  * CPU hotplug:
864ebb2bdceSPetr Mladek  * The kthread worker API is simple and generic. It just provides a way
865ebb2bdceSPetr Mladek  * to create, use, and destroy workers.
866ebb2bdceSPetr Mladek  *
867ebb2bdceSPetr Mladek  * It is up to the API user how to handle CPU hotplug. They have to decide
868ebb2bdceSPetr Mladek  * how to handle pending work items, prevent queuing new ones, and
869ebb2bdceSPetr Mladek  * restore the functionality when the CPU goes off and on. There are a
870ebb2bdceSPetr Mladek  * few catches:
871ebb2bdceSPetr Mladek  *
872ebb2bdceSPetr Mladek  *    - CPU affinity gets lost when it is scheduled on an offline CPU.
873ebb2bdceSPetr Mladek  *
874ebb2bdceSPetr Mladek  *    - The worker might not exist when the CPU was off when the user
875ebb2bdceSPetr Mladek  *      created the workers.
876ebb2bdceSPetr Mladek  *
877ebb2bdceSPetr Mladek  * Good practice is to implement two CPU hotplug callbacks and to
878ebb2bdceSPetr Mladek  * destroy/create the worker when the CPU goes down/up.
879ebb2bdceSPetr Mladek  *
880ebb2bdceSPetr Mladek  * Return:
881ebb2bdceSPetr Mladek  * The pointer to the allocated worker on success, ERR_PTR(-ENOMEM)
882fbae2d44SPetr Mladek  * when the needed structures could not get allocated, and ERR_PTR(-EINTR)
883fbae2d44SPetr Mladek  * when the worker was SIGKILLed.
884fbae2d44SPetr Mladek  */
885fbae2d44SPetr Mladek struct kthread_worker *
886dbf52682SPetr Mladek kthread_create_worker_on_cpu(int cpu, unsigned int flags,
887dbf52682SPetr Mladek 			     const char namefmt[], ...)
888fbae2d44SPetr Mladek {
889fbae2d44SPetr Mladek 	struct kthread_worker *worker;
890fbae2d44SPetr Mladek 	va_list args;
891fbae2d44SPetr Mladek 
892fbae2d44SPetr Mladek 	va_start(args, namefmt);
893dbf52682SPetr Mladek 	worker = __kthread_create_worker(cpu, flags, namefmt, args);
894fbae2d44SPetr Mladek 	va_end(args);
895fbae2d44SPetr Mladek 
896fbae2d44SPetr Mladek 	return worker;
897fbae2d44SPetr Mladek }
898fbae2d44SPetr Mladek EXPORT_SYMBOL(kthread_create_worker_on_cpu);
899fbae2d44SPetr Mladek 
90037be45d4SPetr Mladek /*
90137be45d4SPetr Mladek  * Returns true when the work could not be queued at the moment.
90237be45d4SPetr Mladek  * It happens when it is already pending in a worker list
90337be45d4SPetr Mladek  * or when it is being cancelled.
90437be45d4SPetr Mladek  */
90537be45d4SPetr Mladek static inline bool queuing_blocked(struct kthread_worker *worker,
90637be45d4SPetr Mladek 				   struct kthread_work *work)
90737be45d4SPetr Mladek {
90837be45d4SPetr Mladek 	lockdep_assert_held(&worker->lock);
90937be45d4SPetr Mladek 
91037be45d4SPetr Mladek 	return !list_empty(&work->node) || work->canceling;
91137be45d4SPetr Mladek }
91237be45d4SPetr Mladek 
9138197b3d4SPetr Mladek static void kthread_insert_work_sanity_check(struct kthread_worker *worker,
9148197b3d4SPetr Mladek 					     struct kthread_work *work)
9158197b3d4SPetr Mladek {
9168197b3d4SPetr Mladek 	lockdep_assert_held(&worker->lock);
9178197b3d4SPetr Mladek 	WARN_ON_ONCE(!list_empty(&work->node));
9188197b3d4SPetr Mladek 	/* Do not use a work with >1 worker, see kthread_queue_work() */
9198197b3d4SPetr Mladek 	WARN_ON_ONCE(work->worker && work->worker != worker);
9208197b3d4SPetr Mladek }
9218197b3d4SPetr Mladek 
9229a2e03d8STejun Heo /* insert @work before @pos in @worker */
9233989144fSPetr Mladek static void kthread_insert_work(struct kthread_worker *worker,
9249a2e03d8STejun Heo 				struct kthread_work *work,
9259a2e03d8STejun Heo 				struct list_head *pos)
9269a2e03d8STejun Heo {
9278197b3d4SPetr Mladek 	kthread_insert_work_sanity_check(worker, work);
9289a2e03d8STejun Heo 
929f630c7c6SRob Clark 	trace_sched_kthread_work_queue_work(worker, work);
930f630c7c6SRob Clark 
9319a2e03d8STejun Heo 	list_add_tail(&work->node, pos);
93246f3d976STejun Heo 	work->worker = worker;
933ed1403ecSLai Jiangshan 	if (!worker->current_work && likely(worker->task))
9349a2e03d8STejun Heo 		wake_up_process(worker->task);
9359a2e03d8STejun Heo }
9369a2e03d8STejun Heo 
937b56c0d89STejun Heo /**
9383989144fSPetr Mladek  * kthread_queue_work - queue a kthread_work
939b56c0d89STejun Heo  * @worker: target kthread_worker
940b56c0d89STejun Heo  * @work: kthread_work to queue
941b56c0d89STejun Heo  *
942b56c0d89STejun Heo  * Queue @work to work processor @task for async execution.  @task
943b56c0d89STejun Heo  * must have been created with kthread_worker_create().  Returns %true
944b56c0d89STejun Heo  * if @work was successfully queued, %false if it was already pending.
9458197b3d4SPetr Mladek  *
9468197b3d4SPetr Mladek  * Reinitialize the work if it needs to be used by another worker.
9478197b3d4SPetr Mladek  * For example, when the worker was stopped and started again.
948b56c0d89STejun Heo  */
9493989144fSPetr Mladek bool kthread_queue_work(struct kthread_worker *worker,
950b56c0d89STejun Heo 			struct kthread_work *work)
951b56c0d89STejun Heo {
952b56c0d89STejun Heo 	bool ret = false;
953b56c0d89STejun Heo 	unsigned long flags;
954b56c0d89STejun Heo 
955fe99a4f4SJulia Cartwright 	raw_spin_lock_irqsave(&worker->lock, flags);
95637be45d4SPetr Mladek 	if (!queuing_blocked(worker, work)) {
9573989144fSPetr Mladek 		kthread_insert_work(worker, work, &worker->work_list);
958b56c0d89STejun Heo 		ret = true;
959b56c0d89STejun Heo 	}
960fe99a4f4SJulia Cartwright 	raw_spin_unlock_irqrestore(&worker->lock, flags);
961b56c0d89STejun Heo 	return ret;
962b56c0d89STejun Heo }
9633989144fSPetr Mladek EXPORT_SYMBOL_GPL(kthread_queue_work);
964b56c0d89STejun Heo 
96522597dc3SPetr Mladek /**
96622597dc3SPetr Mladek  * kthread_delayed_work_timer_fn - callback that queues the associated kthread
96722597dc3SPetr Mladek  *	delayed work when the timer expires.
968fe5c3b69SKees Cook  * @t: pointer to the expired timer
96922597dc3SPetr Mladek  *
97022597dc3SPetr Mladek  * The format of the function is defined by struct timer_list.
97122597dc3SPetr Mladek  * It should have been called from irqsafe timer with irq already off.
97222597dc3SPetr Mladek  */
973fe5c3b69SKees Cook void kthread_delayed_work_timer_fn(struct timer_list *t)
97422597dc3SPetr Mladek {
975fe5c3b69SKees Cook 	struct kthread_delayed_work *dwork = from_timer(dwork, t, timer);
97622597dc3SPetr Mladek 	struct kthread_work *work = &dwork->work;
97722597dc3SPetr Mladek 	struct kthread_worker *worker = work->worker;
978ad01423aSSebastian Andrzej Siewior 	unsigned long flags;
97922597dc3SPetr Mladek 
98022597dc3SPetr Mladek 	/*
98122597dc3SPetr Mladek 	 * This might happen when a pending work is reinitialized.
98222597dc3SPetr Mladek 	 * It means that it is used a wrong way.
98322597dc3SPetr Mladek 	 */
98422597dc3SPetr Mladek 	if (WARN_ON_ONCE(!worker))
98522597dc3SPetr Mladek 		return;
98622597dc3SPetr Mladek 
987ad01423aSSebastian Andrzej Siewior 	raw_spin_lock_irqsave(&worker->lock, flags);
98822597dc3SPetr Mladek 	/* Work must not be used with >1 worker, see kthread_queue_work(). */
98922597dc3SPetr Mladek 	WARN_ON_ONCE(work->worker != worker);
99022597dc3SPetr Mladek 
99122597dc3SPetr Mladek 	/* Move the work from worker->delayed_work_list. */
99222597dc3SPetr Mladek 	WARN_ON_ONCE(list_empty(&work->node));
99322597dc3SPetr Mladek 	list_del_init(&work->node);
9946993d0fdSZqiang 	if (!work->canceling)
99522597dc3SPetr Mladek 		kthread_insert_work(worker, work, &worker->work_list);
99622597dc3SPetr Mladek 
997ad01423aSSebastian Andrzej Siewior 	raw_spin_unlock_irqrestore(&worker->lock, flags);
99822597dc3SPetr Mladek }
99922597dc3SPetr Mladek EXPORT_SYMBOL(kthread_delayed_work_timer_fn);
100022597dc3SPetr Mladek 
1001bc88f85cSBen Dooks static void __kthread_queue_delayed_work(struct kthread_worker *worker,
100222597dc3SPetr Mladek 					 struct kthread_delayed_work *dwork,
100322597dc3SPetr Mladek 					 unsigned long delay)
100422597dc3SPetr Mladek {
100522597dc3SPetr Mladek 	struct timer_list *timer = &dwork->timer;
100622597dc3SPetr Mladek 	struct kthread_work *work = &dwork->work;
100722597dc3SPetr Mladek 
10080a5b4128SSami Tolvanen 	WARN_ON_FUNCTION_MISMATCH(timer->function,
10090a5b4128SSami Tolvanen 				  kthread_delayed_work_timer_fn);
101022597dc3SPetr Mladek 
101122597dc3SPetr Mladek 	/*
101222597dc3SPetr Mladek 	 * If @delay is 0, queue @dwork->work immediately.  This is for
101322597dc3SPetr Mladek 	 * both optimization and correctness.  The earliest @timer can
101422597dc3SPetr Mladek 	 * expire is on the closest next tick and delayed_work users depend
101522597dc3SPetr Mladek 	 * on that there's no such delay when @delay is 0.
101622597dc3SPetr Mladek 	 */
101722597dc3SPetr Mladek 	if (!delay) {
101822597dc3SPetr Mladek 		kthread_insert_work(worker, work, &worker->work_list);
101922597dc3SPetr Mladek 		return;
102022597dc3SPetr Mladek 	}
102122597dc3SPetr Mladek 
102222597dc3SPetr Mladek 	/* Be paranoid and try to detect possible races already now. */
102322597dc3SPetr Mladek 	kthread_insert_work_sanity_check(worker, work);
102422597dc3SPetr Mladek 
102522597dc3SPetr Mladek 	list_add(&work->node, &worker->delayed_work_list);
102622597dc3SPetr Mladek 	work->worker = worker;
102722597dc3SPetr Mladek 	timer->expires = jiffies + delay;
102822597dc3SPetr Mladek 	add_timer(timer);
102922597dc3SPetr Mladek }
103022597dc3SPetr Mladek 
103122597dc3SPetr Mladek /**
103222597dc3SPetr Mladek  * kthread_queue_delayed_work - queue the associated kthread work
103322597dc3SPetr Mladek  *	after a delay.
103422597dc3SPetr Mladek  * @worker: target kthread_worker
103522597dc3SPetr Mladek  * @dwork: kthread_delayed_work to queue
103622597dc3SPetr Mladek  * @delay: number of jiffies to wait before queuing
103722597dc3SPetr Mladek  *
103822597dc3SPetr Mladek  * If the work has not been pending it starts a timer that will queue
103922597dc3SPetr Mladek  * the work after the given @delay. If @delay is zero, it queues the
104022597dc3SPetr Mladek  * work immediately.
104122597dc3SPetr Mladek  *
104222597dc3SPetr Mladek  * Return: %false if the @work has already been pending. It means that
104322597dc3SPetr Mladek  * either the timer was running or the work was queued. It returns %true
104422597dc3SPetr Mladek  * otherwise.
104522597dc3SPetr Mladek  */
104622597dc3SPetr Mladek bool kthread_queue_delayed_work(struct kthread_worker *worker,
104722597dc3SPetr Mladek 				struct kthread_delayed_work *dwork,
104822597dc3SPetr Mladek 				unsigned long delay)
104922597dc3SPetr Mladek {
105022597dc3SPetr Mladek 	struct kthread_work *work = &dwork->work;
105122597dc3SPetr Mladek 	unsigned long flags;
105222597dc3SPetr Mladek 	bool ret = false;
105322597dc3SPetr Mladek 
1054fe99a4f4SJulia Cartwright 	raw_spin_lock_irqsave(&worker->lock, flags);
105522597dc3SPetr Mladek 
105637be45d4SPetr Mladek 	if (!queuing_blocked(worker, work)) {
105722597dc3SPetr Mladek 		__kthread_queue_delayed_work(worker, dwork, delay);
105822597dc3SPetr Mladek 		ret = true;
105922597dc3SPetr Mladek 	}
106022597dc3SPetr Mladek 
1061fe99a4f4SJulia Cartwright 	raw_spin_unlock_irqrestore(&worker->lock, flags);
106222597dc3SPetr Mladek 	return ret;
106322597dc3SPetr Mladek }
106422597dc3SPetr Mladek EXPORT_SYMBOL_GPL(kthread_queue_delayed_work);
106522597dc3SPetr Mladek 
10669a2e03d8STejun Heo struct kthread_flush_work {
10679a2e03d8STejun Heo 	struct kthread_work	work;
10689a2e03d8STejun Heo 	struct completion	done;
10699a2e03d8STejun Heo };
10709a2e03d8STejun Heo 
10719a2e03d8STejun Heo static void kthread_flush_work_fn(struct kthread_work *work)
10729a2e03d8STejun Heo {
10739a2e03d8STejun Heo 	struct kthread_flush_work *fwork =
10749a2e03d8STejun Heo 		container_of(work, struct kthread_flush_work, work);
10759a2e03d8STejun Heo 	complete(&fwork->done);
10769a2e03d8STejun Heo }
10779a2e03d8STejun Heo 
1078b56c0d89STejun Heo /**
10793989144fSPetr Mladek  * kthread_flush_work - flush a kthread_work
1080b56c0d89STejun Heo  * @work: work to flush
1081b56c0d89STejun Heo  *
1082b56c0d89STejun Heo  * If @work is queued or executing, wait for it to finish execution.
1083b56c0d89STejun Heo  */
10843989144fSPetr Mladek void kthread_flush_work(struct kthread_work *work)
1085b56c0d89STejun Heo {
108646f3d976STejun Heo 	struct kthread_flush_work fwork = {
108746f3d976STejun Heo 		KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
108846f3d976STejun Heo 		COMPLETION_INITIALIZER_ONSTACK(fwork.done),
108946f3d976STejun Heo 	};
109046f3d976STejun Heo 	struct kthread_worker *worker;
109146f3d976STejun Heo 	bool noop = false;
1092b56c0d89STejun Heo 
109346f3d976STejun Heo 	worker = work->worker;
109446f3d976STejun Heo 	if (!worker)
109546f3d976STejun Heo 		return;
1096b56c0d89STejun Heo 
1097fe99a4f4SJulia Cartwright 	raw_spin_lock_irq(&worker->lock);
10988197b3d4SPetr Mladek 	/* Work must not be used with >1 worker, see kthread_queue_work(). */
10998197b3d4SPetr Mladek 	WARN_ON_ONCE(work->worker != worker);
1100b56c0d89STejun Heo 
110146f3d976STejun Heo 	if (!list_empty(&work->node))
11023989144fSPetr Mladek 		kthread_insert_work(worker, &fwork.work, work->node.next);
110346f3d976STejun Heo 	else if (worker->current_work == work)
11043989144fSPetr Mladek 		kthread_insert_work(worker, &fwork.work,
11053989144fSPetr Mladek 				    worker->work_list.next);
110646f3d976STejun Heo 	else
110746f3d976STejun Heo 		noop = true;
1108b56c0d89STejun Heo 
1109fe99a4f4SJulia Cartwright 	raw_spin_unlock_irq(&worker->lock);
111046f3d976STejun Heo 
111146f3d976STejun Heo 	if (!noop)
111246f3d976STejun Heo 		wait_for_completion(&fwork.done);
1113b56c0d89STejun Heo }
11143989144fSPetr Mladek EXPORT_SYMBOL_GPL(kthread_flush_work);
1115b56c0d89STejun Heo 
111637be45d4SPetr Mladek /*
111734b3d534SPetr Mladek  * Make sure that the timer is neither set nor running and could
111834b3d534SPetr Mladek  * not manipulate the work list_head any longer.
111937be45d4SPetr Mladek  *
112034b3d534SPetr Mladek  * The function is called under worker->lock. The lock is temporary
112134b3d534SPetr Mladek  * released but the timer can't be set again in the meantime.
112237be45d4SPetr Mladek  */
112334b3d534SPetr Mladek static void kthread_cancel_delayed_work_timer(struct kthread_work *work,
112437be45d4SPetr Mladek 					      unsigned long *flags)
112537be45d4SPetr Mladek {
112637be45d4SPetr Mladek 	struct kthread_delayed_work *dwork =
112737be45d4SPetr Mladek 		container_of(work, struct kthread_delayed_work, work);
112837be45d4SPetr Mladek 	struct kthread_worker *worker = work->worker;
112937be45d4SPetr Mladek 
113037be45d4SPetr Mladek 	/*
113137be45d4SPetr Mladek 	 * del_timer_sync() must be called to make sure that the timer
113237be45d4SPetr Mladek 	 * callback is not running. The lock must be temporary released
113337be45d4SPetr Mladek 	 * to avoid a deadlock with the callback. In the meantime,
113437be45d4SPetr Mladek 	 * any queuing is blocked by setting the canceling counter.
113537be45d4SPetr Mladek 	 */
113637be45d4SPetr Mladek 	work->canceling++;
1137fe99a4f4SJulia Cartwright 	raw_spin_unlock_irqrestore(&worker->lock, *flags);
113837be45d4SPetr Mladek 	del_timer_sync(&dwork->timer);
1139fe99a4f4SJulia Cartwright 	raw_spin_lock_irqsave(&worker->lock, *flags);
114037be45d4SPetr Mladek 	work->canceling--;
114137be45d4SPetr Mladek }
114237be45d4SPetr Mladek 
114337be45d4SPetr Mladek /*
11445fa54346SPetr Mladek  * This function removes the work from the worker queue.
11455fa54346SPetr Mladek  *
11465fa54346SPetr Mladek  * It is called under worker->lock. The caller must make sure that
11475fa54346SPetr Mladek  * the timer used by delayed work is not running, e.g. by calling
11485fa54346SPetr Mladek  * kthread_cancel_delayed_work_timer().
114934b3d534SPetr Mladek  *
115034b3d534SPetr Mladek  * The work might still be in use when this function finishes. See the
115134b3d534SPetr Mladek  * current_work proceed by the worker.
115234b3d534SPetr Mladek  *
115334b3d534SPetr Mladek  * Return: %true if @work was pending and successfully canceled,
115434b3d534SPetr Mladek  *	%false if @work was not pending
115534b3d534SPetr Mladek  */
11565fa54346SPetr Mladek static bool __kthread_cancel_work(struct kthread_work *work)
115734b3d534SPetr Mladek {
115834b3d534SPetr Mladek 	/*
115937be45d4SPetr Mladek 	 * Try to remove the work from a worker list. It might either
116037be45d4SPetr Mladek 	 * be from worker->work_list or from worker->delayed_work_list.
116137be45d4SPetr Mladek 	 */
116237be45d4SPetr Mladek 	if (!list_empty(&work->node)) {
116337be45d4SPetr Mladek 		list_del_init(&work->node);
116437be45d4SPetr Mladek 		return true;
116537be45d4SPetr Mladek 	}
116637be45d4SPetr Mladek 
116737be45d4SPetr Mladek 	return false;
116837be45d4SPetr Mladek }
116937be45d4SPetr Mladek 
11709a6b06c8SPetr Mladek /**
11719a6b06c8SPetr Mladek  * kthread_mod_delayed_work - modify delay of or queue a kthread delayed work
11729a6b06c8SPetr Mladek  * @worker: kthread worker to use
11739a6b06c8SPetr Mladek  * @dwork: kthread delayed work to queue
11749a6b06c8SPetr Mladek  * @delay: number of jiffies to wait before queuing
11759a6b06c8SPetr Mladek  *
11769a6b06c8SPetr Mladek  * If @dwork is idle, equivalent to kthread_queue_delayed_work(). Otherwise,
11779a6b06c8SPetr Mladek  * modify @dwork's timer so that it expires after @delay. If @delay is zero,
11789a6b06c8SPetr Mladek  * @work is guaranteed to be queued immediately.
11799a6b06c8SPetr Mladek  *
1180d71ba164SPetr Mladek  * Return: %false if @dwork was idle and queued, %true otherwise.
11819a6b06c8SPetr Mladek  *
11829a6b06c8SPetr Mladek  * A special case is when the work is being canceled in parallel.
11839a6b06c8SPetr Mladek  * It might be caused either by the real kthread_cancel_delayed_work_sync()
11849a6b06c8SPetr Mladek  * or yet another kthread_mod_delayed_work() call. We let the other command
1185d71ba164SPetr Mladek  * win and return %true here. The return value can be used for reference
1186d71ba164SPetr Mladek  * counting and the number of queued works stays the same. Anyway, the caller
1187d71ba164SPetr Mladek  * is supposed to synchronize these operations a reasonable way.
11889a6b06c8SPetr Mladek  *
11899a6b06c8SPetr Mladek  * This function is safe to call from any context including IRQ handler.
11909a6b06c8SPetr Mladek  * See __kthread_cancel_work() and kthread_delayed_work_timer_fn()
11919a6b06c8SPetr Mladek  * for details.
11929a6b06c8SPetr Mladek  */
11939a6b06c8SPetr Mladek bool kthread_mod_delayed_work(struct kthread_worker *worker,
11949a6b06c8SPetr Mladek 			      struct kthread_delayed_work *dwork,
11959a6b06c8SPetr Mladek 			      unsigned long delay)
11969a6b06c8SPetr Mladek {
11979a6b06c8SPetr Mladek 	struct kthread_work *work = &dwork->work;
11989a6b06c8SPetr Mladek 	unsigned long flags;
1199d71ba164SPetr Mladek 	int ret;
12009a6b06c8SPetr Mladek 
1201fe99a4f4SJulia Cartwright 	raw_spin_lock_irqsave(&worker->lock, flags);
12029a6b06c8SPetr Mladek 
12039a6b06c8SPetr Mladek 	/* Do not bother with canceling when never queued. */
1204d71ba164SPetr Mladek 	if (!work->worker) {
1205d71ba164SPetr Mladek 		ret = false;
12069a6b06c8SPetr Mladek 		goto fast_queue;
1207d71ba164SPetr Mladek 	}
12089a6b06c8SPetr Mladek 
12099a6b06c8SPetr Mladek 	/* Work must not be used with >1 worker, see kthread_queue_work() */
12109a6b06c8SPetr Mladek 	WARN_ON_ONCE(work->worker != worker);
12119a6b06c8SPetr Mladek 
12125fa54346SPetr Mladek 	/*
12135fa54346SPetr Mladek 	 * Temporary cancel the work but do not fight with another command
12145fa54346SPetr Mladek 	 * that is canceling the work as well.
12155fa54346SPetr Mladek 	 *
12165fa54346SPetr Mladek 	 * It is a bit tricky because of possible races with another
12175fa54346SPetr Mladek 	 * mod_delayed_work() and cancel_delayed_work() callers.
12185fa54346SPetr Mladek 	 *
12195fa54346SPetr Mladek 	 * The timer must be canceled first because worker->lock is released
12205fa54346SPetr Mladek 	 * when doing so. But the work can be removed from the queue (list)
12215fa54346SPetr Mladek 	 * only when it can be queued again so that the return value can
12225fa54346SPetr Mladek 	 * be used for reference counting.
12235fa54346SPetr Mladek 	 */
12245fa54346SPetr Mladek 	kthread_cancel_delayed_work_timer(work, &flags);
1225d71ba164SPetr Mladek 	if (work->canceling) {
1226d71ba164SPetr Mladek 		/* The number of works in the queue does not change. */
1227d71ba164SPetr Mladek 		ret = true;
12289a6b06c8SPetr Mladek 		goto out;
1229d71ba164SPetr Mladek 	}
12305fa54346SPetr Mladek 	ret = __kthread_cancel_work(work);
12319a6b06c8SPetr Mladek 
12329a6b06c8SPetr Mladek fast_queue:
12339a6b06c8SPetr Mladek 	__kthread_queue_delayed_work(worker, dwork, delay);
12349a6b06c8SPetr Mladek out:
1235fe99a4f4SJulia Cartwright 	raw_spin_unlock_irqrestore(&worker->lock, flags);
12369a6b06c8SPetr Mladek 	return ret;
12379a6b06c8SPetr Mladek }
12389a6b06c8SPetr Mladek EXPORT_SYMBOL_GPL(kthread_mod_delayed_work);
12399a6b06c8SPetr Mladek 
124037be45d4SPetr Mladek static bool __kthread_cancel_work_sync(struct kthread_work *work, bool is_dwork)
124137be45d4SPetr Mladek {
124237be45d4SPetr Mladek 	struct kthread_worker *worker = work->worker;
124337be45d4SPetr Mladek 	unsigned long flags;
124437be45d4SPetr Mladek 	int ret = false;
124537be45d4SPetr Mladek 
124637be45d4SPetr Mladek 	if (!worker)
124737be45d4SPetr Mladek 		goto out;
124837be45d4SPetr Mladek 
1249fe99a4f4SJulia Cartwright 	raw_spin_lock_irqsave(&worker->lock, flags);
125037be45d4SPetr Mladek 	/* Work must not be used with >1 worker, see kthread_queue_work(). */
125137be45d4SPetr Mladek 	WARN_ON_ONCE(work->worker != worker);
125237be45d4SPetr Mladek 
12535fa54346SPetr Mladek 	if (is_dwork)
12545fa54346SPetr Mladek 		kthread_cancel_delayed_work_timer(work, &flags);
12555fa54346SPetr Mladek 
12565fa54346SPetr Mladek 	ret = __kthread_cancel_work(work);
125737be45d4SPetr Mladek 
125837be45d4SPetr Mladek 	if (worker->current_work != work)
125937be45d4SPetr Mladek 		goto out_fast;
126037be45d4SPetr Mladek 
126137be45d4SPetr Mladek 	/*
126237be45d4SPetr Mladek 	 * The work is in progress and we need to wait with the lock released.
126337be45d4SPetr Mladek 	 * In the meantime, block any queuing by setting the canceling counter.
126437be45d4SPetr Mladek 	 */
126537be45d4SPetr Mladek 	work->canceling++;
1266fe99a4f4SJulia Cartwright 	raw_spin_unlock_irqrestore(&worker->lock, flags);
126737be45d4SPetr Mladek 	kthread_flush_work(work);
1268fe99a4f4SJulia Cartwright 	raw_spin_lock_irqsave(&worker->lock, flags);
126937be45d4SPetr Mladek 	work->canceling--;
127037be45d4SPetr Mladek 
127137be45d4SPetr Mladek out_fast:
1272fe99a4f4SJulia Cartwright 	raw_spin_unlock_irqrestore(&worker->lock, flags);
127337be45d4SPetr Mladek out:
127437be45d4SPetr Mladek 	return ret;
127537be45d4SPetr Mladek }
127637be45d4SPetr Mladek 
127737be45d4SPetr Mladek /**
127837be45d4SPetr Mladek  * kthread_cancel_work_sync - cancel a kthread work and wait for it to finish
127937be45d4SPetr Mladek  * @work: the kthread work to cancel
128037be45d4SPetr Mladek  *
128137be45d4SPetr Mladek  * Cancel @work and wait for its execution to finish.  This function
128237be45d4SPetr Mladek  * can be used even if the work re-queues itself. On return from this
128337be45d4SPetr Mladek  * function, @work is guaranteed to be not pending or executing on any CPU.
128437be45d4SPetr Mladek  *
128537be45d4SPetr Mladek  * kthread_cancel_work_sync(&delayed_work->work) must not be used for
128637be45d4SPetr Mladek  * delayed_work's. Use kthread_cancel_delayed_work_sync() instead.
128737be45d4SPetr Mladek  *
128837be45d4SPetr Mladek  * The caller must ensure that the worker on which @work was last
128937be45d4SPetr Mladek  * queued can't be destroyed before this function returns.
129037be45d4SPetr Mladek  *
129137be45d4SPetr Mladek  * Return: %true if @work was pending, %false otherwise.
129237be45d4SPetr Mladek  */
129337be45d4SPetr Mladek bool kthread_cancel_work_sync(struct kthread_work *work)
129437be45d4SPetr Mladek {
129537be45d4SPetr Mladek 	return __kthread_cancel_work_sync(work, false);
129637be45d4SPetr Mladek }
129737be45d4SPetr Mladek EXPORT_SYMBOL_GPL(kthread_cancel_work_sync);
129837be45d4SPetr Mladek 
129937be45d4SPetr Mladek /**
130037be45d4SPetr Mladek  * kthread_cancel_delayed_work_sync - cancel a kthread delayed work and
130137be45d4SPetr Mladek  *	wait for it to finish.
130237be45d4SPetr Mladek  * @dwork: the kthread delayed work to cancel
130337be45d4SPetr Mladek  *
130437be45d4SPetr Mladek  * This is kthread_cancel_work_sync() for delayed works.
130537be45d4SPetr Mladek  *
130637be45d4SPetr Mladek  * Return: %true if @dwork was pending, %false otherwise.
130737be45d4SPetr Mladek  */
130837be45d4SPetr Mladek bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *dwork)
130937be45d4SPetr Mladek {
131037be45d4SPetr Mladek 	return __kthread_cancel_work_sync(&dwork->work, true);
131137be45d4SPetr Mladek }
131237be45d4SPetr Mladek EXPORT_SYMBOL_GPL(kthread_cancel_delayed_work_sync);
131337be45d4SPetr Mladek 
1314b56c0d89STejun Heo /**
13153989144fSPetr Mladek  * kthread_flush_worker - flush all current works on a kthread_worker
1316b56c0d89STejun Heo  * @worker: worker to flush
1317b56c0d89STejun Heo  *
1318b56c0d89STejun Heo  * Wait until all currently executing or pending works on @worker are
1319b56c0d89STejun Heo  * finished.
1320b56c0d89STejun Heo  */
13213989144fSPetr Mladek void kthread_flush_worker(struct kthread_worker *worker)
1322b56c0d89STejun Heo {
1323b56c0d89STejun Heo 	struct kthread_flush_work fwork = {
1324b56c0d89STejun Heo 		KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
1325b56c0d89STejun Heo 		COMPLETION_INITIALIZER_ONSTACK(fwork.done),
1326b56c0d89STejun Heo 	};
1327b56c0d89STejun Heo 
13283989144fSPetr Mladek 	kthread_queue_work(worker, &fwork.work);
1329b56c0d89STejun Heo 	wait_for_completion(&fwork.done);
1330b56c0d89STejun Heo }
13313989144fSPetr Mladek EXPORT_SYMBOL_GPL(kthread_flush_worker);
133235033fe9SPetr Mladek 
133335033fe9SPetr Mladek /**
133435033fe9SPetr Mladek  * kthread_destroy_worker - destroy a kthread worker
133535033fe9SPetr Mladek  * @worker: worker to be destroyed
133635033fe9SPetr Mladek  *
133735033fe9SPetr Mladek  * Flush and destroy @worker.  The simple flush is enough because the kthread
133835033fe9SPetr Mladek  * worker API is used only in trivial scenarios.  There are no multi-step state
133935033fe9SPetr Mladek  * machines needed.
134035033fe9SPetr Mladek  */
134135033fe9SPetr Mladek void kthread_destroy_worker(struct kthread_worker *worker)
134235033fe9SPetr Mladek {
134335033fe9SPetr Mladek 	struct task_struct *task;
134435033fe9SPetr Mladek 
134535033fe9SPetr Mladek 	task = worker->task;
134635033fe9SPetr Mladek 	if (WARN_ON(!task))
134735033fe9SPetr Mladek 		return;
134835033fe9SPetr Mladek 
134935033fe9SPetr Mladek 	kthread_flush_worker(worker);
135035033fe9SPetr Mladek 	kthread_stop(task);
135135033fe9SPetr Mladek 	WARN_ON(!list_empty(&worker->work_list));
135235033fe9SPetr Mladek 	kfree(worker);
135335033fe9SPetr Mladek }
135435033fe9SPetr Mladek EXPORT_SYMBOL(kthread_destroy_worker);
135505e3db95SShaohua Li 
1356f5678e7fSChristoph Hellwig /**
1357f5678e7fSChristoph Hellwig  * kthread_use_mm - make the calling kthread operate on an address space
1358f5678e7fSChristoph Hellwig  * @mm: address space to operate on
13599bf5b9ebSChristoph Hellwig  */
1360f5678e7fSChristoph Hellwig void kthread_use_mm(struct mm_struct *mm)
13619bf5b9ebSChristoph Hellwig {
13629bf5b9ebSChristoph Hellwig 	struct mm_struct *active_mm;
13639bf5b9ebSChristoph Hellwig 	struct task_struct *tsk = current;
13649bf5b9ebSChristoph Hellwig 
1365f5678e7fSChristoph Hellwig 	WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD));
1366f5678e7fSChristoph Hellwig 	WARN_ON_ONCE(tsk->mm);
1367f5678e7fSChristoph Hellwig 
13689bf5b9ebSChristoph Hellwig 	task_lock(tsk);
136938cf307cSPeter Zijlstra 	/* Hold off tlb flush IPIs while switching mm's */
137038cf307cSPeter Zijlstra 	local_irq_disable();
13719bf5b9ebSChristoph Hellwig 	active_mm = tsk->active_mm;
13729bf5b9ebSChristoph Hellwig 	if (active_mm != mm) {
13739bf5b9ebSChristoph Hellwig 		mmgrab(mm);
13749bf5b9ebSChristoph Hellwig 		tsk->active_mm = mm;
13759bf5b9ebSChristoph Hellwig 	}
13769bf5b9ebSChristoph Hellwig 	tsk->mm = mm;
1377618758edSMathieu Desnoyers 	membarrier_update_current_mm(mm);
137838cf307cSPeter Zijlstra 	switch_mm_irqs_off(active_mm, mm, tsk);
137938cf307cSPeter Zijlstra 	local_irq_enable();
13809bf5b9ebSChristoph Hellwig 	task_unlock(tsk);
13819bf5b9ebSChristoph Hellwig #ifdef finish_arch_post_lock_switch
13829bf5b9ebSChristoph Hellwig 	finish_arch_post_lock_switch();
13839bf5b9ebSChristoph Hellwig #endif
13849bf5b9ebSChristoph Hellwig 
1385618758edSMathieu Desnoyers 	/*
1386618758edSMathieu Desnoyers 	 * When a kthread starts operating on an address space, the loop
1387618758edSMathieu Desnoyers 	 * in membarrier_{private,global}_expedited() may not observe
1388618758edSMathieu Desnoyers 	 * that tsk->mm, and not issue an IPI. Membarrier requires a
1389618758edSMathieu Desnoyers 	 * memory barrier after storing to tsk->mm, before accessing
1390618758edSMathieu Desnoyers 	 * user-space memory. A full memory barrier for membarrier
1391618758edSMathieu Desnoyers 	 * {PRIVATE,GLOBAL}_EXPEDITED is implicitly provided by
1392618758edSMathieu Desnoyers 	 * mmdrop(), or explicitly with smp_mb().
1393618758edSMathieu Desnoyers 	 */
13949bf5b9ebSChristoph Hellwig 	if (active_mm != mm)
13959bf5b9ebSChristoph Hellwig 		mmdrop(active_mm);
1396618758edSMathieu Desnoyers 	else
1397618758edSMathieu Desnoyers 		smp_mb();
139837c54f9bSChristoph Hellwig 
13993d13f313SChristoph Hellwig 	to_kthread(tsk)->oldfs = force_uaccess_begin();
14009bf5b9ebSChristoph Hellwig }
1401f5678e7fSChristoph Hellwig EXPORT_SYMBOL_GPL(kthread_use_mm);
14029bf5b9ebSChristoph Hellwig 
1403f5678e7fSChristoph Hellwig /**
1404f5678e7fSChristoph Hellwig  * kthread_unuse_mm - reverse the effect of kthread_use_mm()
1405f5678e7fSChristoph Hellwig  * @mm: address space to operate on
14069bf5b9ebSChristoph Hellwig  */
1407f5678e7fSChristoph Hellwig void kthread_unuse_mm(struct mm_struct *mm)
14089bf5b9ebSChristoph Hellwig {
14099bf5b9ebSChristoph Hellwig 	struct task_struct *tsk = current;
14109bf5b9ebSChristoph Hellwig 
1411f5678e7fSChristoph Hellwig 	WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD));
1412f5678e7fSChristoph Hellwig 	WARN_ON_ONCE(!tsk->mm);
1413f5678e7fSChristoph Hellwig 
14143d13f313SChristoph Hellwig 	force_uaccess_end(to_kthread(tsk)->oldfs);
141537c54f9bSChristoph Hellwig 
14169bf5b9ebSChristoph Hellwig 	task_lock(tsk);
1417618758edSMathieu Desnoyers 	/*
1418618758edSMathieu Desnoyers 	 * When a kthread stops operating on an address space, the loop
1419618758edSMathieu Desnoyers 	 * in membarrier_{private,global}_expedited() may not observe
1420618758edSMathieu Desnoyers 	 * that tsk->mm, and not issue an IPI. Membarrier requires a
1421618758edSMathieu Desnoyers 	 * memory barrier after accessing user-space memory, before
1422618758edSMathieu Desnoyers 	 * clearing tsk->mm.
1423618758edSMathieu Desnoyers 	 */
1424618758edSMathieu Desnoyers 	smp_mb__after_spinlock();
14259bf5b9ebSChristoph Hellwig 	sync_mm_rss(mm);
142638cf307cSPeter Zijlstra 	local_irq_disable();
14279bf5b9ebSChristoph Hellwig 	tsk->mm = NULL;
1428618758edSMathieu Desnoyers 	membarrier_update_current_mm(NULL);
14299bf5b9ebSChristoph Hellwig 	/* active_mm is still 'mm' */
14309bf5b9ebSChristoph Hellwig 	enter_lazy_tlb(mm, tsk);
143138cf307cSPeter Zijlstra 	local_irq_enable();
14329bf5b9ebSChristoph Hellwig 	task_unlock(tsk);
14339bf5b9ebSChristoph Hellwig }
1434f5678e7fSChristoph Hellwig EXPORT_SYMBOL_GPL(kthread_unuse_mm);
14359bf5b9ebSChristoph Hellwig 
14360b508bc9SShaohua Li #ifdef CONFIG_BLK_CGROUP
143705e3db95SShaohua Li /**
143805e3db95SShaohua Li  * kthread_associate_blkcg - associate blkcg to current kthread
143905e3db95SShaohua Li  * @css: the cgroup info
144005e3db95SShaohua Li  *
144105e3db95SShaohua Li  * Current thread must be a kthread. The thread is running jobs on behalf of
144205e3db95SShaohua Li  * other threads. In some cases, we expect the jobs attach cgroup info of
144305e3db95SShaohua Li  * original threads instead of that of current thread. This function stores
144405e3db95SShaohua Li  * original thread's cgroup info in current kthread context for later
144505e3db95SShaohua Li  * retrieval.
144605e3db95SShaohua Li  */
144705e3db95SShaohua Li void kthread_associate_blkcg(struct cgroup_subsys_state *css)
144805e3db95SShaohua Li {
144905e3db95SShaohua Li 	struct kthread *kthread;
145005e3db95SShaohua Li 
145105e3db95SShaohua Li 	if (!(current->flags & PF_KTHREAD))
145205e3db95SShaohua Li 		return;
145305e3db95SShaohua Li 	kthread = to_kthread(current);
145405e3db95SShaohua Li 	if (!kthread)
145505e3db95SShaohua Li 		return;
145605e3db95SShaohua Li 
145705e3db95SShaohua Li 	if (kthread->blkcg_css) {
145805e3db95SShaohua Li 		css_put(kthread->blkcg_css);
145905e3db95SShaohua Li 		kthread->blkcg_css = NULL;
146005e3db95SShaohua Li 	}
146105e3db95SShaohua Li 	if (css) {
146205e3db95SShaohua Li 		css_get(css);
146305e3db95SShaohua Li 		kthread->blkcg_css = css;
146405e3db95SShaohua Li 	}
146505e3db95SShaohua Li }
146605e3db95SShaohua Li EXPORT_SYMBOL(kthread_associate_blkcg);
146705e3db95SShaohua Li 
146805e3db95SShaohua Li /**
146905e3db95SShaohua Li  * kthread_blkcg - get associated blkcg css of current kthread
147005e3db95SShaohua Li  *
147105e3db95SShaohua Li  * Current thread must be a kthread.
147205e3db95SShaohua Li  */
147305e3db95SShaohua Li struct cgroup_subsys_state *kthread_blkcg(void)
147405e3db95SShaohua Li {
147505e3db95SShaohua Li 	struct kthread *kthread;
147605e3db95SShaohua Li 
147705e3db95SShaohua Li 	if (current->flags & PF_KTHREAD) {
147805e3db95SShaohua Li 		kthread = to_kthread(current);
147905e3db95SShaohua Li 		if (kthread)
148005e3db95SShaohua Li 			return kthread->blkcg_css;
148105e3db95SShaohua Li 	}
148205e3db95SShaohua Li 	return NULL;
148305e3db95SShaohua Li }
148405e3db95SShaohua Li EXPORT_SYMBOL(kthread_blkcg);
148505e3db95SShaohua Li #endif
1486