blob: b23a79761df7a6fa77a7631f49d24843de6c9a2d [file] [log] [blame]
Christoph Hellwig511cbce2015-11-10 14:56:14 +01001/*
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>
9#include <linux/interrupt.h>
10#include <linux/cpu.h>
11#include <linux/irq_poll.h>
12#include <linux/delay.h>
13
14static unsigned int irq_poll_budget __read_mostly = 256;
15
16static DEFINE_PER_CPU(struct list_head, blk_cpu_iopoll);
17
18/**
19 * irq_poll_sched - Schedule a run of the iopoll handler
20 * @iop: The parent iopoll structure
21 *
22 * Description:
23 * Add this irq_poll structure to the pending poll list and trigger the
Christoph Hellwigea511902015-12-07 06:41:11 -080024 * raise of the blk iopoll softirq.
Christoph Hellwig511cbce2015-11-10 14:56:14 +010025 **/
26void irq_poll_sched(struct irq_poll *iop)
27{
28 unsigned long flags;
29
Christoph Hellwigea511902015-12-07 06:41:11 -080030 if (test_bit(IRQ_POLL_F_DISABLE, &iop->state))
31 return;
Bart Van Assche2ee177e2015-12-31 09:56:03 +010032 if (test_and_set_bit(IRQ_POLL_F_SCHED, &iop->state))
Christoph Hellwigea511902015-12-07 06:41:11 -080033 return;
34
Christoph Hellwig511cbce2015-11-10 14:56:14 +010035 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);
Thomas Gleixner62bc6302011-11-13 17:17:09 +010039 preempt_check_resched_rt();
Christoph Hellwig511cbce2015-11-10 14:56:14 +010040}
41EXPORT_SYMBOL(irq_poll_sched);
42
43/**
44 * __irq_poll_complete - Mark this @iop as un-polled again
45 * @iop: The parent iopoll structure
46 *
47 * Description:
48 * See irq_poll_complete(). This function must be called with interrupts
49 * disabled.
50 **/
Christoph Hellwig83af1872015-12-07 06:57:25 -080051static void __irq_poll_complete(struct irq_poll *iop)
Christoph Hellwig511cbce2015-11-10 14:56:14 +010052{
53 list_del(&iop->list);
54 smp_mb__before_atomic();
55 clear_bit_unlock(IRQ_POLL_F_SCHED, &iop->state);
56}
Christoph Hellwig511cbce2015-11-10 14:56:14 +010057
58/**
59 * irq_poll_complete - Mark this @iop as un-polled again
60 * @iop: The parent iopoll structure
61 *
62 * Description:
63 * If a driver consumes less than the assigned budget in its run of the
64 * iopoll handler, it'll end the polled mode by calling this function. The
Christoph Hellwigea511902015-12-07 06:41:11 -080065 * iopoll handler will not be invoked again before irq_poll_sched()
Christoph Hellwig511cbce2015-11-10 14:56:14 +010066 * is called.
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);
Thomas Gleixner62bc6302011-11-13 17:17:09 +010075 preempt_check_resched_rt();
Christoph Hellwig511cbce2015-11-10 14:56:14 +010076}
77EXPORT_SYMBOL(irq_poll_complete);
78
Emese Revfy0766f782016-06-20 20:42:34 +020079static void __latent_entropy irq_poll_softirq(struct softirq_action *h)
Christoph Hellwig511cbce2015-11-10 14:56:14 +010080{
81 struct list_head *list = this_cpu_ptr(&blk_cpu_iopoll);
82 int rearm = 0, budget = irq_poll_budget;
83 unsigned long start_time = jiffies;
84
85 local_irq_disable();
86
87 while (!list_empty(list)) {
88 struct irq_poll *iop;
89 int work, weight;
90
91 /*
92 * If softirq window is exhausted then punt.
93 */
94 if (budget <= 0 || time_after(jiffies, start_time)) {
95 rearm = 1;
96 break;
97 }
98
99 local_irq_enable();
Thomas Gleixner62bc6302011-11-13 17:17:09 +0100100 preempt_check_resched_rt();
Christoph Hellwig511cbce2015-11-10 14:56:14 +0100101
102 /* Even though interrupts have been re-enabled, this
103 * access is safe because interrupts can only add new
104 * entries to the tail of this list, and only ->poll()
105 * calls can remove this head entry from the list.
106 */
107 iop = list_entry(list->next, struct irq_poll, list);
108
109 weight = iop->weight;
110 work = 0;
111 if (test_bit(IRQ_POLL_F_SCHED, &iop->state))
112 work = iop->poll(iop, weight);
113
114 budget -= work;
115
116 local_irq_disable();
117
118 /*
119 * Drivers must not modify the iopoll state, if they
120 * consume their assigned weight (or more, some drivers can't
121 * easily just stop processing, they have to complete an
122 * entire mask of commands).In such cases this code
123 * still "owns" the iopoll instance and therefore can
124 * move the instance around on the list at-will.
125 */
126 if (work >= weight) {
Christoph Hellwig0bc92ac2015-12-07 06:56:36 -0800127 if (test_bit(IRQ_POLL_F_DISABLE, &iop->state))
Christoph Hellwig511cbce2015-11-10 14:56:14 +0100128 __irq_poll_complete(iop);
129 else
130 list_move_tail(&iop->list, list);
131 }
132 }
133
134 if (rearm)
135 __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
136
137 local_irq_enable();
Thomas Gleixner62bc6302011-11-13 17:17:09 +0100138 preempt_check_resched_rt();
Christoph Hellwig511cbce2015-11-10 14:56:14 +0100139}
140
141/**
142 * irq_poll_disable - Disable iopoll on this @iop
143 * @iop: The parent iopoll structure
144 *
145 * Description:
146 * Disable io polling and wait for any pending callbacks to have completed.
147 **/
148void irq_poll_disable(struct irq_poll *iop)
149{
150 set_bit(IRQ_POLL_F_DISABLE, &iop->state);
151 while (test_and_set_bit(IRQ_POLL_F_SCHED, &iop->state))
152 msleep(1);
153 clear_bit(IRQ_POLL_F_DISABLE, &iop->state);
154}
155EXPORT_SYMBOL(irq_poll_disable);
156
157/**
158 * irq_poll_enable - Enable iopoll on this @iop
159 * @iop: The parent iopoll structure
160 *
161 * Description:
162 * Enable iopoll on this @iop. Note that the handler run will not be
163 * scheduled, it will only mark it as active.
164 **/
165void irq_poll_enable(struct irq_poll *iop)
166{
167 BUG_ON(!test_bit(IRQ_POLL_F_SCHED, &iop->state));
168 smp_mb__before_atomic();
169 clear_bit_unlock(IRQ_POLL_F_SCHED, &iop->state);
170}
171EXPORT_SYMBOL(irq_poll_enable);
172
173/**
174 * irq_poll_init - Initialize this @iop
175 * @iop: The parent iopoll structure
176 * @weight: The default weight (or command completion budget)
177 * @poll_fn: The handler to invoke
178 *
179 * Description:
Christoph Hellwig78d02642015-12-07 06:38:28 -0800180 * Initialize and enable this irq_poll structure.
Christoph Hellwig511cbce2015-11-10 14:56:14 +0100181 **/
182void irq_poll_init(struct irq_poll *iop, int weight, irq_poll_fn *poll_fn)
183{
184 memset(iop, 0, sizeof(*iop));
185 INIT_LIST_HEAD(&iop->list);
186 iop->weight = weight;
187 iop->poll = poll_fn;
Christoph Hellwig511cbce2015-11-10 14:56:14 +0100188}
189EXPORT_SYMBOL(irq_poll_init);
190
Sebastian Andrzej Siewior75e12ed2016-09-06 19:04:43 +0200191static int irq_poll_cpu_dead(unsigned int cpu)
Christoph Hellwig511cbce2015-11-10 14:56:14 +0100192{
193 /*
194 * If a CPU goes away, splice its entries to the current CPU
195 * and trigger a run of the softirq
196 */
Sebastian Andrzej Siewior75e12ed2016-09-06 19:04:43 +0200197 local_irq_disable();
198 list_splice_init(&per_cpu(blk_cpu_iopoll, cpu),
199 this_cpu_ptr(&blk_cpu_iopoll));
200 __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
201 local_irq_enable();
Thomas Gleixner62bc6302011-11-13 17:17:09 +0100202 preempt_check_resched_rt();
Christoph Hellwig511cbce2015-11-10 14:56:14 +0100203
Sebastian Andrzej Siewior75e12ed2016-09-06 19:04:43 +0200204 return 0;
Christoph Hellwig511cbce2015-11-10 14:56:14 +0100205}
206
Christoph Hellwig511cbce2015-11-10 14:56:14 +0100207static __init int irq_poll_setup(void)
208{
209 int i;
210
211 for_each_possible_cpu(i)
212 INIT_LIST_HEAD(&per_cpu(blk_cpu_iopoll, i));
213
214 open_softirq(IRQ_POLL_SOFTIRQ, irq_poll_softirq);
Sebastian Andrzej Siewior75e12ed2016-09-06 19:04:43 +0200215 cpuhp_setup_state_nocalls(CPUHP_IRQ_POLL_DEAD, "irq_poll:dead", NULL,
216 irq_poll_cpu_dead);
Christoph Hellwig511cbce2015-11-10 14:56:14 +0100217 return 0;
218}
219subsys_initcall(irq_poll_setup);