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 <linux/mutex.h>
16 #include <asm/semaphore.h>
19 * We dont want to execute off keventd since it might
20 * hold a semaphore our callers hold too:
22 static struct workqueue_struct
*helper_wq
;
24 struct kthread_create_info
26 /* Information passed to kthread() from keventd. */
27 int (*threadfn
)(void *data
);
29 struct completion started
;
31 /* Result passed back to kthread_create() from keventd. */
32 struct task_struct
*result
;
33 struct completion done
;
36 struct kthread_stop_info
38 struct task_struct
*k
;
40 struct completion done
;
43 /* Thread stopping is done by setthing this var: lock serializes
44 * multiple kthread_stop calls. */
45 static DEFINE_MUTEX(kthread_stop_lock
);
46 static struct kthread_stop_info kthread_stop_info
;
48 int kthread_should_stop(void)
50 return (kthread_stop_info
.k
== current
);
52 EXPORT_SYMBOL(kthread_should_stop
);
54 static void kthread_exit_files(void)
57 struct task_struct
*tsk
= current
;
59 exit_fs(tsk
); /* current->fs->count--; */
62 atomic_inc(&fs
->count
);
64 current
->files
= init_task
.files
;
65 atomic_inc(&tsk
->files
->count
);
68 static int kthread(void *_create
)
70 struct kthread_create_info
*create
= _create
;
71 int (*threadfn
)(void *data
);
78 /* Copy data: it's on keventd's stack */
79 threadfn
= create
->threadfn
;
82 /* Block and flush all signals (in case we're not from keventd). */
84 sigprocmask(SIG_BLOCK
, &blocked
, NULL
);
85 flush_signals(current
);
87 /* By default we can run anywhere, unlike keventd. */
88 set_cpus_allowed(current
, CPU_MASK_ALL
);
90 /* OK, tell user we're spawned, wait for stop or wakeup */
91 __set_current_state(TASK_INTERRUPTIBLE
);
92 complete(&create
->started
);
95 if (!kthread_should_stop())
98 /* It might have exited on its own, w/o kthread_stop. Check. */
99 if (kthread_should_stop()) {
100 kthread_stop_info
.err
= ret
;
101 complete(&kthread_stop_info
.done
);
106 /* We are keventd: create a thread. */
107 static void keventd_create_kthread(void *_create
)
109 struct kthread_create_info
*create
= _create
;
112 /* We want our own signal handler (we take no signals by default). */
113 pid
= kernel_thread(kthread
, create
, CLONE_FS
| CLONE_FILES
| SIGCHLD
);
115 create
->result
= ERR_PTR(pid
);
117 wait_for_completion(&create
->started
);
118 create
->result
= find_task_by_pid(pid
);
120 complete(&create
->done
);
123 struct task_struct
*kthread_create(int (*threadfn
)(void *data
),
125 const char namefmt
[],
128 struct kthread_create_info create
;
129 DECLARE_WORK(work
, keventd_create_kthread
, &create
);
131 create
.threadfn
= threadfn
;
133 init_completion(&create
.started
);
134 init_completion(&create
.done
);
137 * The workqueue needs to start up first:
140 work
.func(work
.data
);
142 queue_work(helper_wq
, &work
);
143 wait_for_completion(&create
.done
);
145 if (!IS_ERR(create
.result
)) {
147 va_start(args
, namefmt
);
148 vsnprintf(create
.result
->comm
, sizeof(create
.result
->comm
),
153 return create
.result
;
155 EXPORT_SYMBOL(kthread_create
);
157 void kthread_bind(struct task_struct
*k
, unsigned int cpu
)
159 BUG_ON(k
->state
!= TASK_INTERRUPTIBLE
);
160 /* Must have done schedule() in kthread() before we set_task_cpu */
161 wait_task_inactive(k
);
162 set_task_cpu(k
, cpu
);
163 k
->cpus_allowed
= cpumask_of_cpu(cpu
);
165 EXPORT_SYMBOL(kthread_bind
);
167 int kthread_stop(struct task_struct
*k
)
169 return kthread_stop_sem(k
, NULL
);
171 EXPORT_SYMBOL(kthread_stop
);
173 int kthread_stop_sem(struct task_struct
*k
, struct semaphore
*s
)
177 mutex_lock(&kthread_stop_lock
);
179 /* It could exit after stop_info.k set, but before wake_up_process. */
182 /* Must init completion *before* thread sees kthread_stop_info.k */
183 init_completion(&kthread_stop_info
.done
);
186 /* Now set kthread_should_stop() to true, and wake it up. */
187 kthread_stop_info
.k
= k
;
194 /* Once it dies, reset stop ptr, gather result and we're done. */
195 wait_for_completion(&kthread_stop_info
.done
);
196 kthread_stop_info
.k
= NULL
;
197 ret
= kthread_stop_info
.err
;
198 mutex_unlock(&kthread_stop_lock
);
202 EXPORT_SYMBOL(kthread_stop_sem
);
204 static __init
int helper_init(void)
206 helper_wq
= create_singlethread_workqueue("kthread");
211 core_initcall(helper_init
);