cciss: Fix problem with scatter gather elements in the scsi half of the driver
[linux-2.6/next.git] / block / cfq-iosched.c
blob423aee3fd19b7ad44847d9ab4a0546df4cbce004
1 /*
2 * CFQ, or complete fairness queueing, disk scheduler.
4 * Based on ideas from a previously unfinished io
5 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
7 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
8 */
9 #include <linux/module.h>
10 #include <linux/blkdev.h>
11 #include <linux/elevator.h>
12 #include <linux/jiffies.h>
13 #include <linux/rbtree.h>
14 #include <linux/ioprio.h>
15 #include <linux/blktrace_api.h>
16 #include "blk-cgroup.h"
19 * tunables
21 /* max queue in one round of service */
22 static const int cfq_quantum = 4;
23 static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
24 /* maximum backwards seek, in KiB */
25 static const int cfq_back_max = 16 * 1024;
26 /* penalty of a backwards seek */
27 static const int cfq_back_penalty = 2;
28 static const int cfq_slice_sync = HZ / 10;
29 static int cfq_slice_async = HZ / 25;
30 static const int cfq_slice_async_rq = 2;
31 static int cfq_slice_idle = HZ / 125;
32 static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
33 static const int cfq_hist_divisor = 4;
36 * offset from end of service tree
38 #define CFQ_IDLE_DELAY (HZ / 5)
41 * below this threshold, we consider thinktime immediate
43 #define CFQ_MIN_TT (2)
45 #define CFQ_SLICE_SCALE (5)
46 #define CFQ_HW_QUEUE_MIN (5)
47 #define CFQ_SERVICE_SHIFT 12
49 #define CFQQ_SEEK_THR (sector_t)(8 * 100)
50 #define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
51 #define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8)
53 #define RQ_CIC(rq) \
54 ((struct cfq_io_context *) (rq)->elevator_private)
55 #define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elevator_private2)
57 static struct kmem_cache *cfq_pool;
58 static struct kmem_cache *cfq_ioc_pool;
60 static DEFINE_PER_CPU(unsigned long, cfq_ioc_count);
61 static struct completion *ioc_gone;
62 static DEFINE_SPINLOCK(ioc_gone_lock);
64 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
65 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
66 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
68 #define sample_valid(samples) ((samples) > 80)
69 #define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
72 * Most of our rbtree usage is for sorting with min extraction, so
73 * if we cache the leftmost node we don't have to walk down the tree
74 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
75 * move this into the elevator for the rq sorting as well.
77 struct cfq_rb_root {
78 struct rb_root rb;
79 struct rb_node *left;
80 unsigned count;
81 u64 min_vdisktime;
82 struct rb_node *active;
83 unsigned total_weight;
85 #define CFQ_RB_ROOT (struct cfq_rb_root) { RB_ROOT, NULL, 0, 0, }
88 * Per process-grouping structure
90 struct cfq_queue {
91 /* reference count */
92 atomic_t ref;
93 /* various state flags, see below */
94 unsigned int flags;
95 /* parent cfq_data */
96 struct cfq_data *cfqd;
97 /* service_tree member */
98 struct rb_node rb_node;
99 /* service_tree key */
100 unsigned long rb_key;
101 /* prio tree member */
102 struct rb_node p_node;
103 /* prio tree root we belong to, if any */
104 struct rb_root *p_root;
105 /* sorted list of pending requests */
106 struct rb_root sort_list;
107 /* if fifo isn't expired, next request to serve */
108 struct request *next_rq;
109 /* requests queued in sort_list */
110 int queued[2];
111 /* currently allocated requests */
112 int allocated[2];
113 /* fifo list of requests in sort_list */
114 struct list_head fifo;
116 /* time when queue got scheduled in to dispatch first request. */
117 unsigned long dispatch_start;
118 unsigned int allocated_slice;
119 unsigned int slice_dispatch;
120 /* time when first request from queue completed and slice started. */
121 unsigned long slice_start;
122 unsigned long slice_end;
123 long slice_resid;
125 /* pending metadata requests */
126 int meta_pending;
127 /* number of requests that are on the dispatch list or inside driver */
128 int dispatched;
130 /* io prio of this group */
131 unsigned short ioprio, org_ioprio;
132 unsigned short ioprio_class, org_ioprio_class;
134 pid_t pid;
136 u32 seek_history;
137 sector_t last_request_pos;
139 struct cfq_rb_root *service_tree;
140 struct cfq_queue *new_cfqq;
141 struct cfq_group *cfqg;
142 struct cfq_group *orig_cfqg;
143 /* Sectors dispatched in current dispatch round */
144 unsigned long nr_sectors;
148 * First index in the service_trees.
149 * IDLE is handled separately, so it has negative index
151 enum wl_prio_t {
152 BE_WORKLOAD = 0,
153 RT_WORKLOAD = 1,
154 IDLE_WORKLOAD = 2,
158 * Second index in the service_trees.
160 enum wl_type_t {
161 ASYNC_WORKLOAD = 0,
162 SYNC_NOIDLE_WORKLOAD = 1,
163 SYNC_WORKLOAD = 2
166 /* This is per cgroup per device grouping structure */
167 struct cfq_group {
168 /* group service_tree member */
169 struct rb_node rb_node;
171 /* group service_tree key */
172 u64 vdisktime;
173 unsigned int weight;
174 bool on_st;
176 /* number of cfqq currently on this group */
177 int nr_cfqq;
179 /* Per group busy queus average. Useful for workload slice calc. */
180 unsigned int busy_queues_avg[2];
182 * rr lists of queues with requests, onle rr for each priority class.
183 * Counts are embedded in the cfq_rb_root
185 struct cfq_rb_root service_trees[2][3];
186 struct cfq_rb_root service_tree_idle;
188 unsigned long saved_workload_slice;
189 enum wl_type_t saved_workload;
190 enum wl_prio_t saved_serving_prio;
191 struct blkio_group blkg;
192 #ifdef CONFIG_CFQ_GROUP_IOSCHED
193 struct hlist_node cfqd_node;
194 atomic_t ref;
195 #endif
199 * Per block device queue structure
201 struct cfq_data {
202 struct request_queue *queue;
203 /* Root service tree for cfq_groups */
204 struct cfq_rb_root grp_service_tree;
205 struct cfq_group root_group;
208 * The priority currently being served
210 enum wl_prio_t serving_prio;
211 enum wl_type_t serving_type;
212 unsigned long workload_expires;
213 struct cfq_group *serving_group;
214 bool noidle_tree_requires_idle;
217 * Each priority tree is sorted by next_request position. These
218 * trees are used when determining if two or more queues are
219 * interleaving requests (see cfq_close_cooperator).
221 struct rb_root prio_trees[CFQ_PRIO_LISTS];
223 unsigned int busy_queues;
225 int rq_in_driver[2];
226 int sync_flight;
229 * queue-depth detection
231 int rq_queued;
232 int hw_tag;
234 * hw_tag can be
235 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
236 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
237 * 0 => no NCQ
239 int hw_tag_est_depth;
240 unsigned int hw_tag_samples;
243 * idle window management
245 struct timer_list idle_slice_timer;
246 struct work_struct unplug_work;
248 struct cfq_queue *active_queue;
249 struct cfq_io_context *active_cic;
252 * async queue for each priority case
254 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
255 struct cfq_queue *async_idle_cfqq;
257 sector_t last_position;
260 * tunables, see top of file
262 unsigned int cfq_quantum;
263 unsigned int cfq_fifo_expire[2];
264 unsigned int cfq_back_penalty;
265 unsigned int cfq_back_max;
266 unsigned int cfq_slice[2];
267 unsigned int cfq_slice_async_rq;
268 unsigned int cfq_slice_idle;
269 unsigned int cfq_latency;
270 unsigned int cfq_group_isolation;
272 struct list_head cic_list;
275 * Fallback dummy cfqq for extreme OOM conditions
277 struct cfq_queue oom_cfqq;
279 unsigned long last_delayed_sync;
281 /* List of cfq groups being managed on this device*/
282 struct hlist_head cfqg_list;
283 struct rcu_head rcu;
286 static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
288 static struct cfq_rb_root *service_tree_for(struct cfq_group *cfqg,
289 enum wl_prio_t prio,
290 enum wl_type_t type)
292 if (!cfqg)
293 return NULL;
295 if (prio == IDLE_WORKLOAD)
296 return &cfqg->service_tree_idle;
298 return &cfqg->service_trees[prio][type];
301 enum cfqq_state_flags {
302 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
303 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
304 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
305 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
306 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
307 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
308 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
309 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
310 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
311 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
312 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
313 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
314 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
317 #define CFQ_CFQQ_FNS(name) \
318 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
320 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
322 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
324 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
326 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
328 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
331 CFQ_CFQQ_FNS(on_rr);
332 CFQ_CFQQ_FNS(wait_request);
333 CFQ_CFQQ_FNS(must_dispatch);
334 CFQ_CFQQ_FNS(must_alloc_slice);
335 CFQ_CFQQ_FNS(fifo_expire);
336 CFQ_CFQQ_FNS(idle_window);
337 CFQ_CFQQ_FNS(prio_changed);
338 CFQ_CFQQ_FNS(slice_new);
339 CFQ_CFQQ_FNS(sync);
340 CFQ_CFQQ_FNS(coop);
341 CFQ_CFQQ_FNS(split_coop);
342 CFQ_CFQQ_FNS(deep);
343 CFQ_CFQQ_FNS(wait_busy);
344 #undef CFQ_CFQQ_FNS
346 #ifdef CONFIG_DEBUG_CFQ_IOSCHED
347 #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
348 blk_add_trace_msg((cfqd)->queue, "cfq%d%c %s " fmt, (cfqq)->pid, \
349 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
350 blkg_path(&(cfqq)->cfqg->blkg), ##args);
352 #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) \
353 blk_add_trace_msg((cfqd)->queue, "%s " fmt, \
354 blkg_path(&(cfqg)->blkg), ##args); \
356 #else
357 #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
358 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
359 #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0);
360 #endif
361 #define cfq_log(cfqd, fmt, args...) \
362 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
364 /* Traverses through cfq group service trees */
365 #define for_each_cfqg_st(cfqg, i, j, st) \
366 for (i = 0; i <= IDLE_WORKLOAD; i++) \
367 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
368 : &cfqg->service_tree_idle; \
369 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
370 (i == IDLE_WORKLOAD && j == 0); \
371 j++, st = i < IDLE_WORKLOAD ? \
372 &cfqg->service_trees[i][j]: NULL) \
375 static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq)
377 if (cfq_class_idle(cfqq))
378 return IDLE_WORKLOAD;
379 if (cfq_class_rt(cfqq))
380 return RT_WORKLOAD;
381 return BE_WORKLOAD;
385 static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
387 if (!cfq_cfqq_sync(cfqq))
388 return ASYNC_WORKLOAD;
389 if (!cfq_cfqq_idle_window(cfqq))
390 return SYNC_NOIDLE_WORKLOAD;
391 return SYNC_WORKLOAD;
394 static inline int cfq_group_busy_queues_wl(enum wl_prio_t wl,
395 struct cfq_data *cfqd,
396 struct cfq_group *cfqg)
398 if (wl == IDLE_WORKLOAD)
399 return cfqg->service_tree_idle.count;
401 return cfqg->service_trees[wl][ASYNC_WORKLOAD].count
402 + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count
403 + cfqg->service_trees[wl][SYNC_WORKLOAD].count;
406 static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
407 struct cfq_group *cfqg)
409 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count
410 + cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
413 static void cfq_dispatch_insert(struct request_queue *, struct request *);
414 static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool,
415 struct io_context *, gfp_t);
416 static struct cfq_io_context *cfq_cic_lookup(struct cfq_data *,
417 struct io_context *);
419 static inline int rq_in_driver(struct cfq_data *cfqd)
421 return cfqd->rq_in_driver[0] + cfqd->rq_in_driver[1];
424 static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_context *cic,
425 bool is_sync)
427 return cic->cfqq[is_sync];
430 static inline void cic_set_cfqq(struct cfq_io_context *cic,
431 struct cfq_queue *cfqq, bool is_sync)
433 cic->cfqq[is_sync] = cfqq;
437 * We regard a request as SYNC, if it's either a read or has the SYNC bit
438 * set (in which case it could also be direct WRITE).
440 static inline bool cfq_bio_sync(struct bio *bio)
442 return bio_data_dir(bio) == READ || bio_rw_flagged(bio, BIO_RW_SYNCIO);
446 * scheduler run of queue, if there are requests pending and no one in the
447 * driver that will restart queueing
449 static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
451 if (cfqd->busy_queues) {
452 cfq_log(cfqd, "schedule dispatch");
453 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
457 static int cfq_queue_empty(struct request_queue *q)
459 struct cfq_data *cfqd = q->elevator->elevator_data;
461 return !cfqd->rq_queued;
465 * Scale schedule slice based on io priority. Use the sync time slice only
466 * if a queue is marked sync and has sync io queued. A sync queue with async
467 * io only, should not get full sync slice length.
469 static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
470 unsigned short prio)
472 const int base_slice = cfqd->cfq_slice[sync];
474 WARN_ON(prio >= IOPRIO_BE_NR);
476 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
479 static inline int
480 cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
482 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
485 static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
487 u64 d = delta << CFQ_SERVICE_SHIFT;
489 d = d * BLKIO_WEIGHT_DEFAULT;
490 do_div(d, cfqg->weight);
491 return d;
494 static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
496 s64 delta = (s64)(vdisktime - min_vdisktime);
497 if (delta > 0)
498 min_vdisktime = vdisktime;
500 return min_vdisktime;
503 static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
505 s64 delta = (s64)(vdisktime - min_vdisktime);
506 if (delta < 0)
507 min_vdisktime = vdisktime;
509 return min_vdisktime;
512 static void update_min_vdisktime(struct cfq_rb_root *st)
514 u64 vdisktime = st->min_vdisktime;
515 struct cfq_group *cfqg;
517 if (st->active) {
518 cfqg = rb_entry_cfqg(st->active);
519 vdisktime = cfqg->vdisktime;
522 if (st->left) {
523 cfqg = rb_entry_cfqg(st->left);
524 vdisktime = min_vdisktime(vdisktime, cfqg->vdisktime);
527 st->min_vdisktime = max_vdisktime(st->min_vdisktime, vdisktime);
531 * get averaged number of queues of RT/BE priority.
532 * average is updated, with a formula that gives more weight to higher numbers,
533 * to quickly follows sudden increases and decrease slowly
536 static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
537 struct cfq_group *cfqg, bool rt)
539 unsigned min_q, max_q;
540 unsigned mult = cfq_hist_divisor - 1;
541 unsigned round = cfq_hist_divisor / 2;
542 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
544 min_q = min(cfqg->busy_queues_avg[rt], busy);
545 max_q = max(cfqg->busy_queues_avg[rt], busy);
546 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
547 cfq_hist_divisor;
548 return cfqg->busy_queues_avg[rt];
551 static inline unsigned
552 cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
554 struct cfq_rb_root *st = &cfqd->grp_service_tree;
556 return cfq_target_latency * cfqg->weight / st->total_weight;
559 static inline void
560 cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
562 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
563 if (cfqd->cfq_latency) {
565 * interested queues (we consider only the ones with the same
566 * priority class in the cfq group)
568 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
569 cfq_class_rt(cfqq));
570 unsigned sync_slice = cfqd->cfq_slice[1];
571 unsigned expect_latency = sync_slice * iq;
572 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
574 if (expect_latency > group_slice) {
575 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
576 /* scale low_slice according to IO priority
577 * and sync vs async */
578 unsigned low_slice =
579 min(slice, base_low_slice * slice / sync_slice);
580 /* the adapted slice value is scaled to fit all iqs
581 * into the target latency */
582 slice = max(slice * group_slice / expect_latency,
583 low_slice);
586 cfqq->slice_start = jiffies;
587 cfqq->slice_end = jiffies + slice;
588 cfqq->allocated_slice = slice;
589 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
593 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
594 * isn't valid until the first request from the dispatch is activated
595 * and the slice time set.
597 static inline bool cfq_slice_used(struct cfq_queue *cfqq)
599 if (cfq_cfqq_slice_new(cfqq))
600 return 0;
601 if (time_before(jiffies, cfqq->slice_end))
602 return 0;
604 return 1;
608 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
609 * We choose the request that is closest to the head right now. Distance
610 * behind the head is penalized and only allowed to a certain extent.
612 static struct request *
613 cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
615 sector_t s1, s2, d1 = 0, d2 = 0;
616 unsigned long back_max;
617 #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
618 #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
619 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
621 if (rq1 == NULL || rq1 == rq2)
622 return rq2;
623 if (rq2 == NULL)
624 return rq1;
626 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
627 return rq1;
628 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
629 return rq2;
630 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
631 return rq1;
632 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
633 return rq2;
635 s1 = blk_rq_pos(rq1);
636 s2 = blk_rq_pos(rq2);
639 * by definition, 1KiB is 2 sectors
641 back_max = cfqd->cfq_back_max * 2;
644 * Strict one way elevator _except_ in the case where we allow
645 * short backward seeks which are biased as twice the cost of a
646 * similar forward seek.
648 if (s1 >= last)
649 d1 = s1 - last;
650 else if (s1 + back_max >= last)
651 d1 = (last - s1) * cfqd->cfq_back_penalty;
652 else
653 wrap |= CFQ_RQ1_WRAP;
655 if (s2 >= last)
656 d2 = s2 - last;
657 else if (s2 + back_max >= last)
658 d2 = (last - s2) * cfqd->cfq_back_penalty;
659 else
660 wrap |= CFQ_RQ2_WRAP;
662 /* Found required data */
665 * By doing switch() on the bit mask "wrap" we avoid having to
666 * check two variables for all permutations: --> faster!
668 switch (wrap) {
669 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
670 if (d1 < d2)
671 return rq1;
672 else if (d2 < d1)
673 return rq2;
674 else {
675 if (s1 >= s2)
676 return rq1;
677 else
678 return rq2;
681 case CFQ_RQ2_WRAP:
682 return rq1;
683 case CFQ_RQ1_WRAP:
684 return rq2;
685 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
686 default:
688 * Since both rqs are wrapped,
689 * start with the one that's further behind head
690 * (--> only *one* back seek required),
691 * since back seek takes more time than forward.
693 if (s1 <= s2)
694 return rq1;
695 else
696 return rq2;
701 * The below is leftmost cache rbtree addon
703 static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
705 /* Service tree is empty */
706 if (!root->count)
707 return NULL;
709 if (!root->left)
710 root->left = rb_first(&root->rb);
712 if (root->left)
713 return rb_entry(root->left, struct cfq_queue, rb_node);
715 return NULL;
718 static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
720 if (!root->left)
721 root->left = rb_first(&root->rb);
723 if (root->left)
724 return rb_entry_cfqg(root->left);
726 return NULL;
729 static void rb_erase_init(struct rb_node *n, struct rb_root *root)
731 rb_erase(n, root);
732 RB_CLEAR_NODE(n);
735 static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
737 if (root->left == n)
738 root->left = NULL;
739 rb_erase_init(n, &root->rb);
740 --root->count;
744 * would be nice to take fifo expire time into account as well
746 static struct request *
747 cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
748 struct request *last)
750 struct rb_node *rbnext = rb_next(&last->rb_node);
751 struct rb_node *rbprev = rb_prev(&last->rb_node);
752 struct request *next = NULL, *prev = NULL;
754 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
756 if (rbprev)
757 prev = rb_entry_rq(rbprev);
759 if (rbnext)
760 next = rb_entry_rq(rbnext);
761 else {
762 rbnext = rb_first(&cfqq->sort_list);
763 if (rbnext && rbnext != &last->rb_node)
764 next = rb_entry_rq(rbnext);
767 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
770 static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
771 struct cfq_queue *cfqq)
774 * just an approximation, should be ok.
776 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
777 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
780 static inline s64
781 cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
783 return cfqg->vdisktime - st->min_vdisktime;
786 static void
787 __cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
789 struct rb_node **node = &st->rb.rb_node;
790 struct rb_node *parent = NULL;
791 struct cfq_group *__cfqg;
792 s64 key = cfqg_key(st, cfqg);
793 int left = 1;
795 while (*node != NULL) {
796 parent = *node;
797 __cfqg = rb_entry_cfqg(parent);
799 if (key < cfqg_key(st, __cfqg))
800 node = &parent->rb_left;
801 else {
802 node = &parent->rb_right;
803 left = 0;
807 if (left)
808 st->left = &cfqg->rb_node;
810 rb_link_node(&cfqg->rb_node, parent, node);
811 rb_insert_color(&cfqg->rb_node, &st->rb);
814 static void
815 cfq_group_service_tree_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
817 struct cfq_rb_root *st = &cfqd->grp_service_tree;
818 struct cfq_group *__cfqg;
819 struct rb_node *n;
821 cfqg->nr_cfqq++;
822 if (cfqg->on_st)
823 return;
826 * Currently put the group at the end. Later implement something
827 * so that groups get lesser vtime based on their weights, so that
828 * if group does not loose all if it was not continously backlogged.
830 n = rb_last(&st->rb);
831 if (n) {
832 __cfqg = rb_entry_cfqg(n);
833 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
834 } else
835 cfqg->vdisktime = st->min_vdisktime;
837 __cfq_group_service_tree_add(st, cfqg);
838 cfqg->on_st = true;
839 st->total_weight += cfqg->weight;
842 static void
843 cfq_group_service_tree_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
845 struct cfq_rb_root *st = &cfqd->grp_service_tree;
847 if (st->active == &cfqg->rb_node)
848 st->active = NULL;
850 BUG_ON(cfqg->nr_cfqq < 1);
851 cfqg->nr_cfqq--;
853 /* If there are other cfq queues under this group, don't delete it */
854 if (cfqg->nr_cfqq)
855 return;
857 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
858 cfqg->on_st = false;
859 st->total_weight -= cfqg->weight;
860 if (!RB_EMPTY_NODE(&cfqg->rb_node))
861 cfq_rb_erase(&cfqg->rb_node, st);
862 cfqg->saved_workload_slice = 0;
863 blkiocg_update_blkio_group_dequeue_stats(&cfqg->blkg, 1);
866 static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq)
868 unsigned int slice_used;
871 * Queue got expired before even a single request completed or
872 * got expired immediately after first request completion.
874 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
876 * Also charge the seek time incurred to the group, otherwise
877 * if there are mutiple queues in the group, each can dispatch
878 * a single request on seeky media and cause lots of seek time
879 * and group will never know it.
881 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
883 } else {
884 slice_used = jiffies - cfqq->slice_start;
885 if (slice_used > cfqq->allocated_slice)
886 slice_used = cfqq->allocated_slice;
889 cfq_log_cfqq(cfqq->cfqd, cfqq, "sl_used=%u sect=%lu", slice_used,
890 cfqq->nr_sectors);
891 return slice_used;
894 static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
895 struct cfq_queue *cfqq)
897 struct cfq_rb_root *st = &cfqd->grp_service_tree;
898 unsigned int used_sl, charge_sl;
899 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
900 - cfqg->service_tree_idle.count;
902 BUG_ON(nr_sync < 0);
903 used_sl = charge_sl = cfq_cfqq_slice_usage(cfqq);
905 if (!cfq_cfqq_sync(cfqq) && !nr_sync)
906 charge_sl = cfqq->allocated_slice;
908 /* Can't update vdisktime while group is on service tree */
909 cfq_rb_erase(&cfqg->rb_node, st);
910 cfqg->vdisktime += cfq_scale_slice(charge_sl, cfqg);
911 __cfq_group_service_tree_add(st, cfqg);
913 /* This group is being expired. Save the context */
914 if (time_after(cfqd->workload_expires, jiffies)) {
915 cfqg->saved_workload_slice = cfqd->workload_expires
916 - jiffies;
917 cfqg->saved_workload = cfqd->serving_type;
918 cfqg->saved_serving_prio = cfqd->serving_prio;
919 } else
920 cfqg->saved_workload_slice = 0;
922 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
923 st->min_vdisktime);
924 blkiocg_update_blkio_group_stats(&cfqg->blkg, used_sl,
925 cfqq->nr_sectors);
928 #ifdef CONFIG_CFQ_GROUP_IOSCHED
929 static inline struct cfq_group *cfqg_of_blkg(struct blkio_group *blkg)
931 if (blkg)
932 return container_of(blkg, struct cfq_group, blkg);
933 return NULL;
936 void
937 cfq_update_blkio_group_weight(struct blkio_group *blkg, unsigned int weight)
939 cfqg_of_blkg(blkg)->weight = weight;
942 static struct cfq_group *
943 cfq_find_alloc_cfqg(struct cfq_data *cfqd, struct cgroup *cgroup, int create)
945 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
946 struct cfq_group *cfqg = NULL;
947 void *key = cfqd;
948 int i, j;
949 struct cfq_rb_root *st;
950 struct backing_dev_info *bdi = &cfqd->queue->backing_dev_info;
951 unsigned int major, minor;
953 cfqg = cfqg_of_blkg(blkiocg_lookup_group(blkcg, key));
954 if (cfqg || !create)
955 goto done;
957 cfqg = kzalloc_node(sizeof(*cfqg), GFP_ATOMIC, cfqd->queue->node);
958 if (!cfqg)
959 goto done;
961 cfqg->weight = blkcg->weight;
962 for_each_cfqg_st(cfqg, i, j, st)
963 *st = CFQ_RB_ROOT;
964 RB_CLEAR_NODE(&cfqg->rb_node);
967 * Take the initial reference that will be released on destroy
968 * This can be thought of a joint reference by cgroup and
969 * elevator which will be dropped by either elevator exit
970 * or cgroup deletion path depending on who is exiting first.
972 atomic_set(&cfqg->ref, 1);
974 /* Add group onto cgroup list */
975 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
976 blkiocg_add_blkio_group(blkcg, &cfqg->blkg, (void *)cfqd,
977 MKDEV(major, minor));
979 /* Add group on cfqd list */
980 hlist_add_head(&cfqg->cfqd_node, &cfqd->cfqg_list);
982 done:
983 return cfqg;
987 * Search for the cfq group current task belongs to. If create = 1, then also
988 * create the cfq group if it does not exist. request_queue lock must be held.
990 static struct cfq_group *cfq_get_cfqg(struct cfq_data *cfqd, int create)
992 struct cgroup *cgroup;
993 struct cfq_group *cfqg = NULL;
995 rcu_read_lock();
996 cgroup = task_cgroup(current, blkio_subsys_id);
997 cfqg = cfq_find_alloc_cfqg(cfqd, cgroup, create);
998 if (!cfqg && create)
999 cfqg = &cfqd->root_group;
1000 rcu_read_unlock();
1001 return cfqg;
1004 static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1006 /* Currently, all async queues are mapped to root group */
1007 if (!cfq_cfqq_sync(cfqq))
1008 cfqg = &cfqq->cfqd->root_group;
1010 cfqq->cfqg = cfqg;
1011 /* cfqq reference on cfqg */
1012 atomic_inc(&cfqq->cfqg->ref);
1015 static void cfq_put_cfqg(struct cfq_group *cfqg)
1017 struct cfq_rb_root *st;
1018 int i, j;
1020 BUG_ON(atomic_read(&cfqg->ref) <= 0);
1021 if (!atomic_dec_and_test(&cfqg->ref))
1022 return;
1023 for_each_cfqg_st(cfqg, i, j, st)
1024 BUG_ON(!RB_EMPTY_ROOT(&st->rb) || st->active != NULL);
1025 kfree(cfqg);
1028 static void cfq_destroy_cfqg(struct cfq_data *cfqd, struct cfq_group *cfqg)
1030 /* Something wrong if we are trying to remove same group twice */
1031 BUG_ON(hlist_unhashed(&cfqg->cfqd_node));
1033 hlist_del_init(&cfqg->cfqd_node);
1036 * Put the reference taken at the time of creation so that when all
1037 * queues are gone, group can be destroyed.
1039 cfq_put_cfqg(cfqg);
1042 static void cfq_release_cfq_groups(struct cfq_data *cfqd)
1044 struct hlist_node *pos, *n;
1045 struct cfq_group *cfqg;
1047 hlist_for_each_entry_safe(cfqg, pos, n, &cfqd->cfqg_list, cfqd_node) {
1049 * If cgroup removal path got to blk_group first and removed
1050 * it from cgroup list, then it will take care of destroying
1051 * cfqg also.
1053 if (!blkiocg_del_blkio_group(&cfqg->blkg))
1054 cfq_destroy_cfqg(cfqd, cfqg);
1059 * Blk cgroup controller notification saying that blkio_group object is being
1060 * delinked as associated cgroup object is going away. That also means that
1061 * no new IO will come in this group. So get rid of this group as soon as
1062 * any pending IO in the group is finished.
1064 * This function is called under rcu_read_lock(). key is the rcu protected
1065 * pointer. That means "key" is a valid cfq_data pointer as long as we are rcu
1066 * read lock.
1068 * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
1069 * it should not be NULL as even if elevator was exiting, cgroup deltion
1070 * path got to it first.
1072 void cfq_unlink_blkio_group(void *key, struct blkio_group *blkg)
1074 unsigned long flags;
1075 struct cfq_data *cfqd = key;
1077 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1078 cfq_destroy_cfqg(cfqd, cfqg_of_blkg(blkg));
1079 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1082 #else /* GROUP_IOSCHED */
1083 static struct cfq_group *cfq_get_cfqg(struct cfq_data *cfqd, int create)
1085 return &cfqd->root_group;
1087 static inline void
1088 cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1089 cfqq->cfqg = cfqg;
1092 static void cfq_release_cfq_groups(struct cfq_data *cfqd) {}
1093 static inline void cfq_put_cfqg(struct cfq_group *cfqg) {}
1095 #endif /* GROUP_IOSCHED */
1098 * The cfqd->service_trees holds all pending cfq_queue's that have
1099 * requests waiting to be processed. It is sorted in the order that
1100 * we will service the queues.
1102 static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1103 bool add_front)
1105 struct rb_node **p, *parent;
1106 struct cfq_queue *__cfqq;
1107 unsigned long rb_key;
1108 struct cfq_rb_root *service_tree;
1109 int left;
1110 int new_cfqq = 1;
1111 int group_changed = 0;
1113 #ifdef CONFIG_CFQ_GROUP_IOSCHED
1114 if (!cfqd->cfq_group_isolation
1115 && cfqq_type(cfqq) == SYNC_NOIDLE_WORKLOAD
1116 && cfqq->cfqg && cfqq->cfqg != &cfqd->root_group) {
1117 /* Move this cfq to root group */
1118 cfq_log_cfqq(cfqd, cfqq, "moving to root group");
1119 if (!RB_EMPTY_NODE(&cfqq->rb_node))
1120 cfq_group_service_tree_del(cfqd, cfqq->cfqg);
1121 cfqq->orig_cfqg = cfqq->cfqg;
1122 cfqq->cfqg = &cfqd->root_group;
1123 atomic_inc(&cfqd->root_group.ref);
1124 group_changed = 1;
1125 } else if (!cfqd->cfq_group_isolation
1126 && cfqq_type(cfqq) == SYNC_WORKLOAD && cfqq->orig_cfqg) {
1127 /* cfqq is sequential now needs to go to its original group */
1128 BUG_ON(cfqq->cfqg != &cfqd->root_group);
1129 if (!RB_EMPTY_NODE(&cfqq->rb_node))
1130 cfq_group_service_tree_del(cfqd, cfqq->cfqg);
1131 cfq_put_cfqg(cfqq->cfqg);
1132 cfqq->cfqg = cfqq->orig_cfqg;
1133 cfqq->orig_cfqg = NULL;
1134 group_changed = 1;
1135 cfq_log_cfqq(cfqd, cfqq, "moved to origin group");
1137 #endif
1139 service_tree = service_tree_for(cfqq->cfqg, cfqq_prio(cfqq),
1140 cfqq_type(cfqq));
1141 if (cfq_class_idle(cfqq)) {
1142 rb_key = CFQ_IDLE_DELAY;
1143 parent = rb_last(&service_tree->rb);
1144 if (parent && parent != &cfqq->rb_node) {
1145 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1146 rb_key += __cfqq->rb_key;
1147 } else
1148 rb_key += jiffies;
1149 } else if (!add_front) {
1151 * Get our rb key offset. Subtract any residual slice
1152 * value carried from last service. A negative resid
1153 * count indicates slice overrun, and this should position
1154 * the next service time further away in the tree.
1156 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
1157 rb_key -= cfqq->slice_resid;
1158 cfqq->slice_resid = 0;
1159 } else {
1160 rb_key = -HZ;
1161 __cfqq = cfq_rb_first(service_tree);
1162 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
1165 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
1166 new_cfqq = 0;
1168 * same position, nothing more to do
1170 if (rb_key == cfqq->rb_key &&
1171 cfqq->service_tree == service_tree)
1172 return;
1174 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1175 cfqq->service_tree = NULL;
1178 left = 1;
1179 parent = NULL;
1180 cfqq->service_tree = service_tree;
1181 p = &service_tree->rb.rb_node;
1182 while (*p) {
1183 struct rb_node **n;
1185 parent = *p;
1186 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1189 * sort by key, that represents service time.
1191 if (time_before(rb_key, __cfqq->rb_key))
1192 n = &(*p)->rb_left;
1193 else {
1194 n = &(*p)->rb_right;
1195 left = 0;
1198 p = n;
1201 if (left)
1202 service_tree->left = &cfqq->rb_node;
1204 cfqq->rb_key = rb_key;
1205 rb_link_node(&cfqq->rb_node, parent, p);
1206 rb_insert_color(&cfqq->rb_node, &service_tree->rb);
1207 service_tree->count++;
1208 if ((add_front || !new_cfqq) && !group_changed)
1209 return;
1210 cfq_group_service_tree_add(cfqd, cfqq->cfqg);
1213 static struct cfq_queue *
1214 cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
1215 sector_t sector, struct rb_node **ret_parent,
1216 struct rb_node ***rb_link)
1218 struct rb_node **p, *parent;
1219 struct cfq_queue *cfqq = NULL;
1221 parent = NULL;
1222 p = &root->rb_node;
1223 while (*p) {
1224 struct rb_node **n;
1226 parent = *p;
1227 cfqq = rb_entry(parent, struct cfq_queue, p_node);
1230 * Sort strictly based on sector. Smallest to the left,
1231 * largest to the right.
1233 if (sector > blk_rq_pos(cfqq->next_rq))
1234 n = &(*p)->rb_right;
1235 else if (sector < blk_rq_pos(cfqq->next_rq))
1236 n = &(*p)->rb_left;
1237 else
1238 break;
1239 p = n;
1240 cfqq = NULL;
1243 *ret_parent = parent;
1244 if (rb_link)
1245 *rb_link = p;
1246 return cfqq;
1249 static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1251 struct rb_node **p, *parent;
1252 struct cfq_queue *__cfqq;
1254 if (cfqq->p_root) {
1255 rb_erase(&cfqq->p_node, cfqq->p_root);
1256 cfqq->p_root = NULL;
1259 if (cfq_class_idle(cfqq))
1260 return;
1261 if (!cfqq->next_rq)
1262 return;
1264 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
1265 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
1266 blk_rq_pos(cfqq->next_rq), &parent, &p);
1267 if (!__cfqq) {
1268 rb_link_node(&cfqq->p_node, parent, p);
1269 rb_insert_color(&cfqq->p_node, cfqq->p_root);
1270 } else
1271 cfqq->p_root = NULL;
1275 * Update cfqq's position in the service tree.
1277 static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1280 * Resorting requires the cfqq to be on the RR list already.
1282 if (cfq_cfqq_on_rr(cfqq)) {
1283 cfq_service_tree_add(cfqd, cfqq, 0);
1284 cfq_prio_tree_add(cfqd, cfqq);
1289 * add to busy list of queues for service, trying to be fair in ordering
1290 * the pending list according to last request service
1292 static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1294 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
1295 BUG_ON(cfq_cfqq_on_rr(cfqq));
1296 cfq_mark_cfqq_on_rr(cfqq);
1297 cfqd->busy_queues++;
1299 cfq_resort_rr_list(cfqd, cfqq);
1303 * Called when the cfqq no longer has requests pending, remove it from
1304 * the service tree.
1306 static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1308 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
1309 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1310 cfq_clear_cfqq_on_rr(cfqq);
1312 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
1313 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1314 cfqq->service_tree = NULL;
1316 if (cfqq->p_root) {
1317 rb_erase(&cfqq->p_node, cfqq->p_root);
1318 cfqq->p_root = NULL;
1321 cfq_group_service_tree_del(cfqd, cfqq->cfqg);
1322 BUG_ON(!cfqd->busy_queues);
1323 cfqd->busy_queues--;
1327 * rb tree support functions
1329 static void cfq_del_rq_rb(struct request *rq)
1331 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1332 const int sync = rq_is_sync(rq);
1334 BUG_ON(!cfqq->queued[sync]);
1335 cfqq->queued[sync]--;
1337 elv_rb_del(&cfqq->sort_list, rq);
1339 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
1341 * Queue will be deleted from service tree when we actually
1342 * expire it later. Right now just remove it from prio tree
1343 * as it is empty.
1345 if (cfqq->p_root) {
1346 rb_erase(&cfqq->p_node, cfqq->p_root);
1347 cfqq->p_root = NULL;
1352 static void cfq_add_rq_rb(struct request *rq)
1354 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1355 struct cfq_data *cfqd = cfqq->cfqd;
1356 struct request *__alias, *prev;
1358 cfqq->queued[rq_is_sync(rq)]++;
1361 * looks a little odd, but the first insert might return an alias.
1362 * if that happens, put the alias on the dispatch list
1364 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
1365 cfq_dispatch_insert(cfqd->queue, __alias);
1367 if (!cfq_cfqq_on_rr(cfqq))
1368 cfq_add_cfqq_rr(cfqd, cfqq);
1371 * check if this request is a better next-serve candidate
1373 prev = cfqq->next_rq;
1374 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
1377 * adjust priority tree position, if ->next_rq changes
1379 if (prev != cfqq->next_rq)
1380 cfq_prio_tree_add(cfqd, cfqq);
1382 BUG_ON(!cfqq->next_rq);
1385 static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
1387 elv_rb_del(&cfqq->sort_list, rq);
1388 cfqq->queued[rq_is_sync(rq)]--;
1389 cfq_add_rq_rb(rq);
1392 static struct request *
1393 cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
1395 struct task_struct *tsk = current;
1396 struct cfq_io_context *cic;
1397 struct cfq_queue *cfqq;
1399 cic = cfq_cic_lookup(cfqd, tsk->io_context);
1400 if (!cic)
1401 return NULL;
1403 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
1404 if (cfqq) {
1405 sector_t sector = bio->bi_sector + bio_sectors(bio);
1407 return elv_rb_find(&cfqq->sort_list, sector);
1410 return NULL;
1413 static void cfq_activate_request(struct request_queue *q, struct request *rq)
1415 struct cfq_data *cfqd = q->elevator->elevator_data;
1417 cfqd->rq_in_driver[rq_is_sync(rq)]++;
1418 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
1419 rq_in_driver(cfqd));
1421 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
1424 static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
1426 struct cfq_data *cfqd = q->elevator->elevator_data;
1427 const int sync = rq_is_sync(rq);
1429 WARN_ON(!cfqd->rq_in_driver[sync]);
1430 cfqd->rq_in_driver[sync]--;
1431 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
1432 rq_in_driver(cfqd));
1435 static void cfq_remove_request(struct request *rq)
1437 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1439 if (cfqq->next_rq == rq)
1440 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
1442 list_del_init(&rq->queuelist);
1443 cfq_del_rq_rb(rq);
1445 cfqq->cfqd->rq_queued--;
1446 if (rq_is_meta(rq)) {
1447 WARN_ON(!cfqq->meta_pending);
1448 cfqq->meta_pending--;
1452 static int cfq_merge(struct request_queue *q, struct request **req,
1453 struct bio *bio)
1455 struct cfq_data *cfqd = q->elevator->elevator_data;
1456 struct request *__rq;
1458 __rq = cfq_find_rq_fmerge(cfqd, bio);
1459 if (__rq && elv_rq_merge_ok(__rq, bio)) {
1460 *req = __rq;
1461 return ELEVATOR_FRONT_MERGE;
1464 return ELEVATOR_NO_MERGE;
1467 static void cfq_merged_request(struct request_queue *q, struct request *req,
1468 int type)
1470 if (type == ELEVATOR_FRONT_MERGE) {
1471 struct cfq_queue *cfqq = RQ_CFQQ(req);
1473 cfq_reposition_rq_rb(cfqq, req);
1477 static void
1478 cfq_merged_requests(struct request_queue *q, struct request *rq,
1479 struct request *next)
1481 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1483 * reposition in fifo if next is older than rq
1485 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
1486 time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
1487 list_move(&rq->queuelist, &next->queuelist);
1488 rq_set_fifo_time(rq, rq_fifo_time(next));
1491 if (cfqq->next_rq == next)
1492 cfqq->next_rq = rq;
1493 cfq_remove_request(next);
1496 static int cfq_allow_merge(struct request_queue *q, struct request *rq,
1497 struct bio *bio)
1499 struct cfq_data *cfqd = q->elevator->elevator_data;
1500 struct cfq_io_context *cic;
1501 struct cfq_queue *cfqq;
1504 * Disallow merge of a sync bio into an async request.
1506 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
1507 return false;
1510 * Lookup the cfqq that this bio will be queued with. Allow
1511 * merge only if rq is queued there.
1513 cic = cfq_cic_lookup(cfqd, current->io_context);
1514 if (!cic)
1515 return false;
1517 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
1518 return cfqq == RQ_CFQQ(rq);
1521 static void __cfq_set_active_queue(struct cfq_data *cfqd,
1522 struct cfq_queue *cfqq)
1524 if (cfqq) {
1525 cfq_log_cfqq(cfqd, cfqq, "set_active");
1526 cfqq->slice_start = 0;
1527 cfqq->dispatch_start = jiffies;
1528 cfqq->allocated_slice = 0;
1529 cfqq->slice_end = 0;
1530 cfqq->slice_dispatch = 0;
1531 cfqq->nr_sectors = 0;
1533 cfq_clear_cfqq_wait_request(cfqq);
1534 cfq_clear_cfqq_must_dispatch(cfqq);
1535 cfq_clear_cfqq_must_alloc_slice(cfqq);
1536 cfq_clear_cfqq_fifo_expire(cfqq);
1537 cfq_mark_cfqq_slice_new(cfqq);
1539 del_timer(&cfqd->idle_slice_timer);
1542 cfqd->active_queue = cfqq;
1546 * current cfqq expired its slice (or was too idle), select new one
1548 static void
1549 __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1550 bool timed_out)
1552 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
1554 if (cfq_cfqq_wait_request(cfqq))
1555 del_timer(&cfqd->idle_slice_timer);
1557 cfq_clear_cfqq_wait_request(cfqq);
1558 cfq_clear_cfqq_wait_busy(cfqq);
1561 * If this cfqq is shared between multiple processes, check to
1562 * make sure that those processes are still issuing I/Os within
1563 * the mean seek distance. If not, it may be time to break the
1564 * queues apart again.
1566 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
1567 cfq_mark_cfqq_split_coop(cfqq);
1570 * store what was left of this slice, if the queue idled/timed out
1572 if (timed_out && !cfq_cfqq_slice_new(cfqq)) {
1573 cfqq->slice_resid = cfqq->slice_end - jiffies;
1574 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
1577 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
1579 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
1580 cfq_del_cfqq_rr(cfqd, cfqq);
1582 cfq_resort_rr_list(cfqd, cfqq);
1584 if (cfqq == cfqd->active_queue)
1585 cfqd->active_queue = NULL;
1587 if (&cfqq->cfqg->rb_node == cfqd->grp_service_tree.active)
1588 cfqd->grp_service_tree.active = NULL;
1590 if (cfqd->active_cic) {
1591 put_io_context(cfqd->active_cic->ioc);
1592 cfqd->active_cic = NULL;
1596 static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
1598 struct cfq_queue *cfqq = cfqd->active_queue;
1600 if (cfqq)
1601 __cfq_slice_expired(cfqd, cfqq, timed_out);
1605 * Get next queue for service. Unless we have a queue preemption,
1606 * we'll simply select the first cfqq in the service tree.
1608 static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
1610 struct cfq_rb_root *service_tree =
1611 service_tree_for(cfqd->serving_group, cfqd->serving_prio,
1612 cfqd->serving_type);
1614 if (!cfqd->rq_queued)
1615 return NULL;
1617 /* There is nothing to dispatch */
1618 if (!service_tree)
1619 return NULL;
1620 if (RB_EMPTY_ROOT(&service_tree->rb))
1621 return NULL;
1622 return cfq_rb_first(service_tree);
1625 static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
1627 struct cfq_group *cfqg;
1628 struct cfq_queue *cfqq;
1629 int i, j;
1630 struct cfq_rb_root *st;
1632 if (!cfqd->rq_queued)
1633 return NULL;
1635 cfqg = cfq_get_next_cfqg(cfqd);
1636 if (!cfqg)
1637 return NULL;
1639 for_each_cfqg_st(cfqg, i, j, st)
1640 if ((cfqq = cfq_rb_first(st)) != NULL)
1641 return cfqq;
1642 return NULL;
1646 * Get and set a new active queue for service.
1648 static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
1649 struct cfq_queue *cfqq)
1651 if (!cfqq)
1652 cfqq = cfq_get_next_queue(cfqd);
1654 __cfq_set_active_queue(cfqd, cfqq);
1655 return cfqq;
1658 static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
1659 struct request *rq)
1661 if (blk_rq_pos(rq) >= cfqd->last_position)
1662 return blk_rq_pos(rq) - cfqd->last_position;
1663 else
1664 return cfqd->last_position - blk_rq_pos(rq);
1667 static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1668 struct request *rq, bool for_preempt)
1670 return cfq_dist_from_last(cfqd, rq) <= CFQQ_SEEK_THR;
1673 static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
1674 struct cfq_queue *cur_cfqq)
1676 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
1677 struct rb_node *parent, *node;
1678 struct cfq_queue *__cfqq;
1679 sector_t sector = cfqd->last_position;
1681 if (RB_EMPTY_ROOT(root))
1682 return NULL;
1685 * First, if we find a request starting at the end of the last
1686 * request, choose it.
1688 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
1689 if (__cfqq)
1690 return __cfqq;
1693 * If the exact sector wasn't found, the parent of the NULL leaf
1694 * will contain the closest sector.
1696 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
1697 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq, false))
1698 return __cfqq;
1700 if (blk_rq_pos(__cfqq->next_rq) < sector)
1701 node = rb_next(&__cfqq->p_node);
1702 else
1703 node = rb_prev(&__cfqq->p_node);
1704 if (!node)
1705 return NULL;
1707 __cfqq = rb_entry(node, struct cfq_queue, p_node);
1708 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq, false))
1709 return __cfqq;
1711 return NULL;
1715 * cfqd - obvious
1716 * cur_cfqq - passed in so that we don't decide that the current queue is
1717 * closely cooperating with itself.
1719 * So, basically we're assuming that that cur_cfqq has dispatched at least
1720 * one request, and that cfqd->last_position reflects a position on the disk
1721 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
1722 * assumption.
1724 static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
1725 struct cfq_queue *cur_cfqq)
1727 struct cfq_queue *cfqq;
1729 if (!cfq_cfqq_sync(cur_cfqq))
1730 return NULL;
1731 if (CFQQ_SEEKY(cur_cfqq))
1732 return NULL;
1735 * Don't search priority tree if it's the only queue in the group.
1737 if (cur_cfqq->cfqg->nr_cfqq == 1)
1738 return NULL;
1741 * We should notice if some of the queues are cooperating, eg
1742 * working closely on the same area of the disk. In that case,
1743 * we can group them together and don't waste time idling.
1745 cfqq = cfqq_close(cfqd, cur_cfqq);
1746 if (!cfqq)
1747 return NULL;
1749 /* If new queue belongs to different cfq_group, don't choose it */
1750 if (cur_cfqq->cfqg != cfqq->cfqg)
1751 return NULL;
1754 * It only makes sense to merge sync queues.
1756 if (!cfq_cfqq_sync(cfqq))
1757 return NULL;
1758 if (CFQQ_SEEKY(cfqq))
1759 return NULL;
1762 * Do not merge queues of different priority classes
1764 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
1765 return NULL;
1767 return cfqq;
1771 * Determine whether we should enforce idle window for this queue.
1774 static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1776 enum wl_prio_t prio = cfqq_prio(cfqq);
1777 struct cfq_rb_root *service_tree = cfqq->service_tree;
1779 BUG_ON(!service_tree);
1780 BUG_ON(!service_tree->count);
1782 /* We never do for idle class queues. */
1783 if (prio == IDLE_WORKLOAD)
1784 return false;
1786 /* We do for queues that were marked with idle window flag. */
1787 if (cfq_cfqq_idle_window(cfqq) &&
1788 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
1789 return true;
1792 * Otherwise, we do only if they are the last ones
1793 * in their service tree.
1795 return service_tree->count == 1 && cfq_cfqq_sync(cfqq);
1798 static void cfq_arm_slice_timer(struct cfq_data *cfqd)
1800 struct cfq_queue *cfqq = cfqd->active_queue;
1801 struct cfq_io_context *cic;
1802 unsigned long sl;
1805 * SSD device without seek penalty, disable idling. But only do so
1806 * for devices that support queuing, otherwise we still have a problem
1807 * with sync vs async workloads.
1809 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
1810 return;
1812 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
1813 WARN_ON(cfq_cfqq_slice_new(cfqq));
1816 * idle is disabled, either manually or by past process history
1818 if (!cfqd->cfq_slice_idle || !cfq_should_idle(cfqd, cfqq))
1819 return;
1822 * still active requests from this queue, don't idle
1824 if (cfqq->dispatched)
1825 return;
1828 * task has exited, don't wait
1830 cic = cfqd->active_cic;
1831 if (!cic || !atomic_read(&cic->ioc->nr_tasks))
1832 return;
1835 * If our average think time is larger than the remaining time
1836 * slice, then don't idle. This avoids overrunning the allotted
1837 * time slice.
1839 if (sample_valid(cic->ttime_samples) &&
1840 (cfqq->slice_end - jiffies < cic->ttime_mean))
1841 return;
1843 cfq_mark_cfqq_wait_request(cfqq);
1845 sl = cfqd->cfq_slice_idle;
1847 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
1848 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu", sl);
1852 * Move request from internal lists to the request queue dispatch list.
1854 static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
1856 struct cfq_data *cfqd = q->elevator->elevator_data;
1857 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1859 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
1861 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
1862 cfq_remove_request(rq);
1863 cfqq->dispatched++;
1864 elv_dispatch_sort(q, rq);
1866 if (cfq_cfqq_sync(cfqq))
1867 cfqd->sync_flight++;
1868 cfqq->nr_sectors += blk_rq_sectors(rq);
1872 * return expired entry, or NULL to just start from scratch in rbtree
1874 static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
1876 struct request *rq = NULL;
1878 if (cfq_cfqq_fifo_expire(cfqq))
1879 return NULL;
1881 cfq_mark_cfqq_fifo_expire(cfqq);
1883 if (list_empty(&cfqq->fifo))
1884 return NULL;
1886 rq = rq_entry_fifo(cfqq->fifo.next);
1887 if (time_before(jiffies, rq_fifo_time(rq)))
1888 rq = NULL;
1890 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
1891 return rq;
1894 static inline int
1895 cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1897 const int base_rq = cfqd->cfq_slice_async_rq;
1899 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1901 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1905 * Must be called with the queue_lock held.
1907 static int cfqq_process_refs(struct cfq_queue *cfqq)
1909 int process_refs, io_refs;
1911 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
1912 process_refs = atomic_read(&cfqq->ref) - io_refs;
1913 BUG_ON(process_refs < 0);
1914 return process_refs;
1917 static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
1919 int process_refs, new_process_refs;
1920 struct cfq_queue *__cfqq;
1922 /* Avoid a circular list and skip interim queue merges */
1923 while ((__cfqq = new_cfqq->new_cfqq)) {
1924 if (__cfqq == cfqq)
1925 return;
1926 new_cfqq = __cfqq;
1929 process_refs = cfqq_process_refs(cfqq);
1931 * If the process for the cfqq has gone away, there is no
1932 * sense in merging the queues.
1934 if (process_refs == 0)
1935 return;
1938 * Merge in the direction of the lesser amount of work.
1940 new_process_refs = cfqq_process_refs(new_cfqq);
1941 if (new_process_refs >= process_refs) {
1942 cfqq->new_cfqq = new_cfqq;
1943 atomic_add(process_refs, &new_cfqq->ref);
1944 } else {
1945 new_cfqq->new_cfqq = cfqq;
1946 atomic_add(new_process_refs, &cfqq->ref);
1950 static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd,
1951 struct cfq_group *cfqg, enum wl_prio_t prio)
1953 struct cfq_queue *queue;
1954 int i;
1955 bool key_valid = false;
1956 unsigned long lowest_key = 0;
1957 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
1959 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
1960 /* select the one with lowest rb_key */
1961 queue = cfq_rb_first(service_tree_for(cfqg, prio, i));
1962 if (queue &&
1963 (!key_valid || time_before(queue->rb_key, lowest_key))) {
1964 lowest_key = queue->rb_key;
1965 cur_best = i;
1966 key_valid = true;
1970 return cur_best;
1973 static void choose_service_tree(struct cfq_data *cfqd, struct cfq_group *cfqg)
1975 unsigned slice;
1976 unsigned count;
1977 struct cfq_rb_root *st;
1978 unsigned group_slice;
1980 if (!cfqg) {
1981 cfqd->serving_prio = IDLE_WORKLOAD;
1982 cfqd->workload_expires = jiffies + 1;
1983 return;
1986 /* Choose next priority. RT > BE > IDLE */
1987 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
1988 cfqd->serving_prio = RT_WORKLOAD;
1989 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
1990 cfqd->serving_prio = BE_WORKLOAD;
1991 else {
1992 cfqd->serving_prio = IDLE_WORKLOAD;
1993 cfqd->workload_expires = jiffies + 1;
1994 return;
1998 * For RT and BE, we have to choose also the type
1999 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
2000 * expiration time
2002 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
2003 count = st->count;
2006 * check workload expiration, and that we still have other queues ready
2008 if (count && !time_after(jiffies, cfqd->workload_expires))
2009 return;
2011 /* otherwise select new workload type */
2012 cfqd->serving_type =
2013 cfq_choose_wl(cfqd, cfqg, cfqd->serving_prio);
2014 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
2015 count = st->count;
2018 * the workload slice is computed as a fraction of target latency
2019 * proportional to the number of queues in that workload, over
2020 * all the queues in the same priority class
2022 group_slice = cfq_group_slice(cfqd, cfqg);
2024 slice = group_slice * count /
2025 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio],
2026 cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd, cfqg));
2028 if (cfqd->serving_type == ASYNC_WORKLOAD) {
2029 unsigned int tmp;
2032 * Async queues are currently system wide. Just taking
2033 * proportion of queues with-in same group will lead to higher
2034 * async ratio system wide as generally root group is going
2035 * to have higher weight. A more accurate thing would be to
2036 * calculate system wide asnc/sync ratio.
2038 tmp = cfq_target_latency * cfqg_busy_async_queues(cfqd, cfqg);
2039 tmp = tmp/cfqd->busy_queues;
2040 slice = min_t(unsigned, slice, tmp);
2042 /* async workload slice is scaled down according to
2043 * the sync/async slice ratio. */
2044 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
2045 } else
2046 /* sync workload slice is at least 2 * cfq_slice_idle */
2047 slice = max(slice, 2 * cfqd->cfq_slice_idle);
2049 slice = max_t(unsigned, slice, CFQ_MIN_TT);
2050 cfqd->workload_expires = jiffies + slice;
2051 cfqd->noidle_tree_requires_idle = false;
2054 static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
2056 struct cfq_rb_root *st = &cfqd->grp_service_tree;
2057 struct cfq_group *cfqg;
2059 if (RB_EMPTY_ROOT(&st->rb))
2060 return NULL;
2061 cfqg = cfq_rb_first_group(st);
2062 st->active = &cfqg->rb_node;
2063 update_min_vdisktime(st);
2064 return cfqg;
2067 static void cfq_choose_cfqg(struct cfq_data *cfqd)
2069 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
2071 cfqd->serving_group = cfqg;
2073 /* Restore the workload type data */
2074 if (cfqg->saved_workload_slice) {
2075 cfqd->workload_expires = jiffies + cfqg->saved_workload_slice;
2076 cfqd->serving_type = cfqg->saved_workload;
2077 cfqd->serving_prio = cfqg->saved_serving_prio;
2078 } else
2079 cfqd->workload_expires = jiffies - 1;
2081 choose_service_tree(cfqd, cfqg);
2085 * Select a queue for service. If we have a current active queue,
2086 * check whether to continue servicing it, or retrieve and set a new one.
2088 static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
2090 struct cfq_queue *cfqq, *new_cfqq = NULL;
2092 cfqq = cfqd->active_queue;
2093 if (!cfqq)
2094 goto new_queue;
2096 if (!cfqd->rq_queued)
2097 return NULL;
2100 * We were waiting for group to get backlogged. Expire the queue
2102 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
2103 goto expire;
2106 * The active queue has run out of time, expire it and select new.
2108 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
2110 * If slice had not expired at the completion of last request
2111 * we might not have turned on wait_busy flag. Don't expire
2112 * the queue yet. Allow the group to get backlogged.
2114 * The very fact that we have used the slice, that means we
2115 * have been idling all along on this queue and it should be
2116 * ok to wait for this request to complete.
2118 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
2119 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2120 cfqq = NULL;
2121 goto keep_queue;
2122 } else
2123 goto expire;
2127 * The active queue has requests and isn't expired, allow it to
2128 * dispatch.
2130 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
2131 goto keep_queue;
2134 * If another queue has a request waiting within our mean seek
2135 * distance, let it run. The expire code will check for close
2136 * cooperators and put the close queue at the front of the service
2137 * tree. If possible, merge the expiring queue with the new cfqq.
2139 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
2140 if (new_cfqq) {
2141 if (!cfqq->new_cfqq)
2142 cfq_setup_merge(cfqq, new_cfqq);
2143 goto expire;
2147 * No requests pending. If the active queue still has requests in
2148 * flight or is idling for a new request, allow either of these
2149 * conditions to happen (or time out) before selecting a new queue.
2151 if (timer_pending(&cfqd->idle_slice_timer) ||
2152 (cfqq->dispatched && cfq_should_idle(cfqd, cfqq))) {
2153 cfqq = NULL;
2154 goto keep_queue;
2157 expire:
2158 cfq_slice_expired(cfqd, 0);
2159 new_queue:
2161 * Current queue expired. Check if we have to switch to a new
2162 * service tree
2164 if (!new_cfqq)
2165 cfq_choose_cfqg(cfqd);
2167 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
2168 keep_queue:
2169 return cfqq;
2172 static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
2174 int dispatched = 0;
2176 while (cfqq->next_rq) {
2177 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
2178 dispatched++;
2181 BUG_ON(!list_empty(&cfqq->fifo));
2183 /* By default cfqq is not expired if it is empty. Do it explicitly */
2184 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
2185 return dispatched;
2189 * Drain our current requests. Used for barriers and when switching
2190 * io schedulers on-the-fly.
2192 static int cfq_forced_dispatch(struct cfq_data *cfqd)
2194 struct cfq_queue *cfqq;
2195 int dispatched = 0;
2197 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL)
2198 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
2200 cfq_slice_expired(cfqd, 0);
2201 BUG_ON(cfqd->busy_queues);
2203 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
2204 return dispatched;
2207 static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2209 unsigned int max_dispatch;
2212 * Drain async requests before we start sync IO
2214 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_driver[BLK_RW_ASYNC])
2215 return false;
2218 * If this is an async queue and we have sync IO in flight, let it wait
2220 if (cfqd->sync_flight && !cfq_cfqq_sync(cfqq))
2221 return false;
2223 max_dispatch = cfqd->cfq_quantum;
2224 if (cfq_class_idle(cfqq))
2225 max_dispatch = 1;
2228 * Does this cfqq already have too much IO in flight?
2230 if (cfqq->dispatched >= max_dispatch) {
2232 * idle queue must always only have a single IO in flight
2234 if (cfq_class_idle(cfqq))
2235 return false;
2238 * We have other queues, don't allow more IO from this one
2240 if (cfqd->busy_queues > 1)
2241 return false;
2244 * Sole queue user, no limit
2246 max_dispatch = -1;
2250 * Async queues must wait a bit before being allowed dispatch.
2251 * We also ramp up the dispatch depth gradually for async IO,
2252 * based on the last sync IO we serviced
2254 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
2255 unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
2256 unsigned int depth;
2258 depth = last_sync / cfqd->cfq_slice[1];
2259 if (!depth && !cfqq->dispatched)
2260 depth = 1;
2261 if (depth < max_dispatch)
2262 max_dispatch = depth;
2266 * If we're below the current max, allow a dispatch
2268 return cfqq->dispatched < max_dispatch;
2272 * Dispatch a request from cfqq, moving them to the request queue
2273 * dispatch list.
2275 static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2277 struct request *rq;
2279 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
2281 if (!cfq_may_dispatch(cfqd, cfqq))
2282 return false;
2285 * follow expired path, else get first next available
2287 rq = cfq_check_fifo(cfqq);
2288 if (!rq)
2289 rq = cfqq->next_rq;
2292 * insert request into driver dispatch list
2294 cfq_dispatch_insert(cfqd->queue, rq);
2296 if (!cfqd->active_cic) {
2297 struct cfq_io_context *cic = RQ_CIC(rq);
2299 atomic_long_inc(&cic->ioc->refcount);
2300 cfqd->active_cic = cic;
2303 return true;
2307 * Find the cfqq that we need to service and move a request from that to the
2308 * dispatch list
2310 static int cfq_dispatch_requests(struct request_queue *q, int force)
2312 struct cfq_data *cfqd = q->elevator->elevator_data;
2313 struct cfq_queue *cfqq;
2315 if (!cfqd->busy_queues)
2316 return 0;
2318 if (unlikely(force))
2319 return cfq_forced_dispatch(cfqd);
2321 cfqq = cfq_select_queue(cfqd);
2322 if (!cfqq)
2323 return 0;
2326 * Dispatch a request from this cfqq, if it is allowed
2328 if (!cfq_dispatch_request(cfqd, cfqq))
2329 return 0;
2331 cfqq->slice_dispatch++;
2332 cfq_clear_cfqq_must_dispatch(cfqq);
2335 * expire an async queue immediately if it has used up its slice. idle
2336 * queue always expire after 1 dispatch round.
2338 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
2339 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
2340 cfq_class_idle(cfqq))) {
2341 cfqq->slice_end = jiffies + 1;
2342 cfq_slice_expired(cfqd, 0);
2345 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
2346 return 1;
2350 * task holds one reference to the queue, dropped when task exits. each rq
2351 * in-flight on this queue also holds a reference, dropped when rq is freed.
2353 * Each cfq queue took a reference on the parent group. Drop it now.
2354 * queue lock must be held here.
2356 static void cfq_put_queue(struct cfq_queue *cfqq)
2358 struct cfq_data *cfqd = cfqq->cfqd;
2359 struct cfq_group *cfqg, *orig_cfqg;
2361 BUG_ON(atomic_read(&cfqq->ref) <= 0);
2363 if (!atomic_dec_and_test(&cfqq->ref))
2364 return;
2366 cfq_log_cfqq(cfqd, cfqq, "put_queue");
2367 BUG_ON(rb_first(&cfqq->sort_list));
2368 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
2369 cfqg = cfqq->cfqg;
2370 orig_cfqg = cfqq->orig_cfqg;
2372 if (unlikely(cfqd->active_queue == cfqq)) {
2373 __cfq_slice_expired(cfqd, cfqq, 0);
2374 cfq_schedule_dispatch(cfqd);
2377 BUG_ON(cfq_cfqq_on_rr(cfqq));
2378 kmem_cache_free(cfq_pool, cfqq);
2379 cfq_put_cfqg(cfqg);
2380 if (orig_cfqg)
2381 cfq_put_cfqg(orig_cfqg);
2385 * Must always be called with the rcu_read_lock() held
2387 static void
2388 __call_for_each_cic(struct io_context *ioc,
2389 void (*func)(struct io_context *, struct cfq_io_context *))
2391 struct cfq_io_context *cic;
2392 struct hlist_node *n;
2394 hlist_for_each_entry_rcu(cic, n, &ioc->cic_list, cic_list)
2395 func(ioc, cic);
2399 * Call func for each cic attached to this ioc.
2401 static void
2402 call_for_each_cic(struct io_context *ioc,
2403 void (*func)(struct io_context *, struct cfq_io_context *))
2405 rcu_read_lock();
2406 __call_for_each_cic(ioc, func);
2407 rcu_read_unlock();
2410 static void cfq_cic_free_rcu(struct rcu_head *head)
2412 struct cfq_io_context *cic;
2414 cic = container_of(head, struct cfq_io_context, rcu_head);
2416 kmem_cache_free(cfq_ioc_pool, cic);
2417 elv_ioc_count_dec(cfq_ioc_count);
2419 if (ioc_gone) {
2421 * CFQ scheduler is exiting, grab exit lock and check
2422 * the pending io context count. If it hits zero,
2423 * complete ioc_gone and set it back to NULL
2425 spin_lock(&ioc_gone_lock);
2426 if (ioc_gone && !elv_ioc_count_read(cfq_ioc_count)) {
2427 complete(ioc_gone);
2428 ioc_gone = NULL;
2430 spin_unlock(&ioc_gone_lock);
2434 static void cfq_cic_free(struct cfq_io_context *cic)
2436 call_rcu(&cic->rcu_head, cfq_cic_free_rcu);
2439 static void cic_free_func(struct io_context *ioc, struct cfq_io_context *cic)
2441 unsigned long flags;
2443 BUG_ON(!cic->dead_key);
2445 spin_lock_irqsave(&ioc->lock, flags);
2446 radix_tree_delete(&ioc->radix_root, cic->dead_key);
2447 hlist_del_rcu(&cic->cic_list);
2448 spin_unlock_irqrestore(&ioc->lock, flags);
2450 cfq_cic_free(cic);
2454 * Must be called with rcu_read_lock() held or preemption otherwise disabled.
2455 * Only two callers of this - ->dtor() which is called with the rcu_read_lock(),
2456 * and ->trim() which is called with the task lock held
2458 static void cfq_free_io_context(struct io_context *ioc)
2461 * ioc->refcount is zero here, or we are called from elv_unregister(),
2462 * so no more cic's are allowed to be linked into this ioc. So it
2463 * should be ok to iterate over the known list, we will see all cic's
2464 * since no new ones are added.
2466 __call_for_each_cic(ioc, cic_free_func);
2469 static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2471 struct cfq_queue *__cfqq, *next;
2473 if (unlikely(cfqq == cfqd->active_queue)) {
2474 __cfq_slice_expired(cfqd, cfqq, 0);
2475 cfq_schedule_dispatch(cfqd);
2479 * If this queue was scheduled to merge with another queue, be
2480 * sure to drop the reference taken on that queue (and others in
2481 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
2483 __cfqq = cfqq->new_cfqq;
2484 while (__cfqq) {
2485 if (__cfqq == cfqq) {
2486 WARN(1, "cfqq->new_cfqq loop detected\n");
2487 break;
2489 next = __cfqq->new_cfqq;
2490 cfq_put_queue(__cfqq);
2491 __cfqq = next;
2494 cfq_put_queue(cfqq);
2497 static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
2498 struct cfq_io_context *cic)
2500 struct io_context *ioc = cic->ioc;
2502 list_del_init(&cic->queue_list);
2505 * Make sure key == NULL is seen for dead queues
2507 smp_wmb();
2508 cic->dead_key = (unsigned long) cic->key;
2509 cic->key = NULL;
2511 if (ioc->ioc_data == cic)
2512 rcu_assign_pointer(ioc->ioc_data, NULL);
2514 if (cic->cfqq[BLK_RW_ASYNC]) {
2515 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
2516 cic->cfqq[BLK_RW_ASYNC] = NULL;
2519 if (cic->cfqq[BLK_RW_SYNC]) {
2520 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
2521 cic->cfqq[BLK_RW_SYNC] = NULL;
2525 static void cfq_exit_single_io_context(struct io_context *ioc,
2526 struct cfq_io_context *cic)
2528 struct cfq_data *cfqd = cic->key;
2530 if (cfqd) {
2531 struct request_queue *q = cfqd->queue;
2532 unsigned long flags;
2534 spin_lock_irqsave(q->queue_lock, flags);
2537 * Ensure we get a fresh copy of the ->key to prevent
2538 * race between exiting task and queue
2540 smp_read_barrier_depends();
2541 if (cic->key)
2542 __cfq_exit_single_io_context(cfqd, cic);
2544 spin_unlock_irqrestore(q->queue_lock, flags);
2549 * The process that ioc belongs to has exited, we need to clean up
2550 * and put the internal structures we have that belongs to that process.
2552 static void cfq_exit_io_context(struct io_context *ioc)
2554 call_for_each_cic(ioc, cfq_exit_single_io_context);
2557 static struct cfq_io_context *
2558 cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
2560 struct cfq_io_context *cic;
2562 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask | __GFP_ZERO,
2563 cfqd->queue->node);
2564 if (cic) {
2565 cic->last_end_request = jiffies;
2566 INIT_LIST_HEAD(&cic->queue_list);
2567 INIT_HLIST_NODE(&cic->cic_list);
2568 cic->dtor = cfq_free_io_context;
2569 cic->exit = cfq_exit_io_context;
2570 elv_ioc_count_inc(cfq_ioc_count);
2573 return cic;
2576 static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc)
2578 struct task_struct *tsk = current;
2579 int ioprio_class;
2581 if (!cfq_cfqq_prio_changed(cfqq))
2582 return;
2584 ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio);
2585 switch (ioprio_class) {
2586 default:
2587 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
2588 case IOPRIO_CLASS_NONE:
2590 * no prio set, inherit CPU scheduling settings
2592 cfqq->ioprio = task_nice_ioprio(tsk);
2593 cfqq->ioprio_class = task_nice_ioclass(tsk);
2594 break;
2595 case IOPRIO_CLASS_RT:
2596 cfqq->ioprio = task_ioprio(ioc);
2597 cfqq->ioprio_class = IOPRIO_CLASS_RT;
2598 break;
2599 case IOPRIO_CLASS_BE:
2600 cfqq->ioprio = task_ioprio(ioc);
2601 cfqq->ioprio_class = IOPRIO_CLASS_BE;
2602 break;
2603 case IOPRIO_CLASS_IDLE:
2604 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
2605 cfqq->ioprio = 7;
2606 cfq_clear_cfqq_idle_window(cfqq);
2607 break;
2611 * keep track of original prio settings in case we have to temporarily
2612 * elevate the priority of this queue
2614 cfqq->org_ioprio = cfqq->ioprio;
2615 cfqq->org_ioprio_class = cfqq->ioprio_class;
2616 cfq_clear_cfqq_prio_changed(cfqq);
2619 static void changed_ioprio(struct io_context *ioc, struct cfq_io_context *cic)
2621 struct cfq_data *cfqd = cic->key;
2622 struct cfq_queue *cfqq;
2623 unsigned long flags;
2625 if (unlikely(!cfqd))
2626 return;
2628 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2630 cfqq = cic->cfqq[BLK_RW_ASYNC];
2631 if (cfqq) {
2632 struct cfq_queue *new_cfqq;
2633 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic->ioc,
2634 GFP_ATOMIC);
2635 if (new_cfqq) {
2636 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
2637 cfq_put_queue(cfqq);
2641 cfqq = cic->cfqq[BLK_RW_SYNC];
2642 if (cfqq)
2643 cfq_mark_cfqq_prio_changed(cfqq);
2645 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2648 static void cfq_ioc_set_ioprio(struct io_context *ioc)
2650 call_for_each_cic(ioc, changed_ioprio);
2651 ioc->ioprio_changed = 0;
2654 static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2655 pid_t pid, bool is_sync)
2657 RB_CLEAR_NODE(&cfqq->rb_node);
2658 RB_CLEAR_NODE(&cfqq->p_node);
2659 INIT_LIST_HEAD(&cfqq->fifo);
2661 atomic_set(&cfqq->ref, 0);
2662 cfqq->cfqd = cfqd;
2664 cfq_mark_cfqq_prio_changed(cfqq);
2666 if (is_sync) {
2667 if (!cfq_class_idle(cfqq))
2668 cfq_mark_cfqq_idle_window(cfqq);
2669 cfq_mark_cfqq_sync(cfqq);
2671 cfqq->pid = pid;
2674 #ifdef CONFIG_CFQ_GROUP_IOSCHED
2675 static void changed_cgroup(struct io_context *ioc, struct cfq_io_context *cic)
2677 struct cfq_queue *sync_cfqq = cic_to_cfqq(cic, 1);
2678 struct cfq_data *cfqd = cic->key;
2679 unsigned long flags;
2680 struct request_queue *q;
2682 if (unlikely(!cfqd))
2683 return;
2685 q = cfqd->queue;
2687 spin_lock_irqsave(q->queue_lock, flags);
2689 if (sync_cfqq) {
2691 * Drop reference to sync queue. A new sync queue will be
2692 * assigned in new group upon arrival of a fresh request.
2694 cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
2695 cic_set_cfqq(cic, NULL, 1);
2696 cfq_put_queue(sync_cfqq);
2699 spin_unlock_irqrestore(q->queue_lock, flags);
2702 static void cfq_ioc_set_cgroup(struct io_context *ioc)
2704 call_for_each_cic(ioc, changed_cgroup);
2705 ioc->cgroup_changed = 0;
2707 #endif /* CONFIG_CFQ_GROUP_IOSCHED */
2709 static struct cfq_queue *
2710 cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync,
2711 struct io_context *ioc, gfp_t gfp_mask)
2713 struct cfq_queue *cfqq, *new_cfqq = NULL;
2714 struct cfq_io_context *cic;
2715 struct cfq_group *cfqg;
2717 retry:
2718 cfqg = cfq_get_cfqg(cfqd, 1);
2719 cic = cfq_cic_lookup(cfqd, ioc);
2720 /* cic always exists here */
2721 cfqq = cic_to_cfqq(cic, is_sync);
2724 * Always try a new alloc if we fell back to the OOM cfqq
2725 * originally, since it should just be a temporary situation.
2727 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
2728 cfqq = NULL;
2729 if (new_cfqq) {
2730 cfqq = new_cfqq;
2731 new_cfqq = NULL;
2732 } else if (gfp_mask & __GFP_WAIT) {
2733 spin_unlock_irq(cfqd->queue->queue_lock);
2734 new_cfqq = kmem_cache_alloc_node(cfq_pool,
2735 gfp_mask | __GFP_ZERO,
2736 cfqd->queue->node);
2737 spin_lock_irq(cfqd->queue->queue_lock);
2738 if (new_cfqq)
2739 goto retry;
2740 } else {
2741 cfqq = kmem_cache_alloc_node(cfq_pool,
2742 gfp_mask | __GFP_ZERO,
2743 cfqd->queue->node);
2746 if (cfqq) {
2747 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
2748 cfq_init_prio_data(cfqq, ioc);
2749 cfq_link_cfqq_cfqg(cfqq, cfqg);
2750 cfq_log_cfqq(cfqd, cfqq, "alloced");
2751 } else
2752 cfqq = &cfqd->oom_cfqq;
2755 if (new_cfqq)
2756 kmem_cache_free(cfq_pool, new_cfqq);
2758 return cfqq;
2761 static struct cfq_queue **
2762 cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
2764 switch (ioprio_class) {
2765 case IOPRIO_CLASS_RT:
2766 return &cfqd->async_cfqq[0][ioprio];
2767 case IOPRIO_CLASS_BE:
2768 return &cfqd->async_cfqq[1][ioprio];
2769 case IOPRIO_CLASS_IDLE:
2770 return &cfqd->async_idle_cfqq;
2771 default:
2772 BUG();
2776 static struct cfq_queue *
2777 cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct io_context *ioc,
2778 gfp_t gfp_mask)
2780 const int ioprio = task_ioprio(ioc);
2781 const int ioprio_class = task_ioprio_class(ioc);
2782 struct cfq_queue **async_cfqq = NULL;
2783 struct cfq_queue *cfqq = NULL;
2785 if (!is_sync) {
2786 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
2787 cfqq = *async_cfqq;
2790 if (!cfqq)
2791 cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, gfp_mask);
2794 * pin the queue now that it's allocated, scheduler exit will prune it
2796 if (!is_sync && !(*async_cfqq)) {
2797 atomic_inc(&cfqq->ref);
2798 *async_cfqq = cfqq;
2801 atomic_inc(&cfqq->ref);
2802 return cfqq;
2806 * We drop cfq io contexts lazily, so we may find a dead one.
2808 static void
2809 cfq_drop_dead_cic(struct cfq_data *cfqd, struct io_context *ioc,
2810 struct cfq_io_context *cic)
2812 unsigned long flags;
2814 WARN_ON(!list_empty(&cic->queue_list));
2816 spin_lock_irqsave(&ioc->lock, flags);
2818 BUG_ON(ioc->ioc_data == cic);
2820 radix_tree_delete(&ioc->radix_root, (unsigned long) cfqd);
2821 hlist_del_rcu(&cic->cic_list);
2822 spin_unlock_irqrestore(&ioc->lock, flags);
2824 cfq_cic_free(cic);
2827 static struct cfq_io_context *
2828 cfq_cic_lookup(struct cfq_data *cfqd, struct io_context *ioc)
2830 struct cfq_io_context *cic;
2831 unsigned long flags;
2832 void *k;
2834 if (unlikely(!ioc))
2835 return NULL;
2837 rcu_read_lock();
2840 * we maintain a last-hit cache, to avoid browsing over the tree
2842 cic = rcu_dereference(ioc->ioc_data);
2843 if (cic && cic->key == cfqd) {
2844 rcu_read_unlock();
2845 return cic;
2848 do {
2849 cic = radix_tree_lookup(&ioc->radix_root, (unsigned long) cfqd);
2850 rcu_read_unlock();
2851 if (!cic)
2852 break;
2853 /* ->key must be copied to avoid race with cfq_exit_queue() */
2854 k = cic->key;
2855 if (unlikely(!k)) {
2856 cfq_drop_dead_cic(cfqd, ioc, cic);
2857 rcu_read_lock();
2858 continue;
2861 spin_lock_irqsave(&ioc->lock, flags);
2862 rcu_assign_pointer(ioc->ioc_data, cic);
2863 spin_unlock_irqrestore(&ioc->lock, flags);
2864 break;
2865 } while (1);
2867 return cic;
2871 * Add cic into ioc, using cfqd as the search key. This enables us to lookup
2872 * the process specific cfq io context when entered from the block layer.
2873 * Also adds the cic to a per-cfqd list, used when this queue is removed.
2875 static int cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
2876 struct cfq_io_context *cic, gfp_t gfp_mask)
2878 unsigned long flags;
2879 int ret;
2881 ret = radix_tree_preload(gfp_mask);
2882 if (!ret) {
2883 cic->ioc = ioc;
2884 cic->key = cfqd;
2886 spin_lock_irqsave(&ioc->lock, flags);
2887 ret = radix_tree_insert(&ioc->radix_root,
2888 (unsigned long) cfqd, cic);
2889 if (!ret)
2890 hlist_add_head_rcu(&cic->cic_list, &ioc->cic_list);
2891 spin_unlock_irqrestore(&ioc->lock, flags);
2893 radix_tree_preload_end();
2895 if (!ret) {
2896 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2897 list_add(&cic->queue_list, &cfqd->cic_list);
2898 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2902 if (ret)
2903 printk(KERN_ERR "cfq: cic link failed!\n");
2905 return ret;
2909 * Setup general io context and cfq io context. There can be several cfq
2910 * io contexts per general io context, if this process is doing io to more
2911 * than one device managed by cfq.
2913 static struct cfq_io_context *
2914 cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
2916 struct io_context *ioc = NULL;
2917 struct cfq_io_context *cic;
2919 might_sleep_if(gfp_mask & __GFP_WAIT);
2921 ioc = get_io_context(gfp_mask, cfqd->queue->node);
2922 if (!ioc)
2923 return NULL;
2925 cic = cfq_cic_lookup(cfqd, ioc);
2926 if (cic)
2927 goto out;
2929 cic = cfq_alloc_io_context(cfqd, gfp_mask);
2930 if (cic == NULL)
2931 goto err;
2933 if (cfq_cic_link(cfqd, ioc, cic, gfp_mask))
2934 goto err_free;
2936 out:
2937 smp_read_barrier_depends();
2938 if (unlikely(ioc->ioprio_changed))
2939 cfq_ioc_set_ioprio(ioc);
2941 #ifdef CONFIG_CFQ_GROUP_IOSCHED
2942 if (unlikely(ioc->cgroup_changed))
2943 cfq_ioc_set_cgroup(ioc);
2944 #endif
2945 return cic;
2946 err_free:
2947 cfq_cic_free(cic);
2948 err:
2949 put_io_context(ioc);
2950 return NULL;
2953 static void
2954 cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
2956 unsigned long elapsed = jiffies - cic->last_end_request;
2957 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
2959 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
2960 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
2961 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
2964 static void
2965 cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2966 struct request *rq)
2968 sector_t sdist = 0;
2969 sector_t n_sec = blk_rq_sectors(rq);
2970 if (cfqq->last_request_pos) {
2971 if (cfqq->last_request_pos < blk_rq_pos(rq))
2972 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
2973 else
2974 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
2977 cfqq->seek_history <<= 1;
2978 if (blk_queue_nonrot(cfqd->queue))
2979 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
2980 else
2981 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
2985 * Disable idle window if the process thinks too long or seeks so much that
2986 * it doesn't matter
2988 static void
2989 cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2990 struct cfq_io_context *cic)
2992 int old_idle, enable_idle;
2995 * Don't idle for async or idle io prio class
2997 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
2998 return;
3000 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
3002 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3003 cfq_mark_cfqq_deep(cfqq);
3005 if (!atomic_read(&cic->ioc->nr_tasks) || !cfqd->cfq_slice_idle ||
3006 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
3007 enable_idle = 0;
3008 else if (sample_valid(cic->ttime_samples)) {
3009 if (cic->ttime_mean > cfqd->cfq_slice_idle)
3010 enable_idle = 0;
3011 else
3012 enable_idle = 1;
3015 if (old_idle != enable_idle) {
3016 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
3017 if (enable_idle)
3018 cfq_mark_cfqq_idle_window(cfqq);
3019 else
3020 cfq_clear_cfqq_idle_window(cfqq);
3025 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3026 * no or if we aren't sure, a 1 will cause a preempt.
3028 static bool
3029 cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
3030 struct request *rq)
3032 struct cfq_queue *cfqq;
3034 cfqq = cfqd->active_queue;
3035 if (!cfqq)
3036 return false;
3038 if (cfq_class_idle(new_cfqq))
3039 return false;
3041 if (cfq_class_idle(cfqq))
3042 return true;
3045 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3047 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
3048 return false;
3051 * if the new request is sync, but the currently running queue is
3052 * not, let the sync request have priority.
3054 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
3055 return true;
3057 if (new_cfqq->cfqg != cfqq->cfqg)
3058 return false;
3060 if (cfq_slice_used(cfqq))
3061 return true;
3063 /* Allow preemption only if we are idling on sync-noidle tree */
3064 if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD &&
3065 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
3066 new_cfqq->service_tree->count == 2 &&
3067 RB_EMPTY_ROOT(&cfqq->sort_list))
3068 return true;
3071 * So both queues are sync. Let the new request get disk time if
3072 * it's a metadata request and the current queue is doing regular IO.
3074 if (rq_is_meta(rq) && !cfqq->meta_pending)
3075 return true;
3078 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
3080 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
3081 return true;
3083 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
3084 return false;
3087 * if this request is as-good as one we would expect from the
3088 * current cfqq, let it preempt
3090 if (cfq_rq_close(cfqd, cfqq, rq, true))
3091 return true;
3093 return false;
3097 * cfqq preempts the active queue. if we allowed preempt with no slice left,
3098 * let it have half of its nominal slice.
3100 static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3102 cfq_log_cfqq(cfqd, cfqq, "preempt");
3103 cfq_slice_expired(cfqd, 1);
3106 * Put the new queue at the front of the of the current list,
3107 * so we know that it will be selected next.
3109 BUG_ON(!cfq_cfqq_on_rr(cfqq));
3111 cfq_service_tree_add(cfqd, cfqq, 1);
3113 cfqq->slice_end = 0;
3114 cfq_mark_cfqq_slice_new(cfqq);
3118 * Called when a new fs request (rq) is added (to cfqq). Check if there's
3119 * something we should do about it
3121 static void
3122 cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3123 struct request *rq)
3125 struct cfq_io_context *cic = RQ_CIC(rq);
3127 cfqd->rq_queued++;
3128 if (rq_is_meta(rq))
3129 cfqq->meta_pending++;
3131 cfq_update_io_thinktime(cfqd, cic);
3132 cfq_update_io_seektime(cfqd, cfqq, rq);
3133 cfq_update_idle_window(cfqd, cfqq, cic);
3135 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
3137 if (cfqq == cfqd->active_queue) {
3139 * Remember that we saw a request from this process, but
3140 * don't start queuing just yet. Otherwise we risk seeing lots
3141 * of tiny requests, because we disrupt the normal plugging
3142 * and merging. If the request is already larger than a single
3143 * page, let it rip immediately. For that case we assume that
3144 * merging is already done. Ditto for a busy system that
3145 * has other work pending, don't risk delaying until the
3146 * idle timer unplug to continue working.
3148 if (cfq_cfqq_wait_request(cfqq)) {
3149 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3150 cfqd->busy_queues > 1) {
3151 del_timer(&cfqd->idle_slice_timer);
3152 cfq_clear_cfqq_wait_request(cfqq);
3153 __blk_run_queue(cfqd->queue);
3154 } else
3155 cfq_mark_cfqq_must_dispatch(cfqq);
3157 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
3159 * not the active queue - expire current slice if it is
3160 * idle and has expired it's mean thinktime or this new queue
3161 * has some old slice time left and is of higher priority or
3162 * this new queue is RT and the current one is BE
3164 cfq_preempt_queue(cfqd, cfqq);
3165 __blk_run_queue(cfqd->queue);
3169 static void cfq_insert_request(struct request_queue *q, struct request *rq)
3171 struct cfq_data *cfqd = q->elevator->elevator_data;
3172 struct cfq_queue *cfqq = RQ_CFQQ(rq);
3174 cfq_log_cfqq(cfqd, cfqq, "insert_request");
3175 cfq_init_prio_data(cfqq, RQ_CIC(rq)->ioc);
3177 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
3178 list_add_tail(&rq->queuelist, &cfqq->fifo);
3179 cfq_add_rq_rb(rq);
3181 cfq_rq_enqueued(cfqd, cfqq, rq);
3185 * Update hw_tag based on peak queue depth over 50 samples under
3186 * sufficient load.
3188 static void cfq_update_hw_tag(struct cfq_data *cfqd)
3190 struct cfq_queue *cfqq = cfqd->active_queue;
3192 if (rq_in_driver(cfqd) > cfqd->hw_tag_est_depth)
3193 cfqd->hw_tag_est_depth = rq_in_driver(cfqd);
3195 if (cfqd->hw_tag == 1)
3196 return;
3198 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
3199 rq_in_driver(cfqd) <= CFQ_HW_QUEUE_MIN)
3200 return;
3203 * If active queue hasn't enough requests and can idle, cfq might not
3204 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3205 * case
3207 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3208 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
3209 CFQ_HW_QUEUE_MIN && rq_in_driver(cfqd) < CFQ_HW_QUEUE_MIN)
3210 return;
3212 if (cfqd->hw_tag_samples++ < 50)
3213 return;
3215 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
3216 cfqd->hw_tag = 1;
3217 else
3218 cfqd->hw_tag = 0;
3221 static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3223 struct cfq_io_context *cic = cfqd->active_cic;
3225 /* If there are other queues in the group, don't wait */
3226 if (cfqq->cfqg->nr_cfqq > 1)
3227 return false;
3229 if (cfq_slice_used(cfqq))
3230 return true;
3232 /* if slice left is less than think time, wait busy */
3233 if (cic && sample_valid(cic->ttime_samples)
3234 && (cfqq->slice_end - jiffies < cic->ttime_mean))
3235 return true;
3238 * If think times is less than a jiffy than ttime_mean=0 and above
3239 * will not be true. It might happen that slice has not expired yet
3240 * but will expire soon (4-5 ns) during select_queue(). To cover the
3241 * case where think time is less than a jiffy, mark the queue wait
3242 * busy if only 1 jiffy is left in the slice.
3244 if (cfqq->slice_end - jiffies == 1)
3245 return true;
3247 return false;
3250 static void cfq_completed_request(struct request_queue *q, struct request *rq)
3252 struct cfq_queue *cfqq = RQ_CFQQ(rq);
3253 struct cfq_data *cfqd = cfqq->cfqd;
3254 const int sync = rq_is_sync(rq);
3255 unsigned long now;
3257 now = jiffies;
3258 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", !!rq_noidle(rq));
3260 cfq_update_hw_tag(cfqd);
3262 WARN_ON(!cfqd->rq_in_driver[sync]);
3263 WARN_ON(!cfqq->dispatched);
3264 cfqd->rq_in_driver[sync]--;
3265 cfqq->dispatched--;
3267 if (cfq_cfqq_sync(cfqq))
3268 cfqd->sync_flight--;
3270 if (sync) {
3271 RQ_CIC(rq)->last_end_request = now;
3272 if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
3273 cfqd->last_delayed_sync = now;
3277 * If this is the active queue, check if it needs to be expired,
3278 * or if we want to idle in case it has no pending requests.
3280 if (cfqd->active_queue == cfqq) {
3281 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
3283 if (cfq_cfqq_slice_new(cfqq)) {
3284 cfq_set_prio_slice(cfqd, cfqq);
3285 cfq_clear_cfqq_slice_new(cfqq);
3289 * Should we wait for next request to come in before we expire
3290 * the queue.
3292 if (cfq_should_wait_busy(cfqd, cfqq)) {
3293 cfqq->slice_end = jiffies + cfqd->cfq_slice_idle;
3294 cfq_mark_cfqq_wait_busy(cfqq);
3298 * Idling is not enabled on:
3299 * - expired queues
3300 * - idle-priority queues
3301 * - async queues
3302 * - queues with still some requests queued
3303 * - when there is a close cooperator
3305 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
3306 cfq_slice_expired(cfqd, 1);
3307 else if (sync && cfqq_empty &&
3308 !cfq_close_cooperator(cfqd, cfqq)) {
3309 cfqd->noidle_tree_requires_idle |= !rq_noidle(rq);
3311 * Idling is enabled for SYNC_WORKLOAD.
3312 * SYNC_NOIDLE_WORKLOAD idles at the end of the tree
3313 * only if we processed at least one !rq_noidle request
3315 if (cfqd->serving_type == SYNC_WORKLOAD
3316 || cfqd->noidle_tree_requires_idle
3317 || cfqq->cfqg->nr_cfqq == 1)
3318 cfq_arm_slice_timer(cfqd);
3322 if (!rq_in_driver(cfqd))
3323 cfq_schedule_dispatch(cfqd);
3327 * we temporarily boost lower priority queues if they are holding fs exclusive
3328 * resources. they are boosted to normal prio (CLASS_BE/4)
3330 static void cfq_prio_boost(struct cfq_queue *cfqq)
3332 if (has_fs_excl()) {
3334 * boost idle prio on transactions that would lock out other
3335 * users of the filesystem
3337 if (cfq_class_idle(cfqq))
3338 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3339 if (cfqq->ioprio > IOPRIO_NORM)
3340 cfqq->ioprio = IOPRIO_NORM;
3341 } else {
3343 * unboost the queue (if needed)
3345 cfqq->ioprio_class = cfqq->org_ioprio_class;
3346 cfqq->ioprio = cfqq->org_ioprio;
3350 static inline int __cfq_may_queue(struct cfq_queue *cfqq)
3352 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
3353 cfq_mark_cfqq_must_alloc_slice(cfqq);
3354 return ELV_MQUEUE_MUST;
3357 return ELV_MQUEUE_MAY;
3360 static int cfq_may_queue(struct request_queue *q, int rw)
3362 struct cfq_data *cfqd = q->elevator->elevator_data;
3363 struct task_struct *tsk = current;
3364 struct cfq_io_context *cic;
3365 struct cfq_queue *cfqq;
3368 * don't force setup of a queue from here, as a call to may_queue
3369 * does not necessarily imply that a request actually will be queued.
3370 * so just lookup a possibly existing queue, or return 'may queue'
3371 * if that fails
3373 cic = cfq_cic_lookup(cfqd, tsk->io_context);
3374 if (!cic)
3375 return ELV_MQUEUE_MAY;
3377 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
3378 if (cfqq) {
3379 cfq_init_prio_data(cfqq, cic->ioc);
3380 cfq_prio_boost(cfqq);
3382 return __cfq_may_queue(cfqq);
3385 return ELV_MQUEUE_MAY;
3389 * queue lock held here
3391 static void cfq_put_request(struct request *rq)
3393 struct cfq_queue *cfqq = RQ_CFQQ(rq);
3395 if (cfqq) {
3396 const int rw = rq_data_dir(rq);
3398 BUG_ON(!cfqq->allocated[rw]);
3399 cfqq->allocated[rw]--;
3401 put_io_context(RQ_CIC(rq)->ioc);
3403 rq->elevator_private = NULL;
3404 rq->elevator_private2 = NULL;
3406 cfq_put_queue(cfqq);
3410 static struct cfq_queue *
3411 cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_context *cic,
3412 struct cfq_queue *cfqq)
3414 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
3415 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
3416 cfq_mark_cfqq_coop(cfqq->new_cfqq);
3417 cfq_put_queue(cfqq);
3418 return cic_to_cfqq(cic, 1);
3422 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
3423 * was the last process referring to said cfqq.
3425 static struct cfq_queue *
3426 split_cfqq(struct cfq_io_context *cic, struct cfq_queue *cfqq)
3428 if (cfqq_process_refs(cfqq) == 1) {
3429 cfqq->pid = current->pid;
3430 cfq_clear_cfqq_coop(cfqq);
3431 cfq_clear_cfqq_split_coop(cfqq);
3432 return cfqq;
3435 cic_set_cfqq(cic, NULL, 1);
3436 cfq_put_queue(cfqq);
3437 return NULL;
3440 * Allocate cfq data structures associated with this request.
3442 static int
3443 cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
3445 struct cfq_data *cfqd = q->elevator->elevator_data;
3446 struct cfq_io_context *cic;
3447 const int rw = rq_data_dir(rq);
3448 const bool is_sync = rq_is_sync(rq);
3449 struct cfq_queue *cfqq;
3450 unsigned long flags;
3452 might_sleep_if(gfp_mask & __GFP_WAIT);
3454 cic = cfq_get_io_context(cfqd, gfp_mask);
3456 spin_lock_irqsave(q->queue_lock, flags);
3458 if (!cic)
3459 goto queue_fail;
3461 new_queue:
3462 cfqq = cic_to_cfqq(cic, is_sync);
3463 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
3464 cfqq = cfq_get_queue(cfqd, is_sync, cic->ioc, gfp_mask);
3465 cic_set_cfqq(cic, cfqq, is_sync);
3466 } else {
3468 * If the queue was seeky for too long, break it apart.
3470 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
3471 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
3472 cfqq = split_cfqq(cic, cfqq);
3473 if (!cfqq)
3474 goto new_queue;
3478 * Check to see if this queue is scheduled to merge with
3479 * another, closely cooperating queue. The merging of
3480 * queues happens here as it must be done in process context.
3481 * The reference on new_cfqq was taken in merge_cfqqs.
3483 if (cfqq->new_cfqq)
3484 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
3487 cfqq->allocated[rw]++;
3488 atomic_inc(&cfqq->ref);
3490 spin_unlock_irqrestore(q->queue_lock, flags);
3492 rq->elevator_private = cic;
3493 rq->elevator_private2 = cfqq;
3494 return 0;
3496 queue_fail:
3497 if (cic)
3498 put_io_context(cic->ioc);
3500 cfq_schedule_dispatch(cfqd);
3501 spin_unlock_irqrestore(q->queue_lock, flags);
3502 cfq_log(cfqd, "set_request fail");
3503 return 1;
3506 static void cfq_kick_queue(struct work_struct *work)
3508 struct cfq_data *cfqd =
3509 container_of(work, struct cfq_data, unplug_work);
3510 struct request_queue *q = cfqd->queue;
3512 spin_lock_irq(q->queue_lock);
3513 __blk_run_queue(cfqd->queue);
3514 spin_unlock_irq(q->queue_lock);
3518 * Timer running if the active_queue is currently idling inside its time slice
3520 static void cfq_idle_slice_timer(unsigned long data)
3522 struct cfq_data *cfqd = (struct cfq_data *) data;
3523 struct cfq_queue *cfqq;
3524 unsigned long flags;
3525 int timed_out = 1;
3527 cfq_log(cfqd, "idle timer fired");
3529 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
3531 cfqq = cfqd->active_queue;
3532 if (cfqq) {
3533 timed_out = 0;
3536 * We saw a request before the queue expired, let it through
3538 if (cfq_cfqq_must_dispatch(cfqq))
3539 goto out_kick;
3542 * expired
3544 if (cfq_slice_used(cfqq))
3545 goto expire;
3548 * only expire and reinvoke request handler, if there are
3549 * other queues with pending requests
3551 if (!cfqd->busy_queues)
3552 goto out_cont;
3555 * not expired and it has a request pending, let it dispatch
3557 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
3558 goto out_kick;
3561 * Queue depth flag is reset only when the idle didn't succeed
3563 cfq_clear_cfqq_deep(cfqq);
3565 expire:
3566 cfq_slice_expired(cfqd, timed_out);
3567 out_kick:
3568 cfq_schedule_dispatch(cfqd);
3569 out_cont:
3570 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
3573 static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
3575 del_timer_sync(&cfqd->idle_slice_timer);
3576 cancel_work_sync(&cfqd->unplug_work);
3579 static void cfq_put_async_queues(struct cfq_data *cfqd)
3581 int i;
3583 for (i = 0; i < IOPRIO_BE_NR; i++) {
3584 if (cfqd->async_cfqq[0][i])
3585 cfq_put_queue(cfqd->async_cfqq[0][i]);
3586 if (cfqd->async_cfqq[1][i])
3587 cfq_put_queue(cfqd->async_cfqq[1][i]);
3590 if (cfqd->async_idle_cfqq)
3591 cfq_put_queue(cfqd->async_idle_cfqq);
3594 static void cfq_cfqd_free(struct rcu_head *head)
3596 kfree(container_of(head, struct cfq_data, rcu));
3599 static void cfq_exit_queue(struct elevator_queue *e)
3601 struct cfq_data *cfqd = e->elevator_data;
3602 struct request_queue *q = cfqd->queue;
3604 cfq_shutdown_timer_wq(cfqd);
3606 spin_lock_irq(q->queue_lock);
3608 if (cfqd->active_queue)
3609 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
3611 while (!list_empty(&cfqd->cic_list)) {
3612 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
3613 struct cfq_io_context,
3614 queue_list);
3616 __cfq_exit_single_io_context(cfqd, cic);
3619 cfq_put_async_queues(cfqd);
3620 cfq_release_cfq_groups(cfqd);
3621 blkiocg_del_blkio_group(&cfqd->root_group.blkg);
3623 spin_unlock_irq(q->queue_lock);
3625 cfq_shutdown_timer_wq(cfqd);
3627 /* Wait for cfqg->blkg->key accessors to exit their grace periods. */
3628 call_rcu(&cfqd->rcu, cfq_cfqd_free);
3631 static void *cfq_init_queue(struct request_queue *q)
3633 struct cfq_data *cfqd;
3634 int i, j;
3635 struct cfq_group *cfqg;
3636 struct cfq_rb_root *st;
3638 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
3639 if (!cfqd)
3640 return NULL;
3642 /* Init root service tree */
3643 cfqd->grp_service_tree = CFQ_RB_ROOT;
3645 /* Init root group */
3646 cfqg = &cfqd->root_group;
3647 for_each_cfqg_st(cfqg, i, j, st)
3648 *st = CFQ_RB_ROOT;
3649 RB_CLEAR_NODE(&cfqg->rb_node);
3651 /* Give preference to root group over other groups */
3652 cfqg->weight = 2*BLKIO_WEIGHT_DEFAULT;
3654 #ifdef CONFIG_CFQ_GROUP_IOSCHED
3656 * Take a reference to root group which we never drop. This is just
3657 * to make sure that cfq_put_cfqg() does not try to kfree root group
3659 atomic_set(&cfqg->ref, 1);
3660 blkiocg_add_blkio_group(&blkio_root_cgroup, &cfqg->blkg, (void *)cfqd,
3662 #endif
3664 * Not strictly needed (since RB_ROOT just clears the node and we
3665 * zeroed cfqd on alloc), but better be safe in case someone decides
3666 * to add magic to the rb code
3668 for (i = 0; i < CFQ_PRIO_LISTS; i++)
3669 cfqd->prio_trees[i] = RB_ROOT;
3672 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
3673 * Grab a permanent reference to it, so that the normal code flow
3674 * will not attempt to free it.
3676 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
3677 atomic_inc(&cfqd->oom_cfqq.ref);
3678 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, &cfqd->root_group);
3680 INIT_LIST_HEAD(&cfqd->cic_list);
3682 cfqd->queue = q;
3684 init_timer(&cfqd->idle_slice_timer);
3685 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
3686 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
3688 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
3690 cfqd->cfq_quantum = cfq_quantum;
3691 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
3692 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
3693 cfqd->cfq_back_max = cfq_back_max;
3694 cfqd->cfq_back_penalty = cfq_back_penalty;
3695 cfqd->cfq_slice[0] = cfq_slice_async;
3696 cfqd->cfq_slice[1] = cfq_slice_sync;
3697 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
3698 cfqd->cfq_slice_idle = cfq_slice_idle;
3699 cfqd->cfq_latency = 1;
3700 cfqd->cfq_group_isolation = 0;
3701 cfqd->hw_tag = -1;
3703 * we optimistically start assuming sync ops weren't delayed in last
3704 * second, in order to have larger depth for async operations.
3706 cfqd->last_delayed_sync = jiffies - HZ;
3707 INIT_RCU_HEAD(&cfqd->rcu);
3708 return cfqd;
3711 static void cfq_slab_kill(void)
3714 * Caller already ensured that pending RCU callbacks are completed,
3715 * so we should have no busy allocations at this point.
3717 if (cfq_pool)
3718 kmem_cache_destroy(cfq_pool);
3719 if (cfq_ioc_pool)
3720 kmem_cache_destroy(cfq_ioc_pool);
3723 static int __init cfq_slab_setup(void)
3725 cfq_pool = KMEM_CACHE(cfq_queue, 0);
3726 if (!cfq_pool)
3727 goto fail;
3729 cfq_ioc_pool = KMEM_CACHE(cfq_io_context, 0);
3730 if (!cfq_ioc_pool)
3731 goto fail;
3733 return 0;
3734 fail:
3735 cfq_slab_kill();
3736 return -ENOMEM;
3740 * sysfs parts below -->
3742 static ssize_t
3743 cfq_var_show(unsigned int var, char *page)
3745 return sprintf(page, "%d\n", var);
3748 static ssize_t
3749 cfq_var_store(unsigned int *var, const char *page, size_t count)
3751 char *p = (char *) page;
3753 *var = simple_strtoul(p, &p, 10);
3754 return count;
3757 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
3758 static ssize_t __FUNC(struct elevator_queue *e, char *page) \
3760 struct cfq_data *cfqd = e->elevator_data; \
3761 unsigned int __data = __VAR; \
3762 if (__CONV) \
3763 __data = jiffies_to_msecs(__data); \
3764 return cfq_var_show(__data, (page)); \
3766 SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
3767 SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
3768 SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
3769 SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
3770 SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
3771 SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
3772 SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
3773 SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
3774 SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
3775 SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
3776 SHOW_FUNCTION(cfq_group_isolation_show, cfqd->cfq_group_isolation, 0);
3777 #undef SHOW_FUNCTION
3779 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
3780 static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
3782 struct cfq_data *cfqd = e->elevator_data; \
3783 unsigned int __data; \
3784 int ret = cfq_var_store(&__data, (page), count); \
3785 if (__data < (MIN)) \
3786 __data = (MIN); \
3787 else if (__data > (MAX)) \
3788 __data = (MAX); \
3789 if (__CONV) \
3790 *(__PTR) = msecs_to_jiffies(__data); \
3791 else \
3792 *(__PTR) = __data; \
3793 return ret; \
3795 STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
3796 STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
3797 UINT_MAX, 1);
3798 STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
3799 UINT_MAX, 1);
3800 STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
3801 STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
3802 UINT_MAX, 0);
3803 STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
3804 STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
3805 STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
3806 STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
3807 UINT_MAX, 0);
3808 STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
3809 STORE_FUNCTION(cfq_group_isolation_store, &cfqd->cfq_group_isolation, 0, 1, 0);
3810 #undef STORE_FUNCTION
3812 #define CFQ_ATTR(name) \
3813 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
3815 static struct elv_fs_entry cfq_attrs[] = {
3816 CFQ_ATTR(quantum),
3817 CFQ_ATTR(fifo_expire_sync),
3818 CFQ_ATTR(fifo_expire_async),
3819 CFQ_ATTR(back_seek_max),
3820 CFQ_ATTR(back_seek_penalty),
3821 CFQ_ATTR(slice_sync),
3822 CFQ_ATTR(slice_async),
3823 CFQ_ATTR(slice_async_rq),
3824 CFQ_ATTR(slice_idle),
3825 CFQ_ATTR(low_latency),
3826 CFQ_ATTR(group_isolation),
3827 __ATTR_NULL
3830 static struct elevator_type iosched_cfq = {
3831 .ops = {
3832 .elevator_merge_fn = cfq_merge,
3833 .elevator_merged_fn = cfq_merged_request,
3834 .elevator_merge_req_fn = cfq_merged_requests,
3835 .elevator_allow_merge_fn = cfq_allow_merge,
3836 .elevator_dispatch_fn = cfq_dispatch_requests,
3837 .elevator_add_req_fn = cfq_insert_request,
3838 .elevator_activate_req_fn = cfq_activate_request,
3839 .elevator_deactivate_req_fn = cfq_deactivate_request,
3840 .elevator_queue_empty_fn = cfq_queue_empty,
3841 .elevator_completed_req_fn = cfq_completed_request,
3842 .elevator_former_req_fn = elv_rb_former_request,
3843 .elevator_latter_req_fn = elv_rb_latter_request,
3844 .elevator_set_req_fn = cfq_set_request,
3845 .elevator_put_req_fn = cfq_put_request,
3846 .elevator_may_queue_fn = cfq_may_queue,
3847 .elevator_init_fn = cfq_init_queue,
3848 .elevator_exit_fn = cfq_exit_queue,
3849 .trim = cfq_free_io_context,
3851 .elevator_attrs = cfq_attrs,
3852 .elevator_name = "cfq",
3853 .elevator_owner = THIS_MODULE,
3856 #ifdef CONFIG_CFQ_GROUP_IOSCHED
3857 static struct blkio_policy_type blkio_policy_cfq = {
3858 .ops = {
3859 .blkio_unlink_group_fn = cfq_unlink_blkio_group,
3860 .blkio_update_group_weight_fn = cfq_update_blkio_group_weight,
3863 #else
3864 static struct blkio_policy_type blkio_policy_cfq;
3865 #endif
3867 static int __init cfq_init(void)
3870 * could be 0 on HZ < 1000 setups
3872 if (!cfq_slice_async)
3873 cfq_slice_async = 1;
3874 if (!cfq_slice_idle)
3875 cfq_slice_idle = 1;
3877 if (cfq_slab_setup())
3878 return -ENOMEM;
3880 elv_register(&iosched_cfq);
3881 blkio_policy_register(&blkio_policy_cfq);
3883 return 0;
3886 static void __exit cfq_exit(void)
3888 DECLARE_COMPLETION_ONSTACK(all_gone);
3889 blkio_policy_unregister(&blkio_policy_cfq);
3890 elv_unregister(&iosched_cfq);
3891 ioc_gone = &all_gone;
3892 /* ioc_gone's update must be visible before reading ioc_count */
3893 smp_wmb();
3896 * this also protects us from entering cfq_slab_kill() with
3897 * pending RCU callbacks
3899 if (elv_ioc_count_read(cfq_ioc_count))
3900 wait_for_completion(&all_gone);
3901 cfq_slab_kill();
3904 module_init(cfq_init);
3905 module_exit(cfq_exit);
3907 MODULE_AUTHOR("Jens Axboe");
3908 MODULE_LICENSE("GPL");
3909 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");