blk-mq: add support for carrying internal tag information in blk_qc_t
[linux-2.6-block.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>
23#include <linux/delay.h>
aedcd72f 24#include <linux/crash_dump.h>
88c7b2b7 25#include <linux/prefetch.h>
320ae51f
JA
26
27#include <trace/events/block.h>
28
29#include <linux/blk-mq.h>
30#include "blk.h"
31#include "blk-mq.h"
32#include "blk-mq-tag.h"
cf43e6be 33#include "blk-stat.h"
87760e5e 34#include "blk-wbt.h"
320ae51f
JA
35
36static DEFINE_MUTEX(all_q_mutex);
37static LIST_HEAD(all_q_list);
38
320ae51f
JA
39/*
40 * Check if any of the ctx's have pending work in this hardware queue
41 */
42static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
43{
88459642 44 return sbitmap_any_bit_set(&hctx->ctx_map);
1429d7c9
JA
45}
46
320ae51f
JA
47/*
48 * Mark this ctx as having pending work in this hardware queue
49 */
50static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
51 struct blk_mq_ctx *ctx)
52{
88459642
OS
53 if (!sbitmap_test_bit(&hctx->ctx_map, ctx->index_hw))
54 sbitmap_set_bit(&hctx->ctx_map, ctx->index_hw);
1429d7c9
JA
55}
56
57static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
58 struct blk_mq_ctx *ctx)
59{
88459642 60 sbitmap_clear_bit(&hctx->ctx_map, ctx->index_hw);
320ae51f
JA
61}
62
b4c6a028 63void blk_mq_freeze_queue_start(struct request_queue *q)
43a5e4e2 64{
4ecd4fef 65 int freeze_depth;
cddd5d17 66
4ecd4fef
CH
67 freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
68 if (freeze_depth == 1) {
3ef28e83 69 percpu_ref_kill(&q->q_usage_counter);
b94ec296 70 blk_mq_run_hw_queues(q, false);
cddd5d17 71 }
f3af020b 72}
b4c6a028 73EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start);
f3af020b
TH
74
75static void blk_mq_freeze_queue_wait(struct request_queue *q)
76{
3ef28e83 77 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
43a5e4e2
ML
78}
79
f3af020b
TH
80/*
81 * Guarantee no request is in use, so we can change any data structure of
82 * the queue afterward.
83 */
3ef28e83 84void blk_freeze_queue(struct request_queue *q)
f3af020b 85{
3ef28e83
DW
86 /*
87 * In the !blk_mq case we are only calling this to kill the
88 * q_usage_counter, otherwise this increases the freeze depth
89 * and waits for it to return to zero. For this reason there is
90 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
91 * exported to drivers as the only user for unfreeze is blk_mq.
92 */
f3af020b
TH
93 blk_mq_freeze_queue_start(q);
94 blk_mq_freeze_queue_wait(q);
95}
3ef28e83
DW
96
97void blk_mq_freeze_queue(struct request_queue *q)
98{
99 /*
100 * ...just an alias to keep freeze and unfreeze actions balanced
101 * in the blk_mq_* namespace
102 */
103 blk_freeze_queue(q);
104}
c761d96b 105EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
f3af020b 106
b4c6a028 107void blk_mq_unfreeze_queue(struct request_queue *q)
320ae51f 108{
4ecd4fef 109 int freeze_depth;
320ae51f 110
4ecd4fef
CH
111 freeze_depth = atomic_dec_return(&q->mq_freeze_depth);
112 WARN_ON_ONCE(freeze_depth < 0);
113 if (!freeze_depth) {
3ef28e83 114 percpu_ref_reinit(&q->q_usage_counter);
320ae51f 115 wake_up_all(&q->mq_freeze_wq);
add703fd 116 }
320ae51f 117}
b4c6a028 118EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
320ae51f 119
6a83e74d
BVA
120/**
121 * blk_mq_quiesce_queue() - wait until all ongoing queue_rq calls have finished
122 * @q: request queue.
123 *
124 * Note: this function does not prevent that the struct request end_io()
125 * callback function is invoked. Additionally, it is not prevented that
126 * new queue_rq() calls occur unless the queue has been stopped first.
127 */
128void blk_mq_quiesce_queue(struct request_queue *q)
129{
130 struct blk_mq_hw_ctx *hctx;
131 unsigned int i;
132 bool rcu = false;
133
134 blk_mq_stop_hw_queues(q);
135
136 queue_for_each_hw_ctx(q, hctx, i) {
137 if (hctx->flags & BLK_MQ_F_BLOCKING)
138 synchronize_srcu(&hctx->queue_rq_srcu);
139 else
140 rcu = true;
141 }
142 if (rcu)
143 synchronize_rcu();
144}
145EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
146
aed3ea94
JA
147void blk_mq_wake_waiters(struct request_queue *q)
148{
149 struct blk_mq_hw_ctx *hctx;
150 unsigned int i;
151
152 queue_for_each_hw_ctx(q, hctx, i)
153 if (blk_mq_hw_queue_mapped(hctx))
154 blk_mq_tag_wakeup_all(hctx->tags, true);
3fd5940c
KB
155
156 /*
157 * If we are called because the queue has now been marked as
158 * dying, we need to ensure that processes currently waiting on
159 * the queue are notified as well.
160 */
161 wake_up_all(&q->mq_freeze_wq);
aed3ea94
JA
162}
163
320ae51f
JA
164bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
165{
166 return blk_mq_has_free_tags(hctx->tags);
167}
168EXPORT_SYMBOL(blk_mq_can_queue);
169
2c3ad667
JA
170void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
171 struct request *rq, unsigned int op)
320ae51f 172{
af76e555
CH
173 INIT_LIST_HEAD(&rq->queuelist);
174 /* csd/requeue_work/fifo_time is initialized before use */
175 rq->q = q;
320ae51f 176 rq->mq_ctx = ctx;
ef295ecf 177 rq->cmd_flags = op;
e8064021
CH
178 if (blk_queue_io_stat(q))
179 rq->rq_flags |= RQF_IO_STAT;
af76e555
CH
180 /* do not touch atomic flags, it needs atomic ops against the timer */
181 rq->cpu = -1;
af76e555
CH
182 INIT_HLIST_NODE(&rq->hash);
183 RB_CLEAR_NODE(&rq->rb_node);
af76e555
CH
184 rq->rq_disk = NULL;
185 rq->part = NULL;
3ee32372 186 rq->start_time = jiffies;
af76e555
CH
187#ifdef CONFIG_BLK_CGROUP
188 rq->rl = NULL;
0fec08b4 189 set_start_time_ns(rq);
af76e555
CH
190 rq->io_start_time_ns = 0;
191#endif
192 rq->nr_phys_segments = 0;
193#if defined(CONFIG_BLK_DEV_INTEGRITY)
194 rq->nr_integrity_segments = 0;
195#endif
af76e555
CH
196 rq->special = NULL;
197 /* tag was already set */
198 rq->errors = 0;
af76e555 199
6f4a1626
TB
200 rq->cmd = rq->__cmd;
201
af76e555
CH
202 rq->extra_len = 0;
203 rq->sense_len = 0;
204 rq->resid_len = 0;
205 rq->sense = NULL;
206
af76e555 207 INIT_LIST_HEAD(&rq->timeout_list);
f6be4fb4
JA
208 rq->timeout = 0;
209
af76e555
CH
210 rq->end_io = NULL;
211 rq->end_io_data = NULL;
212 rq->next_rq = NULL;
213
ef295ecf 214 ctx->rq_dispatched[op_is_sync(op)]++;
320ae51f 215}
2c3ad667 216EXPORT_SYMBOL_GPL(blk_mq_rq_ctx_init);
320ae51f 217
2c3ad667
JA
218struct request *__blk_mq_alloc_request(struct blk_mq_alloc_data *data,
219 unsigned int op)
5dee8577
CH
220{
221 struct request *rq;
222 unsigned int tag;
223
cb96a42c 224 tag = blk_mq_get_tag(data);
5dee8577 225 if (tag != BLK_MQ_TAG_FAIL) {
cb96a42c 226 rq = data->hctx->tags->rqs[tag];
5dee8577 227
cb96a42c 228 if (blk_mq_tag_busy(data->hctx)) {
e8064021 229 rq->rq_flags = RQF_MQ_INFLIGHT;
cb96a42c 230 atomic_inc(&data->hctx->nr_active);
5dee8577
CH
231 }
232
233 rq->tag = tag;
ef295ecf 234 blk_mq_rq_ctx_init(data->q, data->ctx, rq, op);
5dee8577
CH
235 return rq;
236 }
237
238 return NULL;
239}
2c3ad667 240EXPORT_SYMBOL_GPL(__blk_mq_alloc_request);
5dee8577 241
6f3b0e8b
CH
242struct request *blk_mq_alloc_request(struct request_queue *q, int rw,
243 unsigned int flags)
320ae51f 244{
d852564f
CH
245 struct blk_mq_ctx *ctx;
246 struct blk_mq_hw_ctx *hctx;
320ae51f 247 struct request *rq;
cb96a42c 248 struct blk_mq_alloc_data alloc_data;
a492f075 249 int ret;
320ae51f 250
6f3b0e8b 251 ret = blk_queue_enter(q, flags & BLK_MQ_REQ_NOWAIT);
a492f075
JL
252 if (ret)
253 return ERR_PTR(ret);
320ae51f 254
d852564f 255 ctx = blk_mq_get_ctx(q);
7d7e0f90 256 hctx = blk_mq_map_queue(q, ctx->cpu);
6f3b0e8b 257 blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx);
ef295ecf 258 rq = __blk_mq_alloc_request(&alloc_data, rw);
d852564f 259 blk_mq_put_ctx(ctx);
841bac2c 260
c76541a9 261 if (!rq) {
3ef28e83 262 blk_queue_exit(q);
a492f075 263 return ERR_PTR(-EWOULDBLOCK);
c76541a9 264 }
0c4de0f3
CH
265
266 rq->__data_len = 0;
267 rq->__sector = (sector_t) -1;
268 rq->bio = rq->biotail = NULL;
320ae51f
JA
269 return rq;
270}
4bb659b1 271EXPORT_SYMBOL(blk_mq_alloc_request);
320ae51f 272
1f5bd336
ML
273struct request *blk_mq_alloc_request_hctx(struct request_queue *q, int rw,
274 unsigned int flags, unsigned int hctx_idx)
275{
276 struct blk_mq_hw_ctx *hctx;
277 struct blk_mq_ctx *ctx;
278 struct request *rq;
279 struct blk_mq_alloc_data alloc_data;
280 int ret;
281
282 /*
283 * If the tag allocator sleeps we could get an allocation for a
284 * different hardware context. No need to complicate the low level
285 * allocator for this for the rare use case of a command tied to
286 * a specific queue.
287 */
288 if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)))
289 return ERR_PTR(-EINVAL);
290
291 if (hctx_idx >= q->nr_hw_queues)
292 return ERR_PTR(-EIO);
293
294 ret = blk_queue_enter(q, true);
295 if (ret)
296 return ERR_PTR(ret);
297
c8712c6a
CH
298 /*
299 * Check if the hardware context is actually mapped to anything.
300 * If not tell the caller that it should skip this queue.
301 */
1f5bd336 302 hctx = q->queue_hw_ctx[hctx_idx];
c8712c6a
CH
303 if (!blk_mq_hw_queue_mapped(hctx)) {
304 ret = -EXDEV;
305 goto out_queue_exit;
306 }
1f5bd336
ML
307 ctx = __blk_mq_get_ctx(q, cpumask_first(hctx->cpumask));
308
309 blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx);
ef295ecf 310 rq = __blk_mq_alloc_request(&alloc_data, rw);
1f5bd336 311 if (!rq) {
c8712c6a
CH
312 ret = -EWOULDBLOCK;
313 goto out_queue_exit;
1f5bd336
ML
314 }
315
316 return rq;
c8712c6a
CH
317
318out_queue_exit:
319 blk_queue_exit(q);
320 return ERR_PTR(ret);
1f5bd336
ML
321}
322EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
323
2c3ad667
JA
324void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
325 struct request *rq)
320ae51f
JA
326{
327 const int tag = rq->tag;
328 struct request_queue *q = rq->q;
329
e8064021 330 if (rq->rq_flags & RQF_MQ_INFLIGHT)
0d2602ca 331 atomic_dec(&hctx->nr_active);
87760e5e
JA
332
333 wbt_done(q->rq_wb, &rq->issue_stat);
e8064021 334 rq->rq_flags = 0;
0d2602ca 335
af76e555 336 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
06426adf 337 clear_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags);
4941115b 338 blk_mq_put_tag(hctx, hctx->tags, ctx, tag);
3ef28e83 339 blk_queue_exit(q);
320ae51f
JA
340}
341
16a3c2a7
JA
342static void blk_mq_free_hctx_request(struct blk_mq_hw_ctx *hctx,
343 struct request *rq)
320ae51f
JA
344{
345 struct blk_mq_ctx *ctx = rq->mq_ctx;
320ae51f
JA
346
347 ctx->rq_completed[rq_is_sync(rq)]++;
320ae51f 348 __blk_mq_free_request(hctx, ctx, rq);
7c7f2f2b 349}
7c7f2f2b
JA
350
351void blk_mq_free_request(struct request *rq)
352{
7d7e0f90 353 blk_mq_free_hctx_request(blk_mq_map_queue(rq->q, rq->mq_ctx->cpu), rq);
320ae51f 354}
1a3b595a 355EXPORT_SYMBOL_GPL(blk_mq_free_request);
320ae51f 356
c8a446ad 357inline void __blk_mq_end_request(struct request *rq, int error)
320ae51f 358{
0d11e6ac
ML
359 blk_account_io_done(rq);
360
91b63639 361 if (rq->end_io) {
87760e5e 362 wbt_done(rq->q->rq_wb, &rq->issue_stat);
320ae51f 363 rq->end_io(rq, error);
91b63639
CH
364 } else {
365 if (unlikely(blk_bidi_rq(rq)))
366 blk_mq_free_request(rq->next_rq);
320ae51f 367 blk_mq_free_request(rq);
91b63639 368 }
320ae51f 369}
c8a446ad 370EXPORT_SYMBOL(__blk_mq_end_request);
63151a44 371
c8a446ad 372void blk_mq_end_request(struct request *rq, int error)
63151a44
CH
373{
374 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
375 BUG();
c8a446ad 376 __blk_mq_end_request(rq, error);
63151a44 377}
c8a446ad 378EXPORT_SYMBOL(blk_mq_end_request);
320ae51f 379
30a91cb4 380static void __blk_mq_complete_request_remote(void *data)
320ae51f 381{
3d6efbf6 382 struct request *rq = data;
320ae51f 383
30a91cb4 384 rq->q->softirq_done_fn(rq);
320ae51f 385}
320ae51f 386
ed851860 387static void blk_mq_ipi_complete_request(struct request *rq)
320ae51f
JA
388{
389 struct blk_mq_ctx *ctx = rq->mq_ctx;
38535201 390 bool shared = false;
320ae51f
JA
391 int cpu;
392
38535201 393 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
30a91cb4
CH
394 rq->q->softirq_done_fn(rq);
395 return;
396 }
320ae51f
JA
397
398 cpu = get_cpu();
38535201
CH
399 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
400 shared = cpus_share_cache(cpu, ctx->cpu);
401
402 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
30a91cb4 403 rq->csd.func = __blk_mq_complete_request_remote;
3d6efbf6
CH
404 rq->csd.info = rq;
405 rq->csd.flags = 0;
c46fff2a 406 smp_call_function_single_async(ctx->cpu, &rq->csd);
3d6efbf6 407 } else {
30a91cb4 408 rq->q->softirq_done_fn(rq);
3d6efbf6 409 }
320ae51f
JA
410 put_cpu();
411}
30a91cb4 412
cf43e6be
JA
413static void blk_mq_stat_add(struct request *rq)
414{
415 if (rq->rq_flags & RQF_STATS) {
416 /*
417 * We could rq->mq_ctx here, but there's less of a risk
418 * of races if we have the completion event add the stats
419 * to the local software queue.
420 */
421 struct blk_mq_ctx *ctx;
422
423 ctx = __blk_mq_get_ctx(rq->q, raw_smp_processor_id());
424 blk_stat_add(&ctx->stat[rq_data_dir(rq)], rq);
425 }
426}
427
1fa8cc52 428static void __blk_mq_complete_request(struct request *rq)
ed851860
JA
429{
430 struct request_queue *q = rq->q;
431
cf43e6be
JA
432 blk_mq_stat_add(rq);
433
ed851860 434 if (!q->softirq_done_fn)
c8a446ad 435 blk_mq_end_request(rq, rq->errors);
ed851860
JA
436 else
437 blk_mq_ipi_complete_request(rq);
438}
439
30a91cb4
CH
440/**
441 * blk_mq_complete_request - end I/O on a request
442 * @rq: the request being processed
443 *
444 * Description:
445 * Ends all I/O on a request. It does not handle partial completions.
446 * The actual completion happens out-of-order, through a IPI handler.
447 **/
f4829a9b 448void blk_mq_complete_request(struct request *rq, int error)
30a91cb4 449{
95f09684
JA
450 struct request_queue *q = rq->q;
451
452 if (unlikely(blk_should_fake_timeout(q)))
30a91cb4 453 return;
f4829a9b
CH
454 if (!blk_mark_rq_complete(rq)) {
455 rq->errors = error;
ed851860 456 __blk_mq_complete_request(rq);
f4829a9b 457 }
30a91cb4
CH
458}
459EXPORT_SYMBOL(blk_mq_complete_request);
320ae51f 460
973c0191
KB
461int blk_mq_request_started(struct request *rq)
462{
463 return test_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
464}
465EXPORT_SYMBOL_GPL(blk_mq_request_started);
466
e2490073 467void blk_mq_start_request(struct request *rq)
320ae51f
JA
468{
469 struct request_queue *q = rq->q;
470
471 trace_block_rq_issue(q, rq);
472
742ee69b 473 rq->resid_len = blk_rq_bytes(rq);
91b63639
CH
474 if (unlikely(blk_bidi_rq(rq)))
475 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
742ee69b 476
cf43e6be
JA
477 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
478 blk_stat_set_issue_time(&rq->issue_stat);
479 rq->rq_flags |= RQF_STATS;
87760e5e 480 wbt_issue(q->rq_wb, &rq->issue_stat);
cf43e6be
JA
481 }
482
2b8393b4 483 blk_add_timer(rq);
87ee7b11 484
538b7534
JA
485 /*
486 * Ensure that ->deadline is visible before set the started
487 * flag and clear the completed flag.
488 */
489 smp_mb__before_atomic();
490
87ee7b11
JA
491 /*
492 * Mark us as started and clear complete. Complete might have been
493 * set if requeue raced with timeout, which then marked it as
494 * complete. So be sure to clear complete again when we start
495 * the request, otherwise we'll ignore the completion event.
496 */
4b570521
JA
497 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
498 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
499 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
500 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
49f5baa5
CH
501
502 if (q->dma_drain_size && blk_rq_bytes(rq)) {
503 /*
504 * Make sure space for the drain appears. We know we can do
505 * this because max_hw_segments has been adjusted to be one
506 * fewer than the device can handle.
507 */
508 rq->nr_phys_segments++;
509 }
320ae51f 510}
e2490073 511EXPORT_SYMBOL(blk_mq_start_request);
320ae51f 512
ed0791b2 513static void __blk_mq_requeue_request(struct request *rq)
320ae51f
JA
514{
515 struct request_queue *q = rq->q;
516
517 trace_block_rq_requeue(q, rq);
87760e5e 518 wbt_requeue(q->rq_wb, &rq->issue_stat);
49f5baa5 519
e2490073
CH
520 if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
521 if (q->dma_drain_size && blk_rq_bytes(rq))
522 rq->nr_phys_segments--;
523 }
320ae51f
JA
524}
525
2b053aca 526void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
ed0791b2 527{
ed0791b2 528 __blk_mq_requeue_request(rq);
ed0791b2 529
ed0791b2 530 BUG_ON(blk_queued_rq(rq));
2b053aca 531 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
ed0791b2
CH
532}
533EXPORT_SYMBOL(blk_mq_requeue_request);
534
6fca6a61
CH
535static void blk_mq_requeue_work(struct work_struct *work)
536{
537 struct request_queue *q =
2849450a 538 container_of(work, struct request_queue, requeue_work.work);
6fca6a61
CH
539 LIST_HEAD(rq_list);
540 struct request *rq, *next;
541 unsigned long flags;
542
543 spin_lock_irqsave(&q->requeue_lock, flags);
544 list_splice_init(&q->requeue_list, &rq_list);
545 spin_unlock_irqrestore(&q->requeue_lock, flags);
546
547 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
e8064021 548 if (!(rq->rq_flags & RQF_SOFTBARRIER))
6fca6a61
CH
549 continue;
550
e8064021 551 rq->rq_flags &= ~RQF_SOFTBARRIER;
6fca6a61
CH
552 list_del_init(&rq->queuelist);
553 blk_mq_insert_request(rq, true, false, false);
554 }
555
556 while (!list_empty(&rq_list)) {
557 rq = list_entry(rq_list.next, struct request, queuelist);
558 list_del_init(&rq->queuelist);
559 blk_mq_insert_request(rq, false, false, false);
560 }
561
52d7f1b5 562 blk_mq_run_hw_queues(q, false);
6fca6a61
CH
563}
564
2b053aca
BVA
565void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
566 bool kick_requeue_list)
6fca6a61
CH
567{
568 struct request_queue *q = rq->q;
569 unsigned long flags;
570
571 /*
572 * We abuse this flag that is otherwise used by the I/O scheduler to
573 * request head insertation from the workqueue.
574 */
e8064021 575 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
6fca6a61
CH
576
577 spin_lock_irqsave(&q->requeue_lock, flags);
578 if (at_head) {
e8064021 579 rq->rq_flags |= RQF_SOFTBARRIER;
6fca6a61
CH
580 list_add(&rq->queuelist, &q->requeue_list);
581 } else {
582 list_add_tail(&rq->queuelist, &q->requeue_list);
583 }
584 spin_unlock_irqrestore(&q->requeue_lock, flags);
2b053aca
BVA
585
586 if (kick_requeue_list)
587 blk_mq_kick_requeue_list(q);
6fca6a61
CH
588}
589EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
590
591void blk_mq_kick_requeue_list(struct request_queue *q)
592{
2849450a 593 kblockd_schedule_delayed_work(&q->requeue_work, 0);
6fca6a61
CH
594}
595EXPORT_SYMBOL(blk_mq_kick_requeue_list);
596
2849450a
MS
597void blk_mq_delay_kick_requeue_list(struct request_queue *q,
598 unsigned long msecs)
599{
600 kblockd_schedule_delayed_work(&q->requeue_work,
601 msecs_to_jiffies(msecs));
602}
603EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
604
1885b24d
JA
605void blk_mq_abort_requeue_list(struct request_queue *q)
606{
607 unsigned long flags;
608 LIST_HEAD(rq_list);
609
610 spin_lock_irqsave(&q->requeue_lock, flags);
611 list_splice_init(&q->requeue_list, &rq_list);
612 spin_unlock_irqrestore(&q->requeue_lock, flags);
613
614 while (!list_empty(&rq_list)) {
615 struct request *rq;
616
617 rq = list_first_entry(&rq_list, struct request, queuelist);
618 list_del_init(&rq->queuelist);
619 rq->errors = -EIO;
620 blk_mq_end_request(rq, rq->errors);
621 }
622}
623EXPORT_SYMBOL(blk_mq_abort_requeue_list);
624
0e62f51f
JA
625struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
626{
88c7b2b7
JA
627 if (tag < tags->nr_tags) {
628 prefetch(tags->rqs[tag]);
4ee86bab 629 return tags->rqs[tag];
88c7b2b7 630 }
4ee86bab
HR
631
632 return NULL;
24d2f903
CH
633}
634EXPORT_SYMBOL(blk_mq_tag_to_rq);
635
320ae51f 636struct blk_mq_timeout_data {
46f92d42
CH
637 unsigned long next;
638 unsigned int next_set;
320ae51f
JA
639};
640
90415837 641void blk_mq_rq_timed_out(struct request *req, bool reserved)
320ae51f 642{
f8a5b122 643 const struct blk_mq_ops *ops = req->q->mq_ops;
46f92d42 644 enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER;
87ee7b11
JA
645
646 /*
647 * We know that complete is set at this point. If STARTED isn't set
648 * anymore, then the request isn't active and the "timeout" should
649 * just be ignored. This can happen due to the bitflag ordering.
650 * Timeout first checks if STARTED is set, and if it is, assumes
651 * the request is active. But if we race with completion, then
652 * we both flags will get cleared. So check here again, and ignore
653 * a timeout event with a request that isn't active.
654 */
46f92d42
CH
655 if (!test_bit(REQ_ATOM_STARTED, &req->atomic_flags))
656 return;
87ee7b11 657
46f92d42 658 if (ops->timeout)
0152fb6b 659 ret = ops->timeout(req, reserved);
46f92d42
CH
660
661 switch (ret) {
662 case BLK_EH_HANDLED:
663 __blk_mq_complete_request(req);
664 break;
665 case BLK_EH_RESET_TIMER:
666 blk_add_timer(req);
667 blk_clear_rq_complete(req);
668 break;
669 case BLK_EH_NOT_HANDLED:
670 break;
671 default:
672 printk(KERN_ERR "block: bad eh return: %d\n", ret);
673 break;
674 }
87ee7b11 675}
5b3f25fc 676
81481eb4
CH
677static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
678 struct request *rq, void *priv, bool reserved)
679{
680 struct blk_mq_timeout_data *data = priv;
87ee7b11 681
eb130dbf
KB
682 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
683 /*
684 * If a request wasn't started before the queue was
685 * marked dying, kill it here or it'll go unnoticed.
686 */
a59e0f57
KB
687 if (unlikely(blk_queue_dying(rq->q))) {
688 rq->errors = -EIO;
689 blk_mq_end_request(rq, rq->errors);
690 }
46f92d42 691 return;
eb130dbf 692 }
87ee7b11 693
46f92d42
CH
694 if (time_after_eq(jiffies, rq->deadline)) {
695 if (!blk_mark_rq_complete(rq))
0152fb6b 696 blk_mq_rq_timed_out(rq, reserved);
46f92d42
CH
697 } else if (!data->next_set || time_after(data->next, rq->deadline)) {
698 data->next = rq->deadline;
699 data->next_set = 1;
700 }
87ee7b11
JA
701}
702
287922eb 703static void blk_mq_timeout_work(struct work_struct *work)
320ae51f 704{
287922eb
CH
705 struct request_queue *q =
706 container_of(work, struct request_queue, timeout_work);
81481eb4
CH
707 struct blk_mq_timeout_data data = {
708 .next = 0,
709 .next_set = 0,
710 };
81481eb4 711 int i;
320ae51f 712
71f79fb3
GKB
713 /* A deadlock might occur if a request is stuck requiring a
714 * timeout at the same time a queue freeze is waiting
715 * completion, since the timeout code would not be able to
716 * acquire the queue reference here.
717 *
718 * That's why we don't use blk_queue_enter here; instead, we use
719 * percpu_ref_tryget directly, because we need to be able to
720 * obtain a reference even in the short window between the queue
721 * starting to freeze, by dropping the first reference in
722 * blk_mq_freeze_queue_start, and the moment the last request is
723 * consumed, marked by the instant q_usage_counter reaches
724 * zero.
725 */
726 if (!percpu_ref_tryget(&q->q_usage_counter))
287922eb
CH
727 return;
728
0bf6cd5b 729 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &data);
320ae51f 730
81481eb4
CH
731 if (data.next_set) {
732 data.next = blk_rq_timeout(round_jiffies_up(data.next));
733 mod_timer(&q->timeout, data.next);
0d2602ca 734 } else {
0bf6cd5b
CH
735 struct blk_mq_hw_ctx *hctx;
736
f054b56c
ML
737 queue_for_each_hw_ctx(q, hctx, i) {
738 /* the hctx may be unmapped, so check it here */
739 if (blk_mq_hw_queue_mapped(hctx))
740 blk_mq_tag_idle(hctx);
741 }
0d2602ca 742 }
287922eb 743 blk_queue_exit(q);
320ae51f
JA
744}
745
746/*
747 * Reverse check our software queue for entries that we could potentially
748 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
749 * too much time checking for merges.
750 */
751static bool blk_mq_attempt_merge(struct request_queue *q,
752 struct blk_mq_ctx *ctx, struct bio *bio)
753{
754 struct request *rq;
755 int checked = 8;
756
757 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
758 int el_ret;
759
760 if (!checked--)
761 break;
762
763 if (!blk_rq_merge_ok(rq, bio))
764 continue;
765
766 el_ret = blk_try_merge(rq, bio);
767 if (el_ret == ELEVATOR_BACK_MERGE) {
768 if (bio_attempt_back_merge(q, rq, bio)) {
769 ctx->rq_merged++;
770 return true;
771 }
772 break;
773 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
774 if (bio_attempt_front_merge(q, rq, bio)) {
775 ctx->rq_merged++;
776 return true;
777 }
778 break;
779 }
780 }
781
782 return false;
783}
784
88459642
OS
785struct flush_busy_ctx_data {
786 struct blk_mq_hw_ctx *hctx;
787 struct list_head *list;
788};
789
790static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
791{
792 struct flush_busy_ctx_data *flush_data = data;
793 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
794 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
795
796 sbitmap_clear_bit(sb, bitnr);
797 spin_lock(&ctx->lock);
798 list_splice_tail_init(&ctx->rq_list, flush_data->list);
799 spin_unlock(&ctx->lock);
800 return true;
801}
802
1429d7c9
JA
803/*
804 * Process software queues that have been marked busy, splicing them
805 * to the for-dispatch
806 */
2c3ad667 807void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
1429d7c9 808{
88459642
OS
809 struct flush_busy_ctx_data data = {
810 .hctx = hctx,
811 .list = list,
812 };
1429d7c9 813
88459642 814 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
1429d7c9 815}
2c3ad667 816EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
1429d7c9 817
703fd1c0
JA
818static inline unsigned int queued_to_index(unsigned int queued)
819{
820 if (!queued)
821 return 0;
1429d7c9 822
703fd1c0 823 return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
1429d7c9
JA
824}
825
f04c3df3 826bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list)
320ae51f
JA
827{
828 struct request_queue *q = hctx->queue;
320ae51f 829 struct request *rq;
74c45052
JA
830 LIST_HEAD(driver_list);
831 struct list_head *dptr;
f04c3df3 832 int queued, ret = BLK_MQ_RQ_QUEUE_OK;
320ae51f 833
74c45052
JA
834 /*
835 * Start off with dptr being NULL, so we start the first request
836 * immediately, even if we have more pending.
837 */
838 dptr = NULL;
839
320ae51f
JA
840 /*
841 * Now process all the entries, sending them to the driver.
842 */
1429d7c9 843 queued = 0;
f04c3df3 844 while (!list_empty(list)) {
74c45052 845 struct blk_mq_queue_data bd;
320ae51f 846
f04c3df3 847 rq = list_first_entry(list, struct request, queuelist);
320ae51f 848 list_del_init(&rq->queuelist);
320ae51f 849
74c45052
JA
850 bd.rq = rq;
851 bd.list = dptr;
f04c3df3 852 bd.last = list_empty(list);
74c45052
JA
853
854 ret = q->mq_ops->queue_rq(hctx, &bd);
320ae51f
JA
855 switch (ret) {
856 case BLK_MQ_RQ_QUEUE_OK:
857 queued++;
52b9c330 858 break;
320ae51f 859 case BLK_MQ_RQ_QUEUE_BUSY:
f04c3df3 860 list_add(&rq->queuelist, list);
ed0791b2 861 __blk_mq_requeue_request(rq);
320ae51f
JA
862 break;
863 default:
864 pr_err("blk-mq: bad return on queue: %d\n", ret);
320ae51f 865 case BLK_MQ_RQ_QUEUE_ERROR:
1e93b8c2 866 rq->errors = -EIO;
c8a446ad 867 blk_mq_end_request(rq, rq->errors);
320ae51f
JA
868 break;
869 }
870
871 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
872 break;
74c45052
JA
873
874 /*
875 * We've done the first request. If we have more than 1
876 * left in the list, set dptr to defer issue.
877 */
f04c3df3 878 if (!dptr && list->next != list->prev)
74c45052 879 dptr = &driver_list;
320ae51f
JA
880 }
881
703fd1c0 882 hctx->dispatched[queued_to_index(queued)]++;
320ae51f
JA
883
884 /*
885 * Any items that need requeuing? Stuff them into hctx->dispatch,
886 * that is where we will continue on next queue run.
887 */
f04c3df3 888 if (!list_empty(list)) {
320ae51f 889 spin_lock(&hctx->lock);
f04c3df3 890 list_splice(list, &hctx->dispatch);
320ae51f 891 spin_unlock(&hctx->lock);
f04c3df3 892
9ba52e58
SL
893 /*
894 * the queue is expected stopped with BLK_MQ_RQ_QUEUE_BUSY, but
895 * it's possible the queue is stopped and restarted again
896 * before this. Queue restart will dispatch requests. And since
897 * requests in rq_list aren't added into hctx->dispatch yet,
898 * the requests in rq_list might get lost.
899 *
900 * blk_mq_run_hw_queue() already checks the STOPPED bit
901 **/
902 blk_mq_run_hw_queue(hctx, true);
320ae51f 903 }
f04c3df3
JA
904
905 return ret != BLK_MQ_RQ_QUEUE_BUSY;
906}
907
908/*
909 * Run this hardware queue, pulling any software queues mapped to it in.
910 * Note that this function currently has various problems around ordering
911 * of IO. In particular, we'd like FIFO behaviour on handling existing
912 * items on the hctx->dispatch list. Ignore that for now.
913 */
914static void blk_mq_process_rq_list(struct blk_mq_hw_ctx *hctx)
915{
916 LIST_HEAD(rq_list);
917 LIST_HEAD(driver_list);
918
919 if (unlikely(blk_mq_hctx_stopped(hctx)))
920 return;
921
922 hctx->run++;
923
924 /*
925 * Touch any software queue that has pending entries.
926 */
2c3ad667 927 blk_mq_flush_busy_ctxs(hctx, &rq_list);
f04c3df3
JA
928
929 /*
930 * If we have previous entries on our dispatch list, grab them
931 * and stuff them at the front for more fair dispatch.
932 */
933 if (!list_empty_careful(&hctx->dispatch)) {
934 spin_lock(&hctx->lock);
935 if (!list_empty(&hctx->dispatch))
936 list_splice_init(&hctx->dispatch, &rq_list);
937 spin_unlock(&hctx->lock);
938 }
939
940 blk_mq_dispatch_rq_list(hctx, &rq_list);
320ae51f
JA
941}
942
6a83e74d
BVA
943static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
944{
945 int srcu_idx;
946
947 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
948 cpu_online(hctx->next_cpu));
949
950 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
951 rcu_read_lock();
952 blk_mq_process_rq_list(hctx);
953 rcu_read_unlock();
954 } else {
955 srcu_idx = srcu_read_lock(&hctx->queue_rq_srcu);
956 blk_mq_process_rq_list(hctx);
957 srcu_read_unlock(&hctx->queue_rq_srcu, srcu_idx);
958 }
959}
960
506e931f
JA
961/*
962 * It'd be great if the workqueue API had a way to pass
963 * in a mask and had some smarts for more clever placement.
964 * For now we just round-robin here, switching for every
965 * BLK_MQ_CPU_WORK_BATCH queued items.
966 */
967static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
968{
b657d7e6
CH
969 if (hctx->queue->nr_hw_queues == 1)
970 return WORK_CPU_UNBOUND;
506e931f
JA
971
972 if (--hctx->next_cpu_batch <= 0) {
c02ebfdd 973 int next_cpu;
506e931f
JA
974
975 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
976 if (next_cpu >= nr_cpu_ids)
977 next_cpu = cpumask_first(hctx->cpumask);
978
979 hctx->next_cpu = next_cpu;
980 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
981 }
982
b657d7e6 983 return hctx->next_cpu;
506e931f
JA
984}
985
320ae51f
JA
986void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
987{
5d1b25c1
BVA
988 if (unlikely(blk_mq_hctx_stopped(hctx) ||
989 !blk_mq_hw_queue_mapped(hctx)))
320ae51f
JA
990 return;
991
1b792f2f 992 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
2a90d4aa
PB
993 int cpu = get_cpu();
994 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
398205b8 995 __blk_mq_run_hw_queue(hctx);
2a90d4aa 996 put_cpu();
398205b8
PB
997 return;
998 }
e4043dcf 999
2a90d4aa 1000 put_cpu();
e4043dcf 1001 }
398205b8 1002
27489a3c 1003 kblockd_schedule_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work);
320ae51f
JA
1004}
1005
b94ec296 1006void blk_mq_run_hw_queues(struct request_queue *q, bool async)
320ae51f
JA
1007{
1008 struct blk_mq_hw_ctx *hctx;
1009 int i;
1010
1011 queue_for_each_hw_ctx(q, hctx, i) {
1012 if ((!blk_mq_hctx_has_pending(hctx) &&
1013 list_empty_careful(&hctx->dispatch)) ||
5d1b25c1 1014 blk_mq_hctx_stopped(hctx))
320ae51f
JA
1015 continue;
1016
b94ec296 1017 blk_mq_run_hw_queue(hctx, async);
320ae51f
JA
1018 }
1019}
b94ec296 1020EXPORT_SYMBOL(blk_mq_run_hw_queues);
320ae51f 1021
fd001443
BVA
1022/**
1023 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
1024 * @q: request queue.
1025 *
1026 * The caller is responsible for serializing this function against
1027 * blk_mq_{start,stop}_hw_queue().
1028 */
1029bool blk_mq_queue_stopped(struct request_queue *q)
1030{
1031 struct blk_mq_hw_ctx *hctx;
1032 int i;
1033
1034 queue_for_each_hw_ctx(q, hctx, i)
1035 if (blk_mq_hctx_stopped(hctx))
1036 return true;
1037
1038 return false;
1039}
1040EXPORT_SYMBOL(blk_mq_queue_stopped);
1041
320ae51f
JA
1042void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
1043{
27489a3c 1044 cancel_work(&hctx->run_work);
70f4db63 1045 cancel_delayed_work(&hctx->delay_work);
320ae51f
JA
1046 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
1047}
1048EXPORT_SYMBOL(blk_mq_stop_hw_queue);
1049
280d45f6
CH
1050void blk_mq_stop_hw_queues(struct request_queue *q)
1051{
1052 struct blk_mq_hw_ctx *hctx;
1053 int i;
1054
1055 queue_for_each_hw_ctx(q, hctx, i)
1056 blk_mq_stop_hw_queue(hctx);
1057}
1058EXPORT_SYMBOL(blk_mq_stop_hw_queues);
1059
320ae51f
JA
1060void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
1061{
1062 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
e4043dcf 1063
0ffbce80 1064 blk_mq_run_hw_queue(hctx, false);
320ae51f
JA
1065}
1066EXPORT_SYMBOL(blk_mq_start_hw_queue);
1067
2f268556
CH
1068void blk_mq_start_hw_queues(struct request_queue *q)
1069{
1070 struct blk_mq_hw_ctx *hctx;
1071 int i;
1072
1073 queue_for_each_hw_ctx(q, hctx, i)
1074 blk_mq_start_hw_queue(hctx);
1075}
1076EXPORT_SYMBOL(blk_mq_start_hw_queues);
1077
ae911c5e
JA
1078void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1079{
1080 if (!blk_mq_hctx_stopped(hctx))
1081 return;
1082
1083 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1084 blk_mq_run_hw_queue(hctx, async);
1085}
1086EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
1087
1b4a3258 1088void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
320ae51f
JA
1089{
1090 struct blk_mq_hw_ctx *hctx;
1091 int i;
1092
ae911c5e
JA
1093 queue_for_each_hw_ctx(q, hctx, i)
1094 blk_mq_start_stopped_hw_queue(hctx, async);
320ae51f
JA
1095}
1096EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1097
70f4db63 1098static void blk_mq_run_work_fn(struct work_struct *work)
320ae51f
JA
1099{
1100 struct blk_mq_hw_ctx *hctx;
1101
27489a3c 1102 hctx = container_of(work, struct blk_mq_hw_ctx, run_work);
e4043dcf 1103
320ae51f
JA
1104 __blk_mq_run_hw_queue(hctx);
1105}
1106
70f4db63
CH
1107static void blk_mq_delay_work_fn(struct work_struct *work)
1108{
1109 struct blk_mq_hw_ctx *hctx;
1110
1111 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
1112
1113 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
1114 __blk_mq_run_hw_queue(hctx);
1115}
1116
1117void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1118{
19c66e59
ML
1119 if (unlikely(!blk_mq_hw_queue_mapped(hctx)))
1120 return;
70f4db63 1121
b657d7e6
CH
1122 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
1123 &hctx->delay_work, msecs_to_jiffies(msecs));
70f4db63
CH
1124}
1125EXPORT_SYMBOL(blk_mq_delay_queue);
1126
cfd0c552 1127static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
cfd0c552
ML
1128 struct request *rq,
1129 bool at_head)
320ae51f 1130{
e57690fe
JA
1131 struct blk_mq_ctx *ctx = rq->mq_ctx;
1132
01b983c9
JA
1133 trace_block_rq_insert(hctx->queue, rq);
1134
72a0a36e
CH
1135 if (at_head)
1136 list_add(&rq->queuelist, &ctx->rq_list);
1137 else
1138 list_add_tail(&rq->queuelist, &ctx->rq_list);
cfd0c552 1139}
4bb659b1 1140
2c3ad667
JA
1141void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
1142 bool at_head)
cfd0c552
ML
1143{
1144 struct blk_mq_ctx *ctx = rq->mq_ctx;
1145
e57690fe 1146 __blk_mq_insert_req_list(hctx, rq, at_head);
320ae51f 1147 blk_mq_hctx_mark_pending(hctx, ctx);
320ae51f
JA
1148}
1149
eeabc850 1150void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
e57690fe 1151 bool async)
320ae51f 1152{
e57690fe 1153 struct blk_mq_ctx *ctx = rq->mq_ctx;
eeabc850 1154 struct request_queue *q = rq->q;
7d7e0f90 1155 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
320ae51f 1156
a57a178a
CH
1157 spin_lock(&ctx->lock);
1158 __blk_mq_insert_request(hctx, rq, at_head);
1159 spin_unlock(&ctx->lock);
320ae51f 1160
320ae51f
JA
1161 if (run_queue)
1162 blk_mq_run_hw_queue(hctx, async);
1163}
1164
1165static void blk_mq_insert_requests(struct request_queue *q,
1166 struct blk_mq_ctx *ctx,
1167 struct list_head *list,
1168 int depth,
1169 bool from_schedule)
1170
1171{
7d7e0f90 1172 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
320ae51f
JA
1173
1174 trace_block_unplug(q, depth, !from_schedule);
1175
320ae51f
JA
1176 /*
1177 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1178 * offline now
1179 */
1180 spin_lock(&ctx->lock);
1181 while (!list_empty(list)) {
1182 struct request *rq;
1183
1184 rq = list_first_entry(list, struct request, queuelist);
e57690fe 1185 BUG_ON(rq->mq_ctx != ctx);
320ae51f 1186 list_del_init(&rq->queuelist);
e57690fe 1187 __blk_mq_insert_req_list(hctx, rq, false);
320ae51f 1188 }
cfd0c552 1189 blk_mq_hctx_mark_pending(hctx, ctx);
320ae51f
JA
1190 spin_unlock(&ctx->lock);
1191
320ae51f
JA
1192 blk_mq_run_hw_queue(hctx, from_schedule);
1193}
1194
1195static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1196{
1197 struct request *rqa = container_of(a, struct request, queuelist);
1198 struct request *rqb = container_of(b, struct request, queuelist);
1199
1200 return !(rqa->mq_ctx < rqb->mq_ctx ||
1201 (rqa->mq_ctx == rqb->mq_ctx &&
1202 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1203}
1204
1205void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1206{
1207 struct blk_mq_ctx *this_ctx;
1208 struct request_queue *this_q;
1209 struct request *rq;
1210 LIST_HEAD(list);
1211 LIST_HEAD(ctx_list);
1212 unsigned int depth;
1213
1214 list_splice_init(&plug->mq_list, &list);
1215
1216 list_sort(NULL, &list, plug_ctx_cmp);
1217
1218 this_q = NULL;
1219 this_ctx = NULL;
1220 depth = 0;
1221
1222 while (!list_empty(&list)) {
1223 rq = list_entry_rq(list.next);
1224 list_del_init(&rq->queuelist);
1225 BUG_ON(!rq->q);
1226 if (rq->mq_ctx != this_ctx) {
1227 if (this_ctx) {
1228 blk_mq_insert_requests(this_q, this_ctx,
1229 &ctx_list, depth,
1230 from_schedule);
1231 }
1232
1233 this_ctx = rq->mq_ctx;
1234 this_q = rq->q;
1235 depth = 0;
1236 }
1237
1238 depth++;
1239 list_add_tail(&rq->queuelist, &ctx_list);
1240 }
1241
1242 /*
1243 * If 'this_ctx' is set, we know we have entries to complete
1244 * on 'ctx_list'. Do those.
1245 */
1246 if (this_ctx) {
1247 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1248 from_schedule);
1249 }
1250}
1251
1252static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1253{
1254 init_request_from_bio(rq, bio);
4b570521 1255
6e85eaf3 1256 blk_account_io_start(rq, true);
320ae51f
JA
1257}
1258
274a5843
JA
1259static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx)
1260{
1261 return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
1262 !blk_queue_nomerges(hctx->queue);
1263}
1264
07068d5b
JA
1265static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1266 struct blk_mq_ctx *ctx,
1267 struct request *rq, struct bio *bio)
320ae51f 1268{
e18378a6 1269 if (!hctx_allow_merges(hctx) || !bio_mergeable(bio)) {
07068d5b
JA
1270 blk_mq_bio_to_request(rq, bio);
1271 spin_lock(&ctx->lock);
1272insert_rq:
1273 __blk_mq_insert_request(hctx, rq, false);
1274 spin_unlock(&ctx->lock);
1275 return false;
1276 } else {
274a5843
JA
1277 struct request_queue *q = hctx->queue;
1278
07068d5b
JA
1279 spin_lock(&ctx->lock);
1280 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1281 blk_mq_bio_to_request(rq, bio);
1282 goto insert_rq;
1283 }
320ae51f 1284
07068d5b
JA
1285 spin_unlock(&ctx->lock);
1286 __blk_mq_free_request(hctx, ctx, rq);
1287 return true;
14ec77f3 1288 }
07068d5b 1289}
14ec77f3 1290
07068d5b
JA
1291static struct request *blk_mq_map_request(struct request_queue *q,
1292 struct bio *bio,
2552e3f8 1293 struct blk_mq_alloc_data *data)
07068d5b
JA
1294{
1295 struct blk_mq_hw_ctx *hctx;
1296 struct blk_mq_ctx *ctx;
1297 struct request *rq;
320ae51f 1298
3ef28e83 1299 blk_queue_enter_live(q);
320ae51f 1300 ctx = blk_mq_get_ctx(q);
7d7e0f90 1301 hctx = blk_mq_map_queue(q, ctx->cpu);
320ae51f 1302
ef295ecf 1303 trace_block_getrq(q, bio, bio->bi_opf);
2552e3f8 1304 blk_mq_set_alloc_data(data, q, 0, ctx, hctx);
ef295ecf 1305 rq = __blk_mq_alloc_request(data, bio->bi_opf);
320ae51f 1306
7dd2fb68 1307 data->hctx->queued++;
07068d5b
JA
1308 return rq;
1309}
1310
fd2d3326
JA
1311static blk_qc_t request_to_qc_t(struct blk_mq_hw_ctx *hctx, struct request *rq)
1312{
1313 return blk_tag_to_qc_t(rq->tag, hctx->queue_num, false);
1314}
1315
066a4a73 1316static void blk_mq_try_issue_directly(struct request *rq, blk_qc_t *cookie)
f984df1f
SL
1317{
1318 int ret;
1319 struct request_queue *q = rq->q;
066a4a73 1320 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, rq->mq_ctx->cpu);
f984df1f
SL
1321 struct blk_mq_queue_data bd = {
1322 .rq = rq,
1323 .list = NULL,
1324 .last = 1
1325 };
fd2d3326 1326 blk_qc_t new_cookie = request_to_qc_t(hctx, rq);
f984df1f 1327
2253efc8
BVA
1328 if (blk_mq_hctx_stopped(hctx))
1329 goto insert;
1330
f984df1f
SL
1331 /*
1332 * For OK queue, we are done. For error, kill it. Any other
1333 * error (busy), just add it to our list as we previously
1334 * would have done
1335 */
1336 ret = q->mq_ops->queue_rq(hctx, &bd);
7b371636
JA
1337 if (ret == BLK_MQ_RQ_QUEUE_OK) {
1338 *cookie = new_cookie;
2253efc8 1339 return;
7b371636 1340 }
f984df1f 1341
7b371636
JA
1342 __blk_mq_requeue_request(rq);
1343
1344 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1345 *cookie = BLK_QC_T_NONE;
1346 rq->errors = -EIO;
1347 blk_mq_end_request(rq, rq->errors);
2253efc8 1348 return;
f984df1f 1349 }
7b371636 1350
2253efc8
BVA
1351insert:
1352 blk_mq_insert_request(rq, false, true, true);
f984df1f
SL
1353}
1354
07068d5b
JA
1355/*
1356 * Multiple hardware queue variant. This will not use per-process plugs,
1357 * but will attempt to bypass the hctx queueing if we can go straight to
1358 * hardware for SYNC IO.
1359 */
dece1635 1360static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
07068d5b 1361{
ef295ecf 1362 const int is_sync = op_is_sync(bio->bi_opf);
1eff9d32 1363 const int is_flush_fua = bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
2552e3f8 1364 struct blk_mq_alloc_data data;
07068d5b 1365 struct request *rq;
6a83e74d 1366 unsigned int request_count = 0, srcu_idx;
f984df1f 1367 struct blk_plug *plug;
5b3f341f 1368 struct request *same_queue_rq = NULL;
7b371636 1369 blk_qc_t cookie;
87760e5e 1370 unsigned int wb_acct;
07068d5b
JA
1371
1372 blk_queue_bounce(q, &bio);
1373
1374 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
4246a0b6 1375 bio_io_error(bio);
dece1635 1376 return BLK_QC_T_NONE;
07068d5b
JA
1377 }
1378
54efd50b
KO
1379 blk_queue_split(q, &bio, q->bio_split);
1380
87c279e6
OS
1381 if (!is_flush_fua && !blk_queue_nomerges(q) &&
1382 blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq))
1383 return BLK_QC_T_NONE;
f984df1f 1384
87760e5e
JA
1385 wb_acct = wbt_wait(q->rq_wb, bio, NULL);
1386
07068d5b 1387 rq = blk_mq_map_request(q, bio, &data);
87760e5e
JA
1388 if (unlikely(!rq)) {
1389 __wbt_done(q->rq_wb, wb_acct);
dece1635 1390 return BLK_QC_T_NONE;
87760e5e
JA
1391 }
1392
1393 wbt_track(&rq->issue_stat, wb_acct);
07068d5b 1394
fd2d3326 1395 cookie = request_to_qc_t(data.hctx, rq);
07068d5b
JA
1396
1397 if (unlikely(is_flush_fua)) {
1398 blk_mq_bio_to_request(rq, bio);
1399 blk_insert_flush(rq);
1400 goto run_queue;
1401 }
1402
f984df1f 1403 plug = current->plug;
e167dfb5
JA
1404 /*
1405 * If the driver supports defer issued based on 'last', then
1406 * queue it up like normal since we can potentially save some
1407 * CPU this way.
1408 */
f984df1f
SL
1409 if (((plug && !blk_queue_nomerges(q)) || is_sync) &&
1410 !(data.hctx->flags & BLK_MQ_F_DEFER_ISSUE)) {
1411 struct request *old_rq = NULL;
07068d5b
JA
1412
1413 blk_mq_bio_to_request(rq, bio);
07068d5b
JA
1414
1415 /*
6a83e74d 1416 * We do limited plugging. If the bio can be merged, do that.
f984df1f
SL
1417 * Otherwise the existing request in the plug list will be
1418 * issued. So the plug list will have one request at most
07068d5b 1419 */
f984df1f 1420 if (plug) {
5b3f341f
SL
1421 /*
1422 * The plug list might get flushed before this. If that
b094f89c
JA
1423 * happens, same_queue_rq is invalid and plug list is
1424 * empty
1425 */
5b3f341f
SL
1426 if (same_queue_rq && !list_empty(&plug->mq_list)) {
1427 old_rq = same_queue_rq;
f984df1f 1428 list_del_init(&old_rq->queuelist);
07068d5b 1429 }
f984df1f
SL
1430 list_add_tail(&rq->queuelist, &plug->mq_list);
1431 } else /* is_sync */
1432 old_rq = rq;
1433 blk_mq_put_ctx(data.ctx);
1434 if (!old_rq)
7b371636 1435 goto done;
6a83e74d
BVA
1436
1437 if (!(data.hctx->flags & BLK_MQ_F_BLOCKING)) {
1438 rcu_read_lock();
066a4a73 1439 blk_mq_try_issue_directly(old_rq, &cookie);
6a83e74d
BVA
1440 rcu_read_unlock();
1441 } else {
1442 srcu_idx = srcu_read_lock(&data.hctx->queue_rq_srcu);
066a4a73 1443 blk_mq_try_issue_directly(old_rq, &cookie);
6a83e74d
BVA
1444 srcu_read_unlock(&data.hctx->queue_rq_srcu, srcu_idx);
1445 }
7b371636 1446 goto done;
07068d5b
JA
1447 }
1448
1449 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1450 /*
1451 * For a SYNC request, send it to the hardware immediately. For
1452 * an ASYNC request, just ensure that we run it later on. The
1453 * latter allows for merging opportunities and more efficient
1454 * dispatching.
1455 */
1456run_queue:
1457 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1458 }
07068d5b 1459 blk_mq_put_ctx(data.ctx);
7b371636
JA
1460done:
1461 return cookie;
07068d5b
JA
1462}
1463
1464/*
1465 * Single hardware queue variant. This will attempt to use any per-process
1466 * plug for merging and IO deferral.
1467 */
dece1635 1468static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio)
07068d5b 1469{
ef295ecf 1470 const int is_sync = op_is_sync(bio->bi_opf);
1eff9d32 1471 const int is_flush_fua = bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
e6c4438b
JM
1472 struct blk_plug *plug;
1473 unsigned int request_count = 0;
2552e3f8 1474 struct blk_mq_alloc_data data;
07068d5b 1475 struct request *rq;
7b371636 1476 blk_qc_t cookie;
87760e5e 1477 unsigned int wb_acct;
07068d5b 1478
07068d5b
JA
1479 blk_queue_bounce(q, &bio);
1480
1481 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
4246a0b6 1482 bio_io_error(bio);
dece1635 1483 return BLK_QC_T_NONE;
07068d5b
JA
1484 }
1485
54efd50b
KO
1486 blk_queue_split(q, &bio, q->bio_split);
1487
87c279e6
OS
1488 if (!is_flush_fua && !blk_queue_nomerges(q)) {
1489 if (blk_attempt_plug_merge(q, bio, &request_count, NULL))
1490 return BLK_QC_T_NONE;
1491 } else
1492 request_count = blk_plug_queued_count(q);
07068d5b 1493
87760e5e
JA
1494 wb_acct = wbt_wait(q->rq_wb, bio, NULL);
1495
07068d5b 1496 rq = blk_mq_map_request(q, bio, &data);
87760e5e
JA
1497 if (unlikely(!rq)) {
1498 __wbt_done(q->rq_wb, wb_acct);
dece1635 1499 return BLK_QC_T_NONE;
87760e5e
JA
1500 }
1501
1502 wbt_track(&rq->issue_stat, wb_acct);
320ae51f 1503
fd2d3326 1504 cookie = request_to_qc_t(data.hctx, rq);
320ae51f
JA
1505
1506 if (unlikely(is_flush_fua)) {
1507 blk_mq_bio_to_request(rq, bio);
320ae51f
JA
1508 blk_insert_flush(rq);
1509 goto run_queue;
1510 }
1511
1512 /*
1513 * A task plug currently exists. Since this is completely lockless,
1514 * utilize that to temporarily store requests until the task is
1515 * either done or scheduled away.
1516 */
e6c4438b
JM
1517 plug = current->plug;
1518 if (plug) {
600271d9
SL
1519 struct request *last = NULL;
1520
e6c4438b 1521 blk_mq_bio_to_request(rq, bio);
0a6219a9
ML
1522
1523 /*
1524 * @request_count may become stale because of schedule
1525 * out, so check the list again.
1526 */
1527 if (list_empty(&plug->mq_list))
1528 request_count = 0;
676d0607 1529 if (!request_count)
e6c4438b 1530 trace_block_plug(q);
600271d9
SL
1531 else
1532 last = list_entry_rq(plug->mq_list.prev);
b094f89c
JA
1533
1534 blk_mq_put_ctx(data.ctx);
1535
600271d9
SL
1536 if (request_count >= BLK_MAX_REQUEST_COUNT || (last &&
1537 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
e6c4438b
JM
1538 blk_flush_plug_list(plug, false);
1539 trace_block_plug(q);
320ae51f 1540 }
b094f89c 1541
e6c4438b 1542 list_add_tail(&rq->queuelist, &plug->mq_list);
7b371636 1543 return cookie;
320ae51f
JA
1544 }
1545
07068d5b
JA
1546 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1547 /*
1548 * For a SYNC request, send it to the hardware immediately. For
1549 * an ASYNC request, just ensure that we run it later on. The
1550 * latter allows for merging opportunities and more efficient
1551 * dispatching.
1552 */
1553run_queue:
1554 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
320ae51f
JA
1555 }
1556
07068d5b 1557 blk_mq_put_ctx(data.ctx);
7b371636 1558 return cookie;
320ae51f
JA
1559}
1560
cc71a6f4
JA
1561void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
1562 unsigned int hctx_idx)
95363efd 1563{
e9b267d9 1564 struct page *page;
320ae51f 1565
24d2f903 1566 if (tags->rqs && set->ops->exit_request) {
e9b267d9 1567 int i;
320ae51f 1568
24d2f903
CH
1569 for (i = 0; i < tags->nr_tags; i++) {
1570 if (!tags->rqs[i])
e9b267d9 1571 continue;
24d2f903
CH
1572 set->ops->exit_request(set->driver_data, tags->rqs[i],
1573 hctx_idx, i);
a5164405 1574 tags->rqs[i] = NULL;
e9b267d9 1575 }
320ae51f 1576 }
320ae51f 1577
24d2f903
CH
1578 while (!list_empty(&tags->page_list)) {
1579 page = list_first_entry(&tags->page_list, struct page, lru);
6753471c 1580 list_del_init(&page->lru);
f75782e4
CM
1581 /*
1582 * Remove kmemleak object previously allocated in
1583 * blk_mq_init_rq_map().
1584 */
1585 kmemleak_free(page_address(page));
320ae51f
JA
1586 __free_pages(page, page->private);
1587 }
cc71a6f4 1588}
320ae51f 1589
cc71a6f4
JA
1590void blk_mq_free_rq_map(struct blk_mq_tags *tags)
1591{
24d2f903 1592 kfree(tags->rqs);
cc71a6f4 1593 tags->rqs = NULL;
320ae51f 1594
24d2f903 1595 blk_mq_free_tags(tags);
320ae51f
JA
1596}
1597
cc71a6f4
JA
1598struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
1599 unsigned int hctx_idx,
1600 unsigned int nr_tags,
1601 unsigned int reserved_tags)
320ae51f 1602{
24d2f903 1603 struct blk_mq_tags *tags;
320ae51f 1604
cc71a6f4 1605 tags = blk_mq_init_tags(nr_tags, reserved_tags,
24391c0d
SL
1606 set->numa_node,
1607 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
24d2f903
CH
1608 if (!tags)
1609 return NULL;
320ae51f 1610
cc71a6f4 1611 tags->rqs = kzalloc_node(nr_tags * sizeof(struct request *),
36e1f3d1 1612 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
a5164405 1613 set->numa_node);
24d2f903
CH
1614 if (!tags->rqs) {
1615 blk_mq_free_tags(tags);
1616 return NULL;
1617 }
320ae51f 1618
cc71a6f4
JA
1619 return tags;
1620}
1621
1622static size_t order_to_size(unsigned int order)
1623{
1624 return (size_t)PAGE_SIZE << order;
1625}
1626
1627int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
1628 unsigned int hctx_idx, unsigned int depth)
1629{
1630 unsigned int i, j, entries_per_page, max_order = 4;
1631 size_t rq_size, left;
1632
1633 INIT_LIST_HEAD(&tags->page_list);
1634
320ae51f
JA
1635 /*
1636 * rq_size is the size of the request plus driver payload, rounded
1637 * to the cacheline size
1638 */
24d2f903 1639 rq_size = round_up(sizeof(struct request) + set->cmd_size,
320ae51f 1640 cache_line_size());
cc71a6f4 1641 left = rq_size * depth;
320ae51f 1642
cc71a6f4 1643 for (i = 0; i < depth; ) {
320ae51f
JA
1644 int this_order = max_order;
1645 struct page *page;
1646 int to_do;
1647 void *p;
1648
b3a834b1 1649 while (this_order && left < order_to_size(this_order - 1))
320ae51f
JA
1650 this_order--;
1651
1652 do {
a5164405 1653 page = alloc_pages_node(set->numa_node,
36e1f3d1 1654 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
a5164405 1655 this_order);
320ae51f
JA
1656 if (page)
1657 break;
1658 if (!this_order--)
1659 break;
1660 if (order_to_size(this_order) < rq_size)
1661 break;
1662 } while (1);
1663
1664 if (!page)
24d2f903 1665 goto fail;
320ae51f
JA
1666
1667 page->private = this_order;
24d2f903 1668 list_add_tail(&page->lru, &tags->page_list);
320ae51f
JA
1669
1670 p = page_address(page);
f75782e4
CM
1671 /*
1672 * Allow kmemleak to scan these pages as they contain pointers
1673 * to additional allocations like via ops->init_request().
1674 */
36e1f3d1 1675 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
320ae51f 1676 entries_per_page = order_to_size(this_order) / rq_size;
cc71a6f4 1677 to_do = min(entries_per_page, depth - i);
320ae51f
JA
1678 left -= to_do * rq_size;
1679 for (j = 0; j < to_do; j++) {
24d2f903
CH
1680 tags->rqs[i] = p;
1681 if (set->ops->init_request) {
1682 if (set->ops->init_request(set->driver_data,
1683 tags->rqs[i], hctx_idx, i,
a5164405
JA
1684 set->numa_node)) {
1685 tags->rqs[i] = NULL;
24d2f903 1686 goto fail;
a5164405 1687 }
e9b267d9
CH
1688 }
1689
320ae51f
JA
1690 p += rq_size;
1691 i++;
1692 }
1693 }
cc71a6f4 1694 return 0;
320ae51f 1695
24d2f903 1696fail:
cc71a6f4
JA
1697 blk_mq_free_rqs(set, tags, hctx_idx);
1698 return -ENOMEM;
320ae51f
JA
1699}
1700
e57690fe
JA
1701/*
1702 * 'cpu' is going away. splice any existing rq_list entries from this
1703 * software queue to the hw queue dispatch list, and ensure that it
1704 * gets run.
1705 */
9467f859 1706static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
484b4061 1707{
9467f859 1708 struct blk_mq_hw_ctx *hctx;
484b4061
JA
1709 struct blk_mq_ctx *ctx;
1710 LIST_HEAD(tmp);
1711
9467f859 1712 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
e57690fe 1713 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
484b4061
JA
1714
1715 spin_lock(&ctx->lock);
1716 if (!list_empty(&ctx->rq_list)) {
1717 list_splice_init(&ctx->rq_list, &tmp);
1718 blk_mq_hctx_clear_pending(hctx, ctx);
1719 }
1720 spin_unlock(&ctx->lock);
1721
1722 if (list_empty(&tmp))
9467f859 1723 return 0;
484b4061 1724
e57690fe
JA
1725 spin_lock(&hctx->lock);
1726 list_splice_tail_init(&tmp, &hctx->dispatch);
1727 spin_unlock(&hctx->lock);
484b4061
JA
1728
1729 blk_mq_run_hw_queue(hctx, true);
9467f859 1730 return 0;
484b4061
JA
1731}
1732
9467f859 1733static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
484b4061 1734{
9467f859
TG
1735 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
1736 &hctx->cpuhp_dead);
484b4061
JA
1737}
1738
c3b4afca 1739/* hctx->ctxs will be freed in queue's release handler */
08e98fc6
ML
1740static void blk_mq_exit_hctx(struct request_queue *q,
1741 struct blk_mq_tag_set *set,
1742 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
1743{
f70ced09
ML
1744 unsigned flush_start_tag = set->queue_depth;
1745
08e98fc6
ML
1746 blk_mq_tag_idle(hctx);
1747
f70ced09
ML
1748 if (set->ops->exit_request)
1749 set->ops->exit_request(set->driver_data,
1750 hctx->fq->flush_rq, hctx_idx,
1751 flush_start_tag + hctx_idx);
1752
08e98fc6
ML
1753 if (set->ops->exit_hctx)
1754 set->ops->exit_hctx(hctx, hctx_idx);
1755
6a83e74d
BVA
1756 if (hctx->flags & BLK_MQ_F_BLOCKING)
1757 cleanup_srcu_struct(&hctx->queue_rq_srcu);
1758
9467f859 1759 blk_mq_remove_cpuhp(hctx);
f70ced09 1760 blk_free_flush_queue(hctx->fq);
88459642 1761 sbitmap_free(&hctx->ctx_map);
08e98fc6
ML
1762}
1763
624dbe47
ML
1764static void blk_mq_exit_hw_queues(struct request_queue *q,
1765 struct blk_mq_tag_set *set, int nr_queue)
1766{
1767 struct blk_mq_hw_ctx *hctx;
1768 unsigned int i;
1769
1770 queue_for_each_hw_ctx(q, hctx, i) {
1771 if (i == nr_queue)
1772 break;
08e98fc6 1773 blk_mq_exit_hctx(q, set, hctx, i);
624dbe47 1774 }
624dbe47
ML
1775}
1776
1777static void blk_mq_free_hw_queues(struct request_queue *q,
1778 struct blk_mq_tag_set *set)
1779{
1780 struct blk_mq_hw_ctx *hctx;
1781 unsigned int i;
1782
e09aae7e 1783 queue_for_each_hw_ctx(q, hctx, i)
624dbe47 1784 free_cpumask_var(hctx->cpumask);
624dbe47
ML
1785}
1786
08e98fc6
ML
1787static int blk_mq_init_hctx(struct request_queue *q,
1788 struct blk_mq_tag_set *set,
1789 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
320ae51f 1790{
08e98fc6 1791 int node;
f70ced09 1792 unsigned flush_start_tag = set->queue_depth;
08e98fc6
ML
1793
1794 node = hctx->numa_node;
1795 if (node == NUMA_NO_NODE)
1796 node = hctx->numa_node = set->numa_node;
1797
27489a3c 1798 INIT_WORK(&hctx->run_work, blk_mq_run_work_fn);
08e98fc6
ML
1799 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
1800 spin_lock_init(&hctx->lock);
1801 INIT_LIST_HEAD(&hctx->dispatch);
1802 hctx->queue = q;
1803 hctx->queue_num = hctx_idx;
2404e607 1804 hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED;
08e98fc6 1805
9467f859 1806 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
08e98fc6
ML
1807
1808 hctx->tags = set->tags[hctx_idx];
320ae51f
JA
1809
1810 /*
08e98fc6
ML
1811 * Allocate space for all possible cpus to avoid allocation at
1812 * runtime
320ae51f 1813 */
08e98fc6
ML
1814 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1815 GFP_KERNEL, node);
1816 if (!hctx->ctxs)
1817 goto unregister_cpu_notifier;
320ae51f 1818
88459642
OS
1819 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8), GFP_KERNEL,
1820 node))
08e98fc6 1821 goto free_ctxs;
320ae51f 1822
08e98fc6 1823 hctx->nr_ctx = 0;
320ae51f 1824
08e98fc6
ML
1825 if (set->ops->init_hctx &&
1826 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
1827 goto free_bitmap;
320ae51f 1828
f70ced09
ML
1829 hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size);
1830 if (!hctx->fq)
1831 goto exit_hctx;
320ae51f 1832
f70ced09
ML
1833 if (set->ops->init_request &&
1834 set->ops->init_request(set->driver_data,
1835 hctx->fq->flush_rq, hctx_idx,
1836 flush_start_tag + hctx_idx, node))
1837 goto free_fq;
320ae51f 1838
6a83e74d
BVA
1839 if (hctx->flags & BLK_MQ_F_BLOCKING)
1840 init_srcu_struct(&hctx->queue_rq_srcu);
1841
08e98fc6 1842 return 0;
320ae51f 1843
f70ced09
ML
1844 free_fq:
1845 kfree(hctx->fq);
1846 exit_hctx:
1847 if (set->ops->exit_hctx)
1848 set->ops->exit_hctx(hctx, hctx_idx);
08e98fc6 1849 free_bitmap:
88459642 1850 sbitmap_free(&hctx->ctx_map);
08e98fc6
ML
1851 free_ctxs:
1852 kfree(hctx->ctxs);
1853 unregister_cpu_notifier:
9467f859 1854 blk_mq_remove_cpuhp(hctx);
08e98fc6
ML
1855 return -1;
1856}
320ae51f 1857
320ae51f
JA
1858static void blk_mq_init_cpu_queues(struct request_queue *q,
1859 unsigned int nr_hw_queues)
1860{
1861 unsigned int i;
1862
1863 for_each_possible_cpu(i) {
1864 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1865 struct blk_mq_hw_ctx *hctx;
1866
1867 memset(__ctx, 0, sizeof(*__ctx));
1868 __ctx->cpu = i;
1869 spin_lock_init(&__ctx->lock);
1870 INIT_LIST_HEAD(&__ctx->rq_list);
1871 __ctx->queue = q;
cf43e6be
JA
1872 blk_stat_init(&__ctx->stat[BLK_STAT_READ]);
1873 blk_stat_init(&__ctx->stat[BLK_STAT_WRITE]);
320ae51f
JA
1874
1875 /* If the cpu isn't online, the cpu is mapped to first hctx */
320ae51f
JA
1876 if (!cpu_online(i))
1877 continue;
1878
7d7e0f90 1879 hctx = blk_mq_map_queue(q, i);
e4043dcf 1880
320ae51f
JA
1881 /*
1882 * Set local node, IFF we have more than one hw queue. If
1883 * not, we remain on the home node of the device
1884 */
1885 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
bffed457 1886 hctx->numa_node = local_memory_node(cpu_to_node(i));
320ae51f
JA
1887 }
1888}
1889
cc71a6f4
JA
1890static bool __blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, int hctx_idx)
1891{
1892 int ret = 0;
1893
1894 set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx,
1895 set->queue_depth, set->reserved_tags);
1896 if (!set->tags[hctx_idx])
1897 return false;
1898
1899 ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx,
1900 set->queue_depth);
1901 if (!ret)
1902 return true;
1903
1904 blk_mq_free_rq_map(set->tags[hctx_idx]);
1905 set->tags[hctx_idx] = NULL;
1906 return false;
1907}
1908
1909static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set,
1910 unsigned int hctx_idx)
1911{
1912 blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx);
1913 blk_mq_free_rq_map(set->tags[hctx_idx]);
1914 set->tags[hctx_idx] = NULL;
1915}
1916
5778322e
AM
1917static void blk_mq_map_swqueue(struct request_queue *q,
1918 const struct cpumask *online_mask)
320ae51f 1919{
d1b1cea1 1920 unsigned int i, hctx_idx;
320ae51f
JA
1921 struct blk_mq_hw_ctx *hctx;
1922 struct blk_mq_ctx *ctx;
2a34c087 1923 struct blk_mq_tag_set *set = q->tag_set;
320ae51f 1924
60de074b
AM
1925 /*
1926 * Avoid others reading imcomplete hctx->cpumask through sysfs
1927 */
1928 mutex_lock(&q->sysfs_lock);
1929
320ae51f 1930 queue_for_each_hw_ctx(q, hctx, i) {
e4043dcf 1931 cpumask_clear(hctx->cpumask);
320ae51f
JA
1932 hctx->nr_ctx = 0;
1933 }
1934
1935 /*
1936 * Map software to hardware queues
1937 */
897bb0c7 1938 for_each_possible_cpu(i) {
320ae51f 1939 /* If the cpu isn't online, the cpu is mapped to first hctx */
5778322e 1940 if (!cpumask_test_cpu(i, online_mask))
e4043dcf
JA
1941 continue;
1942
d1b1cea1
GKB
1943 hctx_idx = q->mq_map[i];
1944 /* unmapped hw queue can be remapped after CPU topo changed */
cc71a6f4
JA
1945 if (!set->tags[hctx_idx] &&
1946 !__blk_mq_alloc_rq_map(set, hctx_idx)) {
d1b1cea1
GKB
1947 /*
1948 * If tags initialization fail for some hctx,
1949 * that hctx won't be brought online. In this
1950 * case, remap the current ctx to hctx[0] which
1951 * is guaranteed to always have tags allocated
1952 */
cc71a6f4 1953 q->mq_map[i] = 0;
d1b1cea1
GKB
1954 }
1955
897bb0c7 1956 ctx = per_cpu_ptr(q->queue_ctx, i);
7d7e0f90 1957 hctx = blk_mq_map_queue(q, i);
868f2f0b 1958
e4043dcf 1959 cpumask_set_cpu(i, hctx->cpumask);
320ae51f
JA
1960 ctx->index_hw = hctx->nr_ctx;
1961 hctx->ctxs[hctx->nr_ctx++] = ctx;
1962 }
506e931f 1963
60de074b
AM
1964 mutex_unlock(&q->sysfs_lock);
1965
506e931f 1966 queue_for_each_hw_ctx(q, hctx, i) {
484b4061 1967 /*
a68aafa5
JA
1968 * If no software queues are mapped to this hardware queue,
1969 * disable it and free the request entries.
484b4061
JA
1970 */
1971 if (!hctx->nr_ctx) {
d1b1cea1
GKB
1972 /* Never unmap queue 0. We need it as a
1973 * fallback in case of a new remap fails
1974 * allocation
1975 */
cc71a6f4
JA
1976 if (i && set->tags[i])
1977 blk_mq_free_map_and_requests(set, i);
1978
2a34c087 1979 hctx->tags = NULL;
484b4061
JA
1980 continue;
1981 }
1982
2a34c087
ML
1983 hctx->tags = set->tags[i];
1984 WARN_ON(!hctx->tags);
1985
889fa31f
CY
1986 /*
1987 * Set the map size to the number of mapped software queues.
1988 * This is more accurate and more efficient than looping
1989 * over all possibly mapped software queues.
1990 */
88459642 1991 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
889fa31f 1992
484b4061
JA
1993 /*
1994 * Initialize batch roundrobin counts
1995 */
506e931f
JA
1996 hctx->next_cpu = cpumask_first(hctx->cpumask);
1997 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1998 }
320ae51f
JA
1999}
2000
2404e607 2001static void queue_set_hctx_shared(struct request_queue *q, bool shared)
0d2602ca
JA
2002{
2003 struct blk_mq_hw_ctx *hctx;
0d2602ca
JA
2004 int i;
2005
2404e607
JM
2006 queue_for_each_hw_ctx(q, hctx, i) {
2007 if (shared)
2008 hctx->flags |= BLK_MQ_F_TAG_SHARED;
2009 else
2010 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
2011 }
2012}
2013
2014static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set, bool shared)
2015{
2016 struct request_queue *q;
0d2602ca
JA
2017
2018 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2019 blk_mq_freeze_queue(q);
2404e607 2020 queue_set_hctx_shared(q, shared);
0d2602ca
JA
2021 blk_mq_unfreeze_queue(q);
2022 }
2023}
2024
2025static void blk_mq_del_queue_tag_set(struct request_queue *q)
2026{
2027 struct blk_mq_tag_set *set = q->tag_set;
2028
0d2602ca
JA
2029 mutex_lock(&set->tag_list_lock);
2030 list_del_init(&q->tag_set_list);
2404e607
JM
2031 if (list_is_singular(&set->tag_list)) {
2032 /* just transitioned to unshared */
2033 set->flags &= ~BLK_MQ_F_TAG_SHARED;
2034 /* update existing queue */
2035 blk_mq_update_tag_set_depth(set, false);
2036 }
0d2602ca 2037 mutex_unlock(&set->tag_list_lock);
0d2602ca
JA
2038}
2039
2040static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
2041 struct request_queue *q)
2042{
2043 q->tag_set = set;
2044
2045 mutex_lock(&set->tag_list_lock);
2404e607
JM
2046
2047 /* Check to see if we're transitioning to shared (from 1 to 2 queues). */
2048 if (!list_empty(&set->tag_list) && !(set->flags & BLK_MQ_F_TAG_SHARED)) {
2049 set->flags |= BLK_MQ_F_TAG_SHARED;
2050 /* update existing queue */
2051 blk_mq_update_tag_set_depth(set, true);
2052 }
2053 if (set->flags & BLK_MQ_F_TAG_SHARED)
2054 queue_set_hctx_shared(q, true);
0d2602ca 2055 list_add_tail(&q->tag_set_list, &set->tag_list);
2404e607 2056
0d2602ca
JA
2057 mutex_unlock(&set->tag_list_lock);
2058}
2059
e09aae7e
ML
2060/*
2061 * It is the actual release handler for mq, but we do it from
2062 * request queue's release handler for avoiding use-after-free
2063 * and headache because q->mq_kobj shouldn't have been introduced,
2064 * but we can't group ctx/kctx kobj without it.
2065 */
2066void blk_mq_release(struct request_queue *q)
2067{
2068 struct blk_mq_hw_ctx *hctx;
2069 unsigned int i;
2070
2071 /* hctx kobj stays in hctx */
c3b4afca
ML
2072 queue_for_each_hw_ctx(q, hctx, i) {
2073 if (!hctx)
2074 continue;
2075 kfree(hctx->ctxs);
e09aae7e 2076 kfree(hctx);
c3b4afca 2077 }
e09aae7e 2078
a723bab3
AM
2079 q->mq_map = NULL;
2080
e09aae7e
ML
2081 kfree(q->queue_hw_ctx);
2082
2083 /* ctx kobj stays in queue_ctx */
2084 free_percpu(q->queue_ctx);
2085}
2086
24d2f903 2087struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
b62c21b7
MS
2088{
2089 struct request_queue *uninit_q, *q;
2090
2091 uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
2092 if (!uninit_q)
2093 return ERR_PTR(-ENOMEM);
2094
2095 q = blk_mq_init_allocated_queue(set, uninit_q);
2096 if (IS_ERR(q))
2097 blk_cleanup_queue(uninit_q);
2098
2099 return q;
2100}
2101EXPORT_SYMBOL(blk_mq_init_queue);
2102
868f2f0b
KB
2103static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
2104 struct request_queue *q)
320ae51f 2105{
868f2f0b
KB
2106 int i, j;
2107 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
f14bbe77 2108
868f2f0b 2109 blk_mq_sysfs_unregister(q);
24d2f903 2110 for (i = 0; i < set->nr_hw_queues; i++) {
868f2f0b 2111 int node;
f14bbe77 2112
868f2f0b
KB
2113 if (hctxs[i])
2114 continue;
2115
2116 node = blk_mq_hw_queue_to_node(q->mq_map, i);
cdef54dd
CH
2117 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
2118 GFP_KERNEL, node);
320ae51f 2119 if (!hctxs[i])
868f2f0b 2120 break;
320ae51f 2121
a86073e4 2122 if (!zalloc_cpumask_var_node(&hctxs[i]->cpumask, GFP_KERNEL,
868f2f0b
KB
2123 node)) {
2124 kfree(hctxs[i]);
2125 hctxs[i] = NULL;
2126 break;
2127 }
e4043dcf 2128
0d2602ca 2129 atomic_set(&hctxs[i]->nr_active, 0);
f14bbe77 2130 hctxs[i]->numa_node = node;
320ae51f 2131 hctxs[i]->queue_num = i;
868f2f0b
KB
2132
2133 if (blk_mq_init_hctx(q, set, hctxs[i], i)) {
2134 free_cpumask_var(hctxs[i]->cpumask);
2135 kfree(hctxs[i]);
2136 hctxs[i] = NULL;
2137 break;
2138 }
2139 blk_mq_hctx_kobj_init(hctxs[i]);
320ae51f 2140 }
868f2f0b
KB
2141 for (j = i; j < q->nr_hw_queues; j++) {
2142 struct blk_mq_hw_ctx *hctx = hctxs[j];
2143
2144 if (hctx) {
cc71a6f4
JA
2145 if (hctx->tags)
2146 blk_mq_free_map_and_requests(set, j);
868f2f0b
KB
2147 blk_mq_exit_hctx(q, set, hctx, j);
2148 free_cpumask_var(hctx->cpumask);
2149 kobject_put(&hctx->kobj);
2150 kfree(hctx->ctxs);
2151 kfree(hctx);
2152 hctxs[j] = NULL;
2153
2154 }
2155 }
2156 q->nr_hw_queues = i;
2157 blk_mq_sysfs_register(q);
2158}
2159
2160struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
2161 struct request_queue *q)
2162{
66841672
ML
2163 /* mark the queue as mq asap */
2164 q->mq_ops = set->ops;
2165
868f2f0b
KB
2166 q->queue_ctx = alloc_percpu(struct blk_mq_ctx);
2167 if (!q->queue_ctx)
c7de5726 2168 goto err_exit;
868f2f0b
KB
2169
2170 q->queue_hw_ctx = kzalloc_node(nr_cpu_ids * sizeof(*(q->queue_hw_ctx)),
2171 GFP_KERNEL, set->numa_node);
2172 if (!q->queue_hw_ctx)
2173 goto err_percpu;
2174
bdd17e75 2175 q->mq_map = set->mq_map;
868f2f0b
KB
2176
2177 blk_mq_realloc_hw_ctxs(set, q);
2178 if (!q->nr_hw_queues)
2179 goto err_hctxs;
320ae51f 2180
287922eb 2181 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
e56f698b 2182 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
320ae51f
JA
2183
2184 q->nr_queues = nr_cpu_ids;
320ae51f 2185
94eddfbe 2186 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
320ae51f 2187
05f1dd53
JA
2188 if (!(set->flags & BLK_MQ_F_SG_MERGE))
2189 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
2190
1be036e9
CH
2191 q->sg_reserved_size = INT_MAX;
2192
2849450a 2193 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
6fca6a61
CH
2194 INIT_LIST_HEAD(&q->requeue_list);
2195 spin_lock_init(&q->requeue_lock);
2196
07068d5b
JA
2197 if (q->nr_hw_queues > 1)
2198 blk_queue_make_request(q, blk_mq_make_request);
2199 else
2200 blk_queue_make_request(q, blk_sq_make_request);
2201
eba71768
JA
2202 /*
2203 * Do this after blk_queue_make_request() overrides it...
2204 */
2205 q->nr_requests = set->queue_depth;
2206
64f1c21e
JA
2207 /*
2208 * Default to classic polling
2209 */
2210 q->poll_nsec = -1;
2211
24d2f903
CH
2212 if (set->ops->complete)
2213 blk_queue_softirq_done(q, set->ops->complete);
30a91cb4 2214
24d2f903 2215 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
320ae51f 2216
5778322e 2217 get_online_cpus();
320ae51f 2218 mutex_lock(&all_q_mutex);
320ae51f 2219
4593fdbe 2220 list_add_tail(&q->all_q_node, &all_q_list);
0d2602ca 2221 blk_mq_add_queue_tag_set(set, q);
5778322e 2222 blk_mq_map_swqueue(q, cpu_online_mask);
484b4061 2223
4593fdbe 2224 mutex_unlock(&all_q_mutex);
5778322e 2225 put_online_cpus();
4593fdbe 2226
320ae51f 2227 return q;
18741986 2228
320ae51f 2229err_hctxs:
868f2f0b 2230 kfree(q->queue_hw_ctx);
320ae51f 2231err_percpu:
868f2f0b 2232 free_percpu(q->queue_ctx);
c7de5726
ML
2233err_exit:
2234 q->mq_ops = NULL;
320ae51f
JA
2235 return ERR_PTR(-ENOMEM);
2236}
b62c21b7 2237EXPORT_SYMBOL(blk_mq_init_allocated_queue);
320ae51f
JA
2238
2239void blk_mq_free_queue(struct request_queue *q)
2240{
624dbe47 2241 struct blk_mq_tag_set *set = q->tag_set;
320ae51f 2242
0e626368
AM
2243 mutex_lock(&all_q_mutex);
2244 list_del_init(&q->all_q_node);
2245 mutex_unlock(&all_q_mutex);
2246
87760e5e
JA
2247 wbt_exit(q);
2248
0d2602ca
JA
2249 blk_mq_del_queue_tag_set(q);
2250
624dbe47
ML
2251 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
2252 blk_mq_free_hw_queues(q, set);
320ae51f 2253}
320ae51f
JA
2254
2255/* Basically redo blk_mq_init_queue with queue frozen */
5778322e
AM
2256static void blk_mq_queue_reinit(struct request_queue *q,
2257 const struct cpumask *online_mask)
320ae51f 2258{
4ecd4fef 2259 WARN_ON_ONCE(!atomic_read(&q->mq_freeze_depth));
320ae51f 2260
67aec14c
JA
2261 blk_mq_sysfs_unregister(q);
2262
320ae51f
JA
2263 /*
2264 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
2265 * we should change hctx numa_node according to new topology (this
2266 * involves free and re-allocate memory, worthy doing?)
2267 */
2268
5778322e 2269 blk_mq_map_swqueue(q, online_mask);
320ae51f 2270
67aec14c 2271 blk_mq_sysfs_register(q);
320ae51f
JA
2272}
2273
65d5291e
SAS
2274/*
2275 * New online cpumask which is going to be set in this hotplug event.
2276 * Declare this cpumasks as global as cpu-hotplug operation is invoked
2277 * one-by-one and dynamically allocating this could result in a failure.
2278 */
2279static struct cpumask cpuhp_online_new;
2280
2281static void blk_mq_queue_reinit_work(void)
320ae51f
JA
2282{
2283 struct request_queue *q;
320ae51f
JA
2284
2285 mutex_lock(&all_q_mutex);
f3af020b
TH
2286 /*
2287 * We need to freeze and reinit all existing queues. Freezing
2288 * involves synchronous wait for an RCU grace period and doing it
2289 * one by one may take a long time. Start freezing all queues in
2290 * one swoop and then wait for the completions so that freezing can
2291 * take place in parallel.
2292 */
2293 list_for_each_entry(q, &all_q_list, all_q_node)
2294 blk_mq_freeze_queue_start(q);
415d3dab 2295 list_for_each_entry(q, &all_q_list, all_q_node)
f3af020b
TH
2296 blk_mq_freeze_queue_wait(q);
2297
320ae51f 2298 list_for_each_entry(q, &all_q_list, all_q_node)
65d5291e 2299 blk_mq_queue_reinit(q, &cpuhp_online_new);
f3af020b
TH
2300
2301 list_for_each_entry(q, &all_q_list, all_q_node)
2302 blk_mq_unfreeze_queue(q);
2303
320ae51f 2304 mutex_unlock(&all_q_mutex);
65d5291e
SAS
2305}
2306
2307static int blk_mq_queue_reinit_dead(unsigned int cpu)
2308{
97a32864 2309 cpumask_copy(&cpuhp_online_new, cpu_online_mask);
65d5291e
SAS
2310 blk_mq_queue_reinit_work();
2311 return 0;
2312}
2313
2314/*
2315 * Before hotadded cpu starts handling requests, new mappings must be
2316 * established. Otherwise, these requests in hw queue might never be
2317 * dispatched.
2318 *
2319 * For example, there is a single hw queue (hctx) and two CPU queues (ctx0
2320 * for CPU0, and ctx1 for CPU1).
2321 *
2322 * Now CPU1 is just onlined and a request is inserted into ctx1->rq_list
2323 * and set bit0 in pending bitmap as ctx1->index_hw is still zero.
2324 *
2c3ad667
JA
2325 * And then while running hw queue, blk_mq_flush_busy_ctxs() finds bit0 is set
2326 * in pending bitmap and tries to retrieve requests in hctx->ctxs[0]->rq_list.
2327 * But htx->ctxs[0] is a pointer to ctx0, so the request in ctx1->rq_list is
2328 * ignored.
65d5291e
SAS
2329 */
2330static int blk_mq_queue_reinit_prepare(unsigned int cpu)
2331{
2332 cpumask_copy(&cpuhp_online_new, cpu_online_mask);
2333 cpumask_set_cpu(cpu, &cpuhp_online_new);
2334 blk_mq_queue_reinit_work();
2335 return 0;
320ae51f
JA
2336}
2337
a5164405
JA
2338static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2339{
2340 int i;
2341
cc71a6f4
JA
2342 for (i = 0; i < set->nr_hw_queues; i++)
2343 if (!__blk_mq_alloc_rq_map(set, i))
a5164405 2344 goto out_unwind;
a5164405
JA
2345
2346 return 0;
2347
2348out_unwind:
2349 while (--i >= 0)
cc71a6f4 2350 blk_mq_free_rq_map(set->tags[i]);
a5164405 2351
a5164405
JA
2352 return -ENOMEM;
2353}
2354
2355/*
2356 * Allocate the request maps associated with this tag_set. Note that this
2357 * may reduce the depth asked for, if memory is tight. set->queue_depth
2358 * will be updated to reflect the allocated depth.
2359 */
2360static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2361{
2362 unsigned int depth;
2363 int err;
2364
2365 depth = set->queue_depth;
2366 do {
2367 err = __blk_mq_alloc_rq_maps(set);
2368 if (!err)
2369 break;
2370
2371 set->queue_depth >>= 1;
2372 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
2373 err = -ENOMEM;
2374 break;
2375 }
2376 } while (set->queue_depth);
2377
2378 if (!set->queue_depth || err) {
2379 pr_err("blk-mq: failed to allocate request map\n");
2380 return -ENOMEM;
2381 }
2382
2383 if (depth != set->queue_depth)
2384 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2385 depth, set->queue_depth);
2386
2387 return 0;
2388}
2389
a4391c64
JA
2390/*
2391 * Alloc a tag set to be associated with one or more request queues.
2392 * May fail with EINVAL for various error conditions. May adjust the
2393 * requested depth down, if if it too large. In that case, the set
2394 * value will be stored in set->queue_depth.
2395 */
24d2f903
CH
2396int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
2397{
da695ba2
CH
2398 int ret;
2399
205fb5f5
BVA
2400 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
2401
24d2f903
CH
2402 if (!set->nr_hw_queues)
2403 return -EINVAL;
a4391c64 2404 if (!set->queue_depth)
24d2f903
CH
2405 return -EINVAL;
2406 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
2407 return -EINVAL;
2408
7d7e0f90 2409 if (!set->ops->queue_rq)
24d2f903
CH
2410 return -EINVAL;
2411
a4391c64
JA
2412 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
2413 pr_info("blk-mq: reduced tag depth to %u\n",
2414 BLK_MQ_MAX_DEPTH);
2415 set->queue_depth = BLK_MQ_MAX_DEPTH;
2416 }
24d2f903 2417
6637fadf
SL
2418 /*
2419 * If a crashdump is active, then we are potentially in a very
2420 * memory constrained environment. Limit us to 1 queue and
2421 * 64 tags to prevent using too much memory.
2422 */
2423 if (is_kdump_kernel()) {
2424 set->nr_hw_queues = 1;
2425 set->queue_depth = min(64U, set->queue_depth);
2426 }
868f2f0b
KB
2427 /*
2428 * There is no use for more h/w queues than cpus.
2429 */
2430 if (set->nr_hw_queues > nr_cpu_ids)
2431 set->nr_hw_queues = nr_cpu_ids;
6637fadf 2432
868f2f0b 2433 set->tags = kzalloc_node(nr_cpu_ids * sizeof(struct blk_mq_tags *),
24d2f903
CH
2434 GFP_KERNEL, set->numa_node);
2435 if (!set->tags)
a5164405 2436 return -ENOMEM;
24d2f903 2437
da695ba2
CH
2438 ret = -ENOMEM;
2439 set->mq_map = kzalloc_node(sizeof(*set->mq_map) * nr_cpu_ids,
2440 GFP_KERNEL, set->numa_node);
bdd17e75
CH
2441 if (!set->mq_map)
2442 goto out_free_tags;
2443
da695ba2
CH
2444 if (set->ops->map_queues)
2445 ret = set->ops->map_queues(set);
2446 else
2447 ret = blk_mq_map_queues(set);
2448 if (ret)
2449 goto out_free_mq_map;
2450
2451 ret = blk_mq_alloc_rq_maps(set);
2452 if (ret)
bdd17e75 2453 goto out_free_mq_map;
24d2f903 2454
0d2602ca
JA
2455 mutex_init(&set->tag_list_lock);
2456 INIT_LIST_HEAD(&set->tag_list);
2457
24d2f903 2458 return 0;
bdd17e75
CH
2459
2460out_free_mq_map:
2461 kfree(set->mq_map);
2462 set->mq_map = NULL;
2463out_free_tags:
5676e7b6
RE
2464 kfree(set->tags);
2465 set->tags = NULL;
da695ba2 2466 return ret;
24d2f903
CH
2467}
2468EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2469
2470void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2471{
2472 int i;
2473
cc71a6f4
JA
2474 for (i = 0; i < nr_cpu_ids; i++)
2475 blk_mq_free_map_and_requests(set, i);
484b4061 2476
bdd17e75
CH
2477 kfree(set->mq_map);
2478 set->mq_map = NULL;
2479
981bd189 2480 kfree(set->tags);
5676e7b6 2481 set->tags = NULL;
24d2f903
CH
2482}
2483EXPORT_SYMBOL(blk_mq_free_tag_set);
2484
e3a2b3f9
JA
2485int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2486{
2487 struct blk_mq_tag_set *set = q->tag_set;
2488 struct blk_mq_hw_ctx *hctx;
2489 int i, ret;
2490
2491 if (!set || nr > set->queue_depth)
2492 return -EINVAL;
2493
2494 ret = 0;
2495 queue_for_each_hw_ctx(q, hctx, i) {
e9137d4b
KB
2496 if (!hctx->tags)
2497 continue;
e3a2b3f9
JA
2498 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2499 if (ret)
2500 break;
2501 }
2502
2503 if (!ret)
2504 q->nr_requests = nr;
2505
2506 return ret;
2507}
2508
868f2f0b
KB
2509void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
2510{
2511 struct request_queue *q;
2512
2513 if (nr_hw_queues > nr_cpu_ids)
2514 nr_hw_queues = nr_cpu_ids;
2515 if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues)
2516 return;
2517
2518 list_for_each_entry(q, &set->tag_list, tag_set_list)
2519 blk_mq_freeze_queue(q);
2520
2521 set->nr_hw_queues = nr_hw_queues;
2522 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2523 blk_mq_realloc_hw_ctxs(set, q);
2524
2525 if (q->nr_hw_queues > 1)
2526 blk_queue_make_request(q, blk_mq_make_request);
2527 else
2528 blk_queue_make_request(q, blk_sq_make_request);
2529
2530 blk_mq_queue_reinit(q, cpu_online_mask);
2531 }
2532
2533 list_for_each_entry(q, &set->tag_list, tag_set_list)
2534 blk_mq_unfreeze_queue(q);
2535}
2536EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
2537
64f1c21e
JA
2538static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
2539 struct blk_mq_hw_ctx *hctx,
2540 struct request *rq)
2541{
2542 struct blk_rq_stat stat[2];
2543 unsigned long ret = 0;
2544
2545 /*
2546 * If stats collection isn't on, don't sleep but turn it on for
2547 * future users
2548 */
2549 if (!blk_stat_enable(q))
2550 return 0;
2551
2552 /*
2553 * We don't have to do this once per IO, should optimize this
2554 * to just use the current window of stats until it changes
2555 */
2556 memset(&stat, 0, sizeof(stat));
2557 blk_hctx_stat_get(hctx, stat);
2558
2559 /*
2560 * As an optimistic guess, use half of the mean service time
2561 * for this type of request. We can (and should) make this smarter.
2562 * For instance, if the completion latencies are tight, we can
2563 * get closer than just half the mean. This is especially
2564 * important on devices where the completion latencies are longer
2565 * than ~10 usec.
2566 */
2567 if (req_op(rq) == REQ_OP_READ && stat[BLK_STAT_READ].nr_samples)
2568 ret = (stat[BLK_STAT_READ].mean + 1) / 2;
2569 else if (req_op(rq) == REQ_OP_WRITE && stat[BLK_STAT_WRITE].nr_samples)
2570 ret = (stat[BLK_STAT_WRITE].mean + 1) / 2;
2571
2572 return ret;
2573}
2574
06426adf 2575static bool blk_mq_poll_hybrid_sleep(struct request_queue *q,
64f1c21e 2576 struct blk_mq_hw_ctx *hctx,
06426adf
JA
2577 struct request *rq)
2578{
2579 struct hrtimer_sleeper hs;
2580 enum hrtimer_mode mode;
64f1c21e 2581 unsigned int nsecs;
06426adf
JA
2582 ktime_t kt;
2583
64f1c21e
JA
2584 if (test_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags))
2585 return false;
2586
2587 /*
2588 * poll_nsec can be:
2589 *
2590 * -1: don't ever hybrid sleep
2591 * 0: use half of prev avg
2592 * >0: use this specific value
2593 */
2594 if (q->poll_nsec == -1)
2595 return false;
2596 else if (q->poll_nsec > 0)
2597 nsecs = q->poll_nsec;
2598 else
2599 nsecs = blk_mq_poll_nsecs(q, hctx, rq);
2600
2601 if (!nsecs)
06426adf
JA
2602 return false;
2603
2604 set_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags);
2605
2606 /*
2607 * This will be replaced with the stats tracking code, using
2608 * 'avg_completion_time / 2' as the pre-sleep target.
2609 */
8b0e1953 2610 kt = nsecs;
06426adf
JA
2611
2612 mode = HRTIMER_MODE_REL;
2613 hrtimer_init_on_stack(&hs.timer, CLOCK_MONOTONIC, mode);
2614 hrtimer_set_expires(&hs.timer, kt);
2615
2616 hrtimer_init_sleeper(&hs, current);
2617 do {
2618 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
2619 break;
2620 set_current_state(TASK_UNINTERRUPTIBLE);
2621 hrtimer_start_expires(&hs.timer, mode);
2622 if (hs.task)
2623 io_schedule();
2624 hrtimer_cancel(&hs.timer);
2625 mode = HRTIMER_MODE_ABS;
2626 } while (hs.task && !signal_pending(current));
2627
2628 __set_current_state(TASK_RUNNING);
2629 destroy_hrtimer_on_stack(&hs.timer);
2630 return true;
2631}
2632
bbd7bb70
JA
2633static bool __blk_mq_poll(struct blk_mq_hw_ctx *hctx, struct request *rq)
2634{
2635 struct request_queue *q = hctx->queue;
2636 long state;
2637
06426adf
JA
2638 /*
2639 * If we sleep, have the caller restart the poll loop to reset
2640 * the state. Like for the other success return cases, the
2641 * caller is responsible for checking if the IO completed. If
2642 * the IO isn't complete, we'll get called again and will go
2643 * straight to the busy poll loop.
2644 */
64f1c21e 2645 if (blk_mq_poll_hybrid_sleep(q, hctx, rq))
06426adf
JA
2646 return true;
2647
bbd7bb70
JA
2648 hctx->poll_considered++;
2649
2650 state = current->state;
2651 while (!need_resched()) {
2652 int ret;
2653
2654 hctx->poll_invoked++;
2655
2656 ret = q->mq_ops->poll(hctx, rq->tag);
2657 if (ret > 0) {
2658 hctx->poll_success++;
2659 set_current_state(TASK_RUNNING);
2660 return true;
2661 }
2662
2663 if (signal_pending_state(state, current))
2664 set_current_state(TASK_RUNNING);
2665
2666 if (current->state == TASK_RUNNING)
2667 return true;
2668 if (ret < 0)
2669 break;
2670 cpu_relax();
2671 }
2672
2673 return false;
2674}
2675
2676bool blk_mq_poll(struct request_queue *q, blk_qc_t cookie)
2677{
2678 struct blk_mq_hw_ctx *hctx;
2679 struct blk_plug *plug;
2680 struct request *rq;
2681
2682 if (!q->mq_ops || !q->mq_ops->poll || !blk_qc_t_valid(cookie) ||
2683 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
2684 return false;
2685
2686 plug = current->plug;
2687 if (plug)
2688 blk_flush_plug_list(plug, false);
2689
2690 hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
2691 rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie));
2692
2693 return __blk_mq_poll(hctx, rq);
2694}
2695EXPORT_SYMBOL_GPL(blk_mq_poll);
2696
676141e4
JA
2697void blk_mq_disable_hotplug(void)
2698{
2699 mutex_lock(&all_q_mutex);
2700}
2701
2702void blk_mq_enable_hotplug(void)
2703{
2704 mutex_unlock(&all_q_mutex);
2705}
2706
320ae51f
JA
2707static int __init blk_mq_init(void)
2708{
9467f859
TG
2709 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
2710 blk_mq_hctx_notify_dead);
320ae51f 2711
65d5291e
SAS
2712 cpuhp_setup_state_nocalls(CPUHP_BLK_MQ_PREPARE, "block/mq:prepare",
2713 blk_mq_queue_reinit_prepare,
2714 blk_mq_queue_reinit_dead);
320ae51f
JA
2715 return 0;
2716}
2717subsys_initcall(blk_mq_init);