2 * Functions related to interrupt-poll handling in the block layer. This
3 * is similar to NAPI for network devices.
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/blk-iopoll.h>
13 #include <linux/delay.h>
17 int blk_iopoll_enabled = 1;
18 EXPORT_SYMBOL(blk_iopoll_enabled);
20 static DEFINE_PER_CPU(struct list_head, blk_cpu_iopoll);
23 * blk_iopoll_sched - Schedule a run of the iopoll handler
24 * @iop: The parent iopoll structure
27 * Add this blk_iopoll structure to the pending poll list and trigger the raise
28 * of the blk iopoll softirq. The driver must already have gotten a succesful
29 * return from blk_iopoll_sched_prep() before calling this.
31 void blk_iopoll_sched(struct blk_iopoll *iop)
35 local_irq_save(flags);
36 list_add_tail(&iop->list, &__get_cpu_var(blk_cpu_iopoll));
37 __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
38 local_irq_restore(flags);
40 EXPORT_SYMBOL(blk_iopoll_sched);
43 * __blk_iopoll_complete - Mark this @iop as un-polled again
44 * @iop: The parent iopoll structure
47 * See blk_iopoll_complete(). This function must be called with interrupts disabled.
49 void __blk_iopoll_complete(struct blk_iopoll *iop)
52 smp_mb__before_clear_bit();
53 clear_bit_unlock(IOPOLL_F_SCHED, &iop->state);
55 EXPORT_SYMBOL(__blk_iopoll_complete);
58 * blk_iopoll_complete - Mark this @iop as un-polled again
59 * @iop: The parent iopoll structure
62 * If a driver consumes less than the assigned budget in its run of the iopoll
63 * handler, it'll end the polled mode by calling this function. The iopoll handler
64 * will not be invoked again before blk_iopoll_sched_prep() is called.
66 void blk_iopoll_complete(struct blk_iopoll *iopoll)
70 local_irq_save(flags);
71 __blk_iopoll_complete(iopoll);
72 local_irq_restore(flags);
74 EXPORT_SYMBOL(blk_iopoll_complete);
76 static void blk_iopoll_softirq(struct softirq_action *h)
78 struct list_head *list = &__get_cpu_var(blk_cpu_iopoll);
79 unsigned long start_time = jiffies;
80 int rearm = 0, budget = 64;
84 while (!list_empty(list)) {
85 struct blk_iopoll *iop;
89 * If softirq window is exhausted then punt.
91 if (budget <= 0 || time_after(jiffies, start_time)) {
98 /* Even though interrupts have been re-enabled, this
99 * access is safe because interrupts can only add new
100 * entries to the tail of this list, and only ->poll()
101 * calls can remove this head entry from the list.
103 iop = list_entry(list->next, struct blk_iopoll, list);
105 weight = iop->weight;
107 if (test_bit(IOPOLL_F_SCHED, &iop->state))
108 work = iop->poll(iop, weight);
114 /* Drivers must not modify the NAPI state if they
115 * consume the entire weight. In such cases this code
116 * still "owns" the NAPI instance and therefore can
117 * move the instance around on the list at-will.
119 if (work >= weight) {
120 if (blk_iopoll_disable_pending(iop))
121 __blk_iopoll_complete(iop);
123 list_move_tail(&iop->list, list);
128 __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
134 * blk_iopoll_disable - Disable iopoll on this @iop
135 * @iop: The parent iopoll structure
138 * Disable io polling and wait for any pending callbacks to have completed.
140 void blk_iopoll_disable(struct blk_iopoll *iop)
142 set_bit(IOPOLL_F_DISABLE, &iop->state);
143 while (test_and_set_bit(IOPOLL_F_SCHED, &iop->state))
145 clear_bit(IOPOLL_F_DISABLE, &iop->state);
147 EXPORT_SYMBOL(blk_iopoll_disable);
150 * blk_iopoll_enable - Enable iopoll on this @iop
151 * @iop: The parent iopoll structure
154 * Enable iopoll on this @iop. Note that the handler run will not be scheduled, it
155 * will only mark it as active.
157 void blk_iopoll_enable(struct blk_iopoll *iop)
159 BUG_ON(!test_bit(IOPOLL_F_SCHED, &iop->state));
160 smp_mb__before_clear_bit();
161 clear_bit_unlock(IOPOLL_F_SCHED, &iop->state);
163 EXPORT_SYMBOL(blk_iopoll_enable);
166 * blk_iopoll_init - Initialize this @iop
167 * @iop: The parent iopoll structure
168 * @weight: The default weight (or command completion budget)
169 * @poll_fn: The handler to invoke
172 * Initialize this blk_iopoll structure. Before being actively used, the driver
173 * must call blk_iopoll_enable().
175 void blk_iopoll_init(struct blk_iopoll *iop, int weight, blk_iopoll_fn *poll_fn)
177 memset(iop, 0, sizeof(*iop));
178 INIT_LIST_HEAD(&iop->list);
179 iop->weight = weight;
181 set_bit(IOPOLL_F_SCHED, &iop->state);
183 EXPORT_SYMBOL(blk_iopoll_init);
185 static int __cpuinit blk_iopoll_cpu_notify(struct notifier_block *self,
186 unsigned long action, void *hcpu)
189 * If a CPU goes away, splice its entries to the current CPU
190 * and trigger a run of the softirq
192 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
193 int cpu = (unsigned long) hcpu;
196 list_splice_init(&per_cpu(blk_cpu_iopoll, cpu),
197 &__get_cpu_var(blk_cpu_iopoll));
198 raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
205 static struct notifier_block __cpuinitdata blk_iopoll_cpu_notifier = {
206 .notifier_call = blk_iopoll_cpu_notify,
209 static __init int blk_iopoll_setup(void)
213 for_each_possible_cpu(i)
214 INIT_LIST_HEAD(&per_cpu(blk_cpu_iopoll, i));
216 open_softirq(BLOCK_IOPOLL_SOFTIRQ, blk_iopoll_softirq);
217 register_hotcpu_notifier(&blk_iopoll_cpu_notifier);
220 subsys_initcall(blk_iopoll_setup);