Blame view

block/cfq-iosched.c 127 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>
e60175710   Ingo Molnar   sched/headers: Pr...
11
  #include <linux/sched/clock.h>
1cc9be68e   Al Viro   [PATCH] noise rem...
12
13
  #include <linux/blkdev.h>
  #include <linux/elevator.h>
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
14
  #include <linux/ktime.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
  #include <linux/rbtree.h>
22e2c507c   Jens Axboe   [PATCH] Update cf...
16
  #include <linux/ioprio.h>
7b679138b   Jens Axboe   cfq-iosched: add ...
17
  #include <linux/blktrace_api.h>
eea8f41cc   Tejun Heo   blkcg: move block...
18
  #include <linux/blk-cgroup.h>
6e736be7f   Tejun Heo   block: make ioc g...
19
  #include "blk.h"
87760e5ee   Jens Axboe   block: hook up wr...
20
  #include "blk-wbt.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
23
24
  
  /*
   * tunables
   */
fe094d98e   Jens Axboe   cfq-iosched: make...
25
  /* max queue in one round of service */
abc3c744d   Shaohua Li   cfq-iosched: quan...
26
  static const int cfq_quantum = 8;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
27
  static const u64 cfq_fifo_expire[2] = { NSEC_PER_SEC / 4, NSEC_PER_SEC / 8 };
fe094d98e   Jens Axboe   cfq-iosched: make...
28
29
30
31
  /* 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;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
32
33
  static const u64 cfq_slice_sync = NSEC_PER_SEC / 10;
  static u64 cfq_slice_async = NSEC_PER_SEC / 25;
64100099e   Arjan van de Ven   [BLOCK] mark some...
34
  static const int cfq_slice_async_rq = 2;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
35
36
37
  static u64 cfq_slice_idle = NSEC_PER_SEC / 125;
  static u64 cfq_group_idle = NSEC_PER_SEC / 125;
  static const u64 cfq_target_latency = (u64)NSEC_PER_SEC * 3/10; /* 300 ms */
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
38
  static const int cfq_hist_divisor = 4;
22e2c507c   Jens Axboe   [PATCH] Update cf...
39

d9e7620e6   Jens Axboe   cfq-iosched: rewo...
40
  /*
5be6b7561   Hou Tao   cfq-iosched: fix ...
41
   * offset from end of queue service tree for idle class
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
42
   */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
43
  #define CFQ_IDLE_DELAY		(NSEC_PER_SEC / 5)
5be6b7561   Hou Tao   cfq-iosched: fix ...
44
45
46
47
  /* offset from end of group service tree under time slice mode */
  #define CFQ_SLICE_MODE_GROUP_DELAY (NSEC_PER_SEC / 5)
  /* offset from end of group service under IOPS mode */
  #define CFQ_IOPS_MODE_GROUP_DELAY (HZ / 5)
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
48
49
50
51
  
  /*
   * below this threshold, we consider thinktime immediate
   */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
52
  #define CFQ_MIN_TT		(2 * NSEC_PER_SEC / HZ)
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
53

22e2c507c   Jens Axboe   [PATCH] Update cf...
54
  #define CFQ_SLICE_SCALE		(5)
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
55
  #define CFQ_HW_QUEUE_MIN	(5)
25bc6b077   Vivek Goyal   blkio: Introduce ...
56
  #define CFQ_SERVICE_SHIFT       12
22e2c507c   Jens Axboe   [PATCH] Update cf...
57

3dde36dde   Corrado Zoccolo   cfq-iosched: rewo...
58
  #define CFQQ_SEEK_THR		(sector_t)(8 * 100)
e9ce335df   Shaohua Li   cfq-iosched: fix ...
59
  #define CFQQ_CLOSE_THR		(sector_t)(8 * 1024)
41647e7a9   Corrado Zoccolo   cfq-iosched: reth...
60
  #define CFQQ_SECT_THR_NONROT	(sector_t)(2 * 32)
3dde36dde   Corrado Zoccolo   cfq-iosched: rewo...
61
  #define CFQQ_SEEKY(cfqq)	(hweight32(cfqq->seek_history) > 32/8)
ae54abed6   Shaohua Li   cfq-iosched: spli...
62

a612fddf0   Tejun Heo   block, cfq: move ...
63
64
65
  #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
66

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

22e2c507c   Jens Axboe   [PATCH] Update cf...
69
70
  #define CFQ_PRIO_LISTS		IOPRIO_BE_NR
  #define cfq_class_idle(cfqq)	((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
22e2c507c   Jens Axboe   [PATCH] Update cf...
71
  #define cfq_class_rt(cfqq)	((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
72
  #define sample_valid(samples)	((samples) > 80)
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
73
  #define rb_entry_cfqg(node)	rb_entry((node), struct cfq_group, rb_node)
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
74

e48453c38   Arianna Avanzini   block, cgroup: im...
75
  /* blkio-related constants */
3ecca6293   Tejun Heo   blkcg: s/CFQ_WEIG...
76
77
78
  #define CFQ_WEIGHT_LEGACY_MIN	10
  #define CFQ_WEIGHT_LEGACY_DFL	500
  #define CFQ_WEIGHT_LEGACY_MAX	1000
e48453c38   Arianna Avanzini   block, cgroup: im...
79

c58698073   Tejun Heo   block, cfq: reorg...
80
  struct cfq_ttime {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
81
  	u64 last_end_request;
c58698073   Tejun Heo   block, cfq: reorg...
82

9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
83
84
  	u64 ttime_total;
  	u64 ttime_mean;
c58698073   Tejun Heo   block, cfq: reorg...
85
  	unsigned long ttime_samples;
c58698073   Tejun Heo   block, cfq: reorg...
86
  };
22e2c507c   Jens Axboe   [PATCH] Update cf...
87
  /*
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
88
89
90
91
92
93
   * 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 {
09663c86e   Davidlohr Bueso   block/cfq: replac...
94
  	struct rb_root_cached rb;
f0f1a45f9   Davidlohr Bueso   block/cfq: cache ...
95
  	struct rb_node *rb_rightmost;
aa6f6a3de   Corrado Zoccolo   cfq-iosched: prep...
96
  	unsigned count;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
97
  	u64 min_vdisktime;
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
98
  	struct cfq_ttime ttime;
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
99
  };
09663c86e   Davidlohr Bueso   block/cfq: replac...
100
  #define CFQ_RB_ROOT	(struct cfq_rb_root) { .rb = RB_ROOT_CACHED, \
f0f1a45f9   Davidlohr Bueso   block/cfq: cache ...
101
  			.rb_rightmost = NULL,			     \
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
102
  			.ttime = {.last_end_request = ktime_get_ns(),},}
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
103
104
  
  /*
6118b70b3   Jens Axboe   cfq-iosched: get ...
105
106
107
108
   * Per process-grouping structure
   */
  struct cfq_queue {
  	/* reference count */
30d7b9448   Shaohua Li   block cfq: don't ...
109
  	int ref;
6118b70b3   Jens Axboe   cfq-iosched: get ...
110
111
112
113
114
115
116
  	/* 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 */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
117
  	u64 rb_key;
6118b70b3   Jens Axboe   cfq-iosched: get ...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  	/* 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...
132
  	/* time when queue got scheduled in to dispatch first request. */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
133
134
135
  	u64 dispatch_start;
  	u64 allocated_slice;
  	u64 slice_dispatch;
dae739ebc   Vivek Goyal   blkio: Group time...
136
  	/* time when first request from queue completed and slice started. */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
137
138
  	u64 slice_start;
  	u64 slice_end;
93fdf1478   Jan Kara   cfq-iosched: Conv...
139
  	s64 slice_resid;
6118b70b3   Jens Axboe   cfq-iosched: get ...
140

65299a3b7   Christoph Hellwig   block: separate p...
141
142
  	/* pending priority requests */
  	int prio_pending;
6118b70b3   Jens Axboe   cfq-iosched: get ...
143
144
145
146
147
  	/* number of requests that are on the dispatch list or inside driver */
  	int dispatched;
  
  	/* io prio of this group */
  	unsigned short ioprio, org_ioprio;
b8269db45   Jens Axboe   cfq-iosched: temp...
148
  	unsigned short ioprio_class, org_ioprio_class;
6118b70b3   Jens Axboe   cfq-iosched: get ...
149

c4081ba5c   Richard Kennedy   cfq: reorder cfq_...
150
  	pid_t pid;
3dde36dde   Corrado Zoccolo   cfq-iosched: rewo...
151
  	u32 seek_history;
b2c18e1e0   Jeff Moyer   cfq: calculate th...
152
  	sector_t last_request_pos;
aa6f6a3de   Corrado Zoccolo   cfq-iosched: prep...
153
  	struct cfq_rb_root *service_tree;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
154
  	struct cfq_queue *new_cfqq;
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
155
  	struct cfq_group *cfqg;
c4e7893eb   Vivek Goyal   cfq-iosched: blkt...
156
157
  	/* Number of sectors dispatched from queue in single dispatch round */
  	unsigned long nr_sectors;
6118b70b3   Jens Axboe   cfq-iosched: get ...
158
159
160
  };
  
  /*
718eee057   Corrado Zoccolo   cfq-iosched: fair...
161
   * First index in the service_trees.
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
162
163
   * IDLE is handled separately, so it has negative index
   */
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
164
  enum wl_class_t {
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
165
  	BE_WORKLOAD = 0,
615f0259e   Vivek Goyal   blkio: Implement ...
166
167
  	RT_WORKLOAD = 1,
  	IDLE_WORKLOAD = 2,
b4627321e   Vivek Goyal   cfq-iosched: Fix ...
168
  	CFQ_PRIO_NR,
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
169
170
171
  };
  
  /*
718eee057   Corrado Zoccolo   cfq-iosched: fair...
172
173
174
175
176
177
178
   * 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...
179
180
  struct cfqg_stats {
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
155fead9b   Tejun Heo   blkcg: move blkio...
181
182
183
184
185
186
187
188
  	/* 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;
155fead9b   Tejun Heo   blkcg: move blkio...
189
190
191
192
193
194
195
196
197
198
199
200
201
  	/* 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...
202
  	/* time spent idling for this blkcg_gq */
155fead9b   Tejun Heo   blkcg: move blkio...
203
204
205
206
207
208
209
210
211
212
213
  	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 */
  };
e48453c38   Arianna Avanzini   block, cgroup: im...
214
215
216
  /* Per-cgroup data */
  struct cfq_group_data {
  	/* must be the first member */
814376483   Tejun Heo   blkcg: minor upda...
217
  	struct blkcg_policy_data cpd;
e48453c38   Arianna Avanzini   block, cgroup: im...
218
219
220
221
  
  	unsigned int weight;
  	unsigned int leaf_weight;
  };
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
222
223
  /* This is per cgroup per device grouping structure */
  struct cfq_group {
f95a04afa   Tejun Heo   blkcg: embed stru...
224
225
  	/* must be the first member */
  	struct blkg_policy_data pd;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
226
227
228
229
230
  	/* group service_tree member */
  	struct rb_node rb_node;
  
  	/* group service_tree key */
  	u64 vdisktime;
e71357e11   Tejun Heo   cfq-iosched: add ...
231
232
  
  	/*
7918ffb5b   Tejun Heo   cfq-iosched: impl...
233
234
235
236
237
238
239
240
241
242
243
244
  	 * 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...
245
246
247
248
249
250
251
252
253
254
255
256
  	 * 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 ...
257
258
259
260
261
  	 * 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 ...
262
  	unsigned int weight;
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
263
  	unsigned int new_weight;
3381cb8d2   Tejun Heo   blkcg: move blkio...
264
  	unsigned int dev_weight;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
265

e71357e11   Tejun Heo   cfq-iosched: add ...
266
267
268
  	unsigned int leaf_weight;
  	unsigned int new_leaf_weight;
  	unsigned int dev_leaf_weight;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
269
270
  	/* number of cfqq currently on this group */
  	int nr_cfqq;
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
271
  	/*
4495a7d41   Kyungmin Park   CFQ: Fix typo and...
272
  	 * Per group busy queues average. Useful for workload slice calc. We
b4627321e   Vivek Goyal   cfq-iosched: Fix ...
273
274
275
276
277
278
279
280
281
282
283
  	 * 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 ...
284
285
286
287
  	 * 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...
288

9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
289
  	u64 saved_wl_slice;
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
290
291
  	enum wl_type_t saved_wl_type;
  	enum wl_class_t saved_wl_class;
4eef30499   Tejun Heo   blkcg: move per-q...
292

80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
293
294
  	/* number of requests that are on the dispatch list or inside driver */
  	int dispatched;
7700fc4f6   Shaohua Li   CFQ: add think ti...
295
  	struct cfq_ttime ttime;
0b39920b5   Tejun Heo   cfq-iosched: coll...
296
  	struct cfqg_stats stats;	/* stats for this cfqg */
60a837077   Tejun Heo   cfq-iosched: char...
297
298
299
300
  
  	/* async queue for each priority case */
  	struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
  	struct cfq_queue *async_idle_cfqq;
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
301
  };
718eee057   Corrado Zoccolo   cfq-iosched: fair...
302

c58698073   Tejun Heo   block, cfq: reorg...
303
304
305
306
  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...
307
308
  	int			ioprio;		/* the current ioprio */
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
f4da80727   Tejun Heo   blkcg: remove blk...
309
  	uint64_t		blkcg_serial_nr; /* the current blkcg serial */
598971bfb   Tejun Heo   cfq: don't use ic...
310
  #endif
c58698073   Tejun Heo   block, cfq: reorg...
311
  };
718eee057   Corrado Zoccolo   cfq-iosched: fair...
312
  /*
22e2c507c   Jens Axboe   [PATCH] Update cf...
313
314
   * Per block device queue structure
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
315
  struct cfq_data {
165125e1e   Jens Axboe   [BLOCK] Get rid o...
316
  	struct request_queue *queue;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
317
318
  	/* Root service tree for cfq_groups */
  	struct cfq_rb_root grp_service_tree;
f51b802c1   Tejun Heo   blkcg: use the us...
319
  	struct cfq_group *root_group;
22e2c507c   Jens Axboe   [PATCH] Update cf...
320
321
  
  	/*
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
322
  	 * The priority currently being served
22e2c507c   Jens Axboe   [PATCH] Update cf...
323
  	 */
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
324
325
  	enum wl_class_t serving_wl_class;
  	enum wl_type_t serving_wl_type;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
326
  	u64 workload_expires;
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
327
  	struct cfq_group *serving_group;
a36e71f99   Jens Axboe   cfq-iosched: add ...
328
329
330
331
332
333
334
  
  	/*
  	 * 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...
335
  	unsigned int busy_queues;
ef8a41df8   Shaohua Li   cfq-iosched: give...
336
  	unsigned int busy_sync_queues;
22e2c507c   Jens Axboe   [PATCH] Update cf...
337

53c583d22   Corrado Zoccolo   cfq-iosched: requ...
338
339
  	int rq_in_driver;
  	int rq_in_flight[2];
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
340
341
342
343
344
  
  	/*
  	 * queue-depth detection
  	 */
  	int rq_queued;
25776e359   Jens Axboe   [PATCH] cfq-iosch...
345
  	int hw_tag;
e459dd08f   Corrado Zoccolo   cfq-iosched: fix ...
346
347
348
349
350
351
352
353
  	/*
  	 * 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
354

22e2c507c   Jens Axboe   [PATCH] Update cf...
355
  	/*
22e2c507c   Jens Axboe   [PATCH] Update cf...
356
357
  	 * idle window management
  	 */
911483258   Jan Kara   cfq-iosched: Conv...
358
  	struct hrtimer idle_slice_timer;
23e018a1b   Jens Axboe   block: get rid of...
359
  	struct work_struct unplug_work;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
360

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

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
366
367
368
369
  	/*
  	 * tunables, see top of file
  	 */
  	unsigned int cfq_quantum;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
370
371
  	unsigned int cfq_back_penalty;
  	unsigned int cfq_back_max;
22e2c507c   Jens Axboe   [PATCH] Update cf...
372
  	unsigned int cfq_slice_async_rq;
963b72fc6   Jens Axboe   cfq-iosched: rena...
373
  	unsigned int cfq_latency;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
374
375
376
377
378
  	u64 cfq_fifo_expire[2];
  	u64 cfq_slice[2];
  	u64 cfq_slice_idle;
  	u64 cfq_group_idle;
  	u64 cfq_target_latency;
d9ff41879   Al Viro   [PATCH] make cfq_...
379

6118b70b3   Jens Axboe   cfq-iosched: get ...
380
381
382
383
  	/*
  	 * Fallback dummy cfqq for extreme OOM conditions
  	 */
  	struct cfq_queue oom_cfqq;
365722bb9   Vivek Goyal   cfq-iosched: dela...
384

9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
385
  	u64 last_delayed_sync;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
386
  };
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
387
  static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
60a837077   Tejun Heo   cfq-iosched: char...
388
  static void cfq_put_queue(struct cfq_queue *cfqq);
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
389

34b98d03b   Vivek Goyal   cfq-iosched: Rena...
390
  static struct cfq_rb_root *st_for(struct cfq_group *cfqg,
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
391
  					    enum wl_class_t class,
65b32a573   Vivek Goyal   cfq-iosched: Remo...
392
  					    enum wl_type_t type)
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
393
  {
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
394
395
  	if (!cfqg)
  		return NULL;
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
396
  	if (class == IDLE_WORKLOAD)
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
397
  		return &cfqg->service_tree_idle;
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
398

3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
399
  	return &cfqg->service_trees[class][type];
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
400
  }
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
401
  enum cfqq_state_flags {
b0b8d7494   Jens Axboe   cfq-iosched: docu...
402
403
  	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'...
404
  	CFQ_CFQQ_FLAG_must_dispatch,	/* must be allowed a dispatch */
b0b8d7494   Jens Axboe   cfq-iosched: docu...
405
  	CFQ_CFQQ_FLAG_must_alloc_slice,	/* per-slice must_alloc flag */
b0b8d7494   Jens Axboe   cfq-iosched: docu...
406
407
408
  	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...
409
  	CFQ_CFQQ_FLAG_slice_new,	/* no requests dispatched in slice */
91fac317a   Vasily Tarasov   cfq-iosched: get ...
410
  	CFQ_CFQQ_FLAG_sync,		/* synchronous queue */
b3b6d0408   Jeff Moyer   cfq: change the m...
411
  	CFQ_CFQQ_FLAG_coop,		/* cfqq is shared */
ae54abed6   Shaohua Li   cfq-iosched: spli...
412
  	CFQ_CFQQ_FLAG_split_coop,	/* shared cfqq will be splitted */
76280aff1   Corrado Zoccolo   cfq-iosched: idli...
413
  	CFQ_CFQQ_FLAG_deep,		/* sync cfqq experienced large depth */
f75edf2dc   Vivek Goyal   blkio: Wait for c...
414
  	CFQ_CFQQ_FLAG_wait_busy,	/* Waiting for next request */
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
415
416
417
418
419
  };
  
  #define CFQ_CFQQ_FNS(name)						\
  static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq)		\
  {									\
fe094d98e   Jens Axboe   cfq-iosched: make...
420
  	(cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name);			\
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
421
422
423
  }									\
  static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq)	\
  {									\
fe094d98e   Jens Axboe   cfq-iosched: make...
424
  	(cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name);			\
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
425
426
427
  }									\
  static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq)		\
  {									\
fe094d98e   Jens Axboe   cfq-iosched: make...
428
  	return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0;	\
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
429
430
431
432
  }
  
  CFQ_CFQQ_FNS(on_rr);
  CFQ_CFQQ_FNS(wait_request);
b029195dd   Jens Axboe   cfq-iosched: don'...
433
  CFQ_CFQQ_FNS(must_dispatch);
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
434
  CFQ_CFQQ_FNS(must_alloc_slice);
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
435
436
437
  CFQ_CFQQ_FNS(fifo_expire);
  CFQ_CFQQ_FNS(idle_window);
  CFQ_CFQQ_FNS(prio_changed);
44f7c1606   Jens Axboe   cfq-iosched: defe...
438
  CFQ_CFQQ_FNS(slice_new);
91fac317a   Vasily Tarasov   cfq-iosched: get ...
439
  CFQ_CFQQ_FNS(sync);
a36e71f99   Jens Axboe   cfq-iosched: add ...
440
  CFQ_CFQQ_FNS(coop);
ae54abed6   Shaohua Li   cfq-iosched: spli...
441
  CFQ_CFQQ_FNS(split_coop);
76280aff1   Corrado Zoccolo   cfq-iosched: idli...
442
  CFQ_CFQQ_FNS(deep);
f75edf2dc   Vivek Goyal   blkio: Wait for c...
443
  CFQ_CFQQ_FNS(wait_busy);
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
444
  #undef CFQ_CFQQ_FNS
629ed0b10   Tejun Heo   blkcg: move stati...
445
  #if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
446

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

155fead9b   Tejun Heo   blkcg: move blkio...
491
  	if (cfqg_stats_waiting(stats))
629ed0b10   Tejun Heo   blkcg: move stati...
492
  		return;
155fead9b   Tejun Heo   blkcg: move blkio...
493
  	if (cfqg == curr_cfqg)
629ed0b10   Tejun Heo   blkcg: move stati...
494
  		return;
155fead9b   Tejun Heo   blkcg: move blkio...
495
496
  	stats->start_group_wait_time = sched_clock();
  	cfqg_stats_mark_waiting(stats);
629ed0b10   Tejun Heo   blkcg: move stati...
497
498
499
  }
  
  /* This should be called with the queue_lock held. */
155fead9b   Tejun Heo   blkcg: move blkio...
500
  static void cfqg_stats_end_empty_time(struct cfqg_stats *stats)
629ed0b10   Tejun Heo   blkcg: move stati...
501
502
  {
  	unsigned long long now;
155fead9b   Tejun Heo   blkcg: move blkio...
503
  	if (!cfqg_stats_empty(stats))
629ed0b10   Tejun Heo   blkcg: move stati...
504
505
506
507
508
509
  		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...
510
  	cfqg_stats_clear_empty(stats);
629ed0b10   Tejun Heo   blkcg: move stati...
511
  }
155fead9b   Tejun Heo   blkcg: move blkio...
512
  static void cfqg_stats_update_dequeue(struct cfq_group *cfqg)
629ed0b10   Tejun Heo   blkcg: move stati...
513
  {
155fead9b   Tejun Heo   blkcg: move blkio...
514
  	blkg_stat_add(&cfqg->stats.dequeue, 1);
629ed0b10   Tejun Heo   blkcg: move stati...
515
  }
155fead9b   Tejun Heo   blkcg: move blkio...
516
  static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg)
629ed0b10   Tejun Heo   blkcg: move stati...
517
  {
155fead9b   Tejun Heo   blkcg: move blkio...
518
  	struct cfqg_stats *stats = &cfqg->stats;
629ed0b10   Tejun Heo   blkcg: move stati...
519

4d5e80a76   Tejun Heo   blkcg: s/blkg_rws...
520
  	if (blkg_rwstat_total(&stats->queued))
629ed0b10   Tejun Heo   blkcg: move stati...
521
522
523
524
525
526
527
  		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...
528
  	if (cfqg_stats_empty(stats))
629ed0b10   Tejun Heo   blkcg: move stati...
529
530
531
  		return;
  
  	stats->start_empty_time = sched_clock();
155fead9b   Tejun Heo   blkcg: move blkio...
532
  	cfqg_stats_mark_empty(stats);
629ed0b10   Tejun Heo   blkcg: move stati...
533
  }
155fead9b   Tejun Heo   blkcg: move blkio...
534
  static void cfqg_stats_update_idle_time(struct cfq_group *cfqg)
629ed0b10   Tejun Heo   blkcg: move stati...
535
  {
155fead9b   Tejun Heo   blkcg: move blkio...
536
  	struct cfqg_stats *stats = &cfqg->stats;
629ed0b10   Tejun Heo   blkcg: move stati...
537

155fead9b   Tejun Heo   blkcg: move blkio...
538
  	if (cfqg_stats_idling(stats)) {
629ed0b10   Tejun Heo   blkcg: move stati...
539
540
541
542
543
  		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...
544
  		cfqg_stats_clear_idling(stats);
629ed0b10   Tejun Heo   blkcg: move stati...
545
546
  	}
  }
155fead9b   Tejun Heo   blkcg: move blkio...
547
  static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg)
629ed0b10   Tejun Heo   blkcg: move stati...
548
  {
155fead9b   Tejun Heo   blkcg: move blkio...
549
  	struct cfqg_stats *stats = &cfqg->stats;
629ed0b10   Tejun Heo   blkcg: move stati...
550

155fead9b   Tejun Heo   blkcg: move blkio...
551
  	BUG_ON(cfqg_stats_idling(stats));
629ed0b10   Tejun Heo   blkcg: move stati...
552
553
  
  	stats->start_idle_time = sched_clock();
155fead9b   Tejun Heo   blkcg: move blkio...
554
  	cfqg_stats_mark_idling(stats);
629ed0b10   Tejun Heo   blkcg: move stati...
555
  }
155fead9b   Tejun Heo   blkcg: move blkio...
556
  static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg)
629ed0b10   Tejun Heo   blkcg: move stati...
557
  {
155fead9b   Tejun Heo   blkcg: move blkio...
558
  	struct cfqg_stats *stats = &cfqg->stats;
629ed0b10   Tejun Heo   blkcg: move stati...
559
560
  
  	blkg_stat_add(&stats->avg_queue_size_sum,
4d5e80a76   Tejun Heo   blkcg: s/blkg_rws...
561
  		      blkg_rwstat_total(&stats->queued));
629ed0b10   Tejun Heo   blkcg: move stati...
562
  	blkg_stat_add(&stats->avg_queue_size_samples, 1);
155fead9b   Tejun Heo   blkcg: move blkio...
563
  	cfqg_stats_update_group_wait_time(stats);
629ed0b10   Tejun Heo   blkcg: move stati...
564
565
566
  }
  
  #else	/* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
f48ec1d78   Tejun Heo   cfq: fix build br...
567
568
569
570
571
572
573
  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...
574
575
576
577
  
  #endif	/* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
  
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
578

4ceab71b9   Jens Axboe   cfq-iosched: move...
579
580
581
582
583
584
585
586
  static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd)
  {
  	return pd ? container_of(pd, struct cfq_group, pd) : NULL;
  }
  
  static struct cfq_group_data
  *cpd_to_cfqgd(struct blkcg_policy_data *cpd)
  {
814376483   Tejun Heo   blkcg: minor upda...
587
  	return cpd ? container_of(cpd, struct cfq_group_data, cpd) : NULL;
4ceab71b9   Jens Axboe   cfq-iosched: move...
588
589
590
591
592
593
  }
  
  static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg)
  {
  	return pd_to_blkg(&cfqg->pd);
  }
ffea73fc7   Tejun Heo   block: blkcg_poli...
594
595
596
597
598
599
  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));
  }
e48453c38   Arianna Avanzini   block, cgroup: im...
600
601
602
603
  static struct cfq_group_data *blkcg_to_cfqgd(struct blkcg *blkcg)
  {
  	return cpd_to_cfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_cfq));
  }
d02f7aa8d   Tejun Heo   cfq-iosched: enab...
604
  static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg)
7918ffb5b   Tejun Heo   cfq-iosched: impl...
605
  {
d02f7aa8d   Tejun Heo   cfq-iosched: enab...
606
  	struct blkcg_gq *pblkg = cfqg_to_blkg(cfqg)->parent;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
607

d02f7aa8d   Tejun Heo   cfq-iosched: enab...
608
  	return pblkg ? blkg_to_cfqg(pblkg) : NULL;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
609
  }
3984aa552   Jan Kara   cfq-iosched: Allo...
610
611
612
613
614
615
  static inline bool cfqg_is_descendant(struct cfq_group *cfqg,
  				      struct cfq_group *ancestor)
  {
  	return cgroup_is_descendant(cfqg_to_blkg(cfqg)->blkcg->css.cgroup,
  				    cfqg_to_blkg(ancestor)->blkcg->css.cgroup);
  }
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
616
617
618
619
620
621
622
623
624
  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...
625
  #define cfq_log_cfqq(cfqd, cfqq, fmt, args...)	do {			\
35fe6d763   Shaohua Li   block: use standa...
626
627
628
  	blk_add_cgroup_trace_msg((cfqd)->queue,				\
  			cfqg_to_blkg((cfqq)->cfqg)->blkcg,		\
  			"cfq%d%c%c " fmt, (cfqq)->pid,			\
b226e5c41   Vivek Goyal   cfq-iosched: Prin...
629
630
  			cfq_cfqq_sync((cfqq)) ? 'S' : 'A',		\
  			cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
35fe6d763   Shaohua Li   block: use standa...
631
  			  ##args);					\
54e7ed12b   Tejun Heo   blkcg: remove blk...
632
633
634
  } while (0)
  
  #define cfq_log_cfqg(cfqd, cfqg, fmt, args...)	do {			\
