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@suse.de>
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_queued = 8; /* minimum rq allocate limit per-queue*/
21 static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
22 static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
23 static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
25 static const int cfq_slice_sync = HZ / 10;
26 static int cfq_slice_async = HZ / 25;
27 static const int cfq_slice_async_rq = 2;
28 static int cfq_slice_idle = HZ / 125;
30 #define CFQ_IDLE_GRACE (HZ / 10)
31 #define CFQ_SLICE_SCALE (5)
33 #define CFQ_KEY_ASYNC (0)
35 static DEFINE_SPINLOCK(cfq_exit_lock);
38 * for the hash of cfqq inside the cfqd
40 #define CFQ_QHASH_SHIFT 6
41 #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
42 #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
44 #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
45 #define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
47 #define RQ_DATA(rq) (rq)->elevator_private
52 #define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
53 #define rq_rb_key(rq) (rq)->sector
55 static kmem_cache_t *crq_pool;
56 static kmem_cache_t *cfq_pool;
57 static kmem_cache_t *cfq_ioc_pool;
59 static atomic_t ioc_count = ATOMIC_INIT(0);
60 static struct completion *ioc_gone;
62 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
63 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
64 #define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
65 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
70 #define cfq_cfqq_dispatched(cfqq) \
71 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
73 #define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
75 #define cfq_cfqq_sync(cfqq) \
76 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
78 #define sample_valid(samples) ((samples) > 80)
81 * Per block device queue structure
84 request_queue_t *queue;
87 * rr list of queues with requests and the count of them
89 struct list_head rr_list[CFQ_PRIO_LISTS];
90 struct list_head busy_rr;
91 struct list_head cur_rr;
92 struct list_head idle_rr;
93 unsigned int busy_queues;
96 * non-ordered list of empty cfqq's
98 struct list_head empty_list;
103 struct hlist_head *cfq_hash;
111 * schedule slice state info
114 * idle window management
116 struct timer_list idle_slice_timer;
117 struct work_struct unplug_work;
119 struct cfq_queue *active_queue;
120 struct cfq_io_context *active_cic;
121 int cur_prio, cur_end_prio;
122 unsigned int dispatch_slice;
124 struct timer_list idle_class_timer;
126 sector_t last_sector;
127 unsigned long last_end_request;
129 unsigned int rq_starved;
132 * tunables, see top of file
134 unsigned int cfq_quantum;
135 unsigned int cfq_queued;
136 unsigned int cfq_fifo_expire[2];
137 unsigned int cfq_back_penalty;
138 unsigned int cfq_back_max;
139 unsigned int cfq_slice[2];
140 unsigned int cfq_slice_async_rq;
141 unsigned int cfq_slice_idle;
143 struct list_head cic_list;
147 * Per process-grouping structure
150 /* reference count */
152 /* parent cfq_data */
153 struct cfq_data *cfqd;
154 /* cfqq lookup hash */
155 struct hlist_node cfq_hash;
158 /* on either rr or empty list of cfqd */
159 struct list_head cfq_list;
160 /* sorted list of pending requests */
161 struct rb_root sort_list;
162 /* if fifo isn't expired, next request to serve */
163 struct cfq_rq *next_crq;
164 /* requests queued in sort_list */
166 /* currently allocated requests */
168 /* fifo list of requests in sort_list */
169 struct list_head fifo;
171 unsigned long slice_start;
172 unsigned long slice_end;
173 unsigned long slice_left;
174 unsigned long service_last;
176 /* number of requests that are on the dispatch list */
179 /* io prio of this group */
180 unsigned short ioprio, org_ioprio;
181 unsigned short ioprio_class, org_ioprio_class;
183 /* various state flags, see below */
188 struct rb_node rb_node;
190 struct request *request;
192 struct cfq_queue *cfq_queue;
193 struct cfq_io_context *io_context;
195 unsigned int crq_flags;
198 enum cfqq_state_flags {
199 CFQ_CFQQ_FLAG_on_rr = 0,
200 CFQ_CFQQ_FLAG_wait_request,
201 CFQ_CFQQ_FLAG_must_alloc,
202 CFQ_CFQQ_FLAG_must_alloc_slice,
203 CFQ_CFQQ_FLAG_must_dispatch,
204 CFQ_CFQQ_FLAG_fifo_expire,
205 CFQ_CFQQ_FLAG_idle_window,
206 CFQ_CFQQ_FLAG_prio_changed,
209 #define CFQ_CFQQ_FNS(name) \
210 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
212 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
214 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
216 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
218 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
220 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
224 CFQ_CFQQ_FNS(wait_request);
225 CFQ_CFQQ_FNS(must_alloc);
226 CFQ_CFQQ_FNS(must_alloc_slice);
227 CFQ_CFQQ_FNS(must_dispatch);
228 CFQ_CFQQ_FNS(fifo_expire);
229 CFQ_CFQQ_FNS(idle_window);
230 CFQ_CFQQ_FNS(prio_changed);
233 enum cfq_rq_state_flags {
234 CFQ_CRQ_FLAG_is_sync = 0,
237 #define CFQ_CRQ_FNS(name) \
238 static inline void cfq_mark_crq_##name(struct cfq_rq *crq) \
240 crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name); \
242 static inline void cfq_clear_crq_##name(struct cfq_rq *crq) \
244 crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name); \
246 static inline int cfq_crq_##name(const struct cfq_rq *crq) \
248 return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0; \
251 CFQ_CRQ_FNS(is_sync);
254 static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
255 static void cfq_dispatch_insert(request_queue_t *, struct cfq_rq *);
256 static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk, gfp_t gfp_mask);
259 * scheduler run of queue, if there are requests pending and no one in the
260 * driver that will restart queueing
262 static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
264 if (cfqd->busy_queues)
265 kblockd_schedule_work(&cfqd->unplug_work);
268 static int cfq_queue_empty(request_queue_t *q)
270 struct cfq_data *cfqd = q->elevator->elevator_data;
272 return !cfqd->busy_queues;
275 static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
277 if (rw == READ || rw == WRITE_SYNC)
280 return CFQ_KEY_ASYNC;
284 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
285 * We choose the request that is closest to the head right now. Distance
286 * behind the head is penalized and only allowed to a certain extent.
288 static struct cfq_rq *
289 cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
291 sector_t last, s1, s2, d1 = 0, d2 = 0;
292 unsigned long back_max;
293 #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
294 #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
295 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
297 if (crq1 == NULL || crq1 == crq2)
302 if (cfq_crq_is_sync(crq1) && !cfq_crq_is_sync(crq2))
304 else if (cfq_crq_is_sync(crq2) && !cfq_crq_is_sync(crq1))
307 s1 = crq1->request->sector;
308 s2 = crq2->request->sector;
310 last = cfqd->last_sector;
313 * by definition, 1KiB is 2 sectors
315 back_max = cfqd->cfq_back_max * 2;
318 * Strict one way elevator _except_ in the case where we allow
319 * short backward seeks which are biased as twice the cost of a
320 * similar forward seek.
324 else if (s1 + back_max >= last)
325 d1 = (last - s1) * cfqd->cfq_back_penalty;
327 wrap |= CFQ_RQ1_WRAP;
331 else if (s2 + back_max >= last)
332 d2 = (last - s2) * cfqd->cfq_back_penalty;
334 wrap |= CFQ_RQ2_WRAP;
336 /* Found required data */
339 * By doing switch() on the bit mask "wrap" we avoid having to
340 * check two variables for all permutations: --> faster!
343 case 0: /* common case for CFQ: crq1 and crq2 not wrapped */
359 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both crqs wrapped */
362 * Since both rqs are wrapped,
363 * start with the one that's further behind head
364 * (--> only *one* back seek required),
365 * since back seek takes more time than forward.
375 * would be nice to take fifo expire time into account as well
377 static struct cfq_rq *
378 cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
381 struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
382 struct rb_node *rbnext, *rbprev;
384 if (!(rbnext = rb_next(&last->rb_node))) {
385 rbnext = rb_first(&cfqq->sort_list);
386 if (rbnext == &last->rb_node)
390 rbprev = rb_prev(&last->rb_node);
393 crq_prev = rb_entry_crq(rbprev);
395 crq_next = rb_entry_crq(rbnext);
397 return cfq_choose_req(cfqd, crq_next, crq_prev);
400 static void cfq_update_next_crq(struct cfq_rq *crq)
402 struct cfq_queue *cfqq = crq->cfq_queue;
404 if (cfqq->next_crq == crq)
405 cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
408 static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
410 struct cfq_data *cfqd = cfqq->cfqd;
411 struct list_head *list, *entry;
413 BUG_ON(!cfq_cfqq_on_rr(cfqq));
415 list_del(&cfqq->cfq_list);
417 if (cfq_class_rt(cfqq))
418 list = &cfqd->cur_rr;
419 else if (cfq_class_idle(cfqq))
420 list = &cfqd->idle_rr;
423 * if cfqq has requests in flight, don't allow it to be
424 * found in cfq_set_active_queue before it has finished them.
425 * this is done to increase fairness between a process that
426 * has lots of io pending vs one that only generates one
427 * sporadically or synchronously
429 if (cfq_cfqq_dispatched(cfqq))
430 list = &cfqd->busy_rr;
432 list = &cfqd->rr_list[cfqq->ioprio];
436 * if queue was preempted, just add to front to be fair. busy_rr
437 * isn't sorted, but insert at the back for fairness.
439 if (preempted || list == &cfqd->busy_rr) {
443 list_add_tail(&cfqq->cfq_list, list);
448 * sort by when queue was last serviced
451 while ((entry = entry->prev) != list) {
452 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
454 if (!__cfqq->service_last)
456 if (time_before(__cfqq->service_last, cfqq->service_last))
460 list_add(&cfqq->cfq_list, entry);
464 * add to busy list of queues for service, trying to be fair in ordering
465 * the pending list according to last request service
468 cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
470 BUG_ON(cfq_cfqq_on_rr(cfqq));
471 cfq_mark_cfqq_on_rr(cfqq);
474 cfq_resort_rr_list(cfqq, 0);
478 cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
480 BUG_ON(!cfq_cfqq_on_rr(cfqq));
481 cfq_clear_cfqq_on_rr(cfqq);
482 list_move(&cfqq->cfq_list, &cfqd->empty_list);
484 BUG_ON(!cfqd->busy_queues);
489 * rb tree support functions
491 static inline void cfq_del_crq_rb(struct cfq_rq *crq)
493 struct cfq_queue *cfqq = crq->cfq_queue;
494 struct cfq_data *cfqd = cfqq->cfqd;
495 const int sync = cfq_crq_is_sync(crq);
497 BUG_ON(!cfqq->queued[sync]);
498 cfqq->queued[sync]--;
500 cfq_update_next_crq(crq);
502 rb_erase(&crq->rb_node, &cfqq->sort_list);
504 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
505 cfq_del_cfqq_rr(cfqd, cfqq);
508 static struct cfq_rq *
509 __cfq_add_crq_rb(struct cfq_rq *crq)
511 struct rb_node **p = &crq->cfq_queue->sort_list.rb_node;
512 struct rb_node *parent = NULL;
513 struct cfq_rq *__crq;
517 __crq = rb_entry_crq(parent);
519 if (crq->rb_key < __crq->rb_key)
521 else if (crq->rb_key > __crq->rb_key)
527 rb_link_node(&crq->rb_node, parent, p);
531 static void cfq_add_crq_rb(struct cfq_rq *crq)
533 struct cfq_queue *cfqq = crq->cfq_queue;
534 struct cfq_data *cfqd = cfqq->cfqd;
535 struct request *rq = crq->request;
536 struct cfq_rq *__alias;
538 crq->rb_key = rq_rb_key(rq);
539 cfqq->queued[cfq_crq_is_sync(crq)]++;
542 * looks a little odd, but the first insert might return an alias.
543 * if that happens, put the alias on the dispatch list
545 while ((__alias = __cfq_add_crq_rb(crq)) != NULL)
546 cfq_dispatch_insert(cfqd->queue, __alias);
548 rb_insert_color(&crq->rb_node, &cfqq->sort_list);
550 if (!cfq_cfqq_on_rr(cfqq))
551 cfq_add_cfqq_rr(cfqd, cfqq);
554 * check if this request is a better next-serve candidate
556 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
560 cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
562 rb_erase(&crq->rb_node, &cfqq->sort_list);
563 cfqq->queued[cfq_crq_is_sync(crq)]--;
568 static struct request *
569 cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
571 struct task_struct *tsk = current;
572 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio));
573 struct cfq_queue *cfqq;
577 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
581 sector = bio->bi_sector + bio_sectors(bio);
582 n = cfqq->sort_list.rb_node;
584 struct cfq_rq *crq = rb_entry_crq(n);
586 if (sector < crq->rb_key)
588 else if (sector > crq->rb_key)
598 static void cfq_activate_request(request_queue_t *q, struct request *rq)
600 struct cfq_data *cfqd = q->elevator->elevator_data;
602 cfqd->rq_in_driver++;
605 * If the depth is larger 1, it really could be queueing. But lets
606 * make the mark a little higher - idling could still be good for
607 * low queueing, and a low queueing number could also just indicate
608 * a SCSI mid layer like behaviour where limit+1 is often seen.
610 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
614 static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
616 struct cfq_data *cfqd = q->elevator->elevator_data;
618 WARN_ON(!cfqd->rq_in_driver);
619 cfqd->rq_in_driver--;
622 static void cfq_remove_request(struct request *rq)
624 struct cfq_rq *crq = RQ_DATA(rq);
626 list_del_init(&rq->queuelist);
631 cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
633 struct cfq_data *cfqd = q->elevator->elevator_data;
634 struct request *__rq;
636 __rq = cfq_find_rq_fmerge(cfqd, bio);
637 if (__rq && elv_rq_merge_ok(__rq, bio)) {
639 return ELEVATOR_FRONT_MERGE;
642 return ELEVATOR_NO_MERGE;
645 static void cfq_merged_request(request_queue_t *q, struct request *req)
647 struct cfq_rq *crq = RQ_DATA(req);
649 if (rq_rb_key(req) != crq->rb_key) {
650 struct cfq_queue *cfqq = crq->cfq_queue;
652 cfq_update_next_crq(crq);
653 cfq_reposition_crq_rb(cfqq, crq);
658 cfq_merged_requests(request_queue_t *q, struct request *rq,
659 struct request *next)
661 cfq_merged_request(q, rq);
664 * reposition in fifo if next is older than rq
666 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
667 time_before(next->start_time, rq->start_time))
668 list_move(&rq->queuelist, &next->queuelist);
670 cfq_remove_request(next);
674 __cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
678 * stop potential idle class queues waiting service
680 del_timer(&cfqd->idle_class_timer);
682 cfqq->slice_start = jiffies;
684 cfqq->slice_left = 0;
685 cfq_clear_cfqq_must_alloc_slice(cfqq);
686 cfq_clear_cfqq_fifo_expire(cfqq);
689 cfqd->active_queue = cfqq;
693 * current cfqq expired its slice (or was too idle), select new one
696 __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
699 unsigned long now = jiffies;
701 if (cfq_cfqq_wait_request(cfqq))
702 del_timer(&cfqd->idle_slice_timer);
704 if (!preempted && !cfq_cfqq_dispatched(cfqq)) {
705 cfqq->service_last = now;
706 cfq_schedule_dispatch(cfqd);
709 cfq_clear_cfqq_must_dispatch(cfqq);
710 cfq_clear_cfqq_wait_request(cfqq);
713 * store what was left of this slice, if the queue idled out
716 if (time_after(cfqq->slice_end, now))
717 cfqq->slice_left = cfqq->slice_end - now;
719 cfqq->slice_left = 0;
721 if (cfq_cfqq_on_rr(cfqq))
722 cfq_resort_rr_list(cfqq, preempted);
724 if (cfqq == cfqd->active_queue)
725 cfqd->active_queue = NULL;
727 if (cfqd->active_cic) {
728 put_io_context(cfqd->active_cic->ioc);
729 cfqd->active_cic = NULL;
732 cfqd->dispatch_slice = 0;
735 static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
737 struct cfq_queue *cfqq = cfqd->active_queue;
740 __cfq_slice_expired(cfqd, cfqq, preempted);
753 static int cfq_get_next_prio_level(struct cfq_data *cfqd)
762 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
763 if (!list_empty(&cfqd->rr_list[p])) {
772 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
773 cfqd->cur_end_prio = 0;
780 if (unlikely(prio == -1))
783 BUG_ON(prio >= CFQ_PRIO_LISTS);
785 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
787 cfqd->cur_prio = prio + 1;
788 if (cfqd->cur_prio > cfqd->cur_end_prio) {
789 cfqd->cur_end_prio = cfqd->cur_prio;
792 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
794 cfqd->cur_end_prio = 0;
800 static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
802 struct cfq_queue *cfqq = NULL;
805 * if current list is non-empty, grab first entry. if it is empty,
806 * get next prio level and grab first entry then if any are spliced
808 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
809 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
812 * If no new queues are available, check if the busy list has some
813 * before falling back to idle io.
815 if (!cfqq && !list_empty(&cfqd->busy_rr))
816 cfqq = list_entry_cfqq(cfqd->busy_rr.next);
819 * if we have idle queues and no rt or be queues had pending
820 * requests, either allow immediate service if the grace period
821 * has passed or arm the idle grace timer
823 if (!cfqq && !list_empty(&cfqd->idle_rr)) {
824 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
826 if (time_after_eq(jiffies, end))
827 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
829 mod_timer(&cfqd->idle_class_timer, end);
832 __cfq_set_active_queue(cfqd, cfqq);
836 #define CIC_SEEKY(cic) ((cic)->seek_mean > (128 * 1024))
838 static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
841 struct cfq_io_context *cic;
844 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
845 WARN_ON(cfqq != cfqd->active_queue);
848 * idle is disabled, either manually or by past process history
850 if (!cfqd->cfq_slice_idle)
852 if (!cfq_cfqq_idle_window(cfqq))
855 * task has exited, don't wait
857 cic = cfqd->active_cic;
858 if (!cic || !cic->ioc->task)
861 cfq_mark_cfqq_must_dispatch(cfqq);
862 cfq_mark_cfqq_wait_request(cfqq);
864 sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
867 * we don't want to idle for seeks, but we do want to allow
868 * fair distribution of slice time for a process doing back-to-back
869 * seeks. so allow a little bit of time for him to submit a new rq
871 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
872 sl = min(sl, msecs_to_jiffies(2));
874 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
878 static void cfq_dispatch_insert(request_queue_t *q, struct cfq_rq *crq)
880 struct cfq_data *cfqd = q->elevator->elevator_data;
881 struct cfq_queue *cfqq = crq->cfq_queue;
884 cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
885 cfq_remove_request(crq->request);
886 cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
887 elv_dispatch_sort(q, crq->request);
889 rq = list_entry(q->queue_head.prev, struct request, queuelist);
890 cfqd->last_sector = rq->sector + rq->nr_sectors;
894 * return expired entry, or NULL to just start from scratch in rbtree
896 static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
898 struct cfq_data *cfqd = cfqq->cfqd;
902 if (cfq_cfqq_fifo_expire(cfqq))
905 if (!list_empty(&cfqq->fifo)) {
906 int fifo = cfq_cfqq_class_sync(cfqq);
908 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
910 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
911 cfq_mark_cfqq_fifo_expire(cfqq);
920 * Scale schedule slice based on io priority. Use the sync time slice only
921 * if a queue is marked sync and has sync io queued. A sync queue with async
922 * io only, should not get full sync slice length.
925 cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
927 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
929 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
931 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
935 cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
937 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
941 cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
943 const int base_rq = cfqd->cfq_slice_async_rq;
945 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
947 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
951 * get next queue for service
953 static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
955 unsigned long now = jiffies;
956 struct cfq_queue *cfqq;
958 cfqq = cfqd->active_queue;
965 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
969 * if queue has requests, dispatch one. if not, check if
970 * enough slice is left to wait for one
972 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
974 else if (cfq_cfqq_dispatched(cfqq)) {
977 } else if (cfq_cfqq_class_sync(cfqq)) {
978 if (cfq_arm_slice_timer(cfqd, cfqq))
983 cfq_slice_expired(cfqd, 0);
985 cfqq = cfq_set_active_queue(cfqd);
991 __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
996 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
1002 * follow expired path, else get first next available
1004 if ((crq = cfq_check_fifo(cfqq)) == NULL)
1005 crq = cfqq->next_crq;
1008 * finally, insert request into driver dispatch list
1010 cfq_dispatch_insert(cfqd->queue, crq);
1012 cfqd->dispatch_slice++;
1015 if (!cfqd->active_cic) {
1016 atomic_inc(&crq->io_context->ioc->refcount);
1017 cfqd->active_cic = crq->io_context;
1020 if (RB_EMPTY_ROOT(&cfqq->sort_list))
1023 } while (dispatched < max_dispatch);
1026 * if slice end isn't set yet, set it.
1028 if (!cfqq->slice_end)
1029 cfq_set_prio_slice(cfqd, cfqq);
1032 * expire an async queue immediately if it has used up its slice. idle
1033 * queue always expire after 1 dispatch round.
1035 if ((!cfq_cfqq_sync(cfqq) &&
1036 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1037 cfq_class_idle(cfqq) ||
1038 !cfq_cfqq_idle_window(cfqq))
1039 cfq_slice_expired(cfqd, 0);
1045 cfq_forced_dispatch_cfqqs(struct list_head *list)
1047 struct cfq_queue *cfqq, *next;
1052 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
1053 while ((crq = cfqq->next_crq)) {
1054 cfq_dispatch_insert(cfqq->cfqd->queue, crq);
1057 BUG_ON(!list_empty(&cfqq->fifo));
1064 cfq_forced_dispatch(struct cfq_data *cfqd)
1066 int i, dispatched = 0;
1068 for (i = 0; i < CFQ_PRIO_LISTS; i++)
1069 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
1071 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
1072 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
1073 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
1075 cfq_slice_expired(cfqd, 0);
1077 BUG_ON(cfqd->busy_queues);
1083 cfq_dispatch_requests(request_queue_t *q, int force)
1085 struct cfq_data *cfqd = q->elevator->elevator_data;
1086 struct cfq_queue *cfqq, *prev_cfqq;
1089 if (!cfqd->busy_queues)
1092 if (unlikely(force))
1093 return cfq_forced_dispatch(cfqd);
1097 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
1101 * Don't repeat dispatch from the previous queue.
1103 if (prev_cfqq == cfqq)
1106 cfq_clear_cfqq_must_dispatch(cfqq);
1107 cfq_clear_cfqq_wait_request(cfqq);
1108 del_timer(&cfqd->idle_slice_timer);
1110 max_dispatch = cfqd->cfq_quantum;
1111 if (cfq_class_idle(cfqq))
1114 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1117 * If the dispatch cfqq has idling enabled and is still
1118 * the active queue, break out.
1120 if (cfq_cfqq_idle_window(cfqq) && cfqd->active_queue)
1130 * task holds one reference to the queue, dropped when task exits. each crq
1131 * in-flight on this queue also holds a reference, dropped when crq is freed.
1133 * queue lock must be held here.
1135 static void cfq_put_queue(struct cfq_queue *cfqq)
1137 struct cfq_data *cfqd = cfqq->cfqd;
1139 BUG_ON(atomic_read(&cfqq->ref) <= 0);
1141 if (!atomic_dec_and_test(&cfqq->ref))
1144 BUG_ON(rb_first(&cfqq->sort_list));
1145 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
1146 BUG_ON(cfq_cfqq_on_rr(cfqq));
1148 if (unlikely(cfqd->active_queue == cfqq))
1149 __cfq_slice_expired(cfqd, cfqq, 0);
1152 * it's on the empty list and still hashed
1154 list_del(&cfqq->cfq_list);
1155 hlist_del(&cfqq->cfq_hash);
1156 kmem_cache_free(cfq_pool, cfqq);
1159 static inline struct cfq_queue *
1160 __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1163 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1164 struct hlist_node *entry;
1165 struct cfq_queue *__cfqq;
1167 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
1168 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
1170 if (__cfqq->key == key && (__p == prio || !prio))
1177 static struct cfq_queue *
1178 cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
1180 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
1183 static void cfq_free_io_context(struct io_context *ioc)
1185 struct cfq_io_context *__cic;
1189 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1190 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1191 rb_erase(&__cic->rb_node, &ioc->cic_root);
1192 kmem_cache_free(cfq_ioc_pool, __cic);
1196 if (atomic_sub_and_test(freed, &ioc_count) && ioc_gone)
1200 static void cfq_trim(struct io_context *ioc)
1202 ioc->set_ioprio = NULL;
1203 cfq_free_io_context(ioc);
1207 * Called with interrupts disabled
1209 static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1211 struct cfq_data *cfqd = cic->key;
1219 WARN_ON(!irqs_disabled());
1221 spin_lock(q->queue_lock);
1223 if (cic->cfqq[ASYNC]) {
1224 if (unlikely(cic->cfqq[ASYNC] == cfqd->active_queue))
1225 __cfq_slice_expired(cfqd, cic->cfqq[ASYNC], 0);
1226 cfq_put_queue(cic->cfqq[ASYNC]);
1227 cic->cfqq[ASYNC] = NULL;
1230 if (cic->cfqq[SYNC]) {
1231 if (unlikely(cic->cfqq[SYNC] == cfqd->active_queue))
1232 __cfq_slice_expired(cfqd, cic->cfqq[SYNC], 0);
1233 cfq_put_queue(cic->cfqq[SYNC]);
1234 cic->cfqq[SYNC] = NULL;
1238 list_del_init(&cic->queue_list);
1239 spin_unlock(q->queue_lock);
1242 static void cfq_exit_io_context(struct io_context *ioc)
1244 struct cfq_io_context *__cic;
1245 unsigned long flags;
1249 * put the reference this task is holding to the various queues
1251 spin_lock_irqsave(&cfq_exit_lock, flags);
1253 n = rb_first(&ioc->cic_root);
1255 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1257 cfq_exit_single_io_context(__cic);
1261 spin_unlock_irqrestore(&cfq_exit_lock, flags);
1264 static struct cfq_io_context *
1265 cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
1267 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
1270 memset(cic, 0, sizeof(*cic));
1271 cic->last_end_request = jiffies;
1272 INIT_LIST_HEAD(&cic->queue_list);
1273 cic->dtor = cfq_free_io_context;
1274 cic->exit = cfq_exit_io_context;
1275 atomic_inc(&ioc_count);
1281 static void cfq_init_prio_data(struct cfq_queue *cfqq)
1283 struct task_struct *tsk = current;
1286 if (!cfq_cfqq_prio_changed(cfqq))
1289 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1290 switch (ioprio_class) {
1292 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1293 case IOPRIO_CLASS_NONE:
1295 * no prio set, place us in the middle of the BE classes
1297 cfqq->ioprio = task_nice_ioprio(tsk);
1298 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1300 case IOPRIO_CLASS_RT:
1301 cfqq->ioprio = task_ioprio(tsk);
1302 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1304 case IOPRIO_CLASS_BE:
1305 cfqq->ioprio = task_ioprio(tsk);
1306 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1308 case IOPRIO_CLASS_IDLE:
1309 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1311 cfq_clear_cfqq_idle_window(cfqq);
1316 * keep track of original prio settings in case we have to temporarily
1317 * elevate the priority of this queue
1319 cfqq->org_ioprio = cfqq->ioprio;
1320 cfqq->org_ioprio_class = cfqq->ioprio_class;
1322 if (cfq_cfqq_on_rr(cfqq))
1323 cfq_resort_rr_list(cfqq, 0);
1325 cfq_clear_cfqq_prio_changed(cfqq);
1328 static inline void changed_ioprio(struct cfq_io_context *cic)
1330 struct cfq_data *cfqd = cic->key;
1331 struct cfq_queue *cfqq;
1333 if (unlikely(!cfqd))
1336 spin_lock(cfqd->queue->queue_lock);
1338 cfqq = cic->cfqq[ASYNC];
1340 struct cfq_queue *new_cfqq;
1341 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1344 cic->cfqq[ASYNC] = new_cfqq;
1345 cfq_put_queue(cfqq);
1349 cfqq = cic->cfqq[SYNC];
1351 cfq_mark_cfqq_prio_changed(cfqq);
1353 spin_unlock(cfqd->queue->queue_lock);
1357 * callback from sys_ioprio_set, irqs are disabled
1359 static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
1361 struct cfq_io_context *cic;
1364 spin_lock(&cfq_exit_lock);
1366 n = rb_first(&ioc->cic_root);
1368 cic = rb_entry(n, struct cfq_io_context, rb_node);
1370 changed_ioprio(cic);
1374 spin_unlock(&cfq_exit_lock);
1379 static struct cfq_queue *
1380 cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
1383 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1384 struct cfq_queue *cfqq, *new_cfqq = NULL;
1385 unsigned short ioprio;
1388 ioprio = tsk->ioprio;
1389 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
1395 } else if (gfp_mask & __GFP_WAIT) {
1396 spin_unlock_irq(cfqd->queue->queue_lock);
1397 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1398 spin_lock_irq(cfqd->queue->queue_lock);
1401 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1406 memset(cfqq, 0, sizeof(*cfqq));
1408 INIT_HLIST_NODE(&cfqq->cfq_hash);
1409 INIT_LIST_HEAD(&cfqq->cfq_list);
1410 INIT_LIST_HEAD(&cfqq->fifo);
1413 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1414 atomic_set(&cfqq->ref, 0);
1416 cfqq->service_last = 0;
1418 * set ->slice_left to allow preemption for a new process
1420 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
1421 cfq_mark_cfqq_idle_window(cfqq);
1422 cfq_mark_cfqq_prio_changed(cfqq);
1423 cfq_init_prio_data(cfqq);
1427 kmem_cache_free(cfq_pool, new_cfqq);
1429 atomic_inc(&cfqq->ref);
1431 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1436 cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1438 spin_lock(&cfq_exit_lock);
1439 rb_erase(&cic->rb_node, &ioc->cic_root);
1440 list_del_init(&cic->queue_list);
1441 spin_unlock(&cfq_exit_lock);
1442 kmem_cache_free(cfq_ioc_pool, cic);
1443 atomic_dec(&ioc_count);
1446 static struct cfq_io_context *
1447 cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1450 struct cfq_io_context *cic;
1451 void *k, *key = cfqd;
1454 n = ioc->cic_root.rb_node;
1456 cic = rb_entry(n, struct cfq_io_context, rb_node);
1457 /* ->key must be copied to avoid race with cfq_exit_queue() */
1460 cfq_drop_dead_cic(ioc, cic);
1476 cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1477 struct cfq_io_context *cic)
1480 struct rb_node *parent;
1481 struct cfq_io_context *__cic;
1487 ioc->set_ioprio = cfq_ioc_set_ioprio;
1490 p = &ioc->cic_root.rb_node;
1493 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
1494 /* ->key must be copied to avoid race with cfq_exit_queue() */
1497 cfq_drop_dead_cic(ioc, __cic);
1503 else if (cic->key > k)
1504 p = &(*p)->rb_right;
1509 spin_lock(&cfq_exit_lock);
1510 rb_link_node(&cic->rb_node, parent, p);
1511 rb_insert_color(&cic->rb_node, &ioc->cic_root);
1512 list_add(&cic->queue_list, &cfqd->cic_list);
1513 spin_unlock(&cfq_exit_lock);
1517 * Setup general io context and cfq io context. There can be several cfq
1518 * io contexts per general io context, if this process is doing io to more
1519 * than one device managed by cfq.
1521 static struct cfq_io_context *
1522 cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
1524 struct io_context *ioc = NULL;
1525 struct cfq_io_context *cic;
1527 might_sleep_if(gfp_mask & __GFP_WAIT);
1529 ioc = get_io_context(gfp_mask);
1533 cic = cfq_cic_rb_lookup(cfqd, ioc);
1537 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1541 cfq_cic_link(cfqd, ioc, cic);
1545 put_io_context(ioc);
1550 cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1552 unsigned long elapsed, ttime;
1555 * if this context already has stuff queued, thinktime is from
1556 * last queue not last end
1559 if (time_after(cic->last_end_request, cic->last_queue))
1560 elapsed = jiffies - cic->last_end_request;
1562 elapsed = jiffies - cic->last_queue;
1564 elapsed = jiffies - cic->last_end_request;
1567 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1569 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1570 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1571 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1575 cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
1581 if (cic->last_request_pos < crq->request->sector)
1582 sdist = crq->request->sector - cic->last_request_pos;
1584 sdist = cic->last_request_pos - crq->request->sector;
1587 * Don't allow the seek distance to get too large from the
1588 * odd fragment, pagein, etc
1590 if (cic->seek_samples <= 60) /* second&third seek */
1591 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1593 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1595 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1596 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1597 total = cic->seek_total + (cic->seek_samples/2);
1598 do_div(total, cic->seek_samples);
1599 cic->seek_mean = (sector_t)total;
1603 * Disable idle window if the process thinks too long or seeks so much that
1607 cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1608 struct cfq_io_context *cic)
1610 int enable_idle = cfq_cfqq_idle_window(cfqq);
1612 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1613 (cfqd->hw_tag && CIC_SEEKY(cic)))
1615 else if (sample_valid(cic->ttime_samples)) {
1616 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1623 cfq_mark_cfqq_idle_window(cfqq);
1625 cfq_clear_cfqq_idle_window(cfqq);
1630 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1631 * no or if we aren't sure, a 1 will cause a preempt.
1634 cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1637 struct cfq_queue *cfqq = cfqd->active_queue;
1639 if (cfq_class_idle(new_cfqq))
1645 if (cfq_class_idle(cfqq))
1647 if (!cfq_cfqq_wait_request(new_cfqq))
1650 * if it doesn't have slice left, forget it
1652 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1654 if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
1661 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1662 * let it have half of its nominal slice.
1664 static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1666 struct cfq_queue *__cfqq, *next;
1668 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1669 cfq_resort_rr_list(__cfqq, 1);
1671 if (!cfqq->slice_left)
1672 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1674 cfqq->slice_end = cfqq->slice_left + jiffies;
1675 cfq_slice_expired(cfqd, 1);
1676 __cfq_set_active_queue(cfqd, cfqq);
1680 * should really be a ll_rw_blk.c helper
1682 static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1684 request_queue_t *q = cfqd->queue;
1686 if (!blk_queue_plugged(q))
1689 __generic_unplug_device(q);
1693 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1694 * something we should do about it
1697 cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1700 struct cfq_io_context *cic = crq->io_context;
1703 * we never wait for an async request and we don't allow preemption
1704 * of an async request. so just return early
1706 if (!cfq_crq_is_sync(crq)) {
1708 * sync process issued an async request, if it's waiting
1709 * then expire it and kick rq handling.
1711 if (cic == cfqd->active_cic &&
1712 del_timer(&cfqd->idle_slice_timer)) {
1713 cfq_slice_expired(cfqd, 0);
1714 cfq_start_queueing(cfqd, cfqq);
1719 cfq_update_io_thinktime(cfqd, cic);
1720 cfq_update_io_seektime(cfqd, cic, crq);
1721 cfq_update_idle_window(cfqd, cfqq, cic);
1723 cic->last_queue = jiffies;
1724 cic->last_request_pos = crq->request->sector + crq->request->nr_sectors;
1726 if (cfqq == cfqd->active_queue) {
1728 * if we are waiting for a request for this queue, let it rip
1729 * immediately and flag that we must not expire this queue
1732 if (cfq_cfqq_wait_request(cfqq)) {
1733 cfq_mark_cfqq_must_dispatch(cfqq);
1734 del_timer(&cfqd->idle_slice_timer);
1735 cfq_start_queueing(cfqd, cfqq);
1737 } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
1739 * not the active queue - expire current slice if it is
1740 * idle and has expired it's mean thinktime or this new queue
1741 * has some old slice time left and is of higher priority
1743 cfq_preempt_queue(cfqd, cfqq);
1744 cfq_mark_cfqq_must_dispatch(cfqq);
1745 cfq_start_queueing(cfqd, cfqq);
1749 static void cfq_insert_request(request_queue_t *q, struct request *rq)
1751 struct cfq_data *cfqd = q->elevator->elevator_data;
1752 struct cfq_rq *crq = RQ_DATA(rq);
1753 struct cfq_queue *cfqq = crq->cfq_queue;
1755 cfq_init_prio_data(cfqq);
1757 cfq_add_crq_rb(crq);
1759 list_add_tail(&rq->queuelist, &cfqq->fifo);
1761 cfq_crq_enqueued(cfqd, cfqq, crq);
1764 static void cfq_completed_request(request_queue_t *q, struct request *rq)
1766 struct cfq_rq *crq = RQ_DATA(rq);
1767 struct cfq_queue *cfqq = crq->cfq_queue;
1768 struct cfq_data *cfqd = cfqq->cfqd;
1769 const int sync = cfq_crq_is_sync(crq);
1774 WARN_ON(!cfqd->rq_in_driver);
1775 WARN_ON(!cfqq->on_dispatch[sync]);
1776 cfqd->rq_in_driver--;
1777 cfqq->on_dispatch[sync]--;
1779 if (!cfq_class_idle(cfqq))
1780 cfqd->last_end_request = now;
1782 if (!cfq_cfqq_dispatched(cfqq)) {
1783 if (cfq_cfqq_on_rr(cfqq)) {
1784 cfqq->service_last = now;
1785 cfq_resort_rr_list(cfqq, 0);
1790 crq->io_context->last_end_request = now;
1793 * If this is the active queue, check if it needs to be expired,
1794 * or if we want to idle in case it has no pending requests.
1796 if (cfqd->active_queue == cfqq) {
1797 if (time_after(now, cfqq->slice_end))
1798 cfq_slice_expired(cfqd, 0);
1799 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list)) {
1800 if (!cfq_arm_slice_timer(cfqd, cfqq))
1801 cfq_schedule_dispatch(cfqd);
1806 static struct request *
1807 cfq_former_request(request_queue_t *q, struct request *rq)
1809 struct cfq_rq *crq = RQ_DATA(rq);
1810 struct rb_node *rbprev = rb_prev(&crq->rb_node);
1813 return rb_entry_crq(rbprev)->request;
1818 static struct request *
1819 cfq_latter_request(request_queue_t *q, struct request *rq)
1821 struct cfq_rq *crq = RQ_DATA(rq);
1822 struct rb_node *rbnext = rb_next(&crq->rb_node);
1825 return rb_entry_crq(rbnext)->request;
1831 * we temporarily boost lower priority queues if they are holding fs exclusive
1832 * resources. they are boosted to normal prio (CLASS_BE/4)
1834 static void cfq_prio_boost(struct cfq_queue *cfqq)
1836 const int ioprio_class = cfqq->ioprio_class;
1837 const int ioprio = cfqq->ioprio;
1839 if (has_fs_excl()) {
1841 * boost idle prio on transactions that would lock out other
1842 * users of the filesystem
1844 if (cfq_class_idle(cfqq))
1845 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1846 if (cfqq->ioprio > IOPRIO_NORM)
1847 cfqq->ioprio = IOPRIO_NORM;
1850 * check if we need to unboost the queue
1852 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1853 cfqq->ioprio_class = cfqq->org_ioprio_class;
1854 if (cfqq->ioprio != cfqq->org_ioprio)
1855 cfqq->ioprio = cfqq->org_ioprio;
1859 * refile between round-robin lists if we moved the priority class
1861 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
1862 cfq_cfqq_on_rr(cfqq))
1863 cfq_resort_rr_list(cfqq, 0);
1867 __cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1868 struct task_struct *task, int rw)
1870 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
1871 !cfq_cfqq_must_alloc_slice(cfqq)) {
1872 cfq_mark_cfqq_must_alloc_slice(cfqq);
1873 return ELV_MQUEUE_MUST;
1876 return ELV_MQUEUE_MAY;
1879 static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1881 struct cfq_data *cfqd = q->elevator->elevator_data;
1882 struct task_struct *tsk = current;
1883 struct cfq_queue *cfqq;
1886 * don't force setup of a queue from here, as a call to may_queue
1887 * does not necessarily imply that a request actually will be queued.
1888 * so just lookup a possibly existing queue, or return 'may queue'
1891 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
1893 cfq_init_prio_data(cfqq);
1894 cfq_prio_boost(cfqq);
1896 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
1899 return ELV_MQUEUE_MAY;
1902 static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
1904 struct cfq_data *cfqd = q->elevator->elevator_data;
1906 if (unlikely(cfqd->rq_starved)) {
1907 struct request_list *rl = &q->rq;
1910 if (waitqueue_active(&rl->wait[READ]))
1911 wake_up(&rl->wait[READ]);
1912 if (waitqueue_active(&rl->wait[WRITE]))
1913 wake_up(&rl->wait[WRITE]);
1918 * queue lock held here
1920 static void cfq_put_request(request_queue_t *q, struct request *rq)
1922 struct cfq_data *cfqd = q->elevator->elevator_data;
1923 struct cfq_rq *crq = RQ_DATA(rq);
1926 struct cfq_queue *cfqq = crq->cfq_queue;
1927 const int rw = rq_data_dir(rq);
1929 BUG_ON(!cfqq->allocated[rw]);
1930 cfqq->allocated[rw]--;
1932 put_io_context(crq->io_context->ioc);
1934 mempool_free(crq, cfqd->crq_pool);
1935 rq->elevator_private = NULL;
1937 cfq_check_waiters(q, cfqq);
1938 cfq_put_queue(cfqq);
1943 * Allocate cfq data structures associated with this request.
1946 cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
1949 struct cfq_data *cfqd = q->elevator->elevator_data;
1950 struct task_struct *tsk = current;
1951 struct cfq_io_context *cic;
1952 const int rw = rq_data_dir(rq);
1953 pid_t key = cfq_queue_pid(tsk, rw);
1954 struct cfq_queue *cfqq;
1956 unsigned long flags;
1957 int is_sync = key != CFQ_KEY_ASYNC;
1959 might_sleep_if(gfp_mask & __GFP_WAIT);
1961 cic = cfq_get_io_context(cfqd, gfp_mask);
1963 spin_lock_irqsave(q->queue_lock, flags);
1968 if (!cic->cfqq[is_sync]) {
1969 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
1973 cic->cfqq[is_sync] = cfqq;
1975 cfqq = cic->cfqq[is_sync];
1977 cfqq->allocated[rw]++;
1978 cfq_clear_cfqq_must_alloc(cfqq);
1979 cfqd->rq_starved = 0;
1980 atomic_inc(&cfqq->ref);
1981 spin_unlock_irqrestore(q->queue_lock, flags);
1983 crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
1985 RB_CLEAR_NODE(&crq->rb_node);
1988 crq->cfq_queue = cfqq;
1989 crq->io_context = cic;
1992 cfq_mark_crq_is_sync(crq);
1994 cfq_clear_crq_is_sync(crq);
1996 rq->elevator_private = crq;
2000 spin_lock_irqsave(q->queue_lock, flags);
2001 cfqq->allocated[rw]--;
2002 if (!(cfqq->allocated[0] + cfqq->allocated[1]))
2003 cfq_mark_cfqq_must_alloc(cfqq);
2004 cfq_put_queue(cfqq);
2007 put_io_context(cic->ioc);
2009 * mark us rq allocation starved. we need to kickstart the process
2010 * ourselves if there are no pending requests that can do it for us.
2011 * that would be an extremely rare OOM situation
2013 cfqd->rq_starved = 1;
2014 cfq_schedule_dispatch(cfqd);
2015 spin_unlock_irqrestore(q->queue_lock, flags);
2019 static void cfq_kick_queue(void *data)
2021 request_queue_t *q = data;
2022 struct cfq_data *cfqd = q->elevator->elevator_data;
2023 unsigned long flags;
2025 spin_lock_irqsave(q->queue_lock, flags);
2027 if (cfqd->rq_starved) {
2028 struct request_list *rl = &q->rq;
2031 * we aren't guaranteed to get a request after this, but we
2032 * have to be opportunistic
2035 if (waitqueue_active(&rl->wait[READ]))
2036 wake_up(&rl->wait[READ]);
2037 if (waitqueue_active(&rl->wait[WRITE]))
2038 wake_up(&rl->wait[WRITE]);
2043 spin_unlock_irqrestore(q->queue_lock, flags);
2047 * Timer running if the active_queue is currently idling inside its time slice
2049 static void cfq_idle_slice_timer(unsigned long data)
2051 struct cfq_data *cfqd = (struct cfq_data *) data;
2052 struct cfq_queue *cfqq;
2053 unsigned long flags;
2055 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2057 if ((cfqq = cfqd->active_queue) != NULL) {
2058 unsigned long now = jiffies;
2063 if (time_after(now, cfqq->slice_end))
2067 * only expire and reinvoke request handler, if there are
2068 * other queues with pending requests
2070 if (!cfqd->busy_queues)
2074 * not expired and it has a request pending, let it dispatch
2076 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
2077 cfq_mark_cfqq_must_dispatch(cfqq);
2082 cfq_slice_expired(cfqd, 0);
2084 cfq_schedule_dispatch(cfqd);
2086 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2090 * Timer running if an idle class queue is waiting for service
2092 static void cfq_idle_class_timer(unsigned long data)
2094 struct cfq_data *cfqd = (struct cfq_data *) data;
2095 unsigned long flags, end;
2097 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2100 * race with a non-idle queue, reset timer
2102 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
2103 if (!time_after_eq(jiffies, end))
2104 mod_timer(&cfqd->idle_class_timer, end);
2106 cfq_schedule_dispatch(cfqd);
2108 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2111 static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2113 del_timer_sync(&cfqd->idle_slice_timer);
2114 del_timer_sync(&cfqd->idle_class_timer);
2115 blk_sync_queue(cfqd->queue);
2118 static void cfq_exit_queue(elevator_t *e)
2120 struct cfq_data *cfqd = e->elevator_data;
2121 request_queue_t *q = cfqd->queue;
2123 cfq_shutdown_timer_wq(cfqd);
2125 spin_lock(&cfq_exit_lock);
2126 spin_lock_irq(q->queue_lock);
2128 if (cfqd->active_queue)
2129 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
2131 while (!list_empty(&cfqd->cic_list)) {
2132 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2133 struct cfq_io_context,
2135 if (cic->cfqq[ASYNC]) {
2136 cfq_put_queue(cic->cfqq[ASYNC]);
2137 cic->cfqq[ASYNC] = NULL;
2139 if (cic->cfqq[SYNC]) {
2140 cfq_put_queue(cic->cfqq[SYNC]);
2141 cic->cfqq[SYNC] = NULL;
2144 list_del_init(&cic->queue_list);
2147 spin_unlock_irq(q->queue_lock);
2148 spin_unlock(&cfq_exit_lock);
2150 cfq_shutdown_timer_wq(cfqd);
2152 mempool_destroy(cfqd->crq_pool);
2153 kfree(cfqd->cfq_hash);
2157 static void *cfq_init_queue(request_queue_t *q, elevator_t *e)
2159 struct cfq_data *cfqd;
2162 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2166 memset(cfqd, 0, sizeof(*cfqd));
2168 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2169 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2171 INIT_LIST_HEAD(&cfqd->busy_rr);
2172 INIT_LIST_HEAD(&cfqd->cur_rr);
2173 INIT_LIST_HEAD(&cfqd->idle_rr);
2174 INIT_LIST_HEAD(&cfqd->empty_list);
2175 INIT_LIST_HEAD(&cfqd->cic_list);
2177 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2178 if (!cfqd->cfq_hash)
2181 cfqd->crq_pool = mempool_create_slab_pool(BLKDEV_MIN_RQ, crq_pool);
2182 if (!cfqd->crq_pool)
2185 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2186 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2190 init_timer(&cfqd->idle_slice_timer);
2191 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2192 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2194 init_timer(&cfqd->idle_class_timer);
2195 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2196 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2198 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2200 cfqd->cfq_queued = cfq_queued;
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;
2213 kfree(cfqd->cfq_hash);
2219 static void cfq_slab_kill(void)
2222 kmem_cache_destroy(crq_pool);
2224 kmem_cache_destroy(cfq_pool);
2226 kmem_cache_destroy(cfq_ioc_pool);
2229 static int __init cfq_slab_setup(void)
2231 crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
2236 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2241 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2242 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2253 * sysfs parts below -->
2257 cfq_var_show(unsigned int var, char *page)
2259 return sprintf(page, "%d\n", var);
2263 cfq_var_store(unsigned int *var, const char *page, size_t count)
2265 char *p = (char *) page;
2267 *var = simple_strtoul(p, &p, 10);
2271 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2272 static ssize_t __FUNC(elevator_t *e, char *page) \
2274 struct cfq_data *cfqd = e->elevator_data; \
2275 unsigned int __data = __VAR; \
2277 __data = jiffies_to_msecs(__data); \
2278 return cfq_var_show(__data, (page)); \
2280 SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2281 SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
2282 SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2283 SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
2284 SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2285 SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
2286 SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2287 SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2288 SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2289 SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2290 #undef SHOW_FUNCTION
2292 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2293 static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
2295 struct cfq_data *cfqd = e->elevator_data; \
2296 unsigned int __data; \
2297 int ret = cfq_var_store(&__data, (page), count); \
2298 if (__data < (MIN)) \
2300 else if (__data > (MAX)) \
2303 *(__PTR) = msecs_to_jiffies(__data); \
2305 *(__PTR) = __data; \
2308 STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2309 STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
2310 STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2311 STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
2312 STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2313 STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
2314 STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2315 STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2316 STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2317 STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
2318 #undef STORE_FUNCTION
2320 #define CFQ_ATTR(name) \
2321 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
2323 static struct elv_fs_entry cfq_attrs[] = {
2326 CFQ_ATTR(fifo_expire_sync),
2327 CFQ_ATTR(fifo_expire_async),
2328 CFQ_ATTR(back_seek_max),
2329 CFQ_ATTR(back_seek_penalty),
2330 CFQ_ATTR(slice_sync),
2331 CFQ_ATTR(slice_async),
2332 CFQ_ATTR(slice_async_rq),
2333 CFQ_ATTR(slice_idle),
2337 static struct elevator_type iosched_cfq = {
2339 .elevator_merge_fn = cfq_merge,
2340 .elevator_merged_fn = cfq_merged_request,
2341 .elevator_merge_req_fn = cfq_merged_requests,
2342 .elevator_dispatch_fn = cfq_dispatch_requests,
2343 .elevator_add_req_fn = cfq_insert_request,
2344 .elevator_activate_req_fn = cfq_activate_request,
2345 .elevator_deactivate_req_fn = cfq_deactivate_request,
2346 .elevator_queue_empty_fn = cfq_queue_empty,
2347 .elevator_completed_req_fn = cfq_completed_request,
2348 .elevator_former_req_fn = cfq_former_request,
2349 .elevator_latter_req_fn = cfq_latter_request,
2350 .elevator_set_req_fn = cfq_set_request,
2351 .elevator_put_req_fn = cfq_put_request,
2352 .elevator_may_queue_fn = cfq_may_queue,
2353 .elevator_init_fn = cfq_init_queue,
2354 .elevator_exit_fn = cfq_exit_queue,
2357 .elevator_attrs = cfq_attrs,
2358 .elevator_name = "cfq",
2359 .elevator_owner = THIS_MODULE,
2362 static int __init cfq_init(void)
2367 * could be 0 on HZ < 1000 setups
2369 if (!cfq_slice_async)
2370 cfq_slice_async = 1;
2371 if (!cfq_slice_idle)
2374 if (cfq_slab_setup())
2377 ret = elv_register(&iosched_cfq);
2384 static void __exit cfq_exit(void)
2386 DECLARE_COMPLETION(all_gone);
2387 elv_unregister(&iosched_cfq);
2388 ioc_gone = &all_gone;
2389 /* ioc_gone's update must be visible before reading ioc_count */
2391 if (atomic_read(&ioc_count))
2392 wait_for_completion(ioc_gone);
2397 module_init(cfq_init);
2398 module_exit(cfq_exit);
2400 MODULE_AUTHOR("Jens Axboe");
2401 MODULE_LICENSE("GPL");
2402 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");