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>
1cc9be68e   Al Viro   [PATCH] noise rem...
11
12
  #include <linux/blkdev.h>
  #include <linux/elevator.h>
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
13
  #include <linux/ktime.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
  #include <linux/rbtree.h>
22e2c507c   Jens Axboe   [PATCH] Update cf...
15
  #include <linux/ioprio.h>
7b679138b   Jens Axboe   cfq-iosched: add ...
16
  #include <linux/blktrace_api.h>
eea8f41cc   Tejun Heo   blkcg: move block...
17
  #include <linux/blk-cgroup.h>
6e736be7f   Tejun Heo   block: make ioc g...
18
  #include "blk.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
20
21
22
  
  /*
   * tunables
   */
fe094d98e   Jens Axboe   cfq-iosched: make...
23
  /* max queue in one round of service */
abc3c744d   Shaohua Li   cfq-iosched: quan...
24
  static const int cfq_quantum = 8;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
25
  static const u64 cfq_fifo_expire[2] = { NSEC_PER_SEC / 4, NSEC_PER_SEC / 8 };
fe094d98e   Jens Axboe   cfq-iosched: make...
26
27
28
29
  /* maximum backwards seek, in KiB */
  static const int cfq_back_max = 16 * 1024;
  /* penalty of a backwards seek */
  static const int cfq_back_penalty = 2;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
30
31
  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...
32
  static const int cfq_slice_async_rq = 2;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
33
34
35
  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...
36
  static const int cfq_hist_divisor = 4;
22e2c507c   Jens Axboe   [PATCH] Update cf...
37

d9e7620e6   Jens Axboe   cfq-iosched: rewo...
38
  /*
0871714e0   Jens Axboe   cfq-iosched: rela...
39
   * offset from end of service tree
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
40
   */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
41
  #define CFQ_IDLE_DELAY		(NSEC_PER_SEC / 5)
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
42
43
44
45
  
  /*
   * below this threshold, we consider thinktime immediate
   */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
46
  #define CFQ_MIN_TT		(2 * NSEC_PER_SEC / HZ)
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
47

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

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

a612fddf0   Tejun Heo   block, cfq: move ...
57
58
59
  #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
60

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

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

e48453c38   Arianna Avanzini   block, cgroup: im...
69
  /* blkio-related constants */
3ecca6293   Tejun Heo   blkcg: s/CFQ_WEIG...
70
71
72
  #define CFQ_WEIGHT_LEGACY_MIN	10
  #define CFQ_WEIGHT_LEGACY_DFL	500
  #define CFQ_WEIGHT_LEGACY_MAX	1000
e48453c38   Arianna Avanzini   block, cgroup: im...
73

c58698073   Tejun Heo   block, cfq: reorg...
74
  struct cfq_ttime {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
75
  	u64 last_end_request;
c58698073   Tejun Heo   block, cfq: reorg...
76

9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
77
78
  	u64 ttime_total;
  	u64 ttime_mean;
c58698073   Tejun Heo   block, cfq: reorg...
79
  	unsigned long ttime_samples;
c58698073   Tejun Heo   block, cfq: reorg...
80
  };
22e2c507c   Jens Axboe   [PATCH] Update cf...
81
  /*
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
82
83
84
85
86
87
88
89
   * Most of our rbtree usage is for sorting with min extraction, so
   * if we cache the leftmost node we don't have to walk down the tree
   * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
   * move this into the elevator for the rq sorting as well.
   */
  struct cfq_rb_root {
  	struct rb_root rb;
  	struct rb_node *left;
aa6f6a3de   Corrado Zoccolo   cfq-iosched: prep...
90
  	unsigned count;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
91
  	u64 min_vdisktime;
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
92
  	struct cfq_ttime ttime;
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
93
  };
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
94
  #define CFQ_RB_ROOT	(struct cfq_rb_root) { .rb = RB_ROOT, \
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
95
  			.ttime = {.last_end_request = ktime_get_ns(),},}
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
96
97
  
  /*
6118b70b3   Jens Axboe   cfq-iosched: get ...
98
99
100
101
   * Per process-grouping structure
   */
  struct cfq_queue {
  	/* reference count */
30d7b9448   Shaohua Li   block cfq: don't ...
102
  	int ref;
6118b70b3   Jens Axboe   cfq-iosched: get ...
103
104
105
106
107
108
109
  	/* 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...
110
  	u64 rb_key;
6118b70b3   Jens Axboe   cfq-iosched: get ...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  	/* 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...
125
  	/* time when queue got scheduled in to dispatch first request. */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
126
127
128
  	u64 dispatch_start;
  	u64 allocated_slice;
  	u64 slice_dispatch;
dae739ebc   Vivek Goyal   blkio: Group time...
129
  	/* time when first request from queue completed and slice started. */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
130
131
  	u64 slice_start;
  	u64 slice_end;
93fdf1478   Jan Kara   cfq-iosched: Conv...
132
  	s64 slice_resid;
6118b70b3   Jens Axboe   cfq-iosched: get ...
133

65299a3b7   Christoph Hellwig   block: separate p...
134
135
  	/* pending priority requests */
  	int prio_pending;
6118b70b3   Jens Axboe   cfq-iosched: get ...
136
137
138
139
140
  	/* 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...
141
  	unsigned short ioprio_class, org_ioprio_class;
6118b70b3   Jens Axboe   cfq-iosched: get ...
142

c4081ba5c   Richard Kennedy   cfq: reorder cfq_...
143
  	pid_t pid;
3dde36dde   Corrado Zoccolo   cfq-iosched: rewo...
144
  	u32 seek_history;
b2c18e1e0   Jeff Moyer   cfq: calculate th...
145
  	sector_t last_request_pos;
aa6f6a3de   Corrado Zoccolo   cfq-iosched: prep...
146
  	struct cfq_rb_root *service_tree;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
147
  	struct cfq_queue *new_cfqq;
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
148
  	struct cfq_group *cfqg;
c4e7893eb   Vivek Goyal   cfq-iosched: blkt...
149
150
  	/* Number of sectors dispatched from queue in single dispatch round */
  	unsigned long nr_sectors;
6118b70b3   Jens Axboe   cfq-iosched: get ...
151
152
153
  };
  
  /*
718eee057   Corrado Zoccolo   cfq-iosched: fair...
154
   * First index in the service_trees.
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
155
156
   * IDLE is handled separately, so it has negative index
   */
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
157
  enum wl_class_t {
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
158
  	BE_WORKLOAD = 0,
615f0259e   Vivek Goyal   blkio: Implement ...
159
160
  	RT_WORKLOAD = 1,
  	IDLE_WORKLOAD = 2,
b4627321e   Vivek Goyal   cfq-iosched: Fix ...
161
  	CFQ_PRIO_NR,
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
162
163
164
  };
  
  /*
718eee057   Corrado Zoccolo   cfq-iosched: fair...
165
166
167
168
169
170
171
   * 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...
172
173
  struct cfqg_stats {
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
155fead9b   Tejun Heo   blkcg: move blkio...
174
175
176
177
178
179
180
181
  	/* 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...
182
183
184
185
186
187
188
189
190
191
192
193
194
  	/* total disk time and nr sectors dispatched by this group */
  	struct blkg_stat		time;
  #ifdef CONFIG_DEBUG_BLK_CGROUP
  	/* time not charged to this cgroup */
  	struct blkg_stat		unaccounted_time;
  	/* sum of number of ios queued across all samples */
  	struct blkg_stat		avg_queue_size_sum;
  	/* count of samples taken for average */
  	struct blkg_stat		avg_queue_size_samples;
  	/* how many times this group has been removed from service tree */
  	struct blkg_stat		dequeue;
  	/* total time spent waiting for it to be assigned a timeslice. */
  	struct blkg_stat		group_wait_time;
3c798398e   Tejun Heo   blkcg: mass renam...
195
  	/* time spent idling for this blkcg_gq */
155fead9b   Tejun Heo   blkcg: move blkio...
196
197
198
199
200
201
202
203
204
205
206
  	struct blkg_stat		idle_time;
  	/* total time with empty current active q with other requests queued */
  	struct blkg_stat		empty_time;
  	/* fields after this shouldn't be cleared on stat reset */
  	uint64_t			start_group_wait_time;
  	uint64_t			start_idle_time;
  	uint64_t			start_empty_time;
  	uint16_t			flags;
  #endif	/* CONFIG_DEBUG_BLK_CGROUP */
  #endif	/* CONFIG_CFQ_GROUP_IOSCHED */
  };
e48453c38   Arianna Avanzini   block, cgroup: im...
207
208
209
  /* Per-cgroup data */
  struct cfq_group_data {
  	/* must be the first member */
814376483   Tejun Heo   blkcg: minor upda...
210
  	struct blkcg_policy_data cpd;
e48453c38   Arianna Avanzini   block, cgroup: im...
211
212
213
214
  
  	unsigned int weight;
  	unsigned int leaf_weight;
  };
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
215
216
  /* This is per cgroup per device grouping structure */
  struct cfq_group {
f95a04afa   Tejun Heo   blkcg: embed stru...
217
218
  	/* must be the first member */
  	struct blkg_policy_data pd;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
219
220
221
222
223
  	/* group service_tree member */
  	struct rb_node rb_node;
  
  	/* group service_tree key */
  	u64 vdisktime;
e71357e11   Tejun Heo   cfq-iosched: add ...
224
225
  
  	/*
7918ffb5b   Tejun Heo   cfq-iosched: impl...
226
227
228
229
230
231
232
233
234
235
236
237
  	 * 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...
238
239
240
241
242
243
244
245
246
247
248
249
  	 * 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 ...
250
251
252
253
254
  	 * 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 ...
255
  	unsigned int weight;
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
256
  	unsigned int new_weight;
3381cb8d2   Tejun Heo   blkcg: move blkio...
257
  	unsigned int dev_weight;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
258

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

9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
282
  	u64 saved_wl_slice;
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
283
284
  	enum wl_type_t saved_wl_type;
  	enum wl_class_t saved_wl_class;
4eef30499   Tejun Heo   blkcg: move per-q...
285

80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
286
287
  	/* number of requests that are on the dispatch list or inside driver */
  	int dispatched;
7700fc4f6   Shaohua Li   CFQ: add think ti...
288
  	struct cfq_ttime ttime;
0b39920b5   Tejun Heo   cfq-iosched: coll...
289
  	struct cfqg_stats stats;	/* stats for this cfqg */
60a837077   Tejun Heo   cfq-iosched: char...
290
291
292
293
  
  	/* 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 ...
294
  };
718eee057   Corrado Zoccolo   cfq-iosched: fair...
295

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

53c583d22   Corrado Zoccolo   cfq-iosched: requ...
331
332
  	int rq_in_driver;
  	int rq_in_flight[2];
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
333
334
335
336
337
  
  	/*
  	 * queue-depth detection
  	 */
  	int rq_queued;
25776e359   Jens Axboe   [PATCH] cfq-iosch...
338
  	int hw_tag;
e459dd08f   Corrado Zoccolo   cfq-iosched: fix ...
339
340
341
342
343
344
345
346
  	/*
  	 * 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
347

22e2c507c   Jens Axboe   [PATCH] Update cf...
348
  	/*
22e2c507c   Jens Axboe   [PATCH] Update cf...
349
350
  	 * idle window management
  	 */
911483258   Jan Kara   cfq-iosched: Conv...
351
  	struct hrtimer idle_slice_timer;
23e018a1b   Jens Axboe   block: get rid of...
352
  	struct work_struct unplug_work;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353

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

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
359
360
361
362
  	/*
  	 * tunables, see top of file
  	 */
  	unsigned int cfq_quantum;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
363
364
  	unsigned int cfq_back_penalty;
  	unsigned int cfq_back_max;
22e2c507c   Jens Axboe   [PATCH] Update cf...
365
  	unsigned int cfq_slice_async_rq;
963b72fc6   Jens Axboe   cfq-iosched: rena...
366
  	unsigned int cfq_latency;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
367
368
369
370
371
  	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_...
372

6118b70b3   Jens Axboe   cfq-iosched: get ...
373
374
375
376
  	/*
  	 * Fallback dummy cfqq for extreme OOM conditions
  	 */
  	struct cfq_queue oom_cfqq;
365722bb9   Vivek Goyal   cfq-iosched: dela...
377

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

34b98d03b   Vivek Goyal   cfq-iosched: Rena...
383
  static struct cfq_rb_root *st_for(struct cfq_group *cfqg,
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
384
  					    enum wl_class_t class,
65b32a573   Vivek Goyal   cfq-iosched: Remo...
385
  					    enum wl_type_t type)
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
386
  {
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
387
388
  	if (!cfqg)
  		return NULL;
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
389
  	if (class == IDLE_WORKLOAD)
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
390
  		return &cfqg->service_tree_idle;
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
391

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

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

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

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

155fead9b   Tejun Heo   blkcg: move blkio...
531
  	if (cfqg_stats_idling(stats)) {
629ed0b10   Tejun Heo   blkcg: move stati...
532
533
534
535
536
  		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...
537
  		cfqg_stats_clear_idling(stats);
629ed0b10   Tejun Heo   blkcg: move stati...
538
539
  	}
  }
155fead9b   Tejun Heo   blkcg: move blkio...
540
  static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg)
629ed0b10   Tejun Heo   blkcg: move stati...
541
  {
155fead9b   Tejun Heo   blkcg: move blkio...
542
  	struct cfqg_stats *stats = &cfqg->stats;
629ed0b10   Tejun Heo   blkcg: move stati...
543

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

4ceab71b9   Jens Axboe   cfq-iosched: move...
572
573
574
575
576
577
578
579
  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...
580
  	return cpd ? container_of(cpd, struct cfq_group_data, cpd) : NULL;
4ceab71b9   Jens Axboe   cfq-iosched: move...
581
582
583
584
585
586
  }
  
  static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg)
  {
  	return pd_to_blkg(&cfqg->pd);
  }
ffea73fc7   Tejun Heo   block: blkcg_poli...
587
588
589
590
591
592
  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...
593
594
595
596
  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...
597
  static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg)
7918ffb5b   Tejun Heo   cfq-iosched: impl...
598
  {
d02f7aa8d   Tejun Heo   cfq-iosched: enab...
599
  	struct blkcg_gq *pblkg = cfqg_to_blkg(cfqg)->parent;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
600

d02f7aa8d   Tejun Heo   cfq-iosched: enab...
601
  	return pblkg ? blkg_to_cfqg(pblkg) : NULL;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
602
  }
3984aa552   Jan Kara   cfq-iosched: Allo...
603
604
605
606
607
608
  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...
609
610
611
612
613
614
615
616
617
  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...
618
619
620
621
  #define cfq_log_cfqq(cfqd, cfqq, fmt, args...)	do {			\
  	char __pbuf[128];						\
  									\
  	blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf));	\
b226e5c41   Vivek Goyal   cfq-iosched: Prin...
622
623
624
  	blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \
  			cfq_cfqq_sync((cfqq)) ? 'S' : 'A',		\
  			cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
54e7ed12b   Tejun Heo   blkcg: remove blk...
625
626
627
628
629
630
631
632
633
  			  __pbuf, ##args);				\
  } while (0)
  
  #define cfq_log_cfqg(cfqd, cfqg, fmt, args...)	do {			\
  	char __pbuf[128];						\
  									\
  	blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf));		\
  	blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args);	\
  } while (0)
2868ef7b3   Vivek Goyal   blkio: Some debug...
634

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

155fead9b   Tejun Heo   blkcg: move blkio...
744
  static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
63a4cc248   Mike Christie   blkg_rwstat: sepa...
745
  			struct cfq_group *curr_cfqg, int op, int op_flags) { }
155fead9b   Tejun Heo   blkcg: move blkio...
746
  static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
747
  			uint64_t time, unsigned long unaccounted_time) { }
63a4cc248   Mike Christie   blkg_rwstat: sepa...
748
749
750
751
  static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int op,
  			int op_flags) { }
  static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int op,
  			int op_flags) { }
155fead9b   Tejun Heo   blkcg: move blkio...
752
  static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
63a4cc248   Mike Christie   blkg_rwstat: sepa...
753
754
  			uint64_t start_time, uint64_t io_start_time, int op,
  			int op_flags) { }
2ce4d50f9   Tejun Heo   cfq: collapse cfq...
755

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

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

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

c58698073   Tejun Heo   block, cfq: reorg...
833
834
835
836
837
  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 ...
838
839
840
841
842
843
844
  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...
845
  static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
91fac317a   Vasily Tarasov   cfq-iosched: get ...
846
  {
a6151c3a5   Jens Axboe   cfq-iosched: appl...
847
  	return cic->cfqq[is_sync];
91fac317a   Vasily Tarasov   cfq-iosched: get ...
848
  }
c58698073   Tejun Heo   block, cfq: reorg...
849
850
  static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
  				bool is_sync)
91fac317a   Vasily Tarasov   cfq-iosched: get ...
851
  {
a6151c3a5   Jens Axboe   cfq-iosched: appl...
852
  	cic->cfqq[is_sync] = cfqq;
91fac317a   Vasily Tarasov   cfq-iosched: get ...
853
  }
c58698073   Tejun Heo   block, cfq: reorg...
854
  static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
bca4b914b   Konstantin Khlebnikov   cfq-iosched: remo...
855
  {
c58698073   Tejun Heo   block, cfq: reorg...
856
  	return cic->icq.q->elevator->elevator_data;
bca4b914b   Konstantin Khlebnikov   cfq-iosched: remo...
857
  }
91fac317a   Vasily Tarasov   cfq-iosched: get ...
858
859
860
861
  /*
   * We regard a request as SYNC, if it's either a read or has the SYNC bit
   * set (in which case it could also be direct WRITE).
   */
a6151c3a5   Jens Axboe   cfq-iosched: appl...
862
  static inline bool cfq_bio_sync(struct bio *bio)
91fac317a   Vasily Tarasov   cfq-iosched: get ...
863
  {
1eff9d322   Jens Axboe   block: rename bio...
864
  	return bio_data_dir(bio) == READ || (bio->bi_opf & REQ_SYNC);
91fac317a   Vasily Tarasov   cfq-iosched: get ...
865
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
866

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
867
  /*
99f95e528   Andrew Morton   [PATCH] cfq build...
868
869
870
   * 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...
871
  static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
99f95e528   Andrew Morton   [PATCH] cfq build...
872
  {
7b679138b   Jens Axboe   cfq-iosched: add ...
873
874
  	if (cfqd->busy_queues) {
  		cfq_log(cfqd, "schedule dispatch");
59c3d45e4   Jens Axboe   block: remove 'q'...
875
  		kblockd_schedule_work(&cfqd->unplug_work);
7b679138b   Jens Axboe   cfq-iosched: add ...
876
  	}
99f95e528   Andrew Morton   [PATCH] cfq build...
877
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
878
  /*
44f7c1606   Jens Axboe   cfq-iosched: defe...
879
880
881
882
   * 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...
883
  static inline u64 cfq_prio_slice(struct cfq_data *cfqd, bool sync,
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
884
  				 unsigned short prio)
44f7c1606   Jens Axboe   cfq-iosched: defe...
885
  {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
886
887
  	u64 base_slice = cfqd->cfq_slice[sync];
  	u64 slice = div_u64(base_slice, CFQ_SLICE_SCALE);
44f7c1606   Jens Axboe   cfq-iosched: defe...
888

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

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

1d3650f71   Tejun Heo   cfq-iosched: impl...
915
916
  	/* charge / vfraction */
  	c <<= CFQ_SERVICE_SHIFT;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
917
  	return div_u64(c, vfraction);
25bc6b077   Vivek Goyal   blkio: Introduce ...
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
  }
  
  static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
  {
  	s64 delta = (s64)(vdisktime - min_vdisktime);
  	if (delta > 0)
  		min_vdisktime = vdisktime;
  
  	return min_vdisktime;
  }
  
  static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
  {
  	s64 delta = (s64)(vdisktime - min_vdisktime);
  	if (delta < 0)
  		min_vdisktime = vdisktime;
  
  	return min_vdisktime;
  }
  
  static void update_min_vdisktime(struct cfq_rb_root *st)
  {
25bc6b077   Vivek Goyal   blkio: Introduce ...
940
  	struct cfq_group *cfqg;
25bc6b077   Vivek Goyal   blkio: Introduce ...
941
942
  	if (st->left) {
  		cfqg = rb_entry_cfqg(st->left);
a60327107   Gui Jianfeng   cfq-iosched: Fix ...
943
944
  		st->min_vdisktime = max_vdisktime(st->min_vdisktime,
  						  cfqg->vdisktime);
25bc6b077   Vivek Goyal   blkio: Introduce ...
945
  	}
25bc6b077   Vivek Goyal   blkio: Introduce ...
946
  }
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
947
948
949
950
951
  /*
   * 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 ...
952
953
  static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
  					struct cfq_group *cfqg, bool rt)
5869619cb   Jens Axboe   cfq-iosched: fix ...
954
  {
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
955
956
957
  	unsigned min_q, max_q;
  	unsigned mult  = cfq_hist_divisor - 1;
  	unsigned round = cfq_hist_divisor / 2;
58ff82f34   Vivek Goyal   blkio: Implement ...
958
  	unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
5db5d6427   Corrado Zoccolo   cfq-iosched: adap...
959

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

9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1008
1009
  	cfqq->slice_start = now;
  	cfqq->slice_end = now + slice;
f75edf2dc   Vivek Goyal   blkio: Wait for c...
1010
  	cfqq->allocated_slice = slice;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1011
  	cfq_log_cfqq(cfqd, cfqq, "set_slice=%llu", cfqq->slice_end - now);
44f7c1606   Jens Axboe   cfq-iosched: defe...
1012
1013
1014
1015
1016
1017
1018
  }
  
  /*
   * 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...
1019
  static inline bool cfq_slice_used(struct cfq_queue *cfqq)
44f7c1606   Jens Axboe   cfq-iosched: defe...
1020
1021
  {
  	if (cfq_cfqq_slice_new(cfqq))
c1e44756f   Shaohua Li   cfq-iosched: do c...
1022
  		return false;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1023
  	if (ktime_get_ns() < cfqq->slice_end)
c1e44756f   Shaohua Li   cfq-iosched: do c...
1024
  		return false;
44f7c1606   Jens Axboe   cfq-iosched: defe...
1025

c1e44756f   Shaohua Li   cfq-iosched: do c...
1026
  	return true;
44f7c1606   Jens Axboe   cfq-iosched: defe...
1027
1028
1029
  }
  
  /*
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1030
   * Lifted from AS - choose which of rq1 and rq2 that is best served now.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1031
   * We choose the request that is closest to the head right now. Distance
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1032
   * behind the head is penalized and only allowed to a certain extent.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1033
   */
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1034
  static struct request *
cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
1035
  cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1036
  {
cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
1037
  	sector_t s1, s2, d1 = 0, d2 = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1038
  	unsigned long back_max;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1039
1040
1041
  #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
1042

5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1043
1044
1045
1046
  	if (rq1 == NULL || rq1 == rq2)
  		return rq2;
  	if (rq2 == NULL)
  		return rq1;
9c2c38a12   Jens Axboe   [PATCH] cfq-iosch...
1047

229836bd6   Namhyung Kim   cfq-iosched: redu...
1048
1049
  	if (rq_is_sync(rq1) != rq_is_sync(rq2))
  		return rq_is_sync(rq1) ? rq1 : rq2;
65299a3b7   Christoph Hellwig   block: separate p...
1050
1051
  	if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
  		return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
b53d1ed73   Jens Axboe   Revert "cfq: Remo...
1052

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
  	/*
  	 * 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...
1071
  		wrap |= CFQ_RQ1_WRAP;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1072
1073
1074
1075
1076
1077
  
  	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...
1078
  		wrap |= CFQ_RQ2_WRAP;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1079
1080
  
  	/* Found required data */
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1081
1082
1083
1084
1085
1086
  
  	/*
  	 * 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...
1087
  	case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1088
  		if (d1 < d2)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1089
  			return rq1;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1090
  		else if (d2 < d1)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1091
  			return rq2;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1092
1093
  		else {
  			if (s1 >= s2)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1094
  				return rq1;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1095
  			else
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1096
  				return rq2;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1097
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1098

e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1099
  	case CFQ_RQ2_WRAP:
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1100
  		return rq1;
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1101
  	case CFQ_RQ1_WRAP:
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1102
1103
  		return rq2;
  	case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
e8a99053e   Andreas Mohr   [PATCH] cfq-iosch...
1104
1105
1106
1107
1108
1109
1110
1111
  	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...
1112
  			return rq1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1113
  		else
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1114
  			return rq2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1115
1116
  	}
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
1117
1118
1119
  /*
   * The below is leftmost cache rbtree addon
   */
0871714e0   Jens Axboe   cfq-iosched: rela...
1120
  static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
1121
  {
615f0259e   Vivek Goyal   blkio: Implement ...
1122
1123
1124
  	/* Service tree is empty */
  	if (!root->count)
  		return NULL;
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
1125
1126
  	if (!root->left)
  		root->left = rb_first(&root->rb);
0871714e0   Jens Axboe   cfq-iosched: rela...
1127
1128
1129
1130
  	if (root->left)
  		return rb_entry(root->left, struct cfq_queue, rb_node);
  
  	return NULL;
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
1131
  }
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
  static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
  {
  	if (!root->left)
  		root->left = rb_first(&root->rb);
  
  	if (root->left)
  		return rb_entry_cfqg(root->left);
  
  	return NULL;
  }
a36e71f99   Jens Axboe   cfq-iosched: add ...
1142
1143
1144
1145
1146
  static void rb_erase_init(struct rb_node *n, struct rb_root *root)
  {
  	rb_erase(n, root);
  	RB_CLEAR_NODE(n);
  }
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
1147
1148
1149
1150
  static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
  {
  	if (root->left == n)
  		root->left = NULL;
a36e71f99   Jens Axboe   cfq-iosched: add ...
1151
  	rb_erase_init(n, &root->rb);
aa6f6a3de   Corrado Zoccolo   cfq-iosched: prep...
1152
  	--root->count;
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
1153
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1154
1155
1156
  /*
   * would be nice to take fifo expire time into account as well
   */
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1157
1158
1159
  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
1160
  {
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
1161
1162
  	struct rb_node *rbnext = rb_next(&last->rb_node);
  	struct rb_node *rbprev = rb_prev(&last->rb_node);
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1163
  	struct request *next = NULL, *prev = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1164

21183b07e   Jens Axboe   [PATCH] cfq-iosch...
1165
  	BUG_ON(RB_EMPTY_NODE(&last->rb_node));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1166
1167
  
  	if (rbprev)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1168
  		prev = rb_entry_rq(rbprev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1169

21183b07e   Jens Axboe   [PATCH] cfq-iosch...
1170
  	if (rbnext)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1171
  		next = rb_entry_rq(rbnext);
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
1172
1173
1174
  	else {
  		rbnext = rb_first(&cfqq->sort_list);
  		if (rbnext && rbnext != &last->rb_node)
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
1175
  			next = rb_entry_rq(rbnext);
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
1176
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1177

cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
1178
  	return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1179
  }
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1180
1181
  static u64 cfq_slice_offset(struct cfq_data *cfqd,
  			    struct cfq_queue *cfqq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1182
  {
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
1183
1184
1185
  	/*
  	 * just an approximation, should be ok.
  	 */
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
1186
  	return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
464191c65   Jens Axboe   Revert "cfq: Make...
1187
  		       cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
1188
  }
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
  static inline s64
  cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
  {
  	return cfqg->vdisktime - st->min_vdisktime;
  }
  
  static void
  __cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
  {
  	struct rb_node **node = &st->rb.rb_node;
  	struct rb_node *parent = NULL;
  	struct cfq_group *__cfqg;
  	s64 key = cfqg_key(st, cfqg);
  	int left = 1;
  
  	while (*node != NULL) {
  		parent = *node;
  		__cfqg = rb_entry_cfqg(parent);
  
  		if (key < cfqg_key(st, __cfqg))
  			node = &parent->rb_left;
  		else {
  			node = &parent->rb_right;
  			left = 0;
  		}
  	}
  
  	if (left)
  		st->left = &cfqg->rb_node;
  
  	rb_link_node(&cfqg->rb_node, parent, node);
  	rb_insert_color(&cfqg->rb_node, &st->rb);
  }
7b5af5cff   Toshiaki Makita   cfq-iosched: Add ...
1222
1223
1224
  /*
   * This has to be called only on activation of cfqg
   */
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1225
  static void
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1226
1227
  cfq_update_group_weight(struct cfq_group *cfqg)
  {
3381cb8d2   Tejun Heo   blkcg: move blkio...
1228
  	if (cfqg->new_weight) {
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1229
  		cfqg->weight = cfqg->new_weight;
3381cb8d2   Tejun Heo   blkcg: move blkio...
1230
  		cfqg->new_weight = 0;
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1231
  	}
e15693ef1   Toshiaki Makita   cfq-iosched: Fix ...
1232
1233
1234
1235
1236
1237
  }
  
  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 ...
1238
1239
1240
1241
1242
  
  	if (cfqg->new_leaf_weight) {
  		cfqg->leaf_weight = cfqg->new_leaf_weight;
  		cfqg->new_leaf_weight = 0;
  	}
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1243
1244
1245
1246
1247
  }
  
  static void
  cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
  {
1d3650f71   Tejun Heo   cfq-iosched: impl...
1248
  	unsigned int vfr = 1 << CFQ_SERVICE_SHIFT;	/* start with 1 */
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1249
  	struct cfq_group *pos = cfqg;
1d3650f71   Tejun Heo   cfq-iosched: impl...
1250
  	struct cfq_group *parent;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1251
1252
1253
  	bool propagate;
  
  	/* add to the service tree */
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1254
  	BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
7b5af5cff   Toshiaki Makita   cfq-iosched: Add ...
1255
1256
1257
1258
1259
  	/*
  	 * 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 ...
1260
  	cfq_update_group_leaf_weight(cfqg);
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1261
  	__cfq_group_service_tree_add(st, cfqg);
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1262
1263
  
  	/*
1d3650f71   Tejun Heo   cfq-iosched: impl...
1264
1265
1266
1267
1268
1269
1270
  	 * 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...
1271
1272
1273
  	 */
  	propagate = !pos->nr_active++;
  	pos->children_weight += pos->leaf_weight;
1d3650f71   Tejun Heo   cfq-iosched: impl...
1274
  	vfr = vfr * pos->leaf_weight / pos->children_weight;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1275

1d3650f71   Tejun Heo   cfq-iosched: impl...
1276
1277
1278
1279
1280
1281
  	/*
  	 * 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...
1282
  	while ((parent = cfqg_parent(pos))) {
1d3650f71   Tejun Heo   cfq-iosched: impl...
1283
  		if (propagate) {
e15693ef1   Toshiaki Makita   cfq-iosched: Fix ...
1284
  			cfq_update_group_weight(pos);
1d3650f71   Tejun Heo   cfq-iosched: impl...
1285
1286
1287
1288
  			propagate = !parent->nr_active++;
  			parent->children_weight += pos->weight;
  		}
  		vfr = vfr * pos->weight / parent->children_weight;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1289
1290
  		pos = parent;
  	}
1d3650f71   Tejun Heo   cfq-iosched: impl...
1291
1292
  
  	cfqg->vfraction = max_t(unsigned, vfr, 1);
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1293
1294
1295
1296
  }
  
  static void
  cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1297
1298
1299
1300
1301
1302
  {
  	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 ...
1303
  	if (!RB_EMPTY_NODE(&cfqg->rb_node))
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1304
1305
1306
1307
1308
  		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...
1309
  	 * if group does not loose all if it was not continuously backlogged.
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1310
1311
1312
1313
1314
1315
1316
  	 */
  	n = rb_last(&st->rb);
  	if (n) {
  		__cfqg = rb_entry_cfqg(n);
  		cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
  	} else
  		cfqg->vdisktime = st->min_vdisktime;
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1317
1318
  	cfq_group_service_tree_add(st, cfqg);
  }
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1319

8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1320
1321
1322
  static void
  cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
  {
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
  	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...
1334
  		struct cfq_group *parent = cfqg_parent(pos);
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1335
1336
1337
  
  		/* @pos has 0 nr_active at this point */
  		WARN_ON_ONCE(pos->children_weight);
1d3650f71   Tejun Heo   cfq-iosched: impl...
1338
  		pos->vfraction = 0;
7918ffb5b   Tejun Heo   cfq-iosched: impl...
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
  
  		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'...
1349
1350
  	if (!RB_EMPTY_NODE(&cfqg->rb_node))
  		cfq_rb_erase(&cfqg->rb_node, st);
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1351
1352
1353
  }
  
  static void
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1354
  cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1355
1356
1357
1358
1359
  {
  	struct cfq_rb_root *st = &cfqd->grp_service_tree;
  
  	BUG_ON(cfqg->nr_cfqq < 1);
  	cfqg->nr_cfqq--;
25bc6b077   Vivek Goyal   blkio: Introduce ...
1360

1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1361
1362
1363
  	/* If there are other cfq queues under this group, don't delete it */
  	if (cfqg->nr_cfqq)
  		return;
2868ef7b3   Vivek Goyal   blkio: Some debug...
1364
  	cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1365
  	cfq_group_service_tree_del(st, cfqg);
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
1366
  	cfqg->saved_wl_slice = 0;
155fead9b   Tejun Heo   blkcg: move blkio...
1367
  	cfqg_stats_update_dequeue(cfqg);
dae739ebc   Vivek Goyal   blkio: Group time...
1368
  }
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1369
1370
  static inline u64 cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
  				       u64 *unaccounted_time)
dae739ebc   Vivek Goyal   blkio: Group time...
1371
  {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1372
1373
  	u64 slice_used;
  	u64 now = ktime_get_ns();
dae739ebc   Vivek Goyal   blkio: Group time...
1374
1375
1376
1377
1378
  
  	/*
  	 * Queue got expired before even a single request completed or
  	 * got expired immediately after first request completion.
  	 */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1379
  	if (!cfqq->slice_start || cfqq->slice_start == now) {
dae739ebc   Vivek Goyal   blkio: Group time...
1380
1381
1382
1383
1384
1385
  		/*
  		 * 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...
1386
1387
  		slice_used = max_t(u64, (now - cfqq->dispatch_start),
  					jiffies_to_nsecs(1));
dae739ebc   Vivek Goyal   blkio: Group time...
1388
  	} else {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1389
  		slice_used = now - cfqq->slice_start;
167400d34   Justin TerAvest   blk-cgroup: Add u...
1390
1391
  		if (slice_used > cfqq->allocated_slice) {
  			*unaccounted_time = slice_used - cfqq->allocated_slice;
f75edf2dc   Vivek Goyal   blkio: Wait for c...
1392
  			slice_used = cfqq->allocated_slice;
167400d34   Justin TerAvest   blk-cgroup: Add u...
1393
  		}
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1394
  		if (cfqq->slice_start > cfqq->dispatch_start)
167400d34   Justin TerAvest   blk-cgroup: Add u...
1395
1396
  			*unaccounted_time += cfqq->slice_start -
  					cfqq->dispatch_start;
dae739ebc   Vivek Goyal   blkio: Group time...
1397
  	}
dae739ebc   Vivek Goyal   blkio: Group time...
1398
1399
1400
1401
  	return slice_used;
  }
  
  static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
1402
  				struct cfq_queue *cfqq)
dae739ebc   Vivek Goyal   blkio: Group time...
1403
1404
  {
  	struct cfq_rb_root *st = &cfqd->grp_service_tree;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1405
  	u64 used_sl, charge, unaccounted_sl = 0;
f26bd1f0a   Vivek Goyal   blkio: Determine ...
1406
1407
  	int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
  			- cfqg->service_tree_idle.count;
1d3650f71   Tejun Heo   cfq-iosched: impl...
1408
  	unsigned int vfr;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1409
  	u64 now = ktime_get_ns();
f26bd1f0a   Vivek Goyal   blkio: Determine ...
1410
1411
  
  	BUG_ON(nr_sync < 0);
167400d34   Justin TerAvest   blk-cgroup: Add u...
1412
  	used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
dae739ebc   Vivek Goyal   blkio: Group time...
1413

02b35081f   Vivek Goyal   cfq-iosched: Do g...
1414
1415
1416
1417
  	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...
1418

1d3650f71   Tejun Heo   cfq-iosched: impl...
1419
1420
1421
1422
1423
1424
1425
  	/*
  	 * 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'...
1426
  	cfq_group_service_tree_del(st, cfqg);
1d3650f71   Tejun Heo   cfq-iosched: impl...
1427
  	cfqg->vdisktime += cfqg_scale_charge(charge, vfr);
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
1428
  	cfq_group_service_tree_add(st, cfqg);
dae739ebc   Vivek Goyal   blkio: Group time...
1429
1430
  
  	/* This group is being expired. Save the context */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1431
1432
  	if (cfqd->workload_expires > now) {
  		cfqg->saved_wl_slice = cfqd->workload_expires - now;
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
1433
1434
  		cfqg->saved_wl_type = cfqd->serving_wl_type;
  		cfqg->saved_wl_class = cfqd->serving_wl_class;
dae739ebc   Vivek Goyal   blkio: Group time...
1435
  	} else
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
1436
  		cfqg->saved_wl_slice = 0;
2868ef7b3   Vivek Goyal   blkio: Some debug...
1437
1438
1439
  
  	cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
  					st->min_vdisktime);
fd16d2631   Joe Perches   block: Add __attr...
1440
  	cfq_log_cfqq(cfqq->cfqd, cfqq,
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
1441
  		     "sl_used=%llu disp=%llu charge=%llu iops=%u sect=%lu",
fd16d2631   Joe Perches   block: Add __attr...
1442
1443
  		     used_sl, cfqq->slice_dispatch, charge,
  		     iops_mode(cfqd), cfqq->nr_sectors);
155fead9b   Tejun Heo   blkcg: move blkio...
1444
1445
  	cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
  	cfqg_stats_set_start_empty_time(cfqg);
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
1446
  }
f51b802c1   Tejun Heo   blkcg: use the us...
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
  /**
   * 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...
1462
  	cfqg->ttime.last_end_request = ktime_get_ns();
f51b802c1   Tejun Heo   blkcg: use the us...
1463
  }
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1464
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1465
1466
  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...
1467
  static void cfqg_stats_exit(struct cfqg_stats *stats)
90d3839b9   Peter Zijlstra   block: Use u64_st...
1468
  {
24bdb8ef0   Tejun Heo   blkcg: make blkcg...
1469
1470
1471
1472
  	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...
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
  	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...
1487
  	if (blkg_rwstat_init(&stats->merged, gfp) ||
24bdb8ef0   Tejun Heo   blkcg: make blkcg...
1488
1489
1490
  	    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...
1491
1492
  	    blkg_stat_init(&stats->time, gfp))
  		goto err;
90d3839b9   Peter Zijlstra   block: Use u64_st...
1493
1494
  
  #ifdef CONFIG_DEBUG_BLK_CGROUP
24bdb8ef0   Tejun Heo   blkcg: make blkcg...
1495
1496
1497
1498
1499
1500
1501
1502
  	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...
1503
  #endif
24bdb8ef0   Tejun Heo   blkcg: make blkcg...
1504
1505
1506
1507
  	return 0;
  err:
  	cfqg_stats_exit(stats);
  	return -ENOMEM;
90d3839b9   Peter Zijlstra   block: Use u64_st...
1508
  }
e4a9bde95   Tejun Heo   blkcg: replace bl...
1509
1510
1511
1512
1513
1514
1515
1516
1517
  static struct blkcg_policy_data *cfq_cpd_alloc(gfp_t gfp)
  {
  	struct cfq_group_data *cgd;
  
  	cgd = kzalloc(sizeof(*cgd), GFP_KERNEL);
  	if (!cgd)
  		return NULL;
  	return &cgd->cpd;
  }
814376483   Tejun Heo   blkcg: minor upda...
1518
  static void cfq_cpd_init(struct blkcg_policy_data *cpd)
e48453c38   Arianna Avanzini   block, cgroup: im...
1519
  {
814376483   Tejun Heo   blkcg: minor upda...
1520
  	struct cfq_group_data *cgd = cpd_to_cfqgd(cpd);
9e10a130d   Tejun Heo   cgroup: replace c...
1521
  	unsigned int weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ?
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1522
  			      CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
e48453c38   Arianna Avanzini   block, cgroup: im...
1523

69d7fde59   Tejun Heo   blkcg: use CGROUP...
1524
1525
1526
1527
1528
  	if (cpd_to_blkcg(cpd) == &blkcg_root)
  		weight *= 2;
  
  	cgd->weight = weight;
  	cgd->leaf_weight = weight;
e48453c38   Arianna Avanzini   block, cgroup: im...
1529
  }
e4a9bde95   Tejun Heo   blkcg: replace bl...
1530
1531
1532
1533
  static void cfq_cpd_free(struct blkcg_policy_data *cpd)
  {
  	kfree(cpd_to_cfqgd(cpd));
  }
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1534
1535
1536
  static void cfq_cpd_bind(struct blkcg_policy_data *cpd)
  {
  	struct blkcg *blkcg = cpd_to_blkcg(cpd);
9e10a130d   Tejun Heo   cgroup: replace c...
1537
  	bool on_dfl = cgroup_subsys_on_dfl(io_cgrp_subsys);
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1538
1539
1540
1541
1542
1543
1544
1545
  	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...
1546
1547
  static struct blkg_policy_data *cfq_pd_alloc(gfp_t gfp, int node)
  {
b2ce2643c   Tejun Heo   blk-throttle: cle...
1548
1549
1550
1551
1552
1553
1554
  	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...
1555
1556
1557
1558
  	if (cfqg_stats_init(&cfqg->stats, gfp)) {
  		kfree(cfqg);
  		return NULL;
  	}
b2ce2643c   Tejun Heo   blk-throttle: cle...
1559
1560
  
  	return &cfqg->pd;
001bea73e   Tejun Heo   blkcg: replace bl...
1561
  }
a9520cd6f   Tejun Heo   blkcg: make blkcg...
1562
  static void cfq_pd_init(struct blkg_policy_data *pd)
f469a7b4d   Vivek Goyal   blk-cgroup: Allow...
1563
  {
a9520cd6f   Tejun Heo   blkcg: make blkcg...
1564
1565
  	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...
1566

e48453c38   Arianna Avanzini   block, cgroup: im...
1567
1568
  	cfqg->weight = cgd->weight;
  	cfqg->leaf_weight = cgd->leaf_weight;
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1569
  }
a9520cd6f   Tejun Heo   blkcg: make blkcg...
1570
  static void cfq_pd_offline(struct blkg_policy_data *pd)
0b39920b5   Tejun Heo   cfq-iosched: coll...
1571
  {
a9520cd6f   Tejun Heo   blkcg: make blkcg...
1572
  	struct cfq_group *cfqg = pd_to_cfqg(pd);
60a837077   Tejun Heo   cfq-iosched: char...
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
  	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...
1584
1585
1586
1587
1588
1589
  	/*
  	 * @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...
1590
  	cfqg_stats_xfer_dead(cfqg);
0b39920b5   Tejun Heo   cfq-iosched: coll...
1591
  }
001bea73e   Tejun Heo   blkcg: replace bl...
1592
1593
  static void cfq_pd_free(struct blkg_policy_data *pd)
  {
24bdb8ef0   Tejun Heo   blkcg: make blkcg...
1594
1595
1596
1597
  	struct cfq_group *cfqg = pd_to_cfqg(pd);
  
  	cfqg_stats_exit(&cfqg->stats);
  	return kfree(cfqg);
001bea73e   Tejun Heo   blkcg: replace bl...
1598
  }
a9520cd6f   Tejun Heo   blkcg: make blkcg...
1599
  static void cfq_pd_reset_stats(struct blkg_policy_data *pd)
689665af4   Tejun Heo   cfq-iosched: sepa...
1600
  {
a9520cd6f   Tejun Heo   blkcg: make blkcg...
1601
  	struct cfq_group *cfqg = pd_to_cfqg(pd);
689665af4   Tejun Heo   cfq-iosched: sepa...
1602
1603
  
  	cfqg_stats_reset(&cfqg->stats);
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1604
  }
ae1188963   Tejun Heo   blkcg: consolidat...
1605
1606
  static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
  					 struct blkcg *blkcg)
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1607
  {
ae1188963   Tejun Heo   blkcg: consolidat...
1608
  	struct blkcg_gq *blkg;
f469a7b4d   Vivek Goyal   blk-cgroup: Allow...
1609

ae1188963   Tejun Heo   blkcg: consolidat...
1610
1611
1612
1613
  	blkg = blkg_lookup(blkcg, cfqd->queue);
  	if (likely(blkg))
  		return blkg_to_cfqg(blkg);
  	return NULL;
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1614
1615
1616
1617
  }
  
  static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
  {
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
1618
  	cfqq->cfqg = cfqg;
b1c357696   Vivek Goyal   blkio: Take care ...
1619
  	/* cfqq reference on cfqg */
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
1620
  	cfqg_get(cfqg);
b1c357696   Vivek Goyal   blkio: Take care ...
1621
  }
f95a04afa   Tejun Heo   blkcg: embed stru...
1622
1623
  static u64 cfqg_prfill_weight_device(struct seq_file *sf,
  				     struct blkg_policy_data *pd, int off)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1624
  {
f95a04afa   Tejun Heo   blkcg: embed stru...
1625
  	struct cfq_group *cfqg = pd_to_cfqg(pd);
3381cb8d2   Tejun Heo   blkcg: move blkio...
1626
1627
  
  	if (!cfqg->dev_weight)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1628
  		return 0;
f95a04afa   Tejun Heo   blkcg: embed stru...
1629
  	return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1630
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1631
  static int cfqg_print_weight_device(struct seq_file *sf, void *v)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1632
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1633
1634
1635
  	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/...
1636
1637
  	return 0;
  }
e71357e11   Tejun Heo   cfq-iosched: add ...
1638
1639
1640
1641
1642
1643
1644
1645
1646
  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...
1647
  static int cfqg_print_leaf_weight_device(struct seq_file *sf, void *v)
e71357e11   Tejun Heo   cfq-iosched: add ...
1648
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1649
1650
1651
  	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 ...
1652
1653
  	return 0;
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1654
  static int cfq_print_weight(struct seq_file *sf, void *v)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1655
  {
e48453c38   Arianna Avanzini   block, cgroup: im...
1656
  	struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
9470e4a69   Jens Axboe   cfq-iosched: fix ...
1657
1658
  	struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
  	unsigned int val = 0;
e48453c38   Arianna Avanzini   block, cgroup: im...
1659

9470e4a69   Jens Axboe   cfq-iosched: fix ...
1660
1661
1662
1663
1664
  	if (cgd)
  		val = cgd->weight;
  
  	seq_printf(sf, "%u
  ", val);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1665
1666
  	return 0;
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1667
  static int cfq_print_leaf_weight(struct seq_file *sf, void *v)
e71357e11   Tejun Heo   cfq-iosched: add ...
1668
  {
e48453c38   Arianna Avanzini   block, cgroup: im...
1669
  	struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
9470e4a69   Jens Axboe   cfq-iosched: fix ...
1670
1671
1672
1673
1674
  	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...
1675

9470e4a69   Jens Axboe   cfq-iosched: fix ...
1676
1677
  	seq_printf(sf, "%u
  ", val);
e71357e11   Tejun Heo   cfq-iosched: add ...
1678
1679
  	return 0;
  }
451af504d   Tejun Heo   cgroup: replace c...
1680
1681
  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 ...
1682
  					bool on_dfl, bool is_leaf_weight)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1683
  {
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1684
1685
  	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...
1686
  	struct blkcg *blkcg = css_to_blkcg(of_css(of));
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1687
  	struct blkg_conf_ctx ctx;
3381cb8d2   Tejun Heo   blkcg: move blkio...
1688
  	struct cfq_group *cfqg;
e48453c38   Arianna Avanzini   block, cgroup: im...
1689
  	struct cfq_group_data *cfqgd;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1690
  	int ret;
36aa9e5f5   Tejun Heo   blkcg: move body ...
1691
  	u64 v;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1692

3c798398e   Tejun Heo   blkcg: mass renam...
1693
  	ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1694
1695
  	if (ret)
  		return ret;
2ee867dcf   Tejun Heo   blkcg: implement ...
1696
1697
1698
1699
1700
1701
1702
1703
1704
  	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 ...
1705
  		goto out_finish;
2ee867dcf   Tejun Heo   blkcg: implement ...
1706
  	}
36aa9e5f5   Tejun Heo   blkcg: move body ...
1707

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

20386ce01   Tejun Heo   blkcg: refine err...
1711
  	ret = -ERANGE;
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1712
  	if (!v || (v >= min && v <= max)) {
e71357e11   Tejun Heo   cfq-iosched: add ...
1713
  		if (!is_leaf_weight) {
36aa9e5f5   Tejun Heo   blkcg: move body ...
1714
1715
  			cfqg->dev_weight = v;
  			cfqg->new_weight = v ?: cfqgd->weight;
e71357e11   Tejun Heo   cfq-iosched: add ...
1716
  		} else {
36aa9e5f5   Tejun Heo   blkcg: move body ...
1717
1718
  			cfqg->dev_leaf_weight = v;
  			cfqg->new_leaf_weight = v ?: cfqgd->leaf_weight;
e71357e11   Tejun Heo   cfq-iosched: add ...
1719
  		}
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1720
1721
  		ret = 0;
  	}
36aa9e5f5   Tejun Heo   blkcg: move body ...
1722
  out_finish:
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1723
  	blkg_conf_finish(&ctx);
451af504d   Tejun Heo   cgroup: replace c...
1724
  	return ret ?: nbytes;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1725
  }
451af504d   Tejun Heo   cgroup: replace c...
1726
1727
  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 ...
1728
  {
2ee867dcf   Tejun Heo   blkcg: implement ...
1729
  	return __cfqg_set_weight_device(of, buf, nbytes, off, false, false);
e71357e11   Tejun Heo   cfq-iosched: add ...
1730
  }
451af504d   Tejun Heo   cgroup: replace c...
1731
1732
  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 ...
1733
  {
2ee867dcf   Tejun Heo   blkcg: implement ...
1734
  	return __cfqg_set_weight_device(of, buf, nbytes, off, false, true);
e71357e11   Tejun Heo   cfq-iosched: add ...
1735
  }
dd165eb3b   Tejun Heo   blkcg: misc prepa...
1736
  static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1737
  			    bool on_dfl, bool reset_dev, bool is_leaf_weight)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1738
  {
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1739
1740
  	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...
1741
  	struct blkcg *blkcg = css_to_blkcg(css);
3c798398e   Tejun Heo   blkcg: mass renam...
1742
  	struct blkcg_gq *blkg;
e48453c38   Arianna Avanzini   block, cgroup: im...
1743
  	struct cfq_group_data *cfqgd;
ae994ea97   Jens Axboe   cfq-iosched: fix ...
1744
  	int ret = 0;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1745

69d7fde59   Tejun Heo   blkcg: use CGROUP...
1746
1747
  	if (val < min || val > max)
  		return -ERANGE;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1748
1749
  
  	spin_lock_irq(&blkcg->lock);
e48453c38   Arianna Avanzini   block, cgroup: im...
1750
  	cfqgd = blkcg_to_cfqgd(blkcg);
ae994ea97   Jens Axboe   cfq-iosched: fix ...
1751
1752
1753
1754
  	if (!cfqgd) {
  		ret = -EINVAL;
  		goto out;
  	}
e71357e11   Tejun Heo   cfq-iosched: add ...
1755
1756
  
  	if (!is_leaf_weight)
e48453c38   Arianna Avanzini   block, cgroup: im...
1757
  		cfqgd->weight = val;
e71357e11   Tejun Heo   cfq-iosched: add ...
1758
  	else
e48453c38   Arianna Avanzini   block, cgroup: im...
1759
  		cfqgd->leaf_weight = val;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1760

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

e71357e11   Tejun Heo   cfq-iosched: add ...
1764
1765
1766
1767
  		if (!cfqg)
  			continue;
  
  		if (!is_leaf_weight) {
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1768
1769
  			if (reset_dev)
  				cfqg->dev_weight = 0;
e71357e11   Tejun Heo   cfq-iosched: add ...
1770
  			if (!cfqg->dev_weight)
e48453c38   Arianna Avanzini   block, cgroup: im...
1771
  				cfqg->new_weight = cfqgd->weight;
e71357e11   Tejun Heo   cfq-iosched: add ...
1772
  		} else {
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1773
1774
  			if (reset_dev)
  				cfqg->dev_leaf_weight = 0;
e71357e11   Tejun Heo   cfq-iosched: add ...
1775
  			if (!cfqg->dev_leaf_weight)
e48453c38   Arianna Avanzini   block, cgroup: im...
1776
  				cfqg->new_leaf_weight = cfqgd->leaf_weight;
e71357e11   Tejun Heo   cfq-iosched: add ...
1777
  		}
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1778
  	}
ae994ea97   Jens Axboe   cfq-iosched: fix ...
1779
  out:
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1780
  	spin_unlock_irq(&blkcg->lock);
ae994ea97   Jens Axboe   cfq-iosched: fix ...
1781
  	return ret;
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1782
  }
182446d08   Tejun Heo   cgroup: pass arou...
1783
1784
  static int cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft,
  			  u64 val)
