kthread.c 7.3 KB

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