aboutsummaryrefslogtreecommitdiffstats
path: root/block/cfq-iosched.c
diff options
context:
space:
mode:
Diffstat (limited to 'block/cfq-iosched.c')
-rw-r--r--block/cfq-iosched.c1614
1 files changed, 1414 insertions, 200 deletions
diff --git a/block/cfq-iosched.c b/block/cfq-iosched.c
index aa1e9535e358..5f127cfb2e92 100644
--- a/block/cfq-iosched.c
+++ b/block/cfq-iosched.c
@@ -7,17 +7,20 @@
7 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk> 7 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
8 */ 8 */
9#include <linux/module.h> 9#include <linux/module.h>
10#include <linux/slab.h>
10#include <linux/blkdev.h> 11#include <linux/blkdev.h>
11#include <linux/elevator.h> 12#include <linux/elevator.h>
13#include <linux/jiffies.h>
12#include <linux/rbtree.h> 14#include <linux/rbtree.h>
13#include <linux/ioprio.h> 15#include <linux/ioprio.h>
14#include <linux/blktrace_api.h> 16#include <linux/blktrace_api.h>
17#include "blk-cgroup.h"
15 18
16/* 19/*
17 * tunables 20 * tunables
18 */ 21 */
19/* max queue in one round of service */ 22/* max queue in one round of service */
20static const int cfq_quantum = 4; 23static const int cfq_quantum = 8;
21static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 }; 24static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
22/* maximum backwards seek, in KiB */ 25/* maximum backwards seek, in KiB */
23static const int cfq_back_max = 16 * 1024; 26static const int cfq_back_max = 16 * 1024;
@@ -27,6 +30,8 @@ static const int cfq_slice_sync = HZ / 10;
27static int cfq_slice_async = HZ / 25; 30static int cfq_slice_async = HZ / 25;
28static const int cfq_slice_async_rq = 2; 31static const int cfq_slice_async_rq = 2;
29static int cfq_slice_idle = HZ / 125; 32static int cfq_slice_idle = HZ / 125;
33static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
34static const int cfq_hist_divisor = 4;
30 35
31/* 36/*
32 * offset from end of service tree 37 * offset from end of service tree
@@ -40,6 +45,12 @@ static int cfq_slice_idle = HZ / 125;
40 45
41#define CFQ_SLICE_SCALE (5) 46#define CFQ_SLICE_SCALE (5)
42#define CFQ_HW_QUEUE_MIN (5) 47#define CFQ_HW_QUEUE_MIN (5)
48#define CFQ_SERVICE_SHIFT 12
49
50#define CFQQ_SEEK_THR (sector_t)(8 * 100)
51#define CFQQ_CLOSE_THR (sector_t)(8 * 1024)
52#define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
53#define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8)
43 54
44#define RQ_CIC(rq) \ 55#define RQ_CIC(rq) \
45 ((struct cfq_io_context *) (rq)->elevator_private) 56 ((struct cfq_io_context *) (rq)->elevator_private)
@@ -57,6 +68,7 @@ static DEFINE_SPINLOCK(ioc_gone_lock);
57#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT) 68#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
58 69
59#define sample_valid(samples) ((samples) > 80) 70#define sample_valid(samples) ((samples) > 80)
71#define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
60 72
61/* 73/*
62 * Most of our rbtree usage is for sorting with min extraction, so 74 * Most of our rbtree usage is for sorting with min extraction, so
@@ -67,8 +79,13 @@ static DEFINE_SPINLOCK(ioc_gone_lock);
67struct cfq_rb_root { 79struct cfq_rb_root {
68 struct rb_root rb; 80 struct rb_root rb;
69 struct rb_node *left; 81 struct rb_node *left;
82 unsigned count;
83 unsigned total_weight;
84 u64 min_vdisktime;
85 struct rb_node *active;
70}; 86};
71#define CFQ_RB_ROOT (struct cfq_rb_root) { RB_ROOT, NULL, } 87#define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, .left = NULL, \
88 .count = 0, .min_vdisktime = 0, }
72 89
73/* 90/*
74 * Per process-grouping structure 91 * Per process-grouping structure
@@ -99,9 +116,14 @@ struct cfq_queue {
99 /* fifo list of requests in sort_list */ 116 /* fifo list of requests in sort_list */
100 struct list_head fifo; 117 struct list_head fifo;
101 118
119 /* time when queue got scheduled in to dispatch first request. */
120 unsigned long dispatch_start;
121 unsigned int allocated_slice;
122 unsigned int slice_dispatch;
123 /* time when first request from queue completed and slice started. */
124 unsigned long slice_start;
102 unsigned long slice_end; 125 unsigned long slice_end;
103 long slice_resid; 126 long slice_resid;
104 unsigned int slice_dispatch;
105 127
106 /* pending metadata requests */ 128 /* pending metadata requests */
107 int meta_pending; 129 int meta_pending;
@@ -113,6 +135,67 @@ struct cfq_queue {
113 unsigned short ioprio_class, org_ioprio_class; 135 unsigned short ioprio_class, org_ioprio_class;
114 136
115 pid_t pid; 137 pid_t pid;
138
139 u32 seek_history;
140 sector_t last_request_pos;
141
142 struct cfq_rb_root *service_tree;
143 struct cfq_queue *new_cfqq;
144 struct cfq_group *cfqg;
145 struct cfq_group *orig_cfqg;
146 /* Sectors dispatched in current dispatch round */
147 unsigned long nr_sectors;
148};
149
150/*
151 * First index in the service_trees.
152 * IDLE is handled separately, so it has negative index
153 */
154enum wl_prio_t {
155 BE_WORKLOAD = 0,
156 RT_WORKLOAD = 1,
157 IDLE_WORKLOAD = 2,
158};
159
160/*
161 * Second index in the service_trees.
162 */
163enum wl_type_t {
164 ASYNC_WORKLOAD = 0,
165 SYNC_NOIDLE_WORKLOAD = 1,
166 SYNC_WORKLOAD = 2
167};
168
169/* This is per cgroup per device grouping structure */
170struct cfq_group {
171 /* group service_tree member */
172 struct rb_node rb_node;
173
174 /* group service_tree key */
175 u64 vdisktime;
176 unsigned int weight;
177 bool on_st;
178
179 /* number of cfqq currently on this group */
180 int nr_cfqq;
181
182 /* Per group busy queus average. Useful for workload slice calc. */
183 unsigned int busy_queues_avg[2];
184 /*
185 * rr lists of queues with requests, onle rr for each priority class.
186 * Counts are embedded in the cfq_rb_root
187 */
188 struct cfq_rb_root service_trees[2][3];
189 struct cfq_rb_root service_tree_idle;
190
191 unsigned long saved_workload_slice;
192 enum wl_type_t saved_workload;
193 enum wl_prio_t saved_serving_prio;
194 struct blkio_group blkg;
195#ifdef CONFIG_CFQ_GROUP_IOSCHED
196 struct hlist_node cfqd_node;
197 atomic_t ref;
198#endif
116}; 199};
117 200
118/* 201/*
@@ -120,11 +203,18 @@ struct cfq_queue {
120 */ 203 */
121struct cfq_data { 204struct cfq_data {
122 struct request_queue *queue; 205 struct request_queue *queue;
206 /* Root service tree for cfq_groups */
207 struct cfq_rb_root grp_service_tree;
208 struct cfq_group root_group;
123 209
124 /* 210 /*
125 * rr list of queues with requests and the count of them 211 * The priority currently being served
126 */ 212 */
127 struct cfq_rb_root service_tree; 213 enum wl_prio_t serving_prio;
214 enum wl_type_t serving_type;
215 unsigned long workload_expires;
216 struct cfq_group *serving_group;
217 bool noidle_tree_requires_idle;
128 218
129 /* 219 /*
130 * Each priority tree is sorted by next_request position. These 220 * Each priority tree is sorted by next_request position. These
@@ -135,16 +225,22 @@ struct cfq_data {
135 225
136 unsigned int busy_queues; 226 unsigned int busy_queues;
137 227
138 int rq_in_driver[2]; 228 int rq_in_driver;
139 int sync_flight; 229 int rq_in_flight[2];
140 230
141 /* 231 /*
142 * queue-depth detection 232 * queue-depth detection
143 */ 233 */
144 int rq_queued; 234 int rq_queued;
145 int hw_tag; 235 int hw_tag;
146 int hw_tag_samples; 236 /*
147 int rq_in_driver_peak; 237 * hw_tag can be
238 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
239 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
240 * 0 => no NCQ
241 */
242 int hw_tag_est_depth;
243 unsigned int hw_tag_samples;
148 244
149 /* 245 /*
150 * idle window management 246 * idle window management
@@ -174,6 +270,7 @@ struct cfq_data {
174 unsigned int cfq_slice_async_rq; 270 unsigned int cfq_slice_async_rq;
175 unsigned int cfq_slice_idle; 271 unsigned int cfq_slice_idle;
176 unsigned int cfq_latency; 272 unsigned int cfq_latency;
273 unsigned int cfq_group_isolation;
177 274
178 struct list_head cic_list; 275 struct list_head cic_list;
179 276
@@ -182,9 +279,28 @@ struct cfq_data {
182 */ 279 */
183 struct cfq_queue oom_cfqq; 280 struct cfq_queue oom_cfqq;
184 281
185 unsigned long last_end_sync_rq; 282 unsigned long last_delayed_sync;
283
284 /* List of cfq groups being managed on this device*/
285 struct hlist_head cfqg_list;
286 struct rcu_head rcu;
186}; 287};
187 288
289static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
290
291static struct cfq_rb_root *service_tree_for(struct cfq_group *cfqg,
292 enum wl_prio_t prio,
293 enum wl_type_t type)
294{
295 if (!cfqg)
296 return NULL;
297
298 if (prio == IDLE_WORKLOAD)
299 return &cfqg->service_tree_idle;
300
301 return &cfqg->service_trees[prio][type];
302}
303
188enum cfqq_state_flags { 304enum cfqq_state_flags {
189 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */ 305 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
190 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */ 306 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
@@ -195,8 +311,10 @@ enum cfqq_state_flags {
195 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */ 311 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
196 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */ 312 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
197 CFQ_CFQQ_FLAG_sync, /* synchronous queue */ 313 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
198 CFQ_CFQQ_FLAG_coop, /* has done a coop jump of the queue */ 314 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
199 CFQ_CFQQ_FLAG_coop_preempt, /* coop preempt */ 315 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
316 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
317 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
200}; 318};
201 319
202#define CFQ_CFQQ_FNS(name) \ 320#define CFQ_CFQQ_FNS(name) \
@@ -223,25 +341,84 @@ CFQ_CFQQ_FNS(prio_changed);
223CFQ_CFQQ_FNS(slice_new); 341CFQ_CFQQ_FNS(slice_new);
224CFQ_CFQQ_FNS(sync); 342CFQ_CFQQ_FNS(sync);
225CFQ_CFQQ_FNS(coop); 343CFQ_CFQQ_FNS(coop);
226CFQ_CFQQ_FNS(coop_preempt); 344CFQ_CFQQ_FNS(split_coop);
345CFQ_CFQQ_FNS(deep);
346CFQ_CFQQ_FNS(wait_busy);
227#undef CFQ_CFQQ_FNS 347#undef CFQ_CFQQ_FNS
228 348
349#ifdef CONFIG_DEBUG_CFQ_IOSCHED
350#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
351 blk_add_trace_msg((cfqd)->queue, "cfq%d%c %s " fmt, (cfqq)->pid, \
352 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
353 blkg_path(&(cfqq)->cfqg->blkg), ##args);
354
355#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) \
356 blk_add_trace_msg((cfqd)->queue, "%s " fmt, \
357 blkg_path(&(cfqg)->blkg), ##args); \
358
359#else
229#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \ 360#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
230 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args) 361 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
362#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0);
363#endif
231#define cfq_log(cfqd, fmt, args...) \ 364#define cfq_log(cfqd, fmt, args...) \
232 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args) 365 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
233 366
367/* Traverses through cfq group service trees */
368#define for_each_cfqg_st(cfqg, i, j, st) \
369 for (i = 0; i <= IDLE_WORKLOAD; i++) \
370 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
371 : &cfqg->service_tree_idle; \
372 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
373 (i == IDLE_WORKLOAD && j == 0); \
374 j++, st = i < IDLE_WORKLOAD ? \
375 &cfqg->service_trees[i][j]: NULL) \
376
377
378static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq)
379{
380 if (cfq_class_idle(cfqq))
381 return IDLE_WORKLOAD;
382 if (cfq_class_rt(cfqq))
383 return RT_WORKLOAD;
384 return BE_WORKLOAD;
385}
386
387
388static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
389{
390 if (!cfq_cfqq_sync(cfqq))
391 return ASYNC_WORKLOAD;
392 if (!cfq_cfqq_idle_window(cfqq))
393 return SYNC_NOIDLE_WORKLOAD;
394 return SYNC_WORKLOAD;
395}
396
397static inline int cfq_group_busy_queues_wl(enum wl_prio_t wl,
398 struct cfq_data *cfqd,
399 struct cfq_group *cfqg)
400{
401 if (wl == IDLE_WORKLOAD)
402 return cfqg->service_tree_idle.count;
403
404 return cfqg->service_trees[wl][ASYNC_WORKLOAD].count
405 + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count
406 + cfqg->service_trees[wl][SYNC_WORKLOAD].count;
407}
408
409static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
410 struct cfq_group *cfqg)
411{
412 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count
413 + cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
414}
415
234static void cfq_dispatch_insert(struct request_queue *, struct request *); 416static void cfq_dispatch_insert(struct request_queue *, struct request *);
235static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool, 417static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool,
236 struct io_context *, gfp_t); 418 struct io_context *, gfp_t);
237static struct cfq_io_context *cfq_cic_lookup(struct cfq_data *, 419static struct cfq_io_context *cfq_cic_lookup(struct cfq_data *,
238 struct io_context *); 420 struct io_context *);
239 421
240static inline int rq_in_driver(struct cfq_data *cfqd)
241{
242 return cfqd->rq_in_driver[0] + cfqd->rq_in_driver[1];
243}
244
245static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_context *cic, 422static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_context *cic,
246 bool is_sync) 423 bool is_sync)
247{ 424{
@@ -279,7 +456,7 @@ static int cfq_queue_empty(struct request_queue *q)
279{ 456{
280 struct cfq_data *cfqd = q->elevator->elevator_data; 457 struct cfq_data *cfqd = q->elevator->elevator_data;
281 458
282 return !cfqd->busy_queues; 459 return !cfqd->rq_queued;
283} 460}
284 461
285/* 462/*
@@ -303,10 +480,110 @@ cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
303 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio); 480 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
304} 481}
305 482
483static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
484{
485 u64 d = delta << CFQ_SERVICE_SHIFT;
486
487 d = d * BLKIO_WEIGHT_DEFAULT;
488 do_div(d, cfqg->weight);
489 return d;
490}
491
492static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
493{
494 s64 delta = (s64)(vdisktime - min_vdisktime);
495 if (delta > 0)
496 min_vdisktime = vdisktime;
497
498 return min_vdisktime;
499}
500
501static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
502{
503 s64 delta = (s64)(vdisktime - min_vdisktime);
504 if (delta < 0)
505 min_vdisktime = vdisktime;
506
507 return min_vdisktime;
508}
509
510static void update_min_vdisktime(struct cfq_rb_root *st)
511{
512 u64 vdisktime = st->min_vdisktime;
513 struct cfq_group *cfqg;
514
515 if (st->active) {
516 cfqg = rb_entry_cfqg(st->active);
517 vdisktime = cfqg->vdisktime;
518 }
519
520 if (st->left) {
521 cfqg = rb_entry_cfqg(st->left);
522 vdisktime = min_vdisktime(vdisktime, cfqg->vdisktime);
523 }
524
525 st->min_vdisktime = max_vdisktime(st->min_vdisktime, vdisktime);
526}
527
528/*
529 * get averaged number of queues of RT/BE priority.
530 * average is updated, with a formula that gives more weight to higher numbers,
531 * to quickly follows sudden increases and decrease slowly
532 */
533
534static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
535 struct cfq_group *cfqg, bool rt)
536{
537 unsigned min_q, max_q;
538 unsigned mult = cfq_hist_divisor - 1;
539 unsigned round = cfq_hist_divisor / 2;
540 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
541
542 min_q = min(cfqg->busy_queues_avg[rt], busy);
543 max_q = max(cfqg->busy_queues_avg[rt], busy);
544 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
545 cfq_hist_divisor;
546 return cfqg->busy_queues_avg[rt];
547}
548
549static inline unsigned
550cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
551{
552 struct cfq_rb_root *st = &cfqd->grp_service_tree;
553
554 return cfq_target_latency * cfqg->weight / st->total_weight;
555}
556
306static inline void 557static inline void
307cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) 558cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
308{ 559{
309 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies; 560 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
561 if (cfqd->cfq_latency) {
562 /*
563 * interested queues (we consider only the ones with the same
564 * priority class in the cfq group)
565 */
566 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
567 cfq_class_rt(cfqq));
568 unsigned sync_slice = cfqd->cfq_slice[1];
569 unsigned expect_latency = sync_slice * iq;
570 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
571
572 if (expect_latency > group_slice) {
573 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
574 /* scale low_slice according to IO priority
575 * and sync vs async */
576 unsigned low_slice =
577 min(slice, base_low_slice * slice / sync_slice);
578 /* the adapted slice value is scaled to fit all iqs
579 * into the target latency */
580 slice = max(slice * group_slice / expect_latency,
581 low_slice);
582 }
583 }
584 cfqq->slice_start = jiffies;
585 cfqq->slice_end = jiffies + slice;
586 cfqq->allocated_slice = slice;
310 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies); 587 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
311} 588}
312 589
@@ -331,9 +608,9 @@ static inline bool cfq_slice_used(struct cfq_queue *cfqq)
331 * behind the head is penalized and only allowed to a certain extent. 608 * behind the head is penalized and only allowed to a certain extent.
332 */ 609 */
333static struct request * 610static struct request *
334cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2) 611cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
335{ 612{
336 sector_t last, s1, s2, d1 = 0, d2 = 0; 613 sector_t s1, s2, d1 = 0, d2 = 0;
337 unsigned long back_max; 614 unsigned long back_max;
338#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */ 615#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
339#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */ 616#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
@@ -356,8 +633,6 @@ cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
356 s1 = blk_rq_pos(rq1); 633 s1 = blk_rq_pos(rq1);
357 s2 = blk_rq_pos(rq2); 634 s2 = blk_rq_pos(rq2);
358 635
359 last = cfqd->last_position;
360
361 /* 636 /*
362 * by definition, 1KiB is 2 sectors 637 * by definition, 1KiB is 2 sectors
363 */ 638 */
@@ -425,6 +700,10 @@ cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
425 */ 700 */
426static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root) 701static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
427{ 702{
703 /* Service tree is empty */
704 if (!root->count)
705 return NULL;
706
428 if (!root->left) 707 if (!root->left)
429 root->left = rb_first(&root->rb); 708 root->left = rb_first(&root->rb);
430 709
@@ -434,6 +713,17 @@ static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
434 return NULL; 713 return NULL;
435} 714}
436 715
716static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
717{
718 if (!root->left)
719 root->left = rb_first(&root->rb);
720
721 if (root->left)
722 return rb_entry_cfqg(root->left);
723
724 return NULL;
725}
726
437static void rb_erase_init(struct rb_node *n, struct rb_root *root) 727static void rb_erase_init(struct rb_node *n, struct rb_root *root)
438{ 728{
439 rb_erase(n, root); 729 rb_erase(n, root);
@@ -445,6 +735,7 @@ static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
445 if (root->left == n) 735 if (root->left == n)
446 root->left = NULL; 736 root->left = NULL;
447 rb_erase_init(n, &root->rb); 737 rb_erase_init(n, &root->rb);
738 --root->count;
448} 739}
449 740
450/* 741/*
@@ -471,7 +762,7 @@ cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
471 next = rb_entry_rq(rbnext); 762 next = rb_entry_rq(rbnext);
472 } 763 }
473 764
474 return cfq_choose_req(cfqd, next, prev); 765 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
475} 766}
476 767
477static unsigned long cfq_slice_offset(struct cfq_data *cfqd, 768static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
@@ -480,12 +771,334 @@ static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
480 /* 771 /*
481 * just an approximation, should be ok. 772 * just an approximation, should be ok.
482 */ 773 */
483 return (cfqd->busy_queues - 1) * (cfq_prio_slice(cfqd, 1, 0) - 774 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
484 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio)); 775 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
485} 776}
486 777
778static inline s64
779cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
780{
781 return cfqg->vdisktime - st->min_vdisktime;
782}
783
784static void
785__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
786{
787 struct rb_node **node = &st->rb.rb_node;
788 struct rb_node *parent = NULL;
789 struct cfq_group *__cfqg;
790 s64 key = cfqg_key(st, cfqg);
791 int left = 1;
792
793 while (*node != NULL) {
794 parent = *node;
795 __cfqg = rb_entry_cfqg(parent);
796
797 if (key < cfqg_key(st, __cfqg))
798 node = &parent->rb_left;
799 else {
800 node = &parent->rb_right;
801 left = 0;
802 }
803 }
804
805 if (left)
806 st->left = &cfqg->rb_node;
807
808 rb_link_node(&cfqg->rb_node, parent, node);
809 rb_insert_color(&cfqg->rb_node, &st->rb);
810}
811
812static void
813cfq_group_service_tree_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
814{
815 struct cfq_rb_root *st = &cfqd->grp_service_tree;
816 struct cfq_group *__cfqg;
817 struct rb_node *n;
818
819 cfqg->nr_cfqq++;
820 if (cfqg->on_st)
821 return;
822
823 /*
824 * Currently put the group at the end. Later implement something
825 * so that groups get lesser vtime based on their weights, so that
826 * if group does not loose all if it was not continously backlogged.
827 */
828 n = rb_last(&st->rb);
829 if (n) {
830 __cfqg = rb_entry_cfqg(n);
831 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
832 } else
833 cfqg->vdisktime = st->min_vdisktime;
834
835 __cfq_group_service_tree_add(st, cfqg);
836 cfqg->on_st = true;
837 st->total_weight += cfqg->weight;
838}
839
840static void
841cfq_group_service_tree_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
842{
843 struct cfq_rb_root *st = &cfqd->grp_service_tree;
844
845 if (st->active == &cfqg->rb_node)
846 st->active = NULL;
847
848 BUG_ON(cfqg->nr_cfqq < 1);
849 cfqg->nr_cfqq--;
850
851 /* If there are other cfq queues under this group, don't delete it */
852 if (cfqg->nr_cfqq)
853 return;
854
855 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
856 cfqg->on_st = false;
857 st->total_weight -= cfqg->weight;
858 if (!RB_EMPTY_NODE(&cfqg->rb_node))
859 cfq_rb_erase(&cfqg->rb_node, st);
860 cfqg->saved_workload_slice = 0;
861 blkiocg_update_blkio_group_dequeue_stats(&cfqg->blkg, 1);
862}
863
864static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq)
865{
866 unsigned int slice_used;
867
868 /*
869 * Queue got expired before even a single request completed or
870 * got expired immediately after first request completion.
871 */
872 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
873 /*
874 * Also charge the seek time incurred to the group, otherwise
875 * if there are mutiple queues in the group, each can dispatch
876 * a single request on seeky media and cause lots of seek time
877 * and group will never know it.
878 */
879 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
880 1);
881 } else {
882 slice_used = jiffies - cfqq->slice_start;
883 if (slice_used > cfqq->allocated_slice)
884 slice_used = cfqq->allocated_slice;
885 }
886
887 cfq_log_cfqq(cfqq->cfqd, cfqq, "sl_used=%u sect=%lu", slice_used,
888 cfqq->nr_sectors);
889 return slice_used;
890}
891
892static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
893 struct cfq_queue *cfqq)
894{
895 struct cfq_rb_root *st = &cfqd->grp_service_tree;
896 unsigned int used_sl, charge_sl;
897 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
898 - cfqg->service_tree_idle.count;
899
900 BUG_ON(nr_sync < 0);
901 used_sl = charge_sl = cfq_cfqq_slice_usage(cfqq);
902
903 if (!cfq_cfqq_sync(cfqq) && !nr_sync)
904 charge_sl = cfqq->allocated_slice;
905
906 /* Can't update vdisktime while group is on service tree */
907 cfq_rb_erase(&cfqg->rb_node, st);
908 cfqg->vdisktime += cfq_scale_slice(charge_sl, cfqg);
909 __cfq_group_service_tree_add(st, cfqg);
910
911 /* This group is being expired. Save the context */
912 if (time_after(cfqd->workload_expires, jiffies)) {
913 cfqg->saved_workload_slice = cfqd->workload_expires
914 - jiffies;
915 cfqg->saved_workload = cfqd->serving_type;
916 cfqg->saved_serving_prio = cfqd->serving_prio;
917 } else
918 cfqg->saved_workload_slice = 0;
919
920 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
921 st->min_vdisktime);
922 blkiocg_update_blkio_group_stats(&cfqg->blkg, used_sl,
923 cfqq->nr_sectors);
924}
925
926#ifdef CONFIG_CFQ_GROUP_IOSCHED
927static inline struct cfq_group *cfqg_of_blkg(struct blkio_group *blkg)
928{
929 if (blkg)
930 return container_of(blkg, struct cfq_group, blkg);
931 return NULL;
932}
933
934void
935cfq_update_blkio_group_weight(struct blkio_group *blkg, unsigned int weight)
936{
937 cfqg_of_blkg(blkg)->weight = weight;
938}
939
940static struct cfq_group *
941cfq_find_alloc_cfqg(struct cfq_data *cfqd, struct cgroup *cgroup, int create)
942{
943 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
944 struct cfq_group *cfqg = NULL;
945 void *key = cfqd;
946 int i, j;
947 struct cfq_rb_root *st;
948 struct backing_dev_info *bdi = &cfqd->queue->backing_dev_info;
949 unsigned int major, minor;
950
951 cfqg = cfqg_of_blkg(blkiocg_lookup_group(blkcg, key));
952 if (cfqg && !cfqg->blkg.dev && bdi->dev && dev_name(bdi->dev)) {
953 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
954 cfqg->blkg.dev = MKDEV(major, minor);
955 goto done;
956 }
957 if (cfqg || !create)
958 goto done;
959
960 cfqg = kzalloc_node(sizeof(*cfqg), GFP_ATOMIC, cfqd->queue->node);
961 if (!cfqg)
962 goto done;
963
964 cfqg->weight = blkcg->weight;
965 for_each_cfqg_st(cfqg, i, j, st)
966 *st = CFQ_RB_ROOT;
967 RB_CLEAR_NODE(&cfqg->rb_node);
968
969 /*
970 * Take the initial reference that will be released on destroy
971 * This can be thought of a joint reference by cgroup and
972 * elevator which will be dropped by either elevator exit
973 * or cgroup deletion path depending on who is exiting first.
974 */
975 atomic_set(&cfqg->ref, 1);
976
977 /* Add group onto cgroup list */
978 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
979 blkiocg_add_blkio_group(blkcg, &cfqg->blkg, (void *)cfqd,
980 MKDEV(major, minor));
981
982 /* Add group on cfqd list */
983 hlist_add_head(&cfqg->cfqd_node, &cfqd->cfqg_list);
984
985done:
986 return cfqg;
987}
988
487/* 989/*
488 * The cfqd->service_tree holds all pending cfq_queue's that have 990 * Search for the cfq group current task belongs to. If create = 1, then also
991 * create the cfq group if it does not exist. request_queue lock must be held.
992 */
993static struct cfq_group *cfq_get_cfqg(struct cfq_data *cfqd, int create)
994{
995 struct cgroup *cgroup;
996 struct cfq_group *cfqg = NULL;
997
998 rcu_read_lock();
999 cgroup = task_cgroup(current, blkio_subsys_id);
1000 cfqg = cfq_find_alloc_cfqg(cfqd, cgroup, create);
1001 if (!cfqg && create)
1002 cfqg = &cfqd->root_group;
1003 rcu_read_unlock();
1004 return cfqg;
1005}
1006
1007static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1008{
1009 /* Currently, all async queues are mapped to root group */
1010 if (!cfq_cfqq_sync(cfqq))
1011 cfqg = &cfqq->cfqd->root_group;
1012
1013 cfqq->cfqg = cfqg;
1014 /* cfqq reference on cfqg */
1015 atomic_inc(&cfqq->cfqg->ref);
1016}
1017
1018static void cfq_put_cfqg(struct cfq_group *cfqg)
1019{
1020 struct cfq_rb_root *st;
1021 int i, j;
1022
1023 BUG_ON(atomic_read(&cfqg->ref) <= 0);
1024 if (!atomic_dec_and_test(&cfqg->ref))
1025 return;
1026 for_each_cfqg_st(cfqg, i, j, st)
1027 BUG_ON(!RB_EMPTY_ROOT(&st->rb) || st->active != NULL);
1028 kfree(cfqg);
1029}
1030
1031static void cfq_destroy_cfqg(struct cfq_data *cfqd, struct cfq_group *cfqg)
1032{
1033 /* Something wrong if we are trying to remove same group twice */
1034 BUG_ON(hlist_unhashed(&cfqg->cfqd_node));
1035
1036 hlist_del_init(&cfqg->cfqd_node);
1037
1038 /*
1039 * Put the reference taken at the time of creation so that when all
1040 * queues are gone, group can be destroyed.
1041 */
1042 cfq_put_cfqg(cfqg);
1043}
1044
1045static void cfq_release_cfq_groups(struct cfq_data *cfqd)
1046{
1047 struct hlist_node *pos, *n;
1048 struct cfq_group *cfqg;
1049
1050 hlist_for_each_entry_safe(cfqg, pos, n, &cfqd->cfqg_list, cfqd_node) {
1051 /*
1052 * If cgroup removal path got to blk_group first and removed
1053 * it from cgroup list, then it will take care of destroying
1054 * cfqg also.
1055 */
1056 if (!blkiocg_del_blkio_group(&cfqg->blkg))
1057 cfq_destroy_cfqg(cfqd, cfqg);
1058 }
1059}
1060
1061/*
1062 * Blk cgroup controller notification saying that blkio_group object is being
1063 * delinked as associated cgroup object is going away. That also means that
1064 * no new IO will come in this group. So get rid of this group as soon as
1065 * any pending IO in the group is finished.
1066 *
1067 * This function is called under rcu_read_lock(). key is the rcu protected
1068 * pointer. That means "key" is a valid cfq_data pointer as long as we are rcu
1069 * read lock.
1070 *
1071 * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
1072 * it should not be NULL as even if elevator was exiting, cgroup deltion
1073 * path got to it first.
1074 */
1075void cfq_unlink_blkio_group(void *key, struct blkio_group *blkg)
1076{
1077 unsigned long flags;
1078 struct cfq_data *cfqd = key;
1079
1080 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1081 cfq_destroy_cfqg(cfqd, cfqg_of_blkg(blkg));
1082 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1083}
1084
1085#else /* GROUP_IOSCHED */
1086static struct cfq_group *cfq_get_cfqg(struct cfq_data *cfqd, int create)
1087{
1088 return &cfqd->root_group;
1089}
1090static inline void
1091cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1092 cfqq->cfqg = cfqg;
1093}
1094
1095static void cfq_release_cfq_groups(struct cfq_data *cfqd) {}
1096static inline void cfq_put_cfqg(struct cfq_group *cfqg) {}
1097
1098#endif /* GROUP_IOSCHED */
1099
1100/*
1101 * The cfqd->service_trees holds all pending cfq_queue's that have
489 * requests waiting to be processed. It is sorted in the order that 1102 * requests waiting to be processed. It is sorted in the order that
490 * we will service the queues. 1103 * we will service the queues.
491 */ 1104 */
@@ -495,11 +1108,42 @@ static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
495 struct rb_node **p, *parent; 1108 struct rb_node **p, *parent;
496 struct cfq_queue *__cfqq; 1109 struct cfq_queue *__cfqq;
497 unsigned long rb_key; 1110 unsigned long rb_key;
1111 struct cfq_rb_root *service_tree;
498 int left; 1112 int left;
1113 int new_cfqq = 1;
1114 int group_changed = 0;
1115
1116#ifdef CONFIG_CFQ_GROUP_IOSCHED
1117 if (!cfqd->cfq_group_isolation
1118 && cfqq_type(cfqq) == SYNC_NOIDLE_WORKLOAD
1119 && cfqq->cfqg && cfqq->cfqg != &cfqd->root_group) {
1120 /* Move this cfq to root group */
1121 cfq_log_cfqq(cfqd, cfqq, "moving to root group");
1122 if (!RB_EMPTY_NODE(&cfqq->rb_node))
1123 cfq_group_service_tree_del(cfqd, cfqq->cfqg);
1124 cfqq->orig_cfqg = cfqq->cfqg;
1125 cfqq->cfqg = &cfqd->root_group;
1126 atomic_inc(&cfqd->root_group.ref);
1127 group_changed = 1;
1128 } else if (!cfqd->cfq_group_isolation
1129 && cfqq_type(cfqq) == SYNC_WORKLOAD && cfqq->orig_cfqg) {
1130 /* cfqq is sequential now needs to go to its original group */
1131 BUG_ON(cfqq->cfqg != &cfqd->root_group);
1132 if (!RB_EMPTY_NODE(&cfqq->rb_node))
1133 cfq_group_service_tree_del(cfqd, cfqq->cfqg);
1134 cfq_put_cfqg(cfqq->cfqg);
1135 cfqq->cfqg = cfqq->orig_cfqg;
1136 cfqq->orig_cfqg = NULL;
1137 group_changed = 1;
1138 cfq_log_cfqq(cfqd, cfqq, "moved to origin group");
1139 }
1140#endif
499 1141
1142 service_tree = service_tree_for(cfqq->cfqg, cfqq_prio(cfqq),
1143 cfqq_type(cfqq));
500 if (cfq_class_idle(cfqq)) { 1144 if (cfq_class_idle(cfqq)) {
501 rb_key = CFQ_IDLE_DELAY; 1145 rb_key = CFQ_IDLE_DELAY;
502 parent = rb_last(&cfqd->service_tree.rb); 1146 parent = rb_last(&service_tree->rb);
503 if (parent && parent != &cfqq->rb_node) { 1147 if (parent && parent != &cfqq->rb_node) {
504 __cfqq = rb_entry(parent, struct cfq_queue, rb_node); 1148 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
505 rb_key += __cfqq->rb_key; 1149 rb_key += __cfqq->rb_key;
@@ -517,23 +1161,27 @@ static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
517 cfqq->slice_resid = 0; 1161 cfqq->slice_resid = 0;
518 } else { 1162 } else {
519 rb_key = -HZ; 1163 rb_key = -HZ;
520 __cfqq = cfq_rb_first(&cfqd->service_tree); 1164 __cfqq = cfq_rb_first(service_tree);
521 rb_key += __cfqq ? __cfqq->rb_key : jiffies; 1165 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
522 } 1166 }
523 1167
524 if (!RB_EMPTY_NODE(&cfqq->rb_node)) { 1168 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
1169 new_cfqq = 0;
525 /* 1170 /*
526 * same position, nothing more to do 1171 * same position, nothing more to do
527 */ 1172 */
528 if (rb_key == cfqq->rb_key) 1173 if (rb_key == cfqq->rb_key &&
1174 cfqq->service_tree == service_tree)
529 return; 1175 return;
530 1176
531 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree); 1177 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1178 cfqq->service_tree = NULL;
532 } 1179 }
533 1180
534 left = 1; 1181 left = 1;
535 parent = NULL; 1182 parent = NULL;
536 p = &cfqd->service_tree.rb.rb_node; 1183 cfqq->service_tree = service_tree;
1184 p = &service_tree->rb.rb_node;
537 while (*p) { 1185 while (*p) {
538 struct rb_node **n; 1186 struct rb_node **n;
539 1187
@@ -541,35 +1189,28 @@ static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
541 __cfqq = rb_entry(parent, struct cfq_queue, rb_node); 1189 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
542 1190
543 /* 1191 /*
544 * sort RT queues first, we always want to give 1192 * sort by key, that represents service time.
545 * preference to them. IDLE queues goes to the back.
546 * after that, sort on the next service time.
547 */ 1193 */
548 if (cfq_class_rt(cfqq) > cfq_class_rt(__cfqq)) 1194 if (time_before(rb_key, __cfqq->rb_key))
549 n = &(*p)->rb_left;
550 else if (cfq_class_rt(cfqq) < cfq_class_rt(__cfqq))
551 n = &(*p)->rb_right;
552 else if (cfq_class_idle(cfqq) < cfq_class_idle(__cfqq))
553 n = &(*p)->rb_left;
554 else if (cfq_class_idle(cfqq) > cfq_class_idle(__cfqq))
555 n = &(*p)->rb_right;
556 else if (time_before(rb_key, __cfqq->rb_key))
557 n = &(*p)->rb_left; 1195 n = &(*p)->rb_left;
558 else 1196 else {
559 n = &(*p)->rb_right; 1197 n = &(*p)->rb_right;
560
561 if (n == &(*p)->rb_right)
562 left = 0; 1198 left = 0;
1199 }
563 1200
564 p = n; 1201 p = n;
565 } 1202 }
566 1203
567 if (left) 1204 if (left)
568 cfqd->service_tree.left = &cfqq->rb_node; 1205 service_tree->left = &cfqq->rb_node;
569 1206
570 cfqq->rb_key = rb_key; 1207 cfqq->rb_key = rb_key;
571 rb_link_node(&cfqq->rb_node, parent, p); 1208 rb_link_node(&cfqq->rb_node, parent, p);
572 rb_insert_color(&cfqq->rb_node, &cfqd->service_tree.rb); 1209 rb_insert_color(&cfqq->rb_node, &service_tree->rb);
1210 service_tree->count++;
1211 if ((add_front || !new_cfqq) && !group_changed)
1212 return;
1213 cfq_group_service_tree_add(cfqd, cfqq->cfqg);
573} 1214}
574 1215
575static struct cfq_queue * 1216static struct cfq_queue *
@@ -671,13 +1312,16 @@ static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
671 BUG_ON(!cfq_cfqq_on_rr(cfqq)); 1312 BUG_ON(!cfq_cfqq_on_rr(cfqq));
672 cfq_clear_cfqq_on_rr(cfqq); 1313 cfq_clear_cfqq_on_rr(cfqq);
673 1314
674 if (!RB_EMPTY_NODE(&cfqq->rb_node)) 1315 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
675 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree); 1316 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1317 cfqq->service_tree = NULL;
1318 }
676 if (cfqq->p_root) { 1319 if (cfqq->p_root) {
677 rb_erase(&cfqq->p_node, cfqq->p_root); 1320 rb_erase(&cfqq->p_node, cfqq->p_root);
678 cfqq->p_root = NULL; 1321 cfqq->p_root = NULL;
679 } 1322 }
680 1323
1324 cfq_group_service_tree_del(cfqd, cfqq->cfqg);
681 BUG_ON(!cfqd->busy_queues); 1325 BUG_ON(!cfqd->busy_queues);
682 cfqd->busy_queues--; 1326 cfqd->busy_queues--;
683} 1327}
@@ -688,7 +1332,6 @@ static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
688static void cfq_del_rq_rb(struct request *rq) 1332static void cfq_del_rq_rb(struct request *rq)
689{ 1333{
690 struct cfq_queue *cfqq = RQ_CFQQ(rq); 1334 struct cfq_queue *cfqq = RQ_CFQQ(rq);
691 struct cfq_data *cfqd = cfqq->cfqd;
692 const int sync = rq_is_sync(rq); 1335 const int sync = rq_is_sync(rq);
693 1336
694 BUG_ON(!cfqq->queued[sync]); 1337 BUG_ON(!cfqq->queued[sync]);
@@ -696,8 +1339,17 @@ static void cfq_del_rq_rb(struct request *rq)
696 1339
697 elv_rb_del(&cfqq->sort_list, rq); 1340 elv_rb_del(&cfqq->sort_list, rq);
698 1341
699 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) 1342 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
700 cfq_del_cfqq_rr(cfqd, cfqq); 1343 /*
1344 * Queue will be deleted from service tree when we actually
1345 * expire it later. Right now just remove it from prio tree
1346 * as it is empty.
1347 */
1348 if (cfqq->p_root) {
1349 rb_erase(&cfqq->p_node, cfqq->p_root);
1350 cfqq->p_root = NULL;
1351 }
1352 }
701} 1353}
702 1354
703static void cfq_add_rq_rb(struct request *rq) 1355static void cfq_add_rq_rb(struct request *rq)
@@ -722,7 +1374,7 @@ static void cfq_add_rq_rb(struct request *rq)
722 * check if this request is a better next-serve candidate 1374 * check if this request is a better next-serve candidate
723 */ 1375 */
724 prev = cfqq->next_rq; 1376 prev = cfqq->next_rq;
725 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq); 1377 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
726 1378
727 /* 1379 /*
728 * adjust priority tree position, if ->next_rq changes 1380 * adjust priority tree position, if ->next_rq changes
@@ -765,9 +1417,9 @@ static void cfq_activate_request(struct request_queue *q, struct request *rq)
765{ 1417{
766 struct cfq_data *cfqd = q->elevator->elevator_data; 1418 struct cfq_data *cfqd = q->elevator->elevator_data;
767 1419
768 cfqd->rq_in_driver[rq_is_sync(rq)]++; 1420 cfqd->rq_in_driver++;
769 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d", 1421 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
770 rq_in_driver(cfqd)); 1422 cfqd->rq_in_driver);
771 1423
772 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq); 1424 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
773} 1425}
@@ -775,12 +1427,11 @@ static void cfq_activate_request(struct request_queue *q, struct request *rq)
775static void cfq_deactivate_request(struct request_queue *q, struct request *rq) 1427static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
776{ 1428{
777 struct cfq_data *cfqd = q->elevator->elevator_data; 1429 struct cfq_data *cfqd = q->elevator->elevator_data;
778 const int sync = rq_is_sync(rq);
779 1430
780 WARN_ON(!cfqd->rq_in_driver[sync]); 1431 WARN_ON(!cfqd->rq_in_driver);
781 cfqd->rq_in_driver[sync]--; 1432 cfqd->rq_in_driver--;
782 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d", 1433 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
783 rq_in_driver(cfqd)); 1434 cfqd->rq_in_driver);
784} 1435}
785 1436
786static void cfq_remove_request(struct request *rq) 1437static void cfq_remove_request(struct request *rq)
@@ -829,6 +1480,7 @@ static void
829cfq_merged_requests(struct request_queue *q, struct request *rq, 1480cfq_merged_requests(struct request_queue *q, struct request *rq,
830 struct request *next) 1481 struct request *next)
831{ 1482{
1483 struct cfq_queue *cfqq = RQ_CFQQ(rq);
832 /* 1484 /*
833 * reposition in fifo if next is older than rq 1485 * reposition in fifo if next is older than rq
834 */ 1486 */
@@ -838,6 +1490,8 @@ cfq_merged_requests(struct request_queue *q, struct request *rq,
838 rq_set_fifo_time(rq, rq_fifo_time(next)); 1490 rq_set_fifo_time(rq, rq_fifo_time(next));
839 } 1491 }
840 1492
1493 if (cfqq->next_rq == next)
1494 cfqq->next_rq = rq;
841 cfq_remove_request(next); 1495 cfq_remove_request(next);
842} 1496}
843 1497
@@ -870,9 +1524,14 @@ static void __cfq_set_active_queue(struct cfq_data *cfqd,
870 struct cfq_queue *cfqq) 1524 struct cfq_queue *cfqq)
871{ 1525{
872 if (cfqq) { 1526 if (cfqq) {
873 cfq_log_cfqq(cfqd, cfqq, "set_active"); 1527 cfq_log_cfqq(cfqd, cfqq, "set_active wl_prio:%d wl_type:%d",
1528 cfqd->serving_prio, cfqd->serving_type);
1529 cfqq->slice_start = 0;
1530 cfqq->dispatch_start = jiffies;
1531 cfqq->allocated_slice = 0;
874 cfqq->slice_end = 0; 1532 cfqq->slice_end = 0;
875 cfqq->slice_dispatch = 0; 1533 cfqq->slice_dispatch = 0;
1534 cfqq->nr_sectors = 0;
876 1535
877 cfq_clear_cfqq_wait_request(cfqq); 1536 cfq_clear_cfqq_wait_request(cfqq);
878 cfq_clear_cfqq_must_dispatch(cfqq); 1537 cfq_clear_cfqq_must_dispatch(cfqq);
@@ -899,6 +1558,16 @@ __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
899 del_timer(&cfqd->idle_slice_timer); 1558 del_timer(&cfqd->idle_slice_timer);
900 1559
901 cfq_clear_cfqq_wait_request(cfqq); 1560 cfq_clear_cfqq_wait_request(cfqq);
1561 cfq_clear_cfqq_wait_busy(cfqq);
1562
1563 /*
1564 * If this cfqq is shared between multiple processes, check to
1565 * make sure that those processes are still issuing I/Os within
1566 * the mean seek distance. If not, it may be time to break the
1567 * queues apart again.
1568 */
1569 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
1570 cfq_mark_cfqq_split_coop(cfqq);
902 1571
903 /* 1572 /*
904 * store what was left of this slice, if the queue idled/timed out 1573 * store what was left of this slice, if the queue idled/timed out
@@ -908,11 +1577,19 @@ __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
908 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid); 1577 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
909 } 1578 }
910 1579
1580 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
1581
1582 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
1583 cfq_del_cfqq_rr(cfqd, cfqq);
1584
911 cfq_resort_rr_list(cfqd, cfqq); 1585 cfq_resort_rr_list(cfqd, cfqq);
912 1586
913 if (cfqq == cfqd->active_queue) 1587 if (cfqq == cfqd->active_queue)
914 cfqd->active_queue = NULL; 1588 cfqd->active_queue = NULL;
915 1589
1590 if (&cfqq->cfqg->rb_node == cfqd->grp_service_tree.active)
1591 cfqd->grp_service_tree.active = NULL;
1592
916 if (cfqd->active_cic) { 1593 if (cfqd->active_cic) {
917 put_io_context(cfqd->active_cic->ioc); 1594 put_io_context(cfqd->active_cic->ioc);
918 cfqd->active_cic = NULL; 1595 cfqd->active_cic = NULL;
@@ -933,10 +1610,39 @@ static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
933 */ 1610 */
934static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd) 1611static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
935{ 1612{
936 if (RB_EMPTY_ROOT(&cfqd->service_tree.rb)) 1613 struct cfq_rb_root *service_tree =
1614 service_tree_for(cfqd->serving_group, cfqd->serving_prio,
1615 cfqd->serving_type);
1616
1617 if (!cfqd->rq_queued)
1618 return NULL;
1619
1620 /* There is nothing to dispatch */
1621 if (!service_tree)
1622 return NULL;
1623 if (RB_EMPTY_ROOT(&service_tree->rb))
1624 return NULL;
1625 return cfq_rb_first(service_tree);
1626}
1627
1628static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
1629{
1630 struct cfq_group *cfqg;
1631 struct cfq_queue *cfqq;
1632 int i, j;
1633 struct cfq_rb_root *st;
1634
1635 if (!cfqd->rq_queued)
1636 return NULL;
1637
1638 cfqg = cfq_get_next_cfqg(cfqd);
1639 if (!cfqg)
937 return NULL; 1640 return NULL;
938 1641
939 return cfq_rb_first(&cfqd->service_tree); 1642 for_each_cfqg_st(cfqg, i, j, st)
1643 if ((cfqq = cfq_rb_first(st)) != NULL)
1644 return cfqq;
1645 return NULL;
940} 1646}
941 1647
942/* 1648/*
@@ -945,14 +1651,8 @@ static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
945static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd, 1651static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
946 struct cfq_queue *cfqq) 1652 struct cfq_queue *cfqq)
947{ 1653{
948 if (!cfqq) { 1654 if (!cfqq)
949 cfqq = cfq_get_next_queue(cfqd); 1655 cfqq = cfq_get_next_queue(cfqd);
950 if (cfqq && !cfq_cfqq_coop_preempt(cfqq))
951 cfq_clear_cfqq_coop(cfqq);
952 }
953
954 if (cfqq)
955 cfq_clear_cfqq_coop_preempt(cfqq);
956 1656
957 __cfq_set_active_queue(cfqd, cfqq); 1657 __cfq_set_active_queue(cfqd, cfqq);
958 return cfqq; 1658 return cfqq;
@@ -967,18 +1667,10 @@ static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
967 return cfqd->last_position - blk_rq_pos(rq); 1667 return cfqd->last_position - blk_rq_pos(rq);
968} 1668}
969 1669
970#define CIC_SEEK_THR 8 * 1024 1670static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
971#define CIC_SEEKY(cic) ((cic)->seek_mean > CIC_SEEK_THR) 1671 struct request *rq)
972
973static inline int cfq_rq_close(struct cfq_data *cfqd, struct request *rq)
974{ 1672{
975 struct cfq_io_context *cic = cfqd->active_cic; 1673 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
976 sector_t sdist = cic->seek_mean;
977
978 if (!sample_valid(cic->seek_samples))
979 sdist = CIC_SEEK_THR;
980
981 return cfq_dist_from_last(cfqd, rq) <= sdist;
982} 1674}
983 1675
984static struct cfq_queue *cfqq_close(struct cfq_data *cfqd, 1676static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
@@ -1005,7 +1697,7 @@ static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
1005 * will contain the closest sector. 1697 * will contain the closest sector.
1006 */ 1698 */
1007 __cfqq = rb_entry(parent, struct cfq_queue, p_node); 1699 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
1008 if (cfq_rq_close(cfqd, __cfqq->next_rq)) 1700 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
1009 return __cfqq; 1701 return __cfqq;
1010 1702
1011 if (blk_rq_pos(__cfqq->next_rq) < sector) 1703 if (blk_rq_pos(__cfqq->next_rq) < sector)
@@ -1016,7 +1708,7 @@ static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
1016 return NULL; 1708 return NULL;
1017 1709
1018 __cfqq = rb_entry(node, struct cfq_queue, p_node); 1710 __cfqq = rb_entry(node, struct cfq_queue, p_node);
1019 if (cfq_rq_close(cfqd, __cfqq->next_rq)) 1711 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
1020 return __cfqq; 1712 return __cfqq;
1021 1713
1022 return NULL; 1714 return NULL;
@@ -1033,16 +1725,21 @@ static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
1033 * assumption. 1725 * assumption.
1034 */ 1726 */
1035static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd, 1727static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
1036 struct cfq_queue *cur_cfqq, 1728 struct cfq_queue *cur_cfqq)
1037 bool probe)
1038{ 1729{
1039 struct cfq_queue *cfqq; 1730 struct cfq_queue *cfqq;
1040 1731
1732 if (cfq_class_idle(cur_cfqq))
1733 return NULL;
1734 if (!cfq_cfqq_sync(cur_cfqq))
1735 return NULL;
1736 if (CFQQ_SEEKY(cur_cfqq))
1737 return NULL;
1738
1041 /* 1739 /*
1042 * A valid cfq_io_context is necessary to compare requests against 1740 * Don't search priority tree if it's the only queue in the group.
1043 * the seek_mean of the current cfqq.
1044 */ 1741 */
1045 if (!cfqd->active_cic) 1742 if (cur_cfqq->cfqg->nr_cfqq == 1)
1046 return NULL; 1743 return NULL;
1047 1744
1048 /* 1745 /*
@@ -1054,14 +1751,59 @@ static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
1054 if (!cfqq) 1751 if (!cfqq)
1055 return NULL; 1752 return NULL;
1056 1753
1057 if (cfq_cfqq_coop(cfqq)) 1754 /* If new queue belongs to different cfq_group, don't choose it */
1755 if (cur_cfqq->cfqg != cfqq->cfqg)
1756 return NULL;
1757
1758 /*
1759 * It only makes sense to merge sync queues.
1760 */
1761 if (!cfq_cfqq_sync(cfqq))
1762 return NULL;
1763 if (CFQQ_SEEKY(cfqq))
1764 return NULL;
1765
1766 /*
1767 * Do not merge queues of different priority classes
1768 */
1769 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
1058 return NULL; 1770 return NULL;
1059 1771
1060 if (!probe)
1061 cfq_mark_cfqq_coop(cfqq);
1062 return cfqq; 1772 return cfqq;
1063} 1773}
1064 1774
1775/*
1776 * Determine whether we should enforce idle window for this queue.
1777 */
1778
1779static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1780{
1781 enum wl_prio_t prio = cfqq_prio(cfqq);
1782 struct cfq_rb_root *service_tree = cfqq->service_tree;
1783
1784 BUG_ON(!service_tree);
1785 BUG_ON(!service_tree->count);
1786
1787 /* We never do for idle class queues. */
1788 if (prio == IDLE_WORKLOAD)
1789 return false;
1790
1791 /* We do for queues that were marked with idle window flag. */
1792 if (cfq_cfqq_idle_window(cfqq) &&
1793 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
1794 return true;
1795
1796 /*
1797 * Otherwise, we do only if they are the last ones
1798 * in their service tree.
1799 */
1800 if (service_tree->count == 1 && cfq_cfqq_sync(cfqq))
1801 return 1;
1802 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d",
1803 service_tree->count);
1804 return 0;
1805}
1806
1065static void cfq_arm_slice_timer(struct cfq_data *cfqd) 1807static void cfq_arm_slice_timer(struct cfq_data *cfqd)
1066{ 1808{
1067 struct cfq_queue *cfqq = cfqd->active_queue; 1809 struct cfq_queue *cfqq = cfqd->active_queue;
@@ -1082,13 +1824,13 @@ static void cfq_arm_slice_timer(struct cfq_data *cfqd)
1082 /* 1824 /*
1083 * idle is disabled, either manually or by past process history 1825 * idle is disabled, either manually or by past process history
1084 */ 1826 */
1085 if (!cfqd->cfq_slice_idle || !cfq_cfqq_idle_window(cfqq)) 1827 if (!cfqd->cfq_slice_idle || !cfq_should_idle(cfqd, cfqq))
1086 return; 1828 return;
1087 1829
1088 /* 1830 /*
1089 * still requests with the driver, don't idle 1831 * still active requests from this queue, don't idle
1090 */ 1832 */
1091 if (rq_in_driver(cfqd)) 1833 if (cfqq->dispatched)
1092 return; 1834 return;
1093 1835
1094 /* 1836 /*
@@ -1104,19 +1846,15 @@ static void cfq_arm_slice_timer(struct cfq_data *cfqd)
1104 * time slice. 1846 * time slice.
1105 */ 1847 */
1106 if (sample_valid(cic->ttime_samples) && 1848 if (sample_valid(cic->ttime_samples) &&
1107 (cfqq->slice_end - jiffies < cic->ttime_mean)) 1849 (cfqq->slice_end - jiffies < cic->ttime_mean)) {
1850 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%d",
1851 cic->ttime_mean);
1108 return; 1852 return;
1853 }
1109 1854
1110 cfq_mark_cfqq_wait_request(cfqq); 1855 cfq_mark_cfqq_wait_request(cfqq);
1111 1856
1112 /*
1113 * we don't want to idle for seeks, but we do want to allow
1114 * fair distribution of slice time for a process doing back-to-back
1115 * seeks. so allow a little bit of time for him to submit a new rq
1116 */
1117 sl = cfqd->cfq_slice_idle; 1857 sl = cfqd->cfq_slice_idle;
1118 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
1119 sl = min(sl, msecs_to_jiffies(CFQ_MIN_TT));
1120 1858
1121 mod_timer(&cfqd->idle_slice_timer, jiffies + sl); 1859 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
1122 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu", sl); 1860 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu", sl);
@@ -1137,8 +1875,8 @@ static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
1137 cfqq->dispatched++; 1875 cfqq->dispatched++;
1138 elv_dispatch_sort(q, rq); 1876 elv_dispatch_sort(q, rq);
1139 1877
1140 if (cfq_cfqq_sync(cfqq)) 1878 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
1141 cfqd->sync_flight++; 1879 cfqq->nr_sectors += blk_rq_sectors(rq);
1142} 1880}
1143 1881
1144/* 1882/*
@@ -1175,6 +1913,187 @@ cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1175} 1913}
1176 1914
1177/* 1915/*
1916 * Must be called with the queue_lock held.
1917 */
1918static int cfqq_process_refs(struct cfq_queue *cfqq)
1919{
1920 int process_refs, io_refs;
1921
1922 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
1923 process_refs = atomic_read(&cfqq->ref) - io_refs;
1924 BUG_ON(process_refs < 0);
1925 return process_refs;
1926}
1927
1928static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
1929{
1930 int process_refs, new_process_refs;
1931 struct cfq_queue *__cfqq;
1932
1933 /* Avoid a circular list and skip interim queue merges */
1934 while ((__cfqq = new_cfqq->new_cfqq)) {
1935 if (__cfqq == cfqq)
1936 return;
1937 new_cfqq = __cfqq;
1938 }
1939
1940 process_refs = cfqq_process_refs(cfqq);
1941 /*
1942 * If the process for the cfqq has gone away, there is no
1943 * sense in merging the queues.
1944 */
1945 if (process_refs == 0)
1946 return;
1947
1948 /*
1949 * Merge in the direction of the lesser amount of work.
1950 */
1951 new_process_refs = cfqq_process_refs(new_cfqq);
1952 if (new_process_refs >= process_refs) {
1953 cfqq->new_cfqq = new_cfqq;
1954 atomic_add(process_refs, &new_cfqq->ref);
1955 } else {
1956 new_cfqq->new_cfqq = cfqq;
1957 atomic_add(new_process_refs, &cfqq->ref);
1958 }
1959}
1960
1961static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd,
1962 struct cfq_group *cfqg, enum wl_prio_t prio)
1963{
1964 struct cfq_queue *queue;
1965 int i;
1966 bool key_valid = false;
1967 unsigned long lowest_key = 0;
1968 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
1969
1970 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
1971 /* select the one with lowest rb_key */
1972 queue = cfq_rb_first(service_tree_for(cfqg, prio, i));
1973 if (queue &&
1974 (!key_valid || time_before(queue->rb_key, lowest_key))) {
1975 lowest_key = queue->rb_key;
1976 cur_best = i;
1977 key_valid = true;
1978 }
1979 }
1980
1981 return cur_best;
1982}
1983
1984static void choose_service_tree(struct cfq_data *cfqd, struct cfq_group *cfqg)
1985{
1986 unsigned slice;
1987 unsigned count;
1988 struct cfq_rb_root *st;
1989 unsigned group_slice;
1990
1991 if (!cfqg) {
1992 cfqd->serving_prio = IDLE_WORKLOAD;
1993 cfqd->workload_expires = jiffies + 1;
1994 return;
1995 }
1996
1997 /* Choose next priority. RT > BE > IDLE */
1998 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
1999 cfqd->serving_prio = RT_WORKLOAD;
2000 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
2001 cfqd->serving_prio = BE_WORKLOAD;
2002 else {
2003 cfqd->serving_prio = IDLE_WORKLOAD;
2004 cfqd->workload_expires = jiffies + 1;
2005 return;
2006 }
2007
2008 /*
2009 * For RT and BE, we have to choose also the type
2010 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
2011 * expiration time
2012 */
2013 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
2014 count = st->count;
2015
2016 /*
2017 * check workload expiration, and that we still have other queues ready
2018 */
2019 if (count && !time_after(jiffies, cfqd->workload_expires))
2020 return;
2021
2022 /* otherwise select new workload type */
2023 cfqd->serving_type =
2024 cfq_choose_wl(cfqd, cfqg, cfqd->serving_prio);
2025 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
2026 count = st->count;
2027
2028 /*
2029 * the workload slice is computed as a fraction of target latency
2030 * proportional to the number of queues in that workload, over
2031 * all the queues in the same priority class
2032 */
2033 group_slice = cfq_group_slice(cfqd, cfqg);
2034
2035 slice = group_slice * count /
2036 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio],
2037 cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd, cfqg));
2038
2039 if (cfqd->serving_type == ASYNC_WORKLOAD) {
2040 unsigned int tmp;
2041
2042 /*
2043 * Async queues are currently system wide. Just taking
2044 * proportion of queues with-in same group will lead to higher
2045 * async ratio system wide as generally root group is going
2046 * to have higher weight. A more accurate thing would be to
2047 * calculate system wide asnc/sync ratio.
2048 */
2049 tmp = cfq_target_latency * cfqg_busy_async_queues(cfqd, cfqg);
2050 tmp = tmp/cfqd->busy_queues;
2051 slice = min_t(unsigned, slice, tmp);
2052
2053 /* async workload slice is scaled down according to
2054 * the sync/async slice ratio. */
2055 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
2056 } else
2057 /* sync workload slice is at least 2 * cfq_slice_idle */
2058 slice = max(slice, 2 * cfqd->cfq_slice_idle);
2059
2060 slice = max_t(unsigned, slice, CFQ_MIN_TT);
2061 cfq_log(cfqd, "workload slice:%d", slice);
2062 cfqd->workload_expires = jiffies + slice;
2063 cfqd->noidle_tree_requires_idle = false;
2064}
2065
2066static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
2067{
2068 struct cfq_rb_root *st = &cfqd->grp_service_tree;
2069 struct cfq_group *cfqg;
2070
2071 if (RB_EMPTY_ROOT(&st->rb))
2072 return NULL;
2073 cfqg = cfq_rb_first_group(st);
2074 st->active = &cfqg->rb_node;
2075 update_min_vdisktime(st);
2076 return cfqg;
2077}
2078
2079static void cfq_choose_cfqg(struct cfq_data *cfqd)
2080{
2081 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
2082
2083 cfqd->serving_group = cfqg;
2084
2085 /* Restore the workload type data */
2086 if (cfqg->saved_workload_slice) {
2087 cfqd->workload_expires = jiffies + cfqg->saved_workload_slice;
2088 cfqd->serving_type = cfqg->saved_workload;
2089 cfqd->serving_prio = cfqg->saved_serving_prio;
2090 } else
2091 cfqd->workload_expires = jiffies - 1;
2092
2093 choose_service_tree(cfqd, cfqg);
2094}
2095
2096/*
1178 * Select a queue for service. If we have a current active queue, 2097 * Select a queue for service. If we have a current active queue,
1179 * check whether to continue servicing it, or retrieve and set a new one. 2098 * check whether to continue servicing it, or retrieve and set a new one.
1180 */ 2099 */
@@ -1186,13 +2105,37 @@ static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
1186 if (!cfqq) 2105 if (!cfqq)
1187 goto new_queue; 2106 goto new_queue;
1188 2107
2108 if (!cfqd->rq_queued)
2109 return NULL;
2110
1189 /* 2111 /*
1190 * The active queue has run out of time, expire it and select new. 2112 * We were waiting for group to get backlogged. Expire the queue
1191 */ 2113 */
1192 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) 2114 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
1193 goto expire; 2115 goto expire;
1194 2116
1195 /* 2117 /*
2118 * The active queue has run out of time, expire it and select new.
2119 */
2120 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
2121 /*
2122 * If slice had not expired at the completion of last request
2123 * we might not have turned on wait_busy flag. Don't expire
2124 * the queue yet. Allow the group to get backlogged.
2125 *
2126 * The very fact that we have used the slice, that means we
2127 * have been idling all along on this queue and it should be
2128 * ok to wait for this request to complete.
2129 */
2130 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
2131 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2132 cfqq = NULL;
2133 goto keep_queue;
2134 } else
2135 goto expire;
2136 }
2137
2138 /*
1196 * The active queue has requests and isn't expired, allow it to 2139 * The active queue has requests and isn't expired, allow it to
1197 * dispatch. 2140 * dispatch.
1198 */ 2141 */
@@ -1203,11 +2146,14 @@ static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
1203 * If another queue has a request waiting within our mean seek 2146 * If another queue has a request waiting within our mean seek
1204 * distance, let it run. The expire code will check for close 2147 * distance, let it run. The expire code will check for close
1205 * cooperators and put the close queue at the front of the service 2148 * cooperators and put the close queue at the front of the service
1206 * tree. 2149 * tree. If possible, merge the expiring queue with the new cfqq.
1207 */ 2150 */
1208 new_cfqq = cfq_close_cooperator(cfqd, cfqq, 0); 2151 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
1209 if (new_cfqq) 2152 if (new_cfqq) {
2153 if (!cfqq->new_cfqq)
2154 cfq_setup_merge(cfqq, new_cfqq);
1210 goto expire; 2155 goto expire;
2156 }
1211 2157
1212 /* 2158 /*
1213 * No requests pending. If the active queue still has requests in 2159 * No requests pending. If the active queue still has requests in
@@ -1215,7 +2161,7 @@ static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
1215 * conditions to happen (or time out) before selecting a new queue. 2161 * conditions to happen (or time out) before selecting a new queue.
1216 */ 2162 */
1217 if (timer_pending(&cfqd->idle_slice_timer) || 2163 if (timer_pending(&cfqd->idle_slice_timer) ||
1218 (cfqq->dispatched && cfq_cfqq_idle_window(cfqq))) { 2164 (cfqq->dispatched && cfq_should_idle(cfqd, cfqq))) {
1219 cfqq = NULL; 2165 cfqq = NULL;
1220 goto keep_queue; 2166 goto keep_queue;
1221 } 2167 }
@@ -1223,6 +2169,13 @@ static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
1223expire: 2169expire:
1224 cfq_slice_expired(cfqd, 0); 2170 cfq_slice_expired(cfqd, 0);
1225new_queue: 2171new_queue:
2172 /*
2173 * Current queue expired. Check if we have to switch to a new
2174 * service tree
2175 */
2176 if (!new_cfqq)
2177 cfq_choose_cfqg(cfqd);
2178
1226 cfqq = cfq_set_active_queue(cfqd, new_cfqq); 2179 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
1227keep_queue: 2180keep_queue:
1228 return cfqq; 2181 return cfqq;
@@ -1238,6 +2191,9 @@ static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
1238 } 2191 }
1239 2192
1240 BUG_ON(!list_empty(&cfqq->fifo)); 2193 BUG_ON(!list_empty(&cfqq->fifo));
2194
2195 /* By default cfqq is not expired if it is empty. Do it explicitly */
2196 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
1241 return dispatched; 2197 return dispatched;
1242} 2198}
1243 2199
@@ -1250,10 +2206,12 @@ static int cfq_forced_dispatch(struct cfq_data *cfqd)
1250 struct cfq_queue *cfqq; 2206 struct cfq_queue *cfqq;
1251 int dispatched = 0; 2207 int dispatched = 0;
1252 2208
1253 while ((cfqq = cfq_rb_first(&cfqd->service_tree)) != NULL) 2209 /* Expire the timeslice of the current active queue first */
1254 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
1255
1256 cfq_slice_expired(cfqd, 0); 2210 cfq_slice_expired(cfqd, 0);
2211 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
2212 __cfq_set_active_queue(cfqd, cfqq);
2213 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
2214 }
1257 2215
1258 BUG_ON(cfqd->busy_queues); 2216 BUG_ON(cfqd->busy_queues);
1259 2217
@@ -1261,6 +2219,19 @@ static int cfq_forced_dispatch(struct cfq_data *cfqd)
1261 return dispatched; 2219 return dispatched;
1262} 2220}
1263 2221
2222static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
2223 struct cfq_queue *cfqq)
2224{
2225 /* the queue hasn't finished any request, can't estimate */
2226 if (cfq_cfqq_slice_new(cfqq))
2227 return 1;
2228 if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched,
2229 cfqq->slice_end))
2230 return 1;
2231
2232 return 0;
2233}
2234
1264static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq) 2235static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1265{ 2236{
1266 unsigned int max_dispatch; 2237 unsigned int max_dispatch;
@@ -1268,16 +2239,16 @@ static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1268 /* 2239 /*
1269 * Drain async requests before we start sync IO 2240 * Drain async requests before we start sync IO
1270 */ 2241 */
1271 if (cfq_cfqq_idle_window(cfqq) && cfqd->rq_in_driver[BLK_RW_ASYNC]) 2242 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
1272 return false; 2243 return false;
1273 2244
1274 /* 2245 /*
1275 * If this is an async queue and we have sync IO in flight, let it wait 2246 * If this is an async queue and we have sync IO in flight, let it wait
1276 */ 2247 */
1277 if (cfqd->sync_flight && !cfq_cfqq_sync(cfqq)) 2248 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
1278 return false; 2249 return false;
1279 2250
1280 max_dispatch = cfqd->cfq_quantum; 2251 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
1281 if (cfq_class_idle(cfqq)) 2252 if (cfq_class_idle(cfqq))
1282 max_dispatch = 1; 2253 max_dispatch = 1;
1283 2254
@@ -1294,13 +2265,22 @@ static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1294 /* 2265 /*
1295 * We have other queues, don't allow more IO from this one 2266 * We have other queues, don't allow more IO from this one
1296 */ 2267 */
1297 if (cfqd->busy_queues > 1) 2268 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq))
1298 return false; 2269 return false;
1299 2270
1300 /* 2271 /*
1301 * Sole queue user, allow bigger slice 2272 * Sole queue user, no limit
1302 */ 2273 */
1303 max_dispatch *= 4; 2274 if (cfqd->busy_queues == 1)
2275 max_dispatch = -1;
2276 else
2277 /*
2278 * Normally we start throttling cfqq when cfq_quantum/2
2279 * requests have been dispatched. But we can drive
2280 * deeper queue depths at the beginning of slice
2281 * subjected to upper limit of cfq_quantum.
2282 * */
2283 max_dispatch = cfqd->cfq_quantum;
1304 } 2284 }
1305 2285
1306 /* 2286 /*
@@ -1309,7 +2289,7 @@ static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1309 * based on the last sync IO we serviced 2289 * based on the last sync IO we serviced
1310 */ 2290 */
1311 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) { 2291 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
1312 unsigned long last_sync = jiffies - cfqd->last_end_sync_rq; 2292 unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
1313 unsigned int depth; 2293 unsigned int depth;
1314 2294
1315 depth = last_sync / cfqd->cfq_slice[1]; 2295 depth = last_sync / cfqd->cfq_slice[1];
@@ -1407,11 +2387,13 @@ static int cfq_dispatch_requests(struct request_queue *q, int force)
1407 * task holds one reference to the queue, dropped when task exits. each rq 2387 * task holds one reference to the queue, dropped when task exits. each rq
1408 * in-flight on this queue also holds a reference, dropped when rq is freed. 2388 * in-flight on this queue also holds a reference, dropped when rq is freed.
1409 * 2389 *
2390 * Each cfq queue took a reference on the parent group. Drop it now.
1410 * queue lock must be held here. 2391 * queue lock must be held here.
1411 */ 2392 */
1412static void cfq_put_queue(struct cfq_queue *cfqq) 2393static void cfq_put_queue(struct cfq_queue *cfqq)
1413{ 2394{
1414 struct cfq_data *cfqd = cfqq->cfqd; 2395 struct cfq_data *cfqd = cfqq->cfqd;
2396 struct cfq_group *cfqg, *orig_cfqg;
1415 2397
1416 BUG_ON(atomic_read(&cfqq->ref) <= 0); 2398 BUG_ON(atomic_read(&cfqq->ref) <= 0);
1417 2399
@@ -1421,14 +2403,19 @@ static void cfq_put_queue(struct cfq_queue *cfqq)
1421 cfq_log_cfqq(cfqd, cfqq, "put_queue"); 2403 cfq_log_cfqq(cfqd, cfqq, "put_queue");
1422 BUG_ON(rb_first(&cfqq->sort_list)); 2404 BUG_ON(rb_first(&cfqq->sort_list));
1423 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]); 2405 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
1424 BUG_ON(cfq_cfqq_on_rr(cfqq)); 2406 cfqg = cfqq->cfqg;
2407 orig_cfqg = cfqq->orig_cfqg;
1425 2408
1426 if (unlikely(cfqd->active_queue == cfqq)) { 2409 if (unlikely(cfqd->active_queue == cfqq)) {
1427 __cfq_slice_expired(cfqd, cfqq, 0); 2410 __cfq_slice_expired(cfqd, cfqq, 0);
1428 cfq_schedule_dispatch(cfqd); 2411 cfq_schedule_dispatch(cfqd);
1429 } 2412 }
1430 2413
2414 BUG_ON(cfq_cfqq_on_rr(cfqq));
1431 kmem_cache_free(cfq_pool, cfqq); 2415 kmem_cache_free(cfq_pool, cfqq);
2416 cfq_put_cfqg(cfqg);
2417 if (orig_cfqg)
2418 cfq_put_cfqg(orig_cfqg);
1432} 2419}
1433 2420
1434/* 2421/*
@@ -1518,11 +2505,29 @@ static void cfq_free_io_context(struct io_context *ioc)
1518 2505
1519static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq) 2506static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1520{ 2507{
2508 struct cfq_queue *__cfqq, *next;
2509
1521 if (unlikely(cfqq == cfqd->active_queue)) { 2510 if (unlikely(cfqq == cfqd->active_queue)) {
1522 __cfq_slice_expired(cfqd, cfqq, 0); 2511 __cfq_slice_expired(cfqd, cfqq, 0);
1523 cfq_schedule_dispatch(cfqd); 2512 cfq_schedule_dispatch(cfqd);
1524 } 2513 }
1525 2514
2515 /*
2516 * If this queue was scheduled to merge with another queue, be
2517 * sure to drop the reference taken on that queue (and others in
2518 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
2519 */
2520 __cfqq = cfqq->new_cfqq;
2521 while (__cfqq) {
2522 if (__cfqq == cfqq) {
2523 WARN(1, "cfqq->new_cfqq loop detected\n");
2524 break;
2525 }
2526 next = __cfqq->new_cfqq;
2527 cfq_put_queue(__cfqq);
2528 __cfqq = next;
2529 }
2530
1526 cfq_put_queue(cfqq); 2531 cfq_put_queue(cfqq);
1527} 2532}
1528 2533
@@ -1703,14 +2708,51 @@ static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1703 cfqq->pid = pid; 2708 cfqq->pid = pid;
1704} 2709}
1705 2710
2711#ifdef CONFIG_CFQ_GROUP_IOSCHED
2712static void changed_cgroup(struct io_context *ioc, struct cfq_io_context *cic)
2713{
2714 struct cfq_queue *sync_cfqq = cic_to_cfqq(cic, 1);
2715 struct cfq_data *cfqd = cic->key;
2716 unsigned long flags;
2717 struct request_queue *q;
2718
2719 if (unlikely(!cfqd))
2720 return;
2721
2722 q = cfqd->queue;
2723
2724 spin_lock_irqsave(q->queue_lock, flags);
2725
2726 if (sync_cfqq) {
2727 /*
2728 * Drop reference to sync queue. A new sync queue will be
2729 * assigned in new group upon arrival of a fresh request.
2730 */
2731 cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
2732 cic_set_cfqq(cic, NULL, 1);
2733 cfq_put_queue(sync_cfqq);
2734 }
2735
2736 spin_unlock_irqrestore(q->queue_lock, flags);
2737}
2738
2739static void cfq_ioc_set_cgroup(struct io_context *ioc)
2740{
2741 call_for_each_cic(ioc, changed_cgroup);
2742 ioc->cgroup_changed = 0;
2743}
2744#endif /* CONFIG_CFQ_GROUP_IOSCHED */
2745
1706static struct cfq_queue * 2746static struct cfq_queue *
1707cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync, 2747cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync,
1708 struct io_context *ioc, gfp_t gfp_mask) 2748 struct io_context *ioc, gfp_t gfp_mask)
1709{ 2749{
1710 struct cfq_queue *cfqq, *new_cfqq = NULL; 2750 struct cfq_queue *cfqq, *new_cfqq = NULL;
1711 struct cfq_io_context *cic; 2751 struct cfq_io_context *cic;
2752 struct cfq_group *cfqg;
1712 2753
1713retry: 2754retry:
2755 cfqg = cfq_get_cfqg(cfqd, 1);
1714 cic = cfq_cic_lookup(cfqd, ioc); 2756 cic = cfq_cic_lookup(cfqd, ioc);
1715 /* cic always exists here */ 2757 /* cic always exists here */
1716 cfqq = cic_to_cfqq(cic, is_sync); 2758 cfqq = cic_to_cfqq(cic, is_sync);
@@ -1741,6 +2783,7 @@ retry:
1741 if (cfqq) { 2783 if (cfqq) {
1742 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync); 2784 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
1743 cfq_init_prio_data(cfqq, ioc); 2785 cfq_init_prio_data(cfqq, ioc);
2786 cfq_link_cfqq_cfqg(cfqq, cfqg);
1744 cfq_log_cfqq(cfqd, cfqq, "alloced"); 2787 cfq_log_cfqq(cfqd, cfqq, "alloced");
1745 } else 2788 } else
1746 cfqq = &cfqd->oom_cfqq; 2789 cfqq = &cfqd->oom_cfqq;
@@ -1932,6 +2975,10 @@ out:
1932 if (unlikely(ioc->ioprio_changed)) 2975 if (unlikely(ioc->ioprio_changed))
1933 cfq_ioc_set_ioprio(ioc); 2976 cfq_ioc_set_ioprio(ioc);
1934 2977
2978#ifdef CONFIG_CFQ_GROUP_IOSCHED
2979 if (unlikely(ioc->cgroup_changed))
2980 cfq_ioc_set_cgroup(ioc);
2981#endif
1935 return cic; 2982 return cic;
1936err_free: 2983err_free:
1937 cfq_cic_free(cic); 2984 cfq_cic_free(cic);
@@ -1952,33 +2999,23 @@ cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1952} 2999}
1953 3000
1954static void 3001static void
1955cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic, 3002cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1956 struct request *rq) 3003 struct request *rq)
1957{ 3004{
1958 sector_t sdist; 3005 sector_t sdist = 0;
1959 u64 total; 3006 sector_t n_sec = blk_rq_sectors(rq);
3007 if (cfqq->last_request_pos) {
3008 if (cfqq->last_request_pos < blk_rq_pos(rq))
3009 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
3010 else
3011 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
3012 }
1960 3013
1961 if (!cic->last_request_pos) 3014 cfqq->seek_history <<= 1;
1962 sdist = 0; 3015 if (blk_queue_nonrot(cfqd->queue))
1963 else if (cic->last_request_pos < blk_rq_pos(rq)) 3016 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
1964 sdist = blk_rq_pos(rq) - cic->last_request_pos;
1965 else 3017 else
1966 sdist = cic->last_request_pos - blk_rq_pos(rq); 3018 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
1967
1968 /*
1969 * Don't allow the seek distance to get too large from the
1970 * odd fragment, pagein, etc
1971 */
1972 if (cic->seek_samples <= 60) /* second&third seek */
1973 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1974 else
1975 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1976
1977 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1978 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1979 total = cic->seek_total + (cic->seek_samples/2);
1980 do_div(total, cic->seek_samples);
1981 cic->seek_mean = (sector_t)total;
1982} 3019}
1983 3020
1984/* 3021/*
@@ -1999,14 +3036,14 @@ cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1999 3036
2000 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq); 3037 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
2001 3038
3039 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3040 cfq_mark_cfqq_deep(cfqq);
3041
2002 if (!atomic_read(&cic->ioc->nr_tasks) || !cfqd->cfq_slice_idle || 3042 if (!atomic_read(&cic->ioc->nr_tasks) || !cfqd->cfq_slice_idle ||
2003 (!cfqd->cfq_latency && cfqd->hw_tag && CIC_SEEKY(cic))) 3043 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
2004 enable_idle = 0; 3044 enable_idle = 0;
2005 else if (sample_valid(cic->ttime_samples)) { 3045 else if (sample_valid(cic->ttime_samples)) {
2006 unsigned int slice_idle = cfqd->cfq_slice_idle; 3046 if (cic->ttime_mean > cfqd->cfq_slice_idle)
2007 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
2008 slice_idle = msecs_to_jiffies(CFQ_MIN_TT);
2009 if (cic->ttime_mean > slice_idle)
2010 enable_idle = 0; 3047 enable_idle = 0;
2011 else 3048 else
2012 enable_idle = 1; 3049 enable_idle = 1;
@@ -2035,9 +3072,6 @@ cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
2035 if (!cfqq) 3072 if (!cfqq)
2036 return false; 3073 return false;
2037 3074
2038 if (cfq_slice_used(cfqq))
2039 return true;
2040
2041 if (cfq_class_idle(new_cfqq)) 3075 if (cfq_class_idle(new_cfqq))
2042 return false; 3076 return false;
2043 3077
@@ -2045,12 +3079,31 @@ cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
2045 return true; 3079 return true;
2046 3080
2047 /* 3081 /*
3082 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3083 */
3084 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
3085 return false;
3086
3087 /*
2048 * if the new request is sync, but the currently running queue is 3088 * if the new request is sync, but the currently running queue is
2049 * not, let the sync request have priority. 3089 * not, let the sync request have priority.
2050 */ 3090 */
2051 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq)) 3091 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
2052 return true; 3092 return true;
2053 3093
3094 if (new_cfqq->cfqg != cfqq->cfqg)
3095 return false;
3096
3097 if (cfq_slice_used(cfqq))
3098 return true;
3099
3100 /* Allow preemption only if we are idling on sync-noidle tree */
3101 if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD &&
3102 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
3103 new_cfqq->service_tree->count == 2 &&
3104 RB_EMPTY_ROOT(&cfqq->sort_list))
3105 return true;
3106
2054 /* 3107 /*
2055 * So both queues are sync. Let the new request get disk time if 3108 * So both queues are sync. Let the new request get disk time if
2056 * it's a metadata request and the current queue is doing regular IO. 3109 * it's a metadata request and the current queue is doing regular IO.
@@ -2071,16 +3124,8 @@ cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
2071 * if this request is as-good as one we would expect from the 3124 * if this request is as-good as one we would expect from the
2072 * current cfqq, let it preempt 3125 * current cfqq, let it preempt
2073 */ 3126 */
2074 if (cfq_rq_close(cfqd, rq) && (!cfq_cfqq_coop(new_cfqq) || 3127 if (cfq_rq_close(cfqd, cfqq, rq))
2075 cfqd->busy_queues == 1)) {
2076 /*
2077 * Mark new queue coop_preempt, so its coop flag will not be
2078 * cleared when new queue gets scheduled at the very first time
2079 */
2080 cfq_mark_cfqq_coop_preempt(new_cfqq);
2081 cfq_mark_cfqq_coop(new_cfqq);
2082 return true; 3128 return true;
2083 }
2084 3129
2085 return false; 3130 return false;
2086} 3131}
@@ -2121,10 +3166,10 @@ cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2121 cfqq->meta_pending++; 3166 cfqq->meta_pending++;
2122 3167
2123 cfq_update_io_thinktime(cfqd, cic); 3168 cfq_update_io_thinktime(cfqd, cic);
2124 cfq_update_io_seektime(cfqd, cic, rq); 3169 cfq_update_io_seektime(cfqd, cfqq, rq);
2125 cfq_update_idle_window(cfqd, cfqq, cic); 3170 cfq_update_idle_window(cfqd, cfqq, cic);
2126 3171
2127 cic->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq); 3172 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
2128 3173
2129 if (cfqq == cfqd->active_queue) { 3174 if (cfqq == cfqd->active_queue) {
2130 /* 3175 /*
@@ -2141,9 +3186,10 @@ cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2141 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE || 3186 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
2142 cfqd->busy_queues > 1) { 3187 cfqd->busy_queues > 1) {
2143 del_timer(&cfqd->idle_slice_timer); 3188 del_timer(&cfqd->idle_slice_timer);
2144 __blk_run_queue(cfqd->queue); 3189 cfq_clear_cfqq_wait_request(cfqq);
2145 } 3190 __blk_run_queue(cfqd->queue);
2146 cfq_mark_cfqq_must_dispatch(cfqq); 3191 } else
3192 cfq_mark_cfqq_must_dispatch(cfqq);
2147 } 3193 }
2148 } else if (cfq_should_preempt(cfqd, cfqq, rq)) { 3194 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
2149 /* 3195 /*
@@ -2165,10 +3211,9 @@ static void cfq_insert_request(struct request_queue *q, struct request *rq)
2165 cfq_log_cfqq(cfqd, cfqq, "insert_request"); 3211 cfq_log_cfqq(cfqd, cfqq, "insert_request");
2166 cfq_init_prio_data(cfqq, RQ_CIC(rq)->ioc); 3212 cfq_init_prio_data(cfqq, RQ_CIC(rq)->ioc);
2167 3213
2168 cfq_add_rq_rb(rq);
2169
2170 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]); 3214 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
2171 list_add_tail(&rq->queuelist, &cfqq->fifo); 3215 list_add_tail(&rq->queuelist, &cfqq->fifo);
3216 cfq_add_rq_rb(rq);
2172 3217
2173 cfq_rq_enqueued(cfqd, cfqq, rq); 3218 cfq_rq_enqueued(cfqd, cfqq, rq);
2174} 3219}
@@ -2179,23 +3224,64 @@ static void cfq_insert_request(struct request_queue *q, struct request *rq)
2179 */ 3224 */
2180static void cfq_update_hw_tag(struct cfq_data *cfqd) 3225static void cfq_update_hw_tag(struct cfq_data *cfqd)
2181{ 3226{
2182 if (rq_in_driver(cfqd) > cfqd->rq_in_driver_peak) 3227 struct cfq_queue *cfqq = cfqd->active_queue;
2183 cfqd->rq_in_driver_peak = rq_in_driver(cfqd); 3228
3229 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
3230 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
3231
3232 if (cfqd->hw_tag == 1)
3233 return;
2184 3234
2185 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN && 3235 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
2186 rq_in_driver(cfqd) <= CFQ_HW_QUEUE_MIN) 3236 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
3237 return;
3238
3239 /*
3240 * If active queue hasn't enough requests and can idle, cfq might not
3241 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3242 * case
3243 */
3244 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3245 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
3246 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
2187 return; 3247 return;
2188 3248
2189 if (cfqd->hw_tag_samples++ < 50) 3249 if (cfqd->hw_tag_samples++ < 50)
2190 return; 3250 return;
2191 3251
2192 if (cfqd->rq_in_driver_peak >= CFQ_HW_QUEUE_MIN) 3252 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
2193 cfqd->hw_tag = 1; 3253 cfqd->hw_tag = 1;
2194 else 3254 else
2195 cfqd->hw_tag = 0; 3255 cfqd->hw_tag = 0;
3256}
3257
3258static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3259{
3260 struct cfq_io_context *cic = cfqd->active_cic;
3261
3262 /* If there are other queues in the group, don't wait */
3263 if (cfqq->cfqg->nr_cfqq > 1)
3264 return false;
2196 3265
2197 cfqd->hw_tag_samples = 0; 3266 if (cfq_slice_used(cfqq))
2198 cfqd->rq_in_driver_peak = 0; 3267 return true;
3268
3269 /* if slice left is less than think time, wait busy */
3270 if (cic && sample_valid(cic->ttime_samples)
3271 && (cfqq->slice_end - jiffies < cic->ttime_mean))
3272 return true;
3273
3274 /*
3275 * If think times is less than a jiffy than ttime_mean=0 and above
3276 * will not be true. It might happen that slice has not expired yet
3277 * but will expire soon (4-5 ns) during select_queue(). To cover the
3278 * case where think time is less than a jiffy, mark the queue wait
3279 * busy if only 1 jiffy is left in the slice.
3280 */
3281 if (cfqq->slice_end - jiffies == 1)
3282 return true;
3283
3284 return false;
2199} 3285}
2200 3286
2201static void cfq_completed_request(struct request_queue *q, struct request *rq) 3287static void cfq_completed_request(struct request_queue *q, struct request *rq)
@@ -2206,21 +3292,21 @@ static void cfq_completed_request(struct request_queue *q, struct request *rq)
2206 unsigned long now; 3292 unsigned long now;
2207 3293
2208 now = jiffies; 3294 now = jiffies;
2209 cfq_log_cfqq(cfqd, cfqq, "complete"); 3295 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", !!rq_noidle(rq));
2210 3296
2211 cfq_update_hw_tag(cfqd); 3297 cfq_update_hw_tag(cfqd);
2212 3298
2213 WARN_ON(!cfqd->rq_in_driver[sync]); 3299 WARN_ON(!cfqd->rq_in_driver);
2214 WARN_ON(!cfqq->dispatched); 3300 WARN_ON(!cfqq->dispatched);
2215 cfqd->rq_in_driver[sync]--; 3301 cfqd->rq_in_driver--;
2216 cfqq->dispatched--; 3302 cfqq->dispatched--;
2217 3303
2218 if (cfq_cfqq_sync(cfqq)) 3304 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
2219 cfqd->sync_flight--;
2220 3305
2221 if (sync) { 3306 if (sync) {
2222 RQ_CIC(rq)->last_end_request = now; 3307 RQ_CIC(rq)->last_end_request = now;
2223 cfqd->last_end_sync_rq = now; 3308 if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
3309 cfqd->last_delayed_sync = now;
2224 } 3310 }
2225 3311
2226 /* 3312 /*
@@ -2234,21 +3320,43 @@ static void cfq_completed_request(struct request_queue *q, struct request *rq)
2234 cfq_set_prio_slice(cfqd, cfqq); 3320 cfq_set_prio_slice(cfqd, cfqq);
2235 cfq_clear_cfqq_slice_new(cfqq); 3321 cfq_clear_cfqq_slice_new(cfqq);
2236 } 3322 }
3323
3324 /*
3325 * Should we wait for next request to come in before we expire
3326 * the queue.
3327 */
3328 if (cfq_should_wait_busy(cfqd, cfqq)) {
3329 cfqq->slice_end = jiffies + cfqd->cfq_slice_idle;
3330 cfq_mark_cfqq_wait_busy(cfqq);
3331 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
3332 }
3333
2237 /* 3334 /*
2238 * If there are no requests waiting in this queue, and 3335 * Idling is not enabled on:
2239 * there are other queues ready to issue requests, AND 3336 * - expired queues
2240 * those other queues are issuing requests within our 3337 * - idle-priority queues
2241 * mean seek distance, give them a chance to run instead 3338 * - async queues
2242 * of idling. 3339 * - queues with still some requests queued
3340 * - when there is a close cooperator
2243 */ 3341 */
2244 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq)) 3342 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
2245 cfq_slice_expired(cfqd, 1); 3343 cfq_slice_expired(cfqd, 1);
2246 else if (cfqq_empty && !cfq_close_cooperator(cfqd, cfqq, 1) && 3344 else if (sync && cfqq_empty &&
2247 sync && !rq_noidle(rq)) 3345 !cfq_close_cooperator(cfqd, cfqq)) {
2248 cfq_arm_slice_timer(cfqd); 3346 cfqd->noidle_tree_requires_idle |= !rq_noidle(rq);
3347 /*
3348 * Idling is enabled for SYNC_WORKLOAD.
3349 * SYNC_NOIDLE_WORKLOAD idles at the end of the tree
3350 * only if we processed at least one !rq_noidle request
3351 */
3352 if (cfqd->serving_type == SYNC_WORKLOAD
3353 || cfqd->noidle_tree_requires_idle
3354 || cfqq->cfqg->nr_cfqq == 1)
3355 cfq_arm_slice_timer(cfqd);
3356 }
2249 } 3357 }
2250 3358
2251 if (!rq_in_driver(cfqd)) 3359 if (!cfqd->rq_in_driver)
2252 cfq_schedule_dispatch(cfqd); 3360 cfq_schedule_dispatch(cfqd);
2253} 3361}
2254 3362
@@ -2269,12 +3377,10 @@ static void cfq_prio_boost(struct cfq_queue *cfqq)
2269 cfqq->ioprio = IOPRIO_NORM; 3377 cfqq->ioprio = IOPRIO_NORM;
2270 } else { 3378 } else {
2271 /* 3379 /*
2272 * check if we need to unboost the queue 3380 * unboost the queue (if needed)
2273 */ 3381 */
2274 if (cfqq->ioprio_class != cfqq->org_ioprio_class) 3382 cfqq->ioprio_class = cfqq->org_ioprio_class;
2275 cfqq->ioprio_class = cfqq->org_ioprio_class; 3383 cfqq->ioprio = cfqq->org_ioprio;
2276 if (cfqq->ioprio != cfqq->org_ioprio)
2277 cfqq->ioprio = cfqq->org_ioprio;
2278 } 3384 }
2279} 3385}
2280 3386
@@ -2338,6 +3444,35 @@ static void cfq_put_request(struct request *rq)
2338 } 3444 }
2339} 3445}
2340 3446
3447static struct cfq_queue *
3448cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_context *cic,
3449 struct cfq_queue *cfqq)
3450{
3451 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
3452 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
3453 cfq_mark_cfqq_coop(cfqq->new_cfqq);
3454 cfq_put_queue(cfqq);
3455 return cic_to_cfqq(cic, 1);
3456}
3457
3458/*
3459 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
3460 * was the last process referring to said cfqq.
3461 */
3462static struct cfq_queue *
3463split_cfqq(struct cfq_io_context *cic, struct cfq_queue *cfqq)
3464{
3465 if (cfqq_process_refs(cfqq) == 1) {
3466 cfqq->pid = current->pid;
3467 cfq_clear_cfqq_coop(cfqq);
3468 cfq_clear_cfqq_split_coop(cfqq);
3469 return cfqq;
3470 }
3471
3472 cic_set_cfqq(cic, NULL, 1);
3473 cfq_put_queue(cfqq);
3474 return NULL;
3475}
2341/* 3476/*
2342 * Allocate cfq data structures associated with this request. 3477 * Allocate cfq data structures associated with this request.
2343 */ 3478 */
@@ -2360,10 +3495,30 @@ cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
2360 if (!cic) 3495 if (!cic)
2361 goto queue_fail; 3496 goto queue_fail;
2362 3497
3498new_queue:
2363 cfqq = cic_to_cfqq(cic, is_sync); 3499 cfqq = cic_to_cfqq(cic, is_sync);
2364 if (!cfqq || cfqq == &cfqd->oom_cfqq) { 3500 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
2365 cfqq = cfq_get_queue(cfqd, is_sync, cic->ioc, gfp_mask); 3501 cfqq = cfq_get_queue(cfqd, is_sync, cic->ioc, gfp_mask);
2366 cic_set_cfqq(cic, cfqq, is_sync); 3502 cic_set_cfqq(cic, cfqq, is_sync);
3503 } else {
3504 /*
3505 * If the queue was seeky for too long, break it apart.
3506 */
3507 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
3508 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
3509 cfqq = split_cfqq(cic, cfqq);
3510 if (!cfqq)
3511 goto new_queue;
3512 }
3513
3514 /*
3515 * Check to see if this queue is scheduled to merge with
3516 * another, closely cooperating queue. The merging of
3517 * queues happens here as it must be done in process context.
3518 * The reference on new_cfqq was taken in merge_cfqqs.
3519 */
3520 if (cfqq->new_cfqq)
3521 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
2367 } 3522 }
2368 3523
2369 cfqq->allocated[rw]++; 3524 cfqq->allocated[rw]++;
@@ -2438,6 +3593,11 @@ static void cfq_idle_slice_timer(unsigned long data)
2438 */ 3593 */
2439 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) 3594 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
2440 goto out_kick; 3595 goto out_kick;
3596
3597 /*
3598 * Queue depth flag is reset only when the idle didn't succeed
3599 */
3600 cfq_clear_cfqq_deep(cfqq);
2441 } 3601 }
2442expire: 3602expire:
2443 cfq_slice_expired(cfqd, timed_out); 3603 cfq_slice_expired(cfqd, timed_out);
@@ -2468,6 +3628,11 @@ static void cfq_put_async_queues(struct cfq_data *cfqd)
2468 cfq_put_queue(cfqd->async_idle_cfqq); 3628 cfq_put_queue(cfqd->async_idle_cfqq);
2469} 3629}
2470 3630
3631static void cfq_cfqd_free(struct rcu_head *head)
3632{
3633 kfree(container_of(head, struct cfq_data, rcu));
3634}
3635
2471static void cfq_exit_queue(struct elevator_queue *e) 3636static void cfq_exit_queue(struct elevator_queue *e)
2472{ 3637{
2473 struct cfq_data *cfqd = e->elevator_data; 3638 struct cfq_data *cfqd = e->elevator_data;
@@ -2489,25 +3654,51 @@ static void cfq_exit_queue(struct elevator_queue *e)
2489 } 3654 }
2490 3655
2491 cfq_put_async_queues(cfqd); 3656 cfq_put_async_queues(cfqd);
3657 cfq_release_cfq_groups(cfqd);
3658 blkiocg_del_blkio_group(&cfqd->root_group.blkg);
2492 3659
2493 spin_unlock_irq(q->queue_lock); 3660 spin_unlock_irq(q->queue_lock);
2494 3661
2495 cfq_shutdown_timer_wq(cfqd); 3662 cfq_shutdown_timer_wq(cfqd);
2496 3663
2497 kfree(cfqd); 3664 /* Wait for cfqg->blkg->key accessors to exit their grace periods. */
3665 call_rcu(&cfqd->rcu, cfq_cfqd_free);
2498} 3666}
2499 3667
2500static void *cfq_init_queue(struct request_queue *q) 3668static void *cfq_init_queue(struct request_queue *q)
2501{ 3669{
2502 struct cfq_data *cfqd; 3670 struct cfq_data *cfqd;
2503 int i; 3671 int i, j;
3672 struct cfq_group *cfqg;
3673 struct cfq_rb_root *st;
2504 3674
2505 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node); 3675 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
2506 if (!cfqd) 3676 if (!cfqd)
2507 return NULL; 3677 return NULL;
2508 3678
2509 cfqd->service_tree = CFQ_RB_ROOT; 3679 /* Init root service tree */
3680 cfqd->grp_service_tree = CFQ_RB_ROOT;
2510 3681
3682 /* Init root group */
3683 cfqg = &cfqd->root_group;
3684 for_each_cfqg_st(cfqg, i, j, st)
3685 *st = CFQ_RB_ROOT;
3686 RB_CLEAR_NODE(&cfqg->rb_node);
3687
3688 /* Give preference to root group over other groups */
3689 cfqg->weight = 2*BLKIO_WEIGHT_DEFAULT;
3690
3691#ifdef CONFIG_CFQ_GROUP_IOSCHED
3692 /*
3693 * Take a reference to root group which we never drop. This is just
3694 * to make sure that cfq_put_cfqg() does not try to kfree root group
3695 */
3696 atomic_set(&cfqg->ref, 1);
3697 rcu_read_lock();
3698 blkiocg_add_blkio_group(&blkio_root_cgroup, &cfqg->blkg, (void *)cfqd,
3699 0);
3700 rcu_read_unlock();
3701#endif
2511 /* 3702 /*
2512 * Not strictly needed (since RB_ROOT just clears the node and we 3703 * Not strictly needed (since RB_ROOT just clears the node and we
2513 * zeroed cfqd on alloc), but better be safe in case someone decides 3704 * zeroed cfqd on alloc), but better be safe in case someone decides
@@ -2523,6 +3714,7 @@ static void *cfq_init_queue(struct request_queue *q)
2523 */ 3714 */
2524 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0); 3715 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
2525 atomic_inc(&cfqd->oom_cfqq.ref); 3716 atomic_inc(&cfqd->oom_cfqq.ref);
3717 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, &cfqd->root_group);
2526 3718
2527 INIT_LIST_HEAD(&cfqd->cic_list); 3719 INIT_LIST_HEAD(&cfqd->cic_list);
2528 3720
@@ -2544,8 +3736,14 @@ static void *cfq_init_queue(struct request_queue *q)
2544 cfqd->cfq_slice_async_rq = cfq_slice_async_rq; 3736 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2545 cfqd->cfq_slice_idle = cfq_slice_idle; 3737 cfqd->cfq_slice_idle = cfq_slice_idle;
2546 cfqd->cfq_latency = 1; 3738 cfqd->cfq_latency = 1;
2547 cfqd->hw_tag = 1; 3739 cfqd->cfq_group_isolation = 0;
2548 cfqd->last_end_sync_rq = jiffies; 3740 cfqd->hw_tag = -1;
3741 /*
3742 * we optimistically start assuming sync ops weren't delayed in last
3743 * second, in order to have larger depth for async operations.
3744 */
3745 cfqd->last_delayed_sync = jiffies - HZ;
3746 INIT_RCU_HEAD(&cfqd->rcu);
2549 return cfqd; 3747 return cfqd;
2550} 3748}
2551 3749
@@ -2614,6 +3812,7 @@ SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2614SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1); 3812SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2615SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0); 3813SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2616SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0); 3814SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
3815SHOW_FUNCTION(cfq_group_isolation_show, cfqd->cfq_group_isolation, 0);
2617#undef SHOW_FUNCTION 3816#undef SHOW_FUNCTION
2618 3817
2619#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \ 3818#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
@@ -2646,6 +3845,7 @@ STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2646STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, 3845STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
2647 UINT_MAX, 0); 3846 UINT_MAX, 0);
2648STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0); 3847STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
3848STORE_FUNCTION(cfq_group_isolation_store, &cfqd->cfq_group_isolation, 0, 1, 0);
2649#undef STORE_FUNCTION 3849#undef STORE_FUNCTION
2650 3850
2651#define CFQ_ATTR(name) \ 3851#define CFQ_ATTR(name) \
@@ -2662,6 +3862,7 @@ static struct elv_fs_entry cfq_attrs[] = {
2662 CFQ_ATTR(slice_async_rq), 3862 CFQ_ATTR(slice_async_rq),
2663 CFQ_ATTR(slice_idle), 3863 CFQ_ATTR(slice_idle),
2664 CFQ_ATTR(low_latency), 3864 CFQ_ATTR(low_latency),
3865 CFQ_ATTR(group_isolation),
2665 __ATTR_NULL 3866 __ATTR_NULL
2666}; 3867};
2667 3868
@@ -2691,6 +3892,17 @@ static struct elevator_type iosched_cfq = {
2691 .elevator_owner = THIS_MODULE, 3892 .elevator_owner = THIS_MODULE,
2692}; 3893};
2693 3894
3895#ifdef CONFIG_CFQ_GROUP_IOSCHED
3896static struct blkio_policy_type blkio_policy_cfq = {
3897 .ops = {
3898 .blkio_unlink_group_fn = cfq_unlink_blkio_group,
3899 .blkio_update_group_weight_fn = cfq_update_blkio_group_weight,
3900 },
3901};
3902#else
3903static struct blkio_policy_type blkio_policy_cfq;
3904#endif
3905
2694static int __init cfq_init(void) 3906static int __init cfq_init(void)
2695{ 3907{
2696 /* 3908 /*
@@ -2705,6 +3917,7 @@ static int __init cfq_init(void)
2705 return -ENOMEM; 3917 return -ENOMEM;
2706 3918
2707 elv_register(&iosched_cfq); 3919 elv_register(&iosched_cfq);
3920 blkio_policy_register(&blkio_policy_cfq);
2708 3921
2709 return 0; 3922 return 0;
2710} 3923}
@@ -2712,6 +3925,7 @@ static int __init cfq_init(void)
2712static void __exit cfq_exit(void) 3925static void __exit cfq_exit(void)
2713{ 3926{
2714 DECLARE_COMPLETION_ONSTACK(all_gone); 3927 DECLARE_COMPLETION_ONSTACK(all_gone);
3928 blkio_policy_unregister(&blkio_policy_cfq);
2715 elv_unregister(&iosched_cfq); 3929 elv_unregister(&iosched_cfq);
2716 ioc_gone = &all_gone; 3930 ioc_gone = &all_gone;
2717 /* ioc_gone's update must be visible before reading ioc_count */ 3931 /* ioc_gone's update must be visible before reading ioc_count */