Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / block / elevator.c
blob7c3ba80e5ff4a390e62bb4318d364e95da92cd3c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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
13 * an existing request
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
20 * Jens:
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>
27 #include <linux/fs.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>
41 #include "elevator.h"
42 #include "blk.h"
43 #include "blk-mq-sched.h"
44 #include "blk-pm.h"
45 #include "blk-wbt.h"
46 #include "blk-cgroup.h"
48 static DEFINE_SPINLOCK(elv_list_lock);
49 static LIST_HEAD(elv_list);
52 * Merge hash stuff.
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
58 * merged with rq.
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);
68 return true;
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))
77 return false;
79 if (!elv_iosched_allow_bio_merge(rq, bio))
80 return false;
82 return true;
84 EXPORT_SYMBOL(elv_bio_merge_ok);
86 /**
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))
105 return e;
106 return NULL;
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)))
116 e = NULL;
117 spin_unlock(&elv_list_lock);
118 return e;
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);
129 if (unlikely(!eq))
130 return NULL;
132 __elevator_get(e);
133 eq->type = e;
134 kobject_init(&eq->kobj, &elv_ktype);
135 mutex_init(&eq->sysfs_lock);
136 hash_init(eq->hash);
138 return eq;
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);
148 kfree(e);
151 void elevator_exit(struct request_queue *q)
153 struct elevator_queue *e = q->elevator;
155 ioc_clear_queue(q);
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)
167 hash_del(&rq->hash);
168 rq->rq_flags &= ~RQF_HASHED;
171 void elv_rqhash_del(struct request_queue *q, struct request *rq)
173 if (ELV_ON_HASH(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;
198 struct request *rq;
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);
205 continue;
208 if (rq_hash_key(rq) == offset)
209 return rq;
212 return NULL;
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;
225 while (*p) {
226 parent = *p;
227 __rq = rb_entry(parent, struct request, rb_node);
229 if (blk_rq_pos(rq) < blk_rq_pos(__rq))
230 p = &(*p)->rb_left;
231 else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))
232 p = &(*p)->rb_right;
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;
251 struct request *rq;
253 while (n) {
254 rq = rb_entry(n, struct request, rb_node);
256 if (sector < blk_rq_pos(rq))
257 n = n->rb_left;
258 else if (sector > blk_rq_pos(rq))
259 n = n->rb_right;
260 else
261 return rq;
264 return NULL;
266 EXPORT_SYMBOL(elv_rb_find);
268 enum elv_merge elv_merge(struct request_queue *q, struct request **req,
269 struct bio *bio)
271 struct elevator_queue *e = q->elevator;
272 struct request *__rq;
275 * Levels of merges:
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;
291 return ret;
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)) {
303 *req = __rq;
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
319 * afterwards.
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;
328 bool ret;
330 if (blk_queue_nomerges(q))
331 return false;
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);
338 return true;
341 if (blk_queue_noxmerges(q))
342 return false;
344 ret = false;
346 * See if our hash lookup can find a potential backmerge.
348 while (1) {
349 __rq = elv_rqhash_find(q, blk_rq_pos(rq));
350 if (!__rq || !blk_attempt_req_merge(q, __rq, rq))
351 break;
353 list_add(&rq->queuelist, free);
354 /* The merged request could be merged with others, try again */
355 ret = true;
356 rq = __rq;
359 return ret;
362 void elv_merged_request(struct request_queue *q, struct request *rq,
363 enum elv_merge type)
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);
373 q->last_merge = 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);
385 q->last_merge = 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);
395 return NULL;
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);
405 return NULL;
408 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
410 static ssize_t
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;
415 ssize_t error;
417 if (!entry->show)
418 return -EIO;
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);
424 return error;
427 static ssize_t
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;
433 ssize_t error;
435 if (!entry->store)
436 return -EIO;
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);
442 return error;
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;
458 int error;
460 lockdep_assert_held(&q->sysfs_lock);
462 error = kobject_add(&e->kobj, &q->disk->queue_kobj, "iosched");
463 if (!error) {
464 struct elv_fs_entry *attr = e->type->elevator_attrs;
465 if (attr) {
466 while (attr->attr.name) {
467 if (sysfs_create_file(&e->kobj, &attr->attr))
468 break;
469 attr++;
472 if (uevent)
473 kobject_uevent(&e->kobj, KOBJ_ADD);
475 set_bit(ELEVATOR_FLAG_REGISTERED, &e->flags);
477 return error;
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))
496 return -EINVAL;
497 /* insert_requests and dispatch_request are mandatory */
498 if (WARN_ON_ONCE(!e->ops.insert_requests || !e->ops.dispatch_request))
499 return -EINVAL;
501 /* create icq_cache if requested */
502 if (e->icq_size) {
503 if (WARN_ON(e->icq_size < sizeof(struct io_cq)) ||
504 WARN_ON(e->icq_align < __alignof__(struct io_cq)))
505 return -EINVAL;
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);
511 if (!e->icq_cache)
512 return -ENOMEM;
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);
520 return -EBUSY;
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);
527 return 0;
529 EXPORT_SYMBOL_GPL(elv_register);
531 void elv_unregister(struct elevator_type *e)
533 /* unregister */
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.
542 if (e->icq_cache) {
543 rcu_barrier();
544 kmem_cache_destroy(e->icq_cache);
545 e->icq_cache = NULL;
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))
554 return false;
555 return true;
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)
565 return NULL;
567 if (q->nr_hw_queues != 1 &&
568 !blk_mq_is_shared_tags(q->tag_set->flags))
569 return NULL;
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;
581 int err;
583 if (!elv_support_iosched(q))
584 return;
586 WARN_ON_ONCE(blk_queue_registered(q));
588 if (unlikely(q->elevator))
589 return;
591 e = elevator_get_default(q);
592 if (!e)
593 return;
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);
615 if (err) {
616 pr_warn("\"%s\" elevator initialization failed, "
617 "falling back to \"none\"\n", e->elevator_name);
620 elevator_put(e);
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)
631 int ret;
633 lockdep_assert_held(&q->sysfs_lock);
635 blk_mq_freeze_queue(q);
636 blk_mq_quiesce_queue(q);
638 if (q->elevator) {
639 elv_unregister_queue(q);
640 elevator_exit(q);
643 ret = blk_mq_init_sched(q, new_e);
644 if (ret)
645 goto out_unfreeze;
647 ret = elv_register_queue(q, true);
648 if (ret) {
649 elevator_exit(q);
650 goto out_unfreeze;
652 blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name);
654 out_unfreeze:
655 blk_mq_unquiesce_queue(q);
656 blk_mq_unfreeze_queue(q);
658 if (ret) {
659 pr_warn("elv: switch to \"%s\" failed, falling back to \"none\"\n",
660 new_e->elevator_name);
663 return ret;
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);
674 elevator_exit(q);
675 blk_queue_flag_clear(QUEUE_FLAG_SQ_SCHED, q);
676 q->elevator = NULL;
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;
690 int ret;
692 /* Make sure queue is not in the middle of being removed */
693 if (!blk_queue_registered(q))
694 return -ENOENT;
696 if (!strncmp(elevator_name, "none", 4)) {
697 if (q->elevator)
698 elevator_disable(q);
699 return 0;
702 if (q->elevator && elevator_match(q->elevator->type, elevator_name))
703 return 0;
705 e = elevator_find_get(elevator_name);
706 if (!e)
707 return -EINVAL;
708 ret = elevator_switch(q, e);
709 elevator_put(e);
710 return ret;
713 void elv_iosched_load_module(struct gendisk *disk, const char *buf,
714 size_t count)
716 char elevator_name[ELV_NAME_MAX];
717 struct elevator_type *found;
718 const char *name;
720 if (!elv_support_iosched(disk->queue))
721 return;
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);
730 if (!found)
731 request_module("%s-iosched", name);
734 ssize_t elv_iosched_store(struct gendisk *disk, const char *buf,
735 size_t count)
737 char elevator_name[ELV_NAME_MAX];
738 int ret;
740 if (!elv_support_iosched(disk->queue))
741 return count;
743 strscpy(elevator_name, buf, sizeof(elevator_name));
744 ret = elevator_change(disk->queue, strstrip(elevator_name));
745 if (!ret)
746 return count;
747 return ret;
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;
755 int len = 0;
757 if (!elv_support_iosched(q))
758 return sprintf(name, "none\n");
760 if (!q->elevator) {
761 len += sprintf(name+len, "[none] ");
762 } else {
763 len += sprintf(name+len, "none ");
764 cur = eq->type;
767 spin_lock(&elv_list_lock);
768 list_for_each_entry(e, &elv_list, list) {
769 if (e == cur)
770 len += sprintf(name+len, "[%s] ", e->elevator_name);
771 else
772 len += sprintf(name+len, "%s ", e->elevator_name);
774 spin_unlock(&elv_list_lock);
776 len += sprintf(name+len, "\n");
777 return len;
780 struct request *elv_rb_former_request(struct request_queue *q,
781 struct request *rq)
783 struct rb_node *rbprev = rb_prev(&rq->rb_node);
785 if (rbprev)
786 return rb_entry_rq(rbprev);
788 return NULL;
790 EXPORT_SYMBOL(elv_rb_former_request);
792 struct request *elv_rb_latter_request(struct request_queue *q,
793 struct request *rq)
795 struct rb_node *rbnext = rb_next(&rq->rb_node);
797 if (rbnext)
798 return rb_entry_rq(rbnext);
800 return NULL;
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");
808 return 1;
811 __setup("elevator=", elevator_setup);