Commit | Line | Data |
---|---|---|
5e605b64 JA |
1 | /* |
2 | * Functions related to interrupt-poll handling in the block layer. This | |
3 | * is similar to NAPI for network devices. | |
4 | */ | |
5 | #include <linux/kernel.h> | |
6 | #include <linux/module.h> | |
7 | #include <linux/init.h> | |
8 | #include <linux/bio.h> | |
5e605b64 JA |
9 | #include <linux/interrupt.h> |
10 | #include <linux/cpu.h> | |
511cbce2 | 11 | #include <linux/irq_poll.h> |
5e605b64 JA |
12 | #include <linux/delay.h> |
13 | ||
511cbce2 | 14 | static unsigned int irq_poll_budget __read_mostly = 256; |
37867ae7 | 15 | |
5e605b64 JA |
16 | static DEFINE_PER_CPU(struct list_head, blk_cpu_iopoll); |
17 | ||
18 | /** | |
511cbce2 | 19 | * irq_poll_sched - Schedule a run of the iopoll handler |
5e605b64 JA |
20 | * @iop: The parent iopoll structure |
21 | * | |
22 | * Description: | |
511cbce2 | 23 | * Add this irq_poll structure to the pending poll list and trigger the |
ea51190c | 24 | * raise of the blk iopoll softirq. |
5e605b64 | 25 | **/ |
511cbce2 | 26 | void irq_poll_sched(struct irq_poll *iop) |
5e605b64 JA |
27 | { |
28 | unsigned long flags; | |
29 | ||
ea51190c CH |
30 | if (test_bit(IRQ_POLL_F_DISABLE, &iop->state)) |
31 | return; | |
2ee177e9 | 32 | if (test_and_set_bit(IRQ_POLL_F_SCHED, &iop->state)) |
ea51190c CH |
33 | return; |
34 | ||
5e605b64 | 35 | local_irq_save(flags); |
170d800a | 36 | list_add_tail(&iop->list, this_cpu_ptr(&blk_cpu_iopoll)); |
511cbce2 | 37 | __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ); |
5e605b64 JA |
38 | local_irq_restore(flags); |
39 | } | |
511cbce2 | 40 | EXPORT_SYMBOL(irq_poll_sched); |
5e605b64 JA |
41 | |
42 | /** | |
511cbce2 | 43 | * __irq_poll_complete - Mark this @iop as un-polled again |
5e605b64 JA |
44 | * @iop: The parent iopoll structure |
45 | * | |
46 | * Description: | |
511cbce2 | 47 | * See irq_poll_complete(). This function must be called with interrupts |
1badcfbd | 48 | * disabled. |
5e605b64 | 49 | **/ |
83af187d | 50 | static void __irq_poll_complete(struct irq_poll *iop) |
5e605b64 JA |
51 | { |
52 | list_del(&iop->list); | |
4e857c58 | 53 | smp_mb__before_atomic(); |
511cbce2 | 54 | clear_bit_unlock(IRQ_POLL_F_SCHED, &iop->state); |
5e605b64 | 55 | } |
5e605b64 JA |
56 | |
57 | /** | |
511cbce2 | 58 | * irq_poll_complete - Mark this @iop as un-polled again |
5e605b64 JA |
59 | * @iop: The parent iopoll structure |
60 | * | |
61 | * Description: | |
1badcfbd JA |
62 | * If a driver consumes less than the assigned budget in its run of the |
63 | * iopoll handler, it'll end the polled mode by calling this function. The | |
ea51190c | 64 | * iopoll handler will not be invoked again before irq_poll_sched() |
1badcfbd | 65 | * is called. |
5e605b64 | 66 | **/ |
511cbce2 | 67 | void irq_poll_complete(struct irq_poll *iop) |
5e605b64 JA |
68 | { |
69 | unsigned long flags; | |
70 | ||
71 | local_irq_save(flags); | |
511cbce2 | 72 | __irq_poll_complete(iop); |
5e605b64 JA |
73 | local_irq_restore(flags); |
74 | } | |
511cbce2 | 75 | EXPORT_SYMBOL(irq_poll_complete); |
5e605b64 | 76 | |
511cbce2 | 77 | static void irq_poll_softirq(struct softirq_action *h) |
5e605b64 | 78 | { |
170d800a | 79 | struct list_head *list = this_cpu_ptr(&blk_cpu_iopoll); |
511cbce2 | 80 | int rearm = 0, budget = irq_poll_budget; |
5e605b64 | 81 | unsigned long start_time = jiffies; |
5e605b64 JA |
82 | |
83 | local_irq_disable(); | |
84 | ||
85 | while (!list_empty(list)) { | |
511cbce2 | 86 | struct irq_poll *iop; |
5e605b64 JA |
87 | int work, weight; |
88 | ||
89 | /* | |
90 | * If softirq window is exhausted then punt. | |
91 | */ | |
92 | if (budget <= 0 || time_after(jiffies, start_time)) { | |
93 | rearm = 1; | |
94 | break; | |
95 | } | |
96 | ||
97 | local_irq_enable(); | |
98 | ||
99 | /* Even though interrupts have been re-enabled, this | |
100 | * access is safe because interrupts can only add new | |
101 | * entries to the tail of this list, and only ->poll() | |
102 | * calls can remove this head entry from the list. | |
103 | */ | |
511cbce2 | 104 | iop = list_entry(list->next, struct irq_poll, list); |
5e605b64 JA |
105 | |
106 | weight = iop->weight; | |
107 | work = 0; | |
511cbce2 | 108 | if (test_bit(IRQ_POLL_F_SCHED, &iop->state)) |
5e605b64 JA |
109 | work = iop->poll(iop, weight); |
110 | ||
111 | budget -= work; | |
112 | ||
113 | local_irq_disable(); | |
114 | ||
fca51d64 JA |
115 | /* |
116 | * Drivers must not modify the iopoll state, if they | |
117 | * consume their assigned weight (or more, some drivers can't | |
118 | * easily just stop processing, they have to complete an | |
119 | * entire mask of commands).In such cases this code | |
120 | * still "owns" the iopoll instance and therefore can | |
5e605b64 JA |
121 | * move the instance around on the list at-will. |
122 | */ | |
123 | if (work >= weight) { | |
0bc92ace | 124 | if (test_bit(IRQ_POLL_F_DISABLE, &iop->state)) |
511cbce2 | 125 | __irq_poll_complete(iop); |
5e605b64 JA |
126 | else |
127 | list_move_tail(&iop->list, list); | |
128 | } | |
129 | } | |
130 | ||
131 | if (rearm) | |
511cbce2 | 132 | __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ); |
5e605b64 JA |
133 | |
134 | local_irq_enable(); | |
135 | } | |
136 | ||
137 | /** | |
511cbce2 | 138 | * irq_poll_disable - Disable iopoll on this @iop |
5e605b64 JA |
139 | * @iop: The parent iopoll structure |
140 | * | |
141 | * Description: | |
142 | * Disable io polling and wait for any pending callbacks to have completed. | |
143 | **/ | |
511cbce2 | 144 | void irq_poll_disable(struct irq_poll *iop) |
5e605b64 | 145 | { |
511cbce2 CH |
146 | set_bit(IRQ_POLL_F_DISABLE, &iop->state); |
147 | while (test_and_set_bit(IRQ_POLL_F_SCHED, &iop->state)) | |
5e605b64 | 148 | msleep(1); |
511cbce2 | 149 | clear_bit(IRQ_POLL_F_DISABLE, &iop->state); |
5e605b64 | 150 | } |
511cbce2 | 151 | EXPORT_SYMBOL(irq_poll_disable); |
5e605b64 JA |
152 | |
153 | /** | |
511cbce2 | 154 | * irq_poll_enable - Enable iopoll on this @iop |
5e605b64 JA |
155 | * @iop: The parent iopoll structure |
156 | * | |
157 | * Description: | |
1badcfbd JA |
158 | * Enable iopoll on this @iop. Note that the handler run will not be |
159 | * scheduled, it will only mark it as active. | |
5e605b64 | 160 | **/ |
511cbce2 | 161 | void irq_poll_enable(struct irq_poll *iop) |
5e605b64 | 162 | { |
511cbce2 | 163 | BUG_ON(!test_bit(IRQ_POLL_F_SCHED, &iop->state)); |
4e857c58 | 164 | smp_mb__before_atomic(); |
511cbce2 | 165 | clear_bit_unlock(IRQ_POLL_F_SCHED, &iop->state); |
5e605b64 | 166 | } |
511cbce2 | 167 | EXPORT_SYMBOL(irq_poll_enable); |
5e605b64 JA |
168 | |
169 | /** | |
511cbce2 | 170 | * irq_poll_init - Initialize this @iop |
5e605b64 JA |
171 | * @iop: The parent iopoll structure |
172 | * @weight: The default weight (or command completion budget) | |
173 | * @poll_fn: The handler to invoke | |
174 | * | |
175 | * Description: | |
78d0264e | 176 | * Initialize and enable this irq_poll structure. |
5e605b64 | 177 | **/ |
511cbce2 | 178 | void irq_poll_init(struct irq_poll *iop, int weight, irq_poll_fn *poll_fn) |
5e605b64 JA |
179 | { |
180 | memset(iop, 0, sizeof(*iop)); | |
181 | INIT_LIST_HEAD(&iop->list); | |
182 | iop->weight = weight; | |
183 | iop->poll = poll_fn; | |
5e605b64 | 184 | } |
511cbce2 | 185 | EXPORT_SYMBOL(irq_poll_init); |
5e605b64 | 186 | |
511cbce2 | 187 | static int irq_poll_cpu_notify(struct notifier_block *self, |
0b776b06 | 188 | unsigned long action, void *hcpu) |
5e605b64 JA |
189 | { |
190 | /* | |
191 | * If a CPU goes away, splice its entries to the current CPU | |
192 | * and trigger a run of the softirq | |
193 | */ | |
194 | if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) { | |
195 | int cpu = (unsigned long) hcpu; | |
196 | ||
197 | local_irq_disable(); | |
198 | list_splice_init(&per_cpu(blk_cpu_iopoll, cpu), | |
170d800a | 199 | this_cpu_ptr(&blk_cpu_iopoll)); |
511cbce2 | 200 | __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ); |
5e605b64 JA |
201 | local_irq_enable(); |
202 | } | |
203 | ||
204 | return NOTIFY_OK; | |
205 | } | |
206 | ||
511cbce2 CH |
207 | static struct notifier_block irq_poll_cpu_notifier = { |
208 | .notifier_call = irq_poll_cpu_notify, | |
5e605b64 JA |
209 | }; |
210 | ||
511cbce2 | 211 | static __init int irq_poll_setup(void) |
5e605b64 JA |
212 | { |
213 | int i; | |
214 | ||
215 | for_each_possible_cpu(i) | |
216 | INIT_LIST_HEAD(&per_cpu(blk_cpu_iopoll, i)); | |
217 | ||
511cbce2 CH |
218 | open_softirq(IRQ_POLL_SOFTIRQ, irq_poll_softirq); |
219 | register_hotcpu_notifier(&irq_poll_cpu_notifier); | |
5e605b64 JA |
220 | return 0; |
221 | } | |
511cbce2 | 222 | subsys_initcall(irq_poll_setup); |