blk-softirq.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. struct request_queue *q = req->q;
  88. unsigned long flags;
  89. int ccpu, cpu, group_cpu;
  90. BUG_ON(!q->softirq_done_fn);
  91. local_irq_save(flags);
  92. cpu = smp_processor_id();
  93. group_cpu = blk_cpu_to_group(cpu);
  94. /*
  95. * Select completion CPU
  96. */
  97. if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) && req->cpu != -1)
  98. ccpu = req->cpu;
  99. else
  100. ccpu = cpu;
  101. if (ccpu == cpu || ccpu == group_cpu) {
  102. struct list_head *list;
  103. do_local:
  104. list = &__get_cpu_var(blk_cpu_done);
  105. list_add_tail(&req->csd.list, list);
  106. /*
  107. * if the list only contains our just added request,
  108. * signal a raise of the softirq. If there are already
  109. * entries there, someone already raised the irq but it
  110. * hasn't run yet.
  111. */
  112. if (list->next == &req->csd.list)
  113. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  114. } else if (raise_blk_irq(ccpu, req))
  115. goto do_local;
  116. local_irq_restore(flags);
  117. }
  118. /**
  119. * blk_complete_request - end I/O on a request
  120. * @req: the request being processed
  121. *
  122. * Description:
  123. * Ends all I/O on a request. It does not handle partial completions,
  124. * unless the driver actually implements this in its completion callback
  125. * through requeueing. The actual completion happens out-of-order,
  126. * through a softirq handler. The user must have registered a completion
  127. * callback through blk_queue_softirq_done().
  128. **/
  129. void blk_complete_request(struct request *req)
  130. {
  131. if (unlikely(blk_should_fake_timeout(req->q)))
  132. return;
  133. if (!blk_mark_rq_complete(req))
  134. __blk_complete_request(req);
  135. }
  136. EXPORT_SYMBOL(blk_complete_request);
  137. static __init int blk_softirq_init(void)
  138. {
  139. int i;
  140. for_each_possible_cpu(i)
  141. INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
  142. open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
  143. register_hotcpu_notifier(&blk_cpu_notifier);
  144. return 0;
  145. }
  146. subsys_initcall(blk_softirq_init);