]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * Block device elevator/IO-scheduler. |
3 | * | |
4 | * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE | |
5 | * | |
0fe23479 | 6 | * 30042000 Jens Axboe <axboe@kernel.dk> : |
1da177e4 LT |
7 | * |
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> | |
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> |
eea8f41c | 38 | #include <linux/blk-cgroup.h> |
1da177e4 | 39 | |
55782138 LZ |
40 | #include <trace/events/block.h> |
41 | ||
242f9dcb | 42 | #include "blk.h" |
bd166ef1 | 43 | #include "blk-mq-sched.h" |
8330cdb0 | 44 | #include "blk-wbt.h" |
242f9dcb | 45 | |
1da177e4 LT |
46 | static DEFINE_SPINLOCK(elv_list_lock); |
47 | static LIST_HEAD(elv_list); | |
48 | ||
9817064b JA |
49 | /* |
50 | * Merge hash stuff. | |
51 | */ | |
83096ebf | 52 | #define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq)) |
9817064b | 53 | |
da775265 JA |
54 | /* |
55 | * Query io scheduler to see if the current process issuing bio may be | |
56 | * merged with rq. | |
57 | */ | |
72ef799b | 58 | static int elv_iosched_allow_bio_merge(struct request *rq, struct bio *bio) |
da775265 | 59 | { |
165125e1 | 60 | struct request_queue *q = rq->q; |
b374d18a | 61 | struct elevator_queue *e = q->elevator; |
da775265 | 62 | |
bd166ef1 JA |
63 | if (e->uses_mq && e->type->ops.mq.allow_merge) |
64 | return e->type->ops.mq.allow_merge(q, rq, bio); | |
65 | else if (!e->uses_mq && e->type->ops.sq.elevator_allow_bio_merge_fn) | |
c51ca6cf | 66 | return e->type->ops.sq.elevator_allow_bio_merge_fn(q, rq, bio); |
da775265 JA |
67 | |
68 | return 1; | |
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 | |
8ac0d9a8 JA |
86 | static 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 | ||
2527d997 JA |
96 | /* |
97 | * Return scheduler with name 'name' and with matching 'mq capability | |
98 | */ | |
99 | static struct elevator_type *elevator_find(const char *name, bool mq) | |
1da177e4 | 100 | { |
a22b169d | 101 | struct elevator_type *e; |
1da177e4 | 102 | |
70cee26e | 103 | list_for_each_entry(e, &elv_list, list) { |
8ac0d9a8 | 104 | if (elevator_match(e, name) && (mq == e->uses_mq)) |
a22b169d | 105 | return e; |
1da177e4 | 106 | } |
1da177e4 | 107 | |
a22b169d | 108 | return NULL; |
1da177e4 LT |
109 | } |
110 | ||
111 | static void elevator_put(struct elevator_type *e) | |
112 | { | |
113 | module_put(e->elevator_owner); | |
114 | } | |
115 | ||
2527d997 JA |
116 | static struct elevator_type *elevator_get(struct request_queue *q, |
117 | const char *name, bool try_loading) | |
1da177e4 | 118 | { |
2824bc93 | 119 | struct elevator_type *e; |
1da177e4 | 120 | |
2a12dcd7 | 121 | spin_lock(&elv_list_lock); |
2824bc93 | 122 | |
2527d997 | 123 | e = elevator_find(name, q->mq_ops != NULL); |
21c3c5d2 | 124 | if (!e && try_loading) { |
e1640949 | 125 | spin_unlock(&elv_list_lock); |
490b94be | 126 | request_module("%s-iosched", name); |
e1640949 | 127 | spin_lock(&elv_list_lock); |
2527d997 | 128 | e = elevator_find(name, q->mq_ops != NULL); |
e1640949 JA |
129 | } |
130 | ||
2824bc93 TH |
131 | if (e && !try_module_get(e->elevator_owner)) |
132 | e = NULL; | |
133 | ||
2a12dcd7 | 134 | spin_unlock(&elv_list_lock); |
1da177e4 LT |
135 | return e; |
136 | } | |
137 | ||
484fc254 | 138 | static char chosen_elevator[ELV_NAME_MAX]; |
1da177e4 | 139 | |
5f003976 | 140 | static int __init elevator_setup(char *str) |
1da177e4 | 141 | { |
752a3b79 CE |
142 | /* |
143 | * Be backwards-compatible with previous kernels, so users | |
144 | * won't get the wrong elevator. | |
145 | */ | |
492af635 | 146 | strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1); |
9b41046c | 147 | return 1; |
1da177e4 LT |
148 | } |
149 | ||
150 | __setup("elevator=", elevator_setup); | |
151 | ||
bb813f4c TH |
152 | /* called during boot to load the elevator chosen by the elevator param */ |
153 | void __init load_default_elevator_module(void) | |
154 | { | |
155 | struct elevator_type *e; | |
156 | ||
157 | if (!chosen_elevator[0]) | |
158 | return; | |
159 | ||
2527d997 JA |
160 | /* |
161 | * Boot parameter is deprecated, we haven't supported that for MQ. | |
162 | * Only look for non-mq schedulers from here. | |
163 | */ | |
bb813f4c | 164 | spin_lock(&elv_list_lock); |
2527d997 | 165 | e = elevator_find(chosen_elevator, false); |
bb813f4c TH |
166 | spin_unlock(&elv_list_lock); |
167 | ||
168 | if (!e) | |
169 | request_module("%s-iosched", chosen_elevator); | |
170 | } | |
171 | ||
3d1ab40f AV |
172 | static struct kobj_type elv_ktype; |
173 | ||
d50235b7 | 174 | struct elevator_queue *elevator_alloc(struct request_queue *q, |
165125e1 | 175 | struct elevator_type *e) |
3d1ab40f | 176 | { |
b374d18a | 177 | struct elevator_queue *eq; |
9817064b | 178 | |
c1b511eb | 179 | eq = kzalloc_node(sizeof(*eq), GFP_KERNEL, q->node); |
9817064b | 180 | if (unlikely(!eq)) |
8406a4d5 | 181 | return NULL; |
9817064b | 182 | |
22f746e2 | 183 | eq->type = e; |
f9cb074b | 184 | kobject_init(&eq->kobj, &elv_ktype); |
9817064b | 185 | mutex_init(&eq->sysfs_lock); |
242d98f0 | 186 | hash_init(eq->hash); |
bd166ef1 | 187 | eq->uses_mq = e->uses_mq; |
9817064b | 188 | |
3d1ab40f AV |
189 | return eq; |
190 | } | |
d50235b7 | 191 | EXPORT_SYMBOL(elevator_alloc); |
3d1ab40f AV |
192 | |
193 | static void elevator_release(struct kobject *kobj) | |
194 | { | |
b374d18a | 195 | struct elevator_queue *e; |
9817064b | 196 | |
b374d18a | 197 | e = container_of(kobj, struct elevator_queue, kobj); |
22f746e2 | 198 | elevator_put(e->type); |
3d1ab40f AV |
199 | kfree(e); |
200 | } | |
201 | ||
165125e1 | 202 | int elevator_init(struct request_queue *q, char *name) |
1da177e4 LT |
203 | { |
204 | struct elevator_type *e = NULL; | |
f8fc877d | 205 | int err; |
1da177e4 | 206 | |
eb1c160b TS |
207 | /* |
208 | * q->sysfs_lock must be held to provide mutual exclusion between | |
209 | * elevator_switch() and here. | |
210 | */ | |
211 | lockdep_assert_held(&q->sysfs_lock); | |
212 | ||
1abec4fd MS |
213 | if (unlikely(q->elevator)) |
214 | return 0; | |
215 | ||
4eb166d9 | 216 | if (name) { |
2527d997 | 217 | e = elevator_get(q, name, true); |
4eb166d9 JA |
218 | if (!e) |
219 | return -EINVAL; | |
220 | } | |
1da177e4 | 221 | |
21c3c5d2 | 222 | /* |
d1a987f3 JA |
223 | * Use the default elevator specified by config boot param for |
224 | * non-mq devices, or by config option. Don't try to load modules | |
225 | * as we could be running off async and request_module() isn't | |
226 | * allowed from async. | |
21c3c5d2 | 227 | */ |
d1a987f3 | 228 | if (!e && !q->mq_ops && *chosen_elevator) { |
2527d997 | 229 | e = elevator_get(q, chosen_elevator, false); |
4eb166d9 JA |
230 | if (!e) |
231 | printk(KERN_ERR "I/O scheduler %s not found\n", | |
232 | chosen_elevator); | |
233 | } | |
248d5ca5 | 234 | |
4eb166d9 | 235 | if (!e) { |
b86dd815 JA |
236 | /* |
237 | * For blk-mq devices, we default to using mq-deadline, | |
238 | * if available, for single queue devices. If deadline | |
239 | * isn't available OR we have multiple queues, default | |
240 | * to "none". | |
241 | */ | |
242 | if (q->mq_ops) { | |
243 | if (q->nr_hw_queues == 1) | |
2527d997 | 244 | e = elevator_get(q, "mq-deadline", false); |
b86dd815 JA |
245 | if (!e) |
246 | return 0; | |
247 | } else | |
2527d997 | 248 | e = elevator_get(q, CONFIG_DEFAULT_IOSCHED, false); |
d3484991 | 249 | |
4eb166d9 JA |
250 | if (!e) { |
251 | printk(KERN_ERR | |
252 | "Default I/O scheduler not found. " \ | |
b86dd815 | 253 | "Using noop.\n"); |
2527d997 | 254 | e = elevator_get(q, "noop", false); |
4eb166d9 | 255 | } |
5f003976 ND |
256 | } |
257 | ||
6917ff0b OS |
258 | if (e->uses_mq) |
259 | err = blk_mq_init_sched(q, e); | |
260 | else | |
bd166ef1 | 261 | err = e->ops.sq.elevator_init_fn(q, e); |
6917ff0b | 262 | if (err) |
d32f6b57 SM |
263 | elevator_put(e); |
264 | return err; | |
1da177e4 | 265 | } |
2e662b65 | 266 | |
54d5329d | 267 | void elevator_exit(struct request_queue *q, struct elevator_queue *e) |
1da177e4 | 268 | { |
3d1ab40f | 269 | mutex_lock(&e->sysfs_lock); |
bd166ef1 | 270 | if (e->uses_mq && e->type->ops.mq.exit_sched) |
54d5329d | 271 | blk_mq_exit_sched(q, e); |
bd166ef1 | 272 | else if (!e->uses_mq && e->type->ops.sq.elevator_exit_fn) |
c51ca6cf | 273 | e->type->ops.sq.elevator_exit_fn(e); |
3d1ab40f | 274 | mutex_unlock(&e->sysfs_lock); |
1da177e4 | 275 | |
3d1ab40f | 276 | kobject_put(&e->kobj); |
1da177e4 | 277 | } |
2e662b65 | 278 | |
9817064b JA |
279 | static inline void __elv_rqhash_del(struct request *rq) |
280 | { | |
242d98f0 | 281 | hash_del(&rq->hash); |
e8064021 | 282 | rq->rq_flags &= ~RQF_HASHED; |
9817064b JA |
283 | } |
284 | ||
70b3ea05 | 285 | void elv_rqhash_del(struct request_queue *q, struct request *rq) |
9817064b JA |
286 | { |
287 | if (ELV_ON_HASH(rq)) | |
288 | __elv_rqhash_del(rq); | |
289 | } | |
bd166ef1 | 290 | EXPORT_SYMBOL_GPL(elv_rqhash_del); |
9817064b | 291 | |
70b3ea05 | 292 | void elv_rqhash_add(struct request_queue *q, struct request *rq) |
9817064b | 293 | { |
b374d18a | 294 | struct elevator_queue *e = q->elevator; |
9817064b JA |
295 | |
296 | BUG_ON(ELV_ON_HASH(rq)); | |
242d98f0 | 297 | hash_add(e->hash, &rq->hash, rq_hash_key(rq)); |
e8064021 | 298 | rq->rq_flags |= RQF_HASHED; |
9817064b | 299 | } |
bd166ef1 | 300 | EXPORT_SYMBOL_GPL(elv_rqhash_add); |
9817064b | 301 | |
70b3ea05 | 302 | void elv_rqhash_reposition(struct request_queue *q, struct request *rq) |
9817064b JA |
303 | { |
304 | __elv_rqhash_del(rq); | |
305 | elv_rqhash_add(q, rq); | |
306 | } | |
307 | ||
70b3ea05 | 308 | struct request *elv_rqhash_find(struct request_queue *q, sector_t offset) |
9817064b | 309 | { |
b374d18a | 310 | struct elevator_queue *e = q->elevator; |
b67bfe0d | 311 | struct hlist_node *next; |
9817064b JA |
312 | struct request *rq; |
313 | ||
ee89f812 | 314 | hash_for_each_possible_safe(e->hash, rq, next, hash, offset) { |
9817064b JA |
315 | BUG_ON(!ELV_ON_HASH(rq)); |
316 | ||
317 | if (unlikely(!rq_mergeable(rq))) { | |
318 | __elv_rqhash_del(rq); | |
319 | continue; | |
320 | } | |
321 | ||
322 | if (rq_hash_key(rq) == offset) | |
323 | return rq; | |
324 | } | |
325 | ||
326 | return NULL; | |
327 | } | |
328 | ||
2e662b65 JA |
329 | /* |
330 | * RB-tree support functions for inserting/lookup/removal of requests | |
331 | * in a sorted RB tree. | |
332 | */ | |
796d5116 | 333 | void elv_rb_add(struct rb_root *root, struct request *rq) |
2e662b65 JA |
334 | { |
335 | struct rb_node **p = &root->rb_node; | |
336 | struct rb_node *parent = NULL; | |
337 | struct request *__rq; | |
338 | ||
339 | while (*p) { | |
340 | parent = *p; | |
341 | __rq = rb_entry(parent, struct request, rb_node); | |
342 | ||
83096ebf | 343 | if (blk_rq_pos(rq) < blk_rq_pos(__rq)) |
2e662b65 | 344 | p = &(*p)->rb_left; |
796d5116 | 345 | else if (blk_rq_pos(rq) >= blk_rq_pos(__rq)) |
2e662b65 | 346 | p = &(*p)->rb_right; |
2e662b65 JA |
347 | } |
348 | ||
349 | rb_link_node(&rq->rb_node, parent, p); | |
350 | rb_insert_color(&rq->rb_node, root); | |
2e662b65 | 351 | } |
2e662b65 JA |
352 | EXPORT_SYMBOL(elv_rb_add); |
353 | ||
354 | void elv_rb_del(struct rb_root *root, struct request *rq) | |
355 | { | |
356 | BUG_ON(RB_EMPTY_NODE(&rq->rb_node)); | |
357 | rb_erase(&rq->rb_node, root); | |
358 | RB_CLEAR_NODE(&rq->rb_node); | |
359 | } | |
2e662b65 JA |
360 | EXPORT_SYMBOL(elv_rb_del); |
361 | ||
362 | struct request *elv_rb_find(struct rb_root *root, sector_t sector) | |
363 | { | |
364 | struct rb_node *n = root->rb_node; | |
365 | struct request *rq; | |
366 | ||
367 | while (n) { | |
368 | rq = rb_entry(n, struct request, rb_node); | |
369 | ||
83096ebf | 370 | if (sector < blk_rq_pos(rq)) |
2e662b65 | 371 | n = n->rb_left; |
83096ebf | 372 | else if (sector > blk_rq_pos(rq)) |
2e662b65 JA |
373 | n = n->rb_right; |
374 | else | |
375 | return rq; | |
376 | } | |
377 | ||
378 | return NULL; | |
379 | } | |
2e662b65 JA |
380 | EXPORT_SYMBOL(elv_rb_find); |
381 | ||
8922e16c TH |
382 | /* |
383 | * Insert rq into dispatch queue of q. Queue lock must be held on | |
dbe7f76d | 384 | * entry. rq is sort instead into the dispatch queue. To be used by |
2e662b65 | 385 | * specific elevators. |
8922e16c | 386 | */ |
165125e1 | 387 | void elv_dispatch_sort(struct request_queue *q, struct request *rq) |
8922e16c TH |
388 | { |
389 | sector_t boundary; | |
8922e16c TH |
390 | struct list_head *entry; |
391 | ||
06b86245 TH |
392 | if (q->last_merge == rq) |
393 | q->last_merge = NULL; | |
9817064b JA |
394 | |
395 | elv_rqhash_del(q, rq); | |
396 | ||
15853af9 | 397 | q->nr_sorted--; |
06b86245 | 398 | |
1b47f531 | 399 | boundary = q->end_sector; |
8922e16c TH |
400 | list_for_each_prev(entry, &q->queue_head) { |
401 | struct request *pos = list_entry_rq(entry); | |
402 | ||
7afafc8a | 403 | if (req_op(rq) != req_op(pos)) |
e17fc0a1 | 404 | break; |
783660b2 JA |
405 | if (rq_data_dir(rq) != rq_data_dir(pos)) |
406 | break; | |
e8064021 | 407 | if (pos->rq_flags & (RQF_STARTED | RQF_SOFTBARRIER)) |
8922e16c | 408 | break; |
83096ebf TH |
409 | if (blk_rq_pos(rq) >= boundary) { |
410 | if (blk_rq_pos(pos) < boundary) | |
8922e16c TH |
411 | continue; |
412 | } else { | |
83096ebf | 413 | if (blk_rq_pos(pos) >= boundary) |
8922e16c TH |
414 | break; |
415 | } | |
83096ebf | 416 | if (blk_rq_pos(rq) >= blk_rq_pos(pos)) |
8922e16c TH |
417 | break; |
418 | } | |
419 | ||
420 | list_add(&rq->queuelist, entry); | |
421 | } | |
2e662b65 JA |
422 | EXPORT_SYMBOL(elv_dispatch_sort); |
423 | ||
9817064b | 424 | /* |
2e662b65 JA |
425 | * Insert rq into dispatch queue of q. Queue lock must be held on |
426 | * entry. rq is added to the back of the dispatch queue. To be used by | |
427 | * specific elevators. | |
9817064b JA |
428 | */ |
429 | void elv_dispatch_add_tail(struct request_queue *q, struct request *rq) | |
430 | { | |
431 | if (q->last_merge == rq) | |
432 | q->last_merge = NULL; | |
433 | ||
434 | elv_rqhash_del(q, rq); | |
435 | ||
436 | q->nr_sorted--; | |
437 | ||
438 | q->end_sector = rq_end_sector(rq); | |
439 | q->boundary_rq = rq; | |
440 | list_add_tail(&rq->queuelist, &q->queue_head); | |
441 | } | |
2e662b65 JA |
442 | EXPORT_SYMBOL(elv_dispatch_add_tail); |
443 | ||
34fe7c05 CH |
444 | enum elv_merge elv_merge(struct request_queue *q, struct request **req, |
445 | struct bio *bio) | |
1da177e4 | 446 | { |
b374d18a | 447 | struct elevator_queue *e = q->elevator; |
9817064b | 448 | struct request *__rq; |
06b86245 | 449 | |
488991e2 AB |
450 | /* |
451 | * Levels of merges: | |
452 | * nomerges: No merges at all attempted | |
453 | * noxmerges: Only simple one-hit cache try | |
454 | * merges: All merge tries attempted | |
455 | */ | |
7460d389 | 456 | if (blk_queue_nomerges(q) || !bio_mergeable(bio)) |
488991e2 AB |
457 | return ELEVATOR_NO_MERGE; |
458 | ||
9817064b JA |
459 | /* |
460 | * First try one-hit cache. | |
461 | */ | |
72ef799b | 462 | if (q->last_merge && elv_bio_merge_ok(q->last_merge, bio)) { |
34fe7c05 CH |
463 | enum elv_merge ret = blk_try_merge(q->last_merge, bio); |
464 | ||
06b86245 TH |
465 | if (ret != ELEVATOR_NO_MERGE) { |
466 | *req = q->last_merge; | |
467 | return ret; | |
468 | } | |
469 | } | |
1da177e4 | 470 | |
488991e2 | 471 | if (blk_queue_noxmerges(q)) |
ac9fafa1 AB |
472 | return ELEVATOR_NO_MERGE; |
473 | ||
9817064b JA |
474 | /* |
475 | * See if our hash lookup can find a potential backmerge. | |
476 | */ | |
4f024f37 | 477 | __rq = elv_rqhash_find(q, bio->bi_iter.bi_sector); |
72ef799b | 478 | if (__rq && elv_bio_merge_ok(__rq, bio)) { |
9817064b JA |
479 | *req = __rq; |
480 | return ELEVATOR_BACK_MERGE; | |
481 | } | |
482 | ||
bd166ef1 JA |
483 | if (e->uses_mq && e->type->ops.mq.request_merge) |
484 | return e->type->ops.mq.request_merge(q, req, bio); | |
485 | else if (!e->uses_mq && e->type->ops.sq.elevator_merge_fn) | |
c51ca6cf | 486 | return e->type->ops.sq.elevator_merge_fn(q, req, bio); |
1da177e4 LT |
487 | |
488 | return ELEVATOR_NO_MERGE; | |
489 | } | |
490 | ||
5e84ea3a JA |
491 | /* |
492 | * Attempt to do an insertion back merge. Only check for the case where | |
493 | * we can append 'rq' to an existing request, so we can throw 'rq' away | |
494 | * afterwards. | |
495 | * | |
496 | * Returns true if we merged, false otherwise | |
497 | */ | |
bd166ef1 | 498 | bool elv_attempt_insert_merge(struct request_queue *q, struct request *rq) |
5e84ea3a JA |
499 | { |
500 | struct request *__rq; | |
bee0393c | 501 | bool ret; |
5e84ea3a JA |
502 | |
503 | if (blk_queue_nomerges(q)) | |
504 | return false; | |
505 | ||
506 | /* | |
507 | * First try one-hit cache. | |
508 | */ | |
509 | if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq)) | |
510 | return true; | |
511 | ||
512 | if (blk_queue_noxmerges(q)) | |
513 | return false; | |
514 | ||
bee0393c | 515 | ret = false; |
5e84ea3a JA |
516 | /* |
517 | * See if our hash lookup can find a potential backmerge. | |
518 | */ | |
bee0393c SL |
519 | while (1) { |
520 | __rq = elv_rqhash_find(q, blk_rq_pos(rq)); | |
521 | if (!__rq || !blk_attempt_req_merge(q, __rq, rq)) | |
522 | break; | |
523 | ||
524 | /* The merged request could be merged with others, try again */ | |
525 | ret = true; | |
526 | rq = __rq; | |
527 | } | |
27419322 | 528 | |
bee0393c | 529 | return ret; |
5e84ea3a JA |
530 | } |
531 | ||
34fe7c05 CH |
532 | void elv_merged_request(struct request_queue *q, struct request *rq, |
533 | enum elv_merge type) | |
1da177e4 | 534 | { |
b374d18a | 535 | struct elevator_queue *e = q->elevator; |
1da177e4 | 536 | |
bd166ef1 JA |
537 | if (e->uses_mq && e->type->ops.mq.request_merged) |
538 | e->type->ops.mq.request_merged(q, rq, type); | |
539 | else if (!e->uses_mq && e->type->ops.sq.elevator_merged_fn) | |
c51ca6cf | 540 | e->type->ops.sq.elevator_merged_fn(q, rq, type); |
06b86245 | 541 | |
2e662b65 JA |
542 | if (type == ELEVATOR_BACK_MERGE) |
543 | elv_rqhash_reposition(q, rq); | |
9817064b | 544 | |
06b86245 | 545 | q->last_merge = rq; |
1da177e4 LT |
546 | } |
547 | ||
165125e1 | 548 | void elv_merge_requests(struct request_queue *q, struct request *rq, |
1da177e4 LT |
549 | struct request *next) |
550 | { | |
b374d18a | 551 | struct elevator_queue *e = q->elevator; |
bd166ef1 JA |
552 | bool next_sorted = false; |
553 | ||
554 | if (e->uses_mq && e->type->ops.mq.requests_merged) | |
555 | e->type->ops.mq.requests_merged(q, rq, next); | |
556 | else if (e->type->ops.sq.elevator_merge_req_fn) { | |
a1ae0f74 | 557 | next_sorted = (__force bool)(next->rq_flags & RQF_SORTED); |
bd166ef1 JA |
558 | if (next_sorted) |
559 | e->type->ops.sq.elevator_merge_req_fn(q, rq, next); | |
560 | } | |
06b86245 | 561 | |
9817064b | 562 | elv_rqhash_reposition(q, rq); |
9817064b | 563 | |
5e84ea3a JA |
564 | if (next_sorted) { |
565 | elv_rqhash_del(q, next); | |
566 | q->nr_sorted--; | |
567 | } | |
568 | ||
06b86245 | 569 | q->last_merge = rq; |
1da177e4 LT |
570 | } |
571 | ||
812d4026 DS |
572 | void elv_bio_merged(struct request_queue *q, struct request *rq, |
573 | struct bio *bio) | |
574 | { | |
575 | struct elevator_queue *e = q->elevator; | |
576 | ||
bd166ef1 JA |
577 | if (WARN_ON_ONCE(e->uses_mq)) |
578 | return; | |
579 | ||
c51ca6cf JA |
580 | if (e->type->ops.sq.elevator_bio_merged_fn) |
581 | e->type->ops.sq.elevator_bio_merged_fn(q, rq, bio); | |
812d4026 DS |
582 | } |
583 | ||
47fafbc7 | 584 | #ifdef CONFIG_PM |
c8158819 LM |
585 | static void blk_pm_requeue_request(struct request *rq) |
586 | { | |
e8064021 | 587 | if (rq->q->dev && !(rq->rq_flags & RQF_PM)) |
c8158819 LM |
588 | rq->q->nr_pending--; |
589 | } | |
590 | ||
591 | static void blk_pm_add_request(struct request_queue *q, struct request *rq) | |
592 | { | |
e8064021 | 593 | if (q->dev && !(rq->rq_flags & RQF_PM) && q->nr_pending++ == 0 && |
c8158819 LM |
594 | (q->rpm_status == RPM_SUSPENDED || q->rpm_status == RPM_SUSPENDING)) |
595 | pm_request_resume(q->dev); | |
596 | } | |
597 | #else | |
598 | static inline void blk_pm_requeue_request(struct request *rq) {} | |
599 | static inline void blk_pm_add_request(struct request_queue *q, | |
600 | struct request *rq) | |
601 | { | |
602 | } | |
603 | #endif | |
604 | ||
165125e1 | 605 | void elv_requeue_request(struct request_queue *q, struct request *rq) |
1da177e4 | 606 | { |
1da177e4 LT |
607 | /* |
608 | * it already went through dequeue, we need to decrement the | |
609 | * in_flight count again | |
610 | */ | |
8922e16c | 611 | if (blk_account_rq(rq)) { |
0a7ae2ff | 612 | q->in_flight[rq_is_sync(rq)]--; |
e8064021 | 613 | if (rq->rq_flags & RQF_SORTED) |
cad97516 | 614 | elv_deactivate_rq(q, rq); |
8922e16c | 615 | } |
1da177e4 | 616 | |
e8064021 | 617 | rq->rq_flags &= ~RQF_STARTED; |
1da177e4 | 618 | |
c8158819 LM |
619 | blk_pm_requeue_request(rq); |
620 | ||
b710a480 | 621 | __elv_add_request(q, rq, ELEVATOR_INSERT_REQUEUE); |
1da177e4 LT |
622 | } |
623 | ||
26308eab | 624 | void elv_drain_elevator(struct request_queue *q) |
15853af9 | 625 | { |
bd166ef1 | 626 | struct elevator_queue *e = q->elevator; |
15853af9 | 627 | static int printed; |
e3c78ca5 | 628 | |
bd166ef1 JA |
629 | if (WARN_ON_ONCE(e->uses_mq)) |
630 | return; | |
631 | ||
e3c78ca5 TH |
632 | lockdep_assert_held(q->queue_lock); |
633 | ||
bd166ef1 | 634 | while (e->type->ops.sq.elevator_dispatch_fn(q, 1)) |
15853af9 | 635 | ; |
e3c78ca5 | 636 | if (q->nr_sorted && printed++ < 10) { |
15853af9 TH |
637 | printk(KERN_ERR "%s: forced dispatching is broken " |
638 | "(nr_sorted=%u), please report this\n", | |
22f746e2 | 639 | q->elevator->type->elevator_name, q->nr_sorted); |
15853af9 TH |
640 | } |
641 | } | |
642 | ||
b710a480 | 643 | void __elv_add_request(struct request_queue *q, struct request *rq, int where) |
1da177e4 | 644 | { |
5f3ea37c | 645 | trace_block_rq_insert(q, rq); |
2056a782 | 646 | |
c8158819 LM |
647 | blk_pm_add_request(q, rq); |
648 | ||
1da177e4 LT |
649 | rq->q = q; |
650 | ||
e8064021 | 651 | if (rq->rq_flags & RQF_SOFTBARRIER) { |
b710a480 | 652 | /* barriers are scheduling boundary, update end_sector */ |
57292b58 | 653 | if (!blk_rq_is_passthrough(rq)) { |
b710a480 JA |
654 | q->end_sector = rq_end_sector(rq); |
655 | q->boundary_rq = rq; | |
656 | } | |
e8064021 | 657 | } else if (!(rq->rq_flags & RQF_ELVPRIV) && |
3aa72873 JA |
658 | (where == ELEVATOR_INSERT_SORT || |
659 | where == ELEVATOR_INSERT_SORT_MERGE)) | |
b710a480 JA |
660 | where = ELEVATOR_INSERT_BACK; |
661 | ||
8922e16c | 662 | switch (where) { |
28e7d184 | 663 | case ELEVATOR_INSERT_REQUEUE: |
8922e16c | 664 | case ELEVATOR_INSERT_FRONT: |
e8064021 | 665 | rq->rq_flags |= RQF_SOFTBARRIER; |
8922e16c TH |
666 | list_add(&rq->queuelist, &q->queue_head); |
667 | break; | |
668 | ||
669 | case ELEVATOR_INSERT_BACK: | |
e8064021 | 670 | rq->rq_flags |= RQF_SOFTBARRIER; |
15853af9 | 671 | elv_drain_elevator(q); |
8922e16c TH |
672 | list_add_tail(&rq->queuelist, &q->queue_head); |
673 | /* | |
674 | * We kick the queue here for the following reasons. | |
675 | * - The elevator might have returned NULL previously | |
676 | * to delay requests and returned them now. As the | |
677 | * queue wasn't empty before this request, ll_rw_blk | |
678 | * won't run the queue on return, resulting in hang. | |
679 | * - Usually, back inserted requests won't be merged | |
680 | * with anything. There's no point in delaying queue | |
681 | * processing. | |
682 | */ | |
24ecfbe2 | 683 | __blk_run_queue(q); |
8922e16c TH |
684 | break; |
685 | ||
5e84ea3a JA |
686 | case ELEVATOR_INSERT_SORT_MERGE: |
687 | /* | |
688 | * If we succeed in merging this request with one in the | |
689 | * queue already, we are done - rq has now been freed, | |
690 | * so no need to do anything further. | |
691 | */ | |
692 | if (elv_attempt_insert_merge(q, rq)) | |
693 | break; | |
e29387eb | 694 | /* fall through */ |
8922e16c | 695 | case ELEVATOR_INSERT_SORT: |
57292b58 | 696 | BUG_ON(blk_rq_is_passthrough(rq)); |
e8064021 | 697 | rq->rq_flags |= RQF_SORTED; |
15853af9 | 698 | q->nr_sorted++; |
9817064b JA |
699 | if (rq_mergeable(rq)) { |
700 | elv_rqhash_add(q, rq); | |
701 | if (!q->last_merge) | |
702 | q->last_merge = rq; | |
703 | } | |
704 | ||
ca23509f TH |
705 | /* |
706 | * Some ioscheds (cfq) run q->request_fn directly, so | |
707 | * rq cannot be accessed after calling | |
708 | * elevator_add_req_fn. | |
709 | */ | |
c51ca6cf | 710 | q->elevator->type->ops.sq.elevator_add_req_fn(q, rq); |
8922e16c TH |
711 | break; |
712 | ||
ae1b1539 | 713 | case ELEVATOR_INSERT_FLUSH: |
e8064021 | 714 | rq->rq_flags |= RQF_SOFTBARRIER; |
ae1b1539 TH |
715 | blk_insert_flush(rq); |
716 | break; | |
8922e16c TH |
717 | default: |
718 | printk(KERN_ERR "%s: bad insertion point %d\n", | |
24c03d47 | 719 | __func__, where); |
8922e16c TH |
720 | BUG(); |
721 | } | |
1da177e4 | 722 | } |
2e662b65 JA |
723 | EXPORT_SYMBOL(__elv_add_request); |
724 | ||
7eaceacc | 725 | void elv_add_request(struct request_queue *q, struct request *rq, int where) |
1da177e4 LT |
726 | { |
727 | unsigned long flags; | |
728 | ||
729 | spin_lock_irqsave(q->queue_lock, flags); | |
7eaceacc | 730 | __elv_add_request(q, rq, where); |
1da177e4 LT |
731 | spin_unlock_irqrestore(q->queue_lock, flags); |
732 | } | |
2e662b65 JA |
733 | EXPORT_SYMBOL(elv_add_request); |
734 | ||
165125e1 | 735 | struct request *elv_latter_request(struct request_queue *q, struct request *rq) |
1da177e4 | 736 | { |
b374d18a | 737 | struct elevator_queue *e = q->elevator; |
1da177e4 | 738 | |
bd166ef1 JA |
739 | if (e->uses_mq && e->type->ops.mq.next_request) |
740 | return e->type->ops.mq.next_request(q, rq); | |
741 | else if (!e->uses_mq && e->type->ops.sq.elevator_latter_req_fn) | |
c51ca6cf | 742 | return e->type->ops.sq.elevator_latter_req_fn(q, rq); |
bd166ef1 | 743 | |
1da177e4 LT |
744 | return NULL; |
745 | } | |
746 | ||
165125e1 | 747 | struct request *elv_former_request(struct request_queue *q, struct request *rq) |
1da177e4 | 748 | { |
b374d18a | 749 | struct elevator_queue *e = q->elevator; |
1da177e4 | 750 | |
bd166ef1 JA |
751 | if (e->uses_mq && e->type->ops.mq.former_request) |
752 | return e->type->ops.mq.former_request(q, rq); | |
753 | if (!e->uses_mq && e->type->ops.sq.elevator_former_req_fn) | |
c51ca6cf | 754 | return e->type->ops.sq.elevator_former_req_fn(q, rq); |
1da177e4 LT |
755 | return NULL; |
756 | } | |
757 | ||
852c788f TH |
758 | int elv_set_request(struct request_queue *q, struct request *rq, |
759 | struct bio *bio, gfp_t gfp_mask) | |
1da177e4 | 760 | { |
b374d18a | 761 | struct elevator_queue *e = q->elevator; |
1da177e4 | 762 | |
bd166ef1 JA |
763 | if (WARN_ON_ONCE(e->uses_mq)) |
764 | return 0; | |
765 | ||
c51ca6cf JA |
766 | if (e->type->ops.sq.elevator_set_req_fn) |
767 | return e->type->ops.sq.elevator_set_req_fn(q, rq, bio, gfp_mask); | |
1da177e4 LT |
768 | return 0; |
769 | } | |
770 | ||
165125e1 | 771 | void elv_put_request(struct request_queue *q, struct request *rq) |
1da177e4 | 772 | { |
b374d18a | 773 | struct elevator_queue *e = q->elevator; |
1da177e4 | 774 | |
bd166ef1 JA |
775 | if (WARN_ON_ONCE(e->uses_mq)) |
776 | return; | |
777 | ||
c51ca6cf JA |
778 | if (e->type->ops.sq.elevator_put_req_fn) |
779 | e->type->ops.sq.elevator_put_req_fn(rq); | |
1da177e4 LT |
780 | } |
781 | ||
ef295ecf | 782 | int elv_may_queue(struct request_queue *q, unsigned int op) |
1da177e4 | 783 | { |
b374d18a | 784 | struct elevator_queue *e = q->elevator; |
1da177e4 | 785 | |
bd166ef1 JA |
786 | if (WARN_ON_ONCE(e->uses_mq)) |
787 | return 0; | |
788 | ||
c51ca6cf JA |
789 | if (e->type->ops.sq.elevator_may_queue_fn) |
790 | return e->type->ops.sq.elevator_may_queue_fn(q, op); | |
1da177e4 LT |
791 | |
792 | return ELV_MQUEUE_MAY; | |
793 | } | |
794 | ||
165125e1 | 795 | void elv_completed_request(struct request_queue *q, struct request *rq) |
1da177e4 | 796 | { |
b374d18a | 797 | struct elevator_queue *e = q->elevator; |
1da177e4 | 798 | |
bd166ef1 JA |
799 | if (WARN_ON_ONCE(e->uses_mq)) |
800 | return; | |
801 | ||
1da177e4 LT |
802 | /* |
803 | * request is released from the driver, io must be done | |
804 | */ | |
8922e16c | 805 | if (blk_account_rq(rq)) { |
0a7ae2ff | 806 | q->in_flight[rq_is_sync(rq)]--; |
e8064021 | 807 | if ((rq->rq_flags & RQF_SORTED) && |
c51ca6cf JA |
808 | e->type->ops.sq.elevator_completed_req_fn) |
809 | e->type->ops.sq.elevator_completed_req_fn(q, rq); | |
1bc691d3 | 810 | } |
1da177e4 LT |
811 | } |
812 | ||
3d1ab40f AV |
813 | #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr) |
814 | ||
815 | static ssize_t | |
816 | elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page) | |
1da177e4 | 817 | { |
3d1ab40f | 818 | struct elv_fs_entry *entry = to_elv(attr); |
b374d18a | 819 | struct elevator_queue *e; |
3d1ab40f AV |
820 | ssize_t error; |
821 | ||
822 | if (!entry->show) | |
823 | return -EIO; | |
824 | ||
b374d18a | 825 | e = container_of(kobj, struct elevator_queue, kobj); |
3d1ab40f | 826 | mutex_lock(&e->sysfs_lock); |
22f746e2 | 827 | error = e->type ? entry->show(e, page) : -ENOENT; |
3d1ab40f AV |
828 | mutex_unlock(&e->sysfs_lock); |
829 | return error; | |
830 | } | |
1da177e4 | 831 | |
3d1ab40f AV |
832 | static ssize_t |
833 | elv_attr_store(struct kobject *kobj, struct attribute *attr, | |
834 | const char *page, size_t length) | |
835 | { | |
3d1ab40f | 836 | struct elv_fs_entry *entry = to_elv(attr); |
b374d18a | 837 | struct elevator_queue *e; |
3d1ab40f | 838 | ssize_t error; |
1da177e4 | 839 | |
3d1ab40f AV |
840 | if (!entry->store) |
841 | return -EIO; | |
1da177e4 | 842 | |
b374d18a | 843 | e = container_of(kobj, struct elevator_queue, kobj); |
3d1ab40f | 844 | mutex_lock(&e->sysfs_lock); |
22f746e2 | 845 | error = e->type ? entry->store(e, page, length) : -ENOENT; |
3d1ab40f AV |
846 | mutex_unlock(&e->sysfs_lock); |
847 | return error; | |
848 | } | |
849 | ||
52cf25d0 | 850 | static const struct sysfs_ops elv_sysfs_ops = { |
3d1ab40f AV |
851 | .show = elv_attr_show, |
852 | .store = elv_attr_store, | |
853 | }; | |
854 | ||
855 | static struct kobj_type elv_ktype = { | |
856 | .sysfs_ops = &elv_sysfs_ops, | |
857 | .release = elevator_release, | |
858 | }; | |
859 | ||
5a5bafdc | 860 | int elv_register_queue(struct request_queue *q) |
3d1ab40f | 861 | { |
5a5bafdc | 862 | struct elevator_queue *e = q->elevator; |
3d1ab40f AV |
863 | int error; |
864 | ||
14a23498 BVA |
865 | lockdep_assert_held(&q->sysfs_lock); |
866 | ||
b2d6db58 | 867 | error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched"); |
3d1ab40f | 868 | if (!error) { |
22f746e2 | 869 | struct elv_fs_entry *attr = e->type->elevator_attrs; |
3d1ab40f | 870 | if (attr) { |
e572ec7e AV |
871 | while (attr->attr.name) { |
872 | if (sysfs_create_file(&e->kobj, &attr->attr)) | |
3d1ab40f | 873 | break; |
e572ec7e | 874 | attr++; |
3d1ab40f AV |
875 | } |
876 | } | |
877 | kobject_uevent(&e->kobj, KOBJ_ADD); | |
430c62fb | 878 | e->registered = 1; |
bd166ef1 | 879 | if (!e->uses_mq && e->type->ops.sq.elevator_registered_fn) |
c51ca6cf | 880 | e->type->ops.sq.elevator_registered_fn(q); |
3d1ab40f AV |
881 | } |
882 | return error; | |
1da177e4 | 883 | } |
bc1c1169 | 884 | |
1da177e4 LT |
885 | void elv_unregister_queue(struct request_queue *q) |
886 | { | |
14a23498 BVA |
887 | lockdep_assert_held(&q->sysfs_lock); |
888 | ||
f8fc877d TH |
889 | if (q) { |
890 | struct elevator_queue *e = q->elevator; | |
891 | ||
892 | kobject_uevent(&e->kobj, KOBJ_REMOVE); | |
893 | kobject_del(&e->kobj); | |
894 | e->registered = 0; | |
8330cdb0 JK |
895 | /* Re-enable throttling in case elevator disabled it */ |
896 | wbt_enable_default(q); | |
f8fc877d | 897 | } |
1da177e4 LT |
898 | } |
899 | ||
e567bf71 | 900 | int elv_register(struct elevator_type *e) |
1da177e4 | 901 | { |
1ffb96c5 | 902 | char *def = ""; |
2a12dcd7 | 903 | |
3d3c2379 TH |
904 | /* create icq_cache if requested */ |
905 | if (e->icq_size) { | |
906 | if (WARN_ON(e->icq_size < sizeof(struct io_cq)) || | |
907 | WARN_ON(e->icq_align < __alignof__(struct io_cq))) | |
908 | return -EINVAL; | |
909 | ||
910 | snprintf(e->icq_cache_name, sizeof(e->icq_cache_name), | |
911 | "%s_io_cq", e->elevator_name); | |
912 | e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size, | |
913 | e->icq_align, 0, NULL); | |
914 | if (!e->icq_cache) | |
915 | return -ENOMEM; | |
916 | } | |
917 | ||
918 | /* register, don't allow duplicate names */ | |
2a12dcd7 | 919 | spin_lock(&elv_list_lock); |
2527d997 | 920 | if (elevator_find(e->elevator_name, e->uses_mq)) { |
3d3c2379 TH |
921 | spin_unlock(&elv_list_lock); |
922 | if (e->icq_cache) | |
923 | kmem_cache_destroy(e->icq_cache); | |
924 | return -EBUSY; | |
925 | } | |
1da177e4 | 926 | list_add_tail(&e->list, &elv_list); |
2a12dcd7 | 927 | spin_unlock(&elv_list_lock); |
1da177e4 | 928 | |
3d3c2379 | 929 | /* print pretty message */ |
8ac0d9a8 | 930 | if (elevator_match(e, chosen_elevator) || |
5f003976 | 931 | (!*chosen_elevator && |
8ac0d9a8 | 932 | elevator_match(e, CONFIG_DEFAULT_IOSCHED))) |
1ffb96c5 TV |
933 | def = " (default)"; |
934 | ||
4eb166d9 JA |
935 | printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name, |
936 | def); | |
3d3c2379 | 937 | return 0; |
1da177e4 LT |
938 | } |
939 | EXPORT_SYMBOL_GPL(elv_register); | |
940 | ||
941 | void elv_unregister(struct elevator_type *e) | |
942 | { | |
3d3c2379 | 943 | /* unregister */ |
2a12dcd7 | 944 | spin_lock(&elv_list_lock); |
1da177e4 | 945 | list_del_init(&e->list); |
2a12dcd7 | 946 | spin_unlock(&elv_list_lock); |
3d3c2379 TH |
947 | |
948 | /* | |
949 | * Destroy icq_cache if it exists. icq's are RCU managed. Make | |
950 | * sure all RCU operations are complete before proceeding. | |
951 | */ | |
952 | if (e->icq_cache) { | |
953 | rcu_barrier(); | |
954 | kmem_cache_destroy(e->icq_cache); | |
955 | e->icq_cache = NULL; | |
956 | } | |
1da177e4 LT |
957 | } |
958 | EXPORT_SYMBOL_GPL(elv_unregister); | |
959 | ||
54d5329d OS |
960 | static int elevator_switch_mq(struct request_queue *q, |
961 | struct elevator_type *new_e) | |
962 | { | |
963 | int ret; | |
964 | ||
14a23498 BVA |
965 | lockdep_assert_held(&q->sysfs_lock); |
966 | ||
54d5329d | 967 | blk_mq_freeze_queue(q); |
24f5a90f | 968 | blk_mq_quiesce_queue(q); |
54d5329d OS |
969 | |
970 | if (q->elevator) { | |
971 | if (q->elevator->registered) | |
972 | elv_unregister_queue(q); | |
973 | ioc_clear_queue(q); | |
974 | elevator_exit(q, q->elevator); | |
975 | } | |
976 | ||
977 | ret = blk_mq_init_sched(q, new_e); | |
978 | if (ret) | |
979 | goto out; | |
980 | ||
981 | if (new_e) { | |
982 | ret = elv_register_queue(q); | |
983 | if (ret) { | |
984 | elevator_exit(q, q->elevator); | |
985 | goto out; | |
986 | } | |
987 | } | |
988 | ||
989 | if (new_e) | |
990 | blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name); | |
991 | else | |
992 | blk_add_trace_msg(q, "elv switch: none"); | |
993 | ||
994 | out: | |
24f5a90f | 995 | blk_mq_unquiesce_queue(q); |
54d5329d | 996 | blk_mq_unfreeze_queue(q); |
54d5329d | 997 | return ret; |
54d5329d OS |
998 | } |
999 | ||
1da177e4 LT |
1000 | /* |
1001 | * switch to new_e io scheduler. be careful not to introduce deadlocks - | |
1002 | * we don't free the old io scheduler, before we have allocated what we | |
1003 | * need for the new one. this way we have a chance of going back to the old | |
cb98fc8b | 1004 | * one, if the new one fails init for some reason. |
1da177e4 | 1005 | */ |
165125e1 | 1006 | static int elevator_switch(struct request_queue *q, struct elevator_type *new_e) |
1da177e4 | 1007 | { |
5a5bafdc | 1008 | struct elevator_queue *old = q->elevator; |
bd166ef1 | 1009 | bool old_registered = false; |
e8989fae | 1010 | int err; |
1da177e4 | 1011 | |
14a23498 BVA |
1012 | lockdep_assert_held(&q->sysfs_lock); |
1013 | ||
54d5329d OS |
1014 | if (q->mq_ops) |
1015 | return elevator_switch_mq(q, new_e); | |
bd166ef1 | 1016 | |
5a5bafdc TH |
1017 | /* |
1018 | * Turn on BYPASS and drain all requests w/ elevator private data. | |
1019 | * Block layer doesn't call into a quiesced elevator - all requests | |
1020 | * are directly put on the dispatch list without elevator data | |
1021 | * using INSERT_BACK. All requests have SOFTBARRIER set and no | |
1022 | * merge happens either. | |
1023 | */ | |
bd166ef1 JA |
1024 | if (old) { |
1025 | old_registered = old->registered; | |
1026 | ||
54d5329d | 1027 | blk_queue_bypass_start(q); |
1da177e4 | 1028 | |
bd166ef1 JA |
1029 | /* unregister and clear all auxiliary data of the old elevator */ |
1030 | if (old_registered) | |
1031 | elv_unregister_queue(q); | |
1032 | ||
bd166ef1 | 1033 | ioc_clear_queue(q); |
bd166ef1 | 1034 | } |
f8fc877d | 1035 | |
5a5bafdc | 1036 | /* allocate, init and register new elevator */ |
54d5329d | 1037 | err = new_e->ops.sq.elevator_init_fn(q, new_e); |
6917ff0b OS |
1038 | if (err) |
1039 | goto fail_init; | |
5a5bafdc | 1040 | |
54d5329d OS |
1041 | err = elv_register_queue(q); |
1042 | if (err) | |
1043 | goto fail_register; | |
5a5bafdc TH |
1044 | |
1045 | /* done, kill the old one and finish */ | |
bd166ef1 | 1046 | if (old) { |
54d5329d OS |
1047 | elevator_exit(q, old); |
1048 | blk_queue_bypass_end(q); | |
bd166ef1 JA |
1049 | } |
1050 | ||
54d5329d | 1051 | blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name); |
4722dc52 | 1052 | |
5dd531a0 | 1053 | return 0; |
1da177e4 LT |
1054 | |
1055 | fail_register: | |
54d5329d | 1056 | elevator_exit(q, q->elevator); |
5a5bafdc TH |
1057 | fail_init: |
1058 | /* switch failed, restore and re-register old elevator */ | |
bd166ef1 JA |
1059 | if (old) { |
1060 | q->elevator = old; | |
1061 | elv_register_queue(q); | |
54d5329d | 1062 | blk_queue_bypass_end(q); |
bd166ef1 | 1063 | } |
75ad23bc | 1064 | |
5dd531a0 | 1065 | return err; |
1da177e4 LT |
1066 | } |
1067 | ||
5dd531a0 JA |
1068 | /* |
1069 | * Switch this queue to the given IO scheduler. | |
1070 | */ | |
7c8a3679 | 1071 | static int __elevator_change(struct request_queue *q, const char *name) |
1da177e4 LT |
1072 | { |
1073 | char elevator_name[ELV_NAME_MAX]; | |
1074 | struct elevator_type *e; | |
1075 | ||
e9a823fb DJ |
1076 | /* Make sure queue is not in the middle of being removed */ |
1077 | if (!test_bit(QUEUE_FLAG_REGISTERED, &q->queue_flags)) | |
1078 | return -ENOENT; | |
1079 | ||
bd166ef1 JA |
1080 | /* |
1081 | * Special case for mq, turn off scheduling | |
1082 | */ | |
1083 | if (q->mq_ops && !strncmp(name, "none", 4)) | |
1084 | return elevator_switch(q, NULL); | |
cd43e26f | 1085 | |
ee2e992c | 1086 | strlcpy(elevator_name, name, sizeof(elevator_name)); |
2527d997 | 1087 | e = elevator_get(q, strstrip(elevator_name), true); |
340ff321 | 1088 | if (!e) |
1da177e4 | 1089 | return -EINVAL; |
1da177e4 | 1090 | |
8ac0d9a8 | 1091 | if (q->elevator && elevator_match(q->elevator->type, elevator_name)) { |
2ca7d93b | 1092 | elevator_put(e); |
5dd531a0 | 1093 | return 0; |
2ca7d93b | 1094 | } |
1da177e4 | 1095 | |
5dd531a0 JA |
1096 | return elevator_switch(q, e); |
1097 | } | |
7c8a3679 | 1098 | |
3a5088c8 ML |
1099 | static inline bool elv_support_iosched(struct request_queue *q) |
1100 | { | |
1101 | if (q->mq_ops && q->tag_set && (q->tag_set->flags & | |
1102 | BLK_MQ_F_NO_SCHED)) | |
1103 | return false; | |
1104 | return true; | |
1105 | } | |
1106 | ||
5dd531a0 JA |
1107 | ssize_t elv_iosched_store(struct request_queue *q, const char *name, |
1108 | size_t count) | |
1109 | { | |
1110 | int ret; | |
1111 | ||
3a5088c8 | 1112 | if (!(q->mq_ops || q->request_fn) || !elv_support_iosched(q)) |
5dd531a0 JA |
1113 | return count; |
1114 | ||
7c8a3679 | 1115 | ret = __elevator_change(q, name); |
5dd531a0 JA |
1116 | if (!ret) |
1117 | return count; | |
1118 | ||
5dd531a0 | 1119 | return ret; |
1da177e4 LT |
1120 | } |
1121 | ||
165125e1 | 1122 | ssize_t elv_iosched_show(struct request_queue *q, char *name) |
1da177e4 | 1123 | { |
b374d18a | 1124 | struct elevator_queue *e = q->elevator; |
bd166ef1 | 1125 | struct elevator_type *elv = NULL; |
70cee26e | 1126 | struct elevator_type *__e; |
8ac0d9a8 | 1127 | bool uses_mq = q->mq_ops != NULL; |
1da177e4 LT |
1128 | int len = 0; |
1129 | ||
5fdee212 | 1130 | if (!queue_is_rq_based(q)) |
cd43e26f MP |
1131 | return sprintf(name, "none\n"); |
1132 | ||
bd166ef1 JA |
1133 | if (!q->elevator) |
1134 | len += sprintf(name+len, "[none] "); | |
1135 | else | |
1136 | elv = e->type; | |
cd43e26f | 1137 | |
2a12dcd7 | 1138 | spin_lock(&elv_list_lock); |
70cee26e | 1139 | list_for_each_entry(__e, &elv_list, list) { |
8ac0d9a8 JA |
1140 | if (elv && elevator_match(elv, __e->elevator_name) && |
1141 | (__e->uses_mq == uses_mq)) { | |
1da177e4 | 1142 | len += sprintf(name+len, "[%s] ", elv->elevator_name); |
bd166ef1 JA |
1143 | continue; |
1144 | } | |
3a5088c8 | 1145 | if (__e->uses_mq && q->mq_ops && elv_support_iosched(q)) |
bd166ef1 JA |
1146 | len += sprintf(name+len, "%s ", __e->elevator_name); |
1147 | else if (!__e->uses_mq && !q->mq_ops) | |
1da177e4 LT |
1148 | len += sprintf(name+len, "%s ", __e->elevator_name); |
1149 | } | |
2a12dcd7 | 1150 | spin_unlock(&elv_list_lock); |
1da177e4 | 1151 | |
bd166ef1 JA |
1152 | if (q->mq_ops && q->elevator) |
1153 | len += sprintf(name+len, "none"); | |
1154 | ||
1da177e4 LT |
1155 | len += sprintf(len+name, "\n"); |
1156 | return len; | |
1157 | } | |
1158 | ||
165125e1 JA |
1159 | struct request *elv_rb_former_request(struct request_queue *q, |
1160 | struct request *rq) | |
2e662b65 JA |
1161 | { |
1162 | struct rb_node *rbprev = rb_prev(&rq->rb_node); | |
1163 | ||
1164 | if (rbprev) | |
1165 | return rb_entry_rq(rbprev); | |
1166 | ||
1167 | return NULL; | |
1168 | } | |
2e662b65 JA |
1169 | EXPORT_SYMBOL(elv_rb_former_request); |
1170 | ||
165125e1 JA |
1171 | struct request *elv_rb_latter_request(struct request_queue *q, |
1172 | struct request *rq) | |
2e662b65 JA |
1173 | { |
1174 | struct rb_node *rbnext = rb_next(&rq->rb_node); | |
1175 | ||
1176 | if (rbnext) | |
1177 | return rb_entry_rq(rbnext); | |
1178 | ||
1179 | return NULL; | |
1180 | } | |
2e662b65 | 1181 | EXPORT_SYMBOL(elv_rb_latter_request); |