irq_poll.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Functions related to interrupt-poll handling in the block layer. This
  4. * is similar to NAPI for network devices.
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/bio.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/cpu.h>
  12. #include <linux/irq_poll.h>
  13. #include <linux/delay.h>
  14. static unsigned int irq_poll_budget __read_mostly = 256;
  15. static DEFINE_PER_CPU(struct list_head, blk_cpu_iopoll);
  16. /**
  17. * irq_poll_sched - Schedule a run of the iopoll handler
  18. * @iop: The parent iopoll structure
  19. *
  20. * Description:
  21. * Add this irq_poll structure to the pending poll list and trigger the
  22. * raise of the blk iopoll softirq.
  23. **/
  24. void irq_poll_sched(struct irq_poll *iop)
  25. {
  26. unsigned long flags;
  27. if (test_bit(IRQ_POLL_F_DISABLE, &iop->state))
  28. return;
  29. if (test_and_set_bit(IRQ_POLL_F_SCHED, &iop->state))
  30. return;
  31. local_irq_save(flags);
  32. list_add_tail(&iop->list, this_cpu_ptr(&blk_cpu_iopoll));
  33. raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
  34. local_irq_restore(flags);
  35. }
  36. EXPORT_SYMBOL(irq_poll_sched);
  37. /**
  38. * __irq_poll_complete - Mark this @iop as un-polled again
  39. * @iop: The parent iopoll structure
  40. *
  41. * Description:
  42. * See irq_poll_complete(). This function must be called with interrupts
  43. * disabled.
  44. **/
  45. static void __irq_poll_complete(struct irq_poll *iop)
  46. {
  47. list_del(&iop->list);
  48. smp_mb__before_atomic();
  49. clear_bit_unlock(IRQ_POLL_F_SCHED, &iop->state);
  50. }
  51. /**
  52. * irq_poll_complete - Mark this @iop as un-polled again
  53. * @iop: The parent iopoll structure
  54. *
  55. * Description:
  56. * If a driver consumes less than the assigned budget in its run of the
  57. * iopoll handler, it'll end the polled mode by calling this function. The
  58. * iopoll handler will not be invoked again before irq_poll_sched()
  59. * is called.
  60. **/
  61. void irq_poll_complete(struct irq_poll *iop)
  62. {
  63. unsigned long flags;
  64. local_irq_save(flags);
  65. __irq_poll_complete(iop);
  66. local_irq_restore(flags);
  67. }
  68. EXPORT_SYMBOL(irq_poll_complete);
  69. static void __latent_entropy irq_poll_softirq(struct softirq_action *h)
  70. {
  71. struct list_head *list = this_cpu_ptr(&blk_cpu_iopoll);
  72. int rearm = 0, budget = irq_poll_budget;
  73. unsigned long start_time = jiffies;
  74. local_irq_disable();
  75. while (!list_empty(list)) {
  76. struct irq_poll *iop;
  77. int work, weight;
  78. /*
  79. * If softirq window is exhausted then punt.
  80. */
  81. if (budget <= 0 || time_after(jiffies, start_time)) {
  82. rearm = 1;
  83. break;
  84. }
  85. local_irq_enable();
  86. /* Even though interrupts have been re-enabled, this
  87. * access is safe because interrupts can only add new
  88. * entries to the tail of this list, and only ->poll()
  89. * calls can remove this head entry from the list.
  90. */
  91. iop = list_entry(list->next, struct irq_poll, list);
  92. weight = iop->weight;
  93. work = 0;
  94. if (test_bit(IRQ_POLL_F_SCHED, &iop->state))
  95. work = iop->poll(iop, weight);
  96. budget -= work;
  97. local_irq_disable();
  98. /*
  99. * Drivers must not modify the iopoll state, if they
  100. * consume their assigned weight (or more, some drivers can't
  101. * easily just stop processing, they have to complete an
  102. * entire mask of commands).In such cases this code
  103. * still "owns" the iopoll instance and therefore can
  104. * move the instance around on the list at-will.
  105. */
  106. if (work >= weight) {
  107. if (test_bit(IRQ_POLL_F_DISABLE, &iop->state))
  108. __irq_poll_complete(iop);
  109. else
  110. list_move_tail(&iop->list, list);
  111. }
  112. }
  113. if (rearm)
  114. __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
  115. local_irq_enable();
  116. }
  117. /**
  118. * irq_poll_disable - Disable iopoll on this @iop
  119. * @iop: The parent iopoll structure
  120. *
  121. * Description:
  122. * Disable io polling and wait for any pending callbacks to have completed.
  123. **/
  124. void irq_poll_disable(struct irq_poll *iop)
  125. {
  126. set_bit(IRQ_POLL_F_DISABLE, &iop->state);
  127. while (test_and_set_bit(IRQ_POLL_F_SCHED, &iop->state))
  128. msleep(1);
  129. clear_bit(IRQ_POLL_F_DISABLE, &iop->state);
  130. }
  131. EXPORT_SYMBOL(irq_poll_disable);
  132. /**
  133. * irq_poll_enable - Enable iopoll on this @iop
  134. * @iop: The parent iopoll structure
  135. *
  136. * Description:
  137. * Enable iopoll on this @iop. Note that the handler run will not be
  138. * scheduled, it will only mark it as active.
  139. **/
  140. void irq_poll_enable(struct irq_poll *iop)
  141. {
  142. BUG_ON(!test_bit(IRQ_POLL_F_SCHED, &iop->state));
  143. smp_mb__before_atomic();
  144. clear_bit_unlock(IRQ_POLL_F_SCHED, &iop->state);
  145. }
  146. EXPORT_SYMBOL(irq_poll_enable);
  147. /**
  148. * irq_poll_init - Initialize this @iop
  149. * @iop: The parent iopoll structure
  150. * @weight: The default weight (or command completion budget)
  151. * @poll_fn: The handler to invoke
  152. *
  153. * Description:
  154. * Initialize and enable this irq_poll structure.
  155. **/
  156. void irq_poll_init(struct irq_poll *iop, int weight, irq_poll_fn *poll_fn)
  157. {
  158. memset(iop, 0, sizeof(*iop));
  159. INIT_LIST_HEAD(&iop->list);
  160. iop->weight = weight;
  161. iop->poll = poll_fn;
  162. }
  163. EXPORT_SYMBOL(irq_poll_init);
  164. static int irq_poll_cpu_dead(unsigned int cpu)
  165. {
  166. /*
  167. * If a CPU goes away, splice its entries to the current CPU
  168. * and trigger a run of the softirq
  169. */
  170. local_irq_disable();
  171. list_splice_init(&per_cpu(blk_cpu_iopoll, cpu),
  172. this_cpu_ptr(&blk_cpu_iopoll));
  173. __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
  174. local_irq_enable();
  175. return 0;
  176. }
  177. static __init int irq_poll_setup(void)
  178. {
  179. int i;
  180. for_each_possible_cpu(i)
  181. INIT_LIST_HEAD(&per_cpu(blk_cpu_iopoll, i));
  182. open_softirq(IRQ_POLL_SOFTIRQ, irq_poll_softirq);
  183. cpuhp_setup_state_nocalls(CPUHP_IRQ_POLL_DEAD, "irq_poll:dead", NULL,
  184. irq_poll_cpu_dead);
  185. return 0;
  186. }
  187. subsys_initcall(irq_poll_setup);