]> git.ipfire.org Git - thirdparty/linux.git/blame - block/blk-mq.c
blk-mq: move more request initialization to blk_mq_rq_ctx_init
[thirdparty/linux.git] / block / blk-mq.c
CommitLineData
3dcf60bc 1// SPDX-License-Identifier: GPL-2.0
75bb4625
JA
2/*
3 * Block multiqueue core code
4 *
5 * Copyright (C) 2013-2014 Jens Axboe
6 * Copyright (C) 2013-2014 Christoph Hellwig
7 */
320ae51f
JA
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/backing-dev.h>
11#include <linux/bio.h>
12#include <linux/blkdev.h>
f75782e4 13#include <linux/kmemleak.h>
320ae51f
JA
14#include <linux/mm.h>
15#include <linux/init.h>
16#include <linux/slab.h>
17#include <linux/workqueue.h>
18#include <linux/smp.h>
19#include <linux/llist.h>
20#include <linux/list_sort.h>
21#include <linux/cpu.h>
22#include <linux/cache.h>
23#include <linux/sched/sysctl.h>
105ab3d8 24#include <linux/sched/topology.h>
174cd4b1 25#include <linux/sched/signal.h>
320ae51f 26#include <linux/delay.h>
aedcd72f 27#include <linux/crash_dump.h>
88c7b2b7 28#include <linux/prefetch.h>
a892c8d5 29#include <linux/blk-crypto.h>
320ae51f
JA
30
31#include <trace/events/block.h>
32
33#include <linux/blk-mq.h>
54d4e6ab 34#include <linux/t10-pi.h>
320ae51f
JA
35#include "blk.h"
36#include "blk-mq.h"
9c1051aa 37#include "blk-mq-debugfs.h"
320ae51f 38#include "blk-mq-tag.h"
986d413b 39#include "blk-pm.h"
cf43e6be 40#include "blk-stat.h"
bd166ef1 41#include "blk-mq-sched.h"
c1c80384 42#include "blk-rq-qos.h"
320ae51f 43
34dbad5d
OS
44static void blk_mq_poll_stats_start(struct request_queue *q);
45static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb);
46
720b8ccc
SB
47static int blk_mq_poll_stats_bkt(const struct request *rq)
48{
3d244306 49 int ddir, sectors, bucket;
720b8ccc 50
99c749a4 51 ddir = rq_data_dir(rq);
3d244306 52 sectors = blk_rq_stats_sectors(rq);
720b8ccc 53
3d244306 54 bucket = ddir + 2 * ilog2(sectors);
720b8ccc
SB
55
56 if (bucket < 0)
57 return -1;
58 else if (bucket >= BLK_MQ_POLL_STATS_BKTS)
59 return ddir + BLK_MQ_POLL_STATS_BKTS - 2;
60
61 return bucket;
62}
63
320ae51f 64/*
85fae294
YY
65 * Check if any of the ctx, dispatch list or elevator
66 * have pending work in this hardware queue.
320ae51f 67 */
79f720a7 68static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
320ae51f 69{
79f720a7
JA
70 return !list_empty_careful(&hctx->dispatch) ||
71 sbitmap_any_bit_set(&hctx->ctx_map) ||
bd166ef1 72 blk_mq_sched_has_work(hctx);
1429d7c9
JA
73}
74
320ae51f
JA
75/*
76 * Mark this ctx as having pending work in this hardware queue
77 */
78static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
79 struct blk_mq_ctx *ctx)
80{
f31967f0
JA
81 const int bit = ctx->index_hw[hctx->type];
82
83 if (!sbitmap_test_bit(&hctx->ctx_map, bit))
84 sbitmap_set_bit(&hctx->ctx_map, bit);
1429d7c9
JA
85}
86
87static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
88 struct blk_mq_ctx *ctx)
89{
f31967f0
JA
90 const int bit = ctx->index_hw[hctx->type];
91
92 sbitmap_clear_bit(&hctx->ctx_map, bit);
320ae51f
JA
93}
94
f299b7c7
JA
95struct mq_inflight {
96 struct hd_struct *part;
a2e80f6f 97 unsigned int inflight[2];
f299b7c7
JA
98};
99
7baa8572 100static bool blk_mq_check_inflight(struct blk_mq_hw_ctx *hctx,
f299b7c7
JA
101 struct request *rq, void *priv,
102 bool reserved)
103{
104 struct mq_inflight *mi = priv;
105
6131837b 106 if (rq->part == mi->part)
bb4e6b14 107 mi->inflight[rq_data_dir(rq)]++;
7baa8572
JA
108
109 return true;
f299b7c7
JA
110}
111
e016b782 112unsigned int blk_mq_in_flight(struct request_queue *q, struct hd_struct *part)
f299b7c7 113{
a2e80f6f 114 struct mq_inflight mi = { .part = part };
f299b7c7 115
f299b7c7 116 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
e016b782 117
a2e80f6f 118 return mi.inflight[0] + mi.inflight[1];
bf0ddaba
OS
119}
120
121void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part,
122 unsigned int inflight[2])
123{
a2e80f6f 124 struct mq_inflight mi = { .part = part };
bf0ddaba 125
bb4e6b14 126 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
a2e80f6f
PB
127 inflight[0] = mi.inflight[0];
128 inflight[1] = mi.inflight[1];
bf0ddaba
OS
129}
130
1671d522 131void blk_freeze_queue_start(struct request_queue *q)
43a5e4e2 132{
7996a8b5
BL
133 mutex_lock(&q->mq_freeze_lock);
134 if (++q->mq_freeze_depth == 1) {
3ef28e83 135 percpu_ref_kill(&q->q_usage_counter);
7996a8b5 136 mutex_unlock(&q->mq_freeze_lock);
344e9ffc 137 if (queue_is_mq(q))
055f6e18 138 blk_mq_run_hw_queues(q, false);
7996a8b5
BL
139 } else {
140 mutex_unlock(&q->mq_freeze_lock);
cddd5d17 141 }
f3af020b 142}
1671d522 143EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
f3af020b 144
6bae363e 145void blk_mq_freeze_queue_wait(struct request_queue *q)
f3af020b 146{
3ef28e83 147 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
43a5e4e2 148}
6bae363e 149EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
43a5e4e2 150
f91328c4
KB
151int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
152 unsigned long timeout)
153{
154 return wait_event_timeout(q->mq_freeze_wq,
155 percpu_ref_is_zero(&q->q_usage_counter),
156 timeout);
157}
158EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
43a5e4e2 159
f3af020b
TH
160/*
161 * Guarantee no request is in use, so we can change any data structure of
162 * the queue afterward.
163 */
3ef28e83 164void blk_freeze_queue(struct request_queue *q)
f3af020b 165{
3ef28e83
DW
166 /*
167 * In the !blk_mq case we are only calling this to kill the
168 * q_usage_counter, otherwise this increases the freeze depth
169 * and waits for it to return to zero. For this reason there is
170 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
171 * exported to drivers as the only user for unfreeze is blk_mq.
172 */
1671d522 173 blk_freeze_queue_start(q);
f3af020b
TH
174 blk_mq_freeze_queue_wait(q);
175}
3ef28e83
DW
176
177void blk_mq_freeze_queue(struct request_queue *q)
178{
179 /*
180 * ...just an alias to keep freeze and unfreeze actions balanced
181 * in the blk_mq_* namespace
182 */
183 blk_freeze_queue(q);
184}
c761d96b 185EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
f3af020b 186
b4c6a028 187void blk_mq_unfreeze_queue(struct request_queue *q)
320ae51f 188{
7996a8b5
BL
189 mutex_lock(&q->mq_freeze_lock);
190 q->mq_freeze_depth--;
191 WARN_ON_ONCE(q->mq_freeze_depth < 0);
192 if (!q->mq_freeze_depth) {
bdd63160 193 percpu_ref_resurrect(&q->q_usage_counter);
320ae51f 194 wake_up_all(&q->mq_freeze_wq);
add703fd 195 }
7996a8b5 196 mutex_unlock(&q->mq_freeze_lock);
320ae51f 197}
b4c6a028 198EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
320ae51f 199
852ec809
BVA
200/*
201 * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
202 * mpt3sas driver such that this function can be removed.
203 */
204void blk_mq_quiesce_queue_nowait(struct request_queue *q)
205{
8814ce8a 206 blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q);
852ec809
BVA
207}
208EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait);
209
6a83e74d 210/**
69e07c4a 211 * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
6a83e74d
BVA
212 * @q: request queue.
213 *
214 * Note: this function does not prevent that the struct request end_io()
69e07c4a
ML
215 * callback function is invoked. Once this function is returned, we make
216 * sure no dispatch can happen until the queue is unquiesced via
217 * blk_mq_unquiesce_queue().
6a83e74d
BVA
218 */
219void blk_mq_quiesce_queue(struct request_queue *q)
220{
221 struct blk_mq_hw_ctx *hctx;
222 unsigned int i;
223 bool rcu = false;
224
1d9e9bc6 225 blk_mq_quiesce_queue_nowait(q);
f4560ffe 226
6a83e74d
BVA
227 queue_for_each_hw_ctx(q, hctx, i) {
228 if (hctx->flags & BLK_MQ_F_BLOCKING)
05707b64 229 synchronize_srcu(hctx->srcu);
6a83e74d
BVA
230 else
231 rcu = true;
232 }
233 if (rcu)
234 synchronize_rcu();
235}
236EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
237
e4e73913
ML
238/*
239 * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
240 * @q: request queue.
241 *
242 * This function recovers queue into the state before quiescing
243 * which is done by blk_mq_quiesce_queue.
244 */
245void blk_mq_unquiesce_queue(struct request_queue *q)
246{
8814ce8a 247 blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
f4560ffe 248
1d9e9bc6
ML
249 /* dispatch requests which are inserted during quiescing */
250 blk_mq_run_hw_queues(q, true);
e4e73913
ML
251}
252EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
253
aed3ea94
JA
254void blk_mq_wake_waiters(struct request_queue *q)
255{
256 struct blk_mq_hw_ctx *hctx;
257 unsigned int i;
258
259 queue_for_each_hw_ctx(q, hctx, i)
260 if (blk_mq_hw_queue_mapped(hctx))
261 blk_mq_tag_wakeup_all(hctx->tags, true);
262}
263
fe1f4526 264/*
9a91b05b
HT
265 * Only need start/end time stamping if we have iostat or
266 * blk stats enabled, or using an IO scheduler.
fe1f4526
JA
267 */
268static inline bool blk_mq_need_time_stamp(struct request *rq)
269{
9a91b05b 270 return (rq->rq_flags & (RQF_IO_STAT | RQF_STATS)) || rq->q->elevator;
fe1f4526
JA
271}
272
e4cdf1a1 273static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
7ea4d8a4 274 unsigned int tag, u64 alloc_time_ns)
320ae51f 275{
e4cdf1a1
CH
276 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
277 struct request *rq = tags->static_rqs[tag];
bf9ae8c5 278 req_flags_t rq_flags = 0;
c3a148d2 279
e4cdf1a1
CH
280 if (data->flags & BLK_MQ_REQ_INTERNAL) {
281 rq->tag = -1;
282 rq->internal_tag = tag;
283 } else {
d263ed99 284 if (data->hctx->flags & BLK_MQ_F_TAG_SHARED) {
bf9ae8c5 285 rq_flags = RQF_MQ_INFLIGHT;
e4cdf1a1
CH
286 atomic_inc(&data->hctx->nr_active);
287 }
288 rq->tag = tag;
289 rq->internal_tag = -1;
290 data->hctx->tags->rqs[rq->tag] = rq;
291 }
292
af76e555 293 /* csd/requeue_work/fifo_time is initialized before use */
e4cdf1a1
CH
294 rq->q = data->q;
295 rq->mq_ctx = data->ctx;
ea4f995e 296 rq->mq_hctx = data->hctx;
bf9ae8c5 297 rq->rq_flags = rq_flags;
7ea4d8a4 298 rq->cmd_flags = data->cmd_flags;
1b6d65a0
BVA
299 if (data->flags & BLK_MQ_REQ_PREEMPT)
300 rq->rq_flags |= RQF_PREEMPT;
e4cdf1a1 301 if (blk_queue_io_stat(data->q))
e8064021 302 rq->rq_flags |= RQF_IO_STAT;
7c3fb70f 303 INIT_LIST_HEAD(&rq->queuelist);
af76e555
CH
304 INIT_HLIST_NODE(&rq->hash);
305 RB_CLEAR_NODE(&rq->rb_node);
af76e555
CH
306 rq->rq_disk = NULL;
307 rq->part = NULL;
6f816b4b
TH
308#ifdef CONFIG_BLK_RQ_ALLOC_TIME
309 rq->alloc_time_ns = alloc_time_ns;
310#endif
fe1f4526
JA
311 if (blk_mq_need_time_stamp(rq))
312 rq->start_time_ns = ktime_get_ns();
313 else
314 rq->start_time_ns = 0;
544ccc8d 315 rq->io_start_time_ns = 0;
3d244306 316 rq->stats_sectors = 0;
af76e555
CH
317 rq->nr_phys_segments = 0;
318#if defined(CONFIG_BLK_DEV_INTEGRITY)
319 rq->nr_integrity_segments = 0;
320#endif
a892c8d5 321 blk_crypto_rq_set_defaults(rq);
af76e555 322 /* tag was already set */
079076b3 323 WRITE_ONCE(rq->deadline, 0);
af76e555 324
f6be4fb4
JA
325 rq->timeout = 0;
326
af76e555
CH
327 rq->end_io = NULL;
328 rq->end_io_data = NULL;
af76e555 329
7ea4d8a4 330 data->ctx->rq_dispatched[op_is_sync(data->cmd_flags)]++;
12f5b931 331 refcount_set(&rq->ref, 1);
7ea4d8a4
CH
332
333 if (!op_is_flush(data->cmd_flags)) {
334 struct elevator_queue *e = data->q->elevator;
335
336 rq->elv.icq = NULL;
337 if (e && e->type->ops.prepare_request) {
338 if (e->type->icq_cache)
339 blk_mq_sched_assign_ioc(rq);
340
341 e->type->ops.prepare_request(rq);
342 rq->rq_flags |= RQF_ELVPRIV;
343 }
344 }
345
346 data->hctx->queued++;
e4cdf1a1 347 return rq;
5dee8577
CH
348}
349
e6e7abff 350static struct request *__blk_mq_alloc_request(struct blk_mq_alloc_data *data)
d2c0d383 351{
e6e7abff 352 struct request_queue *q = data->q;
d2c0d383 353 struct elevator_queue *e = q->elevator;
e4cdf1a1 354 unsigned int tag;
c05f4220 355 bool clear_ctx_on_error = false;
6f816b4b 356 u64 alloc_time_ns = 0;
d2c0d383 357
6f816b4b
TH
358 /* alloc_time includes depth and tag waits */
359 if (blk_queue_rq_alloc_time(q))
360 alloc_time_ns = ktime_get_ns();
361
21e768b4
BVA
362 if (likely(!data->ctx)) {
363 data->ctx = blk_mq_get_ctx(q);
c05f4220 364 clear_ctx_on_error = true;
21e768b4 365 }
d2c0d383 366 if (likely(!data->hctx))
f9afca4d 367 data->hctx = blk_mq_map_queue(q, data->cmd_flags,
8ccdf4a3 368 data->ctx);
f9afca4d 369 if (data->cmd_flags & REQ_NOWAIT)
03a07c92 370 data->flags |= BLK_MQ_REQ_NOWAIT;
d2c0d383
CH
371
372 if (e) {
373 data->flags |= BLK_MQ_REQ_INTERNAL;
374
375 /*
376 * Flush requests are special and go directly to the
17a51199
JA
377 * dispatch list. Don't include reserved tags in the
378 * limiting, as it isn't useful.
d2c0d383 379 */
f9afca4d
JA
380 if (!op_is_flush(data->cmd_flags) &&
381 e->type->ops.limit_depth &&
17a51199 382 !(data->flags & BLK_MQ_REQ_RESERVED))
f9afca4d 383 e->type->ops.limit_depth(data->cmd_flags, data);
d263ed99
JW
384 } else {
385 blk_mq_tag_busy(data->hctx);
d2c0d383
CH
386 }
387
e4cdf1a1
CH
388 tag = blk_mq_get_tag(data);
389 if (tag == BLK_MQ_TAG_FAIL) {
c05f4220 390 if (clear_ctx_on_error)
1ad43c00 391 data->ctx = NULL;
037cebb8 392 return NULL;
d2c0d383
CH
393 }
394
7ea4d8a4 395 return blk_mq_rq_ctx_init(data, tag, alloc_time_ns);
d2c0d383
CH
396}
397
cd6ce148 398struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
9a95e4ef 399 blk_mq_req_flags_t flags)
320ae51f 400{
e6e7abff
CH
401 struct blk_mq_alloc_data data = {
402 .q = q,
403 .flags = flags,
404 .cmd_flags = op,
405 };
bd166ef1 406 struct request *rq;
a492f075 407 int ret;
320ae51f 408
3a0a5299 409 ret = blk_queue_enter(q, flags);
a492f075
JL
410 if (ret)
411 return ERR_PTR(ret);
320ae51f 412
e6e7abff 413 rq = __blk_mq_alloc_request(&data);
bd166ef1 414 if (!rq)
a5ea5811 415 goto out_queue_exit;
0c4de0f3
CH
416 rq->__data_len = 0;
417 rq->__sector = (sector_t) -1;
418 rq->bio = rq->biotail = NULL;
320ae51f 419 return rq;
a5ea5811
CH
420out_queue_exit:
421 blk_queue_exit(q);
422 return ERR_PTR(-EWOULDBLOCK);
320ae51f 423}
4bb659b1 424EXPORT_SYMBOL(blk_mq_alloc_request);
320ae51f 425
cd6ce148 426struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
9a95e4ef 427 unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx)
1f5bd336 428{
e6e7abff
CH
429 struct blk_mq_alloc_data data = {
430 .q = q,
431 .flags = flags,
432 .cmd_flags = op,
433 };
1f5bd336 434 struct request *rq;
6d2809d5 435 unsigned int cpu;
1f5bd336
ML
436 int ret;
437
438 /*
439 * If the tag allocator sleeps we could get an allocation for a
440 * different hardware context. No need to complicate the low level
441 * allocator for this for the rare use case of a command tied to
442 * a specific queue.
443 */
444 if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)))
445 return ERR_PTR(-EINVAL);
446
447 if (hctx_idx >= q->nr_hw_queues)
448 return ERR_PTR(-EIO);
449
3a0a5299 450 ret = blk_queue_enter(q, flags);
1f5bd336
ML
451 if (ret)
452 return ERR_PTR(ret);
453
c8712c6a
CH
454 /*
455 * Check if the hardware context is actually mapped to anything.
456 * If not tell the caller that it should skip this queue.
457 */
a5ea5811 458 ret = -EXDEV;
e6e7abff
CH
459 data.hctx = q->queue_hw_ctx[hctx_idx];
460 if (!blk_mq_hw_queue_mapped(data.hctx))
a5ea5811 461 goto out_queue_exit;
e6e7abff
CH
462 cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask);
463 data.ctx = __blk_mq_get_ctx(q, cpu);
1f5bd336 464
a5ea5811 465 ret = -EWOULDBLOCK;
e6e7abff 466 rq = __blk_mq_alloc_request(&data);
6d2809d5 467 if (!rq)
a5ea5811 468 goto out_queue_exit;
6d2809d5 469 return rq;
a5ea5811
CH
470out_queue_exit:
471 blk_queue_exit(q);
472 return ERR_PTR(ret);
1f5bd336
ML
473}
474EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
475
12f5b931
KB
476static void __blk_mq_free_request(struct request *rq)
477{
478 struct request_queue *q = rq->q;
479 struct blk_mq_ctx *ctx = rq->mq_ctx;
ea4f995e 480 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
12f5b931
KB
481 const int sched_tag = rq->internal_tag;
482
a892c8d5 483 blk_crypto_free_request(rq);
986d413b 484 blk_pm_mark_last_busy(rq);
ea4f995e 485 rq->mq_hctx = NULL;
12f5b931 486 if (rq->tag != -1)
cae740a0 487 blk_mq_put_tag(hctx->tags, ctx, rq->tag);
12f5b931 488 if (sched_tag != -1)
cae740a0 489 blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag);
12f5b931
KB
490 blk_mq_sched_restart(hctx);
491 blk_queue_exit(q);
492}
493
6af54051 494void blk_mq_free_request(struct request *rq)
320ae51f 495{
320ae51f 496 struct request_queue *q = rq->q;
6af54051
CH
497 struct elevator_queue *e = q->elevator;
498 struct blk_mq_ctx *ctx = rq->mq_ctx;
ea4f995e 499 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
6af54051 500
5bbf4e5a 501 if (rq->rq_flags & RQF_ELVPRIV) {
f9cd4bfe
JA
502 if (e && e->type->ops.finish_request)
503 e->type->ops.finish_request(rq);
6af54051
CH
504 if (rq->elv.icq) {
505 put_io_context(rq->elv.icq->ioc);
506 rq->elv.icq = NULL;
507 }
508 }
320ae51f 509
6af54051 510 ctx->rq_completed[rq_is_sync(rq)]++;
e8064021 511 if (rq->rq_flags & RQF_MQ_INFLIGHT)
0d2602ca 512 atomic_dec(&hctx->nr_active);
87760e5e 513
7beb2f84
JA
514 if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
515 laptop_io_completion(q->backing_dev_info);
516
a7905043 517 rq_qos_done(q, rq);
0d2602ca 518
12f5b931
KB
519 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
520 if (refcount_dec_and_test(&rq->ref))
521 __blk_mq_free_request(rq);
320ae51f 522}
1a3b595a 523EXPORT_SYMBOL_GPL(blk_mq_free_request);
320ae51f 524
2a842aca 525inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
320ae51f 526{
fe1f4526
JA
527 u64 now = 0;
528
529 if (blk_mq_need_time_stamp(rq))
530 now = ktime_get_ns();
522a7775 531
4bc6339a
OS
532 if (rq->rq_flags & RQF_STATS) {
533 blk_mq_poll_stats_start(rq->q);
522a7775 534 blk_stat_add(rq, now);
4bc6339a
OS
535 }
536
ed88660a
OS
537 if (rq->internal_tag != -1)
538 blk_mq_sched_completed_request(rq, now);
539
522a7775 540 blk_account_io_done(rq, now);
0d11e6ac 541
91b63639 542 if (rq->end_io) {
a7905043 543 rq_qos_done(rq->q, rq);
320ae51f 544 rq->end_io(rq, error);
91b63639 545 } else {
320ae51f 546 blk_mq_free_request(rq);
91b63639 547 }
320ae51f 548}
c8a446ad 549EXPORT_SYMBOL(__blk_mq_end_request);
63151a44 550
2a842aca 551void blk_mq_end_request(struct request *rq, blk_status_t error)
63151a44
CH
552{
553 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
554 BUG();
c8a446ad 555 __blk_mq_end_request(rq, error);
63151a44 556}
c8a446ad 557EXPORT_SYMBOL(blk_mq_end_request);
320ae51f 558
30a91cb4 559static void __blk_mq_complete_request_remote(void *data)
320ae51f 560{
3d6efbf6 561 struct request *rq = data;
c7bb9ad1 562 struct request_queue *q = rq->q;
320ae51f 563
c7bb9ad1 564 q->mq_ops->complete(rq);
320ae51f 565}
320ae51f 566
7b11eab0
KB
567/**
568 * blk_mq_force_complete_rq() - Force complete the request, bypassing any error
569 * injection that could drop the completion.
570 * @rq: Request to be force completed
571 *
572 * Drivers should use blk_mq_complete_request() to complete requests in their
573 * normal IO path. For timeout error recovery, drivers may call this forced
574 * completion routine after they've reclaimed timed out requests to bypass
575 * potentially subsequent fake timeouts.
576 */
577void blk_mq_force_complete_rq(struct request *rq)
320ae51f
JA
578{
579 struct blk_mq_ctx *ctx = rq->mq_ctx;
c7bb9ad1 580 struct request_queue *q = rq->q;
38535201 581 bool shared = false;
320ae51f
JA
582 int cpu;
583
af78ff7c 584 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
36e76539
ML
585 /*
586 * Most of single queue controllers, there is only one irq vector
587 * for handling IO completion, and the only irq's affinity is set
588 * as all possible CPUs. On most of ARCHs, this affinity means the
589 * irq is handled on one specific CPU.
590 *
591 * So complete IO reqeust in softirq context in case of single queue
592 * for not degrading IO performance by irqsoff latency.
593 */
c7bb9ad1 594 if (q->nr_hw_queues == 1) {
36e76539
ML
595 __blk_complete_request(rq);
596 return;
597 }
598
4ab32bf3
JA
599 /*
600 * For a polled request, always complete locallly, it's pointless
601 * to redirect the completion.
602 */
603 if ((rq->cmd_flags & REQ_HIPRI) ||
604 !test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags)) {
c7bb9ad1 605 q->mq_ops->complete(rq);
30a91cb4
CH
606 return;
607 }
320ae51f
JA
608
609 cpu = get_cpu();
c7bb9ad1 610 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags))
38535201
CH
611 shared = cpus_share_cache(cpu, ctx->cpu);
612
613 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
30a91cb4 614 rq->csd.func = __blk_mq_complete_request_remote;
3d6efbf6
CH
615 rq->csd.info = rq;
616 rq->csd.flags = 0;
c46fff2a 617 smp_call_function_single_async(ctx->cpu, &rq->csd);
3d6efbf6 618 } else {
c7bb9ad1 619 q->mq_ops->complete(rq);
3d6efbf6 620 }
320ae51f
JA
621 put_cpu();
622}
7b11eab0 623EXPORT_SYMBOL_GPL(blk_mq_force_complete_rq);
30a91cb4 624
04ced159 625static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
b7435db8 626 __releases(hctx->srcu)
04ced159
JA
627{
628 if (!(hctx->flags & BLK_MQ_F_BLOCKING))
629 rcu_read_unlock();
630 else
05707b64 631 srcu_read_unlock(hctx->srcu, srcu_idx);
04ced159
JA
632}
633
634static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
b7435db8 635 __acquires(hctx->srcu)
04ced159 636{
08b5a6e2
JA
637 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
638 /* shut up gcc false positive */
639 *srcu_idx = 0;
04ced159 640 rcu_read_lock();
08b5a6e2 641 } else
05707b64 642 *srcu_idx = srcu_read_lock(hctx->srcu);
04ced159
JA
643}
644
30a91cb4
CH
645/**
646 * blk_mq_complete_request - end I/O on a request
647 * @rq: the request being processed
648 *
649 * Description:
650 * Ends all I/O on a request. It does not handle partial completions.
651 * The actual completion happens out-of-order, through a IPI handler.
652 **/
16c15eb1 653bool blk_mq_complete_request(struct request *rq)
30a91cb4 654{
12f5b931 655 if (unlikely(blk_should_fake_timeout(rq->q)))
16c15eb1 656 return false;
7b11eab0 657 blk_mq_force_complete_rq(rq);
16c15eb1 658 return true;
30a91cb4
CH
659}
660EXPORT_SYMBOL(blk_mq_complete_request);
320ae51f 661
105663f7
AA
662/**
663 * blk_mq_start_request - Start processing a request
664 * @rq: Pointer to request to be started
665 *
666 * Function used by device drivers to notify the block layer that a request
667 * is going to be processed now, so blk layer can do proper initializations
668 * such as starting the timeout timer.
669 */
e2490073 670void blk_mq_start_request(struct request *rq)
320ae51f
JA
671{
672 struct request_queue *q = rq->q;
673
674 trace_block_rq_issue(q, rq);
675
cf43e6be 676 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
544ccc8d 677 rq->io_start_time_ns = ktime_get_ns();
3d244306 678 rq->stats_sectors = blk_rq_sectors(rq);
cf43e6be 679 rq->rq_flags |= RQF_STATS;
a7905043 680 rq_qos_issue(q, rq);
cf43e6be
JA
681 }
682
1d9bd516 683 WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
538b7534 684
1d9bd516 685 blk_add_timer(rq);
12f5b931 686 WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
49f5baa5 687
54d4e6ab
MG
688#ifdef CONFIG_BLK_DEV_INTEGRITY
689 if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE)
690 q->integrity.profile->prepare_fn(rq);
691#endif
320ae51f 692}
e2490073 693EXPORT_SYMBOL(blk_mq_start_request);
320ae51f 694
ed0791b2 695static void __blk_mq_requeue_request(struct request *rq)
320ae51f
JA
696{
697 struct request_queue *q = rq->q;
698
923218f6
ML
699 blk_mq_put_driver_tag(rq);
700
320ae51f 701 trace_block_rq_requeue(q, rq);
a7905043 702 rq_qos_requeue(q, rq);
49f5baa5 703
12f5b931
KB
704 if (blk_mq_request_started(rq)) {
705 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
da661267 706 rq->rq_flags &= ~RQF_TIMED_OUT;
e2490073 707 }
320ae51f
JA
708}
709
2b053aca 710void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
ed0791b2 711{
ed0791b2 712 __blk_mq_requeue_request(rq);
ed0791b2 713
105976f5
ML
714 /* this request will be re-inserted to io scheduler queue */
715 blk_mq_sched_requeue_request(rq);
716
7d692330 717 BUG_ON(!list_empty(&rq->queuelist));
2b053aca 718 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
ed0791b2
CH
719}
720EXPORT_SYMBOL(blk_mq_requeue_request);
721
6fca6a61
CH
722static void blk_mq_requeue_work(struct work_struct *work)
723{
724 struct request_queue *q =
2849450a 725 container_of(work, struct request_queue, requeue_work.work);
6fca6a61
CH
726 LIST_HEAD(rq_list);
727 struct request *rq, *next;
6fca6a61 728
18e9781d 729 spin_lock_irq(&q->requeue_lock);
6fca6a61 730 list_splice_init(&q->requeue_list, &rq_list);
18e9781d 731 spin_unlock_irq(&q->requeue_lock);
6fca6a61
CH
732
733 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
aef1897c 734 if (!(rq->rq_flags & (RQF_SOFTBARRIER | RQF_DONTPREP)))
6fca6a61
CH
735 continue;
736
e8064021 737 rq->rq_flags &= ~RQF_SOFTBARRIER;
6fca6a61 738 list_del_init(&rq->queuelist);
aef1897c
JW
739 /*
740 * If RQF_DONTPREP, rq has contained some driver specific
741 * data, so insert it to hctx dispatch list to avoid any
742 * merge.
743 */
744 if (rq->rq_flags & RQF_DONTPREP)
01e99aec 745 blk_mq_request_bypass_insert(rq, false, false);
aef1897c
JW
746 else
747 blk_mq_sched_insert_request(rq, true, false, false);
6fca6a61
CH
748 }
749
750 while (!list_empty(&rq_list)) {
751 rq = list_entry(rq_list.next, struct request, queuelist);
752 list_del_init(&rq->queuelist);
9e97d295 753 blk_mq_sched_insert_request(rq, false, false, false);
6fca6a61
CH
754 }
755
52d7f1b5 756 blk_mq_run_hw_queues(q, false);
6fca6a61
CH
757}
758
2b053aca
BVA
759void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
760 bool kick_requeue_list)
6fca6a61
CH
761{
762 struct request_queue *q = rq->q;
763 unsigned long flags;
764
765 /*
766 * We abuse this flag that is otherwise used by the I/O scheduler to
ff821d27 767 * request head insertion from the workqueue.
6fca6a61 768 */
e8064021 769 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
6fca6a61
CH
770
771 spin_lock_irqsave(&q->requeue_lock, flags);
772 if (at_head) {
e8064021 773 rq->rq_flags |= RQF_SOFTBARRIER;
6fca6a61
CH
774 list_add(&rq->queuelist, &q->requeue_list);
775 } else {
776 list_add_tail(&rq->queuelist, &q->requeue_list);
777 }
778 spin_unlock_irqrestore(&q->requeue_lock, flags);
2b053aca
BVA
779
780 if (kick_requeue_list)
781 blk_mq_kick_requeue_list(q);
6fca6a61 782}
6fca6a61
CH
783
784void blk_mq_kick_requeue_list(struct request_queue *q)
785{
ae943d20 786 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
6fca6a61
CH
787}
788EXPORT_SYMBOL(blk_mq_kick_requeue_list);
789
2849450a
MS
790void blk_mq_delay_kick_requeue_list(struct request_queue *q,
791 unsigned long msecs)
792{
d4acf365
BVA
793 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
794 msecs_to_jiffies(msecs));
2849450a
MS
795}
796EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
797
0e62f51f
JA
798struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
799{
88c7b2b7
JA
800 if (tag < tags->nr_tags) {
801 prefetch(tags->rqs[tag]);
4ee86bab 802 return tags->rqs[tag];
88c7b2b7 803 }
4ee86bab
HR
804
805 return NULL;
24d2f903
CH
806}
807EXPORT_SYMBOL(blk_mq_tag_to_rq);
808
3c94d83c
JA
809static bool blk_mq_rq_inflight(struct blk_mq_hw_ctx *hctx, struct request *rq,
810 void *priv, bool reserved)
ae879912
JA
811{
812 /*
3c94d83c
JA
813 * If we find a request that is inflight and the queue matches,
814 * we know the queue is busy. Return false to stop the iteration.
ae879912 815 */
3c94d83c 816 if (rq->state == MQ_RQ_IN_FLIGHT && rq->q == hctx->queue) {
ae879912
JA
817 bool *busy = priv;
818
819 *busy = true;
820 return false;
821 }
822
823 return true;
824}
825
3c94d83c 826bool blk_mq_queue_inflight(struct request_queue *q)
ae879912
JA
827{
828 bool busy = false;
829
3c94d83c 830 blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy);
ae879912
JA
831 return busy;
832}
3c94d83c 833EXPORT_SYMBOL_GPL(blk_mq_queue_inflight);
ae879912 834
358f70da 835static void blk_mq_rq_timed_out(struct request *req, bool reserved)
320ae51f 836{
da661267 837 req->rq_flags |= RQF_TIMED_OUT;
d1210d5a
CH
838 if (req->q->mq_ops->timeout) {
839 enum blk_eh_timer_return ret;
840
841 ret = req->q->mq_ops->timeout(req, reserved);
842 if (ret == BLK_EH_DONE)
843 return;
844 WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
46f92d42 845 }
d1210d5a
CH
846
847 blk_add_timer(req);
87ee7b11 848}
5b3f25fc 849
12f5b931 850static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
81481eb4 851{
12f5b931 852 unsigned long deadline;
87ee7b11 853
12f5b931
KB
854 if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
855 return false;
da661267
CH
856 if (rq->rq_flags & RQF_TIMED_OUT)
857 return false;
a7af0af3 858
079076b3 859 deadline = READ_ONCE(rq->deadline);
12f5b931
KB
860 if (time_after_eq(jiffies, deadline))
861 return true;
a7af0af3 862
12f5b931
KB
863 if (*next == 0)
864 *next = deadline;
865 else if (time_after(*next, deadline))
866 *next = deadline;
867 return false;
87ee7b11
JA
868}
869
7baa8572 870static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
1d9bd516
TH
871 struct request *rq, void *priv, bool reserved)
872{
12f5b931
KB
873 unsigned long *next = priv;
874
875 /*
876 * Just do a quick check if it is expired before locking the request in
877 * so we're not unnecessarilly synchronizing across CPUs.
878 */
879 if (!blk_mq_req_expired(rq, next))
7baa8572 880 return true;
12f5b931
KB
881
882 /*
883 * We have reason to believe the request may be expired. Take a
884 * reference on the request to lock this request lifetime into its
885 * currently allocated context to prevent it from being reallocated in
886 * the event the completion by-passes this timeout handler.
887 *
888 * If the reference was already released, then the driver beat the
889 * timeout handler to posting a natural completion.
890 */
891 if (!refcount_inc_not_zero(&rq->ref))
7baa8572 892 return true;
12f5b931 893
1d9bd516 894 /*
12f5b931
KB
895 * The request is now locked and cannot be reallocated underneath the
896 * timeout handler's processing. Re-verify this exact request is truly
897 * expired; if it is not expired, then the request was completed and
898 * reallocated as a new request.
1d9bd516 899 */
12f5b931 900 if (blk_mq_req_expired(rq, next))
1d9bd516 901 blk_mq_rq_timed_out(rq, reserved);
8d699663
YY
902
903 if (is_flush_rq(rq, hctx))
904 rq->end_io(rq, 0);
905 else if (refcount_dec_and_test(&rq->ref))
12f5b931 906 __blk_mq_free_request(rq);
7baa8572
JA
907
908 return true;
1d9bd516
TH
909}
910
287922eb 911static void blk_mq_timeout_work(struct work_struct *work)
320ae51f 912{
287922eb
CH
913 struct request_queue *q =
914 container_of(work, struct request_queue, timeout_work);
12f5b931 915 unsigned long next = 0;
1d9bd516 916 struct blk_mq_hw_ctx *hctx;
81481eb4 917 int i;
320ae51f 918
71f79fb3
GKB
919 /* A deadlock might occur if a request is stuck requiring a
920 * timeout at the same time a queue freeze is waiting
921 * completion, since the timeout code would not be able to
922 * acquire the queue reference here.
923 *
924 * That's why we don't use blk_queue_enter here; instead, we use
925 * percpu_ref_tryget directly, because we need to be able to
926 * obtain a reference even in the short window between the queue
927 * starting to freeze, by dropping the first reference in
1671d522 928 * blk_freeze_queue_start, and the moment the last request is
71f79fb3
GKB
929 * consumed, marked by the instant q_usage_counter reaches
930 * zero.
931 */
932 if (!percpu_ref_tryget(&q->q_usage_counter))
287922eb
CH
933 return;
934
12f5b931 935 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
320ae51f 936
12f5b931
KB
937 if (next != 0) {
938 mod_timer(&q->timeout, next);
0d2602ca 939 } else {
fcd36c36
BVA
940 /*
941 * Request timeouts are handled as a forward rolling timer. If
942 * we end up here it means that no requests are pending and
943 * also that no request has been pending for a while. Mark
944 * each hctx as idle.
945 */
f054b56c
ML
946 queue_for_each_hw_ctx(q, hctx, i) {
947 /* the hctx may be unmapped, so check it here */
948 if (blk_mq_hw_queue_mapped(hctx))
949 blk_mq_tag_idle(hctx);
950 }
0d2602ca 951 }
287922eb 952 blk_queue_exit(q);
320ae51f
JA
953}
954
88459642
OS
955struct flush_busy_ctx_data {
956 struct blk_mq_hw_ctx *hctx;
957 struct list_head *list;
958};
959
960static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
961{
962 struct flush_busy_ctx_data *flush_data = data;
963 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
964 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
c16d6b5a 965 enum hctx_type type = hctx->type;
88459642 966
88459642 967 spin_lock(&ctx->lock);
c16d6b5a 968 list_splice_tail_init(&ctx->rq_lists[type], flush_data->list);
e9a99a63 969 sbitmap_clear_bit(sb, bitnr);
88459642
OS
970 spin_unlock(&ctx->lock);
971 return true;
972}
973
1429d7c9
JA
974/*
975 * Process software queues that have been marked busy, splicing them
976 * to the for-dispatch
977 */
2c3ad667 978void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
1429d7c9 979{
88459642
OS
980 struct flush_busy_ctx_data data = {
981 .hctx = hctx,
982 .list = list,
983 };
1429d7c9 984
88459642 985 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
1429d7c9 986}
2c3ad667 987EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
1429d7c9 988
b347689f
ML
989struct dispatch_rq_data {
990 struct blk_mq_hw_ctx *hctx;
991 struct request *rq;
992};
993
994static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
995 void *data)
996{
997 struct dispatch_rq_data *dispatch_data = data;
998 struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
999 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
c16d6b5a 1000 enum hctx_type type = hctx->type;
b347689f
ML
1001
1002 spin_lock(&ctx->lock);
c16d6b5a
ML
1003 if (!list_empty(&ctx->rq_lists[type])) {
1004 dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next);
b347689f 1005 list_del_init(&dispatch_data->rq->queuelist);
c16d6b5a 1006 if (list_empty(&ctx->rq_lists[type]))
b347689f
ML
1007 sbitmap_clear_bit(sb, bitnr);
1008 }
1009 spin_unlock(&ctx->lock);
1010
1011 return !dispatch_data->rq;
1012}
1013
1014struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
1015 struct blk_mq_ctx *start)
1016{
f31967f0 1017 unsigned off = start ? start->index_hw[hctx->type] : 0;
b347689f
ML
1018 struct dispatch_rq_data data = {
1019 .hctx = hctx,
1020 .rq = NULL,
1021 };
1022
1023 __sbitmap_for_each_set(&hctx->ctx_map, off,
1024 dispatch_rq_from_ctx, &data);
1025
1026 return data.rq;
1027}
1028
703fd1c0
JA
1029static inline unsigned int queued_to_index(unsigned int queued)
1030{
1031 if (!queued)
1032 return 0;
1429d7c9 1033
703fd1c0 1034 return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
1429d7c9
JA
1035}
1036
8ab6bb9e 1037bool blk_mq_get_driver_tag(struct request *rq)
bd166ef1
JA
1038{
1039 struct blk_mq_alloc_data data = {
1040 .q = rq->q,
ea4f995e 1041 .hctx = rq->mq_hctx,
8ab6bb9e 1042 .flags = BLK_MQ_REQ_NOWAIT,
f9afca4d 1043 .cmd_flags = rq->cmd_flags,
bd166ef1 1044 };
d263ed99 1045 bool shared;
5feeacdd 1046
81380ca1 1047 if (rq->tag != -1)
1fead718 1048 return true;
bd166ef1 1049
415b806d
SG
1050 if (blk_mq_tag_is_reserved(data.hctx->sched_tags, rq->internal_tag))
1051 data.flags |= BLK_MQ_REQ_RESERVED;
1052
d263ed99 1053 shared = blk_mq_tag_busy(data.hctx);
bd166ef1
JA
1054 rq->tag = blk_mq_get_tag(&data);
1055 if (rq->tag >= 0) {
d263ed99 1056 if (shared) {
200e86b3
JA
1057 rq->rq_flags |= RQF_MQ_INFLIGHT;
1058 atomic_inc(&data.hctx->nr_active);
1059 }
bd166ef1 1060 data.hctx->tags->rqs[rq->tag] = rq;
bd166ef1
JA
1061 }
1062
81380ca1 1063 return rq->tag != -1;
bd166ef1
JA
1064}
1065
eb619fdb
JA
1066static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
1067 int flags, void *key)
da55f2cc
OS
1068{
1069 struct blk_mq_hw_ctx *hctx;
1070
1071 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
1072
5815839b 1073 spin_lock(&hctx->dispatch_wait_lock);
e8618575
JA
1074 if (!list_empty(&wait->entry)) {
1075 struct sbitmap_queue *sbq;
1076
1077 list_del_init(&wait->entry);
1078 sbq = &hctx->tags->bitmap_tags;
1079 atomic_dec(&sbq->ws_active);
1080 }
5815839b
ML
1081 spin_unlock(&hctx->dispatch_wait_lock);
1082
da55f2cc
OS
1083 blk_mq_run_hw_queue(hctx, true);
1084 return 1;
1085}
1086
f906a6a0
JA
1087/*
1088 * Mark us waiting for a tag. For shared tags, this involves hooking us into
ee3e4de5
BVA
1089 * the tag wakeups. For non-shared tags, we can simply mark us needing a
1090 * restart. For both cases, take care to check the condition again after
f906a6a0
JA
1091 * marking us as waiting.
1092 */
2278d69f 1093static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
f906a6a0 1094 struct request *rq)
da55f2cc 1095{
e8618575 1096 struct sbitmap_queue *sbq = &hctx->tags->bitmap_tags;
5815839b 1097 struct wait_queue_head *wq;
f906a6a0
JA
1098 wait_queue_entry_t *wait;
1099 bool ret;
da55f2cc 1100
2278d69f 1101 if (!(hctx->flags & BLK_MQ_F_TAG_SHARED)) {
684b7324 1102 blk_mq_sched_mark_restart_hctx(hctx);
f906a6a0 1103
c27d53fb
BVA
1104 /*
1105 * It's possible that a tag was freed in the window between the
1106 * allocation failure and adding the hardware queue to the wait
1107 * queue.
1108 *
1109 * Don't clear RESTART here, someone else could have set it.
1110 * At most this will cost an extra queue run.
1111 */
8ab6bb9e 1112 return blk_mq_get_driver_tag(rq);
eb619fdb 1113 }
eb619fdb 1114
2278d69f 1115 wait = &hctx->dispatch_wait;
c27d53fb
BVA
1116 if (!list_empty_careful(&wait->entry))
1117 return false;
1118
e8618575 1119 wq = &bt_wait_ptr(sbq, hctx)->wait;
5815839b
ML
1120
1121 spin_lock_irq(&wq->lock);
1122 spin_lock(&hctx->dispatch_wait_lock);
c27d53fb 1123 if (!list_empty(&wait->entry)) {
5815839b
ML
1124 spin_unlock(&hctx->dispatch_wait_lock);
1125 spin_unlock_irq(&wq->lock);
c27d53fb 1126 return false;
eb619fdb
JA
1127 }
1128
e8618575 1129 atomic_inc(&sbq->ws_active);
5815839b
ML
1130 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
1131 __add_wait_queue(wq, wait);
c27d53fb 1132
da55f2cc 1133 /*
eb619fdb
JA
1134 * It's possible that a tag was freed in the window between the
1135 * allocation failure and adding the hardware queue to the wait
1136 * queue.
da55f2cc 1137 */
8ab6bb9e 1138 ret = blk_mq_get_driver_tag(rq);
c27d53fb 1139 if (!ret) {
5815839b
ML
1140 spin_unlock(&hctx->dispatch_wait_lock);
1141 spin_unlock_irq(&wq->lock);
c27d53fb 1142 return false;
eb619fdb 1143 }
c27d53fb
BVA
1144
1145 /*
1146 * We got a tag, remove ourselves from the wait queue to ensure
1147 * someone else gets the wakeup.
1148 */
c27d53fb 1149 list_del_init(&wait->entry);
e8618575 1150 atomic_dec(&sbq->ws_active);
5815839b
ML
1151 spin_unlock(&hctx->dispatch_wait_lock);
1152 spin_unlock_irq(&wq->lock);
c27d53fb
BVA
1153
1154 return true;
da55f2cc
OS
1155}
1156
6e768717
ML
1157#define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
1158#define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
1159/*
1160 * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
1161 * - EWMA is one simple way to compute running average value
1162 * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
1163 * - take 4 as factor for avoiding to get too small(0) result, and this
1164 * factor doesn't matter because EWMA decreases exponentially
1165 */
1166static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
1167{
1168 unsigned int ewma;
1169
1170 if (hctx->queue->elevator)
1171 return;
1172
1173 ewma = hctx->dispatch_busy;
1174
1175 if (!ewma && !busy)
1176 return;
1177
1178 ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
1179 if (busy)
1180 ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
1181 ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
1182
1183 hctx->dispatch_busy = ewma;
1184}
1185
86ff7c2a
ML
1186#define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
1187
c92a4103
JT
1188static void blk_mq_handle_dev_resource(struct request *rq,
1189 struct list_head *list)
1190{
1191 struct request *next =
1192 list_first_entry_or_null(list, struct request, queuelist);
1193
1194 /*
1195 * If an I/O scheduler has been configured and we got a driver tag for
1196 * the next request already, free it.
1197 */
1198 if (next)
1199 blk_mq_put_driver_tag(next);
1200
1201 list_add(&rq->queuelist, list);
1202 __blk_mq_requeue_request(rq);
1203}
1204
0512a75b
KB
1205static void blk_mq_handle_zone_resource(struct request *rq,
1206 struct list_head *zone_list)
1207{
1208 /*
1209 * If we end up here it is because we cannot dispatch a request to a
1210 * specific zone due to LLD level zone-write locking or other zone
1211 * related resource not being available. In this case, set the request
1212 * aside in zone_list for retrying it later.
1213 */
1214 list_add(&rq->queuelist, zone_list);
1215 __blk_mq_requeue_request(rq);
1216}
1217
1f57f8d4
JA
1218/*
1219 * Returns true if we did some work AND can potentially do more.
1220 */
de148297 1221bool blk_mq_dispatch_rq_list(struct request_queue *q, struct list_head *list,
eb619fdb 1222 bool got_budget)
320ae51f 1223{
81380ca1 1224 struct blk_mq_hw_ctx *hctx;
6d6f167c 1225 struct request *rq, *nxt;
eb619fdb 1226 bool no_tag = false;
fc17b653 1227 int errors, queued;
86ff7c2a 1228 blk_status_t ret = BLK_STS_OK;
ab3cee37 1229 bool no_budget_avail = false;
0512a75b 1230 LIST_HEAD(zone_list);
320ae51f 1231
81380ca1
OS
1232 if (list_empty(list))
1233 return false;
1234
de148297
ML
1235 WARN_ON(!list_is_singular(list) && got_budget);
1236
320ae51f
JA
1237 /*
1238 * Now process all the entries, sending them to the driver.
1239 */
93efe981 1240 errors = queued = 0;
81380ca1 1241 do {
74c45052 1242 struct blk_mq_queue_data bd;
320ae51f 1243
f04c3df3 1244 rq = list_first_entry(list, struct request, queuelist);
0bca799b 1245
ea4f995e 1246 hctx = rq->mq_hctx;
5fe56de7
JG
1247 if (!got_budget && !blk_mq_get_dispatch_budget(hctx)) {
1248 blk_mq_put_driver_tag(rq);
ab3cee37 1249 no_budget_avail = true;
0bca799b 1250 break;
5fe56de7 1251 }
0bca799b 1252
8ab6bb9e 1253 if (!blk_mq_get_driver_tag(rq)) {
3c782d67 1254 /*
da55f2cc 1255 * The initial allocation attempt failed, so we need to
eb619fdb
JA
1256 * rerun the hardware queue when a tag is freed. The
1257 * waitqueue takes care of that. If the queue is run
1258 * before we add this entry back on the dispatch list,
1259 * we'll re-run it below.
3c782d67 1260 */
2278d69f 1261 if (!blk_mq_mark_tag_wait(hctx, rq)) {
0bca799b 1262 blk_mq_put_dispatch_budget(hctx);
f906a6a0
JA
1263 /*
1264 * For non-shared tags, the RESTART check
1265 * will suffice.
1266 */
1267 if (hctx->flags & BLK_MQ_F_TAG_SHARED)
1268 no_tag = true;
de148297
ML
1269 break;
1270 }
1271 }
1272
320ae51f 1273 list_del_init(&rq->queuelist);
320ae51f 1274
74c45052 1275 bd.rq = rq;
113285b4
JA
1276
1277 /*
1278 * Flag last if we have no more requests, or if we have more
1279 * but can't assign a driver tag to it.
1280 */
1281 if (list_empty(list))
1282 bd.last = true;
1283 else {
113285b4 1284 nxt = list_first_entry(list, struct request, queuelist);
8ab6bb9e 1285 bd.last = !blk_mq_get_driver_tag(nxt);
113285b4 1286 }
74c45052
JA
1287
1288 ret = q->mq_ops->queue_rq(hctx, &bd);
86ff7c2a 1289 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) {
c92a4103 1290 blk_mq_handle_dev_resource(rq, list);
320ae51f 1291 break;
0512a75b
KB
1292 } else if (ret == BLK_STS_ZONE_RESOURCE) {
1293 /*
1294 * Move the request to zone_list and keep going through
1295 * the dispatch list to find more requests the drive can
1296 * accept.
1297 */
1298 blk_mq_handle_zone_resource(rq, &zone_list);
1299 if (list_empty(list))
1300 break;
1301 continue;
fc17b653
CH
1302 }
1303
1304 if (unlikely(ret != BLK_STS_OK)) {
93efe981 1305 errors++;
2a842aca 1306 blk_mq_end_request(rq, BLK_STS_IOERR);
fc17b653 1307 continue;
320ae51f
JA
1308 }
1309
fc17b653 1310 queued++;
81380ca1 1311 } while (!list_empty(list));
320ae51f 1312
0512a75b
KB
1313 if (!list_empty(&zone_list))
1314 list_splice_tail_init(&zone_list, list);
1315
703fd1c0 1316 hctx->dispatched[queued_to_index(queued)]++;
320ae51f
JA
1317
1318 /*
1319 * Any items that need requeuing? Stuff them into hctx->dispatch,
1320 * that is where we will continue on next queue run.
1321 */
f04c3df3 1322 if (!list_empty(list)) {
86ff7c2a
ML
1323 bool needs_restart;
1324
d666ba98
JA
1325 /*
1326 * If we didn't flush the entire list, we could have told
1327 * the driver there was more coming, but that turned out to
1328 * be a lie.
1329 */
536167d4 1330 if (q->mq_ops->commit_rqs && queued)
d666ba98
JA
1331 q->mq_ops->commit_rqs(hctx);
1332
320ae51f 1333 spin_lock(&hctx->lock);
01e99aec 1334 list_splice_tail_init(list, &hctx->dispatch);
320ae51f 1335 spin_unlock(&hctx->lock);
f04c3df3 1336
9ba52e58 1337 /*
710c785f
BVA
1338 * If SCHED_RESTART was set by the caller of this function and
1339 * it is no longer set that means that it was cleared by another
1340 * thread and hence that a queue rerun is needed.
9ba52e58 1341 *
eb619fdb
JA
1342 * If 'no_tag' is set, that means that we failed getting
1343 * a driver tag with an I/O scheduler attached. If our dispatch
1344 * waitqueue is no longer active, ensure that we run the queue
1345 * AFTER adding our entries back to the list.
bd166ef1 1346 *
710c785f
BVA
1347 * If no I/O scheduler has been configured it is possible that
1348 * the hardware queue got stopped and restarted before requests
1349 * were pushed back onto the dispatch list. Rerun the queue to
1350 * avoid starvation. Notes:
1351 * - blk_mq_run_hw_queue() checks whether or not a queue has
1352 * been stopped before rerunning a queue.
1353 * - Some but not all block drivers stop a queue before
fc17b653 1354 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
710c785f 1355 * and dm-rq.
86ff7c2a
ML
1356 *
1357 * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
1358 * bit is set, run queue after a delay to avoid IO stalls
ab3cee37
DA
1359 * that could otherwise occur if the queue is idle. We'll do
1360 * similar if we couldn't get budget and SCHED_RESTART is set.
bd166ef1 1361 */
86ff7c2a
ML
1362 needs_restart = blk_mq_sched_needs_restart(hctx);
1363 if (!needs_restart ||
eb619fdb 1364 (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
bd166ef1 1365 blk_mq_run_hw_queue(hctx, true);
ab3cee37
DA
1366 else if (needs_restart && (ret == BLK_STS_RESOURCE ||
1367 no_budget_avail))
86ff7c2a 1368 blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
1f57f8d4 1369
6e768717 1370 blk_mq_update_dispatch_busy(hctx, true);
1f57f8d4 1371 return false;
6e768717
ML
1372 } else
1373 blk_mq_update_dispatch_busy(hctx, false);
f04c3df3 1374
1f57f8d4
JA
1375 /*
1376 * If the host/device is unable to accept more work, inform the
1377 * caller of that.
1378 */
1379 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
1380 return false;
1381
93efe981 1382 return (queued + errors) != 0;
f04c3df3
JA
1383}
1384
105663f7
AA
1385/**
1386 * __blk_mq_run_hw_queue - Run a hardware queue.
1387 * @hctx: Pointer to the hardware queue to run.
1388 *
1389 * Send pending requests to the hardware.
1390 */
6a83e74d
BVA
1391static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
1392{
1393 int srcu_idx;
1394
b7a71e66
JA
1395 /*
1396 * We should be running this queue from one of the CPUs that
1397 * are mapped to it.
7df938fb
ML
1398 *
1399 * There are at least two related races now between setting
1400 * hctx->next_cpu from blk_mq_hctx_next_cpu() and running
1401 * __blk_mq_run_hw_queue():
1402 *
1403 * - hctx->next_cpu is found offline in blk_mq_hctx_next_cpu(),
1404 * but later it becomes online, then this warning is harmless
1405 * at all
1406 *
1407 * - hctx->next_cpu is found online in blk_mq_hctx_next_cpu(),
1408 * but later it becomes offline, then the warning can't be
1409 * triggered, and we depend on blk-mq timeout handler to
1410 * handle dispatched requests to this hctx
b7a71e66 1411 */
7df938fb
ML
1412 if (!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
1413 cpu_online(hctx->next_cpu)) {
1414 printk(KERN_WARNING "run queue from wrong CPU %d, hctx %s\n",
1415 raw_smp_processor_id(),
1416 cpumask_empty(hctx->cpumask) ? "inactive": "active");
1417 dump_stack();
1418 }
6a83e74d 1419
b7a71e66
JA
1420 /*
1421 * We can't run the queue inline with ints disabled. Ensure that
1422 * we catch bad users of this early.
1423 */
1424 WARN_ON_ONCE(in_interrupt());
1425
04ced159 1426 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
bf4907c0 1427
04ced159
JA
1428 hctx_lock(hctx, &srcu_idx);
1429 blk_mq_sched_dispatch_requests(hctx);
1430 hctx_unlock(hctx, srcu_idx);
6a83e74d
BVA
1431}
1432
f82ddf19
ML
1433static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
1434{
1435 int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
1436
1437 if (cpu >= nr_cpu_ids)
1438 cpu = cpumask_first(hctx->cpumask);
1439 return cpu;
1440}
1441
506e931f
JA
1442/*
1443 * It'd be great if the workqueue API had a way to pass
1444 * in a mask and had some smarts for more clever placement.
1445 * For now we just round-robin here, switching for every
1446 * BLK_MQ_CPU_WORK_BATCH queued items.
1447 */
1448static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
1449{
7bed4595 1450 bool tried = false;
476f8c98 1451 int next_cpu = hctx->next_cpu;
7bed4595 1452
b657d7e6
CH
1453 if (hctx->queue->nr_hw_queues == 1)
1454 return WORK_CPU_UNBOUND;
506e931f
JA
1455
1456 if (--hctx->next_cpu_batch <= 0) {
7bed4595 1457select_cpu:
476f8c98 1458 next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
20e4d813 1459 cpu_online_mask);
506e931f 1460 if (next_cpu >= nr_cpu_ids)
f82ddf19 1461 next_cpu = blk_mq_first_mapped_cpu(hctx);
506e931f
JA
1462 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1463 }
1464
7bed4595
ML
1465 /*
1466 * Do unbound schedule if we can't find a online CPU for this hctx,
1467 * and it should only happen in the path of handling CPU DEAD.
1468 */
476f8c98 1469 if (!cpu_online(next_cpu)) {
7bed4595
ML
1470 if (!tried) {
1471 tried = true;
1472 goto select_cpu;
1473 }
1474
1475 /*
1476 * Make sure to re-select CPU next time once after CPUs
1477 * in hctx->cpumask become online again.
1478 */
476f8c98 1479 hctx->next_cpu = next_cpu;
7bed4595
ML
1480 hctx->next_cpu_batch = 1;
1481 return WORK_CPU_UNBOUND;
1482 }
476f8c98
ML
1483
1484 hctx->next_cpu = next_cpu;
1485 return next_cpu;
506e931f
JA
1486}
1487
105663f7
AA
1488/**
1489 * __blk_mq_delay_run_hw_queue - Run (or schedule to run) a hardware queue.
1490 * @hctx: Pointer to the hardware queue to run.
1491 * @async: If we want to run the queue asynchronously.
1492 * @msecs: Microseconds of delay to wait before running the queue.
1493 *
1494 * If !@async, try to run the queue now. Else, run the queue asynchronously and
1495 * with a delay of @msecs.
1496 */
7587a5ae
BVA
1497static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
1498 unsigned long msecs)
320ae51f 1499{
5435c023 1500 if (unlikely(blk_mq_hctx_stopped(hctx)))
320ae51f
JA
1501 return;
1502
1b792f2f 1503 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
2a90d4aa
PB
1504 int cpu = get_cpu();
1505 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
398205b8 1506 __blk_mq_run_hw_queue(hctx);
2a90d4aa 1507 put_cpu();
398205b8
PB
1508 return;
1509 }
e4043dcf 1510
2a90d4aa 1511 put_cpu();
e4043dcf 1512 }
398205b8 1513
ae943d20
BVA
1514 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
1515 msecs_to_jiffies(msecs));
7587a5ae
BVA
1516}
1517
105663f7
AA
1518/**
1519 * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously.
1520 * @hctx: Pointer to the hardware queue to run.
1521 * @msecs: Microseconds of delay to wait before running the queue.
1522 *
1523 * Run a hardware queue asynchronously with a delay of @msecs.
1524 */
7587a5ae
BVA
1525void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1526{
1527 __blk_mq_delay_run_hw_queue(hctx, true, msecs);
1528}
1529EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
1530
105663f7
AA
1531/**
1532 * blk_mq_run_hw_queue - Start to run a hardware queue.
1533 * @hctx: Pointer to the hardware queue to run.
1534 * @async: If we want to run the queue asynchronously.
1535 *
1536 * Check if the request queue is not in a quiesced state and if there are
1537 * pending requests to be sent. If this is true, run the queue to send requests
1538 * to hardware.
1539 */
626fb735 1540void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
7587a5ae 1541{
24f5a90f
ML
1542 int srcu_idx;
1543 bool need_run;
1544
1545 /*
1546 * When queue is quiesced, we may be switching io scheduler, or
1547 * updating nr_hw_queues, or other things, and we can't run queue
1548 * any more, even __blk_mq_hctx_has_pending() can't be called safely.
1549 *
1550 * And queue will be rerun in blk_mq_unquiesce_queue() if it is
1551 * quiesced.
1552 */
04ced159
JA
1553 hctx_lock(hctx, &srcu_idx);
1554 need_run = !blk_queue_quiesced(hctx->queue) &&
1555 blk_mq_hctx_has_pending(hctx);
1556 hctx_unlock(hctx, srcu_idx);
24f5a90f 1557
626fb735 1558 if (need_run)
79f720a7 1559 __blk_mq_delay_run_hw_queue(hctx, async, 0);
320ae51f 1560}
5b727272 1561EXPORT_SYMBOL(blk_mq_run_hw_queue);
320ae51f 1562
105663f7
AA
1563/**
1564 * blk_mq_run_hw_queue - Run all hardware queues in a request queue.
1565 * @q: Pointer to the request queue to run.
1566 * @async: If we want to run the queue asynchronously.
1567 */
b94ec296 1568void blk_mq_run_hw_queues(struct request_queue *q, bool async)
320ae51f
JA
1569{
1570 struct blk_mq_hw_ctx *hctx;
1571 int i;
1572
1573 queue_for_each_hw_ctx(q, hctx, i) {
79f720a7 1574 if (blk_mq_hctx_stopped(hctx))
320ae51f
JA
1575 continue;
1576
b94ec296 1577 blk_mq_run_hw_queue(hctx, async);
320ae51f
JA
1578 }
1579}
b94ec296 1580EXPORT_SYMBOL(blk_mq_run_hw_queues);
320ae51f 1581
b9151e7b
DA
1582/**
1583 * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously.
1584 * @q: Pointer to the request queue to run.
1585 * @msecs: Microseconds of delay to wait before running the queues.
1586 */
1587void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs)
1588{
1589 struct blk_mq_hw_ctx *hctx;
1590 int i;
1591
1592 queue_for_each_hw_ctx(q, hctx, i) {
1593 if (blk_mq_hctx_stopped(hctx))
1594 continue;
1595
1596 blk_mq_delay_run_hw_queue(hctx, msecs);
1597 }
1598}
1599EXPORT_SYMBOL(blk_mq_delay_run_hw_queues);
1600
fd001443
BVA
1601/**
1602 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
1603 * @q: request queue.
1604 *
1605 * The caller is responsible for serializing this function against
1606 * blk_mq_{start,stop}_hw_queue().
1607 */
1608bool blk_mq_queue_stopped(struct request_queue *q)
1609{
1610 struct blk_mq_hw_ctx *hctx;
1611 int i;
1612
1613 queue_for_each_hw_ctx(q, hctx, i)
1614 if (blk_mq_hctx_stopped(hctx))
1615 return true;
1616
1617 return false;
1618}
1619EXPORT_SYMBOL(blk_mq_queue_stopped);
1620
39a70c76
ML
1621/*
1622 * This function is often used for pausing .queue_rq() by driver when
1623 * there isn't enough resource or some conditions aren't satisfied, and
4d606219 1624 * BLK_STS_RESOURCE is usually returned.
39a70c76
ML
1625 *
1626 * We do not guarantee that dispatch can be drained or blocked
1627 * after blk_mq_stop_hw_queue() returns. Please use
1628 * blk_mq_quiesce_queue() for that requirement.
1629 */
2719aa21
JA
1630void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
1631{
641a9ed6 1632 cancel_delayed_work(&hctx->run_work);
280d45f6 1633
641a9ed6 1634 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
2719aa21 1635}
641a9ed6 1636EXPORT_SYMBOL(blk_mq_stop_hw_queue);
2719aa21 1637
39a70c76
ML
1638/*
1639 * This function is often used for pausing .queue_rq() by driver when
1640 * there isn't enough resource or some conditions aren't satisfied, and
4d606219 1641 * BLK_STS_RESOURCE is usually returned.
39a70c76
ML
1642 *
1643 * We do not guarantee that dispatch can be drained or blocked
1644 * after blk_mq_stop_hw_queues() returns. Please use
1645 * blk_mq_quiesce_queue() for that requirement.
1646 */
2719aa21
JA
1647void blk_mq_stop_hw_queues(struct request_queue *q)
1648{
641a9ed6
ML
1649 struct blk_mq_hw_ctx *hctx;
1650 int i;
1651
1652 queue_for_each_hw_ctx(q, hctx, i)
1653 blk_mq_stop_hw_queue(hctx);
280d45f6
CH
1654}
1655EXPORT_SYMBOL(blk_mq_stop_hw_queues);
1656
320ae51f
JA
1657void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
1658{
1659 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
e4043dcf 1660
0ffbce80 1661 blk_mq_run_hw_queue(hctx, false);
320ae51f
JA
1662}
1663EXPORT_SYMBOL(blk_mq_start_hw_queue);
1664
2f268556
CH
1665void blk_mq_start_hw_queues(struct request_queue *q)
1666{
1667 struct blk_mq_hw_ctx *hctx;
1668 int i;
1669
1670 queue_for_each_hw_ctx(q, hctx, i)
1671 blk_mq_start_hw_queue(hctx);
1672}
1673EXPORT_SYMBOL(blk_mq_start_hw_queues);
1674
ae911c5e
JA
1675void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1676{
1677 if (!blk_mq_hctx_stopped(hctx))
1678 return;
1679
1680 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1681 blk_mq_run_hw_queue(hctx, async);
1682}
1683EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
1684
1b4a3258 1685void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
320ae51f
JA
1686{
1687 struct blk_mq_hw_ctx *hctx;
1688 int i;
1689
ae911c5e
JA
1690 queue_for_each_hw_ctx(q, hctx, i)
1691 blk_mq_start_stopped_hw_queue(hctx, async);
320ae51f
JA
1692}
1693EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1694
70f4db63 1695static void blk_mq_run_work_fn(struct work_struct *work)
320ae51f
JA
1696{
1697 struct blk_mq_hw_ctx *hctx;
1698
9f993737 1699 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
320ae51f 1700
21c6e939 1701 /*
15fe8a90 1702 * If we are stopped, don't run the queue.
21c6e939 1703 */
15fe8a90 1704 if (test_bit(BLK_MQ_S_STOPPED, &hctx->state))
0196d6b4 1705 return;
7587a5ae
BVA
1706
1707 __blk_mq_run_hw_queue(hctx);
1708}
1709
cfd0c552 1710static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
cfd0c552
ML
1711 struct request *rq,
1712 bool at_head)
320ae51f 1713{
e57690fe 1714 struct blk_mq_ctx *ctx = rq->mq_ctx;
c16d6b5a 1715 enum hctx_type type = hctx->type;
e57690fe 1716
7b607814
BVA
1717 lockdep_assert_held(&ctx->lock);
1718
01b983c9
JA
1719 trace_block_rq_insert(hctx->queue, rq);
1720
72a0a36e 1721 if (at_head)
c16d6b5a 1722 list_add(&rq->queuelist, &ctx->rq_lists[type]);
72a0a36e 1723 else
c16d6b5a 1724 list_add_tail(&rq->queuelist, &ctx->rq_lists[type]);
cfd0c552 1725}
4bb659b1 1726
2c3ad667
JA
1727void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
1728 bool at_head)
cfd0c552
ML
1729{
1730 struct blk_mq_ctx *ctx = rq->mq_ctx;
1731
7b607814
BVA
1732 lockdep_assert_held(&ctx->lock);
1733
e57690fe 1734 __blk_mq_insert_req_list(hctx, rq, at_head);
320ae51f 1735 blk_mq_hctx_mark_pending(hctx, ctx);
320ae51f
JA
1736}
1737
105663f7
AA
1738/**
1739 * blk_mq_request_bypass_insert - Insert a request at dispatch list.
1740 * @rq: Pointer to request to be inserted.
1741 * @run_queue: If we should run the hardware queue after inserting the request.
1742 *
157f377b
JA
1743 * Should only be used carefully, when the caller knows we want to
1744 * bypass a potential IO scheduler on the target device.
1745 */
01e99aec
ML
1746void blk_mq_request_bypass_insert(struct request *rq, bool at_head,
1747 bool run_queue)
157f377b 1748{
ea4f995e 1749 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
157f377b
JA
1750
1751 spin_lock(&hctx->lock);
01e99aec
ML
1752 if (at_head)
1753 list_add(&rq->queuelist, &hctx->dispatch);
1754 else
1755 list_add_tail(&rq->queuelist, &hctx->dispatch);
157f377b
JA
1756 spin_unlock(&hctx->lock);
1757
b0850297
ML
1758 if (run_queue)
1759 blk_mq_run_hw_queue(hctx, false);
157f377b
JA
1760}
1761
bd166ef1
JA
1762void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
1763 struct list_head *list)
320ae51f
JA
1764
1765{
3f0cedc7 1766 struct request *rq;
c16d6b5a 1767 enum hctx_type type = hctx->type;
3f0cedc7 1768
320ae51f
JA
1769 /*
1770 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1771 * offline now
1772 */
3f0cedc7 1773 list_for_each_entry(rq, list, queuelist) {
e57690fe 1774 BUG_ON(rq->mq_ctx != ctx);
3f0cedc7 1775 trace_block_rq_insert(hctx->queue, rq);
320ae51f 1776 }
3f0cedc7
ML
1777
1778 spin_lock(&ctx->lock);
c16d6b5a 1779 list_splice_tail_init(list, &ctx->rq_lists[type]);
cfd0c552 1780 blk_mq_hctx_mark_pending(hctx, ctx);
320ae51f 1781 spin_unlock(&ctx->lock);
320ae51f
JA
1782}
1783
3110fc79 1784static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
320ae51f
JA
1785{
1786 struct request *rqa = container_of(a, struct request, queuelist);
1787 struct request *rqb = container_of(b, struct request, queuelist);
1788
7d30a621
PB
1789 if (rqa->mq_ctx != rqb->mq_ctx)
1790 return rqa->mq_ctx > rqb->mq_ctx;
1791 if (rqa->mq_hctx != rqb->mq_hctx)
1792 return rqa->mq_hctx > rqb->mq_hctx;
3110fc79
JA
1793
1794 return blk_rq_pos(rqa) > blk_rq_pos(rqb);
320ae51f
JA
1795}
1796
1797void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1798{
320ae51f 1799 LIST_HEAD(list);
320ae51f 1800
95ed0c5b
PB
1801 if (list_empty(&plug->mq_list))
1802 return;
320ae51f
JA
1803 list_splice_init(&plug->mq_list, &list);
1804
ce5b009c
JA
1805 if (plug->rq_count > 2 && plug->multiple_queues)
1806 list_sort(NULL, &list, plug_rq_cmp);
320ae51f 1807
bcc816df
DZ
1808 plug->rq_count = 0;
1809
95ed0c5b
PB
1810 do {
1811 struct list_head rq_list;
1812 struct request *rq, *head_rq = list_entry_rq(list.next);
1813 struct list_head *pos = &head_rq->queuelist; /* skip first */
1814 struct blk_mq_hw_ctx *this_hctx = head_rq->mq_hctx;
1815 struct blk_mq_ctx *this_ctx = head_rq->mq_ctx;
1816 unsigned int depth = 1;
1817
1818 list_for_each_continue(pos, &list) {
1819 rq = list_entry_rq(pos);
1820 BUG_ON(!rq->q);
1821 if (rq->mq_hctx != this_hctx || rq->mq_ctx != this_ctx)
1822 break;
1823 depth++;
320ae51f
JA
1824 }
1825
95ed0c5b
PB
1826 list_cut_before(&rq_list, &list, pos);
1827 trace_block_unplug(head_rq->q, depth, !from_schedule);
67cae4c9 1828 blk_mq_sched_insert_requests(this_hctx, this_ctx, &rq_list,
bd166ef1 1829 from_schedule);
95ed0c5b 1830 } while(!list_empty(&list));
320ae51f
JA
1831}
1832
14ccb66b
CH
1833static void blk_mq_bio_to_request(struct request *rq, struct bio *bio,
1834 unsigned int nr_segs)
320ae51f 1835{
f924cdde
CH
1836 if (bio->bi_opf & REQ_RAHEAD)
1837 rq->cmd_flags |= REQ_FAILFAST_MASK;
1838
1839 rq->__sector = bio->bi_iter.bi_sector;
1840 rq->write_hint = bio->bi_write_hint;
14ccb66b 1841 blk_rq_bio_prep(rq, bio, nr_segs);
a892c8d5 1842 blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO);
4b570521 1843
b5af37ab 1844 blk_account_io_start(rq);
320ae51f
JA
1845}
1846
0f95549c
MS
1847static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
1848 struct request *rq,
be94f058 1849 blk_qc_t *cookie, bool last)
f984df1f 1850{
f984df1f 1851 struct request_queue *q = rq->q;
f984df1f
SL
1852 struct blk_mq_queue_data bd = {
1853 .rq = rq,
be94f058 1854 .last = last,
f984df1f 1855 };
bd166ef1 1856 blk_qc_t new_cookie;
f06345ad 1857 blk_status_t ret;
0f95549c
MS
1858
1859 new_cookie = request_to_qc_t(hctx, rq);
1860
1861 /*
1862 * For OK queue, we are done. For error, caller may kill it.
1863 * Any other error (busy), just add it to our list as we
1864 * previously would have done.
1865 */
1866 ret = q->mq_ops->queue_rq(hctx, &bd);
1867 switch (ret) {
1868 case BLK_STS_OK:
6ce3dd6e 1869 blk_mq_update_dispatch_busy(hctx, false);
0f95549c
MS
1870 *cookie = new_cookie;
1871 break;
1872 case BLK_STS_RESOURCE:
86ff7c2a 1873 case BLK_STS_DEV_RESOURCE:
6ce3dd6e 1874 blk_mq_update_dispatch_busy(hctx, true);
0f95549c
MS
1875 __blk_mq_requeue_request(rq);
1876 break;
1877 default:
6ce3dd6e 1878 blk_mq_update_dispatch_busy(hctx, false);
0f95549c
MS
1879 *cookie = BLK_QC_T_NONE;
1880 break;
1881 }
1882
1883 return ret;
1884}
1885
fd9c40f6 1886static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
0f95549c 1887 struct request *rq,
396eaf21 1888 blk_qc_t *cookie,
fd9c40f6 1889 bool bypass_insert, bool last)
0f95549c
MS
1890{
1891 struct request_queue *q = rq->q;
d964f04a
ML
1892 bool run_queue = true;
1893
23d4ee19 1894 /*
fd9c40f6 1895 * RCU or SRCU read lock is needed before checking quiesced flag.
23d4ee19 1896 *
fd9c40f6
BVA
1897 * When queue is stopped or quiesced, ignore 'bypass_insert' from
1898 * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller,
1899 * and avoid driver to try to dispatch again.
23d4ee19 1900 */
fd9c40f6 1901 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
d964f04a 1902 run_queue = false;
fd9c40f6
BVA
1903 bypass_insert = false;
1904 goto insert;
d964f04a 1905 }
f984df1f 1906
fd9c40f6
BVA
1907 if (q->elevator && !bypass_insert)
1908 goto insert;
2253efc8 1909
0bca799b 1910 if (!blk_mq_get_dispatch_budget(hctx))
fd9c40f6 1911 goto insert;
bd166ef1 1912
8ab6bb9e 1913 if (!blk_mq_get_driver_tag(rq)) {
0bca799b 1914 blk_mq_put_dispatch_budget(hctx);
fd9c40f6 1915 goto insert;
88022d72 1916 }
de148297 1917
fd9c40f6
BVA
1918 return __blk_mq_issue_directly(hctx, rq, cookie, last);
1919insert:
1920 if (bypass_insert)
1921 return BLK_STS_RESOURCE;
1922
01e99aec 1923 blk_mq_request_bypass_insert(rq, false, run_queue);
fd9c40f6
BVA
1924 return BLK_STS_OK;
1925}
1926
105663f7
AA
1927/**
1928 * blk_mq_try_issue_directly - Try to send a request directly to device driver.
1929 * @hctx: Pointer of the associated hardware queue.
1930 * @rq: Pointer to request to be sent.
1931 * @cookie: Request queue cookie.
1932 *
1933 * If the device has enough resources to accept a new request now, send the
1934 * request directly to device driver. Else, insert at hctx->dispatch queue, so
1935 * we can try send it another time in the future. Requests inserted at this
1936 * queue have higher priority.
1937 */
fd9c40f6
BVA
1938static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
1939 struct request *rq, blk_qc_t *cookie)
1940{
1941 blk_status_t ret;
1942 int srcu_idx;
1943
1944 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
1945
1946 hctx_lock(hctx, &srcu_idx);
1947
1948 ret = __blk_mq_try_issue_directly(hctx, rq, cookie, false, true);
1949 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
01e99aec 1950 blk_mq_request_bypass_insert(rq, false, true);
fd9c40f6
BVA
1951 else if (ret != BLK_STS_OK)
1952 blk_mq_end_request(rq, ret);
1953
1954 hctx_unlock(hctx, srcu_idx);
1955}
1956
1957blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last)
1958{
1959 blk_status_t ret;
1960 int srcu_idx;
1961 blk_qc_t unused_cookie;
1962 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
1963
1964 hctx_lock(hctx, &srcu_idx);
1965 ret = __blk_mq_try_issue_directly(hctx, rq, &unused_cookie, true, last);
04ced159 1966 hctx_unlock(hctx, srcu_idx);
7f556a44
JW
1967
1968 return ret;
5eb6126e
CH
1969}
1970
6ce3dd6e
ML
1971void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
1972 struct list_head *list)
1973{
536167d4
KB
1974 int queued = 0;
1975
6ce3dd6e 1976 while (!list_empty(list)) {
fd9c40f6 1977 blk_status_t ret;
6ce3dd6e
ML
1978 struct request *rq = list_first_entry(list, struct request,
1979 queuelist);
1980
1981 list_del_init(&rq->queuelist);
fd9c40f6
BVA
1982 ret = blk_mq_request_issue_directly(rq, list_empty(list));
1983 if (ret != BLK_STS_OK) {
1984 if (ret == BLK_STS_RESOURCE ||
1985 ret == BLK_STS_DEV_RESOURCE) {
01e99aec 1986 blk_mq_request_bypass_insert(rq, false,
c616cbee 1987 list_empty(list));
fd9c40f6
BVA
1988 break;
1989 }
1990 blk_mq_end_request(rq, ret);
536167d4
KB
1991 } else
1992 queued++;
6ce3dd6e 1993 }
d666ba98
JA
1994
1995 /*
1996 * If we didn't flush the entire list, we could have told
1997 * the driver there was more coming, but that turned out to
1998 * be a lie.
1999 */
536167d4 2000 if (!list_empty(list) && hctx->queue->mq_ops->commit_rqs && queued)
d666ba98 2001 hctx->queue->mq_ops->commit_rqs(hctx);
6ce3dd6e
ML
2002}
2003
ce5b009c
JA
2004static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
2005{
2006 list_add_tail(&rq->queuelist, &plug->mq_list);
2007 plug->rq_count++;
2008 if (!plug->multiple_queues && !list_is_singular(&plug->mq_list)) {
2009 struct request *tmp;
2010
2011 tmp = list_first_entry(&plug->mq_list, struct request,
2012 queuelist);
2013 if (tmp->q != rq->q)
2014 plug->multiple_queues = true;
2015 }
2016}
2017
105663f7
AA
2018/**
2019 * blk_mq_make_request - Create and send a request to block device.
2020 * @q: Request queue pointer.
2021 * @bio: Bio pointer.
2022 *
2023 * Builds up a request structure from @q and @bio and send to the device. The
2024 * request may not be queued directly to hardware if:
2025 * * This request can be merged with another one
2026 * * We want to place request at plug queue for possible future merging
2027 * * There is an IO scheduler active at this queue
2028 *
2029 * It will not queue the request if there is an error with the bio, or at the
2030 * request creation.
2031 *
2032 * Returns: Request queue cookie.
2033 */
8cf7961d 2034blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
07068d5b 2035{
ef295ecf 2036 const int is_sync = op_is_sync(bio->bi_opf);
f73f44eb 2037 const int is_flush_fua = op_is_flush(bio->bi_opf);
e6e7abff
CH
2038 struct blk_mq_alloc_data data = {
2039 .q = q,
2040 };
07068d5b 2041 struct request *rq;
f984df1f 2042 struct blk_plug *plug;
5b3f341f 2043 struct request *same_queue_rq = NULL;
14ccb66b 2044 unsigned int nr_segs;
7b371636 2045 blk_qc_t cookie;
a892c8d5 2046 blk_status_t ret;
07068d5b
JA
2047
2048 blk_queue_bounce(q, &bio);
14ccb66b 2049 __blk_queue_split(q, &bio, &nr_segs);
f36ea50c 2050
e23947bd 2051 if (!bio_integrity_prep(bio))
ac7c5675 2052 goto queue_exit;
07068d5b 2053
87c279e6 2054 if (!is_flush_fua && !blk_queue_nomerges(q) &&
14ccb66b 2055 blk_attempt_plug_merge(q, bio, nr_segs, &same_queue_rq))
ac7c5675 2056 goto queue_exit;
f984df1f 2057
14ccb66b 2058 if (blk_mq_sched_bio_merge(q, bio, nr_segs))
ac7c5675 2059 goto queue_exit;
bd166ef1 2060
d5337560 2061 rq_qos_throttle(q, bio);
87760e5e 2062
7809167d 2063 data.cmd_flags = bio->bi_opf;
e6e7abff 2064 rq = __blk_mq_alloc_request(&data);
87760e5e 2065 if (unlikely(!rq)) {
c1c80384 2066 rq_qos_cleanup(q, bio);
7b6620d7 2067 if (bio->bi_opf & REQ_NOWAIT)
03a07c92 2068 bio_wouldblock_error(bio);
ac7c5675 2069 goto queue_exit;
87760e5e
JA
2070 }
2071
d6f1dda2
XW
2072 trace_block_getrq(q, bio, bio->bi_opf);
2073
c1c80384 2074 rq_qos_track(q, rq, bio);
07068d5b 2075
fd2d3326 2076 cookie = request_to_qc_t(data.hctx, rq);
07068d5b 2077
970d168d
BVA
2078 blk_mq_bio_to_request(rq, bio, nr_segs);
2079
a892c8d5
ST
2080 ret = blk_crypto_init_request(rq);
2081 if (ret != BLK_STS_OK) {
2082 bio->bi_status = ret;
2083 bio_endio(bio);
2084 blk_mq_free_request(rq);
2085 return BLK_QC_T_NONE;
2086 }
2087
b49773e7 2088 plug = blk_mq_plug(q, bio);
07068d5b 2089 if (unlikely(is_flush_fua)) {
105663f7 2090 /* Bypass scheduler for flush requests */
923218f6
ML
2091 blk_insert_flush(rq);
2092 blk_mq_run_hw_queue(data.hctx, true);
3154df26
ML
2093 } else if (plug && (q->nr_hw_queues == 1 || q->mq_ops->commit_rqs ||
2094 !blk_queue_nonrot(q))) {
b2c5d16b
JA
2095 /*
2096 * Use plugging if we have a ->commit_rqs() hook as well, as
2097 * we know the driver uses bd->last in a smart fashion.
3154df26
ML
2098 *
2099 * Use normal plugging if this disk is slow HDD, as sequential
2100 * IO may benefit a lot from plug merging.
b2c5d16b 2101 */
5f0ed774 2102 unsigned int request_count = plug->rq_count;
600271d9
SL
2103 struct request *last = NULL;
2104
676d0607 2105 if (!request_count)
e6c4438b 2106 trace_block_plug(q);
600271d9
SL
2107 else
2108 last = list_entry_rq(plug->mq_list.prev);
b094f89c 2109
600271d9
SL
2110 if (request_count >= BLK_MAX_REQUEST_COUNT || (last &&
2111 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
e6c4438b
JM
2112 blk_flush_plug_list(plug, false);
2113 trace_block_plug(q);
320ae51f 2114 }
b094f89c 2115
ce5b009c 2116 blk_add_rq_to_plug(plug, rq);
a12de1d4 2117 } else if (q->elevator) {
105663f7 2118 /* Insert the request at the IO scheduler queue */
a12de1d4 2119 blk_mq_sched_insert_request(rq, false, true, true);
2299722c 2120 } else if (plug && !blk_queue_nomerges(q)) {
07068d5b 2121 /*
6a83e74d 2122 * We do limited plugging. If the bio can be merged, do that.
f984df1f
SL
2123 * Otherwise the existing request in the plug list will be
2124 * issued. So the plug list will have one request at most
2299722c
CH
2125 * The plug list might get flushed before this. If that happens,
2126 * the plug list is empty, and same_queue_rq is invalid.
07068d5b 2127 */
2299722c
CH
2128 if (list_empty(&plug->mq_list))
2129 same_queue_rq = NULL;
4711b573 2130 if (same_queue_rq) {
2299722c 2131 list_del_init(&same_queue_rq->queuelist);
4711b573
JA
2132 plug->rq_count--;
2133 }
ce5b009c 2134 blk_add_rq_to_plug(plug, rq);
ff3b74b8 2135 trace_block_plug(q);
2299722c 2136
dad7a3be 2137 if (same_queue_rq) {
ea4f995e 2138 data.hctx = same_queue_rq->mq_hctx;
ff3b74b8 2139 trace_block_unplug(q, 1, true);
2299722c 2140 blk_mq_try_issue_directly(data.hctx, same_queue_rq,
fd9c40f6 2141 &cookie);
dad7a3be 2142 }
a12de1d4
ML
2143 } else if ((q->nr_hw_queues > 1 && is_sync) ||
2144 !data.hctx->dispatch_busy) {
105663f7
AA
2145 /*
2146 * There is no scheduler and we can try to send directly
2147 * to the hardware.
2148 */
fd9c40f6 2149 blk_mq_try_issue_directly(data.hctx, rq, &cookie);
ab42f35d 2150 } else {
105663f7 2151 /* Default case. */
8fa9f556 2152 blk_mq_sched_insert_request(rq, false, true, true);
ab42f35d 2153 }
320ae51f 2154
7b371636 2155 return cookie;
ac7c5675
CH
2156queue_exit:
2157 blk_queue_exit(q);
2158 return BLK_QC_T_NONE;
320ae51f 2159}
8cf7961d 2160EXPORT_SYMBOL_GPL(blk_mq_make_request); /* only for request based dm */
320ae51f 2161
cc71a6f4
JA
2162void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2163 unsigned int hctx_idx)
95363efd 2164{
e9b267d9 2165 struct page *page;
320ae51f 2166
24d2f903 2167 if (tags->rqs && set->ops->exit_request) {
e9b267d9 2168 int i;
320ae51f 2169
24d2f903 2170 for (i = 0; i < tags->nr_tags; i++) {
2af8cbe3
JA
2171 struct request *rq = tags->static_rqs[i];
2172
2173 if (!rq)
e9b267d9 2174 continue;
d6296d39 2175 set->ops->exit_request(set, rq, hctx_idx);
2af8cbe3 2176 tags->static_rqs[i] = NULL;
e9b267d9 2177 }
320ae51f 2178 }
320ae51f 2179
24d2f903
CH
2180 while (!list_empty(&tags->page_list)) {
2181 page = list_first_entry(&tags->page_list, struct page, lru);
6753471c 2182 list_del_init(&page->lru);
f75782e4
CM
2183 /*
2184 * Remove kmemleak object previously allocated in
273938bf 2185 * blk_mq_alloc_rqs().
f75782e4
CM
2186 */
2187 kmemleak_free(page_address(page));
320ae51f
JA
2188 __free_pages(page, page->private);
2189 }
cc71a6f4 2190}
320ae51f 2191
cc71a6f4
JA
2192void blk_mq_free_rq_map(struct blk_mq_tags *tags)
2193{
24d2f903 2194 kfree(tags->rqs);
cc71a6f4 2195 tags->rqs = NULL;
2af8cbe3
JA
2196 kfree(tags->static_rqs);
2197 tags->static_rqs = NULL;
320ae51f 2198
24d2f903 2199 blk_mq_free_tags(tags);
320ae51f
JA
2200}
2201
cc71a6f4
JA
2202struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
2203 unsigned int hctx_idx,
2204 unsigned int nr_tags,
2205 unsigned int reserved_tags)
320ae51f 2206{
24d2f903 2207 struct blk_mq_tags *tags;
59f082e4 2208 int node;
320ae51f 2209
7d76f856 2210 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
59f082e4
SL
2211 if (node == NUMA_NO_NODE)
2212 node = set->numa_node;
2213
2214 tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
24391c0d 2215 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
24d2f903
CH
2216 if (!tags)
2217 return NULL;
320ae51f 2218
590b5b7d 2219 tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
36e1f3d1 2220 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
59f082e4 2221 node);
24d2f903
CH
2222 if (!tags->rqs) {
2223 blk_mq_free_tags(tags);
2224 return NULL;
2225 }
320ae51f 2226
590b5b7d
KC
2227 tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
2228 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2229 node);
2af8cbe3
JA
2230 if (!tags->static_rqs) {
2231 kfree(tags->rqs);
2232 blk_mq_free_tags(tags);
2233 return NULL;
2234 }
2235
cc71a6f4
JA
2236 return tags;
2237}
2238
2239static size_t order_to_size(unsigned int order)
2240{
2241 return (size_t)PAGE_SIZE << order;
2242}
2243
1d9bd516
TH
2244static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
2245 unsigned int hctx_idx, int node)
2246{
2247 int ret;
2248
2249 if (set->ops->init_request) {
2250 ret = set->ops->init_request(set, rq, hctx_idx, node);
2251 if (ret)
2252 return ret;
2253 }
2254
12f5b931 2255 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
1d9bd516
TH
2256 return 0;
2257}
2258
cc71a6f4
JA
2259int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2260 unsigned int hctx_idx, unsigned int depth)
2261{
2262 unsigned int i, j, entries_per_page, max_order = 4;
2263 size_t rq_size, left;
59f082e4
SL
2264 int node;
2265
7d76f856 2266 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
59f082e4
SL
2267 if (node == NUMA_NO_NODE)
2268 node = set->numa_node;
cc71a6f4
JA
2269
2270 INIT_LIST_HEAD(&tags->page_list);
2271
320ae51f
JA
2272 /*
2273 * rq_size is the size of the request plus driver payload, rounded
2274 * to the cacheline size
2275 */
24d2f903 2276 rq_size = round_up(sizeof(struct request) + set->cmd_size,
320ae51f 2277 cache_line_size());
cc71a6f4 2278 left = rq_size * depth;
320ae51f 2279
cc71a6f4 2280 for (i = 0; i < depth; ) {
320ae51f
JA
2281 int this_order = max_order;
2282 struct page *page;
2283 int to_do;
2284 void *p;
2285
b3a834b1 2286 while (this_order && left < order_to_size(this_order - 1))
320ae51f
JA
2287 this_order--;
2288
2289 do {
59f082e4 2290 page = alloc_pages_node(node,
36e1f3d1 2291 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
a5164405 2292 this_order);
320ae51f
JA
2293 if (page)
2294 break;
2295 if (!this_order--)
2296 break;
2297 if (order_to_size(this_order) < rq_size)
2298 break;
2299 } while (1);
2300
2301 if (!page)
24d2f903 2302 goto fail;
320ae51f
JA
2303
2304 page->private = this_order;
24d2f903 2305 list_add_tail(&page->lru, &tags->page_list);
320ae51f
JA
2306
2307 p = page_address(page);
f75782e4
CM
2308 /*
2309 * Allow kmemleak to scan these pages as they contain pointers
2310 * to additional allocations like via ops->init_request().
2311 */
36e1f3d1 2312 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
320ae51f 2313 entries_per_page = order_to_size(this_order) / rq_size;
cc71a6f4 2314 to_do = min(entries_per_page, depth - i);
320ae51f
JA
2315 left -= to_do * rq_size;
2316 for (j = 0; j < to_do; j++) {
2af8cbe3
JA
2317 struct request *rq = p;
2318
2319 tags->static_rqs[i] = rq;
1d9bd516
TH
2320 if (blk_mq_init_request(set, rq, hctx_idx, node)) {
2321 tags->static_rqs[i] = NULL;
2322 goto fail;
e9b267d9
CH
2323 }
2324
320ae51f
JA
2325 p += rq_size;
2326 i++;
2327 }
2328 }
cc71a6f4 2329 return 0;
320ae51f 2330
24d2f903 2331fail:
cc71a6f4
JA
2332 blk_mq_free_rqs(set, tags, hctx_idx);
2333 return -ENOMEM;
320ae51f
JA
2334}
2335
e57690fe
JA
2336/*
2337 * 'cpu' is going away. splice any existing rq_list entries from this
2338 * software queue to the hw queue dispatch list, and ensure that it
2339 * gets run.
2340 */
9467f859 2341static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
484b4061 2342{
9467f859 2343 struct blk_mq_hw_ctx *hctx;
484b4061
JA
2344 struct blk_mq_ctx *ctx;
2345 LIST_HEAD(tmp);
c16d6b5a 2346 enum hctx_type type;
484b4061 2347
9467f859 2348 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
e57690fe 2349 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
c16d6b5a 2350 type = hctx->type;
484b4061
JA
2351
2352 spin_lock(&ctx->lock);
c16d6b5a
ML
2353 if (!list_empty(&ctx->rq_lists[type])) {
2354 list_splice_init(&ctx->rq_lists[type], &tmp);
484b4061
JA
2355 blk_mq_hctx_clear_pending(hctx, ctx);
2356 }
2357 spin_unlock(&ctx->lock);
2358
2359 if (list_empty(&tmp))
9467f859 2360 return 0;
484b4061 2361
e57690fe
JA
2362 spin_lock(&hctx->lock);
2363 list_splice_tail_init(&tmp, &hctx->dispatch);
2364 spin_unlock(&hctx->lock);
484b4061
JA
2365
2366 blk_mq_run_hw_queue(hctx, true);
9467f859 2367 return 0;
484b4061
JA
2368}
2369
9467f859 2370static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
484b4061 2371{
9467f859
TG
2372 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
2373 &hctx->cpuhp_dead);
484b4061
JA
2374}
2375
c3b4afca 2376/* hctx->ctxs will be freed in queue's release handler */
08e98fc6
ML
2377static void blk_mq_exit_hctx(struct request_queue *q,
2378 struct blk_mq_tag_set *set,
2379 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
2380{
8ab0b7dc
ML
2381 if (blk_mq_hw_queue_mapped(hctx))
2382 blk_mq_tag_idle(hctx);
08e98fc6 2383
f70ced09 2384 if (set->ops->exit_request)
d6296d39 2385 set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx);
f70ced09 2386
08e98fc6
ML
2387 if (set->ops->exit_hctx)
2388 set->ops->exit_hctx(hctx, hctx_idx);
2389
9467f859 2390 blk_mq_remove_cpuhp(hctx);
2f8f1336
ML
2391
2392 spin_lock(&q->unused_hctx_lock);
2393 list_add(&hctx->hctx_list, &q->unused_hctx_list);
2394 spin_unlock(&q->unused_hctx_lock);
08e98fc6
ML
2395}
2396
624dbe47
ML
2397static void blk_mq_exit_hw_queues(struct request_queue *q,
2398 struct blk_mq_tag_set *set, int nr_queue)
2399{
2400 struct blk_mq_hw_ctx *hctx;
2401 unsigned int i;
2402
2403 queue_for_each_hw_ctx(q, hctx, i) {
2404 if (i == nr_queue)
2405 break;
477e19de 2406 blk_mq_debugfs_unregister_hctx(hctx);
08e98fc6 2407 blk_mq_exit_hctx(q, set, hctx, i);
624dbe47 2408 }
624dbe47
ML
2409}
2410
7c6c5b7c
ML
2411static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
2412{
2413 int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
2414
2415 BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu),
2416 __alignof__(struct blk_mq_hw_ctx)) !=
2417 sizeof(struct blk_mq_hw_ctx));
2418
2419 if (tag_set->flags & BLK_MQ_F_BLOCKING)
2420 hw_ctx_size += sizeof(struct srcu_struct);
2421
2422 return hw_ctx_size;
2423}
2424
08e98fc6
ML
2425static int blk_mq_init_hctx(struct request_queue *q,
2426 struct blk_mq_tag_set *set,
2427 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
320ae51f 2428{
7c6c5b7c
ML
2429 hctx->queue_num = hctx_idx;
2430
2431 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
2432
2433 hctx->tags = set->tags[hctx_idx];
2434
2435 if (set->ops->init_hctx &&
2436 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
2437 goto unregister_cpu_notifier;
08e98fc6 2438
7c6c5b7c
ML
2439 if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx,
2440 hctx->numa_node))
2441 goto exit_hctx;
2442 return 0;
2443
2444 exit_hctx:
2445 if (set->ops->exit_hctx)
2446 set->ops->exit_hctx(hctx, hctx_idx);
2447 unregister_cpu_notifier:
2448 blk_mq_remove_cpuhp(hctx);
2449 return -1;
2450}
2451
2452static struct blk_mq_hw_ctx *
2453blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
2454 int node)
2455{
2456 struct blk_mq_hw_ctx *hctx;
2457 gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY;
2458
2459 hctx = kzalloc_node(blk_mq_hw_ctx_size(set), gfp, node);
2460 if (!hctx)
2461 goto fail_alloc_hctx;
2462
2463 if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node))
2464 goto free_hctx;
2465
2466 atomic_set(&hctx->nr_active, 0);
08e98fc6 2467 if (node == NUMA_NO_NODE)
7c6c5b7c
ML
2468 node = set->numa_node;
2469 hctx->numa_node = node;
08e98fc6 2470
9f993737 2471 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
08e98fc6
ML
2472 spin_lock_init(&hctx->lock);
2473 INIT_LIST_HEAD(&hctx->dispatch);
2474 hctx->queue = q;
2404e607 2475 hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED;
08e98fc6 2476
2f8f1336
ML
2477 INIT_LIST_HEAD(&hctx->hctx_list);
2478
320ae51f 2479 /*
08e98fc6
ML
2480 * Allocate space for all possible cpus to avoid allocation at
2481 * runtime
320ae51f 2482 */
d904bfa7 2483 hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
7c6c5b7c 2484 gfp, node);
08e98fc6 2485 if (!hctx->ctxs)
7c6c5b7c 2486 goto free_cpumask;
320ae51f 2487
5b202853 2488 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
7c6c5b7c 2489 gfp, node))
08e98fc6 2490 goto free_ctxs;
08e98fc6 2491 hctx->nr_ctx = 0;
320ae51f 2492
5815839b 2493 spin_lock_init(&hctx->dispatch_wait_lock);
eb619fdb
JA
2494 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
2495 INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
2496
754a1572 2497 hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp);
f70ced09 2498 if (!hctx->fq)
7c6c5b7c 2499 goto free_bitmap;
320ae51f 2500
6a83e74d 2501 if (hctx->flags & BLK_MQ_F_BLOCKING)
05707b64 2502 init_srcu_struct(hctx->srcu);
7c6c5b7c 2503 blk_mq_hctx_kobj_init(hctx);
6a83e74d 2504
7c6c5b7c 2505 return hctx;
320ae51f 2506
08e98fc6 2507 free_bitmap:
88459642 2508 sbitmap_free(&hctx->ctx_map);
08e98fc6
ML
2509 free_ctxs:
2510 kfree(hctx->ctxs);
7c6c5b7c
ML
2511 free_cpumask:
2512 free_cpumask_var(hctx->cpumask);
2513 free_hctx:
2514 kfree(hctx);
2515 fail_alloc_hctx:
2516 return NULL;
08e98fc6 2517}
320ae51f 2518
320ae51f
JA
2519static void blk_mq_init_cpu_queues(struct request_queue *q,
2520 unsigned int nr_hw_queues)
2521{
b3c661b1
JA
2522 struct blk_mq_tag_set *set = q->tag_set;
2523 unsigned int i, j;
320ae51f
JA
2524
2525 for_each_possible_cpu(i) {
2526 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
2527 struct blk_mq_hw_ctx *hctx;
c16d6b5a 2528 int k;
320ae51f 2529
320ae51f
JA
2530 __ctx->cpu = i;
2531 spin_lock_init(&__ctx->lock);
c16d6b5a
ML
2532 for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++)
2533 INIT_LIST_HEAD(&__ctx->rq_lists[k]);
2534
320ae51f
JA
2535 __ctx->queue = q;
2536
320ae51f
JA
2537 /*
2538 * Set local node, IFF we have more than one hw queue. If
2539 * not, we remain on the home node of the device
2540 */
b3c661b1
JA
2541 for (j = 0; j < set->nr_maps; j++) {
2542 hctx = blk_mq_map_queue_type(q, j, i);
2543 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
2544 hctx->numa_node = local_memory_node(cpu_to_node(i));
2545 }
320ae51f
JA
2546 }
2547}
2548
03b63b02
WZ
2549static bool __blk_mq_alloc_map_and_request(struct blk_mq_tag_set *set,
2550 int hctx_idx)
cc71a6f4
JA
2551{
2552 int ret = 0;
2553
2554 set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx,
2555 set->queue_depth, set->reserved_tags);
2556 if (!set->tags[hctx_idx])
2557 return false;
2558
2559 ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx,
2560 set->queue_depth);
2561 if (!ret)
2562 return true;
2563
2564 blk_mq_free_rq_map(set->tags[hctx_idx]);
2565 set->tags[hctx_idx] = NULL;
2566 return false;
2567}
2568
2569static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set,
2570 unsigned int hctx_idx)
2571{
4e6db0f2 2572 if (set->tags && set->tags[hctx_idx]) {
bd166ef1
JA
2573 blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx);
2574 blk_mq_free_rq_map(set->tags[hctx_idx]);
2575 set->tags[hctx_idx] = NULL;
2576 }
cc71a6f4
JA
2577}
2578
4b855ad3 2579static void blk_mq_map_swqueue(struct request_queue *q)
320ae51f 2580{
b3c661b1 2581 unsigned int i, j, hctx_idx;
320ae51f
JA
2582 struct blk_mq_hw_ctx *hctx;
2583 struct blk_mq_ctx *ctx;
2a34c087 2584 struct blk_mq_tag_set *set = q->tag_set;
320ae51f
JA
2585
2586 queue_for_each_hw_ctx(q, hctx, i) {
e4043dcf 2587 cpumask_clear(hctx->cpumask);
320ae51f 2588 hctx->nr_ctx = 0;
d416c92c 2589 hctx->dispatch_from = NULL;
320ae51f
JA
2590 }
2591
2592 /*
4b855ad3 2593 * Map software to hardware queues.
4412efec
ML
2594 *
2595 * If the cpu isn't present, the cpu is mapped to first hctx.
320ae51f 2596 */
20e4d813 2597 for_each_possible_cpu(i) {
4412efec 2598
897bb0c7 2599 ctx = per_cpu_ptr(q->queue_ctx, i);
b3c661b1 2600 for (j = 0; j < set->nr_maps; j++) {
bb94aea1
JW
2601 if (!set->map[j].nr_queues) {
2602 ctx->hctxs[j] = blk_mq_map_queue_type(q,
2603 HCTX_TYPE_DEFAULT, i);
e5edd5f2 2604 continue;
bb94aea1 2605 }
fd689871
ML
2606 hctx_idx = set->map[j].mq_map[i];
2607 /* unmapped hw queue can be remapped after CPU topo changed */
2608 if (!set->tags[hctx_idx] &&
03b63b02 2609 !__blk_mq_alloc_map_and_request(set, hctx_idx)) {
fd689871
ML
2610 /*
2611 * If tags initialization fail for some hctx,
2612 * that hctx won't be brought online. In this
2613 * case, remap the current ctx to hctx[0] which
2614 * is guaranteed to always have tags allocated
2615 */
2616 set->map[j].mq_map[i] = 0;
2617 }
e5edd5f2 2618
b3c661b1 2619 hctx = blk_mq_map_queue_type(q, j, i);
8ccdf4a3 2620 ctx->hctxs[j] = hctx;
b3c661b1
JA
2621 /*
2622 * If the CPU is already set in the mask, then we've
2623 * mapped this one already. This can happen if
2624 * devices share queues across queue maps.
2625 */
2626 if (cpumask_test_cpu(i, hctx->cpumask))
2627 continue;
2628
2629 cpumask_set_cpu(i, hctx->cpumask);
2630 hctx->type = j;
2631 ctx->index_hw[hctx->type] = hctx->nr_ctx;
2632 hctx->ctxs[hctx->nr_ctx++] = ctx;
2633
2634 /*
2635 * If the nr_ctx type overflows, we have exceeded the
2636 * amount of sw queues we can support.
2637 */
2638 BUG_ON(!hctx->nr_ctx);
2639 }
bb94aea1
JW
2640
2641 for (; j < HCTX_MAX_TYPES; j++)
2642 ctx->hctxs[j] = blk_mq_map_queue_type(q,
2643 HCTX_TYPE_DEFAULT, i);
320ae51f 2644 }
506e931f
JA
2645
2646 queue_for_each_hw_ctx(q, hctx, i) {
4412efec
ML
2647 /*
2648 * If no software queues are mapped to this hardware queue,
2649 * disable it and free the request entries.
2650 */
2651 if (!hctx->nr_ctx) {
2652 /* Never unmap queue 0. We need it as a
2653 * fallback in case of a new remap fails
2654 * allocation
2655 */
2656 if (i && set->tags[i])
2657 blk_mq_free_map_and_requests(set, i);
2658
2659 hctx->tags = NULL;
2660 continue;
2661 }
484b4061 2662
2a34c087
ML
2663 hctx->tags = set->tags[i];
2664 WARN_ON(!hctx->tags);
2665
889fa31f
CY
2666 /*
2667 * Set the map size to the number of mapped software queues.
2668 * This is more accurate and more efficient than looping
2669 * over all possibly mapped software queues.
2670 */
88459642 2671 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
889fa31f 2672
484b4061
JA
2673 /*
2674 * Initialize batch roundrobin counts
2675 */
f82ddf19 2676 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
506e931f
JA
2677 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
2678 }
320ae51f
JA
2679}
2680
8e8320c9
JA
2681/*
2682 * Caller needs to ensure that we're either frozen/quiesced, or that
2683 * the queue isn't live yet.
2684 */
2404e607 2685static void queue_set_hctx_shared(struct request_queue *q, bool shared)
0d2602ca
JA
2686{
2687 struct blk_mq_hw_ctx *hctx;
0d2602ca
JA
2688 int i;
2689
2404e607 2690 queue_for_each_hw_ctx(q, hctx, i) {
97889f9a 2691 if (shared)
2404e607 2692 hctx->flags |= BLK_MQ_F_TAG_SHARED;
97889f9a 2693 else
2404e607
JM
2694 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
2695 }
2696}
2697
8e8320c9
JA
2698static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,
2699 bool shared)
2404e607
JM
2700{
2701 struct request_queue *q;
0d2602ca 2702
705cda97
BVA
2703 lockdep_assert_held(&set->tag_list_lock);
2704
0d2602ca
JA
2705 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2706 blk_mq_freeze_queue(q);
2404e607 2707 queue_set_hctx_shared(q, shared);
0d2602ca
JA
2708 blk_mq_unfreeze_queue(q);
2709 }
2710}
2711
2712static void blk_mq_del_queue_tag_set(struct request_queue *q)
2713{
2714 struct blk_mq_tag_set *set = q->tag_set;
2715
0d2602ca 2716 mutex_lock(&set->tag_list_lock);
705cda97 2717 list_del_rcu(&q->tag_set_list);
2404e607
JM
2718 if (list_is_singular(&set->tag_list)) {
2719 /* just transitioned to unshared */
2720 set->flags &= ~BLK_MQ_F_TAG_SHARED;
2721 /* update existing queue */
2722 blk_mq_update_tag_set_depth(set, false);
2723 }
0d2602ca 2724 mutex_unlock(&set->tag_list_lock);
a347c7ad 2725 INIT_LIST_HEAD(&q->tag_set_list);
0d2602ca
JA
2726}
2727
2728static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
2729 struct request_queue *q)
2730{
0d2602ca 2731 mutex_lock(&set->tag_list_lock);
2404e607 2732
ff821d27
JA
2733 /*
2734 * Check to see if we're transitioning to shared (from 1 to 2 queues).
2735 */
2736 if (!list_empty(&set->tag_list) &&
2737 !(set->flags & BLK_MQ_F_TAG_SHARED)) {
2404e607
JM
2738 set->flags |= BLK_MQ_F_TAG_SHARED;
2739 /* update existing queue */
2740 blk_mq_update_tag_set_depth(set, true);
2741 }
2742 if (set->flags & BLK_MQ_F_TAG_SHARED)
2743 queue_set_hctx_shared(q, true);
705cda97 2744 list_add_tail_rcu(&q->tag_set_list, &set->tag_list);
2404e607 2745
0d2602ca
JA
2746 mutex_unlock(&set->tag_list_lock);
2747}
2748
1db4909e
ML
2749/* All allocations will be freed in release handler of q->mq_kobj */
2750static int blk_mq_alloc_ctxs(struct request_queue *q)
2751{
2752 struct blk_mq_ctxs *ctxs;
2753 int cpu;
2754
2755 ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL);
2756 if (!ctxs)
2757 return -ENOMEM;
2758
2759 ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx);
2760 if (!ctxs->queue_ctx)
2761 goto fail;
2762
2763 for_each_possible_cpu(cpu) {
2764 struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu);
2765 ctx->ctxs = ctxs;
2766 }
2767
2768 q->mq_kobj = &ctxs->kobj;
2769 q->queue_ctx = ctxs->queue_ctx;
2770
2771 return 0;
2772 fail:
2773 kfree(ctxs);
2774 return -ENOMEM;
2775}
2776
e09aae7e
ML
2777/*
2778 * It is the actual release handler for mq, but we do it from
2779 * request queue's release handler for avoiding use-after-free
2780 * and headache because q->mq_kobj shouldn't have been introduced,
2781 * but we can't group ctx/kctx kobj without it.
2782 */
2783void blk_mq_release(struct request_queue *q)
2784{
2f8f1336
ML
2785 struct blk_mq_hw_ctx *hctx, *next;
2786 int i;
e09aae7e 2787
2f8f1336
ML
2788 queue_for_each_hw_ctx(q, hctx, i)
2789 WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list));
2790
2791 /* all hctx are in .unused_hctx_list now */
2792 list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) {
2793 list_del_init(&hctx->hctx_list);
6c8b232e 2794 kobject_put(&hctx->kobj);
c3b4afca 2795 }
e09aae7e
ML
2796
2797 kfree(q->queue_hw_ctx);
2798
7ea5fe31
ML
2799 /*
2800 * release .mq_kobj and sw queue's kobject now because
2801 * both share lifetime with request queue.
2802 */
2803 blk_mq_sysfs_deinit(q);
e09aae7e
ML
2804}
2805
2f227bb9
CH
2806struct request_queue *blk_mq_init_queue_data(struct blk_mq_tag_set *set,
2807 void *queuedata)
b62c21b7
MS
2808{
2809 struct request_queue *uninit_q, *q;
2810
3d745ea5 2811 uninit_q = __blk_alloc_queue(set->numa_node);
b62c21b7
MS
2812 if (!uninit_q)
2813 return ERR_PTR(-ENOMEM);
2f227bb9 2814 uninit_q->queuedata = queuedata;
b62c21b7 2815
737eb78e
DLM
2816 /*
2817 * Initialize the queue without an elevator. device_add_disk() will do
2818 * the initialization.
2819 */
2820 q = blk_mq_init_allocated_queue(set, uninit_q, false);
b62c21b7
MS
2821 if (IS_ERR(q))
2822 blk_cleanup_queue(uninit_q);
2823
2824 return q;
2825}
2f227bb9
CH
2826EXPORT_SYMBOL_GPL(blk_mq_init_queue_data);
2827
2828struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
2829{
2830 return blk_mq_init_queue_data(set, NULL);
2831}
b62c21b7
MS
2832EXPORT_SYMBOL(blk_mq_init_queue);
2833
9316a9ed
JA
2834/*
2835 * Helper for setting up a queue with mq ops, given queue depth, and
2836 * the passed in mq ops flags.
2837 */
2838struct request_queue *blk_mq_init_sq_queue(struct blk_mq_tag_set *set,
2839 const struct blk_mq_ops *ops,
2840 unsigned int queue_depth,
2841 unsigned int set_flags)
2842{
2843 struct request_queue *q;
2844 int ret;
2845
2846 memset(set, 0, sizeof(*set));
2847 set->ops = ops;
2848 set->nr_hw_queues = 1;
b3c661b1 2849 set->nr_maps = 1;
9316a9ed
JA
2850 set->queue_depth = queue_depth;
2851 set->numa_node = NUMA_NO_NODE;
2852 set->flags = set_flags;
2853
2854 ret = blk_mq_alloc_tag_set(set);
2855 if (ret)
2856 return ERR_PTR(ret);
2857
2858 q = blk_mq_init_queue(set);
2859 if (IS_ERR(q)) {
2860 blk_mq_free_tag_set(set);
2861 return q;
2862 }
2863
2864 return q;
2865}
2866EXPORT_SYMBOL(blk_mq_init_sq_queue);
2867
34d11ffa
JW
2868static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
2869 struct blk_mq_tag_set *set, struct request_queue *q,
2870 int hctx_idx, int node)
2871{
2f8f1336 2872 struct blk_mq_hw_ctx *hctx = NULL, *tmp;
34d11ffa 2873
2f8f1336
ML
2874 /* reuse dead hctx first */
2875 spin_lock(&q->unused_hctx_lock);
2876 list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) {
2877 if (tmp->numa_node == node) {
2878 hctx = tmp;
2879 break;
2880 }
2881 }
2882 if (hctx)
2883 list_del_init(&hctx->hctx_list);
2884 spin_unlock(&q->unused_hctx_lock);
2885
2886 if (!hctx)
2887 hctx = blk_mq_alloc_hctx(q, set, node);
34d11ffa 2888 if (!hctx)
7c6c5b7c 2889 goto fail;
34d11ffa 2890
7c6c5b7c
ML
2891 if (blk_mq_init_hctx(q, set, hctx, hctx_idx))
2892 goto free_hctx;
34d11ffa
JW
2893
2894 return hctx;
7c6c5b7c
ML
2895
2896 free_hctx:
2897 kobject_put(&hctx->kobj);
2898 fail:
2899 return NULL;
34d11ffa
JW
2900}
2901
868f2f0b
KB
2902static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
2903 struct request_queue *q)
320ae51f 2904{
e01ad46d 2905 int i, j, end;
868f2f0b 2906 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
f14bbe77 2907
ac0d6b92
BVA
2908 if (q->nr_hw_queues < set->nr_hw_queues) {
2909 struct blk_mq_hw_ctx **new_hctxs;
2910
2911 new_hctxs = kcalloc_node(set->nr_hw_queues,
2912 sizeof(*new_hctxs), GFP_KERNEL,
2913 set->numa_node);
2914 if (!new_hctxs)
2915 return;
2916 if (hctxs)
2917 memcpy(new_hctxs, hctxs, q->nr_hw_queues *
2918 sizeof(*hctxs));
2919 q->queue_hw_ctx = new_hctxs;
ac0d6b92
BVA
2920 kfree(hctxs);
2921 hctxs = new_hctxs;
2922 }
2923
fb350e0a
ML
2924 /* protect against switching io scheduler */
2925 mutex_lock(&q->sysfs_lock);
24d2f903 2926 for (i = 0; i < set->nr_hw_queues; i++) {
868f2f0b 2927 int node;
34d11ffa 2928 struct blk_mq_hw_ctx *hctx;
868f2f0b 2929
7d76f856 2930 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], i);
34d11ffa
JW
2931 /*
2932 * If the hw queue has been mapped to another numa node,
2933 * we need to realloc the hctx. If allocation fails, fallback
2934 * to use the previous one.
2935 */
2936 if (hctxs[i] && (hctxs[i]->numa_node == node))
2937 continue;
868f2f0b 2938
34d11ffa
JW
2939 hctx = blk_mq_alloc_and_init_hctx(set, q, i, node);
2940 if (hctx) {
2f8f1336 2941 if (hctxs[i])
34d11ffa 2942 blk_mq_exit_hctx(q, set, hctxs[i], i);
34d11ffa
JW
2943 hctxs[i] = hctx;
2944 } else {
2945 if (hctxs[i])
2946 pr_warn("Allocate new hctx on node %d fails,\
2947 fallback to previous one on node %d\n",
2948 node, hctxs[i]->numa_node);
2949 else
2950 break;
868f2f0b 2951 }
320ae51f 2952 }
e01ad46d
JW
2953 /*
2954 * Increasing nr_hw_queues fails. Free the newly allocated
2955 * hctxs and keep the previous q->nr_hw_queues.
2956 */
2957 if (i != set->nr_hw_queues) {
2958 j = q->nr_hw_queues;
2959 end = i;
2960 } else {
2961 j = i;
2962 end = q->nr_hw_queues;
2963 q->nr_hw_queues = set->nr_hw_queues;
2964 }
34d11ffa 2965
e01ad46d 2966 for (; j < end; j++) {
868f2f0b
KB
2967 struct blk_mq_hw_ctx *hctx = hctxs[j];
2968
2969 if (hctx) {
cc71a6f4
JA
2970 if (hctx->tags)
2971 blk_mq_free_map_and_requests(set, j);
868f2f0b 2972 blk_mq_exit_hctx(q, set, hctx, j);
868f2f0b 2973 hctxs[j] = NULL;
868f2f0b
KB
2974 }
2975 }
fb350e0a 2976 mutex_unlock(&q->sysfs_lock);
868f2f0b
KB
2977}
2978
2979struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
737eb78e
DLM
2980 struct request_queue *q,
2981 bool elevator_init)
868f2f0b 2982{
66841672
ML
2983 /* mark the queue as mq asap */
2984 q->mq_ops = set->ops;
2985
34dbad5d 2986 q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
720b8ccc
SB
2987 blk_mq_poll_stats_bkt,
2988 BLK_MQ_POLL_STATS_BKTS, q);
34dbad5d
OS
2989 if (!q->poll_cb)
2990 goto err_exit;
2991
1db4909e 2992 if (blk_mq_alloc_ctxs(q))
41de54c6 2993 goto err_poll;
868f2f0b 2994
737f98cf
ML
2995 /* init q->mq_kobj and sw queues' kobjects */
2996 blk_mq_sysfs_init(q);
2997
2f8f1336
ML
2998 INIT_LIST_HEAD(&q->unused_hctx_list);
2999 spin_lock_init(&q->unused_hctx_lock);
3000
868f2f0b
KB
3001 blk_mq_realloc_hw_ctxs(set, q);
3002 if (!q->nr_hw_queues)
3003 goto err_hctxs;
320ae51f 3004
287922eb 3005 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
e56f698b 3006 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
320ae51f 3007
a8908939 3008 q->tag_set = set;
320ae51f 3009
94eddfbe 3010 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
cd19181b
ML
3011 if (set->nr_maps > HCTX_TYPE_POLL &&
3012 set->map[HCTX_TYPE_POLL].nr_queues)
6544d229 3013 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
320ae51f 3014
1be036e9
CH
3015 q->sg_reserved_size = INT_MAX;
3016
2849450a 3017 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
6fca6a61
CH
3018 INIT_LIST_HEAD(&q->requeue_list);
3019 spin_lock_init(&q->requeue_lock);
3020
eba71768
JA
3021 q->nr_requests = set->queue_depth;
3022
64f1c21e
JA
3023 /*
3024 * Default to classic polling
3025 */
29ece8b4 3026 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
64f1c21e 3027
24d2f903 3028 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
0d2602ca 3029 blk_mq_add_queue_tag_set(set, q);
4b855ad3 3030 blk_mq_map_swqueue(q);
4593fdbe 3031
737eb78e
DLM
3032 if (elevator_init)
3033 elevator_init_mq(q);
d3484991 3034
320ae51f 3035 return q;
18741986 3036
320ae51f 3037err_hctxs:
868f2f0b 3038 kfree(q->queue_hw_ctx);
73d9c8d4 3039 q->nr_hw_queues = 0;
1db4909e 3040 blk_mq_sysfs_deinit(q);
41de54c6
JS
3041err_poll:
3042 blk_stat_free_callback(q->poll_cb);
3043 q->poll_cb = NULL;
c7de5726
ML
3044err_exit:
3045 q->mq_ops = NULL;
320ae51f
JA
3046 return ERR_PTR(-ENOMEM);
3047}
b62c21b7 3048EXPORT_SYMBOL(blk_mq_init_allocated_queue);
320ae51f 3049
c7e2d94b
ML
3050/* tags can _not_ be used after returning from blk_mq_exit_queue */
3051void blk_mq_exit_queue(struct request_queue *q)
320ae51f 3052{
624dbe47 3053 struct blk_mq_tag_set *set = q->tag_set;
320ae51f 3054
0d2602ca 3055 blk_mq_del_queue_tag_set(q);
624dbe47 3056 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
320ae51f 3057}
320ae51f 3058
a5164405
JA
3059static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
3060{
3061 int i;
3062
cc71a6f4 3063 for (i = 0; i < set->nr_hw_queues; i++)
03b63b02 3064 if (!__blk_mq_alloc_map_and_request(set, i))
a5164405 3065 goto out_unwind;
a5164405
JA
3066
3067 return 0;
3068
3069out_unwind:
3070 while (--i >= 0)
2e194422 3071 blk_mq_free_map_and_requests(set, i);
a5164405 3072
a5164405
JA
3073 return -ENOMEM;
3074}
3075
3076/*
3077 * Allocate the request maps associated with this tag_set. Note that this
3078 * may reduce the depth asked for, if memory is tight. set->queue_depth
3079 * will be updated to reflect the allocated depth.
3080 */
79fab528 3081static int blk_mq_alloc_map_and_requests(struct blk_mq_tag_set *set)
a5164405
JA
3082{
3083 unsigned int depth;
3084 int err;
3085
3086 depth = set->queue_depth;
3087 do {
3088 err = __blk_mq_alloc_rq_maps(set);
3089 if (!err)
3090 break;
3091
3092 set->queue_depth >>= 1;
3093 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
3094 err = -ENOMEM;
3095 break;
3096 }
3097 } while (set->queue_depth);
3098
3099 if (!set->queue_depth || err) {
3100 pr_err("blk-mq: failed to allocate request map\n");
3101 return -ENOMEM;
3102 }
3103
3104 if (depth != set->queue_depth)
3105 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
3106 depth, set->queue_depth);
3107
3108 return 0;
3109}
3110
ebe8bddb
OS
3111static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
3112{
6e66b493
BVA
3113 /*
3114 * blk_mq_map_queues() and multiple .map_queues() implementations
3115 * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the
3116 * number of hardware queues.
3117 */
3118 if (set->nr_maps == 1)
3119 set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues;
3120
59388702 3121 if (set->ops->map_queues && !is_kdump_kernel()) {
b3c661b1
JA
3122 int i;
3123
7d4901a9
ML
3124 /*
3125 * transport .map_queues is usually done in the following
3126 * way:
3127 *
3128 * for (queue = 0; queue < set->nr_hw_queues; queue++) {
3129 * mask = get_cpu_mask(queue)
3130 * for_each_cpu(cpu, mask)
b3c661b1 3131 * set->map[x].mq_map[cpu] = queue;
7d4901a9
ML
3132 * }
3133 *
3134 * When we need to remap, the table has to be cleared for
3135 * killing stale mapping since one CPU may not be mapped
3136 * to any hw queue.
3137 */
b3c661b1
JA
3138 for (i = 0; i < set->nr_maps; i++)
3139 blk_mq_clear_mq_map(&set->map[i]);
7d4901a9 3140
ebe8bddb 3141 return set->ops->map_queues(set);
b3c661b1
JA
3142 } else {
3143 BUG_ON(set->nr_maps > 1);
7d76f856 3144 return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
b3c661b1 3145 }
ebe8bddb
OS
3146}
3147
f7e76dbc
BVA
3148static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set,
3149 int cur_nr_hw_queues, int new_nr_hw_queues)
3150{
3151 struct blk_mq_tags **new_tags;
3152
3153 if (cur_nr_hw_queues >= new_nr_hw_queues)
3154 return 0;
3155
3156 new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *),
3157 GFP_KERNEL, set->numa_node);
3158 if (!new_tags)
3159 return -ENOMEM;
3160
3161 if (set->tags)
3162 memcpy(new_tags, set->tags, cur_nr_hw_queues *
3163 sizeof(*set->tags));
3164 kfree(set->tags);
3165 set->tags = new_tags;
3166 set->nr_hw_queues = new_nr_hw_queues;
3167
3168 return 0;
3169}
3170
a4391c64
JA
3171/*
3172 * Alloc a tag set to be associated with one or more request queues.
3173 * May fail with EINVAL for various error conditions. May adjust the
c018c84f 3174 * requested depth down, if it's too large. In that case, the set
a4391c64
JA
3175 * value will be stored in set->queue_depth.
3176 */
24d2f903
CH
3177int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
3178{
b3c661b1 3179 int i, ret;
da695ba2 3180
205fb5f5
BVA
3181 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
3182
24d2f903
CH
3183 if (!set->nr_hw_queues)
3184 return -EINVAL;
a4391c64 3185 if (!set->queue_depth)
24d2f903
CH
3186 return -EINVAL;
3187 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
3188 return -EINVAL;
3189
7d7e0f90 3190 if (!set->ops->queue_rq)
24d2f903
CH
3191 return -EINVAL;
3192
de148297
ML
3193 if (!set->ops->get_budget ^ !set->ops->put_budget)
3194 return -EINVAL;
3195
a4391c64
JA
3196 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
3197 pr_info("blk-mq: reduced tag depth to %u\n",
3198 BLK_MQ_MAX_DEPTH);
3199 set->queue_depth = BLK_MQ_MAX_DEPTH;
3200 }
24d2f903 3201
b3c661b1
JA
3202 if (!set->nr_maps)
3203 set->nr_maps = 1;
3204 else if (set->nr_maps > HCTX_MAX_TYPES)
3205 return -EINVAL;
3206
6637fadf
SL
3207 /*
3208 * If a crashdump is active, then we are potentially in a very
3209 * memory constrained environment. Limit us to 1 queue and
3210 * 64 tags to prevent using too much memory.
3211 */
3212 if (is_kdump_kernel()) {
3213 set->nr_hw_queues = 1;
59388702 3214 set->nr_maps = 1;
6637fadf
SL
3215 set->queue_depth = min(64U, set->queue_depth);
3216 }
868f2f0b 3217 /*
392546ae
JA
3218 * There is no use for more h/w queues than cpus if we just have
3219 * a single map
868f2f0b 3220 */
392546ae 3221 if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids)
868f2f0b 3222 set->nr_hw_queues = nr_cpu_ids;
6637fadf 3223
f7e76dbc 3224 if (blk_mq_realloc_tag_set_tags(set, 0, set->nr_hw_queues) < 0)
a5164405 3225 return -ENOMEM;
24d2f903 3226
da695ba2 3227 ret = -ENOMEM;
b3c661b1
JA
3228 for (i = 0; i < set->nr_maps; i++) {
3229 set->map[i].mq_map = kcalloc_node(nr_cpu_ids,
07b35eb5 3230 sizeof(set->map[i].mq_map[0]),
b3c661b1
JA
3231 GFP_KERNEL, set->numa_node);
3232 if (!set->map[i].mq_map)
3233 goto out_free_mq_map;
59388702 3234 set->map[i].nr_queues = is_kdump_kernel() ? 1 : set->nr_hw_queues;
b3c661b1 3235 }
bdd17e75 3236
ebe8bddb 3237 ret = blk_mq_update_queue_map(set);
da695ba2
CH
3238 if (ret)
3239 goto out_free_mq_map;
3240
79fab528 3241 ret = blk_mq_alloc_map_and_requests(set);
da695ba2 3242 if (ret)
bdd17e75 3243 goto out_free_mq_map;
24d2f903 3244
0d2602ca
JA
3245 mutex_init(&set->tag_list_lock);
3246 INIT_LIST_HEAD(&set->tag_list);
3247
24d2f903 3248 return 0;
bdd17e75
CH
3249
3250out_free_mq_map:
b3c661b1
JA
3251 for (i = 0; i < set->nr_maps; i++) {
3252 kfree(set->map[i].mq_map);
3253 set->map[i].mq_map = NULL;
3254 }
5676e7b6
RE
3255 kfree(set->tags);
3256 set->tags = NULL;
da695ba2 3257 return ret;
24d2f903
CH
3258}
3259EXPORT_SYMBOL(blk_mq_alloc_tag_set);
3260
3261void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
3262{
b3c661b1 3263 int i, j;
24d2f903 3264
f7e76dbc 3265 for (i = 0; i < set->nr_hw_queues; i++)
cc71a6f4 3266 blk_mq_free_map_and_requests(set, i);
484b4061 3267
b3c661b1
JA
3268 for (j = 0; j < set->nr_maps; j++) {
3269 kfree(set->map[j].mq_map);
3270 set->map[j].mq_map = NULL;
3271 }
bdd17e75 3272
981bd189 3273 kfree(set->tags);
5676e7b6 3274 set->tags = NULL;
24d2f903
CH
3275}
3276EXPORT_SYMBOL(blk_mq_free_tag_set);
3277
e3a2b3f9
JA
3278int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
3279{
3280 struct blk_mq_tag_set *set = q->tag_set;
3281 struct blk_mq_hw_ctx *hctx;
3282 int i, ret;
3283
bd166ef1 3284 if (!set)
e3a2b3f9
JA
3285 return -EINVAL;
3286
e5fa8140
AZ
3287 if (q->nr_requests == nr)
3288 return 0;
3289
70f36b60 3290 blk_mq_freeze_queue(q);
24f5a90f 3291 blk_mq_quiesce_queue(q);
70f36b60 3292
e3a2b3f9
JA
3293 ret = 0;
3294 queue_for_each_hw_ctx(q, hctx, i) {
e9137d4b
KB
3295 if (!hctx->tags)
3296 continue;
bd166ef1
JA
3297 /*
3298 * If we're using an MQ scheduler, just update the scheduler
3299 * queue depth. This is similar to what the old code would do.
3300 */
70f36b60 3301 if (!hctx->sched_tags) {
c2e82a23 3302 ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
70f36b60
JA
3303 false);
3304 } else {
3305 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
3306 nr, true);
3307 }
e3a2b3f9
JA
3308 if (ret)
3309 break;
77f1e0a5
JA
3310 if (q->elevator && q->elevator->type->ops.depth_updated)
3311 q->elevator->type->ops.depth_updated(hctx);
e3a2b3f9
JA
3312 }
3313
3314 if (!ret)
3315 q->nr_requests = nr;
3316
24f5a90f 3317 blk_mq_unquiesce_queue(q);
70f36b60 3318 blk_mq_unfreeze_queue(q);
70f36b60 3319
e3a2b3f9
JA
3320 return ret;
3321}
3322
d48ece20
JW
3323/*
3324 * request_queue and elevator_type pair.
3325 * It is just used by __blk_mq_update_nr_hw_queues to cache
3326 * the elevator_type associated with a request_queue.
3327 */
3328struct blk_mq_qe_pair {
3329 struct list_head node;
3330 struct request_queue *q;
3331 struct elevator_type *type;
3332};
3333
3334/*
3335 * Cache the elevator_type in qe pair list and switch the
3336 * io scheduler to 'none'
3337 */
3338static bool blk_mq_elv_switch_none(struct list_head *head,
3339 struct request_queue *q)
3340{
3341 struct blk_mq_qe_pair *qe;
3342
3343 if (!q->elevator)
3344 return true;
3345
3346 qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
3347 if (!qe)
3348 return false;
3349
3350 INIT_LIST_HEAD(&qe->node);
3351 qe->q = q;
3352 qe->type = q->elevator->type;
3353 list_add(&qe->node, head);
3354
3355 mutex_lock(&q->sysfs_lock);
3356 /*
3357 * After elevator_switch_mq, the previous elevator_queue will be
3358 * released by elevator_release. The reference of the io scheduler
3359 * module get by elevator_get will also be put. So we need to get
3360 * a reference of the io scheduler module here to prevent it to be
3361 * removed.
3362 */
3363 __module_get(qe->type->elevator_owner);
3364 elevator_switch_mq(q, NULL);
3365 mutex_unlock(&q->sysfs_lock);
3366
3367 return true;
3368}
3369
3370static void blk_mq_elv_switch_back(struct list_head *head,
3371 struct request_queue *q)
3372{
3373 struct blk_mq_qe_pair *qe;
3374 struct elevator_type *t = NULL;
3375
3376 list_for_each_entry(qe, head, node)
3377 if (qe->q == q) {
3378 t = qe->type;
3379 break;
3380 }
3381
3382 if (!t)
3383 return;
3384
3385 list_del(&qe->node);
3386 kfree(qe);
3387
3388 mutex_lock(&q->sysfs_lock);
3389 elevator_switch_mq(q, t);
3390 mutex_unlock(&q->sysfs_lock);
3391}
3392
e4dc2b32
KB
3393static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
3394 int nr_hw_queues)
868f2f0b
KB
3395{
3396 struct request_queue *q;
d48ece20 3397 LIST_HEAD(head);
e01ad46d 3398 int prev_nr_hw_queues;
868f2f0b 3399
705cda97
BVA
3400 lockdep_assert_held(&set->tag_list_lock);
3401
392546ae 3402 if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids)
868f2f0b
KB
3403 nr_hw_queues = nr_cpu_ids;
3404 if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues)
3405 return;
3406
3407 list_for_each_entry(q, &set->tag_list, tag_set_list)
3408 blk_mq_freeze_queue(q);
d48ece20
JW
3409 /*
3410 * Switch IO scheduler to 'none', cleaning up the data associated
3411 * with the previous scheduler. We will switch back once we are done
3412 * updating the new sw to hw queue mappings.
3413 */
3414 list_for_each_entry(q, &set->tag_list, tag_set_list)
3415 if (!blk_mq_elv_switch_none(&head, q))
3416 goto switch_back;
868f2f0b 3417
477e19de
JW
3418 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3419 blk_mq_debugfs_unregister_hctxs(q);
3420 blk_mq_sysfs_unregister(q);
3421 }
3422
a2584e43 3423 prev_nr_hw_queues = set->nr_hw_queues;
f7e76dbc
BVA
3424 if (blk_mq_realloc_tag_set_tags(set, set->nr_hw_queues, nr_hw_queues) <
3425 0)
3426 goto reregister;
3427
868f2f0b 3428 set->nr_hw_queues = nr_hw_queues;
e01ad46d 3429fallback:
aa880ad6 3430 blk_mq_update_queue_map(set);
868f2f0b
KB
3431 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3432 blk_mq_realloc_hw_ctxs(set, q);
e01ad46d
JW
3433 if (q->nr_hw_queues != set->nr_hw_queues) {
3434 pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
3435 nr_hw_queues, prev_nr_hw_queues);
3436 set->nr_hw_queues = prev_nr_hw_queues;
7d76f856 3437 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
e01ad46d
JW
3438 goto fallback;
3439 }
477e19de
JW
3440 blk_mq_map_swqueue(q);
3441 }
3442
f7e76dbc 3443reregister:
477e19de
JW
3444 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3445 blk_mq_sysfs_register(q);
3446 blk_mq_debugfs_register_hctxs(q);
868f2f0b
KB
3447 }
3448
d48ece20
JW
3449switch_back:
3450 list_for_each_entry(q, &set->tag_list, tag_set_list)
3451 blk_mq_elv_switch_back(&head, q);
3452
868f2f0b
KB
3453 list_for_each_entry(q, &set->tag_list, tag_set_list)
3454 blk_mq_unfreeze_queue(q);
3455}
e4dc2b32
KB
3456
3457void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
3458{
3459 mutex_lock(&set->tag_list_lock);
3460 __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
3461 mutex_unlock(&set->tag_list_lock);
3462}
868f2f0b
KB
3463EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
3464
34dbad5d
OS
3465/* Enable polling stats and return whether they were already enabled. */
3466static bool blk_poll_stats_enable(struct request_queue *q)
3467{
3468 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
7dfdbc73 3469 blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q))
34dbad5d
OS
3470 return true;
3471 blk_stat_add_callback(q, q->poll_cb);
3472 return false;
3473}
3474
3475static void blk_mq_poll_stats_start(struct request_queue *q)
3476{
3477 /*
3478 * We don't arm the callback if polling stats are not enabled or the
3479 * callback is already active.
3480 */
3481 if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
3482 blk_stat_is_active(q->poll_cb))
3483 return;
3484
3485 blk_stat_activate_msecs(q->poll_cb, 100);
3486}
3487
3488static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
3489{
3490 struct request_queue *q = cb->data;
720b8ccc 3491 int bucket;
34dbad5d 3492
720b8ccc
SB
3493 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
3494 if (cb->stat[bucket].nr_samples)
3495 q->poll_stat[bucket] = cb->stat[bucket];
3496 }
34dbad5d
OS
3497}
3498
64f1c21e 3499static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
64f1c21e
JA
3500 struct request *rq)
3501{
64f1c21e 3502 unsigned long ret = 0;
720b8ccc 3503 int bucket;
64f1c21e
JA
3504
3505 /*
3506 * If stats collection isn't on, don't sleep but turn it on for
3507 * future users
3508 */
34dbad5d 3509 if (!blk_poll_stats_enable(q))
64f1c21e
JA
3510 return 0;
3511
64f1c21e
JA
3512 /*
3513 * As an optimistic guess, use half of the mean service time
3514 * for this type of request. We can (and should) make this smarter.
3515 * For instance, if the completion latencies are tight, we can
3516 * get closer than just half the mean. This is especially
3517 * important on devices where the completion latencies are longer
720b8ccc
SB
3518 * than ~10 usec. We do use the stats for the relevant IO size
3519 * if available which does lead to better estimates.
64f1c21e 3520 */
720b8ccc
SB
3521 bucket = blk_mq_poll_stats_bkt(rq);
3522 if (bucket < 0)
3523 return ret;
3524
3525 if (q->poll_stat[bucket].nr_samples)
3526 ret = (q->poll_stat[bucket].mean + 1) / 2;
64f1c21e
JA
3527
3528 return ret;
3529}
3530
06426adf
JA
3531static bool blk_mq_poll_hybrid_sleep(struct request_queue *q,
3532 struct request *rq)
3533{
3534 struct hrtimer_sleeper hs;
3535 enum hrtimer_mode mode;
64f1c21e 3536 unsigned int nsecs;
06426adf
JA
3537 ktime_t kt;
3538
76a86f9d 3539 if (rq->rq_flags & RQF_MQ_POLL_SLEPT)
64f1c21e
JA
3540 return false;
3541
3542 /*
1052b8ac 3543 * If we get here, hybrid polling is enabled. Hence poll_nsec can be:
64f1c21e 3544 *
64f1c21e
JA
3545 * 0: use half of prev avg
3546 * >0: use this specific value
3547 */
1052b8ac 3548 if (q->poll_nsec > 0)
64f1c21e
JA
3549 nsecs = q->poll_nsec;
3550 else
cae740a0 3551 nsecs = blk_mq_poll_nsecs(q, rq);
64f1c21e
JA
3552
3553 if (!nsecs)
06426adf
JA
3554 return false;
3555
76a86f9d 3556 rq->rq_flags |= RQF_MQ_POLL_SLEPT;
06426adf
JA
3557
3558 /*
3559 * This will be replaced with the stats tracking code, using
3560 * 'avg_completion_time / 2' as the pre-sleep target.
3561 */
8b0e1953 3562 kt = nsecs;
06426adf
JA
3563
3564 mode = HRTIMER_MODE_REL;
dbc1625f 3565 hrtimer_init_sleeper_on_stack(&hs, CLOCK_MONOTONIC, mode);
06426adf
JA
3566 hrtimer_set_expires(&hs.timer, kt);
3567
06426adf 3568 do {
5a61c363 3569 if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE)
06426adf
JA
3570 break;
3571 set_current_state(TASK_UNINTERRUPTIBLE);
9dd8813e 3572 hrtimer_sleeper_start_expires(&hs, mode);
06426adf
JA
3573 if (hs.task)
3574 io_schedule();
3575 hrtimer_cancel(&hs.timer);
3576 mode = HRTIMER_MODE_ABS;
3577 } while (hs.task && !signal_pending(current));
3578
3579 __set_current_state(TASK_RUNNING);
3580 destroy_hrtimer_on_stack(&hs.timer);
3581 return true;
3582}
3583
1052b8ac
JA
3584static bool blk_mq_poll_hybrid(struct request_queue *q,
3585 struct blk_mq_hw_ctx *hctx, blk_qc_t cookie)
bbd7bb70 3586{
1052b8ac
JA
3587 struct request *rq;
3588
29ece8b4 3589 if (q->poll_nsec == BLK_MQ_POLL_CLASSIC)
1052b8ac
JA
3590 return false;
3591
3592 if (!blk_qc_t_is_internal(cookie))
3593 rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie));
3594 else {
3595 rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie));
3596 /*
3597 * With scheduling, if the request has completed, we'll
3598 * get a NULL return here, as we clear the sched tag when
3599 * that happens. The request still remains valid, like always,
3600 * so we should be safe with just the NULL check.
3601 */
3602 if (!rq)
3603 return false;
3604 }
3605
cae740a0 3606 return blk_mq_poll_hybrid_sleep(q, rq);
1052b8ac
JA
3607}
3608
529262d5
CH
3609/**
3610 * blk_poll - poll for IO completions
3611 * @q: the queue
3612 * @cookie: cookie passed back at IO submission time
3613 * @spin: whether to spin for completions
3614 *
3615 * Description:
3616 * Poll for completions on the passed in queue. Returns number of
3617 * completed entries found. If @spin is true, then blk_poll will continue
3618 * looping until at least one completion is found, unless the task is
3619 * otherwise marked running (or we need to reschedule).
3620 */
3621int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin)
1052b8ac
JA
3622{
3623 struct blk_mq_hw_ctx *hctx;
bbd7bb70
JA
3624 long state;
3625
529262d5
CH
3626 if (!blk_qc_t_valid(cookie) ||
3627 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
1052b8ac
JA
3628 return 0;
3629
529262d5
CH
3630 if (current->plug)
3631 blk_flush_plug_list(current->plug, false);
3632
1052b8ac
JA
3633 hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
3634
06426adf
JA
3635 /*
3636 * If we sleep, have the caller restart the poll loop to reset
3637 * the state. Like for the other success return cases, the
3638 * caller is responsible for checking if the IO completed. If
3639 * the IO isn't complete, we'll get called again and will go
3640 * straight to the busy poll loop.
3641 */
1052b8ac 3642 if (blk_mq_poll_hybrid(q, hctx, cookie))
85f4d4b6 3643 return 1;
06426adf 3644
bbd7bb70
JA
3645 hctx->poll_considered++;
3646
3647 state = current->state;
aa61bec3 3648 do {
bbd7bb70
JA
3649 int ret;
3650
3651 hctx->poll_invoked++;
3652
9743139c 3653 ret = q->mq_ops->poll(hctx);
bbd7bb70
JA
3654 if (ret > 0) {
3655 hctx->poll_success++;
849a3700 3656 __set_current_state(TASK_RUNNING);
85f4d4b6 3657 return ret;
bbd7bb70
JA
3658 }
3659
3660 if (signal_pending_state(state, current))
849a3700 3661 __set_current_state(TASK_RUNNING);
bbd7bb70
JA
3662
3663 if (current->state == TASK_RUNNING)
85f4d4b6 3664 return 1;
0a1b8b87 3665 if (ret < 0 || !spin)
bbd7bb70
JA
3666 break;
3667 cpu_relax();
aa61bec3 3668 } while (!need_resched());
bbd7bb70 3669
67b4110f 3670 __set_current_state(TASK_RUNNING);
85f4d4b6 3671 return 0;
bbd7bb70 3672}
529262d5 3673EXPORT_SYMBOL_GPL(blk_poll);
bbd7bb70 3674
9cf2bab6
JA
3675unsigned int blk_mq_rq_cpu(struct request *rq)
3676{
3677 return rq->mq_ctx->cpu;
3678}
3679EXPORT_SYMBOL(blk_mq_rq_cpu);
3680
320ae51f
JA
3681static int __init blk_mq_init(void)
3682{
9467f859
TG
3683 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
3684 blk_mq_hctx_notify_dead);
320ae51f
JA
3685 return 0;
3686}
3687subsys_initcall(blk_mq_init);