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