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>
7 #include <linux/cgroup.h>
10 struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
13 const char namefmt[], ...);
16 * kthread_create - create a kthread on the current node
17 * @threadfn: the function to run in the thread
18 * @data: data pointer for @threadfn()
19 * @namefmt: printf-style format string for the thread name
20 * @arg...: arguments for @namefmt.
22 * This macro will create a kthread on the current node, leaving it in
23 * the stopped state. This is just a helper for kthread_create_on_node();
24 * see the documentation there for more details.
26 #define kthread_create(threadfn, data, namefmt, arg...) \
27 kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
30 struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
35 void kthread_set_per_cpu(struct task_struct *k, int cpu);
36 bool kthread_is_per_cpu(struct task_struct *k);
39 * kthread_run - create and wake a thread.
40 * @threadfn: the function to run until signal_pending(current).
41 * @data: data ptr for @threadfn.
42 * @namefmt: printf-style name for the thread.
44 * Description: Convenient wrapper for kthread_create() followed by
45 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
47 #define kthread_run(threadfn, data, namefmt, ...) \
49 struct task_struct *__k \
50 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
52 wake_up_process(__k); \
56 void free_kthread_struct(struct task_struct *k);
57 void kthread_bind(struct task_struct *k, unsigned int cpu);
58 void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
59 int kthread_stop(struct task_struct *k);
60 bool kthread_should_stop(void);
61 bool kthread_should_park(void);
62 bool kthread_freezable_should_stop(bool *was_frozen);
63 void *kthread_data(struct task_struct *k);
64 void *kthread_probe_data(struct task_struct *k);
65 int kthread_park(struct task_struct *k);
66 void kthread_unpark(struct task_struct *k);
67 void kthread_parkme(void);
69 int kthreadd(void *unused);
70 extern struct task_struct *kthreadd_task;
71 extern int tsk_fork_get_node(struct task_struct *tsk);
74 * Simple work processor based on kthread.
76 * This provides easier way to make use of kthreads. A kthread_work
77 * can be queued and flushed using queue/kthread_flush_work()
78 * respectively. Queued kthread_works are processed by a kthread
79 * running kthread_worker_fn().
82 typedef void (*kthread_work_func_t)(struct kthread_work *work);
83 void kthread_delayed_work_timer_fn(struct timer_list *t);
86 KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
89 struct kthread_worker {
92 struct list_head work_list;
93 struct list_head delayed_work_list;
94 struct task_struct *task;
95 struct kthread_work *current_work;
99 struct list_head node;
100 kthread_work_func_t func;
101 struct kthread_worker *worker;
102 /* Number of canceling calls that are running at the moment. */
106 struct kthread_delayed_work {
107 struct kthread_work work;
108 struct timer_list timer;
111 #define KTHREAD_WORKER_INIT(worker) { \
112 .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
113 .work_list = LIST_HEAD_INIT((worker).work_list), \
114 .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
117 #define KTHREAD_WORK_INIT(work, fn) { \
118 .node = LIST_HEAD_INIT((work).node), \
122 #define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
123 .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
124 .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
128 #define DEFINE_KTHREAD_WORKER(worker) \
129 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
131 #define DEFINE_KTHREAD_WORK(work, fn) \
132 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
134 #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
135 struct kthread_delayed_work dwork = \
136 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
139 * kthread_worker.lock needs its own lockdep class key when defined on
140 * stack with lockdep enabled. Use the following macros in such cases.
142 #ifdef CONFIG_LOCKDEP
143 # define KTHREAD_WORKER_INIT_ONSTACK(worker) \
144 ({ kthread_init_worker(&worker); worker; })
145 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
146 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
148 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
151 extern void __kthread_init_worker(struct kthread_worker *worker,
152 const char *name, struct lock_class_key *key);
154 #define kthread_init_worker(worker) \
156 static struct lock_class_key __key; \
157 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
160 #define kthread_init_work(work, fn) \
162 memset((work), 0, sizeof(struct kthread_work)); \
163 INIT_LIST_HEAD(&(work)->node); \
164 (work)->func = (fn); \
167 #define kthread_init_delayed_work(dwork, fn) \
169 kthread_init_work(&(dwork)->work, (fn)); \
170 __init_timer(&(dwork)->timer, \
171 kthread_delayed_work_timer_fn, \
175 int kthread_worker_fn(void *worker_ptr);
178 struct kthread_worker *
179 kthread_create_worker(unsigned int flags, const char namefmt[], ...);
181 __printf(3, 4) struct kthread_worker *
182 kthread_create_worker_on_cpu(int cpu, unsigned int flags,
183 const char namefmt[], ...);
185 bool kthread_queue_work(struct kthread_worker *worker,
186 struct kthread_work *work);
188 bool kthread_queue_delayed_work(struct kthread_worker *worker,
189 struct kthread_delayed_work *dwork,
190 unsigned long delay);
192 bool kthread_mod_delayed_work(struct kthread_worker *worker,
193 struct kthread_delayed_work *dwork,
194 unsigned long delay);
196 void kthread_flush_work(struct kthread_work *work);
197 void kthread_flush_worker(struct kthread_worker *worker);
199 bool kthread_cancel_work_sync(struct kthread_work *work);
200 bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
202 void kthread_destroy_worker(struct kthread_worker *worker);
204 #ifdef CONFIG_BLK_CGROUP
205 void kthread_associate_blkcg(struct cgroup_subsys_state *css);
206 struct cgroup_subsys_state *kthread_blkcg(void);
208 static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
209 static inline struct cgroup_subsys_state *kthread_blkcg(void)
214 #endif /* _LINUX_KTHREAD_H */