]> 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 fec8fcc4f8a4ea0f3a866bc4feaae6feb73eb4f5..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>
@@ -92,30 +98,6 @@ static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
        clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
 }
 
-static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,
-                                             struct blk_mq_ctx *ctx,
-                                             gfp_t gfp, bool reserved)
-{
-       struct request *rq;
-       unsigned int tag;
-
-       tag = blk_mq_get_tag(hctx, &ctx->last_tag, gfp, reserved);
-       if (tag != BLK_MQ_TAG_FAIL) {
-               rq = hctx->tags->rqs[tag];
-
-               rq->cmd_flags = 0;
-               if (blk_mq_tag_busy(hctx)) {
-                       rq->cmd_flags = REQ_MQ_INFLIGHT;
-                       atomic_inc(&hctx->nr_active);
-               }
-
-               rq->tag = tag;
-               return rq;
-       }
-
-       return NULL;
-}
-
 static int blk_mq_queue_enter(struct request_queue *q)
 {
        int ret;
@@ -263,65 +245,58 @@ static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
        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, ctx, 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, reserved);
-       } 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;
-}
-EXPORT_SYMBOL(blk_mq_alloc_request);
-
-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)
@@ -434,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);
 
@@ -454,9 +435,13 @@ 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.
         */
-       rq->deadline = jiffies + q->rq_timeout;
+       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
@@ -506,10 +491,68 @@ void blk_mq_requeue_request(struct request *rq)
        blk_clear_rq_complete(rq);
 
        BUG_ON(blk_queued_rq(rq));
-       blk_mq_insert_request(rq, true, true, false);
+       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];
@@ -597,8 +640,16 @@ 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) {
                next = blk_rq_timeout(round_jiffies_up(next));
@@ -1064,21 +1115,161 @@ 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 request_queue *q = hctx->queue;
+
+       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;
+               }
+
+               spin_unlock(&ctx->lock);
+               __blk_mq_free_request(hctx, ctx, rq);
+               return true;
+       }
+}
+
+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 (unlikely(blk_mq_queue_enter(q))) {
+               bio_endio(bio, -EIO);
+               return NULL;
+       }
+
+       ctx = blk_mq_get_ctx(q);
+       hctx = q->mq_ops->map_queue(q, ctx->cpu);
+
+       if (rw_is_sync(bio->bi_rw))
+               rw |= REQ_SYNC;
+
+       trace_block_getrq(q, bio, rw);
+       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);
+
+               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);
-       int rw = bio_data_dir(bio);
+       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);
+               blk_insert_flush(rq);
+               goto run_queue;
+       }
+
+       if (is_sync) {
+               int ret;
+
+               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;
+                       }
+               }
+       }
+
+       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);
+       }
+done:
+       blk_mq_put_ctx(data.ctx);
+}
+
+/*
+ * Single hardware queue variant. This will attempt to use any per-process
+ * plug for merging and IO deferral.
+ */
+static void blk_sq_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);
        unsigned int use_plug, request_count = 0;
+       struct blk_map_ctx data;
+       struct request *rq;
 
        /*
         * 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);
+       use_plug = !is_flush_fua && !is_sync;
 
        blk_queue_bounce(q, &bio);
 
@@ -1087,33 +1278,11 @@ static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
                return;
        }
 
-       if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
+       if (use_plug && !blk_queue_nomerges(q) &&
+           blk_attempt_plug_merge(q, bio, &request_count))
                return;
 
-       if (blk_mq_queue_enter(q)) {
-               bio_endio(bio, -EIO);
-               return;
-       }
-
-       ctx = blk_mq_get_ctx(q);
-       hctx = q->mq_ops->map_queue(q, ctx->cpu);
-
-       if (is_sync)
-               rw |= REQ_SYNC;
-       trace_block_getrq(q, bio, rw);
-       rq = __blk_mq_alloc_request(hctx, ctx, GFP_ATOMIC, false);
-       if (likely(rq))
-               blk_mq_rq_ctx_init(q, ctx, rq, rw);
-       else {
-               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;
-               hctx = q->mq_ops->map_queue(q, ctx->cpu);
-       }
-
-       hctx->queued++;
+       rq = blk_mq_map_request(q, bio, &data);
 
        if (unlikely(is_flush_fua)) {
                blk_mq_bio_to_request(rq, bio);
@@ -1138,37 +1307,23 @@ static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
                                trace_block_plug(q);
                        }
                        list_add_tail(&rq->queuelist, &plug->mq_list);
-                       blk_mq_put_ctx(ctx);
+                       blk_mq_put_ctx(data.ctx);
                        return;
                }
        }
 
-       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);
-       } else {
-               spin_lock(&ctx->lock);
-               if (!blk_mq_attempt_merge(q, ctx, bio)) {
-                       blk_mq_bio_to_request(rq, bio);
-                       goto insert_rq;
-               }
-
-               spin_unlock(&ctx->lock);
-               __blk_mq_free_request(hctx, ctx, rq);
+       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);
        }
 
-
-       /*
-        * 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_put_ctx(data.ctx);
 }
 
 /*
@@ -1180,67 +1335,6 @@ struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int 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 kzalloc_node(sizeof(struct blk_mq_hw_ctx), GFP_KERNEL,
-                               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)
-{
-       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;
-
-       /*
-        * 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;
-
-       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);
-}
-
 static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
                struct blk_mq_tags *tags, unsigned int hctx_idx)
 {
@@ -1382,11 +1476,114 @@ static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
        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
@@ -1438,17 +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_free_bitmap(&hctx->ctx_map);
-       }
+       blk_mq_exit_hw_queues(q, set, i);
 
        return 1;
 }
@@ -1511,6 +1698,24 @@ static void blk_mq_map_swqueue(struct request_queue *q)
        }
 
        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;
        }
@@ -1571,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);
@@ -1583,8 +1789,15 @@ 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;
 
@@ -1592,7 +1805,7 @@ struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
                        goto err_hctxs;
 
                atomic_set(&hctxs[i]->nr_active, 0);
-               hctxs[i]->numa_node = NUMA_NO_NODE;
+               hctxs[i]->numa_node = node;
                hctxs[i]->queue_num = i;
        }
 
@@ -1600,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);
@@ -1609,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;
@@ -1618,7 +1831,15 @@ 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);
+       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);
@@ -1643,29 +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);
@@ -1675,19 +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;
 
        blk_mq_del_queue_tag_set(q);
 
-       queue_for_each_hw_ctx(q, hctx, i) {
-               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_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);
@@ -1753,9 +1969,7 @@ 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;
 
 
@@ -1788,8 +2002,11 @@ 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);