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/interrupt.h>
10 #include <linux/cpu.h>
11 #include <linux/irq_poll.h>
12 #include <linux/delay.h>
14 static unsigned int irq_poll_budget __read_mostly
= 256;
16 static DEFINE_PER_CPU(struct list_head
, blk_cpu_iopoll
);
19 * irq_poll_sched - Schedule a run of the iopoll handler
20 * @iop: The parent iopoll structure
23 * Add this irq_poll structure to the pending poll list and trigger the
24 * raise of the blk iopoll softirq.
26 void irq_poll_sched(struct irq_poll
*iop
)
30 if (test_bit(IRQ_POLL_F_DISABLE
, &iop
->state
))
32 if (test_and_set_bit(IRQ_POLL_F_SCHED
, &iop
->state
))
35 local_irq_save(flags
);
36 list_add_tail(&iop
->list
, this_cpu_ptr(&blk_cpu_iopoll
));
37 __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ
);
38 local_irq_restore(flags
);
40 EXPORT_SYMBOL(irq_poll_sched
);
43 * __irq_poll_complete - Mark this @iop as un-polled again
44 * @iop: The parent iopoll structure
47 * See irq_poll_complete(). This function must be called with interrupts
50 static void __irq_poll_complete(struct irq_poll
*iop
)
53 smp_mb__before_atomic();
54 clear_bit_unlock(IRQ_POLL_F_SCHED
, &iop
->state
);
58 * irq_poll_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
63 * iopoll handler, it'll end the polled mode by calling this function. The
64 * iopoll handler will not be invoked again before irq_poll_sched()
67 void irq_poll_complete(struct irq_poll
*iop
)
71 local_irq_save(flags
);
72 __irq_poll_complete(iop
);
73 local_irq_restore(flags
);
75 EXPORT_SYMBOL(irq_poll_complete
);
77 static void __latent_entropy
irq_poll_softirq(struct softirq_action
*h
)
79 struct list_head
*list
= this_cpu_ptr(&blk_cpu_iopoll
);
80 int rearm
= 0, budget
= irq_poll_budget
;
81 unsigned long start_time
= jiffies
;
85 while (!list_empty(list
)) {
90 * If softirq window is exhausted then punt.
92 if (budget
<= 0 || time_after(jiffies
, start_time
)) {
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.
104 iop
= list_entry(list
->next
, struct irq_poll
, list
);
106 weight
= iop
->weight
;
108 if (test_bit(IRQ_POLL_F_SCHED
, &iop
->state
))
109 work
= iop
->poll(iop
, weight
);
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
121 * move the instance around on the list at-will.
123 if (work
>= weight
) {
124 if (test_bit(IRQ_POLL_F_DISABLE
, &iop
->state
))
125 __irq_poll_complete(iop
);
127 list_move_tail(&iop
->list
, list
);
132 __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ
);
138 * irq_poll_disable - Disable iopoll on this @iop
139 * @iop: The parent iopoll structure
142 * Disable io polling and wait for any pending callbacks to have completed.
144 void irq_poll_disable(struct irq_poll
*iop
)
146 set_bit(IRQ_POLL_F_DISABLE
, &iop
->state
);
147 while (test_and_set_bit(IRQ_POLL_F_SCHED
, &iop
->state
))
149 clear_bit(IRQ_POLL_F_DISABLE
, &iop
->state
);
151 EXPORT_SYMBOL(irq_poll_disable
);
154 * irq_poll_enable - Enable iopoll on this @iop
155 * @iop: The parent iopoll structure
158 * Enable iopoll on this @iop. Note that the handler run will not be
159 * scheduled, it will only mark it as active.
161 void irq_poll_enable(struct irq_poll
*iop
)
163 BUG_ON(!test_bit(IRQ_POLL_F_SCHED
, &iop
->state
));
164 smp_mb__before_atomic();
165 clear_bit_unlock(IRQ_POLL_F_SCHED
, &iop
->state
);
167 EXPORT_SYMBOL(irq_poll_enable
);
170 * irq_poll_init - Initialize this @iop
171 * @iop: The parent iopoll structure
172 * @weight: The default weight (or command completion budget)
173 * @poll_fn: The handler to invoke
176 * Initialize and enable this irq_poll structure.
178 void irq_poll_init(struct irq_poll
*iop
, int weight
, irq_poll_fn
*poll_fn
)
180 memset(iop
, 0, sizeof(*iop
));
181 INIT_LIST_HEAD(&iop
->list
);
182 iop
->weight
= weight
;
185 EXPORT_SYMBOL(irq_poll_init
);
187 static int irq_poll_cpu_dead(unsigned int cpu
)
190 * If a CPU goes away, splice its entries to the current CPU
191 * and trigger a run of the softirq
194 list_splice_init(&per_cpu(blk_cpu_iopoll
, cpu
),
195 this_cpu_ptr(&blk_cpu_iopoll
));
196 __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ
);
202 static __init
int irq_poll_setup(void)
206 for_each_possible_cpu(i
)
207 INIT_LIST_HEAD(&per_cpu(blk_cpu_iopoll
, i
));
209 open_softirq(IRQ_POLL_SOFTIRQ
, irq_poll_softirq
);
210 cpuhp_setup_state_nocalls(CPUHP_IRQ_POLL_DEAD
, "irq_poll:dead", NULL
,
214 subsys_initcall(irq_poll_setup
);