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