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