Blame view

block/blk-softirq.c 4.29 KB
b646fc59b   Jens Axboe   block: split soft...
1
2
3
4
5
6
7
8
9
10
  /*
   * 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>
39be35012   Peter Zijlstra   sched, block: Uni...
11
  #include <linux/sched.h>
b646fc59b   Jens Axboe   block: split soft...
12
13
14
15
  
  #include "blk.h"
  
  static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
c7c22e4d5   Jens Axboe   block: add suppor...
16
17
18
19
  /*
   * Softirq action handler - move entries to local list and loop over them
   * while passing them to the queue registered handler.
   */
0766f788e   Emese Revfy   latent_entropy: M...
20
  static __latent_entropy void blk_done_softirq(struct softirq_action *h)
c7c22e4d5   Jens Axboe   block: add suppor...
21
22
23
24
  {
  	struct list_head *cpu_list, local_list;
  
  	local_irq_disable();
170d800af   Christoph Lameter   block: Replace __...
25
  	cpu_list = this_cpu_ptr(&blk_cpu_done);
c7c22e4d5   Jens Axboe   block: add suppor...
26
27
28
29
30
  	list_replace_init(cpu_list, &local_list);
  	local_irq_enable();
  
  	while (!list_empty(&local_list)) {
  		struct request *rq;
360f92c24   Jens Axboe   block: fix regres...
31
32
  		rq = list_entry(local_list.next, struct request, ipi_list);
  		list_del_init(&rq->ipi_list);
c7c22e4d5   Jens Axboe   block: add suppor...
33
34
35
  		rq->q->softirq_done_fn(rq);
  	}
  }
0a06ff068   Christoph Hellwig   kernel: remove CO...
36
  #ifdef CONFIG_SMP
c7c22e4d5   Jens Axboe   block: add suppor...
37
38
39
40
41
42
43
  static void trigger_softirq(void *data)
  {
  	struct request *rq = data;
  	unsigned long flags;
  	struct list_head *list;
  
  	local_irq_save(flags);
170d800af   Christoph Lameter   block: Replace __...
44
  	list = this_cpu_ptr(&blk_cpu_done);
360f92c24   Jens Axboe   block: fix regres...
45
  	list_add_tail(&rq->ipi_list, list);
c7c22e4d5   Jens Axboe   block: add suppor...
46

360f92c24   Jens Axboe   block: fix regres...
47
  	if (list->next == &rq->ipi_list)
c7c22e4d5   Jens Axboe   block: add suppor...
48
49
50
51
52
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)) {
  		struct call_single_data *data = &rq->csd;
  
  		data->func = trigger_softirq;
  		data->info = rq;
  		data->flags = 0;
c46fff2a3   Frederic Weisbecker   smp: Rename __smp...
64
  		smp_call_function_single_async(cpu, data);
c7c22e4d5   Jens Axboe   block: add suppor...
65
66
67
68
69
  		return 0;
  	}
  
  	return 1;
  }
0a06ff068   Christoph Hellwig   kernel: remove CO...
70
  #else /* CONFIG_SMP */
c7c22e4d5   Jens Axboe   block: add suppor...
71
72
73
74
75
  static int raise_blk_irq(int cpu, struct request *rq)
  {
  	return 1;
  }
  #endif
9a659f43d   Sebastian Andrzej Siewior   block/softirq: Co...
76
  static int blk_softirq_cpu_dead(unsigned int cpu)
b646fc59b   Jens Axboe   block: split soft...
77
78
79
80
81
  {
  	/*
  	 * If a CPU goes away, splice its entries to the current CPU
  	 * and trigger a run of the softirq
  	 */
9a659f43d   Sebastian Andrzej Siewior   block/softirq: Co...
82
83
84
85
86
  	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();
b646fc59b   Jens Axboe   block: split soft...
87

9a659f43d   Sebastian Andrzej Siewior   block/softirq: Co...
88
  	return 0;
b646fc59b   Jens Axboe   block: split soft...
89
  }
242f9dcb8   Jens Axboe   block: unify requ...
90
  void __blk_complete_request(struct request *req)
