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