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