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/elevator.h>
30 #include <linux/bio.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/compiler.h>
35 #include <linux/blktrace_api.h>
36 #include <linux/hash.h>
37 #include <linux/uaccess.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/blk-cgroup.h>
41 #include <trace/events/block.h>
44 #include "blk-mq-sched.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 int 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);
86 static bool elevator_match(const struct elevator_type *e, const char *name)
88 if (!strcmp(e->elevator_name, name))
90 if (e->elevator_alias && !strcmp(e->elevator_alias, name))
97 * Return scheduler with name '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))
111 static void elevator_put(struct elevator_type *e)
113 module_put(e->elevator_owner);
116 static struct elevator_type *elevator_get(struct request_queue *q,
117 const char *name, bool try_loading)
119 struct elevator_type *e;
121 spin_lock(&elv_list_lock);
123 e = elevator_find(name);
124 if (!e && try_loading) {
125 spin_unlock(&elv_list_lock);
126 request_module("%s-iosched", name);
127 spin_lock(&elv_list_lock);
128 e = elevator_find(name);
131 if (e && !try_module_get(e->elevator_owner))
134 spin_unlock(&elv_list_lock);
138 static char chosen_elevator[ELV_NAME_MAX];
140 static int __init elevator_setup(char *str)
143 * Be backwards-compatible with previous kernels, so users
144 * won't get the wrong elevator.
146 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
150 __setup("elevator=", elevator_setup);
152 static struct kobj_type elv_ktype;
154 struct elevator_queue *elevator_alloc(struct request_queue *q,
155 struct elevator_type *e)
157 struct elevator_queue *eq;
159 eq = kzalloc_node(sizeof(*eq), GFP_KERNEL, q->node);
164 kobject_init(&eq->kobj, &elv_ktype);
165 mutex_init(&eq->sysfs_lock);
170 EXPORT_SYMBOL(elevator_alloc);
172 static void elevator_release(struct kobject *kobj)
174 struct elevator_queue *e;
176 e = container_of(kobj, struct elevator_queue, kobj);
177 elevator_put(e->type);
181 void elevator_exit(struct request_queue *q, struct elevator_queue *e)
183 mutex_lock(&e->sysfs_lock);
184 if (e->type->ops.exit_sched)
185 blk_mq_exit_sched(q, e);
186 mutex_unlock(&e->sysfs_lock);
188 kobject_put(&e->kobj);
191 static inline void __elv_rqhash_del(struct request *rq)
194 rq->rq_flags &= ~RQF_HASHED;
197 void elv_rqhash_del(struct request_queue *q, struct request *rq)
200 __elv_rqhash_del(rq);
202 EXPORT_SYMBOL_GPL(elv_rqhash_del);
204 void elv_rqhash_add(struct request_queue *q, struct request *rq)
206 struct elevator_queue *e = q->elevator;
208 BUG_ON(ELV_ON_HASH(rq));
209 hash_add(e->hash, &rq->hash, rq_hash_key(rq));
210 rq->rq_flags |= RQF_HASHED;
212 EXPORT_SYMBOL_GPL(elv_rqhash_add);
214 void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
216 __elv_rqhash_del(rq);
217 elv_rqhash_add(q, rq);
220 struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
222 struct elevator_queue *e = q->elevator;
223 struct hlist_node *next;
226 hash_for_each_possible_safe(e->hash, rq, next, hash, offset) {
227 BUG_ON(!ELV_ON_HASH(rq));
229 if (unlikely(!rq_mergeable(rq))) {
230 __elv_rqhash_del(rq);
234 if (rq_hash_key(rq) == offset)
242 * RB-tree support functions for inserting/lookup/removal of requests
243 * in a sorted RB tree.
245 void elv_rb_add(struct rb_root *root, struct request *rq)
247 struct rb_node **p = &root->rb_node;
248 struct rb_node *parent = NULL;
249 struct request *__rq;
253 __rq = rb_entry(parent, struct request, rb_node);
255 if (blk_rq_pos(rq) < blk_rq_pos(__rq))
257 else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))
261 rb_link_node(&rq->rb_node, parent, p);
262 rb_insert_color(&rq->rb_node, root);
264 EXPORT_SYMBOL(elv_rb_add);
266 void elv_rb_del(struct rb_root *root, struct request *rq)
268 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
269 rb_erase(&rq->rb_node, root);
270 RB_CLEAR_NODE(&rq->rb_node);
272 EXPORT_SYMBOL(elv_rb_del);
274 struct request *elv_rb_find(struct rb_root *root, sector_t sector)
276 struct rb_node *n = root->rb_node;
280 rq = rb_entry(n, struct request, rb_node);
282 if (sector < blk_rq_pos(rq))
284 else if (sector > blk_rq_pos(rq))
292 EXPORT_SYMBOL(elv_rb_find);
294 enum elv_merge elv_merge(struct request_queue *q, struct request **req,
297 struct elevator_queue *e = q->elevator;
298 struct request *__rq;
302 * nomerges: No merges at all attempted
303 * noxmerges: Only simple one-hit cache try
304 * merges: All merge tries attempted
306 if (blk_queue_nomerges(q) || !bio_mergeable(bio))
307 return ELEVATOR_NO_MERGE;
310 * First try one-hit cache.
312 if (q->last_merge && elv_bio_merge_ok(q->last_merge, bio)) {
313 enum elv_merge ret = blk_try_merge(q->last_merge, bio);
315 if (ret != ELEVATOR_NO_MERGE) {
316 *req = q->last_merge;
321 if (blk_queue_noxmerges(q))
322 return ELEVATOR_NO_MERGE;
325 * See if our hash lookup can find a potential backmerge.
327 __rq = elv_rqhash_find(q, bio->bi_iter.bi_sector);
328 if (__rq && elv_bio_merge_ok(__rq, bio)) {
330 return ELEVATOR_BACK_MERGE;
333 if (e->type->ops.request_merge)
334 return e->type->ops.request_merge(q, req, bio);
336 return ELEVATOR_NO_MERGE;
340 * Attempt to do an insertion back merge. Only check for the case where
341 * we can append 'rq' to an existing request, so we can throw 'rq' away
344 * Returns true if we merged, false otherwise
346 bool elv_attempt_insert_merge(struct request_queue *q, struct request *rq)
348 struct request *__rq;
351 if (blk_queue_nomerges(q))
355 * First try one-hit cache.
357 if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq))
360 if (blk_queue_noxmerges(q))
365 * See if our hash lookup can find a potential backmerge.
368 __rq = elv_rqhash_find(q, blk_rq_pos(rq));
369 if (!__rq || !blk_attempt_req_merge(q, __rq, rq))
372 /* The merged request could be merged with others, try again */
380 void elv_merged_request(struct request_queue *q, struct request *rq,
383 struct elevator_queue *e = q->elevator;
385 if (e->type->ops.request_merged)
386 e->type->ops.request_merged(q, rq, type);
388 if (type == ELEVATOR_BACK_MERGE)
389 elv_rqhash_reposition(q, rq);
394 void elv_merge_requests(struct request_queue *q, struct request *rq,
395 struct request *next)
397 struct elevator_queue *e = q->elevator;
399 if (e->type->ops.requests_merged)
400 e->type->ops.requests_merged(q, rq, next);
402 elv_rqhash_reposition(q, rq);
406 struct request *elv_latter_request(struct request_queue *q, struct request *rq)
408 struct elevator_queue *e = q->elevator;
410 if (e->type->ops.next_request)
411 return e->type->ops.next_request(q, rq);
416 struct request *elv_former_request(struct request_queue *q, struct request *rq)
418 struct elevator_queue *e = q->elevator;
420 if (e->type->ops.former_request)
421 return e->type->ops.former_request(q, rq);
426 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
429 elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
431 struct elv_fs_entry *entry = to_elv(attr);
432 struct elevator_queue *e;
438 e = container_of(kobj, struct elevator_queue, kobj);
439 mutex_lock(&e->sysfs_lock);
440 error = e->type ? entry->show(e, page) : -ENOENT;
441 mutex_unlock(&e->sysfs_lock);
446 elv_attr_store(struct kobject *kobj, struct attribute *attr,
447 const char *page, size_t length)
449 struct elv_fs_entry *entry = to_elv(attr);
450 struct elevator_queue *e;
456 e = container_of(kobj, struct elevator_queue, kobj);
457 mutex_lock(&e->sysfs_lock);
458 error = e->type ? entry->store(e, page, length) : -ENOENT;
459 mutex_unlock(&e->sysfs_lock);
463 static const struct sysfs_ops elv_sysfs_ops = {
464 .show = elv_attr_show,
465 .store = elv_attr_store,
468 static struct kobj_type elv_ktype = {
469 .sysfs_ops = &elv_sysfs_ops,
470 .release = elevator_release,
473 int elv_register_queue(struct request_queue *q)
475 struct elevator_queue *e = q->elevator;
478 lockdep_assert_held(&q->sysfs_lock);
480 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
482 struct elv_fs_entry *attr = e->type->elevator_attrs;
484 while (attr->attr.name) {
485 if (sysfs_create_file(&e->kobj, &attr->attr))
490 kobject_uevent(&e->kobj, KOBJ_ADD);
496 void elv_unregister_queue(struct request_queue *q)
498 lockdep_assert_held(&q->sysfs_lock);
501 struct elevator_queue *e = q->elevator;
503 kobject_uevent(&e->kobj, KOBJ_REMOVE);
504 kobject_del(&e->kobj);
506 /* Re-enable throttling in case elevator disabled it */
507 wbt_enable_default(q);
511 int elv_register(struct elevator_type *e)
513 /* create icq_cache if requested */
515 if (WARN_ON(e->icq_size < sizeof(struct io_cq)) ||
516 WARN_ON(e->icq_align < __alignof__(struct io_cq)))
519 snprintf(e->icq_cache_name, sizeof(e->icq_cache_name),
520 "%s_io_cq", e->elevator_name);
521 e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size,
522 e->icq_align, 0, NULL);
527 /* register, don't allow duplicate names */
528 spin_lock(&elv_list_lock);
529 if (elevator_find(e->elevator_name)) {
530 spin_unlock(&elv_list_lock);
531 kmem_cache_destroy(e->icq_cache);
534 list_add_tail(&e->list, &elv_list);
535 spin_unlock(&elv_list_lock);
537 printk(KERN_INFO "io scheduler %s registered\n", e->elevator_name);
541 EXPORT_SYMBOL_GPL(elv_register);
543 void elv_unregister(struct elevator_type *e)
546 spin_lock(&elv_list_lock);
547 list_del_init(&e->list);
548 spin_unlock(&elv_list_lock);
551 * Destroy icq_cache if it exists. icq's are RCU managed. Make
552 * sure all RCU operations are complete before proceeding.
556 kmem_cache_destroy(e->icq_cache);
560 EXPORT_SYMBOL_GPL(elv_unregister);
562 int elevator_switch_mq(struct request_queue *q,
563 struct elevator_type *new_e)
567 lockdep_assert_held(&q->sysfs_lock);
570 if (q->elevator->registered)
571 elv_unregister_queue(q);
573 elevator_exit(q, q->elevator);
576 ret = blk_mq_init_sched(q, new_e);
581 ret = elv_register_queue(q);
583 elevator_exit(q, q->elevator);
589 blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name);
591 blk_add_trace_msg(q, "elv switch: none");
598 * For blk-mq devices, we default to using mq-deadline, if available, for single
599 * queue devices. If deadline isn't available OR we have multiple queues,
602 int elevator_init_mq(struct request_queue *q)
604 struct elevator_type *e;
607 if (q->nr_hw_queues != 1)
611 * q->sysfs_lock must be held to provide mutual exclusion between
612 * elevator_switch() and here.
614 mutex_lock(&q->sysfs_lock);
615 if (unlikely(q->elevator))
618 e = elevator_get(q, "mq-deadline", false);
622 err = blk_mq_init_sched(q, e);
626 mutex_unlock(&q->sysfs_lock);
632 * switch to new_e io scheduler. be careful not to introduce deadlocks -
633 * we don't free the old io scheduler, before we have allocated what we
634 * need for the new one. this way we have a chance of going back to the old
635 * one, if the new one fails init for some reason.
637 static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
641 lockdep_assert_held(&q->sysfs_lock);
643 blk_mq_freeze_queue(q);
644 blk_mq_quiesce_queue(q);
646 err = elevator_switch_mq(q, new_e);
648 blk_mq_unquiesce_queue(q);
649 blk_mq_unfreeze_queue(q);
655 * Switch this queue to the given IO scheduler.
657 static int __elevator_change(struct request_queue *q, const char *name)
659 char elevator_name[ELV_NAME_MAX];
660 struct elevator_type *e;
662 /* Make sure queue is not in the middle of being removed */
663 if (!test_bit(QUEUE_FLAG_REGISTERED, &q->queue_flags))
667 * Special case for mq, turn off scheduling
669 if (!strncmp(name, "none", 4)) {
672 return elevator_switch(q, NULL);
675 strlcpy(elevator_name, name, sizeof(elevator_name));
676 e = elevator_get(q, strstrip(elevator_name), true);
680 if (q->elevator && elevator_match(q->elevator->type, elevator_name)) {
685 return elevator_switch(q, e);
688 static inline bool elv_support_iosched(struct request_queue *q)
690 if (q->tag_set && (q->tag_set->flags & BLK_MQ_F_NO_SCHED))
695 ssize_t elv_iosched_store(struct request_queue *q, const char *name,
700 if (!queue_is_mq(q) || !elv_support_iosched(q))
703 ret = __elevator_change(q, name);
710 ssize_t elv_iosched_show(struct request_queue *q, char *name)
712 struct elevator_queue *e = q->elevator;
713 struct elevator_type *elv = NULL;
714 struct elevator_type *__e;
718 return sprintf(name, "none\n");
721 len += sprintf(name+len, "[none] ");
725 spin_lock(&elv_list_lock);
726 list_for_each_entry(__e, &elv_list, list) {
727 if (elv && elevator_match(elv, __e->elevator_name)) {
728 len += sprintf(name+len, "[%s] ", elv->elevator_name);
731 if (elv_support_iosched(q))
732 len += sprintf(name+len, "%s ", __e->elevator_name);
734 spin_unlock(&elv_list_lock);
737 len += sprintf(name+len, "none");
739 len += sprintf(len+name, "\n");
743 struct request *elv_rb_former_request(struct request_queue *q,
746 struct rb_node *rbprev = rb_prev(&rq->rb_node);
749 return rb_entry_rq(rbprev);
753 EXPORT_SYMBOL(elv_rb_former_request);
755 struct request *elv_rb_latter_request(struct request_queue *q,
758 struct rb_node *rbnext = rb_next(&rq->rb_node);
761 return rb_entry_rq(rbnext);
765 EXPORT_SYMBOL(elv_rb_latter_request);