1
2
3
4
5
6#include <linux/kernel.h>
7#include <linux/module.h>
8#include <linux/init.h>
9#include <linux/bio.h>
10#include <linux/interrupt.h>
11#include <linux/cpu.h>
12#include <linux/irq_poll.h>
13#include <linux/delay.h>
14
15static unsigned int irq_poll_budget __read_mostly = 256;
16
17static DEFINE_PER_CPU(struct list_head, blk_cpu_iopoll);
18
19
20
21
22
23
24
25
26
27void irq_poll_sched(struct irq_poll *iop)
28{
29 unsigned long flags;
30
31 if (test_bit(IRQ_POLL_F_DISABLE, &iop->state))
32 return;
33 if (test_and_set_bit(IRQ_POLL_F_SCHED, &iop->state))
34 return;
35
36 local_irq_save(flags);
37 list_add_tail(&iop->list, this_cpu_ptr(&blk_cpu_iopoll));
38 raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
39 local_irq_restore(flags);
40}
41EXPORT_SYMBOL(irq_poll_sched);
42
43
44
45
46
47
48
49
50
51static void __irq_poll_complete(struct irq_poll *iop)
52{
53 list_del(&iop->list);
54 smp_mb__before_atomic();
55 clear_bit_unlock(IRQ_POLL_F_SCHED, &iop->state);
56}
57
58
59
60
61
62
63
64
65
66
67
68void irq_poll_complete(struct irq_poll *iop)
69{
70 unsigned long flags;
71
72 local_irq_save(flags);
73 __irq_poll_complete(iop);
74 local_irq_restore(flags);
75}
76EXPORT_SYMBOL(irq_poll_complete);
77
78static void __latent_entropy irq_poll_softirq(struct softirq_action *h)
79{
80 struct list_head *list = this_cpu_ptr(&blk_cpu_iopoll);
81 int rearm = 0, budget = irq_poll_budget;
82 unsigned long start_time = jiffies;
83
84 local_irq_disable();
85
86 while (!list_empty(list)) {
87 struct irq_poll *iop;
88 int work, weight;
89
90
91
92
93 if (budget <= 0 || time_after(jiffies, start_time)) {
94 rearm = 1;
95 break;
96 }
97
98 local_irq_enable();
99
100
101
102
103
104
105 iop = list_entry(list->next, struct irq_poll, list);
106
107 weight = iop->weight;
108 work = 0;
109 if (test_bit(IRQ_POLL_F_SCHED, &iop->state))
110 work = iop->poll(iop, weight);
111
112 budget -= work;
113
114 local_irq_disable();
115
116
117
118
119
120
121
122
123
124 if (work >= weight) {
125 if (test_bit(IRQ_POLL_F_DISABLE, &iop->state))
126 __irq_poll_complete(iop);
127 else
128 list_move_tail(&iop->list, list);
129 }
130 }
131
132 if (rearm)
133 __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
134
135 local_irq_enable();
136}
137
138
139
140
141
142
143
144
145void irq_poll_disable(struct irq_poll *iop)
146{
147 set_bit(IRQ_POLL_F_DISABLE, &iop->state);
148 while (test_and_set_bit(IRQ_POLL_F_SCHED, &iop->state))
149 msleep(1);
150 clear_bit(IRQ_POLL_F_DISABLE, &iop->state);
151}
152EXPORT_SYMBOL(irq_poll_disable);
153
154
155
156
157
158
159
160
161
162void irq_poll_enable(struct irq_poll *iop)
163{
164 BUG_ON(!test_bit(IRQ_POLL_F_SCHED, &iop->state));
165 smp_mb__before_atomic();
166 clear_bit_unlock(IRQ_POLL_F_SCHED, &iop->state);
167}
168EXPORT_SYMBOL(irq_poll_enable);
169
170
171
172
173
174
175
176
177
178
179void irq_poll_init(struct irq_poll *iop, int weight, irq_poll_fn *poll_fn)
180{
181 memset(iop, 0, sizeof(*iop));
182 INIT_LIST_HEAD(&iop->list);
183 iop->weight = weight;
184 iop->poll = poll_fn;
185}
186EXPORT_SYMBOL(irq_poll_init);
187
188static int irq_poll_cpu_dead(unsigned int cpu)
189{
190
191
192
193
194 local_irq_disable();
195 list_splice_init(&per_cpu(blk_cpu_iopoll, cpu),
196 this_cpu_ptr(&blk_cpu_iopoll));
197 __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
198 local_irq_enable();
199
200 return 0;
201}
202
203static __init int irq_poll_setup(void)
204{
205 int i;
206
207 for_each_possible_cpu(i)
208 INIT_LIST_HEAD(&per_cpu(blk_cpu_iopoll, i));
209
210 open_softirq(IRQ_POLL_SOFTIRQ, irq_poll_softirq);
211 cpuhp_setup_state_nocalls(CPUHP_IRQ_POLL_DEAD, "irq_poll:dead", NULL,
212 irq_poll_cpu_dead);
213 return 0;
214}
215subsys_initcall(irq_poll_setup);
216