xref: /openbmc/linux/include/linux/kthread.h (revision be33db21)
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);
881da177e4SLinus Torvalds int kthread_stop(struct task_struct *k);
892a1d4460SThomas Gleixner bool kthread_should_stop(void);
902a1d4460SThomas Gleixner bool kthread_should_park(void);
91*ef73d6a4SArve Hjønnevåg bool kthread_should_stop_or_park(void);
928a32c441STejun Heo bool kthread_freezable_should_stop(bool *was_frozen);
9352782c92SJ. Bruce Fields void *kthread_func(struct task_struct *k);
9482805ab7STejun Heo void *kthread_data(struct task_struct *k);
95e700591aSPetr Mladek void *kthread_probe_data(struct task_struct *k);
962a1d4460SThomas Gleixner int kthread_park(struct task_struct *k);
972a1d4460SThomas Gleixner void kthread_unpark(struct task_struct *k);
982a1d4460SThomas Gleixner void kthread_parkme(void);
99bbda86e9SEric W. Biederman void kthread_exit(long result) __noreturn;
100cead1855SEric W. Biederman void kthread_complete_and_exit(struct completion *, long) __noreturn;
1011da177e4SLinus Torvalds 
10273c27992SEric W. Biederman int kthreadd(void *unused);
10373c27992SEric W. Biederman extern struct task_struct *kthreadd_task;
104207205a2SEric Dumazet extern int tsk_fork_get_node(struct task_struct *tsk);
10573c27992SEric W. Biederman 
106b56c0d89STejun Heo /*
107b56c0d89STejun Heo  * Simple work processor based on kthread.
108b56c0d89STejun Heo  *
109b56c0d89STejun Heo  * This provides easier way to make use of kthreads.  A kthread_work
1103989144fSPetr Mladek  * can be queued and flushed using queue/kthread_flush_work()
111b56c0d89STejun Heo  * respectively.  Queued kthread_works are processed by a kthread
112b56c0d89STejun Heo  * running kthread_worker_fn().
113b56c0d89STejun Heo  */
114b56c0d89STejun Heo struct kthread_work;
115b56c0d89STejun Heo typedef void (*kthread_work_func_t)(struct kthread_work *work);
116fe5c3b69SKees Cook void kthread_delayed_work_timer_fn(struct timer_list *t);
117b56c0d89STejun Heo 
118dbf52682SPetr Mladek enum {
119dbf52682SPetr Mladek 	KTW_FREEZABLE		= 1 << 0,	/* freeze during suspend */
120dbf52682SPetr Mladek };
121dbf52682SPetr Mladek 
122b56c0d89STejun Heo struct kthread_worker {
123dbf52682SPetr Mladek 	unsigned int		flags;
124fe99a4f4SJulia Cartwright 	raw_spinlock_t		lock;
125b56c0d89STejun Heo 	struct list_head	work_list;
12622597dc3SPetr Mladek 	struct list_head	delayed_work_list;
127b56c0d89STejun Heo 	struct task_struct	*task;
12846f3d976STejun Heo 	struct kthread_work	*current_work;
129b56c0d89STejun Heo };
130b56c0d89STejun Heo 
131b56c0d89STejun Heo struct kthread_work {
132b56c0d89STejun Heo 	struct list_head	node;
133b56c0d89STejun Heo 	kthread_work_func_t	func;
13446f3d976STejun Heo 	struct kthread_worker	*worker;
13537be45d4SPetr Mladek 	/* Number of canceling calls that are running at the moment. */
13637be45d4SPetr Mladek 	int			canceling;
137b56c0d89STejun Heo };
138b56c0d89STejun Heo 
13922597dc3SPetr Mladek struct kthread_delayed_work {
14022597dc3SPetr Mladek 	struct kthread_work work;
14122597dc3SPetr Mladek 	struct timer_list timer;
14222597dc3SPetr Mladek };
14322597dc3SPetr Mladek 
144b56c0d89STejun Heo #define KTHREAD_WORK_INIT(work, fn)	{				\
145b56c0d89STejun Heo 	.node = LIST_HEAD_INIT((work).node),				\
146b56c0d89STejun Heo 	.func = (fn),							\
147b56c0d89STejun Heo 	}
148b56c0d89STejun Heo 
14922597dc3SPetr Mladek #define KTHREAD_DELAYED_WORK_INIT(dwork, fn) {				\
15022597dc3SPetr Mladek 	.work = KTHREAD_WORK_INIT((dwork).work, (fn)),			\
151841b86f3SKees Cook 	.timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
15222597dc3SPetr Mladek 				     TIMER_IRQSAFE),			\
15322597dc3SPetr Mladek 	}
15422597dc3SPetr Mladek 
155b56c0d89STejun Heo #define DEFINE_KTHREAD_WORK(work, fn)					\
156b56c0d89STejun Heo 	struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
157b56c0d89STejun Heo 
15822597dc3SPetr Mladek #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn)				\
15922597dc3SPetr Mladek 	struct kthread_delayed_work dwork =				\
16022597dc3SPetr Mladek 		KTHREAD_DELAYED_WORK_INIT(dwork, fn)
16122597dc3SPetr Mladek 
1623989144fSPetr Mladek extern void __kthread_init_worker(struct kthread_worker *worker,
1634f32e9b1SYong Zhang 			const char *name, struct lock_class_key *key);
1644f32e9b1SYong Zhang 
1653989144fSPetr Mladek #define kthread_init_worker(worker)					\
1664f32e9b1SYong Zhang 	do {								\
1674f32e9b1SYong Zhang 		static struct lock_class_key __key;			\
1683989144fSPetr Mladek 		__kthread_init_worker((worker), "("#worker")->lock", &__key); \
1694f32e9b1SYong Zhang 	} while (0)
1704f32e9b1SYong Zhang 
1713989144fSPetr Mladek #define kthread_init_work(work, fn)					\
1724f32e9b1SYong Zhang 	do {								\
1734f32e9b1SYong Zhang 		memset((work), 0, sizeof(struct kthread_work));		\
1744f32e9b1SYong Zhang 		INIT_LIST_HEAD(&(work)->node);				\
1754f32e9b1SYong Zhang 		(work)->func = (fn);					\
1764f32e9b1SYong Zhang 	} while (0)
177b56c0d89STejun Heo 
17822597dc3SPetr Mladek #define kthread_init_delayed_work(dwork, fn)				\
17922597dc3SPetr Mladek 	do {								\
18022597dc3SPetr Mladek 		kthread_init_work(&(dwork)->work, (fn));		\
181ad01423aSSebastian Andrzej Siewior 		timer_setup(&(dwork)->timer,				\
18298c985d7SPetr Mladek 			     kthread_delayed_work_timer_fn,		\
18398c985d7SPetr Mladek 			     TIMER_IRQSAFE);				\
18422597dc3SPetr Mladek 	} while (0)
18522597dc3SPetr Mladek 
186b56c0d89STejun Heo int kthread_worker_fn(void *worker_ptr);
187b56c0d89STejun Heo 
188dbf52682SPetr Mladek __printf(2, 3)
189fbae2d44SPetr Mladek struct kthread_worker *
190dbf52682SPetr Mladek kthread_create_worker(unsigned int flags, const char namefmt[], ...);
191fbae2d44SPetr Mladek 
192c0b942a7SNicolas Iooss __printf(3, 4) struct kthread_worker *
193dbf52682SPetr Mladek kthread_create_worker_on_cpu(int cpu, unsigned int flags,
194dbf52682SPetr Mladek 			     const char namefmt[], ...);
195fbae2d44SPetr Mladek 
1963989144fSPetr Mladek bool kthread_queue_work(struct kthread_worker *worker,
197b56c0d89STejun Heo 			struct kthread_work *work);
19822597dc3SPetr Mladek 
19922597dc3SPetr Mladek bool kthread_queue_delayed_work(struct kthread_worker *worker,
20022597dc3SPetr Mladek 				struct kthread_delayed_work *dwork,
20122597dc3SPetr Mladek 				unsigned long delay);
20222597dc3SPetr Mladek 
2039a6b06c8SPetr Mladek bool kthread_mod_delayed_work(struct kthread_worker *worker,
2049a6b06c8SPetr Mladek 			      struct kthread_delayed_work *dwork,
2059a6b06c8SPetr Mladek 			      unsigned long delay);
2069a6b06c8SPetr Mladek 
2073989144fSPetr Mladek void kthread_flush_work(struct kthread_work *work);
2083989144fSPetr Mladek void kthread_flush_worker(struct kthread_worker *worker);
209b56c0d89STejun Heo 
21037be45d4SPetr Mladek bool kthread_cancel_work_sync(struct kthread_work *work);
21137be45d4SPetr Mladek bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
21237be45d4SPetr Mladek 
21335033fe9SPetr Mladek void kthread_destroy_worker(struct kthread_worker *worker);
21435033fe9SPetr Mladek 
215f5678e7fSChristoph Hellwig void kthread_use_mm(struct mm_struct *mm);
216f5678e7fSChristoph Hellwig void kthread_unuse_mm(struct mm_struct *mm);
2179bf5b9ebSChristoph Hellwig 
2188af0c18aSSuren Baghdasaryan struct cgroup_subsys_state;
2198af0c18aSSuren Baghdasaryan 
2200b508bc9SShaohua Li #ifdef CONFIG_BLK_CGROUP
22105e3db95SShaohua Li void kthread_associate_blkcg(struct cgroup_subsys_state *css);
22205e3db95SShaohua Li struct cgroup_subsys_state *kthread_blkcg(void);
22305e3db95SShaohua Li #else
kthread_associate_blkcg(struct cgroup_subsys_state * css)22405e3db95SShaohua Li static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
22505e3db95SShaohua Li #endif
2261da177e4SLinus Torvalds #endif /* _LINUX_KTHREAD_H */
227