e71357e11   Tejun Heo   cfq-iosched: add ...
1785
  {
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1786
  	return __cfq_set_weight(css, val, false, false, false);
e71357e11   Tejun Heo   cfq-iosched: add ...
1787
  }
182446d08   Tejun Heo   cgroup: pass arou...
1788
1789
  static int cfq_set_leaf_weight(struct cgroup_subsys_state *css,
  			       struct cftype *cft, u64 val)
e71357e11   Tejun Heo   cfq-iosched: add ...
1790
  {
69d7fde59   Tejun Heo   blkcg: use CGROUP...
1791
  	return __cfq_set_weight(css, val, false, false, true);
e71357e11   Tejun Heo   cfq-iosched: add ...
1792
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1793
  static int cfqg_print_stat(struct seq_file *sf, void *v)
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1794
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1795
1796
  	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...
1797
1798
  	return 0;
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1799
  static int cfqg_print_rwstat(struct seq_file *sf, void *v)
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1800
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1801
1802
  	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...
1803
1804
  	return 0;
  }
43114018c   Tejun Heo   cfq-iosched: add ...
1805
1806
1807
  static u64 cfqg_prfill_stat_recursive(struct seq_file *sf,
  				      struct blkg_policy_data *pd, int off)
  {
f12c74cab   Tejun Heo   blkcg: make blkg_...
1808
1809
  	u64 sum = blkg_stat_recursive_sum(pd_to_blkg(pd),
  					  &blkcg_policy_cfq, off);
43114018c   Tejun Heo   cfq-iosched: add ...
1810
1811
1812
1813
1814
1815
  	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_...
1816
1817
  	struct blkg_rwstat sum = blkg_rwstat_recursive_sum(pd_to_blkg(pd),
  							&blkcg_policy_cfq, off);
43114018c   Tejun Heo   cfq-iosched: add ...
1818
1819
  	return __blkg_prfill_rwstat(sf, pd, &sum);
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1820
  static int cfqg_print_stat_recursive(struct seq_file *sf, void *v)
43114018c   Tejun Heo   cfq-iosched: add ...
1821
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1822
1823
1824
  	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 ...
1825
1826
  	return 0;
  }
2da8ca822   Tejun Heo   cgroup: replace c...
1827
  static int cfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
43114018c   Tejun Heo   cfq-iosched: add ...
1828
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1829
1830
1831
  	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 ...
1832
1833
  	return 0;
  }
702747cab   Tejun Heo   blkcg: remove cfq...
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
  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/...
1867
  #ifdef CONFIG_DEBUG_BLK_CGROUP
f95a04afa   Tejun Heo   blkcg: embed stru...
1868
1869
  static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
  				      struct blkg_policy_data *pd, int off)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1870
  {
f95a04afa   Tejun Heo   blkcg: embed stru...
1871
  	struct cfq_group *cfqg = pd_to_cfqg(pd);
155fead9b   Tejun Heo   blkcg: move blkio...
1872
  	u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1873
1874
1875
  	u64 v = 0;
  
  	if (samples) {
155fead9b   Tejun Heo   blkcg: move blkio...
1876
  		v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
f3cff25f0   Anatol Pomozov   cfq: explicitly u...
1877
  		v = div64_u64(v, samples);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1878
  	}
f95a04afa   Tejun Heo   blkcg: embed stru...
1879
  	__blkg_prfill_u64(sf, pd, v);
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1880
1881
1882
1883
  	return 0;
  }
  
  /* print avg_queue_size */
2da8ca822   Tejun Heo   cgroup: replace c...
1884
  static int cfqg_print_avg_queue_size(struct seq_file *sf, void *v)
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1885
  {
2da8ca822   Tejun Heo   cgroup: replace c...
1886
1887
1888
  	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/...
1889
1890
1891
  	return 0;
  }
  #endif	/* CONFIG_DEBUG_BLK_CGROUP */
880f50e22   Tejun Heo   blkcg: mark exist...
1892
  static struct cftype cfq_blkcg_legacy_files[] = {
1d3650f71   Tejun Heo   cfq-iosched: impl...
1893
  	/* on root, weight is mapped to leaf_weight */
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1894
1895
  	{
  		.name = "weight_device",
1d3650f71   Tejun Heo   cfq-iosched: impl...
1896
  		.flags = CFTYPE_ONLY_ON_ROOT,
2da8ca822   Tejun Heo   cgroup: replace c...
1897
  		.seq_show = cfqg_print_leaf_weight_device,
451af504d   Tejun Heo   cgroup: replace c...
1898
  		.write = cfqg_set_leaf_weight_device,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1899
1900
1901
  	},
  	{
  		.name = "weight",
1d3650f71   Tejun Heo   cfq-iosched: impl...
1902
  		.flags = CFTYPE_ONLY_ON_ROOT,
2da8ca822   Tejun Heo   cgroup: replace c...
1903
  		.seq_show = cfq_print_leaf_weight,
1d3650f71   Tejun Heo   cfq-iosched: impl...
1904
  		.write_u64 = cfq_set_leaf_weight,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1905
  	},
e71357e11   Tejun Heo   cfq-iosched: add ...
1906

1d3650f71   Tejun Heo   cfq-iosched: impl...
1907
  	/* no such mapping necessary for !roots */
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1908
1909
  	{
  		.name = "weight_device",
1d3650f71   Tejun Heo   cfq-iosched: impl...
1910
  		.flags = CFTYPE_NOT_ON_ROOT,
2da8ca822   Tejun Heo   cgroup: replace c...
1911
  		.seq_show = cfqg_print_weight_device,
451af504d   Tejun Heo   cgroup: replace c...
1912
  		.write = cfqg_set_weight_device,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1913
1914
1915
  	},
  	{
  		.name = "weight",
1d3650f71   Tejun Heo   cfq-iosched: impl...
1916
  		.flags = CFTYPE_NOT_ON_ROOT,
2da8ca822   Tejun Heo   cgroup: replace c...
1917
  		.seq_show = cfq_print_weight,
3381cb8d2   Tejun Heo   blkcg: move blkio...
1918
  		.write_u64 = cfq_set_weight,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1919
  	},
e71357e11   Tejun Heo   cfq-iosched: add ...
1920

e71357e11   Tejun Heo   cfq-iosched: add ...
1921
1922
  	{
  		.name = "leaf_weight_device",
2da8ca822   Tejun Heo   cgroup: replace c...
1923
  		.seq_show = cfqg_print_leaf_weight_device,
451af504d   Tejun Heo   cgroup: replace c...
1924
  		.write = cfqg_set_leaf_weight_device,
e71357e11   Tejun Heo   cfq-iosched: add ...
1925
1926
1927
  	},
  	{
  		.name = "leaf_weight",
2da8ca822   Tejun Heo   cgroup: replace c...
1928
  		.seq_show = cfq_print_leaf_weight,
e71357e11   Tejun Heo   cfq-iosched: add ...
1929
1930
  		.write_u64 = cfq_set_leaf_weight,
  	},
43114018c   Tejun Heo   cfq-iosched: add ...
1931
  	/* statistics, covers only the tasks in the cfqg */
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1932
1933
  	{
  		.name = "time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1934
  		.private = offsetof(struct cfq_group, stats.time),
2da8ca822   Tejun Heo   cgroup: replace c...
1935
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1936
1937
1938
  	},
  	{
  		.name = "sectors",
702747cab   Tejun Heo   blkcg: remove cfq...
1939
  		.seq_show = cfqg_print_stat_sectors,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1940
1941
1942
  	},
  	{
  		.name = "io_service_bytes",
77ea73388   Tejun Heo   blkcg: move io_se...
1943
1944
  		.private = (unsigned long)&blkcg_policy_cfq,
  		.seq_show = blkg_print_stat_bytes,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1945
1946
1947
  	},
  	{
  		.name = "io_serviced",
77ea73388   Tejun Heo   blkcg: move io_se...
1948
1949
  		.private = (unsigned long)&blkcg_policy_cfq,
  		.seq_show = blkg_print_stat_ios,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1950
1951
1952
  	},
  	{
  		.name = "io_service_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1953
  		.private = offsetof(struct cfq_group, stats.service_time),
2da8ca822   Tejun Heo   cgroup: replace c...
1954
  		.seq_show = cfqg_print_rwstat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1955
1956
1957
  	},
  	{
  		.name = "io_wait_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1958
  		.private = offsetof(struct cfq_group, stats.wait_time),
2da8ca822   Tejun Heo   cgroup: replace c...
1959
  		.seq_show = cfqg_print_rwstat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1960
1961
1962
  	},
  	{
  		.name = "io_merged",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1963
  		.private = offsetof(struct cfq_group, stats.merged),
2da8ca822   Tejun Heo   cgroup: replace c...
1964
  		.seq_show = cfqg_print_rwstat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1965
1966
1967
  	},
  	{
  		.name = "io_queued",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
1968
  		.private = offsetof(struct cfq_group, stats.queued),
2da8ca822   Tejun Heo   cgroup: replace c...
1969
  		.seq_show = cfqg_print_rwstat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
1970
  	},
43114018c   Tejun Heo   cfq-iosched: add ...
1971
1972
1973
1974
1975
  
  	/* 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...
1976
  		.seq_show = cfqg_print_stat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1977
1978
1979
  	},
  	{
  		.name = "sectors_recursive",
702747cab   Tejun Heo   blkcg: remove cfq...
1980
  		.seq_show = cfqg_print_stat_sectors_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1981
1982
1983
  	},
  	{
  		.name = "io_service_bytes_recursive",
77ea73388   Tejun Heo   blkcg: move io_se...
1984
1985
  		.private = (unsigned long)&blkcg_policy_cfq,
  		.seq_show = blkg_print_stat_bytes_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1986
1987
1988
  	},
  	{
  		.name = "io_serviced_recursive",
77ea73388   Tejun Heo   blkcg: move io_se...
1989
1990
  		.private = (unsigned long)&blkcg_policy_cfq,
  		.seq_show = blkg_print_stat_ios_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1991
1992
1993
1994
  	},
  	{
  		.name = "io_service_time_recursive",
  		.private = offsetof(struct cfq_group, stats.service_time),
2da8ca822   Tejun Heo   cgroup: replace c...
1995
  		.seq_show = cfqg_print_rwstat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
1996
1997
1998
1999
  	},
  	{
  		.name = "io_wait_time_recursive",
  		.private = offsetof(struct cfq_group, stats.wait_time),
2da8ca822   Tejun Heo   cgroup: replace c...
2000
  		.seq_show = cfqg_print_rwstat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
2001
2002
2003
2004
  	},
  	{
  		.name = "io_merged_recursive",
  		.private = offsetof(struct cfq_group, stats.merged),
2da8ca822   Tejun Heo   cgroup: replace c...
2005
  		.seq_show = cfqg_print_rwstat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
2006
2007
2008
2009
  	},
  	{
  		.name = "io_queued_recursive",
  		.private = offsetof(struct cfq_group, stats.queued),
2da8ca822   Tejun Heo   cgroup: replace c...
2010
  		.seq_show = cfqg_print_rwstat_recursive,
43114018c   Tejun Heo   cfq-iosched: add ...
2011
  	},
60c2bc2d5   Tejun Heo   blkcg: move conf/...
2012
2013
2014
  #ifdef CONFIG_DEBUG_BLK_CGROUP
  	{
  		.name = "avg_queue_size",
2da8ca822   Tejun Heo   cgroup: replace c...
2015
  		.seq_show = cfqg_print_avg_queue_size,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
2016
2017
2018
  	},
  	{
  		.name = "group_wait_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
2019
  		.private = offsetof(struct cfq_group, stats.group_wait_time),
2da8ca822   Tejun Heo   cgroup: replace c...
2020
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
2021
2022
2023
  	},
  	{
  		.name = "idle_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
2024
  		.private = offsetof(struct cfq_group, stats.idle_time),
2da8ca822   Tejun Heo   cgroup: replace c...
2025
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
2026
2027
2028
  	},
  	{
  		.name = "empty_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
2029
  		.private = offsetof(struct cfq_group, stats.empty_time),
2da8ca822   Tejun Heo   cgroup: replace c...
2030
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
2031
2032
2033
  	},
  	{
  		.name = "dequeue",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
2034
  		.private = offsetof(struct cfq_group, stats.dequeue),
2da8ca822   Tejun Heo   cgroup: replace c...
2035
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
2036
2037
2038
  	},
  	{
  		.name = "unaccounted_time",
5bc4afb1e   Tejun Heo   blkcg: drop BLKCG...
2039
  		.private = offsetof(struct cfq_group, stats.unaccounted_time),
2da8ca822   Tejun Heo   cgroup: replace c...
2040
  		.seq_show = cfqg_print_stat,
60c2bc2d5   Tejun Heo   blkcg: move conf/...
2041
2042
2043
2044
  	},
  #endif	/* CONFIG_DEBUG_BLK_CGROUP */
  	{ }	/* terminate */
  };
