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