mfd: wm831x: Remove redundant !pdata checks
[linux/fpc-iii.git] / block / cfq-iosched.c
blobda69b079725fbf62a407db76f7c5c430c52be3f9
1 /*
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>
8 */
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>
19 #include "blk.h"
20 #include "blk-wbt.h"
23 * tunables
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
77 struct cfq_ttime {
78 u64 last_end_request;
80 u64 ttime_total;
81 u64 ttime_mean;
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.
91 struct cfq_rb_root {
92 struct rb_root rb;
93 struct rb_node *left;
94 unsigned count;
95 u64 min_vdisktime;
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
104 struct cfq_queue {
105 /* reference count */
106 int ref;
107 /* various state flags, see below */
108 unsigned int flags;
109 /* parent cfq_data */
110 struct cfq_data *cfqd;
111 /* service_tree member */
112 struct rb_node rb_node;
113 /* service_tree key */
114 u64 rb_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 */
124 int queued[2];
125 /* currently allocated requests */
126 int allocated[2];
127 /* fifo list of requests in sort_list */
128 struct list_head fifo;
130 /* time when queue got scheduled in to dispatch first request. */
131 u64 dispatch_start;
132 u64 allocated_slice;
133 u64 slice_dispatch;
134 /* time when first request from queue completed and slice started. */
135 u64 slice_start;
136 u64 slice_end;
137 s64 slice_resid;
139 /* pending priority requests */
140 int prio_pending;
141 /* number of requests that are on the dispatch list or inside driver */
142 int dispatched;
144 /* io prio of this group */
145 unsigned short ioprio, org_ioprio;
146 unsigned short ioprio_class, org_ioprio_class;
148 pid_t pid;
150 u32 seek_history;
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
164 enum wl_class_t {
165 BE_WORKLOAD = 0,
166 RT_WORKLOAD = 1,
167 IDLE_WORKLOAD = 2,
168 CFQ_PRIO_NR,
172 * Second index in the service_trees.
174 enum wl_type_t {
175 ASYNC_WORKLOAD = 0,
176 SYNC_NOIDLE_WORKLOAD = 1,
177 SYNC_WORKLOAD = 2
180 struct cfqg_stats {
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;
211 uint16_t flags;
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;
221 unsigned int weight;
222 unsigned int leaf_weight;
225 /* This is per cgroup per device grouping structure */
226 struct cfq_group {
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 */
234 u64 vdisktime;
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
243 * of the parent.
245 int nr_active;
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.
266 unsigned int weight;
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 */
275 int nr_cfqq;
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;
295 u64 saved_wl_slice;
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 */
300 int dispatched;
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;
310 struct cfq_io_cq {
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 */
317 #endif
321 * Per block device queue structure
323 struct cfq_data {
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;
347 int rq_in_driver;
348 int rq_in_flight[2];
351 * queue-depth detection
353 int rq_queued;
354 int hw_tag;
356 * hw_tag can be
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)
359 * 0 => no NCQ
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];
384 u64 cfq_slice[2];
385 u64 cfq_slice_idle;
386 u64 cfq_group_idle;
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,
402 enum wl_type_t type)
404 if (!cfqg)
405 return NULL;
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; \
443 CFQ_CFQQ_FNS(on_rr);
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);
451 CFQ_CFQQ_FNS(sync);
452 CFQ_CFQQ_FNS(coop);
453 CFQ_CFQQ_FNS(split_coop);
454 CFQ_CFQQ_FNS(deep);
455 CFQ_CFQQ_FNS(wait_busy);
456 #undef CFQ_CFQQ_FNS
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,
463 CFQG_stats_idling,
464 CFQG_stats_empty,
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)
483 CFQG_FLAG_FNS(empty)
484 #undef CFQG_FLAG_FNS
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))
492 return;
494 now = sched_clock();
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))
508 return;
509 if (cfqg == curr_cfqg)
510 return;
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))
521 return;
523 now = sched_clock();
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))
540 return;
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))
548 return;
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 { \
655 char __pbuf[128]; \
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' : ' ',\
661 __pbuf, ##args); \
662 } while (0)
664 #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
665 char __pbuf[128]; \
667 blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \
668 blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \
669 } while (0)
671 static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
672 struct cfq_group *curr_cfqg,
673 unsigned int op)
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);
686 #endif
689 static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg,
690 unsigned int op)
692 blkg_rwstat_add(&cfqg->stats.queued, op, -1);
695 static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
696 unsigned int op)
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,
703 unsigned int op)
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);
715 /* @stats = 0 */
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);
731 #endif
734 /* @to += @from */
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);
750 #endif
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
756 * it's gone.
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))
765 return;
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)
777 return true;
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' : ' ',\
786 ##args)
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,
794 unsigned int op) { }
795 static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
796 unsigned int op) { }
797 static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
798 uint64_t start_time, uint64_t io_start_time,
799 unsigned int op) { }
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)
819 u64 slice;
820 if (!sample_valid(ttime->ttime_samples))
821 return false;
822 if (group_idle)
823 slice = cfqd->cfq_group_idle;
824 else
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)
839 return true;
840 else
841 return false;
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))
849 return RT_WORKLOAD;
850 return BE_WORKLOAD;
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)
895 if (ioc)
896 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
897 return NULL;
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,
906 bool is_sync)
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,
934 unsigned short prio)
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));
944 static inline u64
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);
975 if (delta > 0)
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);
984 if (delta < 0)
985 min_vdisktime = vdisktime;
987 return min_vdisktime;
990 static void update_min_vdisktime(struct cfq_rb_root *st)
992 struct cfq_group *cfqg;
994 if (st->left) {
995 cfqg = rb_entry_cfqg(st->left);
996 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
997 cfqg->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) /
1018 cfq_hist_divisor;
1019 return cfqg->busy_queues_avg[rt];
1022 static inline u64
1023 cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
1025 return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT;
1028 static inline u64
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;
1045 u64 low_slice;
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);
1057 return slice;
1060 static inline void
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))
1080 return false;
1081 if (ktime_get_ns() < cfqq->slice_end)
1082 return false;
1084 return true;
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)
1102 return rq2;
1103 if (rq2 == NULL)
1104 return rq1;
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.
1125 if (s1 >= last)
1126 d1 = s1 - last;
1127 else if (s1 + back_max >= last)
1128 d1 = (last - s1) * cfqd->cfq_back_penalty;
1129 else
1130 wrap |= CFQ_RQ1_WRAP;
1132 if (s2 >= last)
1133 d2 = s2 - last;
1134 else if (s2 + back_max >= last)
1135 d2 = (last - s2) * cfqd->cfq_back_penalty;
1136 else
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!
1145 switch (wrap) {
1146 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
1147 if (d1 < d2)
1148 return rq1;
1149 else if (d2 < d1)
1150 return rq2;
1151 else {
1152 if (s1 >= s2)
1153 return rq1;
1154 else
1155 return rq2;
1158 case CFQ_RQ2_WRAP:
1159 return rq1;
1160 case CFQ_RQ1_WRAP:
1161 return rq2;
1162 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
1163 default:
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.
1170 if (s1 <= s2)
1171 return rq1;
1172 else
1173 return rq2;
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 */
1183 if (!root->count)
1184 return NULL;
1186 if (!root->left)
1187 root->left = rb_first(&root->rb);
1189 if (root->left)
1190 return rb_entry(root->left, struct cfq_queue, rb_node);
1192 return NULL;
1195 static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
1197 if (!root->left)
1198 root->left = rb_first(&root->rb);
1200 if (root->left)
1201 return rb_entry_cfqg(root->left);
1203 return NULL;
1206 static void rb_erase_init(struct rb_node *n, struct rb_root *root)
1208 rb_erase(n, root);
1209 RB_CLEAR_NODE(n);
1212 static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
1214 if (root->left == n)
1215 root->left = NULL;
1216 rb_erase_init(n, &root->rb);
1217 --root->count;
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));
1233 if (rbprev)
1234 prev = rb_entry_rq(rbprev);
1236 if (rbnext)
1237 next = rb_entry_rq(rbnext);
1238 else {
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));
1257 static inline s64
1258 cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
1260 return cfqg->vdisktime - st->min_vdisktime;
1263 static void
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);
1270 int left = 1;
1272 while (*node != NULL) {
1273 parent = *node;
1274 __cfqg = rb_entry_cfqg(parent);
1276 if (key < cfqg_key(st, __cfqg))
1277 node = &parent->rb_left;
1278 else {
1279 node = &parent->rb_right;
1280 left = 0;
1284 if (left)
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
1294 static void
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;
1303 static void
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;
1314 static void
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;
1320 bool propagate;
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))) {
1353 if (propagate) {
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;
1359 pos = parent;
1362 cfqg->vfraction = max_t(unsigned, vfr, 1);
1365 static void
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;
1370 struct rb_node *n;
1372 cfqg->nr_cfqq++;
1373 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1374 return;
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);
1382 if (n) {
1383 __cfqg = rb_entry_cfqg(n);
1384 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
1385 } else
1386 cfqg->vdisktime = st->min_vdisktime;
1387 cfq_group_service_tree_add(st, cfqg);
1390 static void
1391 cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
1393 struct cfq_group *pos = cfqg;
1394 bool propagate;
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;
1403 while (propagate) {
1404 struct cfq_group *parent = cfqg_parent(pos);
1406 /* @pos has 0 nr_active at this point */
1407 WARN_ON_ONCE(pos->children_weight);
1408 pos->vfraction = 0;
1410 if (!parent)
1411 break;
1413 propagate = !--parent->nr_active;
1414 parent->children_weight -= pos->weight;
1415 pos = parent;
1418 /* remove from the service tree */
1419 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1420 cfq_rb_erase(&cfqg->rb_node, st);
1423 static void
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);
1429 cfqg->nr_cfqq--;
1431 /* If there are other cfq queues under this group, don't delete it */
1432 if (cfqg->nr_cfqq)
1433 return;
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)
1444 u64 slice_used;
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));
1460 } else {
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;
1471 return slice_used;
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;
1481 unsigned int vfr;
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;
1508 } else
1509 cfqg->saved_wl_slice = 0;
1511 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1512 st->min_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;
1531 int i, j;
1533 for_each_cfqg_st(cfqg, i, j, st)
1534 *st = CFQ_RB_ROOT;
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);
1559 #endif
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))
1569 goto err;
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))
1579 goto err;
1580 #endif
1581 return 0;
1582 err:
1583 cfqg_stats_exit(stats);
1584 return -ENOMEM;
1587 static struct blkcg_policy_data *cfq_cpd_alloc(gfp_t gfp)
1589 struct cfq_group_data *cgd;
1591 cgd = kzalloc(sizeof(*cgd), gfp);
1592 if (!cgd)
1593 return NULL;
1594 return &cgd->cpd;
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)
1604 weight *= 2;
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)
1622 weight *= 2;
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);
1633 if (!cfqg)
1634 return NULL;
1636 cfq_init_cfqg_base(cfqg);
1637 if (cfqg_stats_init(&cfqg->stats, gfp)) {
1638 kfree(cfqg);
1639 return NULL;
1642 return &cfqg->pd;
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);
1657 int i;
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);
1683 return kfree(cfqg);
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);
1699 if (likely(blkg))
1700 return blkg_to_cfqg(blkg);
1701 return NULL;
1704 static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1706 cfqq->cfqg = cfqg;
1707 /* cfqq reference on cfqg */
1708 cfqg_get(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)
1717 return 0;
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,
1725 0, false);
1726 return 0;
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)
1735 return 0;
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,
1743 0, false);
1744 return 0;
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;
1753 if (cgd)
1754 val = cgd->weight;
1756 seq_printf(sf, "%u\n", val);
1757 return 0;
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;
1766 if (cgd)
1767 val = cgd->leaf_weight;
1769 seq_printf(sf, "%u\n", val);
1770 return 0;
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;
1783 int ret;
1784 u64 v;
1786 ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
1787 if (ret)
1788 return ret;
1790 if (sscanf(ctx.body, "%llu", &v) == 1) {
1791 /* require "default" on dfl */
1792 ret = -ERANGE;
1793 if (!v && on_dfl)
1794 goto out_finish;
1795 } else if (!strcmp(strim(ctx.body), "default")) {
1796 v = 0;
1797 } else {
1798 ret = -EINVAL;
1799 goto out_finish;
1802 cfqg = blkg_to_cfqg(ctx.blkg);
1803 cfqgd = blkcg_to_cfqgd(blkcg);
1805 ret = -ERANGE;
1806 if (!v || (v >= min && v <= max)) {
1807 if (!is_leaf_weight) {
1808 cfqg->dev_weight = v;
1809 cfqg->new_weight = v ?: cfqgd->weight;
1810 } else {
1811 cfqg->dev_leaf_weight = v;
1812 cfqg->new_leaf_weight = v ?: cfqgd->leaf_weight;
1814 ret = 0;
1816 out_finish:
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;
1841 int ret = 0;
1843 if (val < min || val > max)
1844 return -ERANGE;
1846 spin_lock_irq(&blkcg->lock);
1847 cfqgd = blkcg_to_cfqgd(blkcg);
1848 if (!cfqgd) {
1849 ret = -EINVAL;
1850 goto out;
1853 if (!is_leaf_weight)
1854 cfqgd->weight = val;
1855 else
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);
1861 if (!cfqg)
1862 continue;
1864 if (!is_leaf_weight) {
1865 if (reset_dev)
1866 cfqg->dev_weight = 0;
1867 if (!cfqg->dev_weight)
1868 cfqg->new_weight = cfqgd->weight;
1869 } else {
1870 if (reset_dev)
1871 cfqg->dev_leaf_weight = 0;
1872 if (!cfqg->dev_leaf_weight)
1873 cfqg->new_leaf_weight = cfqgd->leaf_weight;
1877 out:
1878 spin_unlock_irq(&blkcg->lock);
1879 return ret;
1882 static int cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft,
1883 u64 val)
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);
1898 return 0;
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);
1905 return 0;
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);
1929 return 0;
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);
1937 return 0;
1940 static u64 cfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd,
1941 int off)
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);
1952 return 0;
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,
1970 false);
1971 return 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);
1980 u64 v = 0;
1982 if (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);
1987 return 0;
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,
1995 0, false);
1996 return 0;
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,
2009 .name = "weight",
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,
2023 .name = "weight",
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 */
2042 .name = "time",
2043 .private = offsetof(struct cfq_group, stats.time),
2044 .seq_show = cfqg_print_stat,
2047 .name = "sectors",
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,
2142 .name = "dequeue",
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 */
2152 { } /* terminate */
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);
2163 return 0;
2166 static ssize_t cfq_set_weight_on_dfl(struct kernfs_open_file *of,
2167 char *buf, size_t nbytes, loff_t off)
2169 char *endp;
2170 int ret;
2171 u64 v;
2173 buf = strim(buf);
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[] = {
2188 .name = "weight",
2189 .flags = CFTYPE_NOT_ON_ROOT,
2190 .seq_show = cfq_print_weight_on_dfl,
2191 .write = cfq_set_weight_on_dfl,
2193 { } /* terminate */
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;
2203 static inline void
2204 cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
2205 cfqq->cfqg = 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,
2216 bool add_front)
2218 struct rb_node **p, *parent;
2219 struct cfq_queue *__cfqq;
2220 u64 rb_key;
2221 struct cfq_rb_root *st;
2222 int left;
2223 int new_cfqq = 1;
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;
2233 } else
2234 rb_key += now;
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;
2245 } else {
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)) {
2252 new_cfqq = 0;
2254 * same position, nothing more to do
2256 if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
2257 return;
2259 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2260 cfqq->service_tree = NULL;
2263 left = 1;
2264 parent = NULL;
2265 cfqq->service_tree = st;
2266 p = &st->rb.rb_node;
2267 while (*p) {
2268 parent = *p;
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;
2276 else {
2277 p = &parent->rb_right;
2278 left = 0;
2282 if (left)
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);
2288 st->count++;
2289 if (add_front || !new_cfqq)
2290 return;
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;
2302 parent = NULL;
2303 p = &root->rb_node;
2304 while (*p) {
2305 struct rb_node **n;
2307 parent = *p;
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))
2317 n = &(*p)->rb_left;
2318 else
2319 break;
2320 p = n;
2321 cfqq = NULL;
2324 *ret_parent = parent;
2325 if (rb_link)
2326 *rb_link = p;
2327 return cfqq;
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;
2335 if (cfqq->p_root) {
2336 rb_erase(&cfqq->p_node, cfqq->p_root);
2337 cfqq->p_root = NULL;
2340 if (cfq_class_idle(cfqq))
2341 return;
2342 if (!cfqq->next_rq)
2343 return;
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);
2348 if (!__cfqq) {
2349 rb_link_node(&cfqq->p_node, parent, p);
2350 rb_insert_color(&cfqq->p_node, cfqq->p_root);
2351 } else
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
2387 * the service tree.
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;
2399 if (cfqq->p_root) {
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
2428 * as it is empty.
2430 if (cfqq->p_root) {
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);
2470 cfq_add_rq_rb(rq);
2471 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
2472 rq->cmd_flags);
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);
2483 if (!cic)
2484 return NULL;
2486 cfqq = cic_to_cfqq(cic, op_is_sync(bio->bi_opf));
2487 if (cfqq)
2488 return elv_rb_find(&cfqq->sort_list, bio_end_sector(bio));
2490 return NULL;
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);
2522 cfq_del_rq_rb(rq);
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,
2533 struct bio *bio)
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)) {
2540 *req = __rq;
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,
2558 struct bio *bio)
2560 cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_opf);
2563 static void
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)
2581 cfqq->next_rq = rq;
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,
2597 struct bio *bio)
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))
2608 return false;
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);
2615 if (!cic)
2616 return false;
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)
2637 if (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
2663 static void
2664 __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2665 bool timed_out)
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
2687 if (timed_out) {
2688 if (cfq_cfqq_slice_new(cfqq))
2689 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
2690 else
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;
2715 if (cfqq)
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)
2729 return NULL;
2731 /* There is nothing to dispatch */
2732 if (!st)
2733 return NULL;
2734 if (RB_EMPTY_ROOT(&st->rb))
2735 return NULL;
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;
2743 int i, j;
2744 struct cfq_rb_root *st;
2746 if (!cfqd->rq_queued)
2747 return NULL;
2749 cfqg = cfq_get_next_cfqg(cfqd);
2750 if (!cfqg)
2751 return NULL;
2753 for_each_cfqg_st(cfqg, i, j, st) {
2754 cfqq = cfq_rb_first(st);
2755 if (cfqq)
2756 return cfqq;
2758 return NULL;
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)
2767 if (!cfqq)
2768 cfqq = cfq_get_next_queue(cfqd);
2770 __cfq_set_active_queue(cfqd, cfqq);
2771 return cfqq;
2774 static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
2775 struct request *rq)
2777 if (blk_rq_pos(rq) >= cfqd->last_position)
2778 return blk_rq_pos(rq) - cfqd->last_position;
2779 else
2780 return cfqd->last_position - blk_rq_pos(rq);
2783 static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2784 struct request *rq)
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))
2798 return NULL;
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);
2805 if (__cfqq)
2806 return __cfqq;
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))
2814 return __cfqq;
2816 if (blk_rq_pos(__cfqq->next_rq) < sector)
2817 node = rb_next(&__cfqq->p_node);
2818 else
2819 node = rb_prev(&__cfqq->p_node);
2820 if (!node)
2821 return NULL;
2823 __cfqq = rb_entry(node, struct cfq_queue, p_node);
2824 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
2825 return __cfqq;
2827 return NULL;
2831 * cfqd - obvious
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
2838 * assumption.
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))
2846 return NULL;
2847 if (!cfq_cfqq_sync(cur_cfqq))
2848 return NULL;
2849 if (CFQQ_SEEKY(cur_cfqq))
2850 return NULL;
2853 * Don't search priority tree if it's the only queue in the group.
2855 if (cur_cfqq->cfqg->nr_cfqq == 1)
2856 return NULL;
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);
2864 if (!cfqq)
2865 return NULL;
2867 /* If new queue belongs to different cfq_group, don't choose it */
2868 if (cur_cfqq->cfqg != cfqq->cfqg)
2869 return NULL;
2872 * It only makes sense to merge sync queues.
2874 if (!cfq_cfqq_sync(cfqq))
2875 return NULL;
2876 if (CFQQ_SEEKY(cfqq))
2877 return NULL;
2880 * Do not merge queues of different priority classes
2882 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
2883 return NULL;
2885 return 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;
2897 BUG_ON(!st);
2898 BUG_ON(!st->count);
2900 if (!cfqd->cfq_slice_idle)
2901 return false;
2903 /* We never do for idle class queues. */
2904 if (wl_class == IDLE_WORKLOAD)
2905 return false;
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))
2910 return true;
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))
2918 return true;
2919 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
2920 return false;
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)
2937 return;
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;
2949 else
2950 return;
2954 * still active requests from this queue, don't idle
2956 if (cfqq->dispatched)
2957 return;
2960 * task has exited, don't wait
2962 cic = cfqd->active_cic;
2963 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
2964 return;
2967 * If our average think time is larger than the remaining time
2968 * slice, then don't idle. This avoids overrunning the allotted
2969 * time slice.
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);
2975 return;
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.
2982 if (group_idle &&
2983 (cfqq->cfqg->nr_cfqq > 1 ||
2984 cfq_io_thinktime_big(cfqd, &st->ttime, true)))
2985 return;
2987 cfq_mark_cfqq_wait_request(cfqq);
2989 if (group_idle)
2990 sl = cfqd->cfq_group_idle;
2991 else
2992 sl = cfqd->cfq_slice_idle;
2994 hrtimer_start(&cfqd->idle_slice_timer, ns_to_ktime(sl),
2995 HRTIMER_MODE_REL);
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);
3013 cfqq->dispatched++;
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))
3029 return NULL;
3031 cfq_mark_cfqq_fifo_expire(cfqq);
3033 if (list_empty(&cfqq->fifo))
3034 return NULL;
3036 rq = rq_entry_fifo(cfqq->fifo.next);
3037 if (ktime_get_ns() < rq->fifo_time)
3038 rq = NULL;
3040 return rq;
3043 static inline int
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))
3078 return;
3080 /* Avoid a circular list and skip interim queue merges */
3081 while ((__cfqq = new_cfqq->new_cfqq)) {
3082 if (__cfqq == cfqq)
3083 return;
3084 new_cfqq = __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)
3094 return;
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;
3102 } else {
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;
3112 int i;
3113 bool key_valid = false;
3114 u64 lowest_key = 0;
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));
3120 if (queue &&
3121 (!key_valid || queue->rb_key < lowest_key)) {
3122 lowest_key = queue->rb_key;
3123 cur_best = i;
3124 key_valid = true;
3128 return cur_best;
3131 static void
3132 choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
3134 u64 slice;
3135 unsigned count;
3136 struct cfq_rb_root *st;
3137 u64 group_slice;
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;
3146 else {
3147 cfqd->serving_wl_class = IDLE_WORKLOAD;
3148 cfqd->workload_expires = now + jiffies_to_nsecs(1);
3149 return;
3152 if (original_class != cfqd->serving_wl_class)
3153 goto new_workload;
3156 * For RT and BE, we have to choose also the type
3157 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
3158 * expiration time
3160 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
3161 count = st->count;
3164 * check workload expiration, and that we still have other queues ready
3166 if (count && !(now > cfqd->workload_expires))
3167 return;
3169 new_workload:
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);
3174 count = st->count;
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,
3186 cfqg)));
3188 if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
3189 u64 tmp;
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]);
3206 } else
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))
3221 return NULL;
3222 cfqg = cfq_rb_first_group(st);
3223 update_min_vdisktime(st);
3224 return cfqg;
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;
3239 } else
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;
3255 if (!cfqq)
3256 goto new_queue;
3258 if (!cfqd->rq_queued)
3259 return NULL;
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))
3265 goto expire;
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)) {
3282 cfqq = NULL;
3283 goto keep_queue;
3284 } else
3285 goto check_group_idle;
3289 * The active queue has requests and isn't expired, allow it to
3290 * dispatch.
3292 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
3293 goto keep_queue;
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);
3302 if (new_cfqq) {
3303 if (!cfqq->new_cfqq)
3304 cfq_setup_merge(cfqq, new_cfqq);
3305 goto expire;
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)) {
3314 cfqq = NULL;
3315 goto keep_queue;
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)) {
3330 cfqq = NULL;
3331 goto keep_queue;
3335 * If group idle is enabled and there are requests dispatched from
3336 * this group, wait for requests to complete.
3338 check_group_idle:
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)) {
3342 cfqq = NULL;
3343 goto keep_queue;
3346 expire:
3347 cfq_slice_expired(cfqd, 0);
3348 new_queue:
3350 * Current queue expired. Check if we have to switch to a new
3351 * service tree
3353 if (!new_cfqq)
3354 cfq_choose_cfqg(cfqd);
3356 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
3357 keep_queue:
3358 return cfqq;
3361 static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
3363 int dispatched = 0;
3365 while (cfqq->next_rq) {
3366 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
3367 dispatched++;
3370 BUG_ON(!list_empty(&cfqq->fifo));
3372 /* By default cfqq is not expired if it is empty. Do it explicitly */
3373 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
3374 return dispatched;
3378 * Drain our current requests. Used for barriers and when switching
3379 * io schedulers on-the-fly.
3381 static int cfq_forced_dispatch(struct cfq_data *cfqd)
3383 struct cfq_queue *cfqq;
3384 int dispatched = 0;
3386 /* Expire the timeslice of the current active queue first */
3387 cfq_slice_expired(cfqd, 0);
3388 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
3389 __cfq_set_active_queue(cfqd, cfqq);
3390 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
3393 BUG_ON(cfqd->busy_queues);
3395 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
3396 return dispatched;
3399 static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
3400 struct cfq_queue *cfqq)
3402 u64 now = ktime_get_ns();
3404 /* the queue hasn't finished any request, can't estimate */
3405 if (cfq_cfqq_slice_new(cfqq))
3406 return true;
3407 if (now + cfqd->cfq_slice_idle * cfqq->dispatched > cfqq->slice_end)
3408 return true;
3410 return false;
3413 static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3415 unsigned int max_dispatch;
3417 if (cfq_cfqq_must_dispatch(cfqq))
3418 return true;
3421 * Drain async requests before we start sync IO
3423 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
3424 return false;
3427 * If this is an async queue and we have sync IO in flight, let it wait
3429 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
3430 return false;
3432 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
3433 if (cfq_class_idle(cfqq))
3434 max_dispatch = 1;
3437 * Does this cfqq already have too much IO in flight?
3439 if (cfqq->dispatched >= max_dispatch) {
3440 bool promote_sync = false;
3442 * idle queue must always only have a single IO in flight
3444 if (cfq_class_idle(cfqq))
3445 return false;
3448 * If there is only one sync queue
3449 * we can ignore async queue here and give the sync
3450 * queue no dispatch limit. The reason is a sync queue can
3451 * preempt async queue, limiting the sync queue doesn't make
3452 * sense. This is useful for aiostress test.
3454 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
3455 promote_sync = true;
3458 * We have other queues, don't allow more IO from this one
3460 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
3461 !promote_sync)
3462 return false;
3465 * Sole queue user, no limit
3467 if (cfqd->busy_queues == 1 || promote_sync)
3468 max_dispatch = -1;
3469 else
3471 * Normally we start throttling cfqq when cfq_quantum/2
3472 * requests have been dispatched. But we can drive
3473 * deeper queue depths at the beginning of slice
3474 * subjected to upper limit of cfq_quantum.
3475 * */
3476 max_dispatch = cfqd->cfq_quantum;
3480 * Async queues must wait a bit before being allowed dispatch.
3481 * We also ramp up the dispatch depth gradually for async IO,
3482 * based on the last sync IO we serviced
3484 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
3485 u64 last_sync = ktime_get_ns() - cfqd->last_delayed_sync;
3486 unsigned int depth;
3488 depth = div64_u64(last_sync, cfqd->cfq_slice[1]);
3489 if (!depth && !cfqq->dispatched)
3490 depth = 1;
3491 if (depth < max_dispatch)
3492 max_dispatch = depth;
3496 * If we're below the current max, allow a dispatch
3498 return cfqq->dispatched < max_dispatch;
3502 * Dispatch a request from cfqq, moving them to the request queue
3503 * dispatch list.
3505 static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3507 struct request *rq;
3509 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
3511 rq = cfq_check_fifo(cfqq);
3512 if (rq)
3513 cfq_mark_cfqq_must_dispatch(cfqq);
3515 if (!cfq_may_dispatch(cfqd, cfqq))
3516 return false;
3519 * follow expired path, else get first next available
3521 if (!rq)
3522 rq = cfqq->next_rq;
3523 else
3524 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
3527 * insert request into driver dispatch list
3529 cfq_dispatch_insert(cfqd->queue, rq);
3531 if (!cfqd->active_cic) {
3532 struct cfq_io_cq *cic = RQ_CIC(rq);
3534 atomic_long_inc(&cic->icq.ioc->refcount);
3535 cfqd->active_cic = cic;
3538 return true;
3542 * Find the cfqq that we need to service and move a request from that to the
3543 * dispatch list
3545 static int cfq_dispatch_requests(struct request_queue *q, int force)
3547 struct cfq_data *cfqd = q->elevator->elevator_data;
3548 struct cfq_queue *cfqq;
3550 if (!cfqd->busy_queues)
3551 return 0;
3553 if (unlikely(force))
3554 return cfq_forced_dispatch(cfqd);
3556 cfqq = cfq_select_queue(cfqd);
3557 if (!cfqq)
3558 return 0;
3561 * Dispatch a request from this cfqq, if it is allowed
3563 if (!cfq_dispatch_request(cfqd, cfqq))
3564 return 0;
3566 cfqq->slice_dispatch++;
3567 cfq_clear_cfqq_must_dispatch(cfqq);
3570 * expire an async queue immediately if it has used up its slice. idle
3571 * queue always expire after 1 dispatch round.
3573 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
3574 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
3575 cfq_class_idle(cfqq))) {
3576 cfqq->slice_end = ktime_get_ns() + 1;
3577 cfq_slice_expired(cfqd, 0);
3580 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
3581 return 1;
3585 * task holds one reference to the queue, dropped when task exits. each rq
3586 * in-flight on this queue also holds a reference, dropped when rq is freed.
3588 * Each cfq queue took a reference on the parent group. Drop it now.
3589 * queue lock must be held here.
3591 static void cfq_put_queue(struct cfq_queue *cfqq)
3593 struct cfq_data *cfqd = cfqq->cfqd;
3594 struct cfq_group *cfqg;
3596 BUG_ON(cfqq->ref <= 0);
3598 cfqq->ref--;
3599 if (cfqq->ref)
3600 return;
3602 cfq_log_cfqq(cfqd, cfqq, "put_queue");
3603 BUG_ON(rb_first(&cfqq->sort_list));
3604 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
3605 cfqg = cfqq->cfqg;
3607 if (unlikely(cfqd->active_queue == cfqq)) {
3608 __cfq_slice_expired(cfqd, cfqq, 0);
3609 cfq_schedule_dispatch(cfqd);
3612 BUG_ON(cfq_cfqq_on_rr(cfqq));
3613 kmem_cache_free(cfq_pool, cfqq);
3614 cfqg_put(cfqg);
3617 static void cfq_put_cooperator(struct cfq_queue *cfqq)
3619 struct cfq_queue *__cfqq, *next;
3622 * If this queue was scheduled to merge with another queue, be
3623 * sure to drop the reference taken on that queue (and others in
3624 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
3626 __cfqq = cfqq->new_cfqq;
3627 while (__cfqq) {
3628 if (__cfqq == cfqq) {
3629 WARN(1, "cfqq->new_cfqq loop detected\n");
3630 break;
3632 next = __cfqq->new_cfqq;
3633 cfq_put_queue(__cfqq);
3634 __cfqq = next;
3638 static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3640 if (unlikely(cfqq == cfqd->active_queue)) {
3641 __cfq_slice_expired(cfqd, cfqq, 0);
3642 cfq_schedule_dispatch(cfqd);
3645 cfq_put_cooperator(cfqq);
3647 cfq_put_queue(cfqq);
3650 static void cfq_init_icq(struct io_cq *icq)
3652 struct cfq_io_cq *cic = icq_to_cic(icq);
3654 cic->ttime.last_end_request = ktime_get_ns();
3657 static void cfq_exit_icq(struct io_cq *icq)
3659 struct cfq_io_cq *cic = icq_to_cic(icq);
3660 struct cfq_data *cfqd = cic_to_cfqd(cic);
3662 if (cic_to_cfqq(cic, false)) {
3663 cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, false));
3664 cic_set_cfqq(cic, NULL, false);
3667 if (cic_to_cfqq(cic, true)) {
3668 cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, true));
3669 cic_set_cfqq(cic, NULL, true);
3673 static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
3675 struct task_struct *tsk = current;
3676 int ioprio_class;
3678 if (!cfq_cfqq_prio_changed(cfqq))
3679 return;
3681 ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
3682 switch (ioprio_class) {
3683 default:
3684 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
3685 case IOPRIO_CLASS_NONE:
3687 * no prio set, inherit CPU scheduling settings
3689 cfqq->ioprio = task_nice_ioprio(tsk);
3690 cfqq->ioprio_class = task_nice_ioclass(tsk);
3691 break;
3692 case IOPRIO_CLASS_RT:
3693 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
3694 cfqq->ioprio_class = IOPRIO_CLASS_RT;
3695 break;
3696 case IOPRIO_CLASS_BE:
3697 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
3698 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3699 break;
3700 case IOPRIO_CLASS_IDLE:
3701 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
3702 cfqq->ioprio = 7;
3703 cfq_clear_cfqq_idle_window(cfqq);
3704 break;
3708 * keep track of original prio settings in case we have to temporarily
3709 * elevate the priority of this queue
3711 cfqq->org_ioprio = cfqq->ioprio;
3712 cfqq->org_ioprio_class = cfqq->ioprio_class;
3713 cfq_clear_cfqq_prio_changed(cfqq);
3716 static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
3718 int ioprio = cic->icq.ioc->ioprio;
3719 struct cfq_data *cfqd = cic_to_cfqd(cic);
3720 struct cfq_queue *cfqq;
3723 * Check whether ioprio has changed. The condition may trigger
3724 * spuriously on a newly created cic but there's no harm.
3726 if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
3727 return;
3729 cfqq = cic_to_cfqq(cic, false);
3730 if (cfqq) {
3731 cfq_put_queue(cfqq);
3732 cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio);
3733 cic_set_cfqq(cic, cfqq, false);
3736 cfqq = cic_to_cfqq(cic, true);
3737 if (cfqq)
3738 cfq_mark_cfqq_prio_changed(cfqq);
3740 cic->ioprio = ioprio;
3743 static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3744 pid_t pid, bool is_sync)
3746 RB_CLEAR_NODE(&cfqq->rb_node);
3747 RB_CLEAR_NODE(&cfqq->p_node);
3748 INIT_LIST_HEAD(&cfqq->fifo);
3750 cfqq->ref = 0;
3751 cfqq->cfqd = cfqd;
3753 cfq_mark_cfqq_prio_changed(cfqq);
3755 if (is_sync) {
3756 if (!cfq_class_idle(cfqq))
3757 cfq_mark_cfqq_idle_window(cfqq);
3758 cfq_mark_cfqq_sync(cfqq);
3760 cfqq->pid = pid;
3763 #ifdef CONFIG_CFQ_GROUP_IOSCHED
3764 static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
3766 struct cfq_data *cfqd = cic_to_cfqd(cic);
3767 struct cfq_queue *cfqq;
3768 uint64_t serial_nr;
3770 rcu_read_lock();
3771 serial_nr = bio_blkcg(bio)->css.serial_nr;
3772 rcu_read_unlock();
3775 * Check whether blkcg has changed. The condition may trigger
3776 * spuriously on a newly created cic but there's no harm.
3778 if (unlikely(!cfqd) || likely(cic->blkcg_serial_nr == serial_nr))
3779 return;
3782 * Drop reference to queues. New queues will be assigned in new
3783 * group upon arrival of fresh requests.
3785 cfqq = cic_to_cfqq(cic, false);
3786 if (cfqq) {
3787 cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
3788 cic_set_cfqq(cic, NULL, false);
3789 cfq_put_queue(cfqq);
3792 cfqq = cic_to_cfqq(cic, true);
3793 if (cfqq) {
3794 cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
3795 cic_set_cfqq(cic, NULL, true);
3796 cfq_put_queue(cfqq);
3799 cic->blkcg_serial_nr = serial_nr;
3801 #else
3802 static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
3805 #endif /* CONFIG_CFQ_GROUP_IOSCHED */
3807 static struct cfq_queue **
3808 cfq_async_queue_prio(struct cfq_group *cfqg, int ioprio_class, int ioprio)
3810 switch (ioprio_class) {
3811 case IOPRIO_CLASS_RT:
3812 return &cfqg->async_cfqq[0][ioprio];
3813 case IOPRIO_CLASS_NONE:
3814 ioprio = IOPRIO_NORM;
3815 /* fall through */
3816 case IOPRIO_CLASS_BE:
3817 return &cfqg->async_cfqq[1][ioprio];
3818 case IOPRIO_CLASS_IDLE:
3819 return &cfqg->async_idle_cfqq;
3820 default:
3821 BUG();
3825 static struct cfq_queue *
3826 cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
3827 struct bio *bio)
3829 int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
3830 int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
3831 struct cfq_queue **async_cfqq = NULL;
3832 struct cfq_queue *cfqq;
3833 struct cfq_group *cfqg;
3835 rcu_read_lock();
3836 cfqg = cfq_lookup_cfqg(cfqd, bio_blkcg(bio));
3837 if (!cfqg) {
3838 cfqq = &cfqd->oom_cfqq;
3839 goto out;
3842 if (!is_sync) {
3843 if (!ioprio_valid(cic->ioprio)) {
3844 struct task_struct *tsk = current;
3845 ioprio = task_nice_ioprio(tsk);
3846 ioprio_class = task_nice_ioclass(tsk);
3848 async_cfqq = cfq_async_queue_prio(cfqg, ioprio_class, ioprio);
3849 cfqq = *async_cfqq;
3850 if (cfqq)
3851 goto out;
3854 cfqq = kmem_cache_alloc_node(cfq_pool,
3855 GFP_NOWAIT | __GFP_ZERO | __GFP_NOWARN,
3856 cfqd->queue->node);
3857 if (!cfqq) {
3858 cfqq = &cfqd->oom_cfqq;
3859 goto out;
3862 /* cfq_init_cfqq() assumes cfqq->ioprio_class is initialized. */
3863 cfqq->ioprio_class = IOPRIO_CLASS_NONE;
3864 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
3865 cfq_init_prio_data(cfqq, cic);
3866 cfq_link_cfqq_cfqg(cfqq, cfqg);
3867 cfq_log_cfqq(cfqd, cfqq, "alloced");
3869 if (async_cfqq) {
3870 /* a new async queue is created, pin and remember */
3871 cfqq->ref++;
3872 *async_cfqq = cfqq;
3874 out:
3875 cfqq->ref++;
3876 rcu_read_unlock();
3877 return cfqq;
3880 static void
3881 __cfq_update_io_thinktime(struct cfq_ttime *ttime, u64 slice_idle)
3883 u64 elapsed = ktime_get_ns() - ttime->last_end_request;
3884 elapsed = min(elapsed, 2UL * slice_idle);
3886 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
3887 ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed, 8);
3888 ttime->ttime_mean = div64_ul(ttime->ttime_total + 128,
3889 ttime->ttime_samples);
3892 static void
3893 cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3894 struct cfq_io_cq *cic)
3896 if (cfq_cfqq_sync(cfqq)) {
3897 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
3898 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
3899 cfqd->cfq_slice_idle);
3901 #ifdef CONFIG_CFQ_GROUP_IOSCHED
3902 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
3903 #endif
3906 static void
3907 cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3908 struct request *rq)
3910 sector_t sdist = 0;
3911 sector_t n_sec = blk_rq_sectors(rq);
3912 if (cfqq->last_request_pos) {
3913 if (cfqq->last_request_pos < blk_rq_pos(rq))
3914 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
3915 else
3916 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
3919 cfqq->seek_history <<= 1;
3920 if (blk_queue_nonrot(cfqd->queue))
3921 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
3922 else
3923 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
3926 static inline bool req_noidle(struct request *req)
3928 return req_op(req) == REQ_OP_WRITE &&
3929 (req->cmd_flags & (REQ_SYNC | REQ_IDLE)) == REQ_SYNC;
3933 * Disable idle window if the process thinks too long or seeks so much that
3934 * it doesn't matter
3936 static void
3937 cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3938 struct cfq_io_cq *cic)
3940 int old_idle, enable_idle;
3943 * Don't idle for async or idle io prio class
3945 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
3946 return;
3948 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
3950 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3951 cfq_mark_cfqq_deep(cfqq);
3953 if (cfqq->next_rq && req_noidle(cfqq->next_rq))
3954 enable_idle = 0;
3955 else if (!atomic_read(&cic->icq.ioc->active_ref) ||
3956 !cfqd->cfq_slice_idle ||
3957 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
3958 enable_idle = 0;
3959 else if (sample_valid(cic->ttime.ttime_samples)) {
3960 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
3961 enable_idle = 0;
3962 else
3963 enable_idle = 1;
3966 if (old_idle != enable_idle) {
3967 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
3968 if (enable_idle)
3969 cfq_mark_cfqq_idle_window(cfqq);
3970 else
3971 cfq_clear_cfqq_idle_window(cfqq);
3976 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3977 * no or if we aren't sure, a 1 will cause a preempt.
3979 static bool
3980 cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
3981 struct request *rq)
3983 struct cfq_queue *cfqq;
3985 cfqq = cfqd->active_queue;
3986 if (!cfqq)
3987 return false;
3989 if (cfq_class_idle(new_cfqq))
3990 return false;
3992 if (cfq_class_idle(cfqq))
3993 return true;
3996 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3998 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
3999 return false;
4002 * if the new request is sync, but the currently running queue is
4003 * not, let the sync request have priority.
4005 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
4006 return true;
4009 * Treat ancestors of current cgroup the same way as current cgroup.
4010 * For anybody else we disallow preemption to guarantee service
4011 * fairness among cgroups.
4013 if (!cfqg_is_descendant(cfqq->cfqg, new_cfqq->cfqg))
4014 return false;
4016 if (cfq_slice_used(cfqq))
4017 return true;
4020 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
4022 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
4023 return true;
4025 WARN_ON_ONCE(cfqq->ioprio_class != new_cfqq->ioprio_class);
4026 /* Allow preemption only if we are idling on sync-noidle tree */
4027 if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
4028 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
4029 RB_EMPTY_ROOT(&cfqq->sort_list))
4030 return true;
4033 * So both queues are sync. Let the new request get disk time if
4034 * it's a metadata request and the current queue is doing regular IO.
4036 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
4037 return true;
4039 /* An idle queue should not be idle now for some reason */
4040 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
4041 return true;
4043 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
4044 return false;
4047 * if this request is as-good as one we would expect from the
4048 * current cfqq, let it preempt
4050 if (cfq_rq_close(cfqd, cfqq, rq))
4051 return true;
4053 return false;
4057 * cfqq preempts the active queue. if we allowed preempt with no slice left,
4058 * let it have half of its nominal slice.
4060 static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
4062 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
4064 cfq_log_cfqq(cfqd, cfqq, "preempt");
4065 cfq_slice_expired(cfqd, 1);
4068 * workload type is changed, don't save slice, otherwise preempt
4069 * doesn't happen
4071 if (old_type != cfqq_type(cfqq))
4072 cfqq->cfqg->saved_wl_slice = 0;
4075 * Put the new queue at the front of the of the current list,
4076 * so we know that it will be selected next.
4078 BUG_ON(!cfq_cfqq_on_rr(cfqq));
4080 cfq_service_tree_add(cfqd, cfqq, 1);
4082 cfqq->slice_end = 0;
4083 cfq_mark_cfqq_slice_new(cfqq);
4087 * Called when a new fs request (rq) is added (to cfqq). Check if there's
4088 * something we should do about it
4090 static void
4091 cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
4092 struct request *rq)
4094 struct cfq_io_cq *cic = RQ_CIC(rq);
4096 cfqd->rq_queued++;
4097 if (rq->cmd_flags & REQ_PRIO)
4098 cfqq->prio_pending++;
4100 cfq_update_io_thinktime(cfqd, cfqq, cic);
4101 cfq_update_io_seektime(cfqd, cfqq, rq);
4102 cfq_update_idle_window(cfqd, cfqq, cic);
4104 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
4106 if (cfqq == cfqd->active_queue) {
4108 * Remember that we saw a request from this process, but
4109 * don't start queuing just yet. Otherwise we risk seeing lots
4110 * of tiny requests, because we disrupt the normal plugging
4111 * and merging. If the request is already larger than a single
4112 * page, let it rip immediately. For that case we assume that
4113 * merging is already done. Ditto for a busy system that
4114 * has other work pending, don't risk delaying until the
4115 * idle timer unplug to continue working.
4117 if (cfq_cfqq_wait_request(cfqq)) {
4118 if (blk_rq_bytes(rq) > PAGE_SIZE ||
4119 cfqd->busy_queues > 1) {
4120 cfq_del_timer(cfqd, cfqq);
4121 cfq_clear_cfqq_wait_request(cfqq);
4122 __blk_run_queue(cfqd->queue);
4123 } else {
4124 cfqg_stats_update_idle_time(cfqq->cfqg);
4125 cfq_mark_cfqq_must_dispatch(cfqq);
4128 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
4130 * not the active queue - expire current slice if it is
4131 * idle and has expired it's mean thinktime or this new queue
4132 * has some old slice time left and is of higher priority or
4133 * this new queue is RT and the current one is BE
4135 cfq_preempt_queue(cfqd, cfqq);
4136 __blk_run_queue(cfqd->queue);
4140 static void cfq_insert_request(struct request_queue *q, struct request *rq)
4142 struct cfq_data *cfqd = q->elevator->elevator_data;
4143 struct cfq_queue *cfqq = RQ_CFQQ(rq);
4145 cfq_log_cfqq(cfqd, cfqq, "insert_request");
4146 cfq_init_prio_data(cfqq, RQ_CIC(rq));
4148 rq->fifo_time = ktime_get_ns() + cfqd->cfq_fifo_expire[rq_is_sync(rq)];
4149 list_add_tail(&rq->queuelist, &cfqq->fifo);
4150 cfq_add_rq_rb(rq);
4151 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group,
4152 rq->cmd_flags);
4153 cfq_rq_enqueued(cfqd, cfqq, rq);
4157 * Update hw_tag based on peak queue depth over 50 samples under
4158 * sufficient load.
4160 static void cfq_update_hw_tag(struct cfq_data *cfqd)
4162 struct cfq_queue *cfqq = cfqd->active_queue;
4164 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
4165 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
4167 if (cfqd->hw_tag == 1)
4168 return;
4170 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
4171 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
4172 return;
4175 * If active queue hasn't enough requests and can idle, cfq might not
4176 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
4177 * case
4179 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
4180 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
4181 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
4182 return;
4184 if (cfqd->hw_tag_samples++ < 50)
4185 return;
4187 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
4188 cfqd->hw_tag = 1;
4189 else
4190 cfqd->hw_tag = 0;
4193 static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
4195 struct cfq_io_cq *cic = cfqd->active_cic;
4196 u64 now = ktime_get_ns();
4198 /* If the queue already has requests, don't wait */
4199 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
4200 return false;
4202 /* If there are other queues in the group, don't wait */
4203 if (cfqq->cfqg->nr_cfqq > 1)
4204 return false;
4206 /* the only queue in the group, but think time is big */
4207 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
4208 return false;
4210 if (cfq_slice_used(cfqq))
4211 return true;
4213 /* if slice left is less than think time, wait busy */
4214 if (cic && sample_valid(cic->ttime.ttime_samples)
4215 && (cfqq->slice_end - now < cic->ttime.ttime_mean))
4216 return true;
4219 * If think times is less than a jiffy than ttime_mean=0 and above
4220 * will not be true. It might happen that slice has not expired yet
4221 * but will expire soon (4-5 ns) during select_queue(). To cover the
4222 * case where think time is less than a jiffy, mark the queue wait
4223 * busy if only 1 jiffy is left in the slice.
4225 if (cfqq->slice_end - now <= jiffies_to_nsecs(1))
4226 return true;
4228 return false;
4231 static void cfq_completed_request(struct request_queue *q, struct request *rq)
4233 struct cfq_queue *cfqq = RQ_CFQQ(rq);
4234 struct cfq_data *cfqd = cfqq->cfqd;
4235 const int sync = rq_is_sync(rq);
4236 u64 now = ktime_get_ns();
4238 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", req_noidle(rq));
4240 cfq_update_hw_tag(cfqd);
4242 WARN_ON(!cfqd->rq_in_driver);
4243 WARN_ON(!cfqq->dispatched);
4244 cfqd->rq_in_driver--;
4245 cfqq->dispatched--;
4246 (RQ_CFQG(rq))->dispatched--;
4247 cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq),
4248 rq_io_start_time_ns(rq), rq->cmd_flags);
4250 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
4252 if (sync) {
4253 struct cfq_rb_root *st;
4255 RQ_CIC(rq)->ttime.last_end_request = now;
4257 if (cfq_cfqq_on_rr(cfqq))
4258 st = cfqq->service_tree;
4259 else
4260 st = st_for(cfqq->cfqg, cfqq_class(cfqq),
4261 cfqq_type(cfqq));
4263 st->ttime.last_end_request = now;
4265 * We have to do this check in jiffies since start_time is in
4266 * jiffies and it is not trivial to convert to ns. If
4267 * cfq_fifo_expire[1] ever comes close to 1 jiffie, this test
4268 * will become problematic but so far we are fine (the default
4269 * is 128 ms).
4271 if (!time_after(rq->start_time +
4272 nsecs_to_jiffies(cfqd->cfq_fifo_expire[1]),
4273 jiffies))
4274 cfqd->last_delayed_sync = now;
4277 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4278 cfqq->cfqg->ttime.last_end_request = now;
4279 #endif
4282 * If this is the active queue, check if it needs to be expired,
4283 * or if we want to idle in case it has no pending requests.
4285 if (cfqd->active_queue == cfqq) {
4286 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
4288 if (cfq_cfqq_slice_new(cfqq)) {
4289 cfq_set_prio_slice(cfqd, cfqq);
4290 cfq_clear_cfqq_slice_new(cfqq);
4294 * Should we wait for next request to come in before we expire
4295 * the queue.
4297 if (cfq_should_wait_busy(cfqd, cfqq)) {
4298 u64 extend_sl = cfqd->cfq_slice_idle;
4299 if (!cfqd->cfq_slice_idle)
4300 extend_sl = cfqd->cfq_group_idle;
4301 cfqq->slice_end = now + extend_sl;
4302 cfq_mark_cfqq_wait_busy(cfqq);
4303 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
4307 * Idling is not enabled on:
4308 * - expired queues
4309 * - idle-priority queues
4310 * - async queues
4311 * - queues with still some requests queued
4312 * - when there is a close cooperator
4314 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
4315 cfq_slice_expired(cfqd, 1);
4316 else if (sync && cfqq_empty &&
4317 !cfq_close_cooperator(cfqd, cfqq)) {
4318 cfq_arm_slice_timer(cfqd);
4322 if (!cfqd->rq_in_driver)
4323 cfq_schedule_dispatch(cfqd);
4326 static void cfqq_boost_on_prio(struct cfq_queue *cfqq, unsigned int op)
4329 * If REQ_PRIO is set, boost class and prio level, if it's below
4330 * BE/NORM. If prio is not set, restore the potentially boosted
4331 * class/prio level.
4333 if (!(op & REQ_PRIO)) {
4334 cfqq->ioprio_class = cfqq->org_ioprio_class;
4335 cfqq->ioprio = cfqq->org_ioprio;
4336 } else {
4337 if (cfq_class_idle(cfqq))
4338 cfqq->ioprio_class = IOPRIO_CLASS_BE;
4339 if (cfqq->ioprio > IOPRIO_NORM)
4340 cfqq->ioprio = IOPRIO_NORM;
4344 static inline int __cfq_may_queue(struct cfq_queue *cfqq)
4346 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
4347 cfq_mark_cfqq_must_alloc_slice(cfqq);
4348 return ELV_MQUEUE_MUST;
4351 return ELV_MQUEUE_MAY;
4354 static int cfq_may_queue(struct request_queue *q, unsigned int op)
4356 struct cfq_data *cfqd = q->elevator->elevator_data;
4357 struct task_struct *tsk = current;
4358 struct cfq_io_cq *cic;
4359 struct cfq_queue *cfqq;
4362 * don't force setup of a queue from here, as a call to may_queue
4363 * does not necessarily imply that a request actually will be queued.
4364 * so just lookup a possibly existing queue, or return 'may queue'
4365 * if that fails
4367 cic = cfq_cic_lookup(cfqd, tsk->io_context);
4368 if (!cic)
4369 return ELV_MQUEUE_MAY;
4371 cfqq = cic_to_cfqq(cic, op_is_sync(op));
4372 if (cfqq) {
4373 cfq_init_prio_data(cfqq, cic);
4374 cfqq_boost_on_prio(cfqq, op);
4376 return __cfq_may_queue(cfqq);
4379 return ELV_MQUEUE_MAY;
4383 * queue lock held here
4385 static void cfq_put_request(struct request *rq)
4387 struct cfq_queue *cfqq = RQ_CFQQ(rq);
4389 if (cfqq) {
4390 const int rw = rq_data_dir(rq);
4392 BUG_ON(!cfqq->allocated[rw]);
4393 cfqq->allocated[rw]--;
4395 /* Put down rq reference on cfqg */
4396 cfqg_put(RQ_CFQG(rq));
4397 rq->elv.priv[0] = NULL;
4398 rq->elv.priv[1] = NULL;
4400 cfq_put_queue(cfqq);
4404 static struct cfq_queue *
4405 cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
4406 struct cfq_queue *cfqq)
4408 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
4409 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
4410 cfq_mark_cfqq_coop(cfqq->new_cfqq);
4411 cfq_put_queue(cfqq);
4412 return cic_to_cfqq(cic, 1);
4416 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
4417 * was the last process referring to said cfqq.
4419 static struct cfq_queue *
4420 split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
4422 if (cfqq_process_refs(cfqq) == 1) {
4423 cfqq->pid = current->pid;
4424 cfq_clear_cfqq_coop(cfqq);
4425 cfq_clear_cfqq_split_coop(cfqq);
4426 return cfqq;
4429 cic_set_cfqq(cic, NULL, 1);
4431 cfq_put_cooperator(cfqq);
4433 cfq_put_queue(cfqq);
4434 return NULL;
4437 * Allocate cfq data structures associated with this request.
4439 static int
4440 cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
4441 gfp_t gfp_mask)
4443 struct cfq_data *cfqd = q->elevator->elevator_data;
4444 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
4445 const int rw = rq_data_dir(rq);
4446 const bool is_sync = rq_is_sync(rq);
4447 struct cfq_queue *cfqq;
4449 spin_lock_irq(q->queue_lock);
4451 check_ioprio_changed(cic, bio);
4452 check_blkcg_changed(cic, bio);
4453 new_queue:
4454 cfqq = cic_to_cfqq(cic, is_sync);
4455 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
4456 if (cfqq)
4457 cfq_put_queue(cfqq);
4458 cfqq = cfq_get_queue(cfqd, is_sync, cic, bio);
4459 cic_set_cfqq(cic, cfqq, is_sync);
4460 } else {
4462 * If the queue was seeky for too long, break it apart.
4464 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
4465 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
4466 cfqq = split_cfqq(cic, cfqq);
4467 if (!cfqq)
4468 goto new_queue;
4472 * Check to see if this queue is scheduled to merge with
4473 * another, closely cooperating queue. The merging of
4474 * queues happens here as it must be done in process context.
4475 * The reference on new_cfqq was taken in merge_cfqqs.
4477 if (cfqq->new_cfqq)
4478 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
4481 cfqq->allocated[rw]++;
4483 cfqq->ref++;
4484 cfqg_get(cfqq->cfqg);
4485 rq->elv.priv[0] = cfqq;
4486 rq->elv.priv[1] = cfqq->cfqg;
4487 spin_unlock_irq(q->queue_lock);
4489 return 0;
4492 static void cfq_kick_queue(struct work_struct *work)
4494 struct cfq_data *cfqd =
4495 container_of(work, struct cfq_data, unplug_work);
4496 struct request_queue *q = cfqd->queue;
4498 spin_lock_irq(q->queue_lock);
4499 __blk_run_queue(cfqd->queue);
4500 spin_unlock_irq(q->queue_lock);
4504 * Timer running if the active_queue is currently idling inside its time slice
4506 static enum hrtimer_restart cfq_idle_slice_timer(struct hrtimer *timer)
4508 struct cfq_data *cfqd = container_of(timer, struct cfq_data,
4509 idle_slice_timer);
4510 struct cfq_queue *cfqq;
4511 unsigned long flags;
4512 int timed_out = 1;
4514 cfq_log(cfqd, "idle timer fired");
4516 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
4518 cfqq = cfqd->active_queue;
4519 if (cfqq) {
4520 timed_out = 0;
4523 * We saw a request before the queue expired, let it through
4525 if (cfq_cfqq_must_dispatch(cfqq))
4526 goto out_kick;
4529 * expired
4531 if (cfq_slice_used(cfqq))
4532 goto expire;
4535 * only expire and reinvoke request handler, if there are
4536 * other queues with pending requests
4538 if (!cfqd->busy_queues)
4539 goto out_cont;
4542 * not expired and it has a request pending, let it dispatch
4544 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
4545 goto out_kick;
4548 * Queue depth flag is reset only when the idle didn't succeed
4550 cfq_clear_cfqq_deep(cfqq);
4552 expire:
4553 cfq_slice_expired(cfqd, timed_out);
4554 out_kick:
4555 cfq_schedule_dispatch(cfqd);
4556 out_cont:
4557 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
4558 return HRTIMER_NORESTART;
4561 static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
4563 hrtimer_cancel(&cfqd->idle_slice_timer);
4564 cancel_work_sync(&cfqd->unplug_work);
4567 static void cfq_exit_queue(struct elevator_queue *e)
4569 struct cfq_data *cfqd = e->elevator_data;
4570 struct request_queue *q = cfqd->queue;
4572 cfq_shutdown_timer_wq(cfqd);
4574 spin_lock_irq(q->queue_lock);
4576 if (cfqd->active_queue)
4577 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
4579 spin_unlock_irq(q->queue_lock);
4581 cfq_shutdown_timer_wq(cfqd);
4583 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4584 blkcg_deactivate_policy(q, &blkcg_policy_cfq);
4585 #else
4586 kfree(cfqd->root_group);
4587 #endif
4588 kfree(cfqd);
4591 static int cfq_init_queue(struct request_queue *q, struct elevator_type *e)
4593 struct cfq_data *cfqd;
4594 struct blkcg_gq *blkg __maybe_unused;
4595 int i, ret;
4596 struct elevator_queue *eq;
4598 eq = elevator_alloc(q, e);
4599 if (!eq)
4600 return -ENOMEM;
4602 cfqd = kzalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
4603 if (!cfqd) {
4604 kobject_put(&eq->kobj);
4605 return -ENOMEM;
4607 eq->elevator_data = cfqd;
4609 cfqd->queue = q;
4610 spin_lock_irq(q->queue_lock);
4611 q->elevator = eq;
4612 spin_unlock_irq(q->queue_lock);
4614 /* Init root service tree */
4615 cfqd->grp_service_tree = CFQ_RB_ROOT;
4617 /* Init root group and prefer root group over other groups by default */
4618 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4619 ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
4620 if (ret)
4621 goto out_free;
4623 cfqd->root_group = blkg_to_cfqg(q->root_blkg);
4624 #else
4625 ret = -ENOMEM;
4626 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
4627 GFP_KERNEL, cfqd->queue->node);
4628 if (!cfqd->root_group)
4629 goto out_free;
4631 cfq_init_cfqg_base(cfqd->root_group);
4632 cfqd->root_group->weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
4633 cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
4634 #endif
4637 * Not strictly needed (since RB_ROOT just clears the node and we
4638 * zeroed cfqd on alloc), but better be safe in case someone decides
4639 * to add magic to the rb code
4641 for (i = 0; i < CFQ_PRIO_LISTS; i++)
4642 cfqd->prio_trees[i] = RB_ROOT;
4645 * Our fallback cfqq if cfq_get_queue() runs into OOM issues.
4646 * Grab a permanent reference to it, so that the normal code flow
4647 * will not attempt to free it. oom_cfqq is linked to root_group
4648 * but shouldn't hold a reference as it'll never be unlinked. Lose
4649 * the reference from linking right away.
4651 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
4652 cfqd->oom_cfqq.ref++;
4654 spin_lock_irq(q->queue_lock);
4655 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
4656 cfqg_put(cfqd->root_group);
4657 spin_unlock_irq(q->queue_lock);
4659 hrtimer_init(&cfqd->idle_slice_timer, CLOCK_MONOTONIC,
4660 HRTIMER_MODE_REL);
4661 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
4663 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
4665 cfqd->cfq_quantum = cfq_quantum;
4666 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
4667 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
4668 cfqd->cfq_back_max = cfq_back_max;
4669 cfqd->cfq_back_penalty = cfq_back_penalty;
4670 cfqd->cfq_slice[0] = cfq_slice_async;
4671 cfqd->cfq_slice[1] = cfq_slice_sync;
4672 cfqd->cfq_target_latency = cfq_target_latency;
4673 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
4674 cfqd->cfq_slice_idle = cfq_slice_idle;
4675 cfqd->cfq_group_idle = cfq_group_idle;
4676 cfqd->cfq_latency = 1;
4677 cfqd->hw_tag = -1;
4679 * we optimistically start assuming sync ops weren't delayed in last
4680 * second, in order to have larger depth for async operations.
4682 cfqd->last_delayed_sync = ktime_get_ns() - NSEC_PER_SEC;
4683 return 0;
4685 out_free:
4686 kfree(cfqd);
4687 kobject_put(&eq->kobj);
4688 return ret;
4691 static void cfq_registered_queue(struct request_queue *q)
4693 struct elevator_queue *e = q->elevator;
4694 struct cfq_data *cfqd = e->elevator_data;
4697 * Default to IOPS mode with no idling for SSDs
4699 if (blk_queue_nonrot(q))
4700 cfqd->cfq_slice_idle = 0;
4701 wbt_disable_default(q);
4705 * sysfs parts below -->
4707 static ssize_t
4708 cfq_var_show(unsigned int var, char *page)
4710 return sprintf(page, "%u\n", var);
4713 static ssize_t
4714 cfq_var_store(unsigned int *var, const char *page, size_t count)
4716 char *p = (char *) page;
4718 *var = simple_strtoul(p, &p, 10);
4719 return count;
4722 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
4723 static ssize_t __FUNC(struct elevator_queue *e, char *page) \
4725 struct cfq_data *cfqd = e->elevator_data; \
4726 u64 __data = __VAR; \
4727 if (__CONV) \
4728 __data = div_u64(__data, NSEC_PER_MSEC); \
4729 return cfq_var_show(__data, (page)); \
4731 SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
4732 SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
4733 SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
4734 SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
4735 SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
4736 SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
4737 SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
4738 SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
4739 SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
4740 SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
4741 SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
4742 SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1);
4743 #undef SHOW_FUNCTION
4745 #define USEC_SHOW_FUNCTION(__FUNC, __VAR) \
4746 static ssize_t __FUNC(struct elevator_queue *e, char *page) \
4748 struct cfq_data *cfqd = e->elevator_data; \
4749 u64 __data = __VAR; \
4750 __data = div_u64(__data, NSEC_PER_USEC); \
4751 return cfq_var_show(__data, (page)); \
4753 USEC_SHOW_FUNCTION(cfq_slice_idle_us_show, cfqd->cfq_slice_idle);
4754 USEC_SHOW_FUNCTION(cfq_group_idle_us_show, cfqd->cfq_group_idle);
4755 USEC_SHOW_FUNCTION(cfq_slice_sync_us_show, cfqd->cfq_slice[1]);
4756 USEC_SHOW_FUNCTION(cfq_slice_async_us_show, cfqd->cfq_slice[0]);
4757 USEC_SHOW_FUNCTION(cfq_target_latency_us_show, cfqd->cfq_target_latency);
4758 #undef USEC_SHOW_FUNCTION
4760 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
4761 static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
4763 struct cfq_data *cfqd = e->elevator_data; \
4764 unsigned int __data; \
4765 int ret = cfq_var_store(&__data, (page), count); \
4766 if (__data < (MIN)) \
4767 __data = (MIN); \
4768 else if (__data > (MAX)) \
4769 __data = (MAX); \
4770 if (__CONV) \
4771 *(__PTR) = (u64)__data * NSEC_PER_MSEC; \
4772 else \
4773 *(__PTR) = __data; \
4774 return ret; \
4776 STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
4777 STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
4778 UINT_MAX, 1);
4779 STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
4780 UINT_MAX, 1);
4781 STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
4782 STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
4783 UINT_MAX, 0);
4784 STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
4785 STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
4786 STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
4787 STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
4788 STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
4789 UINT_MAX, 0);
4790 STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
4791 STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1);
4792 #undef STORE_FUNCTION
4794 #define USEC_STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
4795 static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
4797 struct cfq_data *cfqd = e->elevator_data; \
4798 unsigned int __data; \
4799 int ret = cfq_var_store(&__data, (page), count); \
4800 if (__data < (MIN)) \
4801 __data = (MIN); \
4802 else if (__data > (MAX)) \
4803 __data = (MAX); \
4804 *(__PTR) = (u64)__data * NSEC_PER_USEC; \
4805 return ret; \
4807 USEC_STORE_FUNCTION(cfq_slice_idle_us_store, &cfqd->cfq_slice_idle, 0, UINT_MAX);
4808 USEC_STORE_FUNCTION(cfq_group_idle_us_store, &cfqd->cfq_group_idle, 0, UINT_MAX);
4809 USEC_STORE_FUNCTION(cfq_slice_sync_us_store, &cfqd->cfq_slice[1], 1, UINT_MAX);
4810 USEC_STORE_FUNCTION(cfq_slice_async_us_store, &cfqd->cfq_slice[0], 1, UINT_MAX);
4811 USEC_STORE_FUNCTION(cfq_target_latency_us_store, &cfqd->cfq_target_latency, 1, UINT_MAX);
4812 #undef USEC_STORE_FUNCTION
4814 #define CFQ_ATTR(name) \
4815 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
4817 static struct elv_fs_entry cfq_attrs[] = {
4818 CFQ_ATTR(quantum),
4819 CFQ_ATTR(fifo_expire_sync),
4820 CFQ_ATTR(fifo_expire_async),
4821 CFQ_ATTR(back_seek_max),
4822 CFQ_ATTR(back_seek_penalty),
4823 CFQ_ATTR(slice_sync),
4824 CFQ_ATTR(slice_sync_us),
4825 CFQ_ATTR(slice_async),
4826 CFQ_ATTR(slice_async_us),
4827 CFQ_ATTR(slice_async_rq),
4828 CFQ_ATTR(slice_idle),
4829 CFQ_ATTR(slice_idle_us),
4830 CFQ_ATTR(group_idle),
4831 CFQ_ATTR(group_idle_us),
4832 CFQ_ATTR(low_latency),
4833 CFQ_ATTR(target_latency),
4834 CFQ_ATTR(target_latency_us),
4835 __ATTR_NULL
4838 static struct elevator_type iosched_cfq = {
4839 .ops.sq = {
4840 .elevator_merge_fn = cfq_merge,
4841 .elevator_merged_fn = cfq_merged_request,
4842 .elevator_merge_req_fn = cfq_merged_requests,
4843 .elevator_allow_bio_merge_fn = cfq_allow_bio_merge,
4844 .elevator_allow_rq_merge_fn = cfq_allow_rq_merge,
4845 .elevator_bio_merged_fn = cfq_bio_merged,
4846 .elevator_dispatch_fn = cfq_dispatch_requests,
4847 .elevator_add_req_fn = cfq_insert_request,
4848 .elevator_activate_req_fn = cfq_activate_request,
4849 .elevator_deactivate_req_fn = cfq_deactivate_request,
4850 .elevator_completed_req_fn = cfq_completed_request,
4851 .elevator_former_req_fn = elv_rb_former_request,
4852 .elevator_latter_req_fn = elv_rb_latter_request,
4853 .elevator_init_icq_fn = cfq_init_icq,
4854 .elevator_exit_icq_fn = cfq_exit_icq,
4855 .elevator_set_req_fn = cfq_set_request,
4856 .elevator_put_req_fn = cfq_put_request,
4857 .elevator_may_queue_fn = cfq_may_queue,
4858 .elevator_init_fn = cfq_init_queue,
4859 .elevator_exit_fn = cfq_exit_queue,
4860 .elevator_registered_fn = cfq_registered_queue,
4862 .icq_size = sizeof(struct cfq_io_cq),
4863 .icq_align = __alignof__(struct cfq_io_cq),
4864 .elevator_attrs = cfq_attrs,
4865 .elevator_name = "cfq",
4866 .elevator_owner = THIS_MODULE,
4869 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4870 static struct blkcg_policy blkcg_policy_cfq = {
4871 .dfl_cftypes = cfq_blkcg_files,
4872 .legacy_cftypes = cfq_blkcg_legacy_files,
4874 .cpd_alloc_fn = cfq_cpd_alloc,
4875 .cpd_init_fn = cfq_cpd_init,
4876 .cpd_free_fn = cfq_cpd_free,
4877 .cpd_bind_fn = cfq_cpd_bind,
4879 .pd_alloc_fn = cfq_pd_alloc,
4880 .pd_init_fn = cfq_pd_init,
4881 .pd_offline_fn = cfq_pd_offline,
4882 .pd_free_fn = cfq_pd_free,
4883 .pd_reset_stats_fn = cfq_pd_reset_stats,
4885 #endif
4887 static int __init cfq_init(void)
4889 int ret;
4891 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4892 ret = blkcg_policy_register(&blkcg_policy_cfq);
4893 if (ret)
4894 return ret;
4895 #else
4896 cfq_group_idle = 0;
4897 #endif
4899 ret = -ENOMEM;
4900 cfq_pool = KMEM_CACHE(cfq_queue, 0);
4901 if (!cfq_pool)
4902 goto err_pol_unreg;
4904 ret = elv_register(&iosched_cfq);
4905 if (ret)
4906 goto err_free_pool;
4908 return 0;
4910 err_free_pool:
4911 kmem_cache_destroy(cfq_pool);
4912 err_pol_unreg:
4913 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4914 blkcg_policy_unregister(&blkcg_policy_cfq);
4915 #endif
4916 return ret;
4919 static void __exit cfq_exit(void)
4921 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4922 blkcg_policy_unregister(&blkcg_policy_cfq);
4923 #endif
4924 elv_unregister(&iosched_cfq);
4925 kmem_cache_destroy(cfq_pool);
4928 module_init(cfq_init);
4929 module_exit(cfq_exit);
4931 MODULE_AUTHOR("Jens Axboe");
4932 MODULE_LICENSE("GPL");
4933 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");