blob: 4eab3d70e880aff0ab1806aff8b3d1685880e61c [file] [log] [blame]
Christoph Hellwig3dcf60bc2019-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>
29#include <linux/elevator.h>
30#include <linux/bio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/init.h>
34#include <linux/compiler.h>
Jens Axboe2056a782006-03-23 20:00:26 +010035#include <linux/blktrace_api.h>
Jens Axboe98170642006-07-28 09:23:08 +020036#include <linux/hash.h>
Jens Axboe0835da62008-08-26 09:15:47 +020037#include <linux/uaccess.h>
Lin Mingc8158812013-03-23 11:42:27 +080038#include <linux/pm_runtime.h>
Tejun Heoeea8f412015-05-22 17:13:17 -040039#include <linux/blk-cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Li Zefan55782132009-06-09 13:43:05 +080041#include <trace/events/block.h>
42
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 *
98 * Return true is the elevator @e name matches @name and if @e provides all the
99 * the feratures spcified by @required_features.
100 */
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);
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600194 if (e->type->ops.exit_sched)
Omar Sandoval54d53292017-04-07 08:52:27 -0600195 blk_mq_exit_sched(q, e);
Al Viro3d1ab402006-03-18 18:35:43 -0500196 mutex_unlock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Al Viro3d1ab402006-03-18 18:35:43 -0500198 kobject_put(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
Jens Axboe2e662b62006-07-13 11:55:04 +0200200
Jens Axboe98170642006-07-28 09:23:08 +0200201static inline void __elv_rqhash_del(struct request *rq)
202{
Sasha Levin242d98f2012-12-17 10:01:27 -0500203 hash_del(&rq->hash);
Christoph Hellwige8064022016-10-20 15:12:13 +0200204 rq->rq_flags &= ~RQF_HASHED;
Jens Axboe98170642006-07-28 09:23:08 +0200205}
206
Jens Axboe70b3ea02016-12-07 08:43:31 -0700207void elv_rqhash_del(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200208{
209 if (ELV_ON_HASH(rq))
210 __elv_rqhash_del(rq);
211}
Jens Axboebd166ef2017-01-17 06:03:22 -0700212EXPORT_SYMBOL_GPL(elv_rqhash_del);
Jens Axboe98170642006-07-28 09:23:08 +0200213
Jens Axboe70b3ea02016-12-07 08:43:31 -0700214void elv_rqhash_add(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200215{
Jens Axboeb374d182008-10-31 10:05:07 +0100216 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200217
218 BUG_ON(ELV_ON_HASH(rq));
Sasha Levin242d98f2012-12-17 10:01:27 -0500219 hash_add(e->hash, &rq->hash, rq_hash_key(rq));
Christoph Hellwige8064022016-10-20 15:12:13 +0200220 rq->rq_flags |= RQF_HASHED;
Jens Axboe98170642006-07-28 09:23:08 +0200221}
Jens Axboebd166ef2017-01-17 06:03:22 -0700222EXPORT_SYMBOL_GPL(elv_rqhash_add);
Jens Axboe98170642006-07-28 09:23:08 +0200223
Jens Axboe70b3ea02016-12-07 08:43:31 -0700224void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200225{
226 __elv_rqhash_del(rq);
227 elv_rqhash_add(q, rq);
228}
229
Jens Axboe70b3ea02016-12-07 08:43:31 -0700230struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
Jens Axboe98170642006-07-28 09:23:08 +0200231{
Jens Axboeb374d182008-10-31 10:05:07 +0100232 struct elevator_queue *e = q->elevator;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800233 struct hlist_node *next;
Jens Axboe98170642006-07-28 09:23:08 +0200234 struct request *rq;
235
Linus Torvaldsee89f812013-02-28 12:52:24 -0800236 hash_for_each_possible_safe(e->hash, rq, next, hash, offset) {
Jens Axboe98170642006-07-28 09:23:08 +0200237 BUG_ON(!ELV_ON_HASH(rq));
238
239 if (unlikely(!rq_mergeable(rq))) {
240 __elv_rqhash_del(rq);
241 continue;
242 }
243
244 if (rq_hash_key(rq) == offset)
245 return rq;
246 }
247
248 return NULL;
249}
250
Tejun Heo8922e162005-10-20 16:23:44 +0200251/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200252 * RB-tree support functions for inserting/lookup/removal of requests
253 * in a sorted RB tree.
254 */
Jeff Moyer796d5112011-06-02 21:19:05 +0200255void elv_rb_add(struct rb_root *root, struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +0200256{
257 struct rb_node **p = &root->rb_node;
258 struct rb_node *parent = NULL;
259 struct request *__rq;
260
261 while (*p) {
262 parent = *p;
263 __rq = rb_entry(parent, struct request, rb_node);
264
Tejun Heo83096eb2009-05-07 22:24:39 +0900265 if (blk_rq_pos(rq) < blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200266 p = &(*p)->rb_left;
Jeff Moyer796d5112011-06-02 21:19:05 +0200267 else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200268 p = &(*p)->rb_right;
Jens Axboe2e662b62006-07-13 11:55:04 +0200269 }
270
271 rb_link_node(&rq->rb_node, parent, p);
272 rb_insert_color(&rq->rb_node, root);
Jens Axboe2e662b62006-07-13 11:55:04 +0200273}
Jens Axboe2e662b62006-07-13 11:55:04 +0200274EXPORT_SYMBOL(elv_rb_add);
275
276void elv_rb_del(struct rb_root *root, struct request *rq)
277{
278 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
279 rb_erase(&rq->rb_node, root);
280 RB_CLEAR_NODE(&rq->rb_node);
281}
Jens Axboe2e662b62006-07-13 11:55:04 +0200282EXPORT_SYMBOL(elv_rb_del);
283
284struct request *elv_rb_find(struct rb_root *root, sector_t sector)
285{
286 struct rb_node *n = root->rb_node;
287 struct request *rq;
288
289 while (n) {
290 rq = rb_entry(n, struct request, rb_node);
291
Tejun Heo83096eb2009-05-07 22:24:39 +0900292 if (sector < blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200293 n = n->rb_left;
Tejun Heo83096eb2009-05-07 22:24:39 +0900294 else if (sector > blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200295 n = n->rb_right;
296 else
297 return rq;
298 }
299
300 return NULL;
301}
Jens Axboe2e662b62006-07-13 11:55:04 +0200302EXPORT_SYMBOL(elv_rb_find);
303
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100304enum elv_merge elv_merge(struct request_queue *q, struct request **req,
305 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Jens Axboeb374d182008-10-31 10:05:07 +0100307 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200308 struct request *__rq;
Tejun Heo06b86242005-10-20 16:46:23 +0200309
Jens Axboe98170642006-07-28 09:23:08 +0200310 /*
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100311 * Levels of merges:
312 * nomerges: No merges at all attempted
313 * noxmerges: Only simple one-hit cache try
314 * merges: All merge tries attempted
315 */
Ming Lei7460d382015-10-20 23:13:55 +0800316 if (blk_queue_nomerges(q) || !bio_mergeable(bio))
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100317 return ELEVATOR_NO_MERGE;
318
319 /*
Jens Axboe98170642006-07-28 09:23:08 +0200320 * First try one-hit cache.
321 */
Tahsin Erdogan72ef7992016-07-07 11:48:22 -0700322 if (q->last_merge && elv_bio_merge_ok(q->last_merge, bio)) {
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100323 enum elv_merge ret = blk_try_merge(q->last_merge, bio);
324
Tejun Heo06b86242005-10-20 16:46:23 +0200325 if (ret != ELEVATOR_NO_MERGE) {
326 *req = q->last_merge;
327 return ret;
328 }
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100331 if (blk_queue_noxmerges(q))
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200332 return ELEVATOR_NO_MERGE;
333
Jens Axboe98170642006-07-28 09:23:08 +0200334 /*
335 * See if our hash lookup can find a potential backmerge.
336 */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700337 __rq = elv_rqhash_find(q, bio->bi_iter.bi_sector);
Tahsin Erdogan72ef7992016-07-07 11:48:22 -0700338 if (__rq && elv_bio_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200339 *req = __rq;
340 return ELEVATOR_BACK_MERGE;
341 }
342
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600343 if (e->type->ops.request_merge)
344 return e->type->ops.request_merge(q, req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 return ELEVATOR_NO_MERGE;
347}
348
Jens Axboe5e84ea32011-03-21 10:14:27 +0100349/*
350 * Attempt to do an insertion back merge. Only check for the case where
351 * we can append 'rq' to an existing request, so we can throw 'rq' away
352 * afterwards.
353 *
354 * Returns true if we merged, false otherwise
355 */
Jens Axboebd166ef2017-01-17 06:03:22 -0700356bool elv_attempt_insert_merge(struct request_queue *q, struct request *rq)
Jens Axboe5e84ea32011-03-21 10:14:27 +0100357{
358 struct request *__rq;
Shaohua Libee03932012-11-09 08:44:27 +0100359 bool ret;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100360
361 if (blk_queue_nomerges(q))
362 return false;
363
364 /*
365 * First try one-hit cache.
366 */
367 if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq))
368 return true;
369
370 if (blk_queue_noxmerges(q))
371 return false;
372
Shaohua Libee03932012-11-09 08:44:27 +0100373 ret = false;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100374 /*
375 * See if our hash lookup can find a potential backmerge.
376 */
Shaohua Libee03932012-11-09 08:44:27 +0100377 while (1) {
378 __rq = elv_rqhash_find(q, blk_rq_pos(rq));
379 if (!__rq || !blk_attempt_req_merge(q, __rq, rq))
380 break;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100381
Shaohua Libee03932012-11-09 08:44:27 +0100382 /* The merged request could be merged with others, try again */
383 ret = true;
384 rq = __rq;
385 }
386
387 return ret;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100388}
389
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100390void elv_merged_request(struct request_queue *q, struct request *rq,
391 enum elv_merge type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Jens Axboeb374d182008-10-31 10:05:07 +0100393 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600395 if (e->type->ops.request_merged)
396 e->type->ops.request_merged(q, rq, type);
Tejun Heo06b86242005-10-20 16:46:23 +0200397
Jens Axboe2e662b62006-07-13 11:55:04 +0200398 if (type == ELEVATOR_BACK_MERGE)
399 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200400
Tejun Heo06b86242005-10-20 16:46:23 +0200401 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Jens Axboe165125e2007-07-24 09:28:11 +0200404void elv_merge_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 struct request *next)
406{
Jens Axboeb374d182008-10-31 10:05:07 +0100407 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600409 if (e->type->ops.requests_merged)
410 e->type->ops.requests_merged(q, rq, next);
Tejun Heo06b86242005-10-20 16:46:23 +0200411
Jens Axboe98170642006-07-28 09:23:08 +0200412 elv_rqhash_reposition(q, rq);
Tejun Heo06b86242005-10-20 16:46:23 +0200413 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Jens Axboe165125e2007-07-24 09:28:11 +0200416struct request *elv_latter_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Jens Axboeb374d182008-10-31 10:05:07 +0100418 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600420 if (e->type->ops.next_request)
421 return e->type->ops.next_request(q, rq);
Jens Axboebd166ef2017-01-17 06:03:22 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return NULL;
424}
425
Jens Axboe165125e2007-07-24 09:28:11 +0200426struct request *elv_former_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Jens Axboeb374d182008-10-31 10:05:07 +0100428 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600430 if (e->type->ops.former_request)
431 return e->type->ops.former_request(q, rq);
Jens Axboea1ce35f2018-10-29 10:23:51 -0600432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return NULL;
434}
435
Al Viro3d1ab402006-03-18 18:35:43 -0500436#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
437
438static ssize_t
439elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
440{
Al Viro3d1ab402006-03-18 18:35:43 -0500441 struct elv_fs_entry *entry = to_elv(attr);
Jens Axboeb374d182008-10-31 10:05:07 +0100442 struct elevator_queue *e;
Al Viro3d1ab402006-03-18 18:35:43 -0500443 ssize_t error;
444
445 if (!entry->show)
446 return -EIO;
447
Jens Axboeb374d182008-10-31 10:05:07 +0100448 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500449 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100450 error = e->type ? entry->show(e, page) : -ENOENT;
Al Viro3d1ab402006-03-18 18:35:43 -0500451 mutex_unlock(&e->sysfs_lock);
452 return error;
453}
454
455static ssize_t
456elv_attr_store(struct kobject *kobj, struct attribute *attr,
457 const char *page, size_t length)
458{
Al Viro3d1ab402006-03-18 18:35:43 -0500459 struct elv_fs_entry *entry = to_elv(attr);
Jens Axboeb374d182008-10-31 10:05:07 +0100460 struct elevator_queue *e;
Al Viro3d1ab402006-03-18 18:35:43 -0500461 ssize_t error;
462
463 if (!entry->store)
464 return -EIO;
465
Jens Axboeb374d182008-10-31 10:05:07 +0100466 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500467 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100468 error = e->type ? entry->store(e, page, length) : -ENOENT;
Al Viro3d1ab402006-03-18 18:35:43 -0500469 mutex_unlock(&e->sysfs_lock);
470 return error;
471}
472
Emese Revfy52cf25d2010-01-19 02:58:23 +0100473static const struct sysfs_ops elv_sysfs_ops = {
Al Viro3d1ab402006-03-18 18:35:43 -0500474 .show = elv_attr_show,
475 .store = elv_attr_store,
476};
477
478static struct kobj_type elv_ktype = {
479 .sysfs_ops = &elv_sysfs_ops,
480 .release = elevator_release,
481};
482
Ming Leicecf5d82019-08-27 19:01:48 +0800483/*
484 * elv_register_queue is called from either blk_register_queue or
485 * elevator_switch, elevator switch is prevented from being happen
486 * in the two paths, so it is safe to not hold q->sysfs_lock.
487 */
488int elv_register_queue(struct request_queue *q, bool uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Tejun Heo5a5bafd2012-03-05 13:14:56 -0800490 struct elevator_queue *e = q->elevator;
Al Viro3d1ab402006-03-18 18:35:43 -0500491 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700493 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
Al Viro3d1ab402006-03-18 18:35:43 -0500494 if (!error) {
Tejun Heo22f746e2011-12-14 00:33:41 +0100495 struct elv_fs_entry *attr = e->type->elevator_attrs;
Al Viro3d1ab402006-03-18 18:35:43 -0500496 if (attr) {
Al Viroe572ec72006-03-18 22:27:18 -0500497 while (attr->attr.name) {
498 if (sysfs_create_file(&e->kobj, &attr->attr))
Al Viro3d1ab402006-03-18 18:35:43 -0500499 break;
Al Viroe572ec72006-03-18 22:27:18 -0500500 attr++;
Al Viro3d1ab402006-03-18 18:35:43 -0500501 }
502 }
Ming Leicecf5d82019-08-27 19:01:48 +0800503 if (uevent)
504 kobject_uevent(&e->kobj, KOBJ_ADD);
505
Jens Axboe430c62f2010-10-07 09:35:16 +0200506 e->registered = 1;
Al Viro3d1ab402006-03-18 18:35:43 -0500507 }
508 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
Jens Axboebc1c1162006-06-08 08:49:06 +0200510
Ming Leicecf5d82019-08-27 19:01:48 +0800511/*
512 * elv_unregister_queue is called from either blk_unregister_queue or
513 * elevator_switch, elevator switch is prevented from being happen
514 * in the two paths, so it is safe to not hold q->sysfs_lock.
515 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516void elv_unregister_queue(struct request_queue *q)
517{
Tejun Heof8fc8772011-12-14 00:33:40 +0100518 if (q) {
519 struct elevator_queue *e = q->elevator;
520
521 kobject_uevent(&e->kobj, KOBJ_REMOVE);
522 kobject_del(&e->kobj);
Ming Leicecf5d82019-08-27 19:01:48 +0800523
Tejun Heof8fc8772011-12-14 00:33:40 +0100524 e->registered = 0;
Jan Kara8330cdb2017-04-19 11:33:27 +0200525 /* Re-enable throttling in case elevator disabled it */
526 wbt_enable_default(q);
Tejun Heof8fc8772011-12-14 00:33:40 +0100527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Jens Axboee567bf72014-06-22 16:32:48 -0600530int elv_register(struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Tejun Heo3d3c2372011-12-14 00:33:42 +0100532 /* create icq_cache if requested */
533 if (e->icq_size) {
534 if (WARN_ON(e->icq_size < sizeof(struct io_cq)) ||
535 WARN_ON(e->icq_align < __alignof__(struct io_cq)))
536 return -EINVAL;
537
538 snprintf(e->icq_cache_name, sizeof(e->icq_cache_name),
539 "%s_io_cq", e->elevator_name);
540 e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size,
541 e->icq_align, 0, NULL);
542 if (!e->icq_cache)
543 return -ENOMEM;
544 }
545
546 /* register, don't allow duplicate names */
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200547 spin_lock(&elv_list_lock);
Damien Le Moal68c43f12019-09-05 18:51:31 +0900548 if (elevator_find(e->elevator_name, 0)) {
Tejun Heo3d3c2372011-12-14 00:33:42 +0100549 spin_unlock(&elv_list_lock);
Chengguang Xu62d2a192018-08-28 07:31:11 +0800550 kmem_cache_destroy(e->icq_cache);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100551 return -EBUSY;
552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 list_add_tail(&e->list, &elv_list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200554 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Hisao Tanabed0b0a812019-04-08 00:27:42 +0900556 printk(KERN_INFO "io scheduler %s registered\n", e->elevator_name);
557
Tejun Heo3d3c2372011-12-14 00:33:42 +0100558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560EXPORT_SYMBOL_GPL(elv_register);
561
562void elv_unregister(struct elevator_type *e)
563{
Tejun Heo3d3c2372011-12-14 00:33:42 +0100564 /* unregister */
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200565 spin_lock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 list_del_init(&e->list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200567 spin_unlock(&elv_list_lock);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100568
569 /*
570 * Destroy icq_cache if it exists. icq's are RCU managed. Make
571 * sure all RCU operations are complete before proceeding.
572 */
573 if (e->icq_cache) {
574 rcu_barrier();
575 kmem_cache_destroy(e->icq_cache);
576 e->icq_cache = NULL;
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579EXPORT_SYMBOL_GPL(elv_unregister);
580
Jianchao Wangd48ece22018-08-21 15:15:03 +0800581int elevator_switch_mq(struct request_queue *q,
Omar Sandoval54d53292017-04-07 08:52:27 -0600582 struct elevator_type *new_e)
583{
584 int ret;
585
Bart Van Assche14a23492018-01-17 11:48:09 -0800586 lockdep_assert_held(&q->sysfs_lock);
587
Omar Sandoval54d53292017-04-07 08:52:27 -0600588 if (q->elevator) {
Ming Leib89f6252019-09-23 23:12:09 +0800589 if (q->elevator->registered)
Omar Sandoval54d53292017-04-07 08:52:27 -0600590 elv_unregister_queue(q);
Ming Leicecf5d82019-08-27 19:01:48 +0800591
Omar Sandoval54d53292017-04-07 08:52:27 -0600592 ioc_clear_queue(q);
593 elevator_exit(q, q->elevator);
594 }
595
596 ret = blk_mq_init_sched(q, new_e);
597 if (ret)
598 goto out;
599
600 if (new_e) {
Ming Leicecf5d82019-08-27 19:01:48 +0800601 ret = elv_register_queue(q, true);
Omar Sandoval54d53292017-04-07 08:52:27 -0600602 if (ret) {
603 elevator_exit(q, q->elevator);
604 goto out;
605 }
606 }
607
608 if (new_e)
609 blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name);
610 else
611 blk_add_trace_msg(q, "elv switch: none");
612
613out:
Omar Sandoval54d53292017-04-07 08:52:27 -0600614 return ret;
Omar Sandoval54d53292017-04-07 08:52:27 -0600615}
616
Damien Le Moal61db4372019-09-05 18:51:29 +0900617static inline bool elv_support_iosched(struct request_queue *q)
618{
Damien Le Moal7a7c5e72019-10-09 07:39:54 +0900619 if (!q->mq_ops ||
620 (q->tag_set && (q->tag_set->flags & BLK_MQ_F_NO_SCHED)))
Damien Le Moal61db4372019-09-05 18:51:29 +0900621 return false;
622 return true;
623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/*
Damien Le Moala0958ba2019-09-05 18:51:32 +0900626 * For single queue devices, default to using mq-deadline. If we have multiple
627 * queues or mq-deadline is not available, default to "none".
628 */
629static struct elevator_type *elevator_get_default(struct request_queue *q)
630{
631 if (q->nr_hw_queues != 1)
632 return NULL;
633
634 return elevator_get(q, "mq-deadline", false);
635}
636
637/*
638 * Get the first elevator providing the features required by the request queue.
639 * Default to "none" if no matching elevator is found.
640 */
641static struct elevator_type *elevator_get_by_features(struct request_queue *q)
642{
Jens Axboea2614252019-09-06 07:02:31 -0600643 struct elevator_type *e, *found = NULL;
Damien Le Moala0958ba2019-09-05 18:51:32 +0900644
645 spin_lock(&elv_list_lock);
646
647 list_for_each_entry(e, &elv_list, list) {
648 if (elv_support_features(e->elevator_features,
Jens Axboea2614252019-09-06 07:02:31 -0600649 q->required_elevator_features)) {
650 found = e;
Damien Le Moala0958ba2019-09-05 18:51:32 +0900651 break;
Jens Axboea2614252019-09-06 07:02:31 -0600652 }
Damien Le Moala0958ba2019-09-05 18:51:32 +0900653 }
654
Jens Axboea2614252019-09-06 07:02:31 -0600655 if (found && !try_module_get(found->elevator_owner))
656 found = NULL;
Damien Le Moala0958ba2019-09-05 18:51:32 +0900657
658 spin_unlock(&elv_list_lock);
Jens Axboea2614252019-09-06 07:02:31 -0600659 return found;
Damien Le Moala0958ba2019-09-05 18:51:32 +0900660}
661
662/*
663 * For a device queue that has no required features, use the default elevator
664 * settings. Otherwise, use the first elevator available matching the required
665 * features. If no suitable elevator is find or if the chosen elevator
666 * initialization fails, fall back to the "none" elevator (no elevator).
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200667 */
Damien Le Moal954b4a52019-09-05 18:51:30 +0900668void elevator_init_mq(struct request_queue *q)
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200669{
670 struct elevator_type *e;
Damien Le Moal954b4a52019-09-05 18:51:30 +0900671 int err;
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200672
Damien Le Moal61db4372019-09-05 18:51:29 +0900673 if (!elv_support_iosched(q))
Damien Le Moal954b4a52019-09-05 18:51:30 +0900674 return;
Damien Le Moal61db4372019-09-05 18:51:29 +0900675
Ming Leic48dac12019-08-27 19:01:45 +0800676 WARN_ON_ONCE(test_bit(QUEUE_FLAG_REGISTERED, &q->queue_flags));
677
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200678 if (unlikely(q->elevator))
Damien Le Moal954b4a52019-09-05 18:51:30 +0900679 return;
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200680
Damien Le Moala0958ba2019-09-05 18:51:32 +0900681 if (!q->required_elevator_features)
682 e = elevator_get_default(q);
683 else
684 e = elevator_get_by_features(q);
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200685 if (!e)
Damien Le Moal954b4a52019-09-05 18:51:30 +0900686 return;
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200687
Damien Le Moal737eb782019-09-05 18:51:33 +0900688 blk_mq_freeze_queue(q);
689 blk_mq_quiesce_queue(q);
690
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200691 err = blk_mq_init_sched(q, e);
Damien Le Moal737eb782019-09-05 18:51:33 +0900692
693 blk_mq_unquiesce_queue(q);
694 blk_mq_unfreeze_queue(q);
695
Damien Le Moal954b4a52019-09-05 18:51:30 +0900696 if (err) {
697 pr_warn("\"%s\" elevator initialization failed, "
698 "falling back to \"none\"\n", e->elevator_name);
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200699 elevator_put(e);
Damien Le Moal954b4a52019-09-05 18:51:30 +0900700 }
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200701}
702
703
704/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 * switch to new_e io scheduler. be careful not to introduce deadlocks -
706 * we don't free the old io scheduler, before we have allocated what we
707 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +0200708 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
Jens Axboe165125e2007-07-24 09:28:11 +0200710static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800712 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Bart Van Assche14a23492018-01-17 11:48:09 -0800714 lockdep_assert_held(&q->sysfs_lock);
715
Jens Axboea1ce35f2018-10-29 10:23:51 -0600716 blk_mq_freeze_queue(q);
717 blk_mq_quiesce_queue(q);
Jianchao Wangd48ece22018-08-21 15:15:03 +0800718
Jens Axboea1ce35f2018-10-29 10:23:51 -0600719 err = elevator_switch_mq(q, new_e);
Jianchao Wangd48ece22018-08-21 15:15:03 +0800720
Jens Axboea1ce35f2018-10-29 10:23:51 -0600721 blk_mq_unquiesce_queue(q);
722 blk_mq_unfreeze_queue(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200723
Jens Axboe5dd531a2010-08-23 13:52:19 +0200724 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Jens Axboe5dd531a2010-08-23 13:52:19 +0200727/*
728 * Switch this queue to the given IO scheduler.
729 */
Tomoki Sekiyama7c8a3672013-10-15 16:42:19 -0600730static int __elevator_change(struct request_queue *q, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 char elevator_name[ELV_NAME_MAX];
733 struct elevator_type *e;
734
David Jefferye9a823f2017-08-28 10:52:44 -0600735 /* Make sure queue is not in the middle of being removed */
Ming Lei58c898b2019-08-27 19:01:47 +0800736 if (!blk_queue_registered(q))
David Jefferye9a823f2017-08-28 10:52:44 -0600737 return -ENOENT;
738
Jens Axboebd166ef2017-01-17 06:03:22 -0700739 /*
740 * Special case for mq, turn off scheduling
741 */
Aleksei Zakharovfbd72122019-02-11 13:50:37 +0300742 if (!strncmp(name, "none", 4)) {
743 if (!q->elevator)
744 return 0;
Jens Axboebd166ef2017-01-17 06:03:22 -0700745 return elevator_switch(q, NULL);
Aleksei Zakharovfbd72122019-02-11 13:50:37 +0300746 }
Martin K. Petersencd43e262009-05-22 17:17:52 -0400747
Li Zefanee2e9922008-10-14 08:49:56 +0200748 strlcpy(elevator_name, name, sizeof(elevator_name));
Jens Axboe2527d992017-10-25 12:33:42 -0600749 e = elevator_get(q, strstrip(elevator_name), true);
Jens Axboe340ff322017-05-10 07:40:04 -0600750 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Damien Le Moal68c43f12019-09-05 18:51:31 +0900753 if (q->elevator &&
754 elevator_match(q->elevator->type, elevator_name, 0)) {
Nate Diller2ca7d932005-10-30 15:02:24 -0800755 elevator_put(e);
Jens Axboe5dd531a2010-08-23 13:52:19 +0200756 return 0;
Nate Diller2ca7d932005-10-30 15:02:24 -0800757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Jens Axboe5dd531a2010-08-23 13:52:19 +0200759 return elevator_switch(q, e);
760}
Tomoki Sekiyama7c8a3672013-10-15 16:42:19 -0600761
Jens Axboe5dd531a2010-08-23 13:52:19 +0200762ssize_t elv_iosched_store(struct request_queue *q, const char *name,
763 size_t count)
764{
765 int ret;
766
Jens Axboe344e9ff2018-11-15 12:22:51 -0700767 if (!queue_is_mq(q) || !elv_support_iosched(q))
Jens Axboe5dd531a2010-08-23 13:52:19 +0200768 return count;
769
Tomoki Sekiyama7c8a3672013-10-15 16:42:19 -0600770 ret = __elevator_change(q, name);
Jens Axboe5dd531a2010-08-23 13:52:19 +0200771 if (!ret)
772 return count;
773
Jens Axboe5dd531a2010-08-23 13:52:19 +0200774 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Jens Axboe165125e2007-07-24 09:28:11 +0200777ssize_t elv_iosched_show(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Jens Axboeb374d182008-10-31 10:05:07 +0100779 struct elevator_queue *e = q->elevator;
Jens Axboebd166ef2017-01-17 06:03:22 -0700780 struct elevator_type *elv = NULL;
Matthias Kaehlcke70cee262007-07-10 12:26:24 +0200781 struct elevator_type *__e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 int len = 0;
783
Jens Axboe344e9ff2018-11-15 12:22:51 -0700784 if (!queue_is_mq(q))
Martin K. Petersencd43e262009-05-22 17:17:52 -0400785 return sprintf(name, "none\n");
786
Jens Axboebd166ef2017-01-17 06:03:22 -0700787 if (!q->elevator)
788 len += sprintf(name+len, "[none] ");
789 else
790 elv = e->type;
Martin K. Petersencd43e262009-05-22 17:17:52 -0400791
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200792 spin_lock(&elv_list_lock);
Matthias Kaehlcke70cee262007-07-10 12:26:24 +0200793 list_for_each_entry(__e, &elv_list, list) {
Damien Le Moal68c43f12019-09-05 18:51:31 +0900794 if (elv && elevator_match(elv, __e->elevator_name, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 len += sprintf(name+len, "[%s] ", elv->elevator_name);
Jens Axboebd166ef2017-01-17 06:03:22 -0700796 continue;
797 }
Damien Le Moal68c43f12019-09-05 18:51:31 +0900798 if (elv_support_iosched(q) &&
799 elevator_match(__e, __e->elevator_name,
800 q->required_elevator_features))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 len += sprintf(name+len, "%s ", __e->elevator_name);
802 }
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200803 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Jens Axboe344e9ff2018-11-15 12:22:51 -0700805 if (q->elevator)
Jens Axboebd166ef2017-01-17 06:03:22 -0700806 len += sprintf(name+len, "none");
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 len += sprintf(len+name, "\n");
809 return len;
810}
811
Jens Axboe165125e2007-07-24 09:28:11 +0200812struct request *elv_rb_former_request(struct request_queue *q,
813 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +0200814{
815 struct rb_node *rbprev = rb_prev(&rq->rb_node);
816
817 if (rbprev)
818 return rb_entry_rq(rbprev);
819
820 return NULL;
821}
Jens Axboe2e662b62006-07-13 11:55:04 +0200822EXPORT_SYMBOL(elv_rb_former_request);
823
Jens Axboe165125e2007-07-24 09:28:11 +0200824struct request *elv_rb_latter_request(struct request_queue *q,
825 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +0200826{
827 struct rb_node *rbnext = rb_next(&rq->rb_node);
828
829 if (rbnext)
830 return rb_entry_rq(rbnext);
831
832 return NULL;
833}
Jens Axboe2e662b62006-07-13 11:55:04 +0200834EXPORT_SYMBOL(elv_rb_latter_request);
Jan Karaf8db3832019-11-06 11:48:57 +0100835
836static int __init elevator_setup(char *str)
837{
838 pr_warn("Kernel parameter elevator= does not have any effect anymore.\n"
839 "Please use sysfs to set IO scheduler for individual devices.\n");
840 return 1;
841}
842
843__setup("elevator=", elevator_setup);