]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blobdiff - block/blk-mq.c
blk-mq: move blk_mq_get_ctx/blk_mq_put_ctx to mq private header
[mirror_ubuntu-artful-kernel.git] / block / blk-mq.c
index 0fbef7e9bef1e9599364250feae7d9d3076f42d9..b9230c522c6bc5d2d79d869de5853f96657b1412 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>
@@ -27,28 +33,6 @@ static LIST_HEAD(all_q_list);
 
 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
 
-static struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
-                                          unsigned int cpu)
-{
-       return per_cpu_ptr(q->queue_ctx, cpu);
-}
-
-/*
- * This assumes per-cpu software queueing queues. They could be per-node
- * as well, for instance. For now this is hardcoded as-is. Note that we don't
- * care about preemption, since we know the ctx's are persistent. This does
- * mean that we can't rely on ctx always matching the currently running CPU.
- */
-static struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
-{
-       return __blk_mq_get_ctx(q, get_cpu());
-}
-
-static void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
-{
-       put_cpu();
-}
-
 /*
  * Check if any of the ctx's have pending work in this hardware queue
  */
@@ -92,30 +76,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;
@@ -217,19 +177,12 @@ static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
        rq->q = q;
        rq->mq_ctx = ctx;
        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);
@@ -239,23 +192,16 @@ static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
 #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;
@@ -263,65 +209,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)
@@ -398,7 +337,7 @@ static void __blk_mq_complete_request_remote(void *data)
        rq->q->softirq_done_fn(rq);
 }
 
-void __blk_mq_complete_request(struct request *rq)
+static void blk_mq_ipi_complete_request(struct request *rq)
 {
        struct blk_mq_ctx *ctx = rq->mq_ctx;
        bool shared = false;
@@ -424,6 +363,16 @@ void __blk_mq_complete_request(struct request *rq)
        put_cpu();
 }
 
+void __blk_mq_complete_request(struct request *rq)
+{
+       struct request_queue *q = rq->q;
+
+       if (!q->softirq_done_fn)
+               blk_mq_end_io(rq, rq->errors);
+       else
+               blk_mq_ipi_complete_request(rq);
+}
+
 /**
  * blk_mq_complete_request - end I/O on a request
  * @rq:                the request being processed
@@ -434,7 +383,9 @@ 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);
@@ -454,9 +405,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
@@ -464,8 +419,10 @@ static void blk_mq_start_request(struct request *rq, bool last)
         * complete. So be sure to clear complete again when we start
         * the request, otherwise we'll ignore the completion event.
         */
-       set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
-       clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
+       if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
+               set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
+       if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
+               clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
 
        if (q->dma_drain_size && blk_rq_bytes(rq)) {
                /*
@@ -506,13 +463,77 @@ 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);
 
-struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
+static void blk_mq_requeue_work(struct work_struct *work)
 {
-       return tags->rqs[tag];
+       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_hw_ctx *hctx, unsigned int tag)
+{
+       struct request_queue *q = hctx->queue;
+
+       if ((q->flush_rq->cmd_flags & REQ_FLUSH_SEQ) &&
+           q->flush_rq->tag == tag)
+               return q->flush_rq;
+
+       return hctx->tags->rqs[tag];
 }
 EXPORT_SYMBOL(blk_mq_tag_to_rq);
 
@@ -541,7 +562,7 @@ static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
                if (tag >= hctx->tags->nr_tags)
                        break;
 
-               rq = blk_mq_tag_to_rq(hctx->tags, tag++);
+               rq = blk_mq_tag_to_rq(hctx, tag++);
                if (rq->q != hctx->queue)
                        continue;
                if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
@@ -597,8 +618,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));
@@ -1061,24 +1090,169 @@ void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
 {
        init_request_from_bio(rq, bio);
-       blk_account_io_start(rq, 1);
+
+       if (blk_do_io_stat(rq)) {
+               rq->start_time = jiffies;
+               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);
+               blk_add_timer(rq);
+
+               /*
+                * 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 +1261,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))
-               return;
-
-       if (blk_mq_queue_enter(q)) {
-               bio_endio(bio, -EIO);
+       if (use_plug && !blk_queue_nomerges(q) &&
+           blk_attempt_plug_merge(q, bio, &request_count))
                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 +1290,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 +1318,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 +1459,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 +1618,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 +1681,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 +1759,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 +1772,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 +1788,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 +1796,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 +1804,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;
@@ -1616,13 +1812,29 @@ struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
        q->mq_ops = set->ops;
        q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
 
+       if (!(set->flags & BLK_MQ_F_SG_MERGE))
+               q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
+
        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);
 
+       /*
+        * 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);
 
@@ -1638,29 +1850,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);
@@ -1670,19 +1882,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);
@@ -1702,6 +1909,8 @@ static void blk_mq_queue_reinit(struct request_queue *q)
 {
        blk_mq_freeze_queue(q);
 
+       blk_mq_sysfs_unregister(q);
+
        blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
 
        /*
@@ -1712,6 +1921,8 @@ static void blk_mq_queue_reinit(struct request_queue *q)
 
        blk_mq_map_swqueue(q);
 
+       blk_mq_sysfs_register(q);
+
        blk_mq_unfreeze_queue(q);
 }
 
@@ -1748,9 +1959,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;
 
 
@@ -1783,12 +1992,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);