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 void 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); \
60 void free_kthread_struct(struct task_struct *k);
61 void kthread_bind(struct task_struct *k, unsigned int cpu);
62 void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
63 int kthread_stop(struct task_struct *k);
64 bool kthread_should_stop(void);
65 bool kthread_should_park(void);
66 bool __kthread_should_park(struct task_struct *k);
67 bool kthread_freezable_should_stop(bool *was_frozen);
68 void *kthread_func(struct task_struct *k);
69 void *kthread_data(struct task_struct *k);
70 void *kthread_probe_data(struct task_struct *k);
71 int kthread_park(struct task_struct *k);
72 void kthread_unpark(struct task_struct *k);
73 void kthread_parkme(void);
75 int kthreadd(void *unused);
76 extern struct task_struct *kthreadd_task;
77 extern int tsk_fork_get_node(struct task_struct *tsk);
80 * Simple work processor based on kthread.
82 * This provides easier way to make use of kthreads. A kthread_work
83 * can be queued and flushed using queue/kthread_flush_work()
84 * respectively. Queued kthread_works are processed by a kthread
85 * running kthread_worker_fn().
88 typedef void (*kthread_work_func_t)(struct kthread_work *work);
89 void kthread_delayed_work_timer_fn(struct timer_list *t);
92 KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
95 struct kthread_worker {
98 struct list_head work_list;
99 struct list_head delayed_work_list;
100 struct task_struct *task;
101 struct kthread_work *current_work;
104 struct kthread_work {
105 struct list_head node;
106 kthread_work_func_t func;
107 struct kthread_worker *worker;
108 /* Number of canceling calls that are running at the moment. */
112 struct kthread_delayed_work {
113 struct kthread_work work;
114 struct timer_list timer;
117 #define KTHREAD_WORKER_INIT(worker) { \
118 .lock = __RAW_SPIN_LOCK_UNLOCKED((worker).lock), \
119 .work_list = LIST_HEAD_INIT((worker).work_list), \
120 .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
123 #define KTHREAD_WORK_INIT(work, fn) { \
124 .node = LIST_HEAD_INIT((work).node), \
128 #define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
129 .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
130 .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
134 #define DEFINE_KTHREAD_WORKER(worker) \
135 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
137 #define DEFINE_KTHREAD_WORK(work, fn) \
138 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
140 #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
141 struct kthread_delayed_work dwork = \
142 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
145 * kthread_worker.lock needs its own lockdep class key when defined on
146 * stack with lockdep enabled. Use the following macros in such cases.
148 #ifdef CONFIG_LOCKDEP
149 # define KTHREAD_WORKER_INIT_ONSTACK(worker) \
150 ({ kthread_init_worker(&worker); worker; })
151 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
152 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
154 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
157 extern void __kthread_init_worker(struct kthread_worker *worker,
158 const char *name, struct lock_class_key *key);
160 #define kthread_init_worker(worker) \
162 static struct lock_class_key __key; \
163 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
166 #define kthread_init_work(work, fn) \
168 memset((work), 0, sizeof(struct kthread_work)); \
169 INIT_LIST_HEAD(&(work)->node); \
170 (work)->func = (fn); \
173 #define kthread_init_delayed_work(dwork, fn) \
175 kthread_init_work(&(dwork)->work, (fn)); \
176 timer_setup(&(dwork)->timer, \
177 kthread_delayed_work_timer_fn, \
181 int kthread_worker_fn(void *worker_ptr);
184 struct kthread_worker *
185 kthread_create_worker(unsigned int flags, const char namefmt[], ...);
187 __printf(3, 4) struct kthread_worker *
188 kthread_create_worker_on_cpu(int cpu, unsigned int flags,
189 const char namefmt[], ...);
191 bool kthread_queue_work(struct kthread_worker *worker,
192 struct kthread_work *work);
194 bool kthread_queue_delayed_work(struct kthread_worker *worker,
195 struct kthread_delayed_work *dwork,
196 unsigned long delay);
198 bool kthread_mod_delayed_work(struct kthread_worker *worker,
199 struct kthread_delayed_work *dwork,
200 unsigned long delay);
202 void kthread_flush_work(struct kthread_work *work);
203 void kthread_flush_worker(struct kthread_worker *worker);
205 bool kthread_cancel_work_sync(struct kthread_work *work);
206 bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
208 void kthread_destroy_worker(struct kthread_worker *worker);
210 void kthread_use_mm(struct mm_struct *mm);
211 void kthread_unuse_mm(struct mm_struct *mm);
213 struct cgroup_subsys_state;
215 #ifdef CONFIG_BLK_CGROUP
216 void kthread_associate_blkcg(struct cgroup_subsys_state *css);
217 struct cgroup_subsys_state *kthread_blkcg(void);
219 static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
220 static inline struct cgroup_subsys_state *kthread_blkcg(void)
225 #endif /* _LINUX_KTHREAD_H */