Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / block / blk-iopoll.c
1 /*
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/blkdev.h>
10 #include <linux/interrupt.h>
11 #include <linux/cpu.h>
12 #include <linux/blk-iopoll.h>
13 #include <linux/delay.h>
14
15 #include "blk.h"
16
17 static unsigned int blk_iopoll_budget __read_mostly = 256;
18
19 static DEFINE_PER_CPU(struct list_head, blk_cpu_iopoll);
20
21 /**
22  * blk_iopoll_sched - Schedule a run of the iopoll handler
23  * @iop:      The parent iopoll structure
24  *
25  * Description:
26  *     Add this blk_iopoll structure to the pending poll list and trigger the
27  *     raise of the blk iopoll softirq. The driver must already have gotten a
28  *     successful return from blk_iopoll_sched_prep() before calling this.
29  **/
30 void blk_iopoll_sched(struct blk_iopoll *iop)
31 {
32         unsigned long flags;
33
34         local_irq_save(flags);
35         list_add_tail(&iop->list, this_cpu_ptr(&blk_cpu_iopoll));
36         __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
37         local_irq_restore(flags);
38         preempt_check_resched_rt();
39 }
40 EXPORT_SYMBOL(blk_iopoll_sched);
41
42 /**
43  * __blk_iopoll_complete - Mark this @iop as un-polled again
44  * @iop:      The parent iopoll structure
45  *
46  * Description:
47  *     See blk_iopoll_complete(). This function must be called with interrupts
48  *     disabled.
49  **/
50 void __blk_iopoll_complete(struct blk_iopoll *iop)
51 {
52         list_del(&iop->list);
53         smp_mb__before_atomic();
54         clear_bit_unlock(IOPOLL_F_SCHED, &iop->state);
55 }
56 EXPORT_SYMBOL(__blk_iopoll_complete);
57
58 /**
59  * blk_iopoll_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
65  *     iopoll handler will not be invoked again before blk_iopoll_sched_prep()
66  *     is called.
67  **/
68 void blk_iopoll_complete(struct blk_iopoll *iop)
69 {
70         unsigned long flags;
71
72         local_irq_save(flags);
73         __blk_iopoll_complete(iop);
74         local_irq_restore(flags);
75 }
76 EXPORT_SYMBOL(blk_iopoll_complete);
77
78 static void blk_iopoll_softirq(struct softirq_action *h)
79 {
80         struct list_head *list = this_cpu_ptr(&blk_cpu_iopoll);
81         int rearm = 0, budget = blk_iopoll_budget;
82         unsigned long start_time = jiffies;
83
84         local_irq_disable();
85
86         while (!list_empty(list)) {
87                 struct blk_iopoll *iop;
88                 int work, weight;
89
90                 /*
91                  * If softirq window is exhausted then punt.
92                  */
93                 if (budget <= 0 || time_after(jiffies, start_time)) {
94                         rearm = 1;
95                         break;
96                 }
97
98                 local_irq_enable();
99
100                 /* Even though interrupts have been re-enabled, this
101                  * access is safe because interrupts can only add new
102                  * entries to the tail of this list, and only ->poll()
103                  * calls can remove this head entry from the list.
104                  */
105                 iop = list_entry(list->next, struct blk_iopoll, list);
106
107                 weight = iop->weight;
108                 work = 0;
109                 if (test_bit(IOPOLL_F_SCHED, &iop->state))
110                         work = iop->poll(iop, weight);
111
112                 budget -= work;
113
114                 local_irq_disable();
115
116                 /*
117                  * Drivers must not modify the iopoll state, if they
118                  * consume their assigned weight (or more, some drivers can't
119                  * easily just stop processing, they have to complete an
120                  * entire mask of commands).In such cases this code
121                  * still "owns" the iopoll instance and therefore can
122                  * move the instance around on the list at-will.
123                  */
124                 if (work >= weight) {
125                         if (blk_iopoll_disable_pending(iop))
126                                 __blk_iopoll_complete(iop);
127                         else
128                                 list_move_tail(&iop->list, list);
129                 }
130         }
131
132         if (rearm)
133                 __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
134
135         local_irq_enable();
136         preempt_check_resched_rt();
137 }
138
139 /**
140  * blk_iopoll_disable - Disable iopoll on this @iop
141  * @iop:      The parent iopoll structure
142  *
143  * Description:
144  *     Disable io polling and wait for any pending callbacks to have completed.
145  **/
146 void blk_iopoll_disable(struct blk_iopoll *iop)
147 {
148         set_bit(IOPOLL_F_DISABLE, &iop->state);
149         while (test_and_set_bit(IOPOLL_F_SCHED, &iop->state))
150                 msleep(1);
151         clear_bit(IOPOLL_F_DISABLE, &iop->state);
152 }
153 EXPORT_SYMBOL(blk_iopoll_disable);
154
155 /**
156  * blk_iopoll_enable - Enable iopoll on this @iop
157  * @iop:      The parent iopoll structure
158  *
159  * Description:
160  *     Enable iopoll on this @iop. Note that the handler run will not be
161  *     scheduled, it will only mark it as active.
162  **/
163 void blk_iopoll_enable(struct blk_iopoll *iop)
164 {
165         BUG_ON(!test_bit(IOPOLL_F_SCHED, &iop->state));
166         smp_mb__before_atomic();
167         clear_bit_unlock(IOPOLL_F_SCHED, &iop->state);
168 }
169 EXPORT_SYMBOL(blk_iopoll_enable);
170
171 /**
172  * blk_iopoll_init - Initialize this @iop
173  * @iop:      The parent iopoll structure
174  * @weight:   The default weight (or command completion budget)
175  * @poll_fn:  The handler to invoke
176  *
177  * Description:
178  *     Initialize this blk_iopoll structure. Before being actively used, the
179  *     driver must call blk_iopoll_enable().
180  **/
181 void blk_iopoll_init(struct blk_iopoll *iop, int weight, blk_iopoll_fn *poll_fn)
182 {
183         memset(iop, 0, sizeof(*iop));
184         INIT_LIST_HEAD(&iop->list);
185         iop->weight = weight;
186         iop->poll = poll_fn;
187         set_bit(IOPOLL_F_SCHED, &iop->state);
188 }
189 EXPORT_SYMBOL(blk_iopoll_init);
190
191 static int blk_iopoll_cpu_notify(struct notifier_block *self,
192                                  unsigned long action, void *hcpu)
193 {
194         /*
195          * If a CPU goes away, splice its entries to the current CPU
196          * and trigger a run of the softirq
197          */
198         if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
199                 int cpu = (unsigned long) hcpu;
200
201                 local_irq_disable();
202                 list_splice_init(&per_cpu(blk_cpu_iopoll, cpu),
203                                  this_cpu_ptr(&blk_cpu_iopoll));
204                 __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
205                 local_irq_enable();
206                 preempt_check_resched_rt();
207         }
208
209         return NOTIFY_OK;
210 }
211
212 static struct notifier_block blk_iopoll_cpu_notifier = {
213         .notifier_call  = blk_iopoll_cpu_notify,
214 };
215
216 static __init int blk_iopoll_setup(void)
217 {
218         int i;
219
220         for_each_possible_cpu(i)
221                 INIT_LIST_HEAD(&per_cpu(blk_cpu_iopoll, i));
222
223         open_softirq(BLOCK_IOPOLL_SOFTIRQ, blk_iopoll_softirq);
224         register_hotcpu_notifier(&blk_iopoll_cpu_notifier);
225         return 0;
226 }
227 subsys_initcall(blk_iopoll_setup);