freezer: take kernel_execve into consideration
[linux-2.6-block.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>
12#include <linux/unistd.h>
13#include <linux/file.h>
14#include <linux/module.h>
97d1f15b 15#include <linux/mutex.h>
1da177e4
LT
16#include <asm/semaphore.h>
17
73c27992
EB
18static DEFINE_SPINLOCK(kthread_create_lock);
19static LIST_HEAD(kthread_create_list);
20struct task_struct *kthreadd_task;
1da177e4
LT
21
22struct kthread_create_info
23{
73c27992 24 /* Information passed to kthread() from kthreadd. */
1da177e4
LT
25 int (*threadfn)(void *data);
26 void *data;
27 struct completion started;
28
73c27992 29 /* Result passed back to kthread_create() from kthreadd. */
1da177e4
LT
30 struct task_struct *result;
31 struct completion done;
65f27f38 32
73c27992 33 struct list_head list;
1da177e4
LT
34};
35
36struct kthread_stop_info
37{
38 struct task_struct *k;
39 int err;
40 struct completion done;
41};
42
43/* Thread stopping is done by setthing this var: lock serializes
44 * multiple kthread_stop calls. */
97d1f15b 45static DEFINE_MUTEX(kthread_stop_lock);
1da177e4
LT
46static struct kthread_stop_info kthread_stop_info;
47
9e37bd30
RD
48/**
49 * kthread_should_stop - should this kthread return now?
50 *
72fd4a35 51 * When someone calls kthread_stop() on your kthread, it will be woken
9e37bd30
RD
52 * and this will return true. You should then return, and your return
53 * value will be passed through to kthread_stop().
54 */
1da177e4
LT
55int kthread_should_stop(void)
56{
57 return (kthread_stop_info.k == current);
58}
59EXPORT_SYMBOL(kthread_should_stop);
60
1da177e4
LT
61static int kthread(void *_create)
62{
63 struct kthread_create_info *create = _create;
64 int (*threadfn)(void *data);
65 void *data;
1da177e4
LT
66 int ret = -EINTR;
67
73c27992 68 /* Copy data: it's on kthread's stack */
1da177e4
LT
69 threadfn = create->threadfn;
70 data = create->data;
71
1da177e4
LT
72 /* OK, tell user we're spawned, wait for stop or wakeup */
73 __set_current_state(TASK_INTERRUPTIBLE);
74 complete(&create->started);
75 schedule();
76
77 if (!kthread_should_stop())
78 ret = threadfn(data);
79
80 /* It might have exited on its own, w/o kthread_stop. Check. */
81 if (kthread_should_stop()) {
82 kthread_stop_info.err = ret;
83 complete(&kthread_stop_info.done);
84 }
85 return 0;
86}
87
73c27992 88static void create_kthread(struct kthread_create_info *create)
1da177e4 89{
1da177e4
LT
90 int pid;
91
92 /* We want our own signal handler (we take no signals by default). */
93 pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
94 if (pid < 0) {
95 create->result = ERR_PTR(pid);
96 } else {
97 wait_for_completion(&create->started);
05eeae20 98 read_lock(&tasklist_lock);
1da177e4 99 create->result = find_task_by_pid(pid);
05eeae20 100 read_unlock(&tasklist_lock);
1da177e4
LT
101 }
102 complete(&create->done);
103}
104
9e37bd30
RD
105/**
106 * kthread_create - create a kthread.
107 * @threadfn: the function to run until signal_pending(current).
108 * @data: data ptr for @threadfn.
109 * @namefmt: printf-style name for the thread.
110 *
111 * Description: This helper function creates and names a kernel
112 * thread. The thread will be stopped: use wake_up_process() to start
113 * it. See also kthread_run(), kthread_create_on_cpu().
114 *
115 * When woken, the thread will run @threadfn() with @data as its
72fd4a35 116 * argument. @threadfn() can either call do_exit() directly if it is a
9e37bd30
RD
117 * standalone thread for which noone will call kthread_stop(), or
118 * return when 'kthread_should_stop()' is true (which means
119 * kthread_stop() has been called). The return value should be zero
120 * or a negative error number; it will be passed to kthread_stop().
121 *
122 * Returns a task_struct or ERR_PTR(-ENOMEM).
123 */
1da177e4
LT
124struct task_struct *kthread_create(int (*threadfn)(void *data),
125 void *data,
126 const char namefmt[],
127 ...)
128{
129 struct kthread_create_info create;
1da177e4
LT
130
131 create.threadfn = threadfn;
132 create.data = data;
133 init_completion(&create.started);
134 init_completion(&create.done);
73c27992
EB
135
136 spin_lock(&kthread_create_lock);
137 list_add_tail(&create.list, &kthread_create_list);
138 wake_up_process(kthreadd_task);
139 spin_unlock(&kthread_create_lock);
140
141 wait_for_completion(&create.done);
142
1da177e4
LT
143 if (!IS_ERR(create.result)) {
144 va_list args;
145 va_start(args, namefmt);
146 vsnprintf(create.result->comm, sizeof(create.result->comm),
147 namefmt, args);
148 va_end(args);
149 }
1da177e4
LT
150 return create.result;
151}
152EXPORT_SYMBOL(kthread_create);
153
9e37bd30
RD
154/**
155 * kthread_bind - bind a just-created kthread to a cpu.
156 * @k: thread created by kthread_create().
157 * @cpu: cpu (might not be online, must be possible) for @k to run on.
158 *
159 * Description: This function is equivalent to set_cpus_allowed(),
160 * except that @cpu doesn't need to be online, and the thread must be
72fd4a35 161 * stopped (i.e., just returned from kthread_create()).
9e37bd30 162 */
1da177e4
LT
163void kthread_bind(struct task_struct *k, unsigned int cpu)
164{
165 BUG_ON(k->state != TASK_INTERRUPTIBLE);
166 /* Must have done schedule() in kthread() before we set_task_cpu */
167 wait_task_inactive(k);
168 set_task_cpu(k, cpu);
169 k->cpus_allowed = cpumask_of_cpu(cpu);
170}
171EXPORT_SYMBOL(kthread_bind);
172
9e37bd30
RD
173/**
174 * kthread_stop - stop a thread created by kthread_create().
175 * @k: thread created by kthread_create().
176 *
177 * Sets kthread_should_stop() for @k to return true, wakes it, and
178 * waits for it to exit. Your threadfn() must not call do_exit()
179 * itself if you use this function! This can also be called after
180 * kthread_create() instead of calling wake_up_process(): the thread
181 * will exit without calling threadfn().
182 *
183 * Returns the result of threadfn(), or %-EINTR if wake_up_process()
184 * was never called.
185 */
1da177e4
LT
186int kthread_stop(struct task_struct *k)
187{
188 int ret;
189
97d1f15b 190 mutex_lock(&kthread_stop_lock);
1da177e4
LT
191
192 /* It could exit after stop_info.k set, but before wake_up_process. */
193 get_task_struct(k);
194
195 /* Must init completion *before* thread sees kthread_stop_info.k */
196 init_completion(&kthread_stop_info.done);
d59dd462 197 smp_wmb();
1da177e4
LT
198
199 /* Now set kthread_should_stop() to true, and wake it up. */
200 kthread_stop_info.k = k;
52e92e57 201 wake_up_process(k);
1da177e4
LT
202 put_task_struct(k);
203
204 /* Once it dies, reset stop ptr, gather result and we're done. */
205 wait_for_completion(&kthread_stop_info.done);
206 kthread_stop_info.k = NULL;
207 ret = kthread_stop_info.err;
97d1f15b 208 mutex_unlock(&kthread_stop_lock);
1da177e4
LT
209
210 return ret;
211}
52e92e57 212EXPORT_SYMBOL(kthread_stop);
1da177e4 213
73c27992
EB
214
215static __init void kthreadd_setup(void)
1da177e4 216{
73c27992 217 struct task_struct *tsk = current;
1da177e4 218
73c27992
EB
219 set_task_comm(tsk, "kthreadd");
220
10ab825b 221 ignore_signals(tsk);
73c27992 222
10ab825b
ON
223 set_user_nice(tsk, -5);
224 set_cpus_allowed(tsk, CPU_MASK_ALL);
1da177e4 225}
1da177e4 226
73c27992
EB
227int kthreadd(void *unused)
228{
229 /* Setup a clean context for our children to inherit. */
230 kthreadd_setup();
231
232 current->flags |= PF_NOFREEZE;
233
234 for (;;) {
235 set_current_state(TASK_INTERRUPTIBLE);
236 if (list_empty(&kthread_create_list))
237 schedule();
238 __set_current_state(TASK_RUNNING);
239
240 spin_lock(&kthread_create_lock);
241 while (!list_empty(&kthread_create_list)) {
242 struct kthread_create_info *create;
243
244 create = list_entry(kthread_create_list.next,
245 struct kthread_create_info, list);
246 list_del_init(&create->list);
247 spin_unlock(&kthread_create_lock);
248
249 create_kthread(create);
250
251 spin_lock(&kthread_create_lock);
252 }
253 spin_unlock(&kthread_create_lock);
254 }
255
256 return 0;
257}