blk-iopoll.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. int blk_iopoll_enabled = 1;
  16. EXPORT_SYMBOL(blk_iopoll_enabled);
  17. static unsigned int blk_iopoll_budget __read_mostly = 256;
  18. static DEFINE_PER_CPU(struct list_head, blk_cpu_iopoll);
  19. /**
  20. * blk_iopoll_sched - Schedule a run of the iopoll handler
  21. * @iop: The parent iopoll structure
  22. *
  23. * Description:
  24. * Add this blk_iopoll structure to the pending poll list and trigger the
  25. * raise of the blk iopoll softirq. The driver must already have gotten a
  26. * successful return from blk_iopoll_sched_prep() before calling this.
  27. **/
  28. void blk_iopoll_sched(struct blk_iopoll *iop)
  29. {
  30. unsigned long flags;
  31. local_irq_save(flags);
  32. list_add_tail(&iop->list, &__get_cpu_var(blk_cpu_iopoll));
  33. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  34. local_irq_restore(flags);
  35. }
  36. EXPORT_SYMBOL(blk_iopoll_sched);
  37. /**
  38. * __blk_iopoll_complete - Mark this @iop as un-polled again
  39. * @iop: The parent iopoll structure
  40. *
  41. * Description:
  42. * See blk_iopoll_complete(). This function must be called with interrupts
  43. * disabled.
  44. **/
  45. void __blk_iopoll_complete(struct blk_iopoll *iop)
  46. {
  47. list_del(&iop->list);
  48. smp_mb__before_clear_bit();
  49. clear_bit_unlock(IOPOLL_F_SCHED, &iop->state);
  50. }
  51. EXPORT_SYMBOL(__blk_iopoll_complete);
  52. /**
  53. * blk_iopoll_complete - Mark this @iop as un-polled again
  54. * @iop: The parent iopoll structure
  55. *
  56. * Description:
  57. * If a driver consumes less than the assigned budget in its run of the
  58. * iopoll handler, it'll end the polled mode by calling this function. The
  59. * iopoll handler will not be invoked again before blk_iopoll_sched_prep()
  60. * is called.
  61. **/
  62. void blk_iopoll_complete(struct blk_iopoll *iopoll)
  63. {
  64. unsigned long flags;
  65. local_irq_save(flags);
  66. __blk_iopoll_complete(iopoll);
  67. local_irq_restore(flags);
  68. }
  69. EXPORT_SYMBOL(blk_iopoll_complete);
  70. static void blk_iopoll_softirq(struct softirq_action *h)
  71. {
  72. struct list_head *list = &__get_cpu_var(blk_cpu_iopoll);
  73. int rearm = 0, budget = blk_iopoll_budget;
  74. unsigned long start_time = jiffies;
  75. local_irq_disable();
  76. while (!list_empty(list)) {
  77. struct blk_iopoll *iop;
  78. int work, weight;
  79. /*
  80. * If softirq window is exhausted then punt.
  81. */
  82. if (budget <= 0 || time_after(jiffies, start_time)) {
  83. rearm = 1;
  84. break;
  85. }
  86. local_irq_enable();
  87. /* Even though interrupts have been re-enabled, this
  88. * access is safe because interrupts can only add new
  89. * entries to the tail of this list, and only ->poll()
  90. * calls can remove this head entry from the list.
  91. */
  92. iop = list_entry(list->next, struct blk_iopoll, list);
  93. weight = iop->weight;
  94. work = 0;
  95. if (test_bit(IOPOLL_F_SCHED, &iop->state))
  96. work = iop->poll(iop, weight);
  97. budget -= work;
  98. local_irq_disable();
  99. /*
  100. * Drivers must not modify the iopoll state, if they
  101. * consume their assigned weight (or more, some drivers can't
  102. * easily just stop processing, they have to complete an
  103. * entire mask of commands).In such cases this code
  104. * still "owns" the iopoll instance and therefore can
  105. * move the instance around on the list at-will.
  106. */
  107. if (work >= weight) {
  108. if (blk_iopoll_disable_pending(iop))
  109. __blk_iopoll_complete(iop);
  110. else
  111. list_move_tail(&iop->list, list);
  112. }
  113. }
  114. if (rearm)
  115. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  116. local_irq_enable();
  117. }
  118. /**
  119. * blk_iopoll_disable - Disable iopoll on this @iop
  120. * @iop: The parent iopoll structure
  121. *
  122. * Description:
  123. * Disable io polling and wait for any pending callbacks to have completed.
  124. **/
  125. void blk_iopoll_disable(struct blk_iopoll *iop)
  126. {
  127. set_bit(IOPOLL_F_DISABLE, &iop->state);
  128. while (test_and_set_bit(IOPOLL_F_SCHED, &iop->state))
  129. msleep(1);
  130. clear_bit(IOPOLL_F_DISABLE, &iop->state);
  131. }
  132. EXPORT_SYMBOL(blk_iopoll_disable);
  133. /**
  134. * blk_iopoll_enable - Enable iopoll on this @iop
  135. * @iop: The parent iopoll structure
  136. *
  137. * Description:
  138. * Enable iopoll on this @iop. Note that the handler run will not be
  139. * scheduled, it will only mark it as active.
  140. **/
  141. void blk_iopoll_enable(struct blk_iopoll *iop)
  142. {
  143. BUG_ON(!test_bit(IOPOLL_F_SCHED, &iop->state));
  144. smp_mb__before_clear_bit();
  145. clear_bit_unlock(IOPOLL_F_SCHED, &iop->state);
  146. }
  147. EXPORT_SYMBOL(blk_iopoll_enable);
  148. /**
  149. * blk_iopoll_init - Initialize this @iop
  150. * @iop: The parent iopoll structure
  151. * @weight: The default weight (or command completion budget)
  152. * @poll_fn: The handler to invoke
  153. *
  154. * Description:
  155. * Initialize this blk_iopoll structure. Before being actively used, the
  156. * driver must call blk_iopoll_enable().
  157. **/
  158. void blk_iopoll_init(struct blk_iopoll *iop, int weight, blk_iopoll_fn *poll_fn)
  159. {
  160. memset(iop, 0, sizeof(*iop));
  161. INIT_LIST_HEAD(&iop->list);
  162. iop->weight = weight;
  163. iop->poll = poll_fn;
  164. set_bit(IOPOLL_F_SCHED, &iop->state);
  165. }
  166. EXPORT_SYMBOL(blk_iopoll_init);
  167. static int __cpuinit blk_iopoll_cpu_notify(struct notifier_block *self,
  168. unsigned long action, void *hcpu)
  169. {
  170. /*
  171. * If a CPU goes away, splice its entries to the current CPU
  172. * and trigger a run of the softirq
  173. */
  174. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  175. int cpu = (unsigned long) hcpu;
  176. local_irq_disable();
  177. list_splice_init(&per_cpu(blk_cpu_iopoll, cpu),
  178. &__get_cpu_var(blk_cpu_iopoll));
  179. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  180. local_irq_enable();
  181. }
  182. return NOTIFY_OK;
  183. }
  184. static struct notifier_block __cpuinitdata blk_iopoll_cpu_notifier = {
  185. .notifier_call = blk_iopoll_cpu_notify,
  186. };
  187. static __init int blk_iopoll_setup(void)
  188. {
  189. int i;
  190. for_each_possible_cpu(i)
  191. INIT_LIST_HEAD(&per_cpu(blk_cpu_iopoll, i));
  192. open_softirq(BLOCK_IOPOLL_SOFTIRQ, blk_iopoll_softirq);
  193. register_hotcpu_notifier(&blk_iopoll_cpu_notifier);
  194. return 0;
  195. }
  196. subsys_initcall(blk_iopoll_setup);