]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - block/blk-mq.c
blk-mq: make ->flush_rq fully transparent to drivers
[mirror_ubuntu-bionic-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
95363efd
JA
1034static int blk_mq_init_hw_commands(struct blk_mq_hw_ctx *hctx,
1035 int (*init)(void *, struct blk_mq_hw_ctx *,
1036 struct request *, unsigned int),
1037 void *data)
1038{
1039 unsigned int i;
1040 int ret = 0;
1041
1042 for (i = 0; i < hctx->queue_depth; i++) {
1043 struct request *rq = hctx->rqs[i];
1044
1045 ret = init(data, hctx, rq, i);
1046 if (ret)
1047 break;
1048 }
1049
1050 return ret;
1051}
1052
1053int blk_mq_init_commands(struct request_queue *q,
1054 int (*init)(void *, struct blk_mq_hw_ctx *,
1055 struct request *, unsigned int),
1056 void *data)
1057{
1058 struct blk_mq_hw_ctx *hctx;
1059 unsigned int i;
1060 int ret = 0;
1061
1062 queue_for_each_hw_ctx(q, hctx, i) {
1063 ret = blk_mq_init_hw_commands(hctx, init, data);
1064 if (ret)
1065 break;
1066 }
1067
1068 return ret;
1069}
1070EXPORT_SYMBOL(blk_mq_init_commands);
1071
1072static void blk_mq_free_hw_commands(struct blk_mq_hw_ctx *hctx,
1073 void (*free)(void *, struct blk_mq_hw_ctx *,
320ae51f
JA
1074 struct request *, unsigned int),
1075 void *data)
1076{
1077 unsigned int i;
1078
1079 for (i = 0; i < hctx->queue_depth; i++) {
1080 struct request *rq = hctx->rqs[i];
1081
95363efd 1082 free(data, hctx, rq, i);
320ae51f
JA
1083 }
1084}
1085
95363efd
JA
1086void blk_mq_free_commands(struct request_queue *q,
1087 void (*free)(void *, struct blk_mq_hw_ctx *,
320ae51f
JA
1088 struct request *, unsigned int),
1089 void *data)
1090{
1091 struct blk_mq_hw_ctx *hctx;
1092 unsigned int i;
1093
1094 queue_for_each_hw_ctx(q, hctx, i)
95363efd 1095 blk_mq_free_hw_commands(hctx, free, data);
320ae51f 1096}
95363efd 1097EXPORT_SYMBOL(blk_mq_free_commands);
320ae51f
JA
1098
1099static void blk_mq_free_rq_map(struct blk_mq_hw_ctx *hctx)
1100{
1101 struct page *page;
1102
1103 while (!list_empty(&hctx->page_list)) {
6753471c
DH
1104 page = list_first_entry(&hctx->page_list, struct page, lru);
1105 list_del_init(&page->lru);
320ae51f
JA
1106 __free_pages(page, page->private);
1107 }
1108
1109 kfree(hctx->rqs);
1110
1111 if (hctx->tags)
1112 blk_mq_free_tags(hctx->tags);
1113}
1114
1115static size_t order_to_size(unsigned int order)
1116{
1117 size_t ret = PAGE_SIZE;
1118
1119 while (order--)
1120 ret *= 2;
1121
1122 return ret;
1123}
1124
1125static int blk_mq_init_rq_map(struct blk_mq_hw_ctx *hctx,
1126 unsigned int reserved_tags, int node)
1127{
1128 unsigned int i, j, entries_per_page, max_order = 4;
1129 size_t rq_size, left;
1130
1131 INIT_LIST_HEAD(&hctx->page_list);
1132
1133 hctx->rqs = kmalloc_node(hctx->queue_depth * sizeof(struct request *),
1134 GFP_KERNEL, node);
1135 if (!hctx->rqs)
1136 return -ENOMEM;
1137
1138 /*
1139 * rq_size is the size of the request plus driver payload, rounded
1140 * to the cacheline size
1141 */
1142 rq_size = round_up(sizeof(struct request) + hctx->cmd_size,
1143 cache_line_size());
1144 left = rq_size * hctx->queue_depth;
1145
1146 for (i = 0; i < hctx->queue_depth;) {
1147 int this_order = max_order;
1148 struct page *page;
1149 int to_do;
1150 void *p;
1151
1152 while (left < order_to_size(this_order - 1) && this_order)
1153 this_order--;
1154
1155 do {
1156 page = alloc_pages_node(node, GFP_KERNEL, this_order);
1157 if (page)
1158 break;
1159 if (!this_order--)
1160 break;
1161 if (order_to_size(this_order) < rq_size)
1162 break;
1163 } while (1);
1164
1165 if (!page)
1166 break;
1167
1168 page->private = this_order;
6753471c 1169 list_add_tail(&page->lru, &hctx->page_list);
320ae51f
JA
1170
1171 p = page_address(page);
1172 entries_per_page = order_to_size(this_order) / rq_size;
1173 to_do = min(entries_per_page, hctx->queue_depth - i);
1174 left -= to_do * rq_size;
1175 for (j = 0; j < to_do; j++) {
1176 hctx->rqs[i] = p;
9d74e257 1177 blk_rq_init(hctx->queue, hctx->rqs[i]);
320ae51f
JA
1178 p += rq_size;
1179 i++;
1180 }
1181 }
1182
1183 if (i < (reserved_tags + BLK_MQ_TAG_MIN))
1184 goto err_rq_map;
1185 else if (i != hctx->queue_depth) {
1186 hctx->queue_depth = i;
1187 pr_warn("%s: queue depth set to %u because of low memory\n",
1188 __func__, i);
1189 }
1190
1191 hctx->tags = blk_mq_init_tags(hctx->queue_depth, reserved_tags, node);
1192 if (!hctx->tags) {
1193err_rq_map:
1194 blk_mq_free_rq_map(hctx);
1195 return -ENOMEM;
1196 }
1197
1198 return 0;
1199}
1200
1201static int blk_mq_init_hw_queues(struct request_queue *q,
1202 struct blk_mq_reg *reg, void *driver_data)
1203{
1204 struct blk_mq_hw_ctx *hctx;
1205 unsigned int i, j;
1206
1207 /*
1208 * Initialize hardware queues
1209 */
1210 queue_for_each_hw_ctx(q, hctx, i) {
1211 unsigned int num_maps;
1212 int node;
1213
1214 node = hctx->numa_node;
1215 if (node == NUMA_NO_NODE)
1216 node = hctx->numa_node = reg->numa_node;
1217
1218 INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn);
1219 spin_lock_init(&hctx->lock);
1220 INIT_LIST_HEAD(&hctx->dispatch);
1221 hctx->queue = q;
1222 hctx->queue_num = i;
1223 hctx->flags = reg->flags;
1224 hctx->queue_depth = reg->queue_depth;
1225 hctx->cmd_size = reg->cmd_size;
1226
1227 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1228 blk_mq_hctx_notify, hctx);
1229 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1230
1231 if (blk_mq_init_rq_map(hctx, reg->reserved_tags, node))
1232 break;
1233
1234 /*
1235 * Allocate space for all possible cpus to avoid allocation in
1236 * runtime
1237 */
1238 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1239 GFP_KERNEL, node);
1240 if (!hctx->ctxs)
1241 break;
1242
1243 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1244 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1245 GFP_KERNEL, node);
1246 if (!hctx->ctx_map)
1247 break;
1248
1249 hctx->nr_ctx_map = num_maps;
1250 hctx->nr_ctx = 0;
1251
1252 if (reg->ops->init_hctx &&
1253 reg->ops->init_hctx(hctx, driver_data, i))
1254 break;
1255 }
1256
1257 if (i == q->nr_hw_queues)
1258 return 0;
1259
1260 /*
1261 * Init failed
1262 */
1263 queue_for_each_hw_ctx(q, hctx, j) {
1264 if (i == j)
1265 break;
1266
1267 if (reg->ops->exit_hctx)
1268 reg->ops->exit_hctx(hctx, j);
1269
1270 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1271 blk_mq_free_rq_map(hctx);
1272 kfree(hctx->ctxs);
1273 }
1274
1275 return 1;
1276}
1277
1278static void blk_mq_init_cpu_queues(struct request_queue *q,
1279 unsigned int nr_hw_queues)
1280{
1281 unsigned int i;
1282
1283 for_each_possible_cpu(i) {
1284 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1285 struct blk_mq_hw_ctx *hctx;
1286
1287 memset(__ctx, 0, sizeof(*__ctx));
1288 __ctx->cpu = i;
1289 spin_lock_init(&__ctx->lock);
1290 INIT_LIST_HEAD(&__ctx->rq_list);
1291 __ctx->queue = q;
1292
1293 /* If the cpu isn't online, the cpu is mapped to first hctx */
320ae51f
JA
1294 if (!cpu_online(i))
1295 continue;
1296
e4043dcf
JA
1297 hctx = q->mq_ops->map_queue(q, i);
1298 cpumask_set_cpu(i, hctx->cpumask);
1299 hctx->nr_ctx++;
1300
320ae51f
JA
1301 /*
1302 * Set local node, IFF we have more than one hw queue. If
1303 * not, we remain on the home node of the device
1304 */
1305 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1306 hctx->numa_node = cpu_to_node(i);
1307 }
1308}
1309
1310static void blk_mq_map_swqueue(struct request_queue *q)
1311{
1312 unsigned int i;
1313 struct blk_mq_hw_ctx *hctx;
1314 struct blk_mq_ctx *ctx;
1315
1316 queue_for_each_hw_ctx(q, hctx, i) {
e4043dcf 1317 cpumask_clear(hctx->cpumask);
320ae51f
JA
1318 hctx->nr_ctx = 0;
1319 }
1320
1321 /*
1322 * Map software to hardware queues
1323 */
1324 queue_for_each_ctx(q, ctx, i) {
1325 /* If the cpu isn't online, the cpu is mapped to first hctx */
e4043dcf
JA
1326 if (!cpu_online(i))
1327 continue;
1328
320ae51f 1329 hctx = q->mq_ops->map_queue(q, i);
e4043dcf 1330 cpumask_set_cpu(i, hctx->cpumask);
320ae51f
JA
1331 ctx->index_hw = hctx->nr_ctx;
1332 hctx->ctxs[hctx->nr_ctx++] = ctx;
1333 }
1334}
1335
1336struct request_queue *blk_mq_init_queue(struct blk_mq_reg *reg,
1337 void *driver_data)
1338{
1339 struct blk_mq_hw_ctx **hctxs;
1340 struct blk_mq_ctx *ctx;
1341 struct request_queue *q;
1342 int i;
1343
1344 if (!reg->nr_hw_queues ||
1345 !reg->ops->queue_rq || !reg->ops->map_queue ||
1346 !reg->ops->alloc_hctx || !reg->ops->free_hctx)
1347 return ERR_PTR(-EINVAL);
1348
1349 if (!reg->queue_depth)
1350 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1351 else if (reg->queue_depth > BLK_MQ_MAX_DEPTH) {
1352 pr_err("blk-mq: queuedepth too large (%u)\n", reg->queue_depth);
1353 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1354 }
1355
1356 if (reg->queue_depth < (reg->reserved_tags + BLK_MQ_TAG_MIN))
1357 return ERR_PTR(-EINVAL);
1358
1359 ctx = alloc_percpu(struct blk_mq_ctx);
1360 if (!ctx)
1361 return ERR_PTR(-ENOMEM);
1362
1363 hctxs = kmalloc_node(reg->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1364 reg->numa_node);
1365
1366 if (!hctxs)
1367 goto err_percpu;
1368
1369 for (i = 0; i < reg->nr_hw_queues; i++) {
1370 hctxs[i] = reg->ops->alloc_hctx(reg, i);
1371 if (!hctxs[i])
1372 goto err_hctxs;
1373
e4043dcf
JA
1374 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1375 goto err_hctxs;
1376
320ae51f
JA
1377 hctxs[i]->numa_node = NUMA_NO_NODE;
1378 hctxs[i]->queue_num = i;
1379 }
1380
1381 q = blk_alloc_queue_node(GFP_KERNEL, reg->numa_node);
1382 if (!q)
1383 goto err_hctxs;
1384
1385 q->mq_map = blk_mq_make_queue_map(reg);
1386 if (!q->mq_map)
1387 goto err_map;
1388
1389 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1390 blk_queue_rq_timeout(q, 30000);
1391
1392 q->nr_queues = nr_cpu_ids;
1393 q->nr_hw_queues = reg->nr_hw_queues;
1394
1395 q->queue_ctx = ctx;
1396 q->queue_hw_ctx = hctxs;
1397
1398 q->mq_ops = reg->ops;
94eddfbe 1399 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
320ae51f 1400
1be036e9
CH
1401 q->sg_reserved_size = INT_MAX;
1402
320ae51f
JA
1403 blk_queue_make_request(q, blk_mq_make_request);
1404 blk_queue_rq_timed_out(q, reg->ops->timeout);
1405 if (reg->timeout)
1406 blk_queue_rq_timeout(q, reg->timeout);
1407
30a91cb4
CH
1408 if (reg->ops->complete)
1409 blk_queue_softirq_done(q, reg->ops->complete);
1410
320ae51f
JA
1411 blk_mq_init_flush(q);
1412 blk_mq_init_cpu_queues(q, reg->nr_hw_queues);
1413
18741986
CH
1414 q->flush_rq = kzalloc(round_up(sizeof(struct request) + reg->cmd_size,
1415 cache_line_size()), GFP_KERNEL);
1416 if (!q->flush_rq)
320ae51f
JA
1417 goto err_hw;
1418
18741986
CH
1419 if (blk_mq_init_hw_queues(q, reg, driver_data))
1420 goto err_flush_rq;
1421
320ae51f
JA
1422 blk_mq_map_swqueue(q);
1423
1424 mutex_lock(&all_q_mutex);
1425 list_add_tail(&q->all_q_node, &all_q_list);
1426 mutex_unlock(&all_q_mutex);
1427
1428 return q;
18741986
CH
1429
1430err_flush_rq:
1431 kfree(q->flush_rq);
320ae51f
JA
1432err_hw:
1433 kfree(q->mq_map);
1434err_map:
1435 blk_cleanup_queue(q);
1436err_hctxs:
1437 for (i = 0; i < reg->nr_hw_queues; i++) {
1438 if (!hctxs[i])
1439 break;
e4043dcf 1440 free_cpumask_var(hctxs[i]->cpumask);
320ae51f
JA
1441 reg->ops->free_hctx(hctxs[i], i);
1442 }
1443 kfree(hctxs);
1444err_percpu:
1445 free_percpu(ctx);
1446 return ERR_PTR(-ENOMEM);
1447}
1448EXPORT_SYMBOL(blk_mq_init_queue);
1449
1450void blk_mq_free_queue(struct request_queue *q)
1451{
1452 struct blk_mq_hw_ctx *hctx;
1453 int i;
1454
1455 queue_for_each_hw_ctx(q, hctx, i) {
320ae51f
JA
1456 kfree(hctx->ctx_map);
1457 kfree(hctx->ctxs);
1458 blk_mq_free_rq_map(hctx);
1459 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1460 if (q->mq_ops->exit_hctx)
1461 q->mq_ops->exit_hctx(hctx, i);
e4043dcf 1462 free_cpumask_var(hctx->cpumask);
320ae51f
JA
1463 q->mq_ops->free_hctx(hctx, i);
1464 }
1465
1466 free_percpu(q->queue_ctx);
1467 kfree(q->queue_hw_ctx);
1468 kfree(q->mq_map);
1469
1470 q->queue_ctx = NULL;
1471 q->queue_hw_ctx = NULL;
1472 q->mq_map = NULL;
1473
1474 mutex_lock(&all_q_mutex);
1475 list_del_init(&q->all_q_node);
1476 mutex_unlock(&all_q_mutex);
1477}
320ae51f
JA
1478
1479/* Basically redo blk_mq_init_queue with queue frozen */
f618ef7c 1480static void blk_mq_queue_reinit(struct request_queue *q)
320ae51f
JA
1481{
1482 blk_mq_freeze_queue(q);
1483
1484 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1485
1486 /*
1487 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1488 * we should change hctx numa_node according to new topology (this
1489 * involves free and re-allocate memory, worthy doing?)
1490 */
1491
1492 blk_mq_map_swqueue(q);
1493
1494 blk_mq_unfreeze_queue(q);
1495}
1496
f618ef7c
PG
1497static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1498 unsigned long action, void *hcpu)
320ae51f
JA
1499{
1500 struct request_queue *q;
1501
1502 /*
1503 * Before new mapping is established, hotadded cpu might already start
1504 * handling requests. This doesn't break anything as we map offline
1505 * CPUs to first hardware queue. We will re-init queue below to get
1506 * optimal settings.
1507 */
1508 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1509 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1510 return NOTIFY_OK;
1511
1512 mutex_lock(&all_q_mutex);
1513 list_for_each_entry(q, &all_q_list, all_q_node)
1514 blk_mq_queue_reinit(q);
1515 mutex_unlock(&all_q_mutex);
1516 return NOTIFY_OK;
1517}
1518
676141e4
JA
1519void blk_mq_disable_hotplug(void)
1520{
1521 mutex_lock(&all_q_mutex);
1522}
1523
1524void blk_mq_enable_hotplug(void)
1525{
1526 mutex_unlock(&all_q_mutex);
1527}
1528
320ae51f
JA
1529static int __init blk_mq_init(void)
1530{
320ae51f
JA
1531 blk_mq_cpu_init();
1532
1533 /* Must be called after percpu_counter_hotcpu_callback() */
1534 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1535
1536 return 0;
1537}
1538subsys_initcall(blk_mq_init);