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