]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - block/blk-mq.c
blk-mq: add ->init_request and ->exit_request methods
[mirror_ubuntu-zesty-kernel.git] / block / blk-mq.c
CommitLineData
320ae51f
JA
1#include <linux/kernel.h>
2#include <linux/module.h>
3#include <linux/backing-dev.h>
4#include <linux/bio.h>
5#include <linux/blkdev.h>
6#include <linux/mm.h>
7#include <linux/init.h>
8#include <linux/slab.h>
9#include <linux/workqueue.h>
10#include <linux/smp.h>
11#include <linux/llist.h>
12#include <linux/list_sort.h>
13#include <linux/cpu.h>
14#include <linux/cache.h>
15#include <linux/sched/sysctl.h>
16#include <linux/delay.h>
17
18#include <trace/events/block.h>
19
20#include <linux/blk-mq.h>
21#include "blk.h"
22#include "blk-mq.h"
23#include "blk-mq-tag.h"
24
25static DEFINE_MUTEX(all_q_mutex);
26static LIST_HEAD(all_q_list);
27
28static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
29
320ae51f
JA
30static struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
31 unsigned int cpu)
32{
33 return per_cpu_ptr(q->queue_ctx, cpu);
34}
35
36/*
37 * This assumes per-cpu software queueing queues. They could be per-node
38 * as well, for instance. For now this is hardcoded as-is. Note that we don't
39 * care about preemption, since we know the ctx's are persistent. This does
40 * mean that we can't rely on ctx always matching the currently running CPU.
41 */
42static struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
43{
44 return __blk_mq_get_ctx(q, get_cpu());
45}
46
47static void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
48{
49 put_cpu();
50}
51
52/*
53 * Check if any of the ctx's have pending work in this hardware queue
54 */
55static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
56{
57 unsigned int i;
58
59 for (i = 0; i < hctx->nr_ctx_map; i++)
60 if (hctx->ctx_map[i])
61 return true;
62
63 return false;
64}
65
66/*
67 * Mark this ctx as having pending work in this hardware queue
68 */
69static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
70 struct blk_mq_ctx *ctx)
71{
72 if (!test_bit(ctx->index_hw, hctx->ctx_map))
73 set_bit(ctx->index_hw, hctx->ctx_map);
74}
75
081241e5
CH
76static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,
77 gfp_t gfp, bool reserved)
320ae51f
JA
78{
79 struct request *rq;
80 unsigned int tag;
81
82 tag = blk_mq_get_tag(hctx->tags, gfp, reserved);
83 if (tag != BLK_MQ_TAG_FAIL) {
84 rq = hctx->rqs[tag];
85 rq->tag = tag;
86
87 return rq;
88 }
89
90 return NULL;
91}
92
93static int blk_mq_queue_enter(struct request_queue *q)
94{
95 int ret;
96
97 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
98 smp_wmb();
99 /* we have problems to freeze the queue if it's initializing */
100 if (!blk_queue_bypass(q) || !blk_queue_init_done(q))
101 return 0;
102
103 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
104
105 spin_lock_irq(q->queue_lock);
106 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
43a5e4e2
ML
107 !blk_queue_bypass(q) || blk_queue_dying(q),
108 *q->queue_lock);
320ae51f 109 /* inc usage with lock hold to avoid freeze_queue runs here */
43a5e4e2 110 if (!ret && !blk_queue_dying(q))
320ae51f 111 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
43a5e4e2
ML
112 else if (blk_queue_dying(q))
113 ret = -ENODEV;
320ae51f
JA
114 spin_unlock_irq(q->queue_lock);
115
116 return ret;
117}
118
119static void blk_mq_queue_exit(struct request_queue *q)
120{
121 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
122}
123
43a5e4e2
ML
124static void __blk_mq_drain_queue(struct request_queue *q)
125{
126 while (true) {
127 s64 count;
128
129 spin_lock_irq(q->queue_lock);
130 count = percpu_counter_sum(&q->mq_usage_counter);
131 spin_unlock_irq(q->queue_lock);
132
133 if (count == 0)
134 break;
135 blk_mq_run_queues(q, false);
136 msleep(10);
137 }
138}
139
320ae51f
JA
140/*
141 * Guarantee no request is in use, so we can change any data structure of
142 * the queue afterward.
143 */
144static void blk_mq_freeze_queue(struct request_queue *q)
145{
146 bool drain;
147
148 spin_lock_irq(q->queue_lock);
149 drain = !q->bypass_depth++;
150 queue_flag_set(QUEUE_FLAG_BYPASS, q);
151 spin_unlock_irq(q->queue_lock);
152
43a5e4e2
ML
153 if (drain)
154 __blk_mq_drain_queue(q);
155}
320ae51f 156
43a5e4e2
ML
157void blk_mq_drain_queue(struct request_queue *q)
158{
159 __blk_mq_drain_queue(q);
320ae51f
JA
160}
161
162static void blk_mq_unfreeze_queue(struct request_queue *q)
163{
164 bool wake = false;
165
166 spin_lock_irq(q->queue_lock);
167 if (!--q->bypass_depth) {
168 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
169 wake = true;
170 }
171 WARN_ON_ONCE(q->bypass_depth < 0);
172 spin_unlock_irq(q->queue_lock);
173 if (wake)
174 wake_up_all(&q->mq_freeze_wq);
175}
176
177bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
178{
179 return blk_mq_has_free_tags(hctx->tags);
180}
181EXPORT_SYMBOL(blk_mq_can_queue);
182
94eddfbe
JA
183static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
184 struct request *rq, unsigned int rw_flags)
320ae51f 185{
94eddfbe
JA
186 if (blk_queue_io_stat(q))
187 rw_flags |= REQ_IO_STAT;
188
320ae51f
JA
189 rq->mq_ctx = ctx;
190 rq->cmd_flags = rw_flags;
0fec08b4
ML
191 rq->start_time = jiffies;
192 set_start_time_ns(rq);
320ae51f
JA
193 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
194}
195
320ae51f
JA
196static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
197 int rw, gfp_t gfp,
198 bool reserved)
199{
200 struct request *rq;
201
202 do {
203 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
204 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
205
18741986 206 rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved);
320ae51f 207 if (rq) {
94eddfbe 208 blk_mq_rq_ctx_init(q, ctx, rq, rw);
320ae51f 209 break;
959a35f1 210 }
320ae51f 211
e4043dcf
JA
212 if (gfp & __GFP_WAIT) {
213 __blk_mq_run_hw_queue(hctx);
214 blk_mq_put_ctx(ctx);
215 } else {
216 blk_mq_put_ctx(ctx);
959a35f1 217 break;
e4043dcf 218 }
959a35f1 219
320ae51f
JA
220 blk_mq_wait_for_tags(hctx->tags);
221 } while (1);
222
223 return rq;
224}
225
18741986 226struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
320ae51f
JA
227{
228 struct request *rq;
229
230 if (blk_mq_queue_enter(q))
231 return NULL;
232
18741986 233 rq = blk_mq_alloc_request_pinned(q, rw, gfp, false);
959a35f1
JM
234 if (rq)
235 blk_mq_put_ctx(rq->mq_ctx);
320ae51f
JA
236 return rq;
237}
238
239struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
240 gfp_t gfp)
241{
242 struct request *rq;
243
244 if (blk_mq_queue_enter(q))
245 return NULL;
246
247 rq = blk_mq_alloc_request_pinned(q, rw, gfp, true);
959a35f1
JM
248 if (rq)
249 blk_mq_put_ctx(rq->mq_ctx);
320ae51f
JA
250 return rq;
251}
252EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
253
320ae51f
JA
254static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
255 struct blk_mq_ctx *ctx, struct request *rq)
256{
257 const int tag = rq->tag;
258 struct request_queue *q = rq->q;
259
9d74e257 260 blk_rq_init(hctx->queue, rq);
320ae51f
JA
261 blk_mq_put_tag(hctx->tags, tag);
262
263 blk_mq_queue_exit(q);
264}
265
266void blk_mq_free_request(struct request *rq)
267{
268 struct blk_mq_ctx *ctx = rq->mq_ctx;
269 struct blk_mq_hw_ctx *hctx;
270 struct request_queue *q = rq->q;
271
272 ctx->rq_completed[rq_is_sync(rq)]++;
273
274 hctx = q->mq_ops->map_queue(q, ctx->cpu);
275 __blk_mq_free_request(hctx, ctx, rq);
276}
277
8727af4b
CH
278/*
279 * Clone all relevant state from a request that has been put on hold in
280 * the flush state machine into the preallocated flush request that hangs
281 * off the request queue.
282 *
283 * For a driver the flush request should be invisible, that's why we are
284 * impersonating the original request here.
285 */
286void blk_mq_clone_flush_request(struct request *flush_rq,
287 struct request *orig_rq)
288{
289 struct blk_mq_hw_ctx *hctx =
290 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
291
292 flush_rq->mq_ctx = orig_rq->mq_ctx;
293 flush_rq->tag = orig_rq->tag;
294 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
295 hctx->cmd_size);
296}
297
7237c740 298bool blk_mq_end_io_partial(struct request *rq, int error, unsigned int nr_bytes)
320ae51f 299{
7237c740
CH
300 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
301 return true;
320ae51f 302
0d11e6ac
ML
303 blk_account_io_done(rq);
304
320ae51f
JA
305 if (rq->end_io)
306 rq->end_io(rq, error);
307 else
308 blk_mq_free_request(rq);
7237c740 309 return false;
320ae51f 310}
7237c740 311EXPORT_SYMBOL(blk_mq_end_io_partial);
320ae51f 312
30a91cb4 313static void __blk_mq_complete_request_remote(void *data)
320ae51f 314{
3d6efbf6 315 struct request *rq = data;
320ae51f 316
30a91cb4 317 rq->q->softirq_done_fn(rq);
320ae51f 318}
320ae51f 319
30a91cb4 320void __blk_mq_complete_request(struct request *rq)
320ae51f
JA
321{
322 struct blk_mq_ctx *ctx = rq->mq_ctx;
323 int cpu;
324
30a91cb4
CH
325 if (!ctx->ipi_redirect) {
326 rq->q->softirq_done_fn(rq);
327 return;
328 }
320ae51f
JA
329
330 cpu = get_cpu();
3d6efbf6 331 if (cpu != ctx->cpu && cpu_online(ctx->cpu)) {
30a91cb4 332 rq->csd.func = __blk_mq_complete_request_remote;
3d6efbf6
CH
333 rq->csd.info = rq;
334 rq->csd.flags = 0;
c46fff2a 335 smp_call_function_single_async(ctx->cpu, &rq->csd);
3d6efbf6 336 } else {
30a91cb4 337 rq->q->softirq_done_fn(rq);
3d6efbf6 338 }
320ae51f
JA
339 put_cpu();
340}
30a91cb4
CH
341
342/**
343 * blk_mq_complete_request - end I/O on a request
344 * @rq: the request being processed
345 *
346 * Description:
347 * Ends all I/O on a request. It does not handle partial completions.
348 * The actual completion happens out-of-order, through a IPI handler.
349 **/
350void blk_mq_complete_request(struct request *rq)
351{
352 if (unlikely(blk_should_fake_timeout(rq->q)))
353 return;
354 if (!blk_mark_rq_complete(rq))
355 __blk_mq_complete_request(rq);
356}
357EXPORT_SYMBOL(blk_mq_complete_request);
320ae51f 358
49f5baa5 359static void blk_mq_start_request(struct request *rq, bool last)
320ae51f
JA
360{
361 struct request_queue *q = rq->q;
362
363 trace_block_rq_issue(q, rq);
364
742ee69b
CH
365 rq->resid_len = blk_rq_bytes(rq);
366
320ae51f
JA
367 /*
368 * Just mark start time and set the started bit. Due to memory
369 * ordering, we know we'll see the correct deadline as long as
370 * REQ_ATOMIC_STARTED is seen.
371 */
372 rq->deadline = jiffies + q->rq_timeout;
373 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
49f5baa5
CH
374
375 if (q->dma_drain_size && blk_rq_bytes(rq)) {
376 /*
377 * Make sure space for the drain appears. We know we can do
378 * this because max_hw_segments has been adjusted to be one
379 * fewer than the device can handle.
380 */
381 rq->nr_phys_segments++;
382 }
383
384 /*
385 * Flag the last request in the series so that drivers know when IO
386 * should be kicked off, if they don't do it on a per-request basis.
387 *
388 * Note: the flag isn't the only condition drivers should do kick off.
389 * If drive is busy, the last request might not have the bit set.
390 */
391 if (last)
392 rq->cmd_flags |= REQ_END;
320ae51f
JA
393}
394
395static void blk_mq_requeue_request(struct request *rq)
396{
397 struct request_queue *q = rq->q;
398
399 trace_block_rq_requeue(q, rq);
400 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
49f5baa5
CH
401
402 rq->cmd_flags &= ~REQ_END;
403
404 if (q->dma_drain_size && blk_rq_bytes(rq))
405 rq->nr_phys_segments--;
320ae51f
JA
406}
407
408struct blk_mq_timeout_data {
409 struct blk_mq_hw_ctx *hctx;
410 unsigned long *next;
411 unsigned int *next_set;
412};
413
414static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
415{
416 struct blk_mq_timeout_data *data = __data;
417 struct blk_mq_hw_ctx *hctx = data->hctx;
418 unsigned int tag;
419
420 /* It may not be in flight yet (this is where
421 * the REQ_ATOMIC_STARTED flag comes in). The requests are
422 * statically allocated, so we know it's always safe to access the
423 * memory associated with a bit offset into ->rqs[].
424 */
425 tag = 0;
426 do {
427 struct request *rq;
428
429 tag = find_next_zero_bit(free_tags, hctx->queue_depth, tag);
430 if (tag >= hctx->queue_depth)
431 break;
432
433 rq = hctx->rqs[tag++];
434
435 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
436 continue;
437
438 blk_rq_check_expired(rq, data->next, data->next_set);
439 } while (1);
440}
441
442static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
443 unsigned long *next,
444 unsigned int *next_set)
445{
446 struct blk_mq_timeout_data data = {
447 .hctx = hctx,
448 .next = next,
449 .next_set = next_set,
450 };
451
452 /*
453 * Ask the tagging code to iterate busy requests, so we can
454 * check them for timeout.
455 */
456 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
457}
458
459static void blk_mq_rq_timer(unsigned long data)
460{
461 struct request_queue *q = (struct request_queue *) data;
462 struct blk_mq_hw_ctx *hctx;
463 unsigned long next = 0;
464 int i, next_set = 0;
465
466 queue_for_each_hw_ctx(q, hctx, i)
467 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
468
469 if (next_set)
470 mod_timer(&q->timeout, round_jiffies_up(next));
471}
472
473/*
474 * Reverse check our software queue for entries that we could potentially
475 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
476 * too much time checking for merges.
477 */
478static bool blk_mq_attempt_merge(struct request_queue *q,
479 struct blk_mq_ctx *ctx, struct bio *bio)
480{
481 struct request *rq;
482 int checked = 8;
483
484 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
485 int el_ret;
486
487 if (!checked--)
488 break;
489
490 if (!blk_rq_merge_ok(rq, bio))
491 continue;
492
493 el_ret = blk_try_merge(rq, bio);
494 if (el_ret == ELEVATOR_BACK_MERGE) {
495 if (bio_attempt_back_merge(q, rq, bio)) {
496 ctx->rq_merged++;
497 return true;
498 }
499 break;
500 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
501 if (bio_attempt_front_merge(q, rq, bio)) {
502 ctx->rq_merged++;
503 return true;
504 }
505 break;
506 }
507 }
508
509 return false;
510}
511
512void blk_mq_add_timer(struct request *rq)
513{
514 __blk_add_timer(rq, NULL);
515}
516
517/*
518 * Run this hardware queue, pulling any software queues mapped to it in.
519 * Note that this function currently has various problems around ordering
520 * of IO. In particular, we'd like FIFO behaviour on handling existing
521 * items on the hctx->dispatch list. Ignore that for now.
522 */
523static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
524{
525 struct request_queue *q = hctx->queue;
526 struct blk_mq_ctx *ctx;
527 struct request *rq;
528 LIST_HEAD(rq_list);
529 int bit, queued;
530
e4043dcf
JA
531 WARN_ON(!preempt_count());
532
5d12f905 533 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
320ae51f
JA
534 return;
535
536 hctx->run++;
537
538 /*
539 * Touch any software queue that has pending entries.
540 */
541 for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
542 clear_bit(bit, hctx->ctx_map);
543 ctx = hctx->ctxs[bit];
544 BUG_ON(bit != ctx->index_hw);
545
546 spin_lock(&ctx->lock);
547 list_splice_tail_init(&ctx->rq_list, &rq_list);
548 spin_unlock(&ctx->lock);
549 }
550
551 /*
552 * If we have previous entries on our dispatch list, grab them
553 * and stuff them at the front for more fair dispatch.
554 */
555 if (!list_empty_careful(&hctx->dispatch)) {
556 spin_lock(&hctx->lock);
557 if (!list_empty(&hctx->dispatch))
558 list_splice_init(&hctx->dispatch, &rq_list);
559 spin_unlock(&hctx->lock);
560 }
561
562 /*
563 * Delete and return all entries from our dispatch list
564 */
565 queued = 0;
566
567 /*
568 * Now process all the entries, sending them to the driver.
569 */
570 while (!list_empty(&rq_list)) {
571 int ret;
572
573 rq = list_first_entry(&rq_list, struct request, queuelist);
574 list_del_init(&rq->queuelist);
320ae51f 575
49f5baa5 576 blk_mq_start_request(rq, list_empty(&rq_list));
320ae51f
JA
577
578 ret = q->mq_ops->queue_rq(hctx, rq);
579 switch (ret) {
580 case BLK_MQ_RQ_QUEUE_OK:
581 queued++;
582 continue;
583 case BLK_MQ_RQ_QUEUE_BUSY:
584 /*
585 * FIXME: we should have a mechanism to stop the queue
586 * like blk_stop_queue, otherwise we will waste cpu
587 * time
588 */
589 list_add(&rq->queuelist, &rq_list);
590 blk_mq_requeue_request(rq);
591 break;
592 default:
593 pr_err("blk-mq: bad return on queue: %d\n", ret);
320ae51f 594 case BLK_MQ_RQ_QUEUE_ERROR:
1e93b8c2 595 rq->errors = -EIO;
320ae51f
JA
596 blk_mq_end_io(rq, rq->errors);
597 break;
598 }
599
600 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
601 break;
602 }
603
604 if (!queued)
605 hctx->dispatched[0]++;
606 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
607 hctx->dispatched[ilog2(queued) + 1]++;
608
609 /*
610 * Any items that need requeuing? Stuff them into hctx->dispatch,
611 * that is where we will continue on next queue run.
612 */
613 if (!list_empty(&rq_list)) {
614 spin_lock(&hctx->lock);
615 list_splice(&rq_list, &hctx->dispatch);
616 spin_unlock(&hctx->lock);
617 }
618}
619
620void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
621{
5d12f905 622 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
320ae51f
JA
623 return;
624
e4043dcf 625 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
320ae51f 626 __blk_mq_run_hw_queue(hctx);
e4043dcf 627 else if (hctx->queue->nr_hw_queues == 1)
59c3d45e 628 kblockd_schedule_delayed_work(&hctx->delayed_work, 0);
e4043dcf
JA
629 else {
630 unsigned int cpu;
631
632 /*
633 * It'd be great if the workqueue API had a way to pass
634 * in a mask and had some smarts for more clever placement
635 * than the first CPU. Or we could round-robin here. For now,
636 * just queue on the first CPU.
637 */
638 cpu = cpumask_first(hctx->cpumask);
639 kblockd_schedule_delayed_work_on(cpu, &hctx->delayed_work, 0);
640 }
320ae51f
JA
641}
642
643void blk_mq_run_queues(struct request_queue *q, bool async)
644{
645 struct blk_mq_hw_ctx *hctx;
646 int i;
647
648 queue_for_each_hw_ctx(q, hctx, i) {
649 if ((!blk_mq_hctx_has_pending(hctx) &&
650 list_empty_careful(&hctx->dispatch)) ||
5d12f905 651 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
320ae51f
JA
652 continue;
653
e4043dcf 654 preempt_disable();
320ae51f 655 blk_mq_run_hw_queue(hctx, async);
e4043dcf 656 preempt_enable();
320ae51f
JA
657 }
658}
659EXPORT_SYMBOL(blk_mq_run_queues);
660
661void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
662{
663 cancel_delayed_work(&hctx->delayed_work);
664 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
665}
666EXPORT_SYMBOL(blk_mq_stop_hw_queue);
667
280d45f6
CH
668void blk_mq_stop_hw_queues(struct request_queue *q)
669{
670 struct blk_mq_hw_ctx *hctx;
671 int i;
672
673 queue_for_each_hw_ctx(q, hctx, i)
674 blk_mq_stop_hw_queue(hctx);
675}
676EXPORT_SYMBOL(blk_mq_stop_hw_queues);
677
320ae51f
JA
678void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
679{
680 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
e4043dcf
JA
681
682 preempt_disable();
320ae51f 683 __blk_mq_run_hw_queue(hctx);
e4043dcf 684 preempt_enable();
320ae51f
JA
685}
686EXPORT_SYMBOL(blk_mq_start_hw_queue);
687
688void blk_mq_start_stopped_hw_queues(struct request_queue *q)
689{
690 struct blk_mq_hw_ctx *hctx;
691 int i;
692
693 queue_for_each_hw_ctx(q, hctx, i) {
694 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
695 continue;
696
697 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
e4043dcf 698 preempt_disable();
320ae51f 699 blk_mq_run_hw_queue(hctx, true);
e4043dcf 700 preempt_enable();
320ae51f
JA
701 }
702}
703EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
704
705static void blk_mq_work_fn(struct work_struct *work)
706{
707 struct blk_mq_hw_ctx *hctx;
708
709 hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work);
e4043dcf
JA
710
711 preempt_disable();
320ae51f 712 __blk_mq_run_hw_queue(hctx);
e4043dcf 713 preempt_enable();
320ae51f
JA
714}
715
716static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
72a0a36e 717 struct request *rq, bool at_head)
320ae51f
JA
718{
719 struct blk_mq_ctx *ctx = rq->mq_ctx;
720
01b983c9
JA
721 trace_block_rq_insert(hctx->queue, rq);
722
72a0a36e
CH
723 if (at_head)
724 list_add(&rq->queuelist, &ctx->rq_list);
725 else
726 list_add_tail(&rq->queuelist, &ctx->rq_list);
320ae51f
JA
727 blk_mq_hctx_mark_pending(hctx, ctx);
728
729 /*
730 * We do this early, to ensure we are on the right CPU.
731 */
732 blk_mq_add_timer(rq);
733}
734
eeabc850
CH
735void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
736 bool async)
320ae51f 737{
eeabc850 738 struct request_queue *q = rq->q;
320ae51f 739 struct blk_mq_hw_ctx *hctx;
eeabc850
CH
740 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
741
742 current_ctx = blk_mq_get_ctx(q);
743 if (!cpu_online(ctx->cpu))
744 rq->mq_ctx = ctx = current_ctx;
320ae51f 745
320ae51f
JA
746 hctx = q->mq_ops->map_queue(q, ctx->cpu);
747
eeabc850
CH
748 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
749 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
320ae51f
JA
750 blk_insert_flush(rq);
751 } else {
320ae51f 752 spin_lock(&ctx->lock);
72a0a36e 753 __blk_mq_insert_request(hctx, rq, at_head);
320ae51f 754 spin_unlock(&ctx->lock);
320ae51f
JA
755 }
756
320ae51f
JA
757 if (run_queue)
758 blk_mq_run_hw_queue(hctx, async);
e4043dcf
JA
759
760 blk_mq_put_ctx(current_ctx);
320ae51f
JA
761}
762
763static void blk_mq_insert_requests(struct request_queue *q,
764 struct blk_mq_ctx *ctx,
765 struct list_head *list,
766 int depth,
767 bool from_schedule)
768
769{
770 struct blk_mq_hw_ctx *hctx;
771 struct blk_mq_ctx *current_ctx;
772
773 trace_block_unplug(q, depth, !from_schedule);
774
775 current_ctx = blk_mq_get_ctx(q);
776
777 if (!cpu_online(ctx->cpu))
778 ctx = current_ctx;
779 hctx = q->mq_ops->map_queue(q, ctx->cpu);
780
781 /*
782 * preemption doesn't flush plug list, so it's possible ctx->cpu is
783 * offline now
784 */
785 spin_lock(&ctx->lock);
786 while (!list_empty(list)) {
787 struct request *rq;
788
789 rq = list_first_entry(list, struct request, queuelist);
790 list_del_init(&rq->queuelist);
791 rq->mq_ctx = ctx;
72a0a36e 792 __blk_mq_insert_request(hctx, rq, false);
320ae51f
JA
793 }
794 spin_unlock(&ctx->lock);
795
320ae51f 796 blk_mq_run_hw_queue(hctx, from_schedule);
e4043dcf 797 blk_mq_put_ctx(current_ctx);
320ae51f
JA
798}
799
800static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
801{
802 struct request *rqa = container_of(a, struct request, queuelist);
803 struct request *rqb = container_of(b, struct request, queuelist);
804
805 return !(rqa->mq_ctx < rqb->mq_ctx ||
806 (rqa->mq_ctx == rqb->mq_ctx &&
807 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
808}
809
810void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
811{
812 struct blk_mq_ctx *this_ctx;
813 struct request_queue *this_q;
814 struct request *rq;
815 LIST_HEAD(list);
816 LIST_HEAD(ctx_list);
817 unsigned int depth;
818
819 list_splice_init(&plug->mq_list, &list);
820
821 list_sort(NULL, &list, plug_ctx_cmp);
822
823 this_q = NULL;
824 this_ctx = NULL;
825 depth = 0;
826
827 while (!list_empty(&list)) {
828 rq = list_entry_rq(list.next);
829 list_del_init(&rq->queuelist);
830 BUG_ON(!rq->q);
831 if (rq->mq_ctx != this_ctx) {
832 if (this_ctx) {
833 blk_mq_insert_requests(this_q, this_ctx,
834 &ctx_list, depth,
835 from_schedule);
836 }
837
838 this_ctx = rq->mq_ctx;
839 this_q = rq->q;
840 depth = 0;
841 }
842
843 depth++;
844 list_add_tail(&rq->queuelist, &ctx_list);
845 }
846
847 /*
848 * If 'this_ctx' is set, we know we have entries to complete
849 * on 'ctx_list'. Do those.
850 */
851 if (this_ctx) {
852 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
853 from_schedule);
854 }
855}
856
857static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
858{
859 init_request_from_bio(rq, bio);
860 blk_account_io_start(rq, 1);
861}
862
863static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
864{
865 struct blk_mq_hw_ctx *hctx;
866 struct blk_mq_ctx *ctx;
867 const int is_sync = rw_is_sync(bio->bi_rw);
868 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
869 int rw = bio_data_dir(bio);
870 struct request *rq;
871 unsigned int use_plug, request_count = 0;
872
873 /*
874 * If we have multiple hardware queues, just go directly to
875 * one of those for sync IO.
876 */
877 use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
878
879 blk_queue_bounce(q, &bio);
880
14ec77f3
NB
881 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
882 bio_endio(bio, -EIO);
883 return;
884 }
885
320ae51f
JA
886 if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
887 return;
888
889 if (blk_mq_queue_enter(q)) {
890 bio_endio(bio, -EIO);
891 return;
892 }
893
894 ctx = blk_mq_get_ctx(q);
895 hctx = q->mq_ops->map_queue(q, ctx->cpu);
896
27fbf4e8
SL
897 if (is_sync)
898 rw |= REQ_SYNC;
320ae51f 899 trace_block_getrq(q, bio, rw);
18741986 900 rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
320ae51f 901 if (likely(rq))
18741986 902 blk_mq_rq_ctx_init(q, ctx, rq, rw);
320ae51f
JA
903 else {
904 blk_mq_put_ctx(ctx);
905 trace_block_sleeprq(q, bio, rw);
18741986
CH
906 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
907 false);
320ae51f
JA
908 ctx = rq->mq_ctx;
909 hctx = q->mq_ops->map_queue(q, ctx->cpu);
910 }
911
912 hctx->queued++;
913
914 if (unlikely(is_flush_fua)) {
915 blk_mq_bio_to_request(rq, bio);
320ae51f
JA
916 blk_insert_flush(rq);
917 goto run_queue;
918 }
919
920 /*
921 * A task plug currently exists. Since this is completely lockless,
922 * utilize that to temporarily store requests until the task is
923 * either done or scheduled away.
924 */
925 if (use_plug) {
926 struct blk_plug *plug = current->plug;
927
928 if (plug) {
929 blk_mq_bio_to_request(rq, bio);
92f399c7 930 if (list_empty(&plug->mq_list))
320ae51f
JA
931 trace_block_plug(q);
932 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
933 blk_flush_plug_list(plug, false);
934 trace_block_plug(q);
935 }
936 list_add_tail(&rq->queuelist, &plug->mq_list);
937 blk_mq_put_ctx(ctx);
938 return;
939 }
940 }
941
942 spin_lock(&ctx->lock);
943
944 if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
945 blk_mq_attempt_merge(q, ctx, bio))
946 __blk_mq_free_request(hctx, ctx, rq);
947 else {
948 blk_mq_bio_to_request(rq, bio);
72a0a36e 949 __blk_mq_insert_request(hctx, rq, false);
320ae51f
JA
950 }
951
952 spin_unlock(&ctx->lock);
320ae51f
JA
953
954 /*
955 * For a SYNC request, send it to the hardware immediately. For an
956 * ASYNC request, just ensure that we run it later on. The latter
957 * allows for merging opportunities and more efficient dispatching.
958 */
959run_queue:
960 blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
e4043dcf 961 blk_mq_put_ctx(ctx);
320ae51f
JA
962}
963
964/*
965 * Default mapping to a software queue, since we use one per CPU.
966 */
967struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
968{
969 return q->queue_hw_ctx[q->mq_map[cpu]];
970}
971EXPORT_SYMBOL(blk_mq_map_queue);
972
973struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_reg *reg,
974 unsigned int hctx_index)
975{
976 return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
977 GFP_KERNEL | __GFP_ZERO, reg->numa_node);
978}
979EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
980
981void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
982 unsigned int hctx_index)
983{
984 kfree(hctx);
985}
986EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
987
988static void blk_mq_hctx_notify(void *data, unsigned long action,
989 unsigned int cpu)
990{
991 struct blk_mq_hw_ctx *hctx = data;
bccb5f7c 992 struct request_queue *q = hctx->queue;
320ae51f
JA
993 struct blk_mq_ctx *ctx;
994 LIST_HEAD(tmp);
995
996 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
997 return;
998
999 /*
1000 * Move ctx entries to new CPU, if this one is going away.
1001 */
bccb5f7c 1002 ctx = __blk_mq_get_ctx(q, cpu);
320ae51f
JA
1003
1004 spin_lock(&ctx->lock);
1005 if (!list_empty(&ctx->rq_list)) {
1006 list_splice_init(&ctx->rq_list, &tmp);
1007 clear_bit(ctx->index_hw, hctx->ctx_map);
1008 }
1009 spin_unlock(&ctx->lock);
1010
1011 if (list_empty(&tmp))
1012 return;
1013
bccb5f7c 1014 ctx = blk_mq_get_ctx(q);
320ae51f
JA
1015 spin_lock(&ctx->lock);
1016
1017 while (!list_empty(&tmp)) {
1018 struct request *rq;
1019
1020 rq = list_first_entry(&tmp, struct request, queuelist);
1021 rq->mq_ctx = ctx;
1022 list_move_tail(&rq->queuelist, &ctx->rq_list);
1023 }
1024
bccb5f7c 1025 hctx = q->mq_ops->map_queue(q, ctx->cpu);
320ae51f
JA
1026 blk_mq_hctx_mark_pending(hctx, ctx);
1027
1028 spin_unlock(&ctx->lock);
bccb5f7c
JA
1029
1030 blk_mq_run_hw_queue(hctx, true);
e4043dcf 1031 blk_mq_put_ctx(ctx);
320ae51f
JA
1032}
1033
e9b267d9 1034static void blk_mq_free_rq_map(struct blk_mq_hw_ctx *hctx, void *driver_data)
95363efd 1035{
e9b267d9 1036 struct page *page;
320ae51f 1037
e9b267d9
CH
1038 if (hctx->rqs && hctx->queue->mq_ops->exit_request) {
1039 int i;
320ae51f 1040
e9b267d9
CH
1041 for (i = 0; i < hctx->queue_depth; i++) {
1042 if (!hctx->rqs[i])
1043 continue;
1044 hctx->queue->mq_ops->exit_request(driver_data, hctx,
1045 hctx->rqs[i], i);
1046 }
320ae51f 1047 }
320ae51f
JA
1048
1049 while (!list_empty(&hctx->page_list)) {
6753471c
DH
1050 page = list_first_entry(&hctx->page_list, struct page, lru);
1051 list_del_init(&page->lru);
320ae51f
JA
1052 __free_pages(page, page->private);
1053 }
1054
1055 kfree(hctx->rqs);
1056
1057 if (hctx->tags)
1058 blk_mq_free_tags(hctx->tags);
1059}
1060
1061static size_t order_to_size(unsigned int order)
1062{
1063 size_t ret = PAGE_SIZE;
1064
1065 while (order--)
1066 ret *= 2;
1067
1068 return ret;
1069}
1070
1071static int blk_mq_init_rq_map(struct blk_mq_hw_ctx *hctx,
e9b267d9 1072 struct blk_mq_reg *reg, void *driver_data, int node)
320ae51f 1073{
e9b267d9 1074 unsigned int reserved_tags = reg->reserved_tags;
320ae51f
JA
1075 unsigned int i, j, entries_per_page, max_order = 4;
1076 size_t rq_size, left;
e9b267d9 1077 int error;
320ae51f
JA
1078
1079 INIT_LIST_HEAD(&hctx->page_list);
1080
1081 hctx->rqs = kmalloc_node(hctx->queue_depth * sizeof(struct request *),
1082 GFP_KERNEL, node);
1083 if (!hctx->rqs)
1084 return -ENOMEM;
1085
1086 /*
1087 * rq_size is the size of the request plus driver payload, rounded
1088 * to the cacheline size
1089 */
1090 rq_size = round_up(sizeof(struct request) + hctx->cmd_size,
1091 cache_line_size());
1092 left = rq_size * hctx->queue_depth;
1093
1094 for (i = 0; i < hctx->queue_depth;) {
1095 int this_order = max_order;
1096 struct page *page;
1097 int to_do;
1098 void *p;
1099
1100 while (left < order_to_size(this_order - 1) && this_order)
1101 this_order--;
1102
1103 do {
1104 page = alloc_pages_node(node, GFP_KERNEL, this_order);
1105 if (page)
1106 break;
1107 if (!this_order--)
1108 break;
1109 if (order_to_size(this_order) < rq_size)
1110 break;
1111 } while (1);
1112
1113 if (!page)
1114 break;
1115
1116 page->private = this_order;
6753471c 1117 list_add_tail(&page->lru, &hctx->page_list);
320ae51f
JA
1118
1119 p = page_address(page);
1120 entries_per_page = order_to_size(this_order) / rq_size;
1121 to_do = min(entries_per_page, hctx->queue_depth - i);
1122 left -= to_do * rq_size;
1123 for (j = 0; j < to_do; j++) {
1124 hctx->rqs[i] = p;
9d74e257 1125 blk_rq_init(hctx->queue, hctx->rqs[i]);
e9b267d9
CH
1126 if (reg->ops->init_request) {
1127 error = reg->ops->init_request(driver_data,
1128 hctx, hctx->rqs[i], i);
1129 if (error)
1130 goto err_rq_map;
1131 }
1132
320ae51f
JA
1133 p += rq_size;
1134 i++;
1135 }
1136 }
1137
e9b267d9
CH
1138 if (i < (reserved_tags + BLK_MQ_TAG_MIN)) {
1139 error = -ENOMEM;
320ae51f 1140 goto err_rq_map;
e9b267d9
CH
1141 }
1142 if (i != hctx->queue_depth) {
320ae51f
JA
1143 hctx->queue_depth = i;
1144 pr_warn("%s: queue depth set to %u because of low memory\n",
1145 __func__, i);
1146 }
1147
1148 hctx->tags = blk_mq_init_tags(hctx->queue_depth, reserved_tags, node);
1149 if (!hctx->tags) {
e9b267d9
CH
1150 error = -ENOMEM;
1151 goto err_rq_map;
320ae51f
JA
1152 }
1153
1154 return 0;
e9b267d9
CH
1155err_rq_map:
1156 blk_mq_free_rq_map(hctx, driver_data);
1157 return error;
320ae51f
JA
1158}
1159
1160static int blk_mq_init_hw_queues(struct request_queue *q,
1161 struct blk_mq_reg *reg, void *driver_data)
1162{
1163 struct blk_mq_hw_ctx *hctx;
1164 unsigned int i, j;
1165
1166 /*
1167 * Initialize hardware queues
1168 */
1169 queue_for_each_hw_ctx(q, hctx, i) {
1170 unsigned int num_maps;
1171 int node;
1172
1173 node = hctx->numa_node;
1174 if (node == NUMA_NO_NODE)
1175 node = hctx->numa_node = reg->numa_node;
1176
1177 INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn);
1178 spin_lock_init(&hctx->lock);
1179 INIT_LIST_HEAD(&hctx->dispatch);
1180 hctx->queue = q;
1181 hctx->queue_num = i;
1182 hctx->flags = reg->flags;
1183 hctx->queue_depth = reg->queue_depth;
1184 hctx->cmd_size = reg->cmd_size;
1185
1186 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1187 blk_mq_hctx_notify, hctx);
1188 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1189
e9b267d9 1190 if (blk_mq_init_rq_map(hctx, reg, driver_data, node))
320ae51f
JA
1191 break;
1192
1193 /*
1194 * Allocate space for all possible cpus to avoid allocation in
1195 * runtime
1196 */
1197 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1198 GFP_KERNEL, node);
1199 if (!hctx->ctxs)
1200 break;
1201
1202 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1203 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1204 GFP_KERNEL, node);
1205 if (!hctx->ctx_map)
1206 break;
1207
1208 hctx->nr_ctx_map = num_maps;
1209 hctx->nr_ctx = 0;
1210
1211 if (reg->ops->init_hctx &&
1212 reg->ops->init_hctx(hctx, driver_data, i))
1213 break;
1214 }
1215
1216 if (i == q->nr_hw_queues)
1217 return 0;
1218
1219 /*
1220 * Init failed
1221 */
1222 queue_for_each_hw_ctx(q, hctx, j) {
1223 if (i == j)
1224 break;
1225
1226 if (reg->ops->exit_hctx)
1227 reg->ops->exit_hctx(hctx, j);
1228
1229 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
e9b267d9 1230 blk_mq_free_rq_map(hctx, driver_data);
320ae51f
JA
1231 kfree(hctx->ctxs);
1232 }
1233
1234 return 1;
1235}
1236
1237static void blk_mq_init_cpu_queues(struct request_queue *q,
1238 unsigned int nr_hw_queues)
1239{
1240 unsigned int i;
1241
1242 for_each_possible_cpu(i) {
1243 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1244 struct blk_mq_hw_ctx *hctx;
1245
1246 memset(__ctx, 0, sizeof(*__ctx));
1247 __ctx->cpu = i;
1248 spin_lock_init(&__ctx->lock);
1249 INIT_LIST_HEAD(&__ctx->rq_list);
1250 __ctx->queue = q;
1251
1252 /* If the cpu isn't online, the cpu is mapped to first hctx */
320ae51f
JA
1253 if (!cpu_online(i))
1254 continue;
1255
e4043dcf
JA
1256 hctx = q->mq_ops->map_queue(q, i);
1257 cpumask_set_cpu(i, hctx->cpumask);
1258 hctx->nr_ctx++;
1259
320ae51f
JA
1260 /*
1261 * Set local node, IFF we have more than one hw queue. If
1262 * not, we remain on the home node of the device
1263 */
1264 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1265 hctx->numa_node = cpu_to_node(i);
1266 }
1267}
1268
1269static void blk_mq_map_swqueue(struct request_queue *q)
1270{
1271 unsigned int i;
1272 struct blk_mq_hw_ctx *hctx;
1273 struct blk_mq_ctx *ctx;
1274
1275 queue_for_each_hw_ctx(q, hctx, i) {
e4043dcf 1276 cpumask_clear(hctx->cpumask);
320ae51f
JA
1277 hctx->nr_ctx = 0;
1278 }
1279
1280 /*
1281 * Map software to hardware queues
1282 */
1283 queue_for_each_ctx(q, ctx, i) {
1284 /* If the cpu isn't online, the cpu is mapped to first hctx */
e4043dcf
JA
1285 if (!cpu_online(i))
1286 continue;
1287
320ae51f 1288 hctx = q->mq_ops->map_queue(q, i);
e4043dcf 1289 cpumask_set_cpu(i, hctx->cpumask);
320ae51f
JA
1290 ctx->index_hw = hctx->nr_ctx;
1291 hctx->ctxs[hctx->nr_ctx++] = ctx;
1292 }
1293}
1294
1295struct request_queue *blk_mq_init_queue(struct blk_mq_reg *reg,
1296 void *driver_data)
1297{
1298 struct blk_mq_hw_ctx **hctxs;
1299 struct blk_mq_ctx *ctx;
1300 struct request_queue *q;
1301 int i;
1302
1303 if (!reg->nr_hw_queues ||
1304 !reg->ops->queue_rq || !reg->ops->map_queue ||
1305 !reg->ops->alloc_hctx || !reg->ops->free_hctx)
1306 return ERR_PTR(-EINVAL);
1307
1308 if (!reg->queue_depth)
1309 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1310 else if (reg->queue_depth > BLK_MQ_MAX_DEPTH) {
1311 pr_err("blk-mq: queuedepth too large (%u)\n", reg->queue_depth);
1312 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1313 }
1314
1315 if (reg->queue_depth < (reg->reserved_tags + BLK_MQ_TAG_MIN))
1316 return ERR_PTR(-EINVAL);
1317
1318 ctx = alloc_percpu(struct blk_mq_ctx);
1319 if (!ctx)
1320 return ERR_PTR(-ENOMEM);
1321
1322 hctxs = kmalloc_node(reg->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1323 reg->numa_node);
1324
1325 if (!hctxs)
1326 goto err_percpu;
1327
1328 for (i = 0; i < reg->nr_hw_queues; i++) {
1329 hctxs[i] = reg->ops->alloc_hctx(reg, i);
1330 if (!hctxs[i])
1331 goto err_hctxs;
1332
e4043dcf
JA
1333 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1334 goto err_hctxs;
1335
320ae51f
JA
1336 hctxs[i]->numa_node = NUMA_NO_NODE;
1337 hctxs[i]->queue_num = i;
1338 }
1339
1340 q = blk_alloc_queue_node(GFP_KERNEL, reg->numa_node);
1341 if (!q)
1342 goto err_hctxs;
1343
1344 q->mq_map = blk_mq_make_queue_map(reg);
1345 if (!q->mq_map)
1346 goto err_map;
1347
1348 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1349 blk_queue_rq_timeout(q, 30000);
1350
1351 q->nr_queues = nr_cpu_ids;
1352 q->nr_hw_queues = reg->nr_hw_queues;
1353
1354 q->queue_ctx = ctx;
1355 q->queue_hw_ctx = hctxs;
1356
1357 q->mq_ops = reg->ops;
94eddfbe 1358 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
320ae51f 1359
1be036e9
CH
1360 q->sg_reserved_size = INT_MAX;
1361
320ae51f
JA
1362 blk_queue_make_request(q, blk_mq_make_request);
1363 blk_queue_rq_timed_out(q, reg->ops->timeout);
1364 if (reg->timeout)
1365 blk_queue_rq_timeout(q, reg->timeout);
1366
30a91cb4
CH
1367 if (reg->ops->complete)
1368 blk_queue_softirq_done(q, reg->ops->complete);
1369
320ae51f
JA
1370 blk_mq_init_flush(q);
1371 blk_mq_init_cpu_queues(q, reg->nr_hw_queues);
1372
18741986
CH
1373 q->flush_rq = kzalloc(round_up(sizeof(struct request) + reg->cmd_size,
1374 cache_line_size()), GFP_KERNEL);
1375 if (!q->flush_rq)
320ae51f
JA
1376 goto err_hw;
1377
18741986
CH
1378 if (blk_mq_init_hw_queues(q, reg, driver_data))
1379 goto err_flush_rq;
1380
320ae51f
JA
1381 blk_mq_map_swqueue(q);
1382
1383 mutex_lock(&all_q_mutex);
1384 list_add_tail(&q->all_q_node, &all_q_list);
1385 mutex_unlock(&all_q_mutex);
1386
1387 return q;
18741986
CH
1388
1389err_flush_rq:
1390 kfree(q->flush_rq);
320ae51f
JA
1391err_hw:
1392 kfree(q->mq_map);
1393err_map:
1394 blk_cleanup_queue(q);
1395err_hctxs:
1396 for (i = 0; i < reg->nr_hw_queues; i++) {
1397 if (!hctxs[i])
1398 break;
e4043dcf 1399 free_cpumask_var(hctxs[i]->cpumask);
320ae51f
JA
1400 reg->ops->free_hctx(hctxs[i], i);
1401 }
1402 kfree(hctxs);
1403err_percpu:
1404 free_percpu(ctx);
1405 return ERR_PTR(-ENOMEM);
1406}
1407EXPORT_SYMBOL(blk_mq_init_queue);
1408
1409void blk_mq_free_queue(struct request_queue *q)
1410{
1411 struct blk_mq_hw_ctx *hctx;
1412 int i;
1413
1414 queue_for_each_hw_ctx(q, hctx, i) {
320ae51f
JA
1415 kfree(hctx->ctx_map);
1416 kfree(hctx->ctxs);
e9b267d9 1417 blk_mq_free_rq_map(hctx, q->queuedata);
320ae51f
JA
1418 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1419 if (q->mq_ops->exit_hctx)
1420 q->mq_ops->exit_hctx(hctx, i);
e4043dcf 1421 free_cpumask_var(hctx->cpumask);
320ae51f
JA
1422 q->mq_ops->free_hctx(hctx, i);
1423 }
1424
1425 free_percpu(q->queue_ctx);
1426 kfree(q->queue_hw_ctx);
1427 kfree(q->mq_map);
1428
1429 q->queue_ctx = NULL;
1430 q->queue_hw_ctx = NULL;
1431 q->mq_map = NULL;
1432
1433 mutex_lock(&all_q_mutex);
1434 list_del_init(&q->all_q_node);
1435 mutex_unlock(&all_q_mutex);
1436}
320ae51f
JA
1437
1438/* Basically redo blk_mq_init_queue with queue frozen */
f618ef7c 1439static void blk_mq_queue_reinit(struct request_queue *q)
320ae51f
JA
1440{
1441 blk_mq_freeze_queue(q);
1442
1443 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1444
1445 /*
1446 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1447 * we should change hctx numa_node according to new topology (this
1448 * involves free and re-allocate memory, worthy doing?)
1449 */
1450
1451 blk_mq_map_swqueue(q);
1452
1453 blk_mq_unfreeze_queue(q);
1454}
1455
f618ef7c
PG
1456static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1457 unsigned long action, void *hcpu)
320ae51f
JA
1458{
1459 struct request_queue *q;
1460
1461 /*
1462 * Before new mapping is established, hotadded cpu might already start
1463 * handling requests. This doesn't break anything as we map offline
1464 * CPUs to first hardware queue. We will re-init queue below to get
1465 * optimal settings.
1466 */
1467 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1468 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1469 return NOTIFY_OK;
1470
1471 mutex_lock(&all_q_mutex);
1472 list_for_each_entry(q, &all_q_list, all_q_node)
1473 blk_mq_queue_reinit(q);
1474 mutex_unlock(&all_q_mutex);
1475 return NOTIFY_OK;
1476}
1477
676141e4
JA
1478void blk_mq_disable_hotplug(void)
1479{
1480 mutex_lock(&all_q_mutex);
1481}
1482
1483void blk_mq_enable_hotplug(void)
1484{
1485 mutex_unlock(&all_q_mutex);
1486}
1487
320ae51f
JA
1488static int __init blk_mq_init(void)
1489{
320ae51f
JA
1490 blk_mq_cpu_init();
1491
1492 /* Must be called after percpu_counter_hotcpu_callback() */
1493 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1494
1495 return 0;
1496}
1497subsys_initcall(blk_mq_init);