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