]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - block/blk-mq-sched.c
bfq-iosched: fix NULL ioc check in bfq_get_rq_private
[mirror_ubuntu-bionic-kernel.git] / block / blk-mq-sched.c
CommitLineData
bd166ef1
JA
1/*
2 * blk-mq scheduling framework
3 *
4 * Copyright (C) 2016 Jens Axboe
5 */
6#include <linux/kernel.h>
7#include <linux/module.h>
8#include <linux/blk-mq.h>
9
10#include <trace/events/block.h>
11
12#include "blk.h"
13#include "blk-mq.h"
d332ce09 14#include "blk-mq-debugfs.h"
bd166ef1
JA
15#include "blk-mq-sched.h"
16#include "blk-mq-tag.h"
17#include "blk-wbt.h"
18
19void blk_mq_sched_free_hctx_data(struct request_queue *q,
20 void (*exit)(struct blk_mq_hw_ctx *))
21{
22 struct blk_mq_hw_ctx *hctx;
23 int i;
24
25 queue_for_each_hw_ctx(q, hctx, i) {
26 if (exit && hctx->sched_data)
27 exit(hctx);
28 kfree(hctx->sched_data);
29 hctx->sched_data = NULL;
30 }
31}
32EXPORT_SYMBOL_GPL(blk_mq_sched_free_hctx_data);
33
bd166ef1 34static void __blk_mq_sched_assign_ioc(struct request_queue *q,
f1ba8261
PV
35 struct request *rq,
36 struct bio *bio,
37 struct io_context *ioc)
bd166ef1 38{
ea511e3c 39 struct elevator_queue *e = q->elevator;
bd166ef1
JA
40 struct io_cq *icq;
41
42 spin_lock_irq(q->queue_lock);
43 icq = ioc_lookup_icq(ioc, q);
44 spin_unlock_irq(q->queue_lock);
45
46 if (!icq) {
47 icq = ioc_create_icq(ioc, q, GFP_ATOMIC);
48 if (!icq)
49 return;
50 }
51
52 rq->elv.icq = icq;
ea511e3c
CH
53 if (e && e->type->ops.mq.get_rq_priv &&
54 e->type->ops.mq.get_rq_priv(q, rq, bio)) {
55 rq->elv.icq = NULL;
bd166ef1
JA
56 return;
57 }
58
ea511e3c
CH
59 rq->rq_flags |= RQF_ELVPRIV;
60 get_io_context(icq->ioc);
bd166ef1
JA
61}
62
d2c0d383
CH
63void blk_mq_sched_assign_ioc(struct request_queue *q, struct request *rq,
64 struct bio *bio)
bd166ef1
JA
65{
66 struct io_context *ioc;
67
68 ioc = rq_ioc(bio);
69 if (ioc)
f1ba8261 70 __blk_mq_sched_assign_ioc(q, rq, bio, ioc);
bd166ef1
JA
71}
72
bd166ef1
JA
73void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)
74{
81380ca1
OS
75 struct request_queue *q = hctx->queue;
76 struct elevator_queue *e = q->elevator;
64765a75
JA
77 const bool has_sched_dispatch = e && e->type->ops.mq.dispatch_request;
78 bool did_work = false;
bd166ef1
JA
79 LIST_HEAD(rq_list);
80
81 if (unlikely(blk_mq_hctx_stopped(hctx)))
82 return;
83
84 hctx->run++;
85
86 /*
87 * If we have previous entries on our dispatch list, grab them first for
88 * more fair dispatch.
89 */
90 if (!list_empty_careful(&hctx->dispatch)) {
91 spin_lock(&hctx->lock);
92 if (!list_empty(&hctx->dispatch))
93 list_splice_init(&hctx->dispatch, &rq_list);
94 spin_unlock(&hctx->lock);
95 }
96
97 /*
98 * Only ask the scheduler for requests, if we didn't have residual
99 * requests from the dispatch list. This is to avoid the case where
100 * we only ever dispatch a fraction of the requests available because
101 * of low device queue depth. Once we pull requests out of the IO
102 * scheduler, we can no longer merge or sort them. So it's best to
103 * leave them there for as long as we can. Mark the hw queue as
104 * needing a restart in that case.
105 */
c13660a0 106 if (!list_empty(&rq_list)) {
d38d3515 107 blk_mq_sched_mark_restart_hctx(hctx);
81380ca1 108 did_work = blk_mq_dispatch_rq_list(q, &rq_list);
64765a75 109 } else if (!has_sched_dispatch) {
c13660a0 110 blk_mq_flush_busy_ctxs(hctx, &rq_list);
81380ca1 111 blk_mq_dispatch_rq_list(q, &rq_list);
64765a75
JA
112 }
113
114 /*
115 * We want to dispatch from the scheduler if we had no work left
116 * on the dispatch list, OR if we did have work but weren't able
117 * to make progress.
118 */
119 if (!did_work && has_sched_dispatch) {
c13660a0
JA
120 do {
121 struct request *rq;
122
123 rq = e->type->ops.mq.dispatch_request(hctx);
124 if (!rq)
125 break;
126 list_add(&rq->queuelist, &rq_list);
81380ca1 127 } while (blk_mq_dispatch_rq_list(q, &rq_list));
c13660a0 128 }
bd166ef1
JA
129}
130
e4d750c9
JA
131bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio,
132 struct request **merged_request)
bd166ef1
JA
133{
134 struct request *rq;
bd166ef1 135
34fe7c05
CH
136 switch (elv_merge(q, &rq, bio)) {
137 case ELEVATOR_BACK_MERGE:
bd166ef1
JA
138 if (!blk_mq_sched_allow_merge(q, rq, bio))
139 return false;
34fe7c05
CH
140 if (!bio_attempt_back_merge(q, rq, bio))
141 return false;
142 *merged_request = attempt_back_merge(q, rq);
143 if (!*merged_request)
144 elv_merged_request(q, rq, ELEVATOR_BACK_MERGE);
145 return true;
146 case ELEVATOR_FRONT_MERGE:
bd166ef1
JA
147 if (!blk_mq_sched_allow_merge(q, rq, bio))
148 return false;
34fe7c05
CH
149 if (!bio_attempt_front_merge(q, rq, bio))
150 return false;
151 *merged_request = attempt_front_merge(q, rq);
152 if (!*merged_request)
153 elv_merged_request(q, rq, ELEVATOR_FRONT_MERGE);
154 return true;
155 default:
156 return false;
bd166ef1 157 }
bd166ef1
JA
158}
159EXPORT_SYMBOL_GPL(blk_mq_sched_try_merge);
160
9bddeb2a
ML
161/*
162 * Reverse check our software queue for entries that we could potentially
163 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
164 * too much time checking for merges.
165 */
166static bool blk_mq_attempt_merge(struct request_queue *q,
167 struct blk_mq_ctx *ctx, struct bio *bio)
168{
169 struct request *rq;
170 int checked = 8;
171
172 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
173 bool merged = false;
174
175 if (!checked--)
176 break;
177
178 if (!blk_rq_merge_ok(rq, bio))
179 continue;
180
181 switch (blk_try_merge(rq, bio)) {
182 case ELEVATOR_BACK_MERGE:
183 if (blk_mq_sched_allow_merge(q, rq, bio))
184 merged = bio_attempt_back_merge(q, rq, bio);
185 break;
186 case ELEVATOR_FRONT_MERGE:
187 if (blk_mq_sched_allow_merge(q, rq, bio))
188 merged = bio_attempt_front_merge(q, rq, bio);
189 break;
190 case ELEVATOR_DISCARD_MERGE:
191 merged = bio_attempt_discard_merge(q, rq, bio);
192 break;
193 default:
194 continue;
195 }
196
197 if (merged)
198 ctx->rq_merged++;
199 return merged;
200 }
201
202 return false;
203}
204
bd166ef1
JA
205bool __blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio)
206{
207 struct elevator_queue *e = q->elevator;
9bddeb2a
ML
208 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
209 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
210 bool ret = false;
bd166ef1 211
9bddeb2a 212 if (e && e->type->ops.mq.bio_merge) {
bd166ef1
JA
213 blk_mq_put_ctx(ctx);
214 return e->type->ops.mq.bio_merge(hctx, bio);
215 }
216
9bddeb2a
ML
217 if (hctx->flags & BLK_MQ_F_SHOULD_MERGE) {
218 /* default per sw-queue merge */
219 spin_lock(&ctx->lock);
220 ret = blk_mq_attempt_merge(q, ctx, bio);
221 spin_unlock(&ctx->lock);
222 }
223
224 blk_mq_put_ctx(ctx);
225 return ret;
bd166ef1
JA
226}
227
228bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq)
229{
230 return rq_mergeable(rq) && elv_attempt_insert_merge(q, rq);
231}
232EXPORT_SYMBOL_GPL(blk_mq_sched_try_insert_merge);
233
234void blk_mq_sched_request_inserted(struct request *rq)
235{
236 trace_block_rq_insert(rq->q, rq);
237}
238EXPORT_SYMBOL_GPL(blk_mq_sched_request_inserted);
239
0cacba6c
OS
240static bool blk_mq_sched_bypass_insert(struct blk_mq_hw_ctx *hctx,
241 struct request *rq)
bd166ef1
JA
242{
243 if (rq->tag == -1) {
244 rq->rq_flags |= RQF_SORTED;
245 return false;
246 }
247
248 /*
249 * If we already have a real request tag, send directly to
250 * the dispatch list.
251 */
252 spin_lock(&hctx->lock);
253 list_add(&rq->queuelist, &hctx->dispatch);
254 spin_unlock(&hctx->lock);
255 return true;
256}
bd166ef1 257
6d8c6c0f 258static bool blk_mq_sched_restart_hctx(struct blk_mq_hw_ctx *hctx)
50e1dab8
JA
259{
260 if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state)) {
261 clear_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
6d8c6c0f 262 if (blk_mq_hctx_has_pending(hctx)) {
50e1dab8 263 blk_mq_run_hw_queue(hctx, true);
6d8c6c0f
BVA
264 return true;
265 }
50e1dab8 266 }
6d8c6c0f 267 return false;
50e1dab8
JA
268}
269
6d8c6c0f
BVA
270/**
271 * list_for_each_entry_rcu_rr - iterate in a round-robin fashion over rcu list
272 * @pos: loop cursor.
273 * @skip: the list element that will not be examined. Iteration starts at
274 * @skip->next.
275 * @head: head of the list to examine. This list must have at least one
276 * element, namely @skip.
277 * @member: name of the list_head structure within typeof(*pos).
278 */
279#define list_for_each_entry_rcu_rr(pos, skip, head, member) \
280 for ((pos) = (skip); \
281 (pos = (pos)->member.next != (head) ? list_entry_rcu( \
282 (pos)->member.next, typeof(*pos), member) : \
283 list_entry_rcu((pos)->member.next->next, typeof(*pos), member)), \
284 (pos) != (skip); )
50e1dab8 285
6d8c6c0f
BVA
286/*
287 * Called after a driver tag has been freed to check whether a hctx needs to
288 * be restarted. Restarts @hctx if its tag set is not shared. Restarts hardware
289 * queues in a round-robin fashion if the tag set of @hctx is shared with other
290 * hardware queues.
291 */
292void blk_mq_sched_restart(struct blk_mq_hw_ctx *const hctx)
293{
294 struct blk_mq_tags *const tags = hctx->tags;
295 struct blk_mq_tag_set *const set = hctx->queue->tag_set;
296 struct request_queue *const queue = hctx->queue, *q;
297 struct blk_mq_hw_ctx *hctx2;
298 unsigned int i, j;
299
300 if (set->flags & BLK_MQ_F_TAG_SHARED) {
301 rcu_read_lock();
302 list_for_each_entry_rcu_rr(q, queue, &set->tag_list,
303 tag_set_list) {
304 queue_for_each_hw_ctx(q, hctx2, i)
305 if (hctx2->tags == tags &&
306 blk_mq_sched_restart_hctx(hctx2))
307 goto done;
308 }
309 j = hctx->queue_num + 1;
310 for (i = 0; i < queue->nr_hw_queues; i++, j++) {
311 if (j == queue->nr_hw_queues)
312 j = 0;
313 hctx2 = queue->queue_hw_ctx[j];
314 if (hctx2->tags == tags &&
315 blk_mq_sched_restart_hctx(hctx2))
316 break;
d38d3515 317 }
6d8c6c0f
BVA
318done:
319 rcu_read_unlock();
d38d3515 320 } else {
50e1dab8 321 blk_mq_sched_restart_hctx(hctx);
50e1dab8
JA
322 }
323}
324
bd6737f1
JA
325/*
326 * Add flush/fua to the queue. If we fail getting a driver tag, then
327 * punt to the requeue list. Requeue will re-invoke us from a context
328 * that's safe to block from.
329 */
330static void blk_mq_sched_insert_flush(struct blk_mq_hw_ctx *hctx,
331 struct request *rq, bool can_block)
332{
333 if (blk_mq_get_driver_tag(rq, &hctx, can_block)) {
334 blk_insert_flush(rq);
335 blk_mq_run_hw_queue(hctx, true);
336 } else
c7a571b4 337 blk_mq_add_to_requeue_list(rq, false, true);
bd6737f1
JA
338}
339
340void blk_mq_sched_insert_request(struct request *rq, bool at_head,
341 bool run_queue, bool async, bool can_block)
342{
343 struct request_queue *q = rq->q;
344 struct elevator_queue *e = q->elevator;
345 struct blk_mq_ctx *ctx = rq->mq_ctx;
346 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
347
f3a8ab7d 348 if (rq->tag == -1 && op_is_flush(rq->cmd_flags)) {
bd6737f1
JA
349 blk_mq_sched_insert_flush(hctx, rq, can_block);
350 return;
351 }
352
0cacba6c
OS
353 if (e && blk_mq_sched_bypass_insert(hctx, rq))
354 goto run;
355
bd6737f1
JA
356 if (e && e->type->ops.mq.insert_requests) {
357 LIST_HEAD(list);
358
359 list_add(&rq->queuelist, &list);
360 e->type->ops.mq.insert_requests(hctx, &list, at_head);
361 } else {
362 spin_lock(&ctx->lock);
363 __blk_mq_insert_request(hctx, rq, at_head);
364 spin_unlock(&ctx->lock);
365 }
366
0cacba6c 367run:
bd6737f1
JA
368 if (run_queue)
369 blk_mq_run_hw_queue(hctx, async);
370}
371
372void blk_mq_sched_insert_requests(struct request_queue *q,
373 struct blk_mq_ctx *ctx,
374 struct list_head *list, bool run_queue_async)
375{
376 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
377 struct elevator_queue *e = hctx->queue->elevator;
378
0cacba6c
OS
379 if (e) {
380 struct request *rq, *next;
381
382 /*
383 * We bypass requests that already have a driver tag assigned,
384 * which should only be flushes. Flushes are only ever inserted
385 * as single requests, so we shouldn't ever hit the
386 * WARN_ON_ONCE() below (but let's handle it just in case).
387 */
388 list_for_each_entry_safe(rq, next, list, queuelist) {
389 if (WARN_ON_ONCE(rq->tag != -1)) {
390 list_del_init(&rq->queuelist);
391 blk_mq_sched_bypass_insert(hctx, rq);
392 }
393 }
394 }
395
bd6737f1
JA
396 if (e && e->type->ops.mq.insert_requests)
397 e->type->ops.mq.insert_requests(hctx, list, false);
398 else
399 blk_mq_insert_requests(hctx, ctx, list);
400
401 blk_mq_run_hw_queue(hctx, run_queue_async);
402}
403
bd166ef1
JA
404static void blk_mq_sched_free_tags(struct blk_mq_tag_set *set,
405 struct blk_mq_hw_ctx *hctx,
406 unsigned int hctx_idx)
407{
408 if (hctx->sched_tags) {
409 blk_mq_free_rqs(set, hctx->sched_tags, hctx_idx);
410 blk_mq_free_rq_map(hctx->sched_tags);
411 hctx->sched_tags = NULL;
412 }
413}
414
6917ff0b
OS
415static int blk_mq_sched_alloc_tags(struct request_queue *q,
416 struct blk_mq_hw_ctx *hctx,
417 unsigned int hctx_idx)
418{
419 struct blk_mq_tag_set *set = q->tag_set;
420 int ret;
421
422 hctx->sched_tags = blk_mq_alloc_rq_map(set, hctx_idx, q->nr_requests,
423 set->reserved_tags);
424 if (!hctx->sched_tags)
425 return -ENOMEM;
426
427 ret = blk_mq_alloc_rqs(set, hctx->sched_tags, hctx_idx, q->nr_requests);
428 if (ret)
429 blk_mq_sched_free_tags(set, hctx, hctx_idx);
430
431 return ret;
432}
433
54d5329d 434static void blk_mq_sched_tags_teardown(struct request_queue *q)
bd166ef1
JA
435{
436 struct blk_mq_tag_set *set = q->tag_set;
437 struct blk_mq_hw_ctx *hctx;
6917ff0b
OS
438 int i;
439
440 queue_for_each_hw_ctx(q, hctx, i)
441 blk_mq_sched_free_tags(set, hctx, i);
442}
443
93252632
OS
444int blk_mq_sched_init_hctx(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
445 unsigned int hctx_idx)
446{
447 struct elevator_queue *e = q->elevator;
ee056f98 448 int ret;
93252632
OS
449
450 if (!e)
451 return 0;
452
ee056f98
OS
453 ret = blk_mq_sched_alloc_tags(q, hctx, hctx_idx);
454 if (ret)
455 return ret;
456
457 if (e->type->ops.mq.init_hctx) {
458 ret = e->type->ops.mq.init_hctx(hctx, hctx_idx);
459 if (ret) {
460 blk_mq_sched_free_tags(q->tag_set, hctx, hctx_idx);
461 return ret;
462 }
463 }
464
d332ce09
OS
465 blk_mq_debugfs_register_sched_hctx(q, hctx);
466
ee056f98 467 return 0;
93252632
OS
468}
469
470void blk_mq_sched_exit_hctx(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
471 unsigned int hctx_idx)
472{
473 struct elevator_queue *e = q->elevator;
474
475 if (!e)
476 return;
477
d332ce09
OS
478 blk_mq_debugfs_unregister_sched_hctx(hctx);
479
ee056f98
OS
480 if (e->type->ops.mq.exit_hctx && hctx->sched_data) {
481 e->type->ops.mq.exit_hctx(hctx, hctx_idx);
482 hctx->sched_data = NULL;
483 }
484
93252632
OS
485 blk_mq_sched_free_tags(q->tag_set, hctx, hctx_idx);
486}
487
6917ff0b
OS
488int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e)
489{
490 struct blk_mq_hw_ctx *hctx;
ee056f98 491 struct elevator_queue *eq;
6917ff0b
OS
492 unsigned int i;
493 int ret;
494
495 if (!e) {
496 q->elevator = NULL;
497 return 0;
498 }
bd166ef1
JA
499
500 /*
501 * Default to 256, since we don't split into sync/async like the
502 * old code did. Additionally, this is a per-hw queue depth.
503 */
504 q->nr_requests = 2 * BLKDEV_MAX_RQ;
505
bd166ef1 506 queue_for_each_hw_ctx(q, hctx, i) {
6917ff0b 507 ret = blk_mq_sched_alloc_tags(q, hctx, i);
bd166ef1 508 if (ret)
6917ff0b 509 goto err;
bd166ef1
JA
510 }
511
6917ff0b
OS
512 ret = e->ops.mq.init_sched(q, e);
513 if (ret)
514 goto err;
bd166ef1 515
d332ce09
OS
516 blk_mq_debugfs_register_sched(q);
517
518 queue_for_each_hw_ctx(q, hctx, i) {
519 if (e->ops.mq.init_hctx) {
ee056f98
OS
520 ret = e->ops.mq.init_hctx(hctx, i);
521 if (ret) {
522 eq = q->elevator;
523 blk_mq_exit_sched(q, eq);
524 kobject_put(&eq->kobj);
525 return ret;
526 }
527 }
d332ce09 528 blk_mq_debugfs_register_sched_hctx(q, hctx);
ee056f98
OS
529 }
530
bd166ef1 531 return 0;
bd166ef1 532
6917ff0b 533err:
54d5329d
OS
534 blk_mq_sched_tags_teardown(q);
535 q->elevator = NULL;
6917ff0b 536 return ret;
bd166ef1 537}
d3484991 538
54d5329d
OS
539void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e)
540{
ee056f98
OS
541 struct blk_mq_hw_ctx *hctx;
542 unsigned int i;
543
d332ce09
OS
544 queue_for_each_hw_ctx(q, hctx, i) {
545 blk_mq_debugfs_unregister_sched_hctx(hctx);
546 if (e->type->ops.mq.exit_hctx && hctx->sched_data) {
547 e->type->ops.mq.exit_hctx(hctx, i);
548 hctx->sched_data = NULL;
ee056f98
OS
549 }
550 }
d332ce09 551 blk_mq_debugfs_unregister_sched(q);
54d5329d
OS
552 if (e->type->ops.mq.exit_sched)
553 e->type->ops.mq.exit_sched(e);
554 blk_mq_sched_tags_teardown(q);
555 q->elevator = NULL;
556}
557
d3484991
JA
558int blk_mq_sched_init(struct request_queue *q)
559{
560 int ret;
561
d3484991
JA
562 mutex_lock(&q->sysfs_lock);
563 ret = elevator_init(q, NULL);
564 mutex_unlock(&q->sysfs_lock);
565
566 return ret;
567}