kthread.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 <asm/semaphore.h>
  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. wait_for_completion(&create->started);
  84. read_lock(&tasklist_lock);
  85. create->result = find_task_by_pid(pid);
  86. read_unlock(&tasklist_lock);
  87. }
  88. complete(&create->done);
  89. }
  90. /**
  91. * kthread_create - create a kthread.
  92. * @threadfn: the function to run until signal_pending(current).
  93. * @data: data ptr for @threadfn.
  94. * @namefmt: printf-style name for the thread.
  95. *
  96. * Description: This helper function creates and names a kernel
  97. * thread. The thread will be stopped: use wake_up_process() to start
  98. * it. See also kthread_run(), kthread_create_on_cpu().
  99. *
  100. * When woken, the thread will run @threadfn() with @data as its
  101. * argument. @threadfn() can either call do_exit() directly if it is a
  102. * standalone thread for which noone will call kthread_stop(), or
  103. * return when 'kthread_should_stop()' is true (which means
  104. * kthread_stop() has been called). The return value should be zero
  105. * or a negative error number; it will be passed to kthread_stop().
  106. *
  107. * Returns a task_struct or ERR_PTR(-ENOMEM).
  108. */
  109. struct task_struct *kthread_create(int (*threadfn)(void *data),
  110. void *data,
  111. const char namefmt[],
  112. ...)
  113. {
  114. struct kthread_create_info create;
  115. create.threadfn = threadfn;
  116. create.data = data;
  117. init_completion(&create.started);
  118. init_completion(&create.done);
  119. spin_lock(&kthread_create_lock);
  120. list_add_tail(&create.list, &kthread_create_list);
  121. wake_up_process(kthreadd_task);
  122. spin_unlock(&kthread_create_lock);
  123. wait_for_completion(&create.done);
  124. if (!IS_ERR(create.result)) {
  125. va_list args;
  126. va_start(args, namefmt);
  127. vsnprintf(create.result->comm, sizeof(create.result->comm),
  128. namefmt, args);
  129. va_end(args);
  130. }
  131. return create.result;
  132. }
  133. EXPORT_SYMBOL(kthread_create);
  134. /**
  135. * kthread_bind - bind a just-created kthread to a cpu.
  136. * @k: thread created by kthread_create().
  137. * @cpu: cpu (might not be online, must be possible) for @k to run on.
  138. *
  139. * Description: This function is equivalent to set_cpus_allowed(),
  140. * except that @cpu doesn't need to be online, and the thread must be
  141. * stopped (i.e., just returned from kthread_create()).
  142. */
  143. void kthread_bind(struct task_struct *k, unsigned int cpu)
  144. {
  145. if (k->state != TASK_UNINTERRUPTIBLE) {
  146. WARN_ON(1);
  147. return;
  148. }
  149. /* Must have done schedule() in kthread() before we set_task_cpu */
  150. wait_task_inactive(k);
  151. set_task_cpu(k, cpu);
  152. k->cpus_allowed = cpumask_of_cpu(cpu);
  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. Your threadfn() must not call do_exit()
  161. * itself if you use this function! This can also be called after
  162. * kthread_create() instead of calling wake_up_process(): the thread
  163. * will exit without calling threadfn().
  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. int ret;
  171. mutex_lock(&kthread_stop_lock);
  172. /* It could exit after stop_info.k set, but before wake_up_process. */
  173. get_task_struct(k);
  174. /* Must init completion *before* thread sees kthread_stop_info.k */
  175. init_completion(&kthread_stop_info.done);
  176. smp_wmb();
  177. /* Now set kthread_should_stop() to true, and wake it up. */
  178. kthread_stop_info.k = k;
  179. wake_up_process(k);
  180. put_task_struct(k);
  181. /* Once it dies, reset stop ptr, gather result and we're done. */
  182. wait_for_completion(&kthread_stop_info.done);
  183. kthread_stop_info.k = NULL;
  184. ret = kthread_stop_info.err;
  185. mutex_unlock(&kthread_stop_lock);
  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, -5);
  196. set_cpus_allowed(tsk, CPU_MASK_ALL);
  197. current->flags |= PF_NOFREEZE;
  198. for (;;) {
  199. set_current_state(TASK_INTERRUPTIBLE);
  200. if (list_empty(&kthread_create_list))
  201. schedule();
  202. __set_current_state(TASK_RUNNING);
  203. spin_lock(&kthread_create_lock);
  204. while (!list_empty(&kthread_create_list)) {
  205. struct kthread_create_info *create;
  206. create = list_entry(kthread_create_list.next,
  207. struct kthread_create_info, list);
  208. list_del_init(&create->list);
  209. spin_unlock(&kthread_create_lock);
  210. create_kthread(create);
  211. spin_lock(&kthread_create_lock);
  212. }
  213. spin_unlock(&kthread_create_lock);
  214. }
  215. return 0;
  216. }