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 set_kthread_struct(struct task_struct *p);
38 void kthread_set_per_cpu(struct task_struct *k, int cpu);
39 bool kthread_is_per_cpu(struct task_struct *k);
42 * kthread_run - create and wake a thread.
43 * @threadfn: the function to run until signal_pending(current).
44 * @data: data ptr for @threadfn.
45 * @namefmt: printf-style name for the thread.
47 * Description: Convenient wrapper for kthread_create() followed by
48 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
50 #define kthread_run(threadfn, data, namefmt, ...) \
52 struct task_struct *__k \
53 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
55 wake_up_process(__k); \
59 void free_kthread_struct(struct task_struct *k);
60 void kthread_bind(struct task_struct *k, unsigned int cpu);
61 void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
62 int kthread_stop(struct task_struct *k);
63 bool kthread_should_stop(void);
64 bool kthread_should_park(void);
65 bool __kthread_should_park(struct task_struct *k);
66 bool kthread_freezable_should_stop(bool *was_frozen);
67 void *kthread_func(struct task_struct *k);
68 void *kthread_data(struct task_struct *k);
69 void *kthread_probe_data(struct task_struct *k);
70 int kthread_park(struct task_struct *k);
71 void kthread_unpark(struct task_struct *k);
72 void kthread_parkme(void);
74 int kthreadd(void *unused);
75 extern struct task_struct *kthreadd_task;
76 extern int tsk_fork_get_node(struct task_struct *tsk);
79 * Simple work processor based on kthread.
81 * This provides easier way to make use of kthreads. A kthread_work
82 * can be queued and flushed using queue/kthread_flush_work()
83 * respectively. Queued kthread_works are processed by a kthread
84 * running kthread_worker_fn().
87 typedef void (*kthread_work_func_t)(struct kthread_work *work);
88 void kthread_delayed_work_timer_fn(struct timer_list *t);
91 KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
94 struct kthread_worker {
97 struct list_head work_list;
98 struct list_head delayed_work_list;
99 struct task_struct *task;
100 struct kthread_work *current_work;
103 struct kthread_work {
104 struct list_head node;
105 kthread_work_func_t func;
106 struct kthread_worker *worker;
107 /* Number of canceling calls that are running at the moment. */
111 struct kthread_delayed_work {
112 struct kthread_work work;
113 struct timer_list timer;
116 #define KTHREAD_WORKER_INIT(worker) { \
117 .lock = __RAW_SPIN_LOCK_UNLOCKED((worker).lock), \
118 .work_list = LIST_HEAD_INIT((worker).work_list), \
119 .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
122 #define KTHREAD_WORK_INIT(work, fn) { \
123 .node = LIST_HEAD_INIT((work).node), \
127 #define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
128 .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
129 .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
133 #define DEFINE_KTHREAD_WORKER(worker) \
134 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
136 #define DEFINE_KTHREAD_WORK(work, fn) \
137 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
139 #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
140 struct kthread_delayed_work dwork = \
141 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
144 * kthread_worker.lock needs its own lockdep class key when defined on
145 * stack with lockdep enabled. Use the following macros in such cases.
147 #ifdef CONFIG_LOCKDEP
148 # define KTHREAD_WORKER_INIT_ONSTACK(worker) \
149 ({ kthread_init_worker(&worker); worker; })
150 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
151 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
153 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
156 extern void __kthread_init_worker(struct kthread_worker *worker,
157 const char *name, struct lock_class_key *key);
159 #define kthread_init_worker(worker) \
161 static struct lock_class_key __key; \
162 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
165 #define kthread_init_work(work, fn) \
167 memset((work), 0, sizeof(struct kthread_work)); \
168 INIT_LIST_HEAD(&(work)->node); \
169 (work)->func = (fn); \
172 #define kthread_init_delayed_work(dwork, fn) \
174 kthread_init_work(&(dwork)->work, (fn)); \
175 timer_setup(&(dwork)->timer, \
176 kthread_delayed_work_timer_fn, \
180 int kthread_worker_fn(void *worker_ptr);
183 struct kthread_worker *
184 kthread_create_worker(unsigned int flags, const char namefmt[], ...);
186 __printf(3, 4) struct kthread_worker *
187 kthread_create_worker_on_cpu(int cpu, unsigned int flags,
188 const char namefmt[], ...);
190 bool kthread_queue_work(struct kthread_worker *worker,
191 struct kthread_work *work);
193 bool kthread_queue_delayed_work(struct kthread_worker *worker,
194 struct kthread_delayed_work *dwork,
195 unsigned long delay);
197 bool kthread_mod_delayed_work(struct kthread_worker *worker,
198 struct kthread_delayed_work *dwork,
199 unsigned long delay);
201 void kthread_flush_work(struct kthread_work *work);
202 void kthread_flush_worker(struct kthread_worker *worker);
204 bool kthread_cancel_work_sync(struct kthread_work *work);
205 bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
207 void kthread_destroy_worker(struct kthread_worker *worker);
209 void kthread_use_mm(struct mm_struct *mm);
210 void kthread_unuse_mm(struct mm_struct *mm);
212 struct cgroup_subsys_state;
214 #ifdef CONFIG_BLK_CGROUP
215 void kthread_associate_blkcg(struct cgroup_subsys_state *css);
216 struct cgroup_subsys_state *kthread_blkcg(void);
218 static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
219 static inline struct cgroup_subsys_state *kthread_blkcg(void)
224 #endif /* _LINUX_KTHREAD_H */