irq_poll.c 5.4 KB

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