2 * CFQ, or complete fairness queueing, disk scheduler.
4 * Based on ideas from a previously unfinished io
5 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
7 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
9 #include <linux/module.h>
10 #include <linux/blkdev.h>
11 #include <linux/elevator.h>
12 #include <linux/hash.h>
13 #include <linux/rbtree.h>
14 #include <linux/ioprio.h>
19 static const int cfq_quantum = 4; /* max queue in one round of service */
20 static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
21 static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
22 static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
24 static const int cfq_slice_sync = HZ / 10;
25 static int cfq_slice_async = HZ / 25;
26 static const int cfq_slice_async_rq = 2;
27 static int cfq_slice_idle = HZ / 125;
29 #define CFQ_IDLE_GRACE (HZ / 10)
30 #define CFQ_SLICE_SCALE (5)
32 #define CFQ_KEY_ASYNC (0)
35 * for the hash of cfqq inside the cfqd
37 #define CFQ_QHASH_SHIFT 6
38 #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
39 #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
41 #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
43 #define RQ_CIC(rq) ((struct cfq_io_context*)(rq)->elevator_private)
44 #define RQ_CFQQ(rq) ((rq)->elevator_private2)
46 static struct kmem_cache *cfq_pool;
47 static struct kmem_cache *cfq_ioc_pool;
49 static DEFINE_PER_CPU(unsigned long, ioc_count);
50 static struct completion *ioc_gone;
52 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
53 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
54 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
59 #define cfq_cfqq_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
61 #define sample_valid(samples) ((samples) > 80)
64 * Per block device queue structure
67 request_queue_t *queue;
70 * rr list of queues with requests and the count of them
72 struct list_head rr_list[CFQ_PRIO_LISTS];
73 struct list_head busy_rr;
74 struct list_head cur_rr;
75 struct list_head idle_rr;
76 unsigned long cur_rr_tick;
77 unsigned int busy_queues;
82 struct hlist_head *cfq_hash;
88 * idle window management
90 struct timer_list idle_slice_timer;
91 struct work_struct unplug_work;
93 struct cfq_queue *active_queue;
94 struct cfq_io_context *active_cic;
95 int cur_prio, cur_end_prio;
96 unsigned long prio_time;
97 unsigned int dispatch_slice;
99 struct timer_list idle_class_timer;
101 sector_t last_position;
102 unsigned long last_end_request;
105 * tunables, see top of file
107 unsigned int cfq_quantum;
108 unsigned int cfq_fifo_expire[2];
109 unsigned int cfq_back_penalty;
110 unsigned int cfq_back_max;
111 unsigned int cfq_slice[2];
112 unsigned int cfq_slice_async_rq;
113 unsigned int cfq_slice_idle;
115 struct list_head cic_list;
117 sector_t new_seek_mean;
122 * Per process-grouping structure
125 /* reference count */
127 /* parent cfq_data */
128 struct cfq_data *cfqd;
129 /* cfqq lookup hash */
130 struct hlist_node cfq_hash;
133 /* member of the rr/busy/cur/idle cfqd list */
134 struct list_head cfq_list;
135 /* in what tick we were last serviced */
136 unsigned long rr_tick;
137 /* sorted list of pending requests */
138 struct rb_root sort_list;
139 /* if fifo isn't expired, next request to serve */
140 struct request *next_rq;
141 /* requests queued in sort_list */
143 /* currently allocated requests */
145 /* pending metadata requests */
147 /* fifo list of requests in sort_list */
148 struct list_head fifo;
150 unsigned long slice_end;
151 unsigned long service_last;
152 unsigned long slice_start;
155 /* number of requests that are on the dispatch list or inside driver */
158 /* io prio of this group */
159 unsigned short ioprio, org_ioprio;
160 unsigned short ioprio_class, org_ioprio_class;
162 /* various state flags, see below */
165 sector_t last_request_pos;
168 enum cfqq_state_flags {
169 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
170 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
171 CFQ_CFQQ_FLAG_must_alloc, /* must be allowed rq alloc */
172 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
173 CFQ_CFQQ_FLAG_must_dispatch, /* must dispatch, even if expired */
174 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
175 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
176 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
177 CFQ_CFQQ_FLAG_queue_new, /* queue never been serviced */
178 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
181 #define CFQ_CFQQ_FNS(name) \
182 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
184 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
186 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
188 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
190 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
192 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
196 CFQ_CFQQ_FNS(wait_request);
197 CFQ_CFQQ_FNS(must_alloc);
198 CFQ_CFQQ_FNS(must_alloc_slice);
199 CFQ_CFQQ_FNS(must_dispatch);
200 CFQ_CFQQ_FNS(fifo_expire);
201 CFQ_CFQQ_FNS(idle_window);
202 CFQ_CFQQ_FNS(prio_changed);
203 CFQ_CFQQ_FNS(queue_new);
204 CFQ_CFQQ_FNS(slice_new);
207 static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
208 static void cfq_dispatch_insert(request_queue_t *, struct request *);
209 static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk, gfp_t gfp_mask);
212 * scheduler run of queue, if there are requests pending and no one in the
213 * driver that will restart queueing
215 static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
217 if (cfqd->busy_queues)
218 kblockd_schedule_work(&cfqd->unplug_work);
221 static int cfq_queue_empty(request_queue_t *q)
223 struct cfq_data *cfqd = q->elevator->elevator_data;
225 return !cfqd->busy_queues;
228 static inline pid_t cfq_queue_pid(struct task_struct *task, int rw, int is_sync)
231 * Use the per-process queue, for read requests and syncronous writes
233 if (!(rw & REQ_RW) || is_sync)
236 return CFQ_KEY_ASYNC;
240 * Scale schedule slice based on io priority. Use the sync time slice only
241 * if a queue is marked sync and has sync io queued. A sync queue with async
242 * io only, should not get full sync slice length.
245 cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
247 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
249 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
251 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
255 cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
257 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
258 cfqq->slice_end += cfqq->slice_resid;
261 * Don't carry over residual for more than one slice, we only want
262 * to slightly correct the fairness. Carrying over forever would
263 * easily introduce oscillations.
265 cfqq->slice_resid = 0;
267 cfqq->slice_start = jiffies;
271 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
272 * isn't valid until the first request from the dispatch is activated
273 * and the slice time set.
275 static inline int cfq_slice_used(struct cfq_queue *cfqq)
277 if (cfq_cfqq_slice_new(cfqq))
279 if (time_before(jiffies, cfqq->slice_end))
286 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
287 * We choose the request that is closest to the head right now. Distance
288 * behind the head is penalized and only allowed to a certain extent.
290 static struct request *
291 cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
293 sector_t last, s1, s2, d1 = 0, d2 = 0;
294 unsigned long back_max;
295 #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
296 #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
297 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
299 if (rq1 == NULL || rq1 == rq2)
304 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
306 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
308 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
310 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
316 last = cfqd->last_position;
319 * by definition, 1KiB is 2 sectors
321 back_max = cfqd->cfq_back_max * 2;
324 * Strict one way elevator _except_ in the case where we allow
325 * short backward seeks which are biased as twice the cost of a
326 * similar forward seek.
330 else if (s1 + back_max >= last)
331 d1 = (last - s1) * cfqd->cfq_back_penalty;
333 wrap |= CFQ_RQ1_WRAP;
337 else if (s2 + back_max >= last)
338 d2 = (last - s2) * cfqd->cfq_back_penalty;
340 wrap |= CFQ_RQ2_WRAP;
342 /* Found required data */
345 * By doing switch() on the bit mask "wrap" we avoid having to
346 * check two variables for all permutations: --> faster!
349 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
365 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
368 * Since both rqs are wrapped,
369 * start with the one that's further behind head
370 * (--> only *one* back seek required),
371 * since back seek takes more time than forward.
381 * would be nice to take fifo expire time into account as well
383 static struct request *
384 cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
385 struct request *last)
387 struct rb_node *rbnext = rb_next(&last->rb_node);
388 struct rb_node *rbprev = rb_prev(&last->rb_node);
389 struct request *next = NULL, *prev = NULL;
391 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
394 prev = rb_entry_rq(rbprev);
397 next = rb_entry_rq(rbnext);
399 rbnext = rb_first(&cfqq->sort_list);
400 if (rbnext && rbnext != &last->rb_node)
401 next = rb_entry_rq(rbnext);
404 return cfq_choose_req(cfqd, next, prev);
408 * This function finds out where to insert a BE queue in the service hierarchy
410 static void cfq_resort_be_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
413 struct list_head *list, *n;
414 struct cfq_queue *__cfqq;
418 * if cfqq has requests in flight, don't allow it to be
419 * found in cfq_set_active_queue before it has finished them.
420 * this is done to increase fairness between a process that
421 * has lots of io pending vs one that only generates one
422 * sporadically or synchronously
424 if (cfqq->dispatched)
425 list = &cfqd->busy_rr;
426 else if (cfqq->ioprio == (cfqd->cur_prio + 1) &&
427 cfq_cfqq_sync(cfqq) &&
428 (time_before(cfqd->prio_time, cfqq->service_last) ||
429 cfq_cfqq_queue_new(cfqq) || preempted)) {
430 list = &cfqd->cur_rr;
433 list = &cfqd->rr_list[cfqq->ioprio];
435 if (!cfq_cfqq_sync(cfqq) || add_tail) {
437 * async queue always goes to the end. this wont be overly
438 * unfair to writes, as the sort of the sync queue wont be
439 * allowed to pass the async queue again.
441 list_add_tail(&cfqq->cfq_list, list);
442 } else if (preempted || cfq_cfqq_queue_new(cfqq)) {
444 * If this queue was preempted or is new (never been serviced),
445 * let it be added first for fairness but beind other new
449 while (n->next != list) {
450 __cfqq = list_entry_cfqq(n->next);
451 if (!cfq_cfqq_queue_new(__cfqq))
456 list_add(&cfqq->cfq_list, n);
459 * sort by last service, but don't cross a new or async
460 * queue. we don't cross a new queue because it hasn't been
461 * service before, and we don't cross an async queue because
462 * it gets added to the end on expire.
465 while ((n = n->prev) != list) {
466 struct cfq_queue *__c = list_entry_cfqq(n);
468 if (!cfq_cfqq_sync(__c) || !__c->service_last)
470 if (time_before(__c->service_last, cfqq->service_last))
473 list_add(&cfqq->cfq_list, n);
477 static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
479 struct cfq_data *cfqd = cfqq->cfqd;
483 * Resorting requires the cfqq to be on the RR list already.
485 if (!cfq_cfqq_on_rr(cfqq))
488 list_del(&cfqq->cfq_list);
490 if (cfq_class_rt(cfqq)) {
492 * At to the front of the current list, but behind other
496 while (n->next != &cfqd->cur_rr)
497 if (!cfq_class_rt(cfqq))
500 list_add(&cfqq->cfq_list, n);
501 } else if (cfq_class_idle(cfqq)) {
503 * IDLE goes to the tail of the idle list
505 list_add_tail(&cfqq->cfq_list, &cfqd->idle_rr);
508 * So we get here, ergo the queue is a regular best-effort queue
510 cfq_resort_be_queue(cfqd, cfqq, preempted);
515 * add to busy list of queues for service, trying to be fair in ordering
516 * the pending list according to last request service
519 cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
521 BUG_ON(cfq_cfqq_on_rr(cfqq));
522 cfq_mark_cfqq_on_rr(cfqq);
525 cfq_resort_rr_list(cfqq, 0);
529 cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
531 BUG_ON(!cfq_cfqq_on_rr(cfqq));
532 cfq_clear_cfqq_on_rr(cfqq);
533 list_del_init(&cfqq->cfq_list);
535 BUG_ON(!cfqd->busy_queues);
540 * rb tree support functions
542 static inline void cfq_del_rq_rb(struct request *rq)
544 struct cfq_queue *cfqq = RQ_CFQQ(rq);
545 struct cfq_data *cfqd = cfqq->cfqd;
546 const int sync = rq_is_sync(rq);
548 BUG_ON(!cfqq->queued[sync]);
549 cfqq->queued[sync]--;
551 elv_rb_del(&cfqq->sort_list, rq);
553 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
554 cfq_del_cfqq_rr(cfqd, cfqq);
557 static void cfq_add_rq_rb(struct request *rq)
559 struct cfq_queue *cfqq = RQ_CFQQ(rq);
560 struct cfq_data *cfqd = cfqq->cfqd;
561 struct request *__alias;
563 cfqq->queued[rq_is_sync(rq)]++;
566 * looks a little odd, but the first insert might return an alias.
567 * if that happens, put the alias on the dispatch list
569 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
570 cfq_dispatch_insert(cfqd->queue, __alias);
572 if (!cfq_cfqq_on_rr(cfqq))
573 cfq_add_cfqq_rr(cfqd, cfqq);
576 * check if this request is a better next-serve candidate
578 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
579 BUG_ON(!cfqq->next_rq);
583 cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
585 elv_rb_del(&cfqq->sort_list, rq);
586 cfqq->queued[rq_is_sync(rq)]--;
590 static struct request *
591 cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
593 struct task_struct *tsk = current;
594 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio), bio_sync(bio));
595 struct cfq_queue *cfqq;
597 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
599 sector_t sector = bio->bi_sector + bio_sectors(bio);
601 return elv_rb_find(&cfqq->sort_list, sector);
607 static void cfq_activate_request(request_queue_t *q, struct request *rq)
609 struct cfq_data *cfqd = q->elevator->elevator_data;
611 cfqd->rq_in_driver++;
614 * If the depth is larger 1, it really could be queueing. But lets
615 * make the mark a little higher - idling could still be good for
616 * low queueing, and a low queueing number could also just indicate
617 * a SCSI mid layer like behaviour where limit+1 is often seen.
619 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
622 cfqd->last_position = rq->hard_sector + rq->hard_nr_sectors;
625 static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
627 struct cfq_data *cfqd = q->elevator->elevator_data;
629 WARN_ON(!cfqd->rq_in_driver);
630 cfqd->rq_in_driver--;
633 static void cfq_remove_request(struct request *rq)
635 struct cfq_queue *cfqq = RQ_CFQQ(rq);
637 if (cfqq->next_rq == rq)
638 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
640 list_del_init(&rq->queuelist);
643 if (rq_is_meta(rq)) {
644 WARN_ON(!cfqq->meta_pending);
645 cfqq->meta_pending--;
650 cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
652 struct cfq_data *cfqd = q->elevator->elevator_data;
653 struct request *__rq;
655 __rq = cfq_find_rq_fmerge(cfqd, bio);
656 if (__rq && elv_rq_merge_ok(__rq, bio)) {
658 return ELEVATOR_FRONT_MERGE;
661 return ELEVATOR_NO_MERGE;
664 static void cfq_merged_request(request_queue_t *q, struct request *req,
667 if (type == ELEVATOR_FRONT_MERGE) {
668 struct cfq_queue *cfqq = RQ_CFQQ(req);
670 cfq_reposition_rq_rb(cfqq, req);
675 cfq_merged_requests(request_queue_t *q, struct request *rq,
676 struct request *next)
679 * reposition in fifo if next is older than rq
681 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
682 time_before(next->start_time, rq->start_time))
683 list_move(&rq->queuelist, &next->queuelist);
685 cfq_remove_request(next);
688 static int cfq_allow_merge(request_queue_t *q, struct request *rq,
691 struct cfq_data *cfqd = q->elevator->elevator_data;
692 const int rw = bio_data_dir(bio);
693 struct cfq_queue *cfqq;
697 * Disallow merge of a sync bio into an async request.
699 if ((bio_data_dir(bio) == READ || bio_sync(bio)) && !rq_is_sync(rq))
703 * Lookup the cfqq that this bio will be queued with. Allow
704 * merge only if rq is queued there.
706 key = cfq_queue_pid(current, rw, bio_sync(bio));
707 cfqq = cfq_find_cfq_hash(cfqd, key, current->ioprio);
709 if (cfqq == RQ_CFQQ(rq))
716 __cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
720 * stop potential idle class queues waiting service
722 del_timer(&cfqd->idle_class_timer);
725 cfq_clear_cfqq_must_alloc_slice(cfqq);
726 cfq_clear_cfqq_fifo_expire(cfqq);
727 cfq_mark_cfqq_slice_new(cfqq);
728 cfqq->rr_tick = cfqd->cur_rr_tick;
731 cfqd->active_queue = cfqq;
735 * current cfqq expired its slice (or was too idle), select new one
738 __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
739 int preempted, int timed_out)
741 if (cfq_cfqq_wait_request(cfqq))
742 del_timer(&cfqd->idle_slice_timer);
744 cfq_clear_cfqq_must_dispatch(cfqq);
745 cfq_clear_cfqq_wait_request(cfqq);
746 cfq_clear_cfqq_queue_new(cfqq);
749 * store what was left of this slice, if the queue idled out
752 if (timed_out && !cfq_cfqq_slice_new(cfqq))
753 cfqq->slice_resid = cfqq->slice_end - jiffies;
755 cfq_resort_rr_list(cfqq, preempted);
757 if (cfqq == cfqd->active_queue)
758 cfqd->active_queue = NULL;
760 if (cfqd->active_cic) {
761 put_io_context(cfqd->active_cic->ioc);
762 cfqd->active_cic = NULL;
765 cfqd->dispatch_slice = 0;
768 static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted,
771 struct cfq_queue *cfqq = cfqd->active_queue;
774 __cfq_slice_expired(cfqd, cfqq, preempted, timed_out);
787 static int cfq_get_next_prio_level(struct cfq_data *cfqd)
796 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
797 if (!list_empty(&cfqd->rr_list[p])) {
806 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
807 cfqd->cur_end_prio = 0;
814 if (unlikely(prio == -1))
817 BUG_ON(prio >= CFQ_PRIO_LISTS);
819 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
821 cfqd->cur_prio = prio + 1;
822 if (cfqd->cur_prio > cfqd->cur_end_prio) {
823 cfqd->cur_end_prio = cfqd->cur_prio;
826 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
828 cfqd->cur_end_prio = 0;
832 cfqd->prio_time = jiffies;
836 static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
839 if (rq->sector >= cfqd->last_position)
840 return rq->sector - cfqd->last_position;
842 return cfqd->last_position - rq->sector;
845 static struct cfq_queue *cfq_get_best_queue(struct cfq_data *cfqd)
847 struct cfq_queue *cfqq = NULL, *__cfqq;
848 sector_t best = -1, dist;
850 list_for_each_entry(__cfqq, &cfqd->cur_rr, cfq_list) {
851 if (!__cfqq->next_rq || !cfq_cfqq_sync(__cfqq))
854 dist = cfq_dist_from_last(cfqd, __cfqq->next_rq);
862 * Only async queue(s) available, grab first entry
865 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
870 static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
872 struct cfq_queue *cfqq = NULL;
874 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1) {
876 * if current list is non-empty, grab first entry. if it is
877 * empty, get next prio level and grab first entry then if any
880 cfqq = cfq_get_best_queue(cfqd);
881 } else if (!list_empty(&cfqd->busy_rr)) {
883 * If no new queues are available, check if the busy list has
884 * some before falling back to idle io.
886 cfqq = list_entry_cfqq(cfqd->busy_rr.next);
887 } else if (!list_empty(&cfqd->idle_rr)) {
889 * if we have idle queues and no rt or be queues had pending
890 * requests, either allow immediate service if the grace period
891 * has passed or arm the idle grace timer
893 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
895 if (time_after_eq(jiffies, end))
896 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
898 mod_timer(&cfqd->idle_class_timer, end);
904 static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
906 struct cfq_queue *cfqq;
911 cfqq = cfq_get_next_queue(cfqd);
915 prio = cfq_prio_to_slice(cfqd, cfqq);
916 if (cfqq->slice_resid > -prio)
919 cfqq->slice_resid += prio;
920 list_del_init(&cfqq->cfq_list);
921 list_add_tail(&cfqq->cfq_list, &cfqd->rr_list[cfqq->ioprio]);
925 __cfq_set_active_queue(cfqd, cfqq);
929 static inline int cfq_rq_close(struct cfq_data *cfqd, struct request *rq)
931 struct cfq_io_context *cic = cfqd->active_cic;
933 if (!sample_valid(cic->seek_samples))
936 return cfq_dist_from_last(cfqd, rq) <= cic->seek_mean;
939 static struct cfq_queue *__cfq_close_cooperator(struct cfq_data *cfqd,
940 struct cfq_queue *cur_cfqq,
941 struct list_head *list)
943 struct cfq_queue *cfqq;
945 list_for_each_entry(cfqq, list, cfq_list) {
946 if (cfqq == cur_cfqq || !cfq_cfqq_sync(cfqq))
949 BUG_ON(!cfqq->next_rq);
951 if (cfq_rq_close(cfqd, cfqq->next_rq))
958 static int cfq_close_cooperator(struct cfq_data *cfqd,
959 struct cfq_queue *cur_cfqq)
961 struct cfq_queue *cfqq;
963 if (!cfqd->busy_queues)
967 * check cur_rr and same-prio rr_list for candidates
969 cfqq = __cfq_close_cooperator(cfqd, cur_cfqq, &cfqd->cur_rr);
973 cfqq = __cfq_close_cooperator(cfqd, cur_cfqq, &cfqd->rr_list[cur_cfqq->ioprio]);
974 if (cfqq && (cfqq->rr_tick == cfqd->cur_rr_tick))
980 #define CIC_SEEKY(cic) ((cic)->seek_mean > (8 * 1024))
982 static void cfq_arm_slice_timer(struct cfq_data *cfqd)
984 struct cfq_queue *cfqq = cfqd->active_queue;
985 struct cfq_io_context *cic;
988 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
989 WARN_ON(cfq_cfqq_slice_new(cfqq));
992 * idle is disabled, either manually or by past process history
994 if (!cfqd->cfq_slice_idle || !cfq_cfqq_idle_window(cfqq))
998 * task has exited, don't wait
1000 cic = cfqd->active_cic;
1001 if (!cic || !cic->ioc->task)
1005 * See if this prio level has a good candidate
1007 if (cfq_close_cooperator(cfqd, cfqq))
1010 cfq_mark_cfqq_must_dispatch(cfqq);
1011 cfq_mark_cfqq_wait_request(cfqq);
1014 * we don't want to idle for seeks, but we do want to allow
1015 * fair distribution of slice time for a process doing back-to-back
1016 * seeks. so allow a little bit of time for him to submit a new rq
1018 sl = cfqd->cfq_slice_idle;
1019 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
1020 sl = min(sl, msecs_to_jiffies(2));
1022 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
1025 static void cfq_dispatch_insert(request_queue_t *q, struct request *rq)
1027 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1029 cfq_remove_request(rq);
1031 elv_dispatch_sort(q, rq);
1035 * return expired entry, or NULL to just start from scratch in rbtree
1037 static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq)
1039 struct cfq_data *cfqd = cfqq->cfqd;
1043 if (cfq_cfqq_fifo_expire(cfqq))
1046 cfq_mark_cfqq_fifo_expire(cfqq);
1048 if (list_empty(&cfqq->fifo))
1051 fifo = cfq_cfqq_sync(cfqq);
1052 rq = rq_entry_fifo(cfqq->fifo.next);
1054 if (time_before(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo]))
1061 cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1063 const int base_rq = cfqd->cfq_slice_async_rq;
1065 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1067 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1071 * get next queue for service
1073 static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
1075 struct cfq_queue *cfqq;
1077 cfqq = cfqd->active_queue;
1082 * The active queue has run out of time, expire it and select new.
1084 if (cfq_slice_used(cfqq))
1088 * The active queue has requests and isn't expired, allow it to
1091 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
1095 * No requests pending. If the active queue still has requests in
1096 * flight or is idling for a new request, allow either of these
1097 * conditions to happen (or time out) before selecting a new queue.
1099 if (cfqq->dispatched || timer_pending(&cfqd->idle_slice_timer)) {
1105 cfq_slice_expired(cfqd, 0, 0);
1107 cfqq = cfq_set_active_queue(cfqd);
1113 __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1118 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
1124 * follow expired path, else get first next available
1126 if ((rq = cfq_check_fifo(cfqq)) == NULL)
1130 * finally, insert request into driver dispatch list
1132 cfq_dispatch_insert(cfqd->queue, rq);
1134 cfqd->dispatch_slice++;
1137 if (!cfqd->active_cic) {
1138 atomic_inc(&RQ_CIC(rq)->ioc->refcount);
1139 cfqd->active_cic = RQ_CIC(rq);
1142 if (RB_EMPTY_ROOT(&cfqq->sort_list))
1145 } while (dispatched < max_dispatch);
1148 * expire an async queue immediately if it has used up its slice. idle
1149 * queue always expire after 1 dispatch round.
1151 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
1152 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1153 cfq_class_idle(cfqq))) {
1154 cfqq->slice_end = jiffies + 1;
1155 cfq_slice_expired(cfqd, 0, 0);
1162 cfq_forced_dispatch_cfqqs(struct list_head *list)
1164 struct cfq_queue *cfqq, *next;
1168 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
1169 while (cfqq->next_rq) {
1170 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
1173 BUG_ON(!list_empty(&cfqq->fifo));
1180 cfq_forced_dispatch(struct cfq_data *cfqd)
1182 int i, dispatched = 0;
1184 for (i = 0; i < CFQ_PRIO_LISTS; i++)
1185 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
1187 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
1188 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
1189 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
1191 cfq_slice_expired(cfqd, 0, 0);
1193 BUG_ON(cfqd->busy_queues);
1199 cfq_dispatch_requests(request_queue_t *q, int force)
1201 struct cfq_data *cfqd = q->elevator->elevator_data;
1202 struct cfq_queue *cfqq;
1205 if (!cfqd->busy_queues)
1208 if (unlikely(force))
1209 return cfq_forced_dispatch(cfqd);
1212 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
1215 if (cfqd->busy_queues > 1) {
1217 * So we have dispatched before in this round, if the
1218 * next queue has idling enabled (must be sync), don't
1219 * allow it service until the previous have completed.
1221 if (cfqd->rq_in_driver && cfq_cfqq_idle_window(cfqq) &&
1224 if (cfqq->dispatched >= cfqd->cfq_quantum)
1228 cfq_clear_cfqq_must_dispatch(cfqq);
1229 cfq_clear_cfqq_wait_request(cfqq);
1230 del_timer(&cfqd->idle_slice_timer);
1232 max_dispatch = cfqd->cfq_quantum;
1233 if (cfq_class_idle(cfqq))
1236 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1243 * task holds one reference to the queue, dropped when task exits. each rq
1244 * in-flight on this queue also holds a reference, dropped when rq is freed.
1246 * queue lock must be held here.
1248 static void cfq_put_queue(struct cfq_queue *cfqq)
1250 struct cfq_data *cfqd = cfqq->cfqd;
1252 BUG_ON(atomic_read(&cfqq->ref) <= 0);
1254 if (!atomic_dec_and_test(&cfqq->ref))
1257 BUG_ON(rb_first(&cfqq->sort_list));
1258 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
1259 BUG_ON(cfq_cfqq_on_rr(cfqq));
1261 if (unlikely(cfqd->active_queue == cfqq)) {
1262 __cfq_slice_expired(cfqd, cfqq, 0, 0);
1263 cfq_schedule_dispatch(cfqd);
1267 * it's on the empty list and still hashed
1269 list_del(&cfqq->cfq_list);
1270 hlist_del(&cfqq->cfq_hash);
1271 kmem_cache_free(cfq_pool, cfqq);
1274 static struct cfq_queue *
1275 __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1278 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1279 struct hlist_node *entry;
1280 struct cfq_queue *__cfqq;
1282 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
1283 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
1285 if (__cfqq->key == key && (__p == prio || !prio))
1292 static struct cfq_queue *
1293 cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
1295 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
1298 static void cfq_free_io_context(struct io_context *ioc)
1300 struct cfq_io_context *__cic;
1304 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1305 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1306 rb_erase(&__cic->rb_node, &ioc->cic_root);
1307 kmem_cache_free(cfq_ioc_pool, __cic);
1311 elv_ioc_count_mod(ioc_count, -freed);
1313 if (ioc_gone && !elv_ioc_count_read(ioc_count))
1317 static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1319 if (unlikely(cfqq == cfqd->active_queue)) {
1320 __cfq_slice_expired(cfqd, cfqq, 0, 0);
1321 cfq_schedule_dispatch(cfqd);
1324 cfq_put_queue(cfqq);
1327 static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1328 struct cfq_io_context *cic)
1330 list_del_init(&cic->queue_list);
1334 if (cic->cfqq[ASYNC]) {
1335 cfq_exit_cfqq(cfqd, cic->cfqq[ASYNC]);
1336 cic->cfqq[ASYNC] = NULL;
1339 if (cic->cfqq[SYNC]) {
1340 cfq_exit_cfqq(cfqd, cic->cfqq[SYNC]);
1341 cic->cfqq[SYNC] = NULL;
1347 * Called with interrupts disabled
1349 static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1351 struct cfq_data *cfqd = cic->key;
1354 request_queue_t *q = cfqd->queue;
1356 spin_lock_irq(q->queue_lock);
1357 __cfq_exit_single_io_context(cfqd, cic);
1358 spin_unlock_irq(q->queue_lock);
1362 static void cfq_exit_io_context(struct io_context *ioc)
1364 struct cfq_io_context *__cic;
1368 * put the reference this task is holding to the various queues
1371 n = rb_first(&ioc->cic_root);
1373 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1375 cfq_exit_single_io_context(__cic);
1380 static struct cfq_io_context *
1381 cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
1383 struct cfq_io_context *cic;
1385 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask, cfqd->queue->node);
1387 memset(cic, 0, sizeof(*cic));
1388 cic->last_end_request = jiffies;
1389 INIT_LIST_HEAD(&cic->queue_list);
1390 cic->dtor = cfq_free_io_context;
1391 cic->exit = cfq_exit_io_context;
1392 elv_ioc_count_inc(ioc_count);
1398 static void cfq_init_prio_data(struct cfq_queue *cfqq)
1400 struct task_struct *tsk = current;
1403 if (!cfq_cfqq_prio_changed(cfqq))
1406 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1407 switch (ioprio_class) {
1409 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1410 case IOPRIO_CLASS_NONE:
1412 * no prio set, place us in the middle of the BE classes
1414 cfqq->ioprio = task_nice_ioprio(tsk);
1415 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1417 case IOPRIO_CLASS_RT:
1418 cfqq->ioprio = task_ioprio(tsk);
1419 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1421 case IOPRIO_CLASS_BE:
1422 cfqq->ioprio = task_ioprio(tsk);
1423 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1425 case IOPRIO_CLASS_IDLE:
1426 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1428 cfq_clear_cfqq_idle_window(cfqq);
1433 * keep track of original prio settings in case we have to temporarily
1434 * elevate the priority of this queue
1436 cfqq->org_ioprio = cfqq->ioprio;
1437 cfqq->org_ioprio_class = cfqq->ioprio_class;
1439 cfq_resort_rr_list(cfqq, 0);
1440 cfq_clear_cfqq_prio_changed(cfqq);
1443 static inline void changed_ioprio(struct cfq_io_context *cic)
1445 struct cfq_data *cfqd = cic->key;
1446 struct cfq_queue *cfqq;
1447 unsigned long flags;
1449 if (unlikely(!cfqd))
1452 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1454 cfqq = cic->cfqq[ASYNC];
1456 struct cfq_queue *new_cfqq;
1457 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1460 cic->cfqq[ASYNC] = new_cfqq;
1461 cfq_put_queue(cfqq);
1465 cfqq = cic->cfqq[SYNC];
1467 cfq_mark_cfqq_prio_changed(cfqq);
1469 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1472 static void cfq_ioc_set_ioprio(struct io_context *ioc)
1474 struct cfq_io_context *cic;
1477 ioc->ioprio_changed = 0;
1479 n = rb_first(&ioc->cic_root);
1481 cic = rb_entry(n, struct cfq_io_context, rb_node);
1483 changed_ioprio(cic);
1488 static struct cfq_queue *
1489 cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
1492 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1493 struct cfq_queue *cfqq, *new_cfqq = NULL;
1494 unsigned short ioprio;
1497 ioprio = tsk->ioprio;
1498 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
1504 } else if (gfp_mask & __GFP_WAIT) {
1506 * Inform the allocator of the fact that we will
1507 * just repeat this allocation if it fails, to allow
1508 * the allocator to do whatever it needs to attempt to
1511 spin_unlock_irq(cfqd->queue->queue_lock);
1512 new_cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask|__GFP_NOFAIL, cfqd->queue->node);
1513 spin_lock_irq(cfqd->queue->queue_lock);
1516 cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask, cfqd->queue->node);
1521 memset(cfqq, 0, sizeof(*cfqq));
1523 INIT_HLIST_NODE(&cfqq->cfq_hash);
1524 INIT_LIST_HEAD(&cfqq->cfq_list);
1525 INIT_LIST_HEAD(&cfqq->fifo);
1528 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1529 atomic_set(&cfqq->ref, 0);
1532 if (key != CFQ_KEY_ASYNC)
1533 cfq_mark_cfqq_idle_window(cfqq);
1535 cfq_mark_cfqq_prio_changed(cfqq);
1536 cfq_mark_cfqq_queue_new(cfqq);
1537 cfq_init_prio_data(cfqq);
1541 kmem_cache_free(cfq_pool, new_cfqq);
1543 atomic_inc(&cfqq->ref);
1545 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1550 cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1552 WARN_ON(!list_empty(&cic->queue_list));
1553 rb_erase(&cic->rb_node, &ioc->cic_root);
1554 kmem_cache_free(cfq_ioc_pool, cic);
1555 elv_ioc_count_dec(ioc_count);
1558 static struct cfq_io_context *
1559 cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1562 struct cfq_io_context *cic;
1563 void *k, *key = cfqd;
1566 n = ioc->cic_root.rb_node;
1568 cic = rb_entry(n, struct cfq_io_context, rb_node);
1569 /* ->key must be copied to avoid race with cfq_exit_queue() */
1572 cfq_drop_dead_cic(ioc, cic);
1588 cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1589 struct cfq_io_context *cic)
1592 struct rb_node *parent;
1593 struct cfq_io_context *__cic;
1594 unsigned long flags;
1602 p = &ioc->cic_root.rb_node;
1605 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
1606 /* ->key must be copied to avoid race with cfq_exit_queue() */
1609 cfq_drop_dead_cic(ioc, __cic);
1615 else if (cic->key > k)
1616 p = &(*p)->rb_right;
1621 rb_link_node(&cic->rb_node, parent, p);
1622 rb_insert_color(&cic->rb_node, &ioc->cic_root);
1624 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1625 list_add(&cic->queue_list, &cfqd->cic_list);
1626 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1630 * Setup general io context and cfq io context. There can be several cfq
1631 * io contexts per general io context, if this process is doing io to more
1632 * than one device managed by cfq.
1634 static struct cfq_io_context *
1635 cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
1637 struct io_context *ioc = NULL;
1638 struct cfq_io_context *cic;
1640 might_sleep_if(gfp_mask & __GFP_WAIT);
1642 ioc = get_io_context(gfp_mask, cfqd->queue->node);
1646 cic = cfq_cic_rb_lookup(cfqd, ioc);
1650 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1654 cfq_cic_link(cfqd, ioc, cic);
1656 smp_read_barrier_depends();
1657 if (unlikely(ioc->ioprio_changed))
1658 cfq_ioc_set_ioprio(ioc);
1662 put_io_context(ioc);
1667 cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1669 unsigned long elapsed = jiffies - cic->last_end_request;
1670 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1672 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1673 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1674 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1678 cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
1684 if (cic->last_request_pos < rq->sector)
1685 sdist = rq->sector - cic->last_request_pos;
1687 sdist = cic->last_request_pos - rq->sector;
1689 if (!cic->seek_samples) {
1690 cfqd->new_seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1691 cfqd->new_seek_mean = cfqd->new_seek_total / 256;
1695 * Don't allow the seek distance to get too large from the
1696 * odd fragment, pagein, etc
1698 if (cic->seek_samples <= 60) /* second&third seek */
1699 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1701 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1703 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1704 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1705 total = cic->seek_total + (cic->seek_samples/2);
1706 do_div(total, cic->seek_samples);
1707 cic->seek_mean = (sector_t)total;
1711 * Disable idle window if the process thinks too long or seeks so much that
1715 cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1716 struct cfq_io_context *cic)
1718 int enable_idle = cfq_cfqq_idle_window(cfqq);
1720 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1721 (cfqd->hw_tag && CIC_SEEKY(cic)))
1723 else if (sample_valid(cic->ttime_samples)) {
1724 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1731 cfq_mark_cfqq_idle_window(cfqq);
1733 cfq_clear_cfqq_idle_window(cfqq);
1737 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1738 * no or if we aren't sure, a 1 will cause a preempt.
1741 cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1744 struct cfq_queue *cfqq;
1746 cfqq = cfqd->active_queue;
1750 if (cfq_slice_used(cfqq))
1753 if (cfq_class_idle(new_cfqq))
1756 if (cfq_class_idle(cfqq))
1760 * if the new request is sync, but the currently running queue is
1761 * not, let the sync request have priority.
1763 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
1767 * So both queues are sync. Let the new request get disk time if
1768 * it's a metadata request and the current queue is doing regular IO.
1770 if (rq_is_meta(rq) && !cfqq->meta_pending)
1773 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
1777 * if this request is as-good as one we would expect from the
1778 * current cfqq, let it preempt
1780 if (cfq_rq_close(cfqd, rq))
1787 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1788 * let it have half of its nominal slice.
1790 static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1792 cfq_slice_expired(cfqd, 1, 1);
1795 * Put the new queue at the front of the of the current list,
1796 * so we know that it will be selected next.
1798 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1799 list_move(&cfqq->cfq_list, &cfqd->cur_rr);
1801 cfqq->slice_end = 0;
1802 cfq_mark_cfqq_slice_new(cfqq);
1806 * Called when a new fs request (rq) is added (to cfqq). Check if there's
1807 * something we should do about it
1810 cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1813 struct cfq_io_context *cic = RQ_CIC(rq);
1816 cfqq->meta_pending++;
1818 cfq_update_io_thinktime(cfqd, cic);
1819 cfq_update_io_seektime(cfqd, cic, rq);
1820 cfq_update_idle_window(cfqd, cfqq, cic);
1822 cic->last_request_pos = rq->sector + rq->nr_sectors;
1823 cfqq->last_request_pos = cic->last_request_pos;
1825 if (cfqq == cfqd->active_queue) {
1827 * if we are waiting for a request for this queue, let it rip
1828 * immediately and flag that we must not expire this queue
1831 if (cfq_cfqq_wait_request(cfqq)) {
1832 cfq_mark_cfqq_must_dispatch(cfqq);
1833 del_timer(&cfqd->idle_slice_timer);
1834 blk_start_queueing(cfqd->queue);
1836 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
1838 * not the active queue - expire current slice if it is
1839 * idle and has expired it's mean thinktime or this new queue
1840 * has some old slice time left and is of higher priority
1842 cfq_preempt_queue(cfqd, cfqq);
1843 cfq_mark_cfqq_must_dispatch(cfqq);
1844 blk_start_queueing(cfqd->queue);
1848 static void cfq_insert_request(request_queue_t *q, struct request *rq)
1850 struct cfq_data *cfqd = q->elevator->elevator_data;
1851 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1853 cfq_init_prio_data(cfqq);
1857 list_add_tail(&rq->queuelist, &cfqq->fifo);
1859 cfq_rq_enqueued(cfqd, cfqq, rq);
1862 static void cfq_completed_request(request_queue_t *q, struct request *rq)
1864 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1865 struct cfq_data *cfqd = cfqq->cfqd;
1866 const int sync = rq_is_sync(rq);
1871 WARN_ON(!cfqd->rq_in_driver);
1872 WARN_ON(!cfqq->dispatched);
1873 cfqd->rq_in_driver--;
1875 cfqq->service_last = now;
1877 if (!cfq_class_idle(cfqq))
1878 cfqd->last_end_request = now;
1880 cfq_resort_rr_list(cfqq, 0);
1883 RQ_CIC(rq)->last_end_request = now;
1886 * If this is the active queue, check if it needs to be expired,
1887 * or if we want to idle in case it has no pending requests.
1889 if (cfqd->active_queue == cfqq) {
1890 if (cfq_cfqq_slice_new(cfqq)) {
1891 cfq_set_prio_slice(cfqd, cfqq);
1892 cfq_clear_cfqq_slice_new(cfqq);
1894 if (cfq_slice_used(cfqq))
1895 cfq_slice_expired(cfqd, 0, 1);
1896 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list))
1897 cfq_arm_slice_timer(cfqd);
1900 if (!cfqd->rq_in_driver)
1901 cfq_schedule_dispatch(cfqd);
1905 * we temporarily boost lower priority queues if they are holding fs exclusive
1906 * resources. they are boosted to normal prio (CLASS_BE/4)
1908 static void cfq_prio_boost(struct cfq_queue *cfqq)
1910 const int ioprio_class = cfqq->ioprio_class;
1911 const int ioprio = cfqq->ioprio;
1913 if (has_fs_excl()) {
1915 * boost idle prio on transactions that would lock out other
1916 * users of the filesystem
1918 if (cfq_class_idle(cfqq))
1919 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1920 if (cfqq->ioprio > IOPRIO_NORM)
1921 cfqq->ioprio = IOPRIO_NORM;
1924 * check if we need to unboost the queue
1926 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1927 cfqq->ioprio_class = cfqq->org_ioprio_class;
1928 if (cfqq->ioprio != cfqq->org_ioprio)
1929 cfqq->ioprio = cfqq->org_ioprio;
1933 * refile between round-robin lists if we moved the priority class
1935 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio))
1936 cfq_resort_rr_list(cfqq, 0);
1939 static inline int __cfq_may_queue(struct cfq_queue *cfqq)
1941 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
1942 !cfq_cfqq_must_alloc_slice(cfqq)) {
1943 cfq_mark_cfqq_must_alloc_slice(cfqq);
1944 return ELV_MQUEUE_MUST;
1947 return ELV_MQUEUE_MAY;
1950 static int cfq_may_queue(request_queue_t *q, int rw)
1952 struct cfq_data *cfqd = q->elevator->elevator_data;
1953 struct task_struct *tsk = current;
1954 struct cfq_queue *cfqq;
1957 key = cfq_queue_pid(tsk, rw, rw & REQ_RW_SYNC);
1960 * don't force setup of a queue from here, as a call to may_queue
1961 * does not necessarily imply that a request actually will be queued.
1962 * so just lookup a possibly existing queue, or return 'may queue'
1965 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
1967 cfq_init_prio_data(cfqq);
1968 cfq_prio_boost(cfqq);
1970 return __cfq_may_queue(cfqq);
1973 return ELV_MQUEUE_MAY;
1977 * queue lock held here
1979 static void cfq_put_request(struct request *rq)
1981 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1984 const int rw = rq_data_dir(rq);
1986 BUG_ON(!cfqq->allocated[rw]);
1987 cfqq->allocated[rw]--;
1989 put_io_context(RQ_CIC(rq)->ioc);
1991 rq->elevator_private = NULL;
1992 rq->elevator_private2 = NULL;
1994 cfq_put_queue(cfqq);
1999 * Allocate cfq data structures associated with this request.
2002 cfq_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
2004 struct cfq_data *cfqd = q->elevator->elevator_data;
2005 struct task_struct *tsk = current;
2006 struct cfq_io_context *cic;
2007 const int rw = rq_data_dir(rq);
2008 const int is_sync = rq_is_sync(rq);
2009 pid_t key = cfq_queue_pid(tsk, rw, is_sync);
2010 struct cfq_queue *cfqq;
2011 unsigned long flags;
2013 might_sleep_if(gfp_mask & __GFP_WAIT);
2015 cic = cfq_get_io_context(cfqd, gfp_mask);
2017 spin_lock_irqsave(q->queue_lock, flags);
2022 if (!cic->cfqq[is_sync]) {
2023 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
2027 cic->cfqq[is_sync] = cfqq;
2029 cfqq = cic->cfqq[is_sync];
2031 cfqq->allocated[rw]++;
2032 cfq_clear_cfqq_must_alloc(cfqq);
2033 atomic_inc(&cfqq->ref);
2035 spin_unlock_irqrestore(q->queue_lock, flags);
2037 rq->elevator_private = cic;
2038 rq->elevator_private2 = cfqq;
2043 put_io_context(cic->ioc);
2045 cfq_schedule_dispatch(cfqd);
2046 spin_unlock_irqrestore(q->queue_lock, flags);
2050 static void cfq_kick_queue(struct work_struct *work)
2052 struct cfq_data *cfqd =
2053 container_of(work, struct cfq_data, unplug_work);
2054 request_queue_t *q = cfqd->queue;
2055 unsigned long flags;
2057 spin_lock_irqsave(q->queue_lock, flags);
2058 blk_start_queueing(q);
2059 spin_unlock_irqrestore(q->queue_lock, flags);
2063 * Timer running if the active_queue is currently idling inside its time slice
2065 static void cfq_idle_slice_timer(unsigned long data)
2067 struct cfq_data *cfqd = (struct cfq_data *) data;
2068 struct cfq_queue *cfqq;
2069 unsigned long flags;
2072 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2074 if ((cfqq = cfqd->active_queue) != NULL) {
2080 if (cfq_slice_used(cfqq))
2084 * only expire and reinvoke request handler, if there are
2085 * other queues with pending requests
2087 if (!cfqd->busy_queues)
2091 * not expired and it has a request pending, let it dispatch
2093 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
2094 cfq_mark_cfqq_must_dispatch(cfqq);
2099 cfq_slice_expired(cfqd, 0, timed_out);
2101 cfq_schedule_dispatch(cfqd);
2103 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2107 * Timer running if an idle class queue is waiting for service
2109 static void cfq_idle_class_timer(unsigned long data)
2111 struct cfq_data *cfqd = (struct cfq_data *) data;
2112 unsigned long flags, end;
2114 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2117 * race with a non-idle queue, reset timer
2119 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
2120 if (!time_after_eq(jiffies, end))
2121 mod_timer(&cfqd->idle_class_timer, end);
2123 cfq_schedule_dispatch(cfqd);
2125 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2128 static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2130 del_timer_sync(&cfqd->idle_slice_timer);
2131 del_timer_sync(&cfqd->idle_class_timer);
2132 blk_sync_queue(cfqd->queue);
2135 static void cfq_exit_queue(elevator_t *e)
2137 struct cfq_data *cfqd = e->elevator_data;
2138 request_queue_t *q = cfqd->queue;
2140 cfq_shutdown_timer_wq(cfqd);
2142 spin_lock_irq(q->queue_lock);
2144 if (cfqd->active_queue)
2145 __cfq_slice_expired(cfqd, cfqd->active_queue, 0, 0);
2147 while (!list_empty(&cfqd->cic_list)) {
2148 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2149 struct cfq_io_context,
2152 __cfq_exit_single_io_context(cfqd, cic);
2155 spin_unlock_irq(q->queue_lock);
2157 cfq_shutdown_timer_wq(cfqd);
2159 kfree(cfqd->cfq_hash);
2163 static void *cfq_init_queue(request_queue_t *q)
2165 struct cfq_data *cfqd;
2168 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
2172 memset(cfqd, 0, sizeof(*cfqd));
2174 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2175 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2177 INIT_LIST_HEAD(&cfqd->busy_rr);
2178 INIT_LIST_HEAD(&cfqd->cur_rr);
2179 INIT_LIST_HEAD(&cfqd->idle_rr);
2180 INIT_LIST_HEAD(&cfqd->cic_list);
2182 cfqd->cfq_hash = kmalloc_node(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL, q->node);
2183 if (!cfqd->cfq_hash)
2186 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2187 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2191 init_timer(&cfqd->idle_slice_timer);
2192 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2193 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2195 init_timer(&cfqd->idle_class_timer);
2196 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2197 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2199 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
2201 cfqd->cfq_quantum = cfq_quantum;
2202 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2203 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
2204 cfqd->cfq_back_max = cfq_back_max;
2205 cfqd->cfq_back_penalty = cfq_back_penalty;
2206 cfqd->cfq_slice[0] = cfq_slice_async;
2207 cfqd->cfq_slice[1] = cfq_slice_sync;
2208 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2209 cfqd->cfq_slice_idle = cfq_slice_idle;
2217 static void cfq_slab_kill(void)
2220 kmem_cache_destroy(cfq_pool);
2222 kmem_cache_destroy(cfq_ioc_pool);
2225 static int __init cfq_slab_setup(void)
2227 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2232 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2233 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2244 * sysfs parts below -->
2247 cfq_var_show(unsigned int var, char *page)
2249 return sprintf(page, "%d\n", var);
2253 cfq_var_store(unsigned int *var, const char *page, size_t count)
2255 char *p = (char *) page;
2257 *var = simple_strtoul(p, &p, 10);
2261 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2262 static ssize_t __FUNC(elevator_t *e, char *page) \
2264 struct cfq_data *cfqd = e->elevator_data; \
2265 unsigned int __data = __VAR; \
2267 __data = jiffies_to_msecs(__data); \
2268 return cfq_var_show(__data, (page)); \
2270 SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2271 SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2272 SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
2273 SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2274 SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
2275 SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2276 SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2277 SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2278 SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2279 #undef SHOW_FUNCTION
2281 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2282 static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
2284 struct cfq_data *cfqd = e->elevator_data; \
2285 unsigned int __data; \
2286 int ret = cfq_var_store(&__data, (page), count); \
2287 if (__data < (MIN)) \
2289 else if (__data > (MAX)) \
2292 *(__PTR) = msecs_to_jiffies(__data); \
2294 *(__PTR) = __data; \
2297 STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2298 STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2299 STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
2300 STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2301 STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
2302 STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2303 STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2304 STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2305 STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
2306 #undef STORE_FUNCTION
2308 #define CFQ_ATTR(name) \
2309 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
2311 static struct elv_fs_entry cfq_attrs[] = {
2313 CFQ_ATTR(fifo_expire_sync),
2314 CFQ_ATTR(fifo_expire_async),
2315 CFQ_ATTR(back_seek_max),
2316 CFQ_ATTR(back_seek_penalty),
2317 CFQ_ATTR(slice_sync),
2318 CFQ_ATTR(slice_async),
2319 CFQ_ATTR(slice_async_rq),
2320 CFQ_ATTR(slice_idle),
2324 static struct elevator_type iosched_cfq = {
2326 .elevator_merge_fn = cfq_merge,
2327 .elevator_merged_fn = cfq_merged_request,
2328 .elevator_merge_req_fn = cfq_merged_requests,
2329 .elevator_allow_merge_fn = cfq_allow_merge,
2330 .elevator_dispatch_fn = cfq_dispatch_requests,
2331 .elevator_add_req_fn = cfq_insert_request,
2332 .elevator_activate_req_fn = cfq_activate_request,
2333 .elevator_deactivate_req_fn = cfq_deactivate_request,
2334 .elevator_queue_empty_fn = cfq_queue_empty,
2335 .elevator_completed_req_fn = cfq_completed_request,
2336 .elevator_former_req_fn = elv_rb_former_request,
2337 .elevator_latter_req_fn = elv_rb_latter_request,
2338 .elevator_set_req_fn = cfq_set_request,
2339 .elevator_put_req_fn = cfq_put_request,
2340 .elevator_may_queue_fn = cfq_may_queue,
2341 .elevator_init_fn = cfq_init_queue,
2342 .elevator_exit_fn = cfq_exit_queue,
2343 .trim = cfq_free_io_context,
2345 .elevator_attrs = cfq_attrs,
2346 .elevator_name = "cfq",
2347 .elevator_owner = THIS_MODULE,
2350 static int __init cfq_init(void)
2355 * could be 0 on HZ < 1000 setups
2357 if (!cfq_slice_async)
2358 cfq_slice_async = 1;
2359 if (!cfq_slice_idle)
2362 if (cfq_slab_setup())
2365 ret = elv_register(&iosched_cfq);
2372 static void __exit cfq_exit(void)
2374 DECLARE_COMPLETION_ONSTACK(all_gone);
2375 elv_unregister(&iosched_cfq);
2376 ioc_gone = &all_gone;
2377 /* ioc_gone's update must be visible before reading ioc_count */
2379 if (elv_ioc_count_read(ioc_count))
2380 wait_for_completion(ioc_gone);
2385 module_init(cfq_init);
2386 module_exit(cfq_exit);
2388 MODULE_AUTHOR("Jens Axboe");
2389 MODULE_LICENSE("GPL");
2390 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");