2 * Block device elevator/IO-scheduler.
4 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
6 * 30042000 Jens Axboe <axboe@suse.de> :
8 * Split the elevator a bit so that it is possible to choose a different
9 * one or even write a new "plug in". There are three pieces:
10 * - elevator_fn, inserts a new request in the queue list
11 * - elevator_merge_fn, decides whether a new buffer can be merged with
13 * - elevator_dequeue_fn, called when a request is taken off the active list
15 * 20082000 Dave Jones <davej@suse.de> :
16 * Removed tests for max-bomb-segments, which was breaking elvtune
17 * when run without -bN
20 * - Rework again to work with bio instead of buffer_heads
21 * - loose bi_dev comparisons, partition handling is right now
22 * - completely modularize elevator setup and teardown
25 #include <linux/kernel.h>
27 #include <linux/blkdev.h>
28 #include <linux/elevator.h>
29 #include <linux/bio.h>
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/compiler.h>
35 #include <linux/delay.h>
36 #include <linux/blktrace_api.h>
38 #include <asm/uaccess.h>
40 static DEFINE_SPINLOCK(elv_list_lock);
41 static LIST_HEAD(elv_list);
44 * can we safely merge with this request?
46 inline int elv_rq_merge_ok(struct request *rq, struct bio *bio)
48 if (!rq_mergeable(rq))
52 * different data direction or already started, don't merge
54 if (bio_data_dir(bio) != rq_data_dir(rq))
58 * same device and no special stuff set, merge is ok
60 if (rq->rq_disk == bio->bi_bdev->bd_disk &&
61 !rq->waiting && !rq->special)
66 EXPORT_SYMBOL(elv_rq_merge_ok);
68 static inline int elv_try_merge(struct request *__rq, struct bio *bio)
70 int ret = ELEVATOR_NO_MERGE;
73 * we can merge and sequence is ok, check if it's possible
75 if (elv_rq_merge_ok(__rq, bio)) {
76 if (__rq->sector + __rq->nr_sectors == bio->bi_sector)
77 ret = ELEVATOR_BACK_MERGE;
78 else if (__rq->sector - bio_sectors(bio) == bio->bi_sector)
79 ret = ELEVATOR_FRONT_MERGE;
85 static struct elevator_type *elevator_find(const char *name)
87 struct elevator_type *e = NULL;
88 struct list_head *entry;
90 list_for_each(entry, &elv_list) {
91 struct elevator_type *__e;
93 __e = list_entry(entry, struct elevator_type, list);
95 if (!strcmp(__e->elevator_name, name)) {
104 static void elevator_put(struct elevator_type *e)
106 module_put(e->elevator_owner);
109 static struct elevator_type *elevator_get(const char *name)
111 struct elevator_type *e;
113 spin_lock_irq(&elv_list_lock);
115 e = elevator_find(name);
116 if (e && !try_module_get(e->elevator_owner))
119 spin_unlock_irq(&elv_list_lock);
124 static void *elevator_init_queue(request_queue_t *q, struct elevator_queue *eq)
126 return eq->ops->elevator_init_fn(q, eq);
129 static void elevator_attach(request_queue_t *q, struct elevator_queue *eq,
133 eq->elevator_data = data;
136 static char chosen_elevator[16];
138 static int __init elevator_setup(char *str)
141 * Be backwards-compatible with previous kernels, so users
142 * won't get the wrong elevator.
144 if (!strcmp(str, "as"))
145 strcpy(chosen_elevator, "anticipatory");
147 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
151 __setup("elevator=", elevator_setup);
153 static struct kobj_type elv_ktype;
155 static elevator_t *elevator_alloc(struct elevator_type *e)
157 elevator_t *eq = kmalloc(sizeof(elevator_t), GFP_KERNEL);
159 memset(eq, 0, sizeof(*eq));
161 eq->elevator_type = e;
162 kobject_init(&eq->kobj);
163 snprintf(eq->kobj.name, KOBJ_NAME_LEN, "%s", "iosched");
164 eq->kobj.ktype = &elv_ktype;
165 mutex_init(&eq->sysfs_lock);
172 static void elevator_release(struct kobject *kobj)
174 elevator_t *e = container_of(kobj, elevator_t, kobj);
175 elevator_put(e->elevator_type);
179 int elevator_init(request_queue_t *q, char *name)
181 struct elevator_type *e = NULL;
182 struct elevator_queue *eq;
186 INIT_LIST_HEAD(&q->queue_head);
187 q->last_merge = NULL;
189 q->boundary_rq = NULL;
191 if (name && !(e = elevator_get(name)))
194 if (!e && *chosen_elevator && !(e = elevator_get(chosen_elevator)))
195 printk("I/O scheduler %s not found\n", chosen_elevator);
197 if (!e && !(e = elevator_get(CONFIG_DEFAULT_IOSCHED))) {
198 printk("Default I/O scheduler not found, using no-op\n");
199 e = elevator_get("noop");
202 eq = elevator_alloc(e);
206 data = elevator_init_queue(q, eq);
208 kobject_put(&eq->kobj);
212 elevator_attach(q, eq, data);
216 void elevator_exit(elevator_t *e)
218 mutex_lock(&e->sysfs_lock);
219 if (e->ops->elevator_exit_fn)
220 e->ops->elevator_exit_fn(e);
222 mutex_unlock(&e->sysfs_lock);
224 kobject_put(&e->kobj);
228 * Insert rq into dispatch queue of q. Queue lock must be held on
229 * entry. If sort != 0, rq is sort-inserted; otherwise, rq will be
230 * appended to the dispatch queue. To be used by specific elevators.
232 void elv_dispatch_sort(request_queue_t *q, struct request *rq)
235 struct list_head *entry;
237 if (q->last_merge == rq)
238 q->last_merge = NULL;
241 boundary = q->end_sector;
243 list_for_each_prev(entry, &q->queue_head) {
244 struct request *pos = list_entry_rq(entry);
246 if (pos->flags & (REQ_SOFTBARRIER|REQ_HARDBARRIER|REQ_STARTED))
248 if (rq->sector >= boundary) {
249 if (pos->sector < boundary)
252 if (pos->sector >= boundary)
255 if (rq->sector >= pos->sector)
259 list_add(&rq->queuelist, entry);
262 int elv_merge(request_queue_t *q, struct request **req, struct bio *bio)
264 elevator_t *e = q->elevator;
268 ret = elv_try_merge(q->last_merge, bio);
269 if (ret != ELEVATOR_NO_MERGE) {
270 *req = q->last_merge;
275 if (e->ops->elevator_merge_fn)
276 return e->ops->elevator_merge_fn(q, req, bio);
278 return ELEVATOR_NO_MERGE;
281 void elv_merged_request(request_queue_t *q, struct request *rq)
283 elevator_t *e = q->elevator;
285 if (e->ops->elevator_merged_fn)
286 e->ops->elevator_merged_fn(q, rq);
291 void elv_merge_requests(request_queue_t *q, struct request *rq,
292 struct request *next)
294 elevator_t *e = q->elevator;
296 if (e->ops->elevator_merge_req_fn)
297 e->ops->elevator_merge_req_fn(q, rq, next);
303 void elv_requeue_request(request_queue_t *q, struct request *rq)
305 elevator_t *e = q->elevator;
308 * it already went through dequeue, we need to decrement the
309 * in_flight count again
311 if (blk_account_rq(rq)) {
313 if (blk_sorted_rq(rq) && e->ops->elevator_deactivate_req_fn)
314 e->ops->elevator_deactivate_req_fn(q, rq);
317 rq->flags &= ~REQ_STARTED;
319 elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
322 static void elv_drain_elevator(request_queue_t *q)
325 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
327 if (q->nr_sorted == 0)
329 if (printed++ < 10) {
330 printk(KERN_ERR "%s: forced dispatching is broken "
331 "(nr_sorted=%u), please report this\n",
332 q->elevator->elevator_type->elevator_name, q->nr_sorted);
336 void elv_insert(request_queue_t *q, struct request *rq, int where)
338 struct list_head *pos;
342 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
347 case ELEVATOR_INSERT_FRONT:
348 rq->flags |= REQ_SOFTBARRIER;
350 list_add(&rq->queuelist, &q->queue_head);
353 case ELEVATOR_INSERT_BACK:
354 rq->flags |= REQ_SOFTBARRIER;
355 elv_drain_elevator(q);
356 list_add_tail(&rq->queuelist, &q->queue_head);
358 * We kick the queue here for the following reasons.
359 * - The elevator might have returned NULL previously
360 * to delay requests and returned them now. As the
361 * queue wasn't empty before this request, ll_rw_blk
362 * won't run the queue on return, resulting in hang.
363 * - Usually, back inserted requests won't be merged
364 * with anything. There's no point in delaying queue
371 case ELEVATOR_INSERT_SORT:
372 BUG_ON(!blk_fs_request(rq));
373 rq->flags |= REQ_SORTED;
375 if (q->last_merge == NULL && rq_mergeable(rq))
378 * Some ioscheds (cfq) run q->request_fn directly, so
379 * rq cannot be accessed after calling
380 * elevator_add_req_fn.
382 q->elevator->ops->elevator_add_req_fn(q, rq);
385 case ELEVATOR_INSERT_REQUEUE:
387 * If ordered flush isn't in progress, we do front
388 * insertion; otherwise, requests should be requeued
391 rq->flags |= REQ_SOFTBARRIER;
393 if (q->ordseq == 0) {
394 list_add(&rq->queuelist, &q->queue_head);
398 ordseq = blk_ordered_req_seq(rq);
400 list_for_each(pos, &q->queue_head) {
401 struct request *pos_rq = list_entry_rq(pos);
402 if (ordseq <= blk_ordered_req_seq(pos_rq))
406 list_add_tail(&rq->queuelist, pos);
408 * most requeues happen because of a busy condition, don't
409 * force unplug of the queue for that case.
415 printk(KERN_ERR "%s: bad insertion point %d\n",
416 __FUNCTION__, where);
420 if (unplug_it && blk_queue_plugged(q)) {
421 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
424 if (nrq >= q->unplug_thresh)
425 __generic_unplug_device(q);
429 void __elv_add_request(request_queue_t *q, struct request *rq, int where,
433 rq->flags |= REQ_ORDERED_COLOR;
435 if (rq->flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
437 * toggle ordered color
439 if (blk_barrier_rq(rq))
443 * barriers implicitly indicate back insertion
445 if (where == ELEVATOR_INSERT_SORT)
446 where = ELEVATOR_INSERT_BACK;
449 * this request is scheduling boundary, update
452 if (blk_fs_request(rq)) {
453 q->end_sector = rq_end_sector(rq);
456 } else if (!(rq->flags & REQ_ELVPRIV) && where == ELEVATOR_INSERT_SORT)
457 where = ELEVATOR_INSERT_BACK;
462 elv_insert(q, rq, where);
465 void elv_add_request(request_queue_t *q, struct request *rq, int where,
470 spin_lock_irqsave(q->queue_lock, flags);
471 __elv_add_request(q, rq, where, plug);
472 spin_unlock_irqrestore(q->queue_lock, flags);
475 static inline struct request *__elv_next_request(request_queue_t *q)
480 while (!list_empty(&q->queue_head)) {
481 rq = list_entry_rq(q->queue_head.next);
482 if (blk_do_ordered(q, &rq))
486 if (!q->elevator->ops->elevator_dispatch_fn(q, 0))
491 struct request *elv_next_request(request_queue_t *q)
496 while ((rq = __elv_next_request(q)) != NULL) {
497 if (!(rq->flags & REQ_STARTED)) {
498 elevator_t *e = q->elevator;
501 * This is the first time the device driver
502 * sees this request (possibly after
503 * requeueing). Notify IO scheduler.
505 if (blk_sorted_rq(rq) &&
506 e->ops->elevator_activate_req_fn)
507 e->ops->elevator_activate_req_fn(q, rq);
510 * just mark as started even if we don't start
511 * it, a request that has been delayed should
512 * not be passed by new incoming requests
514 rq->flags |= REQ_STARTED;
515 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
518 if (!q->boundary_rq || q->boundary_rq == rq) {
519 q->end_sector = rq_end_sector(rq);
520 q->boundary_rq = NULL;
523 if ((rq->flags & REQ_DONTPREP) || !q->prep_rq_fn)
526 ret = q->prep_rq_fn(q, rq);
527 if (ret == BLKPREP_OK) {
529 } else if (ret == BLKPREP_DEFER) {
531 * the request may have been (partially) prepped.
532 * we need to keep this request in the front to
533 * avoid resource deadlock. REQ_STARTED will
534 * prevent other fs requests from passing this one.
538 } else if (ret == BLKPREP_KILL) {
539 int nr_bytes = rq->hard_nr_sectors << 9;
542 nr_bytes = rq->data_len;
544 blkdev_dequeue_request(rq);
545 rq->flags |= REQ_QUIET;
546 end_that_request_chunk(rq, 0, nr_bytes);
547 end_that_request_last(rq, 0);
549 printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
558 void elv_dequeue_request(request_queue_t *q, struct request *rq)
560 BUG_ON(list_empty(&rq->queuelist));
562 list_del_init(&rq->queuelist);
565 * the time frame between a request being removed from the lists
566 * and to it is freed is accounted as io that is in progress at
569 if (blk_account_rq(rq))
573 int elv_queue_empty(request_queue_t *q)
575 elevator_t *e = q->elevator;
577 if (!list_empty(&q->queue_head))
580 if (e->ops->elevator_queue_empty_fn)
581 return e->ops->elevator_queue_empty_fn(q);
586 struct request *elv_latter_request(request_queue_t *q, struct request *rq)
588 elevator_t *e = q->elevator;
590 if (e->ops->elevator_latter_req_fn)
591 return e->ops->elevator_latter_req_fn(q, rq);
595 struct request *elv_former_request(request_queue_t *q, struct request *rq)
597 elevator_t *e = q->elevator;
599 if (e->ops->elevator_former_req_fn)
600 return e->ops->elevator_former_req_fn(q, rq);
604 int elv_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
607 elevator_t *e = q->elevator;
609 if (e->ops->elevator_set_req_fn)
610 return e->ops->elevator_set_req_fn(q, rq, bio, gfp_mask);
612 rq->elevator_private = NULL;
616 void elv_put_request(request_queue_t *q, struct request *rq)
618 elevator_t *e = q->elevator;
620 if (e->ops->elevator_put_req_fn)
621 e->ops->elevator_put_req_fn(q, rq);
624 int elv_may_queue(request_queue_t *q, int rw, struct bio *bio)
626 elevator_t *e = q->elevator;
628 if (e->ops->elevator_may_queue_fn)
629 return e->ops->elevator_may_queue_fn(q, rw, bio);
631 return ELV_MQUEUE_MAY;
634 void elv_completed_request(request_queue_t *q, struct request *rq)
636 elevator_t *e = q->elevator;
639 * request is released from the driver, io must be done
641 if (blk_account_rq(rq)) {
643 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
644 e->ops->elevator_completed_req_fn(q, rq);
648 * Check if the queue is waiting for fs requests to be
649 * drained for flush sequence.
651 if (unlikely(q->ordseq)) {
652 struct request *first_rq = list_entry_rq(q->queue_head.next);
653 if (q->in_flight == 0 &&
654 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
655 blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
656 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
662 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
665 elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
667 elevator_t *e = container_of(kobj, elevator_t, kobj);
668 struct elv_fs_entry *entry = to_elv(attr);
674 mutex_lock(&e->sysfs_lock);
675 error = e->ops ? entry->show(e, page) : -ENOENT;
676 mutex_unlock(&e->sysfs_lock);
681 elv_attr_store(struct kobject *kobj, struct attribute *attr,
682 const char *page, size_t length)
684 elevator_t *e = container_of(kobj, elevator_t, kobj);
685 struct elv_fs_entry *entry = to_elv(attr);
691 mutex_lock(&e->sysfs_lock);
692 error = e->ops ? entry->store(e, page, length) : -ENOENT;
693 mutex_unlock(&e->sysfs_lock);
697 static struct sysfs_ops elv_sysfs_ops = {
698 .show = elv_attr_show,
699 .store = elv_attr_store,
702 static struct kobj_type elv_ktype = {
703 .sysfs_ops = &elv_sysfs_ops,
704 .release = elevator_release,
707 int elv_register_queue(struct request_queue *q)
709 elevator_t *e = q->elevator;
712 e->kobj.parent = &q->kobj;
714 error = kobject_add(&e->kobj);
716 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
718 while (attr->attr.name) {
719 if (sysfs_create_file(&e->kobj, &attr->attr))
724 kobject_uevent(&e->kobj, KOBJ_ADD);
729 static void __elv_unregister_queue(elevator_t *e)
731 kobject_uevent(&e->kobj, KOBJ_REMOVE);
732 kobject_del(&e->kobj);
735 void elv_unregister_queue(struct request_queue *q)
738 __elv_unregister_queue(q->elevator);
741 int elv_register(struct elevator_type *e)
743 spin_lock_irq(&elv_list_lock);
744 BUG_ON(elevator_find(e->elevator_name));
745 list_add_tail(&e->list, &elv_list);
746 spin_unlock_irq(&elv_list_lock);
748 printk(KERN_INFO "io scheduler %s registered", e->elevator_name);
749 if (!strcmp(e->elevator_name, chosen_elevator) ||
750 (!*chosen_elevator &&
751 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
752 printk(" (default)");
756 EXPORT_SYMBOL_GPL(elv_register);
758 void elv_unregister(struct elevator_type *e)
760 struct task_struct *g, *p;
763 * Iterate every thread in the process to remove the io contexts.
766 read_lock(&tasklist_lock);
767 do_each_thread(g, p) {
769 e->ops.trim(p->io_context);
771 } while_each_thread(g, p);
772 read_unlock(&tasklist_lock);
775 spin_lock_irq(&elv_list_lock);
776 list_del_init(&e->list);
777 spin_unlock_irq(&elv_list_lock);
779 EXPORT_SYMBOL_GPL(elv_unregister);
782 * switch to new_e io scheduler. be careful not to introduce deadlocks -
783 * we don't free the old io scheduler, before we have allocated what we
784 * need for the new one. this way we have a chance of going back to the old
785 * one, if the new one fails init for some reason.
787 static int elevator_switch(request_queue_t *q, struct elevator_type *new_e)
789 elevator_t *old_elevator, *e;
793 * Allocate new elevator
795 e = elevator_alloc(new_e);
799 data = elevator_init_queue(q, e);
801 kobject_put(&e->kobj);
806 * Turn on BYPASS and drain all requests w/ elevator private data
808 spin_lock_irq(q->queue_lock);
810 set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
812 elv_drain_elevator(q);
814 while (q->rq.elvpriv) {
817 spin_unlock_irq(q->queue_lock);
819 spin_lock_irq(q->queue_lock);
820 elv_drain_elevator(q);
824 * Remember old elevator.
826 old_elevator = q->elevator;
829 * attach and start new elevator
831 elevator_attach(q, e, data);
833 spin_unlock_irq(q->queue_lock);
835 __elv_unregister_queue(old_elevator);
837 if (elv_register_queue(q))
841 * finally exit old elevator and turn off BYPASS.
843 elevator_exit(old_elevator);
844 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
849 * switch failed, exit the new io scheduler and reattach the old
850 * one again (along with re-adding the sysfs dir)
853 q->elevator = old_elevator;
854 elv_register_queue(q);
855 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
859 ssize_t elv_iosched_store(request_queue_t *q, const char *name, size_t count)
861 char elevator_name[ELV_NAME_MAX];
863 struct elevator_type *e;
865 elevator_name[sizeof(elevator_name) - 1] = '\0';
866 strncpy(elevator_name, name, sizeof(elevator_name) - 1);
867 len = strlen(elevator_name);
869 if (len && elevator_name[len - 1] == '\n')
870 elevator_name[len - 1] = '\0';
872 e = elevator_get(elevator_name);
874 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
878 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
883 if (!elevator_switch(q, e))
884 printk(KERN_ERR "elevator: switch to %s failed\n",elevator_name);
888 ssize_t elv_iosched_show(request_queue_t *q, char *name)
890 elevator_t *e = q->elevator;
891 struct elevator_type *elv = e->elevator_type;
892 struct list_head *entry;
895 spin_lock_irq(q->queue_lock);
896 list_for_each(entry, &elv_list) {
897 struct elevator_type *__e;
899 __e = list_entry(entry, struct elevator_type, list);
900 if (!strcmp(elv->elevator_name, __e->elevator_name))
901 len += sprintf(name+len, "[%s] ", elv->elevator_name);
903 len += sprintf(name+len, "%s ", __e->elevator_name);
905 spin_unlock_irq(q->queue_lock);
907 len += sprintf(len+name, "\n");
911 EXPORT_SYMBOL(elv_dispatch_sort);
912 EXPORT_SYMBOL(elv_add_request);
913 EXPORT_SYMBOL(__elv_add_request);
914 EXPORT_SYMBOL(elv_next_request);
915 EXPORT_SYMBOL(elv_dequeue_request);
916 EXPORT_SYMBOL(elv_queue_empty);
917 EXPORT_SYMBOL(elevator_exit);
918 EXPORT_SYMBOL(elevator_init);