kthread.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. read_lock(&tasklist_lock);
  101. create->result = find_task_by_pid(pid);
  102. read_unlock(&tasklist_lock);
  103. }
  104. complete(&create->done);
  105. }
  106. struct task_struct *kthread_create(int (*threadfn)(void *data),
  107. void *data,
  108. const char namefmt[],
  109. ...)
  110. {
  111. struct kthread_create_info create;
  112. DECLARE_WORK(work, keventd_create_kthread, &create);
  113. create.threadfn = threadfn;
  114. create.data = data;
  115. init_completion(&create.started);
  116. init_completion(&create.done);
  117. /*
  118. * The workqueue needs to start up first:
  119. */
  120. if (!helper_wq)
  121. work.func(work.data);
  122. else {
  123. queue_work(helper_wq, &work);
  124. wait_for_completion(&create.done);
  125. }
  126. if (!IS_ERR(create.result)) {
  127. va_list args;
  128. va_start(args, namefmt);
  129. vsnprintf(create.result->comm, sizeof(create.result->comm),
  130. namefmt, args);
  131. va_end(args);
  132. }
  133. return create.result;
  134. }
  135. EXPORT_SYMBOL(kthread_create);
  136. void kthread_bind(struct task_struct *k, unsigned int cpu)
  137. {
  138. BUG_ON(k->state != TASK_INTERRUPTIBLE);
  139. /* Must have done schedule() in kthread() before we set_task_cpu */
  140. wait_task_inactive(k);
  141. set_task_cpu(k, cpu);
  142. k->cpus_allowed = cpumask_of_cpu(cpu);
  143. }
  144. EXPORT_SYMBOL(kthread_bind);
  145. int kthread_stop(struct task_struct *k)
  146. {
  147. return kthread_stop_sem(k, NULL);
  148. }
  149. EXPORT_SYMBOL(kthread_stop);
  150. int kthread_stop_sem(struct task_struct *k, struct semaphore *s)
  151. {
  152. int ret;
  153. mutex_lock(&kthread_stop_lock);
  154. /* It could exit after stop_info.k set, but before wake_up_process. */
  155. get_task_struct(k);
  156. /* Must init completion *before* thread sees kthread_stop_info.k */
  157. init_completion(&kthread_stop_info.done);
  158. smp_wmb();
  159. /* Now set kthread_should_stop() to true, and wake it up. */
  160. kthread_stop_info.k = k;
  161. if (s)
  162. up(s);
  163. else
  164. wake_up_process(k);
  165. put_task_struct(k);
  166. /* Once it dies, reset stop ptr, gather result and we're done. */
  167. wait_for_completion(&kthread_stop_info.done);
  168. kthread_stop_info.k = NULL;
  169. ret = kthread_stop_info.err;
  170. mutex_unlock(&kthread_stop_lock);
  171. return ret;
  172. }
  173. EXPORT_SYMBOL(kthread_stop_sem);
  174. static __init int helper_init(void)
  175. {
  176. helper_wq = create_singlethread_workqueue("kthread");
  177. BUG_ON(!helper_wq);
  178. return 0;
  179. }
  180. core_initcall(helper_init);