irq_work.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2010 Red Hat, Inc., Peter Zijlstra
  4. *
  5. * Provides a framework for enqueueing and running callbacks from hardirq
  6. * context. The enqueueing is NMI-safe.
  7. */
  8. #include <linux/bug.h>
  9. #include <linux/kernel.h>
  10. #include <linux/export.h>
  11. #include <linux/irq_work.h>
  12. #include <linux/percpu.h>
  13. #include <linux/hardirq.h>
  14. #include <linux/irqflags.h>
  15. #include <linux/sched.h>
  16. #include <linux/tick.h>
  17. #include <linux/cpu.h>
  18. #include <linux/notifier.h>
  19. #include <linux/smp.h>
  20. #include <linux/smpboot.h>
  21. #include <asm/processor.h>
  22. #include <linux/kasan.h>
  23. static DEFINE_PER_CPU(struct llist_head, raised_list);
  24. static DEFINE_PER_CPU(struct llist_head, lazy_list);
  25. static DEFINE_PER_CPU(struct task_struct *, irq_workd);
  26. static void wake_irq_workd(void)
  27. {
  28. struct task_struct *tsk = __this_cpu_read(irq_workd);
  29. if (!llist_empty(this_cpu_ptr(&lazy_list)) && tsk)
  30. wake_up_process(tsk);
  31. }
  32. #ifdef CONFIG_SMP
  33. static void irq_work_wake(struct irq_work *entry)
  34. {
  35. wake_irq_workd();
  36. }
  37. static DEFINE_PER_CPU(struct irq_work, irq_work_wakeup) =
  38. IRQ_WORK_INIT_HARD(irq_work_wake);
  39. #endif
  40. static int irq_workd_should_run(unsigned int cpu)
  41. {
  42. return !llist_empty(this_cpu_ptr(&lazy_list));
  43. }
  44. /*
  45. * Claim the entry so that no one else will poke at it.
  46. */
  47. static bool irq_work_claim(struct irq_work *work)
  48. {
  49. int oflags;
  50. oflags = atomic_fetch_or(IRQ_WORK_CLAIMED | CSD_TYPE_IRQ_WORK, &work->node.a_flags);
  51. /*
  52. * If the work is already pending, no need to raise the IPI.
  53. * The pairing smp_mb() in irq_work_single() makes sure
  54. * everything we did before is visible.
  55. */
  56. if (oflags & IRQ_WORK_PENDING)
  57. return false;
  58. return true;
  59. }
  60. void __weak arch_irq_work_raise(void)
  61. {
  62. /*
  63. * Lame architectures will get the timer tick callback
  64. */
  65. }
  66. /* Enqueue on current CPU, work must already be claimed and preempt disabled */
  67. static void __irq_work_queue_local(struct irq_work *work)
  68. {
  69. struct llist_head *list;
  70. bool rt_lazy_work = false;
  71. bool lazy_work = false;
  72. int work_flags;
  73. work_flags = atomic_read(&work->node.a_flags);
  74. if (work_flags & IRQ_WORK_LAZY)
  75. lazy_work = true;
  76. else if (IS_ENABLED(CONFIG_PREEMPT_RT) &&
  77. !(work_flags & IRQ_WORK_HARD_IRQ))
  78. rt_lazy_work = true;
  79. if (lazy_work || rt_lazy_work)
  80. list = this_cpu_ptr(&lazy_list);
  81. else
  82. list = this_cpu_ptr(&raised_list);
  83. if (!llist_add(&work->node.llist, list))
  84. return;
  85. /* If the work is "lazy", handle it from next tick if any */
  86. if (!lazy_work || tick_nohz_tick_stopped())
  87. arch_irq_work_raise();
  88. }
  89. /* Enqueue the irq work @work on the current CPU */
  90. bool irq_work_queue(struct irq_work *work)
  91. {
  92. /* Only queue if not already pending */
  93. if (!irq_work_claim(work))
  94. return false;
  95. /* Queue the entry and raise the IPI if needed. */
  96. preempt_disable();
  97. __irq_work_queue_local(work);
  98. preempt_enable();
  99. return true;
  100. }
  101. EXPORT_SYMBOL_GPL(irq_work_queue);
  102. /*
  103. * Enqueue the irq_work @work on @cpu unless it's already pending
  104. * somewhere.
  105. *
  106. * Can be re-enqueued while the callback is still in progress.
  107. */
  108. bool irq_work_queue_on(struct irq_work *work, int cpu)
  109. {
  110. #ifndef CONFIG_SMP
  111. return irq_work_queue(work);
  112. #else /* CONFIG_SMP: */
  113. /* All work should have been flushed before going offline */
  114. WARN_ON_ONCE(cpu_is_offline(cpu));
  115. /* Only queue if not already pending */
  116. if (!irq_work_claim(work))
  117. return false;
  118. kasan_record_aux_stack_noalloc(work);
  119. preempt_disable();
  120. if (cpu != smp_processor_id()) {
  121. /* Arch remote IPI send/receive backend aren't NMI safe */
  122. WARN_ON_ONCE(in_nmi());
  123. /*
  124. * On PREEMPT_RT the items which are not marked as
  125. * IRQ_WORK_HARD_IRQ are added to the lazy list and a HARD work
  126. * item is used on the remote CPU to wake the thread.
  127. */
  128. if (IS_ENABLED(CONFIG_PREEMPT_RT) &&
  129. !(atomic_read(&work->node.a_flags) & IRQ_WORK_HARD_IRQ)) {
  130. if (!llist_add(&work->node.llist, &per_cpu(lazy_list, cpu)))
  131. goto out;
  132. work = &per_cpu(irq_work_wakeup, cpu);
  133. if (!irq_work_claim(work))
  134. goto out;
  135. }
  136. __smp_call_single_queue(cpu, &work->node.llist);
  137. } else {
  138. __irq_work_queue_local(work);
  139. }
  140. out:
  141. preempt_enable();
  142. return true;
  143. #endif /* CONFIG_SMP */
  144. }
  145. EXPORT_SYMBOL_GPL(irq_work_queue_on);
  146. bool irq_work_needs_cpu(void)
  147. {
  148. struct llist_head *raised, *lazy;
  149. raised = this_cpu_ptr(&raised_list);
  150. lazy = this_cpu_ptr(&lazy_list);
  151. if (llist_empty(raised) || arch_irq_work_has_interrupt())
  152. if (llist_empty(lazy))
  153. return false;
  154. /* All work should have been flushed before going offline */
  155. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  156. return true;
  157. }
  158. void irq_work_single(void *arg)
  159. {
  160. struct irq_work *work = arg;
  161. int flags;
  162. /*
  163. * Clear the PENDING bit, after this point the @work can be re-used.
  164. * The PENDING bit acts as a lock, and we own it, so we can clear it
  165. * without atomic ops.
  166. */
  167. flags = atomic_read(&work->node.a_flags);
  168. flags &= ~IRQ_WORK_PENDING;
  169. atomic_set(&work->node.a_flags, flags);
  170. /*
  171. * See irq_work_claim().
  172. */
  173. smp_mb();
  174. lockdep_irq_work_enter(flags);
  175. work->func(work);
  176. lockdep_irq_work_exit(flags);
  177. /*
  178. * Clear the BUSY bit, if set, and return to the free state if no-one
  179. * else claimed it meanwhile.
  180. */
  181. (void)atomic_cmpxchg(&work->node.a_flags, flags, flags & ~IRQ_WORK_BUSY);
  182. if ((IS_ENABLED(CONFIG_PREEMPT_RT) && !irq_work_is_hard(work)) ||
  183. !arch_irq_work_has_interrupt())
  184. rcuwait_wake_up(&work->irqwait);
  185. }
  186. static void irq_work_run_list(struct llist_head *list)
  187. {
  188. struct irq_work *work, *tmp;
  189. struct llist_node *llnode;
  190. /*
  191. * On PREEMPT_RT IRQ-work which is not marked as HARD will be processed
  192. * in a per-CPU thread in preemptible context. Only the items which are
  193. * marked as IRQ_WORK_HARD_IRQ will be processed in hardirq context.
  194. */
  195. BUG_ON(!irqs_disabled() && !IS_ENABLED(CONFIG_PREEMPT_RT));
  196. if (llist_empty(list))
  197. return;
  198. llnode = llist_del_all(list);
  199. llist_for_each_entry_safe(work, tmp, llnode, node.llist)
  200. irq_work_single(work);
  201. }
  202. /*
  203. * hotplug calls this through:
  204. * hotplug_cfd() -> flush_smp_call_function_queue()
  205. */
  206. void irq_work_run(void)
  207. {
  208. irq_work_run_list(this_cpu_ptr(&raised_list));
  209. if (!IS_ENABLED(CONFIG_PREEMPT_RT))
  210. irq_work_run_list(this_cpu_ptr(&lazy_list));
  211. else
  212. wake_irq_workd();
  213. }
  214. EXPORT_SYMBOL_GPL(irq_work_run);
  215. void irq_work_tick(void)
  216. {
  217. struct llist_head *raised = this_cpu_ptr(&raised_list);
  218. if (!llist_empty(raised) && !arch_irq_work_has_interrupt())
  219. irq_work_run_list(raised);
  220. if (!IS_ENABLED(CONFIG_PREEMPT_RT))
  221. irq_work_run_list(this_cpu_ptr(&lazy_list));
  222. else
  223. wake_irq_workd();
  224. }
  225. /*
  226. * Synchronize against the irq_work @entry, ensures the entry is not
  227. * currently in use.
  228. */
  229. void irq_work_sync(struct irq_work *work)
  230. {
  231. lockdep_assert_irqs_enabled();
  232. might_sleep();
  233. if ((IS_ENABLED(CONFIG_PREEMPT_RT) && !irq_work_is_hard(work)) ||
  234. !arch_irq_work_has_interrupt()) {
  235. rcuwait_wait_event(&work->irqwait, !irq_work_is_busy(work),
  236. TASK_UNINTERRUPTIBLE);
  237. return;
  238. }
  239. while (irq_work_is_busy(work))
  240. cpu_relax();
  241. }
  242. EXPORT_SYMBOL_GPL(irq_work_sync);
  243. static void run_irq_workd(unsigned int cpu)
  244. {
  245. irq_work_run_list(this_cpu_ptr(&lazy_list));
  246. }
  247. static void irq_workd_setup(unsigned int cpu)
  248. {
  249. sched_set_fifo_low(current);
  250. }
  251. static struct smp_hotplug_thread irqwork_threads = {
  252. .store = &irq_workd,
  253. .setup = irq_workd_setup,
  254. .thread_should_run = irq_workd_should_run,
  255. .thread_fn = run_irq_workd,
  256. .thread_comm = "irq_work/%u",
  257. };
  258. static __init int irq_work_init_threads(void)
  259. {
  260. if (IS_ENABLED(CONFIG_PREEMPT_RT))
  261. BUG_ON(smpboot_register_percpu_thread(&irqwork_threads));
  262. return 0;
  263. }
  264. early_initcall(irq_work_init_threads);