ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / kernel / kthread.c
1 /* Kernel thread helper functions.
2  *   Copyright (C) 2004 IBM Corporation, Rusty Russell.
3  *
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,
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 <asm/semaphore.h>
15
16 struct kthread_create_info
17 {
18         /* Information passed to kthread() from keventd. */
19         int (*threadfn)(void *data);
20         void *data;
21         struct completion started;
22
23         /* Result passed back to kthread_create() from keventd. */
24         struct task_struct *result;
25         struct completion done;
26 };
27
28 struct kthread_stop_info
29 {
30         struct task_struct *k;
31         int err;
32         struct completion done;
33 };
34
35 /* Thread stopping is done by setthing this var: lock serializes
36  * multiple kthread_stop calls. */
37 static DECLARE_MUTEX(kthread_stop_lock);
38 static struct kthread_stop_info kthread_stop_info;
39
40 int kthread_should_stop(void)
41 {
42         return (kthread_stop_info.k == current);
43 }
44
45
46 static void kthread_exit_files(void)
47 {
48         struct fs_struct *fs;
49         struct task_struct *tsk = current;
50
51         exit_fs(tsk);           /* current->fs->count--; */
52         fs = init_task.fs;
53         tsk->fs = fs;
54         atomic_inc(&fs->count);
55         exit_files(tsk);
56         current->files = init_task.files;
57         atomic_inc(&tsk->files->count);
58 }
59
60 static int kthread(void *_create)
61 {
62         struct kthread_create_info *create = _create;
63         int (*threadfn)(void *data);
64         void *data;
65         sigset_t blocked;
66         int ret = -EINTR;
67         cpumask_t mask = CPU_MASK_ALL;
68
69         kthread_exit_files();
70
71         /* Copy data: it's on keventd's stack */
72         threadfn = create->threadfn;
73         data = create->data;
74
75         /* Block and flush all signals (in case we're not from keventd). */
76         sigfillset(&blocked);
77         sigprocmask(SIG_BLOCK, &blocked, NULL);
78         flush_signals(current);
79
80         /* By default we can run anywhere, unlike keventd. */
81         set_cpus_allowed(current, mask);
82
83         /* OK, tell user we're spawned, wait for stop or wakeup */
84         __set_current_state(TASK_INTERRUPTIBLE);
85         complete(&create->started);
86         schedule();
87
88         if (!kthread_should_stop())
89                 ret = threadfn(data);
90
91         /* It might have exited on its own, w/o kthread_stop.  Check. */
92         if (kthread_should_stop()) {
93                 kthread_stop_info.err = ret;
94                 complete(&kthread_stop_info.done);
95         }
96         return 0;
97 }
98
99 /* We are keventd: create a thread. */
100 static void keventd_create_kthread(void *_create)
101 {
102         struct kthread_create_info *create = _create;
103         int pid;
104
105         /* We want our own signal handler (we take no signals by default). */
106         pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
107         if (pid < 0) {
108                 create->result = ERR_PTR(pid);
109         } else {
110                 wait_for_completion(&create->started);
111                 create->result = find_task_by_pid(pid);
112         }
113         complete(&create->done);
114 }
115
116 struct task_struct *kthread_create(int (*threadfn)(void *data),
117                                    void *data,
118                                    const char namefmt[],
119                                    ...)
120 {
121         struct kthread_create_info create;
122         DECLARE_WORK(work, keventd_create_kthread, &create);
123
124         create.threadfn = threadfn;
125         create.data = data;
126         init_completion(&create.started);
127         init_completion(&create.done);
128
129         /* If we're being called to start the first workqueue, we
130          * can't use keventd. */
131         if (!keventd_up())
132                 work.func(work.data);
133         else {
134                 schedule_work(&work);
135                 wait_for_completion(&create.done);
136         }
137         if (!IS_ERR(create.result)) {
138                 va_list args;
139                 va_start(args, namefmt);
140                 vsnprintf(create.result->comm, sizeof(create.result->comm),
141                           namefmt, args);
142                 va_end(args);
143         }
144
145         return create.result;
146 }
147
148 void kthread_bind(struct task_struct *k, unsigned int cpu)
149 {
150         BUG_ON(k->state != TASK_INTERRUPTIBLE);
151         /* Must have done schedule() in kthread() before we set_task_cpu */
152         wait_task_inactive(k);
153         set_task_cpu(k, cpu);
154         k->cpus_allowed = cpumask_of_cpu(cpu);
155 }
156
157 int kthread_stop(struct task_struct *k)
158 {
159         int ret;
160
161         down(&kthread_stop_lock);
162
163         /* It could exit after stop_info.k set, but before wake_up_process. */
164         get_task_struct(k);
165
166         /* Must init completion *before* thread sees kthread_stop_info.k */
167         init_completion(&kthread_stop_info.done);
168         wmb();
169
170         /* Now set kthread_should_stop() to true, and wake it up. */
171         kthread_stop_info.k = k;
172         wake_up_process(k);
173         put_task_struct(k);
174
175         /* Once it dies, reset stop ptr, gather result and we're done. */
176         wait_for_completion(&kthread_stop_info.done);
177         kthread_stop_info.k = NULL;
178         ret = kthread_stop_info.err;
179         up(&kthread_stop_lock);
180
181         return ret;
182 }