xref: /linux/kernel/kthread.c (revision 086e9074f52f6f623e1d6d02edc3cb661bb04f4e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Kernel thread helper functions.
3  *   Copyright (C) 2004 IBM Corporation, Rusty Russell.
4  *
5  * Creation is done via kthreadd, so that we get a clean environment
6  * even if we're invoked from userspace (think modprobe, hotplug cpu,
7  * etc.).
8  */
9 #include <uapi/linux/sched/types.h>
10 #include <linux/sched.h>
11 #include <linux/sched/task.h>
12 #include <linux/kthread.h>
13 #include <linux/completion.h>
14 #include <linux/err.h>
15 #include <linux/cgroup.h>
16 #include <linux/cpuset.h>
17 #include <linux/unistd.h>
18 #include <linux/file.h>
19 #include <linux/export.h>
20 #include <linux/mutex.h>
21 #include <linux/slab.h>
22 #include <linux/freezer.h>
23 #include <linux/ptrace.h>
24 #include <linux/uaccess.h>
25 #include <linux/numa.h>
26 #include <trace/events/sched.h>
27 
28 static DEFINE_SPINLOCK(kthread_create_lock);
29 static LIST_HEAD(kthread_create_list);
30 struct task_struct *kthreadd_task;
31 
32 struct kthread_create_info
33 {
34 	/* Information passed to kthread() from kthreadd. */
35 	int (*threadfn)(void *data);
36 	void *data;
37 	int node;
38 
39 	/* Result passed back to kthread_create() from kthreadd. */
40 	struct task_struct *result;
41 	struct completion *done;
42 
43 	struct list_head list;
44 };
45 
46 struct kthread {
47 	unsigned long flags;
48 	unsigned int cpu;
49 	int (*threadfn)(void *);
50 	void *data;
51 	struct completion parked;
52 	struct completion exited;
53 #ifdef CONFIG_BLK_CGROUP
54 	struct cgroup_subsys_state *blkcg_css;
55 #endif
56 };
57 
58 enum KTHREAD_BITS {
59 	KTHREAD_IS_PER_CPU = 0,
60 	KTHREAD_SHOULD_STOP,
61 	KTHREAD_SHOULD_PARK,
62 };
63 
64 static inline void set_kthread_struct(void *kthread)
65 {
66 	/*
67 	 * We abuse ->set_child_tid to avoid the new member and because it
68 	 * can't be wrongly copied by copy_process(). We also rely on fact
69 	 * that the caller can't exec, so PF_KTHREAD can't be cleared.
70 	 */
71 	current->set_child_tid = (__force void __user *)kthread;
72 }
73 
74 static inline struct kthread *to_kthread(struct task_struct *k)
75 {
76 	WARN_ON(!(k->flags & PF_KTHREAD));
77 	return (__force void *)k->set_child_tid;
78 }
79 
80 void free_kthread_struct(struct task_struct *k)
81 {
82 	struct kthread *kthread;
83 
84 	/*
85 	 * Can be NULL if this kthread was created by kernel_thread()
86 	 * or if kmalloc() in kthread() failed.
87 	 */
88 	kthread = to_kthread(k);
89 #ifdef CONFIG_BLK_CGROUP
90 	WARN_ON_ONCE(kthread && kthread->blkcg_css);
91 #endif
92 	kfree(kthread);
93 }
94 
95 /**
96  * kthread_should_stop - should this kthread return now?
97  *
98  * When someone calls kthread_stop() on your kthread, it will be woken
99  * and this will return true.  You should then return, and your return
100  * value will be passed through to kthread_stop().
101  */
102 bool kthread_should_stop(void)
103 {
104 	return test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags);
105 }
106 EXPORT_SYMBOL(kthread_should_stop);
107 
108 bool __kthread_should_park(struct task_struct *k)
109 {
110 	return test_bit(KTHREAD_SHOULD_PARK, &to_kthread(k)->flags);
111 }
112 EXPORT_SYMBOL_GPL(__kthread_should_park);
113 
114 /**
115  * kthread_should_park - should this kthread park now?
116  *
117  * When someone calls kthread_park() on your kthread, it will be woken
118  * and this will return true.  You should then do the necessary
119  * cleanup and call kthread_parkme()
120  *
121  * Similar to kthread_should_stop(), but this keeps the thread alive
122  * and in a park position. kthread_unpark() "restarts" the thread and
123  * calls the thread function again.
124  */
125 bool kthread_should_park(void)
126 {
127 	return __kthread_should_park(current);
128 }
129 EXPORT_SYMBOL_GPL(kthread_should_park);
130 
131 /**
132  * kthread_freezable_should_stop - should this freezable kthread return now?
133  * @was_frozen: optional out parameter, indicates whether %current was frozen
134  *
135  * kthread_should_stop() for freezable kthreads, which will enter
136  * refrigerator if necessary.  This function is safe from kthread_stop() /
137  * freezer deadlock and freezable kthreads should use this function instead
138  * of calling try_to_freeze() directly.
139  */
140 bool kthread_freezable_should_stop(bool *was_frozen)
141 {
142 	bool frozen = false;
143 
144 	might_sleep();
145 
146 	if (unlikely(freezing(current)))
147 		frozen = __refrigerator(true);
148 
149 	if (was_frozen)
150 		*was_frozen = frozen;
151 
152 	return kthread_should_stop();
153 }
154 EXPORT_SYMBOL_GPL(kthread_freezable_should_stop);
155 
156 /**
157  * kthread_func - return the function specified on kthread creation
158  * @task: kthread task in question
159  *
160  * Returns NULL if the task is not a kthread.
161  */
162 void *kthread_func(struct task_struct *task)
163 {
164 	if (task->flags & PF_KTHREAD)
165 		return to_kthread(task)->threadfn;
166 	return NULL;
167 }
168 EXPORT_SYMBOL_GPL(kthread_func);
169 
170 /**
171  * kthread_data - return data value specified on kthread creation
172  * @task: kthread task in question
173  *
174  * Return the data value specified when kthread @task was created.
175  * The caller is responsible for ensuring the validity of @task when
176  * calling this function.
177  */
178 void *kthread_data(struct task_struct *task)
179 {
180 	return to_kthread(task)->data;
181 }
182 EXPORT_SYMBOL_GPL(kthread_data);
183 
184 /**
185  * kthread_probe_data - speculative version of kthread_data()
186  * @task: possible kthread task in question
187  *
188  * @task could be a kthread task.  Return the data value specified when it
189  * was created if accessible.  If @task isn't a kthread task or its data is
190  * inaccessible for any reason, %NULL is returned.  This function requires
191  * that @task itself is safe to dereference.
192  */
193 void *kthread_probe_data(struct task_struct *task)
194 {
195 	struct kthread *kthread = to_kthread(task);
196 	void *data = NULL;
197 
198 	probe_kernel_read(&data, &kthread->data, sizeof(data));
199 	return data;
200 }
201 
202 static void __kthread_parkme(struct kthread *self)
203 {
204 	for (;;) {
205 		/*
206 		 * TASK_PARKED is a special state; we must serialize against
207 		 * possible pending wakeups to avoid store-store collisions on
208 		 * task->state.
209 		 *
210 		 * Such a collision might possibly result in the task state
211 		 * changin from TASK_PARKED and us failing the
212 		 * wait_task_inactive() in kthread_park().
213 		 */
214 		set_special_state(TASK_PARKED);
215 		if (!test_bit(KTHREAD_SHOULD_PARK, &self->flags))
216 			break;
217 
218 		/*
219 		 * Thread is going to call schedule(), do not preempt it,
220 		 * or the caller of kthread_park() may spend more time in
221 		 * wait_task_inactive().
222 		 */
223 		preempt_disable();
224 		complete(&self->parked);
225 		schedule_preempt_disabled();
226 		preempt_enable();
227 	}
228 	__set_current_state(TASK_RUNNING);
229 }
230 
231 void kthread_parkme(void)
232 {
233 	__kthread_parkme(to_kthread(current));
234 }
235 EXPORT_SYMBOL_GPL(kthread_parkme);
236 
237 static int kthread(void *_create)
238 {
239 	/* Copy data: it's on kthread's stack */
240 	struct kthread_create_info *create = _create;
241 	int (*threadfn)(void *data) = create->threadfn;
242 	void *data = create->data;
243 	struct completion *done;
244 	struct kthread *self;
245 	int ret;
246 
247 	self = kzalloc(sizeof(*self), GFP_KERNEL);
248 	set_kthread_struct(self);
249 
250 	/* If user was SIGKILLed, I release the structure. */
251 	done = xchg(&create->done, NULL);
252 	if (!done) {
253 		kfree(create);
254 		do_exit(-EINTR);
255 	}
256 
257 	if (!self) {
258 		create->result = ERR_PTR(-ENOMEM);
259 		complete(done);
260 		do_exit(-ENOMEM);
261 	}
262 
263 	self->threadfn = threadfn;
264 	self->data = data;
265 	init_completion(&self->exited);
266 	init_completion(&self->parked);
267 	current->vfork_done = &self->exited;
268 
269 	/* OK, tell user we're spawned, wait for stop or wakeup */
270 	__set_current_state(TASK_UNINTERRUPTIBLE);
271 	create->result = current;
272 	/*
273 	 * Thread is going to call schedule(), do not preempt it,
274 	 * or the creator may spend more time in wait_task_inactive().
275 	 */
276 	preempt_disable();
277 	complete(done);
278 	schedule_preempt_disabled();
279 	preempt_enable();
280 
281 	ret = -EINTR;
282 	if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) {
283 		cgroup_kthread_ready();
284 		__kthread_parkme(self);
285 		ret = threadfn(data);
286 	}
287 	do_exit(ret);
288 }
289 
290 /* called from do_fork() to get node information for about to be created task */
291 int tsk_fork_get_node(struct task_struct *tsk)
292 {
293 #ifdef CONFIG_NUMA
294 	if (tsk == kthreadd_task)
295 		return tsk->pref_node_fork;
296 #endif
297 	return NUMA_NO_NODE;
298 }
299 
300 static void create_kthread(struct kthread_create_info *create)
301 {
302 	int pid;
303 
304 #ifdef CONFIG_NUMA
305 	current->pref_node_fork = create->node;
306 #endif
307 	/* We want our own signal handler (we take no signals by default). */
308 	pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
309 	if (pid < 0) {
310 		/* If user was SIGKILLed, I release the structure. */
311 		struct completion *done = xchg(&create->done, NULL);
312 
313 		if (!done) {
314 			kfree(create);
315 			return;
316 		}
317 		create->result = ERR_PTR(pid);
318 		complete(done);
319 	}
320 }
321 
322 static __printf(4, 0)
323 struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data),
324 						    void *data, int node,
325 						    const char namefmt[],
326 						    va_list args)
327 {
328 	DECLARE_COMPLETION_ONSTACK(done);
329 	struct task_struct *task;
330 	struct kthread_create_info *create = kmalloc(sizeof(*create),
331 						     GFP_KERNEL);
332 
333 	if (!create)
334 		return ERR_PTR(-ENOMEM);
335 	create->threadfn = threadfn;
336 	create->data = data;
337 	create->node = node;
338 	create->done = &done;
339 
340 	spin_lock(&kthread_create_lock);
341 	list_add_tail(&create->list, &kthread_create_list);
342 	spin_unlock(&kthread_create_lock);
343 
344 	wake_up_process(kthreadd_task);
345 	/*
346 	 * Wait for completion in killable state, for I might be chosen by
347 	 * the OOM killer while kthreadd is trying to allocate memory for
348 	 * new kernel thread.
349 	 */
350 	if (unlikely(wait_for_completion_killable(&done))) {
351 		/*
352 		 * If I was SIGKILLed before kthreadd (or new kernel thread)
353 		 * calls complete(), leave the cleanup of this structure to
354 		 * that thread.
355 		 */
356 		if (xchg(&create->done, NULL))
357 			return ERR_PTR(-EINTR);
358 		/*
359 		 * kthreadd (or new kernel thread) will call complete()
360 		 * shortly.
361 		 */
362 		wait_for_completion(&done);
363 	}
364 	task = create->result;
365 	if (!IS_ERR(task)) {
366 		static const struct sched_param param = { .sched_priority = 0 };
367 		char name[TASK_COMM_LEN];
368 
369 		/*
370 		 * task is already visible to other tasks, so updating
371 		 * COMM must be protected.
372 		 */
373 		vsnprintf(name, sizeof(name), namefmt, args);
374 		set_task_comm(task, name);
375 		/*
376 		 * root may have changed our (kthreadd's) priority or CPU mask.
377 		 * The kernel thread should not inherit these properties.
378 		 */
379 		sched_setscheduler_nocheck(task, SCHED_NORMAL, &param);
380 		set_cpus_allowed_ptr(task, cpu_all_mask);
381 	}
382 	kfree(create);
383 	return task;
384 }
385 
386 /**
387  * kthread_create_on_node - create a kthread.
388  * @threadfn: the function to run until signal_pending(current).
389  * @data: data ptr for @threadfn.
390  * @node: task and thread structures for the thread are allocated on this node
391  * @namefmt: printf-style name for the thread.
392  *
393  * Description: This helper function creates and names a kernel
394  * thread.  The thread will be stopped: use wake_up_process() to start
395  * it.  See also kthread_run().  The new thread has SCHED_NORMAL policy and
396  * is affine to all CPUs.
397  *
398  * If thread is going to be bound on a particular cpu, give its node
399  * in @node, to get NUMA affinity for kthread stack, or else give NUMA_NO_NODE.
400  * When woken, the thread will run @threadfn() with @data as its
401  * argument. @threadfn() can either call do_exit() directly if it is a
402  * standalone thread for which no one will call kthread_stop(), or
403  * return when 'kthread_should_stop()' is true (which means
404  * kthread_stop() has been called).  The return value should be zero
405  * or a negative error number; it will be passed to kthread_stop().
406  *
407  * Returns a task_struct or ERR_PTR(-ENOMEM) or ERR_PTR(-EINTR).
408  */
409 struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
410 					   void *data, int node,
411 					   const char namefmt[],
412 					   ...)
413 {
414 	struct task_struct *task;
415 	va_list args;
416 
417 	va_start(args, namefmt);
418 	task = __kthread_create_on_node(threadfn, data, node, namefmt, args);
419 	va_end(args);
420 
421 	return task;
422 }
423 EXPORT_SYMBOL(kthread_create_on_node);
424 
425 static void __kthread_bind_mask(struct task_struct *p, const struct cpumask *mask, long state)
426 {
427 	unsigned long flags;
428 
429 	if (!wait_task_inactive(p, state)) {
430 		WARN_ON(1);
431 		return;
432 	}
433 
434 	/* It's safe because the task is inactive. */
435 	raw_spin_lock_irqsave(&p->pi_lock, flags);
436 	do_set_cpus_allowed(p, mask);
437 	p->flags |= PF_NO_SETAFFINITY;
438 	raw_spin_unlock_irqrestore(&p->pi_lock, flags);
439 }
440 
441 static void __kthread_bind(struct task_struct *p, unsigned int cpu, long state)
442 {
443 	__kthread_bind_mask(p, cpumask_of(cpu), state);
444 }
445 
446 void kthread_bind_mask(struct task_struct *p, const struct cpumask *mask)
447 {
448 	__kthread_bind_mask(p, mask, TASK_UNINTERRUPTIBLE);
449 }
450 
451 /**
452  * kthread_bind - bind a just-created kthread to a cpu.
453  * @p: thread created by kthread_create().
454  * @cpu: cpu (might not be online, must be possible) for @k to run on.
455  *
456  * Description: This function is equivalent to set_cpus_allowed(),
457  * except that @cpu doesn't need to be online, and the thread must be
458  * stopped (i.e., just returned from kthread_create()).
459  */
460 void kthread_bind(struct task_struct *p, unsigned int cpu)
461 {
462 	__kthread_bind(p, cpu, TASK_UNINTERRUPTIBLE);
463 }
464 EXPORT_SYMBOL(kthread_bind);
465 
466 /**
467  * kthread_create_on_cpu - Create a cpu bound kthread
468  * @threadfn: the function to run until signal_pending(current).
469  * @data: data ptr for @threadfn.
470  * @cpu: The cpu on which the thread should be bound,
471  * @namefmt: printf-style name for the thread. Format is restricted
472  *	     to "name.*%u". Code fills in cpu number.
473  *
474  * Description: This helper function creates and names a kernel thread
475  * The thread will be woken and put into park mode.
476  */
477 struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
478 					  void *data, unsigned int cpu,
479 					  const char *namefmt)
480 {
481 	struct task_struct *p;
482 
483 	p = kthread_create_on_node(threadfn, data, cpu_to_node(cpu), namefmt,
484 				   cpu);
485 	if (IS_ERR(p))
486 		return p;
487 	kthread_bind(p, cpu);
488 	/* CPU hotplug need to bind once again when unparking the thread. */
489 	set_bit(KTHREAD_IS_PER_CPU, &to_kthread(p)->flags);
490 	to_kthread(p)->cpu = cpu;
491 	return p;
492 }
493 
494 /**
495  * kthread_unpark - unpark a thread created by kthread_create().
496  * @k:		thread created by kthread_create().
497  *
498  * Sets kthread_should_park() for @k to return false, wakes it, and
499  * waits for it to return. If the thread is marked percpu then its
500  * bound to the cpu again.
501  */
502 void kthread_unpark(struct task_struct *k)
503 {
504 	struct kthread *kthread = to_kthread(k);
505 
506 	/*
507 	 * Newly created kthread was parked when the CPU was offline.
508 	 * The binding was lost and we need to set it again.
509 	 */
510 	if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags))
511 		__kthread_bind(k, kthread->cpu, TASK_PARKED);
512 
513 	clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
514 	/*
515 	 * __kthread_parkme() will either see !SHOULD_PARK or get the wakeup.
516 	 */
517 	wake_up_state(k, TASK_PARKED);
518 }
519 EXPORT_SYMBOL_GPL(kthread_unpark);
520 
521 /**
522  * kthread_park - park a thread created by kthread_create().
523  * @k: thread created by kthread_create().
524  *
525  * Sets kthread_should_park() for @k to return true, wakes it, and
526  * waits for it to return. This can also be called after kthread_create()
527  * instead of calling wake_up_process(): the thread will park without
528  * calling threadfn().
529  *
530  * Returns 0 if the thread is parked, -ENOSYS if the thread exited.
531  * If called by the kthread itself just the park bit is set.
532  */
533 int kthread_park(struct task_struct *k)
534 {
535 	struct kthread *kthread = to_kthread(k);
536 
537 	if (WARN_ON(k->flags & PF_EXITING))
538 		return -ENOSYS;
539 
540 	if (WARN_ON_ONCE(test_bit(KTHREAD_SHOULD_PARK, &kthread->flags)))
541 		return -EBUSY;
542 
543 	set_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
544 	if (k != current) {
545 		wake_up_process(k);
546 		/*
547 		 * Wait for __kthread_parkme() to complete(), this means we
548 		 * _will_ have TASK_PARKED and are about to call schedule().
549 		 */
550 		wait_for_completion(&kthread->parked);
551 		/*
552 		 * Now wait for that schedule() to complete and the task to
553 		 * get scheduled out.
554 		 */
555 		WARN_ON_ONCE(!wait_task_inactive(k, TASK_PARKED));
556 	}
557 
558 	return 0;
559 }
560 EXPORT_SYMBOL_GPL(kthread_park);
561 
562 /**
563  * kthread_stop - stop a thread created by kthread_create().
564  * @k: thread created by kthread_create().
565  *
566  * Sets kthread_should_stop() for @k to return true, wakes it, and
567  * waits for it to exit. This can also be called after kthread_create()
568  * instead of calling wake_up_process(): the thread will exit without
569  * calling threadfn().
570  *
571  * If threadfn() may call do_exit() itself, the caller must ensure
572  * task_struct can't go away.
573  *
574  * Returns the result of threadfn(), or %-EINTR if wake_up_process()
575  * was never called.
576  */
577 int kthread_stop(struct task_struct *k)
578 {
579 	struct kthread *kthread;
580 	int ret;
581 
582 	trace_sched_kthread_stop(k);
583 
584 	get_task_struct(k);
585 	kthread = to_kthread(k);
586 	set_bit(KTHREAD_SHOULD_STOP, &kthread->flags);
587 	kthread_unpark(k);
588 	wake_up_process(k);
589 	wait_for_completion(&kthread->exited);
590 	ret = k->exit_code;
591 	put_task_struct(k);
592 
593 	trace_sched_kthread_stop_ret(ret);
594 	return ret;
595 }
596 EXPORT_SYMBOL(kthread_stop);
597 
598 int kthreadd(void *unused)
599 {
600 	struct task_struct *tsk = current;
601 
602 	/* Setup a clean context for our children to inherit. */
603 	set_task_comm(tsk, "kthreadd");
604 	ignore_signals(tsk);
605 	set_cpus_allowed_ptr(tsk, cpu_all_mask);
606 	set_mems_allowed(node_states[N_MEMORY]);
607 
608 	current->flags |= PF_NOFREEZE;
609 	cgroup_init_kthreadd();
610 
611 	for (;;) {
612 		set_current_state(TASK_INTERRUPTIBLE);
613 		if (list_empty(&kthread_create_list))
614 			schedule();
615 		__set_current_state(TASK_RUNNING);
616 
617 		spin_lock(&kthread_create_lock);
618 		while (!list_empty(&kthread_create_list)) {
619 			struct kthread_create_info *create;
620 
621 			create = list_entry(kthread_create_list.next,
622 					    struct kthread_create_info, list);
623 			list_del_init(&create->list);
624 			spin_unlock(&kthread_create_lock);
625 
626 			create_kthread(create);
627 
628 			spin_lock(&kthread_create_lock);
629 		}
630 		spin_unlock(&kthread_create_lock);
631 	}
632 
633 	return 0;
634 }
635 
636 void __kthread_init_worker(struct kthread_worker *worker,
637 				const char *name,
638 				struct lock_class_key *key)
639 {
640 	memset(worker, 0, sizeof(struct kthread_worker));
641 	raw_spin_lock_init(&worker->lock);
642 	lockdep_set_class_and_name(&worker->lock, key, name);
643 	INIT_LIST_HEAD(&worker->work_list);
644 	INIT_LIST_HEAD(&worker->delayed_work_list);
645 }
646 EXPORT_SYMBOL_GPL(__kthread_init_worker);
647 
648 /**
649  * kthread_worker_fn - kthread function to process kthread_worker
650  * @worker_ptr: pointer to initialized kthread_worker
651  *
652  * This function implements the main cycle of kthread worker. It processes
653  * work_list until it is stopped with kthread_stop(). It sleeps when the queue
654  * is empty.
655  *
656  * The works are not allowed to keep any locks, disable preemption or interrupts
657  * when they finish. There is defined a safe point for freezing when one work
658  * finishes and before a new one is started.
659  *
660  * Also the works must not be handled by more than one worker at the same time,
661  * see also kthread_queue_work().
662  */
663 int kthread_worker_fn(void *worker_ptr)
664 {
665 	struct kthread_worker *worker = worker_ptr;
666 	struct kthread_work *work;
667 
668 	/*
669 	 * FIXME: Update the check and remove the assignment when all kthread
670 	 * worker users are created using kthread_create_worker*() functions.
671 	 */
672 	WARN_ON(worker->task && worker->task != current);
673 	worker->task = current;
674 
675 	if (worker->flags & KTW_FREEZABLE)
676 		set_freezable();
677 
678 repeat:
679 	set_current_state(TASK_INTERRUPTIBLE);	/* mb paired w/ kthread_stop */
680 
681 	if (kthread_should_stop()) {
682 		__set_current_state(TASK_RUNNING);
683 		raw_spin_lock_irq(&worker->lock);
684 		worker->task = NULL;
685 		raw_spin_unlock_irq(&worker->lock);
686 		return 0;
687 	}
688 
689 	work = NULL;
690 	raw_spin_lock_irq(&worker->lock);
691 	if (!list_empty(&worker->work_list)) {
692 		work = list_first_entry(&worker->work_list,
693 					struct kthread_work, node);
694 		list_del_init(&work->node);
695 	}
696 	worker->current_work = work;
697 	raw_spin_unlock_irq(&worker->lock);
698 
699 	if (work) {
700 		__set_current_state(TASK_RUNNING);
701 		work->func(work);
702 	} else if (!freezing(current))
703 		schedule();
704 
705 	try_to_freeze();
706 	cond_resched();
707 	goto repeat;
708 }
709 EXPORT_SYMBOL_GPL(kthread_worker_fn);
710 
711 static __printf(3, 0) struct kthread_worker *
712 __kthread_create_worker(int cpu, unsigned int flags,
713 			const char namefmt[], va_list args)
714 {
715 	struct kthread_worker *worker;
716 	struct task_struct *task;
717 	int node = NUMA_NO_NODE;
718 
719 	worker = kzalloc(sizeof(*worker), GFP_KERNEL);
720 	if (!worker)
721 		return ERR_PTR(-ENOMEM);
722 
723 	kthread_init_worker(worker);
724 
725 	if (cpu >= 0)
726 		node = cpu_to_node(cpu);
727 
728 	task = __kthread_create_on_node(kthread_worker_fn, worker,
729 						node, namefmt, args);
730 	if (IS_ERR(task))
731 		goto fail_task;
732 
733 	if (cpu >= 0)
734 		kthread_bind(task, cpu);
735 
736 	worker->flags = flags;
737 	worker->task = task;
738 	wake_up_process(task);
739 	return worker;
740 
741 fail_task:
742 	kfree(worker);
743 	return ERR_CAST(task);
744 }
745 
746 /**
747  * kthread_create_worker - create a kthread worker
748  * @flags: flags modifying the default behavior of the worker
749  * @namefmt: printf-style name for the kthread worker (task).
750  *
751  * Returns a pointer to the allocated worker on success, ERR_PTR(-ENOMEM)
752  * when the needed structures could not get allocated, and ERR_PTR(-EINTR)
753  * when the worker was SIGKILLed.
754  */
755 struct kthread_worker *
756 kthread_create_worker(unsigned int flags, const char namefmt[], ...)
757 {
758 	struct kthread_worker *worker;
759 	va_list args;
760 
761 	va_start(args, namefmt);
762 	worker = __kthread_create_worker(-1, flags, namefmt, args);
763 	va_end(args);
764 
765 	return worker;
766 }
767 EXPORT_SYMBOL(kthread_create_worker);
768 
769 /**
770  * kthread_create_worker_on_cpu - create a kthread worker and bind it
771  *	it to a given CPU and the associated NUMA node.
772  * @cpu: CPU number
773  * @flags: flags modifying the default behavior of the worker
774  * @namefmt: printf-style name for the kthread worker (task).
775  *
776  * Use a valid CPU number if you want to bind the kthread worker
777  * to the given CPU and the associated NUMA node.
778  *
779  * A good practice is to add the cpu number also into the worker name.
780  * For example, use kthread_create_worker_on_cpu(cpu, "helper/%d", cpu).
781  *
782  * Returns a pointer to the allocated worker on success, ERR_PTR(-ENOMEM)
783  * when the needed structures could not get allocated, and ERR_PTR(-EINTR)
784  * when the worker was SIGKILLed.
785  */
786 struct kthread_worker *
787 kthread_create_worker_on_cpu(int cpu, unsigned int flags,
788 			     const char namefmt[], ...)
789 {
790 	struct kthread_worker *worker;
791 	va_list args;
792 
793 	va_start(args, namefmt);
794 	worker = __kthread_create_worker(cpu, flags, namefmt, args);
795 	va_end(args);
796 
797 	return worker;
798 }
799 EXPORT_SYMBOL(kthread_create_worker_on_cpu);
800 
801 /*
802  * Returns true when the work could not be queued at the moment.
803  * It happens when it is already pending in a worker list
804  * or when it is being cancelled.
805  */
806 static inline bool queuing_blocked(struct kthread_worker *worker,
807 				   struct kthread_work *work)
808 {
809 	lockdep_assert_held(&worker->lock);
810 
811 	return !list_empty(&work->node) || work->canceling;
812 }
813 
814 static void kthread_insert_work_sanity_check(struct kthread_worker *worker,
815 					     struct kthread_work *work)
816 {
817 	lockdep_assert_held(&worker->lock);
818 	WARN_ON_ONCE(!list_empty(&work->node));
819 	/* Do not use a work with >1 worker, see kthread_queue_work() */
820 	WARN_ON_ONCE(work->worker && work->worker != worker);
821 }
822 
823 /* insert @work before @pos in @worker */
824 static void kthread_insert_work(struct kthread_worker *worker,
825 				struct kthread_work *work,
826 				struct list_head *pos)
827 {
828 	kthread_insert_work_sanity_check(worker, work);
829 
830 	list_add_tail(&work->node, pos);
831 	work->worker = worker;
832 	if (!worker->current_work && likely(worker->task))
833 		wake_up_process(worker->task);
834 }
835 
836 /**
837  * kthread_queue_work - queue a kthread_work
838  * @worker: target kthread_worker
839  * @work: kthread_work to queue
840  *
841  * Queue @work to work processor @task for async execution.  @task
842  * must have been created with kthread_worker_create().  Returns %true
843  * if @work was successfully queued, %false if it was already pending.
844  *
845  * Reinitialize the work if it needs to be used by another worker.
846  * For example, when the worker was stopped and started again.
847  */
848 bool kthread_queue_work(struct kthread_worker *worker,
849 			struct kthread_work *work)
850 {
851 	bool ret = false;
852 	unsigned long flags;
853 
854 	raw_spin_lock_irqsave(&worker->lock, flags);
855 	if (!queuing_blocked(worker, work)) {
856 		kthread_insert_work(worker, work, &worker->work_list);
857 		ret = true;
858 	}
859 	raw_spin_unlock_irqrestore(&worker->lock, flags);
860 	return ret;
861 }
862 EXPORT_SYMBOL_GPL(kthread_queue_work);
863 
864 /**
865  * kthread_delayed_work_timer_fn - callback that queues the associated kthread
866  *	delayed work when the timer expires.
867  * @t: pointer to the expired timer
868  *
869  * The format of the function is defined by struct timer_list.
870  * It should have been called from irqsafe timer with irq already off.
871  */
872 void kthread_delayed_work_timer_fn(struct timer_list *t)
873 {
874 	struct kthread_delayed_work *dwork = from_timer(dwork, t, timer);
875 	struct kthread_work *work = &dwork->work;
876 	struct kthread_worker *worker = work->worker;
877 	unsigned long flags;
878 
879 	/*
880 	 * This might happen when a pending work is reinitialized.
881 	 * It means that it is used a wrong way.
882 	 */
883 	if (WARN_ON_ONCE(!worker))
884 		return;
885 
886 	raw_spin_lock_irqsave(&worker->lock, flags);
887 	/* Work must not be used with >1 worker, see kthread_queue_work(). */
888 	WARN_ON_ONCE(work->worker != worker);
889 
890 	/* Move the work from worker->delayed_work_list. */
891 	WARN_ON_ONCE(list_empty(&work->node));
892 	list_del_init(&work->node);
893 	kthread_insert_work(worker, work, &worker->work_list);
894 
895 	raw_spin_unlock_irqrestore(&worker->lock, flags);
896 }
897 EXPORT_SYMBOL(kthread_delayed_work_timer_fn);
898 
899 static void __kthread_queue_delayed_work(struct kthread_worker *worker,
900 					 struct kthread_delayed_work *dwork,
901 					 unsigned long delay)
902 {
903 	struct timer_list *timer = &dwork->timer;
904 	struct kthread_work *work = &dwork->work;
905 
906 	WARN_ON_ONCE(timer->function != kthread_delayed_work_timer_fn);
907 
908 	/*
909 	 * If @delay is 0, queue @dwork->work immediately.  This is for
910 	 * both optimization and correctness.  The earliest @timer can
911 	 * expire is on the closest next tick and delayed_work users depend
912 	 * on that there's no such delay when @delay is 0.
913 	 */
914 	if (!delay) {
915 		kthread_insert_work(worker, work, &worker->work_list);
916 		return;
917 	}
918 
919 	/* Be paranoid and try to detect possible races already now. */
920 	kthread_insert_work_sanity_check(worker, work);
921 
922 	list_add(&work->node, &worker->delayed_work_list);
923 	work->worker = worker;
924 	timer->expires = jiffies + delay;
925 	add_timer(timer);
926 }
927 
928 /**
929  * kthread_queue_delayed_work - queue the associated kthread work
930  *	after a delay.
931  * @worker: target kthread_worker
932  * @dwork: kthread_delayed_work to queue
933  * @delay: number of jiffies to wait before queuing
934  *
935  * If the work has not been pending it starts a timer that will queue
936  * the work after the given @delay. If @delay is zero, it queues the
937  * work immediately.
938  *
939  * Return: %false if the @work has already been pending. It means that
940  * either the timer was running or the work was queued. It returns %true
941  * otherwise.
942  */
943 bool kthread_queue_delayed_work(struct kthread_worker *worker,
944 				struct kthread_delayed_work *dwork,
945 				unsigned long delay)
946 {
947 	struct kthread_work *work = &dwork->work;
948 	unsigned long flags;
949 	bool ret = false;
950 
951 	raw_spin_lock_irqsave(&worker->lock, flags);
952 
953 	if (!queuing_blocked(worker, work)) {
954 		__kthread_queue_delayed_work(worker, dwork, delay);
955 		ret = true;
956 	}
957 
958 	raw_spin_unlock_irqrestore(&worker->lock, flags);
959 	return ret;
960 }
961 EXPORT_SYMBOL_GPL(kthread_queue_delayed_work);
962 
963 struct kthread_flush_work {
964 	struct kthread_work	work;
965 	struct completion	done;
966 };
967 
968 static void kthread_flush_work_fn(struct kthread_work *work)
969 {
970 	struct kthread_flush_work *fwork =
971 		container_of(work, struct kthread_flush_work, work);
972 	complete(&fwork->done);
973 }
974 
975 /**
976  * kthread_flush_work - flush a kthread_work
977  * @work: work to flush
978  *
979  * If @work is queued or executing, wait for it to finish execution.
980  */
981 void kthread_flush_work(struct kthread_work *work)
982 {
983 	struct kthread_flush_work fwork = {
984 		KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
985 		COMPLETION_INITIALIZER_ONSTACK(fwork.done),
986 	};
987 	struct kthread_worker *worker;
988 	bool noop = false;
989 
990 	worker = work->worker;
991 	if (!worker)
992 		return;
993 
994 	raw_spin_lock_irq(&worker->lock);
995 	/* Work must not be used with >1 worker, see kthread_queue_work(). */
996 	WARN_ON_ONCE(work->worker != worker);
997 
998 	if (!list_empty(&work->node))
999 		kthread_insert_work(worker, &fwork.work, work->node.next);
1000 	else if (worker->current_work == work)
1001 		kthread_insert_work(worker, &fwork.work,
1002 				    worker->work_list.next);
1003 	else
1004 		noop = true;
1005 
1006 	raw_spin_unlock_irq(&worker->lock);
1007 
1008 	if (!noop)
1009 		wait_for_completion(&fwork.done);
1010 }
1011 EXPORT_SYMBOL_GPL(kthread_flush_work);
1012 
1013 /*
1014  * This function removes the work from the worker queue. Also it makes sure
1015  * that it won't get queued later via the delayed work's timer.
1016  *
1017  * The work might still be in use when this function finishes. See the
1018  * current_work proceed by the worker.
1019  *
1020  * Return: %true if @work was pending and successfully canceled,
1021  *	%false if @work was not pending
1022  */
1023 static bool __kthread_cancel_work(struct kthread_work *work, bool is_dwork,
1024 				  unsigned long *flags)
1025 {
1026 	/* Try to cancel the timer if exists. */
1027 	if (is_dwork) {
1028 		struct kthread_delayed_work *dwork =
1029 			container_of(work, struct kthread_delayed_work, work);
1030 		struct kthread_worker *worker = work->worker;
1031 
1032 		/*
1033 		 * del_timer_sync() must be called to make sure that the timer
1034 		 * callback is not running. The lock must be temporary released
1035 		 * to avoid a deadlock with the callback. In the meantime,
1036 		 * any queuing is blocked by setting the canceling counter.
1037 		 */
1038 		work->canceling++;
1039 		raw_spin_unlock_irqrestore(&worker->lock, *flags);
1040 		del_timer_sync(&dwork->timer);
1041 		raw_spin_lock_irqsave(&worker->lock, *flags);
1042 		work->canceling--;
1043 	}
1044 
1045 	/*
1046 	 * Try to remove the work from a worker list. It might either
1047 	 * be from worker->work_list or from worker->delayed_work_list.
1048 	 */
1049 	if (!list_empty(&work->node)) {
1050 		list_del_init(&work->node);
1051 		return true;
1052 	}
1053 
1054 	return false;
1055 }
1056 
1057 /**
1058  * kthread_mod_delayed_work - modify delay of or queue a kthread delayed work
1059  * @worker: kthread worker to use
1060  * @dwork: kthread delayed work to queue
1061  * @delay: number of jiffies to wait before queuing
1062  *
1063  * If @dwork is idle, equivalent to kthread_queue_delayed_work(). Otherwise,
1064  * modify @dwork's timer so that it expires after @delay. If @delay is zero,
1065  * @work is guaranteed to be queued immediately.
1066  *
1067  * Return: %true if @dwork was pending and its timer was modified,
1068  * %false otherwise.
1069  *
1070  * A special case is when the work is being canceled in parallel.
1071  * It might be caused either by the real kthread_cancel_delayed_work_sync()
1072  * or yet another kthread_mod_delayed_work() call. We let the other command
1073  * win and return %false here. The caller is supposed to synchronize these
1074  * operations a reasonable way.
1075  *
1076  * This function is safe to call from any context including IRQ handler.
1077  * See __kthread_cancel_work() and kthread_delayed_work_timer_fn()
1078  * for details.
1079  */
1080 bool kthread_mod_delayed_work(struct kthread_worker *worker,
1081 			      struct kthread_delayed_work *dwork,
1082 			      unsigned long delay)
1083 {
1084 	struct kthread_work *work = &dwork->work;
1085 	unsigned long flags;
1086 	int ret = false;
1087 
1088 	raw_spin_lock_irqsave(&worker->lock, flags);
1089 
1090 	/* Do not bother with canceling when never queued. */
1091 	if (!work->worker)
1092 		goto fast_queue;
1093 
1094 	/* Work must not be used with >1 worker, see kthread_queue_work() */
1095 	WARN_ON_ONCE(work->worker != worker);
1096 
1097 	/* Do not fight with another command that is canceling this work. */
1098 	if (work->canceling)
1099 		goto out;
1100 
1101 	ret = __kthread_cancel_work(work, true, &flags);
1102 fast_queue:
1103 	__kthread_queue_delayed_work(worker, dwork, delay);
1104 out:
1105 	raw_spin_unlock_irqrestore(&worker->lock, flags);
1106 	return ret;
1107 }
1108 EXPORT_SYMBOL_GPL(kthread_mod_delayed_work);
1109 
1110 static bool __kthread_cancel_work_sync(struct kthread_work *work, bool is_dwork)
1111 {
1112 	struct kthread_worker *worker = work->worker;
1113 	unsigned long flags;
1114 	int ret = false;
1115 
1116 	if (!worker)
1117 		goto out;
1118 
1119 	raw_spin_lock_irqsave(&worker->lock, flags);
1120 	/* Work must not be used with >1 worker, see kthread_queue_work(). */
1121 	WARN_ON_ONCE(work->worker != worker);
1122 
1123 	ret = __kthread_cancel_work(work, is_dwork, &flags);
1124 
1125 	if (worker->current_work != work)
1126 		goto out_fast;
1127 
1128 	/*
1129 	 * The work is in progress and we need to wait with the lock released.
1130 	 * In the meantime, block any queuing by setting the canceling counter.
1131 	 */
1132 	work->canceling++;
1133 	raw_spin_unlock_irqrestore(&worker->lock, flags);
1134 	kthread_flush_work(work);
1135 	raw_spin_lock_irqsave(&worker->lock, flags);
1136 	work->canceling--;
1137 
1138 out_fast:
1139 	raw_spin_unlock_irqrestore(&worker->lock, flags);
1140 out:
1141 	return ret;
1142 }
1143 
1144 /**
1145  * kthread_cancel_work_sync - cancel a kthread work and wait for it to finish
1146  * @work: the kthread work to cancel
1147  *
1148  * Cancel @work and wait for its execution to finish.  This function
1149  * can be used even if the work re-queues itself. On return from this
1150  * function, @work is guaranteed to be not pending or executing on any CPU.
1151  *
1152  * kthread_cancel_work_sync(&delayed_work->work) must not be used for
1153  * delayed_work's. Use kthread_cancel_delayed_work_sync() instead.
1154  *
1155  * The caller must ensure that the worker on which @work was last
1156  * queued can't be destroyed before this function returns.
1157  *
1158  * Return: %true if @work was pending, %false otherwise.
1159  */
1160 bool kthread_cancel_work_sync(struct kthread_work *work)
1161 {
1162 	return __kthread_cancel_work_sync(work, false);
1163 }
1164 EXPORT_SYMBOL_GPL(kthread_cancel_work_sync);
1165 
1166 /**
1167  * kthread_cancel_delayed_work_sync - cancel a kthread delayed work and
1168  *	wait for it to finish.
1169  * @dwork: the kthread delayed work to cancel
1170  *
1171  * This is kthread_cancel_work_sync() for delayed works.
1172  *
1173  * Return: %true if @dwork was pending, %false otherwise.
1174  */
1175 bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *dwork)
1176 {
1177 	return __kthread_cancel_work_sync(&dwork->work, true);
1178 }
1179 EXPORT_SYMBOL_GPL(kthread_cancel_delayed_work_sync);
1180 
1181 /**
1182  * kthread_flush_worker - flush all current works on a kthread_worker
1183  * @worker: worker to flush
1184  *
1185  * Wait until all currently executing or pending works on @worker are
1186  * finished.
1187  */
1188 void kthread_flush_worker(struct kthread_worker *worker)
1189 {
1190 	struct kthread_flush_work fwork = {
1191 		KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
1192 		COMPLETION_INITIALIZER_ONSTACK(fwork.done),
1193 	};
1194 
1195 	kthread_queue_work(worker, &fwork.work);
1196 	wait_for_completion(&fwork.done);
1197 }
1198 EXPORT_SYMBOL_GPL(kthread_flush_worker);
1199 
1200 /**
1201  * kthread_destroy_worker - destroy a kthread worker
1202  * @worker: worker to be destroyed
1203  *
1204  * Flush and destroy @worker.  The simple flush is enough because the kthread
1205  * worker API is used only in trivial scenarios.  There are no multi-step state
1206  * machines needed.
1207  */
1208 void kthread_destroy_worker(struct kthread_worker *worker)
1209 {
1210 	struct task_struct *task;
1211 
1212 	task = worker->task;
1213 	if (WARN_ON(!task))
1214 		return;
1215 
1216 	kthread_flush_worker(worker);
1217 	kthread_stop(task);
1218 	WARN_ON(!list_empty(&worker->work_list));
1219 	kfree(worker);
1220 }
1221 EXPORT_SYMBOL(kthread_destroy_worker);
1222 
1223 #ifdef CONFIG_BLK_CGROUP
1224 /**
1225  * kthread_associate_blkcg - associate blkcg to current kthread
1226  * @css: the cgroup info
1227  *
1228  * Current thread must be a kthread. The thread is running jobs on behalf of
1229  * other threads. In some cases, we expect the jobs attach cgroup info of
1230  * original threads instead of that of current thread. This function stores
1231  * original thread's cgroup info in current kthread context for later
1232  * retrieval.
1233  */
1234 void kthread_associate_blkcg(struct cgroup_subsys_state *css)
1235 {
1236 	struct kthread *kthread;
1237 
1238 	if (!(current->flags & PF_KTHREAD))
1239 		return;
1240 	kthread = to_kthread(current);
1241 	if (!kthread)
1242 		return;
1243 
1244 	if (kthread->blkcg_css) {
1245 		css_put(kthread->blkcg_css);
1246 		kthread->blkcg_css = NULL;
1247 	}
1248 	if (css) {
1249 		css_get(css);
1250 		kthread->blkcg_css = css;
1251 	}
1252 }
1253 EXPORT_SYMBOL(kthread_associate_blkcg);
1254 
1255 /**
1256  * kthread_blkcg - get associated blkcg css of current kthread
1257  *
1258  * Current thread must be a kthread.
1259  */
1260 struct cgroup_subsys_state *kthread_blkcg(void)
1261 {
1262 	struct kthread *kthread;
1263 
1264 	if (current->flags & PF_KTHREAD) {
1265 		kthread = to_kthread(current);
1266 		if (kthread)
1267 			return kthread->blkcg_css;
1268 	}
1269 	return NULL;
1270 }
1271 EXPORT_SYMBOL(kthread_blkcg);
1272 #endif
1273