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