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/unistd.h>
13#include <linux/file.h>
14#include <linux/module.h>
15#include <linux/mutex.h>
16#include <asm/semaphore.h>
17
18static DEFINE_SPINLOCK(kthread_create_lock);
19static LIST_HEAD(kthread_create_list);
20struct task_struct *kthreadd_task;
21
22struct kthread_create_info
23{
24
25 int (*threadfn)(void *data);
26 void *data;
27 struct completion started;
28
29
30 struct task_struct *result;
31 struct completion done;
32
33 struct list_head list;
34};
35
36struct kthread_stop_info
37{
38 struct task_struct *k;
39 int err;
40 struct completion done;
41};
42
43
44
45static DEFINE_MUTEX(kthread_stop_lock);
46static struct kthread_stop_info kthread_stop_info;
47
48
49
50
51
52
53
54
55int kthread_should_stop(void)
56{
57 return (kthread_stop_info.k == current);
58}
59EXPORT_SYMBOL(kthread_should_stop);
60
61static int kthread(void *_create)
62{
63 struct kthread_create_info *create = _create;
64 int (*threadfn)(void *data);
65 void *data;
66 int ret = -EINTR;
67
68
69 threadfn = create->threadfn;
70 data = create->data;
71
72
73 __set_current_state(TASK_UNINTERRUPTIBLE);
74 complete(&create->started);
75 schedule();
76
77 if (!kthread_should_stop())
78 ret = threadfn(data);
79
80
81 if (kthread_should_stop()) {
82 kthread_stop_info.err = ret;
83 complete(&kthread_stop_info.done);
84 }
85 return 0;
86}
87
88static void create_kthread(struct kthread_create_info *create)
89{
90 int pid;
91
92
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);
98 read_lock(&tasklist_lock);
99 create->result = find_task_by_pid(pid);
100 read_unlock(&tasklist_lock);
101 }
102 complete(&create->done);
103}
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124struct task_struct *kthread_create(int (*threadfn)(void *data),
125 void *data,
126 const char namefmt[],
127 ...)
128{
129 struct kthread_create_info create;
130
131 create.threadfn = threadfn;
132 create.data = data;
133 init_completion(&create.started);
134 init_completion(&create.done);
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
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 }
150 return create.result;
151}
152EXPORT_SYMBOL(kthread_create);
153
154
155
156
157
158
159
160
161
162
163void kthread_bind(struct task_struct *k, unsigned int cpu)
164{
165 if (k->state != TASK_UNINTERRUPTIBLE) {
166 WARN_ON(1);
167 return;
168 }
169
170 wait_task_inactive(k);
171 set_task_cpu(k, cpu);
172 k->cpus_allowed = cpumask_of_cpu(cpu);
173}
174EXPORT_SYMBOL(kthread_bind);
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189int kthread_stop(struct task_struct *k)
190{
191 int ret;
192
193 mutex_lock(&kthread_stop_lock);
194
195
196 get_task_struct(k);
197
198
199 init_completion(&kthread_stop_info.done);
200 smp_wmb();
201
202
203 kthread_stop_info.k = k;
204 wake_up_process(k);
205 put_task_struct(k);
206
207
208 wait_for_completion(&kthread_stop_info.done);
209 kthread_stop_info.k = NULL;
210 ret = kthread_stop_info.err;
211 mutex_unlock(&kthread_stop_lock);
212
213 return ret;
214}
215EXPORT_SYMBOL(kthread_stop);
216
217int kthreadd(void *unused)
218{
219 struct task_struct *tsk = current;
220
221
222 set_task_comm(tsk, "kthreadd");
223 ignore_signals(tsk);
224 set_user_nice(tsk, -5);
225 set_cpus_allowed(tsk, CPU_MASK_ALL);
226
227 current->flags |= PF_NOFREEZE;
228
229 for (;;) {
230 set_current_state(TASK_INTERRUPTIBLE);
231 if (list_empty(&kthread_create_list))
232 schedule();
233 __set_current_state(TASK_RUNNING);
234
235 spin_lock(&kthread_create_lock);
236 while (!list_empty(&kthread_create_list)) {
237 struct kthread_create_info *create;
238
239 create = list_entry(kthread_create_list.next,
240 struct kthread_create_info, list);
241 list_del_init(&create->list);
242 spin_unlock(&kthread_create_lock);
243
244 create_kthread(create);
245
246 spin_lock(&kthread_create_lock);
247 }
248 spin_unlock(&kthread_create_lock);
249 }
250
251 return 0;
252}
253