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