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