]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - block/blk-mq.c
blk-mq: Avoid race condition with uninitialized requests
[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>
12#include <linux/mm.h>
13#include <linux/init.h>
14#include <linux/slab.h>
15#include <linux/workqueue.h>
16#include <linux/smp.h>
17#include <linux/llist.h>
18#include <linux/list_sort.h>
19#include <linux/cpu.h>
20#include <linux/cache.h>
21#include <linux/sched/sysctl.h>
22#include <linux/delay.h>
23
24#include <trace/events/block.h>
25
26#include <linux/blk-mq.h>
27#include "blk.h"
28#include "blk-mq.h"
29#include "blk-mq-tag.h"
30
31static DEFINE_MUTEX(all_q_mutex);
32static LIST_HEAD(all_q_list);
33
34static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
35
320ae51f
JA
36/*
37 * Check if any of the ctx's have pending work in this hardware queue
38 */
39static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
40{
41 unsigned int i;
42
1429d7c9
JA
43 for (i = 0; i < hctx->ctx_map.map_size; i++)
44 if (hctx->ctx_map.map[i].word)
320ae51f
JA
45 return true;
46
47 return false;
48}
49
1429d7c9
JA
50static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
51 struct blk_mq_ctx *ctx)
52{
53 return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
54}
55
56#define CTX_TO_BIT(hctx, ctx) \
57 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
58
320ae51f
JA
59/*
60 * Mark this ctx as having pending work in this hardware queue
61 */
62static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
63 struct blk_mq_ctx *ctx)
64{
1429d7c9
JA
65 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
66
67 if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
68 set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
69}
70
71static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
72 struct blk_mq_ctx *ctx)
73{
74 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
75
76 clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
320ae51f
JA
77}
78
320ae51f
JA
79static int blk_mq_queue_enter(struct request_queue *q)
80{
add703fd
TH
81 while (true) {
82 int ret;
320ae51f 83
add703fd
TH
84 if (percpu_ref_tryget_live(&q->mq_usage_counter))
85 return 0;
320ae51f 86
add703fd
TH
87 ret = wait_event_interruptible(q->mq_freeze_wq,
88 !q->mq_freeze_depth || blk_queue_dying(q));
89 if (blk_queue_dying(q))
90 return -ENODEV;
91 if (ret)
92 return ret;
93 }
320ae51f
JA
94}
95
96static void blk_mq_queue_exit(struct request_queue *q)
97{
add703fd
TH
98 percpu_ref_put(&q->mq_usage_counter);
99}
100
101static void blk_mq_usage_counter_release(struct percpu_ref *ref)
102{
103 struct request_queue *q =
104 container_of(ref, struct request_queue, mq_usage_counter);
105
106 wake_up_all(&q->mq_freeze_wq);
320ae51f
JA
107}
108
72d6f02a
TH
109/*
110 * Guarantee no request is in use, so we can change any data structure of
111 * the queue afterward.
112 */
113void blk_mq_freeze_queue(struct request_queue *q)
43a5e4e2 114{
cddd5d17
TH
115 bool freeze;
116
72d6f02a 117 spin_lock_irq(q->queue_lock);
cddd5d17 118 freeze = !q->mq_freeze_depth++;
72d6f02a
TH
119 spin_unlock_irq(q->queue_lock);
120
cddd5d17
TH
121 if (freeze) {
122 percpu_ref_kill(&q->mq_usage_counter);
123 blk_mq_run_queues(q, false);
124 }
add703fd 125 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->mq_usage_counter));
43a5e4e2
ML
126}
127
320ae51f
JA
128static void blk_mq_unfreeze_queue(struct request_queue *q)
129{
cddd5d17 130 bool wake;
320ae51f
JA
131
132 spin_lock_irq(q->queue_lock);
780db207
TH
133 wake = !--q->mq_freeze_depth;
134 WARN_ON_ONCE(q->mq_freeze_depth < 0);
320ae51f 135 spin_unlock_irq(q->queue_lock);
add703fd
TH
136 if (wake) {
137 percpu_ref_reinit(&q->mq_usage_counter);
320ae51f 138 wake_up_all(&q->mq_freeze_wq);
add703fd 139 }
320ae51f
JA
140}
141
142bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
143{
144 return blk_mq_has_free_tags(hctx->tags);
145}
146EXPORT_SYMBOL(blk_mq_can_queue);
147
94eddfbe
JA
148static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
149 struct request *rq, unsigned int rw_flags)
320ae51f 150{
94eddfbe
JA
151 if (blk_queue_io_stat(q))
152 rw_flags |= REQ_IO_STAT;
153
af76e555
CH
154 INIT_LIST_HEAD(&rq->queuelist);
155 /* csd/requeue_work/fifo_time is initialized before use */
156 rq->q = q;
320ae51f 157 rq->mq_ctx = ctx;
0d2602ca 158 rq->cmd_flags |= rw_flags;
af76e555
CH
159 /* do not touch atomic flags, it needs atomic ops against the timer */
160 rq->cpu = -1;
af76e555
CH
161 INIT_HLIST_NODE(&rq->hash);
162 RB_CLEAR_NODE(&rq->rb_node);
af76e555
CH
163 rq->rq_disk = NULL;
164 rq->part = NULL;
3ee32372 165 rq->start_time = jiffies;
af76e555
CH
166#ifdef CONFIG_BLK_CGROUP
167 rq->rl = NULL;
0fec08b4 168 set_start_time_ns(rq);
af76e555
CH
169 rq->io_start_time_ns = 0;
170#endif
171 rq->nr_phys_segments = 0;
172#if defined(CONFIG_BLK_DEV_INTEGRITY)
173 rq->nr_integrity_segments = 0;
174#endif
af76e555
CH
175 rq->special = NULL;
176 /* tag was already set */
177 rq->errors = 0;
af76e555 178
6f4a1626
TB
179 rq->cmd = rq->__cmd;
180
af76e555
CH
181 rq->extra_len = 0;
182 rq->sense_len = 0;
183 rq->resid_len = 0;
184 rq->sense = NULL;
185
af76e555 186 INIT_LIST_HEAD(&rq->timeout_list);
f6be4fb4
JA
187 rq->timeout = 0;
188
af76e555
CH
189 rq->end_io = NULL;
190 rq->end_io_data = NULL;
191 rq->next_rq = NULL;
192
320ae51f
JA
193 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
194}
195
5dee8577 196static struct request *
cb96a42c 197__blk_mq_alloc_request(struct blk_mq_alloc_data *data, int rw)
5dee8577
CH
198{
199 struct request *rq;
200 unsigned int tag;
201
cb96a42c 202 tag = blk_mq_get_tag(data);
5dee8577 203 if (tag != BLK_MQ_TAG_FAIL) {
cb96a42c 204 rq = data->hctx->tags->rqs[tag];
5dee8577 205
cb96a42c 206 if (blk_mq_tag_busy(data->hctx)) {
5dee8577 207 rq->cmd_flags = REQ_MQ_INFLIGHT;
cb96a42c 208 atomic_inc(&data->hctx->nr_active);
5dee8577
CH
209 }
210
211 rq->tag = tag;
cb96a42c 212 blk_mq_rq_ctx_init(data->q, data->ctx, rq, rw);
5dee8577
CH
213 return rq;
214 }
215
216 return NULL;
217}
218
4ce01dd1
CH
219struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
220 bool reserved)
320ae51f 221{
d852564f
CH
222 struct blk_mq_ctx *ctx;
223 struct blk_mq_hw_ctx *hctx;
320ae51f 224 struct request *rq;
cb96a42c 225 struct blk_mq_alloc_data alloc_data;
320ae51f
JA
226
227 if (blk_mq_queue_enter(q))
228 return NULL;
229
d852564f
CH
230 ctx = blk_mq_get_ctx(q);
231 hctx = q->mq_ops->map_queue(q, ctx->cpu);
cb96a42c
ML
232 blk_mq_set_alloc_data(&alloc_data, q, gfp & ~__GFP_WAIT,
233 reserved, ctx, hctx);
d852564f 234
cb96a42c 235 rq = __blk_mq_alloc_request(&alloc_data, rw);
d852564f
CH
236 if (!rq && (gfp & __GFP_WAIT)) {
237 __blk_mq_run_hw_queue(hctx);
238 blk_mq_put_ctx(ctx);
239
240 ctx = blk_mq_get_ctx(q);
241 hctx = q->mq_ops->map_queue(q, ctx->cpu);
cb96a42c
ML
242 blk_mq_set_alloc_data(&alloc_data, q, gfp, reserved, ctx,
243 hctx);
244 rq = __blk_mq_alloc_request(&alloc_data, rw);
245 ctx = alloc_data.ctx;
d852564f
CH
246 }
247 blk_mq_put_ctx(ctx);
320ae51f
JA
248 return rq;
249}
4bb659b1 250EXPORT_SYMBOL(blk_mq_alloc_request);
320ae51f 251
320ae51f
JA
252static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
253 struct blk_mq_ctx *ctx, struct request *rq)
254{
255 const int tag = rq->tag;
256 struct request_queue *q = rq->q;
257
0d2602ca
JA
258 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
259 atomic_dec(&hctx->nr_active);
683d0e12 260 rq->cmd_flags = 0;
0d2602ca 261
af76e555 262 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
0d2602ca 263 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
320ae51f
JA
264 blk_mq_queue_exit(q);
265}
266
267void blk_mq_free_request(struct request *rq)
268{
269 struct blk_mq_ctx *ctx = rq->mq_ctx;
270 struct blk_mq_hw_ctx *hctx;
271 struct request_queue *q = rq->q;
272
273 ctx->rq_completed[rq_is_sync(rq)]++;
274
275 hctx = q->mq_ops->map_queue(q, ctx->cpu);
276 __blk_mq_free_request(hctx, ctx, rq);
277}
278
8727af4b
CH
279/*
280 * Clone all relevant state from a request that has been put on hold in
281 * the flush state machine into the preallocated flush request that hangs
282 * off the request queue.
283 *
284 * For a driver the flush request should be invisible, that's why we are
285 * impersonating the original request here.
286 */
287void blk_mq_clone_flush_request(struct request *flush_rq,
288 struct request *orig_rq)
289{
290 struct blk_mq_hw_ctx *hctx =
291 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
292
293 flush_rq->mq_ctx = orig_rq->mq_ctx;
294 flush_rq->tag = orig_rq->tag;
295 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
296 hctx->cmd_size);
297}
298
63151a44 299inline void __blk_mq_end_io(struct request *rq, int error)
320ae51f 300{
0d11e6ac
ML
301 blk_account_io_done(rq);
302
91b63639 303 if (rq->end_io) {
320ae51f 304 rq->end_io(rq, error);
91b63639
CH
305 } else {
306 if (unlikely(blk_bidi_rq(rq)))
307 blk_mq_free_request(rq->next_rq);
320ae51f 308 blk_mq_free_request(rq);
91b63639 309 }
320ae51f 310}
63151a44
CH
311EXPORT_SYMBOL(__blk_mq_end_io);
312
313void blk_mq_end_io(struct request *rq, int error)
314{
315 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
316 BUG();
317 __blk_mq_end_io(rq, error);
318}
319EXPORT_SYMBOL(blk_mq_end_io);
320ae51f 320
30a91cb4 321static void __blk_mq_complete_request_remote(void *data)
320ae51f 322{
3d6efbf6 323 struct request *rq = data;
320ae51f 324
30a91cb4 325 rq->q->softirq_done_fn(rq);
320ae51f 326}
320ae51f 327
ed851860 328static void blk_mq_ipi_complete_request(struct request *rq)
320ae51f
JA
329{
330 struct blk_mq_ctx *ctx = rq->mq_ctx;
38535201 331 bool shared = false;
320ae51f
JA
332 int cpu;
333
38535201 334 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
30a91cb4
CH
335 rq->q->softirq_done_fn(rq);
336 return;
337 }
320ae51f
JA
338
339 cpu = get_cpu();
38535201
CH
340 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
341 shared = cpus_share_cache(cpu, ctx->cpu);
342
343 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
30a91cb4 344 rq->csd.func = __blk_mq_complete_request_remote;
3d6efbf6
CH
345 rq->csd.info = rq;
346 rq->csd.flags = 0;
c46fff2a 347 smp_call_function_single_async(ctx->cpu, &rq->csd);
3d6efbf6 348 } else {
30a91cb4 349 rq->q->softirq_done_fn(rq);
3d6efbf6 350 }
320ae51f
JA
351 put_cpu();
352}
30a91cb4 353
ed851860
JA
354void __blk_mq_complete_request(struct request *rq)
355{
356 struct request_queue *q = rq->q;
357
358 if (!q->softirq_done_fn)
359 blk_mq_end_io(rq, rq->errors);
360 else
361 blk_mq_ipi_complete_request(rq);
362}
363
30a91cb4
CH
364/**
365 * blk_mq_complete_request - end I/O on a request
366 * @rq: the request being processed
367 *
368 * Description:
369 * Ends all I/O on a request. It does not handle partial completions.
370 * The actual completion happens out-of-order, through a IPI handler.
371 **/
372void blk_mq_complete_request(struct request *rq)
373{
95f09684
JA
374 struct request_queue *q = rq->q;
375
376 if (unlikely(blk_should_fake_timeout(q)))
30a91cb4 377 return;
ed851860
JA
378 if (!blk_mark_rq_complete(rq))
379 __blk_mq_complete_request(rq);
30a91cb4
CH
380}
381EXPORT_SYMBOL(blk_mq_complete_request);
320ae51f 382
49f5baa5 383static void blk_mq_start_request(struct request *rq, bool last)
320ae51f
JA
384{
385 struct request_queue *q = rq->q;
386
387 trace_block_rq_issue(q, rq);
388
742ee69b 389 rq->resid_len = blk_rq_bytes(rq);
91b63639
CH
390 if (unlikely(blk_bidi_rq(rq)))
391 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
742ee69b 392
2b8393b4 393 blk_add_timer(rq);
87ee7b11 394
538b7534
JA
395 /*
396 * Ensure that ->deadline is visible before set the started
397 * flag and clear the completed flag.
398 */
399 smp_mb__before_atomic();
400
87ee7b11
JA
401 /*
402 * Mark us as started and clear complete. Complete might have been
403 * set if requeue raced with timeout, which then marked it as
404 * complete. So be sure to clear complete again when we start
405 * the request, otherwise we'll ignore the completion event.
406 */
4b570521
JA
407 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
408 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
409 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
410 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
49f5baa5
CH
411
412 if (q->dma_drain_size && blk_rq_bytes(rq)) {
413 /*
414 * Make sure space for the drain appears. We know we can do
415 * this because max_hw_segments has been adjusted to be one
416 * fewer than the device can handle.
417 */
418 rq->nr_phys_segments++;
419 }
420
421 /*
422 * Flag the last request in the series so that drivers know when IO
423 * should be kicked off, if they don't do it on a per-request basis.
424 *
425 * Note: the flag isn't the only condition drivers should do kick off.
426 * If drive is busy, the last request might not have the bit set.
427 */
428 if (last)
429 rq->cmd_flags |= REQ_END;
320ae51f
JA
430}
431
ed0791b2 432static void __blk_mq_requeue_request(struct request *rq)
320ae51f
JA
433{
434 struct request_queue *q = rq->q;
435
436 trace_block_rq_requeue(q, rq);
437 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
49f5baa5
CH
438
439 rq->cmd_flags &= ~REQ_END;
440
441 if (q->dma_drain_size && blk_rq_bytes(rq))
442 rq->nr_phys_segments--;
320ae51f
JA
443}
444
ed0791b2
CH
445void blk_mq_requeue_request(struct request *rq)
446{
ed0791b2
CH
447 __blk_mq_requeue_request(rq);
448 blk_clear_rq_complete(rq);
449
ed0791b2 450 BUG_ON(blk_queued_rq(rq));
6fca6a61 451 blk_mq_add_to_requeue_list(rq, true);
ed0791b2
CH
452}
453EXPORT_SYMBOL(blk_mq_requeue_request);
454
6fca6a61
CH
455static void blk_mq_requeue_work(struct work_struct *work)
456{
457 struct request_queue *q =
458 container_of(work, struct request_queue, requeue_work);
459 LIST_HEAD(rq_list);
460 struct request *rq, *next;
461 unsigned long flags;
462
463 spin_lock_irqsave(&q->requeue_lock, flags);
464 list_splice_init(&q->requeue_list, &rq_list);
465 spin_unlock_irqrestore(&q->requeue_lock, flags);
466
467 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
468 if (!(rq->cmd_flags & REQ_SOFTBARRIER))
469 continue;
470
471 rq->cmd_flags &= ~REQ_SOFTBARRIER;
472 list_del_init(&rq->queuelist);
473 blk_mq_insert_request(rq, true, false, false);
474 }
475
476 while (!list_empty(&rq_list)) {
477 rq = list_entry(rq_list.next, struct request, queuelist);
478 list_del_init(&rq->queuelist);
479 blk_mq_insert_request(rq, false, false, false);
480 }
481
482 blk_mq_run_queues(q, false);
483}
484
485void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
486{
487 struct request_queue *q = rq->q;
488 unsigned long flags;
489
490 /*
491 * We abuse this flag that is otherwise used by the I/O scheduler to
492 * request head insertation from the workqueue.
493 */
494 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
495
496 spin_lock_irqsave(&q->requeue_lock, flags);
497 if (at_head) {
498 rq->cmd_flags |= REQ_SOFTBARRIER;
499 list_add(&rq->queuelist, &q->requeue_list);
500 } else {
501 list_add_tail(&rq->queuelist, &q->requeue_list);
502 }
503 spin_unlock_irqrestore(&q->requeue_lock, flags);
504}
505EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
506
507void blk_mq_kick_requeue_list(struct request_queue *q)
508{
509 kblockd_schedule_work(&q->requeue_work);
510}
511EXPORT_SYMBOL(blk_mq_kick_requeue_list);
512
0e62f51f 513static inline bool is_flush_request(struct request *rq, unsigned int tag)
24d2f903 514{
0e62f51f
JA
515 return ((rq->cmd_flags & REQ_FLUSH_SEQ) &&
516 rq->q->flush_rq->tag == tag);
517}
518
519struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
520{
521 struct request *rq = tags->rqs[tag];
22302375 522
0e62f51f
JA
523 if (!is_flush_request(rq, tag))
524 return rq;
22302375 525
0e62f51f 526 return rq->q->flush_rq;
24d2f903
CH
527}
528EXPORT_SYMBOL(blk_mq_tag_to_rq);
529
320ae51f
JA
530struct blk_mq_timeout_data {
531 struct blk_mq_hw_ctx *hctx;
532 unsigned long *next;
533 unsigned int *next_set;
534};
535
536static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
537{
538 struct blk_mq_timeout_data *data = __data;
539 struct blk_mq_hw_ctx *hctx = data->hctx;
540 unsigned int tag;
541
542 /* It may not be in flight yet (this is where
543 * the REQ_ATOMIC_STARTED flag comes in). The requests are
544 * statically allocated, so we know it's always safe to access the
545 * memory associated with a bit offset into ->rqs[].
546 */
547 tag = 0;
548 do {
549 struct request *rq;
550
24d2f903
CH
551 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
552 if (tag >= hctx->tags->nr_tags)
320ae51f
JA
553 break;
554
0e62f51f 555 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
24d2f903
CH
556 if (rq->q != hctx->queue)
557 continue;
320ae51f
JA
558 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
559 continue;
560
561 blk_rq_check_expired(rq, data->next, data->next_set);
562 } while (1);
563}
564
565static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
566 unsigned long *next,
567 unsigned int *next_set)
568{
569 struct blk_mq_timeout_data data = {
570 .hctx = hctx,
571 .next = next,
572 .next_set = next_set,
573 };
574
575 /*
576 * Ask the tagging code to iterate busy requests, so we can
577 * check them for timeout.
578 */
579 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
580}
581
87ee7b11
JA
582static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
583{
584 struct request_queue *q = rq->q;
585
586 /*
587 * We know that complete is set at this point. If STARTED isn't set
588 * anymore, then the request isn't active and the "timeout" should
589 * just be ignored. This can happen due to the bitflag ordering.
590 * Timeout first checks if STARTED is set, and if it is, assumes
591 * the request is active. But if we race with completion, then
592 * we both flags will get cleared. So check here again, and ignore
593 * a timeout event with a request that isn't active.
594 */
595 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
596 return BLK_EH_NOT_HANDLED;
597
598 if (!q->mq_ops->timeout)
599 return BLK_EH_RESET_TIMER;
600
601 return q->mq_ops->timeout(rq);
602}
603
320ae51f
JA
604static void blk_mq_rq_timer(unsigned long data)
605{
606 struct request_queue *q = (struct request_queue *) data;
607 struct blk_mq_hw_ctx *hctx;
608 unsigned long next = 0;
609 int i, next_set = 0;
610
484b4061
JA
611 queue_for_each_hw_ctx(q, hctx, i) {
612 /*
613 * If not software queues are currently mapped to this
614 * hardware queue, there's nothing to check
615 */
616 if (!hctx->nr_ctx || !hctx->tags)
617 continue;
618
320ae51f 619 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
484b4061 620 }
320ae51f 621
0d2602ca
JA
622 if (next_set) {
623 next = blk_rq_timeout(round_jiffies_up(next));
624 mod_timer(&q->timeout, next);
625 } else {
626 queue_for_each_hw_ctx(q, hctx, i)
627 blk_mq_tag_idle(hctx);
628 }
320ae51f
JA
629}
630
631/*
632 * Reverse check our software queue for entries that we could potentially
633 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
634 * too much time checking for merges.
635 */
636static bool blk_mq_attempt_merge(struct request_queue *q,
637 struct blk_mq_ctx *ctx, struct bio *bio)
638{
639 struct request *rq;
640 int checked = 8;
641
642 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
643 int el_ret;
644
645 if (!checked--)
646 break;
647
648 if (!blk_rq_merge_ok(rq, bio))
649 continue;
650
651 el_ret = blk_try_merge(rq, bio);
652 if (el_ret == ELEVATOR_BACK_MERGE) {
653 if (bio_attempt_back_merge(q, rq, bio)) {
654 ctx->rq_merged++;
655 return true;
656 }
657 break;
658 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
659 if (bio_attempt_front_merge(q, rq, bio)) {
660 ctx->rq_merged++;
661 return true;
662 }
663 break;
664 }
665 }
666
667 return false;
668}
669
1429d7c9
JA
670/*
671 * Process software queues that have been marked busy, splicing them
672 * to the for-dispatch
673 */
674static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
675{
676 struct blk_mq_ctx *ctx;
677 int i;
678
679 for (i = 0; i < hctx->ctx_map.map_size; i++) {
680 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
681 unsigned int off, bit;
682
683 if (!bm->word)
684 continue;
685
686 bit = 0;
687 off = i * hctx->ctx_map.bits_per_word;
688 do {
689 bit = find_next_bit(&bm->word, bm->depth, bit);
690 if (bit >= bm->depth)
691 break;
692
693 ctx = hctx->ctxs[bit + off];
694 clear_bit(bit, &bm->word);
695 spin_lock(&ctx->lock);
696 list_splice_tail_init(&ctx->rq_list, list);
697 spin_unlock(&ctx->lock);
698
699 bit++;
700 } while (1);
701 }
702}
703
320ae51f
JA
704/*
705 * Run this hardware queue, pulling any software queues mapped to it in.
706 * Note that this function currently has various problems around ordering
707 * of IO. In particular, we'd like FIFO behaviour on handling existing
708 * items on the hctx->dispatch list. Ignore that for now.
709 */
710static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
711{
712 struct request_queue *q = hctx->queue;
320ae51f
JA
713 struct request *rq;
714 LIST_HEAD(rq_list);
1429d7c9 715 int queued;
320ae51f 716
fd1270d5 717 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
e4043dcf 718
5d12f905 719 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
320ae51f
JA
720 return;
721
722 hctx->run++;
723
724 /*
725 * Touch any software queue that has pending entries.
726 */
1429d7c9 727 flush_busy_ctxs(hctx, &rq_list);
320ae51f
JA
728
729 /*
730 * If we have previous entries on our dispatch list, grab them
731 * and stuff them at the front for more fair dispatch.
732 */
733 if (!list_empty_careful(&hctx->dispatch)) {
734 spin_lock(&hctx->lock);
735 if (!list_empty(&hctx->dispatch))
736 list_splice_init(&hctx->dispatch, &rq_list);
737 spin_unlock(&hctx->lock);
738 }
739
320ae51f
JA
740 /*
741 * Now process all the entries, sending them to the driver.
742 */
1429d7c9 743 queued = 0;
320ae51f
JA
744 while (!list_empty(&rq_list)) {
745 int ret;
746
747 rq = list_first_entry(&rq_list, struct request, queuelist);
748 list_del_init(&rq->queuelist);
320ae51f 749
49f5baa5 750 blk_mq_start_request(rq, list_empty(&rq_list));
320ae51f
JA
751
752 ret = q->mq_ops->queue_rq(hctx, rq);
753 switch (ret) {
754 case BLK_MQ_RQ_QUEUE_OK:
755 queued++;
756 continue;
757 case BLK_MQ_RQ_QUEUE_BUSY:
320ae51f 758 list_add(&rq->queuelist, &rq_list);
ed0791b2 759 __blk_mq_requeue_request(rq);
320ae51f
JA
760 break;
761 default:
762 pr_err("blk-mq: bad return on queue: %d\n", ret);
320ae51f 763 case BLK_MQ_RQ_QUEUE_ERROR:
1e93b8c2 764 rq->errors = -EIO;
320ae51f
JA
765 blk_mq_end_io(rq, rq->errors);
766 break;
767 }
768
769 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
770 break;
771 }
772
773 if (!queued)
774 hctx->dispatched[0]++;
775 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
776 hctx->dispatched[ilog2(queued) + 1]++;
777
778 /*
779 * Any items that need requeuing? Stuff them into hctx->dispatch,
780 * that is where we will continue on next queue run.
781 */
782 if (!list_empty(&rq_list)) {
783 spin_lock(&hctx->lock);
784 list_splice(&rq_list, &hctx->dispatch);
785 spin_unlock(&hctx->lock);
786 }
787}
788
506e931f
JA
789/*
790 * It'd be great if the workqueue API had a way to pass
791 * in a mask and had some smarts for more clever placement.
792 * For now we just round-robin here, switching for every
793 * BLK_MQ_CPU_WORK_BATCH queued items.
794 */
795static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
796{
797 int cpu = hctx->next_cpu;
798
799 if (--hctx->next_cpu_batch <= 0) {
800 int next_cpu;
801
802 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
803 if (next_cpu >= nr_cpu_ids)
804 next_cpu = cpumask_first(hctx->cpumask);
805
806 hctx->next_cpu = next_cpu;
807 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
808 }
809
810 return cpu;
811}
812
320ae51f
JA
813void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
814{
5d12f905 815 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
320ae51f
JA
816 return;
817
e4043dcf 818 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
320ae51f 819 __blk_mq_run_hw_queue(hctx);
e4043dcf 820 else if (hctx->queue->nr_hw_queues == 1)
70f4db63 821 kblockd_schedule_delayed_work(&hctx->run_work, 0);
e4043dcf
JA
822 else {
823 unsigned int cpu;
824
506e931f 825 cpu = blk_mq_hctx_next_cpu(hctx);
70f4db63 826 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
e4043dcf 827 }
320ae51f
JA
828}
829
830void blk_mq_run_queues(struct request_queue *q, bool async)
831{
832 struct blk_mq_hw_ctx *hctx;
833 int i;
834
835 queue_for_each_hw_ctx(q, hctx, i) {
836 if ((!blk_mq_hctx_has_pending(hctx) &&
837 list_empty_careful(&hctx->dispatch)) ||
5d12f905 838 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
320ae51f
JA
839 continue;
840
e4043dcf 841 preempt_disable();
320ae51f 842 blk_mq_run_hw_queue(hctx, async);
e4043dcf 843 preempt_enable();
320ae51f
JA
844 }
845}
846EXPORT_SYMBOL(blk_mq_run_queues);
847
848void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
849{
70f4db63
CH
850 cancel_delayed_work(&hctx->run_work);
851 cancel_delayed_work(&hctx->delay_work);
320ae51f
JA
852 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
853}
854EXPORT_SYMBOL(blk_mq_stop_hw_queue);
855
280d45f6
CH
856void blk_mq_stop_hw_queues(struct request_queue *q)
857{
858 struct blk_mq_hw_ctx *hctx;
859 int i;
860
861 queue_for_each_hw_ctx(q, hctx, i)
862 blk_mq_stop_hw_queue(hctx);
863}
864EXPORT_SYMBOL(blk_mq_stop_hw_queues);
865
320ae51f
JA
866void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
867{
868 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
e4043dcf
JA
869
870 preempt_disable();
0ffbce80 871 blk_mq_run_hw_queue(hctx, false);
e4043dcf 872 preempt_enable();
320ae51f
JA
873}
874EXPORT_SYMBOL(blk_mq_start_hw_queue);
875
2f268556
CH
876void blk_mq_start_hw_queues(struct request_queue *q)
877{
878 struct blk_mq_hw_ctx *hctx;
879 int i;
880
881 queue_for_each_hw_ctx(q, hctx, i)
882 blk_mq_start_hw_queue(hctx);
883}
884EXPORT_SYMBOL(blk_mq_start_hw_queues);
885
886
1b4a3258 887void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
320ae51f
JA
888{
889 struct blk_mq_hw_ctx *hctx;
890 int i;
891
892 queue_for_each_hw_ctx(q, hctx, i) {
893 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
894 continue;
895
896 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
e4043dcf 897 preempt_disable();
1b4a3258 898 blk_mq_run_hw_queue(hctx, async);
e4043dcf 899 preempt_enable();
320ae51f
JA
900 }
901}
902EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
903
70f4db63 904static void blk_mq_run_work_fn(struct work_struct *work)
320ae51f
JA
905{
906 struct blk_mq_hw_ctx *hctx;
907
70f4db63 908 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
e4043dcf 909
320ae51f
JA
910 __blk_mq_run_hw_queue(hctx);
911}
912
70f4db63
CH
913static void blk_mq_delay_work_fn(struct work_struct *work)
914{
915 struct blk_mq_hw_ctx *hctx;
916
917 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
918
919 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
920 __blk_mq_run_hw_queue(hctx);
921}
922
923void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
924{
925 unsigned long tmo = msecs_to_jiffies(msecs);
926
927 if (hctx->queue->nr_hw_queues == 1)
928 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
929 else {
930 unsigned int cpu;
931
506e931f 932 cpu = blk_mq_hctx_next_cpu(hctx);
70f4db63
CH
933 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
934 }
935}
936EXPORT_SYMBOL(blk_mq_delay_queue);
937
320ae51f 938static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
72a0a36e 939 struct request *rq, bool at_head)
320ae51f
JA
940{
941 struct blk_mq_ctx *ctx = rq->mq_ctx;
942
01b983c9
JA
943 trace_block_rq_insert(hctx->queue, rq);
944
72a0a36e
CH
945 if (at_head)
946 list_add(&rq->queuelist, &ctx->rq_list);
947 else
948 list_add_tail(&rq->queuelist, &ctx->rq_list);
4bb659b1 949
320ae51f 950 blk_mq_hctx_mark_pending(hctx, ctx);
320ae51f
JA
951}
952
eeabc850
CH
953void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
954 bool async)
320ae51f 955{
eeabc850 956 struct request_queue *q = rq->q;
320ae51f 957 struct blk_mq_hw_ctx *hctx;
eeabc850
CH
958 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
959
960 current_ctx = blk_mq_get_ctx(q);
961 if (!cpu_online(ctx->cpu))
962 rq->mq_ctx = ctx = current_ctx;
320ae51f 963
320ae51f
JA
964 hctx = q->mq_ops->map_queue(q, ctx->cpu);
965
eeabc850
CH
966 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
967 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
320ae51f
JA
968 blk_insert_flush(rq);
969 } else {
320ae51f 970 spin_lock(&ctx->lock);
72a0a36e 971 __blk_mq_insert_request(hctx, rq, at_head);
320ae51f 972 spin_unlock(&ctx->lock);
320ae51f
JA
973 }
974
320ae51f
JA
975 if (run_queue)
976 blk_mq_run_hw_queue(hctx, async);
e4043dcf
JA
977
978 blk_mq_put_ctx(current_ctx);
320ae51f
JA
979}
980
981static void blk_mq_insert_requests(struct request_queue *q,
982 struct blk_mq_ctx *ctx,
983 struct list_head *list,
984 int depth,
985 bool from_schedule)
986
987{
988 struct blk_mq_hw_ctx *hctx;
989 struct blk_mq_ctx *current_ctx;
990
991 trace_block_unplug(q, depth, !from_schedule);
992
993 current_ctx = blk_mq_get_ctx(q);
994
995 if (!cpu_online(ctx->cpu))
996 ctx = current_ctx;
997 hctx = q->mq_ops->map_queue(q, ctx->cpu);
998
999 /*
1000 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1001 * offline now
1002 */
1003 spin_lock(&ctx->lock);
1004 while (!list_empty(list)) {
1005 struct request *rq;
1006
1007 rq = list_first_entry(list, struct request, queuelist);
1008 list_del_init(&rq->queuelist);
1009 rq->mq_ctx = ctx;
72a0a36e 1010 __blk_mq_insert_request(hctx, rq, false);
320ae51f
JA
1011 }
1012 spin_unlock(&ctx->lock);
1013
320ae51f 1014 blk_mq_run_hw_queue(hctx, from_schedule);
e4043dcf 1015 blk_mq_put_ctx(current_ctx);
320ae51f
JA
1016}
1017
1018static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1019{
1020 struct request *rqa = container_of(a, struct request, queuelist);
1021 struct request *rqb = container_of(b, struct request, queuelist);
1022
1023 return !(rqa->mq_ctx < rqb->mq_ctx ||
1024 (rqa->mq_ctx == rqb->mq_ctx &&
1025 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1026}
1027
1028void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1029{
1030 struct blk_mq_ctx *this_ctx;
1031 struct request_queue *this_q;
1032 struct request *rq;
1033 LIST_HEAD(list);
1034 LIST_HEAD(ctx_list);
1035 unsigned int depth;
1036
1037 list_splice_init(&plug->mq_list, &list);
1038
1039 list_sort(NULL, &list, plug_ctx_cmp);
1040
1041 this_q = NULL;
1042 this_ctx = NULL;
1043 depth = 0;
1044
1045 while (!list_empty(&list)) {
1046 rq = list_entry_rq(list.next);
1047 list_del_init(&rq->queuelist);
1048 BUG_ON(!rq->q);
1049 if (rq->mq_ctx != this_ctx) {
1050 if (this_ctx) {
1051 blk_mq_insert_requests(this_q, this_ctx,
1052 &ctx_list, depth,
1053 from_schedule);
1054 }
1055
1056 this_ctx = rq->mq_ctx;
1057 this_q = rq->q;
1058 depth = 0;
1059 }
1060
1061 depth++;
1062 list_add_tail(&rq->queuelist, &ctx_list);
1063 }
1064
1065 /*
1066 * If 'this_ctx' is set, we know we have entries to complete
1067 * on 'ctx_list'. Do those.
1068 */
1069 if (this_ctx) {
1070 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1071 from_schedule);
1072 }
1073}
1074
1075static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1076{
1077 init_request_from_bio(rq, bio);
4b570521 1078
3ee32372 1079 if (blk_do_io_stat(rq))
4b570521 1080 blk_account_io_start(rq, 1);
320ae51f
JA
1081}
1082
274a5843
JA
1083static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx)
1084{
1085 return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
1086 !blk_queue_nomerges(hctx->queue);
1087}
1088
07068d5b
JA
1089static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1090 struct blk_mq_ctx *ctx,
1091 struct request *rq, struct bio *bio)
320ae51f 1092{
274a5843 1093 if (!hctx_allow_merges(hctx)) {
07068d5b
JA
1094 blk_mq_bio_to_request(rq, bio);
1095 spin_lock(&ctx->lock);
1096insert_rq:
1097 __blk_mq_insert_request(hctx, rq, false);
1098 spin_unlock(&ctx->lock);
1099 return false;
1100 } else {
274a5843
JA
1101 struct request_queue *q = hctx->queue;
1102
07068d5b
JA
1103 spin_lock(&ctx->lock);
1104 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1105 blk_mq_bio_to_request(rq, bio);
1106 goto insert_rq;
1107 }
320ae51f 1108
07068d5b
JA
1109 spin_unlock(&ctx->lock);
1110 __blk_mq_free_request(hctx, ctx, rq);
1111 return true;
14ec77f3 1112 }
07068d5b 1113}
14ec77f3 1114
07068d5b
JA
1115struct blk_map_ctx {
1116 struct blk_mq_hw_ctx *hctx;
1117 struct blk_mq_ctx *ctx;
1118};
1119
1120static struct request *blk_mq_map_request(struct request_queue *q,
1121 struct bio *bio,
1122 struct blk_map_ctx *data)
1123{
1124 struct blk_mq_hw_ctx *hctx;
1125 struct blk_mq_ctx *ctx;
1126 struct request *rq;
1127 int rw = bio_data_dir(bio);
cb96a42c 1128 struct blk_mq_alloc_data alloc_data;
320ae51f 1129
07068d5b 1130 if (unlikely(blk_mq_queue_enter(q))) {
320ae51f 1131 bio_endio(bio, -EIO);
07068d5b 1132 return NULL;
320ae51f
JA
1133 }
1134
1135 ctx = blk_mq_get_ctx(q);
1136 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1137
07068d5b 1138 if (rw_is_sync(bio->bi_rw))
27fbf4e8 1139 rw |= REQ_SYNC;
07068d5b 1140
320ae51f 1141 trace_block_getrq(q, bio, rw);
cb96a42c
ML
1142 blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx,
1143 hctx);
1144 rq = __blk_mq_alloc_request(&alloc_data, rw);
5dee8577 1145 if (unlikely(!rq)) {
793597a6 1146 __blk_mq_run_hw_queue(hctx);
320ae51f
JA
1147 blk_mq_put_ctx(ctx);
1148 trace_block_sleeprq(q, bio, rw);
793597a6
CH
1149
1150 ctx = blk_mq_get_ctx(q);
320ae51f 1151 hctx = q->mq_ops->map_queue(q, ctx->cpu);
cb96a42c
ML
1152 blk_mq_set_alloc_data(&alloc_data, q,
1153 __GFP_WAIT|GFP_ATOMIC, false, ctx, hctx);
1154 rq = __blk_mq_alloc_request(&alloc_data, rw);
1155 ctx = alloc_data.ctx;
1156 hctx = alloc_data.hctx;
320ae51f
JA
1157 }
1158
1159 hctx->queued++;
07068d5b
JA
1160 data->hctx = hctx;
1161 data->ctx = ctx;
1162 return rq;
1163}
1164
1165/*
1166 * Multiple hardware queue variant. This will not use per-process plugs,
1167 * but will attempt to bypass the hctx queueing if we can go straight to
1168 * hardware for SYNC IO.
1169 */
1170static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1171{
1172 const int is_sync = rw_is_sync(bio->bi_rw);
1173 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1174 struct blk_map_ctx data;
1175 struct request *rq;
1176
1177 blk_queue_bounce(q, &bio);
1178
1179 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1180 bio_endio(bio, -EIO);
1181 return;
1182 }
1183
1184 rq = blk_mq_map_request(q, bio, &data);
1185 if (unlikely(!rq))
1186 return;
1187
1188 if (unlikely(is_flush_fua)) {
1189 blk_mq_bio_to_request(rq, bio);
1190 blk_insert_flush(rq);
1191 goto run_queue;
1192 }
1193
1194 if (is_sync) {
1195 int ret;
1196
1197 blk_mq_bio_to_request(rq, bio);
1198 blk_mq_start_request(rq, true);
1199
1200 /*
1201 * For OK queue, we are done. For error, kill it. Any other
1202 * error (busy), just add it to our list as we previously
1203 * would have done
1204 */
1205 ret = q->mq_ops->queue_rq(data.hctx, rq);
1206 if (ret == BLK_MQ_RQ_QUEUE_OK)
1207 goto done;
1208 else {
1209 __blk_mq_requeue_request(rq);
1210
1211 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1212 rq->errors = -EIO;
1213 blk_mq_end_io(rq, rq->errors);
1214 goto done;
1215 }
1216 }
1217 }
1218
1219 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1220 /*
1221 * For a SYNC request, send it to the hardware immediately. For
1222 * an ASYNC request, just ensure that we run it later on. The
1223 * latter allows for merging opportunities and more efficient
1224 * dispatching.
1225 */
1226run_queue:
1227 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1228 }
1229done:
1230 blk_mq_put_ctx(data.ctx);
1231}
1232
1233/*
1234 * Single hardware queue variant. This will attempt to use any per-process
1235 * plug for merging and IO deferral.
1236 */
1237static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
1238{
1239 const int is_sync = rw_is_sync(bio->bi_rw);
1240 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1241 unsigned int use_plug, request_count = 0;
1242 struct blk_map_ctx data;
1243 struct request *rq;
1244
1245 /*
1246 * If we have multiple hardware queues, just go directly to
1247 * one of those for sync IO.
1248 */
1249 use_plug = !is_flush_fua && !is_sync;
1250
1251 blk_queue_bounce(q, &bio);
1252
1253 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1254 bio_endio(bio, -EIO);
1255 return;
1256 }
1257
1258 if (use_plug && !blk_queue_nomerges(q) &&
1259 blk_attempt_plug_merge(q, bio, &request_count))
1260 return;
1261
1262 rq = blk_mq_map_request(q, bio, &data);
ff87bcec
JA
1263 if (unlikely(!rq))
1264 return;
320ae51f
JA
1265
1266 if (unlikely(is_flush_fua)) {
1267 blk_mq_bio_to_request(rq, bio);
320ae51f
JA
1268 blk_insert_flush(rq);
1269 goto run_queue;
1270 }
1271
1272 /*
1273 * A task plug currently exists. Since this is completely lockless,
1274 * utilize that to temporarily store requests until the task is
1275 * either done or scheduled away.
1276 */
1277 if (use_plug) {
1278 struct blk_plug *plug = current->plug;
1279
1280 if (plug) {
1281 blk_mq_bio_to_request(rq, bio);
92f399c7 1282 if (list_empty(&plug->mq_list))
320ae51f
JA
1283 trace_block_plug(q);
1284 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1285 blk_flush_plug_list(plug, false);
1286 trace_block_plug(q);
1287 }
1288 list_add_tail(&rq->queuelist, &plug->mq_list);
07068d5b 1289 blk_mq_put_ctx(data.ctx);
320ae51f
JA
1290 return;
1291 }
1292 }
1293
07068d5b
JA
1294 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1295 /*
1296 * For a SYNC request, send it to the hardware immediately. For
1297 * an ASYNC request, just ensure that we run it later on. The
1298 * latter allows for merging opportunities and more efficient
1299 * dispatching.
1300 */
1301run_queue:
1302 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
320ae51f
JA
1303 }
1304
07068d5b 1305 blk_mq_put_ctx(data.ctx);
320ae51f
JA
1306}
1307
1308/*
1309 * Default mapping to a software queue, since we use one per CPU.
1310 */
1311struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1312{
1313 return q->queue_hw_ctx[q->mq_map[cpu]];
1314}
1315EXPORT_SYMBOL(blk_mq_map_queue);
1316
24d2f903
CH
1317static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1318 struct blk_mq_tags *tags, unsigned int hctx_idx)
95363efd 1319{
e9b267d9 1320 struct page *page;
320ae51f 1321
24d2f903 1322 if (tags->rqs && set->ops->exit_request) {
e9b267d9 1323 int i;
320ae51f 1324
24d2f903
CH
1325 for (i = 0; i < tags->nr_tags; i++) {
1326 if (!tags->rqs[i])
e9b267d9 1327 continue;
24d2f903
CH
1328 set->ops->exit_request(set->driver_data, tags->rqs[i],
1329 hctx_idx, i);
a5164405 1330 tags->rqs[i] = NULL;
e9b267d9 1331 }
320ae51f 1332 }
320ae51f 1333
24d2f903
CH
1334 while (!list_empty(&tags->page_list)) {
1335 page = list_first_entry(&tags->page_list, struct page, lru);
6753471c 1336 list_del_init(&page->lru);
320ae51f
JA
1337 __free_pages(page, page->private);
1338 }
1339
24d2f903 1340 kfree(tags->rqs);
320ae51f 1341
24d2f903 1342 blk_mq_free_tags(tags);
320ae51f
JA
1343}
1344
1345static size_t order_to_size(unsigned int order)
1346{
4ca08500 1347 return (size_t)PAGE_SIZE << order;
320ae51f
JA
1348}
1349
24d2f903
CH
1350static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1351 unsigned int hctx_idx)
320ae51f 1352{
24d2f903 1353 struct blk_mq_tags *tags;
320ae51f
JA
1354 unsigned int i, j, entries_per_page, max_order = 4;
1355 size_t rq_size, left;
1356
24d2f903
CH
1357 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1358 set->numa_node);
1359 if (!tags)
1360 return NULL;
320ae51f 1361
24d2f903
CH
1362 INIT_LIST_HEAD(&tags->page_list);
1363
a5164405
JA
1364 tags->rqs = kzalloc_node(set->queue_depth * sizeof(struct request *),
1365 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
1366 set->numa_node);
24d2f903
CH
1367 if (!tags->rqs) {
1368 blk_mq_free_tags(tags);
1369 return NULL;
1370 }
320ae51f
JA
1371
1372 /*
1373 * rq_size is the size of the request plus driver payload, rounded
1374 * to the cacheline size
1375 */
24d2f903 1376 rq_size = round_up(sizeof(struct request) + set->cmd_size,
320ae51f 1377 cache_line_size());
24d2f903 1378 left = rq_size * set->queue_depth;
320ae51f 1379
24d2f903 1380 for (i = 0; i < set->queue_depth; ) {
320ae51f
JA
1381 int this_order = max_order;
1382 struct page *page;
1383 int to_do;
1384 void *p;
1385
1386 while (left < order_to_size(this_order - 1) && this_order)
1387 this_order--;
1388
1389 do {
a5164405
JA
1390 page = alloc_pages_node(set->numa_node,
1391 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
1392 this_order);
320ae51f
JA
1393 if (page)
1394 break;
1395 if (!this_order--)
1396 break;
1397 if (order_to_size(this_order) < rq_size)
1398 break;
1399 } while (1);
1400
1401 if (!page)
24d2f903 1402 goto fail;
320ae51f
JA
1403
1404 page->private = this_order;
24d2f903 1405 list_add_tail(&page->lru, &tags->page_list);
320ae51f
JA
1406
1407 p = page_address(page);
1408 entries_per_page = order_to_size(this_order) / rq_size;
24d2f903 1409 to_do = min(entries_per_page, set->queue_depth - i);
320ae51f
JA
1410 left -= to_do * rq_size;
1411 for (j = 0; j < to_do; j++) {
24d2f903 1412 tags->rqs[i] = p;
683d0e12
DH
1413 tags->rqs[i]->atomic_flags = 0;
1414 tags->rqs[i]->cmd_flags = 0;
24d2f903
CH
1415 if (set->ops->init_request) {
1416 if (set->ops->init_request(set->driver_data,
1417 tags->rqs[i], hctx_idx, i,
a5164405
JA
1418 set->numa_node)) {
1419 tags->rqs[i] = NULL;
24d2f903 1420 goto fail;
a5164405 1421 }
e9b267d9
CH
1422 }
1423
320ae51f
JA
1424 p += rq_size;
1425 i++;
1426 }
1427 }
1428
24d2f903 1429 return tags;
320ae51f 1430
24d2f903 1431fail:
24d2f903
CH
1432 blk_mq_free_rq_map(set, tags, hctx_idx);
1433 return NULL;
320ae51f
JA
1434}
1435
1429d7c9
JA
1436static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1437{
1438 kfree(bitmap->map);
1439}
1440
1441static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1442{
1443 unsigned int bpw = 8, total, num_maps, i;
1444
1445 bitmap->bits_per_word = bpw;
1446
1447 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1448 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1449 GFP_KERNEL, node);
1450 if (!bitmap->map)
1451 return -ENOMEM;
1452
1453 bitmap->map_size = num_maps;
1454
1455 total = nr_cpu_ids;
1456 for (i = 0; i < num_maps; i++) {
1457 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1458 total -= bitmap->map[i].depth;
1459 }
1460
1461 return 0;
1462}
1463
484b4061
JA
1464static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1465{
1466 struct request_queue *q = hctx->queue;
1467 struct blk_mq_ctx *ctx;
1468 LIST_HEAD(tmp);
1469
1470 /*
1471 * Move ctx entries to new CPU, if this one is going away.
1472 */
1473 ctx = __blk_mq_get_ctx(q, cpu);
1474
1475 spin_lock(&ctx->lock);
1476 if (!list_empty(&ctx->rq_list)) {
1477 list_splice_init(&ctx->rq_list, &tmp);
1478 blk_mq_hctx_clear_pending(hctx, ctx);
1479 }
1480 spin_unlock(&ctx->lock);
1481
1482 if (list_empty(&tmp))
1483 return NOTIFY_OK;
1484
1485 ctx = blk_mq_get_ctx(q);
1486 spin_lock(&ctx->lock);
1487
1488 while (!list_empty(&tmp)) {
1489 struct request *rq;
1490
1491 rq = list_first_entry(&tmp, struct request, queuelist);
1492 rq->mq_ctx = ctx;
1493 list_move_tail(&rq->queuelist, &ctx->rq_list);
1494 }
1495
1496 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1497 blk_mq_hctx_mark_pending(hctx, ctx);
1498
1499 spin_unlock(&ctx->lock);
1500
1501 blk_mq_run_hw_queue(hctx, true);
1502 blk_mq_put_ctx(ctx);
1503 return NOTIFY_OK;
1504}
1505
1506static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
1507{
1508 struct request_queue *q = hctx->queue;
1509 struct blk_mq_tag_set *set = q->tag_set;
1510
1511 if (set->tags[hctx->queue_num])
1512 return NOTIFY_OK;
1513
1514 set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
1515 if (!set->tags[hctx->queue_num])
1516 return NOTIFY_STOP;
1517
1518 hctx->tags = set->tags[hctx->queue_num];
1519 return NOTIFY_OK;
1520}
1521
1522static int blk_mq_hctx_notify(void *data, unsigned long action,
1523 unsigned int cpu)
1524{
1525 struct blk_mq_hw_ctx *hctx = data;
1526
1527 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1528 return blk_mq_hctx_cpu_offline(hctx, cpu);
1529 else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
1530 return blk_mq_hctx_cpu_online(hctx, cpu);
1531
1532 return NOTIFY_OK;
1533}
1534
624dbe47
ML
1535static void blk_mq_exit_hw_queues(struct request_queue *q,
1536 struct blk_mq_tag_set *set, int nr_queue)
1537{
1538 struct blk_mq_hw_ctx *hctx;
1539 unsigned int i;
1540
1541 queue_for_each_hw_ctx(q, hctx, i) {
1542 if (i == nr_queue)
1543 break;
1544
f899fed4
JA
1545 blk_mq_tag_idle(hctx);
1546
624dbe47
ML
1547 if (set->ops->exit_hctx)
1548 set->ops->exit_hctx(hctx, i);
1549
1550 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1551 kfree(hctx->ctxs);
1552 blk_mq_free_bitmap(&hctx->ctx_map);
1553 }
1554
1555}
1556
1557static void blk_mq_free_hw_queues(struct request_queue *q,
1558 struct blk_mq_tag_set *set)
1559{
1560 struct blk_mq_hw_ctx *hctx;
1561 unsigned int i;
1562
1563 queue_for_each_hw_ctx(q, hctx, i) {
1564 free_cpumask_var(hctx->cpumask);
cdef54dd 1565 kfree(hctx);
624dbe47
ML
1566 }
1567}
1568
320ae51f 1569static int blk_mq_init_hw_queues(struct request_queue *q,
24d2f903 1570 struct blk_mq_tag_set *set)
320ae51f
JA
1571{
1572 struct blk_mq_hw_ctx *hctx;
624dbe47 1573 unsigned int i;
320ae51f
JA
1574
1575 /*
1576 * Initialize hardware queues
1577 */
1578 queue_for_each_hw_ctx(q, hctx, i) {
320ae51f
JA
1579 int node;
1580
1581 node = hctx->numa_node;
1582 if (node == NUMA_NO_NODE)
24d2f903 1583 node = hctx->numa_node = set->numa_node;
320ae51f 1584
70f4db63
CH
1585 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1586 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
320ae51f
JA
1587 spin_lock_init(&hctx->lock);
1588 INIT_LIST_HEAD(&hctx->dispatch);
1589 hctx->queue = q;
1590 hctx->queue_num = i;
24d2f903
CH
1591 hctx->flags = set->flags;
1592 hctx->cmd_size = set->cmd_size;
320ae51f
JA
1593
1594 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1595 blk_mq_hctx_notify, hctx);
1596 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1597
24d2f903 1598 hctx->tags = set->tags[i];
320ae51f
JA
1599
1600 /*
a68aafa5 1601 * Allocate space for all possible cpus to avoid allocation at
320ae51f
JA
1602 * runtime
1603 */
1604 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1605 GFP_KERNEL, node);
1606 if (!hctx->ctxs)
1607 break;
1608
1429d7c9 1609 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
320ae51f
JA
1610 break;
1611
320ae51f
JA
1612 hctx->nr_ctx = 0;
1613
24d2f903
CH
1614 if (set->ops->init_hctx &&
1615 set->ops->init_hctx(hctx, set->driver_data, i))
320ae51f
JA
1616 break;
1617 }
1618
1619 if (i == q->nr_hw_queues)
1620 return 0;
1621
1622 /*
1623 * Init failed
1624 */
624dbe47 1625 blk_mq_exit_hw_queues(q, set, i);
320ae51f
JA
1626
1627 return 1;
1628}
1629
1630static void blk_mq_init_cpu_queues(struct request_queue *q,
1631 unsigned int nr_hw_queues)
1632{
1633 unsigned int i;
1634
1635 for_each_possible_cpu(i) {
1636 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1637 struct blk_mq_hw_ctx *hctx;
1638
1639 memset(__ctx, 0, sizeof(*__ctx));
1640 __ctx->cpu = i;
1641 spin_lock_init(&__ctx->lock);
1642 INIT_LIST_HEAD(&__ctx->rq_list);
1643 __ctx->queue = q;
1644
1645 /* If the cpu isn't online, the cpu is mapped to first hctx */
320ae51f
JA
1646 if (!cpu_online(i))
1647 continue;
1648
e4043dcf
JA
1649 hctx = q->mq_ops->map_queue(q, i);
1650 cpumask_set_cpu(i, hctx->cpumask);
1651 hctx->nr_ctx++;
1652
320ae51f
JA
1653 /*
1654 * Set local node, IFF we have more than one hw queue. If
1655 * not, we remain on the home node of the device
1656 */
1657 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1658 hctx->numa_node = cpu_to_node(i);
1659 }
1660}
1661
1662static void blk_mq_map_swqueue(struct request_queue *q)
1663{
1664 unsigned int i;
1665 struct blk_mq_hw_ctx *hctx;
1666 struct blk_mq_ctx *ctx;
1667
1668 queue_for_each_hw_ctx(q, hctx, i) {
e4043dcf 1669 cpumask_clear(hctx->cpumask);
320ae51f
JA
1670 hctx->nr_ctx = 0;
1671 }
1672
1673 /*
1674 * Map software to hardware queues
1675 */
1676 queue_for_each_ctx(q, ctx, i) {
1677 /* If the cpu isn't online, the cpu is mapped to first hctx */
e4043dcf
JA
1678 if (!cpu_online(i))
1679 continue;
1680
320ae51f 1681 hctx = q->mq_ops->map_queue(q, i);
e4043dcf 1682 cpumask_set_cpu(i, hctx->cpumask);
320ae51f
JA
1683 ctx->index_hw = hctx->nr_ctx;
1684 hctx->ctxs[hctx->nr_ctx++] = ctx;
1685 }
506e931f
JA
1686
1687 queue_for_each_hw_ctx(q, hctx, i) {
484b4061 1688 /*
a68aafa5
JA
1689 * If no software queues are mapped to this hardware queue,
1690 * disable it and free the request entries.
484b4061
JA
1691 */
1692 if (!hctx->nr_ctx) {
1693 struct blk_mq_tag_set *set = q->tag_set;
1694
1695 if (set->tags[i]) {
1696 blk_mq_free_rq_map(set, set->tags[i], i);
1697 set->tags[i] = NULL;
1698 hctx->tags = NULL;
1699 }
1700 continue;
1701 }
1702
1703 /*
1704 * Initialize batch roundrobin counts
1705 */
506e931f
JA
1706 hctx->next_cpu = cpumask_first(hctx->cpumask);
1707 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1708 }
320ae51f
JA
1709}
1710
0d2602ca
JA
1711static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
1712{
1713 struct blk_mq_hw_ctx *hctx;
1714 struct request_queue *q;
1715 bool shared;
1716 int i;
1717
1718 if (set->tag_list.next == set->tag_list.prev)
1719 shared = false;
1720 else
1721 shared = true;
1722
1723 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1724 blk_mq_freeze_queue(q);
1725
1726 queue_for_each_hw_ctx(q, hctx, i) {
1727 if (shared)
1728 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1729 else
1730 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1731 }
1732 blk_mq_unfreeze_queue(q);
1733 }
1734}
1735
1736static void blk_mq_del_queue_tag_set(struct request_queue *q)
1737{
1738 struct blk_mq_tag_set *set = q->tag_set;
1739
0d2602ca
JA
1740 mutex_lock(&set->tag_list_lock);
1741 list_del_init(&q->tag_set_list);
1742 blk_mq_update_tag_set_depth(set);
1743 mutex_unlock(&set->tag_list_lock);
0d2602ca
JA
1744}
1745
1746static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1747 struct request_queue *q)
1748{
1749 q->tag_set = set;
1750
1751 mutex_lock(&set->tag_list_lock);
1752 list_add_tail(&q->tag_set_list, &set->tag_list);
1753 blk_mq_update_tag_set_depth(set);
1754 mutex_unlock(&set->tag_list_lock);
1755}
1756
24d2f903 1757struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
320ae51f
JA
1758{
1759 struct blk_mq_hw_ctx **hctxs;
e6cdb092 1760 struct blk_mq_ctx __percpu *ctx;
320ae51f 1761 struct request_queue *q;
f14bbe77 1762 unsigned int *map;
320ae51f
JA
1763 int i;
1764
320ae51f
JA
1765 ctx = alloc_percpu(struct blk_mq_ctx);
1766 if (!ctx)
1767 return ERR_PTR(-ENOMEM);
1768
24d2f903
CH
1769 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1770 set->numa_node);
320ae51f
JA
1771
1772 if (!hctxs)
1773 goto err_percpu;
1774
f14bbe77
JA
1775 map = blk_mq_make_queue_map(set);
1776 if (!map)
1777 goto err_map;
1778
24d2f903 1779 for (i = 0; i < set->nr_hw_queues; i++) {
f14bbe77
JA
1780 int node = blk_mq_hw_queue_to_node(map, i);
1781
cdef54dd
CH
1782 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
1783 GFP_KERNEL, node);
320ae51f
JA
1784 if (!hctxs[i])
1785 goto err_hctxs;
1786
e4043dcf
JA
1787 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1788 goto err_hctxs;
1789
0d2602ca 1790 atomic_set(&hctxs[i]->nr_active, 0);
f14bbe77 1791 hctxs[i]->numa_node = node;
320ae51f
JA
1792 hctxs[i]->queue_num = i;
1793 }
1794
24d2f903 1795 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
320ae51f
JA
1796 if (!q)
1797 goto err_hctxs;
1798
add703fd 1799 if (percpu_ref_init(&q->mq_usage_counter, blk_mq_usage_counter_release))
3d2936f4
ML
1800 goto err_map;
1801
320ae51f
JA
1802 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1803 blk_queue_rq_timeout(q, 30000);
1804
1805 q->nr_queues = nr_cpu_ids;
24d2f903 1806 q->nr_hw_queues = set->nr_hw_queues;
f14bbe77 1807 q->mq_map = map;
320ae51f
JA
1808
1809 q->queue_ctx = ctx;
1810 q->queue_hw_ctx = hctxs;
1811
24d2f903 1812 q->mq_ops = set->ops;
94eddfbe 1813 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
320ae51f 1814
05f1dd53
JA
1815 if (!(set->flags & BLK_MQ_F_SG_MERGE))
1816 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
1817
1be036e9
CH
1818 q->sg_reserved_size = INT_MAX;
1819
6fca6a61
CH
1820 INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
1821 INIT_LIST_HEAD(&q->requeue_list);
1822 spin_lock_init(&q->requeue_lock);
1823
07068d5b
JA
1824 if (q->nr_hw_queues > 1)
1825 blk_queue_make_request(q, blk_mq_make_request);
1826 else
1827 blk_queue_make_request(q, blk_sq_make_request);
1828
87ee7b11 1829 blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
24d2f903
CH
1830 if (set->timeout)
1831 blk_queue_rq_timeout(q, set->timeout);
320ae51f 1832
eba71768
JA
1833 /*
1834 * Do this after blk_queue_make_request() overrides it...
1835 */
1836 q->nr_requests = set->queue_depth;
1837
24d2f903
CH
1838 if (set->ops->complete)
1839 blk_queue_softirq_done(q, set->ops->complete);
30a91cb4 1840
320ae51f 1841 blk_mq_init_flush(q);
24d2f903 1842 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
320ae51f 1843
24d2f903
CH
1844 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1845 set->cmd_size, cache_line_size()),
1846 GFP_KERNEL);
18741986 1847 if (!q->flush_rq)
320ae51f
JA
1848 goto err_hw;
1849
24d2f903 1850 if (blk_mq_init_hw_queues(q, set))
18741986
CH
1851 goto err_flush_rq;
1852
320ae51f
JA
1853 mutex_lock(&all_q_mutex);
1854 list_add_tail(&q->all_q_node, &all_q_list);
1855 mutex_unlock(&all_q_mutex);
1856
0d2602ca
JA
1857 blk_mq_add_queue_tag_set(set, q);
1858
484b4061
JA
1859 blk_mq_map_swqueue(q);
1860
320ae51f 1861 return q;
18741986
CH
1862
1863err_flush_rq:
1864 kfree(q->flush_rq);
320ae51f 1865err_hw:
320ae51f
JA
1866 blk_cleanup_queue(q);
1867err_hctxs:
f14bbe77 1868 kfree(map);
24d2f903 1869 for (i = 0; i < set->nr_hw_queues; i++) {
320ae51f
JA
1870 if (!hctxs[i])
1871 break;
e4043dcf 1872 free_cpumask_var(hctxs[i]->cpumask);
cdef54dd 1873 kfree(hctxs[i]);
320ae51f 1874 }
f14bbe77 1875err_map:
320ae51f
JA
1876 kfree(hctxs);
1877err_percpu:
1878 free_percpu(ctx);
1879 return ERR_PTR(-ENOMEM);
1880}
1881EXPORT_SYMBOL(blk_mq_init_queue);
1882
1883void blk_mq_free_queue(struct request_queue *q)
1884{
624dbe47 1885 struct blk_mq_tag_set *set = q->tag_set;
320ae51f 1886
0d2602ca
JA
1887 blk_mq_del_queue_tag_set(q);
1888
624dbe47
ML
1889 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
1890 blk_mq_free_hw_queues(q, set);
320ae51f 1891
add703fd 1892 percpu_ref_exit(&q->mq_usage_counter);
3d2936f4 1893
320ae51f
JA
1894 free_percpu(q->queue_ctx);
1895 kfree(q->queue_hw_ctx);
1896 kfree(q->mq_map);
1897
1898 q->queue_ctx = NULL;
1899 q->queue_hw_ctx = NULL;
1900 q->mq_map = NULL;
1901
1902 mutex_lock(&all_q_mutex);
1903 list_del_init(&q->all_q_node);
1904 mutex_unlock(&all_q_mutex);
1905}
320ae51f
JA
1906
1907/* Basically redo blk_mq_init_queue with queue frozen */
f618ef7c 1908static void blk_mq_queue_reinit(struct request_queue *q)
320ae51f
JA
1909{
1910 blk_mq_freeze_queue(q);
1911
67aec14c
JA
1912 blk_mq_sysfs_unregister(q);
1913
320ae51f
JA
1914 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1915
1916 /*
1917 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1918 * we should change hctx numa_node according to new topology (this
1919 * involves free and re-allocate memory, worthy doing?)
1920 */
1921
1922 blk_mq_map_swqueue(q);
1923
67aec14c
JA
1924 blk_mq_sysfs_register(q);
1925
320ae51f
JA
1926 blk_mq_unfreeze_queue(q);
1927}
1928
f618ef7c
PG
1929static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1930 unsigned long action, void *hcpu)
320ae51f
JA
1931{
1932 struct request_queue *q;
1933
1934 /*
9fccfed8
JA
1935 * Before new mappings are established, hotadded cpu might already
1936 * start handling requests. This doesn't break anything as we map
1937 * offline CPUs to first hardware queue. We will re-init the queue
1938 * below to get optimal settings.
320ae51f
JA
1939 */
1940 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1941 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1942 return NOTIFY_OK;
1943
1944 mutex_lock(&all_q_mutex);
1945 list_for_each_entry(q, &all_q_list, all_q_node)
1946 blk_mq_queue_reinit(q);
1947 mutex_unlock(&all_q_mutex);
1948 return NOTIFY_OK;
1949}
1950
a5164405
JA
1951static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
1952{
1953 int i;
1954
1955 for (i = 0; i < set->nr_hw_queues; i++) {
1956 set->tags[i] = blk_mq_init_rq_map(set, i);
1957 if (!set->tags[i])
1958 goto out_unwind;
1959 }
1960
1961 return 0;
1962
1963out_unwind:
1964 while (--i >= 0)
1965 blk_mq_free_rq_map(set, set->tags[i], i);
1966
1967 set->tags = NULL;
1968 return -ENOMEM;
1969}
1970
1971/*
1972 * Allocate the request maps associated with this tag_set. Note that this
1973 * may reduce the depth asked for, if memory is tight. set->queue_depth
1974 * will be updated to reflect the allocated depth.
1975 */
1976static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
1977{
1978 unsigned int depth;
1979 int err;
1980
1981 depth = set->queue_depth;
1982 do {
1983 err = __blk_mq_alloc_rq_maps(set);
1984 if (!err)
1985 break;
1986
1987 set->queue_depth >>= 1;
1988 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
1989 err = -ENOMEM;
1990 break;
1991 }
1992 } while (set->queue_depth);
1993
1994 if (!set->queue_depth || err) {
1995 pr_err("blk-mq: failed to allocate request map\n");
1996 return -ENOMEM;
1997 }
1998
1999 if (depth != set->queue_depth)
2000 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2001 depth, set->queue_depth);
2002
2003 return 0;
2004}
2005
a4391c64
JA
2006/*
2007 * Alloc a tag set to be associated with one or more request queues.
2008 * May fail with EINVAL for various error conditions. May adjust the
2009 * requested depth down, if if it too large. In that case, the set
2010 * value will be stored in set->queue_depth.
2011 */
24d2f903
CH
2012int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
2013{
24d2f903
CH
2014 if (!set->nr_hw_queues)
2015 return -EINVAL;
a4391c64 2016 if (!set->queue_depth)
24d2f903
CH
2017 return -EINVAL;
2018 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
2019 return -EINVAL;
2020
cdef54dd 2021 if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue)
24d2f903
CH
2022 return -EINVAL;
2023
a4391c64
JA
2024 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
2025 pr_info("blk-mq: reduced tag depth to %u\n",
2026 BLK_MQ_MAX_DEPTH);
2027 set->queue_depth = BLK_MQ_MAX_DEPTH;
2028 }
24d2f903 2029
48479005
ML
2030 set->tags = kmalloc_node(set->nr_hw_queues *
2031 sizeof(struct blk_mq_tags *),
24d2f903
CH
2032 GFP_KERNEL, set->numa_node);
2033 if (!set->tags)
a5164405 2034 return -ENOMEM;
24d2f903 2035
a5164405
JA
2036 if (blk_mq_alloc_rq_maps(set))
2037 goto enomem;
24d2f903 2038
0d2602ca
JA
2039 mutex_init(&set->tag_list_lock);
2040 INIT_LIST_HEAD(&set->tag_list);
2041
24d2f903 2042 return 0;
a5164405 2043enomem:
5676e7b6
RE
2044 kfree(set->tags);
2045 set->tags = NULL;
24d2f903
CH
2046 return -ENOMEM;
2047}
2048EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2049
2050void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2051{
2052 int i;
2053
484b4061
JA
2054 for (i = 0; i < set->nr_hw_queues; i++) {
2055 if (set->tags[i])
2056 blk_mq_free_rq_map(set, set->tags[i], i);
2057 }
2058
981bd189 2059 kfree(set->tags);
5676e7b6 2060 set->tags = NULL;
24d2f903
CH
2061}
2062EXPORT_SYMBOL(blk_mq_free_tag_set);
2063
e3a2b3f9
JA
2064int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2065{
2066 struct blk_mq_tag_set *set = q->tag_set;
2067 struct blk_mq_hw_ctx *hctx;
2068 int i, ret;
2069
2070 if (!set || nr > set->queue_depth)
2071 return -EINVAL;
2072
2073 ret = 0;
2074 queue_for_each_hw_ctx(q, hctx, i) {
2075 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2076 if (ret)
2077 break;
2078 }
2079
2080 if (!ret)
2081 q->nr_requests = nr;
2082
2083 return ret;
2084}
2085
676141e4
JA
2086void blk_mq_disable_hotplug(void)
2087{
2088 mutex_lock(&all_q_mutex);
2089}
2090
2091void blk_mq_enable_hotplug(void)
2092{
2093 mutex_unlock(&all_q_mutex);
2094}
2095
320ae51f
JA
2096static int __init blk_mq_init(void)
2097{
320ae51f
JA
2098 blk_mq_cpu_init();
2099
add703fd 2100 hotcpu_notifier(blk_mq_queue_reinit_notify, 0);
320ae51f
JA
2101
2102 return 0;
2103}
2104subsys_initcall(blk_mq_init);