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/module.h>
31 #include <linux/slab.h>
32 #include <linux/init.h>
33 #include <linux/compiler.h>
34 #include <linux/delay.h>
35 #include <linux/blktrace_api.h>
37 #include <asm/uaccess.h>
39 static DEFINE_SPINLOCK(elv_list_lock);
40 static LIST_HEAD(elv_list);
43 * can we safely merge with this request?
45 inline int elv_rq_merge_ok(struct request *rq, struct bio *bio)
47 if (!rq_mergeable(rq))
51 * different data direction or already started, don't merge
53 if (bio_data_dir(bio) != rq_data_dir(rq))
57 * same device and no special stuff set, merge is ok
59 if (rq->rq_disk == bio->bi_bdev->bd_disk &&
60 !rq->waiting && !rq->special)
65 EXPORT_SYMBOL(elv_rq_merge_ok);
67 static inline int elv_try_merge(struct request *__rq, struct bio *bio)
69 int ret = ELEVATOR_NO_MERGE;
72 * we can merge and sequence is ok, check if it's possible
74 if (elv_rq_merge_ok(__rq, bio)) {
75 if (__rq->sector + __rq->nr_sectors == bio->bi_sector)
76 ret = ELEVATOR_BACK_MERGE;
77 else if (__rq->sector - bio_sectors(bio) == bio->bi_sector)
78 ret = ELEVATOR_FRONT_MERGE;
84 static struct elevator_type *elevator_find(const char *name)
86 struct elevator_type *e = NULL;
87 struct list_head *entry;
89 list_for_each(entry, &elv_list) {
90 struct elevator_type *__e;
92 __e = list_entry(entry, struct elevator_type, list);
94 if (!strcmp(__e->elevator_name, name)) {
103 static void elevator_put(struct elevator_type *e)
105 module_put(e->elevator_owner);
108 static struct elevator_type *elevator_get(const char *name)
110 struct elevator_type *e;
112 spin_lock_irq(&elv_list_lock);
114 e = elevator_find(name);
115 if (e && !try_module_get(e->elevator_owner))
118 spin_unlock_irq(&elv_list_lock);
123 static void *elevator_init_queue(request_queue_t *q, struct elevator_queue *eq)
125 return eq->ops->elevator_init_fn(q, eq);
128 static void elevator_attach(request_queue_t *q, struct elevator_queue *eq,
132 eq->elevator_data = data;
135 static char chosen_elevator[16];
137 static int __init elevator_setup(char *str)
140 * Be backwards-compatible with previous kernels, so users
141 * won't get the wrong elevator.
143 if (!strcmp(str, "as"))
144 strcpy(chosen_elevator, "anticipatory");
146 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
150 __setup("elevator=", elevator_setup);
152 static struct kobj_type elv_ktype;
154 static elevator_t *elevator_alloc(struct elevator_type *e)
156 elevator_t *eq = kmalloc(sizeof(elevator_t), GFP_KERNEL);
158 memset(eq, 0, sizeof(*eq));
160 eq->elevator_type = e;
161 kobject_init(&eq->kobj);
162 snprintf(eq->kobj.name, KOBJ_NAME_LEN, "%s", "iosched");
163 eq->kobj.ktype = &elv_ktype;
164 mutex_init(&eq->sysfs_lock);
171 static void elevator_release(struct kobject *kobj)
173 elevator_t *e = container_of(kobj, elevator_t, kobj);
174 elevator_put(e->elevator_type);
178 int elevator_init(request_queue_t *q, char *name)
180 struct elevator_type *e = NULL;
181 struct elevator_queue *eq;
185 INIT_LIST_HEAD(&q->queue_head);
186 q->last_merge = NULL;
188 q->boundary_rq = NULL;
190 if (name && !(e = elevator_get(name)))
193 if (!e && *chosen_elevator && !(e = elevator_get(chosen_elevator)))
194 printk("I/O scheduler %s not found\n", chosen_elevator);
196 if (!e && !(e = elevator_get(CONFIG_DEFAULT_IOSCHED))) {
197 printk("Default I/O scheduler not found, using no-op\n");
198 e = elevator_get("noop");
201 eq = elevator_alloc(e);
205 data = elevator_init_queue(q, eq);
207 kobject_put(&eq->kobj);
211 elevator_attach(q, eq, data);
215 void elevator_exit(elevator_t *e)
217 mutex_lock(&e->sysfs_lock);
218 if (e->ops->elevator_exit_fn)
219 e->ops->elevator_exit_fn(e);
221 mutex_unlock(&e->sysfs_lock);
223 kobject_put(&e->kobj);
227 * Insert rq into dispatch queue of q. Queue lock must be held on
228 * entry. If sort != 0, rq is sort-inserted; otherwise, rq will be
229 * appended to the dispatch queue. To be used by specific elevators.
231 void elv_dispatch_sort(request_queue_t *q, struct request *rq)
234 struct list_head *entry;
236 if (q->last_merge == rq)
237 q->last_merge = NULL;
240 boundary = q->end_sector;
242 list_for_each_prev(entry, &q->queue_head) {
243 struct request *pos = list_entry_rq(entry);
245 if (pos->flags & (REQ_SOFTBARRIER|REQ_HARDBARRIER|REQ_STARTED))
247 if (rq->sector >= boundary) {
248 if (pos->sector < boundary)
251 if (pos->sector >= boundary)
254 if (rq->sector >= pos->sector)
258 list_add(&rq->queuelist, entry);
261 int elv_merge(request_queue_t *q, struct request **req, struct bio *bio)
263 elevator_t *e = q->elevator;
267 ret = elv_try_merge(q->last_merge, bio);
268 if (ret != ELEVATOR_NO_MERGE) {
269 *req = q->last_merge;
274 if (e->ops->elevator_merge_fn)
275 return e->ops->elevator_merge_fn(q, req, bio);
277 return ELEVATOR_NO_MERGE;
280 void elv_merged_request(request_queue_t *q, struct request *rq)
282 elevator_t *e = q->elevator;
284 if (e->ops->elevator_merged_fn)
285 e->ops->elevator_merged_fn(q, rq);
290 void elv_merge_requests(request_queue_t *q, struct request *rq,
291 struct request *next)
293 elevator_t *e = q->elevator;
295 if (e->ops->elevator_merge_req_fn)
296 e->ops->elevator_merge_req_fn(q, rq, next);
302 void elv_requeue_request(request_queue_t *q, struct request *rq)
304 elevator_t *e = q->elevator;
307 * it already went through dequeue, we need to decrement the
308 * in_flight count again
310 if (blk_account_rq(rq)) {
312 if (blk_sorted_rq(rq) && e->ops->elevator_deactivate_req_fn)
313 e->ops->elevator_deactivate_req_fn(q, rq);
316 rq->flags &= ~REQ_STARTED;
318 elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
321 static void elv_drain_elevator(request_queue_t *q)
324 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
326 if (q->nr_sorted == 0)
328 if (printed++ < 10) {
329 printk(KERN_ERR "%s: forced dispatching is broken "
330 "(nr_sorted=%u), please report this\n",
331 q->elevator->elevator_type->elevator_name, q->nr_sorted);
335 void elv_insert(request_queue_t *q, struct request *rq, int where)
337 struct list_head *pos;
341 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
346 case ELEVATOR_INSERT_FRONT:
347 rq->flags |= REQ_SOFTBARRIER;
349 list_add(&rq->queuelist, &q->queue_head);
352 case ELEVATOR_INSERT_BACK:
353 rq->flags |= REQ_SOFTBARRIER;
354 elv_drain_elevator(q);
355 list_add_tail(&rq->queuelist, &q->queue_head);
357 * We kick the queue here for the following reasons.
358 * - The elevator might have returned NULL previously
359 * to delay requests and returned them now. As the
360 * queue wasn't empty before this request, ll_rw_blk
361 * won't run the queue on return, resulting in hang.
362 * - Usually, back inserted requests won't be merged
363 * with anything. There's no point in delaying queue
370 case ELEVATOR_INSERT_SORT:
371 BUG_ON(!blk_fs_request(rq));
372 rq->flags |= REQ_SORTED;
374 if (q->last_merge == NULL && rq_mergeable(rq))
377 * Some ioscheds (cfq) run q->request_fn directly, so
378 * rq cannot be accessed after calling
379 * elevator_add_req_fn.
381 q->elevator->ops->elevator_add_req_fn(q, rq);
384 case ELEVATOR_INSERT_REQUEUE:
386 * If ordered flush isn't in progress, we do front
387 * insertion; otherwise, requests should be requeued
390 rq->flags |= REQ_SOFTBARRIER;
392 if (q->ordseq == 0) {
393 list_add(&rq->queuelist, &q->queue_head);
397 ordseq = blk_ordered_req_seq(rq);
399 list_for_each(pos, &q->queue_head) {
400 struct request *pos_rq = list_entry_rq(pos);
401 if (ordseq <= blk_ordered_req_seq(pos_rq))
405 list_add_tail(&rq->queuelist, pos);
407 * most requeues happen because of a busy condition, don't
408 * force unplug of the queue for that case.
414 printk(KERN_ERR "%s: bad insertion point %d\n",
415 __FUNCTION__, where);
419 if (unplug_it && blk_queue_plugged(q)) {
420 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
423 if (nrq >= q->unplug_thresh)
424 __generic_unplug_device(q);
428 void __elv_add_request(request_queue_t *q, struct request *rq, int where,
432 rq->flags |= REQ_ORDERED_COLOR;
434 if (rq->flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
436 * toggle ordered color
438 if (blk_barrier_rq(rq))
442 * barriers implicitly indicate back insertion
444 if (where == ELEVATOR_INSERT_SORT)
445 where = ELEVATOR_INSERT_BACK;
448 * this request is scheduling boundary, update
451 if (blk_fs_request(rq)) {
452 q->end_sector = rq_end_sector(rq);
455 } else if (!(rq->flags & REQ_ELVPRIV) && where == ELEVATOR_INSERT_SORT)
456 where = ELEVATOR_INSERT_BACK;
461 elv_insert(q, rq, where);
464 void elv_add_request(request_queue_t *q, struct request *rq, int where,
469 spin_lock_irqsave(q->queue_lock, flags);
470 __elv_add_request(q, rq, where, plug);
471 spin_unlock_irqrestore(q->queue_lock, flags);
474 static inline struct request *__elv_next_request(request_queue_t *q)
479 while (!list_empty(&q->queue_head)) {
480 rq = list_entry_rq(q->queue_head.next);
481 if (blk_do_ordered(q, &rq))
485 if (!q->elevator->ops->elevator_dispatch_fn(q, 0))
490 struct request *elv_next_request(request_queue_t *q)
495 while ((rq = __elv_next_request(q)) != NULL) {
496 if (!(rq->flags & REQ_STARTED)) {
497 elevator_t *e = q->elevator;
500 * This is the first time the device driver
501 * sees this request (possibly after
502 * requeueing). Notify IO scheduler.
504 if (blk_sorted_rq(rq) &&
505 e->ops->elevator_activate_req_fn)
506 e->ops->elevator_activate_req_fn(q, rq);
509 * just mark as started even if we don't start
510 * it, a request that has been delayed should
511 * not be passed by new incoming requests
513 rq->flags |= REQ_STARTED;
514 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
517 if (!q->boundary_rq || q->boundary_rq == rq) {
518 q->end_sector = rq_end_sector(rq);
519 q->boundary_rq = NULL;
522 if ((rq->flags & REQ_DONTPREP) || !q->prep_rq_fn)
525 ret = q->prep_rq_fn(q, rq);
526 if (ret == BLKPREP_OK) {
528 } else if (ret == BLKPREP_DEFER) {
530 * the request may have been (partially) prepped.
531 * we need to keep this request in the front to
532 * avoid resource deadlock. REQ_STARTED will
533 * prevent other fs requests from passing this one.
537 } else if (ret == BLKPREP_KILL) {
538 int nr_bytes = rq->hard_nr_sectors << 9;
541 nr_bytes = rq->data_len;
543 blkdev_dequeue_request(rq);
544 rq->flags |= REQ_QUIET;
545 end_that_request_chunk(rq, 0, nr_bytes);
546 end_that_request_last(rq, 0);
548 printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
557 void elv_dequeue_request(request_queue_t *q, struct request *rq)
559 BUG_ON(list_empty(&rq->queuelist));
561 list_del_init(&rq->queuelist);
564 * the time frame between a request being removed from the lists
565 * and to it is freed is accounted as io that is in progress at
568 if (blk_account_rq(rq))
572 int elv_queue_empty(request_queue_t *q)
574 elevator_t *e = q->elevator;
576 if (!list_empty(&q->queue_head))
579 if (e->ops->elevator_queue_empty_fn)
580 return e->ops->elevator_queue_empty_fn(q);
585 struct request *elv_latter_request(request_queue_t *q, struct request *rq)
587 elevator_t *e = q->elevator;
589 if (e->ops->elevator_latter_req_fn)
590 return e->ops->elevator_latter_req_fn(q, rq);
594 struct request *elv_former_request(request_queue_t *q, struct request *rq)
596 elevator_t *e = q->elevator;
598 if (e->ops->elevator_former_req_fn)
599 return e->ops->elevator_former_req_fn(q, rq);
603 int elv_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
606 elevator_t *e = q->elevator;
608 if (e->ops->elevator_set_req_fn)
609 return e->ops->elevator_set_req_fn(q, rq, bio, gfp_mask);
611 rq->elevator_private = NULL;
615 void elv_put_request(request_queue_t *q, struct request *rq)
617 elevator_t *e = q->elevator;
619 if (e->ops->elevator_put_req_fn)
620 e->ops->elevator_put_req_fn(q, rq);
623 int elv_may_queue(request_queue_t *q, int rw, struct bio *bio)
625 elevator_t *e = q->elevator;
627 if (e->ops->elevator_may_queue_fn)
628 return e->ops->elevator_may_queue_fn(q, rw, bio);
630 return ELV_MQUEUE_MAY;
633 void elv_completed_request(request_queue_t *q, struct request *rq)
635 elevator_t *e = q->elevator;
638 * request is released from the driver, io must be done
640 if (blk_account_rq(rq)) {
642 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
643 e->ops->elevator_completed_req_fn(q, rq);
647 * Check if the queue is waiting for fs requests to be
648 * drained for flush sequence.
650 if (unlikely(q->ordseq)) {
651 struct request *first_rq = list_entry_rq(q->queue_head.next);
652 if (q->in_flight == 0 &&
653 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
654 blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
655 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
661 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
664 elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
666 elevator_t *e = container_of(kobj, elevator_t, kobj);
667 struct elv_fs_entry *entry = to_elv(attr);
673 mutex_lock(&e->sysfs_lock);
674 error = e->ops ? entry->show(e, page) : -ENOENT;
675 mutex_unlock(&e->sysfs_lock);
680 elv_attr_store(struct kobject *kobj, struct attribute *attr,
681 const char *page, size_t length)
683 elevator_t *e = container_of(kobj, elevator_t, kobj);
684 struct elv_fs_entry *entry = to_elv(attr);
690 mutex_lock(&e->sysfs_lock);
691 error = e->ops ? entry->store(e, page, length) : -ENOENT;
692 mutex_unlock(&e->sysfs_lock);
696 static struct sysfs_ops elv_sysfs_ops = {
697 .show = elv_attr_show,
698 .store = elv_attr_store,
701 static struct kobj_type elv_ktype = {
702 .sysfs_ops = &elv_sysfs_ops,
703 .release = elevator_release,
706 int elv_register_queue(struct request_queue *q)
708 elevator_t *e = q->elevator;
711 e->kobj.parent = &q->kobj;
713 error = kobject_add(&e->kobj);
715 struct elv_fs_entry *attr = e->elevator_type->elevator_attrs;
717 while (attr->attr.name) {
718 if (sysfs_create_file(&e->kobj, &attr->attr))
723 kobject_uevent(&e->kobj, KOBJ_ADD);
728 static void __elv_unregister_queue(elevator_t *e)
730 kobject_uevent(&e->kobj, KOBJ_REMOVE);
731 kobject_del(&e->kobj);
734 void elv_unregister_queue(struct request_queue *q)
737 __elv_unregister_queue(q->elevator);
740 int elv_register(struct elevator_type *e)
742 spin_lock_irq(&elv_list_lock);
743 BUG_ON(elevator_find(e->elevator_name));
744 list_add_tail(&e->list, &elv_list);
745 spin_unlock_irq(&elv_list_lock);
747 printk(KERN_INFO "io scheduler %s registered", e->elevator_name);
748 if (!strcmp(e->elevator_name, chosen_elevator) ||
749 (!*chosen_elevator &&
750 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
751 printk(" (default)");
755 EXPORT_SYMBOL_GPL(elv_register);
757 void elv_unregister(struct elevator_type *e)
759 struct task_struct *g, *p;
762 * Iterate every thread in the process to remove the io contexts.
765 read_lock(&tasklist_lock);
766 do_each_thread(g, p) {
768 e->ops.trim(p->io_context);
770 } while_each_thread(g, p);
771 read_unlock(&tasklist_lock);
774 spin_lock_irq(&elv_list_lock);
775 list_del_init(&e->list);
776 spin_unlock_irq(&elv_list_lock);
778 EXPORT_SYMBOL_GPL(elv_unregister);
781 * switch to new_e io scheduler. be careful not to introduce deadlocks -
782 * we don't free the old io scheduler, before we have allocated what we
783 * need for the new one. this way we have a chance of going back to the old
784 * one, if the new one fails init for some reason.
786 static int elevator_switch(request_queue_t *q, struct elevator_type *new_e)
788 elevator_t *old_elevator, *e;
792 * Allocate new elevator
794 e = elevator_alloc(new_e);
798 data = elevator_init_queue(q, e);
800 kobject_put(&e->kobj);
805 * Turn on BYPASS and drain all requests w/ elevator private data
807 spin_lock_irq(q->queue_lock);
809 set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
811 elv_drain_elevator(q);
813 while (q->rq.elvpriv) {
816 spin_unlock_irq(q->queue_lock);
818 spin_lock_irq(q->queue_lock);
819 elv_drain_elevator(q);
823 * Remember old elevator.
825 old_elevator = q->elevator;
828 * attach and start new elevator
830 elevator_attach(q, e, data);
832 spin_unlock_irq(q->queue_lock);
834 __elv_unregister_queue(old_elevator);
836 if (elv_register_queue(q))
840 * finally exit old elevator and turn off BYPASS.
842 elevator_exit(old_elevator);
843 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
848 * switch failed, exit the new io scheduler and reattach the old
849 * one again (along with re-adding the sysfs dir)
852 q->elevator = old_elevator;
853 elv_register_queue(q);
854 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
858 ssize_t elv_iosched_store(request_queue_t *q, const char *name, size_t count)
860 char elevator_name[ELV_NAME_MAX];
862 struct elevator_type *e;
864 elevator_name[sizeof(elevator_name) - 1] = '\0';
865 strncpy(elevator_name, name, sizeof(elevator_name) - 1);
866 len = strlen(elevator_name);
868 if (len && elevator_name[len - 1] == '\n')
869 elevator_name[len - 1] = '\0';
871 e = elevator_get(elevator_name);
873 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
877 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
882 if (!elevator_switch(q, e))
883 printk(KERN_ERR "elevator: switch to %s failed\n",elevator_name);
887 ssize_t elv_iosched_show(request_queue_t *q, char *name)
889 elevator_t *e = q->elevator;
890 struct elevator_type *elv = e->elevator_type;
891 struct list_head *entry;
894 spin_lock_irq(q->queue_lock);
895 list_for_each(entry, &elv_list) {
896 struct elevator_type *__e;
898 __e = list_entry(entry, struct elevator_type, list);
899 if (!strcmp(elv->elevator_name, __e->elevator_name))
900 len += sprintf(name+len, "[%s] ", elv->elevator_name);
902 len += sprintf(name+len, "%s ", __e->elevator_name);
904 spin_unlock_irq(q->queue_lock);
906 len += sprintf(len+name, "\n");
910 EXPORT_SYMBOL(elv_dispatch_sort);
911 EXPORT_SYMBOL(elv_add_request);
912 EXPORT_SYMBOL(__elv_add_request);
913 EXPORT_SYMBOL(elv_next_request);
914 EXPORT_SYMBOL(elv_dequeue_request);
915 EXPORT_SYMBOL(elv_queue_empty);
916 EXPORT_SYMBOL(elevator_exit);
917 EXPORT_SYMBOL(elevator_init);