PageRenderTime 83ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/kthread.c

https://code.google.com/
C | 252 lines | 146 code | 36 blank | 70 comment | 10 complexity | 8a1a9cdbaf3685b70e9bf49a165ef6f4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /* Kernel thread helper functions.
  2. * Copyright (C) 2004 IBM Corporation, Rusty Russell.
  3. *
  4. * Creation is done via kthreadd, 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/cpuset.h>
  13. #include <linux/unistd.h>
  14. #include <linux/file.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <trace/events/sched.h>
  18. static DEFINE_SPINLOCK(kthread_create_lock);
  19. static LIST_HEAD(kthread_create_list);
  20. struct task_struct *kthreadd_task;
  21. struct kthread_create_info
  22. {
  23. /* Information passed to kthread() from kthreadd. */
  24. int (*threadfn)(void *data);
  25. void *data;
  26. /* Result passed back to kthread_create() from kthreadd. */
  27. struct task_struct *result;
  28. struct completion done;
  29. struct list_head list;
  30. };
  31. struct kthread {
  32. int should_stop;
  33. struct completion exited;
  34. };
  35. #define to_kthread(tsk) \
  36. container_of((tsk)->vfork_done, struct kthread, exited)
  37. /**
  38. * kthread_should_stop - should this kthread return now?
  39. *
  40. * When someone calls kthread_stop() on your kthread, it will be woken
  41. * and this will return true. You should then return, and your return
  42. * value will be passed through to kthread_stop().
  43. */
  44. int kthread_should_stop(void)
  45. {
  46. return to_kthread(current)->should_stop;
  47. }
  48. EXPORT_SYMBOL(kthread_should_stop);
  49. static int kthread(void *_create)
  50. {
  51. /* Copy data: it's on kthread's stack */
  52. struct kthread_create_info *create = _create;
  53. int (*threadfn)(void *data) = create->threadfn;
  54. void *data = create->data;
  55. struct kthread self;
  56. int ret;
  57. self.should_stop = 0;
  58. init_completion(&self.exited);
  59. current->vfork_done = &self.exited;
  60. /* OK, tell user we're spawned, wait for stop or wakeup */
  61. __set_current_state(TASK_UNINTERRUPTIBLE);
  62. create->result = current;
  63. complete(&create->done);
  64. schedule();
  65. ret = -EINTR;
  66. if (!self.should_stop)
  67. ret = threadfn(data);
  68. /* we can't just return, we must preserve "self" on stack */
  69. do_exit(ret);
  70. }
  71. static void create_kthread(struct kthread_create_info *create)
  72. {
  73. int pid;
  74. /* We want our own signal handler (we take no signals by default). */
  75. pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
  76. if (pid < 0) {
  77. create->result = ERR_PTR(pid);
  78. complete(&create->done);
  79. }
  80. }
  81. /**
  82. * kthread_create - create a kthread.
  83. * @threadfn: the function to run until signal_pending(current).
  84. * @data: data ptr for @threadfn.
  85. * @namefmt: printf-style name for the thread.
  86. *
  87. * Description: This helper function creates and names a kernel
  88. * thread. The thread will be stopped: use wake_up_process() to start
  89. * it. See also kthread_run().
  90. *
  91. * When woken, the thread will run @threadfn() with @data as its
  92. * argument. @threadfn() can either call do_exit() directly if it is a
  93. * standalone thread for which noone will call kthread_stop(), or
  94. * return when 'kthread_should_stop()' is true (which means
  95. * kthread_stop() has been called). The return value should be zero
  96. * or a negative error number; it will be passed to kthread_stop().
  97. *
  98. * Returns a task_struct or ERR_PTR(-ENOMEM).
  99. */
  100. struct task_struct *kthread_create(int (*threadfn)(void *data),
  101. void *data,
  102. const char namefmt[],
  103. ...)
  104. {
  105. struct kthread_create_info create;
  106. create.threadfn = threadfn;
  107. create.data = data;
  108. init_completion(&create.done);
  109. spin_lock(&kthread_create_lock);
  110. list_add_tail(&create.list, &kthread_create_list);
  111. spin_unlock(&kthread_create_lock);
  112. wake_up_process(kthreadd_task);
  113. wait_for_completion(&create.done);
  114. if (!IS_ERR(create.result)) {
  115. struct sched_param param = { .sched_priority = 0 };
  116. va_list args;
  117. va_start(args, namefmt);
  118. vsnprintf(create.result->comm, sizeof(create.result->comm),
  119. namefmt, args);
  120. va_end(args);
  121. /*
  122. * root may have changed our (kthreadd's) priority or CPU mask.
  123. * The kernel thread should not inherit these properties.
  124. */
  125. sched_setscheduler_nocheck(create.result, SCHED_NORMAL, &param);
  126. set_cpus_allowed_ptr(create.result, cpu_all_mask);
  127. }
  128. return create.result;
  129. }
  130. EXPORT_SYMBOL(kthread_create);
  131. /**
  132. * kthread_bind - bind a just-created kthread to a cpu.
  133. * @p: thread created by kthread_create().
  134. * @cpu: cpu (might not be online, must be possible) for @k to run on.
  135. *
  136. * Description: This function is equivalent to set_cpus_allowed(),
  137. * except that @cpu doesn't need to be online, and the thread must be
  138. * stopped (i.e., just returned from kthread_create()).
  139. */
  140. void kthread_bind(struct task_struct *p, unsigned int cpu)
  141. {
  142. /* Must have done schedule() in kthread() before we set_task_cpu */
  143. if (!wait_task_inactive(p, TASK_UNINTERRUPTIBLE)) {
  144. WARN_ON(1);
  145. return;
  146. }
  147. /* It's safe because the task is inactive. */
  148. do_set_cpus_allowed(p, cpumask_of(cpu));
  149. p->flags |= PF_THREAD_BOUND;
  150. }
  151. EXPORT_SYMBOL(kthread_bind);
  152. /**
  153. * kthread_stop - stop a thread created by kthread_create().
  154. * @k: thread created by kthread_create().
  155. *
  156. * Sets kthread_should_stop() for @k to return true, wakes it, and
  157. * waits for it to exit. This can also be called after kthread_create()
  158. * instead of calling wake_up_process(): the thread will exit without
  159. * calling threadfn().
  160. *
  161. * If threadfn() may call do_exit() itself, the caller must ensure
  162. * task_struct can't go away.
  163. *
  164. * Returns the result of threadfn(), or %-EINTR if wake_up_process()
  165. * was never called.
  166. */
  167. int kthread_stop(struct task_struct *k)
  168. {
  169. struct kthread *kthread;
  170. int ret;
  171. trace_sched_kthread_stop(k);
  172. get_task_struct(k);
  173. kthread = to_kthread(k);
  174. barrier(); /* it might have exited */
  175. if (k->vfork_done != NULL) {
  176. kthread->should_stop = 1;
  177. wake_up_process(k);
  178. while (!wait_for_completion_timeout(&kthread->exited, 5 * HZ)) {
  179. printk("%s: try to stop %s for more than 5s!\n", __FUNCTION__, k->comm);
  180. sched_show_task(k);
  181. }
  182. }
  183. ret = k->exit_code;
  184. put_task_struct(k);
  185. trace_sched_kthread_stop_ret(ret);
  186. return ret;
  187. }
  188. EXPORT_SYMBOL(kthread_stop);
  189. int kthreadd(void *unused)
  190. {
  191. struct task_struct *tsk = current;
  192. /* Setup a clean context for our children to inherit. */
  193. set_task_comm(tsk, "kthreadd");
  194. ignore_signals(tsk);
  195. set_cpus_allowed_ptr(tsk, cpu_all_mask);
  196. set_mems_allowed(node_states[N_HIGH_MEMORY]);
  197. current->flags |= PF_NOFREEZE | PF_FREEZER_NOSIG;
  198. for (;;) {
  199. set_current_state(TASK_INTERRUPTIBLE);
  200. if (list_empty(&kthread_create_list))
  201. schedule();
  202. __set_current_state(TASK_RUNNING);
  203. spin_lock(&kthread_create_lock);
  204. while (!list_empty(&kthread_create_list)) {
  205. struct kthread_create_info *create;
  206. create = list_entry(kthread_create_list.next,
  207. struct kthread_create_info, list);
  208. list_del_init(&create->list);
  209. spin_unlock(&kthread_create_lock);
  210. create_kthread(create);
  211. spin_lock(&kthread_create_lock);
  212. }
  213. spin_unlock(&kthread_create_lock);
  214. }
  215. return 0;
  216. }