2 * CFQ, or complete fairness queueing, disk scheduler.
4 * Based on ideas from a previously unfinished io
5 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
7 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/blkdev.h>
12 #include <linux/elevator.h>
13 #include <linux/jiffies.h>
14 #include <linux/rbtree.h>
15 #include <linux/ioprio.h>
16 #include <linux/blktrace_api.h>
20 static struct blkio_policy_type blkio_policy_cfq;
25 /* max queue in one round of service */
26 static const int cfq_quantum = 8;
27 static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
28 /* maximum backwards seek, in KiB */
29 static const int cfq_back_max = 16 * 1024;
30 /* penalty of a backwards seek */
31 static const int cfq_back_penalty = 2;
32 static const int cfq_slice_sync = HZ / 10;
33 static int cfq_slice_async = HZ / 25;
34 static const int cfq_slice_async_rq = 2;
35 static int cfq_slice_idle = HZ / 125;
36 static int cfq_group_idle = HZ / 125;
37 static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
38 static const int cfq_hist_divisor = 4;
41 * offset from end of service tree
43 #define CFQ_IDLE_DELAY (HZ / 5)
46 * below this threshold, we consider thinktime immediate
48 #define CFQ_MIN_TT (2)
50 #define CFQ_SLICE_SCALE (5)
51 #define CFQ_HW_QUEUE_MIN (5)
52 #define CFQ_SERVICE_SHIFT 12
54 #define CFQQ_SEEK_THR (sector_t)(8 * 100)
55 #define CFQQ_CLOSE_THR (sector_t)(8 * 1024)
56 #define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
57 #define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8)
59 #define RQ_CIC(rq) icq_to_cic((rq)->elv.icq)
60 #define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0])
61 #define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1])
63 static struct kmem_cache *cfq_pool;
65 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
66 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
67 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
69 #define sample_valid(samples) ((samples) > 80)
70 #define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
73 unsigned long last_end_request;
75 unsigned long ttime_total;
76 unsigned long ttime_samples;
77 unsigned long ttime_mean;
81 * Most of our rbtree usage is for sorting with min extraction, so
82 * if we cache the leftmost node we don't have to walk down the tree
83 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
84 * move this into the elevator for the rq sorting as well.
90 unsigned total_weight;
92 struct cfq_ttime ttime;
94 #define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
95 .ttime = {.last_end_request = jiffies,},}
98 * Per process-grouping structure
101 /* reference count */
103 /* various state flags, see below */
105 /* parent cfq_data */
106 struct cfq_data *cfqd;
107 /* service_tree member */
108 struct rb_node rb_node;
109 /* service_tree key */
110 unsigned long rb_key;
111 /* prio tree member */
112 struct rb_node p_node;
113 /* prio tree root we belong to, if any */
114 struct rb_root *p_root;
115 /* sorted list of pending requests */
116 struct rb_root sort_list;
117 /* if fifo isn't expired, next request to serve */
118 struct request *next_rq;
119 /* requests queued in sort_list */
121 /* currently allocated requests */
123 /* fifo list of requests in sort_list */
124 struct list_head fifo;
126 /* time when queue got scheduled in to dispatch first request. */
127 unsigned long dispatch_start;
128 unsigned int allocated_slice;
129 unsigned int slice_dispatch;
130 /* time when first request from queue completed and slice started. */
131 unsigned long slice_start;
132 unsigned long slice_end;
135 /* pending priority requests */
137 /* number of requests that are on the dispatch list or inside driver */
140 /* io prio of this group */
141 unsigned short ioprio, org_ioprio;
142 unsigned short ioprio_class;
147 sector_t last_request_pos;
149 struct cfq_rb_root *service_tree;
150 struct cfq_queue *new_cfqq;
151 struct cfq_group *cfqg;
152 /* Number of sectors dispatched from queue in single dispatch round */
153 unsigned long nr_sectors;
157 * First index in the service_trees.
158 * IDLE is handled separately, so it has negative index
168 * Second index in the service_trees.
172 SYNC_NOIDLE_WORKLOAD = 1,
176 /* This is per cgroup per device grouping structure */
178 /* group service_tree member */
179 struct rb_node rb_node;
181 /* group service_tree key */
184 unsigned int new_weight;
187 /* number of cfqq currently on this group */
191 * Per group busy queues average. Useful for workload slice calc. We
192 * create the array for each prio class but at run time it is used
193 * only for RT and BE class and slot for IDLE class remains unused.
194 * This is primarily done to avoid confusion and a gcc warning.
196 unsigned int busy_queues_avg[CFQ_PRIO_NR];
198 * rr lists of queues with requests. We maintain service trees for
199 * RT and BE classes. These trees are subdivided in subclasses
200 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
201 * class there is no subclassification and all the cfq queues go on
202 * a single tree service_tree_idle.
203 * Counts are embedded in the cfq_rb_root
205 struct cfq_rb_root service_trees[2][3];
206 struct cfq_rb_root service_tree_idle;
208 unsigned long saved_workload_slice;
209 enum wl_type_t saved_workload;
210 enum wl_prio_t saved_serving_prio;
211 #ifdef CONFIG_CFQ_GROUP_IOSCHED
212 struct hlist_node cfqd_node;
214 /* number of requests that are on the dispatch list or inside driver */
216 struct cfq_ttime ttime;
220 struct io_cq icq; /* must be the first member */
221 struct cfq_queue *cfqq[2];
222 struct cfq_ttime ttime;
226 * Per block device queue structure
229 struct request_queue *queue;
230 /* Root service tree for cfq_groups */
231 struct cfq_rb_root grp_service_tree;
232 struct cfq_group *root_group;
235 * The priority currently being served
237 enum wl_prio_t serving_prio;
238 enum wl_type_t serving_type;
239 unsigned long workload_expires;
240 struct cfq_group *serving_group;
243 * Each priority tree is sorted by next_request position. These
244 * trees are used when determining if two or more queues are
245 * interleaving requests (see cfq_close_cooperator).
247 struct rb_root prio_trees[CFQ_PRIO_LISTS];
249 unsigned int busy_queues;
250 unsigned int busy_sync_queues;
256 * queue-depth detection
262 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
263 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
266 int hw_tag_est_depth;
267 unsigned int hw_tag_samples;
270 * idle window management
272 struct timer_list idle_slice_timer;
273 struct work_struct unplug_work;
275 struct cfq_queue *active_queue;
276 struct cfq_io_cq *active_cic;
279 * async queue for each priority case
281 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
282 struct cfq_queue *async_idle_cfqq;
284 sector_t last_position;
287 * tunables, see top of file
289 unsigned int cfq_quantum;
290 unsigned int cfq_fifo_expire[2];
291 unsigned int cfq_back_penalty;
292 unsigned int cfq_back_max;
293 unsigned int cfq_slice[2];
294 unsigned int cfq_slice_async_rq;
295 unsigned int cfq_slice_idle;
296 unsigned int cfq_group_idle;
297 unsigned int cfq_latency;
300 * Fallback dummy cfqq for extreme OOM conditions
302 struct cfq_queue oom_cfqq;
304 unsigned long last_delayed_sync;
306 /* List of cfq groups being managed on this device*/
307 struct hlist_head cfqg_list;
309 /* Number of groups which are on blkcg->blkg_list */
310 unsigned int nr_blkcg_linked_grps;
313 static inline struct cfq_group *blkg_to_cfqg(struct blkio_group *blkg)
315 return blkg_to_pdata(blkg, &blkio_policy_cfq);
318 static inline struct blkio_group *cfqg_to_blkg(struct cfq_group *cfqg)
320 return pdata_to_blkg(cfqg, &blkio_policy_cfq);
323 static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
325 static struct cfq_rb_root *service_tree_for(struct cfq_group *cfqg,
332 if (prio == IDLE_WORKLOAD)
333 return &cfqg->service_tree_idle;
335 return &cfqg->service_trees[prio][type];
338 enum cfqq_state_flags {
339 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
340 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
341 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
342 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
343 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
344 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
345 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
346 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
347 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
348 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
349 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
350 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
351 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
354 #define CFQ_CFQQ_FNS(name) \
355 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
357 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
359 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
361 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
363 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
365 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
369 CFQ_CFQQ_FNS(wait_request);
370 CFQ_CFQQ_FNS(must_dispatch);
371 CFQ_CFQQ_FNS(must_alloc_slice);
372 CFQ_CFQQ_FNS(fifo_expire);
373 CFQ_CFQQ_FNS(idle_window);
374 CFQ_CFQQ_FNS(prio_changed);
375 CFQ_CFQQ_FNS(slice_new);
378 CFQ_CFQQ_FNS(split_coop);
380 CFQ_CFQQ_FNS(wait_busy);
383 #ifdef CONFIG_CFQ_GROUP_IOSCHED
384 #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
385 blk_add_trace_msg((cfqd)->queue, "cfq%d%c %s " fmt, (cfqq)->pid, \
386 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
387 blkg_path(cfqg_to_blkg((cfqq)->cfqg)), ##args)
389 #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) \
390 blk_add_trace_msg((cfqd)->queue, "%s " fmt, \
391 blkg_path(cfqg_to_blkg((cfqg))), ##args) \
394 #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
395 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
396 #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
398 #define cfq_log(cfqd, fmt, args...) \
399 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
401 /* Traverses through cfq group service trees */
402 #define for_each_cfqg_st(cfqg, i, j, st) \
403 for (i = 0; i <= IDLE_WORKLOAD; i++) \
404 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
405 : &cfqg->service_tree_idle; \
406 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
407 (i == IDLE_WORKLOAD && j == 0); \
408 j++, st = i < IDLE_WORKLOAD ? \
409 &cfqg->service_trees[i][j]: NULL) \
411 static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
412 struct cfq_ttime *ttime, bool group_idle)
415 if (!sample_valid(ttime->ttime_samples))
418 slice = cfqd->cfq_group_idle;
420 slice = cfqd->cfq_slice_idle;
421 return ttime->ttime_mean > slice;
424 static inline bool iops_mode(struct cfq_data *cfqd)
427 * If we are not idling on queues and it is a NCQ drive, parallel
428 * execution of requests is on and measuring time is not possible
429 * in most of the cases until and unless we drive shallower queue
430 * depths and that becomes a performance bottleneck. In such cases
431 * switch to start providing fairness in terms of number of IOs.
433 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
439 static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq)
441 if (cfq_class_idle(cfqq))
442 return IDLE_WORKLOAD;
443 if (cfq_class_rt(cfqq))
449 static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
451 if (!cfq_cfqq_sync(cfqq))
452 return ASYNC_WORKLOAD;
453 if (!cfq_cfqq_idle_window(cfqq))
454 return SYNC_NOIDLE_WORKLOAD;
455 return SYNC_WORKLOAD;
458 static inline int cfq_group_busy_queues_wl(enum wl_prio_t wl,
459 struct cfq_data *cfqd,
460 struct cfq_group *cfqg)
462 if (wl == IDLE_WORKLOAD)
463 return cfqg->service_tree_idle.count;
465 return cfqg->service_trees[wl][ASYNC_WORKLOAD].count
466 + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count
467 + cfqg->service_trees[wl][SYNC_WORKLOAD].count;
470 static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
471 struct cfq_group *cfqg)
473 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count
474 + cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
477 static void cfq_dispatch_insert(struct request_queue *, struct request *);
478 static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool,
479 struct io_context *, gfp_t);
481 static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
483 /* cic->icq is the first member, %NULL will convert to %NULL */
484 return container_of(icq, struct cfq_io_cq, icq);
487 static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
488 struct io_context *ioc)
491 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
495 static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
497 return cic->cfqq[is_sync];
500 static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
503 cic->cfqq[is_sync] = cfqq;
506 static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
508 return cic->icq.q->elevator->elevator_data;
512 * We regard a request as SYNC, if it's either a read or has the SYNC bit
513 * set (in which case it could also be direct WRITE).
515 static inline bool cfq_bio_sync(struct bio *bio)
517 return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC);
521 * scheduler run of queue, if there are requests pending and no one in the
522 * driver that will restart queueing
524 static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
526 if (cfqd->busy_queues) {
527 cfq_log(cfqd, "schedule dispatch");
528 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
533 * Scale schedule slice based on io priority. Use the sync time slice only
534 * if a queue is marked sync and has sync io queued. A sync queue with async
535 * io only, should not get full sync slice length.
537 static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
540 const int base_slice = cfqd->cfq_slice[sync];
542 WARN_ON(prio >= IOPRIO_BE_NR);
544 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
548 cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
550 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
553 static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
555 u64 d = delta << CFQ_SERVICE_SHIFT;
557 d = d * BLKIO_WEIGHT_DEFAULT;
558 do_div(d, cfqg->weight);
562 static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
564 s64 delta = (s64)(vdisktime - min_vdisktime);
566 min_vdisktime = vdisktime;
568 return min_vdisktime;
571 static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
573 s64 delta = (s64)(vdisktime - min_vdisktime);
575 min_vdisktime = vdisktime;
577 return min_vdisktime;
580 static void update_min_vdisktime(struct cfq_rb_root *st)
582 struct cfq_group *cfqg;
585 cfqg = rb_entry_cfqg(st->left);
586 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
592 * get averaged number of queues of RT/BE priority.
593 * average is updated, with a formula that gives more weight to higher numbers,
594 * to quickly follows sudden increases and decrease slowly
597 static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
598 struct cfq_group *cfqg, bool rt)
600 unsigned min_q, max_q;
601 unsigned mult = cfq_hist_divisor - 1;
602 unsigned round = cfq_hist_divisor / 2;
603 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
605 min_q = min(cfqg->busy_queues_avg[rt], busy);
606 max_q = max(cfqg->busy_queues_avg[rt], busy);
607 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
609 return cfqg->busy_queues_avg[rt];
612 static inline unsigned
613 cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
615 struct cfq_rb_root *st = &cfqd->grp_service_tree;
617 return cfq_target_latency * cfqg->weight / st->total_weight;
620 static inline unsigned
621 cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
623 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
624 if (cfqd->cfq_latency) {
626 * interested queues (we consider only the ones with the same
627 * priority class in the cfq group)
629 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
631 unsigned sync_slice = cfqd->cfq_slice[1];
632 unsigned expect_latency = sync_slice * iq;
633 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
635 if (expect_latency > group_slice) {
636 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
637 /* scale low_slice according to IO priority
638 * and sync vs async */
640 min(slice, base_low_slice * slice / sync_slice);
641 /* the adapted slice value is scaled to fit all iqs
642 * into the target latency */
643 slice = max(slice * group_slice / expect_latency,
651 cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
653 unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
655 cfqq->slice_start = jiffies;
656 cfqq->slice_end = jiffies + slice;
657 cfqq->allocated_slice = slice;
658 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
662 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
663 * isn't valid until the first request from the dispatch is activated
664 * and the slice time set.
666 static inline bool cfq_slice_used(struct cfq_queue *cfqq)
668 if (cfq_cfqq_slice_new(cfqq))
670 if (time_before(jiffies, cfqq->slice_end))
677 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
678 * We choose the request that is closest to the head right now. Distance
679 * behind the head is penalized and only allowed to a certain extent.
681 static struct request *
682 cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
684 sector_t s1, s2, d1 = 0, d2 = 0;
685 unsigned long back_max;
686 #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
687 #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
688 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
690 if (rq1 == NULL || rq1 == rq2)
695 if (rq_is_sync(rq1) != rq_is_sync(rq2))
696 return rq_is_sync(rq1) ? rq1 : rq2;
698 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
699 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
701 s1 = blk_rq_pos(rq1);
702 s2 = blk_rq_pos(rq2);
705 * by definition, 1KiB is 2 sectors
707 back_max = cfqd->cfq_back_max * 2;
710 * Strict one way elevator _except_ in the case where we allow
711 * short backward seeks which are biased as twice the cost of a
712 * similar forward seek.
716 else if (s1 + back_max >= last)
717 d1 = (last - s1) * cfqd->cfq_back_penalty;
719 wrap |= CFQ_RQ1_WRAP;
723 else if (s2 + back_max >= last)
724 d2 = (last - s2) * cfqd->cfq_back_penalty;
726 wrap |= CFQ_RQ2_WRAP;
728 /* Found required data */
731 * By doing switch() on the bit mask "wrap" we avoid having to
732 * check two variables for all permutations: --> faster!
735 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
751 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
754 * Since both rqs are wrapped,
755 * start with the one that's further behind head
756 * (--> only *one* back seek required),
757 * since back seek takes more time than forward.
767 * The below is leftmost cache rbtree addon
769 static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
771 /* Service tree is empty */
776 root->left = rb_first(&root->rb);
779 return rb_entry(root->left, struct cfq_queue, rb_node);
784 static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
787 root->left = rb_first(&root->rb);
790 return rb_entry_cfqg(root->left);
795 static void rb_erase_init(struct rb_node *n, struct rb_root *root)
801 static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
805 rb_erase_init(n, &root->rb);
810 * would be nice to take fifo expire time into account as well
812 static struct request *
813 cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
814 struct request *last)
816 struct rb_node *rbnext = rb_next(&last->rb_node);
817 struct rb_node *rbprev = rb_prev(&last->rb_node);
818 struct request *next = NULL, *prev = NULL;
820 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
823 prev = rb_entry_rq(rbprev);
826 next = rb_entry_rq(rbnext);
828 rbnext = rb_first(&cfqq->sort_list);
829 if (rbnext && rbnext != &last->rb_node)
830 next = rb_entry_rq(rbnext);
833 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
836 static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
837 struct cfq_queue *cfqq)
840 * just an approximation, should be ok.
842 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
843 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
847 cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
849 return cfqg->vdisktime - st->min_vdisktime;
853 __cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
855 struct rb_node **node = &st->rb.rb_node;
856 struct rb_node *parent = NULL;
857 struct cfq_group *__cfqg;
858 s64 key = cfqg_key(st, cfqg);
861 while (*node != NULL) {
863 __cfqg = rb_entry_cfqg(parent);
865 if (key < cfqg_key(st, __cfqg))
866 node = &parent->rb_left;
868 node = &parent->rb_right;
874 st->left = &cfqg->rb_node;
876 rb_link_node(&cfqg->rb_node, parent, node);
877 rb_insert_color(&cfqg->rb_node, &st->rb);
881 cfq_update_group_weight(struct cfq_group *cfqg)
883 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
884 if (cfqg->needs_update) {
885 cfqg->weight = cfqg->new_weight;
886 cfqg->needs_update = false;
891 cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
893 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
895 cfq_update_group_weight(cfqg);
896 __cfq_group_service_tree_add(st, cfqg);
897 st->total_weight += cfqg->weight;
901 cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
903 struct cfq_rb_root *st = &cfqd->grp_service_tree;
904 struct cfq_group *__cfqg;
908 if (!RB_EMPTY_NODE(&cfqg->rb_node))
912 * Currently put the group at the end. Later implement something
913 * so that groups get lesser vtime based on their weights, so that
914 * if group does not loose all if it was not continuously backlogged.
916 n = rb_last(&st->rb);
918 __cfqg = rb_entry_cfqg(n);
919 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
921 cfqg->vdisktime = st->min_vdisktime;
922 cfq_group_service_tree_add(st, cfqg);
926 cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
928 st->total_weight -= cfqg->weight;
929 if (!RB_EMPTY_NODE(&cfqg->rb_node))
930 cfq_rb_erase(&cfqg->rb_node, st);
934 cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
936 struct cfq_rb_root *st = &cfqd->grp_service_tree;
938 BUG_ON(cfqg->nr_cfqq < 1);
941 /* If there are other cfq queues under this group, don't delete it */
945 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
946 cfq_group_service_tree_del(st, cfqg);
947 cfqg->saved_workload_slice = 0;
948 cfq_blkiocg_update_dequeue_stats(cfqg_to_blkg(cfqg), 1);
951 static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
952 unsigned int *unaccounted_time)
954 unsigned int slice_used;
957 * Queue got expired before even a single request completed or
958 * got expired immediately after first request completion.
960 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
962 * Also charge the seek time incurred to the group, otherwise
963 * if there are mutiple queues in the group, each can dispatch
964 * a single request on seeky media and cause lots of seek time
965 * and group will never know it.
967 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
970 slice_used = jiffies - cfqq->slice_start;
971 if (slice_used > cfqq->allocated_slice) {
972 *unaccounted_time = slice_used - cfqq->allocated_slice;
973 slice_used = cfqq->allocated_slice;
975 if (time_after(cfqq->slice_start, cfqq->dispatch_start))
976 *unaccounted_time += cfqq->slice_start -
977 cfqq->dispatch_start;
983 static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
984 struct cfq_queue *cfqq)
986 struct cfq_rb_root *st = &cfqd->grp_service_tree;
987 unsigned int used_sl, charge, unaccounted_sl = 0;
988 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
989 - cfqg->service_tree_idle.count;
992 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
995 charge = cfqq->slice_dispatch;
996 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
997 charge = cfqq->allocated_slice;
999 /* Can't update vdisktime while group is on service tree */
1000 cfq_group_service_tree_del(st, cfqg);
1001 cfqg->vdisktime += cfq_scale_slice(charge, cfqg);
1002 /* If a new weight was requested, update now, off tree */
1003 cfq_group_service_tree_add(st, cfqg);
1005 /* This group is being expired. Save the context */
1006 if (time_after(cfqd->workload_expires, jiffies)) {
1007 cfqg->saved_workload_slice = cfqd->workload_expires
1009 cfqg->saved_workload = cfqd->serving_type;
1010 cfqg->saved_serving_prio = cfqd->serving_prio;
1012 cfqg->saved_workload_slice = 0;
1014 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1016 cfq_log_cfqq(cfqq->cfqd, cfqq,
1017 "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
1018 used_sl, cfqq->slice_dispatch, charge,
1019 iops_mode(cfqd), cfqq->nr_sectors);
1020 cfq_blkiocg_update_timeslice_used(cfqg_to_blkg(cfqg), used_sl,
1022 cfq_blkiocg_set_start_empty_time(cfqg_to_blkg(cfqg));
1026 * cfq_init_cfqg_base - initialize base part of a cfq_group
1027 * @cfqg: cfq_group to initialize
1029 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1030 * is enabled or not.
1032 static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1034 struct cfq_rb_root *st;
1037 for_each_cfqg_st(cfqg, i, j, st)
1039 RB_CLEAR_NODE(&cfqg->rb_node);
1041 cfqg->ttime.last_end_request = jiffies;
1044 #ifdef CONFIG_CFQ_GROUP_IOSCHED
1045 static void cfq_update_blkio_group_weight(struct request_queue *q,
1046 struct blkio_group *blkg,
1047 unsigned int weight)
1049 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
1051 cfqg->new_weight = weight;
1052 cfqg->needs_update = true;
1055 static void cfq_link_blkio_group(struct request_queue *q,
1056 struct blkio_group *blkg)
1058 struct cfq_data *cfqd = q->elevator->elevator_data;
1059 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
1061 cfqd->nr_blkcg_linked_grps++;
1063 /* Add group on cfqd list */
1064 hlist_add_head(&cfqg->cfqd_node, &cfqd->cfqg_list);
1067 static void cfq_init_blkio_group(struct blkio_group *blkg)
1069 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
1071 cfq_init_cfqg_base(cfqg);
1072 cfqg->weight = blkg->blkcg->weight;
1076 * Search for the cfq group current task belongs to. request_queue lock must
1079 static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
1080 struct blkio_cgroup *blkcg)
1082 struct request_queue *q = cfqd->queue;
1083 struct cfq_group *cfqg = NULL;
1085 /* avoid lookup for the common case where there's no blkio cgroup */
1086 if (blkcg == &blkio_root_cgroup) {
1087 cfqg = cfqd->root_group;
1089 struct blkio_group *blkg;
1091 blkg = blkg_lookup_create(blkcg, q, BLKIO_POLICY_PROP, false);
1093 cfqg = blkg_to_cfqg(blkg);
1099 static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1101 /* Currently, all async queues are mapped to root group */
1102 if (!cfq_cfqq_sync(cfqq))
1103 cfqg = cfqq->cfqd->root_group;
1106 /* cfqq reference on cfqg */
1107 blkg_get(cfqg_to_blkg(cfqg));
1110 static void cfq_destroy_cfqg(struct cfq_data *cfqd, struct cfq_group *cfqg)
1112 /* Something wrong if we are trying to remove same group twice */
1113 BUG_ON(hlist_unhashed(&cfqg->cfqd_node));
1115 hlist_del_init(&cfqg->cfqd_node);
1117 BUG_ON(cfqd->nr_blkcg_linked_grps <= 0);
1118 cfqd->nr_blkcg_linked_grps--;
1121 * Put the reference taken at the time of creation so that when all
1122 * queues are gone, group can be destroyed.
1124 blkg_put(cfqg_to_blkg(cfqg));
1127 static bool cfq_release_cfq_groups(struct cfq_data *cfqd)
1129 struct hlist_node *pos, *n;
1130 struct cfq_group *cfqg;
1133 hlist_for_each_entry_safe(cfqg, pos, n, &cfqd->cfqg_list, cfqd_node) {
1135 * If cgroup removal path got to blk_group first and removed
1136 * it from cgroup list, then it will take care of destroying
1139 if (!cfq_blkiocg_del_blkio_group(cfqg_to_blkg(cfqg)))
1140 cfq_destroy_cfqg(cfqd, cfqg);
1148 * Blk cgroup controller notification saying that blkio_group object is being
1149 * delinked as associated cgroup object is going away. That also means that
1150 * no new IO will come in this group. So get rid of this group as soon as
1151 * any pending IO in the group is finished.
1153 * This function is called under rcu_read_lock(). key is the rcu protected
1154 * pointer. That means @q is a valid request_queue pointer as long as we
1155 * are rcu read lock.
1157 * @q was fetched from blkio_group under blkio_cgroup->lock. That means
1158 * it should not be NULL as even if elevator was exiting, cgroup deltion
1159 * path got to it first.
1161 static void cfq_unlink_blkio_group(struct request_queue *q,
1162 struct blkio_group *blkg)
1164 struct cfq_data *cfqd = q->elevator->elevator_data;
1165 unsigned long flags;
1167 spin_lock_irqsave(q->queue_lock, flags);
1168 cfq_destroy_cfqg(cfqd, blkg_to_cfqg(blkg));
1169 spin_unlock_irqrestore(q->queue_lock, flags);
1172 static struct elevator_type iosched_cfq;
1174 static bool cfq_clear_queue(struct request_queue *q)
1176 lockdep_assert_held(q->queue_lock);
1178 /* shoot down blkgs iff the current elevator is cfq */
1179 if (!q->elevator || q->elevator->type != &iosched_cfq)
1182 return cfq_release_cfq_groups(q->elevator->elevator_data);
1185 #else /* GROUP_IOSCHED */
1186 static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
1187 struct blkio_cgroup *blkcg)
1189 return cfqd->root_group;
1193 cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1197 static void cfq_release_cfq_groups(struct cfq_data *cfqd) {}
1199 #endif /* GROUP_IOSCHED */
1202 * The cfqd->service_trees holds all pending cfq_queue's that have
1203 * requests waiting to be processed. It is sorted in the order that
1204 * we will service the queues.
1206 static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1209 struct rb_node **p, *parent;
1210 struct cfq_queue *__cfqq;
1211 unsigned long rb_key;
1212 struct cfq_rb_root *service_tree;
1216 service_tree = service_tree_for(cfqq->cfqg, cfqq_prio(cfqq),
1218 if (cfq_class_idle(cfqq)) {
1219 rb_key = CFQ_IDLE_DELAY;
1220 parent = rb_last(&service_tree->rb);
1221 if (parent && parent != &cfqq->rb_node) {
1222 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1223 rb_key += __cfqq->rb_key;
1226 } else if (!add_front) {
1228 * Get our rb key offset. Subtract any residual slice
1229 * value carried from last service. A negative resid
1230 * count indicates slice overrun, and this should position
1231 * the next service time further away in the tree.
1233 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
1234 rb_key -= cfqq->slice_resid;
1235 cfqq->slice_resid = 0;
1238 __cfqq = cfq_rb_first(service_tree);
1239 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
1242 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
1245 * same position, nothing more to do
1247 if (rb_key == cfqq->rb_key &&
1248 cfqq->service_tree == service_tree)
1251 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1252 cfqq->service_tree = NULL;
1257 cfqq->service_tree = service_tree;
1258 p = &service_tree->rb.rb_node;
1263 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1266 * sort by key, that represents service time.
1268 if (time_before(rb_key, __cfqq->rb_key))
1271 n = &(*p)->rb_right;
1279 service_tree->left = &cfqq->rb_node;
1281 cfqq->rb_key = rb_key;
1282 rb_link_node(&cfqq->rb_node, parent, p);
1283 rb_insert_color(&cfqq->rb_node, &service_tree->rb);
1284 service_tree->count++;
1285 if (add_front || !new_cfqq)
1287 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
1290 static struct cfq_queue *
1291 cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
1292 sector_t sector, struct rb_node **ret_parent,
1293 struct rb_node ***rb_link)
1295 struct rb_node **p, *parent;
1296 struct cfq_queue *cfqq = NULL;
1304 cfqq = rb_entry(parent, struct cfq_queue, p_node);
1307 * Sort strictly based on sector. Smallest to the left,
1308 * largest to the right.
1310 if (sector > blk_rq_pos(cfqq->next_rq))
1311 n = &(*p)->rb_right;
1312 else if (sector < blk_rq_pos(cfqq->next_rq))
1320 *ret_parent = parent;
1326 static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1328 struct rb_node **p, *parent;
1329 struct cfq_queue *__cfqq;
1332 rb_erase(&cfqq->p_node, cfqq->p_root);
1333 cfqq->p_root = NULL;
1336 if (cfq_class_idle(cfqq))
1341 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
1342 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
1343 blk_rq_pos(cfqq->next_rq), &parent, &p);
1345 rb_link_node(&cfqq->p_node, parent, p);
1346 rb_insert_color(&cfqq->p_node, cfqq->p_root);
1348 cfqq->p_root = NULL;
1352 * Update cfqq's position in the service tree.
1354 static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1357 * Resorting requires the cfqq to be on the RR list already.
1359 if (cfq_cfqq_on_rr(cfqq)) {
1360 cfq_service_tree_add(cfqd, cfqq, 0);
1361 cfq_prio_tree_add(cfqd, cfqq);
1366 * add to busy list of queues for service, trying to be fair in ordering
1367 * the pending list according to last request service
1369 static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1371 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
1372 BUG_ON(cfq_cfqq_on_rr(cfqq));
1373 cfq_mark_cfqq_on_rr(cfqq);
1374 cfqd->busy_queues++;
1375 if (cfq_cfqq_sync(cfqq))
1376 cfqd->busy_sync_queues++;
1378 cfq_resort_rr_list(cfqd, cfqq);
1382 * Called when the cfqq no longer has requests pending, remove it from
1385 static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1387 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
1388 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1389 cfq_clear_cfqq_on_rr(cfqq);
1391 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
1392 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1393 cfqq->service_tree = NULL;
1396 rb_erase(&cfqq->p_node, cfqq->p_root);
1397 cfqq->p_root = NULL;
1400 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
1401 BUG_ON(!cfqd->busy_queues);
1402 cfqd->busy_queues--;
1403 if (cfq_cfqq_sync(cfqq))
1404 cfqd->busy_sync_queues--;
1408 * rb tree support functions
1410 static void cfq_del_rq_rb(struct request *rq)
1412 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1413 const int sync = rq_is_sync(rq);
1415 BUG_ON(!cfqq->queued[sync]);
1416 cfqq->queued[sync]--;
1418 elv_rb_del(&cfqq->sort_list, rq);
1420 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
1422 * Queue will be deleted from service tree when we actually
1423 * expire it later. Right now just remove it from prio tree
1427 rb_erase(&cfqq->p_node, cfqq->p_root);
1428 cfqq->p_root = NULL;
1433 static void cfq_add_rq_rb(struct request *rq)
1435 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1436 struct cfq_data *cfqd = cfqq->cfqd;
1437 struct request *prev;
1439 cfqq->queued[rq_is_sync(rq)]++;
1441 elv_rb_add(&cfqq->sort_list, rq);
1443 if (!cfq_cfqq_on_rr(cfqq))
1444 cfq_add_cfqq_rr(cfqd, cfqq);
1447 * check if this request is a better next-serve candidate
1449 prev = cfqq->next_rq;
1450 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
1453 * adjust priority tree position, if ->next_rq changes
1455 if (prev != cfqq->next_rq)
1456 cfq_prio_tree_add(cfqd, cfqq);
1458 BUG_ON(!cfqq->next_rq);
1461 static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
1463 elv_rb_del(&cfqq->sort_list, rq);
1464 cfqq->queued[rq_is_sync(rq)]--;
1465 cfq_blkiocg_update_io_remove_stats(cfqg_to_blkg(RQ_CFQG(rq)),
1466 rq_data_dir(rq), rq_is_sync(rq));
1468 cfq_blkiocg_update_io_add_stats(cfqg_to_blkg(RQ_CFQG(rq)),
1469 cfqg_to_blkg(cfqq->cfqd->serving_group),
1470 rq_data_dir(rq), rq_is_sync(rq));
1473 static struct request *
1474 cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
1476 struct task_struct *tsk = current;
1477 struct cfq_io_cq *cic;
1478 struct cfq_queue *cfqq;
1480 cic = cfq_cic_lookup(cfqd, tsk->io_context);
1484 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
1486 sector_t sector = bio->bi_sector + bio_sectors(bio);
1488 return elv_rb_find(&cfqq->sort_list, sector);
1494 static void cfq_activate_request(struct request_queue *q, struct request *rq)
1496 struct cfq_data *cfqd = q->elevator->elevator_data;
1498 cfqd->rq_in_driver++;
1499 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
1500 cfqd->rq_in_driver);
1502 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
1505 static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
1507 struct cfq_data *cfqd = q->elevator->elevator_data;
1509 WARN_ON(!cfqd->rq_in_driver);
1510 cfqd->rq_in_driver--;
1511 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
1512 cfqd->rq_in_driver);
1515 static void cfq_remove_request(struct request *rq)
1517 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1519 if (cfqq->next_rq == rq)
1520 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
1522 list_del_init(&rq->queuelist);
1525 cfqq->cfqd->rq_queued--;
1526 cfq_blkiocg_update_io_remove_stats(cfqg_to_blkg(RQ_CFQG(rq)),
1527 rq_data_dir(rq), rq_is_sync(rq));
1528 if (rq->cmd_flags & REQ_PRIO) {
1529 WARN_ON(!cfqq->prio_pending);
1530 cfqq->prio_pending--;
1534 static int cfq_merge(struct request_queue *q, struct request **req,
1537 struct cfq_data *cfqd = q->elevator->elevator_data;
1538 struct request *__rq;
1540 __rq = cfq_find_rq_fmerge(cfqd, bio);
1541 if (__rq && elv_rq_merge_ok(__rq, bio)) {
1543 return ELEVATOR_FRONT_MERGE;
1546 return ELEVATOR_NO_MERGE;
1549 static void cfq_merged_request(struct request_queue *q, struct request *req,
1552 if (type == ELEVATOR_FRONT_MERGE) {
1553 struct cfq_queue *cfqq = RQ_CFQQ(req);
1555 cfq_reposition_rq_rb(cfqq, req);
1559 static void cfq_bio_merged(struct request_queue *q, struct request *req,
1562 cfq_blkiocg_update_io_merged_stats(cfqg_to_blkg(RQ_CFQG(req)),
1563 bio_data_dir(bio), cfq_bio_sync(bio));
1567 cfq_merged_requests(struct request_queue *q, struct request *rq,
1568 struct request *next)
1570 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1571 struct cfq_data *cfqd = q->elevator->elevator_data;
1574 * reposition in fifo if next is older than rq
1576 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
1577 time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
1578 list_move(&rq->queuelist, &next->queuelist);
1579 rq_set_fifo_time(rq, rq_fifo_time(next));
1582 if (cfqq->next_rq == next)
1584 cfq_remove_request(next);
1585 cfq_blkiocg_update_io_merged_stats(cfqg_to_blkg(RQ_CFQG(rq)),
1586 rq_data_dir(next), rq_is_sync(next));
1588 cfqq = RQ_CFQQ(next);
1590 * all requests of this queue are merged to other queues, delete it
1591 * from the service tree. If it's the active_queue,
1592 * cfq_dispatch_requests() will choose to expire it or do idle
1594 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
1595 cfqq != cfqd->active_queue)
1596 cfq_del_cfqq_rr(cfqd, cfqq);
1599 static int cfq_allow_merge(struct request_queue *q, struct request *rq,
1602 struct cfq_data *cfqd = q->elevator->elevator_data;
1603 struct cfq_io_cq *cic;
1604 struct cfq_queue *cfqq;
1607 * Disallow merge of a sync bio into an async request.
1609 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
1613 * Lookup the cfqq that this bio will be queued with and allow
1614 * merge only if rq is queued there.
1616 cic = cfq_cic_lookup(cfqd, current->io_context);
1620 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
1621 return cfqq == RQ_CFQQ(rq);
1624 static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1626 del_timer(&cfqd->idle_slice_timer);
1627 cfq_blkiocg_update_idle_time_stats(cfqg_to_blkg(cfqq->cfqg));
1630 static void __cfq_set_active_queue(struct cfq_data *cfqd,
1631 struct cfq_queue *cfqq)
1634 cfq_log_cfqq(cfqd, cfqq, "set_active wl_prio:%d wl_type:%d",
1635 cfqd->serving_prio, cfqd->serving_type);
1636 cfq_blkiocg_update_avg_queue_size_stats(cfqg_to_blkg(cfqq->cfqg));
1637 cfqq->slice_start = 0;
1638 cfqq->dispatch_start = jiffies;
1639 cfqq->allocated_slice = 0;
1640 cfqq->slice_end = 0;
1641 cfqq->slice_dispatch = 0;
1642 cfqq->nr_sectors = 0;
1644 cfq_clear_cfqq_wait_request(cfqq);
1645 cfq_clear_cfqq_must_dispatch(cfqq);
1646 cfq_clear_cfqq_must_alloc_slice(cfqq);
1647 cfq_clear_cfqq_fifo_expire(cfqq);
1648 cfq_mark_cfqq_slice_new(cfqq);
1650 cfq_del_timer(cfqd, cfqq);
1653 cfqd->active_queue = cfqq;
1657 * current cfqq expired its slice (or was too idle), select new one
1660 __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1663 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
1665 if (cfq_cfqq_wait_request(cfqq))
1666 cfq_del_timer(cfqd, cfqq);
1668 cfq_clear_cfqq_wait_request(cfqq);
1669 cfq_clear_cfqq_wait_busy(cfqq);
1672 * If this cfqq is shared between multiple processes, check to
1673 * make sure that those processes are still issuing I/Os within
1674 * the mean seek distance. If not, it may be time to break the
1675 * queues apart again.
1677 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
1678 cfq_mark_cfqq_split_coop(cfqq);
1681 * store what was left of this slice, if the queue idled/timed out
1684 if (cfq_cfqq_slice_new(cfqq))
1685 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
1687 cfqq->slice_resid = cfqq->slice_end - jiffies;
1688 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
1691 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
1693 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
1694 cfq_del_cfqq_rr(cfqd, cfqq);
1696 cfq_resort_rr_list(cfqd, cfqq);
1698 if (cfqq == cfqd->active_queue)
1699 cfqd->active_queue = NULL;
1701 if (cfqd->active_cic) {
1702 put_io_context(cfqd->active_cic->icq.ioc);
1703 cfqd->active_cic = NULL;
1707 static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
1709 struct cfq_queue *cfqq = cfqd->active_queue;
1712 __cfq_slice_expired(cfqd, cfqq, timed_out);
1716 * Get next queue for service. Unless we have a queue preemption,
1717 * we'll simply select the first cfqq in the service tree.
1719 static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
1721 struct cfq_rb_root *service_tree =
1722 service_tree_for(cfqd->serving_group, cfqd->serving_prio,
1723 cfqd->serving_type);
1725 if (!cfqd->rq_queued)
1728 /* There is nothing to dispatch */
1731 if (RB_EMPTY_ROOT(&service_tree->rb))
1733 return cfq_rb_first(service_tree);
1736 static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
1738 struct cfq_group *cfqg;
1739 struct cfq_queue *cfqq;
1741 struct cfq_rb_root *st;
1743 if (!cfqd->rq_queued)
1746 cfqg = cfq_get_next_cfqg(cfqd);
1750 for_each_cfqg_st(cfqg, i, j, st)
1751 if ((cfqq = cfq_rb_first(st)) != NULL)
1757 * Get and set a new active queue for service.
1759 static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
1760 struct cfq_queue *cfqq)
1763 cfqq = cfq_get_next_queue(cfqd);
1765 __cfq_set_active_queue(cfqd, cfqq);
1769 static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
1772 if (blk_rq_pos(rq) >= cfqd->last_position)
1773 return blk_rq_pos(rq) - cfqd->last_position;
1775 return cfqd->last_position - blk_rq_pos(rq);
1778 static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1781 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
1784 static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
1785 struct cfq_queue *cur_cfqq)
1787 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
1788 struct rb_node *parent, *node;
1789 struct cfq_queue *__cfqq;
1790 sector_t sector = cfqd->last_position;
1792 if (RB_EMPTY_ROOT(root))
1796 * First, if we find a request starting at the end of the last
1797 * request, choose it.
1799 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
1804 * If the exact sector wasn't found, the parent of the NULL leaf
1805 * will contain the closest sector.
1807 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
1808 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
1811 if (blk_rq_pos(__cfqq->next_rq) < sector)
1812 node = rb_next(&__cfqq->p_node);
1814 node = rb_prev(&__cfqq->p_node);
1818 __cfqq = rb_entry(node, struct cfq_queue, p_node);
1819 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
1827 * cur_cfqq - passed in so that we don't decide that the current queue is
1828 * closely cooperating with itself.
1830 * So, basically we're assuming that that cur_cfqq has dispatched at least
1831 * one request, and that cfqd->last_position reflects a position on the disk
1832 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
1835 static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
1836 struct cfq_queue *cur_cfqq)
1838 struct cfq_queue *cfqq;
1840 if (cfq_class_idle(cur_cfqq))
1842 if (!cfq_cfqq_sync(cur_cfqq))
1844 if (CFQQ_SEEKY(cur_cfqq))
1848 * Don't search priority tree if it's the only queue in the group.
1850 if (cur_cfqq->cfqg->nr_cfqq == 1)
1854 * We should notice if some of the queues are cooperating, eg
1855 * working closely on the same area of the disk. In that case,
1856 * we can group them together and don't waste time idling.
1858 cfqq = cfqq_close(cfqd, cur_cfqq);
1862 /* If new queue belongs to different cfq_group, don't choose it */
1863 if (cur_cfqq->cfqg != cfqq->cfqg)
1867 * It only makes sense to merge sync queues.
1869 if (!cfq_cfqq_sync(cfqq))
1871 if (CFQQ_SEEKY(cfqq))
1875 * Do not merge queues of different priority classes
1877 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
1884 * Determine whether we should enforce idle window for this queue.
1887 static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1889 enum wl_prio_t prio = cfqq_prio(cfqq);
1890 struct cfq_rb_root *service_tree = cfqq->service_tree;
1892 BUG_ON(!service_tree);
1893 BUG_ON(!service_tree->count);
1895 if (!cfqd->cfq_slice_idle)
1898 /* We never do for idle class queues. */
1899 if (prio == IDLE_WORKLOAD)
1902 /* We do for queues that were marked with idle window flag. */
1903 if (cfq_cfqq_idle_window(cfqq) &&
1904 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
1908 * Otherwise, we do only if they are the last ones
1909 * in their service tree.
1911 if (service_tree->count == 1 && cfq_cfqq_sync(cfqq) &&
1912 !cfq_io_thinktime_big(cfqd, &service_tree->ttime, false))
1914 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d",
1915 service_tree->count);
1919 static void cfq_arm_slice_timer(struct cfq_data *cfqd)
1921 struct cfq_queue *cfqq = cfqd->active_queue;
1922 struct cfq_io_cq *cic;
1923 unsigned long sl, group_idle = 0;
1926 * SSD device without seek penalty, disable idling. But only do so
1927 * for devices that support queuing, otherwise we still have a problem
1928 * with sync vs async workloads.
1930 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
1933 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
1934 WARN_ON(cfq_cfqq_slice_new(cfqq));
1937 * idle is disabled, either manually or by past process history
1939 if (!cfq_should_idle(cfqd, cfqq)) {
1940 /* no queue idling. Check for group idling */
1941 if (cfqd->cfq_group_idle)
1942 group_idle = cfqd->cfq_group_idle;
1948 * still active requests from this queue, don't idle
1950 if (cfqq->dispatched)
1954 * task has exited, don't wait
1956 cic = cfqd->active_cic;
1957 if (!cic || !atomic_read(&cic->icq.ioc->nr_tasks))
1961 * If our average think time is larger than the remaining time
1962 * slice, then don't idle. This avoids overrunning the allotted
1965 if (sample_valid(cic->ttime.ttime_samples) &&
1966 (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) {
1967 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%lu",
1968 cic->ttime.ttime_mean);
1972 /* There are other queues in the group, don't do group idle */
1973 if (group_idle && cfqq->cfqg->nr_cfqq > 1)
1976 cfq_mark_cfqq_wait_request(cfqq);
1979 sl = cfqd->cfq_group_idle;
1981 sl = cfqd->cfq_slice_idle;
1983 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
1984 cfq_blkiocg_update_set_idle_time_stats(cfqg_to_blkg(cfqq->cfqg));
1985 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu group_idle: %d", sl,
1986 group_idle ? 1 : 0);
1990 * Move request from internal lists to the request queue dispatch list.
1992 static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
1994 struct cfq_data *cfqd = q->elevator->elevator_data;
1995 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1997 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
1999 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
2000 cfq_remove_request(rq);
2002 (RQ_CFQG(rq))->dispatched++;
2003 elv_dispatch_sort(q, rq);
2005 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
2006 cfqq->nr_sectors += blk_rq_sectors(rq);
2007 cfq_blkiocg_update_dispatch_stats(cfqg_to_blkg(cfqq->cfqg),
2008 blk_rq_bytes(rq), rq_data_dir(rq),
2013 * return expired entry, or NULL to just start from scratch in rbtree
2015 static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
2017 struct request *rq = NULL;
2019 if (cfq_cfqq_fifo_expire(cfqq))
2022 cfq_mark_cfqq_fifo_expire(cfqq);
2024 if (list_empty(&cfqq->fifo))
2027 rq = rq_entry_fifo(cfqq->fifo.next);
2028 if (time_before(jiffies, rq_fifo_time(rq)))
2031 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
2036 cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2038 const int base_rq = cfqd->cfq_slice_async_rq;
2040 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
2042 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
2046 * Must be called with the queue_lock held.
2048 static int cfqq_process_refs(struct cfq_queue *cfqq)
2050 int process_refs, io_refs;
2052 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
2053 process_refs = cfqq->ref - io_refs;
2054 BUG_ON(process_refs < 0);
2055 return process_refs;
2058 static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
2060 int process_refs, new_process_refs;
2061 struct cfq_queue *__cfqq;
2064 * If there are no process references on the new_cfqq, then it is
2065 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
2066 * chain may have dropped their last reference (not just their
2067 * last process reference).
2069 if (!cfqq_process_refs(new_cfqq))
2072 /* Avoid a circular list and skip interim queue merges */
2073 while ((__cfqq = new_cfqq->new_cfqq)) {
2079 process_refs = cfqq_process_refs(cfqq);
2080 new_process_refs = cfqq_process_refs(new_cfqq);
2082 * If the process for the cfqq has gone away, there is no
2083 * sense in merging the queues.
2085 if (process_refs == 0 || new_process_refs == 0)
2089 * Merge in the direction of the lesser amount of work.
2091 if (new_process_refs >= process_refs) {
2092 cfqq->new_cfqq = new_cfqq;
2093 new_cfqq->ref += process_refs;
2095 new_cfqq->new_cfqq = cfqq;
2096 cfqq->ref += new_process_refs;
2100 static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd,
2101 struct cfq_group *cfqg, enum wl_prio_t prio)
2103 struct cfq_queue *queue;
2105 bool key_valid = false;
2106 unsigned long lowest_key = 0;
2107 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
2109 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
2110 /* select the one with lowest rb_key */
2111 queue = cfq_rb_first(service_tree_for(cfqg, prio, i));
2113 (!key_valid || time_before(queue->rb_key, lowest_key))) {
2114 lowest_key = queue->rb_key;
2123 static void choose_service_tree(struct cfq_data *cfqd, struct cfq_group *cfqg)
2127 struct cfq_rb_root *st;
2128 unsigned group_slice;
2129 enum wl_prio_t original_prio = cfqd->serving_prio;
2131 /* Choose next priority. RT > BE > IDLE */
2132 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
2133 cfqd->serving_prio = RT_WORKLOAD;
2134 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
2135 cfqd->serving_prio = BE_WORKLOAD;
2137 cfqd->serving_prio = IDLE_WORKLOAD;
2138 cfqd->workload_expires = jiffies + 1;
2142 if (original_prio != cfqd->serving_prio)
2146 * For RT and BE, we have to choose also the type
2147 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
2150 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
2154 * check workload expiration, and that we still have other queues ready
2156 if (count && !time_after(jiffies, cfqd->workload_expires))
2160 /* otherwise select new workload type */
2161 cfqd->serving_type =
2162 cfq_choose_wl(cfqd, cfqg, cfqd->serving_prio);
2163 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
2167 * the workload slice is computed as a fraction of target latency
2168 * proportional to the number of queues in that workload, over
2169 * all the queues in the same priority class
2171 group_slice = cfq_group_slice(cfqd, cfqg);
2173 slice = group_slice * count /
2174 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio],
2175 cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd, cfqg));
2177 if (cfqd->serving_type == ASYNC_WORKLOAD) {
2181 * Async queues are currently system wide. Just taking
2182 * proportion of queues with-in same group will lead to higher
2183 * async ratio system wide as generally root group is going
2184 * to have higher weight. A more accurate thing would be to
2185 * calculate system wide asnc/sync ratio.
2187 tmp = cfq_target_latency * cfqg_busy_async_queues(cfqd, cfqg);
2188 tmp = tmp/cfqd->busy_queues;
2189 slice = min_t(unsigned, slice, tmp);
2191 /* async workload slice is scaled down according to
2192 * the sync/async slice ratio. */
2193 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
2195 /* sync workload slice is at least 2 * cfq_slice_idle */
2196 slice = max(slice, 2 * cfqd->cfq_slice_idle);
2198 slice = max_t(unsigned, slice, CFQ_MIN_TT);
2199 cfq_log(cfqd, "workload slice:%d", slice);
2200 cfqd->workload_expires = jiffies + slice;
2203 static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
2205 struct cfq_rb_root *st = &cfqd->grp_service_tree;
2206 struct cfq_group *cfqg;
2208 if (RB_EMPTY_ROOT(&st->rb))
2210 cfqg = cfq_rb_first_group(st);
2211 update_min_vdisktime(st);
2215 static void cfq_choose_cfqg(struct cfq_data *cfqd)
2217 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
2219 cfqd->serving_group = cfqg;
2221 /* Restore the workload type data */
2222 if (cfqg->saved_workload_slice) {
2223 cfqd->workload_expires = jiffies + cfqg->saved_workload_slice;
2224 cfqd->serving_type = cfqg->saved_workload;
2225 cfqd->serving_prio = cfqg->saved_serving_prio;
2227 cfqd->workload_expires = jiffies - 1;
2229 choose_service_tree(cfqd, cfqg);
2233 * Select a queue for service. If we have a current active queue,
2234 * check whether to continue servicing it, or retrieve and set a new one.
2236 static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
2238 struct cfq_queue *cfqq, *new_cfqq = NULL;
2240 cfqq = cfqd->active_queue;
2244 if (!cfqd->rq_queued)
2248 * We were waiting for group to get backlogged. Expire the queue
2250 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
2254 * The active queue has run out of time, expire it and select new.
2256 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
2258 * If slice had not expired at the completion of last request
2259 * we might not have turned on wait_busy flag. Don't expire
2260 * the queue yet. Allow the group to get backlogged.
2262 * The very fact that we have used the slice, that means we
2263 * have been idling all along on this queue and it should be
2264 * ok to wait for this request to complete.
2266 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
2267 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2271 goto check_group_idle;
2275 * The active queue has requests and isn't expired, allow it to
2278 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
2282 * If another queue has a request waiting within our mean seek
2283 * distance, let it run. The expire code will check for close
2284 * cooperators and put the close queue at the front of the service
2285 * tree. If possible, merge the expiring queue with the new cfqq.
2287 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
2289 if (!cfqq->new_cfqq)
2290 cfq_setup_merge(cfqq, new_cfqq);
2295 * No requests pending. If the active queue still has requests in
2296 * flight or is idling for a new request, allow either of these
2297 * conditions to happen (or time out) before selecting a new queue.
2299 if (timer_pending(&cfqd->idle_slice_timer)) {
2305 * This is a deep seek queue, but the device is much faster than
2306 * the queue can deliver, don't idle
2308 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
2309 (cfq_cfqq_slice_new(cfqq) ||
2310 (cfqq->slice_end - jiffies > jiffies - cfqq->slice_start))) {
2311 cfq_clear_cfqq_deep(cfqq);
2312 cfq_clear_cfqq_idle_window(cfqq);
2315 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2321 * If group idle is enabled and there are requests dispatched from
2322 * this group, wait for requests to complete.
2325 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
2326 cfqq->cfqg->dispatched &&
2327 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
2333 cfq_slice_expired(cfqd, 0);
2336 * Current queue expired. Check if we have to switch to a new
2340 cfq_choose_cfqg(cfqd);
2342 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
2347 static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
2351 while (cfqq->next_rq) {
2352 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
2356 BUG_ON(!list_empty(&cfqq->fifo));
2358 /* By default cfqq is not expired if it is empty. Do it explicitly */
2359 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
2364 * Drain our current requests. Used for barriers and when switching
2365 * io schedulers on-the-fly.
2367 static int cfq_forced_dispatch(struct cfq_data *cfqd)
2369 struct cfq_queue *cfqq;
2372 /* Expire the timeslice of the current active queue first */
2373 cfq_slice_expired(cfqd, 0);
2374 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
2375 __cfq_set_active_queue(cfqd, cfqq);
2376 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
2379 BUG_ON(cfqd->busy_queues);
2381 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
2385 static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
2386 struct cfq_queue *cfqq)
2388 /* the queue hasn't finished any request, can't estimate */
2389 if (cfq_cfqq_slice_new(cfqq))
2391 if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched,
2398 static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2400 unsigned int max_dispatch;
2403 * Drain async requests before we start sync IO
2405 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
2409 * If this is an async queue and we have sync IO in flight, let it wait
2411 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
2414 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
2415 if (cfq_class_idle(cfqq))
2419 * Does this cfqq already have too much IO in flight?
2421 if (cfqq->dispatched >= max_dispatch) {
2422 bool promote_sync = false;
2424 * idle queue must always only have a single IO in flight
2426 if (cfq_class_idle(cfqq))
2430 * If there is only one sync queue
2431 * we can ignore async queue here and give the sync
2432 * queue no dispatch limit. The reason is a sync queue can
2433 * preempt async queue, limiting the sync queue doesn't make
2434 * sense. This is useful for aiostress test.
2436 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
2437 promote_sync = true;
2440 * We have other queues, don't allow more IO from this one
2442 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
2447 * Sole queue user, no limit
2449 if (cfqd->busy_queues == 1 || promote_sync)
2453 * Normally we start throttling cfqq when cfq_quantum/2
2454 * requests have been dispatched. But we can drive
2455 * deeper queue depths at the beginning of slice
2456 * subjected to upper limit of cfq_quantum.
2458 max_dispatch = cfqd->cfq_quantum;
2462 * Async queues must wait a bit before being allowed dispatch.
2463 * We also ramp up the dispatch depth gradually for async IO,
2464 * based on the last sync IO we serviced
2466 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
2467 unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
2470 depth = last_sync / cfqd->cfq_slice[1];
2471 if (!depth && !cfqq->dispatched)
2473 if (depth < max_dispatch)
2474 max_dispatch = depth;
2478 * If we're below the current max, allow a dispatch
2480 return cfqq->dispatched < max_dispatch;
2484 * Dispatch a request from cfqq, moving them to the request queue
2487 static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2491 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
2493 if (!cfq_may_dispatch(cfqd, cfqq))
2497 * follow expired path, else get first next available
2499 rq = cfq_check_fifo(cfqq);
2504 * insert request into driver dispatch list
2506 cfq_dispatch_insert(cfqd->queue, rq);
2508 if (!cfqd->active_cic) {
2509 struct cfq_io_cq *cic = RQ_CIC(rq);
2511 atomic_long_inc(&cic->icq.ioc->refcount);
2512 cfqd->active_cic = cic;
2519 * Find the cfqq that we need to service and move a request from that to the
2522 static int cfq_dispatch_requests(struct request_queue *q, int force)
2524 struct cfq_data *cfqd = q->elevator->elevator_data;
2525 struct cfq_queue *cfqq;
2527 if (!cfqd->busy_queues)
2530 if (unlikely(force))
2531 return cfq_forced_dispatch(cfqd);
2533 cfqq = cfq_select_queue(cfqd);
2538 * Dispatch a request from this cfqq, if it is allowed
2540 if (!cfq_dispatch_request(cfqd, cfqq))
2543 cfqq->slice_dispatch++;
2544 cfq_clear_cfqq_must_dispatch(cfqq);
2547 * expire an async queue immediately if it has used up its slice. idle
2548 * queue always expire after 1 dispatch round.
2550 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
2551 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
2552 cfq_class_idle(cfqq))) {
2553 cfqq->slice_end = jiffies + 1;
2554 cfq_slice_expired(cfqd, 0);
2557 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
2562 * task holds one reference to the queue, dropped when task exits. each rq
2563 * in-flight on this queue also holds a reference, dropped when rq is freed.
2565 * Each cfq queue took a reference on the parent group. Drop it now.
2566 * queue lock must be held here.
2568 static void cfq_put_queue(struct cfq_queue *cfqq)
2570 struct cfq_data *cfqd = cfqq->cfqd;
2571 struct cfq_group *cfqg;
2573 BUG_ON(cfqq->ref <= 0);
2579 cfq_log_cfqq(cfqd, cfqq, "put_queue");
2580 BUG_ON(rb_first(&cfqq->sort_list));
2581 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
2584 if (unlikely(cfqd->active_queue == cfqq)) {
2585 __cfq_slice_expired(cfqd, cfqq, 0);
2586 cfq_schedule_dispatch(cfqd);
2589 BUG_ON(cfq_cfqq_on_rr(cfqq));
2590 kmem_cache_free(cfq_pool, cfqq);
2591 blkg_put(cfqg_to_blkg(cfqg));
2594 static void cfq_put_cooperator(struct cfq_queue *cfqq)
2596 struct cfq_queue *__cfqq, *next;
2599 * If this queue was scheduled to merge with another queue, be
2600 * sure to drop the reference taken on that queue (and others in
2601 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
2603 __cfqq = cfqq->new_cfqq;
2605 if (__cfqq == cfqq) {
2606 WARN(1, "cfqq->new_cfqq loop detected\n");
2609 next = __cfqq->new_cfqq;
2610 cfq_put_queue(__cfqq);
2615 static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2617 if (unlikely(cfqq == cfqd->active_queue)) {
2618 __cfq_slice_expired(cfqd, cfqq, 0);
2619 cfq_schedule_dispatch(cfqd);
2622 cfq_put_cooperator(cfqq);
2624 cfq_put_queue(cfqq);
2627 static void cfq_init_icq(struct io_cq *icq)
2629 struct cfq_io_cq *cic = icq_to_cic(icq);
2631 cic->ttime.last_end_request = jiffies;
2634 static void cfq_exit_icq(struct io_cq *icq)
2636 struct cfq_io_cq *cic = icq_to_cic(icq);
2637 struct cfq_data *cfqd = cic_to_cfqd(cic);
2639 if (cic->cfqq[BLK_RW_ASYNC]) {
2640 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
2641 cic->cfqq[BLK_RW_ASYNC] = NULL;
2644 if (cic->cfqq[BLK_RW_SYNC]) {
2645 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
2646 cic->cfqq[BLK_RW_SYNC] = NULL;
2650 static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc)
2652 struct task_struct *tsk = current;
2655 if (!cfq_cfqq_prio_changed(cfqq))
2658 ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio);
2659 switch (ioprio_class) {
2661 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
2662 case IOPRIO_CLASS_NONE:
2664 * no prio set, inherit CPU scheduling settings
2666 cfqq->ioprio = task_nice_ioprio(tsk);
2667 cfqq->ioprio_class = task_nice_ioclass(tsk);
2669 case IOPRIO_CLASS_RT:
2670 cfqq->ioprio = task_ioprio(ioc);
2671 cfqq->ioprio_class = IOPRIO_CLASS_RT;
2673 case IOPRIO_CLASS_BE:
2674 cfqq->ioprio = task_ioprio(ioc);
2675 cfqq->ioprio_class = IOPRIO_CLASS_BE;
2677 case IOPRIO_CLASS_IDLE:
2678 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
2680 cfq_clear_cfqq_idle_window(cfqq);
2685 * keep track of original prio settings in case we have to temporarily
2686 * elevate the priority of this queue
2688 cfqq->org_ioprio = cfqq->ioprio;
2689 cfq_clear_cfqq_prio_changed(cfqq);
2692 static void changed_ioprio(struct cfq_io_cq *cic)
2694 struct cfq_data *cfqd = cic_to_cfqd(cic);
2695 struct cfq_queue *cfqq;
2697 if (unlikely(!cfqd))
2700 cfqq = cic->cfqq[BLK_RW_ASYNC];
2702 struct cfq_queue *new_cfqq;
2703 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic->icq.ioc,
2706 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
2707 cfq_put_queue(cfqq);
2711 cfqq = cic->cfqq[BLK_RW_SYNC];
2713 cfq_mark_cfqq_prio_changed(cfqq);
2716 static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2717 pid_t pid, bool is_sync)
2719 RB_CLEAR_NODE(&cfqq->rb_node);
2720 RB_CLEAR_NODE(&cfqq->p_node);
2721 INIT_LIST_HEAD(&cfqq->fifo);
2726 cfq_mark_cfqq_prio_changed(cfqq);
2729 if (!cfq_class_idle(cfqq))
2730 cfq_mark_cfqq_idle_window(cfqq);
2731 cfq_mark_cfqq_sync(cfqq);
2736 #ifdef CONFIG_CFQ_GROUP_IOSCHED
2737 static void changed_cgroup(struct cfq_io_cq *cic)
2739 struct cfq_queue *sync_cfqq = cic_to_cfqq(cic, 1);
2740 struct cfq_data *cfqd = cic_to_cfqd(cic);
2741 struct request_queue *q;
2743 if (unlikely(!cfqd))
2750 * Drop reference to sync queue. A new sync queue will be
2751 * assigned in new group upon arrival of a fresh request.
2753 cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
2754 cic_set_cfqq(cic, NULL, 1);
2755 cfq_put_queue(sync_cfqq);
2758 #endif /* CONFIG_CFQ_GROUP_IOSCHED */
2760 static struct cfq_queue *
2761 cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync,
2762 struct io_context *ioc, gfp_t gfp_mask)
2764 struct blkio_cgroup *blkcg;
2765 struct cfq_queue *cfqq, *new_cfqq = NULL;
2766 struct cfq_io_cq *cic;
2767 struct cfq_group *cfqg;
2772 blkcg = task_blkio_cgroup(current);
2774 cfqg = cfq_lookup_create_cfqg(cfqd, blkcg);
2776 cic = cfq_cic_lookup(cfqd, ioc);
2777 /* cic always exists here */
2778 cfqq = cic_to_cfqq(cic, is_sync);
2781 * Always try a new alloc if we fell back to the OOM cfqq
2782 * originally, since it should just be a temporary situation.
2784 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
2789 } else if (gfp_mask & __GFP_WAIT) {
2791 spin_unlock_irq(cfqd->queue->queue_lock);
2792 new_cfqq = kmem_cache_alloc_node(cfq_pool,
2793 gfp_mask | __GFP_ZERO,
2795 spin_lock_irq(cfqd->queue->queue_lock);
2799 cfqq = kmem_cache_alloc_node(cfq_pool,
2800 gfp_mask | __GFP_ZERO,
2805 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
2806 cfq_init_prio_data(cfqq, ioc);
2807 cfq_link_cfqq_cfqg(cfqq, cfqg);
2808 cfq_log_cfqq(cfqd, cfqq, "alloced");
2810 cfqq = &cfqd->oom_cfqq;
2814 kmem_cache_free(cfq_pool, new_cfqq);
2820 static struct cfq_queue **
2821 cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
2823 switch (ioprio_class) {
2824 case IOPRIO_CLASS_RT:
2825 return &cfqd->async_cfqq[0][ioprio];
2826 case IOPRIO_CLASS_BE:
2827 return &cfqd->async_cfqq[1][ioprio];
2828 case IOPRIO_CLASS_IDLE:
2829 return &cfqd->async_idle_cfqq;
2835 static struct cfq_queue *
2836 cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct io_context *ioc,
2839 const int ioprio = task_ioprio(ioc);
2840 const int ioprio_class = task_ioprio_class(ioc);
2841 struct cfq_queue **async_cfqq = NULL;
2842 struct cfq_queue *cfqq = NULL;
2845 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
2850 cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, gfp_mask);
2853 * pin the queue now that it's allocated, scheduler exit will prune it
2855 if (!is_sync && !(*async_cfqq)) {
2865 __cfq_update_io_thinktime(struct cfq_ttime *ttime, unsigned long slice_idle)
2867 unsigned long elapsed = jiffies - ttime->last_end_request;
2868 elapsed = min(elapsed, 2UL * slice_idle);
2870 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
2871 ttime->ttime_total = (7*ttime->ttime_total + 256*elapsed) / 8;
2872 ttime->ttime_mean = (ttime->ttime_total + 128) / ttime->ttime_samples;
2876 cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2877 struct cfq_io_cq *cic)
2879 if (cfq_cfqq_sync(cfqq)) {
2880 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
2881 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
2882 cfqd->cfq_slice_idle);
2884 #ifdef CONFIG_CFQ_GROUP_IOSCHED
2885 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
2890 cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2894 sector_t n_sec = blk_rq_sectors(rq);
2895 if (cfqq->last_request_pos) {
2896 if (cfqq->last_request_pos < blk_rq_pos(rq))
2897 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
2899 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
2902 cfqq->seek_history <<= 1;
2903 if (blk_queue_nonrot(cfqd->queue))
2904 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
2906 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
2910 * Disable idle window if the process thinks too long or seeks so much that
2914 cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2915 struct cfq_io_cq *cic)
2917 int old_idle, enable_idle;
2920 * Don't idle for async or idle io prio class
2922 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
2925 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
2927 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
2928 cfq_mark_cfqq_deep(cfqq);
2930 if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE))
2932 else if (!atomic_read(&cic->icq.ioc->nr_tasks) ||
2933 !cfqd->cfq_slice_idle ||
2934 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
2936 else if (sample_valid(cic->ttime.ttime_samples)) {
2937 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
2943 if (old_idle != enable_idle) {
2944 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
2946 cfq_mark_cfqq_idle_window(cfqq);
2948 cfq_clear_cfqq_idle_window(cfqq);
2953 * Check if new_cfqq should preempt the currently active queue. Return 0 for
2954 * no or if we aren't sure, a 1 will cause a preempt.
2957 cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
2960 struct cfq_queue *cfqq;
2962 cfqq = cfqd->active_queue;
2966 if (cfq_class_idle(new_cfqq))
2969 if (cfq_class_idle(cfqq))
2973 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
2975 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
2979 * if the new request is sync, but the currently running queue is
2980 * not, let the sync request have priority.
2982 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
2985 if (new_cfqq->cfqg != cfqq->cfqg)
2988 if (cfq_slice_used(cfqq))
2991 /* Allow preemption only if we are idling on sync-noidle tree */
2992 if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD &&
2993 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
2994 new_cfqq->service_tree->count == 2 &&
2995 RB_EMPTY_ROOT(&cfqq->sort_list))
2999 * So both queues are sync. Let the new request get disk time if
3000 * it's a metadata request and the current queue is doing regular IO.
3002 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
3006 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
3008 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
3011 /* An idle queue should not be idle now for some reason */
3012 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
3015 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
3019 * if this request is as-good as one we would expect from the
3020 * current cfqq, let it preempt
3022 if (cfq_rq_close(cfqd, cfqq, rq))
3029 * cfqq preempts the active queue. if we allowed preempt with no slice left,
3030 * let it have half of its nominal slice.
3032 static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3034 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
3036 cfq_log_cfqq(cfqd, cfqq, "preempt");
3037 cfq_slice_expired(cfqd, 1);
3040 * workload type is changed, don't save slice, otherwise preempt
3043 if (old_type != cfqq_type(cfqq))
3044 cfqq->cfqg->saved_workload_slice = 0;
3047 * Put the new queue at the front of the of the current list,
3048 * so we know that it will be selected next.
3050 BUG_ON(!cfq_cfqq_on_rr(cfqq));
3052 cfq_service_tree_add(cfqd, cfqq, 1);
3054 cfqq->slice_end = 0;
3055 cfq_mark_cfqq_slice_new(cfqq);
3059 * Called when a new fs request (rq) is added (to cfqq). Check if there's
3060 * something we should do about it
3063 cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3066 struct cfq_io_cq *cic = RQ_CIC(rq);
3069 if (rq->cmd_flags & REQ_PRIO)
3070 cfqq->prio_pending++;
3072 cfq_update_io_thinktime(cfqd, cfqq, cic);
3073 cfq_update_io_seektime(cfqd, cfqq, rq);
3074 cfq_update_idle_window(cfqd, cfqq, cic);
3076 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
3078 if (cfqq == cfqd->active_queue) {
3080 * Remember that we saw a request from this process, but
3081 * don't start queuing just yet. Otherwise we risk seeing lots
3082 * of tiny requests, because we disrupt the normal plugging
3083 * and merging. If the request is already larger than a single
3084 * page, let it rip immediately. For that case we assume that
3085 * merging is already done. Ditto for a busy system that
3086 * has other work pending, don't risk delaying until the
3087 * idle timer unplug to continue working.
3089 if (cfq_cfqq_wait_request(cfqq)) {
3090 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3091 cfqd->busy_queues > 1) {
3092 cfq_del_timer(cfqd, cfqq);
3093 cfq_clear_cfqq_wait_request(cfqq);
3094 __blk_run_queue(cfqd->queue);
3096 cfq_blkiocg_update_idle_time_stats(
3097 cfqg_to_blkg(cfqq->cfqg));
3098 cfq_mark_cfqq_must_dispatch(cfqq);
3101 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
3103 * not the active queue - expire current slice if it is
3104 * idle and has expired it's mean thinktime or this new queue
3105 * has some old slice time left and is of higher priority or
3106 * this new queue is RT and the current one is BE
3108 cfq_preempt_queue(cfqd, cfqq);
3109 __blk_run_queue(cfqd->queue);
3113 static void cfq_insert_request(struct request_queue *q, struct request *rq)
3115 struct cfq_data *cfqd = q->elevator->elevator_data;
3116 struct cfq_queue *cfqq = RQ_CFQQ(rq);
3118 cfq_log_cfqq(cfqd, cfqq, "insert_request");
3119 cfq_init_prio_data(cfqq, RQ_CIC(rq)->icq.ioc);
3121 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
3122 list_add_tail(&rq->queuelist, &cfqq->fifo);
3124 cfq_blkiocg_update_io_add_stats(cfqg_to_blkg(RQ_CFQG(rq)),
3125 cfqg_to_blkg(cfqd->serving_group),
3126 rq_data_dir(rq), rq_is_sync(rq));
3127 cfq_rq_enqueued(cfqd, cfqq, rq);
3131 * Update hw_tag based on peak queue depth over 50 samples under
3134 static void cfq_update_hw_tag(struct cfq_data *cfqd)
3136 struct cfq_queue *cfqq = cfqd->active_queue;
3138 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
3139 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
3141 if (cfqd->hw_tag == 1)
3144 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
3145 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
3149 * If active queue hasn't enough requests and can idle, cfq might not
3150 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3153 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3154 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
3155 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
3158 if (cfqd->hw_tag_samples++ < 50)
3161 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
3167 static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3169 struct cfq_io_cq *cic = cfqd->active_cic;
3171 /* If the queue already has requests, don't wait */
3172 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
3175 /* If there are other queues in the group, don't wait */
3176 if (cfqq->cfqg->nr_cfqq > 1)
3179 /* the only queue in the group, but think time is big */
3180 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
3183 if (cfq_slice_used(cfqq))
3186 /* if slice left is less than think time, wait busy */
3187 if (cic && sample_valid(cic->ttime.ttime_samples)
3188 && (cfqq->slice_end - jiffies < cic->ttime.ttime_mean))
3192 * If think times is less than a jiffy than ttime_mean=0 and above
3193 * will not be true. It might happen that slice has not expired yet
3194 * but will expire soon (4-5 ns) during select_queue(). To cover the
3195 * case where think time is less than a jiffy, mark the queue wait
3196 * busy if only 1 jiffy is left in the slice.
3198 if (cfqq->slice_end - jiffies == 1)
3204 static void cfq_completed_request(struct request_queue *q, struct request *rq)
3206 struct cfq_queue *cfqq = RQ_CFQQ(rq);
3207 struct cfq_data *cfqd = cfqq->cfqd;
3208 const int sync = rq_is_sync(rq);
3212 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d",
3213 !!(rq->cmd_flags & REQ_NOIDLE));
3215 cfq_update_hw_tag(cfqd);
3217 WARN_ON(!cfqd->rq_in_driver);
3218 WARN_ON(!cfqq->dispatched);
3219 cfqd->rq_in_driver--;
3221 (RQ_CFQG(rq))->dispatched--;
3222 cfq_blkiocg_update_completion_stats(cfqg_to_blkg(cfqq->cfqg),
3223 rq_start_time_ns(rq), rq_io_start_time_ns(rq),
3224 rq_data_dir(rq), rq_is_sync(rq));
3226 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
3229 struct cfq_rb_root *service_tree;
3231 RQ_CIC(rq)->ttime.last_end_request = now;
3233 if (cfq_cfqq_on_rr(cfqq))
3234 service_tree = cfqq->service_tree;
3236 service_tree = service_tree_for(cfqq->cfqg,
3237 cfqq_prio(cfqq), cfqq_type(cfqq));
3238 service_tree->ttime.last_end_request = now;
3239 if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
3240 cfqd->last_delayed_sync = now;
3243 #ifdef CONFIG_CFQ_GROUP_IOSCHED
3244 cfqq->cfqg->ttime.last_end_request = now;
3248 * If this is the active queue, check if it needs to be expired,
3249 * or if we want to idle in case it has no pending requests.
3251 if (cfqd->active_queue == cfqq) {
3252 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
3254 if (cfq_cfqq_slice_new(cfqq)) {
3255 cfq_set_prio_slice(cfqd, cfqq);
3256 cfq_clear_cfqq_slice_new(cfqq);
3260 * Should we wait for next request to come in before we expire
3263 if (cfq_should_wait_busy(cfqd, cfqq)) {
3264 unsigned long extend_sl = cfqd->cfq_slice_idle;
3265 if (!cfqd->cfq_slice_idle)
3266 extend_sl = cfqd->cfq_group_idle;
3267 cfqq->slice_end = jiffies + extend_sl;
3268 cfq_mark_cfqq_wait_busy(cfqq);
3269 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
3273 * Idling is not enabled on:
3275 * - idle-priority queues
3277 * - queues with still some requests queued
3278 * - when there is a close cooperator
3280 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
3281 cfq_slice_expired(cfqd, 1);
3282 else if (sync && cfqq_empty &&
3283 !cfq_close_cooperator(cfqd, cfqq)) {
3284 cfq_arm_slice_timer(cfqd);
3288 if (!cfqd->rq_in_driver)
3289 cfq_schedule_dispatch(cfqd);
3292 static inline int __cfq_may_queue(struct cfq_queue *cfqq)
3294 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
3295 cfq_mark_cfqq_must_alloc_slice(cfqq);
3296 return ELV_MQUEUE_MUST;
3299 return ELV_MQUEUE_MAY;
3302 static int cfq_may_queue(struct request_queue *q, int rw)
3304 struct cfq_data *cfqd = q->elevator->elevator_data;
3305 struct task_struct *tsk = current;
3306 struct cfq_io_cq *cic;
3307 struct cfq_queue *cfqq;
3310 * don't force setup of a queue from here, as a call to may_queue
3311 * does not necessarily imply that a request actually will be queued.
3312 * so just lookup a possibly existing queue, or return 'may queue'
3315 cic = cfq_cic_lookup(cfqd, tsk->io_context);
3317 return ELV_MQUEUE_MAY;
3319 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
3321 cfq_init_prio_data(cfqq, cic->icq.ioc);
3323 return __cfq_may_queue(cfqq);
3326 return ELV_MQUEUE_MAY;
3330 * queue lock held here
3332 static void cfq_put_request(struct request *rq)
3334 struct cfq_queue *cfqq = RQ_CFQQ(rq);
3337 const int rw = rq_data_dir(rq);
3339 BUG_ON(!cfqq->allocated[rw]);
3340 cfqq->allocated[rw]--;
3342 /* Put down rq reference on cfqg */
3343 blkg_put(cfqg_to_blkg(RQ_CFQG(rq)));
3344 rq->elv.priv[0] = NULL;
3345 rq->elv.priv[1] = NULL;
3347 cfq_put_queue(cfqq);
3351 static struct cfq_queue *
3352 cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
3353 struct cfq_queue *cfqq)
3355 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
3356 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
3357 cfq_mark_cfqq_coop(cfqq->new_cfqq);
3358 cfq_put_queue(cfqq);
3359 return cic_to_cfqq(cic, 1);
3363 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
3364 * was the last process referring to said cfqq.
3366 static struct cfq_queue *
3367 split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
3369 if (cfqq_process_refs(cfqq) == 1) {
3370 cfqq->pid = current->pid;
3371 cfq_clear_cfqq_coop(cfqq);
3372 cfq_clear_cfqq_split_coop(cfqq);
3376 cic_set_cfqq(cic, NULL, 1);
3378 cfq_put_cooperator(cfqq);
3380 cfq_put_queue(cfqq);
3384 * Allocate cfq data structures associated with this request.
3387 cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
3389 struct cfq_data *cfqd = q->elevator->elevator_data;
3390 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
3391 const int rw = rq_data_dir(rq);
3392 const bool is_sync = rq_is_sync(rq);
3393 struct cfq_queue *cfqq;
3394 unsigned int changed;
3396 might_sleep_if(gfp_mask & __GFP_WAIT);
3398 spin_lock_irq(q->queue_lock);
3400 /* handle changed notifications */
3401 changed = icq_get_changed(&cic->icq);
3402 if (unlikely(changed & ICQ_IOPRIO_CHANGED))
3403 changed_ioprio(cic);
3404 #ifdef CONFIG_CFQ_GROUP_IOSCHED
3405 if (unlikely(changed & ICQ_CGROUP_CHANGED))
3406 changed_cgroup(cic);
3410 cfqq = cic_to_cfqq(cic, is_sync);
3411 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
3412 cfqq = cfq_get_queue(cfqd, is_sync, cic->icq.ioc, gfp_mask);
3413 cic_set_cfqq(cic, cfqq, is_sync);
3416 * If the queue was seeky for too long, break it apart.
3418 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
3419 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
3420 cfqq = split_cfqq(cic, cfqq);
3426 * Check to see if this queue is scheduled to merge with
3427 * another, closely cooperating queue. The merging of
3428 * queues happens here as it must be done in process context.
3429 * The reference on new_cfqq was taken in merge_cfqqs.
3432 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
3435 cfqq->allocated[rw]++;
3438 blkg_get(cfqg_to_blkg(cfqq->cfqg));
3439 rq->elv.priv[0] = cfqq;
3440 rq->elv.priv[1] = cfqq->cfqg;
3441 spin_unlock_irq(q->queue_lock);
3445 static void cfq_kick_queue(struct work_struct *work)
3447 struct cfq_data *cfqd =
3448 container_of(work, struct cfq_data, unplug_work);
3449 struct request_queue *q = cfqd->queue;
3451 spin_lock_irq(q->queue_lock);
3452 __blk_run_queue(cfqd->queue);
3453 spin_unlock_irq(q->queue_lock);
3457 * Timer running if the active_queue is currently idling inside its time slice
3459 static void cfq_idle_slice_timer(unsigned long data)
3461 struct cfq_data *cfqd = (struct cfq_data *) data;
3462 struct cfq_queue *cfqq;
3463 unsigned long flags;
3466 cfq_log(cfqd, "idle timer fired");
3468 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
3470 cfqq = cfqd->active_queue;
3475 * We saw a request before the queue expired, let it through
3477 if (cfq_cfqq_must_dispatch(cfqq))
3483 if (cfq_slice_used(cfqq))
3487 * only expire and reinvoke request handler, if there are
3488 * other queues with pending requests
3490 if (!cfqd->busy_queues)
3494 * not expired and it has a request pending, let it dispatch
3496 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
3500 * Queue depth flag is reset only when the idle didn't succeed
3502 cfq_clear_cfqq_deep(cfqq);
3505 cfq_slice_expired(cfqd, timed_out);
3507 cfq_schedule_dispatch(cfqd);
3509 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
3512 static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
3514 del_timer_sync(&cfqd->idle_slice_timer);
3515 cancel_work_sync(&cfqd->unplug_work);
3518 static void cfq_put_async_queues(struct cfq_data *cfqd)
3522 for (i = 0; i < IOPRIO_BE_NR; i++) {
3523 if (cfqd->async_cfqq[0][i])
3524 cfq_put_queue(cfqd->async_cfqq[0][i]);
3525 if (cfqd->async_cfqq[1][i])
3526 cfq_put_queue(cfqd->async_cfqq[1][i]);
3529 if (cfqd->async_idle_cfqq)
3530 cfq_put_queue(cfqd->async_idle_cfqq);
3533 static void cfq_exit_queue(struct elevator_queue *e)
3535 struct cfq_data *cfqd = e->elevator_data;
3536 struct request_queue *q = cfqd->queue;
3539 cfq_shutdown_timer_wq(cfqd);
3541 spin_lock_irq(q->queue_lock);
3543 if (cfqd->active_queue)
3544 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
3546 cfq_put_async_queues(cfqd);
3547 cfq_release_cfq_groups(cfqd);
3550 * If there are groups which we could not unlink from blkcg list,
3551 * wait for a rcu period for them to be freed.
3553 if (cfqd->nr_blkcg_linked_grps)
3556 spin_unlock_irq(q->queue_lock);
3558 cfq_shutdown_timer_wq(cfqd);
3561 * Wait for cfqg->blkg->key accessors to exit their grace periods.
3562 * Do this wait only if there are other unlinked groups out
3563 * there. This can happen if cgroup deletion path claimed the
3564 * responsibility of cleaning up a group before queue cleanup code
3567 * Do not call synchronize_rcu() unconditionally as there are drivers
3568 * which create/delete request queue hundreds of times during scan/boot
3569 * and synchronize_rcu() can take significant time and slow down boot.
3574 #ifndef CONFIG_CFQ_GROUP_IOSCHED
3575 kfree(cfqd->root_group);
3580 static int cfq_init_queue(struct request_queue *q)
3582 struct cfq_data *cfqd;
3583 struct blkio_group *blkg __maybe_unused;
3586 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
3591 q->elevator->elevator_data = cfqd;
3593 /* Init root service tree */
3594 cfqd->grp_service_tree = CFQ_RB_ROOT;
3596 /* Init root group and prefer root group over other groups by default */
3597 #ifdef CONFIG_CFQ_GROUP_IOSCHED
3599 spin_lock_irq(q->queue_lock);
3601 blkg = blkg_lookup_create(&blkio_root_cgroup, q, BLKIO_POLICY_PROP,
3604 cfqd->root_group = blkg_to_cfqg(blkg);
3606 spin_unlock_irq(q->queue_lock);
3609 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
3610 GFP_KERNEL, cfqd->queue->node);
3611 if (cfqd->root_group)
3612 cfq_init_cfqg_base(cfqd->root_group);
3614 if (!cfqd->root_group) {
3619 cfqd->root_group->weight = 2*BLKIO_WEIGHT_DEFAULT;
3622 * Not strictly needed (since RB_ROOT just clears the node and we
3623 * zeroed cfqd on alloc), but better be safe in case someone decides
3624 * to add magic to the rb code
3626 for (i = 0; i < CFQ_PRIO_LISTS; i++)
3627 cfqd->prio_trees[i] = RB_ROOT;
3630 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
3631 * Grab a permanent reference to it, so that the normal code flow
3632 * will not attempt to free it. oom_cfqq is linked to root_group
3633 * but shouldn't hold a reference as it'll never be unlinked. Lose
3634 * the reference from linking right away.
3636 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
3637 cfqd->oom_cfqq.ref++;
3639 spin_lock_irq(q->queue_lock);
3640 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
3641 blkg_put(cfqg_to_blkg(cfqd->root_group));
3642 spin_unlock_irq(q->queue_lock);
3644 init_timer(&cfqd->idle_slice_timer);
3645 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
3646 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
3648 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
3650 cfqd->cfq_quantum = cfq_quantum;
3651 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
3652 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
3653 cfqd->cfq_back_max = cfq_back_max;
3654 cfqd->cfq_back_penalty = cfq_back_penalty;
3655 cfqd->cfq_slice[0] = cfq_slice_async;
3656 cfqd->cfq_slice[1] = cfq_slice_sync;
3657 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
3658 cfqd->cfq_slice_idle = cfq_slice_idle;
3659 cfqd->cfq_group_idle = cfq_group_idle;
3660 cfqd->cfq_latency = 1;
3663 * we optimistically start assuming sync ops weren't delayed in last
3664 * second, in order to have larger depth for async operations.
3666 cfqd->last_delayed_sync = jiffies - HZ;
3671 * sysfs parts below -->
3674 cfq_var_show(unsigned int var, char *page)
3676 return sprintf(page, "%d\n", var);
3680 cfq_var_store(unsigned int *var, const char *page, size_t count)
3682 char *p = (char *) page;
3684 *var = simple_strtoul(p, &p, 10);
3688 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
3689 static ssize_t __FUNC(struct elevator_queue *e, char *page) \
3691 struct cfq_data *cfqd = e->elevator_data; \
3692 unsigned int __data = __VAR; \
3694 __data = jiffies_to_msecs(__data); \
3695 return cfq_var_show(__data, (page)); \
3697 SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
3698 SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
3699 SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
3700 SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
3701 SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
3702 SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
3703 SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
3704 SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
3705 SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
3706 SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
3707 SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
3708 #undef SHOW_FUNCTION
3710 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
3711 static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
3713 struct cfq_data *cfqd = e->elevator_data; \
3714 unsigned int __data; \
3715 int ret = cfq_var_store(&__data, (page), count); \
3716 if (__data < (MIN)) \
3718 else if (__data > (MAX)) \
3721 *(__PTR) = msecs_to_jiffies(__data); \
3723 *(__PTR) = __data; \
3726 STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
3727 STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
3729 STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
3731 STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
3732 STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
3734 STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
3735 STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
3736 STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
3737 STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
3738 STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
3740 STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
3741 #undef STORE_FUNCTION
3743 #define CFQ_ATTR(name) \
3744 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
3746 static struct elv_fs_entry cfq_attrs[] = {
3748 CFQ_ATTR(fifo_expire_sync),
3749 CFQ_ATTR(fifo_expire_async),
3750 CFQ_ATTR(back_seek_max),
3751 CFQ_ATTR(back_seek_penalty),
3752 CFQ_ATTR(slice_sync),
3753 CFQ_ATTR(slice_async),
3754 CFQ_ATTR(slice_async_rq),
3755 CFQ_ATTR(slice_idle),
3756 CFQ_ATTR(group_idle),
3757 CFQ_ATTR(low_latency),
3761 static struct elevator_type iosched_cfq = {
3763 .elevator_merge_fn = cfq_merge,
3764 .elevator_merged_fn = cfq_merged_request,
3765 .elevator_merge_req_fn = cfq_merged_requests,
3766 .elevator_allow_merge_fn = cfq_allow_merge,
3767 .elevator_bio_merged_fn = cfq_bio_merged,
3768 .elevator_dispatch_fn = cfq_dispatch_requests,
3769 .elevator_add_req_fn = cfq_insert_request,
3770 .elevator_activate_req_fn = cfq_activate_request,
3771 .elevator_deactivate_req_fn = cfq_deactivate_request,
3772 .elevator_completed_req_fn = cfq_completed_request,
3773 .elevator_former_req_fn = elv_rb_former_request,
3774 .elevator_latter_req_fn = elv_rb_latter_request,
3775 .elevator_init_icq_fn = cfq_init_icq,
3776 .elevator_exit_icq_fn = cfq_exit_icq,
3777 .elevator_set_req_fn = cfq_set_request,
3778 .elevator_put_req_fn = cfq_put_request,
3779 .elevator_may_queue_fn = cfq_may_queue,
3780 .elevator_init_fn = cfq_init_queue,
3781 .elevator_exit_fn = cfq_exit_queue,
3783 .icq_size = sizeof(struct cfq_io_cq),
3784 .icq_align = __alignof__(struct cfq_io_cq),
3785 .elevator_attrs = cfq_attrs,
3786 .elevator_name = "cfq",
3787 .elevator_owner = THIS_MODULE,
3790 #ifdef CONFIG_CFQ_GROUP_IOSCHED
3791 static struct blkio_policy_type blkio_policy_cfq = {
3793 .blkio_init_group_fn = cfq_init_blkio_group,
3794 .blkio_link_group_fn = cfq_link_blkio_group,
3795 .blkio_unlink_group_fn = cfq_unlink_blkio_group,
3796 .blkio_clear_queue_fn = cfq_clear_queue,
3797 .blkio_update_group_weight_fn = cfq_update_blkio_group_weight,
3799 .plid = BLKIO_POLICY_PROP,
3800 .pdata_size = sizeof(struct cfq_group),
3804 static int __init cfq_init(void)
3809 * could be 0 on HZ < 1000 setups
3811 if (!cfq_slice_async)
3812 cfq_slice_async = 1;
3813 if (!cfq_slice_idle)
3816 #ifdef CONFIG_CFQ_GROUP_IOSCHED
3817 if (!cfq_group_idle)
3822 cfq_pool = KMEM_CACHE(cfq_queue, 0);
3826 ret = elv_register(&iosched_cfq);
3828 kmem_cache_destroy(cfq_pool);
3832 #ifdef CONFIG_CFQ_GROUP_IOSCHED
3833 blkio_policy_register(&blkio_policy_cfq);
3838 static void __exit cfq_exit(void)
3840 #ifdef CONFIG_CFQ_GROUP_IOSCHED
3841 blkio_policy_unregister(&blkio_policy_cfq);
3843 elv_unregister(&iosched_cfq);
3844 kmem_cache_destroy(cfq_pool);
3847 module_init(cfq_init);
3848 module_exit(cfq_exit);
3850 MODULE_AUTHOR("Jens Axboe");
3851 MODULE_LICENSE("GPL");
3852 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");