2 * Block device elevator/IO-scheduler.
4 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
6 * 30042000 Jens Axboe <axboe@kernel.dk> :
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/blktrace_api.h>
35 #include <linux/hash.h>
36 #include <linux/uaccess.h>
37 #include <linux/pm_runtime.h>
38 #include <linux/blk-cgroup.h>
40 #include <trace/events/block.h>
43 #include "blk-mq-sched.h"
47 static DEFINE_SPINLOCK(elv_list_lock
);
48 static LIST_HEAD(elv_list
);
53 #define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
56 * Query io scheduler to see if the current process issuing bio may be
59 static int elv_iosched_allow_bio_merge(struct request
*rq
, struct bio
*bio
)
61 struct request_queue
*q
= rq
->q
;
62 struct elevator_queue
*e
= q
->elevator
;
64 if (e
->type
->ops
.allow_merge
)
65 return e
->type
->ops
.allow_merge(q
, rq
, bio
);
71 * can we safely merge with this request?
73 bool elv_bio_merge_ok(struct request
*rq
, struct bio
*bio
)
75 if (!blk_rq_merge_ok(rq
, bio
))
78 if (!elv_iosched_allow_bio_merge(rq
, bio
))
83 EXPORT_SYMBOL(elv_bio_merge_ok
);
85 static bool elevator_match(const struct elevator_type
*e
, const char *name
)
87 if (!strcmp(e
->elevator_name
, name
))
89 if (e
->elevator_alias
&& !strcmp(e
->elevator_alias
, name
))
96 * Return scheduler with name 'name'
98 static struct elevator_type
*elevator_find(const char *name
)
100 struct elevator_type
*e
;
102 list_for_each_entry(e
, &elv_list
, list
) {
103 if (elevator_match(e
, name
))
110 static void elevator_put(struct elevator_type
*e
)
112 module_put(e
->elevator_owner
);
115 static struct elevator_type
*elevator_get(struct request_queue
*q
,
116 const char *name
, bool try_loading
)
118 struct elevator_type
*e
;
120 spin_lock(&elv_list_lock
);
122 e
= elevator_find(name
);
123 if (!e
&& try_loading
) {
124 spin_unlock(&elv_list_lock
);
125 request_module("%s-iosched", name
);
126 spin_lock(&elv_list_lock
);
127 e
= elevator_find(name
);
130 if (e
&& !try_module_get(e
->elevator_owner
))
133 spin_unlock(&elv_list_lock
);
137 static char chosen_elevator
[ELV_NAME_MAX
];
139 static int __init
elevator_setup(char *str
)
142 * Be backwards-compatible with previous kernels, so users
143 * won't get the wrong elevator.
145 strncpy(chosen_elevator
, str
, sizeof(chosen_elevator
) - 1);
149 __setup("elevator=", elevator_setup
);
151 static struct kobj_type elv_ktype
;
153 struct elevator_queue
*elevator_alloc(struct request_queue
*q
,
154 struct elevator_type
*e
)
156 struct elevator_queue
*eq
;
158 eq
= kzalloc_node(sizeof(*eq
), GFP_KERNEL
, q
->node
);
163 kobject_init(&eq
->kobj
, &elv_ktype
);
164 mutex_init(&eq
->sysfs_lock
);
169 EXPORT_SYMBOL(elevator_alloc
);
171 static void elevator_release(struct kobject
*kobj
)
173 struct elevator_queue
*e
;
175 e
= container_of(kobj
, struct elevator_queue
, kobj
);
176 elevator_put(e
->type
);
180 void elevator_exit(struct request_queue
*q
, struct elevator_queue
*e
)
182 mutex_lock(&e
->sysfs_lock
);
183 if (e
->type
->ops
.exit_sched
)
184 blk_mq_exit_sched(q
, e
);
185 mutex_unlock(&e
->sysfs_lock
);
187 kobject_put(&e
->kobj
);
190 static inline void __elv_rqhash_del(struct request
*rq
)
193 rq
->rq_flags
&= ~RQF_HASHED
;
196 void elv_rqhash_del(struct request_queue
*q
, struct request
*rq
)
199 __elv_rqhash_del(rq
);
201 EXPORT_SYMBOL_GPL(elv_rqhash_del
);
203 void elv_rqhash_add(struct request_queue
*q
, struct request
*rq
)
205 struct elevator_queue
*e
= q
->elevator
;
207 BUG_ON(ELV_ON_HASH(rq
));
208 hash_add(e
->hash
, &rq
->hash
, rq_hash_key(rq
));
209 rq
->rq_flags
|= RQF_HASHED
;
211 EXPORT_SYMBOL_GPL(elv_rqhash_add
);
213 void elv_rqhash_reposition(struct request_queue
*q
, struct request
*rq
)
215 __elv_rqhash_del(rq
);
216 elv_rqhash_add(q
, rq
);
219 struct request
*elv_rqhash_find(struct request_queue
*q
, sector_t offset
)
221 struct elevator_queue
*e
= q
->elevator
;
222 struct hlist_node
*next
;
225 hash_for_each_possible_safe(e
->hash
, rq
, next
, hash
, offset
) {
226 BUG_ON(!ELV_ON_HASH(rq
));
228 if (unlikely(!rq_mergeable(rq
))) {
229 __elv_rqhash_del(rq
);
233 if (rq_hash_key(rq
) == offset
)
241 * RB-tree support functions for inserting/lookup/removal of requests
242 * in a sorted RB tree.
244 void elv_rb_add(struct rb_root
*root
, struct request
*rq
)
246 struct rb_node
**p
= &root
->rb_node
;
247 struct rb_node
*parent
= NULL
;
248 struct request
*__rq
;
252 __rq
= rb_entry(parent
, struct request
, rb_node
);
254 if (blk_rq_pos(rq
) < blk_rq_pos(__rq
))
256 else if (blk_rq_pos(rq
) >= blk_rq_pos(__rq
))
260 rb_link_node(&rq
->rb_node
, parent
, p
);
261 rb_insert_color(&rq
->rb_node
, root
);
263 EXPORT_SYMBOL(elv_rb_add
);
265 void elv_rb_del(struct rb_root
*root
, struct request
*rq
)
267 BUG_ON(RB_EMPTY_NODE(&rq
->rb_node
));
268 rb_erase(&rq
->rb_node
, root
);
269 RB_CLEAR_NODE(&rq
->rb_node
);
271 EXPORT_SYMBOL(elv_rb_del
);
273 struct request
*elv_rb_find(struct rb_root
*root
, sector_t sector
)
275 struct rb_node
*n
= root
->rb_node
;
279 rq
= rb_entry(n
, struct request
, rb_node
);
281 if (sector
< blk_rq_pos(rq
))
283 else if (sector
> blk_rq_pos(rq
))
291 EXPORT_SYMBOL(elv_rb_find
);
293 enum elv_merge
elv_merge(struct request_queue
*q
, struct request
**req
,
296 struct elevator_queue
*e
= q
->elevator
;
297 struct request
*__rq
;
301 * nomerges: No merges at all attempted
302 * noxmerges: Only simple one-hit cache try
303 * merges: All merge tries attempted
305 if (blk_queue_nomerges(q
) || !bio_mergeable(bio
))
306 return ELEVATOR_NO_MERGE
;
309 * First try one-hit cache.
311 if (q
->last_merge
&& elv_bio_merge_ok(q
->last_merge
, bio
)) {
312 enum elv_merge ret
= blk_try_merge(q
->last_merge
, bio
);
314 if (ret
!= ELEVATOR_NO_MERGE
) {
315 *req
= q
->last_merge
;
320 if (blk_queue_noxmerges(q
))
321 return ELEVATOR_NO_MERGE
;
324 * See if our hash lookup can find a potential backmerge.
326 __rq
= elv_rqhash_find(q
, bio
->bi_iter
.bi_sector
);
327 if (__rq
&& elv_bio_merge_ok(__rq
, bio
)) {
329 return ELEVATOR_BACK_MERGE
;
332 if (e
->type
->ops
.request_merge
)
333 return e
->type
->ops
.request_merge(q
, req
, bio
);
335 return ELEVATOR_NO_MERGE
;
339 * Attempt to do an insertion back merge. Only check for the case where
340 * we can append 'rq' to an existing request, so we can throw 'rq' away
343 * Returns true if we merged, false otherwise
345 bool elv_attempt_insert_merge(struct request_queue
*q
, struct request
*rq
)
347 struct request
*__rq
;
350 if (blk_queue_nomerges(q
))
354 * First try one-hit cache.
356 if (q
->last_merge
&& blk_attempt_req_merge(q
, q
->last_merge
, rq
))
359 if (blk_queue_noxmerges(q
))
364 * See if our hash lookup can find a potential backmerge.
367 __rq
= elv_rqhash_find(q
, blk_rq_pos(rq
));
368 if (!__rq
|| !blk_attempt_req_merge(q
, __rq
, rq
))
371 /* The merged request could be merged with others, try again */
379 void elv_merged_request(struct request_queue
*q
, struct request
*rq
,
382 struct elevator_queue
*e
= q
->elevator
;
384 if (e
->type
->ops
.request_merged
)
385 e
->type
->ops
.request_merged(q
, rq
, type
);
387 if (type
== ELEVATOR_BACK_MERGE
)
388 elv_rqhash_reposition(q
, rq
);
393 void elv_merge_requests(struct request_queue
*q
, struct request
*rq
,
394 struct request
*next
)
396 struct elevator_queue
*e
= q
->elevator
;
398 if (e
->type
->ops
.requests_merged
)
399 e
->type
->ops
.requests_merged(q
, rq
, next
);
401 elv_rqhash_reposition(q
, rq
);
405 struct request
*elv_latter_request(struct request_queue
*q
, struct request
*rq
)
407 struct elevator_queue
*e
= q
->elevator
;
409 if (e
->type
->ops
.next_request
)
410 return e
->type
->ops
.next_request(q
, rq
);
415 struct request
*elv_former_request(struct request_queue
*q
, struct request
*rq
)
417 struct elevator_queue
*e
= q
->elevator
;
419 if (e
->type
->ops
.former_request
)
420 return e
->type
->ops
.former_request(q
, rq
);
425 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
428 elv_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
430 struct elv_fs_entry
*entry
= to_elv(attr
);
431 struct elevator_queue
*e
;
437 e
= container_of(kobj
, struct elevator_queue
, kobj
);
438 mutex_lock(&e
->sysfs_lock
);
439 error
= e
->type
? entry
->show(e
, page
) : -ENOENT
;
440 mutex_unlock(&e
->sysfs_lock
);
445 elv_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
446 const char *page
, size_t length
)
448 struct elv_fs_entry
*entry
= to_elv(attr
);
449 struct elevator_queue
*e
;
455 e
= container_of(kobj
, struct elevator_queue
, kobj
);
456 mutex_lock(&e
->sysfs_lock
);
457 error
= e
->type
? entry
->store(e
, page
, length
) : -ENOENT
;
458 mutex_unlock(&e
->sysfs_lock
);
462 static const struct sysfs_ops elv_sysfs_ops
= {
463 .show
= elv_attr_show
,
464 .store
= elv_attr_store
,
467 static struct kobj_type elv_ktype
= {
468 .sysfs_ops
= &elv_sysfs_ops
,
469 .release
= elevator_release
,
472 int elv_register_queue(struct request_queue
*q
)
474 struct elevator_queue
*e
= q
->elevator
;
477 lockdep_assert_held(&q
->sysfs_lock
);
479 error
= kobject_add(&e
->kobj
, &q
->kobj
, "%s", "iosched");
481 struct elv_fs_entry
*attr
= e
->type
->elevator_attrs
;
483 while (attr
->attr
.name
) {
484 if (sysfs_create_file(&e
->kobj
, &attr
->attr
))
489 kobject_uevent(&e
->kobj
, KOBJ_ADD
);
495 void elv_unregister_queue(struct request_queue
*q
)
497 lockdep_assert_held(&q
->sysfs_lock
);
500 struct elevator_queue
*e
= q
->elevator
;
502 kobject_uevent(&e
->kobj
, KOBJ_REMOVE
);
503 kobject_del(&e
->kobj
);
505 /* Re-enable throttling in case elevator disabled it */
506 wbt_enable_default(q
);
510 int elv_register(struct elevator_type
*e
)
514 /* create icq_cache if requested */
516 if (WARN_ON(e
->icq_size
< sizeof(struct io_cq
)) ||
517 WARN_ON(e
->icq_align
< __alignof__(struct io_cq
)))
520 snprintf(e
->icq_cache_name
, sizeof(e
->icq_cache_name
),
521 "%s_io_cq", e
->elevator_name
);
522 e
->icq_cache
= kmem_cache_create(e
->icq_cache_name
, e
->icq_size
,
523 e
->icq_align
, 0, NULL
);
528 /* register, don't allow duplicate names */
529 spin_lock(&elv_list_lock
);
530 if (elevator_find(e
->elevator_name
)) {
531 spin_unlock(&elv_list_lock
);
532 kmem_cache_destroy(e
->icq_cache
);
535 list_add_tail(&e
->list
, &elv_list
);
536 spin_unlock(&elv_list_lock
);
538 printk(KERN_INFO
"io scheduler %s registered%s\n", e
->elevator_name
,
542 EXPORT_SYMBOL_GPL(elv_register
);
544 void elv_unregister(struct elevator_type
*e
)
547 spin_lock(&elv_list_lock
);
548 list_del_init(&e
->list
);
549 spin_unlock(&elv_list_lock
);
552 * Destroy icq_cache if it exists. icq's are RCU managed. Make
553 * sure all RCU operations are complete before proceeding.
557 kmem_cache_destroy(e
->icq_cache
);
561 EXPORT_SYMBOL_GPL(elv_unregister
);
563 int elevator_switch_mq(struct request_queue
*q
,
564 struct elevator_type
*new_e
)
568 lockdep_assert_held(&q
->sysfs_lock
);
571 if (q
->elevator
->registered
)
572 elv_unregister_queue(q
);
574 elevator_exit(q
, q
->elevator
);
577 ret
= blk_mq_init_sched(q
, new_e
);
582 ret
= elv_register_queue(q
);
584 elevator_exit(q
, q
->elevator
);
590 blk_add_trace_msg(q
, "elv switch: %s", new_e
->elevator_name
);
592 blk_add_trace_msg(q
, "elv switch: none");
599 * For blk-mq devices, we default to using mq-deadline, if available, for single
600 * queue devices. If deadline isn't available OR we have multiple queues,
603 int elevator_init_mq(struct request_queue
*q
)
605 struct elevator_type
*e
;
608 if (q
->nr_hw_queues
!= 1)
612 * q->sysfs_lock must be held to provide mutual exclusion between
613 * elevator_switch() and here.
615 mutex_lock(&q
->sysfs_lock
);
616 if (unlikely(q
->elevator
))
619 e
= elevator_get(q
, "mq-deadline", false);
623 err
= blk_mq_init_sched(q
, e
);
627 mutex_unlock(&q
->sysfs_lock
);
633 * switch to new_e io scheduler. be careful not to introduce deadlocks -
634 * we don't free the old io scheduler, before we have allocated what we
635 * need for the new one. this way we have a chance of going back to the old
636 * one, if the new one fails init for some reason.
638 static int elevator_switch(struct request_queue
*q
, struct elevator_type
*new_e
)
642 lockdep_assert_held(&q
->sysfs_lock
);
644 blk_mq_freeze_queue(q
);
645 blk_mq_quiesce_queue(q
);
647 err
= elevator_switch_mq(q
, new_e
);
649 blk_mq_unquiesce_queue(q
);
650 blk_mq_unfreeze_queue(q
);
656 * Switch this queue to the given IO scheduler.
658 static int __elevator_change(struct request_queue
*q
, const char *name
)
660 char elevator_name
[ELV_NAME_MAX
];
661 struct elevator_type
*e
;
663 /* Make sure queue is not in the middle of being removed */
664 if (!test_bit(QUEUE_FLAG_REGISTERED
, &q
->queue_flags
))
668 * Special case for mq, turn off scheduling
670 if (!strncmp(name
, "none", 4)) {
673 return elevator_switch(q
, NULL
);
676 strlcpy(elevator_name
, name
, sizeof(elevator_name
));
677 e
= elevator_get(q
, strstrip(elevator_name
), true);
681 if (q
->elevator
&& elevator_match(q
->elevator
->type
, elevator_name
)) {
686 return elevator_switch(q
, e
);
689 static inline bool elv_support_iosched(struct request_queue
*q
)
691 if (q
->tag_set
&& (q
->tag_set
->flags
& BLK_MQ_F_NO_SCHED
))
696 ssize_t
elv_iosched_store(struct request_queue
*q
, const char *name
,
701 if (!queue_is_mq(q
) || !elv_support_iosched(q
))
704 ret
= __elevator_change(q
, name
);
711 ssize_t
elv_iosched_show(struct request_queue
*q
, char *name
)
713 struct elevator_queue
*e
= q
->elevator
;
714 struct elevator_type
*elv
= NULL
;
715 struct elevator_type
*__e
;
719 return sprintf(name
, "none\n");
722 len
+= sprintf(name
+len
, "[none] ");
726 spin_lock(&elv_list_lock
);
727 list_for_each_entry(__e
, &elv_list
, list
) {
728 if (elv
&& elevator_match(elv
, __e
->elevator_name
)) {
729 len
+= sprintf(name
+len
, "[%s] ", elv
->elevator_name
);
732 if (elv_support_iosched(q
))
733 len
+= sprintf(name
+len
, "%s ", __e
->elevator_name
);
735 spin_unlock(&elv_list_lock
);
738 len
+= sprintf(name
+len
, "none");
740 len
+= sprintf(len
+name
, "\n");
744 struct request
*elv_rb_former_request(struct request_queue
*q
,
747 struct rb_node
*rbprev
= rb_prev(&rq
->rb_node
);
750 return rb_entry_rq(rbprev
);
754 EXPORT_SYMBOL(elv_rb_former_request
);
756 struct request
*elv_rb_latter_request(struct request_queue
*q
,
759 struct rb_node
*rbnext
= rb_next(&rq
->rb_node
);
762 return rb_entry_rq(rbnext
);
766 EXPORT_SYMBOL(elv_rb_latter_request
);