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