kthread.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. create->result = current;
  68. complete(&create->started);
  69. schedule();
  70. if (!kthread_should_stop())
  71. ret = threadfn(data);
  72. /* It might have exited on its own, w/o kthread_stop. Check. */
  73. if (kthread_should_stop()) {
  74. kthread_stop_info.err = ret;
  75. complete(&kthread_stop_info.done);
  76. }
  77. return 0;
  78. }
  79. static void create_kthread(struct kthread_create_info *create)
  80. {
  81. int pid;
  82. /* We want our own signal handler (we take no signals by default). */
  83. pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
  84. if (pid < 0)
  85. create->result = ERR_PTR(pid);
  86. else
  87. wait_for_completion(&create->started);
  88. complete(&create->done);
  89. }
  90. /**
  91. * kthread_create - create a kthread.
  92. * @threadfn: the function to run until signal_pending(current).
  93. * @data: data ptr for @threadfn.
  94. * @namefmt: printf-style name for the thread.
  95. *
  96. * Description: This helper function creates and names a kernel
  97. * thread. The thread will be stopped: use wake_up_process() to start
  98. * it. See also kthread_run(), kthread_create_on_cpu().
  99. *
  100. * When woken, the thread will run @threadfn() with @data as its
  101. * argument. @threadfn() can either call do_exit() directly if it is a
  102. * standalone thread for which noone will call kthread_stop(), or
  103. * return when 'kthread_should_stop()' is true (which means
  104. * kthread_stop() has been called). The return value should be zero
  105. * or a negative error number; it will be passed to kthread_stop().
  106. *
  107. * Returns a task_struct or ERR_PTR(-ENOMEM).
  108. */
  109. struct task_struct *kthread_create(int (*threadfn)(void *data),
  110. void *data,
  111. const char namefmt[],
  112. ...)
  113. {
  114. struct kthread_create_info create;
  115. create.threadfn = threadfn;
  116. create.data = data;
  117. init_completion(&create.started);
  118. init_completion(&create.done);
  119. spin_lock(&kthread_create_lock);
  120. list_add_tail(&create.list, &kthread_create_list);
  121. spin_unlock(&kthread_create_lock);
  122. wake_up_process(kthreadd_task);
  123. wait_for_completion(&create.done);
  124. if (!IS_ERR(create.result)) {
  125. struct sched_param param = { .sched_priority = 0 };
  126. va_list args;
  127. va_start(args, namefmt);
  128. vsnprintf(create.result->comm, sizeof(create.result->comm),
  129. namefmt, args);
  130. va_end(args);
  131. /*
  132. * root may have changed our (kthreadd's) priority or CPU mask.
  133. * The kernel thread should not inherit these properties.
  134. */
  135. sched_setscheduler_nocheck(create.result, SCHED_NORMAL, &param);
  136. set_user_nice(create.result, KTHREAD_NICE_LEVEL);
  137. set_cpus_allowed_ptr(create.result, cpu_all_mask);
  138. }
  139. return create.result;
  140. }
  141. EXPORT_SYMBOL(kthread_create);
  142. /**
  143. * kthread_bind - bind a just-created kthread to a cpu.
  144. * @k: thread created by kthread_create().
  145. * @cpu: cpu (might not be online, must be possible) for @k to run on.
  146. *
  147. * Description: This function is equivalent to set_cpus_allowed(),
  148. * except that @cpu doesn't need to be online, and the thread must be
  149. * stopped (i.e., just returned from kthread_create()).
  150. */
  151. void kthread_bind(struct task_struct *k, unsigned int cpu)
  152. {
  153. /* Must have done schedule() in kthread() before we set_task_cpu */
  154. if (!wait_task_inactive(k, TASK_UNINTERRUPTIBLE)) {
  155. WARN_ON(1);
  156. return;
  157. }
  158. set_task_cpu(k, cpu);
  159. k->cpus_allowed = cpumask_of_cpu(cpu);
  160. k->rt.nr_cpus_allowed = 1;
  161. k->flags |= PF_THREAD_BOUND;
  162. }
  163. EXPORT_SYMBOL(kthread_bind);
  164. /**
  165. * kthread_stop - stop a thread created by kthread_create().
  166. * @k: thread created by kthread_create().
  167. *
  168. * Sets kthread_should_stop() for @k to return true, wakes it, and
  169. * waits for it to exit. Your threadfn() must not call do_exit()
  170. * itself if you use this function! This can also be called after
  171. * kthread_create() instead of calling wake_up_process(): the thread
  172. * will exit without calling threadfn().
  173. *
  174. * Returns the result of threadfn(), or %-EINTR if wake_up_process()
  175. * was never called.
  176. */
  177. int kthread_stop(struct task_struct *k)
  178. {
  179. int ret;
  180. mutex_lock(&kthread_stop_lock);
  181. /* It could exit after stop_info.k set, but before wake_up_process. */
  182. get_task_struct(k);
  183. trace_sched_kthread_stop(k);
  184. /* Must init completion *before* thread sees kthread_stop_info.k */
  185. init_completion(&kthread_stop_info.done);
  186. smp_wmb();
  187. /* Now set kthread_should_stop() to true, and wake it up. */
  188. kthread_stop_info.k = k;
  189. wake_up_process(k);
  190. put_task_struct(k);
  191. /* Once it dies, reset stop ptr, gather result and we're done. */
  192. wait_for_completion(&kthread_stop_info.done);
  193. kthread_stop_info.k = NULL;
  194. ret = kthread_stop_info.err;
  195. mutex_unlock(&kthread_stop_lock);
  196. trace_sched_kthread_stop_ret(ret);
  197. return ret;
  198. }
  199. EXPORT_SYMBOL(kthread_stop);
  200. int kthreadd(void *unused)
  201. {
  202. struct task_struct *tsk = current;
  203. /* Setup a clean context for our children to inherit. */
  204. set_task_comm(tsk, "kthreadd");
  205. ignore_signals(tsk);
  206. set_user_nice(tsk, KTHREAD_NICE_LEVEL);
  207. set_cpus_allowed_ptr(tsk, cpu_all_mask);
  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. }