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>
28 static int cfq_quantum = 4;
29 static int cfq_queued = 8;
31 #define CFQ_QHASH_SHIFT 6
32 #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
33 #define list_entry_qhash(entry) list_entry((entry), struct cfq_queue, cfq_hash)
35 #define CFQ_MHASH_SHIFT 8
36 #define CFQ_MHASH_BLOCK(sec) ((sec) >> 3)
37 #define CFQ_MHASH_ENTRIES (1 << CFQ_MHASH_SHIFT)
38 #define CFQ_MHASH_FN(sec) (hash_long(CFQ_MHASH_BLOCK((sec)),CFQ_MHASH_SHIFT))
39 #define ON_MHASH(crq) !list_empty(&(crq)->hash)
40 #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
41 #define list_entry_hash(ptr) list_entry((ptr), struct cfq_rq, hash)
43 #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
45 #define RQ_DATA(rq) ((struct cfq_rq *) (rq)->elevator_private)
47 static kmem_cache_t *crq_pool;
48 static kmem_cache_t *cfq_pool;
49 static mempool_t *cfq_mpool;
52 struct list_head rr_list;
53 struct list_head *dispatch;
54 struct list_head *cfq_hash;
56 struct list_head *crq_hash;
58 unsigned int busy_queues;
59 unsigned int max_queued;
63 request_queue_t *queue;
68 unsigned int cfq_quantum;
69 unsigned int cfq_queued;
73 struct list_head cfq_hash;
74 struct list_head cfq_list;
75 struct rb_root sort_list;
80 * with a simple addition like this, we can do io priorities. almost.
81 * does need a split request free list, too.
88 struct rb_node rb_node;
91 struct request *request;
93 struct cfq_queue *cfq_queue;
95 struct list_head hash;
98 static void cfq_put_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq);
99 static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *cfqd, int pid);
100 static void cfq_dispatch_sort(struct cfq_data *cfqd, struct cfq_queue *cfqq,
104 * lots of deadline iosched dupes, can be abstracted later...
106 static inline void __cfq_del_crq_hash(struct cfq_rq *crq)
108 list_del_init(&crq->hash);
111 static inline void cfq_del_crq_hash(struct cfq_rq *crq)
114 __cfq_del_crq_hash(crq);
117 static void cfq_remove_merge_hints(request_queue_t *q, struct cfq_rq *crq)
119 cfq_del_crq_hash(crq);
121 if (q->last_merge == crq->request)
122 q->last_merge = NULL;
125 static inline void cfq_add_crq_hash(struct cfq_data *cfqd, struct cfq_rq *crq)
127 struct request *rq = crq->request;
129 BUG_ON(ON_MHASH(crq));
131 list_add(&crq->hash, &cfqd->crq_hash[CFQ_MHASH_FN(rq_hash_key(rq))]);
134 static struct request *cfq_find_rq_hash(struct cfq_data *cfqd, sector_t offset)
136 struct list_head *hash_list = &cfqd->crq_hash[CFQ_MHASH_FN(offset)];
137 struct list_head *entry, *next = hash_list->next;
139 while ((entry = next) != hash_list) {
140 struct cfq_rq *crq = list_entry_hash(entry);
141 struct request *__rq = crq->request;
145 BUG_ON(!ON_MHASH(crq));
147 if (!rq_mergeable(__rq)) {
148 __cfq_del_crq_hash(crq);
152 if (rq_hash_key(__rq) == offset)
160 * rb tree support functions
163 #define RB_EMPTY(node) ((node)->rb_node == NULL)
164 #define RB_CLEAR(node) ((node)->rb_color = RB_NONE)
165 #define RB_CLEAR_ROOT(root) ((root)->rb_node = NULL)
166 #define ON_RB(node) ((node)->rb_color != RB_NONE)
167 #define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
168 #define rq_rb_key(rq) (rq)->sector
170 static inline void cfq_del_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
172 if (ON_RB(&crq->rb_node)) {
173 cfqq->queued[rq_data_dir(crq->request)]--;
174 rb_erase(&crq->rb_node, &cfqq->sort_list);
175 crq->cfq_queue = NULL;
179 static struct cfq_rq *
180 __cfq_add_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
182 struct rb_node **p = &cfqq->sort_list.rb_node;
183 struct rb_node *parent = NULL;
184 struct cfq_rq *__crq;
188 __crq = rb_entry_crq(parent);
190 if (crq->rb_key < __crq->rb_key)
192 else if (crq->rb_key > __crq->rb_key)
198 rb_link_node(&crq->rb_node, parent, p);
203 cfq_add_crq_rb(struct cfq_data *cfqd, struct cfq_queue *cfqq,struct cfq_rq *crq)
205 struct request *rq = crq->request;
206 struct cfq_rq *__alias;
208 crq->rb_key = rq_rb_key(rq);
209 cfqq->queued[rq_data_dir(rq)]++;
211 __alias = __cfq_add_crq_rb(cfqq, crq);
213 rb_insert_color(&crq->rb_node, &cfqq->sort_list);
214 crq->cfq_queue = cfqq;
218 cfq_dispatch_sort(cfqd, cfqq, __alias);
222 static struct request *
223 cfq_find_rq_rb(struct cfq_data *cfqd, sector_t sector)
225 struct cfq_queue *cfqq = cfq_find_cfq_hash(cfqd, current->tgid);
231 n = cfqq->sort_list.rb_node;
233 struct cfq_rq *crq = rb_entry_crq(n);
235 if (sector < crq->rb_key)
237 else if (sector > crq->rb_key)
247 static void cfq_remove_request(request_queue_t *q, struct request *rq)
249 struct cfq_data *cfqd = q->elevator.elevator_data;
250 struct cfq_rq *crq = RQ_DATA(rq);
253 struct cfq_queue *cfqq = crq->cfq_queue;
255 cfq_remove_merge_hints(q, crq);
256 list_del_init(&rq->queuelist);
259 cfq_del_crq_rb(cfqq, crq);
261 if (RB_EMPTY(&cfqq->sort_list))
262 cfq_put_queue(cfqd, cfqq);
268 cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
270 struct cfq_data *cfqd = q->elevator.elevator_data;
271 struct request *__rq;
274 ret = elv_try_last_merge(q, bio);
275 if (ret != ELEVATOR_NO_MERGE) {
276 __rq = q->last_merge;
280 __rq = cfq_find_rq_hash(cfqd, bio->bi_sector);
282 BUG_ON(__rq->sector + __rq->nr_sectors != bio->bi_sector);
284 if (elv_rq_merge_ok(__rq, bio)) {
285 ret = ELEVATOR_BACK_MERGE;
290 __rq = cfq_find_rq_rb(cfqd, bio->bi_sector + bio_sectors(bio));
292 if (elv_rq_merge_ok(__rq, bio)) {
293 ret = ELEVATOR_FRONT_MERGE;
298 return ELEVATOR_NO_MERGE;
300 q->last_merge = __rq;
306 static void cfq_merged_request(request_queue_t *q, struct request *req)
308 struct cfq_data *cfqd = q->elevator.elevator_data;
309 struct cfq_rq *crq = RQ_DATA(req);
311 cfq_del_crq_hash(crq);
312 cfq_add_crq_hash(cfqd, crq);
314 if (ON_RB(&crq->rb_node) && (rq_rb_key(req) != crq->rb_key)) {
315 struct cfq_queue *cfqq = crq->cfq_queue;
317 cfq_del_crq_rb(cfqq, crq);
318 cfq_add_crq_rb(cfqd, cfqq, crq);
325 cfq_merged_requests(request_queue_t *q, struct request *req,
326 struct request *next)
328 cfq_merged_request(q, req);
329 cfq_remove_request(q, next);
333 cfq_dispatch_sort(struct cfq_data *cfqd, struct cfq_queue *cfqq,
336 struct list_head *head = cfqd->dispatch, *entry = head;
337 struct request *__rq;
339 cfq_del_crq_rb(cfqq, crq);
340 cfq_remove_merge_hints(cfqd->queue, crq);
342 if (!list_empty(head)) {
343 __rq = list_entry_rq(head->next);
345 if (crq->request->sector < __rq->sector) {
351 while ((entry = entry->prev) != head) {
352 __rq = list_entry_rq(entry);
354 if (crq->request->sector <= __rq->sector)
359 list_add_tail(&crq->request->queuelist, entry);
363 __cfq_dispatch_requests(request_queue_t *q, struct cfq_data *cfqd,
364 struct cfq_queue *cfqq)
366 struct cfq_rq *crq = rb_entry_crq(rb_first(&cfqq->sort_list));
368 cfq_dispatch_sort(cfqd, cfqq, crq);
371 static int cfq_dispatch_requests(request_queue_t *q, struct cfq_data *cfqd)
373 struct cfq_queue *cfqq;
374 struct list_head *entry, *tmp;
375 int ret, queued, good_queues;
377 if (list_empty(&cfqd->rr_list))
383 list_for_each_safe(entry, tmp, &cfqd->rr_list) {
384 cfqq = list_entry_cfqq(cfqd->rr_list.next);
386 BUG_ON(RB_EMPTY(&cfqq->sort_list));
388 __cfq_dispatch_requests(q, cfqd, cfqq);
390 if (RB_EMPTY(&cfqq->sort_list))
391 cfq_put_queue(cfqd, cfqq);
399 if ((queued < cfqd->cfq_quantum) && good_queues)
405 static struct request *cfq_next_request(request_queue_t *q)
407 struct cfq_data *cfqd = q->elevator.elevator_data;
410 if (!list_empty(cfqd->dispatch)) {
413 rq = list_entry_rq(cfqd->dispatch->next);
417 cfq_remove_merge_hints(q, crq);
422 if (cfq_dispatch_requests(q, cfqd))
428 static inline struct cfq_queue *
429 __cfq_find_cfq_hash(struct cfq_data *cfqd, int pid, const int hashval)
431 struct list_head *hash_list = &cfqd->cfq_hash[hashval];
432 struct list_head *entry;
434 list_for_each(entry, hash_list) {
435 struct cfq_queue *__cfqq = list_entry_qhash(entry);
437 if (__cfqq->pid == pid)
444 static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *cfqd, int pid)
446 const int hashval = hash_long(current->tgid, CFQ_QHASH_SHIFT);
448 return __cfq_find_cfq_hash(cfqd, pid, hashval);
451 static void cfq_put_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
454 list_del(&cfqq->cfq_list);
455 list_del(&cfqq->cfq_hash);
456 mempool_free(cfqq, cfq_mpool);
459 static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, int pid)
461 const int hashval = hash_long(current->tgid, CFQ_QHASH_SHIFT);
462 struct cfq_queue *cfqq = __cfq_find_cfq_hash(cfqd, pid, hashval);
465 cfqq = mempool_alloc(cfq_mpool, GFP_NOIO);
467 INIT_LIST_HEAD(&cfqq->cfq_hash);
468 INIT_LIST_HEAD(&cfqq->cfq_list);
469 RB_CLEAR_ROOT(&cfqq->sort_list);
472 cfqq->queued[0] = cfqq->queued[1] = 0;
473 list_add(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
479 static void cfq_enqueue(struct cfq_data *cfqd, struct cfq_rq *crq)
481 struct cfq_queue *cfqq;
483 cfqq = cfq_get_queue(cfqd, current->tgid);
485 cfq_add_crq_rb(cfqd, cfqq, crq);
487 if (list_empty(&cfqq->cfq_list)) {
488 list_add(&cfqq->cfq_list, &cfqd->rr_list);
494 cfq_insert_request(request_queue_t *q, struct request *rq, int where)
496 struct cfq_data *cfqd = q->elevator.elevator_data;
497 struct cfq_rq *crq = RQ_DATA(rq);
500 case ELEVATOR_INSERT_BACK:
501 while (cfq_dispatch_requests(q, cfqd))
503 list_add_tail(&rq->queuelist, cfqd->dispatch);
505 case ELEVATOR_INSERT_FRONT:
506 list_add(&rq->queuelist, cfqd->dispatch);
508 case ELEVATOR_INSERT_SORT:
509 BUG_ON(!blk_fs_request(rq));
510 cfq_enqueue(cfqd, crq);
513 printk("%s: bad insert point %d\n", __FUNCTION__,where);
517 if (rq_mergeable(rq)) {
518 cfq_add_crq_hash(cfqd, crq);
525 static int cfq_queue_empty(request_queue_t *q)
527 struct cfq_data *cfqd = q->elevator.elevator_data;
529 if (list_empty(cfqd->dispatch) && list_empty(&cfqd->rr_list))
535 static struct request *
536 cfq_former_request(request_queue_t *q, struct request *rq)
538 struct cfq_rq *crq = RQ_DATA(rq);
539 struct rb_node *rbprev = rb_prev(&crq->rb_node);
542 return rb_entry_crq(rbprev)->request;
547 static struct request *
548 cfq_latter_request(request_queue_t *q, struct request *rq)
550 struct cfq_rq *crq = RQ_DATA(rq);
551 struct rb_node *rbnext = rb_next(&crq->rb_node);
554 return rb_entry_crq(rbnext)->request;
559 static int cfq_may_queue(request_queue_t *q, int rw)
561 struct cfq_data *cfqd = q->elevator.elevator_data;
562 struct cfq_queue *cfqq;
565 if (!cfqd->busy_queues)
568 cfqq = cfq_find_cfq_hash(cfqd, current->tgid);
570 int limit = (q->nr_requests - cfqd->cfq_queued) / cfqd->busy_queues;
574 else if (limit > cfqd->max_queued)
575 limit = cfqd->max_queued;
577 if (cfqq->queued[rw] > limit)
584 static void cfq_put_request(request_queue_t *q, struct request *rq)
586 struct cfq_data *cfqd = q->elevator.elevator_data;
587 struct cfq_rq *crq = RQ_DATA(rq);
588 struct request_list *rl;
592 BUG_ON(q->last_merge == rq);
593 BUG_ON(ON_MHASH(crq));
595 mempool_free(crq, cfqd->crq_pool);
596 rq->elevator_private = NULL;
600 * work-around for may_queue "bug": if a read gets issued and refused
601 * to queue because writes ate all the allowed slots and no other
602 * reads are pending for this queue, it could get stuck infinitely
603 * since freed_request() only checks the waitqueue for writes when
604 * freeing them. or vice versa for a single write vs many reads.
605 * so check here whether "the other" data direction might be able
606 * to queue and wake them
609 other_rw = rq_data_dir(rq) ^ 1;
610 if (rl->count[other_rw] <= q->nr_requests) {
612 if (waitqueue_active(&rl->wait[other_rw]))
613 wake_up(&rl->wait[other_rw]);
617 static int cfq_set_request(request_queue_t *q, struct request *rq, int gfp_mask)
619 struct cfq_data *cfqd = q->elevator.elevator_data;
620 struct cfq_rq *crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
623 RB_CLEAR(&crq->rb_node);
625 crq->cfq_queue = NULL;
626 INIT_LIST_HEAD(&crq->hash);
627 rq->elevator_private = crq;
634 static void cfq_exit(request_queue_t *q, elevator_t *e)
636 struct cfq_data *cfqd = e->elevator_data;
638 e->elevator_data = NULL;
639 mempool_destroy(cfqd->crq_pool);
640 kfree(cfqd->crq_hash);
641 kfree(cfqd->cfq_hash);
645 static int cfq_init(request_queue_t *q, elevator_t *e)
647 struct cfq_data *cfqd;
650 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
654 memset(cfqd, 0, sizeof(*cfqd));
655 INIT_LIST_HEAD(&cfqd->rr_list);
657 cfqd->crq_hash = kmalloc(sizeof(struct list_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
661 cfqd->cfq_hash = kmalloc(sizeof(struct list_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
665 cfqd->crq_pool = mempool_create(BLKDEV_MIN_RQ, mempool_alloc_slab, mempool_free_slab, crq_pool);
669 for (i = 0; i < CFQ_MHASH_ENTRIES; i++)
670 INIT_LIST_HEAD(&cfqd->crq_hash[i]);
671 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
672 INIT_LIST_HEAD(&cfqd->cfq_hash[i]);
674 cfqd->dispatch = &q->queue_head;
675 e->elevator_data = cfqd;
679 * just set it to some high value, we want anyone to be able to queue
680 * some requests. fairness is handled differently
682 cfqd->max_queued = q->nr_requests;
683 q->nr_requests = 8192;
685 cfqd->cfq_queued = cfq_queued;
686 cfqd->cfq_quantum = cfq_quantum;
690 kfree(cfqd->cfq_hash);
692 kfree(cfqd->crq_hash);
698 static int __init cfq_slab_setup(void)
700 crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
704 panic("cfq_iosched: can't init crq pool\n");
706 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
710 panic("cfq_iosched: can't init cfq pool\n");
712 cfq_mpool = mempool_create(64, mempool_alloc_slab, mempool_free_slab, cfq_pool);
715 panic("cfq_iosched: can't init cfq mpool\n");
720 subsys_initcall(cfq_slab_setup);
723 * sysfs parts below -->
725 struct cfq_fs_entry {
726 struct attribute attr;
727 ssize_t (*show)(struct cfq_data *, char *);
728 ssize_t (*store)(struct cfq_data *, const char *, size_t);
732 cfq_var_show(unsigned int var, char *page)
734 return sprintf(page, "%d\n", var);
738 cfq_var_store(unsigned int *var, const char *page, size_t count)
740 char *p = (char *) page;
742 *var = simple_strtoul(p, &p, 10);
746 #define SHOW_FUNCTION(__FUNC, __VAR) \
747 static ssize_t __FUNC(struct cfq_data *cfqd, char *page) \
749 return cfq_var_show(__VAR, (page)); \
751 SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum);
752 SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued);
755 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
756 static ssize_t __FUNC(struct cfq_data *cfqd, const char *page, size_t count) \
758 int ret = cfq_var_store(__PTR, (page), count); \
759 if (*(__PTR) < (MIN)) \
761 else if (*(__PTR) > (MAX)) \
765 STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, INT_MAX);
766 STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, INT_MAX);
767 #undef STORE_FUNCTION
769 static struct cfq_fs_entry cfq_quantum_entry = {
770 .attr = {.name = "quantum", .mode = S_IRUGO | S_IWUSR },
771 .show = cfq_quantum_show,
772 .store = cfq_quantum_store,
774 static struct cfq_fs_entry cfq_queued_entry = {
775 .attr = {.name = "queued", .mode = S_IRUGO | S_IWUSR },
776 .show = cfq_queued_show,
777 .store = cfq_queued_store,
780 static struct attribute *default_attrs[] = {
781 &cfq_quantum_entry.attr,
782 &cfq_queued_entry.attr,
786 #define to_cfq(atr) container_of((atr), struct cfq_fs_entry, attr)
789 cfq_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
791 elevator_t *e = container_of(kobj, elevator_t, kobj);
792 struct cfq_fs_entry *entry = to_cfq(attr);
797 return entry->show(e->elevator_data, page);
801 cfq_attr_store(struct kobject *kobj, struct attribute *attr,
802 const char *page, size_t length)
804 elevator_t *e = container_of(kobj, elevator_t, kobj);
805 struct cfq_fs_entry *entry = to_cfq(attr);
810 return entry->store(e->elevator_data, page, length);
813 static struct sysfs_ops cfq_sysfs_ops = {
814 .show = cfq_attr_show,
815 .store = cfq_attr_store,
818 struct kobj_type cfq_ktype = {
819 .sysfs_ops = &cfq_sysfs_ops,
820 .default_attrs = default_attrs,
823 elevator_t iosched_cfq = {
824 .elevator_name = "cfq",
825 .elevator_ktype = &cfq_ktype,
826 .elevator_merge_fn = cfq_merge,
827 .elevator_merged_fn = cfq_merged_request,
828 .elevator_merge_req_fn = cfq_merged_requests,
829 .elevator_next_req_fn = cfq_next_request,
830 .elevator_add_req_fn = cfq_insert_request,
831 .elevator_remove_req_fn = cfq_remove_request,
832 .elevator_queue_empty_fn = cfq_queue_empty,
833 .elevator_former_req_fn = cfq_former_request,
834 .elevator_latter_req_fn = cfq_latter_request,
835 .elevator_set_req_fn = cfq_set_request,
836 .elevator_put_req_fn = cfq_put_request,
837 .elevator_may_queue_fn = cfq_may_queue,
838 .elevator_init_fn = cfq_init,
839 .elevator_exit_fn = cfq_exit,
842 EXPORT_SYMBOL(iosched_cfq);