blk-softirq.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. EXPORT_SYMBOL(__blk_complete_request);
  125. /**
  126. * blk_complete_request - end I/O on a request
  127. * @req: the request being processed
  128. *
  129. * Description:
  130. * Ends all I/O on a request. It does not handle partial completions,
  131. * unless the driver actually implements this in its completion callback
  132. * through requeueing. The actual completion happens out-of-order,
  133. * through a softirq handler. The user must have registered a completion
  134. * callback through blk_queue_softirq_done().
  135. **/
  136. void blk_complete_request(struct request *req)
  137. {
  138. if (unlikely(blk_should_fake_timeout(req->q)))
  139. return;
  140. if (!blk_mark_rq_complete(req))
  141. __blk_complete_request(req);
  142. }
  143. EXPORT_SYMBOL(blk_complete_request);
  144. static __init int blk_softirq_init(void)
  145. {
  146. int i;
  147. for_each_possible_cpu(i)
  148. INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
  149. open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
  150. cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
  151. "block/softirq:dead", NULL,
  152. blk_softirq_cpu_dead);
  153. return 0;
  154. }
  155. subsys_initcall(blk_softirq_init);