35fe6d763   Shaohua Li   block: use standa...
635
636
  	blk_add_cgroup_trace_msg((cfqd)->queue,				\
  			cfqg_to_blkg(cfqg)->blkcg, fmt, ##args);	\
54e7ed12b   Tejun Heo   blkcg: remove blk...
637
  } while (0)
2868ef7b3   Vivek Goyal   blkio: Some debug...
638

155fead9b   Tejun Heo   blkcg: move blkio...
639
  static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
ef295ecf0   Christoph Hellwig   block: better op ...
640
641
  					    struct cfq_group *curr_cfqg,
  					    unsigned int op)
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
642
  {
ef295ecf0   Christoph Hellwig   block: better op ...
643
  	blkg_rwstat_add(&cfqg->stats.queued, op, 1);
155fead9b   Tejun Heo   blkcg: move blkio...
644
645
  	cfqg_stats_end_empty_time(&cfqg->stats);
  	cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg);
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
646
  }
155fead9b   Tejun Heo   blkcg: move blkio...
647
  static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
648
  			uint64_t time, unsigned long unaccounted_time)
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
649
  {
155fead9b   Tejun Heo   blkcg: move blkio...
650
  	blkg_stat_add(&cfqg->stats.time, time);
629ed0b10   Tejun Heo   blkcg: move stati...
651
  #ifdef CONFIG_DEBUG_BLK_CGROUP
155fead9b   Tejun Heo   blkcg: move blkio...
652
  	blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time);
629ed0b10   Tejun Heo   blkcg: move stati...
653
  #endif
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
654
  }
ef295ecf0   Christoph Hellwig   block: better op ...
655
656
  static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg,
  					       unsigned int op)
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
657
  {
ef295ecf0   Christoph Hellwig   block: better op ...
658
  	blkg_rwstat_add(&cfqg->stats.queued, op, -1);
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
659
  }
ef295ecf0   Christoph Hellwig   block: better op ...
660
661
  static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
  					       unsigned int op)
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
662
  {
ef295ecf0   Christoph Hellwig   block: better op ...
663
  	blkg_rwstat_add(&cfqg->stats.merged, op, 1);
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
664
  }
155fead9b   Tejun Heo   blkcg: move blkio...
665
  static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
ef295ecf0   Christoph Hellwig   block: better op ...
666
667
  			uint64_t start_time, uint64_t io_start_time,
  			unsigned int op)
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
668
  {
155fead9b   Tejun Heo   blkcg: move blkio...
669
  	struct cfqg_stats *stats = &cfqg->stats;
629ed0b10   Tejun Heo   blkcg: move stati...
670
  	unsigned long long now = sched_clock();
629ed0b10   Tejun Heo   blkcg: move stati...
671
672
  
  	if (time_after64(now, io_start_time))
ef295ecf0   Christoph Hellwig   block: better op ...
673
  		blkg_rwstat_add(&stats->service_time, op, now - io_start_time);
629ed0b10   Tejun Heo   blkcg: move stati...
674
  	if (time_after64(io_start_time, start_time))
ef295ecf0   Christoph Hellwig   block: better op ...
675
  		blkg_rwstat_add(&stats->wait_time, op,
629ed0b10   Tejun Heo   blkcg: move stati...
676
  				io_start_time - start_time);
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
677
  }
689665af4   Tejun Heo   cfq-iosched: sepa...
678
679
  /* @stats = 0 */
  static void cfqg_stats_reset(struct cfqg_stats *stats)
155fead9b   Tejun Heo   blkcg: move blkio...
680
  {
155fead9b   Tejun Heo   blkcg: move blkio...
681
  	/* queued stats shouldn't be cleared */
155fead9b   Tejun Heo   blkcg: move blkio...
682
683
684
685
686
687
688
689
690
691
692
693
694
695
  	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...
696
  /* @to += @from */
e6269c445   Tejun Heo   blkcg: add blkg_[...
697
  static void cfqg_stats_add_aux(struct cfqg_stats *to, struct cfqg_stats *from)
0b39920b5   Tejun Heo   cfq-iosched: coll...
698
699
  {
  	/* queued stats shouldn't be cleared */
e6269c445   Tejun Heo   blkcg: add blkg_[...
700
701
702
703
  	blkg_rwstat_add_aux(&to->merged, &from->merged);
  	blkg_rwstat_add_aux(&to->service_time, &from->service_time);
  	blkg_rwstat_add_aux(&to->wait_time, &from->wait_time);
  	blkg_stat_add_aux(&from->time, &from->time);
0b39920b5   Tejun Heo   cfq-iosched: coll...
704
  #ifdef CONFIG_DEBUG_BLK_CGROUP
e6269c445   Tejun Heo   blkcg: add blkg_[...
705
706
707
708
709
710
711
  	blkg_stat_add_aux(&to->unaccounted_time, &from->unaccounted_time);
  	blkg_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
  	blkg_stat_add_aux(&to->avg_queue_size_samples, &from->avg_queue_size_samples);
  	blkg_stat_add_aux(&to->dequeue, &from->dequeue);
  	blkg_stat_add_aux(&to->group_wait_time, &from->group_wait_time);
  	blkg_stat_add_aux(&to->idle_time, &from->idle_time);
  	blkg_stat_add_aux(&to->empty_time, &from->empty_time);
0b39920b5   Tejun Heo   cfq-iosched: coll...
712
713
714
715
  #endif
  }
  
  /*
e6269c445   Tejun Heo   blkcg: add blkg_[...
716
   * Transfer @cfqg's stats to its parent's aux counts so that the ancestors'
0b39920b5   Tejun Heo   cfq-iosched: coll...
717
718
719
720
721
722
723
724
725
726
727
   * 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;
e6269c445   Tejun Heo   blkcg: add blkg_[...
728
  	cfqg_stats_add_aux(&parent->stats, &cfqg->stats);
0b39920b5   Tejun Heo   cfq-iosched: coll...
729
  	cfqg_stats_reset(&cfqg->stats);
0b39920b5   Tejun Heo   cfq-iosched: coll...
730
  }
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
731
  #else	/* CONFIG_CFQ_GROUP_IOSCHED */
d02f7aa8d   Tejun Heo   cfq-iosched: enab...
732
  static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) { return NULL; }
3984aa552   Jan Kara   cfq-iosched: Allo...
733
734
735
736
737
  static inline bool cfqg_is_descendant(struct cfq_group *cfqg,
  				      struct cfq_group *ancestor)
  {
  	return true;
  }
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
738
739
  static inline void cfqg_get(struct cfq_group *cfqg) { }
  static inline void cfqg_put(struct cfq_group *cfqg) { }
7b679138b   Jens Axboe   cfq-iosched: add ...
740
  #define cfq_log_cfqq(cfqd, cfqq, fmt, args...)	\
b226e5c41   Vivek Goyal   cfq-iosched: Prin...
741
742
743
744
  	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...
745
  #define cfq_log_cfqg(cfqd, cfqg, fmt, args...)		do {} while (0)
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
746

155fead9b   Tejun Heo   blkcg: move blkio...
747
  static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
ef295ecf0   Christoph Hellwig   block: better op ...
748
  			struct cfq_group *curr_cfqg, unsigned int op) { }
155fead9b   Tejun Heo   blkcg: move blkio...
749
  static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
750
  			uint64_t time, unsigned long unaccounted_time) { }
ef295ecf0   Christoph Hellwig   block: better op ...
751
752
753
754
  static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg,
  			unsigned int op) { }
  static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
  			unsigned int op) { }
155fead9b   Tejun Heo   blkcg: move blkio...
755
  static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
ef295ecf0   Christoph Hellwig   block: better op ...
756
757
  			uint64_t start_time, uint64_t io_start_time,
  			unsigned int op) { }
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
758

eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
759
  #endif	/* CONFIG_CFQ_GROUP_IOSCHED */
7b679138b   Jens Axboe   cfq-iosched: add ...
760
761
  #define cfq_log(cfqd, fmt, args...)	\
  	blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
615f0259e   Vivek Goyal   blkio: Implement ...
762
763
764
765
766
767
768
769
770
  /* 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...
771
772
773
  static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
  	struct cfq_ttime *ttime, bool group_idle)
  {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
774
  	u64 slice;
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
775
776
777
778
779
780
781
782
  	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 ...
783

02b35081f   Vivek Goyal   cfq-iosched: Do g...
784
785
786
787
788
789
790
791
792
793
794
795
796
797
  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...
798
  static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq)
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
799
800
801
802
803
804
805
  {
  	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...
806
807
808
809
810
811
812
813
814
  
  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...
815
  static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class,
58ff82f34   Vivek Goyal   blkio: Implement ...
816
817
  					struct cfq_data *cfqd,
  					struct cfq_group *cfqg)
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
818
  {
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
819
  	if (wl_class == IDLE_WORKLOAD)
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
820
  		return cfqg->service_tree_idle.count;
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
821

34b98d03b   Vivek Goyal   cfq-iosched: Rena...
822
823
824
  	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...
825
  }
f26bd1f0a   Vivek Goyal   blkio: Determine ...
826
827
828
  static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
  					struct cfq_group *cfqg)
  {
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
829
830
  	return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count +
  		cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
f26bd1f0a   Vivek Goyal   blkio: Determine ...
831
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
832
  static void cfq_dispatch_insert(struct request_queue *, struct request *);
4f85cb96d   Tejun Heo   block: make block...
833
  static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
2da8de0bb   Tejun Heo   cfq-iosched: remo...
834
  				       struct cfq_io_cq *cic, struct bio *bio);
91fac317a   Vasily Tarasov   cfq-iosched: get ...
835

c58698073   Tejun Heo   block, cfq: reorg...
836
837
838
839
840
  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 ...
841
842
843
844
845
846
847
  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...
848
  static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
91fac317a   Vasily Tarasov   cfq-iosched: get ...
849
  {
a6151c3a5   Jens Axboe   cfq-iosched: appl...
850
  	return cic->cfqq[is_sync];
91fac317a   Vasily Tarasov   cfq-iosched: get ...
851
  }
c58698073   Tejun Heo   block, cfq: reorg...
852
853
  static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
  				bool is_sync)
91fac317a   Vasily Tarasov   cfq-iosched: get ...
854
  {
a6151c3a5   Jens Axboe   cfq-iosched: appl...
855
  	cic->cfqq[is_sync] = cfqq;
91fac317a   Vasily Tarasov   cfq-iosched: get ...
856
  }
c58698073   Tejun Heo   block, cfq: reorg...
857
  static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
bca4b914b   Konstantin Khlebnikov   cfq-iosched: remo...
858
  {
c58698073   Tejun Heo   block, cfq: reorg...
859
  	return cic->icq.q->elevator->elevator_data;
bca4b914b   Konstantin Khlebnikov   cfq-iosched: remo...
860
  }
91fac317a   Vasily Tarasov   cfq-iosched: get ...
861
  /*
99f95e528   Andrew Morton   [PATCH] cfq build...
862
863
864
   * 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...
865
  static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
99f95e528   Andrew Morton   [PATCH] cfq build...
866
  {
7b679138b   Jens Axboe   cfq-iosched: add ...
867
868
  	if (cfqd->busy_queues) {
  		cfq_log(cfqd, "schedule dispatch");
59c3d45e4   Jens Axboe   block: remove 'q'...
869
  		kblockd_schedule_work(&cfqd->unplug_work);
7b679138b   Jens Axboe   cfq-iosched: add ...
870
  	}
99f95e528   Andrew Morton   [PATCH] cfq build...
871
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
872
  /*
44f7c1606   Jens Axboe   cfq-iosched: defe...
873
874
875
876
   * 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.
   */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
877
  static inline u64 cfq_prio_slice(struct cfq_data *cfqd, bool sync,
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
878
  				 unsigned short prio)
44f7c1606   Jens Axboe   cfq-iosched: defe...
879
  {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
880
881
  	u64 base_slice = cfqd->cfq_slice[sync];
  	u64 slice = div_u64(base_slice, CFQ_SLICE_SCALE);
44f7c1606   Jens Axboe   cfq-iosched: defe...
882

d9e7620e6   Jens Axboe   cfq-iosched: rewo...
883
  	WARN_ON(prio >= IOPRIO_BE_NR);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
884
  	return base_slice + (slice * (4 - prio));
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
885
  }
44f7c1606   Jens Axboe   cfq-iosched: defe...
886

9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
887
  static inline u64
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
888
889
890
  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...
891
  }
1d3650f71   Tejun Heo   cfq-iosched: impl...
892
893
894
895
896
897
898
899
900
901
902
903
  /**
   * 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.
   */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
904
  static inline u64 cfqg_scale_charge(u64 charge,
1d3650f71   Tejun Heo   cfq-iosched: impl...
905
  				    unsigned int vfraction)
25bc6b077   Vivek Goyal   blkio: Introduce ...
906
  {
1d3650f71   Tejun Heo   cfq-iosched: impl...
907
  	u64 c = charge << CFQ_SERVICE_SHIFT;	/* make it fixed point */
25bc6b077   Vivek Goyal   blkio: Introduce ...
908

1d3650f71   Tejun Heo   cfq-iosched: impl...
909
910
  	/* charge / vfraction */
  	c <<= CFQ_SERVICE_SHIFT;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
911
  	return div_u64(c, vfraction);
25bc6b077   Vivek Goyal   blkio: Introduce ...
912
913
914
915
916
917
918
919
920
921
  }
  
  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;
  }
25bc6b077   Vivek Goyal   blkio: Introduce ...
922
923
  static void update_min_vdisktime(struct cfq_rb_root *st)
  {
09663c86e   Davidlohr Bueso   block/cfq: replac...
924
925
  	if (!RB_EMPTY_ROOT(&st->rb.rb_root)) {
  		struct cfq_group *cfqg = rb_entry_cfqg(st->rb.rb_leftmost);
25bc6b077   Vivek Goyal   blkio: Introduce ...
926

a60327107   Gui Jianfeng   cfq-iosched: Fix ...
927
928
  		st->min_vdisktime = max_vdisktime(st->min_vdisktime,
  						  cfqg->vdisktime);
25bc6b077   Vivek Goyal   blkio: Introduce ...
929
  	}
25bc6b077   Vivek Goyal   blkio: Introduce ...
930
  }
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
931
932
933
934
935
  /*
   * 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 ...
936
937
  static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
  					struct cfq_group *cfqg, bool rt)
5869619cb   Jens Axboe   cfq-iosched: fix ...
938
  {
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
939
940
941
  	unsigned min_q, max_q;
  	unsigned mult  = cfq_hist_divisor - 1;
  	unsigned round = cfq_hist_divisor / 2;
58ff82f34   Vivek Goyal   blkio: Implement ...
942
  	unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
943

58ff82f34   Vivek Goyal   blkio: Implement ...
944
945
946
  	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...
947
  		cfq_hist_divisor;
58ff82f34   Vivek Goyal   blkio: Implement ...
948
949
  	return cfqg->busy_queues_avg[rt];
  }
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
950
  static inline u64
58ff82f34   Vivek Goyal   blkio: Implement ...
951
952
  cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
  {
41cad6ab2   Tejun Heo   cfq-iosched: conv...
953
  	return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT;
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
954
  }
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
955
  static inline u64
ba5bd520f   Vivek Goyal   cfq: rename a fun...
956
  cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
44f7c1606   Jens Axboe   cfq-iosched: defe...
957
  {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
958
  	u64 slice = cfq_prio_to_slice(cfqd, cfqq);
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
959
  	if (cfqd->cfq_latency) {
58ff82f34   Vivek Goyal   blkio: Implement ...
960
961
962
963
964
965
  		/*
  		 * 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));
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
966
967
968
  		u64 sync_slice = cfqd->cfq_slice[1];
  		u64 expect_latency = sync_slice * iq;
  		u64 group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
58ff82f34   Vivek Goyal   blkio: Implement ...
969
970
  
  		if (expect_latency > group_slice) {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
971
972
  			u64 base_low_slice = 2 * cfqd->cfq_slice_idle;
  			u64 low_slice;
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
973
974
  			/* scale low_slice according to IO priority
  			 * and sync vs async */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
975
976
  			low_slice = div64_u64(base_low_slice*slice, sync_slice);
  			low_slice = min(slice, low_slice);
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
977
978
  			/* the adapted slice value is scaled to fit all iqs
  			 * into the target latency */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
979
980
  			slice = div64_u64(slice*group_slice, expect_latency);
  			slice = max(slice, low_slice);
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
981
982
  		}
  	}
c553f8e33   Shaohua Li   block cfq: compen...
983
984
985
986
987
988
  	return slice;
  }
  
  static inline void
  cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
989
990
  	u64 slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
  	u64 now = ktime_get_ns();
c553f8e33   Shaohua Li   block cfq: compen...
991

9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
992
993
  	cfqq->slice_start = now;
  	cfqq->slice_end = now + slice;
f75edf2dc   Vivek Goyal   blkio: Wait for c...
994
  	cfqq->allocated_slice = slice;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
995
  	cfq_log_cfqq(cfqd, cfqq, "set_slice=%llu", cfqq->slice_end - now);
44f7c1606   Jens Axboe   cfq-iosched: defe...
996
997
998
999
1000
1001
1002
  }
  
  /*
   * 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...
1003
  static inline bool cfq_slice_used(struct cfq_queue *cfqq)
44f7c1606   Jens Axboe   cfq-iosched: defe...
1004
1005
  {
  	if (cfq_cfqq_slice_new(cfqq))
c1e44756f   Shaohua Li   cfq-iosched: do c...
1006
  		return false;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1007
  	if (ktime_get_ns() < cfqq->slice_end)
c1e44756f   Shaohua Li   cfq-iosched: do c...
1008
  		return false;
44f7c1606   Jens Axboe   cfq-iosched: defe...
1009

c1e44756f   Shaohua Li   cfq-iosched: do c...
1010
  	return true;
44f7c1606   Jens Axboe   cfq-iosched: defe...
1011
1012
1013
  }
  
  /*
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1014
   * Lifted from AS - choose which of rq1 and rq2 that is best served now.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1015
   * We choose the request that is closest to the head right now. Distance
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1016
   * behind the head is penalized and only allowed to a certain extent.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1017
   */
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1018
  static struct request *
cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
1019
  cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1020
  {
cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
1021
  	sector_t s1, s2, d1 = 0, d2 = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1022
  	unsigned long back_max;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1023
1024
1025
  #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
1026

5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1027
1028
1029
1030
  	if (rq1 == NULL || rq1 == rq2)
  		return rq2;
  	if (rq2 == NULL)
  		return rq1;
9c2c38a12   Jens Axboe   [PATCH] cfq-iosch...
1031

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

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
  	/*
  	 * 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...
1055
  		wrap |= CFQ_RQ1_WRAP;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1056
1057
1058
1059
1060
1061
  
  	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...
1062
  		wrap |= CFQ_RQ2_WRAP;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1063
1064
  
  	/* Found required data */
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1065
1066
1067
1068
1069
1070
  
  	/*
  	 * 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...
1071
  	case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1072
  		if (d1 < d2)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1073
  			return rq1;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1074
  		else if (d2 < d1)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1075
  			return rq2;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1076
1077
  		else {
  			if (s1 >= s2)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1078
  				return rq1;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1079
  			else
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1080
  				return rq2;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1081
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1082

e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1083
  	case CFQ_RQ2_WRAP:
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1084
  		return rq1;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1085
  	case CFQ_RQ1_WRAP:
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1086
1087
  		return rq2;
  	case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1088
1089
1090
1091
1092
1093
1094
1095
  	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...
1096
  			return rq1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1097
  		else
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1098
  			return rq2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1099
1100
  	}
  }
0871714e0   Jens Axboe   cfq-iosched: rela...
1101
  static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
1102
  {
615f0259e   Vivek Goyal   blkio: Implement ...
1103
1104
1105
  	/* Service tree is empty */
  	if (!root->count)
  		return NULL;
09663c86e   Davidlohr Bueso   block/cfq: replac...
1106
  	return rb_entry(rb_first_cached(&root->rb), struct cfq_queue, rb_node);
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
1107
  }
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1108
1109
  static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
  {
09663c86e   Davidlohr Bueso   block/cfq: replac...
1110
  	return rb_entry_cfqg(rb_first_cached(&root->rb));
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1111
  }
09663c86e   Davidlohr Bueso   block/cfq: replac...
1112
  static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
a36e71f99   Jens Axboe   cfq-iosched: add ...
1113
  {
f0f1a45f9   Davidlohr Bueso   block/cfq: cache ...
1114
1115
  	if (root->rb_rightmost == n)
  		root->rb_rightmost = rb_prev(n);
09663c86e   Davidlohr Bueso   block/cfq: replac...
1116
  	rb_erase_cached(n, &root->rb);
a36e71f99   Jens Axboe   cfq-iosched: add ...
1117
  	RB_CLEAR_NODE(n);
a36e71f99   Jens Axboe   cfq-iosched: add ...
1118

aa6f6a3de   Corrado Zoccolo   cfq-iosched: prep...
1119
  	--root->count;
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
1120
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1121
1122
1123
  /*
   * would be nice to take fifo expire time into account as well
   */
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1124
1125
1126
  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
1127
  {
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
1128
1129
  	struct rb_node *rbnext = rb_next(&last->rb_node);
  	struct rb_node *rbprev = rb_prev(&last->rb_node);
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1130
  	struct request *next = NULL, *prev = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1131

21183b07e   Jens Axboe   [PATCH] cfq-iosch...
1132
  	BUG_ON(RB_EMPTY_NODE(&last->rb_node));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1133
1134
  
  	if (rbprev)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1135
  		prev = rb_entry_rq(rbprev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1136

21183b07e   Jens Axboe   [PATCH] cfq-iosch...
1137
  	if (rbnext)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1138
  		next = rb_entry_rq(rbnext);
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
1139
1140
1141
  	else {
  		rbnext = rb_first(&cfqq->sort_list);
  		if (rbnext && rbnext != &last->rb_node)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1142
  			next = rb_entry_rq(rbnext);
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
1143
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1144

cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
1145
  	return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1146
  }
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1147
1148
  static u64 cfq_slice_offset(struct cfq_data *cfqd,
  			    struct cfq_queue *cfqq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1149
  {
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
1150
1151
1152
  	/*
  	 * just an approximation, should be ok.
  	 */
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
1153
  	return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
464191c65   Jens Axboe   Revert "cfq: Make...
1154
  		       cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
1155
  }
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1156
1157
1158
1159
1160
1161
1162
1163
1164
  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)
  {
09663c86e   Davidlohr Bueso   block/cfq: replac...
1165
  	struct rb_node **node = &st->rb.rb_root.rb_node;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1166
1167
1168
  	struct rb_node *parent = NULL;
  	struct cfq_group *__cfqg;
  	s64 key = cfqg_key(st, cfqg);
f0f1a45f9   Davidlohr Bueso   block/cfq: cache ...
1169
  	bool leftmost = true, rightmost = true;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1170
1171
1172
1173
  
  	while (*node != NULL) {
  		parent = *node;
  		__cfqg = rb_entry_cfqg(parent);
f0f1a45f9   Davidlohr Bueso   block/cfq: cache ...
1174
  		if (key < cfqg_key(st, __cfqg)) {
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1175
  			node = &parent->rb_left;
f0f1a45f9   Davidlohr Bueso   block/cfq: cache ...
1176
1177
  			rightmost = false;
  		} else {
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1178
  			node = &parent->rb_right;
09663c86e   Davidlohr Bueso   block/cfq: replac...
1179
  			leftmost = false;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1180
1181
  		}
  	}
f0f1a45f9   Davidlohr Bueso   block/cfq: cache ...
1182
1183
  	if (rightmost)
  		st->rb_rightmost = &cfqg->rb_node;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1184
  	rb_link_node(&cfqg->rb_node, parent, node);
09663c86e   Davidlohr Bueso   block/cfq: replac...
1185
  	rb_insert_color_cached(&cfqg->rb_node, &st->rb, leftmost);
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1186
  }
7b5af5cff   Toshiaki Makita   cfq-iosched: Add ...
1187
1188
1189
  /*
   * This has to be called only on activation of cfqg
   */
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1190
  static void
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1191
1192
  cfq_update_group_weight(struct cfq_group *cfqg)
  {
3381cb8d2   Tejun Heo   blkcg: move blkio...
1193
  	if (cfqg->new_weight) {
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1194
  		cfqg->weight = cfqg->new_weight;
3381cb8d2   Tejun Heo   blkcg: move blkio...
1195
  		cfqg->new_weight = 0;
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1196
  	}
e15693ef1   Toshiaki Makita   cfq-iosched: Fix ...
1197
1198
1199
1200
1201
1202
  }
  
  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 ...
1203
1204
1205
1206
1207
  
  	if (cfqg->new_leaf_weight) {
  		cfqg->leaf_weight = cfqg->new_leaf_weight;
  		cfqg->new_leaf_weight = 0;
  	}
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1208
1209
1210
1211
1212
  }
  
  static void
  cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
  {
1d3650f71   Tejun Heo   cfq-iosched: impl...
1213
  	unsigned int vfr = 1 << CFQ_SERVICE_SHIFT;	/* start with 1 */
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1214
  	struct cfq_group *pos = cfqg;
1d3650f71   Tejun Heo   cfq-iosched: impl...
1215
  	struct cfq_group *parent;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1216
1217
1218
  	bool propagate;
  
  	/* add to the service tree */
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1219
  	BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
7b5af5cff   Toshiaki Makita   cfq-iosched: Add ...
1220
1221
1222
1223
1224
  	/*
  	 * 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 ...
1225
  	cfq_update_group_leaf_weight(cfqg);
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1226
  	__cfq_group_service_tree_add(st, cfqg);
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1227
1228
  
  	/*
1d3650f71   Tejun Heo   cfq-iosched: impl...
1229
1230
1231
1232
1233
1234
1235
  	 * 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...
1236
1237
1238
  	 */
  	propagate = !pos->nr_active++;
  	pos->children_weight += pos->leaf_weight;
1d3650f71   Tejun Heo   cfq-iosched: impl...
1239
  	vfr = vfr * pos->leaf_weight / pos->children_weight;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1240

1d3650f71   Tejun Heo   cfq-iosched: impl...
1241
1242
1243
1244
1245
1246
  	/*
  	 * 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...
1247
  	while ((parent = cfqg_parent(pos))) {
1d3650f71   Tejun Heo   cfq-iosched: impl...
1248
  		if (propagate) {
e15693ef1   Toshiaki Makita   cfq-iosched: Fix ...
1249
  			cfq_update_group_weight(pos);
1d3650f71   Tejun Heo   cfq-iosched: impl...
1250
1251
1252
1253
  			propagate = !parent->nr_active++;
  			parent->children_weight += pos->weight;
  		}
  		vfr = vfr * pos->weight / parent->children_weight;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1254
1255
  		pos = parent;
  	}
1d3650f71   Tejun Heo   cfq-iosched: impl...
1256
1257
  
  	cfqg->vfraction = max_t(unsigned, vfr, 1);
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1258
  }
5be6b7561   Hou Tao   cfq-iosched: fix ...
1259
1260
1261
1262
1263
1264
1265
  static inline u64 cfq_get_cfqg_vdisktime_delay(struct cfq_data *cfqd)
  {
  	if (!iops_mode(cfqd))
  		return CFQ_SLICE_MODE_GROUP_DELAY;
  	else
  		return CFQ_IOPS_MODE_GROUP_DELAY;
  }
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1266
1267
  static void
  cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1268
1269
1270
1271
1272
1273
  {
  	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 ...
1274
  	if (!RB_EMPTY_NODE(&cfqg->rb_node))
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1275
1276
1277
1278
1279
  		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...
1280
  	 * if group does not loose all if it was not continuously backlogged.
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1281
  	 */
f0f1a45f9   Davidlohr Bueso   block/cfq: cache ...
1282
  	n = st->rb_rightmost;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1283
1284
  	if (n) {
  		__cfqg = rb_entry_cfqg(n);
5be6b7561   Hou Tao   cfq-iosched: fix ...
1285
1286
  		cfqg->vdisktime = __cfqg->vdisktime +
  			cfq_get_cfqg_vdisktime_delay(cfqd);
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1287
1288
  	} else
  		cfqg->vdisktime = st->min_vdisktime;
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1289
1290
  	cfq_group_service_tree_add(st, cfqg);
  }
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1291

8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1292
1293
1294
  static void
  cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
  {
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
  	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...
1306
  		struct cfq_group *parent = cfqg_parent(pos);
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1307
1308
1309
  
  		/* @pos has 0 nr_active at this point */
  		WARN_ON_ONCE(pos->children_weight);
1d3650f71   Tejun Heo   cfq-iosched: impl...
1310
  		pos->vfraction = 0;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
  
  		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'...
1321
1322
  	if (!RB_EMPTY_NODE(&cfqg->rb_node))
  		cfq_rb_erase(&cfqg->rb_node, st);
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1323
1324
1325
  }
  
  static void
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1326
  cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1327
1328
1329
1330
1331
  {
  	struct cfq_rb_root *st = &cfqd->grp_service_tree;
  
  	BUG_ON(cfqg->nr_cfqq < 1);
  	cfqg->nr_cfqq--;
25bc6b077   Vivek Goyal   blkio: Introduce ...
1332

1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1333
1334
1335
  	/* If there are other cfq queues under this group, don't delete it */
  	if (cfqg->nr_cfqq)
  		return;
2868ef7b3   Vivek Goyal   blkio: Some debug...
1336
  	cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1337
  	cfq_group_service_tree_del(st, cfqg);
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
1338
  	cfqg->saved_wl_slice = 0;
155fead9b   Tejun Heo   blkcg: move blkio...
1339
  	cfqg_stats_update_dequeue(cfqg);
dae739ebc   Vivek Goyal   blkio: Group time...
1340
  }
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1341
1342
  static inline u64 cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
  				       u64 *unaccounted_time)
dae739ebc   Vivek Goyal   blkio: Group time...
1343
  {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1344
1345
  	u64 slice_used;
  	u64 now = ktime_get_ns();
dae739ebc   Vivek Goyal   blkio: Group time...
1346
1347
1348
1349
1350
  
  	/*
  	 * Queue got expired before even a single request completed or
  	 * got expired immediately after first request completion.
  	 */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1351
  	if (!cfqq->slice_start || cfqq->slice_start == now) {
dae739ebc   Vivek Goyal   blkio: Group time...
1352
1353
1354
1355
1356
1357
  		/*
  		 * 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.
  		 */
0b31c10c6   Jan Kara   cfq-iosched: Char...
1358
1359
  		slice_used = max_t(u64, (now - cfqq->dispatch_start),
  					jiffies_to_nsecs(1));
dae739ebc   Vivek Goyal   blkio: Group time...
1360
  	} else {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1361
  		slice_used = now - cfqq->slice_start;
167400d34   Justin TerAvest   blk-cgroup: Add u...
1362
1363
  		if (slice_used > cfqq->allocated_slice) {
  			*unaccounted_time = slice_used - cfqq->allocated_slice;
f75edf2dc   Vivek Goyal   blkio: Wait for c...
1364
  			slice_used = cfqq->allocated_slice;
167400d34   Justin TerAvest   blk-cgroup: Add u...
1365
  		}
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1366
  		if (cfqq->slice_start > cfqq->dispatch_start)
167400d34   Justin TerAvest   blk-cgroup: Add u...
1367
1368
  			*unaccounted_time += cfqq->slice_start -
  					cfqq->dispatch_start;
dae739ebc   Vivek Goyal   blkio: Group time...
1369
  	}
dae739ebc   Vivek Goyal   blkio: Group time...
1370
1371
1372
1373
  	return slice_used;
  }
  
  static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
1374
  				struct cfq_queue *cfqq)
