blkio: Introduce per cfq group weights and vdisktime calculations
[linux-2.6-block.git] / block / cfq-iosched.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * CFQ, or complete fairness queueing, disk scheduler.
3 *
4 * Based on ideas from a previously unfinished io
5 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
6 *
0fe23479 7 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
1da177e4 8 */
1da177e4 9#include <linux/module.h>
1cc9be68
AV
10#include <linux/blkdev.h>
11#include <linux/elevator.h>
ad5ebd2f 12#include <linux/jiffies.h>
1da177e4 13#include <linux/rbtree.h>
22e2c507 14#include <linux/ioprio.h>
7b679138 15#include <linux/blktrace_api.h>
25bc6b07 16#include "blk-cgroup.h"
1da177e4
LT
17
18/*
19 * tunables
20 */
fe094d98
JA
21/* max queue in one round of service */
22static const int cfq_quantum = 4;
64100099 23static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
fe094d98
JA
24/* maximum backwards seek, in KiB */
25static const int cfq_back_max = 16 * 1024;
26/* penalty of a backwards seek */
27static const int cfq_back_penalty = 2;
64100099 28static const int cfq_slice_sync = HZ / 10;
3b18152c 29static int cfq_slice_async = HZ / 25;
64100099 30static const int cfq_slice_async_rq = 2;
caaa5f9f 31static int cfq_slice_idle = HZ / 125;
5db5d642
CZ
32static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
33static const int cfq_hist_divisor = 4;
22e2c507 34
d9e7620e 35/*
0871714e 36 * offset from end of service tree
d9e7620e 37 */
0871714e 38#define CFQ_IDLE_DELAY (HZ / 5)
d9e7620e
JA
39
40/*
41 * below this threshold, we consider thinktime immediate
42 */
43#define CFQ_MIN_TT (2)
44
e6c5bc73
JM
45/*
46 * Allow merged cfqqs to perform this amount of seeky I/O before
47 * deciding to break the queues up again.
48 */
49#define CFQQ_COOP_TOUT (HZ)
50
22e2c507 51#define CFQ_SLICE_SCALE (5)
45333d5a 52#define CFQ_HW_QUEUE_MIN (5)
25bc6b07 53#define CFQ_SERVICE_SHIFT 12
22e2c507 54
fe094d98
JA
55#define RQ_CIC(rq) \
56 ((struct cfq_io_context *) (rq)->elevator_private)
7b679138 57#define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elevator_private2)
1da177e4 58
e18b890b
CL
59static struct kmem_cache *cfq_pool;
60static struct kmem_cache *cfq_ioc_pool;
1da177e4 61
245b2e70 62static DEFINE_PER_CPU(unsigned long, cfq_ioc_count);
334e94de 63static struct completion *ioc_gone;
9a11b4ed 64static DEFINE_SPINLOCK(ioc_gone_lock);
334e94de 65
22e2c507
JA
66#define CFQ_PRIO_LISTS IOPRIO_BE_NR
67#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
22e2c507
JA
68#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
69
206dc69b 70#define sample_valid(samples) ((samples) > 80)
1fa8f6d6 71#define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
206dc69b 72
cc09e299
JA
73/*
74 * Most of our rbtree usage is for sorting with min extraction, so
75 * if we cache the leftmost node we don't have to walk down the tree
76 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
77 * move this into the elevator for the rq sorting as well.
78 */
79struct cfq_rb_root {
80 struct rb_root rb;
81 struct rb_node *left;
aa6f6a3d 82 unsigned count;
1fa8f6d6 83 u64 min_vdisktime;
25bc6b07 84 struct rb_node *active;
cc09e299 85};
1fa8f6d6 86#define CFQ_RB_ROOT (struct cfq_rb_root) { RB_ROOT, NULL, 0, 0, }
cc09e299 87
6118b70b
JA
88/*
89 * Per process-grouping structure
90 */
91struct cfq_queue {
92 /* reference count */
93 atomic_t ref;
94 /* various state flags, see below */
95 unsigned int flags;
96 /* parent cfq_data */
97 struct cfq_data *cfqd;
98 /* service_tree member */
99 struct rb_node rb_node;
100 /* service_tree key */
101 unsigned long rb_key;
102 /* prio tree member */
103 struct rb_node p_node;
104 /* prio tree root we belong to, if any */
105 struct rb_root *p_root;
106 /* sorted list of pending requests */
107 struct rb_root sort_list;
108 /* if fifo isn't expired, next request to serve */
109 struct request *next_rq;
110 /* requests queued in sort_list */
111 int queued[2];
112 /* currently allocated requests */
113 int allocated[2];
114 /* fifo list of requests in sort_list */
115 struct list_head fifo;
116
117 unsigned long slice_end;
118 long slice_resid;
119 unsigned int slice_dispatch;
120
121 /* pending metadata requests */
122 int meta_pending;
123 /* number of requests that are on the dispatch list or inside driver */
124 int dispatched;
125
126 /* io prio of this group */
127 unsigned short ioprio, org_ioprio;
128 unsigned short ioprio_class, org_ioprio_class;
129
b2c18e1e
JM
130 unsigned int seek_samples;
131 u64 seek_total;
132 sector_t seek_mean;
133 sector_t last_request_pos;
e6c5bc73 134 unsigned long seeky_start;
b2c18e1e 135
6118b70b 136 pid_t pid;
df5fe3e8 137
aa6f6a3d 138 struct cfq_rb_root *service_tree;
df5fe3e8 139 struct cfq_queue *new_cfqq;
cdb16e8f 140 struct cfq_group *cfqg;
6118b70b
JA
141};
142
c0324a02 143/*
718eee05 144 * First index in the service_trees.
c0324a02
CZ
145 * IDLE is handled separately, so it has negative index
146 */
147enum wl_prio_t {
c0324a02 148 BE_WORKLOAD = 0,
615f0259
VG
149 RT_WORKLOAD = 1,
150 IDLE_WORKLOAD = 2,
c0324a02
CZ
151};
152
718eee05
CZ
153/*
154 * Second index in the service_trees.
155 */
156enum wl_type_t {
157 ASYNC_WORKLOAD = 0,
158 SYNC_NOIDLE_WORKLOAD = 1,
159 SYNC_WORKLOAD = 2
160};
161
cdb16e8f
VG
162/* This is per cgroup per device grouping structure */
163struct cfq_group {
1fa8f6d6
VG
164 /* group service_tree member */
165 struct rb_node rb_node;
166
167 /* group service_tree key */
168 u64 vdisktime;
25bc6b07 169 unsigned int weight;
1fa8f6d6
VG
170 bool on_st;
171
172 /* number of cfqq currently on this group */
173 int nr_cfqq;
174
cdb16e8f
VG
175 /*
176 * rr lists of queues with requests, onle rr for each priority class.
177 * Counts are embedded in the cfq_rb_root
178 */
179 struct cfq_rb_root service_trees[2][3];
180 struct cfq_rb_root service_tree_idle;
181};
718eee05 182
22e2c507
JA
183/*
184 * Per block device queue structure
185 */
1da177e4 186struct cfq_data {
165125e1 187 struct request_queue *queue;
1fa8f6d6
VG
188 /* Root service tree for cfq_groups */
189 struct cfq_rb_root grp_service_tree;
cdb16e8f 190 struct cfq_group root_group;
22e2c507 191
c0324a02
CZ
192 /*
193 * The priority currently being served
22e2c507 194 */
c0324a02 195 enum wl_prio_t serving_prio;
718eee05
CZ
196 enum wl_type_t serving_type;
197 unsigned long workload_expires;
cdb16e8f 198 struct cfq_group *serving_group;
8e550632 199 bool noidle_tree_requires_idle;
a36e71f9
JA
200
201 /*
202 * Each priority tree is sorted by next_request position. These
203 * trees are used when determining if two or more queues are
204 * interleaving requests (see cfq_close_cooperator).
205 */
206 struct rb_root prio_trees[CFQ_PRIO_LISTS];
207
22e2c507 208 unsigned int busy_queues;
5db5d642 209 unsigned int busy_queues_avg[2];
22e2c507 210
5ad531db 211 int rq_in_driver[2];
3ed9a296 212 int sync_flight;
45333d5a
AC
213
214 /*
215 * queue-depth detection
216 */
217 int rq_queued;
25776e35 218 int hw_tag;
e459dd08
CZ
219 /*
220 * hw_tag can be
221 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
222 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
223 * 0 => no NCQ
224 */
225 int hw_tag_est_depth;
226 unsigned int hw_tag_samples;
1da177e4 227
22e2c507
JA
228 /*
229 * idle window management
230 */
231 struct timer_list idle_slice_timer;
23e018a1 232 struct work_struct unplug_work;
1da177e4 233
22e2c507
JA
234 struct cfq_queue *active_queue;
235 struct cfq_io_context *active_cic;
22e2c507 236
c2dea2d1
VT
237 /*
238 * async queue for each priority case
239 */
240 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
241 struct cfq_queue *async_idle_cfqq;
15c31be4 242
6d048f53 243 sector_t last_position;
1da177e4 244
1da177e4
LT
245 /*
246 * tunables, see top of file
247 */
248 unsigned int cfq_quantum;
22e2c507 249 unsigned int cfq_fifo_expire[2];
1da177e4
LT
250 unsigned int cfq_back_penalty;
251 unsigned int cfq_back_max;
22e2c507
JA
252 unsigned int cfq_slice[2];
253 unsigned int cfq_slice_async_rq;
254 unsigned int cfq_slice_idle;
963b72fc 255 unsigned int cfq_latency;
d9ff4187
AV
256
257 struct list_head cic_list;
1da177e4 258
6118b70b
JA
259 /*
260 * Fallback dummy cfqq for extreme OOM conditions
261 */
262 struct cfq_queue oom_cfqq;
365722bb
VG
263
264 unsigned long last_end_sync_rq;
1da177e4
LT
265};
266
cdb16e8f
VG
267static struct cfq_rb_root *service_tree_for(struct cfq_group *cfqg,
268 enum wl_prio_t prio,
718eee05 269 enum wl_type_t type,
c0324a02
CZ
270 struct cfq_data *cfqd)
271{
1fa8f6d6
VG
272 if (!cfqg)
273 return NULL;
274
c0324a02 275 if (prio == IDLE_WORKLOAD)
cdb16e8f 276 return &cfqg->service_tree_idle;
c0324a02 277
cdb16e8f 278 return &cfqg->service_trees[prio][type];
c0324a02
CZ
279}
280
3b18152c 281enum cfqq_state_flags {
b0b8d749
JA
282 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
283 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
b029195d 284 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
b0b8d749 285 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
b0b8d749
JA
286 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
287 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
288 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
44f7c160 289 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
91fac317 290 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
b3b6d040 291 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
76280aff 292 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
3b18152c
JA
293};
294
295#define CFQ_CFQQ_FNS(name) \
296static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
297{ \
fe094d98 298 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
3b18152c
JA
299} \
300static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
301{ \
fe094d98 302 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
3b18152c
JA
303} \
304static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
305{ \
fe094d98 306 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
3b18152c
JA
307}
308
309CFQ_CFQQ_FNS(on_rr);
310CFQ_CFQQ_FNS(wait_request);
b029195d 311CFQ_CFQQ_FNS(must_dispatch);
3b18152c 312CFQ_CFQQ_FNS(must_alloc_slice);
3b18152c
JA
313CFQ_CFQQ_FNS(fifo_expire);
314CFQ_CFQQ_FNS(idle_window);
315CFQ_CFQQ_FNS(prio_changed);
44f7c160 316CFQ_CFQQ_FNS(slice_new);
91fac317 317CFQ_CFQQ_FNS(sync);
a36e71f9 318CFQ_CFQQ_FNS(coop);
76280aff 319CFQ_CFQQ_FNS(deep);
3b18152c
JA
320#undef CFQ_CFQQ_FNS
321
7b679138
JA
322#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
323 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
324#define cfq_log(cfqd, fmt, args...) \
325 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
326
615f0259
VG
327/* Traverses through cfq group service trees */
328#define for_each_cfqg_st(cfqg, i, j, st) \
329 for (i = 0; i <= IDLE_WORKLOAD; i++) \
330 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
331 : &cfqg->service_tree_idle; \
332 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
333 (i == IDLE_WORKLOAD && j == 0); \
334 j++, st = i < IDLE_WORKLOAD ? \
335 &cfqg->service_trees[i][j]: NULL) \
336
337
c0324a02
CZ
338static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq)
339{
340 if (cfq_class_idle(cfqq))
341 return IDLE_WORKLOAD;
342 if (cfq_class_rt(cfqq))
343 return RT_WORKLOAD;
344 return BE_WORKLOAD;
345}
346
718eee05
CZ
347
348static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
349{
350 if (!cfq_cfqq_sync(cfqq))
351 return ASYNC_WORKLOAD;
352 if (!cfq_cfqq_idle_window(cfqq))
353 return SYNC_NOIDLE_WORKLOAD;
354 return SYNC_WORKLOAD;
355}
356
c0324a02
CZ
357static inline int cfq_busy_queues_wl(enum wl_prio_t wl, struct cfq_data *cfqd)
358{
cdb16e8f
VG
359 struct cfq_group *cfqg = &cfqd->root_group;
360
c0324a02 361 if (wl == IDLE_WORKLOAD)
cdb16e8f 362 return cfqg->service_tree_idle.count;
c0324a02 363
cdb16e8f
VG
364 return cfqg->service_trees[wl][ASYNC_WORKLOAD].count
365 + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count
366 + cfqg->service_trees[wl][SYNC_WORKLOAD].count;
c0324a02
CZ
367}
368
165125e1 369static void cfq_dispatch_insert(struct request_queue *, struct request *);
a6151c3a 370static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool,
fd0928df 371 struct io_context *, gfp_t);
4ac845a2 372static struct cfq_io_context *cfq_cic_lookup(struct cfq_data *,
91fac317
VT
373 struct io_context *);
374
5ad531db
JA
375static inline int rq_in_driver(struct cfq_data *cfqd)
376{
377 return cfqd->rq_in_driver[0] + cfqd->rq_in_driver[1];
378}
379
91fac317 380static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_context *cic,
a6151c3a 381 bool is_sync)
91fac317 382{
a6151c3a 383 return cic->cfqq[is_sync];
91fac317
VT
384}
385
386static inline void cic_set_cfqq(struct cfq_io_context *cic,
a6151c3a 387 struct cfq_queue *cfqq, bool is_sync)
91fac317 388{
a6151c3a 389 cic->cfqq[is_sync] = cfqq;
91fac317
VT
390}
391
392/*
393 * We regard a request as SYNC, if it's either a read or has the SYNC bit
394 * set (in which case it could also be direct WRITE).
395 */
a6151c3a 396static inline bool cfq_bio_sync(struct bio *bio)
91fac317 397{
a6151c3a 398 return bio_data_dir(bio) == READ || bio_rw_flagged(bio, BIO_RW_SYNCIO);
91fac317 399}
1da177e4 400
99f95e52
AM
401/*
402 * scheduler run of queue, if there are requests pending and no one in the
403 * driver that will restart queueing
404 */
23e018a1 405static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
99f95e52 406{
7b679138
JA
407 if (cfqd->busy_queues) {
408 cfq_log(cfqd, "schedule dispatch");
23e018a1 409 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
7b679138 410 }
99f95e52
AM
411}
412
165125e1 413static int cfq_queue_empty(struct request_queue *q)
99f95e52
AM
414{
415 struct cfq_data *cfqd = q->elevator->elevator_data;
416
f04a6424 417 return !cfqd->rq_queued;
99f95e52
AM
418}
419
44f7c160
JA
420/*
421 * Scale schedule slice based on io priority. Use the sync time slice only
422 * if a queue is marked sync and has sync io queued. A sync queue with async
423 * io only, should not get full sync slice length.
424 */
a6151c3a 425static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
d9e7620e 426 unsigned short prio)
44f7c160 427{
d9e7620e 428 const int base_slice = cfqd->cfq_slice[sync];
44f7c160 429
d9e7620e
JA
430 WARN_ON(prio >= IOPRIO_BE_NR);
431
432 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
433}
44f7c160 434
d9e7620e
JA
435static inline int
436cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
437{
438 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
44f7c160
JA
439}
440
25bc6b07
VG
441static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
442{
443 u64 d = delta << CFQ_SERVICE_SHIFT;
444
445 d = d * BLKIO_WEIGHT_DEFAULT;
446 do_div(d, cfqg->weight);
447 return d;
448}
449
450static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
451{
452 s64 delta = (s64)(vdisktime - min_vdisktime);
453 if (delta > 0)
454 min_vdisktime = vdisktime;
455
456 return min_vdisktime;
457}
458
459static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
460{
461 s64 delta = (s64)(vdisktime - min_vdisktime);
462 if (delta < 0)
463 min_vdisktime = vdisktime;
464
465 return min_vdisktime;
466}
467
468static void update_min_vdisktime(struct cfq_rb_root *st)
469{
470 u64 vdisktime = st->min_vdisktime;
471 struct cfq_group *cfqg;
472
473 if (st->active) {
474 cfqg = rb_entry_cfqg(st->active);
475 vdisktime = cfqg->vdisktime;
476 }
477
478 if (st->left) {
479 cfqg = rb_entry_cfqg(st->left);
480 vdisktime = min_vdisktime(vdisktime, cfqg->vdisktime);
481 }
482
483 st->min_vdisktime = max_vdisktime(st->min_vdisktime, vdisktime);
484}
485
5db5d642
CZ
486/*
487 * get averaged number of queues of RT/BE priority.
488 * average is updated, with a formula that gives more weight to higher numbers,
489 * to quickly follows sudden increases and decrease slowly
490 */
491
5869619c
JA
492static inline unsigned cfq_get_avg_queues(struct cfq_data *cfqd, bool rt)
493{
5db5d642
CZ
494 unsigned min_q, max_q;
495 unsigned mult = cfq_hist_divisor - 1;
496 unsigned round = cfq_hist_divisor / 2;
c0324a02 497 unsigned busy = cfq_busy_queues_wl(rt, cfqd);
5db5d642
CZ
498
499 min_q = min(cfqd->busy_queues_avg[rt], busy);
500 max_q = max(cfqd->busy_queues_avg[rt], busy);
501 cfqd->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
502 cfq_hist_divisor;
503 return cfqd->busy_queues_avg[rt];
504}
505
44f7c160
JA
506static inline void
507cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
508{
5db5d642
CZ
509 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
510 if (cfqd->cfq_latency) {
511 /* interested queues (we consider only the ones with the same
512 * priority class) */
513 unsigned iq = cfq_get_avg_queues(cfqd, cfq_class_rt(cfqq));
514 unsigned sync_slice = cfqd->cfq_slice[1];
515 unsigned expect_latency = sync_slice * iq;
516 if (expect_latency > cfq_target_latency) {
517 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
518 /* scale low_slice according to IO priority
519 * and sync vs async */
520 unsigned low_slice =
521 min(slice, base_low_slice * slice / sync_slice);
522 /* the adapted slice value is scaled to fit all iqs
523 * into the target latency */
524 slice = max(slice * cfq_target_latency / expect_latency,
525 low_slice);
526 }
527 }
528 cfqq->slice_end = jiffies + slice;
7b679138 529 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
44f7c160
JA
530}
531
532/*
533 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
534 * isn't valid until the first request from the dispatch is activated
535 * and the slice time set.
536 */
a6151c3a 537static inline bool cfq_slice_used(struct cfq_queue *cfqq)
44f7c160
JA
538{
539 if (cfq_cfqq_slice_new(cfqq))
540 return 0;
541 if (time_before(jiffies, cfqq->slice_end))
542 return 0;
543
544 return 1;
545}
546
1da177e4 547/*
5e705374 548 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
1da177e4 549 * We choose the request that is closest to the head right now. Distance
e8a99053 550 * behind the head is penalized and only allowed to a certain extent.
1da177e4 551 */
5e705374 552static struct request *
cf7c25cf 553cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
1da177e4 554{
cf7c25cf 555 sector_t s1, s2, d1 = 0, d2 = 0;
1da177e4 556 unsigned long back_max;
e8a99053
AM
557#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
558#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
559 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
1da177e4 560
5e705374
JA
561 if (rq1 == NULL || rq1 == rq2)
562 return rq2;
563 if (rq2 == NULL)
564 return rq1;
9c2c38a1 565
5e705374
JA
566 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
567 return rq1;
568 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
569 return rq2;
374f84ac
JA
570 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
571 return rq1;
572 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
573 return rq2;
1da177e4 574
83096ebf
TH
575 s1 = blk_rq_pos(rq1);
576 s2 = blk_rq_pos(rq2);
1da177e4 577
1da177e4
LT
578 /*
579 * by definition, 1KiB is 2 sectors
580 */
581 back_max = cfqd->cfq_back_max * 2;
582
583 /*
584 * Strict one way elevator _except_ in the case where we allow
585 * short backward seeks which are biased as twice the cost of a
586 * similar forward seek.
587 */
588 if (s1 >= last)
589 d1 = s1 - last;
590 else if (s1 + back_max >= last)
591 d1 = (last - s1) * cfqd->cfq_back_penalty;
592 else
e8a99053 593 wrap |= CFQ_RQ1_WRAP;
1da177e4
LT
594
595 if (s2 >= last)
596 d2 = s2 - last;
597 else if (s2 + back_max >= last)
598 d2 = (last - s2) * cfqd->cfq_back_penalty;
599 else
e8a99053 600 wrap |= CFQ_RQ2_WRAP;
1da177e4
LT
601
602 /* Found required data */
e8a99053
AM
603
604 /*
605 * By doing switch() on the bit mask "wrap" we avoid having to
606 * check two variables for all permutations: --> faster!
607 */
608 switch (wrap) {
5e705374 609 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
e8a99053 610 if (d1 < d2)
5e705374 611 return rq1;
e8a99053 612 else if (d2 < d1)
5e705374 613 return rq2;
e8a99053
AM
614 else {
615 if (s1 >= s2)
5e705374 616 return rq1;
e8a99053 617 else
5e705374 618 return rq2;
e8a99053 619 }
1da177e4 620
e8a99053 621 case CFQ_RQ2_WRAP:
5e705374 622 return rq1;
e8a99053 623 case CFQ_RQ1_WRAP:
5e705374
JA
624 return rq2;
625 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
e8a99053
AM
626 default:
627 /*
628 * Since both rqs are wrapped,
629 * start with the one that's further behind head
630 * (--> only *one* back seek required),
631 * since back seek takes more time than forward.
632 */
633 if (s1 <= s2)
5e705374 634 return rq1;
1da177e4 635 else
5e705374 636 return rq2;
1da177e4
LT
637 }
638}
639
498d3aa2
JA
640/*
641 * The below is leftmost cache rbtree addon
642 */
0871714e 643static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
cc09e299 644{
615f0259
VG
645 /* Service tree is empty */
646 if (!root->count)
647 return NULL;
648
cc09e299
JA
649 if (!root->left)
650 root->left = rb_first(&root->rb);
651
0871714e
JA
652 if (root->left)
653 return rb_entry(root->left, struct cfq_queue, rb_node);
654
655 return NULL;
cc09e299
JA
656}
657
1fa8f6d6
VG
658static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
659{
660 if (!root->left)
661 root->left = rb_first(&root->rb);
662
663 if (root->left)
664 return rb_entry_cfqg(root->left);
665
666 return NULL;
667}
668
a36e71f9
JA
669static void rb_erase_init(struct rb_node *n, struct rb_root *root)
670{
671 rb_erase(n, root);
672 RB_CLEAR_NODE(n);
673}
674
cc09e299
JA
675static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
676{
677 if (root->left == n)
678 root->left = NULL;
a36e71f9 679 rb_erase_init(n, &root->rb);
aa6f6a3d 680 --root->count;
cc09e299
JA
681}
682
1da177e4
LT
683/*
684 * would be nice to take fifo expire time into account as well
685 */
5e705374
JA
686static struct request *
687cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
688 struct request *last)
1da177e4 689{
21183b07
JA
690 struct rb_node *rbnext = rb_next(&last->rb_node);
691 struct rb_node *rbprev = rb_prev(&last->rb_node);
5e705374 692 struct request *next = NULL, *prev = NULL;
1da177e4 693
21183b07 694 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
1da177e4
LT
695
696 if (rbprev)
5e705374 697 prev = rb_entry_rq(rbprev);
1da177e4 698
21183b07 699 if (rbnext)
5e705374 700 next = rb_entry_rq(rbnext);
21183b07
JA
701 else {
702 rbnext = rb_first(&cfqq->sort_list);
703 if (rbnext && rbnext != &last->rb_node)
5e705374 704 next = rb_entry_rq(rbnext);
21183b07 705 }
1da177e4 706
cf7c25cf 707 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
1da177e4
LT
708}
709
d9e7620e
JA
710static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
711 struct cfq_queue *cfqq)
1da177e4 712{
d9e7620e
JA
713 /*
714 * just an approximation, should be ok.
715 */
cdb16e8f 716 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
464191c6 717 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
d9e7620e
JA
718}
719
1fa8f6d6
VG
720static inline s64
721cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
722{
723 return cfqg->vdisktime - st->min_vdisktime;
724}
725
726static void
727__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
728{
729 struct rb_node **node = &st->rb.rb_node;
730 struct rb_node *parent = NULL;
731 struct cfq_group *__cfqg;
732 s64 key = cfqg_key(st, cfqg);
733 int left = 1;
734
735 while (*node != NULL) {
736 parent = *node;
737 __cfqg = rb_entry_cfqg(parent);
738
739 if (key < cfqg_key(st, __cfqg))
740 node = &parent->rb_left;
741 else {
742 node = &parent->rb_right;
743 left = 0;
744 }
745 }
746
747 if (left)
748 st->left = &cfqg->rb_node;
749
750 rb_link_node(&cfqg->rb_node, parent, node);
751 rb_insert_color(&cfqg->rb_node, &st->rb);
752}
753
754static void
755cfq_group_service_tree_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
756{
757 struct cfq_rb_root *st = &cfqd->grp_service_tree;
758 struct cfq_group *__cfqg;
759 struct rb_node *n;
760
761 cfqg->nr_cfqq++;
762 if (cfqg->on_st)
763 return;
764
765 /*
766 * Currently put the group at the end. Later implement something
767 * so that groups get lesser vtime based on their weights, so that
768 * if group does not loose all if it was not continously backlogged.
769 */
770 n = rb_last(&st->rb);
771 if (n) {
772 __cfqg = rb_entry_cfqg(n);
773 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
774 } else
775 cfqg->vdisktime = st->min_vdisktime;
776
777 __cfq_group_service_tree_add(st, cfqg);
778 cfqg->on_st = true;
779}
780
781static void
782cfq_group_service_tree_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
783{
784 struct cfq_rb_root *st = &cfqd->grp_service_tree;
785
25bc6b07
VG
786 if (st->active == &cfqg->rb_node)
787 st->active = NULL;
788
1fa8f6d6
VG
789 BUG_ON(cfqg->nr_cfqq < 1);
790 cfqg->nr_cfqq--;
25bc6b07 791
1fa8f6d6
VG
792 /* If there are other cfq queues under this group, don't delete it */
793 if (cfqg->nr_cfqq)
794 return;
795
796 cfqg->on_st = false;
797 if (!RB_EMPTY_NODE(&cfqg->rb_node))
798 cfq_rb_erase(&cfqg->rb_node, st);
799}
800
498d3aa2 801/*
c0324a02 802 * The cfqd->service_trees holds all pending cfq_queue's that have
498d3aa2
JA
803 * requests waiting to be processed. It is sorted in the order that
804 * we will service the queues.
805 */
a36e71f9 806static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
a6151c3a 807 bool add_front)
d9e7620e 808{
0871714e
JA
809 struct rb_node **p, *parent;
810 struct cfq_queue *__cfqq;
d9e7620e 811 unsigned long rb_key;
c0324a02 812 struct cfq_rb_root *service_tree;
498d3aa2 813 int left;
d9e7620e 814
cdb16e8f
VG
815 service_tree = service_tree_for(cfqq->cfqg, cfqq_prio(cfqq),
816 cfqq_type(cfqq), cfqd);
0871714e
JA
817 if (cfq_class_idle(cfqq)) {
818 rb_key = CFQ_IDLE_DELAY;
aa6f6a3d 819 parent = rb_last(&service_tree->rb);
0871714e
JA
820 if (parent && parent != &cfqq->rb_node) {
821 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
822 rb_key += __cfqq->rb_key;
823 } else
824 rb_key += jiffies;
825 } else if (!add_front) {
b9c8946b
JA
826 /*
827 * Get our rb key offset. Subtract any residual slice
828 * value carried from last service. A negative resid
829 * count indicates slice overrun, and this should position
830 * the next service time further away in the tree.
831 */
edd75ffd 832 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
b9c8946b 833 rb_key -= cfqq->slice_resid;
edd75ffd 834 cfqq->slice_resid = 0;
48e025e6
CZ
835 } else {
836 rb_key = -HZ;
aa6f6a3d 837 __cfqq = cfq_rb_first(service_tree);
48e025e6
CZ
838 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
839 }
1da177e4 840
d9e7620e 841 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
99f9628a 842 /*
d9e7620e 843 * same position, nothing more to do
99f9628a 844 */
c0324a02
CZ
845 if (rb_key == cfqq->rb_key &&
846 cfqq->service_tree == service_tree)
d9e7620e 847 return;
1da177e4 848
aa6f6a3d
CZ
849 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
850 cfqq->service_tree = NULL;
1da177e4 851 }
d9e7620e 852
498d3aa2 853 left = 1;
0871714e 854 parent = NULL;
aa6f6a3d
CZ
855 cfqq->service_tree = service_tree;
856 p = &service_tree->rb.rb_node;
d9e7620e 857 while (*p) {
67060e37 858 struct rb_node **n;
cc09e299 859
d9e7620e
JA
860 parent = *p;
861 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
862
0c534e0a 863 /*
c0324a02 864 * sort by key, that represents service time.
0c534e0a 865 */
c0324a02 866 if (time_before(rb_key, __cfqq->rb_key))
67060e37 867 n = &(*p)->rb_left;
c0324a02 868 else {
67060e37 869 n = &(*p)->rb_right;
cc09e299 870 left = 0;
c0324a02 871 }
67060e37
JA
872
873 p = n;
d9e7620e
JA
874 }
875
cc09e299 876 if (left)
aa6f6a3d 877 service_tree->left = &cfqq->rb_node;
cc09e299 878
d9e7620e
JA
879 cfqq->rb_key = rb_key;
880 rb_link_node(&cfqq->rb_node, parent, p);
aa6f6a3d
CZ
881 rb_insert_color(&cfqq->rb_node, &service_tree->rb);
882 service_tree->count++;
1fa8f6d6 883 cfq_group_service_tree_add(cfqd, cfqq->cfqg);
1da177e4
LT
884}
885
a36e71f9 886static struct cfq_queue *
f2d1f0ae
JA
887cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
888 sector_t sector, struct rb_node **ret_parent,
889 struct rb_node ***rb_link)
a36e71f9 890{
a36e71f9
JA
891 struct rb_node **p, *parent;
892 struct cfq_queue *cfqq = NULL;
893
894 parent = NULL;
895 p = &root->rb_node;
896 while (*p) {
897 struct rb_node **n;
898
899 parent = *p;
900 cfqq = rb_entry(parent, struct cfq_queue, p_node);
901
902 /*
903 * Sort strictly based on sector. Smallest to the left,
904 * largest to the right.
905 */
2e46e8b2 906 if (sector > blk_rq_pos(cfqq->next_rq))
a36e71f9 907 n = &(*p)->rb_right;
2e46e8b2 908 else if (sector < blk_rq_pos(cfqq->next_rq))
a36e71f9
JA
909 n = &(*p)->rb_left;
910 else
911 break;
912 p = n;
3ac6c9f8 913 cfqq = NULL;
a36e71f9
JA
914 }
915
916 *ret_parent = parent;
917 if (rb_link)
918 *rb_link = p;
3ac6c9f8 919 return cfqq;
a36e71f9
JA
920}
921
922static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
923{
a36e71f9
JA
924 struct rb_node **p, *parent;
925 struct cfq_queue *__cfqq;
926
f2d1f0ae
JA
927 if (cfqq->p_root) {
928 rb_erase(&cfqq->p_node, cfqq->p_root);
929 cfqq->p_root = NULL;
930 }
a36e71f9
JA
931
932 if (cfq_class_idle(cfqq))
933 return;
934 if (!cfqq->next_rq)
935 return;
936
f2d1f0ae 937 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
2e46e8b2
TH
938 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
939 blk_rq_pos(cfqq->next_rq), &parent, &p);
3ac6c9f8
JA
940 if (!__cfqq) {
941 rb_link_node(&cfqq->p_node, parent, p);
f2d1f0ae
JA
942 rb_insert_color(&cfqq->p_node, cfqq->p_root);
943 } else
944 cfqq->p_root = NULL;
a36e71f9
JA
945}
946
498d3aa2
JA
947/*
948 * Update cfqq's position in the service tree.
949 */
edd75ffd 950static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
6d048f53 951{
6d048f53
JA
952 /*
953 * Resorting requires the cfqq to be on the RR list already.
954 */
a36e71f9 955 if (cfq_cfqq_on_rr(cfqq)) {
edd75ffd 956 cfq_service_tree_add(cfqd, cfqq, 0);
a36e71f9
JA
957 cfq_prio_tree_add(cfqd, cfqq);
958 }
6d048f53
JA
959}
960
1da177e4
LT
961/*
962 * add to busy list of queues for service, trying to be fair in ordering
22e2c507 963 * the pending list according to last request service
1da177e4 964 */
febffd61 965static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1da177e4 966{
7b679138 967 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
3b18152c
JA
968 BUG_ON(cfq_cfqq_on_rr(cfqq));
969 cfq_mark_cfqq_on_rr(cfqq);
1da177e4
LT
970 cfqd->busy_queues++;
971
edd75ffd 972 cfq_resort_rr_list(cfqd, cfqq);
1da177e4
LT
973}
974
498d3aa2
JA
975/*
976 * Called when the cfqq no longer has requests pending, remove it from
977 * the service tree.
978 */
febffd61 979static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1da177e4 980{
7b679138 981 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
3b18152c
JA
982 BUG_ON(!cfq_cfqq_on_rr(cfqq));
983 cfq_clear_cfqq_on_rr(cfqq);
1da177e4 984
aa6f6a3d
CZ
985 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
986 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
987 cfqq->service_tree = NULL;
988 }
f2d1f0ae
JA
989 if (cfqq->p_root) {
990 rb_erase(&cfqq->p_node, cfqq->p_root);
991 cfqq->p_root = NULL;
992 }
d9e7620e 993
1fa8f6d6 994 cfq_group_service_tree_del(cfqd, cfqq->cfqg);
1da177e4
LT
995 BUG_ON(!cfqd->busy_queues);
996 cfqd->busy_queues--;
997}
998
999/*
1000 * rb tree support functions
1001 */
febffd61 1002static void cfq_del_rq_rb(struct request *rq)
1da177e4 1003{
5e705374 1004 struct cfq_queue *cfqq = RQ_CFQQ(rq);
5e705374 1005 const int sync = rq_is_sync(rq);
1da177e4 1006
b4878f24
JA
1007 BUG_ON(!cfqq->queued[sync]);
1008 cfqq->queued[sync]--;
1da177e4 1009
5e705374 1010 elv_rb_del(&cfqq->sort_list, rq);
1da177e4 1011
f04a6424
VG
1012 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
1013 /*
1014 * Queue will be deleted from service tree when we actually
1015 * expire it later. Right now just remove it from prio tree
1016 * as it is empty.
1017 */
1018 if (cfqq->p_root) {
1019 rb_erase(&cfqq->p_node, cfqq->p_root);
1020 cfqq->p_root = NULL;
1021 }
1022 }
1da177e4
LT
1023}
1024
5e705374 1025static void cfq_add_rq_rb(struct request *rq)
1da177e4 1026{
5e705374 1027 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1da177e4 1028 struct cfq_data *cfqd = cfqq->cfqd;
a36e71f9 1029 struct request *__alias, *prev;
1da177e4 1030
5380a101 1031 cfqq->queued[rq_is_sync(rq)]++;
1da177e4
LT
1032
1033 /*
1034 * looks a little odd, but the first insert might return an alias.
1035 * if that happens, put the alias on the dispatch list
1036 */
21183b07 1037 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
5e705374 1038 cfq_dispatch_insert(cfqd->queue, __alias);
5fccbf61
JA
1039
1040 if (!cfq_cfqq_on_rr(cfqq))
1041 cfq_add_cfqq_rr(cfqd, cfqq);
5044eed4
JA
1042
1043 /*
1044 * check if this request is a better next-serve candidate
1045 */
a36e71f9 1046 prev = cfqq->next_rq;
cf7c25cf 1047 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
a36e71f9
JA
1048
1049 /*
1050 * adjust priority tree position, if ->next_rq changes
1051 */
1052 if (prev != cfqq->next_rq)
1053 cfq_prio_tree_add(cfqd, cfqq);
1054
5044eed4 1055 BUG_ON(!cfqq->next_rq);
1da177e4
LT
1056}
1057
febffd61 1058static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
1da177e4 1059{
5380a101
JA
1060 elv_rb_del(&cfqq->sort_list, rq);
1061 cfqq->queued[rq_is_sync(rq)]--;
5e705374 1062 cfq_add_rq_rb(rq);
1da177e4
LT
1063}
1064
206dc69b
JA
1065static struct request *
1066cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
1da177e4 1067{
206dc69b 1068 struct task_struct *tsk = current;
91fac317 1069 struct cfq_io_context *cic;
206dc69b 1070 struct cfq_queue *cfqq;
1da177e4 1071
4ac845a2 1072 cic = cfq_cic_lookup(cfqd, tsk->io_context);
91fac317
VT
1073 if (!cic)
1074 return NULL;
1075
1076 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
89850f7e
JA
1077 if (cfqq) {
1078 sector_t sector = bio->bi_sector + bio_sectors(bio);
1079
21183b07 1080 return elv_rb_find(&cfqq->sort_list, sector);
89850f7e 1081 }
1da177e4 1082
1da177e4
LT
1083 return NULL;
1084}
1085
165125e1 1086static void cfq_activate_request(struct request_queue *q, struct request *rq)
1da177e4 1087{
22e2c507 1088 struct cfq_data *cfqd = q->elevator->elevator_data;
3b18152c 1089
5ad531db 1090 cfqd->rq_in_driver[rq_is_sync(rq)]++;
7b679138 1091 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
5ad531db 1092 rq_in_driver(cfqd));
25776e35 1093
5b93629b 1094 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
1da177e4
LT
1095}
1096
165125e1 1097static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
1da177e4 1098{
b4878f24 1099 struct cfq_data *cfqd = q->elevator->elevator_data;
5ad531db 1100 const int sync = rq_is_sync(rq);
b4878f24 1101
5ad531db
JA
1102 WARN_ON(!cfqd->rq_in_driver[sync]);
1103 cfqd->rq_in_driver[sync]--;
7b679138 1104 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
5ad531db 1105 rq_in_driver(cfqd));
1da177e4
LT
1106}
1107
b4878f24 1108static void cfq_remove_request(struct request *rq)
1da177e4 1109{
5e705374 1110 struct cfq_queue *cfqq = RQ_CFQQ(rq);
21183b07 1111
5e705374
JA
1112 if (cfqq->next_rq == rq)
1113 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
1da177e4 1114
b4878f24 1115 list_del_init(&rq->queuelist);
5e705374 1116 cfq_del_rq_rb(rq);
374f84ac 1117
45333d5a 1118 cfqq->cfqd->rq_queued--;
374f84ac
JA
1119 if (rq_is_meta(rq)) {
1120 WARN_ON(!cfqq->meta_pending);
1121 cfqq->meta_pending--;
1122 }
1da177e4
LT
1123}
1124
165125e1
JA
1125static int cfq_merge(struct request_queue *q, struct request **req,
1126 struct bio *bio)
1da177e4
LT
1127{
1128 struct cfq_data *cfqd = q->elevator->elevator_data;
1129 struct request *__rq;
1da177e4 1130
206dc69b 1131 __rq = cfq_find_rq_fmerge(cfqd, bio);
22e2c507 1132 if (__rq && elv_rq_merge_ok(__rq, bio)) {
9817064b
JA
1133 *req = __rq;
1134 return ELEVATOR_FRONT_MERGE;
1da177e4
LT
1135 }
1136
1137 return ELEVATOR_NO_MERGE;
1da177e4
LT
1138}
1139
165125e1 1140static void cfq_merged_request(struct request_queue *q, struct request *req,
21183b07 1141 int type)
1da177e4 1142{
21183b07 1143 if (type == ELEVATOR_FRONT_MERGE) {
5e705374 1144 struct cfq_queue *cfqq = RQ_CFQQ(req);
1da177e4 1145
5e705374 1146 cfq_reposition_rq_rb(cfqq, req);
1da177e4 1147 }
1da177e4
LT
1148}
1149
1150static void
165125e1 1151cfq_merged_requests(struct request_queue *q, struct request *rq,
1da177e4
LT
1152 struct request *next)
1153{
cf7c25cf 1154 struct cfq_queue *cfqq = RQ_CFQQ(rq);
22e2c507
JA
1155 /*
1156 * reposition in fifo if next is older than rq
1157 */
1158 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
30996f40 1159 time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
22e2c507 1160 list_move(&rq->queuelist, &next->queuelist);
30996f40
JA
1161 rq_set_fifo_time(rq, rq_fifo_time(next));
1162 }
22e2c507 1163
cf7c25cf
CZ
1164 if (cfqq->next_rq == next)
1165 cfqq->next_rq = rq;
b4878f24 1166 cfq_remove_request(next);
22e2c507
JA
1167}
1168
165125e1 1169static int cfq_allow_merge(struct request_queue *q, struct request *rq,
da775265
JA
1170 struct bio *bio)
1171{
1172 struct cfq_data *cfqd = q->elevator->elevator_data;
91fac317 1173 struct cfq_io_context *cic;
da775265 1174 struct cfq_queue *cfqq;
da775265
JA
1175
1176 /*
ec8acb69 1177 * Disallow merge of a sync bio into an async request.
da775265 1178 */
91fac317 1179 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
a6151c3a 1180 return false;
da775265
JA
1181
1182 /*
719d3402
JA
1183 * Lookup the cfqq that this bio will be queued with. Allow
1184 * merge only if rq is queued there.
da775265 1185 */
4ac845a2 1186 cic = cfq_cic_lookup(cfqd, current->io_context);
91fac317 1187 if (!cic)
a6151c3a 1188 return false;
719d3402 1189
91fac317 1190 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
a6151c3a 1191 return cfqq == RQ_CFQQ(rq);
da775265
JA
1192}
1193
febffd61
JA
1194static void __cfq_set_active_queue(struct cfq_data *cfqd,
1195 struct cfq_queue *cfqq)
22e2c507
JA
1196{
1197 if (cfqq) {
7b679138 1198 cfq_log_cfqq(cfqd, cfqq, "set_active");
22e2c507 1199 cfqq->slice_end = 0;
2f5cb738
JA
1200 cfqq->slice_dispatch = 0;
1201
2f5cb738 1202 cfq_clear_cfqq_wait_request(cfqq);
b029195d 1203 cfq_clear_cfqq_must_dispatch(cfqq);
3b18152c
JA
1204 cfq_clear_cfqq_must_alloc_slice(cfqq);
1205 cfq_clear_cfqq_fifo_expire(cfqq);
44f7c160 1206 cfq_mark_cfqq_slice_new(cfqq);
2f5cb738
JA
1207
1208 del_timer(&cfqd->idle_slice_timer);
22e2c507
JA
1209 }
1210
1211 cfqd->active_queue = cfqq;
1212}
1213
7b14e3b5
JA
1214/*
1215 * current cfqq expired its slice (or was too idle), select new one
1216 */
1217static void
1218__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
a6151c3a 1219 bool timed_out)
7b14e3b5 1220{
7b679138
JA
1221 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
1222
7b14e3b5
JA
1223 if (cfq_cfqq_wait_request(cfqq))
1224 del_timer(&cfqd->idle_slice_timer);
1225
7b14e3b5
JA
1226 cfq_clear_cfqq_wait_request(cfqq);
1227
1228 /*
6084cdda 1229 * store what was left of this slice, if the queue idled/timed out
7b14e3b5 1230 */
7b679138 1231 if (timed_out && !cfq_cfqq_slice_new(cfqq)) {
c5b680f3 1232 cfqq->slice_resid = cfqq->slice_end - jiffies;
7b679138
JA
1233 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
1234 }
7b14e3b5 1235
f04a6424
VG
1236 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
1237 cfq_del_cfqq_rr(cfqd, cfqq);
1238
edd75ffd 1239 cfq_resort_rr_list(cfqd, cfqq);
7b14e3b5
JA
1240
1241 if (cfqq == cfqd->active_queue)
1242 cfqd->active_queue = NULL;
1243
1244 if (cfqd->active_cic) {
1245 put_io_context(cfqd->active_cic->ioc);
1246 cfqd->active_cic = NULL;
1247 }
7b14e3b5
JA
1248}
1249
a6151c3a 1250static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
7b14e3b5
JA
1251{
1252 struct cfq_queue *cfqq = cfqd->active_queue;
1253
1254 if (cfqq)
6084cdda 1255 __cfq_slice_expired(cfqd, cfqq, timed_out);
7b14e3b5
JA
1256}
1257
498d3aa2
JA
1258/*
1259 * Get next queue for service. Unless we have a queue preemption,
1260 * we'll simply select the first cfqq in the service tree.
1261 */
6d048f53 1262static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
22e2c507 1263{
c0324a02 1264 struct cfq_rb_root *service_tree =
cdb16e8f
VG
1265 service_tree_for(cfqd->serving_group, cfqd->serving_prio,
1266 cfqd->serving_type, cfqd);
d9e7620e 1267
f04a6424
VG
1268 if (!cfqd->rq_queued)
1269 return NULL;
1270
1fa8f6d6
VG
1271 /* There is nothing to dispatch */
1272 if (!service_tree)
1273 return NULL;
c0324a02
CZ
1274 if (RB_EMPTY_ROOT(&service_tree->rb))
1275 return NULL;
1276 return cfq_rb_first(service_tree);
6d048f53
JA
1277}
1278
f04a6424
VG
1279static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
1280{
1281 struct cfq_group *cfqg = &cfqd->root_group;
1282 struct cfq_queue *cfqq;
1283 int i, j;
1284 struct cfq_rb_root *st;
1285
1286 if (!cfqd->rq_queued)
1287 return NULL;
1288
1289 for_each_cfqg_st(cfqg, i, j, st)
1290 if ((cfqq = cfq_rb_first(st)) != NULL)
1291 return cfqq;
1292 return NULL;
1293}
1294
498d3aa2
JA
1295/*
1296 * Get and set a new active queue for service.
1297 */
a36e71f9
JA
1298static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
1299 struct cfq_queue *cfqq)
6d048f53 1300{
e00ef799 1301 if (!cfqq)
a36e71f9 1302 cfqq = cfq_get_next_queue(cfqd);
6d048f53 1303
22e2c507 1304 __cfq_set_active_queue(cfqd, cfqq);
3b18152c 1305 return cfqq;
22e2c507
JA
1306}
1307
d9e7620e
JA
1308static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
1309 struct request *rq)
1310{
83096ebf
TH
1311 if (blk_rq_pos(rq) >= cfqd->last_position)
1312 return blk_rq_pos(rq) - cfqd->last_position;
d9e7620e 1313 else
83096ebf 1314 return cfqd->last_position - blk_rq_pos(rq);
d9e7620e
JA
1315}
1316
b2c18e1e
JM
1317#define CFQQ_SEEK_THR 8 * 1024
1318#define CFQQ_SEEKY(cfqq) ((cfqq)->seek_mean > CFQQ_SEEK_THR)
04dc6e71 1319
b2c18e1e
JM
1320static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1321 struct request *rq)
6d048f53 1322{
b2c18e1e 1323 sector_t sdist = cfqq->seek_mean;
6d048f53 1324
b2c18e1e
JM
1325 if (!sample_valid(cfqq->seek_samples))
1326 sdist = CFQQ_SEEK_THR;
6d048f53 1327
04dc6e71 1328 return cfq_dist_from_last(cfqd, rq) <= sdist;
6d048f53
JA
1329}
1330
a36e71f9
JA
1331static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
1332 struct cfq_queue *cur_cfqq)
1333{
f2d1f0ae 1334 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
a36e71f9
JA
1335 struct rb_node *parent, *node;
1336 struct cfq_queue *__cfqq;
1337 sector_t sector = cfqd->last_position;
1338
1339 if (RB_EMPTY_ROOT(root))
1340 return NULL;
1341
1342 /*
1343 * First, if we find a request starting at the end of the last
1344 * request, choose it.
1345 */
f2d1f0ae 1346 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
a36e71f9
JA
1347 if (__cfqq)
1348 return __cfqq;
1349
1350 /*
1351 * If the exact sector wasn't found, the parent of the NULL leaf
1352 * will contain the closest sector.
1353 */
1354 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
b2c18e1e 1355 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
a36e71f9
JA
1356 return __cfqq;
1357
2e46e8b2 1358 if (blk_rq_pos(__cfqq->next_rq) < sector)
a36e71f9
JA
1359 node = rb_next(&__cfqq->p_node);
1360 else
1361 node = rb_prev(&__cfqq->p_node);
1362 if (!node)
1363 return NULL;
1364
1365 __cfqq = rb_entry(node, struct cfq_queue, p_node);
b2c18e1e 1366 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
a36e71f9
JA
1367 return __cfqq;
1368
1369 return NULL;
1370}
1371
1372/*
1373 * cfqd - obvious
1374 * cur_cfqq - passed in so that we don't decide that the current queue is
1375 * closely cooperating with itself.
1376 *
1377 * So, basically we're assuming that that cur_cfqq has dispatched at least
1378 * one request, and that cfqd->last_position reflects a position on the disk
1379 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
1380 * assumption.
1381 */
1382static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
b3b6d040 1383 struct cfq_queue *cur_cfqq)
6d048f53 1384{
a36e71f9
JA
1385 struct cfq_queue *cfqq;
1386
e6c5bc73
JM
1387 if (!cfq_cfqq_sync(cur_cfqq))
1388 return NULL;
1389 if (CFQQ_SEEKY(cur_cfqq))
1390 return NULL;
1391
6d048f53 1392 /*
d9e7620e
JA
1393 * We should notice if some of the queues are cooperating, eg
1394 * working closely on the same area of the disk. In that case,
1395 * we can group them together and don't waste time idling.
6d048f53 1396 */
a36e71f9
JA
1397 cfqq = cfqq_close(cfqd, cur_cfqq);
1398 if (!cfqq)
1399 return NULL;
1400
df5fe3e8
JM
1401 /*
1402 * It only makes sense to merge sync queues.
1403 */
1404 if (!cfq_cfqq_sync(cfqq))
1405 return NULL;
e6c5bc73
JM
1406 if (CFQQ_SEEKY(cfqq))
1407 return NULL;
df5fe3e8 1408
c0324a02
CZ
1409 /*
1410 * Do not merge queues of different priority classes
1411 */
1412 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
1413 return NULL;
1414
a36e71f9 1415 return cfqq;
6d048f53
JA
1416}
1417
a6d44e98
CZ
1418/*
1419 * Determine whether we should enforce idle window for this queue.
1420 */
1421
1422static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1423{
1424 enum wl_prio_t prio = cfqq_prio(cfqq);
718eee05 1425 struct cfq_rb_root *service_tree = cfqq->service_tree;
a6d44e98 1426
f04a6424
VG
1427 BUG_ON(!service_tree);
1428 BUG_ON(!service_tree->count);
1429
a6d44e98
CZ
1430 /* We never do for idle class queues. */
1431 if (prio == IDLE_WORKLOAD)
1432 return false;
1433
1434 /* We do for queues that were marked with idle window flag. */
1435 if (cfq_cfqq_idle_window(cfqq))
1436 return true;
1437
1438 /*
1439 * Otherwise, we do only if they are the last ones
1440 * in their service tree.
1441 */
f04a6424 1442 return service_tree->count == 1;
a6d44e98
CZ
1443}
1444
6d048f53 1445static void cfq_arm_slice_timer(struct cfq_data *cfqd)
22e2c507 1446{
1792669c 1447 struct cfq_queue *cfqq = cfqd->active_queue;
206dc69b 1448 struct cfq_io_context *cic;
7b14e3b5
JA
1449 unsigned long sl;
1450
a68bbddb 1451 /*
f7d7b7a7
JA
1452 * SSD device without seek penalty, disable idling. But only do so
1453 * for devices that support queuing, otherwise we still have a problem
1454 * with sync vs async workloads.
a68bbddb 1455 */
f7d7b7a7 1456 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
a68bbddb
JA
1457 return;
1458
dd67d051 1459 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
6d048f53 1460 WARN_ON(cfq_cfqq_slice_new(cfqq));
22e2c507
JA
1461
1462 /*
1463 * idle is disabled, either manually or by past process history
1464 */
a6d44e98 1465 if (!cfqd->cfq_slice_idle || !cfq_should_idle(cfqd, cfqq))
6d048f53
JA
1466 return;
1467
7b679138 1468 /*
8e550632 1469 * still active requests from this queue, don't idle
7b679138 1470 */
8e550632 1471 if (cfqq->dispatched)
7b679138
JA
1472 return;
1473
22e2c507
JA
1474 /*
1475 * task has exited, don't wait
1476 */
206dc69b 1477 cic = cfqd->active_cic;
66dac98e 1478 if (!cic || !atomic_read(&cic->ioc->nr_tasks))
6d048f53
JA
1479 return;
1480
355b659c
CZ
1481 /*
1482 * If our average think time is larger than the remaining time
1483 * slice, then don't idle. This avoids overrunning the allotted
1484 * time slice.
1485 */
1486 if (sample_valid(cic->ttime_samples) &&
1487 (cfqq->slice_end - jiffies < cic->ttime_mean))
1488 return;
1489
3b18152c 1490 cfq_mark_cfqq_wait_request(cfqq);
22e2c507 1491
6d048f53 1492 sl = cfqd->cfq_slice_idle;
206dc69b 1493
7b14e3b5 1494 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
9481ffdc 1495 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu", sl);
1da177e4
LT
1496}
1497
498d3aa2
JA
1498/*
1499 * Move request from internal lists to the request queue dispatch list.
1500 */
165125e1 1501static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
1da177e4 1502{
3ed9a296 1503 struct cfq_data *cfqd = q->elevator->elevator_data;
5e705374 1504 struct cfq_queue *cfqq = RQ_CFQQ(rq);
22e2c507 1505
7b679138
JA
1506 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
1507
06d21886 1508 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
5380a101 1509 cfq_remove_request(rq);
6d048f53 1510 cfqq->dispatched++;
5380a101 1511 elv_dispatch_sort(q, rq);
3ed9a296
JA
1512
1513 if (cfq_cfqq_sync(cfqq))
1514 cfqd->sync_flight++;
1da177e4
LT
1515}
1516
1517/*
1518 * return expired entry, or NULL to just start from scratch in rbtree
1519 */
febffd61 1520static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
1da177e4 1521{
30996f40 1522 struct request *rq = NULL;
1da177e4 1523
3b18152c 1524 if (cfq_cfqq_fifo_expire(cfqq))
1da177e4 1525 return NULL;
cb887411
JA
1526
1527 cfq_mark_cfqq_fifo_expire(cfqq);
1528
89850f7e
JA
1529 if (list_empty(&cfqq->fifo))
1530 return NULL;
1da177e4 1531
89850f7e 1532 rq = rq_entry_fifo(cfqq->fifo.next);
30996f40 1533 if (time_before(jiffies, rq_fifo_time(rq)))
7b679138 1534 rq = NULL;
1da177e4 1535
30996f40 1536 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
6d048f53 1537 return rq;
1da177e4
LT
1538}
1539
22e2c507
JA
1540static inline int
1541cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1542{
1543 const int base_rq = cfqd->cfq_slice_async_rq;
1da177e4 1544
22e2c507 1545 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1da177e4 1546
22e2c507 1547 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1da177e4
LT
1548}
1549
df5fe3e8
JM
1550/*
1551 * Must be called with the queue_lock held.
1552 */
1553static int cfqq_process_refs(struct cfq_queue *cfqq)
1554{
1555 int process_refs, io_refs;
1556
1557 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
1558 process_refs = atomic_read(&cfqq->ref) - io_refs;
1559 BUG_ON(process_refs < 0);
1560 return process_refs;
1561}
1562
1563static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
1564{
e6c5bc73 1565 int process_refs, new_process_refs;
df5fe3e8
JM
1566 struct cfq_queue *__cfqq;
1567
1568 /* Avoid a circular list and skip interim queue merges */
1569 while ((__cfqq = new_cfqq->new_cfqq)) {
1570 if (__cfqq == cfqq)
1571 return;
1572 new_cfqq = __cfqq;
1573 }
1574
1575 process_refs = cfqq_process_refs(cfqq);
1576 /*
1577 * If the process for the cfqq has gone away, there is no
1578 * sense in merging the queues.
1579 */
1580 if (process_refs == 0)
1581 return;
1582
e6c5bc73
JM
1583 /*
1584 * Merge in the direction of the lesser amount of work.
1585 */
1586 new_process_refs = cfqq_process_refs(new_cfqq);
1587 if (new_process_refs >= process_refs) {
1588 cfqq->new_cfqq = new_cfqq;
1589 atomic_add(process_refs, &new_cfqq->ref);
1590 } else {
1591 new_cfqq->new_cfqq = cfqq;
1592 atomic_add(new_process_refs, &cfqq->ref);
1593 }
df5fe3e8
JM
1594}
1595
cdb16e8f
VG
1596static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd,
1597 struct cfq_group *cfqg, enum wl_prio_t prio,
1598 bool prio_changed)
718eee05
CZ
1599{
1600 struct cfq_queue *queue;
1601 int i;
1602 bool key_valid = false;
1603 unsigned long lowest_key = 0;
1604 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
1605
1606 if (prio_changed) {
1607 /*
1608 * When priorities switched, we prefer starting
1609 * from SYNC_NOIDLE (first choice), or just SYNC
1610 * over ASYNC
1611 */
cdb16e8f 1612 if (service_tree_for(cfqg, prio, cur_best, cfqd)->count)
718eee05
CZ
1613 return cur_best;
1614 cur_best = SYNC_WORKLOAD;
cdb16e8f 1615 if (service_tree_for(cfqg, prio, cur_best, cfqd)->count)
718eee05
CZ
1616 return cur_best;
1617
1618 return ASYNC_WORKLOAD;
1619 }
1620
1621 for (i = 0; i < 3; ++i) {
1622 /* otherwise, select the one with lowest rb_key */
cdb16e8f 1623 queue = cfq_rb_first(service_tree_for(cfqg, prio, i, cfqd));
718eee05
CZ
1624 if (queue &&
1625 (!key_valid || time_before(queue->rb_key, lowest_key))) {
1626 lowest_key = queue->rb_key;
1627 cur_best = i;
1628 key_valid = true;
1629 }
1630 }
1631
1632 return cur_best;
1633}
1634
cdb16e8f 1635static void choose_service_tree(struct cfq_data *cfqd, struct cfq_group *cfqg)
718eee05
CZ
1636{
1637 enum wl_prio_t previous_prio = cfqd->serving_prio;
1638 bool prio_changed;
1639 unsigned slice;
1640 unsigned count;
cdb16e8f 1641 struct cfq_rb_root *st;
718eee05 1642
1fa8f6d6
VG
1643 if (!cfqg) {
1644 cfqd->serving_prio = IDLE_WORKLOAD;
1645 cfqd->workload_expires = jiffies + 1;
1646 return;
1647 }
1648
718eee05
CZ
1649 /* Choose next priority. RT > BE > IDLE */
1650 if (cfq_busy_queues_wl(RT_WORKLOAD, cfqd))
1651 cfqd->serving_prio = RT_WORKLOAD;
1652 else if (cfq_busy_queues_wl(BE_WORKLOAD, cfqd))
1653 cfqd->serving_prio = BE_WORKLOAD;
1654 else {
1655 cfqd->serving_prio = IDLE_WORKLOAD;
1656 cfqd->workload_expires = jiffies + 1;
1657 return;
1658 }
1659
1660 /*
1661 * For RT and BE, we have to choose also the type
1662 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
1663 * expiration time
1664 */
1665 prio_changed = (cfqd->serving_prio != previous_prio);
cdb16e8f
VG
1666 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type,
1667 cfqd);
1668 count = st->count;
718eee05
CZ
1669
1670 /*
1671 * If priority didn't change, check workload expiration,
1672 * and that we still have other queues ready
1673 */
1674 if (!prio_changed && count &&
1675 !time_after(jiffies, cfqd->workload_expires))
1676 return;
1677
1678 /* otherwise select new workload type */
1679 cfqd->serving_type =
cdb16e8f
VG
1680 cfq_choose_wl(cfqd, cfqg, cfqd->serving_prio, prio_changed);
1681 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type,
1682 cfqd);
1683 count = st->count;
718eee05
CZ
1684
1685 /*
1686 * the workload slice is computed as a fraction of target latency
1687 * proportional to the number of queues in that workload, over
1688 * all the queues in the same priority class
1689 */
1690 slice = cfq_target_latency * count /
1691 max_t(unsigned, cfqd->busy_queues_avg[cfqd->serving_prio],
1692 cfq_busy_queues_wl(cfqd->serving_prio, cfqd));
1693
1694 if (cfqd->serving_type == ASYNC_WORKLOAD)
1695 /* async workload slice is scaled down according to
1696 * the sync/async slice ratio. */
1697 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
1698 else
1699 /* sync workload slice is at least 2 * cfq_slice_idle */
1700 slice = max(slice, 2 * cfqd->cfq_slice_idle);
1701
1702 slice = max_t(unsigned, slice, CFQ_MIN_TT);
1703 cfqd->workload_expires = jiffies + slice;
8e550632 1704 cfqd->noidle_tree_requires_idle = false;
718eee05
CZ
1705}
1706
1fa8f6d6
VG
1707static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
1708{
1709 struct cfq_rb_root *st = &cfqd->grp_service_tree;
25bc6b07 1710 struct cfq_group *cfqg;
1fa8f6d6
VG
1711
1712 if (RB_EMPTY_ROOT(&st->rb))
1713 return NULL;
25bc6b07
VG
1714 cfqg = cfq_rb_first_group(st);
1715 st->active = &cfqg->rb_node;
1716 update_min_vdisktime(st);
1717 return cfqg;
1fa8f6d6
VG
1718}
1719
cdb16e8f
VG
1720static void cfq_choose_cfqg(struct cfq_data *cfqd)
1721{
1fa8f6d6
VG
1722 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
1723
1724 cfqd->serving_group = cfqg;
1725 choose_service_tree(cfqd, cfqg);
cdb16e8f
VG
1726}
1727
22e2c507 1728/*
498d3aa2
JA
1729 * Select a queue for service. If we have a current active queue,
1730 * check whether to continue servicing it, or retrieve and set a new one.
22e2c507 1731 */
1b5ed5e1 1732static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
1da177e4 1733{
a36e71f9 1734 struct cfq_queue *cfqq, *new_cfqq = NULL;
1da177e4 1735
22e2c507
JA
1736 cfqq = cfqd->active_queue;
1737 if (!cfqq)
1738 goto new_queue;
1da177e4 1739
f04a6424
VG
1740 if (!cfqd->rq_queued)
1741 return NULL;
22e2c507 1742 /*
6d048f53 1743 * The active queue has run out of time, expire it and select new.
22e2c507 1744 */
b029195d 1745 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
3b18152c 1746 goto expire;
1da177e4 1747
22e2c507 1748 /*
6d048f53
JA
1749 * The active queue has requests and isn't expired, allow it to
1750 * dispatch.
22e2c507 1751 */
dd67d051 1752 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
22e2c507 1753 goto keep_queue;
6d048f53 1754
a36e71f9
JA
1755 /*
1756 * If another queue has a request waiting within our mean seek
1757 * distance, let it run. The expire code will check for close
1758 * cooperators and put the close queue at the front of the service
df5fe3e8 1759 * tree. If possible, merge the expiring queue with the new cfqq.
a36e71f9 1760 */
b3b6d040 1761 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
df5fe3e8
JM
1762 if (new_cfqq) {
1763 if (!cfqq->new_cfqq)
1764 cfq_setup_merge(cfqq, new_cfqq);
a36e71f9 1765 goto expire;
df5fe3e8 1766 }
a36e71f9 1767
6d048f53
JA
1768 /*
1769 * No requests pending. If the active queue still has requests in
1770 * flight or is idling for a new request, allow either of these
1771 * conditions to happen (or time out) before selecting a new queue.
1772 */
cc197479 1773 if (timer_pending(&cfqd->idle_slice_timer) ||
a6d44e98 1774 (cfqq->dispatched && cfq_should_idle(cfqd, cfqq))) {
caaa5f9f
JA
1775 cfqq = NULL;
1776 goto keep_queue;
22e2c507
JA
1777 }
1778
3b18152c 1779expire:
6084cdda 1780 cfq_slice_expired(cfqd, 0);
3b18152c 1781new_queue:
718eee05
CZ
1782 /*
1783 * Current queue expired. Check if we have to switch to a new
1784 * service tree
1785 */
1786 if (!new_cfqq)
cdb16e8f 1787 cfq_choose_cfqg(cfqd);
718eee05 1788
a36e71f9 1789 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
22e2c507 1790keep_queue:
3b18152c 1791 return cfqq;
22e2c507
JA
1792}
1793
febffd61 1794static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
d9e7620e
JA
1795{
1796 int dispatched = 0;
1797
1798 while (cfqq->next_rq) {
1799 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
1800 dispatched++;
1801 }
1802
1803 BUG_ON(!list_empty(&cfqq->fifo));
f04a6424
VG
1804
1805 /* By default cfqq is not expired if it is empty. Do it explicitly */
1806 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
d9e7620e
JA
1807 return dispatched;
1808}
1809
498d3aa2
JA
1810/*
1811 * Drain our current requests. Used for barriers and when switching
1812 * io schedulers on-the-fly.
1813 */
d9e7620e 1814static int cfq_forced_dispatch(struct cfq_data *cfqd)
1b5ed5e1 1815{
0871714e 1816 struct cfq_queue *cfqq;
d9e7620e 1817 int dispatched = 0;
cdb16e8f 1818
f04a6424
VG
1819 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL)
1820 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
1b5ed5e1 1821
6084cdda 1822 cfq_slice_expired(cfqd, 0);
1b5ed5e1
TH
1823 BUG_ON(cfqd->busy_queues);
1824
6923715a 1825 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
1b5ed5e1
TH
1826 return dispatched;
1827}
1828
0b182d61 1829static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2f5cb738 1830{
2f5cb738 1831 unsigned int max_dispatch;
22e2c507 1832
5ad531db
JA
1833 /*
1834 * Drain async requests before we start sync IO
1835 */
a6d44e98 1836 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_driver[BLK_RW_ASYNC])
0b182d61 1837 return false;
5ad531db 1838
2f5cb738
JA
1839 /*
1840 * If this is an async queue and we have sync IO in flight, let it wait
1841 */
1842 if (cfqd->sync_flight && !cfq_cfqq_sync(cfqq))
0b182d61 1843 return false;
2f5cb738
JA
1844
1845 max_dispatch = cfqd->cfq_quantum;
1846 if (cfq_class_idle(cfqq))
1847 max_dispatch = 1;
b4878f24 1848
2f5cb738
JA
1849 /*
1850 * Does this cfqq already have too much IO in flight?
1851 */
1852 if (cfqq->dispatched >= max_dispatch) {
1853 /*
1854 * idle queue must always only have a single IO in flight
1855 */
3ed9a296 1856 if (cfq_class_idle(cfqq))
0b182d61 1857 return false;
3ed9a296 1858
2f5cb738
JA
1859 /*
1860 * We have other queues, don't allow more IO from this one
1861 */
1862 if (cfqd->busy_queues > 1)
0b182d61 1863 return false;
9ede209e 1864
365722bb 1865 /*
474b18cc 1866 * Sole queue user, no limit
365722bb 1867 */
474b18cc 1868 max_dispatch = -1;
8e296755
JA
1869 }
1870
1871 /*
1872 * Async queues must wait a bit before being allowed dispatch.
1873 * We also ramp up the dispatch depth gradually for async IO,
1874 * based on the last sync IO we serviced
1875 */
963b72fc 1876 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
8e296755
JA
1877 unsigned long last_sync = jiffies - cfqd->last_end_sync_rq;
1878 unsigned int depth;
365722bb 1879
61f0c1dc 1880 depth = last_sync / cfqd->cfq_slice[1];
e00c54c3
JA
1881 if (!depth && !cfqq->dispatched)
1882 depth = 1;
8e296755
JA
1883 if (depth < max_dispatch)
1884 max_dispatch = depth;
2f5cb738 1885 }
3ed9a296 1886
0b182d61
JA
1887 /*
1888 * If we're below the current max, allow a dispatch
1889 */
1890 return cfqq->dispatched < max_dispatch;
1891}
1892
1893/*
1894 * Dispatch a request from cfqq, moving them to the request queue
1895 * dispatch list.
1896 */
1897static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1898{
1899 struct request *rq;
1900
1901 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
1902
1903 if (!cfq_may_dispatch(cfqd, cfqq))
1904 return false;
1905
1906 /*
1907 * follow expired path, else get first next available
1908 */
1909 rq = cfq_check_fifo(cfqq);
1910 if (!rq)
1911 rq = cfqq->next_rq;
1912
1913 /*
1914 * insert request into driver dispatch list
1915 */
1916 cfq_dispatch_insert(cfqd->queue, rq);
1917
1918 if (!cfqd->active_cic) {
1919 struct cfq_io_context *cic = RQ_CIC(rq);
1920
1921 atomic_long_inc(&cic->ioc->refcount);
1922 cfqd->active_cic = cic;
1923 }
1924
1925 return true;
1926}
1927
1928/*
1929 * Find the cfqq that we need to service and move a request from that to the
1930 * dispatch list
1931 */
1932static int cfq_dispatch_requests(struct request_queue *q, int force)
1933{
1934 struct cfq_data *cfqd = q->elevator->elevator_data;
1935 struct cfq_queue *cfqq;
1936
1937 if (!cfqd->busy_queues)
1938 return 0;
1939
1940 if (unlikely(force))
1941 return cfq_forced_dispatch(cfqd);
1942
1943 cfqq = cfq_select_queue(cfqd);
1944 if (!cfqq)
8e296755
JA
1945 return 0;
1946
2f5cb738 1947 /*
0b182d61 1948 * Dispatch a request from this cfqq, if it is allowed
2f5cb738 1949 */
0b182d61
JA
1950 if (!cfq_dispatch_request(cfqd, cfqq))
1951 return 0;
1952
2f5cb738 1953 cfqq->slice_dispatch++;
b029195d 1954 cfq_clear_cfqq_must_dispatch(cfqq);
22e2c507 1955
2f5cb738
JA
1956 /*
1957 * expire an async queue immediately if it has used up its slice. idle
1958 * queue always expire after 1 dispatch round.
1959 */
1960 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
1961 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1962 cfq_class_idle(cfqq))) {
1963 cfqq->slice_end = jiffies + 1;
1964 cfq_slice_expired(cfqd, 0);
1da177e4
LT
1965 }
1966
b217a903 1967 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
2f5cb738 1968 return 1;
1da177e4
LT
1969}
1970
1da177e4 1971/*
5e705374
JA
1972 * task holds one reference to the queue, dropped when task exits. each rq
1973 * in-flight on this queue also holds a reference, dropped when rq is freed.
1da177e4
LT
1974 *
1975 * queue lock must be held here.
1976 */
1977static void cfq_put_queue(struct cfq_queue *cfqq)
1978{
22e2c507
JA
1979 struct cfq_data *cfqd = cfqq->cfqd;
1980
1981 BUG_ON(atomic_read(&cfqq->ref) <= 0);
1da177e4
LT
1982
1983 if (!atomic_dec_and_test(&cfqq->ref))
1984 return;
1985
7b679138 1986 cfq_log_cfqq(cfqd, cfqq, "put_queue");
1da177e4 1987 BUG_ON(rb_first(&cfqq->sort_list));
22e2c507 1988 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
1da177e4 1989
28f95cbc 1990 if (unlikely(cfqd->active_queue == cfqq)) {
6084cdda 1991 __cfq_slice_expired(cfqd, cfqq, 0);
23e018a1 1992 cfq_schedule_dispatch(cfqd);
28f95cbc 1993 }
22e2c507 1994
f04a6424 1995 BUG_ON(cfq_cfqq_on_rr(cfqq));
1da177e4
LT
1996 kmem_cache_free(cfq_pool, cfqq);
1997}
1998
d6de8be7
JA
1999/*
2000 * Must always be called with the rcu_read_lock() held
2001 */
07416d29
JA
2002static void
2003__call_for_each_cic(struct io_context *ioc,
2004 void (*func)(struct io_context *, struct cfq_io_context *))
2005{
2006 struct cfq_io_context *cic;
2007 struct hlist_node *n;
2008
2009 hlist_for_each_entry_rcu(cic, n, &ioc->cic_list, cic_list)
2010 func(ioc, cic);
2011}
2012
4ac845a2 2013/*
34e6bbf2 2014 * Call func for each cic attached to this ioc.
4ac845a2 2015 */
34e6bbf2 2016static void
4ac845a2
JA
2017call_for_each_cic(struct io_context *ioc,
2018 void (*func)(struct io_context *, struct cfq_io_context *))
1da177e4 2019{
4ac845a2 2020 rcu_read_lock();
07416d29 2021 __call_for_each_cic(ioc, func);
4ac845a2 2022 rcu_read_unlock();
34e6bbf2
FC
2023}
2024
2025static void cfq_cic_free_rcu(struct rcu_head *head)
2026{
2027 struct cfq_io_context *cic;
2028
2029 cic = container_of(head, struct cfq_io_context, rcu_head);
2030
2031 kmem_cache_free(cfq_ioc_pool, cic);
245b2e70 2032 elv_ioc_count_dec(cfq_ioc_count);
34e6bbf2 2033
9a11b4ed
JA
2034 if (ioc_gone) {
2035 /*
2036 * CFQ scheduler is exiting, grab exit lock and check
2037 * the pending io context count. If it hits zero,
2038 * complete ioc_gone and set it back to NULL
2039 */
2040 spin_lock(&ioc_gone_lock);
245b2e70 2041 if (ioc_gone && !elv_ioc_count_read(cfq_ioc_count)) {
9a11b4ed
JA
2042 complete(ioc_gone);
2043 ioc_gone = NULL;
2044 }
2045 spin_unlock(&ioc_gone_lock);
2046 }
34e6bbf2 2047}
4ac845a2 2048
34e6bbf2
FC
2049static void cfq_cic_free(struct cfq_io_context *cic)
2050{
2051 call_rcu(&cic->rcu_head, cfq_cic_free_rcu);
4ac845a2
JA
2052}
2053
2054static void cic_free_func(struct io_context *ioc, struct cfq_io_context *cic)
2055{
2056 unsigned long flags;
2057
2058 BUG_ON(!cic->dead_key);
2059
2060 spin_lock_irqsave(&ioc->lock, flags);
2061 radix_tree_delete(&ioc->radix_root, cic->dead_key);
ffc4e759 2062 hlist_del_rcu(&cic->cic_list);
4ac845a2
JA
2063 spin_unlock_irqrestore(&ioc->lock, flags);
2064
34e6bbf2 2065 cfq_cic_free(cic);
4ac845a2
JA
2066}
2067
d6de8be7
JA
2068/*
2069 * Must be called with rcu_read_lock() held or preemption otherwise disabled.
2070 * Only two callers of this - ->dtor() which is called with the rcu_read_lock(),
2071 * and ->trim() which is called with the task lock held
2072 */
4ac845a2
JA
2073static void cfq_free_io_context(struct io_context *ioc)
2074{
4ac845a2 2075 /*
34e6bbf2
FC
2076 * ioc->refcount is zero here, or we are called from elv_unregister(),
2077 * so no more cic's are allowed to be linked into this ioc. So it
2078 * should be ok to iterate over the known list, we will see all cic's
2079 * since no new ones are added.
4ac845a2 2080 */
07416d29 2081 __call_for_each_cic(ioc, cic_free_func);
1da177e4
LT
2082}
2083
89850f7e 2084static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1da177e4 2085{
df5fe3e8
JM
2086 struct cfq_queue *__cfqq, *next;
2087
28f95cbc 2088 if (unlikely(cfqq == cfqd->active_queue)) {
6084cdda 2089 __cfq_slice_expired(cfqd, cfqq, 0);
23e018a1 2090 cfq_schedule_dispatch(cfqd);
28f95cbc 2091 }
22e2c507 2092
df5fe3e8
JM
2093 /*
2094 * If this queue was scheduled to merge with another queue, be
2095 * sure to drop the reference taken on that queue (and others in
2096 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
2097 */
2098 __cfqq = cfqq->new_cfqq;
2099 while (__cfqq) {
2100 if (__cfqq == cfqq) {
2101 WARN(1, "cfqq->new_cfqq loop detected\n");
2102 break;
2103 }
2104 next = __cfqq->new_cfqq;
2105 cfq_put_queue(__cfqq);
2106 __cfqq = next;
2107 }
2108
89850f7e
JA
2109 cfq_put_queue(cfqq);
2110}
22e2c507 2111
89850f7e
JA
2112static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
2113 struct cfq_io_context *cic)
2114{
4faa3c81
FC
2115 struct io_context *ioc = cic->ioc;
2116
fc46379d 2117 list_del_init(&cic->queue_list);
4ac845a2
JA
2118
2119 /*
2120 * Make sure key == NULL is seen for dead queues
2121 */
fc46379d 2122 smp_wmb();
4ac845a2 2123 cic->dead_key = (unsigned long) cic->key;
fc46379d
JA
2124 cic->key = NULL;
2125
4faa3c81
FC
2126 if (ioc->ioc_data == cic)
2127 rcu_assign_pointer(ioc->ioc_data, NULL);
2128
ff6657c6
JA
2129 if (cic->cfqq[BLK_RW_ASYNC]) {
2130 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
2131 cic->cfqq[BLK_RW_ASYNC] = NULL;
12a05732
AV
2132 }
2133
ff6657c6
JA
2134 if (cic->cfqq[BLK_RW_SYNC]) {
2135 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
2136 cic->cfqq[BLK_RW_SYNC] = NULL;
12a05732 2137 }
89850f7e
JA
2138}
2139
4ac845a2
JA
2140static void cfq_exit_single_io_context(struct io_context *ioc,
2141 struct cfq_io_context *cic)
89850f7e
JA
2142{
2143 struct cfq_data *cfqd = cic->key;
2144
89850f7e 2145 if (cfqd) {
165125e1 2146 struct request_queue *q = cfqd->queue;
4ac845a2 2147 unsigned long flags;
89850f7e 2148
4ac845a2 2149 spin_lock_irqsave(q->queue_lock, flags);
62c1fe9d
JA
2150
2151 /*
2152 * Ensure we get a fresh copy of the ->key to prevent
2153 * race between exiting task and queue
2154 */
2155 smp_read_barrier_depends();
2156 if (cic->key)
2157 __cfq_exit_single_io_context(cfqd, cic);
2158
4ac845a2 2159 spin_unlock_irqrestore(q->queue_lock, flags);
89850f7e 2160 }
1da177e4
LT
2161}
2162
498d3aa2
JA
2163/*
2164 * The process that ioc belongs to has exited, we need to clean up
2165 * and put the internal structures we have that belongs to that process.
2166 */
e2d74ac0 2167static void cfq_exit_io_context(struct io_context *ioc)
1da177e4 2168{
4ac845a2 2169 call_for_each_cic(ioc, cfq_exit_single_io_context);
1da177e4
LT
2170}
2171
22e2c507 2172static struct cfq_io_context *
8267e268 2173cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
1da177e4 2174{
b5deef90 2175 struct cfq_io_context *cic;
1da177e4 2176
94f6030c
CL
2177 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask | __GFP_ZERO,
2178 cfqd->queue->node);
1da177e4 2179 if (cic) {
22e2c507 2180 cic->last_end_request = jiffies;
553698f9 2181 INIT_LIST_HEAD(&cic->queue_list);
ffc4e759 2182 INIT_HLIST_NODE(&cic->cic_list);
22e2c507
JA
2183 cic->dtor = cfq_free_io_context;
2184 cic->exit = cfq_exit_io_context;
245b2e70 2185 elv_ioc_count_inc(cfq_ioc_count);
1da177e4
LT
2186 }
2187
2188 return cic;
2189}
2190
fd0928df 2191static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc)
22e2c507
JA
2192{
2193 struct task_struct *tsk = current;
2194 int ioprio_class;
2195
3b18152c 2196 if (!cfq_cfqq_prio_changed(cfqq))
22e2c507
JA
2197 return;
2198
fd0928df 2199 ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio);
22e2c507 2200 switch (ioprio_class) {
fe094d98
JA
2201 default:
2202 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
2203 case IOPRIO_CLASS_NONE:
2204 /*
6d63c275 2205 * no prio set, inherit CPU scheduling settings
fe094d98
JA
2206 */
2207 cfqq->ioprio = task_nice_ioprio(tsk);
6d63c275 2208 cfqq->ioprio_class = task_nice_ioclass(tsk);
fe094d98
JA
2209 break;
2210 case IOPRIO_CLASS_RT:
2211 cfqq->ioprio = task_ioprio(ioc);
2212 cfqq->ioprio_class = IOPRIO_CLASS_RT;
2213 break;
2214 case IOPRIO_CLASS_BE:
2215 cfqq->ioprio = task_ioprio(ioc);
2216 cfqq->ioprio_class = IOPRIO_CLASS_BE;
2217 break;
2218 case IOPRIO_CLASS_IDLE:
2219 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
2220 cfqq->ioprio = 7;
2221 cfq_clear_cfqq_idle_window(cfqq);
2222 break;
22e2c507
JA
2223 }
2224
2225 /*
2226 * keep track of original prio settings in case we have to temporarily
2227 * elevate the priority of this queue
2228 */
2229 cfqq->org_ioprio = cfqq->ioprio;
2230 cfqq->org_ioprio_class = cfqq->ioprio_class;
3b18152c 2231 cfq_clear_cfqq_prio_changed(cfqq);
22e2c507
JA
2232}
2233
febffd61 2234static void changed_ioprio(struct io_context *ioc, struct cfq_io_context *cic)
22e2c507 2235{
478a82b0
AV
2236 struct cfq_data *cfqd = cic->key;
2237 struct cfq_queue *cfqq;
c1b707d2 2238 unsigned long flags;
35e6077c 2239
caaa5f9f
JA
2240 if (unlikely(!cfqd))
2241 return;
2242
c1b707d2 2243 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
caaa5f9f 2244
ff6657c6 2245 cfqq = cic->cfqq[BLK_RW_ASYNC];
caaa5f9f
JA
2246 if (cfqq) {
2247 struct cfq_queue *new_cfqq;
ff6657c6
JA
2248 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic->ioc,
2249 GFP_ATOMIC);
caaa5f9f 2250 if (new_cfqq) {
ff6657c6 2251 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
caaa5f9f
JA
2252 cfq_put_queue(cfqq);
2253 }
22e2c507 2254 }
caaa5f9f 2255
ff6657c6 2256 cfqq = cic->cfqq[BLK_RW_SYNC];
caaa5f9f
JA
2257 if (cfqq)
2258 cfq_mark_cfqq_prio_changed(cfqq);
2259
c1b707d2 2260 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
22e2c507
JA
2261}
2262
fc46379d 2263static void cfq_ioc_set_ioprio(struct io_context *ioc)
22e2c507 2264{
4ac845a2 2265 call_for_each_cic(ioc, changed_ioprio);
fc46379d 2266 ioc->ioprio_changed = 0;
22e2c507
JA
2267}
2268
d5036d77 2269static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
a6151c3a 2270 pid_t pid, bool is_sync)
d5036d77
JA
2271{
2272 RB_CLEAR_NODE(&cfqq->rb_node);
2273 RB_CLEAR_NODE(&cfqq->p_node);
2274 INIT_LIST_HEAD(&cfqq->fifo);
2275
2276 atomic_set(&cfqq->ref, 0);
2277 cfqq->cfqd = cfqd;
2278
2279 cfq_mark_cfqq_prio_changed(cfqq);
2280
2281 if (is_sync) {
2282 if (!cfq_class_idle(cfqq))
2283 cfq_mark_cfqq_idle_window(cfqq);
2284 cfq_mark_cfqq_sync(cfqq);
2285 }
2286 cfqq->pid = pid;
2287}
2288
cdb16e8f
VG
2289static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
2290{
2291 cfqq->cfqg = cfqg;
2292}
2293
2294static struct cfq_group *cfq_get_cfqg(struct cfq_data *cfqd, int create)
2295{
2296 return &cfqd->root_group;
2297}
2298
22e2c507 2299static struct cfq_queue *
a6151c3a 2300cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync,
fd0928df 2301 struct io_context *ioc, gfp_t gfp_mask)
22e2c507 2302{
22e2c507 2303 struct cfq_queue *cfqq, *new_cfqq = NULL;
91fac317 2304 struct cfq_io_context *cic;
cdb16e8f 2305 struct cfq_group *cfqg;
22e2c507
JA
2306
2307retry:
cdb16e8f 2308 cfqg = cfq_get_cfqg(cfqd, 1);
4ac845a2 2309 cic = cfq_cic_lookup(cfqd, ioc);
91fac317
VT
2310 /* cic always exists here */
2311 cfqq = cic_to_cfqq(cic, is_sync);
22e2c507 2312
6118b70b
JA
2313 /*
2314 * Always try a new alloc if we fell back to the OOM cfqq
2315 * originally, since it should just be a temporary situation.
2316 */
2317 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
2318 cfqq = NULL;
22e2c507
JA
2319 if (new_cfqq) {
2320 cfqq = new_cfqq;
2321 new_cfqq = NULL;
2322 } else if (gfp_mask & __GFP_WAIT) {
2323 spin_unlock_irq(cfqd->queue->queue_lock);
94f6030c 2324 new_cfqq = kmem_cache_alloc_node(cfq_pool,
6118b70b 2325 gfp_mask | __GFP_ZERO,
94f6030c 2326 cfqd->queue->node);
22e2c507 2327 spin_lock_irq(cfqd->queue->queue_lock);
6118b70b
JA
2328 if (new_cfqq)
2329 goto retry;
22e2c507 2330 } else {
94f6030c
CL
2331 cfqq = kmem_cache_alloc_node(cfq_pool,
2332 gfp_mask | __GFP_ZERO,
2333 cfqd->queue->node);
22e2c507
JA
2334 }
2335
6118b70b
JA
2336 if (cfqq) {
2337 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
2338 cfq_init_prio_data(cfqq, ioc);
cdb16e8f 2339 cfq_link_cfqq_cfqg(cfqq, cfqg);
6118b70b
JA
2340 cfq_log_cfqq(cfqd, cfqq, "alloced");
2341 } else
2342 cfqq = &cfqd->oom_cfqq;
22e2c507
JA
2343 }
2344
2345 if (new_cfqq)
2346 kmem_cache_free(cfq_pool, new_cfqq);
2347
22e2c507
JA
2348 return cfqq;
2349}
2350
c2dea2d1
VT
2351static struct cfq_queue **
2352cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
2353{
fe094d98 2354 switch (ioprio_class) {
c2dea2d1
VT
2355 case IOPRIO_CLASS_RT:
2356 return &cfqd->async_cfqq[0][ioprio];
2357 case IOPRIO_CLASS_BE:
2358 return &cfqd->async_cfqq[1][ioprio];
2359 case IOPRIO_CLASS_IDLE:
2360 return &cfqd->async_idle_cfqq;
2361 default:
2362 BUG();
2363 }
2364}
2365
15c31be4 2366static struct cfq_queue *
a6151c3a 2367cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct io_context *ioc,
15c31be4
JA
2368 gfp_t gfp_mask)
2369{
fd0928df
JA
2370 const int ioprio = task_ioprio(ioc);
2371 const int ioprio_class = task_ioprio_class(ioc);
c2dea2d1 2372 struct cfq_queue **async_cfqq = NULL;
15c31be4
JA
2373 struct cfq_queue *cfqq = NULL;
2374
c2dea2d1
VT
2375 if (!is_sync) {
2376 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
2377 cfqq = *async_cfqq;
2378 }
2379
6118b70b 2380 if (!cfqq)
fd0928df 2381 cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, gfp_mask);
15c31be4
JA
2382
2383 /*
2384 * pin the queue now that it's allocated, scheduler exit will prune it
2385 */
c2dea2d1 2386 if (!is_sync && !(*async_cfqq)) {
15c31be4 2387 atomic_inc(&cfqq->ref);
c2dea2d1 2388 *async_cfqq = cfqq;
15c31be4
JA
2389 }
2390
2391 atomic_inc(&cfqq->ref);
2392 return cfqq;
2393}
2394
498d3aa2
JA
2395/*
2396 * We drop cfq io contexts lazily, so we may find a dead one.
2397 */
dbecf3ab 2398static void
4ac845a2
JA
2399cfq_drop_dead_cic(struct cfq_data *cfqd, struct io_context *ioc,
2400 struct cfq_io_context *cic)
dbecf3ab 2401{
4ac845a2
JA
2402 unsigned long flags;
2403
fc46379d 2404 WARN_ON(!list_empty(&cic->queue_list));
597bc485 2405
4ac845a2
JA
2406 spin_lock_irqsave(&ioc->lock, flags);
2407
4faa3c81 2408 BUG_ON(ioc->ioc_data == cic);
597bc485 2409
4ac845a2 2410 radix_tree_delete(&ioc->radix_root, (unsigned long) cfqd);
ffc4e759 2411 hlist_del_rcu(&cic->cic_list);
4ac845a2
JA
2412 spin_unlock_irqrestore(&ioc->lock, flags);
2413
2414 cfq_cic_free(cic);
dbecf3ab
OH
2415}
2416
e2d74ac0 2417static struct cfq_io_context *
4ac845a2 2418cfq_cic_lookup(struct cfq_data *cfqd, struct io_context *ioc)
e2d74ac0 2419{
e2d74ac0 2420 struct cfq_io_context *cic;
d6de8be7 2421 unsigned long flags;
4ac845a2 2422 void *k;
e2d74ac0 2423
91fac317
VT
2424 if (unlikely(!ioc))
2425 return NULL;
2426
d6de8be7
JA
2427 rcu_read_lock();
2428
597bc485
JA
2429 /*
2430 * we maintain a last-hit cache, to avoid browsing over the tree
2431 */
4ac845a2 2432 cic = rcu_dereference(ioc->ioc_data);
d6de8be7
JA
2433 if (cic && cic->key == cfqd) {
2434 rcu_read_unlock();
597bc485 2435 return cic;
d6de8be7 2436 }
597bc485 2437
4ac845a2 2438 do {
4ac845a2
JA
2439 cic = radix_tree_lookup(&ioc->radix_root, (unsigned long) cfqd);
2440 rcu_read_unlock();
2441 if (!cic)
2442 break;
be3b0753
OH
2443 /* ->key must be copied to avoid race with cfq_exit_queue() */
2444 k = cic->key;
2445 if (unlikely(!k)) {
4ac845a2 2446 cfq_drop_dead_cic(cfqd, ioc, cic);
d6de8be7 2447 rcu_read_lock();
4ac845a2 2448 continue;
dbecf3ab 2449 }
e2d74ac0 2450
d6de8be7 2451 spin_lock_irqsave(&ioc->lock, flags);
4ac845a2 2452 rcu_assign_pointer(ioc->ioc_data, cic);
d6de8be7 2453 spin_unlock_irqrestore(&ioc->lock, flags);
4ac845a2
JA
2454 break;
2455 } while (1);
e2d74ac0 2456
4ac845a2 2457 return cic;
e2d74ac0
JA
2458}
2459
4ac845a2
JA
2460/*
2461 * Add cic into ioc, using cfqd as the search key. This enables us to lookup
2462 * the process specific cfq io context when entered from the block layer.
2463 * Also adds the cic to a per-cfqd list, used when this queue is removed.
2464 */
febffd61
JA
2465static int cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
2466 struct cfq_io_context *cic, gfp_t gfp_mask)
e2d74ac0 2467{
0261d688 2468 unsigned long flags;
4ac845a2 2469 int ret;
e2d74ac0 2470
4ac845a2
JA
2471 ret = radix_tree_preload(gfp_mask);
2472 if (!ret) {
2473 cic->ioc = ioc;
2474 cic->key = cfqd;
e2d74ac0 2475
4ac845a2
JA
2476 spin_lock_irqsave(&ioc->lock, flags);
2477 ret = radix_tree_insert(&ioc->radix_root,
2478 (unsigned long) cfqd, cic);
ffc4e759
JA
2479 if (!ret)
2480 hlist_add_head_rcu(&cic->cic_list, &ioc->cic_list);
4ac845a2 2481 spin_unlock_irqrestore(&ioc->lock, flags);
e2d74ac0 2482
4ac845a2
JA
2483 radix_tree_preload_end();
2484
2485 if (!ret) {
2486 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2487 list_add(&cic->queue_list, &cfqd->cic_list);
2488 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2489 }
e2d74ac0
JA
2490 }
2491
4ac845a2
JA
2492 if (ret)
2493 printk(KERN_ERR "cfq: cic link failed!\n");
fc46379d 2494
4ac845a2 2495 return ret;
e2d74ac0
JA
2496}
2497
1da177e4
LT
2498/*
2499 * Setup general io context and cfq io context. There can be several cfq
2500 * io contexts per general io context, if this process is doing io to more
e2d74ac0 2501 * than one device managed by cfq.
1da177e4
LT
2502 */
2503static struct cfq_io_context *
e2d74ac0 2504cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
1da177e4 2505{
22e2c507 2506 struct io_context *ioc = NULL;
1da177e4 2507 struct cfq_io_context *cic;
1da177e4 2508
22e2c507 2509 might_sleep_if(gfp_mask & __GFP_WAIT);
1da177e4 2510
b5deef90 2511 ioc = get_io_context(gfp_mask, cfqd->queue->node);
1da177e4
LT
2512 if (!ioc)
2513 return NULL;
2514
4ac845a2 2515 cic = cfq_cic_lookup(cfqd, ioc);
e2d74ac0
JA
2516 if (cic)
2517 goto out;
1da177e4 2518
e2d74ac0
JA
2519 cic = cfq_alloc_io_context(cfqd, gfp_mask);
2520 if (cic == NULL)
2521 goto err;
1da177e4 2522
4ac845a2
JA
2523 if (cfq_cic_link(cfqd, ioc, cic, gfp_mask))
2524 goto err_free;
2525
1da177e4 2526out:
fc46379d
JA
2527 smp_read_barrier_depends();
2528 if (unlikely(ioc->ioprio_changed))
2529 cfq_ioc_set_ioprio(ioc);
2530
1da177e4 2531 return cic;
4ac845a2
JA
2532err_free:
2533 cfq_cic_free(cic);
1da177e4
LT
2534err:
2535 put_io_context(ioc);
2536 return NULL;
2537}
2538
22e2c507
JA
2539static void
2540cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1da177e4 2541{
aaf1228d
JA
2542 unsigned long elapsed = jiffies - cic->last_end_request;
2543 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
db3b5848 2544
22e2c507
JA
2545 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
2546 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
2547 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
2548}
1da177e4 2549
206dc69b 2550static void
b2c18e1e 2551cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
6d048f53 2552 struct request *rq)
206dc69b
JA
2553{
2554 sector_t sdist;
2555 u64 total;
2556
b2c18e1e 2557 if (!cfqq->last_request_pos)
4d00aa47 2558 sdist = 0;
b2c18e1e
JM
2559 else if (cfqq->last_request_pos < blk_rq_pos(rq))
2560 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
206dc69b 2561 else
b2c18e1e 2562 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
206dc69b
JA
2563
2564 /*
2565 * Don't allow the seek distance to get too large from the
2566 * odd fragment, pagein, etc
2567 */
b2c18e1e
JM
2568 if (cfqq->seek_samples <= 60) /* second&third seek */
2569 sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*1024);
206dc69b 2570 else
b2c18e1e 2571 sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*64);
206dc69b 2572
b2c18e1e
JM
2573 cfqq->seek_samples = (7*cfqq->seek_samples + 256) / 8;
2574 cfqq->seek_total = (7*cfqq->seek_total + (u64)256*sdist) / 8;
2575 total = cfqq->seek_total + (cfqq->seek_samples/2);
2576 do_div(total, cfqq->seek_samples);
2577 cfqq->seek_mean = (sector_t)total;
e6c5bc73
JM
2578
2579 /*
2580 * If this cfqq is shared between multiple processes, check to
2581 * make sure that those processes are still issuing I/Os within
2582 * the mean seek distance. If not, it may be time to break the
2583 * queues apart again.
2584 */
2585 if (cfq_cfqq_coop(cfqq)) {
2586 if (CFQQ_SEEKY(cfqq) && !cfqq->seeky_start)
2587 cfqq->seeky_start = jiffies;
2588 else if (!CFQQ_SEEKY(cfqq))
2589 cfqq->seeky_start = 0;
2590 }
206dc69b 2591}
1da177e4 2592
22e2c507
JA
2593/*
2594 * Disable idle window if the process thinks too long or seeks so much that
2595 * it doesn't matter
2596 */
2597static void
2598cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2599 struct cfq_io_context *cic)
2600{
7b679138 2601 int old_idle, enable_idle;
1be92f2f 2602
0871714e
JA
2603 /*
2604 * Don't idle for async or idle io prio class
2605 */
2606 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
1be92f2f
JA
2607 return;
2608
c265a7f4 2609 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
1da177e4 2610
76280aff
CZ
2611 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
2612 cfq_mark_cfqq_deep(cfqq);
2613
66dac98e 2614 if (!atomic_read(&cic->ioc->nr_tasks) || !cfqd->cfq_slice_idle ||
76280aff
CZ
2615 (!cfq_cfqq_deep(cfqq) && sample_valid(cfqq->seek_samples)
2616 && CFQQ_SEEKY(cfqq)))
22e2c507
JA
2617 enable_idle = 0;
2618 else if (sample_valid(cic->ttime_samples)) {
718eee05 2619 if (cic->ttime_mean > cfqd->cfq_slice_idle)
22e2c507
JA
2620 enable_idle = 0;
2621 else
2622 enable_idle = 1;
1da177e4
LT
2623 }
2624
7b679138
JA
2625 if (old_idle != enable_idle) {
2626 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
2627 if (enable_idle)
2628 cfq_mark_cfqq_idle_window(cfqq);
2629 else
2630 cfq_clear_cfqq_idle_window(cfqq);
2631 }
22e2c507 2632}
1da177e4 2633
22e2c507
JA
2634/*
2635 * Check if new_cfqq should preempt the currently active queue. Return 0 for
2636 * no or if we aren't sure, a 1 will cause a preempt.
2637 */
a6151c3a 2638static bool
22e2c507 2639cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
5e705374 2640 struct request *rq)
22e2c507 2641{
6d048f53 2642 struct cfq_queue *cfqq;
22e2c507 2643
6d048f53
JA
2644 cfqq = cfqd->active_queue;
2645 if (!cfqq)
a6151c3a 2646 return false;
22e2c507 2647
6d048f53 2648 if (cfq_slice_used(cfqq))
a6151c3a 2649 return true;
6d048f53
JA
2650
2651 if (cfq_class_idle(new_cfqq))
a6151c3a 2652 return false;
22e2c507
JA
2653
2654 if (cfq_class_idle(cfqq))
a6151c3a 2655 return true;
1e3335de 2656
f04a6424 2657 /* Allow preemption only if we are idling on sync-noidle tree */
e4a22919
CZ
2658 if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD &&
2659 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
f04a6424
VG
2660 new_cfqq->service_tree->count == 2 &&
2661 RB_EMPTY_ROOT(&cfqq->sort_list))
718eee05
CZ
2662 return true;
2663
374f84ac
JA
2664 /*
2665 * if the new request is sync, but the currently running queue is
2666 * not, let the sync request have priority.
2667 */
5e705374 2668 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
a6151c3a 2669 return true;
1e3335de 2670
374f84ac
JA
2671 /*
2672 * So both queues are sync. Let the new request get disk time if
2673 * it's a metadata request and the current queue is doing regular IO.
2674 */
2675 if (rq_is_meta(rq) && !cfqq->meta_pending)
e6ec4fe2 2676 return true;
22e2c507 2677
3a9a3f6c
DS
2678 /*
2679 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
2680 */
2681 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
a6151c3a 2682 return true;
3a9a3f6c 2683
1e3335de 2684 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
a6151c3a 2685 return false;
1e3335de
JA
2686
2687 /*
2688 * if this request is as-good as one we would expect from the
2689 * current cfqq, let it preempt
2690 */
e00ef799 2691 if (cfq_rq_close(cfqd, cfqq, rq))
a6151c3a 2692 return true;
1e3335de 2693
a6151c3a 2694 return false;
22e2c507
JA
2695}
2696
2697/*
2698 * cfqq preempts the active queue. if we allowed preempt with no slice left,
2699 * let it have half of its nominal slice.
2700 */
2701static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2702{
7b679138 2703 cfq_log_cfqq(cfqd, cfqq, "preempt");
6084cdda 2704 cfq_slice_expired(cfqd, 1);
22e2c507 2705
bf572256
JA
2706 /*
2707 * Put the new queue at the front of the of the current list,
2708 * so we know that it will be selected next.
2709 */
2710 BUG_ON(!cfq_cfqq_on_rr(cfqq));
edd75ffd
JA
2711
2712 cfq_service_tree_add(cfqd, cfqq, 1);
bf572256 2713
44f7c160
JA
2714 cfqq->slice_end = 0;
2715 cfq_mark_cfqq_slice_new(cfqq);
22e2c507
JA
2716}
2717
22e2c507 2718/*
5e705374 2719 * Called when a new fs request (rq) is added (to cfqq). Check if there's
22e2c507
JA
2720 * something we should do about it
2721 */
2722static void
5e705374
JA
2723cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2724 struct request *rq)
22e2c507 2725{
5e705374 2726 struct cfq_io_context *cic = RQ_CIC(rq);
12e9fddd 2727
45333d5a 2728 cfqd->rq_queued++;
374f84ac
JA
2729 if (rq_is_meta(rq))
2730 cfqq->meta_pending++;
2731
9c2c38a1 2732 cfq_update_io_thinktime(cfqd, cic);
b2c18e1e 2733 cfq_update_io_seektime(cfqd, cfqq, rq);
9c2c38a1
JA
2734 cfq_update_idle_window(cfqd, cfqq, cic);
2735
b2c18e1e 2736 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
22e2c507
JA
2737
2738 if (cfqq == cfqd->active_queue) {
2739 /*
b029195d
JA
2740 * Remember that we saw a request from this process, but
2741 * don't start queuing just yet. Otherwise we risk seeing lots
2742 * of tiny requests, because we disrupt the normal plugging
d6ceb25e
JA
2743 * and merging. If the request is already larger than a single
2744 * page, let it rip immediately. For that case we assume that
2d870722
JA
2745 * merging is already done. Ditto for a busy system that
2746 * has other work pending, don't risk delaying until the
2747 * idle timer unplug to continue working.
22e2c507 2748 */
d6ceb25e 2749 if (cfq_cfqq_wait_request(cfqq)) {
2d870722
JA
2750 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
2751 cfqd->busy_queues > 1) {
d6ceb25e 2752 del_timer(&cfqd->idle_slice_timer);
bf791937
VG
2753 __blk_run_queue(cfqd->queue);
2754 } else
2755 cfq_mark_cfqq_must_dispatch(cfqq);
d6ceb25e 2756 }
5e705374 2757 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
22e2c507
JA
2758 /*
2759 * not the active queue - expire current slice if it is
2760 * idle and has expired it's mean thinktime or this new queue
3a9a3f6c
DS
2761 * has some old slice time left and is of higher priority or
2762 * this new queue is RT and the current one is BE
22e2c507
JA
2763 */
2764 cfq_preempt_queue(cfqd, cfqq);
a7f55792 2765 __blk_run_queue(cfqd->queue);
22e2c507 2766 }
1da177e4
LT
2767}
2768
165125e1 2769static void cfq_insert_request(struct request_queue *q, struct request *rq)
1da177e4 2770{
b4878f24 2771 struct cfq_data *cfqd = q->elevator->elevator_data;
5e705374 2772 struct cfq_queue *cfqq = RQ_CFQQ(rq);
22e2c507 2773
7b679138 2774 cfq_log_cfqq(cfqd, cfqq, "insert_request");
fd0928df 2775 cfq_init_prio_data(cfqq, RQ_CIC(rq)->ioc);
1da177e4 2776
30996f40 2777 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
22e2c507 2778 list_add_tail(&rq->queuelist, &cfqq->fifo);
aa6f6a3d 2779 cfq_add_rq_rb(rq);
22e2c507 2780
5e705374 2781 cfq_rq_enqueued(cfqd, cfqq, rq);
1da177e4
LT
2782}
2783
45333d5a
AC
2784/*
2785 * Update hw_tag based on peak queue depth over 50 samples under
2786 * sufficient load.
2787 */
2788static void cfq_update_hw_tag(struct cfq_data *cfqd)
2789{
1a1238a7
SL
2790 struct cfq_queue *cfqq = cfqd->active_queue;
2791
e459dd08
CZ
2792 if (rq_in_driver(cfqd) > cfqd->hw_tag_est_depth)
2793 cfqd->hw_tag_est_depth = rq_in_driver(cfqd);
2794
2795 if (cfqd->hw_tag == 1)
2796 return;
45333d5a
AC
2797
2798 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
5ad531db 2799 rq_in_driver(cfqd) <= CFQ_HW_QUEUE_MIN)
45333d5a
AC
2800 return;
2801
1a1238a7
SL
2802 /*
2803 * If active queue hasn't enough requests and can idle, cfq might not
2804 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
2805 * case
2806 */
2807 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
2808 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
2809 CFQ_HW_QUEUE_MIN && rq_in_driver(cfqd) < CFQ_HW_QUEUE_MIN)
2810 return;
2811
45333d5a
AC
2812 if (cfqd->hw_tag_samples++ < 50)
2813 return;
2814
e459dd08 2815 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
45333d5a
AC
2816 cfqd->hw_tag = 1;
2817 else
2818 cfqd->hw_tag = 0;
45333d5a
AC
2819}
2820
165125e1 2821static void cfq_completed_request(struct request_queue *q, struct request *rq)
1da177e4 2822{
5e705374 2823 struct cfq_queue *cfqq = RQ_CFQQ(rq);
b4878f24 2824 struct cfq_data *cfqd = cfqq->cfqd;
5380a101 2825 const int sync = rq_is_sync(rq);
b4878f24 2826 unsigned long now;
1da177e4 2827
b4878f24 2828 now = jiffies;
7b679138 2829 cfq_log_cfqq(cfqd, cfqq, "complete");
1da177e4 2830
45333d5a
AC
2831 cfq_update_hw_tag(cfqd);
2832
5ad531db 2833 WARN_ON(!cfqd->rq_in_driver[sync]);
6d048f53 2834 WARN_ON(!cfqq->dispatched);
5ad531db 2835 cfqd->rq_in_driver[sync]--;
6d048f53 2836 cfqq->dispatched--;
1da177e4 2837
3ed9a296
JA
2838 if (cfq_cfqq_sync(cfqq))
2839 cfqd->sync_flight--;
2840
365722bb 2841 if (sync) {
5e705374 2842 RQ_CIC(rq)->last_end_request = now;
365722bb
VG
2843 cfqd->last_end_sync_rq = now;
2844 }
caaa5f9f
JA
2845
2846 /*
2847 * If this is the active queue, check if it needs to be expired,
2848 * or if we want to idle in case it has no pending requests.
2849 */
2850 if (cfqd->active_queue == cfqq) {
a36e71f9
JA
2851 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
2852
44f7c160
JA
2853 if (cfq_cfqq_slice_new(cfqq)) {
2854 cfq_set_prio_slice(cfqd, cfqq);
2855 cfq_clear_cfqq_slice_new(cfqq);
2856 }
a36e71f9 2857 /*
8e550632
CZ
2858 * Idling is not enabled on:
2859 * - expired queues
2860 * - idle-priority queues
2861 * - async queues
2862 * - queues with still some requests queued
2863 * - when there is a close cooperator
a36e71f9 2864 */
0871714e 2865 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
6084cdda 2866 cfq_slice_expired(cfqd, 1);
8e550632
CZ
2867 else if (sync && cfqq_empty &&
2868 !cfq_close_cooperator(cfqd, cfqq)) {
2869 cfqd->noidle_tree_requires_idle |= !rq_noidle(rq);
2870 /*
2871 * Idling is enabled for SYNC_WORKLOAD.
2872 * SYNC_NOIDLE_WORKLOAD idles at the end of the tree
2873 * only if we processed at least one !rq_noidle request
2874 */
2875 if (cfqd->serving_type == SYNC_WORKLOAD
2876 || cfqd->noidle_tree_requires_idle)
2877 cfq_arm_slice_timer(cfqd);
2878 }
caaa5f9f 2879 }
6d048f53 2880
5ad531db 2881 if (!rq_in_driver(cfqd))
23e018a1 2882 cfq_schedule_dispatch(cfqd);
1da177e4
LT
2883}
2884
22e2c507
JA
2885/*
2886 * we temporarily boost lower priority queues if they are holding fs exclusive
2887 * resources. they are boosted to normal prio (CLASS_BE/4)
2888 */
2889static void cfq_prio_boost(struct cfq_queue *cfqq)
1da177e4 2890{
22e2c507
JA
2891 if (has_fs_excl()) {
2892 /*
2893 * boost idle prio on transactions that would lock out other
2894 * users of the filesystem
2895 */
2896 if (cfq_class_idle(cfqq))
2897 cfqq->ioprio_class = IOPRIO_CLASS_BE;
2898 if (cfqq->ioprio > IOPRIO_NORM)
2899 cfqq->ioprio = IOPRIO_NORM;
2900 } else {
2901 /*
dddb7451 2902 * unboost the queue (if needed)
22e2c507 2903 */
dddb7451
CZ
2904 cfqq->ioprio_class = cfqq->org_ioprio_class;
2905 cfqq->ioprio = cfqq->org_ioprio;
22e2c507 2906 }
22e2c507 2907}
1da177e4 2908
89850f7e 2909static inline int __cfq_may_queue(struct cfq_queue *cfqq)
22e2c507 2910{
1b379d8d 2911 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
3b18152c 2912 cfq_mark_cfqq_must_alloc_slice(cfqq);
22e2c507 2913 return ELV_MQUEUE_MUST;
3b18152c 2914 }
1da177e4 2915
22e2c507 2916 return ELV_MQUEUE_MAY;
22e2c507
JA
2917}
2918
165125e1 2919static int cfq_may_queue(struct request_queue *q, int rw)
22e2c507
JA
2920{
2921 struct cfq_data *cfqd = q->elevator->elevator_data;
2922 struct task_struct *tsk = current;
91fac317 2923 struct cfq_io_context *cic;
22e2c507
JA
2924 struct cfq_queue *cfqq;
2925
2926 /*
2927 * don't force setup of a queue from here, as a call to may_queue
2928 * does not necessarily imply that a request actually will be queued.
2929 * so just lookup a possibly existing queue, or return 'may queue'
2930 * if that fails
2931 */
4ac845a2 2932 cic = cfq_cic_lookup(cfqd, tsk->io_context);
91fac317
VT
2933 if (!cic)
2934 return ELV_MQUEUE_MAY;
2935
b0b78f81 2936 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
22e2c507 2937 if (cfqq) {
fd0928df 2938 cfq_init_prio_data(cfqq, cic->ioc);
22e2c507
JA
2939 cfq_prio_boost(cfqq);
2940
89850f7e 2941 return __cfq_may_queue(cfqq);
22e2c507
JA
2942 }
2943
2944 return ELV_MQUEUE_MAY;
1da177e4
LT
2945}
2946
1da177e4
LT
2947/*
2948 * queue lock held here
2949 */
bb37b94c 2950static void cfq_put_request(struct request *rq)
1da177e4 2951{
5e705374 2952 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1da177e4 2953
5e705374 2954 if (cfqq) {
22e2c507 2955 const int rw = rq_data_dir(rq);
1da177e4 2956
22e2c507
JA
2957 BUG_ON(!cfqq->allocated[rw]);
2958 cfqq->allocated[rw]--;
1da177e4 2959
5e705374 2960 put_io_context(RQ_CIC(rq)->ioc);
1da177e4 2961
1da177e4 2962 rq->elevator_private = NULL;
5e705374 2963 rq->elevator_private2 = NULL;
1da177e4 2964
1da177e4
LT
2965 cfq_put_queue(cfqq);
2966 }
2967}
2968
df5fe3e8
JM
2969static struct cfq_queue *
2970cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_context *cic,
2971 struct cfq_queue *cfqq)
2972{
2973 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
2974 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
b3b6d040 2975 cfq_mark_cfqq_coop(cfqq->new_cfqq);
df5fe3e8
JM
2976 cfq_put_queue(cfqq);
2977 return cic_to_cfqq(cic, 1);
2978}
2979
e6c5bc73
JM
2980static int should_split_cfqq(struct cfq_queue *cfqq)
2981{
2982 if (cfqq->seeky_start &&
2983 time_after(jiffies, cfqq->seeky_start + CFQQ_COOP_TOUT))
2984 return 1;
2985 return 0;
2986}
2987
2988/*
2989 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
2990 * was the last process referring to said cfqq.
2991 */
2992static struct cfq_queue *
2993split_cfqq(struct cfq_io_context *cic, struct cfq_queue *cfqq)
2994{
2995 if (cfqq_process_refs(cfqq) == 1) {
2996 cfqq->seeky_start = 0;
2997 cfqq->pid = current->pid;
2998 cfq_clear_cfqq_coop(cfqq);
2999 return cfqq;
3000 }
3001
3002 cic_set_cfqq(cic, NULL, 1);
3003 cfq_put_queue(cfqq);
3004 return NULL;
3005}
1da177e4 3006/*
22e2c507 3007 * Allocate cfq data structures associated with this request.
1da177e4 3008 */
22e2c507 3009static int
165125e1 3010cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
1da177e4
LT
3011{
3012 struct cfq_data *cfqd = q->elevator->elevator_data;
3013 struct cfq_io_context *cic;
3014 const int rw = rq_data_dir(rq);
a6151c3a 3015 const bool is_sync = rq_is_sync(rq);
22e2c507 3016 struct cfq_queue *cfqq;
1da177e4
LT
3017 unsigned long flags;
3018
3019 might_sleep_if(gfp_mask & __GFP_WAIT);
3020
e2d74ac0 3021 cic = cfq_get_io_context(cfqd, gfp_mask);
22e2c507 3022
1da177e4
LT
3023 spin_lock_irqsave(q->queue_lock, flags);
3024
22e2c507
JA
3025 if (!cic)
3026 goto queue_fail;
3027
e6c5bc73 3028new_queue:
91fac317 3029 cfqq = cic_to_cfqq(cic, is_sync);
32f2e807 3030 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
fd0928df 3031 cfqq = cfq_get_queue(cfqd, is_sync, cic->ioc, gfp_mask);
91fac317 3032 cic_set_cfqq(cic, cfqq, is_sync);
df5fe3e8 3033 } else {
e6c5bc73
JM
3034 /*
3035 * If the queue was seeky for too long, break it apart.
3036 */
3037 if (cfq_cfqq_coop(cfqq) && should_split_cfqq(cfqq)) {
3038 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
3039 cfqq = split_cfqq(cic, cfqq);
3040 if (!cfqq)
3041 goto new_queue;
3042 }
3043
df5fe3e8
JM
3044 /*
3045 * Check to see if this queue is scheduled to merge with
3046 * another, closely cooperating queue. The merging of
3047 * queues happens here as it must be done in process context.
3048 * The reference on new_cfqq was taken in merge_cfqqs.
3049 */
3050 if (cfqq->new_cfqq)
3051 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
91fac317 3052 }
1da177e4
LT
3053
3054 cfqq->allocated[rw]++;
22e2c507 3055 atomic_inc(&cfqq->ref);
1da177e4 3056
5e705374 3057 spin_unlock_irqrestore(q->queue_lock, flags);
3b18152c 3058
5e705374
JA
3059 rq->elevator_private = cic;
3060 rq->elevator_private2 = cfqq;
3061 return 0;
1da177e4 3062
22e2c507
JA
3063queue_fail:
3064 if (cic)
3065 put_io_context(cic->ioc);
89850f7e 3066
23e018a1 3067 cfq_schedule_dispatch(cfqd);
1da177e4 3068 spin_unlock_irqrestore(q->queue_lock, flags);
7b679138 3069 cfq_log(cfqd, "set_request fail");
1da177e4
LT
3070 return 1;
3071}
3072
65f27f38 3073static void cfq_kick_queue(struct work_struct *work)
22e2c507 3074{
65f27f38 3075 struct cfq_data *cfqd =
23e018a1 3076 container_of(work, struct cfq_data, unplug_work);
165125e1 3077 struct request_queue *q = cfqd->queue;
22e2c507 3078
40bb54d1 3079 spin_lock_irq(q->queue_lock);
a7f55792 3080 __blk_run_queue(cfqd->queue);
40bb54d1 3081 spin_unlock_irq(q->queue_lock);
22e2c507
JA
3082}
3083
3084/*
3085 * Timer running if the active_queue is currently idling inside its time slice
3086 */
3087static void cfq_idle_slice_timer(unsigned long data)
3088{
3089 struct cfq_data *cfqd = (struct cfq_data *) data;
3090 struct cfq_queue *cfqq;
3091 unsigned long flags;
3c6bd2f8 3092 int timed_out = 1;
22e2c507 3093
7b679138
JA
3094 cfq_log(cfqd, "idle timer fired");
3095
22e2c507
JA
3096 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
3097
fe094d98
JA
3098 cfqq = cfqd->active_queue;
3099 if (cfqq) {
3c6bd2f8
JA
3100 timed_out = 0;
3101
b029195d
JA
3102 /*
3103 * We saw a request before the queue expired, let it through
3104 */
3105 if (cfq_cfqq_must_dispatch(cfqq))
3106 goto out_kick;
3107
22e2c507
JA
3108 /*
3109 * expired
3110 */
44f7c160 3111 if (cfq_slice_used(cfqq))
22e2c507
JA
3112 goto expire;
3113
3114 /*
3115 * only expire and reinvoke request handler, if there are
3116 * other queues with pending requests
3117 */
caaa5f9f 3118 if (!cfqd->busy_queues)
22e2c507 3119 goto out_cont;
22e2c507
JA
3120
3121 /*
3122 * not expired and it has a request pending, let it dispatch
3123 */
75e50984 3124 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
22e2c507 3125 goto out_kick;
76280aff
CZ
3126
3127 /*
3128 * Queue depth flag is reset only when the idle didn't succeed
3129 */
3130 cfq_clear_cfqq_deep(cfqq);
22e2c507
JA
3131 }
3132expire:
6084cdda 3133 cfq_slice_expired(cfqd, timed_out);
22e2c507 3134out_kick:
23e018a1 3135 cfq_schedule_dispatch(cfqd);
22e2c507
JA
3136out_cont:
3137 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
3138}
3139
3b18152c
JA
3140static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
3141{
3142 del_timer_sync(&cfqd->idle_slice_timer);
23e018a1 3143 cancel_work_sync(&cfqd->unplug_work);
3b18152c 3144}
22e2c507 3145
c2dea2d1
VT
3146static void cfq_put_async_queues(struct cfq_data *cfqd)
3147{
3148 int i;
3149
3150 for (i = 0; i < IOPRIO_BE_NR; i++) {
3151 if (cfqd->async_cfqq[0][i])
3152 cfq_put_queue(cfqd->async_cfqq[0][i]);
3153 if (cfqd->async_cfqq[1][i])
3154 cfq_put_queue(cfqd->async_cfqq[1][i]);
c2dea2d1 3155 }
2389d1ef
ON
3156
3157 if (cfqd->async_idle_cfqq)
3158 cfq_put_queue(cfqd->async_idle_cfqq);
c2dea2d1
VT
3159}
3160
b374d18a 3161static void cfq_exit_queue(struct elevator_queue *e)
1da177e4 3162{
22e2c507 3163 struct cfq_data *cfqd = e->elevator_data;
165125e1 3164 struct request_queue *q = cfqd->queue;
22e2c507 3165
3b18152c 3166 cfq_shutdown_timer_wq(cfqd);
e2d74ac0 3167
d9ff4187 3168 spin_lock_irq(q->queue_lock);
e2d74ac0 3169
d9ff4187 3170 if (cfqd->active_queue)
6084cdda 3171 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
e2d74ac0
JA
3172
3173 while (!list_empty(&cfqd->cic_list)) {
d9ff4187
AV
3174 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
3175 struct cfq_io_context,
3176 queue_list);
89850f7e
JA
3177
3178 __cfq_exit_single_io_context(cfqd, cic);
d9ff4187 3179 }
e2d74ac0 3180
c2dea2d1 3181 cfq_put_async_queues(cfqd);
15c31be4 3182
d9ff4187 3183 spin_unlock_irq(q->queue_lock);
a90d742e
AV
3184
3185 cfq_shutdown_timer_wq(cfqd);
3186
a90d742e 3187 kfree(cfqd);
1da177e4
LT
3188}
3189
165125e1 3190static void *cfq_init_queue(struct request_queue *q)
1da177e4
LT
3191{
3192 struct cfq_data *cfqd;
718eee05 3193 int i, j;
cdb16e8f 3194 struct cfq_group *cfqg;
615f0259 3195 struct cfq_rb_root *st;
1da177e4 3196
94f6030c 3197 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
1da177e4 3198 if (!cfqd)
bc1c1169 3199 return NULL;
1da177e4 3200
1fa8f6d6
VG
3201 /* Init root service tree */
3202 cfqd->grp_service_tree = CFQ_RB_ROOT;
3203
cdb16e8f
VG
3204 /* Init root group */
3205 cfqg = &cfqd->root_group;
615f0259
VG
3206 for_each_cfqg_st(cfqg, i, j, st)
3207 *st = CFQ_RB_ROOT;
1fa8f6d6 3208 RB_CLEAR_NODE(&cfqg->rb_node);
26a2ac00 3209
25bc6b07
VG
3210 /* Give preference to root group over other groups */
3211 cfqg->weight = 2*BLKIO_WEIGHT_DEFAULT;
3212
26a2ac00
JA
3213 /*
3214 * Not strictly needed (since RB_ROOT just clears the node and we
3215 * zeroed cfqd on alloc), but better be safe in case someone decides
3216 * to add magic to the rb code
3217 */
3218 for (i = 0; i < CFQ_PRIO_LISTS; i++)
3219 cfqd->prio_trees[i] = RB_ROOT;
3220
6118b70b
JA
3221 /*
3222 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
3223 * Grab a permanent reference to it, so that the normal code flow
3224 * will not attempt to free it.
3225 */
3226 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
3227 atomic_inc(&cfqd->oom_cfqq.ref);
cdb16e8f 3228 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, &cfqd->root_group);
6118b70b 3229
d9ff4187 3230 INIT_LIST_HEAD(&cfqd->cic_list);
1da177e4 3231
1da177e4 3232 cfqd->queue = q;
1da177e4 3233
22e2c507
JA
3234 init_timer(&cfqd->idle_slice_timer);
3235 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
3236 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
3237
23e018a1 3238 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
22e2c507 3239
1da177e4 3240 cfqd->cfq_quantum = cfq_quantum;
22e2c507
JA
3241 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
3242 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
1da177e4
LT
3243 cfqd->cfq_back_max = cfq_back_max;
3244 cfqd->cfq_back_penalty = cfq_back_penalty;
22e2c507
JA
3245 cfqd->cfq_slice[0] = cfq_slice_async;
3246 cfqd->cfq_slice[1] = cfq_slice_sync;
3247 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
3248 cfqd->cfq_slice_idle = cfq_slice_idle;
963b72fc 3249 cfqd->cfq_latency = 1;
e459dd08 3250 cfqd->hw_tag = -1;
365722bb 3251 cfqd->last_end_sync_rq = jiffies;
bc1c1169 3252 return cfqd;
1da177e4
LT
3253}
3254
3255static void cfq_slab_kill(void)
3256{
d6de8be7
JA
3257 /*
3258 * Caller already ensured that pending RCU callbacks are completed,
3259 * so we should have no busy allocations at this point.
3260 */
1da177e4
LT
3261 if (cfq_pool)
3262 kmem_cache_destroy(cfq_pool);
3263 if (cfq_ioc_pool)
3264 kmem_cache_destroy(cfq_ioc_pool);
3265}
3266
3267static int __init cfq_slab_setup(void)
3268{
0a31bd5f 3269 cfq_pool = KMEM_CACHE(cfq_queue, 0);
1da177e4
LT
3270 if (!cfq_pool)
3271 goto fail;
3272
34e6bbf2 3273 cfq_ioc_pool = KMEM_CACHE(cfq_io_context, 0);
1da177e4
LT
3274 if (!cfq_ioc_pool)
3275 goto fail;
3276
3277 return 0;
3278fail:
3279 cfq_slab_kill();
3280 return -ENOMEM;
3281}
3282
1da177e4
LT
3283/*
3284 * sysfs parts below -->
3285 */
1da177e4
LT
3286static ssize_t
3287cfq_var_show(unsigned int var, char *page)
3288{
3289 return sprintf(page, "%d\n", var);
3290}
3291
3292static ssize_t
3293cfq_var_store(unsigned int *var, const char *page, size_t count)
3294{
3295 char *p = (char *) page;
3296
3297 *var = simple_strtoul(p, &p, 10);
3298 return count;
3299}
3300
1da177e4 3301#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
b374d18a 3302static ssize_t __FUNC(struct elevator_queue *e, char *page) \
1da177e4 3303{ \
3d1ab40f 3304 struct cfq_data *cfqd = e->elevator_data; \
1da177e4
LT
3305 unsigned int __data = __VAR; \
3306 if (__CONV) \
3307 __data = jiffies_to_msecs(__data); \
3308 return cfq_var_show(__data, (page)); \
3309}
3310SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
22e2c507
JA
3311SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
3312SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
e572ec7e
AV
3313SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
3314SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
22e2c507
JA
3315SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
3316SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
3317SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
3318SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
963b72fc 3319SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
1da177e4
LT
3320#undef SHOW_FUNCTION
3321
3322#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
b374d18a 3323static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
1da177e4 3324{ \
3d1ab40f 3325 struct cfq_data *cfqd = e->elevator_data; \
1da177e4
LT
3326 unsigned int __data; \
3327 int ret = cfq_var_store(&__data, (page), count); \
3328 if (__data < (MIN)) \
3329 __data = (MIN); \
3330 else if (__data > (MAX)) \
3331 __data = (MAX); \
3332 if (__CONV) \
3333 *(__PTR) = msecs_to_jiffies(__data); \
3334 else \
3335 *(__PTR) = __data; \
3336 return ret; \
3337}
3338STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
fe094d98
JA
3339STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
3340 UINT_MAX, 1);
3341STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
3342 UINT_MAX, 1);
e572ec7e 3343STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
fe094d98
JA
3344STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
3345 UINT_MAX, 0);
22e2c507
JA
3346STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
3347STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
3348STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
fe094d98
JA
3349STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
3350 UINT_MAX, 0);
963b72fc 3351STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
1da177e4
LT
3352#undef STORE_FUNCTION
3353
e572ec7e
AV
3354#define CFQ_ATTR(name) \
3355 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
3356
3357static struct elv_fs_entry cfq_attrs[] = {
3358 CFQ_ATTR(quantum),
e572ec7e
AV
3359 CFQ_ATTR(fifo_expire_sync),
3360 CFQ_ATTR(fifo_expire_async),
3361 CFQ_ATTR(back_seek_max),
3362 CFQ_ATTR(back_seek_penalty),
3363 CFQ_ATTR(slice_sync),
3364 CFQ_ATTR(slice_async),
3365 CFQ_ATTR(slice_async_rq),
3366 CFQ_ATTR(slice_idle),
963b72fc 3367 CFQ_ATTR(low_latency),
e572ec7e 3368 __ATTR_NULL
1da177e4
LT
3369};
3370
1da177e4
LT
3371static struct elevator_type iosched_cfq = {
3372 .ops = {
3373 .elevator_merge_fn = cfq_merge,
3374 .elevator_merged_fn = cfq_merged_request,
3375 .elevator_merge_req_fn = cfq_merged_requests,
da775265 3376 .elevator_allow_merge_fn = cfq_allow_merge,
b4878f24 3377 .elevator_dispatch_fn = cfq_dispatch_requests,
1da177e4 3378 .elevator_add_req_fn = cfq_insert_request,
b4878f24 3379 .elevator_activate_req_fn = cfq_activate_request,
1da177e4
LT
3380 .elevator_deactivate_req_fn = cfq_deactivate_request,
3381 .elevator_queue_empty_fn = cfq_queue_empty,
3382 .elevator_completed_req_fn = cfq_completed_request,
21183b07
JA
3383 .elevator_former_req_fn = elv_rb_former_request,
3384 .elevator_latter_req_fn = elv_rb_latter_request,
1da177e4
LT
3385 .elevator_set_req_fn = cfq_set_request,
3386 .elevator_put_req_fn = cfq_put_request,
3387 .elevator_may_queue_fn = cfq_may_queue,
3388 .elevator_init_fn = cfq_init_queue,
3389 .elevator_exit_fn = cfq_exit_queue,
fc46379d 3390 .trim = cfq_free_io_context,
1da177e4 3391 },
3d1ab40f 3392 .elevator_attrs = cfq_attrs,
1da177e4
LT
3393 .elevator_name = "cfq",
3394 .elevator_owner = THIS_MODULE,
3395};
3396
3397static int __init cfq_init(void)
3398{
22e2c507
JA
3399 /*
3400 * could be 0 on HZ < 1000 setups
3401 */
3402 if (!cfq_slice_async)
3403 cfq_slice_async = 1;
3404 if (!cfq_slice_idle)
3405 cfq_slice_idle = 1;
3406
1da177e4
LT
3407 if (cfq_slab_setup())
3408 return -ENOMEM;
3409
2fdd82bd 3410 elv_register(&iosched_cfq);
1da177e4 3411
2fdd82bd 3412 return 0;
1da177e4
LT
3413}
3414
3415static void __exit cfq_exit(void)
3416{
6e9a4738 3417 DECLARE_COMPLETION_ONSTACK(all_gone);
1da177e4 3418 elv_unregister(&iosched_cfq);
334e94de 3419 ioc_gone = &all_gone;
fba82272
OH
3420 /* ioc_gone's update must be visible before reading ioc_count */
3421 smp_wmb();
d6de8be7
JA
3422
3423 /*
3424 * this also protects us from entering cfq_slab_kill() with
3425 * pending RCU callbacks
3426 */
245b2e70 3427 if (elv_ioc_count_read(cfq_ioc_count))
9a11b4ed 3428 wait_for_completion(&all_gone);
83521d3e 3429 cfq_slab_kill();
1da177e4
LT
3430}
3431
3432module_init(cfq_init);
3433module_exit(cfq_exit);
3434
3435MODULE_AUTHOR("Jens Axboe");
3436MODULE_LICENSE("GPL");
3437MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");