]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blobdiff - block/blk-mq.c
blk-mq: remove alloc_hctx and free_hctx methods
[mirror_ubuntu-artful-kernel.git] / block / blk-mq.c
index da3808823e44ec76a98203517bede8f3e58ef611..f27fe44230c2f9555a7fc7e1c567663961d8212a 100644 (file)
@@ -1,3 +1,9 @@
+/*
+ * Block multiqueue core code
+ *
+ * Copyright (C) 2013-2014 Jens Axboe
+ * Copyright (C) 2013-2014 Christoph Hellwig
+ */
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/backing-dev.h>
@@ -56,39 +62,40 @@ static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
 {
        unsigned int i;
 
-       for (i = 0; i < hctx->nr_ctx_map; i++)
-               if (hctx->ctx_map[i])
+       for (i = 0; i < hctx->ctx_map.map_size; i++)
+               if (hctx->ctx_map.map[i].word)
                        return true;
 
        return false;
 }
 
+static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
+                                             struct blk_mq_ctx *ctx)
+{
+       return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
+}
+
+#define CTX_TO_BIT(hctx, ctx)  \
+       ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
+
 /*
  * Mark this ctx as having pending work in this hardware queue
  */
 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
                                     struct blk_mq_ctx *ctx)
 {
-       if (!test_bit(ctx->index_hw, hctx->ctx_map))
-               set_bit(ctx->index_hw, hctx->ctx_map);
+       struct blk_align_bitmap *bm = get_bm(hctx, ctx);
+
+       if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
+               set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
 }
 
-static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,
-                                             gfp_t gfp, bool reserved)
+static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
+                                     struct blk_mq_ctx *ctx)
 {
-       struct request *rq;
-       unsigned int tag;
-
-       tag = blk_mq_get_tag(hctx->tags, gfp, reserved);
-       if (tag != BLK_MQ_TAG_FAIL) {
-               rq = hctx->tags->rqs[tag];
-               blk_rq_init(hctx->queue, rq);
-               rq->tag = tag;
-
-               return rq;
-       }
+       struct blk_align_bitmap *bm = get_bm(hctx, ctx);
 
-       return NULL;
+       clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
 }
 
 static int blk_mq_queue_enter(struct request_queue *q)
@@ -187,70 +194,109 @@ static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
        if (blk_queue_io_stat(q))
                rw_flags |= REQ_IO_STAT;
 
+       INIT_LIST_HEAD(&rq->queuelist);
+       /* csd/requeue_work/fifo_time is initialized before use */
+       rq->q = q;
        rq->mq_ctx = ctx;
-       rq->cmd_flags = rw_flags;
+       rq->cmd_flags |= rw_flags;
+       rq->cmd_type = 0;
+       /* do not touch atomic flags, it needs atomic ops against the timer */
+       rq->cpu = -1;
+       rq->__data_len = 0;
+       rq->__sector = (sector_t) -1;
+       rq->bio = NULL;
+       rq->biotail = NULL;
+       INIT_HLIST_NODE(&rq->hash);
+       RB_CLEAR_NODE(&rq->rb_node);
+       memset(&rq->flush, 0, max(sizeof(rq->flush), sizeof(rq->elv)));
+       rq->rq_disk = NULL;
+       rq->part = NULL;
        rq->start_time = jiffies;
+#ifdef CONFIG_BLK_CGROUP
+       rq->rl = NULL;
        set_start_time_ns(rq);
+       rq->io_start_time_ns = 0;
+#endif
+       rq->nr_phys_segments = 0;
+#if defined(CONFIG_BLK_DEV_INTEGRITY)
+       rq->nr_integrity_segments = 0;
+#endif
+       rq->ioprio = 0;
+       rq->special = NULL;
+       /* tag was already set */
+       rq->errors = 0;
+       memset(rq->__cmd, 0, sizeof(rq->__cmd));
+       rq->cmd = rq->__cmd;
+       rq->cmd_len = BLK_MAX_CDB;
+
+       rq->extra_len = 0;
+       rq->sense_len = 0;
+       rq->resid_len = 0;
+       rq->sense = NULL;
+
+       rq->deadline = 0;
+       INIT_LIST_HEAD(&rq->timeout_list);
+       rq->timeout = 0;
+       rq->retries = 0;
+       rq->end_io = NULL;
+       rq->end_io_data = NULL;
+       rq->next_rq = NULL;
+
        ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
 }
 
-static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
-                                                  int rw, gfp_t gfp,
-                                                  bool reserved)
+static struct request *
+__blk_mq_alloc_request(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
+               struct blk_mq_ctx *ctx, int rw, gfp_t gfp, bool reserved)
 {
        struct request *rq;
+       unsigned int tag;
 
-       do {
-               struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
-               struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
-
-               rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved);
-               if (rq) {
-                       blk_mq_rq_ctx_init(q, ctx, rq, rw);
-                       break;
-               }
+       tag = blk_mq_get_tag(hctx, &ctx->last_tag, gfp, reserved);
+       if (tag != BLK_MQ_TAG_FAIL) {
+               rq = hctx->tags->rqs[tag];
 
-               if (gfp & __GFP_WAIT) {
-                       __blk_mq_run_hw_queue(hctx);
-                       blk_mq_put_ctx(ctx);
-               } else {
-                       blk_mq_put_ctx(ctx);
-                       break;
+               rq->cmd_flags = 0;
+               if (blk_mq_tag_busy(hctx)) {
+                       rq->cmd_flags = REQ_MQ_INFLIGHT;
+                       atomic_inc(&hctx->nr_active);
                }
 
-               blk_mq_wait_for_tags(hctx->tags);
-       } while (1);
+               rq->tag = tag;
+               blk_mq_rq_ctx_init(q, ctx, rq, rw);
+               return rq;
+       }
 
-       return rq;
+       return NULL;
 }
 
-struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
+struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
+               bool reserved)
 {
+       struct blk_mq_ctx *ctx;
+       struct blk_mq_hw_ctx *hctx;
        struct request *rq;
 
        if (blk_mq_queue_enter(q))
                return NULL;
 
-       rq = blk_mq_alloc_request_pinned(q, rw, gfp, false);
-       if (rq)
-               blk_mq_put_ctx(rq->mq_ctx);
-       return rq;
-}
-
-struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
-                                             gfp_t gfp)
-{
-       struct request *rq;
+       ctx = blk_mq_get_ctx(q);
+       hctx = q->mq_ops->map_queue(q, ctx->cpu);
 
-       if (blk_mq_queue_enter(q))
-               return NULL;
+       rq = __blk_mq_alloc_request(q, hctx, ctx, rw, gfp & ~__GFP_WAIT,
+                                   reserved);
+       if (!rq && (gfp & __GFP_WAIT)) {
+               __blk_mq_run_hw_queue(hctx);
+               blk_mq_put_ctx(ctx);
 
-       rq = blk_mq_alloc_request_pinned(q, rw, gfp, true);
-       if (rq)
-               blk_mq_put_ctx(rq->mq_ctx);
+               ctx = blk_mq_get_ctx(q);
+               hctx = q->mq_ops->map_queue(q, ctx->cpu);
+               rq =  __blk_mq_alloc_request(q, hctx, ctx, rw, gfp, reserved);
+       }
+       blk_mq_put_ctx(ctx);
        return rq;
 }
-EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
+EXPORT_SYMBOL(blk_mq_alloc_request);
 
 static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
                                  struct blk_mq_ctx *ctx, struct request *rq)
@@ -258,7 +304,11 @@ static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
        const int tag = rq->tag;
        struct request_queue *q = rq->q;
 
-       blk_mq_put_tag(hctx->tags, tag);
+       if (rq->cmd_flags & REQ_MQ_INFLIGHT)
+               atomic_dec(&hctx->nr_active);
+
+       clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
+       blk_mq_put_tag(hctx, tag, &ctx->last_tag);
        blk_mq_queue_exit(q);
 }
 
