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#include <linux/cgroup.h>
8
9__printf(4, 5)
10struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
11 void *data,
12 int node,
13 const char namefmt[], ...);
14
15/**
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.
21 *
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.
25 */
26#define kthread_create(threadfn, data, namefmt, arg...) \
27 kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
28
29
30struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
31 void *data,
32 unsigned int cpu,
33 const char *namefmt);
34
35/**
36 * kthread_run - create and wake a thread.
37 * @threadfn: the function to run until signal_pending(current).
38 * @data: data ptr for @threadfn.
39 * @namefmt: printf-style name for the thread.
40 *
41 * Description: Convenient wrapper for kthread_create() followed by
42 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
43 */
44#define kthread_run(threadfn, data, namefmt, ...) \
45({ \
46 struct task_struct *__k \
47 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
48 if (!IS_ERR(__k)) \
49 wake_up_process(__k); \
50 __k; \
51})
52
53void free_kthread_struct(struct task_struct *k);
54void kthread_bind(struct task_struct *k, unsigned int cpu);
55void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
56int kthread_stop(struct task_struct *k);
57bool kthread_should_stop(void);
58bool kthread_should_park(void);
59bool __kthread_should_park(struct task_struct *k);
60bool kthread_freezable_should_stop(bool *was_frozen);
61void *kthread_data(struct task_struct *k);
62void *kthread_probe_data(struct task_struct *k);
63int kthread_park(struct task_struct *k);
64void kthread_unpark(struct task_struct *k);
65void kthread_parkme(void);
66
67int kthreadd(void *unused);
68extern struct task_struct *kthreadd_task;
69extern int tsk_fork_get_node(struct task_struct *tsk);
70
71/*
72 * Simple work processor based on kthread.
73 *
74 * This provides easier way to make use of kthreads. A kthread_work
75 * can be queued and flushed using queue/kthread_flush_work()
76 * respectively. Queued kthread_works are processed by a kthread
77 * running kthread_worker_fn().
78 */
79struct kthread_work;
80typedef void (*kthread_work_func_t)(struct kthread_work *work);
81void kthread_delayed_work_timer_fn(struct timer_list *t);
82
83enum {
84 KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
85};
86
87struct kthread_worker {
88 unsigned int flags;
89 raw_spinlock_t lock;
90 struct list_head work_list;
91 struct list_head delayed_work_list;
92 struct task_struct *task;
93 struct kthread_work *current_work;
94};
95
96struct kthread_work {
97 struct list_head node;
98 kthread_work_func_t func;
99 struct kthread_worker *worker;
100 /* Number of canceling calls that are running at the moment. */
101 int canceling;
102};
103
104struct kthread_delayed_work {
105 struct kthread_work work;
106 struct timer_list timer;
107};
108
109#define KTHREAD_WORKER_INIT(worker) { \
110 .lock = __RAW_SPIN_LOCK_UNLOCKED((worker).lock), \
111 .work_list = LIST_HEAD_INIT((worker).work_list), \
112 .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
113 }
114
115#define KTHREAD_WORK_INIT(work, fn) { \
116 .node = LIST_HEAD_INIT((work).node), \
117 .func = (fn), \
118 }
119
120#define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
121 .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
122 .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
123 TIMER_IRQSAFE), \
124 }
125
126#define DEFINE_KTHREAD_WORKER(worker) \
127 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
128
129#define DEFINE_KTHREAD_WORK(work, fn) \
130 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
131
132#define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
133 struct kthread_delayed_work dwork = \
134 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
135
136/*
137 * kthread_worker.lock needs its own lockdep class key when defined on
138 * stack with lockdep enabled. Use the following macros in such cases.
139 */
140#ifdef CONFIG_LOCKDEP
141# define KTHREAD_WORKER_INIT_ONSTACK(worker) \
142 ({ kthread_init_worker(&worker); worker; })
143# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
144 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
145#else
146# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
147#endif
148
149extern void __kthread_init_worker(struct kthread_worker *worker,
150 const char *name, struct lock_class_key *key);
151
152#define kthread_init_worker(worker) \
153 do { \
154 static struct lock_class_key __key; \
155 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
156 } while (0)
157
158#define kthread_init_work(work, fn) \
159 do { \
160 memset((work), 0, sizeof(struct kthread_work)); \
161 INIT_LIST_HEAD(&(work)->node); \
162 (work)->func = (fn); \
163 } while (0)
164
165#define kthread_init_delayed_work(dwork, fn) \
166 do { \
167 kthread_init_work(&(dwork)->work, (fn)); \
168 timer_setup(&(dwork)->timer, \
169 kthread_delayed_work_timer_fn, 0); \
170 } while (0)
171
172int kthread_worker_fn(void *worker_ptr);
173
174__printf(2, 3)
175struct kthread_worker *
176kthread_create_worker(unsigned int flags, const char namefmt[], ...);
177
178__printf(3, 4) struct kthread_worker *
179kthread_create_worker_on_cpu(int cpu, unsigned int flags,
180 const char namefmt[], ...);
181
182bool kthread_queue_work(struct kthread_worker *worker,
183 struct kthread_work *work);
184
185bool kthread_queue_delayed_work(struct kthread_worker *worker,
186 struct kthread_delayed_work *dwork,
187 unsigned long delay);
188
189bool kthread_mod_delayed_work(struct kthread_worker *worker,
190 struct kthread_delayed_work *dwork,
191 unsigned long delay);
192
193void kthread_flush_work(struct kthread_work *work);
194void kthread_flush_worker(struct kthread_worker *worker);
195
196bool kthread_cancel_work_sync(struct kthread_work *work);
197bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
198
199void kthread_destroy_worker(struct kthread_worker *worker);
200
201#ifdef CONFIG_BLK_CGROUP
202void kthread_associate_blkcg(struct cgroup_subsys_state *css);
203struct cgroup_subsys_state *kthread_blkcg(void);
204#else
205static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
206static inline struct cgroup_subsys_state *kthread_blkcg(void)
207{
208 return NULL;
209}
210#endif
211#endif /* _LINUX_KTHREAD_H */
212