xref: /linux-6.15/include/linux/kthread.h (revision b04e317b)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_KTHREAD_H
31da177e4SLinus Torvalds #define _LINUX_KTHREAD_H
41da177e4SLinus Torvalds /* Simple interface for creating and stopping kernel threads without mess. */
51da177e4SLinus Torvalds #include <linux/err.h>
61da177e4SLinus Torvalds #include <linux/sched.h>
71da177e4SLinus Torvalds 
89bf5b9ebSChristoph Hellwig struct mm_struct;
99bf5b9ebSChristoph Hellwig 
10b9075fa9SJoe Perches __printf(4, 5)
11207205a2SEric Dumazet struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
121da177e4SLinus Torvalds 					   void *data,
13207205a2SEric Dumazet 					   int node,
14b9075fa9SJoe Perches 					   const char namefmt[], ...);
15207205a2SEric Dumazet 
16e154ccc8SJonathan Corbet /**
17e154ccc8SJonathan Corbet  * kthread_create - create a kthread on the current node
18e154ccc8SJonathan Corbet  * @threadfn: the function to run in the thread
19e154ccc8SJonathan Corbet  * @data: data pointer for @threadfn()
20e154ccc8SJonathan Corbet  * @namefmt: printf-style format string for the thread name
2120ce0c2dSJonathan Neuschäfer  * @arg: arguments for @namefmt.
22e154ccc8SJonathan Corbet  *
23e154ccc8SJonathan Corbet  * This macro will create a kthread on the current node, leaving it in
24e154ccc8SJonathan Corbet  * the stopped state.  This is just a helper for kthread_create_on_node();
25e154ccc8SJonathan Corbet  * see the documentation there for more details.
26e154ccc8SJonathan Corbet  */
27207205a2SEric Dumazet #define kthread_create(threadfn, data, namefmt, arg...) \
28e9f06986SAndrew Morton 	kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
29207205a2SEric Dumazet 
301da177e4SLinus Torvalds 
312a1d4460SThomas Gleixner struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
322a1d4460SThomas Gleixner 					  void *data,
332a1d4460SThomas Gleixner 					  unsigned int cpu,
342a1d4460SThomas Gleixner 					  const char *namefmt);
352a1d4460SThomas Gleixner 
36d6986ce2SYafang Shao void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk);
3740966e31SEric W. Biederman bool set_kthread_struct(struct task_struct *p);
3800b89fe0SValentin Schneider 
39ac687e6eSPeter Zijlstra void kthread_set_per_cpu(struct task_struct *k, int cpu);
40ac687e6eSPeter Zijlstra bool kthread_is_per_cpu(struct task_struct *k);
41ac687e6eSPeter Zijlstra 
421da177e4SLinus Torvalds /**
439e37bd30SRandy Dunlap  * kthread_run - create and wake a thread.
441da177e4SLinus Torvalds  * @threadfn: the function to run until signal_pending(current).
451da177e4SLinus Torvalds  * @data: data ptr for @threadfn.
461da177e4SLinus Torvalds  * @namefmt: printf-style name for the thread.
471da177e4SLinus Torvalds  *
481da177e4SLinus Torvalds  * Description: Convenient wrapper for kthread_create() followed by
499e37bd30SRandy Dunlap  * wake_up_process().  Returns the kthread or ERR_PTR(-ENOMEM).
509e37bd30SRandy Dunlap  */
511da177e4SLinus Torvalds #define kthread_run(threadfn, data, namefmt, ...)			   \
521da177e4SLinus Torvalds ({									   \
531da177e4SLinus Torvalds 	struct task_struct *__k						   \
541da177e4SLinus Torvalds 		= kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
551da177e4SLinus Torvalds 	if (!IS_ERR(__k))						   \
561da177e4SLinus Torvalds 		wake_up_process(__k);					   \
571da177e4SLinus Torvalds 	__k;								   \
581da177e4SLinus Torvalds })
591da177e4SLinus Torvalds 
60800977f6SCai Huoqing /**
61800977f6SCai Huoqing  * kthread_run_on_cpu - create and wake a cpu bound thread.
62800977f6SCai Huoqing  * @threadfn: the function to run until signal_pending(current).
63800977f6SCai Huoqing  * @data: data ptr for @threadfn.
64800977f6SCai Huoqing  * @cpu: The cpu on which the thread should be bound,
65800977f6SCai Huoqing  * @namefmt: printf-style name for the thread. Format is restricted
66800977f6SCai Huoqing  *	     to "name.*%u". Code fills in cpu number.
67800977f6SCai Huoqing  *
68800977f6SCai Huoqing  * Description: Convenient wrapper for kthread_create_on_cpu()
69800977f6SCai Huoqing  * followed by wake_up_process().  Returns the kthread or
70800977f6SCai Huoqing  * ERR_PTR(-ENOMEM).
71800977f6SCai Huoqing  */
72800977f6SCai Huoqing static inline struct task_struct *
kthread_run_on_cpu(int (* threadfn)(void * data),void * data,unsigned int cpu,const char * namefmt)73800977f6SCai Huoqing kthread_run_on_cpu(int (*threadfn)(void *data), void *data,
74800977f6SCai Huoqing 			unsigned int cpu, const char *namefmt)
75800977f6SCai Huoqing {
76800977f6SCai Huoqing 	struct task_struct *p;
77800977f6SCai Huoqing 
78800977f6SCai Huoqing 	p = kthread_create_on_cpu(threadfn, data, cpu, namefmt);
79800977f6SCai Huoqing 	if (!IS_ERR(p))
80800977f6SCai Huoqing 		wake_up_process(p);
81800977f6SCai Huoqing 
82800977f6SCai Huoqing 	return p;
83800977f6SCai Huoqing }
84800977f6SCai Huoqing 
851da5c46fSOleg Nesterov void free_kthread_struct(struct task_struct *k);
861da177e4SLinus Torvalds void kthread_bind(struct task_struct *k, unsigned int cpu);
8725834c73SPeter Zijlstra void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
884d13f430SFrederic Weisbecker int kthread_affine_preferred(struct task_struct *p, const struct cpumask *mask);
891da177e4SLinus Torvalds int kthread_stop(struct task_struct *k);
906309727eSAndreas Gruenbacher int kthread_stop_put(struct task_struct *k);
912a1d4460SThomas Gleixner bool kthread_should_stop(void);
922a1d4460SThomas Gleixner bool kthread_should_park(void);
93ef73d6a4SArve Hjønnevåg bool kthread_should_stop_or_park(void);
948a32c441STejun Heo bool kthread_freezable_should_stop(bool *was_frozen);
9552782c92SJ. Bruce Fields void *kthread_func(struct task_struct *k);
9682805ab7STejun Heo void *kthread_data(struct task_struct *k);
97e700591aSPetr Mladek void *kthread_probe_data(struct task_struct *k);
982a1d4460SThomas Gleixner int kthread_park(struct task_struct *k);
992a1d4460SThomas Gleixner void kthread_unpark(struct task_struct *k);
1002a1d4460SThomas Gleixner void kthread_parkme(void);
101bbda86e9SEric W. Biederman void kthread_exit(long result) __noreturn;
102cead1855SEric W. Biederman void kthread_complete_and_exit(struct completion *, long) __noreturn;
1031da177e4SLinus Torvalds 
10473c27992SEric W. Biederman int kthreadd(void *unused);
10573c27992SEric W. Biederman extern struct task_struct *kthreadd_task;
106207205a2SEric Dumazet extern int tsk_fork_get_node(struct task_struct *tsk);
10773c27992SEric W. Biederman 
108b56c0d89STejun Heo /*
109b56c0d89STejun Heo  * Simple work processor based on kthread.
110b56c0d89STejun Heo  *
111b56c0d89STejun Heo  * This provides easier way to make use of kthreads.  A kthread_work
1123989144fSPetr Mladek  * can be queued and flushed using queue/kthread_flush_work()
113b56c0d89STejun Heo  * respectively.  Queued kthread_works are processed by a kthread
114b56c0d89STejun Heo  * running kthread_worker_fn().
115b56c0d89STejun Heo  */
116b56c0d89STejun Heo struct kthread_work;
117b56c0d89STejun Heo typedef void (*kthread_work_func_t)(struct kthread_work *work);
118fe5c3b69SKees Cook void kthread_delayed_work_timer_fn(struct timer_list *t);
119b56c0d89STejun Heo 
120dbf52682SPetr Mladek enum {
121dbf52682SPetr Mladek 	KTW_FREEZABLE		= 1 << 0,	/* freeze during suspend */
122dbf52682SPetr Mladek };
123dbf52682SPetr Mladek 
124b56c0d89STejun Heo struct kthread_worker {
125dbf52682SPetr Mladek 	unsigned int		flags;
126fe99a4f4SJulia Cartwright 	raw_spinlock_t		lock;
127b56c0d89STejun Heo 	struct list_head	work_list;
12822597dc3SPetr Mladek 	struct list_head	delayed_work_list;
129b56c0d89STejun Heo 	struct task_struct	*task;
13046f3d976STejun Heo 	struct kthread_work	*current_work;
131b56c0d89STejun Heo };
132b56c0d89STejun Heo 
133b56c0d89STejun Heo struct kthread_work {
134b56c0d89STejun Heo 	struct list_head	node;
135b56c0d89STejun Heo 	kthread_work_func_t	func;
13646f3d976STejun Heo 	struct kthread_worker	*worker;
13737be45d4SPetr Mladek 	/* Number of canceling calls that are running at the moment. */
13837be45d4SPetr Mladek 	int			canceling;
139b56c0d89STejun Heo };
140b56c0d89STejun Heo 
14122597dc3SPetr Mladek struct kthread_delayed_work {
14222597dc3SPetr Mladek 	struct kthread_work work;
14322597dc3SPetr Mladek 	struct timer_list timer;
14422597dc3SPetr Mladek };
14522597dc3SPetr Mladek 
146b56c0d89STejun Heo #define KTHREAD_WORK_INIT(work, fn)	{				\
147b56c0d89STejun Heo 	.node = LIST_HEAD_INIT((work).node),				\
148b56c0d89STejun Heo 	.func = (fn),							\
149b56c0d89STejun Heo 	}
150b56c0d89STejun Heo 
15122597dc3SPetr Mladek #define KTHREAD_DELAYED_WORK_INIT(dwork, fn) {				\
15222597dc3SPetr Mladek 	.work = KTHREAD_WORK_INIT((dwork).work, (fn)),			\
153841b86f3SKees Cook 	.timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
15422597dc3SPetr Mladek 				     TIMER_IRQSAFE),			\
15522597dc3SPetr Mladek 	}
15622597dc3SPetr Mladek 
157b56c0d89STejun Heo #define DEFINE_KTHREAD_WORK(work, fn)					\
158b56c0d89STejun Heo 	struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
159b56c0d89STejun Heo 
16022597dc3SPetr Mladek #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn)				\
16122597dc3SPetr Mladek 	struct kthread_delayed_work dwork =				\
16222597dc3SPetr Mladek 		KTHREAD_DELAYED_WORK_INIT(dwork, fn)
16322597dc3SPetr Mladek 
1643989144fSPetr Mladek extern void __kthread_init_worker(struct kthread_worker *worker,
1654f32e9b1SYong Zhang 			const char *name, struct lock_class_key *key);
1664f32e9b1SYong Zhang 
1673989144fSPetr Mladek #define kthread_init_worker(worker)					\
1684f32e9b1SYong Zhang 	do {								\
1694f32e9b1SYong Zhang 		static struct lock_class_key __key;			\
1703989144fSPetr Mladek 		__kthread_init_worker((worker), "("#worker")->lock", &__key); \
1714f32e9b1SYong Zhang 	} while (0)
1724f32e9b1SYong Zhang 
1733989144fSPetr Mladek #define kthread_init_work(work, fn)					\
1744f32e9b1SYong Zhang 	do {								\
1754f32e9b1SYong Zhang 		memset((work), 0, sizeof(struct kthread_work));		\
1764f32e9b1SYong Zhang 		INIT_LIST_HEAD(&(work)->node);				\
1774f32e9b1SYong Zhang 		(work)->func = (fn);					\
1784f32e9b1SYong Zhang 	} while (0)
179b56c0d89STejun Heo 
18022597dc3SPetr Mladek #define kthread_init_delayed_work(dwork, fn)				\
18122597dc3SPetr Mladek 	do {								\
18222597dc3SPetr Mladek 		kthread_init_work(&(dwork)->work, (fn));		\
183ad01423aSSebastian Andrzej Siewior 		timer_setup(&(dwork)->timer,				\
18498c985d7SPetr Mladek 			     kthread_delayed_work_timer_fn,		\
18598c985d7SPetr Mladek 			     TIMER_IRQSAFE);				\
18622597dc3SPetr Mladek 	} while (0)
18722597dc3SPetr Mladek 
188b56c0d89STejun Heo int kthread_worker_fn(void *worker_ptr);
189b56c0d89STejun Heo 
19041f70d8eSFrederic Weisbecker __printf(3, 4)
19141f70d8eSFrederic Weisbecker struct kthread_worker *kthread_create_worker_on_node(unsigned int flags,
19241f70d8eSFrederic Weisbecker 						     int node,
193dbf52682SPetr Mladek 						     const char namefmt[], ...);
194fbae2d44SPetr Mladek 
19541f70d8eSFrederic Weisbecker #define kthread_create_worker(flags, namefmt, ...) \
196*b04e317bSFrederic Weisbecker 	kthread_create_worker_on_node(flags, NUMA_NO_NODE, namefmt, ## __VA_ARGS__);
197*b04e317bSFrederic Weisbecker 
198*b04e317bSFrederic Weisbecker /**
199*b04e317bSFrederic Weisbecker  * kthread_run_worker - create and wake a kthread worker.
200*b04e317bSFrederic Weisbecker  * @flags: flags modifying the default behavior of the worker
201*b04e317bSFrederic Weisbecker  * @namefmt: printf-style name for the thread.
202*b04e317bSFrederic Weisbecker  *
203*b04e317bSFrederic Weisbecker  * Description: Convenient wrapper for kthread_create_worker() followed by
204*b04e317bSFrederic Weisbecker  * wake_up_process().  Returns the kthread_worker or ERR_PTR(-ENOMEM).
205*b04e317bSFrederic Weisbecker  */
206*b04e317bSFrederic Weisbecker #define kthread_run_worker(flags, namefmt, ...)					\
20741f70d8eSFrederic Weisbecker ({										\
20841f70d8eSFrederic Weisbecker 	struct kthread_worker *__kw						\
209*b04e317bSFrederic Weisbecker 		= kthread_create_worker(flags, namefmt, ## __VA_ARGS__);	\
21041f70d8eSFrederic Weisbecker 	if (!IS_ERR(__kw))							\
21141f70d8eSFrederic Weisbecker 		wake_up_process(__kw->task);					\
21241f70d8eSFrederic Weisbecker 	__kw;									\
21341f70d8eSFrederic Weisbecker })
21441f70d8eSFrederic Weisbecker 
21541f70d8eSFrederic Weisbecker struct kthread_worker *
21641f70d8eSFrederic Weisbecker kthread_create_worker_on_cpu(int cpu, unsigned int flags,
21741f70d8eSFrederic Weisbecker 			     const char namefmt[]);
21841f70d8eSFrederic Weisbecker 
219*b04e317bSFrederic Weisbecker /**
220*b04e317bSFrederic Weisbecker  * kthread_run_worker_on_cpu - create and wake a cpu bound kthread worker.
221*b04e317bSFrederic Weisbecker  * @cpu: CPU number
222*b04e317bSFrederic Weisbecker  * @flags: flags modifying the default behavior of the worker
223*b04e317bSFrederic Weisbecker  * @namefmt: printf-style name for the thread. Format is restricted
224*b04e317bSFrederic Weisbecker  *	     to "name.*%u". Code fills in cpu number.
225*b04e317bSFrederic Weisbecker  *
226*b04e317bSFrederic Weisbecker  * Description: Convenient wrapper for kthread_create_worker_on_cpu()
227*b04e317bSFrederic Weisbecker  * followed by wake_up_process().  Returns the kthread_worker or
228*b04e317bSFrederic Weisbecker  * ERR_PTR(-ENOMEM).
229*b04e317bSFrederic Weisbecker  */
230*b04e317bSFrederic Weisbecker static inline struct kthread_worker *
kthread_run_worker_on_cpu(int cpu,unsigned int flags,const char namefmt[])231*b04e317bSFrederic Weisbecker kthread_run_worker_on_cpu(int cpu, unsigned int flags,
232*b04e317bSFrederic Weisbecker 			  const char namefmt[])
233*b04e317bSFrederic Weisbecker {
234*b04e317bSFrederic Weisbecker 	struct kthread_worker *kw;
235*b04e317bSFrederic Weisbecker 
236*b04e317bSFrederic Weisbecker 	kw = kthread_create_worker_on_cpu(cpu, flags, namefmt);
237*b04e317bSFrederic Weisbecker 	if (!IS_ERR(kw))
238*b04e317bSFrederic Weisbecker 		wake_up_process(kw->task);
239*b04e317bSFrederic Weisbecker 
240*b04e317bSFrederic Weisbecker 	return kw;
241*b04e317bSFrederic Weisbecker }
242*b04e317bSFrederic Weisbecker 
2433989144fSPetr Mladek bool kthread_queue_work(struct kthread_worker *worker,
244b56c0d89STejun Heo 			struct kthread_work *work);
24522597dc3SPetr Mladek 
24622597dc3SPetr Mladek bool kthread_queue_delayed_work(struct kthread_worker *worker,
24722597dc3SPetr Mladek 				struct kthread_delayed_work *dwork,
24822597dc3SPetr Mladek 				unsigned long delay);
24922597dc3SPetr Mladek 
2509a6b06c8SPetr Mladek bool kthread_mod_delayed_work(struct kthread_worker *worker,
2519a6b06c8SPetr Mladek 			      struct kthread_delayed_work *dwork,
2529a6b06c8SPetr Mladek 			      unsigned long delay);
2539a6b06c8SPetr Mladek 
2543989144fSPetr Mladek void kthread_flush_work(struct kthread_work *work);
2553989144fSPetr Mladek void kthread_flush_worker(struct kthread_worker *worker);
256b56c0d89STejun Heo 
25737be45d4SPetr Mladek bool kthread_cancel_work_sync(struct kthread_work *work);
25837be45d4SPetr Mladek bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
25937be45d4SPetr Mladek 
26035033fe9SPetr Mladek void kthread_destroy_worker(struct kthread_worker *worker);
26135033fe9SPetr Mladek 
262f5678e7fSChristoph Hellwig void kthread_use_mm(struct mm_struct *mm);
263f5678e7fSChristoph Hellwig void kthread_unuse_mm(struct mm_struct *mm);
2649bf5b9ebSChristoph Hellwig 
2658af0c18aSSuren Baghdasaryan struct cgroup_subsys_state;
2668af0c18aSSuren Baghdasaryan 
2670b508bc9SShaohua Li #ifdef CONFIG_BLK_CGROUP
26805e3db95SShaohua Li void kthread_associate_blkcg(struct cgroup_subsys_state *css);
26905e3db95SShaohua Li struct cgroup_subsys_state *kthread_blkcg(void);
27005e3db95SShaohua Li #else
kthread_associate_blkcg(struct cgroup_subsys_state * css)27105e3db95SShaohua Li static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
27205e3db95SShaohua Li #endif
2731da177e4SLinus Torvalds #endif /* _LINUX_KTHREAD_H */
274