kthread.c 6.7 KB

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