2ee867dcf   Tejun Heo   blkcg: implement ...
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
  
  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...
2070
  		ret = __cfq_set_weight(of_css(of), v, true, false, false);
2ee867dcf   Tejun Heo   blkcg: implement ...
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
  		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...
2087
  #else /* GROUP_IOSCHED */
ae1188963   Tejun Heo   blkcg: consolidat...
2088
2089
  static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
  					 struct blkcg *blkcg)
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
2090
  {
f51b802c1   Tejun Heo   blkcg: use the us...
2091
  	return cfqd->root_group;
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
2092
  }
7f1dc8a2d   Vivek Goyal   blkio: Fix blkio ...
2093

25fb5169d   Vivek Goyal   blkio: Dynamic cf...
2094
2095
2096
2097
2098
2099
  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...
2100
  /*
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
2101
   * The cfqd->service_trees holds all pending cfq_queue's that have
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2102
2103
2104
   * requests waiting to be processed. It is sorted in the order that
   * we will service the queues.
   */
a36e71f99   Jens Axboe   cfq-iosched: add ...
2105
  static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
a6151c3a5   Jens Axboe   cfq-iosched: appl...
2106
  				 bool add_front)
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2107
  {
0871714e0   Jens Axboe   cfq-iosched: rela...
2108
2109
  	struct rb_node **p, *parent;
  	struct cfq_queue *__cfqq;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2110
  	u64 rb_key;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2111
  	struct cfq_rb_root *st;
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2112
  	int left;
dae739ebc   Vivek Goyal   blkio: Group time...
2113
  	int new_cfqq = 1;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2114
  	u64 now = ktime_get_ns();
ae30c2865   Vivek Goyal   blkio: Implement ...
2115

34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2116
  	st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
0871714e0   Jens Axboe   cfq-iosched: rela...
2117
2118
  	if (cfq_class_idle(cfqq)) {
  		rb_key = CFQ_IDLE_DELAY;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2119
  		parent = rb_last(&st->rb);
0871714e0   Jens Axboe   cfq-iosched: rela...
2120
2121
2122
2123
  		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...
2124
  			rb_key += now;
0871714e0   Jens Axboe   cfq-iosched: rela...
2125
  	} else if (!add_front) {
b9c8946b1   Jens Axboe   cfq-iosched: fix ...
2126
2127
2128
2129
2130
2131
  		/*
  		 * 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...
2132
  		rb_key = cfq_slice_offset(cfqd, cfqq) + now;
b9c8946b1   Jens Axboe   cfq-iosched: fix ...
2133
  		rb_key -= cfqq->slice_resid;
edd75ffd9   Jens Axboe   cfq-iosched: get ...
2134
  		cfqq->slice_resid = 0;
48e025e63   Corrado Zoccolo   cfq-iosched: fix ...
2135
  	} else {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2136
  		rb_key = -NSEC_PER_SEC;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2137
  		__cfqq = cfq_rb_first(st);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2138
  		rb_key += __cfqq ? __cfqq->rb_key : now;
48e025e63   Corrado Zoccolo   cfq-iosched: fix ...
2139
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2140

d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2141
  	if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
dae739ebc   Vivek Goyal   blkio: Group time...
2142
  		new_cfqq = 0;
99f9628ab   Jens Axboe   [PATCH] cfq-iosch...
2143
  		/*
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2144
  		 * same position, nothing more to do
99f9628ab   Jens Axboe   [PATCH] cfq-iosch...
2145
  		 */
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2146
  		if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2147
  			return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2148

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

498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2153
  	left = 1;
0871714e0   Jens Axboe   cfq-iosched: rela...
2154
  	parent = NULL;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2155
2156
  	cfqq->service_tree = st;
  	p = &st->rb.rb_node;
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2157
2158
2159
  	while (*p) {
  		parent = *p;
  		__cfqq = rb_entry(parent, struct cfq_queue, rb_node);
0c534e0a4   Jens Axboe   cfq-iosched: sort...
2160
  		/*
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
2161
  		 * sort by key, that represents service time.
0c534e0a4   Jens Axboe   cfq-iosched: sort...
2162
  		 */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2163
  		if (rb_key < __cfqq->rb_key)
1f23f1215   Vivek Goyal   cfq-iosched: Get ...
2164
  			p = &parent->rb_left;
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
2165
  		else {
1f23f1215   Vivek Goyal   cfq-iosched: Get ...
2166
  			p = &parent->rb_right;
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
2167
  			left = 0;
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
2168
  		}
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2169
  	}
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
2170
  	if (left)
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2171
  		st->left = &cfqq->rb_node;
cc09e2990   Jens Axboe   [PATCH] cfq-iosch...
2172

d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2173
2174
  	cfqq->rb_key = rb_key;
  	rb_link_node(&cfqq->rb_node, parent, p);
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2175
2176
  	rb_insert_color(&cfqq->rb_node, &st->rb);
  	st->count++;
20359f27e   Namhyung Kim   cfq-iosched: remo...
2177
  	if (add_front || !new_cfqq)
dae739ebc   Vivek Goyal   blkio: Group time...
2178
  		return;
8184f93ec   Justin TerAvest   cfq-iosched: Don'...
2179
  	cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2180
  }
a36e71f99   Jens Axboe   cfq-iosched: add ...
2181
  static struct cfq_queue *
f2d1f0ae7   Jens Axboe   cfq-iosched: cach...
2182
2183
2184
  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 ...
2185
  {
a36e71f99   Jens Axboe   cfq-iosched: add ...
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
  	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...
2201
  		if (sector > blk_rq_pos(cfqq->next_rq))
a36e71f99   Jens Axboe   cfq-iosched: add ...
2202
  			n = &(*p)->rb_right;
2e46e8b27   Tejun Heo   block: drop reque...
2203
  		else if (sector < blk_rq_pos(cfqq->next_rq))
a36e71f99   Jens Axboe   cfq-iosched: add ...
2204
2205
2206
2207
  			n = &(*p)->rb_left;
  		else
  			break;
  		p = n;
3ac6c9f8a   Jens Axboe   cfq-iosched: fix ...
2208
  		cfqq = NULL;
a36e71f99   Jens Axboe   cfq-iosched: add ...
2209
2210
2211
2212
2213
  	}
  
  	*ret_parent = parent;
  	if (rb_link)
  		*rb_link = p;
3ac6c9f8a   Jens Axboe   cfq-iosched: fix ...
2214
  	return cfqq;
a36e71f99   Jens Axboe   cfq-iosched: add ...
2215
2216
2217
2218
  }
  
  static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
a36e71f99   Jens Axboe   cfq-iosched: add ...
2219
2220
  	struct rb_node **p, *parent;
  	struct cfq_queue *__cfqq;
f2d1f0ae7   Jens Axboe   cfq-iosched: cach...
2221
2222
2223
2224
  	if (cfqq->p_root) {
  		rb_erase(&cfqq->p_node, cfqq->p_root);
  		cfqq->p_root = NULL;
  	}
a36e71f99   Jens Axboe   cfq-iosched: add ...
2225
2226
2227
2228
2229
  
  	if (cfq_class_idle(cfqq))
  		return;
  	if (!cfqq->next_rq)
  		return;
f2d1f0ae7   Jens Axboe   cfq-iosched: cach...
2230
  	cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
2e46e8b27   Tejun Heo   block: drop reque...
2231
2232
  	__cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
  				      blk_rq_pos(cfqq->next_rq), &parent, &p);
3ac6c9f8a   Jens Axboe   cfq-iosched: fix ...
2233
2234
  	if (!__cfqq) {
  		rb_link_node(&cfqq->p_node, parent, p);
f2d1f0ae7   Jens Axboe   cfq-iosched: cach...
2235
2236
2237
  		rb_insert_color(&cfqq->p_node, cfqq->p_root);
  	} else
  		cfqq->p_root = NULL;
a36e71f99   Jens Axboe   cfq-iosched: add ...
2238
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2239
2240
2241
  /*
   * Update cfqq's position in the service tree.
   */
edd75ffd9   Jens Axboe   cfq-iosched: get ...
2242
  static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
