kthread.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 kthread_set_per_cpu(struct task_struct *k, int cpu);
  31. bool kthread_is_per_cpu(struct task_struct *k);
  32. /**
  33. * kthread_run - create and wake a thread.
  34. * @threadfn: the function to run until signal_pending(current).
  35. * @data: data ptr for @threadfn.
  36. * @namefmt: printf-style name for the thread.
  37. *
  38. * Description: Convenient wrapper for kthread_create() followed by
  39. * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
  40. */
  41. #define kthread_run(threadfn, data, namefmt, ...) \
  42. ({ \
  43. struct task_struct *__k \
  44. = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
  45. if (!IS_ERR(__k)) \
  46. wake_up_process(__k); \
  47. __k; \
  48. })
  49. void free_kthread_struct(struct task_struct *k);
  50. void kthread_bind(struct task_struct *k, unsigned int cpu);
  51. void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
  52. int kthread_stop(struct task_struct *k);
  53. bool kthread_should_stop(void);
  54. bool kthread_should_park(void);
  55. bool __kthread_should_park(struct task_struct *k);
  56. bool kthread_freezable_should_stop(bool *was_frozen);
  57. void *kthread_func(struct task_struct *k);
  58. void *kthread_data(struct task_struct *k);
  59. void *kthread_probe_data(struct task_struct *k);
  60. int kthread_park(struct task_struct *k);
  61. void kthread_unpark(struct task_struct *k);
  62. void kthread_parkme(void);
  63. int kthreadd(void *unused);
  64. extern struct task_struct *kthreadd_task;
  65. extern int tsk_fork_get_node(struct task_struct *tsk);
  66. /*
  67. * Simple work processor based on kthread.
  68. *
  69. * This provides easier way to make use of kthreads. A kthread_work
  70. * can be queued and flushed using queue/kthread_flush_work()
  71. * respectively. Queued kthread_works are processed by a kthread
  72. * running kthread_worker_fn().
  73. */
  74. struct kthread_work;
  75. typedef void (*kthread_work_func_t)(struct kthread_work *work);
  76. void kthread_delayed_work_timer_fn(struct timer_list *t);
  77. enum {
  78. KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
  79. };
  80. struct kthread_worker {
  81. unsigned int flags;
  82. raw_spinlock_t lock;
  83. struct list_head work_list;
  84. struct list_head delayed_work_list;
  85. struct task_struct *task;
  86. struct kthread_work *current_work;
  87. };
  88. struct kthread_work {
  89. struct list_head node;
  90. kthread_work_func_t func;
  91. struct kthread_worker *worker;
  92. /* Number of canceling calls that are running at the moment. */
  93. int canceling;
  94. };
  95. struct kthread_delayed_work {
  96. struct kthread_work work;
  97. struct timer_list timer;
  98. };
  99. #define KTHREAD_WORKER_INIT(worker) { \
  100. .lock = __RAW_SPIN_LOCK_UNLOCKED((worker).lock), \
  101. .work_list = LIST_HEAD_INIT((worker).work_list), \
  102. .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
  103. }
  104. #define KTHREAD_WORK_INIT(work, fn) { \
  105. .node = LIST_HEAD_INIT((work).node), \
  106. .func = (fn), \
  107. }
  108. #define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
  109. .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
  110. .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
  111. TIMER_IRQSAFE), \
  112. }
  113. #define DEFINE_KTHREAD_WORKER(worker) \
  114. struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
  115. #define DEFINE_KTHREAD_WORK(work, fn) \
  116. struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
  117. #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
  118. struct kthread_delayed_work dwork = \
  119. KTHREAD_DELAYED_WORK_INIT(dwork, fn)
  120. /*
  121. * kthread_worker.lock needs its own lockdep class key when defined on
  122. * stack with lockdep enabled. Use the following macros in such cases.
  123. */
  124. #ifdef CONFIG_LOCKDEP
  125. # define KTHREAD_WORKER_INIT_ONSTACK(worker) \
  126. ({ kthread_init_worker(&worker); worker; })
  127. # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
  128. struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
  129. #else
  130. # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
  131. #endif
  132. extern void __kthread_init_worker(struct kthread_worker *worker,
  133. const char *name, struct lock_class_key *key);
  134. #define kthread_init_worker(worker) \
  135. do { \
  136. static struct lock_class_key __key; \
  137. __kthread_init_worker((worker), "("#worker")->lock", &__key); \
  138. } while (0)
  139. #define kthread_init_work(work, fn) \
  140. do { \
  141. memset((work), 0, sizeof(struct kthread_work)); \
  142. INIT_LIST_HEAD(&(work)->node); \
  143. (work)->func = (fn); \
  144. } while (0)
  145. #define kthread_init_delayed_work(dwork, fn) \
  146. do { \
  147. kthread_init_work(&(dwork)->work, (fn)); \
  148. timer_setup(&(dwork)->timer, \
  149. kthread_delayed_work_timer_fn, \
  150. TIMER_IRQSAFE); \
  151. } while (0)
  152. int kthread_worker_fn(void *worker_ptr);
  153. __printf(2, 3)
  154. struct kthread_worker *
  155. kthread_create_worker(unsigned int flags, const char namefmt[], ...);
  156. __printf(3, 4) struct kthread_worker *
  157. kthread_create_worker_on_cpu(int cpu, unsigned int flags,
  158. const char namefmt[], ...);
  159. bool kthread_queue_work(struct kthread_worker *worker,
  160. struct kthread_work *work);
  161. bool kthread_queue_delayed_work(struct kthread_worker *worker,
  162. struct kthread_delayed_work *dwork,
  163. unsigned long delay);
  164. bool kthread_mod_delayed_work(struct kthread_worker *worker,
  165. struct kthread_delayed_work *dwork,
  166. unsigned long delay);
  167. void kthread_flush_work(struct kthread_work *work);
  168. void kthread_flush_worker(struct kthread_worker *worker);
  169. bool kthread_cancel_work_sync(struct kthread_work *work);
  170. bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
  171. void kthread_destroy_worker(struct kthread_worker *worker);
  172. void kthread_use_mm(struct mm_struct *mm);
  173. void kthread_unuse_mm(struct mm_struct *mm);
  174. struct cgroup_subsys_state;
  175. #ifdef CONFIG_BLK_CGROUP
  176. void kthread_associate_blkcg(struct cgroup_subsys_state *css);
  177. struct cgroup_subsys_state *kthread_blkcg(void);
  178. #else
  179. static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
  180. static inline struct cgroup_subsys_state *kthread_blkcg(void)
  181. {
  182. return NULL;
  183. }
  184. #endif
  185. #endif /* _LINUX_KTHREAD_H */