Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* Kernel thread helper functions. |
3 | * Copyright (C) 2004 IBM Corporation, Rusty Russell. | |
9bf5b9eb | 4 | * Copyright (C) 2009 Red Hat, Inc. |
1da177e4 | 5 | * |
73c27992 | 6 | * Creation is done via kthreadd, so that we get a clean environment |
1da177e4 LT |
7 | * even if we're invoked from userspace (think modprobe, hotplug cpu, |
8 | * etc.). | |
9 | */ | |
ae7e81c0 | 10 | #include <uapi/linux/sched/types.h> |
9bf5b9eb CH |
11 | #include <linux/mm.h> |
12 | #include <linux/mmu_context.h> | |
1da177e4 | 13 | #include <linux/sched.h> |
9bf5b9eb | 14 | #include <linux/sched/mm.h> |
29930025 | 15 | #include <linux/sched/task.h> |
1da177e4 LT |
16 | #include <linux/kthread.h> |
17 | #include <linux/completion.h> | |
18 | #include <linux/err.h> | |
8af0c18a | 19 | #include <linux/cgroup.h> |
58568d2a | 20 | #include <linux/cpuset.h> |
1da177e4 LT |
21 | #include <linux/unistd.h> |
22 | #include <linux/file.h> | |
9984de1a | 23 | #include <linux/export.h> |
97d1f15b | 24 | #include <linux/mutex.h> |
b56c0d89 TH |
25 | #include <linux/slab.h> |
26 | #include <linux/freezer.h> | |
a74fb73c | 27 | #include <linux/ptrace.h> |
cd42d559 | 28 | #include <linux/uaccess.h> |
98fa15f3 | 29 | #include <linux/numa.h> |
9cc5b865 | 30 | #include <linux/sched/isolation.h> |
ad8d75ff | 31 | #include <trace/events/sched.h> |
1da177e4 | 32 | |
9bf5b9eb | 33 | |
73c27992 EB |
34 | static DEFINE_SPINLOCK(kthread_create_lock); |
35 | static LIST_HEAD(kthread_create_list); | |
36 | struct task_struct *kthreadd_task; | |
1da177e4 LT |
37 | |
38 | struct kthread_create_info | |
39 | { | |
73c27992 | 40 | /* Information passed to kthread() from kthreadd. */ |
1da177e4 LT |
41 | int (*threadfn)(void *data); |
42 | void *data; | |
207205a2 | 43 | int node; |
1da177e4 | 44 | |
73c27992 | 45 | /* Result passed back to kthread_create() from kthreadd. */ |
1da177e4 | 46 | struct task_struct *result; |
786235ee | 47 | struct completion *done; |
65f27f38 | 48 | |
73c27992 | 49 | struct list_head list; |
1da177e4 LT |
50 | }; |
51 | ||
63706172 | 52 | struct kthread { |
2a1d4460 TG |
53 | unsigned long flags; |
54 | unsigned int cpu; | |
6b124879 | 55 | int result; |
52782c92 | 56 | int (*threadfn)(void *); |
82805ab7 | 57 | void *data; |
37c54f9b | 58 | mm_segment_t oldfs; |
2a1d4460 | 59 | struct completion parked; |
63706172 | 60 | struct completion exited; |
0b508bc9 | 61 | #ifdef CONFIG_BLK_CGROUP |
05e3db95 SL |
62 | struct cgroup_subsys_state *blkcg_css; |
63 | #endif | |
1da177e4 LT |
64 | }; |
65 | ||
2a1d4460 TG |
66 | enum KTHREAD_BITS { |
67 | KTHREAD_IS_PER_CPU = 0, | |
68 | KTHREAD_SHOULD_STOP, | |
69 | KTHREAD_SHOULD_PARK, | |
2a1d4460 TG |
70 | }; |
71 | ||
4ecdafc8 ON |
72 | static inline struct kthread *to_kthread(struct task_struct *k) |
73 | { | |
1da5c46f ON |
74 | WARN_ON(!(k->flags & PF_KTHREAD)); |
75 | return (__force void *)k->set_child_tid; | |
4ecdafc8 ON |
76 | } |
77 | ||
3a7956e2 PZ |
78 | /* |
79 | * Variant of to_kthread() that doesn't assume @p is a kthread. | |
80 | * | |
81 | * Per construction; when: | |
82 | * | |
83 | * (p->flags & PF_KTHREAD) && p->set_child_tid | |
84 | * | |
85 | * the task is both a kthread and struct kthread is persistent. However | |
86 | * PF_KTHREAD on it's own is not, kernel_thread() can exec() (See umh.c and | |
87 | * begin_new_exec()). | |
88 | */ | |
89 | static inline struct kthread *__to_kthread(struct task_struct *p) | |
90 | { | |
91 | void *kthread = (__force void *)p->set_child_tid; | |
92 | if (kthread && !(p->flags & PF_KTHREAD)) | |
93 | kthread = NULL; | |
94 | return kthread; | |
95 | } | |
96 | ||
40966e31 | 97 | bool set_kthread_struct(struct task_struct *p) |
00b89fe0 VS |
98 | { |
99 | struct kthread *kthread; | |
100 | ||
40966e31 EB |
101 | if (WARN_ON_ONCE(to_kthread(p))) |
102 | return false; | |
00b89fe0 VS |
103 | |
104 | kthread = kzalloc(sizeof(*kthread), GFP_KERNEL); | |
40966e31 EB |
105 | if (!kthread) |
106 | return false; | |
107 | ||
108 | init_completion(&kthread->exited); | |
109 | init_completion(&kthread->parked); | |
110 | p->vfork_done = &kthread->exited; | |
111 | ||
00b89fe0 VS |
112 | /* |
113 | * We abuse ->set_child_tid to avoid the new member and because it | |
40966e31 | 114 | * can't be wrongly copied by copy_process(). |
00b89fe0 VS |
115 | */ |
116 | p->set_child_tid = (__force void __user *)kthread; | |
40966e31 | 117 | return true; |
00b89fe0 VS |
118 | } |
119 | ||
1da5c46f ON |
120 | void free_kthread_struct(struct task_struct *k) |
121 | { | |
05e3db95 SL |
122 | struct kthread *kthread; |
123 | ||
1da5c46f | 124 | /* |
40966e31 | 125 | * Can be NULL if kmalloc() in set_kthread_struct() failed. |
1da5c46f | 126 | */ |
05e3db95 | 127 | kthread = to_kthread(k); |
0b508bc9 | 128 | #ifdef CONFIG_BLK_CGROUP |
05e3db95 SL |
129 | WARN_ON_ONCE(kthread && kthread->blkcg_css); |
130 | #endif | |
40966e31 | 131 | k->set_child_tid = (__force void __user *)NULL; |
05e3db95 | 132 | kfree(kthread); |
1da5c46f ON |
133 | } |
134 | ||
9e37bd30 RD |
135 | /** |
136 | * kthread_should_stop - should this kthread return now? | |
137 | * | |
72fd4a35 | 138 | * When someone calls kthread_stop() on your kthread, it will be woken |
9e37bd30 RD |
139 | * and this will return true. You should then return, and your return |
140 | * value will be passed through to kthread_stop(). | |
141 | */ | |
2a1d4460 | 142 | bool kthread_should_stop(void) |
1da177e4 | 143 | { |
2a1d4460 | 144 | return test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags); |
1da177e4 LT |
145 | } |
146 | EXPORT_SYMBOL(kthread_should_stop); | |
147 | ||
0121805d MK |
148 | bool __kthread_should_park(struct task_struct *k) |
149 | { | |
150 | return test_bit(KTHREAD_SHOULD_PARK, &to_kthread(k)->flags); | |
151 | } | |
152 | EXPORT_SYMBOL_GPL(__kthread_should_park); | |
153 | ||
2a1d4460 TG |
154 | /** |
155 | * kthread_should_park - should this kthread park now? | |
156 | * | |
157 | * When someone calls kthread_park() on your kthread, it will be woken | |
158 | * and this will return true. You should then do the necessary | |
159 | * cleanup and call kthread_parkme() | |
160 | * | |
161 | * Similar to kthread_should_stop(), but this keeps the thread alive | |
162 | * and in a park position. kthread_unpark() "restarts" the thread and | |
163 | * calls the thread function again. | |
164 | */ | |
165 | bool kthread_should_park(void) | |
166 | { | |
0121805d | 167 | return __kthread_should_park(current); |
2a1d4460 | 168 | } |
18896451 | 169 | EXPORT_SYMBOL_GPL(kthread_should_park); |
2a1d4460 | 170 | |
8a32c441 TH |
171 | /** |
172 | * kthread_freezable_should_stop - should this freezable kthread return now? | |
173 | * @was_frozen: optional out parameter, indicates whether %current was frozen | |
174 | * | |
175 | * kthread_should_stop() for freezable kthreads, which will enter | |
176 | * refrigerator if necessary. This function is safe from kthread_stop() / | |
177 | * freezer deadlock and freezable kthreads should use this function instead | |
178 | * of calling try_to_freeze() directly. | |
179 | */ | |
180 | bool kthread_freezable_should_stop(bool *was_frozen) | |
181 | { | |
182 | bool frozen = false; | |
183 | ||
184 | might_sleep(); | |
185 | ||
186 | if (unlikely(freezing(current))) | |
187 | frozen = __refrigerator(true); | |
188 | ||
189 | if (was_frozen) | |
190 | *was_frozen = frozen; | |
191 | ||
192 | return kthread_should_stop(); | |
193 | } | |
194 | EXPORT_SYMBOL_GPL(kthread_freezable_should_stop); | |
195 | ||
52782c92 BF |
196 | /** |
197 | * kthread_func - return the function specified on kthread creation | |
198 | * @task: kthread task in question | |
199 | * | |
200 | * Returns NULL if the task is not a kthread. | |
201 | */ | |
202 | void *kthread_func(struct task_struct *task) | |
203 | { | |
3a7956e2 PZ |
204 | struct kthread *kthread = __to_kthread(task); |
205 | if (kthread) | |
206 | return kthread->threadfn; | |
52782c92 BF |
207 | return NULL; |
208 | } | |
209 | EXPORT_SYMBOL_GPL(kthread_func); | |
210 | ||
82805ab7 TH |
211 | /** |
212 | * kthread_data - return data value specified on kthread creation | |
213 | * @task: kthread task in question | |
214 | * | |
215 | * Return the data value specified when kthread @task was created. | |
216 | * The caller is responsible for ensuring the validity of @task when | |
217 | * calling this function. | |
218 | */ | |
219 | void *kthread_data(struct task_struct *task) | |
220 | { | |
221 | return to_kthread(task)->data; | |
222 | } | |
52782c92 | 223 | EXPORT_SYMBOL_GPL(kthread_data); |
82805ab7 | 224 | |
cd42d559 | 225 | /** |
e700591a | 226 | * kthread_probe_data - speculative version of kthread_data() |
cd42d559 TH |
227 | * @task: possible kthread task in question |
228 | * | |
229 | * @task could be a kthread task. Return the data value specified when it | |
230 | * was created if accessible. If @task isn't a kthread task or its data is | |
231 | * inaccessible for any reason, %NULL is returned. This function requires | |
232 | * that @task itself is safe to dereference. | |
233 | */ | |
e700591a | 234 | void *kthread_probe_data(struct task_struct *task) |
cd42d559 | 235 | { |
3a7956e2 | 236 | struct kthread *kthread = __to_kthread(task); |
cd42d559 TH |
237 | void *data = NULL; |
238 | ||
3a7956e2 PZ |
239 | if (kthread) |
240 | copy_from_kernel_nofault(&data, &kthread->data, sizeof(data)); | |
cd42d559 TH |
241 | return data; |
242 | } | |
243 | ||
2a1d4460 TG |
244 | static void __kthread_parkme(struct kthread *self) |
245 | { | |
741a76b3 | 246 | for (;;) { |
1cef1150 PZ |
247 | /* |
248 | * TASK_PARKED is a special state; we must serialize against | |
249 | * possible pending wakeups to avoid store-store collisions on | |
250 | * task->state. | |
251 | * | |
252 | * Such a collision might possibly result in the task state | |
253 | * changin from TASK_PARKED and us failing the | |
254 | * wait_task_inactive() in kthread_park(). | |
255 | */ | |
256 | set_special_state(TASK_PARKED); | |
741a76b3 PZ |
257 | if (!test_bit(KTHREAD_SHOULD_PARK, &self->flags)) |
258 | break; | |
1cef1150 | 259 | |
26c7295b LC |
260 | /* |
261 | * Thread is going to call schedule(), do not preempt it, | |
262 | * or the caller of kthread_park() may spend more time in | |
263 | * wait_task_inactive(). | |
264 | */ | |
265 | preempt_disable(); | |
f83ee19b | 266 | complete(&self->parked); |
26c7295b LC |
267 | schedule_preempt_disabled(); |
268 | preempt_enable(); | |
2a1d4460 | 269 | } |
2a1d4460 TG |
270 | __set_current_state(TASK_RUNNING); |
271 | } | |
272 | ||
273 | void kthread_parkme(void) | |
274 | { | |
275 | __kthread_parkme(to_kthread(current)); | |
276 | } | |
18896451 | 277 | EXPORT_SYMBOL_GPL(kthread_parkme); |
2a1d4460 | 278 | |
bbda86e9 EB |
279 | /** |
280 | * kthread_exit - Cause the current kthread return @result to kthread_stop(). | |
281 | * @result: The integer value to return to kthread_stop(). | |
282 | * | |
283 | * While kthread_exit can be called directly, it exists so that | |
284 | * functions which do some additional work in non-modular code such as | |
285 | * module_put_and_kthread_exit can be implemented. | |
286 | * | |
287 | * Does not return. | |
288 | */ | |
289 | void __noreturn kthread_exit(long result) | |
290 | { | |
6b124879 EB |
291 | struct kthread *kthread = to_kthread(current); |
292 | kthread->result = result; | |
293 | do_exit(0); | |
bbda86e9 EB |
294 | } |
295 | ||
cead1855 | 296 | /** |
5eb6f228 | 297 | * kthread_complete_and_exit - Exit the current kthread. |
cead1855 EB |
298 | * @comp: Completion to complete |
299 | * @code: The integer value to return to kthread_stop(). | |
300 | * | |
301 | * If present complete @comp and the reuturn code to kthread_stop(). | |
302 | * | |
303 | * A kernel thread whose module may be removed after the completion of | |
304 | * @comp can use this function exit safely. | |
305 | * | |
306 | * Does not return. | |
307 | */ | |
308 | void __noreturn kthread_complete_and_exit(struct completion *comp, long code) | |
309 | { | |
310 | if (comp) | |
311 | complete(comp); | |
312 | ||
313 | kthread_exit(code); | |
314 | } | |
315 | EXPORT_SYMBOL(kthread_complete_and_exit); | |
316 | ||
1da177e4 LT |
317 | static int kthread(void *_create) |
318 | { | |
1a7243ca | 319 | static const struct sched_param param = { .sched_priority = 0 }; |
63706172 | 320 | /* Copy data: it's on kthread's stack */ |
1da177e4 | 321 | struct kthread_create_info *create = _create; |
63706172 ON |
322 | int (*threadfn)(void *data) = create->threadfn; |
323 | void *data = create->data; | |
786235ee | 324 | struct completion *done; |
1da5c46f | 325 | struct kthread *self; |
63706172 | 326 | int ret; |
1da177e4 | 327 | |
00b89fe0 | 328 | self = to_kthread(current); |
1da177e4 | 329 | |
786235ee TH |
330 | /* If user was SIGKILLed, I release the structure. */ |
331 | done = xchg(&create->done, NULL); | |
332 | if (!done) { | |
333 | kfree(create); | |
bbda86e9 | 334 | kthread_exit(-EINTR); |
786235ee | 335 | } |
1da5c46f | 336 | |
52782c92 | 337 | self->threadfn = threadfn; |
1da5c46f | 338 | self->data = data; |
1da5c46f | 339 | |
1a7243ca SAS |
340 | /* |
341 | * The new thread inherited kthreadd's priority and CPU mask. Reset | |
342 | * back to default in case they have been changed. | |
343 | */ | |
344 | sched_setscheduler_nocheck(current, SCHED_NORMAL, ¶m); | |
345 | set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_KTHREAD)); | |
346 | ||
1da177e4 | 347 | /* OK, tell user we're spawned, wait for stop or wakeup */ |
a076e4bc | 348 | __set_current_state(TASK_UNINTERRUPTIBLE); |
3217ab97 | 349 | create->result = current; |
26c7295b LC |
350 | /* |
351 | * Thread is going to call schedule(), do not preempt it, | |
352 | * or the creator may spend more time in wait_task_inactive(). | |
353 | */ | |
354 | preempt_disable(); | |
786235ee | 355 | complete(done); |
26c7295b LC |
356 | schedule_preempt_disabled(); |
357 | preempt_enable(); | |
1da177e4 | 358 | |
63706172 | 359 | ret = -EINTR; |
1da5c46f | 360 | if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) { |
77f88796 | 361 | cgroup_kthread_ready(); |
1da5c46f | 362 | __kthread_parkme(self); |
2a1d4460 TG |
363 | ret = threadfn(data); |
364 | } | |
bbda86e9 | 365 | kthread_exit(ret); |
1da177e4 LT |
366 | } |
367 | ||
cb5021ca | 368 | /* called from kernel_clone() to get node information for about to be created task */ |
207205a2 ED |
369 | int tsk_fork_get_node(struct task_struct *tsk) |
370 | { | |
371 | #ifdef CONFIG_NUMA | |
372 | if (tsk == kthreadd_task) | |
373 | return tsk->pref_node_fork; | |
374 | #endif | |
81c98869 | 375 | return NUMA_NO_NODE; |
207205a2 ED |
376 | } |
377 | ||
73c27992 | 378 | static void create_kthread(struct kthread_create_info *create) |
1da177e4 | 379 | { |
1da177e4 LT |
380 | int pid; |
381 | ||
207205a2 ED |
382 | #ifdef CONFIG_NUMA |
383 | current->pref_node_fork = create->node; | |
384 | #endif | |
1da177e4 LT |
385 | /* We want our own signal handler (we take no signals by default). */ |
386 | pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD); | |
cdd140bd | 387 | if (pid < 0) { |
786235ee TH |
388 | /* If user was SIGKILLed, I release the structure. */ |
389 | struct completion *done = xchg(&create->done, NULL); | |
390 | ||
391 | if (!done) { | |
392 | kfree(create); | |
393 | return; | |
394 | } | |
1da177e4 | 395 | create->result = ERR_PTR(pid); |
786235ee | 396 | complete(done); |
cdd140bd | 397 | } |
1da177e4 LT |
398 | } |
399 | ||
c0b942a7 NI |
400 | static __printf(4, 0) |
401 | struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data), | |
255451e4 PM |
402 | void *data, int node, |
403 | const char namefmt[], | |
404 | va_list args) | |
1da177e4 | 405 | { |
786235ee TH |
406 | DECLARE_COMPLETION_ONSTACK(done); |
407 | struct task_struct *task; | |
408 | struct kthread_create_info *create = kmalloc(sizeof(*create), | |
409 | GFP_KERNEL); | |
410 | ||
411 | if (!create) | |
412 | return ERR_PTR(-ENOMEM); | |
413 | create->threadfn = threadfn; | |
414 | create->data = data; | |
415 | create->node = node; | |
416 | create->done = &done; | |
73c27992 EB |
417 | |
418 | spin_lock(&kthread_create_lock); | |
786235ee | 419 | list_add_tail(&create->list, &kthread_create_list); |
73c27992 EB |
420 | spin_unlock(&kthread_create_lock); |
421 | ||
cbd9b67b | 422 | wake_up_process(kthreadd_task); |
786235ee TH |
423 | /* |
424 | * Wait for completion in killable state, for I might be chosen by | |
425 | * the OOM killer while kthreadd is trying to allocate memory for | |
426 | * new kernel thread. | |
427 | */ | |
428 | if (unlikely(wait_for_completion_killable(&done))) { | |
429 | /* | |
430 | * If I was SIGKILLed before kthreadd (or new kernel thread) | |
431 | * calls complete(), leave the cleanup of this structure to | |
432 | * that thread. | |
433 | */ | |
434 | if (xchg(&create->done, NULL)) | |
8fe6929c | 435 | return ERR_PTR(-EINTR); |
786235ee TH |
436 | /* |
437 | * kthreadd (or new kernel thread) will call complete() | |
438 | * shortly. | |
439 | */ | |
440 | wait_for_completion(&done); | |
441 | } | |
442 | task = create->result; | |
443 | if (!IS_ERR(task)) { | |
3e536e22 | 444 | char name[TASK_COMM_LEN]; |
1c99315b | 445 | |
3e536e22 SD |
446 | /* |
447 | * task is already visible to other tasks, so updating | |
448 | * COMM must be protected. | |
449 | */ | |
450 | vsnprintf(name, sizeof(name), namefmt, args); | |
451 | set_task_comm(task, name); | |
1da177e4 | 452 | } |
786235ee TH |
453 | kfree(create); |
454 | return task; | |
1da177e4 | 455 | } |
255451e4 PM |
456 | |
457 | /** | |
458 | * kthread_create_on_node - create a kthread. | |
459 | * @threadfn: the function to run until signal_pending(current). | |
460 | * @data: data ptr for @threadfn. | |
461 | * @node: task and thread structures for the thread are allocated on this node | |
462 | * @namefmt: printf-style name for the thread. | |
463 | * | |
464 | * Description: This helper function creates and names a kernel | |
465 | * thread. The thread will be stopped: use wake_up_process() to start | |
466 | * it. See also kthread_run(). The new thread has SCHED_NORMAL policy and | |
467 | * is affine to all CPUs. | |
468 | * | |
469 | * If thread is going to be bound on a particular cpu, give its node | |
470 | * in @node, to get NUMA affinity for kthread stack, or else give NUMA_NO_NODE. | |
471 | * When woken, the thread will run @threadfn() with @data as its | |
111e7049 | 472 | * argument. @threadfn() can either return directly if it is a |
255451e4 PM |
473 | * standalone thread for which no one will call kthread_stop(), or |
474 | * return when 'kthread_should_stop()' is true (which means | |
475 | * kthread_stop() has been called). The return value should be zero | |
476 | * or a negative error number; it will be passed to kthread_stop(). | |
477 | * | |
478 | * Returns a task_struct or ERR_PTR(-ENOMEM) or ERR_PTR(-EINTR). | |
479 | */ | |
480 | struct task_struct *kthread_create_on_node(int (*threadfn)(void *data), | |
481 | void *data, int node, | |
482 | const char namefmt[], | |
483 | ...) | |
484 | { | |
485 | struct task_struct *task; | |
486 | va_list args; | |
487 | ||
488 | va_start(args, namefmt); | |
489 | task = __kthread_create_on_node(threadfn, data, node, namefmt, args); | |
490 | va_end(args); | |
491 | ||
492 | return task; | |
493 | } | |
207205a2 | 494 | EXPORT_SYMBOL(kthread_create_on_node); |
1da177e4 | 495 | |
2f064a59 | 496 | static void __kthread_bind_mask(struct task_struct *p, const struct cpumask *mask, unsigned int state) |
2a1d4460 | 497 | { |
25834c73 PZ |
498 | unsigned long flags; |
499 | ||
f2530dc7 TG |
500 | if (!wait_task_inactive(p, state)) { |
501 | WARN_ON(1); | |
502 | return; | |
503 | } | |
25834c73 | 504 | |
2a1d4460 | 505 | /* It's safe because the task is inactive. */ |
25834c73 PZ |
506 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
507 | do_set_cpus_allowed(p, mask); | |
14a40ffc | 508 | p->flags |= PF_NO_SETAFFINITY; |
25834c73 PZ |
509 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
510 | } | |
511 | ||
2f064a59 | 512 | static void __kthread_bind(struct task_struct *p, unsigned int cpu, unsigned int state) |
25834c73 PZ |
513 | { |
514 | __kthread_bind_mask(p, cpumask_of(cpu), state); | |
515 | } | |
516 | ||
517 | void kthread_bind_mask(struct task_struct *p, const struct cpumask *mask) | |
518 | { | |
519 | __kthread_bind_mask(p, mask, TASK_UNINTERRUPTIBLE); | |
2a1d4460 TG |
520 | } |
521 | ||
881232b7 PZ |
522 | /** |
523 | * kthread_bind - bind a just-created kthread to a cpu. | |
524 | * @p: thread created by kthread_create(). | |
525 | * @cpu: cpu (might not be online, must be possible) for @k to run on. | |
526 | * | |
527 | * Description: This function is equivalent to set_cpus_allowed(), | |
528 | * except that @cpu doesn't need to be online, and the thread must be | |
529 | * stopped (i.e., just returned from kthread_create()). | |
530 | */ | |
531 | void kthread_bind(struct task_struct *p, unsigned int cpu) | |
532 | { | |
f2530dc7 | 533 | __kthread_bind(p, cpu, TASK_UNINTERRUPTIBLE); |
881232b7 PZ |
534 | } |
535 | EXPORT_SYMBOL(kthread_bind); | |
536 | ||
2a1d4460 TG |
537 | /** |
538 | * kthread_create_on_cpu - Create a cpu bound kthread | |
539 | * @threadfn: the function to run until signal_pending(current). | |
540 | * @data: data ptr for @threadfn. | |
541 | * @cpu: The cpu on which the thread should be bound, | |
542 | * @namefmt: printf-style name for the thread. Format is restricted | |
543 | * to "name.*%u". Code fills in cpu number. | |
544 | * | |
545 | * Description: This helper function creates and names a kernel thread | |
2a1d4460 TG |
546 | */ |
547 | struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data), | |
548 | void *data, unsigned int cpu, | |
549 | const char *namefmt) | |
550 | { | |
551 | struct task_struct *p; | |
552 | ||
10922838 | 553 | p = kthread_create_on_node(threadfn, data, cpu_to_node(cpu), namefmt, |
2a1d4460 TG |
554 | cpu); |
555 | if (IS_ERR(p)) | |
556 | return p; | |
a65d4096 PM |
557 | kthread_bind(p, cpu); |
558 | /* CPU hotplug need to bind once again when unparking the thread. */ | |
2a1d4460 | 559 | to_kthread(p)->cpu = cpu; |
2a1d4460 TG |
560 | return p; |
561 | } | |
562 | ||
ac687e6e PZ |
563 | void kthread_set_per_cpu(struct task_struct *k, int cpu) |
564 | { | |
565 | struct kthread *kthread = to_kthread(k); | |
566 | if (!kthread) | |
567 | return; | |
568 | ||
569 | WARN_ON_ONCE(!(k->flags & PF_NO_SETAFFINITY)); | |
570 | ||
571 | if (cpu < 0) { | |
572 | clear_bit(KTHREAD_IS_PER_CPU, &kthread->flags); | |
573 | return; | |
574 | } | |
575 | ||
576 | kthread->cpu = cpu; | |
577 | set_bit(KTHREAD_IS_PER_CPU, &kthread->flags); | |
578 | } | |
579 | ||
3a7956e2 | 580 | bool kthread_is_per_cpu(struct task_struct *p) |
ac687e6e | 581 | { |
3a7956e2 | 582 | struct kthread *kthread = __to_kthread(p); |
ac687e6e PZ |
583 | if (!kthread) |
584 | return false; | |
585 | ||
586 | return test_bit(KTHREAD_IS_PER_CPU, &kthread->flags); | |
587 | } | |
588 | ||
cf380a4a ON |
589 | /** |
590 | * kthread_unpark - unpark a thread created by kthread_create(). | |
591 | * @k: thread created by kthread_create(). | |
592 | * | |
593 | * Sets kthread_should_park() for @k to return false, wakes it, and | |
594 | * waits for it to return. If the thread is marked percpu then its | |
595 | * bound to the cpu again. | |
596 | */ | |
597 | void kthread_unpark(struct task_struct *k) | |
f2530dc7 | 598 | { |
cf380a4a ON |
599 | struct kthread *kthread = to_kthread(k); |
600 | ||
f2530dc7 | 601 | /* |
85f1abe0 PZ |
602 | * Newly created kthread was parked when the CPU was offline. |
603 | * The binding was lost and we need to set it again. | |
f2530dc7 | 604 | */ |
85f1abe0 PZ |
605 | if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags)) |
606 | __kthread_bind(k, kthread->cpu, TASK_PARKED); | |
607 | ||
608 | clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags); | |
1cef1150 PZ |
609 | /* |
610 | * __kthread_parkme() will either see !SHOULD_PARK or get the wakeup. | |
611 | */ | |
85f1abe0 | 612 | wake_up_state(k, TASK_PARKED); |
f2530dc7 | 613 | } |
18896451 | 614 | EXPORT_SYMBOL_GPL(kthread_unpark); |
2a1d4460 TG |
615 | |
616 | /** | |
617 | * kthread_park - park a thread created by kthread_create(). | |
618 | * @k: thread created by kthread_create(). | |
619 | * | |
620 | * Sets kthread_should_park() for @k to return true, wakes it, and | |
621 | * waits for it to return. This can also be called after kthread_create() | |
622 | * instead of calling wake_up_process(): the thread will park without | |
623 | * calling threadfn(). | |
624 | * | |
625 | * Returns 0 if the thread is parked, -ENOSYS if the thread exited. | |
626 | * If called by the kthread itself just the park bit is set. | |
627 | */ | |
628 | int kthread_park(struct task_struct *k) | |
629 | { | |
cf380a4a ON |
630 | struct kthread *kthread = to_kthread(k); |
631 | ||
632 | if (WARN_ON(k->flags & PF_EXITING)) | |
633 | return -ENOSYS; | |
634 | ||
f83ee19b PZ |
635 | if (WARN_ON_ONCE(test_bit(KTHREAD_SHOULD_PARK, &kthread->flags))) |
636 | return -EBUSY; | |
637 | ||
85f1abe0 PZ |
638 | set_bit(KTHREAD_SHOULD_PARK, &kthread->flags); |
639 | if (k != current) { | |
640 | wake_up_process(k); | |
1cef1150 PZ |
641 | /* |
642 | * Wait for __kthread_parkme() to complete(), this means we | |
643 | * _will_ have TASK_PARKED and are about to call schedule(). | |
644 | */ | |
85f1abe0 | 645 | wait_for_completion(&kthread->parked); |
1cef1150 PZ |
646 | /* |
647 | * Now wait for that schedule() to complete and the task to | |
648 | * get scheduled out. | |
649 | */ | |
650 | WARN_ON_ONCE(!wait_task_inactive(k, TASK_PARKED)); | |
2a1d4460 | 651 | } |
cf380a4a ON |
652 | |
653 | return 0; | |
2a1d4460 | 654 | } |
18896451 | 655 | EXPORT_SYMBOL_GPL(kthread_park); |
2a1d4460 | 656 | |
9e37bd30 RD |
657 | /** |
658 | * kthread_stop - stop a thread created by kthread_create(). | |
659 | * @k: thread created by kthread_create(). | |
660 | * | |
661 | * Sets kthread_should_stop() for @k to return true, wakes it, and | |
9ae26027 ON |
662 | * waits for it to exit. This can also be called after kthread_create() |
663 | * instead of calling wake_up_process(): the thread will exit without | |
664 | * calling threadfn(). | |
665 | * | |
bbda86e9 | 666 | * If threadfn() may call kthread_exit() itself, the caller must ensure |
9ae26027 | 667 | * task_struct can't go away. |
9e37bd30 RD |
668 | * |
669 | * Returns the result of threadfn(), or %-EINTR if wake_up_process() | |
670 | * was never called. | |
671 | */ | |
1da177e4 LT |
672 | int kthread_stop(struct task_struct *k) |
673 | { | |
b5c5442b | 674 | struct kthread *kthread; |
1da177e4 LT |
675 | int ret; |
676 | ||
0a16b607 | 677 | trace_sched_kthread_stop(k); |
b5c5442b ON |
678 | |
679 | get_task_struct(k); | |
efb29fbf ON |
680 | kthread = to_kthread(k); |
681 | set_bit(KTHREAD_SHOULD_STOP, &kthread->flags); | |
cf380a4a | 682 | kthread_unpark(k); |
efb29fbf ON |
683 | wake_up_process(k); |
684 | wait_for_completion(&kthread->exited); | |
6b124879 | 685 | ret = kthread->result; |
1da177e4 | 686 | put_task_struct(k); |
0a16b607 | 687 | |
b5c5442b | 688 | trace_sched_kthread_stop_ret(ret); |
1da177e4 LT |
689 | return ret; |
690 | } | |
52e92e57 | 691 | EXPORT_SYMBOL(kthread_stop); |
1da177e4 | 692 | |
e804a4a4 | 693 | int kthreadd(void *unused) |
1da177e4 | 694 | { |
73c27992 | 695 | struct task_struct *tsk = current; |
1da177e4 | 696 | |
e804a4a4 | 697 | /* Setup a clean context for our children to inherit. */ |
73c27992 | 698 | set_task_comm(tsk, "kthreadd"); |
10ab825b | 699 | ignore_signals(tsk); |
9cc5b865 | 700 | set_cpus_allowed_ptr(tsk, housekeeping_cpumask(HK_FLAG_KTHREAD)); |
aee4faa4 | 701 | set_mems_allowed(node_states[N_MEMORY]); |
73c27992 | 702 | |
34b087e4 | 703 | current->flags |= PF_NOFREEZE; |
77f88796 | 704 | cgroup_init_kthreadd(); |
73c27992 EB |
705 | |
706 | for (;;) { | |
707 | set_current_state(TASK_INTERRUPTIBLE); | |
708 | if (list_empty(&kthread_create_list)) | |
709 | schedule(); | |
710 | __set_current_state(TASK_RUNNING); | |
711 | ||
712 | spin_lock(&kthread_create_lock); | |
713 | while (!list_empty(&kthread_create_list)) { | |
714 | struct kthread_create_info *create; | |
715 | ||
716 | create = list_entry(kthread_create_list.next, | |
717 | struct kthread_create_info, list); | |
718 | list_del_init(&create->list); | |
719 | spin_unlock(&kthread_create_lock); | |
720 | ||
721 | create_kthread(create); | |
722 | ||
723 | spin_lock(&kthread_create_lock); | |
724 | } | |
725 | spin_unlock(&kthread_create_lock); | |
726 | } | |
727 | ||
728 | return 0; | |
729 | } | |
b56c0d89 | 730 | |
3989144f | 731 | void __kthread_init_worker(struct kthread_worker *worker, |
4f32e9b1 YZ |
732 | const char *name, |
733 | struct lock_class_key *key) | |
734 | { | |
dbf52682 | 735 | memset(worker, 0, sizeof(struct kthread_worker)); |
fe99a4f4 | 736 | raw_spin_lock_init(&worker->lock); |
4f32e9b1 YZ |
737 | lockdep_set_class_and_name(&worker->lock, key, name); |
738 | INIT_LIST_HEAD(&worker->work_list); | |
22597dc3 | 739 | INIT_LIST_HEAD(&worker->delayed_work_list); |
4f32e9b1 | 740 | } |
3989144f | 741 | EXPORT_SYMBOL_GPL(__kthread_init_worker); |
4f32e9b1 | 742 | |
b56c0d89 TH |
743 | /** |
744 | * kthread_worker_fn - kthread function to process kthread_worker | |
745 | * @worker_ptr: pointer to initialized kthread_worker | |
746 | * | |
fbae2d44 PM |
747 | * This function implements the main cycle of kthread worker. It processes |
748 | * work_list until it is stopped with kthread_stop(). It sleeps when the queue | |
749 | * is empty. | |
b56c0d89 | 750 | * |
fbae2d44 PM |
751 | * The works are not allowed to keep any locks, disable preemption or interrupts |
752 | * when they finish. There is defined a safe point for freezing when one work | |
753 | * finishes and before a new one is started. | |
8197b3d4 PM |
754 | * |
755 | * Also the works must not be handled by more than one worker at the same time, | |
756 | * see also kthread_queue_work(). | |
b56c0d89 TH |
757 | */ |
758 | int kthread_worker_fn(void *worker_ptr) | |
759 | { | |
760 | struct kthread_worker *worker = worker_ptr; | |
761 | struct kthread_work *work; | |
762 | ||
fbae2d44 PM |
763 | /* |
764 | * FIXME: Update the check and remove the assignment when all kthread | |
765 | * worker users are created using kthread_create_worker*() functions. | |
766 | */ | |
767 | WARN_ON(worker->task && worker->task != current); | |
b56c0d89 | 768 | worker->task = current; |
dbf52682 PM |
769 | |
770 | if (worker->flags & KTW_FREEZABLE) | |
771 | set_freezable(); | |
772 | ||
b56c0d89 TH |
773 | repeat: |
774 | set_current_state(TASK_INTERRUPTIBLE); /* mb paired w/ kthread_stop */ | |
775 | ||
776 | if (kthread_should_stop()) { | |
777 | __set_current_state(TASK_RUNNING); | |
fe99a4f4 | 778 | raw_spin_lock_irq(&worker->lock); |
b56c0d89 | 779 | worker->task = NULL; |
fe99a4f4 | 780 | raw_spin_unlock_irq(&worker->lock); |
b56c0d89 TH |
781 | return 0; |
782 | } | |
783 | ||
784 | work = NULL; | |
fe99a4f4 | 785 | raw_spin_lock_irq(&worker->lock); |
b56c0d89 TH |
786 | if (!list_empty(&worker->work_list)) { |
787 | work = list_first_entry(&worker->work_list, | |
788 | struct kthread_work, node); | |
789 | list_del_init(&work->node); | |
790 | } | |
46f3d976 | 791 | worker->current_work = work; |
fe99a4f4 | 792 | raw_spin_unlock_irq(&worker->lock); |
b56c0d89 TH |
793 | |
794 | if (work) { | |
f630c7c6 | 795 | kthread_work_func_t func = work->func; |
b56c0d89 | 796 | __set_current_state(TASK_RUNNING); |
f630c7c6 | 797 | trace_sched_kthread_work_execute_start(work); |
b56c0d89 | 798 | work->func(work); |
f630c7c6 RC |
799 | /* |
800 | * Avoid dereferencing work after this point. The trace | |
801 | * event only cares about the address. | |
802 | */ | |
803 | trace_sched_kthread_work_execute_end(work, func); | |
b56c0d89 TH |
804 | } else if (!freezing(current)) |
805 | schedule(); | |
806 | ||
807 | try_to_freeze(); | |
22cf8bc6 | 808 | cond_resched(); |
b56c0d89 TH |
809 | goto repeat; |
810 | } | |
811 | EXPORT_SYMBOL_GPL(kthread_worker_fn); | |
812 | ||
c0b942a7 | 813 | static __printf(3, 0) struct kthread_worker * |
dbf52682 PM |
814 | __kthread_create_worker(int cpu, unsigned int flags, |
815 | const char namefmt[], va_list args) | |
fbae2d44 PM |
816 | { |
817 | struct kthread_worker *worker; | |
818 | struct task_struct *task; | |
98fa15f3 | 819 | int node = NUMA_NO_NODE; |
fbae2d44 PM |
820 | |
821 | worker = kzalloc(sizeof(*worker), GFP_KERNEL); | |
822 | if (!worker) | |
823 | return ERR_PTR(-ENOMEM); | |
824 | ||
825 | kthread_init_worker(worker); | |
826 | ||
8fb9dcbd ON |
827 | if (cpu >= 0) |
828 | node = cpu_to_node(cpu); | |
fbae2d44 | 829 | |
8fb9dcbd ON |
830 | task = __kthread_create_on_node(kthread_worker_fn, worker, |
831 | node, namefmt, args); | |
fbae2d44 PM |
832 | if (IS_ERR(task)) |
833 | goto fail_task; | |
834 | ||
8fb9dcbd ON |
835 | if (cpu >= 0) |
836 | kthread_bind(task, cpu); | |
837 | ||
dbf52682 | 838 | worker->flags = flags; |
fbae2d44 PM |
839 | worker->task = task; |
840 | wake_up_process(task); | |
841 | return worker; | |
842 | ||
843 | fail_task: | |
844 | kfree(worker); | |
845 | return ERR_CAST(task); | |
846 | } | |
847 | ||
848 | /** | |
849 | * kthread_create_worker - create a kthread worker | |
dbf52682 | 850 | * @flags: flags modifying the default behavior of the worker |
fbae2d44 PM |
851 | * @namefmt: printf-style name for the kthread worker (task). |
852 | * | |
853 | * Returns a pointer to the allocated worker on success, ERR_PTR(-ENOMEM) | |
854 | * when the needed structures could not get allocated, and ERR_PTR(-EINTR) | |
855 | * when the worker was SIGKILLed. | |
856 | */ | |
857 | struct kthread_worker * | |
dbf52682 | 858 | kthread_create_worker(unsigned int flags, const char namefmt[], ...) |
fbae2d44 PM |
859 | { |
860 | struct kthread_worker *worker; | |
861 | va_list args; | |
862 | ||
863 | va_start(args, namefmt); | |
dbf52682 | 864 | worker = __kthread_create_worker(-1, flags, namefmt, args); |
fbae2d44 PM |
865 | va_end(args); |
866 | ||
867 | return worker; | |
868 | } | |
869 | EXPORT_SYMBOL(kthread_create_worker); | |
870 | ||
871 | /** | |
872 | * kthread_create_worker_on_cpu - create a kthread worker and bind it | |
7b7b8a2c | 873 | * to a given CPU and the associated NUMA node. |
fbae2d44 | 874 | * @cpu: CPU number |
dbf52682 | 875 | * @flags: flags modifying the default behavior of the worker |
fbae2d44 PM |
876 | * @namefmt: printf-style name for the kthread worker (task). |
877 | * | |
878 | * Use a valid CPU number if you want to bind the kthread worker | |
879 | * to the given CPU and the associated NUMA node. | |
880 | * | |
881 | * A good practice is to add the cpu number also into the worker name. | |
882 | * For example, use kthread_create_worker_on_cpu(cpu, "helper/%d", cpu). | |
883 | * | |
ebb2bdce PM |
884 | * CPU hotplug: |
885 | * The kthread worker API is simple and generic. It just provides a way | |
886 | * to create, use, and destroy workers. | |
887 | * | |
888 | * It is up to the API user how to handle CPU hotplug. They have to decide | |
889 | * how to handle pending work items, prevent queuing new ones, and | |
890 | * restore the functionality when the CPU goes off and on. There are a | |
891 | * few catches: | |
892 | * | |
893 | * - CPU affinity gets lost when it is scheduled on an offline CPU. | |
894 | * | |
895 | * - The worker might not exist when the CPU was off when the user | |
896 | * created the workers. | |
897 | * | |
898 | * Good practice is to implement two CPU hotplug callbacks and to | |
899 | * destroy/create the worker when the CPU goes down/up. | |
900 | * | |
901 | * Return: | |
902 | * The pointer to the allocated worker on success, ERR_PTR(-ENOMEM) | |
fbae2d44 PM |
903 | * when the needed structures could not get allocated, and ERR_PTR(-EINTR) |
904 | * when the worker was SIGKILLed. | |
905 | */ | |
906 | struct kthread_worker * | |
dbf52682 PM |
907 | kthread_create_worker_on_cpu(int cpu, unsigned int flags, |
908 | const char namefmt[], ...) | |
fbae2d44 PM |
909 | { |
910 | struct kthread_worker *worker; | |
911 | va_list args; | |
912 | ||
913 | va_start(args, namefmt); | |
dbf52682 | 914 | worker = __kthread_create_worker(cpu, flags, namefmt, args); |
fbae2d44 PM |
915 | va_end(args); |
916 | ||
917 | return worker; | |
918 | } | |
919 | EXPORT_SYMBOL(kthread_create_worker_on_cpu); | |
920 | ||
37be45d4 PM |
921 | /* |
922 | * Returns true when the work could not be queued at the moment. | |
923 | * It happens when it is already pending in a worker list | |
924 | * or when it is being cancelled. | |
925 | */ | |
926 | static inline bool queuing_blocked(struct kthread_worker *worker, | |
927 | struct kthread_work *work) | |
928 | { | |
929 | lockdep_assert_held(&worker->lock); | |
930 | ||
931 | return !list_empty(&work->node) || work->canceling; | |
932 | } | |
933 | ||
8197b3d4 PM |
934 | static void kthread_insert_work_sanity_check(struct kthread_worker *worker, |
935 | struct kthread_work *work) | |
936 | { | |
937 | lockdep_assert_held(&worker->lock); | |
938 | WARN_ON_ONCE(!list_empty(&work->node)); | |
939 | /* Do not use a work with >1 worker, see kthread_queue_work() */ | |
940 | WARN_ON_ONCE(work->worker && work->worker != worker); | |
941 | } | |
942 | ||
9a2e03d8 | 943 | /* insert @work before @pos in @worker */ |
3989144f | 944 | static void kthread_insert_work(struct kthread_worker *worker, |
8197b3d4 PM |
945 | struct kthread_work *work, |
946 | struct list_head *pos) | |
9a2e03d8 | 947 | { |
8197b3d4 | 948 | kthread_insert_work_sanity_check(worker, work); |
9a2e03d8 | 949 | |
f630c7c6 RC |
950 | trace_sched_kthread_work_queue_work(worker, work); |
951 | ||
9a2e03d8 | 952 | list_add_tail(&work->node, pos); |
46f3d976 | 953 | work->worker = worker; |
ed1403ec | 954 | if (!worker->current_work && likely(worker->task)) |
9a2e03d8 TH |
955 | wake_up_process(worker->task); |
956 | } | |
957 | ||
b56c0d89 | 958 | /** |
3989144f | 959 | * kthread_queue_work - queue a kthread_work |
b56c0d89 TH |
960 | * @worker: target kthread_worker |
961 | * @work: kthread_work to queue | |
962 | * | |
963 | * Queue @work to work processor @task for async execution. @task | |
964 | * must have been created with kthread_worker_create(). Returns %true | |
965 | * if @work was successfully queued, %false if it was already pending. | |
8197b3d4 PM |
966 | * |
967 | * Reinitialize the work if it needs to be used by another worker. | |
968 | * For example, when the worker was stopped and started again. | |
b56c0d89 | 969 | */ |
3989144f | 970 | bool kthread_queue_work(struct kthread_worker *worker, |
b56c0d89 TH |
971 | struct kthread_work *work) |
972 | { | |
973 | bool ret = false; | |
974 | unsigned long flags; | |
975 | ||
fe99a4f4 | 976 | raw_spin_lock_irqsave(&worker->lock, flags); |
37be45d4 | 977 | if (!queuing_blocked(worker, work)) { |
3989144f | 978 | kthread_insert_work(worker, work, &worker->work_list); |
b56c0d89 TH |
979 | ret = true; |
980 | } | |
fe99a4f4 | 981 | raw_spin_unlock_irqrestore(&worker->lock, flags); |
b56c0d89 TH |
982 | return ret; |
983 | } | |
3989144f | 984 | EXPORT_SYMBOL_GPL(kthread_queue_work); |
b56c0d89 | 985 | |
22597dc3 PM |
986 | /** |
987 | * kthread_delayed_work_timer_fn - callback that queues the associated kthread | |
988 | * delayed work when the timer expires. | |
fe5c3b69 | 989 | * @t: pointer to the expired timer |
22597dc3 PM |
990 | * |
991 | * The format of the function is defined by struct timer_list. | |
992 | * It should have been called from irqsafe timer with irq already off. | |
993 | */ | |
fe5c3b69 | 994 | void kthread_delayed_work_timer_fn(struct timer_list *t) |
22597dc3 | 995 | { |
fe5c3b69 | 996 | struct kthread_delayed_work *dwork = from_timer(dwork, t, timer); |
22597dc3 PM |
997 | struct kthread_work *work = &dwork->work; |
998 | struct kthread_worker *worker = work->worker; | |
ad01423a | 999 | unsigned long flags; |
22597dc3 PM |
1000 | |
1001 | /* | |
1002 | * This might happen when a pending work is reinitialized. | |
1003 | * It means that it is used a wrong way. | |
1004 | */ | |
1005 | if (WARN_ON_ONCE(!worker)) | |
1006 | return; | |
1007 | ||
ad01423a | 1008 | raw_spin_lock_irqsave(&worker->lock, flags); |
22597dc3 PM |
1009 | /* Work must not be used with >1 worker, see kthread_queue_work(). */ |
1010 | WARN_ON_ONCE(work->worker != worker); | |
1011 | ||
1012 | /* Move the work from worker->delayed_work_list. */ | |
1013 | WARN_ON_ONCE(list_empty(&work->node)); | |
1014 | list_del_init(&work->node); | |
6993d0fd Z |
1015 | if (!work->canceling) |
1016 | kthread_insert_work(worker, work, &worker->work_list); | |
22597dc3 | 1017 | |
ad01423a | 1018 | raw_spin_unlock_irqrestore(&worker->lock, flags); |
22597dc3 PM |
1019 | } |
1020 | EXPORT_SYMBOL(kthread_delayed_work_timer_fn); | |
1021 | ||
bc88f85c BD |
1022 | static void __kthread_queue_delayed_work(struct kthread_worker *worker, |
1023 | struct kthread_delayed_work *dwork, | |
1024 | unsigned long delay) | |
22597dc3 PM |
1025 | { |
1026 | struct timer_list *timer = &dwork->timer; | |
1027 | struct kthread_work *work = &dwork->work; | |
1028 | ||
0a5b4128 ST |
1029 | WARN_ON_FUNCTION_MISMATCH(timer->function, |
1030 | kthread_delayed_work_timer_fn); | |
22597dc3 PM |
1031 | |
1032 | /* | |
1033 | * If @delay is 0, queue @dwork->work immediately. This is for | |
1034 | * both optimization and correctness. The earliest @timer can | |
1035 | * expire is on the closest next tick and delayed_work users depend | |
1036 | * on that there's no such delay when @delay is 0. | |
1037 | */ | |
1038 | if (!delay) { | |
1039 | kthread_insert_work(worker, work, &worker->work_list); | |
1040 | return; | |
1041 | } | |
1042 | ||
1043 | /* Be paranoid and try to detect possible races already now. */ | |
1044 | kthread_insert_work_sanity_check(worker, work); | |
1045 | ||
1046 | list_add(&work->node, &worker->delayed_work_list); | |
1047 | work->worker = worker; | |
22597dc3 PM |
1048 | timer->expires = jiffies + delay; |
1049 | add_timer(timer); | |
1050 | } | |
1051 | ||
1052 | /** | |
1053 | * kthread_queue_delayed_work - queue the associated kthread work | |
1054 | * after a delay. | |
1055 | * @worker: target kthread_worker | |
1056 | * @dwork: kthread_delayed_work to queue | |
1057 | * @delay: number of jiffies to wait before queuing | |
1058 | * | |
1059 | * If the work has not been pending it starts a timer that will queue | |
1060 | * the work after the given @delay. If @delay is zero, it queues the | |
1061 | * work immediately. | |
1062 | * | |
1063 | * Return: %false if the @work has already been pending. It means that | |
1064 | * either the timer was running or the work was queued. It returns %true | |
1065 | * otherwise. | |
1066 | */ | |
1067 | bool kthread_queue_delayed_work(struct kthread_worker *worker, | |
1068 | struct kthread_delayed_work *dwork, | |
1069 | unsigned long delay) | |
1070 | { | |
1071 | struct kthread_work *work = &dwork->work; | |
1072 | unsigned long flags; | |
1073 | bool ret = false; | |
1074 | ||
fe99a4f4 | 1075 | raw_spin_lock_irqsave(&worker->lock, flags); |
22597dc3 | 1076 | |
37be45d4 | 1077 | if (!queuing_blocked(worker, work)) { |
22597dc3 PM |
1078 | __kthread_queue_delayed_work(worker, dwork, delay); |
1079 | ret = true; | |
1080 | } | |
1081 | ||
fe99a4f4 | 1082 | raw_spin_unlock_irqrestore(&worker->lock, flags); |
22597dc3 PM |
1083 | return ret; |
1084 | } | |
1085 | EXPORT_SYMBOL_GPL(kthread_queue_delayed_work); | |
1086 | ||
9a2e03d8 TH |
1087 | struct kthread_flush_work { |
1088 | struct kthread_work work; | |
1089 | struct completion done; | |
1090 | }; | |
1091 | ||
1092 | static void kthread_flush_work_fn(struct kthread_work *work) | |
1093 | { | |
1094 | struct kthread_flush_work *fwork = | |
1095 | container_of(work, struct kthread_flush_work, work); | |
1096 | complete(&fwork->done); | |
1097 | } | |
1098 | ||
b56c0d89 | 1099 | /** |
3989144f | 1100 | * kthread_flush_work - flush a kthread_work |
b56c0d89 TH |
1101 | * @work: work to flush |
1102 | * | |
1103 | * If @work is queued or executing, wait for it to finish execution. | |
1104 | */ | |
3989144f | 1105 | void kthread_flush_work(struct kthread_work *work) |
b56c0d89 | 1106 | { |
46f3d976 TH |
1107 | struct kthread_flush_work fwork = { |
1108 | KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn), | |
1109 | COMPLETION_INITIALIZER_ONSTACK(fwork.done), | |
1110 | }; | |
1111 | struct kthread_worker *worker; | |
1112 | bool noop = false; | |
1113 | ||
46f3d976 TH |
1114 | worker = work->worker; |
1115 | if (!worker) | |
1116 | return; | |
b56c0d89 | 1117 | |
fe99a4f4 | 1118 | raw_spin_lock_irq(&worker->lock); |
8197b3d4 PM |
1119 | /* Work must not be used with >1 worker, see kthread_queue_work(). */ |
1120 | WARN_ON_ONCE(work->worker != worker); | |
b56c0d89 | 1121 | |
46f3d976 | 1122 | if (!list_empty(&work->node)) |
3989144f | 1123 | kthread_insert_work(worker, &fwork.work, work->node.next); |
46f3d976 | 1124 | else if (worker->current_work == work) |
3989144f PM |
1125 | kthread_insert_work(worker, &fwork.work, |
1126 | worker->work_list.next); | |
46f3d976 TH |
1127 | else |
1128 | noop = true; | |
b56c0d89 | 1129 | |
fe99a4f4 | 1130 | raw_spin_unlock_irq(&worker->lock); |
b56c0d89 | 1131 | |
46f3d976 TH |
1132 | if (!noop) |
1133 | wait_for_completion(&fwork.done); | |
b56c0d89 | 1134 | } |
3989144f | 1135 | EXPORT_SYMBOL_GPL(kthread_flush_work); |
b56c0d89 | 1136 | |
34b3d534 PM |
1137 | /* |
1138 | * Make sure that the timer is neither set nor running and could | |
1139 | * not manipulate the work list_head any longer. | |
1140 | * | |
1141 | * The function is called under worker->lock. The lock is temporary | |
1142 | * released but the timer can't be set again in the meantime. | |
1143 | */ | |
1144 | static void kthread_cancel_delayed_work_timer(struct kthread_work *work, | |
1145 | unsigned long *flags) | |
1146 | { | |
1147 | struct kthread_delayed_work *dwork = | |
1148 | container_of(work, struct kthread_delayed_work, work); | |
1149 | struct kthread_worker *worker = work->worker; | |
1150 | ||
1151 | /* | |
1152 | * del_timer_sync() must be called to make sure that the timer | |
1153 | * callback is not running. The lock must be temporary released | |
1154 | * to avoid a deadlock with the callback. In the meantime, | |
1155 | * any queuing is blocked by setting the canceling counter. | |
1156 | */ | |
1157 | work->canceling++; | |
1158 | raw_spin_unlock_irqrestore(&worker->lock, *flags); | |
1159 | del_timer_sync(&dwork->timer); | |
1160 | raw_spin_lock_irqsave(&worker->lock, *flags); | |
1161 | work->canceling--; | |
1162 | } | |
1163 | ||
37be45d4 | 1164 | /* |
5fa54346 PM |
1165 | * This function removes the work from the worker queue. |
1166 | * | |
1167 | * It is called under worker->lock. The caller must make sure that | |
1168 | * the timer used by delayed work is not running, e.g. by calling | |
1169 | * kthread_cancel_delayed_work_timer(). | |
37be45d4 PM |
1170 | * |
1171 | * The work might still be in use when this function finishes. See the | |
1172 | * current_work proceed by the worker. | |
1173 | * | |
1174 | * Return: %true if @work was pending and successfully canceled, | |
1175 | * %false if @work was not pending | |
1176 | */ | |
5fa54346 | 1177 | static bool __kthread_cancel_work(struct kthread_work *work) |
37be45d4 | 1178 | { |
37be45d4 PM |
1179 | /* |
1180 | * Try to remove the work from a worker list. It might either | |
1181 | * be from worker->work_list or from worker->delayed_work_list. | |
1182 | */ | |
1183 | if (!list_empty(&work->node)) { | |
1184 | list_del_init(&work->node); | |
1185 | return true; | |
1186 | } | |
1187 | ||
1188 | return false; | |
1189 | } | |
1190 | ||
9a6b06c8 PM |
1191 | /** |
1192 | * kthread_mod_delayed_work - modify delay of or queue a kthread delayed work | |
1193 | * @worker: kthread worker to use | |
1194 | * @dwork: kthread delayed work to queue | |
1195 | * @delay: number of jiffies to wait before queuing | |
1196 | * | |
1197 | * If @dwork is idle, equivalent to kthread_queue_delayed_work(). Otherwise, | |
1198 | * modify @dwork's timer so that it expires after @delay. If @delay is zero, | |
1199 | * @work is guaranteed to be queued immediately. | |
1200 | * | |
d71ba164 | 1201 | * Return: %false if @dwork was idle and queued, %true otherwise. |
9a6b06c8 PM |
1202 | * |
1203 | * A special case is when the work is being canceled in parallel. | |
1204 | * It might be caused either by the real kthread_cancel_delayed_work_sync() | |
1205 | * or yet another kthread_mod_delayed_work() call. We let the other command | |
d71ba164 PM |
1206 | * win and return %true here. The return value can be used for reference |
1207 | * counting and the number of queued works stays the same. Anyway, the caller | |
1208 | * is supposed to synchronize these operations a reasonable way. | |
9a6b06c8 PM |
1209 | * |
1210 | * This function is safe to call from any context including IRQ handler. | |
1211 | * See __kthread_cancel_work() and kthread_delayed_work_timer_fn() | |
1212 | * for details. | |
1213 | */ | |
1214 | bool kthread_mod_delayed_work(struct kthread_worker *worker, | |
1215 | struct kthread_delayed_work *dwork, | |
1216 | unsigned long delay) | |
1217 | { | |
1218 | struct kthread_work *work = &dwork->work; | |
1219 | unsigned long flags; | |
d71ba164 | 1220 | int ret; |
9a6b06c8 | 1221 | |
fe99a4f4 | 1222 | raw_spin_lock_irqsave(&worker->lock, flags); |
9a6b06c8 PM |
1223 | |
1224 | /* Do not bother with canceling when never queued. */ | |
d71ba164 PM |
1225 | if (!work->worker) { |
1226 | ret = false; | |
9a6b06c8 | 1227 | goto fast_queue; |
d71ba164 | 1228 | } |
9a6b06c8 PM |
1229 | |
1230 | /* Work must not be used with >1 worker, see kthread_queue_work() */ | |
1231 | WARN_ON_ONCE(work->worker != worker); | |
1232 | ||
5fa54346 PM |
1233 | /* |
1234 | * Temporary cancel the work but do not fight with another command | |
1235 | * that is canceling the work as well. | |
1236 | * | |
1237 | * It is a bit tricky because of possible races with another | |
1238 | * mod_delayed_work() and cancel_delayed_work() callers. | |
1239 | * | |
1240 | * The timer must be canceled first because worker->lock is released | |
1241 | * when doing so. But the work can be removed from the queue (list) | |
1242 | * only when it can be queued again so that the return value can | |
1243 | * be used for reference counting. | |
1244 | */ | |
1245 | kthread_cancel_delayed_work_timer(work, &flags); | |
d71ba164 PM |
1246 | if (work->canceling) { |
1247 | /* The number of works in the queue does not change. */ | |
1248 | ret = true; | |
9a6b06c8 | 1249 | goto out; |
d71ba164 | 1250 | } |
5fa54346 | 1251 | ret = __kthread_cancel_work(work); |
9a6b06c8 | 1252 | |
9a6b06c8 PM |
1253 | fast_queue: |
1254 | __kthread_queue_delayed_work(worker, dwork, delay); | |
1255 | out: | |
fe99a4f4 | 1256 | raw_spin_unlock_irqrestore(&worker->lock, flags); |
9a6b06c8 PM |
1257 | return ret; |
1258 | } | |
1259 | EXPORT_SYMBOL_GPL(kthread_mod_delayed_work); | |
1260 | ||
37be45d4 PM |
1261 | static bool __kthread_cancel_work_sync(struct kthread_work *work, bool is_dwork) |
1262 | { | |
1263 | struct kthread_worker *worker = work->worker; | |
1264 | unsigned long flags; | |
1265 | int ret = false; | |
1266 | ||
1267 | if (!worker) | |
1268 | goto out; | |
1269 | ||
fe99a4f4 | 1270 | raw_spin_lock_irqsave(&worker->lock, flags); |
37be45d4 PM |
1271 | /* Work must not be used with >1 worker, see kthread_queue_work(). */ |
1272 | WARN_ON_ONCE(work->worker != worker); | |
1273 | ||
5fa54346 PM |
1274 | if (is_dwork) |
1275 | kthread_cancel_delayed_work_timer(work, &flags); | |
1276 | ||
1277 | ret = __kthread_cancel_work(work); | |
37be45d4 PM |
1278 | |
1279 | if (worker->current_work != work) | |
1280 | goto out_fast; | |
1281 | ||
1282 | /* | |
1283 | * The work is in progress and we need to wait with the lock released. | |
1284 | * In the meantime, block any queuing by setting the canceling counter. | |
1285 | */ | |
1286 | work->canceling++; | |
fe99a4f4 | 1287 | raw_spin_unlock_irqrestore(&worker->lock, flags); |
37be45d4 | 1288 | kthread_flush_work(work); |
fe99a4f4 | 1289 | raw_spin_lock_irqsave(&worker->lock, flags); |
37be45d4 PM |
1290 | work->canceling--; |
1291 | ||
1292 | out_fast: | |
fe99a4f4 | 1293 | raw_spin_unlock_irqrestore(&worker->lock, flags); |
37be45d4 PM |
1294 | out: |
1295 | return ret; | |
1296 | } | |
1297 | ||
1298 | /** | |
1299 | * kthread_cancel_work_sync - cancel a kthread work and wait for it to finish | |
1300 | * @work: the kthread work to cancel | |
1301 | * | |
1302 | * Cancel @work and wait for its execution to finish. This function | |
1303 | * can be used even if the work re-queues itself. On return from this | |
1304 | * function, @work is guaranteed to be not pending or executing on any CPU. | |
1305 | * | |
1306 | * kthread_cancel_work_sync(&delayed_work->work) must not be used for | |
1307 | * delayed_work's. Use kthread_cancel_delayed_work_sync() instead. | |
1308 | * | |
1309 | * The caller must ensure that the worker on which @work was last | |
1310 | * queued can't be destroyed before this function returns. | |
1311 | * | |
1312 | * Return: %true if @work was pending, %false otherwise. | |
1313 | */ | |
1314 | bool kthread_cancel_work_sync(struct kthread_work *work) | |
1315 | { | |
1316 | return __kthread_cancel_work_sync(work, false); | |
1317 | } | |
1318 | EXPORT_SYMBOL_GPL(kthread_cancel_work_sync); | |
1319 | ||
1320 | /** | |
1321 | * kthread_cancel_delayed_work_sync - cancel a kthread delayed work and | |
1322 | * wait for it to finish. | |
1323 | * @dwork: the kthread delayed work to cancel | |
1324 | * | |
1325 | * This is kthread_cancel_work_sync() for delayed works. | |
1326 | * | |
1327 | * Return: %true if @dwork was pending, %false otherwise. | |
1328 | */ | |
1329 | bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *dwork) | |
1330 | { | |
1331 | return __kthread_cancel_work_sync(&dwork->work, true); | |
1332 | } | |
1333 | EXPORT_SYMBOL_GPL(kthread_cancel_delayed_work_sync); | |
1334 | ||
b56c0d89 | 1335 | /** |
3989144f | 1336 | * kthread_flush_worker - flush all current works on a kthread_worker |
b56c0d89 TH |
1337 | * @worker: worker to flush |
1338 | * | |
1339 | * Wait until all currently executing or pending works on @worker are | |
1340 | * finished. | |
1341 | */ | |
3989144f | 1342 | void kthread_flush_worker(struct kthread_worker *worker) |
b56c0d89 TH |
1343 | { |
1344 | struct kthread_flush_work fwork = { | |
1345 | KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn), | |
1346 | COMPLETION_INITIALIZER_ONSTACK(fwork.done), | |
1347 | }; | |
1348 | ||
3989144f | 1349 | kthread_queue_work(worker, &fwork.work); |
b56c0d89 TH |
1350 | wait_for_completion(&fwork.done); |
1351 | } | |
3989144f | 1352 | EXPORT_SYMBOL_GPL(kthread_flush_worker); |
35033fe9 PM |
1353 | |
1354 | /** | |
1355 | * kthread_destroy_worker - destroy a kthread worker | |
1356 | * @worker: worker to be destroyed | |
1357 | * | |
1358 | * Flush and destroy @worker. The simple flush is enough because the kthread | |
1359 | * worker API is used only in trivial scenarios. There are no multi-step state | |
1360 | * machines needed. | |
1361 | */ | |
1362 | void kthread_destroy_worker(struct kthread_worker *worker) | |
1363 | { | |
1364 | struct task_struct *task; | |
1365 | ||
1366 | task = worker->task; | |
1367 | if (WARN_ON(!task)) | |
1368 | return; | |
1369 | ||
1370 | kthread_flush_worker(worker); | |
1371 | kthread_stop(task); | |
1372 | WARN_ON(!list_empty(&worker->work_list)); | |
1373 | kfree(worker); | |
1374 | } | |
1375 | EXPORT_SYMBOL(kthread_destroy_worker); | |
05e3db95 | 1376 | |
f5678e7f CH |
1377 | /** |
1378 | * kthread_use_mm - make the calling kthread operate on an address space | |
1379 | * @mm: address space to operate on | |
9bf5b9eb | 1380 | */ |
f5678e7f | 1381 | void kthread_use_mm(struct mm_struct *mm) |
9bf5b9eb CH |
1382 | { |
1383 | struct mm_struct *active_mm; | |
1384 | struct task_struct *tsk = current; | |
1385 | ||
f5678e7f CH |
1386 | WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD)); |
1387 | WARN_ON_ONCE(tsk->mm); | |
1388 | ||
9bf5b9eb | 1389 | task_lock(tsk); |
38cf307c PZ |
1390 | /* Hold off tlb flush IPIs while switching mm's */ |
1391 | local_irq_disable(); | |
9bf5b9eb CH |
1392 | active_mm = tsk->active_mm; |
1393 | if (active_mm != mm) { | |
1394 | mmgrab(mm); | |
1395 | tsk->active_mm = mm; | |
1396 | } | |
1397 | tsk->mm = mm; | |
618758ed | 1398 | membarrier_update_current_mm(mm); |
38cf307c PZ |
1399 | switch_mm_irqs_off(active_mm, mm, tsk); |
1400 | local_irq_enable(); | |
9bf5b9eb CH |
1401 | task_unlock(tsk); |
1402 | #ifdef finish_arch_post_lock_switch | |
1403 | finish_arch_post_lock_switch(); | |
1404 | #endif | |
1405 | ||
618758ed MD |
1406 | /* |
1407 | * When a kthread starts operating on an address space, the loop | |
1408 | * in membarrier_{private,global}_expedited() may not observe | |
1409 | * that tsk->mm, and not issue an IPI. Membarrier requires a | |
1410 | * memory barrier after storing to tsk->mm, before accessing | |
1411 | * user-space memory. A full memory barrier for membarrier | |
1412 | * {PRIVATE,GLOBAL}_EXPEDITED is implicitly provided by | |
1413 | * mmdrop(), or explicitly with smp_mb(). | |
1414 | */ | |
9bf5b9eb CH |
1415 | if (active_mm != mm) |
1416 | mmdrop(active_mm); | |
618758ed MD |
1417 | else |
1418 | smp_mb(); | |
37c54f9b | 1419 | |
3d13f313 | 1420 | to_kthread(tsk)->oldfs = force_uaccess_begin(); |
9bf5b9eb | 1421 | } |
f5678e7f | 1422 | EXPORT_SYMBOL_GPL(kthread_use_mm); |
9bf5b9eb | 1423 | |
f5678e7f CH |
1424 | /** |
1425 | * kthread_unuse_mm - reverse the effect of kthread_use_mm() | |
1426 | * @mm: address space to operate on | |
9bf5b9eb | 1427 | */ |
f5678e7f | 1428 | void kthread_unuse_mm(struct mm_struct *mm) |
9bf5b9eb CH |
1429 | { |
1430 | struct task_struct *tsk = current; | |
1431 | ||
f5678e7f CH |
1432 | WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD)); |
1433 | WARN_ON_ONCE(!tsk->mm); | |
1434 | ||
3d13f313 | 1435 | force_uaccess_end(to_kthread(tsk)->oldfs); |
37c54f9b | 1436 | |
9bf5b9eb | 1437 | task_lock(tsk); |
618758ed MD |
1438 | /* |
1439 | * When a kthread stops operating on an address space, the loop | |
1440 | * in membarrier_{private,global}_expedited() may not observe | |
1441 | * that tsk->mm, and not issue an IPI. Membarrier requires a | |
1442 | * memory barrier after accessing user-space memory, before | |
1443 | * clearing tsk->mm. | |
1444 | */ | |
1445 | smp_mb__after_spinlock(); | |
9bf5b9eb | 1446 | sync_mm_rss(mm); |
38cf307c | 1447 | local_irq_disable(); |
9bf5b9eb | 1448 | tsk->mm = NULL; |
618758ed | 1449 | membarrier_update_current_mm(NULL); |
9bf5b9eb CH |
1450 | /* active_mm is still 'mm' */ |
1451 | enter_lazy_tlb(mm, tsk); | |
38cf307c | 1452 | local_irq_enable(); |
9bf5b9eb CH |
1453 | task_unlock(tsk); |
1454 | } | |
f5678e7f | 1455 | EXPORT_SYMBOL_GPL(kthread_unuse_mm); |
9bf5b9eb | 1456 | |
0b508bc9 | 1457 | #ifdef CONFIG_BLK_CGROUP |
05e3db95 SL |
1458 | /** |
1459 | * kthread_associate_blkcg - associate blkcg to current kthread | |
1460 | * @css: the cgroup info | |
1461 | * | |
1462 | * Current thread must be a kthread. The thread is running jobs on behalf of | |
1463 | * other threads. In some cases, we expect the jobs attach cgroup info of | |
1464 | * original threads instead of that of current thread. This function stores | |
1465 | * original thread's cgroup info in current kthread context for later | |
1466 | * retrieval. | |
1467 | */ | |
1468 | void kthread_associate_blkcg(struct cgroup_subsys_state *css) | |
1469 | { | |
1470 | struct kthread *kthread; | |
1471 | ||
1472 | if (!(current->flags & PF_KTHREAD)) | |
1473 | return; | |
1474 | kthread = to_kthread(current); | |
1475 | if (!kthread) | |
1476 | return; | |
1477 | ||
1478 | if (kthread->blkcg_css) { | |
1479 | css_put(kthread->blkcg_css); | |
1480 | kthread->blkcg_css = NULL; | |
1481 | } | |
1482 | if (css) { | |
1483 | css_get(css); | |
1484 | kthread->blkcg_css = css; | |
1485 | } | |
1486 | } | |
1487 | EXPORT_SYMBOL(kthread_associate_blkcg); | |
1488 | ||
1489 | /** | |
1490 | * kthread_blkcg - get associated blkcg css of current kthread | |
1491 | * | |
1492 | * Current thread must be a kthread. | |
1493 | */ | |
1494 | struct cgroup_subsys_state *kthread_blkcg(void) | |
1495 | { | |
1496 | struct kthread *kthread; | |
1497 | ||
1498 | if (current->flags & PF_KTHREAD) { | |
1499 | kthread = to_kthread(current); | |
1500 | if (kthread) | |
1501 | return kthread->blkcg_css; | |
1502 | } | |
1503 | return NULL; | |
1504 | } | |
1505 | EXPORT_SYMBOL(kthread_blkcg); | |
1506 | #endif |