blob: f05e90d4e695a3f5f5a04ae89c997cd11ad1263c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Block device elevator/IO-scheduler.
3 *
4 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 *
Jens Axboe0fe23472006-09-04 15:41:16 +02006 * 30042000 Jens Axboe <axboe@kernel.dk> :
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Split the elevator a bit so that it is possible to choose a different
9 * one or even write a new "plug in". There are three pieces:
10 * - elevator_fn, inserts a new request in the queue list
11 * - elevator_merge_fn, decides whether a new buffer can be merged with
12 * an existing request
13 * - elevator_dequeue_fn, called when a request is taken off the active list
14 *
15 * 20082000 Dave Jones <davej@suse.de> :
16 * Removed tests for max-bomb-segments, which was breaking elvtune
17 * when run without -bN
18 *
19 * Jens:
20 * - Rework again to work with bio instead of buffer_heads
21 * - loose bi_dev comparisons, partition handling is right now
22 * - completely modularize elevator setup and teardown
23 *
24 */
25#include <linux/kernel.h>
26#include <linux/fs.h>
27#include <linux/blkdev.h>
28#include <linux/elevator.h>
29#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
Jens Axboe242f9dc2008-09-14 05:55:09 -070042#include "blk.h"
Jens Axboebd166ef2017-01-17 06:03:22 -070043#include "blk-mq-sched.h"
Bart Van Asschebca6b062018-09-26 14:01:03 -070044#include "blk-pm.h"
Jan Kara8330cdb2017-04-19 11:33:27 +020045#include "blk-wbt.h"
Jens Axboe242f9dc2008-09-14 05:55:09 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static DEFINE_SPINLOCK(elv_list_lock);
48static LIST_HEAD(elv_list);
49
50/*
Jens Axboe98170642006-07-28 09:23:08 +020051 * Merge hash stuff.
52 */
Tejun Heo83096eb2009-05-07 22:24:39 +090053#define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
Jens Axboe98170642006-07-28 09:23:08 +020054
55/*
Jens Axboeda775262006-12-20 11:04:12 +010056 * Query io scheduler to see if the current process issuing bio may be
57 * merged with rq.
58 */
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070059static int elv_iosched_allow_bio_merge(struct request *rq, struct bio *bio)
Jens Axboeda775262006-12-20 11:04:12 +010060{
Jens Axboe165125e2007-07-24 09:28:11 +020061 struct request_queue *q = rq->q;
Jens Axboeb374d182008-10-31 10:05:07 +010062 struct elevator_queue *e = q->elevator;
Jens Axboeda775262006-12-20 11:04:12 +010063
Jens Axboef9cd4bf2018-11-01 16:41:41 -060064 if (e->type->ops.allow_merge)
65 return e->type->ops.allow_merge(q, rq, bio);
Jens Axboeda775262006-12-20 11:04:12 +010066
67 return 1;
68}
69
70/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * can we safely merge with this request?
72 */
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070073bool elv_bio_merge_ok(struct request *rq, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Tejun Heo050c8ea2012-02-08 09:19:38 +010075 if (!blk_rq_merge_ok(rq, bio))
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070076 return false;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020077
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070078 if (!elv_iosched_allow_bio_merge(rq, bio))
79 return false;
Jens Axboeda775262006-12-20 11:04:12 +010080
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070081 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
Tahsin Erdogan72ef7992016-07-07 11:48:22 -070083EXPORT_SYMBOL(elv_bio_merge_ok);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Jens Axboe8ac0d9a2017-10-25 12:35:02 -060085static bool elevator_match(const struct elevator_type *e, const char *name)
86{
87 if (!strcmp(e->elevator_name, name))
88 return true;
89 if (e->elevator_alias && !strcmp(e->elevator_alias, name))
90 return true;
91
92 return false;
93}
94
Jens Axboe2527d992017-10-25 12:33:42 -060095/*
Jens Axboea1ce35f2018-10-29 10:23:51 -060096 * Return scheduler with name 'name'
Jens Axboe2527d992017-10-25 12:33:42 -060097 */
Jens Axboea1ce35f2018-10-29 10:23:51 -060098static struct elevator_type *elevator_find(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Vasily Tarasova22b1692006-10-11 09:24:27 +0200100 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Matthias Kaehlcke70cee262007-07-10 12:26:24 +0200102 list_for_each_entry(e, &elv_list, list) {
Jens Axboea1ce35f2018-10-29 10:23:51 -0600103 if (elevator_match(e, name))
Vasily Tarasova22b1692006-10-11 09:24:27 +0200104 return e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Vasily Tarasova22b1692006-10-11 09:24:27 +0200107 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110static void elevator_put(struct elevator_type *e)
111{
112 module_put(e->elevator_owner);
113}
114
Jens Axboe2527d992017-10-25 12:33:42 -0600115static struct elevator_type *elevator_get(struct request_queue *q,
116 const char *name, bool try_loading)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Tejun Heo2824bc932005-10-20 10:56:41 +0200118 struct elevator_type *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200120 spin_lock(&elv_list_lock);
Tejun Heo2824bc932005-10-20 10:56:41 +0200121
Jens Axboea1ce35f2018-10-29 10:23:51 -0600122 e = elevator_find(name);
Tejun Heo21c3c5d2013-01-22 16:48:03 -0800123 if (!e && try_loading) {
Jens Axboee1640942008-02-19 10:20:37 +0100124 spin_unlock(&elv_list_lock);
Kees Cook490b94b2011-05-05 18:02:12 -0600125 request_module("%s-iosched", name);
Jens Axboee1640942008-02-19 10:20:37 +0100126 spin_lock(&elv_list_lock);
Jens Axboea1ce35f2018-10-29 10:23:51 -0600127 e = elevator_find(name);
Jens Axboee1640942008-02-19 10:20:37 +0100128 }
129
Tejun Heo2824bc932005-10-20 10:56:41 +0200130 if (e && !try_module_get(e->elevator_owner))
131 e = NULL;
132
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200133 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return e;
135}
136
Wang Sheng-Hui484fc252011-09-08 12:32:14 +0200137static char chosen_elevator[ELV_NAME_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Nate Diller5f003972006-01-24 10:07:58 +0100139static int __init elevator_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Chuck Ebbert752a3b72006-01-16 09:47:37 +0100141 /*
142 * Be backwards-compatible with previous kernels, so users
143 * won't get the wrong elevator.
144 */
Jens Axboe492af632009-10-03 09:37:51 +0200145 strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800146 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149__setup("elevator=", elevator_setup);
150
Al Viro3d1ab402006-03-18 18:35:43 -0500151static struct kobj_type elv_ktype;
152
Jianpeng Mad50235b2013-07-03 13:25:24 +0200153struct elevator_queue *elevator_alloc(struct request_queue *q,
Jens Axboe165125e2007-07-24 09:28:11 +0200154 struct elevator_type *e)
Al Viro3d1ab402006-03-18 18:35:43 -0500155{
Jens Axboeb374d182008-10-31 10:05:07 +0100156 struct elevator_queue *eq;
Jens Axboe98170642006-07-28 09:23:08 +0200157
Joe Perchesc1b511e2013-08-29 15:21:42 -0700158 eq = kzalloc_node(sizeof(*eq), GFP_KERNEL, q->node);
Jens Axboe98170642006-07-28 09:23:08 +0200159 if (unlikely(!eq))
Chao Yu8406a4d2015-04-23 10:47:44 -0600160 return NULL;
Jens Axboe98170642006-07-28 09:23:08 +0200161
Tejun Heo22f746e2011-12-14 00:33:41 +0100162 eq->type = e;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700163 kobject_init(&eq->kobj, &elv_ktype);
Jens Axboe98170642006-07-28 09:23:08 +0200164 mutex_init(&eq->sysfs_lock);
Sasha Levin242d98f2012-12-17 10:01:27 -0500165 hash_init(eq->hash);
Jens Axboe98170642006-07-28 09:23:08 +0200166
Al Viro3d1ab402006-03-18 18:35:43 -0500167 return eq;
168}
Jianpeng Mad50235b2013-07-03 13:25:24 +0200169EXPORT_SYMBOL(elevator_alloc);
Al Viro3d1ab402006-03-18 18:35:43 -0500170
171static void elevator_release(struct kobject *kobj)
172{
Jens Axboeb374d182008-10-31 10:05:07 +0100173 struct elevator_queue *e;
Jens Axboe98170642006-07-28 09:23:08 +0200174
Jens Axboeb374d182008-10-31 10:05:07 +0100175 e = container_of(kobj, struct elevator_queue, kobj);
Tejun Heo22f746e2011-12-14 00:33:41 +0100176 elevator_put(e->type);
Al Viro3d1ab402006-03-18 18:35:43 -0500177 kfree(e);
178}
179
Omar Sandoval54d53292017-04-07 08:52:27 -0600180void elevator_exit(struct request_queue *q, struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Al Viro3d1ab402006-03-18 18:35:43 -0500182 mutex_lock(&e->sysfs_lock);
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600183 if (e->type->ops.exit_sched)
Omar Sandoval54d53292017-04-07 08:52:27 -0600184 blk_mq_exit_sched(q, e);
Al Viro3d1ab402006-03-18 18:35:43 -0500185 mutex_unlock(&e->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Al Viro3d1ab402006-03-18 18:35:43 -0500187 kobject_put(&e->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
Jens Axboe2e662b62006-07-13 11:55:04 +0200189
Jens Axboe98170642006-07-28 09:23:08 +0200190static inline void __elv_rqhash_del(struct request *rq)
191{
Sasha Levin242d98f2012-12-17 10:01:27 -0500192 hash_del(&rq->hash);
Christoph Hellwige8064022016-10-20 15:12:13 +0200193 rq->rq_flags &= ~RQF_HASHED;
Jens Axboe98170642006-07-28 09:23:08 +0200194}
195
Jens Axboe70b3ea02016-12-07 08:43:31 -0700196void elv_rqhash_del(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200197{
198 if (ELV_ON_HASH(rq))
199 __elv_rqhash_del(rq);
200}
Jens Axboebd166ef2017-01-17 06:03:22 -0700201EXPORT_SYMBOL_GPL(elv_rqhash_del);
Jens Axboe98170642006-07-28 09:23:08 +0200202
Jens Axboe70b3ea02016-12-07 08:43:31 -0700203void elv_rqhash_add(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200204{
Jens Axboeb374d182008-10-31 10:05:07 +0100205 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200206
207 BUG_ON(ELV_ON_HASH(rq));
Sasha Levin242d98f2012-12-17 10:01:27 -0500208 hash_add(e->hash, &rq->hash, rq_hash_key(rq));
Christoph Hellwige8064022016-10-20 15:12:13 +0200209 rq->rq_flags |= RQF_HASHED;
Jens Axboe98170642006-07-28 09:23:08 +0200210}
Jens Axboebd166ef2017-01-17 06:03:22 -0700211EXPORT_SYMBOL_GPL(elv_rqhash_add);
Jens Axboe98170642006-07-28 09:23:08 +0200212
Jens Axboe70b3ea02016-12-07 08:43:31 -0700213void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
Jens Axboe98170642006-07-28 09:23:08 +0200214{
215 __elv_rqhash_del(rq);
216 elv_rqhash_add(q, rq);
217}
218
Jens Axboe70b3ea02016-12-07 08:43:31 -0700219struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
Jens Axboe98170642006-07-28 09:23:08 +0200220{
Jens Axboeb374d182008-10-31 10:05:07 +0100221 struct elevator_queue *e = q->elevator;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800222 struct hlist_node *next;
Jens Axboe98170642006-07-28 09:23:08 +0200223 struct request *rq;
224
Linus Torvaldsee89f812013-02-28 12:52:24 -0800225 hash_for_each_possible_safe(e->hash, rq, next, hash, offset) {
Jens Axboe98170642006-07-28 09:23:08 +0200226 BUG_ON(!ELV_ON_HASH(rq));
227
228 if (unlikely(!rq_mergeable(rq))) {
229 __elv_rqhash_del(rq);
230 continue;
231 }
232
233 if (rq_hash_key(rq) == offset)
234 return rq;
235 }
236
237 return NULL;
238}
239
Tejun Heo8922e162005-10-20 16:23:44 +0200240/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200241 * RB-tree support functions for inserting/lookup/removal of requests
242 * in a sorted RB tree.
243 */
Jeff Moyer796d5112011-06-02 21:19:05 +0200244void elv_rb_add(struct rb_root *root, struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +0200245{
246 struct rb_node **p = &root->rb_node;
247 struct rb_node *parent = NULL;
248 struct request *__rq;
249
250 while (*p) {
251 parent = *p;
252 __rq = rb_entry(parent, struct request, rb_node);
253
Tejun Heo83096eb2009-05-07 22:24:39 +0900254 if (blk_rq_pos(rq) < blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200255 p = &(*p)->rb_left;
Jeff Moyer796d5112011-06-02 21:19:05 +0200256 else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200257 p = &(*p)->rb_right;
Jens Axboe2e662b62006-07-13 11:55:04 +0200258 }
259
260 rb_link_node(&rq->rb_node, parent, p);
261 rb_insert_color(&rq->rb_node, root);
Jens Axboe2e662b62006-07-13 11:55:04 +0200262}
Jens Axboe2e662b62006-07-13 11:55:04 +0200263EXPORT_SYMBOL(elv_rb_add);
264
265void elv_rb_del(struct rb_root *root, struct request *rq)
266{
267 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
268 rb_erase(&rq->rb_node, root);
269 RB_CLEAR_NODE(&rq->rb_node);
270}
Jens Axboe2e662b62006-07-13 11:55:04 +0200271EXPORT_SYMBOL(elv_rb_del);
272
273struct request *elv_rb_find(struct rb_root *root, sector_t sector)
274{
275 struct rb_node *n = root->rb_node;
276 struct request *rq;
277
278 while (n) {
279 rq = rb_entry(n, struct request, rb_node);
280
Tejun Heo83096eb2009-05-07 22:24:39 +0900281 if (sector < blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200282 n = n->rb_left;
Tejun Heo83096eb2009-05-07 22:24:39 +0900283 else if (sector > blk_rq_pos(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200284 n = n->rb_right;
285 else
286 return rq;
287 }
288
289 return NULL;
290}
Jens Axboe2e662b62006-07-13 11:55:04 +0200291EXPORT_SYMBOL(elv_rb_find);
292
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100293enum elv_merge elv_merge(struct request_queue *q, struct request **req,
294 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Jens Axboeb374d182008-10-31 10:05:07 +0100296 struct elevator_queue *e = q->elevator;
Jens Axboe98170642006-07-28 09:23:08 +0200297 struct request *__rq;
Tejun Heo06b86242005-10-20 16:46:23 +0200298
Jens Axboe98170642006-07-28 09:23:08 +0200299 /*
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100300 * Levels of merges:
301 * nomerges: No merges at all attempted
302 * noxmerges: Only simple one-hit cache try
303 * merges: All merge tries attempted
304 */
Ming Lei7460d382015-10-20 23:13:55 +0800305 if (blk_queue_nomerges(q) || !bio_mergeable(bio))
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100306 return ELEVATOR_NO_MERGE;
307
308 /*
Jens Axboe98170642006-07-28 09:23:08 +0200309 * First try one-hit cache.
310 */
Tahsin Erdogan72ef7992016-07-07 11:48:22 -0700311 if (q->last_merge && elv_bio_merge_ok(q->last_merge, bio)) {
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100312 enum elv_merge ret = blk_try_merge(q->last_merge, bio);
313
Tejun Heo06b86242005-10-20 16:46:23 +0200314 if (ret != ELEVATOR_NO_MERGE) {
315 *req = q->last_merge;
316 return ret;
317 }
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100320 if (blk_queue_noxmerges(q))
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200321 return ELEVATOR_NO_MERGE;
322
Jens Axboe98170642006-07-28 09:23:08 +0200323 /*
324 * See if our hash lookup can find a potential backmerge.
325 */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700326 __rq = elv_rqhash_find(q, bio->bi_iter.bi_sector);
Tahsin Erdogan72ef7992016-07-07 11:48:22 -0700327 if (__rq && elv_bio_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200328 *req = __rq;
329 return ELEVATOR_BACK_MERGE;
330 }
331
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600332 if (e->type->ops.request_merge)
333 return e->type->ops.request_merge(q, req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 return ELEVATOR_NO_MERGE;
336}
337
Jens Axboe5e84ea32011-03-21 10:14:27 +0100338/*
339 * Attempt to do an insertion back merge. Only check for the case where
340 * we can append 'rq' to an existing request, so we can throw 'rq' away
341 * afterwards.
342 *
343 * Returns true if we merged, false otherwise
344 */
Jens Axboebd166ef2017-01-17 06:03:22 -0700345bool elv_attempt_insert_merge(struct request_queue *q, struct request *rq)
Jens Axboe5e84ea32011-03-21 10:14:27 +0100346{
347 struct request *__rq;
Shaohua Libee03932012-11-09 08:44:27 +0100348 bool ret;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100349
350 if (blk_queue_nomerges(q))
351 return false;
352
353 /*
354 * First try one-hit cache.
355 */
356 if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq))
357 return true;
358
359 if (blk_queue_noxmerges(q))
360 return false;
361
Shaohua Libee03932012-11-09 08:44:27 +0100362 ret = false;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100363 /*
364 * See if our hash lookup can find a potential backmerge.
365 */
Shaohua Libee03932012-11-09 08:44:27 +0100366 while (1) {
367 __rq = elv_rqhash_find(q, blk_rq_pos(rq));
368 if (!__rq || !blk_attempt_req_merge(q, __rq, rq))
369 break;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100370
Shaohua Libee03932012-11-09 08:44:27 +0100371 /* The merged request could be merged with others, try again */
372 ret = true;
373 rq = __rq;
374 }
375
376 return ret;
Jens Axboe5e84ea32011-03-21 10:14:27 +0100377}
378
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100379void elv_merged_request(struct request_queue *q, struct request *rq,
380 enum elv_merge type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Jens Axboeb374d182008-10-31 10:05:07 +0100382 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600384 if (e->type->ops.request_merged)
385 e->type->ops.request_merged(q, rq, type);
Tejun Heo06b86242005-10-20 16:46:23 +0200386
Jens Axboe2e662b62006-07-13 11:55:04 +0200387 if (type == ELEVATOR_BACK_MERGE)
388 elv_rqhash_reposition(q, rq);
Jens Axboe98170642006-07-28 09:23:08 +0200389
Tejun Heo06b86242005-10-20 16:46:23 +0200390 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Jens Axboe165125e2007-07-24 09:28:11 +0200393void elv_merge_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct request *next)
395{
Jens Axboeb374d182008-10-31 10:05:07 +0100396 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600398 if (e->type->ops.requests_merged)
399 e->type->ops.requests_merged(q, rq, next);
Tejun Heo06b86242005-10-20 16:46:23 +0200400
Jens Axboe98170642006-07-28 09:23:08 +0200401 elv_rqhash_reposition(q, rq);
Tejun Heo06b86242005-10-20 16:46:23 +0200402 q->last_merge = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Jens Axboe165125e2007-07-24 09:28:11 +0200405struct request *elv_latter_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
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.next_request)
410 return e->type->ops.next_request(q, rq);
Jens Axboebd166ef2017-01-17 06:03:22 -0700411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return NULL;
413}
414
Jens Axboe165125e2007-07-24 09:28:11 +0200415struct request *elv_former_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Jens Axboeb374d182008-10-31 10:05:07 +0100417 struct elevator_queue *e = q->elevator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600419 if (e->type->ops.former_request)
420 return e->type->ops.former_request(q, rq);
Jens Axboea1ce35f2018-10-29 10:23:51 -0600421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return NULL;
423}
424
Al Viro3d1ab402006-03-18 18:35:43 -0500425#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
426
427static ssize_t
428elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
429{
Al Viro3d1ab402006-03-18 18:35:43 -0500430 struct elv_fs_entry *entry = to_elv(attr);
Jens Axboeb374d182008-10-31 10:05:07 +0100431 struct elevator_queue *e;
Al Viro3d1ab402006-03-18 18:35:43 -0500432 ssize_t error;
433
434 if (!entry->show)
435 return -EIO;
436
Jens Axboeb374d182008-10-31 10:05:07 +0100437 e = container_of(kobj, struct elevator_queue, kobj);
Al Viro3d1ab402006-03-18 18:35:43 -0500438 mutex_lock(&e->sysfs_lock);
Tejun Heo22f746e2011-12-14 00:33:41 +0100439 error = e->type ? entry->show(e, page) : -ENOENT;
Al Viro3d1ab402006-03-18 18:35:43 -0500440 mutex_unlock(&e->sysfs_lock);
441 return error;
442}
443
444static ssize_t
445elv_attr_store(struct kobject *kobj, struct attribute *attr,
446 const char *page, size_t length)
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->store)
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->store(e, page, length) : -ENOENT;
Al Viro3d1ab402006-03-18 18:35:43 -0500458 mutex_unlock(&e->sysfs_lock);
459 return error;
460}
461
Emese Revfy52cf25d2010-01-19 02:58:23 +0100462static const struct sysfs_ops elv_sysfs_ops = {
Al Viro3d1ab402006-03-18 18:35:43 -0500463 .show = elv_attr_show,
464 .store = elv_attr_store,
465};
466
467static struct kobj_type elv_ktype = {
468 .sysfs_ops = &elv_sysfs_ops,
469 .release = elevator_release,
470};
471
Tejun Heo5a5bafd2012-03-05 13:14:56 -0800472int elv_register_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Tejun Heo5a5bafd2012-03-05 13:14:56 -0800474 struct elevator_queue *e = q->elevator;
Al Viro3d1ab402006-03-18 18:35:43 -0500475 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Bart Van Assche14a23492018-01-17 11:48:09 -0800477 lockdep_assert_held(&q->sysfs_lock);
478
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700479 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
Al Viro3d1ab402006-03-18 18:35:43 -0500480 if (!error) {
Tejun Heo22f746e2011-12-14 00:33:41 +0100481 struct elv_fs_entry *attr = e->type->elevator_attrs;
Al Viro3d1ab402006-03-18 18:35:43 -0500482 if (attr) {
Al Viroe572ec72006-03-18 22:27:18 -0500483 while (attr->attr.name) {
484 if (sysfs_create_file(&e->kobj, &attr->attr))
Al Viro3d1ab402006-03-18 18:35:43 -0500485 break;
Al Viroe572ec72006-03-18 22:27:18 -0500486 attr++;
Al Viro3d1ab402006-03-18 18:35:43 -0500487 }
488 }
489 kobject_uevent(&e->kobj, KOBJ_ADD);
Jens Axboe430c62f2010-10-07 09:35:16 +0200490 e->registered = 1;
Al Viro3d1ab402006-03-18 18:35:43 -0500491 }
492 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
Jens Axboebc1c1162006-06-08 08:49:06 +0200494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495void elv_unregister_queue(struct request_queue *q)
496{
Bart Van Assche14a23492018-01-17 11:48:09 -0800497 lockdep_assert_held(&q->sysfs_lock);
498
Tejun Heof8fc8772011-12-14 00:33:40 +0100499 if (q) {
500 struct elevator_queue *e = q->elevator;
501
502 kobject_uevent(&e->kobj, KOBJ_REMOVE);
503 kobject_del(&e->kobj);
504 e->registered = 0;
Jan Kara8330cdb2017-04-19 11:33:27 +0200505 /* Re-enable throttling in case elevator disabled it */
506 wbt_enable_default(q);
Tejun Heof8fc8772011-12-14 00:33:40 +0100507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
Jens Axboee567bf72014-06-22 16:32:48 -0600510int elv_register(struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Thibaut VARENE1ffb96c2007-03-15 12:59:19 +0100512 char *def = "";
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200513
Tejun Heo3d3c2372011-12-14 00:33:42 +0100514 /* create icq_cache if requested */
515 if (e->icq_size) {
516 if (WARN_ON(e->icq_size < sizeof(struct io_cq)) ||
517 WARN_ON(e->icq_align < __alignof__(struct io_cq)))
518 return -EINVAL;
519
520 snprintf(e->icq_cache_name, sizeof(e->icq_cache_name),
521 "%s_io_cq", e->elevator_name);
522 e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size,
523 e->icq_align, 0, NULL);
524 if (!e->icq_cache)
525 return -ENOMEM;
526 }
527
528 /* register, don't allow duplicate names */
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200529 spin_lock(&elv_list_lock);
Jens Axboea1ce35f2018-10-29 10:23:51 -0600530 if (elevator_find(e->elevator_name)) {
Tejun Heo3d3c2372011-12-14 00:33:42 +0100531 spin_unlock(&elv_list_lock);
Chengguang Xu62d2a192018-08-28 07:31:11 +0800532 kmem_cache_destroy(e->icq_cache);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100533 return -EBUSY;
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 list_add_tail(&e->list, &elv_list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200536 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Jens Axboe4eb166d2008-02-01 00:37:27 +0100538 printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
539 def);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542EXPORT_SYMBOL_GPL(elv_register);
543
544void elv_unregister(struct elevator_type *e)
545{
Tejun Heo3d3c2372011-12-14 00:33:42 +0100546 /* unregister */
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200547 spin_lock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 list_del_init(&e->list);
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200549 spin_unlock(&elv_list_lock);
Tejun Heo3d3c2372011-12-14 00:33:42 +0100550
551 /*
552 * Destroy icq_cache if it exists. icq's are RCU managed. Make
553 * sure all RCU operations are complete before proceeding.
554 */
555 if (e->icq_cache) {
556 rcu_barrier();
557 kmem_cache_destroy(e->icq_cache);
558 e->icq_cache = NULL;
559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561EXPORT_SYMBOL_GPL(elv_unregister);
562
Jianchao Wangd48ece22018-08-21 15:15:03 +0800563int elevator_switch_mq(struct request_queue *q,
Omar Sandoval54d53292017-04-07 08:52:27 -0600564 struct elevator_type *new_e)
565{
566 int ret;
567
Bart Van Assche14a23492018-01-17 11:48:09 -0800568 lockdep_assert_held(&q->sysfs_lock);
569
Omar Sandoval54d53292017-04-07 08:52:27 -0600570 if (q->elevator) {
571 if (q->elevator->registered)
572 elv_unregister_queue(q);
573 ioc_clear_queue(q);
574 elevator_exit(q, q->elevator);
575 }
576
577 ret = blk_mq_init_sched(q, new_e);
578 if (ret)
579 goto out;
580
581 if (new_e) {
582 ret = elv_register_queue(q);
583 if (ret) {
584 elevator_exit(q, q->elevator);
585 goto out;
586 }
587 }
588
589 if (new_e)
590 blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name);
591 else
592 blk_add_trace_msg(q, "elv switch: none");
593
594out:
Omar Sandoval54d53292017-04-07 08:52:27 -0600595 return ret;
Omar Sandoval54d53292017-04-07 08:52:27 -0600596}
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598/*
Christoph Hellwig131d08e2018-05-31 19:11:40 +0200599 * For blk-mq devices, we default to using mq-deadline, if available, for single
600 * queue devices. If deadline isn't available OR we have multiple queues,
601 * default to "none".
602 */
603int elevator_init_mq(struct request_queue *q)
604{
605 struct elevator_type *e;
606 int err = 0;
607
608 if (q->nr_hw_queues != 1)
609 return 0;
610
611 /*
612 * q->sysfs_lock must be held to provide mutual exclusion between
613 * elevator_switch() and here.
614 */
615 mutex_lock(&q->sysfs_lock);
616 if (unlikely(q->elevator))
617 goto out_unlock;
618
619 e = elevator_get(q, "mq-deadline", false);
620 if (!e)
621 goto out_unlock;
622
623 err = blk_mq_init_sched(q, e);
624 if (err)
625 elevator_put(e);
626out_unlock:
627 mutex_unlock(&q->sysfs_lock);
628 return err;
629}
630
631
632/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 * switch to new_e io scheduler. be careful not to introduce deadlocks -
634 * we don't free the old io scheduler, before we have allocated what we
635 * need for the new one. this way we have a chance of going back to the old
Tejun Heocb98fc82005-10-28 08:29:39 +0200636 * one, if the new one fails init for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
Jens Axboe165125e2007-07-24 09:28:11 +0200638static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800640 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Bart Van Assche14a23492018-01-17 11:48:09 -0800642 lockdep_assert_held(&q->sysfs_lock);
643
Jens Axboea1ce35f2018-10-29 10:23:51 -0600644 blk_mq_freeze_queue(q);
645 blk_mq_quiesce_queue(q);
Jianchao Wangd48ece22018-08-21 15:15:03 +0800646
Jens Axboea1ce35f2018-10-29 10:23:51 -0600647 err = elevator_switch_mq(q, new_e);
Jianchao Wangd48ece22018-08-21 15:15:03 +0800648
Jens Axboea1ce35f2018-10-29 10:23:51 -0600649 blk_mq_unquiesce_queue(q);
650 blk_mq_unfreeze_queue(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200651
Jens Axboe5dd531a2010-08-23 13:52:19 +0200652 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Jens Axboe5dd531a2010-08-23 13:52:19 +0200655/*
656 * Switch this queue to the given IO scheduler.
657 */
Tomoki Sekiyama7c8a3672013-10-15 16:42:19 -0600658static int __elevator_change(struct request_queue *q, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 char elevator_name[ELV_NAME_MAX];
661 struct elevator_type *e;
662
David Jefferye9a823f2017-08-28 10:52:44 -0600663 /* Make sure queue is not in the middle of being removed */
664 if (!test_bit(QUEUE_FLAG_REGISTERED, &q->queue_flags))
665 return -ENOENT;
666
Jens Axboebd166ef2017-01-17 06:03:22 -0700667 /*
668 * Special case for mq, turn off scheduling
669 */
Jens Axboe344e9ff2018-11-15 12:22:51 -0700670 if (!strncmp(name, "none", 4))
Jens Axboebd166ef2017-01-17 06:03:22 -0700671 return elevator_switch(q, NULL);
Martin K. Petersencd43e262009-05-22 17:17:52 -0400672
Li Zefanee2e9922008-10-14 08:49:56 +0200673 strlcpy(elevator_name, name, sizeof(elevator_name));
Jens Axboe2527d992017-10-25 12:33:42 -0600674 e = elevator_get(q, strstrip(elevator_name), true);
Jens Axboe340ff322017-05-10 07:40:04 -0600675 if (!e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Jens Axboe8ac0d9a2017-10-25 12:35:02 -0600678 if (q->elevator && elevator_match(q->elevator->type, elevator_name)) {
Nate Diller2ca7d932005-10-30 15:02:24 -0800679 elevator_put(e);
Jens Axboe5dd531a2010-08-23 13:52:19 +0200680 return 0;
Nate Diller2ca7d932005-10-30 15:02:24 -0800681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Jens Axboe5dd531a2010-08-23 13:52:19 +0200683 return elevator_switch(q, e);
684}
Tomoki Sekiyama7c8a3672013-10-15 16:42:19 -0600685
Ming Lei3a5088c2017-04-15 20:38:22 +0800686static inline bool elv_support_iosched(struct request_queue *q)
687{
Jens Axboe344e9ff2018-11-15 12:22:51 -0700688 if (q->tag_set && (q->tag_set->flags & BLK_MQ_F_NO_SCHED))
Ming Lei3a5088c2017-04-15 20:38:22 +0800689 return false;
690 return true;
691}
692
Jens Axboe5dd531a2010-08-23 13:52:19 +0200693ssize_t elv_iosched_store(struct request_queue *q, const char *name,
694 size_t count)
695{
696 int ret;
697
Jens Axboe344e9ff2018-11-15 12:22:51 -0700698 if (!queue_is_mq(q) || !elv_support_iosched(q))
Jens Axboe5dd531a2010-08-23 13:52:19 +0200699 return count;
700
Tomoki Sekiyama7c8a3672013-10-15 16:42:19 -0600701 ret = __elevator_change(q, name);
Jens Axboe5dd531a2010-08-23 13:52:19 +0200702 if (!ret)
703 return count;
704
Jens Axboe5dd531a2010-08-23 13:52:19 +0200705 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Jens Axboe165125e2007-07-24 09:28:11 +0200708ssize_t elv_iosched_show(struct request_queue *q, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Jens Axboeb374d182008-10-31 10:05:07 +0100710 struct elevator_queue *e = q->elevator;
Jens Axboebd166ef2017-01-17 06:03:22 -0700711 struct elevator_type *elv = NULL;
Matthias Kaehlcke70cee262007-07-10 12:26:24 +0200712 struct elevator_type *__e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 int len = 0;
714
Jens Axboe344e9ff2018-11-15 12:22:51 -0700715 if (!queue_is_mq(q))
Martin K. Petersencd43e262009-05-22 17:17:52 -0400716 return sprintf(name, "none\n");
717
Jens Axboebd166ef2017-01-17 06:03:22 -0700718 if (!q->elevator)
719 len += sprintf(name+len, "[none] ");
720 else
721 elv = e->type;
Martin K. Petersencd43e262009-05-22 17:17:52 -0400722
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200723 spin_lock(&elv_list_lock);
Matthias Kaehlcke70cee262007-07-10 12:26:24 +0200724 list_for_each_entry(__e, &elv_list, list) {
Jens Axboea1ce35f2018-10-29 10:23:51 -0600725 if (elv && elevator_match(elv, __e->elevator_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 len += sprintf(name+len, "[%s] ", elv->elevator_name);
Jens Axboebd166ef2017-01-17 06:03:22 -0700727 continue;
728 }
Jens Axboea1ce35f2018-10-29 10:23:51 -0600729 if (elv_support_iosched(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 len += sprintf(name+len, "%s ", __e->elevator_name);
731 }
Jens Axboe2a12dcd2007-04-26 14:41:53 +0200732 spin_unlock(&elv_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Jens Axboe344e9ff2018-11-15 12:22:51 -0700734 if (q->elevator)
Jens Axboebd166ef2017-01-17 06:03:22 -0700735 len += sprintf(name+len, "none");
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 len += sprintf(len+name, "\n");
738 return len;
739}
740
Jens Axboe165125e2007-07-24 09:28:11 +0200741struct request *elv_rb_former_request(struct request_queue *q,
742 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +0200743{
744 struct rb_node *rbprev = rb_prev(&rq->rb_node);
745
746 if (rbprev)
747 return rb_entry_rq(rbprev);
748
749 return NULL;
750}
Jens Axboe2e662b62006-07-13 11:55:04 +0200751EXPORT_SYMBOL(elv_rb_former_request);
752
Jens Axboe165125e2007-07-24 09:28:11 +0200753struct request *elv_rb_latter_request(struct request_queue *q,
754 struct request *rq)
Jens Axboe2e662b62006-07-13 11:55:04 +0200755{
756 struct rb_node *rbnext = rb_next(&rq->rb_node);
757
758 if (rbnext)
759 return rb_entry_rq(rbnext);
760
761 return NULL;
762}
Jens Axboe2e662b62006-07-13 11:55:04 +0200763EXPORT_SYMBOL(elv_rb_latter_request);