@@ -326,15 +376,19 @@ static void __blk_mq_complete_request_remote(void *data)
 void __blk_mq_complete_request(struct request *rq)
 {
        struct blk_mq_ctx *ctx = rq->mq_ctx;
+       bool shared = false;
        int cpu;
 
-       if (!ctx->ipi_redirect) {
+       if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
                rq->q->softirq_done_fn(rq);
                return;
        }
 
        cpu = get_cpu();
-       if (cpu != ctx->cpu && cpu_online(ctx->cpu)) {
+       if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
+               shared = cpus_share_cache(cpu, ctx->cpu);
+
+       if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
                rq->csd.func = __blk_mq_complete_request_remote;
                rq->csd.info = rq;
                rq->csd.flags = 0;
@@ -355,10 +409,16 @@ void __blk_mq_complete_request(struct request *rq)
  **/
 void blk_mq_complete_request(struct request *rq)
 {
-       if (unlikely(blk_should_fake_timeout(rq->q)))
+       struct request_queue *q = rq->q;
+
+       if (unlikely(blk_should_fake_timeout(q)))
                return;
-       if (!blk_mark_rq_complete(rq))
-               __blk_mq_complete_request(rq);
+       if (!blk_mark_rq_complete(rq)) {
+               if (q->softirq_done_fn)
+                       __blk_mq_complete_request(rq);
+               else
+                       blk_mq_end_io(rq, rq->errors);
+       }
 }
 EXPORT_SYMBOL(blk_mq_complete_request);
 
@@ -375,10 +435,22 @@ static void blk_mq_start_request(struct request *rq, bool last)
        /*
         * Just mark start time and set the started bit. Due to memory
         * ordering, we know we'll see the correct deadline as long as
-        * REQ_ATOMIC_STARTED is seen.
+        * REQ_ATOMIC_STARTED is seen. Use the default queue timeout,
+        * unless one has been set in the request.
+        */
+       if (!rq->timeout)
+               rq->deadline = jiffies + q->rq_timeout;
+       else
+               rq->deadline = jiffies + rq->timeout;
+
+       /*
+        * Mark us as started and clear complete. Complete might have been
+        * set if requeue raced with timeout, which then marked it as
+        * complete. So be sure to clear complete again when we start
+        * the request, otherwise we'll ignore the completion event.
         */
-       rq->deadline = jiffies + q->rq_timeout;
        set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
+       clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
 
        if (q->dma_drain_size && blk_rq_bytes(rq)) {
                /*
@@ -400,7 +472,7 @@ static void blk_mq_start_request(struct request *rq, bool last)
                rq->cmd_flags |= REQ_END;
 }
 
-static void blk_mq_requeue_request(struct request *rq)
+static void __blk_mq_requeue_request(struct request *rq)
 {
        struct request_queue *q = rq->q;
 
@@ -413,6 +485,74 @@ static void blk_mq_requeue_request(struct request *rq)
                rq->nr_phys_segments--;
 }
 
+void blk_mq_requeue_request(struct request *rq)
+{
+       __blk_mq_requeue_request(rq);
+       blk_clear_rq_complete(rq);
+
+       BUG_ON(blk_queued_rq(rq));
+       blk_mq_add_to_requeue_list(rq, true);
+}
+EXPORT_SYMBOL(blk_mq_requeue_request);
+
+static void blk_mq_requeue_work(struct work_struct *work)
+{
+       struct request_queue *q =
+               container_of(work, struct request_queue, requeue_work);
+       LIST_HEAD(rq_list);
+       struct request *rq, *next;
+       unsigned long flags;
+
+       spin_lock_irqsave(&q->requeue_lock, flags);
+       list_splice_init(&q->requeue_list, &rq_list);
+       spin_unlock_irqrestore(&q->requeue_lock, flags);
+
+       list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
+               if (!(rq->cmd_flags & REQ_SOFTBARRIER))
+                       continue;
+
+               rq->cmd_flags &= ~REQ_SOFTBARRIER;
+               list_del_init(&rq->queuelist);
+               blk_mq_insert_request(rq, true, false, false);
+       }
+
+       while (!list_empty(&rq_list)) {
+               rq = list_entry(rq_list.next, struct request, queuelist);
+               list_del_init(&rq->queuelist);
+               blk_mq_insert_request(rq, false, false, false);
+       }
+
+       blk_mq_run_queues(q, false);
+}
+
+void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
+{
+       struct request_queue *q = rq->q;
+       unsigned long flags;
+
+       /*
+        * We abuse this flag that is otherwise used by the I/O scheduler to
+        * request head insertation from the workqueue.
+        */
+       BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
+
+       spin_lock_irqsave(&q->requeue_lock, flags);
+       if (at_head) {
+               rq->cmd_flags |= REQ_SOFTBARRIER;
+               list_add(&rq->queuelist, &q->requeue_list);
+       } else {
+               list_add_tail(&rq->queuelist, &q->requeue_list);
+       }
+       spin_unlock_irqrestore(&q->requeue_lock, flags);
+}
+EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
+
+void blk_mq_kick_requeue_list(struct request_queue *q)
+{
+       kblockd_schedule_work(&q->requeue_work);
+}
+EXPORT_SYMBOL(blk_mq_kick_requeue_list);
+
 struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
 {
        return tags->rqs[tag];
@@ -471,6 +611,28 @@ static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
        blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
 }
 
+static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
+{
+       struct request_queue *q = rq->q;
+
+       /*
+        * We know that complete is set at this point. If STARTED isn't set
+        * anymore, then the request isn't active and the "timeout" should
+        * just be ignored. This can happen due to the bitflag ordering.
+        * Timeout first checks if STARTED is set, and if it is, assumes
+        * the request is active. But if we race with completion, then
+        * we both flags will get cleared. So check here again, and ignore
+        * a timeout event with a request that isn't active.
+        */
+       if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
+               return BLK_EH_NOT_HANDLED;
+
+       if (!q->mq_ops->timeout)
+               return BLK_EH_RESET_TIMER;
+
+       return q->mq_ops->timeout(rq);
+}
+
 static void blk_mq_rq_timer(unsigned long data)
 {
        struct request_queue *q = (struct request_queue *) data;
@@ -478,11 +640,24 @@ static void blk_mq_rq_timer(unsigned long data)
        unsigned long next = 0;
        int i, next_set = 0;
 
-       queue_for_each_hw_ctx(q, hctx, i)
+       queue_for_each_hw_ctx(q, hctx, i) {
+               /*
+                * If not software queues are currently mapped to this
+                * hardware queue, there's nothing to check
+                */
+               if (!hctx->nr_ctx || !hctx->tags)
+                       continue;
+
                blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
+       }
 
-       if (next_set)
-               mod_timer(&q->timeout, round_jiffies_up(next));
+       if (next_set) {
+               next = blk_rq_timeout(round_jiffies_up(next));
+               mod_timer(&q->timeout, next);
+       } else {
+               queue_for_each_hw_ctx(q, hctx, i)
+                       blk_mq_tag_idle(hctx);
+       }
 }
 
 /*
@@ -524,9 +699,38 @@ static bool blk_mq_attempt_merge(struct request_queue *q,
        return false;
 }
 
-void blk_mq_add_timer(struct request *rq)
+/*
+ * Process software queues that have been marked busy, splicing them
+ * to the for-dispatch
+ */
+static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
 {
-       __blk_add_timer(rq, NULL);
+       struct blk_mq_ctx *ctx;
+       int i;
+
+       for (i = 0; i < hctx->ctx_map.map_size; i++) {
+               struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
+               unsigned int off, bit;
+
+               if (!bm->word)
+                       continue;
+
+               bit = 0;
+               off = i * hctx->ctx_map.bits_per_word;
+               do {
+                       bit = find_next_bit(&bm->word, bm->depth, bit);
+                       if (bit >= bm->depth)
+                               break;
+
+                       ctx = hctx->ctxs[bit + off];
+                       clear_bit(bit, &bm->word);
+                       spin_lock(&ctx->lock);
+                       list_splice_tail_init(&ctx->rq_list, list);
+                       spin_unlock(&ctx->lock);
+
+                       bit++;
+               } while (1);
+       }
 }
 
 /*
@@ -538,10 +742,9 @@ void blk_mq_add_timer(struct request *rq)
 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
 {
        struct request_queue *q = hctx->queue;
-       struct blk_mq_ctx *ctx;
        struct request *rq;
        LIST_HEAD(rq_list);
-       int bit, queued;
+       int queued;
 
        WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
 
@@ -553,15 +756,7 @@ static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
        /*
         * Touch any software queue that has pending entries.
         */
-       for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
-               clear_bit(bit, hctx->ctx_map);
-               ctx = hctx->ctxs[bit];
-               BUG_ON(bit != ctx->index_hw);
-
-               spin_lock(&ctx->lock);
-               list_splice_tail_init(&ctx->rq_list, &rq_list);
-               spin_unlock(&ctx->lock);
-       }
+       flush_busy_ctxs(hctx, &rq_list);
 
        /*
         * If we have previous entries on our dispatch list, grab them
@@ -574,14 +769,10 @@ static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
                spin_unlock(&hctx->lock);
        }
 
-       /*
-        * Delete and return all entries from our dispatch list
-        */
-       queued = 0;
-
        /*
         * Now process all the entries, sending them to the driver.
         */
+       queued = 0;
        while (!list_empty(&rq_list)) {
                int ret;
 
@@ -596,13 +787,8 @@ static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
                        queued++;
                        continue;
                case BLK_MQ_RQ_QUEUE_BUSY:
-                       /*
-                        * FIXME: we should have a mechanism to stop the queue
-                        * like blk_stop_queue, otherwise we will waste cpu
-                        * time
-                        */
                        list_add(&rq->queuelist, &rq_list);
-                       blk_mq_requeue_request(rq);
+                       __blk_mq_requeue_request(rq);
                        break;
                default:
                        pr_err("blk-mq: bad return on queue: %d\n", ret);
@@ -632,6 +818,30 @@ static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
        }
 }
 
