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 /* opaque kthread data */
11 struct kthread;
12
13 /*
14 * When "(p->flags & PF_KTHREAD)" is set the task is a kthread and will
15 * always remain a kthread. For kthreads p->worker_private always
16 * points to a struct kthread. For tasks that are not kthreads
17 * p->worker_private is used to point to other things.
18 *
19 * Return NULL for any task that is not a kthread.
20 */
tsk_is_kthread(struct task_struct * p)21 static inline struct kthread *tsk_is_kthread(struct task_struct *p)
22 {
23 if (p->flags & PF_KTHREAD)
24 return p->worker_private;
25 return NULL;
26 }
27
28 __printf(4, 5)
29 struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
30 void *data,
31 int node,
32 const char namefmt[], ...);
33
34 /**
35 * kthread_create - create a kthread on the current node
36 * @threadfn: the function to run in the thread
37 * @data: data pointer for @threadfn()
38 * @namefmt: printf-style format string for the thread name
39 * @arg: arguments for @namefmt.
40 *
41 * This macro will create a kthread on the current node, leaving it in
42 * the stopped state. This is just a helper for kthread_create_on_node();
43 * see the documentation there for more details.
44 */
45 #define kthread_create(threadfn, data, namefmt, arg...) \
46 kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
47
48
49 struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
50 void *data,
51 unsigned int cpu,
52 const char *namefmt);
53
54 void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk);
55 bool set_kthread_struct(struct task_struct *p);
56
57 void kthread_set_per_cpu(struct task_struct *k, int cpu);
58 bool kthread_is_per_cpu(struct task_struct *k);
59
60 /**
61 * kthread_run - create and wake a thread.
62 * @threadfn: the function to run until signal_pending(current).
63 * @data: data ptr for @threadfn.
64 * @namefmt: printf-style name for the thread.
65 *
66 * Description: Convenient wrapper for kthread_create() followed by
67 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
68 */
69 #define kthread_run(threadfn, data, namefmt, ...) \
70 ({ \
71 struct task_struct *__k \
72 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
73 if (!IS_ERR(__k)) \
74 wake_up_process(__k); \
75 __k; \
76 })
77
78 /**
79 * kthread_run_on_cpu - create and wake a cpu bound thread.
80 * @threadfn: the function to run until signal_pending(current).
81 * @data: data ptr for @threadfn.
82 * @cpu: The cpu on which the thread should be bound,
83 * @namefmt: printf-style name for the thread. Format is restricted
84 * to "name.*%u". Code fills in cpu number.
85 *
86 * Description: Convenient wrapper for kthread_create_on_cpu()
87 * followed by wake_up_process(). Returns the kthread or
88 * ERR_PTR(-ENOMEM).
89 */
90 static inline struct task_struct *
kthread_run_on_cpu(int (* threadfn)(void * data),void * data,unsigned int cpu,const char * namefmt)91 kthread_run_on_cpu(int (*threadfn)(void *data), void *data,
92 unsigned int cpu, const char *namefmt)
93 {
94 struct task_struct *p;
95
96 p = kthread_create_on_cpu(threadfn, data, cpu, namefmt);
97 if (!IS_ERR(p))
98 wake_up_process(p);
99
100 return p;
101 }
102
103 void free_kthread_struct(struct task_struct *k);
104 void kthread_bind(struct task_struct *k, unsigned int cpu);
105 void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
106 int kthread_affine_preferred(struct task_struct *p, const struct cpumask *mask);
107 int kthread_stop(struct task_struct *k);
108 int kthread_stop_put(struct task_struct *k);
109 bool kthread_should_stop(void);
110 bool kthread_should_park(void);
111 bool kthread_should_stop_or_park(void);
112 bool kthread_freezable_should_stop(bool *was_frozen);
113 void *kthread_func(struct task_struct *k);
114 void *kthread_data(struct task_struct *k);
115 void *kthread_probe_data(struct task_struct *k);
116 int kthread_park(struct task_struct *k);
117 void kthread_unpark(struct task_struct *k);
118 void kthread_parkme(void);
119 #define kthread_exit(result) do_exit(result)
120 void kthread_complete_and_exit(struct completion *, long) __noreturn;
121 int kthreads_update_housekeeping(void);
122 void kthread_do_exit(struct kthread *, long);
123
124 int kthreadd(void *unused);
125 extern struct task_struct *kthreadd_task;
126 extern int tsk_fork_get_node(struct task_struct *tsk);
127
128 /*
129 * Simple work processor based on kthread.
130 *
131 * This provides easier way to make use of kthreads. A kthread_work
132 * can be queued and flushed using queue/kthread_flush_work()
133 * respectively. Queued kthread_works are processed by a kthread
134 * running kthread_worker_fn().
135 */
136 struct kthread_work;
137 typedef void (*kthread_work_func_t)(struct kthread_work *work);
138 void kthread_delayed_work_timer_fn(struct timer_list *t);
139
140 enum {
141 KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
142 };
143
144 struct kthread_worker {
145 unsigned int flags;
146 raw_spinlock_t lock;
147 struct list_head work_list;
148 struct list_head delayed_work_list;
149 struct task_struct *task;
150 struct kthread_work *current_work;
151 };
152
153 struct kthread_work {
154 struct list_head node;
155 kthread_work_func_t func;
156 struct kthread_worker *worker;
157 /* Number of canceling calls that are running at the moment. */
158 int canceling;
159 };
160
161 struct kthread_delayed_work {
162 struct kthread_work work;
163 struct timer_list timer;
164 };
165
166 #define KTHREAD_WORK_INIT(work, fn) { \
167 .node = LIST_HEAD_INIT((work).node), \
168 .func = (fn), \
169 }
170
171 #define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
172 .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
173 .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
174 TIMER_IRQSAFE), \
175 }
176
177 #define DEFINE_KTHREAD_WORK(work, fn) \
178 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
179
180 #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
181 struct kthread_delayed_work dwork = \
182 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
183
184 extern void __kthread_init_worker(struct kthread_worker *worker,
185 const char *name, struct lock_class_key *key);
186
187 #define kthread_init_worker(worker) \
188 do { \
189 static struct lock_class_key __key; \
190 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
191 } while (0)
192
193 #define kthread_init_work(work, fn) \
194 do { \
195 memset((work), 0, sizeof(struct kthread_work)); \
196 INIT_LIST_HEAD(&(work)->node); \
197 (work)->func = (fn); \
198 } while (0)
199
200 #define kthread_init_delayed_work(dwork, fn) \
201 do { \
202 kthread_init_work(&(dwork)->work, (fn)); \
203 timer_setup(&(dwork)->timer, \
204 kthread_delayed_work_timer_fn, \
205 TIMER_IRQSAFE); \
206 } while (0)
207
208 int kthread_worker_fn(void *worker_ptr);
209
210 __printf(3, 4)
211 struct kthread_worker *kthread_create_worker_on_node(unsigned int flags,
212 int node,
213 const char namefmt[], ...);
214
215 #define kthread_create_worker(flags, namefmt, ...) \
216 kthread_create_worker_on_node(flags, NUMA_NO_NODE, namefmt, ## __VA_ARGS__);
217
218 /**
219 * kthread_run_worker - create and wake a kthread worker.
220 * @flags: flags modifying the default behavior of the worker
221 * @namefmt: printf-style name for the thread.
222 *
223 * Description: Convenient wrapper for kthread_create_worker() followed by
224 * wake_up_process(). Returns the kthread_worker or ERR_PTR(-ENOMEM).
225 */
226 #define kthread_run_worker(flags, namefmt, ...) \
227 ({ \
228 struct kthread_worker *__kw \
229 = kthread_create_worker(flags, namefmt, ## __VA_ARGS__); \
230 if (!IS_ERR(__kw)) \
231 wake_up_process(__kw->task); \
232 __kw; \
233 })
234
235 struct kthread_worker *
236 kthread_create_worker_on_cpu(int cpu, unsigned int flags,
237 const char namefmt[]);
238
239 /**
240 * kthread_run_worker_on_cpu - create and wake a cpu bound kthread worker.
241 * @cpu: CPU number
242 * @flags: flags modifying the default behavior of the worker
243 * @namefmt: printf-style name for the thread. Format is restricted
244 * to "name.*%u". Code fills in cpu number.
245 *
246 * Description: Convenient wrapper for kthread_create_worker_on_cpu()
247 * followed by wake_up_process(). Returns the kthread_worker or
248 * ERR_PTR(-ENOMEM).
249 */
250 static inline struct kthread_worker *
kthread_run_worker_on_cpu(int cpu,unsigned int flags,const char namefmt[])251 kthread_run_worker_on_cpu(int cpu, unsigned int flags,
252 const char namefmt[])
253 {
254 struct kthread_worker *kw;
255
256 kw = kthread_create_worker_on_cpu(cpu, flags, namefmt);
257 if (!IS_ERR(kw))
258 wake_up_process(kw->task);
259
260 return kw;
261 }
262
263 bool kthread_queue_work(struct kthread_worker *worker,
264 struct kthread_work *work);
265
266 bool kthread_queue_delayed_work(struct kthread_worker *worker,
267 struct kthread_delayed_work *dwork,
268 unsigned long delay);
269
270 bool kthread_mod_delayed_work(struct kthread_worker *worker,
271 struct kthread_delayed_work *dwork,
272 unsigned long delay);
273
274 void kthread_flush_work(struct kthread_work *work);
275 void kthread_flush_worker(struct kthread_worker *worker);
276
277 bool kthread_cancel_work_sync(struct kthread_work *work);
278 bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
279
280 void kthread_destroy_worker(struct kthread_worker *worker);
281
282 void kthread_use_mm(struct mm_struct *mm);
283 void kthread_unuse_mm(struct mm_struct *mm);
284
285 struct cgroup_subsys_state;
286
287 #ifdef CONFIG_BLK_CGROUP
288 void kthread_associate_blkcg(struct cgroup_subsys_state *css);
289 struct cgroup_subsys_state *kthread_blkcg(void);
290 #else
kthread_associate_blkcg(struct cgroup_subsys_state * css)291 static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
292 #endif
293 #endif /* _LINUX_KTHREAD_H */
294