Commit | Line | Data |
---|---|---|
3dcf60bc | 1 | // SPDX-License-Identifier: GPL-2.0 |
bd166ef1 JA |
2 | /* |
3 | * blk-mq scheduling framework | |
4 | * | |
5 | * Copyright (C) 2016 Jens Axboe | |
6 | */ | |
7 | #include <linux/kernel.h> | |
8 | #include <linux/module.h> | |
9 | #include <linux/blk-mq.h> | |
6e6fcbc2 | 10 | #include <linux/list_sort.h> |
bd166ef1 JA |
11 | |
12 | #include <trace/events/block.h> | |
13 | ||
14 | #include "blk.h" | |
15 | #include "blk-mq.h" | |
d332ce09 | 16 | #include "blk-mq-debugfs.h" |
bd166ef1 JA |
17 | #include "blk-mq-sched.h" |
18 | #include "blk-mq-tag.h" | |
19 | #include "blk-wbt.h" | |
20 | ||
8e8320c9 | 21 | /* |
c31e76bc | 22 | * Mark a hardware queue as needing a restart. |
8e8320c9 | 23 | */ |
7211aef8 | 24 | void blk_mq_sched_mark_restart_hctx(struct blk_mq_hw_ctx *hctx) |
8e8320c9 JA |
25 | { |
26 | if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state)) | |
27 | return; | |
28 | ||
97889f9a | 29 | set_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state); |
8e8320c9 | 30 | } |
7211aef8 | 31 | EXPORT_SYMBOL_GPL(blk_mq_sched_mark_restart_hctx); |
8e8320c9 | 32 | |
e9ea1596 | 33 | void __blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx) |
8e8320c9 | 34 | { |
97889f9a | 35 | clear_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state); |
8e8320c9 | 36 | |
d7d8535f ML |
37 | /* |
38 | * Order clearing SCHED_RESTART and list_empty_careful(&hctx->dispatch) | |
39 | * in blk_mq_run_hw_queue(). Its pair is the barrier in | |
40 | * blk_mq_dispatch_rq_list(). So dispatch code won't see SCHED_RESTART, | |
41 | * meantime new request added to hctx->dispatch is missed to check in | |
42 | * blk_mq_run_hw_queue(). | |
43 | */ | |
44 | smp_mb(); | |
45 | ||
97889f9a | 46 | blk_mq_run_hw_queue(hctx, true); |
8e8320c9 JA |
47 | } |
48 | ||
4f0f586b ST |
49 | static int sched_rq_cmp(void *priv, const struct list_head *a, |
50 | const struct list_head *b) | |
6e6fcbc2 ML |
51 | { |
52 | struct request *rqa = container_of(a, struct request, queuelist); | |
53 | struct request *rqb = container_of(b, struct request, queuelist); | |
54 | ||
55 | return rqa->mq_hctx > rqb->mq_hctx; | |
56 | } | |
57 | ||
58 | static bool blk_mq_dispatch_hctx_list(struct list_head *rq_list) | |
59 | { | |
60 | struct blk_mq_hw_ctx *hctx = | |
61 | list_first_entry(rq_list, struct request, queuelist)->mq_hctx; | |
62 | struct request *rq; | |
63 | LIST_HEAD(hctx_list); | |
64 | unsigned int count = 0; | |
6e6fcbc2 ML |
65 | |
66 | list_for_each_entry(rq, rq_list, queuelist) { | |
67 | if (rq->mq_hctx != hctx) { | |
68 | list_cut_before(&hctx_list, rq_list, &rq->queuelist); | |
69 | goto dispatch; | |
70 | } | |
71 | count++; | |
72 | } | |
73 | list_splice_tail_init(rq_list, &hctx_list); | |
74 | ||
75 | dispatch: | |
106e71c5 | 76 | return blk_mq_dispatch_rq_list(hctx, &hctx_list, count); |
6e6fcbc2 ML |
77 | } |
78 | ||
a0823421 DA |
79 | #define BLK_MQ_BUDGET_DELAY 3 /* ms units */ |
80 | ||
1f460b63 ML |
81 | /* |
82 | * Only SCSI implements .get_budget and .put_budget, and SCSI restarts | |
83 | * its queue by itself in its completion handler, so we don't need to | |
01542f65 | 84 | * restart queue if .get_budget() fails to get the budget. |
28d65729 SQ |
85 | * |
86 | * Returns -EAGAIN if hctx->dispatch was found non-empty and run_work has to | |
87 | * be run again. This is necessary to avoid starving flushes. | |
1f460b63 | 88 | */ |
6e6fcbc2 | 89 | static int __blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx) |
caf8eb0d ML |
90 | { |
91 | struct request_queue *q = hctx->queue; | |
92 | struct elevator_queue *e = q->elevator; | |
6e6fcbc2 ML |
93 | bool multi_hctxs = false, run_queue = false; |
94 | bool dispatched = false, busy = false; | |
95 | unsigned int max_dispatch; | |
caf8eb0d | 96 | LIST_HEAD(rq_list); |
6e6fcbc2 ML |
97 | int count = 0; |
98 | ||
99 | if (hctx->dispatch_busy) | |
100 | max_dispatch = 1; | |
101 | else | |
102 | max_dispatch = hctx->queue->nr_requests; | |
caf8eb0d ML |
103 | |
104 | do { | |
6e6fcbc2 | 105 | struct request *rq; |
2a5a24aa | 106 | int budget_token; |
6e6fcbc2 | 107 | |
f9cd4bfe | 108 | if (e->type->ops.has_work && !e->type->ops.has_work(hctx)) |
caf8eb0d | 109 | break; |
de148297 | 110 | |
28d65729 | 111 | if (!list_empty_careful(&hctx->dispatch)) { |
6e6fcbc2 | 112 | busy = true; |
28d65729 SQ |
113 | break; |
114 | } | |
115 | ||
2a5a24aa ML |
116 | budget_token = blk_mq_get_dispatch_budget(q); |
117 | if (budget_token < 0) | |
1f460b63 | 118 | break; |
de148297 | 119 | |
f9cd4bfe | 120 | rq = e->type->ops.dispatch_request(hctx); |
de148297 | 121 | if (!rq) { |
2a5a24aa | 122 | blk_mq_put_dispatch_budget(q, budget_token); |
a0823421 DA |
123 | /* |
124 | * We're releasing without dispatching. Holding the | |
125 | * budget could have blocked any "hctx"s with the | |
126 | * same queue and if we didn't dispatch then there's | |
127 | * no guarantee anyone will kick the queue. Kick it | |
128 | * ourselves. | |
129 | */ | |
6e6fcbc2 | 130 | run_queue = true; |
de148297 | 131 | break; |
de148297 ML |
132 | } |
133 | ||
2a5a24aa ML |
134 | blk_mq_set_rq_budget_token(rq, budget_token); |
135 | ||
de148297 ML |
136 | /* |
137 | * Now this rq owns the budget which has to be released | |
138 | * if this rq won't be queued to driver via .queue_rq() | |
139 | * in blk_mq_dispatch_rq_list(). | |
140 | */ | |
6e6fcbc2 | 141 | list_add_tail(&rq->queuelist, &rq_list); |
61347154 | 142 | count++; |
6e6fcbc2 ML |
143 | if (rq->mq_hctx != hctx) |
144 | multi_hctxs = true; | |
61347154 JK |
145 | |
146 | /* | |
147 | * If we cannot get tag for the request, stop dequeueing | |
148 | * requests from the IO scheduler. We are unlikely to be able | |
149 | * to submit them anyway and it creates false impression for | |
150 | * scheduling heuristics that the device can take more IO. | |
151 | */ | |
152 | if (!blk_mq_get_driver_tag(rq)) | |
153 | break; | |
154 | } while (count < max_dispatch); | |
6e6fcbc2 ML |
155 | |
156 | if (!count) { | |
157 | if (run_queue) | |
158 | blk_mq_delay_run_hw_queues(q, BLK_MQ_BUDGET_DELAY); | |
159 | } else if (multi_hctxs) { | |
160 | /* | |
161 | * Requests from different hctx may be dequeued from some | |
162 | * schedulers, such as bfq and deadline. | |
163 | * | |
164 | * Sort the requests in the list according to their hctx, | |
165 | * dispatch batching requests from same hctx at a time. | |
166 | */ | |
167 | list_sort(NULL, &rq_list, sched_rq_cmp); | |
168 | do { | |
169 | dispatched |= blk_mq_dispatch_hctx_list(&rq_list); | |
170 | } while (!list_empty(&rq_list)); | |
171 | } else { | |
172 | dispatched = blk_mq_dispatch_rq_list(hctx, &rq_list, count); | |
173 | } | |
174 | ||
175 | if (busy) | |
176 | return -EAGAIN; | |
177 | return !!dispatched; | |
178 | } | |
179 | ||
180 | static int blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx) | |
181 | { | |
572299f0 | 182 | unsigned long end = jiffies + HZ; |
6e6fcbc2 ML |
183 | int ret; |
184 | ||
185 | do { | |
186 | ret = __blk_mq_do_dispatch_sched(hctx); | |
572299f0 SK |
187 | if (ret != 1) |
188 | break; | |
189 | if (need_resched() || time_is_before_jiffies(end)) { | |
190 | blk_mq_delay_run_hw_queue(hctx, 0); | |
191 | break; | |
192 | } | |
193 | } while (1); | |
28d65729 SQ |
194 | |
195 | return ret; | |
caf8eb0d ML |
196 | } |
197 | ||
b347689f ML |
198 | static struct blk_mq_ctx *blk_mq_next_ctx(struct blk_mq_hw_ctx *hctx, |
199 | struct blk_mq_ctx *ctx) | |
200 | { | |
f31967f0 | 201 | unsigned short idx = ctx->index_hw[hctx->type]; |
b347689f ML |
202 | |
203 | if (++idx == hctx->nr_ctx) | |
204 | idx = 0; | |
205 | ||
206 | return hctx->ctxs[idx]; | |
207 | } | |
208 | ||
1f460b63 ML |
209 | /* |
210 | * Only SCSI implements .get_budget and .put_budget, and SCSI restarts | |
211 | * its queue by itself in its completion handler, so we don't need to | |
01542f65 | 212 | * restart queue if .get_budget() fails to get the budget. |
28d65729 SQ |
213 | * |
214 | * Returns -EAGAIN if hctx->dispatch was found non-empty and run_work has to | |
c4aecaa2 | 215 | * be run again. This is necessary to avoid starving flushes. |
1f460b63 | 216 | */ |
28d65729 | 217 | static int blk_mq_do_dispatch_ctx(struct blk_mq_hw_ctx *hctx) |
b347689f ML |
218 | { |
219 | struct request_queue *q = hctx->queue; | |
220 | LIST_HEAD(rq_list); | |
221 | struct blk_mq_ctx *ctx = READ_ONCE(hctx->dispatch_from); | |
28d65729 | 222 | int ret = 0; |
445874e8 | 223 | struct request *rq; |
b347689f ML |
224 | |
225 | do { | |
2a5a24aa ML |
226 | int budget_token; |
227 | ||
28d65729 SQ |
228 | if (!list_empty_careful(&hctx->dispatch)) { |
229 | ret = -EAGAIN; | |
230 | break; | |
231 | } | |
232 | ||
b347689f ML |
233 | if (!sbitmap_any_bit_set(&hctx->ctx_map)) |
234 | break; | |
235 | ||
2a5a24aa ML |
236 | budget_token = blk_mq_get_dispatch_budget(q); |
237 | if (budget_token < 0) | |
1f460b63 | 238 | break; |
b347689f ML |
239 | |
240 | rq = blk_mq_dequeue_from_ctx(hctx, ctx); | |
241 | if (!rq) { | |
2a5a24aa | 242 | blk_mq_put_dispatch_budget(q, budget_token); |
a0823421 DA |
243 | /* |
244 | * We're releasing without dispatching. Holding the | |
245 | * budget could have blocked any "hctx"s with the | |
246 | * same queue and if we didn't dispatch then there's | |
247 | * no guarantee anyone will kick the queue. Kick it | |
248 | * ourselves. | |
249 | */ | |
250 | blk_mq_delay_run_hw_queues(q, BLK_MQ_BUDGET_DELAY); | |
b347689f | 251 | break; |
b347689f ML |
252 | } |
253 | ||
2a5a24aa ML |
254 | blk_mq_set_rq_budget_token(rq, budget_token); |
255 | ||
b347689f ML |
256 | /* |
257 | * Now this rq owns the budget which has to be released | |
258 | * if this rq won't be queued to driver via .queue_rq() | |
259 | * in blk_mq_dispatch_rq_list(). | |
260 | */ | |
261 | list_add(&rq->queuelist, &rq_list); | |
262 | ||
263 | /* round robin for fair dispatch */ | |
264 | ctx = blk_mq_next_ctx(hctx, rq->mq_ctx); | |
265 | ||
1fd40b5e | 266 | } while (blk_mq_dispatch_rq_list(rq->mq_hctx, &rq_list, 1)); |
b347689f ML |
267 | |
268 | WRITE_ONCE(hctx->dispatch_from, ctx); | |
28d65729 | 269 | return ret; |
b347689f ML |
270 | } |
271 | ||
e1b586f2 | 272 | static int __blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx) |
bd166ef1 | 273 | { |
81380ca1 | 274 | struct request_queue *q = hctx->queue; |
e42cfb1d | 275 | const bool has_sched = q->elevator; |
28d65729 | 276 | int ret = 0; |
bd166ef1 JA |
277 | LIST_HEAD(rq_list); |
278 | ||
bd166ef1 JA |
279 | /* |
280 | * If we have previous entries on our dispatch list, grab them first for | |
281 | * more fair dispatch. | |
282 | */ | |
283 | if (!list_empty_careful(&hctx->dispatch)) { | |
284 | spin_lock(&hctx->lock); | |
285 | if (!list_empty(&hctx->dispatch)) | |
286 | list_splice_init(&hctx->dispatch, &rq_list); | |
287 | spin_unlock(&hctx->lock); | |
288 | } | |
289 | ||
290 | /* | |
291 | * Only ask the scheduler for requests, if we didn't have residual | |
292 | * requests from the dispatch list. This is to avoid the case where | |
293 | * we only ever dispatch a fraction of the requests available because | |
294 | * of low device queue depth. Once we pull requests out of the IO | |
295 | * scheduler, we can no longer merge or sort them. So it's best to | |
296 | * leave them there for as long as we can. Mark the hw queue as | |
297 | * needing a restart in that case. | |
caf8eb0d ML |
298 | * |
299 | * We want to dispatch from the scheduler if there was nothing | |
300 | * on the dispatch list or we were able to dispatch from the | |
301 | * dispatch list. | |
bd166ef1 | 302 | */ |
c13660a0 | 303 | if (!list_empty(&rq_list)) { |
d38d3515 | 304 | blk_mq_sched_mark_restart_hctx(hctx); |
1fd40b5e | 305 | if (blk_mq_dispatch_rq_list(hctx, &rq_list, 0)) { |
e42cfb1d | 306 | if (has_sched) |
28d65729 | 307 | ret = blk_mq_do_dispatch_sched(hctx); |
b347689f | 308 | else |
28d65729 | 309 | ret = blk_mq_do_dispatch_ctx(hctx); |
b347689f | 310 | } |
e42cfb1d | 311 | } else if (has_sched) { |
28d65729 | 312 | ret = blk_mq_do_dispatch_sched(hctx); |
6e768717 ML |
313 | } else if (hctx->dispatch_busy) { |
314 | /* dequeue request one by one from sw queue if queue is busy */ | |
28d65729 | 315 | ret = blk_mq_do_dispatch_ctx(hctx); |
caf8eb0d | 316 | } else { |
c13660a0 | 317 | blk_mq_flush_busy_ctxs(hctx, &rq_list); |
1fd40b5e | 318 | blk_mq_dispatch_rq_list(hctx, &rq_list, 0); |
64765a75 | 319 | } |
28d65729 SQ |
320 | |
321 | return ret; | |
322 | } | |
323 | ||
324 | void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx) | |
325 | { | |
326 | struct request_queue *q = hctx->queue; | |
327 | ||
328 | /* RCU or SRCU read lock is needed before checking quiesced flag */ | |
329 | if (unlikely(blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q))) | |
330 | return; | |
331 | ||
332 | hctx->run++; | |
333 | ||
334 | /* | |
335 | * A return of -EAGAIN is an indication that hctx->dispatch is not | |
336 | * empty and we must run again in order to avoid starving flushes. | |
337 | */ | |
338 | if (__blk_mq_sched_dispatch_requests(hctx) == -EAGAIN) { | |
339 | if (__blk_mq_sched_dispatch_requests(hctx) == -EAGAIN) | |
340 | blk_mq_run_hw_queue(hctx, true); | |
341 | } | |
bd166ef1 JA |
342 | } |
343 | ||
179ae84f | 344 | bool blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio, |
14ccb66b | 345 | unsigned int nr_segs) |
bd166ef1 JA |
346 | { |
347 | struct elevator_queue *e = q->elevator; | |
efed9a33 OS |
348 | struct blk_mq_ctx *ctx; |
349 | struct blk_mq_hw_ctx *hctx; | |
9bddeb2a | 350 | bool ret = false; |
c16d6b5a | 351 | enum hctx_type type; |
bd166ef1 | 352 | |
900e0807 JA |
353 | if (e && e->type->ops.bio_merge) { |
354 | ret = e->type->ops.bio_merge(q, bio, nr_segs); | |
355 | goto out_put; | |
356 | } | |
bd166ef1 | 357 | |
efed9a33 OS |
358 | ctx = blk_mq_get_ctx(q); |
359 | hctx = blk_mq_map_queue(q, bio->bi_opf, ctx); | |
c16d6b5a | 360 | type = hctx->type; |
cdfcef9e BW |
361 | if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE) || |
362 | list_empty_careful(&ctx->rq_lists[type])) | |
900e0807 | 363 | goto out_put; |
cdfcef9e BW |
364 | |
365 | /* default per sw-queue merge */ | |
366 | spin_lock(&ctx->lock); | |
367 | /* | |
368 | * Reverse check our software queue for entries that we could | |
369 | * potentially merge with. Currently includes a hand-wavy stop | |
370 | * count of 8, to not spend too much time checking for merges. | |
371 | */ | |
9a14d6ce | 372 | if (blk_bio_list_merge(q, &ctx->rq_lists[type], bio, nr_segs)) |
cdfcef9e | 373 | ret = true; |
9bddeb2a | 374 | |
cdfcef9e | 375 | spin_unlock(&ctx->lock); |
900e0807 | 376 | out_put: |
9bddeb2a | 377 | return ret; |
bd166ef1 JA |
378 | } |
379 | ||
fd2ef39c JK |
380 | bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq, |
381 | struct list_head *free) | |
bd166ef1 | 382 | { |
fd2ef39c | 383 | return rq_mergeable(rq) && elv_attempt_insert_merge(q, rq, free); |
bd166ef1 JA |
384 | } |
385 | EXPORT_SYMBOL_GPL(blk_mq_sched_try_insert_merge); | |
386 | ||
0cacba6c OS |
387 | static bool blk_mq_sched_bypass_insert(struct blk_mq_hw_ctx *hctx, |
388 | struct request *rq) | |
bd166ef1 | 389 | { |
01e99aec ML |
390 | /* |
391 | * dispatch flush and passthrough rq directly | |
392 | * | |
393 | * passthrough request has to be added to hctx->dispatch directly. | |
394 | * For some reason, device may be in one situation which can't | |
395 | * handle FS request, so STS_RESOURCE is always returned and the | |
396 | * FS request will be added to hctx->dispatch. However passthrough | |
397 | * request may be required at that time for fixing the problem. If | |
398 | * passthrough request is added to scheduler queue, there isn't any | |
399 | * chance to dispatch it given we prioritize requests in hctx->dispatch. | |
400 | */ | |
401 | if ((rq->rq_flags & RQF_FLUSH_SEQ) || blk_rq_is_passthrough(rq)) | |
a6a252e6 | 402 | return true; |
a6a252e6 | 403 | |
a6a252e6 | 404 | return false; |
bd166ef1 | 405 | } |
bd166ef1 | 406 | |
bd6737f1 | 407 | void blk_mq_sched_insert_request(struct request *rq, bool at_head, |
9e97d295 | 408 | bool run_queue, bool async) |
bd6737f1 JA |
409 | { |
410 | struct request_queue *q = rq->q; | |
411 | struct elevator_queue *e = q->elevator; | |
412 | struct blk_mq_ctx *ctx = rq->mq_ctx; | |
ea4f995e | 413 | struct blk_mq_hw_ctx *hctx = rq->mq_hctx; |
bd6737f1 | 414 | |
e44a6a23 | 415 | WARN_ON(e && (rq->tag != BLK_MQ_NO_TAG)); |
923218f6 | 416 | |
5218e12e | 417 | if (blk_mq_sched_bypass_insert(hctx, rq)) { |
cc3200ea ML |
418 | /* |
419 | * Firstly normal IO request is inserted to scheduler queue or | |
420 | * sw queue, meantime we add flush request to dispatch queue( | |
421 | * hctx->dispatch) directly and there is at most one in-flight | |
422 | * flush request for each hw queue, so it doesn't matter to add | |
423 | * flush request to tail or front of the dispatch queue. | |
424 | * | |
425 | * Secondly in case of NCQ, flush request belongs to non-NCQ | |
426 | * command, and queueing it will fail when there is any | |
427 | * in-flight normal IO request(NCQ command). When adding flush | |
428 | * rq to the front of hctx->dispatch, it is easier to introduce | |
429 | * extra time to flush rq's latency because of S_SCHED_RESTART | |
430 | * compared with adding to the tail of dispatch queue, then | |
431 | * chance of flush merge is increased, and less flush requests | |
432 | * will be issued to controller. It is observed that ~10% time | |
433 | * is saved in blktests block/004 on disk attached to AHCI/NCQ | |
434 | * drive when adding flush rq to the front of hctx->dispatch. | |
435 | * | |
436 | * Simply queue flush rq to the front of hctx->dispatch so that | |
437 | * intensive flush workloads can benefit in case of NCQ HW. | |
438 | */ | |
439 | at_head = (rq->rq_flags & RQF_FLUSH_SEQ) ? true : at_head; | |
01e99aec | 440 | blk_mq_request_bypass_insert(rq, at_head, false); |
0cacba6c | 441 | goto run; |
01e99aec | 442 | } |
0cacba6c | 443 | |
e42cfb1d | 444 | if (e) { |
bd6737f1 JA |
445 | LIST_HEAD(list); |
446 | ||
447 | list_add(&rq->queuelist, &list); | |
f9cd4bfe | 448 | e->type->ops.insert_requests(hctx, &list, at_head); |
bd6737f1 JA |
449 | } else { |
450 | spin_lock(&ctx->lock); | |
451 | __blk_mq_insert_request(hctx, rq, at_head); | |
452 | spin_unlock(&ctx->lock); | |
453 | } | |
454 | ||
0cacba6c | 455 | run: |
bd6737f1 JA |
456 | if (run_queue) |
457 | blk_mq_run_hw_queue(hctx, async); | |
458 | } | |
459 | ||
67cae4c9 | 460 | void blk_mq_sched_insert_requests(struct blk_mq_hw_ctx *hctx, |
bd6737f1 JA |
461 | struct blk_mq_ctx *ctx, |
462 | struct list_head *list, bool run_queue_async) | |
463 | { | |
f9afca4d | 464 | struct elevator_queue *e; |
e87eb301 ML |
465 | struct request_queue *q = hctx->queue; |
466 | ||
467 | /* | |
468 | * blk_mq_sched_insert_requests() is called from flush plug | |
469 | * context only, and hold one usage counter to prevent queue | |
470 | * from being released. | |
471 | */ | |
472 | percpu_ref_get(&q->q_usage_counter); | |
bd6737f1 | 473 | |
f9afca4d | 474 | e = hctx->queue->elevator; |
e42cfb1d | 475 | if (e) { |
f9cd4bfe | 476 | e->type->ops.insert_requests(hctx, list, false); |
e42cfb1d | 477 | } else { |
6ce3dd6e ML |
478 | /* |
479 | * try to issue requests directly if the hw queue isn't | |
480 | * busy in case of 'none' scheduler, and this way may save | |
481 | * us one extra enqueue & dequeue to sw queue. | |
482 | */ | |
ef1661ba | 483 | if (!hctx->dispatch_busy && !run_queue_async) { |
4cafe86c ML |
484 | blk_mq_run_dispatch_ops(hctx->queue, |
485 | blk_mq_try_issue_list_directly(hctx, list)); | |
fd9c40f6 | 486 | if (list_empty(list)) |
e87eb301 | 487 | goto out; |
fd9c40f6 BVA |
488 | } |
489 | blk_mq_insert_requests(hctx, ctx, list); | |
6ce3dd6e | 490 | } |
bd6737f1 JA |
491 | |
492 | blk_mq_run_hw_queue(hctx, run_queue_async); | |
e87eb301 ML |
493 | out: |
494 | percpu_ref_put(&q->q_usage_counter); | |
bd6737f1 JA |
495 | } |
496 | ||
d99a6bb3 JG |
497 | static int blk_mq_sched_alloc_map_and_rqs(struct request_queue *q, |
498 | struct blk_mq_hw_ctx *hctx, | |
499 | unsigned int hctx_idx) | |
6917ff0b | 500 | { |
079a2e3e JG |
501 | if (blk_mq_is_shared_tags(q->tag_set->flags)) { |
502 | hctx->sched_tags = q->sched_shared_tags; | |
e155b0c2 JG |
503 | return 0; |
504 | } | |
505 | ||
63064be1 JG |
506 | hctx->sched_tags = blk_mq_alloc_map_and_rqs(q->tag_set, hctx_idx, |
507 | q->nr_requests); | |
6917ff0b | 508 | |
6917ff0b OS |
509 | if (!hctx->sched_tags) |
510 | return -ENOMEM; | |
63064be1 | 511 | return 0; |
6917ff0b OS |
512 | } |
513 | ||
079a2e3e | 514 | static void blk_mq_exit_sched_shared_tags(struct request_queue *queue) |
e155b0c2 | 515 | { |
079a2e3e JG |
516 | blk_mq_free_rq_map(queue->sched_shared_tags); |
517 | queue->sched_shared_tags = NULL; | |
e155b0c2 JG |
518 | } |
519 | ||
c3e22192 | 520 | /* called in queue's release handler, tagset has gone away */ |
e155b0c2 | 521 | static void blk_mq_sched_tags_teardown(struct request_queue *q, unsigned int flags) |
bd166ef1 | 522 | { |
bd166ef1 | 523 | struct blk_mq_hw_ctx *hctx; |
4f481208 | 524 | unsigned long i; |
6917ff0b | 525 | |
c3e22192 ML |
526 | queue_for_each_hw_ctx(q, hctx, i) { |
527 | if (hctx->sched_tags) { | |
8bdf7b3f | 528 | if (!blk_mq_is_shared_tags(flags)) |
e155b0c2 | 529 | blk_mq_free_rq_map(hctx->sched_tags); |
c3e22192 ML |
530 | hctx->sched_tags = NULL; |
531 | } | |
532 | } | |
e155b0c2 | 533 | |
079a2e3e JG |
534 | if (blk_mq_is_shared_tags(flags)) |
535 | blk_mq_exit_sched_shared_tags(q); | |
6917ff0b OS |
536 | } |
537 | ||
079a2e3e | 538 | static int blk_mq_init_sched_shared_tags(struct request_queue *queue) |
d97e594c JG |
539 | { |
540 | struct blk_mq_tag_set *set = queue->tag_set; | |
d97e594c JG |
541 | |
542 | /* | |
543 | * Set initial depth at max so that we don't need to reallocate for | |
544 | * updating nr_requests. | |
545 | */ | |
079a2e3e | 546 | queue->sched_shared_tags = blk_mq_alloc_map_and_rqs(set, |
e155b0c2 JG |
547 | BLK_MQ_NO_HCTX_IDX, |
548 | MAX_SCHED_RQ); | |
079a2e3e | 549 | if (!queue->sched_shared_tags) |
e155b0c2 | 550 | return -ENOMEM; |
d97e594c | 551 | |
079a2e3e | 552 | blk_mq_tag_update_sched_shared_tags(queue); |
d97e594c JG |
553 | |
554 | return 0; | |
555 | } | |
556 | ||
8ed40ee3 | 557 | /* caller must have a reference to @e, will grab another one if successful */ |
6917ff0b OS |
558 | int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e) |
559 | { | |
4f481208 | 560 | unsigned int flags = q->tag_set->flags; |
6917ff0b | 561 | struct blk_mq_hw_ctx *hctx; |
ee056f98 | 562 | struct elevator_queue *eq; |
4f481208 | 563 | unsigned long i; |
6917ff0b OS |
564 | int ret; |
565 | ||
bd166ef1 | 566 | /* |
32825c45 ML |
567 | * Default to double of smaller one between hw queue_depth and 128, |
568 | * since we don't split into sync/async like the old code did. | |
569 | * Additionally, this is a per-hw queue depth. | |
bd166ef1 | 570 | */ |
32825c45 | 571 | q->nr_requests = 2 * min_t(unsigned int, q->tag_set->queue_depth, |
d2a27964 | 572 | BLKDEV_DEFAULT_RQ); |
bd166ef1 | 573 | |
079a2e3e JG |
574 | if (blk_mq_is_shared_tags(flags)) { |
575 | ret = blk_mq_init_sched_shared_tags(q); | |
bd166ef1 | 576 | if (ret) |
e155b0c2 | 577 | return ret; |
d97e594c JG |
578 | } |
579 | ||
e155b0c2 JG |
580 | queue_for_each_hw_ctx(q, hctx, i) { |
581 | ret = blk_mq_sched_alloc_map_and_rqs(q, hctx, i); | |
d97e594c | 582 | if (ret) |
d99a6bb3 | 583 | goto err_free_map_and_rqs; |
bd166ef1 JA |
584 | } |
585 | ||
f9cd4bfe | 586 | ret = e->ops.init_sched(q, e); |
6917ff0b | 587 | if (ret) |
e155b0c2 | 588 | goto err_free_map_and_rqs; |
bd166ef1 | 589 | |
5cf9c91b | 590 | mutex_lock(&q->debugfs_mutex); |
d332ce09 | 591 | blk_mq_debugfs_register_sched(q); |
5cf9c91b | 592 | mutex_unlock(&q->debugfs_mutex); |
d332ce09 OS |
593 | |
594 | queue_for_each_hw_ctx(q, hctx, i) { | |
f9cd4bfe JA |
595 | if (e->ops.init_hctx) { |
596 | ret = e->ops.init_hctx(hctx, i); | |
ee056f98 OS |
597 | if (ret) { |
598 | eq = q->elevator; | |
1820f4f0 | 599 | blk_mq_sched_free_rqs(q); |
ee056f98 OS |
600 | blk_mq_exit_sched(q, eq); |
601 | kobject_put(&eq->kobj); | |
602 | return ret; | |
603 | } | |
604 | } | |
5cf9c91b | 605 | mutex_lock(&q->debugfs_mutex); |
d332ce09 | 606 | blk_mq_debugfs_register_sched_hctx(q, hctx); |
5cf9c91b | 607 | mutex_unlock(&q->debugfs_mutex); |
ee056f98 OS |
608 | } |
609 | ||
bd166ef1 | 610 | return 0; |
bd166ef1 | 611 | |
d99a6bb3 | 612 | err_free_map_and_rqs: |
1820f4f0 | 613 | blk_mq_sched_free_rqs(q); |
e155b0c2 JG |
614 | blk_mq_sched_tags_teardown(q, flags); |
615 | ||
54d5329d | 616 | q->elevator = NULL; |
6917ff0b | 617 | return ret; |
bd166ef1 | 618 | } |
d3484991 | 619 | |
c3e22192 ML |
620 | /* |
621 | * called in either blk_queue_cleanup or elevator_switch, tagset | |
622 | * is required for freeing requests | |
623 | */ | |
1820f4f0 | 624 | void blk_mq_sched_free_rqs(struct request_queue *q) |
c3e22192 ML |
625 | { |
626 | struct blk_mq_hw_ctx *hctx; | |
4f481208 | 627 | unsigned long i; |
c3e22192 | 628 | |
079a2e3e JG |
629 | if (blk_mq_is_shared_tags(q->tag_set->flags)) { |
630 | blk_mq_free_rqs(q->tag_set, q->sched_shared_tags, | |
e155b0c2 JG |
631 | BLK_MQ_NO_HCTX_IDX); |
632 | } else { | |
633 | queue_for_each_hw_ctx(q, hctx, i) { | |
634 | if (hctx->sched_tags) | |
635 | blk_mq_free_rqs(q->tag_set, | |
636 | hctx->sched_tags, i); | |
637 | } | |
c3e22192 ML |
638 | } |
639 | } | |
640 | ||
54d5329d OS |
641 | void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e) |
642 | { | |
ee056f98 | 643 | struct blk_mq_hw_ctx *hctx; |
4f481208 | 644 | unsigned long i; |
f0c1c4d2 | 645 | unsigned int flags = 0; |
ee056f98 | 646 | |
d332ce09 | 647 | queue_for_each_hw_ctx(q, hctx, i) { |
5cf9c91b | 648 | mutex_lock(&q->debugfs_mutex); |
d332ce09 | 649 | blk_mq_debugfs_unregister_sched_hctx(hctx); |
5cf9c91b CH |
650 | mutex_unlock(&q->debugfs_mutex); |
651 | ||
f9cd4bfe JA |
652 | if (e->type->ops.exit_hctx && hctx->sched_data) { |
653 | e->type->ops.exit_hctx(hctx, i); | |
d332ce09 | 654 | hctx->sched_data = NULL; |
ee056f98 | 655 | } |
f0c1c4d2 | 656 | flags = hctx->flags; |
ee056f98 | 657 | } |
5cf9c91b CH |
658 | |
659 | mutex_lock(&q->debugfs_mutex); | |
d332ce09 | 660 | blk_mq_debugfs_unregister_sched(q); |
5cf9c91b CH |
661 | mutex_unlock(&q->debugfs_mutex); |
662 | ||
f9cd4bfe JA |
663 | if (e->type->ops.exit_sched) |
664 | e->type->ops.exit_sched(e); | |
e155b0c2 | 665 | blk_mq_sched_tags_teardown(q, flags); |
54d5329d OS |
666 | q->elevator = NULL; |
667 | } |