kthread.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* Kernel thread helper functions.
  2. * Copyright (C) 2004 IBM Corporation, Rusty Russell.
  3. *
  4. * Creation is done via keventd, 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. /*
  18. * We dont want to execute off keventd since it might
  19. * hold a semaphore our callers hold too:
  20. */
  21. static struct workqueue_struct *helper_wq;
  22. struct kthread_create_info
  23. {
  24. /* Information passed to kthread() from keventd. */
  25. int (*threadfn)(void *data);
  26. void *data;
  27. struct completion started;
  28. /* Result passed back to kthread_create() from keventd. */
  29. struct task_struct *result;
  30. struct completion done;
  31. struct work_struct work;
  32. };
  33. struct kthread_stop_info
  34. {
  35. struct task_struct *k;
  36. int err;
  37. struct completion done;
  38. };
  39. /* Thread stopping is done by setthing this var: lock serializes
  40. * multiple kthread_stop calls. */
  41. static DEFINE_MUTEX(kthread_stop_lock);
  42. static struct kthread_stop_info kthread_stop_info;
  43. /**
  44. * kthread_should_stop - should this kthread return now?
  45. *
  46. * When someone calls kthread_stop() on your kthread, it will be woken
  47. * and this will return true. You should then return, and your return
  48. * value will be passed through to kthread_stop().
  49. */
  50. int kthread_should_stop(void)
  51. {
  52. return (kthread_stop_info.k == current);
  53. }
  54. EXPORT_SYMBOL(kthread_should_stop);
  55. static void kthread_exit_files(void)
  56. {
  57. struct fs_struct *fs;
  58. struct task_struct *tsk = current;
  59. exit_fs(tsk); /* current->fs->count--; */
  60. fs = init_task.fs;
  61. tsk->fs = fs;
  62. atomic_inc(&fs->count);
  63. exit_files(tsk);
  64. current->files = init_task.files;
  65. atomic_inc(&tsk->files->count);
  66. }
  67. static int kthread(void *_create)
  68. {
  69. struct kthread_create_info *create = _create;
  70. int (*threadfn)(void *data);
  71. void *data;
  72. sigset_t blocked;
  73. int ret = -EINTR;
  74. kthread_exit_files();
  75. /* Copy data: it's on keventd's stack */
  76. threadfn = create->threadfn;
  77. data = create->data;
  78. /* Block and flush all signals (in case we're not from keventd). */
  79. sigfillset(&blocked);
  80. sigprocmask(SIG_BLOCK, &blocked, NULL);
  81. flush_signals(current);
  82. /* By default we can run anywhere, unlike keventd. */
  83. set_cpus_allowed(current, CPU_MASK_ALL);
  84. /* OK, tell user we're spawned, wait for stop or wakeup */
  85. __set_current_state(TASK_INTERRUPTIBLE);
  86. complete(&create->started);
  87. schedule();
  88. if (!kthread_should_stop())
  89. ret = threadfn(data);
  90. /* It might have exited on its own, w/o kthread_stop. Check. */
  91. if (kthread_should_stop()) {
  92. kthread_stop_info.err = ret;
  93. complete(&kthread_stop_info.done);
  94. }
  95. return 0;
  96. }
  97. /* We are keventd: create a thread. */
  98. static void keventd_create_kthread(struct work_struct *work)
  99. {
  100. struct kthread_create_info *create =
  101. container_of(work, struct kthread_create_info, work);
  102. int pid;
  103. /* We want our own signal handler (we take no signals by default). */
  104. pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
  105. if (pid < 0) {
  106. create->result = ERR_PTR(pid);
  107. } else {
  108. wait_for_completion(&create->started);
  109. read_lock(&tasklist_lock);
  110. create->result = find_task_by_pid(pid);
  111. read_unlock(&tasklist_lock);
  112. }
  113. complete(&create->done);
  114. }
  115. /**
  116. * kthread_create - create a kthread.
  117. * @threadfn: the function to run until signal_pending(current).
  118. * @data: data ptr for @threadfn.
  119. * @namefmt: printf-style name for the thread.
  120. *
  121. * Description: This helper function creates and names a kernel
  122. * thread. The thread will be stopped: use wake_up_process() to start
  123. * it. See also kthread_run(), kthread_create_on_cpu().
  124. *
  125. * When woken, the thread will run @threadfn() with @data as its
  126. * argument. @threadfn() can either call do_exit() directly if it is a
  127. * standalone thread for which noone will call kthread_stop(), or
  128. * return when 'kthread_should_stop()' is true (which means
  129. * kthread_stop() has been called). The return value should be zero
  130. * or a negative error number; it will be passed to kthread_stop().
  131. *
  132. * Returns a task_struct or ERR_PTR(-ENOMEM).
  133. */
  134. struct task_struct *kthread_create(int (*threadfn)(void *data),
  135. void *data,
  136. const char namefmt[],
  137. ...)
  138. {
  139. struct kthread_create_info create;
  140. create.threadfn = threadfn;
  141. create.data = data;
  142. init_completion(&create.started);
  143. init_completion(&create.done);
  144. INIT_WORK(&create.work, keventd_create_kthread);
  145. /*
  146. * The workqueue needs to start up first:
  147. */
  148. if (!helper_wq)
  149. create.work.func(&create.work);
  150. else {
  151. queue_work(helper_wq, &create.work);
  152. wait_for_completion(&create.done);
  153. }
  154. if (!IS_ERR(create.result)) {
  155. va_list args;
  156. va_start(args, namefmt);
  157. vsnprintf(create.result->comm, sizeof(create.result->comm),
  158. namefmt, args);
  159. va_end(args);
  160. }
  161. return create.result;
  162. }
  163. EXPORT_SYMBOL(kthread_create);
  164. /**
  165. * kthread_bind - bind a just-created kthread to a cpu.
  166. * @k: thread created by kthread_create().
  167. * @cpu: cpu (might not be online, must be possible) for @k to run on.
  168. *
  169. * Description: This function is equivalent to set_cpus_allowed(),
  170. * except that @cpu doesn't need to be online, and the thread must be
  171. * stopped (i.e., just returned from kthread_create()).
  172. */
  173. void kthread_bind(struct task_struct *k, unsigned int cpu)
  174. {
  175. BUG_ON(k->state != TASK_INTERRUPTIBLE);
  176. /* Must have done schedule() in kthread() before we set_task_cpu */
  177. wait_task_inactive(k);
  178. set_task_cpu(k, cpu);
  179. k->cpus_allowed = cpumask_of_cpu(cpu);
  180. }
  181. EXPORT_SYMBOL(kthread_bind);
  182. /**
  183. * kthread_stop - stop a thread created by kthread_create().
  184. * @k: thread created by kthread_create().
  185. *
  186. * Sets kthread_should_stop() for @k to return true, wakes it, and
  187. * waits for it to exit. Your threadfn() must not call do_exit()
  188. * itself if you use this function! This can also be called after
  189. * kthread_create() instead of calling wake_up_process(): the thread
  190. * will exit without calling threadfn().
  191. *
  192. * Returns the result of threadfn(), or %-EINTR if wake_up_process()
  193. * was never called.
  194. */
  195. int kthread_stop(struct task_struct *k)
  196. {
  197. int ret;
  198. mutex_lock(&kthread_stop_lock);
  199. /* It could exit after stop_info.k set, but before wake_up_process. */
  200. get_task_struct(k);
  201. /* Must init completion *before* thread sees kthread_stop_info.k */
  202. init_completion(&kthread_stop_info.done);
  203. smp_wmb();
  204. /* Now set kthread_should_stop() to true, and wake it up. */
  205. kthread_stop_info.k = k;
  206. wake_up_process(k);
  207. put_task_struct(k);
  208. /* Once it dies, reset stop ptr, gather result and we're done. */
  209. wait_for_completion(&kthread_stop_info.done);
  210. kthread_stop_info.k = NULL;
  211. ret = kthread_stop_info.err;
  212. mutex_unlock(&kthread_stop_lock);
  213. return ret;
  214. }
  215. EXPORT_SYMBOL(kthread_stop);
  216. static __init int helper_init(void)
  217. {
  218. helper_wq = create_singlethread_workqueue("kthread");
  219. BUG_ON(!helper_wq);
  220. return 0;
  221. }
  222. core_initcall(helper_init);