b646fc59b   Jens Axboe   block: split soft...
91
  {
39be35012   Peter Zijlstra   sched, block: Uni...
92
  	int ccpu, cpu;
c7c22e4d5   Jens Axboe   block: add suppor...
93
  	struct request_queue *q = req->q;
b646fc59b   Jens Axboe   block: split soft...
94
  	unsigned long flags;
39be35012   Peter Zijlstra   sched, block: Uni...
95
  	bool shared = false;
b646fc59b   Jens Axboe   block: split soft...
96

c7c22e4d5   Jens Axboe   block: add suppor...
97
  	BUG_ON(!q->softirq_done_fn);
b646fc59b   Jens Axboe   block: split soft...
98
99
  
  	local_irq_save(flags);
c7c22e4d5   Jens Axboe   block: add suppor...
100
  	cpu = smp_processor_id();
b646fc59b   Jens Axboe   block: split soft...
101

c7c22e4d5   Jens Axboe   block: add suppor...
102
103
104
  	/*
  	 * Select completion CPU
  	 */
8ad6a56f5   Tao Ma   block: Don't chec...
105
  	if (req->cpu != -1) {
c7c22e4d5   Jens Axboe   block: add suppor...
106
  		ccpu = req->cpu;
39be35012   Peter Zijlstra   sched, block: Uni...
107
108
  		if (!test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags))
  			shared = cpus_share_cache(cpu, ccpu);
5757a6d76   Dan Williams   block: strict rq_...
109
  	} else
c7c22e4d5   Jens Axboe   block: add suppor...
110
  		ccpu = cpu;
bcf30e75b   Shaohua Li   block: improve rq...
111
  	/*
39be35012   Peter Zijlstra   sched, block: Uni...
112
113
  	 * If current CPU and requested CPU share a cache, run the softirq on
  	 * the current CPU. One might concern this is just like
bcf30e75b   Shaohua Li   block: improve rq...
114
115
116
117
118
  	 * 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.
  	 */
39be35012   Peter Zijlstra   sched, block: Uni...
119
  	if (ccpu == cpu || shared) {
c7c22e4d5   Jens Axboe   block: add suppor...
120
121
  		struct list_head *list;
  do_local:
170d800af   Christoph Lameter   block: Replace __...
122
  		list = this_cpu_ptr(&blk_cpu_done);
360f92c24   Jens Axboe   block: fix regres...
123
  		list_add_tail(&req->ipi_list, list);
c7c22e4d5   Jens Axboe   block: add suppor...
124
125
126
127
128
129
130
  
  		/*
  		 * 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.
  		 */
360f92c24   Jens Axboe   block: fix regres...
131
  		if (list->next == &req->ipi_list)
c7c22e4d5   Jens Axboe   block: add suppor...
132
133
134
  			raise_softirq_irqoff(BLOCK_SOFTIRQ);
  	} else if (raise_blk_irq(ccpu, req))
  		goto do_local;
b646fc59b   Jens Axboe   block: split soft...
135
136
137
  
  	local_irq_restore(flags);
  }
242f9dcb8   Jens Axboe   block: unify requ...
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  
  /**
   * 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)
  {
581d4e28d   Jens Axboe   block: add fault ...
152
153
  	if (unlikely(blk_should_fake_timeout(req->q)))
  		return;
242f9dcb8   Jens Axboe   block: unify requ...
154
155
156
  	if (!blk_mark_rq_complete(req))
  		__blk_complete_request(req);
  }
b646fc59b   Jens Axboe   block: split soft...
157
  EXPORT_SYMBOL(blk_complete_request);
3c18ce71a   Roel Kluin   block: make blk_s...
158
  static __init int blk_softirq_init(void)
b646fc59b   Jens Axboe   block: split soft...
159
160
161
162
163
164
165
  {
  	int i;
  
  	for_each_possible_cpu(i)
  		INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
  
  	open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
9a659f43d   Sebastian Andrzej Siewior   block/softirq: Co...
166
167
168
  	cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
  				  "block/softirq:dead", NULL,
  				  blk_softirq_cpu_dead);
b646fc59b   Jens Axboe   block: split soft...
169
170
171
  	return 0;
  }
  subsys_initcall(blk_softirq_init);