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