6d048f531   Jens Axboe   cfq-iosched: deve...
2243
  {
6d048f531   Jens Axboe   cfq-iosched: deve...
2244
2245
2246
  	/*
  	 * Resorting requires the cfqq to be on the RR list already.
  	 */
a36e71f99   Jens Axboe   cfq-iosched: add ...
2247
  	if (cfq_cfqq_on_rr(cfqq)) {
edd75ffd9   Jens Axboe   cfq-iosched: get ...
2248
  		cfq_service_tree_add(cfqd, cfqq, 0);
a36e71f99   Jens Axboe   cfq-iosched: add ...
2249
2250
  		cfq_prio_tree_add(cfqd, cfqq);
  	}
6d048f531   Jens Axboe   cfq-iosched: deve...
2251
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2252
2253
  /*
   * add to busy list of queues for service, trying to be fair in ordering
22e2c507c   Jens Axboe   [PATCH] Update cf...
2254
   * the pending list according to last request service
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2255
   */
febffd618   Jens Axboe   cfq-iosched: kill...
2256
  static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2257
  {
7b679138b   Jens Axboe   cfq-iosched: add ...
2258
  	cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2259
2260
  	BUG_ON(cfq_cfqq_on_rr(cfqq));
  	cfq_mark_cfqq_on_rr(cfqq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2261
  	cfqd->busy_queues++;
ef8a41df8   Shaohua Li   cfq-iosched: give...
2262
2263
  	if (cfq_cfqq_sync(cfqq))
  		cfqd->busy_sync_queues++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2264

edd75ffd9   Jens Axboe   cfq-iosched: get ...
2265
  	cfq_resort_rr_list(cfqd, cfqq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2266
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2267
2268
2269
2270
  /*
   * Called when the cfqq no longer has requests pending, remove it from
   * the service tree.
   */
febffd618   Jens Axboe   cfq-iosched: kill...
2271
  static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2272
  {
7b679138b   Jens Axboe   cfq-iosched: add ...
2273
  	cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2274
2275
  	BUG_ON(!cfq_cfqq_on_rr(cfqq));
  	cfq_clear_cfqq_on_rr(cfqq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2276

aa6f6a3de   Corrado Zoccolo   cfq-iosched: prep...
2277
2278
2279
2280
  	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...
2281
2282
2283
2284
  	if (cfqq->p_root) {
  		rb_erase(&cfqq->p_node, cfqq->p_root);
  		cfqq->p_root = NULL;
  	}
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2285

8184f93ec   Justin TerAvest   cfq-iosched: Don'...
2286
  	cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2287
2288
  	BUG_ON(!cfqd->busy_queues);
  	cfqd->busy_queues--;
ef8a41df8   Shaohua Li   cfq-iosched: give...
2289
2290
  	if (cfq_cfqq_sync(cfqq))
  		cfqd->busy_sync_queues--;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2291
2292
2293
2294
2295
  }
  
  /*
   * rb tree support functions
   */
febffd618   Jens Axboe   cfq-iosched: kill...
2296
  static void cfq_del_rq_rb(struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2297
  {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2298
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2299
  	const int sync = rq_is_sync(rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2300

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

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

f04a64246   Vivek Goyal   blkio: Keep queue...
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
  	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
2317
  }
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2318
  static void cfq_add_rq_rb(struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2319
  {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2320
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2321
  	struct cfq_data *cfqd = cfqq->cfqd;
796d5116c   Jeff Moyer   iosched: prevent ...
2322
  	struct request *prev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2323

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

796d5116c   Jeff Moyer   iosched: prevent ...
2326
  	elv_rb_add(&cfqq->sort_list, rq);
5fccbf61b   Jens Axboe   [PATCH] CFQ: requ...
2327
2328
2329
  
  	if (!cfq_cfqq_on_rr(cfqq))
  		cfq_add_cfqq_rr(cfqd, cfqq);
5044eed48   Jens Axboe   cfq-iosched: fix ...
2330
2331
2332
2333
  
  	/*
  	 * check if this request is a better next-serve candidate
  	 */
a36e71f99   Jens Axboe   cfq-iosched: add ...
2334
  	prev = cfqq->next_rq;
cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
2335
  	cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
a36e71f99   Jens Axboe   cfq-iosched: add ...
2336
2337
2338
2339
2340
2341
  
  	/*
  	 * 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 ...
2342
  	BUG_ON(!cfqq->next_rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2343
  }
febffd618   Jens Axboe   cfq-iosched: kill...
2344
  static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2345
  {
5380a101d   Jens Axboe   [PATCH] cfq-iosch...
2346
2347
  	elv_rb_del(&cfqq->sort_list, rq);
  	cfqq->queued[rq_is_sync(rq)]--;
63a4cc248   Mike Christie   blkg_rwstat: sepa...
2348
  	cfqg_stats_update_io_remove(RQ_CFQG(rq), req_op(rq), rq->cmd_flags);
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2349
  	cfq_add_rq_rb(rq);
155fead9b   Tejun Heo   blkcg: move blkio...
2350
  	cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
63a4cc248   Mike Christie   blkg_rwstat: sepa...
2351
  				 req_op(rq), rq->cmd_flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2352
  }
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2353
2354
  static struct request *
  cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2355
  {
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2356
  	struct task_struct *tsk = current;
c58698073   Tejun Heo   block, cfq: reorg...
2357
  	struct cfq_io_cq *cic;
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2358
  	struct cfq_queue *cfqq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2359

4ac845a2e   Jens Axboe   block: cfq: make ...
2360
  	cic = cfq_cic_lookup(cfqd, tsk->io_context);
91fac317a   Vasily Tarasov   cfq-iosched: get ...
2361
2362
2363
2364
  	if (!cic)
  		return NULL;
  
  	cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
f73a1c7d1   Kent Overstreet   block: Add bio_en...
2365
2366
  	if (cfqq)
  		return elv_rb_find(&cfqq->sort_list, bio_end_sector(bio));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2367

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

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

5b93629b4   Tejun Heo   block: implement ...
2378
  	cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2379
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
2380
  static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2381
  {
b4878f245   Jens Axboe   [PATCH] 02/05: up...
2382
  	struct cfq_data *cfqd = q->elevator->elevator_data;
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
2383
2384
  	WARN_ON(!cfqd->rq_in_driver);
  	cfqd->rq_in_driver--;
7b679138b   Jens Axboe   cfq-iosched: add ...
2385
  	cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
2386
  						cfqd->rq_in_driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2387
  }
b4878f245   Jens Axboe   [PATCH] 02/05: up...
2388
  static void cfq_remove_request(struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2389
  {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2390
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
2391

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

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

45333d5a3   Aaron Carroll   cfq-iosched: fix ...
2398
  	cfqq->cfqd->rq_queued--;
63a4cc248   Mike Christie   blkg_rwstat: sepa...
2399
  	cfqg_stats_update_io_remove(RQ_CFQG(rq), req_op(rq), rq->cmd_flags);
65299a3b7   Christoph Hellwig   block: separate p...
2400
2401
2402
  	if (rq->cmd_flags & REQ_PRIO) {
  		WARN_ON(!cfqq->prio_pending);
  		cfqq->prio_pending--;
b53d1ed73   Jens Axboe   Revert "cfq: Remo...
2403
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2404
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
2405
2406
  static int cfq_merge(struct request_queue *q, struct request **req,
  		     struct bio *bio)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2407
2408
2409
  {
  	struct cfq_data *cfqd = q->elevator->elevator_data;
  	struct request *__rq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2410

206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2411
  	__rq = cfq_find_rq_fmerge(cfqd, bio);
72ef799b3   Tahsin Erdogan   block: do not mer...
2412
  	if (__rq && elv_bio_merge_ok(__rq, bio)) {
9817064b6   Jens Axboe   [PATCH] elevator:...
2413
2414
  		*req = __rq;
  		return ELEVATOR_FRONT_MERGE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2415
2416
2417
  	}
  
  	return ELEVATOR_NO_MERGE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2418
  }
165125e1e   Jens Axboe   [BLOCK] Get rid o...
2419
  static void cfq_merged_request(struct request_queue *q, struct request *req,
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
2420
  			       int type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2421
  {
21183b07e   Jens Axboe   [PATCH] cfq-iosch...
2422
  	if (type == ELEVATOR_FRONT_MERGE) {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2423
  		struct cfq_queue *cfqq = RQ_CFQQ(req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2424

5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2425
  		cfq_reposition_rq_rb(cfqq, req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2426
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2427
  }
812d40264   Divyesh Shah   blkio: Add io_mer...
2428
2429
2430
  static void cfq_bio_merged(struct request_queue *q, struct request *req,
  				struct bio *bio)
  {
1eff9d322   Jens Axboe   block: rename bio...
2431
  	cfqg_stats_update_io_merged(RQ_CFQG(req), bio_op(bio), bio->bi_opf);
812d40264   Divyesh Shah   blkio: Add io_mer...
2432
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2433
  static void
165125e1e   Jens Axboe   [BLOCK] Get rid o...
2434
  cfq_merged_requests(struct request_queue *q, struct request *rq,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2435
2436
  		    struct request *next)
  {
cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
2437
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
4a0b75c7d   Shaohua Li   block, cfq: fix e...
2438
  	struct cfq_data *cfqd = q->elevator->elevator_data;
22e2c507c   Jens Axboe   [PATCH] Update cf...
2439
2440
2441
2442
  	/*
  	 * reposition in fifo if next is older than rq
  	 */
  	if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2443
  	    next->fifo_time < rq->fifo_time &&
3d106fba2   Shaohua Li   block CFQ: avoid ...
2444
  	    cfqq == RQ_CFQQ(next)) {
22e2c507c   Jens Axboe   [PATCH] Update cf...
2445
  		list_move(&rq->queuelist, &next->queuelist);
8b4922d31   Jan Kara   block: Stop abusi...
2446
  		rq->fifo_time = next->fifo_time;
30996f40b   Jens Axboe   cfq-iosched: fix ...
2447
  	}
22e2c507c   Jens Axboe   [PATCH] Update cf...
2448

cf7c25cf9   Corrado Zoccolo   cfq-iosched: fix ...
2449
2450
  	if (cfqq->next_rq == next)
  		cfqq->next_rq = rq;
b4878f245   Jens Axboe   [PATCH] 02/05: up...
2451
  	cfq_remove_request(next);
63a4cc248   Mike Christie   blkg_rwstat: sepa...
2452
  	cfqg_stats_update_io_merged(RQ_CFQG(rq), req_op(next), next->cmd_flags);
4a0b75c7d   Shaohua Li   block, cfq: fix e...
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
  
  	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...
2463
  }
72ef799b3   Tahsin Erdogan   block: do not mer...
2464
2465
  static int cfq_allow_bio_merge(struct request_queue *q, struct request *rq,
  			       struct bio *bio)
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2466
2467
  {
  	struct cfq_data *cfqd = q->elevator->elevator_data;
c58698073   Tejun Heo   block, cfq: reorg...
2468
  	struct cfq_io_cq *cic;
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2469
  	struct cfq_queue *cfqq;
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2470
2471
  
  	/*
ec8acb690   Jens Axboe   [PATCH] cfq-iosch...
2472
  	 * Disallow merge of a sync bio into an async request.
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2473
  	 */
91fac317a   Vasily Tarasov   cfq-iosched: get ...
2474
  	if (cfq_bio_sync(bio) && !rq_is_sync(rq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
2475
  		return false;
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2476
2477
  
  	/*
f1a4f4d35   Tejun Heo   block, cfq: fix c...
2478
  	 * Lookup the cfqq that this bio will be queued with and allow
07c2bd373   Tejun Heo   block: don't call...
2479
  	 * merge only if rq is queued there.
f1a4f4d35   Tejun Heo   block, cfq: fix c...
2480
  	 */
07c2bd373   Tejun Heo   block: don't call...
2481
2482
2483
  	cic = cfq_cic_lookup(cfqd, current->io_context);
  	if (!cic)
  		return false;
719d34027   Jens Axboe   [PATCH] cfq-iosch...
2484

91fac317a   Vasily Tarasov   cfq-iosched: get ...
2485
  	cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
a6151c3a5   Jens Axboe   cfq-iosched: appl...
2486
  	return cfqq == RQ_CFQQ(rq);
da7752650   Jens Axboe   [PATCH] cfq-iosch...
2487
  }
72ef799b3   Tahsin Erdogan   block: do not mer...
2488
2489
2490
2491
2492
  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...
2493
2494
  static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
911483258   Jan Kara   cfq-iosched: Conv...
2495
  	hrtimer_try_to_cancel(&cfqd->idle_slice_timer);
155fead9b   Tejun Heo   blkcg: move blkio...
2496
  	cfqg_stats_update_idle_time(cfqq->cfqg);
812df48d1   Divyesh Shah   blkio: Add more d...
2497
  }
febffd618   Jens Axboe   cfq-iosched: kill...
2498
2499
  static void __cfq_set_active_queue(struct cfq_data *cfqd,
  				   struct cfq_queue *cfqq)
22e2c507c   Jens Axboe   [PATCH] Update cf...
2500
2501
  {
  	if (cfqq) {
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
2502
  		cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2503
  				cfqd->serving_wl_class, cfqd->serving_wl_type);
155fead9b   Tejun Heo   blkcg: move blkio...
2504
  		cfqg_stats_update_avg_queue_size(cfqq->cfqg);
62a37f6ba   Justin TerAvest   cfq-iosched: Don'...
2505
  		cfqq->slice_start = 0;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2506
  		cfqq->dispatch_start = ktime_get_ns();
62a37f6ba   Justin TerAvest   cfq-iosched: Don'...
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
  		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...
2519
2520
2521
2522
2523
2524
  	}
  
  	cfqd->active_queue = cfqq;
  }
  
  /*
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2525
2526
2527
2528
   * 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...
2529
  		    bool timed_out)
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2530
  {
7b679138b   Jens Axboe   cfq-iosched: add ...
2531
  	cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2532
  	if (cfq_cfqq_wait_request(cfqq))
812df48d1   Divyesh Shah   blkio: Add more d...
2533
  		cfq_del_timer(cfqd, cfqq);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2534

7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2535
  	cfq_clear_cfqq_wait_request(cfqq);
f75edf2dc   Vivek Goyal   blkio: Wait for c...
2536
  	cfq_clear_cfqq_wait_busy(cfqq);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2537
2538
  
  	/*
ae54abed6   Shaohua Li   cfq-iosched: spli...
2539
2540
2541
2542
2543
2544
2545
2546
2547
  	 * 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'...
2548
  	 * store what was left of this slice, if the queue idled/timed out
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2549
  	 */
c553f8e33   Shaohua Li   block cfq: compen...
2550
2551
  	if (timed_out) {
  		if (cfq_cfqq_slice_new(cfqq))
ba5bd520f   Vivek Goyal   cfq: rename a fun...
2552
  			cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
c553f8e33   Shaohua Li   block cfq: compen...
2553
  		else
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2554
  			cfqq->slice_resid = cfqq->slice_end - ktime_get_ns();
93fdf1478   Jan Kara   cfq-iosched: Conv...
2555
  		cfq_log_cfqq(cfqd, cfqq, "resid=%lld", cfqq->slice_resid);
7b679138b   Jens Axboe   cfq-iosched: add ...
2556
  	}
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2557

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

f04a64246   Vivek Goyal   blkio: Keep queue...
2560
2561
  	if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
  		cfq_del_cfqq_rr(cfqd, cfqq);
edd75ffd9   Jens Axboe   cfq-iosched: get ...
2562
  	cfq_resort_rr_list(cfqd, cfqq);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2563
2564
2565
2566
2567
  
  	if (cfqq == cfqd->active_queue)
  		cfqd->active_queue = NULL;
  
  	if (cfqd->active_cic) {
11a3122f6   Tejun Heo   block: strip out ...
2568
  		put_io_context(cfqd->active_cic->icq.ioc);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2569
2570
  		cfqd->active_cic = NULL;
  	}
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2571
  }
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
2572
  static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2573
2574
2575
2576
  {
  	struct cfq_queue *cfqq = cfqd->active_queue;
  
  	if (cfqq)
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
2577
  		__cfq_slice_expired(cfqd, cfqq, timed_out);
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2578
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2579
2580
2581
2582
  /*
   * 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...
2583
  static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
22e2c507c   Jens Axboe   [PATCH] Update cf...
2584
  {
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2585
2586
  	struct cfq_rb_root *st = st_for(cfqd->serving_group,
  			cfqd->serving_wl_class, cfqd->serving_wl_type);
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2587

f04a64246   Vivek Goyal   blkio: Keep queue...
2588
2589
  	if (!cfqd->rq_queued)
  		return NULL;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
2590
  	/* There is nothing to dispatch */
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2591
  	if (!st)
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
2592
  		return NULL;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2593
  	if (RB_EMPTY_ROOT(&st->rb))
c0324a020   Corrado Zoccolo   cfq-iosched: reim...
2594
  		return NULL;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2595
  	return cfq_rb_first(st);
6d048f531   Jens Axboe   cfq-iosched: deve...
2596
  }
f04a64246   Vivek Goyal   blkio: Keep queue...
2597
2598
  static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
  {
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
2599
  	struct cfq_group *cfqg;
f04a64246   Vivek Goyal   blkio: Keep queue...
2600
2601
2602
2603
2604
2605
  	struct cfq_queue *cfqq;
  	int i, j;
  	struct cfq_rb_root *st;
  
  	if (!cfqd->rq_queued)
  		return NULL;
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
2606
2607
2608
  	cfqg = cfq_get_next_cfqg(cfqd);
  	if (!cfqg)
  		return NULL;
f04a64246   Vivek Goyal   blkio: Keep queue...
2609
2610
2611
2612
2613
  	for_each_cfqg_st(cfqg, i, j, st)
  		if ((cfqq = cfq_rb_first(st)) != NULL)
  			return cfqq;
  	return NULL;
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2614
2615
2616
  /*
   * Get and set a new active queue for service.
   */
a36e71f99   Jens Axboe   cfq-iosched: add ...
2617
2618
  static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
  					      struct cfq_queue *cfqq)
6d048f531   Jens Axboe   cfq-iosched: deve...
2619
  {
e00ef7997   Jens Axboe   cfq-iosched: get ...
2620
  	if (!cfqq)
a36e71f99   Jens Axboe   cfq-iosched: add ...
2621
  		cfqq = cfq_get_next_queue(cfqd);
6d048f531   Jens Axboe   cfq-iosched: deve...
2622

22e2c507c   Jens Axboe   [PATCH] Update cf...
2623
  	__cfq_set_active_queue(cfqd, cfqq);
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2624
  	return cfqq;
22e2c507c   Jens Axboe   [PATCH] Update cf...
2625
  }
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2626
2627
2628
  static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
  					  struct request *rq)
  {
83096ebf1   Tejun Heo   block: convert to...
2629
2630
  	if (blk_rq_pos(rq) >= cfqd->last_position)
  		return blk_rq_pos(rq) - cfqd->last_position;
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2631
  	else
83096ebf1   Tejun Heo   block: convert to...
2632
  		return cfqd->last_position - blk_rq_pos(rq);
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
2633
  }
b2c18e1e0   Jeff Moyer   cfq: calculate th...
2634
  static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
e9ce335df   Shaohua Li   cfq-iosched: fix ...
2635
  			       struct request *rq)
6d048f531   Jens Axboe   cfq-iosched: deve...
2636
  {
e9ce335df   Shaohua Li   cfq-iosched: fix ...
2637
  	return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
6d048f531   Jens Axboe   cfq-iosched: deve...
2638
  }
a36e71f99   Jens Axboe   cfq-iosched: add ...
2639
2640
2641
  static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
  				    struct cfq_queue *cur_cfqq)
  {
f2d1f0ae7   Jens Axboe   cfq-iosched: cach...
2642
  	struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
a36e71f99   Jens Axboe   cfq-iosched: add ...
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
  	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...
2654
  	__cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
a36e71f99   Jens Axboe   cfq-iosched: add ...
2655
2656
2657
2658
2659
2660
2661
2662
  	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 ...
2663
  	if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
a36e71f99   Jens Axboe   cfq-iosched: add ...
2664
  		return __cfqq;
2e46e8b27   Tejun Heo   block: drop reque...
2665
  	if (blk_rq_pos(__cfqq->next_rq) < sector)
a36e71f99   Jens Axboe   cfq-iosched: add ...
2666
2667
2668
2669
2670
2671
2672
  		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 ...
2673
  	if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
a36e71f99   Jens Axboe   cfq-iosched: add ...
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
  		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...
2690
  					      struct cfq_queue *cur_cfqq)
6d048f531   Jens Axboe   cfq-iosched: deve...
2691
  {
a36e71f99   Jens Axboe   cfq-iosched: add ...
2692
  	struct cfq_queue *cfqq;
39c01b219   Divyesh Shah   cfq-iosched: Do n...
2693
2694
  	if (cfq_class_idle(cur_cfqq))
  		return NULL;
e6c5bc737   Jeff Moyer   cfq: break apart ...
2695
2696
2697
2698
  	if (!cfq_cfqq_sync(cur_cfqq))
  		return NULL;
  	if (CFQQ_SEEKY(cur_cfqq))
  		return NULL;
a36e71f99   Jens Axboe   cfq-iosched: add ...
2699
  	/*
b9d8f4c73   Gui Jianfeng   cfq: Optimization...
2700
2701
2702
2703
2704
2705
  	 * 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...
2706
2707
2708
  	 * 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...
2709
  	 */
a36e71f99   Jens Axboe   cfq-iosched: add ...
2710
2711
2712
  	cfqq = cfqq_close(cfqd, cur_cfqq);
  	if (!cfqq)
  		return NULL;
8682e1f15   Vivek Goyal   blkio: Provide so...
2713
2714
2715
  	/* 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...
2716
2717
2718
2719
2720
  	/*
  	 * It only makes sense to merge sync queues.
  	 */
  	if (!cfq_cfqq_sync(cfqq))
  		return NULL;
e6c5bc737   Jeff Moyer   cfq: break apart ...
2721
2722
  	if (CFQQ_SEEKY(cfqq))
  		return NULL;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2723

c0324a020   Corrado Zoccolo   cfq-iosched: reim...
2724
2725
2726
2727
2728
  	/*
  	 * 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 ...
2729
  	return cfqq;
6d048f531   Jens Axboe   cfq-iosched: deve...
2730
  }
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2731
2732
2733
2734
2735
2736
  /*
   * 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...
2737
  	enum wl_class_t wl_class = cfqq_class(cfqq);
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2738
  	struct cfq_rb_root *st = cfqq->service_tree;
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2739

34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2740
2741
  	BUG_ON(!st);
  	BUG_ON(!st->count);
f04a64246   Vivek Goyal   blkio: Keep queue...
2742

b6508c161   Vivek Goyal   cfq-iosched: Do n...
2743
2744
  	if (!cfqd->cfq_slice_idle)
  		return false;
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2745
  	/* We never do for idle class queues. */
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
2746
  	if (wl_class == IDLE_WORKLOAD)
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2747
2748
2749
  		return false;
  
  	/* We do for queues that were marked with idle window flag. */
3c764b7a6   Shaohua Li   cfq-iosched: make...
2750
2751
  	if (cfq_cfqq_idle_window(cfqq) &&
  	   !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2752
2753
2754
2755
2756
2757
  		return true;
  
  	/*
  	 * Otherwise, we do only if they are the last ones
  	 * in their service tree.
  	 */
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2758
2759
  	if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
  	   !cfq_io_thinktime_big(cfqd, &st->ttime, false))
c1e44756f   Shaohua Li   cfq-iosched: do c...
2760
  		return true;
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2761
  	cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
c1e44756f   Shaohua Li   cfq-iosched: do c...
2762
  	return false;
a6d44e982   Corrado Zoccolo   cfq-iosched: enab...
2763
  }
6d048f531   Jens Axboe   cfq-iosched: deve...
2764
  static void cfq_arm_slice_timer(struct cfq_data *cfqd)
22e2c507c   Jens Axboe   [PATCH] Update cf...
2765
  {
1792669cc   Jens Axboe   cfq-iosched: don'...
2766
  	struct cfq_queue *cfqq = cfqd->active_queue;
e795421e4   Jan Kara   cfq-iosched: Don'...
2767
  	struct cfq_rb_root *st = cfqq->service_tree;
c58698073   Tejun Heo   block, cfq: reorg...
2768
  	struct cfq_io_cq *cic;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2769
2770
  	u64 sl, group_idle = 0;
  	u64 now = ktime_get_ns();
7b14e3b52   Jens Axboe   [PATCH] cfq-iosch...
2771

a68bbddba   Jens Axboe   block: add queue ...
2772
  	/*
f7d7b7a7a   Jens Axboe   block: as/cfq ssd...
2773
2774
2775
  	 * 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 ...
2776
  	 */
f7d7b7a7a   Jens Axboe   block: as/cfq ssd...
2777
  	if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
a68bbddba   Jens Axboe   block: add queue ...
2778
  		return;
dd67d0515   Jens Axboe   [PATCH] rbtree: s...
2779
  	WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
6d048f531   Jens Axboe   cfq-iosched: deve...
2780
  	WARN_ON(cfq_cfqq_slice_new(cfqq));
22e2c507c   Jens Axboe   [PATCH] Update cf...
2781
2782
2783
2784
  
  	/*
  	 * idle is disabled, either manually or by past process history
  	 */
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2785
2786
2787
2788
2789
2790
2791
  	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...
2792

22e2c507c   Jens Axboe   [PATCH] Update cf...
2793
  	/*
8e550632c   Corrado Zoccolo   cfq-iosched: fix ...
2794
  	 * still active requests from this queue, don't idle
7b679138b   Jens Axboe   cfq-iosched: add ...
2795
  	 */
8e550632c   Corrado Zoccolo   cfq-iosched: fix ...
2796
  	if (cfqq->dispatched)
7b679138b   Jens Axboe   cfq-iosched: add ...
2797
2798
2799
  		return;
  
  	/*
22e2c507c   Jens Axboe   [PATCH] Update cf...
2800
2801
  	 * task has exited, don't wait
  	 */
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2802
  	cic = cfqd->active_cic;
f6e8d01be   Tejun Heo   block: add io_con...
2803
  	if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
6d048f531   Jens Axboe   cfq-iosched: deve...
2804
  		return;
355b659c8   Corrado Zoccolo   cfq-iosched: avoi...
2805
2806
2807
2808
2809
  	/*
  	 * 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...
2810
  	if (sample_valid(cic->ttime.ttime_samples) &&
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2811
2812
  	    (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...
2813
  			     cic->ttime.ttime_mean);
355b659c8   Corrado Zoccolo   cfq-iosched: avoi...
2814
  		return;
b1ffe737f   Divyesh Shah   cfq-iosched: Add ...
2815
  	}
355b659c8   Corrado Zoccolo   cfq-iosched: avoi...
2816

e795421e4   Jan Kara   cfq-iosched: Don'...
2817
2818
2819
2820
2821
2822
2823
  	/*
  	 * 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...
2824
  		return;
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2825
  	cfq_mark_cfqq_wait_request(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
2826

80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2827
2828
2829
2830
  	if (group_idle)
  		sl = cfqd->cfq_group_idle;
  	else
  		sl = cfqd->cfq_slice_idle;
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
2831

911483258   Jan Kara   cfq-iosched: Conv...
2832
2833
  	hrtimer_start(&cfqd->idle_slice_timer, ns_to_ktime(sl),
  		      HRTIMER_MODE_REL);
155fead9b   Tejun Heo   blkcg: move blkio...
2834
  	cfqg_stats_set_start_idle_time(cfqq->cfqg);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2835
  	cfq_log_cfqq(cfqd, cfqq, "arm_idle: %llu group_idle: %d", sl,
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2836
  			group_idle ? 1 : 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2837
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
2838
2839
2840
  /*
   * Move request from internal lists to the request queue dispatch list.
   */
165125e1e   Jens Axboe   [BLOCK] Get rid o...
2841
  static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2842
  {
3ed9a2965   Jens Axboe   cfq-iosched: impr...
2843
  	struct cfq_data *cfqd = q->elevator->elevator_data;
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
2844
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
2845

7b679138b   Jens Axboe   cfq-iosched: add ...
2846
  	cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
06d218864   Jeff Moyer   cfq: choose a new...
2847
  	cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
5380a101d   Jens Axboe   [PATCH] cfq-iosch...
2848
  	cfq_remove_request(rq);
6d048f531   Jens Axboe   cfq-iosched: deve...
2849
  	cfqq->dispatched++;
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
2850
  	(RQ_CFQG(rq))->dispatched++;
5380a101d   Jens Axboe   [PATCH] cfq-iosch...
2851
  	elv_dispatch_sort(q, rq);
3ed9a2965   Jens Axboe   cfq-iosched: impr...
2852

53c583d22   Corrado Zoccolo   cfq-iosched: requ...
2853
  	cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
c4e7893eb   Vivek Goyal   cfq-iosched: blkt...
2854
  	cfqq->nr_sectors += blk_rq_sectors(rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2855
2856
2857
2858
2859
  }
  
  /*
   * return expired entry, or NULL to just start from scratch in rbtree
   */
febffd618   Jens Axboe   cfq-iosched: kill...
2860
  static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2861
  {
30996f40b   Jens Axboe   cfq-iosched: fix ...
2862
  	struct request *rq = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2863

3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
2864
  	if (cfq_cfqq_fifo_expire(cfqq))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2865
  		return NULL;
cb8874119   Jens Axboe   cfq-iosched: twea...
2866
2867
  
  	cfq_mark_cfqq_fifo_expire(cfqq);
89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
2868
2869
  	if (list_empty(&cfqq->fifo))
  		return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2870

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

30996f40b   Jens Axboe   cfq-iosched: fix ...
2875
  	cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
6d048f531   Jens Axboe   cfq-iosched: deve...
2876
  	return rq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2877
  }
22e2c507c   Jens Axboe   [PATCH] Update cf...
2878
2879
2880
2881
  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
2882

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

b9f8ce059   Namhyung Kim   cfq-iosched: alge...
2885
  	return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2886
  }
22e2c507c   Jens Axboe   [PATCH] Update cf...
2887
  /*
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2888
2889
2890
2891
2892
2893
2894
   * 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 ...
2895
  	process_refs = cfqq->ref - io_refs;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2896
2897
2898
2899
2900
2901
  	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 ...
2902
  	int process_refs, new_process_refs;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2903
  	struct cfq_queue *__cfqq;
c10b61f09   Jeff Moyer   cfq: Don't allow ...
2904
2905
2906
2907
2908
2909
2910
2911
  	/*
  	 * 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...
2912
2913
2914
2915
2916
2917
2918
2919
  	/* 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 ...
2920
  	new_process_refs = cfqq_process_refs(new_cfqq);
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2921
2922
2923
2924
  	/*
  	 * If the process for the cfqq has gone away, there is no
  	 * sense in merging the queues.
  	 */
c10b61f09   Jeff Moyer   cfq: Don't allow ...
2925
  	if (process_refs == 0 || new_process_refs == 0)
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2926
  		return;
e6c5bc737   Jeff Moyer   cfq: break apart ...
2927
2928
2929
  	/*
  	 * Merge in the direction of the lesser amount of work.
  	 */
e6c5bc737   Jeff Moyer   cfq: break apart ...
2930
2931
  	if (new_process_refs >= process_refs) {
  		cfqq->new_cfqq = new_cfqq;
30d7b9448   Shaohua Li   block cfq: don't ...
2932
  		new_cfqq->ref += process_refs;
e6c5bc737   Jeff Moyer   cfq: break apart ...
2933
2934
  	} else {
  		new_cfqq->new_cfqq = cfqq;
30d7b9448   Shaohua Li   block cfq: don't ...
2935
  		cfqq->ref += new_process_refs;
e6c5bc737   Jeff Moyer   cfq: break apart ...
2936
  	}
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
2937
  }
6d816ec7c   Vivek Goyal   cfq-iosched: Rena...
2938
  static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
3bf10fea3   Vivek Goyal   cfq-iosched: Prop...
2939
  			struct cfq_group *cfqg, enum wl_class_t wl_class)
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2940
2941
2942
2943
  {
  	struct cfq_queue *queue;
  	int i;
  	bool key_valid = false;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2944
  	u64 lowest_key = 0;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2945
  	enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
65b32a573   Vivek Goyal   cfq-iosched: Remo...
2946
2947
  	for (i = 0; i <= SYNC_WORKLOAD; ++i) {
  		/* select the one with lowest rb_key */
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2948
  		queue = cfq_rb_first(st_for(cfqg, wl_class, i));
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2949
  		if (queue &&
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2950
  		    (!key_valid || queue->rb_key < lowest_key)) {
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2951
2952
2953
2954
2955
2956
2957
2958
  			lowest_key = queue->rb_key;
  			cur_best = i;
  			key_valid = true;
  		}
  	}
  
  	return cur_best;
  }
6d816ec7c   Vivek Goyal   cfq-iosched: Rena...
2959
2960
  static void
  choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2961
  {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2962
  	u64 slice;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2963
  	unsigned count;
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
2964
  	struct cfq_rb_root *st;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2965
  	u64 group_slice;
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2966
  	enum wl_class_t original_class = cfqd->serving_wl_class;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2967
  	u64 now = ktime_get_ns();
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
2968

718eee057   Corrado Zoccolo   cfq-iosched: fair...
2969
  	/* Choose next priority. RT > BE > IDLE */
58ff82f34   Vivek Goyal   blkio: Implement ...
2970
  	if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2971
  		cfqd->serving_wl_class = RT_WORKLOAD;
58ff82f34   Vivek Goyal   blkio: Implement ...
2972
  	else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2973
  		cfqd->serving_wl_class = BE_WORKLOAD;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2974
  	else {
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2975
  		cfqd->serving_wl_class = IDLE_WORKLOAD;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2976
  		cfqd->workload_expires = now + jiffies_to_nsecs(1);
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2977
2978
  		return;
  	}
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2979
  	if (original_class != cfqd->serving_wl_class)
e4ea0c16a   Shaohua Li writes   block cfq: select...
2980
  		goto new_workload;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2981
2982
2983
2984
2985
  	/*
  	 * 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...
2986
  	st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
2987
  	count = st->count;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2988
2989
  
  	/*
65b32a573   Vivek Goyal   cfq-iosched: Remo...
2990
  	 * check workload expiration, and that we still have other queues ready
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2991
  	 */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
2992
  	if (count && !(now > cfqd->workload_expires))
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2993
  		return;
e4ea0c16a   Shaohua Li writes   block cfq: select...
2994
  new_workload:
718eee057   Corrado Zoccolo   cfq-iosched: fair...
2995
  	/* otherwise select new workload type */
6d816ec7c   Vivek Goyal   cfq-iosched: Rena...
2996
  	cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
2997
  					cfqd->serving_wl_class);
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
2998
  	st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
2999
  	count = st->count;
718eee057   Corrado Zoccolo   cfq-iosched: fair...
3000
3001
3002
3003
3004
3005
  
  	/*
  	 * 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 ...
3006
  	group_slice = cfq_group_slice(cfqd, cfqg);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3007
  	slice = div_u64(group_slice * count,
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
3008
3009
  		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...
3010
  					cfqg)));
718eee057   Corrado Zoccolo   cfq-iosched: fair...
3011

4d2ceea4c   Vivek Goyal   cfq-iosched: More...
3012
  	if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3013
  		u64 tmp;
f26bd1f0a   Vivek Goyal   blkio: Determine ...
3014
3015
3016
3017
3018
3019
3020
3021
  
  		/*
  		 * 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...
3022
3023
  		tmp = cfqd->cfq_target_latency *
  			cfqg_busy_async_queues(cfqd, cfqg);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3024
3025
  		tmp = div_u64(tmp, cfqd->busy_queues);
  		slice = min_t(u64, slice, tmp);
f26bd1f0a   Vivek Goyal   blkio: Determine ...
3026

718eee057   Corrado Zoccolo   cfq-iosched: fair...
3027
3028
  		/* async workload slice is scaled down according to
  		 * the sync/async slice ratio. */
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3029
  		slice = div64_u64(slice*cfqd->cfq_slice[0], cfqd->cfq_slice[1]);
f26bd1f0a   Vivek Goyal   blkio: Determine ...
3030
  	} else
718eee057   Corrado Zoccolo   cfq-iosched: fair...
3031
3032
  		/* sync workload slice is at least 2 * cfq_slice_idle */
  		slice = max(slice, 2 * cfqd->cfq_slice_idle);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3033
3034
3035
  	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...
3036
  }
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
3037
3038
3039
  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 ...
