kthread.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_stop - stop a thread created by kthread_create().
  133. * @k: thread created by kthread_create().
  134. *
  135. * Sets kthread_should_stop() for @k to return true, wakes it, and
  136. * waits for it to exit. This can also be called after kthread_create()
  137. * instead of calling wake_up_process(): the thread will exit without
  138. * calling threadfn().
  139. *
  140. * If threadfn() may call do_exit() itself, the caller must ensure
  141. * task_struct can't go away.
  142. *
  143. * Returns the result of threadfn(), or %-EINTR if wake_up_process()
  144. * was never called.
  145. */
  146. int kthread_stop(struct task_struct *k)
  147. {
  148. struct kthread *kthread;
  149. int ret;
  150. trace_sched_kthread_stop(k);
  151. get_task_struct(k);
  152. kthread = to_kthread(k);
  153. barrier(); /* it might have exited */
  154. if (k->vfork_done != NULL) {
  155. kthread->should_stop = 1;
  156. wake_up_process(k);
  157. wait_for_completion(&kthread->exited);
  158. }
  159. ret = k->exit_code;
  160. put_task_struct(k);
  161. trace_sched_kthread_stop_ret(ret);
  162. return ret;
  163. }
  164. EXPORT_SYMBOL(kthread_stop);
  165. int kthreadd(void *unused)
  166. {
  167. struct task_struct *tsk = current;
  168. /* Setup a clean context for our children to inherit. */
  169. set_task_comm(tsk, "kthreadd");
  170. ignore_signals(tsk);
  171. set_cpus_allowed_ptr(tsk, cpu_all_mask);
  172. set_mems_allowed(node_possible_map);
  173. current->flags |= PF_NOFREEZE | PF_FREEZER_NOSIG;
  174. for (;;) {
  175. set_current_state(TASK_INTERRUPTIBLE);
  176. if (list_empty(&kthread_create_list))
  177. schedule();
  178. __set_current_state(TASK_RUNNING);
  179. spin_lock(&kthread_create_lock);
  180. while (!list_empty(&kthread_create_list)) {
  181. struct kthread_create_info *create;
  182. create = list_entry(kthread_create_list.next,
  183. struct kthread_create_info, list);
  184. list_del_init(&create->list);
  185. spin_unlock(&kthread_create_lock);
  186. create_kthread(create);
  187. spin_lock(&kthread_create_lock);
  188. }
  189. spin_unlock(&kthread_create_lock);
  190. }
  191. return 0;
  192. }