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> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 15 | #include <linux/export.h> |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 16 | #include <linux/mutex.h> |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | #include <linux/freezer.h> |
Al Viro | a74fb73 | 2012-10-10 21:28:25 -0400 | [diff] [blame] | 19 | #include <linux/ptrace.h> |
Steven Rostedt | ad8d75f | 2009-04-14 19:39:12 -0400 | [diff] [blame] | 20 | #include <trace/events/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 22 | static DEFINE_SPINLOCK(kthread_create_lock); |
| 23 | static LIST_HEAD(kthread_create_list); |
| 24 | struct task_struct *kthreadd_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | struct kthread_create_info |
| 27 | { |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 28 | /* Information passed to kthread() from kthreadd. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | int (*threadfn)(void *data); |
| 30 | void *data; |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 31 | int node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 33 | /* Result passed back to kthread_create() from kthreadd. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | struct task_struct *result; |
| 35 | struct completion done; |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 36 | |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 37 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 40 | struct kthread { |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 41 | unsigned long flags; |
| 42 | unsigned int cpu; |
Tejun Heo | 82805ab | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 43 | void *data; |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 44 | struct completion parked; |
Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 45 | struct completion exited; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 48 | enum KTHREAD_BITS { |
| 49 | KTHREAD_IS_PER_CPU = 0, |
| 50 | KTHREAD_SHOULD_STOP, |
| 51 | KTHREAD_SHOULD_PARK, |
| 52 | KTHREAD_IS_PARKED, |
| 53 | }; |
| 54 | |
Oleg Nesterov | 4ecdafc | 2013-04-29 15:05:01 -0700 | [diff] [blame^] | 55 | #define __to_kthread(vfork) \ |
| 56 | container_of(vfork, struct kthread, exited) |
| 57 | |
| 58 | static inline struct kthread *to_kthread(struct task_struct *k) |
| 59 | { |
| 60 | return __to_kthread(k->vfork_done); |
| 61 | } |
| 62 | |
| 63 | static struct kthread *to_live_kthread(struct task_struct *k) |
| 64 | { |
| 65 | struct completion *vfork = ACCESS_ONCE(k->vfork_done); |
| 66 | if (likely(vfork)) |
| 67 | return __to_kthread(vfork); |
| 68 | return NULL; |
| 69 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 71 | /** |
| 72 | * kthread_should_stop - should this kthread return now? |
| 73 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 74 | * When someone calls kthread_stop() on your kthread, it will be woken |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 75 | * and this will return true. You should then return, and your return |
| 76 | * value will be passed through to kthread_stop(). |
| 77 | */ |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 78 | bool kthread_should_stop(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 80 | return test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | EXPORT_SYMBOL(kthread_should_stop); |
| 83 | |
Tejun Heo | 82805ab | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 84 | /** |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 85 | * kthread_should_park - should this kthread park now? |
| 86 | * |
| 87 | * When someone calls kthread_park() on your kthread, it will be woken |
| 88 | * and this will return true. You should then do the necessary |
| 89 | * cleanup and call kthread_parkme() |
| 90 | * |
| 91 | * Similar to kthread_should_stop(), but this keeps the thread alive |
| 92 | * and in a park position. kthread_unpark() "restarts" the thread and |
| 93 | * calls the thread function again. |
| 94 | */ |
| 95 | bool kthread_should_park(void) |
| 96 | { |
| 97 | return test_bit(KTHREAD_SHOULD_PARK, &to_kthread(current)->flags); |
| 98 | } |
| 99 | |
| 100 | /** |
Tejun Heo | 8a32c44 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 101 | * kthread_freezable_should_stop - should this freezable kthread return now? |
| 102 | * @was_frozen: optional out parameter, indicates whether %current was frozen |
| 103 | * |
| 104 | * kthread_should_stop() for freezable kthreads, which will enter |
| 105 | * refrigerator if necessary. This function is safe from kthread_stop() / |
| 106 | * freezer deadlock and freezable kthreads should use this function instead |
| 107 | * of calling try_to_freeze() directly. |
| 108 | */ |
| 109 | bool kthread_freezable_should_stop(bool *was_frozen) |
| 110 | { |
| 111 | bool frozen = false; |
| 112 | |
| 113 | might_sleep(); |
| 114 | |
| 115 | if (unlikely(freezing(current))) |
| 116 | frozen = __refrigerator(true); |
| 117 | |
| 118 | if (was_frozen) |
| 119 | *was_frozen = frozen; |
| 120 | |
| 121 | return kthread_should_stop(); |
| 122 | } |
| 123 | EXPORT_SYMBOL_GPL(kthread_freezable_should_stop); |
| 124 | |
| 125 | /** |
Tejun Heo | 82805ab | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 126 | * kthread_data - return data value specified on kthread creation |
| 127 | * @task: kthread task in question |
| 128 | * |
| 129 | * Return the data value specified when kthread @task was created. |
| 130 | * The caller is responsible for ensuring the validity of @task when |
| 131 | * calling this function. |
| 132 | */ |
| 133 | void *kthread_data(struct task_struct *task) |
| 134 | { |
| 135 | return to_kthread(task)->data; |
| 136 | } |
| 137 | |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 138 | static void __kthread_parkme(struct kthread *self) |
| 139 | { |
Thomas Gleixner | f2530dc | 2013-04-09 09:33:34 +0200 | [diff] [blame] | 140 | __set_current_state(TASK_PARKED); |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 141 | while (test_bit(KTHREAD_SHOULD_PARK, &self->flags)) { |
| 142 | if (!test_and_set_bit(KTHREAD_IS_PARKED, &self->flags)) |
| 143 | complete(&self->parked); |
| 144 | schedule(); |
Thomas Gleixner | f2530dc | 2013-04-09 09:33:34 +0200 | [diff] [blame] | 145 | __set_current_state(TASK_PARKED); |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 146 | } |
| 147 | clear_bit(KTHREAD_IS_PARKED, &self->flags); |
| 148 | __set_current_state(TASK_RUNNING); |
| 149 | } |
| 150 | |
| 151 | void kthread_parkme(void) |
| 152 | { |
| 153 | __kthread_parkme(to_kthread(current)); |
| 154 | } |
| 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | static int kthread(void *_create) |
| 157 | { |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 158 | /* Copy data: it's on kthread's stack */ |
Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 159 | struct kthread_create_info *create = _create; |
| 160 | int (*threadfn)(void *data) = create->threadfn; |
| 161 | void *data = create->data; |
| 162 | struct kthread self; |
| 163 | int ret; |
| 164 | |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 165 | self.flags = 0; |
Tejun Heo | 82805ab | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 166 | self.data = data; |
Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 167 | init_completion(&self.exited); |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 168 | init_completion(&self.parked); |
Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 169 | current->vfork_done = &self.exited; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | /* OK, tell user we're spawned, wait for stop or wakeup */ |
Oleg Nesterov | a076e4b | 2007-05-23 13:57:27 -0700 | [diff] [blame] | 172 | __set_current_state(TASK_UNINTERRUPTIBLE); |
Vitaliy Gusev | 3217ab9 | 2009-04-09 09:50:35 -0600 | [diff] [blame] | 173 | create->result = current; |
Oleg Nesterov | cdd140b | 2009-06-17 16:27:43 -0700 | [diff] [blame] | 174 | complete(&create->done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | schedule(); |
| 176 | |
Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 177 | ret = -EINTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 179 | if (!test_bit(KTHREAD_SHOULD_STOP, &self.flags)) { |
| 180 | __kthread_parkme(&self); |
| 181 | ret = threadfn(data); |
| 182 | } |
Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 183 | /* we can't just return, we must preserve "self" on stack */ |
| 184 | do_exit(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 187 | /* called from do_fork() to get node information for about to be created task */ |
| 188 | int tsk_fork_get_node(struct task_struct *tsk) |
| 189 | { |
| 190 | #ifdef CONFIG_NUMA |
| 191 | if (tsk == kthreadd_task) |
| 192 | return tsk->pref_node_fork; |
| 193 | #endif |
| 194 | return numa_node_id(); |
| 195 | } |
| 196 | |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 197 | static void create_kthread(struct kthread_create_info *create) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | int pid; |
| 200 | |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 201 | #ifdef CONFIG_NUMA |
| 202 | current->pref_node_fork = create->node; |
| 203 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | /* We want our own signal handler (we take no signals by default). */ |
| 205 | pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD); |
Oleg Nesterov | cdd140b | 2009-06-17 16:27:43 -0700 | [diff] [blame] | 206 | if (pid < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | create->result = ERR_PTR(pid); |
Oleg Nesterov | cdd140b | 2009-06-17 16:27:43 -0700 | [diff] [blame] | 208 | complete(&create->done); |
| 209 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 212 | /** |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 213 | * kthread_create_on_node - create a kthread. |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 214 | * @threadfn: the function to run until signal_pending(current). |
| 215 | * @data: data ptr for @threadfn. |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 216 | * @node: memory node number. |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 217 | * @namefmt: printf-style name for the thread. |
| 218 | * |
| 219 | * Description: This helper function creates and names a kernel |
| 220 | * thread. The thread will be stopped: use wake_up_process() to start |
Anton Blanchard | 301ba04 | 2010-02-09 15:07:40 +1100 | [diff] [blame] | 221 | * it. See also kthread_run(). |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 222 | * |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 223 | * If thread is going to be bound on a particular cpu, give its node |
| 224 | * in @node, to get NUMA affinity for kthread stack, or else give -1. |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 225 | * 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] | 226 | * argument. @threadfn() can either call do_exit() directly if it is a |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 227 | * standalone thread for which no one will call kthread_stop(), or |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 228 | * return when 'kthread_should_stop()' is true (which means |
| 229 | * kthread_stop() has been called). The return value should be zero |
| 230 | * or a negative error number; it will be passed to kthread_stop(). |
| 231 | * |
| 232 | * Returns a task_struct or ERR_PTR(-ENOMEM). |
| 233 | */ |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 234 | struct task_struct *kthread_create_on_node(int (*threadfn)(void *data), |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 235 | void *data, int node, |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 236 | const char namefmt[], |
| 237 | ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
| 239 | struct kthread_create_info create; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | create.threadfn = threadfn; |
| 242 | create.data = data; |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 243 | create.node = node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | init_completion(&create.done); |
| 245 | |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 246 | spin_lock(&kthread_create_lock); |
| 247 | list_add_tail(&create.list, &kthread_create_list); |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 248 | spin_unlock(&kthread_create_lock); |
| 249 | |
Dmitry Adamushko | cbd9b67 | 2008-04-29 00:59:23 -0700 | [diff] [blame] | 250 | wake_up_process(kthreadd_task); |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 251 | wait_for_completion(&create.done); |
| 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | if (!IS_ERR(create.result)) { |
Peter Zijlstra | c9b5f50 | 2011-01-07 13:41:40 +0100 | [diff] [blame] | 254 | static const struct sched_param param = { .sched_priority = 0 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | va_list args; |
Oleg Nesterov | 1c99315 | 2009-04-09 09:50:36 -0600 | [diff] [blame] | 256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | va_start(args, namefmt); |
| 258 | vsnprintf(create.result->comm, sizeof(create.result->comm), |
| 259 | namefmt, args); |
| 260 | va_end(args); |
Oleg Nesterov | 1c99315 | 2009-04-09 09:50:36 -0600 | [diff] [blame] | 261 | /* |
| 262 | * root may have changed our (kthreadd's) priority or CPU mask. |
| 263 | * The kernel thread should not inherit these properties. |
| 264 | */ |
| 265 | sched_setscheduler_nocheck(create.result, SCHED_NORMAL, ¶m); |
Oleg Nesterov | 1c99315 | 2009-04-09 09:50:36 -0600 | [diff] [blame] | 266 | set_cpus_allowed_ptr(create.result, cpu_all_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | return create.result; |
| 269 | } |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 270 | EXPORT_SYMBOL(kthread_create_on_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Thomas Gleixner | f2530dc | 2013-04-09 09:33:34 +0200 | [diff] [blame] | 272 | static void __kthread_bind(struct task_struct *p, unsigned int cpu, long state) |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 273 | { |
Thomas Gleixner | f2530dc | 2013-04-09 09:33:34 +0200 | [diff] [blame] | 274 | /* Must have done schedule() in kthread() before we set_task_cpu */ |
| 275 | if (!wait_task_inactive(p, state)) { |
| 276 | WARN_ON(1); |
| 277 | return; |
| 278 | } |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 279 | /* It's safe because the task is inactive. */ |
| 280 | do_set_cpus_allowed(p, cpumask_of(cpu)); |
| 281 | p->flags |= PF_THREAD_BOUND; |
| 282 | } |
| 283 | |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 284 | /** |
Peter Zijlstra | 881232b | 2009-12-16 18:04:39 +0100 | [diff] [blame] | 285 | * kthread_bind - bind a just-created kthread to a cpu. |
| 286 | * @p: thread created by kthread_create(). |
| 287 | * @cpu: cpu (might not be online, must be possible) for @k to run on. |
| 288 | * |
| 289 | * Description: This function is equivalent to set_cpus_allowed(), |
| 290 | * except that @cpu doesn't need to be online, and the thread must be |
| 291 | * stopped (i.e., just returned from kthread_create()). |
| 292 | */ |
| 293 | void kthread_bind(struct task_struct *p, unsigned int cpu) |
| 294 | { |
Thomas Gleixner | f2530dc | 2013-04-09 09:33:34 +0200 | [diff] [blame] | 295 | __kthread_bind(p, cpu, TASK_UNINTERRUPTIBLE); |
Peter Zijlstra | 881232b | 2009-12-16 18:04:39 +0100 | [diff] [blame] | 296 | } |
| 297 | EXPORT_SYMBOL(kthread_bind); |
| 298 | |
| 299 | /** |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 300 | * kthread_create_on_cpu - Create a cpu bound kthread |
| 301 | * @threadfn: the function to run until signal_pending(current). |
| 302 | * @data: data ptr for @threadfn. |
| 303 | * @cpu: The cpu on which the thread should be bound, |
| 304 | * @namefmt: printf-style name for the thread. Format is restricted |
| 305 | * to "name.*%u". Code fills in cpu number. |
| 306 | * |
| 307 | * Description: This helper function creates and names a kernel thread |
| 308 | * The thread will be woken and put into park mode. |
| 309 | */ |
| 310 | struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data), |
| 311 | void *data, unsigned int cpu, |
| 312 | const char *namefmt) |
| 313 | { |
| 314 | struct task_struct *p; |
| 315 | |
| 316 | p = kthread_create_on_node(threadfn, data, cpu_to_node(cpu), namefmt, |
| 317 | cpu); |
| 318 | if (IS_ERR(p)) |
| 319 | return p; |
| 320 | set_bit(KTHREAD_IS_PER_CPU, &to_kthread(p)->flags); |
| 321 | to_kthread(p)->cpu = cpu; |
| 322 | /* Park the thread to get it out of TASK_UNINTERRUPTIBLE state */ |
| 323 | kthread_park(p); |
| 324 | return p; |
| 325 | } |
| 326 | |
| 327 | static struct kthread *task_get_live_kthread(struct task_struct *k) |
| 328 | { |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 329 | get_task_struct(k); |
Oleg Nesterov | 4ecdafc | 2013-04-29 15:05:01 -0700 | [diff] [blame^] | 330 | return to_live_kthread(k); |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Thomas Gleixner | f2530dc | 2013-04-09 09:33:34 +0200 | [diff] [blame] | 333 | static void __kthread_unpark(struct task_struct *k, struct kthread *kthread) |
| 334 | { |
| 335 | clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags); |
| 336 | /* |
| 337 | * We clear the IS_PARKED bit here as we don't wait |
| 338 | * until the task has left the park code. So if we'd |
| 339 | * park before that happens we'd see the IS_PARKED bit |
| 340 | * which might be about to be cleared. |
| 341 | */ |
| 342 | if (test_and_clear_bit(KTHREAD_IS_PARKED, &kthread->flags)) { |
| 343 | if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags)) |
| 344 | __kthread_bind(k, kthread->cpu, TASK_PARKED); |
| 345 | wake_up_state(k, TASK_PARKED); |
| 346 | } |
| 347 | } |
| 348 | |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 349 | /** |
| 350 | * kthread_unpark - unpark a thread created by kthread_create(). |
| 351 | * @k: thread created by kthread_create(). |
| 352 | * |
| 353 | * Sets kthread_should_park() for @k to return false, wakes it, and |
| 354 | * waits for it to return. If the thread is marked percpu then its |
| 355 | * bound to the cpu again. |
| 356 | */ |
| 357 | void kthread_unpark(struct task_struct *k) |
| 358 | { |
| 359 | struct kthread *kthread = task_get_live_kthread(k); |
| 360 | |
Thomas Gleixner | f2530dc | 2013-04-09 09:33:34 +0200 | [diff] [blame] | 361 | if (kthread) |
| 362 | __kthread_unpark(k, kthread); |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 363 | put_task_struct(k); |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * kthread_park - park a thread created by kthread_create(). |
| 368 | * @k: thread created by kthread_create(). |
| 369 | * |
| 370 | * Sets kthread_should_park() for @k to return true, wakes it, and |
| 371 | * waits for it to return. This can also be called after kthread_create() |
| 372 | * instead of calling wake_up_process(): the thread will park without |
| 373 | * calling threadfn(). |
| 374 | * |
| 375 | * Returns 0 if the thread is parked, -ENOSYS if the thread exited. |
| 376 | * If called by the kthread itself just the park bit is set. |
| 377 | */ |
| 378 | int kthread_park(struct task_struct *k) |
| 379 | { |
| 380 | struct kthread *kthread = task_get_live_kthread(k); |
| 381 | int ret = -ENOSYS; |
| 382 | |
| 383 | if (kthread) { |
| 384 | if (!test_bit(KTHREAD_IS_PARKED, &kthread->flags)) { |
| 385 | set_bit(KTHREAD_SHOULD_PARK, &kthread->flags); |
| 386 | if (k != current) { |
| 387 | wake_up_process(k); |
| 388 | wait_for_completion(&kthread->parked); |
| 389 | } |
| 390 | } |
| 391 | ret = 0; |
| 392 | } |
| 393 | put_task_struct(k); |
| 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | /** |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 398 | * kthread_stop - stop a thread created by kthread_create(). |
| 399 | * @k: thread created by kthread_create(). |
| 400 | * |
| 401 | * Sets kthread_should_stop() for @k to return true, wakes it, and |
Oleg Nesterov | 9ae2602 | 2009-06-19 02:51:13 +0200 | [diff] [blame] | 402 | * waits for it to exit. This can also be called after kthread_create() |
| 403 | * instead of calling wake_up_process(): the thread will exit without |
| 404 | * calling threadfn(). |
| 405 | * |
| 406 | * If threadfn() may call do_exit() itself, the caller must ensure |
| 407 | * task_struct can't go away. |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 408 | * |
| 409 | * Returns the result of threadfn(), or %-EINTR if wake_up_process() |
| 410 | * was never called. |
| 411 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | int kthread_stop(struct task_struct *k) |
| 413 | { |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 414 | struct kthread *kthread = task_get_live_kthread(k); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | int ret; |
| 416 | |
Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 417 | trace_sched_kthread_stop(k); |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 418 | if (kthread) { |
| 419 | set_bit(KTHREAD_SHOULD_STOP, &kthread->flags); |
Thomas Gleixner | f2530dc | 2013-04-09 09:33:34 +0200 | [diff] [blame] | 420 | __kthread_unpark(k, kthread); |
Oleg Nesterov | 6370617 | 2009-06-17 16:27:45 -0700 | [diff] [blame] | 421 | wake_up_process(k); |
| 422 | wait_for_completion(&kthread->exited); |
| 423 | } |
| 424 | ret = k->exit_code; |
Mathieu Desnoyers | 0a16b60 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | put_task_struct(k); |
Mathieu Desnoyers | 0a16b60 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 427 | trace_sched_kthread_stop_ret(ret); |
| 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | return ret; |
| 430 | } |
Adrian Bunk | 52e92e5 | 2006-07-14 00:24:05 -0700 | [diff] [blame] | 431 | EXPORT_SYMBOL(kthread_stop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
Satyam Sharma | e804a4a | 2007-07-31 00:39:16 -0700 | [diff] [blame] | 433 | int kthreadd(void *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | { |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 435 | struct task_struct *tsk = current; |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 436 | |
Satyam Sharma | e804a4a | 2007-07-31 00:39:16 -0700 | [diff] [blame] | 437 | /* Setup a clean context for our children to inherit. */ |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 438 | set_task_comm(tsk, "kthreadd"); |
Oleg Nesterov | 10ab825 | 2007-05-09 02:34:37 -0700 | [diff] [blame] | 439 | ignore_signals(tsk); |
Rusty Russell | 1a2142a | 2009-03-30 22:05:10 -0600 | [diff] [blame] | 440 | set_cpus_allowed_ptr(tsk, cpu_all_mask); |
Lai Jiangshan | aee4faa | 2012-12-12 13:51:39 -0800 | [diff] [blame] | 441 | set_mems_allowed(node_states[N_MEMORY]); |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 442 | |
Tejun Heo | 34b087e | 2011-11-23 09:28:17 -0800 | [diff] [blame] | 443 | current->flags |= PF_NOFREEZE; |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 444 | |
| 445 | for (;;) { |
| 446 | set_current_state(TASK_INTERRUPTIBLE); |
| 447 | if (list_empty(&kthread_create_list)) |
| 448 | schedule(); |
| 449 | __set_current_state(TASK_RUNNING); |
| 450 | |
| 451 | spin_lock(&kthread_create_lock); |
| 452 | while (!list_empty(&kthread_create_list)) { |
| 453 | struct kthread_create_info *create; |
| 454 | |
| 455 | create = list_entry(kthread_create_list.next, |
| 456 | struct kthread_create_info, list); |
| 457 | list_del_init(&create->list); |
| 458 | spin_unlock(&kthread_create_lock); |
| 459 | |
| 460 | create_kthread(create); |
| 461 | |
| 462 | spin_lock(&kthread_create_lock); |
| 463 | } |
| 464 | spin_unlock(&kthread_create_lock); |
| 465 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
| 467 | return 0; |
| 468 | } |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 469 | |
Yong Zhang | 4f32e9b | 2010-12-22 10:27:53 +0100 | [diff] [blame] | 470 | void __init_kthread_worker(struct kthread_worker *worker, |
| 471 | const char *name, |
| 472 | struct lock_class_key *key) |
| 473 | { |
| 474 | spin_lock_init(&worker->lock); |
| 475 | lockdep_set_class_and_name(&worker->lock, key, name); |
| 476 | INIT_LIST_HEAD(&worker->work_list); |
| 477 | worker->task = NULL; |
| 478 | } |
| 479 | EXPORT_SYMBOL_GPL(__init_kthread_worker); |
| 480 | |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 481 | /** |
| 482 | * kthread_worker_fn - kthread function to process kthread_worker |
| 483 | * @worker_ptr: pointer to initialized kthread_worker |
| 484 | * |
| 485 | * This function can be used as @threadfn to kthread_create() or |
| 486 | * kthread_run() with @worker_ptr argument pointing to an initialized |
| 487 | * kthread_worker. The started kthread will process work_list until |
| 488 | * the it is stopped with kthread_stop(). A kthread can also call |
| 489 | * this function directly after extra initialization. |
| 490 | * |
| 491 | * Different kthreads can be used for the same kthread_worker as long |
| 492 | * as there's only one kthread attached to it at any given time. A |
| 493 | * kthread_worker without an attached kthread simply collects queued |
| 494 | * kthread_works. |
| 495 | */ |
| 496 | int kthread_worker_fn(void *worker_ptr) |
| 497 | { |
| 498 | struct kthread_worker *worker = worker_ptr; |
| 499 | struct kthread_work *work; |
| 500 | |
| 501 | WARN_ON(worker->task); |
| 502 | worker->task = current; |
| 503 | repeat: |
| 504 | set_current_state(TASK_INTERRUPTIBLE); /* mb paired w/ kthread_stop */ |
| 505 | |
| 506 | if (kthread_should_stop()) { |
| 507 | __set_current_state(TASK_RUNNING); |
| 508 | spin_lock_irq(&worker->lock); |
| 509 | worker->task = NULL; |
| 510 | spin_unlock_irq(&worker->lock); |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | work = NULL; |
| 515 | spin_lock_irq(&worker->lock); |
| 516 | if (!list_empty(&worker->work_list)) { |
| 517 | work = list_first_entry(&worker->work_list, |
| 518 | struct kthread_work, node); |
| 519 | list_del_init(&work->node); |
| 520 | } |
Tejun Heo | 46f3d97 | 2012-07-19 13:52:53 -0700 | [diff] [blame] | 521 | worker->current_work = work; |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 522 | spin_unlock_irq(&worker->lock); |
| 523 | |
| 524 | if (work) { |
| 525 | __set_current_state(TASK_RUNNING); |
| 526 | work->func(work); |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 527 | } else if (!freezing(current)) |
| 528 | schedule(); |
| 529 | |
| 530 | try_to_freeze(); |
| 531 | goto repeat; |
| 532 | } |
| 533 | EXPORT_SYMBOL_GPL(kthread_worker_fn); |
| 534 | |
Tejun Heo | 9a2e03d | 2012-07-19 13:52:53 -0700 | [diff] [blame] | 535 | /* insert @work before @pos in @worker */ |
| 536 | static void insert_kthread_work(struct kthread_worker *worker, |
| 537 | struct kthread_work *work, |
| 538 | struct list_head *pos) |
| 539 | { |
| 540 | lockdep_assert_held(&worker->lock); |
| 541 | |
| 542 | list_add_tail(&work->node, pos); |
Tejun Heo | 46f3d97 | 2012-07-19 13:52:53 -0700 | [diff] [blame] | 543 | work->worker = worker; |
Tejun Heo | 9a2e03d | 2012-07-19 13:52:53 -0700 | [diff] [blame] | 544 | if (likely(worker->task)) |
| 545 | wake_up_process(worker->task); |
| 546 | } |
| 547 | |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 548 | /** |
| 549 | * queue_kthread_work - queue a kthread_work |
| 550 | * @worker: target kthread_worker |
| 551 | * @work: kthread_work to queue |
| 552 | * |
| 553 | * Queue @work to work processor @task for async execution. @task |
| 554 | * must have been created with kthread_worker_create(). Returns %true |
| 555 | * if @work was successfully queued, %false if it was already pending. |
| 556 | */ |
| 557 | bool queue_kthread_work(struct kthread_worker *worker, |
| 558 | struct kthread_work *work) |
| 559 | { |
| 560 | bool ret = false; |
| 561 | unsigned long flags; |
| 562 | |
| 563 | spin_lock_irqsave(&worker->lock, flags); |
| 564 | if (list_empty(&work->node)) { |
Tejun Heo | 9a2e03d | 2012-07-19 13:52:53 -0700 | [diff] [blame] | 565 | insert_kthread_work(worker, work, &worker->work_list); |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 566 | ret = true; |
| 567 | } |
| 568 | spin_unlock_irqrestore(&worker->lock, flags); |
| 569 | return ret; |
| 570 | } |
| 571 | EXPORT_SYMBOL_GPL(queue_kthread_work); |
| 572 | |
Tejun Heo | 9a2e03d | 2012-07-19 13:52:53 -0700 | [diff] [blame] | 573 | struct kthread_flush_work { |
| 574 | struct kthread_work work; |
| 575 | struct completion done; |
| 576 | }; |
| 577 | |
| 578 | static void kthread_flush_work_fn(struct kthread_work *work) |
| 579 | { |
| 580 | struct kthread_flush_work *fwork = |
| 581 | container_of(work, struct kthread_flush_work, work); |
| 582 | complete(&fwork->done); |
| 583 | } |
| 584 | |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 585 | /** |
| 586 | * flush_kthread_work - flush a kthread_work |
| 587 | * @work: work to flush |
| 588 | * |
| 589 | * If @work is queued or executing, wait for it to finish execution. |
| 590 | */ |
| 591 | void flush_kthread_work(struct kthread_work *work) |
| 592 | { |
Tejun Heo | 46f3d97 | 2012-07-19 13:52:53 -0700 | [diff] [blame] | 593 | struct kthread_flush_work fwork = { |
| 594 | KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn), |
| 595 | COMPLETION_INITIALIZER_ONSTACK(fwork.done), |
| 596 | }; |
| 597 | struct kthread_worker *worker; |
| 598 | bool noop = false; |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 599 | |
Tejun Heo | 46f3d97 | 2012-07-19 13:52:53 -0700 | [diff] [blame] | 600 | retry: |
| 601 | worker = work->worker; |
| 602 | if (!worker) |
| 603 | return; |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 604 | |
Tejun Heo | 46f3d97 | 2012-07-19 13:52:53 -0700 | [diff] [blame] | 605 | spin_lock_irq(&worker->lock); |
| 606 | if (work->worker != worker) { |
| 607 | spin_unlock_irq(&worker->lock); |
| 608 | goto retry; |
| 609 | } |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 610 | |
Tejun Heo | 46f3d97 | 2012-07-19 13:52:53 -0700 | [diff] [blame] | 611 | if (!list_empty(&work->node)) |
| 612 | insert_kthread_work(worker, &fwork.work, work->node.next); |
| 613 | else if (worker->current_work == work) |
| 614 | insert_kthread_work(worker, &fwork.work, worker->work_list.next); |
| 615 | else |
| 616 | noop = true; |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 617 | |
Tejun Heo | 46f3d97 | 2012-07-19 13:52:53 -0700 | [diff] [blame] | 618 | spin_unlock_irq(&worker->lock); |
| 619 | |
| 620 | if (!noop) |
| 621 | wait_for_completion(&fwork.done); |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 622 | } |
| 623 | EXPORT_SYMBOL_GPL(flush_kthread_work); |
| 624 | |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 625 | /** |
| 626 | * flush_kthread_worker - flush all current works on a kthread_worker |
| 627 | * @worker: worker to flush |
| 628 | * |
| 629 | * Wait until all currently executing or pending works on @worker are |
| 630 | * finished. |
| 631 | */ |
| 632 | void flush_kthread_worker(struct kthread_worker *worker) |
| 633 | { |
| 634 | struct kthread_flush_work fwork = { |
| 635 | KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn), |
| 636 | COMPLETION_INITIALIZER_ONSTACK(fwork.done), |
| 637 | }; |
| 638 | |
| 639 | queue_kthread_work(worker, &fwork.work); |
| 640 | wait_for_completion(&fwork.done); |
| 641 | } |
| 642 | EXPORT_SYMBOL_GPL(flush_kthread_worker); |