Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
1da177e4 LT |
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 | ||
9bf5b9eb CH |
8 | struct mm_struct; |
9 | ||
b9075fa9 | 10 | __printf(4, 5) |
207205a2 ED |
11 | struct task_struct *kthread_create_on_node(int (*threadfn)(void *data), |
12 | void *data, | |
13 | int node, | |
b9075fa9 | 14 | const char namefmt[], ...); |
207205a2 | 15 | |
e154ccc8 JC |
16 | /** |
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 | |
d16977f3 | 21 | * @arg...: arguments for @namefmt. |
e154ccc8 JC |
22 | * |
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. | |
26 | */ | |
207205a2 | 27 | #define kthread_create(threadfn, data, namefmt, arg...) \ |
e9f06986 | 28 | kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg) |
207205a2 | 29 | |
1da177e4 | 30 | |
2a1d4460 TG |
31 | struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data), |
32 | void *data, | |
33 | unsigned int cpu, | |
34 | const char *namefmt); | |
35 | ||
1da177e4 | 36 | /** |
9e37bd30 | 37 | * kthread_run - create and wake a thread. |
1da177e4 LT |
38 | * @threadfn: the function to run until signal_pending(current). |
39 | * @data: data ptr for @threadfn. | |
40 | * @namefmt: printf-style name for the thread. | |
41 | * | |
42 | * Description: Convenient wrapper for kthread_create() followed by | |
9e37bd30 RD |
43 | * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM). |
44 | */ | |
1da177e4 LT |
45 | #define kthread_run(threadfn, data, namefmt, ...) \ |
46 | ({ \ | |
47 | struct task_struct *__k \ | |
48 | = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \ | |
49 | if (!IS_ERR(__k)) \ | |
50 | wake_up_process(__k); \ | |
51 | __k; \ | |
52 | }) | |
53 | ||
1da5c46f | 54 | void free_kthread_struct(struct task_struct *k); |
1da177e4 | 55 | void kthread_bind(struct task_struct *k, unsigned int cpu); |
25834c73 | 56 | void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask); |
1da177e4 | 57 | int kthread_stop(struct task_struct *k); |
2a1d4460 TG |
58 | bool kthread_should_stop(void); |
59 | bool kthread_should_park(void); | |
0121805d | 60 | bool __kthread_should_park(struct task_struct *k); |
8a32c441 | 61 | bool kthread_freezable_should_stop(bool *was_frozen); |
52782c92 | 62 | void *kthread_func(struct task_struct *k); |
82805ab7 | 63 | void *kthread_data(struct task_struct *k); |
e700591a | 64 | void *kthread_probe_data(struct task_struct *k); |
2a1d4460 TG |
65 | int kthread_park(struct task_struct *k); |
66 | void kthread_unpark(struct task_struct *k); | |
67 | void kthread_parkme(void); | |
1da177e4 | 68 | |
73c27992 EB |
69 | int kthreadd(void *unused); |
70 | extern struct task_struct *kthreadd_task; | |
207205a2 | 71 | extern int tsk_fork_get_node(struct task_struct *tsk); |
73c27992 | 72 | |
b56c0d89 TH |
73 | /* |
74 | * Simple work processor based on kthread. | |
75 | * | |
76 | * This provides easier way to make use of kthreads. A kthread_work | |
3989144f | 77 | * can be queued and flushed using queue/kthread_flush_work() |
b56c0d89 TH |
78 | * respectively. Queued kthread_works are processed by a kthread |
79 | * running kthread_worker_fn(). | |
b56c0d89 TH |
80 | */ |
81 | struct kthread_work; | |
82 | typedef void (*kthread_work_func_t)(struct kthread_work *work); | |
fe5c3b69 | 83 | void kthread_delayed_work_timer_fn(struct timer_list *t); |
b56c0d89 | 84 | |
dbf52682 PM |
85 | enum { |
86 | KTW_FREEZABLE = 1 << 0, /* freeze during suspend */ | |
87 | }; | |
88 | ||
b56c0d89 | 89 | struct kthread_worker { |
dbf52682 | 90 | unsigned int flags; |
fe99a4f4 | 91 | raw_spinlock_t lock; |
b56c0d89 | 92 | struct list_head work_list; |
22597dc3 | 93 | struct list_head delayed_work_list; |
b56c0d89 | 94 | struct task_struct *task; |
46f3d976 | 95 | struct kthread_work *current_work; |
b56c0d89 TH |
96 | }; |
97 | ||
98 | struct kthread_work { | |
99 | struct list_head node; | |
100 | kthread_work_func_t func; | |
46f3d976 | 101 | struct kthread_worker *worker; |
37be45d4 PM |
102 | /* Number of canceling calls that are running at the moment. */ |
103 | int canceling; | |
b56c0d89 TH |
104 | }; |
105 | ||
22597dc3 PM |
106 | struct kthread_delayed_work { |
107 | struct kthread_work work; | |
108 | struct timer_list timer; | |
109 | }; | |
110 | ||
b56c0d89 | 111 | #define KTHREAD_WORKER_INIT(worker) { \ |
fe99a4f4 | 112 | .lock = __RAW_SPIN_LOCK_UNLOCKED((worker).lock), \ |
b56c0d89 | 113 | .work_list = LIST_HEAD_INIT((worker).work_list), \ |
22597dc3 | 114 | .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\ |
b56c0d89 TH |
115 | } |
116 | ||
117 | #define KTHREAD_WORK_INIT(work, fn) { \ | |
118 | .node = LIST_HEAD_INIT((work).node), \ | |
119 | .func = (fn), \ | |
b56c0d89 TH |
120 | } |
121 | ||
22597dc3 PM |
122 | #define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \ |
123 | .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \ | |
841b86f3 | 124 | .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\ |
22597dc3 PM |
125 | TIMER_IRQSAFE), \ |
126 | } | |
127 | ||
b56c0d89 TH |
128 | #define DEFINE_KTHREAD_WORKER(worker) \ |
129 | struct kthread_worker worker = KTHREAD_WORKER_INIT(worker) | |
130 | ||
131 | #define DEFINE_KTHREAD_WORK(work, fn) \ | |
132 | struct kthread_work work = KTHREAD_WORK_INIT(work, fn) | |
133 | ||
22597dc3 PM |
134 | #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \ |
135 | struct kthread_delayed_work dwork = \ | |
136 | KTHREAD_DELAYED_WORK_INIT(dwork, fn) | |
137 | ||
4f32e9b1 | 138 | /* |
95847e1b LJ |
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. | |
4f32e9b1 YZ |
141 | */ |
142 | #ifdef CONFIG_LOCKDEP | |
143 | # define KTHREAD_WORKER_INIT_ONSTACK(worker) \ | |
3989144f | 144 | ({ kthread_init_worker(&worker); worker; }) |
4f32e9b1 YZ |
145 | # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \ |
146 | struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker) | |
4f32e9b1 YZ |
147 | #else |
148 | # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker) | |
4f32e9b1 YZ |
149 | #endif |
150 | ||
3989144f | 151 | extern void __kthread_init_worker(struct kthread_worker *worker, |
4f32e9b1 YZ |
152 | const char *name, struct lock_class_key *key); |
153 | ||
3989144f | 154 | #define kthread_init_worker(worker) \ |
4f32e9b1 YZ |
155 | do { \ |
156 | static struct lock_class_key __key; \ | |
3989144f | 157 | __kthread_init_worker((worker), "("#worker")->lock", &__key); \ |
4f32e9b1 YZ |
158 | } while (0) |
159 | ||
3989144f | 160 | #define kthread_init_work(work, fn) \ |
4f32e9b1 YZ |
161 | do { \ |
162 | memset((work), 0, sizeof(struct kthread_work)); \ | |
163 | INIT_LIST_HEAD(&(work)->node); \ | |
164 | (work)->func = (fn); \ | |
4f32e9b1 | 165 | } while (0) |
b56c0d89 | 166 | |
22597dc3 PM |
167 | #define kthread_init_delayed_work(dwork, fn) \ |
168 | do { \ | |
169 | kthread_init_work(&(dwork)->work, (fn)); \ | |
ad01423a | 170 | timer_setup(&(dwork)->timer, \ |
98c985d7 PM |
171 | kthread_delayed_work_timer_fn, \ |
172 | TIMER_IRQSAFE); \ | |
22597dc3 PM |
173 | } while (0) |
174 | ||
b56c0d89 TH |
175 | int kthread_worker_fn(void *worker_ptr); |
176 | ||
dbf52682 | 177 | __printf(2, 3) |
fbae2d44 | 178 | struct kthread_worker * |
dbf52682 | 179 | kthread_create_worker(unsigned int flags, const char namefmt[], ...); |
fbae2d44 | 180 | |
c0b942a7 | 181 | __printf(3, 4) struct kthread_worker * |
dbf52682 PM |
182 | kthread_create_worker_on_cpu(int cpu, unsigned int flags, |
183 | const char namefmt[], ...); | |
fbae2d44 | 184 | |
3989144f | 185 | bool kthread_queue_work(struct kthread_worker *worker, |
b56c0d89 | 186 | struct kthread_work *work); |
22597dc3 PM |
187 | |
188 | bool kthread_queue_delayed_work(struct kthread_worker *worker, | |
189 | struct kthread_delayed_work *dwork, | |
190 | unsigned long delay); | |
191 | ||
9a6b06c8 PM |
192 | bool kthread_mod_delayed_work(struct kthread_worker *worker, |
193 | struct kthread_delayed_work *dwork, | |
194 | unsigned long delay); | |
195 | ||
3989144f PM |
196 | void kthread_flush_work(struct kthread_work *work); |
197 | void kthread_flush_worker(struct kthread_worker *worker); | |
b56c0d89 | 198 | |
37be45d4 PM |
199 | bool kthread_cancel_work_sync(struct kthread_work *work); |
200 | bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work); | |
201 | ||
35033fe9 PM |
202 | void kthread_destroy_worker(struct kthread_worker *worker); |
203 | ||
f5678e7f CH |
204 | void kthread_use_mm(struct mm_struct *mm); |
205 | void kthread_unuse_mm(struct mm_struct *mm); | |
9bf5b9eb | 206 | |
8af0c18a SB |
207 | struct cgroup_subsys_state; |
208 | ||
0b508bc9 | 209 | #ifdef CONFIG_BLK_CGROUP |
05e3db95 SL |
210 | void kthread_associate_blkcg(struct cgroup_subsys_state *css); |
211 | struct cgroup_subsys_state *kthread_blkcg(void); | |
212 | #else | |
213 | static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { } | |
214 | static inline struct cgroup_subsys_state *kthread_blkcg(void) | |
215 | { | |
216 | return NULL; | |
217 | } | |
218 | #endif | |
1da177e4 | 219 | #endif /* _LINUX_KTHREAD_H */ |