1
2
3
4
5
6
7
8#include <linux/sched.h>
9#include <linux/kthread.h>
10#include <linux/completion.h>
11#include <linux/err.h>
12#include <linux/cpuset.h>
13#include <linux/unistd.h>
14#include <linux/file.h>
15#include <linux/export.h>
16#include <linux/mutex.h>
17#include <linux/slab.h>
18#include <linux/freezer.h>
19#include <linux/ptrace.h>
20#include <trace/events/sched.h>
21
22static DEFINE_SPINLOCK(kthread_create_lock);
23static LIST_HEAD(kthread_create_list);
24struct task_struct *kthreadd_task;
25
26struct kthread_create_info
27{
28
29 int (*threadfn)(void *data);
30 void *data;
31 int node;
32
33
34 struct task_struct *result;
35 struct completion done;
36
37 struct list_head list;
38};
39
40struct kthread {
41 unsigned long flags;
42 unsigned int cpu;
43 void *data;
44 struct completion parked;
45 struct completion exited;
46};
47
48enum KTHREAD_BITS {
49 KTHREAD_IS_PER_CPU = 0,
50 KTHREAD_SHOULD_STOP,
51 KTHREAD_SHOULD_PARK,
52 KTHREAD_IS_PARKED,
53};
54
55#define to_kthread(tsk) \
56 container_of((tsk)->vfork_done, struct kthread, exited)
57
58
59
60
61
62
63
64
65bool kthread_should_stop(void)
66{
67 return test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags);
68}
69EXPORT_SYMBOL(kthread_should_stop);
70
71
72
73
74
75
76
77
78
79
80
81
82bool kthread_should_park(void)
83{
84 return test_bit(KTHREAD_SHOULD_PARK, &to_kthread(current)->flags);
85}
86
87
88
89
90
91
92
93
94
95
96bool kthread_freezable_should_stop(bool *was_frozen)
97{
98 bool frozen = false;
99
100 might_sleep();
101
102 if (unlikely(freezing(current)))
103 frozen = __refrigerator(true);
104
105 if (was_frozen)
106 *was_frozen = frozen;
107
108 return kthread_should_stop();
109}
110EXPORT_SYMBOL_GPL(kthread_freezable_should_stop);
111
112
113
114
115
116
117
118
119
120void *kthread_data(struct task_struct *task)
121{
122 return to_kthread(task)->data;
123}
124
125static void __kthread_parkme(struct kthread *self)
126{
127 __set_current_state(TASK_INTERRUPTIBLE);
128 while (test_bit(KTHREAD_SHOULD_PARK, &self->flags)) {
129 if (!test_and_set_bit(KTHREAD_IS_PARKED, &self->flags))
130 complete(&self->parked);
131 schedule();
132 __set_current_state(TASK_INTERRUPTIBLE);
133 }
134 clear_bit(KTHREAD_IS_PARKED, &self->flags);
135 __set_current_state(TASK_RUNNING);
136}
137
138void kthread_parkme(void)
139{
140 __kthread_parkme(to_kthread(current));
141}
142
143static int kthread(void *_create)
144{
145
146 struct kthread_create_info *create = _create;
147 int (*threadfn)(void *data) = create->threadfn;
148 void *data = create->data;
149 struct kthread self;
150 int ret;
151
152 self.flags = 0;
153 self.data = data;
154 init_completion(&self.exited);
155 init_completion(&self.parked);
156 current->vfork_done = &self.exited;
157
158
159 __set_current_state(TASK_UNINTERRUPTIBLE);
160 create->result = current;
161 complete(&create->done);
162 schedule();
163
164 ret = -EINTR;
165
166 if (!test_bit(KTHREAD_SHOULD_STOP, &self.flags)) {
167 __kthread_parkme(&self);
168 ret = threadfn(data);
169 }
170
171 do_exit(ret);
172}
173
174
175int tsk_fork_get_node(struct task_struct *tsk)
176{
177#ifdef CONFIG_NUMA
178 if (tsk == kthreadd_task)
179 return tsk->pref_node_fork;
180#endif
181 return numa_node_id();
182}
183
184static void create_kthread(struct kthread_create_info *create)
185{
186 int pid;
187
188#ifdef CONFIG_NUMA
189 current->pref_node_fork = create->node;
190#endif
191
192 pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
193 if (pid < 0) {
194 create->result = ERR_PTR(pid);
195 complete(&create->done);
196 }
197}
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
222 void *data, int node,
223 const char namefmt[],
224 ...)
225{
226 struct kthread_create_info create;
227
228 create.threadfn = threadfn;
229 create.data = data;
230 create.node = node;
231 init_completion(&create.done);
232
233 spin_lock(&kthread_create_lock);
234 list_add_tail(&create.list, &kthread_create_list);
235 spin_unlock(&kthread_create_lock);
236
237 wake_up_process(kthreadd_task);
238 wait_for_completion(&create.done);
239
240 if (!IS_ERR(create.result)) {
241 static const struct sched_param param = { .sched_priority = 0 };
242 va_list args;
243
244 va_start(args, namefmt);
245 vsnprintf(create.result->comm, sizeof(create.result->comm),
246 namefmt, args);
247 va_end(args);
248
249
250
251
252 sched_setscheduler_nocheck(create.result, SCHED_NORMAL, ¶m);
253 set_cpus_allowed_ptr(create.result, cpu_all_mask);
254 }
255 return create.result;
256}
257EXPORT_SYMBOL(kthread_create_on_node);
258
259static void __kthread_bind(struct task_struct *p, unsigned int cpu)
260{
261
262 do_set_cpus_allowed(p, cpumask_of(cpu));
263 p->flags |= PF_THREAD_BOUND;
264}
265
266
267
268
269
270
271
272
273
274
275void kthread_bind(struct task_struct *p, unsigned int cpu)
276{
277
278 if (!wait_task_inactive(p, TASK_UNINTERRUPTIBLE)) {
279 WARN_ON(1);
280 return;
281 }
282 __kthread_bind(p, cpu);
283}
284EXPORT_SYMBOL(kthread_bind);
285
286
287
288
289
290
291
292
293
294
295
296
297struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
298 void *data, unsigned int cpu,
299 const char *namefmt)
300{
301 struct task_struct *p;
302
303 p = kthread_create_on_node(threadfn, data, cpu_to_node(cpu), namefmt,
304 cpu);
305 if (IS_ERR(p))
306 return p;
307 set_bit(KTHREAD_IS_PER_CPU, &to_kthread(p)->flags);
308 to_kthread(p)->cpu = cpu;
309
310 kthread_park(p);
311 return p;
312}
313
314static struct kthread *task_get_live_kthread(struct task_struct *k)
315{
316 struct kthread *kthread;
317
318 get_task_struct(k);
319 kthread = to_kthread(k);
320
321 barrier();
322 if (k->vfork_done != NULL)
323 return kthread;
324 return NULL;
325}
326
327
328
329
330
331
332
333
334
335void kthread_unpark(struct task_struct *k)
336{
337 struct kthread *kthread = task_get_live_kthread(k);
338
339 if (kthread) {
340 clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
341
342
343
344
345
346
347 if (test_and_clear_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
348 if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags))
349 __kthread_bind(k, kthread->cpu);
350 wake_up_process(k);
351 }
352 }
353 put_task_struct(k);
354}
355
356
357
358
359
360
361
362
363
364
365
366
367
368int kthread_park(struct task_struct *k)
369{
370 struct kthread *kthread = task_get_live_kthread(k);
371 int ret = -ENOSYS;
372
373 if (kthread) {
374 if (!test_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
375 set_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
376 if (k != current) {
377 wake_up_process(k);
378 wait_for_completion(&kthread->parked);
379 }
380 }
381 ret = 0;
382 }
383 put_task_struct(k);
384 return ret;
385}
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402int kthread_stop(struct task_struct *k)
403{
404 struct kthread *kthread = task_get_live_kthread(k);
405 int ret;
406
407 trace_sched_kthread_stop(k);
408 if (kthread) {
409 set_bit(KTHREAD_SHOULD_STOP, &kthread->flags);
410 clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
411 wake_up_process(k);
412 wait_for_completion(&kthread->exited);
413 }
414 ret = k->exit_code;
415
416 put_task_struct(k);
417 trace_sched_kthread_stop_ret(ret);
418
419 return ret;
420}
421EXPORT_SYMBOL(kthread_stop);
422
423int kthreadd(void *unused)
424{
425 struct task_struct *tsk = current;
426
427
428 set_task_comm(tsk, "kthreadd");
429 ignore_signals(tsk);
430 set_cpus_allowed_ptr(tsk, cpu_all_mask);
431 set_mems_allowed(node_states[N_HIGH_MEMORY]);
432
433 current->flags |= PF_NOFREEZE;
434
435 for (;;) {
436 set_current_state(TASK_INTERRUPTIBLE);
437 if (list_empty(&kthread_create_list))
438 schedule();
439 __set_current_state(TASK_RUNNING);
440
441 spin_lock(&kthread_create_lock);
442 while (!list_empty(&kthread_create_list)) {
443 struct kthread_create_info *create;
444
445 create = list_entry(kthread_create_list.next,
446 struct kthread_create_info, list);
447 list_del_init(&create->list);
448 spin_unlock(&kthread_create_lock);
449
450 create_kthread(create);
451
452 spin_lock(&kthread_create_lock);
453 }
454 spin_unlock(&kthread_create_lock);
455 }
456
457 return 0;
458}
459
460void __init_kthread_worker(struct kthread_worker *worker,
461 const char *name,
462 struct lock_class_key *key)
463{
464 spin_lock_init(&worker->lock);
465 lockdep_set_class_and_name(&worker->lock, key, name);
466 INIT_LIST_HEAD(&worker->work_list);
467 worker->task = NULL;
468}
469EXPORT_SYMBOL_GPL(__init_kthread_worker);
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486int kthread_worker_fn(void *worker_ptr)
487{
488 struct kthread_worker *worker = worker_ptr;
489 struct kthread_work *work;
490
491 WARN_ON(worker->task);
492 worker->task = current;
493repeat:
494 set_current_state(TASK_INTERRUPTIBLE);
495
496 if (kthread_should_stop()) {
497 __set_current_state(TASK_RUNNING);
498 spin_lock_irq(&worker->lock);
499 worker->task = NULL;
500 spin_unlock_irq(&worker->lock);
501 return 0;
502 }
503
504 work = NULL;
505 spin_lock_irq(&worker->lock);
506 if (!list_empty(&worker->work_list)) {
507 work = list_first_entry(&worker->work_list,
508 struct kthread_work, node);
509 list_del_init(&work->node);
510 }
511 worker->current_work = work;
512 spin_unlock_irq(&worker->lock);
513
514 if (work) {
515 __set_current_state(TASK_RUNNING);
516 work->func(work);
517 } else if (!freezing(current))
518 schedule();
519
520 try_to_freeze();
521 goto repeat;
522}
523EXPORT_SYMBOL_GPL(kthread_worker_fn);
524
525
526static void insert_kthread_work(struct kthread_worker *worker,
527 struct kthread_work *work,
528 struct list_head *pos)
529{
530 lockdep_assert_held(&worker->lock);
531
532 list_add_tail(&work->node, pos);
533 work->worker = worker;
534 if (likely(worker->task))
535 wake_up_process(worker->task);
536}
537
538
539
540
541
542
543
544
545
546
547bool queue_kthread_work(struct kthread_worker *worker,
548 struct kthread_work *work)
549{
550 bool ret = false;
551 unsigned long flags;
552
553 spin_lock_irqsave(&worker->lock, flags);
554 if (list_empty(&work->node)) {
555 insert_kthread_work(worker, work, &worker->work_list);
556 ret = true;
557 }
558 spin_unlock_irqrestore(&worker->lock, flags);
559 return ret;
560}
561EXPORT_SYMBOL_GPL(queue_kthread_work);
562
563struct kthread_flush_work {
564 struct kthread_work work;
565 struct completion done;
566};
567
568static void kthread_flush_work_fn(struct kthread_work *work)
569{
570 struct kthread_flush_work *fwork =
571 container_of(work, struct kthread_flush_work, work);
572 complete(&fwork->done);
573}
574
575
576
577
578
579
580
581void flush_kthread_work(struct kthread_work *work)
582{
583 struct kthread_flush_work fwork = {
584 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
585 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
586 };
587 struct kthread_worker *worker;
588 bool noop = false;
589
590retry:
591 worker = work->worker;
592 if (!worker)
593 return;
594
595 spin_lock_irq(&worker->lock);
596 if (work->worker != worker) {
597 spin_unlock_irq(&worker->lock);
598 goto retry;
599 }
600
601 if (!list_empty(&work->node))
602 insert_kthread_work(worker, &fwork.work, work->node.next);
603 else if (worker->current_work == work)
604 insert_kthread_work(worker, &fwork.work, worker->work_list.next);
605 else
606 noop = true;
607
608 spin_unlock_irq(&worker->lock);
609
610 if (!noop)
611 wait_for_completion(&fwork.done);
612}
613EXPORT_SYMBOL_GPL(flush_kthread_work);
614
615
616
617
618
619
620
621
622void flush_kthread_worker(struct kthread_worker *worker)
623{
624 struct kthread_flush_work fwork = {
625 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
626 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
627 };
628
629 queue_kthread_work(worker, &fwork.work);
630 wait_for_completion(&fwork.done);
631}
632EXPORT_SYMBOL_GPL(flush_kthread_worker);
633