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>
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 int elevator_attach(request_queue_t *q, struct elevator_type *e,
124 struct elevator_queue *eq)
128 memset(eq, 0, sizeof(*eq));
130 eq->elevator_type = e;
134 if (eq->ops->elevator_init_fn)
135 ret = eq->ops->elevator_init_fn(q, eq);
140 static char chosen_elevator[16];
142 static int __init elevator_setup(char *str)
145 * Be backwards-compatible with previous kernels, so users
146 * won't get the wrong elevator.
148 if (!strcmp(str, "as"))
149 strcpy(chosen_elevator, "anticipatory");
151 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
155 __setup("elevator=", elevator_setup);
157 int elevator_init(request_queue_t *q, char *name)
159 struct elevator_type *e = NULL;
160 struct elevator_queue *eq;
163 INIT_LIST_HEAD(&q->queue_head);
164 q->last_merge = NULL;
166 q->boundary_rq = NULL;
168 if (name && !(e = elevator_get(name)))
171 if (!e && *chosen_elevator && !(e = elevator_get(chosen_elevator)))
172 printk("I/O scheduler %s not found\n", chosen_elevator);
174 if (!e && !(e = elevator_get(CONFIG_DEFAULT_IOSCHED))) {
175 printk("Default I/O scheduler not found, using no-op\n");
176 e = elevator_get("noop");
179 eq = kmalloc(sizeof(struct elevator_queue), GFP_KERNEL);
185 ret = elevator_attach(q, e, eq);
194 void elevator_exit(elevator_t *e)
196 if (e->ops->elevator_exit_fn)
197 e->ops->elevator_exit_fn(e);
199 elevator_put(e->elevator_type);
200 e->elevator_type = NULL;
205 * Insert rq into dispatch queue of q. Queue lock must be held on
206 * entry. If sort != 0, rq is sort-inserted; otherwise, rq will be
207 * appended to the dispatch queue. To be used by specific elevators.
209 void elv_dispatch_sort(request_queue_t *q, struct request *rq)
212 struct list_head *entry;
214 if (q->last_merge == rq)
215 q->last_merge = NULL;
218 boundary = q->end_sector;
220 list_for_each_prev(entry, &q->queue_head) {
221 struct request *pos = list_entry_rq(entry);
223 if (pos->flags & (REQ_SOFTBARRIER|REQ_HARDBARRIER|REQ_STARTED))
225 if (rq->sector >= boundary) {
226 if (pos->sector < boundary)
229 if (pos->sector >= boundary)
232 if (rq->sector >= pos->sector)
236 list_add(&rq->queuelist, entry);
239 int elv_merge(request_queue_t *q, struct request **req, struct bio *bio)
241 elevator_t *e = q->elevator;
245 ret = elv_try_merge(q->last_merge, bio);
246 if (ret != ELEVATOR_NO_MERGE) {
247 *req = q->last_merge;
252 if (e->ops->elevator_merge_fn)
253 return e->ops->elevator_merge_fn(q, req, bio);
255 return ELEVATOR_NO_MERGE;
258 void elv_merged_request(request_queue_t *q, struct request *rq)
260 elevator_t *e = q->elevator;
262 if (e->ops->elevator_merged_fn)
263 e->ops->elevator_merged_fn(q, rq);
268 void elv_merge_requests(request_queue_t *q, struct request *rq,
269 struct request *next)
271 elevator_t *e = q->elevator;
273 if (e->ops->elevator_merge_req_fn)
274 e->ops->elevator_merge_req_fn(q, rq, next);
280 void elv_requeue_request(request_queue_t *q, struct request *rq)
282 elevator_t *e = q->elevator;
285 * it already went through dequeue, we need to decrement the
286 * in_flight count again
288 if (blk_account_rq(rq)) {
290 if (blk_sorted_rq(rq) && e->ops->elevator_deactivate_req_fn)
291 e->ops->elevator_deactivate_req_fn(q, rq);
294 rq->flags &= ~REQ_STARTED;
296 elv_insert(q, rq, ELEVATOR_INSERT_REQUEUE);
299 static void elv_drain_elevator(request_queue_t *q)
302 while (q->elevator->ops->elevator_dispatch_fn(q, 1))
304 if (q->nr_sorted == 0)
306 if (printed++ < 10) {
307 printk(KERN_ERR "%s: forced dispatching is broken "
308 "(nr_sorted=%u), please report this\n",
309 q->elevator->elevator_type->elevator_name, q->nr_sorted);
313 void elv_insert(request_queue_t *q, struct request *rq, int where)
315 struct list_head *pos;
321 case ELEVATOR_INSERT_FRONT:
322 rq->flags |= REQ_SOFTBARRIER;
324 list_add(&rq->queuelist, &q->queue_head);
327 case ELEVATOR_INSERT_BACK:
328 rq->flags |= REQ_SOFTBARRIER;
329 elv_drain_elevator(q);
330 list_add_tail(&rq->queuelist, &q->queue_head);
332 * We kick the queue here for the following reasons.
333 * - The elevator might have returned NULL previously
334 * to delay requests and returned them now. As the
335 * queue wasn't empty before this request, ll_rw_blk
336 * won't run the queue on return, resulting in hang.
337 * - Usually, back inserted requests won't be merged
338 * with anything. There's no point in delaying queue
345 case ELEVATOR_INSERT_SORT:
346 BUG_ON(!blk_fs_request(rq));
347 rq->flags |= REQ_SORTED;
349 if (q->last_merge == NULL && rq_mergeable(rq))
352 * Some ioscheds (cfq) run q->request_fn directly, so
353 * rq cannot be accessed after calling
354 * elevator_add_req_fn.
356 q->elevator->ops->elevator_add_req_fn(q, rq);
359 case ELEVATOR_INSERT_REQUEUE:
361 * If ordered flush isn't in progress, we do front
362 * insertion; otherwise, requests should be requeued
365 rq->flags |= REQ_SOFTBARRIER;
367 if (q->ordseq == 0) {
368 list_add(&rq->queuelist, &q->queue_head);
372 ordseq = blk_ordered_req_seq(rq);
374 list_for_each(pos, &q->queue_head) {
375 struct request *pos_rq = list_entry_rq(pos);
376 if (ordseq <= blk_ordered_req_seq(pos_rq))
380 list_add_tail(&rq->queuelist, pos);
384 printk(KERN_ERR "%s: bad insertion point %d\n",
385 __FUNCTION__, where);
389 if (blk_queue_plugged(q)) {
390 int nrq = q->rq.count[READ] + q->rq.count[WRITE]
393 if (nrq >= q->unplug_thresh)
394 __generic_unplug_device(q);
398 void __elv_add_request(request_queue_t *q, struct request *rq, int where,
402 rq->flags |= REQ_ORDERED_COLOR;
404 if (rq->flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)) {
406 * toggle ordered color
408 if (blk_barrier_rq(rq))
412 * barriers implicitly indicate back insertion
414 if (where == ELEVATOR_INSERT_SORT)
415 where = ELEVATOR_INSERT_BACK;
418 * this request is scheduling boundary, update
421 if (blk_fs_request(rq)) {
422 q->end_sector = rq_end_sector(rq);
425 } else if (!(rq->flags & REQ_ELVPRIV) && where == ELEVATOR_INSERT_SORT)
426 where = ELEVATOR_INSERT_BACK;
431 elv_insert(q, rq, where);
434 void elv_add_request(request_queue_t *q, struct request *rq, int where,
439 spin_lock_irqsave(q->queue_lock, flags);
440 __elv_add_request(q, rq, where, plug);
441 spin_unlock_irqrestore(q->queue_lock, flags);
444 static inline struct request *__elv_next_request(request_queue_t *q)
449 while (!list_empty(&q->queue_head)) {
450 rq = list_entry_rq(q->queue_head.next);
451 if (blk_do_ordered(q, &rq))
455 if (!q->elevator->ops->elevator_dispatch_fn(q, 0))
460 struct request *elv_next_request(request_queue_t *q)
465 while ((rq = __elv_next_request(q)) != NULL) {
466 if (!(rq->flags & REQ_STARTED)) {
467 elevator_t *e = q->elevator;
470 * This is the first time the device driver
471 * sees this request (possibly after
472 * requeueing). Notify IO scheduler.
474 if (blk_sorted_rq(rq) &&
475 e->ops->elevator_activate_req_fn)
476 e->ops->elevator_activate_req_fn(q, rq);
479 * just mark as started even if we don't start
480 * it, a request that has been delayed should
481 * not be passed by new incoming requests
483 rq->flags |= REQ_STARTED;
486 if (!q->boundary_rq || q->boundary_rq == rq) {
487 q->end_sector = rq_end_sector(rq);
488 q->boundary_rq = NULL;
491 if ((rq->flags & REQ_DONTPREP) || !q->prep_rq_fn)
494 ret = q->prep_rq_fn(q, rq);
495 if (ret == BLKPREP_OK) {
497 } else if (ret == BLKPREP_DEFER) {
499 * the request may have been (partially) prepped.
500 * we need to keep this request in the front to
501 * avoid resource deadlock. REQ_STARTED will
502 * prevent other fs requests from passing this one.
506 } else if (ret == BLKPREP_KILL) {
507 int nr_bytes = rq->hard_nr_sectors << 9;
510 nr_bytes = rq->data_len;
512 blkdev_dequeue_request(rq);
513 rq->flags |= REQ_QUIET;
514 end_that_request_chunk(rq, 0, nr_bytes);
515 end_that_request_last(rq, 0);
517 printk(KERN_ERR "%s: bad return=%d\n", __FUNCTION__,
526 void elv_dequeue_request(request_queue_t *q, struct request *rq)
528 BUG_ON(list_empty(&rq->queuelist));
530 list_del_init(&rq->queuelist);
533 * the time frame between a request being removed from the lists
534 * and to it is freed is accounted as io that is in progress at
537 if (blk_account_rq(rq))
541 int elv_queue_empty(request_queue_t *q)
543 elevator_t *e = q->elevator;
545 if (!list_empty(&q->queue_head))
548 if (e->ops->elevator_queue_empty_fn)
549 return e->ops->elevator_queue_empty_fn(q);
554 struct request *elv_latter_request(request_queue_t *q, struct request *rq)
556 elevator_t *e = q->elevator;
558 if (e->ops->elevator_latter_req_fn)
559 return e->ops->elevator_latter_req_fn(q, rq);
563 struct request *elv_former_request(request_queue_t *q, struct request *rq)
565 elevator_t *e = q->elevator;
567 if (e->ops->elevator_former_req_fn)
568 return e->ops->elevator_former_req_fn(q, rq);
572 int elv_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
575 elevator_t *e = q->elevator;
577 if (e->ops->elevator_set_req_fn)
578 return e->ops->elevator_set_req_fn(q, rq, bio, gfp_mask);
580 rq->elevator_private = NULL;
584 void elv_put_request(request_queue_t *q, struct request *rq)
586 elevator_t *e = q->elevator;
588 if (e->ops->elevator_put_req_fn)
589 e->ops->elevator_put_req_fn(q, rq);
592 int elv_may_queue(request_queue_t *q, int rw, struct bio *bio)
594 elevator_t *e = q->elevator;
596 if (e->ops->elevator_may_queue_fn)
597 return e->ops->elevator_may_queue_fn(q, rw, bio);
599 return ELV_MQUEUE_MAY;
602 void elv_completed_request(request_queue_t *q, struct request *rq)
604 elevator_t *e = q->elevator;
607 * request is released from the driver, io must be done
609 if (blk_account_rq(rq)) {
611 if (blk_sorted_rq(rq) && e->ops->elevator_completed_req_fn)
612 e->ops->elevator_completed_req_fn(q, rq);
616 * Check if the queue is waiting for fs requests to be
617 * drained for flush sequence.
619 if (unlikely(q->ordseq)) {
620 struct request *first_rq = list_entry_rq(q->queue_head.next);
621 if (q->in_flight == 0 &&
622 blk_ordered_cur_seq(q) == QUEUE_ORDSEQ_DRAIN &&
623 blk_ordered_req_seq(first_rq) > QUEUE_ORDSEQ_DRAIN) {
624 blk_ordered_complete_seq(q, QUEUE_ORDSEQ_DRAIN, 0);
630 int elv_register_queue(struct request_queue *q)
632 elevator_t *e = q->elevator;
634 e->kobj.parent = kobject_get(&q->kobj);
638 snprintf(e->kobj.name, KOBJ_NAME_LEN, "%s", "iosched");
639 e->kobj.ktype = e->elevator_type->elevator_ktype;
641 return kobject_register(&e->kobj);
644 void elv_unregister_queue(struct request_queue *q)
647 elevator_t *e = q->elevator;
648 kobject_unregister(&e->kobj);
649 kobject_put(&q->kobj);
653 int elv_register(struct elevator_type *e)
655 spin_lock_irq(&elv_list_lock);
656 if (elevator_find(e->elevator_name))
658 list_add_tail(&e->list, &elv_list);
659 spin_unlock_irq(&elv_list_lock);
661 printk(KERN_INFO "io scheduler %s registered", e->elevator_name);
662 if (!strcmp(e->elevator_name, chosen_elevator) ||
663 (!*chosen_elevator &&
664 !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
665 printk(" (default)");
669 EXPORT_SYMBOL_GPL(elv_register);
671 void elv_unregister(struct elevator_type *e)
673 struct task_struct *g, *p;
676 * Iterate every thread in the process to remove the io contexts.
678 read_lock(&tasklist_lock);
679 do_each_thread(g, p) {
680 struct io_context *ioc = p->io_context;
681 if (ioc && ioc->cic) {
682 ioc->cic->exit(ioc->cic);
683 ioc->cic->dtor(ioc->cic);
686 if (ioc && ioc->aic) {
687 ioc->aic->exit(ioc->aic);
688 ioc->aic->dtor(ioc->aic);
691 } while_each_thread(g, p);
692 read_unlock(&tasklist_lock);
694 spin_lock_irq(&elv_list_lock);
695 list_del_init(&e->list);
696 spin_unlock_irq(&elv_list_lock);
698 EXPORT_SYMBOL_GPL(elv_unregister);
701 * switch to new_e io scheduler. be careful not to introduce deadlocks -
702 * we don't free the old io scheduler, before we have allocated what we
703 * need for the new one. this way we have a chance of going back to the old
704 * one, if the new one fails init for some reason.
706 static void elevator_switch(request_queue_t *q, struct elevator_type *new_e)
708 elevator_t *old_elevator, *e;
711 * Allocate new elevator
713 e = kmalloc(sizeof(elevator_t), GFP_KERNEL);
718 * Turn on BYPASS and drain all requests w/ elevator private data
720 spin_lock_irq(q->queue_lock);
722 set_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
724 elv_drain_elevator(q);
726 while (q->rq.elvpriv) {
729 spin_unlock_irq(q->queue_lock);
731 spin_lock_irq(q->queue_lock);
732 elv_drain_elevator(q);
735 spin_unlock_irq(q->queue_lock);
738 * unregister old elevator data
740 elv_unregister_queue(q);
741 old_elevator = q->elevator;
744 * attach and start new elevator
746 if (elevator_attach(q, new_e, e))
749 if (elv_register_queue(q))
753 * finally exit old elevator and turn off BYPASS.
755 elevator_exit(old_elevator);
756 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
761 * switch failed, exit the new io scheduler and reattach the old
762 * one again (along with re-adding the sysfs dir)
767 q->elevator = old_elevator;
768 elv_register_queue(q);
769 clear_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
773 printk(KERN_ERR "elevator: switch to %s failed\n",new_e->elevator_name);
776 ssize_t elv_iosched_store(request_queue_t *q, const char *name, size_t count)
778 char elevator_name[ELV_NAME_MAX];
780 struct elevator_type *e;
782 elevator_name[sizeof(elevator_name) - 1] = '\0';
783 strncpy(elevator_name, name, sizeof(elevator_name) - 1);
784 len = strlen(elevator_name);
786 if (len && elevator_name[len - 1] == '\n')
787 elevator_name[len - 1] = '\0';
789 e = elevator_get(elevator_name);
791 printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
795 if (!strcmp(elevator_name, q->elevator->elevator_type->elevator_name)) {
800 elevator_switch(q, e);
804 ssize_t elv_iosched_show(request_queue_t *q, char *name)
806 elevator_t *e = q->elevator;
807 struct elevator_type *elv = e->elevator_type;
808 struct list_head *entry;
811 spin_lock_irq(q->queue_lock);
812 list_for_each(entry, &elv_list) {
813 struct elevator_type *__e;
815 __e = list_entry(entry, struct elevator_type, list);
816 if (!strcmp(elv->elevator_name, __e->elevator_name))
817 len += sprintf(name+len, "[%s] ", elv->elevator_name);
819 len += sprintf(name+len, "%s ", __e->elevator_name);
821 spin_unlock_irq(q->queue_lock);
823 len += sprintf(len+name, "\n");
827 EXPORT_SYMBOL(elv_dispatch_sort);
828 EXPORT_SYMBOL(elv_add_request);
829 EXPORT_SYMBOL(__elv_add_request);
830 EXPORT_SYMBOL(elv_requeue_request);
831 EXPORT_SYMBOL(elv_next_request);
832 EXPORT_SYMBOL(elv_dequeue_request);
833 EXPORT_SYMBOL(elv_queue_empty);
834 EXPORT_SYMBOL(elv_completed_request);
835 EXPORT_SYMBOL(elevator_exit);
836 EXPORT_SYMBOL(elevator_init);