dae739ebc   Vivek Goyal   blkio: Group time...
1375
1376
  {
  	struct cfq_rb_root *st = &cfqd->grp_service_tree;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1377
  	u64 used_sl, charge, unaccounted_sl = 0;
f26bd1f0a   Vivek Goyal   blkio: Determine ...
1378
1379
  	int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
  			- cfqg->service_tree_idle.count;
1d3650f71   Tejun Heo   cfq-iosched: impl...
1380
  	unsigned int vfr;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1381
  	u64 now = ktime_get_ns();
f26bd1f0a   Vivek Goyal   blkio: Determine ...
1382
1383
  
  	BUG_ON(nr_sync < 0);
167400d34   Justin TerAvest   blk-cgroup: Add u...
1384
  	used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
dae739ebc   Vivek Goyal   blkio: Group time...
1385

02b35081f   Vivek Goyal   cfq-iosched: Do g...
1386
1387
1388
1389
  	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...
1390

1d3650f71   Tejun Heo   cfq-iosched: impl...
1391
1392
1393
1394
1395
1396
1397
  	/*
  	 * 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'...
1398
  	cfq_group_service_tree_del(st, cfqg);
1d3650f71   Tejun Heo   cfq-iosched: impl...
1399
  	cfqg->vdisktime += cfqg_scale_charge(charge, vfr);
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1400
  	cfq_group_service_tree_add(st, cfqg);
dae739ebc   Vivek Goyal   blkio: Group time...
1401
1402
  
  	/* This group is being expired. Save the context */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1403
1404
  	if (cfqd->workload_expires > now) {
  		cfqg->saved_wl_slice = cfqd->workload_expires - now;
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
1405
1406
  		cfqg->saved_wl_type = cfqd->serving_wl_type;
  		cfqg->saved_wl_class = cfqd->serving_wl_class;
dae739ebc   Vivek Goyal   blkio: Group time...
1407
  	} else
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
1408
  		cfqg->saved_wl_slice = 0;
2868ef7b3   Vivek Goyal   blkio: Some debug...
1409
1410
1411
  
  	cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
  					st->min_vdisktime);
fd16d2631   Joe Perches   block: Add __attr...
1412
  	cfq_log_cfqq(cfqq->cfqd, cfqq,
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1413
  		     "sl_used=%llu disp=%llu charge=%llu iops=%u sect=%lu",
fd16d2631   Joe Perches   block: Add __attr...
1414
1415
  		     used_sl, cfqq->slice_dispatch, charge,
  		     iops_mode(cfqd), cfqq->nr_sectors);
155fead9b   Tejun Heo   blkcg: move blkio...
1416
1417
  	cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
  	cfqg_stats_set_start_empty_time(cfqg);
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1418
  }
f51b802c1   Tejun Heo   blkcg: use the us...
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
  /**
   * 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);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1434
  	cfqg->ttime.last_end_request = ktime_get_ns();
f51b802c1   Tejun Heo   blkcg: use the us...
1435
  }
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1436
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1437
1438
  static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
  			    bool on_dfl, bool reset_dev, bool is_leaf_weight);
24bdb8ef0   Tejun Heo   blkcg: make blkcg...
1439
  static void cfqg_stats_exit(struct cfqg_stats *stats)
90d3839b9   Peter Zijlstra   block: Use u64_st...
1440
  {
24bdb8ef0   Tejun Heo   blkcg: make blkcg...
1441
1442
1443
1444
  	blkg_rwstat_exit(&stats->merged);
  	blkg_rwstat_exit(&stats->service_time);
  	blkg_rwstat_exit(&stats->wait_time);
  	blkg_rwstat_exit(&stats->queued);
24bdb8ef0   Tejun Heo   blkcg: make blkcg...
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
  	blkg_stat_exit(&stats->time);
  #ifdef CONFIG_DEBUG_BLK_CGROUP
  	blkg_stat_exit(&stats->unaccounted_time);
  	blkg_stat_exit(&stats->avg_queue_size_sum);
  	blkg_stat_exit(&stats->avg_queue_size_samples);
  	blkg_stat_exit(&stats->dequeue);
  	blkg_stat_exit(&stats->group_wait_time);
  	blkg_stat_exit(&stats->idle_time);
  	blkg_stat_exit(&stats->empty_time);
  #endif
  }
  
  static int cfqg_stats_init(struct cfqg_stats *stats, gfp_t gfp)
  {
77ea73388   Tejun Heo   blkcg: move io_se...
1459
  	if (blkg_rwstat_init(&stats->merged, gfp) ||
24bdb8ef0   Tejun Heo   blkcg: make blkcg...
1460
1461
1462
  	    blkg_rwstat_init(&stats->service_time, gfp) ||
  	    blkg_rwstat_init(&stats->wait_time, gfp) ||
  	    blkg_rwstat_init(&stats->queued, gfp) ||
24bdb8ef0   Tejun Heo   blkcg: make blkcg...
1463
1464
  	    blkg_stat_init(&stats->time, gfp))
  		goto err;
90d3839b9   Peter Zijlstra   block: Use u64_st...
1465
1466
  
  #ifdef CONFIG_DEBUG_BLK_CGROUP
24bdb8ef0   Tejun Heo   blkcg: make blkcg...
1467
1468
1469
1470
1471
1472
1473
1474
  	if (blkg_stat_init(&stats->unaccounted_time, gfp) ||
  	    blkg_stat_init(&stats->avg_queue_size_sum, gfp) ||
  	    blkg_stat_init(&stats->avg_queue_size_samples, gfp) ||
  	    blkg_stat_init(&stats->dequeue, gfp) ||
  	    blkg_stat_init(&stats->group_wait_time, gfp) ||
  	    blkg_stat_init(&stats->idle_time, gfp) ||
  	    blkg_stat_init(&stats->empty_time, gfp))
  		goto err;
90d3839b9   Peter Zijlstra   block: Use u64_st...
1475
  #endif
24bdb8ef0   Tejun Heo   blkcg: make blkcg...
1476
1477
1478
1479
  	return 0;
  err:
  	cfqg_stats_exit(stats);
  	return -ENOMEM;
90d3839b9   Peter Zijlstra   block: Use u64_st...
1480
  }
e4a9bde95   Tejun Heo   blkcg: replace bl...
1481
1482
1483
  static struct blkcg_policy_data *cfq_cpd_alloc(gfp_t gfp)
  {
  	struct cfq_group_data *cgd;
ebc4ff661   Tejun Heo   block: cfq_cpd_al...
1484
  	cgd = kzalloc(sizeof(*cgd), gfp);
e4a9bde95   Tejun Heo   blkcg: replace bl...
1485
1486
1487
1488
  	if (!cgd)
  		return NULL;
  	return &cgd->cpd;
  }
814376483   Tejun Heo   blkcg: minor upda...
1489
  static void cfq_cpd_init(struct blkcg_policy_data *cpd)
e48453c38   Arianna Avanzini   block, cgroup: im...
1490
  {
814376483   Tejun Heo   blkcg: minor upda...
1491
  	struct cfq_group_data *cgd = cpd_to_cfqgd(cpd);
9e10a130d   Tejun Heo   cgroup: replace c...
1492
  	unsigned int weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ?
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1493
  			      CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
e48453c38   Arianna Avanzini   block, cgroup: im...
1494

69d7fde59   Tejun Heo   blkcg: use CGROUP...
1495
1496
1497
1498
1499
  	if (cpd_to_blkcg(cpd) == &blkcg_root)
  		weight *= 2;
  
  	cgd->weight = weight;
  	cgd->leaf_weight = weight;
e48453c38   Arianna Avanzini   block, cgroup: im...
1500
  }
e4a9bde95   Tejun Heo   blkcg: replace bl...
1501
1502
1503
1504
  static void cfq_cpd_free(struct blkcg_policy_data *cpd)
  {
  	kfree(cpd_to_cfqgd(cpd));
  }
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1505
1506
1507
  static void cfq_cpd_bind(struct blkcg_policy_data *cpd)
  {
  	struct blkcg *blkcg = cpd_to_blkcg(cpd);
9e10a130d   Tejun Heo   cgroup: replace c...
1508
  	bool on_dfl = cgroup_subsys_on_dfl(io_cgrp_subsys);
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1509
1510
1511
1512
1513
1514
1515
1516
  	unsigned int weight = on_dfl ? CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
  
  	if (blkcg == &blkcg_root)
  		weight *= 2;
  
  	WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, false));
  	WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, true));
  }
001bea73e   Tejun Heo   blkcg: replace bl...
1517
1518
  static struct blkg_policy_data *cfq_pd_alloc(gfp_t gfp, int node)
  {
b2ce2643c   Tejun Heo   blk-throttle: cle...
1519
1520
1521
1522
1523
1524
1525
  	struct cfq_group *cfqg;
  
  	cfqg = kzalloc_node(sizeof(*cfqg), gfp, node);
  	if (!cfqg)
  		return NULL;
  
  	cfq_init_cfqg_base(cfqg);
24bdb8ef0   Tejun Heo   blkcg: make blkcg...
1526
1527
1528
1529
  	if (cfqg_stats_init(&cfqg->stats, gfp)) {
  		kfree(cfqg);
  		return NULL;
  	}
b2ce2643c   Tejun Heo   blk-throttle: cle...
1530
1531
  
  	return &cfqg->pd;
001bea73e   Tejun Heo   blkcg: replace bl...
1532
  }
a9520cd6f   Tejun Heo   blkcg: make blkcg...
1533
  static void cfq_pd_init(struct blkg_policy_data *pd)
f469a7b4d   Vivek Goyal   blk-cgroup: Allow...
1534
  {
a9520cd6f   Tejun Heo   blkcg: make blkcg...
1535
1536
  	struct cfq_group *cfqg = pd_to_cfqg(pd);
  	struct cfq_group_data *cgd = blkcg_to_cfqgd(pd->blkg->blkcg);
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1537

e48453c38   Arianna Avanzini   block, cgroup: im...
1538
1539
  	cfqg->weight = cgd->weight;
  	cfqg->leaf_weight = cgd->leaf_weight;
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1540
  }
a9520cd6f   Tejun Heo   blkcg: make blkcg...
1541
  static void cfq_pd_offline(struct blkg_policy_data *pd)
0b39920b5   Tejun Heo   cfq-iosched: coll...
1542
  {
a9520cd6f   Tejun Heo   blkcg: make blkcg...
1543
  	struct cfq_group *cfqg = pd_to_cfqg(pd);
60a837077   Tejun Heo   cfq-iosched: char...
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
  	int i;
  
  	for (i = 0; i < IOPRIO_BE_NR; i++) {
  		if (cfqg->async_cfqq[0][i])
  			cfq_put_queue(cfqg->async_cfqq[0][i]);
  		if (cfqg->async_cfqq[1][i])
  			cfq_put_queue(cfqg->async_cfqq[1][i]);
  	}
  
  	if (cfqg->async_idle_cfqq)
  		cfq_put_queue(cfqg->async_idle_cfqq);
0b39920b5   Tejun Heo   cfq-iosched: coll...
1555
1556
1557
1558
1559
1560
  	/*
  	 * @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...
  	 */
60a837077   Tejun Heo   cfq-iosched: char...
1561
  	cfqg_stats_xfer_dead(cfqg);
0b39920b5   Tejun Heo   cfq-iosched: coll...
1562
  }
001bea73e   Tejun Heo   blkcg: replace bl...
1563
1564
  static void cfq_pd_free(struct blkg_policy_data *pd)
  {
24bdb8ef0   Tejun Heo   blkcg: make blkcg...
1565
1566
1567
1568
  	struct cfq_group *cfqg = pd_to_cfqg(pd);
  
  	cfqg_stats_exit(&cfqg->stats);
  	return kfree(cfqg);
001bea73e   Tejun Heo   blkcg: replace bl...
1569
  }
a9520cd6f   Tejun Heo   blkcg: make blkcg...
1570
  static void cfq_pd_reset_stats(struct blkg_policy_data *pd)
689665af4   Tejun Heo   cfq-iosched: sepa...
1571
  {
a9520cd6f   Tejun Heo   blkcg: make blkcg...
1572
  	struct cfq_group *cfqg = pd_to_cfqg(pd);
689665af4   Tejun Heo   cfq-iosched: sepa...
1573
1574
  
  	cfqg_stats_reset(&cfqg->stats);
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1575
  }
ae1188963   Tejun Heo   blkcg: consolidat...
1576
1577
  static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
  					 struct blkcg *blkcg)
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1578
  {
ae1188963   Tejun Heo   blkcg: consolidat...
1579
  	struct blkcg_gq *blkg;
f469a7b4d   Vivek Goyal   blk-cgroup: Allow...
1580

ae1188963   Tejun Heo   blkcg: consolidat...
1581
1582
1583
1584
  	blkg = blkg_lookup(blkcg, cfqd->queue);
  	if (likely(blkg))
  		return blkg_to_cfqg(blkg);
  	return NULL;
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1585
1586
1587
1588
  }
  
  static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
  {
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1589
  	cfqq->cfqg = cfqg;
b1c357696   Vivek Goyal   blkio: Take care ...
1590
  	/* cfqq reference on cfqg */
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
1591
  	cfqg_get(cfqg);
b1c357696   Vivek Goyal   blkio: Take care ...
1592
  }
f95a04afa   Tejun Heo   blkcg: embed stru...
1593
1594
  static u64 cfqg_prfill_weight_device(struct seq_file *sf,
  				     struct blkg_policy_data *pd, int off)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1595
  {
f95a04afa   Tejun Heo   blkcg: embed stru...
1596
  	struct cfq_group *cfqg = pd_to_cfqg(pd);
3381cb8d2   Tejun Heo   blkcg: move blkio...
1597
1598
  
  	if (!cfqg->dev_weight)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1599
  		return 0;
f95a04afa   Tejun Heo   blkcg: embed stru...
1600
  	return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1601
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1602
  static int cfqg_print_weight_device(struct seq_file *sf, void *v)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1603
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1604
1605
1606
  	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/...
1607
1608
  	return 0;
  }
e71357e11   Tejun Heo   cfq-iosched: add ...
1609
1610
1611
1612
1613
1614
1615
1616
1617
  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...
1618
  static int cfqg_print_leaf_weight_device(struct seq_file *sf, void *v)
e71357e11   Tejun Heo   cfq-iosched: add ...
1619
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1620
1621
1622
  	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 ...
1623
1624
  	return 0;
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1625
  static int cfq_print_weight(struct seq_file *sf, void *v)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1626
  {
e48453c38   Arianna Avanzini   block, cgroup: im...
1627
  	struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
9470e4a69   Jens Axboe   cfq-iosched: fix ...
1628
1629
  	struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
  	unsigned int val = 0;
e48453c38   Arianna Avanzini   block, cgroup: im...
1630

9470e4a69   Jens Axboe   cfq-iosched: fix ...
1631
1632
1633
1634
1635
  	if (cgd)
  		val = cgd->weight;
  
  	seq_printf(sf, "%u
  ", val);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1636
1637
  	return 0;
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1638
  static int cfq_print_leaf_weight(struct seq_file *sf, void *v)
e71357e11   Tejun Heo   cfq-iosched: add ...
1639
  {
e48453c38   Arianna Avanzini   block, cgroup: im...
1640
  	struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
9470e4a69   Jens Axboe   cfq-iosched: fix ...
1641
1642
1643
1644
1645
  	struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
  	unsigned int val = 0;
  
  	if (cgd)
  		val = cgd->leaf_weight;
e48453c38   Arianna Avanzini   block, cgroup: im...
1646

9470e4a69   Jens Axboe   cfq-iosched: fix ...
1647
1648
  	seq_printf(sf, "%u
  ", val);
e71357e11   Tejun Heo   cfq-iosched: add ...
1649
1650
  	return 0;
  }
451af504d   Tejun Heo   cgroup: replace c...
1651
1652
  static ssize_t __cfqg_set_weight_device(struct kernfs_open_file *of,
  					char *buf, size_t nbytes, loff_t off,
2ee867dcf   Tejun Heo   blkcg: implement ...
1653
  					bool on_dfl, bool is_leaf_weight)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1654
  {
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1655
1656
  	unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
  	unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
451af504d   Tejun Heo   cgroup: replace c...
1657
  	struct blkcg *blkcg = css_to_blkcg(of_css(of));
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1658
  	struct blkg_conf_ctx ctx;
3381cb8d2   Tejun Heo   blkcg: move blkio...
1659
  	struct cfq_group *cfqg;
e48453c38   Arianna Avanzini   block, cgroup: im...
1660
  	struct cfq_group_data *cfqgd;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1661
  	int ret;
36aa9e5f5   Tejun Heo   blkcg: move body ...
1662
  	u64 v;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1663

3c798398e   Tejun Heo   blkcg: mass renam...
1664
  	ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1665
1666
  	if (ret)
  		return ret;
2ee867dcf   Tejun Heo   blkcg: implement ...
1667
1668
1669
1670
1671
1672
1673
1674
1675
  	if (sscanf(ctx.body, "%llu", &v) == 1) {
  		/* require "default" on dfl */
  		ret = -ERANGE;
  		if (!v && on_dfl)
  			goto out_finish;
  	} else if (!strcmp(strim(ctx.body), "default")) {
  		v = 0;
  	} else {
  		ret = -EINVAL;
36aa9e5f5   Tejun Heo   blkcg: move body ...
1676
  		goto out_finish;
2ee867dcf   Tejun Heo   blkcg: implement ...
1677
  	}
36aa9e5f5   Tejun Heo   blkcg: move body ...
1678

3381cb8d2   Tejun Heo   blkcg: move blkio...
1679
  	cfqg = blkg_to_cfqg(ctx.blkg);
e48453c38   Arianna Avanzini   block, cgroup: im...
1680
  	cfqgd = blkcg_to_cfqgd(blkcg);
ae994ea97   Jens Axboe   cfq-iosched: fix ...
1681

20386ce01   Tejun Heo   blkcg: refine err...
1682
  	ret = -ERANGE;
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1683
  	if (!v || (v >= min && v <= max)) {
e71357e11   Tejun Heo   cfq-iosched: add ...
1684
  		if (!is_leaf_weight) {
36aa9e5f5   Tejun Heo   blkcg: move body ...
1685
1686
  			cfqg->dev_weight = v;
  			cfqg->new_weight = v ?: cfqgd->weight;
e71357e11   Tejun Heo   cfq-iosched: add ...
1687
  		} else {
36aa9e5f5   Tejun Heo   blkcg: move body ...
1688
1689
  			cfqg->dev_leaf_weight = v;
  			cfqg->new_leaf_weight = v ?: cfqgd->leaf_weight;
e71357e11   Tejun Heo   cfq-iosched: add ...
1690
  		}
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1691
1692
  		ret = 0;
  	}
36aa9e5f5   Tejun Heo   blkcg: move body ...
1693
  out_finish:
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1694
  	blkg_conf_finish(&ctx);
451af504d   Tejun Heo   cgroup: replace c...
1695
  	return ret ?: nbytes;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1696
  }
451af504d   Tejun Heo   cgroup: replace c...
1697
1698
  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 ...
1699
  {
2ee867dcf   Tejun Heo   blkcg: implement ...
1700
  	return __cfqg_set_weight_device(of, buf, nbytes, off, false, false);
e71357e11   Tejun Heo   cfq-iosched: add ...
1701
  }
451af504d   Tejun Heo   cgroup: replace c...
1702
1703
  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 ...
1704
  {
2ee867dcf   Tejun Heo   blkcg: implement ...
1705
  	return __cfqg_set_weight_device(of, buf, nbytes, off, false, true);
e71357e11   Tejun Heo   cfq-iosched: add ...
1706
  }
dd165eb3b   Tejun Heo   blkcg: misc prepa...
1707
  static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1708
  			    bool on_dfl, bool reset_dev, bool is_leaf_weight)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1709
  {
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1710
1711
  	unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
  	unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
182446d08   Tejun Heo   cgroup: pass arou...
1712
  	struct blkcg *blkcg = css_to_blkcg(css);
3c798398e   Tejun Heo   blkcg: mass renam...
1713
  	struct blkcg_gq *blkg;
e48453c38   Arianna Avanzini   block, cgroup: im...
1714
  	struct cfq_group_data *cfqgd;
ae994ea97   Jens Axboe   cfq-iosched: fix ...
1715
  	int ret = 0;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1716

69d7fde59   Tejun Heo   blkcg: use CGROUP...
1717
1718
  	if (val < min || val > max)
  		return -ERANGE;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1719
1720
  
  	spin_lock_irq(&blkcg->lock);
e48453c38   Arianna Avanzini   block, cgroup: im...
1721
  	cfqgd = blkcg_to_cfqgd(blkcg);
ae994ea97   Jens Axboe   cfq-iosched: fix ...
1722
1723
1724
1725
  	if (!cfqgd) {
  		ret = -EINVAL;
  		goto out;
  	}
e71357e11   Tejun Heo   cfq-iosched: add ...
1726
1727
  
  	if (!is_leaf_weight)
e48453c38   Arianna Avanzini   block, cgroup: im...
1728
  		cfqgd->weight = val;
e71357e11   Tejun Heo   cfq-iosched: add ...
1729
  	else
e48453c38   Arianna Avanzini   block, cgroup: im...
1730
  		cfqgd->leaf_weight = val;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1731

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

e71357e11   Tejun Heo   cfq-iosched: add ...
1735
1736
1737
1738
  		if (!cfqg)
  			continue;
  
  		if (!is_leaf_weight) {
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1739
1740
  			if (reset_dev)
  				cfqg->dev_weight = 0;
e71357e11   Tejun Heo   cfq-iosched: add ...
1741
  			if (!cfqg->dev_weight)
e48453c38   Arianna Avanzini   block, cgroup: im...
1742
  				cfqg->new_weight = cfqgd->weight;
e71357e11   Tejun Heo   cfq-iosched: add ...
1743
  		} else {
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1744
1745
  			if (reset_dev)
  				cfqg->dev_leaf_weight = 0;
e71357e11   Tejun Heo   cfq-iosched: add ...
1746
  			if (!cfqg->dev_leaf_weight)
e48453c38   Arianna Avanzini   block, cgroup: im...
1747
  				cfqg->new_leaf_weight = cfqgd->leaf_weight;
e71357e11   Tejun Heo   cfq-iosched: add ...
1748
  		}
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1749
  	}
ae994ea97   Jens Axboe   cfq-iosched: fix ...
1750
  out:
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1751
  	spin_unlock_irq(&blkcg->lock);
ae994ea97   Jens Axboe   cfq-iosched: fix ...
1752
  	return ret;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1753
  }
182446d08   Tejun Heo   cgroup: pass arou...
1754
1755
  static int cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft,
  			  u64 val)
e71357e11   Tejun Heo   cfq-iosched: add ...
1756
  {
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1757
  	return __cfq_set_weight(css, val, false, false, false);
e71357e11   Tejun Heo   cfq-iosched: add ...
1758
  }
182446d08   Tejun Heo   cgroup: pass arou...
1759
1760
  static int cfq_set_leaf_weight(struct cgroup_subsys_state *css,
  			       struct cftype *cft, u64 val)
e71357e11   Tejun Heo   cfq-iosched: add ...
1761
  {
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1762
  	return __cfq_set_weight(css, val, false, false, true);
e71357e11   Tejun Heo   cfq-iosched: add ...
1763
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1764
  static int cfqg_print_stat(struct seq_file *sf, void *v)
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1765
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1766
1767
  	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...
1768
1769
  	return 0;
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1770
  static int cfqg_print_rwstat(struct seq_file *sf, void *v)
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1771
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1772
1773
  	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...
1774
1775
  	return 0;
  }
43114018c   Tejun Heo   cfq-iosched: add ...
1776
1777
1778
  static u64 cfqg_prfill_stat_recursive(struct seq_file *sf,
  				      struct blkg_policy_data *pd, int off)
  {
f12c74cab   Tejun Heo   blkcg: make blkg_...
1779
1780
  	u64 sum = blkg_stat_recursive_sum(pd_to_blkg(pd),
  					  &blkcg_policy_cfq, off);
43114018c   Tejun Heo   cfq-iosched: add ...
1781
1782
1783
1784
1785
1786
  	return __blkg_prfill_u64(sf, pd, sum);
  }
  
  static u64 cfqg_prfill_rwstat_recursive(struct seq_file *sf,
  					struct blkg_policy_data *pd, int off)
  {
f12c74cab   Tejun Heo   blkcg: make blkg_...
1787
1788
  	struct blkg_rwstat sum = blkg_rwstat_recursive_sum(pd_to_blkg(pd),
  							&blkcg_policy_cfq, off);
43114018c   Tejun Heo   cfq-iosched: add ...
1789
1790
  	return __blkg_prfill_rwstat(sf, pd, &sum);
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1791
  static int cfqg_print_stat_recursive(struct seq_file *sf, void *v)
43114018c   Tejun Heo   cfq-iosched: add ...
1792
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1793
1794
1795
  	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 ...
1796
1797
  	return 0;
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1798
  static int cfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
43114018c   Tejun Heo   cfq-iosched: add ...
1799
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1800
1801
1802
  	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 ...
1803
1804
  	return 0;
  }