3040
  	struct cfq_group *cfqg;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
3041
3042
3043
  
  	if (RB_EMPTY_ROOT(&st->rb))
  		return NULL;
25bc6b077   Vivek Goyal   blkio: Introduce ...
3044
  	cfqg = cfq_rb_first_group(st);
25bc6b077   Vivek Goyal   blkio: Introduce ...
3045
3046
  	update_min_vdisktime(st);
  	return cfqg;
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
3047
  }
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
3048
3049
  static void cfq_choose_cfqg(struct cfq_data *cfqd)
  {
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
3050
  	struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3051
  	u64 now = ktime_get_ns();
1fa8f6d68   Vivek Goyal   blkio: Introduce ...
3052
3053
  
  	cfqd->serving_group = cfqg;
dae739ebc   Vivek Goyal   blkio: Group time...
3054
3055
  
  	/* Restore the workload type data */
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
3056
  	if (cfqg->saved_wl_slice) {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3057
  		cfqd->workload_expires = now + cfqg->saved_wl_slice;
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
3058
3059
  		cfqd->serving_wl_type = cfqg->saved_wl_type;
  		cfqd->serving_wl_class = cfqg->saved_wl_class;
66ae29197   Gui Jianfeng   cfq: set workload...
3060
  	} else
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3061
  		cfqd->workload_expires = now - 1;
66ae29197   Gui Jianfeng   cfq: set workload...
3062

6d816ec7c   Vivek Goyal   cfq-iosched: Rena...
3063
  	choose_wl_class_and_type(cfqd, cfqg);
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
3064
  }
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
3065
  /*
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
3066
3067
   * 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...
3068
   */
1b5ed5e1f   Tejun Heo   [BLOCK] cfq-iosch...
3069
  static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3070
  {
a36e71f99   Jens Axboe   cfq-iosched: add ...
3071
  	struct cfq_queue *cfqq, *new_cfqq = NULL;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3072
  	u64 now = ktime_get_ns();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3073

22e2c507c   Jens Axboe   [PATCH] Update cf...
3074
3075
3076
  	cfqq = cfqd->active_queue;
  	if (!cfqq)
  		goto new_queue;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3077

f04a64246   Vivek Goyal   blkio: Keep queue...
3078
3079
  	if (!cfqd->rq_queued)
  		return NULL;
c244bb50a   Vivek Goyal   cfq-iosched: Get ...
3080
3081
3082
3083
3084
3085
  
  	/*
  	 * 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...
3086
  	/*
6d048f531   Jens Axboe   cfq-iosched: deve...
3087
  	 * The active queue has run out of time, expire it and select new.
22e2c507c   Jens Axboe   [PATCH] Update cf...
3088
  	 */
7667aa063   Vivek Goyal   cfq-iosched: Take...
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
  	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...
3099
3100
3101
  		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...
3102
  			goto keep_queue;
82bbbf28d   Vivek Goyal   Fix a CFQ crash i...
3103
  		} else
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
3104
  			goto check_group_idle;
7667aa063   Vivek Goyal   cfq-iosched: Take...
3105
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3106

22e2c507c   Jens Axboe   [PATCH] Update cf...
3107
  	/*
6d048f531   Jens Axboe   cfq-iosched: deve...
3108
3109
  	 * The active queue has requests and isn't expired, allow it to
  	 * dispatch.
22e2c507c   Jens Axboe   [PATCH] Update cf...
3110
  	 */
dd67d0515   Jens Axboe   [PATCH] rbtree: s...
3111
  	if (!RB_EMPTY_ROOT(&cfqq->sort_list))
22e2c507c   Jens Axboe   [PATCH] Update cf...
3112
  		goto keep_queue;
6d048f531   Jens Axboe   cfq-iosched: deve...
3113
3114
  
  	/*
a36e71f99   Jens Axboe   cfq-iosched: add ...
3115
3116
3117
  	 * 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...
3118
  	 * tree.  If possible, merge the expiring queue with the new cfqq.
a36e71f99   Jens Axboe   cfq-iosched: add ...
3119
  	 */
b3b6d0408   Jeff Moyer   cfq: change the m...
3120
  	new_cfqq = cfq_close_cooperator(cfqd, cfqq);
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
3121
3122
3123
  	if (new_cfqq) {
  		if (!cfqq->new_cfqq)
  			cfq_setup_merge(cfqq, new_cfqq);
a36e71f99   Jens Axboe   cfq-iosched: add ...
3124
  		goto expire;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
3125
  	}
a36e71f99   Jens Axboe   cfq-iosched: add ...
3126
3127
  
  	/*
6d048f531   Jens Axboe   cfq-iosched: deve...
3128
3129
3130
3131
  	 * 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...
3132
  	if (hrtimer_active(&cfqd->idle_slice_timer)) {
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
3133
3134
3135
  		cfqq = NULL;
  		goto keep_queue;
  	}
8e1ac6655   Shaohua Li   cfq-iosched: don'...
3136
3137
3138
3139
3140
3141
  	/*
  	 * 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...
3142
  	    (cfqq->slice_end - now > now - cfqq->slice_start))) {
8e1ac6655   Shaohua Li   cfq-iosched: don'...
3143
3144
3145
  		cfq_clear_cfqq_deep(cfqq);
  		cfq_clear_cfqq_idle_window(cfqq);
  	}
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
  	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...
3156
3157
3158
  	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...
3159
3160
  		cfqq = NULL;
  		goto keep_queue;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3161
  	}
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
3162
  expire:
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
3163
  	cfq_slice_expired(cfqd, 0);
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
3164
  new_queue:
718eee057   Corrado Zoccolo   cfq-iosched: fair...
3165
3166
3167
3168
3169
  	/*
  	 * Current queue expired. Check if we have to switch to a new
  	 * service tree
  	 */
  	if (!new_cfqq)
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
3170
  		cfq_choose_cfqg(cfqd);
718eee057   Corrado Zoccolo   cfq-iosched: fair...
3171

a36e71f99   Jens Axboe   cfq-iosched: add ...
3172
  	cfqq = cfq_set_active_queue(cfqd, new_cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3173
  keep_queue:
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
3174
  	return cfqq;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3175
  }
febffd618   Jens Axboe   cfq-iosched: kill...
3176
  static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
3177
3178
3179
3180
3181
3182
3183
3184
3185
  {
  	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...
3186
3187
  
  	/* By default cfqq is not expired if it is empty. Do it explicitly */
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
3188
  	__cfq_slice_expired(cfqq->cfqd, cfqq, 0);
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
3189
3190
  	return dispatched;
  }
498d3aa2b   Jens Axboe   [PATCH] cfq-iosch...
3191
3192
3193
3194
  /*
   * Drain our current requests. Used for barriers and when switching
   * io schedulers on-the-fly.
   */
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
3195
  static int cfq_forced_dispatch(struct cfq_data *cfqd)
1b5ed5e1f   Tejun Heo   [BLOCK] cfq-iosch...
3196
  {
0871714e0   Jens Axboe   cfq-iosched: rela...
3197
  	struct cfq_queue *cfqq;
d9e7620e6   Jens Axboe   cfq-iosched: rewo...
3198
  	int dispatched = 0;
cdb16e8f7   Vivek Goyal   blkio: Introduce ...
3199

3440c49f5   Divyesh Shah   cfq-iosched: Fix ...
3200
  	/* Expire the timeslice of the current active queue first */
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
3201
  	cfq_slice_expired(cfqd, 0);
3440c49f5   Divyesh Shah   cfq-iosched: Fix ...
3202
3203
  	while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
  		__cfq_set_active_queue(cfqd, cfqq);
f04a64246   Vivek Goyal   blkio: Keep queue...
3204
  		dispatched += __cfq_forced_dispatch_cfqq(cfqq);
3440c49f5   Divyesh Shah   cfq-iosched: Fix ...
3205
  	}
1b5ed5e1f   Tejun Heo   [BLOCK] cfq-iosch...
3206

1b5ed5e1f   Tejun Heo   [BLOCK] cfq-iosch...
3207
  	BUG_ON(cfqd->busy_queues);
6923715ae   Jeff Moyer   cfq: remove extra...
3208
  	cfq_log(cfqd, "forced_dispatch=%d", dispatched);
1b5ed5e1f   Tejun Heo   [BLOCK] cfq-iosch...
3209
3210
  	return dispatched;
  }
abc3c744d   Shaohua Li   cfq-iosched: quan...
3211
3212
3213
  static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
  	struct cfq_queue *cfqq)
  {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3214
  	u64 now = ktime_get_ns();
abc3c744d   Shaohua Li   cfq-iosched: quan...
3215
3216
  	/* the queue hasn't finished any request, can't estimate */
  	if (cfq_cfqq_slice_new(cfqq))
c1e44756f   Shaohua Li   cfq-iosched: do c...
3217
  		return true;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3218
  	if (now + cfqd->cfq_slice_idle * cfqq->dispatched > cfqq->slice_end)
c1e44756f   Shaohua Li   cfq-iosched: do c...
3219
  		return true;
abc3c744d   Shaohua Li   cfq-iosched: quan...
3220

c1e44756f   Shaohua Li   cfq-iosched: do c...
3221
  	return false;
abc3c744d   Shaohua Li   cfq-iosched: quan...
3222
  }
