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