arc, print-fatal-signals: reduce duplicated information
[deliverable/linux.git] / kernel / kthread.c
CommitLineData
1da177e4
LT
1/* Kernel thread helper functions.
2 * Copyright (C) 2004 IBM Corporation, Rusty Russell.
3 *
73c27992 4 * Creation is done via kthreadd, so that we get a clean environment
1da177e4
LT
5 * even if we're invoked from userspace (think modprobe, hotplug cpu,
6 * etc.).
7 */
8#include <linux/sched.h>
9#include <linux/kthread.h>
10#include <linux/completion.h>
11#include <linux/err.h>
58568d2a 12#include <linux/cpuset.h>
1da177e4
LT
13#include <linux/unistd.h>
14#include <linux/file.h>
9984de1a 15#include <linux/export.h>
97d1f15b 16#include <linux/mutex.h>
b56c0d89
TH
17#include <linux/slab.h>
18#include <linux/freezer.h>
a74fb73c 19#include <linux/ptrace.h>
ad8d75ff 20#include <trace/events/sched.h>
1da177e4 21
73c27992
EB
22static DEFINE_SPINLOCK(kthread_create_lock);
23static LIST_HEAD(kthread_create_list);
24struct task_struct *kthreadd_task;
1da177e4
LT
25
26struct kthread_create_info
27{
73c27992 28 /* Information passed to kthread() from kthreadd. */
1da177e4
LT
29 int (*threadfn)(void *data);
30 void *data;
207205a2 31 int node;
1da177e4 32
73c27992 33 /* Result passed back to kthread_create() from kthreadd. */
1da177e4
LT
34 struct task_struct *result;
35 struct completion done;
65f27f38 36
73c27992 37 struct list_head list;
1da177e4
LT
38};
39
63706172 40struct kthread {
2a1d4460
TG
41 unsigned long flags;
42 unsigned int cpu;
82805ab7 43 void *data;
2a1d4460 44 struct completion parked;
63706172 45 struct completion exited;
1da177e4
LT
46};
47
2a1d4460
TG
48enum KTHREAD_BITS {
49 KTHREAD_IS_PER_CPU = 0,
50 KTHREAD_SHOULD_STOP,
51 KTHREAD_SHOULD_PARK,
52 KTHREAD_IS_PARKED,
53};
54
4ecdafc8
ON
55#define __to_kthread(vfork) \
56 container_of(vfork, struct kthread, exited)
57
58static inline struct kthread *to_kthread(struct task_struct *k)
59{
60 return __to_kthread(k->vfork_done);
61}
62
63static struct kthread *to_live_kthread(struct task_struct *k)
64{
65 struct completion *vfork = ACCESS_ONCE(k->vfork_done);
66 if (likely(vfork))
67 return __to_kthread(vfork);
68 return NULL;
69}
1da177e4 70
9e37bd30
RD
71/**
72 * kthread_should_stop - should this kthread return now?
73 *
72fd4a35 74 * When someone calls kthread_stop() on your kthread, it will be woken
9e37bd30
RD
75 * and this will return true. You should then return, and your return
76 * value will be passed through to kthread_stop().
77 */
2a1d4460 78bool kthread_should_stop(void)
1da177e4 79{
2a1d4460 80 return test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags);
1da177e4
LT
81}
82EXPORT_SYMBOL(kthread_should_stop);
83
2a1d4460
TG
84/**
85 * kthread_should_park - should this kthread park now?
86 *
87 * When someone calls kthread_park() on your kthread, it will be woken
88 * and this will return true. You should then do the necessary
89 * cleanup and call kthread_parkme()
90 *
91 * Similar to kthread_should_stop(), but this keeps the thread alive
92 * and in a park position. kthread_unpark() "restarts" the thread and
93 * calls the thread function again.
94 */
95bool kthread_should_park(void)
96{
97 return test_bit(KTHREAD_SHOULD_PARK, &to_kthread(current)->flags);
98}
99
8a32c441
TH
100/**
101 * kthread_freezable_should_stop - should this freezable kthread return now?
102 * @was_frozen: optional out parameter, indicates whether %current was frozen
103 *
104 * kthread_should_stop() for freezable kthreads, which will enter
105 * refrigerator if necessary. This function is safe from kthread_stop() /
106 * freezer deadlock and freezable kthreads should use this function instead
107 * of calling try_to_freeze() directly.
108 */
109bool kthread_freezable_should_stop(bool *was_frozen)
110{
111 bool frozen = false;
112
113 might_sleep();
114
115 if (unlikely(freezing(current)))
116 frozen = __refrigerator(true);
117
118 if (was_frozen)
119 *was_frozen = frozen;
120
121 return kthread_should_stop();
122}
123EXPORT_SYMBOL_GPL(kthread_freezable_should_stop);
124
82805ab7
TH
125/**
126 * kthread_data - return data value specified on kthread creation
127 * @task: kthread task in question
128 *
129 * Return the data value specified when kthread @task was created.
130 * The caller is responsible for ensuring the validity of @task when
131 * calling this function.
132 */
133void *kthread_data(struct task_struct *task)
134{
135 return to_kthread(task)->data;
136}
137
2a1d4460
TG
138static void __kthread_parkme(struct kthread *self)
139{
f2530dc7 140 __set_current_state(TASK_PARKED);
2a1d4460
TG
141 while (test_bit(KTHREAD_SHOULD_PARK, &self->flags)) {
142 if (!test_and_set_bit(KTHREAD_IS_PARKED, &self->flags))
143 complete(&self->parked);
144 schedule();
f2530dc7 145 __set_current_state(TASK_PARKED);
2a1d4460
TG
146 }
147 clear_bit(KTHREAD_IS_PARKED, &self->flags);
148 __set_current_state(TASK_RUNNING);
149}
150
151void kthread_parkme(void)
152{
153 __kthread_parkme(to_kthread(current));
154}
155
1da177e4
LT
156static int kthread(void *_create)
157{
63706172 158 /* Copy data: it's on kthread's stack */
1da177e4 159 struct kthread_create_info *create = _create;
63706172
ON
160 int (*threadfn)(void *data) = create->threadfn;
161 void *data = create->data;
162 struct kthread self;
163 int ret;
1da177e4 164
2a1d4460 165 self.flags = 0;
82805ab7 166 self.data = data;
63706172 167 init_completion(&self.exited);
2a1d4460 168 init_completion(&self.parked);
63706172 169 current->vfork_done = &self.exited;
1da177e4 170
1da177e4 171 /* OK, tell user we're spawned, wait for stop or wakeup */
a076e4bc 172 __set_current_state(TASK_UNINTERRUPTIBLE);
3217ab97 173 create->result = current;
cdd140bd 174 complete(&create->done);
1da177e4
LT
175 schedule();
176
63706172 177 ret = -EINTR;
1da177e4 178
2a1d4460
TG
179 if (!test_bit(KTHREAD_SHOULD_STOP, &self.flags)) {
180 __kthread_parkme(&self);
181 ret = threadfn(data);
182 }
63706172
ON
183 /* we can't just return, we must preserve "self" on stack */
184 do_exit(ret);
1da177e4
LT
185}
186
207205a2
ED
187/* called from do_fork() to get node information for about to be created task */
188int tsk_fork_get_node(struct task_struct *tsk)
189{
190#ifdef CONFIG_NUMA
191 if (tsk == kthreadd_task)
192 return tsk->pref_node_fork;
193#endif
194 return numa_node_id();
195}
196
73c27992 197static void create_kthread(struct kthread_create_info *create)
1da177e4 198{
1da177e4
LT
199 int pid;
200
207205a2
ED
201#ifdef CONFIG_NUMA
202 current->pref_node_fork = create->node;
203#endif
1da177e4
LT
204 /* We want our own signal handler (we take no signals by default). */
205 pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
cdd140bd 206 if (pid < 0) {
1da177e4 207 create->result = ERR_PTR(pid);
cdd140bd
ON
208 complete(&create->done);
209 }
1da177e4
LT
210}
211
9e37bd30 212/**
207205a2 213 * kthread_create_on_node - create a kthread.
9e37bd30
RD
214 * @threadfn: the function to run until signal_pending(current).
215 * @data: data ptr for @threadfn.
207205a2 216 * @node: memory node number.
9e37bd30
RD
217 * @namefmt: printf-style name for the thread.
218 *
219 * Description: This helper function creates and names a kernel
220 * thread. The thread will be stopped: use wake_up_process() to start
301ba045 221 * it. See also kthread_run().
9e37bd30 222 *
207205a2
ED
223 * If thread is going to be bound on a particular cpu, give its node
224 * in @node, to get NUMA affinity for kthread stack, or else give -1.
9e37bd30 225 * When woken, the thread will run @threadfn() with @data as its
72fd4a35 226 * argument. @threadfn() can either call do_exit() directly if it is a
25985edc 227 * standalone thread for which no one will call kthread_stop(), or
9e37bd30
RD
228 * return when 'kthread_should_stop()' is true (which means
229 * kthread_stop() has been called). The return value should be zero
230 * or a negative error number; it will be passed to kthread_stop().
231 *
232 * Returns a task_struct or ERR_PTR(-ENOMEM).
233 */
207205a2 234struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
2a1d4460 235 void *data, int node,
207205a2
ED
236 const char namefmt[],
237 ...)
1da177e4
LT
238{
239 struct kthread_create_info create;
1da177e4
LT
240
241 create.threadfn = threadfn;
242 create.data = data;
207205a2 243 create.node = node;
1da177e4 244 init_completion(&create.done);
73c27992
EB
245
246 spin_lock(&kthread_create_lock);
247 list_add_tail(&create.list, &kthread_create_list);
73c27992
EB
248 spin_unlock(&kthread_create_lock);
249
cbd9b67b 250 wake_up_process(kthreadd_task);
73c27992
EB
251 wait_for_completion(&create.done);
252
1da177e4 253 if (!IS_ERR(create.result)) {
c9b5f501 254 static const struct sched_param param = { .sched_priority = 0 };
1da177e4 255 va_list args;
1c99315b 256
1da177e4
LT
257 va_start(args, namefmt);
258 vsnprintf(create.result->comm, sizeof(create.result->comm),
259 namefmt, args);
260 va_end(args);
1c99315b
ON
261 /*
262 * root may have changed our (kthreadd's) priority or CPU mask.
263 * The kernel thread should not inherit these properties.
264 */
265 sched_setscheduler_nocheck(create.result, SCHED_NORMAL, &param);
1c99315b 266 set_cpus_allowed_ptr(create.result, cpu_all_mask);
1da177e4 267 }
1da177e4
LT
268 return create.result;
269}
207205a2 270EXPORT_SYMBOL(kthread_create_on_node);
1da177e4 271
f2530dc7 272static void __kthread_bind(struct task_struct *p, unsigned int cpu, long state)
2a1d4460 273{
f2530dc7
TG
274 /* Must have done schedule() in kthread() before we set_task_cpu */
275 if (!wait_task_inactive(p, state)) {
276 WARN_ON(1);
277 return;
278 }
2a1d4460
TG
279 /* It's safe because the task is inactive. */
280 do_set_cpus_allowed(p, cpumask_of(cpu));
14a40ffc 281 p->flags |= PF_NO_SETAFFINITY;
2a1d4460
TG
282}
283
881232b7
PZ
284/**
285 * kthread_bind - bind a just-created kthread to a cpu.
286 * @p: thread created by kthread_create().
287 * @cpu: cpu (might not be online, must be possible) for @k to run on.
288 *
289 * Description: This function is equivalent to set_cpus_allowed(),
290 * except that @cpu doesn't need to be online, and the thread must be
291 * stopped (i.e., just returned from kthread_create()).
292 */
293void kthread_bind(struct task_struct *p, unsigned int cpu)
294{
f2530dc7 295 __kthread_bind(p, cpu, TASK_UNINTERRUPTIBLE);
881232b7
PZ
296}
297EXPORT_SYMBOL(kthread_bind);
298
2a1d4460
TG
299/**
300 * kthread_create_on_cpu - Create a cpu bound kthread
301 * @threadfn: the function to run until signal_pending(current).
302 * @data: data ptr for @threadfn.
303 * @cpu: The cpu on which the thread should be bound,
304 * @namefmt: printf-style name for the thread. Format is restricted
305 * to "name.*%u". Code fills in cpu number.
306 *
307 * Description: This helper function creates and names a kernel thread
308 * The thread will be woken and put into park mode.
309 */
310struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
311 void *data, unsigned int cpu,
312 const char *namefmt)
313{
314 struct task_struct *p;
315
316 p = kthread_create_on_node(threadfn, data, cpu_to_node(cpu), namefmt,
317 cpu);
318 if (IS_ERR(p))
319 return p;
320 set_bit(KTHREAD_IS_PER_CPU, &to_kthread(p)->flags);
321 to_kthread(p)->cpu = cpu;
322 /* Park the thread to get it out of TASK_UNINTERRUPTIBLE state */
323 kthread_park(p);
324 return p;
325}
326
f2530dc7
TG
327static void __kthread_unpark(struct task_struct *k, struct kthread *kthread)
328{
329 clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
330 /*
331 * We clear the IS_PARKED bit here as we don't wait
332 * until the task has left the park code. So if we'd
333 * park before that happens we'd see the IS_PARKED bit
334 * which might be about to be cleared.
335 */
336 if (test_and_clear_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
337 if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags))
338 __kthread_bind(k, kthread->cpu, TASK_PARKED);
339 wake_up_state(k, TASK_PARKED);
340 }
341}
342
2a1d4460
TG
343/**
344 * kthread_unpark - unpark a thread created by kthread_create().
345 * @k: thread created by kthread_create().
346 *
347 * Sets kthread_should_park() for @k to return false, wakes it, and
348 * waits for it to return. If the thread is marked percpu then its
349 * bound to the cpu again.
350 */
351void kthread_unpark(struct task_struct *k)
352{
b5c5442b 353 struct kthread *kthread = to_live_kthread(k);
2a1d4460 354
f2530dc7
TG
355 if (kthread)
356 __kthread_unpark(k, kthread);
2a1d4460
TG
357}
358
359/**
360 * kthread_park - park a thread created by kthread_create().
361 * @k: thread created by kthread_create().
362 *
363 * Sets kthread_should_park() for @k to return true, wakes it, and
364 * waits for it to return. This can also be called after kthread_create()
365 * instead of calling wake_up_process(): the thread will park without
366 * calling threadfn().
367 *
368 * Returns 0 if the thread is parked, -ENOSYS if the thread exited.
369 * If called by the kthread itself just the park bit is set.
370 */
371int kthread_park(struct task_struct *k)
372{
b5c5442b 373 struct kthread *kthread = to_live_kthread(k);
2a1d4460
TG
374 int ret = -ENOSYS;
375
376 if (kthread) {
377 if (!test_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
378 set_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
379 if (k != current) {
380 wake_up_process(k);
381 wait_for_completion(&kthread->parked);
382 }
383 }
384 ret = 0;
385 }
2a1d4460
TG
386 return ret;
387}
388
9e37bd30
RD
389/**
390 * kthread_stop - stop a thread created by kthread_create().
391 * @k: thread created by kthread_create().
392 *
393 * Sets kthread_should_stop() for @k to return true, wakes it, and
9ae26027
ON
394 * waits for it to exit. This can also be called after kthread_create()
395 * instead of calling wake_up_process(): the thread will exit without
396 * calling threadfn().
397 *
398 * If threadfn() may call do_exit() itself, the caller must ensure
399 * task_struct can't go away.
9e37bd30
RD
400 *
401 * Returns the result of threadfn(), or %-EINTR if wake_up_process()
402 * was never called.
403 */
1da177e4
LT
404int kthread_stop(struct task_struct *k)
405{
b5c5442b 406 struct kthread *kthread;
1da177e4
LT
407 int ret;
408
0a16b607 409 trace_sched_kthread_stop(k);
b5c5442b
ON
410
411 get_task_struct(k);
412 kthread = to_live_kthread(k);
2a1d4460
TG
413 if (kthread) {
414 set_bit(KTHREAD_SHOULD_STOP, &kthread->flags);
f2530dc7 415 __kthread_unpark(k, kthread);
63706172
ON
416 wake_up_process(k);
417 wait_for_completion(&kthread->exited);
418 }
419 ret = k->exit_code;
1da177e4 420 put_task_struct(k);
0a16b607 421
b5c5442b 422 trace_sched_kthread_stop_ret(ret);
1da177e4
LT
423 return ret;
424}
52e92e57 425EXPORT_SYMBOL(kthread_stop);
1da177e4 426
e804a4a4 427int kthreadd(void *unused)
1da177e4 428{
73c27992 429 struct task_struct *tsk = current;
1da177e4 430
e804a4a4 431 /* Setup a clean context for our children to inherit. */
73c27992 432 set_task_comm(tsk, "kthreadd");
10ab825b 433 ignore_signals(tsk);
1a2142af 434 set_cpus_allowed_ptr(tsk, cpu_all_mask);
aee4faa4 435 set_mems_allowed(node_states[N_MEMORY]);
73c27992 436
34b087e4 437 current->flags |= PF_NOFREEZE;
73c27992
EB
438
439 for (;;) {
440 set_current_state(TASK_INTERRUPTIBLE);
441 if (list_empty(&kthread_create_list))
442 schedule();
443 __set_current_state(TASK_RUNNING);
444
445 spin_lock(&kthread_create_lock);
446 while (!list_empty(&kthread_create_list)) {
447 struct kthread_create_info *create;
448
449 create = list_entry(kthread_create_list.next,
450 struct kthread_create_info, list);
451 list_del_init(&create->list);
452 spin_unlock(&kthread_create_lock);
453
454 create_kthread(create);
455
456 spin_lock(&kthread_create_lock);
457 }
458 spin_unlock(&kthread_create_lock);
459 }
460
461 return 0;
462}
b56c0d89 463
4f32e9b1
YZ
464void __init_kthread_worker(struct kthread_worker *worker,
465 const char *name,
466 struct lock_class_key *key)
467{
468 spin_lock_init(&worker->lock);
469 lockdep_set_class_and_name(&worker->lock, key, name);
470 INIT_LIST_HEAD(&worker->work_list);
471 worker->task = NULL;
472}
473EXPORT_SYMBOL_GPL(__init_kthread_worker);
474
b56c0d89
TH
475/**
476 * kthread_worker_fn - kthread function to process kthread_worker
477 * @worker_ptr: pointer to initialized kthread_worker
478 *
479 * This function can be used as @threadfn to kthread_create() or
480 * kthread_run() with @worker_ptr argument pointing to an initialized
481 * kthread_worker. The started kthread will process work_list until
482 * the it is stopped with kthread_stop(). A kthread can also call
483 * this function directly after extra initialization.
484 *
485 * Different kthreads can be used for the same kthread_worker as long
486 * as there's only one kthread attached to it at any given time. A
487 * kthread_worker without an attached kthread simply collects queued
488 * kthread_works.
489 */
490int kthread_worker_fn(void *worker_ptr)
491{
492 struct kthread_worker *worker = worker_ptr;
493 struct kthread_work *work;
494
495 WARN_ON(worker->task);
496 worker->task = current;
497repeat:
498 set_current_state(TASK_INTERRUPTIBLE); /* mb paired w/ kthread_stop */
499
500 if (kthread_should_stop()) {
501 __set_current_state(TASK_RUNNING);
502 spin_lock_irq(&worker->lock);
503 worker->task = NULL;
504 spin_unlock_irq(&worker->lock);
505 return 0;
506 }
507
508 work = NULL;
509 spin_lock_irq(&worker->lock);
510 if (!list_empty(&worker->work_list)) {
511 work = list_first_entry(&worker->work_list,
512 struct kthread_work, node);
513 list_del_init(&work->node);
514 }
46f3d976 515 worker->current_work = work;
b56c0d89
TH
516 spin_unlock_irq(&worker->lock);
517
518 if (work) {
519 __set_current_state(TASK_RUNNING);
520 work->func(work);
b56c0d89
TH
521 } else if (!freezing(current))
522 schedule();
523
524 try_to_freeze();
525 goto repeat;
526}
527EXPORT_SYMBOL_GPL(kthread_worker_fn);
528
9a2e03d8
TH
529/* insert @work before @pos in @worker */
530static void insert_kthread_work(struct kthread_worker *worker,
531 struct kthread_work *work,
532 struct list_head *pos)
533{
534 lockdep_assert_held(&worker->lock);
535
536 list_add_tail(&work->node, pos);
46f3d976 537 work->worker = worker;
9a2e03d8
TH
538 if (likely(worker->task))
539 wake_up_process(worker->task);
540}
541
b56c0d89
TH
542/**
543 * queue_kthread_work - queue a kthread_work
544 * @worker: target kthread_worker
545 * @work: kthread_work to queue
546 *
547 * Queue @work to work processor @task for async execution. @task
548 * must have been created with kthread_worker_create(). Returns %true
549 * if @work was successfully queued, %false if it was already pending.
550 */
551bool queue_kthread_work(struct kthread_worker *worker,
552 struct kthread_work *work)
553{
554 bool ret = false;
555 unsigned long flags;
556
557 spin_lock_irqsave(&worker->lock, flags);
558 if (list_empty(&work->node)) {
9a2e03d8 559 insert_kthread_work(worker, work, &worker->work_list);
b56c0d89
TH
560 ret = true;
561 }
562 spin_unlock_irqrestore(&worker->lock, flags);
563 return ret;
564}
565EXPORT_SYMBOL_GPL(queue_kthread_work);
566
9a2e03d8
TH
567struct kthread_flush_work {
568 struct kthread_work work;
569 struct completion done;
570};
571
572static void kthread_flush_work_fn(struct kthread_work *work)
573{
574 struct kthread_flush_work *fwork =
575 container_of(work, struct kthread_flush_work, work);
576 complete(&fwork->done);
577}
578
b56c0d89
TH
579/**
580 * flush_kthread_work - flush a kthread_work
581 * @work: work to flush
582 *
583 * If @work is queued or executing, wait for it to finish execution.
584 */
585void flush_kthread_work(struct kthread_work *work)
586{
46f3d976
TH
587 struct kthread_flush_work fwork = {
588 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
589 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
590 };
591 struct kthread_worker *worker;
592 bool noop = false;
593
594retry:
595 worker = work->worker;
596 if (!worker)
597 return;
b56c0d89 598
46f3d976
TH
599 spin_lock_irq(&worker->lock);
600 if (work->worker != worker) {
601 spin_unlock_irq(&worker->lock);
602 goto retry;
603 }
b56c0d89 604
46f3d976
TH
605 if (!list_empty(&work->node))
606 insert_kthread_work(worker, &fwork.work, work->node.next);
607 else if (worker->current_work == work)
608 insert_kthread_work(worker, &fwork.work, worker->work_list.next);
609 else
610 noop = true;
b56c0d89 611
46f3d976 612 spin_unlock_irq(&worker->lock);
b56c0d89 613
46f3d976
TH
614 if (!noop)
615 wait_for_completion(&fwork.done);
b56c0d89
TH
616}
617EXPORT_SYMBOL_GPL(flush_kthread_work);
618
b56c0d89
TH
619/**
620 * flush_kthread_worker - flush all current works on a kthread_worker
621 * @worker: worker to flush
622 *
623 * Wait until all currently executing or pending works on @worker are
624 * finished.
625 */
626void flush_kthread_worker(struct kthread_worker *worker)
627{
628 struct kthread_flush_work fwork = {
629 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
630 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
631 };
632
633 queue_kthread_work(worker, &fwork.work);
634 wait_for_completion(&fwork.done);
635}
636EXPORT_SYMBOL_GPL(flush_kthread_worker);
This page took 0.645948 seconds and 5 git commands to generate.