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