2 * CFQ, or complete fairness queueing, disk scheduler.
4 * Based on ideas from a previously unfinished io
5 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
7 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/blkdev.h>
12 #include <linux/elevator.h>
13 #include <linux/jiffies.h>
14 #include <linux/rbtree.h>
15 #include <linux/ioprio.h>
16 #include <linux/blktrace_api.h>
18 #include "blk-cgroup.h"
23 /* max queue in one round of service */
24 static const int cfq_quantum
= 8;
25 static const int cfq_fifo_expire
[2] = { HZ
/ 4, HZ
/ 8 };
26 /* maximum backwards seek, in KiB */
27 static const int cfq_back_max
= 16 * 1024;
28 /* penalty of a backwards seek */
29 static const int cfq_back_penalty
= 2;
30 static const int cfq_slice_sync
= HZ
/ 10;
31 static int cfq_slice_async
= HZ
/ 25;
32 static const int cfq_slice_async_rq
= 2;
33 static int cfq_slice_idle
= HZ
/ 125;
34 static int cfq_group_idle
= HZ
/ 125;
35 static const int cfq_target_latency
= HZ
* 3/10; /* 300 ms */
36 static const int cfq_hist_divisor
= 4;
39 * offset from end of service tree
41 #define CFQ_IDLE_DELAY (HZ / 5)
44 * below this threshold, we consider thinktime immediate
46 #define CFQ_MIN_TT (2)
48 #define CFQ_SLICE_SCALE (5)
49 #define CFQ_HW_QUEUE_MIN (5)
50 #define CFQ_SERVICE_SHIFT 12
52 #define CFQQ_SEEK_THR (sector_t)(8 * 100)
53 #define CFQQ_CLOSE_THR (sector_t)(8 * 1024)
54 #define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
55 #define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8)
57 #define RQ_CIC(rq) icq_to_cic((rq)->elv.icq)
58 #define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0])
59 #define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1])
61 static struct kmem_cache
*cfq_pool
;
63 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
64 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
65 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
67 #define sample_valid(samples) ((samples) > 80)
68 #define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
71 unsigned long last_end_request
;
73 unsigned long ttime_total
;
74 unsigned long ttime_samples
;
75 unsigned long ttime_mean
;
79 * Most of our rbtree usage is for sorting with min extraction, so
80 * if we cache the leftmost node we don't have to walk down the tree
81 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
82 * move this into the elevator for the rq sorting as well.
88 unsigned total_weight
;
90 struct cfq_ttime ttime
;
92 #define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
93 .ttime = {.last_end_request = jiffies,},}
96 * Per process-grouping structure
101 /* various state flags, see below */
103 /* parent cfq_data */
104 struct cfq_data
*cfqd
;
105 /* service_tree member */
106 struct rb_node rb_node
;
107 /* service_tree key */
108 unsigned long rb_key
;
109 /* prio tree member */
110 struct rb_node p_node
;
111 /* prio tree root we belong to, if any */
112 struct rb_root
*p_root
;
113 /* sorted list of pending requests */
114 struct rb_root sort_list
;
115 /* if fifo isn't expired, next request to serve */
116 struct request
*next_rq
;
117 /* requests queued in sort_list */
119 /* currently allocated requests */
121 /* fifo list of requests in sort_list */
122 struct list_head fifo
;
124 /* time when queue got scheduled in to dispatch first request. */
125 unsigned long dispatch_start
;
126 unsigned int allocated_slice
;
127 unsigned int slice_dispatch
;
128 /* time when first request from queue completed and slice started. */
129 unsigned long slice_start
;
130 unsigned long slice_end
;
133 /* pending priority requests */
135 /* number of requests that are on the dispatch list or inside driver */
138 /* io prio of this group */
139 unsigned short ioprio
, org_ioprio
;
140 unsigned short ioprio_class
;
145 sector_t last_request_pos
;
147 struct cfq_rb_root
*service_tree
;
148 struct cfq_queue
*new_cfqq
;
149 struct cfq_group
*cfqg
;
150 /* Number of sectors dispatched from queue in single dispatch round */
151 unsigned long nr_sectors
;
155 * First index in the service_trees.
156 * IDLE is handled separately, so it has negative index
166 * Second index in the service_trees.
170 SYNC_NOIDLE_WORKLOAD
= 1,
175 #ifdef CONFIG_CFQ_GROUP_IOSCHED
176 /* total bytes transferred */
177 struct blkg_rwstat service_bytes
;
178 /* total IOs serviced, post merge */
179 struct blkg_rwstat serviced
;
180 /* number of ios merged */
181 struct blkg_rwstat merged
;
182 /* total time spent on device in ns, may not be accurate w/ queueing */
183 struct blkg_rwstat service_time
;
184 /* total time spent waiting in scheduler queue in ns */
185 struct blkg_rwstat wait_time
;
186 /* number of IOs queued up */
187 struct blkg_rwstat queued
;
188 /* total sectors transferred */
189 struct blkg_stat sectors
;
190 /* total disk time and nr sectors dispatched by this group */
191 struct blkg_stat time
;
192 #ifdef CONFIG_DEBUG_BLK_CGROUP
193 /* time not charged to this cgroup */
194 struct blkg_stat unaccounted_time
;
195 /* sum of number of ios queued across all samples */
196 struct blkg_stat avg_queue_size_sum
;
197 /* count of samples taken for average */
198 struct blkg_stat avg_queue_size_samples
;
199 /* how many times this group has been removed from service tree */
200 struct blkg_stat dequeue
;
201 /* total time spent waiting for it to be assigned a timeslice. */
202 struct blkg_stat group_wait_time
;
203 /* time spent idling for this blkcg_gq */
204 struct blkg_stat idle_time
;
205 /* total time with empty current active q with other requests queued */
206 struct blkg_stat empty_time
;
207 /* fields after this shouldn't be cleared on stat reset */
208 uint64_t start_group_wait_time
;
209 uint64_t start_idle_time
;
210 uint64_t start_empty_time
;
212 #endif /* CONFIG_DEBUG_BLK_CGROUP */
213 #endif /* CONFIG_CFQ_GROUP_IOSCHED */
216 /* This is per cgroup per device grouping structure */
218 /* must be the first member */
219 struct blkg_policy_data pd
;
221 /* group service_tree member */
222 struct rb_node rb_node
;
224 /* group service_tree key */
228 * The number of active cfqgs and sum of their weights under this
229 * cfqg. This covers this cfqg's leaf_weight and all children's
230 * weights, but does not cover weights of further descendants.
232 * If a cfqg is on the service tree, it's active. An active cfqg
233 * also activates its parent and contributes to the children_weight
237 unsigned int children_weight
;
240 * There are two weights - (internal) weight is the weight of this
241 * cfqg against the sibling cfqgs. leaf_weight is the wight of
242 * this cfqg against the child cfqgs. For the root cfqg, both
243 * weights are kept in sync for backward compatibility.
246 unsigned int new_weight
;
247 unsigned int dev_weight
;
249 unsigned int leaf_weight
;
250 unsigned int new_leaf_weight
;
251 unsigned int dev_leaf_weight
;
253 /* number of cfqq currently on this group */
257 * Per group busy queues average. Useful for workload slice calc. We
258 * create the array for each prio class but at run time it is used
259 * only for RT and BE class and slot for IDLE class remains unused.
260 * This is primarily done to avoid confusion and a gcc warning.
262 unsigned int busy_queues_avg
[CFQ_PRIO_NR
];
264 * rr lists of queues with requests. We maintain service trees for
265 * RT and BE classes. These trees are subdivided in subclasses
266 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
267 * class there is no subclassification and all the cfq queues go on
268 * a single tree service_tree_idle.
269 * Counts are embedded in the cfq_rb_root
271 struct cfq_rb_root service_trees
[2][3];
272 struct cfq_rb_root service_tree_idle
;
274 unsigned long saved_wl_slice
;
275 enum wl_type_t saved_wl_type
;
276 enum wl_class_t saved_wl_class
;
278 /* number of requests that are on the dispatch list or inside driver */
280 struct cfq_ttime ttime
;
281 struct cfqg_stats stats
;
285 struct io_cq icq
; /* must be the first member */
286 struct cfq_queue
*cfqq
[2];
287 struct cfq_ttime ttime
;
288 int ioprio
; /* the current ioprio */
289 #ifdef CONFIG_CFQ_GROUP_IOSCHED
290 uint64_t blkcg_id
; /* the current blkcg ID */
295 * Per block device queue structure
298 struct request_queue
*queue
;
299 /* Root service tree for cfq_groups */
300 struct cfq_rb_root grp_service_tree
;
301 struct cfq_group
*root_group
;
304 * The priority currently being served
306 enum wl_class_t serving_wl_class
;
307 enum wl_type_t serving_wl_type
;
308 unsigned long workload_expires
;
309 struct cfq_group
*serving_group
;
312 * Each priority tree is sorted by next_request position. These
313 * trees are used when determining if two or more queues are
314 * interleaving requests (see cfq_close_cooperator).
316 struct rb_root prio_trees
[CFQ_PRIO_LISTS
];
318 unsigned int busy_queues
;
319 unsigned int busy_sync_queues
;
325 * queue-depth detection
331 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
332 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
335 int hw_tag_est_depth
;
336 unsigned int hw_tag_samples
;
339 * idle window management
341 struct timer_list idle_slice_timer
;
342 struct work_struct unplug_work
;
344 struct cfq_queue
*active_queue
;
345 struct cfq_io_cq
*active_cic
;
348 * async queue for each priority case
350 struct cfq_queue
*async_cfqq
[2][IOPRIO_BE_NR
];
351 struct cfq_queue
*async_idle_cfqq
;
353 sector_t last_position
;
356 * tunables, see top of file
358 unsigned int cfq_quantum
;
359 unsigned int cfq_fifo_expire
[2];
360 unsigned int cfq_back_penalty
;
361 unsigned int cfq_back_max
;
362 unsigned int cfq_slice
[2];
363 unsigned int cfq_slice_async_rq
;
364 unsigned int cfq_slice_idle
;
365 unsigned int cfq_group_idle
;
366 unsigned int cfq_latency
;
367 unsigned int cfq_target_latency
;
370 * Fallback dummy cfqq for extreme OOM conditions
372 struct cfq_queue oom_cfqq
;
374 unsigned long last_delayed_sync
;
377 static struct cfq_group
*cfq_get_next_cfqg(struct cfq_data
*cfqd
);
379 static struct cfq_rb_root
*st_for(struct cfq_group
*cfqg
,
380 enum wl_class_t
class,
386 if (class == IDLE_WORKLOAD
)
387 return &cfqg
->service_tree_idle
;
389 return &cfqg
->service_trees
[class][type
];
392 enum cfqq_state_flags
{
393 CFQ_CFQQ_FLAG_on_rr
= 0, /* on round-robin busy list */
394 CFQ_CFQQ_FLAG_wait_request
, /* waiting for a request */
395 CFQ_CFQQ_FLAG_must_dispatch
, /* must be allowed a dispatch */
396 CFQ_CFQQ_FLAG_must_alloc_slice
, /* per-slice must_alloc flag */
397 CFQ_CFQQ_FLAG_fifo_expire
, /* FIFO checked in this slice */
398 CFQ_CFQQ_FLAG_idle_window
, /* slice idling enabled */
399 CFQ_CFQQ_FLAG_prio_changed
, /* task priority has changed */
400 CFQ_CFQQ_FLAG_slice_new
, /* no requests dispatched in slice */
401 CFQ_CFQQ_FLAG_sync
, /* synchronous queue */
402 CFQ_CFQQ_FLAG_coop
, /* cfqq is shared */
403 CFQ_CFQQ_FLAG_split_coop
, /* shared cfqq will be splitted */
404 CFQ_CFQQ_FLAG_deep
, /* sync cfqq experienced large depth */
405 CFQ_CFQQ_FLAG_wait_busy
, /* Waiting for next request */
408 #define CFQ_CFQQ_FNS(name) \
409 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
411 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
413 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
415 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
417 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
419 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
423 CFQ_CFQQ_FNS(wait_request
);
424 CFQ_CFQQ_FNS(must_dispatch
);
425 CFQ_CFQQ_FNS(must_alloc_slice
);
426 CFQ_CFQQ_FNS(fifo_expire
);
427 CFQ_CFQQ_FNS(idle_window
);
428 CFQ_CFQQ_FNS(prio_changed
);
429 CFQ_CFQQ_FNS(slice_new
);
432 CFQ_CFQQ_FNS(split_coop
);
434 CFQ_CFQQ_FNS(wait_busy
);
437 static inline struct cfq_group
*pd_to_cfqg(struct blkg_policy_data
*pd
)
439 return pd
? container_of(pd
, struct cfq_group
, pd
) : NULL
;
442 static inline struct blkcg_gq
*cfqg_to_blkg(struct cfq_group
*cfqg
)
444 return pd_to_blkg(&cfqg
->pd
);
447 #if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
449 /* cfqg stats flags */
450 enum cfqg_stats_flags
{
451 CFQG_stats_waiting
= 0,
456 #define CFQG_FLAG_FNS(name) \
457 static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \
459 stats->flags |= (1 << CFQG_stats_##name); \
461 static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats) \
463 stats->flags &= ~(1 << CFQG_stats_##name); \
465 static inline int cfqg_stats_##name(struct cfqg_stats *stats) \
467 return (stats->flags & (1 << CFQG_stats_##name)) != 0; \
470 CFQG_FLAG_FNS(waiting)
471 CFQG_FLAG_FNS(idling
)
475 /* This should be called with the queue_lock held. */
476 static void cfqg_stats_update_group_wait_time(struct cfqg_stats
*stats
)
478 unsigned long long now
;
480 if (!cfqg_stats_waiting(stats
))
484 if (time_after64(now
, stats
->start_group_wait_time
))
485 blkg_stat_add(&stats
->group_wait_time
,
486 now
- stats
->start_group_wait_time
);
487 cfqg_stats_clear_waiting(stats
);
490 /* This should be called with the queue_lock held. */
491 static void cfqg_stats_set_start_group_wait_time(struct cfq_group
*cfqg
,
492 struct cfq_group
*curr_cfqg
)
494 struct cfqg_stats
*stats
= &cfqg
->stats
;
496 if (cfqg_stats_waiting(stats
))
498 if (cfqg
== curr_cfqg
)
500 stats
->start_group_wait_time
= sched_clock();
501 cfqg_stats_mark_waiting(stats
);
504 /* This should be called with the queue_lock held. */
505 static void cfqg_stats_end_empty_time(struct cfqg_stats
*stats
)
507 unsigned long long now
;
509 if (!cfqg_stats_empty(stats
))
513 if (time_after64(now
, stats
->start_empty_time
))
514 blkg_stat_add(&stats
->empty_time
,
515 now
- stats
->start_empty_time
);
516 cfqg_stats_clear_empty(stats
);
519 static void cfqg_stats_update_dequeue(struct cfq_group
*cfqg
)
521 blkg_stat_add(&cfqg
->stats
.dequeue
, 1);
524 static void cfqg_stats_set_start_empty_time(struct cfq_group
*cfqg
)
526 struct cfqg_stats
*stats
= &cfqg
->stats
;
528 if (blkg_rwstat_sum(&stats
->queued
))
532 * group is already marked empty. This can happen if cfqq got new
533 * request in parent group and moved to this group while being added
534 * to service tree. Just ignore the event and move on.
536 if (cfqg_stats_empty(stats
))
539 stats
->start_empty_time
= sched_clock();
540 cfqg_stats_mark_empty(stats
);
543 static void cfqg_stats_update_idle_time(struct cfq_group
*cfqg
)
545 struct cfqg_stats
*stats
= &cfqg
->stats
;
547 if (cfqg_stats_idling(stats
)) {
548 unsigned long long now
= sched_clock();
550 if (time_after64(now
, stats
->start_idle_time
))
551 blkg_stat_add(&stats
->idle_time
,
552 now
- stats
->start_idle_time
);
553 cfqg_stats_clear_idling(stats
);
557 static void cfqg_stats_set_start_idle_time(struct cfq_group
*cfqg
)
559 struct cfqg_stats
*stats
= &cfqg
->stats
;
561 BUG_ON(cfqg_stats_idling(stats
));
563 stats
->start_idle_time
= sched_clock();
564 cfqg_stats_mark_idling(stats
);
567 static void cfqg_stats_update_avg_queue_size(struct cfq_group
*cfqg
)
569 struct cfqg_stats
*stats
= &cfqg
->stats
;
571 blkg_stat_add(&stats
->avg_queue_size_sum
,
572 blkg_rwstat_sum(&stats
->queued
));
573 blkg_stat_add(&stats
->avg_queue_size_samples
, 1);
574 cfqg_stats_update_group_wait_time(stats
);
577 #else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
579 static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group
*cfqg
, struct cfq_group
*curr_cfqg
) { }
580 static inline void cfqg_stats_end_empty_time(struct cfqg_stats
*stats
) { }
581 static inline void cfqg_stats_update_dequeue(struct cfq_group
*cfqg
) { }
582 static inline void cfqg_stats_set_start_empty_time(struct cfq_group
*cfqg
) { }
583 static inline void cfqg_stats_update_idle_time(struct cfq_group
*cfqg
) { }
584 static inline void cfqg_stats_set_start_idle_time(struct cfq_group
*cfqg
) { }
585 static inline void cfqg_stats_update_avg_queue_size(struct cfq_group
*cfqg
) { }
587 #endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
589 #ifdef CONFIG_CFQ_GROUP_IOSCHED
591 static struct blkcg_policy blkcg_policy_cfq
;
593 static inline struct cfq_group
*blkg_to_cfqg(struct blkcg_gq
*blkg
)
595 return pd_to_cfqg(blkg_to_pd(blkg
, &blkcg_policy_cfq
));
599 * Determine the parent cfqg for weight calculation. Currently, cfqg
600 * scheduling is flat and the root is the parent of everyone else.
602 static inline struct cfq_group
*cfqg_flat_parent(struct cfq_group
*cfqg
)
604 struct blkcg_gq
*blkg
= cfqg_to_blkg(cfqg
);
605 struct cfq_group
*root
;
609 root
= blkg_to_cfqg(blkg
);
611 return root
!= cfqg
? root
: NULL
;
614 static inline void cfqg_get(struct cfq_group
*cfqg
)
616 return blkg_get(cfqg_to_blkg(cfqg
));
619 static inline void cfqg_put(struct cfq_group
*cfqg
)
621 return blkg_put(cfqg_to_blkg(cfqg
));
624 #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \
627 blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \
628 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \
629 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
630 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
634 #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
637 blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \
638 blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \
641 static inline void cfqg_stats_update_io_add(struct cfq_group
*cfqg
,
642 struct cfq_group
*curr_cfqg
, int rw
)
644 blkg_rwstat_add(&cfqg
->stats
.queued
, rw
, 1);
645 cfqg_stats_end_empty_time(&cfqg
->stats
);
646 cfqg_stats_set_start_group_wait_time(cfqg
, curr_cfqg
);
649 static inline void cfqg_stats_update_timeslice_used(struct cfq_group
*cfqg
,
650 unsigned long time
, unsigned long unaccounted_time
)
652 blkg_stat_add(&cfqg
->stats
.time
, time
);
653 #ifdef CONFIG_DEBUG_BLK_CGROUP
654 blkg_stat_add(&cfqg
->stats
.unaccounted_time
, unaccounted_time
);
658 static inline void cfqg_stats_update_io_remove(struct cfq_group
*cfqg
, int rw
)
660 blkg_rwstat_add(&cfqg
->stats
.queued
, rw
, -1);
663 static inline void cfqg_stats_update_io_merged(struct cfq_group
*cfqg
, int rw
)
665 blkg_rwstat_add(&cfqg
->stats
.merged
, rw
, 1);
668 static inline void cfqg_stats_update_dispatch(struct cfq_group
*cfqg
,
669 uint64_t bytes
, int rw
)
671 blkg_stat_add(&cfqg
->stats
.sectors
, bytes
>> 9);
672 blkg_rwstat_add(&cfqg
->stats
.serviced
, rw
, 1);
673 blkg_rwstat_add(&cfqg
->stats
.service_bytes
, rw
, bytes
);
676 static inline void cfqg_stats_update_completion(struct cfq_group
*cfqg
,
677 uint64_t start_time
, uint64_t io_start_time
, int rw
)
679 struct cfqg_stats
*stats
= &cfqg
->stats
;
680 unsigned long long now
= sched_clock();
682 if (time_after64(now
, io_start_time
))
683 blkg_rwstat_add(&stats
->service_time
, rw
, now
- io_start_time
);
684 if (time_after64(io_start_time
, start_time
))
685 blkg_rwstat_add(&stats
->wait_time
, rw
,
686 io_start_time
- start_time
);
689 static void cfq_pd_reset_stats(struct blkcg_gq
*blkg
)
691 struct cfq_group
*cfqg
= blkg_to_cfqg(blkg
);
692 struct cfqg_stats
*stats
= &cfqg
->stats
;
694 /* queued stats shouldn't be cleared */
695 blkg_rwstat_reset(&stats
->service_bytes
);
696 blkg_rwstat_reset(&stats
->serviced
);
697 blkg_rwstat_reset(&stats
->merged
);
698 blkg_rwstat_reset(&stats
->service_time
);
699 blkg_rwstat_reset(&stats
->wait_time
);
700 blkg_stat_reset(&stats
->time
);
701 #ifdef CONFIG_DEBUG_BLK_CGROUP
702 blkg_stat_reset(&stats
->unaccounted_time
);
703 blkg_stat_reset(&stats
->avg_queue_size_sum
);
704 blkg_stat_reset(&stats
->avg_queue_size_samples
);
705 blkg_stat_reset(&stats
->dequeue
);
706 blkg_stat_reset(&stats
->group_wait_time
);
707 blkg_stat_reset(&stats
->idle_time
);
708 blkg_stat_reset(&stats
->empty_time
);
712 #else /* CONFIG_CFQ_GROUP_IOSCHED */
714 static inline struct cfq_group
*cfqg_flat_parent(struct cfq_group
*cfqg
) { return NULL
; }
715 static inline void cfqg_get(struct cfq_group
*cfqg
) { }
716 static inline void cfqg_put(struct cfq_group
*cfqg
) { }
718 #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
719 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \
720 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
721 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
723 #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
725 static inline void cfqg_stats_update_io_add(struct cfq_group
*cfqg
,
726 struct cfq_group
*curr_cfqg
, int rw
) { }
727 static inline void cfqg_stats_update_timeslice_used(struct cfq_group
*cfqg
,
728 unsigned long time
, unsigned long unaccounted_time
) { }
729 static inline void cfqg_stats_update_io_remove(struct cfq_group
*cfqg
, int rw
) { }
730 static inline void cfqg_stats_update_io_merged(struct cfq_group
*cfqg
, int rw
) { }
731 static inline void cfqg_stats_update_dispatch(struct cfq_group
*cfqg
,
732 uint64_t bytes
, int rw
) { }
733 static inline void cfqg_stats_update_completion(struct cfq_group
*cfqg
,
734 uint64_t start_time
, uint64_t io_start_time
, int rw
) { }
736 #endif /* CONFIG_CFQ_GROUP_IOSCHED */
738 #define cfq_log(cfqd, fmt, args...) \
739 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
741 /* Traverses through cfq group service trees */
742 #define for_each_cfqg_st(cfqg, i, j, st) \
743 for (i = 0; i <= IDLE_WORKLOAD; i++) \
744 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
745 : &cfqg->service_tree_idle; \
746 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
747 (i == IDLE_WORKLOAD && j == 0); \
748 j++, st = i < IDLE_WORKLOAD ? \
749 &cfqg->service_trees[i][j]: NULL) \
751 static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
752 struct cfq_ttime
*ttime
, bool group_idle
)
755 if (!sample_valid(ttime
->ttime_samples
))
758 slice
= cfqd
->cfq_group_idle
;
760 slice
= cfqd
->cfq_slice_idle
;
761 return ttime
->ttime_mean
> slice
;
764 static inline bool iops_mode(struct cfq_data
*cfqd
)
767 * If we are not idling on queues and it is a NCQ drive, parallel
768 * execution of requests is on and measuring time is not possible
769 * in most of the cases until and unless we drive shallower queue
770 * depths and that becomes a performance bottleneck. In such cases
771 * switch to start providing fairness in terms of number of IOs.
773 if (!cfqd
->cfq_slice_idle
&& cfqd
->hw_tag
)
779 static inline enum wl_class_t
cfqq_class(struct cfq_queue
*cfqq
)
781 if (cfq_class_idle(cfqq
))
782 return IDLE_WORKLOAD
;
783 if (cfq_class_rt(cfqq
))
789 static enum wl_type_t
cfqq_type(struct cfq_queue
*cfqq
)
791 if (!cfq_cfqq_sync(cfqq
))
792 return ASYNC_WORKLOAD
;
793 if (!cfq_cfqq_idle_window(cfqq
))
794 return SYNC_NOIDLE_WORKLOAD
;
795 return SYNC_WORKLOAD
;
798 static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class
,
799 struct cfq_data
*cfqd
,
800 struct cfq_group
*cfqg
)
802 if (wl_class
== IDLE_WORKLOAD
)
803 return cfqg
->service_tree_idle
.count
;
805 return cfqg
->service_trees
[wl_class
][ASYNC_WORKLOAD
].count
+
806 cfqg
->service_trees
[wl_class
][SYNC_NOIDLE_WORKLOAD
].count
+
807 cfqg
->service_trees
[wl_class
][SYNC_WORKLOAD
].count
;
810 static inline int cfqg_busy_async_queues(struct cfq_data
*cfqd
,
811 struct cfq_group
*cfqg
)
813 return cfqg
->service_trees
[RT_WORKLOAD
][ASYNC_WORKLOAD
].count
+
814 cfqg
->service_trees
[BE_WORKLOAD
][ASYNC_WORKLOAD
].count
;
817 static void cfq_dispatch_insert(struct request_queue
*, struct request
*);
818 static struct cfq_queue
*cfq_get_queue(struct cfq_data
*cfqd
, bool is_sync
,
819 struct cfq_io_cq
*cic
, struct bio
*bio
,
822 static inline struct cfq_io_cq
*icq_to_cic(struct io_cq
*icq
)
824 /* cic->icq is the first member, %NULL will convert to %NULL */
825 return container_of(icq
, struct cfq_io_cq
, icq
);
828 static inline struct cfq_io_cq
*cfq_cic_lookup(struct cfq_data
*cfqd
,
829 struct io_context
*ioc
)
832 return icq_to_cic(ioc_lookup_icq(ioc
, cfqd
->queue
));
836 static inline struct cfq_queue
*cic_to_cfqq(struct cfq_io_cq
*cic
, bool is_sync
)
838 return cic
->cfqq
[is_sync
];
841 static inline void cic_set_cfqq(struct cfq_io_cq
*cic
, struct cfq_queue
*cfqq
,
844 cic
->cfqq
[is_sync
] = cfqq
;
847 static inline struct cfq_data
*cic_to_cfqd(struct cfq_io_cq
*cic
)
849 return cic
->icq
.q
->elevator
->elevator_data
;
853 * We regard a request as SYNC, if it's either a read or has the SYNC bit
854 * set (in which case it could also be direct WRITE).
856 static inline bool cfq_bio_sync(struct bio
*bio
)
858 return bio_data_dir(bio
) == READ
|| (bio
->bi_rw
& REQ_SYNC
);
862 * scheduler run of queue, if there are requests pending and no one in the
863 * driver that will restart queueing
865 static inline void cfq_schedule_dispatch(struct cfq_data
*cfqd
)
867 if (cfqd
->busy_queues
) {
868 cfq_log(cfqd
, "schedule dispatch");
869 kblockd_schedule_work(cfqd
->queue
, &cfqd
->unplug_work
);
874 * Scale schedule slice based on io priority. Use the sync time slice only
875 * if a queue is marked sync and has sync io queued. A sync queue with async
876 * io only, should not get full sync slice length.
878 static inline int cfq_prio_slice(struct cfq_data
*cfqd
, bool sync
,
881 const int base_slice
= cfqd
->cfq_slice
[sync
];
883 WARN_ON(prio
>= IOPRIO_BE_NR
);
885 return base_slice
+ (base_slice
/CFQ_SLICE_SCALE
* (4 - prio
));
889 cfq_prio_to_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
891 return cfq_prio_slice(cfqd
, cfq_cfqq_sync(cfqq
), cfqq
->ioprio
);
894 static inline u64
cfq_scale_slice(unsigned long delta
, struct cfq_group
*cfqg
)
896 u64 d
= delta
<< CFQ_SERVICE_SHIFT
;
898 d
= d
* CFQ_WEIGHT_DEFAULT
;
899 do_div(d
, cfqg
->weight
);
903 static inline u64
max_vdisktime(u64 min_vdisktime
, u64 vdisktime
)
905 s64 delta
= (s64
)(vdisktime
- min_vdisktime
);
907 min_vdisktime
= vdisktime
;
909 return min_vdisktime
;
912 static inline u64
min_vdisktime(u64 min_vdisktime
, u64 vdisktime
)
914 s64 delta
= (s64
)(vdisktime
- min_vdisktime
);
916 min_vdisktime
= vdisktime
;
918 return min_vdisktime
;
921 static void update_min_vdisktime(struct cfq_rb_root
*st
)
923 struct cfq_group
*cfqg
;
926 cfqg
= rb_entry_cfqg(st
->left
);
927 st
->min_vdisktime
= max_vdisktime(st
->min_vdisktime
,
933 * get averaged number of queues of RT/BE priority.
934 * average is updated, with a formula that gives more weight to higher numbers,
935 * to quickly follows sudden increases and decrease slowly
938 static inline unsigned cfq_group_get_avg_queues(struct cfq_data
*cfqd
,
939 struct cfq_group
*cfqg
, bool rt
)
941 unsigned min_q
, max_q
;
942 unsigned mult
= cfq_hist_divisor
- 1;
943 unsigned round
= cfq_hist_divisor
/ 2;
944 unsigned busy
= cfq_group_busy_queues_wl(rt
, cfqd
, cfqg
);
946 min_q
= min(cfqg
->busy_queues_avg
[rt
], busy
);
947 max_q
= max(cfqg
->busy_queues_avg
[rt
], busy
);
948 cfqg
->busy_queues_avg
[rt
] = (mult
* max_q
+ min_q
+ round
) /
950 return cfqg
->busy_queues_avg
[rt
];
953 static inline unsigned
954 cfq_group_slice(struct cfq_data
*cfqd
, struct cfq_group
*cfqg
)
956 struct cfq_rb_root
*st
= &cfqd
->grp_service_tree
;
958 return cfqd
->cfq_target_latency
* cfqg
->weight
/ st
->total_weight
;
961 static inline unsigned
962 cfq_scaled_cfqq_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
964 unsigned slice
= cfq_prio_to_slice(cfqd
, cfqq
);
965 if (cfqd
->cfq_latency
) {
967 * interested queues (we consider only the ones with the same
968 * priority class in the cfq group)
970 unsigned iq
= cfq_group_get_avg_queues(cfqd
, cfqq
->cfqg
,
972 unsigned sync_slice
= cfqd
->cfq_slice
[1];
973 unsigned expect_latency
= sync_slice
* iq
;
974 unsigned group_slice
= cfq_group_slice(cfqd
, cfqq
->cfqg
);
976 if (expect_latency
> group_slice
) {
977 unsigned base_low_slice
= 2 * cfqd
->cfq_slice_idle
;
978 /* scale low_slice according to IO priority
979 * and sync vs async */
981 min(slice
, base_low_slice
* slice
/ sync_slice
);
982 /* the adapted slice value is scaled to fit all iqs
983 * into the target latency */
984 slice
= max(slice
* group_slice
/ expect_latency
,
992 cfq_set_prio_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
994 unsigned slice
= cfq_scaled_cfqq_slice(cfqd
, cfqq
);
996 cfqq
->slice_start
= jiffies
;
997 cfqq
->slice_end
= jiffies
+ slice
;
998 cfqq
->allocated_slice
= slice
;
999 cfq_log_cfqq(cfqd
, cfqq
, "set_slice=%lu", cfqq
->slice_end
- jiffies
);
1003 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
1004 * isn't valid until the first request from the dispatch is activated
1005 * and the slice time set.
1007 static inline bool cfq_slice_used(struct cfq_queue
*cfqq
)
1009 if (cfq_cfqq_slice_new(cfqq
))
1011 if (time_before(jiffies
, cfqq
->slice_end
))
1018 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
1019 * We choose the request that is closest to the head right now. Distance
1020 * behind the head is penalized and only allowed to a certain extent.
1022 static struct request
*
1023 cfq_choose_req(struct cfq_data
*cfqd
, struct request
*rq1
, struct request
*rq2
, sector_t last
)
1025 sector_t s1
, s2
, d1
= 0, d2
= 0;
1026 unsigned long back_max
;
1027 #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
1028 #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
1029 unsigned wrap
= 0; /* bit mask: requests behind the disk head? */
1031 if (rq1
== NULL
|| rq1
== rq2
)
1036 if (rq_is_sync(rq1
) != rq_is_sync(rq2
))
1037 return rq_is_sync(rq1
) ? rq1
: rq2
;
1039 if ((rq1
->cmd_flags
^ rq2
->cmd_flags
) & REQ_PRIO
)
1040 return rq1
->cmd_flags
& REQ_PRIO
? rq1
: rq2
;
1042 s1
= blk_rq_pos(rq1
);
1043 s2
= blk_rq_pos(rq2
);
1046 * by definition, 1KiB is 2 sectors
1048 back_max
= cfqd
->cfq_back_max
* 2;
1051 * Strict one way elevator _except_ in the case where we allow
1052 * short backward seeks which are biased as twice the cost of a
1053 * similar forward seek.
1057 else if (s1
+ back_max
>= last
)
1058 d1
= (last
- s1
) * cfqd
->cfq_back_penalty
;
1060 wrap
|= CFQ_RQ1_WRAP
;
1064 else if (s2
+ back_max
>= last
)
1065 d2
= (last
- s2
) * cfqd
->cfq_back_penalty
;
1067 wrap
|= CFQ_RQ2_WRAP
;
1069 /* Found required data */
1072 * By doing switch() on the bit mask "wrap" we avoid having to
1073 * check two variables for all permutations: --> faster!
1076 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
1092 case (CFQ_RQ1_WRAP
|CFQ_RQ2_WRAP
): /* both rqs wrapped */
1095 * Since both rqs are wrapped,
1096 * start with the one that's further behind head
1097 * (--> only *one* back seek required),
1098 * since back seek takes more time than forward.
1108 * The below is leftmost cache rbtree addon
1110 static struct cfq_queue
*cfq_rb_first(struct cfq_rb_root
*root
)
1112 /* Service tree is empty */
1117 root
->left
= rb_first(&root
->rb
);
1120 return rb_entry(root
->left
, struct cfq_queue
, rb_node
);
1125 static struct cfq_group
*cfq_rb_first_group(struct cfq_rb_root
*root
)
1128 root
->left
= rb_first(&root
->rb
);
1131 return rb_entry_cfqg(root
->left
);
1136 static void rb_erase_init(struct rb_node
*n
, struct rb_root
*root
)
1142 static void cfq_rb_erase(struct rb_node
*n
, struct cfq_rb_root
*root
)
1144 if (root
->left
== n
)
1146 rb_erase_init(n
, &root
->rb
);
1151 * would be nice to take fifo expire time into account as well
1153 static struct request
*
1154 cfq_find_next_rq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1155 struct request
*last
)
1157 struct rb_node
*rbnext
= rb_next(&last
->rb_node
);
1158 struct rb_node
*rbprev
= rb_prev(&last
->rb_node
);
1159 struct request
*next
= NULL
, *prev
= NULL
;
1161 BUG_ON(RB_EMPTY_NODE(&last
->rb_node
));
1164 prev
= rb_entry_rq(rbprev
);
1167 next
= rb_entry_rq(rbnext
);
1169 rbnext
= rb_first(&cfqq
->sort_list
);
1170 if (rbnext
&& rbnext
!= &last
->rb_node
)
1171 next
= rb_entry_rq(rbnext
);
1174 return cfq_choose_req(cfqd
, next
, prev
, blk_rq_pos(last
));
1177 static unsigned long cfq_slice_offset(struct cfq_data
*cfqd
,
1178 struct cfq_queue
*cfqq
)
1181 * just an approximation, should be ok.
1183 return (cfqq
->cfqg
->nr_cfqq
- 1) * (cfq_prio_slice(cfqd
, 1, 0) -
1184 cfq_prio_slice(cfqd
, cfq_cfqq_sync(cfqq
), cfqq
->ioprio
));
1188 cfqg_key(struct cfq_rb_root
*st
, struct cfq_group
*cfqg
)
1190 return cfqg
->vdisktime
- st
->min_vdisktime
;
1194 __cfq_group_service_tree_add(struct cfq_rb_root
*st
, struct cfq_group
*cfqg
)
1196 struct rb_node
**node
= &st
->rb
.rb_node
;
1197 struct rb_node
*parent
= NULL
;
1198 struct cfq_group
*__cfqg
;
1199 s64 key
= cfqg_key(st
, cfqg
);
1202 while (*node
!= NULL
) {
1204 __cfqg
= rb_entry_cfqg(parent
);
1206 if (key
< cfqg_key(st
, __cfqg
))
1207 node
= &parent
->rb_left
;
1209 node
= &parent
->rb_right
;
1215 st
->left
= &cfqg
->rb_node
;
1217 rb_link_node(&cfqg
->rb_node
, parent
, node
);
1218 rb_insert_color(&cfqg
->rb_node
, &st
->rb
);
1222 cfq_update_group_weight(struct cfq_group
*cfqg
)
1224 BUG_ON(!RB_EMPTY_NODE(&cfqg
->rb_node
));
1226 if (cfqg
->new_weight
) {
1227 cfqg
->weight
= cfqg
->new_weight
;
1228 cfqg
->new_weight
= 0;
1231 if (cfqg
->new_leaf_weight
) {
1232 cfqg
->leaf_weight
= cfqg
->new_leaf_weight
;
1233 cfqg
->new_leaf_weight
= 0;
1238 cfq_group_service_tree_add(struct cfq_rb_root
*st
, struct cfq_group
*cfqg
)
1240 struct cfq_group
*pos
= cfqg
;
1243 /* add to the service tree */
1244 BUG_ON(!RB_EMPTY_NODE(&cfqg
->rb_node
));
1246 cfq_update_group_weight(cfqg
);
1247 __cfq_group_service_tree_add(st
, cfqg
);
1248 st
->total_weight
+= cfqg
->weight
;
1251 * Activate @cfqg and propagate activation upwards until we meet an
1252 * already activated node or reach root.
1254 propagate
= !pos
->nr_active
++;
1255 pos
->children_weight
+= pos
->leaf_weight
;
1258 struct cfq_group
*parent
= cfqg_flat_parent(pos
);
1263 propagate
= !parent
->nr_active
++;
1264 parent
->children_weight
+= pos
->weight
;
1270 cfq_group_notify_queue_add(struct cfq_data
*cfqd
, struct cfq_group
*cfqg
)
1272 struct cfq_rb_root
*st
= &cfqd
->grp_service_tree
;
1273 struct cfq_group
*__cfqg
;
1277 if (!RB_EMPTY_NODE(&cfqg
->rb_node
))
1281 * Currently put the group at the end. Later implement something
1282 * so that groups get lesser vtime based on their weights, so that
1283 * if group does not loose all if it was not continuously backlogged.
1285 n
= rb_last(&st
->rb
);
1287 __cfqg
= rb_entry_cfqg(n
);
1288 cfqg
->vdisktime
= __cfqg
->vdisktime
+ CFQ_IDLE_DELAY
;
1290 cfqg
->vdisktime
= st
->min_vdisktime
;
1291 cfq_group_service_tree_add(st
, cfqg
);
1295 cfq_group_service_tree_del(struct cfq_rb_root
*st
, struct cfq_group
*cfqg
)
1297 struct cfq_group
*pos
= cfqg
;
1301 * Undo activation from cfq_group_service_tree_add(). Deactivate
1302 * @cfqg and propagate deactivation upwards.
1304 propagate
= !--pos
->nr_active
;
1305 pos
->children_weight
-= pos
->leaf_weight
;
1308 struct cfq_group
*parent
= cfqg_flat_parent(pos
);
1310 /* @pos has 0 nr_active at this point */
1311 WARN_ON_ONCE(pos
->children_weight
);
1316 propagate
= !--parent
->nr_active
;
1317 parent
->children_weight
-= pos
->weight
;
1321 /* remove from the service tree */
1322 st
->total_weight
-= cfqg
->weight
;
1323 if (!RB_EMPTY_NODE(&cfqg
->rb_node
))
1324 cfq_rb_erase(&cfqg
->rb_node
, st
);
1328 cfq_group_notify_queue_del(struct cfq_data
*cfqd
, struct cfq_group
*cfqg
)
1330 struct cfq_rb_root
*st
= &cfqd
->grp_service_tree
;
1332 BUG_ON(cfqg
->nr_cfqq
< 1);
1335 /* If there are other cfq queues under this group, don't delete it */
1339 cfq_log_cfqg(cfqd
, cfqg
, "del_from_rr group");
1340 cfq_group_service_tree_del(st
, cfqg
);
1341 cfqg
->saved_wl_slice
= 0;
1342 cfqg_stats_update_dequeue(cfqg
);
1345 static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue
*cfqq
,
1346 unsigned int *unaccounted_time
)
1348 unsigned int slice_used
;
1351 * Queue got expired before even a single request completed or
1352 * got expired immediately after first request completion.
1354 if (!cfqq
->slice_start
|| cfqq
->slice_start
== jiffies
) {
1356 * Also charge the seek time incurred to the group, otherwise
1357 * if there are mutiple queues in the group, each can dispatch
1358 * a single request on seeky media and cause lots of seek time
1359 * and group will never know it.
1361 slice_used
= max_t(unsigned, (jiffies
- cfqq
->dispatch_start
),
1364 slice_used
= jiffies
- cfqq
->slice_start
;
1365 if (slice_used
> cfqq
->allocated_slice
) {
1366 *unaccounted_time
= slice_used
- cfqq
->allocated_slice
;
1367 slice_used
= cfqq
->allocated_slice
;
1369 if (time_after(cfqq
->slice_start
, cfqq
->dispatch_start
))
1370 *unaccounted_time
+= cfqq
->slice_start
-
1371 cfqq
->dispatch_start
;
1377 static void cfq_group_served(struct cfq_data
*cfqd
, struct cfq_group
*cfqg
,
1378 struct cfq_queue
*cfqq
)
1380 struct cfq_rb_root
*st
= &cfqd
->grp_service_tree
;
1381 unsigned int used_sl
, charge
, unaccounted_sl
= 0;
1382 int nr_sync
= cfqg
->nr_cfqq
- cfqg_busy_async_queues(cfqd
, cfqg
)
1383 - cfqg
->service_tree_idle
.count
;
1385 BUG_ON(nr_sync
< 0);
1386 used_sl
= charge
= cfq_cfqq_slice_usage(cfqq
, &unaccounted_sl
);
1388 if (iops_mode(cfqd
))
1389 charge
= cfqq
->slice_dispatch
;
1390 else if (!cfq_cfqq_sync(cfqq
) && !nr_sync
)
1391 charge
= cfqq
->allocated_slice
;
1393 /* Can't update vdisktime while group is on service tree */
1394 cfq_group_service_tree_del(st
, cfqg
);
1395 cfqg
->vdisktime
+= cfq_scale_slice(charge
, cfqg
);
1396 /* If a new weight was requested, update now, off tree */
1397 cfq_group_service_tree_add(st
, cfqg
);
1399 /* This group is being expired. Save the context */
1400 if (time_after(cfqd
->workload_expires
, jiffies
)) {
1401 cfqg
->saved_wl_slice
= cfqd
->workload_expires
1403 cfqg
->saved_wl_type
= cfqd
->serving_wl_type
;
1404 cfqg
->saved_wl_class
= cfqd
->serving_wl_class
;
1406 cfqg
->saved_wl_slice
= 0;
1408 cfq_log_cfqg(cfqd
, cfqg
, "served: vt=%llu min_vt=%llu", cfqg
->vdisktime
,
1410 cfq_log_cfqq(cfqq
->cfqd
, cfqq
,
1411 "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
1412 used_sl
, cfqq
->slice_dispatch
, charge
,
1413 iops_mode(cfqd
), cfqq
->nr_sectors
);
1414 cfqg_stats_update_timeslice_used(cfqg
, used_sl
, unaccounted_sl
);
1415 cfqg_stats_set_start_empty_time(cfqg
);
1419 * cfq_init_cfqg_base - initialize base part of a cfq_group
1420 * @cfqg: cfq_group to initialize
1422 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1423 * is enabled or not.
1425 static void cfq_init_cfqg_base(struct cfq_group
*cfqg
)
1427 struct cfq_rb_root
*st
;
1430 for_each_cfqg_st(cfqg
, i
, j
, st
)
1432 RB_CLEAR_NODE(&cfqg
->rb_node
);
1434 cfqg
->ttime
.last_end_request
= jiffies
;
1437 #ifdef CONFIG_CFQ_GROUP_IOSCHED
1438 static void cfq_pd_init(struct blkcg_gq
*blkg
)
1440 struct cfq_group
*cfqg
= blkg_to_cfqg(blkg
);
1442 cfq_init_cfqg_base(cfqg
);
1443 cfqg
->weight
= blkg
->blkcg
->cfq_weight
;
1444 cfqg
->leaf_weight
= blkg
->blkcg
->cfq_leaf_weight
;
1448 * Search for the cfq group current task belongs to. request_queue lock must
1451 static struct cfq_group
*cfq_lookup_create_cfqg(struct cfq_data
*cfqd
,
1452 struct blkcg
*blkcg
)
1454 struct request_queue
*q
= cfqd
->queue
;
1455 struct cfq_group
*cfqg
= NULL
;
1457 /* avoid lookup for the common case where there's no blkcg */
1458 if (blkcg
== &blkcg_root
) {
1459 cfqg
= cfqd
->root_group
;
1461 struct blkcg_gq
*blkg
;
1463 blkg
= blkg_lookup_create(blkcg
, q
);
1465 cfqg
= blkg_to_cfqg(blkg
);
1471 static void cfq_link_cfqq_cfqg(struct cfq_queue
*cfqq
, struct cfq_group
*cfqg
)
1473 /* Currently, all async queues are mapped to root group */
1474 if (!cfq_cfqq_sync(cfqq
))
1475 cfqg
= cfqq
->cfqd
->root_group
;
1478 /* cfqq reference on cfqg */
1482 static u64
cfqg_prfill_weight_device(struct seq_file
*sf
,
1483 struct blkg_policy_data
*pd
, int off
)
1485 struct cfq_group
*cfqg
= pd_to_cfqg(pd
);
1487 if (!cfqg
->dev_weight
)
1489 return __blkg_prfill_u64(sf
, pd
, cfqg
->dev_weight
);
1492 static int cfqg_print_weight_device(struct cgroup
*cgrp
, struct cftype
*cft
,
1493 struct seq_file
*sf
)
1495 blkcg_print_blkgs(sf
, cgroup_to_blkcg(cgrp
),
1496 cfqg_prfill_weight_device
, &blkcg_policy_cfq
, 0,
1501 static u64
cfqg_prfill_leaf_weight_device(struct seq_file
*sf
,
1502 struct blkg_policy_data
*pd
, int off
)
1504 struct cfq_group
*cfqg
= pd_to_cfqg(pd
);
1506 if (!cfqg
->dev_leaf_weight
)
1508 return __blkg_prfill_u64(sf
, pd
, cfqg
->dev_leaf_weight
);
1511 static int cfqg_print_leaf_weight_device(struct cgroup
*cgrp
,
1513 struct seq_file
*sf
)
1515 blkcg_print_blkgs(sf
, cgroup_to_blkcg(cgrp
),
1516 cfqg_prfill_leaf_weight_device
, &blkcg_policy_cfq
, 0,
1521 static int cfq_print_weight(struct cgroup
*cgrp
, struct cftype
*cft
,
1522 struct seq_file
*sf
)
1524 seq_printf(sf
, "%u\n", cgroup_to_blkcg(cgrp
)->cfq_weight
);
1528 static int cfq_print_leaf_weight(struct cgroup
*cgrp
, struct cftype
*cft
,
1529 struct seq_file
*sf
)
1531 seq_printf(sf
, "%u\n",
1532 cgroup_to_blkcg(cgrp
)->cfq_leaf_weight
);
1536 static int __cfqg_set_weight_device(struct cgroup
*cgrp
, struct cftype
*cft
,
1537 const char *buf
, bool is_leaf_weight
)
1539 struct blkcg
*blkcg
= cgroup_to_blkcg(cgrp
);
1540 struct blkg_conf_ctx ctx
;
1541 struct cfq_group
*cfqg
;
1544 ret
= blkg_conf_prep(blkcg
, &blkcg_policy_cfq
, buf
, &ctx
);
1549 cfqg
= blkg_to_cfqg(ctx
.blkg
);
1550 if (!ctx
.v
|| (ctx
.v
>= CFQ_WEIGHT_MIN
&& ctx
.v
<= CFQ_WEIGHT_MAX
)) {
1551 if (!is_leaf_weight
) {
1552 cfqg
->dev_weight
= ctx
.v
;
1553 cfqg
->new_weight
= ctx
.v
?: blkcg
->cfq_weight
;
1555 cfqg
->dev_leaf_weight
= ctx
.v
;
1556 cfqg
->new_leaf_weight
= ctx
.v
?: blkcg
->cfq_leaf_weight
;
1561 blkg_conf_finish(&ctx
);
1565 static int cfqg_set_weight_device(struct cgroup
*cgrp
, struct cftype
*cft
,
1568 return __cfqg_set_weight_device(cgrp
, cft
, buf
, false);
1571 static int cfqg_set_leaf_weight_device(struct cgroup
*cgrp
, struct cftype
*cft
,
1574 return __cfqg_set_weight_device(cgrp
, cft
, buf
, true);
1577 static int __cfq_set_weight(struct cgroup
*cgrp
, struct cftype
*cft
, u64 val
,
1578 bool is_leaf_weight
)
1580 struct blkcg
*blkcg
= cgroup_to_blkcg(cgrp
);
1581 struct blkcg_gq
*blkg
;
1582 struct hlist_node
*n
;
1584 if (val
< CFQ_WEIGHT_MIN
|| val
> CFQ_WEIGHT_MAX
)
1587 spin_lock_irq(&blkcg
->lock
);
1589 if (!is_leaf_weight
)
1590 blkcg
->cfq_weight
= val
;
1592 blkcg
->cfq_leaf_weight
= val
;
1594 hlist_for_each_entry(blkg
, n
, &blkcg
->blkg_list
, blkcg_node
) {
1595 struct cfq_group
*cfqg
= blkg_to_cfqg(blkg
);
1600 if (!is_leaf_weight
) {
1601 if (!cfqg
->dev_weight
)
1602 cfqg
->new_weight
= blkcg
->cfq_weight
;
1604 if (!cfqg
->dev_leaf_weight
)
1605 cfqg
->new_leaf_weight
= blkcg
->cfq_leaf_weight
;
1609 spin_unlock_irq(&blkcg
->lock
);
1613 static int cfq_set_weight(struct cgroup
*cgrp
, struct cftype
*cft
, u64 val
)
1615 return __cfq_set_weight(cgrp
, cft
, val
, false);
1618 static int cfq_set_leaf_weight(struct cgroup
*cgrp
, struct cftype
*cft
, u64 val
)
1620 return __cfq_set_weight(cgrp
, cft
, val
, true);
1623 static int cfqg_print_stat(struct cgroup
*cgrp
, struct cftype
*cft
,
1624 struct seq_file
*sf
)
1626 struct blkcg
*blkcg
= cgroup_to_blkcg(cgrp
);
1628 blkcg_print_blkgs(sf
, blkcg
, blkg_prfill_stat
, &blkcg_policy_cfq
,
1629 cft
->private, false);
1633 static int cfqg_print_rwstat(struct cgroup
*cgrp
, struct cftype
*cft
,
1634 struct seq_file
*sf
)
1636 struct blkcg
*blkcg
= cgroup_to_blkcg(cgrp
);
1638 blkcg_print_blkgs(sf
, blkcg
, blkg_prfill_rwstat
, &blkcg_policy_cfq
,
1639 cft
->private, true);
1643 #ifdef CONFIG_DEBUG_BLK_CGROUP
1644 static u64
cfqg_prfill_avg_queue_size(struct seq_file
*sf
,
1645 struct blkg_policy_data
*pd
, int off
)
1647 struct cfq_group
*cfqg
= pd_to_cfqg(pd
);
1648 u64 samples
= blkg_stat_read(&cfqg
->stats
.avg_queue_size_samples
);
1652 v
= blkg_stat_read(&cfqg
->stats
.avg_queue_size_sum
);
1655 __blkg_prfill_u64(sf
, pd
, v
);
1659 /* print avg_queue_size */
1660 static int cfqg_print_avg_queue_size(struct cgroup
*cgrp
, struct cftype
*cft
,
1661 struct seq_file
*sf
)
1663 struct blkcg
*blkcg
= cgroup_to_blkcg(cgrp
);
1665 blkcg_print_blkgs(sf
, blkcg
, cfqg_prfill_avg_queue_size
,
1666 &blkcg_policy_cfq
, 0, false);
1669 #endif /* CONFIG_DEBUG_BLK_CGROUP */
1671 static struct cftype cfq_blkcg_files
[] = {
1673 .name
= "weight_device",
1674 .read_seq_string
= cfqg_print_weight_device
,
1675 .write_string
= cfqg_set_weight_device
,
1676 .max_write_len
= 256,
1680 .read_seq_string
= cfq_print_weight
,
1681 .write_u64
= cfq_set_weight
,
1684 /* on root, leaf_weight is mapped to weight */
1686 .name
= "leaf_weight_device",
1687 .flags
= CFTYPE_ONLY_ON_ROOT
,
1688 .read_seq_string
= cfqg_print_weight_device
,
1689 .write_string
= cfqg_set_weight_device
,
1690 .max_write_len
= 256,
1693 .name
= "leaf_weight",
1694 .flags
= CFTYPE_ONLY_ON_ROOT
,
1695 .read_seq_string
= cfq_print_weight
,
1696 .write_u64
= cfq_set_weight
,
1699 /* no such mapping necessary for !roots */
1701 .name
= "leaf_weight_device",
1702 .flags
= CFTYPE_NOT_ON_ROOT
,
1703 .read_seq_string
= cfqg_print_leaf_weight_device
,
1704 .write_string
= cfqg_set_leaf_weight_device
,
1705 .max_write_len
= 256,
1708 .name
= "leaf_weight",
1709 .flags
= CFTYPE_NOT_ON_ROOT
,
1710 .read_seq_string
= cfq_print_leaf_weight
,
1711 .write_u64
= cfq_set_leaf_weight
,
1716 .private = offsetof(struct cfq_group
, stats
.time
),
1717 .read_seq_string
= cfqg_print_stat
,
1721 .private = offsetof(struct cfq_group
, stats
.sectors
),
1722 .read_seq_string
= cfqg_print_stat
,
1725 .name
= "io_service_bytes",
1726 .private = offsetof(struct cfq_group
, stats
.service_bytes
),
1727 .read_seq_string
= cfqg_print_rwstat
,
1730 .name
= "io_serviced",
1731 .private = offsetof(struct cfq_group
, stats
.serviced
),
1732 .read_seq_string
= cfqg_print_rwstat
,
1735 .name
= "io_service_time",
1736 .private = offsetof(struct cfq_group
, stats
.service_time
),
1737 .read_seq_string
= cfqg_print_rwstat
,
1740 .name
= "io_wait_time",
1741 .private = offsetof(struct cfq_group
, stats
.wait_time
),
1742 .read_seq_string
= cfqg_print_rwstat
,
1745 .name
= "io_merged",
1746 .private = offsetof(struct cfq_group
, stats
.merged
),
1747 .read_seq_string
= cfqg_print_rwstat
,
1750 .name
= "io_queued",
1751 .private = offsetof(struct cfq_group
, stats
.queued
),
1752 .read_seq_string
= cfqg_print_rwstat
,
1754 #ifdef CONFIG_DEBUG_BLK_CGROUP
1756 .name
= "avg_queue_size",
1757 .read_seq_string
= cfqg_print_avg_queue_size
,
1760 .name
= "group_wait_time",
1761 .private = offsetof(struct cfq_group
, stats
.group_wait_time
),
1762 .read_seq_string
= cfqg_print_stat
,
1765 .name
= "idle_time",
1766 .private = offsetof(struct cfq_group
, stats
.idle_time
),
1767 .read_seq_string
= cfqg_print_stat
,
1770 .name
= "empty_time",
1771 .private = offsetof(struct cfq_group
, stats
.empty_time
),
1772 .read_seq_string
= cfqg_print_stat
,
1776 .private = offsetof(struct cfq_group
, stats
.dequeue
),
1777 .read_seq_string
= cfqg_print_stat
,
1780 .name
= "unaccounted_time",
1781 .private = offsetof(struct cfq_group
, stats
.unaccounted_time
),
1782 .read_seq_string
= cfqg_print_stat
,
1784 #endif /* CONFIG_DEBUG_BLK_CGROUP */
1787 #else /* GROUP_IOSCHED */
1788 static struct cfq_group
*cfq_lookup_create_cfqg(struct cfq_data
*cfqd
,
1789 struct blkcg
*blkcg
)
1791 return cfqd
->root_group
;
1795 cfq_link_cfqq_cfqg(struct cfq_queue
*cfqq
, struct cfq_group
*cfqg
) {
1799 #endif /* GROUP_IOSCHED */
1802 * The cfqd->service_trees holds all pending cfq_queue's that have
1803 * requests waiting to be processed. It is sorted in the order that
1804 * we will service the queues.
1806 static void cfq_service_tree_add(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1809 struct rb_node
**p
, *parent
;
1810 struct cfq_queue
*__cfqq
;
1811 unsigned long rb_key
;
1812 struct cfq_rb_root
*st
;
1816 st
= st_for(cfqq
->cfqg
, cfqq_class(cfqq
), cfqq_type(cfqq
));
1817 if (cfq_class_idle(cfqq
)) {
1818 rb_key
= CFQ_IDLE_DELAY
;
1819 parent
= rb_last(&st
->rb
);
1820 if (parent
&& parent
!= &cfqq
->rb_node
) {
1821 __cfqq
= rb_entry(parent
, struct cfq_queue
, rb_node
);
1822 rb_key
+= __cfqq
->rb_key
;
1825 } else if (!add_front
) {
1827 * Get our rb key offset. Subtract any residual slice
1828 * value carried from last service. A negative resid
1829 * count indicates slice overrun, and this should position
1830 * the next service time further away in the tree.
1832 rb_key
= cfq_slice_offset(cfqd
, cfqq
) + jiffies
;
1833 rb_key
-= cfqq
->slice_resid
;
1834 cfqq
->slice_resid
= 0;
1837 __cfqq
= cfq_rb_first(st
);
1838 rb_key
+= __cfqq
? __cfqq
->rb_key
: jiffies
;
1841 if (!RB_EMPTY_NODE(&cfqq
->rb_node
)) {
1844 * same position, nothing more to do
1846 if (rb_key
== cfqq
->rb_key
&& cfqq
->service_tree
== st
)
1849 cfq_rb_erase(&cfqq
->rb_node
, cfqq
->service_tree
);
1850 cfqq
->service_tree
= NULL
;
1855 cfqq
->service_tree
= st
;
1856 p
= &st
->rb
.rb_node
;
1859 __cfqq
= rb_entry(parent
, struct cfq_queue
, rb_node
);
1862 * sort by key, that represents service time.
1864 if (time_before(rb_key
, __cfqq
->rb_key
))
1865 p
= &parent
->rb_left
;
1867 p
= &parent
->rb_right
;
1873 st
->left
= &cfqq
->rb_node
;
1875 cfqq
->rb_key
= rb_key
;
1876 rb_link_node(&cfqq
->rb_node
, parent
, p
);
1877 rb_insert_color(&cfqq
->rb_node
, &st
->rb
);
1879 if (add_front
|| !new_cfqq
)
1881 cfq_group_notify_queue_add(cfqd
, cfqq
->cfqg
);
1884 static struct cfq_queue
*
1885 cfq_prio_tree_lookup(struct cfq_data
*cfqd
, struct rb_root
*root
,
1886 sector_t sector
, struct rb_node
**ret_parent
,
1887 struct rb_node
***rb_link
)
1889 struct rb_node
**p
, *parent
;
1890 struct cfq_queue
*cfqq
= NULL
;
1898 cfqq
= rb_entry(parent
, struct cfq_queue
, p_node
);
1901 * Sort strictly based on sector. Smallest to the left,
1902 * largest to the right.
1904 if (sector
> blk_rq_pos(cfqq
->next_rq
))
1905 n
= &(*p
)->rb_right
;
1906 else if (sector
< blk_rq_pos(cfqq
->next_rq
))
1914 *ret_parent
= parent
;
1920 static void cfq_prio_tree_add(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1922 struct rb_node
**p
, *parent
;
1923 struct cfq_queue
*__cfqq
;
1926 rb_erase(&cfqq
->p_node
, cfqq
->p_root
);
1927 cfqq
->p_root
= NULL
;
1930 if (cfq_class_idle(cfqq
))
1935 cfqq
->p_root
= &cfqd
->prio_trees
[cfqq
->org_ioprio
];
1936 __cfqq
= cfq_prio_tree_lookup(cfqd
, cfqq
->p_root
,
1937 blk_rq_pos(cfqq
->next_rq
), &parent
, &p
);
1939 rb_link_node(&cfqq
->p_node
, parent
, p
);
1940 rb_insert_color(&cfqq
->p_node
, cfqq
->p_root
);
1942 cfqq
->p_root
= NULL
;
1946 * Update cfqq's position in the service tree.
1948 static void cfq_resort_rr_list(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1951 * Resorting requires the cfqq to be on the RR list already.
1953 if (cfq_cfqq_on_rr(cfqq
)) {
1954 cfq_service_tree_add(cfqd
, cfqq
, 0);
1955 cfq_prio_tree_add(cfqd
, cfqq
);
1960 * add to busy list of queues for service, trying to be fair in ordering
1961 * the pending list according to last request service
1963 static void cfq_add_cfqq_rr(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1965 cfq_log_cfqq(cfqd
, cfqq
, "add_to_rr");
1966 BUG_ON(cfq_cfqq_on_rr(cfqq
));
1967 cfq_mark_cfqq_on_rr(cfqq
);
1968 cfqd
->busy_queues
++;
1969 if (cfq_cfqq_sync(cfqq
))
1970 cfqd
->busy_sync_queues
++;
1972 cfq_resort_rr_list(cfqd
, cfqq
);
1976 * Called when the cfqq no longer has requests pending, remove it from
1979 static void cfq_del_cfqq_rr(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1981 cfq_log_cfqq(cfqd
, cfqq
, "del_from_rr");
1982 BUG_ON(!cfq_cfqq_on_rr(cfqq
));
1983 cfq_clear_cfqq_on_rr(cfqq
);
1985 if (!RB_EMPTY_NODE(&cfqq
->rb_node
)) {
1986 cfq_rb_erase(&cfqq
->rb_node
, cfqq
->service_tree
);
1987 cfqq
->service_tree
= NULL
;
1990 rb_erase(&cfqq
->p_node
, cfqq
->p_root
);
1991 cfqq
->p_root
= NULL
;
1994 cfq_group_notify_queue_del(cfqd
, cfqq
->cfqg
);
1995 BUG_ON(!cfqd
->busy_queues
);
1996 cfqd
->busy_queues
--;
1997 if (cfq_cfqq_sync(cfqq
))
1998 cfqd
->busy_sync_queues
--;
2002 * rb tree support functions
2004 static void cfq_del_rq_rb(struct request
*rq
)
2006 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
2007 const int sync
= rq_is_sync(rq
);
2009 BUG_ON(!cfqq
->queued
[sync
]);
2010 cfqq
->queued
[sync
]--;
2012 elv_rb_del(&cfqq
->sort_list
, rq
);
2014 if (cfq_cfqq_on_rr(cfqq
) && RB_EMPTY_ROOT(&cfqq
->sort_list
)) {
2016 * Queue will be deleted from service tree when we actually
2017 * expire it later. Right now just remove it from prio tree
2021 rb_erase(&cfqq
->p_node
, cfqq
->p_root
);
2022 cfqq
->p_root
= NULL
;
2027 static void cfq_add_rq_rb(struct request
*rq
)
2029 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
2030 struct cfq_data
*cfqd
= cfqq
->cfqd
;
2031 struct request
*prev
;
2033 cfqq
->queued
[rq_is_sync(rq
)]++;
2035 elv_rb_add(&cfqq
->sort_list
, rq
);
2037 if (!cfq_cfqq_on_rr(cfqq
))
2038 cfq_add_cfqq_rr(cfqd
, cfqq
);
2041 * check if this request is a better next-serve candidate
2043 prev
= cfqq
->next_rq
;
2044 cfqq
->next_rq
= cfq_choose_req(cfqd
, cfqq
->next_rq
, rq
, cfqd
->last_position
);
2047 * adjust priority tree position, if ->next_rq changes
2049 if (prev
!= cfqq
->next_rq
)
2050 cfq_prio_tree_add(cfqd
, cfqq
);
2052 BUG_ON(!cfqq
->next_rq
);
2055 static void cfq_reposition_rq_rb(struct cfq_queue
*cfqq
, struct request
*rq
)
2057 elv_rb_del(&cfqq
->sort_list
, rq
);
2058 cfqq
->queued
[rq_is_sync(rq
)]--;
2059 cfqg_stats_update_io_remove(RQ_CFQG(rq
), rq
->cmd_flags
);
2061 cfqg_stats_update_io_add(RQ_CFQG(rq
), cfqq
->cfqd
->serving_group
,
2065 static struct request
*
2066 cfq_find_rq_fmerge(struct cfq_data
*cfqd
, struct bio
*bio
)
2068 struct task_struct
*tsk
= current
;
2069 struct cfq_io_cq
*cic
;
2070 struct cfq_queue
*cfqq
;
2072 cic
= cfq_cic_lookup(cfqd
, tsk
->io_context
);
2076 cfqq
= cic_to_cfqq(cic
, cfq_bio_sync(bio
));
2078 sector_t sector
= bio
->bi_sector
+ bio_sectors(bio
);
2080 return elv_rb_find(&cfqq
->sort_list
, sector
);
2086 static void cfq_activate_request(struct request_queue
*q
, struct request
*rq
)
2088 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
2090 cfqd
->rq_in_driver
++;
2091 cfq_log_cfqq(cfqd
, RQ_CFQQ(rq
), "activate rq, drv=%d",
2092 cfqd
->rq_in_driver
);
2094 cfqd
->last_position
= blk_rq_pos(rq
) + blk_rq_sectors(rq
);
2097 static void cfq_deactivate_request(struct request_queue
*q
, struct request
*rq
)
2099 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
2101 WARN_ON(!cfqd
->rq_in_driver
);
2102 cfqd
->rq_in_driver
--;
2103 cfq_log_cfqq(cfqd
, RQ_CFQQ(rq
), "deactivate rq, drv=%d",
2104 cfqd
->rq_in_driver
);
2107 static void cfq_remove_request(struct request
*rq
)
2109 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
2111 if (cfqq
->next_rq
== rq
)
2112 cfqq
->next_rq
= cfq_find_next_rq(cfqq
->cfqd
, cfqq
, rq
);
2114 list_del_init(&rq
->queuelist
);
2117 cfqq
->cfqd
->rq_queued
--;
2118 cfqg_stats_update_io_remove(RQ_CFQG(rq
), rq
->cmd_flags
);
2119 if (rq
->cmd_flags
& REQ_PRIO
) {
2120 WARN_ON(!cfqq
->prio_pending
);
2121 cfqq
->prio_pending
--;
2125 static int cfq_merge(struct request_queue
*q
, struct request
**req
,
2128 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
2129 struct request
*__rq
;
2131 __rq
= cfq_find_rq_fmerge(cfqd
, bio
);
2132 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
2134 return ELEVATOR_FRONT_MERGE
;
2137 return ELEVATOR_NO_MERGE
;
2140 static void cfq_merged_request(struct request_queue
*q
, struct request
*req
,
2143 if (type
== ELEVATOR_FRONT_MERGE
) {
2144 struct cfq_queue
*cfqq
= RQ_CFQQ(req
);
2146 cfq_reposition_rq_rb(cfqq
, req
);
2150 static void cfq_bio_merged(struct request_queue
*q
, struct request
*req
,
2153 cfqg_stats_update_io_merged(RQ_CFQG(req
), bio
->bi_rw
);
2157 cfq_merged_requests(struct request_queue
*q
, struct request
*rq
,
2158 struct request
*next
)
2160 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
2161 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
2164 * reposition in fifo if next is older than rq
2166 if (!list_empty(&rq
->queuelist
) && !list_empty(&next
->queuelist
) &&
2167 time_before(rq_fifo_time(next
), rq_fifo_time(rq
)) &&
2168 cfqq
== RQ_CFQQ(next
)) {
2169 list_move(&rq
->queuelist
, &next
->queuelist
);
2170 rq_set_fifo_time(rq
, rq_fifo_time(next
));
2173 if (cfqq
->next_rq
== next
)
2175 cfq_remove_request(next
);
2176 cfqg_stats_update_io_merged(RQ_CFQG(rq
), next
->cmd_flags
);
2178 cfqq
= RQ_CFQQ(next
);
2180 * all requests of this queue are merged to other queues, delete it
2181 * from the service tree. If it's the active_queue,
2182 * cfq_dispatch_requests() will choose to expire it or do idle
2184 if (cfq_cfqq_on_rr(cfqq
) && RB_EMPTY_ROOT(&cfqq
->sort_list
) &&
2185 cfqq
!= cfqd
->active_queue
)
2186 cfq_del_cfqq_rr(cfqd
, cfqq
);
2189 static int cfq_allow_merge(struct request_queue
*q
, struct request
*rq
,
2192 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
2193 struct cfq_io_cq
*cic
;
2194 struct cfq_queue
*cfqq
;
2197 * Disallow merge of a sync bio into an async request.
2199 if (cfq_bio_sync(bio
) && !rq_is_sync(rq
))
2203 * Lookup the cfqq that this bio will be queued with and allow
2204 * merge only if rq is queued there.
2206 cic
= cfq_cic_lookup(cfqd
, current
->io_context
);
2210 cfqq
= cic_to_cfqq(cic
, cfq_bio_sync(bio
));
2211 return cfqq
== RQ_CFQQ(rq
);
2214 static inline void cfq_del_timer(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
2216 del_timer(&cfqd
->idle_slice_timer
);
2217 cfqg_stats_update_idle_time(cfqq
->cfqg
);
2220 static void __cfq_set_active_queue(struct cfq_data
*cfqd
,
2221 struct cfq_queue
*cfqq
)
2224 cfq_log_cfqq(cfqd
, cfqq
, "set_active wl_class:%d wl_type:%d",
2225 cfqd
->serving_wl_class
, cfqd
->serving_wl_type
);
2226 cfqg_stats_update_avg_queue_size(cfqq
->cfqg
);
2227 cfqq
->slice_start
= 0;
2228 cfqq
->dispatch_start
= jiffies
;
2229 cfqq
->allocated_slice
= 0;
2230 cfqq
->slice_end
= 0;
2231 cfqq
->slice_dispatch
= 0;
2232 cfqq
->nr_sectors
= 0;
2234 cfq_clear_cfqq_wait_request(cfqq
);
2235 cfq_clear_cfqq_must_dispatch(cfqq
);
2236 cfq_clear_cfqq_must_alloc_slice(cfqq
);
2237 cfq_clear_cfqq_fifo_expire(cfqq
);
2238 cfq_mark_cfqq_slice_new(cfqq
);
2240 cfq_del_timer(cfqd
, cfqq
);
2243 cfqd
->active_queue
= cfqq
;
2247 * current cfqq expired its slice (or was too idle), select new one
2250 __cfq_slice_expired(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
2253 cfq_log_cfqq(cfqd
, cfqq
, "slice expired t=%d", timed_out
);
2255 if (cfq_cfqq_wait_request(cfqq
))
2256 cfq_del_timer(cfqd
, cfqq
);
2258 cfq_clear_cfqq_wait_request(cfqq
);
2259 cfq_clear_cfqq_wait_busy(cfqq
);
2262 * If this cfqq is shared between multiple processes, check to
2263 * make sure that those processes are still issuing I/Os within
2264 * the mean seek distance. If not, it may be time to break the
2265 * queues apart again.
2267 if (cfq_cfqq_coop(cfqq
) && CFQQ_SEEKY(cfqq
))
2268 cfq_mark_cfqq_split_coop(cfqq
);
2271 * store what was left of this slice, if the queue idled/timed out
2274 if (cfq_cfqq_slice_new(cfqq
))
2275 cfqq
->slice_resid
= cfq_scaled_cfqq_slice(cfqd
, cfqq
);
2277 cfqq
->slice_resid
= cfqq
->slice_end
- jiffies
;
2278 cfq_log_cfqq(cfqd
, cfqq
, "resid=%ld", cfqq
->slice_resid
);
2281 cfq_group_served(cfqd
, cfqq
->cfqg
, cfqq
);
2283 if (cfq_cfqq_on_rr(cfqq
) && RB_EMPTY_ROOT(&cfqq
->sort_list
))
2284 cfq_del_cfqq_rr(cfqd
, cfqq
);
2286 cfq_resort_rr_list(cfqd
, cfqq
);
2288 if (cfqq
== cfqd
->active_queue
)
2289 cfqd
->active_queue
= NULL
;
2291 if (cfqd
->active_cic
) {
2292 put_io_context(cfqd
->active_cic
->icq
.ioc
);
2293 cfqd
->active_cic
= NULL
;
2297 static inline void cfq_slice_expired(struct cfq_data
*cfqd
, bool timed_out
)
2299 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
2302 __cfq_slice_expired(cfqd
, cfqq
, timed_out
);
2306 * Get next queue for service. Unless we have a queue preemption,
2307 * we'll simply select the first cfqq in the service tree.
2309 static struct cfq_queue
*cfq_get_next_queue(struct cfq_data
*cfqd
)
2311 struct cfq_rb_root
*st
= st_for(cfqd
->serving_group
,
2312 cfqd
->serving_wl_class
, cfqd
->serving_wl_type
);
2314 if (!cfqd
->rq_queued
)
2317 /* There is nothing to dispatch */
2320 if (RB_EMPTY_ROOT(&st
->rb
))
2322 return cfq_rb_first(st
);
2325 static struct cfq_queue
*cfq_get_next_queue_forced(struct cfq_data
*cfqd
)
2327 struct cfq_group
*cfqg
;
2328 struct cfq_queue
*cfqq
;
2330 struct cfq_rb_root
*st
;
2332 if (!cfqd
->rq_queued
)
2335 cfqg
= cfq_get_next_cfqg(cfqd
);
2339 for_each_cfqg_st(cfqg
, i
, j
, st
)
2340 if ((cfqq
= cfq_rb_first(st
)) != NULL
)
2346 * Get and set a new active queue for service.
2348 static struct cfq_queue
*cfq_set_active_queue(struct cfq_data
*cfqd
,
2349 struct cfq_queue
*cfqq
)
2352 cfqq
= cfq_get_next_queue(cfqd
);
2354 __cfq_set_active_queue(cfqd
, cfqq
);
2358 static inline sector_t
cfq_dist_from_last(struct cfq_data
*cfqd
,
2361 if (blk_rq_pos(rq
) >= cfqd
->last_position
)
2362 return blk_rq_pos(rq
) - cfqd
->last_position
;
2364 return cfqd
->last_position
- blk_rq_pos(rq
);
2367 static inline int cfq_rq_close(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
2370 return cfq_dist_from_last(cfqd
, rq
) <= CFQQ_CLOSE_THR
;
2373 static struct cfq_queue
*cfqq_close(struct cfq_data
*cfqd
,
2374 struct cfq_queue
*cur_cfqq
)
2376 struct rb_root
*root
= &cfqd
->prio_trees
[cur_cfqq
->org_ioprio
];
2377 struct rb_node
*parent
, *node
;
2378 struct cfq_queue
*__cfqq
;
2379 sector_t sector
= cfqd
->last_position
;
2381 if (RB_EMPTY_ROOT(root
))
2385 * First, if we find a request starting at the end of the last
2386 * request, choose it.
2388 __cfqq
= cfq_prio_tree_lookup(cfqd
, root
, sector
, &parent
, NULL
);
2393 * If the exact sector wasn't found, the parent of the NULL leaf
2394 * will contain the closest sector.
2396 __cfqq
= rb_entry(parent
, struct cfq_queue
, p_node
);
2397 if (cfq_rq_close(cfqd
, cur_cfqq
, __cfqq
->next_rq
))
2400 if (blk_rq_pos(__cfqq
->next_rq
) < sector
)
2401 node
= rb_next(&__cfqq
->p_node
);
2403 node
= rb_prev(&__cfqq
->p_node
);
2407 __cfqq
= rb_entry(node
, struct cfq_queue
, p_node
);
2408 if (cfq_rq_close(cfqd
, cur_cfqq
, __cfqq
->next_rq
))
2416 * cur_cfqq - passed in so that we don't decide that the current queue is
2417 * closely cooperating with itself.
2419 * So, basically we're assuming that that cur_cfqq has dispatched at least
2420 * one request, and that cfqd->last_position reflects a position on the disk
2421 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
2424 static struct cfq_queue
*cfq_close_cooperator(struct cfq_data
*cfqd
,
2425 struct cfq_queue
*cur_cfqq
)
2427 struct cfq_queue
*cfqq
;
2429 if (cfq_class_idle(cur_cfqq
))
2431 if (!cfq_cfqq_sync(cur_cfqq
))
2433 if (CFQQ_SEEKY(cur_cfqq
))
2437 * Don't search priority tree if it's the only queue in the group.
2439 if (cur_cfqq
->cfqg
->nr_cfqq
== 1)
2443 * We should notice if some of the queues are cooperating, eg
2444 * working closely on the same area of the disk. In that case,
2445 * we can group them together and don't waste time idling.
2447 cfqq
= cfqq_close(cfqd
, cur_cfqq
);
2451 /* If new queue belongs to different cfq_group, don't choose it */
2452 if (cur_cfqq
->cfqg
!= cfqq
->cfqg
)
2456 * It only makes sense to merge sync queues.
2458 if (!cfq_cfqq_sync(cfqq
))
2460 if (CFQQ_SEEKY(cfqq
))
2464 * Do not merge queues of different priority classes
2466 if (cfq_class_rt(cfqq
) != cfq_class_rt(cur_cfqq
))
2473 * Determine whether we should enforce idle window for this queue.
2476 static bool cfq_should_idle(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
2478 enum wl_class_t wl_class
= cfqq_class(cfqq
);
2479 struct cfq_rb_root
*st
= cfqq
->service_tree
;
2484 if (!cfqd
->cfq_slice_idle
)
2487 /* We never do for idle class queues. */
2488 if (wl_class
== IDLE_WORKLOAD
)
2491 /* We do for queues that were marked with idle window flag. */
2492 if (cfq_cfqq_idle_window(cfqq
) &&
2493 !(blk_queue_nonrot(cfqd
->queue
) && cfqd
->hw_tag
))
2497 * Otherwise, we do only if they are the last ones
2498 * in their service tree.
2500 if (st
->count
== 1 && cfq_cfqq_sync(cfqq
) &&
2501 !cfq_io_thinktime_big(cfqd
, &st
->ttime
, false))
2503 cfq_log_cfqq(cfqd
, cfqq
, "Not idling. st->count:%d", st
->count
);
2507 static void cfq_arm_slice_timer(struct cfq_data
*cfqd
)
2509 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
2510 struct cfq_io_cq
*cic
;
2511 unsigned long sl
, group_idle
= 0;
2514 * SSD device without seek penalty, disable idling. But only do so
2515 * for devices that support queuing, otherwise we still have a problem
2516 * with sync vs async workloads.
2518 if (blk_queue_nonrot(cfqd
->queue
) && cfqd
->hw_tag
)
2521 WARN_ON(!RB_EMPTY_ROOT(&cfqq
->sort_list
));
2522 WARN_ON(cfq_cfqq_slice_new(cfqq
));
2525 * idle is disabled, either manually or by past process history
2527 if (!cfq_should_idle(cfqd
, cfqq
)) {
2528 /* no queue idling. Check for group idling */
2529 if (cfqd
->cfq_group_idle
)
2530 group_idle
= cfqd
->cfq_group_idle
;
2536 * still active requests from this queue, don't idle
2538 if (cfqq
->dispatched
)
2542 * task has exited, don't wait
2544 cic
= cfqd
->active_cic
;
2545 if (!cic
|| !atomic_read(&cic
->icq
.ioc
->active_ref
))
2549 * If our average think time is larger than the remaining time
2550 * slice, then don't idle. This avoids overrunning the allotted
2553 if (sample_valid(cic
->ttime
.ttime_samples
) &&
2554 (cfqq
->slice_end
- jiffies
< cic
->ttime
.ttime_mean
)) {
2555 cfq_log_cfqq(cfqd
, cfqq
, "Not idling. think_time:%lu",
2556 cic
->ttime
.ttime_mean
);
2560 /* There are other queues in the group, don't do group idle */
2561 if (group_idle
&& cfqq
->cfqg
->nr_cfqq
> 1)
2564 cfq_mark_cfqq_wait_request(cfqq
);
2567 sl
= cfqd
->cfq_group_idle
;
2569 sl
= cfqd
->cfq_slice_idle
;
2571 mod_timer(&cfqd
->idle_slice_timer
, jiffies
+ sl
);
2572 cfqg_stats_set_start_idle_time(cfqq
->cfqg
);
2573 cfq_log_cfqq(cfqd
, cfqq
, "arm_idle: %lu group_idle: %d", sl
,
2574 group_idle
? 1 : 0);
2578 * Move request from internal lists to the request queue dispatch list.
2580 static void cfq_dispatch_insert(struct request_queue
*q
, struct request
*rq
)
2582 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
2583 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
2585 cfq_log_cfqq(cfqd
, cfqq
, "dispatch_insert");
2587 cfqq
->next_rq
= cfq_find_next_rq(cfqd
, cfqq
, rq
);
2588 cfq_remove_request(rq
);
2590 (RQ_CFQG(rq
))->dispatched
++;
2591 elv_dispatch_sort(q
, rq
);
2593 cfqd
->rq_in_flight
[cfq_cfqq_sync(cfqq
)]++;
2594 cfqq
->nr_sectors
+= blk_rq_sectors(rq
);
2595 cfqg_stats_update_dispatch(cfqq
->cfqg
, blk_rq_bytes(rq
), rq
->cmd_flags
);
2599 * return expired entry, or NULL to just start from scratch in rbtree
2601 static struct request
*cfq_check_fifo(struct cfq_queue
*cfqq
)
2603 struct request
*rq
= NULL
;
2605 if (cfq_cfqq_fifo_expire(cfqq
))
2608 cfq_mark_cfqq_fifo_expire(cfqq
);
2610 if (list_empty(&cfqq
->fifo
))
2613 rq
= rq_entry_fifo(cfqq
->fifo
.next
);
2614 if (time_before(jiffies
, rq_fifo_time(rq
)))
2617 cfq_log_cfqq(cfqq
->cfqd
, cfqq
, "fifo=%p", rq
);
2622 cfq_prio_to_maxrq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
2624 const int base_rq
= cfqd
->cfq_slice_async_rq
;
2626 WARN_ON(cfqq
->ioprio
>= IOPRIO_BE_NR
);
2628 return 2 * base_rq
* (IOPRIO_BE_NR
- cfqq
->ioprio
);
2632 * Must be called with the queue_lock held.
2634 static int cfqq_process_refs(struct cfq_queue
*cfqq
)
2636 int process_refs
, io_refs
;
2638 io_refs
= cfqq
->allocated
[READ
] + cfqq
->allocated
[WRITE
];
2639 process_refs
= cfqq
->ref
- io_refs
;
2640 BUG_ON(process_refs
< 0);
2641 return process_refs
;
2644 static void cfq_setup_merge(struct cfq_queue
*cfqq
, struct cfq_queue
*new_cfqq
)
2646 int process_refs
, new_process_refs
;
2647 struct cfq_queue
*__cfqq
;
2650 * If there are no process references on the new_cfqq, then it is
2651 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
2652 * chain may have dropped their last reference (not just their
2653 * last process reference).
2655 if (!cfqq_process_refs(new_cfqq
))
2658 /* Avoid a circular list and skip interim queue merges */
2659 while ((__cfqq
= new_cfqq
->new_cfqq
)) {
2665 process_refs
= cfqq_process_refs(cfqq
);
2666 new_process_refs
= cfqq_process_refs(new_cfqq
);
2668 * If the process for the cfqq has gone away, there is no
2669 * sense in merging the queues.
2671 if (process_refs
== 0 || new_process_refs
== 0)
2675 * Merge in the direction of the lesser amount of work.
2677 if (new_process_refs
>= process_refs
) {
2678 cfqq
->new_cfqq
= new_cfqq
;
2679 new_cfqq
->ref
+= process_refs
;
2681 new_cfqq
->new_cfqq
= cfqq
;
2682 cfqq
->ref
+= new_process_refs
;
2686 static enum wl_type_t
cfq_choose_wl_type(struct cfq_data
*cfqd
,
2687 struct cfq_group
*cfqg
, enum wl_class_t wl_class
)
2689 struct cfq_queue
*queue
;
2691 bool key_valid
= false;
2692 unsigned long lowest_key
= 0;
2693 enum wl_type_t cur_best
= SYNC_NOIDLE_WORKLOAD
;
2695 for (i
= 0; i
<= SYNC_WORKLOAD
; ++i
) {
2696 /* select the one with lowest rb_key */
2697 queue
= cfq_rb_first(st_for(cfqg
, wl_class
, i
));
2699 (!key_valid
|| time_before(queue
->rb_key
, lowest_key
))) {
2700 lowest_key
= queue
->rb_key
;
2710 choose_wl_class_and_type(struct cfq_data
*cfqd
, struct cfq_group
*cfqg
)
2714 struct cfq_rb_root
*st
;
2715 unsigned group_slice
;
2716 enum wl_class_t original_class
= cfqd
->serving_wl_class
;
2718 /* Choose next priority. RT > BE > IDLE */
2719 if (cfq_group_busy_queues_wl(RT_WORKLOAD
, cfqd
, cfqg
))
2720 cfqd
->serving_wl_class
= RT_WORKLOAD
;
2721 else if (cfq_group_busy_queues_wl(BE_WORKLOAD
, cfqd
, cfqg
))
2722 cfqd
->serving_wl_class
= BE_WORKLOAD
;
2724 cfqd
->serving_wl_class
= IDLE_WORKLOAD
;
2725 cfqd
->workload_expires
= jiffies
+ 1;
2729 if (original_class
!= cfqd
->serving_wl_class
)
2733 * For RT and BE, we have to choose also the type
2734 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
2737 st
= st_for(cfqg
, cfqd
->serving_wl_class
, cfqd
->serving_wl_type
);
2741 * check workload expiration, and that we still have other queues ready
2743 if (count
&& !time_after(jiffies
, cfqd
->workload_expires
))
2747 /* otherwise select new workload type */
2748 cfqd
->serving_wl_type
= cfq_choose_wl_type(cfqd
, cfqg
,
2749 cfqd
->serving_wl_class
);
2750 st
= st_for(cfqg
, cfqd
->serving_wl_class
, cfqd
->serving_wl_type
);
2754 * the workload slice is computed as a fraction of target latency
2755 * proportional to the number of queues in that workload, over
2756 * all the queues in the same priority class
2758 group_slice
= cfq_group_slice(cfqd
, cfqg
);
2760 slice
= group_slice
* count
/
2761 max_t(unsigned, cfqg
->busy_queues_avg
[cfqd
->serving_wl_class
],
2762 cfq_group_busy_queues_wl(cfqd
->serving_wl_class
, cfqd
,
2765 if (cfqd
->serving_wl_type
== ASYNC_WORKLOAD
) {
2769 * Async queues are currently system wide. Just taking
2770 * proportion of queues with-in same group will lead to higher
2771 * async ratio system wide as generally root group is going
2772 * to have higher weight. A more accurate thing would be to
2773 * calculate system wide asnc/sync ratio.
2775 tmp
= cfqd
->cfq_target_latency
*
2776 cfqg_busy_async_queues(cfqd
, cfqg
);
2777 tmp
= tmp
/cfqd
->busy_queues
;
2778 slice
= min_t(unsigned, slice
, tmp
);
2780 /* async workload slice is scaled down according to
2781 * the sync/async slice ratio. */
2782 slice
= slice
* cfqd
->cfq_slice
[0] / cfqd
->cfq_slice
[1];
2784 /* sync workload slice is at least 2 * cfq_slice_idle */
2785 slice
= max(slice
, 2 * cfqd
->cfq_slice_idle
);
2787 slice
= max_t(unsigned, slice
, CFQ_MIN_TT
);
2788 cfq_log(cfqd
, "workload slice:%d", slice
);
2789 cfqd
->workload_expires
= jiffies
+ slice
;
2792 static struct cfq_group
*cfq_get_next_cfqg(struct cfq_data
*cfqd
)
2794 struct cfq_rb_root
*st
= &cfqd
->grp_service_tree
;
2795 struct cfq_group
*cfqg
;
2797 if (RB_EMPTY_ROOT(&st
->rb
))
2799 cfqg
= cfq_rb_first_group(st
);
2800 update_min_vdisktime(st
);
2804 static void cfq_choose_cfqg(struct cfq_data
*cfqd
)
2806 struct cfq_group
*cfqg
= cfq_get_next_cfqg(cfqd
);
2808 cfqd
->serving_group
= cfqg
;
2810 /* Restore the workload type data */
2811 if (cfqg
->saved_wl_slice
) {
2812 cfqd
->workload_expires
= jiffies
+ cfqg
->saved_wl_slice
;
2813 cfqd
->serving_wl_type
= cfqg
->saved_wl_type
;
2814 cfqd
->serving_wl_class
= cfqg
->saved_wl_class
;
2816 cfqd
->workload_expires
= jiffies
- 1;
2818 choose_wl_class_and_type(cfqd
, cfqg
);
2822 * Select a queue for service. If we have a current active queue,
2823 * check whether to continue servicing it, or retrieve and set a new one.
2825 static struct cfq_queue
*cfq_select_queue(struct cfq_data
*cfqd
)
2827 struct cfq_queue
*cfqq
, *new_cfqq
= NULL
;
2829 cfqq
= cfqd
->active_queue
;
2833 if (!cfqd
->rq_queued
)
2837 * We were waiting for group to get backlogged. Expire the queue
2839 if (cfq_cfqq_wait_busy(cfqq
) && !RB_EMPTY_ROOT(&cfqq
->sort_list
))
2843 * The active queue has run out of time, expire it and select new.
2845 if (cfq_slice_used(cfqq
) && !cfq_cfqq_must_dispatch(cfqq
)) {
2847 * If slice had not expired at the completion of last request
2848 * we might not have turned on wait_busy flag. Don't expire
2849 * the queue yet. Allow the group to get backlogged.
2851 * The very fact that we have used the slice, that means we
2852 * have been idling all along on this queue and it should be
2853 * ok to wait for this request to complete.
2855 if (cfqq
->cfqg
->nr_cfqq
== 1 && RB_EMPTY_ROOT(&cfqq
->sort_list
)
2856 && cfqq
->dispatched
&& cfq_should_idle(cfqd
, cfqq
)) {
2860 goto check_group_idle
;
2864 * The active queue has requests and isn't expired, allow it to
2867 if (!RB_EMPTY_ROOT(&cfqq
->sort_list
))
2871 * If another queue has a request waiting within our mean seek
2872 * distance, let it run. The expire code will check for close
2873 * cooperators and put the close queue at the front of the service
2874 * tree. If possible, merge the expiring queue with the new cfqq.
2876 new_cfqq
= cfq_close_cooperator(cfqd
, cfqq
);
2878 if (!cfqq
->new_cfqq
)
2879 cfq_setup_merge(cfqq
, new_cfqq
);
2884 * No requests pending. If the active queue still has requests in
2885 * flight or is idling for a new request, allow either of these
2886 * conditions to happen (or time out) before selecting a new queue.
2888 if (timer_pending(&cfqd
->idle_slice_timer
)) {
2894 * This is a deep seek queue, but the device is much faster than
2895 * the queue can deliver, don't idle
2897 if (CFQQ_SEEKY(cfqq
) && cfq_cfqq_idle_window(cfqq
) &&
2898 (cfq_cfqq_slice_new(cfqq
) ||
2899 (cfqq
->slice_end
- jiffies
> jiffies
- cfqq
->slice_start
))) {
2900 cfq_clear_cfqq_deep(cfqq
);
2901 cfq_clear_cfqq_idle_window(cfqq
);
2904 if (cfqq
->dispatched
&& cfq_should_idle(cfqd
, cfqq
)) {
2910 * If group idle is enabled and there are requests dispatched from
2911 * this group, wait for requests to complete.
2914 if (cfqd
->cfq_group_idle
&& cfqq
->cfqg
->nr_cfqq
== 1 &&
2915 cfqq
->cfqg
->dispatched
&&
2916 !cfq_io_thinktime_big(cfqd
, &cfqq
->cfqg
->ttime
, true)) {
2922 cfq_slice_expired(cfqd
, 0);
2925 * Current queue expired. Check if we have to switch to a new
2929 cfq_choose_cfqg(cfqd
);
2931 cfqq
= cfq_set_active_queue(cfqd
, new_cfqq
);
2936 static int __cfq_forced_dispatch_cfqq(struct cfq_queue
*cfqq
)
2940 while (cfqq
->next_rq
) {
2941 cfq_dispatch_insert(cfqq
->cfqd
->queue
, cfqq
->next_rq
);
2945 BUG_ON(!list_empty(&cfqq
->fifo
));
2947 /* By default cfqq is not expired if it is empty. Do it explicitly */
2948 __cfq_slice_expired(cfqq
->cfqd
, cfqq
, 0);
2953 * Drain our current requests. Used for barriers and when switching
2954 * io schedulers on-the-fly.
2956 static int cfq_forced_dispatch(struct cfq_data
*cfqd
)
2958 struct cfq_queue
*cfqq
;
2961 /* Expire the timeslice of the current active queue first */
2962 cfq_slice_expired(cfqd
, 0);
2963 while ((cfqq
= cfq_get_next_queue_forced(cfqd
)) != NULL
) {
2964 __cfq_set_active_queue(cfqd
, cfqq
);
2965 dispatched
+= __cfq_forced_dispatch_cfqq(cfqq
);
2968 BUG_ON(cfqd
->busy_queues
);
2970 cfq_log(cfqd
, "forced_dispatch=%d", dispatched
);
2974 static inline bool cfq_slice_used_soon(struct cfq_data
*cfqd
,
2975 struct cfq_queue
*cfqq
)
2977 /* the queue hasn't finished any request, can't estimate */
2978 if (cfq_cfqq_slice_new(cfqq
))
2980 if (time_after(jiffies
+ cfqd
->cfq_slice_idle
* cfqq
->dispatched
,
2987 static bool cfq_may_dispatch(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
2989 unsigned int max_dispatch
;
2992 * Drain async requests before we start sync IO
2994 if (cfq_should_idle(cfqd
, cfqq
) && cfqd
->rq_in_flight
[BLK_RW_ASYNC
])
2998 * If this is an async queue and we have sync IO in flight, let it wait
3000 if (cfqd
->rq_in_flight
[BLK_RW_SYNC
] && !cfq_cfqq_sync(cfqq
))
3003 max_dispatch
= max_t(unsigned int, cfqd
->cfq_quantum
/ 2, 1);
3004 if (cfq_class_idle(cfqq
))
3008 * Does this cfqq already have too much IO in flight?
3010 if (cfqq
->dispatched
>= max_dispatch
) {
3011 bool promote_sync
= false;
3013 * idle queue must always only have a single IO in flight
3015 if (cfq_class_idle(cfqq
))
3019 * If there is only one sync queue
3020 * we can ignore async queue here and give the sync
3021 * queue no dispatch limit. The reason is a sync queue can
3022 * preempt async queue, limiting the sync queue doesn't make
3023 * sense. This is useful for aiostress test.
3025 if (cfq_cfqq_sync(cfqq
) && cfqd
->busy_sync_queues
== 1)
3026 promote_sync
= true;
3029 * We have other queues, don't allow more IO from this one
3031 if (cfqd
->busy_queues
> 1 && cfq_slice_used_soon(cfqd
, cfqq
) &&
3036 * Sole queue user, no limit
3038 if (cfqd
->busy_queues
== 1 || promote_sync
)
3042 * Normally we start throttling cfqq when cfq_quantum/2
3043 * requests have been dispatched. But we can drive
3044 * deeper queue depths at the beginning of slice
3045 * subjected to upper limit of cfq_quantum.
3047 max_dispatch
= cfqd
->cfq_quantum
;
3051 * Async queues must wait a bit before being allowed dispatch.
3052 * We also ramp up the dispatch depth gradually for async IO,
3053 * based on the last sync IO we serviced
3055 if (!cfq_cfqq_sync(cfqq
) && cfqd
->cfq_latency
) {
3056 unsigned long last_sync
= jiffies
- cfqd
->last_delayed_sync
;
3059 depth
= last_sync
/ cfqd
->cfq_slice
[1];
3060 if (!depth
&& !cfqq
->dispatched
)
3062 if (depth
< max_dispatch
)
3063 max_dispatch
= depth
;
3067 * If we're below the current max, allow a dispatch
3069 return cfqq
->dispatched
< max_dispatch
;
3073 * Dispatch a request from cfqq, moving them to the request queue
3076 static bool cfq_dispatch_request(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
3080 BUG_ON(RB_EMPTY_ROOT(&cfqq
->sort_list
));
3082 if (!cfq_may_dispatch(cfqd
, cfqq
))
3086 * follow expired path, else get first next available
3088 rq
= cfq_check_fifo(cfqq
);
3093 * insert request into driver dispatch list
3095 cfq_dispatch_insert(cfqd
->queue
, rq
);
3097 if (!cfqd
->active_cic
) {
3098 struct cfq_io_cq
*cic
= RQ_CIC(rq
);
3100 atomic_long_inc(&cic
->icq
.ioc
->refcount
);
3101 cfqd
->active_cic
= cic
;
3108 * Find the cfqq that we need to service and move a request from that to the
3111 static int cfq_dispatch_requests(struct request_queue
*q
, int force
)
3113 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
3114 struct cfq_queue
*cfqq
;
3116 if (!cfqd
->busy_queues
)
3119 if (unlikely(force
))
3120 return cfq_forced_dispatch(cfqd
);
3122 cfqq
= cfq_select_queue(cfqd
);
3127 * Dispatch a request from this cfqq, if it is allowed
3129 if (!cfq_dispatch_request(cfqd
, cfqq
))
3132 cfqq
->slice_dispatch
++;
3133 cfq_clear_cfqq_must_dispatch(cfqq
);
3136 * expire an async queue immediately if it has used up its slice. idle
3137 * queue always expire after 1 dispatch round.
3139 if (cfqd
->busy_queues
> 1 && ((!cfq_cfqq_sync(cfqq
) &&
3140 cfqq
->slice_dispatch
>= cfq_prio_to_maxrq(cfqd
, cfqq
)) ||
3141 cfq_class_idle(cfqq
))) {
3142 cfqq
->slice_end
= jiffies
+ 1;
3143 cfq_slice_expired(cfqd
, 0);
3146 cfq_log_cfqq(cfqd
, cfqq
, "dispatched a request");
3151 * task holds one reference to the queue, dropped when task exits. each rq
3152 * in-flight on this queue also holds a reference, dropped when rq is freed.
3154 * Each cfq queue took a reference on the parent group. Drop it now.
3155 * queue lock must be held here.
3157 static void cfq_put_queue(struct cfq_queue
*cfqq
)
3159 struct cfq_data
*cfqd
= cfqq
->cfqd
;
3160 struct cfq_group
*cfqg
;
3162 BUG_ON(cfqq
->ref
<= 0);
3168 cfq_log_cfqq(cfqd
, cfqq
, "put_queue");
3169 BUG_ON(rb_first(&cfqq
->sort_list
));
3170 BUG_ON(cfqq
->allocated
[READ
] + cfqq
->allocated
[WRITE
]);
3173 if (unlikely(cfqd
->active_queue
== cfqq
)) {
3174 __cfq_slice_expired(cfqd
, cfqq
, 0);
3175 cfq_schedule_dispatch(cfqd
);
3178 BUG_ON(cfq_cfqq_on_rr(cfqq
));
3179 kmem_cache_free(cfq_pool
, cfqq
);
3183 static void cfq_put_cooperator(struct cfq_queue
*cfqq
)
3185 struct cfq_queue
*__cfqq
, *next
;
3188 * If this queue was scheduled to merge with another queue, be
3189 * sure to drop the reference taken on that queue (and others in
3190 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
3192 __cfqq
= cfqq
->new_cfqq
;
3194 if (__cfqq
== cfqq
) {
3195 WARN(1, "cfqq->new_cfqq loop detected\n");
3198 next
= __cfqq
->new_cfqq
;
3199 cfq_put_queue(__cfqq
);
3204 static void cfq_exit_cfqq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
3206 if (unlikely(cfqq
== cfqd
->active_queue
)) {
3207 __cfq_slice_expired(cfqd
, cfqq
, 0);
3208 cfq_schedule_dispatch(cfqd
);
3211 cfq_put_cooperator(cfqq
);
3213 cfq_put_queue(cfqq
);
3216 static void cfq_init_icq(struct io_cq
*icq
)
3218 struct cfq_io_cq
*cic
= icq_to_cic(icq
);
3220 cic
->ttime
.last_end_request
= jiffies
;
3223 static void cfq_exit_icq(struct io_cq
*icq
)
3225 struct cfq_io_cq
*cic
= icq_to_cic(icq
);
3226 struct cfq_data
*cfqd
= cic_to_cfqd(cic
);
3228 if (cic
->cfqq
[BLK_RW_ASYNC
]) {
3229 cfq_exit_cfqq(cfqd
, cic
->cfqq
[BLK_RW_ASYNC
]);
3230 cic
->cfqq
[BLK_RW_ASYNC
] = NULL
;
3233 if (cic
->cfqq
[BLK_RW_SYNC
]) {
3234 cfq_exit_cfqq(cfqd
, cic
->cfqq
[BLK_RW_SYNC
]);
3235 cic
->cfqq
[BLK_RW_SYNC
] = NULL
;
3239 static void cfq_init_prio_data(struct cfq_queue
*cfqq
, struct cfq_io_cq
*cic
)
3241 struct task_struct
*tsk
= current
;
3244 if (!cfq_cfqq_prio_changed(cfqq
))
3247 ioprio_class
= IOPRIO_PRIO_CLASS(cic
->ioprio
);
3248 switch (ioprio_class
) {
3250 printk(KERN_ERR
"cfq: bad prio %x\n", ioprio_class
);
3251 case IOPRIO_CLASS_NONE
:
3253 * no prio set, inherit CPU scheduling settings
3255 cfqq
->ioprio
= task_nice_ioprio(tsk
);
3256 cfqq
->ioprio_class
= task_nice_ioclass(tsk
);
3258 case IOPRIO_CLASS_RT
:
3259 cfqq
->ioprio
= IOPRIO_PRIO_DATA(cic
->ioprio
);
3260 cfqq
->ioprio_class
= IOPRIO_CLASS_RT
;
3262 case IOPRIO_CLASS_BE
:
3263 cfqq
->ioprio
= IOPRIO_PRIO_DATA(cic
->ioprio
);
3264 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
3266 case IOPRIO_CLASS_IDLE
:
3267 cfqq
->ioprio_class
= IOPRIO_CLASS_IDLE
;
3269 cfq_clear_cfqq_idle_window(cfqq
);
3274 * keep track of original prio settings in case we have to temporarily
3275 * elevate the priority of this queue
3277 cfqq
->org_ioprio
= cfqq
->ioprio
;
3278 cfq_clear_cfqq_prio_changed(cfqq
);
3281 static void check_ioprio_changed(struct cfq_io_cq
*cic
, struct bio
*bio
)
3283 int ioprio
= cic
->icq
.ioc
->ioprio
;
3284 struct cfq_data
*cfqd
= cic_to_cfqd(cic
);
3285 struct cfq_queue
*cfqq
;
3288 * Check whether ioprio has changed. The condition may trigger
3289 * spuriously on a newly created cic but there's no harm.
3291 if (unlikely(!cfqd
) || likely(cic
->ioprio
== ioprio
))
3294 cfqq
= cic
->cfqq
[BLK_RW_ASYNC
];
3296 struct cfq_queue
*new_cfqq
;
3297 new_cfqq
= cfq_get_queue(cfqd
, BLK_RW_ASYNC
, cic
, bio
,
3300 cic
->cfqq
[BLK_RW_ASYNC
] = new_cfqq
;
3301 cfq_put_queue(cfqq
);
3305 cfqq
= cic
->cfqq
[BLK_RW_SYNC
];
3307 cfq_mark_cfqq_prio_changed(cfqq
);
3309 cic
->ioprio
= ioprio
;
3312 static void cfq_init_cfqq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
3313 pid_t pid
, bool is_sync
)
3315 RB_CLEAR_NODE(&cfqq
->rb_node
);
3316 RB_CLEAR_NODE(&cfqq
->p_node
);
3317 INIT_LIST_HEAD(&cfqq
->fifo
);
3322 cfq_mark_cfqq_prio_changed(cfqq
);
3325 if (!cfq_class_idle(cfqq
))
3326 cfq_mark_cfqq_idle_window(cfqq
);
3327 cfq_mark_cfqq_sync(cfqq
);
3332 #ifdef CONFIG_CFQ_GROUP_IOSCHED
3333 static void check_blkcg_changed(struct cfq_io_cq
*cic
, struct bio
*bio
)
3335 struct cfq_data
*cfqd
= cic_to_cfqd(cic
);
3336 struct cfq_queue
*sync_cfqq
;
3340 id
= bio_blkcg(bio
)->id
;
3344 * Check whether blkcg has changed. The condition may trigger
3345 * spuriously on a newly created cic but there's no harm.
3347 if (unlikely(!cfqd
) || likely(cic
->blkcg_id
== id
))
3350 sync_cfqq
= cic_to_cfqq(cic
, 1);
3353 * Drop reference to sync queue. A new sync queue will be
3354 * assigned in new group upon arrival of a fresh request.
3356 cfq_log_cfqq(cfqd
, sync_cfqq
, "changed cgroup");
3357 cic_set_cfqq(cic
, NULL
, 1);
3358 cfq_put_queue(sync_cfqq
);
3364 static inline void check_blkcg_changed(struct cfq_io_cq
*cic
, struct bio
*bio
) { }
3365 #endif /* CONFIG_CFQ_GROUP_IOSCHED */
3367 static struct cfq_queue
*
3368 cfq_find_alloc_queue(struct cfq_data
*cfqd
, bool is_sync
, struct cfq_io_cq
*cic
,
3369 struct bio
*bio
, gfp_t gfp_mask
)
3371 struct blkcg
*blkcg
;
3372 struct cfq_queue
*cfqq
, *new_cfqq
= NULL
;
3373 struct cfq_group
*cfqg
;
3378 blkcg
= bio_blkcg(bio
);
3379 cfqg
= cfq_lookup_create_cfqg(cfqd
, blkcg
);
3380 cfqq
= cic_to_cfqq(cic
, is_sync
);
3383 * Always try a new alloc if we fell back to the OOM cfqq
3384 * originally, since it should just be a temporary situation.
3386 if (!cfqq
|| cfqq
== &cfqd
->oom_cfqq
) {
3391 } else if (gfp_mask
& __GFP_WAIT
) {
3393 spin_unlock_irq(cfqd
->queue
->queue_lock
);
3394 new_cfqq
= kmem_cache_alloc_node(cfq_pool
,
3395 gfp_mask
| __GFP_ZERO
,
3397 spin_lock_irq(cfqd
->queue
->queue_lock
);
3401 cfqq
= kmem_cache_alloc_node(cfq_pool
,
3402 gfp_mask
| __GFP_ZERO
,
3407 cfq_init_cfqq(cfqd
, cfqq
, current
->pid
, is_sync
);
3408 cfq_init_prio_data(cfqq
, cic
);
3409 cfq_link_cfqq_cfqg(cfqq
, cfqg
);
3410 cfq_log_cfqq(cfqd
, cfqq
, "alloced");
3412 cfqq
= &cfqd
->oom_cfqq
;
3416 kmem_cache_free(cfq_pool
, new_cfqq
);
3422 static struct cfq_queue
**
3423 cfq_async_queue_prio(struct cfq_data
*cfqd
, int ioprio_class
, int ioprio
)
3425 switch (ioprio_class
) {
3426 case IOPRIO_CLASS_RT
:
3427 return &cfqd
->async_cfqq
[0][ioprio
];
3428 case IOPRIO_CLASS_NONE
:
3429 ioprio
= IOPRIO_NORM
;
3431 case IOPRIO_CLASS_BE
:
3432 return &cfqd
->async_cfqq
[1][ioprio
];
3433 case IOPRIO_CLASS_IDLE
:
3434 return &cfqd
->async_idle_cfqq
;
3440 static struct cfq_queue
*
3441 cfq_get_queue(struct cfq_data
*cfqd
, bool is_sync
, struct cfq_io_cq
*cic
,
3442 struct bio
*bio
, gfp_t gfp_mask
)
3444 const int ioprio_class
= IOPRIO_PRIO_CLASS(cic
->ioprio
);
3445 const int ioprio
= IOPRIO_PRIO_DATA(cic
->ioprio
);
3446 struct cfq_queue
**async_cfqq
= NULL
;
3447 struct cfq_queue
*cfqq
= NULL
;
3450 async_cfqq
= cfq_async_queue_prio(cfqd
, ioprio_class
, ioprio
);
3455 cfqq
= cfq_find_alloc_queue(cfqd
, is_sync
, cic
, bio
, gfp_mask
);
3458 * pin the queue now that it's allocated, scheduler exit will prune it
3460 if (!is_sync
&& !(*async_cfqq
)) {
3470 __cfq_update_io_thinktime(struct cfq_ttime
*ttime
, unsigned long slice_idle
)
3472 unsigned long elapsed
= jiffies
- ttime
->last_end_request
;
3473 elapsed
= min(elapsed
, 2UL * slice_idle
);
3475 ttime
->ttime_samples
= (7*ttime
->ttime_samples
+ 256) / 8;
3476 ttime
->ttime_total
= (7*ttime
->ttime_total
+ 256*elapsed
) / 8;
3477 ttime
->ttime_mean
= (ttime
->ttime_total
+ 128) / ttime
->ttime_samples
;
3481 cfq_update_io_thinktime(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
3482 struct cfq_io_cq
*cic
)
3484 if (cfq_cfqq_sync(cfqq
)) {
3485 __cfq_update_io_thinktime(&cic
->ttime
, cfqd
->cfq_slice_idle
);
3486 __cfq_update_io_thinktime(&cfqq
->service_tree
->ttime
,
3487 cfqd
->cfq_slice_idle
);
3489 #ifdef CONFIG_CFQ_GROUP_IOSCHED
3490 __cfq_update_io_thinktime(&cfqq
->cfqg
->ttime
, cfqd
->cfq_group_idle
);
3495 cfq_update_io_seektime(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
3499 sector_t n_sec
= blk_rq_sectors(rq
);
3500 if (cfqq
->last_request_pos
) {
3501 if (cfqq
->last_request_pos
< blk_rq_pos(rq
))
3502 sdist
= blk_rq_pos(rq
) - cfqq
->last_request_pos
;
3504 sdist
= cfqq
->last_request_pos
- blk_rq_pos(rq
);
3507 cfqq
->seek_history
<<= 1;
3508 if (blk_queue_nonrot(cfqd
->queue
))
3509 cfqq
->seek_history
|= (n_sec
< CFQQ_SECT_THR_NONROT
);
3511 cfqq
->seek_history
|= (sdist
> CFQQ_SEEK_THR
);
3515 * Disable idle window if the process thinks too long or seeks so much that
3519 cfq_update_idle_window(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
3520 struct cfq_io_cq
*cic
)
3522 int old_idle
, enable_idle
;
3525 * Don't idle for async or idle io prio class
3527 if (!cfq_cfqq_sync(cfqq
) || cfq_class_idle(cfqq
))
3530 enable_idle
= old_idle
= cfq_cfqq_idle_window(cfqq
);
3532 if (cfqq
->queued
[0] + cfqq
->queued
[1] >= 4)
3533 cfq_mark_cfqq_deep(cfqq
);
3535 if (cfqq
->next_rq
&& (cfqq
->next_rq
->cmd_flags
& REQ_NOIDLE
))
3537 else if (!atomic_read(&cic
->icq
.ioc
->active_ref
) ||
3538 !cfqd
->cfq_slice_idle
||
3539 (!cfq_cfqq_deep(cfqq
) && CFQQ_SEEKY(cfqq
)))
3541 else if (sample_valid(cic
->ttime
.ttime_samples
)) {
3542 if (cic
->ttime
.ttime_mean
> cfqd
->cfq_slice_idle
)
3548 if (old_idle
!= enable_idle
) {
3549 cfq_log_cfqq(cfqd
, cfqq
, "idle=%d", enable_idle
);
3551 cfq_mark_cfqq_idle_window(cfqq
);
3553 cfq_clear_cfqq_idle_window(cfqq
);
3558 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3559 * no or if we aren't sure, a 1 will cause a preempt.
3562 cfq_should_preempt(struct cfq_data
*cfqd
, struct cfq_queue
*new_cfqq
,
3565 struct cfq_queue
*cfqq
;
3567 cfqq
= cfqd
->active_queue
;
3571 if (cfq_class_idle(new_cfqq
))
3574 if (cfq_class_idle(cfqq
))
3578 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3580 if (cfq_class_rt(cfqq
) && !cfq_class_rt(new_cfqq
))
3584 * if the new request is sync, but the currently running queue is
3585 * not, let the sync request have priority.
3587 if (rq_is_sync(rq
) && !cfq_cfqq_sync(cfqq
))
3590 if (new_cfqq
->cfqg
!= cfqq
->cfqg
)
3593 if (cfq_slice_used(cfqq
))
3596 /* Allow preemption only if we are idling on sync-noidle tree */
3597 if (cfqd
->serving_wl_type
== SYNC_NOIDLE_WORKLOAD
&&
3598 cfqq_type(new_cfqq
) == SYNC_NOIDLE_WORKLOAD
&&
3599 new_cfqq
->service_tree
->count
== 2 &&
3600 RB_EMPTY_ROOT(&cfqq
->sort_list
))
3604 * So both queues are sync. Let the new request get disk time if
3605 * it's a metadata request and the current queue is doing regular IO.
3607 if ((rq
->cmd_flags
& REQ_PRIO
) && !cfqq
->prio_pending
)
3611 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
3613 if (cfq_class_rt(new_cfqq
) && !cfq_class_rt(cfqq
))
3616 /* An idle queue should not be idle now for some reason */
3617 if (RB_EMPTY_ROOT(&cfqq
->sort_list
) && !cfq_should_idle(cfqd
, cfqq
))
3620 if (!cfqd
->active_cic
|| !cfq_cfqq_wait_request(cfqq
))
3624 * if this request is as-good as one we would expect from the
3625 * current cfqq, let it preempt
3627 if (cfq_rq_close(cfqd
, cfqq
, rq
))
3634 * cfqq preempts the active queue. if we allowed preempt with no slice left,
3635 * let it have half of its nominal slice.
3637 static void cfq_preempt_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
3639 enum wl_type_t old_type
= cfqq_type(cfqd
->active_queue
);
3641 cfq_log_cfqq(cfqd
, cfqq
, "preempt");
3642 cfq_slice_expired(cfqd
, 1);
3645 * workload type is changed, don't save slice, otherwise preempt
3648 if (old_type
!= cfqq_type(cfqq
))
3649 cfqq
->cfqg
->saved_wl_slice
= 0;
3652 * Put the new queue at the front of the of the current list,
3653 * so we know that it will be selected next.
3655 BUG_ON(!cfq_cfqq_on_rr(cfqq
));
3657 cfq_service_tree_add(cfqd
, cfqq
, 1);
3659 cfqq
->slice_end
= 0;
3660 cfq_mark_cfqq_slice_new(cfqq
);
3664 * Called when a new fs request (rq) is added (to cfqq). Check if there's
3665 * something we should do about it
3668 cfq_rq_enqueued(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
3671 struct cfq_io_cq
*cic
= RQ_CIC(rq
);
3674 if (rq
->cmd_flags
& REQ_PRIO
)
3675 cfqq
->prio_pending
++;
3677 cfq_update_io_thinktime(cfqd
, cfqq
, cic
);
3678 cfq_update_io_seektime(cfqd
, cfqq
, rq
);
3679 cfq_update_idle_window(cfqd
, cfqq
, cic
);
3681 cfqq
->last_request_pos
= blk_rq_pos(rq
) + blk_rq_sectors(rq
);
3683 if (cfqq
== cfqd
->active_queue
) {
3685 * Remember that we saw a request from this process, but
3686 * don't start queuing just yet. Otherwise we risk seeing lots
3687 * of tiny requests, because we disrupt the normal plugging
3688 * and merging. If the request is already larger than a single
3689 * page, let it rip immediately. For that case we assume that
3690 * merging is already done. Ditto for a busy system that
3691 * has other work pending, don't risk delaying until the
3692 * idle timer unplug to continue working.
3694 if (cfq_cfqq_wait_request(cfqq
)) {
3695 if (blk_rq_bytes(rq
) > PAGE_CACHE_SIZE
||
3696 cfqd
->busy_queues
> 1) {
3697 cfq_del_timer(cfqd
, cfqq
);
3698 cfq_clear_cfqq_wait_request(cfqq
);
3699 __blk_run_queue(cfqd
->queue
);
3701 cfqg_stats_update_idle_time(cfqq
->cfqg
);
3702 cfq_mark_cfqq_must_dispatch(cfqq
);
3705 } else if (cfq_should_preempt(cfqd
, cfqq
, rq
)) {
3707 * not the active queue - expire current slice if it is
3708 * idle and has expired it's mean thinktime or this new queue
3709 * has some old slice time left and is of higher priority or
3710 * this new queue is RT and the current one is BE
3712 cfq_preempt_queue(cfqd
, cfqq
);
3713 __blk_run_queue(cfqd
->queue
);
3717 static void cfq_insert_request(struct request_queue
*q
, struct request
*rq
)
3719 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
3720 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
3722 cfq_log_cfqq(cfqd
, cfqq
, "insert_request");
3723 cfq_init_prio_data(cfqq
, RQ_CIC(rq
));
3725 rq_set_fifo_time(rq
, jiffies
+ cfqd
->cfq_fifo_expire
[rq_is_sync(rq
)]);
3726 list_add_tail(&rq
->queuelist
, &cfqq
->fifo
);
3728 cfqg_stats_update_io_add(RQ_CFQG(rq
), cfqd
->serving_group
,
3730 cfq_rq_enqueued(cfqd
, cfqq
, rq
);
3734 * Update hw_tag based on peak queue depth over 50 samples under
3737 static void cfq_update_hw_tag(struct cfq_data
*cfqd
)
3739 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
3741 if (cfqd
->rq_in_driver
> cfqd
->hw_tag_est_depth
)
3742 cfqd
->hw_tag_est_depth
= cfqd
->rq_in_driver
;
3744 if (cfqd
->hw_tag
== 1)
3747 if (cfqd
->rq_queued
<= CFQ_HW_QUEUE_MIN
&&
3748 cfqd
->rq_in_driver
<= CFQ_HW_QUEUE_MIN
)
3752 * If active queue hasn't enough requests and can idle, cfq might not
3753 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3756 if (cfqq
&& cfq_cfqq_idle_window(cfqq
) &&
3757 cfqq
->dispatched
+ cfqq
->queued
[0] + cfqq
->queued
[1] <
3758 CFQ_HW_QUEUE_MIN
&& cfqd
->rq_in_driver
< CFQ_HW_QUEUE_MIN
)
3761 if (cfqd
->hw_tag_samples
++ < 50)
3764 if (cfqd
->hw_tag_est_depth
>= CFQ_HW_QUEUE_MIN
)
3770 static bool cfq_should_wait_busy(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
3772 struct cfq_io_cq
*cic
= cfqd
->active_cic
;
3774 /* If the queue already has requests, don't wait */
3775 if (!RB_EMPTY_ROOT(&cfqq
->sort_list
))
3778 /* If there are other queues in the group, don't wait */
3779 if (cfqq
->cfqg
->nr_cfqq
> 1)
3782 /* the only queue in the group, but think time is big */
3783 if (cfq_io_thinktime_big(cfqd
, &cfqq
->cfqg
->ttime
, true))
3786 if (cfq_slice_used(cfqq
))
3789 /* if slice left is less than think time, wait busy */
3790 if (cic
&& sample_valid(cic
->ttime
.ttime_samples
)
3791 && (cfqq
->slice_end
- jiffies
< cic
->ttime
.ttime_mean
))
3795 * If think times is less than a jiffy than ttime_mean=0 and above
3796 * will not be true. It might happen that slice has not expired yet
3797 * but will expire soon (4-5 ns) during select_queue(). To cover the
3798 * case where think time is less than a jiffy, mark the queue wait
3799 * busy if only 1 jiffy is left in the slice.
3801 if (cfqq
->slice_end
- jiffies
== 1)
3807 static void cfq_completed_request(struct request_queue
*q
, struct request
*rq
)
3809 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
3810 struct cfq_data
*cfqd
= cfqq
->cfqd
;
3811 const int sync
= rq_is_sync(rq
);
3815 cfq_log_cfqq(cfqd
, cfqq
, "complete rqnoidle %d",
3816 !!(rq
->cmd_flags
& REQ_NOIDLE
));
3818 cfq_update_hw_tag(cfqd
);
3820 WARN_ON(!cfqd
->rq_in_driver
);
3821 WARN_ON(!cfqq
->dispatched
);
3822 cfqd
->rq_in_driver
--;
3824 (RQ_CFQG(rq
))->dispatched
--;
3825 cfqg_stats_update_completion(cfqq
->cfqg
, rq_start_time_ns(rq
),
3826 rq_io_start_time_ns(rq
), rq
->cmd_flags
);
3828 cfqd
->rq_in_flight
[cfq_cfqq_sync(cfqq
)]--;
3831 struct cfq_rb_root
*st
;
3833 RQ_CIC(rq
)->ttime
.last_end_request
= now
;
3835 if (cfq_cfqq_on_rr(cfqq
))
3836 st
= cfqq
->service_tree
;
3838 st
= st_for(cfqq
->cfqg
, cfqq_class(cfqq
),
3841 st
->ttime
.last_end_request
= now
;
3842 if (!time_after(rq
->start_time
+ cfqd
->cfq_fifo_expire
[1], now
))
3843 cfqd
->last_delayed_sync
= now
;
3846 #ifdef CONFIG_CFQ_GROUP_IOSCHED
3847 cfqq
->cfqg
->ttime
.last_end_request
= now
;
3851 * If this is the active queue, check if it needs to be expired,
3852 * or if we want to idle in case it has no pending requests.
3854 if (cfqd
->active_queue
== cfqq
) {
3855 const bool cfqq_empty
= RB_EMPTY_ROOT(&cfqq
->sort_list
);
3857 if (cfq_cfqq_slice_new(cfqq
)) {
3858 cfq_set_prio_slice(cfqd
, cfqq
);
3859 cfq_clear_cfqq_slice_new(cfqq
);
3863 * Should we wait for next request to come in before we expire
3866 if (cfq_should_wait_busy(cfqd
, cfqq
)) {
3867 unsigned long extend_sl
= cfqd
->cfq_slice_idle
;
3868 if (!cfqd
->cfq_slice_idle
)
3869 extend_sl
= cfqd
->cfq_group_idle
;
3870 cfqq
->slice_end
= jiffies
+ extend_sl
;
3871 cfq_mark_cfqq_wait_busy(cfqq
);
3872 cfq_log_cfqq(cfqd
, cfqq
, "will busy wait");
3876 * Idling is not enabled on:
3878 * - idle-priority queues
3880 * - queues with still some requests queued
3881 * - when there is a close cooperator
3883 if (cfq_slice_used(cfqq
) || cfq_class_idle(cfqq
))
3884 cfq_slice_expired(cfqd
, 1);
3885 else if (sync
&& cfqq_empty
&&
3886 !cfq_close_cooperator(cfqd
, cfqq
)) {
3887 cfq_arm_slice_timer(cfqd
);
3891 if (!cfqd
->rq_in_driver
)
3892 cfq_schedule_dispatch(cfqd
);
3895 static inline int __cfq_may_queue(struct cfq_queue
*cfqq
)
3897 if (cfq_cfqq_wait_request(cfqq
) && !cfq_cfqq_must_alloc_slice(cfqq
)) {
3898 cfq_mark_cfqq_must_alloc_slice(cfqq
);
3899 return ELV_MQUEUE_MUST
;
3902 return ELV_MQUEUE_MAY
;
3905 static int cfq_may_queue(struct request_queue
*q
, int rw
)
3907 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
3908 struct task_struct
*tsk
= current
;
3909 struct cfq_io_cq
*cic
;
3910 struct cfq_queue
*cfqq
;
3913 * don't force setup of a queue from here, as a call to may_queue
3914 * does not necessarily imply that a request actually will be queued.
3915 * so just lookup a possibly existing queue, or return 'may queue'
3918 cic
= cfq_cic_lookup(cfqd
, tsk
->io_context
);
3920 return ELV_MQUEUE_MAY
;
3922 cfqq
= cic_to_cfqq(cic
, rw_is_sync(rw
));
3924 cfq_init_prio_data(cfqq
, cic
);
3926 return __cfq_may_queue(cfqq
);
3929 return ELV_MQUEUE_MAY
;
3933 * queue lock held here
3935 static void cfq_put_request(struct request
*rq
)
3937 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
3940 const int rw
= rq_data_dir(rq
);
3942 BUG_ON(!cfqq
->allocated
[rw
]);
3943 cfqq
->allocated
[rw
]--;
3945 /* Put down rq reference on cfqg */
3946 cfqg_put(RQ_CFQG(rq
));
3947 rq
->elv
.priv
[0] = NULL
;
3948 rq
->elv
.priv
[1] = NULL
;
3950 cfq_put_queue(cfqq
);
3954 static struct cfq_queue
*
3955 cfq_merge_cfqqs(struct cfq_data
*cfqd
, struct cfq_io_cq
*cic
,
3956 struct cfq_queue
*cfqq
)
3958 cfq_log_cfqq(cfqd
, cfqq
, "merging with queue %p", cfqq
->new_cfqq
);
3959 cic_set_cfqq(cic
, cfqq
->new_cfqq
, 1);
3960 cfq_mark_cfqq_coop(cfqq
->new_cfqq
);
3961 cfq_put_queue(cfqq
);
3962 return cic_to_cfqq(cic
, 1);
3966 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
3967 * was the last process referring to said cfqq.
3969 static struct cfq_queue
*
3970 split_cfqq(struct cfq_io_cq
*cic
, struct cfq_queue
*cfqq
)
3972 if (cfqq_process_refs(cfqq
) == 1) {
3973 cfqq
->pid
= current
->pid
;
3974 cfq_clear_cfqq_coop(cfqq
);
3975 cfq_clear_cfqq_split_coop(cfqq
);
3979 cic_set_cfqq(cic
, NULL
, 1);
3981 cfq_put_cooperator(cfqq
);
3983 cfq_put_queue(cfqq
);
3987 * Allocate cfq data structures associated with this request.
3990 cfq_set_request(struct request_queue
*q
, struct request
*rq
, struct bio
*bio
,
3993 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
3994 struct cfq_io_cq
*cic
= icq_to_cic(rq
->elv
.icq
);
3995 const int rw
= rq_data_dir(rq
);
3996 const bool is_sync
= rq_is_sync(rq
);
3997 struct cfq_queue
*cfqq
;
3999 might_sleep_if(gfp_mask
& __GFP_WAIT
);
4001 spin_lock_irq(q
->queue_lock
);
4003 check_ioprio_changed(cic
, bio
);
4004 check_blkcg_changed(cic
, bio
);
4006 cfqq
= cic_to_cfqq(cic
, is_sync
);
4007 if (!cfqq
|| cfqq
== &cfqd
->oom_cfqq
) {
4008 cfqq
= cfq_get_queue(cfqd
, is_sync
, cic
, bio
, gfp_mask
);
4009 cic_set_cfqq(cic
, cfqq
, is_sync
);
4012 * If the queue was seeky for too long, break it apart.
4014 if (cfq_cfqq_coop(cfqq
) && cfq_cfqq_split_coop(cfqq
)) {
4015 cfq_log_cfqq(cfqd
, cfqq
, "breaking apart cfqq");
4016 cfqq
= split_cfqq(cic
, cfqq
);
4022 * Check to see if this queue is scheduled to merge with
4023 * another, closely cooperating queue. The merging of
4024 * queues happens here as it must be done in process context.
4025 * The reference on new_cfqq was taken in merge_cfqqs.
4028 cfqq
= cfq_merge_cfqqs(cfqd
, cic
, cfqq
);
4031 cfqq
->allocated
[rw
]++;
4034 cfqg_get(cfqq
->cfqg
);
4035 rq
->elv
.priv
[0] = cfqq
;
4036 rq
->elv
.priv
[1] = cfqq
->cfqg
;
4037 spin_unlock_irq(q
->queue_lock
);
4041 static void cfq_kick_queue(struct work_struct
*work
)
4043 struct cfq_data
*cfqd
=
4044 container_of(work
, struct cfq_data
, unplug_work
);
4045 struct request_queue
*q
= cfqd
->queue
;
4047 spin_lock_irq(q
->queue_lock
);
4048 __blk_run_queue(cfqd
->queue
);
4049 spin_unlock_irq(q
->queue_lock
);
4053 * Timer running if the active_queue is currently idling inside its time slice
4055 static void cfq_idle_slice_timer(unsigned long data
)
4057 struct cfq_data
*cfqd
= (struct cfq_data
*) data
;
4058 struct cfq_queue
*cfqq
;
4059 unsigned long flags
;
4062 cfq_log(cfqd
, "idle timer fired");
4064 spin_lock_irqsave(cfqd
->queue
->queue_lock
, flags
);
4066 cfqq
= cfqd
->active_queue
;
4071 * We saw a request before the queue expired, let it through
4073 if (cfq_cfqq_must_dispatch(cfqq
))
4079 if (cfq_slice_used(cfqq
))
4083 * only expire and reinvoke request handler, if there are
4084 * other queues with pending requests
4086 if (!cfqd
->busy_queues
)
4090 * not expired and it has a request pending, let it dispatch
4092 if (!RB_EMPTY_ROOT(&cfqq
->sort_list
))
4096 * Queue depth flag is reset only when the idle didn't succeed
4098 cfq_clear_cfqq_deep(cfqq
);
4101 cfq_slice_expired(cfqd
, timed_out
);
4103 cfq_schedule_dispatch(cfqd
);
4105 spin_unlock_irqrestore(cfqd
->queue
->queue_lock
, flags
);
4108 static void cfq_shutdown_timer_wq(struct cfq_data
*cfqd
)
4110 del_timer_sync(&cfqd
->idle_slice_timer
);
4111 cancel_work_sync(&cfqd
->unplug_work
);
4114 static void cfq_put_async_queues(struct cfq_data
*cfqd
)
4118 for (i
= 0; i
< IOPRIO_BE_NR
; i
++) {
4119 if (cfqd
->async_cfqq
[0][i
])
4120 cfq_put_queue(cfqd
->async_cfqq
[0][i
]);
4121 if (cfqd
->async_cfqq
[1][i
])
4122 cfq_put_queue(cfqd
->async_cfqq
[1][i
]);
4125 if (cfqd
->async_idle_cfqq
)
4126 cfq_put_queue(cfqd
->async_idle_cfqq
);
4129 static void cfq_exit_queue(struct elevator_queue
*e
)
4131 struct cfq_data
*cfqd
= e
->elevator_data
;
4132 struct request_queue
*q
= cfqd
->queue
;
4134 cfq_shutdown_timer_wq(cfqd
);
4136 spin_lock_irq(q
->queue_lock
);
4138 if (cfqd
->active_queue
)
4139 __cfq_slice_expired(cfqd
, cfqd
->active_queue
, 0);
4141 cfq_put_async_queues(cfqd
);
4143 spin_unlock_irq(q
->queue_lock
);
4145 cfq_shutdown_timer_wq(cfqd
);
4147 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4148 blkcg_deactivate_policy(q
, &blkcg_policy_cfq
);
4150 kfree(cfqd
->root_group
);
4155 static int cfq_init_queue(struct request_queue
*q
)
4157 struct cfq_data
*cfqd
;
4158 struct blkcg_gq
*blkg __maybe_unused
;
4161 cfqd
= kmalloc_node(sizeof(*cfqd
), GFP_KERNEL
| __GFP_ZERO
, q
->node
);
4166 q
->elevator
->elevator_data
= cfqd
;
4168 /* Init root service tree */
4169 cfqd
->grp_service_tree
= CFQ_RB_ROOT
;
4171 /* Init root group and prefer root group over other groups by default */
4172 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4173 ret
= blkcg_activate_policy(q
, &blkcg_policy_cfq
);
4177 cfqd
->root_group
= blkg_to_cfqg(q
->root_blkg
);
4180 cfqd
->root_group
= kzalloc_node(sizeof(*cfqd
->root_group
),
4181 GFP_KERNEL
, cfqd
->queue
->node
);
4182 if (!cfqd
->root_group
)
4185 cfq_init_cfqg_base(cfqd
->root_group
);
4187 cfqd
->root_group
->weight
= 2 * CFQ_WEIGHT_DEFAULT
;
4188 cfqd
->root_group
->leaf_weight
= 2 * CFQ_WEIGHT_DEFAULT
;
4191 * Not strictly needed (since RB_ROOT just clears the node and we
4192 * zeroed cfqd on alloc), but better be safe in case someone decides
4193 * to add magic to the rb code
4195 for (i
= 0; i
< CFQ_PRIO_LISTS
; i
++)
4196 cfqd
->prio_trees
[i
] = RB_ROOT
;
4199 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
4200 * Grab a permanent reference to it, so that the normal code flow
4201 * will not attempt to free it. oom_cfqq is linked to root_group
4202 * but shouldn't hold a reference as it'll never be unlinked. Lose
4203 * the reference from linking right away.
4205 cfq_init_cfqq(cfqd
, &cfqd
->oom_cfqq
, 1, 0);
4206 cfqd
->oom_cfqq
.ref
++;
4208 spin_lock_irq(q
->queue_lock
);
4209 cfq_link_cfqq_cfqg(&cfqd
->oom_cfqq
, cfqd
->root_group
);
4210 cfqg_put(cfqd
->root_group
);
4211 spin_unlock_irq(q
->queue_lock
);
4213 init_timer(&cfqd
->idle_slice_timer
);
4214 cfqd
->idle_slice_timer
.function
= cfq_idle_slice_timer
;
4215 cfqd
->idle_slice_timer
.data
= (unsigned long) cfqd
;
4217 INIT_WORK(&cfqd
->unplug_work
, cfq_kick_queue
);
4219 cfqd
->cfq_quantum
= cfq_quantum
;
4220 cfqd
->cfq_fifo_expire
[0] = cfq_fifo_expire
[0];
4221 cfqd
->cfq_fifo_expire
[1] = cfq_fifo_expire
[1];
4222 cfqd
->cfq_back_max
= cfq_back_max
;
4223 cfqd
->cfq_back_penalty
= cfq_back_penalty
;
4224 cfqd
->cfq_slice
[0] = cfq_slice_async
;
4225 cfqd
->cfq_slice
[1] = cfq_slice_sync
;
4226 cfqd
->cfq_target_latency
= cfq_target_latency
;
4227 cfqd
->cfq_slice_async_rq
= cfq_slice_async_rq
;
4228 cfqd
->cfq_slice_idle
= cfq_slice_idle
;
4229 cfqd
->cfq_group_idle
= cfq_group_idle
;
4230 cfqd
->cfq_latency
= 1;
4233 * we optimistically start assuming sync ops weren't delayed in last
4234 * second, in order to have larger depth for async operations.
4236 cfqd
->last_delayed_sync
= jiffies
- HZ
;
4245 * sysfs parts below -->
4248 cfq_var_show(unsigned int var
, char *page
)
4250 return sprintf(page
, "%d\n", var
);
4254 cfq_var_store(unsigned int *var
, const char *page
, size_t count
)
4256 char *p
= (char *) page
;
4258 *var
= simple_strtoul(p
, &p
, 10);
4262 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
4263 static ssize_t __FUNC(struct elevator_queue *e, char *page) \
4265 struct cfq_data *cfqd = e->elevator_data; \
4266 unsigned int __data = __VAR; \
4268 __data = jiffies_to_msecs(__data); \
4269 return cfq_var_show(__data, (page)); \
4271 SHOW_FUNCTION(cfq_quantum_show
, cfqd
->cfq_quantum
, 0);
4272 SHOW_FUNCTION(cfq_fifo_expire_sync_show
, cfqd
->cfq_fifo_expire
[1], 1);
4273 SHOW_FUNCTION(cfq_fifo_expire_async_show
, cfqd
->cfq_fifo_expire
[0], 1);
4274 SHOW_FUNCTION(cfq_back_seek_max_show
, cfqd
->cfq_back_max
, 0);
4275 SHOW_FUNCTION(cfq_back_seek_penalty_show
, cfqd
->cfq_back_penalty
, 0);
4276 SHOW_FUNCTION(cfq_slice_idle_show
, cfqd
->cfq_slice_idle
, 1);
4277 SHOW_FUNCTION(cfq_group_idle_show
, cfqd
->cfq_group_idle
, 1);
4278 SHOW_FUNCTION(cfq_slice_sync_show
, cfqd
->cfq_slice
[1], 1);
4279 SHOW_FUNCTION(cfq_slice_async_show
, cfqd
->cfq_slice
[0], 1);
4280 SHOW_FUNCTION(cfq_slice_async_rq_show
, cfqd
->cfq_slice_async_rq
, 0);
4281 SHOW_FUNCTION(cfq_low_latency_show
, cfqd
->cfq_latency
, 0);
4282 SHOW_FUNCTION(cfq_target_latency_show
, cfqd
->cfq_target_latency
, 1);
4283 #undef SHOW_FUNCTION
4285 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
4286 static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
4288 struct cfq_data *cfqd = e->elevator_data; \
4289 unsigned int __data; \
4290 int ret = cfq_var_store(&__data, (page), count); \
4291 if (__data < (MIN)) \
4293 else if (__data > (MAX)) \
4296 *(__PTR) = msecs_to_jiffies(__data); \
4298 *(__PTR) = __data; \
4301 STORE_FUNCTION(cfq_quantum_store
, &cfqd
->cfq_quantum
, 1, UINT_MAX
, 0);
4302 STORE_FUNCTION(cfq_fifo_expire_sync_store
, &cfqd
->cfq_fifo_expire
[1], 1,
4304 STORE_FUNCTION(cfq_fifo_expire_async_store
, &cfqd
->cfq_fifo_expire
[0], 1,
4306 STORE_FUNCTION(cfq_back_seek_max_store
, &cfqd
->cfq_back_max
, 0, UINT_MAX
, 0);
4307 STORE_FUNCTION(cfq_back_seek_penalty_store
, &cfqd
->cfq_back_penalty
, 1,
4309 STORE_FUNCTION(cfq_slice_idle_store
, &cfqd
->cfq_slice_idle
, 0, UINT_MAX
, 1);
4310 STORE_FUNCTION(cfq_group_idle_store
, &cfqd
->cfq_group_idle
, 0, UINT_MAX
, 1);
4311 STORE_FUNCTION(cfq_slice_sync_store
, &cfqd
->cfq_slice
[1], 1, UINT_MAX
, 1);
4312 STORE_FUNCTION(cfq_slice_async_store
, &cfqd
->cfq_slice
[0], 1, UINT_MAX
, 1);
4313 STORE_FUNCTION(cfq_slice_async_rq_store
, &cfqd
->cfq_slice_async_rq
, 1,
4315 STORE_FUNCTION(cfq_low_latency_store
, &cfqd
->cfq_latency
, 0, 1, 0);
4316 STORE_FUNCTION(cfq_target_latency_store
, &cfqd
->cfq_target_latency
, 1, UINT_MAX
, 1);
4317 #undef STORE_FUNCTION
4319 #define CFQ_ATTR(name) \
4320 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
4322 static struct elv_fs_entry cfq_attrs
[] = {
4324 CFQ_ATTR(fifo_expire_sync
),
4325 CFQ_ATTR(fifo_expire_async
),
4326 CFQ_ATTR(back_seek_max
),
4327 CFQ_ATTR(back_seek_penalty
),
4328 CFQ_ATTR(slice_sync
),
4329 CFQ_ATTR(slice_async
),
4330 CFQ_ATTR(slice_async_rq
),
4331 CFQ_ATTR(slice_idle
),
4332 CFQ_ATTR(group_idle
),
4333 CFQ_ATTR(low_latency
),
4334 CFQ_ATTR(target_latency
),
4338 static struct elevator_type iosched_cfq
= {
4340 .elevator_merge_fn
= cfq_merge
,
4341 .elevator_merged_fn
= cfq_merged_request
,
4342 .elevator_merge_req_fn
= cfq_merged_requests
,
4343 .elevator_allow_merge_fn
= cfq_allow_merge
,
4344 .elevator_bio_merged_fn
= cfq_bio_merged
,
4345 .elevator_dispatch_fn
= cfq_dispatch_requests
,
4346 .elevator_add_req_fn
= cfq_insert_request
,
4347 .elevator_activate_req_fn
= cfq_activate_request
,
4348 .elevator_deactivate_req_fn
= cfq_deactivate_request
,
4349 .elevator_completed_req_fn
= cfq_completed_request
,
4350 .elevator_former_req_fn
= elv_rb_former_request
,
4351 .elevator_latter_req_fn
= elv_rb_latter_request
,
4352 .elevator_init_icq_fn
= cfq_init_icq
,
4353 .elevator_exit_icq_fn
= cfq_exit_icq
,
4354 .elevator_set_req_fn
= cfq_set_request
,
4355 .elevator_put_req_fn
= cfq_put_request
,
4356 .elevator_may_queue_fn
= cfq_may_queue
,
4357 .elevator_init_fn
= cfq_init_queue
,
4358 .elevator_exit_fn
= cfq_exit_queue
,
4360 .icq_size
= sizeof(struct cfq_io_cq
),
4361 .icq_align
= __alignof__(struct cfq_io_cq
),
4362 .elevator_attrs
= cfq_attrs
,
4363 .elevator_name
= "cfq",
4364 .elevator_owner
= THIS_MODULE
,
4367 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4368 static struct blkcg_policy blkcg_policy_cfq
= {
4369 .pd_size
= sizeof(struct cfq_group
),
4370 .cftypes
= cfq_blkcg_files
,
4372 .pd_init_fn
= cfq_pd_init
,
4373 .pd_reset_stats_fn
= cfq_pd_reset_stats
,
4377 static int __init
cfq_init(void)
4382 * could be 0 on HZ < 1000 setups
4384 if (!cfq_slice_async
)
4385 cfq_slice_async
= 1;
4386 if (!cfq_slice_idle
)
4389 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4390 if (!cfq_group_idle
)
4393 ret
= blkcg_policy_register(&blkcg_policy_cfq
);
4401 cfq_pool
= KMEM_CACHE(cfq_queue
, 0);
4405 ret
= elv_register(&iosched_cfq
);
4412 kmem_cache_destroy(cfq_pool
);
4414 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4415 blkcg_policy_unregister(&blkcg_policy_cfq
);
4420 static void __exit
cfq_exit(void)
4422 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4423 blkcg_policy_unregister(&blkcg_policy_cfq
);
4425 elv_unregister(&iosched_cfq
);
4426 kmem_cache_destroy(cfq_pool
);
4429 module_init(cfq_init
);
4430 module_exit(cfq_exit
);
4432 MODULE_AUTHOR("Jens Axboe");
4433 MODULE_LICENSE("GPL");
4434 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");