2 * Deadline i/o scheduler.
4 * Copyright (C) 2002 Jens Axboe <axboe@suse.de>
6 #include <linux/kernel.h>
8 #include <linux/blkdev.h>
9 #include <linux/elevator.h>
10 #include <linux/bio.h>
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/init.h>
15 #include <linux/compiler.h>
16 #include <linux/hash.h>
17 #include <linux/rbtree.h>
20 * See Documentation/block/deadline-iosched.txt
22 static const int read_expire = HZ / 2; /* max time before a read is submitted. */
23 static const int write_expire = 5 * HZ; /* ditto for writes, these limits are SOFT! */
24 static const int writes_starved = 2; /* max times reads can starve a write */
25 static const int fifo_batch = 16; /* # of sequential requests treated as one
26 by the above parameters. For throughput. */
28 static const int deadline_hash_shift = 5;
29 #define DL_HASH_BLOCK(sec) ((sec) >> 3)
30 #define DL_HASH_FN(sec) (hash_long(DL_HASH_BLOCK((sec)), deadline_hash_shift))
31 #define DL_HASH_ENTRIES (1 << deadline_hash_shift)
32 #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
33 #define list_entry_hash(ptr) list_entry((ptr), struct deadline_rq, hash)
34 #define ON_HASH(drq) (drq)->on_hash
36 struct deadline_data {
42 * requests (deadline_rq s) are present on both sort_list and fifo_list
44 struct rb_root sort_list[2];
45 struct list_head fifo_list[2];
48 * next in sort order. read, write or both are NULL
50 struct deadline_rq *next_drq[2];
51 struct list_head *hash; /* request hash */
52 unsigned int batching; /* number of sequential requests made */
53 sector_t last_sector; /* head position */
54 unsigned int starved; /* times reads have starved writes */
57 * settings that change how the i/o scheduler behaves
72 * rbtree index, key is the starting offset
74 struct rb_node rb_node;
77 struct request *request;
80 * request hash, key is the ending offset (for back merge lookup)
82 struct list_head hash;
88 struct list_head fifo;
89 unsigned long expires;
92 static void deadline_move_request(struct deadline_data *dd, struct deadline_rq *drq);
94 static kmem_cache_t *drq_pool;
96 #define RQ_DATA(rq) ((struct deadline_rq *) (rq)->elevator_private)
99 * the back merge hash support functions
101 static inline void __deadline_del_drq_hash(struct deadline_rq *drq)
104 list_del_init(&drq->hash);
107 static inline void deadline_del_drq_hash(struct deadline_rq *drq)
110 __deadline_del_drq_hash(drq);
114 deadline_add_drq_hash(struct deadline_data *dd, struct deadline_rq *drq)
116 struct request *rq = drq->request;
118 BUG_ON(ON_HASH(drq));
121 list_add(&drq->hash, &dd->hash[DL_HASH_FN(rq_hash_key(rq))]);
125 * move hot entry to front of chain
128 deadline_hot_drq_hash(struct deadline_data *dd, struct deadline_rq *drq)
130 struct request *rq = drq->request;
131 struct list_head *head = &dd->hash[DL_HASH_FN(rq_hash_key(rq))];
133 if (ON_HASH(drq) && drq->hash.prev != head) {
134 list_del(&drq->hash);
135 list_add(&drq->hash, head);
139 static struct request *
140 deadline_find_drq_hash(struct deadline_data *dd, sector_t offset)
142 struct list_head *hash_list = &dd->hash[DL_HASH_FN(offset)];
143 struct list_head *entry, *next = hash_list->next;
145 while ((entry = next) != hash_list) {
146 struct deadline_rq *drq = list_entry_hash(entry);
147 struct request *__rq = drq->request;
151 BUG_ON(!ON_HASH(drq));
153 if (!rq_mergeable(__rq)) {
154 __deadline_del_drq_hash(drq);
158 if (rq_hash_key(__rq) == offset)
166 * rb tree support functions
169 #define RB_EMPTY(root) ((root)->rb_node == NULL)
170 #define ON_RB(node) ((node)->rb_color != RB_NONE)
171 #define RB_CLEAR(node) ((node)->rb_color = RB_NONE)
172 #define rb_entry_drq(node) rb_entry((node), struct deadline_rq, rb_node)
173 #define DRQ_RB_ROOT(dd, drq) (&(dd)->sort_list[rq_data_dir((drq)->request)])
174 #define rq_rb_key(rq) (rq)->sector
176 static struct deadline_rq *
177 __deadline_add_drq_rb(struct deadline_data *dd, struct deadline_rq *drq)
179 struct rb_node **p = &DRQ_RB_ROOT(dd, drq)->rb_node;
180 struct rb_node *parent = NULL;
181 struct deadline_rq *__drq;
185 __drq = rb_entry_drq(parent);
187 if (drq->rb_key < __drq->rb_key)
189 else if (drq->rb_key > __drq->rb_key)
195 rb_link_node(&drq->rb_node, parent, p);
200 deadline_add_drq_rb(struct deadline_data *dd, struct deadline_rq *drq)
202 struct deadline_rq *__alias;
204 drq->rb_key = rq_rb_key(drq->request);
207 __alias = __deadline_add_drq_rb(dd, drq);
209 rb_insert_color(&drq->rb_node, DRQ_RB_ROOT(dd, drq));
213 deadline_move_request(dd, __alias);
218 deadline_del_drq_rb(struct deadline_data *dd, struct deadline_rq *drq)
220 const int data_dir = rq_data_dir(drq->request);
222 if (dd->next_drq[data_dir] == drq) {
223 struct rb_node *rbnext = rb_next(&drq->rb_node);
225 dd->next_drq[data_dir] = NULL;
227 dd->next_drq[data_dir] = rb_entry_drq(rbnext);
230 BUG_ON(!ON_RB(&drq->rb_node));
231 rb_erase(&drq->rb_node, DRQ_RB_ROOT(dd, drq));
232 RB_CLEAR(&drq->rb_node);
235 static struct request *
236 deadline_find_drq_rb(struct deadline_data *dd, sector_t sector, int data_dir)
238 struct rb_node *n = dd->sort_list[data_dir].rb_node;
239 struct deadline_rq *drq;
242 drq = rb_entry_drq(n);
244 if (sector < drq->rb_key)
246 else if (sector > drq->rb_key)
256 * deadline_find_first_drq finds the first (lowest sector numbered) request
257 * for the specified data_dir. Used to sweep back to the start of the disk
258 * (1-way elevator) after we process the last (highest sector) request.
260 static struct deadline_rq *
261 deadline_find_first_drq(struct deadline_data *dd, int data_dir)
263 struct rb_node *n = dd->sort_list[data_dir].rb_node;
266 if (n->rb_left == NULL)
267 return rb_entry_drq(n);
274 * add drq to rbtree and fifo
277 deadline_add_request(struct request_queue *q, struct request *rq)
279 struct deadline_data *dd = q->elevator->elevator_data;
280 struct deadline_rq *drq = RQ_DATA(rq);
282 const int data_dir = rq_data_dir(drq->request);
284 deadline_add_drq_rb(dd, drq);
286 * set expire time (only used for reads) and add to fifo list
288 drq->expires = jiffies + dd->fifo_expire[data_dir];
289 list_add_tail(&drq->fifo, &dd->fifo_list[data_dir]);
291 if (rq_mergeable(rq))
292 deadline_add_drq_hash(dd, drq);
296 * remove rq from rbtree, fifo, and hash
298 static void deadline_remove_request(request_queue_t *q, struct request *rq)
300 struct deadline_rq *drq = RQ_DATA(rq);
301 struct deadline_data *dd = q->elevator->elevator_data;
303 list_del_init(&drq->fifo);
304 deadline_del_drq_rb(dd, drq);
305 deadline_del_drq_hash(drq);
309 deadline_merge(request_queue_t *q, struct request **req, struct bio *bio)
311 struct deadline_data *dd = q->elevator->elevator_data;
312 struct request *__rq;
316 * see if the merge hash can satisfy a back merge
318 __rq = deadline_find_drq_hash(dd, bio->bi_sector);
320 BUG_ON(__rq->sector + __rq->nr_sectors != bio->bi_sector);
322 if (elv_rq_merge_ok(__rq, bio)) {
323 ret = ELEVATOR_BACK_MERGE;
329 * check for front merge
331 if (dd->front_merges) {
332 sector_t rb_key = bio->bi_sector + bio_sectors(bio);
334 __rq = deadline_find_drq_rb(dd, rb_key, bio_data_dir(bio));
336 BUG_ON(rb_key != rq_rb_key(__rq));
338 if (elv_rq_merge_ok(__rq, bio)) {
339 ret = ELEVATOR_FRONT_MERGE;
345 return ELEVATOR_NO_MERGE;
348 deadline_hot_drq_hash(dd, RQ_DATA(__rq));
353 static void deadline_merged_request(request_queue_t *q, struct request *req)
355 struct deadline_data *dd = q->elevator->elevator_data;
356 struct deadline_rq *drq = RQ_DATA(req);
359 * hash always needs to be repositioned, key is end sector
361 deadline_del_drq_hash(drq);
362 deadline_add_drq_hash(dd, drq);
365 * if the merge was a front merge, we need to reposition request
367 if (rq_rb_key(req) != drq->rb_key) {
368 deadline_del_drq_rb(dd, drq);
369 deadline_add_drq_rb(dd, drq);
374 deadline_merged_requests(request_queue_t *q, struct request *req,
375 struct request *next)
377 struct deadline_data *dd = q->elevator->elevator_data;
378 struct deadline_rq *drq = RQ_DATA(req);
379 struct deadline_rq *dnext = RQ_DATA(next);
385 * reposition drq (this is the merged request) in hash, and in rbtree
386 * in case of a front merge
388 deadline_del_drq_hash(drq);
389 deadline_add_drq_hash(dd, drq);
391 if (rq_rb_key(req) != drq->rb_key) {
392 deadline_del_drq_rb(dd, drq);
393 deadline_add_drq_rb(dd, drq);
397 * if dnext expires before drq, assign its expire time to drq
398 * and move into dnext position (dnext will be deleted) in fifo
400 if (!list_empty(&drq->fifo) && !list_empty(&dnext->fifo)) {
401 if (time_before(dnext->expires, drq->expires)) {
402 list_move(&drq->fifo, &dnext->fifo);
403 drq->expires = dnext->expires;
408 * kill knowledge of next, this one is a goner
410 deadline_remove_request(q, next);
414 * move request from sort list to dispatch queue.
417 deadline_move_to_dispatch(struct deadline_data *dd, struct deadline_rq *drq)
419 request_queue_t *q = drq->request->q;
421 deadline_remove_request(q, drq->request);
422 elv_dispatch_add_tail(q, drq->request);
426 * move an entry to dispatch queue
429 deadline_move_request(struct deadline_data *dd, struct deadline_rq *drq)
431 const int data_dir = rq_data_dir(drq->request);
432 struct rb_node *rbnext = rb_next(&drq->rb_node);
434 dd->next_drq[READ] = NULL;
435 dd->next_drq[WRITE] = NULL;
438 dd->next_drq[data_dir] = rb_entry_drq(rbnext);
440 dd->last_sector = drq->request->sector + drq->request->nr_sectors;
443 * take it off the sort and fifo list, move
446 deadline_move_to_dispatch(dd, drq);
449 #define list_entry_fifo(ptr) list_entry((ptr), struct deadline_rq, fifo)
452 * deadline_check_fifo returns 0 if there are no expired reads on the fifo,
453 * 1 otherwise. Requires !list_empty(&dd->fifo_list[data_dir])
455 static inline int deadline_check_fifo(struct deadline_data *dd, int ddir)
457 struct deadline_rq *drq = list_entry_fifo(dd->fifo_list[ddir].next);
462 if (time_after(jiffies, drq->expires))
469 * deadline_dispatch_requests selects the best request according to
470 * read/write expire, fifo_batch, etc
472 static int deadline_dispatch_requests(request_queue_t *q, int force)
474 struct deadline_data *dd = q->elevator->elevator_data;
475 const int reads = !list_empty(&dd->fifo_list[READ]);
476 const int writes = !list_empty(&dd->fifo_list[WRITE]);
477 struct deadline_rq *drq;
481 * batches are currently reads XOR writes
483 if (dd->next_drq[WRITE])
484 drq = dd->next_drq[WRITE];
486 drq = dd->next_drq[READ];
489 /* we have a "next request" */
491 if (dd->last_sector != drq->request->sector)
492 /* end the batch on a non sequential request */
493 dd->batching += dd->fifo_batch;
495 if (dd->batching < dd->fifo_batch)
496 /* we are still entitled to batch */
497 goto dispatch_request;
501 * at this point we are not running a batch. select the appropriate
502 * data direction (read / write)
506 BUG_ON(RB_EMPTY(&dd->sort_list[READ]));
508 if (writes && (dd->starved++ >= dd->writes_starved))
509 goto dispatch_writes;
513 goto dispatch_find_request;
517 * there are either no reads or writes have been starved
522 BUG_ON(RB_EMPTY(&dd->sort_list[WRITE]));
528 goto dispatch_find_request;
533 dispatch_find_request:
535 * we are not running a batch, find best request for selected data_dir
537 if (deadline_check_fifo(dd, data_dir)) {
538 /* An expired request exists - satisfy it */
540 drq = list_entry_fifo(dd->fifo_list[data_dir].next);
542 } else if (dd->next_drq[data_dir]) {
544 * The last req was the same dir and we have a next request in
545 * sort order. No expired requests so continue on from here.
547 drq = dd->next_drq[data_dir];
550 * The last req was the other direction or we have run out of
551 * higher-sectored requests. Go back to the lowest sectored
552 * request (1 way elevator) and start a new batch.
555 drq = deadline_find_first_drq(dd, data_dir);
560 * drq is the selected appropriate request.
563 deadline_move_request(dd, drq);
568 static int deadline_queue_empty(request_queue_t *q)
570 struct deadline_data *dd = q->elevator->elevator_data;
572 return list_empty(&dd->fifo_list[WRITE])
573 && list_empty(&dd->fifo_list[READ]);
576 static struct request *
577 deadline_former_request(request_queue_t *q, struct request *rq)
579 struct deadline_rq *drq = RQ_DATA(rq);
580 struct rb_node *rbprev = rb_prev(&drq->rb_node);
583 return rb_entry_drq(rbprev)->request;
588 static struct request *
589 deadline_latter_request(request_queue_t *q, struct request *rq)
591 struct deadline_rq *drq = RQ_DATA(rq);
592 struct rb_node *rbnext = rb_next(&drq->rb_node);
595 return rb_entry_drq(rbnext)->request;
600 static void deadline_exit_queue(elevator_t *e)
602 struct deadline_data *dd = e->elevator_data;
604 BUG_ON(!list_empty(&dd->fifo_list[READ]));
605 BUG_ON(!list_empty(&dd->fifo_list[WRITE]));
607 mempool_destroy(dd->drq_pool);
613 * initialize elevator private data (deadline_data), and alloc a drq for
614 * each request on the free lists
616 static int deadline_init_queue(request_queue_t *q, elevator_t *e)
618 struct deadline_data *dd;
624 dd = kmalloc_node(sizeof(*dd), GFP_KERNEL, q->node);
627 memset(dd, 0, sizeof(*dd));
629 dd->hash = kmalloc_node(sizeof(struct list_head)*DL_HASH_ENTRIES,
630 GFP_KERNEL, q->node);
636 dd->drq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
637 mempool_free_slab, drq_pool, q->node);
644 for (i = 0; i < DL_HASH_ENTRIES; i++)
645 INIT_LIST_HEAD(&dd->hash[i]);
647 INIT_LIST_HEAD(&dd->fifo_list[READ]);
648 INIT_LIST_HEAD(&dd->fifo_list[WRITE]);
649 dd->sort_list[READ] = RB_ROOT;
650 dd->sort_list[WRITE] = RB_ROOT;
651 dd->fifo_expire[READ] = read_expire;
652 dd->fifo_expire[WRITE] = write_expire;
653 dd->writes_starved = writes_starved;
654 dd->front_merges = 1;
655 dd->fifo_batch = fifo_batch;
656 e->elevator_data = dd;
660 static void deadline_put_request(request_queue_t *q, struct request *rq)
662 struct deadline_data *dd = q->elevator->elevator_data;
663 struct deadline_rq *drq = RQ_DATA(rq);
665 mempool_free(drq, dd->drq_pool);
666 rq->elevator_private = NULL;
670 deadline_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
673 struct deadline_data *dd = q->elevator->elevator_data;
674 struct deadline_rq *drq;
676 drq = mempool_alloc(dd->drq_pool, gfp_mask);
678 memset(drq, 0, sizeof(*drq));
679 RB_CLEAR(&drq->rb_node);
682 INIT_LIST_HEAD(&drq->hash);
685 INIT_LIST_HEAD(&drq->fifo);
687 rq->elevator_private = drq;
697 struct deadline_fs_entry {
698 struct attribute attr;
699 ssize_t (*show)(struct deadline_data *, char *);
700 ssize_t (*store)(struct deadline_data *, const char *, size_t);
704 deadline_var_show(int var, char *page)
706 return sprintf(page, "%d\n", var);
710 deadline_var_store(int *var, const char *page, size_t count)
712 char *p = (char *) page;
714 *var = simple_strtol(p, &p, 10);
718 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
719 static ssize_t __FUNC(struct deadline_data *dd, char *page) \
721 int __data = __VAR; \
723 __data = jiffies_to_msecs(__data); \
724 return deadline_var_show(__data, (page)); \
726 SHOW_FUNCTION(deadline_readexpire_show, dd->fifo_expire[READ], 1);
727 SHOW_FUNCTION(deadline_writeexpire_show, dd->fifo_expire[WRITE], 1);
728 SHOW_FUNCTION(deadline_writesstarved_show, dd->writes_starved, 0);
729 SHOW_FUNCTION(deadline_frontmerges_show, dd->front_merges, 0);
730 SHOW_FUNCTION(deadline_fifobatch_show, dd->fifo_batch, 0);
733 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
734 static ssize_t __FUNC(struct deadline_data *dd, const char *page, size_t count) \
737 int ret = deadline_var_store(&__data, (page), count); \
738 if (__data < (MIN)) \
740 else if (__data > (MAX)) \
743 *(__PTR) = msecs_to_jiffies(__data); \
748 STORE_FUNCTION(deadline_readexpire_store, &dd->fifo_expire[READ], 0, INT_MAX, 1);
749 STORE_FUNCTION(deadline_writeexpire_store, &dd->fifo_expire[WRITE], 0, INT_MAX, 1);
750 STORE_FUNCTION(deadline_writesstarved_store, &dd->writes_starved, INT_MIN, INT_MAX, 0);
751 STORE_FUNCTION(deadline_frontmerges_store, &dd->front_merges, 0, 1, 0);
752 STORE_FUNCTION(deadline_fifobatch_store, &dd->fifo_batch, 0, INT_MAX, 0);
753 #undef STORE_FUNCTION
755 static struct deadline_fs_entry deadline_readexpire_entry = {
756 .attr = {.name = "read_expire", .mode = S_IRUGO | S_IWUSR },
757 .show = deadline_readexpire_show,
758 .store = deadline_readexpire_store,
760 static struct deadline_fs_entry deadline_writeexpire_entry = {
761 .attr = {.name = "write_expire", .mode = S_IRUGO | S_IWUSR },
762 .show = deadline_writeexpire_show,
763 .store = deadline_writeexpire_store,
765 static struct deadline_fs_entry deadline_writesstarved_entry = {
766 .attr = {.name = "writes_starved", .mode = S_IRUGO | S_IWUSR },
767 .show = deadline_writesstarved_show,
768 .store = deadline_writesstarved_store,
770 static struct deadline_fs_entry deadline_frontmerges_entry = {
771 .attr = {.name = "front_merges", .mode = S_IRUGO | S_IWUSR },
772 .show = deadline_frontmerges_show,
773 .store = deadline_frontmerges_store,
775 static struct deadline_fs_entry deadline_fifobatch_entry = {
776 .attr = {.name = "fifo_batch", .mode = S_IRUGO | S_IWUSR },
777 .show = deadline_fifobatch_show,
778 .store = deadline_fifobatch_store,
781 static struct attribute *default_attrs[] = {
782 &deadline_readexpire_entry.attr,
783 &deadline_writeexpire_entry.attr,
784 &deadline_writesstarved_entry.attr,
785 &deadline_frontmerges_entry.attr,
786 &deadline_fifobatch_entry.attr,
790 #define to_deadline(atr) container_of((atr), struct deadline_fs_entry, attr)
793 deadline_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
795 elevator_t *e = container_of(kobj, elevator_t, kobj);
796 struct deadline_fs_entry *entry = to_deadline(attr);
801 return entry->show(e->elevator_data, page);
805 deadline_attr_store(struct kobject *kobj, struct attribute *attr,
806 const char *page, size_t length)
808 elevator_t *e = container_of(kobj, elevator_t, kobj);
809 struct deadline_fs_entry *entry = to_deadline(attr);
814 return entry->store(e->elevator_data, page, length);
817 static struct sysfs_ops deadline_sysfs_ops = {
818 .show = deadline_attr_show,
819 .store = deadline_attr_store,
822 static struct kobj_type deadline_ktype = {
823 .sysfs_ops = &deadline_sysfs_ops,
824 .default_attrs = default_attrs,
827 static struct elevator_type iosched_deadline = {
829 .elevator_merge_fn = deadline_merge,
830 .elevator_merged_fn = deadline_merged_request,
831 .elevator_merge_req_fn = deadline_merged_requests,
832 .elevator_dispatch_fn = deadline_dispatch_requests,
833 .elevator_add_req_fn = deadline_add_request,
834 .elevator_queue_empty_fn = deadline_queue_empty,
835 .elevator_former_req_fn = deadline_former_request,
836 .elevator_latter_req_fn = deadline_latter_request,
837 .elevator_set_req_fn = deadline_set_request,
838 .elevator_put_req_fn = deadline_put_request,
839 .elevator_init_fn = deadline_init_queue,
840 .elevator_exit_fn = deadline_exit_queue,
843 .elevator_ktype = &deadline_ktype,
844 .elevator_name = "deadline",
845 .elevator_owner = THIS_MODULE,
848 static int __init deadline_init(void)
852 drq_pool = kmem_cache_create("deadline_drq", sizeof(struct deadline_rq),
858 ret = elv_register(&iosched_deadline);
860 kmem_cache_destroy(drq_pool);
865 static void __exit deadline_exit(void)
867 kmem_cache_destroy(drq_pool);
868 elv_unregister(&iosched_deadline);
871 module_init(deadline_init);
872 module_exit(deadline_exit);
874 MODULE_AUTHOR("Jens Axboe");
875 MODULE_LICENSE("GPL");
876 MODULE_DESCRIPTION("deadline IO scheduler");