+/*
+ * It'd be great if the workqueue API had a way to pass
+ * in a mask and had some smarts for more clever placement.
+ * For now we just round-robin here, switching for every
+ * BLK_MQ_CPU_WORK_BATCH queued items.
+ */
+static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
+{
+       int cpu = hctx->next_cpu;
+
+       if (--hctx->next_cpu_batch <= 0) {
+               int next_cpu;
+
+               next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
+               if (next_cpu >= nr_cpu_ids)
+                       next_cpu = cpumask_first(hctx->cpumask);
+
+               hctx->next_cpu = next_cpu;
+               hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
+       }
+
+       return cpu;
+}
+
 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
 {
        if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
@@ -640,18 +850,12 @@ void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
        if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
                __blk_mq_run_hw_queue(hctx);
        else if (hctx->queue->nr_hw_queues == 1)
-               kblockd_schedule_delayed_work(&hctx->delayed_work, 0);
+               kblockd_schedule_delayed_work(&hctx->run_work, 0);
        else {
                unsigned int cpu;
 
-               /*
-                * It'd be great if the workqueue API had a way to pass
-                * in a mask and had some smarts for more clever placement
-                * than the first CPU. Or we could round-robin here. For now,
-                * just queue on the first CPU.
-                */
-               cpu = cpumask_first(hctx->cpumask);
-               kblockd_schedule_delayed_work_on(cpu, &hctx->delayed_work, 0);
+               cpu = blk_mq_hctx_next_cpu(hctx);
+               kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
        }
 }
 
@@ -675,7 +879,8 @@ EXPORT_SYMBOL(blk_mq_run_queues);
 
 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
 {
-       cancel_delayed_work(&hctx->delayed_work);
+       cancel_delayed_work(&hctx->run_work);
+       cancel_delayed_work(&hctx->delay_work);
        set_bit(BLK_MQ_S_STOPPED, &hctx->state);
 }
 EXPORT_SYMBOL(blk_mq_stop_hw_queue);
@@ -700,6 +905,17 @@ void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
 }
 EXPORT_SYMBOL(blk_mq_start_hw_queue);
 
+void blk_mq_start_hw_queues(struct request_queue *q)
+{
+       struct blk_mq_hw_ctx *hctx;
+       int i;
+
+       queue_for_each_hw_ctx(q, hctx, i)
+               blk_mq_start_hw_queue(hctx);
+}
+EXPORT_SYMBOL(blk_mq_start_hw_queues);
+
+
 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
 {
        struct blk_mq_hw_ctx *hctx;
@@ -717,15 +933,40 @@ void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
 }
 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
 
-static void blk_mq_work_fn(struct work_struct *work)
+static void blk_mq_run_work_fn(struct work_struct *work)
 {
        struct blk_mq_hw_ctx *hctx;
 
-       hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work);
+       hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
 
        __blk_mq_run_hw_queue(hctx);
 }
 
+static void blk_mq_delay_work_fn(struct work_struct *work)
+{
+       struct blk_mq_hw_ctx *hctx;
+
+       hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
+
+       if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
+               __blk_mq_run_hw_queue(hctx);
+}
+
+void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
+{
+       unsigned long tmo = msecs_to_jiffies(msecs);
+
+       if (hctx->queue->nr_hw_queues == 1)
+               kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
+       else {
+               unsigned int cpu;
+
+               cpu = blk_mq_hctx_next_cpu(hctx);
+               kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
+       }
+}
+EXPORT_SYMBOL(blk_mq_delay_queue);
+
 static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
                                    struct request *rq, bool at_head)
 {
@@ -737,12 +978,13 @@ static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
                list_add(&rq->queuelist, &ctx->rq_list);
        else
                list_add_tail(&rq->queuelist, &ctx->rq_list);
+
        blk_mq_hctx_mark_pending(hctx, ctx);
 
        /*
         * We do this early, to ensure we are on the right CPU.
         */
-       blk_mq_add_timer(rq);
+       blk_add_timer(rq);
 }
 
 void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
@@ -873,56 +1115,98 @@ static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
        blk_account_io_start(rq, 1);
 }
 
-static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
+static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
+                                        struct blk_mq_ctx *ctx,
+                                        struct request *rq, struct bio *bio)
 {
-       struct blk_mq_hw_ctx *hctx;
-       struct blk_mq_ctx *ctx;
-       const int is_sync = rw_is_sync(bio->bi_rw);
-       const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
-       int rw = bio_data_dir(bio);
-       struct request *rq;
-       unsigned int use_plug, request_count = 0;
-
-       /*
-        * If we have multiple hardware queues, just go directly to
-        * one of those for sync IO.
-        */
-       use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
+       struct request_queue *q = hctx->queue;
 
-       blk_queue_bounce(q, &bio);
+       if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) {
+               blk_mq_bio_to_request(rq, bio);
+               spin_lock(&ctx->lock);
+insert_rq:
+               __blk_mq_insert_request(hctx, rq, false);
+               spin_unlock(&ctx->lock);
+               return false;
+       } else {
+               spin_lock(&ctx->lock);
+               if (!blk_mq_attempt_merge(q, ctx, bio)) {
+                       blk_mq_bio_to_request(rq, bio);
+                       goto insert_rq;
+               }
 
-       if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
-               bio_endio(bio, -EIO);
-               return;
+               spin_unlock(&ctx->lock);
+               __blk_mq_free_request(hctx, ctx, rq);
+               return true;
        }
+}
 
