kthread.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. #define KTHREAD_NICE_LEVEL (-5)
  17. static DEFINE_SPINLOCK(kthread_create_lock);
  18. static LIST_HEAD(kthread_create_list);
  19. struct task_struct *kthreadd_task;
  20. struct kthread_create_info
  21. {
  22. /* Information passed to kthread() from kthreadd. */
  23. int (*threadfn)(void *data);
  24. void *data;
  25. struct completion started;
  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_stop_info
  32. {
  33. struct task_struct *k;
  34. int err;
  35. struct completion done;
  36. };
  37. /* Thread stopping is done by setthing this var: lock serializes
  38. * multiple kthread_stop calls. */
  39. static DEFINE_MUTEX(kthread_stop_lock);
  40. static struct kthread_stop_info kthread_stop_info;
  41. /**
  42. * kthread_should_stop - should this kthread return now?
  43. *
  44. * When someone calls kthread_stop() on your kthread, it will be woken
  45. * and this will return true. You should then return, and your return
  46. * value will be passed through to kthread_stop().
  47. */
  48. int kthread_should_stop(void)
  49. {
  50. return (kthread_stop_info.k == current);
  51. }
  52. EXPORT_SYMBOL(kthread_should_stop);
  53. static int kthread(void *_create)
  54. {
  55. struct kthread_create_info *create = _create;
  56. int (*threadfn)(void *data);
  57. void *data;
  58. int ret = -EINTR;
  59. /* Copy data: it's on kthread's stack */
  60. threadfn = create->threadfn;
  61. data = create->data;
  62. /* OK, tell user we're spawned, wait for stop or wakeup */
  63. __set_current_state(TASK_UNINTERRUPTIBLE);
  64. complete(&create->started);
  65. schedule();
  66. if (!kthread_should_stop())
  67. ret = threadfn(data);
  68. /* It might have exited on its own, w/o kthread_stop. Check. */
  69. if (kthread_should_stop()) {
  70. kthread_stop_info.err = ret;
  71. complete(&kthread_stop_info.done);
  72. }
  73. return 0;
  74. }
  75. static void create_kthread(struct kthread_create_info *create)
  76. {
  77. int pid;
  78. /* We want our own signal handler (we take no signals by default). */
  79. pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
  80. if (pid < 0) {
  81. create->result = ERR_PTR(pid);
  82. } else {
  83. struct sched_param param = { .sched_priority = 0 };
  84. wait_for_completion(&create->started);
  85. read_lock(&tasklist_lock);
  86. create->result = find_task_by_pid(pid);
  87. read_unlock(&tasklist_lock);
  88. /*
  89. * root may have changed our (kthreadd's) priority or CPU mask.
  90. * The kernel thread should not inherit these properties.
  91. */
  92. sched_setscheduler(create->result, SCHED_NORMAL, &param);
  93. set_user_nice(create->result, KTHREAD_NICE_LEVEL);
  94. set_cpus_allowed(create->result, CPU_MASK_ALL);
  95. }
  96. complete(&create->done);
  97. }
  98. /**
  99. * kthread_create - create a kthread.
  100. * @threadfn: the function to run until signal_pending(current).
  101. * @data: data ptr for @threadfn.
  102. * @namefmt: printf-style name for the thread.
  103. *
  104. * Description: This helper function creates and names a kernel
  105. * thread. The thread will be stopped: use wake_up_process() to start
  106. * it. See also kthread_run(), kthread_create_on_cpu().
  107. *
  108. * When woken, the thread will run @threadfn() with @data as its
  109. * argument. @threadfn() can either call do_exit() directly if it is a
  110. * standalone thread for which noone will call kthread_stop(), or
  111. * return when 'kthread_should_stop()' is true (which means
  112. * kthread_stop() has been called). The return value should be zero
  113. * or a negative error number; it will be passed to kthread_stop().
  114. *
  115. * Returns a task_struct or ERR_PTR(-ENOMEM).
  116. */
  117. struct task_struct *kthread_create(int (*threadfn)(void *data),
  118. void *data,
  119. const char namefmt[],
  120. ...)
  121. {
  122. struct kthread_create_info create;
  123. create.threadfn = threadfn;
  124. create.data = data;
  125. init_completion(&create.started);
  126. init_completion(&create.done);
  127. spin_lock(&kthread_create_lock);
  128. list_add_tail(&create.list, &kthread_create_list);
  129. spin_unlock(&kthread_create_lock);
  130. wake_up_process(kthreadd_task);
  131. wait_for_completion(&create.done);
  132. if (!IS_ERR(create.result)) {
  133. va_list args;
  134. va_start(args, namefmt);
  135. vsnprintf(create.result->comm, sizeof(create.result->comm),
  136. namefmt, args);
  137. va_end(args);
  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. if (k->state != TASK_UNINTERRUPTIBLE) {
  154. WARN_ON(1);
  155. return;
  156. }
  157. /* Must have done schedule() in kthread() before we set_task_cpu */
  158. wait_task_inactive(k);
  159. set_task_cpu(k, cpu);
  160. k->cpus_allowed = cpumask_of_cpu(cpu);
  161. k->rt.nr_cpus_allowed = 1;
  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. /* Must init completion *before* thread sees kthread_stop_info.k */
  184. init_completion(&kthread_stop_info.done);
  185. smp_wmb();
  186. /* Now set kthread_should_stop() to true, and wake it up. */
  187. kthread_stop_info.k = k;
  188. wake_up_process(k);
  189. put_task_struct(k);
  190. /* Once it dies, reset stop ptr, gather result and we're done. */
  191. wait_for_completion(&kthread_stop_info.done);
  192. kthread_stop_info.k = NULL;
  193. ret = kthread_stop_info.err;
  194. mutex_unlock(&kthread_stop_lock);
  195. return ret;
  196. }
  197. EXPORT_SYMBOL(kthread_stop);
  198. int kthreadd(void *unused)
  199. {
  200. struct task_struct *tsk = current;
  201. /* Setup a clean context for our children to inherit. */
  202. set_task_comm(tsk, "kthreadd");
  203. ignore_signals(tsk);
  204. set_user_nice(tsk, KTHREAD_NICE_LEVEL);
  205. set_cpus_allowed(tsk, CPU_MASK_ALL);
  206. current->flags |= PF_NOFREEZE;
  207. for (;;) {
  208. set_current_state(TASK_INTERRUPTIBLE);
  209. if (list_empty(&kthread_create_list))
  210. schedule();
  211. __set_current_state(TASK_RUNNING);
  212. spin_lock(&kthread_create_lock);
  213. while (!list_empty(&kthread_create_list)) {
  214. struct kthread_create_info *create;
  215. create = list_entry(kthread_create_list.next,
  216. struct kthread_create_info, list);
  217. list_del_init(&create->list);
  218. spin_unlock(&kthread_create_lock);
  219. create_kthread(create);
  220. spin_lock(&kthread_create_lock);
  221. }
  222. spin_unlock(&kthread_create_lock);
  223. }
  224. return 0;
  225. }