1 // SPDX-License-Identifier: GPL-2.0
3 * Functions related to softirq rq completions
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/init.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>
17 static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
20 * Softirq action handler - move entries to local list and loop over them
21 * while passing them to the queue registered handler.
23 static __latent_entropy void blk_done_softirq(struct softirq_action *h)
25 struct list_head *cpu_list, local_list;
28 cpu_list = this_cpu_ptr(&blk_cpu_done);
29 list_replace_init(cpu_list, &local_list);
32 while (!list_empty(&local_list)) {
35 rq = list_entry(local_list.next, struct request, ipi_list);
36 list_del_init(&rq->ipi_list);
37 rq->q->softirq_done_fn(rq);
42 static void trigger_softirq(void *data)
44 struct request *rq = data;
46 struct list_head *list;
48 local_irq_save(flags);
49 list = this_cpu_ptr(&blk_cpu_done);
50 list_add_tail(&rq->ipi_list, list);
52 if (list->next == &rq->ipi_list)
53 raise_softirq_irqoff(BLOCK_SOFTIRQ);
55 local_irq_restore(flags);
59 * Setup and invoke a run of 'trigger_softirq' on the given cpu.
61 static int raise_blk_irq(int cpu, struct request *rq)
63 if (cpu_online(cpu)) {
64 call_single_data_t *data = &rq->csd;
66 data->func = trigger_softirq;
70 smp_call_function_single_async(cpu, data);
76 #else /* CONFIG_SMP */
77 static int raise_blk_irq(int cpu, struct request *rq)
83 static int blk_softirq_cpu_dead(unsigned int cpu)
86 * If a CPU goes away, splice its entries to the current CPU
87 * and trigger a run of the softirq
90 list_splice_init(&per_cpu(blk_cpu_done, cpu),
91 this_cpu_ptr(&blk_cpu_done));
92 raise_softirq_irqoff(BLOCK_SOFTIRQ);
98 void __blk_complete_request(struct request *req)
101 struct request_queue *q = req->q;
105 BUG_ON(!q->softirq_done_fn);
107 local_irq_save(flags);
108 cpu = smp_processor_id();
111 * Select completion CPU
113 if (req->cpu != -1) {
115 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags))
116 shared = cpus_share_cache(cpu, ccpu);
121 * If current CPU and requested CPU share a cache, run the softirq on
122 * the current CPU. One might concern this is just like
123 * QUEUE_FLAG_SAME_FORCE, but actually not. blk_complete_request() is
124 * running in interrupt handler, and currently I/O controller doesn't
125 * support multiple interrupts, so current CPU is unique actually. This
126 * avoids IPI sending from current CPU to the first CPU of a group.
128 if (ccpu == cpu || shared) {
129 struct list_head *list;
131 list = this_cpu_ptr(&blk_cpu_done);
132 list_add_tail(&req->ipi_list, list);
135 * if the list only contains our just added request,
136 * signal a raise of the softirq. If there are already
137 * entries there, someone already raised the irq but it
140 if (list->next == &req->ipi_list)
141 raise_softirq_irqoff(BLOCK_SOFTIRQ);
142 } else if (raise_blk_irq(ccpu, req))
145 local_irq_restore(flags);
147 EXPORT_SYMBOL(__blk_complete_request);
150 * blk_complete_request - end I/O on a request
151 * @req: the request being processed
154 * Ends all I/O on a request. It does not handle partial completions,
155 * unless the driver actually implements this in its completion callback
156 * through requeueing. The actual completion happens out-of-order,
157 * through a softirq handler. The user must have registered a completion
158 * callback through blk_queue_softirq_done().
160 void blk_complete_request(struct request *req)
162 if (unlikely(blk_should_fake_timeout(req->q)))
164 if (!blk_mark_rq_complete(req))
165 __blk_complete_request(req);
167 EXPORT_SYMBOL(blk_complete_request);
169 static __init int blk_softirq_init(void)
173 for_each_possible_cpu(i)
174 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
176 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
177 cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
178 "block/softirq:dead", NULL,
179 blk_softirq_cpu_dead);
182 subsys_initcall(blk_softirq_init);