| 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 |  * | 
| Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 4 |  * Creation is done via kthreadd, so that we get a clean environment | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> | 
| Miao Xie | 58568d2 | 2009-06-16 15:31:49 -0700 | [diff] [blame] | 12 | #include <linux/cpuset.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/unistd.h> | 
 | 14 | #include <linux/file.h> | 
 | 15 | #include <linux/module.h> | 
| Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 16 | #include <linux/mutex.h> | 
| Steven Rostedt | ad8d75f | 2009-04-14 19:39:12 -0400 | [diff] [blame] | 17 | #include <trace/events/sched.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 |  | 
| Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 19 | static DEFINE_SPINLOCK(kthread_create_lock); | 
 | 20 | static LIST_HEAD(kthread_create_list); | 
 | 21 | struct task_struct *kthreadd_task; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 |  | 
 | 23 | struct kthread_create_info | 
 | 24 | { | 
| Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 25 | 	/* Information passed to kthread() from kthreadd. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | 	int (*threadfn)(void *data); | 
 | 27 | 	void *data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 |  | 
| Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 29 | 	/* Result passed back to kthread_create() from kthreadd. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | 	struct task_struct *result; | 
 | 31 | 	struct completion done; | 
| David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 32 |  | 
| Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 33 | 	struct list_head list; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | }; | 
 | 35 |  | 
| Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 36 | struct kthread { | 
 | 37 | 	int should_stop; | 
 | 38 | 	struct completion exited; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | }; | 
 | 40 |  | 
| Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 41 | #define to_kthread(tsk)	\ | 
 | 42 | 	container_of((tsk)->vfork_done, struct kthread, exited) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 |  | 
| Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 44 | /** | 
 | 45 |  * kthread_should_stop - should this kthread return now? | 
 | 46 |  * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 47 |  * When someone calls kthread_stop() on your kthread, it will be woken | 
| Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 48 |  * and this will return true.  You should then return, and your return | 
 | 49 |  * value will be passed through to kthread_stop(). | 
 | 50 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | int kthread_should_stop(void) | 
 | 52 | { | 
| Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 53 | 	return to_kthread(current)->should_stop; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } | 
 | 55 | EXPORT_SYMBOL(kthread_should_stop); | 
 | 56 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | static int kthread(void *_create) | 
 | 58 | { | 
| Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 59 | 	/* Copy data: it's on kthread's stack */ | 
| Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 60 | 	struct kthread_create_info *create = _create; | 
 | 61 | 	int (*threadfn)(void *data) = create->threadfn; | 
 | 62 | 	void *data = create->data; | 
 | 63 | 	struct kthread self; | 
 | 64 | 	int ret; | 
 | 65 |  | 
 | 66 | 	self.should_stop = 0; | 
 | 67 | 	init_completion(&self.exited); | 
 | 68 | 	current->vfork_done = &self.exited; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | 	/* OK, tell user we're spawned, wait for stop or wakeup */ | 
| Oleg Nesterov | a076e4b | 2007-05-23 13:57:27 -0700 | [diff] [blame] | 71 | 	__set_current_state(TASK_UNINTERRUPTIBLE); | 
| Vitaliy Gusev | 3217ab9 | 2009-04-09 09:50:35 -0600 | [diff] [blame] | 72 | 	create->result = current; | 
| Oleg Nesterov | cdd140b | 2009-06-17 16:27:43 -0700 | [diff] [blame] | 73 | 	complete(&create->done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | 	schedule(); | 
 | 75 |  | 
| Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 76 | 	ret = -EINTR; | 
 | 77 | 	if (!self.should_stop) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | 		ret = threadfn(data); | 
 | 79 |  | 
| Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 80 | 	/* we can't just return, we must preserve "self" on stack */ | 
 | 81 | 	do_exit(ret); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } | 
 | 83 |  | 
| Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 84 | static void create_kthread(struct kthread_create_info *create) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | 	int pid; | 
 | 87 |  | 
 | 88 | 	/* We want our own signal handler (we take no signals by default). */ | 
 | 89 | 	pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD); | 
| Oleg Nesterov | cdd140b | 2009-06-17 16:27:43 -0700 | [diff] [blame] | 90 | 	if (pid < 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | 		create->result = ERR_PTR(pid); | 
| Oleg Nesterov | cdd140b | 2009-06-17 16:27:43 -0700 | [diff] [blame] | 92 | 		complete(&create->done); | 
 | 93 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } | 
 | 95 |  | 
| Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 96 | /** | 
 | 97 |  * kthread_create - create a kthread. | 
 | 98 |  * @threadfn: the function to run until signal_pending(current). | 
 | 99 |  * @data: data ptr for @threadfn. | 
 | 100 |  * @namefmt: printf-style name for the thread. | 
 | 101 |  * | 
 | 102 |  * Description: This helper function creates and names a kernel | 
 | 103 |  * thread.  The thread will be stopped: use wake_up_process() to start | 
| Anton Blanchard | 301ba04 | 2010-02-09 15:07:40 +1100 | [diff] [blame] | 104 |  * it.  See also kthread_run(). | 
| Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 105 |  * | 
 | 106 |  * When woken, the thread will run @threadfn() with @data as its | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 107 |  * argument. @threadfn() can either call do_exit() directly if it is a | 
| Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 108 |  * standalone thread for which noone will call kthread_stop(), or | 
 | 109 |  * return when 'kthread_should_stop()' is true (which means | 
 | 110 |  * kthread_stop() has been called).  The return value should be zero | 
 | 111 |  * or a negative error number; it will be passed to kthread_stop(). | 
 | 112 |  * | 
 | 113 |  * Returns a task_struct or ERR_PTR(-ENOMEM). | 
 | 114 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | struct task_struct *kthread_create(int (*threadfn)(void *data), | 
 | 116 | 				   void *data, | 
 | 117 | 				   const char namefmt[], | 
 | 118 | 				   ...) | 
 | 119 | { | 
 | 120 | 	struct kthread_create_info create; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 |  | 
 | 122 | 	create.threadfn = threadfn; | 
 | 123 | 	create.data = data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | 	init_completion(&create.done); | 
 | 125 |  | 
| Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 126 | 	spin_lock(&kthread_create_lock); | 
 | 127 | 	list_add_tail(&create.list, &kthread_create_list); | 
| Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 128 | 	spin_unlock(&kthread_create_lock); | 
 | 129 |  | 
| Dmitry Adamushko | cbd9b67 | 2008-04-29 00:59:23 -0700 | [diff] [blame] | 130 | 	wake_up_process(kthreadd_task); | 
| Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 131 | 	wait_for_completion(&create.done); | 
 | 132 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | 	if (!IS_ERR(create.result)) { | 
| Oleg Nesterov | 1c99315 | 2009-04-09 09:50:36 -0600 | [diff] [blame] | 134 | 		struct sched_param param = { .sched_priority = 0 }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | 		va_list args; | 
| Oleg Nesterov | 1c99315 | 2009-04-09 09:50:36 -0600 | [diff] [blame] | 136 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | 		va_start(args, namefmt); | 
 | 138 | 		vsnprintf(create.result->comm, sizeof(create.result->comm), | 
 | 139 | 			  namefmt, args); | 
 | 140 | 		va_end(args); | 
| Oleg Nesterov | 1c99315 | 2009-04-09 09:50:36 -0600 | [diff] [blame] | 141 | 		/* | 
 | 142 | 		 * root may have changed our (kthreadd's) priority or CPU mask. | 
 | 143 | 		 * The kernel thread should not inherit these properties. | 
 | 144 | 		 */ | 
 | 145 | 		sched_setscheduler_nocheck(create.result, SCHED_NORMAL, ¶m); | 
| Oleg Nesterov | 1c99315 | 2009-04-09 09:50:36 -0600 | [diff] [blame] | 146 | 		set_cpus_allowed_ptr(create.result, cpu_all_mask); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | 	return create.result; | 
 | 149 | } | 
 | 150 | EXPORT_SYMBOL(kthread_create); | 
 | 151 |  | 
| Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 152 | /** | 
| Peter Zijlstra | 881232b | 2009-12-16 18:04:39 +0100 | [diff] [blame] | 153 |  * kthread_bind - bind a just-created kthread to a cpu. | 
 | 154 |  * @p: thread created by kthread_create(). | 
 | 155 |  * @cpu: cpu (might not be online, must be possible) for @k to run on. | 
 | 156 |  * | 
 | 157 |  * Description: This function is equivalent to set_cpus_allowed(), | 
 | 158 |  * except that @cpu doesn't need to be online, and the thread must be | 
 | 159 |  * stopped (i.e., just returned from kthread_create()). | 
 | 160 |  */ | 
 | 161 | void kthread_bind(struct task_struct *p, unsigned int cpu) | 
 | 162 | { | 
 | 163 | 	/* Must have done schedule() in kthread() before we set_task_cpu */ | 
 | 164 | 	if (!wait_task_inactive(p, TASK_UNINTERRUPTIBLE)) { | 
 | 165 | 		WARN_ON(1); | 
 | 166 | 		return; | 
 | 167 | 	} | 
 | 168 |  | 
 | 169 | 	p->cpus_allowed = cpumask_of_cpu(cpu); | 
 | 170 | 	p->rt.nr_cpus_allowed = 1; | 
 | 171 | 	p->flags |= PF_THREAD_BOUND; | 
 | 172 | } | 
 | 173 | EXPORT_SYMBOL(kthread_bind); | 
 | 174 |  | 
 | 175 | /** | 
| Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 176 |  * kthread_stop - stop a thread created by kthread_create(). | 
 | 177 |  * @k: thread created by kthread_create(). | 
 | 178 |  * | 
 | 179 |  * Sets kthread_should_stop() for @k to return true, wakes it, and | 
| Oleg Nesterov | 9ae2602 | 2009-06-19 02:51:13 +0200 | [diff] [blame] | 180 |  * waits for it to exit. This can also be called after kthread_create() | 
 | 181 |  * instead of calling wake_up_process(): the thread will exit without | 
 | 182 |  * calling threadfn(). | 
 | 183 |  * | 
 | 184 |  * If threadfn() may call do_exit() itself, the caller must ensure | 
 | 185 |  * task_struct can't go away. | 
| Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 186 |  * | 
 | 187 |  * Returns the result of threadfn(), or %-EINTR if wake_up_process() | 
 | 188 |  * was never called. | 
 | 189 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | int kthread_stop(struct task_struct *k) | 
 | 191 | { | 
| Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 192 | 	struct kthread *kthread; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | 	int ret; | 
 | 194 |  | 
| Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 195 | 	trace_sched_kthread_stop(k); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | 	get_task_struct(k); | 
 | 197 |  | 
| Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 198 | 	kthread = to_kthread(k); | 
 | 199 | 	barrier(); /* it might have exited */ | 
 | 200 | 	if (k->vfork_done != NULL) { | 
 | 201 | 		kthread->should_stop = 1; | 
 | 202 | 		wake_up_process(k); | 
 | 203 | 		wait_for_completion(&kthread->exited); | 
 | 204 | 	} | 
 | 205 | 	ret = k->exit_code; | 
| Mathieu Desnoyers | 0a16b60 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 206 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | 	put_task_struct(k); | 
| Mathieu Desnoyers | 0a16b60 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 208 | 	trace_sched_kthread_stop_ret(ret); | 
 | 209 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | 	return ret; | 
 | 211 | } | 
| Adrian Bunk | 52e92e5 | 2006-07-14 00:24:05 -0700 | [diff] [blame] | 212 | EXPORT_SYMBOL(kthread_stop); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 |  | 
| Satyam Sharma | e804a4a | 2007-07-31 00:39:16 -0700 | [diff] [blame] | 214 | int kthreadd(void *unused) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { | 
| Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 216 | 	struct task_struct *tsk = current; | 
| Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 217 |  | 
| Satyam Sharma | e804a4a | 2007-07-31 00:39:16 -0700 | [diff] [blame] | 218 | 	/* Setup a clean context for our children to inherit. */ | 
| Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 219 | 	set_task_comm(tsk, "kthreadd"); | 
| Oleg Nesterov | 10ab825 | 2007-05-09 02:34:37 -0700 | [diff] [blame] | 220 | 	ignore_signals(tsk); | 
| Rusty Russell | 1a2142a | 2009-03-30 22:05:10 -0600 | [diff] [blame] | 221 | 	set_cpus_allowed_ptr(tsk, cpu_all_mask); | 
| Miao Xie | 5ab116c | 2010-03-23 13:35:34 -0700 | [diff] [blame] | 222 | 	set_mems_allowed(node_states[N_HIGH_MEMORY]); | 
| Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 223 |  | 
| Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 224 | 	current->flags |= PF_NOFREEZE | PF_FREEZER_NOSIG; | 
| Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 225 |  | 
 | 226 | 	for (;;) { | 
 | 227 | 		set_current_state(TASK_INTERRUPTIBLE); | 
 | 228 | 		if (list_empty(&kthread_create_list)) | 
 | 229 | 			schedule(); | 
 | 230 | 		__set_current_state(TASK_RUNNING); | 
 | 231 |  | 
 | 232 | 		spin_lock(&kthread_create_lock); | 
 | 233 | 		while (!list_empty(&kthread_create_list)) { | 
 | 234 | 			struct kthread_create_info *create; | 
 | 235 |  | 
 | 236 | 			create = list_entry(kthread_create_list.next, | 
 | 237 | 					    struct kthread_create_info, list); | 
 | 238 | 			list_del_init(&create->list); | 
 | 239 | 			spin_unlock(&kthread_create_lock); | 
 | 240 |  | 
 | 241 | 			create_kthread(create); | 
 | 242 |  | 
 | 243 | 			spin_lock(&kthread_create_lock); | 
 | 244 | 		} | 
 | 245 | 		spin_unlock(&kthread_create_lock); | 
 | 246 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 |  | 
 | 248 | 	return 0; | 
 | 249 | } |