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/cpuset.h>
13#include <linux/unistd.h>
14#include <linux/file.h>
15#include <linux/module.h>
16#include <linux/mutex.h>
17#include <linux/slab.h>
18#include <linux/freezer.h>
19#include <trace/events/sched.h>
20
21static DEFINE_SPINLOCK(kthread_create_lock);
22static LIST_HEAD(kthread_create_list);
23struct task_struct *kthreadd_task;
24
25struct kthread_create_info
26{
27
28 int (*threadfn)(void *data);
29 void *data;
30
31
32 struct task_struct *result;
33 struct completion done;
34
35 struct list_head list;
36};
37
38struct kthread {
39 int should_stop;
40 void *data;
41 struct completion exited;
42};
43
44#define to_kthread(tsk) \
45 container_of((tsk)->vfork_done, struct kthread, exited)
46
47
48
49
50
51
52
53
54int kthread_should_stop(void)
55{
56 return to_kthread(current)->should_stop;
57}
58EXPORT_SYMBOL(kthread_should_stop);
59
60
61
62
63
64
65
66
67
68void *kthread_data(struct task_struct *task)
69{
70 return to_kthread(task)->data;
71}
72
73static int kthread(void *_create)
74{
75
76 struct kthread_create_info *create = _create;
77 int (*threadfn)(void *data) = create->threadfn;
78 void *data = create->data;
79 struct kthread self;
80 int ret;
81
82 self.should_stop = 0;
83 self.data = data;
84 init_completion(&self.exited);
85 current->vfork_done = &self.exited;
86
87
88 __set_current_state(TASK_UNINTERRUPTIBLE);
89 create->result = current;
90 complete(&create->done);
91 schedule();
92
93 ret = -EINTR;
94 if (!self.should_stop)
95 ret = threadfn(data);
96
97
98 do_exit(ret);
99}
100
101static void create_kthread(struct kthread_create_info *create)
102{
103 int pid;
104
105
106 pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
107 if (pid < 0) {
108 create->result = ERR_PTR(pid);
109 complete(&create->done);
110 }
111}
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132struct task_struct *kthread_create(int (*threadfn)(void *data),
133 void *data,
134 const char namefmt[],
135 ...)
136{
137 struct kthread_create_info create;
138
139 create.threadfn = threadfn;
140 create.data = data;
141 init_completion(&create.done);
142
143 spin_lock(&kthread_create_lock);
144 list_add_tail(&create.list, &kthread_create_list);
145 spin_unlock(&kthread_create_lock);
146
147 wake_up_process(kthreadd_task);
148 wait_for_completion(&create.done);
149
150 if (!IS_ERR(create.result)) {
151 static const struct sched_param param = { .sched_priority = 0 };
152 va_list args;
153
154 va_start(args, namefmt);
155 vsnprintf(create.result->comm, sizeof(create.result->comm),
156 namefmt, args);
157 va_end(args);
158
159
160
161
162 sched_setscheduler_nocheck(create.result, SCHED_NORMAL, ¶m);
163 set_cpus_allowed_ptr(create.result, cpu_all_mask);
164 }
165 return create.result;
166}
167EXPORT_SYMBOL(kthread_create);
168
169
170
171
172
173
174
175
176
177
178void kthread_bind(struct task_struct *p, unsigned int cpu)
179{
180
181 if (!wait_task_inactive(p, TASK_UNINTERRUPTIBLE)) {
182 WARN_ON(1);
183 return;
184 }
185
186 p->cpus_allowed = cpumask_of_cpu(cpu);
187 p->rt.nr_cpus_allowed = 1;
188 p->flags |= PF_THREAD_BOUND;
189}
190EXPORT_SYMBOL(kthread_bind);
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207int kthread_stop(struct task_struct *k)
208{
209 struct kthread *kthread;
210 int ret;
211
212 trace_sched_kthread_stop(k);
213 get_task_struct(k);
214
215 kthread = to_kthread(k);
216 barrier();
217 if (k->vfork_done != NULL) {
218 kthread->should_stop = 1;
219 wake_up_process(k);
220 wait_for_completion(&kthread->exited);
221 }
222 ret = k->exit_code;
223
224 put_task_struct(k);
225 trace_sched_kthread_stop_ret(ret);
226
227 return ret;
228}
229EXPORT_SYMBOL(kthread_stop);
230
231int kthreadd(void *unused)
232{
233 struct task_struct *tsk = current;
234
235
236 set_task_comm(tsk, "kthreadd");
237 ignore_signals(tsk);
238 set_cpus_allowed_ptr(tsk, cpu_all_mask);
239 set_mems_allowed(node_states[N_HIGH_MEMORY]);
240
241 current->flags |= PF_NOFREEZE | PF_FREEZER_NOSIG;
242
243 for (;;) {
244 set_current_state(TASK_INTERRUPTIBLE);
245 if (list_empty(&kthread_create_list))
246 schedule();
247 __set_current_state(TASK_RUNNING);
248
249 spin_lock(&kthread_create_lock);
250 while (!list_empty(&kthread_create_list)) {
251 struct kthread_create_info *create;
252
253 create = list_entry(kthread_create_list.next,
254 struct kthread_create_info, list);
255 list_del_init(&create->list);
256 spin_unlock(&kthread_create_lock);
257
258 create_kthread(create);
259
260 spin_lock(&kthread_create_lock);
261 }
262 spin_unlock(&kthread_create_lock);
263 }
264
265 return 0;
266}
267
268void __init_kthread_worker(struct kthread_worker *worker,
269 const char *name,
270 struct lock_class_key *key)
271{
272 spin_lock_init(&worker->lock);
273 lockdep_set_class_and_name(&worker->lock, key, name);
274 INIT_LIST_HEAD(&worker->work_list);
275 worker->task = NULL;
276}
277EXPORT_SYMBOL_GPL(__init_kthread_worker);
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294int kthread_worker_fn(void *worker_ptr)
295{
296 struct kthread_worker *worker = worker_ptr;
297 struct kthread_work *work;
298
299 WARN_ON(worker->task);
300 worker->task = current;
301repeat:
302 set_current_state(TASK_INTERRUPTIBLE);
303
304 if (kthread_should_stop()) {
305 __set_current_state(TASK_RUNNING);
306 spin_lock_irq(&worker->lock);
307 worker->task = NULL;
308 spin_unlock_irq(&worker->lock);
309 return 0;
310 }
311
312 work = NULL;
313 spin_lock_irq(&worker->lock);
314 if (!list_empty(&worker->work_list)) {
315 work = list_first_entry(&worker->work_list,
316 struct kthread_work, node);
317 list_del_init(&work->node);
318 }
319 spin_unlock_irq(&worker->lock);
320
321 if (work) {
322 __set_current_state(TASK_RUNNING);
323 work->func(work);
324 smp_wmb();
325 work->done_seq = work->queue_seq;
326 smp_mb();
327 if (atomic_read(&work->flushing))
328 wake_up_all(&work->done);
329 } else if (!freezing(current))
330 schedule();
331
332 try_to_freeze();
333 goto repeat;
334}
335EXPORT_SYMBOL_GPL(kthread_worker_fn);
336
337
338
339
340
341
342
343
344
345
346bool queue_kthread_work(struct kthread_worker *worker,
347 struct kthread_work *work)
348{
349 bool ret = false;
350 unsigned long flags;
351
352 spin_lock_irqsave(&worker->lock, flags);
353 if (list_empty(&work->node)) {
354 list_add_tail(&work->node, &worker->work_list);
355 work->queue_seq++;
356 if (likely(worker->task))
357 wake_up_process(worker->task);
358 ret = true;
359 }
360 spin_unlock_irqrestore(&worker->lock, flags);
361 return ret;
362}
363EXPORT_SYMBOL_GPL(queue_kthread_work);
364
365
366
367
368
369
370
371void flush_kthread_work(struct kthread_work *work)
372{
373 int seq = work->queue_seq;
374
375 atomic_inc(&work->flushing);
376
377
378
379
380
381 smp_mb__after_atomic_inc();
382
383
384 wait_event(work->done, seq - work->done_seq <= 0);
385 atomic_dec(&work->flushing);
386
387
388
389
390
391 smp_mb__after_atomic_dec();
392}
393EXPORT_SYMBOL_GPL(flush_kthread_work);
394
395struct kthread_flush_work {
396 struct kthread_work work;
397 struct completion done;
398};
399
400static void kthread_flush_work_fn(struct kthread_work *work)
401{
402 struct kthread_flush_work *fwork =
403 container_of(work, struct kthread_flush_work, work);
404 complete(&fwork->done);
405}
406
407
408
409
410
411
412
413
414void flush_kthread_worker(struct kthread_worker *worker)
415{
416 struct kthread_flush_work fwork = {
417 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
418 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
419 };
420
421 queue_kthread_work(worker, &fwork.work);
422 wait_for_completion(&fwork.done);
423}
424EXPORT_SYMBOL_GPL(flush_kthread_worker);
425