blk-softirq.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Functions related to softirq rq completions
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/bio.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/cpu.h>
  11. #include "blk.h"
  12. static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
  13. /*
  14. * Softirq action handler - move entries to local list and loop over them
  15. * while passing them to the queue registered handler.
  16. */
  17. static void blk_done_softirq(struct softirq_action *h)
  18. {
  19. struct list_head *cpu_list, local_list;
  20. local_irq_disable();
  21. cpu_list = &__get_cpu_var(blk_cpu_done);
  22. list_replace_init(cpu_list, &local_list);
  23. local_irq_enable();
  24. while (!list_empty(&local_list)) {
  25. struct request *rq;
  26. rq = list_entry(local_list.next, struct request, csd.list);
  27. list_del_init(&rq->csd.list);
  28. rq->q->softirq_done_fn(rq);
  29. }
  30. }
  31. #if defined(CONFIG_SMP) && defined(CONFIG_USE_GENERIC_SMP_HELPERS)
  32. static void trigger_softirq(void *data)
  33. {
  34. struct request *rq = data;
  35. unsigned long flags;
  36. struct list_head *list;
  37. local_irq_save(flags);
  38. list = &__get_cpu_var(blk_cpu_done);
  39. list_add_tail(&rq->csd.list, list);
  40. if (list->next == &rq->csd.list)
  41. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  42. local_irq_restore(flags);
  43. }
  44. /*
  45. * Setup and invoke a run of 'trigger_softirq' on the given cpu.
  46. */
  47. static int raise_blk_irq(int cpu, struct request *rq)
  48. {
  49. if (cpu_online(cpu)) {
  50. struct call_single_data *data = &rq->csd;
  51. data->func = trigger_softirq;
  52. data->info = rq;
  53. data->flags = 0;
  54. __smp_call_function_single(cpu, data, 0);
  55. return 0;
  56. }
  57. return 1;
  58. }
  59. #else /* CONFIG_SMP && CONFIG_USE_GENERIC_SMP_HELPERS */
  60. static int raise_blk_irq(int cpu, struct request *rq)
  61. {
  62. return 1;
  63. }
  64. #endif
  65. static int __cpuinit blk_cpu_notify(struct notifier_block *self,
  66. unsigned long action, void *hcpu)
  67. {
  68. /*
  69. * If a CPU goes away, splice its entries to the current CPU
  70. * and trigger a run of the softirq
  71. */
  72. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  73. int cpu = (unsigned long) hcpu;
  74. local_irq_disable();
  75. list_splice_init(&per_cpu(blk_cpu_done, cpu),
  76. &__get_cpu_var(blk_cpu_done));
  77. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  78. local_irq_enable();
  79. }
  80. return NOTIFY_OK;
  81. }
  82. static struct notifier_block __cpuinitdata blk_cpu_notifier = {
  83. .notifier_call = blk_cpu_notify,
  84. };
  85. void __blk_complete_request(struct request *req)
  86. {
  87. int ccpu, cpu, group_cpu = NR_CPUS;
  88. struct request_queue *q = req->q;
  89. unsigned long flags;
  90. BUG_ON(!q->softirq_done_fn);
  91. local_irq_save(flags);
  92. cpu = smp_processor_id();
  93. /*
  94. * Select completion CPU
  95. */
  96. if (req->cpu != -1) {
  97. ccpu = req->cpu;
  98. if (!test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags)) {
  99. ccpu = blk_cpu_to_group(ccpu);
  100. group_cpu = blk_cpu_to_group(cpu);
  101. }
  102. } else
  103. ccpu = cpu;
  104. /*
  105. * If current CPU and requested CPU are in the same group, running
  106. * softirq in current CPU. One might concern this is just like
  107. * QUEUE_FLAG_SAME_FORCE, but actually not. blk_complete_request() is
  108. * running in interrupt handler, and currently I/O controller doesn't
  109. * support multiple interrupts, so current CPU is unique actually. This
  110. * avoids IPI sending from current CPU to the first CPU of a group.
  111. */
  112. if (ccpu == cpu || ccpu == group_cpu) {
  113. struct list_head *list;
  114. do_local:
  115. list = &__get_cpu_var(blk_cpu_done);
  116. list_add_tail(&req->csd.list, list);
  117. /*
  118. * if the list only contains our just added request,
  119. * signal a raise of the softirq. If there are already
  120. * entries there, someone already raised the irq but it
  121. * hasn't run yet.
  122. */
  123. if (list->next == &req->csd.list)
  124. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  125. } else if (raise_blk_irq(ccpu, req))
  126. goto do_local;
  127. local_irq_restore(flags);
  128. }
  129. /**
  130. * blk_complete_request - end I/O on a request
  131. * @req: the request being processed
  132. *
  133. * Description:
  134. * Ends all I/O on a request. It does not handle partial completions,
  135. * unless the driver actually implements this in its completion callback
  136. * through requeueing. The actual completion happens out-of-order,
  137. * through a softirq handler. The user must have registered a completion
  138. * callback through blk_queue_softirq_done().
  139. **/
  140. void blk_complete_request(struct request *req)
  141. {
  142. if (unlikely(blk_should_fake_timeout(req->q)))
  143. return;
  144. if (!blk_mark_rq_complete(req))
  145. __blk_complete_request(req);
  146. }
  147. EXPORT_SYMBOL(blk_complete_request);
  148. static __init int blk_softirq_init(void)
  149. {
  150. int i;
  151. for_each_possible_cpu(i)
  152. INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
  153. open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
  154. register_hotcpu_notifier(&blk_cpu_notifier);
  155. return 0;
  156. }
  157. subsys_initcall(blk_softirq_init);