kthread.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* Kernel thread helper functions.
  2. * Copyright (C) 2004 IBM Corporation, Rusty Russell.
  3. *
  4. * Creation is done via kthreadd, 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>
  15. #include <linux/mutex.h>
  16. #include <trace/sched.h>
  17. #define KTHREAD_NICE_LEVEL (-5)
  18. static DEFINE_SPINLOCK(kthread_create_lock);
  19. static LIST_HEAD(kthread_create_list);
  20. struct task_struct *kthreadd_task;
  21. DEFINE_TRACE(sched_kthread_stop);
  22. DEFINE_TRACE(sched_kthread_stop_ret);
  23. struct kthread_create_info
  24. {
  25. /* Information passed to kthread() from kthreadd. */
  26. int (*threadfn)(void *data);
  27. void *data;
  28. struct completion started;
  29. /* Result passed back to kthread_create() from kthreadd. */
  30. struct task_struct *result;
  31. struct completion done;
  32. struct list_head list;
  33. };
  34. struct kthread_stop_info
  35. {
  36. struct task_struct *k;
  37. int err;
  38. struct completion done;
  39. };
  40. /* Thread stopping is done by setthing this var: lock serializes
  41. * multiple kthread_stop calls. */
  42. static DEFINE_MUTEX(kthread_stop_lock);
  43. static struct kthread_stop_info kthread_stop_info;
  44. /**
  45. * kthread_should_stop - should this kthread return now?
  46. *
  47. * When someone calls kthread_stop() on your kthread, it will be woken
  48. * and this will return true. You should then return, and your return
  49. * value will be passed through to kthread_stop().
  50. */
  51. int kthread_should_stop(void)
  52. {
  53. return (kthread_stop_info.k == current);
  54. }
  55. EXPORT_SYMBOL(kthread_should_stop);
  56. static int kthread(void *_create)
  57. {
  58. struct kthread_create_info *create = _create;
  59. int (*threadfn)(void *data);
  60. void *data;
  61. int ret = -EINTR;
  62. /* Copy data: it's on kthread's stack */
  63. threadfn = create->threadfn;
  64. data = create->data;
  65. /* OK, tell user we're spawned, wait for stop or wakeup */
  66. __set_current_state(TASK_UNINTERRUPTIBLE);
  67. complete(&create->started);
  68. schedule();
  69. if (!kthread_should_stop())
  70. ret = threadfn(data);
  71. /* It might have exited on its own, w/o kthread_stop. Check. */
  72. if (kthread_should_stop()) {
  73. kthread_stop_info.err = ret;
  74. complete(&kthread_stop_info.done);
  75. }
  76. return 0;
  77. }
  78. static void create_kthread(struct kthread_create_info *create)
  79. {
  80. int pid;
  81. /* We want our own signal handler (we take no signals by default). */
  82. pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
  83. if (pid < 0) {
  84. create->result = ERR_PTR(pid);
  85. } else {
  86. struct sched_param param = { .sched_priority = 0 };
  87. wait_for_completion(&create->started);
  88. read_lock(&tasklist_lock);
  89. create->result = find_task_by_pid_ns(pid, &init_pid_ns);
  90. read_unlock(&tasklist_lock);
  91. /*
  92. * root may have changed our (kthreadd's) priority or CPU mask.
  93. * The kernel thread should not inherit these properties.
  94. */
  95. sched_setscheduler(create->result, SCHED_NORMAL, &param);
  96. set_user_nice(create->result, KTHREAD_NICE_LEVEL);
  97. set_cpus_allowed_ptr(create->result, cpu_all_mask);
  98. }
  99. complete(&create->done);
  100. }
  101. /**
  102. * kthread_create - create a kthread.
  103. * @threadfn: the function to run until signal_pending(current).
  104. * @data: data ptr for @threadfn.
  105. * @namefmt: printf-style name for the thread.
  106. *
  107. * Description: This helper function creates and names a kernel
  108. * thread. The thread will be stopped: use wake_up_process() to start
  109. * it. See also kthread_run(), kthread_create_on_cpu().
  110. *
  111. * When woken, the thread will run @threadfn() with @data as its
  112. * argument. @threadfn() can either call do_exit() directly if it is a
  113. * standalone thread for which noone will call kthread_stop(), or
  114. * return when 'kthread_should_stop()' is true (which means
  115. * kthread_stop() has been called). The return value should be zero
  116. * or a negative error number; it will be passed to kthread_stop().
  117. *
  118. * Returns a task_struct or ERR_PTR(-ENOMEM).
  119. */
  120. struct task_struct *kthread_create(int (*threadfn)(void *data),
  121. void *data,
  122. const char namefmt[],
  123. ...)
  124. {
  125. struct kthread_create_info create;
  126. create.threadfn = threadfn;
  127. create.data = data;
  128. init_completion(&create.started);
  129. init_completion(&create.done);
  130. spin_lock(&kthread_create_lock);
  131. list_add_tail(&create.list, &kthread_create_list);
  132. spin_unlock(&kthread_create_lock);
  133. wake_up_process(kthreadd_task);
  134. wait_for_completion(&create.done);
  135. if (!IS_ERR(create.result)) {
  136. va_list args;
  137. va_start(args, namefmt);
  138. vsnprintf(create.result->comm, sizeof(create.result->comm),
  139. namefmt, args);
  140. va_end(args);
  141. }
  142. return create.result;
  143. }
  144. EXPORT_SYMBOL(kthread_create);
  145. /**
  146. * kthread_bind - bind a just-created kthread to a cpu.
  147. * @k: thread created by kthread_create().
  148. * @cpu: cpu (might not be online, must be possible) for @k to run on.
  149. *
  150. * Description: This function is equivalent to set_cpus_allowed(),
  151. * except that @cpu doesn't need to be online, and the thread must be
  152. * stopped (i.e., just returned from kthread_create()).
  153. */
  154. void kthread_bind(struct task_struct *k, unsigned int cpu)
  155. {
  156. /* Must have done schedule() in kthread() before we set_task_cpu */
  157. if (!wait_task_inactive(k, TASK_UNINTERRUPTIBLE)) {
  158. WARN_ON(1);
  159. return;
  160. }
  161. set_task_cpu(k, cpu);
  162. k->cpus_allowed = cpumask_of_cpu(cpu);
  163. k->rt.nr_cpus_allowed = 1;
  164. k->flags |= PF_THREAD_BOUND;
  165. }
  166. EXPORT_SYMBOL(kthread_bind);
  167. /**
  168. * kthread_stop - stop a thread created by kthread_create().
  169. * @k: thread created by kthread_create().
  170. *
  171. * Sets kthread_should_stop() for @k to return true, wakes it, and
  172. * waits for it to exit. Your threadfn() must not call do_exit()
  173. * itself if you use this function! This can also be called after
  174. * kthread_create() instead of calling wake_up_process(): the thread
  175. * will exit without calling threadfn().
  176. *
  177. * Returns the result of threadfn(), or %-EINTR if wake_up_process()
  178. * was never called.
  179. */
  180. int kthread_stop(struct task_struct *k)
  181. {
  182. int ret;
  183. mutex_lock(&kthread_stop_lock);
  184. /* It could exit after stop_info.k set, but before wake_up_process. */
  185. get_task_struct(k);
  186. trace_sched_kthread_stop(k);
  187. /* Must init completion *before* thread sees kthread_stop_info.k */
  188. init_completion(&kthread_stop_info.done);
  189. smp_wmb();
  190. /* Now set kthread_should_stop() to true, and wake it up. */
  191. kthread_stop_info.k = k;
  192. wake_up_process(k);
  193. put_task_struct(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);
  199. trace_sched_kthread_stop_ret(ret);
  200. return ret;
  201. }
  202. EXPORT_SYMBOL(kthread_stop);
  203. int kthreadd(void *unused)
  204. {
  205. struct task_struct *tsk = current;
  206. /* Setup a clean context for our children to inherit. */
  207. set_task_comm(tsk, "kthreadd");
  208. ignore_signals(tsk);
  209. set_user_nice(tsk, KTHREAD_NICE_LEVEL);
  210. set_cpus_allowed_ptr(tsk, cpu_all_mask);
  211. current->flags |= PF_NOFREEZE | PF_FREEZER_NOSIG;
  212. for (;;) {
  213. set_current_state(TASK_INTERRUPTIBLE);
  214. if (list_empty(&kthread_create_list))
  215. schedule();
  216. __set_current_state(TASK_RUNNING);
  217. spin_lock(&kthread_create_lock);
  218. while (!list_empty(&kthread_create_list)) {
  219. struct kthread_create_info *create;
  220. create = list_entry(kthread_create_list.next,
  221. struct kthread_create_info, list);
  222. list_del_init(&create->list);
  223. spin_unlock(&kthread_create_lock);
  224. create_kthread(create);
  225. spin_lock(&kthread_create_lock);
  226. }
  227. spin_unlock(&kthread_create_lock);
  228. }
  229. return 0;
  230. }