blk-softirq.c 4.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9 10 11
/*
 * Functions related to softirq rq completions
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/bio.h>
#include <linux/blkdev.h>
#include <linux/interrupt.h>
#include <linux/cpu.h>
12
#include <linux/sched.h>
13
#include <linux/sched/topology.h>
14 15 16 17 18

#include "blk.h"

static DEFINE_PER_CPU(struct list_head, blk_cpu_done);

19 20 21 22
/*
 * Softirq action handler - move entries to local list and loop over them
 * while passing them to the queue registered handler.
 */
23
static __latent_entropy void blk_done_softirq(struct softirq_action *h)
24 25 26 27
{
	struct list_head *cpu_list, local_list;

	local_irq_disable();
28
	cpu_list = this_cpu_ptr(&blk_cpu_done);
29 30 31 32 33 34
	list_replace_init(cpu_list, &local_list);
	local_irq_enable();

	while (!list_empty(&local_list)) {
		struct request *rq;

35 36
		rq = list_entry(local_list.next, struct request, ipi_list);
		list_del_init(&rq->ipi_list);
37 38 39 40
		rq->q->softirq_done_fn(rq);
	}
}

41
#ifdef CONFIG_SMP
42 43 44 45 46 47 48
static void trigger_softirq(void *data)
{
	struct request *rq = data;
	unsigned long flags;
	struct list_head *list;

	local_irq_save(flags);
49
	list = this_cpu_ptr(&blk_cpu_done);
50
	list_add_tail(&rq->ipi_list, list);
51

52
	if (list->next == &rq->ipi_list)
53 54 55 56 57 58 59 60 61 62 63
		raise_softirq_irqoff(BLOCK_SOFTIRQ);

	local_irq_restore(flags);
}

/*
 * Setup and invoke a run of 'trigger_softirq' on the given cpu.
 */
static int raise_blk_irq(int cpu, struct request *rq)
{
	if (cpu_online(cpu)) {
64
		call_single_data_t *data = &rq->csd;
65 66 67 68 69

		data->func = trigger_softirq;
		data->info = rq;
		data->flags = 0;

70
		smp_call_function_single_async(cpu, data);
71 72 73 74 75
		return 0;
	}

	return 1;
}
76
#else /* CONFIG_SMP */
77 78 79 80 81 82
static int raise_blk_irq(int cpu, struct request *rq)
{
	return 1;
}
#endif

83
static int blk_softirq_cpu_dead(unsigned int cpu)
84 85 86 87 88
{
	/*
	 * If a CPU goes away, splice its entries to the current CPU
	 * and trigger a run of the softirq
	 */
89 90 91 92 93
	local_irq_disable();
	list_splice_init(&per_cpu(blk_cpu_done, cpu),
			 this_cpu_ptr(&blk_cpu_done));
	raise_softirq_irqoff(BLOCK_SOFTIRQ);
	local_irq_enable();
94

95
	return 0;
96 97
}

J
Jens Axboe 已提交
98
void __blk_complete_request(struct request *req)
99
{
100
	int ccpu, cpu;
101
	struct request_queue *q = req->q;
102
	unsigned long flags;
103
	bool shared = false;
104

105
	BUG_ON(!q->softirq_done_fn);
106 107

	local_irq_save(flags);
108
	cpu = smp_processor_id();
109

110 111 112
	/*
	 * Select completion CPU
	 */
113
	if (req->cpu != -1) {
114
		ccpu = req->cpu;
115 116
		if (!test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags))
			shared = cpus_share_cache(cpu, ccpu);
D
Dan Williams 已提交
117
	} else
118 119
		ccpu = cpu;

120
	/*
121 122
	 * If current CPU and requested CPU share a cache, run the softirq on
	 * the current CPU. One might concern this is just like
123 124 125 126 127
	 * QUEUE_FLAG_SAME_FORCE, but actually not. blk_complete_request() is
	 * running in interrupt handler, and currently I/O controller doesn't
	 * support multiple interrupts, so current CPU is unique actually. This
	 * avoids IPI sending from current CPU to the first CPU of a group.
	 */
128
	if (ccpu == cpu || shared) {
129 130
		struct list_head *list;
do_local:
131
		list = this_cpu_ptr(&blk_cpu_done);
132
		list_add_tail(&req->ipi_list, list);
133 134 135 136 137 138 139

		/*
		 * if the list only contains our just added request,
		 * signal a raise of the softirq. If there are already
		 * entries there, someone already raised the irq but it
		 * hasn't run yet.
		 */
140
		if (list->next == &req->ipi_list)
141 142 143
			raise_softirq_irqoff(BLOCK_SOFTIRQ);
	} else if (raise_blk_irq(ccpu, req))
		goto do_local;
144 145 146

	local_irq_restore(flags);
}
147
EXPORT_SYMBOL(__blk_complete_request);
J
Jens Axboe 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161

/**
 * blk_complete_request - end I/O on a request
 * @req:      the request being processed
 *
 * Description:
 *     Ends all I/O on a request. It does not handle partial completions,
 *     unless the driver actually implements this in its completion callback
 *     through requeueing. The actual completion happens out-of-order,
 *     through a softirq handler. The user must have registered a completion
 *     callback through blk_queue_softirq_done().
 **/
void blk_complete_request(struct request *req)
{
162 163
	if (unlikely(blk_should_fake_timeout(req->q)))
		return;
J
Jens Axboe 已提交
164 165 166
	if (!blk_mark_rq_complete(req))
		__blk_complete_request(req);
}
167 168
EXPORT_SYMBOL(blk_complete_request);

R
Roel Kluin 已提交
169
static __init int blk_softirq_init(void)
170 171 172 173 174 175 176
{
	int i;

	for_each_possible_cpu(i)
		INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));

	open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
177 178 179
	cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
				  "block/softirq:dead", NULL,
				  blk_softirq_cpu_dead);
180 181 182
	return 0;
}
subsys_initcall(blk_softirq_init);