1 /* Kernel thread helper functions.
2 * Copyright (C) 2004 IBM Corporation, Rusty Russell.
4 * Creation is done via keventd, so that we get a clean environment
5 * even if we're invoked from userspace (think modprobe, hotplug cpu,
8 #include <linux/sched.h>
9 #include <linux/kthread.h>
10 #include <linux/completion.h>
11 #include <linux/err.h>
12 #include <linux/unistd.h>
13 #include <linux/file.h>
14 #include <linux/module.h>
15 #include <asm/semaphore.h>
17 struct kthread_create_info
19 /* Information passed to kthread() from keventd. */
20 int (*threadfn)(void *data);
22 struct completion started;
24 /* Result passed back to kthread_create() from keventd. */
25 struct task_struct *result;
26 struct completion done;
29 struct kthread_stop_info
31 struct task_struct *k;
33 struct completion done;
36 /* Thread stopping is done by setthing this var: lock serializes
37 * multiple kthread_stop calls. */
38 static DECLARE_MUTEX(kthread_stop_lock);
39 static struct kthread_stop_info kthread_stop_info;
41 int kthread_should_stop(void)
43 return (kthread_stop_info.k == current);
45 EXPORT_SYMBOL(kthread_should_stop);
47 static void kthread_exit_files(void)
50 struct task_struct *tsk = current;
52 exit_fs(tsk); /* current->fs->count--; */
55 atomic_inc(&fs->count);
57 current->files = init_task.files;
58 atomic_inc(&tsk->files->count);
61 static int kthread(void *_create)
63 struct kthread_create_info *create = _create;
64 int (*threadfn)(void *data);
68 cpumask_t mask = CPU_MASK_ALL;
72 /* Copy data: it's on keventd's stack */
73 threadfn = create->threadfn;
76 /* Block and flush all signals (in case we're not from keventd). */
78 sigprocmask(SIG_BLOCK, &blocked, NULL);
79 flush_signals(current);
81 /* By default we can run anywhere, unlike keventd. */
82 set_cpus_allowed(current, mask);
84 /* OK, tell user we're spawned, wait for stop or wakeup */
85 __set_current_state(TASK_INTERRUPTIBLE);
86 complete(&create->started);
89 if (!kthread_should_stop())
92 /* It might have exited on its own, w/o kthread_stop. Check. */
93 if (kthread_should_stop()) {
94 kthread_stop_info.err = ret;
95 complete(&kthread_stop_info.done);
100 /* We are keventd: create a thread. */
101 static void keventd_create_kthread(void *_create)
103 struct kthread_create_info *create = _create;
106 /* We want our own signal handler (we take no signals by default). */
107 pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
109 create->result = ERR_PTR(pid);
111 wait_for_completion(&create->started);
112 create->result = find_task_by_pid(pid);
114 complete(&create->done);
117 struct task_struct *kthread_create(int (*threadfn)(void *data),
119 const char namefmt[],
122 struct kthread_create_info create;
123 DECLARE_WORK(work, keventd_create_kthread, &create);
125 create.threadfn = threadfn;
127 init_completion(&create.started);
128 init_completion(&create.done);
130 /* If we're being called to start the first workqueue, we
131 * can't use keventd. */
133 work.func(work.data);
135 schedule_work(&work);
136 wait_for_completion(&create.done);
138 if (!IS_ERR(create.result)) {
140 va_start(args, namefmt);
141 vsnprintf(create.result->comm, sizeof(create.result->comm),
146 return create.result;
148 EXPORT_SYMBOL(kthread_create);
150 void kthread_bind(struct task_struct *k, unsigned int cpu)
152 BUG_ON(k->state != TASK_INTERRUPTIBLE);
153 /* Must have done schedule() in kthread() before we set_task_cpu */
154 wait_task_inactive(k);
155 set_task_cpu(k, cpu);
156 k->cpus_allowed = cpumask_of_cpu(cpu);
158 EXPORT_SYMBOL(kthread_bind);
160 int kthread_stop(struct task_struct *k)
164 down(&kthread_stop_lock);
166 /* It could exit after stop_info.k set, but before wake_up_process. */
169 /* Must init completion *before* thread sees kthread_stop_info.k */
170 init_completion(&kthread_stop_info.done);
173 /* Now set kthread_should_stop() to true, and wake it up. */
174 kthread_stop_info.k = k;
178 /* Once it dies, reset stop ptr, gather result and we're done. */
179 wait_for_completion(&kthread_stop_info.done);
180 kthread_stop_info.k = NULL;
181 ret = kthread_stop_info.err;
182 up(&kthread_stop_lock);
186 EXPORT_SYMBOL(kthread_stop);