0b182d617   Jens Axboe   cfq-iosched: abst...
3223
  static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3224
  {
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3225
  	unsigned int max_dispatch;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3226

2f5cb7381   Jens Axboe   cfq-iosched: chan...
3227
  	/*
5ad531db6   Jens Axboe   cfq-iosched: drai...
3228
3229
  	 * Drain async requests before we start sync IO
  	 */
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3230
  	if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
0b182d617   Jens Axboe   cfq-iosched: abst...
3231
  		return false;
5ad531db6   Jens Axboe   cfq-iosched: drai...
3232
3233
  
  	/*
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3234
3235
  	 * If this is an async queue and we have sync IO in flight, let it wait
  	 */
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3236
  	if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
0b182d617   Jens Axboe   cfq-iosched: abst...
3237
  		return false;
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3238

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

2f5cb7381   Jens Axboe   cfq-iosched: chan...
3243
3244
3245
3246
  	/*
  	 * Does this cfqq already have too much IO in flight?
  	 */
  	if (cfqq->dispatched >= max_dispatch) {
ef8a41df8   Shaohua Li   cfq-iosched: give...
3247
  		bool promote_sync = false;
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3248
3249
3250
  		/*
  		 * idle queue must always only have a single IO in flight
  		 */
3ed9a2965   Jens Axboe   cfq-iosched: impr...
3251
  		if (cfq_class_idle(cfqq))
0b182d617   Jens Axboe   cfq-iosched: abst...
3252
  			return false;
3ed9a2965   Jens Axboe   cfq-iosched: impr...
3253

2f5cb7381   Jens Axboe   cfq-iosched: chan...
3254
  		/*
c4ade94fc   Li, Shaohua   cfq-iosched: remo...
3255
3256
  		 * If there is only one sync queue
  		 * we can ignore async queue here and give the sync
ef8a41df8   Shaohua Li   cfq-iosched: give...
3257
3258
3259
3260
  		 * 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...
3261
3262
  		if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
  			promote_sync = true;
ef8a41df8   Shaohua Li   cfq-iosched: give...
3263
3264
  
  		/*
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3265
3266
  		 * We have other queues, don't allow more IO from this one
  		 */
ef8a41df8   Shaohua Li   cfq-iosched: give...
3267
3268
  		if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
  				!promote_sync)
0b182d617   Jens Axboe   cfq-iosched: abst...
3269
  			return false;
9ede209e8   Jens Axboe   cfq-iosched: impr...
3270

2f5cb7381   Jens Axboe   cfq-iosched: chan...
3271
  		/*
474b18ccc   Shaohua Li   cfq-iosched: no d...
3272
  		 * Sole queue user, no limit
365722bb9   Vivek Goyal   cfq-iosched: dela...
3273
  		 */
ef8a41df8   Shaohua Li   cfq-iosched: give...
3274
  		if (cfqd->busy_queues == 1 || promote_sync)
abc3c744d   Shaohua Li   cfq-iosched: quan...
3275
3276
3277
3278
3279
3280
3281
3282
3283
  			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...
3284
3285
3286
3287
3288
3289
3290
  	}
  
  	/*
  	 * 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...
3291
  	if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3292
  		u64 last_sync = ktime_get_ns() - cfqd->last_delayed_sync;
8e2967555   Jens Axboe   cfq-iosched: impl...
3293
  		unsigned int depth;
365722bb9   Vivek Goyal   cfq-iosched: dela...
3294

9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3295
  		depth = div64_u64(last_sync, cfqd->cfq_slice[1]);
e00c54c36   Jens Axboe   cfq-iosched: don'...
3296
3297
  		if (!depth && !cfqq->dispatched)
  			depth = 1;
8e2967555   Jens Axboe   cfq-iosched: impl...
3298
3299
  		if (depth < max_dispatch)
  			max_dispatch = depth;
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3300
  	}
3ed9a2965   Jens Axboe   cfq-iosched: impr...
3301

0b182d617   Jens Axboe   cfq-iosched: abst...
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
  	/*
  	 * If we're below the current max, allow a dispatch
  	 */
  	return cfqq->dispatched < max_dispatch;
  }
  
  /*
   * Dispatch a request from cfqq, moving them to the request queue
   * dispatch list.
   */
  static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
  	struct request *rq;
  
  	BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
  
  	if (!cfq_may_dispatch(cfqd, cfqq))
  		return false;
  
  	/*
  	 * follow expired path, else get first next available
  	 */
  	rq = cfq_check_fifo(cfqq);
  	if (!rq)
  		rq = cfqq->next_rq;
  
  	/*
  	 * insert request into driver dispatch list
  	 */
  	cfq_dispatch_insert(cfqd->queue, rq);
  
  	if (!cfqd->active_cic) {
c58698073   Tejun Heo   block, cfq: reorg...
3334
  		struct cfq_io_cq *cic = RQ_CIC(rq);
0b182d617   Jens Axboe   cfq-iosched: abst...
3335

c58698073   Tejun Heo   block, cfq: reorg...
3336
  		atomic_long_inc(&cic->icq.ioc->refcount);
0b182d617   Jens Axboe   cfq-iosched: abst...
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
  		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...
3360
  		return 0;
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3361
  	/*
0b182d617   Jens Axboe   cfq-iosched: abst...
3362
  	 * Dispatch a request from this cfqq, if it is allowed
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3363
  	 */
0b182d617   Jens Axboe   cfq-iosched: abst...
3364
3365
  	if (!cfq_dispatch_request(cfqd, cfqq))
  		return 0;
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3366
  	cfqq->slice_dispatch++;
b029195dd   Jens Axboe   cfq-iosched: don'...
3367
  	cfq_clear_cfqq_must_dispatch(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3368

2f5cb7381   Jens Axboe   cfq-iosched: chan...
3369
3370
3371
3372
3373
3374
3375
  	/*
  	 * 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...
3376
  		cfqq->slice_end = ktime_get_ns() + 1;
e5ff082e8   Vivek Goyal   blkio: Fix anothe...
3377
  		cfq_slice_expired(cfqd, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3378
  	}
b217a903a   Shan Wei   cfq: fix the log ...
3379
  	cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
2f5cb7381   Jens Axboe   cfq-iosched: chan...
3380
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3381
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3382
  /*
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3383
3384
   * 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
3385
   *
b1c357696   Vivek Goyal   blkio: Take care ...
3386
   * Each cfq queue took a reference on the parent group. Drop it now.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3387
3388
3389
3390
   * queue lock must be held here.
   */
  static void cfq_put_queue(struct cfq_queue *cfqq)
  {
22e2c507c   Jens Axboe   [PATCH] Update cf...
3391
  	struct cfq_data *cfqd = cfqq->cfqd;
0bbfeb832   Justin TerAvest   cfq-iosched: Alwa...
3392
  	struct cfq_group *cfqg;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3393

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

30d7b9448   Shaohua Li   block cfq: don't ...
3396
3397
  	cfqq->ref--;
  	if (cfqq->ref)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3398
  		return;
7b679138b   Jens Axboe   cfq-iosched: add ...
3399
  	cfq_log_cfqq(cfqd, cfqq, "put_queue");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3400
  	BUG_ON(rb_first(&cfqq->sort_list));
22e2c507c   Jens Axboe   [PATCH] Update cf...
3401
  	BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
b1c357696   Vivek Goyal   blkio: Take care ...
3402
  	cfqg = cfqq->cfqg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3403

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

f04a64246   Vivek Goyal   blkio: Keep queue...
3409
  	BUG_ON(cfq_cfqq_on_rr(cfqq));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3410
  	kmem_cache_free(cfq_pool, cfqq);
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
3411
  	cfqg_put(cfqg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3412
  }
d02a2c077   Shaohua Li   cfq-iosched: fix ...
3413
  static void cfq_put_cooperator(struct cfq_queue *cfqq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3414
  {
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
3415
  	struct cfq_queue *__cfqq, *next;
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
  	/*
  	 * 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 ...
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
  }
  
  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...
3442

89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
3443
3444
  	cfq_put_queue(cfqq);
  }
22e2c507c   Jens Axboe   [PATCH] Update cf...
3445

9b84cacd0   Tejun Heo   block, cfq: restr...
3446
3447
3448
  static void cfq_init_icq(struct io_cq *icq)
  {
  	struct cfq_io_cq *cic = icq_to_cic(icq);
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3449
  	cic->ttime.last_end_request = ktime_get_ns();
9b84cacd0   Tejun Heo   block, cfq: restr...
3450
  }
c58698073   Tejun Heo   block, cfq: reorg...
3451
  static void cfq_exit_icq(struct io_cq *icq)
89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
3452
  {
c58698073   Tejun Heo   block, cfq: reorg...
3453
  	struct cfq_io_cq *cic = icq_to_cic(icq);
283287a52   Tejun Heo   block, cfq: misc ...
3454
  	struct cfq_data *cfqd = cic_to_cfqd(cic);
4faa3c815   Fabio Checconi   cfq-iosched: do n...
3455

563180a44   Tejun Heo   cfq-iosched: mino...
3456
3457
3458
  	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...
3459
  	}
563180a44   Tejun Heo   cfq-iosched: mino...
3460
3461
3462
  	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...
3463
  	}
89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
3464
  }
abede6da2   Tejun Heo   cfq: pass around ...
3465
  static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3466
3467
3468
  {
  	struct task_struct *tsk = current;
  	int ioprio_class;
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
3469
  	if (!cfq_cfqq_prio_changed(cfqq))
22e2c507c   Jens Axboe   [PATCH] Update cf...
3470
  		return;
598971bfb   Tejun Heo   cfq: don't use ic...
3471
  	ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3472
  	switch (ioprio_class) {
fe094d98e   Jens Axboe   cfq-iosched: make...
3473
3474
3475
3476
3477
  	default:
  		printk(KERN_ERR "cfq: bad prio %x
  ", ioprio_class);
  	case IOPRIO_CLASS_NONE:
  		/*
6d63c2755   Jens Axboe   cfq-iosched: make...
3478
  		 * no prio set, inherit CPU scheduling settings
fe094d98e   Jens Axboe   cfq-iosched: make...
3479
3480
  		 */
  		cfqq->ioprio = task_nice_ioprio(tsk);
6d63c2755   Jens Axboe   cfq-iosched: make...
3481
  		cfqq->ioprio_class = task_nice_ioclass(tsk);
fe094d98e   Jens Axboe   cfq-iosched: make...
3482
3483
  		break;
  	case IOPRIO_CLASS_RT:
598971bfb   Tejun Heo   cfq: don't use ic...
3484
  		cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
fe094d98e   Jens Axboe   cfq-iosched: make...
3485
3486
3487
  		cfqq->ioprio_class = IOPRIO_CLASS_RT;
  		break;
  	case IOPRIO_CLASS_BE:
598971bfb   Tejun Heo   cfq: don't use ic...
3488
  		cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
fe094d98e   Jens Axboe   cfq-iosched: make...
3489
3490
3491
3492
3493
3494
3495
  		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...
3496
3497
3498
3499
3500
3501
3502
  	}
  
  	/*
  	 * 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...
3503
  	cfqq->org_ioprio_class = cfqq->ioprio_class;
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
3504
  	cfq_clear_cfqq_prio_changed(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3505
  }
598971bfb   Tejun Heo   cfq: don't use ic...
3506
  static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3507
  {
598971bfb   Tejun Heo   cfq: don't use ic...
3508
  	int ioprio = cic->icq.ioc->ioprio;
bca4b914b   Konstantin Khlebnikov   cfq-iosched: remo...
3509
  	struct cfq_data *cfqd = cic_to_cfqd(cic);
478a82b0e   Al Viro   [PATCH] switch to...
3510
  	struct cfq_queue *cfqq;
35e6077cb   Jens Axboe   [PATCH] cfq-iosch...
3511

598971bfb   Tejun Heo   cfq: don't use ic...
3512
3513
3514
3515
3516
  	/*
  	 * 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...
3517
  		return;
563180a44   Tejun Heo   cfq-iosched: mino...
3518
  	cfqq = cic_to_cfqq(cic, false);
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
3519
  	if (cfqq) {
563180a44   Tejun Heo   cfq-iosched: mino...
3520
  		cfq_put_queue(cfqq);
2da8de0bb   Tejun Heo   cfq-iosched: remo...
3521
  		cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio);
563180a44   Tejun Heo   cfq-iosched: mino...
3522
  		cic_set_cfqq(cic, cfqq, false);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3523
  	}
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
3524

563180a44   Tejun Heo   cfq-iosched: mino...
3525
  	cfqq = cic_to_cfqq(cic, true);
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
3526
3527
  	if (cfqq)
  		cfq_mark_cfqq_prio_changed(cfqq);
598971bfb   Tejun Heo   cfq: don't use ic...
3528
3529
  
  	cic->ioprio = ioprio;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3530
  }
d5036d770   Jens Axboe   cfq-iosched: move...
3531
  static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3532
  			  pid_t pid, bool is_sync)
d5036d770   Jens Axboe   cfq-iosched: move...
3533
3534
3535
3536
  {
  	RB_CLEAR_NODE(&cfqq->rb_node);
  	RB_CLEAR_NODE(&cfqq->p_node);
  	INIT_LIST_HEAD(&cfqq->fifo);
30d7b9448   Shaohua Li   block cfq: don't ...
3537
  	cfqq->ref = 0;
d5036d770   Jens Axboe   cfq-iosched: move...
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
  	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...
3549
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
598971bfb   Tejun Heo   cfq: don't use ic...
3550
  static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
24610333d   Vivek Goyal   blkio: Drop the r...
3551
  {
bca4b914b   Konstantin Khlebnikov   cfq-iosched: remo...
3552
  	struct cfq_data *cfqd = cic_to_cfqd(cic);
60a837077   Tejun Heo   cfq-iosched: char...
3553
  	struct cfq_queue *cfqq;
f4da80727   Tejun Heo   blkcg: remove blk...
3554
  	uint64_t serial_nr;
24610333d   Vivek Goyal   blkio: Drop the r...
3555

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

598971bfb   Tejun Heo   cfq: don't use ic...
3560
3561
3562
3563
  	/*
  	 * 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...
3564
  	if (unlikely(!cfqd) || likely(cic->blkcg_serial_nr == serial_nr))
598971bfb   Tejun Heo   cfq: don't use ic...
3565
  		return;
24610333d   Vivek Goyal   blkio: Drop the r...
3566

60a837077   Tejun Heo   cfq-iosched: char...
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
  	/*
  	 * 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...
3583
  	}
598971bfb   Tejun Heo   cfq: don't use ic...
3584

f4da80727   Tejun Heo   blkcg: remove blk...
3585
  	cic->blkcg_serial_nr = serial_nr;
24610333d   Vivek Goyal   blkio: Drop the r...
3586
  }
598971bfb   Tejun Heo   cfq: don't use ic...
3587
3588
  #else
  static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) { }
24610333d   Vivek Goyal   blkio: Drop the r...
3589
  #endif  /* CONFIG_CFQ_GROUP_IOSCHED */
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3590
  static struct cfq_queue **
60a837077   Tejun Heo   cfq-iosched: char...
3591
  cfq_async_queue_prio(struct cfq_group *cfqg, int ioprio_class, int ioprio)
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3592
  {
fe094d98e   Jens Axboe   cfq-iosched: make...
3593
  	switch (ioprio_class) {
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3594
  	case IOPRIO_CLASS_RT:
60a837077   Tejun Heo   cfq-iosched: char...
3595
  		return &cfqg->async_cfqq[0][ioprio];
598971bfb   Tejun Heo   cfq: don't use ic...
3596
3597
3598
  	case IOPRIO_CLASS_NONE:
  		ioprio = IOPRIO_NORM;
  		/* fall through */
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3599
  	case IOPRIO_CLASS_BE:
60a837077   Tejun Heo   cfq-iosched: char...
3600
  		return &cfqg->async_cfqq[1][ioprio];
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3601
  	case IOPRIO_CLASS_IDLE:
60a837077   Tejun Heo   cfq-iosched: char...
3602
  		return &cfqg->async_idle_cfqq;
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3603
3604
3605
3606
  	default:
  		BUG();
  	}
  }
15c31be4d   Jens Axboe   cfq-iosched: fix ...
3607
  static struct cfq_queue *
abede6da2   Tejun Heo   cfq: pass around ...
3608
  cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
2da8de0bb   Tejun Heo   cfq-iosched: remo...
3609
  	      struct bio *bio)
15c31be4d   Jens Axboe   cfq-iosched: fix ...
3610
  {
c6ce19432   Jeff Moyer   cfq-iosched: fix ...
3611
3612
  	int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
  	int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
d4aad7ff0   Tejun Heo   cfq-iosched: fold...
3613
  	struct cfq_queue **async_cfqq = NULL;
4ebc1c61d   Tejun Heo   cfq-iosched: simp...
3614
  	struct cfq_queue *cfqq;
322731ed0   Tejun Heo   cfq-iosched: move...
3615
3616
3617
  	struct cfq_group *cfqg;
  
  	rcu_read_lock();
ae1188963   Tejun Heo   blkcg: consolidat...
3618
  	cfqg = cfq_lookup_cfqg(cfqd, bio_blkcg(bio));
322731ed0   Tejun Heo   cfq-iosched: move...
3619
3620
3621
3622
  	if (!cfqg) {
  		cfqq = &cfqd->oom_cfqq;
  		goto out;
  	}
15c31be4d   Jens Axboe   cfq-iosched: fix ...
3623

c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3624
  	if (!is_sync) {
c6ce19432   Jeff Moyer   cfq-iosched: fix ...
3625
3626
3627
3628
3629
  		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...
3630
  		async_cfqq = cfq_async_queue_prio(cfqg, ioprio_class, ioprio);
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3631
  		cfqq = *async_cfqq;
4ebc1c61d   Tejun Heo   cfq-iosched: simp...
3632
3633
  		if (cfqq)
  			goto out;
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3634
  	}
d4aad7ff0   Tejun Heo   cfq-iosched: fold...
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
  	cfqq = kmem_cache_alloc_node(cfq_pool, GFP_NOWAIT | __GFP_ZERO,
  				     cfqd->queue->node);
  	if (!cfqq) {
  		cfqq = &cfqd->oom_cfqq;
  		goto out;
  	}
  
  	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 ...
3646

d4aad7ff0   Tejun Heo   cfq-iosched: fold...
3647
3648
  	if (async_cfqq) {
  		/* a new async queue is created, pin and remember */
30d7b9448   Shaohua Li   block cfq: don't ...
3649
  		cfqq->ref++;
c2dea2d1f   Vasily Tarasov   cfq: async queue ...
3650
  		*async_cfqq = cfqq;
15c31be4d   Jens Axboe   cfq-iosched: fix ...
3651
  	}
4ebc1c61d   Tejun Heo   cfq-iosched: simp...
3652
  out:
30d7b9448   Shaohua Li   block cfq: don't ...
3653
  	cfqq->ref++;
322731ed0   Tejun Heo   cfq-iosched: move...
3654
  	rcu_read_unlock();
15c31be4d   Jens Axboe   cfq-iosched: fix ...
3655
3656
  	return cfqq;
  }
22e2c507c   Jens Axboe   [PATCH] Update cf...
3657
  static void
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3658
  __cfq_update_io_thinktime(struct cfq_ttime *ttime, u64 slice_idle)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3659
  {
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3660
  	u64 elapsed = ktime_get_ns() - ttime->last_end_request;
383cd7213   Shaohua Li   CFQ: move think t...
3661
  	elapsed = min(elapsed, 2UL * slice_idle);
db3b5848e   Kiyoshi Ueda   When cfq I/O sche...
3662

383cd7213   Shaohua Li   CFQ: move think t...
3663
  	ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3664
3665
3666
  	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...
3667
3668
3669
3670
  }
  
  static void
  cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
c58698073   Tejun Heo   block, cfq: reorg...
3671
  			struct cfq_io_cq *cic)
383cd7213   Shaohua Li   CFQ: move think t...
3672
  {
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
3673
  	if (cfq_cfqq_sync(cfqq)) {
383cd7213   Shaohua Li   CFQ: move think t...
3674
  		__cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
3675
3676
3677
  		__cfq_update_io_thinktime(&cfqq->service_tree->ttime,
  			cfqd->cfq_slice_idle);
  	}
7700fc4f6   Shaohua Li   CFQ: add think ti...
3678
3679
3680
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
  	__cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
  #endif
22e2c507c   Jens Axboe   [PATCH] Update cf...
3681
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3682

206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
3683
  static void
b2c18e1e0   Jeff Moyer   cfq: calculate th...
3684
  cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
6d048f531   Jens Axboe   cfq-iosched: deve...
3685
  		       struct request *rq)
206dc69b3   Jens Axboe   [BLOCK] cfq-iosch...
3686
  {
3dde36dde   Corrado Zoccolo   cfq-iosched: rewo...
3687
  	sector_t sdist = 0;
41647e7a9   Corrado Zoccolo   cfq-iosched: reth...
3688
  	sector_t n_sec = blk_rq_sectors(rq);
3dde36dde   Corrado Zoccolo   cfq-iosched: rewo...
3689
3690
3691
3692
3693
3694
  	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...
3695

3dde36dde   Corrado Zoccolo   cfq-iosched: rewo...
3696
  	cfqq->seek_history <<= 1;
41647e7a9   Corrado Zoccolo   cfq-iosched: reth...
3697
3698
3699
3700
  	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...
3701
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3702

22e2c507c   Jens Axboe   [PATCH] Update cf...
3703
3704
3705
3706
3707
3708
  /*
   * 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...
3709
  		       struct cfq_io_cq *cic)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3710
  {
7b679138b   Jens Axboe   cfq-iosched: add ...
3711
  	int old_idle, enable_idle;
1be92f2fc   Jens Axboe   cfq-iosched: neve...
3712

0871714e0   Jens Axboe   cfq-iosched: rela...
3713
3714
3715
3716
  	/*
  	 * 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...
3717
  		return;
c265a7f41   Jens Axboe   cfq-iosched: get ...
3718
  	enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3719

76280aff1   Corrado Zoccolo   cfq-iosched: idli...
3720
3721
  	if (cfqq->queued[0] + cfqq->queued[1] >= 4)
  		cfq_mark_cfqq_deep(cfqq);
749ef9f84   Corrado Zoccolo   cfq: improve fsyn...
3722
3723
  	if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE))
  		enable_idle = 0;
f6e8d01be   Tejun Heo   block: add io_con...
3724
  	else if (!atomic_read(&cic->icq.ioc->active_ref) ||
c58698073   Tejun Heo   block, cfq: reorg...
3725
3726
  		 !cfqd->cfq_slice_idle ||
  		 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
22e2c507c   Jens Axboe   [PATCH] Update cf...
3727
  		enable_idle = 0;
383cd7213   Shaohua Li   CFQ: move think t...
3728
3729
  	else if (sample_valid(cic->ttime.ttime_samples)) {
  		if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3730
3731
3732
  			enable_idle = 0;
  		else
  			enable_idle = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3733
  	}
7b679138b   Jens Axboe   cfq-iosched: add ...
3734
3735
3736
3737
3738
3739
3740
  	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...
3741
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3742

22e2c507c   Jens Axboe   [PATCH] Update cf...
3743
3744
3745
3746
  /*
   * 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...
3747
  static bool
22e2c507c   Jens Axboe   [PATCH] Update cf...
3748
  cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3749
  		   struct request *rq)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3750
  {
6d048f531   Jens Axboe   cfq-iosched: deve...
3751
  	struct cfq_queue *cfqq;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3752

6d048f531   Jens Axboe   cfq-iosched: deve...
3753
3754
  	cfqq = cfqd->active_queue;
  	if (!cfqq)
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3755
  		return false;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3756

6d048f531   Jens Axboe   cfq-iosched: deve...
3757
  	if (cfq_class_idle(new_cfqq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3758
  		return false;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3759
3760
  
  	if (cfq_class_idle(cfqq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3761
  		return true;
1e3335de0   Jens Axboe   cfq-iosched: impr...
3762

22e2c507c   Jens Axboe   [PATCH] Update cf...
3763
  	/*
875feb63b   Divyesh Shah   cfq-iosched: Resp...
3764
3765
3766
3767
3768
3769
  	 * 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...
3770
3771
3772
  	 * if the new request is sync, but the currently running queue is
  	 * not, let the sync request have priority.
  	 */
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3773
  	if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3774
  		return true;
1e3335de0   Jens Axboe   cfq-iosched: impr...
3775

3984aa552   Jan Kara   cfq-iosched: Allo...
3776
3777
3778
3779
3780
3781
  	/*
  	 * 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...
3782
3783
3784
3785
  		return false;
  
  	if (cfq_slice_used(cfqq))
  		return true;
6c80731c7   Jan Kara   cfq-iosched: Reor...
3786
3787
3788
3789
3790
3791
3792
  	/*
  	 * 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...
3793
  	/* Allow preemption only if we are idling on sync-noidle tree */
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
3794
  	if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
8682e1f15   Vivek Goyal   blkio: Provide so...
3795
  	    cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
8682e1f15   Vivek Goyal   blkio: Provide so...
3796
3797
  	    RB_EMPTY_ROOT(&cfqq->sort_list))
  		return true;
374f84ac3   Jens Axboe   [PATCH] cfq-iosch...
3798
  	/*
b53d1ed73   Jens Axboe   Revert "cfq: Remo...
3799
3800
3801
  	 * 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...
3802
  	if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
b53d1ed73   Jens Axboe   Revert "cfq: Remo...
3803
  		return true;
d2d59e18a   Shaohua Li   cfq-iosched: sche...
3804
3805
3806
  	/* 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...
3807
  	if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3808
  		return false;
1e3335de0   Jens Axboe   cfq-iosched: impr...
3809
3810
3811
3812
3813
  
  	/*
  	 * if this request is as-good as one we would expect from the
  	 * current cfqq, let it preempt
  	 */
e9ce335df   Shaohua Li   cfq-iosched: fix ...
3814
  	if (cfq_rq_close(cfqd, cfqq, rq))
a6151c3a5   Jens Axboe   cfq-iosched: appl...
3815
  		return true;
1e3335de0   Jens Axboe   cfq-iosched: impr...
3816

a6151c3a5   Jens Axboe   cfq-iosched: appl...
3817
  	return false;
22e2c507c   Jens Axboe   [PATCH] Update cf...
3818
3819
3820
3821
3822
3823
3824
3825
  }
  
  /*
   * 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...
3826
  	enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
7b679138b   Jens Axboe   cfq-iosched: add ...
3827
  	cfq_log_cfqq(cfqd, cfqq, "preempt");
df0793abb   Shaohua Li   block,cfq: change...
3828
  	cfq_slice_expired(cfqd, 1);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3829

bf5722567   Jens Axboe   [PATCH] cfq-iosch...
3830
  	/*
f8ae6e3eb   Shaohua Li   block cfq: make q...
3831
3832
3833
  	 * workload type is changed, don't save slice, otherwise preempt
  	 * doesn't happen
  	 */
df0793abb   Shaohua Li   block,cfq: change...
3834
  	if (old_type != cfqq_type(cfqq))
4d2ceea4c   Vivek Goyal   cfq-iosched: More...
3835
  		cfqq->cfqg->saved_wl_slice = 0;
f8ae6e3eb   Shaohua Li   block cfq: make q...
3836
3837
  
  	/*
bf5722567   Jens Axboe   [PATCH] cfq-iosch...
3838
3839
3840
3841
  	 * 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 ...
3842
3843
  
  	cfq_service_tree_add(cfqd, cfqq, 1);
eda5e0c91   Justin TerAvest   cfq-iosched: Don'...
3844

62a37f6ba   Justin TerAvest   cfq-iosched: Don'...
3845
3846
  	cfqq->slice_end = 0;
  	cfq_mark_cfqq_slice_new(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
3847
3848
3849
  }
  
  /*
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3850
   * Called when a new fs request (rq) is added (to cfqq). Check if there's
22e2c507c   Jens Axboe   [PATCH] Update cf...
3851
3852
3853
   * something we should do about it
   */
  static void
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3854
3855
  cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  		struct request *rq)
