Christoph Hellwig | 3dcf60b | 2019-04-30 14:42:43 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Block device elevator/IO-scheduler. |
| 4 | * |
| 5 | * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE |
| 6 | * |
Jens Axboe | 0fe2347 | 2006-09-04 15:41:16 +0200 | [diff] [blame] | 7 | * 30042000 Jens Axboe <axboe@kernel.dk> : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 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 |
| 15 | * |
| 16 | * 20082000 Dave Jones <davej@suse.de> : |
| 17 | * Removed tests for max-bomb-segments, which was breaking elvtune |
| 18 | * when run without -bN |
| 19 | * |
| 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 |
| 24 | * |
| 25 | */ |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/fs.h> |
| 28 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/bio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/module.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/init.h> |
| 33 | #include <linux/compiler.h> |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 34 | #include <linux/blktrace_api.h> |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 35 | #include <linux/hash.h> |
Jens Axboe | 0835da6 | 2008-08-26 09:15:47 +0200 | [diff] [blame] | 36 | #include <linux/uaccess.h> |
Lin Ming | c815881 | 2013-03-23 11:42:27 +0800 | [diff] [blame] | 37 | #include <linux/pm_runtime.h> |
Tejun Heo | eea8f41 | 2015-05-22 17:13:17 -0400 | [diff] [blame] | 38 | #include <linux/blk-cgroup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 40 | #include <trace/events/block.h> |
| 41 | |
Christoph Hellwig | 2e9bc34 | 2021-09-20 14:33:23 +0200 | [diff] [blame] | 42 | #include "elevator.h" |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 43 | #include "blk.h" |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 44 | #include "blk-mq-sched.h" |
Bart Van Assche | bca6b06 | 2018-09-26 14:01:03 -0700 | [diff] [blame] | 45 | #include "blk-pm.h" |
Jan Kara | 8330cdb | 2017-04-19 11:33:27 +0200 | [diff] [blame] | 46 | #include "blk-wbt.h" |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static DEFINE_SPINLOCK(elv_list_lock); |
| 49 | static LIST_HEAD(elv_list); |
| 50 | |
| 51 | /* |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 52 | * Merge hash stuff. |
| 53 | */ |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 54 | #define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq)) |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 55 | |
| 56 | /* |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 57 | * Query io scheduler to see if the current process issuing bio may be |
| 58 | * merged with rq. |
| 59 | */ |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 60 | static int elv_iosched_allow_bio_merge(struct request *rq, struct bio *bio) |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 61 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 62 | struct request_queue *q = rq->q; |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 63 | struct elevator_queue *e = q->elevator; |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 64 | |
Jens Axboe | f9cd4bf | 2018-11-01 16:41:41 -0600 | [diff] [blame] | 65 | if (e->type->ops.allow_merge) |
| 66 | return e->type->ops.allow_merge(q, rq, bio); |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 67 | |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | * can we safely merge with this request? |
| 73 | */ |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 74 | bool elv_bio_merge_ok(struct request *rq, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 76 | if (!blk_rq_merge_ok(rq, bio)) |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 77 | return false; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 78 | |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 79 | if (!elv_iosched_allow_bio_merge(rq, bio)) |
| 80 | return false; |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 81 | |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 82 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 84 | EXPORT_SYMBOL(elv_bio_merge_ok); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Damien Le Moal | 68c43f1 | 2019-09-05 18:51:31 +0900 | [diff] [blame] | 86 | static inline bool elv_support_features(unsigned int elv_features, |
| 87 | unsigned int required_features) |
Jens Axboe | 8ac0d9a | 2017-10-25 12:35:02 -0600 | [diff] [blame] | 88 | { |
Damien Le Moal | 68c43f1 | 2019-09-05 18:51:31 +0900 | [diff] [blame] | 89 | return (required_features & elv_features) == required_features; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * elevator_match - Test an elevator name and features |
| 94 | * @e: Scheduler to test |
| 95 | * @name: Elevator name to test |
| 96 | * @required_features: Features that the elevator must provide |
| 97 | * |
Randy Dunlap | 5b8f65e | 2020-07-30 18:42:29 -0700 | [diff] [blame] | 98 | * Return true if the elevator @e name matches @name and if @e provides all |
| 99 | * the features specified by @required_features. |
Damien Le Moal | 68c43f1 | 2019-09-05 18:51:31 +0900 | [diff] [blame] | 100 | */ |
| 101 | static bool elevator_match(const struct elevator_type *e, const char *name, |
| 102 | unsigned int required_features) |
| 103 | { |
| 104 | if (!elv_support_features(e->elevator_features, required_features)) |
| 105 | return false; |
Jens Axboe | 8ac0d9a | 2017-10-25 12:35:02 -0600 | [diff] [blame] | 106 | if (!strcmp(e->elevator_name, name)) |
| 107 | return true; |
| 108 | if (e->elevator_alias && !strcmp(e->elevator_alias, name)) |
| 109 | return true; |
| 110 | |
| 111 | return false; |
| 112 | } |
| 113 | |
Damien Le Moal | 68c43f1 | 2019-09-05 18:51:31 +0900 | [diff] [blame] | 114 | /** |
| 115 | * elevator_find - Find an elevator |
| 116 | * @name: Name of the elevator to find |
| 117 | * @required_features: Features that the elevator must provide |
| 118 | * |
| 119 | * Return the first registered scheduler with name @name and supporting the |
| 120 | * features @required_features and NULL otherwise. |
Jens Axboe | 2527d99 | 2017-10-25 12:33:42 -0600 | [diff] [blame] | 121 | */ |
Damien Le Moal | 68c43f1 | 2019-09-05 18:51:31 +0900 | [diff] [blame] | 122 | static struct elevator_type *elevator_find(const char *name, |
| 123 | unsigned int required_features) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
Vasily Tarasov | a22b169 | 2006-10-11 09:24:27 +0200 | [diff] [blame] | 125 | struct elevator_type *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Matthias Kaehlcke | 70cee26 | 2007-07-10 12:26:24 +0200 | [diff] [blame] | 127 | list_for_each_entry(e, &elv_list, list) { |
Damien Le Moal | 68c43f1 | 2019-09-05 18:51:31 +0900 | [diff] [blame] | 128 | if (elevator_match(e, name, required_features)) |
Vasily Tarasov | a22b169 | 2006-10-11 09:24:27 +0200 | [diff] [blame] | 129 | return e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Vasily Tarasov | a22b169 | 2006-10-11 09:24:27 +0200 | [diff] [blame] | 132 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static void elevator_put(struct elevator_type *e) |
| 136 | { |
| 137 | module_put(e->elevator_owner); |
| 138 | } |
| 139 | |
Jens Axboe | 2527d99 | 2017-10-25 12:33:42 -0600 | [diff] [blame] | 140 | static struct elevator_type *elevator_get(struct request_queue *q, |
| 141 | const char *name, bool try_loading) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 143 | struct elevator_type *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 145 | spin_lock(&elv_list_lock); |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 146 | |
Damien Le Moal | 68c43f1 | 2019-09-05 18:51:31 +0900 | [diff] [blame] | 147 | e = elevator_find(name, q->required_elevator_features); |
Tejun Heo | 21c3c5d | 2013-01-22 16:48:03 -0800 | [diff] [blame] | 148 | if (!e && try_loading) { |
Jens Axboe | e164094 | 2008-02-19 10:20:37 +0100 | [diff] [blame] | 149 | spin_unlock(&elv_list_lock); |
Kees Cook | 490b94b | 2011-05-05 18:02:12 -0600 | [diff] [blame] | 150 | request_module("%s-iosched", name); |
Jens Axboe | e164094 | 2008-02-19 10:20:37 +0100 | [diff] [blame] | 151 | spin_lock(&elv_list_lock); |
Damien Le Moal | 68c43f1 | 2019-09-05 18:51:31 +0900 | [diff] [blame] | 152 | e = elevator_find(name, q->required_elevator_features); |
Jens Axboe | e164094 | 2008-02-19 10:20:37 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 155 | if (e && !try_module_get(e->elevator_owner)) |
| 156 | e = NULL; |
| 157 | |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 158 | spin_unlock(&elv_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | return e; |
| 160 | } |
| 161 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 162 | static struct kobj_type elv_ktype; |
| 163 | |
Jianpeng Ma | d50235b | 2013-07-03 13:25:24 +0200 | [diff] [blame] | 164 | struct elevator_queue *elevator_alloc(struct request_queue *q, |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 165 | struct elevator_type *e) |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 166 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 167 | struct elevator_queue *eq; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 168 | |
Joe Perches | c1b511e | 2013-08-29 15:21:42 -0700 | [diff] [blame] | 169 | eq = kzalloc_node(sizeof(*eq), GFP_KERNEL, q->node); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 170 | if (unlikely(!eq)) |
Chao Yu | 8406a4d | 2015-04-23 10:47:44 -0600 | [diff] [blame] | 171 | return NULL; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 172 | |
Tejun Heo | 22f746e | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 173 | eq->type = e; |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 174 | kobject_init(&eq->kobj, &elv_ktype); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 175 | mutex_init(&eq->sysfs_lock); |
Sasha Levin | 242d98f | 2012-12-17 10:01:27 -0500 | [diff] [blame] | 176 | hash_init(eq->hash); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 177 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 178 | return eq; |
| 179 | } |
Jianpeng Ma | d50235b | 2013-07-03 13:25:24 +0200 | [diff] [blame] | 180 | EXPORT_SYMBOL(elevator_alloc); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 181 | |
| 182 | static void elevator_release(struct kobject *kobj) |
| 183 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 184 | struct elevator_queue *e; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 185 | |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 186 | e = container_of(kobj, struct elevator_queue, kobj); |
Tejun Heo | 22f746e | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 187 | elevator_put(e->type); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 188 | kfree(e); |
| 189 | } |
| 190 | |
Christoph Hellwig | 0c6cb3a | 2021-11-23 19:53:07 +0100 | [diff] [blame] | 191 | void elevator_exit(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
Christoph Hellwig | 0c6cb3a | 2021-11-23 19:53:07 +0100 | [diff] [blame] | 193 | struct elevator_queue *e = q->elevator; |
| 194 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 195 | mutex_lock(&e->sysfs_lock); |
Yufen Yu | dd1c372 | 2020-10-08 23:26:27 -0400 | [diff] [blame] | 196 | blk_mq_exit_sched(q, e); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 197 | mutex_unlock(&e->sysfs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 199 | kobject_put(&e->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 201 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 202 | static inline void __elv_rqhash_del(struct request *rq) |
| 203 | { |
Sasha Levin | 242d98f | 2012-12-17 10:01:27 -0500 | [diff] [blame] | 204 | hash_del(&rq->hash); |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 205 | rq->rq_flags &= ~RQF_HASHED; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Jens Axboe | 70b3ea0 | 2016-12-07 08:43:31 -0700 | [diff] [blame] | 208 | void elv_rqhash_del(struct request_queue *q, struct request *rq) |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 209 | { |
| 210 | if (ELV_ON_HASH(rq)) |
| 211 | __elv_rqhash_del(rq); |
| 212 | } |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 213 | EXPORT_SYMBOL_GPL(elv_rqhash_del); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 214 | |
Jens Axboe | 70b3ea0 | 2016-12-07 08:43:31 -0700 | [diff] [blame] | 215 | void elv_rqhash_add(struct request_queue *q, struct request *rq) |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 216 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 217 | struct elevator_queue *e = q->elevator; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 218 | |
| 219 | BUG_ON(ELV_ON_HASH(rq)); |
Sasha Levin | 242d98f | 2012-12-17 10:01:27 -0500 | [diff] [blame] | 220 | hash_add(e->hash, &rq->hash, rq_hash_key(rq)); |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 221 | rq->rq_flags |= RQF_HASHED; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 222 | } |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 223 | EXPORT_SYMBOL_GPL(elv_rqhash_add); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 224 | |
Jens Axboe | 70b3ea0 | 2016-12-07 08:43:31 -0700 | [diff] [blame] | 225 | void elv_rqhash_reposition(struct request_queue *q, struct request *rq) |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 226 | { |
| 227 | __elv_rqhash_del(rq); |
| 228 | elv_rqhash_add(q, rq); |
| 229 | } |
| 230 | |
Jens Axboe | 70b3ea0 | 2016-12-07 08:43:31 -0700 | [diff] [blame] | 231 | struct request *elv_rqhash_find(struct request_queue *q, sector_t offset) |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 232 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 233 | struct elevator_queue *e = q->elevator; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 234 | struct hlist_node *next; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 235 | struct request *rq; |
| 236 | |
Linus Torvalds | ee89f81 | 2013-02-28 12:52:24 -0800 | [diff] [blame] | 237 | hash_for_each_possible_safe(e->hash, rq, next, hash, offset) { |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 238 | BUG_ON(!ELV_ON_HASH(rq)); |
| 239 | |
| 240 | if (unlikely(!rq_mergeable(rq))) { |
| 241 | __elv_rqhash_del(rq); |
| 242 | continue; |
| 243 | } |
| 244 | |
| 245 | if (rq_hash_key(rq) == offset) |
| 246 | return rq; |
| 247 | } |
| 248 | |
| 249 | return NULL; |
| 250 | } |
| 251 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 252 | /* |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 253 | * RB-tree support functions for inserting/lookup/removal of requests |
| 254 | * in a sorted RB tree. |
| 255 | */ |
Jeff Moyer | 796d511 | 2011-06-02 21:19:05 +0200 | [diff] [blame] | 256 | void elv_rb_add(struct rb_root *root, struct request *rq) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 257 | { |
| 258 | struct rb_node **p = &root->rb_node; |
| 259 | struct rb_node *parent = NULL; |
| 260 | struct request *__rq; |
| 261 | |
| 262 | while (*p) { |
| 263 | parent = *p; |
| 264 | __rq = rb_entry(parent, struct request, rb_node); |
| 265 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 266 | if (blk_rq_pos(rq) < blk_rq_pos(__rq)) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 267 | p = &(*p)->rb_left; |
Jeff Moyer | 796d511 | 2011-06-02 21:19:05 +0200 | [diff] [blame] | 268 | else if (blk_rq_pos(rq) >= blk_rq_pos(__rq)) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 269 | p = &(*p)->rb_right; |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | rb_link_node(&rq->rb_node, parent, p); |
| 273 | rb_insert_color(&rq->rb_node, root); |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 274 | } |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 275 | EXPORT_SYMBOL(elv_rb_add); |
| 276 | |
| 277 | void elv_rb_del(struct rb_root *root, struct request *rq) |
| 278 | { |
| 279 | BUG_ON(RB_EMPTY_NODE(&rq->rb_node)); |
| 280 | rb_erase(&rq->rb_node, root); |
| 281 | RB_CLEAR_NODE(&rq->rb_node); |
| 282 | } |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 283 | EXPORT_SYMBOL(elv_rb_del); |
| 284 | |
| 285 | struct request *elv_rb_find(struct rb_root *root, sector_t sector) |
| 286 | { |
| 287 | struct rb_node *n = root->rb_node; |
| 288 | struct request *rq; |
| 289 | |
| 290 | while (n) { |
| 291 | rq = rb_entry(n, struct request, rb_node); |
| 292 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 293 | if (sector < blk_rq_pos(rq)) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 294 | n = n->rb_left; |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 295 | else if (sector > blk_rq_pos(rq)) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 296 | n = n->rb_right; |
| 297 | else |
| 298 | return rq; |
| 299 | } |
| 300 | |
| 301 | return NULL; |
| 302 | } |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 303 | EXPORT_SYMBOL(elv_rb_find); |
| 304 | |
Christoph Hellwig | 34fe7c0 | 2017-02-08 14:46:48 +0100 | [diff] [blame] | 305 | enum elv_merge elv_merge(struct request_queue *q, struct request **req, |
| 306 | struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 308 | struct elevator_queue *e = q->elevator; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 309 | struct request *__rq; |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 310 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 311 | /* |
Alan D. Brunelle | 488991e | 2010-01-29 09:04:08 +0100 | [diff] [blame] | 312 | * Levels of merges: |
| 313 | * nomerges: No merges at all attempted |
| 314 | * noxmerges: Only simple one-hit cache try |
| 315 | * merges: All merge tries attempted |
| 316 | */ |
Ming Lei | 7460d38 | 2015-10-20 23:13:55 +0800 | [diff] [blame] | 317 | if (blk_queue_nomerges(q) || !bio_mergeable(bio)) |
Alan D. Brunelle | 488991e | 2010-01-29 09:04:08 +0100 | [diff] [blame] | 318 | return ELEVATOR_NO_MERGE; |
| 319 | |
| 320 | /* |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 321 | * First try one-hit cache. |
| 322 | */ |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 323 | if (q->last_merge && elv_bio_merge_ok(q->last_merge, bio)) { |
Christoph Hellwig | 34fe7c0 | 2017-02-08 14:46:48 +0100 | [diff] [blame] | 324 | enum elv_merge ret = blk_try_merge(q->last_merge, bio); |
| 325 | |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 326 | if (ret != ELEVATOR_NO_MERGE) { |
| 327 | *req = q->last_merge; |
| 328 | return ret; |
| 329 | } |
| 330 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
Alan D. Brunelle | 488991e | 2010-01-29 09:04:08 +0100 | [diff] [blame] | 332 | if (blk_queue_noxmerges(q)) |
Alan D. Brunelle | ac9fafa | 2008-04-29 14:44:19 +0200 | [diff] [blame] | 333 | return ELEVATOR_NO_MERGE; |
| 334 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 335 | /* |
| 336 | * See if our hash lookup can find a potential backmerge. |
| 337 | */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 338 | __rq = elv_rqhash_find(q, bio->bi_iter.bi_sector); |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 339 | if (__rq && elv_bio_merge_ok(__rq, bio)) { |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 340 | *req = __rq; |
Ming Lei | 866663b | 2021-07-29 11:42:26 +0800 | [diff] [blame] | 341 | |
| 342 | if (blk_discard_mergable(__rq)) |
| 343 | return ELEVATOR_DISCARD_MERGE; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 344 | return ELEVATOR_BACK_MERGE; |
| 345 | } |
| 346 | |
Jens Axboe | f9cd4bf | 2018-11-01 16:41:41 -0600 | [diff] [blame] | 347 | if (e->type->ops.request_merge) |
| 348 | return e->type->ops.request_merge(q, req, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
| 350 | return ELEVATOR_NO_MERGE; |
| 351 | } |
| 352 | |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 353 | /* |
| 354 | * Attempt to do an insertion back merge. Only check for the case where |
| 355 | * we can append 'rq' to an existing request, so we can throw 'rq' away |
| 356 | * afterwards. |
| 357 | * |
Jan Kara | fd2ef39 | 2021-06-23 11:36:34 +0200 | [diff] [blame] | 358 | * Returns true if we merged, false otherwise. 'free' will contain all |
| 359 | * requests that need to be freed. |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 360 | */ |
Jan Kara | fd2ef39 | 2021-06-23 11:36:34 +0200 | [diff] [blame] | 361 | bool elv_attempt_insert_merge(struct request_queue *q, struct request *rq, |
| 362 | struct list_head *free) |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 363 | { |
| 364 | struct request *__rq; |
Shaohua Li | bee0393 | 2012-11-09 08:44:27 +0100 | [diff] [blame] | 365 | bool ret; |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 366 | |
| 367 | if (blk_queue_nomerges(q)) |
| 368 | return false; |
| 369 | |
| 370 | /* |
| 371 | * First try one-hit cache. |
| 372 | */ |
Jan Kara | fd2ef39 | 2021-06-23 11:36:34 +0200 | [diff] [blame] | 373 | if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq)) { |
| 374 | list_add(&rq->queuelist, free); |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 375 | return true; |
Jan Kara | fd2ef39 | 2021-06-23 11:36:34 +0200 | [diff] [blame] | 376 | } |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 377 | |
| 378 | if (blk_queue_noxmerges(q)) |
| 379 | return false; |
| 380 | |
Shaohua Li | bee0393 | 2012-11-09 08:44:27 +0100 | [diff] [blame] | 381 | ret = false; |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 382 | /* |
| 383 | * See if our hash lookup can find a potential backmerge. |
| 384 | */ |
Shaohua Li | bee0393 | 2012-11-09 08:44:27 +0100 | [diff] [blame] | 385 | while (1) { |
| 386 | __rq = elv_rqhash_find(q, blk_rq_pos(rq)); |
| 387 | if (!__rq || !blk_attempt_req_merge(q, __rq, rq)) |
| 388 | break; |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 389 | |
Jan Kara | fd2ef39 | 2021-06-23 11:36:34 +0200 | [diff] [blame] | 390 | list_add(&rq->queuelist, free); |
Shaohua Li | bee0393 | 2012-11-09 08:44:27 +0100 | [diff] [blame] | 391 | /* The merged request could be merged with others, try again */ |
| 392 | ret = true; |
| 393 | rq = __rq; |
| 394 | } |
| 395 | |
| 396 | return ret; |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 397 | } |
| 398 | |
Christoph Hellwig | 34fe7c0 | 2017-02-08 14:46:48 +0100 | [diff] [blame] | 399 | void elv_merged_request(struct request_queue *q, struct request *rq, |
| 400 | enum elv_merge type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 402 | struct elevator_queue *e = q->elevator; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Jens Axboe | f9cd4bf | 2018-11-01 16:41:41 -0600 | [diff] [blame] | 404 | if (e->type->ops.request_merged) |
| 405 | e->type->ops.request_merged(q, rq, type); |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 406 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 407 | if (type == ELEVATOR_BACK_MERGE) |
| 408 | elv_rqhash_reposition(q, rq); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 409 | |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 410 | q->last_merge = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 413 | void elv_merge_requests(struct request_queue *q, struct request *rq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | struct request *next) |
| 415 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 416 | struct elevator_queue *e = q->elevator; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
Jens Axboe | f9cd4bf | 2018-11-01 16:41:41 -0600 | [diff] [blame] | 418 | if (e->type->ops.requests_merged) |
| 419 | e->type->ops.requests_merged(q, rq, next); |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 420 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 421 | elv_rqhash_reposition(q, rq); |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 422 | q->last_merge = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 425 | struct request *elv_latter_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 427 | struct elevator_queue *e = q->elevator; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
Jens Axboe | f9cd4bf | 2018-11-01 16:41:41 -0600 | [diff] [blame] | 429 | if (e->type->ops.next_request) |
| 430 | return e->type->ops.next_request(q, rq); |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | return NULL; |
| 433 | } |
| 434 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 435 | struct request *elv_former_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 437 | struct elevator_queue *e = q->elevator; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
Jens Axboe | f9cd4bf | 2018-11-01 16:41:41 -0600 | [diff] [blame] | 439 | if (e->type->ops.former_request) |
| 440 | return e->type->ops.former_request(q, rq); |
Jens Axboe | a1ce35f | 2018-10-29 10:23:51 -0600 | [diff] [blame] | 441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | return NULL; |
| 443 | } |
| 444 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 445 | #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr) |
| 446 | |
| 447 | static ssize_t |
| 448 | elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 449 | { |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 450 | struct elv_fs_entry *entry = to_elv(attr); |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 451 | struct elevator_queue *e; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 452 | ssize_t error; |
| 453 | |
| 454 | if (!entry->show) |
| 455 | return -EIO; |
| 456 | |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 457 | e = container_of(kobj, struct elevator_queue, kobj); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 458 | mutex_lock(&e->sysfs_lock); |
Tejun Heo | 22f746e | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 459 | error = e->type ? entry->show(e, page) : -ENOENT; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 460 | mutex_unlock(&e->sysfs_lock); |
| 461 | return error; |
| 462 | } |
| 463 | |
| 464 | static ssize_t |
| 465 | elv_attr_store(struct kobject *kobj, struct attribute *attr, |
| 466 | const char *page, size_t length) |
| 467 | { |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 468 | struct elv_fs_entry *entry = to_elv(attr); |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 469 | struct elevator_queue *e; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 470 | ssize_t error; |
| 471 | |
| 472 | if (!entry->store) |
| 473 | return -EIO; |
| 474 | |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 475 | e = container_of(kobj, struct elevator_queue, kobj); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 476 | mutex_lock(&e->sysfs_lock); |
Tejun Heo | 22f746e | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 477 | error = e->type ? entry->store(e, page, length) : -ENOENT; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 478 | mutex_unlock(&e->sysfs_lock); |
| 479 | return error; |
| 480 | } |
| 481 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 482 | static const struct sysfs_ops elv_sysfs_ops = { |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 483 | .show = elv_attr_show, |
| 484 | .store = elv_attr_store, |
| 485 | }; |
| 486 | |
| 487 | static struct kobj_type elv_ktype = { |
| 488 | .sysfs_ops = &elv_sysfs_ops, |
| 489 | .release = elevator_release, |
| 490 | }; |
| 491 | |
Ming Lei | cecf5d8 | 2019-08-27 19:01:48 +0800 | [diff] [blame] | 492 | int elv_register_queue(struct request_queue *q, bool uevent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | { |
Tejun Heo | 5a5bafd | 2012-03-05 13:14:56 -0800 | [diff] [blame] | 494 | struct elevator_queue *e = q->elevator; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 495 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
Yufen Yu | f0c6ae0 | 2020-10-08 23:26:31 -0400 | [diff] [blame] | 497 | lockdep_assert_held(&q->sysfs_lock); |
| 498 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 499 | error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched"); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 500 | if (!error) { |
Tejun Heo | 22f746e | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 501 | struct elv_fs_entry *attr = e->type->elevator_attrs; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 502 | if (attr) { |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 503 | while (attr->attr.name) { |
| 504 | if (sysfs_create_file(&e->kobj, &attr->attr)) |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 505 | break; |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 506 | attr++; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 507 | } |
| 508 | } |
Ming Lei | cecf5d8 | 2019-08-27 19:01:48 +0800 | [diff] [blame] | 509 | if (uevent) |
| 510 | kobject_uevent(&e->kobj, KOBJ_ADD); |
| 511 | |
Jens Axboe | 430c62f | 2010-10-07 09:35:16 +0200 | [diff] [blame] | 512 | e->registered = 1; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 513 | } |
| 514 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | } |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | void elv_unregister_queue(struct request_queue *q) |
| 518 | { |
Yufen Yu | f0c6ae0 | 2020-10-08 23:26:31 -0400 | [diff] [blame] | 519 | lockdep_assert_held(&q->sysfs_lock); |
| 520 | |
Tejun Heo | f8fc877 | 2011-12-14 00:33:40 +0100 | [diff] [blame] | 521 | if (q) { |
| 522 | struct elevator_queue *e = q->elevator; |
| 523 | |
| 524 | kobject_uevent(&e->kobj, KOBJ_REMOVE); |
| 525 | kobject_del(&e->kobj); |
Ming Lei | cecf5d8 | 2019-08-27 19:01:48 +0800 | [diff] [blame] | 526 | |
Tejun Heo | f8fc877 | 2011-12-14 00:33:40 +0100 | [diff] [blame] | 527 | e->registered = 0; |
Jan Kara | 8330cdb | 2017-04-19 11:33:27 +0200 | [diff] [blame] | 528 | /* Re-enable throttling in case elevator disabled it */ |
| 529 | wbt_enable_default(q); |
Tejun Heo | f8fc877 | 2011-12-14 00:33:40 +0100 | [diff] [blame] | 530 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Jens Axboe | e567bf7 | 2014-06-22 16:32:48 -0600 | [diff] [blame] | 533 | int elv_register(struct elevator_type *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | { |
Damien Le Moal | e42cfb1 | 2021-06-18 10:59:22 +0900 | [diff] [blame] | 535 | /* insert_requests and dispatch_request are mandatory */ |
| 536 | if (WARN_ON_ONCE(!e->ops.insert_requests || !e->ops.dispatch_request)) |
| 537 | return -EINVAL; |
| 538 | |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 539 | /* create icq_cache if requested */ |
| 540 | if (e->icq_size) { |
| 541 | if (WARN_ON(e->icq_size < sizeof(struct io_cq)) || |
| 542 | WARN_ON(e->icq_align < __alignof__(struct io_cq))) |
| 543 | return -EINVAL; |
| 544 | |
| 545 | snprintf(e->icq_cache_name, sizeof(e->icq_cache_name), |
| 546 | "%s_io_cq", e->elevator_name); |
| 547 | e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size, |
| 548 | e->icq_align, 0, NULL); |
| 549 | if (!e->icq_cache) |
| 550 | return -ENOMEM; |
| 551 | } |
| 552 | |
| 553 | /* register, don't allow duplicate names */ |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 554 | spin_lock(&elv_list_lock); |
Damien Le Moal | 68c43f1 | 2019-09-05 18:51:31 +0900 | [diff] [blame] | 555 | if (elevator_find(e->elevator_name, 0)) { |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 556 | spin_unlock(&elv_list_lock); |
Chengguang Xu | 62d2a19 | 2018-08-28 07:31:11 +0800 | [diff] [blame] | 557 | kmem_cache_destroy(e->icq_cache); |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 558 | return -EBUSY; |
| 559 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | list_add_tail(&e->list, &elv_list); |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 561 | spin_unlock(&elv_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
Hisao Tanabe | d0b0a81 | 2019-04-08 00:27:42 +0900 | [diff] [blame] | 563 | printk(KERN_INFO "io scheduler %s registered\n", e->elevator_name); |
| 564 | |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 565 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | } |
| 567 | EXPORT_SYMBOL_GPL(elv_register); |
| 568 | |
| 569 | void elv_unregister(struct elevator_type *e) |
| 570 | { |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 571 | /* unregister */ |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 572 | spin_lock(&elv_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | list_del_init(&e->list); |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 574 | spin_unlock(&elv_list_lock); |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 575 | |
| 576 | /* |
| 577 | * Destroy icq_cache if it exists. icq's are RCU managed. Make |
| 578 | * sure all RCU operations are complete before proceeding. |
| 579 | */ |
| 580 | if (e->icq_cache) { |
| 581 | rcu_barrier(); |
| 582 | kmem_cache_destroy(e->icq_cache); |
| 583 | e->icq_cache = NULL; |
| 584 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | } |
| 586 | EXPORT_SYMBOL_GPL(elv_unregister); |
| 587 | |
Jianchao Wang | d48ece2 | 2018-08-21 15:15:03 +0800 | [diff] [blame] | 588 | int elevator_switch_mq(struct request_queue *q, |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 589 | struct elevator_type *new_e) |
| 590 | { |
| 591 | int ret; |
| 592 | |
Bart Van Assche | 14a2349 | 2018-01-17 11:48:09 -0800 | [diff] [blame] | 593 | lockdep_assert_held(&q->sysfs_lock); |
| 594 | |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 595 | if (q->elevator) { |
Ming Lei | b89f625 | 2019-09-23 23:12:09 +0800 | [diff] [blame] | 596 | if (q->elevator->registered) |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 597 | elv_unregister_queue(q); |
Ming Lei | cecf5d8 | 2019-08-27 19:01:48 +0800 | [diff] [blame] | 598 | |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 599 | ioc_clear_queue(q); |
Christoph Hellwig | f46b81c | 2021-11-23 19:53:06 +0100 | [diff] [blame] | 600 | blk_mq_sched_free_rqs(q); |
Christoph Hellwig | 0c6cb3a | 2021-11-23 19:53:07 +0100 | [diff] [blame] | 601 | elevator_exit(q); |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | ret = blk_mq_init_sched(q, new_e); |
| 605 | if (ret) |
| 606 | goto out; |
| 607 | |
| 608 | if (new_e) { |
Ming Lei | cecf5d8 | 2019-08-27 19:01:48 +0800 | [diff] [blame] | 609 | ret = elv_register_queue(q, true); |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 610 | if (ret) { |
Christoph Hellwig | f46b81c | 2021-11-23 19:53:06 +0100 | [diff] [blame] | 611 | blk_mq_sched_free_rqs(q); |
Christoph Hellwig | 0c6cb3a | 2021-11-23 19:53:07 +0100 | [diff] [blame] | 612 | elevator_exit(q); |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 613 | goto out; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | if (new_e) |
| 618 | blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name); |
| 619 | else |
| 620 | blk_add_trace_msg(q, "elv switch: none"); |
| 621 | |
| 622 | out: |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 623 | return ret; |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 624 | } |
| 625 | |
Damien Le Moal | 61db437 | 2019-09-05 18:51:29 +0900 | [diff] [blame] | 626 | static inline bool elv_support_iosched(struct request_queue *q) |
| 627 | { |
Yufen Yu | 6251b75 | 2020-10-08 23:26:28 -0400 | [diff] [blame] | 628 | if (!queue_is_mq(q) || |
Damien Le Moal | 7a7c5e7 | 2019-10-09 07:39:54 +0900 | [diff] [blame] | 629 | (q->tag_set && (q->tag_set->flags & BLK_MQ_F_NO_SCHED))) |
Damien Le Moal | 61db437 | 2019-09-05 18:51:29 +0900 | [diff] [blame] | 630 | return false; |
| 631 | return true; |
| 632 | } |
| 633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | /* |
Damien Le Moal | a0958ba | 2019-09-05 18:51:32 +0900 | [diff] [blame] | 635 | * For single queue devices, default to using mq-deadline. If we have multiple |
| 636 | * queues or mq-deadline is not available, default to "none". |
| 637 | */ |
| 638 | static struct elevator_type *elevator_get_default(struct request_queue *q) |
| 639 | { |
Bart Van Assche | 90b7198 | 2021-08-05 10:41:59 -0700 | [diff] [blame] | 640 | if (q->tag_set && q->tag_set->flags & BLK_MQ_F_NO_SCHED_BY_DEFAULT) |
| 641 | return NULL; |
| 642 | |
Ming Lei | 580dca8 | 2021-04-06 11:19:33 +0800 | [diff] [blame] | 643 | if (q->nr_hw_queues != 1 && |
John Garry | 079a2e3 | 2021-10-05 18:23:39 +0800 | [diff] [blame] | 644 | !blk_mq_is_shared_tags(q->tag_set->flags)) |
Damien Le Moal | a0958ba | 2019-09-05 18:51:32 +0900 | [diff] [blame] | 645 | return NULL; |
| 646 | |
| 647 | return elevator_get(q, "mq-deadline", false); |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * Get the first elevator providing the features required by the request queue. |
| 652 | * Default to "none" if no matching elevator is found. |
| 653 | */ |
| 654 | static struct elevator_type *elevator_get_by_features(struct request_queue *q) |
| 655 | { |
Jens Axboe | a261425 | 2019-09-06 07:02:31 -0600 | [diff] [blame] | 656 | struct elevator_type *e, *found = NULL; |
Damien Le Moal | a0958ba | 2019-09-05 18:51:32 +0900 | [diff] [blame] | 657 | |
| 658 | spin_lock(&elv_list_lock); |
| 659 | |
| 660 | list_for_each_entry(e, &elv_list, list) { |
| 661 | if (elv_support_features(e->elevator_features, |
Jens Axboe | a261425 | 2019-09-06 07:02:31 -0600 | [diff] [blame] | 662 | q->required_elevator_features)) { |
| 663 | found = e; |
Damien Le Moal | a0958ba | 2019-09-05 18:51:32 +0900 | [diff] [blame] | 664 | break; |
Jens Axboe | a261425 | 2019-09-06 07:02:31 -0600 | [diff] [blame] | 665 | } |
Damien Le Moal | a0958ba | 2019-09-05 18:51:32 +0900 | [diff] [blame] | 666 | } |
| 667 | |
Jens Axboe | a261425 | 2019-09-06 07:02:31 -0600 | [diff] [blame] | 668 | if (found && !try_module_get(found->elevator_owner)) |
| 669 | found = NULL; |
Damien Le Moal | a0958ba | 2019-09-05 18:51:32 +0900 | [diff] [blame] | 670 | |
| 671 | spin_unlock(&elv_list_lock); |
Jens Axboe | a261425 | 2019-09-06 07:02:31 -0600 | [diff] [blame] | 672 | return found; |
Damien Le Moal | a0958ba | 2019-09-05 18:51:32 +0900 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /* |
| 676 | * For a device queue that has no required features, use the default elevator |
| 677 | * settings. Otherwise, use the first elevator available matching the required |
| 678 | * features. If no suitable elevator is find or if the chosen elevator |
| 679 | * initialization fails, fall back to the "none" elevator (no elevator). |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 680 | */ |
Damien Le Moal | 954b4a5 | 2019-09-05 18:51:30 +0900 | [diff] [blame] | 681 | void elevator_init_mq(struct request_queue *q) |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 682 | { |
| 683 | struct elevator_type *e; |
Damien Le Moal | 954b4a5 | 2019-09-05 18:51:30 +0900 | [diff] [blame] | 684 | int err; |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 685 | |
Damien Le Moal | 61db437 | 2019-09-05 18:51:29 +0900 | [diff] [blame] | 686 | if (!elv_support_iosched(q)) |
Damien Le Moal | 954b4a5 | 2019-09-05 18:51:30 +0900 | [diff] [blame] | 687 | return; |
Damien Le Moal | 61db437 | 2019-09-05 18:51:29 +0900 | [diff] [blame] | 688 | |
Yufen Yu | 75e6c00 | 2020-10-08 23:26:29 -0400 | [diff] [blame] | 689 | WARN_ON_ONCE(blk_queue_registered(q)); |
Ming Lei | c48dac1 | 2019-08-27 19:01:45 +0800 | [diff] [blame] | 690 | |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 691 | if (unlikely(q->elevator)) |
Damien Le Moal | 954b4a5 | 2019-09-05 18:51:30 +0900 | [diff] [blame] | 692 | return; |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 693 | |
Damien Le Moal | a0958ba | 2019-09-05 18:51:32 +0900 | [diff] [blame] | 694 | if (!q->required_elevator_features) |
| 695 | e = elevator_get_default(q); |
| 696 | else |
| 697 | e = elevator_get_by_features(q); |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 698 | if (!e) |
Damien Le Moal | 954b4a5 | 2019-09-05 18:51:30 +0900 | [diff] [blame] | 699 | return; |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 700 | |
Ming Lei | 245a489 | 2021-11-17 19:55:02 +0800 | [diff] [blame] | 701 | /* |
| 702 | * We are called before adding disk, when there isn't any FS I/O, |
| 703 | * so freezing queue plus canceling dispatch work is enough to |
| 704 | * drain any dispatch activities originated from passthrough |
| 705 | * requests, then no need to quiesce queue which may add long boot |
| 706 | * latency, especially when lots of disks are involved. |
| 707 | */ |
Damien Le Moal | 737eb78 | 2019-09-05 18:51:33 +0900 | [diff] [blame] | 708 | blk_mq_freeze_queue(q); |
Ming Lei | 245a489 | 2021-11-17 19:55:02 +0800 | [diff] [blame] | 709 | blk_mq_cancel_work_sync(q); |
Damien Le Moal | 737eb78 | 2019-09-05 18:51:33 +0900 | [diff] [blame] | 710 | |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 711 | err = blk_mq_init_sched(q, e); |
Damien Le Moal | 737eb78 | 2019-09-05 18:51:33 +0900 | [diff] [blame] | 712 | |
Damien Le Moal | 737eb78 | 2019-09-05 18:51:33 +0900 | [diff] [blame] | 713 | blk_mq_unfreeze_queue(q); |
| 714 | |
Damien Le Moal | 954b4a5 | 2019-09-05 18:51:30 +0900 | [diff] [blame] | 715 | if (err) { |
| 716 | pr_warn("\"%s\" elevator initialization failed, " |
| 717 | "falling back to \"none\"\n", e->elevator_name); |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 718 | elevator_put(e); |
Damien Le Moal | 954b4a5 | 2019-09-05 18:51:30 +0900 | [diff] [blame] | 719 | } |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 720 | } |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 721 | |
| 722 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | * switch to new_e io scheduler. be careful not to introduce deadlocks - |
| 724 | * we don't free the old io scheduler, before we have allocated what we |
| 725 | * need for the new one. this way we have a chance of going back to the old |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 726 | * one, if the new one fails init for some reason. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 728 | static int elevator_switch(struct request_queue *q, struct elevator_type *new_e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | { |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 730 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
Bart Van Assche | 14a2349 | 2018-01-17 11:48:09 -0800 | [diff] [blame] | 732 | lockdep_assert_held(&q->sysfs_lock); |
| 733 | |
Jens Axboe | a1ce35f | 2018-10-29 10:23:51 -0600 | [diff] [blame] | 734 | blk_mq_freeze_queue(q); |
| 735 | blk_mq_quiesce_queue(q); |
Jianchao Wang | d48ece2 | 2018-08-21 15:15:03 +0800 | [diff] [blame] | 736 | |
Jens Axboe | a1ce35f | 2018-10-29 10:23:51 -0600 | [diff] [blame] | 737 | err = elevator_switch_mq(q, new_e); |
Jianchao Wang | d48ece2 | 2018-08-21 15:15:03 +0800 | [diff] [blame] | 738 | |
Jens Axboe | a1ce35f | 2018-10-29 10:23:51 -0600 | [diff] [blame] | 739 | blk_mq_unquiesce_queue(q); |
| 740 | blk_mq_unfreeze_queue(q); |
Nick Piggin | 75ad23b | 2008-04-29 14:48:33 +0200 | [diff] [blame] | 741 | |
Jens Axboe | 5dd531a | 2010-08-23 13:52:19 +0200 | [diff] [blame] | 742 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Jens Axboe | 5dd531a | 2010-08-23 13:52:19 +0200 | [diff] [blame] | 745 | /* |
| 746 | * Switch this queue to the given IO scheduler. |
| 747 | */ |
Tomoki Sekiyama | 7c8a367 | 2013-10-15 16:42:19 -0600 | [diff] [blame] | 748 | static int __elevator_change(struct request_queue *q, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | { |
| 750 | char elevator_name[ELV_NAME_MAX]; |
| 751 | struct elevator_type *e; |
| 752 | |
David Jeffery | e9a823f | 2017-08-28 10:52:44 -0600 | [diff] [blame] | 753 | /* Make sure queue is not in the middle of being removed */ |
Ming Lei | 58c898b | 2019-08-27 19:01:47 +0800 | [diff] [blame] | 754 | if (!blk_queue_registered(q)) |
David Jeffery | e9a823f | 2017-08-28 10:52:44 -0600 | [diff] [blame] | 755 | return -ENOENT; |
| 756 | |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 757 | /* |
| 758 | * Special case for mq, turn off scheduling |
| 759 | */ |
Aleksei Zakharov | fbd7212 | 2019-02-11 13:50:37 +0300 | [diff] [blame] | 760 | if (!strncmp(name, "none", 4)) { |
| 761 | if (!q->elevator) |
| 762 | return 0; |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 763 | return elevator_switch(q, NULL); |
Aleksei Zakharov | fbd7212 | 2019-02-11 13:50:37 +0300 | [diff] [blame] | 764 | } |
Martin K. Petersen | cd43e26 | 2009-05-22 17:17:52 -0400 | [diff] [blame] | 765 | |
Li Zefan | ee2e992 | 2008-10-14 08:49:56 +0200 | [diff] [blame] | 766 | strlcpy(elevator_name, name, sizeof(elevator_name)); |
Jens Axboe | 2527d99 | 2017-10-25 12:33:42 -0600 | [diff] [blame] | 767 | e = elevator_get(q, strstrip(elevator_name), true); |
Jens Axboe | 340ff32 | 2017-05-10 07:40:04 -0600 | [diff] [blame] | 768 | if (!e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
Damien Le Moal | 68c43f1 | 2019-09-05 18:51:31 +0900 | [diff] [blame] | 771 | if (q->elevator && |
| 772 | elevator_match(q->elevator->type, elevator_name, 0)) { |
Nate Diller | 2ca7d93 | 2005-10-30 15:02:24 -0800 | [diff] [blame] | 773 | elevator_put(e); |
Jens Axboe | 5dd531a | 2010-08-23 13:52:19 +0200 | [diff] [blame] | 774 | return 0; |
Nate Diller | 2ca7d93 | 2005-10-30 15:02:24 -0800 | [diff] [blame] | 775 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | |
Jens Axboe | 5dd531a | 2010-08-23 13:52:19 +0200 | [diff] [blame] | 777 | return elevator_switch(q, e); |
| 778 | } |
Tomoki Sekiyama | 7c8a367 | 2013-10-15 16:42:19 -0600 | [diff] [blame] | 779 | |
Jens Axboe | 5dd531a | 2010-08-23 13:52:19 +0200 | [diff] [blame] | 780 | ssize_t elv_iosched_store(struct request_queue *q, const char *name, |
| 781 | size_t count) |
| 782 | { |
| 783 | int ret; |
| 784 | |
Yufen Yu | 6251b75 | 2020-10-08 23:26:28 -0400 | [diff] [blame] | 785 | if (!elv_support_iosched(q)) |
Jens Axboe | 5dd531a | 2010-08-23 13:52:19 +0200 | [diff] [blame] | 786 | return count; |
| 787 | |
Tomoki Sekiyama | 7c8a367 | 2013-10-15 16:42:19 -0600 | [diff] [blame] | 788 | ret = __elevator_change(q, name); |
Jens Axboe | 5dd531a | 2010-08-23 13:52:19 +0200 | [diff] [blame] | 789 | if (!ret) |
| 790 | return count; |
| 791 | |
Jens Axboe | 5dd531a | 2010-08-23 13:52:19 +0200 | [diff] [blame] | 792 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | } |
| 794 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 795 | ssize_t elv_iosched_show(struct request_queue *q, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 797 | struct elevator_queue *e = q->elevator; |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 798 | struct elevator_type *elv = NULL; |
Matthias Kaehlcke | 70cee26 | 2007-07-10 12:26:24 +0200 | [diff] [blame] | 799 | struct elevator_type *__e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | int len = 0; |
| 801 | |
Jens Axboe | 344e9ff | 2018-11-15 12:22:51 -0700 | [diff] [blame] | 802 | if (!queue_is_mq(q)) |
Martin K. Petersen | cd43e26 | 2009-05-22 17:17:52 -0400 | [diff] [blame] | 803 | return sprintf(name, "none\n"); |
| 804 | |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 805 | if (!q->elevator) |
| 806 | len += sprintf(name+len, "[none] "); |
| 807 | else |
| 808 | elv = e->type; |
Martin K. Petersen | cd43e26 | 2009-05-22 17:17:52 -0400 | [diff] [blame] | 809 | |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 810 | spin_lock(&elv_list_lock); |
Matthias Kaehlcke | 70cee26 | 2007-07-10 12:26:24 +0200 | [diff] [blame] | 811 | list_for_each_entry(__e, &elv_list, list) { |
Damien Le Moal | 68c43f1 | 2019-09-05 18:51:31 +0900 | [diff] [blame] | 812 | if (elv && elevator_match(elv, __e->elevator_name, 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | len += sprintf(name+len, "[%s] ", elv->elevator_name); |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 814 | continue; |
| 815 | } |
Damien Le Moal | 68c43f1 | 2019-09-05 18:51:31 +0900 | [diff] [blame] | 816 | if (elv_support_iosched(q) && |
| 817 | elevator_match(__e, __e->elevator_name, |
| 818 | q->required_elevator_features)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | len += sprintf(name+len, "%s ", __e->elevator_name); |
| 820 | } |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 821 | spin_unlock(&elv_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
Jens Axboe | 344e9ff | 2018-11-15 12:22:51 -0700 | [diff] [blame] | 823 | if (q->elevator) |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 824 | len += sprintf(name+len, "none"); |
| 825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | len += sprintf(len+name, "\n"); |
| 827 | return len; |
| 828 | } |
| 829 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 830 | struct request *elv_rb_former_request(struct request_queue *q, |
| 831 | struct request *rq) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 832 | { |
| 833 | struct rb_node *rbprev = rb_prev(&rq->rb_node); |
| 834 | |
| 835 | if (rbprev) |
| 836 | return rb_entry_rq(rbprev); |
| 837 | |
| 838 | return NULL; |
| 839 | } |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 840 | EXPORT_SYMBOL(elv_rb_former_request); |
| 841 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 842 | struct request *elv_rb_latter_request(struct request_queue *q, |
| 843 | struct request *rq) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 844 | { |
| 845 | struct rb_node *rbnext = rb_next(&rq->rb_node); |
| 846 | |
| 847 | if (rbnext) |
| 848 | return rb_entry_rq(rbnext); |
| 849 | |
| 850 | return NULL; |
| 851 | } |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 852 | EXPORT_SYMBOL(elv_rb_latter_request); |
Jan Kara | f8db383 | 2019-11-06 11:48:57 +0100 | [diff] [blame] | 853 | |
| 854 | static int __init elevator_setup(char *str) |
| 855 | { |
| 856 | pr_warn("Kernel parameter elevator= does not have any effect anymore.\n" |
| 857 | "Please use sysfs to set IO scheduler for individual devices.\n"); |
| 858 | return 1; |
| 859 | } |
| 860 | |
| 861 | __setup("elevator=", elevator_setup); |