• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_KTHREAD_H
3 #define _LINUX_KTHREAD_H
4 /* Simple interface for creating and stopping kernel threads without mess. */
5 #include <linux/err.h>
6 #include <linux/sched.h>
7 
8 struct mm_struct;
9 
10 __printf(4, 5)
11 struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
12 					   void *data,
13 					   int node,
14 					   const char namefmt[], ...);
15 
16 /**
17  * kthread_create - create a kthread on the current node
18  * @threadfn: the function to run in the thread
19  * @data: data pointer for @threadfn()
20  * @namefmt: printf-style format string for the thread name
21  * @arg: arguments for @namefmt.
22  *
23  * This macro will create a kthread on the current node, leaving it in
24  * the stopped state.  This is just a helper for kthread_create_on_node();
25  * see the documentation there for more details.
26  */
27 #define kthread_create(threadfn, data, namefmt, arg...) \
28 	kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
29 
30 
31 struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
32 					  void *data,
33 					  unsigned int cpu,
34 					  const char *namefmt);
35 
36 void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk);
37 void set_kthread_struct(struct task_struct *p);
38 
39 void kthread_set_per_cpu(struct task_struct *k, int cpu);
40 bool kthread_is_per_cpu(struct task_struct *k);
41 
42 /**
43  * kthread_run - create and wake a thread.
44  * @threadfn: the function to run until signal_pending(current).
45  * @data: data ptr for @threadfn.
46  * @namefmt: printf-style name for the thread.
47  *
48  * Description: Convenient wrapper for kthread_create() followed by
49  * wake_up_process().  Returns the kthread or ERR_PTR(-ENOMEM).
50  */
51 #define kthread_run(threadfn, data, namefmt, ...)			   \
52 ({									   \
53 	struct task_struct *__k						   \
54 		= kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
55 	if (!IS_ERR(__k))						   \
56 		wake_up_process(__k);					   \
57 	__k;								   \
58 })
59 
60 /**
61  * kthread_run_on_cpu - create and wake a cpu bound thread.
62  * @threadfn: the function to run until signal_pending(current).
63  * @data: data ptr for @threadfn.
64  * @cpu: The cpu on which the thread should be bound,
65  * @namefmt: printf-style name for the thread. Format is restricted
66  *	     to "name.*%u". Code fills in cpu number.
67  *
68  * Description: Convenient wrapper for kthread_create_on_cpu()
69  * followed by wake_up_process().  Returns the kthread or
70  * ERR_PTR(-ENOMEM).
71  */
72 static inline struct task_struct *
kthread_run_on_cpu(int (* threadfn)(void * data),void * data,unsigned int cpu,const char * namefmt)73 kthread_run_on_cpu(int (*threadfn)(void *data), void *data,
74 			unsigned int cpu, const char *namefmt)
75 {
76 	struct task_struct *p;
77 
78 	p = kthread_create_on_cpu(threadfn, data, cpu, namefmt);
79 	if (!IS_ERR(p))
80 		wake_up_process(p);
81 
82 	return p;
83 }
84 
85 void free_kthread_struct(struct task_struct *k);
86 void kthread_bind(struct task_struct *k, unsigned int cpu);
87 void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
88 int kthread_stop(struct task_struct *k);
89 bool kthread_should_stop(void);
90 bool kthread_should_park(void);
91 bool __kthread_should_park(struct task_struct *k);
92 bool kthread_freezable_should_stop(bool *was_frozen);
93 void *kthread_func(struct task_struct *k);
94 void *kthread_data(struct task_struct *k);
95 void *kthread_probe_data(struct task_struct *k);
96 int kthread_park(struct task_struct *k);
97 void kthread_unpark(struct task_struct *k);
98 void kthread_parkme(void);
99 
100 int kthreadd(void *unused);
101 extern struct task_struct *kthreadd_task;
102 extern int tsk_fork_get_node(struct task_struct *tsk);
103 
104 /*
105  * Simple work processor based on kthread.
106  *
107  * This provides easier way to make use of kthreads.  A kthread_work
108  * can be queued and flushed using queue/kthread_flush_work()
109  * respectively.  Queued kthread_works are processed by a kthread
110  * running kthread_worker_fn().
111  */
112 struct kthread_work;
113 typedef void (*kthread_work_func_t)(struct kthread_work *work);
114 void kthread_delayed_work_timer_fn(struct timer_list *t);
115 
116 enum {
117 	KTW_FREEZABLE		= 1 << 0,	/* freeze during suspend */
118 };
119 
120 struct kthread_worker {
121 	unsigned int		flags;
122 	raw_spinlock_t		lock;
123 	struct list_head	work_list;
124 	struct list_head	delayed_work_list;
125 	struct task_struct	*task;
126 	struct kthread_work	*current_work;
127 };
128 
129 struct kthread_work {
130 	struct list_head	node;
131 	kthread_work_func_t	func;
132 	struct kthread_worker	*worker;
133 	/* Number of canceling calls that are running at the moment. */
134 	int			canceling;
135 };
136 
137 struct kthread_delayed_work {
138 	struct kthread_work work;
139 	struct timer_list timer;
140 };
141 
142 #define KTHREAD_WORKER_INIT(worker)	{				\
143 	.lock = __RAW_SPIN_LOCK_UNLOCKED((worker).lock),		\
144 	.work_list = LIST_HEAD_INIT((worker).work_list),		\
145 	.delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
146 	}
147 
148 #define KTHREAD_WORK_INIT(work, fn)	{				\
149 	.node = LIST_HEAD_INIT((work).node),				\
150 	.func = (fn),							\
151 	}
152 
153 #define KTHREAD_DELAYED_WORK_INIT(dwork, fn) {				\
154 	.work = KTHREAD_WORK_INIT((dwork).work, (fn)),			\
155 	.timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
156 				     TIMER_IRQSAFE),			\
157 	}
158 
159 #define DEFINE_KTHREAD_WORKER(worker)					\
160 	struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
161 
162 #define DEFINE_KTHREAD_WORK(work, fn)					\
163 	struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
164 
165 #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn)				\
166 	struct kthread_delayed_work dwork =				\
167 		KTHREAD_DELAYED_WORK_INIT(dwork, fn)
168 
169 /*
170  * kthread_worker.lock needs its own lockdep class key when defined on
171  * stack with lockdep enabled.  Use the following macros in such cases.
172  */
173 #ifdef CONFIG_LOCKDEP
174 # define KTHREAD_WORKER_INIT_ONSTACK(worker)				\
175 	({ kthread_init_worker(&worker); worker; })
176 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker)				\
177 	struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
178 #else
179 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
180 #endif
181 
182 extern void __kthread_init_worker(struct kthread_worker *worker,
183 			const char *name, struct lock_class_key *key);
184 
185 #define kthread_init_worker(worker)					\
186 	do {								\
187 		static struct lock_class_key __key;			\
188 		__kthread_init_worker((worker), "("#worker")->lock", &__key); \
189 	} while (0)
190 
191 #define kthread_init_work(work, fn)					\
192 	do {								\
193 		memset((work), 0, sizeof(struct kthread_work));		\
194 		INIT_LIST_HEAD(&(work)->node);				\
195 		(work)->func = (fn);					\
196 	} while (0)
197 
198 #define kthread_init_delayed_work(dwork, fn)				\
199 	do {								\
200 		kthread_init_work(&(dwork)->work, (fn));		\
201 		timer_setup(&(dwork)->timer,				\
202 			     kthread_delayed_work_timer_fn,		\
203 			     TIMER_IRQSAFE);				\
204 	} while (0)
205 
206 int kthread_worker_fn(void *worker_ptr);
207 
208 __printf(2, 3)
209 struct kthread_worker *
210 kthread_create_worker(unsigned int flags, const char namefmt[], ...);
211 
212 __printf(3, 4) struct kthread_worker *
213 kthread_create_worker_on_cpu(int cpu, unsigned int flags,
214 			     const char namefmt[], ...);
215 
216 bool kthread_queue_work(struct kthread_worker *worker,
217 			struct kthread_work *work);
218 
219 bool kthread_queue_delayed_work(struct kthread_worker *worker,
220 				struct kthread_delayed_work *dwork,
221 				unsigned long delay);
222 
223 bool kthread_mod_delayed_work(struct kthread_worker *worker,
224 			      struct kthread_delayed_work *dwork,
225 			      unsigned long delay);
226 
227 void kthread_flush_work(struct kthread_work *work);
228 void kthread_flush_worker(struct kthread_worker *worker);
229 
230 bool kthread_cancel_work_sync(struct kthread_work *work);
231 bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
232 
233 void kthread_destroy_worker(struct kthread_worker *worker);
234 
235 void kthread_use_mm(struct mm_struct *mm);
236 void kthread_unuse_mm(struct mm_struct *mm);
237 
238 struct cgroup_subsys_state;
239 
240 #ifdef CONFIG_BLK_CGROUP
241 void kthread_associate_blkcg(struct cgroup_subsys_state *css);
242 struct cgroup_subsys_state *kthread_blkcg(void);
243 #else
kthread_associate_blkcg(struct cgroup_subsys_state * css)244 static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
kthread_blkcg(void)245 static inline struct cgroup_subsys_state *kthread_blkcg(void)
246 {
247 	return NULL;
248 }
249 #endif
250 #endif /* _LINUX_KTHREAD_H */
251