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(const char *name
)
111 struct elevator_type
*e
;
113 spin_lock(&elv_list_lock
);
114 e
= __elevator_find(name
);
115 if (e
&& (!elevator_tryget(e
)))
117 spin_unlock(&elv_list_lock
);
121 static const struct kobj_type elv_ktype
;
123 struct elevator_queue
*elevator_alloc(struct request_queue
*q
,
124 struct elevator_type
*e
)
126 struct elevator_queue
*eq
;
128 eq
= kzalloc_node(sizeof(*eq
), GFP_KERNEL
, q
->node
);
134 kobject_init(&eq
->kobj
, &elv_ktype
);
135 mutex_init(&eq
->sysfs_lock
);
140 EXPORT_SYMBOL(elevator_alloc
);
142 static void elevator_release(struct kobject
*kobj
)
144 struct elevator_queue
*e
;
146 e
= container_of(kobj
, struct elevator_queue
, kobj
);
147 elevator_put(e
->type
);
151 void elevator_exit(struct request_queue
*q
)
153 struct elevator_queue
*e
= q
->elevator
;
156 blk_mq_sched_free_rqs(q
);
158 mutex_lock(&e
->sysfs_lock
);
159 blk_mq_exit_sched(q
, e
);
160 mutex_unlock(&e
->sysfs_lock
);
162 kobject_put(&e
->kobj
);
165 static inline void __elv_rqhash_del(struct request
*rq
)
168 rq
->rq_flags
&= ~RQF_HASHED
;
171 void elv_rqhash_del(struct request_queue
*q
, struct request
*rq
)
174 __elv_rqhash_del(rq
);
176 EXPORT_SYMBOL_GPL(elv_rqhash_del
);
178 void elv_rqhash_add(struct request_queue
*q
, struct request
*rq
)
180 struct elevator_queue
*e
= q
->elevator
;
182 BUG_ON(ELV_ON_HASH(rq
));
183 hash_add(e
->hash
, &rq
->hash
, rq_hash_key(rq
));
184 rq
->rq_flags
|= RQF_HASHED
;
186 EXPORT_SYMBOL_GPL(elv_rqhash_add
);
188 void elv_rqhash_reposition(struct request_queue
*q
, struct request
*rq
)
190 __elv_rqhash_del(rq
);
191 elv_rqhash_add(q
, rq
);
194 struct request
*elv_rqhash_find(struct request_queue
*q
, sector_t offset
)
196 struct elevator_queue
*e
= q
->elevator
;
197 struct hlist_node
*next
;
200 hash_for_each_possible_safe(e
->hash
, rq
, next
, hash
, offset
) {
201 BUG_ON(!ELV_ON_HASH(rq
));
203 if (unlikely(!rq_mergeable(rq
))) {
204 __elv_rqhash_del(rq
);
208 if (rq_hash_key(rq
) == offset
)
216 * RB-tree support functions for inserting/lookup/removal of requests
217 * in a sorted RB tree.
219 void elv_rb_add(struct rb_root
*root
, struct request
*rq
)
221 struct rb_node
**p
= &root
->rb_node
;
222 struct rb_node
*parent
= NULL
;
223 struct request
*__rq
;
227 __rq
= rb_entry(parent
, struct request
, rb_node
);
229 if (blk_rq_pos(rq
) < blk_rq_pos(__rq
))
231 else if (blk_rq_pos(rq
) >= blk_rq_pos(__rq
))
235 rb_link_node(&rq
->rb_node
, parent
, p
);
236 rb_insert_color(&rq
->rb_node
, root
);
238 EXPORT_SYMBOL(elv_rb_add
);
240 void elv_rb_del(struct rb_root
*root
, struct request
*rq
)
242 BUG_ON(RB_EMPTY_NODE(&rq
->rb_node
));
243 rb_erase(&rq
->rb_node
, root
);
244 RB_CLEAR_NODE(&rq
->rb_node
);
246 EXPORT_SYMBOL(elv_rb_del
);
248 struct request
*elv_rb_find(struct rb_root
*root
, sector_t sector
)
250 struct rb_node
*n
= root
->rb_node
;
254 rq
= rb_entry(n
, struct request
, rb_node
);
256 if (sector
< blk_rq_pos(rq
))
258 else if (sector
> blk_rq_pos(rq
))
266 EXPORT_SYMBOL(elv_rb_find
);
268 enum elv_merge
elv_merge(struct request_queue
*q
, struct request
**req
,
271 struct elevator_queue
*e
= q
->elevator
;
272 struct request
*__rq
;
276 * nomerges: No merges at all attempted
277 * noxmerges: Only simple one-hit cache try
278 * merges: All merge tries attempted
280 if (blk_queue_nomerges(q
) || !bio_mergeable(bio
))
281 return ELEVATOR_NO_MERGE
;
284 * First try one-hit cache.
286 if (q
->last_merge
&& elv_bio_merge_ok(q
->last_merge
, bio
)) {
287 enum elv_merge ret
= blk_try_merge(q
->last_merge
, bio
);
289 if (ret
!= ELEVATOR_NO_MERGE
) {
290 *req
= q
->last_merge
;
295 if (blk_queue_noxmerges(q
))
296 return ELEVATOR_NO_MERGE
;
299 * See if our hash lookup can find a potential backmerge.
301 __rq
= elv_rqhash_find(q
, bio
->bi_iter
.bi_sector
);
302 if (__rq
&& elv_bio_merge_ok(__rq
, bio
)) {
305 if (blk_discard_mergable(__rq
))
306 return ELEVATOR_DISCARD_MERGE
;
307 return ELEVATOR_BACK_MERGE
;
310 if (e
->type
->ops
.request_merge
)
311 return e
->type
->ops
.request_merge(q
, req
, bio
);
313 return ELEVATOR_NO_MERGE
;
317 * Attempt to do an insertion back merge. Only check for the case where
318 * we can append 'rq' to an existing request, so we can throw 'rq' away
321 * Returns true if we merged, false otherwise. 'free' will contain all
322 * requests that need to be freed.
324 bool elv_attempt_insert_merge(struct request_queue
*q
, struct request
*rq
,
325 struct list_head
*free
)
327 struct request
*__rq
;
330 if (blk_queue_nomerges(q
))
334 * First try one-hit cache.
336 if (q
->last_merge
&& blk_attempt_req_merge(q
, q
->last_merge
, rq
)) {
337 list_add(&rq
->queuelist
, free
);
341 if (blk_queue_noxmerges(q
))
346 * See if our hash lookup can find a potential backmerge.
349 __rq
= elv_rqhash_find(q
, blk_rq_pos(rq
));
350 if (!__rq
|| !blk_attempt_req_merge(q
, __rq
, rq
))
353 list_add(&rq
->queuelist
, free
);
354 /* The merged request could be merged with others, try again */
362 void elv_merged_request(struct request_queue
*q
, struct request
*rq
,
365 struct elevator_queue
*e
= q
->elevator
;
367 if (e
->type
->ops
.request_merged
)
368 e
->type
->ops
.request_merged(q
, rq
, type
);
370 if (type
== ELEVATOR_BACK_MERGE
)
371 elv_rqhash_reposition(q
, rq
);
376 void elv_merge_requests(struct request_queue
*q
, struct request
*rq
,
377 struct request
*next
)
379 struct elevator_queue
*e
= q
->elevator
;
381 if (e
->type
->ops
.requests_merged
)
382 e
->type
->ops
.requests_merged(q
, rq
, next
);
384 elv_rqhash_reposition(q
, rq
);
388 struct request
*elv_latter_request(struct request_queue
*q
, struct request
*rq
)
390 struct elevator_queue
*e
= q
->elevator
;
392 if (e
->type
->ops
.next_request
)
393 return e
->type
->ops
.next_request(q
, rq
);
398 struct request
*elv_former_request(struct request_queue
*q
, struct request
*rq
)
400 struct elevator_queue
*e
= q
->elevator
;
402 if (e
->type
->ops
.former_request
)
403 return e
->type
->ops
.former_request(q
, rq
);
408 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
411 elv_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
413 struct elv_fs_entry
*entry
= to_elv(attr
);
414 struct elevator_queue
*e
;
420 e
= container_of(kobj
, struct elevator_queue
, kobj
);
421 mutex_lock(&e
->sysfs_lock
);
422 error
= e
->type
? entry
->show(e
, page
) : -ENOENT
;
423 mutex_unlock(&e
->sysfs_lock
);
428 elv_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
429 const char *page
, size_t length
)
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
->store(e
, page
, length
) : -ENOENT
;
441 mutex_unlock(&e
->sysfs_lock
);
445 static const struct sysfs_ops elv_sysfs_ops
= {
446 .show
= elv_attr_show
,
447 .store
= elv_attr_store
,
450 static const struct kobj_type elv_ktype
= {
451 .sysfs_ops
= &elv_sysfs_ops
,
452 .release
= elevator_release
,
455 int elv_register_queue(struct request_queue
*q
, bool uevent
)
457 struct elevator_queue
*e
= q
->elevator
;
460 lockdep_assert_held(&q
->sysfs_lock
);
462 error
= kobject_add(&e
->kobj
, &q
->disk
->queue_kobj
, "iosched");
464 struct elv_fs_entry
*attr
= e
->type
->elevator_attrs
;
466 while (attr
->attr
.name
) {
467 if (sysfs_create_file(&e
->kobj
, &attr
->attr
))
473 kobject_uevent(&e
->kobj
, KOBJ_ADD
);
475 set_bit(ELEVATOR_FLAG_REGISTERED
, &e
->flags
);
480 void elv_unregister_queue(struct request_queue
*q
)
482 struct elevator_queue
*e
= q
->elevator
;
484 lockdep_assert_held(&q
->sysfs_lock
);
486 if (e
&& test_and_clear_bit(ELEVATOR_FLAG_REGISTERED
, &e
->flags
)) {
487 kobject_uevent(&e
->kobj
, KOBJ_REMOVE
);
488 kobject_del(&e
->kobj
);
492 int elv_register(struct elevator_type
*e
)
494 /* finish request is mandatory */
495 if (WARN_ON_ONCE(!e
->ops
.finish_request
))
497 /* insert_requests and dispatch_request are mandatory */
498 if (WARN_ON_ONCE(!e
->ops
.insert_requests
|| !e
->ops
.dispatch_request
))
501 /* create icq_cache if requested */
503 if (WARN_ON(e
->icq_size
< sizeof(struct io_cq
)) ||
504 WARN_ON(e
->icq_align
< __alignof__(struct io_cq
)))
507 snprintf(e
->icq_cache_name
, sizeof(e
->icq_cache_name
),
508 "%s_io_cq", e
->elevator_name
);
509 e
->icq_cache
= kmem_cache_create(e
->icq_cache_name
, e
->icq_size
,
510 e
->icq_align
, 0, NULL
);
515 /* register, don't allow duplicate names */
516 spin_lock(&elv_list_lock
);
517 if (__elevator_find(e
->elevator_name
)) {
518 spin_unlock(&elv_list_lock
);
519 kmem_cache_destroy(e
->icq_cache
);
522 list_add_tail(&e
->list
, &elv_list
);
523 spin_unlock(&elv_list_lock
);
525 printk(KERN_INFO
"io scheduler %s registered\n", e
->elevator_name
);
529 EXPORT_SYMBOL_GPL(elv_register
);
531 void elv_unregister(struct elevator_type
*e
)
534 spin_lock(&elv_list_lock
);
535 list_del_init(&e
->list
);
536 spin_unlock(&elv_list_lock
);
539 * Destroy icq_cache if it exists. icq's are RCU managed. Make
540 * sure all RCU operations are complete before proceeding.
544 kmem_cache_destroy(e
->icq_cache
);
548 EXPORT_SYMBOL_GPL(elv_unregister
);
550 static inline bool elv_support_iosched(struct request_queue
*q
)
552 if (!queue_is_mq(q
) ||
553 (q
->tag_set
->flags
& BLK_MQ_F_NO_SCHED
))
559 * For single queue devices, default to using mq-deadline. If we have multiple
560 * queues or mq-deadline is not available, default to "none".
562 static struct elevator_type
*elevator_get_default(struct request_queue
*q
)
564 if (q
->tag_set
->flags
& BLK_MQ_F_NO_SCHED_BY_DEFAULT
)
567 if (q
->nr_hw_queues
!= 1 &&
568 !blk_mq_is_shared_tags(q
->tag_set
->flags
))
571 return elevator_find_get("mq-deadline");
575 * Use the default elevator settings. If the chosen elevator initialization
576 * fails, fall back to the "none" elevator (no elevator).
578 void elevator_init_mq(struct request_queue
*q
)
580 struct elevator_type
*e
;
583 if (!elv_support_iosched(q
))
586 WARN_ON_ONCE(blk_queue_registered(q
));
588 if (unlikely(q
->elevator
))
591 e
= elevator_get_default(q
);
596 * We are called before adding disk, when there isn't any FS I/O,
597 * so freezing queue plus canceling dispatch work is enough to
598 * drain any dispatch activities originated from passthrough
599 * requests, then no need to quiesce queue which may add long boot
600 * latency, especially when lots of disks are involved.
602 * Disk isn't added yet, so verifying queue lock only manually.
604 blk_freeze_queue_start_non_owner(q
);
605 blk_freeze_acquire_lock(q
, true, false);
606 blk_mq_freeze_queue_wait(q
);
608 blk_mq_cancel_work_sync(q
);
610 err
= blk_mq_init_sched(q
, e
);
612 blk_unfreeze_release_lock(q
, true, false);
613 blk_mq_unfreeze_queue_non_owner(q
);
616 pr_warn("\"%s\" elevator initialization failed, "
617 "falling back to \"none\"\n", e
->elevator_name
);
624 * Switch to new_e io scheduler.
626 * If switching fails, we are most likely running out of memory and not able
627 * to restore the old io scheduler, so leaving the io scheduler being none.
629 int elevator_switch(struct request_queue
*q
, struct elevator_type
*new_e
)
633 lockdep_assert_held(&q
->sysfs_lock
);
635 blk_mq_freeze_queue(q
);
636 blk_mq_quiesce_queue(q
);
639 elv_unregister_queue(q
);
643 ret
= blk_mq_init_sched(q
, new_e
);
647 ret
= elv_register_queue(q
, true);
652 blk_add_trace_msg(q
, "elv switch: %s", new_e
->elevator_name
);
655 blk_mq_unquiesce_queue(q
);
656 blk_mq_unfreeze_queue(q
);
659 pr_warn("elv: switch to \"%s\" failed, falling back to \"none\"\n",
660 new_e
->elevator_name
);
666 void elevator_disable(struct request_queue
*q
)
668 lockdep_assert_held(&q
->sysfs_lock
);
670 blk_mq_freeze_queue(q
);
671 blk_mq_quiesce_queue(q
);
673 elv_unregister_queue(q
);
675 blk_queue_flag_clear(QUEUE_FLAG_SQ_SCHED
, q
);
677 q
->nr_requests
= q
->tag_set
->queue_depth
;
678 blk_add_trace_msg(q
, "elv switch: none");
680 blk_mq_unquiesce_queue(q
);
681 blk_mq_unfreeze_queue(q
);
685 * Switch this queue to the given IO scheduler.
687 static int elevator_change(struct request_queue
*q
, const char *elevator_name
)
689 struct elevator_type
*e
;
692 /* Make sure queue is not in the middle of being removed */
693 if (!blk_queue_registered(q
))
696 if (!strncmp(elevator_name
, "none", 4)) {
702 if (q
->elevator
&& elevator_match(q
->elevator
->type
, elevator_name
))
705 e
= elevator_find_get(elevator_name
);
708 ret
= elevator_switch(q
, e
);
713 void elv_iosched_load_module(struct gendisk
*disk
, const char *buf
,
716 char elevator_name
[ELV_NAME_MAX
];
717 struct elevator_type
*found
;
720 if (!elv_support_iosched(disk
->queue
))
723 strscpy(elevator_name
, buf
, sizeof(elevator_name
));
724 name
= strstrip(elevator_name
);
726 spin_lock(&elv_list_lock
);
727 found
= __elevator_find(name
);
728 spin_unlock(&elv_list_lock
);
731 request_module("%s-iosched", name
);
734 ssize_t
elv_iosched_store(struct gendisk
*disk
, const char *buf
,
737 char elevator_name
[ELV_NAME_MAX
];
740 if (!elv_support_iosched(disk
->queue
))
743 strscpy(elevator_name
, buf
, sizeof(elevator_name
));
744 ret
= elevator_change(disk
->queue
, strstrip(elevator_name
));
750 ssize_t
elv_iosched_show(struct gendisk
*disk
, char *name
)
752 struct request_queue
*q
= disk
->queue
;
753 struct elevator_queue
*eq
= q
->elevator
;
754 struct elevator_type
*cur
= NULL
, *e
;
757 if (!elv_support_iosched(q
))
758 return sprintf(name
, "none\n");
761 len
+= sprintf(name
+len
, "[none] ");
763 len
+= sprintf(name
+len
, "none ");
767 spin_lock(&elv_list_lock
);
768 list_for_each_entry(e
, &elv_list
, list
) {
770 len
+= sprintf(name
+len
, "[%s] ", e
->elevator_name
);
772 len
+= sprintf(name
+len
, "%s ", e
->elevator_name
);
774 spin_unlock(&elv_list_lock
);
776 len
+= sprintf(name
+len
, "\n");
780 struct request
*elv_rb_former_request(struct request_queue
*q
,
783 struct rb_node
*rbprev
= rb_prev(&rq
->rb_node
);
786 return rb_entry_rq(rbprev
);
790 EXPORT_SYMBOL(elv_rb_former_request
);
792 struct request
*elv_rb_latter_request(struct request_queue
*q
,
795 struct rb_node
*rbnext
= rb_next(&rq
->rb_node
);
798 return rb_entry_rq(rbnext
);
802 EXPORT_SYMBOL(elv_rb_latter_request
);
804 static int __init
elevator_setup(char *str
)
806 pr_warn("Kernel parameter elevator= does not have any effect anymore.\n"
807 "Please use sysfs to set IO scheduler for individual devices.\n");
811 __setup("elevator=", elevator_setup
);