blob: 3fe236829aeba73bdb5ce3caa87bdb7eb0d9bb73 [file] [log] [blame]
Jens Axboeb646fc52008-07-28 13:06:00 +02001/*
2 * Functions related to softirq rq completions
3 */
4#include <linux/kernel.h>
5#include <linux/module.h>
6#include <linux/init.h>
7#include <linux/bio.h>
8#include <linux/blkdev.h>
9#include <linux/interrupt.h>
10#include <linux/cpu.h>
Peter Zijlstra39be3502012-01-26 12:44:34 +010011#include <linux/sched.h>
Jens Axboeb646fc52008-07-28 13:06:00 +020012
13#include "blk.h"
14
15static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
16
Jens Axboec7c22e42008-09-13 20:26:01 +020017/*
18 * Softirq action handler - move entries to local list and loop over them
19 * while passing them to the queue registered handler.
20 */
21static void blk_done_softirq(struct softirq_action *h)
22{
23 struct list_head *cpu_list, local_list;
24
25 local_irq_disable();
26 cpu_list = &__get_cpu_var(blk_cpu_done);
27 list_replace_init(cpu_list, &local_list);
28 local_irq_enable();
29
30 while (!list_empty(&local_list)) {
31 struct request *rq;
32
33 rq = list_entry(local_list.next, struct request, csd.list);
34 list_del_init(&rq->csd.list);
35 rq->q->softirq_done_fn(rq);
36 }
37}
38
39#if defined(CONFIG_SMP) && defined(CONFIG_USE_GENERIC_SMP_HELPERS)
40static void trigger_softirq(void *data)
41{
42 struct request *rq = data;
43 unsigned long flags;
44 struct list_head *list;
45
46 local_irq_save(flags);
47 list = &__get_cpu_var(blk_cpu_done);
48 list_add_tail(&rq->csd.list, list);
49
50 if (list->next == &rq->csd.list)
51 raise_softirq_irqoff(BLOCK_SOFTIRQ);
52
53 local_irq_restore(flags);
Thomas Gleixnerf5f0f292011-11-13 17:17:09 +010054 preempt_check_resched_rt();
Jens Axboec7c22e42008-09-13 20:26:01 +020055}
56
57/*
58 * Setup and invoke a run of 'trigger_softirq' on the given cpu.
59 */
60static int raise_blk_irq(int cpu, struct request *rq)
61{
62 if (cpu_online(cpu)) {
63 struct call_single_data *data = &rq->csd;
64
65 data->func = trigger_softirq;
66 data->info = rq;
67 data->flags = 0;
68
Peter Zijlstra6e275632009-02-25 13:59:48 +010069 __smp_call_function_single(cpu, data, 0);
Jens Axboec7c22e42008-09-13 20:26:01 +020070 return 0;
71 }
72
73 return 1;
74}
75#else /* CONFIG_SMP && CONFIG_USE_GENERIC_SMP_HELPERS */
76static int raise_blk_irq(int cpu, struct request *rq)
77{
78 return 1;
79}
80#endif
81
Jens Axboeb646fc52008-07-28 13:06:00 +020082static int __cpuinit blk_cpu_notify(struct notifier_block *self,
83 unsigned long action, void *hcpu)
84{
85 /*
86 * If a CPU goes away, splice its entries to the current CPU
87 * and trigger a run of the softirq
88 */
89 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
90 int cpu = (unsigned long) hcpu;
91
92 local_irq_disable();
93 list_splice_init(&per_cpu(blk_cpu_done, cpu),
94 &__get_cpu_var(blk_cpu_done));
95 raise_softirq_irqoff(BLOCK_SOFTIRQ);
96 local_irq_enable();
Thomas Gleixnerf5f0f292011-11-13 17:17:09 +010097 preempt_check_resched_rt();
Jens Axboeb646fc52008-07-28 13:06:00 +020098 }
99
100 return NOTIFY_OK;
101}
102
Jens Axboec7c22e42008-09-13 20:26:01 +0200103static struct notifier_block __cpuinitdata blk_cpu_notifier = {
Jens Axboeb646fc52008-07-28 13:06:00 +0200104 .notifier_call = blk_cpu_notify,
105};
106
Jens Axboe242f9dc2008-09-14 05:55:09 -0700107void __blk_complete_request(struct request *req)
Jens Axboeb646fc52008-07-28 13:06:00 +0200108{
Peter Zijlstra39be3502012-01-26 12:44:34 +0100109 int ccpu, cpu;
Jens Axboec7c22e42008-09-13 20:26:01 +0200110 struct request_queue *q = req->q;
Jens Axboeb646fc52008-07-28 13:06:00 +0200111 unsigned long flags;
Peter Zijlstra39be3502012-01-26 12:44:34 +0100112 bool shared = false;
Jens Axboeb646fc52008-07-28 13:06:00 +0200113
Jens Axboec7c22e42008-09-13 20:26:01 +0200114 BUG_ON(!q->softirq_done_fn);
Jens Axboeb646fc52008-07-28 13:06:00 +0200115
116 local_irq_save(flags);
Jens Axboec7c22e42008-09-13 20:26:01 +0200117 cpu = smp_processor_id();
Jens Axboeb646fc52008-07-28 13:06:00 +0200118
Jens Axboec7c22e42008-09-13 20:26:01 +0200119 /*
120 * Select completion CPU
121 */
Tao Ma8ad6a562011-09-14 09:31:01 +0200122 if (req->cpu != -1) {
Jens Axboec7c22e42008-09-13 20:26:01 +0200123 ccpu = req->cpu;
Peter Zijlstra39be3502012-01-26 12:44:34 +0100124 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags))
125 shared = cpus_share_cache(cpu, ccpu);
Dan Williams5757a6d2011-07-23 20:44:25 +0200126 } else
Jens Axboec7c22e42008-09-13 20:26:01 +0200127 ccpu = cpu;
128
Shaohua Libcf30e72011-08-11 10:39:04 +0200129 /*
Peter Zijlstra39be3502012-01-26 12:44:34 +0100130 * If current CPU and requested CPU share a cache, run the softirq on
131 * the current CPU. One might concern this is just like
Shaohua Libcf30e72011-08-11 10:39:04 +0200132 * QUEUE_FLAG_SAME_FORCE, but actually not. blk_complete_request() is
133 * running in interrupt handler, and currently I/O controller doesn't
134 * support multiple interrupts, so current CPU is unique actually. This
135 * avoids IPI sending from current CPU to the first CPU of a group.
136 */
Peter Zijlstra39be3502012-01-26 12:44:34 +0100137 if (ccpu == cpu || shared) {
Jens Axboec7c22e42008-09-13 20:26:01 +0200138 struct list_head *list;
139do_local:
140 list = &__get_cpu_var(blk_cpu_done);
141 list_add_tail(&req->csd.list, list);
142
143 /*
144 * if the list only contains our just added request,
145 * signal a raise of the softirq. If there are already
146 * entries there, someone already raised the irq but it
147 * hasn't run yet.
148 */
149 if (list->next == &req->csd.list)
150 raise_softirq_irqoff(BLOCK_SOFTIRQ);
151 } else if (raise_blk_irq(ccpu, req))
152 goto do_local;
Jens Axboeb646fc52008-07-28 13:06:00 +0200153
154 local_irq_restore(flags);
Thomas Gleixnerf5f0f292011-11-13 17:17:09 +0100155 preempt_check_resched_rt();
Jens Axboeb646fc52008-07-28 13:06:00 +0200156}
Jens Axboe242f9dc2008-09-14 05:55:09 -0700157
158/**
159 * blk_complete_request - end I/O on a request
160 * @req: the request being processed
161 *
162 * Description:
163 * Ends all I/O on a request. It does not handle partial completions,
164 * unless the driver actually implements this in its completion callback
165 * through requeueing. The actual completion happens out-of-order,
166 * through a softirq handler. The user must have registered a completion
167 * callback through blk_queue_softirq_done().
168 **/
169void blk_complete_request(struct request *req)
170{
Jens Axboe581d4e22008-09-14 05:56:33 -0700171 if (unlikely(blk_should_fake_timeout(req->q)))
172 return;
Jens Axboe242f9dc2008-09-14 05:55:09 -0700173 if (!blk_mark_rq_complete(req))
174 __blk_complete_request(req);
175}
Jens Axboeb646fc52008-07-28 13:06:00 +0200176EXPORT_SYMBOL(blk_complete_request);
177
Roel Kluin3c18ce72008-12-10 15:47:33 +0100178static __init int blk_softirq_init(void)
Jens Axboeb646fc52008-07-28 13:06:00 +0200179{
180 int i;
181
182 for_each_possible_cpu(i)
183 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
184
185 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
186 register_hotcpu_notifier(&blk_cpu_notifier);
187 return 0;
188}
189subsys_initcall(blk_softirq_init);