702747cab   Tejun Heo   blkcg: remove cfq...
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
  static u64 cfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd,
  			       int off)
  {
  	u64 sum = blkg_rwstat_total(&pd->blkg->stat_bytes);
  
  	return __blkg_prfill_u64(sf, pd, sum >> 9);
  }
  
  static int cfqg_print_stat_sectors(struct seq_file *sf, void *v)
  {
  	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  			  cfqg_prfill_sectors, &blkcg_policy_cfq, 0, false);
  	return 0;
  }
  
  static u64 cfqg_prfill_sectors_recursive(struct seq_file *sf,
  					 struct blkg_policy_data *pd, int off)
  {
  	struct blkg_rwstat tmp = blkg_rwstat_recursive_sum(pd->blkg, NULL,
  					offsetof(struct blkcg_gq, stat_bytes));
  	u64 sum = atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) +
  		atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]);
  
  	return __blkg_prfill_u64(sf, pd, sum >> 9);
  }
  
  static int cfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v)
  {
  	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  			  cfqg_prfill_sectors_recursive, &blkcg_policy_cfq, 0,
  			  false);
  	return 0;
  }
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1838
  #ifdef CONFIG_DEBUG_BLK_CGROUP
f95a04afa   Tejun Heo   blkcg: embed stru...
1839
1840
  static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
  				      struct blkg_policy_data *pd, int off)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1841
  {
f95a04afa   Tejun Heo   blkcg: embed stru...
1842
  	struct cfq_group *cfqg = pd_to_cfqg(pd);
155fead9b   Tejun Heo   blkcg: move blkio...
1843
  	u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1844
1845
1846
  	u64 v = 0;
  
  	if (samples) {
155fead9b   Tejun Heo   blkcg: move blkio...
1847
  		v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
f3cff25f0   Anatol Pomozov   cfq: explicitly u...
1848
  		v = div64_u64(v, samples);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1849
  	}
f95a04afa   Tejun Heo   blkcg: embed stru...
1850
  	__blkg_prfill_u64(sf, pd, v);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1851
1852
1853
1854
  	return 0;
  }
  
  /* print avg_queue_size */
2da8ca822   Tejun Heo   cgroup: replace c...
1855
  static int cfqg_print_avg_queue_size(struct seq_file *sf, void *v)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1856
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1857
1858
1859
  	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/...
1860
1861
1862
  	return 0;
  }
  #endif	/* CONFIG_DEBUG_BLK_CGROUP */
880f50e22   Tejun Heo   blkcg: mark exist...
1863
  static struct cftype cfq_blkcg_legacy_files[] = {
1d3650f71   Tejun Heo   cfq-iosched: impl...
1864
  	/* on root, weight is mapped to leaf_weight */
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1865
1866
  	{
  		.name = "weight_device",
1d3650f71   Tejun Heo   cfq-iosched: impl...
1867
  		.flags = CFTYPE_ONLY_ON_ROOT,
2da8ca822   Tejun Heo   cgroup: replace c...
1868
  		.seq_show = cfqg_print_leaf_weight_device,
451af504d   Tejun Heo   cgroup: replace c...
1869
  		.write = cfqg_set_leaf_weight_device,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1870
1871
1872
  	},
  	{
  		.name = "weight",
1d3650f71   Tejun Heo   cfq-iosched: impl...
1873
  		.flags = CFTYPE_ONLY_ON_ROOT,
2da8ca822   Tejun Heo   cgroup: replace c...
1874
  		.seq_show = cfq_print_leaf_weight,
1d3650f71   Tejun Heo   cfq-iosched: impl...
1875
  		.write_u64 = cfq_set_leaf_weight,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1876
  	},
e71357e11   Tejun Heo   cfq-iosched: add ...
1877

1d3650f71   Tejun Heo   cfq-iosched: impl...
1878
  	/* no such mapping necessary for !roots */
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1879
1880
  	{
  		.name = "weight_device",
1d3650f71   Tejun Heo   cfq-iosched: impl...
1881
  		.flags = CFTYPE_NOT_ON_ROOT,
2da8ca822   Tejun Heo   cgroup: replace c...
1882
  		.seq_show = cfqg_print_weight_device,
451af504d   Tejun Heo   cgroup: replace c...
1883
  		.write = cfqg_set_weight_device,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1884
1885
1886
  	},
  	{
  		.name = "weight",
1d3650f71   Tejun Heo   cfq-iosched: impl...
1887
  		.flags = CFTYPE_NOT_ON_ROOT,
2da8ca822   Tejun Heo   cgroup: replace c...
1888
  		.seq_show = cfq_print_weight,
3381cb8d2   Tejun Heo   blkcg: move blkio...
1889
  		.write_u64 = cfq_set_weight,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1890
  	},
e71357e11   Tejun Heo   cfq-iosched: add ...
1891

e71357e11   Tejun Heo   cfq-iosched: add ...
1892
1893
  	{
  		.name = "leaf_weight_device",
2da8ca822   Tejun Heo   cgroup: replace c...
1894
  		.seq_show = cfqg_print_leaf_weight_device,
451af504d   Tejun Heo   cgroup: replace c...
1895
  		.write = cfqg_set_leaf_weight_device,
e71357e11   Tejun Heo   cfq-iosched: add ...
1896
1897
1898
  	},
  	{
  		.name = "leaf_weight",
2da8ca822   Tejun Heo   cgroup: replace c...
1899
  		.seq_show = cfq_print_leaf_weight,
e71357e11   Tejun Heo   cfq-iosched: add ...
1900
1901
  		.write_u64 = cfq_set_leaf_weight,
  	},
43114018c   Tejun Heo   cfq-iosched: add ...
1902
  	/* statistics, covers only the tasks in the cfqg */
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1903
1904
  	{
  		.name = "time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1905
  		.private = offsetof(struct cfq_group, stats.time),
2da8ca822   Tejun Heo   cgroup: replace c...
1906
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1907
1908
1909
  	},
  	{
  		.name = "sectors",
702747cab   Tejun Heo   blkcg: remove cfq...
1910
  		.seq_show = cfqg_print_stat_sectors,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1911
1912
1913
  	},
  	{
  		.name = "io_service_bytes",
77ea73388   Tejun Heo   blkcg: move io_se...
1914
1915
  		.private = (unsigned long)&blkcg_policy_cfq,
  		.seq_show = blkg_print_stat_bytes,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1916
1917
1918
  	},
  	{
  		.name = "io_serviced",
77ea73388   Tejun Heo   blkcg: move io_se...
1919
1920
  		.private = (unsigned long)&blkcg_policy_cfq,
  		.seq_show = blkg_print_stat_ios,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1921
1922
1923
  	},
  	{
  		.name = "io_service_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1924
  		.private = offsetof(struct cfq_group, stats.service_time),
2da8ca822   Tejun Heo   cgroup: replace c...
1925
  		.seq_show = cfqg_print_rwstat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1926
1927
1928
  	},
  	{
  		.name = "io_wait_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1929
  		.private = offsetof(struct cfq_group, stats.wait_time),
2da8ca822   Tejun Heo   cgroup: replace c...
1930
  		.seq_show = cfqg_print_rwstat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1931
1932
1933
  	},
  	{
  		.name = "io_merged",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1934
  		.private = offsetof(struct cfq_group, stats.merged),
2da8ca822   Tejun Heo   cgroup: replace c...
1935
  		.seq_show = cfqg_print_rwstat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1936
1937
1938
  	},
  	{
  		.name = "io_queued",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1939
  		.private = offsetof(struct cfq_group, stats.queued),
2da8ca822   Tejun Heo   cgroup: replace c...
1940
  		.seq_show = cfqg_print_rwstat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1941
  	},
43114018c   Tejun Heo   cfq-iosched: add ...
1942
1943
1944
1945
1946
  
  	/* 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...
1947
  		.seq_show = cfqg_print_stat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1948
1949
1950
  	},
  	{
  		.name = "sectors_recursive",
702747cab   Tejun Heo   blkcg: remove cfq...
1951
  		.seq_show = cfqg_print_stat_sectors_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1952
1953
1954
  	},
  	{
  		.name = "io_service_bytes_recursive",
77ea73388   Tejun Heo   blkcg: move io_se...
1955
1956
  		.private = (unsigned long)&blkcg_policy_cfq,
  		.seq_show = blkg_print_stat_bytes_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1957
1958
1959
  	},
  	{
  		.name = "io_serviced_recursive",
77ea73388   Tejun Heo   blkcg: move io_se...
1960
1961
  		.private = (unsigned long)&blkcg_policy_cfq,
  		.seq_show = blkg_print_stat_ios_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1962
1963
1964
1965
  	},
  	{
  		.name = "io_service_time_recursive",
  		.private = offsetof(struct cfq_group, stats.service_time),
2da8ca822   Tejun Heo   cgroup: replace c...
1966
  		.seq_show = cfqg_print_rwstat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1967
1968
1969
1970
  	},
  	{
  		.name = "io_wait_time_recursive",
  		.private = offsetof(struct cfq_group, stats.wait_time),
2da8ca822   Tejun Heo   cgroup: replace c...
1971
  		.seq_show = cfqg_print_rwstat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1972
1973
1974
1975
  	},
  	{
  		.name = "io_merged_recursive",
  		.private = offsetof(struct cfq_group, stats.merged),
2da8ca822   Tejun Heo   cgroup: replace c...
1976
  		.seq_show = cfqg_print_rwstat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1977
1978
1979
1980
  	},
  	{
  		.name = "io_queued_recursive",
  		.private = offsetof(struct cfq_group, stats.queued),
2da8ca822   Tejun Heo   cgroup: replace c...
1981
  		.seq_show = cfqg_print_rwstat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1982
  	},
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1983
1984
1985
  #ifdef CONFIG_DEBUG_BLK_CGROUP
  	{
  		.name = "avg_queue_size",
2da8ca822   Tejun Heo   cgroup: replace c...
1986
  		.seq_show = cfqg_print_avg_queue_size,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1987
1988
1989
  	},
  	{
  		.name = "group_wait_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1990
  		.private = offsetof(struct cfq_group, stats.group_wait_time),
2da8ca822   Tejun Heo   cgroup: replace c...
1991
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1992
1993
1994
  	},
  	{
  		.name = "idle_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1995
  		.private = offsetof(struct cfq_group, stats.idle_time),
2da8ca822   Tejun Heo   cgroup: replace c...
1996
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1997
1998
1999
  	},
  	{
  		.name = "empty_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
2000
  		.private = offsetof(struct cfq_group, stats.empty_time),
2da8ca822   Tejun Heo   cgroup: replace c...
2001
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
2002
2003
2004
  	},
  	{
  		.name = "dequeue",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
2005
  		.private = offsetof(struct cfq_group, stats.dequeue),
2da8ca822   Tejun Heo   cgroup: replace c...
2006
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
2007
2008
2009
  	},
  	{
  		.name = "unaccounted_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
2010
  		.private = offsetof(struct cfq_group, stats.unaccounted_time),
2da8ca822   Tejun Heo   cgroup: replace c...
2011
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
2012
2013
2014
2015
  	},
  #endif	/* CONFIG_DEBUG_BLK_CGROUP */
  	{ }	/* terminate */
  };
2ee867dcf   Tejun Heo   blkcg: implement ...
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
  
  static int cfq_print_weight_on_dfl(struct seq_file *sf, void *v)
  {
  	struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
  	struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
  
  	seq_printf(sf, "default %u
  ", cgd->weight);
  	blkcg_print_blkgs(sf, blkcg, cfqg_prfill_weight_device,
  			  &blkcg_policy_cfq, 0, false);
  	return 0;
  }
  
  static ssize_t cfq_set_weight_on_dfl(struct kernfs_open_file *of,
  				     char *buf, size_t nbytes, loff_t off)
  {
  	char *endp;
  	int ret;
  	u64 v;
  
  	buf = strim(buf);
  
  	/* "WEIGHT" or "default WEIGHT" sets the default weight */
  	v = simple_strtoull(buf, &endp, 0);
  	if (*endp == '\0' || sscanf(buf, "default %llu", &v) == 1) {
69d7fde59   Tejun Heo   blkcg: use CGROUP...
2041
  		ret = __cfq_set_weight(of_css(of), v, true, false, false);
2ee867dcf   Tejun Heo   blkcg: implement ...
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
  		return ret ?: nbytes;
  	}
  
  	/* "MAJ:MIN WEIGHT" */
  	return __cfqg_set_weight_device(of, buf, nbytes, off, true, false);
  }
  
  static struct cftype cfq_blkcg_files[] = {
  	{
  		.name = "weight",
  		.flags = CFTYPE_NOT_ON_ROOT,
  		.seq_show = cfq_print_weight_on_dfl,
  		.write = cfq_set_weight_on_dfl,
  	},
  	{ }	/* terminate */
  };
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
2058
  #else /* GROUP_IOSCHED */
ae1188963   Tejun Heo   blkcg: consolidat...
2059
2060
  static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
  					 struct blkcg *blkcg)
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
2061
  {
f51b802c1   Tejun Heo   blkcg: use the us...
2062
  	return cfqd->root_group;
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
2063
  }
7f1dc8a2d   Vivek Goyal   blkio: Fix blkio ...
2064

25fb5169d   Vivek Goyal   blkio: Dynamic cf...
2065
2066
2067
2068
2069
2070
  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...
2071
  /*
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
2072
   * The cfqd->service_trees holds all pending cfq_queue's that have
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2073
2074
2075
   * requests waiting to be processed. It is sorted in the order that
   * we will service the queues.
   */
a36e71f99   Jens Axboe   cfq-iosched: add ...
2076
  static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
a6151c3a5   Jens Axboe   cfq-iosched: appl...
2077
  				 bool add_front)
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2078
  {
0871714e0   Jens Axboe   cfq-iosched: rela...
2079
2080
  	struct rb_node **p, *parent;
  	struct cfq_queue *__cfqq;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2081
  	u64 rb_key;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2082
  	struct cfq_rb_root *st;
09663c86e   Davidlohr Bueso   block/cfq: replac...
2083
  	bool leftmost = true;
dae739ebc   Vivek Goyal   blkio: Group time...
2084
  	int new_cfqq = 1;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2085
  	u64 now = ktime_get_ns();
ae30c2865   Vivek Goyal   blkio: Implement ...
2086

34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2087
  	st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
0871714e0   Jens Axboe   cfq-iosched: rela...
2088
2089
  	if (cfq_class_idle(cfqq)) {
  		rb_key = CFQ_IDLE_DELAY;
f0f1a45f9   Davidlohr Bueso   block/cfq: cache ...
2090
  		parent = st->rb_rightmost;
0871714e0   Jens Axboe   cfq-iosched: rela...
2091
2092
2093
2094
  		if (parent && parent != &cfqq->rb_node) {
  			__cfqq = rb_entry(parent, struct cfq_queue, rb_node);
  			rb_key += __cfqq->rb_key;
  		} else
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2095
  			rb_key += now;
0871714e0   Jens Axboe   cfq-iosched: rela...
2096
  	} else if (!add_front) {
b9c8946b1   Jens Axboe   cfq-iosched: fix ...
2097
2098
2099
2100
2101
2102
  		/*
  		 * 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.
  		 */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2103
  		rb_key = cfq_slice_offset(cfqd, cfqq) + now;
b9c8946b1   Jens Axboe   cfq-iosched: fix ...
2104
  		rb_key -= cfqq->slice_resid;
edd75ffd9   Jens Axboe   cfq-iosched: get ...
2105
  		cfqq->slice_resid = 0;
48e025e63   Corrado Zoccolo   cfq-iosched: fix ...
2106
  	} else {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2107
  		rb_key = -NSEC_PER_SEC;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2108
  		__cfqq = cfq_rb_first(st);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2109
  		rb_key += __cfqq ? __cfqq->rb_key : now;
48e025e63   Corrado Zoccolo   cfq-iosched: fix ...
2110
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2111

d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2112
  	if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
dae739ebc   Vivek Goyal   blkio: Group time...
2113
  		new_cfqq = 0;
99f9628ab   Jens Axboe   [PATCH] cfq-iosch...
2114
  		/*
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2115
  		 * same position, nothing more to do
99f9628ab   Jens Axboe   [PATCH] cfq-iosch...
2116
  		 */
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2117
  		if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2118
  			return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2119

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

0871714e0   Jens Axboe   cfq-iosched: rela...
2124
  	parent = NULL;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2125
  	cfqq->service_tree = st;
09663c86e   Davidlohr Bueso   block/cfq: replac...
2126
  	p = &st->rb.rb_root.rb_node;
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2127
2128
2129
  	while (*p) {
  		parent = *p;
  		__cfqq = rb_entry(parent, struct cfq_queue, rb_node);
0c534e0a4   Jens Axboe   cfq-iosched: sort...
2130
  		/*
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
2131
  		 * sort by key, that represents service time.
0c534e0a4   Jens Axboe   cfq-iosched: sort...
2132
  		 */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2133
  		if (rb_key < __cfqq->rb_key)
1f23f1215   Vivek Goyal   cfq-iosched: Get ...
2134
  			p = &parent->rb_left;
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
2135
  		else {
1f23f1215   Vivek Goyal   cfq-iosched: Get ...
2136
  			p = &parent->rb_right;
09663c86e   Davidlohr Bueso   block/cfq: replac...
2137
  			leftmost = false;
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
2138
  		}
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2139
2140
2141
2142
  	}
  
  	cfqq->rb_key = rb_key;
  	rb_link_node(&cfqq->rb_node, parent, p);
09663c86e   Davidlohr Bueso   block/cfq: replac...
2143
  	rb_insert_color_cached(&cfqq->rb_node, &st->rb, leftmost);
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2144
  	st->count++;
20359f27e   Namhyung Kim   cfq-iosched: remo...
2145
  	if (add_front || !new_cfqq)
dae739ebc   Vivek Goyal   blkio: Group time...
2146
  		return;
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
2147
  	cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2148
  }
a36e71f99   Jens Axboe   cfq-iosched: add ...
2149
  static struct cfq_queue *
f2d1f0ae7   Jens Axboe   cfq-iosched: cach...
2150
2151
2152
  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 ...
2153
  {
a36e71f99   Jens Axboe   cfq-iosched: add ...
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
  	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...
2169
  		if (sector > blk_rq_pos(cfqq->next_rq))
a36e71f99   Jens Axboe   cfq-iosched: add ...
2170
  			n = &(*p)->rb_right;
2e46e8b27   Tejun Heo   block: drop reque...
2171
  		else if (sector < blk_rq_pos(cfqq->next_rq))
a36e71f99   Jens Axboe   cfq-iosched: add ...
2172
2173
2174
2175
  			n = &(*p)->rb_left;
  		else
  			break;
  		p = n;
3ac6c9f8a   Jens Axboe   cfq-iosched: fix ...
2176
  		cfqq = NULL;
a36e71f99   Jens Axboe   cfq-iosched: add ...
2177
2178
2179
2180
2181
  	}
  
  	*ret_parent = parent;
  	if (rb_link)
  		*rb_link = p;
3ac6c9f8a   Jens Axboe   cfq-iosched: fix ...
2182
  	return cfqq;
a36e71f99   Jens Axboe   cfq-iosched: add ...
2183
2184
2185
2186
  }
  
  static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
a36e71f99   Jens Axboe   cfq-iosched: add ...
2187
2188
  	struct rb_node **p, *parent;
  	struct cfq_queue *__cfqq;
f2d1f0ae7   Jens Axboe   cfq-iosched: cach...
2189
2190
2191
2192
  	if (cfqq->p_root) {
  		rb_erase(&cfqq->p_node, cfqq->p_root);
  		cfqq->p_root = NULL;
  	}
a36e71f99   Jens Axboe   cfq-iosched: add ...
2193
2194
2195
2196
2197
  
  	if (cfq_class_idle(cfqq))
  		return;
  	if (!cfqq->next_rq)
  		return;
f2d1f0ae7   Jens Axboe   cfq-iosched: cach...
2198
  	cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
2e46e8b27   Tejun Heo   block: drop reque...
2199
2200
  	__cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
  				      blk_rq_pos(cfqq->next_rq), &parent, &p);
3ac6c9f8a   Jens Axboe   cfq-iosched: fix ...
2201
2202
  	if (!__cfqq) {
  		rb_link_node(&cfqq->p_node, parent, p);
f2d1f0ae7   Jens Axboe   cfq-iosched: cach...
2203
2204
2205
  		rb_insert_color(&cfqq->p_node, cfqq->p_root);
  	} else
  		cfqq->p_root = NULL;
a36e71f99   Jens Axboe   cfq-iosched: add ...
2206
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2207
2208
2209
  /*
   * Update cfqq's position in the service tree.
   */
edd75ffd9   Jens Axboe   cfq-iosched: get ...
2210
  static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
