]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - block/elevator.c
UBUNTU: Ubuntu-5.11.0-22.23
[mirror_ubuntu-hirsute-kernel.git] / block / elevator.c
CommitLineData
3dcf60bc 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
1da177e4
LT
3 * Block device elevator/IO-scheduler.
4 *
5 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
6 *
0fe23479 7 * 30042000 Jens Axboe <axboe@kernel.dk> :
1da177e4
LT
8 *
9 * Split the elevator a bit so that it is possible to choose a different
10 * one or even write a new "plug in". There are three pieces:
11 * - elevator_fn, inserts a new request in the queue list
12 * - elevator_merge_fn, decides whether a new buffer can be merged with
13 * an existing request
14 * - elevator_dequeue_fn, called when a request is taken off the active list
15 *
16 * 20082000 Dave Jones <davej@suse.de> :
17 * Removed tests for max-bomb-segments, which was breaking elvtune
18 * when run without -bN
19 *
20 * Jens:
21 * - Rework again to work with bio instead of buffer_heads
22 * - loose bi_dev comparisons, partition handling is right now
23 * - completely modularize elevator setup and teardown
24 *
25 */
26#include <linux/kernel.h>
27#include <linux/fs.h>
28#include <linux/blkdev.h>
29#include <linux/elevator.h>
30#include <linux/bio.h>
1da177e4
LT
31#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/init.h>
34#include <linux/compiler.h>
2056a782 35#include <linux/blktrace_api.h>
9817064b 36#include <linux/hash.h>
0835da67 37#include <linux/uaccess.h>
c8158819 38#include <linux/pm_runtime.h>
eea8f41c 39#include <linux/blk-cgroup.h>
1da177e4 40
55782138
LZ
41#include <trace/events/block.h>
42
242f9dcb 43#include "blk.h"
bd166ef1 44#include "blk-mq-sched.h"
bca6b067 45#include "blk-pm.h"
8330cdb0 46#include "blk-wbt.h"
242f9dcb 47
1da177e4
LT
48static DEFINE_SPINLOCK(elv_list_lock);
49static LIST_HEAD(elv_list);
50
9817064b
JA
51/*
52 * Merge hash stuff.
53 */
83096ebf 54#define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
9817064b 55
da775265
JA
56/*
57 * Query io scheduler to see if the current process issuing bio may be
58 * merged with rq.
59 */
72ef799b 60static int elv_iosched_allow_bio_merge(struct request *rq, struct bio *bio)
da775265 61{
165125e1 62 struct request_queue *q = rq->q;
b374d18a 63 struct elevator_queue *e = q->elevator;
da775265 64
f9cd4bfe
JA
65 if (e->type->ops.allow_merge)
66 return e->type->ops.allow_merge(q, rq, bio);
da775265
JA
67
68 return 1;
69}
70
1da177e4
LT
71/*
72 * can we safely merge with this request?
73 */
72ef799b 74bool elv_bio_merge_ok(struct request *rq, struct bio *bio)
1da177e4 75{
050c8ea8 76 if (!blk_rq_merge_ok(rq, bio))
72ef799b 77 return false;
7ba1ba12 78
72ef799b
TE
79 if (!elv_iosched_allow_bio_merge(rq, bio))
80 return false;
1da177e4 81
72ef799b 82 return true;
1da177e4 83}
72ef799b 84EXPORT_SYMBOL(elv_bio_merge_ok);
1da177e4 85
68c43f13
DLM
86static inline bool elv_support_features(unsigned int elv_features,
87 unsigned int required_features)
8ac0d9a8 88{
68c43f13
DLM
89 return (required_features & elv_features) == required_features;
90}
91
92/**
93 * elevator_match - Test an elevator name and features
94 * @e: Scheduler to test
95 * @name: Elevator name to test
96 * @required_features: Features that the elevator must provide
97 *
5b8f65e1
RD
98 * Return true if the elevator @e name matches @name and if @e provides all
99 * the features specified by @required_features.
68c43f13
DLM
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;
8ac0d9a8
JA
106 if (!strcmp(e->elevator_name, name))
107 return true;
108 if (e->elevator_alias && !strcmp(e->elevator_alias, name))
109 return true;
110
111 return false;
112}
113
68c43f13
DLM
114/**
115 * elevator_find - Find an elevator
116 * @name: Name of the elevator to find
117 * @required_features: Features that the elevator must provide
118 *
119 * Return the first registered scheduler with name @name and supporting the
120 * features @required_features and NULL otherwise.
2527d997 121 */
68c43f13
DLM
122static struct elevator_type *elevator_find(const char *name,
123 unsigned int required_features)
1da177e4 124{
a22b169d 125 struct elevator_type *e;
1da177e4 126
70cee26e 127 list_for_each_entry(e, &elv_list, list) {
68c43f13 128 if (elevator_match(e, name, required_features))
a22b169d 129 return e;
1da177e4 130 }
1da177e4 131
a22b169d 132 return NULL;
1da177e4
LT
133}
134
135static void elevator_put(struct elevator_type *e)
136{
137 module_put(e->elevator_owner);
138}
139
2527d997
JA
140static struct elevator_type *elevator_get(struct request_queue *q,
141 const char *name, bool try_loading)
1da177e4 142{
2824bc93 143 struct elevator_type *e;
1da177e4 144
2a12dcd7 145 spin_lock(&elv_list_lock);
2824bc93 146
68c43f13 147 e = elevator_find(name, q->required_elevator_features);
21c3c5d2 148 if (!e && try_loading) {
e1640949 149 spin_unlock(&elv_list_lock);
490b94be 150 request_module("%s-iosched", name);
e1640949 151 spin_lock(&elv_list_lock);
68c43f13 152 e = elevator_find(name, q->required_elevator_features);
e1640949
JA
153 }
154
2824bc93
TH
155 if (e && !try_module_get(e->elevator_owner))
156 e = NULL;
157
2a12dcd7 158 spin_unlock(&elv_list_lock);
1da177e4
LT
159 return e;
160}
161
3d1ab40f
AV
162static struct kobj_type elv_ktype;
163
d50235b7 164struct elevator_queue *elevator_alloc(struct request_queue *q,
165125e1 165 struct elevator_type *e)
3d1ab40f 166{
b374d18a 167 struct elevator_queue *eq;
9817064b 168
c1b511eb 169 eq = kzalloc_node(sizeof(*eq), GFP_KERNEL, q->node);
9817064b 170 if (unlikely(!eq))
8406a4d5 171 return NULL;
9817064b 172
22f746e2 173 eq->type = e;
f9cb074b 174 kobject_init(&eq->kobj, &elv_ktype);
9817064b 175 mutex_init(&eq->sysfs_lock);
242d98f0 176 hash_init(eq->hash);
9817064b 177
3d1ab40f
AV
178 return eq;
179}
d50235b7 180EXPORT_SYMBOL(elevator_alloc);
3d1ab40f
AV
181
182static void elevator_release(struct kobject *kobj)
183{
b374d18a 184 struct elevator_queue *e;
9817064b 185
b374d18a 186 e = container_of(kobj, struct elevator_queue, kobj);
22f746e2 187 elevator_put(e->type);
3d1ab40f
AV
188 kfree(e);
189}
190
c3e22192 191void __elevator_exit(struct request_queue *q, struct elevator_queue *e)
1da177e4 192{
3d1ab40f 193 mutex_lock(&e->sysfs_lock);
dd1c372d 194 blk_mq_exit_sched(q, e);
3d1ab40f 195 mutex_unlock(&e->sysfs_lock);
1da177e4 196
3d1ab40f 197 kobject_put(&e->kobj);
1da177e4 198}
2e662b65 199
9817064b
JA
200static inline void __elv_rqhash_del(struct request *rq)
201{
242d98f0 202 hash_del(&rq->hash);
e8064021 203 rq->rq_flags &= ~RQF_HASHED;
9817064b
JA
204}
205
70b3ea05 206void elv_rqhash_del(struct request_queue *q, struct request *rq)
9817064b
JA
207{
208 if (ELV_ON_HASH(rq))
209 __elv_rqhash_del(rq);
210}
bd166ef1 211EXPORT_SYMBOL_GPL(elv_rqhash_del);
9817064b 212
70b3ea05 213void elv_rqhash_add(struct request_queue *q, struct request *rq)
9817064b 214{
b374d18a 215 struct elevator_queue *e = q->elevator;
9817064b
JA
216
217 BUG_ON(ELV_ON_HASH(rq));
242d98f0 218 hash_add(e->hash, &rq->hash, rq_hash_key(rq));
e8064021 219 rq->rq_flags |= RQF_HASHED;
9817064b 220}
bd166ef1 221EXPORT_SYMBOL_GPL(elv_rqhash_add);
9817064b 222
70b3ea05 223void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
9817064b
JA
224{
225 __elv_rqhash_del(rq);
226 elv_rqhash_add(q, rq);
227}
228
70b3ea05 229struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
9817064b 230{
b374d18a 231 struct elevator_queue *e = q->elevator;
b67bfe0d 232 struct hlist_node *next;
9817064b
JA
233 struct request *rq;
234
ee89f812 235 hash_for_each_possible_safe(e->hash, rq, next, hash, offset) {
9817064b
JA
236 BUG_ON(!ELV_ON_HASH(rq));
237
238 if (unlikely(!rq_mergeable(rq))) {
239 __elv_rqhash_del(rq);
240 continue;
241 }
242
243 if (rq_hash_key(rq) == offset)
244 return rq;
245 }
246
247 return NULL;
248}
249
2e662b65
JA
250/*
251 * RB-tree support functions for inserting/lookup/removal of requests
252 * in a sorted RB tree.
253 */
796d5116 254void elv_rb_add(struct rb_root *root, struct request *rq)
2e662b65
JA
255{
256 struct rb_node **p = &root->rb_node;
257 struct rb_node *parent = NULL;
258 struct request *__rq;
259
260 while (*p) {
261 parent = *p;
262 __rq = rb_entry(parent, struct request, rb_node);
263
83096ebf 264 if (blk_rq_pos(rq) < blk_rq_pos(__rq))
2e662b65 265 p = &(*p)->rb_left;
796d5116 266 else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))
2e662b65 267 p = &(*p)->rb_right;
2e662b65
JA
268 }
269
270 rb_link_node(&rq->rb_node, parent, p);
271 rb_insert_color(&rq->rb_node, root);
2e662b65 272}
2e662b65
JA
273EXPORT_SYMBOL(elv_rb_add);
274
275void elv_rb_del(struct rb_root *root, struct request *rq)
276{
277 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
278 rb_erase(&rq->rb_node, root);
279 RB_CLEAR_NODE(&rq->rb_node);
280}
2e662b65
JA
281EXPORT_SYMBOL(elv_rb_del);
282
283struct request *elv_rb_find(struct rb_root *root, sector_t sector)
284{
285 struct rb_node *n = root->rb_node;
286 struct request *rq;
287
288 while (n) {
289 rq = rb_entry(n, struct request, rb_node);
290
83096ebf 291 if (sector < blk_rq_pos(rq))
2e662b65 292 n = n->rb_left;
83096ebf 293 else if (sector > blk_rq_pos(rq))
2e662b65
JA
294 n = n->rb_right;
295 else
296 return rq;
297 }
298
299 return NULL;
300}
2e662b65
JA
301EXPORT_SYMBOL(elv_rb_find);
302
34fe7c05
CH
303enum elv_merge elv_merge(struct request_queue *q, struct request **req,
304 struct bio *bio)
1da177e4 305{
b374d18a 306 struct elevator_queue *e = q->elevator;
9817064b 307 struct request *__rq;
06b86245 308
488991e2
AB
309 /*
310 * Levels of merges:
311 * nomerges: No merges at all attempted
312 * noxmerges: Only simple one-hit cache try
313 * merges: All merge tries attempted
314 */
7460d389 315 if (blk_queue_nomerges(q) || !bio_mergeable(bio))
488991e2
AB
316 return ELEVATOR_NO_MERGE;
317
9817064b
JA
318 /*
319 * First try one-hit cache.
320 */
72ef799b 321 if (q->last_merge && elv_bio_merge_ok(q->last_merge, bio)) {
34fe7c05
CH
322 enum elv_merge ret = blk_try_merge(q->last_merge, bio);
323
06b86245
TH
324 if (ret != ELEVATOR_NO_MERGE) {
325 *req = q->last_merge;
326 return ret;
327 }
328 }
1da177e4 329
488991e2 330 if (blk_queue_noxmerges(q))
ac9fafa1
AB
331 return ELEVATOR_NO_MERGE;
332
9817064b
JA
333 /*
334 * See if our hash lookup can find a potential backmerge.
335 */
4f024f37 336 __rq = elv_rqhash_find(q, bio->bi_iter.bi_sector);
72ef799b 337 if (__rq && elv_bio_merge_ok(__rq, bio)) {
9817064b
JA
338 *req = __rq;
339 return ELEVATOR_BACK_MERGE;
340 }
341
f9cd4bfe
JA
342 if (e->type->ops.request_merge)
343 return e->type->ops.request_merge(q, req, bio);
1da177e4
LT
344
345 return ELEVATOR_NO_MERGE;
346}
347
5e84ea3a
JA
348/*
349 * Attempt to do an insertion back merge. Only check for the case where
350 * we can append 'rq' to an existing request, so we can throw 'rq' away
351 * afterwards.
352 *
353 * Returns true if we merged, false otherwise
354 */
bd166ef1 355bool elv_attempt_insert_merge(struct request_queue *q, struct request *rq)
5e84ea3a
JA
356{
357 struct request *__rq;
bee0393c 358 bool ret;
5e84ea3a
JA
359
360 if (blk_queue_nomerges(q))
361 return false;
362
363 /*
364 * First try one-hit cache.
365 */
366 if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq))
367 return true;
368
369 if (blk_queue_noxmerges(q))
370 return false;
371
bee0393c 372 ret = false;
5e84ea3a
JA
373 /*
374 * See if our hash lookup can find a potential backmerge.
375 */
bee0393c
SL
376 while (1) {
377 __rq = elv_rqhash_find(q, blk_rq_pos(rq));
378 if (!__rq || !blk_attempt_req_merge(q, __rq, rq))
379 break;
380
381 /* The merged request could be merged with others, try again */
382 ret = true;
383 rq = __rq;
384 }
27419322 385
bee0393c 386 return ret;
5e84ea3a
JA
387}
388
34fe7c05
CH
389void elv_merged_request(struct request_queue *q, struct request *rq,
390 enum elv_merge type)
1da177e4 391{
b374d18a 392 struct elevator_queue *e = q->elevator;
1da177e4 393
f9cd4bfe
JA
394 if (e->type->ops.request_merged)
395 e->type->ops.request_merged(q, rq, type);
06b86245 396
2e662b65
JA
397 if (type == ELEVATOR_BACK_MERGE)
398 elv_rqhash_reposition(q, rq);
9817064b 399
06b86245 400 q->last_merge = rq;
1da177e4
LT
401}
402
165125e1 403void elv_merge_requests(struct request_queue *q, struct request *rq,
1da177e4
LT
404 struct request *next)
405{
b374d18a 406 struct elevator_queue *e = q->elevator;
bd166ef1 407
f9cd4bfe
JA
408 if (e->type->ops.requests_merged)
409 e->type->ops.requests_merged(q, rq, next);
06b86245 410
9817064b 411 elv_rqhash_reposition(q, rq);
06b86245 412 q->last_merge = rq;
1da177e4
LT
413}
414
165125e1 415struct request *elv_latter_request(struct request_queue *q, struct request *rq)
1da177e4 416{
b374d18a 417 struct elevator_queue *e = q->elevator;
1da177e4 418
f9cd4bfe
JA
419 if (e->type->ops.next_request)
420 return e->type->ops.next_request(q, rq);
bd166ef1 421
1da177e4
LT
422 return NULL;
423}
424
165125e1 425struct request *elv_former_request(struct request_queue *q, struct request *rq)
1da177e4 426{
b374d18a 427 struct elevator_queue *e = q->elevator;
1da177e4 428
f9cd4bfe
JA
429 if (e->type->ops.former_request)
430 return e->type->ops.former_request(q, rq);
bd166ef1 431
a1ce35fa 432 return NULL;
1da177e4
LT
433}
434
3d1ab40f
AV
435#define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
436
437static ssize_t
438elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1da177e4 439{
3d1ab40f 440 struct elv_fs_entry *entry = to_elv(attr);
b374d18a 441 struct elevator_queue *e;
3d1ab40f
AV
442 ssize_t error;
443
444 if (!entry->show)
445 return -EIO;
446
b374d18a 447 e = container_of(kobj, struct elevator_queue, kobj);
3d1ab40f 448 mutex_lock(&e->sysfs_lock);
22f746e2 449 error = e->type ? entry->show(e, page) : -ENOENT;
3d1ab40f
AV
450 mutex_unlock(&e->sysfs_lock);
451 return error;
452}
1da177e4 453
3d1ab40f
AV
454static ssize_t
455elv_attr_store(struct kobject *kobj, struct attribute *attr,
456 const char *page, size_t length)
457{
3d1ab40f 458 struct elv_fs_entry *entry = to_elv(attr);
b374d18a 459 struct elevator_queue *e;
3d1ab40f 460 ssize_t error;
1da177e4 461
3d1ab40f
AV
462 if (!entry->store)
463 return -EIO;
1da177e4 464
b374d18a 465 e = container_of(kobj, struct elevator_queue, kobj);
3d1ab40f 466 mutex_lock(&e->sysfs_lock);
22f746e2 467 error = e->type ? entry->store(e, page, length) : -ENOENT;
3d1ab40f
AV
468 mutex_unlock(&e->sysfs_lock);
469 return error;
470}
471
52cf25d0 472static const struct sysfs_ops elv_sysfs_ops = {
3d1ab40f
AV
473 .show = elv_attr_show,
474 .store = elv_attr_store,
475};
476
477static struct kobj_type elv_ktype = {
478 .sysfs_ops = &elv_sysfs_ops,
479 .release = elevator_release,
480};
481
cecf5d87 482int elv_register_queue(struct request_queue *q, bool uevent)
3d1ab40f 483{
5a5bafdc 484 struct elevator_queue *e = q->elevator;
3d1ab40f
AV
485 int error;
486
f0c6ae09
YY
487 lockdep_assert_held(&q->sysfs_lock);
488
b2d6db58 489 error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
3d1ab40f 490 if (!error) {
22f746e2 491 struct elv_fs_entry *attr = e->type->elevator_attrs;
3d1ab40f 492 if (attr) {
e572ec7e
AV
493 while (attr->attr.name) {
494 if (sysfs_create_file(&e->kobj, &attr->attr))
3d1ab40f 495 break;
e572ec7e 496 attr++;
3d1ab40f
AV
497 }
498 }
cecf5d87
ML
499 if (uevent)
500 kobject_uevent(&e->kobj, KOBJ_ADD);
501
430c62fb 502 e->registered = 1;
3d1ab40f
AV
503 }
504 return error;
1da177e4 505}
bc1c1169 506
1da177e4
LT
507void elv_unregister_queue(struct request_queue *q)
508{
f0c6ae09
YY
509 lockdep_assert_held(&q->sysfs_lock);
510
f8fc877d
TH
511 if (q) {
512 struct elevator_queue *e = q->elevator;
513
514 kobject_uevent(&e->kobj, KOBJ_REMOVE);
515 kobject_del(&e->kobj);
cecf5d87 516
f8fc877d 517 e->registered = 0;
8330cdb0
JK
518 /* Re-enable throttling in case elevator disabled it */
519 wbt_enable_default(q);
f8fc877d 520 }
1da177e4
LT
521}
522
e567bf71 523int elv_register(struct elevator_type *e)
1da177e4 524{
3d3c2379
TH
525 /* create icq_cache if requested */
526 if (e->icq_size) {
527 if (WARN_ON(e->icq_size < sizeof(struct io_cq)) ||
528 WARN_ON(e->icq_align < __alignof__(struct io_cq)))
529 return -EINVAL;
530
531 snprintf(e->icq_cache_name, sizeof(e->icq_cache_name),
532 "%s_io_cq", e->elevator_name);
533 e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size,
534 e->icq_align, 0, NULL);
535 if (!e->icq_cache)
536 return -ENOMEM;
537 }
538
539 /* register, don't allow duplicate names */
2a12dcd7 540 spin_lock(&elv_list_lock);
68c43f13 541 if (elevator_find(e->elevator_name, 0)) {
3d3c2379 542 spin_unlock(&elv_list_lock);
62d2a194 543 kmem_cache_destroy(e->icq_cache);
3d3c2379
TH
544 return -EBUSY;
545 }
1da177e4 546 list_add_tail(&e->list, &elv_list);
2a12dcd7 547 spin_unlock(&elv_list_lock);
1da177e4 548
d0b0a81a
HT
549 printk(KERN_INFO "io scheduler %s registered\n", e->elevator_name);
550
3d3c2379 551 return 0;
1da177e4
LT
552}
553EXPORT_SYMBOL_GPL(elv_register);
554
555void elv_unregister(struct elevator_type *e)
556{
3d3c2379 557 /* unregister */
2a12dcd7 558 spin_lock(&elv_list_lock);
1da177e4 559 list_del_init(&e->list);
2a12dcd7 560 spin_unlock(&elv_list_lock);
3d3c2379
TH
561
562 /*
563 * Destroy icq_cache if it exists. icq's are RCU managed. Make
564 * sure all RCU operations are complete before proceeding.
565 */
566 if (e->icq_cache) {
567 rcu_barrier();
568 kmem_cache_destroy(e->icq_cache);
569 e->icq_cache = NULL;
570 }
1da177e4
LT
571}
572EXPORT_SYMBOL_GPL(elv_unregister);
573
d48ece20 574int elevator_switch_mq(struct request_queue *q,
54d5329d
OS
575 struct elevator_type *new_e)
576{
577 int ret;
578
14a23498
BVA
579 lockdep_assert_held(&q->sysfs_lock);
580
54d5329d 581 if (q->elevator) {
b89f625e 582 if (q->elevator->registered)
54d5329d 583 elv_unregister_queue(q);
cecf5d87 584
54d5329d
OS
585 ioc_clear_queue(q);
586 elevator_exit(q, q->elevator);
587 }
588
589 ret = blk_mq_init_sched(q, new_e);
590 if (ret)
591 goto out;
592
593 if (new_e) {
cecf5d87 594 ret = elv_register_queue(q, true);
54d5329d
OS
595 if (ret) {
596 elevator_exit(q, q->elevator);
597 goto out;
598 }
599 }
600
601 if (new_e)
602 blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name);
603 else
604 blk_add_trace_msg(q, "elv switch: none");
605
606out:
54d5329d 607 return ret;
54d5329d
OS
608}
609
61db437d
DLM
610static inline bool elv_support_iosched(struct request_queue *q)
611{
6251b754 612 if (!queue_is_mq(q) ||
7a7c5e71 613 (q->tag_set && (q->tag_set->flags & BLK_MQ_F_NO_SCHED)))
61db437d
DLM
614 return false;
615 return true;
616}
617
131d08e1 618/*
a0958ba7
DLM
619 * For single queue devices, default to using mq-deadline. If we have multiple
620 * queues or mq-deadline is not available, default to "none".
621 */
622static struct elevator_type *elevator_get_default(struct request_queue *q)
623{
624 if (q->nr_hw_queues != 1)
625 return NULL;
626
627 return elevator_get(q, "mq-deadline", false);
628}
629
630/*
631 * Get the first elevator providing the features required by the request queue.
632 * Default to "none" if no matching elevator is found.
633 */
634static struct elevator_type *elevator_get_by_features(struct request_queue *q)
635{
a2614255 636 struct elevator_type *e, *found = NULL;
a0958ba7
DLM
637
638 spin_lock(&elv_list_lock);
639
640 list_for_each_entry(e, &elv_list, list) {
641 if (elv_support_features(e->elevator_features,
a2614255
JA
642 q->required_elevator_features)) {
643 found = e;
a0958ba7 644 break;
a2614255 645 }
a0958ba7
DLM
646 }
647
a2614255
JA
648 if (found && !try_module_get(found->elevator_owner))
649 found = NULL;
a0958ba7
DLM
650
651 spin_unlock(&elv_list_lock);
a2614255 652 return found;
a0958ba7
DLM
653}
654
655/*
656 * For a device queue that has no required features, use the default elevator
657 * settings. Otherwise, use the first elevator available matching the required
658 * features. If no suitable elevator is find or if the chosen elevator
659 * initialization fails, fall back to the "none" elevator (no elevator).
131d08e1 660 */
954b4a5c 661void elevator_init_mq(struct request_queue *q)
131d08e1
CH
662{
663 struct elevator_type *e;
954b4a5c 664 int err;
131d08e1 665
61db437d 666 if (!elv_support_iosched(q))
954b4a5c 667 return;
61db437d 668
75e6c00f 669 WARN_ON_ONCE(blk_queue_registered(q));
c48dac13 670
131d08e1 671 if (unlikely(q->elevator))
954b4a5c 672 return;
131d08e1 673
a0958ba7
DLM
674 if (!q->required_elevator_features)
675 e = elevator_get_default(q);
676 else
677 e = elevator_get_by_features(q);
131d08e1 678 if (!e)
954b4a5c 679 return;
131d08e1 680
737eb78e
DLM
681 blk_mq_freeze_queue(q);
682 blk_mq_quiesce_queue(q);
683
131d08e1 684 err = blk_mq_init_sched(q, e);
737eb78e
DLM
685
686 blk_mq_unquiesce_queue(q);
687 blk_mq_unfreeze_queue(q);
688
954b4a5c
DLM
689 if (err) {
690 pr_warn("\"%s\" elevator initialization failed, "
691 "falling back to \"none\"\n", e->elevator_name);
131d08e1 692 elevator_put(e);
954b4a5c 693 }
131d08e1
CH
694}
695
696
1da177e4
LT
697/*
698 * switch to new_e io scheduler. be careful not to introduce deadlocks -
699 * we don't free the old io scheduler, before we have allocated what we
700 * need for the new one. this way we have a chance of going back to the old
cb98fc8b 701 * one, if the new one fails init for some reason.
1da177e4 702 */
165125e1 703static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
1da177e4 704{
e8989fae 705 int err;
1da177e4 706
14a23498
BVA
707 lockdep_assert_held(&q->sysfs_lock);
708
a1ce35fa
JA
709 blk_mq_freeze_queue(q);
710 blk_mq_quiesce_queue(q);
4722dc52 711
a1ce35fa 712 err = elevator_switch_mq(q, new_e);
1da177e4 713
a1ce35fa
JA
714 blk_mq_unquiesce_queue(q);
715 blk_mq_unfreeze_queue(q);
75ad23bc 716
5dd531a0 717 return err;
1da177e4
LT
718}
719
5dd531a0
JA
720/*
721 * Switch this queue to the given IO scheduler.
722 */
7c8a3679 723static int __elevator_change(struct request_queue *q, const char *name)
1da177e4
LT
724{
725 char elevator_name[ELV_NAME_MAX];
726 struct elevator_type *e;
727
e9a823fb 728 /* Make sure queue is not in the middle of being removed */
58c898ba 729 if (!blk_queue_registered(q))
e9a823fb
DJ
730 return -ENOENT;
731
bd166ef1
JA
732 /*
733 * Special case for mq, turn off scheduling
734 */
fbd72127
AZ
735 if (!strncmp(name, "none", 4)) {
736 if (!q->elevator)
737 return 0;
bd166ef1 738 return elevator_switch(q, NULL);
fbd72127 739 }
cd43e26f 740
ee2e992c 741 strlcpy(elevator_name, name, sizeof(elevator_name));
2527d997 742 e = elevator_get(q, strstrip(elevator_name), true);
340ff321 743 if (!e)
1da177e4 744 return -EINVAL;
1da177e4 745
68c43f13
DLM
746 if (q->elevator &&
747 elevator_match(q->elevator->type, elevator_name, 0)) {
2ca7d93b 748 elevator_put(e);
5dd531a0 749 return 0;
2ca7d93b 750 }
1da177e4 751
5dd531a0
JA
752 return elevator_switch(q, e);
753}
7c8a3679 754
5dd531a0
JA
755ssize_t elv_iosched_store(struct request_queue *q, const char *name,
756 size_t count)
757{
758 int ret;
759
6251b754 760 if (!elv_support_iosched(q))
5dd531a0
JA
761 return count;
762
7c8a3679 763 ret = __elevator_change(q, name);
5dd531a0
JA
764 if (!ret)
765 return count;
766
5dd531a0 767 return ret;
1da177e4
LT
768}
769
165125e1 770ssize_t elv_iosched_show(struct request_queue *q, char *name)
1da177e4 771{
b374d18a 772 struct elevator_queue *e = q->elevator;
bd166ef1 773 struct elevator_type *elv = NULL;
70cee26e 774 struct elevator_type *__e;
1da177e4
LT
775 int len = 0;
776
344e9ffc 777 if (!queue_is_mq(q))
cd43e26f
MP
778 return sprintf(name, "none\n");
779
bd166ef1
JA
780 if (!q->elevator)
781 len += sprintf(name+len, "[none] ");
782 else
783 elv = e->type;
cd43e26f 784
2a12dcd7 785 spin_lock(&elv_list_lock);
70cee26e 786 list_for_each_entry(__e, &elv_list, list) {
68c43f13 787 if (elv && elevator_match(elv, __e->elevator_name, 0)) {
1da177e4 788 len += sprintf(name+len, "[%s] ", elv->elevator_name);
bd166ef1
JA
789 continue;
790 }
68c43f13
DLM
791 if (elv_support_iosched(q) &&
792 elevator_match(__e, __e->elevator_name,
793 q->required_elevator_features))
1da177e4
LT
794 len += sprintf(name+len, "%s ", __e->elevator_name);
795 }
2a12dcd7 796 spin_unlock(&elv_list_lock);
1da177e4 797
344e9ffc 798 if (q->elevator)
bd166ef1
JA
799 len += sprintf(name+len, "none");
800
1da177e4
LT
801 len += sprintf(len+name, "\n");
802 return len;
803}
804
165125e1
JA
805struct request *elv_rb_former_request(struct request_queue *q,
806 struct request *rq)
2e662b65
JA
807{
808 struct rb_node *rbprev = rb_prev(&rq->rb_node);
809
810 if (rbprev)
811 return rb_entry_rq(rbprev);
812
813 return NULL;
814}
2e662b65
JA
815EXPORT_SYMBOL(elv_rb_former_request);
816
165125e1
JA
817struct request *elv_rb_latter_request(struct request_queue *q,
818 struct request *rq)
2e662b65
JA
819{
820 struct rb_node *rbnext = rb_next(&rq->rb_node);
821
822 if (rbnext)
823 return rb_entry_rq(rbnext);
824
825 return NULL;
826}
2e662b65 827EXPORT_SYMBOL(elv_rb_latter_request);
f8db3835
JK
828
829static int __init elevator_setup(char *str)
830{
831 pr_warn("Kernel parameter elevator= does not have any effect anymore.\n"
832 "Please use sysfs to set IO scheduler for individual devices.\n");
833 return 1;
834}
835
836__setup("elevator=", elevator_setup);