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