kthread.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_KTHREAD_H
  3. #define _LINUX_KTHREAD_H
  4. /* Simple interface for creating and stopping kernel threads without mess. */
  5. #include <linux/err.h>
  6. #include <linux/sched.h>
  7. struct mm_struct;
  8. __printf(4, 5)
  9. struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
  10. void *data,
  11. int node,
  12. const char namefmt[], ...);
  13. /**
  14. * kthread_create - create a kthread on the current node
  15. * @threadfn: the function to run in the thread
  16. * @data: data pointer for @threadfn()
  17. * @namefmt: printf-style format string for the thread name
  18. * @arg: arguments for @namefmt.
  19. *
  20. * This macro will create a kthread on the current node, leaving it in
  21. * the stopped state. This is just a helper for kthread_create_on_node();
  22. * see the documentation there for more details.
  23. */
  24. #define kthread_create(threadfn, data, namefmt, arg...) \
  25. kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
  26. struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
  27. void *data,
  28. unsigned int cpu,
  29. const char *namefmt);
  30. void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk);
  31. bool set_kthread_struct(struct task_struct *p);
  32. void kthread_set_per_cpu(struct task_struct *k, int cpu);
  33. bool kthread_is_per_cpu(struct task_struct *k);
  34. /**
  35. * kthread_run - create and wake a thread.
  36. * @threadfn: the function to run until signal_pending(current).
  37. * @data: data ptr for @threadfn.
  38. * @namefmt: printf-style name for the thread.
  39. *
  40. * Description: Convenient wrapper for kthread_create() followed by
  41. * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
  42. */
  43. #define kthread_run(threadfn, data, namefmt, ...) \
  44. ({ \
  45. struct task_struct *__k \
  46. = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
  47. if (!IS_ERR(__k)) \
  48. wake_up_process(__k); \
  49. __k; \
  50. })
  51. /**
  52. * kthread_run_on_cpu - create and wake a cpu bound thread.
  53. * @threadfn: the function to run until signal_pending(current).
  54. * @data: data ptr for @threadfn.
  55. * @cpu: The cpu on which the thread should be bound,
  56. * @namefmt: printf-style name for the thread. Format is restricted
  57. * to "name.*%u". Code fills in cpu number.
  58. *
  59. * Description: Convenient wrapper for kthread_create_on_cpu()
  60. * followed by wake_up_process(). Returns the kthread or
  61. * ERR_PTR(-ENOMEM).
  62. */
  63. static inline struct task_struct *
  64. kthread_run_on_cpu(int (*threadfn)(void *data), void *data,
  65. unsigned int cpu, const char *namefmt)
  66. {
  67. struct task_struct *p;
  68. p = kthread_create_on_cpu(threadfn, data, cpu, namefmt);
  69. if (!IS_ERR(p))
  70. wake_up_process(p);
  71. return p;
  72. }
  73. void free_kthread_struct(struct task_struct *k);
  74. void kthread_bind(struct task_struct *k, unsigned int cpu);
  75. void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
  76. int kthread_stop(struct task_struct *k);
  77. bool kthread_should_stop(void);
  78. bool kthread_should_park(void);
  79. bool __kthread_should_park(struct task_struct *k);
  80. bool kthread_freezable_should_stop(bool *was_frozen);
  81. void *kthread_func(struct task_struct *k);
  82. void *kthread_data(struct task_struct *k);
  83. void *kthread_probe_data(struct task_struct *k);
  84. int kthread_park(struct task_struct *k);
  85. void kthread_unpark(struct task_struct *k);
  86. void kthread_parkme(void);
  87. void kthread_exit(long result) __noreturn;
  88. void kthread_complete_and_exit(struct completion *, long) __noreturn;
  89. int kthreadd(void *unused);
  90. extern struct task_struct *kthreadd_task;
  91. extern int tsk_fork_get_node(struct task_struct *tsk);
  92. /*
  93. * Simple work processor based on kthread.
  94. *
  95. * This provides easier way to make use of kthreads. A kthread_work
  96. * can be queued and flushed using queue/kthread_flush_work()
  97. * respectively. Queued kthread_works are processed by a kthread
  98. * running kthread_worker_fn().
  99. */
  100. struct kthread_work;
  101. typedef void (*kthread_work_func_t)(struct kthread_work *work);
  102. void kthread_delayed_work_timer_fn(struct timer_list *t);
  103. enum {
  104. KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
  105. };
  106. struct kthread_worker {
  107. unsigned int flags;
  108. raw_spinlock_t lock;
  109. struct list_head work_list;
  110. struct list_head delayed_work_list;
  111. struct task_struct *task;
  112. struct kthread_work *current_work;
  113. };
  114. struct kthread_work {
  115. struct list_head node;
  116. kthread_work_func_t func;
  117. struct kthread_worker *worker;
  118. /* Number of canceling calls that are running at the moment. */
  119. int canceling;
  120. };
  121. struct kthread_delayed_work {
  122. struct kthread_work work;
  123. struct timer_list timer;
  124. };
  125. #define KTHREAD_WORK_INIT(work, fn) { \
  126. .node = LIST_HEAD_INIT((work).node), \
  127. .func = (fn), \
  128. }
  129. #define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
  130. .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
  131. .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
  132. TIMER_IRQSAFE), \
  133. }
  134. #define DEFINE_KTHREAD_WORK(work, fn) \
  135. struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
  136. #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
  137. struct kthread_delayed_work dwork = \
  138. KTHREAD_DELAYED_WORK_INIT(dwork, fn)
  139. extern void __kthread_init_worker(struct kthread_worker *worker,
  140. const char *name, struct lock_class_key *key);
  141. #define kthread_init_worker(worker) \
  142. do { \
  143. static struct lock_class_key __key; \
  144. __kthread_init_worker((worker), "("#worker")->lock", &__key); \
  145. } while (0)
  146. #define kthread_init_work(work, fn) \
  147. do { \
  148. memset((work), 0, sizeof(struct kthread_work)); \
  149. INIT_LIST_HEAD(&(work)->node); \
  150. (work)->func = (fn); \
  151. } while (0)
  152. #define kthread_init_delayed_work(dwork, fn) \
  153. do { \
  154. kthread_init_work(&(dwork)->work, (fn)); \
  155. timer_setup(&(dwork)->timer, \
  156. kthread_delayed_work_timer_fn, \
  157. TIMER_IRQSAFE); \
  158. } while (0)
  159. int kthread_worker_fn(void *worker_ptr);
  160. __printf(2, 3)
  161. struct kthread_worker *
  162. kthread_create_worker(unsigned int flags, const char namefmt[], ...);
  163. __printf(3, 4) struct kthread_worker *
  164. kthread_create_worker_on_cpu(int cpu, unsigned int flags,
  165. const char namefmt[], ...);
  166. bool kthread_queue_work(struct kthread_worker *worker,
  167. struct kthread_work *work);
  168. bool kthread_queue_delayed_work(struct kthread_worker *worker,
  169. struct kthread_delayed_work *dwork,
  170. unsigned long delay);
  171. bool kthread_mod_delayed_work(struct kthread_worker *worker,
  172. struct kthread_delayed_work *dwork,
  173. unsigned long delay);
  174. void kthread_flush_work(struct kthread_work *work);
  175. void kthread_flush_worker(struct kthread_worker *worker);
  176. bool kthread_cancel_work_sync(struct kthread_work *work);
  177. bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
  178. void kthread_destroy_worker(struct kthread_worker *worker);
  179. void kthread_use_mm(struct mm_struct *mm);
  180. void kthread_unuse_mm(struct mm_struct *mm);
  181. struct cgroup_subsys_state;
  182. #ifdef CONFIG_BLK_CGROUP
  183. void kthread_associate_blkcg(struct cgroup_subsys_state *css);
  184. struct cgroup_subsys_state *kthread_blkcg(void);
  185. #else
  186. static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
  187. #endif
  188. #endif /* _LINUX_KTHREAD_H */