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