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