blk-mq: initialize sg_reserved_size
[cascardo/linux.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                 if (q->dma_drain_size && blk_rq_bytes(rq)) {
586                         /*
587                          * make sure space for the drain appears we
588                          * know we can do this because max_hw_segments
589                          * has been adjusted to be one fewer than the
590                          * device can handle
591                          */
592                         rq->nr_phys_segments++;
593                 }
594
595                 /*
596                  * Last request in the series. Flag it as such, this
597                  * enables drivers to know when IO should be kicked off,
598                  * if they don't do it on a per-request basis.
599                  *
600                  * Note: the flag isn't the only condition drivers
601                  * should do kick off. If drive is busy, the last
602                  * request might not have the bit set.
603                  */
604                 if (list_empty(&rq_list))
605                         rq->cmd_flags |= REQ_END;
606
607                 ret = q->mq_ops->queue_rq(hctx, rq);
608                 switch (ret) {
609                 case BLK_MQ_RQ_QUEUE_OK:
610                         queued++;
611                         continue;
612                 case BLK_MQ_RQ_QUEUE_BUSY:
613                         /*
614                          * FIXME: we should have a mechanism to stop the queue
615                          * like blk_stop_queue, otherwise we will waste cpu
616                          * time
617                          */
618                         list_add(&rq->queuelist, &rq_list);
619                         blk_mq_requeue_request(rq);
620                         break;
621                 default:
622                         pr_err("blk-mq: bad return on queue: %d\n", ret);
623                         rq->errors = -EIO;
624                 case BLK_MQ_RQ_QUEUE_ERROR:
625                         blk_mq_end_io(rq, rq->errors);
626                         break;
627                 }
628
629                 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
630                         break;
631         }
632
633         if (!queued)
634                 hctx->dispatched[0]++;
635         else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
636                 hctx->dispatched[ilog2(queued) + 1]++;
637
638         /*
639          * Any items that need requeuing? Stuff them into hctx->dispatch,
640          * that is where we will continue on next queue run.
641          */
642         if (!list_empty(&rq_list)) {
643                 spin_lock(&hctx->lock);
644                 list_splice(&rq_list, &hctx->dispatch);
645                 spin_unlock(&hctx->lock);
646         }
647 }
648
649 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
650 {
651         if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->flags)))
652                 return;
653
654         if (!async)
655                 __blk_mq_run_hw_queue(hctx);
656         else {
657                 struct request_queue *q = hctx->queue;
658
659                 kblockd_schedule_delayed_work(q, &hctx->delayed_work, 0);
660         }
661 }
662
663 void blk_mq_run_queues(struct request_queue *q, bool async)
664 {
665         struct blk_mq_hw_ctx *hctx;
666         int i;
667
668         queue_for_each_hw_ctx(q, hctx, i) {
669                 if ((!blk_mq_hctx_has_pending(hctx) &&
670                     list_empty_careful(&hctx->dispatch)) ||
671                     test_bit(BLK_MQ_S_STOPPED, &hctx->flags))
672                         continue;
673
674                 blk_mq_run_hw_queue(hctx, async);
675         }
676 }
677 EXPORT_SYMBOL(blk_mq_run_queues);
678
679 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
680 {
681         cancel_delayed_work(&hctx->delayed_work);
682         set_bit(BLK_MQ_S_STOPPED, &hctx->state);
683 }
684 EXPORT_SYMBOL(blk_mq_stop_hw_queue);
685
686 void blk_mq_stop_hw_queues(struct request_queue *q)
687 {
688         struct blk_mq_hw_ctx *hctx;
689         int i;
690
691         queue_for_each_hw_ctx(q, hctx, i)
692                 blk_mq_stop_hw_queue(hctx);
693 }
694 EXPORT_SYMBOL(blk_mq_stop_hw_queues);
695
696 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
697 {
698         clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
699         __blk_mq_run_hw_queue(hctx);
700 }
701 EXPORT_SYMBOL(blk_mq_start_hw_queue);
702
703 void blk_mq_start_stopped_hw_queues(struct request_queue *q)
704 {
705         struct blk_mq_hw_ctx *hctx;
706         int i;
707
708         queue_for_each_hw_ctx(q, hctx, i) {
709                 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
710                         continue;
711
712                 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
713                 blk_mq_run_hw_queue(hctx, true);
714         }
715 }
716 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
717
718 static void blk_mq_work_fn(struct work_struct *work)
719 {
720         struct blk_mq_hw_ctx *hctx;
721
722         hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work);
723         __blk_mq_run_hw_queue(hctx);
724 }
725
726 static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
727                                     struct request *rq, bool at_head)
728 {
729         struct blk_mq_ctx *ctx = rq->mq_ctx;
730
731         trace_block_rq_insert(hctx->queue, rq);
732
733         if (at_head)
734                 list_add(&rq->queuelist, &ctx->rq_list);
735         else
736                 list_add_tail(&rq->queuelist, &ctx->rq_list);
737         blk_mq_hctx_mark_pending(hctx, ctx);
738
739         /*
740          * We do this early, to ensure we are on the right CPU.
741          */
742         blk_mq_add_timer(rq);
743 }
744
745 void blk_mq_insert_request(struct request_queue *q, struct request *rq,
746                            bool at_head, bool run_queue)
747 {
748         struct blk_mq_hw_ctx *hctx;
749         struct blk_mq_ctx *ctx, *current_ctx;
750
751         ctx = rq->mq_ctx;
752         hctx = q->mq_ops->map_queue(q, ctx->cpu);
753
754         if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
755                 blk_insert_flush(rq);
756         } else {
757                 current_ctx = blk_mq_get_ctx(q);
758
759                 if (!cpu_online(ctx->cpu)) {
760                         ctx = current_ctx;
761                         hctx = q->mq_ops->map_queue(q, ctx->cpu);
762                         rq->mq_ctx = ctx;
763                 }
764                 spin_lock(&ctx->lock);
765                 __blk_mq_insert_request(hctx, rq, at_head);
766                 spin_unlock(&ctx->lock);
767
768                 blk_mq_put_ctx(current_ctx);
769         }
770
771         if (run_queue)
772                 __blk_mq_run_hw_queue(hctx);
773 }
774 EXPORT_SYMBOL(blk_mq_insert_request);
775
776 /*
777  * This is a special version of blk_mq_insert_request to bypass FLUSH request
778  * check. Should only be used internally.
779  */
780 void blk_mq_run_request(struct request *rq, bool run_queue, bool async)
781 {
782         struct request_queue *q = rq->q;
783         struct blk_mq_hw_ctx *hctx;
784         struct blk_mq_ctx *ctx, *current_ctx;
785
786         current_ctx = blk_mq_get_ctx(q);
787
788         ctx = rq->mq_ctx;
789         if (!cpu_online(ctx->cpu)) {
790                 ctx = current_ctx;
791                 rq->mq_ctx = ctx;
792         }
793         hctx = q->mq_ops->map_queue(q, ctx->cpu);
794
795         /* ctx->cpu might be offline */
796         spin_lock(&ctx->lock);
797         __blk_mq_insert_request(hctx, rq, false);
798         spin_unlock(&ctx->lock);
799
800         blk_mq_put_ctx(current_ctx);
801
802         if (run_queue)
803                 blk_mq_run_hw_queue(hctx, async);
804 }
805
806 static void blk_mq_insert_requests(struct request_queue *q,
807                                      struct blk_mq_ctx *ctx,
808                                      struct list_head *list,
809                                      int depth,
810                                      bool from_schedule)
811
812 {
813         struct blk_mq_hw_ctx *hctx;
814         struct blk_mq_ctx *current_ctx;
815
816         trace_block_unplug(q, depth, !from_schedule);
817
818         current_ctx = blk_mq_get_ctx(q);
819
820         if (!cpu_online(ctx->cpu))
821                 ctx = current_ctx;
822         hctx = q->mq_ops->map_queue(q, ctx->cpu);
823
824         /*
825          * preemption doesn't flush plug list, so it's possible ctx->cpu is
826          * offline now
827          */
828         spin_lock(&ctx->lock);
829         while (!list_empty(list)) {
830                 struct request *rq;
831
832                 rq = list_first_entry(list, struct request, queuelist);
833                 list_del_init(&rq->queuelist);
834                 rq->mq_ctx = ctx;
835                 __blk_mq_insert_request(hctx, rq, false);
836         }
837         spin_unlock(&ctx->lock);
838
839         blk_mq_put_ctx(current_ctx);
840
841         blk_mq_run_hw_queue(hctx, from_schedule);
842 }
843
844 static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
845 {
846         struct request *rqa = container_of(a, struct request, queuelist);
847         struct request *rqb = container_of(b, struct request, queuelist);
848
849         return !(rqa->mq_ctx < rqb->mq_ctx ||
850                  (rqa->mq_ctx == rqb->mq_ctx &&
851                   blk_rq_pos(rqa) < blk_rq_pos(rqb)));
852 }
853
854 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
855 {
856         struct blk_mq_ctx *this_ctx;
857         struct request_queue *this_q;
858         struct request *rq;
859         LIST_HEAD(list);
860         LIST_HEAD(ctx_list);
861         unsigned int depth;
862
863         list_splice_init(&plug->mq_list, &list);
864
865         list_sort(NULL, &list, plug_ctx_cmp);
866
867         this_q = NULL;
868         this_ctx = NULL;
869         depth = 0;
870
871         while (!list_empty(&list)) {
872                 rq = list_entry_rq(list.next);
873                 list_del_init(&rq->queuelist);
874                 BUG_ON(!rq->q);
875                 if (rq->mq_ctx != this_ctx) {
876                         if (this_ctx) {
877                                 blk_mq_insert_requests(this_q, this_ctx,
878                                                         &ctx_list, depth,
879                                                         from_schedule);
880                         }
881
882                         this_ctx = rq->mq_ctx;
883                         this_q = rq->q;
884                         depth = 0;
885                 }
886
887                 depth++;
888                 list_add_tail(&rq->queuelist, &ctx_list);
889         }
890
891         /*
892          * If 'this_ctx' is set, we know we have entries to complete
893          * on 'ctx_list'. Do those.
894          */
895         if (this_ctx) {
896                 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
897                                        from_schedule);
898         }
899 }
900
901 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
902 {
903         init_request_from_bio(rq, bio);
904         blk_account_io_start(rq, 1);
905 }
906
907 static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
908 {
909         struct blk_mq_hw_ctx *hctx;
910         struct blk_mq_ctx *ctx;
911         const int is_sync = rw_is_sync(bio->bi_rw);
912         const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
913         int rw = bio_data_dir(bio);
914         struct request *rq;
915         unsigned int use_plug, request_count = 0;
916
917         /*
918          * If we have multiple hardware queues, just go directly to
919          * one of those for sync IO.
920          */
921         use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
922
923         blk_queue_bounce(q, &bio);
924
925         if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
926                 return;
927
928         if (blk_mq_queue_enter(q)) {
929                 bio_endio(bio, -EIO);
930                 return;
931         }
932
933         ctx = blk_mq_get_ctx(q);
934         hctx = q->mq_ops->map_queue(q, ctx->cpu);
935
936         trace_block_getrq(q, bio, rw);
937         rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false, bio->bi_rw);
938         if (likely(rq))
939                 blk_mq_rq_ctx_init(q, ctx, rq, bio->bi_rw);
940         else {
941                 blk_mq_put_ctx(ctx);
942                 trace_block_sleeprq(q, bio, rw);
943                 rq = blk_mq_alloc_request_pinned(q, bio->bi_rw,
944                                 __GFP_WAIT|GFP_ATOMIC, false);
945                 ctx = rq->mq_ctx;
946                 hctx = q->mq_ops->map_queue(q, ctx->cpu);
947         }
948
949         hctx->queued++;
950
951         if (unlikely(is_flush_fua)) {
952                 blk_mq_bio_to_request(rq, bio);
953                 blk_mq_put_ctx(ctx);
954                 blk_insert_flush(rq);
955                 goto run_queue;
956         }
957
958         /*
959          * A task plug currently exists. Since this is completely lockless,
960          * utilize that to temporarily store requests until the task is
961          * either done or scheduled away.
962          */
963         if (use_plug) {
964                 struct blk_plug *plug = current->plug;
965
966                 if (plug) {
967                         blk_mq_bio_to_request(rq, bio);
968                         if (list_empty(&plug->mq_list))
969                                 trace_block_plug(q);
970                         else if (request_count >= BLK_MAX_REQUEST_COUNT) {
971                                 blk_flush_plug_list(plug, false);
972                                 trace_block_plug(q);
973                         }
974                         list_add_tail(&rq->queuelist, &plug->mq_list);
975                         blk_mq_put_ctx(ctx);
976                         return;
977                 }
978         }
979
980         spin_lock(&ctx->lock);
981
982         if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
983             blk_mq_attempt_merge(q, ctx, bio))
984                 __blk_mq_free_request(hctx, ctx, rq);
985         else {
986                 blk_mq_bio_to_request(rq, bio);
987                 __blk_mq_insert_request(hctx, rq, false);
988         }
989
990         spin_unlock(&ctx->lock);
991         blk_mq_put_ctx(ctx);
992
993         /*
994          * For a SYNC request, send it to the hardware immediately. For an
995          * ASYNC request, just ensure that we run it later on. The latter
996          * allows for merging opportunities and more efficient dispatching.
997          */
998 run_queue:
999         blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
1000 }
1001
1002 /*
1003  * Default mapping to a software queue, since we use one per CPU.
1004  */
1005 struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1006 {
1007         return q->queue_hw_ctx[q->mq_map[cpu]];
1008 }
1009 EXPORT_SYMBOL(blk_mq_map_queue);
1010
1011 struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_reg *reg,
1012                                                    unsigned int hctx_index)
1013 {
1014         return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
1015                                 GFP_KERNEL | __GFP_ZERO, reg->numa_node);
1016 }
1017 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
1018
1019 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
1020                                  unsigned int hctx_index)
1021 {
1022         kfree(hctx);
1023 }
1024 EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
1025
1026 static void blk_mq_hctx_notify(void *data, unsigned long action,
1027                                unsigned int cpu)
1028 {
1029         struct blk_mq_hw_ctx *hctx = data;
1030         struct blk_mq_ctx *ctx;
1031         LIST_HEAD(tmp);
1032
1033         if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1034                 return;
1035
1036         /*
1037          * Move ctx entries to new CPU, if this one is going away.
1038          */
1039         ctx = __blk_mq_get_ctx(hctx->queue, cpu);
1040
1041         spin_lock(&ctx->lock);
1042         if (!list_empty(&ctx->rq_list)) {
1043                 list_splice_init(&ctx->rq_list, &tmp);
1044                 clear_bit(ctx->index_hw, hctx->ctx_map);
1045         }
1046         spin_unlock(&ctx->lock);
1047
1048         if (list_empty(&tmp))
1049                 return;
1050
1051         ctx = blk_mq_get_ctx(hctx->queue);
1052         spin_lock(&ctx->lock);
1053
1054         while (!list_empty(&tmp)) {
1055                 struct request *rq;
1056
1057                 rq = list_first_entry(&tmp, struct request, queuelist);
1058                 rq->mq_ctx = ctx;
1059                 list_move_tail(&rq->queuelist, &ctx->rq_list);
1060         }
1061
1062         blk_mq_hctx_mark_pending(hctx, ctx);
1063
1064         spin_unlock(&ctx->lock);
1065         blk_mq_put_ctx(ctx);
1066 }
1067
1068 static void blk_mq_init_hw_commands(struct blk_mq_hw_ctx *hctx,
1069                                     void (*init)(void *, struct blk_mq_hw_ctx *,
1070                                         struct request *, unsigned int),
1071                                     void *data)
1072 {
1073         unsigned int i;
1074
1075         for (i = 0; i < hctx->queue_depth; i++) {
1076                 struct request *rq = hctx->rqs[i];
1077
1078                 init(data, hctx, rq, i);
1079         }
1080 }
1081
1082 void blk_mq_init_commands(struct request_queue *q,
1083                           void (*init)(void *, struct blk_mq_hw_ctx *,
1084                                         struct request *, unsigned int),
1085                           void *data)
1086 {
1087         struct blk_mq_hw_ctx *hctx;
1088         unsigned int i;
1089
1090         queue_for_each_hw_ctx(q, hctx, i)
1091                 blk_mq_init_hw_commands(hctx, init, data);
1092 }
1093 EXPORT_SYMBOL(blk_mq_init_commands);
1094
1095 static void blk_mq_free_rq_map(struct blk_mq_hw_ctx *hctx)
1096 {
1097         struct page *page;
1098
1099         while (!list_empty(&hctx->page_list)) {
1100                 page = list_first_entry(&hctx->page_list, struct page, lru);
1101                 list_del_init(&page->lru);
1102                 __free_pages(page, page->private);
1103         }
1104
1105         kfree(hctx->rqs);
1106
1107         if (hctx->tags)
1108                 blk_mq_free_tags(hctx->tags);
1109 }
1110
1111 static size_t order_to_size(unsigned int order)
1112 {
1113         size_t ret = PAGE_SIZE;
1114
1115         while (order--)
1116                 ret *= 2;
1117
1118         return ret;
1119 }
1120
1121 static int blk_mq_init_rq_map(struct blk_mq_hw_ctx *hctx,
1122                               unsigned int reserved_tags, int node)
1123 {
1124         unsigned int i, j, entries_per_page, max_order = 4;
1125         size_t rq_size, left;
1126
1127         INIT_LIST_HEAD(&hctx->page_list);
1128
1129         hctx->rqs = kmalloc_node(hctx->queue_depth * sizeof(struct request *),
1130                                         GFP_KERNEL, node);
1131         if (!hctx->rqs)
1132                 return -ENOMEM;
1133
1134         /*
1135          * rq_size is the size of the request plus driver payload, rounded
1136          * to the cacheline size
1137          */
1138         rq_size = round_up(sizeof(struct request) + hctx->cmd_size,
1139                                 cache_line_size());
1140         left = rq_size * hctx->queue_depth;
1141
1142         for (i = 0; i < hctx->queue_depth;) {
1143                 int this_order = max_order;
1144                 struct page *page;
1145                 int to_do;
1146                 void *p;
1147
1148                 while (left < order_to_size(this_order - 1) && this_order)
1149                         this_order--;
1150
1151                 do {
1152                         page = alloc_pages_node(node, GFP_KERNEL, this_order);
1153                         if (page)
1154                                 break;
1155                         if (!this_order--)
1156                                 break;
1157                         if (order_to_size(this_order) < rq_size)
1158                                 break;
1159                 } while (1);
1160
1161                 if (!page)
1162                         break;
1163
1164                 page->private = this_order;
1165                 list_add_tail(&page->lru, &hctx->page_list);
1166
1167                 p = page_address(page);
1168                 entries_per_page = order_to_size(this_order) / rq_size;
1169                 to_do = min(entries_per_page, hctx->queue_depth - i);
1170                 left -= to_do * rq_size;
1171                 for (j = 0; j < to_do; j++) {
1172                         hctx->rqs[i] = p;
1173                         blk_mq_rq_init(hctx, hctx->rqs[i]);
1174                         p += rq_size;
1175                         i++;
1176                 }
1177         }
1178
1179         if (i < (reserved_tags + BLK_MQ_TAG_MIN))
1180                 goto err_rq_map;
1181         else if (i != hctx->queue_depth) {
1182                 hctx->queue_depth = i;
1183                 pr_warn("%s: queue depth set to %u because of low memory\n",
1184                                         __func__, i);
1185         }
1186
1187         hctx->tags = blk_mq_init_tags(hctx->queue_depth, reserved_tags, node);
1188         if (!hctx->tags) {
1189 err_rq_map:
1190                 blk_mq_free_rq_map(hctx);
1191                 return -ENOMEM;
1192         }
1193
1194         return 0;
1195 }
1196
1197 static int blk_mq_init_hw_queues(struct request_queue *q,
1198                                  struct blk_mq_reg *reg, void *driver_data)
1199 {
1200         struct blk_mq_hw_ctx *hctx;
1201         unsigned int i, j;
1202
1203         /*
1204          * Initialize hardware queues
1205          */
1206         queue_for_each_hw_ctx(q, hctx, i) {
1207                 unsigned int num_maps;
1208                 int node;
1209
1210                 node = hctx->numa_node;
1211                 if (node == NUMA_NO_NODE)
1212                         node = hctx->numa_node = reg->numa_node;
1213
1214                 INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn);
1215                 spin_lock_init(&hctx->lock);
1216                 INIT_LIST_HEAD(&hctx->dispatch);
1217                 hctx->queue = q;
1218                 hctx->queue_num = i;
1219                 hctx->flags = reg->flags;
1220                 hctx->queue_depth = reg->queue_depth;
1221                 hctx->reserved_tags = reg->reserved_tags;
1222                 hctx->cmd_size = reg->cmd_size;
1223                 atomic_set(&hctx->pending_flush, 0);
1224
1225                 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1226                                                 blk_mq_hctx_notify, hctx);
1227                 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1228
1229                 if (blk_mq_init_rq_map(hctx, reg->reserved_tags, node))
1230                         break;
1231
1232                 /*
1233                  * Allocate space for all possible cpus to avoid allocation in
1234                  * runtime
1235                  */
1236                 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1237                                                 GFP_KERNEL, node);
1238                 if (!hctx->ctxs)
1239                         break;
1240
1241                 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1242                 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1243                                                 GFP_KERNEL, node);
1244                 if (!hctx->ctx_map)
1245                         break;
1246
1247                 hctx->nr_ctx_map = num_maps;
1248                 hctx->nr_ctx = 0;
1249
1250                 if (reg->ops->init_hctx &&
1251                     reg->ops->init_hctx(hctx, driver_data, i))
1252                         break;
1253         }
1254
1255         if (i == q->nr_hw_queues)
1256                 return 0;
1257
1258         /*
1259          * Init failed
1260          */
1261         queue_for_each_hw_ctx(q, hctx, j) {
1262                 if (i == j)
1263                         break;
1264
1265                 if (reg->ops->exit_hctx)
1266                         reg->ops->exit_hctx(hctx, j);
1267
1268                 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1269                 blk_mq_free_rq_map(hctx);
1270                 kfree(hctx->ctxs);
1271         }
1272
1273         return 1;
1274 }
1275
1276 static void blk_mq_init_cpu_queues(struct request_queue *q,
1277                                    unsigned int nr_hw_queues)
1278 {
1279         unsigned int i;
1280
1281         for_each_possible_cpu(i) {
1282                 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1283                 struct blk_mq_hw_ctx *hctx;
1284
1285                 memset(__ctx, 0, sizeof(*__ctx));
1286                 __ctx->cpu = i;
1287                 spin_lock_init(&__ctx->lock);
1288                 INIT_LIST_HEAD(&__ctx->rq_list);
1289                 __ctx->queue = q;
1290
1291                 /* If the cpu isn't online, the cpu is mapped to first hctx */
1292                 hctx = q->mq_ops->map_queue(q, i);
1293                 hctx->nr_ctx++;
1294
1295                 if (!cpu_online(i))
1296                         continue;
1297
1298                 /*
1299                  * Set local node, IFF we have more than one hw queue. If
1300                  * not, we remain on the home node of the device
1301                  */
1302                 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1303                         hctx->numa_node = cpu_to_node(i);
1304         }
1305 }
1306
1307 static void blk_mq_map_swqueue(struct request_queue *q)
1308 {
1309         unsigned int i;
1310         struct blk_mq_hw_ctx *hctx;
1311         struct blk_mq_ctx *ctx;
1312
1313         queue_for_each_hw_ctx(q, hctx, i) {
1314                 hctx->nr_ctx = 0;
1315         }
1316
1317         /*
1318          * Map software to hardware queues
1319          */
1320         queue_for_each_ctx(q, ctx, i) {
1321                 /* If the cpu isn't online, the cpu is mapped to first hctx */
1322                 hctx = q->mq_ops->map_queue(q, i);
1323                 ctx->index_hw = hctx->nr_ctx;
1324                 hctx->ctxs[hctx->nr_ctx++] = ctx;
1325         }
1326 }
1327
1328 struct request_queue *blk_mq_init_queue(struct blk_mq_reg *reg,
1329                                         void *driver_data)
1330 {
1331         struct blk_mq_hw_ctx **hctxs;
1332         struct blk_mq_ctx *ctx;
1333         struct request_queue *q;
1334         int i;
1335
1336         if (!reg->nr_hw_queues ||
1337             !reg->ops->queue_rq || !reg->ops->map_queue ||
1338             !reg->ops->alloc_hctx || !reg->ops->free_hctx)
1339                 return ERR_PTR(-EINVAL);
1340
1341         if (!reg->queue_depth)
1342                 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1343         else if (reg->queue_depth > BLK_MQ_MAX_DEPTH) {
1344                 pr_err("blk-mq: queuedepth too large (%u)\n", reg->queue_depth);
1345                 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1346         }
1347
1348         if (reg->queue_depth < (reg->reserved_tags + BLK_MQ_TAG_MIN))
1349                 return ERR_PTR(-EINVAL);
1350
1351         ctx = alloc_percpu(struct blk_mq_ctx);
1352         if (!ctx)
1353                 return ERR_PTR(-ENOMEM);
1354
1355         hctxs = kmalloc_node(reg->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1356                         reg->numa_node);
1357
1358         if (!hctxs)
1359                 goto err_percpu;
1360
1361         for (i = 0; i < reg->nr_hw_queues; i++) {
1362                 hctxs[i] = reg->ops->alloc_hctx(reg, i);
1363                 if (!hctxs[i])
1364                         goto err_hctxs;
1365
1366                 hctxs[i]->numa_node = NUMA_NO_NODE;
1367                 hctxs[i]->queue_num = i;
1368         }
1369
1370         q = blk_alloc_queue_node(GFP_KERNEL, reg->numa_node);
1371         if (!q)
1372                 goto err_hctxs;
1373
1374         q->mq_map = blk_mq_make_queue_map(reg);
1375         if (!q->mq_map)
1376                 goto err_map;
1377
1378         setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1379         blk_queue_rq_timeout(q, 30000);
1380
1381         q->nr_queues = nr_cpu_ids;
1382         q->nr_hw_queues = reg->nr_hw_queues;
1383
1384         q->queue_ctx = ctx;
1385         q->queue_hw_ctx = hctxs;
1386
1387         q->mq_ops = reg->ops;
1388         q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
1389
1390         q->sg_reserved_size = INT_MAX;
1391
1392         blk_queue_make_request(q, blk_mq_make_request);
1393         blk_queue_rq_timed_out(q, reg->ops->timeout);
1394         if (reg->timeout)
1395                 blk_queue_rq_timeout(q, reg->timeout);
1396
1397         blk_mq_init_flush(q);
1398         blk_mq_init_cpu_queues(q, reg->nr_hw_queues);
1399
1400         if (blk_mq_init_hw_queues(q, reg, driver_data))
1401                 goto err_hw;
1402
1403         blk_mq_map_swqueue(q);
1404
1405         mutex_lock(&all_q_mutex);
1406         list_add_tail(&q->all_q_node, &all_q_list);
1407         mutex_unlock(&all_q_mutex);
1408
1409         return q;
1410 err_hw:
1411         kfree(q->mq_map);
1412 err_map:
1413         blk_cleanup_queue(q);
1414 err_hctxs:
1415         for (i = 0; i < reg->nr_hw_queues; i++) {
1416                 if (!hctxs[i])
1417                         break;
1418                 reg->ops->free_hctx(hctxs[i], i);
1419         }
1420         kfree(hctxs);
1421 err_percpu:
1422         free_percpu(ctx);
1423         return ERR_PTR(-ENOMEM);
1424 }
1425 EXPORT_SYMBOL(blk_mq_init_queue);
1426
1427 void blk_mq_free_queue(struct request_queue *q)
1428 {
1429         struct blk_mq_hw_ctx *hctx;
1430         int i;
1431
1432         queue_for_each_hw_ctx(q, hctx, i) {
1433                 kfree(hctx->ctx_map);
1434                 kfree(hctx->ctxs);
1435                 blk_mq_free_rq_map(hctx);
1436                 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1437                 if (q->mq_ops->exit_hctx)
1438                         q->mq_ops->exit_hctx(hctx, i);
1439                 q->mq_ops->free_hctx(hctx, i);
1440         }
1441
1442         free_percpu(q->queue_ctx);
1443         kfree(q->queue_hw_ctx);
1444         kfree(q->mq_map);
1445
1446         q->queue_ctx = NULL;
1447         q->queue_hw_ctx = NULL;
1448         q->mq_map = NULL;
1449
1450         mutex_lock(&all_q_mutex);
1451         list_del_init(&q->all_q_node);
1452         mutex_unlock(&all_q_mutex);
1453 }
1454
1455 /* Basically redo blk_mq_init_queue with queue frozen */
1456 static void blk_mq_queue_reinit(struct request_queue *q)
1457 {
1458         blk_mq_freeze_queue(q);
1459
1460         blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1461
1462         /*
1463          * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1464          * we should change hctx numa_node according to new topology (this
1465          * involves free and re-allocate memory, worthy doing?)
1466          */
1467
1468         blk_mq_map_swqueue(q);
1469
1470         blk_mq_unfreeze_queue(q);
1471 }
1472
1473 static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1474                                       unsigned long action, void *hcpu)
1475 {
1476         struct request_queue *q;
1477
1478         /*
1479          * Before new mapping is established, hotadded cpu might already start
1480          * handling requests. This doesn't break anything as we map offline
1481          * CPUs to first hardware queue. We will re-init queue below to get
1482          * optimal settings.
1483          */
1484         if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1485             action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1486                 return NOTIFY_OK;
1487
1488         mutex_lock(&all_q_mutex);
1489         list_for_each_entry(q, &all_q_list, all_q_node)
1490                 blk_mq_queue_reinit(q);
1491         mutex_unlock(&all_q_mutex);
1492         return NOTIFY_OK;
1493 }
1494
1495 static int __init blk_mq_init(void)
1496 {
1497         blk_mq_cpu_init();
1498
1499         /* Must be called after percpu_counter_hotcpu_callback() */
1500         hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1501
1502         return 0;
1503 }
1504 subsys_initcall(blk_mq_init);