2 * linux/drivers/block/deadline-iosched.c
4 * Deadline i/o scheduler.
6 * Copyright (C) 2002 Jens Axboe <axboe@suse.de>
8 #include <linux/kernel.h>
10 #include <linux/blkdev.h>
11 #include <linux/elevator.h>
12 #include <linux/bio.h>
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/init.h>
17 #include <linux/compiler.h>
18 #include <linux/hash.h>
19 #include <linux/rbtree.h>
22 * See Documentation/block/deadline-iosched.txt
24 static int read_expire = HZ / 2; /* max time before a read is submitted. */
25 static int write_expire = 5 * HZ; /* ditto for writes, these limits are SOFT! */
26 static int writes_starved = 2; /* max times reads can starve a write */
27 static int fifo_batch = 16; /* # of sequential requests treated as one
28 by the above parameters. For throughput. */
30 static const int deadline_hash_shift = 5;
31 #define DL_HASH_BLOCK(sec) ((sec) >> 3)
32 #define DL_HASH_FN(sec) (hash_long(DL_HASH_BLOCK((sec)), deadline_hash_shift))
33 #define DL_HASH_ENTRIES (1 << deadline_hash_shift)
34 #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
35 #define list_entry_hash(ptr) list_entry((ptr), struct deadline_rq, hash)
36 #define ON_HASH(drq) (drq)->on_hash
38 struct deadline_data {
44 * requests (deadline_rq s) are present on both sort_list and fifo_list
46 struct rb_root sort_list[2];
47 struct list_head fifo_list[2];
50 * next in sort order. read, write or both are NULL
52 struct deadline_rq *next_drq[2];
53 struct list_head *hash; /* request hash */
54 unsigned int batching; /* number of sequential requests made */
55 sector_t last_sector; /* head position */
56 unsigned int starved; /* times reads have starved writes */
59 * settings that change how the i/o scheduler behaves
74 * rbtree index, key is the starting offset
76 struct rb_node rb_node;
79 struct request *request;
82 * request hash, key is the ending offset (for back merge lookup)
84 struct list_head hash;
90 struct list_head fifo;
91 unsigned long expires;
94 static void deadline_move_request(struct deadline_data *dd, struct deadline_rq *drq);
96 static kmem_cache_t *drq_pool;
98 #define RQ_DATA(rq) ((struct deadline_rq *) (rq)->elevator_private)
101 * the back merge hash support functions
103 static inline void __deadline_del_drq_hash(struct deadline_rq *drq)
106 list_del_init(&drq->hash);
109 static inline void deadline_del_drq_hash(struct deadline_rq *drq)
112 __deadline_del_drq_hash(drq);
116 deadline_remove_merge_hints(request_queue_t *q, struct deadline_rq *drq)
118 deadline_del_drq_hash(drq);
120 if (q->last_merge == drq->request)
121 q->last_merge = NULL;
125 deadline_add_drq_hash(struct deadline_data *dd, struct deadline_rq *drq)
127 struct request *rq = drq->request;
129 BUG_ON(ON_HASH(drq));
132 list_add(&drq->hash, &dd->hash[DL_HASH_FN(rq_hash_key(rq))]);
136 * move hot entry to front of chain
139 deadline_hot_drq_hash(struct deadline_data *dd, struct deadline_rq *drq)
141 struct request *rq = drq->request;
142 struct list_head *head = &dd->hash[DL_HASH_FN(rq_hash_key(rq))];
144 if (ON_HASH(drq) && drq->hash.prev != head) {
145 list_del(&drq->hash);
146 list_add(&drq->hash, head);
150 static struct request *
151 deadline_find_drq_hash(struct deadline_data *dd, sector_t offset)
153 struct list_head *hash_list = &dd->hash[DL_HASH_FN(offset)];
154 struct list_head *entry, *next = hash_list->next;
156 while ((entry = next) != hash_list) {
157 struct deadline_rq *drq = list_entry_hash(entry);
158 struct request *__rq = drq->request;
162 BUG_ON(!ON_HASH(drq));
164 if (!rq_mergeable(__rq)) {
165 __deadline_del_drq_hash(drq);
169 if (rq_hash_key(__rq) == offset)
177 * rb tree support functions
180 #define RB_EMPTY(root) ((root)->rb_node == NULL)
181 #define ON_RB(node) ((node)->rb_color != RB_NONE)
182 #define RB_CLEAR(node) ((node)->rb_color = RB_NONE)
183 #define rb_entry_drq(node) rb_entry((node), struct deadline_rq, rb_node)
184 #define DRQ_RB_ROOT(dd, drq) (&(dd)->sort_list[rq_data_dir((drq)->request)])
185 #define rq_rb_key(rq) (rq)->sector
187 static struct deadline_rq *
188 __deadline_add_drq_rb(struct deadline_data *dd, struct deadline_rq *drq)
190 struct rb_node **p = &DRQ_RB_ROOT(dd, drq)->rb_node;
191 struct rb_node *parent = NULL;
192 struct deadline_rq *__drq;
196 __drq = rb_entry_drq(parent);
198 if (drq->rb_key < __drq->rb_key)
200 else if (drq->rb_key > __drq->rb_key)
206 rb_link_node(&drq->rb_node, parent, p);
211 deadline_add_drq_rb(struct deadline_data *dd, struct deadline_rq *drq)
213 struct deadline_rq *__alias;
215 drq->rb_key = rq_rb_key(drq->request);
218 __alias = __deadline_add_drq_rb(dd, drq);
220 rb_insert_color(&drq->rb_node, DRQ_RB_ROOT(dd, drq));
224 deadline_move_request(dd, __alias);
229 deadline_del_drq_rb(struct deadline_data *dd, struct deadline_rq *drq)
231 const int data_dir = rq_data_dir(drq->request);
233 if (dd->next_drq[data_dir] == drq) {
234 struct rb_node *rbnext = rb_next(&drq->rb_node);
236 dd->next_drq[data_dir] = NULL;
238 dd->next_drq[data_dir] = rb_entry_drq(rbnext);
241 BUG_ON(!ON_RB(&drq->rb_node));
242 rb_erase(&drq->rb_node, DRQ_RB_ROOT(dd, drq));
243 RB_CLEAR(&drq->rb_node);
246 static struct request *
247 deadline_find_drq_rb(struct deadline_data *dd, sector_t sector, int data_dir)
249 struct rb_node *n = dd->sort_list[data_dir].rb_node;
250 struct deadline_rq *drq;
253 drq = rb_entry_drq(n);
255 if (sector < drq->rb_key)
257 else if (sector > drq->rb_key)
267 * deadline_find_first_drq finds the first (lowest sector numbered) request
268 * for the specified data_dir. Used to sweep back to the start of the disk
269 * (1-way elevator) after we process the last (highest sector) request.
271 static struct deadline_rq *
272 deadline_find_first_drq(struct deadline_data *dd, int data_dir)
274 struct rb_node *n = dd->sort_list[data_dir].rb_node;
277 if (n->rb_left == NULL)
278 return rb_entry_drq(n);
285 * add drq to rbtree and fifo
288 deadline_add_request(struct request_queue *q, struct request *rq)
290 struct deadline_data *dd = q->elevator->elevator_data;
291 struct deadline_rq *drq = RQ_DATA(rq);
293 const int data_dir = rq_data_dir(drq->request);
295 deadline_add_drq_rb(dd, drq);
297 * set expire time (only used for reads) and add to fifo list
299 drq->expires = jiffies + dd->fifo_expire[data_dir];
300 list_add_tail(&drq->fifo, &dd->fifo_list[data_dir]);
302 if (rq_mergeable(rq)) {
303 deadline_add_drq_hash(dd, drq);
311 * remove rq from rbtree, fifo, and hash
313 static void deadline_remove_request(request_queue_t *q, struct request *rq)
315 struct deadline_rq *drq = RQ_DATA(rq);
316 struct deadline_data *dd = q->elevator->elevator_data;
318 list_del_init(&drq->fifo);
319 deadline_remove_merge_hints(q, drq);
320 deadline_del_drq_rb(dd, drq);
324 deadline_merge(request_queue_t *q, struct request **req, struct bio *bio)
326 struct deadline_data *dd = q->elevator->elevator_data;
327 struct request *__rq;
331 * try last_merge to avoid going to hash
333 ret = elv_try_last_merge(q, bio);
334 if (ret != ELEVATOR_NO_MERGE) {
335 __rq = q->last_merge;
340 * see if the merge hash can satisfy a back merge
342 __rq = deadline_find_drq_hash(dd, bio->bi_sector);
344 BUG_ON(__rq->sector + __rq->nr_sectors != bio->bi_sector);
346 if (elv_rq_merge_ok(__rq, bio)) {
347 ret = ELEVATOR_BACK_MERGE;
353 * check for front merge
355 if (dd->front_merges) {
356 sector_t rb_key = bio->bi_sector + bio_sectors(bio);
358 __rq = deadline_find_drq_rb(dd, rb_key, bio_data_dir(bio));
360 BUG_ON(rb_key != rq_rb_key(__rq));
362 if (elv_rq_merge_ok(__rq, bio)) {
363 ret = ELEVATOR_FRONT_MERGE;
369 return ELEVATOR_NO_MERGE;
371 q->last_merge = __rq;
374 deadline_hot_drq_hash(dd, RQ_DATA(__rq));
379 static void deadline_merged_request(request_queue_t *q, struct request *req)
381 struct deadline_data *dd = q->elevator->elevator_data;
382 struct deadline_rq *drq = RQ_DATA(req);
385 * hash always needs to be repositioned, key is end sector
387 deadline_del_drq_hash(drq);
388 deadline_add_drq_hash(dd, drq);
391 * if the merge was a front merge, we need to reposition request
393 if (rq_rb_key(req) != drq->rb_key) {
394 deadline_del_drq_rb(dd, drq);
395 deadline_add_drq_rb(dd, drq);
402 deadline_merged_requests(request_queue_t *q, struct request *req,
403 struct request *next)
405 struct deadline_data *dd = q->elevator->elevator_data;
406 struct deadline_rq *drq = RQ_DATA(req);
407 struct deadline_rq *dnext = RQ_DATA(next);
413 * reposition drq (this is the merged request) in hash, and in rbtree
414 * in case of a front merge
416 deadline_del_drq_hash(drq);
417 deadline_add_drq_hash(dd, drq);
419 if (rq_rb_key(req) != drq->rb_key) {
420 deadline_del_drq_rb(dd, drq);
421 deadline_add_drq_rb(dd, drq);
425 * if dnext expires before drq, assign its expire time to drq
426 * and move into dnext position (dnext will be deleted) in fifo
428 if (!list_empty(&drq->fifo) && !list_empty(&dnext->fifo)) {
429 if (time_before(dnext->expires, drq->expires)) {
430 list_move(&drq->fifo, &dnext->fifo);
431 drq->expires = dnext->expires;
436 * kill knowledge of next, this one is a goner
438 deadline_remove_request(q, next);
442 * move request from sort list to dispatch queue.
445 deadline_move_to_dispatch(struct deadline_data *dd, struct deadline_rq *drq)
447 request_queue_t *q = drq->request->q;
449 deadline_remove_request(q, drq->request);
450 elv_dispatch_add_tail(q, drq->request);
454 * move an entry to dispatch queue
457 deadline_move_request(struct deadline_data *dd, struct deadline_rq *drq)
459 const int data_dir = rq_data_dir(drq->request);
460 struct rb_node *rbnext = rb_next(&drq->rb_node);
462 dd->next_drq[READ] = NULL;
463 dd->next_drq[WRITE] = NULL;
466 dd->next_drq[data_dir] = rb_entry_drq(rbnext);
468 dd->last_sector = drq->request->sector + drq->request->nr_sectors;
471 * take it off the sort and fifo list, move
474 deadline_move_to_dispatch(dd, drq);
477 #define list_entry_fifo(ptr) list_entry((ptr), struct deadline_rq, fifo)
480 * deadline_check_fifo returns 0 if there are no expired reads on the fifo,
481 * 1 otherwise. Requires !list_empty(&dd->fifo_list[data_dir])
483 static inline int deadline_check_fifo(struct deadline_data *dd, int ddir)
485 struct deadline_rq *drq = list_entry_fifo(dd->fifo_list[ddir].next);
490 if (time_after(jiffies, drq->expires))
497 * deadline_dispatch_requests selects the best request according to
498 * read/write expire, fifo_batch, etc
500 static int deadline_dispatch_requests(request_queue_t *q, int force)
502 struct deadline_data *dd = q->elevator->elevator_data;
503 const int reads = !list_empty(&dd->fifo_list[READ]);
504 const int writes = !list_empty(&dd->fifo_list[WRITE]);
505 struct deadline_rq *drq;
509 * batches are currently reads XOR writes
511 if (dd->next_drq[WRITE])
512 drq = dd->next_drq[WRITE];
514 drq = dd->next_drq[READ];
517 /* we have a "next request" */
519 if (dd->last_sector != drq->request->sector)
520 /* end the batch on a non sequential request */
521 dd->batching += dd->fifo_batch;
523 if (dd->batching < dd->fifo_batch)
524 /* we are still entitled to batch */
525 goto dispatch_request;
529 * at this point we are not running a batch. select the appropriate
530 * data direction (read / write)
534 BUG_ON(RB_EMPTY(&dd->sort_list[READ]));
536 if (writes && (dd->starved++ >= dd->writes_starved))
537 goto dispatch_writes;
541 goto dispatch_find_request;
545 * there are either no reads or writes have been starved
550 BUG_ON(RB_EMPTY(&dd->sort_list[WRITE]));
556 goto dispatch_find_request;
561 dispatch_find_request:
563 * we are not running a batch, find best request for selected data_dir
565 if (deadline_check_fifo(dd, data_dir)) {
566 /* An expired request exists - satisfy it */
568 drq = list_entry_fifo(dd->fifo_list[data_dir].next);
570 } else if (dd->next_drq[data_dir]) {
572 * The last req was the same dir and we have a next request in
573 * sort order. No expired requests so continue on from here.
575 drq = dd->next_drq[data_dir];
578 * The last req was the other direction or we have run out of
579 * higher-sectored requests. Go back to the lowest sectored
580 * request (1 way elevator) and start a new batch.
583 drq = deadline_find_first_drq(dd, data_dir);
588 * drq is the selected appropriate request.
591 deadline_move_request(dd, drq);
596 static int deadline_queue_empty(request_queue_t *q)
598 struct deadline_data *dd = q->elevator->elevator_data;
600 return list_empty(&dd->fifo_list[WRITE])
601 && list_empty(&dd->fifo_list[READ]);
604 static struct request *
605 deadline_former_request(request_queue_t *q, struct request *rq)
607 struct deadline_rq *drq = RQ_DATA(rq);
608 struct rb_node *rbprev = rb_prev(&drq->rb_node);
611 return rb_entry_drq(rbprev)->request;
616 static struct request *
617 deadline_latter_request(request_queue_t *q, struct request *rq)
619 struct deadline_rq *drq = RQ_DATA(rq);
620 struct rb_node *rbnext = rb_next(&drq->rb_node);
623 return rb_entry_drq(rbnext)->request;
628 static void deadline_exit_queue(elevator_t *e)
630 struct deadline_data *dd = e->elevator_data;
632 BUG_ON(!list_empty(&dd->fifo_list[READ]));
633 BUG_ON(!list_empty(&dd->fifo_list[WRITE]));
635 mempool_destroy(dd->drq_pool);
641 * initialize elevator private data (deadline_data), and alloc a drq for
642 * each request on the free lists
644 static int deadline_init_queue(request_queue_t *q, elevator_t *e)
646 struct deadline_data *dd;
652 dd = kmalloc_node(sizeof(*dd), GFP_KERNEL, q->node);
655 memset(dd, 0, sizeof(*dd));
657 dd->hash = kmalloc_node(sizeof(struct list_head)*DL_HASH_ENTRIES,
658 GFP_KERNEL, q->node);
664 dd->drq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
665 mempool_free_slab, drq_pool, q->node);
672 for (i = 0; i < DL_HASH_ENTRIES; i++)
673 INIT_LIST_HEAD(&dd->hash[i]);
675 INIT_LIST_HEAD(&dd->fifo_list[READ]);
676 INIT_LIST_HEAD(&dd->fifo_list[WRITE]);
677 dd->sort_list[READ] = RB_ROOT;
678 dd->sort_list[WRITE] = RB_ROOT;
679 dd->fifo_expire[READ] = read_expire;
680 dd->fifo_expire[WRITE] = write_expire;
681 dd->writes_starved = writes_starved;
682 dd->front_merges = 1;
683 dd->fifo_batch = fifo_batch;
684 e->elevator_data = dd;
688 static void deadline_put_request(request_queue_t *q, struct request *rq)
690 struct deadline_data *dd = q->elevator->elevator_data;
691 struct deadline_rq *drq = RQ_DATA(rq);
693 mempool_free(drq, dd->drq_pool);
694 rq->elevator_private = NULL;
698 deadline_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
701 struct deadline_data *dd = q->elevator->elevator_data;
702 struct deadline_rq *drq;
704 drq = mempool_alloc(dd->drq_pool, gfp_mask);
706 memset(drq, 0, sizeof(*drq));
707 RB_CLEAR(&drq->rb_node);
710 INIT_LIST_HEAD(&drq->hash);
713 INIT_LIST_HEAD(&drq->fifo);
715 rq->elevator_private = drq;
725 struct deadline_fs_entry {
726 struct attribute attr;
727 ssize_t (*show)(struct deadline_data *, char *);
728 ssize_t (*store)(struct deadline_data *, const char *, size_t);
732 deadline_var_show(int var, char *page)
734 return sprintf(page, "%d\n", var);
738 deadline_var_store(int *var, const char *page, size_t count)
740 char *p = (char *) page;
742 *var = simple_strtol(p, &p, 10);
746 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
747 static ssize_t __FUNC(struct deadline_data *dd, char *page) \
749 int __data = __VAR; \
751 __data = jiffies_to_msecs(__data); \
752 return deadline_var_show(__data, (page)); \
754 SHOW_FUNCTION(deadline_readexpire_show, dd->fifo_expire[READ], 1);
755 SHOW_FUNCTION(deadline_writeexpire_show, dd->fifo_expire[WRITE], 1);
756 SHOW_FUNCTION(deadline_writesstarved_show, dd->writes_starved, 0);
757 SHOW_FUNCTION(deadline_frontmerges_show, dd->front_merges, 0);
758 SHOW_FUNCTION(deadline_fifobatch_show, dd->fifo_batch, 0);
761 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
762 static ssize_t __FUNC(struct deadline_data *dd, const char *page, size_t count) \
765 int ret = deadline_var_store(&__data, (page), count); \
766 if (__data < (MIN)) \
768 else if (__data > (MAX)) \
771 *(__PTR) = msecs_to_jiffies(__data); \
776 STORE_FUNCTION(deadline_readexpire_store, &dd->fifo_expire[READ], 0, INT_MAX, 1);
777 STORE_FUNCTION(deadline_writeexpire_store, &dd->fifo_expire[WRITE], 0, INT_MAX, 1);
778 STORE_FUNCTION(deadline_writesstarved_store, &dd->writes_starved, INT_MIN, INT_MAX, 0);
779 STORE_FUNCTION(deadline_frontmerges_store, &dd->front_merges, 0, 1, 0);
780 STORE_FUNCTION(deadline_fifobatch_store, &dd->fifo_batch, 0, INT_MAX, 0);
781 #undef STORE_FUNCTION
783 static struct deadline_fs_entry deadline_readexpire_entry = {
784 .attr = {.name = "read_expire", .mode = S_IRUGO | S_IWUSR },
785 .show = deadline_readexpire_show,
786 .store = deadline_readexpire_store,
788 static struct deadline_fs_entry deadline_writeexpire_entry = {
789 .attr = {.name = "write_expire", .mode = S_IRUGO | S_IWUSR },
790 .show = deadline_writeexpire_show,
791 .store = deadline_writeexpire_store,
793 static struct deadline_fs_entry deadline_writesstarved_entry = {
794 .attr = {.name = "writes_starved", .mode = S_IRUGO | S_IWUSR },
795 .show = deadline_writesstarved_show,
796 .store = deadline_writesstarved_store,
798 static struct deadline_fs_entry deadline_frontmerges_entry = {
799 .attr = {.name = "front_merges", .mode = S_IRUGO | S_IWUSR },
800 .show = deadline_frontmerges_show,
801 .store = deadline_frontmerges_store,
803 static struct deadline_fs_entry deadline_fifobatch_entry = {
804 .attr = {.name = "fifo_batch", .mode = S_IRUGO | S_IWUSR },
805 .show = deadline_fifobatch_show,
806 .store = deadline_fifobatch_store,
809 static struct attribute *default_attrs[] = {
810 &deadline_readexpire_entry.attr,
811 &deadline_writeexpire_entry.attr,
812 &deadline_writesstarved_entry.attr,
813 &deadline_frontmerges_entry.attr,
814 &deadline_fifobatch_entry.attr,
818 #define to_deadline(atr) container_of((atr), struct deadline_fs_entry, attr)
821 deadline_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
823 elevator_t *e = container_of(kobj, elevator_t, kobj);
824 struct deadline_fs_entry *entry = to_deadline(attr);
829 return entry->show(e->elevator_data, page);
833 deadline_attr_store(struct kobject *kobj, struct attribute *attr,
834 const char *page, size_t length)
836 elevator_t *e = container_of(kobj, elevator_t, kobj);
837 struct deadline_fs_entry *entry = to_deadline(attr);
842 return entry->store(e->elevator_data, page, length);
845 static struct sysfs_ops deadline_sysfs_ops = {
846 .show = deadline_attr_show,
847 .store = deadline_attr_store,
850 static struct kobj_type deadline_ktype = {
851 .sysfs_ops = &deadline_sysfs_ops,
852 .default_attrs = default_attrs,
855 static struct elevator_type iosched_deadline = {
857 .elevator_merge_fn = deadline_merge,
858 .elevator_merged_fn = deadline_merged_request,
859 .elevator_merge_req_fn = deadline_merged_requests,
860 .elevator_dispatch_fn = deadline_dispatch_requests,
861 .elevator_add_req_fn = deadline_add_request,
862 .elevator_queue_empty_fn = deadline_queue_empty,
863 .elevator_former_req_fn = deadline_former_request,
864 .elevator_latter_req_fn = deadline_latter_request,
865 .elevator_set_req_fn = deadline_set_request,
866 .elevator_put_req_fn = deadline_put_request,
867 .elevator_init_fn = deadline_init_queue,
868 .elevator_exit_fn = deadline_exit_queue,
871 .elevator_ktype = &deadline_ktype,
872 .elevator_name = "deadline",
873 .elevator_owner = THIS_MODULE,
876 static int __init deadline_init(void)
880 drq_pool = kmem_cache_create("deadline_drq", sizeof(struct deadline_rq),
886 ret = elv_register(&iosched_deadline);
888 kmem_cache_destroy(drq_pool);
893 static void __exit deadline_exit(void)
895 kmem_cache_destroy(drq_pool);
896 elv_unregister(&iosched_deadline);
899 module_init(deadline_init);
900 module_exit(deadline_exit);
902 MODULE_AUTHOR("Jens Axboe");
903 MODULE_LICENSE("GPL");
904 MODULE_DESCRIPTION("deadline IO scheduler");