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/sched/clock.h>
12 #include <linux/blkdev.h>
13 #include <linux/elevator.h>
14 #include <linux/ktime.h>
15 #include <linux/rbtree.h>
16 #include <linux/ioprio.h>
17 #include <linux/blktrace_api.h>
18 #include <linux/blk-cgroup.h>
25 /* max queue in one round of service */
26 static const int cfq_quantum = 8;
27 static const u64 cfq_fifo_expire[2] = { NSEC_PER_SEC / 4, NSEC_PER_SEC / 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 u64 cfq_slice_sync = NSEC_PER_SEC / 10;
33 static u64 cfq_slice_async = NSEC_PER_SEC / 25;
34 static const int cfq_slice_async_rq = 2;
35 static u64 cfq_slice_idle = NSEC_PER_SEC / 125;
36 static u64 cfq_group_idle = NSEC_PER_SEC / 125;
37 static const u64 cfq_target_latency = (u64)NSEC_PER_SEC * 3/10; /* 300 ms */
38 static const int cfq_hist_divisor = 4;
41 * offset from end of service tree
43 #define CFQ_IDLE_DELAY (NSEC_PER_SEC / 5)
46 * below this threshold, we consider thinktime immediate
48 #define CFQ_MIN_TT (2 * NSEC_PER_SEC / HZ)
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)
72 /* blkio-related constants */
73 #define CFQ_WEIGHT_LEGACY_MIN 10
74 #define CFQ_WEIGHT_LEGACY_DFL 500
75 #define CFQ_WEIGHT_LEGACY_MAX 1000
82 unsigned long ttime_samples;
86 * Most of our rbtree usage is for sorting with min extraction, so
87 * if we cache the leftmost node we don't have to walk down the tree
88 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
89 * move this into the elevator for the rq sorting as well.
96 struct cfq_ttime ttime;
98 #define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
99 .ttime = {.last_end_request = ktime_get_ns(),},}
102 * Per process-grouping structure
105 /* reference count */
107 /* various state flags, see below */
109 /* parent cfq_data */
110 struct cfq_data *cfqd;
111 /* service_tree member */
112 struct rb_node rb_node;
113 /* service_tree key */
115 /* prio tree member */
116 struct rb_node p_node;
117 /* prio tree root we belong to, if any */
118 struct rb_root *p_root;
119 /* sorted list of pending requests */
120 struct rb_root sort_list;
121 /* if fifo isn't expired, next request to serve */
122 struct request *next_rq;
123 /* requests queued in sort_list */
125 /* currently allocated requests */
127 /* fifo list of requests in sort_list */
128 struct list_head fifo;
130 /* time when queue got scheduled in to dispatch first request. */
134 /* time when first request from queue completed and slice started. */
139 /* pending priority requests */
141 /* number of requests that are on the dispatch list or inside driver */
144 /* io prio of this group */
145 unsigned short ioprio, org_ioprio;
146 unsigned short ioprio_class, org_ioprio_class;
151 sector_t last_request_pos;
153 struct cfq_rb_root *service_tree;
154 struct cfq_queue *new_cfqq;
155 struct cfq_group *cfqg;
156 /* Number of sectors dispatched from queue in single dispatch round */
157 unsigned long nr_sectors;
161 * First index in the service_trees.
162 * IDLE is handled separately, so it has negative index
172 * Second index in the service_trees.
176 SYNC_NOIDLE_WORKLOAD = 1,
181 #ifdef CONFIG_CFQ_GROUP_IOSCHED
182 /* number of ios merged */
183 struct blkg_rwstat merged;
184 /* total time spent on device in ns, may not be accurate w/ queueing */
185 struct blkg_rwstat service_time;
186 /* total time spent waiting in scheduler queue in ns */
187 struct blkg_rwstat wait_time;
188 /* number of IOs queued up */
189 struct blkg_rwstat queued;
190 /* total disk time and nr sectors dispatched by this group */
191 struct blkg_stat time;
192 #ifdef CONFIG_DEBUG_BLK_CGROUP
193 /* time not charged to this cgroup */
194 struct blkg_stat unaccounted_time;
195 /* sum of number of ios queued across all samples */
196 struct blkg_stat avg_queue_size_sum;
197 /* count of samples taken for average */
198 struct blkg_stat avg_queue_size_samples;
199 /* how many times this group has been removed from service tree */
200 struct blkg_stat dequeue;
201 /* total time spent waiting for it to be assigned a timeslice. */
202 struct blkg_stat group_wait_time;
203 /* time spent idling for this blkcg_gq */
204 struct blkg_stat idle_time;
205 /* total time with empty current active q with other requests queued */
206 struct blkg_stat empty_time;
207 /* fields after this shouldn't be cleared on stat reset */
208 uint64_t start_group_wait_time;
209 uint64_t start_idle_time;
210 uint64_t start_empty_time;
212 #endif /* CONFIG_DEBUG_BLK_CGROUP */
213 #endif /* CONFIG_CFQ_GROUP_IOSCHED */
216 /* Per-cgroup data */
217 struct cfq_group_data {
218 /* must be the first member */
219 struct blkcg_policy_data cpd;
222 unsigned int leaf_weight;
225 /* This is per cgroup per device grouping structure */
227 /* must be the first member */
228 struct blkg_policy_data pd;
230 /* group service_tree member */
231 struct rb_node rb_node;
233 /* group service_tree key */
237 * The number of active cfqgs and sum of their weights under this
238 * cfqg. This covers this cfqg's leaf_weight and all children's
239 * weights, but does not cover weights of further descendants.
241 * If a cfqg is on the service tree, it's active. An active cfqg
242 * also activates its parent and contributes to the children_weight
246 unsigned int children_weight;
249 * vfraction is the fraction of vdisktime that the tasks in this
250 * cfqg are entitled to. This is determined by compounding the
251 * ratios walking up from this cfqg to the root.
253 * It is in fixed point w/ CFQ_SERVICE_SHIFT and the sum of all
254 * vfractions on a service tree is approximately 1. The sum may
255 * deviate a bit due to rounding errors and fluctuations caused by
256 * cfqgs entering and leaving the service tree.
258 unsigned int vfraction;
261 * There are two weights - (internal) weight is the weight of this
262 * cfqg against the sibling cfqgs. leaf_weight is the wight of
263 * this cfqg against the child cfqgs. For the root cfqg, both
264 * weights are kept in sync for backward compatibility.
267 unsigned int new_weight;
268 unsigned int dev_weight;
270 unsigned int leaf_weight;
271 unsigned int new_leaf_weight;
272 unsigned int dev_leaf_weight;
274 /* number of cfqq currently on this group */
278 * Per group busy queues average. Useful for workload slice calc. We
279 * create the array for each prio class but at run time it is used
280 * only for RT and BE class and slot for IDLE class remains unused.
281 * This is primarily done to avoid confusion and a gcc warning.
283 unsigned int busy_queues_avg[CFQ_PRIO_NR];
285 * rr lists of queues with requests. We maintain service trees for
286 * RT and BE classes. These trees are subdivided in subclasses
287 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
288 * class there is no subclassification and all the cfq queues go on
289 * a single tree service_tree_idle.
290 * Counts are embedded in the cfq_rb_root
292 struct cfq_rb_root service_trees[2][3];
293 struct cfq_rb_root service_tree_idle;
296 enum wl_type_t saved_wl_type;
297 enum wl_class_t saved_wl_class;
299 /* number of requests that are on the dispatch list or inside driver */
301 struct cfq_ttime ttime;
302 struct cfqg_stats stats; /* stats for this cfqg */
304 /* async queue for each priority case */
305 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
306 struct cfq_queue *async_idle_cfqq;
311 struct io_cq icq; /* must be the first member */
312 struct cfq_queue *cfqq[2];
313 struct cfq_ttime ttime;
314 int ioprio; /* the current ioprio */
315 #ifdef CONFIG_CFQ_GROUP_IOSCHED
316 uint64_t blkcg_serial_nr; /* the current blkcg serial */
321 * Per block device queue structure
324 struct request_queue *queue;
325 /* Root service tree for cfq_groups */
326 struct cfq_rb_root grp_service_tree;
327 struct cfq_group *root_group;
330 * The priority currently being served
332 enum wl_class_t serving_wl_class;
333 enum wl_type_t serving_wl_type;
334 u64 workload_expires;
335 struct cfq_group *serving_group;
338 * Each priority tree is sorted by next_request position. These
339 * trees are used when determining if two or more queues are
340 * interleaving requests (see cfq_close_cooperator).
342 struct rb_root prio_trees[CFQ_PRIO_LISTS];
344 unsigned int busy_queues;
345 unsigned int busy_sync_queues;
351 * queue-depth detection
357 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
358 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
361 int hw_tag_est_depth;
362 unsigned int hw_tag_samples;
365 * idle window management
367 struct hrtimer idle_slice_timer;
368 struct work_struct unplug_work;
370 struct cfq_queue *active_queue;
371 struct cfq_io_cq *active_cic;
373 sector_t last_position;
376 * tunables, see top of file
378 unsigned int cfq_quantum;
379 unsigned int cfq_back_penalty;
380 unsigned int cfq_back_max;
381 unsigned int cfq_slice_async_rq;
382 unsigned int cfq_latency;
383 u64 cfq_fifo_expire[2];
387 u64 cfq_target_latency;
390 * Fallback dummy cfqq for extreme OOM conditions
392 struct cfq_queue oom_cfqq;
394 u64 last_delayed_sync;
397 static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
398 static void cfq_put_queue(struct cfq_queue *cfqq);
400 static struct cfq_rb_root *st_for(struct cfq_group *cfqg,
401 enum wl_class_t class,
407 if (class == IDLE_WORKLOAD)
408 return &cfqg->service_tree_idle;
410 return &cfqg->service_trees[class][type];
413 enum cfqq_state_flags {
414 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
415 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
416 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
417 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
418 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
419 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
420 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
421 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
422 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
423 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
424 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
425 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
426 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
429 #define CFQ_CFQQ_FNS(name) \
430 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
432 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
434 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
436 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
438 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
440 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
444 CFQ_CFQQ_FNS(wait_request);
445 CFQ_CFQQ_FNS(must_dispatch);
446 CFQ_CFQQ_FNS(must_alloc_slice);
447 CFQ_CFQQ_FNS(fifo_expire);
448 CFQ_CFQQ_FNS(idle_window);
449 CFQ_CFQQ_FNS(prio_changed);
450 CFQ_CFQQ_FNS(slice_new);
453 CFQ_CFQQ_FNS(split_coop);
455 CFQ_CFQQ_FNS(wait_busy);
458 #if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
460 /* cfqg stats flags */
461 enum cfqg_stats_flags {
462 CFQG_stats_waiting = 0,
467 #define CFQG_FLAG_FNS(name) \
468 static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \
470 stats->flags |= (1 << CFQG_stats_##name); \
472 static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats) \
474 stats->flags &= ~(1 << CFQG_stats_##name); \
476 static inline int cfqg_stats_##name(struct cfqg_stats *stats) \
478 return (stats->flags & (1 << CFQG_stats_##name)) != 0; \
481 CFQG_FLAG_FNS(waiting)
482 CFQG_FLAG_FNS(idling)
486 /* This should be called with the queue_lock held. */
487 static void cfqg_stats_update_group_wait_time(struct cfqg_stats *stats)
489 unsigned long long now;
491 if (!cfqg_stats_waiting(stats))
495 if (time_after64(now, stats->start_group_wait_time))
496 blkg_stat_add(&stats->group_wait_time,
497 now - stats->start_group_wait_time);
498 cfqg_stats_clear_waiting(stats);
501 /* This should be called with the queue_lock held. */
502 static void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg,
503 struct cfq_group *curr_cfqg)
505 struct cfqg_stats *stats = &cfqg->stats;
507 if (cfqg_stats_waiting(stats))
509 if (cfqg == curr_cfqg)
511 stats->start_group_wait_time = sched_clock();
512 cfqg_stats_mark_waiting(stats);
515 /* This should be called with the queue_lock held. */
516 static void cfqg_stats_end_empty_time(struct cfqg_stats *stats)
518 unsigned long long now;
520 if (!cfqg_stats_empty(stats))
524 if (time_after64(now, stats->start_empty_time))
525 blkg_stat_add(&stats->empty_time,
526 now - stats->start_empty_time);
527 cfqg_stats_clear_empty(stats);
530 static void cfqg_stats_update_dequeue(struct cfq_group *cfqg)
532 blkg_stat_add(&cfqg->stats.dequeue, 1);
535 static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg)
537 struct cfqg_stats *stats = &cfqg->stats;
539 if (blkg_rwstat_total(&stats->queued))
543 * group is already marked empty. This can happen if cfqq got new
544 * request in parent group and moved to this group while being added
545 * to service tree. Just ignore the event and move on.
547 if (cfqg_stats_empty(stats))
550 stats->start_empty_time = sched_clock();
551 cfqg_stats_mark_empty(stats);
554 static void cfqg_stats_update_idle_time(struct cfq_group *cfqg)
556 struct cfqg_stats *stats = &cfqg->stats;
558 if (cfqg_stats_idling(stats)) {
559 unsigned long long now = sched_clock();
561 if (time_after64(now, stats->start_idle_time))
562 blkg_stat_add(&stats->idle_time,
563 now - stats->start_idle_time);
564 cfqg_stats_clear_idling(stats);
568 static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg)
570 struct cfqg_stats *stats = &cfqg->stats;
572 BUG_ON(cfqg_stats_idling(stats));
574 stats->start_idle_time = sched_clock();
575 cfqg_stats_mark_idling(stats);
578 static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg)
580 struct cfqg_stats *stats = &cfqg->stats;
582 blkg_stat_add(&stats->avg_queue_size_sum,
583 blkg_rwstat_total(&stats->queued));
584 blkg_stat_add(&stats->avg_queue_size_samples, 1);
585 cfqg_stats_update_group_wait_time(stats);
588 #else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
590 static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, struct cfq_group *curr_cfqg) { }
591 static inline void cfqg_stats_end_empty_time(struct cfqg_stats *stats) { }
592 static inline void cfqg_stats_update_dequeue(struct cfq_group *cfqg) { }
593 static inline void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) { }
594 static inline void cfqg_stats_update_idle_time(struct cfq_group *cfqg) { }
595 static inline void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) { }
596 static inline void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) { }
598 #endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
600 #ifdef CONFIG_CFQ_GROUP_IOSCHED
602 static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd)
604 return pd ? container_of(pd, struct cfq_group, pd) : NULL;
607 static struct cfq_group_data
608 *cpd_to_cfqgd(struct blkcg_policy_data *cpd)
610 return cpd ? container_of(cpd, struct cfq_group_data, cpd) : NULL;
613 static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg)
615 return pd_to_blkg(&cfqg->pd);
618 static struct blkcg_policy blkcg_policy_cfq;
620 static inline struct cfq_group *blkg_to_cfqg(struct blkcg_gq *blkg)
622 return pd_to_cfqg(blkg_to_pd(blkg, &blkcg_policy_cfq));
625 static struct cfq_group_data *blkcg_to_cfqgd(struct blkcg *blkcg)
627 return cpd_to_cfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_cfq));
630 static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg)
632 struct blkcg_gq *pblkg = cfqg_to_blkg(cfqg)->parent;
634 return pblkg ? blkg_to_cfqg(pblkg) : NULL;
637 static inline bool cfqg_is_descendant(struct cfq_group *cfqg,
638 struct cfq_group *ancestor)
640 return cgroup_is_descendant(cfqg_to_blkg(cfqg)->blkcg->css.cgroup,
641 cfqg_to_blkg(ancestor)->blkcg->css.cgroup);
644 static inline void cfqg_get(struct cfq_group *cfqg)
646 return blkg_get(cfqg_to_blkg(cfqg));
649 static inline void cfqg_put(struct cfq_group *cfqg)
651 return blkg_put(cfqg_to_blkg(cfqg));
654 #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \
657 blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \
658 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \
659 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
660 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
664 #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
667 blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \
668 blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \
671 static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
672 struct cfq_group *curr_cfqg,
675 blkg_rwstat_add(&cfqg->stats.queued, op, 1);
676 cfqg_stats_end_empty_time(&cfqg->stats);
677 cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg);
680 static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
681 uint64_t time, unsigned long unaccounted_time)
683 blkg_stat_add(&cfqg->stats.time, time);
684 #ifdef CONFIG_DEBUG_BLK_CGROUP
685 blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time);
689 static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg,
692 blkg_rwstat_add(&cfqg->stats.queued, op, -1);
695 static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
698 blkg_rwstat_add(&cfqg->stats.merged, op, 1);
701 static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
702 uint64_t start_time, uint64_t io_start_time,
705 struct cfqg_stats *stats = &cfqg->stats;
706 unsigned long long now = sched_clock();
708 if (time_after64(now, io_start_time))
709 blkg_rwstat_add(&stats->service_time, op, now - io_start_time);
710 if (time_after64(io_start_time, start_time))
711 blkg_rwstat_add(&stats->wait_time, op,
712 io_start_time - start_time);
716 static void cfqg_stats_reset(struct cfqg_stats *stats)
718 /* queued stats shouldn't be cleared */
719 blkg_rwstat_reset(&stats->merged);
720 blkg_rwstat_reset(&stats->service_time);
721 blkg_rwstat_reset(&stats->wait_time);
722 blkg_stat_reset(&stats->time);
723 #ifdef CONFIG_DEBUG_BLK_CGROUP
724 blkg_stat_reset(&stats->unaccounted_time);
725 blkg_stat_reset(&stats->avg_queue_size_sum);
726 blkg_stat_reset(&stats->avg_queue_size_samples);
727 blkg_stat_reset(&stats->dequeue);
728 blkg_stat_reset(&stats->group_wait_time);
729 blkg_stat_reset(&stats->idle_time);
730 blkg_stat_reset(&stats->empty_time);
735 static void cfqg_stats_add_aux(struct cfqg_stats *to, struct cfqg_stats *from)
737 /* queued stats shouldn't be cleared */
738 blkg_rwstat_add_aux(&to->merged, &from->merged);
739 blkg_rwstat_add_aux(&to->service_time, &from->service_time);
740 blkg_rwstat_add_aux(&to->wait_time, &from->wait_time);
741 blkg_stat_add_aux(&from->time, &from->time);
742 #ifdef CONFIG_DEBUG_BLK_CGROUP
743 blkg_stat_add_aux(&to->unaccounted_time, &from->unaccounted_time);
744 blkg_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
745 blkg_stat_add_aux(&to->avg_queue_size_samples, &from->avg_queue_size_samples);
746 blkg_stat_add_aux(&to->dequeue, &from->dequeue);
747 blkg_stat_add_aux(&to->group_wait_time, &from->group_wait_time);
748 blkg_stat_add_aux(&to->idle_time, &from->idle_time);
749 blkg_stat_add_aux(&to->empty_time, &from->empty_time);
754 * Transfer @cfqg's stats to its parent's aux counts so that the ancestors'
755 * recursive stats can still account for the amount used by this cfqg after
758 static void cfqg_stats_xfer_dead(struct cfq_group *cfqg)
760 struct cfq_group *parent = cfqg_parent(cfqg);
762 lockdep_assert_held(cfqg_to_blkg(cfqg)->q->queue_lock);
764 if (unlikely(!parent))
767 cfqg_stats_add_aux(&parent->stats, &cfqg->stats);
768 cfqg_stats_reset(&cfqg->stats);
771 #else /* CONFIG_CFQ_GROUP_IOSCHED */
773 static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) { return NULL; }
774 static inline bool cfqg_is_descendant(struct cfq_group *cfqg,
775 struct cfq_group *ancestor)
779 static inline void cfqg_get(struct cfq_group *cfqg) { }
780 static inline void cfqg_put(struct cfq_group *cfqg) { }
782 #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
783 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \
784 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
785 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
787 #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
789 static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
790 struct cfq_group *curr_cfqg, unsigned int op) { }
791 static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
792 uint64_t time, unsigned long unaccounted_time) { }
793 static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg,
795 static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
797 static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
798 uint64_t start_time, uint64_t io_start_time,
801 #endif /* CONFIG_CFQ_GROUP_IOSCHED */
803 #define cfq_log(cfqd, fmt, args...) \
804 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
806 /* Traverses through cfq group service trees */
807 #define for_each_cfqg_st(cfqg, i, j, st) \
808 for (i = 0; i <= IDLE_WORKLOAD; i++) \
809 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
810 : &cfqg->service_tree_idle; \
811 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
812 (i == IDLE_WORKLOAD && j == 0); \
813 j++, st = i < IDLE_WORKLOAD ? \
814 &cfqg->service_trees[i][j]: NULL) \
816 static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
817 struct cfq_ttime *ttime, bool group_idle)
820 if (!sample_valid(ttime->ttime_samples))
823 slice = cfqd->cfq_group_idle;
825 slice = cfqd->cfq_slice_idle;
826 return ttime->ttime_mean > slice;
829 static inline bool iops_mode(struct cfq_data *cfqd)
832 * If we are not idling on queues and it is a NCQ drive, parallel
833 * execution of requests is on and measuring time is not possible
834 * in most of the cases until and unless we drive shallower queue
835 * depths and that becomes a performance bottleneck. In such cases
836 * switch to start providing fairness in terms of number of IOs.
838 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
844 static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq)
846 if (cfq_class_idle(cfqq))
847 return IDLE_WORKLOAD;
848 if (cfq_class_rt(cfqq))
854 static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
856 if (!cfq_cfqq_sync(cfqq))
857 return ASYNC_WORKLOAD;
858 if (!cfq_cfqq_idle_window(cfqq))
859 return SYNC_NOIDLE_WORKLOAD;
860 return SYNC_WORKLOAD;
863 static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class,
864 struct cfq_data *cfqd,
865 struct cfq_group *cfqg)
867 if (wl_class == IDLE_WORKLOAD)
868 return cfqg->service_tree_idle.count;
870 return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count +
871 cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count +
872 cfqg->service_trees[wl_class][SYNC_WORKLOAD].count;
875 static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
876 struct cfq_group *cfqg)
878 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count +
879 cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
882 static void cfq_dispatch_insert(struct request_queue *, struct request *);
883 static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
884 struct cfq_io_cq *cic, struct bio *bio);
886 static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
888 /* cic->icq is the first member, %NULL will convert to %NULL */
889 return container_of(icq, struct cfq_io_cq, icq);
892 static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
893 struct io_context *ioc)
896 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
900 static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
902 return cic->cfqq[is_sync];
905 static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
908 cic->cfqq[is_sync] = cfqq;
911 static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
913 return cic->icq.q->elevator->elevator_data;
917 * scheduler run of queue, if there are requests pending and no one in the
918 * driver that will restart queueing
920 static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
922 if (cfqd->busy_queues) {
923 cfq_log(cfqd, "schedule dispatch");
924 kblockd_schedule_work(&cfqd->unplug_work);
929 * Scale schedule slice based on io priority. Use the sync time slice only
930 * if a queue is marked sync and has sync io queued. A sync queue with async
931 * io only, should not get full sync slice length.
933 static inline u64 cfq_prio_slice(struct cfq_data *cfqd, bool sync,
936 u64 base_slice = cfqd->cfq_slice[sync];
937 u64 slice = div_u64(base_slice, CFQ_SLICE_SCALE);
939 WARN_ON(prio >= IOPRIO_BE_NR);
941 return base_slice + (slice * (4 - prio));
945 cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
947 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
951 * cfqg_scale_charge - scale disk time charge according to cfqg weight
952 * @charge: disk time being charged
953 * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT
955 * Scale @charge according to @vfraction, which is in range (0, 1]. The
956 * scaling is inversely proportional.
958 * scaled = charge / vfraction
960 * The result is also in fixed point w/ CFQ_SERVICE_SHIFT.
962 static inline u64 cfqg_scale_charge(u64 charge,
963 unsigned int vfraction)
965 u64 c = charge << CFQ_SERVICE_SHIFT; /* make it fixed point */
967 /* charge / vfraction */
968 c <<= CFQ_SERVICE_SHIFT;
969 return div_u64(c, vfraction);
972 static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
974 s64 delta = (s64)(vdisktime - min_vdisktime);
976 min_vdisktime = vdisktime;
978 return min_vdisktime;
981 static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
983 s64 delta = (s64)(vdisktime - min_vdisktime);
985 min_vdisktime = vdisktime;
987 return min_vdisktime;
990 static void update_min_vdisktime(struct cfq_rb_root *st)
992 struct cfq_group *cfqg;
995 cfqg = rb_entry_cfqg(st->left);
996 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
1002 * get averaged number of queues of RT/BE priority.
1003 * average is updated, with a formula that gives more weight to higher numbers,
1004 * to quickly follows sudden increases and decrease slowly
1007 static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
1008 struct cfq_group *cfqg, bool rt)
1010 unsigned min_q, max_q;
1011 unsigned mult = cfq_hist_divisor - 1;
1012 unsigned round = cfq_hist_divisor / 2;
1013 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
1015 min_q = min(cfqg->busy_queues_avg[rt], busy);
1016 max_q = max(cfqg->busy_queues_avg[rt], busy);
1017 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
1019 return cfqg->busy_queues_avg[rt];
1023 cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
1025 return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT;
1029 cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1031 u64 slice = cfq_prio_to_slice(cfqd, cfqq);
1032 if (cfqd->cfq_latency) {
1034 * interested queues (we consider only the ones with the same
1035 * priority class in the cfq group)
1037 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
1038 cfq_class_rt(cfqq));
1039 u64 sync_slice = cfqd->cfq_slice[1];
1040 u64 expect_latency = sync_slice * iq;
1041 u64 group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
1043 if (expect_latency > group_slice) {
1044 u64 base_low_slice = 2 * cfqd->cfq_slice_idle;
1047 /* scale low_slice according to IO priority
1048 * and sync vs async */
1049 low_slice = div64_u64(base_low_slice*slice, sync_slice);
1050 low_slice = min(slice, low_slice);
1051 /* the adapted slice value is scaled to fit all iqs
1052 * into the target latency */
1053 slice = div64_u64(slice*group_slice, expect_latency);
1054 slice = max(slice, low_slice);
1061 cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1063 u64 slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
1064 u64 now = ktime_get_ns();
1066 cfqq->slice_start = now;
1067 cfqq->slice_end = now + slice;
1068 cfqq->allocated_slice = slice;
1069 cfq_log_cfqq(cfqd, cfqq, "set_slice=%llu", cfqq->slice_end - now);
1073 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
1074 * isn't valid until the first request from the dispatch is activated
1075 * and the slice time set.
1077 static inline bool cfq_slice_used(struct cfq_queue *cfqq)
1079 if (cfq_cfqq_slice_new(cfqq))
1081 if (ktime_get_ns() < cfqq->slice_end)
1088 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
1089 * We choose the request that is closest to the head right now. Distance
1090 * behind the head is penalized and only allowed to a certain extent.
1092 static struct request *
1093 cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
1095 sector_t s1, s2, d1 = 0, d2 = 0;
1096 unsigned long back_max;
1097 #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
1098 #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
1099 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
1101 if (rq1 == NULL || rq1 == rq2)
1106 if (rq_is_sync(rq1) != rq_is_sync(rq2))
1107 return rq_is_sync(rq1) ? rq1 : rq2;
1109 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
1110 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
1112 s1 = blk_rq_pos(rq1);
1113 s2 = blk_rq_pos(rq2);
1116 * by definition, 1KiB is 2 sectors
1118 back_max = cfqd->cfq_back_max * 2;
1121 * Strict one way elevator _except_ in the case where we allow
1122 * short backward seeks which are biased as twice the cost of a
1123 * similar forward seek.
1127 else if (s1 + back_max >= last)
1128 d1 = (last - s1) * cfqd->cfq_back_penalty;
1130 wrap |= CFQ_RQ1_WRAP;
1134 else if (s2 + back_max >= last)
1135 d2 = (last - s2) * cfqd->cfq_back_penalty;
1137 wrap |= CFQ_RQ2_WRAP;
1139 /* Found required data */
1142 * By doing switch() on the bit mask "wrap" we avoid having to
1143 * check two variables for all permutations: --> faster!
1146 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
1162 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
1165 * Since both rqs are wrapped,
1166 * start with the one that's further behind head
1167 * (--> only *one* back seek required),
1168 * since back seek takes more time than forward.
1178 * The below is leftmost cache rbtree addon
1180 static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
1182 /* Service tree is empty */
1187 root->left = rb_first(&root->rb);
1190 return rb_entry(root->left, struct cfq_queue, rb_node);
1195 static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
1198 root->left = rb_first(&root->rb);
1201 return rb_entry_cfqg(root->left);
1206 static void rb_erase_init(struct rb_node *n, struct rb_root *root)
1212 static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
1214 if (root->left == n)
1216 rb_erase_init(n, &root->rb);
1221 * would be nice to take fifo expire time into account as well
1223 static struct request *
1224 cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1225 struct request *last)
1227 struct rb_node *rbnext = rb_next(&last->rb_node);
1228 struct rb_node *rbprev = rb_prev(&last->rb_node);
1229 struct request *next = NULL, *prev = NULL;
1231 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
1234 prev = rb_entry_rq(rbprev);
1237 next = rb_entry_rq(rbnext);
1239 rbnext = rb_first(&cfqq->sort_list);
1240 if (rbnext && rbnext != &last->rb_node)
1241 next = rb_entry_rq(rbnext);
1244 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
1247 static u64 cfq_slice_offset(struct cfq_data *cfqd,
1248 struct cfq_queue *cfqq)
1251 * just an approximation, should be ok.
1253 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
1254 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
1258 cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
1260 return cfqg->vdisktime - st->min_vdisktime;
1264 __cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1266 struct rb_node **node = &st->rb.rb_node;
1267 struct rb_node *parent = NULL;
1268 struct cfq_group *__cfqg;
1269 s64 key = cfqg_key(st, cfqg);
1272 while (*node != NULL) {
1274 __cfqg = rb_entry_cfqg(parent);
1276 if (key < cfqg_key(st, __cfqg))
1277 node = &parent->rb_left;
1279 node = &parent->rb_right;
1285 st->left = &cfqg->rb_node;
1287 rb_link_node(&cfqg->rb_node, parent, node);
1288 rb_insert_color(&cfqg->rb_node, &st->rb);
1292 * This has to be called only on activation of cfqg
1295 cfq_update_group_weight(struct cfq_group *cfqg)
1297 if (cfqg->new_weight) {
1298 cfqg->weight = cfqg->new_weight;
1299 cfqg->new_weight = 0;
1304 cfq_update_group_leaf_weight(struct cfq_group *cfqg)
1306 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1308 if (cfqg->new_leaf_weight) {
1309 cfqg->leaf_weight = cfqg->new_leaf_weight;
1310 cfqg->new_leaf_weight = 0;
1315 cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1317 unsigned int vfr = 1 << CFQ_SERVICE_SHIFT; /* start with 1 */
1318 struct cfq_group *pos = cfqg;
1319 struct cfq_group *parent;
1322 /* add to the service tree */
1323 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1326 * Update leaf_weight. We cannot update weight at this point
1327 * because cfqg might already have been activated and is
1328 * contributing its current weight to the parent's child_weight.
1330 cfq_update_group_leaf_weight(cfqg);
1331 __cfq_group_service_tree_add(st, cfqg);
1334 * Activate @cfqg and calculate the portion of vfraction @cfqg is
1335 * entitled to. vfraction is calculated by walking the tree
1336 * towards the root calculating the fraction it has at each level.
1337 * The compounded ratio is how much vfraction @cfqg owns.
1339 * Start with the proportion tasks in this cfqg has against active
1340 * children cfqgs - its leaf_weight against children_weight.
1342 propagate = !pos->nr_active++;
1343 pos->children_weight += pos->leaf_weight;
1344 vfr = vfr * pos->leaf_weight / pos->children_weight;
1347 * Compound ->weight walking up the tree. Both activation and
1348 * vfraction calculation are done in the same loop. Propagation
1349 * stops once an already activated node is met. vfraction
1350 * calculation should always continue to the root.
1352 while ((parent = cfqg_parent(pos))) {
1354 cfq_update_group_weight(pos);
1355 propagate = !parent->nr_active++;
1356 parent->children_weight += pos->weight;
1358 vfr = vfr * pos->weight / parent->children_weight;
1362 cfqg->vfraction = max_t(unsigned, vfr, 1);
1366 cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
1368 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1369 struct cfq_group *__cfqg;
1373 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1377 * Currently put the group at the end. Later implement something
1378 * so that groups get lesser vtime based on their weights, so that
1379 * if group does not loose all if it was not continuously backlogged.
1381 n = rb_last(&st->rb);
1383 __cfqg = rb_entry_cfqg(n);
1384 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
1386 cfqg->vdisktime = st->min_vdisktime;
1387 cfq_group_service_tree_add(st, cfqg);
1391 cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
1393 struct cfq_group *pos = cfqg;
1397 * Undo activation from cfq_group_service_tree_add(). Deactivate
1398 * @cfqg and propagate deactivation upwards.
1400 propagate = !--pos->nr_active;
1401 pos->children_weight -= pos->leaf_weight;
1404 struct cfq_group *parent = cfqg_parent(pos);
1406 /* @pos has 0 nr_active at this point */
1407 WARN_ON_ONCE(pos->children_weight);
1413 propagate = !--parent->nr_active;
1414 parent->children_weight -= pos->weight;
1418 /* remove from the service tree */
1419 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1420 cfq_rb_erase(&cfqg->rb_node, st);
1424 cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
1426 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1428 BUG_ON(cfqg->nr_cfqq < 1);
1431 /* If there are other cfq queues under this group, don't delete it */
1435 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
1436 cfq_group_service_tree_del(st, cfqg);
1437 cfqg->saved_wl_slice = 0;
1438 cfqg_stats_update_dequeue(cfqg);
1441 static inline u64 cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
1442 u64 *unaccounted_time)
1445 u64 now = ktime_get_ns();
1448 * Queue got expired before even a single request completed or
1449 * got expired immediately after first request completion.
1451 if (!cfqq->slice_start || cfqq->slice_start == now) {
1453 * Also charge the seek time incurred to the group, otherwise
1454 * if there are mutiple queues in the group, each can dispatch
1455 * a single request on seeky media and cause lots of seek time
1456 * and group will never know it.
1458 slice_used = max_t(u64, (now - cfqq->dispatch_start),
1459 jiffies_to_nsecs(1));
1461 slice_used = now - cfqq->slice_start;
1462 if (slice_used > cfqq->allocated_slice) {
1463 *unaccounted_time = slice_used - cfqq->allocated_slice;
1464 slice_used = cfqq->allocated_slice;
1466 if (cfqq->slice_start > cfqq->dispatch_start)
1467 *unaccounted_time += cfqq->slice_start -
1468 cfqq->dispatch_start;
1474 static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
1475 struct cfq_queue *cfqq)
1477 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1478 u64 used_sl, charge, unaccounted_sl = 0;
1479 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
1480 - cfqg->service_tree_idle.count;
1482 u64 now = ktime_get_ns();
1484 BUG_ON(nr_sync < 0);
1485 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
1487 if (iops_mode(cfqd))
1488 charge = cfqq->slice_dispatch;
1489 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
1490 charge = cfqq->allocated_slice;
1493 * Can't update vdisktime while on service tree and cfqg->vfraction
1494 * is valid only while on it. Cache vfr, leave the service tree,
1495 * update vdisktime and go back on. The re-addition to the tree
1496 * will also update the weights as necessary.
1498 vfr = cfqg->vfraction;
1499 cfq_group_service_tree_del(st, cfqg);
1500 cfqg->vdisktime += cfqg_scale_charge(charge, vfr);
1501 cfq_group_service_tree_add(st, cfqg);
1503 /* This group is being expired. Save the context */
1504 if (cfqd->workload_expires > now) {
1505 cfqg->saved_wl_slice = cfqd->workload_expires - now;
1506 cfqg->saved_wl_type = cfqd->serving_wl_type;
1507 cfqg->saved_wl_class = cfqd->serving_wl_class;
1509 cfqg->saved_wl_slice = 0;
1511 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1513 cfq_log_cfqq(cfqq->cfqd, cfqq,
1514 "sl_used=%llu disp=%llu charge=%llu iops=%u sect=%lu",
1515 used_sl, cfqq->slice_dispatch, charge,
1516 iops_mode(cfqd), cfqq->nr_sectors);
1517 cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
1518 cfqg_stats_set_start_empty_time(cfqg);
1522 * cfq_init_cfqg_base - initialize base part of a cfq_group
1523 * @cfqg: cfq_group to initialize
1525 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1526 * is enabled or not.
1528 static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1530 struct cfq_rb_root *st;
1533 for_each_cfqg_st(cfqg, i, j, st)
1535 RB_CLEAR_NODE(&cfqg->rb_node);
1537 cfqg->ttime.last_end_request = ktime_get_ns();
1540 #ifdef CONFIG_CFQ_GROUP_IOSCHED
1541 static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
1542 bool on_dfl, bool reset_dev, bool is_leaf_weight);
1544 static void cfqg_stats_exit(struct cfqg_stats *stats)
1546 blkg_rwstat_exit(&stats->merged);
1547 blkg_rwstat_exit(&stats->service_time);
1548 blkg_rwstat_exit(&stats->wait_time);
1549 blkg_rwstat_exit(&stats->queued);
1550 blkg_stat_exit(&stats->time);
1551 #ifdef CONFIG_DEBUG_BLK_CGROUP
1552 blkg_stat_exit(&stats->unaccounted_time);
1553 blkg_stat_exit(&stats->avg_queue_size_sum);
1554 blkg_stat_exit(&stats->avg_queue_size_samples);
1555 blkg_stat_exit(&stats->dequeue);
1556 blkg_stat_exit(&stats->group_wait_time);
1557 blkg_stat_exit(&stats->idle_time);
1558 blkg_stat_exit(&stats->empty_time);
1562 static int cfqg_stats_init(struct cfqg_stats *stats, gfp_t gfp)
1564 if (blkg_rwstat_init(&stats->merged, gfp) ||
1565 blkg_rwstat_init(&stats->service_time, gfp) ||
1566 blkg_rwstat_init(&stats->wait_time, gfp) ||
1567 blkg_rwstat_init(&stats->queued, gfp) ||
1568 blkg_stat_init(&stats->time, gfp))
1571 #ifdef CONFIG_DEBUG_BLK_CGROUP
1572 if (blkg_stat_init(&stats->unaccounted_time, gfp) ||
1573 blkg_stat_init(&stats->avg_queue_size_sum, gfp) ||
1574 blkg_stat_init(&stats->avg_queue_size_samples, gfp) ||
1575 blkg_stat_init(&stats->dequeue, gfp) ||
1576 blkg_stat_init(&stats->group_wait_time, gfp) ||
1577 blkg_stat_init(&stats->idle_time, gfp) ||
1578 blkg_stat_init(&stats->empty_time, gfp))
1583 cfqg_stats_exit(stats);
1587 static struct blkcg_policy_data *cfq_cpd_alloc(gfp_t gfp)
1589 struct cfq_group_data *cgd;
1591 cgd = kzalloc(sizeof(*cgd), gfp);
1597 static void cfq_cpd_init(struct blkcg_policy_data *cpd)
1599 struct cfq_group_data *cgd = cpd_to_cfqgd(cpd);
1600 unsigned int weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ?
1601 CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
1603 if (cpd_to_blkcg(cpd) == &blkcg_root)
1606 cgd->weight = weight;
1607 cgd->leaf_weight = weight;
1610 static void cfq_cpd_free(struct blkcg_policy_data *cpd)
1612 kfree(cpd_to_cfqgd(cpd));
1615 static void cfq_cpd_bind(struct blkcg_policy_data *cpd)
1617 struct blkcg *blkcg = cpd_to_blkcg(cpd);
1618 bool on_dfl = cgroup_subsys_on_dfl(io_cgrp_subsys);
1619 unsigned int weight = on_dfl ? CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
1621 if (blkcg == &blkcg_root)
1624 WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, false));
1625 WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, true));
1628 static struct blkg_policy_data *cfq_pd_alloc(gfp_t gfp, int node)
1630 struct cfq_group *cfqg;
1632 cfqg = kzalloc_node(sizeof(*cfqg), gfp, node);
1636 cfq_init_cfqg_base(cfqg);
1637 if (cfqg_stats_init(&cfqg->stats, gfp)) {
1645 static void cfq_pd_init(struct blkg_policy_data *pd)
1647 struct cfq_group *cfqg = pd_to_cfqg(pd);
1648 struct cfq_group_data *cgd = blkcg_to_cfqgd(pd->blkg->blkcg);
1650 cfqg->weight = cgd->weight;
1651 cfqg->leaf_weight = cgd->leaf_weight;
1654 static void cfq_pd_offline(struct blkg_policy_data *pd)
1656 struct cfq_group *cfqg = pd_to_cfqg(pd);
1659 for (i = 0; i < IOPRIO_BE_NR; i++) {
1660 if (cfqg->async_cfqq[0][i])
1661 cfq_put_queue(cfqg->async_cfqq[0][i]);
1662 if (cfqg->async_cfqq[1][i])
1663 cfq_put_queue(cfqg->async_cfqq[1][i]);
1666 if (cfqg->async_idle_cfqq)
1667 cfq_put_queue(cfqg->async_idle_cfqq);
1670 * @blkg is going offline and will be ignored by
1671 * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so
1672 * that they don't get lost. If IOs complete after this point, the
1673 * stats for them will be lost. Oh well...
1675 cfqg_stats_xfer_dead(cfqg);
1678 static void cfq_pd_free(struct blkg_policy_data *pd)
1680 struct cfq_group *cfqg = pd_to_cfqg(pd);
1682 cfqg_stats_exit(&cfqg->stats);
1686 static void cfq_pd_reset_stats(struct blkg_policy_data *pd)
1688 struct cfq_group *cfqg = pd_to_cfqg(pd);
1690 cfqg_stats_reset(&cfqg->stats);
1693 static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
1694 struct blkcg *blkcg)
1696 struct blkcg_gq *blkg;
1698 blkg = blkg_lookup(blkcg, cfqd->queue);
1700 return blkg_to_cfqg(blkg);
1704 static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1707 /* cfqq reference on cfqg */
1711 static u64 cfqg_prfill_weight_device(struct seq_file *sf,
1712 struct blkg_policy_data *pd, int off)
1714 struct cfq_group *cfqg = pd_to_cfqg(pd);
1716 if (!cfqg->dev_weight)
1718 return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
1721 static int cfqg_print_weight_device(struct seq_file *sf, void *v)
1723 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1724 cfqg_prfill_weight_device, &blkcg_policy_cfq,
1729 static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf,
1730 struct blkg_policy_data *pd, int off)
1732 struct cfq_group *cfqg = pd_to_cfqg(pd);
1734 if (!cfqg->dev_leaf_weight)
1736 return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight);
1739 static int cfqg_print_leaf_weight_device(struct seq_file *sf, void *v)
1741 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1742 cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq,
1747 static int cfq_print_weight(struct seq_file *sf, void *v)
1749 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
1750 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
1751 unsigned int val = 0;
1756 seq_printf(sf, "%u\n", val);
1760 static int cfq_print_leaf_weight(struct seq_file *sf, void *v)
1762 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
1763 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
1764 unsigned int val = 0;
1767 val = cgd->leaf_weight;
1769 seq_printf(sf, "%u\n", val);
1773 static ssize_t __cfqg_set_weight_device(struct kernfs_open_file *of,
1774 char *buf, size_t nbytes, loff_t off,
1775 bool on_dfl, bool is_leaf_weight)
1777 unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
1778 unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
1779 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1780 struct blkg_conf_ctx ctx;
1781 struct cfq_group *cfqg;
1782 struct cfq_group_data *cfqgd;
1786 ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
1790 if (sscanf(ctx.body, "%llu", &v) == 1) {
1791 /* require "default" on dfl */
1795 } else if (!strcmp(strim(ctx.body), "default")) {
1802 cfqg = blkg_to_cfqg(ctx.blkg);
1803 cfqgd = blkcg_to_cfqgd(blkcg);
1806 if (!v || (v >= min && v <= max)) {
1807 if (!is_leaf_weight) {
1808 cfqg->dev_weight = v;
1809 cfqg->new_weight = v ?: cfqgd->weight;
1811 cfqg->dev_leaf_weight = v;
1812 cfqg->new_leaf_weight = v ?: cfqgd->leaf_weight;
1817 blkg_conf_finish(&ctx);
1818 return ret ?: nbytes;
1821 static ssize_t cfqg_set_weight_device(struct kernfs_open_file *of,
1822 char *buf, size_t nbytes, loff_t off)
1824 return __cfqg_set_weight_device(of, buf, nbytes, off, false, false);
1827 static ssize_t cfqg_set_leaf_weight_device(struct kernfs_open_file *of,
1828 char *buf, size_t nbytes, loff_t off)
1830 return __cfqg_set_weight_device(of, buf, nbytes, off, false, true);
1833 static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
1834 bool on_dfl, bool reset_dev, bool is_leaf_weight)
1836 unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
1837 unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
1838 struct blkcg *blkcg = css_to_blkcg(css);
1839 struct blkcg_gq *blkg;
1840 struct cfq_group_data *cfqgd;
1843 if (val < min || val > max)
1846 spin_lock_irq(&blkcg->lock);
1847 cfqgd = blkcg_to_cfqgd(blkcg);
1853 if (!is_leaf_weight)
1854 cfqgd->weight = val;
1856 cfqgd->leaf_weight = val;
1858 hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
1859 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
1864 if (!is_leaf_weight) {
1866 cfqg->dev_weight = 0;
1867 if (!cfqg->dev_weight)
1868 cfqg->new_weight = cfqgd->weight;
1871 cfqg->dev_leaf_weight = 0;
1872 if (!cfqg->dev_leaf_weight)
1873 cfqg->new_leaf_weight = cfqgd->leaf_weight;
1878 spin_unlock_irq(&blkcg->lock);
1882 static int cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft,
1885 return __cfq_set_weight(css, val, false, false, false);
1888 static int cfq_set_leaf_weight(struct cgroup_subsys_state *css,
1889 struct cftype *cft, u64 val)
1891 return __cfq_set_weight(css, val, false, false, true);
1894 static int cfqg_print_stat(struct seq_file *sf, void *v)
1896 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat,
1897 &blkcg_policy_cfq, seq_cft(sf)->private, false);
1901 static int cfqg_print_rwstat(struct seq_file *sf, void *v)
1903 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat,
1904 &blkcg_policy_cfq, seq_cft(sf)->private, true);
1908 static u64 cfqg_prfill_stat_recursive(struct seq_file *sf,
1909 struct blkg_policy_data *pd, int off)
1911 u64 sum = blkg_stat_recursive_sum(pd_to_blkg(pd),
1912 &blkcg_policy_cfq, off);
1913 return __blkg_prfill_u64(sf, pd, sum);
1916 static u64 cfqg_prfill_rwstat_recursive(struct seq_file *sf,
1917 struct blkg_policy_data *pd, int off)
1919 struct blkg_rwstat sum = blkg_rwstat_recursive_sum(pd_to_blkg(pd),
1920 &blkcg_policy_cfq, off);
1921 return __blkg_prfill_rwstat(sf, pd, &sum);
1924 static int cfqg_print_stat_recursive(struct seq_file *sf, void *v)
1926 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1927 cfqg_prfill_stat_recursive, &blkcg_policy_cfq,
1928 seq_cft(sf)->private, false);
1932 static int cfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
1934 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1935 cfqg_prfill_rwstat_recursive, &blkcg_policy_cfq,
1936 seq_cft(sf)->private, true);
1940 static u64 cfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd,
1943 u64 sum = blkg_rwstat_total(&pd->blkg->stat_bytes);
1945 return __blkg_prfill_u64(sf, pd, sum >> 9);
1948 static int cfqg_print_stat_sectors(struct seq_file *sf, void *v)
1950 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1951 cfqg_prfill_sectors, &blkcg_policy_cfq, 0, false);
1955 static u64 cfqg_prfill_sectors_recursive(struct seq_file *sf,
1956 struct blkg_policy_data *pd, int off)
1958 struct blkg_rwstat tmp = blkg_rwstat_recursive_sum(pd->blkg, NULL,
1959 offsetof(struct blkcg_gq, stat_bytes));
1960 u64 sum = atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) +
1961 atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]);
1963 return __blkg_prfill_u64(sf, pd, sum >> 9);
1966 static int cfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v)
1968 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1969 cfqg_prfill_sectors_recursive, &blkcg_policy_cfq, 0,
1974 #ifdef CONFIG_DEBUG_BLK_CGROUP
1975 static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
1976 struct blkg_policy_data *pd, int off)
1978 struct cfq_group *cfqg = pd_to_cfqg(pd);
1979 u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
1983 v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
1984 v = div64_u64(v, samples);
1986 __blkg_prfill_u64(sf, pd, v);
1990 /* print avg_queue_size */
1991 static int cfqg_print_avg_queue_size(struct seq_file *sf, void *v)
1993 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1994 cfqg_prfill_avg_queue_size, &blkcg_policy_cfq,
1998 #endif /* CONFIG_DEBUG_BLK_CGROUP */
2000 static struct cftype cfq_blkcg_legacy_files[] = {
2001 /* on root, weight is mapped to leaf_weight */
2003 .name = "weight_device",
2004 .flags = CFTYPE_ONLY_ON_ROOT,
2005 .seq_show = cfqg_print_leaf_weight_device,
2006 .write = cfqg_set_leaf_weight_device,
2010 .flags = CFTYPE_ONLY_ON_ROOT,
2011 .seq_show = cfq_print_leaf_weight,
2012 .write_u64 = cfq_set_leaf_weight,
2015 /* no such mapping necessary for !roots */
2017 .name = "weight_device",
2018 .flags = CFTYPE_NOT_ON_ROOT,
2019 .seq_show = cfqg_print_weight_device,
2020 .write = cfqg_set_weight_device,
2024 .flags = CFTYPE_NOT_ON_ROOT,
2025 .seq_show = cfq_print_weight,
2026 .write_u64 = cfq_set_weight,
2030 .name = "leaf_weight_device",
2031 .seq_show = cfqg_print_leaf_weight_device,
2032 .write = cfqg_set_leaf_weight_device,
2035 .name = "leaf_weight",
2036 .seq_show = cfq_print_leaf_weight,
2037 .write_u64 = cfq_set_leaf_weight,
2040 /* statistics, covers only the tasks in the cfqg */
2043 .private = offsetof(struct cfq_group, stats.time),
2044 .seq_show = cfqg_print_stat,
2048 .seq_show = cfqg_print_stat_sectors,
2051 .name = "io_service_bytes",
2052 .private = (unsigned long)&blkcg_policy_cfq,
2053 .seq_show = blkg_print_stat_bytes,
2056 .name = "io_serviced",
2057 .private = (unsigned long)&blkcg_policy_cfq,
2058 .seq_show = blkg_print_stat_ios,
2061 .name = "io_service_time",
2062 .private = offsetof(struct cfq_group, stats.service_time),
2063 .seq_show = cfqg_print_rwstat,
2066 .name = "io_wait_time",
2067 .private = offsetof(struct cfq_group, stats.wait_time),
2068 .seq_show = cfqg_print_rwstat,
2071 .name = "io_merged",
2072 .private = offsetof(struct cfq_group, stats.merged),
2073 .seq_show = cfqg_print_rwstat,
2076 .name = "io_queued",
2077 .private = offsetof(struct cfq_group, stats.queued),
2078 .seq_show = cfqg_print_rwstat,
2081 /* the same statictics which cover the cfqg and its descendants */
2083 .name = "time_recursive",
2084 .private = offsetof(struct cfq_group, stats.time),
2085 .seq_show = cfqg_print_stat_recursive,
2088 .name = "sectors_recursive",
2089 .seq_show = cfqg_print_stat_sectors_recursive,
2092 .name = "io_service_bytes_recursive",
2093 .private = (unsigned long)&blkcg_policy_cfq,
2094 .seq_show = blkg_print_stat_bytes_recursive,
2097 .name = "io_serviced_recursive",
2098 .private = (unsigned long)&blkcg_policy_cfq,
2099 .seq_show = blkg_print_stat_ios_recursive,
2102 .name = "io_service_time_recursive",
2103 .private = offsetof(struct cfq_group, stats.service_time),
2104 .seq_show = cfqg_print_rwstat_recursive,
2107 .name = "io_wait_time_recursive",
2108 .private = offsetof(struct cfq_group, stats.wait_time),
2109 .seq_show = cfqg_print_rwstat_recursive,
2112 .name = "io_merged_recursive",
2113 .private = offsetof(struct cfq_group, stats.merged),
2114 .seq_show = cfqg_print_rwstat_recursive,
2117 .name = "io_queued_recursive",
2118 .private = offsetof(struct cfq_group, stats.queued),
2119 .seq_show = cfqg_print_rwstat_recursive,
2121 #ifdef CONFIG_DEBUG_BLK_CGROUP
2123 .name = "avg_queue_size",
2124 .seq_show = cfqg_print_avg_queue_size,
2127 .name = "group_wait_time",
2128 .private = offsetof(struct cfq_group, stats.group_wait_time),
2129 .seq_show = cfqg_print_stat,
2132 .name = "idle_time",
2133 .private = offsetof(struct cfq_group, stats.idle_time),
2134 .seq_show = cfqg_print_stat,
2137 .name = "empty_time",
2138 .private = offsetof(struct cfq_group, stats.empty_time),
2139 .seq_show = cfqg_print_stat,
2143 .private = offsetof(struct cfq_group, stats.dequeue),
2144 .seq_show = cfqg_print_stat,
2147 .name = "unaccounted_time",
2148 .private = offsetof(struct cfq_group, stats.unaccounted_time),
2149 .seq_show = cfqg_print_stat,
2151 #endif /* CONFIG_DEBUG_BLK_CGROUP */
2155 static int cfq_print_weight_on_dfl(struct seq_file *sf, void *v)
2157 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
2158 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
2160 seq_printf(sf, "default %u\n", cgd->weight);
2161 blkcg_print_blkgs(sf, blkcg, cfqg_prfill_weight_device,
2162 &blkcg_policy_cfq, 0, false);
2166 static ssize_t cfq_set_weight_on_dfl(struct kernfs_open_file *of,
2167 char *buf, size_t nbytes, loff_t off)
2175 /* "WEIGHT" or "default WEIGHT" sets the default weight */
2176 v = simple_strtoull(buf, &endp, 0);
2177 if (*endp == '\0' || sscanf(buf, "default %llu", &v) == 1) {
2178 ret = __cfq_set_weight(of_css(of), v, true, false, false);
2179 return ret ?: nbytes;
2182 /* "MAJ:MIN WEIGHT" */
2183 return __cfqg_set_weight_device(of, buf, nbytes, off, true, false);
2186 static struct cftype cfq_blkcg_files[] = {
2189 .flags = CFTYPE_NOT_ON_ROOT,
2190 .seq_show = cfq_print_weight_on_dfl,
2191 .write = cfq_set_weight_on_dfl,
2196 #else /* GROUP_IOSCHED */
2197 static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
2198 struct blkcg *blkcg)
2200 return cfqd->root_group;
2204 cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
2208 #endif /* GROUP_IOSCHED */
2211 * The cfqd->service_trees holds all pending cfq_queue's that have
2212 * requests waiting to be processed. It is sorted in the order that
2213 * we will service the queues.
2215 static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2218 struct rb_node **p, *parent;
2219 struct cfq_queue *__cfqq;
2221 struct cfq_rb_root *st;
2224 u64 now = ktime_get_ns();
2226 st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
2227 if (cfq_class_idle(cfqq)) {
2228 rb_key = CFQ_IDLE_DELAY;
2229 parent = rb_last(&st->rb);
2230 if (parent && parent != &cfqq->rb_node) {
2231 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
2232 rb_key += __cfqq->rb_key;
2235 } else if (!add_front) {
2237 * Get our rb key offset. Subtract any residual slice
2238 * value carried from last service. A negative resid
2239 * count indicates slice overrun, and this should position
2240 * the next service time further away in the tree.
2242 rb_key = cfq_slice_offset(cfqd, cfqq) + now;
2243 rb_key -= cfqq->slice_resid;
2244 cfqq->slice_resid = 0;
2246 rb_key = -NSEC_PER_SEC;
2247 __cfqq = cfq_rb_first(st);
2248 rb_key += __cfqq ? __cfqq->rb_key : now;
2251 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
2254 * same position, nothing more to do
2256 if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
2259 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2260 cfqq->service_tree = NULL;
2265 cfqq->service_tree = st;
2266 p = &st->rb.rb_node;
2269 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
2272 * sort by key, that represents service time.
2274 if (rb_key < __cfqq->rb_key)
2275 p = &parent->rb_left;
2277 p = &parent->rb_right;
2283 st->left = &cfqq->rb_node;
2285 cfqq->rb_key = rb_key;
2286 rb_link_node(&cfqq->rb_node, parent, p);
2287 rb_insert_color(&cfqq->rb_node, &st->rb);
2289 if (add_front || !new_cfqq)
2291 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
2294 static struct cfq_queue *
2295 cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
2296 sector_t sector, struct rb_node **ret_parent,
2297 struct rb_node ***rb_link)
2299 struct rb_node **p, *parent;
2300 struct cfq_queue *cfqq = NULL;
2308 cfqq = rb_entry(parent, struct cfq_queue, p_node);
2311 * Sort strictly based on sector. Smallest to the left,
2312 * largest to the right.
2314 if (sector > blk_rq_pos(cfqq->next_rq))
2315 n = &(*p)->rb_right;
2316 else if (sector < blk_rq_pos(cfqq->next_rq))
2324 *ret_parent = parent;
2330 static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2332 struct rb_node **p, *parent;
2333 struct cfq_queue *__cfqq;
2336 rb_erase(&cfqq->p_node, cfqq->p_root);
2337 cfqq->p_root = NULL;
2340 if (cfq_class_idle(cfqq))
2345 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
2346 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
2347 blk_rq_pos(cfqq->next_rq), &parent, &p);
2349 rb_link_node(&cfqq->p_node, parent, p);
2350 rb_insert_color(&cfqq->p_node, cfqq->p_root);
2352 cfqq->p_root = NULL;
2356 * Update cfqq's position in the service tree.
2358 static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2361 * Resorting requires the cfqq to be on the RR list already.
2363 if (cfq_cfqq_on_rr(cfqq)) {
2364 cfq_service_tree_add(cfqd, cfqq, 0);
2365 cfq_prio_tree_add(cfqd, cfqq);
2370 * add to busy list of queues for service, trying to be fair in ordering
2371 * the pending list according to last request service
2373 static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2375 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
2376 BUG_ON(cfq_cfqq_on_rr(cfqq));
2377 cfq_mark_cfqq_on_rr(cfqq);
2378 cfqd->busy_queues++;
2379 if (cfq_cfqq_sync(cfqq))
2380 cfqd->busy_sync_queues++;
2382 cfq_resort_rr_list(cfqd, cfqq);
2386 * Called when the cfqq no longer has requests pending, remove it from
2389 static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2391 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
2392 BUG_ON(!cfq_cfqq_on_rr(cfqq));
2393 cfq_clear_cfqq_on_rr(cfqq);
2395 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
2396 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2397 cfqq->service_tree = NULL;
2400 rb_erase(&cfqq->p_node, cfqq->p_root);
2401 cfqq->p_root = NULL;
2404 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
2405 BUG_ON(!cfqd->busy_queues);
2406 cfqd->busy_queues--;
2407 if (cfq_cfqq_sync(cfqq))
2408 cfqd->busy_sync_queues--;
2412 * rb tree support functions
2414 static void cfq_del_rq_rb(struct request *rq)
2416 struct cfq_queue *cfqq = RQ_CFQQ(rq);
2417 const int sync = rq_is_sync(rq);
2419 BUG_ON(!cfqq->queued[sync]);
2420 cfqq->queued[sync]--;
2422 elv_rb_del(&cfqq->sort_list, rq);
2424 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
2426 * Queue will be deleted from service tree when we actually
2427 * expire it later. Right now just remove it from prio tree
2431 rb_erase(&cfqq->p_node, cfqq->p_root);
2432 cfqq->p_root = NULL;
2437 static void cfq_add_rq_rb(struct request *rq)
2439 struct cfq_queue *cfqq = RQ_CFQQ(rq);
2440 struct cfq_data *cfqd = cfqq->cfqd;
2441 struct request *prev;
2443 cfqq->queued[rq_is_sync(rq)]++;
2445 elv_rb_add(&cfqq->sort_list, rq);
2447 if (!cfq_cfqq_on_rr(cfqq))
2448 cfq_add_cfqq_rr(cfqd, cfqq);
2451 * check if this request is a better next-serve candidate
2453 prev = cfqq->next_rq;
2454 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
2457 * adjust priority tree position, if ->next_rq changes
2459 if (prev != cfqq->next_rq)
2460 cfq_prio_tree_add(cfqd, cfqq);
2462 BUG_ON(!cfqq->next_rq);
2465 static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
2467 elv_rb_del(&cfqq->sort_list, rq);
2468 cfqq->queued[rq_is_sync(rq)]--;
2469 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
2471 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
2475 static struct request *
2476 cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
2478 struct task_struct *tsk = current;
2479 struct cfq_io_cq *cic;
2480 struct cfq_queue *cfqq;
2482 cic = cfq_cic_lookup(cfqd, tsk->io_context);
2486 cfqq = cic_to_cfqq(cic, op_is_sync(bio->bi_opf));
2488 return elv_rb_find(&cfqq->sort_list, bio_end_sector(bio));
2493 static void cfq_activate_request(struct request_queue *q, struct request *rq)
2495 struct cfq_data *cfqd = q->elevator->elevator_data;
2497 cfqd->rq_in_driver++;
2498 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
2499 cfqd->rq_in_driver);
2501 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
2504 static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
2506 struct cfq_data *cfqd = q->elevator->elevator_data;
2508 WARN_ON(!cfqd->rq_in_driver);
2509 cfqd->rq_in_driver--;
2510 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
2511 cfqd->rq_in_driver);
2514 static void cfq_remove_request(struct request *rq)
2516 struct cfq_queue *cfqq = RQ_CFQQ(rq);
2518 if (cfqq->next_rq == rq)
2519 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
2521 list_del_init(&rq->queuelist);
2524 cfqq->cfqd->rq_queued--;
2525 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
2526 if (rq->cmd_flags & REQ_PRIO) {
2527 WARN_ON(!cfqq->prio_pending);
2528 cfqq->prio_pending--;
2532 static enum elv_merge cfq_merge(struct request_queue *q, struct request **req,
2535 struct cfq_data *cfqd = q->elevator->elevator_data;
2536 struct request *__rq;
2538 __rq = cfq_find_rq_fmerge(cfqd, bio);
2539 if (__rq && elv_bio_merge_ok(__rq, bio)) {
2541 return ELEVATOR_FRONT_MERGE;
2544 return ELEVATOR_NO_MERGE;
2547 static void cfq_merged_request(struct request_queue *q, struct request *req,
2548 enum elv_merge type)
2550 if (type == ELEVATOR_FRONT_MERGE) {
2551 struct cfq_queue *cfqq = RQ_CFQQ(req);
2553 cfq_reposition_rq_rb(cfqq, req);
2557 static void cfq_bio_merged(struct request_queue *q, struct request *req,
2560 cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_opf);
2564 cfq_merged_requests(struct request_queue *q, struct request *rq,
2565 struct request *next)
2567 struct cfq_queue *cfqq = RQ_CFQQ(rq);
2568 struct cfq_data *cfqd = q->elevator->elevator_data;
2571 * reposition in fifo if next is older than rq
2573 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
2574 next->fifo_time < rq->fifo_time &&
2575 cfqq == RQ_CFQQ(next)) {
2576 list_move(&rq->queuelist, &next->queuelist);
2577 rq->fifo_time = next->fifo_time;
2580 if (cfqq->next_rq == next)
2582 cfq_remove_request(next);
2583 cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags);
2585 cfqq = RQ_CFQQ(next);
2587 * all requests of this queue are merged to other queues, delete it
2588 * from the service tree. If it's the active_queue,
2589 * cfq_dispatch_requests() will choose to expire it or do idle
2591 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
2592 cfqq != cfqd->active_queue)
2593 cfq_del_cfqq_rr(cfqd, cfqq);
2596 static int cfq_allow_bio_merge(struct request_queue *q, struct request *rq,
2599 struct cfq_data *cfqd = q->elevator->elevator_data;
2600 bool is_sync = op_is_sync(bio->bi_opf);
2601 struct cfq_io_cq *cic;
2602 struct cfq_queue *cfqq;
2605 * Disallow merge of a sync bio into an async request.
2607 if (is_sync && !rq_is_sync(rq))
2611 * Lookup the cfqq that this bio will be queued with and allow
2612 * merge only if rq is queued there.
2614 cic = cfq_cic_lookup(cfqd, current->io_context);
2618 cfqq = cic_to_cfqq(cic, is_sync);
2619 return cfqq == RQ_CFQQ(rq);
2622 static int cfq_allow_rq_merge(struct request_queue *q, struct request *rq,
2623 struct request *next)
2625 return RQ_CFQQ(rq) == RQ_CFQQ(next);
2628 static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2630 hrtimer_try_to_cancel(&cfqd->idle_slice_timer);
2631 cfqg_stats_update_idle_time(cfqq->cfqg);
2634 static void __cfq_set_active_queue(struct cfq_data *cfqd,
2635 struct cfq_queue *cfqq)
2638 cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
2639 cfqd->serving_wl_class, cfqd->serving_wl_type);
2640 cfqg_stats_update_avg_queue_size(cfqq->cfqg);
2641 cfqq->slice_start = 0;
2642 cfqq->dispatch_start = ktime_get_ns();
2643 cfqq->allocated_slice = 0;
2644 cfqq->slice_end = 0;
2645 cfqq->slice_dispatch = 0;
2646 cfqq->nr_sectors = 0;
2648 cfq_clear_cfqq_wait_request(cfqq);
2649 cfq_clear_cfqq_must_dispatch(cfqq);
2650 cfq_clear_cfqq_must_alloc_slice(cfqq);
2651 cfq_clear_cfqq_fifo_expire(cfqq);
2652 cfq_mark_cfqq_slice_new(cfqq);
2654 cfq_del_timer(cfqd, cfqq);
2657 cfqd->active_queue = cfqq;
2661 * current cfqq expired its slice (or was too idle), select new one
2664 __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2667 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
2669 if (cfq_cfqq_wait_request(cfqq))
2670 cfq_del_timer(cfqd, cfqq);
2672 cfq_clear_cfqq_wait_request(cfqq);
2673 cfq_clear_cfqq_wait_busy(cfqq);
2676 * If this cfqq is shared between multiple processes, check to
2677 * make sure that those processes are still issuing I/Os within
2678 * the mean seek distance. If not, it may be time to break the
2679 * queues apart again.
2681 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
2682 cfq_mark_cfqq_split_coop(cfqq);
2685 * store what was left of this slice, if the queue idled/timed out
2688 if (cfq_cfqq_slice_new(cfqq))
2689 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
2691 cfqq->slice_resid = cfqq->slice_end - ktime_get_ns();
2692 cfq_log_cfqq(cfqd, cfqq, "resid=%lld", cfqq->slice_resid);
2695 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
2697 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
2698 cfq_del_cfqq_rr(cfqd, cfqq);
2700 cfq_resort_rr_list(cfqd, cfqq);
2702 if (cfqq == cfqd->active_queue)
2703 cfqd->active_queue = NULL;
2705 if (cfqd->active_cic) {
2706 put_io_context(cfqd->active_cic->icq.ioc);
2707 cfqd->active_cic = NULL;
2711 static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
2713 struct cfq_queue *cfqq = cfqd->active_queue;
2716 __cfq_slice_expired(cfqd, cfqq, timed_out);
2720 * Get next queue for service. Unless we have a queue preemption,
2721 * we'll simply select the first cfqq in the service tree.
2723 static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
2725 struct cfq_rb_root *st = st_for(cfqd->serving_group,
2726 cfqd->serving_wl_class, cfqd->serving_wl_type);
2728 if (!cfqd->rq_queued)
2731 /* There is nothing to dispatch */
2734 if (RB_EMPTY_ROOT(&st->rb))
2736 return cfq_rb_first(st);
2739 static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
2741 struct cfq_group *cfqg;
2742 struct cfq_queue *cfqq;
2744 struct cfq_rb_root *st;
2746 if (!cfqd->rq_queued)
2749 cfqg = cfq_get_next_cfqg(cfqd);
2753 for_each_cfqg_st(cfqg, i, j, st) {
2754 cfqq = cfq_rb_first(st);
2762 * Get and set a new active queue for service.
2764 static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
2765 struct cfq_queue *cfqq)
2768 cfqq = cfq_get_next_queue(cfqd);
2770 __cfq_set_active_queue(cfqd, cfqq);
2774 static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
2777 if (blk_rq_pos(rq) >= cfqd->last_position)
2778 return blk_rq_pos(rq) - cfqd->last_position;
2780 return cfqd->last_position - blk_rq_pos(rq);
2783 static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2786 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
2789 static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
2790 struct cfq_queue *cur_cfqq)
2792 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
2793 struct rb_node *parent, *node;
2794 struct cfq_queue *__cfqq;
2795 sector_t sector = cfqd->last_position;
2797 if (RB_EMPTY_ROOT(root))
2801 * First, if we find a request starting at the end of the last
2802 * request, choose it.
2804 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
2809 * If the exact sector wasn't found, the parent of the NULL leaf
2810 * will contain the closest sector.
2812 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
2813 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
2816 if (blk_rq_pos(__cfqq->next_rq) < sector)
2817 node = rb_next(&__cfqq->p_node);
2819 node = rb_prev(&__cfqq->p_node);
2823 __cfqq = rb_entry(node, struct cfq_queue, p_node);
2824 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
2832 * cur_cfqq - passed in so that we don't decide that the current queue is
2833 * closely cooperating with itself.
2835 * So, basically we're assuming that that cur_cfqq has dispatched at least
2836 * one request, and that cfqd->last_position reflects a position on the disk
2837 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
2840 static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
2841 struct cfq_queue *cur_cfqq)
2843 struct cfq_queue *cfqq;
2845 if (cfq_class_idle(cur_cfqq))
2847 if (!cfq_cfqq_sync(cur_cfqq))
2849 if (CFQQ_SEEKY(cur_cfqq))
2853 * Don't search priority tree if it's the only queue in the group.
2855 if (cur_cfqq->cfqg->nr_cfqq == 1)
2859 * We should notice if some of the queues are cooperating, eg
2860 * working closely on the same area of the disk. In that case,
2861 * we can group them together and don't waste time idling.
2863 cfqq = cfqq_close(cfqd, cur_cfqq);
2867 /* If new queue belongs to different cfq_group, don't choose it */
2868 if (cur_cfqq->cfqg != cfqq->cfqg)
2872 * It only makes sense to merge sync queues.
2874 if (!cfq_cfqq_sync(cfqq))
2876 if (CFQQ_SEEKY(cfqq))
2880 * Do not merge queues of different priority classes
2882 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
2889 * Determine whether we should enforce idle window for this queue.
2892 static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2894 enum wl_class_t wl_class = cfqq_class(cfqq);
2895 struct cfq_rb_root *st = cfqq->service_tree;
2900 if (!cfqd->cfq_slice_idle)
2903 /* We never do for idle class queues. */
2904 if (wl_class == IDLE_WORKLOAD)
2907 /* We do for queues that were marked with idle window flag. */
2908 if (cfq_cfqq_idle_window(cfqq) &&
2909 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
2913 * Otherwise, we do only if they are the last ones
2914 * in their service tree.
2916 if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
2917 !cfq_io_thinktime_big(cfqd, &st->ttime, false))
2919 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
2923 static void cfq_arm_slice_timer(struct cfq_data *cfqd)
2925 struct cfq_queue *cfqq = cfqd->active_queue;
2926 struct cfq_rb_root *st = cfqq->service_tree;
2927 struct cfq_io_cq *cic;
2928 u64 sl, group_idle = 0;
2929 u64 now = ktime_get_ns();
2932 * SSD device without seek penalty, disable idling. But only do so
2933 * for devices that support queuing, otherwise we still have a problem
2934 * with sync vs async workloads.
2936 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
2939 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
2940 WARN_ON(cfq_cfqq_slice_new(cfqq));
2943 * idle is disabled, either manually or by past process history
2945 if (!cfq_should_idle(cfqd, cfqq)) {
2946 /* no queue idling. Check for group idling */
2947 if (cfqd->cfq_group_idle)
2948 group_idle = cfqd->cfq_group_idle;
2954 * still active requests from this queue, don't idle
2956 if (cfqq->dispatched)
2960 * task has exited, don't wait
2962 cic = cfqd->active_cic;
2963 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
2967 * If our average think time is larger than the remaining time
2968 * slice, then don't idle. This avoids overrunning the allotted
2971 if (sample_valid(cic->ttime.ttime_samples) &&
2972 (cfqq->slice_end - now < cic->ttime.ttime_mean)) {
2973 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%llu",
2974 cic->ttime.ttime_mean);
2979 * There are other queues in the group or this is the only group and
2980 * it has too big thinktime, don't do group idle.
2983 (cfqq->cfqg->nr_cfqq > 1 ||
2984 cfq_io_thinktime_big(cfqd, &st->ttime, true)))
2987 cfq_mark_cfqq_wait_request(cfqq);
2990 sl = cfqd->cfq_group_idle;
2992 sl = cfqd->cfq_slice_idle;
2994 hrtimer_start(&cfqd->idle_slice_timer, ns_to_ktime(sl),
2996 cfqg_stats_set_start_idle_time(cfqq->cfqg);
2997 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %llu group_idle: %d", sl,
2998 group_idle ? 1 : 0);
3002 * Move request from internal lists to the request queue dispatch list.
3004 static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
3006 struct cfq_data *cfqd = q->elevator->elevator_data;
3007 struct cfq_queue *cfqq = RQ_CFQQ(rq);
3009 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
3011 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
3012 cfq_remove_request(rq);
3014 (RQ_CFQG(rq))->dispatched++;
3015 elv_dispatch_sort(q, rq);
3017 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
3018 cfqq->nr_sectors += blk_rq_sectors(rq);
3022 * return expired entry, or NULL to just start from scratch in rbtree
3024 static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
3026 struct request *rq = NULL;
3028 if (cfq_cfqq_fifo_expire(cfqq))
3031 cfq_mark_cfqq_fifo_expire(cfqq);
3033 if (list_empty(&cfqq->fifo))
3036 rq = rq_entry_fifo(cfqq->fifo.next);
3037 if (ktime_get_ns() < rq->fifo_time)
3044 cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3046 const int base_rq = cfqd->cfq_slice_async_rq;
3048 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
3050 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
3054 * Must be called with the queue_lock held.
3056 static int cfqq_process_refs(struct cfq_queue *cfqq)
3058 int process_refs, io_refs;
3060 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
3061 process_refs = cfqq->ref - io_refs;
3062 BUG_ON(process_refs < 0);
3063 return process_refs;
3066 static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
3068 int process_refs, new_process_refs;
3069 struct cfq_queue *__cfqq;
3072 * If there are no process references on the new_cfqq, then it is
3073 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
3074 * chain may have dropped their last reference (not just their
3075 * last process reference).
3077 if (!cfqq_process_refs(new_cfqq))
3080 /* Avoid a circular list and skip interim queue merges */
3081 while ((__cfqq = new_cfqq->new_cfqq)) {
3087 process_refs = cfqq_process_refs(cfqq);
3088 new_process_refs = cfqq_process_refs(new_cfqq);
3090 * If the process for the cfqq has gone away, there is no
3091 * sense in merging the queues.
3093 if (process_refs == 0 || new_process_refs == 0)
3097 * Merge in the direction of the lesser amount of work.
3099 if (new_process_refs >= process_refs) {
3100 cfqq->new_cfqq = new_cfqq;
3101 new_cfqq->ref += process_refs;
3103 new_cfqq->new_cfqq = cfqq;
3104 cfqq->ref += new_process_refs;
3108 static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
3109 struct cfq_group *cfqg, enum wl_class_t wl_class)
3111 struct cfq_queue *queue;
3113 bool key_valid = false;
3115 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
3117 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
3118 /* select the one with lowest rb_key */
3119 queue = cfq_rb_first(st_for(cfqg, wl_class, i));
3121 (!key_valid || queue->rb_key < lowest_key)) {
3122 lowest_key = queue->rb_key;
3132 choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
3136 struct cfq_rb_root *st;
3138 enum wl_class_t original_class = cfqd->serving_wl_class;
3139 u64 now = ktime_get_ns();
3141 /* Choose next priority. RT > BE > IDLE */
3142 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
3143 cfqd->serving_wl_class = RT_WORKLOAD;
3144 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
3145 cfqd->serving_wl_class = BE_WORKLOAD;
3147 cfqd->serving_wl_class = IDLE_WORKLOAD;
3148 cfqd->workload_expires = now + jiffies_to_nsecs(1);
3152 if (original_class != cfqd->serving_wl_class)
3156 * For RT and BE, we have to choose also the type
3157 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
3160 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
3164 * check workload expiration, and that we still have other queues ready
3166 if (count && !(now > cfqd->workload_expires))
3170 /* otherwise select new workload type */
3171 cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
3172 cfqd->serving_wl_class);
3173 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
3177 * the workload slice is computed as a fraction of target latency
3178 * proportional to the number of queues in that workload, over
3179 * all the queues in the same priority class
3181 group_slice = cfq_group_slice(cfqd, cfqg);
3183 slice = div_u64(group_slice * count,
3184 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
3185 cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
3188 if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
3192 * Async queues are currently system wide. Just taking
3193 * proportion of queues with-in same group will lead to higher
3194 * async ratio system wide as generally root group is going
3195 * to have higher weight. A more accurate thing would be to
3196 * calculate system wide asnc/sync ratio.
3198 tmp = cfqd->cfq_target_latency *
3199 cfqg_busy_async_queues(cfqd, cfqg);
3200 tmp = div_u64(tmp, cfqd->busy_queues);
3201 slice = min_t(u64, slice, tmp);
3203 /* async workload slice is scaled down according to
3204 * the sync/async slice ratio. */
3205 slice = div64_u64(slice*cfqd->cfq_slice[0], cfqd->cfq_slice[1]);
3207 /* sync workload slice is at least 2 * cfq_slice_idle */
3208 slice = max(slice, 2 * cfqd->cfq_slice_idle);
3210 slice = max_t(u64, slice, CFQ_MIN_TT);
3211 cfq_log(cfqd, "workload slice:%llu", slice);
3212 cfqd->workload_expires = now + slice;
3215 static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
3217 struct cfq_rb_root *st = &cfqd->grp_service_tree;
3218 struct cfq_group *cfqg;
3220 if (RB_EMPTY_ROOT(&st->rb))
3222 cfqg = cfq_rb_first_group(st);
3223 update_min_vdisktime(st);
3227 static void cfq_choose_cfqg(struct cfq_data *cfqd)
3229 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
3230 u64 now = ktime_get_ns();
3232 cfqd->serving_group = cfqg;
3234 /* Restore the workload type data */
3235 if (cfqg->saved_wl_slice) {
3236 cfqd->workload_expires = now + cfqg->saved_wl_slice;
3237 cfqd->serving_wl_type = cfqg->saved_wl_type;
3238 cfqd->serving_wl_class = cfqg->saved_wl_class;
3240 cfqd->workload_expires = now - 1;
3242 choose_wl_class_and_type(cfqd, cfqg);
3246 * Select a queue for service. If we have a current active queue,
3247 * check whether to continue servicing it, or retrieve and set a new one.
3249 static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
3251 struct cfq_queue *cfqq, *new_cfqq = NULL;
3252 u64 now = ktime_get_ns();
3254 cfqq = cfqd->active_queue;
3258 if (!cfqd->rq_queued)
3262 * We were waiting for group to get backlogged. Expire the queue
3264 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
3268 * The active queue has run out of time, expire it and select new.
3270 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
3272 * If slice had not expired at the completion of last request
3273 * we might not have turned on wait_busy flag. Don't expire
3274 * the queue yet. Allow the group to get backlogged.
3276 * The very fact that we have used the slice, that means we
3277 * have been idling all along on this queue and it should be
3278 * ok to wait for this request to complete.
3280 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
3281 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
3285 goto check_group_idle;
3289 * The active queue has requests and isn't expired, allow it to
3292 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
3296 * If another queue has a request waiting within our mean seek
3297 * distance, let it run. The expire code will check for close
3298 * cooperators and put the close queue at the front of the service
3299 * tree. If possible, merge the expiring queue with the new cfqq.
3301 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
3303 if (!cfqq->new_cfqq)
3304 cfq_setup_merge(cfqq, new_cfqq);
3309 * No requests pending. If the active queue still has requests in
3310 * flight or is idling for a new request, allow either of these
3311 * conditions to happen (or time out) before selecting a new queue.
3313 if (hrtimer_active(&cfqd->idle_slice_timer)) {
3319 * This is a deep seek queue, but the device is much faster than
3320 * the queue can deliver, don't idle
3322 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
3323 (cfq_cfqq_slice_new(cfqq) ||
3324 (cfqq->slice_end - now > now - cfqq->slice_start))) {
3325 cfq_clear_cfqq_deep(cfqq);
3326 cfq_clear_cfqq_idle_window(cfqq);
3329 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
3335 * If group idle is enabled and there are requests dispatched from
3336 * this group, wait for requests to complete.
3339 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
3340 cfqq->cfqg->dispatched &&
3341 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
3347 cfq_slice_expired(cfqd, 0);
3350 * Current queue expired. Check if we have to switch to a new
3354 cfq_choose_cfqg(cfqd);
3356 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
3361 static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)