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