kthread.c 4.9 KB

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