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. */
6 #include <linux/sched.h>
11 struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
14 const char namefmt[], ...);
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.
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.
27 #define kthread_create(threadfn, data, namefmt, arg...) \
28 kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
31 struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
36 void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk);
37 bool set_kthread_struct(struct task_struct *p);
39 void kthread_set_per_cpu(struct task_struct *k, int cpu);
40 bool kthread_is_per_cpu(struct task_struct *k);
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.
48 * Description: Convenient wrapper for kthread_create() followed by
49 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
51 #define kthread_run(threadfn, data, namefmt, ...) \
53 struct task_struct *__k \
54 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
56 wake_up_process(__k); \
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.
68 * Description: Convenient wrapper for kthread_create_on_cpu()
69 * followed by wake_up_process(). Returns the kthread or
72 static inline struct task_struct *
73 kthread_run_on_cpu(int (*threadfn)(void *data), void *data,
74 unsigned int cpu, const char *namefmt)
76 struct task_struct *p;
78 p = kthread_create_on_cpu(threadfn, data, cpu, namefmt);
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 void kthread_exit(long result) __noreturn;
100 void kthread_complete_and_exit(struct completion *, long) __noreturn;
102 int kthreadd(void *unused);
103 extern struct task_struct *kthreadd_task;
104 extern int tsk_fork_get_node(struct task_struct *tsk);
107 * Simple work processor based on kthread.
109 * This provides easier way to make use of kthreads. A kthread_work
110 * can be queued and flushed using queue/kthread_flush_work()
111 * respectively. Queued kthread_works are processed by a kthread
112 * running kthread_worker_fn().
115 typedef void (*kthread_work_func_t)(struct kthread_work *work);
116 void kthread_delayed_work_timer_fn(struct timer_list *t);
119 KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
122 struct kthread_worker {
125 struct list_head work_list;
126 struct list_head delayed_work_list;
127 struct task_struct *task;
128 struct kthread_work *current_work;
131 struct kthread_work {
132 struct list_head node;
133 kthread_work_func_t func;
134 struct kthread_worker *worker;
135 /* Number of canceling calls that are running at the moment. */
139 struct kthread_delayed_work {
140 struct kthread_work work;
141 struct timer_list timer;
144 #define KTHREAD_WORKER_INIT(worker) { \
145 .lock = __RAW_SPIN_LOCK_UNLOCKED((worker).lock), \
146 .work_list = LIST_HEAD_INIT((worker).work_list), \
147 .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
150 #define KTHREAD_WORK_INIT(work, fn) { \
151 .node = LIST_HEAD_INIT((work).node), \
155 #define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
156 .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
157 .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
161 #define DEFINE_KTHREAD_WORKER(worker) \
162 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
164 #define DEFINE_KTHREAD_WORK(work, fn) \
165 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
167 #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
168 struct kthread_delayed_work dwork = \
169 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
172 * kthread_worker.lock needs its own lockdep class key when defined on
173 * stack with lockdep enabled. Use the following macros in such cases.
175 #ifdef CONFIG_LOCKDEP
176 # define KTHREAD_WORKER_INIT_ONSTACK(worker) \
177 ({ kthread_init_worker(&worker); worker; })
178 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
179 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
181 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
184 extern void __kthread_init_worker(struct kthread_worker *worker,
185 const char *name, struct lock_class_key *key);
187 #define kthread_init_worker(worker) \
189 static struct lock_class_key __key; \
190 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
193 #define kthread_init_work(work, fn) \
195 memset((work), 0, sizeof(struct kthread_work)); \
196 INIT_LIST_HEAD(&(work)->node); \
197 (work)->func = (fn); \
200 #define kthread_init_delayed_work(dwork, fn) \
202 kthread_init_work(&(dwork)->work, (fn)); \
203 timer_setup(&(dwork)->timer, \
204 kthread_delayed_work_timer_fn, \
208 int kthread_worker_fn(void *worker_ptr);
211 struct kthread_worker *
212 kthread_create_worker(unsigned int flags, const char namefmt[], ...);
214 __printf(3, 4) struct kthread_worker *
215 kthread_create_worker_on_cpu(int cpu, unsigned int flags,
216 const char namefmt[], ...);
218 bool kthread_queue_work(struct kthread_worker *worker,
219 struct kthread_work *work);
221 bool kthread_queue_delayed_work(struct kthread_worker *worker,
222 struct kthread_delayed_work *dwork,
223 unsigned long delay);
225 bool kthread_mod_delayed_work(struct kthread_worker *worker,
226 struct kthread_delayed_work *dwork,
227 unsigned long delay);
229 void kthread_flush_work(struct kthread_work *work);
230 void kthread_flush_worker(struct kthread_worker *worker);
232 bool kthread_cancel_work_sync(struct kthread_work *work);
233 bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
235 void kthread_destroy_worker(struct kthread_worker *worker);
237 void kthread_use_mm(struct mm_struct *mm);
238 void kthread_unuse_mm(struct mm_struct *mm);
240 struct cgroup_subsys_state;
242 #ifdef CONFIG_BLK_CGROUP
243 void kthread_associate_blkcg(struct cgroup_subsys_state *css);
244 struct cgroup_subsys_state *kthread_blkcg(void);
246 static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
247 static inline struct cgroup_subsys_state *kthread_blkcg(void)
252 #endif /* _LINUX_KTHREAD_H */