6d048f531   Jens Axboe   cfq-iosched: deve...
2211
  {
6d048f531   Jens Axboe   cfq-iosched: deve...
2212
2213
2214
  	/*
  	 * Resorting requires the cfqq to be on the RR list already.
  	 */
a36e71f99   Jens Axboe   cfq-iosched: add ...
2215
  	if (cfq_cfqq_on_rr(cfqq)) {
edd75ffd9   Jens Axboe   cfq-iosched: get ...
2216
  		cfq_service_tree_add(cfqd, cfqq, 0);
a36e71f99   Jens Axboe   cfq-iosched: add ...
2217
2218
  		cfq_prio_tree_add(cfqd, cfqq);
  	}
6d048f531   Jens Axboe   cfq-iosched: deve...
2219
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2220
2221
  /*
   * add to busy list of queues for service, trying to be fair in ordering
22e2c507c   Jens Axboe   [PATCH] Update cf...
2222
   * the pending list according to last request service
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2223
   */
febffd618   Jens Axboe   cfq-iosched: kill...
2224
  static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2225
  {
7b679138b   Jens Axboe   cfq-iosched: add ...
2226
  	cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2227
2228
  	BUG_ON(cfq_cfqq_on_rr(cfqq));
  	cfq_mark_cfqq_on_rr(cfqq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2229
  	cfqd->busy_queues++;
ef8a41df8   Shaohua Li   cfq-iosched: give...
2230
2231
  	if (cfq_cfqq_sync(cfqq))
  		cfqd->busy_sync_queues++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2232

edd75ffd9   Jens Axboe   cfq-iosched: get ...
2233
  	cfq_resort_rr_list(cfqd, cfqq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2234
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2235
2236
2237
2238
  /*
   * Called when the cfqq no longer has requests pending, remove it from
   * the service tree.
   */
febffd618   Jens Axboe   cfq-iosched: kill...
2239
  static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2240
  {
7b679138b   Jens Axboe   cfq-iosched: add ...
2241
  	cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2242
2243
  	BUG_ON(!cfq_cfqq_on_rr(cfqq));
  	cfq_clear_cfqq_on_rr(cfqq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2244

aa6f6a3de   Corrado Zoccolo   cfq-iosched: prep...
2245
2246
2247
2248
  	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...
2249
2250
2251
2252
  	if (cfqq->p_root) {
  		rb_erase(&cfqq->p_node, cfqq->p_root);
  		cfqq->p_root = NULL;
  	}
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2253

8184f93ec   Justin TerAvest   cfq-iosched: Don'...
2254
  	cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2255
2256
  	BUG_ON(!cfqd->busy_queues);
  	cfqd->busy_queues--;
ef8a41df8   Shaohua Li   cfq-iosched: give...
2257
2258
  	if (cfq_cfqq_sync(cfqq))
  		cfqd->busy_sync_queues--;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2259
2260
2261
2262
2263
  }
  
  /*
   * rb tree support functions
   */
febffd618   Jens Axboe   cfq-iosched: kill...
2264
  static void cfq_del_rq_rb(struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2265
  {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2266
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2267
  	const int sync = rq_is_sync(rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2268

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

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

f04a64246   Vivek Goyal   blkio: Keep queue...
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
  	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
2285
  }
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2286
  static void cfq_add_rq_rb(struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2287
  {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2288
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2289
  	struct cfq_data *cfqd = cfqq->cfqd;
796d5116c   Jeff Moyer   iosched: prevent ...
2290
  	struct request *prev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2291

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

796d5116c   Jeff Moyer   iosched: prevent ...
2294
  	elv_rb_add(&cfqq->sort_list, rq);
5fccbf61b   Jens Axboe   [PATCH] CFQ: requ...
2295
2296
2297
  
  	if (!cfq_cfqq_on_rr(cfqq))
  		cfq_add_cfqq_rr(cfqd, cfqq);
5044eed48   Jens Axboe   cfq-iosched: fix ...
2298
2299
2300
2301
  
  	/*
  	 * check if this request is a better next-serve candidate
  	 */
a36e71f99   Jens Axboe   cfq-iosched: add ...
2302
  	prev = cfqq->next_rq;
cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
2303
  	cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
a36e71f99   Jens Axboe   cfq-iosched: add ...
2304
2305
2306
2307
2308
2309
  
  	/*
  	 * 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 ...
2310
  	BUG_ON(!cfqq->next_rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2311
  }
febffd618   Jens Axboe   cfq-iosched: kill...
2312
  static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2313
  {
5380a101d   Jens Axboe   [PATCH] cfq-iosch...
2314
2315
  	elv_rb_del(&cfqq->sort_list, rq);
  	cfqq->queued[rq_is_sync(rq)]--;
ef295ecf0   Christoph Hellwig   block: better op ...
2316
  	cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2317
  	cfq_add_rq_rb(rq);
155fead9b   Tejun Heo   blkcg: move blkio...
2318
  	cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
ef295ecf0   Christoph Hellwig   block: better op ...
2319
  				 rq->cmd_flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2320
  }
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2321
2322
  static struct request *
  cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2323
  {
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2324
  	struct task_struct *tsk = current;
c58698073   Tejun Heo   block, cfq: reorg...
2325
  	struct cfq_io_cq *cic;
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2326
  	struct cfq_queue *cfqq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2327

4ac845a2e   Jens Axboe   block: cfq: make ...
2328
  	cic = cfq_cic_lookup(cfqd, tsk->io_context);
91fac317a   Vasily Tarasov   cfq-iosched: get ...
2329
2330
  	if (!cic)
  		return NULL;
aa39ebd40   Christoph Hellwig   cfq-iosched: use ...
2331
  	cfqq = cic_to_cfqq(cic, op_is_sync(bio->bi_opf));
f73a1c7d1   Kent Overstreet   block: Add bio_en...
2332
2333
  	if (cfqq)
  		return elv_rb_find(&cfqq->sort_list, bio_end_sector(bio));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2334

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

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

5b93629b4   Tejun Heo   block: implement ...
2345
  	cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2346
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
2347
  static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2348
  {
b4878f245   Jens Axboe   [PATCH] 02/05: up...
2349
  	struct cfq_data *cfqd = q->elevator->elevator_data;
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
2350
2351
  	WARN_ON(!cfqd->rq_in_driver);
  	cfqd->rq_in_driver--;
7b679138b   Jens Axboe   cfq-iosched: add ...
2352
  	cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
2353
  						cfqd->rq_in_driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2354
  }
b4878f245   Jens Axboe   [PATCH] 02/05: up...
2355
  static void cfq_remove_request(struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2356
  {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2357
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
2358

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

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

45333d5a3   Aaron Carroll   cfq-iosched: fix ...
2365
  	cfqq->cfqd->rq_queued--;
ef295ecf0   Christoph Hellwig   block: better op ...
2366
  	cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
65299a3b7   Christoph Hellwig   block: separate p...
2367
2368
2369
  	if (rq->cmd_flags & REQ_PRIO) {
  		WARN_ON(!cfqq->prio_pending);
  		cfqq->prio_pending--;
b53d1ed73   Jens Axboe   Revert "cfq: Remo...
2370
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2371
  }
34fe7c054   Christoph Hellwig   block: enumify EL...
2372
  static enum elv_merge cfq_merge(struct request_queue *q, struct request **req,
165125e1e   Jens Axboe   [BLOCK] Get rid o...
2373
  		     struct bio *bio)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2374
2375
2376
  {
  	struct cfq_data *cfqd = q->elevator->elevator_data;
  	struct request *__rq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2377

206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2378
  	__rq = cfq_find_rq_fmerge(cfqd, bio);
72ef799b3   Tahsin Erdogan   block: do not mer...
2379
  	if (__rq && elv_bio_merge_ok(__rq, bio)) {
9817064b6   Jens Axboe   [PATCH] elevator:...
2380
2381
  		*req = __rq;
  		return ELEVATOR_FRONT_MERGE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2382
2383
2384
  	}
  
  	return ELEVATOR_NO_MERGE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2385
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
2386
  static void cfq_merged_request(struct request_queue *q, struct request *req,
34fe7c054   Christoph Hellwig   block: enumify EL...
2387
  			       enum elv_merge type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2388
  {
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
2389
  	if (type == ELEVATOR_FRONT_MERGE) {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2390
  		struct cfq_queue *cfqq = RQ_CFQQ(req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2391

5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2392
  		cfq_reposition_rq_rb(cfqq, req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2393
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2394
  }
812d40264   Divyesh Shah   blkio: Add io_mer...
2395
2396
2397
  static void cfq_bio_merged(struct request_queue *q, struct request *req,
  				struct bio *bio)
  {
ef295ecf0   Christoph Hellwig   block: better op ...
2398
  	cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_opf);
812d40264   Divyesh Shah   blkio: Add io_mer...
2399
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2400
  static void
165125e1e   Jens Axboe   [BLOCK] Get rid o...
2401
  cfq_merged_requests(struct request_queue *q, struct request *rq,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2402
2403
  		    struct request *next)
  {
cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
2404
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
4a0b75c7d   Shaohua Li   block, cfq: fix e...
2405
  	struct cfq_data *cfqd = q->elevator->elevator_data;
22e2c507c   Jens Axboe   [PATCH] Update cf...
2406
2407
2408
2409
  	/*
  	 * reposition in fifo if next is older than rq
  	 */
  	if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2410
  	    next->fifo_time < rq->fifo_time &&
3d106fba2   Shaohua Li   block CFQ: avoid ...
2411
  	    cfqq == RQ_CFQQ(next)) {
22e2c507c   Jens Axboe   [PATCH] Update cf...
2412
  		list_move(&rq->queuelist, &next->queuelist);
8b4922d31   Jan Kara   block: Stop abusi...
2413
  		rq->fifo_time = next->fifo_time;
30996f40b   Jens Axboe   cfq-iosched: fix ...
2414
  	}
22e2c507c   Jens Axboe   [PATCH] Update cf...
2415

cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
2416
2417
  	if (cfqq->next_rq == next)
  		cfqq->next_rq = rq;
b4878f245   Jens Axboe   [PATCH] 02/05: up...
2418
  	cfq_remove_request(next);
ef295ecf0   Christoph Hellwig   block: better op ...
2419
  	cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags);
4a0b75c7d   Shaohua Li   block, cfq: fix e...
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
  
  	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...
2430
  }
72ef799b3   Tahsin Erdogan   block: do not mer...
2431
2432
  static int cfq_allow_bio_merge(struct request_queue *q, struct request *rq,
  			       struct bio *bio)
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2433
2434
  {
  	struct cfq_data *cfqd = q->elevator->elevator_data;
aa39ebd40   Christoph Hellwig   cfq-iosched: use ...
2435
  	bool is_sync = op_is_sync(bio->bi_opf);
c58698073   Tejun Heo   block, cfq: reorg...
2436
  	struct cfq_io_cq *cic;
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2437
  	struct cfq_queue *cfqq;
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2438
2439
  
  	/*
ec8acb690   Jens Axboe   [PATCH] cfq-iosch...
2440
  	 * Disallow merge of a sync bio into an async request.
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2441
  	 */
aa39ebd40   Christoph Hellwig   cfq-iosched: use ...
2442
  	if (is_sync && !rq_is_sync(rq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
2443
  		return false;
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2444
2445
  
  	/*
f1a4f4d35   Tejun Heo   block, cfq: fix c...
2446
  	 * Lookup the cfqq that this bio will be queued with and allow
07c2bd373   Tejun Heo   block: don't call...
2447
  	 * merge only if rq is queued there.
f1a4f4d35   Tejun Heo   block, cfq: fix c...
2448
  	 */
07c2bd373   Tejun Heo   block: don't call...
2449
2450
2451
  	cic = cfq_cic_lookup(cfqd, current->io_context);
  	if (!cic)
  		return false;
719d34027   Jens Axboe   [PATCH] cfq-iosch...
2452

aa39ebd40   Christoph Hellwig   cfq-iosched: use ...
2453
  	cfqq = cic_to_cfqq(cic, is_sync);
a6151c3a5   Jens Axboe   cfq-iosched: appl...
2454
  	return cfqq == RQ_CFQQ(rq);
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2455
  }
72ef799b3   Tahsin Erdogan   block: do not mer...
2456
2457
2458
2459
2460
  static int cfq_allow_rq_merge(struct request_queue *q, struct request *rq,
  			      struct request *next)
  {
  	return RQ_CFQQ(rq) == RQ_CFQQ(next);
  }
812df48d1   Divyesh Shah   blkio: Add more d...
2461
2462
  static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
911483258   Jan Kara   cfq-iosched: Conv...
2463
  	hrtimer_try_to_cancel(&cfqd->idle_slice_timer);
155fead9b   Tejun Heo   blkcg: move blkio...
2464
  	cfqg_stats_update_idle_time(cfqq->cfqg);
812df48d1   Divyesh Shah   blkio: Add more d...
2465
  }
febffd618   Jens Axboe   cfq-iosched: kill...
2466
2467
  static void __cfq_set_active_queue(struct cfq_data *cfqd,
  				   struct cfq_queue *cfqq)
22e2c507c   Jens Axboe   [PATCH] Update cf...
2468
2469
  {
  	if (cfqq) {
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
2470
  		cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2471
  				cfqd->serving_wl_class, cfqd->serving_wl_type);
155fead9b   Tejun Heo   blkcg: move blkio...
2472
  		cfqg_stats_update_avg_queue_size(cfqq->cfqg);
62a37f6ba   Justin TerAvest   cfq-iosched: Don'...
2473
  		cfqq->slice_start = 0;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2474
  		cfqq->dispatch_start = ktime_get_ns();
62a37f6ba   Justin TerAvest   cfq-iosched: Don'...
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
  		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...
2487
2488
2489
2490
2491
2492
  	}
  
  	cfqd->active_queue = cfqq;
  }
  
  /*
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2493
2494
2495
2496
   * 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...
2497
  		    bool timed_out)
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2498
  {
7b679138b   Jens Axboe   cfq-iosched: add ...
2499
  	cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2500
  	if (cfq_cfqq_wait_request(cfqq))
812df48d1   Divyesh Shah   blkio: Add more d...
2501
  		cfq_del_timer(cfqd, cfqq);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2502

7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2503
  	cfq_clear_cfqq_wait_request(cfqq);
f75edf2dc   Vivek Goyal   blkio: Wait for c...
2504
  	cfq_clear_cfqq_wait_busy(cfqq);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2505
2506
  
  	/*
ae54abed6   Shaohua Li   cfq-iosched: spli...
2507
2508
2509
2510
2511
2512
2513
2514
2515
  	 * 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'...
2516
  	 * store what was left of this slice, if the queue idled/timed out
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2517
  	 */
c553f8e33   Shaohua Li   block cfq: compen...
2518
2519
  	if (timed_out) {
  		if (cfq_cfqq_slice_new(cfqq))
ba5bd520f   Vivek Goyal   cfq: rename a fun...
2520
  			cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
c553f8e33   Shaohua Li   block cfq: compen...
2521
  		else
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2522
  			cfqq->slice_resid = cfqq->slice_end - ktime_get_ns();
93fdf1478   Jan Kara   cfq-iosched: Conv...
2523
  		cfq_log_cfqq(cfqd, cfqq, "resid=%lld", cfqq->slice_resid);
7b679138b   Jens Axboe   cfq-iosched: add ...
2524
  	}
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2525

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

f04a64246   Vivek Goyal   blkio: Keep queue...
2528
2529
  	if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
  		cfq_del_cfqq_rr(cfqd, cfqq);
edd75ffd9   Jens Axboe   cfq-iosched: get ...
2530
  	cfq_resort_rr_list(cfqd, cfqq);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2531
2532
2533
2534
2535
  
  	if (cfqq == cfqd->active_queue)
  		cfqd->active_queue = NULL;
  
  	if (cfqd->active_cic) {
11a3122f6   Tejun Heo   block: strip out ...
2536
  		put_io_context(cfqd->active_cic->icq.ioc);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2537
2538
  		cfqd->active_cic = NULL;
  	}
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2539
  }
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
2540
  static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2541
2542
2543
2544
  {
  	struct cfq_queue *cfqq = cfqd->active_queue;
  
  	if (cfqq)
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
2545
  		__cfq_slice_expired(cfqd, cfqq, timed_out);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2546
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2547
2548
2549
2550
  /*
   * 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...
2551
  static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
22e2c507c   Jens Axboe   [PATCH] Update cf...
2552
  {
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2553
2554
  	struct cfq_rb_root *st = st_for(cfqd->serving_group,
  			cfqd->serving_wl_class, cfqd->serving_wl_type);
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2555

f04a64246   Vivek Goyal   blkio: Keep queue...
2556
2557
  	if (!cfqd->rq_queued)
  		return NULL;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
2558
  	/* There is nothing to dispatch */
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2559
  	if (!st)
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
2560
  		return NULL;
09663c86e   Davidlohr Bueso   block/cfq: replac...
2561
  	if (RB_EMPTY_ROOT(&st->rb.rb_root))
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
2562
  		return NULL;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2563
  	return cfq_rb_first(st);
6d048f531   Jens Axboe   cfq-iosched: deve...
2564
  }
f04a64246   Vivek Goyal   blkio: Keep queue...
2565
2566
  static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
  {
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
2567
  	struct cfq_group *cfqg;
f04a64246   Vivek Goyal   blkio: Keep queue...
2568
2569
2570
2571
2572
2573
  	struct cfq_queue *cfqq;
  	int i, j;
  	struct cfq_rb_root *st;
  
  	if (!cfqd->rq_queued)
  		return NULL;
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
2574
2575
2576
  	cfqg = cfq_get_next_cfqg(cfqd);
  	if (!cfqg)
  		return NULL;
1cf417530   Markus Elfring   cfq-iosched: Adju...
2577
2578
2579
  	for_each_cfqg_st(cfqg, i, j, st) {
  		cfqq = cfq_rb_first(st);
  		if (cfqq)
f04a64246   Vivek Goyal   blkio: Keep queue...
2580
  			return cfqq;
1cf417530   Markus Elfring   cfq-iosched: Adju...
2581
  	}
f04a64246   Vivek Goyal   blkio: Keep queue...
2582
2583
  	return NULL;
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2584
2585
2586
  /*
   * Get and set a new active queue for service.
   */
a36e71f99   Jens Axboe   cfq-iosched: add ...
2587
2588
  static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
  					      struct cfq_queue *cfqq)
6d048f531   Jens Axboe   cfq-iosched: deve...
2589
  {
e00ef7997   Jens Axboe   cfq-iosched: get ...
2590
  	if (!cfqq)
a36e71f99   Jens Axboe   cfq-iosched: add ...
2591
  		cfqq = cfq_get_next_queue(cfqd);
6d048f531   Jens Axboe   cfq-iosched: deve...
2592

22e2c507c   Jens Axboe   [PATCH] Update cf...
2593
  	__cfq_set_active_queue(cfqd, cfqq);
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2594
  	return cfqq;
22e2c507c   Jens Axboe   [PATCH] Update cf...
2595
  }
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2596
2597
2598
  static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
  					  struct request *rq)
  {
83096ebf1   Tejun Heo   block: convert to...
2599
2600
  	if (blk_rq_pos(rq) >= cfqd->last_position)
  		return blk_rq_pos(rq) - cfqd->last_position;
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2601
  	else
83096ebf1   Tejun Heo   block: convert to...
2602
  		return cfqd->last_position - blk_rq_pos(rq);
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2603
  }
b2c18e1e0   Jeff Moyer   cfq: calculate th...
2604
  static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
e9ce335df   Shaohua Li   cfq-iosched: fix ...
2605
  			       struct request *rq)
6d048f531   Jens Axboe   cfq-iosched: deve...
2606
  {
e9ce335df   Shaohua Li   cfq-iosched: fix ...
2607
  	return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
6d048f531   Jens Axboe   cfq-iosched: deve...
2608
  }
a36e71f99   Jens Axboe   cfq-iosched: add ...
2609
2610
2611
  static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
  				    struct cfq_queue *cur_cfqq)
  {
f2d1f0ae7   Jens Axboe   cfq-iosched: cach...
2612
  	struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
a36e71f99   Jens Axboe   cfq-iosched: add ...
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
  	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...
2624
  	__cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
a36e71f99   Jens Axboe   cfq-iosched: add ...
2625
2626
2627
2628
2629
2630
2631
2632
  	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 ...
2633
  	if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
a36e71f99   Jens Axboe   cfq-iosched: add ...
2634
  		return __cfqq;
2e46e8b27   Tejun Heo   block: drop reque...
2635
  	if (blk_rq_pos(__cfqq->next_rq) < sector)
a36e71f99   Jens Axboe   cfq-iosched: add ...
2636
2637
2638
2639
2640
2641
2642
  		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 ...
2643
  	if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
a36e71f99   Jens Axboe   cfq-iosched: add ...
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
  		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...
2660
  					      struct cfq_queue *cur_cfqq)
6d048f531   Jens Axboe   cfq-iosched: deve...
2661
  {
a36e71f99   Jens Axboe   cfq-iosched: add ...
2662
  	struct cfq_queue *cfqq;
39c01b219   Divyesh Shah   cfq-iosched: Do n...
2663
2664
  	if (cfq_class_idle(cur_cfqq))
  		return NULL;
e6c5bc737   Jeff Moyer   cfq: break apart ...
2665
2666
2667
2668
  	if (!cfq_cfqq_sync(cur_cfqq))
  		return NULL;
  	if (CFQQ_SEEKY(cur_cfqq))
  		return NULL;
a36e71f99   Jens Axboe   cfq-iosched: add ...
2669
  	/*
b9d8f4c73   Gui Jianfeng   cfq: Optimization...
2670
2671
2672
2673
2674
2675
  	 * 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...
2676
2677
2678
  	 * 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...
2679
  	 */
a36e71f99   Jens Axboe   cfq-iosched: add ...
2680
2681
2682
  	cfqq = cfqq_close(cfqd, cur_cfqq);
  	if (!cfqq)
  		return NULL;
8682e1f15   Vivek Goyal   blkio: Provide so...
2683
2684
2685
  	/* 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...
2686
2687
2688
2689
2690
  	/*
  	 * It only makes sense to merge sync queues.
  	 */
  	if (!cfq_cfqq_sync(cfqq))
  		return NULL;
e6c5bc737   Jeff Moyer   cfq: break apart ...
2691
2692
  	if (CFQQ_SEEKY(cfqq))
  		return NULL;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2693

c0324a020   Corrado Zoccolo   cfq-iosched: reim...
2694
2695
2696
2697
2698
  	/*
  	 * 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 ...
2699
  	return cfqq;
6d048f531   Jens Axboe   cfq-iosched: deve...
2700
  }
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2701
2702
2703
2704
2705
2706
  /*
   * 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...
2707
  	enum wl_class_t wl_class = cfqq_class(cfqq);
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2708
  	struct cfq_rb_root *st = cfqq->service_tree;
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2709

34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2710
2711
  	BUG_ON(!st);
  	BUG_ON(!st->count);
f04a64246   Vivek Goyal   blkio: Keep queue...
2712

b6508c161   Vivek Goyal   cfq-iosched: Do n...
2713
2714
  	if (!cfqd->cfq_slice_idle)
  		return false;
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2715
  	/* We never do for idle class queues. */
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
2716
  	if (wl_class == IDLE_WORKLOAD)
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2717
2718
2719
  		return false;
  
  	/* We do for queues that were marked with idle window flag. */
3c764b7a6   Shaohua Li   cfq-iosched: make...
2720
2721
  	if (cfq_cfqq_idle_window(cfqq) &&
  	   !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2722
2723
2724
2725
2726
2727
  		return true;
  
  	/*
  	 * Otherwise, we do only if they are the last ones
  	 * in their service tree.
  	 */
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2728
2729
  	if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
  	   !cfq_io_thinktime_big(cfqd, &st->ttime, false))
c1e44756f   Shaohua Li   cfq-iosched: do c...
2730
  		return true;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2731
  	cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
c1e44756f   Shaohua Li   cfq-iosched: do c...
2732
  	return false;
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2733
  }
6d048f531   Jens Axboe   cfq-iosched: deve...
2734
  static void cfq_arm_slice_timer(struct cfq_data *cfqd)
22e2c507c   Jens Axboe   [PATCH] Update cf...
2735
  {
1792669cc   Jens Axboe   cfq-iosched: don'...
2736
  	struct cfq_queue *cfqq = cfqd->active_queue;
e795421e4   Jan Kara   cfq-iosched: Don'...
2737
  	struct cfq_rb_root *st = cfqq->service_tree;
c58698073   Tejun Heo   block, cfq: reorg...
2738
  	struct cfq_io_cq *cic;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2739
2740
  	u64 sl, group_idle = 0;
  	u64 now = ktime_get_ns();
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2741

a68bbddba   Jens Axboe   block: add queue ...
2742
  	/*
f7d7b7a7a   Jens Axboe   block: as/cfq ssd...
2743
2744
2745
  	 * 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 ...
2746
  	 */
b3193bc0d   Ritesh Harjani   cfq: Give a chanc...
2747
2748
  	if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag &&
  		!cfqd->cfq_group_idle)
a68bbddba   Jens Axboe   block: add queue ...
2749
  		return;
dd67d0515   Jens Axboe   [PATCH] rbtree: s...
2750
  	WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
6d048f531   Jens Axboe   cfq-iosched: deve...
2751
  	WARN_ON(cfq_cfqq_slice_new(cfqq));
22e2c507c   Jens Axboe   [PATCH] Update cf...
2752
2753
2754
2755
  
  	/*
  	 * idle is disabled, either manually or by past process history
  	 */
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2756
2757
2758
2759
2760
2761
2762
  	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...
2763

22e2c507c   Jens Axboe   [PATCH] Update cf...
2764
  	/*
8e550632c   Corrado Zoccolo   cfq-iosched: fix ...
2765
  	 * still active requests from this queue, don't idle
7b679138b   Jens Axboe   cfq-iosched: add ...
2766
  	 */
8e550632c   Corrado Zoccolo   cfq-iosched: fix ...
2767
  	if (cfqq->dispatched)
7b679138b   Jens Axboe   cfq-iosched: add ...
2768
2769
2770
  		return;
  
  	/*
22e2c507c   Jens Axboe   [PATCH] Update cf...
2771
2772
  	 * task has exited, don't wait
  	 */
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2773
  	cic = cfqd->active_cic;
f6e8d01be   Tejun Heo   block: add io_con...
2774
  	if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
6d048f531   Jens Axboe   cfq-iosched: deve...
2775
  		return;
355b659c8   Corrado Zoccolo   cfq-iosched: avoi...
2776
2777
2778
2779
2780
  	/*
  	 * 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...
2781
  	if (sample_valid(cic->ttime.ttime_samples) &&
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2782
2783
  	    (cfqq->slice_end - now < cic->ttime.ttime_mean)) {
  		cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%llu",
383cd7213   Shaohua Li   CFQ: move think t...
2784
  			     cic->ttime.ttime_mean);
355b659c8   Corrado Zoccolo   cfq-iosched: avoi...
2785
  		return;
b1ffe737f   Divyesh Shah   cfq-iosched: Add ...
2786
  	}
355b659c8   Corrado Zoccolo   cfq-iosched: avoi...
2787

e795421e4   Jan Kara   cfq-iosched: Don'...
2788
2789
2790
2791
2792
2793
2794
  	/*
  	 * There are other queues in the group or this is the only group and
  	 * it has too big thinktime, don't do group idle.
  	 */
  	if (group_idle &&
  	    (cfqq->cfqg->nr_cfqq > 1 ||
  	     cfq_io_thinktime_big(cfqd, &st->ttime, true)))
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2795
  		return;
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2796
  	cfq_mark_cfqq_wait_request(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
2797

80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2798
2799
2800
2801
  	if (group_idle)
  		sl = cfqd->cfq_group_idle;
  	else
  		sl = cfqd->cfq_slice_idle;
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2802

911483258   Jan Kara   cfq-iosched: Conv...
2803
2804
  	hrtimer_start(&cfqd->idle_slice_timer, ns_to_ktime(sl),
  		      HRTIMER_MODE_REL);
155fead9b   Tejun Heo   blkcg: move blkio...
2805
  	cfqg_stats_set_start_idle_time(cfqq->cfqg);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2806
  	cfq_log_cfqq(cfqd, cfqq, "arm_idle: %llu group_idle: %d", sl,
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2807
  			group_idle ? 1 : 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2808
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2809
2810
2811
  /*
   * Move request from internal lists to the request queue dispatch list.
   */
165125e1e   Jens Axboe   [BLOCK] Get rid o...
2812
  static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2813
  {
3ed9a2965   Jens Axboe   cfq-iosched: impr...
2814
  	struct cfq_data *cfqd = q->elevator->elevator_data;
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2815
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
2816

7b679138b   Jens Axboe   cfq-iosched: add ...
2817
  	cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
06d218864   Jeff Moyer   cfq: choose a new...
2818
  	cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
5380a101d   Jens Axboe   [PATCH] cfq-iosch...
2819
  	cfq_remove_request(rq);
6d048f531   Jens Axboe   cfq-iosched: deve...
2820
  	cfqq->dispatched++;
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2821
  	(RQ_CFQG(rq))->dispatched++;
5380a101d   Jens Axboe   [PATCH] cfq-iosch...
2822
  	elv_dispatch_sort(q, rq);
3ed9a2965   Jens Axboe   cfq-iosched: impr...
2823

53c583d22   Corrado Zoccolo   cfq-iosched: requ...
2824
  	cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
c4e7893eb   Vivek Goyal   cfq-iosched: blkt...
2825
  	cfqq->nr_sectors += blk_rq_sectors(rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2826
2827
2828
2829
2830
  }
  
  /*
   * return expired entry, or NULL to just start from scratch in rbtree
   */
febffd618   Jens Axboe   cfq-iosched: kill...
2831
  static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2832
  {
30996f40b   Jens Axboe   cfq-iosched: fix ...
2833
  	struct request *rq = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2834

3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2835
  	if (cfq_cfqq_fifo_expire(cfqq))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2836
  		return NULL;
cb8874119   Jens Axboe   cfq-iosched: twea...
2837
2838
  
  	cfq_mark_cfqq_fifo_expire(cfqq);
89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
2839
2840
  	if (list_empty(&cfqq->fifo))
  		return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2841

89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
2842
  	rq = rq_entry_fifo(cfqq->fifo.next);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2843
  	if (ktime_get_ns() < rq->fifo_time)
7b679138b   Jens Axboe   cfq-iosched: add ...
2844
  		rq = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2845

6d048f531   Jens Axboe   cfq-iosched: deve...
2846
  	return rq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2847
  }
22e2c507c   Jens Axboe   [PATCH] Update cf...
2848
2849
2850
2851
  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
2852

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

b9f8ce059   Namhyung Kim   cfq-iosched: alge...
2855
  	return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2856
  }
22e2c507c   Jens Axboe   [PATCH] Update cf...
2857
  /*
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2858
2859
2860
2861
2862
2863
2864
   * 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 ...
2865
  	process_refs = cfqq->ref - io_refs;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2866
2867
2868
2869
2870
2871
  	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 ...
2872
  	int process_refs, new_process_refs;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2873
  	struct cfq_queue *__cfqq;
c10b61f09   Jeff Moyer   cfq: Don't allow ...
2874
2875
2876
2877
2878
2879
2880
2881
  	/*
  	 * 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...
2882
2883
2884
2885
2886
2887
2888
2889
  	/* 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 ...
2890
  	new_process_refs = cfqq_process_refs(new_cfqq);
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2891
2892
2893
2894
  	/*
  	 * If the process for the cfqq has gone away, there is no
  	 * sense in merging the queues.
  	 */
c10b61f09   Jeff Moyer   cfq: Don't allow ...
2895
  	if (process_refs == 0 || new_process_refs == 0)
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2896
  		return;
e6c5bc737   Jeff Moyer   cfq: break apart ...
2897
2898
2899
  	/*
  	 * Merge in the direction of the lesser amount of work.
  	 */
e6c5bc737   Jeff Moyer   cfq: break apart ...
2900
2901
  	if (new_process_refs >= process_refs) {
  		cfqq->new_cfqq = new_cfqq;
30d7b9448   Shaohua Li   block cfq: don't ...
2902
  		new_cfqq->ref += process_refs;
e6c5bc737   Jeff Moyer   cfq: break apart ...
2903
2904
  	} else {
  		new_cfqq->new_cfqq = cfqq;
30d7b9448   Shaohua Li   block cfq: don't ...
2905
  		cfqq->ref += new_process_refs;
e6c5bc737   Jeff Moyer   cfq: break apart ...
2906
  	}
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2907
  }
6d816ec7c   Vivek Goyal   cfq-iosched: Rena...
2908
  static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
2909
  			struct cfq_group *cfqg, enum wl_class_t wl_class)
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2910
2911
2912
2913
  {
  	struct cfq_queue *queue;
  	int i;
  	bool key_valid = false;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2914
  	u64 lowest_key = 0;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2915
  	enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
65b32a573   Vivek Goyal   cfq-iosched: Remo...
2916
2917
  	for (i = 0; i <= SYNC_WORKLOAD; ++i) {
  		/* select the one with lowest rb_key */
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2918
  		queue = cfq_rb_first(st_for(cfqg, wl_class, i));
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2919
  		if (queue &&
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2920
  		    (!key_valid || queue->rb_key < lowest_key)) {
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2921
2922
2923
2924
2925
2926
2927
2928
  			lowest_key = queue->rb_key;
  			cur_best = i;
  			key_valid = true;
  		}
  	}
  
  	return cur_best;
  }
6d816ec7c   Vivek Goyal   cfq-iosched: Rena...
2929
2930
  static void
  choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2931
  {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2932
  	u64 slice;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2933
  	unsigned count;
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
2934
  	struct cfq_rb_root *st;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2935
  	u64 group_slice;
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2936
  	enum wl_class_t original_class = cfqd->serving_wl_class;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2937
  	u64 now = ktime_get_ns();
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
2938

718eee057   Corrado Zoccolo   cfq-iosched: fair...
2939
  	/* Choose next priority. RT > BE > IDLE */
58ff82f34   Vivek Goyal   blkio: Implement ...
2940
  	if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2941
  		cfqd->serving_wl_class = RT_WORKLOAD;
58ff82f34   Vivek Goyal   blkio: Implement ...
2942
  	else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2943
  		cfqd->serving_wl_class = BE_WORKLOAD;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2944
  	else {
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2945
  		cfqd->serving_wl_class = IDLE_WORKLOAD;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2946
  		cfqd->workload_expires = now + jiffies_to_nsecs(1);
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2947
2948
  		return;
  	}
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2949
  	if (original_class != cfqd->serving_wl_class)
e4ea0c16a   Shaohua Li writes   block cfq: select...
2950
  		goto new_workload;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2951
2952
2953
2954
2955
  	/*
  	 * 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...
2956
  	st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
2957
  	count = st->count;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2958
2959
  
  	/*
65b32a573   Vivek Goyal   cfq-iosched: Remo...
2960
  	 * check workload expiration, and that we still have other queues ready
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2961
  	 */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2962
  	if (count && !(now > cfqd->workload_expires))
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2963
  		return;
e4ea0c16a   Shaohua Li writes   block cfq: select...
2964
  new_workload:
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2965
  	/* otherwise select new workload type */
6d816ec7c   Vivek Goyal   cfq-iosched: Rena...
2966
  	cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2967
  					cfqd->serving_wl_class);
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2968
  	st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
2969
  	count = st->count;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2970
2971
2972
2973
2974
2975
  
  	/*
  	 * 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 ...
2976
  	group_slice = cfq_group_slice(cfqd, cfqg);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2977
  	slice = div_u64(group_slice * count,
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2978
2979
  		max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
  		      cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2980
  					cfqg)));
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2981

4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2982
  	if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2983
  		u64 tmp;
f26bd1f0a   Vivek Goyal   blkio: Determine ...
2984
2985
2986
2987
2988
2989
2990
2991
  
  		/*
  		 * 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...
2992
2993
  		tmp = cfqd->cfq_target_latency *
  			cfqg_busy_async_queues(cfqd, cfqg);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2994
2995
  		tmp = div_u64(tmp, cfqd->busy_queues);
  		slice = min_t(u64, slice, tmp);
f26bd1f0a   Vivek Goyal   blkio: Determine ...
2996

718eee057   Corrado Zoccolo   cfq-iosched: fair...
2997
2998
  		/* async workload slice is scaled down according to
  		 * the sync/async slice ratio. */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2999
  		slice = div64_u64(slice*cfqd->cfq_slice[0], cfqd->cfq_slice[1]);
f26bd1f0a   Vivek Goyal   blkio: Determine ...
3000
  	} else
718eee057   Corrado Zoccolo   cfq-iosched: fair...
3001
3002
  		/* sync workload slice is at least 2 * cfq_slice_idle */
  		slice = max(slice, 2 * cfqd->cfq_slice_idle);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3003
3004
3005
  	slice = max_t(u64, slice, CFQ_MIN_TT);
  	cfq_log(cfqd, "workload slice:%llu", slice);
  	cfqd->workload_expires = now + slice;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
3006
  }
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
3007
3008
3009
  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 ...
3010
  	struct cfq_group *cfqg;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
3011

09663c86e   Davidlohr Bueso   block/cfq: replac...
3012
  	if (RB_EMPTY_ROOT(&st->rb.rb_root))
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
3013
  		return NULL;
