Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
8struct mm_struct;
9
10__printf(4, 5)
11struct 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
31struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
32 void *data,
33 unsigned int cpu,
34 const char *namefmt);
35
36void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk);
37bool set_kthread_struct(struct task_struct *p);
38
39void kthread_set_per_cpu(struct task_struct *k, int cpu);
40bool 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 */
72static inline struct task_struct *
73kthread_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
85void free_kthread_struct(struct task_struct *k);
86void kthread_bind(struct task_struct *k, unsigned int cpu);
87void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
88int kthread_affine_preferred(struct task_struct *p, const struct cpumask *mask);
89int kthread_stop(struct task_struct *k);
90int kthread_stop_put(struct task_struct *k);
91bool kthread_should_stop(void);
92bool kthread_should_park(void);
93bool kthread_should_stop_or_park(void);
94bool kthread_freezable_should_stop(bool *was_frozen);
95void *kthread_func(struct task_struct *k);
96void *kthread_data(struct task_struct *k);
97void *kthread_probe_data(struct task_struct *k);
98int kthread_park(struct task_struct *k);
99void kthread_unpark(struct task_struct *k);
100void kthread_parkme(void);
101void kthread_exit(long result) __noreturn;
102void kthread_complete_and_exit(struct completion *, long) __noreturn;
103int kthreads_update_housekeeping(void);
104
105int kthreadd(void *unused);
106extern struct task_struct *kthreadd_task;
107extern int tsk_fork_get_node(struct task_struct *tsk);
108
109/*
110 * Simple work processor based on kthread.
111 *
112 * This provides easier way to make use of kthreads. A kthread_work
113 * can be queued and flushed using queue/kthread_flush_work()
114 * respectively. Queued kthread_works are processed by a kthread
115 * running kthread_worker_fn().
116 */
117struct kthread_work;
118typedef void (*kthread_work_func_t)(struct kthread_work *work);
119void kthread_delayed_work_timer_fn(struct timer_list *t);
120
121enum {
122 KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
123};
124
125struct kthread_worker {
126 unsigned int flags;
127 raw_spinlock_t lock;
128 struct list_head work_list;
129 struct list_head delayed_work_list;
130 struct task_struct *task;
131 struct kthread_work *current_work;
132};
133
134struct kthread_work {
135 struct list_head node;
136 kthread_work_func_t func;
137 struct kthread_worker *worker;
138 /* Number of canceling calls that are running at the moment. */
139 int canceling;
140};
141
142struct kthread_delayed_work {
143 struct kthread_work work;
144 struct timer_list timer;
145};
146
147#define KTHREAD_WORK_INIT(work, fn) { \
148 .node = LIST_HEAD_INIT((work).node), \
149 .func = (fn), \
150 }
151
152#define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
153 .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
154 .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
155 TIMER_IRQSAFE), \
156 }
157
158#define DEFINE_KTHREAD_WORK(work, fn) \
159 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
160
161#define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
162 struct kthread_delayed_work dwork = \
163 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
164
165extern void __kthread_init_worker(struct kthread_worker *worker,
166 const char *name, struct lock_class_key *key);
167
168#define kthread_init_worker(worker) \
169 do { \
170 static struct lock_class_key __key; \
171 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
172 } while (0)
173
174#define kthread_init_work(work, fn) \
175 do { \
176 memset((work), 0, sizeof(struct kthread_work)); \
177 INIT_LIST_HEAD(&(work)->node); \
178 (work)->func = (fn); \
179 } while (0)
180
181#define kthread_init_delayed_work(dwork, fn) \
182 do { \
183 kthread_init_work(&(dwork)->work, (fn)); \
184 timer_setup(&(dwork)->timer, \
185 kthread_delayed_work_timer_fn, \
186 TIMER_IRQSAFE); \
187 } while (0)
188
189int kthread_worker_fn(void *worker_ptr);
190
191__printf(3, 4)
192struct kthread_worker *kthread_create_worker_on_node(unsigned int flags,
193 int node,
194 const char namefmt[], ...);
195
196#define kthread_create_worker(flags, namefmt, ...) \
197 kthread_create_worker_on_node(flags, NUMA_NO_NODE, namefmt, ## __VA_ARGS__);
198
199/**
200 * kthread_run_worker - create and wake a kthread worker.
201 * @flags: flags modifying the default behavior of the worker
202 * @namefmt: printf-style name for the thread.
203 *
204 * Description: Convenient wrapper for kthread_create_worker() followed by
205 * wake_up_process(). Returns the kthread_worker or ERR_PTR(-ENOMEM).
206 */
207#define kthread_run_worker(flags, namefmt, ...) \
208({ \
209 struct kthread_worker *__kw \
210 = kthread_create_worker(flags, namefmt, ## __VA_ARGS__); \
211 if (!IS_ERR(__kw)) \
212 wake_up_process(__kw->task); \
213 __kw; \
214})
215
216struct kthread_worker *
217kthread_create_worker_on_cpu(int cpu, unsigned int flags,
218 const char namefmt[]);
219
220/**
221 * kthread_run_worker_on_cpu - create and wake a cpu bound kthread worker.
222 * @cpu: CPU number
223 * @flags: flags modifying the default behavior of the worker
224 * @namefmt: printf-style name for the thread. Format is restricted
225 * to "name.*%u". Code fills in cpu number.
226 *
227 * Description: Convenient wrapper for kthread_create_worker_on_cpu()
228 * followed by wake_up_process(). Returns the kthread_worker or
229 * ERR_PTR(-ENOMEM).
230 */
231static inline struct kthread_worker *
232kthread_run_worker_on_cpu(int cpu, unsigned int flags,
233 const char namefmt[])
234{
235 struct kthread_worker *kw;
236
237 kw = kthread_create_worker_on_cpu(cpu, flags, namefmt);
238 if (!IS_ERR(kw))
239 wake_up_process(kw->task);
240
241 return kw;
242}
243
244bool kthread_queue_work(struct kthread_worker *worker,
245 struct kthread_work *work);
246
247bool kthread_queue_delayed_work(struct kthread_worker *worker,
248 struct kthread_delayed_work *dwork,
249 unsigned long delay);
250
251bool kthread_mod_delayed_work(struct kthread_worker *worker,
252 struct kthread_delayed_work *dwork,
253 unsigned long delay);
254
255void kthread_flush_work(struct kthread_work *work);
256void kthread_flush_worker(struct kthread_worker *worker);
257
258bool kthread_cancel_work_sync(struct kthread_work *work);
259bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
260
261void kthread_destroy_worker(struct kthread_worker *worker);
262
263void kthread_use_mm(struct mm_struct *mm);
264void kthread_unuse_mm(struct mm_struct *mm);
265
266struct cgroup_subsys_state;
267
268#ifdef CONFIG_BLK_CGROUP
269void kthread_associate_blkcg(struct cgroup_subsys_state *css);
270struct cgroup_subsys_state *kthread_blkcg(void);
271#else
272static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
273#endif
274#endif /* _LINUX_KTHREAD_H */