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