Blame view

block/cfq-iosched.c 120 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
   *  CFQ, or complete fairness queueing, disk scheduler.
   *
   *  Based on ideas from a previously unfinished io
   *  scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
   *
0fe234795   Jens Axboe   [PATCH] Update ax...
7
   *  Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
  #include <linux/module.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
10
  #include <linux/slab.h>
1cc9be68e   Al Viro   [PATCH] noise rem...
11
12
  #include <linux/blkdev.h>
  #include <linux/elevator.h>
ad5ebd2fa   Randy Dunlap   block: jiffies fixes
13
  #include <linux/jiffies.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
  #include <linux/rbtree.h>
22e2c507c   Jens Axboe   [PATCH] Update cf...
15
  #include <linux/ioprio.h>
7b679138b   Jens Axboe   cfq-iosched: add ...
16
  #include <linux/blktrace_api.h>
6e736be7f   Tejun Heo   block: make ioc g...
17
  #include "blk.h"
629ed0b10   Tejun Heo   blkcg: move stati...
18
  #include "blk-cgroup.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
20
21
22
  
  /*
   * tunables
   */
fe094d98e   Jens Axboe   cfq-iosched: make...
23
  /* max queue in one round of service */
abc3c744d   Shaohua Li   cfq-iosched: quan...
24
  static const int cfq_quantum = 8;
64100099e   Arjan van de Ven   [BLOCK] mark some...
25
  static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
fe094d98e   Jens Axboe   cfq-iosched: make...
26
27
28
29
  /* maximum backwards seek, in KiB */
  static const int cfq_back_max = 16 * 1024;
  /* penalty of a backwards seek */
  static const int cfq_back_penalty = 2;
64100099e   Arjan van de Ven   [BLOCK] mark some...
30
  static const int cfq_slice_sync = HZ / 10;
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
31
  static int cfq_slice_async = HZ / 25;
64100099e   Arjan van de Ven   [BLOCK] mark some...
32
  static const int cfq_slice_async_rq = 2;
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
33
  static int cfq_slice_idle = HZ / 125;
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
34
  static int cfq_group_idle = HZ / 125;
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
35
36
  static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
  static const int cfq_hist_divisor = 4;
22e2c507c   Jens Axboe   [PATCH] Update cf...
37

d9e7620e6   Jens Axboe   cfq-iosched: rewo...
38
  /*
0871714e0   Jens Axboe   cfq-iosched: rela...
39
   * offset from end of service tree
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
40
   */
0871714e0   Jens Axboe   cfq-iosched: rela...
41
  #define CFQ_IDLE_DELAY		(HZ / 5)
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
42
43
44
45
46
  
  /*
   * below this threshold, we consider thinktime immediate
   */
  #define CFQ_MIN_TT		(2)
22e2c507c   Jens Axboe   [PATCH] Update cf...
47
  #define CFQ_SLICE_SCALE		(5)
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
48
  #define CFQ_HW_QUEUE_MIN	(5)
25bc6b077   Vivek Goyal   blkio: Introduce ...
49
  #define CFQ_SERVICE_SHIFT       12
22e2c507c   Jens Axboe   [PATCH] Update cf...
50

3dde36dde   Corrado Zoccolo   cfq-iosched: rewo...
51
  #define CFQQ_SEEK_THR		(sector_t)(8 * 100)
e9ce335df   Shaohua Li   cfq-iosched: fix ...
52
  #define CFQQ_CLOSE_THR		(sector_t)(8 * 1024)
41647e7a9   Corrado Zoccolo   cfq-iosched: reth...
53
  #define CFQQ_SECT_THR_NONROT	(sector_t)(2 * 32)
3dde36dde   Corrado Zoccolo   cfq-iosched: rewo...
54
  #define CFQQ_SEEKY(cfqq)	(hweight32(cfqq->seek_history) > 32/8)
ae54abed6   Shaohua Li   cfq-iosched: spli...
55

a612fddf0   Tejun Heo   block, cfq: move ...
56
57
58
  #define RQ_CIC(rq)		icq_to_cic((rq)->elv.icq)
  #define RQ_CFQQ(rq)		(struct cfq_queue *) ((rq)->elv.priv[0])
  #define RQ_CFQG(rq)		(struct cfq_group *) ((rq)->elv.priv[1])
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59

e18b890bb   Christoph Lameter   [PATCH] slab: rem...
60
  static struct kmem_cache *cfq_pool;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61

22e2c507c   Jens Axboe   [PATCH] Update cf...
62
63
  #define CFQ_PRIO_LISTS		IOPRIO_BE_NR
  #define cfq_class_idle(cfqq)	((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
22e2c507c   Jens Axboe   [PATCH] Update cf...
64
  #define cfq_class_rt(cfqq)	((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
65
  #define sample_valid(samples)	((samples) > 80)
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
66
  #define rb_entry_cfqg(node)	rb_entry((node), struct cfq_group, rb_node)
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
67

c58698073   Tejun Heo   block, cfq: reorg...
68
69
70
71
72
73
74
  struct cfq_ttime {
  	unsigned long last_end_request;
  
  	unsigned long ttime_total;
  	unsigned long ttime_samples;
  	unsigned long ttime_mean;
  };
22e2c507c   Jens Axboe   [PATCH] Update cf...
75
  /*
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
76
77
78
79
80
81
82
83
   * Most of our rbtree usage is for sorting with min extraction, so
   * if we cache the leftmost node we don't have to walk down the tree
   * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
   * move this into the elevator for the rq sorting as well.
   */
  struct cfq_rb_root {
  	struct rb_root rb;
  	struct rb_node *left;
aa6f6a3de   Corrado Zoccolo   cfq-iosched: prep...
84
  	unsigned count;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
85
  	u64 min_vdisktime;
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
86
  	struct cfq_ttime ttime;
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
87
  };
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
88
89
  #define CFQ_RB_ROOT	(struct cfq_rb_root) { .rb = RB_ROOT, \
  			.ttime = {.last_end_request = jiffies,},}
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
90
91
  
  /*
6118b70b3   Jens Axboe   cfq-iosched: get ...
92
93
94
95
   * Per process-grouping structure
   */
  struct cfq_queue {
  	/* reference count */
30d7b9448   Shaohua Li   block cfq: don't ...
96
  	int ref;
6118b70b3   Jens Axboe   cfq-iosched: get ...
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  	/* various state flags, see below */
  	unsigned int flags;
  	/* parent cfq_data */
  	struct cfq_data *cfqd;
  	/* service_tree member */
  	struct rb_node rb_node;
  	/* service_tree key */
  	unsigned long rb_key;
  	/* prio tree member */
  	struct rb_node p_node;
  	/* prio tree root we belong to, if any */
  	struct rb_root *p_root;
  	/* sorted list of pending requests */
  	struct rb_root sort_list;
  	/* if fifo isn't expired, next request to serve */
  	struct request *next_rq;
  	/* requests queued in sort_list */
  	int queued[2];
  	/* currently allocated requests */
  	int allocated[2];
  	/* fifo list of requests in sort_list */
  	struct list_head fifo;
dae739ebc   Vivek Goyal   blkio: Group time...
119
120
  	/* time when queue got scheduled in to dispatch first request. */
  	unsigned long dispatch_start;
f75edf2dc   Vivek Goyal   blkio: Wait for c...
121
  	unsigned int allocated_slice;
c4081ba5c   Richard Kennedy   cfq: reorder cfq_...
122
  	unsigned int slice_dispatch;
dae739ebc   Vivek Goyal   blkio: Group time...
123
124
  	/* time when first request from queue completed and slice started. */
  	unsigned long slice_start;
6118b70b3   Jens Axboe   cfq-iosched: get ...
125
126
  	unsigned long slice_end;
  	long slice_resid;
6118b70b3   Jens Axboe   cfq-iosched: get ...
127

65299a3b7   Christoph Hellwig   block: separate p...
128
129
  	/* pending priority requests */
  	int prio_pending;
6118b70b3   Jens Axboe   cfq-iosched: get ...
130
131
132
133
134
  	/* number of requests that are on the dispatch list or inside driver */
  	int dispatched;
  
  	/* io prio of this group */
  	unsigned short ioprio, org_ioprio;
4aede84b3   Justin TerAvest   fixlet: Remove fs...
135
  	unsigned short ioprio_class;
6118b70b3   Jens Axboe   cfq-iosched: get ...
136

c4081ba5c   Richard Kennedy   cfq: reorder cfq_...
137
  	pid_t pid;
3dde36dde   Corrado Zoccolo   cfq-iosched: rewo...
138
  	u32 seek_history;
b2c18e1e0   Jeff Moyer   cfq: calculate th...
139
  	sector_t last_request_pos;
aa6f6a3de   Corrado Zoccolo   cfq-iosched: prep...
140
  	struct cfq_rb_root *service_tree;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
141
  	struct cfq_queue *new_cfqq;
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
142
  	struct cfq_group *cfqg;
c4e7893eb   Vivek Goyal   cfq-iosched: blkt...
143
144
  	/* Number of sectors dispatched from queue in single dispatch round */
  	unsigned long nr_sectors;
6118b70b3   Jens Axboe   cfq-iosched: get ...
145
146
147
  };
  
  /*
718eee057   Corrado Zoccolo   cfq-iosched: fair...
148
   * First index in the service_trees.
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
149
150
   * IDLE is handled separately, so it has negative index
   */
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
151
  enum wl_class_t {
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
152
  	BE_WORKLOAD = 0,
615f0259e   Vivek Goyal   blkio: Implement ...
153
154
  	RT_WORKLOAD = 1,
  	IDLE_WORKLOAD = 2,
b4627321e   Vivek Goyal   cfq-iosched: Fix ...
155
  	CFQ_PRIO_NR,
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
156
157
158
  };
  
  /*
718eee057   Corrado Zoccolo   cfq-iosched: fair...
159
160
161
162
163
164
165
   * Second index in the service_trees.
   */
  enum wl_type_t {
  	ASYNC_WORKLOAD = 0,
  	SYNC_NOIDLE_WORKLOAD = 1,
  	SYNC_WORKLOAD = 2
  };
155fead9b   Tejun Heo   blkcg: move blkio...
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  struct cfqg_stats {
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
  	/* total bytes transferred */
  	struct blkg_rwstat		service_bytes;
  	/* total IOs serviced, post merge */
  	struct blkg_rwstat		serviced;
  	/* number of ios merged */
  	struct blkg_rwstat		merged;
  	/* total time spent on device in ns, may not be accurate w/ queueing */
  	struct blkg_rwstat		service_time;
  	/* total time spent waiting in scheduler queue in ns */
  	struct blkg_rwstat		wait_time;
  	/* number of IOs queued up */
  	struct blkg_rwstat		queued;
  	/* total sectors transferred */
  	struct blkg_stat		sectors;
  	/* total disk time and nr sectors dispatched by this group */
  	struct blkg_stat		time;
  #ifdef CONFIG_DEBUG_BLK_CGROUP
  	/* time not charged to this cgroup */
  	struct blkg_stat		unaccounted_time;
  	/* sum of number of ios queued across all samples */
  	struct blkg_stat		avg_queue_size_sum;
  	/* count of samples taken for average */
  	struct blkg_stat		avg_queue_size_samples;
  	/* how many times this group has been removed from service tree */
  	struct blkg_stat		dequeue;
  	/* total time spent waiting for it to be assigned a timeslice. */
  	struct blkg_stat		group_wait_time;
3c798398e   Tejun Heo   blkcg: mass renam...
195
  	/* time spent idling for this blkcg_gq */
155fead9b   Tejun Heo   blkcg: move blkio...
196
197
198
199
200
201
202
203
204
205
206
  	struct blkg_stat		idle_time;
  	/* total time with empty current active q with other requests queued */
  	struct blkg_stat		empty_time;
  	/* fields after this shouldn't be cleared on stat reset */
  	uint64_t			start_group_wait_time;
  	uint64_t			start_idle_time;
  	uint64_t			start_empty_time;
  	uint16_t			flags;
  #endif	/* CONFIG_DEBUG_BLK_CGROUP */
  #endif	/* CONFIG_CFQ_GROUP_IOSCHED */
  };
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
207
208
  /* This is per cgroup per device grouping structure */
  struct cfq_group {
f95a04afa   Tejun Heo   blkcg: embed stru...
209
210
  	/* must be the first member */
  	struct blkg_policy_data pd;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
211
212
213
214
215
  	/* group service_tree member */
  	struct rb_node rb_node;
  
  	/* group service_tree key */
  	u64 vdisktime;
e71357e11   Tejun Heo   cfq-iosched: add ...
216
217
  
  	/*
7918ffb5b   Tejun Heo   cfq-iosched: impl...
218
219
220
221
222
223
224
225
226
227
228
229
  	 * The number of active cfqgs and sum of their weights under this
  	 * cfqg.  This covers this cfqg's leaf_weight and all children's
  	 * weights, but does not cover weights of further descendants.
  	 *
  	 * If a cfqg is on the service tree, it's active.  An active cfqg
  	 * also activates its parent and contributes to the children_weight
  	 * of the parent.
  	 */
  	int nr_active;
  	unsigned int children_weight;
  
  	/*
1d3650f71   Tejun Heo   cfq-iosched: impl...
230
231
232
233
234
235
236
237
238
239
240
241
  	 * vfraction is the fraction of vdisktime that the tasks in this
  	 * cfqg are entitled to.  This is determined by compounding the
  	 * ratios walking up from this cfqg to the root.
  	 *
  	 * It is in fixed point w/ CFQ_SERVICE_SHIFT and the sum of all
  	 * vfractions on a service tree is approximately 1.  The sum may
  	 * deviate a bit due to rounding errors and fluctuations caused by
  	 * cfqgs entering and leaving the service tree.
  	 */
  	unsigned int vfraction;
  
  	/*
e71357e11   Tejun Heo   cfq-iosched: add ...
242
243
244
245
246
  	 * There are two weights - (internal) weight is the weight of this
  	 * cfqg against the sibling cfqgs.  leaf_weight is the wight of
  	 * this cfqg against the child cfqgs.  For the root cfqg, both
  	 * weights are kept in sync for backward compatibility.
  	 */
25bc6b077   Vivek Goyal   blkio: Introduce ...
247
  	unsigned int weight;
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
248
  	unsigned int new_weight;
3381cb8d2   Tejun Heo   blkcg: move blkio...
249
  	unsigned int dev_weight;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
250

e71357e11   Tejun Heo   cfq-iosched: add ...
251
252
253
  	unsigned int leaf_weight;
  	unsigned int new_leaf_weight;
  	unsigned int dev_leaf_weight;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
254
255
  	/* number of cfqq currently on this group */
  	int nr_cfqq;
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
256
  	/*
4495a7d41   Kyungmin Park   CFQ: Fix typo and...
257
  	 * Per group busy queues average. Useful for workload slice calc. We
b4627321e   Vivek Goyal   cfq-iosched: Fix ...
258
259
260
261
262
263
264
265
266
267
268
  	 * create the array for each prio class but at run time it is used
  	 * only for RT and BE class and slot for IDLE class remains unused.
  	 * This is primarily done to avoid confusion and a gcc warning.
  	 */
  	unsigned int busy_queues_avg[CFQ_PRIO_NR];
  	/*
  	 * rr lists of queues with requests. We maintain service trees for
  	 * RT and BE classes. These trees are subdivided in subclasses
  	 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
  	 * class there is no subclassification and all the cfq queues go on
  	 * a single tree service_tree_idle.
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
269
270
271
272
  	 * Counts are embedded in the cfq_rb_root
  	 */
  	struct cfq_rb_root service_trees[2][3];
  	struct cfq_rb_root service_tree_idle;
dae739ebc   Vivek Goyal   blkio: Group time...
273

4d2ceea4c   Vivek Goyal   cfq-iosched: More...
274
275
276
  	unsigned long saved_wl_slice;
  	enum wl_type_t saved_wl_type;
  	enum wl_class_t saved_wl_class;
4eef30499   Tejun Heo   blkcg: move per-q...
277

80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
278
279
  	/* number of requests that are on the dispatch list or inside driver */
  	int dispatched;
7700fc4f6   Shaohua Li   CFQ: add think ti...
280
  	struct cfq_ttime ttime;
0b39920b5   Tejun Heo   cfq-iosched: coll...
281
282
  	struct cfqg_stats stats;	/* stats for this cfqg */
  	struct cfqg_stats dead_stats;	/* stats pushed from dead children */
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
283
  };
718eee057   Corrado Zoccolo   cfq-iosched: fair...
284

c58698073   Tejun Heo   block, cfq: reorg...
285
286
287
288
  struct cfq_io_cq {
  	struct io_cq		icq;		/* must be the first member */
  	struct cfq_queue	*cfqq[2];
  	struct cfq_ttime	ttime;
598971bfb   Tejun Heo   cfq: don't use ic...
289
290
  	int			ioprio;		/* the current ioprio */
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
f4da80727   Tejun Heo   blkcg: remove blk...
291
  	uint64_t		blkcg_serial_nr; /* the current blkcg serial */
598971bfb   Tejun Heo   cfq: don't use ic...
292
  #endif
c58698073   Tejun Heo   block, cfq: reorg...
293
  };
718eee057   Corrado Zoccolo   cfq-iosched: fair...
294
  /*
22e2c507c   Jens Axboe   [PATCH] Update cf...
295
296
   * Per block device queue structure
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
297
  struct cfq_data {
165125e1e   Jens Axboe   [BLOCK] Get rid o...
298
  	struct request_queue *queue;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
299
300
  	/* Root service tree for cfq_groups */
  	struct cfq_rb_root grp_service_tree;
f51b802c1   Tejun Heo   blkcg: use the us...
301
  	struct cfq_group *root_group;
22e2c507c   Jens Axboe   [PATCH] Update cf...
302
303
  
  	/*
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
304
  	 * The priority currently being served
22e2c507c   Jens Axboe   [PATCH] Update cf...
305
  	 */
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
306
307
  	enum wl_class_t serving_wl_class;
  	enum wl_type_t serving_wl_type;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
308
  	unsigned long workload_expires;
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
309
  	struct cfq_group *serving_group;
a36e71f99   Jens Axboe   cfq-iosched: add ...
310
311
312
313
314
315
316
  
  	/*
  	 * Each priority tree is sorted by next_request position.  These
  	 * trees are used when determining if two or more queues are
  	 * interleaving requests (see cfq_close_cooperator).
  	 */
  	struct rb_root prio_trees[CFQ_PRIO_LISTS];
22e2c507c   Jens Axboe   [PATCH] Update cf...
317
  	unsigned int busy_queues;
ef8a41df8   Shaohua Li   cfq-iosched: give...
318
  	unsigned int busy_sync_queues;
22e2c507c   Jens Axboe   [PATCH] Update cf...
319

53c583d22   Corrado Zoccolo   cfq-iosched: requ...
320
321
  	int rq_in_driver;
  	int rq_in_flight[2];
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
322
323
324
325
326
  
  	/*
  	 * queue-depth detection
  	 */
  	int rq_queued;
25776e359   Jens Axboe   [PATCH] cfq-iosch...
327
  	int hw_tag;
e459dd08f   Corrado Zoccolo   cfq-iosched: fix ...
328
329
330
331
332
333
334
335
  	/*
  	 * hw_tag can be
  	 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
  	 *  1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
  	 *  0 => no NCQ
  	 */
  	int hw_tag_est_depth;
  	unsigned int hw_tag_samples;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336

22e2c507c   Jens Axboe   [PATCH] Update cf...
337
  	/*
22e2c507c   Jens Axboe   [PATCH] Update cf...
338
339
340
  	 * idle window management
  	 */
  	struct timer_list idle_slice_timer;
23e018a1b   Jens Axboe   block: get rid of...
341
  	struct work_struct unplug_work;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
342

22e2c507c   Jens Axboe   [PATCH] Update cf...
343
  	struct cfq_queue *active_queue;
c58698073   Tejun Heo   block, cfq: reorg...
344
  	struct cfq_io_cq *active_cic;
22e2c507c   Jens Axboe   [PATCH] Update cf...
345

c2dea2d1f   Vasily Tarasov   cfq: async queue ...
346
347
348
349
350
  	/*
  	 * async queue for each priority case
  	 */
  	struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
  	struct cfq_queue *async_idle_cfqq;
15c31be4d   Jens Axboe   cfq-iosched: fix ...
351

6d048f531   Jens Axboe   cfq-iosched: deve...
352
  	sector_t last_position;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
354
355
356
357
  	/*
  	 * tunables, see top of file
  	 */
  	unsigned int cfq_quantum;
22e2c507c   Jens Axboe   [PATCH] Update cf...
358
  	unsigned int cfq_fifo_expire[2];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
359
360
  	unsigned int cfq_back_penalty;
  	unsigned int cfq_back_max;
22e2c507c   Jens Axboe   [PATCH] Update cf...
361
362
363
  	unsigned int cfq_slice[2];
  	unsigned int cfq_slice_async_rq;
  	unsigned int cfq_slice_idle;
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
364
  	unsigned int cfq_group_idle;
963b72fc6   Jens Axboe   cfq-iosched: rena...
365
  	unsigned int cfq_latency;
5bf14c072   Tao Ma   block: Make cfq_t...
366
  	unsigned int cfq_target_latency;
d9ff41879   Al Viro   [PATCH] make cfq_...
367

6118b70b3   Jens Axboe   cfq-iosched: get ...
368
369
370
371
  	/*
  	 * Fallback dummy cfqq for extreme OOM conditions
  	 */
  	struct cfq_queue oom_cfqq;
365722bb9   Vivek Goyal   cfq-iosched: dela...
372

573412b29   Corrado Zoccolo   cfq-iosched: redu...
373
  	unsigned long last_delayed_sync;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
374
  };
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
375
  static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
376
  static struct cfq_rb_root *st_for(struct cfq_group *cfqg,
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
377
  					    enum wl_class_t class,
65b32a573   Vivek Goyal   cfq-iosched: Remo...
378
  					    enum wl_type_t type)
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
379
  {
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
380
381
  	if (!cfqg)
  		return NULL;
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
382
  	if (class == IDLE_WORKLOAD)
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
383
  		return &cfqg->service_tree_idle;
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
384

3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
385
  	return &cfqg->service_trees[class][type];
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
386
  }
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
387
  enum cfqq_state_flags {
b0b8d7494   Jens Axboe   cfq-iosched: docu...
388
389
  	CFQ_CFQQ_FLAG_on_rr = 0,	/* on round-robin busy list */
  	CFQ_CFQQ_FLAG_wait_request,	/* waiting for a request */
b029195dd   Jens Axboe   cfq-iosched: don'...
390
  	CFQ_CFQQ_FLAG_must_dispatch,	/* must be allowed a dispatch */
b0b8d7494   Jens Axboe   cfq-iosched: docu...
391
  	CFQ_CFQQ_FLAG_must_alloc_slice,	/* per-slice must_alloc flag */
b0b8d7494   Jens Axboe   cfq-iosched: docu...
392
393
394
  	CFQ_CFQQ_FLAG_fifo_expire,	/* FIFO checked in this slice */
  	CFQ_CFQQ_FLAG_idle_window,	/* slice idling enabled */
  	CFQ_CFQQ_FLAG_prio_changed,	/* task priority has changed */
44f7c1606   Jens Axboe   cfq-iosched: defe...
395
  	CFQ_CFQQ_FLAG_slice_new,	/* no requests dispatched in slice */
91fac317a   Vasily Tarasov   cfq-iosched: get ...
396
  	CFQ_CFQQ_FLAG_sync,		/* synchronous queue */
b3b6d0408   Jeff Moyer   cfq: change the m...
397
  	CFQ_CFQQ_FLAG_coop,		/* cfqq is shared */
ae54abed6   Shaohua Li   cfq-iosched: spli...
398
  	CFQ_CFQQ_FLAG_split_coop,	/* shared cfqq will be splitted */
76280aff1   Corrado Zoccolo   cfq-iosched: idli...
399
  	CFQ_CFQQ_FLAG_deep,		/* sync cfqq experienced large depth */
f75edf2dc   Vivek Goyal   blkio: Wait for c...
400
  	CFQ_CFQQ_FLAG_wait_busy,	/* Waiting for next request */
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
401
402
403
404
405
  };
  
  #define CFQ_CFQQ_FNS(name)						\
  static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq)		\
  {									\
fe094d98e   Jens Axboe   cfq-iosched: make...
406
  	(cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name);			\
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
407
408
409
  }									\
  static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq)	\
  {									\
fe094d98e   Jens Axboe   cfq-iosched: make...
410
  	(cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name);			\
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
411
412
413
  }									\
  static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq)		\
  {									\
fe094d98e   Jens Axboe   cfq-iosched: make...
414
  	return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0;	\
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
415
416
417
418
  }
  
  CFQ_CFQQ_FNS(on_rr);
  CFQ_CFQQ_FNS(wait_request);
b029195dd   Jens Axboe   cfq-iosched: don'...
419
  CFQ_CFQQ_FNS(must_dispatch);
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
420
  CFQ_CFQQ_FNS(must_alloc_slice);
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
421
422
423
  CFQ_CFQQ_FNS(fifo_expire);
  CFQ_CFQQ_FNS(idle_window);
  CFQ_CFQQ_FNS(prio_changed);
44f7c1606   Jens Axboe   cfq-iosched: defe...
424
  CFQ_CFQQ_FNS(slice_new);
91fac317a   Vasily Tarasov   cfq-iosched: get ...
425
  CFQ_CFQQ_FNS(sync);
a36e71f99   Jens Axboe   cfq-iosched: add ...
426
  CFQ_CFQQ_FNS(coop);
ae54abed6   Shaohua Li   cfq-iosched: spli...
427
  CFQ_CFQQ_FNS(split_coop);
76280aff1   Corrado Zoccolo   cfq-iosched: idli...
428
  CFQ_CFQQ_FNS(deep);
f75edf2dc   Vivek Goyal   blkio: Wait for c...
429
  CFQ_CFQQ_FNS(wait_busy);
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
430
  #undef CFQ_CFQQ_FNS
f95a04afa   Tejun Heo   blkcg: embed stru...
431
432
433
434
  static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd)
  {
  	return pd ? container_of(pd, struct cfq_group, pd) : NULL;
  }
f95a04afa   Tejun Heo   blkcg: embed stru...
435
436
437
438
  static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg)
  {
  	return pd_to_blkg(&cfqg->pd);
  }
629ed0b10   Tejun Heo   blkcg: move stati...
439
  #if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
440

155fead9b   Tejun Heo   blkcg: move blkio...
441
442
443
444
445
  /* cfqg stats flags */
  enum cfqg_stats_flags {
  	CFQG_stats_waiting = 0,
  	CFQG_stats_idling,
  	CFQG_stats_empty,
629ed0b10   Tejun Heo   blkcg: move stati...
446
  };
155fead9b   Tejun Heo   blkcg: move blkio...
447
448
  #define CFQG_FLAG_FNS(name)						\
  static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats)	\
629ed0b10   Tejun Heo   blkcg: move stati...
449
  {									\
155fead9b   Tejun Heo   blkcg: move blkio...
450
  	stats->flags |= (1 << CFQG_stats_##name);			\
629ed0b10   Tejun Heo   blkcg: move stati...
451
  }									\
155fead9b   Tejun Heo   blkcg: move blkio...
452
  static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats)	\
629ed0b10   Tejun Heo   blkcg: move stati...
453
  {									\
155fead9b   Tejun Heo   blkcg: move blkio...
454
  	stats->flags &= ~(1 << CFQG_stats_##name);			\
629ed0b10   Tejun Heo   blkcg: move stati...
455
  }									\
155fead9b   Tejun Heo   blkcg: move blkio...
456
  static inline int cfqg_stats_##name(struct cfqg_stats *stats)		\
629ed0b10   Tejun Heo   blkcg: move stati...
457
  {									\
155fead9b   Tejun Heo   blkcg: move blkio...
458
  	return (stats->flags & (1 << CFQG_stats_##name)) != 0;		\
629ed0b10   Tejun Heo   blkcg: move stati...
459
  }									\
155fead9b   Tejun Heo   blkcg: move blkio...
460
461
462
463
  CFQG_FLAG_FNS(waiting)
  CFQG_FLAG_FNS(idling)
  CFQG_FLAG_FNS(empty)
  #undef CFQG_FLAG_FNS
629ed0b10   Tejun Heo   blkcg: move stati...
464
465
  
  /* This should be called with the queue_lock held. */
155fead9b   Tejun Heo   blkcg: move blkio...
466
  static void cfqg_stats_update_group_wait_time(struct cfqg_stats *stats)
629ed0b10   Tejun Heo   blkcg: move stati...
467
468
  {
  	unsigned long long now;
155fead9b   Tejun Heo   blkcg: move blkio...
469
  	if (!cfqg_stats_waiting(stats))
629ed0b10   Tejun Heo   blkcg: move stati...
470
471
472
473
474
475
  		return;
  
  	now = sched_clock();
  	if (time_after64(now, stats->start_group_wait_time))
  		blkg_stat_add(&stats->group_wait_time,
  			      now - stats->start_group_wait_time);
155fead9b   Tejun Heo   blkcg: move blkio...
476
  	cfqg_stats_clear_waiting(stats);
629ed0b10   Tejun Heo   blkcg: move stati...
477
478
479
  }
  
  /* This should be called with the queue_lock held. */
155fead9b   Tejun Heo   blkcg: move blkio...
480
481
  static void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg,
  						 struct cfq_group *curr_cfqg)
629ed0b10   Tejun Heo   blkcg: move stati...
482
  {
155fead9b   Tejun Heo   blkcg: move blkio...
483
  	struct cfqg_stats *stats = &cfqg->stats;
629ed0b10   Tejun Heo   blkcg: move stati...
484

155fead9b   Tejun Heo   blkcg: move blkio...
485
  	if (cfqg_stats_waiting(stats))
629ed0b10   Tejun Heo   blkcg: move stati...
486
  		return;
155fead9b   Tejun Heo   blkcg: move blkio...
487
  	if (cfqg == curr_cfqg)
629ed0b10   Tejun Heo   blkcg: move stati...
488
  		return;
155fead9b   Tejun Heo   blkcg: move blkio...
489
490
  	stats->start_group_wait_time = sched_clock();
  	cfqg_stats_mark_waiting(stats);
629ed0b10   Tejun Heo   blkcg: move stati...
491
492
493
  }
  
  /* This should be called with the queue_lock held. */
155fead9b   Tejun Heo   blkcg: move blkio...
494
  static void cfqg_stats_end_empty_time(struct cfqg_stats *stats)
629ed0b10   Tejun Heo   blkcg: move stati...
495
496
  {
  	unsigned long long now;
155fead9b   Tejun Heo   blkcg: move blkio...
497
  	if (!cfqg_stats_empty(stats))
629ed0b10   Tejun Heo   blkcg: move stati...
498
499
500
501
502
503
  		return;
  
  	now = sched_clock();
  	if (time_after64(now, stats->start_empty_time))
  		blkg_stat_add(&stats->empty_time,
  			      now - stats->start_empty_time);
155fead9b   Tejun Heo   blkcg: move blkio...
504
  	cfqg_stats_clear_empty(stats);
629ed0b10   Tejun Heo   blkcg: move stati...
505
  }
155fead9b   Tejun Heo   blkcg: move blkio...
506
  static void cfqg_stats_update_dequeue(struct cfq_group *cfqg)
629ed0b10   Tejun Heo   blkcg: move stati...
507
  {
155fead9b   Tejun Heo   blkcg: move blkio...
508
  	blkg_stat_add(&cfqg->stats.dequeue, 1);
629ed0b10   Tejun Heo   blkcg: move stati...
509
  }
155fead9b   Tejun Heo   blkcg: move blkio...
510
  static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg)
629ed0b10   Tejun Heo   blkcg: move stati...
511
  {
155fead9b   Tejun Heo   blkcg: move blkio...
512
  	struct cfqg_stats *stats = &cfqg->stats;
629ed0b10   Tejun Heo   blkcg: move stati...
513

4d5e80a76   Tejun Heo   blkcg: s/blkg_rws...
514
  	if (blkg_rwstat_total(&stats->queued))
629ed0b10   Tejun Heo   blkcg: move stati...
515
516
517
518
519
520
521
  		return;
  
  	/*
  	 * group is already marked empty. This can happen if cfqq got new
  	 * request in parent group and moved to this group while being added
  	 * to service tree. Just ignore the event and move on.
  	 */
155fead9b   Tejun Heo   blkcg: move blkio...
522
  	if (cfqg_stats_empty(stats))
629ed0b10   Tejun Heo   blkcg: move stati...
523
524
525
  		return;
  
  	stats->start_empty_time = sched_clock();
155fead9b   Tejun Heo   blkcg: move blkio...
526
  	cfqg_stats_mark_empty(stats);
629ed0b10   Tejun Heo   blkcg: move stati...
527
  }
155fead9b   Tejun Heo   blkcg: move blkio...
528
  static void cfqg_stats_update_idle_time(struct cfq_group *cfqg)
629ed0b10   Tejun Heo   blkcg: move stati...
529
  {
155fead9b   Tejun Heo   blkcg: move blkio...
530
  	struct cfqg_stats *stats = &cfqg->stats;
629ed0b10   Tejun Heo   blkcg: move stati...
531

155fead9b   Tejun Heo   blkcg: move blkio...
532
  	if (cfqg_stats_idling(stats)) {
629ed0b10   Tejun Heo   blkcg: move stati...
533
534
535
536
537
  		unsigned long long now = sched_clock();
  
  		if (time_after64(now, stats->start_idle_time))
  			blkg_stat_add(&stats->idle_time,
  				      now - stats->start_idle_time);
155fead9b   Tejun Heo   blkcg: move blkio...
538
  		cfqg_stats_clear_idling(stats);
629ed0b10   Tejun Heo   blkcg: move stati...
539
540
  	}
  }
155fead9b   Tejun Heo   blkcg: move blkio...
541
  static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg)
