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