kthread.h 6.2 KB

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