| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 <linux/module.h> | 
| Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 15 | #include <linux/mutex.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/semaphore.h> | 
 | 17 |  | 
 | 18 | /* | 
 | 19 |  * We dont want to execute off keventd since it might | 
 | 20 |  * hold a semaphore our callers hold too: | 
 | 21 |  */ | 
 | 22 | static struct workqueue_struct *helper_wq; | 
 | 23 |  | 
 | 24 | struct kthread_create_info | 
 | 25 | { | 
 | 26 | 	/* Information passed to kthread() from keventd. */ | 
 | 27 | 	int (*threadfn)(void *data); | 
 | 28 | 	void *data; | 
 | 29 | 	struct completion started; | 
 | 30 |  | 
 | 31 | 	/* Result passed back to kthread_create() from keventd. */ | 
 | 32 | 	struct task_struct *result; | 
 | 33 | 	struct completion done; | 
 | 34 | }; | 
 | 35 |  | 
 | 36 | struct 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. */ | 
| Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 45 | static DEFINE_MUTEX(kthread_stop_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | static struct kthread_stop_info kthread_stop_info; | 
 | 47 |  | 
| Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 48 | /** | 
 | 49 |  * kthread_should_stop - should this kthread return now? | 
 | 50 |  * | 
 | 51 |  * When someone calls kthread_stop on your kthread, it will be woken | 
 | 52 |  * and this will return true.  You should then return, and your return | 
 | 53 |  * value will be passed through to kthread_stop(). | 
 | 54 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | int kthread_should_stop(void) | 
 | 56 | { | 
 | 57 | 	return (kthread_stop_info.k == current); | 
 | 58 | } | 
 | 59 | EXPORT_SYMBOL(kthread_should_stop); | 
 | 60 |  | 
 | 61 | static void kthread_exit_files(void) | 
 | 62 | { | 
 | 63 | 	struct fs_struct *fs; | 
 | 64 | 	struct task_struct *tsk = current; | 
 | 65 |  | 
 | 66 | 	exit_fs(tsk);		/* current->fs->count--; */ | 
 | 67 | 	fs = init_task.fs; | 
 | 68 | 	tsk->fs = fs; | 
 | 69 | 	atomic_inc(&fs->count); | 
 | 70 |  	exit_files(tsk); | 
 | 71 | 	current->files = init_task.files; | 
 | 72 | 	atomic_inc(&tsk->files->count); | 
 | 73 | } | 
 | 74 |  | 
 | 75 | static int kthread(void *_create) | 
 | 76 | { | 
 | 77 | 	struct kthread_create_info *create = _create; | 
 | 78 | 	int (*threadfn)(void *data); | 
 | 79 | 	void *data; | 
 | 80 | 	sigset_t blocked; | 
 | 81 | 	int ret = -EINTR; | 
 | 82 |  | 
 | 83 | 	kthread_exit_files(); | 
 | 84 |  | 
 | 85 | 	/* Copy data: it's on keventd's stack */ | 
 | 86 | 	threadfn = create->threadfn; | 
 | 87 | 	data = create->data; | 
 | 88 |  | 
 | 89 | 	/* Block and flush all signals (in case we're not from keventd). */ | 
 | 90 | 	sigfillset(&blocked); | 
 | 91 | 	sigprocmask(SIG_BLOCK, &blocked, NULL); | 
 | 92 | 	flush_signals(current); | 
 | 93 |  | 
 | 94 | 	/* By default we can run anywhere, unlike keventd. */ | 
 | 95 | 	set_cpus_allowed(current, CPU_MASK_ALL); | 
 | 96 |  | 
 | 97 | 	/* OK, tell user we're spawned, wait for stop or wakeup */ | 
 | 98 | 	__set_current_state(TASK_INTERRUPTIBLE); | 
 | 99 | 	complete(&create->started); | 
 | 100 | 	schedule(); | 
 | 101 |  | 
 | 102 | 	if (!kthread_should_stop()) | 
 | 103 | 		ret = threadfn(data); | 
 | 104 |  | 
 | 105 | 	/* It might have exited on its own, w/o kthread_stop.  Check. */ | 
 | 106 | 	if (kthread_should_stop()) { | 
 | 107 | 		kthread_stop_info.err = ret; | 
 | 108 | 		complete(&kthread_stop_info.done); | 
 | 109 | 	} | 
 | 110 | 	return 0; | 
 | 111 | } | 
 | 112 |  | 
 | 113 | /* We are keventd: create a thread. */ | 
 | 114 | static void keventd_create_kthread(void *_create) | 
 | 115 | { | 
 | 116 | 	struct kthread_create_info *create = _create; | 
 | 117 | 	int pid; | 
 | 118 |  | 
 | 119 | 	/* We want our own signal handler (we take no signals by default). */ | 
 | 120 | 	pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD); | 
 | 121 | 	if (pid < 0) { | 
 | 122 | 		create->result = ERR_PTR(pid); | 
 | 123 | 	} else { | 
 | 124 | 		wait_for_completion(&create->started); | 
| Andrew Morton | 05eeae2 | 2006-03-25 03:07:48 -0800 | [diff] [blame] | 125 | 		read_lock(&tasklist_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | 		create->result = find_task_by_pid(pid); | 
| Andrew Morton | 05eeae2 | 2006-03-25 03:07:48 -0800 | [diff] [blame] | 127 | 		read_unlock(&tasklist_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | 	} | 
 | 129 | 	complete(&create->done); | 
 | 130 | } | 
 | 131 |  | 
| Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 132 | /** | 
 | 133 |  * kthread_create - create a kthread. | 
 | 134 |  * @threadfn: the function to run until signal_pending(current). | 
 | 135 |  * @data: data ptr for @threadfn. | 
 | 136 |  * @namefmt: printf-style name for the thread. | 
 | 137 |  * | 
 | 138 |  * Description: This helper function creates and names a kernel | 
 | 139 |  * thread.  The thread will be stopped: use wake_up_process() to start | 
 | 140 |  * it.  See also kthread_run(), kthread_create_on_cpu(). | 
 | 141 |  * | 
 | 142 |  * When woken, the thread will run @threadfn() with @data as its | 
 | 143 |  * argument. @threadfn can either call do_exit() directly if it is a | 
 | 144 |  * standalone thread for which noone will call kthread_stop(), or | 
 | 145 |  * return when 'kthread_should_stop()' is true (which means | 
 | 146 |  * kthread_stop() has been called).  The return value should be zero | 
 | 147 |  * or a negative error number; it will be passed to kthread_stop(). | 
 | 148 |  * | 
 | 149 |  * Returns a task_struct or ERR_PTR(-ENOMEM). | 
 | 150 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | struct task_struct *kthread_create(int (*threadfn)(void *data), | 
 | 152 | 				   void *data, | 
 | 153 | 				   const char namefmt[], | 
 | 154 | 				   ...) | 
 | 155 | { | 
 | 156 | 	struct kthread_create_info create; | 
 | 157 | 	DECLARE_WORK(work, keventd_create_kthread, &create); | 
 | 158 |  | 
 | 159 | 	create.threadfn = threadfn; | 
 | 160 | 	create.data = data; | 
 | 161 | 	init_completion(&create.started); | 
 | 162 | 	init_completion(&create.done); | 
 | 163 |  | 
 | 164 | 	/* | 
 | 165 | 	 * The workqueue needs to start up first: | 
 | 166 | 	 */ | 
 | 167 | 	if (!helper_wq) | 
 | 168 | 		work.func(work.data); | 
 | 169 | 	else { | 
 | 170 | 		queue_work(helper_wq, &work); | 
 | 171 | 		wait_for_completion(&create.done); | 
 | 172 | 	} | 
 | 173 | 	if (!IS_ERR(create.result)) { | 
 | 174 | 		va_list args; | 
 | 175 | 		va_start(args, namefmt); | 
 | 176 | 		vsnprintf(create.result->comm, sizeof(create.result->comm), | 
 | 177 | 			  namefmt, args); | 
 | 178 | 		va_end(args); | 
 | 179 | 	} | 
 | 180 |  | 
 | 181 | 	return create.result; | 
 | 182 | } | 
 | 183 | EXPORT_SYMBOL(kthread_create); | 
 | 184 |  | 
| Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 185 | /** | 
 | 186 |  * kthread_bind - bind a just-created kthread to a cpu. | 
 | 187 |  * @k: thread created by kthread_create(). | 
 | 188 |  * @cpu: cpu (might not be online, must be possible) for @k to run on. | 
 | 189 |  * | 
 | 190 |  * Description: This function is equivalent to set_cpus_allowed(), | 
 | 191 |  * except that @cpu doesn't need to be online, and the thread must be | 
 | 192 |  * stopped (i.e., just returned from kthread_create(). | 
 | 193 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | void kthread_bind(struct task_struct *k, unsigned int cpu) | 
 | 195 | { | 
 | 196 | 	BUG_ON(k->state != TASK_INTERRUPTIBLE); | 
 | 197 | 	/* Must have done schedule() in kthread() before we set_task_cpu */ | 
 | 198 | 	wait_task_inactive(k); | 
 | 199 | 	set_task_cpu(k, cpu); | 
 | 200 | 	k->cpus_allowed = cpumask_of_cpu(cpu); | 
 | 201 | } | 
 | 202 | EXPORT_SYMBOL(kthread_bind); | 
 | 203 |  | 
| Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 204 | /** | 
 | 205 |  * kthread_stop - stop a thread created by kthread_create(). | 
 | 206 |  * @k: thread created by kthread_create(). | 
 | 207 |  * | 
 | 208 |  * Sets kthread_should_stop() for @k to return true, wakes it, and | 
 | 209 |  * waits for it to exit.  Your threadfn() must not call do_exit() | 
 | 210 |  * itself if you use this function!  This can also be called after | 
 | 211 |  * kthread_create() instead of calling wake_up_process(): the thread | 
 | 212 |  * will exit without calling threadfn(). | 
 | 213 |  * | 
 | 214 |  * Returns the result of threadfn(), or %-EINTR if wake_up_process() | 
 | 215 |  * was never called. | 
 | 216 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | int kthread_stop(struct task_struct *k) | 
 | 218 | { | 
| Alan Stern | 61e1a9e | 2005-10-30 15:01:40 -0800 | [diff] [blame] | 219 | 	return kthread_stop_sem(k, NULL); | 
 | 220 | } | 
 | 221 | EXPORT_SYMBOL(kthread_stop); | 
 | 222 |  | 
| Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 223 | /** | 
 | 224 |  * kthread_stop_sem - stop a thread created by kthread_create(). | 
 | 225 |  * @k: thread created by kthread_create(). | 
 | 226 |  * @s: semaphore that @k waits on while idle. | 
 | 227 |  * | 
 | 228 |  * Does essentially the same thing as kthread_stop() above, but wakes | 
 | 229 |  * @k by calling up(@s). | 
 | 230 |  * | 
 | 231 |  * Returns the result of threadfn(), or %-EINTR if wake_up_process() | 
 | 232 |  * was never called. | 
 | 233 |  */ | 