-       if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
-               return;
+struct blk_map_ctx {
+       struct blk_mq_hw_ctx *hctx;
+       struct blk_mq_ctx *ctx;
+};
+
+static struct request *blk_mq_map_request(struct request_queue *q,
+                                         struct bio *bio,
+                                         struct blk_map_ctx *data)
+{
+       struct blk_mq_hw_ctx *hctx;
+       struct blk_mq_ctx *ctx;
+       struct request *rq;
+       int rw = bio_data_dir(bio);
 
-       if (blk_mq_queue_enter(q)) {
+       if (unlikely(blk_mq_queue_enter(q))) {
                bio_endio(bio, -EIO);
-               return;
+               return NULL;
        }
 
        ctx = blk_mq_get_ctx(q);
        hctx = q->mq_ops->map_queue(q, ctx->cpu);
 
-       if (is_sync)
+       if (rw_is_sync(bio->bi_rw))
                rw |= REQ_SYNC;
+
        trace_block_getrq(q, bio, rw);
-       rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
-       if (likely(rq))
-               blk_mq_rq_ctx_init(q, ctx, rq, rw);
-       else {
+       rq = __blk_mq_alloc_request(q, hctx, ctx, rw, GFP_ATOMIC, false);
+       if (unlikely(!rq)) {
+               __blk_mq_run_hw_queue(hctx);
                blk_mq_put_ctx(ctx);
                trace_block_sleeprq(q, bio, rw);
-               rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
-                                                       false);
-               ctx = rq->mq_ctx;
+
+               ctx = blk_mq_get_ctx(q);
                hctx = q->mq_ops->map_queue(q, ctx->cpu);
+               rq = __blk_mq_alloc_request(q, hctx, ctx, rw,
+                                           __GFP_WAIT|GFP_ATOMIC, false);
        }
 
        hctx->queued++;
+       data->hctx = hctx;
+       data->ctx = ctx;
+       return rq;
+}
+
+/*
+ * Multiple hardware queue variant. This will not use per-process plugs,
+ * but will attempt to bypass the hctx queueing if we can go straight to
+ * hardware for SYNC IO.
+ */
+static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
+{
+       const int is_sync = rw_is_sync(bio->bi_rw);
+       const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
+       struct blk_map_ctx data;
+       struct request *rq;
+
+       blk_queue_bounce(q, &bio);
+
+       if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
+               bio_endio(bio, -EIO);
+               return;
+       }
+
+       rq = blk_mq_map_request(q, bio, &data);
+       if (unlikely(!rq))
+               return;
 
        if (unlikely(is_flush_fua)) {
                blk_mq_bio_to_request(rq, bio);
@@ -930,119 +1214,126 @@ static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
                goto run_queue;
        }
 
-       /*
-        * A task plug currently exists. Since this is completely lockless,
-        * utilize that to temporarily store requests until the task is
-        * either done or scheduled away.
-        */
-       if (use_plug) {
-               struct blk_plug *plug = current->plug;
+       if (is_sync) {
+               int ret;
 
-               if (plug) {
-                       blk_mq_bio_to_request(rq, bio);
-                       if (list_empty(&plug->mq_list))
-                               trace_block_plug(q);
-                       else if (request_count >= BLK_MAX_REQUEST_COUNT) {
-                               blk_flush_plug_list(plug, false);
-                               trace_block_plug(q);
+               blk_mq_bio_to_request(rq, bio);
+               blk_mq_start_request(rq, true);
+
+               /*
+                * For OK queue, we are done. For error, kill it. Any other
+                * error (busy), just add it to our list as we previously
+                * would have done
+                */
+               ret = q->mq_ops->queue_rq(data.hctx, rq);
+               if (ret == BLK_MQ_RQ_QUEUE_OK)
+                       goto done;
+               else {
+                       __blk_mq_requeue_request(rq);
+
+                       if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
+                               rq->errors = -EIO;
+                               blk_mq_end_io(rq, rq->errors);
+                               goto done;
                        }
-                       list_add_tail(&rq->queuelist, &plug->mq_list);
-                       blk_mq_put_ctx(ctx);
-                       return;
                }
        }
 
-       spin_lock(&ctx->lock);
-
-       if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
-           blk_mq_attempt_merge(q, ctx, bio))
-               __blk_mq_free_request(hctx, ctx, rq);
-       else {
-               blk_mq_bio_to_request(rq, bio);
-               __blk_mq_insert_request(hctx, rq, false);
-       }
-
-       spin_unlock(&ctx->lock);
-
-       /*
-        * For a SYNC request, send it to the hardware immediately. For an
-        * ASYNC request, just ensure that we run it later on. The latter
-        * allows for merging opportunities and more efficient dispatching.
-        */
+       if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
+               /*
+                * For a SYNC request, send it to the hardware immediately. For
+                * an ASYNC request, just ensure that we run it later on. The
+                * latter allows for merging opportunities and more efficient
+                * dispatching.
+                */
 run_queue:
-       blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
-       blk_mq_put_ctx(ctx);
+               blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
+       }
+done:
+       blk_mq_put_ctx(data.ctx);
 }
 
 /*
- * Default mapping to a software queue, since we use one per CPU.
+ * Single hardware queue variant. This will attempt to use any per-process
+ * plug for merging and IO deferral.
  */
-struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
-{
-       return q->queue_hw_ctx[q->mq_map[cpu]];
-}
-EXPORT_SYMBOL(blk_mq_map_queue);
-
-struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *set,
-                                                  unsigned int hctx_index)
-{
-       return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
-                               GFP_KERNEL | __GFP_ZERO, set->numa_node);
-}
-EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
-
-void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
-                                unsigned int hctx_index)
+static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
 {
-       kfree(hctx);
-}
-EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
-
-static void blk_mq_hctx_notify(void *data, unsigned long action,
-                              unsigned int cpu)
-{
-       struct blk_mq_hw_ctx *hctx = data;
-       struct request_queue *q = hctx->queue;
-       struct blk_mq_ctx *ctx;
-       LIST_HEAD(tmp);
-
-       if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
-               return;
+       const int is_sync = rw_is_sync(bio->bi_rw);
+       const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
+       unsigned int use_plug, request_count = 0;
+       struct blk_map_ctx data;
+       struct request *rq;
 
        /*
-        * Move ctx entries to new CPU, if this one is going away.
+        * If we have multiple hardware queues, just go directly to
+        * one of those for sync IO.
         */
-       ctx = __blk_mq_get_ctx(q, cpu);
+       use_plug = !is_flush_fua && !is_sync;
 
-       spin_lock(&ctx->lock);
-       if (!list_empty(&ctx->rq_list)) {
-               list_splice_init(&ctx->rq_list, &tmp);
-               clear_bit(ctx->index_hw, hctx->ctx_map);
+       blk_queue_bounce(q, &bio);
+
+       if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
+               bio_endio(bio, -EIO);
+               return;
        }
-       spin_unlock(&ctx->lock);
 
-       if (list_empty(&tmp))
+       if (use_plug && !blk_queue_nomerges(q) &&
+           blk_attempt_plug_merge(q, bio, &request_count))
                return;
 
-       ctx = blk_mq_get_ctx(q);
-       spin_lock(&ctx->lock);
+       rq = blk_mq_map_request(q, bio, &data);
 
-       while (!list_empty(&tmp)) {
-               struct request *rq;
+       if (unlikely(is_flush_fua)) {
+               blk_mq_bio_to_request(rq, bio);
+               blk_insert_flush(rq);
+               goto run_queue;
+       }
 
-               rq = list_first_entry(&tmp, struct request, queuelist);
-               rq->mq_ctx = ctx;
-               list_move_tail(&rq->queuelist, &ctx->rq_list);
+       /*
+        * A task plug currently exists. Since this is completely lockless,
+        * utilize that to temporarily store requests until the task is
+        * either done or scheduled away.
+        */
+       if (use_plug) {
+               struct blk_plug *plug = current->plug;
+
+               if (plug) {
+                       blk_mq_bio_to_request(rq, bio);
+                       if (list_empty(&plug->mq_list))
+                               trace_block_plug(q);
+                       else if (request_count >= BLK_MAX_REQUEST_COUNT) {
+                               blk_flush_plug_list(plug, false);
+                               trace_block_plug(q);
+                       }
+                       list_add_tail(&rq->queuelist, &plug->mq_list);
+                       blk_mq_put_ctx(data.ctx);
+                       return;
+               }
        }
 
-       hctx = q->mq_ops->map_queue(q, ctx->cpu);
-       blk_mq_hctx_mark_pending(hctx, ctx);
+       if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
+               /*
+                * For a SYNC request, send it to the hardware immediately. For
+                * an ASYNC request, just ensure that we run it later on. The
+                * latter allows for merging opportunities and more efficient
+                * dispatching.
+                */
+run_queue:
+               blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
+       }
 
-       spin_unlock(&ctx->lock);
+       blk_mq_put_ctx(data.ctx);
+}
 
-       blk_mq_run_hw_queue(hctx, true);
-       blk_mq_put_ctx(ctx);
+/*
+ * Default mapping to a software queue, since we use one per CPU.
+ */
+struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
+{
+       return q->queue_hw_ctx[q->mq_map[cpu]];
 }
+EXPORT_SYMBOL(blk_mq_map_queue);
 
 static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
                struct blk_mq_tags *tags, unsigned int hctx_idx)
@@ -1073,12 +1364,7 @@ static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
 
 static size_t order_to_size(unsigned int order)
 {
-       size_t ret = PAGE_SIZE;
-
-       while (order--)
-               ret *= 2;
-
-       return ret;
+       return (size_t)PAGE_SIZE << order;
 }
 
 static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
@@ -1162,24 +1448,155 @@ fail:
        return NULL;
 }
 
