Commit | Line | Data |
---|---|---|
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> | |
1da177e4 | 29 | #include <linux/bio.h> |
1da177e4 LT |
30 | #include <linux/module.h> |
31 | #include <linux/slab.h> | |
32 | #include <linux/init.h> | |
33 | #include <linux/compiler.h> | |
2056a782 | 34 | #include <linux/blktrace_api.h> |
9817064b | 35 | #include <linux/hash.h> |
0835da67 | 36 | #include <linux/uaccess.h> |
c8158819 | 37 | #include <linux/pm_runtime.h> |
1da177e4 | 38 | |
55782138 LZ |
39 | #include <trace/events/block.h> |
40 | ||
2e9bc346 | 41 | #include "elevator.h" |
242f9dcb | 42 | #include "blk.h" |
bd166ef1 | 43 | #include "blk-mq-sched.h" |
bca6b067 | 44 | #include "blk-pm.h" |
8330cdb0 | 45 | #include "blk-wbt.h" |
672fdcf0 | 46 | #include "blk-cgroup.h" |
242f9dcb | 47 | |
1da177e4 LT |
48 | static DEFINE_SPINLOCK(elv_list_lock); |
49 | static 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 | */ | |
8d283ee6 | 60 | static bool 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 | 67 | |
8d283ee6 | 68 | return true; |
da775265 JA |
69 | } |
70 | ||
1da177e4 LT |
71 | /* |
72 | * can we safely merge with this request? | |
73 | */ | |
72ef799b | 74 | bool 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 | 84 | EXPORT_SYMBOL(elv_bio_merge_ok); |
1da177e4 | 85 | |
ffb86425 CH |
86 | static inline bool elv_support_features(struct request_queue *q, |
87 | const struct elevator_type *e) | |
8ac0d9a8 | 88 | { |
ffb86425 CH |
89 | return (q->required_elevator_features & e->elevator_features) == |
90 | q->required_elevator_features; | |
68c43f13 DLM |
91 | } |
92 | ||
93 | /** | |
f69b5e8f | 94 | * elevator_match - Check whether @e's name or alias matches @name |
68c43f13 DLM |
95 | * @e: Scheduler to test |
96 | * @name: Elevator name to test | |
68c43f13 | 97 | * |
f69b5e8f | 98 | * Return true if the elevator @e's name or alias matches @name. |
68c43f13 | 99 | */ |
ffb86425 | 100 | static bool elevator_match(const struct elevator_type *e, const char *name) |
68c43f13 | 101 | { |
ffb86425 CH |
102 | return !strcmp(e->elevator_name, name) || |
103 | (e->elevator_alias && !strcmp(e->elevator_alias, name)); | |
8ac0d9a8 JA |
104 | } |
105 | ||
ffb86425 | 106 | static struct elevator_type *__elevator_find(const char *name) |
1da177e4 | 107 | { |
a22b169d | 108 | struct elevator_type *e; |
1da177e4 | 109 | |
ffb86425 CH |
110 | list_for_each_entry(e, &elv_list, list) |
111 | if (elevator_match(e, name)) | |
a22b169d | 112 | return e; |
a22b169d | 113 | return NULL; |
1da177e4 LT |
114 | } |
115 | ||
81eaca44 CH |
116 | static struct elevator_type *elevator_find_get(struct request_queue *q, |
117 | const char *name) | |
1da177e4 | 118 | { |
2824bc93 | 119 | struct elevator_type *e; |
1da177e4 | 120 | |
2a12dcd7 | 121 | spin_lock(&elv_list_lock); |
ffb86425 CH |
122 | e = __elevator_find(name); |
123 | if (e && (!elv_support_features(q, e) || !elevator_tryget(e))) | |
2824bc93 | 124 | e = NULL; |
2a12dcd7 | 125 | spin_unlock(&elv_list_lock); |
1da177e4 LT |
126 | return e; |
127 | } | |
128 | ||
5f622417 | 129 | static const struct kobj_type elv_ktype; |
3d1ab40f | 130 | |
d50235b7 | 131 | struct elevator_queue *elevator_alloc(struct request_queue *q, |
165125e1 | 132 | struct elevator_type *e) |
3d1ab40f | 133 | { |
b374d18a | 134 | struct elevator_queue *eq; |
9817064b | 135 | |
c1b511eb | 136 | eq = kzalloc_node(sizeof(*eq), GFP_KERNEL, q->node); |
9817064b | 137 | if (unlikely(!eq)) |
8406a4d5 | 138 | return NULL; |
9817064b | 139 | |
8ed40ee3 | 140 | __elevator_get(e); |
22f746e2 | 141 | eq->type = e; |
f9cb074b | 142 | kobject_init(&eq->kobj, &elv_ktype); |
9817064b | 143 | mutex_init(&eq->sysfs_lock); |
242d98f0 | 144 | hash_init(eq->hash); |
9817064b | 145 | |
3d1ab40f AV |
146 | return eq; |
147 | } | |
d50235b7 | 148 | EXPORT_SYMBOL(elevator_alloc); |
3d1ab40f AV |
149 | |
150 | static void elevator_release(struct kobject *kobj) | |
151 | { | |
b374d18a | 152 | struct elevator_queue *e; |
9817064b | 153 | |
b374d18a | 154 | e = container_of(kobj, struct elevator_queue, kobj); |
22f746e2 | 155 | elevator_put(e->type); |
3d1ab40f AV |
156 | kfree(e); |
157 | } | |
158 | ||
0c6cb3a2 | 159 | void elevator_exit(struct request_queue *q) |
1da177e4 | 160 | { |
0c6cb3a2 CH |
161 | struct elevator_queue *e = q->elevator; |
162 | ||
28883074 CH |
163 | ioc_clear_queue(q); |
164 | blk_mq_sched_free_rqs(q); | |
165 | ||
3d1ab40f | 166 | mutex_lock(&e->sysfs_lock); |
dd1c372d | 167 | blk_mq_exit_sched(q, e); |
3d1ab40f | 168 | mutex_unlock(&e->sysfs_lock); |
1da177e4 | 169 | |
3d1ab40f | 170 | kobject_put(&e->kobj); |
1da177e4 | 171 | } |
2e662b65 | 172 | |
9817064b JA |
173 | static inline void __elv_rqhash_del(struct request *rq) |
174 | { | |
242d98f0 | 175 | hash_del(&rq->hash); |
e8064021 | 176 | rq->rq_flags &= ~RQF_HASHED; |
9817064b JA |
177 | } |
178 | ||
70b3ea05 | 179 | void elv_rqhash_del(struct request_queue *q, struct request *rq) |
9817064b JA |
180 | { |
181 | if (ELV_ON_HASH(rq)) | |
182 | __elv_rqhash_del(rq); | |
183 | } | |
bd166ef1 | 184 | EXPORT_SYMBOL_GPL(elv_rqhash_del); |
9817064b | 185 | |
70b3ea05 | 186 | void elv_rqhash_add(struct request_queue *q, struct request *rq) |
9817064b | 187 | { |
b374d18a | 188 | struct elevator_queue *e = q->elevator; |
9817064b JA |
189 | |
190 | BUG_ON(ELV_ON_HASH(rq)); | |
242d98f0 | 191 | hash_add(e->hash, &rq->hash, rq_hash_key(rq)); |
e8064021 | 192 | rq->rq_flags |= RQF_HASHED; |
9817064b | 193 | } |
bd166ef1 | 194 | EXPORT_SYMBOL_GPL(elv_rqhash_add); |
9817064b | 195 | |
70b3ea05 | 196 | void elv_rqhash_reposition(struct request_queue *q, struct request *rq) |
9817064b JA |
197 | { |
198 | __elv_rqhash_del(rq); | |
199 | elv_rqhash_add(q, rq); | |
200 | } | |
201 | ||
70b3ea05 | 202 | struct request *elv_rqhash_find(struct request_queue *q, sector_t offset) |
9817064b | 203 | { |
b374d18a | 204 | struct elevator_queue *e = q->elevator; |
b67bfe0d | 205 | struct hlist_node *next; |
9817064b JA |
206 | struct request *rq; |
207 | ||
ee89f812 | 208 | hash_for_each_possible_safe(e->hash, rq, next, hash, offset) { |
9817064b JA |
209 | BUG_ON(!ELV_ON_HASH(rq)); |
210 | ||
211 | if (unlikely(!rq_mergeable(rq))) { | |
212 | __elv_rqhash_del(rq); | |
213 | continue; | |
214 | } | |
215 | ||
216 | if (rq_hash_key(rq) == offset) | |
217 | return rq; | |
218 | } | |
219 | ||
220 | return NULL; | |
221 | } | |
222 | ||
2e662b65 JA |
223 | /* |
224 | * RB-tree support functions for inserting/lookup/removal of requests | |
225 | * in a sorted RB tree. | |
226 | */ | |
796d5116 | 227 | void elv_rb_add(struct rb_root *root, struct request *rq) |
2e662b65 JA |
228 | { |
229 | struct rb_node **p = &root->rb_node; | |
230 | struct rb_node *parent = NULL; | |
231 | struct request *__rq; | |
232 | ||
233 | while (*p) { | |
234 | parent = *p; | |
235 | __rq = rb_entry(parent, struct request, rb_node); | |
236 | ||
83096ebf | 237 | if (blk_rq_pos(rq) < blk_rq_pos(__rq)) |
2e662b65 | 238 | p = &(*p)->rb_left; |
796d5116 | 239 | else if (blk_rq_pos(rq) >= blk_rq_pos(__rq)) |
2e662b65 | 240 | p = &(*p)->rb_right; |
2e662b65 JA |
241 | } |
242 | ||
243 | rb_link_node(&rq->rb_node, parent, p); | |
244 | rb_insert_color(&rq->rb_node, root); | |
2e662b65 | 245 | } |
2e662b65 JA |
246 | EXPORT_SYMBOL(elv_rb_add); |
247 | ||
248 | void elv_rb_del(struct rb_root *root, struct request *rq) | |
249 | { | |
250 | BUG_ON(RB_EMPTY_NODE(&rq->rb_node)); | |
251 | rb_erase(&rq->rb_node, root); | |
252 | RB_CLEAR_NODE(&rq->rb_node); | |
253 | } | |
2e662b65 JA |
254 | EXPORT_SYMBOL(elv_rb_del); |
255 | ||
256 | struct request *elv_rb_find(struct rb_root *root, sector_t sector) | |
257 | { | |
258 | struct rb_node *n = root->rb_node; | |
259 | struct request *rq; | |
260 | ||
261 | while (n) { | |
262 | rq = rb_entry(n, struct request, rb_node); | |
263 | ||
83096ebf | 264 | if (sector < blk_rq_pos(rq)) |
2e662b65 | 265 | n = n->rb_left; |
83096ebf | 266 | else if (sector > blk_rq_pos(rq)) |
2e662b65 JA |
267 | n = n->rb_right; |
268 | else | |
269 | return rq; | |
270 | } | |
271 | ||
272 | return NULL; | |
273 | } | |
2e662b65 JA |
274 | EXPORT_SYMBOL(elv_rb_find); |
275 | ||
34fe7c05 CH |
276 | enum elv_merge elv_merge(struct request_queue *q, struct request **req, |
277 | struct bio *bio) | |
1da177e4 | 278 | { |
b374d18a | 279 | struct elevator_queue *e = q->elevator; |
9817064b | 280 | struct request *__rq; |
06b86245 | 281 | |
488991e2 AB |
282 | /* |
283 | * Levels of merges: | |
284 | * nomerges: No merges at all attempted | |
285 | * noxmerges: Only simple one-hit cache try | |
286 | * merges: All merge tries attempted | |
287 | */ | |
7460d389 | 288 | if (blk_queue_nomerges(q) || !bio_mergeable(bio)) |
488991e2 AB |
289 | return ELEVATOR_NO_MERGE; |
290 | ||
9817064b JA |
291 | /* |
292 | * First try one-hit cache. | |
293 | */ | |
72ef799b | 294 | if (q->last_merge && elv_bio_merge_ok(q->last_merge, bio)) { |
34fe7c05 CH |
295 | enum elv_merge ret = blk_try_merge(q->last_merge, bio); |
296 | ||
06b86245 TH |
297 | if (ret != ELEVATOR_NO_MERGE) { |
298 | *req = q->last_merge; | |
299 | return ret; | |
300 | } | |
301 | } | |
1da177e4 | 302 | |
488991e2 | 303 | if (blk_queue_noxmerges(q)) |
ac9fafa1 AB |
304 | return ELEVATOR_NO_MERGE; |
305 | ||
9817064b JA |
306 | /* |
307 | * See if our hash lookup can find a potential backmerge. | |
308 | */ | |
4f024f37 | 309 | __rq = elv_rqhash_find(q, bio->bi_iter.bi_sector); |
72ef799b | 310 | if (__rq && elv_bio_merge_ok(__rq, bio)) { |
9817064b | 311 | *req = __rq; |
866663b7 ML |
312 | |
313 | if (blk_discard_mergable(__rq)) | |
314 | return ELEVATOR_DISCARD_MERGE; | |
9817064b JA |
315 | return ELEVATOR_BACK_MERGE; |
316 | } | |
317 | ||
f9cd4bfe JA |
318 | if (e->type->ops.request_merge) |
319 | return e->type->ops.request_merge(q, req, bio); | |
1da177e4 LT |
320 | |
321 | return ELEVATOR_NO_MERGE; | |
322 | } | |
323 | ||
5e84ea3a JA |
324 | /* |
325 | * Attempt to do an insertion back merge. Only check for the case where | |
326 | * we can append 'rq' to an existing request, so we can throw 'rq' away | |
327 | * afterwards. | |
328 | * | |
fd2ef39c JK |
329 | * Returns true if we merged, false otherwise. 'free' will contain all |
330 | * requests that need to be freed. | |
5e84ea3a | 331 | */ |
fd2ef39c JK |
332 | bool elv_attempt_insert_merge(struct request_queue *q, struct request *rq, |
333 | struct list_head *free) | |
5e84ea3a JA |
334 | { |
335 | struct request *__rq; | |
bee0393c | 336 | bool ret; |
5e84ea3a JA |
337 | |
338 | if (blk_queue_nomerges(q)) | |
339 | return false; | |
340 | ||
341 | /* | |
342 | * First try one-hit cache. | |
343 | */ | |
fd2ef39c JK |
344 | if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq)) { |
345 | list_add(&rq->queuelist, free); | |
5e84ea3a | 346 | return true; |
fd2ef39c | 347 | } |
5e84ea3a JA |
348 | |
349 | if (blk_queue_noxmerges(q)) | |
350 | return false; | |
351 | ||
bee0393c | 352 | ret = false; |
5e84ea3a JA |
353 | /* |
354 | * See if our hash lookup can find a potential backmerge. | |
355 | */ | |
bee0393c SL |
356 | while (1) { |
357 | __rq = elv_rqhash_find(q, blk_rq_pos(rq)); | |
358 | if (!__rq || !blk_attempt_req_merge(q, __rq, rq)) | |
359 | break; | |
360 | ||
fd2ef39c | 361 | list_add(&rq->queuelist, free); |
bee0393c SL |
362 | /* The merged request could be merged with others, try again */ |
363 | ret = true; | |
364 | rq = __rq; | |
365 | } | |
27419322 | 366 | |
bee0393c | 367 | return ret; |
5e84ea3a JA |
368 | } |
369 | ||
34fe7c05 CH |
370 | void elv_merged_request(struct request_queue *q, struct request *rq, |
371 | enum elv_merge type) | |
1da177e4 | 372 | { |
b374d18a | 373 | struct elevator_queue *e = q->elevator; |
1da177e4 | 374 | |
f9cd4bfe JA |
375 | if (e->type->ops.request_merged) |
376 | e->type->ops.request_merged(q, rq, type); | |
06b86245 | 377 | |
2e662b65 JA |
378 | if (type == ELEVATOR_BACK_MERGE) |
379 | elv_rqhash_reposition(q, rq); | |
9817064b | 380 | |
06b86245 | 381 | q->last_merge = rq; |
1da177e4 LT |
382 | } |
383 | ||
165125e1 | 384 | void elv_merge_requests(struct request_queue *q, struct request *rq, |
1da177e4 LT |
385 | struct request *next) |
386 | { | |
b374d18a | 387 | struct elevator_queue *e = q->elevator; |
bd166ef1 | 388 | |
f9cd4bfe JA |
389 | if (e->type->ops.requests_merged) |
390 | e->type->ops.requests_merged(q, rq, next); | |
06b86245 | 391 | |
9817064b | 392 | elv_rqhash_reposition(q, rq); |
06b86245 | 393 | q->last_merge = rq; |
1da177e4 LT |
394 | } |
395 | ||
165125e1 | 396 | struct request *elv_latter_request(struct request_queue *q, struct request *rq) |
1da177e4 | 397 | { |
b374d18a | 398 | struct elevator_queue *e = q->elevator; |
1da177e4 | 399 | |
f9cd4bfe JA |
400 | if (e->type->ops.next_request) |
401 | return e->type->ops.next_request(q, rq); | |
bd166ef1 | 402 | |
1da177e4 LT |
403 | return NULL; |
404 | } | |
405 | ||
165125e1 | 406 | struct request *elv_former_request(struct request_queue *q, struct request *rq) |
1da177e4 | 407 | { |
b374d18a | 408 | struct elevator_queue *e = q->elevator; |
1da177e4 | 409 | |
f9cd4bfe JA |
410 | if (e->type->ops.former_request) |
411 | return e->type->ops.former_request(q, rq); | |
bd166ef1 | 412 | |
a1ce35fa | 413 | return NULL; |
1da177e4 LT |
414 | } |
415 | ||
3d1ab40f AV |
416 | #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr) |
417 | ||
418 | static ssize_t | |
419 | elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page) | |
1da177e4 | 420 | { |
3d1ab40f | 421 | struct elv_fs_entry *entry = to_elv(attr); |
b374d18a | 422 | struct elevator_queue *e; |
3d1ab40f AV |
423 | ssize_t error; |
424 | ||
425 | if (!entry->show) | |
426 | return -EIO; | |
427 | ||
b374d18a | 428 | e = container_of(kobj, struct elevator_queue, kobj); |
3d1ab40f | 429 | mutex_lock(&e->sysfs_lock); |
22f746e2 | 430 | error = e->type ? entry->show(e, page) : -ENOENT; |
3d1ab40f AV |
431 | mutex_unlock(&e->sysfs_lock); |
432 | return error; | |
433 | } | |
1da177e4 | 434 | |
3d1ab40f AV |
435 | static ssize_t |
436 | elv_attr_store(struct kobject *kobj, struct attribute *attr, | |
437 | const char *page, size_t length) | |
438 | { | |
3d1ab40f | 439 | struct elv_fs_entry *entry = to_elv(attr); |
b374d18a | 440 | struct elevator_queue *e; |
3d1ab40f | 441 | ssize_t error; |
1da177e4 | 442 | |
3d1ab40f AV |
443 | if (!entry->store) |
444 | return -EIO; | |
1da177e4 | 445 | |
b374d18a | 446 | e = container_of(kobj, struct elevator_queue, kobj); |
3d1ab40f | 447 | mutex_lock(&e->sysfs_lock); |
22f746e2 | 448 | error = e->type ? entry->store(e, page, length) : -ENOENT; |
3d1ab40f AV |
449 | mutex_unlock(&e->sysfs_lock); |
450 | return error; | |
451 | } | |
452 | ||
52cf25d0 | 453 | static const struct sysfs_ops elv_sysfs_ops = { |
3d1ab40f AV |
454 | .show = elv_attr_show, |
455 | .store = elv_attr_store, | |
456 | }; | |
457 | ||
5f622417 | 458 | static const struct kobj_type elv_ktype = { |
3d1ab40f AV |
459 | .sysfs_ops = &elv_sysfs_ops, |
460 | .release = elevator_release, | |
461 | }; | |
462 | ||
cecf5d87 | 463 | int elv_register_queue(struct request_queue *q, bool uevent) |
3d1ab40f | 464 | { |
5a5bafdc | 465 | struct elevator_queue *e = q->elevator; |
3d1ab40f AV |
466 | int error; |
467 | ||
f0c6ae09 YY |
468 | lockdep_assert_held(&q->sysfs_lock); |
469 | ||
2bd85221 | 470 | error = kobject_add(&e->kobj, &q->disk->queue_kobj, "iosched"); |
3d1ab40f | 471 | if (!error) { |
22f746e2 | 472 | struct elv_fs_entry *attr = e->type->elevator_attrs; |
3d1ab40f | 473 | if (attr) { |
e572ec7e AV |
474 | while (attr->attr.name) { |
475 | if (sysfs_create_file(&e->kobj, &attr->attr)) | |
3d1ab40f | 476 | break; |
e572ec7e | 477 | attr++; |
3d1ab40f AV |
478 | } |
479 | } | |
cecf5d87 ML |
480 | if (uevent) |
481 | kobject_uevent(&e->kobj, KOBJ_ADD); | |
482 | ||
181d0663 | 483 | set_bit(ELEVATOR_FLAG_REGISTERED, &e->flags); |
3d1ab40f AV |
484 | } |
485 | return error; | |
1da177e4 | 486 | } |
bc1c1169 | 487 | |
1da177e4 LT |
488 | void elv_unregister_queue(struct request_queue *q) |
489 | { | |
f5ec592d EB |
490 | struct elevator_queue *e = q->elevator; |
491 | ||
f0c6ae09 YY |
492 | lockdep_assert_held(&q->sysfs_lock); |
493 | ||
181d0663 | 494 | if (e && test_and_clear_bit(ELEVATOR_FLAG_REGISTERED, &e->flags)) { |
f8fc877d TH |
495 | kobject_uevent(&e->kobj, KOBJ_REMOVE); |
496 | kobject_del(&e->kobj); | |
f8fc877d | 497 | } |
1da177e4 LT |
498 | } |
499 | ||
e567bf71 | 500 | int elv_register(struct elevator_type *e) |
1da177e4 | 501 | { |
e42cfb1d DLM |
502 | /* insert_requests and dispatch_request are mandatory */ |
503 | if (WARN_ON_ONCE(!e->ops.insert_requests || !e->ops.dispatch_request)) | |
504 | return -EINVAL; | |
505 | ||
3d3c2379 TH |
506 | /* create icq_cache if requested */ |
507 | if (e->icq_size) { | |
508 | if (WARN_ON(e->icq_size < sizeof(struct io_cq)) || | |
509 | WARN_ON(e->icq_align < __alignof__(struct io_cq))) | |
510 | return -EINVAL; | |
511 | ||
512 | snprintf(e->icq_cache_name, sizeof(e->icq_cache_name), | |
513 | "%s_io_cq", e->elevator_name); | |
514 | e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size, | |
515 | e->icq_align, 0, NULL); | |
516 | if (!e->icq_cache) | |
517 | return -ENOMEM; | |
518 | } | |
519 | ||
520 | /* register, don't allow duplicate names */ | |
2a12dcd7 | 521 | spin_lock(&elv_list_lock); |
ffb86425 | 522 | if (__elevator_find(e->elevator_name)) { |
3d3c2379 | 523 | spin_unlock(&elv_list_lock); |
62d2a194 | 524 | kmem_cache_destroy(e->icq_cache); |
3d3c2379 TH |
525 | return -EBUSY; |
526 | } | |
1da177e4 | 527 | list_add_tail(&e->list, &elv_list); |
2a12dcd7 | 528 | spin_unlock(&elv_list_lock); |
1da177e4 | 529 | |
d0b0a81a HT |
530 | printk(KERN_INFO "io scheduler %s registered\n", e->elevator_name); |
531 | ||
3d3c2379 | 532 | return 0; |
1da177e4 LT |
533 | } |
534 | EXPORT_SYMBOL_GPL(elv_register); | |
535 | ||
536 | void elv_unregister(struct elevator_type *e) | |
537 | { | |
3d3c2379 | 538 | /* unregister */ |
2a12dcd7 | 539 | spin_lock(&elv_list_lock); |
1da177e4 | 540 | list_del_init(&e->list); |
2a12dcd7 | 541 | spin_unlock(&elv_list_lock); |
3d3c2379 TH |
542 | |
543 | /* | |
544 | * Destroy icq_cache if it exists. icq's are RCU managed. Make | |
545 | * sure all RCU operations are complete before proceeding. | |
546 | */ | |
547 | if (e->icq_cache) { | |
548 | rcu_barrier(); | |
549 | kmem_cache_destroy(e->icq_cache); | |
550 | e->icq_cache = NULL; | |
551 | } | |
1da177e4 LT |
552 | } |
553 | EXPORT_SYMBOL_GPL(elv_unregister); | |
554 | ||
61db437d DLM |
555 | static inline bool elv_support_iosched(struct request_queue *q) |
556 | { | |
6251b754 | 557 | if (!queue_is_mq(q) || |
7a7c5e71 | 558 | (q->tag_set && (q->tag_set->flags & BLK_MQ_F_NO_SCHED))) |
61db437d DLM |
559 | return false; |
560 | return true; | |
561 | } | |
562 | ||
131d08e1 | 563 | /* |
a0958ba7 DLM |
564 | * For single queue devices, default to using mq-deadline. If we have multiple |
565 | * queues or mq-deadline is not available, default to "none". | |
566 | */ | |
567 | static struct elevator_type *elevator_get_default(struct request_queue *q) | |
568 | { | |
90b71980 BVA |
569 | if (q->tag_set && q->tag_set->flags & BLK_MQ_F_NO_SCHED_BY_DEFAULT) |
570 | return NULL; | |
571 | ||
580dca81 | 572 | if (q->nr_hw_queues != 1 && |
079a2e3e | 573 | !blk_mq_is_shared_tags(q->tag_set->flags)) |
a0958ba7 DLM |
574 | return NULL; |
575 | ||
81eaca44 | 576 | return elevator_find_get(q, "mq-deadline"); |
a0958ba7 DLM |
577 | } |
578 | ||
579 | /* | |
580 | * Get the first elevator providing the features required by the request queue. | |
581 | * Default to "none" if no matching elevator is found. | |
582 | */ | |
583 | static struct elevator_type *elevator_get_by_features(struct request_queue *q) | |
584 | { | |
a2614255 | 585 | struct elevator_type *e, *found = NULL; |
a0958ba7 DLM |
586 | |
587 | spin_lock(&elv_list_lock); | |
588 | ||
589 | list_for_each_entry(e, &elv_list, list) { | |
ffb86425 | 590 | if (elv_support_features(q, e)) { |
a2614255 | 591 | found = e; |
a0958ba7 | 592 | break; |
a2614255 | 593 | } |
a0958ba7 DLM |
594 | } |
595 | ||
dd6f7f17 | 596 | if (found && !elevator_tryget(found)) |
a2614255 | 597 | found = NULL; |
a0958ba7 DLM |
598 | |
599 | spin_unlock(&elv_list_lock); | |
a2614255 | 600 | return found; |
a0958ba7 DLM |
601 | } |
602 | ||
603 | /* | |
604 | * For a device queue that has no required features, use the default elevator | |
605 | * settings. Otherwise, use the first elevator available matching the required | |
606 | * features. If no suitable elevator is find or if the chosen elevator | |
607 | * initialization fails, fall back to the "none" elevator (no elevator). | |
131d08e1 | 608 | */ |
954b4a5c | 609 | void elevator_init_mq(struct request_queue *q) |
131d08e1 CH |
610 | { |
611 | struct elevator_type *e; | |
954b4a5c | 612 | int err; |
131d08e1 | 613 | |
61db437d | 614 | if (!elv_support_iosched(q)) |
954b4a5c | 615 | return; |
61db437d | 616 | |
75e6c00f | 617 | WARN_ON_ONCE(blk_queue_registered(q)); |
c48dac13 | 618 | |
131d08e1 | 619 | if (unlikely(q->elevator)) |
954b4a5c | 620 | return; |
131d08e1 | 621 | |
a0958ba7 DLM |
622 | if (!q->required_elevator_features) |
623 | e = elevator_get_default(q); | |
624 | else | |
625 | e = elevator_get_by_features(q); | |
131d08e1 | 626 | if (!e) |
954b4a5c | 627 | return; |
131d08e1 | 628 | |
245a489e ML |
629 | /* |
630 | * We are called before adding disk, when there isn't any FS I/O, | |
631 | * so freezing queue plus canceling dispatch work is enough to | |
632 | * drain any dispatch activities originated from passthrough | |
633 | * requests, then no need to quiesce queue which may add long boot | |
634 | * latency, especially when lots of disks are involved. | |
635 | */ | |
737eb78e | 636 | blk_mq_freeze_queue(q); |
245a489e | 637 | blk_mq_cancel_work_sync(q); |
737eb78e | 638 | |
131d08e1 | 639 | err = blk_mq_init_sched(q, e); |
737eb78e | 640 | |
737eb78e DLM |
641 | blk_mq_unfreeze_queue(q); |
642 | ||
954b4a5c DLM |
643 | if (err) { |
644 | pr_warn("\"%s\" elevator initialization failed, " | |
645 | "falling back to \"none\"\n", e->elevator_name); | |
954b4a5c | 646 | } |
8ed40ee3 JC |
647 | |
648 | elevator_put(e); | |
131d08e1 | 649 | } |
131d08e1 | 650 | |
1da177e4 | 651 | /* |
ac1171bd JC |
652 | * Switch to new_e io scheduler. |
653 | * | |
654 | * If switching fails, we are most likely running out of memory and not able | |
655 | * to restore the old io scheduler, so leaving the io scheduler being none. | |
1da177e4 | 656 | */ |
8237c01f | 657 | int elevator_switch(struct request_queue *q, struct elevator_type *new_e) |
1da177e4 | 658 | { |
64b36075 | 659 | int ret; |
1da177e4 | 660 | |
14a23498 BVA |
661 | lockdep_assert_held(&q->sysfs_lock); |
662 | ||
a1ce35fa JA |
663 | blk_mq_freeze_queue(q); |
664 | blk_mq_quiesce_queue(q); | |
4722dc52 | 665 | |
64b36075 CH |
666 | if (q->elevator) { |
667 | elv_unregister_queue(q); | |
668 | elevator_exit(q); | |
669 | } | |
670 | ||
671 | ret = blk_mq_init_sched(q, new_e); | |
672 | if (ret) | |
673 | goto out_unfreeze; | |
674 | ||
675 | ret = elv_register_queue(q, true); | |
676 | if (ret) { | |
677 | elevator_exit(q); | |
678 | goto out_unfreeze; | |
679 | } | |
680 | blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name); | |
1da177e4 | 681 | |
64b36075 | 682 | out_unfreeze: |
a1ce35fa JA |
683 | blk_mq_unquiesce_queue(q); |
684 | blk_mq_unfreeze_queue(q); | |
e0cca8bc JC |
685 | |
686 | if (ret) { | |
687 | pr_warn("elv: switch to \"%s\" failed, falling back to \"none\"\n", | |
688 | new_e->elevator_name); | |
689 | } | |
690 | ||
64b36075 CH |
691 | return ret; |
692 | } | |
693 | ||
694 | void elevator_disable(struct request_queue *q) | |
695 | { | |
696 | lockdep_assert_held(&q->sysfs_lock); | |
75ad23bc | 697 | |
64b36075 CH |
698 | blk_mq_freeze_queue(q); |
699 | blk_mq_quiesce_queue(q); | |
700 | ||
701 | elv_unregister_queue(q); | |
702 | elevator_exit(q); | |
703 | blk_queue_flag_clear(QUEUE_FLAG_SQ_SCHED, q); | |
704 | q->elevator = NULL; | |
705 | q->nr_requests = q->tag_set->queue_depth; | |
706 | blk_add_trace_msg(q, "elv switch: none"); | |
707 | ||
708 | blk_mq_unquiesce_queue(q); | |
709 | blk_mq_unfreeze_queue(q); | |
1da177e4 LT |
710 | } |
711 | ||
5dd531a0 JA |
712 | /* |
713 | * Switch this queue to the given IO scheduler. | |
714 | */ | |
58367c8a | 715 | static int elevator_change(struct request_queue *q, const char *elevator_name) |
1da177e4 | 716 | { |
1da177e4 | 717 | struct elevator_type *e; |
8ed40ee3 | 718 | int ret; |
1da177e4 | 719 | |
e9a823fb | 720 | /* Make sure queue is not in the middle of being removed */ |
58c898ba | 721 | if (!blk_queue_registered(q)) |
e9a823fb DJ |
722 | return -ENOENT; |
723 | ||
58367c8a | 724 | if (!strncmp(elevator_name, "none", 4)) { |
64b36075 CH |
725 | if (q->elevator) |
726 | elevator_disable(q); | |
727 | return 0; | |
fbd72127 | 728 | } |
cd43e26f | 729 | |
ffb86425 | 730 | if (q->elevator && elevator_match(q->elevator->type, elevator_name)) |
b54c2ad9 JC |
731 | return 0; |
732 | ||
81eaca44 CH |
733 | e = elevator_find_get(q, elevator_name); |
734 | if (!e) { | |
735 | request_module("%s-iosched", elevator_name); | |
736 | e = elevator_find_get(q, elevator_name); | |
737 | if (!e) | |
738 | return -EINVAL; | |
739 | } | |
8ed40ee3 JC |
740 | ret = elevator_switch(q, e); |
741 | elevator_put(e); | |
742 | return ret; | |
5dd531a0 | 743 | } |
7c8a3679 | 744 | |
58367c8a | 745 | ssize_t elv_iosched_store(struct request_queue *q, const char *buf, |
5dd531a0 JA |
746 | size_t count) |
747 | { | |
58367c8a | 748 | char elevator_name[ELV_NAME_MAX]; |
5dd531a0 JA |
749 | int ret; |
750 | ||
6251b754 | 751 | if (!elv_support_iosched(q)) |
5dd531a0 JA |
752 | return count; |
753 | ||
20d09975 | 754 | strscpy(elevator_name, buf, sizeof(elevator_name)); |
58367c8a | 755 | ret = elevator_change(q, strstrip(elevator_name)); |
5dd531a0 JA |
756 | if (!ret) |
757 | return count; | |
5dd531a0 | 758 | return ret; |
1da177e4 LT |
759 | } |
760 | ||
165125e1 | 761 | ssize_t elv_iosched_show(struct request_queue *q, char *name) |
1da177e4 | 762 | { |
16095af2 CH |
763 | struct elevator_queue *eq = q->elevator; |
764 | struct elevator_type *cur = NULL, *e; | |
1da177e4 LT |
765 | int len = 0; |
766 | ||
aae2a643 | 767 | if (!elv_support_iosched(q)) |
cd43e26f MP |
768 | return sprintf(name, "none\n"); |
769 | ||
7919d679 | 770 | if (!q->elevator) { |
bd166ef1 | 771 | len += sprintf(name+len, "[none] "); |
7919d679 JC |
772 | } else { |
773 | len += sprintf(name+len, "none "); | |
16095af2 | 774 | cur = eq->type; |
7919d679 | 775 | } |
cd43e26f | 776 | |
2a12dcd7 | 777 | spin_lock(&elv_list_lock); |
16095af2 | 778 | list_for_each_entry(e, &elv_list, list) { |
5998249e | 779 | if (e == cur) |
7a3b3660 | 780 | len += sprintf(name+len, "[%s] ", e->elevator_name); |
5998249e | 781 | else if (elv_support_features(q, e)) |
16095af2 | 782 | len += sprintf(name+len, "%s ", e->elevator_name); |
1da177e4 | 783 | } |
2a12dcd7 | 784 | spin_unlock(&elv_list_lock); |
1da177e4 | 785 | |
c6451ede | 786 | len += sprintf(name+len, "\n"); |
1da177e4 LT |
787 | return len; |
788 | } | |
789 | ||
165125e1 JA |
790 | struct request *elv_rb_former_request(struct request_queue *q, |
791 | struct request *rq) | |
2e662b65 JA |
792 | { |
793 | struct rb_node *rbprev = rb_prev(&rq->rb_node); | |
794 | ||
795 | if (rbprev) | |
796 | return rb_entry_rq(rbprev); | |
797 | ||
798 | return NULL; | |
799 | } | |
2e662b65 JA |
800 | EXPORT_SYMBOL(elv_rb_former_request); |
801 | ||
165125e1 JA |
802 | struct request *elv_rb_latter_request(struct request_queue *q, |
803 | struct request *rq) | |
2e662b65 JA |
804 | { |
805 | struct rb_node *rbnext = rb_next(&rq->rb_node); | |
806 | ||
807 | if (rbnext) | |
808 | return rb_entry_rq(rbnext); | |
809 | ||
810 | return NULL; | |
811 | } | |
2e662b65 | 812 | EXPORT_SYMBOL(elv_rb_latter_request); |
f8db3835 JK |
813 | |
814 | static int __init elevator_setup(char *str) | |
815 | { | |
816 | pr_warn("Kernel parameter elevator= does not have any effect anymore.\n" | |
817 | "Please use sysfs to set IO scheduler for individual devices.\n"); | |
818 | return 1; | |
819 | } | |
820 | ||
821 | __setup("elevator=", elevator_setup); |