22e2c507c   Jens Axboe   [PATCH] Update cf...
3856
  {
c58698073   Tejun Heo   block, cfq: reorg...
3857
  	struct cfq_io_cq *cic = RQ_CIC(rq);
12e9fddd6   Jens Axboe   [PATCH] cfq-iosch...
3858

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

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

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

9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3909
  	rq->fifo_time = ktime_get_ns() + cfqd->cfq_fifo_expire[rq_is_sync(rq)];
22e2c507c   Jens Axboe   [PATCH] Update cf...
3910
  	list_add_tail(&rq->queuelist, &cfqq->fifo);
aa6f6a3de   Corrado Zoccolo   cfq-iosched: prep...
3911
  	cfq_add_rq_rb(rq);
63a4cc248   Mike Christie   blkg_rwstat: sepa...
3912
  	cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group, req_op(rq),
155fead9b   Tejun Heo   blkcg: move blkio...
3913
  				 rq->cmd_flags);
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
3914
  	cfq_rq_enqueued(cfqd, cfqq, rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3915
  }
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3916
3917
3918
3919
3920
3921
  /*
   * 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...
3922
  	struct cfq_queue *cfqq = cfqd->active_queue;
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3923
3924
  	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 ...
3925
3926
3927
  
  	if (cfqd->hw_tag == 1)
  		return;
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3928
3929
  
  	if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3930
  	    cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3931
  		return;
1a1238a7d   Shaohua Li   cfq-iosched: impr...
3932
3933
3934
3935
3936
3937
3938
  	/*
  	 * 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...
3939
  	    CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
1a1238a7d   Shaohua Li   cfq-iosched: impr...
3940
  		return;
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3941
3942
  	if (cfqd->hw_tag_samples++ < 50)
  		return;
e459dd08f   Corrado Zoccolo   cfq-iosched: fix ...
3943
  	if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3944
3945
3946
  		cfqd->hw_tag = 1;
  	else
  		cfqd->hw_tag = 0;
45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3947
  }
7667aa063   Vivek Goyal   cfq-iosched: Take...
3948
3949
  static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  {
c58698073   Tejun Heo   block, cfq: reorg...
3950
  	struct cfq_io_cq *cic = cfqd->active_cic;
9a7f38c42   Jeff Moyer   cfq-iosched: Conv...
3951
  	u64 now = ktime_get_ns();
7667aa063   Vivek Goyal   cfq-iosched: Take...
3952

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

33659ebba   Christoph Hellwig   block: remove wra...
3989
3990
  	cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d",
  		     !!(rq->cmd_flags & REQ_NOIDLE));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3991

45333d5a3   Aaron Carroll   cfq-iosched: fix ...
3992
  	cfq_update_hw_tag(cfqd);
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3993
  	WARN_ON(!cfqd->rq_in_driver);
6d048f531   Jens Axboe   cfq-iosched: deve...
3994
  	WARN_ON(!cfqq->dispatched);
53c583d22   Corrado Zoccolo   cfq-iosched: requ...
3995
  	cfqd->rq_in_driver--;
6d048f531   Jens Axboe   cfq-iosched: deve...
3996
  	cfqq->dispatched--;
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
3997
  	(RQ_CFQG(rq))->dispatched--;
155fead9b   Tejun Heo   blkcg: move blkio...
3998
  	cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq),
63a4cc248   Mike Christie   blkg_rwstat: sepa...
3999
4000
  				     rq_io_start_time_ns(rq), req_op(rq),
  				     rq->cmd_flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4001

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

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

383cd7213   Shaohua Li   CFQ: move think t...
4007
  		RQ_CIC(rq)->ttime.last_end_request = now;
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
4008
4009
  
  		if (cfq_cfqq_on_rr(cfqq))
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
4010
  			st = cfqq->service_tree;
f5f2b6ceb   Shaohua Li   CFQ: add think ti...
4011
  		else
34b98d03b   Vivek Goyal   cfq-iosched: Rena...
4012
4013
4014
4015
  			st = st_for(cfqq->cfqg, cfqq_class(cfqq),
  					cfqq_type(cfqq));
  
  		st->ttime.last_end_request = now;
149321a61   Jan Kara   cfq-iosched: Fix ...
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
  		/*
  		 * 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...
4026
  			cfqd->last_delayed_sync = now;
365722bb9   Vivek Goyal   cfq-iosched: dela...
4027
  	}
caaa5f9f0   Jens Axboe   [PATCH] cfq-iosch...
4028

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

53c583d22   Corrado Zoccolo   cfq-iosched: requ...
4071
  	if (!cfqd->rq_in_driver)
23e018a1b   Jens Axboe   block: get rid of...
4072
  		cfq_schedule_dispatch(cfqd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4073
  }
b8269db45   Jens Axboe   cfq-iosched: temp...
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
  static void cfqq_boost_on_prio(struct cfq_queue *cfqq, int op_flags)
  {
  	/*
  	 * 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.
  	 */
  	if (!(op_flags & REQ_PRIO)) {
  		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...
4091
  static inline int __cfq_may_queue(struct cfq_queue *cfqq)
22e2c507c   Jens Axboe   [PATCH] Update cf...
4092
  {
1b379d8da   Jens Axboe   cfq-iosched: get ...
4093
  	if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
4094
  		cfq_mark_cfqq_must_alloc_slice(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4095
  		return ELV_MQUEUE_MUST;
3b18152c3   Jens Axboe   [PATCH] CFQ io sc...
4096
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4097

22e2c507c   Jens Axboe   [PATCH] Update cf...
4098
  	return ELV_MQUEUE_MAY;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4099
  }
ba568ea0a   Mike Christie   block: prepare el...
4100
  static int cfq_may_queue(struct request_queue *q, int op, int op_flags)
22e2c507c   Jens Axboe   [PATCH] Update cf...
4101
4102
4103
  {
  	struct cfq_data *cfqd = q->elevator->elevator_data;
  	struct task_struct *tsk = current;
c58698073   Tejun Heo   block, cfq: reorg...
4104
  	struct cfq_io_cq *cic;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4105
4106
4107
4108
4109
4110
4111
4112
  	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 ...
4113
  	cic = cfq_cic_lookup(cfqd, tsk->io_context);
91fac317a   Vasily Tarasov   cfq-iosched: get ...
4114
4115
  	if (!cic)
  		return ELV_MQUEUE_MAY;
d9d8c5c48   Mike Christie   block: convert is...
4116
  	cfqq = cic_to_cfqq(cic, rw_is_sync(op, op_flags));
22e2c507c   Jens Axboe   [PATCH] Update cf...
4117
  	if (cfqq) {
abede6da2   Tejun Heo   cfq: pass around ...
4118
  		cfq_init_prio_data(cfqq, cic);
b8269db45   Jens Axboe   cfq-iosched: temp...
4119
  		cfqq_boost_on_prio(cfqq, op_flags);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4120

89850f7ee   Jens Axboe   [PATCH] cfq-iosch...
4121
  		return __cfq_may_queue(cfqq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4122
4123
4124
  	}
  
  	return ELV_MQUEUE_MAY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4125
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4126
4127
4128
  /*
   * queue lock held here
   */
bb37b94c6   Jens Axboe   [BLOCK] Cleanup u...
4129
  static void cfq_put_request(struct request *rq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4130
  {
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
4131
  	struct cfq_queue *cfqq = RQ_CFQQ(rq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4132

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

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

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4144
4145
4146
  		cfq_put_queue(cfqq);
  	}
  }
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
4147
  static struct cfq_queue *
c58698073   Tejun Heo   block, cfq: reorg...
4148
  cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
4149
4150
4151
4152
  		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...
4153
  	cfq_mark_cfqq_coop(cfqq->new_cfqq);
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
4154
4155
4156
  	cfq_put_queue(cfqq);
  	return cic_to_cfqq(cic, 1);
  }
e6c5bc737   Jeff Moyer   cfq: break apart ...
4157
4158
4159
4160
4161
  /*
   * 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...
4162
  split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
e6c5bc737   Jeff Moyer   cfq: break apart ...
4163
4164
  {
  	if (cfqq_process_refs(cfqq) == 1) {
e6c5bc737   Jeff Moyer   cfq: break apart ...
4165
4166
  		cfqq->pid = current->pid;
  		cfq_clear_cfqq_coop(cfqq);
ae54abed6   Shaohua Li   cfq-iosched: spli...
4167
  		cfq_clear_cfqq_split_coop(cfqq);
e6c5bc737   Jeff Moyer   cfq: break apart ...
4168
4169
4170
4171
  		return cfqq;
  	}
  
  	cic_set_cfqq(cic, NULL, 1);
d02a2c077   Shaohua Li   cfq-iosched: fix ...
4172
4173
  
  	cfq_put_cooperator(cfqq);
e6c5bc737   Jeff Moyer   cfq: break apart ...
4174
4175
4176
  	cfq_put_queue(cfqq);
  	return NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4177
  /*
22e2c507c   Jens Axboe   [PATCH] Update cf...
4178
   * Allocate cfq data structures associated with this request.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4179
   */
22e2c507c   Jens Axboe   [PATCH] Update cf...
4180
  static int
852c788f8   Tejun Heo   block: implement ...
4181
4182
  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
4183
4184
  {
  	struct cfq_data *cfqd = q->elevator->elevator_data;
f1f8cc946   Tejun Heo   block, cfq: move ...
4185
  	struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4186
  	const int rw = rq_data_dir(rq);
a6151c3a5   Jens Axboe   cfq-iosched: appl...
4187
  	const bool is_sync = rq_is_sync(rq);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4188
  	struct cfq_queue *cfqq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4189

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

598971bfb   Tejun Heo   cfq: don't use ic...
4192
4193
  	check_ioprio_changed(cic, bio);
  	check_blkcg_changed(cic, bio);
e6c5bc737   Jeff Moyer   cfq: break apart ...
4194
  new_queue:
91fac317a   Vasily Tarasov   cfq-iosched: get ...
4195
  	cfqq = cic_to_cfqq(cic, is_sync);
32f2e807a   Vivek Goyal   cfq-iosched: rese...
4196
  	if (!cfqq || cfqq == &cfqd->oom_cfqq) {
bce6133b0   Tejun Heo   cfq-iosched: fix ...
4197
4198
  		if (cfqq)
  			cfq_put_queue(cfqq);
2da8de0bb   Tejun Heo   cfq-iosched: remo...
4199
  		cfqq = cfq_get_queue(cfqd, is_sync, cic, bio);
91fac317a   Vasily Tarasov   cfq-iosched: get ...
4200
  		cic_set_cfqq(cic, cfqq, is_sync);
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
4201
4202
  	} else {
  		/*
e6c5bc737   Jeff Moyer   cfq: break apart ...
4203
4204
  		 * If the queue was seeky for too long, break it apart.
  		 */
ae54abed6   Shaohua Li   cfq-iosched: spli...
4205
  		if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
e6c5bc737   Jeff Moyer   cfq: break apart ...
4206
4207
4208
4209
4210
4211
4212
  			cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
  			cfqq = split_cfqq(cic, cfqq);
  			if (!cfqq)
  				goto new_queue;
  		}
  
  		/*
df5fe3e8e   Jeff Moyer   cfq: merge cooper...
4213
4214
4215
4216
4217
4218
4219
  		 * 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 ...
4220
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4221
4222
  
  	cfqq->allocated[rw]++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4223

6fae9c251   Jens Axboe   Merge commit 'v2....
4224
  	cfqq->ref++;
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
4225
  	cfqg_get(cfqq->cfqg);
a612fddf0   Tejun Heo   block, cfq: move ...
4226
  	rq->elv.priv[0] = cfqq;
1adaf3dde   Tejun Heo   blkcg: move refcn...
4227
  	rq->elv.priv[1] = cfqq->cfqg;
216284c35   Tejun Heo   block, cfq: fix r...
4228
  	spin_unlock_irq(q->queue_lock);
5e7053747   Jens Axboe   [PATCH] cfq-iosch...
4229
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4230
  }
65f27f384   David Howells   WorkStruct: Pass ...
4231
  static void cfq_kick_queue(struct work_struct *work)
22e2c507c   Jens Axboe   [PATCH] Update cf...
4232
  {
65f27f384   David Howells   WorkStruct: Pass ...
4233
  	struct cfq_data *cfqd =
23e018a1b   Jens Axboe   block: get rid of...
4234
  		container_of(work, struct cfq_data, unplug_work);
165125e1e   Jens Axboe   [BLOCK] Get rid o...
4235
  	struct request_queue *q = cfqd->queue;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4236

40bb54d19   Jens Axboe   cfq-iosched: no n...
4237
  	spin_lock_irq(q->queue_lock);
24ecfbe27   Christoph Hellwig   block: add blk_ru...
4238
  	__blk_run_queue(cfqd->queue);
40bb54d19   Jens Axboe   cfq-iosched: no n...
4239
  	spin_unlock_irq(q->queue_lock);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4240
4241
4242
4243
4244
  }
  
  /*
   * Timer running if the active_queue is currently idling inside its time slice
   */
911483258   Jan Kara   cfq-iosched: Conv...
4245
  static enum hrtimer_restart cfq_idle_slice_timer(struct hrtimer *timer)
22e2c507c   Jens Axboe   [PATCH] Update cf...
4246
  {
911483258   Jan Kara   cfq-iosched: Conv...
4247
4248
  	struct cfq_data *cfqd = container_of(timer, struct cfq_data,
  					     idle_slice_timer);
22e2c507c   Jens Axboe   [PATCH] Update cf...
4249
4250
  	struct cfq_queue *cfqq;
  	unsigned long flags;
3c6bd2f87   Jens Axboe   cfq-iosched: chec...
4251
  	int timed_out = 1;
22e2c507c   Jens Axboe   [PATCH] Update cf...
4252

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

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

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

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

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

03aa264ac   Tejun Heo   blkcg: let blkcg ...
4314
  	spin_unlock_irq(q->queue_lock);
a90d742e4   Al Viro   [PATCH] don't bot...
4315
  	cfq_shutdown_timer_wq(cfqd);
ffea73fc7   Tejun Heo   block: blkcg_poli...
4316
4317
4318
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
  	blkcg_deactivate_policy(q, &blkcg_policy_cfq);
  #else
f51b802c1   Tejun Heo   blkcg: use the us...
4319
  	kfree(cfqd->root_group);
2abae55f5   Vivek Goyal   cfq-iosched: Fix ...
4320
  #endif
56edf7d75   Vivek Goyal   cfq-iosched: Fix ...
4321
  	kfree(cfqd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4322
  }
d50235b7b   Jianpeng Ma   elevator: Fix a r...
4323
  static int cfq_init_queue(struct request_queue *q, struct elevator_type *e)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4324
4325
  {
  	struct cfq_data *cfqd;
3c798398e   Tejun Heo   blkcg: mass renam...
4326
  	struct blkcg_gq *blkg __maybe_unused;
a2b1693ba   Tejun Heo   blkcg: implement ...
4327
  	int i, ret;
d50235b7b   Jianpeng Ma   elevator: Fix a r...
4328
4329
4330
4331
4332
  	struct elevator_queue *eq;
  
  	eq = elevator_alloc(q, e);
  	if (!eq)
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4333

c1b511eb2   Joe Perches   block: Convert km...
4334
  	cfqd = kzalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
d50235b7b   Jianpeng Ma   elevator: Fix a r...
4335
4336
  	if (!cfqd) {
  		kobject_put(&eq->kobj);
b2fab5acd   Tejun Heo   elevator: make el...
4337
  		return -ENOMEM;
d50235b7b   Jianpeng Ma   elevator: Fix a r...
4338
4339
  	}
  	eq->elevator_data = cfqd;
80b15c738   Konstantin Khlebnikov   cfq-iosched: comp...
4340

f51b802c1   Tejun Heo   blkcg: use the us...
4341
  	cfqd->queue = q;
d50235b7b   Jianpeng Ma   elevator: Fix a r...
4342
4343
4344
  	spin_lock_irq(q->queue_lock);
  	q->elevator = eq;
  	spin_unlock_irq(q->queue_lock);
f51b802c1   Tejun Heo   blkcg: use the us...
4345

1fa8f6d68   Vivek Goyal   blkio: Introduce ...
4346
4347
  	/* Init root service tree */
  	cfqd->grp_service_tree = CFQ_RB_ROOT;
f51b802c1   Tejun Heo   blkcg: use the us...
4348
  	/* Init root group and prefer root group over other groups by default */
25fb5169d   Vivek Goyal   blkio: Dynamic cf...
4349
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398e   Tejun Heo   blkcg: mass renam...
4350
  	ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
a2b1693ba   Tejun Heo   blkcg: implement ...
4351
4352
  	if (ret)
  		goto out_free;
f51b802c1   Tejun Heo   blkcg: use the us...
4353

a2b1693ba   Tejun Heo   blkcg: implement ...
4354
  	cfqd->root_group = blkg_to_cfqg(q->root_blkg);
f51b802c1   Tejun Heo   blkcg: use the us...
4355
  #else
a2b1693ba   Tejun Heo   blkcg: implement ...
4356
  	ret = -ENOMEM;
f51b802c1   Tejun Heo   blkcg: use the us...
4357
4358
  	cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
  					GFP_KERNEL, cfqd->queue->node);
a2b1693ba   Tejun Heo   blkcg: implement ...
4359
4360
  	if (!cfqd->root_group)
  		goto out_free;
5624a4e44   Vivek Goyal   blk-throttle: Mak...
4361

a2b1693ba   Tejun Heo   blkcg: implement ...
4362
  	cfq_init_cfqg_base(cfqd->root_group);
3ecca6293   Tejun Heo   blkcg: s/CFQ_WEIG...
4363
4364
  	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...
4365
  #endif
5624a4e44   Vivek Goyal   blk-throttle: Mak...
4366

26a2ac009   Jens Axboe   cfq-iosched: clea...
4367
4368
4369
4370
4371
4372
4373
  	/*
  	 * 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 ...
4374
  	/*
d4aad7ff0   Tejun Heo   cfq-iosched: fold...
4375
  	 * Our fallback cfqq if cfq_get_queue() runs into OOM issues.
6118b70b3   Jens Axboe   cfq-iosched: get ...
4376
  	 * Grab a permanent reference to it, so that the normal code flow
f51b802c1   Tejun Heo   blkcg: use the us...
4377
4378
4379
  	 * 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 ...
4380
4381
  	 */
  	cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
30d7b9448   Shaohua Li   block cfq: don't ...
4382
  	cfqd->oom_cfqq.ref++;
1adaf3dde   Tejun Heo   blkcg: move refcn...
4383
4384
  
  	spin_lock_irq(q->queue_lock);
f51b802c1   Tejun Heo   blkcg: use the us...
4385
  	cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
eb7d8c07f   Tejun Heo   cfq: fix cfqg ref...
4386
  	cfqg_put(cfqd->root_group);
1adaf3dde   Tejun Heo   blkcg: move refcn...
4387
  	spin_unlock_irq(q->queue_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4388

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

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

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

e4a9bde95   Tejun Heo   blkcg: replace bl...
4595
  	.cpd_alloc_fn		= cfq_cpd_alloc,
e48453c38   Arianna Avanzini   block, cgroup: im...
4596
  	.cpd_init_fn		= cfq_cpd_init,
e4a9bde95   Tejun Heo   blkcg: replace bl...
4597
  	.cpd_free_fn		= cfq_cpd_free,
69d7fde59   Tejun Heo   blkcg: use CGROUP...
4598
  	.cpd_bind_fn		= cfq_cpd_bind,
e4a9bde95   Tejun Heo   blkcg: replace bl...
4599

001bea73e   Tejun Heo   blkcg: replace bl...
4600
  	.pd_alloc_fn		= cfq_pd_alloc,
f9fcc2d39   Tejun Heo   blkcg: collapse b...
4601
  	.pd_init_fn		= cfq_pd_init,
0b39920b5   Tejun Heo   cfq-iosched: coll...
4602
  	.pd_offline_fn		= cfq_pd_offline,
001bea73e   Tejun Heo   blkcg: replace bl...
4603
  	.pd_free_fn		= cfq_pd_free,
f9fcc2d39   Tejun Heo   blkcg: collapse b...
4604
  	.pd_reset_stats_fn	= cfq_pd_reset_stats,
3e2520668   Vivek Goyal   blkio: Implement ...
4605
  };
3e2520668   Vivek Goyal   blkio: Implement ...
4606
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4607
4608
  static int __init cfq_init(void)
  {
3d3c2379f   Tejun Heo   block, cfq: move ...
4609
  	int ret;
80bdf0c78   Vivek Goyal   cfq-iosched: Impl...
4610
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398e   Tejun Heo   blkcg: mass renam...
4611
  	ret = blkcg_policy_register(&blkcg_policy_cfq);
8bd435b30   Tejun Heo   blkcg: remove sta...
4612
4613
  	if (ret)
  		return ret;
ffea73fc7   Tejun Heo   block: blkcg_poli...
4614
4615
4616
  #else
  	cfq_group_idle = 0;
  #endif
8bd435b30   Tejun Heo   blkcg: remove sta...
4617

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

3d3c2379f   Tejun Heo   block, cfq: move ...
4623
  	ret = elv_register(&iosched_cfq);
8bd435b30   Tejun Heo   blkcg: remove sta...
4624
4625
  	if (ret)
  		goto err_free_pool;
3d3c2379f   Tejun Heo   block, cfq: move ...
4626

2fdd82bd8   Adrian Bunk   block: let elv_re...
4627
  	return 0;
8bd435b30   Tejun Heo   blkcg: remove sta...
4628
4629
4630
4631
  
  err_free_pool:
  	kmem_cache_destroy(cfq_pool);
  err_pol_unreg:
ffea73fc7   Tejun Heo   block: blkcg_poli...
4632
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398e   Tejun Heo   blkcg: mass renam...
4633
  	blkcg_policy_unregister(&blkcg_policy_cfq);
ffea73fc7   Tejun Heo   block: blkcg_poli...
4634
  #endif
8bd435b30   Tejun Heo   blkcg: remove sta...
4635
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4636
4637
4638
4639
  }
  
  static void __exit cfq_exit(void)
  {
ffea73fc7   Tejun Heo   block: blkcg_poli...
4640
  #ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398e   Tejun Heo   blkcg: mass renam...
4641
  	blkcg_policy_unregister(&blkcg_policy_cfq);
ffea73fc7   Tejun Heo   block: blkcg_poli...
4642
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4643
  	elv_unregister(&iosched_cfq);
3d3c2379f   Tejun Heo   block, cfq: move ...
4644
  	kmem_cache_destroy(cfq_pool);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4645
4646
4647
4648
4649
4650
4651
4652
  }
  
  module_init(cfq_init);
  module_exit(cfq_exit);
  
  MODULE_AUTHOR("Jens Axboe");
  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");