blob: ec55d5fc0b3e75cf369182d61c6009cd7791c35e [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>
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
Jens Axboe8ac0d9a2017-10-25 12:35:02 -060086static bool elevator_match(const struct elevator_type *e, const char *name)
87{
88 if (!strcmp(e->elevator_name, name))
89 return true;
90 if (e->elevator_alias && !strcmp(e->elevator_alias, name))
91 return true;
92
93 return false;
94}
95
Jens Axboe2527d992017-10-25 12:33:42 -060096/*
Jens Axboea1ce35f2018-10-29 10:23:51 -060097 * Return scheduler with name 'name'
Jens Axboe2527d992017-10-25 12:33:42 -060098 */
Jens Axboea1ce35f2018-10-29 10:23:51 -060099static struct elevator_type *elevator_find(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Vasily Tarasova22b1692006-10-11 09:24:27 +0200101 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Matthias Kaehlcke70cee262007-07-10 12:26:24 +0200103 list_for_each_entry(e, &elv_list, list) {
Jens Axboea1ce35f2018-10-29 10:23:51 -0600104 if (elevator_match(e, name))
Vasily Tarasova22b1692006-10-11 09:24:27 +0200105 return e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Vasily Tarasova22b1692006-10-11 09:24:27 +0200108 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111static void elevator_put(struct elevator_type *e)
112{
113 module_put(e->elevator_owner);
114}
115
Jens Axboe2527d992017-10-25 12:33:42 -0600116static struct elevator_type *elevator_get(struct request_queue *q,
117 const char *name, bool try_loading)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Tejun Heo2824bc932005-10-20 10:56:41 +0200119 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200121 spin_lock(&elv_list_lock);
Tejun Heo2824bc932005-10-20 10:56:41 +0200122
Jens Axboea1ce35f2018-10-29 10:23:51 -0600123 e = elevator_find(name);
Tejun Heo21c3c5d2013-01-22 16:48:03 -0800124 if (!e && try_loading) {
Jens Axboee1640942008-02-19 10:20:37 +0100125 spin_unlock(&elv_list_lock);
Kees Cook490b94b2011-05-05 18:02:12 -0600126 request_module("%s-iosched", name);
Jens Axboee1640942008-02-19 10:20:37 +0100127 spin_lock(&elv_list_lock);
Jens Axboea1ce35f2018-10-29 10:23:51 -0600128 e = elevator_find(name);
Jens Axboee1640942008-02-19 10:20:37 +0100129 }
130
Tejun Heo2824bc932005-10-20 10:56:41 +0200131 if (e && !try_module_get(e->elevator_owner))
132 e = NULL;
133
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200134 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return e;
136}
137
Wang Sheng-Hui484fc252011-09-08 12:32:14 +0200138static char chosen_elevator[ELV_NAME_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Nate Diller5f003972006-01-24 10:07:58 +0100140static int __init elevator_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100142 /*
143 * Be backwards-compatible with previous kernels, so users
144 * won't get the wrong elevator.
145 */
Jens Axboe492af632009-10-03 09:37:51 +0200146 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800147 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150__setup("elevator=", elevator_setup);
151
Al Viro3d1ab402006-03-18 18:35:43 -0500152static struct kobj_type elv_ktype;
153
Jianpeng Mad50235b2013-07-03 13:25:24 +0200154struct elevator_queue *elevator_alloc(struct request_queue *q,
Jens Axboe165125e2007-07-24 09:28:11 +0200155 struct elevator_type *e)
Al Viro3d1ab402006-03-18 18:35:43 -0500156{
Jens Axboeb374d182008-10-31 10:05:07 +0100157 struct elevator_queue *eq;
Jens Axboe98170642006-07-28 09:23:08 +0200158
Joe Perchesc1b511e2013-08-29 15:21:42 -0700159 eq = kzalloc_node(sizeof(*eq), GFP_KERNEL, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200160 if (unlikely(!eq))
Chao Yu8406a4d2015-04-23 10:47:44 -0600161 return NULL;
Jens Axboe98170642006-07-28 09:23:08 +0200162
Tejun Heo22f746e2011-12-14 00:33:41 +0100163 eq->type = e;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700164 kobject_init(&eq->kobj, &elv_ktype);
Jens Axboe98170642006-07-28 09:23:08 +0200165 mutex_init(&eq->sysfs_lock);
Sasha Levin242d98f2012-12-17 10:01:27 -0500166 hash_init(eq->hash);
Jens Axboe98170642006-07-28 09:23:08 +0200167
Al Viro3d1ab402006-03-18 18:35:43 -0500168 return eq;
169}
Jianpeng Mad50235b2013-07-03 13:25:24 +0200170EXPORT_SYMBOL(elevator_alloc);
Al Viro3d1ab402006-03-18 18:35:43 -0500171
172static void elevator_release(struct kobject *kobj)
173{
Jens Axboeb374d182008-10-31 10:05:07 +0100174 struct elevator_queue *e;
Jens Axboe98170642006-07-28 09:23:08 +0200175
Jens Axboeb374d182008-10-31 10:05:07 +0100176 e = container_of(kobj, struct elevator_queue, kobj);
Tejun Heo22f746e2011-12-14 00:33:41 +0100177 elevator_put(e->type);
Al Viro3d1ab402006-03-18 18:35:43 -0500178 kfree(e);
179}
180
Omar Sandoval54d53292017-04-07 08:52:27 -0600181void elevator_exit(struct request_queue *q, struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Al Viro3d1ab402006-03-18 18:35:43 -0500183 mutex_lock(&e->sysfs_lock);
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600184 if (e->type->ops.exit_sched)
Omar Sandoval54d53292017-04-07 08:52:27 -0600185 blk_mq_exit_sched(q, e);
Al Viro3d1ab402006-03-18 18:35:43 -0500186 mutex_unlock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Al Viro3d1ab402006-03-18 18:35:43 -0500188 kobject_put(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
Jens Axboe2e662b62006-07-13 11:55:04 +0200190
Jens Axboe98170642006-07-28 09:23:08 +0200191static inline void __elv_rqhash_del(struct request *rq)
192{
Sasha Levin242d98f2012-12-17 10:01:27 -0500193 hash_del(&rq->hash);
Christoph Hellwige8064022016-10-20 15:12:13 +0200194 rq->rq_flags &= ~RQF_HASHED;
Jens Axboe98170642006-07-28 09:23:08 +0200195}
196
Jens Axboe70b3ea02016-12-07 08:43:31 -0700197void elv_rqhash_del(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200198{
199 if (ELV_ON_HASH(rq))
200 __elv_rqhash_del(rq);
201}
Jens Axboebd166ef2017-01-17 06:03:22 -0700202EXPORT_SYMBOL_GPL(elv_rqhash_del);
Jens Axboe98170642006-07-28 09:23:08 +0200203
Jens Axboe70b3ea02016-12-07 08:43:31 -0700204void elv_rqhash_add(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200205{
Jens Axboeb374d182008-10-31 10:05:07 +0100206 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200207
208 BUG_ON(ELV_ON_HASH(rq));
Sasha Levin242d98f2012-12-17 10:01:27 -0500209 hash_add(e->hash, &rq->hash, rq_hash_key(rq));
Christoph Hellwige8064022016-10-20 15:12:13 +0200210 rq->rq_flags |= RQF_HASHED;
Jens Axboe98170642006-07-28 09:23:08 +0200211}
Jens Axboebd166ef2017-01-17 06:03:22 -0700212EXPORT_SYMBOL_GPL(elv_rqhash_add);
Jens Axboe98170642006-07-28 09:23:08 +0200213
Jens Axboe70b3ea02016-12-07 08:43:31 -0700214void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200215{
216 __elv_rqhash_del(rq);
217 elv_rqhash_add(q, rq);
218}
219
Jens Axboe70b3ea02016-12-07 08:43:31 -0700220struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
Jens Axboe98170642006-07-28 09:23:08 +0200221{
Jens Axboeb374d182008-10-31 10:05:07 +0100222 struct elevator_queue *e = q->elevator;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800223 struct hlist_node *next;
Jens Axboe98170642006-07-28 09:23:08 +0200224 struct request *rq;
225
Linus Torvaldsee89f812013-02-28 12:52:24 -0800226 hash_for_each_possible_safe(e->hash, rq, next, hash, offset) {
Jens Axboe98170642006-07-28 09:23:08 +0200227 BUG_ON(!ELV_ON_HASH(rq));
228
229 if (unlikely(!rq_mergeable(rq))) {
230 __elv_rqhash_del(rq);
231 continue;
232 }
233
234 if (rq_hash_key(rq) == offset)
235 return rq;
236 }
237
238 return NULL;
239}
240
Tejun Heo8922e162005-10-20 16:23:44 +0200241/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200242 * RB-tree support functions for inserting/lookup/removal of requests
243 * in a sorted RB tree.
244 */
Jeff Moyer796d5112011-06-02 21:19:05 +0200245void elv_rb_add(struct rb_root *root, struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +0200246{
247 struct rb_node **p = &root->rb_node;
248 struct rb_node *parent = NULL;
249 struct request *__rq;
250
251 while (*p) {
252 parent = *p;
253 __rq = rb_entry(parent, struct request, rb_node);
254
Tejun Heo83096eb2009-05-07 22:24:39 +0900255 if (blk_rq_pos(rq) < blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200256 p = &(*p)->rb_left;
Jeff Moyer796d5112011-06-02 21:19:05 +0200257 else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200258 p = &(*p)->rb_right;
Jens Axboe2e662b62006-07-13 11:55:04 +0200259 }
260
261 rb_link_node(&rq->rb_node, parent, p);
262 rb_insert_color(&rq->rb_node, root);
Jens Axboe2e662b62006-07-13 11:55:04 +0200263}
Jens Axboe2e662b62006-07-13 11:55:04 +0200264EXPORT_SYMBOL(elv_rb_add);
265
266void elv_rb_del(struct rb_root *root, struct request *rq)
267{
268 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
269 rb_erase(&rq->rb_node, root);
270 RB_CLEAR_NODE(&rq->rb_node);
271}
Jens Axboe2e662b62006-07-13 11:55:04 +0200272EXPORT_SYMBOL(elv_rb_del);
273
274struct request *elv_rb_find(struct rb_root *root, sector_t sector)
275{
276 struct rb_node *n = root->rb_node;
277 struct request *rq;
278
279 while (n) {
280 rq = rb_entry(n, struct request, rb_node);
281
Tejun Heo83096eb2009-05-07 22:24:39 +0900282 if (sector < blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200283 n = n->rb_left;
Tejun Heo83096eb2009-05-07 22:24:39 +0900284 else if (sector > blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200285 n = n->rb_right;
286 else
287 return rq;
288 }
289
290 return NULL;
291}
Jens Axboe2e662b62006-07-13 11:55:04 +0200292EXPORT_SYMBOL(elv_rb_find);
293
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100294enum elv_merge elv_merge(struct request_queue *q, struct request **req,
295 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Jens Axboeb374d182008-10-31 10:05:07 +0100297 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200298 struct request *__rq;
Tejun Heo06b86242005-10-20 16:46:23 +0200299
Jens Axboe98170642006-07-28 09:23:08 +0200300 /*
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100301 * Levels of merges:
302 * nomerges: No merges at all attempted
303 * noxmerges: Only simple one-hit cache try
304 * merges: All merge tries attempted
305 */
Ming Lei7460d382015-10-20 23:13:55 +0800306 if (blk_queue_nomerges(q) || !bio_mergeable(bio))
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100307 return ELEVATOR_NO_MERGE;
308
309 /*
Jens Axboe98170642006-07-28 09:23:08 +0200310 * First try one-hit cache.
311 */
Tahsin Erdogan72ef7992016-07-07 11:48:22 -0700312 if (q->last_merge && elv_bio_merge_ok(q->last_merge, bio)) {
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100313 enum elv_merge ret = blk_try_merge(q->last_merge, bio);
314
Tejun Heo06b86242005-10-20 16:46:23 +0200315 if (ret != ELEVATOR_NO_MERGE) {
316 *req = q->last_merge;
317 return ret;
318 }
319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100321 if (blk_queue_noxmerges(q))
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200322 return ELEVATOR_NO_MERGE;
323
Jens Axboe98170642006-07-28 09:23:08 +0200324 /*
325 * See if our hash lookup can find a potential backmerge.
326 */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700327 __rq = elv_rqhash_find(q, bio->bi_iter.bi_sector);
Tahsin Erdogan72ef7992016-07-07 11:48:22 -0700328 if (__rq && elv_bio_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200329 *req = __rq;
330 return ELEVATOR_BACK_MERGE;
331 }
332
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600333 if (e->type->ops.request_merge)
334 return e->type->ops.request_merge(q, req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 return ELEVATOR_NO_MERGE;
337}
338
Jens Axboe5e84ea32011-03-21 10:14:27 +0100339/*
340 * Attempt to do an insertion back merge. Only check for the case where
341 * we can append 'rq' to an existing request, so we can throw 'rq' away
342 * afterwards.
343 *
344 * Returns true if we merged, false otherwise
345 */
Jens Axboebd166ef2017-01-17 06:03:22 -0700346bool elv_attempt_insert_merge(struct request_queue *q, struct request *rq)
Jens Axboe5e84ea32011-03-21 10:14:27 +0100347{
348 struct request *__rq;
Shaohua Libee03932012-11-09 08:44:27 +0100349 bool ret;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100350
351 if (blk_queue_nomerges(q))
352 return false;
353
354 /*
355 * First try one-hit cache.
356 */
357 if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq))
358 return true;
359
360 if (blk_queue_noxmerges(q))
361 return false;
362
Shaohua Libee03932012-11-09 08:44:27 +0100363 ret = false;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100364 /*
365 * See if our hash lookup can find a potential backmerge.
366 */
Shaohua Libee03932012-11-09 08:44:27 +0100367 while (1) {
368 __rq = elv_rqhash_find(q, blk_rq_pos(rq));
369 if (!__rq || !blk_attempt_req_merge(q, __rq, rq))
370 break;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100371
Shaohua Libee03932012-11-09 08:44:27 +0100372 /* The merged request could be merged with others, try again */
373 ret = true;
374 rq = __rq;
375 }
376
377 return ret;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100378}
379
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100380void elv_merged_request(struct request_queue *q, struct request *rq,
381 enum elv_merge type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Jens Axboeb374d182008-10-31 10:05:07 +0100383 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600385 if (e->type->ops.request_merged)
386 e->type->ops.request_merged(q, rq, type);
Tejun Heo06b86242005-10-20 16:46:23 +0200387
Jens Axboe2e662b62006-07-13 11:55:04 +0200388 if (type == ELEVATOR_BACK_MERGE)
389 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200390
Tejun Heo06b86242005-10-20 16:46:23 +0200391 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Jens Axboe165125e2007-07-24 09:28:11 +0200394void elv_merge_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct request *next)
396{
Jens Axboeb374d182008-10-31 10:05:07 +0100397 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600399 if (e->type->ops.requests_merged)
400 e->type->ops.requests_merged(q, rq, next);
Tejun Heo06b86242005-10-20 16:46:23 +0200401
Jens Axboe98170642006-07-28 09:23:08 +0200402 elv_rqhash_reposition(q, rq);
Tejun Heo06b86242005-10-20 16:46:23 +0200403 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Jens Axboe165125e2007-07-24 09:28:11 +0200406struct request *elv_latter_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Jens Axboeb374d182008-10-31 10:05:07 +0100408 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600410 if (e->type->ops.next_request)
411 return e->type->ops.next_request(q, rq);
Jens Axboebd166ef2017-01-17 06:03:22 -0700412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return NULL;
414}
415
Jens Axboe165125e2007-07-24 09:28:11 +0200416struct request *elv_former_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.former_request)
421 return e->type->ops.former_request(q, rq);
Jens Axboea1ce35f2018-10-29 10:23:51 -0600422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return NULL;
424}
425
Al Viro3d1ab402006-03-18 18:35:43 -0500426#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
427
428static ssize_t
429elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
430{
Al Viro3d1ab402006-03-18 18:35:43 -0500431 struct elv_fs_entry *entry = to_elv(attr);
Jens Axboeb374d182008-10-31 10:05:07 +0100432 struct elevator_queue *e;
Al Viro3d1ab402006-03-18 18:35:43 -0500433 ssize_t error;
434
435 if (!entry->show)
436 return -EIO;
437
Jens Axboeb374d182008-10-31 10:05:07 +0100438 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500439 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100440 error = e->type ? entry->show(e, page) : -ENOENT;
Al Viro3d1ab402006-03-18 18:35:43 -0500441 mutex_unlock(&e->sysfs_lock);
442 return error;
443}
444
445static ssize_t
446elv_attr_store(struct kobject *kobj, struct attribute *attr,
447 const char *page, size_t length)
448{
Al Viro3d1ab402006-03-18 18:35:43 -0500449 struct elv_fs_entry *entry = to_elv(attr);
Jens Axboeb374d182008-10-31 10:05:07 +0100450 struct elevator_queue *e;
Al Viro3d1ab402006-03-18 18:35:43 -0500451 ssize_t error;
452
453 if (!entry->store)
454 return -EIO;
455
Jens Axboeb374d182008-10-31 10:05:07 +0100456 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500457 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100458 error = e->type ? entry->store(e, page, length) : -ENOENT;
Al Viro3d1ab402006-03-18 18:35:43 -0500459 mutex_unlock(&e->sysfs_lock);
460 return error;
461}
462
Emese Revfy52cf25d2010-01-19 02:58:23 +0100463static const struct sysfs_ops elv_sysfs_ops = {
Al Viro3d1ab402006-03-18 18:35:43 -0500464 .show = elv_attr_show,
465 .store = elv_attr_store,
466};
467
468static struct kobj_type elv_ktype = {
469 .sysfs_ops = &elv_sysfs_ops,
470 .release = elevator_release,
471};
472
Tejun Heo5a5bafd2012-03-05 13:14:56 -0800473int elv_register_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Tejun Heo5a5bafd2012-03-05 13:14:56 -0800475 struct elevator_queue *e = q->elevator;
Al Viro3d1ab402006-03-18 18:35:43 -0500476 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Bart Van Assche14a23492018-01-17 11:48:09 -0800478 lockdep_assert_held(&q->sysfs_lock);
479
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700480 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
Al Viro3d1ab402006-03-18 18:35:43 -0500481 if (!error) {
Tejun Heo22f746e2011-12-14 00:33:41 +0100482 struct elv_fs_entry *attr = e->type->elevator_attrs;
Al Viro3d1ab402006-03-18 18:35:43 -0500483 if (attr) {
Al Viroe572ec72006-03-18 22:27:18 -0500484 while (attr->attr.name) {
485 if (sysfs_create_file(&e->kobj, &attr->attr))
Al Viro3d1ab402006-03-18 18:35:43 -0500486 break;
Al Viroe572ec72006-03-18 22:27:18 -0500487 attr++;
Al Viro3d1ab402006-03-18 18:35:43 -0500488 }
489 }
490 kobject_uevent(&e->kobj, KOBJ_ADD);
Jens Axboe430c62f2010-10-07 09:35:16 +0200491 e->registered = 1;
Al Viro3d1ab402006-03-18 18:35:43 -0500492 }
493 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
Jens Axboebc1c1162006-06-08 08:49:06 +0200495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496void elv_unregister_queue(struct request_queue *q)
497{
Bart Van Assche14a23492018-01-17 11:48:09 -0800498 lockdep_assert_held(&q->sysfs_lock);
499
Tejun Heof8fc8772011-12-14 00:33:40 +0100500 if (q) {
501 struct elevator_queue *e = q->elevator;
502
503 kobject_uevent(&e->kobj, KOBJ_REMOVE);
504 kobject_del(&e->kobj);
505 e->registered = 0;
Jan Kara8330cdb2017-04-19 11:33:27 +0200506 /* Re-enable throttling in case elevator disabled it */
507 wbt_enable_default(q);
Tejun Heof8fc8772011-12-14 00:33:40 +0100508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Jens Axboee567bf72014-06-22 16:32:48 -0600511int elv_register(struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Tejun Heo3d3c2372011-12-14 00:33:42 +0100513 /* create icq_cache if requested */
514 if (e->icq_size) {
515 if (WARN_ON(e->icq_size < sizeof(struct io_cq)) ||
516 WARN_ON(e->icq_align < __alignof__(struct io_cq)))
517 return -EINVAL;
518
519 snprintf(e->icq_cache_name, sizeof(e->icq_cache_name),
520 "%s_io_cq", e->elevator_name);
521 e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size,
522 e->icq_align, 0, NULL);
523 if (!e->icq_cache)
524 return -ENOMEM;
525 }
526
527 /* register, don't allow duplicate names */
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200528 spin_lock(&elv_list_lock);
Jens Axboea1ce35f2018-10-29 10:23:51 -0600529 if (elevator_find(e->elevator_name)) {
Tejun Heo3d3c2372011-12-14 00:33:42 +0100530 spin_unlock(&elv_list_lock);
Chengguang Xu62d2a192018-08-28 07:31:11 +0800531 kmem_cache_destroy(e->icq_cache);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100532 return -EBUSY;
533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 list_add_tail(&e->list, &elv_list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200535 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Hisao Tanabed0b0a812019-04-08 00:27:42 +0900537 printk(KERN_INFO "io scheduler %s registered\n", e->elevator_name);
538
Tejun Heo3d3c2372011-12-14 00:33:42 +0100539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541EXPORT_SYMBOL_GPL(elv_register);
542
543void elv_unregister(struct elevator_type *e)
544{
Tejun Heo3d3c2372011-12-14 00:33:42 +0100545 /* unregister */
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200546 spin_lock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 list_del_init(&e->list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200548 spin_unlock(&elv_list_lock);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100549
550 /*
551 * Destroy icq_cache if it exists. icq's are RCU managed. Make
552 * sure all RCU operations are complete before proceeding.
553 */
554 if (e->icq_cache) {
555 rcu_barrier();
556 kmem_cache_destroy(e->icq_cache);
557 e->icq_cache = NULL;
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560EXPORT_SYMBOL_GPL(elv_unregister);
561
Jianchao Wangd48ece22018-08-21 15:15:03 +0800562int elevator_switch_mq(struct request_queue *q,
Omar Sandoval54d53292017-04-07 08:52:27 -0600563 struct elevator_type *new_e)
564{
565 int ret;
566
Bart Van Assche14a23492018-01-17 11:48:09 -0800567 lockdep_assert_held(&q->sysfs_lock);
568
Omar Sandoval54d53292017-04-07 08:52:27 -0600569 if (q->elevator) {
570 if (q->elevator->registered)
571 elv_unregister_queue(q);
572 ioc_clear_queue(q);
573 elevator_exit(q, q->elevator);
574 }
575
576 ret = blk_mq_init_sched(q, new_e);
577 if (ret)
578 goto out;
579
580 if (new_e) {
581 ret = elv_register_queue(q);
582 if (ret) {
583 elevator_exit(q, q->elevator);
584 goto out;
585 }
586 }
587
588 if (new_e)
589 blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name);
590 else
591 blk_add_trace_msg(q, "elv switch: none");
592
593out:
Omar Sandoval54d53292017-04-07 08:52:27 -0600594 return ret;
Omar Sandoval54d53292017-04-07 08:52:27 -0600595}
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597/*
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200598 * For blk-mq devices, we default to using mq-deadline, if available, for single
599 * queue devices. If deadline isn't available OR we have multiple queues,
600 * default to "none".
601 */
602int elevator_init_mq(struct request_queue *q)
603{
604 struct elevator_type *e;
605 int err = 0;
606
607 if (q->nr_hw_queues != 1)
608 return 0;
609
610 /*
611 * q->sysfs_lock must be held to provide mutual exclusion between
612 * elevator_switch() and here.
613 */
614 mutex_lock(&q->sysfs_lock);
615 if (unlikely(q->elevator))
616 goto out_unlock;
617
618 e = elevator_get(q, "mq-deadline", false);
619 if (!e)
620 goto out_unlock;
621
622 err = blk_mq_init_sched(q, e);
623 if (err)
624 elevator_put(e);
625out_unlock:
626 mutex_unlock(&q->sysfs_lock);
627 return err;
628}
629
630
631/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 * switch to new_e io scheduler. be careful not to introduce deadlocks -
633 * we don't free the old io scheduler, before we have allocated what we
634 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +0200635 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
Jens Axboe165125e2007-07-24 09:28:11 +0200637static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800639 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Bart Van Assche14a23492018-01-17 11:48:09 -0800641 lockdep_assert_held(&q->sysfs_lock);
642
Jens Axboea1ce35f2018-10-29 10:23:51 -0600643 blk_mq_freeze_queue(q);
644 blk_mq_quiesce_queue(q);
Jianchao Wangd48ece22018-08-21 15:15:03 +0800645
Jens Axboea1ce35f2018-10-29 10:23:51 -0600646 err = elevator_switch_mq(q, new_e);
Jianchao Wangd48ece22018-08-21 15:15:03 +0800647
Jens Axboea1ce35f2018-10-29 10:23:51 -0600648 blk_mq_unquiesce_queue(q);
649 blk_mq_unfreeze_queue(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200650
Jens Axboe5dd531a2010-08-23 13:52:19 +0200651 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Jens Axboe5dd531a2010-08-23 13:52:19 +0200654/*
655 * Switch this queue to the given IO scheduler.
656 */
Tomoki Sekiyama7c8a3672013-10-15 16:42:19 -0600657static int __elevator_change(struct request_queue *q, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 char elevator_name[ELV_NAME_MAX];
660 struct elevator_type *e;
661
David Jefferye9a823f2017-08-28 10:52:44 -0600662 /* Make sure queue is not in the middle of being removed */
663 if (!test_bit(QUEUE_FLAG_REGISTERED, &q->queue_flags))
664 return -ENOENT;
665
Jens Axboebd166ef2017-01-17 06:03:22 -0700666 /*
667 * Special case for mq, turn off scheduling
668 */
Aleksei Zakharovfbd72122019-02-11 13:50:37 +0300669 if (!strncmp(name, "none", 4)) {
670 if (!q->elevator)
671 return 0;
Jens Axboebd166ef2017-01-17 06:03:22 -0700672 return elevator_switch(q, NULL);
Aleksei Zakharovfbd72122019-02-11 13:50:37 +0300673 }
Martin K. Petersencd43e262009-05-22 17:17:52 -0400674
Li Zefanee2e9922008-10-14 08:49:56 +0200675 strlcpy(elevator_name, name, sizeof(elevator_name));
Jens Axboe2527d992017-10-25 12:33:42 -0600676 e = elevator_get(q, strstrip(elevator_name), true);
Jens Axboe340ff322017-05-10 07:40:04 -0600677 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Jens Axboe8ac0d9a2017-10-25 12:35:02 -0600680 if (q->elevator && elevator_match(q->elevator->type, elevator_name)) {
Nate Diller2ca7d932005-10-30 15:02:24 -0800681 elevator_put(e);
Jens Axboe5dd531a2010-08-23 13:52:19 +0200682 return 0;
Nate Diller2ca7d932005-10-30 15:02:24 -0800683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Jens Axboe5dd531a2010-08-23 13:52:19 +0200685 return elevator_switch(q, e);
686}
Tomoki Sekiyama7c8a3672013-10-15 16:42:19 -0600687
Ming Lei3a5088c2017-04-15 20:38:22 +0800688static inline bool elv_support_iosched(struct request_queue *q)
689{
Jens Axboe344e9ff2018-11-15 12:22:51 -0700690 if (q->tag_set && (q->tag_set->flags & BLK_MQ_F_NO_SCHED))
Ming Lei3a5088c2017-04-15 20:38:22 +0800691 return false;
692 return true;
693}
694
Jens Axboe5dd531a2010-08-23 13:52:19 +0200695ssize_t elv_iosched_store(struct request_queue *q, const char *name,
696 size_t count)
697{
698 int ret;
699
Jens Axboe344e9ff2018-11-15 12:22:51 -0700700 if (!queue_is_mq(q) || !elv_support_iosched(q))
Jens Axboe5dd531a2010-08-23 13:52:19 +0200701 return count;
702
Tomoki Sekiyama7c8a3672013-10-15 16:42:19 -0600703 ret = __elevator_change(q, name);
Jens Axboe5dd531a2010-08-23 13:52:19 +0200704 if (!ret)
705 return count;
706
Jens Axboe5dd531a2010-08-23 13:52:19 +0200707 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
Jens Axboe165125e2007-07-24 09:28:11 +0200710ssize_t elv_iosched_show(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Jens Axboeb374d182008-10-31 10:05:07 +0100712 struct elevator_queue *e = q->elevator;
Jens Axboebd166ef2017-01-17 06:03:22 -0700713 struct elevator_type *elv = NULL;
Matthias Kaehlcke70cee262007-07-10 12:26:24 +0200714 struct elevator_type *__e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 int len = 0;
716
Jens Axboe344e9ff2018-11-15 12:22:51 -0700717 if (!queue_is_mq(q))
Martin K. Petersencd43e262009-05-22 17:17:52 -0400718 return sprintf(name, "none\n");
719
Jens Axboebd166ef2017-01-17 06:03:22 -0700720 if (!q->elevator)
721 len += sprintf(name+len, "[none] ");
722 else
723 elv = e->type;
Martin K. Petersencd43e262009-05-22 17:17:52 -0400724
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200725 spin_lock(&elv_list_lock);
Matthias Kaehlcke70cee262007-07-10 12:26:24 +0200726 list_for_each_entry(__e, &elv_list, list) {
Jens Axboea1ce35f2018-10-29 10:23:51 -0600727 if (elv && elevator_match(elv, __e->elevator_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 len += sprintf(name+len, "[%s] ", elv->elevator_name);
Jens Axboebd166ef2017-01-17 06:03:22 -0700729 continue;
730 }
Jens Axboea1ce35f2018-10-29 10:23:51 -0600731 if (elv_support_iosched(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 len += sprintf(name+len, "%s ", __e->elevator_name);
733 }
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200734 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Jens Axboe344e9ff2018-11-15 12:22:51 -0700736 if (q->elevator)
Jens Axboebd166ef2017-01-17 06:03:22 -0700737 len += sprintf(name+len, "none");
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 len += sprintf(len+name, "\n");
740 return len;
741}
742
Jens Axboe165125e2007-07-24 09:28:11 +0200743struct request *elv_rb_former_request(struct request_queue *q,
744 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +0200745{
746 struct rb_node *rbprev = rb_prev(&rq->rb_node);
747
748 if (rbprev)
749 return rb_entry_rq(rbprev);
750
751 return NULL;
752}
Jens Axboe2e662b62006-07-13 11:55:04 +0200753EXPORT_SYMBOL(elv_rb_former_request);
754
Jens Axboe165125e2007-07-24 09:28:11 +0200755struct request *elv_rb_latter_request(struct request_queue *q,
756 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +0200757{
758 struct rb_node *rbnext = rb_next(&rq->rb_node);
759
760 if (rbnext)
761 return rb_entry_rq(rbnext);
762
763 return NULL;
764}
Jens Axboe2e662b62006-07-13 11:55:04 +0200765EXPORT_SYMBOL(elv_rb_latter_request);