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.
89 struct cfq_ttime ttime
;
91 #define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
92 .ttime = {.last_end_request = jiffies,},}
95 * Per process-grouping structure
100 /* various state flags, see below */
102 /* parent cfq_data */
103 struct cfq_data
*cfqd
;
104 /* service_tree member */
105 struct rb_node rb_node
;
106 /* service_tree key */
107 unsigned long rb_key
;
108 /* prio tree member */
109 struct rb_node p_node
;
110 /* prio tree root we belong to, if any */
111 struct rb_root
*p_root
;
112 /* sorted list of pending requests */
113 struct rb_root sort_list
;
114 /* if fifo isn't expired, next request to serve */
115 struct request
*next_rq
;
116 /* requests queued in sort_list */
118 /* currently allocated requests */
120 /* fifo list of requests in sort_list */
121 struct list_head fifo
;
123 /* time when queue got scheduled in to dispatch first request. */
124 unsigned long dispatch_start
;
125 unsigned int allocated_slice
;
126 unsigned int slice_dispatch
;
127 /* time when first request from queue completed and slice started. */
128 unsigned long slice_start
;
129 unsigned long slice_end
;
132 /* pending priority requests */
134 /* number of requests that are on the dispatch list or inside driver */
137 /* io prio of this group */
138 unsigned short ioprio
, org_ioprio
;
139 unsigned short ioprio_class
;
144 sector_t last_request_pos
;
146 struct cfq_rb_root
*service_tree
;
147 struct cfq_queue
*new_cfqq
;
148 struct cfq_group
*cfqg
;
149 /* Number of sectors dispatched from queue in single dispatch round */
150 unsigned long nr_sectors
;
154 * First index in the service_trees.
155 * IDLE is handled separately, so it has negative index
165 * Second index in the service_trees.
169 SYNC_NOIDLE_WORKLOAD
= 1,
174 #ifdef CONFIG_CFQ_GROUP_IOSCHED
175 /* total bytes transferred */
176 struct blkg_rwstat service_bytes
;
177 /* total IOs serviced, post merge */
178 struct blkg_rwstat serviced
;
179 /* number of ios merged */
180 struct blkg_rwstat merged
;
181 /* total time spent on device in ns, may not be accurate w/ queueing */
182 struct blkg_rwstat service_time
;
183 /* total time spent waiting in scheduler queue in ns */
184 struct blkg_rwstat wait_time
;
185 /* number of IOs queued up */
186 struct blkg_rwstat queued
;
187 /* total sectors transferred */
188 struct blkg_stat sectors
;
189 /* total disk time and nr sectors dispatched by this group */
190 struct blkg_stat time
;
191 #ifdef CONFIG_DEBUG_BLK_CGROUP
192 /* time not charged to this cgroup */
193 struct blkg_stat unaccounted_time
;
194 /* sum of number of ios queued across all samples */
195 struct blkg_stat avg_queue_size_sum
;
196 /* count of samples taken for average */
197 struct blkg_stat avg_queue_size_samples
;
198 /* how many times this group has been removed from service tree */
199 struct blkg_stat dequeue
;
200 /* total time spent waiting for it to be assigned a timeslice. */
201 struct blkg_stat group_wait_time
;
202 /* time spent idling for this blkcg_gq */
203 struct blkg_stat idle_time
;
204 /* total time with empty current active q with other requests queued */
205 struct blkg_stat empty_time
;
206 /* fields after this shouldn't be cleared on stat reset */
207 uint64_t start_group_wait_time
;
208 uint64_t start_idle_time
;
209 uint64_t start_empty_time
;
211 #endif /* CONFIG_DEBUG_BLK_CGROUP */
212 #endif /* CONFIG_CFQ_GROUP_IOSCHED */
215 /* This is per cgroup per device grouping structure */
217 /* must be the first member */
218 struct blkg_policy_data pd
;
220 /* group service_tree member */
221 struct rb_node rb_node
;
223 /* group service_tree key */
227 * The number of active cfqgs and sum of their weights under this
228 * cfqg. This covers this cfqg's leaf_weight and all children's
229 * weights, but does not cover weights of further descendants.
231 * If a cfqg is on the service tree, it's active. An active cfqg
232 * also activates its parent and contributes to the children_weight
236 unsigned int children_weight
;
239 * vfraction is the fraction of vdisktime that the tasks in this
240 * cfqg are entitled to. This is determined by compounding the
241 * ratios walking up from this cfqg to the root.
243 * It is in fixed point w/ CFQ_SERVICE_SHIFT and the sum of all
244 * vfractions on a service tree is approximately 1. The sum may
245 * deviate a bit due to rounding errors and fluctuations caused by
246 * cfqgs entering and leaving the service tree.
248 unsigned int vfraction
;
251 * There are two weights - (internal) weight is the weight of this
252 * cfqg against the sibling cfqgs. leaf_weight is the wight of
253 * this cfqg against the child cfqgs. For the root cfqg, both
254 * weights are kept in sync for backward compatibility.
257 unsigned int new_weight
;
258 unsigned int dev_weight
;
260 unsigned int leaf_weight
;
261 unsigned int new_leaf_weight
;
262 unsigned int dev_leaf_weight
;
264 /* number of cfqq currently on this group */
268 * Per group busy queues average. Useful for workload slice calc. We
269 * create the array for each prio class but at run time it is used
270 * only for RT and BE class and slot for IDLE class remains unused.
271 * This is primarily done to avoid confusion and a gcc warning.
273 unsigned int busy_queues_avg
[CFQ_PRIO_NR
];
275 * rr lists of queues with requests. We maintain service trees for
276 * RT and BE classes. These trees are subdivided in subclasses
277 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
278 * class there is no subclassification and all the cfq queues go on
279 * a single tree service_tree_idle.
280 * Counts are embedded in the cfq_rb_root
282 struct cfq_rb_root service_trees
[2][3];
283 struct cfq_rb_root service_tree_idle
;
285 unsigned long saved_wl_slice
;
286 enum wl_type_t saved_wl_type
;
287 enum wl_class_t saved_wl_class
;
289 /* number of requests that are on the dispatch list or inside driver */
291 struct cfq_ttime ttime
;
292 struct cfqg_stats stats
;
296 struct io_cq icq
; /* must be the first member */
297 struct cfq_queue
*cfqq
[2];
298 struct cfq_ttime ttime
;
299 int ioprio
; /* the current ioprio */
300 #ifdef CONFIG_CFQ_GROUP_IOSCHED
301 uint64_t blkcg_id
; /* the current blkcg ID */
306 * Per block device queue structure
309 struct request_queue
*queue
;
310 /* Root service tree for cfq_groups */
311 struct cfq_rb_root grp_service_tree
;
312 struct cfq_group
*root_group
;
315 * The priority currently being served
317 enum wl_class_t serving_wl_class
;
318 enum wl_type_t serving_wl_type
;
319 unsigned long workload_expires
;
320 struct cfq_group
*serving_group
;
323 * Each priority tree is sorted by next_request position. These
324 * trees are used when determining if two or more queues are
325 * interleaving requests (see cfq_close_cooperator).
327 struct rb_root prio_trees
[CFQ_PRIO_LISTS
];
329 unsigned int busy_queues
;
330 unsigned int busy_sync_queues
;
336 * queue-depth detection
342 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
343 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
346 int hw_tag_est_depth
;
347 unsigned int hw_tag_samples
;
350 * idle window management
352 struct timer_list idle_slice_timer
;
353 struct work_struct unplug_work
;
355 struct cfq_queue
*active_queue
;
356 struct cfq_io_cq
*active_cic
;
359 * async queue for each priority case
361 struct cfq_queue
*async_cfqq
[2][IOPRIO_BE_NR
];
362 struct cfq_queue
*async_idle_cfqq
;
364 sector_t last_position
;
367 * tunables, see top of file
369 unsigned int cfq_quantum
;
370 unsigned int cfq_fifo_expire
[2];
371 unsigned int cfq_back_penalty
;
372 unsigned int cfq_back_max
;
373 unsigned int cfq_slice
[2];
374 unsigned int cfq_slice_async_rq
;
375 unsigned int cfq_slice_idle
;
376 unsigned int cfq_group_idle
;
377 unsigned int cfq_latency
;
378 unsigned int cfq_target_latency
;
381 * Fallback dummy cfqq for extreme OOM conditions
383 struct cfq_queue oom_cfqq
;
385 unsigned long last_delayed_sync
;
388 static struct cfq_group
*cfq_get_next_cfqg(struct cfq_data
*cfqd
);
390 static struct cfq_rb_root
*st_for(struct cfq_group
*cfqg
,
391 enum wl_class_t
class,
397 if (class == IDLE_WORKLOAD
)
398 return &cfqg
->service_tree_idle
;
400 return &cfqg
->service_trees
[class][type
];
403 enum cfqq_state_flags
{
404 CFQ_CFQQ_FLAG_on_rr
= 0, /* on round-robin busy list */
405 CFQ_CFQQ_FLAG_wait_request
, /* waiting for a request */
406 CFQ_CFQQ_FLAG_must_dispatch
, /* must be allowed a dispatch */
407 CFQ_CFQQ_FLAG_must_alloc_slice
, /* per-slice must_alloc flag */
408 CFQ_CFQQ_FLAG_fifo_expire
, /* FIFO checked in this slice */
409 CFQ_CFQQ_FLAG_idle_window
, /* slice idling enabled */
410 CFQ_CFQQ_FLAG_prio_changed
, /* task priority has changed */
411 CFQ_CFQQ_FLAG_slice_new
, /* no requests dispatched in slice */
412 CFQ_CFQQ_FLAG_sync
, /* synchronous queue */
413 CFQ_CFQQ_FLAG_coop
, /* cfqq is shared */
414 CFQ_CFQQ_FLAG_split_coop
, /* shared cfqq will be splitted */
415 CFQ_CFQQ_FLAG_deep
, /* sync cfqq experienced large depth */
416 CFQ_CFQQ_FLAG_wait_busy
, /* Waiting for next request */
419 #define CFQ_CFQQ_FNS(name) \
420 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
422 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
424 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
426 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
428 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
430 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
434 CFQ_CFQQ_FNS(wait_request
);
435 CFQ_CFQQ_FNS(must_dispatch
);
436 CFQ_CFQQ_FNS(must_alloc_slice
);
437 CFQ_CFQQ_FNS(fifo_expire
);
438 CFQ_CFQQ_FNS(idle_window
);
439 CFQ_CFQQ_FNS(prio_changed
);
440 CFQ_CFQQ_FNS(slice_new
);
443 CFQ_CFQQ_FNS(split_coop
);
445 CFQ_CFQQ_FNS(wait_busy
);
448 static inline struct cfq_group
*pd_to_cfqg(struct blkg_policy_data
*pd
)
450 return pd
? container_of(pd
, struct cfq_group
, pd
) : NULL
;
453 static inline struct blkcg_gq
*cfqg_to_blkg(struct cfq_group
*cfqg
)
455 return pd_to_blkg(&cfqg
->pd
);
458 #if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
460 /* cfqg stats flags */
461 enum cfqg_stats_flags
{
462 CFQG_stats_waiting
= 0,
467 #define CFQG_FLAG_FNS(name) \
468 static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \
470 stats->flags |= (1 << CFQG_stats_##name); \
472 static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats) \
474 stats->flags &= ~(1 << CFQG_stats_##name); \
476 static inline int cfqg_stats_##name(struct cfqg_stats *stats) \
478 return (stats->flags & (1 << CFQG_stats_##name)) != 0; \
481 CFQG_FLAG_FNS(waiting)
482 CFQG_FLAG_FNS(idling
)
486 /* This should be called with the queue_lock held. */
487 static void cfqg_stats_update_group_wait_time(struct cfqg_stats
*stats
)
489 unsigned long long now
;
491 if (!cfqg_stats_waiting(stats
))
495 if (time_after64(now
, stats
->start_group_wait_time
))
496 blkg_stat_add(&stats
->group_wait_time
,
497 now
- stats
->start_group_wait_time
);
498 cfqg_stats_clear_waiting(stats
);
501 /* This should be called with the queue_lock held. */
502 static void cfqg_stats_set_start_group_wait_time(struct cfq_group
*cfqg
,
503 struct cfq_group
*curr_cfqg
)
505 struct cfqg_stats
*stats
= &cfqg
->stats
;
507 if (cfqg_stats_waiting(stats
))
509 if (cfqg
== curr_cfqg
)
511 stats
->start_group_wait_time
= sched_clock();
512 cfqg_stats_mark_waiting(stats
);
515 /* This should be called with the queue_lock held. */
516 static void cfqg_stats_end_empty_time(struct cfqg_stats
*stats
)
518 unsigned long long now
;
520 if (!cfqg_stats_empty(stats
))
524 if (time_after64(now
, stats
->start_empty_time
))
525 blkg_stat_add(&stats
->empty_time
,
526 now
- stats
->start_empty_time
);
527 cfqg_stats_clear_empty(stats
);
530 static void cfqg_stats_update_dequeue(struct cfq_group
*cfqg
)
532 blkg_stat_add(&cfqg
->stats
.dequeue
, 1);
535 static void cfqg_stats_set_start_empty_time(struct cfq_group
*cfqg
)
537 struct cfqg_stats
*stats
= &cfqg
->stats
;
539 if (blkg_rwstat_total(&stats
->queued
))
543 * group is already marked empty. This can happen if cfqq got new
544 * request in parent group and moved to this group while being added
545 * to service tree. Just ignore the event and move on.
547 if (cfqg_stats_empty(stats
))
550 stats
->start_empty_time
= sched_clock();
551 cfqg_stats_mark_empty(stats
);
554 static void cfqg_stats_update_idle_time(struct cfq_group
*cfqg
)
556 struct cfqg_stats
*stats
= &cfqg
->stats
;
558 if (cfqg_stats_idling(stats
)) {
559 unsigned long long now
= sched_clock();
561 if (time_after64(now
, stats
->start_idle_time
))
562 blkg_stat_add(&stats
->idle_time
,
563 now
- stats
->start_idle_time
);
564 cfqg_stats_clear_idling(stats
);
568 static void cfqg_stats_set_start_idle_time(struct cfq_group
*cfqg
)
570 struct cfqg_stats
*stats
= &cfqg
->stats
;
572 BUG_ON(cfqg_stats_idling(stats
));
574 stats
->start_idle_time
= sched_clock();
575 cfqg_stats_mark_idling(stats
);
578 static void cfqg_stats_update_avg_queue_size(struct cfq_group
*cfqg
)
580 struct cfqg_stats
*stats
= &cfqg
->stats
;
582 blkg_stat_add(&stats
->avg_queue_size_sum
,
583 blkg_rwstat_total(&stats
->queued
));
584 blkg_stat_add(&stats
->avg_queue_size_samples
, 1);
585 cfqg_stats_update_group_wait_time(stats
);
588 #else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
590 static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group
*cfqg
, struct cfq_group
*curr_cfqg
) { }
591 static inline void cfqg_stats_end_empty_time(struct cfqg_stats
*stats
) { }
592 static inline void cfqg_stats_update_dequeue(struct cfq_group
*cfqg
) { }
593 static inline void cfqg_stats_set_start_empty_time(struct cfq_group
*cfqg
) { }
594 static inline void cfqg_stats_update_idle_time(struct cfq_group
*cfqg
) { }
595 static inline void cfqg_stats_set_start_idle_time(struct cfq_group
*cfqg
) { }
596 static inline void cfqg_stats_update_avg_queue_size(struct cfq_group
*cfqg
) { }
598 #endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
600 #ifdef CONFIG_CFQ_GROUP_IOSCHED
602 static struct blkcg_policy blkcg_policy_cfq
;
604 static inline struct cfq_group
*blkg_to_cfqg(struct blkcg_gq
*blkg
)
606 return pd_to_cfqg(blkg_to_pd(blkg
, &blkcg_policy_cfq
));
609 static inline struct cfq_group
*cfqg_parent(struct cfq_group
*cfqg
)
611 struct blkcg_gq
*pblkg
= cfqg_to_blkg(cfqg
)->parent
;
613 return pblkg
? blkg_to_cfqg(pblkg
) : NULL
;
616 static inline void cfqg_get(struct cfq_group
*cfqg
)
618 return blkg_get(cfqg_to_blkg(cfqg
));
621 static inline void cfqg_put(struct cfq_group
*cfqg
)
623 return blkg_put(cfqg_to_blkg(cfqg
));
626 #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \
629 blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \
630 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \
631 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
632 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
636 #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
639 blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \
640 blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \
643 static inline void cfqg_stats_update_io_add(struct cfq_group
*cfqg
,
644 struct cfq_group
*curr_cfqg
, int rw
)
646 blkg_rwstat_add(&cfqg
->stats
.queued
, rw
, 1);
647 cfqg_stats_end_empty_time(&cfqg
->stats
);
648 cfqg_stats_set_start_group_wait_time(cfqg
, curr_cfqg
);
651 static inline void cfqg_stats_update_timeslice_used(struct cfq_group
*cfqg
,
652 unsigned long time
, unsigned long unaccounted_time
)
654 blkg_stat_add(&cfqg
->stats
.time
, time
);
655 #ifdef CONFIG_DEBUG_BLK_CGROUP
656 blkg_stat_add(&cfqg
->stats
.unaccounted_time
, unaccounted_time
);
660 static inline void cfqg_stats_update_io_remove(struct cfq_group
*cfqg
, int rw
)
662 blkg_rwstat_add(&cfqg
->stats
.queued
, rw
, -1);
665 static inline void cfqg_stats_update_io_merged(struct cfq_group
*cfqg
, int rw
)
667 blkg_rwstat_add(&cfqg
->stats
.merged
, rw
, 1);
670 static inline void cfqg_stats_update_dispatch(struct cfq_group
*cfqg
,
671 uint64_t bytes
, int rw
)
673 blkg_stat_add(&cfqg
->stats
.sectors
, bytes
>> 9);
674 blkg_rwstat_add(&cfqg
->stats
.serviced
, rw
, 1);
675 blkg_rwstat_add(&cfqg
->stats
.service_bytes
, rw
, bytes
);
678 static inline void cfqg_stats_update_completion(struct cfq_group
*cfqg
,
679 uint64_t start_time
, uint64_t io_start_time
, int rw
)
681 struct cfqg_stats
*stats
= &cfqg
->stats
;
682 unsigned long long now
= sched_clock();
684 if (time_after64(now
, io_start_time
))
685 blkg_rwstat_add(&stats
->service_time
, rw
, now
- io_start_time
);
686 if (time_after64(io_start_time
, start_time
))
687 blkg_rwstat_add(&stats
->wait_time
, rw
,
688 io_start_time
- start_time
);
691 static void cfq_pd_reset_stats(struct blkcg_gq
*blkg
)
693 struct cfq_group
*cfqg
= blkg_to_cfqg(blkg
);
694 struct cfqg_stats
*stats
= &cfqg
->stats
;
696 /* queued stats shouldn't be cleared */
697 blkg_rwstat_reset(&stats
->service_bytes
);
698 blkg_rwstat_reset(&stats
->serviced
);
699 blkg_rwstat_reset(&stats
->merged
);
700 blkg_rwstat_reset(&stats
->service_time
);
701 blkg_rwstat_reset(&stats
->wait_time
);
702 blkg_stat_reset(&stats
->time
);
703 #ifdef CONFIG_DEBUG_BLK_CGROUP
704 blkg_stat_reset(&stats
->unaccounted_time
);
705 blkg_stat_reset(&stats
->avg_queue_size_sum
);
706 blkg_stat_reset(&stats
->avg_queue_size_samples
);
707 blkg_stat_reset(&stats
->dequeue
);
708 blkg_stat_reset(&stats
->group_wait_time
);
709 blkg_stat_reset(&stats
->idle_time
);
710 blkg_stat_reset(&stats
->empty_time
);
714 #else /* CONFIG_CFQ_GROUP_IOSCHED */
716 static inline struct cfq_group
*cfqg_parent(struct cfq_group
*cfqg
) { return NULL
; }
717 static inline void cfqg_get(struct cfq_group
*cfqg
) { }
718 static inline void cfqg_put(struct cfq_group
*cfqg
) { }
720 #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
721 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \
722 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
723 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
725 #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
727 static inline void cfqg_stats_update_io_add(struct cfq_group
*cfqg
,
728 struct cfq_group
*curr_cfqg
, int rw
) { }
729 static inline void cfqg_stats_update_timeslice_used(struct cfq_group
*cfqg
,
730 unsigned long time
, unsigned long unaccounted_time
) { }
731 static inline void cfqg_stats_update_io_remove(struct cfq_group
*cfqg
, int rw
) { }
732 static inline void cfqg_stats_update_io_merged(struct cfq_group
*cfqg
, int rw
) { }
733 static inline void cfqg_stats_update_dispatch(struct cfq_group
*cfqg
,
734 uint64_t bytes
, int rw
) { }
735 static inline void cfqg_stats_update_completion(struct cfq_group
*cfqg
,
736 uint64_t start_time
, uint64_t io_start_time
, int rw
) { }
738 #endif /* CONFIG_CFQ_GROUP_IOSCHED */
740 #define cfq_log(cfqd, fmt, args...) \
741 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
743 /* Traverses through cfq group service trees */
744 #define for_each_cfqg_st(cfqg, i, j, st) \
745 for (i = 0; i <= IDLE_WORKLOAD; i++) \
746 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
747 : &cfqg->service_tree_idle; \
748 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
749 (i == IDLE_WORKLOAD && j == 0); \
750 j++, st = i < IDLE_WORKLOAD ? \
751 &cfqg->service_trees[i][j]: NULL) \
753 static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
754 struct cfq_ttime
*ttime
, bool group_idle
)
757 if (!sample_valid(ttime
->ttime_samples
))
760 slice
= cfqd
->cfq_group_idle
;
762 slice
= cfqd
->cfq_slice_idle
;
763 return ttime
->ttime_mean
> slice
;
766 static inline bool iops_mode(struct cfq_data
*cfqd
)
769 * If we are not idling on queues and it is a NCQ drive, parallel
770 * execution of requests is on and measuring time is not possible
771 * in most of the cases until and unless we drive shallower queue
772 * depths and that becomes a performance bottleneck. In such cases
773 * switch to start providing fairness in terms of number of IOs.
775 if (!cfqd
->cfq_slice_idle
&& cfqd
->hw_tag
)
781 static inline enum wl_class_t
cfqq_class(struct cfq_queue
*cfqq
)
783 if (cfq_class_idle(cfqq
))
784 return IDLE_WORKLOAD
;
785 if (cfq_class_rt(cfqq
))
791 static enum wl_type_t
cfqq_type(struct cfq_queue
*cfqq
)
793 if (!cfq_cfqq_sync(cfqq
))
794 return ASYNC_WORKLOAD
;
795 if (!cfq_cfqq_idle_window(cfqq
))
796 return SYNC_NOIDLE_WORKLOAD
;
797 return SYNC_WORKLOAD
;
800 static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class
,
801 struct cfq_data
*cfqd
,
802 struct cfq_group
*cfqg
)
804 if (wl_class
== IDLE_WORKLOAD
)
805 return cfqg
->service_tree_idle
.count
;
807 return cfqg
->service_trees
[wl_class
][ASYNC_WORKLOAD
].count
+
808 cfqg
->service_trees
[wl_class
][SYNC_NOIDLE_WORKLOAD
].count
+
809 cfqg
->service_trees
[wl_class
][SYNC_WORKLOAD
].count
;
812 static inline int cfqg_busy_async_queues(struct cfq_data
*cfqd
,
813 struct cfq_group
*cfqg
)
815 return cfqg
->service_trees
[RT_WORKLOAD
][ASYNC_WORKLOAD
].count
+
816 cfqg
->service_trees
[BE_WORKLOAD
][ASYNC_WORKLOAD
].count
;
819 static void cfq_dispatch_insert(struct request_queue
*, struct request
*);
820 static struct cfq_queue
*cfq_get_queue(struct cfq_data
*cfqd
, bool is_sync
,
821 struct cfq_io_cq
*cic
, struct bio
*bio
,
824 static inline struct cfq_io_cq
*icq_to_cic(struct io_cq
*icq
)
826 /* cic->icq is the first member, %NULL will convert to %NULL */
827 return container_of(icq
, struct cfq_io_cq
, icq
);
830 static inline struct cfq_io_cq
*cfq_cic_lookup(struct cfq_data
*cfqd
,
831 struct io_context
*ioc
)
834 return icq_to_cic(ioc_lookup_icq(ioc
, cfqd
->queue
));
838 static inline struct cfq_queue
*cic_to_cfqq(struct cfq_io_cq
*cic
, bool is_sync
)
840 return cic
->cfqq
[is_sync
];
843 static inline void cic_set_cfqq(struct cfq_io_cq
*cic
, struct cfq_queue
*cfqq
,
846 cic
->cfqq
[is_sync
] = cfqq
;
849 static inline struct cfq_data
*cic_to_cfqd(struct cfq_io_cq
*cic
)
851 return cic
->icq
.q
->elevator
->elevator_data
;
855 * We regard a request as SYNC, if it's either a read or has the SYNC bit
856 * set (in which case it could also be direct WRITE).
858 static inline bool cfq_bio_sync(struct bio
*bio
)
860 return bio_data_dir(bio
) == READ
|| (bio
->bi_rw
& REQ_SYNC
);
864 * scheduler run of queue, if there are requests pending and no one in the
865 * driver that will restart queueing
867 static inline void cfq_schedule_dispatch(struct cfq_data
*cfqd
)
869 if (cfqd
->busy_queues
) {
870 cfq_log(cfqd
, "schedule dispatch");
871 kblockd_schedule_work(cfqd
->queue
, &cfqd
->unplug_work
);
876 * Scale schedule slice based on io priority. Use the sync time slice only
877 * if a queue is marked sync and has sync io queued. A sync queue with async
878 * io only, should not get full sync slice length.
880 static inline int cfq_prio_slice(struct cfq_data
*cfqd
, bool sync
,
883 const int base_slice
= cfqd
->cfq_slice
[sync
];
885 WARN_ON(prio
>= IOPRIO_BE_NR
);
887 return base_slice
+ (base_slice
/CFQ_SLICE_SCALE
* (4 - prio
));
891 cfq_prio_to_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
893 return cfq_prio_slice(cfqd
, cfq_cfqq_sync(cfqq
), cfqq
->ioprio
);
897 * cfqg_scale_charge - scale disk time charge according to cfqg weight
898 * @charge: disk time being charged
899 * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT
901 * Scale @charge according to @vfraction, which is in range (0, 1]. The
902 * scaling is inversely proportional.
904 * scaled = charge / vfraction
906 * The result is also in fixed point w/ CFQ_SERVICE_SHIFT.
908 static inline u64
cfqg_scale_charge(unsigned long charge
,
909 unsigned int vfraction
)
911 u64 c
= charge
<< CFQ_SERVICE_SHIFT
; /* make it fixed point */
913 /* charge / vfraction */
914 c
<<= CFQ_SERVICE_SHIFT
;
915 do_div(c
, vfraction
);
919 static inline u64
max_vdisktime(u64 min_vdisktime
, u64 vdisktime
)
921 s64 delta
= (s64
)(vdisktime
- min_vdisktime
);
923 min_vdisktime
= vdisktime
;
925 return min_vdisktime
;
928 static inline u64
min_vdisktime(u64 min_vdisktime
, u64 vdisktime
)
930 s64 delta
= (s64
)(vdisktime
- min_vdisktime
);
932 min_vdisktime
= vdisktime
;
934 return min_vdisktime
;
937 static void update_min_vdisktime(struct cfq_rb_root
*st
)
939 struct cfq_group
*cfqg
;
942 cfqg
= rb_entry_cfqg(st
->left
);
943 st
->min_vdisktime
= max_vdisktime(st
->min_vdisktime
,
949 * get averaged number of queues of RT/BE priority.
950 * average is updated, with a formula that gives more weight to higher numbers,
951 * to quickly follows sudden increases and decrease slowly
954 static inline unsigned cfq_group_get_avg_queues(struct cfq_data
*cfqd
,
955 struct cfq_group
*cfqg
, bool rt
)
957 unsigned min_q
, max_q
;
958 unsigned mult
= cfq_hist_divisor
- 1;
959 unsigned round
= cfq_hist_divisor
/ 2;
960 unsigned busy
= cfq_group_busy_queues_wl(rt
, cfqd
, cfqg
);
962 min_q
= min(cfqg
->busy_queues_avg
[rt
], busy
);
963 max_q
= max(cfqg
->busy_queues_avg
[rt
], busy
);
964 cfqg
->busy_queues_avg
[rt
] = (mult
* max_q
+ min_q
+ round
) /
966 return cfqg
->busy_queues_avg
[rt
];
969 static inline unsigned
970 cfq_group_slice(struct cfq_data
*cfqd
, struct cfq_group
*cfqg
)
972 return cfqd
->cfq_target_latency
* cfqg
->vfraction
>> CFQ_SERVICE_SHIFT
;
975 static inline unsigned
976 cfq_scaled_cfqq_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
978 unsigned slice
= cfq_prio_to_slice(cfqd
, cfqq
);
979 if (cfqd
->cfq_latency
) {
981 * interested queues (we consider only the ones with the same
982 * priority class in the cfq group)
984 unsigned iq
= cfq_group_get_avg_queues(cfqd
, cfqq
->cfqg
,
986 unsigned sync_slice
= cfqd
->cfq_slice
[1];
987 unsigned expect_latency
= sync_slice
* iq
;
988 unsigned group_slice
= cfq_group_slice(cfqd
, cfqq
->cfqg
);
990 if (expect_latency
> group_slice
) {
991 unsigned base_low_slice
= 2 * cfqd
->cfq_slice_idle
;
992 /* scale low_slice according to IO priority
993 * and sync vs async */
995 min(slice
, base_low_slice
* slice
/ sync_slice
);
996 /* the adapted slice value is scaled to fit all iqs
997 * into the target latency */
998 slice
= max(slice
* group_slice
/ expect_latency
,
1006 cfq_set_prio_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1008 unsigned slice
= cfq_scaled_cfqq_slice(cfqd
, cfqq
);
1010 cfqq
->slice_start
= jiffies
;
1011 cfqq
->slice_end
= jiffies
+ slice
;
1012 cfqq
->allocated_slice
= slice
;
1013 cfq_log_cfqq(cfqd
, cfqq
, "set_slice=%lu", cfqq
->slice_end
- jiffies
);
1017 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
1018 * isn't valid until the first request from the dispatch is activated
1019 * and the slice time set.
1021 static inline bool cfq_slice_used(struct cfq_queue
*cfqq
)
1023 if (cfq_cfqq_slice_new(cfqq
))
1025 if (time_before(jiffies
, cfqq
->slice_end
))
1032 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
1033 * We choose the request that is closest to the head right now. Distance
1034 * behind the head is penalized and only allowed to a certain extent.
1036 static struct request
*
1037 cfq_choose_req(struct cfq_data
*cfqd
, struct request
*rq1
, struct request
*rq2
, sector_t last
)
1039 sector_t s1
, s2
, d1
= 0, d2
= 0;
1040 unsigned long back_max
;
1041 #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
1042 #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
1043 unsigned wrap
= 0; /* bit mask: requests behind the disk head? */
1045 if (rq1
== NULL
|| rq1
== rq2
)
1050 if (rq_is_sync(rq1
) != rq_is_sync(rq2
))
1051 return rq_is_sync(rq1
) ? rq1
: rq2
;
1053 if ((rq1
->cmd_flags
^ rq2
->cmd_flags
) & REQ_PRIO
)
1054 return rq1
->cmd_flags
& REQ_PRIO
? rq1
: rq2
;
1056 s1
= blk_rq_pos(rq1
);
1057 s2
= blk_rq_pos(rq2
);
1060 * by definition, 1KiB is 2 sectors
1062 back_max
= cfqd
->cfq_back_max
* 2;
1065 * Strict one way elevator _except_ in the case where we allow
1066 * short backward seeks which are biased as twice the cost of a
1067 * similar forward seek.
1071 else if (s1
+ back_max
>= last
)
1072 d1
= (last
- s1
) * cfqd
->cfq_back_penalty
;
1074 wrap
|= CFQ_RQ1_WRAP
;
1078 else if (s2
+ back_max
>= last
)
1079 d2
= (last
- s2
) * cfqd
->cfq_back_penalty
;
1081 wrap
|= CFQ_RQ2_WRAP
;
1083 /* Found required data */
1086 * By doing switch() on the bit mask "wrap" we avoid having to
1087 * check two variables for all permutations: --> faster!
1090 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
1106 case (CFQ_RQ1_WRAP
|CFQ_RQ2_WRAP
): /* both rqs wrapped */
1109 * Since both rqs are wrapped,
1110 * start with the one that's further behind head
1111 * (--> only *one* back seek required),
1112 * since back seek takes more time than forward.
1122 * The below is leftmost cache rbtree addon
1124 static struct cfq_queue
*cfq_rb_first(struct cfq_rb_root
*root
)
1126 /* Service tree is empty */
1131 root
->left
= rb_first(&root
->rb
);
1134 return rb_entry(root
->left
, struct cfq_queue
, rb_node
);
1139 static struct cfq_group
*cfq_rb_first_group(struct cfq_rb_root
*root
)
1142 root
->left
= rb_first(&root
->rb
);
1145 return rb_entry_cfqg(root
->left
);
1150 static void rb_erase_init(struct rb_node
*n
, struct rb_root
*root
)
1156 static void cfq_rb_erase(struct rb_node
*n
, struct cfq_rb_root
*root
)
1158 if (root
->left
== n
)
1160 rb_erase_init(n
, &root
->rb
);
1165 * would be nice to take fifo expire time into account as well
1167 static struct request
*
1168 cfq_find_next_rq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1169 struct request
*last
)
1171 struct rb_node
*rbnext
= rb_next(&last
->rb_node
);
1172 struct rb_node
*rbprev
= rb_prev(&last
->rb_node
);
1173 struct request
*next
= NULL
, *prev
= NULL
;
1175 BUG_ON(RB_EMPTY_NODE(&last
->rb_node
));
1178 prev
= rb_entry_rq(rbprev
);
1181 next
= rb_entry_rq(rbnext
);
1183 rbnext
= rb_first(&cfqq
->sort_list
);
1184 if (rbnext
&& rbnext
!= &last
->rb_node
)
1185 next
= rb_entry_rq(rbnext
);
1188 return cfq_choose_req(cfqd
, next
, prev
, blk_rq_pos(last
));
1191 static unsigned long cfq_slice_offset(struct cfq_data
*cfqd
,
1192 struct cfq_queue
*cfqq
)
1195 * just an approximation, should be ok.
1197 return (cfqq
->cfqg
->nr_cfqq
- 1) * (cfq_prio_slice(cfqd
, 1, 0) -
1198 cfq_prio_slice(cfqd
, cfq_cfqq_sync(cfqq
), cfqq
->ioprio
));
1202 cfqg_key(struct cfq_rb_root
*st
, struct cfq_group
*cfqg
)
1204 return cfqg
->vdisktime
- st
->min_vdisktime
;
1208 __cfq_group_service_tree_add(struct cfq_rb_root
*st
, struct cfq_group
*cfqg
)
1210 struct rb_node
**node
= &st
->rb
.rb_node
;
1211 struct rb_node
*parent
= NULL
;
1212 struct cfq_group
*__cfqg
;
1213 s64 key
= cfqg_key(st
, cfqg
);
1216 while (*node
!= NULL
) {
1218 __cfqg
= rb_entry_cfqg(parent
);
1220 if (key
< cfqg_key(st
, __cfqg
))
1221 node
= &parent
->rb_left
;
1223 node
= &parent
->rb_right
;
1229 st
->left
= &cfqg
->rb_node
;
1231 rb_link_node(&cfqg
->rb_node
, parent
, node
);
1232 rb_insert_color(&cfqg
->rb_node
, &st
->rb
);
1236 cfq_update_group_weight(struct cfq_group
*cfqg
)
1238 BUG_ON(!RB_EMPTY_NODE(&cfqg
->rb_node
));
1240 if (cfqg
->new_weight
) {
1241 cfqg
->weight
= cfqg
->new_weight
;
1242 cfqg
->new_weight
= 0;
1245 if (cfqg
->new_leaf_weight
) {
1246 cfqg
->leaf_weight
= cfqg
->new_leaf_weight
;
1247 cfqg
->new_leaf_weight
= 0;
1252 cfq_group_service_tree_add(struct cfq_rb_root
*st
, struct cfq_group
*cfqg
)
1254 unsigned int vfr
= 1 << CFQ_SERVICE_SHIFT
; /* start with 1 */
1255 struct cfq_group
*pos
= cfqg
;
1256 struct cfq_group
*parent
;
1259 /* add to the service tree */
1260 BUG_ON(!RB_EMPTY_NODE(&cfqg
->rb_node
));
1262 cfq_update_group_weight(cfqg
);
1263 __cfq_group_service_tree_add(st
, cfqg
);
1266 * Activate @cfqg and calculate the portion of vfraction @cfqg is
1267 * entitled to. vfraction is calculated by walking the tree
1268 * towards the root calculating the fraction it has at each level.
1269 * The compounded ratio is how much vfraction @cfqg owns.
1271 * Start with the proportion tasks in this cfqg has against active
1272 * children cfqgs - its leaf_weight against children_weight.
1274 propagate
= !pos
->nr_active
++;
1275 pos
->children_weight
+= pos
->leaf_weight
;
1276 vfr
= vfr
* pos
->leaf_weight
/ pos
->children_weight
;
1279 * Compound ->weight walking up the tree. Both activation and
1280 * vfraction calculation are done in the same loop. Propagation
1281 * stops once an already activated node is met. vfraction
1282 * calculation should always continue to the root.
1284 while ((parent
= cfqg_parent(pos
))) {
1286 propagate
= !parent
->nr_active
++;
1287 parent
->children_weight
+= pos
->weight
;
1289 vfr
= vfr
* pos
->weight
/ parent
->children_weight
;
1293 cfqg
->vfraction
= max_t(unsigned, vfr
, 1);
1297 cfq_group_notify_queue_add(struct cfq_data
*cfqd
, struct cfq_group
*cfqg
)
1299 struct cfq_rb_root
*st
= &cfqd
->grp_service_tree
;
1300 struct cfq_group
*__cfqg
;
1304 if (!RB_EMPTY_NODE(&cfqg
->rb_node
))
1308 * Currently put the group at the end. Later implement something
1309 * so that groups get lesser vtime based on their weights, so that
1310 * if group does not loose all if it was not continuously backlogged.
1312 n
= rb_last(&st
->rb
);
1314 __cfqg
= rb_entry_cfqg(n
);
1315 cfqg
->vdisktime
= __cfqg
->vdisktime
+ CFQ_IDLE_DELAY
;
1317 cfqg
->vdisktime
= st
->min_vdisktime
;
1318 cfq_group_service_tree_add(st
, cfqg
);
1322 cfq_group_service_tree_del(struct cfq_rb_root
*st
, struct cfq_group
*cfqg
)
1324 struct cfq_group
*pos
= cfqg
;
1328 * Undo activation from cfq_group_service_tree_add(). Deactivate
1329 * @cfqg and propagate deactivation upwards.
1331 propagate
= !--pos
->nr_active
;
1332 pos
->children_weight
-= pos
->leaf_weight
;
1335 struct cfq_group
*parent
= cfqg_parent(pos
);
1337 /* @pos has 0 nr_active at this point */
1338 WARN_ON_ONCE(pos
->children_weight
);
1344 propagate
= !--parent
->nr_active
;
1345 parent
->children_weight
-= pos
->weight
;
1349 /* remove from the service tree */
1350 if (!RB_EMPTY_NODE(&cfqg
->rb_node
))
1351 cfq_rb_erase(&cfqg
->rb_node
, st
);
1355 cfq_group_notify_queue_del(struct cfq_data
*cfqd
, struct cfq_group
*cfqg
)
1357 struct cfq_rb_root
*st
= &cfqd
->grp_service_tree
;
1359 BUG_ON(cfqg
->nr_cfqq
< 1);
1362 /* If there are other cfq queues under this group, don't delete it */
1366 cfq_log_cfqg(cfqd
, cfqg
, "del_from_rr group");
1367 cfq_group_service_tree_del(st
, cfqg
);
1368 cfqg
->saved_wl_slice
= 0;
1369 cfqg_stats_update_dequeue(cfqg
);
1372 static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue
*cfqq
,
1373 unsigned int *unaccounted_time
)
1375 unsigned int slice_used
;
1378 * Queue got expired before even a single request completed or
1379 * got expired immediately after first request completion.
1381 if (!cfqq
->slice_start
|| cfqq
->slice_start
== jiffies
) {
1383 * Also charge the seek time incurred to the group, otherwise
1384 * if there are mutiple queues in the group, each can dispatch
1385 * a single request on seeky media and cause lots of seek time
1386 * and group will never know it.
1388 slice_used
= max_t(unsigned, (jiffies
- cfqq
->dispatch_start
),
1391 slice_used
= jiffies
- cfqq
->slice_start
;
1392 if (slice_used
> cfqq
->allocated_slice
) {
1393 *unaccounted_time
= slice_used
- cfqq
->allocated_slice
;
1394 slice_used
= cfqq
->allocated_slice
;
1396 if (time_after(cfqq
->slice_start
, cfqq
->dispatch_start
))
1397 *unaccounted_time
+= cfqq
->slice_start
-
1398 cfqq
->dispatch_start
;
1404 static void cfq_group_served(struct cfq_data
*cfqd
, struct cfq_group
*cfqg
,
1405 struct cfq_queue
*cfqq
)
1407 struct cfq_rb_root
*st
= &cfqd
->grp_service_tree
;
1408 unsigned int used_sl
, charge
, unaccounted_sl
= 0;
1409 int nr_sync
= cfqg
->nr_cfqq
- cfqg_busy_async_queues(cfqd
, cfqg
)
1410 - cfqg
->service_tree_idle
.count
;
1413 BUG_ON(nr_sync
< 0);
1414 used_sl
= charge
= cfq_cfqq_slice_usage(cfqq
, &unaccounted_sl
);
1416 if (iops_mode(cfqd
))
1417 charge
= cfqq
->slice_dispatch
;
1418 else if (!cfq_cfqq_sync(cfqq
) && !nr_sync
)
1419 charge
= cfqq
->allocated_slice
;
1422 * Can't update vdisktime while on service tree and cfqg->vfraction
1423 * is valid only while on it. Cache vfr, leave the service tree,
1424 * update vdisktime and go back on. The re-addition to the tree
1425 * will also update the weights as necessary.
1427 vfr
= cfqg
->vfraction
;
1428 cfq_group_service_tree_del(st
, cfqg
);
1429 cfqg
->vdisktime
+= cfqg_scale_charge(charge
, vfr
);
1430 cfq_group_service_tree_add(st
, cfqg
);
1432 /* This group is being expired. Save the context */
1433 if (time_after(cfqd
->workload_expires
, jiffies
)) {
1434 cfqg
->saved_wl_slice
= cfqd
->workload_expires
1436 cfqg
->saved_wl_type
= cfqd
->serving_wl_type
;
1437 cfqg
->saved_wl_class
= cfqd
->serving_wl_class
;
1439 cfqg
->saved_wl_slice
= 0;
1441 cfq_log_cfqg(cfqd
, cfqg
, "served: vt=%llu min_vt=%llu", cfqg
->vdisktime
,
1443 cfq_log_cfqq(cfqq
->cfqd
, cfqq
,
1444 "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
1445 used_sl
, cfqq
->slice_dispatch
, charge
,
1446 iops_mode(cfqd
), cfqq
->nr_sectors
);
1447 cfqg_stats_update_timeslice_used(cfqg
, used_sl
, unaccounted_sl
);
1448 cfqg_stats_set_start_empty_time(cfqg
);
1452 * cfq_init_cfqg_base - initialize base part of a cfq_group
1453 * @cfqg: cfq_group to initialize
1455 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1456 * is enabled or not.
1458 static void cfq_init_cfqg_base(struct cfq_group
*cfqg
)
1460 struct cfq_rb_root
*st
;
1463 for_each_cfqg_st(cfqg
, i
, j
, st
)
1465 RB_CLEAR_NODE(&cfqg
->rb_node
);
1467 cfqg
->ttime
.last_end_request
= jiffies
;
1470 #ifdef CONFIG_CFQ_GROUP_IOSCHED
1471 static void cfq_pd_init(struct blkcg_gq
*blkg
)
1473 struct cfq_group
*cfqg
= blkg_to_cfqg(blkg
);
1475 cfq_init_cfqg_base(cfqg
);
1476 cfqg
->weight
= blkg
->blkcg
->cfq_weight
;
1477 cfqg
->leaf_weight
= blkg
->blkcg
->cfq_leaf_weight
;
1481 * Search for the cfq group current task belongs to. request_queue lock must
1484 static struct cfq_group
*cfq_lookup_create_cfqg(struct cfq_data
*cfqd
,
1485 struct blkcg
*blkcg
)
1487 struct request_queue
*q
= cfqd
->queue
;
1488 struct cfq_group
*cfqg
= NULL
;
1490 /* avoid lookup for the common case where there's no blkcg */
1491 if (blkcg
== &blkcg_root
) {
1492 cfqg
= cfqd
->root_group
;
1494 struct blkcg_gq
*blkg
;
1496 blkg
= blkg_lookup_create(blkcg
, q
);
1498 cfqg
= blkg_to_cfqg(blkg
);
1504 static void cfq_link_cfqq_cfqg(struct cfq_queue
*cfqq
, struct cfq_group
*cfqg
)
1506 /* Currently, all async queues are mapped to root group */
1507 if (!cfq_cfqq_sync(cfqq
))
1508 cfqg
= cfqq
->cfqd
->root_group
;
1511 /* cfqq reference on cfqg */
1515 static u64
cfqg_prfill_weight_device(struct seq_file
*sf
,
1516 struct blkg_policy_data
*pd
, int off
)
1518 struct cfq_group
*cfqg
= pd_to_cfqg(pd
);
1520 if (!cfqg
->dev_weight
)
1522 return __blkg_prfill_u64(sf
, pd
, cfqg
->dev_weight
);
1525 static int cfqg_print_weight_device(struct cgroup
*cgrp
, struct cftype
*cft
,
1526 struct seq_file
*sf
)
1528 blkcg_print_blkgs(sf
, cgroup_to_blkcg(cgrp
),
1529 cfqg_prfill_weight_device
, &blkcg_policy_cfq
, 0,
1534 static u64
cfqg_prfill_leaf_weight_device(struct seq_file
*sf
,
1535 struct blkg_policy_data
*pd
, int off
)
1537 struct cfq_group
*cfqg
= pd_to_cfqg(pd
);
1539 if (!cfqg
->dev_leaf_weight
)
1541 return __blkg_prfill_u64(sf
, pd
, cfqg
->dev_leaf_weight
);
1544 static int cfqg_print_leaf_weight_device(struct cgroup
*cgrp
,
1546 struct seq_file
*sf
)
1548 blkcg_print_blkgs(sf
, cgroup_to_blkcg(cgrp
),
1549 cfqg_prfill_leaf_weight_device
, &blkcg_policy_cfq
, 0,
1554 static int cfq_print_weight(struct cgroup
*cgrp
, struct cftype
*cft
,
1555 struct seq_file
*sf
)
1557 seq_printf(sf
, "%u\n", cgroup_to_blkcg(cgrp
)->cfq_weight
);
1561 static int cfq_print_leaf_weight(struct cgroup
*cgrp
, struct cftype
*cft
,
1562 struct seq_file
*sf
)
1564 seq_printf(sf
, "%u\n",
1565 cgroup_to_blkcg(cgrp
)->cfq_leaf_weight
);
1569 static int __cfqg_set_weight_device(struct cgroup
*cgrp
, struct cftype
*cft
,
1570 const char *buf
, bool is_leaf_weight
)
1572 struct blkcg
*blkcg
= cgroup_to_blkcg(cgrp
);
1573 struct blkg_conf_ctx ctx
;
1574 struct cfq_group
*cfqg
;
1577 ret
= blkg_conf_prep(blkcg
, &blkcg_policy_cfq
, buf
, &ctx
);
1582 cfqg
= blkg_to_cfqg(ctx
.blkg
);
1583 if (!ctx
.v
|| (ctx
.v
>= CFQ_WEIGHT_MIN
&& ctx
.v
<= CFQ_WEIGHT_MAX
)) {
1584 if (!is_leaf_weight
) {
1585 cfqg
->dev_weight
= ctx
.v
;
1586 cfqg
->new_weight
= ctx
.v
?: blkcg
->cfq_weight
;
1588 cfqg
->dev_leaf_weight
= ctx
.v
;
1589 cfqg
->new_leaf_weight
= ctx
.v
?: blkcg
->cfq_leaf_weight
;
1594 blkg_conf_finish(&ctx
);
1598 static int cfqg_set_weight_device(struct cgroup
*cgrp
, struct cftype
*cft
,
1601 return __cfqg_set_weight_device(cgrp
, cft
, buf
, false);
1604 static int cfqg_set_leaf_weight_device(struct cgroup
*cgrp
, struct cftype
*cft
,
1607 return __cfqg_set_weight_device(cgrp
, cft
, buf
, true);
1610 static int __cfq_set_weight(struct cgroup
*cgrp
, struct cftype
*cft
, u64 val
,
1611 bool is_leaf_weight
)
1613 struct blkcg
*blkcg
= cgroup_to_blkcg(cgrp
);
1614 struct blkcg_gq
*blkg
;
1615 struct hlist_node
*n
;
1617 if (val
< CFQ_WEIGHT_MIN
|| val
> CFQ_WEIGHT_MAX
)
1620 spin_lock_irq(&blkcg
->lock
);
1622 if (!is_leaf_weight
)
1623 blkcg
->cfq_weight
= val
;
1625 blkcg
->cfq_leaf_weight
= val
;
1627 hlist_for_each_entry(blkg
, n
, &blkcg
->blkg_list
, blkcg_node
) {
1628 struct cfq_group
*cfqg
= blkg_to_cfqg(blkg
);
1633 if (!is_leaf_weight
) {
1634 if (!cfqg
->dev_weight
)
1635 cfqg
->new_weight
= blkcg
->cfq_weight
;
1637 if (!cfqg
->dev_leaf_weight
)
1638 cfqg
->new_leaf_weight
= blkcg
->cfq_leaf_weight
;
1642 spin_unlock_irq(&blkcg
->lock
);
1646 static int cfq_set_weight(struct cgroup
*cgrp
, struct cftype
*cft
, u64 val
)
1648 return __cfq_set_weight(cgrp
, cft
, val
, false);
1651 static int cfq_set_leaf_weight(struct cgroup
*cgrp
, struct cftype
*cft
, u64 val
)
1653 return __cfq_set_weight(cgrp
, cft
, val
, true);
1656 static int cfqg_print_stat(struct cgroup
*cgrp
, struct cftype
*cft
,
1657 struct seq_file
*sf
)
1659 struct blkcg
*blkcg
= cgroup_to_blkcg(cgrp
);
1661 blkcg_print_blkgs(sf
, blkcg
, blkg_prfill_stat
, &blkcg_policy_cfq
,
1662 cft
->private, false);
1666 static int cfqg_print_rwstat(struct cgroup
*cgrp
, struct cftype
*cft
,
1667 struct seq_file
*sf
)
1669 struct blkcg
*blkcg
= cgroup_to_blkcg(cgrp
);
1671 blkcg_print_blkgs(sf
, blkcg
, blkg_prfill_rwstat
, &blkcg_policy_cfq
,
1672 cft
->private, true);
1676 #ifdef CONFIG_DEBUG_BLK_CGROUP
1677 static u64
cfqg_prfill_avg_queue_size(struct seq_file
*sf
,
1678 struct blkg_policy_data
*pd
, int off
)
1680 struct cfq_group
*cfqg
= pd_to_cfqg(pd
);
1681 u64 samples
= blkg_stat_read(&cfqg
->stats
.avg_queue_size_samples
);
1685 v
= blkg_stat_read(&cfqg
->stats
.avg_queue_size_sum
);
1688 __blkg_prfill_u64(sf
, pd
, v
);
1692 /* print avg_queue_size */
1693 static int cfqg_print_avg_queue_size(struct cgroup
*cgrp
, struct cftype
*cft
,
1694 struct seq_file
*sf
)
1696 struct blkcg
*blkcg
= cgroup_to_blkcg(cgrp
);
1698 blkcg_print_blkgs(sf
, blkcg
, cfqg_prfill_avg_queue_size
,
1699 &blkcg_policy_cfq
, 0, false);
1702 #endif /* CONFIG_DEBUG_BLK_CGROUP */
1704 static struct cftype cfq_blkcg_files
[] = {
1705 /* on root, weight is mapped to leaf_weight */
1707 .name
= "weight_device",
1708 .flags
= CFTYPE_ONLY_ON_ROOT
,
1709 .read_seq_string
= cfqg_print_leaf_weight_device
,
1710 .write_string
= cfqg_set_leaf_weight_device
,
1711 .max_write_len
= 256,
1715 .flags
= CFTYPE_ONLY_ON_ROOT
,
1716 .read_seq_string
= cfq_print_leaf_weight
,
1717 .write_u64
= cfq_set_leaf_weight
,
1720 /* no such mapping necessary for !roots */
1722 .name
= "weight_device",
1723 .flags
= CFTYPE_NOT_ON_ROOT
,
1724 .read_seq_string
= cfqg_print_weight_device
,
1725 .write_string
= cfqg_set_weight_device
,
1726 .max_write_len
= 256,
1730 .flags
= CFTYPE_NOT_ON_ROOT
,
1731 .read_seq_string
= cfq_print_weight
,
1732 .write_u64
= cfq_set_weight
,
1736 .name
= "leaf_weight_device",
1737 .read_seq_string
= cfqg_print_leaf_weight_device
,
1738 .write_string
= cfqg_set_leaf_weight_device
,
1739 .max_write_len
= 256,
1742 .name
= "leaf_weight",
1743 .read_seq_string
= cfq_print_leaf_weight
,
1744 .write_u64
= cfq_set_leaf_weight
,
1749 .private = offsetof(struct cfq_group
, stats
.time
),
1750 .read_seq_string
= cfqg_print_stat
,
1754 .private = offsetof(struct cfq_group
, stats
.sectors
),
1755 .read_seq_string
= cfqg_print_stat
,
1758 .name
= "io_service_bytes",
1759 .private = offsetof(struct cfq_group
, stats
.service_bytes
),
1760 .read_seq_string
= cfqg_print_rwstat
,
1763 .name
= "io_serviced",
1764 .private = offsetof(struct cfq_group
, stats
.serviced
),
1765 .read_seq_string
= cfqg_print_rwstat
,
1768 .name
= "io_service_time",
1769 .private = offsetof(struct cfq_group
, stats
.service_time
),
1770 .read_seq_string
= cfqg_print_rwstat
,
1773 .name
= "io_wait_time",
1774 .private = offsetof(struct cfq_group
, stats
.wait_time
),
1775 .read_seq_string
= cfqg_print_rwstat
,
1778 .name
= "io_merged",
1779 .private = offsetof(struct cfq_group
, stats
.merged
),
1780 .read_seq_string
= cfqg_print_rwstat
,
1783 .name
= "io_queued",
1784 .private = offsetof(struct cfq_group
, stats
.queued
),
1785 .read_seq_string
= cfqg_print_rwstat
,
1787 #ifdef CONFIG_DEBUG_BLK_CGROUP
1789 .name
= "avg_queue_size",
1790 .read_seq_string
= cfqg_print_avg_queue_size
,
1793 .name
= "group_wait_time",
1794 .private = offsetof(struct cfq_group
, stats
.group_wait_time
),
1795 .read_seq_string
= cfqg_print_stat
,
1798 .name
= "idle_time",
1799 .private = offsetof(struct cfq_group
, stats
.idle_time
),
1800 .read_seq_string
= cfqg_print_stat
,
1803 .name
= "empty_time",
1804 .private = offsetof(struct cfq_group
, stats
.empty_time
),
1805 .read_seq_string
= cfqg_print_stat
,
1809 .private = offsetof(struct cfq_group
, stats
.dequeue
),
1810 .read_seq_string
= cfqg_print_stat
,
1813 .name
= "unaccounted_time",
1814 .private = offsetof(struct cfq_group
, stats
.unaccounted_time
),
1815 .read_seq_string
= cfqg_print_stat
,
1817 #endif /* CONFIG_DEBUG_BLK_CGROUP */
1820 #else /* GROUP_IOSCHED */
1821 static struct cfq_group
*cfq_lookup_create_cfqg(struct cfq_data
*cfqd
,
1822 struct blkcg
*blkcg
)
1824 return cfqd
->root_group
;
1828 cfq_link_cfqq_cfqg(struct cfq_queue
*cfqq
, struct cfq_group
*cfqg
) {
1832 #endif /* GROUP_IOSCHED */
1835 * The cfqd->service_trees holds all pending cfq_queue's that have
1836 * requests waiting to be processed. It is sorted in the order that
1837 * we will service the queues.
1839 static void cfq_service_tree_add(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1842 struct rb_node
**p
, *parent
;
1843 struct cfq_queue
*__cfqq
;
1844 unsigned long rb_key
;
1845 struct cfq_rb_root
*st
;
1849 st
= st_for(cfqq
->cfqg
, cfqq_class(cfqq
), cfqq_type(cfqq
));
1850 if (cfq_class_idle(cfqq
)) {
1851 rb_key
= CFQ_IDLE_DELAY
;
1852 parent
= rb_last(&st
->rb
);
1853 if (parent
&& parent
!= &cfqq
->rb_node
) {
1854 __cfqq
= rb_entry(parent
, struct cfq_queue
, rb_node
);
1855 rb_key
+= __cfqq
->rb_key
;
1858 } else if (!add_front
) {
1860 * Get our rb key offset. Subtract any residual slice
1861 * value carried from last service. A negative resid
1862 * count indicates slice overrun, and this should position
1863 * the next service time further away in the tree.
1865 rb_key
= cfq_slice_offset(cfqd
, cfqq
) + jiffies
;
1866 rb_key
-= cfqq
->slice_resid
;
1867 cfqq
->slice_resid
= 0;
1870 __cfqq
= cfq_rb_first(st
);
1871 rb_key
+= __cfqq
? __cfqq
->rb_key
: jiffies
;
1874 if (!RB_EMPTY_NODE(&cfqq
->rb_node
)) {
1877 * same position, nothing more to do
1879 if (rb_key
== cfqq
->rb_key
&& cfqq
->service_tree
== st
)
1882 cfq_rb_erase(&cfqq
->rb_node
, cfqq
->service_tree
);
1883 cfqq
->service_tree
= NULL
;
1888 cfqq
->service_tree
= st
;
1889 p
= &st
->rb
.rb_node
;
1892 __cfqq
= rb_entry(parent
, struct cfq_queue
, rb_node
);
1895 * sort by key, that represents service time.
1897 if (time_before(rb_key
, __cfqq
->rb_key
))
1898 p
= &parent
->rb_left
;
1900 p
= &parent
->rb_right
;
1906 st
->left
= &cfqq
->rb_node
;
1908 cfqq
->rb_key
= rb_key
;
1909 rb_link_node(&cfqq
->rb_node
, parent
, p
);
1910 rb_insert_color(&cfqq
->rb_node
, &st
->rb
);
1912 if (add_front
|| !new_cfqq
)
1914 cfq_group_notify_queue_add(cfqd
, cfqq
->cfqg
);
1917 static struct cfq_queue
*
1918 cfq_prio_tree_lookup(struct cfq_data
*cfqd
, struct rb_root
*root
,
1919 sector_t sector
, struct rb_node
**ret_parent
,
1920 struct rb_node
***rb_link
)
1922 struct rb_node
**p
, *parent
;
1923 struct cfq_queue
*cfqq
= NULL
;
1931 cfqq
= rb_entry(parent
, struct cfq_queue
, p_node
);
1934 * Sort strictly based on sector. Smallest to the left,
1935 * largest to the right.
1937 if (sector
> blk_rq_pos(cfqq
->next_rq
))
1938 n
= &(*p
)->rb_right
;
1939 else if (sector
< blk_rq_pos(cfqq
->next_rq
))
1947 *ret_parent
= parent
;
1953 static void cfq_prio_tree_add(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1955 struct rb_node
**p
, *parent
;
1956 struct cfq_queue
*__cfqq
;
1959 rb_erase(&cfqq
->p_node
, cfqq
->p_root
);
1960 cfqq
->p_root
= NULL
;
1963 if (cfq_class_idle(cfqq
))
1968 cfqq
->p_root
= &cfqd
->prio_trees
[cfqq
->org_ioprio
];
1969 __cfqq
= cfq_prio_tree_lookup(cfqd
, cfqq
->p_root
,
1970 blk_rq_pos(cfqq
->next_rq
), &parent
, &p
);
1972 rb_link_node(&cfqq
->p_node
, parent
, p
);
1973 rb_insert_color(&cfqq
->p_node
, cfqq
->p_root
);
1975 cfqq
->p_root
= NULL
;
1979 * Update cfqq's position in the service tree.
1981 static void cfq_resort_rr_list(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1984 * Resorting requires the cfqq to be on the RR list already.
1986 if (cfq_cfqq_on_rr(cfqq
)) {
1987 cfq_service_tree_add(cfqd
, cfqq
, 0);
1988 cfq_prio_tree_add(cfqd
, cfqq
);
1993 * add to busy list of queues for service, trying to be fair in ordering
1994 * the pending list according to last request service
1996 static void cfq_add_cfqq_rr(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1998 cfq_log_cfqq(cfqd
, cfqq
, "add_to_rr");
1999 BUG_ON(cfq_cfqq_on_rr(cfqq
));
2000 cfq_mark_cfqq_on_rr(cfqq
);
2001 cfqd
->busy_queues
++;
2002 if (cfq_cfqq_sync(cfqq
))
2003 cfqd
->busy_sync_queues
++;
2005 cfq_resort_rr_list(cfqd
, cfqq
);
2009 * Called when the cfqq no longer has requests pending, remove it from
2012 static void cfq_del_cfqq_rr(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
2014 cfq_log_cfqq(cfqd
, cfqq
, "del_from_rr");
2015 BUG_ON(!cfq_cfqq_on_rr(cfqq
));
2016 cfq_clear_cfqq_on_rr(cfqq
);
2018 if (!RB_EMPTY_NODE(&cfqq
->rb_node
)) {
2019 cfq_rb_erase(&cfqq
->rb_node
, cfqq
->service_tree
);
2020 cfqq
->service_tree
= NULL
;
2023 rb_erase(&cfqq
->p_node
, cfqq
->p_root
);
2024 cfqq
->p_root
= NULL
;
2027 cfq_group_notify_queue_del(cfqd
, cfqq
->cfqg
);
2028 BUG_ON(!cfqd
->busy_queues
);
2029 cfqd
->busy_queues
--;
2030 if (cfq_cfqq_sync(cfqq
))
2031 cfqd
->busy_sync_queues
--;
2035 * rb tree support functions
2037 static void cfq_del_rq_rb(struct request
*rq
)
2039 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
2040 const int sync
= rq_is_sync(rq
);
2042 BUG_ON(!cfqq
->queued
[sync
]);
2043 cfqq
->queued
[sync
]--;
2045 elv_rb_del(&cfqq
->sort_list
, rq
);
2047 if (cfq_cfqq_on_rr(cfqq
) && RB_EMPTY_ROOT(&cfqq
->sort_list
)) {
2049 * Queue will be deleted from service tree when we actually
2050 * expire it later. Right now just remove it from prio tree
2054 rb_erase(&cfqq
->p_node
, cfqq
->p_root
);
2055 cfqq
->p_root
= NULL
;
2060 static void cfq_add_rq_rb(struct request
*rq
)
2062 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
2063 struct cfq_data
*cfqd
= cfqq
->cfqd
;
2064 struct request
*prev
;
2066 cfqq
->queued
[rq_is_sync(rq
)]++;
2068 elv_rb_add(&cfqq
->sort_list
, rq
);
2070 if (!cfq_cfqq_on_rr(cfqq
))
2071 cfq_add_cfqq_rr(cfqd
, cfqq
);
2074 * check if this request is a better next-serve candidate
2076 prev
= cfqq
->next_rq
;
2077 cfqq
->next_rq
= cfq_choose_req(cfqd
, cfqq
->next_rq
, rq
, cfqd
->last_position
);
2080 * adjust priority tree position, if ->next_rq changes
2082 if (prev
!= cfqq
->next_rq
)
2083 cfq_prio_tree_add(cfqd
, cfqq
);
2085 BUG_ON(!cfqq
->next_rq
);
2088 static void cfq_reposition_rq_rb(struct cfq_queue
*cfqq
, struct request
*rq
)
2090 elv_rb_del(&cfqq
->sort_list
, rq
);
2091 cfqq
->queued
[rq_is_sync(rq
)]--;
2092 cfqg_stats_update_io_remove(RQ_CFQG(rq
), rq
->cmd_flags
);
2094 cfqg_stats_update_io_add(RQ_CFQG(rq
), cfqq
->cfqd
->serving_group
,
2098 static struct request
*
2099 cfq_find_rq_fmerge(struct cfq_data
*cfqd
, struct bio
*bio
)
2101 struct task_struct
*tsk
= current
;
2102 struct cfq_io_cq
*cic
;
2103 struct cfq_queue
*cfqq
;
2105 cic
= cfq_cic_lookup(cfqd
, tsk
->io_context
);
2109 cfqq
= cic_to_cfqq(cic
, cfq_bio_sync(bio
));
2111 sector_t sector
= bio
->bi_sector
+ bio_sectors(bio
);
2113 return elv_rb_find(&cfqq
->sort_list
, sector
);
2119 static void cfq_activate_request(struct request_queue
*q
, struct request
*rq
)
2121 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
2123 cfqd
->rq_in_driver
++;
2124 cfq_log_cfqq(cfqd
, RQ_CFQQ(rq
), "activate rq, drv=%d",
2125 cfqd
->rq_in_driver
);
2127 cfqd
->last_position
= blk_rq_pos(rq
) + blk_rq_sectors(rq
);
2130 static void cfq_deactivate_request(struct request_queue
*q
, struct request
*rq
)
2132 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
2134 WARN_ON(!cfqd
->rq_in_driver
);
2135 cfqd
->rq_in_driver
--;
2136 cfq_log_cfqq(cfqd
, RQ_CFQQ(rq
), "deactivate rq, drv=%d",
2137 cfqd
->rq_in_driver
);
2140 static void cfq_remove_request(struct request
*rq
)
2142 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
2144 if (cfqq
->next_rq
== rq
)
2145 cfqq
->next_rq
= cfq_find_next_rq(cfqq
->cfqd
, cfqq
, rq
);
2147 list_del_init(&rq
->queuelist
);
2150 cfqq
->cfqd
->rq_queued
--;
2151 cfqg_stats_update_io_remove(RQ_CFQG(rq
), rq
->cmd_flags
);
2152 if (rq
->cmd_flags
& REQ_PRIO
) {
2153 WARN_ON(!cfqq
->prio_pending
);
2154 cfqq
->prio_pending
--;
2158 static int cfq_merge(struct request_queue
*q
, struct request
**req
,
2161 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
2162 struct request
*__rq
;
2164 __rq
= cfq_find_rq_fmerge(cfqd
, bio
);
2165 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
2167 return ELEVATOR_FRONT_MERGE
;
2170 return ELEVATOR_NO_MERGE
;
2173 static void cfq_merged_request(struct request_queue
*q
, struct request
*req
,
2176 if (type
== ELEVATOR_FRONT_MERGE
) {
2177 struct cfq_queue
*cfqq
= RQ_CFQQ(req
);
2179 cfq_reposition_rq_rb(cfqq
, req
);
2183 static void cfq_bio_merged(struct request_queue
*q
, struct request
*req
,
2186 cfqg_stats_update_io_merged(RQ_CFQG(req
), bio
->bi_rw
);
2190 cfq_merged_requests(struct request_queue
*q
, struct request
*rq
,
2191 struct request
*next
)
2193 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
2194 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
2197 * reposition in fifo if next is older than rq
2199 if (!list_empty(&rq
->queuelist
) && !list_empty(&next
->queuelist
) &&
2200 time_before(rq_fifo_time(next
), rq_fifo_time(rq
)) &&
2201 cfqq
== RQ_CFQQ(next
)) {
2202 list_move(&rq
->queuelist
, &next
->queuelist
);
2203 rq_set_fifo_time(rq
, rq_fifo_time(next
));
2206 if (cfqq
->next_rq
== next
)
2208 cfq_remove_request(next
);
2209 cfqg_stats_update_io_merged(RQ_CFQG(rq
), next
->cmd_flags
);
2211 cfqq
= RQ_CFQQ(next
);
2213 * all requests of this queue are merged to other queues, delete it
2214 * from the service tree. If it's the active_queue,
2215 * cfq_dispatch_requests() will choose to expire it or do idle
2217 if (cfq_cfqq_on_rr(cfqq
) && RB_EMPTY_ROOT(&cfqq
->sort_list
) &&
2218 cfqq
!= cfqd
->active_queue
)
2219 cfq_del_cfqq_rr(cfqd
, cfqq
);
2222 static int cfq_allow_merge(struct request_queue
*q
, struct request
*rq
,
2225 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
2226 struct cfq_io_cq
*cic
;
2227 struct cfq_queue
*cfqq
;
2230 * Disallow merge of a sync bio into an async request.
2232 if (cfq_bio_sync(bio
) && !rq_is_sync(rq
))
2236 * Lookup the cfqq that this bio will be queued with and allow
2237 * merge only if rq is queued there.
2239 cic
= cfq_cic_lookup(cfqd
, current
->io_context
);
2243 cfqq
= cic_to_cfqq(cic
, cfq_bio_sync(bio
));
2244 return cfqq
== RQ_CFQQ(rq
);
2247 static inline void cfq_del_timer(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
2249 del_timer(&cfqd
->idle_slice_timer
);
2250 cfqg_stats_update_idle_time(cfqq
->cfqg
);
2253 static void __cfq_set_active_queue(struct cfq_data
*cfqd
,
2254 struct cfq_queue
*cfqq
)
2257 cfq_log_cfqq(cfqd
, cfqq
, "set_active wl_class:%d wl_type:%d",
2258 cfqd
->serving_wl_class
, cfqd
->serving_wl_type
);
2259 cfqg_stats_update_avg_queue_size(cfqq
->cfqg
);
2260 cfqq
->slice_start
= 0;
2261 cfqq
->dispatch_start
= jiffies
;
2262 cfqq
->allocated_slice
= 0;
2263 cfqq
->slice_end
= 0;
2264 cfqq
->slice_dispatch
= 0;
2265 cfqq
->nr_sectors
= 0;
2267 cfq_clear_cfqq_wait_request(cfqq
);
2268 cfq_clear_cfqq_must_dispatch(cfqq
);
2269 cfq_clear_cfqq_must_alloc_slice(cfqq
);
2270 cfq_clear_cfqq_fifo_expire(cfqq
);
2271 cfq_mark_cfqq_slice_new(cfqq
);
2273 cfq_del_timer(cfqd
, cfqq
);
2276 cfqd
->active_queue
= cfqq
;
2280 * current cfqq expired its slice (or was too idle), select new one
2283 __cfq_slice_expired(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
2286 cfq_log_cfqq(cfqd
, cfqq
, "slice expired t=%d", timed_out
);
2288 if (cfq_cfqq_wait_request(cfqq
))
2289 cfq_del_timer(cfqd
, cfqq
);
2291 cfq_clear_cfqq_wait_request(cfqq
);
2292 cfq_clear_cfqq_wait_busy(cfqq
);
2295 * If this cfqq is shared between multiple processes, check to
2296 * make sure that those processes are still issuing I/Os within
2297 * the mean seek distance. If not, it may be time to break the
2298 * queues apart again.
2300 if (cfq_cfqq_coop(cfqq
) && CFQQ_SEEKY(cfqq
))
2301 cfq_mark_cfqq_split_coop(cfqq
);
2304 * store what was left of this slice, if the queue idled/timed out
2307 if (cfq_cfqq_slice_new(cfqq
))
2308 cfqq
->slice_resid
= cfq_scaled_cfqq_slice(cfqd
, cfqq
);
2310 cfqq
->slice_resid
= cfqq
->slice_end
- jiffies
;
2311 cfq_log_cfqq(cfqd
, cfqq
, "resid=%ld", cfqq
->slice_resid
);
2314 cfq_group_served(cfqd
, cfqq
->cfqg
, cfqq
);
2316 if (cfq_cfqq_on_rr(cfqq
) && RB_EMPTY_ROOT(&cfqq
->sort_list
))
2317 cfq_del_cfqq_rr(cfqd
, cfqq
);
2319 cfq_resort_rr_list(cfqd
, cfqq
);
2321 if (cfqq
== cfqd
->active_queue
)
2322 cfqd
->active_queue
= NULL
;
2324 if (cfqd
->active_cic
) {
2325 put_io_context(cfqd
->active_cic
->icq
.ioc
);
2326 cfqd
->active_cic
= NULL
;
2330 static inline void cfq_slice_expired(struct cfq_data
*cfqd
, bool timed_out
)
2332 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
2335 __cfq_slice_expired(cfqd
, cfqq
, timed_out
);
2339 * Get next queue for service. Unless we have a queue preemption,
2340 * we'll simply select the first cfqq in the service tree.
2342 static struct cfq_queue
*cfq_get_next_queue(struct cfq_data
*cfqd
)
2344 struct cfq_rb_root
*st
= st_for(cfqd
->serving_group
,
2345 cfqd
->serving_wl_class
, cfqd
->serving_wl_type
);
2347 if (!cfqd
->rq_queued
)
2350 /* There is nothing to dispatch */
2353 if (RB_EMPTY_ROOT(&st
->rb
))
2355 return cfq_rb_first(st
);
2358 static struct cfq_queue
*cfq_get_next_queue_forced(struct cfq_data
*cfqd
)
2360 struct cfq_group
*cfqg
;
2361 struct cfq_queue
*cfqq
;
2363 struct cfq_rb_root
*st
;
2365 if (!cfqd
->rq_queued
)
2368 cfqg
= cfq_get_next_cfqg(cfqd
);
2372 for_each_cfqg_st(cfqg
, i
, j
, st
)
2373 if ((cfqq
= cfq_rb_first(st
)) != NULL
)
2379 * Get and set a new active queue for service.
2381 static struct cfq_queue
*cfq_set_active_queue(struct cfq_data
*cfqd
,
2382 struct cfq_queue
*cfqq
)
2385 cfqq
= cfq_get_next_queue(cfqd
);
2387 __cfq_set_active_queue(cfqd
, cfqq
);
2391 static inline sector_t
cfq_dist_from_last(struct cfq_data
*cfqd
,
2394 if (blk_rq_pos(rq
) >= cfqd
->last_position
)
2395 return blk_rq_pos(rq
) - cfqd
->last_position
;
2397 return cfqd
->last_position
- blk_rq_pos(rq
);
2400 static inline int cfq_rq_close(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
2403 return cfq_dist_from_last(cfqd
, rq
) <= CFQQ_CLOSE_THR
;
2406 static struct cfq_queue
*cfqq_close(struct cfq_data
*cfqd
,
2407 struct cfq_queue
*cur_cfqq
)
2409 struct rb_root
*root
= &cfqd
->prio_trees
[cur_cfqq
->org_ioprio
];
2410 struct rb_node
*parent
, *node
;
2411 struct cfq_queue
*__cfqq
;
2412 sector_t sector
= cfqd
->last_position
;
2414 if (RB_EMPTY_ROOT(root
))
2418 * First, if we find a request starting at the end of the last
2419 * request, choose it.
2421 __cfqq
= cfq_prio_tree_lookup(cfqd
, root
, sector
, &parent
, NULL
);
2426 * If the exact sector wasn't found, the parent of the NULL leaf
2427 * will contain the closest sector.
2429 __cfqq
= rb_entry(parent
, struct cfq_queue
, p_node
);
2430 if (cfq_rq_close(cfqd
, cur_cfqq
, __cfqq
->next_rq
))
2433 if (blk_rq_pos(__cfqq
->next_rq
) < sector
)
2434 node
= rb_next(&__cfqq
->p_node
);
2436 node
= rb_prev(&__cfqq
->p_node
);
2440 __cfqq
= rb_entry(node
, struct cfq_queue
, p_node
);
2441 if (cfq_rq_close(cfqd
, cur_cfqq
, __cfqq
->next_rq
))
2449 * cur_cfqq - passed in so that we don't decide that the current queue is
2450 * closely cooperating with itself.
2452 * So, basically we're assuming that that cur_cfqq has dispatched at least
2453 * one request, and that cfqd->last_position reflects a position on the disk
2454 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
2457 static struct cfq_queue
*cfq_close_cooperator(struct cfq_data
*cfqd
,
2458 struct cfq_queue
*cur_cfqq
)
2460 struct cfq_queue
*cfqq
;
2462 if (cfq_class_idle(cur_cfqq
))
2464 if (!cfq_cfqq_sync(cur_cfqq
))
2466 if (CFQQ_SEEKY(cur_cfqq
))
2470 * Don't search priority tree if it's the only queue in the group.
2472 if (cur_cfqq
->cfqg
->nr_cfqq
== 1)
2476 * We should notice if some of the queues are cooperating, eg
2477 * working closely on the same area of the disk. In that case,
2478 * we can group them together and don't waste time idling.
2480 cfqq
= cfqq_close(cfqd
, cur_cfqq
);
2484 /* If new queue belongs to different cfq_group, don't choose it */
2485 if (cur_cfqq
->cfqg
!= cfqq
->cfqg
)
2489 * It only makes sense to merge sync queues.
2491 if (!cfq_cfqq_sync(cfqq
))
2493 if (CFQQ_SEEKY(cfqq
))
2497 * Do not merge queues of different priority classes
2499 if (cfq_class_rt(cfqq
) != cfq_class_rt(cur_cfqq
))
2506 * Determine whether we should enforce idle window for this queue.
2509 static bool cfq_should_idle(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
2511 enum wl_class_t wl_class
= cfqq_class(cfqq
);
2512 struct cfq_rb_root
*st
= cfqq
->service_tree
;
2517 if (!cfqd
->cfq_slice_idle
)
2520 /* We never do for idle class queues. */
2521 if (wl_class
== IDLE_WORKLOAD
)
2524 /* We do for queues that were marked with idle window flag. */
2525 if (cfq_cfqq_idle_window(cfqq
) &&
2526 !(blk_queue_nonrot(cfqd
->queue
) && cfqd
->hw_tag
))
2530 * Otherwise, we do only if they are the last ones
2531 * in their service tree.
2533 if (st
->count
== 1 && cfq_cfqq_sync(cfqq
) &&
2534 !cfq_io_thinktime_big(cfqd
, &st
->ttime
, false))
2536 cfq_log_cfqq(cfqd
, cfqq
, "Not idling. st->count:%d", st
->count
);
2540 static void cfq_arm_slice_timer(struct cfq_data
*cfqd
)
2542 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
2543 struct cfq_io_cq
*cic
;
2544 unsigned long sl
, group_idle
= 0;
2547 * SSD device without seek penalty, disable idling. But only do so
2548 * for devices that support queuing, otherwise we still have a problem
2549 * with sync vs async workloads.
2551 if (blk_queue_nonrot(cfqd
->queue
) && cfqd
->hw_tag
)
2554 WARN_ON(!RB_EMPTY_ROOT(&cfqq
->sort_list
));
2555 WARN_ON(cfq_cfqq_slice_new(cfqq
));
2558 * idle is disabled, either manually or by past process history
2560 if (!cfq_should_idle(cfqd
, cfqq
)) {
2561 /* no queue idling. Check for group idling */
2562 if (cfqd
->cfq_group_idle
)
2563 group_idle
= cfqd
->cfq_group_idle
;
2569 * still active requests from this queue, don't idle
2571 if (cfqq
->dispatched
)
2575 * task has exited, don't wait
2577 cic
= cfqd
->active_cic
;
2578 if (!cic
|| !atomic_read(&cic
->icq
.ioc
->active_ref
))
2582 * If our average think time is larger than the remaining time
2583 * slice, then don't idle. This avoids overrunning the allotted
2586 if (sample_valid(cic
->ttime
.ttime_samples
) &&
2587 (cfqq
->slice_end
- jiffies
< cic
->ttime
.ttime_mean
)) {
2588 cfq_log_cfqq(cfqd
, cfqq
, "Not idling. think_time:%lu",
2589 cic
->ttime
.ttime_mean
);
2593 /* There are other queues in the group, don't do group idle */
2594 if (group_idle
&& cfqq
->cfqg
->nr_cfqq
> 1)
2597 cfq_mark_cfqq_wait_request(cfqq
);
2600 sl
= cfqd
->cfq_group_idle
;
2602 sl
= cfqd
->cfq_slice_idle
;
2604 mod_timer(&cfqd
->idle_slice_timer
, jiffies
+ sl
);
2605 cfqg_stats_set_start_idle_time(cfqq
->cfqg
);
2606 cfq_log_cfqq(cfqd
, cfqq
, "arm_idle: %lu group_idle: %d", sl
,
2607 group_idle
? 1 : 0);
2611 * Move request from internal lists to the request queue dispatch list.
2613 static void cfq_dispatch_insert(struct request_queue
*q
, struct request
*rq
)
2615 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
2616 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
2618 cfq_log_cfqq(cfqd
, cfqq
, "dispatch_insert");
2620 cfqq
->next_rq
= cfq_find_next_rq(cfqd
, cfqq
, rq
);
2621 cfq_remove_request(rq
);
2623 (RQ_CFQG(rq
))->dispatched
++;
2624 elv_dispatch_sort(q
, rq
);
2626 cfqd
->rq_in_flight
[cfq_cfqq_sync(cfqq
)]++;
2627 cfqq
->nr_sectors
+= blk_rq_sectors(rq
);
2628 cfqg_stats_update_dispatch(cfqq
->cfqg
, blk_rq_bytes(rq
), rq
->cmd_flags
);
2632 * return expired entry, or NULL to just start from scratch in rbtree
2634 static struct request
*cfq_check_fifo(struct cfq_queue
*cfqq
)
2636 struct request
*rq
= NULL
;
2638 if (cfq_cfqq_fifo_expire(cfqq
))
2641 cfq_mark_cfqq_fifo_expire(cfqq
);
2643 if (list_empty(&cfqq
->fifo
))
2646 rq
= rq_entry_fifo(cfqq
->fifo
.next
);
2647 if (time_before(jiffies
, rq_fifo_time(rq
)))
2650 cfq_log_cfqq(cfqq
->cfqd
, cfqq
, "fifo=%p", rq
);
2655 cfq_prio_to_maxrq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
2657 const int base_rq
= cfqd
->cfq_slice_async_rq
;
2659 WARN_ON(cfqq
->ioprio
>= IOPRIO_BE_NR
);
2661 return 2 * base_rq
* (IOPRIO_BE_NR
- cfqq
->ioprio
);
2665 * Must be called with the queue_lock held.
2667 static int cfqq_process_refs(struct cfq_queue
*cfqq
)
2669 int process_refs
, io_refs
;
2671 io_refs
= cfqq
->allocated
[READ
] + cfqq
->allocated
[WRITE
];
2672 process_refs
= cfqq
->ref
- io_refs
;
2673 BUG_ON(process_refs
< 0);
2674 return process_refs
;
2677 static void cfq_setup_merge(struct cfq_queue
*cfqq
, struct cfq_queue
*new_cfqq
)
2679 int process_refs
, new_process_refs
;
2680 struct cfq_queue
*__cfqq
;
2683 * If there are no process references on the new_cfqq, then it is
2684 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
2685 * chain may have dropped their last reference (not just their
2686 * last process reference).
2688 if (!cfqq_process_refs(new_cfqq
))
2691 /* Avoid a circular list and skip interim queue merges */
2692 while ((__cfqq
= new_cfqq
->new_cfqq
)) {
2698 process_refs
= cfqq_process_refs(cfqq
);
2699 new_process_refs
= cfqq_process_refs(new_cfqq
);
2701 * If the process for the cfqq has gone away, there is no
2702 * sense in merging the queues.
2704 if (process_refs
== 0 || new_process_refs
== 0)
2708 * Merge in the direction of the lesser amount of work.
2710 if (new_process_refs
>= process_refs
) {
2711 cfqq
->new_cfqq
= new_cfqq
;
2712 new_cfqq
->ref
+= process_refs
;
2714 new_cfqq
->new_cfqq
= cfqq
;
2715 cfqq
->ref
+= new_process_refs
;
2719 static enum wl_type_t
cfq_choose_wl_type(struct cfq_data
*cfqd
,
2720 struct cfq_group
*cfqg
, enum wl_class_t wl_class
)
2722 struct cfq_queue
*queue
;
2724 bool key_valid
= false;
2725 unsigned long lowest_key
= 0;
2726 enum wl_type_t cur_best
= SYNC_NOIDLE_WORKLOAD
;
2728 for (i
= 0; i
<= SYNC_WORKLOAD
; ++i
) {
2729 /* select the one with lowest rb_key */
2730 queue
= cfq_rb_first(st_for(cfqg
, wl_class
, i
));
2732 (!key_valid
|| time_before(queue
->rb_key
, lowest_key
))) {
2733 lowest_key
= queue
->rb_key
;
2743 choose_wl_class_and_type(struct cfq_data
*cfqd
, struct cfq_group
*cfqg
)
2747 struct cfq_rb_root
*st
;
2748 unsigned group_slice
;
2749 enum wl_class_t original_class
= cfqd
->serving_wl_class
;
2751 /* Choose next priority. RT > BE > IDLE */
2752 if (cfq_group_busy_queues_wl(RT_WORKLOAD
, cfqd
, cfqg
))
2753 cfqd
->serving_wl_class
= RT_WORKLOAD
;
2754 else if (cfq_group_busy_queues_wl(BE_WORKLOAD
, cfqd
, cfqg
))
2755 cfqd
->serving_wl_class
= BE_WORKLOAD
;
2757 cfqd
->serving_wl_class
= IDLE_WORKLOAD
;
2758 cfqd
->workload_expires
= jiffies
+ 1;
2762 if (original_class
!= cfqd
->serving_wl_class
)
2766 * For RT and BE, we have to choose also the type
2767 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
2770 st
= st_for(cfqg
, cfqd
->serving_wl_class
, cfqd
->serving_wl_type
);
2774 * check workload expiration, and that we still have other queues ready
2776 if (count
&& !time_after(jiffies
, cfqd
->workload_expires
))
2780 /* otherwise select new workload type */
2781 cfqd
->serving_wl_type
= cfq_choose_wl_type(cfqd
, cfqg
,
2782 cfqd
->serving_wl_class
);
2783 st
= st_for(cfqg
, cfqd
->serving_wl_class
, cfqd
->serving_wl_type
);
2787 * the workload slice is computed as a fraction of target latency
2788 * proportional to the number of queues in that workload, over
2789 * all the queues in the same priority class
2791 group_slice
= cfq_group_slice(cfqd
, cfqg
);
2793 slice
= group_slice
* count
/
2794 max_t(unsigned, cfqg
->busy_queues_avg
[cfqd
->serving_wl_class
],
2795 cfq_group_busy_queues_wl(cfqd
->serving_wl_class
, cfqd
,
2798 if (cfqd
->serving_wl_type
== ASYNC_WORKLOAD
) {
2802 * Async queues are currently system wide. Just taking
2803 * proportion of queues with-in same group will lead to higher
2804 * async ratio system wide as generally root group is going
2805 * to have higher weight. A more accurate thing would be to
2806 * calculate system wide asnc/sync ratio.
2808 tmp
= cfqd
->cfq_target_latency
*
2809 cfqg_busy_async_queues(cfqd
, cfqg
);
2810 tmp
= tmp
/cfqd
->busy_queues
;
2811 slice
= min_t(unsigned, slice
, tmp
);
2813 /* async workload slice is scaled down according to
2814 * the sync/async slice ratio. */
2815 slice
= slice
* cfqd
->cfq_slice
[0] / cfqd
->cfq_slice
[1];
2817 /* sync workload slice is at least 2 * cfq_slice_idle */
2818 slice
= max(slice
, 2 * cfqd
->cfq_slice_idle
);
2820 slice
= max_t(unsigned, slice
, CFQ_MIN_TT
);
2821 cfq_log(cfqd
, "workload slice:%d", slice
);
2822 cfqd
->workload_expires
= jiffies
+ slice
;
2825 static struct cfq_group
*cfq_get_next_cfqg(struct cfq_data
*cfqd
)
2827 struct cfq_rb_root
*st
= &cfqd
->grp_service_tree
;
2828 struct cfq_group
*cfqg
;
2830 if (RB_EMPTY_ROOT(&st
->rb
))
2832 cfqg
= cfq_rb_first_group(st
);
2833 update_min_vdisktime(st
);
2837 static void cfq_choose_cfqg(struct cfq_data
*cfqd
)
2839 struct cfq_group
*cfqg
= cfq_get_next_cfqg(cfqd
);
2841 cfqd
->serving_group
= cfqg
;
2843 /* Restore the workload type data */
2844 if (cfqg
->saved_wl_slice
) {
2845 cfqd
->workload_expires
= jiffies
+ cfqg
->saved_wl_slice
;
2846 cfqd
->serving_wl_type
= cfqg
->saved_wl_type
;
2847 cfqd
->serving_wl_class
= cfqg
->saved_wl_class
;
2849 cfqd
->workload_expires
= jiffies
- 1;
2851 choose_wl_class_and_type(cfqd
, cfqg
);
2855 * Select a queue for service. If we have a current active queue,
2856 * check whether to continue servicing it, or retrieve and set a new one.
2858 static struct cfq_queue
*cfq_select_queue(struct cfq_data
*cfqd
)
2860 struct cfq_queue
*cfqq
, *new_cfqq
= NULL
;
2862 cfqq
= cfqd
->active_queue
;
2866 if (!cfqd
->rq_queued
)
2870 * We were waiting for group to get backlogged. Expire the queue
2872 if (cfq_cfqq_wait_busy(cfqq
) && !RB_EMPTY_ROOT(&cfqq
->sort_list
))
2876 * The active queue has run out of time, expire it and select new.
2878 if (cfq_slice_used(cfqq
) && !cfq_cfqq_must_dispatch(cfqq
)) {
2880 * If slice had not expired at the completion of last request
2881 * we might not have turned on wait_busy flag. Don't expire
2882 * the queue yet. Allow the group to get backlogged.
2884 * The very fact that we have used the slice, that means we
2885 * have been idling all along on this queue and it should be
2886 * ok to wait for this request to complete.
2888 if (cfqq
->cfqg
->nr_cfqq
== 1 && RB_EMPTY_ROOT(&cfqq
->sort_list
)
2889 && cfqq
->dispatched
&& cfq_should_idle(cfqd
, cfqq
)) {
2893 goto check_group_idle
;
2897 * The active queue has requests and isn't expired, allow it to
2900 if (!RB_EMPTY_ROOT(&cfqq
->sort_list
))
2904 * If another queue has a request waiting within our mean seek
2905 * distance, let it run. The expire code will check for close
2906 * cooperators and put the close queue at the front of the service
2907 * tree. If possible, merge the expiring queue with the new cfqq.
2909 new_cfqq
= cfq_close_cooperator(cfqd
, cfqq
);
2911 if (!cfqq
->new_cfqq
)
2912 cfq_setup_merge(cfqq
, new_cfqq
);
2917 * No requests pending. If the active queue still has requests in
2918 * flight or is idling for a new request, allow either of these
2919 * conditions to happen (or time out) before selecting a new queue.
2921 if (timer_pending(&cfqd
->idle_slice_timer
)) {
2927 * This is a deep seek queue, but the device is much faster than
2928 * the queue can deliver, don't idle
2930 if (CFQQ_SEEKY(cfqq
) && cfq_cfqq_idle_window(cfqq
) &&
2931 (cfq_cfqq_slice_new(cfqq
) ||
2932 (cfqq
->slice_end
- jiffies
> jiffies
- cfqq
->slice_start
))) {
2933 cfq_clear_cfqq_deep(cfqq
);
2934 cfq_clear_cfqq_idle_window(cfqq
);
2937 if (cfqq
->dispatched
&& cfq_should_idle(cfqd
, cfqq
)) {
2943 * If group idle is enabled and there are requests dispatched from
2944 * this group, wait for requests to complete.
2947 if (cfqd
->cfq_group_idle
&& cfqq
->cfqg
->nr_cfqq
== 1 &&
2948 cfqq
->cfqg
->dispatched
&&
2949 !cfq_io_thinktime_big(cfqd
, &cfqq
->cfqg
->ttime
, true)) {
2955 cfq_slice_expired(cfqd
, 0);
2958 * Current queue expired. Check if we have to switch to a new
2962 cfq_choose_cfqg(cfqd
);
2964 cfqq
= cfq_set_active_queue(cfqd
, new_cfqq
);
2969 static int __cfq_forced_dispatch_cfqq(struct cfq_queue
*cfqq
)
2973 while (cfqq
->next_rq
) {
2974 cfq_dispatch_insert(cfqq
->cfqd
->queue
, cfqq
->next_rq
);
2978 BUG_ON(!list_empty(&cfqq
->fifo
));
2980 /* By default cfqq is not expired if it is empty. Do it explicitly */
2981 __cfq_slice_expired(cfqq
->cfqd
, cfqq
, 0);
2986 * Drain our current requests. Used for barriers and when switching
2987 * io schedulers on-the-fly.
2989 static int cfq_forced_dispatch(struct cfq_data
*cfqd
)
2991 struct cfq_queue
*cfqq
;
2994 /* Expire the timeslice of the current active queue first */
2995 cfq_slice_expired(cfqd
, 0);
2996 while ((cfqq
= cfq_get_next_queue_forced(cfqd
)) != NULL
) {
2997 __cfq_set_active_queue(cfqd
, cfqq
);
2998 dispatched
+= __cfq_forced_dispatch_cfqq(cfqq
);
3001 BUG_ON(cfqd
->busy_queues
);
3003 cfq_log(cfqd
, "forced_dispatch=%d", dispatched
);
3007 static inline bool cfq_slice_used_soon(struct cfq_data
*cfqd
,
3008 struct cfq_queue
*cfqq
)
3010 /* the queue hasn't finished any request, can't estimate */
3011 if (cfq_cfqq_slice_new(cfqq
))
3013 if (time_after(jiffies
+ cfqd
->cfq_slice_idle
* cfqq
->dispatched
,
3020 static bool cfq_may_dispatch(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
3022 unsigned int max_dispatch
;
3025 * Drain async requests before we start sync IO
3027 if (cfq_should_idle(cfqd
, cfqq
) && cfqd
->rq_in_flight
[BLK_RW_ASYNC
])
3031 * If this is an async queue and we have sync IO in flight, let it wait
3033 if (cfqd
->rq_in_flight
[BLK_RW_SYNC
] && !cfq_cfqq_sync(cfqq
))
3036 max_dispatch
= max_t(unsigned int, cfqd
->cfq_quantum
/ 2, 1);
3037 if (cfq_class_idle(cfqq
))
3041 * Does this cfqq already have too much IO in flight?
3043 if (cfqq
->dispatched
>= max_dispatch
) {
3044 bool promote_sync
= false;
3046 * idle queue must always only have a single IO in flight
3048 if (cfq_class_idle(cfqq
))
3052 * If there is only one sync queue
3053 * we can ignore async queue here and give the sync
3054 * queue no dispatch limit. The reason is a sync queue can
3055 * preempt async queue, limiting the sync queue doesn't make
3056 * sense. This is useful for aiostress test.
3058 if (cfq_cfqq_sync(cfqq
) && cfqd
->busy_sync_queues
== 1)
3059 promote_sync
= true;
3062 * We have other queues, don't allow more IO from this one
3064 if (cfqd
->busy_queues
> 1 && cfq_slice_used_soon(cfqd
, cfqq
) &&
3069 * Sole queue user, no limit
3071 if (cfqd
->busy_queues
== 1 || promote_sync
)
3075 * Normally we start throttling cfqq when cfq_quantum/2
3076 * requests have been dispatched. But we can drive
3077 * deeper queue depths at the beginning of slice
3078 * subjected to upper limit of cfq_quantum.
3080 max_dispatch
= cfqd
->cfq_quantum
;
3084 * Async queues must wait a bit before being allowed dispatch.
3085 * We also ramp up the dispatch depth gradually for async IO,
3086 * based on the last sync IO we serviced
3088 if (!cfq_cfqq_sync(cfqq
) && cfqd
->cfq_latency
) {
3089 unsigned long last_sync
= jiffies
- cfqd
->last_delayed_sync
;
3092 depth
= last_sync
/ cfqd
->cfq_slice
[1];
3093 if (!depth
&& !cfqq
->dispatched
)
3095 if (depth
< max_dispatch
)
3096 max_dispatch
= depth
;
3100 * If we're below the current max, allow a dispatch
3102 return cfqq
->dispatched
< max_dispatch
;
3106 * Dispatch a request from cfqq, moving them to the request queue
3109 static bool cfq_dispatch_request(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
3113 BUG_ON(RB_EMPTY_ROOT(&cfqq
->sort_list
));
3115 if (!cfq_may_dispatch(cfqd
, cfqq
))
3119 * follow expired path, else get first next available
3121 rq
= cfq_check_fifo(cfqq
);
3126 * insert request into driver dispatch list
3128 cfq_dispatch_insert(cfqd
->queue
, rq
);
3130 if (!cfqd
->active_cic
) {
3131 struct cfq_io_cq
*cic
= RQ_CIC(rq
);
3133 atomic_long_inc(&cic
->icq
.ioc
->refcount
);
3134 cfqd
->active_cic
= cic
;
3141 * Find the cfqq that we need to service and move a request from that to the
3144 static int cfq_dispatch_requests(struct request_queue
*q
, int force
)
3146 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
3147 struct cfq_queue
*cfqq
;
3149 if (!cfqd
->busy_queues
)
3152 if (unlikely(force
))
3153 return cfq_forced_dispatch(cfqd
);
3155 cfqq
= cfq_select_queue(cfqd
);
3160 * Dispatch a request from this cfqq, if it is allowed
3162 if (!cfq_dispatch_request(cfqd
, cfqq
))
3165 cfqq
->slice_dispatch
++;
3166 cfq_clear_cfqq_must_dispatch(cfqq
);
3169 * expire an async queue immediately if it has used up its slice. idle
3170 * queue always expire after 1 dispatch round.
3172 if (cfqd
->busy_queues
> 1 && ((!cfq_cfqq_sync(cfqq
) &&
3173 cfqq
->slice_dispatch
>= cfq_prio_to_maxrq(cfqd
, cfqq
)) ||
3174 cfq_class_idle(cfqq
))) {
3175 cfqq
->slice_end
= jiffies
+ 1;
3176 cfq_slice_expired(cfqd
, 0);
3179 cfq_log_cfqq(cfqd
, cfqq
, "dispatched a request");
3184 * task holds one reference to the queue, dropped when task exits. each rq
3185 * in-flight on this queue also holds a reference, dropped when rq is freed.
3187 * Each cfq queue took a reference on the parent group. Drop it now.
3188 * queue lock must be held here.
3190 static void cfq_put_queue(struct cfq_queue
*cfqq
)
3192 struct cfq_data
*cfqd
= cfqq
->cfqd
;
3193 struct cfq_group
*cfqg
;
3195 BUG_ON(cfqq
->ref
<= 0);
3201 cfq_log_cfqq(cfqd
, cfqq
, "put_queue");
3202 BUG_ON(rb_first(&cfqq
->sort_list
));
3203 BUG_ON(cfqq
->allocated
[READ
] + cfqq
->allocated
[WRITE
]);
3206 if (unlikely(cfqd
->active_queue
== cfqq
)) {
3207 __cfq_slice_expired(cfqd
, cfqq
, 0);
3208 cfq_schedule_dispatch(cfqd
);
3211 BUG_ON(cfq_cfqq_on_rr(cfqq
));
3212 kmem_cache_free(cfq_pool
, cfqq
);
3216 static void cfq_put_cooperator(struct cfq_queue
*cfqq
)
3218 struct cfq_queue
*__cfqq
, *next
;
3221 * If this queue was scheduled to merge with another queue, be
3222 * sure to drop the reference taken on that queue (and others in
3223 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
3225 __cfqq
= cfqq
->new_cfqq
;
3227 if (__cfqq
== cfqq
) {
3228 WARN(1, "cfqq->new_cfqq loop detected\n");
3231 next
= __cfqq
->new_cfqq
;
3232 cfq_put_queue(__cfqq
);
3237 static void cfq_exit_cfqq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
3239 if (unlikely(cfqq
== cfqd
->active_queue
)) {
3240 __cfq_slice_expired(cfqd
, cfqq
, 0);
3241 cfq_schedule_dispatch(cfqd
);
3244 cfq_put_cooperator(cfqq
);
3246 cfq_put_queue(cfqq
);
3249 static void cfq_init_icq(struct io_cq
*icq
)
3251 struct cfq_io_cq
*cic
= icq_to_cic(icq
);
3253 cic
->ttime
.last_end_request
= jiffies
;
3256 static void cfq_exit_icq(struct io_cq
*icq
)
3258 struct cfq_io_cq
*cic
= icq_to_cic(icq
);
3259 struct cfq_data
*cfqd
= cic_to_cfqd(cic
);
3261 if (cic
->cfqq
[BLK_RW_ASYNC
]) {
3262 cfq_exit_cfqq(cfqd
, cic
->cfqq
[BLK_RW_ASYNC
]);
3263 cic
->cfqq
[BLK_RW_ASYNC
] = NULL
;
3266 if (cic
->cfqq
[BLK_RW_SYNC
]) {
3267 cfq_exit_cfqq(cfqd
, cic
->cfqq
[BLK_RW_SYNC
]);
3268 cic
->cfqq
[BLK_RW_SYNC
] = NULL
;
3272 static void cfq_init_prio_data(struct cfq_queue
*cfqq
, struct cfq_io_cq
*cic
)
3274 struct task_struct
*tsk
= current
;
3277 if (!cfq_cfqq_prio_changed(cfqq
))
3280 ioprio_class
= IOPRIO_PRIO_CLASS(cic
->ioprio
);
3281 switch (ioprio_class
) {
3283 printk(KERN_ERR
"cfq: bad prio %x\n", ioprio_class
);
3284 case IOPRIO_CLASS_NONE
:
3286 * no prio set, inherit CPU scheduling settings
3288 cfqq
->ioprio
= task_nice_ioprio(tsk
);
3289 cfqq
->ioprio_class
= task_nice_ioclass(tsk
);
3291 case IOPRIO_CLASS_RT
:
3292 cfqq
->ioprio
= IOPRIO_PRIO_DATA(cic
->ioprio
);
3293 cfqq
->ioprio_class
= IOPRIO_CLASS_RT
;
3295 case IOPRIO_CLASS_BE
:
3296 cfqq
->ioprio
= IOPRIO_PRIO_DATA(cic
->ioprio
);
3297 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
3299 case IOPRIO_CLASS_IDLE
:
3300 cfqq
->ioprio_class
= IOPRIO_CLASS_IDLE
;
3302 cfq_clear_cfqq_idle_window(cfqq
);
3307 * keep track of original prio settings in case we have to temporarily
3308 * elevate the priority of this queue
3310 cfqq
->org_ioprio
= cfqq
->ioprio
;
3311 cfq_clear_cfqq_prio_changed(cfqq
);
3314 static void check_ioprio_changed(struct cfq_io_cq
*cic
, struct bio
*bio
)
3316 int ioprio
= cic
->icq
.ioc
->ioprio
;
3317 struct cfq_data
*cfqd
= cic_to_cfqd(cic
);
3318 struct cfq_queue
*cfqq
;
3321 * Check whether ioprio has changed. The condition may trigger
3322 * spuriously on a newly created cic but there's no harm.
3324 if (unlikely(!cfqd
) || likely(cic
->ioprio
== ioprio
))
3327 cfqq
= cic
->cfqq
[BLK_RW_ASYNC
];
3329 struct cfq_queue
*new_cfqq
;
3330 new_cfqq
= cfq_get_queue(cfqd
, BLK_RW_ASYNC
, cic
, bio
,
3333 cic
->cfqq
[BLK_RW_ASYNC
] = new_cfqq
;
3334 cfq_put_queue(cfqq
);
3338 cfqq
= cic
->cfqq
[BLK_RW_SYNC
];
3340 cfq_mark_cfqq_prio_changed(cfqq
);
3342 cic
->ioprio
= ioprio
;
3345 static void cfq_init_cfqq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
3346 pid_t pid
, bool is_sync
)
3348 RB_CLEAR_NODE(&cfqq
->rb_node
);
3349 RB_CLEAR_NODE(&cfqq
->p_node
);
3350 INIT_LIST_HEAD(&cfqq
->fifo
);
3355 cfq_mark_cfqq_prio_changed(cfqq
);
3358 if (!cfq_class_idle(cfqq
))
3359 cfq_mark_cfqq_idle_window(cfqq
);
3360 cfq_mark_cfqq_sync(cfqq
);
3365 #ifdef CONFIG_CFQ_GROUP_IOSCHED
3366 static void check_blkcg_changed(struct cfq_io_cq
*cic
, struct bio
*bio
)
3368 struct cfq_data
*cfqd
= cic_to_cfqd(cic
);
3369 struct cfq_queue
*sync_cfqq
;
3373 id
= bio_blkcg(bio
)->id
;
3377 * Check whether blkcg has changed. The condition may trigger
3378 * spuriously on a newly created cic but there's no harm.
3380 if (unlikely(!cfqd
) || likely(cic
->blkcg_id
== id
))
3383 sync_cfqq
= cic_to_cfqq(cic
, 1);
3386 * Drop reference to sync queue. A new sync queue will be
3387 * assigned in new group upon arrival of a fresh request.
3389 cfq_log_cfqq(cfqd
, sync_cfqq
, "changed cgroup");
3390 cic_set_cfqq(cic
, NULL
, 1);
3391 cfq_put_queue(sync_cfqq
);
3397 static inline void check_blkcg_changed(struct cfq_io_cq
*cic
, struct bio
*bio
) { }
3398 #endif /* CONFIG_CFQ_GROUP_IOSCHED */
3400 static struct cfq_queue
*
3401 cfq_find_alloc_queue(struct cfq_data
*cfqd
, bool is_sync
, struct cfq_io_cq
*cic
,
3402 struct bio
*bio
, gfp_t gfp_mask
)
3404 struct blkcg
*blkcg
;
3405 struct cfq_queue
*cfqq
, *new_cfqq
= NULL
;
3406 struct cfq_group
*cfqg
;
3411 blkcg
= bio_blkcg(bio
);
3412 cfqg
= cfq_lookup_create_cfqg(cfqd
, blkcg
);
3413 cfqq
= cic_to_cfqq(cic
, is_sync
);
3416 * Always try a new alloc if we fell back to the OOM cfqq
3417 * originally, since it should just be a temporary situation.
3419 if (!cfqq
|| cfqq
== &cfqd
->oom_cfqq
) {
3424 } else if (gfp_mask
& __GFP_WAIT
) {
3426 spin_unlock_irq(cfqd
->queue
->queue_lock
);
3427 new_cfqq
= kmem_cache_alloc_node(cfq_pool
,
3428 gfp_mask
| __GFP_ZERO
,
3430 spin_lock_irq(cfqd
->queue
->queue_lock
);
3434 cfqq
= kmem_cache_alloc_node(cfq_pool
,
3435 gfp_mask
| __GFP_ZERO
,
3440 cfq_init_cfqq(cfqd
, cfqq
, current
->pid
, is_sync
);
3441 cfq_init_prio_data(cfqq
, cic
);
3442 cfq_link_cfqq_cfqg(cfqq
, cfqg
);
3443 cfq_log_cfqq(cfqd
, cfqq
, "alloced");
3445 cfqq
= &cfqd
->oom_cfqq
;
3449 kmem_cache_free(cfq_pool
, new_cfqq
);
3455 static struct cfq_queue
**
3456 cfq_async_queue_prio(struct cfq_data
*cfqd
, int ioprio_class
, int ioprio
)
3458 switch (ioprio_class
) {
3459 case IOPRIO_CLASS_RT
:
3460 return &cfqd
->async_cfqq
[0][ioprio
];
3461 case IOPRIO_CLASS_NONE
:
3462 ioprio
= IOPRIO_NORM
;
3464 case IOPRIO_CLASS_BE
:
3465 return &cfqd
->async_cfqq
[1][ioprio
];
3466 case IOPRIO_CLASS_IDLE
:
3467 return &cfqd
->async_idle_cfqq
;
3473 static struct cfq_queue
*
3474 cfq_get_queue(struct cfq_data
*cfqd
, bool is_sync
, struct cfq_io_cq
*cic
,
3475 struct bio
*bio
, gfp_t gfp_mask
)
3477 const int ioprio_class
= IOPRIO_PRIO_CLASS(cic
->ioprio
);
3478 const int ioprio
= IOPRIO_PRIO_DATA(cic
->ioprio
);
3479 struct cfq_queue
**async_cfqq
= NULL
;
3480 struct cfq_queue
*cfqq
= NULL
;
3483 async_cfqq
= cfq_async_queue_prio(cfqd
, ioprio_class
, ioprio
);
3488 cfqq
= cfq_find_alloc_queue(cfqd
, is_sync
, cic
, bio
, gfp_mask
);
3491 * pin the queue now that it's allocated, scheduler exit will prune it
3493 if (!is_sync
&& !(*async_cfqq
)) {
3503 __cfq_update_io_thinktime(struct cfq_ttime
*ttime
, unsigned long slice_idle
)
3505 unsigned long elapsed
= jiffies
- ttime
->last_end_request
;
3506 elapsed
= min(elapsed
, 2UL * slice_idle
);
3508 ttime
->ttime_samples
= (7*ttime
->ttime_samples
+ 256) / 8;
3509 ttime
->ttime_total
= (7*ttime
->ttime_total
+ 256*elapsed
) / 8;
3510 ttime
->ttime_mean
= (ttime
->ttime_total
+ 128) / ttime
->ttime_samples
;
3514 cfq_update_io_thinktime(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
3515 struct cfq_io_cq
*cic
)
3517 if (cfq_cfqq_sync(cfqq
)) {
3518 __cfq_update_io_thinktime(&cic
->ttime
, cfqd
->cfq_slice_idle
);
3519 __cfq_update_io_thinktime(&cfqq
->service_tree
->ttime
,
3520 cfqd
->cfq_slice_idle
);
3522 #ifdef CONFIG_CFQ_GROUP_IOSCHED
3523 __cfq_update_io_thinktime(&cfqq
->cfqg
->ttime
, cfqd
->cfq_group_idle
);
3528 cfq_update_io_seektime(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
3532 sector_t n_sec
= blk_rq_sectors(rq
);
3533 if (cfqq
->last_request_pos
) {
3534 if (cfqq
->last_request_pos
< blk_rq_pos(rq
))
3535 sdist
= blk_rq_pos(rq
) - cfqq
->last_request_pos
;
3537 sdist
= cfqq
->last_request_pos
- blk_rq_pos(rq
);
3540 cfqq
->seek_history
<<= 1;
3541 if (blk_queue_nonrot(cfqd
->queue
))
3542 cfqq
->seek_history
|= (n_sec
< CFQQ_SECT_THR_NONROT
);
3544 cfqq
->seek_history
|= (sdist
> CFQQ_SEEK_THR
);
3548 * Disable idle window if the process thinks too long or seeks so much that
3552 cfq_update_idle_window(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
3553 struct cfq_io_cq
*cic
)
3555 int old_idle
, enable_idle
;
3558 * Don't idle for async or idle io prio class
3560 if (!cfq_cfqq_sync(cfqq
) || cfq_class_idle(cfqq
))
3563 enable_idle
= old_idle
= cfq_cfqq_idle_window(cfqq
);
3565 if (cfqq
->queued
[0] + cfqq
->queued
[1] >= 4)
3566 cfq_mark_cfqq_deep(cfqq
);
3568 if (cfqq
->next_rq
&& (cfqq
->next_rq
->cmd_flags
& REQ_NOIDLE
))
3570 else if (!atomic_read(&cic
->icq
.ioc
->active_ref
) ||
3571 !cfqd
->cfq_slice_idle
||
3572 (!cfq_cfqq_deep(cfqq
) && CFQQ_SEEKY(cfqq
)))
3574 else if (sample_valid(cic
->ttime
.ttime_samples
)) {
3575 if (cic
->ttime
.ttime_mean
> cfqd
->cfq_slice_idle
)
3581 if (old_idle
!= enable_idle
) {
3582 cfq_log_cfqq(cfqd
, cfqq
, "idle=%d", enable_idle
);
3584 cfq_mark_cfqq_idle_window(cfqq
);
3586 cfq_clear_cfqq_idle_window(cfqq
);
3591 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3592 * no or if we aren't sure, a 1 will cause a preempt.
3595 cfq_should_preempt(struct cfq_data
*cfqd
, struct cfq_queue
*new_cfqq
,
3598 struct cfq_queue
*cfqq
;
3600 cfqq
= cfqd
->active_queue
;
3604 if (cfq_class_idle(new_cfqq
))
3607 if (cfq_class_idle(cfqq
))
3611 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3613 if (cfq_class_rt(cfqq
) && !cfq_class_rt(new_cfqq
))
3617 * if the new request is sync, but the currently running queue is
3618 * not, let the sync request have priority.
3620 if (rq_is_sync(rq
) && !cfq_cfqq_sync(cfqq
))
3623 if (new_cfqq
->cfqg
!= cfqq
->cfqg
)
3626 if (cfq_slice_used(cfqq
))
3629 /* Allow preemption only if we are idling on sync-noidle tree */
3630 if (cfqd
->serving_wl_type
== SYNC_NOIDLE_WORKLOAD
&&
3631 cfqq_type(new_cfqq
) == SYNC_NOIDLE_WORKLOAD
&&
3632 new_cfqq
->service_tree
->count
== 2 &&
3633 RB_EMPTY_ROOT(&cfqq
->sort_list
))
3637 * So both queues are sync. Let the new request get disk time if
3638 * it's a metadata request and the current queue is doing regular IO.
3640 if ((rq
->cmd_flags
& REQ_PRIO
) && !cfqq
->prio_pending
)
3644 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
3646 if (cfq_class_rt(new_cfqq
) && !cfq_class_rt(cfqq
))
3649 /* An idle queue should not be idle now for some reason */
3650 if (RB_EMPTY_ROOT(&cfqq
->sort_list
) && !cfq_should_idle(cfqd
, cfqq
))
3653 if (!cfqd
->active_cic
|| !cfq_cfqq_wait_request(cfqq
))
3657 * if this request is as-good as one we would expect from the
3658 * current cfqq, let it preempt
3660 if (cfq_rq_close(cfqd
, cfqq
, rq
))
3667 * cfqq preempts the active queue. if we allowed preempt with no slice left,
3668 * let it have half of its nominal slice.
3670 static void cfq_preempt_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
3672 enum wl_type_t old_type
= cfqq_type(cfqd
->active_queue
);
3674 cfq_log_cfqq(cfqd
, cfqq
, "preempt");
3675 cfq_slice_expired(cfqd
, 1);
3678 * workload type is changed, don't save slice, otherwise preempt
3681 if (old_type
!= cfqq_type(cfqq
))
3682 cfqq
->cfqg
->saved_wl_slice
= 0;
3685 * Put the new queue at the front of the of the current list,
3686 * so we know that it will be selected next.
3688 BUG_ON(!cfq_cfqq_on_rr(cfqq
));
3690 cfq_service_tree_add(cfqd
, cfqq
, 1);
3692 cfqq
->slice_end
= 0;
3693 cfq_mark_cfqq_slice_new(cfqq
);
3697 * Called when a new fs request (rq) is added (to cfqq). Check if there's
3698 * something we should do about it
3701 cfq_rq_enqueued(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
3704 struct cfq_io_cq
*cic
= RQ_CIC(rq
);
3707 if (rq
->cmd_flags
& REQ_PRIO
)
3708 cfqq
->prio_pending
++;
3710 cfq_update_io_thinktime(cfqd
, cfqq
, cic
);
3711 cfq_update_io_seektime(cfqd
, cfqq
, rq
);
3712 cfq_update_idle_window(cfqd
, cfqq
, cic
);
3714 cfqq
->last_request_pos
= blk_rq_pos(rq
) + blk_rq_sectors(rq
);
3716 if (cfqq
== cfqd
->active_queue
) {
3718 * Remember that we saw a request from this process, but
3719 * don't start queuing just yet. Otherwise we risk seeing lots
3720 * of tiny requests, because we disrupt the normal plugging
3721 * and merging. If the request is already larger than a single
3722 * page, let it rip immediately. For that case we assume that
3723 * merging is already done. Ditto for a busy system that
3724 * has other work pending, don't risk delaying until the
3725 * idle timer unplug to continue working.
3727 if (cfq_cfqq_wait_request(cfqq
)) {
3728 if (blk_rq_bytes(rq
) > PAGE_CACHE_SIZE
||
3729 cfqd
->busy_queues
> 1) {
3730 cfq_del_timer(cfqd
, cfqq
);
3731 cfq_clear_cfqq_wait_request(cfqq
);
3732 __blk_run_queue(cfqd
->queue
);
3734 cfqg_stats_update_idle_time(cfqq
->cfqg
);
3735 cfq_mark_cfqq_must_dispatch(cfqq
);
3738 } else if (cfq_should_preempt(cfqd
, cfqq
, rq
)) {
3740 * not the active queue - expire current slice if it is
3741 * idle and has expired it's mean thinktime or this new queue
3742 * has some old slice time left and is of higher priority or
3743 * this new queue is RT and the current one is BE
3745 cfq_preempt_queue(cfqd
, cfqq
);
3746 __blk_run_queue(cfqd
->queue
);
3750 static void cfq_insert_request(struct request_queue
*q
, struct request
*rq
)
3752 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
3753 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
3755 cfq_log_cfqq(cfqd
, cfqq
, "insert_request");
3756 cfq_init_prio_data(cfqq
, RQ_CIC(rq
));
3758 rq_set_fifo_time(rq
, jiffies
+ cfqd
->cfq_fifo_expire
[rq_is_sync(rq
)]);
3759 list_add_tail(&rq
->queuelist
, &cfqq
->fifo
);
3761 cfqg_stats_update_io_add(RQ_CFQG(rq
), cfqd
->serving_group
,
3763 cfq_rq_enqueued(cfqd
, cfqq
, rq
);
3767 * Update hw_tag based on peak queue depth over 50 samples under
3770 static void cfq_update_hw_tag(struct cfq_data
*cfqd
)
3772 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
3774 if (cfqd
->rq_in_driver
> cfqd
->hw_tag_est_depth
)
3775 cfqd
->hw_tag_est_depth
= cfqd
->rq_in_driver
;
3777 if (cfqd
->hw_tag
== 1)
3780 if (cfqd
->rq_queued
<= CFQ_HW_QUEUE_MIN
&&
3781 cfqd
->rq_in_driver
<= CFQ_HW_QUEUE_MIN
)
3785 * If active queue hasn't enough requests and can idle, cfq might not
3786 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3789 if (cfqq
&& cfq_cfqq_idle_window(cfqq
) &&
3790 cfqq
->dispatched
+ cfqq
->queued
[0] + cfqq
->queued
[1] <
3791 CFQ_HW_QUEUE_MIN
&& cfqd
->rq_in_driver
< CFQ_HW_QUEUE_MIN
)
3794 if (cfqd
->hw_tag_samples
++ < 50)
3797 if (cfqd
->hw_tag_est_depth
>= CFQ_HW_QUEUE_MIN
)
3803 static bool cfq_should_wait_busy(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
3805 struct cfq_io_cq
*cic
= cfqd
->active_cic
;
3807 /* If the queue already has requests, don't wait */
3808 if (!RB_EMPTY_ROOT(&cfqq
->sort_list
))
3811 /* If there are other queues in the group, don't wait */
3812 if (cfqq
->cfqg
->nr_cfqq
> 1)
3815 /* the only queue in the group, but think time is big */
3816 if (cfq_io_thinktime_big(cfqd
, &cfqq
->cfqg
->ttime
, true))
3819 if (cfq_slice_used(cfqq
))
3822 /* if slice left is less than think time, wait busy */
3823 if (cic
&& sample_valid(cic
->ttime
.ttime_samples
)
3824 && (cfqq
->slice_end
- jiffies
< cic
->ttime
.ttime_mean
))
3828 * If think times is less than a jiffy than ttime_mean=0 and above
3829 * will not be true. It might happen that slice has not expired yet
3830 * but will expire soon (4-5 ns) during select_queue(). To cover the
3831 * case where think time is less than a jiffy, mark the queue wait
3832 * busy if only 1 jiffy is left in the slice.
3834 if (cfqq
->slice_end
- jiffies
== 1)
3840 static void cfq_completed_request(struct request_queue
*q
, struct request
*rq
)
3842 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
3843 struct cfq_data
*cfqd
= cfqq
->cfqd
;
3844 const int sync
= rq_is_sync(rq
);
3848 cfq_log_cfqq(cfqd
, cfqq
, "complete rqnoidle %d",
3849 !!(rq
->cmd_flags
& REQ_NOIDLE
));
3851 cfq_update_hw_tag(cfqd
);
3853 WARN_ON(!cfqd
->rq_in_driver
);
3854 WARN_ON(!cfqq
->dispatched
);
3855 cfqd
->rq_in_driver
--;
3857 (RQ_CFQG(rq
))->dispatched
--;
3858 cfqg_stats_update_completion(cfqq
->cfqg
, rq_start_time_ns(rq
),
3859 rq_io_start_time_ns(rq
), rq
->cmd_flags
);
3861 cfqd
->rq_in_flight
[cfq_cfqq_sync(cfqq
)]--;
3864 struct cfq_rb_root
*st
;
3866 RQ_CIC(rq
)->ttime
.last_end_request
= now
;
3868 if (cfq_cfqq_on_rr(cfqq
))
3869 st
= cfqq
->service_tree
;
3871 st
= st_for(cfqq
->cfqg
, cfqq_class(cfqq
),
3874 st
->ttime
.last_end_request
= now
;
3875 if (!time_after(rq
->start_time
+ cfqd
->cfq_fifo_expire
[1], now
))
3876 cfqd
->last_delayed_sync
= now
;
3879 #ifdef CONFIG_CFQ_GROUP_IOSCHED
3880 cfqq
->cfqg
->ttime
.last_end_request
= now
;
3884 * If this is the active queue, check if it needs to be expired,
3885 * or if we want to idle in case it has no pending requests.
3887 if (cfqd
->active_queue
== cfqq
) {
3888 const bool cfqq_empty
= RB_EMPTY_ROOT(&cfqq
->sort_list
);
3890 if (cfq_cfqq_slice_new(cfqq
)) {
3891 cfq_set_prio_slice(cfqd
, cfqq
);
3892 cfq_clear_cfqq_slice_new(cfqq
);
3896 * Should we wait for next request to come in before we expire
3899 if (cfq_should_wait_busy(cfqd
, cfqq
)) {
3900 unsigned long extend_sl
= cfqd
->cfq_slice_idle
;
3901 if (!cfqd
->cfq_slice_idle
)
3902 extend_sl
= cfqd
->cfq_group_idle
;
3903 cfqq
->slice_end
= jiffies
+ extend_sl
;
3904 cfq_mark_cfqq_wait_busy(cfqq
);
3905 cfq_log_cfqq(cfqd
, cfqq
, "will busy wait");
3909 * Idling is not enabled on:
3911 * - idle-priority queues
3913 * - queues with still some requests queued
3914 * - when there is a close cooperator
3916 if (cfq_slice_used(cfqq
) || cfq_class_idle(cfqq
))
3917 cfq_slice_expired(cfqd
, 1);
3918 else if (sync
&& cfqq_empty
&&
3919 !cfq_close_cooperator(cfqd
, cfqq
)) {
3920 cfq_arm_slice_timer(cfqd
);
3924 if (!cfqd
->rq_in_driver
)
3925 cfq_schedule_dispatch(cfqd
);
3928 static inline int __cfq_may_queue(struct cfq_queue
*cfqq
)
3930 if (cfq_cfqq_wait_request(cfqq
) && !cfq_cfqq_must_alloc_slice(cfqq
)) {
3931 cfq_mark_cfqq_must_alloc_slice(cfqq
);
3932 return ELV_MQUEUE_MUST
;
3935 return ELV_MQUEUE_MAY
;
3938 static int cfq_may_queue(struct request_queue
*q
, int rw
)
3940 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
3941 struct task_struct
*tsk
= current
;
3942 struct cfq_io_cq
*cic
;
3943 struct cfq_queue
*cfqq
;
3946 * don't force setup of a queue from here, as a call to may_queue
3947 * does not necessarily imply that a request actually will be queued.
3948 * so just lookup a possibly existing queue, or return 'may queue'
3951 cic
= cfq_cic_lookup(cfqd
, tsk
->io_context
);
3953 return ELV_MQUEUE_MAY
;
3955 cfqq
= cic_to_cfqq(cic
, rw_is_sync(rw
));
3957 cfq_init_prio_data(cfqq
, cic
);
3959 return __cfq_may_queue(cfqq
);
3962 return ELV_MQUEUE_MAY
;
3966 * queue lock held here
3968 static void cfq_put_request(struct request
*rq
)
3970 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
3973 const int rw
= rq_data_dir(rq
);
3975 BUG_ON(!cfqq
->allocated
[rw
]);
3976 cfqq
->allocated
[rw
]--;
3978 /* Put down rq reference on cfqg */
3979 cfqg_put(RQ_CFQG(rq
));
3980 rq
->elv
.priv
[0] = NULL
;
3981 rq
->elv
.priv
[1] = NULL
;
3983 cfq_put_queue(cfqq
);
3987 static struct cfq_queue
*
3988 cfq_merge_cfqqs(struct cfq_data
*cfqd
, struct cfq_io_cq
*cic
,
3989 struct cfq_queue
*cfqq
)
3991 cfq_log_cfqq(cfqd
, cfqq
, "merging with queue %p", cfqq
->new_cfqq
);
3992 cic_set_cfqq(cic
, cfqq
->new_cfqq
, 1);
3993 cfq_mark_cfqq_coop(cfqq
->new_cfqq
);
3994 cfq_put_queue(cfqq
);
3995 return cic_to_cfqq(cic
, 1);
3999 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
4000 * was the last process referring to said cfqq.
4002 static struct cfq_queue
*
4003 split_cfqq(struct cfq_io_cq
*cic
, struct cfq_queue
*cfqq
)
4005 if (cfqq_process_refs(cfqq
) == 1) {
4006 cfqq
->pid
= current
->pid
;
4007 cfq_clear_cfqq_coop(cfqq
);
4008 cfq_clear_cfqq_split_coop(cfqq
);
4012 cic_set_cfqq(cic
, NULL
, 1);
4014 cfq_put_cooperator(cfqq
);
4016 cfq_put_queue(cfqq
);
4020 * Allocate cfq data structures associated with this request.
4023 cfq_set_request(struct request_queue
*q
, struct request
*rq
, struct bio
*bio
,
4026 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
4027 struct cfq_io_cq
*cic
= icq_to_cic(rq
->elv
.icq
);
4028 const int rw
= rq_data_dir(rq
);
4029 const bool is_sync
= rq_is_sync(rq
);
4030 struct cfq_queue
*cfqq
;
4032 might_sleep_if(gfp_mask
& __GFP_WAIT
);
4034 spin_lock_irq(q
->queue_lock
);
4036 check_ioprio_changed(cic
, bio
);
4037 check_blkcg_changed(cic
, bio
);
4039 cfqq
= cic_to_cfqq(cic
, is_sync
);
4040 if (!cfqq
|| cfqq
== &cfqd
->oom_cfqq
) {
4041 cfqq
= cfq_get_queue(cfqd
, is_sync
, cic
, bio
, gfp_mask
);
4042 cic_set_cfqq(cic
, cfqq
, is_sync
);
4045 * If the queue was seeky for too long, break it apart.
4047 if (cfq_cfqq_coop(cfqq
) && cfq_cfqq_split_coop(cfqq
)) {
4048 cfq_log_cfqq(cfqd
, cfqq
, "breaking apart cfqq");
4049 cfqq
= split_cfqq(cic
, cfqq
);
4055 * Check to see if this queue is scheduled to merge with
4056 * another, closely cooperating queue. The merging of
4057 * queues happens here as it must be done in process context.
4058 * The reference on new_cfqq was taken in merge_cfqqs.
4061 cfqq
= cfq_merge_cfqqs(cfqd
, cic
, cfqq
);
4064 cfqq
->allocated
[rw
]++;
4067 cfqg_get(cfqq
->cfqg
);
4068 rq
->elv
.priv
[0] = cfqq
;
4069 rq
->elv
.priv
[1] = cfqq
->cfqg
;
4070 spin_unlock_irq(q
->queue_lock
);
4074 static void cfq_kick_queue(struct work_struct
*work
)
4076 struct cfq_data
*cfqd
=
4077 container_of(work
, struct cfq_data
, unplug_work
);
4078 struct request_queue
*q
= cfqd
->queue
;
4080 spin_lock_irq(q
->queue_lock
);
4081 __blk_run_queue(cfqd
->queue
);
4082 spin_unlock_irq(q
->queue_lock
);
4086 * Timer running if the active_queue is currently idling inside its time slice
4088 static void cfq_idle_slice_timer(unsigned long data
)
4090 struct cfq_data
*cfqd
= (struct cfq_data
*) data
;
4091 struct cfq_queue
*cfqq
;
4092 unsigned long flags
;
4095 cfq_log(cfqd
, "idle timer fired");
4097 spin_lock_irqsave(cfqd
->queue
->queue_lock
, flags
);
4099 cfqq
= cfqd
->active_queue
;
4104 * We saw a request before the queue expired, let it through
4106 if (cfq_cfqq_must_dispatch(cfqq
))
4112 if (cfq_slice_used(cfqq
))
4116 * only expire and reinvoke request handler, if there are
4117 * other queues with pending requests
4119 if (!cfqd
->busy_queues
)
4123 * not expired and it has a request pending, let it dispatch
4125 if (!RB_EMPTY_ROOT(&cfqq
->sort_list
))
4129 * Queue depth flag is reset only when the idle didn't succeed
4131 cfq_clear_cfqq_deep(cfqq
);
4134 cfq_slice_expired(cfqd
, timed_out
);
4136 cfq_schedule_dispatch(cfqd
);
4138 spin_unlock_irqrestore(cfqd
->queue
->queue_lock
, flags
);
4141 static void cfq_shutdown_timer_wq(struct cfq_data
*cfqd
)
4143 del_timer_sync(&cfqd
->idle_slice_timer
);
4144 cancel_work_sync(&cfqd
->unplug_work
);
4147 static void cfq_put_async_queues(struct cfq_data
*cfqd
)
4151 for (i
= 0; i
< IOPRIO_BE_NR
; i
++) {
4152 if (cfqd
->async_cfqq
[0][i
])
4153 cfq_put_queue(cfqd
->async_cfqq
[0][i
]);
4154 if (cfqd
->async_cfqq
[1][i
])
4155 cfq_put_queue(cfqd
->async_cfqq
[1][i
]);
4158 if (cfqd
->async_idle_cfqq
)
4159 cfq_put_queue(cfqd
->async_idle_cfqq
);
4162 static void cfq_exit_queue(struct elevator_queue
*e
)
4164 struct cfq_data
*cfqd
= e
->elevator_data
;
4165 struct request_queue
*q
= cfqd
->queue
;
4167 cfq_shutdown_timer_wq(cfqd
);
4169 spin_lock_irq(q
->queue_lock
);
4171 if (cfqd
->active_queue
)
4172 __cfq_slice_expired(cfqd
, cfqd
->active_queue
, 0);
4174 cfq_put_async_queues(cfqd
);
4176 spin_unlock_irq(q
->queue_lock
);
4178 cfq_shutdown_timer_wq(cfqd
);
4180 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4181 blkcg_deactivate_policy(q
, &blkcg_policy_cfq
);
4183 kfree(cfqd
->root_group
);
4188 static int cfq_init_queue(struct request_queue
*q
)
4190 struct cfq_data
*cfqd
;
4191 struct blkcg_gq
*blkg __maybe_unused
;
4194 cfqd
= kmalloc_node(sizeof(*cfqd
), GFP_KERNEL
| __GFP_ZERO
, q
->node
);
4199 q
->elevator
->elevator_data
= cfqd
;
4201 /* Init root service tree */
4202 cfqd
->grp_service_tree
= CFQ_RB_ROOT
;
4204 /* Init root group and prefer root group over other groups by default */
4205 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4206 ret
= blkcg_activate_policy(q
, &blkcg_policy_cfq
);
4210 cfqd
->root_group
= blkg_to_cfqg(q
->root_blkg
);
4213 cfqd
->root_group
= kzalloc_node(sizeof(*cfqd
->root_group
),
4214 GFP_KERNEL
, cfqd
->queue
->node
);
4215 if (!cfqd
->root_group
)
4218 cfq_init_cfqg_base(cfqd
->root_group
);
4220 cfqd
->root_group
->weight
= 2 * CFQ_WEIGHT_DEFAULT
;
4221 cfqd
->root_group
->leaf_weight
= 2 * CFQ_WEIGHT_DEFAULT
;
4224 * Not strictly needed (since RB_ROOT just clears the node and we
4225 * zeroed cfqd on alloc), but better be safe in case someone decides
4226 * to add magic to the rb code
4228 for (i
= 0; i
< CFQ_PRIO_LISTS
; i
++)
4229 cfqd
->prio_trees
[i
] = RB_ROOT
;
4232 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
4233 * Grab a permanent reference to it, so that the normal code flow
4234 * will not attempt to free it. oom_cfqq is linked to root_group
4235 * but shouldn't hold a reference as it'll never be unlinked. Lose
4236 * the reference from linking right away.
4238 cfq_init_cfqq(cfqd
, &cfqd
->oom_cfqq
, 1, 0);
4239 cfqd
->oom_cfqq
.ref
++;
4241 spin_lock_irq(q
->queue_lock
);
4242 cfq_link_cfqq_cfqg(&cfqd
->oom_cfqq
, cfqd
->root_group
);
4243 cfqg_put(cfqd
->root_group
);
4244 spin_unlock_irq(q
->queue_lock
);
4246 init_timer(&cfqd
->idle_slice_timer
);
4247 cfqd
->idle_slice_timer
.function
= cfq_idle_slice_timer
;
4248 cfqd
->idle_slice_timer
.data
= (unsigned long) cfqd
;
4250 INIT_WORK(&cfqd
->unplug_work
, cfq_kick_queue
);
4252 cfqd
->cfq_quantum
= cfq_quantum
;
4253 cfqd
->cfq_fifo_expire
[0] = cfq_fifo_expire
[0];
4254 cfqd
->cfq_fifo_expire
[1] = cfq_fifo_expire
[1];
4255 cfqd
->cfq_back_max
= cfq_back_max
;
4256 cfqd
->cfq_back_penalty
= cfq_back_penalty
;
4257 cfqd
->cfq_slice
[0] = cfq_slice_async
;
4258 cfqd
->cfq_slice
[1] = cfq_slice_sync
;
4259 cfqd
->cfq_target_latency
= cfq_target_latency
;
4260 cfqd
->cfq_slice_async_rq
= cfq_slice_async_rq
;
4261 cfqd
->cfq_slice_idle
= cfq_slice_idle
;
4262 cfqd
->cfq_group_idle
= cfq_group_idle
;
4263 cfqd
->cfq_latency
= 1;
4266 * we optimistically start assuming sync ops weren't delayed in last
4267 * second, in order to have larger depth for async operations.
4269 cfqd
->last_delayed_sync
= jiffies
- HZ
;
4278 * sysfs parts below -->
4281 cfq_var_show(unsigned int var
, char *page
)
4283 return sprintf(page
, "%d\n", var
);
4287 cfq_var_store(unsigned int *var
, const char *page
, size_t count
)
4289 char *p
= (char *) page
;
4291 *var
= simple_strtoul(p
, &p
, 10);
4295 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
4296 static ssize_t __FUNC(struct elevator_queue *e, char *page) \
4298 struct cfq_data *cfqd = e->elevator_data; \
4299 unsigned int __data = __VAR; \
4301 __data = jiffies_to_msecs(__data); \
4302 return cfq_var_show(__data, (page)); \
4304 SHOW_FUNCTION(cfq_quantum_show
, cfqd
->cfq_quantum
, 0);
4305 SHOW_FUNCTION(cfq_fifo_expire_sync_show
, cfqd
->cfq_fifo_expire
[1], 1);
4306 SHOW_FUNCTION(cfq_fifo_expire_async_show
, cfqd
->cfq_fifo_expire
[0], 1);
4307 SHOW_FUNCTION(cfq_back_seek_max_show
, cfqd
->cfq_back_max
, 0);
4308 SHOW_FUNCTION(cfq_back_seek_penalty_show
, cfqd
->cfq_back_penalty
, 0);
4309 SHOW_FUNCTION(cfq_slice_idle_show
, cfqd
->cfq_slice_idle
, 1);
4310 SHOW_FUNCTION(cfq_group_idle_show
, cfqd
->cfq_group_idle
, 1);
4311 SHOW_FUNCTION(cfq_slice_sync_show
, cfqd
->cfq_slice
[1], 1);
4312 SHOW_FUNCTION(cfq_slice_async_show
, cfqd
->cfq_slice
[0], 1);
4313 SHOW_FUNCTION(cfq_slice_async_rq_show
, cfqd
->cfq_slice_async_rq
, 0);
4314 SHOW_FUNCTION(cfq_low_latency_show
, cfqd
->cfq_latency
, 0);
4315 SHOW_FUNCTION(cfq_target_latency_show
, cfqd
->cfq_target_latency
, 1);
4316 #undef SHOW_FUNCTION
4318 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
4319 static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
4321 struct cfq_data *cfqd = e->elevator_data; \
4322 unsigned int __data; \
4323 int ret = cfq_var_store(&__data, (page), count); \
4324 if (__data < (MIN)) \
4326 else if (__data > (MAX)) \
4329 *(__PTR) = msecs_to_jiffies(__data); \
4331 *(__PTR) = __data; \
4334 STORE_FUNCTION(cfq_quantum_store
, &cfqd
->cfq_quantum
, 1, UINT_MAX
, 0);
4335 STORE_FUNCTION(cfq_fifo_expire_sync_store
, &cfqd
->cfq_fifo_expire
[1], 1,
4337 STORE_FUNCTION(cfq_fifo_expire_async_store
, &cfqd
->cfq_fifo_expire
[0], 1,
4339 STORE_FUNCTION(cfq_back_seek_max_store
, &cfqd
->cfq_back_max
, 0, UINT_MAX
, 0);
4340 STORE_FUNCTION(cfq_back_seek_penalty_store
, &cfqd
->cfq_back_penalty
, 1,
4342 STORE_FUNCTION(cfq_slice_idle_store
, &cfqd
->cfq_slice_idle
, 0, UINT_MAX
, 1);
4343 STORE_FUNCTION(cfq_group_idle_store
, &cfqd
->cfq_group_idle
, 0, UINT_MAX
, 1);
4344 STORE_FUNCTION(cfq_slice_sync_store
, &cfqd
->cfq_slice
[1], 1, UINT_MAX
, 1);
4345 STORE_FUNCTION(cfq_slice_async_store
, &cfqd
->cfq_slice
[0], 1, UINT_MAX
, 1);
4346 STORE_FUNCTION(cfq_slice_async_rq_store
, &cfqd
->cfq_slice_async_rq
, 1,
4348 STORE_FUNCTION(cfq_low_latency_store
, &cfqd
->cfq_latency
, 0, 1, 0);
4349 STORE_FUNCTION(cfq_target_latency_store
, &cfqd
->cfq_target_latency
, 1, UINT_MAX
, 1);
4350 #undef STORE_FUNCTION
4352 #define CFQ_ATTR(name) \
4353 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
4355 static struct elv_fs_entry cfq_attrs
[] = {
4357 CFQ_ATTR(fifo_expire_sync
),
4358 CFQ_ATTR(fifo_expire_async
),
4359 CFQ_ATTR(back_seek_max
),
4360 CFQ_ATTR(back_seek_penalty
),
4361 CFQ_ATTR(slice_sync
),
4362 CFQ_ATTR(slice_async
),
4363 CFQ_ATTR(slice_async_rq
),
4364 CFQ_ATTR(slice_idle
),
4365 CFQ_ATTR(group_idle
),
4366 CFQ_ATTR(low_latency
),
4367 CFQ_ATTR(target_latency
),
4371 static struct elevator_type iosched_cfq
= {
4373 .elevator_merge_fn
= cfq_merge
,
4374 .elevator_merged_fn
= cfq_merged_request
,
4375 .elevator_merge_req_fn
= cfq_merged_requests
,
4376 .elevator_allow_merge_fn
= cfq_allow_merge
,
4377 .elevator_bio_merged_fn
= cfq_bio_merged
,
4378 .elevator_dispatch_fn
= cfq_dispatch_requests
,
4379 .elevator_add_req_fn
= cfq_insert_request
,
4380 .elevator_activate_req_fn
= cfq_activate_request
,
4381 .elevator_deactivate_req_fn
= cfq_deactivate_request
,
4382 .elevator_completed_req_fn
= cfq_completed_request
,
4383 .elevator_former_req_fn
= elv_rb_former_request
,
4384 .elevator_latter_req_fn
= elv_rb_latter_request
,
4385 .elevator_init_icq_fn
= cfq_init_icq
,
4386 .elevator_exit_icq_fn
= cfq_exit_icq
,
4387 .elevator_set_req_fn
= cfq_set_request
,
4388 .elevator_put_req_fn
= cfq_put_request
,
4389 .elevator_may_queue_fn
= cfq_may_queue
,
4390 .elevator_init_fn
= cfq_init_queue
,
4391 .elevator_exit_fn
= cfq_exit_queue
,
4393 .icq_size
= sizeof(struct cfq_io_cq
),
4394 .icq_align
= __alignof__(struct cfq_io_cq
),
4395 .elevator_attrs
= cfq_attrs
,
4396 .elevator_name
= "cfq",
4397 .elevator_owner
= THIS_MODULE
,
4400 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4401 static struct blkcg_policy blkcg_policy_cfq
= {
4402 .pd_size
= sizeof(struct cfq_group
),
4403 .cftypes
= cfq_blkcg_files
,
4405 .pd_init_fn
= cfq_pd_init
,
4406 .pd_reset_stats_fn
= cfq_pd_reset_stats
,
4410 static int __init
cfq_init(void)
4415 * could be 0 on HZ < 1000 setups
4417 if (!cfq_slice_async
)
4418 cfq_slice_async
= 1;
4419 if (!cfq_slice_idle
)
4422 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4423 if (!cfq_group_idle
)
4426 ret
= blkcg_policy_register(&blkcg_policy_cfq
);
4434 cfq_pool
= KMEM_CACHE(cfq_queue
, 0);
4438 ret
= elv_register(&iosched_cfq
);
4445 kmem_cache_destroy(cfq_pool
);
4447 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4448 blkcg_policy_unregister(&blkcg_policy_cfq
);
4453 static void __exit
cfq_exit(void)
4455 #ifdef CONFIG_CFQ_GROUP_IOSCHED
4456 blkcg_policy_unregister(&blkcg_policy_cfq
);
4458 elv_unregister(&iosched_cfq
);
4459 kmem_cache_destroy(cfq_pool
);
4462 module_init(cfq_init
);
4463 module_exit(cfq_exit
);
4465 MODULE_AUTHOR("Jens Axboe");
4466 MODULE_LICENSE("GPL");
4467 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");