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