| Alan Stern | 61e1a9e | 2005-10-30 15:01:40 -0800 | [diff] [blame] | 234 | int kthread_stop_sem(struct task_struct *k, struct semaphore *s) | 
 | 235 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | 	int ret; | 
 | 237 |  | 
| Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 238 | 	mutex_lock(&kthread_stop_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 |  | 
 | 240 | 	/* It could exit after stop_info.k set, but before wake_up_process. */ | 
 | 241 | 	get_task_struct(k); | 
 | 242 |  | 
 | 243 | 	/* Must init completion *before* thread sees kthread_stop_info.k */ | 
 | 244 | 	init_completion(&kthread_stop_info.done); | 
| akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 245 | 	smp_wmb(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 |  | 
 | 247 | 	/* Now set kthread_should_stop() to true, and wake it up. */ | 
 | 248 | 	kthread_stop_info.k = k; | 
| Alan Stern | 61e1a9e | 2005-10-30 15:01:40 -0800 | [diff] [blame] | 249 | 	if (s) | 
 | 250 | 		up(s); | 
 | 251 | 	else | 
 | 252 | 		wake_up_process(k); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | 	put_task_struct(k); | 
 | 254 |  | 
 | 255 | 	/* Once it dies, reset stop ptr, gather result and we're done. */ | 
 | 256 | 	wait_for_completion(&kthread_stop_info.done); | 
 | 257 | 	kthread_stop_info.k = NULL; | 
 | 258 | 	ret = kthread_stop_info.err; | 
| Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 259 | 	mutex_unlock(&kthread_stop_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 |  | 
 | 261 | 	return ret; | 
 | 262 | } | 
| Alan Stern | 61e1a9e | 2005-10-30 15:01:40 -0800 | [diff] [blame] | 263 | EXPORT_SYMBOL(kthread_stop_sem); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 |  | 
 | 265 | static __init int helper_init(void) | 
 | 266 | { | 
 | 267 | 	helper_wq = create_singlethread_workqueue("kthread"); | 
 | 268 | 	BUG_ON(!helper_wq); | 
 | 269 |  | 
 | 270 | 	return 0; | 
 | 271 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 |  | 
| Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 273 | core_initcall(helper_init); |