blk-softirq.c 4.3 KB

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