Jens Axboe | 75bb462 | 2014-05-28 10:15:41 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Block multiqueue core code |
| 3 | * |
| 4 | * Copyright (C) 2013-2014 Jens Axboe |
| 5 | * Copyright (C) 2013-2014 Christoph Hellwig |
| 6 | */ |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 7 | #include <linux/kernel.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/backing-dev.h> |
| 10 | #include <linux/bio.h> |
| 11 | #include <linux/blkdev.h> |
| 12 | #include <linux/mm.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/workqueue.h> |
| 16 | #include <linux/smp.h> |
| 17 | #include <linux/llist.h> |
| 18 | #include <linux/list_sort.h> |
| 19 | #include <linux/cpu.h> |
| 20 | #include <linux/cache.h> |
| 21 | #include <linux/sched/sysctl.h> |
| 22 | #include <linux/delay.h> |
| 23 | |
| 24 | #include <trace/events/block.h> |
| 25 | |
| 26 | #include <linux/blk-mq.h> |
| 27 | #include "blk.h" |
| 28 | #include "blk-mq.h" |
| 29 | #include "blk-mq-tag.h" |
| 30 | |
| 31 | static DEFINE_MUTEX(all_q_mutex); |
| 32 | static LIST_HEAD(all_q_list); |
| 33 | |
| 34 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx); |
| 35 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 36 | /* |
| 37 | * Check if any of the ctx's have pending work in this hardware queue |
| 38 | */ |
| 39 | static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx) |
| 40 | { |
| 41 | unsigned int i; |
| 42 | |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 43 | for (i = 0; i < hctx->ctx_map.map_size; i++) |
| 44 | if (hctx->ctx_map.map[i].word) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 45 | return true; |
| 46 | |
| 47 | return false; |
| 48 | } |
| 49 | |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 50 | static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx, |
| 51 | struct blk_mq_ctx *ctx) |
| 52 | { |
| 53 | return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word]; |
| 54 | } |
| 55 | |
| 56 | #define CTX_TO_BIT(hctx, ctx) \ |
| 57 | ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1)) |
| 58 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 59 | /* |
| 60 | * Mark this ctx as having pending work in this hardware queue |
| 61 | */ |
| 62 | static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx, |
| 63 | struct blk_mq_ctx *ctx) |
| 64 | { |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 65 | struct blk_align_bitmap *bm = get_bm(hctx, ctx); |
| 66 | |
| 67 | if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word)) |
| 68 | set_bit(CTX_TO_BIT(hctx, ctx), &bm->word); |
| 69 | } |
| 70 | |
| 71 | static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx, |
| 72 | struct blk_mq_ctx *ctx) |
| 73 | { |
| 74 | struct blk_align_bitmap *bm = get_bm(hctx, ctx); |
| 75 | |
| 76 | clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 79 | static int blk_mq_queue_enter(struct request_queue *q) |
| 80 | { |
| 81 | int ret; |
| 82 | |
| 83 | __percpu_counter_add(&q->mq_usage_counter, 1, 1000000); |
| 84 | smp_wmb(); |
Keith Busch | 3b632cf | 2014-06-06 10:22:07 -0600 | [diff] [blame] | 85 | |
| 86 | /* we have problems freezing the queue if it's initializing */ |
| 87 | if (!blk_queue_dying(q) && |
| 88 | (!blk_queue_bypass(q) || !blk_queue_init_done(q))) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 89 | return 0; |
| 90 | |
| 91 | __percpu_counter_add(&q->mq_usage_counter, -1, 1000000); |
| 92 | |
| 93 | spin_lock_irq(q->queue_lock); |
| 94 | ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq, |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 95 | !blk_queue_bypass(q) || blk_queue_dying(q), |
| 96 | *q->queue_lock); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 97 | /* inc usage with lock hold to avoid freeze_queue runs here */ |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 98 | if (!ret && !blk_queue_dying(q)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 99 | __percpu_counter_add(&q->mq_usage_counter, 1, 1000000); |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 100 | else if (blk_queue_dying(q)) |
| 101 | ret = -ENODEV; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 102 | spin_unlock_irq(q->queue_lock); |
| 103 | |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | static void blk_mq_queue_exit(struct request_queue *q) |
| 108 | { |
| 109 | __percpu_counter_add(&q->mq_usage_counter, -1, 1000000); |
| 110 | } |
| 111 | |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 112 | static void __blk_mq_drain_queue(struct request_queue *q) |
| 113 | { |
| 114 | while (true) { |
| 115 | s64 count; |
| 116 | |
| 117 | spin_lock_irq(q->queue_lock); |
| 118 | count = percpu_counter_sum(&q->mq_usage_counter); |
| 119 | spin_unlock_irq(q->queue_lock); |
| 120 | |
| 121 | if (count == 0) |
| 122 | break; |
| 123 | blk_mq_run_queues(q, false); |
| 124 | msleep(10); |
| 125 | } |
| 126 | } |
| 127 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 128 | /* |
| 129 | * Guarantee no request is in use, so we can change any data structure of |
| 130 | * the queue afterward. |
| 131 | */ |
| 132 | static void blk_mq_freeze_queue(struct request_queue *q) |
| 133 | { |
| 134 | bool drain; |
| 135 | |
| 136 | spin_lock_irq(q->queue_lock); |
| 137 | drain = !q->bypass_depth++; |
| 138 | queue_flag_set(QUEUE_FLAG_BYPASS, q); |
| 139 | spin_unlock_irq(q->queue_lock); |
| 140 | |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 141 | if (drain) |
| 142 | __blk_mq_drain_queue(q); |
| 143 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 144 | |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 145 | void blk_mq_drain_queue(struct request_queue *q) |
| 146 | { |
| 147 | __blk_mq_drain_queue(q); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static void blk_mq_unfreeze_queue(struct request_queue *q) |
| 151 | { |
| 152 | bool wake = false; |
| 153 | |
| 154 | spin_lock_irq(q->queue_lock); |
| 155 | if (!--q->bypass_depth) { |
| 156 | queue_flag_clear(QUEUE_FLAG_BYPASS, q); |
| 157 | wake = true; |
| 158 | } |
| 159 | WARN_ON_ONCE(q->bypass_depth < 0); |
| 160 | spin_unlock_irq(q->queue_lock); |
| 161 | if (wake) |
| 162 | wake_up_all(&q->mq_freeze_wq); |
| 163 | } |
| 164 | |
| 165 | bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx) |
| 166 | { |
| 167 | return blk_mq_has_free_tags(hctx->tags); |
| 168 | } |
| 169 | EXPORT_SYMBOL(blk_mq_can_queue); |
| 170 | |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 171 | static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx, |
| 172 | struct request *rq, unsigned int rw_flags) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 173 | { |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 174 | if (blk_queue_io_stat(q)) |
| 175 | rw_flags |= REQ_IO_STAT; |
| 176 | |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 177 | INIT_LIST_HEAD(&rq->queuelist); |
| 178 | /* csd/requeue_work/fifo_time is initialized before use */ |
| 179 | rq->q = q; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 180 | rq->mq_ctx = ctx; |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 181 | rq->cmd_flags |= rw_flags; |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 182 | /* do not touch atomic flags, it needs atomic ops against the timer */ |
| 183 | rq->cpu = -1; |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 184 | INIT_HLIST_NODE(&rq->hash); |
| 185 | RB_CLEAR_NODE(&rq->rb_node); |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 186 | rq->rq_disk = NULL; |
| 187 | rq->part = NULL; |
Jens Axboe | 3ee3237 | 2014-06-09 09:36:53 -0600 | [diff] [blame^] | 188 | rq->start_time = jiffies; |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 189 | #ifdef CONFIG_BLK_CGROUP |
| 190 | rq->rl = NULL; |
Ming Lei | 0fec08b | 2014-01-03 10:00:08 -0700 | [diff] [blame] | 191 | set_start_time_ns(rq); |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 192 | rq->io_start_time_ns = 0; |
| 193 | #endif |
| 194 | rq->nr_phys_segments = 0; |
| 195 | #if defined(CONFIG_BLK_DEV_INTEGRITY) |
| 196 | rq->nr_integrity_segments = 0; |
| 197 | #endif |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 198 | rq->special = NULL; |
| 199 | /* tag was already set */ |
| 200 | rq->errors = 0; |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 201 | |
| 202 | rq->extra_len = 0; |
| 203 | rq->sense_len = 0; |
| 204 | rq->resid_len = 0; |
| 205 | rq->sense = NULL; |
| 206 | |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 207 | INIT_LIST_HEAD(&rq->timeout_list); |
Jens Axboe | f6be4fb | 2014-06-06 11:03:48 -0600 | [diff] [blame] | 208 | rq->timeout = 0; |
| 209 | |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 210 | rq->end_io = NULL; |
| 211 | rq->end_io_data = NULL; |
| 212 | rq->next_rq = NULL; |
| 213 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 214 | ctx->rq_dispatched[rw_is_sync(rw_flags)]++; |
| 215 | } |
| 216 | |
Christoph Hellwig | 5dee857 | 2014-05-27 20:59:47 +0200 | [diff] [blame] | 217 | static struct request * |
Ming Lei | cb96a42c | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 218 | __blk_mq_alloc_request(struct blk_mq_alloc_data *data, int rw) |
Christoph Hellwig | 5dee857 | 2014-05-27 20:59:47 +0200 | [diff] [blame] | 219 | { |
| 220 | struct request *rq; |
| 221 | unsigned int tag; |
| 222 | |
Ming Lei | cb96a42c | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 223 | tag = blk_mq_get_tag(data); |
Christoph Hellwig | 5dee857 | 2014-05-27 20:59:47 +0200 | [diff] [blame] | 224 | if (tag != BLK_MQ_TAG_FAIL) { |
Ming Lei | cb96a42c | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 225 | rq = data->hctx->tags->rqs[tag]; |
Christoph Hellwig | 5dee857 | 2014-05-27 20:59:47 +0200 | [diff] [blame] | 226 | |
| 227 | rq->cmd_flags = 0; |
Ming Lei | cb96a42c | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 228 | if (blk_mq_tag_busy(data->hctx)) { |
Christoph Hellwig | 5dee857 | 2014-05-27 20:59:47 +0200 | [diff] [blame] | 229 | rq->cmd_flags = REQ_MQ_INFLIGHT; |
Ming Lei | cb96a42c | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 230 | atomic_inc(&data->hctx->nr_active); |
Christoph Hellwig | 5dee857 | 2014-05-27 20:59:47 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | rq->tag = tag; |
Ming Lei | cb96a42c | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 234 | blk_mq_rq_ctx_init(data->q, data->ctx, rq, rw); |
Christoph Hellwig | 5dee857 | 2014-05-27 20:59:47 +0200 | [diff] [blame] | 235 | return rq; |
| 236 | } |
| 237 | |
| 238 | return NULL; |
| 239 | } |
| 240 | |
Christoph Hellwig | 4ce01dd | 2014-05-27 20:59:46 +0200 | [diff] [blame] | 241 | struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp, |
| 242 | bool reserved) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 243 | { |
Christoph Hellwig | d852564 | 2014-05-27 20:59:50 +0200 | [diff] [blame] | 244 | struct blk_mq_ctx *ctx; |
| 245 | struct blk_mq_hw_ctx *hctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 246 | struct request *rq; |
Ming Lei | cb96a42c | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 247 | struct blk_mq_alloc_data alloc_data; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 248 | |
| 249 | if (blk_mq_queue_enter(q)) |
| 250 | return NULL; |
| 251 | |
Christoph Hellwig | d852564 | 2014-05-27 20:59:50 +0200 | [diff] [blame] | 252 | ctx = blk_mq_get_ctx(q); |
| 253 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
Ming Lei | cb96a42c | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 254 | blk_mq_set_alloc_data(&alloc_data, q, gfp & ~__GFP_WAIT, |
| 255 | reserved, ctx, hctx); |
Christoph Hellwig | d852564 | 2014-05-27 20:59:50 +0200 | [diff] [blame] | 256 | |
Ming Lei | cb96a42c | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 257 | rq = __blk_mq_alloc_request(&alloc_data, rw); |
Christoph Hellwig | d852564 | 2014-05-27 20:59:50 +0200 | [diff] [blame] | 258 | if (!rq && (gfp & __GFP_WAIT)) { |
| 259 | __blk_mq_run_hw_queue(hctx); |
| 260 | blk_mq_put_ctx(ctx); |
| 261 | |
| 262 | ctx = blk_mq_get_ctx(q); |
| 263 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
Ming Lei | cb96a42c | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 264 | blk_mq_set_alloc_data(&alloc_data, q, gfp, reserved, ctx, |
| 265 | hctx); |
| 266 | rq = __blk_mq_alloc_request(&alloc_data, rw); |
| 267 | ctx = alloc_data.ctx; |
Christoph Hellwig | d852564 | 2014-05-27 20:59:50 +0200 | [diff] [blame] | 268 | } |
| 269 | blk_mq_put_ctx(ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 270 | return rq; |
| 271 | } |
Jens Axboe | 4bb659b | 2014-05-09 09:36:49 -0600 | [diff] [blame] | 272 | EXPORT_SYMBOL(blk_mq_alloc_request); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 273 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 274 | static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx, |
| 275 | struct blk_mq_ctx *ctx, struct request *rq) |
| 276 | { |
| 277 | const int tag = rq->tag; |
| 278 | struct request_queue *q = rq->q; |
| 279 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 280 | if (rq->cmd_flags & REQ_MQ_INFLIGHT) |
| 281 | atomic_dec(&hctx->nr_active); |
| 282 | |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 283 | clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 284 | blk_mq_put_tag(hctx, tag, &ctx->last_tag); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 285 | blk_mq_queue_exit(q); |
| 286 | } |
| 287 | |
| 288 | void blk_mq_free_request(struct request *rq) |
| 289 | { |
| 290 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
| 291 | struct blk_mq_hw_ctx *hctx; |
| 292 | struct request_queue *q = rq->q; |
| 293 | |
| 294 | ctx->rq_completed[rq_is_sync(rq)]++; |
| 295 | |
| 296 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 297 | __blk_mq_free_request(hctx, ctx, rq); |
| 298 | } |
| 299 | |
Christoph Hellwig | 8727af4 | 2014-04-14 10:30:08 +0200 | [diff] [blame] | 300 | /* |
| 301 | * Clone all relevant state from a request that has been put on hold in |
| 302 | * the flush state machine into the preallocated flush request that hangs |
| 303 | * off the request queue. |
| 304 | * |
| 305 | * For a driver the flush request should be invisible, that's why we are |
| 306 | * impersonating the original request here. |
| 307 | */ |
| 308 | void blk_mq_clone_flush_request(struct request *flush_rq, |
| 309 | struct request *orig_rq) |
| 310 | { |
| 311 | struct blk_mq_hw_ctx *hctx = |
| 312 | orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu); |
| 313 | |
| 314 | flush_rq->mq_ctx = orig_rq->mq_ctx; |
| 315 | flush_rq->tag = orig_rq->tag; |
| 316 | memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq), |
| 317 | hctx->cmd_size); |
| 318 | } |
| 319 | |
Christoph Hellwig | 63151a4 | 2014-04-16 09:44:52 +0200 | [diff] [blame] | 320 | inline void __blk_mq_end_io(struct request *rq, int error) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 321 | { |
Ming Lei | 0d11e6a | 2013-12-05 10:50:39 -0700 | [diff] [blame] | 322 | blk_account_io_done(rq); |
| 323 | |
Christoph Hellwig | 91b6363 | 2014-04-16 09:44:53 +0200 | [diff] [blame] | 324 | if (rq->end_io) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 325 | rq->end_io(rq, error); |
Christoph Hellwig | 91b6363 | 2014-04-16 09:44:53 +0200 | [diff] [blame] | 326 | } else { |
| 327 | if (unlikely(blk_bidi_rq(rq))) |
| 328 | blk_mq_free_request(rq->next_rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 329 | blk_mq_free_request(rq); |
Christoph Hellwig | 91b6363 | 2014-04-16 09:44:53 +0200 | [diff] [blame] | 330 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 331 | } |
Christoph Hellwig | 63151a4 | 2014-04-16 09:44:52 +0200 | [diff] [blame] | 332 | EXPORT_SYMBOL(__blk_mq_end_io); |
| 333 | |
| 334 | void blk_mq_end_io(struct request *rq, int error) |
| 335 | { |
| 336 | if (blk_update_request(rq, error, blk_rq_bytes(rq))) |
| 337 | BUG(); |
| 338 | __blk_mq_end_io(rq, error); |
| 339 | } |
| 340 | EXPORT_SYMBOL(blk_mq_end_io); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 341 | |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 342 | static void __blk_mq_complete_request_remote(void *data) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 343 | { |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 344 | struct request *rq = data; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 345 | |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 346 | rq->q->softirq_done_fn(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 347 | } |
| 348 | |
Jens Axboe | ed85186 | 2014-05-30 21:20:50 -0600 | [diff] [blame] | 349 | static void blk_mq_ipi_complete_request(struct request *rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 350 | { |
| 351 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
Christoph Hellwig | 3853520 | 2014-04-25 02:32:53 -0700 | [diff] [blame] | 352 | bool shared = false; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 353 | int cpu; |
| 354 | |
Christoph Hellwig | 3853520 | 2014-04-25 02:32:53 -0700 | [diff] [blame] | 355 | if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) { |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 356 | rq->q->softirq_done_fn(rq); |
| 357 | return; |
| 358 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 359 | |
| 360 | cpu = get_cpu(); |
Christoph Hellwig | 3853520 | 2014-04-25 02:32:53 -0700 | [diff] [blame] | 361 | if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags)) |
| 362 | shared = cpus_share_cache(cpu, ctx->cpu); |
| 363 | |
| 364 | if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) { |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 365 | rq->csd.func = __blk_mq_complete_request_remote; |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 366 | rq->csd.info = rq; |
| 367 | rq->csd.flags = 0; |
Frederic Weisbecker | c46fff2 | 2014-02-24 16:40:02 +0100 | [diff] [blame] | 368 | smp_call_function_single_async(ctx->cpu, &rq->csd); |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 369 | } else { |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 370 | rq->q->softirq_done_fn(rq); |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 371 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 372 | put_cpu(); |
| 373 | } |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 374 | |
Jens Axboe | ed85186 | 2014-05-30 21:20:50 -0600 | [diff] [blame] | 375 | void __blk_mq_complete_request(struct request *rq) |
| 376 | { |
| 377 | struct request_queue *q = rq->q; |
| 378 | |
| 379 | if (!q->softirq_done_fn) |
| 380 | blk_mq_end_io(rq, rq->errors); |
| 381 | else |
| 382 | blk_mq_ipi_complete_request(rq); |
| 383 | } |
| 384 | |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 385 | /** |
| 386 | * blk_mq_complete_request - end I/O on a request |
| 387 | * @rq: the request being processed |
| 388 | * |
| 389 | * Description: |
| 390 | * Ends all I/O on a request. It does not handle partial completions. |
| 391 | * The actual completion happens out-of-order, through a IPI handler. |
| 392 | **/ |
| 393 | void blk_mq_complete_request(struct request *rq) |
| 394 | { |
Jens Axboe | 95f0968 | 2014-05-27 17:46:48 -0600 | [diff] [blame] | 395 | struct request_queue *q = rq->q; |
| 396 | |
| 397 | if (unlikely(blk_should_fake_timeout(q))) |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 398 | return; |
Jens Axboe | ed85186 | 2014-05-30 21:20:50 -0600 | [diff] [blame] | 399 | if (!blk_mark_rq_complete(rq)) |
| 400 | __blk_mq_complete_request(rq); |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 401 | } |
| 402 | EXPORT_SYMBOL(blk_mq_complete_request); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 403 | |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 404 | static void blk_mq_start_request(struct request *rq, bool last) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 405 | { |
| 406 | struct request_queue *q = rq->q; |
| 407 | |
| 408 | trace_block_rq_issue(q, rq); |
| 409 | |
Christoph Hellwig | 742ee69 | 2014-04-14 10:30:06 +0200 | [diff] [blame] | 410 | rq->resid_len = blk_rq_bytes(rq); |
Christoph Hellwig | 91b6363 | 2014-04-16 09:44:53 +0200 | [diff] [blame] | 411 | if (unlikely(blk_bidi_rq(rq))) |
| 412 | rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq); |
Christoph Hellwig | 742ee69 | 2014-04-14 10:30:06 +0200 | [diff] [blame] | 413 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 414 | /* |
| 415 | * Just mark start time and set the started bit. Due to memory |
| 416 | * ordering, we know we'll see the correct deadline as long as |
Jens Axboe | c22d9d8 | 2014-05-23 14:14:57 -0600 | [diff] [blame] | 417 | * REQ_ATOMIC_STARTED is seen. Use the default queue timeout, |
| 418 | * unless one has been set in the request. |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 419 | */ |
Jens Axboe | c22d9d8 | 2014-05-23 14:14:57 -0600 | [diff] [blame] | 420 | if (!rq->timeout) |
| 421 | rq->deadline = jiffies + q->rq_timeout; |
| 422 | else |
| 423 | rq->deadline = jiffies + rq->timeout; |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | * Mark us as started and clear complete. Complete might have been |
| 427 | * set if requeue raced with timeout, which then marked it as |
| 428 | * complete. So be sure to clear complete again when we start |
| 429 | * the request, otherwise we'll ignore the completion event. |
| 430 | */ |
Jens Axboe | 4b57052 | 2014-05-29 11:00:11 -0600 | [diff] [blame] | 431 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
| 432 | set_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
| 433 | if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags)) |
| 434 | clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags); |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 435 | |
| 436 | if (q->dma_drain_size && blk_rq_bytes(rq)) { |
| 437 | /* |
| 438 | * Make sure space for the drain appears. We know we can do |
| 439 | * this because max_hw_segments has been adjusted to be one |
| 440 | * fewer than the device can handle. |
| 441 | */ |
| 442 | rq->nr_phys_segments++; |
| 443 | } |
| 444 | |
| 445 | /* |
| 446 | * Flag the last request in the series so that drivers know when IO |
| 447 | * should be kicked off, if they don't do it on a per-request basis. |
| 448 | * |
| 449 | * Note: the flag isn't the only condition drivers should do kick off. |
| 450 | * If drive is busy, the last request might not have the bit set. |
| 451 | */ |
| 452 | if (last) |
| 453 | rq->cmd_flags |= REQ_END; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 456 | static void __blk_mq_requeue_request(struct request *rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 457 | { |
| 458 | struct request_queue *q = rq->q; |
| 459 | |
| 460 | trace_block_rq_requeue(q, rq); |
| 461 | clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 462 | |
| 463 | rq->cmd_flags &= ~REQ_END; |
| 464 | |
| 465 | if (q->dma_drain_size && blk_rq_bytes(rq)) |
| 466 | rq->nr_phys_segments--; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 467 | } |
| 468 | |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 469 | void blk_mq_requeue_request(struct request *rq) |
| 470 | { |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 471 | __blk_mq_requeue_request(rq); |
| 472 | blk_clear_rq_complete(rq); |
| 473 | |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 474 | BUG_ON(blk_queued_rq(rq)); |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame] | 475 | blk_mq_add_to_requeue_list(rq, true); |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 476 | } |
| 477 | EXPORT_SYMBOL(blk_mq_requeue_request); |
| 478 | |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame] | 479 | static void blk_mq_requeue_work(struct work_struct *work) |
| 480 | { |
| 481 | struct request_queue *q = |
| 482 | container_of(work, struct request_queue, requeue_work); |
| 483 | LIST_HEAD(rq_list); |
| 484 | struct request *rq, *next; |
| 485 | unsigned long flags; |
| 486 | |
| 487 | spin_lock_irqsave(&q->requeue_lock, flags); |
| 488 | list_splice_init(&q->requeue_list, &rq_list); |
| 489 | spin_unlock_irqrestore(&q->requeue_lock, flags); |
| 490 | |
| 491 | list_for_each_entry_safe(rq, next, &rq_list, queuelist) { |
| 492 | if (!(rq->cmd_flags & REQ_SOFTBARRIER)) |
| 493 | continue; |
| 494 | |
| 495 | rq->cmd_flags &= ~REQ_SOFTBARRIER; |
| 496 | list_del_init(&rq->queuelist); |
| 497 | blk_mq_insert_request(rq, true, false, false); |
| 498 | } |
| 499 | |
| 500 | while (!list_empty(&rq_list)) { |
| 501 | rq = list_entry(rq_list.next, struct request, queuelist); |
| 502 | list_del_init(&rq->queuelist); |
| 503 | blk_mq_insert_request(rq, false, false, false); |
| 504 | } |
| 505 | |
| 506 | blk_mq_run_queues(q, false); |
| 507 | } |
| 508 | |
| 509 | void blk_mq_add_to_requeue_list(struct request *rq, bool at_head) |
| 510 | { |
| 511 | struct request_queue *q = rq->q; |
| 512 | unsigned long flags; |
| 513 | |
| 514 | /* |
| 515 | * We abuse this flag that is otherwise used by the I/O scheduler to |
| 516 | * request head insertation from the workqueue. |
| 517 | */ |
| 518 | BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER); |
| 519 | |
| 520 | spin_lock_irqsave(&q->requeue_lock, flags); |
| 521 | if (at_head) { |
| 522 | rq->cmd_flags |= REQ_SOFTBARRIER; |
| 523 | list_add(&rq->queuelist, &q->requeue_list); |
| 524 | } else { |
| 525 | list_add_tail(&rq->queuelist, &q->requeue_list); |
| 526 | } |
| 527 | spin_unlock_irqrestore(&q->requeue_lock, flags); |
| 528 | } |
| 529 | EXPORT_SYMBOL(blk_mq_add_to_requeue_list); |
| 530 | |
| 531 | void blk_mq_kick_requeue_list(struct request_queue *q) |
| 532 | { |
| 533 | kblockd_schedule_work(&q->requeue_work); |
| 534 | } |
| 535 | EXPORT_SYMBOL(blk_mq_kick_requeue_list); |
| 536 | |
Jens Axboe | 0e62f51 | 2014-06-04 10:23:49 -0600 | [diff] [blame] | 537 | static inline bool is_flush_request(struct request *rq, unsigned int tag) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 538 | { |
Jens Axboe | 0e62f51 | 2014-06-04 10:23:49 -0600 | [diff] [blame] | 539 | return ((rq->cmd_flags & REQ_FLUSH_SEQ) && |
| 540 | rq->q->flush_rq->tag == tag); |
| 541 | } |
Shaohua Li | 2230237 | 2014-05-30 08:06:42 -0600 | [diff] [blame] | 542 | |
Jens Axboe | 0e62f51 | 2014-06-04 10:23:49 -0600 | [diff] [blame] | 543 | struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag) |
| 544 | { |
| 545 | struct request *rq = tags->rqs[tag]; |
Shaohua Li | 2230237 | 2014-05-30 08:06:42 -0600 | [diff] [blame] | 546 | |
Jens Axboe | 0e62f51 | 2014-06-04 10:23:49 -0600 | [diff] [blame] | 547 | if (!is_flush_request(rq, tag)) |
| 548 | return rq; |
| 549 | |
| 550 | return rq->q->flush_rq; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 551 | } |
| 552 | EXPORT_SYMBOL(blk_mq_tag_to_rq); |
| 553 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 554 | struct blk_mq_timeout_data { |
| 555 | struct blk_mq_hw_ctx *hctx; |
| 556 | unsigned long *next; |
| 557 | unsigned int *next_set; |
| 558 | }; |
| 559 | |
| 560 | static void blk_mq_timeout_check(void *__data, unsigned long *free_tags) |
| 561 | { |
| 562 | struct blk_mq_timeout_data *data = __data; |
| 563 | struct blk_mq_hw_ctx *hctx = data->hctx; |
| 564 | unsigned int tag; |
| 565 | |
| 566 | /* It may not be in flight yet (this is where |
| 567 | * the REQ_ATOMIC_STARTED flag comes in). The requests are |
| 568 | * statically allocated, so we know it's always safe to access the |
| 569 | * memory associated with a bit offset into ->rqs[]. |
| 570 | */ |
| 571 | tag = 0; |
| 572 | do { |
| 573 | struct request *rq; |
| 574 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 575 | tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag); |
| 576 | if (tag >= hctx->tags->nr_tags) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 577 | break; |
| 578 | |
Jens Axboe | 0e62f51 | 2014-06-04 10:23:49 -0600 | [diff] [blame] | 579 | rq = blk_mq_tag_to_rq(hctx->tags, tag++); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 580 | if (rq->q != hctx->queue) |
| 581 | continue; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 582 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
| 583 | continue; |
| 584 | |
| 585 | blk_rq_check_expired(rq, data->next, data->next_set); |
| 586 | } while (1); |
| 587 | } |
| 588 | |
| 589 | static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx, |
| 590 | unsigned long *next, |
| 591 | unsigned int *next_set) |
| 592 | { |
| 593 | struct blk_mq_timeout_data data = { |
| 594 | .hctx = hctx, |
| 595 | .next = next, |
| 596 | .next_set = next_set, |
| 597 | }; |
| 598 | |
| 599 | /* |
| 600 | * Ask the tagging code to iterate busy requests, so we can |
| 601 | * check them for timeout. |
| 602 | */ |
| 603 | blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data); |
| 604 | } |
| 605 | |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 606 | static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq) |
| 607 | { |
| 608 | struct request_queue *q = rq->q; |
| 609 | |
| 610 | /* |
| 611 | * We know that complete is set at this point. If STARTED isn't set |
| 612 | * anymore, then the request isn't active and the "timeout" should |
| 613 | * just be ignored. This can happen due to the bitflag ordering. |
| 614 | * Timeout first checks if STARTED is set, and if it is, assumes |
| 615 | * the request is active. But if we race with completion, then |
| 616 | * we both flags will get cleared. So check here again, and ignore |
| 617 | * a timeout event with a request that isn't active. |
| 618 | */ |
| 619 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
| 620 | return BLK_EH_NOT_HANDLED; |
| 621 | |
| 622 | if (!q->mq_ops->timeout) |
| 623 | return BLK_EH_RESET_TIMER; |
| 624 | |
| 625 | return q->mq_ops->timeout(rq); |
| 626 | } |
| 627 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 628 | static void blk_mq_rq_timer(unsigned long data) |
| 629 | { |
| 630 | struct request_queue *q = (struct request_queue *) data; |
| 631 | struct blk_mq_hw_ctx *hctx; |
| 632 | unsigned long next = 0; |
| 633 | int i, next_set = 0; |
| 634 | |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 635 | queue_for_each_hw_ctx(q, hctx, i) { |
| 636 | /* |
| 637 | * If not software queues are currently mapped to this |
| 638 | * hardware queue, there's nothing to check |
| 639 | */ |
| 640 | if (!hctx->nr_ctx || !hctx->tags) |
| 641 | continue; |
| 642 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 643 | blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set); |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 644 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 645 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 646 | if (next_set) { |
| 647 | next = blk_rq_timeout(round_jiffies_up(next)); |
| 648 | mod_timer(&q->timeout, next); |
| 649 | } else { |
| 650 | queue_for_each_hw_ctx(q, hctx, i) |
| 651 | blk_mq_tag_idle(hctx); |
| 652 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /* |
| 656 | * Reverse check our software queue for entries that we could potentially |
| 657 | * merge with. Currently includes a hand-wavy stop count of 8, to not spend |
| 658 | * too much time checking for merges. |
| 659 | */ |
| 660 | static bool blk_mq_attempt_merge(struct request_queue *q, |
| 661 | struct blk_mq_ctx *ctx, struct bio *bio) |
| 662 | { |
| 663 | struct request *rq; |
| 664 | int checked = 8; |
| 665 | |
| 666 | list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) { |
| 667 | int el_ret; |
| 668 | |
| 669 | if (!checked--) |
| 670 | break; |
| 671 | |
| 672 | if (!blk_rq_merge_ok(rq, bio)) |
| 673 | continue; |
| 674 | |
| 675 | el_ret = blk_try_merge(rq, bio); |
| 676 | if (el_ret == ELEVATOR_BACK_MERGE) { |
| 677 | if (bio_attempt_back_merge(q, rq, bio)) { |
| 678 | ctx->rq_merged++; |
| 679 | return true; |
| 680 | } |
| 681 | break; |
| 682 | } else if (el_ret == ELEVATOR_FRONT_MERGE) { |
| 683 | if (bio_attempt_front_merge(q, rq, bio)) { |
| 684 | ctx->rq_merged++; |
| 685 | return true; |
| 686 | } |
| 687 | break; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | return false; |
| 692 | } |
| 693 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 694 | /* |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 695 | * Process software queues that have been marked busy, splicing them |
| 696 | * to the for-dispatch |
| 697 | */ |
| 698 | static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list) |
| 699 | { |
| 700 | struct blk_mq_ctx *ctx; |
| 701 | int i; |
| 702 | |
| 703 | for (i = 0; i < hctx->ctx_map.map_size; i++) { |
| 704 | struct blk_align_bitmap *bm = &hctx->ctx_map.map[i]; |
| 705 | unsigned int off, bit; |
| 706 | |
| 707 | if (!bm->word) |
| 708 | continue; |
| 709 | |
| 710 | bit = 0; |
| 711 | off = i * hctx->ctx_map.bits_per_word; |
| 712 | do { |
| 713 | bit = find_next_bit(&bm->word, bm->depth, bit); |
| 714 | if (bit >= bm->depth) |
| 715 | break; |
| 716 | |
| 717 | ctx = hctx->ctxs[bit + off]; |
| 718 | clear_bit(bit, &bm->word); |
| 719 | spin_lock(&ctx->lock); |
| 720 | list_splice_tail_init(&ctx->rq_list, list); |
| 721 | spin_unlock(&ctx->lock); |
| 722 | |
| 723 | bit++; |
| 724 | } while (1); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | /* |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 729 | * Run this hardware queue, pulling any software queues mapped to it in. |
| 730 | * Note that this function currently has various problems around ordering |
| 731 | * of IO. In particular, we'd like FIFO behaviour on handling existing |
| 732 | * items on the hctx->dispatch list. Ignore that for now. |
| 733 | */ |
| 734 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 735 | { |
| 736 | struct request_queue *q = hctx->queue; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 737 | struct request *rq; |
| 738 | LIST_HEAD(rq_list); |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 739 | int queued; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 740 | |
Jens Axboe | fd1270d | 2014-04-16 09:23:48 -0600 | [diff] [blame] | 741 | WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 742 | |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 743 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 744 | return; |
| 745 | |
| 746 | hctx->run++; |
| 747 | |
| 748 | /* |
| 749 | * Touch any software queue that has pending entries. |
| 750 | */ |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 751 | flush_busy_ctxs(hctx, &rq_list); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 752 | |
| 753 | /* |
| 754 | * If we have previous entries on our dispatch list, grab them |
| 755 | * and stuff them at the front for more fair dispatch. |
| 756 | */ |
| 757 | if (!list_empty_careful(&hctx->dispatch)) { |
| 758 | spin_lock(&hctx->lock); |
| 759 | if (!list_empty(&hctx->dispatch)) |
| 760 | list_splice_init(&hctx->dispatch, &rq_list); |
| 761 | spin_unlock(&hctx->lock); |
| 762 | } |
| 763 | |
| 764 | /* |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 765 | * Now process all the entries, sending them to the driver. |
| 766 | */ |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 767 | queued = 0; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 768 | while (!list_empty(&rq_list)) { |
| 769 | int ret; |
| 770 | |
| 771 | rq = list_first_entry(&rq_list, struct request, queuelist); |
| 772 | list_del_init(&rq->queuelist); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 773 | |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 774 | blk_mq_start_request(rq, list_empty(&rq_list)); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 775 | |
| 776 | ret = q->mq_ops->queue_rq(hctx, rq); |
| 777 | switch (ret) { |
| 778 | case BLK_MQ_RQ_QUEUE_OK: |
| 779 | queued++; |
| 780 | continue; |
| 781 | case BLK_MQ_RQ_QUEUE_BUSY: |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 782 | list_add(&rq->queuelist, &rq_list); |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 783 | __blk_mq_requeue_request(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 784 | break; |
| 785 | default: |
| 786 | pr_err("blk-mq: bad return on queue: %d\n", ret); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 787 | case BLK_MQ_RQ_QUEUE_ERROR: |
Christoph Hellwig | 1e93b8c | 2014-02-11 08:27:13 -0800 | [diff] [blame] | 788 | rq->errors = -EIO; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 789 | blk_mq_end_io(rq, rq->errors); |
| 790 | break; |
| 791 | } |
| 792 | |
| 793 | if (ret == BLK_MQ_RQ_QUEUE_BUSY) |
| 794 | break; |
| 795 | } |
| 796 | |
| 797 | if (!queued) |
| 798 | hctx->dispatched[0]++; |
| 799 | else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1))) |
| 800 | hctx->dispatched[ilog2(queued) + 1]++; |
| 801 | |
| 802 | /* |
| 803 | * Any items that need requeuing? Stuff them into hctx->dispatch, |
| 804 | * that is where we will continue on next queue run. |
| 805 | */ |
| 806 | if (!list_empty(&rq_list)) { |
| 807 | spin_lock(&hctx->lock); |
| 808 | list_splice(&rq_list, &hctx->dispatch); |
| 809 | spin_unlock(&hctx->lock); |
| 810 | } |
| 811 | } |
| 812 | |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 813 | /* |
| 814 | * It'd be great if the workqueue API had a way to pass |
| 815 | * in a mask and had some smarts for more clever placement. |
| 816 | * For now we just round-robin here, switching for every |
| 817 | * BLK_MQ_CPU_WORK_BATCH queued items. |
| 818 | */ |
| 819 | static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) |
| 820 | { |
| 821 | int cpu = hctx->next_cpu; |
| 822 | |
| 823 | if (--hctx->next_cpu_batch <= 0) { |
| 824 | int next_cpu; |
| 825 | |
| 826 | next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask); |
| 827 | if (next_cpu >= nr_cpu_ids) |
| 828 | next_cpu = cpumask_first(hctx->cpumask); |
| 829 | |
| 830 | hctx->next_cpu = next_cpu; |
| 831 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
| 832 | } |
| 833 | |
| 834 | return cpu; |
| 835 | } |
| 836 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 837 | void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) |
| 838 | { |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 839 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 840 | return; |
| 841 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 842 | if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 843 | __blk_mq_run_hw_queue(hctx); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 844 | else if (hctx->queue->nr_hw_queues == 1) |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 845 | kblockd_schedule_delayed_work(&hctx->run_work, 0); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 846 | else { |
| 847 | unsigned int cpu; |
| 848 | |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 849 | cpu = blk_mq_hctx_next_cpu(hctx); |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 850 | kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 851 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | void blk_mq_run_queues(struct request_queue *q, bool async) |
| 855 | { |
| 856 | struct blk_mq_hw_ctx *hctx; |
| 857 | int i; |
| 858 | |
| 859 | queue_for_each_hw_ctx(q, hctx, i) { |
| 860 | if ((!blk_mq_hctx_has_pending(hctx) && |
| 861 | list_empty_careful(&hctx->dispatch)) || |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 862 | test_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 863 | continue; |
| 864 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 865 | preempt_disable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 866 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 867 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | EXPORT_SYMBOL(blk_mq_run_queues); |
| 871 | |
| 872 | void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 873 | { |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 874 | cancel_delayed_work(&hctx->run_work); |
| 875 | cancel_delayed_work(&hctx->delay_work); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 876 | set_bit(BLK_MQ_S_STOPPED, &hctx->state); |
| 877 | } |
| 878 | EXPORT_SYMBOL(blk_mq_stop_hw_queue); |
| 879 | |
Christoph Hellwig | 280d45f | 2013-10-25 14:45:58 +0100 | [diff] [blame] | 880 | void blk_mq_stop_hw_queues(struct request_queue *q) |
| 881 | { |
| 882 | struct blk_mq_hw_ctx *hctx; |
| 883 | int i; |
| 884 | |
| 885 | queue_for_each_hw_ctx(q, hctx, i) |
| 886 | blk_mq_stop_hw_queue(hctx); |
| 887 | } |
| 888 | EXPORT_SYMBOL(blk_mq_stop_hw_queues); |
| 889 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 890 | void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 891 | { |
| 892 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 893 | |
| 894 | preempt_disable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 895 | __blk_mq_run_hw_queue(hctx); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 896 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 897 | } |
| 898 | EXPORT_SYMBOL(blk_mq_start_hw_queue); |
| 899 | |
Christoph Hellwig | 2f26855 | 2014-04-16 09:44:56 +0200 | [diff] [blame] | 900 | void blk_mq_start_hw_queues(struct request_queue *q) |
| 901 | { |
| 902 | struct blk_mq_hw_ctx *hctx; |
| 903 | int i; |
| 904 | |
| 905 | queue_for_each_hw_ctx(q, hctx, i) |
| 906 | blk_mq_start_hw_queue(hctx); |
| 907 | } |
| 908 | EXPORT_SYMBOL(blk_mq_start_hw_queues); |
| 909 | |
| 910 | |
Christoph Hellwig | 1b4a325 | 2014-04-16 09:44:54 +0200 | [diff] [blame] | 911 | void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 912 | { |
| 913 | struct blk_mq_hw_ctx *hctx; |
| 914 | int i; |
| 915 | |
| 916 | queue_for_each_hw_ctx(q, hctx, i) { |
| 917 | if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
| 918 | continue; |
| 919 | |
| 920 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 921 | preempt_disable(); |
Christoph Hellwig | 1b4a325 | 2014-04-16 09:44:54 +0200 | [diff] [blame] | 922 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 923 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); |
| 927 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 928 | static void blk_mq_run_work_fn(struct work_struct *work) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 929 | { |
| 930 | struct blk_mq_hw_ctx *hctx; |
| 931 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 932 | hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 933 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 934 | __blk_mq_run_hw_queue(hctx); |
| 935 | } |
| 936 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 937 | static void blk_mq_delay_work_fn(struct work_struct *work) |
| 938 | { |
| 939 | struct blk_mq_hw_ctx *hctx; |
| 940 | |
| 941 | hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work); |
| 942 | |
| 943 | if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
| 944 | __blk_mq_run_hw_queue(hctx); |
| 945 | } |
| 946 | |
| 947 | void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) |
| 948 | { |
| 949 | unsigned long tmo = msecs_to_jiffies(msecs); |
| 950 | |
| 951 | if (hctx->queue->nr_hw_queues == 1) |
| 952 | kblockd_schedule_delayed_work(&hctx->delay_work, tmo); |
| 953 | else { |
| 954 | unsigned int cpu; |
| 955 | |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 956 | cpu = blk_mq_hctx_next_cpu(hctx); |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 957 | kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo); |
| 958 | } |
| 959 | } |
| 960 | EXPORT_SYMBOL(blk_mq_delay_queue); |
| 961 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 962 | static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 963 | struct request *rq, bool at_head) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 964 | { |
| 965 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
| 966 | |
Jens Axboe | 01b983c | 2013-11-19 18:59:10 -0700 | [diff] [blame] | 967 | trace_block_rq_insert(hctx->queue, rq); |
| 968 | |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 969 | if (at_head) |
| 970 | list_add(&rq->queuelist, &ctx->rq_list); |
| 971 | else |
| 972 | list_add_tail(&rq->queuelist, &ctx->rq_list); |
Jens Axboe | 4bb659b | 2014-05-09 09:36:49 -0600 | [diff] [blame] | 973 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 974 | blk_mq_hctx_mark_pending(hctx, ctx); |
| 975 | |
| 976 | /* |
| 977 | * We do this early, to ensure we are on the right CPU. |
| 978 | */ |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 979 | blk_add_timer(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 980 | } |
| 981 | |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 982 | void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue, |
| 983 | bool async) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 984 | { |
| 985 | struct request_queue *q = rq->q; |
| 986 | struct blk_mq_hw_ctx *hctx; |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 987 | struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 988 | |
| 989 | current_ctx = blk_mq_get_ctx(q); |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 990 | if (!cpu_online(ctx->cpu)) |
| 991 | rq->mq_ctx = ctx = current_ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 992 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 993 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 994 | |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 995 | if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) && |
| 996 | !(rq->cmd_flags & (REQ_FLUSH_SEQ))) { |
| 997 | blk_insert_flush(rq); |
| 998 | } else { |
| 999 | spin_lock(&ctx->lock); |
| 1000 | __blk_mq_insert_request(hctx, rq, at_head); |
| 1001 | spin_unlock(&ctx->lock); |
| 1002 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1003 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1004 | if (run_queue) |
| 1005 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1006 | |
| 1007 | blk_mq_put_ctx(current_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | static void blk_mq_insert_requests(struct request_queue *q, |
| 1011 | struct blk_mq_ctx *ctx, |
| 1012 | struct list_head *list, |
| 1013 | int depth, |
| 1014 | bool from_schedule) |
| 1015 | |
| 1016 | { |
| 1017 | struct blk_mq_hw_ctx *hctx; |
| 1018 | struct blk_mq_ctx *current_ctx; |
| 1019 | |
| 1020 | trace_block_unplug(q, depth, !from_schedule); |
| 1021 | |
| 1022 | current_ctx = blk_mq_get_ctx(q); |
| 1023 | |
| 1024 | if (!cpu_online(ctx->cpu)) |
| 1025 | ctx = current_ctx; |
| 1026 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 1027 | |
| 1028 | /* |
| 1029 | * preemption doesn't flush plug list, so it's possible ctx->cpu is |
| 1030 | * offline now |
| 1031 | */ |
| 1032 | spin_lock(&ctx->lock); |
| 1033 | while (!list_empty(list)) { |
| 1034 | struct request *rq; |
| 1035 | |
| 1036 | rq = list_first_entry(list, struct request, queuelist); |
| 1037 | list_del_init(&rq->queuelist); |
| 1038 | rq->mq_ctx = ctx; |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 1039 | __blk_mq_insert_request(hctx, rq, false); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1040 | } |
| 1041 | spin_unlock(&ctx->lock); |
| 1042 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1043 | blk_mq_run_hw_queue(hctx, from_schedule); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1044 | blk_mq_put_ctx(current_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1048 | { |
| 1049 | struct request *rqa = container_of(a, struct request, queuelist); |
| 1050 | struct request *rqb = container_of(b, struct request, queuelist); |
| 1051 | |
| 1052 | return !(rqa->mq_ctx < rqb->mq_ctx || |
| 1053 | (rqa->mq_ctx == rqb->mq_ctx && |
| 1054 | blk_rq_pos(rqa) < blk_rq_pos(rqb))); |
| 1055 | } |
| 1056 | |
| 1057 | void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) |
| 1058 | { |
| 1059 | struct blk_mq_ctx *this_ctx; |
| 1060 | struct request_queue *this_q; |
| 1061 | struct request *rq; |
| 1062 | LIST_HEAD(list); |
| 1063 | LIST_HEAD(ctx_list); |
| 1064 | unsigned int depth; |
| 1065 | |
| 1066 | list_splice_init(&plug->mq_list, &list); |
| 1067 | |
| 1068 | list_sort(NULL, &list, plug_ctx_cmp); |
| 1069 | |
| 1070 | this_q = NULL; |
| 1071 | this_ctx = NULL; |
| 1072 | depth = 0; |
| 1073 | |
| 1074 | while (!list_empty(&list)) { |
| 1075 | rq = list_entry_rq(list.next); |
| 1076 | list_del_init(&rq->queuelist); |
| 1077 | BUG_ON(!rq->q); |
| 1078 | if (rq->mq_ctx != this_ctx) { |
| 1079 | if (this_ctx) { |
| 1080 | blk_mq_insert_requests(this_q, this_ctx, |
| 1081 | &ctx_list, depth, |
| 1082 | from_schedule); |
| 1083 | } |
| 1084 | |
| 1085 | this_ctx = rq->mq_ctx; |
| 1086 | this_q = rq->q; |
| 1087 | depth = 0; |
| 1088 | } |
| 1089 | |
| 1090 | depth++; |
| 1091 | list_add_tail(&rq->queuelist, &ctx_list); |
| 1092 | } |
| 1093 | |
| 1094 | /* |
| 1095 | * If 'this_ctx' is set, we know we have entries to complete |
| 1096 | * on 'ctx_list'. Do those. |
| 1097 | */ |
| 1098 | if (this_ctx) { |
| 1099 | blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth, |
| 1100 | from_schedule); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | static void blk_mq_bio_to_request(struct request *rq, struct bio *bio) |
| 1105 | { |
| 1106 | init_request_from_bio(rq, bio); |
Jens Axboe | 4b57052 | 2014-05-29 11:00:11 -0600 | [diff] [blame] | 1107 | |
Jens Axboe | 3ee3237 | 2014-06-09 09:36:53 -0600 | [diff] [blame^] | 1108 | if (blk_do_io_stat(rq)) |
Jens Axboe | 4b57052 | 2014-05-29 11:00:11 -0600 | [diff] [blame] | 1109 | blk_account_io_start(rq, 1); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1110 | } |
| 1111 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1112 | static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx, |
| 1113 | struct blk_mq_ctx *ctx, |
| 1114 | struct request *rq, struct bio *bio) |
| 1115 | { |
| 1116 | struct request_queue *q = hctx->queue; |
| 1117 | |
| 1118 | if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) { |
| 1119 | blk_mq_bio_to_request(rq, bio); |
| 1120 | spin_lock(&ctx->lock); |
| 1121 | insert_rq: |
| 1122 | __blk_mq_insert_request(hctx, rq, false); |
| 1123 | spin_unlock(&ctx->lock); |
| 1124 | return false; |
| 1125 | } else { |
| 1126 | spin_lock(&ctx->lock); |
| 1127 | if (!blk_mq_attempt_merge(q, ctx, bio)) { |
| 1128 | blk_mq_bio_to_request(rq, bio); |
| 1129 | goto insert_rq; |
| 1130 | } |
| 1131 | |
| 1132 | spin_unlock(&ctx->lock); |
| 1133 | __blk_mq_free_request(hctx, ctx, rq); |
| 1134 | return true; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | struct blk_map_ctx { |
| 1139 | struct blk_mq_hw_ctx *hctx; |
| 1140 | struct blk_mq_ctx *ctx; |
| 1141 | }; |
| 1142 | |
| 1143 | static struct request *blk_mq_map_request(struct request_queue *q, |
| 1144 | struct bio *bio, |
| 1145 | struct blk_map_ctx *data) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1146 | { |
| 1147 | struct blk_mq_hw_ctx *hctx; |
| 1148 | struct blk_mq_ctx *ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1149 | struct request *rq; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1150 | int rw = bio_data_dir(bio); |
Ming Lei | cb96a42c | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 1151 | struct blk_mq_alloc_data alloc_data; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1152 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1153 | if (unlikely(blk_mq_queue_enter(q))) { |
Nicholas Bellinger | 14ec77f | 2014-02-07 13:45:39 -0700 | [diff] [blame] | 1154 | bio_endio(bio, -EIO); |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1155 | return NULL; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | ctx = blk_mq_get_ctx(q); |
| 1159 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 1160 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1161 | if (rw_is_sync(bio->bi_rw)) |
Shaohua Li | 27fbf4e | 2014-02-19 20:20:21 +0800 | [diff] [blame] | 1162 | rw |= REQ_SYNC; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1163 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1164 | trace_block_getrq(q, bio, rw); |
Ming Lei | cb96a42c | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 1165 | blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx, |
| 1166 | hctx); |
| 1167 | rq = __blk_mq_alloc_request(&alloc_data, rw); |
Christoph Hellwig | 5dee857 | 2014-05-27 20:59:47 +0200 | [diff] [blame] | 1168 | if (unlikely(!rq)) { |
Christoph Hellwig | 793597a | 2014-05-27 20:59:49 +0200 | [diff] [blame] | 1169 | __blk_mq_run_hw_queue(hctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1170 | blk_mq_put_ctx(ctx); |
| 1171 | trace_block_sleeprq(q, bio, rw); |
Christoph Hellwig | 793597a | 2014-05-27 20:59:49 +0200 | [diff] [blame] | 1172 | |
| 1173 | ctx = blk_mq_get_ctx(q); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1174 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
Ming Lei | cb96a42c | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 1175 | blk_mq_set_alloc_data(&alloc_data, q, |
| 1176 | __GFP_WAIT|GFP_ATOMIC, false, ctx, hctx); |
| 1177 | rq = __blk_mq_alloc_request(&alloc_data, rw); |
| 1178 | ctx = alloc_data.ctx; |
| 1179 | hctx = alloc_data.hctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | hctx->queued++; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1183 | data->hctx = hctx; |
| 1184 | data->ctx = ctx; |
| 1185 | return rq; |
| 1186 | } |
| 1187 | |
| 1188 | /* |
| 1189 | * Multiple hardware queue variant. This will not use per-process plugs, |
| 1190 | * but will attempt to bypass the hctx queueing if we can go straight to |
| 1191 | * hardware for SYNC IO. |
| 1192 | */ |
| 1193 | static void blk_mq_make_request(struct request_queue *q, struct bio *bio) |
| 1194 | { |
| 1195 | const int is_sync = rw_is_sync(bio->bi_rw); |
| 1196 | const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA); |
| 1197 | struct blk_map_ctx data; |
| 1198 | struct request *rq; |
| 1199 | |
| 1200 | blk_queue_bounce(q, &bio); |
| 1201 | |
| 1202 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { |
| 1203 | bio_endio(bio, -EIO); |
| 1204 | return; |
| 1205 | } |
| 1206 | |
| 1207 | rq = blk_mq_map_request(q, bio, &data); |
| 1208 | if (unlikely(!rq)) |
| 1209 | return; |
| 1210 | |
| 1211 | if (unlikely(is_flush_fua)) { |
| 1212 | blk_mq_bio_to_request(rq, bio); |
| 1213 | blk_insert_flush(rq); |
| 1214 | goto run_queue; |
| 1215 | } |
| 1216 | |
| 1217 | if (is_sync) { |
| 1218 | int ret; |
| 1219 | |
| 1220 | blk_mq_bio_to_request(rq, bio); |
| 1221 | blk_mq_start_request(rq, true); |
Jens Axboe | feff689 | 2014-05-30 15:42:56 -0600 | [diff] [blame] | 1222 | blk_add_timer(rq); |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1223 | |
| 1224 | /* |
| 1225 | * For OK queue, we are done. For error, kill it. Any other |
| 1226 | * error (busy), just add it to our list as we previously |
| 1227 | * would have done |
| 1228 | */ |
| 1229 | ret = q->mq_ops->queue_rq(data.hctx, rq); |
| 1230 | if (ret == BLK_MQ_RQ_QUEUE_OK) |
| 1231 | goto done; |
| 1232 | else { |
| 1233 | __blk_mq_requeue_request(rq); |
| 1234 | |
| 1235 | if (ret == BLK_MQ_RQ_QUEUE_ERROR) { |
| 1236 | rq->errors = -EIO; |
| 1237 | blk_mq_end_io(rq, rq->errors); |
| 1238 | goto done; |
| 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) { |
| 1244 | /* |
| 1245 | * For a SYNC request, send it to the hardware immediately. For |
| 1246 | * an ASYNC request, just ensure that we run it later on. The |
| 1247 | * latter allows for merging opportunities and more efficient |
| 1248 | * dispatching. |
| 1249 | */ |
| 1250 | run_queue: |
| 1251 | blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua); |
| 1252 | } |
| 1253 | done: |
| 1254 | blk_mq_put_ctx(data.ctx); |
| 1255 | } |
| 1256 | |
| 1257 | /* |
| 1258 | * Single hardware queue variant. This will attempt to use any per-process |
| 1259 | * plug for merging and IO deferral. |
| 1260 | */ |
| 1261 | static void blk_sq_make_request(struct request_queue *q, struct bio *bio) |
| 1262 | { |
| 1263 | const int is_sync = rw_is_sync(bio->bi_rw); |
| 1264 | const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA); |
| 1265 | unsigned int use_plug, request_count = 0; |
| 1266 | struct blk_map_ctx data; |
| 1267 | struct request *rq; |
| 1268 | |
| 1269 | /* |
| 1270 | * If we have multiple hardware queues, just go directly to |
| 1271 | * one of those for sync IO. |
| 1272 | */ |
| 1273 | use_plug = !is_flush_fua && !is_sync; |
| 1274 | |
| 1275 | blk_queue_bounce(q, &bio); |
| 1276 | |
| 1277 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { |
| 1278 | bio_endio(bio, -EIO); |
| 1279 | return; |
| 1280 | } |
| 1281 | |
| 1282 | if (use_plug && !blk_queue_nomerges(q) && |
| 1283 | blk_attempt_plug_merge(q, bio, &request_count)) |
| 1284 | return; |
| 1285 | |
| 1286 | rq = blk_mq_map_request(q, bio, &data); |
Jens Axboe | ff87bce | 2014-06-03 11:59:49 -0600 | [diff] [blame] | 1287 | if (unlikely(!rq)) |
| 1288 | return; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1289 | |
| 1290 | if (unlikely(is_flush_fua)) { |
| 1291 | blk_mq_bio_to_request(rq, bio); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1292 | blk_insert_flush(rq); |
| 1293 | goto run_queue; |
| 1294 | } |
| 1295 | |
| 1296 | /* |
| 1297 | * A task plug currently exists. Since this is completely lockless, |
| 1298 | * utilize that to temporarily store requests until the task is |
| 1299 | * either done or scheduled away. |
| 1300 | */ |
| 1301 | if (use_plug) { |
| 1302 | struct blk_plug *plug = current->plug; |
| 1303 | |
| 1304 | if (plug) { |
| 1305 | blk_mq_bio_to_request(rq, bio); |
Shaohua Li | 92f399c | 2013-10-29 12:01:03 -0600 | [diff] [blame] | 1306 | if (list_empty(&plug->mq_list)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1307 | trace_block_plug(q); |
| 1308 | else if (request_count >= BLK_MAX_REQUEST_COUNT) { |
| 1309 | blk_flush_plug_list(plug, false); |
| 1310 | trace_block_plug(q); |
| 1311 | } |
| 1312 | list_add_tail(&rq->queuelist, &plug->mq_list); |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1313 | blk_mq_put_ctx(data.ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1314 | return; |
| 1315 | } |
| 1316 | } |
| 1317 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1318 | if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) { |
| 1319 | /* |
| 1320 | * For a SYNC request, send it to the hardware immediately. For |
| 1321 | * an ASYNC request, just ensure that we run it later on. The |
| 1322 | * latter allows for merging opportunities and more efficient |
| 1323 | * dispatching. |
| 1324 | */ |
| 1325 | run_queue: |
| 1326 | blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1327 | } |
| 1328 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1329 | blk_mq_put_ctx(data.ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | /* |
| 1333 | * Default mapping to a software queue, since we use one per CPU. |
| 1334 | */ |
| 1335 | struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu) |
| 1336 | { |
| 1337 | return q->queue_hw_ctx[q->mq_map[cpu]]; |
| 1338 | } |
| 1339 | EXPORT_SYMBOL(blk_mq_map_queue); |
| 1340 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1341 | static void blk_mq_free_rq_map(struct blk_mq_tag_set *set, |
| 1342 | struct blk_mq_tags *tags, unsigned int hctx_idx) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1343 | { |
| 1344 | struct page *page; |
| 1345 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1346 | if (tags->rqs && set->ops->exit_request) { |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1347 | int i; |
| 1348 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1349 | for (i = 0; i < tags->nr_tags; i++) { |
| 1350 | if (!tags->rqs[i]) |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1351 | continue; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1352 | set->ops->exit_request(set->driver_data, tags->rqs[i], |
| 1353 | hctx_idx, i); |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1354 | } |
| 1355 | } |
| 1356 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1357 | while (!list_empty(&tags->page_list)) { |
| 1358 | page = list_first_entry(&tags->page_list, struct page, lru); |
Dave Hansen | 6753471 | 2014-01-08 20:17:46 -0700 | [diff] [blame] | 1359 | list_del_init(&page->lru); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1360 | __free_pages(page, page->private); |
| 1361 | } |
| 1362 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1363 | kfree(tags->rqs); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1364 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1365 | blk_mq_free_tags(tags); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | static size_t order_to_size(unsigned int order) |
| 1369 | { |
Ming Lei | 4ca0850 | 2014-04-19 18:00:18 +0800 | [diff] [blame] | 1370 | return (size_t)PAGE_SIZE << order; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1371 | } |
| 1372 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1373 | static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set, |
| 1374 | unsigned int hctx_idx) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1375 | { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1376 | struct blk_mq_tags *tags; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1377 | unsigned int i, j, entries_per_page, max_order = 4; |
| 1378 | size_t rq_size, left; |
| 1379 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1380 | tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags, |
| 1381 | set->numa_node); |
| 1382 | if (!tags) |
| 1383 | return NULL; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1384 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1385 | INIT_LIST_HEAD(&tags->page_list); |
| 1386 | |
| 1387 | tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *), |
| 1388 | GFP_KERNEL, set->numa_node); |
| 1389 | if (!tags->rqs) { |
| 1390 | blk_mq_free_tags(tags); |
| 1391 | return NULL; |
| 1392 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1393 | |
| 1394 | /* |
| 1395 | * rq_size is the size of the request plus driver payload, rounded |
| 1396 | * to the cacheline size |
| 1397 | */ |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1398 | rq_size = round_up(sizeof(struct request) + set->cmd_size, |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1399 | cache_line_size()); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1400 | left = rq_size * set->queue_depth; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1401 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1402 | for (i = 0; i < set->queue_depth; ) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1403 | int this_order = max_order; |
| 1404 | struct page *page; |
| 1405 | int to_do; |
| 1406 | void *p; |
| 1407 | |
| 1408 | while (left < order_to_size(this_order - 1) && this_order) |
| 1409 | this_order--; |
| 1410 | |
| 1411 | do { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1412 | page = alloc_pages_node(set->numa_node, GFP_KERNEL, |
| 1413 | this_order); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1414 | if (page) |
| 1415 | break; |
| 1416 | if (!this_order--) |
| 1417 | break; |
| 1418 | if (order_to_size(this_order) < rq_size) |
| 1419 | break; |
| 1420 | } while (1); |
| 1421 | |
| 1422 | if (!page) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1423 | goto fail; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1424 | |
| 1425 | page->private = this_order; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1426 | list_add_tail(&page->lru, &tags->page_list); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1427 | |
| 1428 | p = page_address(page); |
| 1429 | entries_per_page = order_to_size(this_order) / rq_size; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1430 | to_do = min(entries_per_page, set->queue_depth - i); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1431 | left -= to_do * rq_size; |
| 1432 | for (j = 0; j < to_do; j++) { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1433 | tags->rqs[i] = p; |
| 1434 | if (set->ops->init_request) { |
| 1435 | if (set->ops->init_request(set->driver_data, |
| 1436 | tags->rqs[i], hctx_idx, i, |
| 1437 | set->numa_node)) |
| 1438 | goto fail; |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1439 | } |
| 1440 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1441 | p += rq_size; |
| 1442 | i++; |
| 1443 | } |
| 1444 | } |
| 1445 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1446 | return tags; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1447 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1448 | fail: |
| 1449 | pr_warn("%s: failed to allocate requests\n", __func__); |
| 1450 | blk_mq_free_rq_map(set, tags, hctx_idx); |
| 1451 | return NULL; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1452 | } |
| 1453 | |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 1454 | static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap) |
| 1455 | { |
| 1456 | kfree(bitmap->map); |
| 1457 | } |
| 1458 | |
| 1459 | static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node) |
| 1460 | { |
| 1461 | unsigned int bpw = 8, total, num_maps, i; |
| 1462 | |
| 1463 | bitmap->bits_per_word = bpw; |
| 1464 | |
| 1465 | num_maps = ALIGN(nr_cpu_ids, bpw) / bpw; |
| 1466 | bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap), |
| 1467 | GFP_KERNEL, node); |
| 1468 | if (!bitmap->map) |
| 1469 | return -ENOMEM; |
| 1470 | |
| 1471 | bitmap->map_size = num_maps; |
| 1472 | |
| 1473 | total = nr_cpu_ids; |
| 1474 | for (i = 0; i < num_maps; i++) { |
| 1475 | bitmap->map[i].depth = min(total, bitmap->bits_per_word); |
| 1476 | total -= bitmap->map[i].depth; |
| 1477 | } |
| 1478 | |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1482 | static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu) |
| 1483 | { |
| 1484 | struct request_queue *q = hctx->queue; |
| 1485 | struct blk_mq_ctx *ctx; |
| 1486 | LIST_HEAD(tmp); |
| 1487 | |
| 1488 | /* |
| 1489 | * Move ctx entries to new CPU, if this one is going away. |
| 1490 | */ |
| 1491 | ctx = __blk_mq_get_ctx(q, cpu); |
| 1492 | |
| 1493 | spin_lock(&ctx->lock); |
| 1494 | if (!list_empty(&ctx->rq_list)) { |
| 1495 | list_splice_init(&ctx->rq_list, &tmp); |
| 1496 | blk_mq_hctx_clear_pending(hctx, ctx); |
| 1497 | } |
| 1498 | spin_unlock(&ctx->lock); |
| 1499 | |
| 1500 | if (list_empty(&tmp)) |
| 1501 | return NOTIFY_OK; |
| 1502 | |
| 1503 | ctx = blk_mq_get_ctx(q); |
| 1504 | spin_lock(&ctx->lock); |
| 1505 | |
| 1506 | while (!list_empty(&tmp)) { |
| 1507 | struct request *rq; |
| 1508 | |
| 1509 | rq = list_first_entry(&tmp, struct request, queuelist); |
| 1510 | rq->mq_ctx = ctx; |
| 1511 | list_move_tail(&rq->queuelist, &ctx->rq_list); |
| 1512 | } |
| 1513 | |
| 1514 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 1515 | blk_mq_hctx_mark_pending(hctx, ctx); |
| 1516 | |
| 1517 | spin_unlock(&ctx->lock); |
| 1518 | |
| 1519 | blk_mq_run_hw_queue(hctx, true); |
| 1520 | blk_mq_put_ctx(ctx); |
| 1521 | return NOTIFY_OK; |
| 1522 | } |
| 1523 | |
| 1524 | static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu) |
| 1525 | { |
| 1526 | struct request_queue *q = hctx->queue; |
| 1527 | struct blk_mq_tag_set *set = q->tag_set; |
| 1528 | |
| 1529 | if (set->tags[hctx->queue_num]) |
| 1530 | return NOTIFY_OK; |
| 1531 | |
| 1532 | set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num); |
| 1533 | if (!set->tags[hctx->queue_num]) |
| 1534 | return NOTIFY_STOP; |
| 1535 | |
| 1536 | hctx->tags = set->tags[hctx->queue_num]; |
| 1537 | return NOTIFY_OK; |
| 1538 | } |
| 1539 | |
| 1540 | static int blk_mq_hctx_notify(void *data, unsigned long action, |
| 1541 | unsigned int cpu) |
| 1542 | { |
| 1543 | struct blk_mq_hw_ctx *hctx = data; |
| 1544 | |
| 1545 | if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) |
| 1546 | return blk_mq_hctx_cpu_offline(hctx, cpu); |
| 1547 | else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) |
| 1548 | return blk_mq_hctx_cpu_online(hctx, cpu); |
| 1549 | |
| 1550 | return NOTIFY_OK; |
| 1551 | } |
| 1552 | |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1553 | static void blk_mq_exit_hw_queues(struct request_queue *q, |
| 1554 | struct blk_mq_tag_set *set, int nr_queue) |
| 1555 | { |
| 1556 | struct blk_mq_hw_ctx *hctx; |
| 1557 | unsigned int i; |
| 1558 | |
| 1559 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1560 | if (i == nr_queue) |
| 1561 | break; |
| 1562 | |
Jens Axboe | f899fed | 2014-06-04 09:11:53 -0600 | [diff] [blame] | 1563 | blk_mq_tag_idle(hctx); |
| 1564 | |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1565 | if (set->ops->exit_hctx) |
| 1566 | set->ops->exit_hctx(hctx, i); |
| 1567 | |
| 1568 | blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier); |
| 1569 | kfree(hctx->ctxs); |
| 1570 | blk_mq_free_bitmap(&hctx->ctx_map); |
| 1571 | } |
| 1572 | |
| 1573 | } |
| 1574 | |
| 1575 | static void blk_mq_free_hw_queues(struct request_queue *q, |
| 1576 | struct blk_mq_tag_set *set) |
| 1577 | { |
| 1578 | struct blk_mq_hw_ctx *hctx; |
| 1579 | unsigned int i; |
| 1580 | |
| 1581 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1582 | free_cpumask_var(hctx->cpumask); |
Christoph Hellwig | cdef54d | 2014-05-28 18:11:06 +0200 | [diff] [blame] | 1583 | kfree(hctx); |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1584 | } |
| 1585 | } |
| 1586 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1587 | static int blk_mq_init_hw_queues(struct request_queue *q, |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1588 | struct blk_mq_tag_set *set) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1589 | { |
| 1590 | struct blk_mq_hw_ctx *hctx; |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1591 | unsigned int i; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1592 | |
| 1593 | /* |
| 1594 | * Initialize hardware queues |
| 1595 | */ |
| 1596 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1597 | int node; |
| 1598 | |
| 1599 | node = hctx->numa_node; |
| 1600 | if (node == NUMA_NO_NODE) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1601 | node = hctx->numa_node = set->numa_node; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1602 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 1603 | INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn); |
| 1604 | INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1605 | spin_lock_init(&hctx->lock); |
| 1606 | INIT_LIST_HEAD(&hctx->dispatch); |
| 1607 | hctx->queue = q; |
| 1608 | hctx->queue_num = i; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1609 | hctx->flags = set->flags; |
| 1610 | hctx->cmd_size = set->cmd_size; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1611 | |
| 1612 | blk_mq_init_cpu_notifier(&hctx->cpu_notifier, |
| 1613 | blk_mq_hctx_notify, hctx); |
| 1614 | blk_mq_register_cpu_notifier(&hctx->cpu_notifier); |
| 1615 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1616 | hctx->tags = set->tags[i]; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1617 | |
| 1618 | /* |
| 1619 | * Allocate space for all possible cpus to avoid allocation in |
| 1620 | * runtime |
| 1621 | */ |
| 1622 | hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *), |
| 1623 | GFP_KERNEL, node); |
| 1624 | if (!hctx->ctxs) |
| 1625 | break; |
| 1626 | |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 1627 | if (blk_mq_alloc_bitmap(&hctx->ctx_map, node)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1628 | break; |
| 1629 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1630 | hctx->nr_ctx = 0; |
| 1631 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1632 | if (set->ops->init_hctx && |
| 1633 | set->ops->init_hctx(hctx, set->driver_data, i)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1634 | break; |
| 1635 | } |
| 1636 | |
| 1637 | if (i == q->nr_hw_queues) |
| 1638 | return 0; |
| 1639 | |
| 1640 | /* |
| 1641 | * Init failed |
| 1642 | */ |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1643 | blk_mq_exit_hw_queues(q, set, i); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1644 | |
| 1645 | return 1; |
| 1646 | } |
| 1647 | |
| 1648 | static void blk_mq_init_cpu_queues(struct request_queue *q, |
| 1649 | unsigned int nr_hw_queues) |
| 1650 | { |
| 1651 | unsigned int i; |
| 1652 | |
| 1653 | for_each_possible_cpu(i) { |
| 1654 | struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); |
| 1655 | struct blk_mq_hw_ctx *hctx; |
| 1656 | |
| 1657 | memset(__ctx, 0, sizeof(*__ctx)); |
| 1658 | __ctx->cpu = i; |
| 1659 | spin_lock_init(&__ctx->lock); |
| 1660 | INIT_LIST_HEAD(&__ctx->rq_list); |
| 1661 | __ctx->queue = q; |
| 1662 | |
| 1663 | /* If the cpu isn't online, the cpu is mapped to first hctx */ |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1664 | if (!cpu_online(i)) |
| 1665 | continue; |
| 1666 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1667 | hctx = q->mq_ops->map_queue(q, i); |
| 1668 | cpumask_set_cpu(i, hctx->cpumask); |
| 1669 | hctx->nr_ctx++; |
| 1670 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1671 | /* |
| 1672 | * Set local node, IFF we have more than one hw queue. If |
| 1673 | * not, we remain on the home node of the device |
| 1674 | */ |
| 1675 | if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) |
| 1676 | hctx->numa_node = cpu_to_node(i); |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | static void blk_mq_map_swqueue(struct request_queue *q) |
| 1681 | { |
| 1682 | unsigned int i; |
| 1683 | struct blk_mq_hw_ctx *hctx; |
| 1684 | struct blk_mq_ctx *ctx; |
| 1685 | |
| 1686 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1687 | cpumask_clear(hctx->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1688 | hctx->nr_ctx = 0; |
| 1689 | } |
| 1690 | |
| 1691 | /* |
| 1692 | * Map software to hardware queues |
| 1693 | */ |
| 1694 | queue_for_each_ctx(q, ctx, i) { |
| 1695 | /* If the cpu isn't online, the cpu is mapped to first hctx */ |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1696 | if (!cpu_online(i)) |
| 1697 | continue; |
| 1698 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1699 | hctx = q->mq_ops->map_queue(q, i); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1700 | cpumask_set_cpu(i, hctx->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1701 | ctx->index_hw = hctx->nr_ctx; |
| 1702 | hctx->ctxs[hctx->nr_ctx++] = ctx; |
| 1703 | } |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 1704 | |
| 1705 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1706 | /* |
| 1707 | * If not software queues are mapped to this hardware queue, |
| 1708 | * disable it and free the request entries |
| 1709 | */ |
| 1710 | if (!hctx->nr_ctx) { |
| 1711 | struct blk_mq_tag_set *set = q->tag_set; |
| 1712 | |
| 1713 | if (set->tags[i]) { |
| 1714 | blk_mq_free_rq_map(set, set->tags[i], i); |
| 1715 | set->tags[i] = NULL; |
| 1716 | hctx->tags = NULL; |
| 1717 | } |
| 1718 | continue; |
| 1719 | } |
| 1720 | |
| 1721 | /* |
| 1722 | * Initialize batch roundrobin counts |
| 1723 | */ |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 1724 | hctx->next_cpu = cpumask_first(hctx->cpumask); |
| 1725 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
| 1726 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1727 | } |
| 1728 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1729 | static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set) |
| 1730 | { |
| 1731 | struct blk_mq_hw_ctx *hctx; |
| 1732 | struct request_queue *q; |
| 1733 | bool shared; |
| 1734 | int i; |
| 1735 | |
| 1736 | if (set->tag_list.next == set->tag_list.prev) |
| 1737 | shared = false; |
| 1738 | else |
| 1739 | shared = true; |
| 1740 | |
| 1741 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
| 1742 | blk_mq_freeze_queue(q); |
| 1743 | |
| 1744 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1745 | if (shared) |
| 1746 | hctx->flags |= BLK_MQ_F_TAG_SHARED; |
| 1747 | else |
| 1748 | hctx->flags &= ~BLK_MQ_F_TAG_SHARED; |
| 1749 | } |
| 1750 | blk_mq_unfreeze_queue(q); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | static void blk_mq_del_queue_tag_set(struct request_queue *q) |
| 1755 | { |
| 1756 | struct blk_mq_tag_set *set = q->tag_set; |
| 1757 | |
| 1758 | blk_mq_freeze_queue(q); |
| 1759 | |
| 1760 | mutex_lock(&set->tag_list_lock); |
| 1761 | list_del_init(&q->tag_set_list); |
| 1762 | blk_mq_update_tag_set_depth(set); |
| 1763 | mutex_unlock(&set->tag_list_lock); |
| 1764 | |
| 1765 | blk_mq_unfreeze_queue(q); |
| 1766 | } |
| 1767 | |
| 1768 | static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set, |
| 1769 | struct request_queue *q) |
| 1770 | { |
| 1771 | q->tag_set = set; |
| 1772 | |
| 1773 | mutex_lock(&set->tag_list_lock); |
| 1774 | list_add_tail(&q->tag_set_list, &set->tag_list); |
| 1775 | blk_mq_update_tag_set_depth(set); |
| 1776 | mutex_unlock(&set->tag_list_lock); |
| 1777 | } |
| 1778 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1779 | struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1780 | { |
| 1781 | struct blk_mq_hw_ctx **hctxs; |
Ming Lei | e6cdb09 | 2014-06-03 11:24:06 +0800 | [diff] [blame] | 1782 | struct blk_mq_ctx __percpu *ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1783 | struct request_queue *q; |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1784 | unsigned int *map; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1785 | int i; |
| 1786 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1787 | ctx = alloc_percpu(struct blk_mq_ctx); |
| 1788 | if (!ctx) |
| 1789 | return ERR_PTR(-ENOMEM); |
| 1790 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1791 | hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL, |
| 1792 | set->numa_node); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1793 | |
| 1794 | if (!hctxs) |
| 1795 | goto err_percpu; |
| 1796 | |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1797 | map = blk_mq_make_queue_map(set); |
| 1798 | if (!map) |
| 1799 | goto err_map; |
| 1800 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1801 | for (i = 0; i < set->nr_hw_queues; i++) { |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1802 | int node = blk_mq_hw_queue_to_node(map, i); |
| 1803 | |
Christoph Hellwig | cdef54d | 2014-05-28 18:11:06 +0200 | [diff] [blame] | 1804 | hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx), |
| 1805 | GFP_KERNEL, node); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1806 | if (!hctxs[i]) |
| 1807 | goto err_hctxs; |
| 1808 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1809 | if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL)) |
| 1810 | goto err_hctxs; |
| 1811 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1812 | atomic_set(&hctxs[i]->nr_active, 0); |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1813 | hctxs[i]->numa_node = node; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1814 | hctxs[i]->queue_num = i; |
| 1815 | } |
| 1816 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1817 | q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1818 | if (!q) |
| 1819 | goto err_hctxs; |
| 1820 | |
Ming Lei | 3d2936f | 2014-05-27 23:35:14 +0800 | [diff] [blame] | 1821 | if (percpu_counter_init(&q->mq_usage_counter, 0)) |
| 1822 | goto err_map; |
| 1823 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1824 | setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q); |
| 1825 | blk_queue_rq_timeout(q, 30000); |
| 1826 | |
| 1827 | q->nr_queues = nr_cpu_ids; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1828 | q->nr_hw_queues = set->nr_hw_queues; |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1829 | q->mq_map = map; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1830 | |
| 1831 | q->queue_ctx = ctx; |
| 1832 | q->queue_hw_ctx = hctxs; |
| 1833 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1834 | q->mq_ops = set->ops; |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 1835 | q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1836 | |
Jens Axboe | 05f1dd5 | 2014-05-29 09:53:32 -0600 | [diff] [blame] | 1837 | if (!(set->flags & BLK_MQ_F_SG_MERGE)) |
| 1838 | q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE; |
| 1839 | |
Christoph Hellwig | 1be036e | 2014-02-07 10:22:39 -0800 | [diff] [blame] | 1840 | q->sg_reserved_size = INT_MAX; |
| 1841 | |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame] | 1842 | INIT_WORK(&q->requeue_work, blk_mq_requeue_work); |
| 1843 | INIT_LIST_HEAD(&q->requeue_list); |
| 1844 | spin_lock_init(&q->requeue_lock); |
| 1845 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1846 | if (q->nr_hw_queues > 1) |
| 1847 | blk_queue_make_request(q, blk_mq_make_request); |
| 1848 | else |
| 1849 | blk_queue_make_request(q, blk_sq_make_request); |
| 1850 | |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 1851 | blk_queue_rq_timed_out(q, blk_mq_rq_timed_out); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1852 | if (set->timeout) |
| 1853 | blk_queue_rq_timeout(q, set->timeout); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1854 | |
Jens Axboe | eba7176 | 2014-05-20 15:17:27 -0600 | [diff] [blame] | 1855 | /* |
| 1856 | * Do this after blk_queue_make_request() overrides it... |
| 1857 | */ |
| 1858 | q->nr_requests = set->queue_depth; |
| 1859 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1860 | if (set->ops->complete) |
| 1861 | blk_queue_softirq_done(q, set->ops->complete); |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 1862 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1863 | blk_mq_init_flush(q); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1864 | blk_mq_init_cpu_queues(q, set->nr_hw_queues); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1865 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1866 | q->flush_rq = kzalloc(round_up(sizeof(struct request) + |
| 1867 | set->cmd_size, cache_line_size()), |
| 1868 | GFP_KERNEL); |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1869 | if (!q->flush_rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1870 | goto err_hw; |
| 1871 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1872 | if (blk_mq_init_hw_queues(q, set)) |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1873 | goto err_flush_rq; |
| 1874 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1875 | mutex_lock(&all_q_mutex); |
| 1876 | list_add_tail(&q->all_q_node, &all_q_list); |
| 1877 | mutex_unlock(&all_q_mutex); |
| 1878 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1879 | blk_mq_add_queue_tag_set(set, q); |
| 1880 | |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1881 | blk_mq_map_swqueue(q); |
| 1882 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1883 | return q; |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1884 | |
| 1885 | err_flush_rq: |
| 1886 | kfree(q->flush_rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1887 | err_hw: |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1888 | blk_cleanup_queue(q); |
| 1889 | err_hctxs: |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1890 | kfree(map); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1891 | for (i = 0; i < set->nr_hw_queues; i++) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1892 | if (!hctxs[i]) |
| 1893 | break; |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1894 | free_cpumask_var(hctxs[i]->cpumask); |
Christoph Hellwig | cdef54d | 2014-05-28 18:11:06 +0200 | [diff] [blame] | 1895 | kfree(hctxs[i]); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1896 | } |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1897 | err_map: |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1898 | kfree(hctxs); |
| 1899 | err_percpu: |
| 1900 | free_percpu(ctx); |
| 1901 | return ERR_PTR(-ENOMEM); |
| 1902 | } |
| 1903 | EXPORT_SYMBOL(blk_mq_init_queue); |
| 1904 | |
| 1905 | void blk_mq_free_queue(struct request_queue *q) |
| 1906 | { |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1907 | struct blk_mq_tag_set *set = q->tag_set; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1908 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1909 | blk_mq_del_queue_tag_set(q); |
| 1910 | |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1911 | blk_mq_exit_hw_queues(q, set, set->nr_hw_queues); |
| 1912 | blk_mq_free_hw_queues(q, set); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1913 | |
Ming Lei | 3d2936f | 2014-05-27 23:35:14 +0800 | [diff] [blame] | 1914 | percpu_counter_destroy(&q->mq_usage_counter); |
| 1915 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1916 | free_percpu(q->queue_ctx); |
| 1917 | kfree(q->queue_hw_ctx); |
| 1918 | kfree(q->mq_map); |
| 1919 | |
| 1920 | q->queue_ctx = NULL; |
| 1921 | q->queue_hw_ctx = NULL; |
| 1922 | q->mq_map = NULL; |
| 1923 | |
| 1924 | mutex_lock(&all_q_mutex); |
| 1925 | list_del_init(&q->all_q_node); |
| 1926 | mutex_unlock(&all_q_mutex); |
| 1927 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1928 | |
| 1929 | /* Basically redo blk_mq_init_queue with queue frozen */ |
Paul Gortmaker | f618ef7 | 2013-11-14 08:26:02 -0700 | [diff] [blame] | 1930 | static void blk_mq_queue_reinit(struct request_queue *q) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1931 | { |
| 1932 | blk_mq_freeze_queue(q); |
| 1933 | |
Jens Axboe | 67aec14 | 2014-05-30 08:25:36 -0600 | [diff] [blame] | 1934 | blk_mq_sysfs_unregister(q); |
| 1935 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1936 | blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues); |
| 1937 | |
| 1938 | /* |
| 1939 | * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe |
| 1940 | * we should change hctx numa_node according to new topology (this |
| 1941 | * involves free and re-allocate memory, worthy doing?) |
| 1942 | */ |
| 1943 | |
| 1944 | blk_mq_map_swqueue(q); |
| 1945 | |
Jens Axboe | 67aec14 | 2014-05-30 08:25:36 -0600 | [diff] [blame] | 1946 | blk_mq_sysfs_register(q); |
| 1947 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1948 | blk_mq_unfreeze_queue(q); |
| 1949 | } |
| 1950 | |
Paul Gortmaker | f618ef7 | 2013-11-14 08:26:02 -0700 | [diff] [blame] | 1951 | static int blk_mq_queue_reinit_notify(struct notifier_block *nb, |
| 1952 | unsigned long action, void *hcpu) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1953 | { |
| 1954 | struct request_queue *q; |
| 1955 | |
| 1956 | /* |
Jens Axboe | 9fccfed | 2014-05-08 14:50:19 -0600 | [diff] [blame] | 1957 | * Before new mappings are established, hotadded cpu might already |
| 1958 | * start handling requests. This doesn't break anything as we map |
| 1959 | * offline CPUs to first hardware queue. We will re-init the queue |
| 1960 | * below to get optimal settings. |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1961 | */ |
| 1962 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN && |
| 1963 | action != CPU_ONLINE && action != CPU_ONLINE_FROZEN) |
| 1964 | return NOTIFY_OK; |
| 1965 | |
| 1966 | mutex_lock(&all_q_mutex); |
| 1967 | list_for_each_entry(q, &all_q_list, all_q_node) |
| 1968 | blk_mq_queue_reinit(q); |
| 1969 | mutex_unlock(&all_q_mutex); |
| 1970 | return NOTIFY_OK; |
| 1971 | } |
| 1972 | |
Jens Axboe | a4391c6 | 2014-06-05 15:21:56 -0600 | [diff] [blame] | 1973 | /* |
| 1974 | * Alloc a tag set to be associated with one or more request queues. |
| 1975 | * May fail with EINVAL for various error conditions. May adjust the |
| 1976 | * requested depth down, if if it too large. In that case, the set |
| 1977 | * value will be stored in set->queue_depth. |
| 1978 | */ |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1979 | int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) |
| 1980 | { |
| 1981 | int i; |
| 1982 | |
| 1983 | if (!set->nr_hw_queues) |
| 1984 | return -EINVAL; |
Jens Axboe | a4391c6 | 2014-06-05 15:21:56 -0600 | [diff] [blame] | 1985 | if (!set->queue_depth) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1986 | return -EINVAL; |
| 1987 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) |
| 1988 | return -EINVAL; |
| 1989 | |
Christoph Hellwig | cdef54d | 2014-05-28 18:11:06 +0200 | [diff] [blame] | 1990 | if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1991 | return -EINVAL; |
| 1992 | |
Jens Axboe | a4391c6 | 2014-06-05 15:21:56 -0600 | [diff] [blame] | 1993 | if (set->queue_depth > BLK_MQ_MAX_DEPTH) { |
| 1994 | pr_info("blk-mq: reduced tag depth to %u\n", |
| 1995 | BLK_MQ_MAX_DEPTH); |
| 1996 | set->queue_depth = BLK_MQ_MAX_DEPTH; |
| 1997 | } |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1998 | |
Ming Lei | 48479005 | 2014-04-19 18:00:17 +0800 | [diff] [blame] | 1999 | set->tags = kmalloc_node(set->nr_hw_queues * |
| 2000 | sizeof(struct blk_mq_tags *), |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2001 | GFP_KERNEL, set->numa_node); |
| 2002 | if (!set->tags) |
| 2003 | goto out; |
| 2004 | |
| 2005 | for (i = 0; i < set->nr_hw_queues; i++) { |
| 2006 | set->tags[i] = blk_mq_init_rq_map(set, i); |
| 2007 | if (!set->tags[i]) |
| 2008 | goto out_unwind; |
| 2009 | } |
| 2010 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 2011 | mutex_init(&set->tag_list_lock); |
| 2012 | INIT_LIST_HEAD(&set->tag_list); |
| 2013 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2014 | return 0; |
| 2015 | |
| 2016 | out_unwind: |
| 2017 | while (--i >= 0) |
| 2018 | blk_mq_free_rq_map(set, set->tags[i], i); |
| 2019 | out: |
| 2020 | return -ENOMEM; |
| 2021 | } |
| 2022 | EXPORT_SYMBOL(blk_mq_alloc_tag_set); |
| 2023 | |
| 2024 | void blk_mq_free_tag_set(struct blk_mq_tag_set *set) |
| 2025 | { |
| 2026 | int i; |
| 2027 | |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 2028 | for (i = 0; i < set->nr_hw_queues; i++) { |
| 2029 | if (set->tags[i]) |
| 2030 | blk_mq_free_rq_map(set, set->tags[i], i); |
| 2031 | } |
| 2032 | |
Ming Lei | 981bd18 | 2014-04-24 00:07:34 +0800 | [diff] [blame] | 2033 | kfree(set->tags); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2034 | } |
| 2035 | EXPORT_SYMBOL(blk_mq_free_tag_set); |
| 2036 | |
Jens Axboe | e3a2b3f | 2014-05-20 11:49:02 -0600 | [diff] [blame] | 2037 | int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) |
| 2038 | { |
| 2039 | struct blk_mq_tag_set *set = q->tag_set; |
| 2040 | struct blk_mq_hw_ctx *hctx; |
| 2041 | int i, ret; |
| 2042 | |
| 2043 | if (!set || nr > set->queue_depth) |
| 2044 | return -EINVAL; |
| 2045 | |
| 2046 | ret = 0; |
| 2047 | queue_for_each_hw_ctx(q, hctx, i) { |
| 2048 | ret = blk_mq_tag_update_depth(hctx->tags, nr); |
| 2049 | if (ret) |
| 2050 | break; |
| 2051 | } |
| 2052 | |
| 2053 | if (!ret) |
| 2054 | q->nr_requests = nr; |
| 2055 | |
| 2056 | return ret; |
| 2057 | } |
| 2058 | |
Jens Axboe | 676141e | 2014-03-20 13:29:18 -0600 | [diff] [blame] | 2059 | void blk_mq_disable_hotplug(void) |
| 2060 | { |
| 2061 | mutex_lock(&all_q_mutex); |
| 2062 | } |
| 2063 | |
| 2064 | void blk_mq_enable_hotplug(void) |
| 2065 | { |
| 2066 | mutex_unlock(&all_q_mutex); |
| 2067 | } |
| 2068 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2069 | static int __init blk_mq_init(void) |
| 2070 | { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2071 | blk_mq_cpu_init(); |
| 2072 | |
| 2073 | /* Must be called after percpu_counter_hotcpu_callback() */ |
| 2074 | hotcpu_notifier(blk_mq_queue_reinit_notify, -10); |
| 2075 | |
| 2076 | return 0; |
| 2077 | } |
| 2078 | subsys_initcall(blk_mq_init); |