629ed0b10   Tejun Heo   blkcg: move stati...
542
  {
155fead9b   Tejun Heo   blkcg: move blkio...
543
  	struct cfqg_stats *stats = &cfqg->stats;
629ed0b10   Tejun Heo   blkcg: move stati...
544

155fead9b   Tejun Heo   blkcg: move blkio...
545
  	BUG_ON(cfqg_stats_idling(stats));
629ed0b10   Tejun Heo   blkcg: move stati...
546
547
  
  	stats->start_idle_time = sched_clock();
155fead9b   Tejun Heo   blkcg: move blkio...
548
  	cfqg_stats_mark_idling(stats);
629ed0b10   Tejun Heo   blkcg: move stati...
549
  }
155fead9b   Tejun Heo   blkcg: move blkio...
550
  static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg)
629ed0b10   Tejun Heo   blkcg: move stati...
551
  {
155fead9b   Tejun Heo   blkcg: move blkio...
552
  	struct cfqg_stats *stats = &cfqg->stats;
629ed0b10   Tejun Heo   blkcg: move stati...
553
554
  
  	blkg_stat_add(&stats->avg_queue_size_sum,
4d5e80a76   Tejun Heo   blkcg: s/blkg_rws...
555
  		      blkg_rwstat_total(&stats->queued));
629ed0b10   Tejun Heo   blkcg: move stati...
556
  	blkg_stat_add(&stats->avg_queue_size_samples, 1);
155fead9b   Tejun Heo   blkcg: move blkio...
557
  	cfqg_stats_update_group_wait_time(stats);
629ed0b10   Tejun Heo   blkcg: move stati...
558
559
560
  }
  
  #else	/* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
f48ec1d78   Tejun Heo   cfq: fix build br...
561
562
563
564
565
566
567
  static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, struct cfq_group *curr_cfqg) { }
  static inline void cfqg_stats_end_empty_time(struct cfqg_stats *stats) { }
  static inline void cfqg_stats_update_dequeue(struct cfq_group *cfqg) { }
  static inline void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) { }
  static inline void cfqg_stats_update_idle_time(struct cfq_group *cfqg) { }
  static inline void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) { }
  static inline void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) { }
629ed0b10   Tejun Heo   blkcg: move stati...
568
569
570
571
  
  #endif	/* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
  
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
572

ffea73fc7   Tejun Heo   block: blkcg_poli...
573
574
575
576
577
578
  static struct blkcg_policy blkcg_policy_cfq;
  
  static inline struct cfq_group *blkg_to_cfqg(struct blkcg_gq *blkg)
  {
  	return pd_to_cfqg(blkg_to_pd(blkg, &blkcg_policy_cfq));
  }
d02f7aa8d   Tejun Heo   cfq-iosched: enab...
579
  static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg)
7918ffb5b   Tejun Heo   cfq-iosched: impl...
580
  {
d02f7aa8d   Tejun Heo   cfq-iosched: enab...
581
  	struct blkcg_gq *pblkg = cfqg_to_blkg(cfqg)->parent;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
582

d02f7aa8d   Tejun Heo   cfq-iosched: enab...
583
  	return pblkg ? blkg_to_cfqg(pblkg) : NULL;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
584
  }
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
585
586
587
588
589
590
591
592
593
  static inline void cfqg_get(struct cfq_group *cfqg)
  {
  	return blkg_get(cfqg_to_blkg(cfqg));
  }
  
  static inline void cfqg_put(struct cfq_group *cfqg)
  {
  	return blkg_put(cfqg_to_blkg(cfqg));
  }
54e7ed12b   Tejun Heo   blkcg: remove blk...
594
595
596
597
  #define cfq_log_cfqq(cfqd, cfqq, fmt, args...)	do {			\
  	char __pbuf[128];						\
  									\
  	blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf));	\
b226e5c41   Vivek Goyal   cfq-iosched: Prin...
598
599
600
  	blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \
  			cfq_cfqq_sync((cfqq)) ? 'S' : 'A',		\
  			cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
54e7ed12b   Tejun Heo   blkcg: remove blk...
601
602
603
604
605
606
607
608
609
  			  __pbuf, ##args);				\
  } while (0)
  
  #define cfq_log_cfqg(cfqd, cfqg, fmt, args...)	do {			\
  	char __pbuf[128];						\
  									\
  	blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf));		\
  	blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args);	\
  } while (0)
2868ef7b3   Vivek Goyal   blkio: Some debug...
610

155fead9b   Tejun Heo   blkcg: move blkio...
611
612
  static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
  					    struct cfq_group *curr_cfqg, int rw)
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
613
  {
155fead9b   Tejun Heo   blkcg: move blkio...
614
615
616
  	blkg_rwstat_add(&cfqg->stats.queued, rw, 1);
  	cfqg_stats_end_empty_time(&cfqg->stats);
  	cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg);
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
617
  }
155fead9b   Tejun Heo   blkcg: move blkio...
618
619
  static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
  			unsigned long time, unsigned long unaccounted_time)
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
620
  {
155fead9b   Tejun Heo   blkcg: move blkio...
621
  	blkg_stat_add(&cfqg->stats.time, time);
629ed0b10   Tejun Heo   blkcg: move stati...
622
  #ifdef CONFIG_DEBUG_BLK_CGROUP
155fead9b   Tejun Heo   blkcg: move blkio...
623
  	blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time);
629ed0b10   Tejun Heo   blkcg: move stati...
624
  #endif
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
625
  }
155fead9b   Tejun Heo   blkcg: move blkio...
626
  static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw)
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
627
  {
155fead9b   Tejun Heo   blkcg: move blkio...
628
  	blkg_rwstat_add(&cfqg->stats.queued, rw, -1);
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
629
  }
155fead9b   Tejun Heo   blkcg: move blkio...
630
  static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw)
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
631
  {
155fead9b   Tejun Heo   blkcg: move blkio...
632
  	blkg_rwstat_add(&cfqg->stats.merged, rw, 1);
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
633
  }
155fead9b   Tejun Heo   blkcg: move blkio...
634
635
  static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
  					      uint64_t bytes, int rw)
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
636
  {
155fead9b   Tejun Heo   blkcg: move blkio...
637
638
639
  	blkg_stat_add(&cfqg->stats.sectors, bytes >> 9);
  	blkg_rwstat_add(&cfqg->stats.serviced, rw, 1);
  	blkg_rwstat_add(&cfqg->stats.service_bytes, rw, bytes);
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
640
  }
155fead9b   Tejun Heo   blkcg: move blkio...
641
642
  static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
  			uint64_t start_time, uint64_t io_start_time, int rw)
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
643
  {
155fead9b   Tejun Heo   blkcg: move blkio...
644
  	struct cfqg_stats *stats = &cfqg->stats;
629ed0b10   Tejun Heo   blkcg: move stati...
645
  	unsigned long long now = sched_clock();
629ed0b10   Tejun Heo   blkcg: move stati...
646
647
648
649
650
651
  
  	if (time_after64(now, io_start_time))
  		blkg_rwstat_add(&stats->service_time, rw, now - io_start_time);
  	if (time_after64(io_start_time, start_time))
  		blkg_rwstat_add(&stats->wait_time, rw,
  				io_start_time - start_time);
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
652
  }
689665af4   Tejun Heo   cfq-iosched: sepa...
653
654
  /* @stats = 0 */
  static void cfqg_stats_reset(struct cfqg_stats *stats)
155fead9b   Tejun Heo   blkcg: move blkio...
655
  {
155fead9b   Tejun Heo   blkcg: move blkio...
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
  	/* queued stats shouldn't be cleared */
  	blkg_rwstat_reset(&stats->service_bytes);
  	blkg_rwstat_reset(&stats->serviced);
  	blkg_rwstat_reset(&stats->merged);
  	blkg_rwstat_reset(&stats->service_time);
  	blkg_rwstat_reset(&stats->wait_time);
  	blkg_stat_reset(&stats->time);
  #ifdef CONFIG_DEBUG_BLK_CGROUP
  	blkg_stat_reset(&stats->unaccounted_time);
  	blkg_stat_reset(&stats->avg_queue_size_sum);
  	blkg_stat_reset(&stats->avg_queue_size_samples);
  	blkg_stat_reset(&stats->dequeue);
  	blkg_stat_reset(&stats->group_wait_time);
  	blkg_stat_reset(&stats->idle_time);
  	blkg_stat_reset(&stats->empty_time);
  #endif
  }
0b39920b5   Tejun Heo   cfq-iosched: coll...
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
  /* @to += @from */
  static void cfqg_stats_merge(struct cfqg_stats *to, struct cfqg_stats *from)
  {
  	/* queued stats shouldn't be cleared */
  	blkg_rwstat_merge(&to->service_bytes, &from->service_bytes);
  	blkg_rwstat_merge(&to->serviced, &from->serviced);
  	blkg_rwstat_merge(&to->merged, &from->merged);
  	blkg_rwstat_merge(&to->service_time, &from->service_time);
  	blkg_rwstat_merge(&to->wait_time, &from->wait_time);
  	blkg_stat_merge(&from->time, &from->time);
  #ifdef CONFIG_DEBUG_BLK_CGROUP
  	blkg_stat_merge(&to->unaccounted_time, &from->unaccounted_time);
  	blkg_stat_merge(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
  	blkg_stat_merge(&to->avg_queue_size_samples, &from->avg_queue_size_samples);
  	blkg_stat_merge(&to->dequeue, &from->dequeue);
  	blkg_stat_merge(&to->group_wait_time, &from->group_wait_time);
  	blkg_stat_merge(&to->idle_time, &from->idle_time);
  	blkg_stat_merge(&to->empty_time, &from->empty_time);
  #endif
  }
  
  /*
   * Transfer @cfqg's stats to its parent's dead_stats so that the ancestors'
   * recursive stats can still account for the amount used by this cfqg after
   * it's gone.
   */
  static void cfqg_stats_xfer_dead(struct cfq_group *cfqg)
  {
  	struct cfq_group *parent = cfqg_parent(cfqg);
  
  	lockdep_assert_held(cfqg_to_blkg(cfqg)->q->queue_lock);
  
  	if (unlikely(!parent))
  		return;
  
  	cfqg_stats_merge(&parent->dead_stats, &cfqg->stats);
  	cfqg_stats_merge(&parent->dead_stats, &cfqg->dead_stats);
  	cfqg_stats_reset(&cfqg->stats);
  	cfqg_stats_reset(&cfqg->dead_stats);
  }
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
713
  #else	/* CONFIG_CFQ_GROUP_IOSCHED */
d02f7aa8d   Tejun Heo   cfq-iosched: enab...
714
  static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) { return NULL; }
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
715
716
  static inline void cfqg_get(struct cfq_group *cfqg) { }
  static inline void cfqg_put(struct cfq_group *cfqg) { }
7b679138b   Jens Axboe   cfq-iosched: add ...
717
  #define cfq_log_cfqq(cfqd, cfqq, fmt, args...)	\
b226e5c41   Vivek Goyal   cfq-iosched: Prin...
718
719
720
721
  	blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid,	\
  			cfq_cfqq_sync((cfqq)) ? 'S' : 'A',		\
  			cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
  				##args)
4495a7d41   Kyungmin Park   CFQ: Fix typo and...
722
  #define cfq_log_cfqg(cfqd, cfqg, fmt, args...)		do {} while (0)
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
723

155fead9b   Tejun Heo   blkcg: move blkio...
724
725
726
727
728
729
730
731
732
733
  static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
  			struct cfq_group *curr_cfqg, int rw) { }
  static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
  			unsigned long time, unsigned long unaccounted_time) { }
  static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw) { }
  static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw) { }
  static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
  					      uint64_t bytes, int rw) { }
  static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
  			uint64_t start_time, uint64_t io_start_time, int rw) { }
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
734

eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
735
  #endif	/* CONFIG_CFQ_GROUP_IOSCHED */
7b679138b   Jens Axboe   cfq-iosched: add ...
736
737
  #define cfq_log(cfqd, fmt, args...)	\
  	blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
615f0259e   Vivek Goyal   blkio: Implement ...
738
739
740
741
742
743
744
745
746
  /* Traverses through cfq group service trees */
  #define for_each_cfqg_st(cfqg, i, j, st) \
  	for (i = 0; i <= IDLE_WORKLOAD; i++) \
  		for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
  			: &cfqg->service_tree_idle; \
  			(i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
  			(i == IDLE_WORKLOAD && j == 0); \
  			j++, st = i < IDLE_WORKLOAD ? \
  			&cfqg->service_trees[i][j]: NULL) \
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
747
748
749
750
751
752
753
754
755
756
757
758
  static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
  	struct cfq_ttime *ttime, bool group_idle)
  {
  	unsigned long slice;
  	if (!sample_valid(ttime->ttime_samples))
  		return false;
  	if (group_idle)
  		slice = cfqd->cfq_group_idle;
  	else
  		slice = cfqd->cfq_slice_idle;
  	return ttime->ttime_mean > slice;
  }
615f0259e   Vivek Goyal   blkio: Implement ...
759

02b35081f   Vivek Goyal   cfq-iosched: Do g...
760
761
762
763
764
765
766
767
768
769
770
771
772
773
  static inline bool iops_mode(struct cfq_data *cfqd)
  {
  	/*
  	 * If we are not idling on queues and it is a NCQ drive, parallel
  	 * execution of requests is on and measuring time is not possible
  	 * in most of the cases until and unless we drive shallower queue
  	 * depths and that becomes a performance bottleneck. In such cases
  	 * switch to start providing fairness in terms of number of IOs.
  	 */
  	if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
  		return true;
  	else
  		return false;
  }
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
774
  static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq)
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
775
776
777
778
779
780
781
  {
  	if (cfq_class_idle(cfqq))
  		return IDLE_WORKLOAD;
  	if (cfq_class_rt(cfqq))
  		return RT_WORKLOAD;
  	return BE_WORKLOAD;
  }
718eee057   Corrado Zoccolo   cfq-iosched: fair...
782
783
784
785
786
787
788
789
790
  
  static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
  {
  	if (!cfq_cfqq_sync(cfqq))
  		return ASYNC_WORKLOAD;
  	if (!cfq_cfqq_idle_window(cfqq))
  		return SYNC_NOIDLE_WORKLOAD;
  	return SYNC_WORKLOAD;
  }
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
791
  static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class,
58ff82f34   Vivek Goyal   blkio: Implement ...
792
793
  					struct cfq_data *cfqd,
  					struct cfq_group *cfqg)
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
794
  {
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
795
  	if (wl_class == IDLE_WORKLOAD)
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
796
  		return cfqg->service_tree_idle.count;
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
797

34b98d03b   Vivek Goyal   cfq-iosched: Rena...
798
799
800
  	return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count +
  		cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count +
  		cfqg->service_trees[wl_class][SYNC_WORKLOAD].count;
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
801
  }
f26bd1f0a   Vivek Goyal   blkio: Determine ...
802
803
804
  static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
  					struct cfq_group *cfqg)
  {
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
805
806
  	return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count +
  		cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
f26bd1f0a   Vivek Goyal   blkio: Determine ...
807
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
808
  static void cfq_dispatch_insert(struct request_queue *, struct request *);
4f85cb96d   Tejun Heo   block: make block...
809
  static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
abede6da2   Tejun Heo   cfq: pass around ...
810
  				       struct cfq_io_cq *cic, struct bio *bio,
4f85cb96d   Tejun Heo   block: make block...
811
  				       gfp_t gfp_mask);
91fac317a   Vasily Tarasov   cfq-iosched: get ...
812

c58698073   Tejun Heo   block, cfq: reorg...
813
814
815
816
817
  static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
  {
  	/* cic->icq is the first member, %NULL will convert to %NULL */
  	return container_of(icq, struct cfq_io_cq, icq);
  }
47fdd4ca9   Tejun Heo   block, cfq: move ...
818
819
820
821
822
823
824
  static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
  					       struct io_context *ioc)
  {
  	if (ioc)
  		return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
  	return NULL;
  }
c58698073   Tejun Heo   block, cfq: reorg...
825
  static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
91fac317a   Vasily Tarasov   cfq-iosched: get ...
826
  {
a6151c3a5   Jens Axboe   cfq-iosched: appl...
827
  	return cic->cfqq[is_sync];
91fac317a   Vasily Tarasov   cfq-iosched: get ...
828
  }
c58698073   Tejun Heo   block, cfq: reorg...
829
830
  static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
  				bool is_sync)
91fac317a   Vasily Tarasov   cfq-iosched: get ...
831
  {
a6151c3a5   Jens Axboe   cfq-iosched: appl...
832
  	cic->cfqq[is_sync] = cfqq;
91fac317a   Vasily Tarasov   cfq-iosched: get ...
833
  }
c58698073   Tejun Heo   block, cfq: reorg...
834
  static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
bca4b914b   Konstantin Khlebnikov   cfq-iosched: remo...
835
  {
c58698073   Tejun Heo   block, cfq: reorg...
836
  	return cic->icq.q->elevator->elevator_data;
bca4b914b   Konstantin Khlebnikov   cfq-iosched: remo...
837
  }
91fac317a   Vasily Tarasov   cfq-iosched: get ...
838
839
840
841
  /*
   * We regard a request as SYNC, if it's either a read or has the SYNC bit
   * set (in which case it could also be direct WRITE).
   */
a6151c3a5   Jens Axboe   cfq-iosched: appl...
842
  static inline bool cfq_bio_sync(struct bio *bio)
91fac317a   Vasily Tarasov   cfq-iosched: get ...
843
  {
7b6d91dae   Christoph Hellwig   block: unify flag...
844
  	return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC);
91fac317a   Vasily Tarasov   cfq-iosched: get ...
845
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
846

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
847
  /*
99f95e528   Andrew Morton   [PATCH] cfq build...
848
849
850
   * scheduler run of queue, if there are requests pending and no one in the
   * driver that will restart queueing
   */
23e018a1b   Jens Axboe   block: get rid of...
851
  static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
99f95e528   Andrew Morton   [PATCH] cfq build...
852
  {
7b679138b   Jens Axboe   cfq-iosched: add ...
853
854
  	if (cfqd->busy_queues) {
  		cfq_log(cfqd, "schedule dispatch");
59c3d45e4   Jens Axboe   block: remove 'q'...
855
  		kblockd_schedule_work(&cfqd->unplug_work);
7b679138b   Jens Axboe   cfq-iosched: add ...
856
  	}
99f95e528   Andrew Morton   [PATCH] cfq build...
857
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
858
  /*
44f7c1606   Jens Axboe   cfq-iosched: defe...
859
860
861
862
   * Scale schedule slice based on io priority. Use the sync time slice only
   * if a queue is marked sync and has sync io queued. A sync queue with async
   * io only, should not get full sync slice length.
   */
a6151c3a5   Jens Axboe   cfq-iosched: appl...
863
  static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
864
  				 unsigned short prio)
44f7c1606   Jens Axboe   cfq-iosched: defe...
865
  {
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
866
  	const int base_slice = cfqd->cfq_slice[sync];
44f7c1606   Jens Axboe   cfq-iosched: defe...
867

d9e7620e6   Jens Axboe   cfq-iosched: rewo...
868
869
870
871
  	WARN_ON(prio >= IOPRIO_BE_NR);
  
  	return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
  }
44f7c1606   Jens Axboe   cfq-iosched: defe...
872

d9e7620e6   Jens Axboe   cfq-iosched: rewo...
873
874
875
876
  static inline int
  cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
  	return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
44f7c1606   Jens Axboe   cfq-iosched: defe...
877
  }
1d3650f71   Tejun Heo   cfq-iosched: impl...
878
879
880
881
882
883
884
885
886
887
888
889
890
891
  /**
   * cfqg_scale_charge - scale disk time charge according to cfqg weight
   * @charge: disk time being charged
   * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT
   *
   * Scale @charge according to @vfraction, which is in range (0, 1].  The
   * scaling is inversely proportional.
   *
   * scaled = charge / vfraction
   *
   * The result is also in fixed point w/ CFQ_SERVICE_SHIFT.
   */
  static inline u64 cfqg_scale_charge(unsigned long charge,
  				    unsigned int vfraction)
25bc6b077   Vivek Goyal   blkio: Introduce ...
892
  {
1d3650f71   Tejun Heo   cfq-iosched: impl...
893
  	u64 c = charge << CFQ_SERVICE_SHIFT;	/* make it fixed point */
25bc6b077   Vivek Goyal   blkio: Introduce ...
894

1d3650f71   Tejun Heo   cfq-iosched: impl...
895
896
897
898
  	/* charge / vfraction */
  	c <<= CFQ_SERVICE_SHIFT;
  	do_div(c, vfraction);
  	return c;
25bc6b077   Vivek Goyal   blkio: Introduce ...
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
  }
  
  static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
  {
  	s64 delta = (s64)(vdisktime - min_vdisktime);
  	if (delta > 0)
  		min_vdisktime = vdisktime;
  
  	return min_vdisktime;
  }
  
  static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
  {
  	s64 delta = (s64)(vdisktime - min_vdisktime);
  	if (delta < 0)
  		min_vdisktime = vdisktime;
  
  	return min_vdisktime;
  }
  
  static void update_min_vdisktime(struct cfq_rb_root *st)
  {
25bc6b077   Vivek Goyal   blkio: Introduce ...
921
  	struct cfq_group *cfqg;
25bc6b077   Vivek Goyal   blkio: Introduce ...
922
923
  	if (st->left) {
  		cfqg = rb_entry_cfqg(st->left);
a60327107   Gui Jianfeng   cfq-iosched: Fix ...
924
925
  		st->min_vdisktime = max_vdisktime(st->min_vdisktime,
  						  cfqg->vdisktime);
25bc6b077   Vivek Goyal   blkio: Introduce ...
926
  	}
25bc6b077   Vivek Goyal   blkio: Introduce ...
927
  }
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
928
929
930
931
932
  /*
   * get averaged number of queues of RT/BE priority.
   * average is updated, with a formula that gives more weight to higher numbers,
   * to quickly follows sudden increases and decrease slowly
   */
58ff82f34   Vivek Goyal   blkio: Implement ...
933
934
  static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
  					struct cfq_group *cfqg, bool rt)
5869619cb   Jens Axboe   cfq-iosched: fix ...
935
  {
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
936
937
938
  	unsigned min_q, max_q;
  	unsigned mult  = cfq_hist_divisor - 1;
  	unsigned round = cfq_hist_divisor / 2;
58ff82f34   Vivek Goyal   blkio: Implement ...
939
  	unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
940

58ff82f34   Vivek Goyal   blkio: Implement ...
941
942
943
  	min_q = min(cfqg->busy_queues_avg[rt], busy);
  	max_q = max(cfqg->busy_queues_avg[rt], busy);
  	cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
944
  		cfq_hist_divisor;
58ff82f34   Vivek Goyal   blkio: Implement ...
945
946
947
948
949
950
  	return cfqg->busy_queues_avg[rt];
  }
  
  static inline unsigned
  cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
  {
41cad6ab2   Tejun Heo   cfq-iosched: conv...
951
  	return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT;
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
952
  }
c553f8e33   Shaohua Li   block cfq: compen...
953
  static inline unsigned
ba5bd520f   Vivek Goyal   cfq: rename a fun...
954
  cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
44f7c1606   Jens Axboe   cfq-iosched: defe...
955
  {
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
956
957
  	unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
  	if (cfqd->cfq_latency) {
58ff82f34   Vivek Goyal   blkio: Implement ...
958
959
960
961
962
963
  		/*
  		 * interested queues (we consider only the ones with the same
  		 * priority class in the cfq group)
  		 */
  		unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
  						cfq_class_rt(cfqq));
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
964
965
  		unsigned sync_slice = cfqd->cfq_slice[1];
  		unsigned expect_latency = sync_slice * iq;
58ff82f34   Vivek Goyal   blkio: Implement ...
966
967
968
  		unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
  
  		if (expect_latency > group_slice) {
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
969
970
971
972
973
974
975
  			unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
  			/* scale low_slice according to IO priority
  			 * and sync vs async */
  			unsigned low_slice =
  				min(slice, base_low_slice * slice / sync_slice);
  			/* the adapted slice value is scaled to fit all iqs
  			 * into the target latency */
58ff82f34   Vivek Goyal   blkio: Implement ...
976
  			slice = max(slice * group_slice / expect_latency,
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
977
978
979
  				    low_slice);
  		}
  	}
c553f8e33   Shaohua Li   block cfq: compen...
980
981
982
983
984
985
  	return slice;
  }
  
  static inline void
  cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
ba5bd520f   Vivek Goyal   cfq: rename a fun...
986
  	unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
c553f8e33   Shaohua Li   block cfq: compen...
987

dae739ebc   Vivek Goyal   blkio: Group time...
988
  	cfqq->slice_start = jiffies;
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
989
  	cfqq->slice_end = jiffies + slice;
f75edf2dc   Vivek Goyal   blkio: Wait for c...
990
  	cfqq->allocated_slice = slice;
7b679138b   Jens Axboe   cfq-iosched: add ...
991
  	cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
44f7c1606   Jens Axboe   cfq-iosched: defe...
992
993
994
995
996
997
998
  }
  
  /*
   * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
   * isn't valid until the first request from the dispatch is activated
   * and the slice time set.
   */
a6151c3a5   Jens Axboe   cfq-iosched: appl...
999
  static inline bool cfq_slice_used(struct cfq_queue *cfqq)
44f7c1606   Jens Axboe   cfq-iosched: defe...
1000
1001
  {
  	if (cfq_cfqq_slice_new(cfqq))
c1e44756f   Shaohua Li   cfq-iosched: do c...
1002
  		return false;
44f7c1606   Jens Axboe   cfq-iosched: defe...
1003
  	if (time_before(jiffies, cfqq->slice_end))
c1e44756f   Shaohua Li   cfq-iosched: do c...
1004
  		return false;
44f7c1606   Jens Axboe   cfq-iosched: defe...
1005

c1e44756f   Shaohua Li   cfq-iosched: do c...
1006
  	return true;
44f7c1606   Jens Axboe   cfq-iosched: defe...
1007
1008
1009
  }
  
  /*
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1010
   * Lifted from AS - choose which of rq1 and rq2 that is best served now.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1011
   * We choose the request that is closest to the head right now. Distance
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1012
   * behind the head is penalized and only allowed to a certain extent.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1013
   */
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1014
  static struct request *
cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
1015
  cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1016
  {
cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
1017
  	sector_t s1, s2, d1 = 0, d2 = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1018
  	unsigned long back_max;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1019
1020
1021
  #define CFQ_RQ1_WRAP	0x01 /* request 1 wraps */
  #define CFQ_RQ2_WRAP	0x02 /* request 2 wraps */
  	unsigned wrap = 0; /* bit mask: requests behind the disk head? */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1022

5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1023
1024
1025
1026
  	if (rq1 == NULL || rq1 == rq2)
  		return rq2;
  	if (rq2 == NULL)
  		return rq1;
9c2c38a12   Jens Axboe   [PATCH] cfq-iosch...
1027

229836bd6   Namhyung Kim   cfq-iosched: redu...
1028
1029
  	if (rq_is_sync(rq1) != rq_is_sync(rq2))
  		return rq_is_sync(rq1) ? rq1 : rq2;
65299a3b7   Christoph Hellwig   block: separate p...
1030
1031
  	if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
  		return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
b53d1ed73   Jens Axboe   Revert "cfq: Remo...
1032

83096ebf1   Tejun Heo   block: convert to...
1033
1034
  	s1 = blk_rq_pos(rq1);
  	s2 = blk_rq_pos(rq2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1035

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
  	/*
  	 * by definition, 1KiB is 2 sectors
  	 */
  	back_max = cfqd->cfq_back_max * 2;
  
  	/*
  	 * Strict one way elevator _except_ in the case where we allow
  	 * short backward seeks which are biased as twice the cost of a
  	 * similar forward seek.
  	 */
  	if (s1 >= last)
  		d1 = s1 - last;
  	else if (s1 + back_max >= last)
  		d1 = (last - s1) * cfqd->cfq_back_penalty;
  	else
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1051
  		wrap |= CFQ_RQ1_WRAP;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1052
1053
1054
1055
1056
1057
  
  	if (s2 >= last)
  		d2 = s2 - last;
  	else if (s2 + back_max >= last)
  		d2 = (last - s2) * cfqd->cfq_back_penalty;
  	else
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1058
  		wrap |= CFQ_RQ2_WRAP;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1059
1060
  
  	/* Found required data */
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1061
1062
1063
1064
1065
1066
  
  	/*
  	 * By doing switch() on the bit mask "wrap" we avoid having to
  	 * check two variables for all permutations: --> faster!
  	 */
  	switch (wrap) {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1067
  	case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1068
  		if (d1 < d2)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1069
  			return rq1;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1070
  		else if (d2 < d1)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1071
  			return rq2;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1072
1073
  		else {
  			if (s1 >= s2)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1074
  				return rq1;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1075
  			else
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1076
  				return rq2;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1077
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1078

e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1079
  	case CFQ_RQ2_WRAP:
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1080
  		return rq1;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1081
  	case CFQ_RQ1_WRAP:
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1082
1083
  		return rq2;
  	case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1084
1085
1086
1087
1088
1089
1090
1091
  	default:
  		/*
  		 * Since both rqs are wrapped,
  		 * start with the one that's further behind head
  		 * (--> only *one* back seek required),
  		 * since back seek takes more time than forward.
  		 */
  		if (s1 <= s2)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1092
  			return rq1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1093
  		else
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1094
  			return rq2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1095
1096
  	}
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
1097
1098
1099
  /*
   * The below is leftmost cache rbtree addon
   */
0871714e0   Jens Axboe   cfq-iosched: rela...
1100
  static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
1101
  {
615f0259e   Vivek Goyal   blkio: Implement ...
1102
1103
1104
  	/* Service tree is empty */
  	if (!root->count)
  		return NULL;
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
1105
1106
  	if (!root->left)
  		root->left = rb_first(&root->rb);
0871714e0   Jens Axboe   cfq-iosched: rela...
1107
1108
1109
1110
  	if (root->left)
  		return rb_entry(root->left, struct cfq_queue, rb_node);
  
  	return NULL;
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
1111
  }
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
  static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
  {
  	if (!root->left)
  		root->left = rb_first(&root->rb);
  
  	if (root->left)
  		return rb_entry_cfqg(root->left);
  
  	return NULL;
  }
a36e71f99   Jens Axboe   cfq-iosched: add ...
1122
1123
1124
1125
1126
  static void rb_erase_init(struct rb_node *n, struct rb_root *root)
  {
  	rb_erase(n, root);
  	RB_CLEAR_NODE(n);
  }
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
1127
1128
1129
1130
  static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
  {
  	if (root->left == n)
  		root->left = NULL;
a36e71f99   Jens Axboe   cfq-iosched: add ...
1131
  	rb_erase_init(n, &root->rb);
aa6f6a3de   Corrado Zoccolo   cfq-iosched: prep...
1132
  	--root->count;
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
1133
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1134
1135
1136
  /*
   * would be nice to take fifo expire time into account as well
   */
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1137
1138
1139
  static struct request *
  cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  		  struct request *last)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1140
  {
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
1141
1142
  	struct rb_node *rbnext = rb_next(&last->rb_node);
  	struct rb_node *rbprev = rb_prev(&last->rb_node);
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1143
  	struct request *next = NULL, *prev = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1144

21183b07e   Jens Axboe   [PATCH] cfq-iosch...
1145
  	BUG_ON(RB_EMPTY_NODE(&last->rb_node));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1146
1147
  
  	if (rbprev)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1148
  		prev = rb_entry_rq(rbprev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1149

21183b07e   Jens Axboe   [PATCH] cfq-iosch...
1150
  	if (rbnext)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1151
  		next = rb_entry_rq(rbnext);
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
1152
1153
1154
  	else {
  		rbnext = rb_first(&cfqq->sort_list);
  		if (rbnext && rbnext != &last->rb_node)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1155
  			next = rb_entry_rq(rbnext);
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
1156
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1157

cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
1158
  	return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1159
  }
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
1160
1161
  static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
  				      struct cfq_queue *cfqq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1162
  {
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
1163
1164
1165
  	/*
  	 * just an approximation, should be ok.
  	 */
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
1166
  	return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
464191c65   Jens Axboe   Revert "cfq: Make...
1167
  		       cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
1168
  }
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
  static inline s64
  cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
  {
  	return cfqg->vdisktime - st->min_vdisktime;
  }
  
  static void
  __cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
  {
  	struct rb_node **node = &st->rb.rb_node;
  	struct rb_node *parent = NULL;
  	struct cfq_group *__cfqg;
  	s64 key = cfqg_key(st, cfqg);
  	int left = 1;
  
  	while (*node != NULL) {
  		parent = *node;
  		__cfqg = rb_entry_cfqg(parent);
  
  		if (key < cfqg_key(st, __cfqg))
  			node = &parent->rb_left;
  		else {
  			node = &parent->rb_right;
  			left = 0;
  		}
  	}
  
  	if (left)
  		st->left = &cfqg->rb_node;
  
  	rb_link_node(&cfqg->rb_node, parent, node);
  	rb_insert_color(&cfqg->rb_node, &st->rb);
  }
7b5af5cff   Toshiaki Makita   cfq-iosched: Add ...
1202
1203
1204
  /*
   * This has to be called only on activation of cfqg
   */
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1205
  static void
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1206
1207
  cfq_update_group_weight(struct cfq_group *cfqg)
  {
3381cb8d2   Tejun Heo   blkcg: move blkio...
1208
  	if (cfqg->new_weight) {
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1209
  		cfqg->weight = cfqg->new_weight;
3381cb8d2   Tejun Heo   blkcg: move blkio...
1210
  		cfqg->new_weight = 0;
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1211
  	}
e15693ef1   Toshiaki Makita   cfq-iosched: Fix ...
1212
1213
1214
1215
1216
1217
  }
  
  static void
  cfq_update_group_leaf_weight(struct cfq_group *cfqg)
  {
  	BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
e71357e11   Tejun Heo   cfq-iosched: add ...
1218
1219
1220
1221
1222
  
  	if (cfqg->new_leaf_weight) {
  		cfqg->leaf_weight = cfqg->new_leaf_weight;
  		cfqg->new_leaf_weight = 0;
  	}
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1223
1224
1225
1226
1227
  }
  
  static void
  cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
  {
1d3650f71   Tejun Heo   cfq-iosched: impl...
1228
  	unsigned int vfr = 1 << CFQ_SERVICE_SHIFT;	/* start with 1 */
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1229
  	struct cfq_group *pos = cfqg;
1d3650f71   Tejun Heo   cfq-iosched: impl...
1230
  	struct cfq_group *parent;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1231
1232
1233
  	bool propagate;
  
  	/* add to the service tree */
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1234
  	BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
7b5af5cff   Toshiaki Makita   cfq-iosched: Add ...
1235
1236
1237
1238
1239
  	/*
  	 * Update leaf_weight.  We cannot update weight at this point
  	 * because cfqg might already have been activated and is
  	 * contributing its current weight to the parent's child_weight.
  	 */
e15693ef1   Toshiaki Makita   cfq-iosched: Fix ...
1240
  	cfq_update_group_leaf_weight(cfqg);
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1241
  	__cfq_group_service_tree_add(st, cfqg);
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1242
1243
  
  	/*
1d3650f71   Tejun Heo   cfq-iosched: impl...
1244
1245
1246
1247
1248
1249
1250
  	 * Activate @cfqg and calculate the portion of vfraction @cfqg is
  	 * entitled to.  vfraction is calculated by walking the tree
  	 * towards the root calculating the fraction it has at each level.
  	 * The compounded ratio is how much vfraction @cfqg owns.
  	 *
  	 * Start with the proportion tasks in this cfqg has against active
  	 * children cfqgs - its leaf_weight against children_weight.
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1251
1252
1253
  	 */
  	propagate = !pos->nr_active++;
  	pos->children_weight += pos->leaf_weight;
1d3650f71   Tejun Heo   cfq-iosched: impl...
1254
  	vfr = vfr * pos->leaf_weight / pos->children_weight;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1255

1d3650f71   Tejun Heo   cfq-iosched: impl...
1256
1257
1258
1259
1260
1261
  	/*
  	 * Compound ->weight walking up the tree.  Both activation and
  	 * vfraction calculation are done in the same loop.  Propagation
  	 * stops once an already activated node is met.  vfraction
  	 * calculation should always continue to the root.
  	 */
d02f7aa8d   Tejun Heo   cfq-iosched: enab...
1262
  	while ((parent = cfqg_parent(pos))) {
1d3650f71   Tejun Heo   cfq-iosched: impl...
1263
  		if (propagate) {
e15693ef1   Toshiaki Makita   cfq-iosched: Fix ...
1264
  			cfq_update_group_weight(pos);
1d3650f71   Tejun Heo   cfq-iosched: impl...
1265
1266
1267
1268
  			propagate = !parent->nr_active++;
  			parent->children_weight += pos->weight;
  		}
  		vfr = vfr * pos->weight / parent->children_weight;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1269
1270
  		pos = parent;
  	}
1d3650f71   Tejun Heo   cfq-iosched: impl...
1271
1272
  
  	cfqg->vfraction = max_t(unsigned, vfr, 1);
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1273
1274
1275
1276
  }
  
  static void
  cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1277
1278
1279
1280
1281
1282
  {
  	struct cfq_rb_root *st = &cfqd->grp_service_tree;
  	struct cfq_group *__cfqg;
  	struct rb_node *n;
  
  	cfqg->nr_cfqq++;
760701bfe   Gui Jianfeng   cfq-iosched: Get ...
1283
  	if (!RB_EMPTY_NODE(&cfqg->rb_node))
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1284
1285
1286
1287
1288
  		return;
  
  	/*
  	 * Currently put the group at the end. Later implement something
  	 * so that groups get lesser vtime based on their weights, so that
25985edce   Lucas De Marchi   Fix common misspe...
1289
  	 * if group does not loose all if it was not continuously backlogged.
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1290
1291
1292
1293
1294
1295
1296
  	 */
  	n = rb_last(&st->rb);
  	if (n) {
  		__cfqg = rb_entry_cfqg(n);
  		cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
  	} else
  		cfqg->vdisktime = st->min_vdisktime;
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1297
1298
  	cfq_group_service_tree_add(st, cfqg);
  }
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1299

8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1300
1301
1302
  static void
  cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
  {
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
  	struct cfq_group *pos = cfqg;
  	bool propagate;
  
  	/*
  	 * Undo activation from cfq_group_service_tree_add().  Deactivate
  	 * @cfqg and propagate deactivation upwards.
  	 */
  	propagate = !--pos->nr_active;
  	pos->children_weight -= pos->leaf_weight;
  
  	while (propagate) {
d02f7aa8d   Tejun Heo   cfq-iosched: enab...
1314
  		struct cfq_group *parent = cfqg_parent(pos);
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1315
1316
1317
  
  		/* @pos has 0 nr_active at this point */
  		WARN_ON_ONCE(pos->children_weight);
1d3650f71   Tejun Heo   cfq-iosched: impl...
1318
  		pos->vfraction = 0;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
  
  		if (!parent)
  			break;
  
  		propagate = !--parent->nr_active;
  		parent->children_weight -= pos->weight;
  		pos = parent;
  	}
  
  	/* remove from the service tree */
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1329
1330
  	if (!RB_EMPTY_NODE(&cfqg->rb_node))
  		cfq_rb_erase(&cfqg->rb_node, st);
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1331
1332
1333
  }
  
  static void
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1334
  cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1335
1336
1337
1338
1339
  {
  	struct cfq_rb_root *st = &cfqd->grp_service_tree;
  
  	BUG_ON(cfqg->nr_cfqq < 1);
  	cfqg->nr_cfqq--;
25bc6b077   Vivek Goyal   blkio: Introduce ...
1340

1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1341
1342
1343
  	/* If there are other cfq queues under this group, don't delete it */
  	if (cfqg->nr_cfqq)
  		return;
2868ef7b3   Vivek Goyal   blkio: Some debug...
1344
  	cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1345
  	cfq_group_service_tree_del(st, cfqg);
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
1346
  	cfqg->saved_wl_slice = 0;
155fead9b   Tejun Heo   blkcg: move blkio...
1347
  	cfqg_stats_update_dequeue(cfqg);
dae739ebc   Vivek Goyal   blkio: Group time...
1348
  }
167400d34   Justin TerAvest   blk-cgroup: Add u...
1349
1350
  static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
  						unsigned int *unaccounted_time)
dae739ebc   Vivek Goyal   blkio: Group time...
1351
  {
f75edf2dc   Vivek Goyal   blkio: Wait for c...
1352
  	unsigned int slice_used;
dae739ebc   Vivek Goyal   blkio: Group time...
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
  
  	/*
  	 * Queue got expired before even a single request completed or
  	 * got expired immediately after first request completion.
  	 */
  	if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
  		/*
  		 * Also charge the seek time incurred to the group, otherwise
  		 * if there are mutiple queues in the group, each can dispatch
  		 * a single request on seeky media and cause lots of seek time
  		 * and group will never know it.
  		 */
  		slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
  					1);
  	} else {
  		slice_used = jiffies - cfqq->slice_start;
167400d34   Justin TerAvest   blk-cgroup: Add u...
1369
1370
  		if (slice_used > cfqq->allocated_slice) {
  			*unaccounted_time = slice_used - cfqq->allocated_slice;
f75edf2dc   Vivek Goyal   blkio: Wait for c...
1371
  			slice_used = cfqq->allocated_slice;
167400d34   Justin TerAvest   blk-cgroup: Add u...
1372
1373
1374
1375
  		}
  		if (time_after(cfqq->slice_start, cfqq->dispatch_start))
  			*unaccounted_time += cfqq->slice_start -
  					cfqq->dispatch_start;
dae739ebc   Vivek Goyal   blkio: Group time...
1376
  	}
dae739ebc   Vivek Goyal   blkio: Group time...
1377
1378
1379
1380
  	return slice_used;
  }
  
  static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
1381
  				struct cfq_queue *cfqq)
dae739ebc   Vivek Goyal   blkio: Group time...
1382
1383
  {
  	struct cfq_rb_root *st = &cfqd->grp_service_tree;
167400d34   Justin TerAvest   blk-cgroup: Add u...
1384
  	unsigned int used_sl, charge, unaccounted_sl = 0;
f26bd1f0a   Vivek Goyal   blkio: Determine ...
1385
1386
  	int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
  			- cfqg->service_tree_idle.count;
1d3650f71   Tejun Heo   cfq-iosched: impl...
1387
  	unsigned int vfr;
f26bd1f0a   Vivek Goyal   blkio: Determine ...
1388
1389
  
  	BUG_ON(nr_sync < 0);
167400d34   Justin TerAvest   blk-cgroup: Add u...
1390
  	used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
dae739ebc   Vivek Goyal   blkio: Group time...
1391

02b35081f   Vivek Goyal   cfq-iosched: Do g...
1392
1393
1394
1395
  	if (iops_mode(cfqd))
  		charge = cfqq->slice_dispatch;
  	else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
  		charge = cfqq->allocated_slice;
dae739ebc   Vivek Goyal   blkio: Group time...
1396

1d3650f71   Tejun Heo   cfq-iosched: impl...
1397
1398
1399
1400
1401
1402
1403
  	/*
  	 * Can't update vdisktime while on service tree and cfqg->vfraction
  	 * is valid only while on it.  Cache vfr, leave the service tree,
  	 * update vdisktime and go back on.  The re-addition to the tree
  	 * will also update the weights as necessary.
  	 */
  	vfr = cfqg->vfraction;
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1404
  	cfq_group_service_tree_del(st, cfqg);
1d3650f71   Tejun Heo   cfq-iosched: impl...
1405
  	cfqg->vdisktime += cfqg_scale_charge(charge, vfr);
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1406
  	cfq_group_service_tree_add(st, cfqg);
dae739ebc   Vivek Goyal   blkio: Group time...
1407
1408
1409
  
  	/* This group is being expired. Save the context */
  	if (time_after(cfqd->workload_expires, jiffies)) {
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
1410
  		cfqg->saved_wl_slice = cfqd->workload_expires
dae739ebc   Vivek Goyal   blkio: Group time...
1411
  						- jiffies;
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
1412
1413
  		cfqg->saved_wl_type = cfqd->serving_wl_type;
  		cfqg->saved_wl_class = cfqd->serving_wl_class;
dae739ebc   Vivek Goyal   blkio: Group time...
1414
  	} else
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
1415
  		cfqg->saved_wl_slice = 0;
2868ef7b3   Vivek Goyal   blkio: Some debug...
1416
1417
1418
  
  	cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
  					st->min_vdisktime);
fd16d2631   Joe Perches   block: Add __attr...
1419
1420
1421
1422
  	cfq_log_cfqq(cfqq->cfqd, cfqq,
  		     "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
  		     used_sl, cfqq->slice_dispatch, charge,
  		     iops_mode(cfqd), cfqq->nr_sectors);
155fead9b   Tejun Heo   blkcg: move blkio...
1423
1424
  	cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
  	cfqg_stats_set_start_empty_time(cfqg);
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1425
  }
f51b802c1   Tejun Heo   blkcg: use the us...
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
  /**
   * cfq_init_cfqg_base - initialize base part of a cfq_group
   * @cfqg: cfq_group to initialize
   *
   * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
   * is enabled or not.
   */
  static void cfq_init_cfqg_base(struct cfq_group *cfqg)
  {
  	struct cfq_rb_root *st;
  	int i, j;
  
  	for_each_cfqg_st(cfqg, i, j, st)
  		*st = CFQ_RB_ROOT;
  	RB_CLEAR_NODE(&cfqg->rb_node);
  
  	cfqg->ttime.last_end_request = jiffies;
  }
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1444
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
90d3839b9   Peter Zijlstra   block: Use u64_st...
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
  static void cfqg_stats_init(struct cfqg_stats *stats)
  {
  	blkg_rwstat_init(&stats->service_bytes);
  	blkg_rwstat_init(&stats->serviced);
  	blkg_rwstat_init(&stats->merged);
  	blkg_rwstat_init(&stats->service_time);
  	blkg_rwstat_init(&stats->wait_time);
  	blkg_rwstat_init(&stats->queued);
  
  	blkg_stat_init(&stats->sectors);
  	blkg_stat_init(&stats->time);
  
  #ifdef CONFIG_DEBUG_BLK_CGROUP
  	blkg_stat_init(&stats->unaccounted_time);
  	blkg_stat_init(&stats->avg_queue_size_sum);
  	blkg_stat_init(&stats->avg_queue_size_samples);
  	blkg_stat_init(&stats->dequeue);
  	blkg_stat_init(&stats->group_wait_time);
  	blkg_stat_init(&stats->idle_time);
  	blkg_stat_init(&stats->empty_time);
  #endif
  }
3c798398e   Tejun Heo   blkcg: mass renam...
1467
  static void cfq_pd_init(struct blkcg_gq *blkg)
f469a7b4d   Vivek Goyal   blk-cgroup: Allow...
1468
  {
0381411e4   Tejun Heo   blkcg: let blkcg ...
1469
  	struct cfq_group *cfqg = blkg_to_cfqg(blkg);
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1470

f51b802c1   Tejun Heo   blkcg: use the us...
1471
  	cfq_init_cfqg_base(cfqg);
3381cb8d2   Tejun Heo   blkcg: move blkio...
1472
  	cfqg->weight = blkg->blkcg->cfq_weight;
e71357e11   Tejun Heo   cfq-iosched: add ...
1473
  	cfqg->leaf_weight = blkg->blkcg->cfq_leaf_weight;
90d3839b9   Peter Zijlstra   block: Use u64_st...
1474
1475
  	cfqg_stats_init(&cfqg->stats);
  	cfqg_stats_init(&cfqg->dead_stats);
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1476
  }
0b39920b5   Tejun Heo   cfq-iosched: coll...
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
  static void cfq_pd_offline(struct blkcg_gq *blkg)
  {
  	/*
  	 * @blkg is going offline and will be ignored by
  	 * blkg_[rw]stat_recursive_sum().  Transfer stats to the parent so
  	 * that they don't get lost.  If IOs complete after this point, the
  	 * stats for them will be lost.  Oh well...
  	 */
  	cfqg_stats_xfer_dead(blkg_to_cfqg(blkg));
  }
43114018c   Tejun Heo   cfq-iosched: add ...
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
  /* offset delta from cfqg->stats to cfqg->dead_stats */
  static const int dead_stats_off_delta = offsetof(struct cfq_group, dead_stats) -
  					offsetof(struct cfq_group, stats);
  
  /* to be used by recursive prfill, sums live and dead stats recursively */
  static u64 cfqg_stat_pd_recursive_sum(struct blkg_policy_data *pd, int off)
  {
  	u64 sum = 0;
  
  	sum += blkg_stat_recursive_sum(pd, off);
  	sum += blkg_stat_recursive_sum(pd, off + dead_stats_off_delta);
  	return sum;
  }
  
  /* to be used by recursive prfill, sums live and dead rwstats recursively */
  static struct blkg_rwstat cfqg_rwstat_pd_recursive_sum(struct blkg_policy_data *pd,
  						       int off)
  {
  	struct blkg_rwstat a, b;
  
  	a = blkg_rwstat_recursive_sum(pd, off);
  	b = blkg_rwstat_recursive_sum(pd, off + dead_stats_off_delta);
  	blkg_rwstat_merge(&a, &b);
  	return a;
  }
689665af4   Tejun Heo   cfq-iosched: sepa...
1512
1513
1514
1515
1516
  static void cfq_pd_reset_stats(struct blkcg_gq *blkg)
  {
  	struct cfq_group *cfqg = blkg_to_cfqg(blkg);
  
  	cfqg_stats_reset(&cfqg->stats);
0b39920b5   Tejun Heo   cfq-iosched: coll...
1517
  	cfqg_stats_reset(&cfqg->dead_stats);
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1518
1519
1520
  }
  
  /*
3e59cf9d6   Vivek Goyal   cfq-iosched: Get ...
1521
1522
   * Search for the cfq group current task belongs to. request_queue lock must
   * be held.
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1523
   */
cd1604fab   Tejun Heo   blkcg: factor out...
1524
  static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
3c798398e   Tejun Heo   blkcg: mass renam...
1525
  						struct blkcg *blkcg)
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1526
  {
f469a7b4d   Vivek Goyal   blk-cgroup: Allow...
1527
  	struct request_queue *q = cfqd->queue;
cd1604fab   Tejun Heo   blkcg: factor out...
1528
  	struct cfq_group *cfqg = NULL;
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1529

3c798398e   Tejun Heo   blkcg: mass renam...
1530
1531
  	/* avoid lookup for the common case where there's no blkcg */
  	if (blkcg == &blkcg_root) {
cd1604fab   Tejun Heo   blkcg: factor out...
1532
1533
  		cfqg = cfqd->root_group;
  	} else {
3c798398e   Tejun Heo   blkcg: mass renam...
1534
  		struct blkcg_gq *blkg;
f469a7b4d   Vivek Goyal   blk-cgroup: Allow...
1535

3c96cb32d   Tejun Heo   blkcg: drop stuff...
1536
  		blkg = blkg_lookup_create(blkcg, q);
cd1604fab   Tejun Heo   blkcg: factor out...
1537
  		if (!IS_ERR(blkg))
0381411e4   Tejun Heo   blkcg: let blkcg ...
1538
  			cfqg = blkg_to_cfqg(blkg);
cd1604fab   Tejun Heo   blkcg: factor out...
1539
  	}
f469a7b4d   Vivek Goyal   blk-cgroup: Allow...
1540

25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1541
1542
1543
1544
1545
1546
1547
  	return cfqg;
  }
  
  static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
  {
  	/* Currently, all async queues are mapped to root group */
  	if (!cfq_cfqq_sync(cfqq))
f51b802c1   Tejun Heo   blkcg: use the us...
1548
  		cfqg = cfqq->cfqd->root_group;
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1549
1550
  
  	cfqq->cfqg = cfqg;
b1c357696   Vivek Goyal   blkio: Take care ...
1551
  	/* cfqq reference on cfqg */
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
1552
  	cfqg_get(cfqg);
b1c357696   Vivek Goyal   blkio: Take care ...
1553
  }
f95a04afa   Tejun Heo   blkcg: embed stru...
1554
1555
  static u64 cfqg_prfill_weight_device(struct seq_file *sf,
  				     struct blkg_policy_data *pd, int off)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1556
  {
f95a04afa   Tejun Heo   blkcg: embed stru...
1557
  	struct cfq_group *cfqg = pd_to_cfqg(pd);
3381cb8d2   Tejun Heo   blkcg: move blkio...
1558
1559
  
  	if (!cfqg->dev_weight)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1560
  		return 0;
f95a04afa   Tejun Heo   blkcg: embed stru...
1561
  	return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1562
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1563
  static int cfqg_print_weight_device(struct seq_file *sf, void *v)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1564
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1565
1566
1567
  	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  			  cfqg_prfill_weight_device, &blkcg_policy_cfq,
  			  0, false);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1568
1569
  	return 0;
  }
e71357e11   Tejun Heo   cfq-iosched: add ...
1570
1571
1572
1573
1574
1575
1576
1577
1578
  static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf,
  					  struct blkg_policy_data *pd, int off)
  {
  	struct cfq_group *cfqg = pd_to_cfqg(pd);
  
  	if (!cfqg->dev_leaf_weight)
  		return 0;
  	return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight);
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1579
  static int cfqg_print_leaf_weight_device(struct seq_file *sf, void *v)
e71357e11   Tejun Heo   cfq-iosched: add ...
1580
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1581
1582
1583
  	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  			  cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq,
  			  0, false);
e71357e11   Tejun Heo   cfq-iosched: add ...
1584
1585
  	return 0;
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1586
  static int cfq_print_weight(struct seq_file *sf, void *v)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1587
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1588
1589
  	seq_printf(sf, "%u
  ", css_to_blkcg(seq_css(sf))->cfq_weight);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1590
1591
  	return 0;
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1592
  static int cfq_print_leaf_weight(struct seq_file *sf, void *v)
e71357e11   Tejun Heo   cfq-iosched: add ...
1593
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1594
1595
  	seq_printf(sf, "%u
  ", css_to_blkcg(seq_css(sf))->cfq_leaf_weight);
e71357e11   Tejun Heo   cfq-iosched: add ...
1596
1597
  	return 0;
  }
451af504d   Tejun Heo   cgroup: replace c...
1598
1599
1600
  static ssize_t __cfqg_set_weight_device(struct kernfs_open_file *of,
  					char *buf, size_t nbytes, loff_t off,
  					bool is_leaf_weight)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1601
  {
451af504d   Tejun Heo   cgroup: replace c...
1602
  	struct blkcg *blkcg = css_to_blkcg(of_css(of));
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1603
  	struct blkg_conf_ctx ctx;
3381cb8d2   Tejun Heo   blkcg: move blkio...
1604
  	struct cfq_group *cfqg;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1605
  	int ret;
3c798398e   Tejun Heo   blkcg: mass renam...
1606
  	ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1607
1608
1609
1610
  	if (ret)
  		return ret;
  
  	ret = -EINVAL;
3381cb8d2   Tejun Heo   blkcg: move blkio...
1611
  	cfqg = blkg_to_cfqg(ctx.blkg);
a2b1693ba   Tejun Heo   blkcg: implement ...
1612
  	if (!ctx.v || (ctx.v >= CFQ_WEIGHT_MIN && ctx.v <= CFQ_WEIGHT_MAX)) {
e71357e11   Tejun Heo   cfq-iosched: add ...
1613
1614
1615
1616
1617
1618
1619
  		if (!is_leaf_weight) {
  			cfqg->dev_weight = ctx.v;
  			cfqg->new_weight = ctx.v ?: blkcg->cfq_weight;
  		} else {
  			cfqg->dev_leaf_weight = ctx.v;
  			cfqg->new_leaf_weight = ctx.v ?: blkcg->cfq_leaf_weight;
  		}
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1620
1621
1622
1623
  		ret = 0;
  	}
  
  	blkg_conf_finish(&ctx);
451af504d   Tejun Heo   cgroup: replace c...
1624
  	return ret ?: nbytes;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1625
  }
451af504d   Tejun Heo   cgroup: replace c...
1626
1627
  static ssize_t cfqg_set_weight_device(struct kernfs_open_file *of,
  				      char *buf, size_t nbytes, loff_t off)
e71357e11   Tejun Heo   cfq-iosched: add ...
1628
  {
451af504d   Tejun Heo   cgroup: replace c...
1629
  	return __cfqg_set_weight_device(of, buf, nbytes, off, false);
e71357e11   Tejun Heo   cfq-iosched: add ...
1630
  }
451af504d   Tejun Heo   cgroup: replace c...
1631
1632
  static ssize_t cfqg_set_leaf_weight_device(struct kernfs_open_file *of,
  					   char *buf, size_t nbytes, loff_t off)
e71357e11   Tejun Heo   cfq-iosched: add ...
1633
  {
451af504d   Tejun Heo   cgroup: replace c...
1634
  	return __cfqg_set_weight_device(of, buf, nbytes, off, true);
e71357e11   Tejun Heo   cfq-iosched: add ...
1635
  }
182446d08   Tejun Heo   cgroup: pass arou...
1636
1637
  static int __cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft,
  			    u64 val, bool is_leaf_weight)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1638
  {
182446d08   Tejun Heo   cgroup: pass arou...
1639
  	struct blkcg *blkcg = css_to_blkcg(css);
3c798398e   Tejun Heo   blkcg: mass renam...
1640
  	struct blkcg_gq *blkg;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1641

3381cb8d2   Tejun Heo   blkcg: move blkio...
1642
  	if (val < CFQ_WEIGHT_MIN || val > CFQ_WEIGHT_MAX)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1643
1644
1645
  		return -EINVAL;
  
  	spin_lock_irq(&blkcg->lock);
e71357e11   Tejun Heo   cfq-iosched: add ...
1646
1647
1648
1649
1650
  
  	if (!is_leaf_weight)
  		blkcg->cfq_weight = val;
  	else
  		blkcg->cfq_leaf_weight = val;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1651

b67bfe0d4   Sasha Levin   hlist: drop the n...
1652
  	hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
3381cb8d2   Tejun Heo   blkcg: move blkio...
1653
  		struct cfq_group *cfqg = blkg_to_cfqg(blkg);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1654

e71357e11   Tejun Heo   cfq-iosched: add ...
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
  		if (!cfqg)
  			continue;
  
  		if (!is_leaf_weight) {
  			if (!cfqg->dev_weight)
  				cfqg->new_weight = blkcg->cfq_weight;
  		} else {
  			if (!cfqg->dev_leaf_weight)
  				cfqg->new_leaf_weight = blkcg->cfq_leaf_weight;
  		}
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1665
1666
1667
1668
1669
  	}
  
  	spin_unlock_irq(&blkcg->lock);
  	return 0;
  }
182446d08   Tejun Heo   cgroup: pass arou...
1670
1671
  static int cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft,
  			  u64 val)
e71357e11   Tejun Heo   cfq-iosched: add ...
1672
  {
182446d08   Tejun Heo   cgroup: pass arou...
1673
  	return __cfq_set_weight(css, cft, val, false);
e71357e11   Tejun Heo   cfq-iosched: add ...
1674
  }
182446d08   Tejun Heo   cgroup: pass arou...
1675
1676
  static int cfq_set_leaf_weight(struct cgroup_subsys_state *css,
  			       struct cftype *cft, u64 val)
e71357e11   Tejun Heo   cfq-iosched: add ...
1677
  {
182446d08   Tejun Heo   cgroup: pass arou...
1678
  	return __cfq_set_weight(css, cft, val, true);
e71357e11   Tejun Heo   cfq-iosched: add ...
1679
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1680
  static int cfqg_print_stat(struct seq_file *sf, void *v)
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1681
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1682
1683
  	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat,
  			  &blkcg_policy_cfq, seq_cft(sf)->private, false);
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1684
1685
  	return 0;
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1686
  static int cfqg_print_rwstat(struct seq_file *sf, void *v)
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1687
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1688
1689
  	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat,
  			  &blkcg_policy_cfq, seq_cft(sf)->private, true);
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1690
1691
  	return 0;
  }
