blk-iopoll.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/blkdev.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/cpu.h>
  12. #include <linux/blk-iopoll.h>
  13. #include <linux/delay.h>
  14. #include "blk.h"
  15. static unsigned int blk_iopoll_budget __read_mostly = 256;
  16. static DEFINE_PER_CPU(struct list_head, blk_cpu_iopoll);
  17. /**
  18. * blk_iopoll_sched - Schedule a run of the iopoll handler
  19. * @iop: The parent iopoll structure
  20. *
  21. * Description:
  22. * Add this blk_iopoll structure to the pending poll list and trigger the
  23. * raise of the blk iopoll softirq. The driver must already have gotten a
  24. * successful return from blk_iopoll_sched_prep() before calling this.
  25. **/
  26. void blk_iopoll_sched(struct blk_iopoll *iop)
  27. {
  28. unsigned long flags;
  29. local_irq_save(flags);
  30. list_add_tail(&iop->list, this_cpu_ptr(&blk_cpu_iopoll));
  31. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  32. local_irq_restore(flags);
  33. }
  34. EXPORT_SYMBOL(blk_iopoll_sched);
  35. /**
  36. * __blk_iopoll_complete - Mark this @iop as un-polled again
  37. * @iop: The parent iopoll structure
  38. *
  39. * Description:
  40. * See blk_iopoll_complete(). This function must be called with interrupts
  41. * disabled.
  42. **/
  43. void __blk_iopoll_complete(struct blk_iopoll *iop)
  44. {
  45. list_del(&iop->list);
  46. smp_mb__before_atomic();
  47. clear_bit_unlock(IOPOLL_F_SCHED, &iop->state);
  48. }
  49. EXPORT_SYMBOL(__blk_iopoll_complete);
  50. /**
  51. * blk_iopoll_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 blk_iopoll_sched_prep()
  58. * is called.
  59. **/
  60. void blk_iopoll_complete(struct blk_iopoll *iop)
  61. {
  62. unsigned long flags;
  63. local_irq_save(flags);
  64. __blk_iopoll_complete(iop);
  65. local_irq_restore(flags);
  66. }
  67. EXPORT_SYMBOL(blk_iopoll_complete);
  68. static void blk_iopoll_softirq(struct softirq_action *h)
  69. {
  70. struct list_head *list = this_cpu_ptr(&blk_cpu_iopoll);
  71. int rearm = 0, budget = blk_iopoll_budget;
  72. unsigned long start_time = jiffies;
  73. local_irq_disable();
  74. while (!list_empty(list)) {
  75. struct blk_iopoll *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 blk_iopoll, list);
  91. weight = iop->weight;
  92. work = 0;
  93. if (test_bit(IOPOLL_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 (blk_iopoll_disable_pending(iop))
  107. __blk_iopoll_complete(iop);
  108. else
  109. list_move_tail(&iop->list, list);
  110. }
  111. }
  112. if (rearm)
  113. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  114. local_irq_enable();
  115. }
  116. /**
  117. * blk_iopoll_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 blk_iopoll_disable(struct blk_iopoll *iop)
  124. {
  125. set_bit(IOPOLL_F_DISABLE, &iop->state);
  126. while (test_and_set_bit(IOPOLL_F_SCHED, &iop->state))
  127. msleep(1);
  128. clear_bit(IOPOLL_F_DISABLE, &iop->state);
  129. }
  130. EXPORT_SYMBOL(blk_iopoll_disable);
  131. /**
  132. * blk_iopoll_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 blk_iopoll_enable(struct blk_iopoll *iop)
  140. {
  141. BUG_ON(!test_bit(IOPOLL_F_SCHED, &iop->state));
  142. smp_mb__before_atomic();
  143. clear_bit_unlock(IOPOLL_F_SCHED, &iop->state);
  144. }
  145. EXPORT_SYMBOL(blk_iopoll_enable);
  146. /**
  147. * blk_iopoll_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 this blk_iopoll structure. Before being actively used, the
  154. * driver must call blk_iopoll_enable().
  155. **/
  156. void blk_iopoll_init(struct blk_iopoll *iop, int weight, blk_iopoll_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. set_bit(IOPOLL_F_SCHED, &iop->state);
  163. }
  164. EXPORT_SYMBOL(blk_iopoll_init);
  165. static int blk_iopoll_cpu_notify(struct notifier_block *self,
  166. unsigned long action, void *hcpu)
  167. {
  168. /*
  169. * If a CPU goes away, splice its entries to the current CPU
  170. * and trigger a run of the softirq
  171. */
  172. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  173. int cpu = (unsigned long) hcpu;
  174. local_irq_disable();
  175. list_splice_init(&per_cpu(blk_cpu_iopoll, cpu),
  176. this_cpu_ptr(&blk_cpu_iopoll));
  177. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  178. local_irq_enable();
  179. }
  180. return NOTIFY_OK;
  181. }
  182. static struct notifier_block blk_iopoll_cpu_notifier = {
  183. .notifier_call = blk_iopoll_cpu_notify,
  184. };
  185. static __init int blk_iopoll_setup(void)
  186. {
  187. int i;
  188. for_each_possible_cpu(i)
  189. INIT_LIST_HEAD(&per_cpu(blk_cpu_iopoll, i));
  190. open_softirq(BLOCK_IOPOLL_SOFTIRQ, blk_iopoll_softirq);
  191. register_hotcpu_notifier(&blk_iopoll_cpu_notifier);
  192. return 0;
  193. }
  194. subsys_initcall(blk_iopoll_setup);