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