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