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