25bc6b077   Vivek Goyal   blkio: Introduce ...
3014
  	cfqg = cfq_rb_first_group(st);
25bc6b077   Vivek Goyal   blkio: Introduce ...
3015
3016
  	update_min_vdisktime(st);
  	return cfqg;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
3017
  }
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
3018
3019
  static void cfq_choose_cfqg(struct cfq_data *cfqd)
  {
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
3020
  	struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3021
  	u64 now = ktime_get_ns();
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
3022
3023
  
  	cfqd->serving_group = cfqg;
dae739ebc   Vivek Goyal   blkio: Group time...
3024
3025
  
  	/* Restore the workload type data */
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
3026
  	if (cfqg->saved_wl_slice) {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3027
  		cfqd->workload_expires = now + cfqg->saved_wl_slice;
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
3028
3029
  		cfqd->serving_wl_type = cfqg->saved_wl_type;
  		cfqd->serving_wl_class = cfqg->saved_wl_class;
66ae29197   Gui Jianfeng   cfq: set workload...
3030
  	} else
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3031
  		cfqd->workload_expires = now - 1;
66ae29197   Gui Jianfeng   cfq: set workload...
3032

6d816ec7c   Vivek Goyal   cfq-iosched: Rena...
3033
  	choose_wl_class_and_type(cfqd, cfqg);
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
3034
  }
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
3035
  /*
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
3036
3037
   * 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...
3038
   */
1b5ed5e1f   Tejun Heo   [BLOCK] cfq-iosch...
3039
  static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3040
  {
a36e71f99   Jens Axboe   cfq-iosched: add ...
3041
  	struct cfq_queue *cfqq, *new_cfqq = NULL;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3042
  	u64 now = ktime_get_ns();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3043

22e2c507c   Jens Axboe   [PATCH] Update cf...
3044
3045
3046
  	cfqq = cfqd->active_queue;
  	if (!cfqq)
  		goto new_queue;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3047

f04a64246   Vivek Goyal   blkio: Keep queue...
3048
3049
  	if (!cfqd->rq_queued)
  		return NULL;
c244bb50a   Vivek Goyal   cfq-iosched: Get ...
3050
3051
3052
3053
3054
3055
  
  	/*
  	 * 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...
3056
  	/*
6d048f531   Jens Axboe   cfq-iosched: deve...
3057
  	 * The active queue has run out of time, expire it and select new.
22e2c507c   Jens Axboe   [PATCH] Update cf...
3058
  	 */
7667aa063   Vivek Goyal   cfq-iosched: Take...
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
  	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...
3069
3070
3071
  		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...
3072
  			goto keep_queue;
82bbbf28d   Vivek Goyal   Fix a CFQ crash i...
3073
  		} else
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
3074
  			goto check_group_idle;
7667aa063   Vivek Goyal   cfq-iosched: Take...
3075
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3076

22e2c507c   Jens Axboe   [PATCH] Update cf...
3077
  	/*
6d048f531   Jens Axboe   cfq-iosched: deve...
3078
3079
  	 * The active queue has requests and isn't expired, allow it to
  	 * dispatch.
22e2c507c   Jens Axboe   [PATCH] Update cf...
3080
  	 */
dd67d0515   Jens Axboe   [PATCH] rbtree: s...
3081
  	if (!RB_EMPTY_ROOT(&cfqq->sort_list))
22e2c507c   Jens Axboe   [PATCH] Update cf...
3082
  		goto keep_queue;
6d048f531   Jens Axboe   cfq-iosched: deve...
3083
3084
  
  	/*
a36e71f99   Jens Axboe   cfq-iosched: add ...
3085
3086
3087
  	 * 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...
3088
  	 * tree.  If possible, merge the expiring queue with the new cfqq.
a36e71f99   Jens Axboe   cfq-iosched: add ...
3089
  	 */
b3b6d0408   Jeff Moyer   cfq: change the m...
3090
  	new_cfqq = cfq_close_cooperator(cfqd, cfqq);
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
3091
3092
3093
  	if (new_cfqq) {
  		if (!cfqq->new_cfqq)
  			cfq_setup_merge(cfqq, new_cfqq);
a36e71f99   Jens Axboe   cfq-iosched: add ...
3094
  		goto expire;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
3095
  	}
a36e71f99   Jens Axboe   cfq-iosched: add ...
3096
3097
  
  	/*
6d048f531   Jens Axboe   cfq-iosched: deve...
3098
3099
3100
3101
  	 * 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.
  	 */
911483258   Jan Kara   cfq-iosched: Conv...
3102
  	if (hrtimer_active(&cfqd->idle_slice_timer)) {
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
3103
3104
3105
  		cfqq = NULL;
  		goto keep_queue;
  	}
8e1ac6655   Shaohua Li   cfq-iosched: don'...
3106
3107
3108
3109
3110
3111
  	/*
  	 * 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) ||
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3112
  	    (cfqq->slice_end - now > now - cfqq->slice_start))) {
8e1ac6655   Shaohua Li   cfq-iosched: don'...
3113
3114
3115
  		cfq_clear_cfqq_deep(cfqq);
  		cfq_clear_cfqq_idle_window(cfqq);
  	}
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
  	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...
3126
3127
3128
  	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...
3129
3130
  		cfqq = NULL;
  		goto keep_queue;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3131
  	}
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
3132
  expire:
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
3133
  	cfq_slice_expired(cfqd, 0);
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
3134
  new_queue:
718eee057   Corrado Zoccolo   cfq-iosched: fair...
3135
3136
3137
3138
3139
  	/*
  	 * Current queue expired. Check if we have to switch to a new
  	 * service tree
  	 */
  	if (!new_cfqq)
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
3140
  		cfq_choose_cfqg(cfqd);
718eee057   Corrado Zoccolo   cfq-iosched: fair...
3141

a36e71f99   Jens Axboe   cfq-iosched: add ...
3142
  	cfqq = cfq_set_active_queue(cfqd, new_cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3143
  keep_queue:
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
3144
  	return cfqq;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3145
  }
febffd618   Jens Axboe   cfq-iosched: kill...
3146
  static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
3147
3148
3149
3150
3151
3152
3153
3154
3155
  {
  	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...
3156
3157
  
  	/* By default cfqq is not expired if it is empty. Do it explicitly */
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
3158
  	__cfq_slice_expired(cfqq->cfqd, cfqq, 0);
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
3159
3160
  	return dispatched;
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
3161
3162
3163
3164
  /*
   * Drain our current requests. Used for barriers and when switching
   * io schedulers on-the-fly.
   */
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
3165
  static int cfq_forced_dispatch(struct cfq_data *cfqd)
1b5ed5e1f   Tejun Heo   [BLOCK] cfq-iosch...
3166
  {
0871714e0   Jens Axboe   cfq-iosched: rela...
3167
  	struct cfq_queue *cfqq;
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
3168
  	int dispatched = 0;
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
3169

3440c49f5   Divyesh Shah   cfq-iosched: Fix ...
3170
  	/* Expire the timeslice of the current active queue first */
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
3171
  	cfq_slice_expired(cfqd, 0);
3440c49f5   Divyesh Shah   cfq-iosched: Fix ...
3172
3173
  	while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
  		__cfq_set_active_queue(cfqd, cfqq);
f04a64246   Vivek Goyal   blkio: Keep queue...
3174
  		dispatched += __cfq_forced_dispatch_cfqq(cfqq);
3440c49f5   Divyesh Shah   cfq-iosched: Fix ...
3175
  	}
1b5ed5e1f   Tejun Heo   [BLOCK] cfq-iosch...
3176

1b5ed5e1f   Tejun Heo   [BLOCK] cfq-iosch...
3177
  	BUG_ON(cfqd->busy_queues);
6923715ae   Jeff Moyer   cfq: remove extra...
3178
  	cfq_log(cfqd, "forced_dispatch=%d", dispatched);
1b5ed5e1f   Tejun Heo   [BLOCK] cfq-iosch...
3179
3180
  	return dispatched;
  }
abc3c744d   Shaohua Li   cfq-iosched: quan...
3181
3182
3183
  static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
  	struct cfq_queue *cfqq)
  {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3184
  	u64 now = ktime_get_ns();
abc3c744d   Shaohua Li   cfq-iosched: quan...
3185
3186
  	/* the queue hasn't finished any request, can't estimate */
  	if (cfq_cfqq_slice_new(cfqq))
c1e44756f   Shaohua Li   cfq-iosched: do c...
3187
  		return true;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3188
  	if (now + cfqd->cfq_slice_idle * cfqq->dispatched > cfqq->slice_end)
c1e44756f   Shaohua Li   cfq-iosched: do c...
3189
  		return true;
abc3c744d   Shaohua Li   cfq-iosched: quan...
3190

c1e44756f   Shaohua Li   cfq-iosched: do c...
3191
  	return false;
abc3c744d   Shaohua Li   cfq-iosched: quan...
3192
  }
0b182d617   Jens Axboe   cfq-iosched: abst...
3193
  static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3194
  {
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3195
  	unsigned int max_dispatch;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3196

3932a86b4   Glauber Costa   cfq: fix starvati...
3197
3198
  	if (cfq_cfqq_must_dispatch(cfqq))
  		return true;
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3199
  	/*
5ad531db6   Jens Axboe   cfq-iosched: drai...
3200
3201
  	 * Drain async requests before we start sync IO
  	 */
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3202
  	if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
0b182d617   Jens Axboe   cfq-iosched: abst...
3203
  		return false;
5ad531db6   Jens Axboe   cfq-iosched: drai...
3204
3205
  
  	/*
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3206
3207
  	 * If this is an async queue and we have sync IO in flight, let it wait
  	 */
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3208
  	if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
0b182d617   Jens Axboe   cfq-iosched: abst...
3209
  		return false;
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3210

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

2f5cb7381   Jens Axboe   cfq-iosched: chan...
3215
3216
3217
3218
  	/*
  	 * Does this cfqq already have too much IO in flight?
  	 */
  	if (cfqq->dispatched >= max_dispatch) {
ef8a41df8   Shaohua Li   cfq-iosched: give...
3219
  		bool promote_sync = false;
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3220
3221
3222
  		/*
  		 * idle queue must always only have a single IO in flight
  		 */
3ed9a2965   Jens Axboe   cfq-iosched: impr...
3223
  		if (cfq_class_idle(cfqq))
0b182d617   Jens Axboe   cfq-iosched: abst...
3224
  			return false;
3ed9a2965   Jens Axboe   cfq-iosched: impr...
3225

2f5cb7381   Jens Axboe   cfq-iosched: chan...
3226
  		/*
c4ade94fc   Li, Shaohua   cfq-iosched: remo...
3227
3228
  		 * If there is only one sync queue
  		 * we can ignore async queue here and give the sync
ef8a41df8   Shaohua Li   cfq-iosched: give...
3229
3230
3231
3232
  		 * 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...
3233
3234
  		if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
  			promote_sync = true;
ef8a41df8   Shaohua Li   cfq-iosched: give...
3235
3236
  
  		/*
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3237
3238
  		 * We have other queues, don't allow more IO from this one
  		 */
ef8a41df8   Shaohua Li   cfq-iosched: give...
3239
3240
  		if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
  				!promote_sync)
0b182d617   Jens Axboe   cfq-iosched: abst...
3241
  			return false;
9ede209e8   Jens Axboe   cfq-iosched: impr...
3242

2f5cb7381   Jens Axboe   cfq-iosched: chan...
3243
  		/*
474b18ccc   Shaohua Li   cfq-iosched: no d...
3244
  		 * Sole queue user, no limit
365722bb9   Vivek Goyal   cfq-iosched: dela...
3245
  		 */
ef8a41df8   Shaohua Li   cfq-iosched: give...
3246
  		if (cfqd->busy_queues == 1 || promote_sync)
abc3c744d   Shaohua Li   cfq-iosched: quan...
3247
3248
3249
3250
3251
3252
3253
3254
3255
  			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...
3256
3257
3258
3259
3260
3261
3262
  	}
  
  	/*
  	 * 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...
3263
  	if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3264
  		u64 last_sync = ktime_get_ns() - cfqd->last_delayed_sync;
8e2967555   Jens Axboe   cfq-iosched: impl...
3265
  		unsigned int depth;
365722bb9   Vivek Goyal   cfq-iosched: dela...
3266

9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3267
  		depth = div64_u64(last_sync, cfqd->cfq_slice[1]);
e00c54c36   Jens Axboe   cfq-iosched: don'...
3268
3269
  		if (!depth && !cfqq->dispatched)
  			depth = 1;
8e2967555   Jens Axboe   cfq-iosched: impl...
3270
3271
  		if (depth < max_dispatch)
  			max_dispatch = depth;
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3272
  	}
3ed9a2965   Jens Axboe   cfq-iosched: impr...
3273

0b182d617   Jens Axboe   cfq-iosched: abst...
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
  	/*
  	 * 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));
3932a86b4   Glauber Costa   cfq: fix starvati...
3289
3290
3291
  	rq = cfq_check_fifo(cfqq);
  	if (rq)
  		cfq_mark_cfqq_must_dispatch(cfqq);
0b182d617   Jens Axboe   cfq-iosched: abst...
3292
3293
3294
3295
3296
3297
  	if (!cfq_may_dispatch(cfqd, cfqq))
  		return false;
  
  	/*
  	 * follow expired path, else get first next available
  	 */
0b182d617   Jens Axboe   cfq-iosched: abst...
3298
3299
  	if (!rq)
  		rq = cfqq->next_rq;
3932a86b4   Glauber Costa   cfq: fix starvati...
3300
3301
  	else
  		cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
