kthread.c 6.6 KB

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