43114018c   Tejun Heo   cfq-iosched: add ...
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
  static u64 cfqg_prfill_stat_recursive(struct seq_file *sf,
  				      struct blkg_policy_data *pd, int off)
  {
  	u64 sum = cfqg_stat_pd_recursive_sum(pd, off);
  
  	return __blkg_prfill_u64(sf, pd, sum);
  }
  
  static u64 cfqg_prfill_rwstat_recursive(struct seq_file *sf,
  					struct blkg_policy_data *pd, int off)
  {
  	struct blkg_rwstat sum = cfqg_rwstat_pd_recursive_sum(pd, off);
  
  	return __blkg_prfill_rwstat(sf, pd, &sum);
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1707
  static int cfqg_print_stat_recursive(struct seq_file *sf, void *v)
43114018c   Tejun Heo   cfq-iosched: add ...
1708
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1709
1710
1711
  	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  			  cfqg_prfill_stat_recursive, &blkcg_policy_cfq,
  			  seq_cft(sf)->private, false);
43114018c   Tejun Heo   cfq-iosched: add ...
1712
1713
  	return 0;
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1714
  static int cfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
43114018c   Tejun Heo   cfq-iosched: add ...
1715
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1716
1717
1718
  	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  			  cfqg_prfill_rwstat_recursive, &blkcg_policy_cfq,
  			  seq_cft(sf)->private, true);
43114018c   Tejun Heo   cfq-iosched: add ...
1719
1720
  	return 0;
  }
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1721
  #ifdef CONFIG_DEBUG_BLK_CGROUP
f95a04afa   Tejun Heo   blkcg: embed stru...
1722
1723
  static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
  				      struct blkg_policy_data *pd, int off)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1724
  {
f95a04afa   Tejun Heo   blkcg: embed stru...
1725
  	struct cfq_group *cfqg = pd_to_cfqg(pd);
155fead9b   Tejun Heo   blkcg: move blkio...
1726
  	u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1727
1728
1729
  	u64 v = 0;
  
  	if (samples) {
155fead9b   Tejun Heo   blkcg: move blkio...
1730
  		v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
f3cff25f0   Anatol Pomozov   cfq: explicitly u...
1731
  		v = div64_u64(v, samples);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1732
  	}
f95a04afa   Tejun Heo   blkcg: embed stru...
1733
  	__blkg_prfill_u64(sf, pd, v);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1734
1735
1736
1737
  	return 0;
  }
  
  /* print avg_queue_size */
2da8ca822   Tejun Heo   cgroup: replace c...
1738
  static int cfqg_print_avg_queue_size(struct seq_file *sf, void *v)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1739
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1740
1741
1742
  	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  			  cfqg_prfill_avg_queue_size, &blkcg_policy_cfq,
  			  0, false);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1743
1744
1745
1746
1747
  	return 0;
  }
  #endif	/* CONFIG_DEBUG_BLK_CGROUP */
  
  static struct cftype cfq_blkcg_files[] = {
1d3650f71   Tejun Heo   cfq-iosched: impl...
1748
  	/* on root, weight is mapped to leaf_weight */
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1749
1750
  	{
  		.name = "weight_device",
1d3650f71   Tejun Heo   cfq-iosched: impl...
1751
  		.flags = CFTYPE_ONLY_ON_ROOT,
2da8ca822   Tejun Heo   cgroup: replace c...
1752
  		.seq_show = cfqg_print_leaf_weight_device,
451af504d   Tejun Heo   cgroup: replace c...
1753
  		.write = cfqg_set_leaf_weight_device,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1754
1755
1756
  	},
  	{
  		.name = "weight",
1d3650f71   Tejun Heo   cfq-iosched: impl...
1757
  		.flags = CFTYPE_ONLY_ON_ROOT,
2da8ca822   Tejun Heo   cgroup: replace c...
1758
  		.seq_show = cfq_print_leaf_weight,
1d3650f71   Tejun Heo   cfq-iosched: impl...
1759
  		.write_u64 = cfq_set_leaf_weight,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1760
  	},
e71357e11   Tejun Heo   cfq-iosched: add ...
1761

1d3650f71   Tejun Heo   cfq-iosched: impl...
1762
  	/* no such mapping necessary for !roots */
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1763
1764
  	{
  		.name = "weight_device",
1d3650f71   Tejun Heo   cfq-iosched: impl...
1765
  		.flags = CFTYPE_NOT_ON_ROOT,
2da8ca822   Tejun Heo   cgroup: replace c...
1766
  		.seq_show = cfqg_print_weight_device,
451af504d   Tejun Heo   cgroup: replace c...
1767
  		.write = cfqg_set_weight_device,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1768
1769
1770
  	},
  	{
  		.name = "weight",
1d3650f71   Tejun Heo   cfq-iosched: impl...
1771
  		.flags = CFTYPE_NOT_ON_ROOT,
2da8ca822   Tejun Heo   cgroup: replace c...
1772
  		.seq_show = cfq_print_weight,
3381cb8d2   Tejun Heo   blkcg: move blkio...
1773
  		.write_u64 = cfq_set_weight,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1774
  	},
e71357e11   Tejun Heo   cfq-iosched: add ...
1775

e71357e11   Tejun Heo   cfq-iosched: add ...
1776
1777
  	{
  		.name = "leaf_weight_device",
2da8ca822   Tejun Heo   cgroup: replace c...
1778
  		.seq_show = cfqg_print_leaf_weight_device,
451af504d   Tejun Heo   cgroup: replace c...
1779
  		.write = cfqg_set_leaf_weight_device,
e71357e11   Tejun Heo   cfq-iosched: add ...
1780
1781
1782
  	},
  	{
  		.name = "leaf_weight",
2da8ca822   Tejun Heo   cgroup: replace c...
1783
  		.seq_show = cfq_print_leaf_weight,
e71357e11   Tejun Heo   cfq-iosched: add ...
1784
1785
  		.write_u64 = cfq_set_leaf_weight,
  	},
43114018c   Tejun Heo   cfq-iosched: add ...
1786
  	/* statistics, covers only the tasks in the cfqg */
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1787
1788
  	{
  		.name = "time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1789
  		.private = offsetof(struct cfq_group, stats.time),
2da8ca822   Tejun Heo   cgroup: replace c...
1790
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1791
1792
1793
  	},
  	{
  		.name = "sectors",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1794
  		.private = offsetof(struct cfq_group, stats.sectors),
2da8ca822   Tejun Heo   cgroup: replace c...
1795
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1796
1797
1798
  	},
  	{
  		.name = "io_service_bytes",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1799
  		.private = offsetof(struct cfq_group, stats.service_bytes),
2da8ca822   Tejun Heo   cgroup: replace c...
1800
  		.seq_show = cfqg_print_rwstat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1801
1802
1803
  	},
  	{
  		.name = "io_serviced",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1804
  		.private = offsetof(struct cfq_group, stats.serviced),
2da8ca822   Tejun Heo   cgroup: replace c...
1805
  		.seq_show = cfqg_print_rwstat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1806
1807
1808
  	},
  	{
  		.name = "io_service_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1809
  		.private = offsetof(struct cfq_group, stats.service_time),
2da8ca822   Tejun Heo   cgroup: replace c...
1810
  		.seq_show = cfqg_print_rwstat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1811
1812
1813
  	},
  	{
  		.name = "io_wait_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1814
  		.private = offsetof(struct cfq_group, stats.wait_time),
2da8ca822   Tejun Heo   cgroup: replace c...
1815
  		.seq_show = cfqg_print_rwstat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1816
1817
1818
  	},
  	{
  		.name = "io_merged",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1819
  		.private = offsetof(struct cfq_group, stats.merged),
2da8ca822   Tejun Heo   cgroup: replace c...
1820
  		.seq_show = cfqg_print_rwstat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1821
1822
1823
  	},
  	{
  		.name = "io_queued",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1824
  		.private = offsetof(struct cfq_group, stats.queued),
2da8ca822   Tejun Heo   cgroup: replace c...
1825
  		.seq_show = cfqg_print_rwstat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1826
  	},
43114018c   Tejun Heo   cfq-iosched: add ...
1827
1828
1829
1830
1831
  
  	/* the same statictics which cover the cfqg and its descendants */
  	{
  		.name = "time_recursive",
  		.private = offsetof(struct cfq_group, stats.time),
2da8ca822   Tejun Heo   cgroup: replace c...
1832
  		.seq_show = cfqg_print_stat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1833
1834
1835
1836
  	},
  	{
  		.name = "sectors_recursive",
  		.private = offsetof(struct cfq_group, stats.sectors),
2da8ca822   Tejun Heo   cgroup: replace c...
1837
  		.seq_show = cfqg_print_stat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1838
1839
1840
1841
  	},
  	{
  		.name = "io_service_bytes_recursive",
  		.private = offsetof(struct cfq_group, stats.service_bytes),
2da8ca822   Tejun Heo   cgroup: replace c...
1842
  		.seq_show = cfqg_print_rwstat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1843
1844
1845
1846
  	},
  	{
  		.name = "io_serviced_recursive",
  		.private = offsetof(struct cfq_group, stats.serviced),
2da8ca822   Tejun Heo   cgroup: replace c...
1847
  		.seq_show = cfqg_print_rwstat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1848
1849
1850
1851
  	},
  	{
  		.name = "io_service_time_recursive",
  		.private = offsetof(struct cfq_group, stats.service_time),
2da8ca822   Tejun Heo   cgroup: replace c...
1852
  		.seq_show = cfqg_print_rwstat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1853
1854
1855
1856
  	},
  	{
  		.name = "io_wait_time_recursive",
  		.private = offsetof(struct cfq_group, stats.wait_time),
2da8ca822   Tejun Heo   cgroup: replace c...
1857
  		.seq_show = cfqg_print_rwstat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1858
1859
1860
1861
  	},
  	{
  		.name = "io_merged_recursive",
  		.private = offsetof(struct cfq_group, stats.merged),
2da8ca822   Tejun Heo   cgroup: replace c...
1862
  		.seq_show = cfqg_print_rwstat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1863
1864
1865
1866
  	},
  	{
  		.name = "io_queued_recursive",
  		.private = offsetof(struct cfq_group, stats.queued),
2da8ca822   Tejun Heo   cgroup: replace c...
1867
  		.seq_show = cfqg_print_rwstat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1868
  	},
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1869
1870
1871
  #ifdef CONFIG_DEBUG_BLK_CGROUP
  	{
  		.name = "avg_queue_size",
2da8ca822   Tejun Heo   cgroup: replace c...
1872
  		.seq_show = cfqg_print_avg_queue_size,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1873
1874
1875
  	},
  	{
  		.name = "group_wait_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1876
  		.private = offsetof(struct cfq_group, stats.group_wait_time),
2da8ca822   Tejun Heo   cgroup: replace c...
1877
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1878
1879
1880
  	},
  	{
  		.name = "idle_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1881
  		.private = offsetof(struct cfq_group, stats.idle_time),
2da8ca822   Tejun Heo   cgroup: replace c...
1882
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1883
1884
1885
  	},
  	{
  		.name = "empty_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1886
  		.private = offsetof(struct cfq_group, stats.empty_time),
2da8ca822   Tejun Heo   cgroup: replace c...
1887
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1888
1889
1890
  	},
  	{
  		.name = "dequeue",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1891
  		.private = offsetof(struct cfq_group, stats.dequeue),
2da8ca822   Tejun Heo   cgroup: replace c...
1892
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1893
1894
1895
  	},
  	{
  		.name = "unaccounted_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1896
  		.private = offsetof(struct cfq_group, stats.unaccounted_time),
2da8ca822   Tejun Heo   cgroup: replace c...
1897
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1898
1899
1900
1901
  	},
  #endif	/* CONFIG_DEBUG_BLK_CGROUP */
  	{ }	/* terminate */
  };
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1902
  #else /* GROUP_IOSCHED */
cd1604fab   Tejun Heo   blkcg: factor out...
1903
  static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
3c798398e   Tejun Heo   blkcg: mass renam...
1904
  						struct blkcg *blkcg)
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1905
  {
f51b802c1   Tejun Heo   blkcg: use the us...
1906
  	return cfqd->root_group;
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1907
  }
7f1dc8a2d   Vivek Goyal   blkio: Fix blkio ...
1908

25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1909
1910
1911
1912
1913
1914
  static inline void
  cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
  	cfqq->cfqg = cfqg;
  }
  
  #endif /* GROUP_IOSCHED */
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
1915
  /*
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
1916
   * The cfqd->service_trees holds all pending cfq_queue's that have
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
1917
1918
1919
   * requests waiting to be processed. It is sorted in the order that
   * we will service the queues.
   */
a36e71f99   Jens Axboe   cfq-iosched: add ...
1920
  static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
a6151c3a5   Jens Axboe   cfq-iosched: appl...
1921
  				 bool add_front)
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
1922
  {
0871714e0   Jens Axboe   cfq-iosched: rela...
1923
1924
  	struct rb_node **p, *parent;
  	struct cfq_queue *__cfqq;
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
1925
  	unsigned long rb_key;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
1926
  	struct cfq_rb_root *st;
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
1927
  	int left;
dae739ebc   Vivek Goyal   blkio: Group time...
1928
  	int new_cfqq = 1;
ae30c2865   Vivek Goyal   blkio: Implement ...
1929

34b98d03b   Vivek Goyal   cfq-iosched: Rena...
1930
  	st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
0871714e0   Jens Axboe   cfq-iosched: rela...
1931
1932
  	if (cfq_class_idle(cfqq)) {
  		rb_key = CFQ_IDLE_DELAY;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
1933
  		parent = rb_last(&st->rb);
0871714e0   Jens Axboe   cfq-iosched: rela...
1934
1935
1936
1937
1938
1939
  		if (parent && parent != &cfqq->rb_node) {
  			__cfqq = rb_entry(parent, struct cfq_queue, rb_node);
  			rb_key += __cfqq->rb_key;
  		} else
  			rb_key += jiffies;
  	} else if (!add_front) {
b9c8946b1   Jens Axboe   cfq-iosched: fix ...
1940
1941
1942
1943
1944
1945
  		/*
  		 * Get our rb key offset. Subtract any residual slice
  		 * value carried from last service. A negative resid
  		 * count indicates slice overrun, and this should position
  		 * the next service time further away in the tree.
  		 */
edd75ffd9   Jens Axboe   cfq-iosched: get ...
1946
  		rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
b9c8946b1   Jens Axboe   cfq-iosched: fix ...
1947
  		rb_key -= cfqq->slice_resid;
edd75ffd9   Jens Axboe   cfq-iosched: get ...
1948
  		cfqq->slice_resid = 0;
48e025e63   Corrado Zoccolo   cfq-iosched: fix ...
1949
1950
  	} else {
  		rb_key = -HZ;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
1951
  		__cfqq = cfq_rb_first(st);
48e025e63   Corrado Zoccolo   cfq-iosched: fix ...
1952
1953
  		rb_key += __cfqq ? __cfqq->rb_key : jiffies;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1954

d9e7620e6   Jens Axboe   cfq-iosched: rewo...
1955
  	if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
dae739ebc   Vivek Goyal   blkio: Group time...
1956
  		new_cfqq = 0;
99f9628ab   Jens Axboe   [PATCH] cfq-iosch...
1957
  		/*
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
1958
  		 * same position, nothing more to do
99f9628ab   Jens Axboe   [PATCH] cfq-iosch...
1959
  		 */
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
1960
  		if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
1961
  			return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1962

aa6f6a3de   Corrado Zoccolo   cfq-iosched: prep...
1963
1964
  		cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
  		cfqq->service_tree = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1965
  	}
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
1966

498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
1967
  	left = 1;
0871714e0   Jens Axboe   cfq-iosched: rela...
1968
  	parent = NULL;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
1969
1970
  	cfqq->service_tree = st;
  	p = &st->rb.rb_node;
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
1971
1972
1973
  	while (*p) {
  		parent = *p;
  		__cfqq = rb_entry(parent, struct cfq_queue, rb_node);
0c534e0a4   Jens Axboe   cfq-iosched: sort...
1974
  		/*
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
1975
  		 * sort by key, that represents service time.
0c534e0a4   Jens Axboe   cfq-iosched: sort...
1976
  		 */
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
1977
  		if (time_before(rb_key, __cfqq->rb_key))
1f23f1215   Vivek Goyal   cfq-iosched: Get ...
1978
  			p = &parent->rb_left;
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
1979
  		else {
1f23f1215   Vivek Goyal   cfq-iosched: Get ...
1980
  			p = &parent->rb_right;
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
1981
  			left = 0;
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
1982
  		}
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
1983
  	}
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
1984
  	if (left)
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
1985
  		st->left = &cfqq->rb_node;
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
1986

d9e7620e6   Jens Axboe   cfq-iosched: rewo...
1987
1988
  	cfqq->rb_key = rb_key;
  	rb_link_node(&cfqq->rb_node, parent, p);
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
1989
1990
  	rb_insert_color(&cfqq->rb_node, &st->rb);
  	st->count++;
20359f27e   Namhyung Kim   cfq-iosched: remo...
1991
  	if (add_front || !new_cfqq)
dae739ebc   Vivek Goyal   blkio: Group time...
1992
  		return;
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1993
  	cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1994
  }
a36e71f99   Jens Axboe   cfq-iosched: add ...
1995
  static struct cfq_queue *
f2d1f0ae7   Jens Axboe   cfq-iosched: cach...
1996
1997
1998
  cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
  		     sector_t sector, struct rb_node **ret_parent,
  		     struct rb_node ***rb_link)
a36e71f99   Jens Axboe   cfq-iosched: add ...
1999
  {
a36e71f99   Jens Axboe   cfq-iosched: add ...
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
  	struct rb_node **p, *parent;
  	struct cfq_queue *cfqq = NULL;
  
  	parent = NULL;
  	p = &root->rb_node;
  	while (*p) {
  		struct rb_node **n;
  
  		parent = *p;
  		cfqq = rb_entry(parent, struct cfq_queue, p_node);
  
  		/*
  		 * Sort strictly based on sector.  Smallest to the left,
  		 * largest to the right.
  		 */
2e46e8b27   Tejun Heo   block: drop reque...
2015
  		if (sector > blk_rq_pos(cfqq->next_rq))
a36e71f99   Jens Axboe   cfq-iosched: add ...
2016
  			n = &(*p)->rb_right;
2e46e8b27   Tejun Heo   block: drop reque...
2017
  		else if (sector < blk_rq_pos(cfqq->next_rq))
a36e71f99   Jens Axboe   cfq-iosched: add ...
2018
2019
2020
2021
  			n = &(*p)->rb_left;
  		else
  			break;
  		p = n;
3ac6c9f8a   Jens Axboe   cfq-iosched: fix ...
2022
  		cfqq = NULL;
a36e71f99   Jens Axboe   cfq-iosched: add ...
2023
2024
2025
2026
2027
  	}
  
  	*ret_parent = parent;
  	if (rb_link)
  		*rb_link = p;
3ac6c9f8a   Jens Axboe   cfq-iosched: fix ...
2028
  	return cfqq;
a36e71f99   Jens Axboe   cfq-iosched: add ...
2029
2030
2031
2032
  }
  
  static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
a36e71f99   Jens Axboe   cfq-iosched: add ...
2033
2034
  	struct rb_node **p, *parent;
  	struct cfq_queue *__cfqq;
f2d1f0ae7   Jens Axboe   cfq-iosched: cach...
2035
2036
2037
2038
  	if (cfqq->p_root) {
  		rb_erase(&cfqq->p_node, cfqq->p_root);
  		cfqq->p_root = NULL;
  	}
a36e71f99   Jens Axboe   cfq-iosched: add ...
2039
2040
2041
2042
2043
  
  	if (cfq_class_idle(cfqq))
  		return;
  	if (!cfqq->next_rq)
  		return;
f2d1f0ae7   Jens Axboe   cfq-iosched: cach...
2044
  	cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
2e46e8b27   Tejun Heo   block: drop reque...
2045
2046
  	__cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
  				      blk_rq_pos(cfqq->next_rq), &parent, &p);
3ac6c9f8a   Jens Axboe   cfq-iosched: fix ...
2047
2048
  	if (!__cfqq) {
  		rb_link_node(&cfqq->p_node, parent, p);
f2d1f0ae7   Jens Axboe   cfq-iosched: cach...
2049
2050
2051
  		rb_insert_color(&cfqq->p_node, cfqq->p_root);
  	} else
  		cfqq->p_root = NULL;
a36e71f99   Jens Axboe   cfq-iosched: add ...
2052
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2053
2054
2055
  /*
   * Update cfqq's position in the service tree.
   */
edd75ffd9   Jens Axboe   cfq-iosched: get ...
2056
  static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
