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_queue
*eq
)
129 if (eq
->ops
->elevator_init_fn
)
130 ret
= eq
->ops
->elevator_init_fn(q
, eq
);
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
;
184 INIT_LIST_HEAD(&q
->queue_head
);
185 q
->last_merge
= NULL
;
187 q
->boundary_rq
= NULL
;
189 if (name
&& !(e
= elevator_get(name
)))
192 if (!e
&& *chosen_elevator
&& !(e
= elevator_get(chosen_elevator
)))
193 printk("I/O scheduler %s not found\n", chosen_elevator
);
195 if (!e
&& !(e
= elevator_get(CONFIG_DEFAULT_IOSCHED
))) {
196 printk("Default I/O scheduler not found, using no-op\n");
197 e
= elevator_get("noop");
200 eq
= elevator_alloc(e
);
204 ret
= elevator_attach(q
, eq
);
206 kobject_put(&eq
->kobj
);
211 void elevator_exit(elevator_t
*e
)
213 mutex_lock(&e
->sysfs_lock
);
214 if (e
->ops
->elevator_exit_fn
)
215 e
->ops
->elevator_exit_fn(e
);
217 mutex_unlock(&e
->sysfs_lock
);
219 kobject_put(&e
->kobj
);
223 * Insert rq into dispatch queue of q. Queue lock must be held on
224 * entry. If sort != 0, rq is sort-inserted; otherwise, rq will be
225 * appended to the dispatch queue. To be used by specific elevators.
227 void elv_dispatch_sort(request_queue_t
*q
, struct request
*rq
)
230 struct list_head
*entry
;
232 if (q
->last_merge
== rq
)
233 q
->last_merge
= NULL
;
236 boundary
= q
->end_sector
;
238 list_for_each_prev(entry
, &q
->queue_head
) {
239 struct request
*pos
= list_entry_rq(entry
);
241 if (pos
->flags
& (REQ_SOFTBARRIER
|REQ_HARDBARRIER
|REQ_STARTED
))
243 if (rq
->sector
>= boundary
) {
244 if (pos
->sector
< boundary
)
247 if (pos
->sector
>= boundary
)
250 if (rq
->sector
>= pos
->sector
)
254 list_add(&rq
->queuelist
, entry
);
257 int elv_merge(request_queue_t
*q
, struct request
**req
, struct bio
*bio
)
259 elevator_t
*e
= q
->elevator
;
263 ret
= elv_try_merge(q
->last_merge
, bio
);
264 if (ret
!= ELEVATOR_NO_MERGE
) {
265 *req
= q
->last_merge
;
270 if (e
->ops
->elevator_merge_fn
)
271 return e
->ops
->elevator_merge_fn(q
, req
, bio
);
273 return ELEVATOR_NO_MERGE
;
276 void elv_merged_request(request_queue_t
*q
, struct request
*rq
)
278 elevator_t
*e
= q
->elevator
;
280 if (e
->ops
->elevator_merged_fn
)
281 e
->ops
->elevator_merged_fn(q
, rq
);
286 void elv_merge_requests(request_queue_t
*q
, struct request
*rq
,
287 struct request
*next
)
289 elevator_t
*e
= q
->elevator
;
291 if (e
->ops
->elevator_merge_req_fn
)
292 e
->ops
->elevator_merge_req_fn(q
, rq
, next
);
298 void elv_requeue_request(request_queue_t
*q
, struct request
*rq
)
300 elevator_t
*e
= q
->elevator
;
303 * it already went through dequeue, we need to decrement the
304 * in_flight count again
306 if (blk_account_rq(rq
)) {
308 if (blk_sorted_rq(rq
) && e
->ops
->elevator_deactivate_req_fn
)
309 e
->ops
->elevator_deactivate_req_fn(q
, rq
);
312 rq
->flags
&= ~REQ_STARTED
;
314 elv_insert(q
, rq
, ELEVATOR_INSERT_REQUEUE
);
317 static void elv_drain_elevator(request_queue_t
*q
)
320 while (q
->elevator
->ops
->elevator_dispatch_fn(q
, 1))
322 if (q
->nr_sorted
== 0)
324 if (printed
++ < 10) {
325 printk(KERN_ERR
"%s: forced dispatching is broken "
326 "(nr_sorted=%u), please report this\n",
327 q
->elevator
->elevator_type
->elevator_name
, q
->nr_sorted
);
331 void elv_insert(request_queue_t
*q
, struct request
*rq
, int where
)
333 struct list_head
*pos
;
339 case ELEVATOR_INSERT_FRONT
:
340 rq
->flags
|= REQ_SOFTBARRIER
;
342 list_add(&rq
->queuelist
, &q
->queue_head
);
345 case ELEVATOR_INSERT_BACK
:
346 rq
->flags
|= REQ_SOFTBARRIER
;
347 elv_drain_elevator(q
);
348 list_add_tail(&rq
->queuelist
, &q
->queue_head
);
350 * We kick the queue here for the following reasons.
351 * - The elevator might have returned NULL previously
352 * to delay requests and returned them now. As the
353 * queue wasn't empty before this request, ll_rw_blk
354 * won't run the queue on return, resulting in hang.
355 * - Usually, back inserted requests won't be merged
356 * with anything. There's no point in delaying queue
363 case ELEVATOR_INSERT_SORT
:
364 BUG_ON(!blk_fs_request(rq
));
365 rq
->flags
|= REQ_SORTED
;
367 if (q
->last_merge
== NULL
&& rq_mergeable(rq
))
370 * Some ioscheds (cfq) run q->request_fn directly, so
371 * rq cannot be accessed after calling
372 * elevator_add_req_fn.
374 q
->elevator
->ops
->elevator_add_req_fn(q
, rq
);
377 case ELEVATOR_INSERT_REQUEUE
:
379 * If ordered flush isn't in progress, we do front
380 * insertion; otherwise, requests should be requeued
383 rq
->flags
|= REQ_SOFTBARRIER
;
385 if (q
->ordseq
== 0) {
386 list_add(&rq
->queuelist
, &q
->queue_head
);
390 ordseq
= blk_ordered_req_seq(rq
);
392 list_for_each(pos
, &q
->queue_head
) {
393 struct request
*pos_rq
= list_entry_rq(pos
);
394 if (ordseq
<= blk_ordered_req_seq(pos_rq
))
398 list_add_tail(&rq
->queuelist
, pos
);
402 printk(KERN_ERR
"%s: bad insertion point %d\n",
403 __FUNCTION__
, where
);
407 if (blk_queue_plugged(q
)) {
408 int nrq
= q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]
411 if (nrq
>= q
->unplug_thresh
)
412 __generic_unplug_device(q
);
416 void __elv_add_request(request_queue_t
*q
, struct request
*rq
, int where
,
420 rq
->flags
|= REQ_ORDERED_COLOR
;
422 if (rq
->flags
& (REQ_SOFTBARRIER
| REQ_HARDBARRIER
)) {
424 * toggle ordered color
426 if (blk_barrier_rq(rq
))
430 * barriers implicitly indicate back insertion
432 if (where
== ELEVATOR_INSERT_SORT
)
433 where
= ELEVATOR_INSERT_BACK
;
436 * this request is scheduling boundary, update
439 if (blk_fs_request(rq
)) {
440 q
->end_sector
= rq_end_sector(rq
);
443 } else if (!(rq
->flags
& REQ_ELVPRIV
) && where
== ELEVATOR_INSERT_SORT
)
444 where
= ELEVATOR_INSERT_BACK
;
449 elv_insert(q
, rq
, where
);
452 void elv_add_request(request_queue_t
*q
, struct request
*rq
, int where
,
457 spin_lock_irqsave(q
->queue_lock
, flags
);
458 __elv_add_request(q
, rq
, where
, plug
);
459 spin_unlock_irqrestore(q
->queue_lock
, flags
);
462 static inline struct request
*__elv_next_request(request_queue_t
*q
)
467 while (!list_empty(&q
->queue_head
)) {
468 rq
= list_entry_rq(q
->queue_head
.next
);
469 if (blk_do_ordered(q
, &rq
))
473 if (!q
->elevator
->ops
->elevator_dispatch_fn(q
, 0))
478 struct request
*elv_next_request(request_queue_t
*q
)
483 while ((rq
= __elv_next_request(q
)) != NULL
) {
484 if (!(rq
->flags
& REQ_STARTED
)) {
485 elevator_t
*e
= q
->elevator
;
488 * This is the first time the device driver
489 * sees this request (possibly after
490 * requeueing). Notify IO scheduler.
492 if (blk_sorted_rq(rq
) &&
493 e
->ops
->elevator_activate_req_fn
)
494 e
->ops
->elevator_activate_req_fn(q
, rq
);
497 * just mark as started even if we don't start
498 * it, a request that has been delayed should
499 * not be passed by new incoming requests
501 rq
->flags
|= REQ_STARTED
;
504 if (!q
->boundary_rq
|| q
->boundary_rq
== rq
) {
505 q
->end_sector
= rq_end_sector(rq
);
506 q
->boundary_rq
= NULL
;
509 if ((rq
->flags
& REQ_DONTPREP
) || !q
->prep_rq_fn
)
512 ret
= q
->prep_rq_fn(q
, rq
);
513 if (ret
== BLKPREP_OK
) {
515 } else if (ret
== BLKPREP_DEFER
) {
517 * the request may have been (partially) prepped.
518 * we need to keep this request in the front to
519 * avoid resource deadlock. REQ_STARTED will
520 * prevent other fs requests from passing this one.
524 } else if (ret
== BLKPREP_KILL
) {
525 int nr_bytes
= rq
->hard_nr_sectors
<< 9;
528 nr_bytes
= rq
->data_len
;
530 blkdev_dequeue_request(rq
);
531 rq
->flags
|= REQ_QUIET
;
532 end_that_request_chunk(rq
, 0, nr_bytes
);
533 end_that_request_last(rq
, 0);
535 printk(KERN_ERR
"%s: bad return=%d\n", __FUNCTION__
,
544 void elv_dequeue_request(request_queue_t
*q
, struct request
*rq
)
546 BUG_ON(list_empty(&rq
->queuelist
));
548 list_del_init(&rq
->queuelist
);
551 * the time frame between a request being removed from the lists
552 * and to it is freed is accounted as io that is in progress at
555 if (blk_account_rq(rq
))
559 int elv_queue_empty(request_queue_t
*q
)
561 elevator_t
*e
= q
->elevator
;
563 if (!list_empty(&q
->queue_head
))
566 if (e
->ops
->elevator_queue_empty_fn
)
567 return e
->ops
->elevator_queue_empty_fn(q
);
572 struct request
*elv_latter_request(request_queue_t
*q
, struct request
*rq
)
574 elevator_t
*e
= q
->elevator
;
576 if (e
->ops
->elevator_latter_req_fn
)
577 return e
->ops
->elevator_latter_req_fn(q
, rq
);
581 struct request
*elv_former_request(request_queue_t
*q
, struct request
*rq
)
583 elevator_t
*e
= q
->elevator
;
585 if (e
->ops
->elevator_former_req_fn
)
586 return e
->ops
->elevator_former_req_fn(q
, rq
);
590 int elv_set_request(request_queue_t
*q
, struct request
*rq
, struct bio
*bio
,
593 elevator_t
*e
= q
->elevator
;
595 if (e
->ops
->elevator_set_req_fn
)
596 return e
->ops
->elevator_set_req_fn(q
, rq
, bio
, gfp_mask
);
598 rq
->elevator_private
= NULL
;
602 void elv_put_request(request_queue_t
*q
, struct request
*rq
)
604 elevator_t
*e
= q
->elevator
;
606 if (e
->ops
->elevator_put_req_fn
)
607 e
->ops
->elevator_put_req_fn(q
, rq
);
610 int elv_may_queue(request_queue_t
*q
, int rw
, struct bio
*bio
)
612 elevator_t
*e
= q
->elevator
;
614 if (e
->ops
->elevator_may_queue_fn
)
615 return e
->ops
->elevator_may_queue_fn(q
, rw
, bio
);
617 return ELV_MQUEUE_MAY
;
620 void elv_completed_request(request_queue_t
*q
, struct request
*rq
)
622 elevator_t
*e
= q
->elevator
;
625 * request is released from the driver, io must be done
627 if (blk_account_rq(rq
)) {
629 if (blk_sorted_rq(rq
) && e
->ops
->elevator_completed_req_fn
)
630 e
->ops
->elevator_completed_req_fn(q
, rq
);
634 * Check if the queue is waiting for fs requests to be
635 * drained for flush sequence.
637 if (unlikely(q
->ordseq
)) {
638 struct request
*first_rq
= list_entry_rq(q
->queue_head
.next
);
639 if (q
->in_flight
== 0 &&
640 blk_ordered_cur_seq(q
) == QUEUE_ORDSEQ_DRAIN
&&
641 blk_ordered_req_seq(first_rq
) > QUEUE_ORDSEQ_DRAIN
) {
642 blk_ordered_complete_seq(q
, QUEUE_ORDSEQ_DRAIN
, 0);
648 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
651 elv_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
653 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
654 struct elv_fs_entry
*entry
= to_elv(attr
);
660 mutex_lock(&e
->sysfs_lock
);
661 error
= e
->ops
? entry
->show(e
, page
) : -ENOENT
;
662 mutex_unlock(&e
->sysfs_lock
);
667 elv_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
668 const char *page
, size_t length
)
670 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
671 struct elv_fs_entry
*entry
= to_elv(attr
);
677 mutex_lock(&e
->sysfs_lock
);
678 error
= e
->ops
? entry
->store(e
, page
, length
) : -ENOENT
;
679 mutex_unlock(&e
->sysfs_lock
);
683 static struct sysfs_ops elv_sysfs_ops
= {
684 .show
= elv_attr_show
,
685 .store
= elv_attr_store
,
688 static struct kobj_type elv_ktype
= {
689 .sysfs_ops
= &elv_sysfs_ops
,
690 .release
= elevator_release
,
693 int elv_register_queue(struct request_queue
*q
)
695 elevator_t
*e
= q
->elevator
;
698 e
->kobj
.parent
= &q
->kobj
;
700 error
= kobject_add(&e
->kobj
);
702 struct elv_fs_entry
*attr
= e
->elevator_type
->elevator_attrs
;
704 while (attr
->attr
.name
) {
705 if (sysfs_create_file(&e
->kobj
, &attr
->attr
))
710 kobject_uevent(&e
->kobj
, KOBJ_ADD
);
715 void elv_unregister_queue(struct request_queue
*q
)
718 elevator_t
*e
= q
->elevator
;
719 kobject_uevent(&e
->kobj
, KOBJ_REMOVE
);
720 kobject_del(&e
->kobj
);
724 int elv_register(struct elevator_type
*e
)
726 spin_lock_irq(&elv_list_lock
);
727 if (elevator_find(e
->elevator_name
))
729 list_add_tail(&e
->list
, &elv_list
);
730 spin_unlock_irq(&elv_list_lock
);
732 printk(KERN_INFO
"io scheduler %s registered", e
->elevator_name
);
733 if (!strcmp(e
->elevator_name
, chosen_elevator
) ||
734 (!*chosen_elevator
&&
735 !strcmp(e
->elevator_name
, CONFIG_DEFAULT_IOSCHED
)))
736 printk(" (default)");
740 EXPORT_SYMBOL_GPL(elv_register
);
742 void elv_unregister(struct elevator_type
*e
)
744 struct task_struct
*g
, *p
;
747 * Iterate every thread in the process to remove the io contexts.
750 read_lock(&tasklist_lock
);
751 do_each_thread(g
, p
) {
753 e
->ops
.trim(p
->io_context
);
755 } while_each_thread(g
, p
);
756 read_unlock(&tasklist_lock
);
759 spin_lock_irq(&elv_list_lock
);
760 list_del_init(&e
->list
);
761 spin_unlock_irq(&elv_list_lock
);
763 EXPORT_SYMBOL_GPL(elv_unregister
);
766 * switch to new_e io scheduler. be careful not to introduce deadlocks -
767 * we don't free the old io scheduler, before we have allocated what we
768 * need for the new one. this way we have a chance of going back to the old
769 * one, if the new one fails init for some reason.
771 static int elevator_switch(request_queue_t
*q
, struct elevator_type
*new_e
)
773 elevator_t
*old_elevator
, *e
;
776 * Allocate new elevator
778 e
= elevator_alloc(new_e
);
783 * Turn on BYPASS and drain all requests w/ elevator private data
785 spin_lock_irq(q
->queue_lock
);
787 set_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
789 elv_drain_elevator(q
);
791 while (q
->rq
.elvpriv
) {
794 spin_unlock_irq(q
->queue_lock
);
796 spin_lock_irq(q
->queue_lock
);
797 elv_drain_elevator(q
);
800 spin_unlock_irq(q
->queue_lock
);
803 * unregister old elevator data
805 elv_unregister_queue(q
);
806 old_elevator
= q
->elevator
;
809 * attach and start new elevator
811 if (elevator_attach(q
, e
))
814 if (elv_register_queue(q
))
818 * finally exit old elevator and turn off BYPASS.
820 elevator_exit(old_elevator
);
821 clear_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
826 * switch failed, exit the new io scheduler and reattach the old
827 * one again (along with re-adding the sysfs dir)
832 q
->elevator
= old_elevator
;
833 elv_register_queue(q
);
834 clear_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
836 kobject_put(&e
->kobj
);
840 ssize_t
elv_iosched_store(request_queue_t
*q
, const char *name
, size_t count
)
842 char elevator_name
[ELV_NAME_MAX
];
844 struct elevator_type
*e
;
846 elevator_name
[sizeof(elevator_name
) - 1] = '\0';
847 strncpy(elevator_name
, name
, sizeof(elevator_name
) - 1);
848 len
= strlen(elevator_name
);
850 if (len
&& elevator_name
[len
- 1] == '\n')
851 elevator_name
[len
- 1] = '\0';
853 e
= elevator_get(elevator_name
);
855 printk(KERN_ERR
"elevator: type %s not found\n", elevator_name
);
859 if (!strcmp(elevator_name
, q
->elevator
->elevator_type
->elevator_name
)) {
864 if (!elevator_switch(q
, e
))
865 printk(KERN_ERR
"elevator: switch to %s failed\n",elevator_name
);
869 ssize_t
elv_iosched_show(request_queue_t
*q
, char *name
)
871 elevator_t
*e
= q
->elevator
;
872 struct elevator_type
*elv
= e
->elevator_type
;
873 struct list_head
*entry
;
876 spin_lock_irq(q
->queue_lock
);
877 list_for_each(entry
, &elv_list
) {
878 struct elevator_type
*__e
;
880 __e
= list_entry(entry
, struct elevator_type
, list
);
881 if (!strcmp(elv
->elevator_name
, __e
->elevator_name
))
882 len
+= sprintf(name
+len
, "[%s] ", elv
->elevator_name
);
884 len
+= sprintf(name
+len
, "%s ", __e
->elevator_name
);
886 spin_unlock_irq(q
->queue_lock
);
888 len
+= sprintf(len
+name
, "\n");
892 EXPORT_SYMBOL(elv_dispatch_sort
);
893 EXPORT_SYMBOL(elv_add_request
);
894 EXPORT_SYMBOL(__elv_add_request
);
895 EXPORT_SYMBOL(elv_requeue_request
);
896 EXPORT_SYMBOL(elv_next_request
);
897 EXPORT_SYMBOL(elv_dequeue_request
);
898 EXPORT_SYMBOL(elv_queue_empty
);
899 EXPORT_SYMBOL(elv_completed_request
);
900 EXPORT_SYMBOL(elevator_exit
);
901 EXPORT_SYMBOL(elevator_init
);