+static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
+{
+       kfree(bitmap->map);
+}
+
+static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
+{
+       unsigned int bpw = 8, total, num_maps, i;
+
+       bitmap->bits_per_word = bpw;
+
+       num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
+       bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
+                                       GFP_KERNEL, node);
+       if (!bitmap->map)
+               return -ENOMEM;
+
+       bitmap->map_size = num_maps;
+
+       total = nr_cpu_ids;
+       for (i = 0; i < num_maps; i++) {
+               bitmap->map[i].depth = min(total, bitmap->bits_per_word);
+               total -= bitmap->map[i].depth;
+       }
+
+       return 0;
+}
+
+static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
+{
+       struct request_queue *q = hctx->queue;
+       struct blk_mq_ctx *ctx;
+       LIST_HEAD(tmp);
+
+       /*
+        * Move ctx entries to new CPU, if this one is going away.
+        */
+       ctx = __blk_mq_get_ctx(q, cpu);
+
+       spin_lock(&ctx->lock);
+       if (!list_empty(&ctx->rq_list)) {
+               list_splice_init(&ctx->rq_list, &tmp);
+               blk_mq_hctx_clear_pending(hctx, ctx);
+       }
+       spin_unlock(&ctx->lock);
+
+       if (list_empty(&tmp))
+               return NOTIFY_OK;
+
+       ctx = blk_mq_get_ctx(q);
+       spin_lock(&ctx->lock);
+
+       while (!list_empty(&tmp)) {
+               struct request *rq;
+
+               rq = list_first_entry(&tmp, struct request, queuelist);
+               rq->mq_ctx = ctx;
+               list_move_tail(&rq->queuelist, &ctx->rq_list);
+       }
+
+       hctx = q->mq_ops->map_queue(q, ctx->cpu);
+       blk_mq_hctx_mark_pending(hctx, ctx);
+
+       spin_unlock(&ctx->lock);
+
+       blk_mq_run_hw_queue(hctx, true);
+       blk_mq_put_ctx(ctx);
+       return NOTIFY_OK;
+}
+
+static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
+{
+       struct request_queue *q = hctx->queue;
+       struct blk_mq_tag_set *set = q->tag_set;
+
+       if (set->tags[hctx->queue_num])
+               return NOTIFY_OK;
+
+       set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
+       if (!set->tags[hctx->queue_num])
+               return NOTIFY_STOP;
+
+       hctx->tags = set->tags[hctx->queue_num];
+       return NOTIFY_OK;
+}
+
+static int blk_mq_hctx_notify(void *data, unsigned long action,
+                             unsigned int cpu)
+{
+       struct blk_mq_hw_ctx *hctx = data;
+
+       if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
+               return blk_mq_hctx_cpu_offline(hctx, cpu);
+       else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
+               return blk_mq_hctx_cpu_online(hctx, cpu);
+
+       return NOTIFY_OK;
+}
+
+static void blk_mq_exit_hw_queues(struct request_queue *q,
+               struct blk_mq_tag_set *set, int nr_queue)
+{
+       struct blk_mq_hw_ctx *hctx;
+       unsigned int i;
+
+       queue_for_each_hw_ctx(q, hctx, i) {
+               if (i == nr_queue)
+                       break;
+
+               if (set->ops->exit_hctx)
+                       set->ops->exit_hctx(hctx, i);
+
+               blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
+               kfree(hctx->ctxs);
+               blk_mq_free_bitmap(&hctx->ctx_map);
+       }
+
+}
+
+static void blk_mq_free_hw_queues(struct request_queue *q,
+               struct blk_mq_tag_set *set)
+{
+       struct blk_mq_hw_ctx *hctx;
+       unsigned int i;
+
+       queue_for_each_hw_ctx(q, hctx, i) {
+               free_cpumask_var(hctx->cpumask);
+               kfree(hctx);
+       }
+}
+
 static int blk_mq_init_hw_queues(struct request_queue *q,
                struct blk_mq_tag_set *set)
 {
        struct blk_mq_hw_ctx *hctx;
-       unsigned int i, j;
+       unsigned int i;
 
        /*
         * Initialize hardware queues
         */
        queue_for_each_hw_ctx(q, hctx, i) {
-               unsigned int num_maps;
                int node;
 
                node = hctx->numa_node;
                if (node == NUMA_NO_NODE)
                        node = hctx->numa_node = set->numa_node;
 
-               INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn);
+               INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
+               INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
                spin_lock_init(&hctx->lock);
                INIT_LIST_HEAD(&hctx->dispatch);
                hctx->queue = q;
@@ -1202,13 +1619,9 @@ static int blk_mq_init_hw_queues(struct request_queue *q,
                if (!hctx->ctxs)
                        break;
 
-               num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
-               hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
-                                               GFP_KERNEL, node);
-               if (!hctx->ctx_map)
+               if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
                        break;
 
-               hctx->nr_ctx_map = num_maps;
                hctx->nr_ctx = 0;
 
                if (set->ops->init_hctx &&
@@ -1222,16 +1635,7 @@ static int blk_mq_init_hw_queues(struct request_queue *q,
        /*
         * Init failed
         */
-       queue_for_each_hw_ctx(q, hctx, j) {
-               if (i == j)
-                       break;
-
-               if (set->ops->exit_hctx)
-                       set->ops->exit_hctx(hctx, j);
-
-               blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
-               kfree(hctx->ctxs);
-       }
+       blk_mq_exit_hw_queues(q, set, i);
 
        return 1;
 }
@@ -1292,6 +1696,79 @@ static void blk_mq_map_swqueue(struct request_queue *q)
                ctx->index_hw = hctx->nr_ctx;
                hctx->ctxs[hctx->nr_ctx++] = ctx;
        }
+
+       queue_for_each_hw_ctx(q, hctx, i) {
+               /*
+                * If not software queues are mapped to this hardware queue,
+                * disable it and free the request entries
+                */
+               if (!hctx->nr_ctx) {
+                       struct blk_mq_tag_set *set = q->tag_set;
+
+                       if (set->tags[i]) {
+                               blk_mq_free_rq_map(set, set->tags[i], i);
+                               set->tags[i] = NULL;
+                               hctx->tags = NULL;
+                       }
+                       continue;
+               }
+
+               /*
+                * Initialize batch roundrobin counts
+                */
+               hctx->next_cpu = cpumask_first(hctx->cpumask);
+               hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
+       }
+}
+
+static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
+{
+       struct blk_mq_hw_ctx *hctx;
+       struct request_queue *q;
+       bool shared;
+       int i;
+
+       if (set->tag_list.next == set->tag_list.prev)
+               shared = false;
+       else
+               shared = true;
+
+       list_for_each_entry(q, &set->tag_list, tag_set_list) {
+               blk_mq_freeze_queue(q);
+
+               queue_for_each_hw_ctx(q, hctx, i) {
+                       if (shared)
+                               hctx->flags |= BLK_MQ_F_TAG_SHARED;
+                       else
+                               hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
+               }
+               blk_mq_unfreeze_queue(q);
+       }
+}
+
+static void blk_mq_del_queue_tag_set(struct request_queue *q)
+{
+       struct blk_mq_tag_set *set = q->tag_set;
+
+       blk_mq_freeze_queue(q);
+
+       mutex_lock(&set->tag_list_lock);
+       list_del_init(&q->tag_set_list);
+       blk_mq_update_tag_set_depth(set);
+       mutex_unlock(&set->tag_list_lock);
+
+       blk_mq_unfreeze_queue(q);
+}
+
+static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
+                                    struct request_queue *q)
+{
+       q->tag_set = set;
+
+       mutex_lock(&set->tag_list_lock);
+       list_add_tail(&q->tag_set_list, &set->tag_list);
+       blk_mq_update_tag_set_depth(set);
+       mutex_unlock(&set->tag_list_lock);
 }
 
 struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
@@ -1299,6 +1776,7 @@ struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
        struct blk_mq_hw_ctx **hctxs;
        struct blk_mq_ctx *ctx;
        struct request_queue *q;
+       unsigned int *map;
        int i;
 
        ctx = alloc_percpu(struct blk_mq_ctx);
@@ -1311,15 +1789,23 @@ struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
        if (!hctxs)
                goto err_percpu;
 
+       map = blk_mq_make_queue_map(set);
+       if (!map)
+               goto err_map;
+
        for (i = 0; i < set->nr_hw_queues; i++) {
-               hctxs[i] = set->ops->alloc_hctx(set, i);
+               int node = blk_mq_hw_queue_to_node(map, i);
+
+               hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
+                                       GFP_KERNEL, node);
                if (!hctxs[i])
                        goto err_hctxs;
 
                if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
                        goto err_hctxs;
 
-               hctxs[i]->numa_node = NUMA_NO_NODE;
+               atomic_set(&hctxs[i]->nr_active, 0);
+               hctxs[i]->numa_node = node;
                hctxs[i]->queue_num = i;
        }
 
@@ -1327,8 +1813,7 @@ struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
        if (!q)
                goto err_hctxs;
 