6d048f531   Jens Axboe   cfq-iosched: deve...
2057
  {
6d048f531   Jens Axboe   cfq-iosched: deve...
2058
2059
2060
  	/*
  	 * Resorting requires the cfqq to be on the RR list already.
  	 */
a36e71f99   Jens Axboe   cfq-iosched: add ...
2061
  	if (cfq_cfqq_on_rr(cfqq)) {
edd75ffd9   Jens Axboe   cfq-iosched: get ...
2062
  		cfq_service_tree_add(cfqd, cfqq, 0);
a36e71f99   Jens Axboe   cfq-iosched: add ...
2063
2064
  		cfq_prio_tree_add(cfqd, cfqq);
  	}
6d048f531   Jens Axboe   cfq-iosched: deve...
2065
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2066
2067
  /*
   * add to busy list of queues for service, trying to be fair in ordering
22e2c507c   Jens Axboe   [PATCH] Update cf...
2068
   * the pending list according to last request service
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2069
   */
febffd618   Jens Axboe   cfq-iosched: kill...
2070
  static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2071
  {
7b679138b   Jens Axboe   cfq-iosched: add ...
2072
  	cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2073
2074
  	BUG_ON(cfq_cfqq_on_rr(cfqq));
  	cfq_mark_cfqq_on_rr(cfqq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2075
  	cfqd->busy_queues++;
ef8a41df8   Shaohua Li   cfq-iosched: give...
2076
2077
  	if (cfq_cfqq_sync(cfqq))
  		cfqd->busy_sync_queues++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2078

edd75ffd9   Jens Axboe   cfq-iosched: get ...
2079
  	cfq_resort_rr_list(cfqd, cfqq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2080
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2081
2082
2083
2084
  /*
   * Called when the cfqq no longer has requests pending, remove it from
   * the service tree.
   */
febffd618   Jens Axboe   cfq-iosched: kill...
2085
  static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2086
  {
7b679138b   Jens Axboe   cfq-iosched: add ...
2087
  	cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2088
2089
  	BUG_ON(!cfq_cfqq_on_rr(cfqq));
  	cfq_clear_cfqq_on_rr(cfqq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2090

aa6f6a3de   Corrado Zoccolo   cfq-iosched: prep...
2091
2092
2093
2094
  	if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
  		cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
  		cfqq->service_tree = NULL;
  	}
f2d1f0ae7   Jens Axboe   cfq-iosched: cach...
2095
2096
2097
2098
  	if (cfqq->p_root) {
  		rb_erase(&cfqq->p_node, cfqq->p_root);
  		cfqq->p_root = NULL;
  	}
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2099

8184f93ec   Justin TerAvest   cfq-iosched: Don'...
2100
  	cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2101
2102
  	BUG_ON(!cfqd->busy_queues);
  	cfqd->busy_queues--;
ef8a41df8   Shaohua Li   cfq-iosched: give...
2103
2104
  	if (cfq_cfqq_sync(cfqq))
  		cfqd->busy_sync_queues--;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2105
2106
2107
2108
2109
  }
  
  /*
   * rb tree support functions
   */
febffd618   Jens Axboe   cfq-iosched: kill...
2110
  static void cfq_del_rq_rb(struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2111
  {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2112
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2113
  	const int sync = rq_is_sync(rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2114

b4878f245   Jens Axboe   [PATCH] 02/05: up...
2115
2116
  	BUG_ON(!cfqq->queued[sync]);
  	cfqq->queued[sync]--;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2117

5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2118
  	elv_rb_del(&cfqq->sort_list, rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2119

f04a64246   Vivek Goyal   blkio: Keep queue...
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
  	if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
  		/*
  		 * Queue will be deleted from service tree when we actually
  		 * expire it later. Right now just remove it from prio tree
  		 * as it is empty.
  		 */
  		if (cfqq->p_root) {
  			rb_erase(&cfqq->p_node, cfqq->p_root);
  			cfqq->p_root = NULL;
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2131
  }
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2132
  static void cfq_add_rq_rb(struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2133
  {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2134
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2135
  	struct cfq_data *cfqd = cfqq->cfqd;
796d5116c   Jeff Moyer   iosched: prevent ...
2136
  	struct request *prev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2137

5380a101d   Jens Axboe   [PATCH] cfq-iosch...
2138
  	cfqq->queued[rq_is_sync(rq)]++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2139

796d5116c   Jeff Moyer   iosched: prevent ...
2140
  	elv_rb_add(&cfqq->sort_list, rq);
5fccbf61b   Jens Axboe   [PATCH] CFQ: requ...
2141
2142
2143
  
  	if (!cfq_cfqq_on_rr(cfqq))
  		cfq_add_cfqq_rr(cfqd, cfqq);
5044eed48   Jens Axboe   cfq-iosched: fix ...
2144
2145
2146
2147
  
  	/*
  	 * check if this request is a better next-serve candidate
  	 */
a36e71f99   Jens Axboe   cfq-iosched: add ...
2148
  	prev = cfqq->next_rq;
cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
2149
  	cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
a36e71f99   Jens Axboe   cfq-iosched: add ...
2150
2151
2152
2153
2154
2155
  
  	/*
  	 * adjust priority tree position, if ->next_rq changes
  	 */
  	if (prev != cfqq->next_rq)
  		cfq_prio_tree_add(cfqd, cfqq);
5044eed48   Jens Axboe   cfq-iosched: fix ...
2156
  	BUG_ON(!cfqq->next_rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2157
  }
febffd618   Jens Axboe   cfq-iosched: kill...
2158
  static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2159
  {
5380a101d   Jens Axboe   [PATCH] cfq-iosch...
2160
2161
  	elv_rb_del(&cfqq->sort_list, rq);
  	cfqq->queued[rq_is_sync(rq)]--;
155fead9b   Tejun Heo   blkcg: move blkio...
2162
  	cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2163
  	cfq_add_rq_rb(rq);
155fead9b   Tejun Heo   blkcg: move blkio...
2164
2165
  	cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
  				 rq->cmd_flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2166
  }
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2167
2168
  static struct request *
  cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2169
  {
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2170
  	struct task_struct *tsk = current;
c58698073   Tejun Heo   block, cfq: reorg...
2171
  	struct cfq_io_cq *cic;
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2172
  	struct cfq_queue *cfqq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2173

4ac845a2e   Jens Axboe   block: cfq: make ...
2174
  	cic = cfq_cic_lookup(cfqd, tsk->io_context);
91fac317a   Vasily Tarasov   cfq-iosched: get ...
2175
2176
2177
2178
  	if (!cic)
  		return NULL;
  
  	cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
f73a1c7d1   Kent Overstreet   block: Add bio_en...
2179
2180
  	if (cfqq)
  		return elv_rb_find(&cfqq->sort_list, bio_end_sector(bio));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2181

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2182
2183
  	return NULL;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
2184
  static void cfq_activate_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2185
  {
22e2c507c   Jens Axboe   [PATCH] Update cf...
2186
  	struct cfq_data *cfqd = q->elevator->elevator_data;
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2187

53c583d22   Corrado Zoccolo   cfq-iosched: requ...
2188
  	cfqd->rq_in_driver++;
7b679138b   Jens Axboe   cfq-iosched: add ...
2189
  	cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
2190
  						cfqd->rq_in_driver);
25776e359   Jens Axboe   [PATCH] cfq-iosch...
2191

5b93629b4   Tejun Heo   block: implement ...
2192
  	cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2193
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
2194
  static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2195
  {
b4878f245   Jens Axboe   [PATCH] 02/05: up...
2196
  	struct cfq_data *cfqd = q->elevator->elevator_data;
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
2197
2198
  	WARN_ON(!cfqd->rq_in_driver);
  	cfqd->rq_in_driver--;
7b679138b   Jens Axboe   cfq-iosched: add ...
2199
  	cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
2200
  						cfqd->rq_in_driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2201
  }
b4878f245   Jens Axboe   [PATCH] 02/05: up...
2202
  static void cfq_remove_request(struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2203
  {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2204
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
2205

5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2206
2207
  	if (cfqq->next_rq == rq)
  		cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2208

b4878f245   Jens Axboe   [PATCH] 02/05: up...
2209
  	list_del_init(&rq->queuelist);
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2210
  	cfq_del_rq_rb(rq);
374f84ac3   Jens Axboe   [PATCH] cfq-iosch...
2211

45333d5a3   Aaron Carroll   cfq-iosched: fix ...
2212
  	cfqq->cfqd->rq_queued--;
155fead9b   Tejun Heo   blkcg: move blkio...
2213
  	cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
65299a3b7   Christoph Hellwig   block: separate p...
2214
2215
2216
  	if (rq->cmd_flags & REQ_PRIO) {
  		WARN_ON(!cfqq->prio_pending);
  		cfqq->prio_pending--;
b53d1ed73   Jens Axboe   Revert "cfq: Remo...
2217
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2218
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
2219
2220
  static int cfq_merge(struct request_queue *q, struct request **req,
  		     struct bio *bio)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2221
2222
2223
  {
  	struct cfq_data *cfqd = q->elevator->elevator_data;
  	struct request *__rq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2224

206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2225
  	__rq = cfq_find_rq_fmerge(cfqd, bio);
22e2c507c   Jens Axboe   [PATCH] Update cf...
2226
  	if (__rq && elv_rq_merge_ok(__rq, bio)) {
9817064b6   Jens Axboe   [PATCH] elevator:...
2227
2228
  		*req = __rq;
  		return ELEVATOR_FRONT_MERGE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2229
2230
2231
  	}
  
  	return ELEVATOR_NO_MERGE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2232
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
2233
  static void cfq_merged_request(struct request_queue *q, struct request *req,
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
2234
  			       int type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2235
  {
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
2236
  	if (type == ELEVATOR_FRONT_MERGE) {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2237
  		struct cfq_queue *cfqq = RQ_CFQQ(req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2238

5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2239
  		cfq_reposition_rq_rb(cfqq, req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2240
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2241
  }
812d40264   Divyesh Shah   blkio: Add io_mer...
2242
2243
2244
  static void cfq_bio_merged(struct request_queue *q, struct request *req,
  				struct bio *bio)
  {
155fead9b   Tejun Heo   blkcg: move blkio...
2245
  	cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_rw);
812d40264   Divyesh Shah   blkio: Add io_mer...
2246
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2247
  static void
165125e1e   Jens Axboe   [BLOCK] Get rid o...
2248
  cfq_merged_requests(struct request_queue *q, struct request *rq,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2249
2250
  		    struct request *next)
  {
cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
2251
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
4a0b75c7d   Shaohua Li   block, cfq: fix e...
2252
  	struct cfq_data *cfqd = q->elevator->elevator_data;
22e2c507c   Jens Axboe   [PATCH] Update cf...
2253
2254
2255
2256
  	/*
  	 * reposition in fifo if next is older than rq
  	 */
  	if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
8b4922d31   Jan Kara   block: Stop abusi...
2257
  	    time_before(next->fifo_time, rq->fifo_time) &&
3d106fba2   Shaohua Li   block CFQ: avoid ...
2258
  	    cfqq == RQ_CFQQ(next)) {
22e2c507c   Jens Axboe   [PATCH] Update cf...
2259
  		list_move(&rq->queuelist, &next->queuelist);
8b4922d31   Jan Kara   block: Stop abusi...
2260
  		rq->fifo_time = next->fifo_time;
30996f40b   Jens Axboe   cfq-iosched: fix ...
2261
  	}
22e2c507c   Jens Axboe   [PATCH] Update cf...
2262

cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
2263
2264
  	if (cfqq->next_rq == next)
  		cfqq->next_rq = rq;
b4878f245   Jens Axboe   [PATCH] 02/05: up...
2265
  	cfq_remove_request(next);
155fead9b   Tejun Heo   blkcg: move blkio...
2266
  	cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags);
4a0b75c7d   Shaohua Li   block, cfq: fix e...
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
  
  	cfqq = RQ_CFQQ(next);
  	/*
  	 * all requests of this queue are merged to other queues, delete it
  	 * from the service tree. If it's the active_queue,
  	 * cfq_dispatch_requests() will choose to expire it or do idle
  	 */
  	if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
  	    cfqq != cfqd->active_queue)
  		cfq_del_cfqq_rr(cfqd, cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
2277
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
2278
  static int cfq_allow_merge(struct request_queue *q, struct request *rq,
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2279
2280
2281
  			   struct bio *bio)
  {
  	struct cfq_data *cfqd = q->elevator->elevator_data;
c58698073   Tejun Heo   block, cfq: reorg...
2282
  	struct cfq_io_cq *cic;
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2283
  	struct cfq_queue *cfqq;
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2284
2285
  
  	/*
ec8acb690   Jens Axboe   [PATCH] cfq-iosch...
2286
  	 * Disallow merge of a sync bio into an async request.
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2287
  	 */
91fac317a   Vasily Tarasov   cfq-iosched: get ...
2288
  	if (cfq_bio_sync(bio) && !rq_is_sync(rq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
2289
  		return false;
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2290
2291
  
  	/*
f1a4f4d35   Tejun Heo   block, cfq: fix c...
2292
  	 * Lookup the cfqq that this bio will be queued with and allow
07c2bd373   Tejun Heo   block: don't call...
2293
  	 * merge only if rq is queued there.
f1a4f4d35   Tejun Heo   block, cfq: fix c...
2294
  	 */
07c2bd373   Tejun Heo   block: don't call...
2295
2296
2297
  	cic = cfq_cic_lookup(cfqd, current->io_context);
  	if (!cic)
  		return false;
719d34027   Jens Axboe   [PATCH] cfq-iosch...
2298

91fac317a   Vasily Tarasov   cfq-iosched: get ...
2299
  	cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
a6151c3a5   Jens Axboe   cfq-iosched: appl...
2300
  	return cfqq == RQ_CFQQ(rq);
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2301
  }
812df48d1   Divyesh Shah   blkio: Add more d...
2302
2303
2304
  static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
  	del_timer(&cfqd->idle_slice_timer);
155fead9b   Tejun Heo   blkcg: move blkio...
2305
  	cfqg_stats_update_idle_time(cfqq->cfqg);
812df48d1   Divyesh Shah   blkio: Add more d...
2306
  }
febffd618   Jens Axboe   cfq-iosched: kill...
2307
2308
  static void __cfq_set_active_queue(struct cfq_data *cfqd,
  				   struct cfq_queue *cfqq)
22e2c507c   Jens Axboe   [PATCH] Update cf...
2309
2310
  {
  	if (cfqq) {
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
2311
  		cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2312
  				cfqd->serving_wl_class, cfqd->serving_wl_type);
155fead9b   Tejun Heo   blkcg: move blkio...
2313
  		cfqg_stats_update_avg_queue_size(cfqq->cfqg);
62a37f6ba   Justin TerAvest   cfq-iosched: Don'...
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
  		cfqq->slice_start = 0;
  		cfqq->dispatch_start = jiffies;
  		cfqq->allocated_slice = 0;
  		cfqq->slice_end = 0;
  		cfqq->slice_dispatch = 0;
  		cfqq->nr_sectors = 0;
  
  		cfq_clear_cfqq_wait_request(cfqq);
  		cfq_clear_cfqq_must_dispatch(cfqq);
  		cfq_clear_cfqq_must_alloc_slice(cfqq);
  		cfq_clear_cfqq_fifo_expire(cfqq);
  		cfq_mark_cfqq_slice_new(cfqq);
  
  		cfq_del_timer(cfqd, cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
2328
2329
2330
2331
2332
2333
  	}
  
  	cfqd->active_queue = cfqq;
  }
  
  /*
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2334
2335
2336
2337
   * current cfqq expired its slice (or was too idle), select new one
   */
  static void
  __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
2338
  		    bool timed_out)
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2339
  {
7b679138b   Jens Axboe   cfq-iosched: add ...
2340
  	cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2341
  	if (cfq_cfqq_wait_request(cfqq))
812df48d1   Divyesh Shah   blkio: Add more d...
2342
  		cfq_del_timer(cfqd, cfqq);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2343

7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2344
  	cfq_clear_cfqq_wait_request(cfqq);
f75edf2dc   Vivek Goyal   blkio: Wait for c...
2345
  	cfq_clear_cfqq_wait_busy(cfqq);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2346
2347
  
  	/*
ae54abed6   Shaohua Li   cfq-iosched: spli...
2348
2349
2350
2351
2352
2353
2354
2355
2356
  	 * If this cfqq is shared between multiple processes, check to
  	 * make sure that those processes are still issuing I/Os within
  	 * the mean seek distance.  If not, it may be time to break the
  	 * queues apart again.
  	 */
  	if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
  		cfq_mark_cfqq_split_coop(cfqq);
  
  	/*
6084cdda0   Jens Axboe   cfq-iosched: don'...
2357
  	 * store what was left of this slice, if the queue idled/timed out
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2358
  	 */
c553f8e33   Shaohua Li   block cfq: compen...
2359
2360
  	if (timed_out) {
  		if (cfq_cfqq_slice_new(cfqq))
ba5bd520f   Vivek Goyal   cfq: rename a fun...
2361
  			cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
c553f8e33   Shaohua Li   block cfq: compen...
2362
2363
  		else
  			cfqq->slice_resid = cfqq->slice_end - jiffies;
7b679138b   Jens Axboe   cfq-iosched: add ...
2364
2365
  		cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
  	}
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2366

e5ff082e8   Vivek Goyal   blkio: Fix anothe...
2367
  	cfq_group_served(cfqd, cfqq->cfqg, cfqq);
dae739ebc   Vivek Goyal   blkio: Group time...
2368

f04a64246   Vivek Goyal   blkio: Keep queue...
2369
2370
  	if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
  		cfq_del_cfqq_rr(cfqd, cfqq);
edd75ffd9   Jens Axboe   cfq-iosched: get ...
2371
  	cfq_resort_rr_list(cfqd, cfqq);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2372
2373
2374
2375
2376
  
  	if (cfqq == cfqd->active_queue)
  		cfqd->active_queue = NULL;
  
  	if (cfqd->active_cic) {
11a3122f6   Tejun Heo   block: strip out ...
2377
  		put_io_context(cfqd->active_cic->icq.ioc);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2378
2379
  		cfqd->active_cic = NULL;
  	}
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2380
  }
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
2381
  static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2382
2383
2384
2385
  {
  	struct cfq_queue *cfqq = cfqd->active_queue;
  
  	if (cfqq)
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
2386
  		__cfq_slice_expired(cfqd, cfqq, timed_out);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2387
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2388
2389
2390
2391
  /*
   * Get next queue for service. Unless we have a queue preemption,
   * we'll simply select the first cfqq in the service tree.
   */
6d048f531   Jens Axboe   cfq-iosched: deve...
2392
  static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
22e2c507c   Jens Axboe   [PATCH] Update cf...
2393
  {
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2394
2395
  	struct cfq_rb_root *st = st_for(cfqd->serving_group,
  			cfqd->serving_wl_class, cfqd->serving_wl_type);
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2396

f04a64246   Vivek Goyal   blkio: Keep queue...
2397
2398
  	if (!cfqd->rq_queued)
  		return NULL;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
2399
  	/* There is nothing to dispatch */
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2400
  	if (!st)
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
2401
  		return NULL;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2402
  	if (RB_EMPTY_ROOT(&st->rb))
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
2403
  		return NULL;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2404
  	return cfq_rb_first(st);
6d048f531   Jens Axboe   cfq-iosched: deve...
2405
  }
f04a64246   Vivek Goyal   blkio: Keep queue...
2406
2407
  static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
  {
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
2408
  	struct cfq_group *cfqg;
f04a64246   Vivek Goyal   blkio: Keep queue...
2409
2410
2411
2412
2413
2414
  	struct cfq_queue *cfqq;
  	int i, j;
  	struct cfq_rb_root *st;
  
  	if (!cfqd->rq_queued)
  		return NULL;
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
2415
2416
2417
  	cfqg = cfq_get_next_cfqg(cfqd);
  	if (!cfqg)
  		return NULL;
f04a64246   Vivek Goyal   blkio: Keep queue...
2418
2419
2420
2421
2422
  	for_each_cfqg_st(cfqg, i, j, st)
  		if ((cfqq = cfq_rb_first(st)) != NULL)
  			return cfqq;
  	return NULL;
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2423
2424
2425
  /*
   * Get and set a new active queue for service.
   */
a36e71f99   Jens Axboe   cfq-iosched: add ...
2426
2427
  static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
  					      struct cfq_queue *cfqq)
6d048f531   Jens Axboe   cfq-iosched: deve...
2428
  {
e00ef7997   Jens Axboe   cfq-iosched: get ...
2429
  	if (!cfqq)
a36e71f99   Jens Axboe   cfq-iosched: add ...
2430
  		cfqq = cfq_get_next_queue(cfqd);
6d048f531   Jens Axboe   cfq-iosched: deve...
2431

22e2c507c   Jens Axboe   [PATCH] Update cf...
2432
  	__cfq_set_active_queue(cfqd, cfqq);
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2433
  	return cfqq;
22e2c507c   Jens Axboe   [PATCH] Update cf...
2434
  }
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2435
2436
2437
  static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
  					  struct request *rq)
  {
83096ebf1   Tejun Heo   block: convert to...
2438
2439
  	if (blk_rq_pos(rq) >= cfqd->last_position)
  		return blk_rq_pos(rq) - cfqd->last_position;
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2440
  	else
83096ebf1   Tejun Heo   block: convert to...
2441
  		return cfqd->last_position - blk_rq_pos(rq);
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2442
  }
b2c18e1e0   Jeff Moyer   cfq: calculate th...
2443
  static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
e9ce335df   Shaohua Li   cfq-iosched: fix ...
2444
  			       struct request *rq)
6d048f531   Jens Axboe   cfq-iosched: deve...
2445
  {
e9ce335df   Shaohua Li   cfq-iosched: fix ...
2446
  	return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
6d048f531   Jens Axboe   cfq-iosched: deve...
2447
  }
a36e71f99   Jens Axboe   cfq-iosched: add ...
2448
2449
2450
  static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
  				    struct cfq_queue *cur_cfqq)
  {
f2d1f0ae7   Jens Axboe   cfq-iosched: cach...
2451
  	struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
a36e71f99   Jens Axboe   cfq-iosched: add ...
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
  	struct rb_node *parent, *node;
  	struct cfq_queue *__cfqq;
  	sector_t sector = cfqd->last_position;
  
  	if (RB_EMPTY_ROOT(root))
  		return NULL;
  
  	/*
  	 * First, if we find a request starting at the end of the last
  	 * request, choose it.
  	 */
f2d1f0ae7   Jens Axboe   cfq-iosched: cach...
2463
  	__cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
a36e71f99   Jens Axboe   cfq-iosched: add ...
2464
2465
2466
2467
2468
2469
2470
2471
  	if (__cfqq)
  		return __cfqq;
  
  	/*
  	 * If the exact sector wasn't found, the parent of the NULL leaf
  	 * will contain the closest sector.
  	 */
  	__cfqq = rb_entry(parent, struct cfq_queue, p_node);
e9ce335df   Shaohua Li   cfq-iosched: fix ...
2472
  	if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
a36e71f99   Jens Axboe   cfq-iosched: add ...
2473
  		return __cfqq;
2e46e8b27   Tejun Heo   block: drop reque...
2474
  	if (blk_rq_pos(__cfqq->next_rq) < sector)
a36e71f99   Jens Axboe   cfq-iosched: add ...
2475
2476
2477
2478
2479
2480
2481
  		node = rb_next(&__cfqq->p_node);
  	else
  		node = rb_prev(&__cfqq->p_node);
  	if (!node)
  		return NULL;
  
  	__cfqq = rb_entry(node, struct cfq_queue, p_node);
e9ce335df   Shaohua Li   cfq-iosched: fix ...
2482
  	if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
a36e71f99   Jens Axboe   cfq-iosched: add ...
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
  		return __cfqq;
  
  	return NULL;
  }
  
  /*
   * cfqd - obvious
   * cur_cfqq - passed in so that we don't decide that the current queue is
   * 	      closely cooperating with itself.
   *
   * So, basically we're assuming that that cur_cfqq has dispatched at least
   * one request, and that cfqd->last_position reflects a position on the disk
   * associated with the I/O issued by cur_cfqq.  I'm not sure this is a valid
   * assumption.
   */
  static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
b3b6d0408   Jeff Moyer   cfq: change the m...
2499
  					      struct cfq_queue *cur_cfqq)
6d048f531   Jens Axboe   cfq-iosched: deve...
2500
  {
a36e71f99   Jens Axboe   cfq-iosched: add ...
2501
  	struct cfq_queue *cfqq;
39c01b219   Divyesh Shah   cfq-iosched: Do n...
2502
2503
  	if (cfq_class_idle(cur_cfqq))
  		return NULL;
e6c5bc737   Jeff Moyer   cfq: break apart ...
2504
2505
2506
2507
  	if (!cfq_cfqq_sync(cur_cfqq))
  		return NULL;
  	if (CFQQ_SEEKY(cur_cfqq))
  		return NULL;
a36e71f99   Jens Axboe   cfq-iosched: add ...
2508
  	/*
b9d8f4c73   Gui Jianfeng   cfq: Optimization...
2509
2510
2511
2512
2513
2514
  	 * Don't search priority tree if it's the only queue in the group.
  	 */
  	if (cur_cfqq->cfqg->nr_cfqq == 1)
  		return NULL;
  
  	/*
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2515
2516
2517
  	 * We should notice if some of the queues are cooperating, eg
  	 * working closely on the same area of the disk. In that case,
  	 * we can group them together and don't waste time idling.
6d048f531   Jens Axboe   cfq-iosched: deve...
2518
  	 */
a36e71f99   Jens Axboe   cfq-iosched: add ...
2519
2520
2521
  	cfqq = cfqq_close(cfqd, cur_cfqq);
  	if (!cfqq)
  		return NULL;
8682e1f15   Vivek Goyal   blkio: Provide so...
2522
2523
2524
  	/* If new queue belongs to different cfq_group, don't choose it */
  	if (cur_cfqq->cfqg != cfqq->cfqg)
  		return NULL;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2525
2526
2527
2528
2529
  	/*
  	 * It only makes sense to merge sync queues.
  	 */
  	if (!cfq_cfqq_sync(cfqq))
  		return NULL;
e6c5bc737   Jeff Moyer   cfq: break apart ...
2530
2531
  	if (CFQQ_SEEKY(cfqq))
  		return NULL;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2532

c0324a020   Corrado Zoccolo   cfq-iosched: reim...
2533
2534
2535
2536
2537
  	/*
  	 * Do not merge queues of different priority classes
  	 */
  	if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
  		return NULL;
a36e71f99   Jens Axboe   cfq-iosched: add ...
2538
  	return cfqq;
6d048f531   Jens Axboe   cfq-iosched: deve...
2539
  }
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2540
2541
2542
2543
2544
2545
  /*
   * Determine whether we should enforce idle window for this queue.
   */
  
  static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
2546
  	enum wl_class_t wl_class = cfqq_class(cfqq);
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2547
  	struct cfq_rb_root *st = cfqq->service_tree;
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2548

34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2549
2550
  	BUG_ON(!st);
  	BUG_ON(!st->count);
f04a64246   Vivek Goyal   blkio: Keep queue...
2551

b6508c161   Vivek Goyal   cfq-iosched: Do n...
2552
2553
  	if (!cfqd->cfq_slice_idle)
  		return false;
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2554
  	/* We never do for idle class queues. */
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
2555
  	if (wl_class == IDLE_WORKLOAD)
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2556
2557
2558
  		return false;
  
  	/* We do for queues that were marked with idle window flag. */
3c764b7a6   Shaohua Li   cfq-iosched: make...
2559
2560
  	if (cfq_cfqq_idle_window(cfqq) &&
  	   !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2561
2562
2563
2564
2565
2566
  		return true;
  
  	/*
  	 * Otherwise, we do only if they are the last ones
  	 * in their service tree.
  	 */
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2567
2568
  	if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
  	   !cfq_io_thinktime_big(cfqd, &st->ttime, false))
c1e44756f   Shaohua Li   cfq-iosched: do c...
2569
  		return true;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2570
  	cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
c1e44756f   Shaohua Li   cfq-iosched: do c...
2571
  	return false;
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2572
  }
6d048f531   Jens Axboe   cfq-iosched: deve...
2573
  static void cfq_arm_slice_timer(struct cfq_data *cfqd)
22e2c507c   Jens Axboe   [PATCH] Update cf...
2574
  {
1792669cc   Jens Axboe   cfq-iosched: don'...
2575
  	struct cfq_queue *cfqq = cfqd->active_queue;
c58698073   Tejun Heo   block, cfq: reorg...
2576
  	struct cfq_io_cq *cic;
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2577
  	unsigned long sl, group_idle = 0;
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2578

a68bbddba   Jens Axboe   block: add queue ...
2579
  	/*
f7d7b7a7a   Jens Axboe   block: as/cfq ssd...
2580
2581
2582
  	 * SSD device without seek penalty, disable idling. But only do so
  	 * for devices that support queuing, otherwise we still have a problem
  	 * with sync vs async workloads.
a68bbddba   Jens Axboe   block: add queue ...
2583
  	 */
f7d7b7a7a   Jens Axboe   block: as/cfq ssd...
2584
  	if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
a68bbddba   Jens Axboe   block: add queue ...
2585
  		return;
dd67d0515   Jens Axboe   [PATCH] rbtree: s...
2586
  	WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
6d048f531   Jens Axboe   cfq-iosched: deve...
2587
  	WARN_ON(cfq_cfqq_slice_new(cfqq));
22e2c507c   Jens Axboe   [PATCH] Update cf...
2588
2589
2590
2591
  
  	/*
  	 * idle is disabled, either manually or by past process history
  	 */
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2592
2593
2594
2595
2596
2597
2598
  	if (!cfq_should_idle(cfqd, cfqq)) {
  		/* no queue idling. Check for group idling */
  		if (cfqd->cfq_group_idle)
  			group_idle = cfqd->cfq_group_idle;
  		else
  			return;
  	}
6d048f531   Jens Axboe   cfq-iosched: deve...
2599

22e2c507c   Jens Axboe   [PATCH] Update cf...
2600
  	/*
8e550632c   Corrado Zoccolo   cfq-iosched: fix ...
2601
  	 * still active requests from this queue, don't idle
7b679138b   Jens Axboe   cfq-iosched: add ...
2602
  	 */
8e550632c   Corrado Zoccolo   cfq-iosched: fix ...
2603
  	if (cfqq->dispatched)
7b679138b   Jens Axboe   cfq-iosched: add ...
2604
2605
2606
  		return;
  
  	/*
22e2c507c   Jens Axboe   [PATCH] Update cf...
2607
2608
  	 * task has exited, don't wait
  	 */
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2609
  	cic = cfqd->active_cic;
f6e8d01be   Tejun Heo   block: add io_con...
2610
  	if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
6d048f531   Jens Axboe   cfq-iosched: deve...
2611
  		return;
355b659c8   Corrado Zoccolo   cfq-iosched: avoi...
2612
2613
2614
2615
2616
  	/*
  	 * If our average think time is larger than the remaining time
  	 * slice, then don't idle. This avoids overrunning the allotted
  	 * time slice.
  	 */
383cd7213   Shaohua Li   CFQ: move think t...
2617
2618
  	if (sample_valid(cic->ttime.ttime_samples) &&
  	    (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) {
fd16d2631   Joe Perches   block: Add __attr...
2619
  		cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%lu",
383cd7213   Shaohua Li   CFQ: move think t...
2620
  			     cic->ttime.ttime_mean);
355b659c8   Corrado Zoccolo   cfq-iosched: avoi...
2621
  		return;
b1ffe737f   Divyesh Shah   cfq-iosched: Add ...
2622
  	}
355b659c8   Corrado Zoccolo   cfq-iosched: avoi...
2623

80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2624
2625
2626
  	/* There are other queues in the group, don't do group idle */
  	if (group_idle && cfqq->cfqg->nr_cfqq > 1)
  		return;
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2627
  	cfq_mark_cfqq_wait_request(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
2628

80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2629
2630
2631
2632
  	if (group_idle)
  		sl = cfqd->cfq_group_idle;
  	else
  		sl = cfqd->cfq_slice_idle;
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2633

7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2634
  	mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
155fead9b   Tejun Heo   blkcg: move blkio...
2635
  	cfqg_stats_set_start_idle_time(cfqq->cfqg);
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2636
2637
  	cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu group_idle: %d", sl,
  			group_idle ? 1 : 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2638
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2639
2640
2641
  /*
   * Move request from internal lists to the request queue dispatch list.
   */
165125e1e   Jens Axboe   [BLOCK] Get rid o...
2642
  static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2643
  {
3ed9a2965   Jens Axboe   cfq-iosched: impr...
2644
  	struct cfq_data *cfqd = q->elevator->elevator_data;
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2645
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
2646

7b679138b   Jens Axboe   cfq-iosched: add ...
2647
  	cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
06d218864   Jeff Moyer   cfq: choose a new...
2648
  	cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
5380a101d   Jens Axboe   [PATCH] cfq-iosch...
2649
  	cfq_remove_request(rq);
6d048f531   Jens Axboe   cfq-iosched: deve...
2650
  	cfqq->dispatched++;
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2651
  	(RQ_CFQG(rq))->dispatched++;
5380a101d   Jens Axboe   [PATCH] cfq-iosch...
2652
  	elv_dispatch_sort(q, rq);
3ed9a2965   Jens Axboe   cfq-iosched: impr...
2653

53c583d22   Corrado Zoccolo   cfq-iosched: requ...
2654
  	cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
c4e7893eb   Vivek Goyal   cfq-iosched: blkt...
2655
  	cfqq->nr_sectors += blk_rq_sectors(rq);
155fead9b   Tejun Heo   blkcg: move blkio...
2656
  	cfqg_stats_update_dispatch(cfqq->cfqg, blk_rq_bytes(rq), rq->cmd_flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2657
2658
2659
2660
2661
  }
  
  /*
   * return expired entry, or NULL to just start from scratch in rbtree
   */
febffd618   Jens Axboe   cfq-iosched: kill...
2662
  static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2663
  {
30996f40b   Jens Axboe   cfq-iosched: fix ...
2664
  	struct request *rq = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2665

3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2666
  	if (cfq_cfqq_fifo_expire(cfqq))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2667
  		return NULL;
cb8874119   Jens Axboe   cfq-iosched: twea...
2668
2669
  
  	cfq_mark_cfqq_fifo_expire(cfqq);
89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
2670
2671
  	if (list_empty(&cfqq->fifo))
  		return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2672

89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
2673
  	rq = rq_entry_fifo(cfqq->fifo.next);
8b4922d31   Jan Kara   block: Stop abusi...
2674
  	if (time_before(jiffies, rq->fifo_time))
7b679138b   Jens Axboe   cfq-iosched: add ...
2675
  		rq = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2676

30996f40b   Jens Axboe   cfq-iosched: fix ...
2677
  	cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
6d048f531   Jens Axboe   cfq-iosched: deve...
2678
  	return rq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2679
  }
22e2c507c   Jens Axboe   [PATCH] Update cf...
2680
2681
2682
2683
  static inline int
  cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
  	const int base_rq = cfqd->cfq_slice_async_rq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2684

22e2c507c   Jens Axboe   [PATCH] Update cf...
2685
  	WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2686

b9f8ce059   Namhyung Kim   cfq-iosched: alge...
2687
  	return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2688
  }
22e2c507c   Jens Axboe   [PATCH] Update cf...
2689
  /*
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2690
2691
2692
2693
2694
2695
2696
   * Must be called with the queue_lock held.
   */
  static int cfqq_process_refs(struct cfq_queue *cfqq)
  {
  	int process_refs, io_refs;
  
  	io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
30d7b9448   Shaohua Li   block cfq: don't ...
2697
  	process_refs = cfqq->ref - io_refs;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2698
2699
2700
2701
2702
2703
  	BUG_ON(process_refs < 0);
  	return process_refs;
  }
  
  static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
  {
e6c5bc737   Jeff Moyer   cfq: break apart ...
2704
  	int process_refs, new_process_refs;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2705
  	struct cfq_queue *__cfqq;
c10b61f09   Jeff Moyer   cfq: Don't allow ...
2706
2707
2708
2709
2710
2711
2712
2713
  	/*
  	 * If there are no process references on the new_cfqq, then it is
  	 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
  	 * chain may have dropped their last reference (not just their
  	 * last process reference).
  	 */
  	if (!cfqq_process_refs(new_cfqq))
  		return;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2714
2715
2716
2717
2718
2719
2720
2721
  	/* Avoid a circular list and skip interim queue merges */
  	while ((__cfqq = new_cfqq->new_cfqq)) {
  		if (__cfqq == cfqq)
  			return;
  		new_cfqq = __cfqq;
  	}
  
  	process_refs = cfqq_process_refs(cfqq);
c10b61f09   Jeff Moyer   cfq: Don't allow ...
2722
  	new_process_refs = cfqq_process_refs(new_cfqq);
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2723
2724
2725
2726
  	/*
  	 * If the process for the cfqq has gone away, there is no
  	 * sense in merging the queues.
  	 */
c10b61f09   Jeff Moyer   cfq: Don't allow ...
2727
  	if (process_refs == 0 || new_process_refs == 0)
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2728
  		return;
e6c5bc737   Jeff Moyer   cfq: break apart ...
2729
2730
2731
  	/*
  	 * Merge in the direction of the lesser amount of work.
  	 */
e6c5bc737   Jeff Moyer   cfq: break apart ...
2732
2733
  	if (new_process_refs >= process_refs) {
  		cfqq->new_cfqq = new_cfqq;
30d7b9448   Shaohua Li   block cfq: don't ...
2734
  		new_cfqq->ref += process_refs;
e6c5bc737   Jeff Moyer   cfq: break apart ...
2735
2736
  	} else {
  		new_cfqq->new_cfqq = cfqq;
30d7b9448   Shaohua Li   block cfq: don't ...
2737
  		cfqq->ref += new_process_refs;
e6c5bc737   Jeff Moyer   cfq: break apart ...
2738
  	}
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2739
  }
6d816ec7c   Vivek Goyal   cfq-iosched: Rena...
2740
  static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
2741
  			struct cfq_group *cfqg, enum wl_class_t wl_class)
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2742
2743
2744
2745
2746
2747
  {
  	struct cfq_queue *queue;
  	int i;
  	bool key_valid = false;
  	unsigned long lowest_key = 0;
  	enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
65b32a573   Vivek Goyal   cfq-iosched: Remo...
2748
2749
  	for (i = 0; i <= SYNC_WORKLOAD; ++i) {
  		/* select the one with lowest rb_key */
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2750
  		queue = cfq_rb_first(st_for(cfqg, wl_class, i));
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
  		if (queue &&
  		    (!key_valid || time_before(queue->rb_key, lowest_key))) {
  			lowest_key = queue->rb_key;
  			cur_best = i;
  			key_valid = true;
  		}
  	}
  
  	return cur_best;
  }
6d816ec7c   Vivek Goyal   cfq-iosched: Rena...
2761
2762
  static void
  choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2763
  {
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2764
2765
  	unsigned slice;
  	unsigned count;
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
2766
  	struct cfq_rb_root *st;
58ff82f34   Vivek Goyal   blkio: Implement ...
2767
  	unsigned group_slice;
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2768
  	enum wl_class_t original_class = cfqd->serving_wl_class;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
2769

718eee057   Corrado Zoccolo   cfq-iosched: fair...
2770
  	/* Choose next priority. RT > BE > IDLE */
58ff82f34   Vivek Goyal   blkio: Implement ...
2771
  	if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2772
  		cfqd->serving_wl_class = RT_WORKLOAD;
58ff82f34   Vivek Goyal   blkio: Implement ...
2773
  	else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2774
  		cfqd->serving_wl_class = BE_WORKLOAD;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2775
  	else {
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2776
  		cfqd->serving_wl_class = IDLE_WORKLOAD;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2777
2778
2779
  		cfqd->workload_expires = jiffies + 1;
  		return;
  	}
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2780
  	if (original_class != cfqd->serving_wl_class)
e4ea0c16a   Shaohua Li writes   block cfq: select...
2781
  		goto new_workload;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2782
2783
2784
2785
2786
  	/*
  	 * For RT and BE, we have to choose also the type
  	 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
  	 * expiration time
  	 */
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2787
  	st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
2788
  	count = st->count;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2789
2790
  
  	/*
65b32a573   Vivek Goyal   cfq-iosched: Remo...
2791
  	 * check workload expiration, and that we still have other queues ready
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2792
  	 */
65b32a573   Vivek Goyal   cfq-iosched: Remo...
2793
  	if (count && !time_after(jiffies, cfqd->workload_expires))
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2794
  		return;
e4ea0c16a   Shaohua Li writes   block cfq: select...
2795
  new_workload:
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2796
  	/* otherwise select new workload type */
6d816ec7c   Vivek Goyal   cfq-iosched: Rena...
2797
  	cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2798
  					cfqd->serving_wl_class);
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2799
  	st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
2800
  	count = st->count;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2801
2802
2803
2804
2805
2806
  
  	/*
  	 * the workload slice is computed as a fraction of target latency
  	 * proportional to the number of queues in that workload, over
  	 * all the queues in the same priority class
  	 */
58ff82f34   Vivek Goyal   blkio: Implement ...
2807
2808
2809
  	group_slice = cfq_group_slice(cfqd, cfqg);
  
  	slice = group_slice * count /
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2810
2811
  		max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
  		      cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
2812
  					cfqg));
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2813

4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2814
  	if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
f26bd1f0a   Vivek Goyal   blkio: Determine ...
2815
2816
2817
2818
2819
2820
2821
2822
2823
  		unsigned int tmp;
  
  		/*
  		 * Async queues are currently system wide. Just taking
  		 * proportion of queues with-in same group will lead to higher
  		 * async ratio system wide as generally root group is going
  		 * to have higher weight. A more accurate thing would be to
  		 * calculate system wide asnc/sync ratio.
  		 */
5bf14c072   Tao Ma   block: Make cfq_t...
2824
2825
  		tmp = cfqd->cfq_target_latency *
  			cfqg_busy_async_queues(cfqd, cfqg);
f26bd1f0a   Vivek Goyal   blkio: Determine ...
2826
2827
  		tmp = tmp/cfqd->busy_queues;
  		slice = min_t(unsigned, slice, tmp);
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2828
2829
2830
  		/* async workload slice is scaled down according to
  		 * the sync/async slice ratio. */
  		slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
f26bd1f0a   Vivek Goyal   blkio: Determine ...
2831
  	} else
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2832
2833
2834
2835
  		/* sync workload slice is at least 2 * cfq_slice_idle */
  		slice = max(slice, 2 * cfqd->cfq_slice_idle);
  
  	slice = max_t(unsigned, slice, CFQ_MIN_TT);
b1ffe737f   Divyesh Shah   cfq-iosched: Add ...
2836
  	cfq_log(cfqd, "workload slice:%d", slice);
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2837
2838
  	cfqd->workload_expires = jiffies + slice;
  }
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
2839
2840
2841
  static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
  {
  	struct cfq_rb_root *st = &cfqd->grp_service_tree;
25bc6b077   Vivek Goyal   blkio: Introduce ...
2842
  	struct cfq_group *cfqg;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
2843
2844
2845
  
  	if (RB_EMPTY_ROOT(&st->rb))
  		return NULL;
25bc6b077   Vivek Goyal   blkio: Introduce ...
2846
  	cfqg = cfq_rb_first_group(st);
25bc6b077   Vivek Goyal   blkio: Introduce ...
2847
2848
  	update_min_vdisktime(st);
  	return cfqg;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
2849
  }
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
2850
2851
  static void cfq_choose_cfqg(struct cfq_data *cfqd)
  {
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
2852
2853
2854
  	struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
  
  	cfqd->serving_group = cfqg;
dae739ebc   Vivek Goyal   blkio: Group time...
2855
2856
  
  	/* Restore the workload type data */
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2857
2858
2859
2860
  	if (cfqg->saved_wl_slice) {
  		cfqd->workload_expires = jiffies + cfqg->saved_wl_slice;
  		cfqd->serving_wl_type = cfqg->saved_wl_type;
  		cfqd->serving_wl_class = cfqg->saved_wl_class;
66ae29197   Gui Jianfeng   cfq: set workload...
2861
2862
  	} else
  		cfqd->workload_expires = jiffies - 1;
6d816ec7c   Vivek Goyal   cfq-iosched: Rena...
2863
  	choose_wl_class_and_type(cfqd, cfqg);
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
2864
  }
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2865
  /*
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2866
2867
   * Select a queue for service. If we have a current active queue,
   * check whether to continue servicing it, or retrieve and set a new one.
22e2c507c   Jens Axboe   [PATCH] Update cf...
2868
   */
1b5ed5e1f   Tejun Heo   [BLOCK] cfq-iosch...
2869
  static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2870
  {
a36e71f99   Jens Axboe   cfq-iosched: add ...
2871
  	struct cfq_queue *cfqq, *new_cfqq = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2872

22e2c507c   Jens Axboe   [PATCH] Update cf...
2873
2874
2875
  	cfqq = cfqd->active_queue;
  	if (!cfqq)
  		goto new_queue;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2876

f04a64246   Vivek Goyal   blkio: Keep queue...
2877
2878
  	if (!cfqd->rq_queued)
  		return NULL;
c244bb50a   Vivek Goyal   cfq-iosched: Get ...
2879
2880
2881
2882
2883
2884
  
  	/*
  	 * We were waiting for group to get backlogged. Expire the queue
  	 */
  	if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
  		goto expire;
22e2c507c   Jens Axboe   [PATCH] Update cf...
2885
  	/*
6d048f531   Jens Axboe   cfq-iosched: deve...
2886
  	 * The active queue has run out of time, expire it and select new.
22e2c507c   Jens Axboe   [PATCH] Update cf...
2887
  	 */
7667aa063   Vivek Goyal   cfq-iosched: Take...
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
  	if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
  		/*
  		 * If slice had not expired at the completion of last request
  		 * we might not have turned on wait_busy flag. Don't expire
  		 * the queue yet. Allow the group to get backlogged.
  		 *
  		 * The very fact that we have used the slice, that means we
  		 * have been idling all along on this queue and it should be
  		 * ok to wait for this request to complete.
  		 */
82bbbf28d   Vivek Goyal   Fix a CFQ crash i...
2898
2899
2900
  		if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
  		    && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
  			cfqq = NULL;
7667aa063   Vivek Goyal   cfq-iosched: Take...
2901
  			goto keep_queue;
82bbbf28d   Vivek Goyal   Fix a CFQ crash i...
2902
  		} else
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2903
  			goto check_group_idle;
7667aa063   Vivek Goyal   cfq-iosched: Take...
2904
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2905

22e2c507c   Jens Axboe   [PATCH] Update cf...
2906
  	/*
6d048f531   Jens Axboe   cfq-iosched: deve...
2907
2908
  	 * The active queue has requests and isn't expired, allow it to
  	 * dispatch.
22e2c507c   Jens Axboe   [PATCH] Update cf...
2909
  	 */
dd67d0515   Jens Axboe   [PATCH] rbtree: s...
2910
  	if (!RB_EMPTY_ROOT(&cfqq->sort_list))
22e2c507c   Jens Axboe   [PATCH] Update cf...
2911
  		goto keep_queue;
6d048f531   Jens Axboe   cfq-iosched: deve...
2912
2913
  
  	/*
a36e71f99   Jens Axboe   cfq-iosched: add ...
2914
2915
2916
  	 * If another queue has a request waiting within our mean seek
  	 * distance, let it run.  The expire code will check for close
  	 * cooperators and put the close queue at the front of the service
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2917
  	 * tree.  If possible, merge the expiring queue with the new cfqq.
a36e71f99   Jens Axboe   cfq-iosched: add ...
2918
  	 */
b3b6d0408   Jeff Moyer   cfq: change the m...
2919
  	new_cfqq = cfq_close_cooperator(cfqd, cfqq);
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2920
2921
2922
  	if (new_cfqq) {
  		if (!cfqq->new_cfqq)
  			cfq_setup_merge(cfqq, new_cfqq);
a36e71f99   Jens Axboe   cfq-iosched: add ...
2923
  		goto expire;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2924
  	}
a36e71f99   Jens Axboe   cfq-iosched: add ...
2925
2926
  
  	/*
6d048f531   Jens Axboe   cfq-iosched: deve...
2927
2928
2929
2930
  	 * No requests pending. If the active queue still has requests in
  	 * flight or is idling for a new request, allow either of these
  	 * conditions to happen (or time out) before selecting a new queue.
  	 */
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2931
2932
2933
2934
  	if (timer_pending(&cfqd->idle_slice_timer)) {
  		cfqq = NULL;
  		goto keep_queue;
  	}
8e1ac6655   Shaohua Li   cfq-iosched: don'...
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
  	/*
  	 * This is a deep seek queue, but the device is much faster than
  	 * the queue can deliver, don't idle
  	 **/
  	if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
  	    (cfq_cfqq_slice_new(cfqq) ||
  	    (cfqq->slice_end - jiffies > jiffies - cfqq->slice_start))) {
  		cfq_clear_cfqq_deep(cfqq);
  		cfq_clear_cfqq_idle_window(cfqq);
  	}
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
  	if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
  		cfqq = NULL;
  		goto keep_queue;
  	}
  
  	/*
  	 * If group idle is enabled and there are requests dispatched from
  	 * this group, wait for requests to complete.
  	 */
  check_group_idle:
7700fc4f6   Shaohua Li   CFQ: add think ti...
2955
2956
2957
  	if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
  	    cfqq->cfqg->dispatched &&
  	    !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
2958
2959
  		cfqq = NULL;
  		goto keep_queue;
22e2c507c   Jens Axboe   [PATCH] Update cf...
2960
  	}
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2961
  expire:
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
2962
  	cfq_slice_expired(cfqd, 0);
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2963
  new_queue:
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2964
2965
2966
2967
2968
  	/*
  	 * Current queue expired. Check if we have to switch to a new
  	 * service tree
  	 */
  	if (!new_cfqq)
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
2969
  		cfq_choose_cfqg(cfqd);
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2970

a36e71f99   Jens Axboe   cfq-iosched: add ...
2971
  	cfqq = cfq_set_active_queue(cfqd, new_cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
2972
  keep_queue:
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2973
  	return cfqq;
22e2c507c   Jens Axboe   [PATCH] Update cf...
2974
  }
febffd618   Jens Axboe   cfq-iosched: kill...
2975
  static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2976
2977
2978
2979
2980
2981
2982
2983
2984
  {
  	int dispatched = 0;
  
  	while (cfqq->next_rq) {
  		cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
  		dispatched++;
  	}
  
  	BUG_ON(!list_empty(&cfqq->fifo));
f04a64246   Vivek Goyal   blkio: Keep queue...
2985
2986
  
  	/* By default cfqq is not expired if it is empty. Do it explicitly */
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
2987
  	__cfq_slice_expired(cfqq->cfqd, cfqq, 0);
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2988
2989
  	return dispatched;
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2990
2991
2992
2993
  /*
   * Drain our current requests. Used for barriers and when switching
   * io schedulers on-the-fly.
   */
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2994
  static int cfq_forced_dispatch(struct cfq_data *cfqd)
1b5ed5e1f   Tejun Heo   [BLOCK] cfq-iosch...
2995
  {
0871714e0   Jens Axboe   cfq-iosched: rela...
2996
  	struct cfq_queue *cfqq;
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2997
  	int dispatched = 0;
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
2998

3440c49f5   Divyesh Shah   cfq-iosched: Fix ...
2999
  	/* Expire the timeslice of the current active queue first */
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
3000
  	cfq_slice_expired(cfqd, 0);
3440c49f5   Divyesh Shah   cfq-iosched: Fix ...
3001
3002
  	while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
  		__cfq_set_active_queue(cfqd, cfqq);
f04a64246   Vivek Goyal   blkio: Keep queue...
3003
  		dispatched += __cfq_forced_dispatch_cfqq(cfqq);
3440c49f5   Divyesh Shah   cfq-iosched: Fix ...
3004
  	}
1b5ed5e1f   Tejun Heo   [BLOCK] cfq-iosch...
3005

1b5ed5e1f   Tejun Heo   [BLOCK] cfq-iosch...
3006
  	BUG_ON(cfqd->busy_queues);
6923715ae   Jeff Moyer   cfq: remove extra...
3007
  	cfq_log(cfqd, "forced_dispatch=%d", dispatched);
1b5ed5e1f   Tejun Heo   [BLOCK] cfq-iosch...
3008
3009
  	return dispatched;
  }
abc3c744d   Shaohua Li   cfq-iosched: quan...
3010
3011
3012
3013
3014
  static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
  	struct cfq_queue *cfqq)
  {
  	/* the queue hasn't finished any request, can't estimate */
  	if (cfq_cfqq_slice_new(cfqq))
c1e44756f   Shaohua Li   cfq-iosched: do c...
3015
  		return true;
abc3c744d   Shaohua Li   cfq-iosched: quan...
3016
3017
  	if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched,
  		cfqq->slice_end))
c1e44756f   Shaohua Li   cfq-iosched: do c...
3018
  		return true;
abc3c744d   Shaohua Li   cfq-iosched: quan...
3019

c1e44756f   Shaohua Li   cfq-iosched: do c...
3020
  	return false;
abc3c744d   Shaohua Li   cfq-iosched: quan...
3021
  }
0b182d617   Jens Axboe   cfq-iosched: abst...
3022
  static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3023
  {
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3024
  	unsigned int max_dispatch;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3025

2f5cb7381   Jens Axboe   cfq-iosched: chan...
3026
  	/*
5ad531db6   Jens Axboe   cfq-iosched: drai...
3027
3028
  	 * Drain async requests before we start sync IO
  	 */
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3029
  	if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
0b182d617   Jens Axboe   cfq-iosched: abst...
3030
  		return false;
5ad531db6   Jens Axboe   cfq-iosched: drai...
3031
3032
  
  	/*
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3033
3034
  	 * If this is an async queue and we have sync IO in flight, let it wait
  	 */
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3035
  	if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
0b182d617   Jens Axboe   cfq-iosched: abst...
3036
  		return false;
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3037

abc3c744d   Shaohua Li   cfq-iosched: quan...
3038
  	max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3039
3040
  	if (cfq_class_idle(cfqq))
  		max_dispatch = 1;
b4878f245   Jens Axboe   [PATCH] 02/05: up...
3041

2f5cb7381   Jens Axboe   cfq-iosched: chan...
3042
3043
3044
3045
  	/*
  	 * Does this cfqq already have too much IO in flight?
  	 */
  	if (cfqq->dispatched >= max_dispatch) {
ef8a41df8   Shaohua Li   cfq-iosched: give...
3046
  		bool promote_sync = false;
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3047
3048
3049
  		/*
  		 * idle queue must always only have a single IO in flight
  		 */
3ed9a2965   Jens Axboe   cfq-iosched: impr...
3050
  		if (cfq_class_idle(cfqq))
0b182d617   Jens Axboe   cfq-iosched: abst...
3051
  			return false;
3ed9a2965   Jens Axboe   cfq-iosched: impr...
3052

2f5cb7381   Jens Axboe   cfq-iosched: chan...
3053
  		/*
c4ade94fc   Li, Shaohua   cfq-iosched: remo...
3054
3055
  		 * If there is only one sync queue
  		 * we can ignore async queue here and give the sync
ef8a41df8   Shaohua Li   cfq-iosched: give...
3056
3057
3058
3059
  		 * queue no dispatch limit. The reason is a sync queue can
  		 * preempt async queue, limiting the sync queue doesn't make
  		 * sense. This is useful for aiostress test.
  		 */
c4ade94fc   Li, Shaohua   cfq-iosched: remo...
3060
3061
  		if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
  			promote_sync = true;
ef8a41df8   Shaohua Li   cfq-iosched: give...
3062
3063
  
  		/*
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3064
3065
  		 * We have other queues, don't allow more IO from this one
  		 */
ef8a41df8   Shaohua Li   cfq-iosched: give...
3066
3067
  		if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
  				!promote_sync)
0b182d617   Jens Axboe   cfq-iosched: abst...
3068
  			return false;
9ede209e8   Jens Axboe   cfq-iosched: impr...
3069

2f5cb7381   Jens Axboe   cfq-iosched: chan...
3070
  		/*
474b18ccc   Shaohua Li   cfq-iosched: no d...
3071
  		 * Sole queue user, no limit
365722bb9   Vivek Goyal   cfq-iosched: dela...
3072
  		 */
ef8a41df8   Shaohua Li   cfq-iosched: give...
3073
  		if (cfqd->busy_queues == 1 || promote_sync)
abc3c744d   Shaohua Li   cfq-iosched: quan...
3074
3075
3076
3077
3078
3079
3080
3081
3082
  			max_dispatch = -1;
  		else
  			/*
  			 * Normally we start throttling cfqq when cfq_quantum/2
  			 * requests have been dispatched. But we can drive
  			 * deeper queue depths at the beginning of slice
  			 * subjected to upper limit of cfq_quantum.
  			 * */
  			max_dispatch = cfqd->cfq_quantum;
8e2967555   Jens Axboe   cfq-iosched: impl...
3083
3084
3085
3086
3087
3088
3089
  	}
  
  	/*
  	 * Async queues must wait a bit before being allowed dispatch.
  	 * We also ramp up the dispatch depth gradually for async IO,
  	 * based on the last sync IO we serviced
  	 */
963b72fc6   Jens Axboe   cfq-iosched: rena...
3090
  	if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
573412b29   Corrado Zoccolo   cfq-iosched: redu...
3091
  		unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
8e2967555   Jens Axboe   cfq-iosched: impl...
3092
  		unsigned int depth;
365722bb9   Vivek Goyal   cfq-iosched: dela...
3093

61f0c1dca   Jens Axboe   cfq-iosched: use ...
3094
  		depth = last_sync / cfqd->cfq_slice[1];
e00c54c36   Jens Axboe   cfq-iosched: don'...
3095
3096
  		if (!depth && !cfqq->dispatched)
  			depth = 1;
8e2967555   Jens Axboe   cfq-iosched: impl...
3097
3098
  		if (depth < max_dispatch)
  			max_dispatch = depth;
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3099
  	}
3ed9a2965   Jens Axboe   cfq-iosched: impr...
3100

0b182d617   Jens Axboe   cfq-iosched: abst...
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
  	/*
  	 * If we're below the current max, allow a dispatch
  	 */
  	return cfqq->dispatched < max_dispatch;
  }
  
  /*
   * Dispatch a request from cfqq, moving them to the request queue
   * dispatch list.
   */
  static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
  	struct request *rq;
  
  	BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
  
  	if (!cfq_may_dispatch(cfqd, cfqq))
  		return false;
  
  	/*
  	 * follow expired path, else get first next available
  	 */
  	rq = cfq_check_fifo(cfqq);
  	if (!rq)
  		rq = cfqq->next_rq;
  
  	/*
  	 * insert request into driver dispatch list
  	 */
  	cfq_dispatch_insert(cfqd->queue, rq);
  
  	if (!cfqd->active_cic) {
c58698073   Tejun Heo   block, cfq: reorg...
3133
  		struct cfq_io_cq *cic = RQ_CIC(rq);
0b182d617   Jens Axboe   cfq-iosched: abst...
3134

c58698073   Tejun Heo   block, cfq: reorg...
3135
  		atomic_long_inc(&cic->icq.ioc->refcount);
0b182d617   Jens Axboe   cfq-iosched: abst...
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
  		cfqd->active_cic = cic;
  	}
  
  	return true;
  }
  
  /*
   * Find the cfqq that we need to service and move a request from that to the
   * dispatch list
   */
  static int cfq_dispatch_requests(struct request_queue *q, int force)
  {
  	struct cfq_data *cfqd = q->elevator->elevator_data;
  	struct cfq_queue *cfqq;
  
  	if (!cfqd->busy_queues)
  		return 0;
  
  	if (unlikely(force))
  		return cfq_forced_dispatch(cfqd);
  
  	cfqq = cfq_select_queue(cfqd);
  	if (!cfqq)
8e2967555   Jens Axboe   cfq-iosched: impl...
3159
  		return 0;
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3160
  	/*
0b182d617   Jens Axboe   cfq-iosched: abst...
3161
  	 * Dispatch a request from this cfqq, if it is allowed
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3162
  	 */
0b182d617   Jens Axboe   cfq-iosched: abst...
3163
3164
  	if (!cfq_dispatch_request(cfqd, cfqq))
  		return 0;
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3165
  	cfqq->slice_dispatch++;
b029195dd   Jens Axboe   cfq-iosched: don'...
3166
  	cfq_clear_cfqq_must_dispatch(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3167

2f5cb7381   Jens Axboe   cfq-iosched: chan...
3168
3169
3170
3171
3172
3173
3174
3175
  	/*
  	 * expire an async queue immediately if it has used up its slice. idle
  	 * queue always expire after 1 dispatch round.
  	 */
  	if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
  	    cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
  	    cfq_class_idle(cfqq))) {
  		cfqq->slice_end = jiffies + 1;
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
3176
  		cfq_slice_expired(cfqd, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3177
  	}
b217a903a   Shan Wei   cfq: fix the log ...
3178
  	cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3179
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3180
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3181
  /*
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3182
3183
   * task holds one reference to the queue, dropped when task exits. each rq
   * in-flight on this queue also holds a reference, dropped when rq is freed.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3184
   *
b1c357696   Vivek Goyal   blkio: Take care ...
3185
   * Each cfq queue took a reference on the parent group. Drop it now.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3186
3187
3188
3189
   * queue lock must be held here.
   */
  static void cfq_put_queue(struct cfq_queue *cfqq)
  {
22e2c507c   Jens Axboe   [PATCH] Update cf...
3190
  	struct cfq_data *cfqd = cfqq->cfqd;
0bbfeb832   Justin TerAvest   cfq-iosched: Alwa...
3191
  	struct cfq_group *cfqg;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3192

30d7b9448   Shaohua Li   block cfq: don't ...
3193
  	BUG_ON(cfqq->ref <= 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3194

30d7b9448   Shaohua Li   block cfq: don't ...
3195
3196
  	cfqq->ref--;
  	if (cfqq->ref)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3197
  		return;
7b679138b   Jens Axboe   cfq-iosched: add ...
3198
  	cfq_log_cfqq(cfqd, cfqq, "put_queue");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3199
  	BUG_ON(rb_first(&cfqq->sort_list));
22e2c507c   Jens Axboe   [PATCH] Update cf...
3200
  	BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
b1c357696   Vivek Goyal   blkio: Take care ...
3201
  	cfqg = cfqq->cfqg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3202

28f95cbc3   Jens Axboe   cfq-iosched: remo...
3203
  	if (unlikely(cfqd->active_queue == cfqq)) {
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
3204
  		__cfq_slice_expired(cfqd, cfqq, 0);
23e018a1b   Jens Axboe   block: get rid of...
3205
  		cfq_schedule_dispatch(cfqd);
28f95cbc3   Jens Axboe   cfq-iosched: remo...
3206
  	}
22e2c507c   Jens Axboe   [PATCH] Update cf...
3207

f04a64246   Vivek Goyal   blkio: Keep queue...
3208
  	BUG_ON(cfq_cfqq_on_rr(cfqq));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3209
  	kmem_cache_free(cfq_pool, cfqq);
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
3210
  	cfqg_put(cfqg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3211
  }
d02a2c077   Shaohua Li   cfq-iosched: fix ...
3212
  static void cfq_put_cooperator(struct cfq_queue *cfqq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3213
  {
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
3214
  	struct cfq_queue *__cfqq, *next;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
  	/*
  	 * If this queue was scheduled to merge with another queue, be
  	 * sure to drop the reference taken on that queue (and others in
  	 * the merge chain).  See cfq_setup_merge and cfq_merge_cfqqs.
  	 */
  	__cfqq = cfqq->new_cfqq;
  	while (__cfqq) {
  		if (__cfqq == cfqq) {
  			WARN(1, "cfqq->new_cfqq loop detected
  ");
  			break;
  		}
  		next = __cfqq->new_cfqq;
  		cfq_put_queue(__cfqq);
  		__cfqq = next;
  	}
d02a2c077   Shaohua Li   cfq-iosched: fix ...
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
  }
  
  static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
  	if (unlikely(cfqq == cfqd->active_queue)) {
  		__cfq_slice_expired(cfqd, cfqq, 0);
  		cfq_schedule_dispatch(cfqd);
  	}
  
  	cfq_put_cooperator(cfqq);
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
3241

89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
3242
3243
  	cfq_put_queue(cfqq);
  }
22e2c507c   Jens Axboe   [PATCH] Update cf...
3244

9b84cacd0   Tejun Heo   block, cfq: restr...
3245
3246
3247
3248
3249
3250
  static void cfq_init_icq(struct io_cq *icq)
  {
  	struct cfq_io_cq *cic = icq_to_cic(icq);
  
  	cic->ttime.last_end_request = jiffies;
  }
c58698073   Tejun Heo   block, cfq: reorg...
3251
  static void cfq_exit_icq(struct io_cq *icq)
89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
3252
  {
c58698073   Tejun Heo   block, cfq: reorg...
3253
  	struct cfq_io_cq *cic = icq_to_cic(icq);
283287a52   Tejun Heo   block, cfq: misc ...
3254
  	struct cfq_data *cfqd = cic_to_cfqd(cic);
4faa3c815   Fabio Checconi   cfq-iosched: do n...
3255

ff6657c6c   Jens Axboe   cfq-iosched: get ...
3256
3257
3258
  	if (cic->cfqq[BLK_RW_ASYNC]) {
  		cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
  		cic->cfqq[BLK_RW_ASYNC] = NULL;
12a057321   Al Viro   [PATCH] keep sync...
3259
  	}
ff6657c6c   Jens Axboe   cfq-iosched: get ...
3260
3261
3262
  	if (cic->cfqq[BLK_RW_SYNC]) {
  		cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
  		cic->cfqq[BLK_RW_SYNC] = NULL;
12a057321   Al Viro   [PATCH] keep sync...
3263
  	}
89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
3264
  }
abede6da2   Tejun Heo   cfq: pass around ...
3265
  static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3266
3267
3268
  {
  	struct task_struct *tsk = current;
  	int ioprio_class;
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
3269
  	if (!cfq_cfqq_prio_changed(cfqq))
22e2c507c   Jens Axboe   [PATCH] Update cf...
3270
  		return;
598971bfb   Tejun Heo   cfq: don't use ic...
3271
  	ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3272
  	switch (ioprio_class) {
fe094d98e   Jens Axboe   cfq-iosched: make...
3273
3274
3275
3276
3277
  	default:
  		printk(KERN_ERR "cfq: bad prio %x
  ", ioprio_class);
  	case IOPRIO_CLASS_NONE:
  		/*
6d63c2755   Jens Axboe   cfq-iosched: make...
3278
  		 * no prio set, inherit CPU scheduling settings
fe094d98e   Jens Axboe   cfq-iosched: make...
3279
3280
  		 */
  		cfqq->ioprio = task_nice_ioprio(tsk);
6d63c2755   Jens Axboe   cfq-iosched: make...
3281
  		cfqq->ioprio_class = task_nice_ioclass(tsk);
fe094d98e   Jens Axboe   cfq-iosched: make...
3282
3283
  		break;
  	case IOPRIO_CLASS_RT:
598971bfb   Tejun Heo   cfq: don't use ic...
3284
  		cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
fe094d98e   Jens Axboe   cfq-iosched: make...
3285
3286
3287
  		cfqq->ioprio_class = IOPRIO_CLASS_RT;
  		break;
  	case IOPRIO_CLASS_BE:
598971bfb   Tejun Heo   cfq: don't use ic...
3288
  		cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
fe094d98e   Jens Axboe   cfq-iosched: make...
3289
3290
3291
3292
3293
3294
3295
  		cfqq->ioprio_class = IOPRIO_CLASS_BE;
  		break;
  	case IOPRIO_CLASS_IDLE:
  		cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
  		cfqq->ioprio = 7;
  		cfq_clear_cfqq_idle_window(cfqq);
  		break;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3296
3297
3298
3299
3300
3301
3302
  	}
  
  	/*
  	 * keep track of original prio settings in case we have to temporarily
  	 * elevate the priority of this queue
  	 */
  	cfqq->org_ioprio = cfqq->ioprio;
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
3303
  	cfq_clear_cfqq_prio_changed(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3304
  }
598971bfb   Tejun Heo   cfq: don't use ic...
3305
  static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3306
  {
598971bfb   Tejun Heo   cfq: don't use ic...
3307
  	int ioprio = cic->icq.ioc->ioprio;
bca4b914b   Konstantin Khlebnikov   cfq-iosched: remo...
3308
  	struct cfq_data *cfqd = cic_to_cfqd(cic);
478a82b0e   Al Viro   [PATCH] switch to...
3309
  	struct cfq_queue *cfqq;
35e6077cb   Jens Axboe   [PATCH] cfq-iosch...
3310

598971bfb   Tejun Heo   cfq: don't use ic...
3311
3312
3313
3314
3315
  	/*
  	 * Check whether ioprio has changed.  The condition may trigger
  	 * spuriously on a newly created cic but there's no harm.
  	 */
  	if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
3316
  		return;
ff6657c6c   Jens Axboe   cfq-iosched: get ...
3317
  	cfqq = cic->cfqq[BLK_RW_ASYNC];
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
3318
3319
  	if (cfqq) {
  		struct cfq_queue *new_cfqq;
abede6da2   Tejun Heo   cfq: pass around ...
3320
3321
  		new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio,
  					 GFP_ATOMIC);
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
3322
  		if (new_cfqq) {
ff6657c6c   Jens Axboe   cfq-iosched: get ...
3323
  			cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
3324
3325
  			cfq_put_queue(cfqq);
  		}
22e2c507c   Jens Axboe   [PATCH] Update cf...
3326
  	}
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
3327

ff6657c6c   Jens Axboe   cfq-iosched: get ...
3328
  	cfqq = cic->cfqq[BLK_RW_SYNC];
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
3329
3330
  	if (cfqq)
  		cfq_mark_cfqq_prio_changed(cfqq);
598971bfb   Tejun Heo   cfq: don't use ic...
3331
3332
  
  	cic->ioprio = ioprio;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3333
  }
d5036d770   Jens Axboe   cfq-iosched: move...
3334
  static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3335
  			  pid_t pid, bool is_sync)
d5036d770   Jens Axboe   cfq-iosched: move...
3336
3337
3338
3339
  {
  	RB_CLEAR_NODE(&cfqq->rb_node);
  	RB_CLEAR_NODE(&cfqq->p_node);
  	INIT_LIST_HEAD(&cfqq->fifo);
30d7b9448   Shaohua Li   block cfq: don't ...
3340
  	cfqq->ref = 0;
d5036d770   Jens Axboe   cfq-iosched: move...
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
  	cfqq->cfqd = cfqd;
  
  	cfq_mark_cfqq_prio_changed(cfqq);
  
  	if (is_sync) {
  		if (!cfq_class_idle(cfqq))
  			cfq_mark_cfqq_idle_window(cfqq);
  		cfq_mark_cfqq_sync(cfqq);
  	}
  	cfqq->pid = pid;
  }
24610333d   Vivek Goyal   blkio: Drop the r...
3352
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
598971bfb   Tejun Heo   cfq: don't use ic...
3353
  static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
24610333d   Vivek Goyal   blkio: Drop the r...
3354
  {
bca4b914b   Konstantin Khlebnikov   cfq-iosched: remo...
3355
  	struct cfq_data *cfqd = cic_to_cfqd(cic);
598971bfb   Tejun Heo   cfq: don't use ic...
3356
  	struct cfq_queue *sync_cfqq;
f4da80727   Tejun Heo   blkcg: remove blk...
3357
  	uint64_t serial_nr;
24610333d   Vivek Goyal   blkio: Drop the r...
3358

598971bfb   Tejun Heo   cfq: don't use ic...
3359
  	rcu_read_lock();
f4da80727   Tejun Heo   blkcg: remove blk...
3360
  	serial_nr = bio_blkcg(bio)->css.serial_nr;
598971bfb   Tejun Heo   cfq: don't use ic...
3361
  	rcu_read_unlock();
24610333d   Vivek Goyal   blkio: Drop the r...
3362

598971bfb   Tejun Heo   cfq: don't use ic...
3363
3364
3365
3366
  	/*
  	 * Check whether blkcg has changed.  The condition may trigger
  	 * spuriously on a newly created cic but there's no harm.
  	 */
f4da80727   Tejun Heo   blkcg: remove blk...
3367
  	if (unlikely(!cfqd) || likely(cic->blkcg_serial_nr == serial_nr))
598971bfb   Tejun Heo   cfq: don't use ic...
3368
  		return;
24610333d   Vivek Goyal   blkio: Drop the r...
3369

598971bfb   Tejun Heo   cfq: don't use ic...
3370
  	sync_cfqq = cic_to_cfqq(cic, 1);
24610333d   Vivek Goyal   blkio: Drop the r...
3371
3372
3373
3374
3375
3376
3377
3378
3379
  	if (sync_cfqq) {
  		/*
  		 * Drop reference to sync queue. A new sync queue will be
  		 * assigned in new group upon arrival of a fresh request.
  		 */
  		cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
  		cic_set_cfqq(cic, NULL, 1);
  		cfq_put_queue(sync_cfqq);
  	}
598971bfb   Tejun Heo   cfq: don't use ic...
3380

f4da80727   Tejun Heo   blkcg: remove blk...
3381
  	cic->blkcg_serial_nr = serial_nr;
24610333d   Vivek Goyal   blkio: Drop the r...
3382
  }
598971bfb   Tejun Heo   cfq: don't use ic...
3383
3384
  #else
  static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) { }
24610333d   Vivek Goyal   blkio: Drop the r...
3385
  #endif  /* CONFIG_CFQ_GROUP_IOSCHED */
22e2c507c   Jens Axboe   [PATCH] Update cf...
3386
  static struct cfq_queue *
abede6da2   Tejun Heo   cfq: pass around ...
3387
3388
  cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
  		     struct bio *bio, gfp_t gfp_mask)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3389
  {
3c798398e   Tejun Heo   blkcg: mass renam...
3390
  	struct blkcg *blkcg;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3391
  	struct cfq_queue *cfqq, *new_cfqq = NULL;
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
3392
  	struct cfq_group *cfqg;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3393
3394
  
  retry:
2a7f12441   Tejun Heo   blkcg: move rcu_r...
3395
  	rcu_read_lock();
3c798398e   Tejun Heo   blkcg: mass renam...
3396
  	blkcg = bio_blkcg(bio);
cd1604fab   Tejun Heo   blkcg: factor out...
3397
  	cfqg = cfq_lookup_create_cfqg(cfqd, blkcg);
69abaffec   Konstantin Khlebnikov   cfq-iosched: hand...
3398
3399
3400
3401
  	if (!cfqg) {
  		cfqq = &cfqd->oom_cfqq;
  		goto out;
  	}
91fac317a   Vasily Tarasov   cfq-iosched: get ...
3402
  	cfqq = cic_to_cfqq(cic, is_sync);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3403

6118b70b3   Jens Axboe   cfq-iosched: get ...
3404
3405
3406
3407
3408
3409
  	/*
  	 * Always try a new alloc if we fell back to the OOM cfqq
  	 * originally, since it should just be a temporary situation.
  	 */
  	if (!cfqq || cfqq == &cfqd->oom_cfqq) {
  		cfqq = NULL;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3410
3411
3412
3413
  		if (new_cfqq) {
  			cfqq = new_cfqq;
  			new_cfqq = NULL;
  		} else if (gfp_mask & __GFP_WAIT) {
2a7f12441   Tejun Heo   blkcg: move rcu_r...
3414
  			rcu_read_unlock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
3415
  			spin_unlock_irq(cfqd->queue->queue_lock);
94f6030ca   Christoph Lameter   Slab allocators: ...
3416
  			new_cfqq = kmem_cache_alloc_node(cfq_pool,
6118b70b3   Jens Axboe   cfq-iosched: get ...
3417
  					gfp_mask | __GFP_ZERO,
94f6030ca   Christoph Lameter   Slab allocators: ...
3418
  					cfqd->queue->node);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3419
  			spin_lock_irq(cfqd->queue->queue_lock);
6118b70b3   Jens Axboe   cfq-iosched: get ...
3420
3421
  			if (new_cfqq)
  				goto retry;
a3cc86c2f   Glauber Costa   cfq: fix lock imb...
3422
3423
  			else
  				return &cfqd->oom_cfqq;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3424
  		} else {
94f6030ca   Christoph Lameter   Slab allocators: ...
3425
3426
3427
  			cfqq = kmem_cache_alloc_node(cfq_pool,
  					gfp_mask | __GFP_ZERO,
  					cfqd->queue->node);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3428
  		}
6118b70b3   Jens Axboe   cfq-iosched: get ...
3429
3430
  		if (cfqq) {
  			cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
abede6da2   Tejun Heo   cfq: pass around ...
3431
  			cfq_init_prio_data(cfqq, cic);
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
3432
  			cfq_link_cfqq_cfqg(cfqq, cfqg);
6118b70b3   Jens Axboe   cfq-iosched: get ...
3433
3434
3435
  			cfq_log_cfqq(cfqd, cfqq, "alloced");
  		} else
  			cfqq = &cfqd->oom_cfqq;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3436
  	}
69abaffec   Konstantin Khlebnikov   cfq-iosched: hand...
3437
  out:
22e2c507c   Jens Axboe   [PATCH] Update cf...
3438
3439
  	if (new_cfqq)
  		kmem_cache_free(cfq_pool, new_cfqq);
2a7f12441   Tejun Heo   blkcg: move rcu_r...
3440
  	rcu_read_unlock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
3441
3442
  	return cfqq;
  }
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3443
3444
3445
  static struct cfq_queue **
  cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
  {
fe094d98e   Jens Axboe   cfq-iosched: make...
3446
  	switch (ioprio_class) {
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3447
3448
  	case IOPRIO_CLASS_RT:
  		return &cfqd->async_cfqq[0][ioprio];
598971bfb   Tejun Heo   cfq: don't use ic...
3449
3450
3451
  	case IOPRIO_CLASS_NONE:
  		ioprio = IOPRIO_NORM;
  		/* fall through */
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3452
3453
3454
3455
3456
3457
3458
3459
  	case IOPRIO_CLASS_BE:
  		return &cfqd->async_cfqq[1][ioprio];
  	case IOPRIO_CLASS_IDLE:
  		return &cfqd->async_idle_cfqq;
  	default:
  		BUG();
  	}
  }
15c31be4d   Jens Axboe   cfq-iosched: fix ...
3460
  static struct cfq_queue *
abede6da2   Tejun Heo   cfq: pass around ...
3461
  cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
4f85cb96d   Tejun Heo   block: make block...
3462
  	      struct bio *bio, gfp_t gfp_mask)
15c31be4d   Jens Axboe   cfq-iosched: fix ...
3463
  {
c6ce19432   Jeff Moyer   cfq-iosched: fix ...
3464
3465
  	int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
  	int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3466
  	struct cfq_queue **async_cfqq = NULL;
15c31be4d   Jens Axboe   cfq-iosched: fix ...
3467
  	struct cfq_queue *cfqq = NULL;
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3468
  	if (!is_sync) {
c6ce19432   Jeff Moyer   cfq-iosched: fix ...
3469
3470
3471
3472
3473
  		if (!ioprio_valid(cic->ioprio)) {
  			struct task_struct *tsk = current;
  			ioprio = task_nice_ioprio(tsk);
  			ioprio_class = task_nice_ioclass(tsk);
  		}
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3474
3475
3476
  		async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
  		cfqq = *async_cfqq;
  	}
6118b70b3   Jens Axboe   cfq-iosched: get ...
3477
  	if (!cfqq)
abede6da2   Tejun Heo   cfq: pass around ...
3478
  		cfqq = cfq_find_alloc_queue(cfqd, is_sync, cic, bio, gfp_mask);
15c31be4d   Jens Axboe   cfq-iosched: fix ...
3479
3480
3481
3482
  
  	/*
  	 * pin the queue now that it's allocated, scheduler exit will prune it
  	 */
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3483
  	if (!is_sync && !(*async_cfqq)) {
30d7b9448   Shaohua Li   block cfq: don't ...
3484
  		cfqq->ref++;
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3485
  		*async_cfqq = cfqq;
15c31be4d   Jens Axboe   cfq-iosched: fix ...
3486
  	}
30d7b9448   Shaohua Li   block cfq: don't ...
3487
  	cfqq->ref++;
15c31be4d   Jens Axboe   cfq-iosched: fix ...
3488
3489
  	return cfqq;
  }
22e2c507c   Jens Axboe   [PATCH] Update cf...
3490
  static void
383cd7213   Shaohua Li   CFQ: move think t...
3491
  __cfq_update_io_thinktime(struct cfq_ttime *ttime, unsigned long slice_idle)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3492
  {
383cd7213   Shaohua Li   CFQ: move think t...
3493
3494
  	unsigned long elapsed = jiffies - ttime->last_end_request;
  	elapsed = min(elapsed, 2UL * slice_idle);
db3b5848e   Kiyoshi Ueda   When cfq I/O sche...
3495

383cd7213   Shaohua Li   CFQ: move think t...
3496
3497
3498
3499
3500
3501
3502
  	ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
  	ttime->ttime_total = (7*ttime->ttime_total + 256*elapsed) / 8;
  	ttime->ttime_mean = (ttime->ttime_total + 128) / ttime->ttime_samples;
  }
  
  static void
  cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
c58698073   Tejun Heo   block, cfq: reorg...
3503
  			struct cfq_io_cq *cic)
383cd7213   Shaohua Li   CFQ: move think t...
3504
  {
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
3505
  	if (cfq_cfqq_sync(cfqq)) {
383cd7213   Shaohua Li   CFQ: move think t...
3506
  		__cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
3507
3508
3509
  		__cfq_update_io_thinktime(&cfqq->service_tree->ttime,
  			cfqd->cfq_slice_idle);
  	}
7700fc4f6   Shaohua Li   CFQ: add think ti...
3510
3511
3512
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
  	__cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
  #endif
22e2c507c   Jens Axboe   [PATCH] Update cf...
3513
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3514

206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
3515
  static void
b2c18e1e0   Jeff Moyer   cfq: calculate th...
3516
  cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
6d048f531   Jens Axboe   cfq-iosched: deve...
3517
  		       struct request *rq)
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
3518
  {
3dde36dde   Corrado Zoccolo   cfq-iosched: rewo...
3519
  	sector_t sdist = 0;
41647e7a9   Corrado Zoccolo   cfq-iosched: reth...
3520
  	sector_t n_sec = blk_rq_sectors(rq);
3dde36dde   Corrado Zoccolo   cfq-iosched: rewo...
3521
3522
3523
3524
3525
3526
  	if (cfqq->last_request_pos) {
  		if (cfqq->last_request_pos < blk_rq_pos(rq))
  			sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
  		else
  			sdist = cfqq->last_request_pos - blk_rq_pos(rq);
  	}
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
3527

3dde36dde   Corrado Zoccolo   cfq-iosched: rewo...
3528
  	cfqq->seek_history <<= 1;
41647e7a9   Corrado Zoccolo   cfq-iosched: reth...
3529
3530
3531
3532
  	if (blk_queue_nonrot(cfqd->queue))
  		cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
  	else
  		cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
3533
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3534

22e2c507c   Jens Axboe   [PATCH] Update cf...
3535
3536
3537
3538
3539
3540
  /*
   * Disable idle window if the process thinks too long or seeks so much that
   * it doesn't matter
   */
  static void
  cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
c58698073   Tejun Heo   block, cfq: reorg...
3541
  		       struct cfq_io_cq *cic)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3542
  {
7b679138b   Jens Axboe   cfq-iosched: add ...
3543
  	int old_idle, enable_idle;
1be92f2fc   Jens Axboe   cfq-iosched: neve...
3544

0871714e0   Jens Axboe   cfq-iosched: rela...
3545
3546
3547
3548
  	/*
  	 * Don't idle for async or idle io prio class
  	 */
  	if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
1be92f2fc   Jens Axboe   cfq-iosched: neve...
3549
  		return;
c265a7f41   Jens Axboe   cfq-iosched: get ...
3550
  	enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3551

76280aff1   Corrado Zoccolo   cfq-iosched: idli...
3552
3553
  	if (cfqq->queued[0] + cfqq->queued[1] >= 4)
  		cfq_mark_cfqq_deep(cfqq);
749ef9f84   Corrado Zoccolo   cfq: improve fsyn...
3554
3555
  	if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE))
  		enable_idle = 0;
f6e8d01be   Tejun Heo   block: add io_con...
3556
  	else if (!atomic_read(&cic->icq.ioc->active_ref) ||
c58698073   Tejun Heo   block, cfq: reorg...
3557
3558
  		 !cfqd->cfq_slice_idle ||
  		 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
22e2c507c   Jens Axboe   [PATCH] Update cf...
3559
  		enable_idle = 0;
383cd7213   Shaohua Li   CFQ: move think t...
3560
3561
  	else if (sample_valid(cic->ttime.ttime_samples)) {
  		if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3562
3563
3564
  			enable_idle = 0;
  		else
  			enable_idle = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3565
  	}
7b679138b   Jens Axboe   cfq-iosched: add ...
3566
3567
3568
3569
3570
3571
3572
  	if (old_idle != enable_idle) {
  		cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
  		if (enable_idle)
  			cfq_mark_cfqq_idle_window(cfqq);
  		else
  			cfq_clear_cfqq_idle_window(cfqq);
  	}
22e2c507c   Jens Axboe   [PATCH] Update cf...
3573
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3574

22e2c507c   Jens Axboe   [PATCH] Update cf...
3575
3576
3577
3578
  /*
   * Check if new_cfqq should preempt the currently active queue. Return 0 for
   * no or if we aren't sure, a 1 will cause a preempt.
   */
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3579
  static bool
22e2c507c   Jens Axboe   [PATCH] Update cf...
3580
  cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3581
  		   struct request *rq)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3582
  {
6d048f531   Jens Axboe   cfq-iosched: deve...
3583
  	struct cfq_queue *cfqq;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3584

6d048f531   Jens Axboe   cfq-iosched: deve...
3585
3586
  	cfqq = cfqd->active_queue;
  	if (!cfqq)
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3587
  		return false;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3588

6d048f531   Jens Axboe   cfq-iosched: deve...
3589
  	if (cfq_class_idle(new_cfqq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3590
  		return false;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3591
3592
  
  	if (cfq_class_idle(cfqq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3593
  		return true;
1e3335de0   Jens Axboe   cfq-iosched: impr...
3594

22e2c507c   Jens Axboe   [PATCH] Update cf...
3595
  	/*
875feb63b   Divyesh Shah   cfq-iosched: Resp...
3596
3597
3598
3599
3600
3601
  	 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
  	 */
  	if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
  		return false;
  
  	/*
374f84ac3   Jens Axboe   [PATCH] cfq-iosch...
3602
3603
3604
  	 * if the new request is sync, but the currently running queue is
  	 * not, let the sync request have priority.
  	 */
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3605
  	if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3606
  		return true;
1e3335de0   Jens Axboe   cfq-iosched: impr...
3607

8682e1f15   Vivek Goyal   blkio: Provide so...
3608
3609
3610
3611
3612
3613
3614
  	if (new_cfqq->cfqg != cfqq->cfqg)
  		return false;
  
  	if (cfq_slice_used(cfqq))
  		return true;
  
  	/* Allow preemption only if we are idling on sync-noidle tree */
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
3615
  	if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
8682e1f15   Vivek Goyal   blkio: Provide so...
3616
3617
3618
3619
  	    cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
  	    new_cfqq->service_tree->count == 2 &&
  	    RB_EMPTY_ROOT(&cfqq->sort_list))
  		return true;
374f84ac3   Jens Axboe   [PATCH] cfq-iosch...
3620
  	/*
b53d1ed73   Jens Axboe   Revert "cfq: Remo...
3621
3622
3623
  	 * So both queues are sync. Let the new request get disk time if
  	 * it's a metadata request and the current queue is doing regular IO.
  	 */
65299a3b7   Christoph Hellwig   block: separate p...
3624
  	if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
b53d1ed73   Jens Axboe   Revert "cfq: Remo...
3625
3626
3627
  		return true;
  
  	/*
3a9a3f6cc   Divyesh Shah   cfq-iosched: Allo...
3628
3629
3630
  	 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
  	 */
  	if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3631
  		return true;
3a9a3f6cc   Divyesh Shah   cfq-iosched: Allo...
3632

d2d59e18a   Shaohua Li   cfq-iosched: sche...
3633
3634
3635
  	/* An idle queue should not be idle now for some reason */
  	if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
  		return true;
1e3335de0   Jens Axboe   cfq-iosched: impr...
3636
  	if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3637
  		return false;
1e3335de0   Jens Axboe   cfq-iosched: impr...
3638
3639
3640
3641
3642
  
  	/*
  	 * if this request is as-good as one we would expect from the
  	 * current cfqq, let it preempt
  	 */
e9ce335df   Shaohua Li   cfq-iosched: fix ...
3643
  	if (cfq_rq_close(cfqd, cfqq, rq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3644
  		return true;
1e3335de0   Jens Axboe   cfq-iosched: impr...
3645

a6151c3a5   Jens Axboe   cfq-iosched: appl...
3646
  	return false;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3647
3648
3649
3650
3651
3652
3653
3654
  }
  
  /*
   * cfqq preempts the active queue. if we allowed preempt with no slice left,
   * let it have half of its nominal slice.
   */
  static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
df0793abb   Shaohua Li   block,cfq: change...
3655
  	enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
7b679138b   Jens Axboe   cfq-iosched: add ...
3656
  	cfq_log_cfqq(cfqd, cfqq, "preempt");
df0793abb   Shaohua Li   block,cfq: change...
3657
  	cfq_slice_expired(cfqd, 1);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3658

bf5722567   Jens Axboe   [PATCH] cfq-iosch...
3659
  	/*
f8ae6e3eb   Shaohua Li   block cfq: make q...
3660
3661
3662
  	 * workload type is changed, don't save slice, otherwise preempt
  	 * doesn't happen
  	 */
df0793abb   Shaohua Li   block,cfq: change...
3663
  	if (old_type != cfqq_type(cfqq))
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
3664
  		cfqq->cfqg->saved_wl_slice = 0;
f8ae6e3eb   Shaohua Li   block cfq: make q...
3665
3666
  
  	/*
bf5722567   Jens Axboe   [PATCH] cfq-iosch...
3667
3668
3669
3670
  	 * Put the new queue at the front of the of the current list,
  	 * so we know that it will be selected next.
  	 */
  	BUG_ON(!cfq_cfqq_on_rr(cfqq));
edd75ffd9   Jens Axboe   cfq-iosched: get ...
3671
3672
  
  	cfq_service_tree_add(cfqd, cfqq, 1);
eda5e0c91   Justin TerAvest   cfq-iosched: Don'...
3673

62a37f6ba   Justin TerAvest   cfq-iosched: Don'...
3674
3675
  	cfqq->slice_end = 0;
  	cfq_mark_cfqq_slice_new(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3676
3677
3678
  }
  
  /*
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3679
   * Called when a new fs request (rq) is added (to cfqq). Check if there's
22e2c507c   Jens Axboe   [PATCH] Update cf...
3680
3681
3682
   * something we should do about it
   */
  static void
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3683
3684
  cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  		struct request *rq)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3685
  {
c58698073   Tejun Heo   block, cfq: reorg...
3686
  	struct cfq_io_cq *cic = RQ_CIC(rq);
12e9fddd6   Jens Axboe   [PATCH] cfq-iosch...
3687

45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3688
  	cfqd->rq_queued++;
65299a3b7   Christoph Hellwig   block: separate p...
3689
3690
  	if (rq->cmd_flags & REQ_PRIO)
  		cfqq->prio_pending++;
374f84ac3   Jens Axboe   [PATCH] cfq-iosch...
3691

383cd7213   Shaohua Li   CFQ: move think t...
3692
  	cfq_update_io_thinktime(cfqd, cfqq, cic);
b2c18e1e0   Jeff Moyer   cfq: calculate th...
3693
  	cfq_update_io_seektime(cfqd, cfqq, rq);
9c2c38a12   Jens Axboe   [PATCH] cfq-iosch...
3694
  	cfq_update_idle_window(cfqd, cfqq, cic);
b2c18e1e0   Jeff Moyer   cfq: calculate th...
3695
  	cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3696
3697
3698
  
  	if (cfqq == cfqd->active_queue) {
  		/*
b029195dd   Jens Axboe   cfq-iosched: don'...
3699
3700
3701
  		 * Remember that we saw a request from this process, but
  		 * don't start queuing just yet. Otherwise we risk seeing lots
  		 * of tiny requests, because we disrupt the normal plugging
d6ceb25e8   Jens Axboe   cfq-iosched: don'...
3702
3703
  		 * and merging. If the request is already larger than a single
  		 * page, let it rip immediately. For that case we assume that
2d8707229   Jens Axboe   cfq-iosched: twea...
3704
3705
3706
  		 * merging is already done. Ditto for a busy system that
  		 * has other work pending, don't risk delaying until the
  		 * idle timer unplug to continue working.
22e2c507c   Jens Axboe   [PATCH] Update cf...
3707
  		 */
d6ceb25e8   Jens Axboe   cfq-iosched: don'...
3708
  		if (cfq_cfqq_wait_request(cfqq)) {
2d8707229   Jens Axboe   cfq-iosched: twea...
3709
3710
  			if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
  			    cfqd->busy_queues > 1) {
812df48d1   Divyesh Shah   blkio: Add more d...
3711
  				cfq_del_timer(cfqd, cfqq);
554554f60   Gui Jianfeng   cfq: Remove wait_...
3712
  				cfq_clear_cfqq_wait_request(cfqq);
24ecfbe27   Christoph Hellwig   block: add blk_ru...
3713
  				__blk_run_queue(cfqd->queue);
a11cdaa7a   Divyesh Shah   block: Update to ...
3714
  			} else {
155fead9b   Tejun Heo   blkcg: move blkio...
3715
  				cfqg_stats_update_idle_time(cfqq->cfqg);
bf7919371   Vivek Goyal   blkio: Set must_d...
3716
  				cfq_mark_cfqq_must_dispatch(cfqq);
a11cdaa7a   Divyesh Shah   block: Update to ...
3717
  			}
d6ceb25e8   Jens Axboe   cfq-iosched: don'...
3718
  		}
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3719
  	} else if (cfq_should_preempt(cfqd, cfqq, rq)) {
22e2c507c   Jens Axboe   [PATCH] Update cf...
3720
3721
3722
  		/*
  		 * not the active queue - expire current slice if it is
  		 * idle and has expired it's mean thinktime or this new queue
3a9a3f6cc   Divyesh Shah   cfq-iosched: Allo...
3723
3724
  		 * has some old slice time left and is of higher priority or
  		 * this new queue is RT and the current one is BE
22e2c507c   Jens Axboe   [PATCH] Update cf...
3725
3726
  		 */
  		cfq_preempt_queue(cfqd, cfqq);
24ecfbe27   Christoph Hellwig   block: add blk_ru...
3727
  		__blk_run_queue(cfqd->queue);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3728
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3729
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
3730
  static void cfq_insert_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3731
  {
b4878f245   Jens Axboe   [PATCH] 02/05: up...
3732
  	struct cfq_data *cfqd = q->elevator->elevator_data;
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3733
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3734

7b679138b   Jens Axboe   cfq-iosched: add ...
3735
  	cfq_log_cfqq(cfqd, cfqq, "insert_request");
abede6da2   Tejun Heo   cfq: pass around ...
3736
  	cfq_init_prio_data(cfqq, RQ_CIC(rq));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3737

8b4922d31   Jan Kara   block: Stop abusi...
3738
  	rq->fifo_time = jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)];
22e2c507c   Jens Axboe   [PATCH] Update cf...
3739
  	list_add_tail(&rq->queuelist, &cfqq->fifo);
aa6f6a3de   Corrado Zoccolo   cfq-iosched: prep...
3740
  	cfq_add_rq_rb(rq);
155fead9b   Tejun Heo   blkcg: move blkio...
3741
3742
  	cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group,
  				 rq->cmd_flags);
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3743
  	cfq_rq_enqueued(cfqd, cfqq, rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3744
  }
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3745
3746
3747
3748
3749
3750
  /*
   * Update hw_tag based on peak queue depth over 50 samples under
   * sufficient load.
   */
  static void cfq_update_hw_tag(struct cfq_data *cfqd)
  {
1a1238a7d   Shaohua Li   cfq-iosched: impr...
3751
  	struct cfq_queue *cfqq = cfqd->active_queue;
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3752
3753
  	if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
  		cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
e459dd08f   Corrado Zoccolo   cfq-iosched: fix ...
3754
3755
3756
  
  	if (cfqd->hw_tag == 1)
  		return;
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3757
3758
  
  	if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3759
  	    cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3760
  		return;
1a1238a7d   Shaohua Li   cfq-iosched: impr...
3761
3762
3763
3764
3765
3766
3767
  	/*
  	 * If active queue hasn't enough requests and can idle, cfq might not
  	 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
  	 * case
  	 */
  	if (cfqq && cfq_cfqq_idle_window(cfqq) &&
  	    cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3768
  	    CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
1a1238a7d   Shaohua Li   cfq-iosched: impr...
3769
  		return;
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3770
3771
  	if (cfqd->hw_tag_samples++ < 50)
  		return;
e459dd08f   Corrado Zoccolo   cfq-iosched: fix ...
3772
  	if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3773
3774
3775
  		cfqd->hw_tag = 1;
  	else
  		cfqd->hw_tag = 0;
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3776
  }
7667aa063   Vivek Goyal   cfq-iosched: Take...
3777
3778
  static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
c58698073   Tejun Heo   block, cfq: reorg...
3779
  	struct cfq_io_cq *cic = cfqd->active_cic;
7667aa063   Vivek Goyal   cfq-iosched: Take...
3780

02a8f01b5   Justin TerAvest   cfq-iosched: Don'...
3781
3782
3783
  	/* If the queue already has requests, don't wait */
  	if (!RB_EMPTY_ROOT(&cfqq->sort_list))
  		return false;
7667aa063   Vivek Goyal   cfq-iosched: Take...
3784
3785
3786
  	/* If there are other queues in the group, don't wait */
  	if (cfqq->cfqg->nr_cfqq > 1)
  		return false;
7700fc4f6   Shaohua Li   CFQ: add think ti...
3787
3788
3789
  	/* the only queue in the group, but think time is big */
  	if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
  		return false;
7667aa063   Vivek Goyal   cfq-iosched: Take...
3790
3791
3792
3793
  	if (cfq_slice_used(cfqq))
  		return true;
  
  	/* if slice left is less than think time, wait busy */
383cd7213   Shaohua Li   CFQ: move think t...
3794
3795
  	if (cic && sample_valid(cic->ttime.ttime_samples)
  	    && (cfqq->slice_end - jiffies < cic->ttime.ttime_mean))
7667aa063   Vivek Goyal   cfq-iosched: Take...
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
  		return true;
  
  	/*
  	 * If think times is less than a jiffy than ttime_mean=0 and above
  	 * will not be true. It might happen that slice has not expired yet
  	 * but will expire soon (4-5 ns) during select_queue(). To cover the
  	 * case where think time is less than a jiffy, mark the queue wait
  	 * busy if only 1 jiffy is left in the slice.
  	 */
  	if (cfqq->slice_end - jiffies == 1)
  		return true;
  
  	return false;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
3810
  static void cfq_completed_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3811
  {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3812
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
b4878f245   Jens Axboe   [PATCH] 02/05: up...
3813
  	struct cfq_data *cfqd = cfqq->cfqd;
5380a101d   Jens Axboe   [PATCH] cfq-iosch...
3814
  	const int sync = rq_is_sync(rq);
b4878f245   Jens Axboe   [PATCH] 02/05: up...
3815
  	unsigned long now;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3816

b4878f245   Jens Axboe   [PATCH] 02/05: up...
3817
  	now = jiffies;
33659ebba   Christoph Hellwig   block: remove wra...
3818
3819
  	cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d",
  		     !!(rq->cmd_flags & REQ_NOIDLE));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3820

45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3821
  	cfq_update_hw_tag(cfqd);
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3822
  	WARN_ON(!cfqd->rq_in_driver);
6d048f531   Jens Axboe   cfq-iosched: deve...
3823
  	WARN_ON(!cfqq->dispatched);
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3824
  	cfqd->rq_in_driver--;
6d048f531   Jens Axboe   cfq-iosched: deve...
3825
  	cfqq->dispatched--;
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
3826
  	(RQ_CFQG(rq))->dispatched--;
155fead9b   Tejun Heo   blkcg: move blkio...
3827
3828
  	cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq),
  				     rq_io_start_time_ns(rq), rq->cmd_flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3829

53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3830
  	cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
3ed9a2965   Jens Axboe   cfq-iosched: impr...
3831

365722bb9   Vivek Goyal   cfq-iosched: dela...
3832
  	if (sync) {
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
3833
  		struct cfq_rb_root *st;
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
3834

383cd7213   Shaohua Li   CFQ: move think t...
3835
  		RQ_CIC(rq)->ttime.last_end_request = now;
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
3836
3837
  
  		if (cfq_cfqq_on_rr(cfqq))
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
3838
  			st = cfqq->service_tree;
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
3839
  		else
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
3840
3841
3842
3843
  			st = st_for(cfqq->cfqg, cfqq_class(cfqq),
  					cfqq_type(cfqq));
  
  		st->ttime.last_end_request = now;
573412b29   Corrado Zoccolo   cfq-iosched: redu...
3844
3845
  		if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
  			cfqd->last_delayed_sync = now;
365722bb9   Vivek Goyal   cfq-iosched: dela...
3846
  	}
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
3847

7700fc4f6   Shaohua Li   CFQ: add think ti...
3848
3849
3850
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
  	cfqq->cfqg->ttime.last_end_request = now;
  #endif
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
3851
3852
3853
3854
3855
  	/*
  	 * If this is the active queue, check if it needs to be expired,
  	 * or if we want to idle in case it has no pending requests.
  	 */
  	if (cfqd->active_queue == cfqq) {
a36e71f99   Jens Axboe   cfq-iosched: add ...
3856
  		const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
44f7c1606   Jens Axboe   cfq-iosched: defe...
3857
3858
3859
3860
  		if (cfq_cfqq_slice_new(cfqq)) {
  			cfq_set_prio_slice(cfqd, cfqq);
  			cfq_clear_cfqq_slice_new(cfqq);
  		}
f75edf2dc   Vivek Goyal   blkio: Wait for c...
3861
3862
  
  		/*
7667aa063   Vivek Goyal   cfq-iosched: Take...
3863
3864
  		 * Should we wait for next request to come in before we expire
  		 * the queue.
f75edf2dc   Vivek Goyal   blkio: Wait for c...
3865
  		 */
7667aa063   Vivek Goyal   cfq-iosched: Take...
3866
  		if (cfq_should_wait_busy(cfqd, cfqq)) {
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
3867
3868
3869
3870
  			unsigned long extend_sl = cfqd->cfq_slice_idle;
  			if (!cfqd->cfq_slice_idle)
  				extend_sl = cfqd->cfq_group_idle;
  			cfqq->slice_end = jiffies + extend_sl;
f75edf2dc   Vivek Goyal   blkio: Wait for c...
3871
  			cfq_mark_cfqq_wait_busy(cfqq);
b1ffe737f   Divyesh Shah   cfq-iosched: Add ...
3872
  			cfq_log_cfqq(cfqd, cfqq, "will busy wait");
f75edf2dc   Vivek Goyal   blkio: Wait for c...
3873
  		}
a36e71f99   Jens Axboe   cfq-iosched: add ...
3874
  		/*
8e550632c   Corrado Zoccolo   cfq-iosched: fix ...
3875
3876
3877
3878
3879
3880
  		 * Idling is not enabled on:
  		 * - expired queues
  		 * - idle-priority queues
  		 * - async queues
  		 * - queues with still some requests queued
  		 * - when there is a close cooperator
a36e71f99   Jens Axboe   cfq-iosched: add ...
3881
  		 */
0871714e0   Jens Axboe   cfq-iosched: rela...
3882
  		if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
3883
  			cfq_slice_expired(cfqd, 1);
8e550632c   Corrado Zoccolo   cfq-iosched: fix ...
3884
3885
  		else if (sync && cfqq_empty &&
  			 !cfq_close_cooperator(cfqd, cfqq)) {
749ef9f84   Corrado Zoccolo   cfq: improve fsyn...
3886
  			cfq_arm_slice_timer(cfqd);
8e550632c   Corrado Zoccolo   cfq-iosched: fix ...
3887
  		}
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
3888
  	}
6d048f531   Jens Axboe   cfq-iosched: deve...
3889

53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3890
  	if (!cfqd->rq_in_driver)
23e018a1b   Jens Axboe   block: get rid of...
3891
  		cfq_schedule_dispatch(cfqd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3892
  }
89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
3893
  static inline int __cfq_may_queue(struct cfq_queue *cfqq)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3894
  {
1b379d8da   Jens Axboe   cfq-iosched: get ...
3895
  	if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
3896
  		cfq_mark_cfqq_must_alloc_slice(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3897
  		return ELV_MQUEUE_MUST;
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
3898
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3899

22e2c507c   Jens Axboe   [PATCH] Update cf...
3900
  	return ELV_MQUEUE_MAY;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3901
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
3902
  static int cfq_may_queue(struct request_queue *q, int rw)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3903
3904
3905
  {
  	struct cfq_data *cfqd = q->elevator->elevator_data;
  	struct task_struct *tsk = current;
c58698073   Tejun Heo   block, cfq: reorg...
3906
  	struct cfq_io_cq *cic;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3907
3908
3909
3910
3911
3912
3913
3914
  	struct cfq_queue *cfqq;
  
  	/*
  	 * don't force setup of a queue from here, as a call to may_queue
  	 * does not necessarily imply that a request actually will be queued.
  	 * so just lookup a possibly existing queue, or return 'may queue'
  	 * if that fails
  	 */
4ac845a2e   Jens Axboe   block: cfq: make ...
3915
  	cic = cfq_cic_lookup(cfqd, tsk->io_context);
91fac317a   Vasily Tarasov   cfq-iosched: get ...
3916
3917
  	if (!cic)
  		return ELV_MQUEUE_MAY;
b0b78f81a   Jens Axboe   cfq-iosched: use ...
3918
  	cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
22e2c507c   Jens Axboe   [PATCH] Update cf...
3919
  	if (cfqq) {
abede6da2   Tejun Heo   cfq: pass around ...
3920
  		cfq_init_prio_data(cfqq, cic);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3921

89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
3922
  		return __cfq_may_queue(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3923
3924
3925
  	}
  
  	return ELV_MQUEUE_MAY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3926
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3927
3928
3929
  /*
   * queue lock held here
   */
bb37b94c6   Jens Axboe   [BLOCK] Cleanup u...
3930
  static void cfq_put_request(struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3931
  {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3932
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3933

5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3934
  	if (cfqq) {
22e2c507c   Jens Axboe   [PATCH] Update cf...
3935
  		const int rw = rq_data_dir(rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3936

22e2c507c   Jens Axboe   [PATCH] Update cf...
3937
3938
  		BUG_ON(!cfqq->allocated[rw]);
  		cfqq->allocated[rw]--;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3939

7f1dc8a2d   Vivek Goyal   blkio: Fix blkio ...
3940
  		/* Put down rq reference on cfqg */
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
3941
  		cfqg_put(RQ_CFQG(rq));
a612fddf0   Tejun Heo   block, cfq: move ...
3942
3943
  		rq->elv.priv[0] = NULL;
  		rq->elv.priv[1] = NULL;
7f1dc8a2d   Vivek Goyal   blkio: Fix blkio ...
3944

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3945
3946
3947
  		cfq_put_queue(cfqq);
  	}
  }
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
3948
  static struct cfq_queue *
c58698073   Tejun Heo   block, cfq: reorg...
3949
  cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
3950
3951
3952
3953
  		struct cfq_queue *cfqq)
  {
  	cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
  	cic_set_cfqq(cic, cfqq->new_cfqq, 1);
b3b6d0408   Jeff Moyer   cfq: change the m...
3954
  	cfq_mark_cfqq_coop(cfqq->new_cfqq);
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
3955
3956
3957
  	cfq_put_queue(cfqq);
  	return cic_to_cfqq(cic, 1);
  }
e6c5bc737   Jeff Moyer   cfq: break apart ...
3958
3959
3960
3961
3962
  /*
   * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
   * was the last process referring to said cfqq.
   */
  static struct cfq_queue *
c58698073   Tejun Heo   block, cfq: reorg...
3963
  split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
e6c5bc737   Jeff Moyer   cfq: break apart ...
3964
3965
  {
  	if (cfqq_process_refs(cfqq) == 1) {
e6c5bc737   Jeff Moyer   cfq: break apart ...
3966
3967
  		cfqq->pid = current->pid;
  		cfq_clear_cfqq_coop(cfqq);
ae54abed6   Shaohua Li   cfq-iosched: spli...
3968
  		cfq_clear_cfqq_split_coop(cfqq);
e6c5bc737   Jeff Moyer   cfq: break apart ...
3969
3970
3971
3972
  		return cfqq;
  	}
  
  	cic_set_cfqq(cic, NULL, 1);
d02a2c077   Shaohua Li   cfq-iosched: fix ...
3973
3974
  
  	cfq_put_cooperator(cfqq);
e6c5bc737   Jeff Moyer   cfq: break apart ...
3975
3976
3977
  	cfq_put_queue(cfqq);
  	return NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3978
  /*
22e2c507c   Jens Axboe   [PATCH] Update cf...
3979
   * Allocate cfq data structures associated with this request.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3980
   */
22e2c507c   Jens Axboe   [PATCH] Update cf...
3981
  static int
852c788f8   Tejun Heo   block: implement ...
3982
3983
  cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
  		gfp_t gfp_mask)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3984
3985
  {
  	struct cfq_data *cfqd = q->elevator->elevator_data;
f1f8cc946   Tejun Heo   block, cfq: move ...
3986
  	struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3987
  	const int rw = rq_data_dir(rq);
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3988
  	const bool is_sync = rq_is_sync(rq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3989
  	struct cfq_queue *cfqq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3990
3991
  
  	might_sleep_if(gfp_mask & __GFP_WAIT);
216284c35   Tejun Heo   block, cfq: fix r...
3992
  	spin_lock_irq(q->queue_lock);
f1f8cc946   Tejun Heo   block, cfq: move ...
3993

598971bfb   Tejun Heo   cfq: don't use ic...
3994
3995
  	check_ioprio_changed(cic, bio);
  	check_blkcg_changed(cic, bio);
e6c5bc737   Jeff Moyer   cfq: break apart ...
3996
  new_queue:
91fac317a   Vasily Tarasov   cfq-iosched: get ...
3997
  	cfqq = cic_to_cfqq(cic, is_sync);
32f2e807a   Vivek Goyal   cfq-iosched: rese...
3998
  	if (!cfqq || cfqq == &cfqd->oom_cfqq) {
abede6da2   Tejun Heo   cfq: pass around ...
3999
  		cfqq = cfq_get_queue(cfqd, is_sync, cic, bio, gfp_mask);
91fac317a   Vasily Tarasov   cfq-iosched: get ...
4000
  		cic_set_cfqq(cic, cfqq, is_sync);
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
4001
4002
  	} else {
  		/*
e6c5bc737   Jeff Moyer   cfq: break apart ...
4003
4004
  		 * If the queue was seeky for too long, break it apart.
  		 */
ae54abed6   Shaohua Li   cfq-iosched: spli...
4005
  		if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
e6c5bc737   Jeff Moyer   cfq: break apart ...
4006
4007
4008
4009
4010
4011
4012
  			cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
  			cfqq = split_cfqq(cic, cfqq);
  			if (!cfqq)
  				goto new_queue;
  		}
  
  		/*
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
4013
4014
4015
4016
4017
4018
4019
  		 * Check to see if this queue is scheduled to merge with
  		 * another, closely cooperating queue.  The merging of
  		 * queues happens here as it must be done in process context.
  		 * The reference on new_cfqq was taken in merge_cfqqs.
  		 */
  		if (cfqq->new_cfqq)
  			cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
91fac317a   Vasily Tarasov   cfq-iosched: get ...
4020
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4021
4022
  
  	cfqq->allocated[rw]++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4023

6fae9c251   Jens Axboe   Merge commit 'v2....
4024
  	cfqq->ref++;
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
4025
  	cfqg_get(cfqq->cfqg);
a612fddf0   Tejun Heo   block, cfq: move ...
4026
  	rq->elv.priv[0] = cfqq;
1adaf3dde   Tejun Heo   blkcg: move refcn...
4027
  	rq->elv.priv[1] = cfqq->cfqg;
216284c35   Tejun Heo   block, cfq: fix r...
4028
  	spin_unlock_irq(q->queue_lock);
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
4029
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4030
  }
65f27f384   David Howells   WorkStruct: Pass ...
4031
  static void cfq_kick_queue(struct work_struct *work)
22e2c507c   Jens Axboe   [PATCH] Update cf...
4032
  {
65f27f384   David Howells   WorkStruct: Pass ...
4033
  	struct cfq_data *cfqd =
23e018a1b   Jens Axboe   block: get rid of...
4034
  		container_of(work, struct cfq_data, unplug_work);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
4035
  	struct request_queue *q = cfqd->queue;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4036

40bb54d19   Jens Axboe   cfq-iosched: no n...
4037
  	spin_lock_irq(q->queue_lock);
24ecfbe27   Christoph Hellwig   block: add blk_ru...
4038
  	__blk_run_queue(cfqd->queue);
40bb54d19   Jens Axboe   cfq-iosched: no n...
4039
  	spin_unlock_irq(q->queue_lock);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
  }
  
  /*
   * Timer running if the active_queue is currently idling inside its time slice
   */
  static void cfq_idle_slice_timer(unsigned long data)
  {
  	struct cfq_data *cfqd = (struct cfq_data *) data;
  	struct cfq_queue *cfqq;
  	unsigned long flags;
3c6bd2f87   Jens Axboe   cfq-iosched: chec...
4050
  	int timed_out = 1;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4051

7b679138b   Jens Axboe   cfq-iosched: add ...
4052
  	cfq_log(cfqd, "idle timer fired");
22e2c507c   Jens Axboe   [PATCH] Update cf...
4053
  	spin_lock_irqsave(cfqd->queue->queue_lock, flags);
fe094d98e   Jens Axboe   cfq-iosched: make...
4054
4055
  	cfqq = cfqd->active_queue;
  	if (cfqq) {
3c6bd2f87   Jens Axboe   cfq-iosched: chec...
4056
  		timed_out = 0;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4057
  		/*
b029195dd   Jens Axboe   cfq-iosched: don'...
4058
4059
4060
4061
4062
4063
  		 * We saw a request before the queue expired, let it through
  		 */
  		if (cfq_cfqq_must_dispatch(cfqq))
  			goto out_kick;
  
  		/*
22e2c507c   Jens Axboe   [PATCH] Update cf...
4064
4065
  		 * expired
  		 */
44f7c1606   Jens Axboe   cfq-iosched: defe...
4066
  		if (cfq_slice_used(cfqq))
22e2c507c   Jens Axboe   [PATCH] Update cf...
4067
4068
4069
4070
4071
4072
  			goto expire;
  
  		/*
  		 * only expire and reinvoke request handler, if there are
  		 * other queues with pending requests
  		 */
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
4073
  		if (!cfqd->busy_queues)
22e2c507c   Jens Axboe   [PATCH] Update cf...
4074
  			goto out_cont;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4075
4076
4077
4078
  
  		/*
  		 * not expired and it has a request pending, let it dispatch
  		 */
75e50984f   Jens Axboe   cfq-iosched: kill...
4079
  		if (!RB_EMPTY_ROOT(&cfqq->sort_list))
22e2c507c   Jens Axboe   [PATCH] Update cf...
4080
  			goto out_kick;
76280aff1   Corrado Zoccolo   cfq-iosched: idli...
4081
4082
4083
4084
4085
  
  		/*
  		 * Queue depth flag is reset only when the idle didn't succeed
  		 */
  		cfq_clear_cfqq_deep(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4086
4087
  	}
  expire:
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
4088
  	cfq_slice_expired(cfqd, timed_out);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4089
  out_kick:
23e018a1b   Jens Axboe   block: get rid of...
4090
  	cfq_schedule_dispatch(cfqd);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4091
4092
4093
  out_cont:
  	spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  }
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
4094
4095
4096
  static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
  {
  	del_timer_sync(&cfqd->idle_slice_timer);
23e018a1b   Jens Axboe   block: get rid of...
4097
  	cancel_work_sync(&cfqd->unplug_work);
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
4098
  }
22e2c507c   Jens Axboe   [PATCH] Update cf...
4099

c2dea2d1f   Vasily Tarasov   cfq: async queue ...
4100
4101
4102
4103
4104
4105
4106
4107
4108
  static void cfq_put_async_queues(struct cfq_data *cfqd)
  {
  	int i;
  
  	for (i = 0; i < IOPRIO_BE_NR; i++) {
  		if (cfqd->async_cfqq[0][i])
  			cfq_put_queue(cfqd->async_cfqq[0][i]);
  		if (cfqd->async_cfqq[1][i])
  			cfq_put_queue(cfqd->async_cfqq[1][i]);
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
4109
  	}
2389d1ef1   Oleg Nesterov   cfq: fix IOPRIO_C...
4110
4111
4112
  
  	if (cfqd->async_idle_cfqq)
  		cfq_put_queue(cfqd->async_idle_cfqq);
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
4113
  }
b374d18a4   Jens Axboe   block: get rid of...
4114
  static void cfq_exit_queue(struct elevator_queue *e)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4115
  {
22e2c507c   Jens Axboe   [PATCH] Update cf...
4116
  	struct cfq_data *cfqd = e->elevator_data;
165125e1e   Jens Axboe   [BLOCK] Get rid o...
4117
  	struct request_queue *q = cfqd->queue;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4118

3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
4119
  	cfq_shutdown_timer_wq(cfqd);
e2d74ac06   Jens Axboe   [PATCH] [BLOCK] c...
4120

d9ff41879   Al Viro   [PATCH] make cfq_...
4121
  	spin_lock_irq(q->queue_lock);
e2d74ac06   Jens Axboe   [PATCH] [BLOCK] c...
4122

d9ff41879   Al Viro   [PATCH] make cfq_...
4123
  	if (cfqd->active_queue)
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
4124
  		__cfq_slice_expired(cfqd, cfqd->active_queue, 0);
e2d74ac06   Jens Axboe   [PATCH] [BLOCK] c...
4125

c2dea2d1f   Vasily Tarasov   cfq: async queue ...
4126
  	cfq_put_async_queues(cfqd);
03aa264ac   Tejun Heo   blkcg: let blkcg ...
4127
4128
  
  	spin_unlock_irq(q->queue_lock);
a90d742e4   Al Viro   [PATCH] don't bot...
4129
  	cfq_shutdown_timer_wq(cfqd);
ffea73fc7   Tejun Heo   block: blkcg_poli...
4130
4131
4132
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
  	blkcg_deactivate_policy(q, &blkcg_policy_cfq);
  #else
f51b802c1   Tejun Heo   blkcg: use the us...
4133
  	kfree(cfqd->root_group);
2abae55f5   Vivek Goyal   cfq-iosched: Fix ...
4134
  #endif
56edf7d75   Vivek Goyal   cfq-iosched: Fix ...
4135
  	kfree(cfqd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4136
  }
d50235b7b   Jianpeng Ma   elevator: Fix a r...
4137
  static int cfq_init_queue(struct request_queue *q, struct elevator_type *e)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4138
4139
  {
  	struct cfq_data *cfqd;
3c798398e   Tejun Heo   blkcg: mass renam...
4140
  	struct blkcg_gq *blkg __maybe_unused;
a2b1693ba   Tejun Heo   blkcg: implement ...
4141
  	int i, ret;
d50235b7b   Jianpeng Ma   elevator: Fix a r...
4142
4143
4144
4145
4146
  	struct elevator_queue *eq;
  
  	eq = elevator_alloc(q, e);
  	if (!eq)
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4147

c1b511eb2   Joe Perches   block: Convert km...
4148
  	cfqd = kzalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
d50235b7b   Jianpeng Ma   elevator: Fix a r...
4149
4150
  	if (!cfqd) {
  		kobject_put(&eq->kobj);
b2fab5acd   Tejun Heo   elevator: make el...
4151
  		return -ENOMEM;
d50235b7b   Jianpeng Ma   elevator: Fix a r...
4152
4153
  	}
  	eq->elevator_data = cfqd;
80b15c738   Konstantin Khlebnikov   cfq-iosched: comp...
4154

f51b802c1   Tejun Heo   blkcg: use the us...
4155
  	cfqd->queue = q;
d50235b7b   Jianpeng Ma   elevator: Fix a r...
4156
4157
4158
  	spin_lock_irq(q->queue_lock);
  	q->elevator = eq;
  	spin_unlock_irq(q->queue_lock);
f51b802c1   Tejun Heo   blkcg: use the us...
4159

1fa8f6d68   Vivek Goyal   blkio: Introduce ...
4160
4161
  	/* Init root service tree */
  	cfqd->grp_service_tree = CFQ_RB_ROOT;
f51b802c1   Tejun Heo   blkcg: use the us...
4162
  	/* Init root group and prefer root group over other groups by default */
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
4163
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398e   Tejun Heo   blkcg: mass renam...
4164
  	ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
a2b1693ba   Tejun Heo   blkcg: implement ...
4165
4166
  	if (ret)
  		goto out_free;
f51b802c1   Tejun Heo   blkcg: use the us...
4167

a2b1693ba   Tejun Heo   blkcg: implement ...
4168
  	cfqd->root_group = blkg_to_cfqg(q->root_blkg);
f51b802c1   Tejun Heo   blkcg: use the us...
4169
  #else
a2b1693ba   Tejun Heo   blkcg: implement ...
4170
  	ret = -ENOMEM;
f51b802c1   Tejun Heo   blkcg: use the us...
4171
4172
  	cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
  					GFP_KERNEL, cfqd->queue->node);
a2b1693ba   Tejun Heo   blkcg: implement ...
4173
4174
  	if (!cfqd->root_group)
  		goto out_free;
5624a4e44   Vivek Goyal   blk-throttle: Mak...
4175

a2b1693ba   Tejun Heo   blkcg: implement ...
4176
4177
  	cfq_init_cfqg_base(cfqd->root_group);
  #endif
3381cb8d2   Tejun Heo   blkcg: move blkio...
4178
  	cfqd->root_group->weight = 2 * CFQ_WEIGHT_DEFAULT;
e71357e11   Tejun Heo   cfq-iosched: add ...
4179
  	cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_DEFAULT;
5624a4e44   Vivek Goyal   blk-throttle: Mak...
4180

26a2ac009   Jens Axboe   cfq-iosched: clea...
4181
4182
4183
4184
4185
4186
4187
  	/*
  	 * Not strictly needed (since RB_ROOT just clears the node and we
  	 * zeroed cfqd on alloc), but better be safe in case someone decides
  	 * to add magic to the rb code
  	 */
  	for (i = 0; i < CFQ_PRIO_LISTS; i++)
  		cfqd->prio_trees[i] = RB_ROOT;
6118b70b3   Jens Axboe   cfq-iosched: get ...
4188
4189
4190
  	/*
  	 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
  	 * Grab a permanent reference to it, so that the normal code flow
f51b802c1   Tejun Heo   blkcg: use the us...
4191
4192
4193
  	 * will not attempt to free it.  oom_cfqq is linked to root_group
  	 * but shouldn't hold a reference as it'll never be unlinked.  Lose
  	 * the reference from linking right away.
6118b70b3   Jens Axboe   cfq-iosched: get ...
4194
4195
  	 */
  	cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
30d7b9448   Shaohua Li   block cfq: don't ...
4196
  	cfqd->oom_cfqq.ref++;
1adaf3dde   Tejun Heo   blkcg: move refcn...
4197
4198
  
  	spin_lock_irq(q->queue_lock);
f51b802c1   Tejun Heo   blkcg: use the us...
4199
  	cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
4200
  	cfqg_put(cfqd->root_group);
1adaf3dde   Tejun Heo   blkcg: move refcn...
4201
  	spin_unlock_irq(q->queue_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4202

22e2c507c   Jens Axboe   [PATCH] Update cf...
4203
4204
4205
  	init_timer(&cfqd->idle_slice_timer);
  	cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
  	cfqd->idle_slice_timer.data = (unsigned long) cfqd;
23e018a1b   Jens Axboe   block: get rid of...
4206
  	INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4207

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4208
  	cfqd->cfq_quantum = cfq_quantum;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4209
4210
  	cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
  	cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4211
4212
  	cfqd->cfq_back_max = cfq_back_max;
  	cfqd->cfq_back_penalty = cfq_back_penalty;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4213
4214
  	cfqd->cfq_slice[0] = cfq_slice_async;
  	cfqd->cfq_slice[1] = cfq_slice_sync;
5bf14c072   Tao Ma   block: Make cfq_t...
4215
  	cfqd->cfq_target_latency = cfq_target_latency;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4216
4217
  	cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
  	cfqd->cfq_slice_idle = cfq_slice_idle;
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
4218
  	cfqd->cfq_group_idle = cfq_group_idle;
963b72fc6   Jens Axboe   cfq-iosched: rena...
4219
  	cfqd->cfq_latency = 1;
e459dd08f   Corrado Zoccolo   cfq-iosched: fix ...
4220
  	cfqd->hw_tag = -1;
edc71131c   Corrado Zoccolo   cfq-iosched: comm...
4221
4222
4223
4224
  	/*
  	 * we optimistically start assuming sync ops weren't delayed in last
  	 * second, in order to have larger depth for async operations.
  	 */
573412b29   Corrado Zoccolo   cfq-iosched: redu...
4225
  	cfqd->last_delayed_sync = jiffies - HZ;
b2fab5acd   Tejun Heo   elevator: make el...
4226
  	return 0;
a2b1693ba   Tejun Heo   blkcg: implement ...
4227
4228
4229
  
  out_free:
  	kfree(cfqd);
d50235b7b   Jianpeng Ma   elevator: Fix a r...
4230
  	kobject_put(&eq->kobj);
a2b1693ba   Tejun Heo   blkcg: implement ...
4231
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4232
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4233
4234
4235
  /*
   * sysfs parts below -->
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4236
4237
4238
  static ssize_t
  cfq_var_show(unsigned int var, char *page)
  {
176167ad9   Masanari Iida   block: Fix format...
4239
4240
  	return sprintf(page, "%u
  ", var);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
  }
  
  static ssize_t
  cfq_var_store(unsigned int *var, const char *page, size_t count)
  {
  	char *p = (char *) page;
  
  	*var = simple_strtoul(p, &p, 10);
  	return count;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4251
  #define SHOW_FUNCTION(__FUNC, __VAR, __CONV)				\
b374d18a4   Jens Axboe   block: get rid of...
4252
  static ssize_t __FUNC(struct elevator_queue *e, char *page)		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4253
  {									\
3d1ab40f4   Al Viro   [PATCH] elevator_...
4254
  	struct cfq_data *cfqd = e->elevator_data;			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4255
4256
4257
4258
4259
4260
  	unsigned int __data = __VAR;					\
  	if (__CONV)							\
  		__data = jiffies_to_msecs(__data);			\
  	return cfq_var_show(__data, (page));				\
  }
  SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4261
4262
  SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
  SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
e572ec7e4   Al Viro   [PATCH] fix rmmod...
4263
4264
  SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
  SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4265
  SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
4266
  SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4267
4268
4269
  SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
  SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
  SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
963b72fc6   Jens Axboe   cfq-iosched: rena...
4270
  SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
5bf14c072   Tao Ma   block: Make cfq_t...
4271
  SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4272
4273
4274
  #undef SHOW_FUNCTION
  
  #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV)			\
b374d18a4   Jens Axboe   block: get rid of...
4275
  static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count)	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4276
  {									\
3d1ab40f4   Al Viro   [PATCH] elevator_...
4277
  	struct cfq_data *cfqd = e->elevator_data;			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
  	unsigned int __data;						\
  	int ret = cfq_var_store(&__data, (page), count);		\
  	if (__data < (MIN))						\
  		__data = (MIN);						\
  	else if (__data > (MAX))					\
  		__data = (MAX);						\
  	if (__CONV)							\
  		*(__PTR) = msecs_to_jiffies(__data);			\
  	else								\
  		*(__PTR) = __data;					\
  	return ret;							\
  }
  STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
fe094d98e   Jens Axboe   cfq-iosched: make...
4291
4292
4293
4294
  STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
  		UINT_MAX, 1);
  STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
  		UINT_MAX, 1);
e572ec7e4   Al Viro   [PATCH] fix rmmod...
4295
  STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
fe094d98e   Jens Axboe   cfq-iosched: make...
4296
4297
  STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
  		UINT_MAX, 0);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4298
  STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
4299
  STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4300
4301
  STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
  STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
fe094d98e   Jens Axboe   cfq-iosched: make...
4302
4303
  STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
  		UINT_MAX, 0);
963b72fc6   Jens Axboe   cfq-iosched: rena...
4304
  STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
5bf14c072   Tao Ma   block: Make cfq_t...
4305
  STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4306
  #undef STORE_FUNCTION
e572ec7e4   Al Viro   [PATCH] fix rmmod...
4307
4308
4309
4310
4311
  #define CFQ_ATTR(name) \
  	__ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
  
  static struct elv_fs_entry cfq_attrs[] = {
  	CFQ_ATTR(quantum),
e572ec7e4   Al Viro   [PATCH] fix rmmod...
4312
4313
4314
4315
4316
4317
4318
4319
  	CFQ_ATTR(fifo_expire_sync),
  	CFQ_ATTR(fifo_expire_async),
  	CFQ_ATTR(back_seek_max),
  	CFQ_ATTR(back_seek_penalty),
  	CFQ_ATTR(slice_sync),
  	CFQ_ATTR(slice_async),
  	CFQ_ATTR(slice_async_rq),
  	CFQ_ATTR(slice_idle),
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
4320
  	CFQ_ATTR(group_idle),
963b72fc6   Jens Axboe   cfq-iosched: rena...
4321
  	CFQ_ATTR(low_latency),
5bf14c072   Tao Ma   block: Make cfq_t...
4322
  	CFQ_ATTR(target_latency),
e572ec7e4   Al Viro   [PATCH] fix rmmod...
4323
  	__ATTR_NULL
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4324
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4325
4326
4327
4328
4329
  static struct elevator_type iosched_cfq = {
  	.ops = {
  		.elevator_merge_fn = 		cfq_merge,
  		.elevator_merged_fn =		cfq_merged_request,
  		.elevator_merge_req_fn =	cfq_merged_requests,
da7752650   Jens Axboe   [PATCH] cfq-iosch...
4330
  		.elevator_allow_merge_fn =	cfq_allow_merge,
812d40264   Divyesh Shah   blkio: Add io_mer...
4331
  		.elevator_bio_merged_fn =	cfq_bio_merged,
b4878f245   Jens Axboe   [PATCH] 02/05: up...
4332
  		.elevator_dispatch_fn =		cfq_dispatch_requests,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4333
  		.elevator_add_req_fn =		cfq_insert_request,
b4878f245   Jens Axboe   [PATCH] 02/05: up...
4334
  		.elevator_activate_req_fn =	cfq_activate_request,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4335
  		.elevator_deactivate_req_fn =	cfq_deactivate_request,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4336
  		.elevator_completed_req_fn =	cfq_completed_request,
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
4337
4338
  		.elevator_former_req_fn =	elv_rb_former_request,
  		.elevator_latter_req_fn =	elv_rb_latter_request,
9b84cacd0   Tejun Heo   block, cfq: restr...
4339
  		.elevator_init_icq_fn =		cfq_init_icq,
7e5a87944   Tejun Heo   block, cfq: move ...
4340
  		.elevator_exit_icq_fn =		cfq_exit_icq,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4341
4342
4343
4344
4345
4346
  		.elevator_set_req_fn =		cfq_set_request,
  		.elevator_put_req_fn =		cfq_put_request,
  		.elevator_may_queue_fn =	cfq_may_queue,
  		.elevator_init_fn =		cfq_init_queue,
  		.elevator_exit_fn =		cfq_exit_queue,
  	},
3d3c2379f   Tejun Heo   block, cfq: move ...
4347
4348
  	.icq_size	=	sizeof(struct cfq_io_cq),
  	.icq_align	=	__alignof__(struct cfq_io_cq),
3d1ab40f4   Al Viro   [PATCH] elevator_...
4349
  	.elevator_attrs =	cfq_attrs,
3d3c2379f   Tejun Heo   block, cfq: move ...
4350
  	.elevator_name	=	"cfq",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4351
4352
  	.elevator_owner =	THIS_MODULE,
  };
3e2520668   Vivek Goyal   blkio: Implement ...
4353
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398e   Tejun Heo   blkcg: mass renam...
4354
  static struct blkcg_policy blkcg_policy_cfq = {
f9fcc2d39   Tejun Heo   blkcg: collapse b...
4355
4356
4357
4358
  	.pd_size		= sizeof(struct cfq_group),
  	.cftypes		= cfq_blkcg_files,
  
  	.pd_init_fn		= cfq_pd_init,
0b39920b5   Tejun Heo   cfq-iosched: coll...
4359
  	.pd_offline_fn		= cfq_pd_offline,
f9fcc2d39   Tejun Heo   blkcg: collapse b...
4360
  	.pd_reset_stats_fn	= cfq_pd_reset_stats,
3e2520668   Vivek Goyal   blkio: Implement ...
4361
  };
3e2520668   Vivek Goyal   blkio: Implement ...
4362
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4363
4364
  static int __init cfq_init(void)
  {
3d3c2379f   Tejun Heo   block, cfq: move ...
4365
  	int ret;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4366
4367
4368
4369
4370
4371
4372
  	/*
  	 * could be 0 on HZ < 1000 setups
  	 */
  	if (!cfq_slice_async)
  		cfq_slice_async = 1;
  	if (!cfq_slice_idle)
  		cfq_slice_idle = 1;
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
4373
4374
4375
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
  	if (!cfq_group_idle)
  		cfq_group_idle = 1;
8bd435b30   Tejun Heo   blkcg: remove sta...
4376

3c798398e   Tejun Heo   blkcg: mass renam...
4377
  	ret = blkcg_policy_register(&blkcg_policy_cfq);
8bd435b30   Tejun Heo   blkcg: remove sta...
4378
4379
  	if (ret)
  		return ret;
ffea73fc7   Tejun Heo   block: blkcg_poli...
4380
4381
4382
  #else
  	cfq_group_idle = 0;
  #endif
8bd435b30   Tejun Heo   blkcg: remove sta...
4383

fd7949564   Tejun Heo   block: fix return...
4384
  	ret = -ENOMEM;
3d3c2379f   Tejun Heo   block, cfq: move ...
4385
4386
  	cfq_pool = KMEM_CACHE(cfq_queue, 0);
  	if (!cfq_pool)
8bd435b30   Tejun Heo   blkcg: remove sta...
4387
  		goto err_pol_unreg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4388

3d3c2379f   Tejun Heo   block, cfq: move ...
4389
  	ret = elv_register(&iosched_cfq);
8bd435b30   Tejun Heo   blkcg: remove sta...
4390
4391
  	if (ret)
  		goto err_free_pool;
3d3c2379f   Tejun Heo   block, cfq: move ...
4392

2fdd82bd8   Adrian Bunk   block: let elv_re...
4393
  	return 0;
8bd435b30   Tejun Heo   blkcg: remove sta...
4394
4395
4396
4397
  
  err_free_pool:
  	kmem_cache_destroy(cfq_pool);
  err_pol_unreg:
ffea73fc7   Tejun Heo   block: blkcg_poli...
4398
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398e   Tejun Heo   blkcg: mass renam...
4399
  	blkcg_policy_unregister(&blkcg_policy_cfq);
ffea73fc7   Tejun Heo   block: blkcg_poli...
4400
  #endif
8bd435b30   Tejun Heo   blkcg: remove sta...
4401
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4402
4403
4404
4405
  }
  
  static void __exit cfq_exit(void)
  {
ffea73fc7   Tejun Heo   block: blkcg_poli...
4406
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398e   Tejun Heo   blkcg: mass renam...
4407
  	blkcg_policy_unregister(&blkcg_policy_cfq);
ffea73fc7   Tejun Heo   block: blkcg_poli...
4408
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4409
  	elv_unregister(&iosched_cfq);
3d3c2379f   Tejun Heo   block, cfq: move ...
4410
  	kmem_cache_destroy(cfq_pool);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4411
4412
4413
4414
4415
4416
4417
4418
  }
  
  module_init(cfq_init);
  module_exit(cfq_exit);
  
  MODULE_AUTHOR("Jens Axboe");
  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");