Blame view

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