-       q->mq_map = blk_mq_make_queue_map(set);
-       if (!q->mq_map)
+       if (percpu_counter_init(&q->mq_usage_counter, 0))
                goto err_map;
 
        setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
@@ -1336,6 +1821,7 @@ struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
 
        q->nr_queues = nr_cpu_ids;
        q->nr_hw_queues = set->nr_hw_queues;
+       q->mq_map = map;
 
        q->queue_ctx = ctx;
        q->queue_hw_ctx = hctxs;
@@ -1345,11 +1831,24 @@ struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
 
        q->sg_reserved_size = INT_MAX;
 
-       blk_queue_make_request(q, blk_mq_make_request);
-       blk_queue_rq_timed_out(q, set->ops->timeout);
+       INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
+       INIT_LIST_HEAD(&q->requeue_list);
+       spin_lock_init(&q->requeue_lock);
+
+       if (q->nr_hw_queues > 1)
+               blk_queue_make_request(q, blk_mq_make_request);
+       else
+               blk_queue_make_request(q, blk_sq_make_request);
+
+       blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
        if (set->timeout)
                blk_queue_rq_timeout(q, set->timeout);
 
+       /*
+        * Do this after blk_queue_make_request() overrides it...
+        */
+       q->nr_requests = set->queue_depth;
+
        if (set->ops->complete)
                blk_queue_softirq_done(q, set->ops->complete);
 
@@ -1365,27 +1864,29 @@ struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
        if (blk_mq_init_hw_queues(q, set))
                goto err_flush_rq;
 
-       blk_mq_map_swqueue(q);
-
        mutex_lock(&all_q_mutex);
        list_add_tail(&q->all_q_node, &all_q_list);
        mutex_unlock(&all_q_mutex);
 
+       blk_mq_add_queue_tag_set(set, q);
+
+       blk_mq_map_swqueue(q);
+
        return q;
 
 err_flush_rq:
        kfree(q->flush_rq);
 err_hw:
-       kfree(q->mq_map);
-err_map:
        blk_cleanup_queue(q);
 err_hctxs:
+       kfree(map);
        for (i = 0; i < set->nr_hw_queues; i++) {
                if (!hctxs[i])
                        break;
                free_cpumask_var(hctxs[i]->cpumask);
-               set->ops->free_hctx(hctxs[i], i);
+               kfree(hctxs[i]);
        }
+err_map:
        kfree(hctxs);
 err_percpu:
        free_percpu(ctx);
@@ -1395,18 +1896,14 @@ EXPORT_SYMBOL(blk_mq_init_queue);
 
 void blk_mq_free_queue(struct request_queue *q)
 {
-       struct blk_mq_hw_ctx *hctx;
-       int i;
+       struct blk_mq_tag_set   *set = q->tag_set;
 
-       queue_for_each_hw_ctx(q, hctx, i) {
-               kfree(hctx->ctx_map);
-               kfree(hctx->ctxs);
-               blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
-               if (q->mq_ops->exit_hctx)
-                       q->mq_ops->exit_hctx(hctx, i);
-               free_cpumask_var(hctx->cpumask);
-               q->mq_ops->free_hctx(hctx, i);
-       }
+       blk_mq_del_queue_tag_set(q);
+
+       blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
+       blk_mq_free_hw_queues(q, set);
+
+       percpu_counter_destroy(&q->mq_usage_counter);
 
        free_percpu(q->queue_ctx);
        kfree(q->queue_hw_ctx);
@@ -1445,10 +1942,10 @@ static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
        struct request_queue *q;
 
        /*
-        * Before new mapping is established, hotadded cpu might already start
-        * handling requests. This doesn't break anything as we map offline
-        * CPUs to first hardware queue. We will re-init queue below to get
-        * optimal settings.
+        * Before new mappings are established, hotadded cpu might already
+        * start handling requests. This doesn't break anything as we map
+        * offline CPUs to first hardware queue. We will re-init the queue
+        * below to get optimal settings.
         */
        if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
            action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
@@ -1472,13 +1969,12 @@ int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
        if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
                return -EINVAL;
 
-       if (!set->nr_hw_queues ||
-           !set->ops->queue_rq || !set->ops->map_queue ||
-           !set->ops->alloc_hctx || !set->ops->free_hctx)
+       if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue)
                return -EINVAL;
 
 
-       set->tags = kmalloc_node(set->nr_hw_queues * sizeof(struct blk_mq_tags),
+       set->tags = kmalloc_node(set->nr_hw_queues *
+                                sizeof(struct blk_mq_tags *),
                                 GFP_KERNEL, set->numa_node);
        if (!set->tags)
                goto out;
@@ -1489,6 +1985,9 @@ int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
                        goto out_unwind;
        }
 
+       mutex_init(&set->tag_list_lock);
+       INIT_LIST_HEAD(&set->tag_list);
+
        return 0;
 
 out_unwind:
@@ -1503,11 +2002,37 @@ void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
 {
        int i;
 
-       for (i = 0; i < set->nr_hw_queues; i++)
-               blk_mq_free_rq_map(set, set->tags[i], i);
+       for (i = 0; i < set->nr_hw_queues; i++) {
+               if (set->tags[i])
+                       blk_mq_free_rq_map(set, set->tags[i], i);
+       }
+
+       kfree(set->tags);
 }
 EXPORT_SYMBOL(blk_mq_free_tag_set);
 
+int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
+{
+       struct blk_mq_tag_set *set = q->tag_set;
+       struct blk_mq_hw_ctx *hctx;
+       int i, ret;
+
+       if (!set || nr > set->queue_depth)
+               return -EINVAL;
+
+       ret = 0;
+       queue_for_each_hw_ctx(q, hctx, i) {
+               ret = blk_mq_tag_update_depth(hctx->tags, nr);
+               if (ret)
+                       break;
+       }
+
+       if (!ret)
+               q->nr_requests = nr;
+
+       return ret;
+}
+
 void blk_mq_disable_hotplug(void)
 {
        mutex_lock(&all_q_mutex);