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> |
| 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 | |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 18 | static DEFINE_SPINLOCK(kthread_create_lock); |
| 19 | static LIST_HEAD(kthread_create_list); |
| 20 | struct task_struct *kthreadd_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | struct kthread_create_info |
| 23 | { |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 24 | /* Information passed to kthread() from kthreadd. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | int (*threadfn)(void *data); |
| 26 | void *data; |
| 27 | struct completion started; |
| 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 | |
| 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 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 51 | * When someone calls kthread_stop() on your kthread, it will be woken |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | static int kthread(void *_create) |
| 62 | { |
| 63 | struct kthread_create_info *create = _create; |
| 64 | int (*threadfn)(void *data); |
| 65 | void *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | int ret = -EINTR; |
| 67 | |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 68 | /* Copy data: it's on kthread's stack */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | threadfn = create->threadfn; |
| 70 | data = create->data; |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* OK, tell user we're spawned, wait for stop or wakeup */ |
Oleg Nesterov | a076e4b | 2007-05-23 13:57:27 -0700 | [diff] [blame] | 73 | __set_current_state(TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | complete(&create->started); |
| 75 | schedule(); |
| 76 | |
| 77 | if (!kthread_should_stop()) |
| 78 | ret = threadfn(data); |
| 79 | |
| 80 | /* It might have exited on its own, w/o kthread_stop. Check. */ |
| 81 | if (kthread_should_stop()) { |
| 82 | kthread_stop_info.err = ret; |
| 83 | complete(&kthread_stop_info.done); |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 88 | static void create_kthread(struct kthread_create_info *create) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | int pid; |
| 91 | |
| 92 | /* We want our own signal handler (we take no signals by default). */ |
| 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); |
Andrew Morton | 05eeae2 | 2006-03-25 03:07:48 -0800 | [diff] [blame] | 98 | read_lock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | create->result = find_task_by_pid(pid); |
Andrew Morton | 05eeae2 | 2006-03-25 03:07:48 -0800 | [diff] [blame] | 100 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
| 102 | complete(&create->done); |
| 103 | } |
| 104 | |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 105 | /** |
| 106 | * kthread_create - create a kthread. |
| 107 | * @threadfn: the function to run until signal_pending(current). |
| 108 | * @data: data ptr for @threadfn. |
| 109 | * @namefmt: printf-style name for the thread. |
| 110 | * |
| 111 | * Description: This helper function creates and names a kernel |
| 112 | * thread. The thread will be stopped: use wake_up_process() to start |
| 113 | * it. See also kthread_run(), kthread_create_on_cpu(). |
| 114 | * |
| 115 | * 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] | 116 | * argument. @threadfn() can either call do_exit() directly if it is a |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 117 | * standalone thread for which noone will call kthread_stop(), or |
| 118 | * return when 'kthread_should_stop()' is true (which means |
| 119 | * kthread_stop() has been called). The return value should be zero |
| 120 | * or a negative error number; it will be passed to kthread_stop(). |
| 121 | * |
| 122 | * Returns a task_struct or ERR_PTR(-ENOMEM). |
| 123 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | struct task_struct *kthread_create(int (*threadfn)(void *data), |
| 125 | void *data, |
| 126 | const char namefmt[], |
| 127 | ...) |
| 128 | { |
| 129 | struct kthread_create_info create; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | create.threadfn = threadfn; |
| 132 | create.data = data; |
| 133 | init_completion(&create.started); |
| 134 | init_completion(&create.done); |
| 135 | |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | return create.result; |
| 151 | } |
| 152 | EXPORT_SYMBOL(kthread_create); |
| 153 | |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 154 | /** |
| 155 | * kthread_bind - bind a just-created kthread to a cpu. |
| 156 | * @k: thread created by kthread_create(). |
| 157 | * @cpu: cpu (might not be online, must be possible) for @k to run on. |
| 158 | * |
| 159 | * Description: This function is equivalent to set_cpus_allowed(), |
| 160 | * except that @cpu doesn't need to be online, and the thread must be |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 161 | * stopped (i.e., just returned from kthread_create()). |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 162 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | void kthread_bind(struct task_struct *k, unsigned int cpu) |
| 164 | { |
Oleg Nesterov | a076e4b | 2007-05-23 13:57:27 -0700 | [diff] [blame] | 165 | if (k->state != TASK_UNINTERRUPTIBLE) { |
| 166 | WARN_ON(1); |
| 167 | return; |
| 168 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | /* Must have done schedule() in kthread() before we set_task_cpu */ |
| 170 | wait_task_inactive(k); |
| 171 | set_task_cpu(k, cpu); |
| 172 | k->cpus_allowed = cpumask_of_cpu(cpu); |
| 173 | } |
| 174 | EXPORT_SYMBOL(kthread_bind); |
| 175 | |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 176 | /** |
| 177 | * kthread_stop - stop a thread created by kthread_create(). |
| 178 | * @k: thread created by kthread_create(). |
| 179 | * |
| 180 | * Sets kthread_should_stop() for @k to return true, wakes it, and |
| 181 | * waits for it to exit. Your threadfn() must not call do_exit() |
| 182 | * itself if you use this function! This can also be called after |
| 183 | * kthread_create() instead of calling wake_up_process(): the thread |
| 184 | * will exit without calling threadfn(). |
| 185 | * |
| 186 | * Returns the result of threadfn(), or %-EINTR if wake_up_process() |
| 187 | * was never called. |
| 188 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | int kthread_stop(struct task_struct *k) |
| 190 | { |
| 191 | int ret; |
| 192 | |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 193 | mutex_lock(&kthread_stop_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
| 195 | /* It could exit after stop_info.k set, but before wake_up_process. */ |
| 196 | get_task_struct(k); |
| 197 | |
| 198 | /* Must init completion *before* thread sees kthread_stop_info.k */ |
| 199 | init_completion(&kthread_stop_info.done); |
akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 200 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | /* Now set kthread_should_stop() to true, and wake it up. */ |
| 203 | kthread_stop_info.k = k; |
Adrian Bunk | 52e92e5 | 2006-07-14 00:24:05 -0700 | [diff] [blame] | 204 | wake_up_process(k); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | put_task_struct(k); |
| 206 | |
| 207 | /* Once it dies, reset stop ptr, gather result and we're done. */ |
| 208 | wait_for_completion(&kthread_stop_info.done); |
| 209 | kthread_stop_info.k = NULL; |
| 210 | ret = kthread_stop_info.err; |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 211 | mutex_unlock(&kthread_stop_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | return ret; |
| 214 | } |
Adrian Bunk | 52e92e5 | 2006-07-14 00:24:05 -0700 | [diff] [blame] | 215 | EXPORT_SYMBOL(kthread_stop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 217 | |
Jan Beulich | 98011f5 | 2007-07-15 23:38:17 -0700 | [diff] [blame] | 218 | static noinline __init_refok void kthreadd_setup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 220 | struct task_struct *tsk = current; |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 221 | |
| 222 | set_task_comm(tsk, "kthreadd"); |
| 223 | |
Oleg Nesterov | 10ab825 | 2007-05-09 02:34:37 -0700 | [diff] [blame] | 224 | ignore_signals(tsk); |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 225 | |
Oleg Nesterov | 10ab825 | 2007-05-09 02:34:37 -0700 | [diff] [blame] | 226 | set_user_nice(tsk, -5); |
| 227 | set_cpus_allowed(tsk, CPU_MASK_ALL); |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | int kthreadd(void *unused) |
| 231 | { |
| 232 | /* Setup a clean context for our children to inherit. */ |
| 233 | kthreadd_setup(); |
| 234 | |
| 235 | current->flags |= PF_NOFREEZE; |
| 236 | |
| 237 | for (;;) { |
| 238 | set_current_state(TASK_INTERRUPTIBLE); |
| 239 | if (list_empty(&kthread_create_list)) |
| 240 | schedule(); |
| 241 | __set_current_state(TASK_RUNNING); |
| 242 | |
| 243 | spin_lock(&kthread_create_lock); |
| 244 | while (!list_empty(&kthread_create_list)) { |
| 245 | struct kthread_create_info *create; |
| 246 | |
| 247 | create = list_entry(kthread_create_list.next, |
| 248 | struct kthread_create_info, list); |
| 249 | list_del_init(&create->list); |
| 250 | spin_unlock(&kthread_create_lock); |
| 251 | |
| 252 | create_kthread(create); |
| 253 | |
| 254 | spin_lock(&kthread_create_lock); |
| 255 | } |
| 256 | spin_unlock(&kthread_create_lock); |
| 257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
| 259 | return 0; |
| 260 | } |