2 * linux/drivers/block/cfq-iosched.c
4 * CFQ, or complete fairness queueing, disk scheduler.
6 * Based on ideas from a previously unfinished io
7 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
9 * Copyright (C) 2003 Jens Axboe <axboe@suse.de>
11 #include <linux/kernel.h>
13 #include <linux/blkdev.h>
14 #include <linux/elevator.h>
15 #include <linux/bio.h>
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/compiler.h>
21 #include <linux/hash.h>
22 #include <linux/rbtree.h>
23 #include <linux/mempool.h>
24 #include <linux/ioprio.h>
25 #include <linux/writeback.h>
30 static int cfq_quantum = 4; /* max queue in one round of service */
31 static int cfq_queued = 8; /* minimum rq allocate limit per-queue*/
32 static int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
33 static int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
34 static int cfq_back_penalty = 2; /* penalty of a backwards seek */
36 static int cfq_slice_sync = HZ / 10;
37 static int cfq_slice_async = HZ / 50;
38 static int cfq_slice_async_rq = 2;
39 static int cfq_slice_idle = HZ / 50;
41 #define CFQ_IDLE_GRACE (HZ / 10)
42 #define CFQ_SLICE_SCALE (5)
44 #define CFQ_KEY_ASYNC (0)
47 * disable queueing at the driver/hardware level
49 static int cfq_max_depth = 1;
52 * for the hash of cfqq inside the cfqd
54 #define CFQ_QHASH_SHIFT 6
55 #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
56 #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
59 * for the hash of crq inside the cfqq
61 #define CFQ_MHASH_SHIFT 6
62 #define CFQ_MHASH_BLOCK(sec) ((sec) >> 3)
63 #define CFQ_MHASH_ENTRIES (1 << CFQ_MHASH_SHIFT)
64 #define CFQ_MHASH_FN(sec) hash_long(CFQ_MHASH_BLOCK(sec), CFQ_MHASH_SHIFT)
65 #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
66 #define list_entry_hash(ptr) hlist_entry((ptr), struct cfq_rq, hash)
68 #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
69 #define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
71 #define RQ_DATA(rq) (rq)->elevator_private
77 #define RB_EMPTY(node) ((node)->rb_node == NULL)
78 #define RB_CLEAR_COLOR(node) (node)->rb_color = RB_NONE
79 #define RB_CLEAR(node) do { \
80 (node)->rb_parent = NULL; \
81 RB_CLEAR_COLOR((node)); \
82 (node)->rb_right = NULL; \
83 (node)->rb_left = NULL; \
85 #define RB_CLEAR_ROOT(root) ((root)->rb_node = NULL)
86 #define ON_RB(node) ((node)->rb_color != RB_NONE)
87 #define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
88 #define rq_rb_key(rq) (rq)->sector
90 static kmem_cache_t *crq_pool;
91 static kmem_cache_t *cfq_pool;
92 static kmem_cache_t *cfq_ioc_pool;
94 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
95 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
96 #define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
97 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
99 #define cfq_cfqq_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
102 * Per block device queue structure
106 request_queue_t *queue;
109 * rr list of queues with requests and the count of them
111 struct list_head rr_list[CFQ_PRIO_LISTS];
112 struct list_head busy_rr;
113 struct list_head cur_rr;
114 struct list_head idle_rr;
115 unsigned int busy_queues;
118 * non-ordered list of empty cfqq's
120 struct list_head empty_list;
125 struct hlist_head *cfq_hash;
128 * global crq hash for all queues
130 struct hlist_head *crq_hash;
132 unsigned int max_queued;
139 * schedule slice state info
142 * idle window management
144 struct timer_list idle_slice_timer;
145 struct work_struct unplug_work;
147 struct cfq_queue *active_queue;
148 struct cfq_io_context *active_cic;
149 int cur_prio, cur_end_prio;
150 unsigned int dispatch_slice;
152 struct timer_list idle_class_timer;
154 sector_t last_sector;
155 unsigned long last_end_request;
157 unsigned int rq_starved;
160 * tunables, see top of file
162 unsigned int cfq_quantum;
163 unsigned int cfq_queued;
164 unsigned int cfq_fifo_expire[2];
165 unsigned int cfq_back_penalty;
166 unsigned int cfq_back_max;
167 unsigned int cfq_slice[2];
168 unsigned int cfq_slice_async_rq;
169 unsigned int cfq_slice_idle;
170 unsigned int cfq_max_depth;
174 * Per process-grouping structure
177 /* reference count */
179 /* parent cfq_data */
180 struct cfq_data *cfqd;
181 /* cfqq lookup hash */
182 struct hlist_node cfq_hash;
185 /* on either rr or empty list of cfqd */
186 struct list_head cfq_list;
187 /* sorted list of pending requests */
188 struct rb_root sort_list;
189 /* if fifo isn't expired, next request to serve */
190 struct cfq_rq *next_crq;
191 /* requests queued in sort_list */
193 /* currently allocated requests */
195 /* fifo list of requests in sort_list */
196 struct list_head fifo;
198 unsigned long slice_start;
199 unsigned long slice_end;
200 unsigned long slice_left;
201 unsigned long service_last;
203 /* number of requests that have been handed to the driver */
206 /* io prio of this group */
207 unsigned short ioprio, org_ioprio;
208 unsigned short ioprio_class, org_ioprio_class;
210 /* whether queue is on rr (or empty) list */
212 /* idle slice, waiting for new request submission */
213 unsigned wait_request : 1;
214 /* set when wait_request gets set, reset on first rq alloc */
215 unsigned must_alloc : 1;
216 /* only gets one must_alloc per slice */
217 unsigned must_alloc_slice : 1;
218 /* idle slice, request added, now waiting to dispatch it */
219 unsigned must_dispatch : 1;
220 /* fifo expire per-slice */
221 unsigned fifo_expire : 1;
223 unsigned idle_window : 1;
224 unsigned prio_changed : 1;
228 struct rb_node rb_node;
230 struct request *request;
231 struct hlist_node hash;
233 struct cfq_queue *cfq_queue;
234 struct cfq_io_context *io_context;
236 unsigned in_flight : 1;
237 unsigned accounted : 1;
238 unsigned is_sync : 1;
239 unsigned requeued : 1;
242 static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int);
243 static void cfq_dispatch_sort(request_queue_t *, struct cfq_rq *);
244 static void cfq_put_cfqd(struct cfq_data *cfqd);
246 #define process_sync(tsk) ((tsk)->flags & PF_SYNCWRITE)
249 * lots of deadline iosched dupes, can be abstracted later...
251 static inline void cfq_del_crq_hash(struct cfq_rq *crq)
253 hlist_del_init(&crq->hash);
256 static void cfq_remove_merge_hints(request_queue_t *q, struct cfq_rq *crq)
258 cfq_del_crq_hash(crq);
260 if (q->last_merge == crq->request)
261 q->last_merge = NULL;
264 static inline void cfq_add_crq_hash(struct cfq_data *cfqd, struct cfq_rq *crq)
266 const int hash_idx = CFQ_MHASH_FN(rq_hash_key(crq->request));
268 hlist_add_head(&crq->hash, &cfqd->crq_hash[hash_idx]);
271 static struct request *cfq_find_rq_hash(struct cfq_data *cfqd, sector_t offset)
273 struct hlist_head *hash_list = &cfqd->crq_hash[CFQ_MHASH_FN(offset)];
274 struct hlist_node *entry, *next;
276 hlist_for_each_safe(entry, next, hash_list) {
277 struct cfq_rq *crq = list_entry_hash(entry);
278 struct request *__rq = crq->request;
280 if (!rq_mergeable(__rq)) {
281 cfq_del_crq_hash(crq);
285 if (rq_hash_key(__rq) == offset)
293 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
294 * We choose the request that is closest to the head right now. Distance
295 * behind the head are penalized and only allowed to a certain extent.
297 static struct cfq_rq *
298 cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
300 sector_t last, s1, s2, d1 = 0, d2 = 0;
301 int r1_wrap = 0, r2_wrap = 0; /* requests are behind the disk head */
302 unsigned long back_max;
304 if (crq1 == NULL || crq1 == crq2)
313 s1 = crq1->request->sector;
314 s2 = crq2->request->sector;
316 last = cfqd->last_sector;
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;
337 else if (s2 + back_max >= last)
338 d2 = (last - s2) * cfqd->cfq_back_penalty;
342 /* Found required data */
343 if (!r1_wrap && r2_wrap)
345 else if (!r2_wrap && r1_wrap)
347 else if (r1_wrap && r2_wrap) {
348 /* both behind the head */
355 /* Both requests in front of the head */
369 * would be nice to take fifo expire time into account as well
371 static struct cfq_rq *
372 cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
375 struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
376 struct rb_node *rbnext, *rbprev;
379 if (ON_RB(&last->rb_node))
380 rbnext = rb_next(&last->rb_node);
382 rbnext = rb_first(&cfqq->sort_list);
383 if (rbnext == &last->rb_node)
387 rbprev = rb_prev(&last->rb_node);
390 crq_prev = rb_entry_crq(rbprev);
392 crq_next = rb_entry_crq(rbnext);
394 return cfq_choose_req(cfqd, crq_next, crq_prev);
397 static void cfq_update_next_crq(struct cfq_rq *crq)
399 struct cfq_queue *cfqq = crq->cfq_queue;
401 if (cfqq->next_crq == crq)
402 cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
405 static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
407 struct cfq_data *cfqd = cfqq->cfqd;
408 struct list_head *list, *entry;
410 BUG_ON(!cfqq->on_rr);
412 list_del(&cfqq->cfq_list);
414 if (cfq_class_rt(cfqq))
415 list = &cfqd->cur_rr;
416 else if (cfq_class_idle(cfqq))
417 list = &cfqd->idle_rr;
420 * if cfqq has requests in flight, don't allow it to be
421 * found in cfq_set_active_queue before it has finished them.
422 * this is done to increase fairness between a process that
423 * has lots of io pending vs one that only generates one
424 * sporadically or synchronously
427 list = &cfqd->busy_rr;
429 list = &cfqd->rr_list[cfqq->ioprio];
433 * if queue was preempted, just add to front to be fair. busy_rr
436 if (preempted || list == &cfqd->busy_rr) {
437 list_add(&cfqq->cfq_list, list);
442 * sort by when queue was last serviced
445 while ((entry = entry->prev) != list) {
446 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
448 if (!__cfqq->service_last)
450 if (time_before(__cfqq->service_last, cfqq->service_last))
454 list_add(&cfqq->cfq_list, entry);
458 * add to busy list of queues for service, trying to be fair in ordering
459 * the pending list according to last request service
462 cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq, int requeue)
468 cfq_resort_rr_list(cfqq, requeue);
472 cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
474 BUG_ON(!cfqq->on_rr);
476 list_move(&cfqq->cfq_list, &cfqd->empty_list);
478 BUG_ON(!cfqd->busy_queues);
483 * rb tree support functions
485 static inline void cfq_del_crq_rb(struct cfq_rq *crq)
487 struct cfq_queue *cfqq = crq->cfq_queue;
489 if (ON_RB(&crq->rb_node)) {
490 struct cfq_data *cfqd = cfqq->cfqd;
491 const int sync = crq->is_sync;
493 BUG_ON(!cfqq->queued[sync]);
494 cfqq->queued[sync]--;
496 cfq_update_next_crq(crq);
498 rb_erase(&crq->rb_node, &cfqq->sort_list);
499 RB_CLEAR_COLOR(&crq->rb_node);
501 if (cfqq->on_rr && RB_EMPTY(&cfqq->sort_list))
502 cfq_del_cfqq_rr(cfqd, cfqq);
506 static struct cfq_rq *
507 __cfq_add_crq_rb(struct cfq_rq *crq)
509 struct rb_node **p = &crq->cfq_queue->sort_list.rb_node;
510 struct rb_node *parent = NULL;
511 struct cfq_rq *__crq;
515 __crq = rb_entry_crq(parent);
517 if (crq->rb_key < __crq->rb_key)
519 else if (crq->rb_key > __crq->rb_key)
525 rb_link_node(&crq->rb_node, parent, p);
529 static void cfq_add_crq_rb(struct cfq_rq *crq)
531 struct cfq_queue *cfqq = crq->cfq_queue;
532 struct cfq_data *cfqd = cfqq->cfqd;
533 struct request *rq = crq->request;
534 struct cfq_rq *__alias;
536 crq->rb_key = rq_rb_key(rq);
537 cfqq->queued[crq->is_sync]++;
540 * looks a little odd, but the first insert might return an alias.
541 * if that happens, put the alias on the dispatch list
543 while ((__alias = __cfq_add_crq_rb(crq)) != NULL)
544 cfq_dispatch_sort(cfqd->queue, __alias);
546 rb_insert_color(&crq->rb_node, &cfqq->sort_list);
549 cfq_add_cfqq_rr(cfqd, cfqq, crq->requeued);
552 * check if this request is a better next-serve candidate
554 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
558 cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
560 if (ON_RB(&crq->rb_node)) {
561 rb_erase(&crq->rb_node, &cfqq->sort_list);
562 cfqq->queued[crq->is_sync]--;
568 static struct request *cfq_find_rq_rb(struct cfq_data *cfqd, sector_t sector)
571 struct cfq_queue *cfqq = cfq_find_cfq_hash(cfqd, current->pid);
577 n = cfqq->sort_list.rb_node;
579 struct cfq_rq *crq = rb_entry_crq(n);
581 if (sector < crq->rb_key)
583 else if (sector > crq->rb_key)
593 static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
595 struct cfq_data *cfqd = q->elevator->elevator_data;
596 struct cfq_rq *crq = RQ_DATA(rq);
599 struct cfq_queue *cfqq = crq->cfq_queue;
601 if (crq->accounted) {
603 WARN_ON(!cfqd->rq_in_driver);
604 cfqd->rq_in_driver--;
606 if (crq->in_flight) {
608 WARN_ON(!cfqq->in_flight);
616 * make sure the service time gets corrected on reissue of this request
618 static void cfq_requeue_request(request_queue_t *q, struct request *rq)
620 cfq_deactivate_request(q, rq);
621 list_add(&rq->queuelist, &q->queue_head);
624 static void cfq_remove_request(request_queue_t *q, struct request *rq)
626 struct cfq_rq *crq = RQ_DATA(rq);
629 list_del_init(&rq->queuelist);
631 cfq_remove_merge_hints(q, crq);
637 cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
639 struct cfq_data *cfqd = q->elevator->elevator_data;
640 struct request *__rq;
643 ret = elv_try_last_merge(q, bio);
644 if (ret != ELEVATOR_NO_MERGE) {
645 __rq = q->last_merge;
649 __rq = cfq_find_rq_hash(cfqd, bio->bi_sector);
650 if (__rq && elv_rq_merge_ok(__rq, bio)) {
651 ret = ELEVATOR_BACK_MERGE;
655 __rq = cfq_find_rq_rb(cfqd, bio->bi_sector + bio_sectors(bio));
656 if (__rq && elv_rq_merge_ok(__rq, bio)) {
657 ret = ELEVATOR_FRONT_MERGE;
661 return ELEVATOR_NO_MERGE;
663 q->last_merge = __rq;
669 static void cfq_merged_request(request_queue_t *q, struct request *req)
671 struct cfq_data *cfqd = q->elevator->elevator_data;
672 struct cfq_rq *crq = RQ_DATA(req);
674 cfq_del_crq_hash(crq);
675 cfq_add_crq_hash(cfqd, crq);
677 if (ON_RB(&crq->rb_node) && (rq_rb_key(req) != crq->rb_key)) {
678 struct cfq_queue *cfqq = crq->cfq_queue;
680 cfq_update_next_crq(crq);
681 cfq_reposition_crq_rb(cfqq, crq);
688 cfq_merged_requests(request_queue_t *q, struct request *rq,
689 struct request *next)
691 cfq_merged_request(q, rq);
694 * reposition in fifo if next is older than rq
696 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
697 time_before(next->start_time, rq->start_time))
698 list_move(&rq->queuelist, &next->queuelist);
700 cfq_remove_request(q, next);
704 __cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
708 * stop potential idle class queues waiting service
710 del_timer(&cfqd->idle_class_timer);
712 cfqq->slice_start = jiffies;
714 cfqq->slice_left = 0;
715 cfqq->must_alloc_slice = 0;
716 cfqq->fifo_expire = 0;
719 cfqd->active_queue = cfqq;
732 static int cfq_get_next_prio_level(struct cfq_data *cfqd)
741 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
742 if (!list_empty(&cfqd->rr_list[p])) {
751 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
752 cfqd->cur_end_prio = 0;
759 if (unlikely(prio == -1))
762 BUG_ON(prio >= CFQ_PRIO_LISTS);
764 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
766 cfqd->cur_prio = prio + 1;
767 if (cfqd->cur_prio > cfqd->cur_end_prio) {
768 cfqd->cur_end_prio = cfqd->cur_prio;
771 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
773 cfqd->cur_end_prio = 0;
779 static void cfq_set_active_queue(struct cfq_data *cfqd)
781 struct cfq_queue *cfqq = NULL;
784 * if current list is non-empty, grab first entry. if it is empty,
785 * get next prio level and grab first entry then if any are spliced
787 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
788 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
791 * if we have idle queues and no rt or be queues had pending
792 * requests, either allow immediate service if the grace period
793 * has passed or arm the idle grace timer
795 if (!cfqq && !list_empty(&cfqd->idle_rr)) {
796 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
798 if (time_after_eq(jiffies, end))
799 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
801 mod_timer(&cfqd->idle_class_timer, end);
804 __cfq_set_active_queue(cfqd, cfqq);
808 * current cfqq expired its slice (or was too idle), select new one
810 static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
812 struct cfq_queue *cfqq = cfqd->active_queue;
815 unsigned long now = jiffies;
817 if (cfqq->wait_request)
818 del_timer(&cfqd->idle_slice_timer);
820 if (!preempted && !cfqq->in_flight)
821 cfqq->service_last = now;
823 cfqq->must_dispatch = 0;
824 cfqq->wait_request = 0;
827 * store what was left of this slice, if the queue idled out
830 if (time_after(now, cfqq->slice_end))
831 cfqq->slice_left = now - cfqq->slice_end;
833 cfqq->slice_left = 0;
836 cfq_resort_rr_list(cfqq, preempted);
838 cfqd->active_queue = NULL;
840 if (cfqd->active_cic) {
841 put_io_context(cfqd->active_cic->ioc);
842 cfqd->active_cic = NULL;
846 cfqd->dispatch_slice = 0;
849 static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
852 WARN_ON(!RB_EMPTY(&cfqq->sort_list));
853 WARN_ON(cfqq != cfqd->active_queue);
856 * idle is disabled, either manually or by past process history
858 if (!cfqd->cfq_slice_idle)
860 if (!cfqq->idle_window)
863 * task has exited, don't wait
865 if (cfqd->active_cic && !cfqd->active_cic->ioc->task)
868 cfqq->wait_request = 1;
869 cfqq->must_alloc = 1;
871 if (!timer_pending(&cfqd->idle_slice_timer)) {
872 unsigned long slice_left = cfqq->slice_end - 1;
874 cfqd->idle_slice_timer.expires = min(jiffies + cfqd->cfq_slice_idle, slice_left);
875 add_timer(&cfqd->idle_slice_timer);
882 * we dispatch cfqd->cfq_quantum requests in total from the rr_list queues,
883 * this function sector sorts the selected request to minimize seeks. we start
884 * at cfqd->last_sector, not 0.
886 static void cfq_dispatch_sort(request_queue_t *q, struct cfq_rq *crq)
888 struct cfq_data *cfqd = q->elevator->elevator_data;
889 struct cfq_queue *cfqq = crq->cfq_queue;
890 struct list_head *head = &q->queue_head, *entry = head;
891 struct request *__rq;
894 list_del(&crq->request->queuelist);
896 last = cfqd->last_sector;
897 list_for_each_entry_reverse(__rq, head, queuelist) {
898 struct cfq_rq *__crq = RQ_DATA(__rq);
900 if (blk_barrier_rq(__rq))
902 if (!blk_fs_request(__rq))
907 if (__rq->sector <= crq->request->sector)
909 if (__rq->sector > last && crq->request->sector < last) {
910 last = crq->request->sector + crq->request->nr_sectors;
913 entry = &__rq->queuelist;
916 cfqd->last_sector = last;
918 cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
921 cfq_remove_merge_hints(q, crq);
926 list_add_tail(&crq->request->queuelist, entry);
930 * return expired entry, or NULL to just start from scratch in rbtree
932 static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
934 struct cfq_data *cfqd = cfqq->cfqd;
938 if (cfqq->fifo_expire)
941 if (!list_empty(&cfqq->fifo)) {
942 int fifo = cfq_cfqq_sync(cfqq);
944 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
946 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
947 cfqq->fifo_expire = 1;
956 * Scale schedule slice based on io priority
959 cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
961 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
963 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
965 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
969 cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
971 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
975 cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
977 const int base_rq = cfqd->cfq_slice_async_rq;
979 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
981 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
985 * get next queue for service
987 static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd, int force)
989 unsigned long now = jiffies;
990 struct cfq_queue *cfqq;
992 cfqq = cfqd->active_queue;
999 if (!cfqq->must_dispatch && time_after(jiffies, cfqq->slice_end))
1003 * if queue has requests, dispatch one. if not, check if
1004 * enough slice is left to wait for one
1006 if (!RB_EMPTY(&cfqq->sort_list))
1008 else if (!force && cfq_cfqq_sync(cfqq) &&
1009 time_before(now, cfqq->slice_end)) {
1010 if (cfq_arm_slice_timer(cfqd, cfqq))
1015 cfq_slice_expired(cfqd, 0);
1016 cfq_set_active_queue(cfqd);
1018 return cfqd->active_queue;
1022 __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1027 BUG_ON(RB_EMPTY(&cfqq->sort_list));
1033 * follow expired path, else get first next available
1035 if ((crq = cfq_check_fifo(cfqq)) == NULL)
1036 crq = cfqq->next_crq;
1039 * finally, insert request into driver dispatch list
1041 cfq_dispatch_sort(cfqd->queue, crq);
1043 cfqd->dispatch_slice++;
1046 if (!cfqd->active_cic) {
1047 atomic_inc(&crq->io_context->ioc->refcount);
1048 cfqd->active_cic = crq->io_context;
1051 if (RB_EMPTY(&cfqq->sort_list))
1054 } while (dispatched < max_dispatch);
1057 * if slice end isn't set yet, set it. if at least one request was
1058 * sync, use the sync time slice value
1060 if (!cfqq->slice_end)
1061 cfq_set_prio_slice(cfqd, cfqq);
1064 * expire an async queue immediately if it has used up its slice. idle
1065 * queue always expire after 1 dispatch round.
1067 if ((!cfq_cfqq_sync(cfqq) &&
1068 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1069 cfq_class_idle(cfqq))
1070 cfq_slice_expired(cfqd, 0);
1076 cfq_dispatch_requests(request_queue_t *q, int max_dispatch, int force)
1078 struct cfq_data *cfqd = q->elevator->elevator_data;
1079 struct cfq_queue *cfqq;
1081 if (!cfqd->busy_queues)
1084 cfqq = cfq_select_queue(cfqd, force);
1086 cfqq->wait_request = 0;
1087 cfqq->must_dispatch = 0;
1088 del_timer(&cfqd->idle_slice_timer);
1090 if (cfq_class_idle(cfqq))
1093 return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1099 static inline void cfq_account_dispatch(struct cfq_rq *crq)
1101 struct cfq_queue *cfqq = crq->cfq_queue;
1102 struct cfq_data *cfqd = cfqq->cfqd;
1104 if (unlikely(!blk_fs_request(crq->request)))
1108 * accounted bit is necessary since some drivers will call
1109 * elv_next_request() many times for the same request (eg ide)
1115 cfqd->rq_in_driver++;
1119 cfq_account_completion(struct cfq_queue *cfqq, struct cfq_rq *crq)
1121 struct cfq_data *cfqd = cfqq->cfqd;
1124 if (!crq->accounted)
1129 WARN_ON(!cfqd->rq_in_driver);
1130 cfqd->rq_in_driver--;
1132 if (!cfq_class_idle(cfqq))
1133 cfqd->last_end_request = now;
1135 if (!cfqq->in_flight && cfqq->on_rr) {
1136 cfqq->service_last = now;
1137 cfq_resort_rr_list(cfqq, 0);
1141 crq->io_context->last_end_request = now;
1144 static struct request *cfq_next_request(request_queue_t *q)
1146 struct cfq_data *cfqd = q->elevator->elevator_data;
1149 if (!list_empty(&q->queue_head)) {
1152 rq = list_entry_rq(q->queue_head.next);
1157 * if idle window is disabled, allow queue buildup
1159 if (!crq->in_flight && !crq->cfq_queue->idle_window &&
1160 cfqd->rq_in_driver >= cfqd->cfq_max_depth)
1163 cfq_remove_merge_hints(q, crq);
1164 cfq_account_dispatch(crq);
1170 if (cfq_dispatch_requests(q, cfqd->cfq_quantum, 0))
1177 * task holds one reference to the queue, dropped when task exits. each crq
1178 * in-flight on this queue also holds a reference, dropped when crq is freed.
1180 * queue lock must be held here.
1182 static void cfq_put_queue(struct cfq_queue *cfqq)
1184 struct cfq_data *cfqd = cfqq->cfqd;
1186 BUG_ON(atomic_read(&cfqq->ref) <= 0);
1188 if (!atomic_dec_and_test(&cfqq->ref))
1191 BUG_ON(rb_first(&cfqq->sort_list));
1192 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
1193 BUG_ON(cfqq->on_rr);
1195 if (unlikely(cfqd->active_queue == cfqq)) {
1196 cfq_slice_expired(cfqd, 0);
1197 kblockd_schedule_work(&cfqd->unplug_work);
1200 cfq_put_cfqd(cfqq->cfqd);
1203 * it's on the empty list and still hashed
1205 list_del(&cfqq->cfq_list);
1206 hlist_del(&cfqq->cfq_hash);
1207 kmem_cache_free(cfq_pool, cfqq);
1210 static inline struct cfq_queue *
1211 __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, const int hashval)
1213 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1214 struct hlist_node *entry, *next;
1216 hlist_for_each_safe(entry, next, hash_list) {
1217 struct cfq_queue *__cfqq = list_entry_qhash(entry);
1219 if (__cfqq->key == key)
1226 static struct cfq_queue *
1227 cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key)
1229 return __cfq_find_cfq_hash(cfqd, key, hash_long(key, CFQ_QHASH_SHIFT));
1232 static void cfq_free_io_context(struct cfq_io_context *cic)
1234 struct cfq_io_context *__cic;
1235 struct list_head *entry, *next;
1237 list_for_each_safe(entry, next, &cic->list) {
1238 __cic = list_entry(entry, struct cfq_io_context, list);
1239 kmem_cache_free(cfq_ioc_pool, __cic);
1242 kmem_cache_free(cfq_ioc_pool, cic);
1246 * Called with interrupts disabled
1248 static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1250 struct cfq_data *cfqd = cic->cfqq->cfqd;
1251 request_queue_t *q = cfqd->queue;
1253 WARN_ON(!irqs_disabled());
1255 spin_lock(q->queue_lock);
1257 if (unlikely(cic->cfqq == cfqd->active_queue)) {
1258 cfq_slice_expired(cfqd, 0);
1259 kblockd_schedule_work(&cfqd->unplug_work);
1262 cfq_put_queue(cic->cfqq);
1264 spin_unlock(q->queue_lock);
1268 * Another task may update the task cic list, if it is doing a queue lookup
1269 * on its behalf. cfq_cic_lock excludes such concurrent updates
1271 static void cfq_exit_io_context(struct cfq_io_context *cic)
1273 struct cfq_io_context *__cic;
1274 struct list_head *entry;
1275 unsigned long flags;
1277 local_irq_save(flags);
1280 * put the reference this task is holding to the various queues
1282 list_for_each(entry, &cic->list) {
1283 __cic = list_entry(entry, struct cfq_io_context, list);
1284 cfq_exit_single_io_context(__cic);
1287 cfq_exit_single_io_context(cic);
1288 local_irq_restore(flags);
1291 static struct cfq_io_context *
1292 cfq_alloc_io_context(struct cfq_data *cfqd, int gfp_mask)
1294 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
1297 INIT_LIST_HEAD(&cic->list);
1300 cic->last_end_request = jiffies;
1301 cic->ttime_total = 0;
1302 cic->ttime_samples = 0;
1303 cic->ttime_mean = 0;
1304 cic->dtor = cfq_free_io_context;
1305 cic->exit = cfq_exit_io_context;
1311 static void cfq_init_prio_data(struct cfq_queue *cfqq)
1313 struct task_struct *tsk = current;
1316 if (!cfqq->prio_changed)
1319 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1320 switch (ioprio_class) {
1322 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1323 case IOPRIO_CLASS_NONE:
1325 * no prio set, place us in the middle of the BE classes
1327 cfqq->ioprio = task_nice_ioprio(tsk);
1328 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1330 case IOPRIO_CLASS_RT:
1331 cfqq->ioprio = task_ioprio(tsk);
1332 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1334 case IOPRIO_CLASS_BE:
1335 cfqq->ioprio = task_ioprio(tsk);
1336 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1338 case IOPRIO_CLASS_IDLE:
1339 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1341 cfqq->idle_window = 0;
1346 * keep track of original prio settings in case we have to temporarily
1347 * elevate the priority of this queue
1349 cfqq->org_ioprio = cfqq->ioprio;
1350 cfqq->org_ioprio_class = cfqq->ioprio_class;
1353 cfq_resort_rr_list(cfqq, 0);
1355 cfqq->prio_changed = 0;
1358 static inline void changed_ioprio(struct cfq_queue *cfqq)
1361 struct cfq_data *cfqd = cfqq->cfqd;
1363 spin_lock(cfqd->queue->queue_lock);
1364 cfqq->prio_changed = 1;
1365 cfq_init_prio_data(cfqq);
1366 spin_unlock(cfqd->queue->queue_lock);
1371 * callback from sys_ioprio_set, irqs are disabled
1373 static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
1375 struct cfq_io_context *cic = ioc->cic;
1377 changed_ioprio(cic->cfqq);
1379 list_for_each_entry(cic, &cic->list, list)
1380 changed_ioprio(cic->cfqq);
1385 static struct cfq_queue *
1386 cfq_get_queue(struct cfq_data *cfqd, unsigned int key, int gfp_mask)
1388 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1389 struct cfq_queue *cfqq, *new_cfqq = NULL;
1392 cfqq = __cfq_find_cfq_hash(cfqd, key, hashval);
1398 } else if (gfp_mask & __GFP_WAIT) {
1399 spin_unlock_irq(cfqd->queue->queue_lock);
1400 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1401 spin_lock_irq(cfqd->queue->queue_lock);
1404 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1409 memset(cfqq, 0, sizeof(*cfqq));
1411 INIT_HLIST_NODE(&cfqq->cfq_hash);
1412 INIT_LIST_HEAD(&cfqq->cfq_list);
1413 RB_CLEAR_ROOT(&cfqq->sort_list);
1414 INIT_LIST_HEAD(&cfqq->fifo);
1417 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1418 atomic_set(&cfqq->ref, 0);
1420 atomic_inc(&cfqd->ref);
1421 cfqq->service_last = 0;
1423 * set ->slice_left to allow preemption for a new process
1425 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
1426 cfqq->idle_window = 1;
1428 cfqq->ioprio_class = -1;
1429 cfqq->prio_changed = 1;
1433 kmem_cache_free(cfq_pool, new_cfqq);
1435 atomic_inc(&cfqq->ref);
1437 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1442 * Setup general io context and cfq io context. There can be several cfq
1443 * io contexts per general io context, if this process is doing io to more
1444 * than one device managed by cfq. Note that caller is holding a reference to
1445 * cfqq, so we don't need to worry about it disappearing
1447 static struct cfq_io_context *
1448 cfq_get_io_context(struct cfq_data *cfqd, pid_t pid, int gfp_mask)
1450 struct io_context *ioc = NULL;
1451 struct cfq_io_context *cic;
1453 might_sleep_if(gfp_mask & __GFP_WAIT);
1455 ioc = get_io_context(gfp_mask);
1459 if ((cic = ioc->cic) == NULL) {
1460 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1466 * manually increment generic io_context usage count, it
1467 * cannot go away since we are already holding one ref to it
1470 ioc->set_ioprio = cfq_ioc_set_ioprio;
1473 atomic_inc(&cfqd->ref);
1475 struct cfq_io_context *__cic;
1478 * the first cic on the list is actually the head itself
1480 if (cic->key == cfqd)
1484 * cic exists, check if we already are there. linear search
1485 * should be ok here, the list will usually not be more than
1486 * 1 or a few entries long
1488 list_for_each_entry(__cic, &cic->list, list) {
1490 * this process is already holding a reference to
1491 * this queue, so no need to get one more
1493 if (__cic->key == cfqd) {
1500 * nope, process doesn't have a cic assoicated with this
1501 * cfqq yet. get a new one and add to list
1503 __cic = cfq_alloc_io_context(cfqd, gfp_mask);
1509 atomic_inc(&cfqd->ref);
1510 list_add(&__cic->list, &cic->list);
1517 put_io_context(ioc);
1522 cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1524 unsigned long elapsed, ttime;
1527 * if this context already has stuff queued, thinktime is from
1528 * last queue not last end
1531 if (time_after(cic->last_end_request, cic->last_queue))
1532 elapsed = jiffies - cic->last_end_request;
1534 elapsed = jiffies - cic->last_queue;
1536 elapsed = jiffies - cic->last_end_request;
1539 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1541 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1542 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1543 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1546 #define sample_valid(samples) ((samples) > 80)
1549 * Disable idle window if the process thinks too long or seeks so much that
1553 cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1554 struct cfq_io_context *cic)
1556 int enable_idle = cfqq->idle_window;
1558 if (!cic->ioc->task || !cfqd->cfq_slice_idle)
1560 else if (sample_valid(cic->ttime_samples)) {
1561 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1567 cfqq->idle_window = enable_idle;
1572 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1573 * no or if we aren't sure, a 1 will cause a preempt.
1576 cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1579 struct cfq_queue *cfqq = cfqd->active_queue;
1581 if (cfq_class_idle(new_cfqq))
1587 if (cfq_class_idle(cfqq))
1589 if (!new_cfqq->wait_request)
1592 * if it doesn't have slice left, forget it
1594 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1596 if (crq->is_sync && !cfq_cfqq_sync(cfqq))
1603 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1604 * let it have half of its nominal slice.
1606 static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1608 struct cfq_queue *__cfqq, *next;
1610 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1611 cfq_resort_rr_list(__cfqq, 1);
1613 if (!cfqq->slice_left)
1614 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1616 cfqq->slice_end = cfqq->slice_left + jiffies;
1617 cfq_slice_expired(cfqd, 1);
1618 __cfq_set_active_queue(cfqd, cfqq);
1622 * should really be a ll_rw_blk.c helper
1624 static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1626 request_queue_t *q = cfqd->queue;
1628 if (!blk_queue_plugged(q))
1631 __generic_unplug_device(q);
1635 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1636 * something we should do about it
1639 cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1642 const int sync = crq->is_sync;
1644 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
1647 struct cfq_io_context *cic = crq->io_context;
1649 cfq_update_io_thinktime(cfqd, cic);
1650 cfq_update_idle_window(cfqd, cfqq, cic);
1652 cic->last_queue = jiffies;
1655 if (cfqq == cfqd->active_queue) {
1657 * if we are waiting for a request for this queue, let it rip
1658 * immediately and flag that we must not expire this queue
1661 if (cfqq->wait_request) {
1662 cfqq->must_dispatch = 1;
1663 del_timer(&cfqd->idle_slice_timer);
1664 cfq_start_queueing(cfqd, cfqq);
1666 } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
1668 * not the active queue - expire current slice if it is
1669 * idle and has expired it's mean thinktime or this new queue
1670 * has some old slice time left and is of higher priority
1672 cfq_preempt_queue(cfqd, cfqq);
1673 cfqq->must_dispatch = 1;
1674 cfq_start_queueing(cfqd, cfqq);
1678 static void cfq_enqueue(struct cfq_data *cfqd, struct request *rq)
1680 struct cfq_rq *crq = RQ_DATA(rq);
1681 struct cfq_queue *cfqq = crq->cfq_queue;
1683 cfq_init_prio_data(cfqq);
1685 cfq_add_crq_rb(crq);
1687 list_add_tail(&rq->queuelist, &cfqq->fifo);
1689 if (rq_mergeable(rq)) {
1690 cfq_add_crq_hash(cfqd, crq);
1692 if (!cfqd->queue->last_merge)
1693 cfqd->queue->last_merge = rq;
1696 cfq_crq_enqueued(cfqd, cfqq, crq);
1700 cfq_insert_request(request_queue_t *q, struct request *rq, int where)
1702 struct cfq_data *cfqd = q->elevator->elevator_data;
1705 case ELEVATOR_INSERT_BACK:
1706 while (cfq_dispatch_requests(q, INT_MAX, 1))
1708 list_add_tail(&rq->queuelist, &q->queue_head);
1710 * If we were idling with pending requests on
1711 * inactive cfqqs, force dispatching will
1712 * remove the idle timer and the queue won't
1713 * be kicked by __make_request() afterward.
1716 kblockd_schedule_work(&cfqd->unplug_work);
1718 case ELEVATOR_INSERT_FRONT:
1719 list_add(&rq->queuelist, &q->queue_head);
1721 case ELEVATOR_INSERT_SORT:
1722 BUG_ON(!blk_fs_request(rq));
1723 cfq_enqueue(cfqd, rq);
1726 printk("%s: bad insert point %d\n", __FUNCTION__,where);
1731 static inline int cfq_pending_requests(struct cfq_data *cfqd)
1733 return !list_empty(&cfqd->queue->queue_head) || cfqd->busy_queues;
1736 static int cfq_queue_empty(request_queue_t *q)
1738 struct cfq_data *cfqd = q->elevator->elevator_data;
1740 return !cfq_pending_requests(cfqd);
1743 static void cfq_completed_request(request_queue_t *q, struct request *rq)
1745 struct cfq_rq *crq = RQ_DATA(rq);
1746 struct cfq_queue *cfqq;
1748 if (unlikely(!blk_fs_request(rq)))
1751 cfqq = crq->cfq_queue;
1753 if (crq->in_flight) {
1754 WARN_ON(!cfqq->in_flight);
1758 cfq_account_completion(cfqq, crq);
1761 static struct request *
1762 cfq_former_request(request_queue_t *q, struct request *rq)
1764 struct cfq_rq *crq = RQ_DATA(rq);
1765 struct rb_node *rbprev = rb_prev(&crq->rb_node);
1768 return rb_entry_crq(rbprev)->request;
1773 static struct request *
1774 cfq_latter_request(request_queue_t *q, struct request *rq)
1776 struct cfq_rq *crq = RQ_DATA(rq);
1777 struct rb_node *rbnext = rb_next(&crq->rb_node);
1780 return rb_entry_crq(rbnext)->request;
1786 * we temporarily boost lower priority queues if they are holding fs exclusive
1787 * resources. they are boosted to normal prio (CLASS_BE/4)
1789 static void cfq_prio_boost(struct cfq_queue *cfqq)
1791 const int ioprio_class = cfqq->ioprio_class;
1792 const int ioprio = cfqq->ioprio;
1794 if (has_fs_excl()) {
1796 * boost idle prio on transactions that would lock out other
1797 * users of the filesystem
1799 if (cfq_class_idle(cfqq))
1800 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1801 if (cfqq->ioprio > IOPRIO_NORM)
1802 cfqq->ioprio = IOPRIO_NORM;
1805 * check if we need to unboost the queue
1807 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1808 cfqq->ioprio_class = cfqq->org_ioprio_class;
1809 if (cfqq->ioprio != cfqq->org_ioprio)
1810 cfqq->ioprio = cfqq->org_ioprio;
1814 * refile between round-robin lists if we moved the priority class
1816 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
1818 cfq_resort_rr_list(cfqq, 0);
1821 static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
1823 if (rw == READ || process_sync(task))
1826 return CFQ_KEY_ASYNC;
1830 __cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1831 struct task_struct *task, int rw)
1833 if (cfqq->wait_request && cfqq->must_alloc)
1834 return ELV_MQUEUE_MUST;
1836 return ELV_MQUEUE_MAY;
1838 if (!cfqq || task->flags & PF_MEMALLOC)
1839 return ELV_MQUEUE_MAY;
1840 if (!cfqq->allocated[rw] || cfqq->must_alloc) {
1841 if (cfqq->wait_request)
1842 return ELV_MQUEUE_MUST;
1845 * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
1846 * can quickly flood the queue with writes from a single task
1848 if (rw == READ || !cfqq->must_alloc_slice) {
1849 cfqq->must_alloc_slice = 1;
1850 return ELV_MQUEUE_MUST;
1853 return ELV_MQUEUE_MAY;
1855 if (cfq_class_idle(cfqq))
1856 return ELV_MQUEUE_NO;
1857 if (cfqq->allocated[rw] >= cfqd->max_queued) {
1858 struct io_context *ioc = get_io_context(GFP_ATOMIC);
1859 int ret = ELV_MQUEUE_NO;
1861 if (ioc && ioc->nr_batch_requests)
1862 ret = ELV_MQUEUE_MAY;
1864 put_io_context(ioc);
1868 return ELV_MQUEUE_MAY;
1872 static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1874 struct cfq_data *cfqd = q->elevator->elevator_data;
1875 struct task_struct *tsk = current;
1876 struct cfq_queue *cfqq;
1879 * don't force setup of a queue from here, as a call to may_queue
1880 * does not necessarily imply that a request actually will be queued.
1881 * so just lookup a possibly existing queue, or return 'may queue'
1884 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw));
1886 cfq_init_prio_data(cfqq);
1887 cfq_prio_boost(cfqq);
1889 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
1892 return ELV_MQUEUE_MAY;
1895 static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
1897 struct cfq_data *cfqd = q->elevator->elevator_data;
1898 struct request_list *rl = &q->rq;
1900 if (cfqq->allocated[READ] <= cfqd->max_queued || cfqd->rq_starved) {
1902 if (waitqueue_active(&rl->wait[READ]))
1903 wake_up(&rl->wait[READ]);
1906 if (cfqq->allocated[WRITE] <= cfqd->max_queued || cfqd->rq_starved) {
1908 if (waitqueue_active(&rl->wait[WRITE]))
1909 wake_up(&rl->wait[WRITE]);
1914 * queue lock held here
1916 static void cfq_put_request(request_queue_t *q, struct request *rq)
1918 struct cfq_data *cfqd = q->elevator->elevator_data;
1919 struct cfq_rq *crq = RQ_DATA(rq);
1922 struct cfq_queue *cfqq = crq->cfq_queue;
1923 const int rw = rq_data_dir(rq);
1925 BUG_ON(!cfqq->allocated[rw]);
1926 cfqq->allocated[rw]--;
1928 put_io_context(crq->io_context->ioc);
1930 mempool_free(crq, cfqd->crq_pool);
1931 rq->elevator_private = NULL;
1933 cfq_check_waiters(q, cfqq);
1934 cfq_put_queue(cfqq);
1939 * Allocate cfq data structures associated with this request.
1942 cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
1945 struct cfq_data *cfqd = q->elevator->elevator_data;
1946 struct cfq_io_context *cic;
1947 const int rw = rq_data_dir(rq);
1948 struct cfq_queue *cfqq;
1950 unsigned long flags;
1952 might_sleep_if(gfp_mask & __GFP_WAIT);
1954 cic = cfq_get_io_context(cfqd, cfq_queue_pid(current, rw), gfp_mask);
1956 spin_lock_irqsave(q->queue_lock, flags);
1962 cfqq = cfq_get_queue(cfqd, current->pid, gfp_mask);
1970 cfqq->allocated[rw]++;
1971 cfqq->must_alloc = 0;
1972 cfqd->rq_starved = 0;
1973 atomic_inc(&cfqq->ref);
1974 spin_unlock_irqrestore(q->queue_lock, flags);
1976 crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
1978 RB_CLEAR(&crq->rb_node);
1981 INIT_HLIST_NODE(&crq->hash);
1982 crq->cfq_queue = cfqq;
1983 crq->io_context = cic;
1984 crq->in_flight = crq->accounted = 0;
1985 crq->is_sync = (rw == READ || process_sync(current));
1987 rq->elevator_private = crq;
1991 spin_lock_irqsave(q->queue_lock, flags);
1992 cfqq->allocated[rw]--;
1993 if (!(cfqq->allocated[0] + cfqq->allocated[1]))
1994 cfqq->must_alloc = 1;
1995 cfq_put_queue(cfqq);
1998 put_io_context(cic->ioc);
2000 * mark us rq allocation starved. we need to kickstart the process
2001 * ourselves if there are no pending requests that can do it for us.
2002 * that would be an extremely rare OOM situation
2004 cfqd->rq_starved = 1;
2005 kblockd_schedule_work(&cfqd->unplug_work);
2006 spin_unlock_irqrestore(q->queue_lock, flags);
2010 static void cfq_kick_queue(void *data)
2012 request_queue_t *q = data;
2013 struct cfq_data *cfqd = q->elevator->elevator_data;
2014 unsigned long flags;
2016 spin_lock_irqsave(q->queue_lock, flags);
2018 if (cfqd->rq_starved) {
2019 struct request_list *rl = &q->rq;
2022 * we aren't guaranteed to get a request after this, but we
2023 * have to be opportunistic
2026 if (waitqueue_active(&rl->wait[READ]))
2027 wake_up(&rl->wait[READ]);
2028 if (waitqueue_active(&rl->wait[WRITE]))
2029 wake_up(&rl->wait[WRITE]);
2034 spin_unlock_irqrestore(q->queue_lock, flags);
2038 * Timer running if the active_queue is currently idling inside its time slice
2040 static void cfq_idle_slice_timer(unsigned long data)
2042 struct cfq_data *cfqd = (struct cfq_data *) data;
2043 struct cfq_queue *cfqq;
2044 unsigned long flags;
2046 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2048 if ((cfqq = cfqd->active_queue) != NULL) {
2049 unsigned long now = jiffies;
2054 if (time_after(now, cfqq->slice_end))
2058 * only expire and reinvoke request handler, if there are
2059 * other queues with pending requests
2061 if (!cfq_pending_requests(cfqd)) {
2062 cfqd->idle_slice_timer.expires = min(now + cfqd->cfq_slice_idle, cfqq->slice_end);
2063 add_timer(&cfqd->idle_slice_timer);
2068 * not expired and it has a request pending, let it dispatch
2070 if (!RB_EMPTY(&cfqq->sort_list)) {
2071 cfqq->must_dispatch = 1;
2076 cfq_slice_expired(cfqd, 0);
2078 if (cfq_pending_requests(cfqd))
2079 kblockd_schedule_work(&cfqd->unplug_work);
2081 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2085 * Timer running if an idle class queue is waiting for service
2087 static void cfq_idle_class_timer(unsigned long data)
2089 struct cfq_data *cfqd = (struct cfq_data *) data;
2090 unsigned long flags, end;
2092 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2095 * race with a non-idle queue, reset timer
2097 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
2098 if (!time_after_eq(jiffies, end)) {
2099 cfqd->idle_class_timer.expires = end;
2100 add_timer(&cfqd->idle_class_timer);
2102 kblockd_schedule_work(&cfqd->unplug_work);
2104 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2108 static void cfq_put_cfqd(struct cfq_data *cfqd)
2110 request_queue_t *q = cfqd->queue;
2112 if (!atomic_dec_and_test(&cfqd->ref))
2119 mempool_destroy(cfqd->crq_pool);
2120 kfree(cfqd->crq_hash);
2121 kfree(cfqd->cfq_hash);
2125 static void cfq_exit_queue(elevator_t *e)
2127 struct cfq_data *cfqd = e->elevator_data;
2129 del_timer_sync(&cfqd->idle_slice_timer);
2130 del_timer_sync(&cfqd->idle_class_timer);
2134 static int cfq_init_queue(request_queue_t *q, elevator_t *e)
2136 struct cfq_data *cfqd;
2139 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2143 memset(cfqd, 0, sizeof(*cfqd));
2145 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2146 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2148 INIT_LIST_HEAD(&cfqd->busy_rr);
2149 INIT_LIST_HEAD(&cfqd->cur_rr);
2150 INIT_LIST_HEAD(&cfqd->idle_rr);
2151 INIT_LIST_HEAD(&cfqd->empty_list);
2153 cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
2154 if (!cfqd->crq_hash)
2157 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2158 if (!cfqd->cfq_hash)
2161 cfqd->crq_pool = mempool_create(BLKDEV_MIN_RQ, mempool_alloc_slab, mempool_free_slab, crq_pool);
2162 if (!cfqd->crq_pool)
2165 for (i = 0; i < CFQ_MHASH_ENTRIES; i++)
2166 INIT_HLIST_HEAD(&cfqd->crq_hash[i]);
2167 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2168 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2170 e->elevator_data = cfqd;
2173 atomic_inc(&q->refcnt);
2175 cfqd->max_queued = q->nr_requests / 4;
2176 q->nr_batching = cfq_queued;
2178 init_timer(&cfqd->idle_slice_timer);
2179 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2180 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2182 init_timer(&cfqd->idle_class_timer);
2183 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2184 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2186 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2188 atomic_set(&cfqd->ref, 1);
2190 cfqd->cfq_queued = cfq_queued;
2191 cfqd->cfq_quantum = cfq_quantum;
2192 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2193 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
2194 cfqd->cfq_back_max = cfq_back_max;
2195 cfqd->cfq_back_penalty = cfq_back_penalty;
2196 cfqd->cfq_slice[0] = cfq_slice_async;
2197 cfqd->cfq_slice[1] = cfq_slice_sync;
2198 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2199 cfqd->cfq_slice_idle = cfq_slice_idle;
2200 cfqd->cfq_max_depth = cfq_max_depth;
2203 kfree(cfqd->cfq_hash);
2205 kfree(cfqd->crq_hash);
2211 static void cfq_slab_kill(void)
2214 kmem_cache_destroy(crq_pool);
2216 kmem_cache_destroy(cfq_pool);
2218 kmem_cache_destroy(cfq_ioc_pool);
2221 static int __init cfq_slab_setup(void)
2223 crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
2228 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2233 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2234 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2245 * sysfs parts below -->
2247 struct cfq_fs_entry {
2248 struct attribute attr;
2249 ssize_t (*show)(struct cfq_data *, char *);
2250 ssize_t (*store)(struct cfq_data *, const char *, size_t);
2254 cfq_var_show(unsigned int var, char *page)
2256 return sprintf(page, "%d\n", var);
2260 cfq_var_store(unsigned int *var, const char *page, size_t count)
2262 char *p = (char *) page;
2264 *var = simple_strtoul(p, &p, 10);
2268 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2269 static ssize_t __FUNC(struct cfq_data *cfqd, char *page) \
2271 unsigned int __data = __VAR; \
2273 __data = jiffies_to_msecs(__data); \
2274 return cfq_var_show(__data, (page)); \
2276 SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2277 SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
2278 SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2279 SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
2280 SHOW_FUNCTION(cfq_back_max_show, cfqd->cfq_back_max, 0);
2281 SHOW_FUNCTION(cfq_back_penalty_show, cfqd->cfq_back_penalty, 0);
2282 SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2283 SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2284 SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2285 SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2286 SHOW_FUNCTION(cfq_max_depth_show, cfqd->cfq_max_depth, 0);
2287 #undef SHOW_FUNCTION
2289 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2290 static ssize_t __FUNC(struct cfq_data *cfqd, const char *page, size_t count) \
2292 unsigned int __data; \
2293 int ret = cfq_var_store(&__data, (page), count); \
2294 if (__data < (MIN)) \
2296 else if (__data > (MAX)) \
2299 *(__PTR) = msecs_to_jiffies(__data); \
2301 *(__PTR) = __data; \
2304 STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2305 STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
2306 STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2307 STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
2308 STORE_FUNCTION(cfq_back_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2309 STORE_FUNCTION(cfq_back_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
2310 STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2311 STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2312 STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2313 STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
2314 STORE_FUNCTION(cfq_max_depth_store, &cfqd->cfq_max_depth, 1, UINT_MAX, 0);
2315 #undef STORE_FUNCTION
2317 static struct cfq_fs_entry cfq_quantum_entry = {
2318 .attr = {.name = "quantum", .mode = S_IRUGO | S_IWUSR },
2319 .show = cfq_quantum_show,
2320 .store = cfq_quantum_store,
2322 static struct cfq_fs_entry cfq_queued_entry = {
2323 .attr = {.name = "queued", .mode = S_IRUGO | S_IWUSR },
2324 .show = cfq_queued_show,
2325 .store = cfq_queued_store,
2327 static struct cfq_fs_entry cfq_fifo_expire_sync_entry = {
2328 .attr = {.name = "fifo_expire_sync", .mode = S_IRUGO | S_IWUSR },
2329 .show = cfq_fifo_expire_sync_show,
2330 .store = cfq_fifo_expire_sync_store,
2332 static struct cfq_fs_entry cfq_fifo_expire_async_entry = {
2333 .attr = {.name = "fifo_expire_async", .mode = S_IRUGO | S_IWUSR },
2334 .show = cfq_fifo_expire_async_show,
2335 .store = cfq_fifo_expire_async_store,
2337 static struct cfq_fs_entry cfq_back_max_entry = {
2338 .attr = {.name = "back_seek_max", .mode = S_IRUGO | S_IWUSR },
2339 .show = cfq_back_max_show,
2340 .store = cfq_back_max_store,
2342 static struct cfq_fs_entry cfq_back_penalty_entry = {
2343 .attr = {.name = "back_seek_penalty", .mode = S_IRUGO | S_IWUSR },
2344 .show = cfq_back_penalty_show,
2345 .store = cfq_back_penalty_store,
2347 static struct cfq_fs_entry cfq_slice_sync_entry = {
2348 .attr = {.name = "slice_sync", .mode = S_IRUGO | S_IWUSR },
2349 .show = cfq_slice_sync_show,
2350 .store = cfq_slice_sync_store,
2352 static struct cfq_fs_entry cfq_slice_async_entry = {
2353 .attr = {.name = "slice_async", .mode = S_IRUGO | S_IWUSR },
2354 .show = cfq_slice_async_show,
2355 .store = cfq_slice_async_store,
2357 static struct cfq_fs_entry cfq_slice_async_rq_entry = {
2358 .attr = {.name = "slice_async_rq", .mode = S_IRUGO | S_IWUSR },
2359 .show = cfq_slice_async_rq_show,
2360 .store = cfq_slice_async_rq_store,
2362 static struct cfq_fs_entry cfq_slice_idle_entry = {
2363 .attr = {.name = "slice_idle", .mode = S_IRUGO | S_IWUSR },
2364 .show = cfq_slice_idle_show,
2365 .store = cfq_slice_idle_store,
2367 static struct cfq_fs_entry cfq_max_depth_entry = {
2368 .attr = {.name = "max_depth", .mode = S_IRUGO | S_IWUSR },
2369 .show = cfq_max_depth_show,
2370 .store = cfq_max_depth_store,
2372 static struct attribute *default_attrs[] = {
2373 &cfq_quantum_entry.attr,
2374 &cfq_queued_entry.attr,
2375 &cfq_fifo_expire_sync_entry.attr,
2376 &cfq_fifo_expire_async_entry.attr,
2377 &cfq_back_max_entry.attr,
2378 &cfq_back_penalty_entry.attr,
2379 &cfq_slice_sync_entry.attr,
2380 &cfq_slice_async_entry.attr,
2381 &cfq_slice_async_rq_entry.attr,
2382 &cfq_slice_idle_entry.attr,
2383 &cfq_max_depth_entry.attr,
2387 #define to_cfq(atr) container_of((atr), struct cfq_fs_entry, attr)
2390 cfq_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2392 elevator_t *e = container_of(kobj, elevator_t, kobj);
2393 struct cfq_fs_entry *entry = to_cfq(attr);
2398 return entry->show(e->elevator_data, page);
2402 cfq_attr_store(struct kobject *kobj, struct attribute *attr,
2403 const char *page, size_t length)
2405 elevator_t *e = container_of(kobj, elevator_t, kobj);
2406 struct cfq_fs_entry *entry = to_cfq(attr);
2411 return entry->store(e->elevator_data, page, length);
2414 static struct sysfs_ops cfq_sysfs_ops = {
2415 .show = cfq_attr_show,
2416 .store = cfq_attr_store,
2419 static struct kobj_type cfq_ktype = {
2420 .sysfs_ops = &cfq_sysfs_ops,
2421 .default_attrs = default_attrs,
2424 static struct elevator_type iosched_cfq = {
2426 .elevator_merge_fn = cfq_merge,
2427 .elevator_merged_fn = cfq_merged_request,
2428 .elevator_merge_req_fn = cfq_merged_requests,
2429 .elevator_next_req_fn = cfq_next_request,
2430 .elevator_add_req_fn = cfq_insert_request,
2431 .elevator_remove_req_fn = cfq_remove_request,
2432 .elevator_requeue_req_fn = cfq_requeue_request,
2433 .elevator_deactivate_req_fn = cfq_deactivate_request,
2434 .elevator_queue_empty_fn = cfq_queue_empty,
2435 .elevator_completed_req_fn = cfq_completed_request,
2436 .elevator_former_req_fn = cfq_former_request,
2437 .elevator_latter_req_fn = cfq_latter_request,
2438 .elevator_set_req_fn = cfq_set_request,
2439 .elevator_put_req_fn = cfq_put_request,
2440 .elevator_may_queue_fn = cfq_may_queue,
2441 .elevator_init_fn = cfq_init_queue,
2442 .elevator_exit_fn = cfq_exit_queue,
2444 .elevator_ktype = &cfq_ktype,
2445 .elevator_name = "cfq",
2446 .elevator_owner = THIS_MODULE,
2449 static int __init cfq_init(void)
2454 * could be 0 on HZ < 1000 setups
2456 if (!cfq_slice_async)
2457 cfq_slice_async = 1;
2458 if (!cfq_slice_idle)
2461 if (cfq_slab_setup())
2464 ret = elv_register(&iosched_cfq);
2471 static void __exit cfq_exit(void)
2473 struct task_struct *g, *p;
2474 unsigned long flags;
2476 read_lock_irqsave(&tasklist_lock, flags);
2479 * iterate each process in the system, removing our io_context
2481 do_each_thread(g, p) {
2482 struct io_context *ioc = p->io_context;
2484 if (ioc && ioc->cic) {
2485 ioc->cic->exit(ioc->cic);
2486 cfq_free_io_context(ioc->cic);
2489 } while_each_thread(g, p);
2491 read_unlock_irqrestore(&tasklist_lock, flags);
2494 elv_unregister(&iosched_cfq);
2497 module_init(cfq_init);
2498 module_exit(cfq_exit);
2500 MODULE_AUTHOR("Jens Axboe");
2501 MODULE_LICENSE("GPL");
2502 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");