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