1 // SPDX-License-Identifier: GPL-2.0
3 * Block device elevator/IO-scheduler.
5 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
7 * 30042000 Jens Axboe <axboe@kernel.dk> :
9 * Split the elevator a bit so that it is possible to choose a different
10 * one or even write a new "plug in". There are three pieces:
11 * - elevator_fn, inserts a new request in the queue list
12 * - elevator_merge_fn, decides whether a new buffer can be merged with
14 * - elevator_dequeue_fn, called when a request is taken off the active list
16 * 20082000 Dave Jones <davej@suse.de> :
17 * Removed tests for max-bomb-segments, which was breaking elvtune
18 * when run without -bN
21 * - Rework again to work with bio instead of buffer_heads
22 * - loose bi_dev comparisons, partition handling is right now
23 * - completely modularize elevator setup and teardown
26 #include <linux/kernel.h>
28 #include <linux/blkdev.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>
39 #include <trace/events/block.h>
43 #include "blk-mq-sched.h"
46 #include "blk-cgroup.h"
48 static DEFINE_SPINLOCK(elv_list_lock
);
49 static LIST_HEAD(elv_list
);
54 #define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
57 * Query io scheduler to see if the current process issuing bio may be
60 static bool elv_iosched_allow_bio_merge(struct request
*rq
, struct bio
*bio
)
62 struct request_queue
*q
= rq
->q
;
63 struct elevator_queue
*e
= q
->elevator
;
65 if (e
->type
->ops
.allow_merge
)
66 return e
->type
->ops
.allow_merge(q
, rq
, bio
);
72 * can we safely merge with this request?
74 bool elv_bio_merge_ok(struct request
*rq
, struct bio
*bio
)
76 if (!blk_rq_merge_ok(rq
, bio
))
79 if (!elv_iosched_allow_bio_merge(rq
, bio
))
84 EXPORT_SYMBOL(elv_bio_merge_ok
);
87 * elevator_match - Check whether @e's name or alias matches @name
88 * @e: Scheduler to test
89 * @name: Elevator name to test
91 * Return true if the elevator @e's name or alias matches @name.
93 static bool elevator_match(const struct elevator_type
*e
, const char *name
)
95 return !strcmp(e
->elevator_name
, name
) ||
96 (e
->elevator_alias
&& !strcmp(e
->elevator_alias
, name
));
99 static struct elevator_type
*__elevator_find(const char *name
)
101 struct elevator_type
*e
;
103 list_for_each_entry(e
, &elv_list
, list
)
104 if (elevator_match(e
, name
))
109 static struct elevator_type
*elevator_find_get(struct request_queue
*q
,
112 struct elevator_type
*e
;
114 spin_lock(&elv_list_lock
);
115 e
= __elevator_find(name
);
116 if (e
&& (!elevator_tryget(e
)))
118 spin_unlock(&elv_list_lock
);
122 static const struct kobj_type elv_ktype
;
124 struct elevator_queue
*elevator_alloc(struct request_queue
*q
,
125 struct elevator_type
*e
)
127 struct elevator_queue
*eq
;
129 eq
= kzalloc_node(sizeof(*eq
), GFP_KERNEL
, q
->node
);
135 kobject_init(&eq
->kobj
, &elv_ktype
);
136 mutex_init(&eq
->sysfs_lock
);
141 EXPORT_SYMBOL(elevator_alloc
);
143 static void elevator_release(struct kobject
*kobj
)
145 struct elevator_queue
*e
;
147 e
= container_of(kobj
, struct elevator_queue
, kobj
);
148 elevator_put(e
->type
);
152 void elevator_exit(struct request_queue
*q
)
154 struct elevator_queue
*e
= q
->elevator
;
157 blk_mq_sched_free_rqs(q
);
159 mutex_lock(&e
->sysfs_lock
);
160 blk_mq_exit_sched(q
, e
);
161 mutex_unlock(&e
->sysfs_lock
);
163 kobject_put(&e
->kobj
);
166 static inline void __elv_rqhash_del(struct request
*rq
)
169 rq
->rq_flags
&= ~RQF_HASHED
;
172 void elv_rqhash_del(struct request_queue
*q
, struct request
*rq
)
175 __elv_rqhash_del(rq
);
177 EXPORT_SYMBOL_GPL(elv_rqhash_del
);
179 void elv_rqhash_add(struct request_queue
*q
, struct request
*rq
)
181 struct elevator_queue
*e
= q
->elevator
;
183 BUG_ON(ELV_ON_HASH(rq
));
184 hash_add(e
->hash
, &rq
->hash
, rq_hash_key(rq
));
185 rq
->rq_flags
|= RQF_HASHED
;
187 EXPORT_SYMBOL_GPL(elv_rqhash_add
);
189 void elv_rqhash_reposition(struct request_queue
*q
, struct request
*rq
)
191 __elv_rqhash_del(rq
);
192 elv_rqhash_add(q
, rq
);
195 struct request
*elv_rqhash_find(struct request_queue
*q
, sector_t offset
)
197 struct elevator_queue
*e
= q
->elevator
;
198 struct hlist_node
*next
;
201 hash_for_each_possible_safe(e
->hash
, rq
, next
, hash
, offset
) {
202 BUG_ON(!ELV_ON_HASH(rq
));
204 if (unlikely(!rq_mergeable(rq
))) {
205 __elv_rqhash_del(rq
);
209 if (rq_hash_key(rq
) == offset
)
217 * RB-tree support functions for inserting/lookup/removal of requests
218 * in a sorted RB tree.
220 void elv_rb_add(struct rb_root
*root
, struct request
*rq
)
222 struct rb_node
**p
= &root
->rb_node
;
223 struct rb_node
*parent
= NULL
;
224 struct request
*__rq
;
228 __rq
= rb_entry(parent
, struct request
, rb_node
);
230 if (blk_rq_pos(rq
) < blk_rq_pos(__rq
))
232 else if (blk_rq_pos(rq
) >= blk_rq_pos(__rq
))
236 rb_link_node(&rq
->rb_node
, parent
, p
);
237 rb_insert_color(&rq
->rb_node
, root
);
239 EXPORT_SYMBOL(elv_rb_add
);
241 void elv_rb_del(struct rb_root
*root
, struct request
*rq
)
243 BUG_ON(RB_EMPTY_NODE(&rq
->rb_node
));
244 rb_erase(&rq
->rb_node
, root
);
245 RB_CLEAR_NODE(&rq
->rb_node
);
247 EXPORT_SYMBOL(elv_rb_del
);
249 struct request
*elv_rb_find(struct rb_root
*root
, sector_t sector
)
251 struct rb_node
*n
= root
->rb_node
;
255 rq
= rb_entry(n
, struct request
, rb_node
);
257 if (sector
< blk_rq_pos(rq
))
259 else if (sector
> blk_rq_pos(rq
))
267 EXPORT_SYMBOL(elv_rb_find
);
269 enum elv_merge
elv_merge(struct request_queue
*q
, struct request
**req
,
272 struct elevator_queue
*e
= q
->elevator
;
273 struct request
*__rq
;
277 * nomerges: No merges at all attempted
278 * noxmerges: Only simple one-hit cache try
279 * merges: All merge tries attempted
281 if (blk_queue_nomerges(q
) || !bio_mergeable(bio
))
282 return ELEVATOR_NO_MERGE
;
285 * First try one-hit cache.
287 if (q
->last_merge
&& elv_bio_merge_ok(q
->last_merge
, bio
)) {
288 enum elv_merge ret
= blk_try_merge(q
->last_merge
, bio
);
290 if (ret
!= ELEVATOR_NO_MERGE
) {
291 *req
= q
->last_merge
;
296 if (blk_queue_noxmerges(q
))
297 return ELEVATOR_NO_MERGE
;
300 * See if our hash lookup can find a potential backmerge.
302 __rq
= elv_rqhash_find(q
, bio
->bi_iter
.bi_sector
);
303 if (__rq
&& elv_bio_merge_ok(__rq
, bio
)) {
306 if (blk_discard_mergable(__rq
))
307 return ELEVATOR_DISCARD_MERGE
;
308 return ELEVATOR_BACK_MERGE
;
311 if (e
->type
->ops
.request_merge
)
312 return e
->type
->ops
.request_merge(q
, req
, bio
);
314 return ELEVATOR_NO_MERGE
;
318 * Attempt to do an insertion back merge. Only check for the case where
319 * we can append 'rq' to an existing request, so we can throw 'rq' away
322 * Returns true if we merged, false otherwise. 'free' will contain all
323 * requests that need to be freed.
325 bool elv_attempt_insert_merge(struct request_queue
*q
, struct request
*rq
,
326 struct list_head
*free
)
328 struct request
*__rq
;
331 if (blk_queue_nomerges(q
))
335 * First try one-hit cache.
337 if (q
->last_merge
&& blk_attempt_req_merge(q
, q
->last_merge
, rq
)) {
338 list_add(&rq
->queuelist
, free
);
342 if (blk_queue_noxmerges(q
))
347 * See if our hash lookup can find a potential backmerge.
350 __rq
= elv_rqhash_find(q
, blk_rq_pos(rq
));
351 if (!__rq
|| !blk_attempt_req_merge(q
, __rq
, rq
))
354 list_add(&rq
->queuelist
, free
);
355 /* The merged request could be merged with others, try again */
363 void elv_merged_request(struct request_queue
*q
, struct request
*rq
,
366 struct elevator_queue
*e
= q
->elevator
;
368 if (e
->type
->ops
.request_merged
)
369 e
->type
->ops
.request_merged(q
, rq
, type
);
371 if (type
== ELEVATOR_BACK_MERGE
)
372 elv_rqhash_reposition(q
, rq
);
377 void elv_merge_requests(struct request_queue
*q
, struct request
*rq
,
378 struct request
*next
)
380 struct elevator_queue
*e
= q
->elevator
;
382 if (e
->type
->ops
.requests_merged
)
383 e
->type
->ops
.requests_merged(q
, rq
, next
);
385 elv_rqhash_reposition(q
, rq
);
389 struct request
*elv_latter_request(struct request_queue
*q
, struct request
*rq
)
391 struct elevator_queue
*e
= q
->elevator
;
393 if (e
->type
->ops
.next_request
)
394 return e
->type
->ops
.next_request(q
, rq
);
399 struct request
*elv_former_request(struct request_queue
*q
, struct request
*rq
)
401 struct elevator_queue
*e
= q
->elevator
;
403 if (e
->type
->ops
.former_request
)
404 return e
->type
->ops
.former_request(q
, rq
);
409 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
412 elv_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
414 struct elv_fs_entry
*entry
= to_elv(attr
);
415 struct elevator_queue
*e
;
421 e
= container_of(kobj
, struct elevator_queue
, kobj
);
422 mutex_lock(&e
->sysfs_lock
);
423 error
= e
->type
? entry
->show(e
, page
) : -ENOENT
;
424 mutex_unlock(&e
->sysfs_lock
);
429 elv_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
430 const char *page
, size_t length
)
432 struct elv_fs_entry
*entry
= to_elv(attr
);
433 struct elevator_queue
*e
;
439 e
= container_of(kobj
, struct elevator_queue
, kobj
);
440 mutex_lock(&e
->sysfs_lock
);
441 error
= e
->type
? entry
->store(e
, page
, length
) : -ENOENT
;
442 mutex_unlock(&e
->sysfs_lock
);
446 static const struct sysfs_ops elv_sysfs_ops
= {
447 .show
= elv_attr_show
,
448 .store
= elv_attr_store
,
451 static const struct kobj_type elv_ktype
= {
452 .sysfs_ops
= &elv_sysfs_ops
,
453 .release
= elevator_release
,
456 int elv_register_queue(struct request_queue
*q
, bool uevent
)
458 struct elevator_queue
*e
= q
->elevator
;
461 lockdep_assert_held(&q
->sysfs_lock
);
463 error
= kobject_add(&e
->kobj
, &q
->disk
->queue_kobj
, "iosched");
465 struct elv_fs_entry
*attr
= e
->type
->elevator_attrs
;
467 while (attr
->attr
.name
) {
468 if (sysfs_create_file(&e
->kobj
, &attr
->attr
))
474 kobject_uevent(&e
->kobj
, KOBJ_ADD
);
476 set_bit(ELEVATOR_FLAG_REGISTERED
, &e
->flags
);
481 void elv_unregister_queue(struct request_queue
*q
)
483 struct elevator_queue
*e
= q
->elevator
;
485 lockdep_assert_held(&q
->sysfs_lock
);
487 if (e
&& test_and_clear_bit(ELEVATOR_FLAG_REGISTERED
, &e
->flags
)) {
488 kobject_uevent(&e
->kobj
, KOBJ_REMOVE
);
489 kobject_del(&e
->kobj
);
493 int elv_register(struct elevator_type
*e
)
495 /* finish request is mandatory */
496 if (WARN_ON_ONCE(!e
->ops
.finish_request
))
498 /* insert_requests and dispatch_request are mandatory */
499 if (WARN_ON_ONCE(!e
->ops
.insert_requests
|| !e
->ops
.dispatch_request
))
502 /* create icq_cache if requested */
504 if (WARN_ON(e
->icq_size
< sizeof(struct io_cq
)) ||
505 WARN_ON(e
->icq_align
< __alignof__(struct io_cq
)))
508 snprintf(e
->icq_cache_name
, sizeof(e
->icq_cache_name
),
509 "%s_io_cq", e
->elevator_name
);
510 e
->icq_cache
= kmem_cache_create(e
->icq_cache_name
, e
->icq_size
,
511 e
->icq_align
, 0, NULL
);
516 /* register, don't allow duplicate names */
517 spin_lock(&elv_list_lock
);
518 if (__elevator_find(e
->elevator_name
)) {
519 spin_unlock(&elv_list_lock
);
520 kmem_cache_destroy(e
->icq_cache
);
523 list_add_tail(&e
->list
, &elv_list
);
524 spin_unlock(&elv_list_lock
);
526 printk(KERN_INFO
"io scheduler %s registered\n", e
->elevator_name
);
530 EXPORT_SYMBOL_GPL(elv_register
);
532 void elv_unregister(struct elevator_type
*e
)
535 spin_lock(&elv_list_lock
);
536 list_del_init(&e
->list
);
537 spin_unlock(&elv_list_lock
);
540 * Destroy icq_cache if it exists. icq's are RCU managed. Make
541 * sure all RCU operations are complete before proceeding.
545 kmem_cache_destroy(e
->icq_cache
);
549 EXPORT_SYMBOL_GPL(elv_unregister
);
551 static inline bool elv_support_iosched(struct request_queue
*q
)
553 if (!queue_is_mq(q
) ||
554 (q
->tag_set
&& (q
->tag_set
->flags
& BLK_MQ_F_NO_SCHED
)))
560 * For single queue devices, default to using mq-deadline. If we have multiple
561 * queues or mq-deadline is not available, default to "none".
563 static struct elevator_type
*elevator_get_default(struct request_queue
*q
)
565 if (q
->tag_set
&& q
->tag_set
->flags
& BLK_MQ_F_NO_SCHED_BY_DEFAULT
)
568 if (q
->nr_hw_queues
!= 1 &&
569 !blk_mq_is_shared_tags(q
->tag_set
->flags
))
572 return elevator_find_get(q
, "mq-deadline");
576 * Use the default elevator settings. If the chosen elevator initialization
577 * fails, fall back to the "none" elevator (no elevator).
579 void elevator_init_mq(struct request_queue
*q
)
581 struct elevator_type
*e
;
584 if (!elv_support_iosched(q
))
587 WARN_ON_ONCE(blk_queue_registered(q
));
589 if (unlikely(q
->elevator
))
592 e
= elevator_get_default(q
);
597 * We are called before adding disk, when there isn't any FS I/O,
598 * so freezing queue plus canceling dispatch work is enough to
599 * drain any dispatch activities originated from passthrough
600 * requests, then no need to quiesce queue which may add long boot
601 * latency, especially when lots of disks are involved.
603 blk_mq_freeze_queue(q
);
604 blk_mq_cancel_work_sync(q
);
606 err
= blk_mq_init_sched(q
, e
);
608 blk_mq_unfreeze_queue(q
);
611 pr_warn("\"%s\" elevator initialization failed, "
612 "falling back to \"none\"\n", e
->elevator_name
);
619 * Switch to new_e io scheduler.
621 * If switching fails, we are most likely running out of memory and not able
622 * to restore the old io scheduler, so leaving the io scheduler being none.
624 int elevator_switch(struct request_queue
*q
, struct elevator_type
*new_e
)
628 lockdep_assert_held(&q
->sysfs_lock
);
630 blk_mq_freeze_queue(q
);
631 blk_mq_quiesce_queue(q
);
634 elv_unregister_queue(q
);
638 ret
= blk_mq_init_sched(q
, new_e
);
642 ret
= elv_register_queue(q
, true);
647 blk_add_trace_msg(q
, "elv switch: %s", new_e
->elevator_name
);
650 blk_mq_unquiesce_queue(q
);
651 blk_mq_unfreeze_queue(q
);
654 pr_warn("elv: switch to \"%s\" failed, falling back to \"none\"\n",
655 new_e
->elevator_name
);
661 void elevator_disable(struct request_queue
*q
)
663 lockdep_assert_held(&q
->sysfs_lock
);
665 blk_mq_freeze_queue(q
);
666 blk_mq_quiesce_queue(q
);
668 elv_unregister_queue(q
);
670 blk_queue_flag_clear(QUEUE_FLAG_SQ_SCHED
, q
);
672 q
->nr_requests
= q
->tag_set
->queue_depth
;
673 blk_add_trace_msg(q
, "elv switch: none");
675 blk_mq_unquiesce_queue(q
);
676 blk_mq_unfreeze_queue(q
);
680 * Switch this queue to the given IO scheduler.
682 static int elevator_change(struct request_queue
*q
, const char *elevator_name
)
684 struct elevator_type
*e
;
687 /* Make sure queue is not in the middle of being removed */
688 if (!blk_queue_registered(q
))
691 if (!strncmp(elevator_name
, "none", 4)) {
697 if (q
->elevator
&& elevator_match(q
->elevator
->type
, elevator_name
))
700 e
= elevator_find_get(q
, elevator_name
);
702 request_module("%s-iosched", elevator_name
);
703 e
= elevator_find_get(q
, elevator_name
);
707 ret
= elevator_switch(q
, e
);
712 ssize_t
elv_iosched_store(struct gendisk
*disk
, const char *buf
,
715 char elevator_name
[ELV_NAME_MAX
];
718 if (!elv_support_iosched(disk
->queue
))
721 strscpy(elevator_name
, buf
, sizeof(elevator_name
));
722 ret
= elevator_change(disk
->queue
, strstrip(elevator_name
));
728 ssize_t
elv_iosched_show(struct gendisk
*disk
, char *name
)
730 struct request_queue
*q
= disk
->queue
;
731 struct elevator_queue
*eq
= q
->elevator
;
732 struct elevator_type
*cur
= NULL
, *e
;
735 if (!elv_support_iosched(q
))
736 return sprintf(name
, "none\n");
739 len
+= sprintf(name
+len
, "[none] ");
741 len
+= sprintf(name
+len
, "none ");
745 spin_lock(&elv_list_lock
);
746 list_for_each_entry(e
, &elv_list
, list
) {
748 len
+= sprintf(name
+len
, "[%s] ", e
->elevator_name
);
750 len
+= sprintf(name
+len
, "%s ", e
->elevator_name
);
752 spin_unlock(&elv_list_lock
);
754 len
+= sprintf(name
+len
, "\n");
758 struct request
*elv_rb_former_request(struct request_queue
*q
,
761 struct rb_node
*rbprev
= rb_prev(&rq
->rb_node
);
764 return rb_entry_rq(rbprev
);
768 EXPORT_SYMBOL(elv_rb_former_request
);
770 struct request
*elv_rb_latter_request(struct request_queue
*q
,
773 struct rb_node
*rbnext
= rb_next(&rq
->rb_node
);
776 return rb_entry_rq(rbnext
);
780 EXPORT_SYMBOL(elv_rb_latter_request
);
782 static int __init
elevator_setup(char *str
)
784 pr_warn("Kernel parameter elevator= does not have any effect anymore.\n"
785 "Please use sysfs to set IO scheduler for individual devices.\n");
789 __setup("elevator=", elevator_setup
);