kthread.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. int kthread_should_stop(void)
  43. {
  44. return (kthread_stop_info.k == current);
  45. }
  46. EXPORT_SYMBOL(kthread_should_stop);
  47. static void kthread_exit_files(void)
  48. {
  49. struct fs_struct *fs;
  50. struct task_struct *tsk = current;
  51. exit_fs(tsk); /* current->fs->count--; */
  52. fs = init_task.fs;
  53. tsk->fs = fs;
  54. atomic_inc(&fs->count);
  55. exit_files(tsk);
  56. current->files = init_task.files;
  57. atomic_inc(&tsk->files->count);
  58. }
  59. static int kthread(void *_create)
  60. {
  61. struct kthread_create_info *create = _create;
  62. int (*threadfn)(void *data);
  63. void *data;
  64. sigset_t blocked;
  65. int ret = -EINTR;
  66. kthread_exit_files();
  67. /* Copy data: it's on keventd's stack */
  68. threadfn = create->threadfn;
  69. data = create->data;
  70. /* Block and flush all signals (in case we're not from keventd). */
  71. sigfillset(&blocked);
  72. sigprocmask(SIG_BLOCK, &blocked, NULL);
  73. flush_signals(current);
  74. /* By default we can run anywhere, unlike keventd. */
  75. set_cpus_allowed(current, CPU_MASK_ALL);
  76. /* OK, tell user we're spawned, wait for stop or wakeup */
  77. __set_current_state(TASK_INTERRUPTIBLE);
  78. complete(&create->started);
  79. schedule();
  80. if (!kthread_should_stop())
  81. ret = threadfn(data);
  82. /* It might have exited on its own, w/o kthread_stop. Check. */
  83. if (kthread_should_stop()) {
  84. kthread_stop_info.err = ret;
  85. complete(&kthread_stop_info.done);
  86. }
  87. return 0;
  88. }
  89. /* We are keventd: create a thread. */
  90. static void keventd_create_kthread(void *_create)
  91. {
  92. struct kthread_create_info *create = _create;
  93. int pid;
  94. /* We want our own signal handler (we take no signals by default). */
  95. pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
  96. if (pid < 0) {
  97. create->result = ERR_PTR(pid);
  98. } else {
  99. wait_for_completion(&create->started);
  100. create->result = find_task_by_pid(pid);
  101. }
  102. complete(&create->done);
  103. }
  104. struct task_struct *kthread_create(int (*threadfn)(void *data),
  105. void *data,
  106. const char namefmt[],
  107. ...)
  108. {
  109. struct kthread_create_info create;
  110. DECLARE_WORK(work, keventd_create_kthread, &create);
  111. create.threadfn = threadfn;
  112. create.data = data;
  113. init_completion(&create.started);
  114. init_completion(&create.done);
  115. /*
  116. * The workqueue needs to start up first:
  117. */
  118. if (!helper_wq)
  119. work.func(work.data);
  120. else {
  121. queue_work(helper_wq, &work);
  122. wait_for_completion(&create.done);
  123. }
  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. void kthread_bind(struct task_struct *k, unsigned int cpu)
  135. {
  136. BUG_ON(k->state != TASK_INTERRUPTIBLE);
  137. /* Must have done schedule() in kthread() before we set_task_cpu */
  138. wait_task_inactive(k);
  139. set_task_cpu(k, cpu);
  140. k->cpus_allowed = cpumask_of_cpu(cpu);
  141. }
  142. EXPORT_SYMBOL(kthread_bind);
  143. int kthread_stop(struct task_struct *k)
  144. {
  145. return kthread_stop_sem(k, NULL);
  146. }
  147. EXPORT_SYMBOL(kthread_stop);
  148. int kthread_stop_sem(struct task_struct *k, struct semaphore *s)
  149. {
  150. int ret;
  151. mutex_lock(&kthread_stop_lock);
  152. /* It could exit after stop_info.k set, but before wake_up_process. */
  153. get_task_struct(k);
  154. /* Must init completion *before* thread sees kthread_stop_info.k */
  155. init_completion(&kthread_stop_info.done);
  156. smp_wmb();
  157. /* Now set kthread_should_stop() to true, and wake it up. */
  158. kthread_stop_info.k = k;
  159. if (s)
  160. up(s);
  161. else
  162. wake_up_process(k);
  163. put_task_struct(k);
  164. /* Once it dies, reset stop ptr, gather result and we're done. */
  165. wait_for_completion(&kthread_stop_info.done);
  166. kthread_stop_info.k = NULL;
  167. ret = kthread_stop_info.err;
  168. mutex_unlock(&kthread_stop_lock);
  169. return ret;
  170. }
  171. EXPORT_SYMBOL(kthread_stop_sem);
  172. static __init int helper_init(void)
  173. {
  174. helper_wq = create_singlethread_workqueue("kthread");
  175. BUG_ON(!helper_wq);
  176. return 0;
  177. }
  178. core_initcall(helper_init);