blob: 19a78d5516ba7e7fd1b999739657f266f29a8044 [file] [log] [blame]
Christoph Hellwig3dcf60b2019-04-30 14:42:43 -04001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Block device elevator/IO-scheduler.
4 *
5 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
6 *
Jens Axboe0fe23472006-09-04 15:41:16 +02007 * 30042000 Jens Axboe <axboe@kernel.dk> :
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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 Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/bio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/slab.h>
32#include <linux/init.h>
33#include <linux/compiler.h>
Jens Axboe2056a782006-03-23 20:00:26 +010034#include <linux/blktrace_api.h>
Jens Axboe98170642006-07-28 09:23:08 +020035#include <linux/hash.h>
Jens Axboe0835da62008-08-26 09:15:47 +020036#include <linux/uaccess.h>
Lin Mingc8158812013-03-23 11:42:27 +080037#include <linux/pm_runtime.h>
Tejun Heoeea8f412015-05-22 17:13:17 -040038#include <linux/blk-cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Li Zefan55782132009-06-09 13:43:05 +080040#include <trace/events/block.h>
41
Christoph Hellwig2e9bc342021-09-20 14:33:23 +020042#include "elevator.h"
Jens Axboe242f9dc2008-09-14 05:55:09 -070043#include "blk.h"
Jens Axboebd166ef2017-01-17 06:03:22 -070044#include "blk-mq-sched.h"
Bart Van Asschebca6b062018-09-26 14:01:03 -070045#include "blk-pm.h"
Jan Kara8330cdb2017-04-19 11:33:27 +020046#include "blk-wbt.h"
Jens Axboe242f9dc2008-09-14 05:55:09 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static DEFINE_SPINLOCK(elv_list_lock);
49static LIST_HEAD(elv_list);
50
51/*
Jens Axboe98170642006-07-28 09:23:08 +020052 * Merge hash stuff.
53 */
Tejun Heo83096eb2009-05-07 22:24:39 +090054#define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
Jens Axboe98170642006-07-28 09:23:08 +020055
56/*
Jens Axboeda775262006-12-20 11:04:12 +010057 * Query io scheduler to see if the current process issuing bio may be
58 * merged with rq.
59 */
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070060static int elv_iosched_allow_bio_merge(struct request *rq, struct bio *bio)
Jens Axboeda775262006-12-20 11:04:12 +010061{
Jens Axboe165125e2007-07-24 09:28:11 +020062 struct request_queue *q = rq->q;
Jens Axboeb374d182008-10-31 10:05:07 +010063 struct elevator_queue *e = q->elevator;
Jens Axboeda775262006-12-20 11:04:12 +010064
Jens Axboef9cd4bf2018-11-01 16:41:41 -060065 if (e->type->ops.allow_merge)
66 return e->type->ops.allow_merge(q, rq, bio);
Jens Axboeda775262006-12-20 11:04:12 +010067
68 return 1;
69}
70
71/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * can we safely merge with this request?
73 */
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070074bool elv_bio_merge_ok(struct request *rq, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Tejun Heo050c8ea2012-02-08 09:19:38 +010076 if (!blk_rq_merge_ok(rq, bio))
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070077 return false;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020078
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070079 if (!elv_iosched_allow_bio_merge(rq, bio))
80 return false;
Jens Axboeda775262006-12-20 11:04:12 +010081
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070082 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070084EXPORT_SYMBOL(elv_bio_merge_ok);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Damien Le Moal68c43f12019-09-05 18:51:31 +090086static inline bool elv_support_features(unsigned int elv_features,
87 unsigned int required_features)
Jens Axboe8ac0d9a2017-10-25 12:35:02 -060088{
Damien Le Moal68c43f12019-09-05 18:51:31 +090089 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 Dunlap5b8f65e2020-07-30 18:42:29 -070098 * Return true if the elevator @e name matches @name and if @e provides all
99 * the features specified by @required_features.
Damien Le Moal68c43f12019-09-05 18:51:31 +0900100 */
101static 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 Axboe8ac0d9a2017-10-25 12:35:02 -0600106 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 Moal68c43f12019-09-05 18:51:31 +0900114/**
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 Axboe2527d992017-10-25 12:33:42 -0600121 */
Damien Le Moal68c43f12019-09-05 18:51:31 +0900122static struct elevator_type *elevator_find(const char *name,
123 unsigned int required_features)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Vasily Tarasova22b1692006-10-11 09:24:27 +0200125 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Matthias Kaehlcke70cee262007-07-10 12:26:24 +0200127 list_for_each_entry(e, &elv_list, list) {
Damien Le Moal68c43f12019-09-05 18:51:31 +0900128 if (elevator_match(e, name, required_features))
Vasily Tarasova22b1692006-10-11 09:24:27 +0200129 return e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Vasily Tarasova22b1692006-10-11 09:24:27 +0200132 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135static void elevator_put(struct elevator_type *e)
136{
137 module_put(e->elevator_owner);
138}
139
Jens Axboe2527d992017-10-25 12:33:42 -0600140static struct elevator_type *elevator_get(struct request_queue *q,
141 const char *name, bool try_loading)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Tejun Heo2824bc932005-10-20 10:56:41 +0200143 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200145 spin_lock(&elv_list_lock);
Tejun Heo2824bc932005-10-20 10:56:41 +0200146
Damien Le Moal68c43f12019-09-05 18:51:31 +0900147 e = elevator_find(name, q->required_elevator_features);
Tejun Heo21c3c5d2013-01-22 16:48:03 -0800148 if (!e && try_loading) {
Jens Axboee1640942008-02-19 10:20:37 +0100149 spin_unlock(&elv_list_lock);
Kees Cook490b94b2011-05-05 18:02:12 -0600150 request_module("%s-iosched", name);
Jens Axboee1640942008-02-19 10:20:37 +0100151 spin_lock(&elv_list_lock);
Damien Le Moal68c43f12019-09-05 18:51:31 +0900152 e = elevator_find(name, q->required_elevator_features);
Jens Axboee1640942008-02-19 10:20:37 +0100153 }
154
Tejun Heo2824bc932005-10-20 10:56:41 +0200155 if (e && !try_module_get(e->elevator_owner))
156 e = NULL;
157
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200158 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return e;
160}
161
Al Viro3d1ab402006-03-18 18:35:43 -0500162static struct kobj_type elv_ktype;
163
Jianpeng Mad50235b2013-07-03 13:25:24 +0200164struct elevator_queue *elevator_alloc(struct request_queue *q,
Jens Axboe165125e2007-07-24 09:28:11 +0200165 struct elevator_type *e)
Al Viro3d1ab402006-03-18 18:35:43 -0500166{
Jens Axboeb374d182008-10-31 10:05:07 +0100167 struct elevator_queue *eq;
Jens Axboe98170642006-07-28 09:23:08 +0200168
Joe Perchesc1b511e2013-08-29 15:21:42 -0700169 eq = kzalloc_node(sizeof(*eq), GFP_KERNEL, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200170 if (unlikely(!eq))
Chao Yu8406a4d2015-04-23 10:47:44 -0600171 return NULL;
Jens Axboe98170642006-07-28 09:23:08 +0200172
Tejun Heo22f746e2011-12-14 00:33:41 +0100173 eq->type = e;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700174 kobject_init(&eq->kobj, &elv_ktype);
Jens Axboe98170642006-07-28 09:23:08 +0200175 mutex_init(&eq->sysfs_lock);
Sasha Levin242d98f2012-12-17 10:01:27 -0500176 hash_init(eq->hash);
Jens Axboe98170642006-07-28 09:23:08 +0200177
Al Viro3d1ab402006-03-18 18:35:43 -0500178 return eq;
179}
Jianpeng Mad50235b2013-07-03 13:25:24 +0200180EXPORT_SYMBOL(elevator_alloc);
Al Viro3d1ab402006-03-18 18:35:43 -0500181
182static void elevator_release(struct kobject *kobj)
183{
Jens Axboeb374d182008-10-31 10:05:07 +0100184 struct elevator_queue *e;
Jens Axboe98170642006-07-28 09:23:08 +0200185
Jens Axboeb374d182008-10-31 10:05:07 +0100186 e = container_of(kobj, struct elevator_queue, kobj);
Tejun Heo22f746e2011-12-14 00:33:41 +0100187 elevator_put(e->type);
Al Viro3d1ab402006-03-18 18:35:43 -0500188 kfree(e);
189}
190
Ming Leic3e22192019-06-04 21:08:02 +0800191void __elevator_exit(struct request_queue *q, struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Al Viro3d1ab402006-03-18 18:35:43 -0500193 mutex_lock(&e->sysfs_lock);
Yufen Yudd1c3722020-10-08 23:26:27 -0400194 blk_mq_exit_sched(q, e);
Al Viro3d1ab402006-03-18 18:35:43 -0500195 mutex_unlock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Al Viro3d1ab402006-03-18 18:35:43 -0500197 kobject_put(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
Jens Axboe2e662b62006-07-13 11:55:04 +0200199
Jens Axboe98170642006-07-28 09:23:08 +0200200static inline void __elv_rqhash_del(struct request *rq)
201{
Sasha Levin242d98f2012-12-17 10:01:27 -0500202 hash_del(&rq->hash);
Christoph Hellwige8064022016-10-20 15:12:13 +0200203 rq->rq_flags &= ~RQF_HASHED;
Jens Axboe98170642006-07-28 09:23:08 +0200204}
205
Jens Axboe70b3ea02016-12-07 08:43:31 -0700206void elv_rqhash_del(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200207{
208 if (ELV_ON_HASH(rq))
209 __elv_rqhash_del(rq);
210}
Jens Axboebd166ef2017-01-17 06:03:22 -0700211EXPORT_SYMBOL_GPL(elv_rqhash_del);
Jens Axboe98170642006-07-28 09:23:08 +0200212
Jens Axboe70b3ea02016-12-07 08:43:31 -0700213void elv_rqhash_add(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200214{
Jens Axboeb374d182008-10-31 10:05:07 +0100215 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200216
217 BUG_ON(ELV_ON_HASH(rq));
Sasha Levin242d98f2012-12-17 10:01:27 -0500218 hash_add(e->hash, &rq->hash, rq_hash_key(rq));
Christoph Hellwige8064022016-10-20 15:12:13 +0200219 rq->rq_flags |= RQF_HASHED;
Jens Axboe98170642006-07-28 09:23:08 +0200220}
Jens Axboebd166ef2017-01-17 06:03:22 -0700221EXPORT_SYMBOL_GPL(elv_rqhash_add);
Jens Axboe98170642006-07-28 09:23:08 +0200222
Jens Axboe70b3ea02016-12-07 08:43:31 -0700223void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200224{
225 __elv_rqhash_del(rq);
226 elv_rqhash_add(q, rq);
227}
228
Jens Axboe70b3ea02016-12-07 08:43:31 -0700229struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
Jens Axboe98170642006-07-28 09:23:08 +0200230{
Jens Axboeb374d182008-10-31 10:05:07 +0100231 struct elevator_queue *e = q->elevator;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800232 struct hlist_node *next;
Jens Axboe98170642006-07-28 09:23:08 +0200233 struct request *rq;
234
Linus Torvaldsee89f812013-02-28 12:52:24 -0800235 hash_for_each_possible_safe(e->hash, rq, next, hash, offset) {
Jens Axboe98170642006-07-28 09:23:08 +0200236 BUG_ON(!ELV_ON_HASH(rq));
237
238 if (unlikely(!rq_mergeable(rq))) {
239 __elv_rqhash_del(rq);
240 continue;
241 }
242
243 if (rq_hash_key(rq) == offset)
244 return rq;
245 }
246
247 return NULL;
248}
249
Tejun Heo8922e162005-10-20 16:23:44 +0200250/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200251 * RB-tree support functions for inserting/lookup/removal of requests
252 * in a sorted RB tree.
253 */
Jeff Moyer796d5112011-06-02 21:19:05 +0200254void elv_rb_add(struct rb_root *root, struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +0200255{
256 struct rb_node **p = &root->rb_node;
257 struct rb_node *parent = NULL;
258 struct request *__rq;
259
260 while (*p) {
261 parent = *p;
262 __rq = rb_entry(parent, struct request, rb_node);
263
Tejun Heo83096eb2009-05-07 22:24:39 +0900264 if (blk_rq_pos(rq) < blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200265 p = &(*p)->rb_left;
Jeff Moyer796d5112011-06-02 21:19:05 +0200266 else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200267 p = &(*p)->rb_right;
Jens Axboe2e662b62006-07-13 11:55:04 +0200268 }
269
270 rb_link_node(&rq->rb_node, parent, p);
271 rb_insert_color(&rq->rb_node, root);
Jens Axboe2e662b62006-07-13 11:55:04 +0200272}
Jens Axboe2e662b62006-07-13 11:55:04 +0200273EXPORT_SYMBOL(elv_rb_add);
274
275void elv_rb_del(struct rb_root *root, struct request *rq)
276{
277 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
278 rb_erase(&rq->rb_node, root);
279 RB_CLEAR_NODE(&rq->rb_node);
280}
Jens Axboe2e662b62006-07-13 11:55:04 +0200281EXPORT_SYMBOL(elv_rb_del);
282
283struct request *elv_rb_find(struct rb_root *root, sector_t sector)
284{
285 struct rb_node *n = root->rb_node;
286 struct request *rq;
287
288 while (n) {
289 rq = rb_entry(n, struct request, rb_node);
290
Tejun Heo83096eb2009-05-07 22:24:39 +0900291 if (sector < blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200292 n = n->rb_left;
Tejun Heo83096eb2009-05-07 22:24:39 +0900293 else if (sector > blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200294 n = n->rb_right;
295 else
296 return rq;
297 }
298
299 return NULL;
300}
Jens Axboe2e662b62006-07-13 11:55:04 +0200301EXPORT_SYMBOL(elv_rb_find);
302
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100303enum elv_merge elv_merge(struct request_queue *q, struct request **req,
304 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Jens Axboeb374d182008-10-31 10:05:07 +0100306 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200307 struct request *__rq;
Tejun Heo06b86242005-10-20 16:46:23 +0200308
Jens Axboe98170642006-07-28 09:23:08 +0200309 /*
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100310 * Levels of merges:
311 * nomerges: No merges at all attempted
312 * noxmerges: Only simple one-hit cache try
313 * merges: All merge tries attempted
314 */
Ming Lei7460d382015-10-20 23:13:55 +0800315 if (blk_queue_nomerges(q) || !bio_mergeable(bio))
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100316 return ELEVATOR_NO_MERGE;
317
318 /*
Jens Axboe98170642006-07-28 09:23:08 +0200319 * First try one-hit cache.
320 */
Tahsin Erdogan72ef7992016-07-07 11:48:22 -0700321 if (q->last_merge && elv_bio_merge_ok(q->last_merge, bio)) {
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100322 enum elv_merge ret = blk_try_merge(q->last_merge, bio);
323
Tejun Heo06b86242005-10-20 16:46:23 +0200324 if (ret != ELEVATOR_NO_MERGE) {
325 *req = q->last_merge;
326 return ret;
327 }
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100330 if (blk_queue_noxmerges(q))
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200331 return ELEVATOR_NO_MERGE;
332
Jens Axboe98170642006-07-28 09:23:08 +0200333 /*
334 * See if our hash lookup can find a potential backmerge.
335 */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700336 __rq = elv_rqhash_find(q, bio->bi_iter.bi_sector);
Tahsin Erdogan72ef7992016-07-07 11:48:22 -0700337 if (__rq && elv_bio_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200338 *req = __rq;
Ming Lei866663b2021-07-29 11:42:26 +0800339
340 if (blk_discard_mergable(__rq))
341 return ELEVATOR_DISCARD_MERGE;
Jens Axboe98170642006-07-28 09:23:08 +0200342 return ELEVATOR_BACK_MERGE;
343 }
344
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600345 if (e->type->ops.request_merge)
346 return e->type->ops.request_merge(q, req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 return ELEVATOR_NO_MERGE;
349}
350
Jens Axboe5e84ea32011-03-21 10:14:27 +0100351/*
352 * Attempt to do an insertion back merge. Only check for the case where
353 * we can append 'rq' to an existing request, so we can throw 'rq' away
354 * afterwards.
355 *
Jan Karafd2ef392021-06-23 11:36:34 +0200356 * Returns true if we merged, false otherwise. 'free' will contain all
357 * requests that need to be freed.
Jens Axboe5e84ea32011-03-21 10:14:27 +0100358 */
Jan Karafd2ef392021-06-23 11:36:34 +0200359bool elv_attempt_insert_merge(struct request_queue *q, struct request *rq,
360 struct list_head *free)
Jens Axboe5e84ea32011-03-21 10:14:27 +0100361{
362 struct request *__rq;
Shaohua Libee03932012-11-09 08:44:27 +0100363 bool ret;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100364
365 if (blk_queue_nomerges(q))
366 return false;
367
368 /*
369 * First try one-hit cache.
370 */
Jan Karafd2ef392021-06-23 11:36:34 +0200371 if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq)) {
372 list_add(&rq->queuelist, free);
Jens Axboe5e84ea32011-03-21 10:14:27 +0100373 return true;
Jan Karafd2ef392021-06-23 11:36:34 +0200374 }
Jens Axboe5e84ea32011-03-21 10:14:27 +0100375
376 if (blk_queue_noxmerges(q))
377 return false;
378
Shaohua Libee03932012-11-09 08:44:27 +0100379 ret = false;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100380 /*
381 * See if our hash lookup can find a potential backmerge.
382 */
Shaohua Libee03932012-11-09 08:44:27 +0100383 while (1) {
384 __rq = elv_rqhash_find(q, blk_rq_pos(rq));
385 if (!__rq || !blk_attempt_req_merge(q, __rq, rq))
386 break;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100387
Jan Karafd2ef392021-06-23 11:36:34 +0200388 list_add(&rq->queuelist, free);
Shaohua Libee03932012-11-09 08:44:27 +0100389 /* The merged request could be merged with others, try again */
390 ret = true;
391 rq = __rq;
392 }
393
394 return ret;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100395}
396
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100397void elv_merged_request(struct request_queue *q, struct request *rq,
398 enum elv_merge type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Jens Axboeb374d182008-10-31 10:05:07 +0100400 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600402 if (e->type->ops.request_merged)
403 e->type->ops.request_merged(q, rq, type);
Tejun Heo06b86242005-10-20 16:46:23 +0200404
Jens Axboe2e662b62006-07-13 11:55:04 +0200405 if (type == ELEVATOR_BACK_MERGE)
406 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200407
Tejun Heo06b86242005-10-20 16:46:23 +0200408 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
Jens Axboe165125e2007-07-24 09:28:11 +0200411void elv_merge_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 struct request *next)
413{
Jens Axboeb374d182008-10-31 10:05:07 +0100414 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600416 if (e->type->ops.requests_merged)
417 e->type->ops.requests_merged(q, rq, next);
Tejun Heo06b86242005-10-20 16:46:23 +0200418
Jens Axboe98170642006-07-28 09:23:08 +0200419 elv_rqhash_reposition(q, rq);
Tejun Heo06b86242005-10-20 16:46:23 +0200420 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Jens Axboe165125e2007-07-24 09:28:11 +0200423struct request *elv_latter_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Jens Axboeb374d182008-10-31 10:05:07 +0100425 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600427 if (e->type->ops.next_request)
428 return e->type->ops.next_request(q, rq);
Jens Axboebd166ef2017-01-17 06:03:22 -0700429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return NULL;
431}
432
Jens Axboe165125e2007-07-24 09:28:11 +0200433struct request *elv_former_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Jens Axboeb374d182008-10-31 10:05:07 +0100435 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600437 if (e->type->ops.former_request)
438 return e->type->ops.former_request(q, rq);
Jens Axboea1ce35f2018-10-29 10:23:51 -0600439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return NULL;
441}
442
Al Viro3d1ab402006-03-18 18:35:43 -0500443#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
444
445static ssize_t
446elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
447{
Al Viro3d1ab402006-03-18 18:35:43 -0500448 struct elv_fs_entry *entry = to_elv(attr);
Jens Axboeb374d182008-10-31 10:05:07 +0100449 struct elevator_queue *e;
Al Viro3d1ab402006-03-18 18:35:43 -0500450 ssize_t error;
451
452 if (!entry->show)
453 return -EIO;
454
Jens Axboeb374d182008-10-31 10:05:07 +0100455 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500456 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100457 error = e->type ? entry->show(e, page) : -ENOENT;
Al Viro3d1ab402006-03-18 18:35:43 -0500458 mutex_unlock(&e->sysfs_lock);
459 return error;
460}
461
462static ssize_t
463elv_attr_store(struct kobject *kobj, struct attribute *attr,
464 const char *page, size_t length)
465{
Al Viro3d1ab402006-03-18 18:35:43 -0500466 struct elv_fs_entry *entry = to_elv(attr);
Jens Axboeb374d182008-10-31 10:05:07 +0100467 struct elevator_queue *e;
Al Viro3d1ab402006-03-18 18:35:43 -0500468 ssize_t error;
469
470 if (!entry->store)
471 return -EIO;
472
Jens Axboeb374d182008-10-31 10:05:07 +0100473 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500474 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100475 error = e->type ? entry->store(e, page, length) : -ENOENT;
Al Viro3d1ab402006-03-18 18:35:43 -0500476 mutex_unlock(&e->sysfs_lock);
477 return error;
478}
479
Emese Revfy52cf25d2010-01-19 02:58:23 +0100480static const struct sysfs_ops elv_sysfs_ops = {
Al Viro3d1ab402006-03-18 18:35:43 -0500481 .show = elv_attr_show,
482 .store = elv_attr_store,
483};
484
485static struct kobj_type elv_ktype = {
486 .sysfs_ops = &elv_sysfs_ops,
487 .release = elevator_release,
488};
489
Ming Leicecf5d82019-08-27 19:01:48 +0800490int elv_register_queue(struct request_queue *q, bool uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Tejun Heo5a5bafd2012-03-05 13:14:56 -0800492 struct elevator_queue *e = q->elevator;
Al Viro3d1ab402006-03-18 18:35:43 -0500493 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Yufen Yuf0c6ae02020-10-08 23:26:31 -0400495 lockdep_assert_held(&q->sysfs_lock);
496
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700497 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
Al Viro3d1ab402006-03-18 18:35:43 -0500498 if (!error) {
Tejun Heo22f746e2011-12-14 00:33:41 +0100499 struct elv_fs_entry *attr = e->type->elevator_attrs;
Al Viro3d1ab402006-03-18 18:35:43 -0500500 if (attr) {
Al Viroe572ec72006-03-18 22:27:18 -0500501 while (attr->attr.name) {
502 if (sysfs_create_file(&e->kobj, &attr->attr))
Al Viro3d1ab402006-03-18 18:35:43 -0500503 break;
Al Viroe572ec72006-03-18 22:27:18 -0500504 attr++;
Al Viro3d1ab402006-03-18 18:35:43 -0500505 }
506 }
Ming Leicecf5d82019-08-27 19:01:48 +0800507 if (uevent)
508 kobject_uevent(&e->kobj, KOBJ_ADD);
509
Jens Axboe430c62f2010-10-07 09:35:16 +0200510 e->registered = 1;
Al Viro3d1ab402006-03-18 18:35:43 -0500511 }
512 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
Jens Axboebc1c1162006-06-08 08:49:06 +0200514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515void elv_unregister_queue(struct request_queue *q)
516{
Yufen Yuf0c6ae02020-10-08 23:26:31 -0400517 lockdep_assert_held(&q->sysfs_lock);
518
Tejun Heof8fc8772011-12-14 00:33:40 +0100519 if (q) {
520 struct elevator_queue *e = q->elevator;
521
522 kobject_uevent(&e->kobj, KOBJ_REMOVE);
523 kobject_del(&e->kobj);
Ming Leicecf5d82019-08-27 19:01:48 +0800524
Tejun Heof8fc8772011-12-14 00:33:40 +0100525 e->registered = 0;
Jan Kara8330cdb2017-04-19 11:33:27 +0200526 /* Re-enable throttling in case elevator disabled it */
527 wbt_enable_default(q);
Tejun Heof8fc8772011-12-14 00:33:40 +0100528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Jens Axboee567bf72014-06-22 16:32:48 -0600531int elv_register(struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Damien Le Moale42cfb12021-06-18 10:59:22 +0900533 /* insert_requests and dispatch_request are mandatory */
534 if (WARN_ON_ONCE(!e->ops.insert_requests || !e->ops.dispatch_request))
535 return -EINVAL;
536
Tejun Heo3d3c2372011-12-14 00:33:42 +0100537 /* create icq_cache if requested */
538 if (e->icq_size) {
539 if (WARN_ON(e->icq_size < sizeof(struct io_cq)) ||
540 WARN_ON(e->icq_align < __alignof__(struct io_cq)))
541 return -EINVAL;
542
543 snprintf(e->icq_cache_name, sizeof(e->icq_cache_name),
544 "%s_io_cq", e->elevator_name);
545 e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size,
546 e->icq_align, 0, NULL);
547 if (!e->icq_cache)
548 return -ENOMEM;
549 }
550
551 /* register, don't allow duplicate names */
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200552 spin_lock(&elv_list_lock);
Damien Le Moal68c43f12019-09-05 18:51:31 +0900553 if (elevator_find(e->elevator_name, 0)) {
Tejun Heo3d3c2372011-12-14 00:33:42 +0100554 spin_unlock(&elv_list_lock);
Chengguang Xu62d2a192018-08-28 07:31:11 +0800555 kmem_cache_destroy(e->icq_cache);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100556 return -EBUSY;
557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 list_add_tail(&e->list, &elv_list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200559 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Hisao Tanabed0b0a812019-04-08 00:27:42 +0900561 printk(KERN_INFO "io scheduler %s registered\n", e->elevator_name);
562
Tejun Heo3d3c2372011-12-14 00:33:42 +0100563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565EXPORT_SYMBOL_GPL(elv_register);
566
567void elv_unregister(struct elevator_type *e)
568{
Tejun Heo3d3c2372011-12-14 00:33:42 +0100569 /* unregister */
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200570 spin_lock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 list_del_init(&e->list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200572 spin_unlock(&elv_list_lock);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100573
574 /*
575 * Destroy icq_cache if it exists. icq's are RCU managed. Make
576 * sure all RCU operations are complete before proceeding.
577 */
578 if (e->icq_cache) {
579 rcu_barrier();
580 kmem_cache_destroy(e->icq_cache);
581 e->icq_cache = NULL;
582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584EXPORT_SYMBOL_GPL(elv_unregister);
585
Jianchao Wangd48ece22018-08-21 15:15:03 +0800586int elevator_switch_mq(struct request_queue *q,
Omar Sandoval54d53292017-04-07 08:52:27 -0600587 struct elevator_type *new_e)
588{
589 int ret;
590
Bart Van Assche14a23492018-01-17 11:48:09 -0800591 lockdep_assert_held(&q->sysfs_lock);
592
Omar Sandoval54d53292017-04-07 08:52:27 -0600593 if (q->elevator) {
Ming Leib89f6252019-09-23 23:12:09 +0800594 if (q->elevator->registered)
Omar Sandoval54d53292017-04-07 08:52:27 -0600595 elv_unregister_queue(q);
Ming Leicecf5d82019-08-27 19:01:48 +0800596
Omar Sandoval54d53292017-04-07 08:52:27 -0600597 ioc_clear_queue(q);
598 elevator_exit(q, q->elevator);
599 }
600
601 ret = blk_mq_init_sched(q, new_e);
602 if (ret)
603 goto out;
604
605 if (new_e) {
Ming Leicecf5d82019-08-27 19:01:48 +0800606 ret = elv_register_queue(q, true);
Omar Sandoval54d53292017-04-07 08:52:27 -0600607 if (ret) {
608 elevator_exit(q, q->elevator);
609 goto out;
610 }
611 }
612
613 if (new_e)
614 blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name);
615 else
616 blk_add_trace_msg(q, "elv switch: none");
617
618out:
Omar Sandoval54d53292017-04-07 08:52:27 -0600619 return ret;
Omar Sandoval54d53292017-04-07 08:52:27 -0600620}
621
Damien Le Moal61db4372019-09-05 18:51:29 +0900622static inline bool elv_support_iosched(struct request_queue *q)
623{
Yufen Yu6251b752020-10-08 23:26:28 -0400624 if (!queue_is_mq(q) ||
Damien Le Moal7a7c5e72019-10-09 07:39:54 +0900625 (q->tag_set && (q->tag_set->flags & BLK_MQ_F_NO_SCHED)))
Damien Le Moal61db4372019-09-05 18:51:29 +0900626 return false;
627 return true;
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/*
Damien Le Moala0958ba2019-09-05 18:51:32 +0900631 * For single queue devices, default to using mq-deadline. If we have multiple
632 * queues or mq-deadline is not available, default to "none".
633 */
634static struct elevator_type *elevator_get_default(struct request_queue *q)
635{
Bart Van Assche90b71982021-08-05 10:41:59 -0700636 if (q->tag_set && q->tag_set->flags & BLK_MQ_F_NO_SCHED_BY_DEFAULT)
637 return NULL;
638
Ming Lei580dca82021-04-06 11:19:33 +0800639 if (q->nr_hw_queues != 1 &&
John Garry079a2e32021-10-05 18:23:39 +0800640 !blk_mq_is_shared_tags(q->tag_set->flags))
Damien Le Moala0958ba2019-09-05 18:51:32 +0900641 return NULL;
642
643 return elevator_get(q, "mq-deadline", false);
644}
645
646/*
647 * Get the first elevator providing the features required by the request queue.
648 * Default to "none" if no matching elevator is found.
649 */
650static struct elevator_type *elevator_get_by_features(struct request_queue *q)
651{
Jens Axboea2614252019-09-06 07:02:31 -0600652 struct elevator_type *e, *found = NULL;
Damien Le Moala0958ba2019-09-05 18:51:32 +0900653
654 spin_lock(&elv_list_lock);
655
656 list_for_each_entry(e, &elv_list, list) {
657 if (elv_support_features(e->elevator_features,
Jens Axboea2614252019-09-06 07:02:31 -0600658 q->required_elevator_features)) {
659 found = e;
Damien Le Moala0958ba2019-09-05 18:51:32 +0900660 break;
Jens Axboea2614252019-09-06 07:02:31 -0600661 }
Damien Le Moala0958ba2019-09-05 18:51:32 +0900662 }
663
Jens Axboea2614252019-09-06 07:02:31 -0600664 if (found && !try_module_get(found->elevator_owner))
665 found = NULL;
Damien Le Moala0958ba2019-09-05 18:51:32 +0900666
667 spin_unlock(&elv_list_lock);
Jens Axboea2614252019-09-06 07:02:31 -0600668 return found;
Damien Le Moala0958ba2019-09-05 18:51:32 +0900669}
670
671/*
672 * For a device queue that has no required features, use the default elevator
673 * settings. Otherwise, use the first elevator available matching the required
674 * features. If no suitable elevator is find or if the chosen elevator
675 * initialization fails, fall back to the "none" elevator (no elevator).
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200676 */
Damien Le Moal954b4a52019-09-05 18:51:30 +0900677void elevator_init_mq(struct request_queue *q)
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200678{
679 struct elevator_type *e;
Damien Le Moal954b4a52019-09-05 18:51:30 +0900680 int err;
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200681
Damien Le Moal61db4372019-09-05 18:51:29 +0900682 if (!elv_support_iosched(q))
Damien Le Moal954b4a52019-09-05 18:51:30 +0900683 return;
Damien Le Moal61db4372019-09-05 18:51:29 +0900684
Yufen Yu75e6c002020-10-08 23:26:29 -0400685 WARN_ON_ONCE(blk_queue_registered(q));
Ming Leic48dac12019-08-27 19:01:45 +0800686
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200687 if (unlikely(q->elevator))
Damien Le Moal954b4a52019-09-05 18:51:30 +0900688 return;
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200689
Damien Le Moala0958ba2019-09-05 18:51:32 +0900690 if (!q->required_elevator_features)
691 e = elevator_get_default(q);
692 else
693 e = elevator_get_by_features(q);
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200694 if (!e)
Damien Le Moal954b4a52019-09-05 18:51:30 +0900695 return;
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200696
Ming Lei245a4892021-11-17 19:55:02 +0800697 /*
698 * We are called before adding disk, when there isn't any FS I/O,
699 * so freezing queue plus canceling dispatch work is enough to
700 * drain any dispatch activities originated from passthrough
701 * requests, then no need to quiesce queue which may add long boot
702 * latency, especially when lots of disks are involved.
703 */
Damien Le Moal737eb782019-09-05 18:51:33 +0900704 blk_mq_freeze_queue(q);
Ming Lei245a4892021-11-17 19:55:02 +0800705 blk_mq_cancel_work_sync(q);
Damien Le Moal737eb782019-09-05 18:51:33 +0900706
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200707 err = blk_mq_init_sched(q, e);
Damien Le Moal737eb782019-09-05 18:51:33 +0900708
Damien Le Moal737eb782019-09-05 18:51:33 +0900709 blk_mq_unfreeze_queue(q);
710
Damien Le Moal954b4a52019-09-05 18:51:30 +0900711 if (err) {
712 pr_warn("\"%s\" elevator initialization failed, "
713 "falling back to \"none\"\n", e->elevator_name);
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200714 elevator_put(e);
Damien Le Moal954b4a52019-09-05 18:51:30 +0900715 }
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200716}
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200717
718/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 * switch to new_e io scheduler. be careful not to introduce deadlocks -
720 * we don't free the old io scheduler, before we have allocated what we
721 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +0200722 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 */
Jens Axboe165125e2007-07-24 09:28:11 +0200724static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800726 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Bart Van Assche14a23492018-01-17 11:48:09 -0800728 lockdep_assert_held(&q->sysfs_lock);
729
Jens Axboea1ce35f2018-10-29 10:23:51 -0600730 blk_mq_freeze_queue(q);
731 blk_mq_quiesce_queue(q);
Jianchao Wangd48ece22018-08-21 15:15:03 +0800732
Jens Axboea1ce35f2018-10-29 10:23:51 -0600733 err = elevator_switch_mq(q, new_e);
Jianchao Wangd48ece22018-08-21 15:15:03 +0800734
Jens Axboea1ce35f2018-10-29 10:23:51 -0600735 blk_mq_unquiesce_queue(q);
736 blk_mq_unfreeze_queue(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200737
Jens Axboe5dd531a2010-08-23 13:52:19 +0200738 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Jens Axboe5dd531a2010-08-23 13:52:19 +0200741/*
742 * Switch this queue to the given IO scheduler.
743 */
Tomoki Sekiyama7c8a3672013-10-15 16:42:19 -0600744static int __elevator_change(struct request_queue *q, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 char elevator_name[ELV_NAME_MAX];
747 struct elevator_type *e;
748
David Jefferye9a823f2017-08-28 10:52:44 -0600749 /* Make sure queue is not in the middle of being removed */
Ming Lei58c898b2019-08-27 19:01:47 +0800750 if (!blk_queue_registered(q))
David Jefferye9a823f2017-08-28 10:52:44 -0600751 return -ENOENT;
752
Jens Axboebd166ef2017-01-17 06:03:22 -0700753 /*
754 * Special case for mq, turn off scheduling
755 */
Aleksei Zakharovfbd72122019-02-11 13:50:37 +0300756 if (!strncmp(name, "none", 4)) {
757 if (!q->elevator)
758 return 0;
Jens Axboebd166ef2017-01-17 06:03:22 -0700759 return elevator_switch(q, NULL);
Aleksei Zakharovfbd72122019-02-11 13:50:37 +0300760 }
Martin K. Petersencd43e262009-05-22 17:17:52 -0400761
Li Zefanee2e9922008-10-14 08:49:56 +0200762 strlcpy(elevator_name, name, sizeof(elevator_name));
Jens Axboe2527d992017-10-25 12:33:42 -0600763 e = elevator_get(q, strstrip(elevator_name), true);
Jens Axboe340ff322017-05-10 07:40:04 -0600764 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Damien Le Moal68c43f12019-09-05 18:51:31 +0900767 if (q->elevator &&
768 elevator_match(q->elevator->type, elevator_name, 0)) {
Nate Diller2ca7d932005-10-30 15:02:24 -0800769 elevator_put(e);
Jens Axboe5dd531a2010-08-23 13:52:19 +0200770 return 0;
Nate Diller2ca7d932005-10-30 15:02:24 -0800771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Jens Axboe5dd531a2010-08-23 13:52:19 +0200773 return elevator_switch(q, e);
774}
Tomoki Sekiyama7c8a3672013-10-15 16:42:19 -0600775
Jens Axboe5dd531a2010-08-23 13:52:19 +0200776ssize_t elv_iosched_store(struct request_queue *q, const char *name,
777 size_t count)
778{
779 int ret;
780
Yufen Yu6251b752020-10-08 23:26:28 -0400781 if (!elv_support_iosched(q))
Jens Axboe5dd531a2010-08-23 13:52:19 +0200782 return count;
783
Tomoki Sekiyama7c8a3672013-10-15 16:42:19 -0600784 ret = __elevator_change(q, name);
Jens Axboe5dd531a2010-08-23 13:52:19 +0200785 if (!ret)
786 return count;
787
Jens Axboe5dd531a2010-08-23 13:52:19 +0200788 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Jens Axboe165125e2007-07-24 09:28:11 +0200791ssize_t elv_iosched_show(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Jens Axboeb374d182008-10-31 10:05:07 +0100793 struct elevator_queue *e = q->elevator;
Jens Axboebd166ef2017-01-17 06:03:22 -0700794 struct elevator_type *elv = NULL;
Matthias Kaehlcke70cee262007-07-10 12:26:24 +0200795 struct elevator_type *__e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 int len = 0;
797
Jens Axboe344e9ff2018-11-15 12:22:51 -0700798 if (!queue_is_mq(q))
Martin K. Petersencd43e262009-05-22 17:17:52 -0400799 return sprintf(name, "none\n");
800
Jens Axboebd166ef2017-01-17 06:03:22 -0700801 if (!q->elevator)
802 len += sprintf(name+len, "[none] ");
803 else
804 elv = e->type;
Martin K. Petersencd43e262009-05-22 17:17:52 -0400805
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200806 spin_lock(&elv_list_lock);
Matthias Kaehlcke70cee262007-07-10 12:26:24 +0200807 list_for_each_entry(__e, &elv_list, list) {
Damien Le Moal68c43f12019-09-05 18:51:31 +0900808 if (elv && elevator_match(elv, __e->elevator_name, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 len += sprintf(name+len, "[%s] ", elv->elevator_name);
Jens Axboebd166ef2017-01-17 06:03:22 -0700810 continue;
811 }
Damien Le Moal68c43f12019-09-05 18:51:31 +0900812 if (elv_support_iosched(q) &&
813 elevator_match(__e, __e->elevator_name,
814 q->required_elevator_features))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 len += sprintf(name+len, "%s ", __e->elevator_name);
816 }
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200817 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Jens Axboe344e9ff2018-11-15 12:22:51 -0700819 if (q->elevator)
Jens Axboebd166ef2017-01-17 06:03:22 -0700820 len += sprintf(name+len, "none");
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 len += sprintf(len+name, "\n");
823 return len;
824}
825
Jens Axboe165125e2007-07-24 09:28:11 +0200826struct request *elv_rb_former_request(struct request_queue *q,
827 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +0200828{
829 struct rb_node *rbprev = rb_prev(&rq->rb_node);
830
831 if (rbprev)
832 return rb_entry_rq(rbprev);
833
834 return NULL;
835}
Jens Axboe2e662b62006-07-13 11:55:04 +0200836EXPORT_SYMBOL(elv_rb_former_request);
837
Jens Axboe165125e2007-07-24 09:28:11 +0200838struct request *elv_rb_latter_request(struct request_queue *q,
839 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +0200840{
841 struct rb_node *rbnext = rb_next(&rq->rb_node);
842
843 if (rbnext)
844 return rb_entry_rq(rbnext);
845
846 return NULL;
847}
Jens Axboe2e662b62006-07-13 11:55:04 +0200848EXPORT_SYMBOL(elv_rb_latter_request);
Jan Karaf8db3832019-11-06 11:48:57 +0100849
850static int __init elevator_setup(char *str)
851{
852 pr_warn("Kernel parameter elevator= does not have any effect anymore.\n"
853 "Please use sysfs to set IO scheduler for individual devices.\n");
854 return 1;
855}
856
857__setup("elevator=", elevator_setup);