]> Pileus Git - ~andy/linux/blob - block/blk-mq.c
blk-mq: dont assume rq->errors is set when returning an error from ->queue_rq
[~andy/linux] / 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 {
199         return blk_mq_alloc_rq(hctx, gfp, reserved);
200 }
201
202 static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
203                                                    int rw, gfp_t gfp,
204                                                    bool reserved)
205 {
206         struct request *rq;
207
208         do {
209                 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
210                 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
211
212                 rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved);
213                 if (rq) {
214                         blk_mq_rq_ctx_init(q, ctx, rq, rw);
215                         break;
216                 }
217
218                 blk_mq_put_ctx(ctx);
219                 if (!(gfp & __GFP_WAIT))
220                         break;
221
222                 __blk_mq_run_hw_queue(hctx);
223                 blk_mq_wait_for_tags(hctx->tags);
224         } while (1);
225
226         return rq;
227 }
228
229 struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
230 {
231         struct request *rq;
232
233         if (blk_mq_queue_enter(q))
234                 return NULL;
235
236         rq = blk_mq_alloc_request_pinned(q, rw, gfp, false);
237         if (rq)
238                 blk_mq_put_ctx(rq->mq_ctx);
239         return rq;
240 }
241
242 struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
243                                               gfp_t gfp)
244 {
245         struct request *rq;
246
247         if (blk_mq_queue_enter(q))
248                 return NULL;
249
250         rq = blk_mq_alloc_request_pinned(q, rw, gfp, true);
251         if (rq)
252                 blk_mq_put_ctx(rq->mq_ctx);
253         return rq;
254 }
255 EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
256
257 /*
258  * Re-init and set pdu, if we have it
259  */
260 void blk_mq_rq_init(struct blk_mq_hw_ctx *hctx, struct request *rq)
261 {
262         blk_rq_init(hctx->queue, rq);
263
264         if (hctx->cmd_size)
265                 rq->special = blk_mq_rq_to_pdu(rq);
266 }
267
268 static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
269                                   struct blk_mq_ctx *ctx, struct request *rq)
270 {
271         const int tag = rq->tag;
272         struct request_queue *q = rq->q;
273
274         blk_mq_rq_init(hctx, rq);
275         blk_mq_put_tag(hctx->tags, tag);
276
277         blk_mq_queue_exit(q);
278 }
279
280 void blk_mq_free_request(struct request *rq)
281 {
282         struct blk_mq_ctx *ctx = rq->mq_ctx;
283         struct blk_mq_hw_ctx *hctx;
284         struct request_queue *q = rq->q;
285
286         ctx->rq_completed[rq_is_sync(rq)]++;
287
288         hctx = q->mq_ops->map_queue(q, ctx->cpu);
289         __blk_mq_free_request(hctx, ctx, rq);
290 }
291
292 static void blk_mq_bio_endio(struct request *rq, struct bio *bio, int error)
293 {
294         if (error)
295                 clear_bit(BIO_UPTODATE, &bio->bi_flags);
296         else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
297                 error = -EIO;
298
299         if (unlikely(rq->cmd_flags & REQ_QUIET))
300                 set_bit(BIO_QUIET, &bio->bi_flags);
301
302         /* don't actually finish bio if it's part of flush sequence */
303         if (!(rq->cmd_flags & REQ_FLUSH_SEQ))
304                 bio_endio(bio, error);
305 }
306
307 void blk_mq_end_io(struct request *rq, int error)
308 {
309         struct bio *bio = rq->bio;
310         unsigned int bytes = 0;
311
312         trace_block_rq_complete(rq->q, rq);
313
314         while (bio) {
315                 struct bio *next = bio->bi_next;
316
317                 bio->bi_next = NULL;
318                 bytes += bio->bi_iter.bi_size;
319                 blk_mq_bio_endio(rq, bio, error);
320                 bio = next;
321         }
322
323         blk_account_io_completion(rq, bytes);
324
325         blk_account_io_done(rq);
326
327         if (rq->end_io)
328                 rq->end_io(rq, error);
329         else
330                 blk_mq_free_request(rq);
331 }
332 EXPORT_SYMBOL(blk_mq_end_io);
333
334 static void __blk_mq_complete_request_remote(void *data)
335 {
336         struct request *rq = data;
337
338         rq->q->softirq_done_fn(rq);
339 }
340
341 void __blk_mq_complete_request(struct request *rq)
342 {
343         struct blk_mq_ctx *ctx = rq->mq_ctx;
344         int cpu;
345
346         if (!ctx->ipi_redirect) {
347                 rq->q->softirq_done_fn(rq);
348                 return;
349         }
350
351         cpu = get_cpu();
352         if (cpu != ctx->cpu && cpu_online(ctx->cpu)) {
353                 rq->csd.func = __blk_mq_complete_request_remote;
354                 rq->csd.info = rq;
355                 rq->csd.flags = 0;
356                 __smp_call_function_single(ctx->cpu, &rq->csd, 0);
357         } else {
358                 rq->q->softirq_done_fn(rq);
359         }
360         put_cpu();
361 }
362
363 /**
364  * blk_mq_complete_request - end I/O on a request
365  * @rq:         the request being processed
366  *
367  * Description:
368  *      Ends all I/O on a request. It does not handle partial completions.
369  *      The actual completion happens out-of-order, through a IPI handler.
370  **/
371 void blk_mq_complete_request(struct request *rq)
372 {
373         if (unlikely(blk_should_fake_timeout(rq->q)))
374                 return;
375         if (!blk_mark_rq_complete(rq))
376                 __blk_mq_complete_request(rq);
377 }
378 EXPORT_SYMBOL(blk_mq_complete_request);
379
380 static void blk_mq_start_request(struct request *rq)
381 {
382         struct request_queue *q = rq->q;
383
384         trace_block_rq_issue(q, rq);
385
386         /*
387          * Just mark start time and set the started bit. Due to memory
388          * ordering, we know we'll see the correct deadline as long as
389          * REQ_ATOMIC_STARTED is seen.
390          */
391         rq->deadline = jiffies + q->rq_timeout;
392         set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
393 }
394
395 static void blk_mq_requeue_request(struct request *rq)
396 {
397         struct request_queue *q = rq->q;
398
399         trace_block_rq_requeue(q, rq);
400         clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
401 }
402
403 struct blk_mq_timeout_data {
404         struct blk_mq_hw_ctx *hctx;
405         unsigned long *next;
406         unsigned int *next_set;
407 };
408
409 static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
410 {
411         struct blk_mq_timeout_data *data = __data;
412         struct blk_mq_hw_ctx *hctx = data->hctx;
413         unsigned int tag;
414
415          /* It may not be in flight yet (this is where
416          * the REQ_ATOMIC_STARTED flag comes in). The requests are
417          * statically allocated, so we know it's always safe to access the
418          * memory associated with a bit offset into ->rqs[].
419          */
420         tag = 0;
421         do {
422                 struct request *rq;
423
424                 tag = find_next_zero_bit(free_tags, hctx->queue_depth, tag);
425                 if (tag >= hctx->queue_depth)
426                         break;
427
428                 rq = hctx->rqs[tag++];
429
430                 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
431                         continue;
432
433                 blk_rq_check_expired(rq, data->next, data->next_set);
434         } while (1);
435 }
436
437 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
438                                         unsigned long *next,
439                                         unsigned int *next_set)
440 {
441         struct blk_mq_timeout_data data = {
442                 .hctx           = hctx,
443                 .next           = next,
444                 .next_set       = next_set,
445         };
446
447         /*
448          * Ask the tagging code to iterate busy requests, so we can
449          * check them for timeout.
450          */
451         blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
452 }
453
454 static void blk_mq_rq_timer(unsigned long data)
455 {
456         struct request_queue *q = (struct request_queue *) data;
457         struct blk_mq_hw_ctx *hctx;
458         unsigned long next = 0;
459         int i, next_set = 0;
460
461         queue_for_each_hw_ctx(q, hctx, i)
462                 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
463
464         if (next_set)
465                 mod_timer(&q->timeout, round_jiffies_up(next));
466 }
467
468 /*
469  * Reverse check our software queue for entries that we could potentially
470  * merge with. Currently includes a hand-wavy stop count of 8, to not spend
471  * too much time checking for merges.
472  */
473 static bool blk_mq_attempt_merge(struct request_queue *q,
474                                  struct blk_mq_ctx *ctx, struct bio *bio)
475 {
476         struct request *rq;
477         int checked = 8;
478
479         list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
480                 int el_ret;
481
482                 if (!checked--)
483                         break;
484
485                 if (!blk_rq_merge_ok(rq, bio))
486                         continue;
487
488                 el_ret = blk_try_merge(rq, bio);
489                 if (el_ret == ELEVATOR_BACK_MERGE) {
490                         if (bio_attempt_back_merge(q, rq, bio)) {
491                                 ctx->rq_merged++;
492                                 return true;
493                         }
494                         break;
495                 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
496                         if (bio_attempt_front_merge(q, rq, bio)) {
497                                 ctx->rq_merged++;
498                                 return true;
499                         }
500                         break;
501                 }
502         }
503
504         return false;
505 }
506
507 void blk_mq_add_timer(struct request *rq)
508 {
509         __blk_add_timer(rq, NULL);
510 }
511
512 /*
513  * Run this hardware queue, pulling any software queues mapped to it in.
514  * Note that this function currently has various problems around ordering
515  * of IO. In particular, we'd like FIFO behaviour on handling existing
516  * items on the hctx->dispatch list. Ignore that for now.
517  */
518 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
519 {
520         struct request_queue *q = hctx->queue;
521         struct blk_mq_ctx *ctx;
522         struct request *rq;
523         LIST_HEAD(rq_list);
524         int bit, queued;
525
526         if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->flags)))
527                 return;
528
529         hctx->run++;
530
531         /*
532          * Touch any software queue that has pending entries.
533          */
534         for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
535                 clear_bit(bit, hctx->ctx_map);
536                 ctx = hctx->ctxs[bit];
537                 BUG_ON(bit != ctx->index_hw);
538
539                 spin_lock(&ctx->lock);
540                 list_splice_tail_init(&ctx->rq_list, &rq_list);
541                 spin_unlock(&ctx->lock);
542         }
543
544         /*
545          * If we have previous entries on our dispatch list, grab them
546          * and stuff them at the front for more fair dispatch.
547          */
548         if (!list_empty_careful(&hctx->dispatch)) {
549                 spin_lock(&hctx->lock);
550                 if (!list_empty(&hctx->dispatch))
551                         list_splice_init(&hctx->dispatch, &rq_list);
552                 spin_unlock(&hctx->lock);
553         }
554
555         /*
556          * Delete and return all entries from our dispatch list
557          */
558         queued = 0;
559
560         /*
561          * Now process all the entries, sending them to the driver.
562          */
563         while (!list_empty(&rq_list)) {
564                 int ret;
565
566                 rq = list_first_entry(&rq_list, struct request, queuelist);
567                 list_del_init(&rq->queuelist);
568                 blk_mq_start_request(rq);
569
570                 if (q->dma_drain_size && blk_rq_bytes(rq)) {
571                         /*
572                          * make sure space for the drain appears we
573                          * know we can do this because max_hw_segments
574                          * has been adjusted to be one fewer than the
575                          * device can handle
576                          */
577                         rq->nr_phys_segments++;
578                 }
579
580                 /*
581                  * Last request in the series. Flag it as such, this
582                  * enables drivers to know when IO should be kicked off,
583                  * if they don't do it on a per-request basis.
584                  *
585                  * Note: the flag isn't the only condition drivers
586                  * should do kick off. If drive is busy, the last
587                  * request might not have the bit set.
588                  */
589                 if (list_empty(&rq_list))
590                         rq->cmd_flags |= REQ_END;
591
592                 ret = q->mq_ops->queue_rq(hctx, rq);
593                 switch (ret) {
594                 case BLK_MQ_RQ_QUEUE_OK:
595                         queued++;
596                         continue;
597                 case BLK_MQ_RQ_QUEUE_BUSY:
598                         /*
599                          * FIXME: we should have a mechanism to stop the queue
600                          * like blk_stop_queue, otherwise we will waste cpu
601                          * time
602                          */
603                         list_add(&rq->queuelist, &rq_list);
604                         blk_mq_requeue_request(rq);
605                         break;
606                 default:
607                         pr_err("blk-mq: bad return on queue: %d\n", ret);
608                 case BLK_MQ_RQ_QUEUE_ERROR:
609                         rq->errors = -EIO;
610                         blk_mq_end_io(rq, rq->errors);
611                         break;
612                 }
613
614                 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
615                         break;
616         }
617
618         if (!queued)
619                 hctx->dispatched[0]++;
620         else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
621                 hctx->dispatched[ilog2(queued) + 1]++;
622
623         /*
624          * Any items that need requeuing? Stuff them into hctx->dispatch,
625          * that is where we will continue on next queue run.
626          */
627         if (!list_empty(&rq_list)) {
628                 spin_lock(&hctx->lock);
629                 list_splice(&rq_list, &hctx->dispatch);
630                 spin_unlock(&hctx->lock);
631         }
632 }
633
634 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
635 {
636         if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->flags)))
637                 return;
638
639         if (!async)
640                 __blk_mq_run_hw_queue(hctx);
641         else {
642                 struct request_queue *q = hctx->queue;
643
644                 kblockd_schedule_delayed_work(q, &hctx->delayed_work, 0);
645         }
646 }
647
648 void blk_mq_run_queues(struct request_queue *q, bool async)
649 {
650         struct blk_mq_hw_ctx *hctx;
651         int i;
652
653         queue_for_each_hw_ctx(q, hctx, i) {
654                 if ((!blk_mq_hctx_has_pending(hctx) &&
655                     list_empty_careful(&hctx->dispatch)) ||
656                     test_bit(BLK_MQ_S_STOPPED, &hctx->flags))
657                         continue;
658
659                 blk_mq_run_hw_queue(hctx, async);
660         }
661 }
662 EXPORT_SYMBOL(blk_mq_run_queues);
663
664 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
665 {
666         cancel_delayed_work(&hctx->delayed_work);
667         set_bit(BLK_MQ_S_STOPPED, &hctx->state);
668 }
669 EXPORT_SYMBOL(blk_mq_stop_hw_queue);
670
671 void blk_mq_stop_hw_queues(struct request_queue *q)
672 {
673         struct blk_mq_hw_ctx *hctx;
674         int i;
675
676         queue_for_each_hw_ctx(q, hctx, i)
677                 blk_mq_stop_hw_queue(hctx);
678 }
679 EXPORT_SYMBOL(blk_mq_stop_hw_queues);
680
681 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
682 {
683         clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
684         __blk_mq_run_hw_queue(hctx);
685 }
686 EXPORT_SYMBOL(blk_mq_start_hw_queue);
687
688 void blk_mq_start_stopped_hw_queues(struct request_queue *q)
689 {
690         struct blk_mq_hw_ctx *hctx;
691         int i;
692
693         queue_for_each_hw_ctx(q, hctx, i) {
694                 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
695                         continue;
696
697                 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
698                 blk_mq_run_hw_queue(hctx, true);
699         }
700 }
701 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
702
703 static void blk_mq_work_fn(struct work_struct *work)
704 {
705         struct blk_mq_hw_ctx *hctx;
706
707         hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work);
708         __blk_mq_run_hw_queue(hctx);
709 }
710
711 static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
712                                     struct request *rq, bool at_head)
713 {
714         struct blk_mq_ctx *ctx = rq->mq_ctx;
715
716         trace_block_rq_insert(hctx->queue, rq);
717
718         if (at_head)
719                 list_add(&rq->queuelist, &ctx->rq_list);
720         else
721                 list_add_tail(&rq->queuelist, &ctx->rq_list);
722         blk_mq_hctx_mark_pending(hctx, ctx);
723
724         /*
725          * We do this early, to ensure we are on the right CPU.
726          */
727         blk_mq_add_timer(rq);
728 }
729
730 void blk_mq_insert_request(struct request_queue *q, struct request *rq,
731                            bool at_head, bool run_queue)
732 {
733         struct blk_mq_hw_ctx *hctx;
734         struct blk_mq_ctx *ctx, *current_ctx;
735
736         ctx = rq->mq_ctx;
737         hctx = q->mq_ops->map_queue(q, ctx->cpu);
738
739         if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
740                 blk_insert_flush(rq);
741         } else {
742                 current_ctx = blk_mq_get_ctx(q);
743
744                 if (!cpu_online(ctx->cpu)) {
745                         ctx = current_ctx;
746                         hctx = q->mq_ops->map_queue(q, ctx->cpu);
747                         rq->mq_ctx = ctx;
748                 }
749                 spin_lock(&ctx->lock);
750                 __blk_mq_insert_request(hctx, rq, at_head);
751                 spin_unlock(&ctx->lock);
752
753                 blk_mq_put_ctx(current_ctx);
754         }
755
756         if (run_queue)
757                 __blk_mq_run_hw_queue(hctx);
758 }
759 EXPORT_SYMBOL(blk_mq_insert_request);
760
761 /*
762  * This is a special version of blk_mq_insert_request to bypass FLUSH request
763  * check. Should only be used internally.
764  */
765 void blk_mq_run_request(struct request *rq, bool run_queue, bool async)
766 {
767         struct request_queue *q = rq->q;
768         struct blk_mq_hw_ctx *hctx;
769         struct blk_mq_ctx *ctx, *current_ctx;
770
771         current_ctx = blk_mq_get_ctx(q);
772
773         ctx = rq->mq_ctx;
774         if (!cpu_online(ctx->cpu)) {
775                 ctx = current_ctx;
776                 rq->mq_ctx = ctx;
777         }
778         hctx = q->mq_ops->map_queue(q, ctx->cpu);
779
780         /* ctx->cpu might be offline */
781         spin_lock(&ctx->lock);
782         __blk_mq_insert_request(hctx, rq, false);
783         spin_unlock(&ctx->lock);
784
785         blk_mq_put_ctx(current_ctx);
786
787         if (run_queue)
788                 blk_mq_run_hw_queue(hctx, async);
789 }
790
791 static void blk_mq_insert_requests(struct request_queue *q,
792                                      struct blk_mq_ctx *ctx,
793                                      struct list_head *list,
794                                      int depth,
795                                      bool from_schedule)
796
797 {
798         struct blk_mq_hw_ctx *hctx;
799         struct blk_mq_ctx *current_ctx;
800
801         trace_block_unplug(q, depth, !from_schedule);
802
803         current_ctx = blk_mq_get_ctx(q);
804
805         if (!cpu_online(ctx->cpu))
806                 ctx = current_ctx;
807         hctx = q->mq_ops->map_queue(q, ctx->cpu);
808
809         /*
810          * preemption doesn't flush plug list, so it's possible ctx->cpu is
811          * offline now
812          */
813         spin_lock(&ctx->lock);
814         while (!list_empty(list)) {
815                 struct request *rq;
816
817                 rq = list_first_entry(list, struct request, queuelist);
818                 list_del_init(&rq->queuelist);
819                 rq->mq_ctx = ctx;
820                 __blk_mq_insert_request(hctx, rq, false);
821         }
822         spin_unlock(&ctx->lock);
823
824         blk_mq_put_ctx(current_ctx);
825
826         blk_mq_run_hw_queue(hctx, from_schedule);
827 }
828
829 static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
830 {
831         struct request *rqa = container_of(a, struct request, queuelist);
832         struct request *rqb = container_of(b, struct request, queuelist);
833
834         return !(rqa->mq_ctx < rqb->mq_ctx ||
835                  (rqa->mq_ctx == rqb->mq_ctx &&
836                   blk_rq_pos(rqa) < blk_rq_pos(rqb)));
837 }
838
839 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
840 {
841         struct blk_mq_ctx *this_ctx;
842         struct request_queue *this_q;
843         struct request *rq;
844         LIST_HEAD(list);
845         LIST_HEAD(ctx_list);
846         unsigned int depth;
847
848         list_splice_init(&plug->mq_list, &list);
849
850         list_sort(NULL, &list, plug_ctx_cmp);
851
852         this_q = NULL;
853         this_ctx = NULL;
854         depth = 0;
855
856         while (!list_empty(&list)) {
857                 rq = list_entry_rq(list.next);
858                 list_del_init(&rq->queuelist);
859                 BUG_ON(!rq->q);
860                 if (rq->mq_ctx != this_ctx) {
861                         if (this_ctx) {
862                                 blk_mq_insert_requests(this_q, this_ctx,
863                                                         &ctx_list, depth,
864                                                         from_schedule);
865                         }
866
867                         this_ctx = rq->mq_ctx;
868                         this_q = rq->q;
869                         depth = 0;
870                 }
871
872                 depth++;
873                 list_add_tail(&rq->queuelist, &ctx_list);
874         }
875
876         /*
877          * If 'this_ctx' is set, we know we have entries to complete
878          * on 'ctx_list'. Do those.
879          */
880         if (this_ctx) {
881                 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
882                                        from_schedule);
883         }
884 }
885
886 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
887 {
888         init_request_from_bio(rq, bio);
889         blk_account_io_start(rq, 1);
890 }
891
892 static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
893 {
894         struct blk_mq_hw_ctx *hctx;
895         struct blk_mq_ctx *ctx;
896         const int is_sync = rw_is_sync(bio->bi_rw);
897         const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
898         int rw = bio_data_dir(bio);
899         struct request *rq;
900         unsigned int use_plug, request_count = 0;
901
902         /*
903          * If we have multiple hardware queues, just go directly to
904          * one of those for sync IO.
905          */
906         use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
907
908         blk_queue_bounce(q, &bio);
909
910         if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
911                 bio_endio(bio, -EIO);
912                 return;
913         }
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);
928         if (likely(rq))
929                 blk_mq_rq_ctx_init(q, ctx, rq, rw);
930         else {
931                 blk_mq_put_ctx(ctx);
932                 trace_block_sleeprq(q, bio, rw);
933                 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
934                                                         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->cmd_size = reg->cmd_size;
1212
1213                 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1214                                                 blk_mq_hctx_notify, hctx);
1215                 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1216
1217                 if (blk_mq_init_rq_map(hctx, reg->reserved_tags, node))
1218                         break;
1219
1220                 /*
1221                  * Allocate space for all possible cpus to avoid allocation in
1222                  * runtime
1223                  */
1224                 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1225                                                 GFP_KERNEL, node);
1226                 if (!hctx->ctxs)
1227                         break;
1228
1229                 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1230                 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1231                                                 GFP_KERNEL, node);
1232                 if (!hctx->ctx_map)
1233                         break;
1234
1235                 hctx->nr_ctx_map = num_maps;
1236                 hctx->nr_ctx = 0;
1237
1238                 if (reg->ops->init_hctx &&
1239                     reg->ops->init_hctx(hctx, driver_data, i))
1240                         break;
1241         }
1242
1243         if (i == q->nr_hw_queues)
1244                 return 0;
1245
1246         /*
1247          * Init failed
1248          */
1249         queue_for_each_hw_ctx(q, hctx, j) {
1250                 if (i == j)
1251                         break;
1252
1253                 if (reg->ops->exit_hctx)
1254                         reg->ops->exit_hctx(hctx, j);
1255
1256                 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1257                 blk_mq_free_rq_map(hctx);
1258                 kfree(hctx->ctxs);
1259         }
1260
1261         return 1;
1262 }
1263
1264 static void blk_mq_init_cpu_queues(struct request_queue *q,
1265                                    unsigned int nr_hw_queues)
1266 {
1267         unsigned int i;
1268
1269         for_each_possible_cpu(i) {
1270                 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1271                 struct blk_mq_hw_ctx *hctx;
1272
1273                 memset(__ctx, 0, sizeof(*__ctx));
1274                 __ctx->cpu = i;
1275                 spin_lock_init(&__ctx->lock);
1276                 INIT_LIST_HEAD(&__ctx->rq_list);
1277                 __ctx->queue = q;
1278
1279                 /* If the cpu isn't online, the cpu is mapped to first hctx */
1280                 hctx = q->mq_ops->map_queue(q, i);
1281                 hctx->nr_ctx++;
1282
1283                 if (!cpu_online(i))
1284                         continue;
1285
1286                 /*
1287                  * Set local node, IFF we have more than one hw queue. If
1288                  * not, we remain on the home node of the device
1289                  */
1290                 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1291                         hctx->numa_node = cpu_to_node(i);
1292         }
1293 }
1294
1295 static void blk_mq_map_swqueue(struct request_queue *q)
1296 {
1297         unsigned int i;
1298         struct blk_mq_hw_ctx *hctx;
1299         struct blk_mq_ctx *ctx;
1300
1301         queue_for_each_hw_ctx(q, hctx, i) {
1302                 hctx->nr_ctx = 0;
1303         }
1304
1305         /*
1306          * Map software to hardware queues
1307          */
1308         queue_for_each_ctx(q, ctx, i) {
1309                 /* If the cpu isn't online, the cpu is mapped to first hctx */
1310                 hctx = q->mq_ops->map_queue(q, i);
1311                 ctx->index_hw = hctx->nr_ctx;
1312                 hctx->ctxs[hctx->nr_ctx++] = ctx;
1313         }
1314 }
1315
1316 struct request_queue *blk_mq_init_queue(struct blk_mq_reg *reg,
1317                                         void *driver_data)
1318 {
1319         struct blk_mq_hw_ctx **hctxs;
1320         struct blk_mq_ctx *ctx;
1321         struct request_queue *q;
1322         int i;
1323
1324         if (!reg->nr_hw_queues ||
1325             !reg->ops->queue_rq || !reg->ops->map_queue ||
1326             !reg->ops->alloc_hctx || !reg->ops->free_hctx)
1327                 return ERR_PTR(-EINVAL);
1328
1329         if (!reg->queue_depth)
1330                 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1331         else if (reg->queue_depth > BLK_MQ_MAX_DEPTH) {
1332                 pr_err("blk-mq: queuedepth too large (%u)\n", reg->queue_depth);
1333                 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1334         }
1335
1336         if (reg->queue_depth < (reg->reserved_tags + BLK_MQ_TAG_MIN))
1337                 return ERR_PTR(-EINVAL);
1338
1339         ctx = alloc_percpu(struct blk_mq_ctx);
1340         if (!ctx)
1341                 return ERR_PTR(-ENOMEM);
1342
1343         hctxs = kmalloc_node(reg->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1344                         reg->numa_node);
1345
1346         if (!hctxs)
1347                 goto err_percpu;
1348
1349         for (i = 0; i < reg->nr_hw_queues; i++) {
1350                 hctxs[i] = reg->ops->alloc_hctx(reg, i);
1351                 if (!hctxs[i])
1352                         goto err_hctxs;
1353
1354                 hctxs[i]->numa_node = NUMA_NO_NODE;
1355                 hctxs[i]->queue_num = i;
1356         }
1357
1358         q = blk_alloc_queue_node(GFP_KERNEL, reg->numa_node);
1359         if (!q)
1360                 goto err_hctxs;
1361
1362         q->mq_map = blk_mq_make_queue_map(reg);
1363         if (!q->mq_map)
1364                 goto err_map;
1365
1366         setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1367         blk_queue_rq_timeout(q, 30000);
1368
1369         q->nr_queues = nr_cpu_ids;
1370         q->nr_hw_queues = reg->nr_hw_queues;
1371
1372         q->queue_ctx = ctx;
1373         q->queue_hw_ctx = hctxs;
1374
1375         q->mq_ops = reg->ops;
1376         q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
1377
1378         q->sg_reserved_size = INT_MAX;
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         if (reg->ops->complete)
1386                 blk_queue_softirq_done(q, reg->ops->complete);
1387
1388         blk_mq_init_flush(q);
1389         blk_mq_init_cpu_queues(q, reg->nr_hw_queues);
1390
1391         q->flush_rq = kzalloc(round_up(sizeof(struct request) + reg->cmd_size,
1392                                 cache_line_size()), GFP_KERNEL);
1393         if (!q->flush_rq)
1394                 goto err_hw;
1395
1396         if (blk_mq_init_hw_queues(q, reg, driver_data))
1397                 goto err_flush_rq;
1398
1399         blk_mq_map_swqueue(q);
1400
1401         mutex_lock(&all_q_mutex);
1402         list_add_tail(&q->all_q_node, &all_q_list);
1403         mutex_unlock(&all_q_mutex);
1404
1405         return q;
1406
1407 err_flush_rq:
1408         kfree(q->flush_rq);
1409 err_hw:
1410         kfree(q->mq_map);
1411 err_map:
1412         blk_cleanup_queue(q);
1413 err_hctxs:
1414         for (i = 0; i < reg->nr_hw_queues; i++) {
1415                 if (!hctxs[i])
1416                         break;
1417                 reg->ops->free_hctx(hctxs[i], i);
1418         }
1419         kfree(hctxs);
1420 err_percpu:
1421         free_percpu(ctx);
1422         return ERR_PTR(-ENOMEM);
1423 }
1424 EXPORT_SYMBOL(blk_mq_init_queue);
1425
1426 void blk_mq_free_queue(struct request_queue *q)
1427 {
1428         struct blk_mq_hw_ctx *hctx;
1429         int i;
1430
1431         queue_for_each_hw_ctx(q, hctx, i) {
1432                 kfree(hctx->ctx_map);
1433                 kfree(hctx->ctxs);
1434                 blk_mq_free_rq_map(hctx);
1435                 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1436                 if (q->mq_ops->exit_hctx)
1437                         q->mq_ops->exit_hctx(hctx, i);
1438                 q->mq_ops->free_hctx(hctx, i);
1439         }
1440
1441         free_percpu(q->queue_ctx);
1442         kfree(q->queue_hw_ctx);
1443         kfree(q->mq_map);
1444
1445         q->queue_ctx = NULL;
1446         q->queue_hw_ctx = NULL;
1447         q->mq_map = NULL;
1448
1449         mutex_lock(&all_q_mutex);
1450         list_del_init(&q->all_q_node);
1451         mutex_unlock(&all_q_mutex);
1452 }
1453
1454 /* Basically redo blk_mq_init_queue with queue frozen */
1455 static void blk_mq_queue_reinit(struct request_queue *q)
1456 {
1457         blk_mq_freeze_queue(q);
1458
1459         blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1460
1461         /*
1462          * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1463          * we should change hctx numa_node according to new topology (this
1464          * involves free and re-allocate memory, worthy doing?)
1465          */
1466
1467         blk_mq_map_swqueue(q);
1468
1469         blk_mq_unfreeze_queue(q);
1470 }
1471
1472 static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1473                                       unsigned long action, void *hcpu)
1474 {
1475         struct request_queue *q;
1476
1477         /*
1478          * Before new mapping is established, hotadded cpu might already start
1479          * handling requests. This doesn't break anything as we map offline
1480          * CPUs to first hardware queue. We will re-init queue below to get
1481          * optimal settings.
1482          */
1483         if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1484             action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1485                 return NOTIFY_OK;
1486
1487         mutex_lock(&all_q_mutex);
1488         list_for_each_entry(q, &all_q_list, all_q_node)
1489                 blk_mq_queue_reinit(q);
1490         mutex_unlock(&all_q_mutex);
1491         return NOTIFY_OK;
1492 }
1493
1494 static int __init blk_mq_init(void)
1495 {
1496         blk_mq_cpu_init();
1497
1498         /* Must be called after percpu_counter_hotcpu_callback() */
1499         hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1500
1501         return 0;
1502 }
1503 subsys_initcall(blk_mq_init);