0b182d617   Jens Axboe   cfq-iosched: abst...
3302
3303
3304
3305
3306
3307
3308
  
  	/*
  	 * insert request into driver dispatch list
  	 */
  	cfq_dispatch_insert(cfqd->queue, rq);
  
  	if (!cfqd->active_cic) {
c58698073   Tejun Heo   block, cfq: reorg...
3309
  		struct cfq_io_cq *cic = RQ_CIC(rq);
0b182d617   Jens Axboe   cfq-iosched: abst...
3310

c58698073   Tejun Heo   block, cfq: reorg...
3311
  		atomic_long_inc(&cic->icq.ioc->refcount);
0b182d617   Jens Axboe   cfq-iosched: abst...
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
  		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...
3335
  		return 0;
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3336
  	/*
0b182d617   Jens Axboe   cfq-iosched: abst...
3337
  	 * Dispatch a request from this cfqq, if it is allowed
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3338
  	 */
0b182d617   Jens Axboe   cfq-iosched: abst...
3339
3340
  	if (!cfq_dispatch_request(cfqd, cfqq))
  		return 0;
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3341
  	cfqq->slice_dispatch++;
b029195dd   Jens Axboe   cfq-iosched: don'...
3342
  	cfq_clear_cfqq_must_dispatch(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3343

2f5cb7381   Jens Axboe   cfq-iosched: chan...
3344
3345
3346
3347
3348
3349
3350
  	/*
  	 * 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))) {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3351
  		cfqq->slice_end = ktime_get_ns() + 1;
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
3352
  		cfq_slice_expired(cfqd, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3353
  	}
b217a903a   Shan Wei   cfq: fix the log ...
3354
  	cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3355
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3356
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3357
  /*
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3358
3359
   * 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
3360
   *
b1c357696   Vivek Goyal   blkio: Take care ...
3361
   * Each cfq queue took a reference on the parent group. Drop it now.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3362
3363
3364
3365
   * queue lock must be held here.
   */
  static void cfq_put_queue(struct cfq_queue *cfqq)
  {
22e2c507c   Jens Axboe   [PATCH] Update cf...
3366
  	struct cfq_data *cfqd = cfqq->cfqd;
0bbfeb832   Justin TerAvest   cfq-iosched: Alwa...
3367
  	struct cfq_group *cfqg;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3368

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

30d7b9448   Shaohua Li   block cfq: don't ...
3371
3372
  	cfqq->ref--;
  	if (cfqq->ref)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3373
  		return;
7b679138b   Jens Axboe   cfq-iosched: add ...
3374
  	cfq_log_cfqq(cfqd, cfqq, "put_queue");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3375
  	BUG_ON(rb_first(&cfqq->sort_list));
22e2c507c   Jens Axboe   [PATCH] Update cf...
3376
  	BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
b1c357696   Vivek Goyal   blkio: Take care ...
3377
  	cfqg = cfqq->cfqg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3378

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

f04a64246   Vivek Goyal   blkio: Keep queue...
3384
  	BUG_ON(cfq_cfqq_on_rr(cfqq));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3385
  	kmem_cache_free(cfq_pool, cfqq);
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
3386
  	cfqg_put(cfqg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3387
  }
d02a2c077   Shaohua Li   cfq-iosched: fix ...
3388
  static void cfq_put_cooperator(struct cfq_queue *cfqq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3389
  {
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
3390
  	struct cfq_queue *__cfqq, *next;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
  	/*
  	 * 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 ...
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
  }
  
  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...
3417

89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
3418
3419
  	cfq_put_queue(cfqq);
  }
22e2c507c   Jens Axboe   [PATCH] Update cf...
3420

9b84cacd0   Tejun Heo   block, cfq: restr...
3421
3422
3423
  static void cfq_init_icq(struct io_cq *icq)
  {
  	struct cfq_io_cq *cic = icq_to_cic(icq);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3424
  	cic->ttime.last_end_request = ktime_get_ns();
9b84cacd0   Tejun Heo   block, cfq: restr...
3425
  }
c58698073   Tejun Heo   block, cfq: reorg...
3426
  static void cfq_exit_icq(struct io_cq *icq)
89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
3427
  {
c58698073   Tejun Heo   block, cfq: reorg...
3428
  	struct cfq_io_cq *cic = icq_to_cic(icq);
283287a52   Tejun Heo   block, cfq: misc ...
3429
  	struct cfq_data *cfqd = cic_to_cfqd(cic);
4faa3c815   Fabio Checconi   cfq-iosched: do n...
3430

563180a44   Tejun Heo   cfq-iosched: mino...
3431
3432
3433
  	if (cic_to_cfqq(cic, false)) {
  		cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, false));
  		cic_set_cfqq(cic, NULL, false);
12a057321   Al Viro   [PATCH] keep sync...
3434
  	}
563180a44   Tejun Heo   cfq-iosched: mino...
3435
3436
3437
  	if (cic_to_cfqq(cic, true)) {
  		cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, true));
  		cic_set_cfqq(cic, NULL, true);
12a057321   Al Viro   [PATCH] keep sync...
3438
  	}
89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
3439
  }
abede6da2   Tejun Heo   cfq: pass around ...
3440
  static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3441
3442
3443
  {
  	struct task_struct *tsk = current;
  	int ioprio_class;
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
3444
  	if (!cfq_cfqq_prio_changed(cfqq))
22e2c507c   Jens Axboe   [PATCH] Update cf...
3445
  		return;
598971bfb   Tejun Heo   cfq: don't use ic...
3446
  	ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3447
  	switch (ioprio_class) {
fe094d98e   Jens Axboe   cfq-iosched: make...
3448
3449
3450
3451
3452
  	default:
  		printk(KERN_ERR "cfq: bad prio %x
  ", ioprio_class);
  	case IOPRIO_CLASS_NONE:
  		/*
6d63c2755   Jens Axboe   cfq-iosched: make...
3453
  		 * no prio set, inherit CPU scheduling settings
fe094d98e   Jens Axboe   cfq-iosched: make...
3454
3455
  		 */
  		cfqq->ioprio = task_nice_ioprio(tsk);
6d63c2755   Jens Axboe   cfq-iosched: make...
3456
  		cfqq->ioprio_class = task_nice_ioclass(tsk);
fe094d98e   Jens Axboe   cfq-iosched: make...
3457
3458
  		break;
  	case IOPRIO_CLASS_RT:
598971bfb   Tejun Heo   cfq: don't use ic...
3459
  		cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
fe094d98e   Jens Axboe   cfq-iosched: make...
3460
3461
3462
  		cfqq->ioprio_class = IOPRIO_CLASS_RT;
  		break;
  	case IOPRIO_CLASS_BE:
598971bfb   Tejun Heo   cfq: don't use ic...
3463
  		cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
fe094d98e   Jens Axboe   cfq-iosched: make...
3464
3465
3466
3467
3468
3469
3470
  		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...
3471
3472
3473
3474
3475
3476
3477
  	}
  
  	/*
  	 * keep track of original prio settings in case we have to temporarily
  	 * elevate the priority of this queue
  	 */
  	cfqq->org_ioprio = cfqq->ioprio;
b8269db45   Jens Axboe   cfq-iosched: temp...
3478
  	cfqq->org_ioprio_class = cfqq->ioprio_class;
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
3479
  	cfq_clear_cfqq_prio_changed(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3480
  }
598971bfb   Tejun Heo   cfq: don't use ic...
3481
  static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3482
  {
598971bfb   Tejun Heo   cfq: don't use ic...
3483
  	int ioprio = cic->icq.ioc->ioprio;
bca4b914b   Konstantin Khlebnikov   cfq-iosched: remo...
3484
  	struct cfq_data *cfqd = cic_to_cfqd(cic);
478a82b0e   Al Viro   [PATCH] switch to...
3485
  	struct cfq_queue *cfqq;
35e6077cb   Jens Axboe   [PATCH] cfq-iosch...
3486

598971bfb   Tejun Heo   cfq: don't use ic...
3487
3488
3489
3490
3491
  	/*
  	 * 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...
3492
  		return;
563180a44   Tejun Heo   cfq-iosched: mino...
3493
  	cfqq = cic_to_cfqq(cic, false);
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
3494
  	if (cfqq) {
563180a44   Tejun Heo   cfq-iosched: mino...
3495
  		cfq_put_queue(cfqq);
2da8de0bb   Tejun Heo   cfq-iosched: remo...
3496
  		cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio);
563180a44   Tejun Heo   cfq-iosched: mino...
3497
  		cic_set_cfqq(cic, cfqq, false);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3498
  	}
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
3499

563180a44   Tejun Heo   cfq-iosched: mino...
3500
  	cfqq = cic_to_cfqq(cic, true);
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
3501
3502
  	if (cfqq)
  		cfq_mark_cfqq_prio_changed(cfqq);
598971bfb   Tejun Heo   cfq: don't use ic...
3503
3504
  
  	cic->ioprio = ioprio;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3505
  }
d5036d770   Jens Axboe   cfq-iosched: move...
3506
  static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3507
  			  pid_t pid, bool is_sync)
d5036d770   Jens Axboe   cfq-iosched: move...
3508
3509
3510
3511
  {
  	RB_CLEAR_NODE(&cfqq->rb_node);
  	RB_CLEAR_NODE(&cfqq->p_node);
  	INIT_LIST_HEAD(&cfqq->fifo);
30d7b9448   Shaohua Li   block cfq: don't ...
3512
  	cfqq->ref = 0;
d5036d770   Jens Axboe   cfq-iosched: move...
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
  	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...
3524
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
142bbdfcc   Jan Kara   cfq: Disable writ...
3525
  static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
24610333d   Vivek Goyal   blkio: Drop the r...
3526
  {
bca4b914b   Konstantin Khlebnikov   cfq-iosched: remo...
3527
  	struct cfq_data *cfqd = cic_to_cfqd(cic);
60a837077   Tejun Heo   cfq-iosched: char...
3528
  	struct cfq_queue *cfqq;
f4da80727   Tejun Heo   blkcg: remove blk...
3529
  	uint64_t serial_nr;
24610333d   Vivek Goyal   blkio: Drop the r...
3530

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

598971bfb   Tejun Heo   cfq: don't use ic...
3535
3536
3537
3538
  	/*
  	 * 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...
3539
  	if (unlikely(!cfqd) || likely(cic->blkcg_serial_nr == serial_nr))
142bbdfcc   Jan Kara   cfq: Disable writ...
3540
  		return;
87760e5ee   Jens Axboe   block: hook up wr...
3541
3542
  
  	/*
60a837077   Tejun Heo   cfq-iosched: char...
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
  	 * Drop reference to queues.  New queues will be assigned in new
  	 * group upon arrival of fresh requests.
  	 */
  	cfqq = cic_to_cfqq(cic, false);
  	if (cfqq) {
  		cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
  		cic_set_cfqq(cic, NULL, false);
  		cfq_put_queue(cfqq);
  	}
  
  	cfqq = cic_to_cfqq(cic, true);
  	if (cfqq) {
  		cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
  		cic_set_cfqq(cic, NULL, true);
  		cfq_put_queue(cfqq);
24610333d   Vivek Goyal   blkio: Drop the r...
3558
  	}
598971bfb   Tejun Heo   cfq: don't use ic...
3559

f4da80727   Tejun Heo   blkcg: remove blk...
3560
  	cic->blkcg_serial_nr = serial_nr;
24610333d   Vivek Goyal   blkio: Drop the r...
3561
  }
598971bfb   Tejun Heo   cfq: don't use ic...
3562
  #else
142bbdfcc   Jan Kara   cfq: Disable writ...
3563
  static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
5d7f5ce15   Jens Axboe   cfq-iosched: don'...
3564
  {
5d7f5ce15   Jens Axboe   cfq-iosched: don'...
3565
  }
24610333d   Vivek Goyal   blkio: Drop the r...
3566
  #endif  /* CONFIG_CFQ_GROUP_IOSCHED */
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3567
  static struct cfq_queue **
60a837077   Tejun Heo   cfq-iosched: char...
3568
  cfq_async_queue_prio(struct cfq_group *cfqg, int ioprio_class, int ioprio)
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3569
  {
fe094d98e   Jens Axboe   cfq-iosched: make...
3570
  	switch (ioprio_class) {
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3571
  	case IOPRIO_CLASS_RT:
60a837077   Tejun Heo   cfq-iosched: char...
3572
  		return &cfqg->async_cfqq[0][ioprio];
598971bfb   Tejun Heo   cfq: don't use ic...
3573
3574
3575
  	case IOPRIO_CLASS_NONE:
  		ioprio = IOPRIO_NORM;
  		/* fall through */
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3576
  	case IOPRIO_CLASS_BE:
60a837077   Tejun Heo   cfq-iosched: char...
3577
  		return &cfqg->async_cfqq[1][ioprio];
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3578
  	case IOPRIO_CLASS_IDLE:
60a837077   Tejun Heo   cfq-iosched: char...
3579
  		return &cfqg->async_idle_cfqq;
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3580
3581
3582
3583
  	default:
  		BUG();
  	}
  }
15c31be4d   Jens Axboe   cfq-iosched: fix ...
3584
  static struct cfq_queue *
abede6da2   Tejun Heo   cfq: pass around ...
3585
  cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
2da8de0bb   Tejun Heo   cfq-iosched: remo...
3586
  	      struct bio *bio)
15c31be4d   Jens Axboe   cfq-iosched: fix ...
3587
  {
c6ce19432   Jeff Moyer   cfq-iosched: fix ...
3588
3589
  	int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
  	int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
d4aad7ff0   Tejun Heo   cfq-iosched: fold...
3590
  	struct cfq_queue **async_cfqq = NULL;
4ebc1c61d   Tejun Heo   cfq-iosched: simp...
3591
  	struct cfq_queue *cfqq;
322731ed0   Tejun Heo   cfq-iosched: move...
3592
3593
3594
  	struct cfq_group *cfqg;
  
  	rcu_read_lock();
ae1188963   Tejun Heo   blkcg: consolidat...
3595
  	cfqg = cfq_lookup_cfqg(cfqd, bio_blkcg(bio));
322731ed0   Tejun Heo   cfq-iosched: move...
3596
3597
3598
3599
  	if (!cfqg) {
  		cfqq = &cfqd->oom_cfqq;
  		goto out;
  	}
15c31be4d   Jens Axboe   cfq-iosched: fix ...
3600

c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3601
  	if (!is_sync) {
c6ce19432   Jeff Moyer   cfq-iosched: fix ...
3602
3603
3604
3605
3606
  		if (!ioprio_valid(cic->ioprio)) {
  			struct task_struct *tsk = current;
  			ioprio = task_nice_ioprio(tsk);
  			ioprio_class = task_nice_ioclass(tsk);
  		}
60a837077   Tejun Heo   cfq-iosched: char...
3607
  		async_cfqq = cfq_async_queue_prio(cfqg, ioprio_class, ioprio);
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3608
  		cfqq = *async_cfqq;
4ebc1c61d   Tejun Heo   cfq-iosched: simp...
3609
3610
  		if (cfqq)
  			goto out;
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3611
  	}
e00f4f4d0   Tejun Heo   block,blkcg: use ...
3612
3613
  	cfqq = kmem_cache_alloc_node(cfq_pool,
  				     GFP_NOWAIT | __GFP_ZERO | __GFP_NOWARN,
d4aad7ff0   Tejun Heo   cfq-iosched: fold...
3614
3615
3616
3617
3618
  				     cfqd->queue->node);
  	if (!cfqq) {
  		cfqq = &cfqd->oom_cfqq;
  		goto out;
  	}
4d608baac   Alexander Potapenko   block: Initialize...
3619
3620
  	/* cfq_init_cfqq() assumes cfqq->ioprio_class is initialized. */
  	cfqq->ioprio_class = IOPRIO_CLASS_NONE;
d4aad7ff0   Tejun Heo   cfq-iosched: fold...
3621
3622
3623
3624
  	cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
  	cfq_init_prio_data(cfqq, cic);
  	cfq_link_cfqq_cfqg(cfqq, cfqg);
  	cfq_log_cfqq(cfqd, cfqq, "alloced");
15c31be4d   Jens Axboe   cfq-iosched: fix ...
3625

d4aad7ff0   Tejun Heo   cfq-iosched: fold...
3626
3627
  	if (async_cfqq) {
  		/* a new async queue is created, pin and remember */
30d7b9448   Shaohua Li   block cfq: don't ...
3628
  		cfqq->ref++;
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3629
  		*async_cfqq = cfqq;
15c31be4d   Jens Axboe   cfq-iosched: fix ...
3630
  	}
4ebc1c61d   Tejun Heo   cfq-iosched: simp...
3631
  out:
30d7b9448   Shaohua Li   block cfq: don't ...
3632
  	cfqq->ref++;
322731ed0   Tejun Heo   cfq-iosched: move...
3633
  	rcu_read_unlock();
15c31be4d   Jens Axboe   cfq-iosched: fix ...
3634
3635
  	return cfqq;
  }
22e2c507c   Jens Axboe   [PATCH] Update cf...
3636
  static void
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3637
  __cfq_update_io_thinktime(struct cfq_ttime *ttime, u64 slice_idle)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3638
  {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3639
  	u64 elapsed = ktime_get_ns() - ttime->last_end_request;
383cd7213   Shaohua Li   CFQ: move think t...
3640
  	elapsed = min(elapsed, 2UL * slice_idle);
db3b5848e   Kiyoshi Ueda   When cfq I/O sche...
3641

383cd7213   Shaohua Li   CFQ: move think t...
3642
  	ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3643
3644
3645
  	ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed,  8);
  	ttime->ttime_mean = div64_ul(ttime->ttime_total + 128,
  				     ttime->ttime_samples);
383cd7213   Shaohua Li   CFQ: move think t...
3646
3647
3648
3649
  }
  
  static void
  cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
c58698073   Tejun Heo   block, cfq: reorg...
3650
  			struct cfq_io_cq *cic)
383cd7213   Shaohua Li   CFQ: move think t...
3651
  {
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
3652
  	if (cfq_cfqq_sync(cfqq)) {
383cd7213   Shaohua Li   CFQ: move think t...
3653
  		__cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
3654
3655
3656
  		__cfq_update_io_thinktime(&cfqq->service_tree->ttime,
  			cfqd->cfq_slice_idle);
  	}
7700fc4f6   Shaohua Li   CFQ: add think ti...
3657
3658
3659
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
  	__cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
  #endif
22e2c507c   Jens Axboe   [PATCH] Update cf...
3660
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3661

206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
3662
  static void
b2c18e1e0   Jeff Moyer   cfq: calculate th...
3663
  cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
6d048f531   Jens Axboe   cfq-iosched: deve...
3664
  		       struct request *rq)
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
3665
  {
3dde36dde   Corrado Zoccolo   cfq-iosched: rewo...
3666
  	sector_t sdist = 0;
41647e7a9   Corrado Zoccolo   cfq-iosched: reth...
3667
  	sector_t n_sec = blk_rq_sectors(rq);
3dde36dde   Corrado Zoccolo   cfq-iosched: rewo...
3668
3669
3670
3671
3672
3673
  	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...
3674

3dde36dde   Corrado Zoccolo   cfq-iosched: rewo...
3675
  	cfqq->seek_history <<= 1;
41647e7a9   Corrado Zoccolo   cfq-iosched: reth...
3676
3677
3678
3679
  	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...
3680
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3681

a2b809672   Christoph Hellwig   block: replace RE...
3682
3683
3684
3685
3686
  static inline bool req_noidle(struct request *req)
  {
  	return req_op(req) == REQ_OP_WRITE &&
  		(req->cmd_flags & (REQ_SYNC | REQ_IDLE)) == REQ_SYNC;
  }
22e2c507c   Jens Axboe   [PATCH] Update cf...
3687
3688
3689
3690
3691
3692
  /*
   * 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...
3693
  		       struct cfq_io_cq *cic)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3694
  {
7b679138b   Jens Axboe   cfq-iosched: add ...
3695
  	int old_idle, enable_idle;
1be92f2fc   Jens Axboe   cfq-iosched: neve...
3696

0871714e0   Jens Axboe   cfq-iosched: rela...
3697
3698
3699
3700
  	/*
  	 * 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...
3701
  		return;
c265a7f41   Jens Axboe   cfq-iosched: get ...
3702
  	enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3703

76280aff1   Corrado Zoccolo   cfq-iosched: idli...
3704
3705
  	if (cfqq->queued[0] + cfqq->queued[1] >= 4)
  		cfq_mark_cfqq_deep(cfqq);
a2b809672   Christoph Hellwig   block: replace RE...
3706
  	if (cfqq->next_rq && req_noidle(cfqq->next_rq))
749ef9f84   Corrado Zoccolo   cfq: improve fsyn...
3707
  		enable_idle = 0;
f6e8d01be   Tejun Heo   block: add io_con...
3708
  	else if (!atomic_read(&cic->icq.ioc->active_ref) ||
c58698073   Tejun Heo   block, cfq: reorg...
3709
3710
  		 !cfqd->cfq_slice_idle ||
  		 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
22e2c507c   Jens Axboe   [PATCH] Update cf...
3711
  		enable_idle = 0;
383cd7213   Shaohua Li   CFQ: move think t...
3712
3713
  	else if (sample_valid(cic->ttime.ttime_samples)) {
  		if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3714
3715
3716
  			enable_idle = 0;
  		else
  			enable_idle = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3717
  	}
7b679138b   Jens Axboe   cfq-iosched: add ...
3718
3719
3720
3721
3722
3723
3724
  	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...
3725
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3726

22e2c507c   Jens Axboe   [PATCH] Update cf...
3727
3728
3729
3730
  /*
   * 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...
3731
  static bool
22e2c507c   Jens Axboe   [PATCH] Update cf...
3732
  cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3733
  		   struct request *rq)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3734
  {
6d048f531   Jens Axboe   cfq-iosched: deve...
3735
  	struct cfq_queue *cfqq;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3736

6d048f531   Jens Axboe   cfq-iosched: deve...
3737
3738
  	cfqq = cfqd->active_queue;
  	if (!cfqq)
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3739
  		return false;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3740

6d048f531   Jens Axboe   cfq-iosched: deve...
3741
  	if (cfq_class_idle(new_cfqq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3742
  		return false;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3743
3744
  
  	if (cfq_class_idle(cfqq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3745
  		return true;
1e3335de0   Jens Axboe   cfq-iosched: impr...
3746

22e2c507c   Jens Axboe   [PATCH] Update cf...
3747
  	/*
875feb63b   Divyesh Shah   cfq-iosched: Resp...
3748
3749
3750
3751
3752
3753
  	 * 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...
3754
3755
3756
  	 * if the new request is sync, but the currently running queue is
  	 * not, let the sync request have priority.
  	 */
3932a86b4   Glauber Costa   cfq: fix starvati...
3757
  	if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3758
  		return true;
1e3335de0   Jens Axboe   cfq-iosched: impr...
3759

3984aa552   Jan Kara   cfq-iosched: Allo...
3760
3761
3762
3763
3764
3765
  	/*
  	 * Treat ancestors of current cgroup the same way as current cgroup.
  	 * For anybody else we disallow preemption to guarantee service
  	 * fairness among cgroups.
  	 */
  	if (!cfqg_is_descendant(cfqq->cfqg, new_cfqq->cfqg))
8682e1f15   Vivek Goyal   blkio: Provide so...
3766
3767
3768
3769
  		return false;
  
  	if (cfq_slice_used(cfqq))
  		return true;
6c80731c7   Jan Kara   cfq-iosched: Reor...
3770
3771
3772
3773
3774
3775
3776
  	/*
  	 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
  	 */
  	if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
  		return true;
  
  	WARN_ON_ONCE(cfqq->ioprio_class != new_cfqq->ioprio_class);
8682e1f15   Vivek Goyal   blkio: Provide so...
3777
  	/* Allow preemption only if we are idling on sync-noidle tree */
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
3778
  	if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
8682e1f15   Vivek Goyal   blkio: Provide so...
3779
  	    cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
8682e1f15   Vivek Goyal   blkio: Provide so...
3780
3781
  	    RB_EMPTY_ROOT(&cfqq->sort_list))
  		return true;
374f84ac3   Jens Axboe   [PATCH] cfq-iosch...
3782
  	/*
b53d1ed73   Jens Axboe   Revert "cfq: Remo...
3783
3784
3785
  	 * 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...
3786
  	if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
b53d1ed73   Jens Axboe   Revert "cfq: Remo...
3787
  		return true;
d2d59e18a   Shaohua Li   cfq-iosched: sche...
3788
3789
3790
  	/* 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...
3791
  	if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3792
  		return false;
1e3335de0   Jens Axboe   cfq-iosched: impr...
3793
3794
3795
3796
3797
  
  	/*
  	 * if this request is as-good as one we would expect from the
  	 * current cfqq, let it preempt
  	 */
e9ce335df   Shaohua Li   cfq-iosched: fix ...
3798
  	if (cfq_rq_close(cfqd, cfqq, rq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3799
  		return true;
1e3335de0   Jens Axboe   cfq-iosched: impr...
3800

a6151c3a5   Jens Axboe   cfq-iosched: appl...
3801
  	return false;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3802
3803
3804
3805
3806
3807
3808
3809
  }
  
  /*
   * 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...
3810
  	enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
7b679138b   Jens Axboe   cfq-iosched: add ...
3811
  	cfq_log_cfqq(cfqd, cfqq, "preempt");
df0793abb   Shaohua Li   block,cfq: change...
3812
  	cfq_slice_expired(cfqd, 1);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3813

bf5722567   Jens Axboe   [PATCH] cfq-iosch...
3814
  	/*
f8ae6e3eb   Shaohua Li   block cfq: make q...
3815
3816
3817
  	 * workload type is changed, don't save slice, otherwise preempt
  	 * doesn't happen
  	 */
df0793abb   Shaohua Li   block,cfq: change...
3818
  	if (old_type != cfqq_type(cfqq))
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
3819
  		cfqq->cfqg->saved_wl_slice = 0;
f8ae6e3eb   Shaohua Li   block cfq: make q...
3820
3821
  
  	/*
bf5722567   Jens Axboe   [PATCH] cfq-iosch...
3822
3823
3824
3825
  	 * 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 ...
3826
3827
  
  	cfq_service_tree_add(cfqd, cfqq, 1);
eda5e0c91   Justin TerAvest   cfq-iosched: Don'...
3828

62a37f6ba   Justin TerAvest   cfq-iosched: Don'...
3829
3830
  	cfqq->slice_end = 0;
  	cfq_mark_cfqq_slice_new(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3831
3832
3833
  }
  
  /*
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3834
   * Called when a new fs request (rq) is added (to cfqq). Check if there's
22e2c507c   Jens Axboe   [PATCH] Update cf...
3835
3836
3837
   * something we should do about it
   */
  static void
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3838
3839
  cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  		struct request *rq)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3840
  {
c58698073   Tejun Heo   block, cfq: reorg...
3841
  	struct cfq_io_cq *cic = RQ_CIC(rq);
12e9fddd6   Jens Axboe   [PATCH] cfq-iosch...
3842

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

383cd7213   Shaohua Li   CFQ: move think t...
3847
  	cfq_update_io_thinktime(cfqd, cfqq, cic);
b2c18e1e0   Jeff Moyer   cfq: calculate th...
3848
  	cfq_update_io_seektime(cfqd, cfqq, rq);
9c2c38a12   Jens Axboe   [PATCH] cfq-iosch...
3849
  	cfq_update_idle_window(cfqd, cfqq, cic);
b2c18e1e0   Jeff Moyer   cfq: calculate th...
3850
  	cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3851
3852
3853
  
  	if (cfqq == cfqd->active_queue) {
  		/*
b029195dd   Jens Axboe   cfq-iosched: don'...
3854
3855
3856
  		 * 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'...
3857
3858
  		 * 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...
3859
3860
3861
  		 * 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...
3862
  		 */
d6ceb25e8   Jens Axboe   cfq-iosched: don'...
3863
  		if (cfq_cfqq_wait_request(cfqq)) {
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
3864
  			if (blk_rq_bytes(rq) > PAGE_SIZE ||
2d8707229   Jens Axboe   cfq-iosched: twea...
3865
  			    cfqd->busy_queues > 1) {
812df48d1   Divyesh Shah   blkio: Add more d...
3866
  				cfq_del_timer(cfqd, cfqq);
554554f60   Gui Jianfeng   cfq: Remove wait_...
3867
  				cfq_clear_cfqq_wait_request(cfqq);
24ecfbe27   Christoph Hellwig   block: add blk_ru...
3868
  				__blk_run_queue(cfqd->queue);
a11cdaa7a   Divyesh Shah   block: Update to ...
3869
  			} else {
155fead9b   Tejun Heo   blkcg: move blkio...
3870
  				cfqg_stats_update_idle_time(cfqq->cfqg);
bf7919371   Vivek Goyal   blkio: Set must_d...
3871
  				cfq_mark_cfqq_must_dispatch(cfqq);
a11cdaa7a   Divyesh Shah   block: Update to ...
3872
  			}
d6ceb25e8   Jens Axboe   cfq-iosched: don'...
3873
  		}
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3874
  	} else if (cfq_should_preempt(cfqd, cfqq, rq)) {
22e2c507c   Jens Axboe   [PATCH] Update cf...
3875
3876
3877
  		/*
  		 * 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...
3878
3879
  		 * 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...
3880
3881
  		 */
  		cfq_preempt_queue(cfqd, cfqq);
24ecfbe27   Christoph Hellwig   block: add blk_ru...
3882
  		__blk_run_queue(cfqd->queue);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3883
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3884
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
3885
  static void cfq_insert_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3886
  {
b4878f245   Jens Axboe   [PATCH] 02/05: up...
3887
  	struct cfq_data *cfqd = q->elevator->elevator_data;
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3888
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3889

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

9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3893
  	rq->fifo_time = ktime_get_ns() + cfqd->cfq_fifo_expire[rq_is_sync(rq)];
22e2c507c   Jens Axboe   [PATCH] Update cf...
3894
  	list_add_tail(&rq->queuelist, &cfqq->fifo);
aa6f6a3de   Corrado Zoccolo   cfq-iosched: prep...
3895
  	cfq_add_rq_rb(rq);
ef295ecf0   Christoph Hellwig   block: better op ...
3896
  	cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group,
155fead9b   Tejun Heo   blkcg: move blkio...
3897
  				 rq->cmd_flags);
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3898
  	cfq_rq_enqueued(cfqd, cfqq, rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3899
  }
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3900
3901
3902
3903
3904
3905
  /*
   * 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...
3906
  	struct cfq_queue *cfqq = cfqd->active_queue;
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3907
3908
  	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 ...
3909
3910
3911
  
  	if (cfqd->hw_tag == 1)
  		return;
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3912
3913
  
  	if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3914
  	    cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3915
  		return;
1a1238a7d   Shaohua Li   cfq-iosched: impr...
3916
3917
3918
3919
3920
3921
3922
  	/*
  	 * 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...
3923
  	    CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
1a1238a7d   Shaohua Li   cfq-iosched: impr...
3924
  		return;
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3925
3926
  	if (cfqd->hw_tag_samples++ < 50)
  		return;
e459dd08f   Corrado Zoccolo   cfq-iosched: fix ...
3927
  	if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3928
3929
3930
  		cfqd->hw_tag = 1;
  	else
  		cfqd->hw_tag = 0;
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3931
  }
7667aa063   Vivek Goyal   cfq-iosched: Take...
3932
3933
  static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
c58698073   Tejun Heo   block, cfq: reorg...
3934
  	struct cfq_io_cq *cic = cfqd->active_cic;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3935
  	u64 now = ktime_get_ns();
7667aa063   Vivek Goyal   cfq-iosched: Take...
3936

02a8f01b5   Justin TerAvest   cfq-iosched: Don'...
3937
3938
3939
  	/* If the queue already has requests, don't wait */
  	if (!RB_EMPTY_ROOT(&cfqq->sort_list))
  		return false;
7667aa063   Vivek Goyal   cfq-iosched: Take...
3940
3941
3942
  	/* 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...
3943
3944
3945
  	/* 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...
3946
3947
3948
3949
  	if (cfq_slice_used(cfqq))
  		return true;
  
  	/* if slice left is less than think time, wait busy */
383cd7213   Shaohua Li   CFQ: move think t...
3950
  	if (cic && sample_valid(cic->ttime.ttime_samples)
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3951
  	    && (cfqq->slice_end - now < cic->ttime.ttime_mean))
7667aa063   Vivek Goyal   cfq-iosched: Take...
3952
3953
3954
3955
3956
3957
3958
3959
3960
  		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.
  	 */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3961
  	if (cfqq->slice_end - now <= jiffies_to_nsecs(1))
7667aa063   Vivek Goyal   cfq-iosched: Take...
3962
3963
3964
3965
  		return true;
  
  	return false;
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
3966
  static void cfq_completed_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3967
  {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3968
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
b4878f245   Jens Axboe   [PATCH] 02/05: up...
3969
  	struct cfq_data *cfqd = cfqq->cfqd;
5380a101d   Jens Axboe   [PATCH] cfq-iosch...
3970
  	const int sync = rq_is_sync(rq);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3971
  	u64 now = ktime_get_ns();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3972

a2b809672   Christoph Hellwig   block: replace RE...
3973
  	cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", req_noidle(rq));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3974

45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3975
  	cfq_update_hw_tag(cfqd);
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3976
  	WARN_ON(!cfqd->rq_in_driver);
6d048f531   Jens Axboe   cfq-iosched: deve...
3977
  	WARN_ON(!cfqq->dispatched);
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3978
  	cfqd->rq_in_driver--;
6d048f531   Jens Axboe   cfq-iosched: deve...
3979
  	cfqq->dispatched--;
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
3980
  	(RQ_CFQG(rq))->dispatched--;
155fead9b   Tejun Heo   blkcg: move blkio...
3981
  	cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq),
ef295ecf0   Christoph Hellwig   block: better op ...
3982
  				     rq_io_start_time_ns(rq), rq->cmd_flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3983

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

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

383cd7213   Shaohua Li   CFQ: move think t...
3989
  		RQ_CIC(rq)->ttime.last_end_request = now;
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
3990
3991
  
  		if (cfq_cfqq_on_rr(cfqq))
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
3992
  			st = cfqq->service_tree;
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
3993
  		else
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
3994
3995
3996
3997
  			st = st_for(cfqq->cfqg, cfqq_class(cfqq),
  					cfqq_type(cfqq));
  
  		st->ttime.last_end_request = now;
149321a61   Jan Kara   cfq-iosched: Fix ...
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
  		/*
  		 * We have to do this check in jiffies since start_time is in
  		 * jiffies and it is not trivial to convert to ns. If
  		 * cfq_fifo_expire[1] ever comes close to 1 jiffie, this test
  		 * will become problematic but so far we are fine (the default
  		 * is 128 ms).
  		 */
  		if (!time_after(rq->start_time +
  				  nsecs_to_jiffies(cfqd->cfq_fifo_expire[1]),
  				jiffies))
573412b29   Corrado Zoccolo   cfq-iosched: redu...
4008
  			cfqd->last_delayed_sync = now;
365722bb9   Vivek Goyal   cfq-iosched: dela...
4009
  	}
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
4010

7700fc4f6   Shaohua Li   CFQ: add think ti...
4011
4012
4013
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
  	cfqq->cfqg->ttime.last_end_request = now;
  #endif
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
4014
4015
4016
4017
4018
  	/*
  	 * 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 ...
4019
  		const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
44f7c1606   Jens Axboe   cfq-iosched: defe...
4020
4021
4022
4023
  		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...
4024
4025
  
  		/*
7667aa063   Vivek Goyal   cfq-iosched: Take...
4026
4027
  		 * Should we wait for next request to come in before we expire
  		 * the queue.
f75edf2dc   Vivek Goyal   blkio: Wait for c...
4028
  		 */
7667aa063   Vivek Goyal   cfq-iosched: Take...
4029
  		if (cfq_should_wait_busy(cfqd, cfqq)) {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
4030
  			u64 extend_sl = cfqd->cfq_slice_idle;
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
4031
4032
  			if (!cfqd->cfq_slice_idle)
  				extend_sl = cfqd->cfq_group_idle;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
4033
  			cfqq->slice_end = now + extend_sl;
f75edf2dc   Vivek Goyal   blkio: Wait for c...
4034
  			cfq_mark_cfqq_wait_busy(cfqq);
b1ffe737f   Divyesh Shah   cfq-iosched: Add ...
4035
  			cfq_log_cfqq(cfqd, cfqq, "will busy wait");
f75edf2dc   Vivek Goyal   blkio: Wait for c...
4036
  		}
a36e71f99   Jens Axboe   cfq-iosched: add ...
4037
  		/*
8e550632c   Corrado Zoccolo   cfq-iosched: fix ...
4038
4039
4040
4041
4042
4043
  		 * 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 ...
4044
  		 */
0871714e0   Jens Axboe   cfq-iosched: rela...
4045
  		if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
4046
  			cfq_slice_expired(cfqd, 1);
8e550632c   Corrado Zoccolo   cfq-iosched: fix ...
4047
4048
  		else if (sync && cfqq_empty &&
  			 !cfq_close_cooperator(cfqd, cfqq)) {
749ef9f84   Corrado Zoccolo   cfq: improve fsyn...
4049
  			cfq_arm_slice_timer(cfqd);
8e550632c   Corrado Zoccolo   cfq-iosched: fix ...
4050
  		}
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
4051
  	}
6d048f531   Jens Axboe   cfq-iosched: deve...
4052

53c583d22   Corrado Zoccolo   cfq-iosched: requ...
4053
  	if (!cfqd->rq_in_driver)
23e018a1b   Jens Axboe   block: get rid of...
4054
  		cfq_schedule_dispatch(cfqd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4055
  }
ef295ecf0   Christoph Hellwig   block: better op ...
4056
  static void cfqq_boost_on_prio(struct cfq_queue *cfqq, unsigned int op)
b8269db45   Jens Axboe   cfq-iosched: temp...
4057
4058
4059
4060
4061
4062
  {
  	/*
  	 * If REQ_PRIO is set, boost class and prio level, if it's below
  	 * BE/NORM. If prio is not set, restore the potentially boosted
  	 * class/prio level.
  	 */
ef295ecf0   Christoph Hellwig   block: better op ...
4063
  	if (!(op & REQ_PRIO)) {
b8269db45   Jens Axboe   cfq-iosched: temp...
4064
4065
4066
4067
4068
4069
4070
4071
4072
  		cfqq->ioprio_class = cfqq->org_ioprio_class;
  		cfqq->ioprio = cfqq->org_ioprio;
  	} else {
  		if (cfq_class_idle(cfqq))
  			cfqq->ioprio_class = IOPRIO_CLASS_BE;
  		if (cfqq->ioprio > IOPRIO_NORM)
  			cfqq->ioprio = IOPRIO_NORM;
  	}
  }
89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
4073
  static inline int __cfq_may_queue(struct cfq_queue *cfqq)
22e2c507c   Jens Axboe   [PATCH] Update cf...
4074
  {
1b379d8da   Jens Axboe   cfq-iosched: get ...
4075
  	if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
4076
  		cfq_mark_cfqq_must_alloc_slice(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4077
  		return ELV_MQUEUE_MUST;
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
4078
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4079

22e2c507c   Jens Axboe   [PATCH] Update cf...
4080
  	return ELV_MQUEUE_MAY;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4081
  }
ef295ecf0   Christoph Hellwig   block: better op ...
4082
  static int cfq_may_queue(struct request_queue *q, unsigned int op)
22e2c507c   Jens Axboe   [PATCH] Update cf...
4083
4084
4085
  {
  	struct cfq_data *cfqd = q->elevator->elevator_data;
  	struct task_struct *tsk = current;
c58698073   Tejun Heo   block, cfq: reorg...
4086
  	struct cfq_io_cq *cic;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4087
4088
4089
4090
4091
4092
4093
4094
  	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 ...
4095
  	cic = cfq_cic_lookup(cfqd, tsk->io_context);
91fac317a   Vasily Tarasov   cfq-iosched: get ...
4096
4097
  	if (!cic)
  		return ELV_MQUEUE_MAY;
ef295ecf0   Christoph Hellwig   block: better op ...
4098
  	cfqq = cic_to_cfqq(cic, op_is_sync(op));
22e2c507c   Jens Axboe   [PATCH] Update cf...
4099
  	if (cfqq) {
abede6da2   Tejun Heo   cfq: pass around ...
4100
  		cfq_init_prio_data(cfqq, cic);
ef295ecf0   Christoph Hellwig   block: better op ...
4101
  		cfqq_boost_on_prio(cfqq, op);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4102

89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
4103
  		return __cfq_may_queue(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4104
4105
4106
  	}
  
  	return ELV_MQUEUE_MAY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4107
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4108
4109
4110
  /*
   * queue lock held here
   */
bb37b94c6   Jens Axboe   [BLOCK] Cleanup u...
4111
  static void cfq_put_request(struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4112
  {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
4113
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4114

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

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

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4126
4127
4128
  		cfq_put_queue(cfqq);
  	}
  }
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
4129
  static struct cfq_queue *
c58698073   Tejun Heo   block, cfq: reorg...
4130
  cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
4131
4132
4133
4134
  		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...
4135
  	cfq_mark_cfqq_coop(cfqq->new_cfqq);
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
4136
4137
4138
  	cfq_put_queue(cfqq);
  	return cic_to_cfqq(cic, 1);
  }
e6c5bc737   Jeff Moyer   cfq: break apart ...
4139
4140
4141
4142
4143
  /*
   * 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...
4144
  split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
e6c5bc737   Jeff Moyer   cfq: break apart ...
4145
4146
  {
  	if (cfqq_process_refs(cfqq) == 1) {
e6c5bc737   Jeff Moyer   cfq: break apart ...
4147
4148
  		cfqq->pid = current->pid;
  		cfq_clear_cfqq_coop(cfqq);
ae54abed6   Shaohua Li   cfq-iosched: spli...
4149
  		cfq_clear_cfqq_split_coop(cfqq);
e6c5bc737   Jeff Moyer   cfq: break apart ...
4150
4151
4152
4153
  		return cfqq;
  	}
  
  	cic_set_cfqq(cic, NULL, 1);
d02a2c077   Shaohua Li   cfq-iosched: fix ...
4154
4155
  
  	cfq_put_cooperator(cfqq);
e6c5bc737   Jeff Moyer   cfq: break apart ...
4156
4157
4158
  	cfq_put_queue(cfqq);
  	return NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4159
  /*
22e2c507c   Jens Axboe   [PATCH] Update cf...
4160
   * Allocate cfq data structures associated with this request.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4161
   */
22e2c507c   Jens Axboe   [PATCH] Update cf...
4162
  static int
852c788f8   Tejun Heo   block: implement ...
4163
4164
  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
4165
4166
  {
  	struct cfq_data *cfqd = q->elevator->elevator_data;
f1f8cc946   Tejun Heo   block, cfq: move ...
4167
  	struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4168
  	const int rw = rq_data_dir(rq);
a6151c3a5   Jens Axboe   cfq-iosched: appl...
4169
  	const bool is_sync = rq_is_sync(rq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4170
  	struct cfq_queue *cfqq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4171

216284c35   Tejun Heo   block, cfq: fix r...
4172
  	spin_lock_irq(q->queue_lock);
f1f8cc946   Tejun Heo   block, cfq: move ...
4173

598971bfb   Tejun Heo   cfq: don't use ic...
4174
  	check_ioprio_changed(cic, bio);
142bbdfcc   Jan Kara   cfq: Disable writ...
4175
  	check_blkcg_changed(cic, bio);
e6c5bc737   Jeff Moyer   cfq: break apart ...
4176
  new_queue:
91fac317a   Vasily Tarasov   cfq-iosched: get ...
4177
  	cfqq = cic_to_cfqq(cic, is_sync);
32f2e807a   Vivek Goyal   cfq-iosched: rese...
4178
  	if (!cfqq || cfqq == &cfqd->oom_cfqq) {
bce6133b0   Tejun Heo   cfq-iosched: fix ...
4179
4180
  		if (cfqq)
  			cfq_put_queue(cfqq);
2da8de0bb   Tejun Heo   cfq-iosched: remo...
4181
  		cfqq = cfq_get_queue(cfqd, is_sync, cic, bio);
91fac317a   Vasily Tarasov   cfq-iosched: get ...
4182
  		cic_set_cfqq(cic, cfqq, is_sync);
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
4183
4184
  	} else {
  		/*
e6c5bc737   Jeff Moyer   cfq: break apart ...
4185
4186
  		 * If the queue was seeky for too long, break it apart.
  		 */
ae54abed6   Shaohua Li   cfq-iosched: spli...
4187
  		if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
e6c5bc737   Jeff Moyer   cfq: break apart ...
4188
4189
4190
4191
4192
4193
4194
  			cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
  			cfqq = split_cfqq(cic, cfqq);
  			if (!cfqq)
  				goto new_queue;
  		}
  
  		/*
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
4195
4196
4197
4198
4199
4200
4201
  		 * 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 ...
4202
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4203
4204
  
  	cfqq->allocated[rw]++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4205

6fae9c251   Jens Axboe   Merge commit 'v2....
4206
  	cfqq->ref++;
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
4207
  	cfqg_get(cfqq->cfqg);
a612fddf0   Tejun Heo   block, cfq: move ...
4208
  	rq->elv.priv[0] = cfqq;
1adaf3dde   Tejun Heo   blkcg: move refcn...
4209
  	rq->elv.priv[1] = cfqq->cfqg;
216284c35   Tejun Heo   block, cfq: fix r...
4210
  	spin_unlock_irq(q->queue_lock);
5d7f5ce15   Jens Axboe   cfq-iosched: don'...
4211

5e7053747   Jens Axboe   [PATCH] cfq-iosch...
4212
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4213
  }
65f27f384   David Howells   WorkStruct: Pass ...
4214
  static void cfq_kick_queue(struct work_struct *work)
22e2c507c   Jens Axboe   [PATCH] Update cf...
4215
  {
65f27f384   David Howells   WorkStruct: Pass ...
4216
  	struct cfq_data *cfqd =
23e018a1b   Jens Axboe   block: get rid of...
4217
  		container_of(work, struct cfq_data, unplug_work);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
4218
  	struct request_queue *q = cfqd->queue;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4219

40bb54d19   Jens Axboe   cfq-iosched: no n...
4220
  	spin_lock_irq(q->queue_lock);
24ecfbe27   Christoph Hellwig   block: add blk_ru...
4221
  	__blk_run_queue(cfqd->queue);
40bb54d19   Jens Axboe   cfq-iosched: no n...
4222
  	spin_unlock_irq(q->queue_lock);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4223
4224
4225
4226
4227
  }
  
  /*
   * Timer running if the active_queue is currently idling inside its time slice
   */
911483258   Jan Kara   cfq-iosched: Conv...
4228
  static enum hrtimer_restart cfq_idle_slice_timer(struct hrtimer *timer)
22e2c507c   Jens Axboe   [PATCH] Update cf...
4229
  {
911483258   Jan Kara   cfq-iosched: Conv...
4230
4231
  	struct cfq_data *cfqd = container_of(timer, struct cfq_data,
  					     idle_slice_timer);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4232
4233
  	struct cfq_queue *cfqq;
  	unsigned long flags;
3c6bd2f87   Jens Axboe   cfq-iosched: chec...
4234
  	int timed_out = 1;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4235

7b679138b   Jens Axboe   cfq-iosched: add ...
4236
  	cfq_log(cfqd, "idle timer fired");
22e2c507c   Jens Axboe   [PATCH] Update cf...
4237
  	spin_lock_irqsave(cfqd->queue->queue_lock, flags);
fe094d98e   Jens Axboe   cfq-iosched: make...
4238
4239
  	cfqq = cfqd->active_queue;
  	if (cfqq) {
3c6bd2f87   Jens Axboe   cfq-iosched: chec...
4240
  		timed_out = 0;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4241
  		/*
b029195dd   Jens Axboe   cfq-iosched: don'...
4242
4243
4244
4245
4246
4247
  		 * 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...
4248
4249
  		 * expired
  		 */
44f7c1606   Jens Axboe   cfq-iosched: defe...
4250
  		if (cfq_slice_used(cfqq))
22e2c507c   Jens Axboe   [PATCH] Update cf...
4251
4252
4253
4254
4255
4256
  			goto expire;
  
  		/*
  		 * only expire and reinvoke request handler, if there are
  		 * other queues with pending requests
  		 */
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
4257
  		if (!cfqd->busy_queues)
22e2c507c   Jens Axboe   [PATCH] Update cf...
4258
  			goto out_cont;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4259
4260
4261
4262
  
  		/*
  		 * not expired and it has a request pending, let it dispatch
  		 */
75e50984f   Jens Axboe   cfq-iosched: kill...
4263
  		if (!RB_EMPTY_ROOT(&cfqq->sort_list))
22e2c507c   Jens Axboe   [PATCH] Update cf...
4264
  			goto out_kick;
76280aff1   Corrado Zoccolo   cfq-iosched: idli...
4265
4266
4267
4268
4269
  
  		/*
  		 * Queue depth flag is reset only when the idle didn't succeed
  		 */
  		cfq_clear_cfqq_deep(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4270
4271
  	}
  expire:
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
4272
  	cfq_slice_expired(cfqd, timed_out);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4273
  out_kick:
23e018a1b   Jens Axboe   block: get rid of...
4274
  	cfq_schedule_dispatch(cfqd);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4275
4276
  out_cont:
  	spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
911483258   Jan Kara   cfq-iosched: Conv...
4277
  	return HRTIMER_NORESTART;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4278
  }
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
4279
4280
  static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
  {
911483258   Jan Kara   cfq-iosched: Conv...
4281
  	hrtimer_cancel(&cfqd->idle_slice_timer);
23e018a1b   Jens Axboe   block: get rid of...
4282
  	cancel_work_sync(&cfqd->unplug_work);
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
4283
  }
22e2c507c   Jens Axboe   [PATCH] Update cf...
4284

b374d18a4   Jens Axboe   block: get rid of...
4285
  static void cfq_exit_queue(struct elevator_queue *e)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4286
  {
22e2c507c   Jens Axboe   [PATCH] Update cf...
4287
  	struct cfq_data *cfqd = e->elevator_data;
165125e1e   Jens Axboe   [BLOCK] Get rid o...
4288
  	struct request_queue *q = cfqd->queue;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4289

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

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

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

03aa264ac   Tejun Heo   blkcg: let blkcg ...
4297
  	spin_unlock_irq(q->queue_lock);
a90d742e4   Al Viro   [PATCH] don't bot...
4298
  	cfq_shutdown_timer_wq(cfqd);
ffea73fc7   Tejun Heo   block: blkcg_poli...
4299
4300
4301
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
  	blkcg_deactivate_policy(q, &blkcg_policy_cfq);
  #else
f51b802c1   Tejun Heo   blkcg: use the us...
4302
  	kfree(cfqd->root_group);
2abae55f5   Vivek Goyal   cfq-iosched: Fix ...
4303
  #endif
56edf7d75   Vivek Goyal   cfq-iosched: Fix ...
4304
  	kfree(cfqd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4305
  }
d50235b7b   Jianpeng Ma   elevator: Fix a r...
4306
  static int cfq_init_queue(struct request_queue *q, struct elevator_type *e)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4307
4308
  {
  	struct cfq_data *cfqd;
3c798398e   Tejun Heo   blkcg: mass renam...
4309
  	struct blkcg_gq *blkg __maybe_unused;
a2b1693ba   Tejun Heo   blkcg: implement ...
4310
  	int i, ret;
d50235b7b   Jianpeng Ma   elevator: Fix a r...
4311
4312
4313
4314
4315
  	struct elevator_queue *eq;
  
  	eq = elevator_alloc(q, e);
  	if (!eq)
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4316

c1b511eb2   Joe Perches   block: Convert km...
4317
  	cfqd = kzalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
d50235b7b   Jianpeng Ma   elevator: Fix a r...
4318
4319
  	if (!cfqd) {
  		kobject_put(&eq->kobj);
b2fab5acd   Tejun Heo   elevator: make el...
4320
  		return -ENOMEM;
d50235b7b   Jianpeng Ma   elevator: Fix a r...
4321
4322
  	}
  	eq->elevator_data = cfqd;
80b15c738   Konstantin Khlebnikov   cfq-iosched: comp...
4323

f51b802c1   Tejun Heo   blkcg: use the us...
4324
  	cfqd->queue = q;
d50235b7b   Jianpeng Ma   elevator: Fix a r...
4325
4326
4327
  	spin_lock_irq(q->queue_lock);
  	q->elevator = eq;
  	spin_unlock_irq(q->queue_lock);
f51b802c1   Tejun Heo   blkcg: use the us...
4328

1fa8f6d68   Vivek Goyal   blkio: Introduce ...
4329
4330
  	/* Init root service tree */
  	cfqd->grp_service_tree = CFQ_RB_ROOT;
f51b802c1   Tejun Heo   blkcg: use the us...
4331
  	/* Init root group and prefer root group over other groups by default */
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
4332
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398e   Tejun Heo   blkcg: mass renam...
4333
  	ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
a2b1693ba   Tejun Heo   blkcg: implement ...
4334
4335
  	if (ret)
  		goto out_free;
f51b802c1   Tejun Heo   blkcg: use the us...
4336

a2b1693ba   Tejun Heo   blkcg: implement ...
4337
  	cfqd->root_group = blkg_to_cfqg(q->root_blkg);
f51b802c1   Tejun Heo   blkcg: use the us...
4338
  #else
a2b1693ba   Tejun Heo   blkcg: implement ...
4339
  	ret = -ENOMEM;
f51b802c1   Tejun Heo   blkcg: use the us...
4340
4341
  	cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
  					GFP_KERNEL, cfqd->queue->node);
a2b1693ba   Tejun Heo   blkcg: implement ...
4342
4343
  	if (!cfqd->root_group)
  		goto out_free;
5624a4e44   Vivek Goyal   blk-throttle: Mak...
4344

a2b1693ba   Tejun Heo   blkcg: implement ...
4345
  	cfq_init_cfqg_base(cfqd->root_group);
3ecca6293   Tejun Heo   blkcg: s/CFQ_WEIG...
4346
4347
  	cfqd->root_group->weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
  	cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
69d7fde59   Tejun Heo   blkcg: use CGROUP...
4348
  #endif
5624a4e44   Vivek Goyal   blk-throttle: Mak...
4349

26a2ac009   Jens Axboe   cfq-iosched: clea...
4350
4351
4352
4353
4354
4355
4356
  	/*
  	 * 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 ...
4357
  	/*
d4aad7ff0   Tejun Heo   cfq-iosched: fold...
4358
  	 * Our fallback cfqq if cfq_get_queue() runs into OOM issues.
6118b70b3   Jens Axboe   cfq-iosched: get ...
4359
  	 * Grab a permanent reference to it, so that the normal code flow
f51b802c1   Tejun Heo   blkcg: use the us...
4360
4361
4362
  	 * 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 ...
4363
4364
  	 */
  	cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
30d7b9448   Shaohua Li   block cfq: don't ...
4365
  	cfqd->oom_cfqq.ref++;
1adaf3dde   Tejun Heo   blkcg: move refcn...
4366
4367
  
  	spin_lock_irq(q->queue_lock);
f51b802c1   Tejun Heo   blkcg: use the us...
4368
  	cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
4369
  	cfqg_put(cfqd->root_group);
1adaf3dde   Tejun Heo   blkcg: move refcn...
4370
  	spin_unlock_irq(q->queue_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4371

911483258   Jan Kara   cfq-iosched: Conv...
4372
4373
  	hrtimer_init(&cfqd->idle_slice_timer, CLOCK_MONOTONIC,
  		     HRTIMER_MODE_REL);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4374
  	cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4375

23e018a1b   Jens Axboe   block: get rid of...
4376
  	INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4377

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4378
  	cfqd->cfq_quantum = cfq_quantum;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4379
4380
  	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
4381
4382
  	cfqd->cfq_back_max = cfq_back_max;
  	cfqd->cfq_back_penalty = cfq_back_penalty;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4383
4384
  	cfqd->cfq_slice[0] = cfq_slice_async;
  	cfqd->cfq_slice[1] = cfq_slice_sync;
5bf14c072   Tao Ma   block: Make cfq_t...
4385
  	cfqd->cfq_target_latency = cfq_target_latency;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4386
  	cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
0bb979472   Jens Axboe   cfq-iosched: fix ...
4387
  	cfqd->cfq_slice_idle = cfq_slice_idle;
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
4388
  	cfqd->cfq_group_idle = cfq_group_idle;
963b72fc6   Jens Axboe   cfq-iosched: rena...
4389
  	cfqd->cfq_latency = 1;
e459dd08f   Corrado Zoccolo   cfq-iosched: fix ...
4390
  	cfqd->hw_tag = -1;
edc71131c   Corrado Zoccolo   cfq-iosched: comm...
4391
4392
4393
4394
  	/*
  	 * we optimistically start assuming sync ops weren't delayed in last
  	 * second, in order to have larger depth for async operations.
  	 */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
4395
  	cfqd->last_delayed_sync = ktime_get_ns() - NSEC_PER_SEC;
b2fab5acd   Tejun Heo   elevator: make el...
4396
  	return 0;
a2b1693ba   Tejun Heo   blkcg: implement ...
4397
4398
4399
  
  out_free:
  	kfree(cfqd);
d50235b7b   Jianpeng Ma   elevator: Fix a r...
4400
  	kobject_put(&eq->kobj);
a2b1693ba   Tejun Heo   blkcg: implement ...
4401
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4402
  }
0bb979472   Jens Axboe   cfq-iosched: fix ...
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
  static void cfq_registered_queue(struct request_queue *q)
  {
  	struct elevator_queue *e = q->elevator;
  	struct cfq_data *cfqd = e->elevator_data;
  
  	/*
  	 * Default to IOPS mode with no idling for SSDs
  	 */
  	if (blk_queue_nonrot(q))
  		cfqd->cfq_slice_idle = 0;
142bbdfcc   Jan Kara   cfq: Disable writ...
4413
  	wbt_disable_default(q);
0bb979472   Jens Axboe   cfq-iosched: fix ...
4414
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4415
4416
4417
  /*
   * sysfs parts below -->
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4418
4419
4420
  static ssize_t
  cfq_var_show(unsigned int var, char *page)
  {
176167ad9   Masanari Iida   block: Fix format...
4421
4422
  	return sprintf(page, "%u
  ", var);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4423
  }
235f8da11   weiping zhang   block, scheduler:...
4424
4425
  static void
  cfq_var_store(unsigned int *var, const char *page)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4426
4427
4428
4429
  {
  	char *p = (char *) page;
  
  	*var = simple_strtoul(p, &p, 10);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4430
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4431
  #define SHOW_FUNCTION(__FUNC, __VAR, __CONV)				\
b374d18a4   Jens Axboe   block: get rid of...
4432
  static ssize_t __FUNC(struct elevator_queue *e, char *page)		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4433
  {									\
3d1ab40f4   Al Viro   [PATCH] elevator_...
4434
  	struct cfq_data *cfqd = e->elevator_data;			\
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
4435
  	u64 __data = __VAR;						\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4436
  	if (__CONV)							\
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
4437
  		__data = div_u64(__data, NSEC_PER_MSEC);			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4438
4439
4440
  	return cfq_var_show(__data, (page));				\
  }
  SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4441
4442
  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...
4443
4444
  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...
4445
  SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
4446
  SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4447
4448
4449
  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...
4450
  SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
5bf14c072   Tao Ma   block: Make cfq_t...
4451
  SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4452
  #undef SHOW_FUNCTION
d2d481d04   Jeff Moyer   cfq-iosched: Expo...
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
  #define USEC_SHOW_FUNCTION(__FUNC, __VAR)				\
  static ssize_t __FUNC(struct elevator_queue *e, char *page)		\
  {									\
  	struct cfq_data *cfqd = e->elevator_data;			\
  	u64 __data = __VAR;						\
  	__data = div_u64(__data, NSEC_PER_USEC);			\
  	return cfq_var_show(__data, (page));				\
  }
  USEC_SHOW_FUNCTION(cfq_slice_idle_us_show, cfqd->cfq_slice_idle);
  USEC_SHOW_FUNCTION(cfq_group_idle_us_show, cfqd->cfq_group_idle);
  USEC_SHOW_FUNCTION(cfq_slice_sync_us_show, cfqd->cfq_slice[1]);
  USEC_SHOW_FUNCTION(cfq_slice_async_us_show, cfqd->cfq_slice[0]);
  USEC_SHOW_FUNCTION(cfq_target_latency_us_show, cfqd->cfq_target_latency);
  #undef USEC_SHOW_FUNCTION
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4467
  #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV)			\
b374d18a4   Jens Axboe   block: get rid of...
4468
  static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count)	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4469
  {									\
3d1ab40f4   Al Viro   [PATCH] elevator_...
4470
  	struct cfq_data *cfqd = e->elevator_data;			\
9dd38052a   Bart Van Assche   cfq: Suppress com...
4471
4472
  	unsigned int __data, __min = (MIN), __max = (MAX);		\
  									\
235f8da11   weiping zhang   block, scheduler:...
4473
  	cfq_var_store(&__data, (page));					\
9dd38052a   Bart Van Assche   cfq: Suppress com...
4474
4475
4476
4477
  	if (__data < __min)						\
  		__data = __min;						\
  	else if (__data > __max)					\
  		__data = __max;						\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4478
  	if (__CONV)							\
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
4479
  		*(__PTR) = (u64)__data * NSEC_PER_MSEC;			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4480
4481
  	else								\
  		*(__PTR) = __data;					\
235f8da11   weiping zhang   block, scheduler:...
4482
  	return count;							\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4483
4484
  }
  STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
fe094d98e   Jens Axboe   cfq-iosched: make...
4485
4486
4487
4488
  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...
4489
  STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
fe094d98e   Jens Axboe   cfq-iosched: make...
4490
4491
  STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
  		UINT_MAX, 0);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4492
  STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
4493
  STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4494
4495
  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...
4496
4497
  STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
  		UINT_MAX, 0);
963b72fc6   Jens Axboe   cfq-iosched: rena...
4498
  STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
5bf14c072   Tao Ma   block: Make cfq_t...
4499
  STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4500
  #undef STORE_FUNCTION
d2d481d04   Jeff Moyer   cfq-iosched: Expo...
4501
4502
4503
4504
  #define USEC_STORE_FUNCTION(__FUNC, __PTR, MIN, MAX)			\
  static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count)	\
  {									\
  	struct cfq_data *cfqd = e->elevator_data;			\
9dd38052a   Bart Van Assche   cfq: Suppress com...
4505
4506
  	unsigned int __data, __min = (MIN), __max = (MAX);		\
  									\
235f8da11   weiping zhang   block, scheduler:...
4507
  	cfq_var_store(&__data, (page));					\
9dd38052a   Bart Van Assche   cfq: Suppress com...
4508
4509
4510
4511
  	if (__data < __min)						\
  		__data = __min;						\
  	else if (__data > __max)					\
  		__data = __max;						\
d2d481d04   Jeff Moyer   cfq-iosched: Expo...
4512
  	*(__PTR) = (u64)__data * NSEC_PER_USEC;				\
235f8da11   weiping zhang   block, scheduler:...
4513
  	return count;							\
d2d481d04   Jeff Moyer   cfq-iosched: Expo...
4514
4515
4516
4517
4518
4519
4520
  }
  USEC_STORE_FUNCTION(cfq_slice_idle_us_store, &cfqd->cfq_slice_idle, 0, UINT_MAX);
  USEC_STORE_FUNCTION(cfq_group_idle_us_store, &cfqd->cfq_group_idle, 0, UINT_MAX);
  USEC_STORE_FUNCTION(cfq_slice_sync_us_store, &cfqd->cfq_slice[1], 1, UINT_MAX);
  USEC_STORE_FUNCTION(cfq_slice_async_us_store, &cfqd->cfq_slice[0], 1, UINT_MAX);
  USEC_STORE_FUNCTION(cfq_target_latency_us_store, &cfqd->cfq_target_latency, 1, UINT_MAX);
  #undef USEC_STORE_FUNCTION
e572ec7e4   Al Viro   [PATCH] fix rmmod...
4521
4522
4523
4524
4525
  #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...
4526
4527
4528
4529
4530
  	CFQ_ATTR(fifo_expire_sync),
  	CFQ_ATTR(fifo_expire_async),
  	CFQ_ATTR(back_seek_max),
  	CFQ_ATTR(back_seek_penalty),
  	CFQ_ATTR(slice_sync),
d2d481d04   Jeff Moyer   cfq-iosched: Expo...
4531
  	CFQ_ATTR(slice_sync_us),
e572ec7e4   Al Viro   [PATCH] fix rmmod...
4532
  	CFQ_ATTR(slice_async),
d2d481d04   Jeff Moyer   cfq-iosched: Expo...
4533
  	CFQ_ATTR(slice_async_us),
e572ec7e4   Al Viro   [PATCH] fix rmmod...
4534
4535
  	CFQ_ATTR(slice_async_rq),
  	CFQ_ATTR(slice_idle),
d2d481d04   Jeff Moyer   cfq-iosched: Expo...
4536
  	CFQ_ATTR(slice_idle_us),
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
4537
  	CFQ_ATTR(group_idle),
d2d481d04   Jeff Moyer   cfq-iosched: Expo...
4538
  	CFQ_ATTR(group_idle_us),
963b72fc6   Jens Axboe   cfq-iosched: rena...
4539
  	CFQ_ATTR(low_latency),
5bf14c072   Tao Ma   block: Make cfq_t...
4540
  	CFQ_ATTR(target_latency),
d2d481d04   Jeff Moyer   cfq-iosched: Expo...
4541
  	CFQ_ATTR(target_latency_us),
e572ec7e4   Al Viro   [PATCH] fix rmmod...
4542
  	__ATTR_NULL
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4543
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4544
  static struct elevator_type iosched_cfq = {
c51ca6cf5   Jens Axboe   block: move exist...
4545
  	.ops.sq = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4546
4547
4548
  		.elevator_merge_fn = 		cfq_merge,
  		.elevator_merged_fn =		cfq_merged_request,
  		.elevator_merge_req_fn =	cfq_merged_requests,
72ef799b3   Tahsin Erdogan   block: do not mer...
4549
4550
  		.elevator_allow_bio_merge_fn =	cfq_allow_bio_merge,
  		.elevator_allow_rq_merge_fn =	cfq_allow_rq_merge,
812d40264   Divyesh Shah   blkio: Add io_mer...
4551
  		.elevator_bio_merged_fn =	cfq_bio_merged,
b4878f245   Jens Axboe   [PATCH] 02/05: up...
4552
  		.elevator_dispatch_fn =		cfq_dispatch_requests,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4553
  		.elevator_add_req_fn =		cfq_insert_request,
b4878f245   Jens Axboe   [PATCH] 02/05: up...
4554
  		.elevator_activate_req_fn =	cfq_activate_request,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4555
  		.elevator_deactivate_req_fn =	cfq_deactivate_request,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4556
  		.elevator_completed_req_fn =	cfq_completed_request,
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
4557
4558
  		.elevator_former_req_fn =	elv_rb_former_request,
  		.elevator_latter_req_fn =	elv_rb_latter_request,
9b84cacd0   Tejun Heo   block, cfq: restr...
4559
  		.elevator_init_icq_fn =		cfq_init_icq,
7e5a87944   Tejun Heo   block, cfq: move ...
4560
  		.elevator_exit_icq_fn =		cfq_exit_icq,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4561
4562
4563
4564
4565
  		.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,
0bb979472   Jens Axboe   cfq-iosched: fix ...
4566
  		.elevator_registered_fn =	cfq_registered_queue,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4567
  	},
3d3c2379f   Tejun Heo   block, cfq: move ...
4568
4569
  	.icq_size	=	sizeof(struct cfq_io_cq),
  	.icq_align	=	__alignof__(struct cfq_io_cq),
3d1ab40f4   Al Viro   [PATCH] elevator_...
4570
  	.elevator_attrs =	cfq_attrs,
3d3c2379f   Tejun Heo   block, cfq: move ...
4571
  	.elevator_name	=	"cfq",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4572
4573
  	.elevator_owner =	THIS_MODULE,
  };
3e2520668   Vivek Goyal   blkio: Implement ...
4574
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398e   Tejun Heo   blkcg: mass renam...
4575
  static struct blkcg_policy blkcg_policy_cfq = {
2ee867dcf   Tejun Heo   blkcg: implement ...
4576
  	.dfl_cftypes		= cfq_blkcg_files,
880f50e22   Tejun Heo   blkcg: mark exist...
4577
  	.legacy_cftypes		= cfq_blkcg_legacy_files,
f9fcc2d39   Tejun Heo   blkcg: collapse b...
4578

e4a9bde95   Tejun Heo   blkcg: replace bl...
4579
  	.cpd_alloc_fn		= cfq_cpd_alloc,
e48453c38   Arianna Avanzini   block, cgroup: im...
4580
  	.cpd_init_fn		= cfq_cpd_init,
e4a9bde95   Tejun Heo   blkcg: replace bl...
4581
  	.cpd_free_fn		= cfq_cpd_free,
69d7fde59   Tejun Heo   blkcg: use CGROUP...
4582
  	.cpd_bind_fn		= cfq_cpd_bind,
e4a9bde95   Tejun Heo   blkcg: replace bl...
4583

001bea73e   Tejun Heo   blkcg: replace bl...
4584
  	.pd_alloc_fn		= cfq_pd_alloc,
f9fcc2d39   Tejun Heo   blkcg: collapse b...
4585
  	.pd_init_fn		= cfq_pd_init,
0b39920b5   Tejun Heo   cfq-iosched: coll...
4586
  	.pd_offline_fn		= cfq_pd_offline,
001bea73e   Tejun Heo   blkcg: replace bl...
4587
  	.pd_free_fn		= cfq_pd_free,
f9fcc2d39   Tejun Heo   blkcg: collapse b...
4588
  	.pd_reset_stats_fn	= cfq_pd_reset_stats,
3e2520668   Vivek Goyal   blkio: Implement ...
4589
  };
3e2520668   Vivek Goyal   blkio: Implement ...
4590
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4591
4592
  static int __init cfq_init(void)
  {
3d3c2379f   Tejun Heo   block, cfq: move ...
4593
  	int ret;
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
4594
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398e   Tejun Heo   blkcg: mass renam...
4595
  	ret = blkcg_policy_register(&blkcg_policy_cfq);
8bd435b30   Tejun Heo   blkcg: remove sta...
4596
4597
  	if (ret)
  		return ret;
ffea73fc7   Tejun Heo   block: blkcg_poli...
4598
4599
4600
  #else
  	cfq_group_idle = 0;
  #endif
8bd435b30   Tejun Heo   blkcg: remove sta...
4601

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

3d3c2379f   Tejun Heo   block, cfq: move ...
4607
  	ret = elv_register(&iosched_cfq);
8bd435b30   Tejun Heo   blkcg: remove sta...
4608
4609
  	if (ret)
  		goto err_free_pool;
3d3c2379f   Tejun Heo   block, cfq: move ...
4610

2fdd82bd8   Adrian Bunk   block: let elv_re...
4611
  	return 0;
8bd435b30   Tejun Heo   blkcg: remove sta...
4612
4613
4614
4615
  
  err_free_pool:
  	kmem_cache_destroy(cfq_pool);
  err_pol_unreg:
ffea73fc7   Tejun Heo   block: blkcg_poli...
4616
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398e   Tejun Heo   blkcg: mass renam...
4617
  	blkcg_policy_unregister(&blkcg_policy_cfq);
ffea73fc7   Tejun Heo   block: blkcg_poli...
4618
  #endif
8bd435b30   Tejun Heo   blkcg: remove sta...
4619
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4620
4621
4622
4623
  }
  
  static void __exit cfq_exit(void)
  {
ffea73fc7   Tejun Heo   block: blkcg_poli...
4624
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398e   Tejun Heo   blkcg: mass renam...
4625
  	blkcg_policy_unregister(&blkcg_policy_cfq);
ffea73fc7   Tejun Heo   block: blkcg_poli...
4626
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4627
  	elv_unregister(&iosched_cfq);
3d3c2379f   Tejun Heo   block, cfq: move ...
4628
  	kmem_cache_destroy(cfq_pool);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4629
4630
4631
4632
4633
4634
4635
4636
  }
  
  module_init(cfq_init);
  module_exit(cfq_exit);
  
  MODULE_AUTHOR("Jens Axboe");
  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");