Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/module.h> |
| 3 | #include <linux/backing-dev.h> |
| 4 | #include <linux/bio.h> |
| 5 | #include <linux/blkdev.h> |
| 6 | #include <linux/mm.h> |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/workqueue.h> |
| 10 | #include <linux/smp.h> |
| 11 | #include <linux/llist.h> |
| 12 | #include <linux/list_sort.h> |
| 13 | #include <linux/cpu.h> |
| 14 | #include <linux/cache.h> |
| 15 | #include <linux/sched/sysctl.h> |
| 16 | #include <linux/delay.h> |
| 17 | |
| 18 | #include <trace/events/block.h> |
| 19 | |
| 20 | #include <linux/blk-mq.h> |
| 21 | #include "blk.h" |
| 22 | #include "blk-mq.h" |
| 23 | #include "blk-mq-tag.h" |
| 24 | |
| 25 | static DEFINE_MUTEX(all_q_mutex); |
| 26 | static LIST_HEAD(all_q_list); |
| 27 | |
| 28 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx); |
| 29 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 30 | static struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q, |
| 31 | unsigned int cpu) |
| 32 | { |
| 33 | return per_cpu_ptr(q->queue_ctx, cpu); |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * This assumes per-cpu software queueing queues. They could be per-node |
| 38 | * as well, for instance. For now this is hardcoded as-is. Note that we don't |
| 39 | * care about preemption, since we know the ctx's are persistent. This does |
| 40 | * mean that we can't rely on ctx always matching the currently running CPU. |
| 41 | */ |
| 42 | static struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q) |
| 43 | { |
| 44 | return __blk_mq_get_ctx(q, get_cpu()); |
| 45 | } |
| 46 | |
| 47 | static void blk_mq_put_ctx(struct blk_mq_ctx *ctx) |
| 48 | { |
| 49 | put_cpu(); |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Check if any of the ctx's have pending work in this hardware queue |
| 54 | */ |
| 55 | static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx) |
| 56 | { |
| 57 | unsigned int i; |
| 58 | |
| 59 | for (i = 0; i < hctx->nr_ctx_map; i++) |
| 60 | if (hctx->ctx_map[i]) |
| 61 | return true; |
| 62 | |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Mark this ctx as having pending work in this hardware queue |
| 68 | */ |
| 69 | static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx, |
| 70 | struct blk_mq_ctx *ctx) |
| 71 | { |
| 72 | if (!test_bit(ctx->index_hw, hctx->ctx_map)) |
| 73 | set_bit(ctx->index_hw, hctx->ctx_map); |
| 74 | } |
| 75 | |
Christoph Hellwig | 081241e | 2014-02-20 15:32:36 -0800 | [diff] [blame] | 76 | static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx, |
| 77 | gfp_t gfp, bool reserved) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 78 | { |
| 79 | struct request *rq; |
| 80 | unsigned int tag; |
| 81 | |
| 82 | tag = blk_mq_get_tag(hctx->tags, gfp, reserved); |
| 83 | if (tag != BLK_MQ_TAG_FAIL) { |
| 84 | rq = hctx->rqs[tag]; |
Christoph Hellwig | ed44832 | 2014-04-14 10:30:10 +0200 | [diff] [blame^] | 85 | blk_rq_init(hctx->queue, rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 86 | rq->tag = tag; |
| 87 | |
| 88 | return rq; |
| 89 | } |
| 90 | |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | static int blk_mq_queue_enter(struct request_queue *q) |
| 95 | { |
| 96 | int ret; |
| 97 | |
| 98 | __percpu_counter_add(&q->mq_usage_counter, 1, 1000000); |
| 99 | smp_wmb(); |
| 100 | /* we have problems to freeze the queue if it's initializing */ |
| 101 | if (!blk_queue_bypass(q) || !blk_queue_init_done(q)) |
| 102 | return 0; |
| 103 | |
| 104 | __percpu_counter_add(&q->mq_usage_counter, -1, 1000000); |
| 105 | |
| 106 | spin_lock_irq(q->queue_lock); |
| 107 | ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq, |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 108 | !blk_queue_bypass(q) || blk_queue_dying(q), |
| 109 | *q->queue_lock); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 110 | /* inc usage with lock hold to avoid freeze_queue runs here */ |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 111 | if (!ret && !blk_queue_dying(q)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 112 | __percpu_counter_add(&q->mq_usage_counter, 1, 1000000); |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 113 | else if (blk_queue_dying(q)) |
| 114 | ret = -ENODEV; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 115 | spin_unlock_irq(q->queue_lock); |
| 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static void blk_mq_queue_exit(struct request_queue *q) |
| 121 | { |
| 122 | __percpu_counter_add(&q->mq_usage_counter, -1, 1000000); |
| 123 | } |
| 124 | |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 125 | static void __blk_mq_drain_queue(struct request_queue *q) |
| 126 | { |
| 127 | while (true) { |
| 128 | s64 count; |
| 129 | |
| 130 | spin_lock_irq(q->queue_lock); |
| 131 | count = percpu_counter_sum(&q->mq_usage_counter); |
| 132 | spin_unlock_irq(q->queue_lock); |
| 133 | |
| 134 | if (count == 0) |
| 135 | break; |
| 136 | blk_mq_run_queues(q, false); |
| 137 | msleep(10); |
| 138 | } |
| 139 | } |
| 140 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 141 | /* |
| 142 | * Guarantee no request is in use, so we can change any data structure of |
| 143 | * the queue afterward. |
| 144 | */ |
| 145 | static void blk_mq_freeze_queue(struct request_queue *q) |
| 146 | { |
| 147 | bool drain; |
| 148 | |
| 149 | spin_lock_irq(q->queue_lock); |
| 150 | drain = !q->bypass_depth++; |
| 151 | queue_flag_set(QUEUE_FLAG_BYPASS, q); |
| 152 | spin_unlock_irq(q->queue_lock); |
| 153 | |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 154 | if (drain) |
| 155 | __blk_mq_drain_queue(q); |
| 156 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 157 | |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 158 | void blk_mq_drain_queue(struct request_queue *q) |
| 159 | { |
| 160 | __blk_mq_drain_queue(q); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static void blk_mq_unfreeze_queue(struct request_queue *q) |
| 164 | { |
| 165 | bool wake = false; |
| 166 | |
| 167 | spin_lock_irq(q->queue_lock); |
| 168 | if (!--q->bypass_depth) { |
| 169 | queue_flag_clear(QUEUE_FLAG_BYPASS, q); |
| 170 | wake = true; |
| 171 | } |
| 172 | WARN_ON_ONCE(q->bypass_depth < 0); |
| 173 | spin_unlock_irq(q->queue_lock); |
| 174 | if (wake) |
| 175 | wake_up_all(&q->mq_freeze_wq); |
| 176 | } |
| 177 | |
| 178 | bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx) |
| 179 | { |
| 180 | return blk_mq_has_free_tags(hctx->tags); |
| 181 | } |
| 182 | EXPORT_SYMBOL(blk_mq_can_queue); |
| 183 | |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 184 | static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx, |
| 185 | struct request *rq, unsigned int rw_flags) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 186 | { |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 187 | if (blk_queue_io_stat(q)) |
| 188 | rw_flags |= REQ_IO_STAT; |
| 189 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 190 | rq->mq_ctx = ctx; |
| 191 | rq->cmd_flags = rw_flags; |
Ming Lei | 0fec08b | 2014-01-03 10:00:08 -0700 | [diff] [blame] | 192 | rq->start_time = jiffies; |
| 193 | set_start_time_ns(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 194 | ctx->rq_dispatched[rw_is_sync(rw_flags)]++; |
| 195 | } |
| 196 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 197 | static struct request *blk_mq_alloc_request_pinned(struct request_queue *q, |
| 198 | int rw, gfp_t gfp, |
| 199 | bool reserved) |
| 200 | { |
| 201 | struct request *rq; |
| 202 | |
| 203 | do { |
| 204 | struct blk_mq_ctx *ctx = blk_mq_get_ctx(q); |
| 205 | struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 206 | |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 207 | rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 208 | if (rq) { |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 209 | blk_mq_rq_ctx_init(q, ctx, rq, rw); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 210 | break; |
Jeff Moyer | 959a35f | 2013-12-03 14:23:00 -0700 | [diff] [blame] | 211 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 212 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 213 | if (gfp & __GFP_WAIT) { |
| 214 | __blk_mq_run_hw_queue(hctx); |
| 215 | blk_mq_put_ctx(ctx); |
| 216 | } else { |
| 217 | blk_mq_put_ctx(ctx); |
Jeff Moyer | 959a35f | 2013-12-03 14:23:00 -0700 | [diff] [blame] | 218 | break; |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 219 | } |
Jeff Moyer | 959a35f | 2013-12-03 14:23:00 -0700 | [diff] [blame] | 220 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 221 | blk_mq_wait_for_tags(hctx->tags); |
| 222 | } while (1); |
| 223 | |
| 224 | return rq; |
| 225 | } |
| 226 | |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 227 | struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 228 | { |
| 229 | struct request *rq; |
| 230 | |
| 231 | if (blk_mq_queue_enter(q)) |
| 232 | return NULL; |
| 233 | |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 234 | rq = blk_mq_alloc_request_pinned(q, rw, gfp, false); |
Jeff Moyer | 959a35f | 2013-12-03 14:23:00 -0700 | [diff] [blame] | 235 | if (rq) |
| 236 | blk_mq_put_ctx(rq->mq_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 237 | return rq; |
| 238 | } |
| 239 | |
| 240 | struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw, |
| 241 | gfp_t gfp) |
| 242 | { |
| 243 | struct request *rq; |
| 244 | |
| 245 | if (blk_mq_queue_enter(q)) |
| 246 | return NULL; |
| 247 | |
| 248 | rq = blk_mq_alloc_request_pinned(q, rw, gfp, true); |
Jeff Moyer | 959a35f | 2013-12-03 14:23:00 -0700 | [diff] [blame] | 249 | if (rq) |
| 250 | blk_mq_put_ctx(rq->mq_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 251 | return rq; |
| 252 | } |
| 253 | EXPORT_SYMBOL(blk_mq_alloc_reserved_request); |
| 254 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 255 | static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx, |
| 256 | struct blk_mq_ctx *ctx, struct request *rq) |
| 257 | { |
| 258 | const int tag = rq->tag; |
| 259 | struct request_queue *q = rq->q; |
| 260 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 261 | blk_mq_put_tag(hctx->tags, tag); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 262 | blk_mq_queue_exit(q); |
| 263 | } |
| 264 | |
| 265 | void blk_mq_free_request(struct request *rq) |
| 266 | { |
| 267 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
| 268 | struct blk_mq_hw_ctx *hctx; |
| 269 | struct request_queue *q = rq->q; |
| 270 | |
| 271 | ctx->rq_completed[rq_is_sync(rq)]++; |
| 272 | |
| 273 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 274 | __blk_mq_free_request(hctx, ctx, rq); |
| 275 | } |
| 276 | |
Christoph Hellwig | 8727af4 | 2014-04-14 10:30:08 +0200 | [diff] [blame] | 277 | /* |
| 278 | * Clone all relevant state from a request that has been put on hold in |
| 279 | * the flush state machine into the preallocated flush request that hangs |
| 280 | * off the request queue. |
| 281 | * |
| 282 | * For a driver the flush request should be invisible, that's why we are |
| 283 | * impersonating the original request here. |
| 284 | */ |
| 285 | void blk_mq_clone_flush_request(struct request *flush_rq, |
| 286 | struct request *orig_rq) |
| 287 | { |
| 288 | struct blk_mq_hw_ctx *hctx = |
| 289 | orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu); |
| 290 | |
| 291 | flush_rq->mq_ctx = orig_rq->mq_ctx; |
| 292 | flush_rq->tag = orig_rq->tag; |
| 293 | memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq), |
| 294 | hctx->cmd_size); |
| 295 | } |
| 296 | |
Christoph Hellwig | 7237c74 | 2014-02-20 15:32:38 -0800 | [diff] [blame] | 297 | bool blk_mq_end_io_partial(struct request *rq, int error, unsigned int nr_bytes) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 298 | { |
Christoph Hellwig | 7237c74 | 2014-02-20 15:32:38 -0800 | [diff] [blame] | 299 | if (blk_update_request(rq, error, blk_rq_bytes(rq))) |
| 300 | return true; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 301 | |
Ming Lei | 0d11e6a | 2013-12-05 10:50:39 -0700 | [diff] [blame] | 302 | blk_account_io_done(rq); |
| 303 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 304 | if (rq->end_io) |
| 305 | rq->end_io(rq, error); |
| 306 | else |
| 307 | blk_mq_free_request(rq); |
Christoph Hellwig | 7237c74 | 2014-02-20 15:32:38 -0800 | [diff] [blame] | 308 | return false; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 309 | } |
Christoph Hellwig | 7237c74 | 2014-02-20 15:32:38 -0800 | [diff] [blame] | 310 | EXPORT_SYMBOL(blk_mq_end_io_partial); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 311 | |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 312 | static void __blk_mq_complete_request_remote(void *data) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 313 | { |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 314 | struct request *rq = data; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 315 | |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 316 | rq->q->softirq_done_fn(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 317 | } |
| 318 | |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 319 | void __blk_mq_complete_request(struct request *rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 320 | { |
| 321 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
| 322 | int cpu; |
| 323 | |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 324 | if (!ctx->ipi_redirect) { |
| 325 | rq->q->softirq_done_fn(rq); |
| 326 | return; |
| 327 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 328 | |
| 329 | cpu = get_cpu(); |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 330 | if (cpu != ctx->cpu && cpu_online(ctx->cpu)) { |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 331 | rq->csd.func = __blk_mq_complete_request_remote; |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 332 | rq->csd.info = rq; |
| 333 | rq->csd.flags = 0; |
Frederic Weisbecker | c46fff2 | 2014-02-24 16:40:02 +0100 | [diff] [blame] | 334 | smp_call_function_single_async(ctx->cpu, &rq->csd); |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 335 | } else { |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 336 | rq->q->softirq_done_fn(rq); |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 337 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 338 | put_cpu(); |
| 339 | } |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 340 | |
| 341 | /** |
| 342 | * blk_mq_complete_request - end I/O on a request |
| 343 | * @rq: the request being processed |
| 344 | * |
| 345 | * Description: |
| 346 | * Ends all I/O on a request. It does not handle partial completions. |
| 347 | * The actual completion happens out-of-order, through a IPI handler. |
| 348 | **/ |
| 349 | void blk_mq_complete_request(struct request *rq) |
| 350 | { |
| 351 | if (unlikely(blk_should_fake_timeout(rq->q))) |
| 352 | return; |
| 353 | if (!blk_mark_rq_complete(rq)) |
| 354 | __blk_mq_complete_request(rq); |
| 355 | } |
| 356 | EXPORT_SYMBOL(blk_mq_complete_request); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 357 | |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 358 | static void blk_mq_start_request(struct request *rq, bool last) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 359 | { |
| 360 | struct request_queue *q = rq->q; |
| 361 | |
| 362 | trace_block_rq_issue(q, rq); |
| 363 | |
Christoph Hellwig | 742ee69 | 2014-04-14 10:30:06 +0200 | [diff] [blame] | 364 | rq->resid_len = blk_rq_bytes(rq); |
| 365 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 366 | /* |
| 367 | * Just mark start time and set the started bit. Due to memory |
| 368 | * ordering, we know we'll see the correct deadline as long as |
| 369 | * REQ_ATOMIC_STARTED is seen. |
| 370 | */ |
| 371 | rq->deadline = jiffies + q->rq_timeout; |
| 372 | set_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 373 | |
| 374 | if (q->dma_drain_size && blk_rq_bytes(rq)) { |
| 375 | /* |
| 376 | * Make sure space for the drain appears. We know we can do |
| 377 | * this because max_hw_segments has been adjusted to be one |
| 378 | * fewer than the device can handle. |
| 379 | */ |
| 380 | rq->nr_phys_segments++; |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Flag the last request in the series so that drivers know when IO |
| 385 | * should be kicked off, if they don't do it on a per-request basis. |
| 386 | * |
| 387 | * Note: the flag isn't the only condition drivers should do kick off. |
| 388 | * If drive is busy, the last request might not have the bit set. |
| 389 | */ |
| 390 | if (last) |
| 391 | rq->cmd_flags |= REQ_END; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static void blk_mq_requeue_request(struct request *rq) |
| 395 | { |
| 396 | struct request_queue *q = rq->q; |
| 397 | |
| 398 | trace_block_rq_requeue(q, rq); |
| 399 | clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 400 | |
| 401 | rq->cmd_flags &= ~REQ_END; |
| 402 | |
| 403 | if (q->dma_drain_size && blk_rq_bytes(rq)) |
| 404 | rq->nr_phys_segments--; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | struct blk_mq_timeout_data { |
| 408 | struct blk_mq_hw_ctx *hctx; |
| 409 | unsigned long *next; |
| 410 | unsigned int *next_set; |
| 411 | }; |
| 412 | |
| 413 | static void blk_mq_timeout_check(void *__data, unsigned long *free_tags) |
| 414 | { |
| 415 | struct blk_mq_timeout_data *data = __data; |
| 416 | struct blk_mq_hw_ctx *hctx = data->hctx; |
| 417 | unsigned int tag; |
| 418 | |
| 419 | /* It may not be in flight yet (this is where |
| 420 | * the REQ_ATOMIC_STARTED flag comes in). The requests are |
| 421 | * statically allocated, so we know it's always safe to access the |
| 422 | * memory associated with a bit offset into ->rqs[]. |
| 423 | */ |
| 424 | tag = 0; |
| 425 | do { |
| 426 | struct request *rq; |
| 427 | |
| 428 | tag = find_next_zero_bit(free_tags, hctx->queue_depth, tag); |
| 429 | if (tag >= hctx->queue_depth) |
| 430 | break; |
| 431 | |
| 432 | rq = hctx->rqs[tag++]; |
| 433 | |
| 434 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
| 435 | continue; |
| 436 | |
| 437 | blk_rq_check_expired(rq, data->next, data->next_set); |
| 438 | } while (1); |
| 439 | } |
| 440 | |
| 441 | static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx, |
| 442 | unsigned long *next, |
| 443 | unsigned int *next_set) |
| 444 | { |
| 445 | struct blk_mq_timeout_data data = { |
| 446 | .hctx = hctx, |
| 447 | .next = next, |
| 448 | .next_set = next_set, |
| 449 | }; |
| 450 | |
| 451 | /* |
| 452 | * Ask the tagging code to iterate busy requests, so we can |
| 453 | * check them for timeout. |
| 454 | */ |
| 455 | blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data); |
| 456 | } |
| 457 | |
| 458 | static void blk_mq_rq_timer(unsigned long data) |
| 459 | { |
| 460 | struct request_queue *q = (struct request_queue *) data; |
| 461 | struct blk_mq_hw_ctx *hctx; |
| 462 | unsigned long next = 0; |
| 463 | int i, next_set = 0; |
| 464 | |
| 465 | queue_for_each_hw_ctx(q, hctx, i) |
| 466 | blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set); |
| 467 | |
| 468 | if (next_set) |
| 469 | mod_timer(&q->timeout, round_jiffies_up(next)); |
| 470 | } |
| 471 | |
| 472 | /* |
| 473 | * Reverse check our software queue for entries that we could potentially |
| 474 | * merge with. Currently includes a hand-wavy stop count of 8, to not spend |
| 475 | * too much time checking for merges. |
| 476 | */ |
| 477 | static bool blk_mq_attempt_merge(struct request_queue *q, |
| 478 | struct blk_mq_ctx *ctx, struct bio *bio) |
| 479 | { |
| 480 | struct request *rq; |
| 481 | int checked = 8; |
| 482 | |
| 483 | list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) { |
| 484 | int el_ret; |
| 485 | |
| 486 | if (!checked--) |
| 487 | break; |
| 488 | |
| 489 | if (!blk_rq_merge_ok(rq, bio)) |
| 490 | continue; |
| 491 | |
| 492 | el_ret = blk_try_merge(rq, bio); |
| 493 | if (el_ret == ELEVATOR_BACK_MERGE) { |
| 494 | if (bio_attempt_back_merge(q, rq, bio)) { |
| 495 | ctx->rq_merged++; |
| 496 | return true; |
| 497 | } |
| 498 | break; |
| 499 | } else if (el_ret == ELEVATOR_FRONT_MERGE) { |
| 500 | if (bio_attempt_front_merge(q, rq, bio)) { |
| 501 | ctx->rq_merged++; |
| 502 | return true; |
| 503 | } |
| 504 | break; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | return false; |
| 509 | } |
| 510 | |
| 511 | void blk_mq_add_timer(struct request *rq) |
| 512 | { |
| 513 | __blk_add_timer(rq, NULL); |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Run this hardware queue, pulling any software queues mapped to it in. |
| 518 | * Note that this function currently has various problems around ordering |
| 519 | * of IO. In particular, we'd like FIFO behaviour on handling existing |
| 520 | * items on the hctx->dispatch list. Ignore that for now. |
| 521 | */ |
| 522 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 523 | { |
| 524 | struct request_queue *q = hctx->queue; |
| 525 | struct blk_mq_ctx *ctx; |
| 526 | struct request *rq; |
| 527 | LIST_HEAD(rq_list); |
| 528 | int bit, queued; |
| 529 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 530 | WARN_ON(!preempt_count()); |
| 531 | |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 532 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 533 | return; |
| 534 | |
| 535 | hctx->run++; |
| 536 | |
| 537 | /* |
| 538 | * Touch any software queue that has pending entries. |
| 539 | */ |
| 540 | for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) { |
| 541 | clear_bit(bit, hctx->ctx_map); |
| 542 | ctx = hctx->ctxs[bit]; |
| 543 | BUG_ON(bit != ctx->index_hw); |
| 544 | |
| 545 | spin_lock(&ctx->lock); |
| 546 | list_splice_tail_init(&ctx->rq_list, &rq_list); |
| 547 | spin_unlock(&ctx->lock); |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * If we have previous entries on our dispatch list, grab them |
| 552 | * and stuff them at the front for more fair dispatch. |
| 553 | */ |
| 554 | if (!list_empty_careful(&hctx->dispatch)) { |
| 555 | spin_lock(&hctx->lock); |
| 556 | if (!list_empty(&hctx->dispatch)) |
| 557 | list_splice_init(&hctx->dispatch, &rq_list); |
| 558 | spin_unlock(&hctx->lock); |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * Delete and return all entries from our dispatch list |
| 563 | */ |
| 564 | queued = 0; |
| 565 | |
| 566 | /* |
| 567 | * Now process all the entries, sending them to the driver. |
| 568 | */ |
| 569 | while (!list_empty(&rq_list)) { |
| 570 | int ret; |
| 571 | |
| 572 | rq = list_first_entry(&rq_list, struct request, queuelist); |
| 573 | list_del_init(&rq->queuelist); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 574 | |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 575 | blk_mq_start_request(rq, list_empty(&rq_list)); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 576 | |
| 577 | ret = q->mq_ops->queue_rq(hctx, rq); |
| 578 | switch (ret) { |
| 579 | case BLK_MQ_RQ_QUEUE_OK: |
| 580 | queued++; |
| 581 | continue; |
| 582 | case BLK_MQ_RQ_QUEUE_BUSY: |
| 583 | /* |
| 584 | * FIXME: we should have a mechanism to stop the queue |
| 585 | * like blk_stop_queue, otherwise we will waste cpu |
| 586 | * time |
| 587 | */ |
| 588 | list_add(&rq->queuelist, &rq_list); |
| 589 | blk_mq_requeue_request(rq); |
| 590 | break; |
| 591 | default: |
| 592 | pr_err("blk-mq: bad return on queue: %d\n", ret); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 593 | case BLK_MQ_RQ_QUEUE_ERROR: |
Christoph Hellwig | 1e93b8c | 2014-02-11 08:27:13 -0800 | [diff] [blame] | 594 | rq->errors = -EIO; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 595 | blk_mq_end_io(rq, rq->errors); |
| 596 | break; |
| 597 | } |
| 598 | |
| 599 | if (ret == BLK_MQ_RQ_QUEUE_BUSY) |
| 600 | break; |
| 601 | } |
| 602 | |
| 603 | if (!queued) |
| 604 | hctx->dispatched[0]++; |
| 605 | else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1))) |
| 606 | hctx->dispatched[ilog2(queued) + 1]++; |
| 607 | |
| 608 | /* |
| 609 | * Any items that need requeuing? Stuff them into hctx->dispatch, |
| 610 | * that is where we will continue on next queue run. |
| 611 | */ |
| 612 | if (!list_empty(&rq_list)) { |
| 613 | spin_lock(&hctx->lock); |
| 614 | list_splice(&rq_list, &hctx->dispatch); |
| 615 | spin_unlock(&hctx->lock); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) |
| 620 | { |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 621 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 622 | return; |
| 623 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 624 | if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 625 | __blk_mq_run_hw_queue(hctx); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 626 | else if (hctx->queue->nr_hw_queues == 1) |
Jens Axboe | 59c3d45 | 2014-04-08 09:15:35 -0600 | [diff] [blame] | 627 | kblockd_schedule_delayed_work(&hctx->delayed_work, 0); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 628 | else { |
| 629 | unsigned int cpu; |
| 630 | |
| 631 | /* |
| 632 | * It'd be great if the workqueue API had a way to pass |
| 633 | * in a mask and had some smarts for more clever placement |
| 634 | * than the first CPU. Or we could round-robin here. For now, |
| 635 | * just queue on the first CPU. |
| 636 | */ |
| 637 | cpu = cpumask_first(hctx->cpumask); |
| 638 | kblockd_schedule_delayed_work_on(cpu, &hctx->delayed_work, 0); |
| 639 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | void blk_mq_run_queues(struct request_queue *q, bool async) |
| 643 | { |
| 644 | struct blk_mq_hw_ctx *hctx; |
| 645 | int i; |
| 646 | |
| 647 | queue_for_each_hw_ctx(q, hctx, i) { |
| 648 | if ((!blk_mq_hctx_has_pending(hctx) && |
| 649 | list_empty_careful(&hctx->dispatch)) || |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 650 | test_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 651 | continue; |
| 652 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 653 | preempt_disable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 654 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 655 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | EXPORT_SYMBOL(blk_mq_run_queues); |
| 659 | |
| 660 | void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 661 | { |
| 662 | cancel_delayed_work(&hctx->delayed_work); |
| 663 | set_bit(BLK_MQ_S_STOPPED, &hctx->state); |
| 664 | } |
| 665 | EXPORT_SYMBOL(blk_mq_stop_hw_queue); |
| 666 | |
Christoph Hellwig | 280d45f | 2013-10-25 14:45:58 +0100 | [diff] [blame] | 667 | void blk_mq_stop_hw_queues(struct request_queue *q) |
| 668 | { |
| 669 | struct blk_mq_hw_ctx *hctx; |
| 670 | int i; |
| 671 | |
| 672 | queue_for_each_hw_ctx(q, hctx, i) |
| 673 | blk_mq_stop_hw_queue(hctx); |
| 674 | } |
| 675 | EXPORT_SYMBOL(blk_mq_stop_hw_queues); |
| 676 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 677 | void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 678 | { |
| 679 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 680 | |
| 681 | preempt_disable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 682 | __blk_mq_run_hw_queue(hctx); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 683 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 684 | } |
| 685 | EXPORT_SYMBOL(blk_mq_start_hw_queue); |
| 686 | |
| 687 | void blk_mq_start_stopped_hw_queues(struct request_queue *q) |
| 688 | { |
| 689 | struct blk_mq_hw_ctx *hctx; |
| 690 | int i; |
| 691 | |
| 692 | queue_for_each_hw_ctx(q, hctx, i) { |
| 693 | if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
| 694 | continue; |
| 695 | |
| 696 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 697 | preempt_disable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 698 | blk_mq_run_hw_queue(hctx, true); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 699 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); |
| 703 | |
| 704 | static void blk_mq_work_fn(struct work_struct *work) |
| 705 | { |
| 706 | struct blk_mq_hw_ctx *hctx; |
| 707 | |
| 708 | hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 709 | |
| 710 | preempt_disable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 711 | __blk_mq_run_hw_queue(hctx); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 712 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 716 | struct request *rq, bool at_head) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 717 | { |
| 718 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
| 719 | |
Jens Axboe | 01b983c | 2013-11-19 18:59:10 -0700 | [diff] [blame] | 720 | trace_block_rq_insert(hctx->queue, rq); |
| 721 | |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 722 | if (at_head) |
| 723 | list_add(&rq->queuelist, &ctx->rq_list); |
| 724 | else |
| 725 | list_add_tail(&rq->queuelist, &ctx->rq_list); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 726 | blk_mq_hctx_mark_pending(hctx, ctx); |
| 727 | |
| 728 | /* |
| 729 | * We do this early, to ensure we are on the right CPU. |
| 730 | */ |
| 731 | blk_mq_add_timer(rq); |
| 732 | } |
| 733 | |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 734 | void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue, |
| 735 | bool async) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 736 | { |
| 737 | struct request_queue *q = rq->q; |
| 738 | struct blk_mq_hw_ctx *hctx; |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 739 | struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 740 | |
| 741 | current_ctx = blk_mq_get_ctx(q); |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 742 | if (!cpu_online(ctx->cpu)) |
| 743 | rq->mq_ctx = ctx = current_ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 744 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 745 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 746 | |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 747 | if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) && |
| 748 | !(rq->cmd_flags & (REQ_FLUSH_SEQ))) { |
| 749 | blk_insert_flush(rq); |
| 750 | } else { |
| 751 | spin_lock(&ctx->lock); |
| 752 | __blk_mq_insert_request(hctx, rq, at_head); |
| 753 | spin_unlock(&ctx->lock); |
| 754 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 755 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 756 | if (run_queue) |
| 757 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 758 | |
| 759 | blk_mq_put_ctx(current_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | static void blk_mq_insert_requests(struct request_queue *q, |
| 763 | struct blk_mq_ctx *ctx, |
| 764 | struct list_head *list, |
| 765 | int depth, |
| 766 | bool from_schedule) |
| 767 | |
| 768 | { |
| 769 | struct blk_mq_hw_ctx *hctx; |
| 770 | struct blk_mq_ctx *current_ctx; |
| 771 | |
| 772 | trace_block_unplug(q, depth, !from_schedule); |
| 773 | |
| 774 | current_ctx = blk_mq_get_ctx(q); |
| 775 | |
| 776 | if (!cpu_online(ctx->cpu)) |
| 777 | ctx = current_ctx; |
| 778 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 779 | |
| 780 | /* |
| 781 | * preemption doesn't flush plug list, so it's possible ctx->cpu is |
| 782 | * offline now |
| 783 | */ |
| 784 | spin_lock(&ctx->lock); |
| 785 | while (!list_empty(list)) { |
| 786 | struct request *rq; |
| 787 | |
| 788 | rq = list_first_entry(list, struct request, queuelist); |
| 789 | list_del_init(&rq->queuelist); |
| 790 | rq->mq_ctx = ctx; |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 791 | __blk_mq_insert_request(hctx, rq, false); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 792 | } |
| 793 | spin_unlock(&ctx->lock); |
| 794 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 795 | blk_mq_run_hw_queue(hctx, from_schedule); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 796 | blk_mq_put_ctx(current_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 800 | { |
| 801 | struct request *rqa = container_of(a, struct request, queuelist); |
| 802 | struct request *rqb = container_of(b, struct request, queuelist); |
| 803 | |
| 804 | return !(rqa->mq_ctx < rqb->mq_ctx || |
| 805 | (rqa->mq_ctx == rqb->mq_ctx && |
| 806 | blk_rq_pos(rqa) < blk_rq_pos(rqb))); |
| 807 | } |
| 808 | |
| 809 | void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) |
| 810 | { |
| 811 | struct blk_mq_ctx *this_ctx; |
| 812 | struct request_queue *this_q; |
| 813 | struct request *rq; |
| 814 | LIST_HEAD(list); |
| 815 | LIST_HEAD(ctx_list); |
| 816 | unsigned int depth; |
| 817 | |
| 818 | list_splice_init(&plug->mq_list, &list); |
| 819 | |
| 820 | list_sort(NULL, &list, plug_ctx_cmp); |
| 821 | |
| 822 | this_q = NULL; |
| 823 | this_ctx = NULL; |
| 824 | depth = 0; |
| 825 | |
| 826 | while (!list_empty(&list)) { |
| 827 | rq = list_entry_rq(list.next); |
| 828 | list_del_init(&rq->queuelist); |
| 829 | BUG_ON(!rq->q); |
| 830 | if (rq->mq_ctx != this_ctx) { |
| 831 | if (this_ctx) { |
| 832 | blk_mq_insert_requests(this_q, this_ctx, |
| 833 | &ctx_list, depth, |
| 834 | from_schedule); |
| 835 | } |
| 836 | |
| 837 | this_ctx = rq->mq_ctx; |
| 838 | this_q = rq->q; |
| 839 | depth = 0; |
| 840 | } |
| 841 | |
| 842 | depth++; |
| 843 | list_add_tail(&rq->queuelist, &ctx_list); |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | * If 'this_ctx' is set, we know we have entries to complete |
| 848 | * on 'ctx_list'. Do those. |
| 849 | */ |
| 850 | if (this_ctx) { |
| 851 | blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth, |
| 852 | from_schedule); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | static void blk_mq_bio_to_request(struct request *rq, struct bio *bio) |
| 857 | { |
| 858 | init_request_from_bio(rq, bio); |
| 859 | blk_account_io_start(rq, 1); |
| 860 | } |
| 861 | |
| 862 | static void blk_mq_make_request(struct request_queue *q, struct bio *bio) |
| 863 | { |
| 864 | struct blk_mq_hw_ctx *hctx; |
| 865 | struct blk_mq_ctx *ctx; |
| 866 | const int is_sync = rw_is_sync(bio->bi_rw); |
| 867 | const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA); |
| 868 | int rw = bio_data_dir(bio); |
| 869 | struct request *rq; |
| 870 | unsigned int use_plug, request_count = 0; |
| 871 | |
| 872 | /* |
| 873 | * If we have multiple hardware queues, just go directly to |
| 874 | * one of those for sync IO. |
| 875 | */ |
| 876 | use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync); |
| 877 | |
| 878 | blk_queue_bounce(q, &bio); |
| 879 | |
Nicholas Bellinger | 14ec77f | 2014-02-07 13:45:39 -0700 | [diff] [blame] | 880 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { |
| 881 | bio_endio(bio, -EIO); |
| 882 | return; |
| 883 | } |
| 884 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 885 | if (use_plug && blk_attempt_plug_merge(q, bio, &request_count)) |
| 886 | return; |
| 887 | |
| 888 | if (blk_mq_queue_enter(q)) { |
| 889 | bio_endio(bio, -EIO); |
| 890 | return; |
| 891 | } |
| 892 | |
| 893 | ctx = blk_mq_get_ctx(q); |
| 894 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 895 | |
Shaohua Li | 27fbf4e8 | 2014-02-19 20:20:21 +0800 | [diff] [blame] | 896 | if (is_sync) |
| 897 | rw |= REQ_SYNC; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 898 | trace_block_getrq(q, bio, rw); |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 899 | rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 900 | if (likely(rq)) |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 901 | blk_mq_rq_ctx_init(q, ctx, rq, rw); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 902 | else { |
| 903 | blk_mq_put_ctx(ctx); |
| 904 | trace_block_sleeprq(q, bio, rw); |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 905 | rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC, |
| 906 | false); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 907 | ctx = rq->mq_ctx; |
| 908 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 909 | } |
| 910 | |
| 911 | hctx->queued++; |
| 912 | |
| 913 | if (unlikely(is_flush_fua)) { |
| 914 | blk_mq_bio_to_request(rq, bio); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 915 | blk_insert_flush(rq); |
| 916 | goto run_queue; |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | * A task plug currently exists. Since this is completely lockless, |
| 921 | * utilize that to temporarily store requests until the task is |
| 922 | * either done or scheduled away. |
| 923 | */ |
| 924 | if (use_plug) { |
| 925 | struct blk_plug *plug = current->plug; |
| 926 | |
| 927 | if (plug) { |
| 928 | blk_mq_bio_to_request(rq, bio); |
Shaohua Li | 92f399c | 2013-10-29 12:01:03 -0600 | [diff] [blame] | 929 | if (list_empty(&plug->mq_list)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 930 | trace_block_plug(q); |
| 931 | else if (request_count >= BLK_MAX_REQUEST_COUNT) { |
| 932 | blk_flush_plug_list(plug, false); |
| 933 | trace_block_plug(q); |
| 934 | } |
| 935 | list_add_tail(&rq->queuelist, &plug->mq_list); |
| 936 | blk_mq_put_ctx(ctx); |
| 937 | return; |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | spin_lock(&ctx->lock); |
| 942 | |
| 943 | if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) && |
| 944 | blk_mq_attempt_merge(q, ctx, bio)) |
| 945 | __blk_mq_free_request(hctx, ctx, rq); |
| 946 | else { |
| 947 | blk_mq_bio_to_request(rq, bio); |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 948 | __blk_mq_insert_request(hctx, rq, false); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | spin_unlock(&ctx->lock); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 952 | |
| 953 | /* |
| 954 | * For a SYNC request, send it to the hardware immediately. For an |
| 955 | * ASYNC request, just ensure that we run it later on. The latter |
| 956 | * allows for merging opportunities and more efficient dispatching. |
| 957 | */ |
| 958 | run_queue: |
| 959 | blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 960 | blk_mq_put_ctx(ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | /* |
| 964 | * Default mapping to a software queue, since we use one per CPU. |
| 965 | */ |
| 966 | struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu) |
| 967 | { |
| 968 | return q->queue_hw_ctx[q->mq_map[cpu]]; |
| 969 | } |
| 970 | EXPORT_SYMBOL(blk_mq_map_queue); |
| 971 | |
| 972 | struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_reg *reg, |
| 973 | unsigned int hctx_index) |
| 974 | { |
| 975 | return kmalloc_node(sizeof(struct blk_mq_hw_ctx), |
| 976 | GFP_KERNEL | __GFP_ZERO, reg->numa_node); |
| 977 | } |
| 978 | EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue); |
| 979 | |
| 980 | void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx, |
| 981 | unsigned int hctx_index) |
| 982 | { |
| 983 | kfree(hctx); |
| 984 | } |
| 985 | EXPORT_SYMBOL(blk_mq_free_single_hw_queue); |
| 986 | |
| 987 | static void blk_mq_hctx_notify(void *data, unsigned long action, |
| 988 | unsigned int cpu) |
| 989 | { |
| 990 | struct blk_mq_hw_ctx *hctx = data; |
Jens Axboe | bccb5f7 | 2014-04-04 21:34:48 -0600 | [diff] [blame] | 991 | struct request_queue *q = hctx->queue; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 992 | struct blk_mq_ctx *ctx; |
| 993 | LIST_HEAD(tmp); |
| 994 | |
| 995 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) |
| 996 | return; |
| 997 | |
| 998 | /* |
| 999 | * Move ctx entries to new CPU, if this one is going away. |
| 1000 | */ |
Jens Axboe | bccb5f7 | 2014-04-04 21:34:48 -0600 | [diff] [blame] | 1001 | ctx = __blk_mq_get_ctx(q, cpu); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1002 | |
| 1003 | spin_lock(&ctx->lock); |
| 1004 | if (!list_empty(&ctx->rq_list)) { |
| 1005 | list_splice_init(&ctx->rq_list, &tmp); |
| 1006 | clear_bit(ctx->index_hw, hctx->ctx_map); |
| 1007 | } |
| 1008 | spin_unlock(&ctx->lock); |
| 1009 | |
| 1010 | if (list_empty(&tmp)) |
| 1011 | return; |
| 1012 | |
Jens Axboe | bccb5f7 | 2014-04-04 21:34:48 -0600 | [diff] [blame] | 1013 | ctx = blk_mq_get_ctx(q); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1014 | spin_lock(&ctx->lock); |
| 1015 | |
| 1016 | while (!list_empty(&tmp)) { |
| 1017 | struct request *rq; |
| 1018 | |
| 1019 | rq = list_first_entry(&tmp, struct request, queuelist); |
| 1020 | rq->mq_ctx = ctx; |
| 1021 | list_move_tail(&rq->queuelist, &ctx->rq_list); |
| 1022 | } |
| 1023 | |
Jens Axboe | bccb5f7 | 2014-04-04 21:34:48 -0600 | [diff] [blame] | 1024 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1025 | blk_mq_hctx_mark_pending(hctx, ctx); |
| 1026 | |
| 1027 | spin_unlock(&ctx->lock); |
Jens Axboe | bccb5f7 | 2014-04-04 21:34:48 -0600 | [diff] [blame] | 1028 | |
| 1029 | blk_mq_run_hw_queue(hctx, true); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1030 | blk_mq_put_ctx(ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1031 | } |
| 1032 | |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1033 | static void blk_mq_free_rq_map(struct blk_mq_hw_ctx *hctx, void *driver_data) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1034 | { |
| 1035 | struct page *page; |
| 1036 | |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1037 | if (hctx->rqs && hctx->queue->mq_ops->exit_request) { |
| 1038 | int i; |
| 1039 | |
| 1040 | for (i = 0; i < hctx->queue_depth; i++) { |
| 1041 | if (!hctx->rqs[i]) |
| 1042 | continue; |
| 1043 | hctx->queue->mq_ops->exit_request(driver_data, hctx, |
| 1044 | hctx->rqs[i], i); |
| 1045 | } |
| 1046 | } |
| 1047 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1048 | while (!list_empty(&hctx->page_list)) { |
Dave Hansen | 6753471 | 2014-01-08 20:17:46 -0700 | [diff] [blame] | 1049 | page = list_first_entry(&hctx->page_list, struct page, lru); |
| 1050 | list_del_init(&page->lru); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1051 | __free_pages(page, page->private); |
| 1052 | } |
| 1053 | |
| 1054 | kfree(hctx->rqs); |
| 1055 | |
| 1056 | if (hctx->tags) |
| 1057 | blk_mq_free_tags(hctx->tags); |
| 1058 | } |
| 1059 | |
| 1060 | static size_t order_to_size(unsigned int order) |
| 1061 | { |
| 1062 | size_t ret = PAGE_SIZE; |
| 1063 | |
| 1064 | while (order--) |
| 1065 | ret *= 2; |
| 1066 | |
| 1067 | return ret; |
| 1068 | } |
| 1069 | |
| 1070 | static int blk_mq_init_rq_map(struct blk_mq_hw_ctx *hctx, |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1071 | struct blk_mq_reg *reg, void *driver_data, int node) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1072 | { |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1073 | unsigned int reserved_tags = reg->reserved_tags; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1074 | unsigned int i, j, entries_per_page, max_order = 4; |
| 1075 | size_t rq_size, left; |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1076 | int error; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1077 | |
| 1078 | INIT_LIST_HEAD(&hctx->page_list); |
| 1079 | |
| 1080 | hctx->rqs = kmalloc_node(hctx->queue_depth * sizeof(struct request *), |
| 1081 | GFP_KERNEL, node); |
| 1082 | if (!hctx->rqs) |
| 1083 | return -ENOMEM; |
| 1084 | |
| 1085 | /* |
| 1086 | * rq_size is the size of the request plus driver payload, rounded |
| 1087 | * to the cacheline size |
| 1088 | */ |
| 1089 | rq_size = round_up(sizeof(struct request) + hctx->cmd_size, |
| 1090 | cache_line_size()); |
| 1091 | left = rq_size * hctx->queue_depth; |
| 1092 | |
| 1093 | for (i = 0; i < hctx->queue_depth;) { |
| 1094 | int this_order = max_order; |
| 1095 | struct page *page; |
| 1096 | int to_do; |
| 1097 | void *p; |
| 1098 | |
| 1099 | while (left < order_to_size(this_order - 1) && this_order) |
| 1100 | this_order--; |
| 1101 | |
| 1102 | do { |
| 1103 | page = alloc_pages_node(node, GFP_KERNEL, this_order); |
| 1104 | if (page) |
| 1105 | break; |
| 1106 | if (!this_order--) |
| 1107 | break; |
| 1108 | if (order_to_size(this_order) < rq_size) |
| 1109 | break; |
| 1110 | } while (1); |
| 1111 | |
| 1112 | if (!page) |
| 1113 | break; |
| 1114 | |
| 1115 | page->private = this_order; |
Dave Hansen | 6753471 | 2014-01-08 20:17:46 -0700 | [diff] [blame] | 1116 | list_add_tail(&page->lru, &hctx->page_list); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1117 | |
| 1118 | p = page_address(page); |
| 1119 | entries_per_page = order_to_size(this_order) / rq_size; |
| 1120 | to_do = min(entries_per_page, hctx->queue_depth - i); |
| 1121 | left -= to_do * rq_size; |
| 1122 | for (j = 0; j < to_do; j++) { |
| 1123 | hctx->rqs[i] = p; |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1124 | if (reg->ops->init_request) { |
| 1125 | error = reg->ops->init_request(driver_data, |
| 1126 | hctx, hctx->rqs[i], i); |
| 1127 | if (error) |
| 1128 | goto err_rq_map; |
| 1129 | } |
| 1130 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1131 | p += rq_size; |
| 1132 | i++; |
| 1133 | } |
| 1134 | } |
| 1135 | |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1136 | if (i < (reserved_tags + BLK_MQ_TAG_MIN)) { |
| 1137 | error = -ENOMEM; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1138 | goto err_rq_map; |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1139 | } |
| 1140 | if (i != hctx->queue_depth) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1141 | hctx->queue_depth = i; |
| 1142 | pr_warn("%s: queue depth set to %u because of low memory\n", |
| 1143 | __func__, i); |
| 1144 | } |
| 1145 | |
| 1146 | hctx->tags = blk_mq_init_tags(hctx->queue_depth, reserved_tags, node); |
| 1147 | if (!hctx->tags) { |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1148 | error = -ENOMEM; |
| 1149 | goto err_rq_map; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | return 0; |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1153 | err_rq_map: |
| 1154 | blk_mq_free_rq_map(hctx, driver_data); |
| 1155 | return error; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | static int blk_mq_init_hw_queues(struct request_queue *q, |
| 1159 | struct blk_mq_reg *reg, void *driver_data) |
| 1160 | { |
| 1161 | struct blk_mq_hw_ctx *hctx; |
| 1162 | unsigned int i, j; |
| 1163 | |
| 1164 | /* |
| 1165 | * Initialize hardware queues |
| 1166 | */ |
| 1167 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1168 | unsigned int num_maps; |
| 1169 | int node; |
| 1170 | |
| 1171 | node = hctx->numa_node; |
| 1172 | if (node == NUMA_NO_NODE) |
| 1173 | node = hctx->numa_node = reg->numa_node; |
| 1174 | |
| 1175 | INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn); |
| 1176 | spin_lock_init(&hctx->lock); |
| 1177 | INIT_LIST_HEAD(&hctx->dispatch); |
| 1178 | hctx->queue = q; |
| 1179 | hctx->queue_num = i; |
| 1180 | hctx->flags = reg->flags; |
| 1181 | hctx->queue_depth = reg->queue_depth; |
| 1182 | hctx->cmd_size = reg->cmd_size; |
| 1183 | |
| 1184 | blk_mq_init_cpu_notifier(&hctx->cpu_notifier, |
| 1185 | blk_mq_hctx_notify, hctx); |
| 1186 | blk_mq_register_cpu_notifier(&hctx->cpu_notifier); |
| 1187 | |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1188 | if (blk_mq_init_rq_map(hctx, reg, driver_data, node)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1189 | break; |
| 1190 | |
| 1191 | /* |
| 1192 | * Allocate space for all possible cpus to avoid allocation in |
| 1193 | * runtime |
| 1194 | */ |
| 1195 | hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *), |
| 1196 | GFP_KERNEL, node); |
| 1197 | if (!hctx->ctxs) |
| 1198 | break; |
| 1199 | |
| 1200 | num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG; |
| 1201 | hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long), |
| 1202 | GFP_KERNEL, node); |
| 1203 | if (!hctx->ctx_map) |
| 1204 | break; |
| 1205 | |
| 1206 | hctx->nr_ctx_map = num_maps; |
| 1207 | hctx->nr_ctx = 0; |
| 1208 | |
| 1209 | if (reg->ops->init_hctx && |
| 1210 | reg->ops->init_hctx(hctx, driver_data, i)) |
| 1211 | break; |
| 1212 | } |
| 1213 | |
| 1214 | if (i == q->nr_hw_queues) |
| 1215 | return 0; |
| 1216 | |
| 1217 | /* |
| 1218 | * Init failed |
| 1219 | */ |
| 1220 | queue_for_each_hw_ctx(q, hctx, j) { |
| 1221 | if (i == j) |
| 1222 | break; |
| 1223 | |
| 1224 | if (reg->ops->exit_hctx) |
| 1225 | reg->ops->exit_hctx(hctx, j); |
| 1226 | |
| 1227 | blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier); |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1228 | blk_mq_free_rq_map(hctx, driver_data); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1229 | kfree(hctx->ctxs); |
| 1230 | } |
| 1231 | |
| 1232 | return 1; |
| 1233 | } |
| 1234 | |
| 1235 | static void blk_mq_init_cpu_queues(struct request_queue *q, |
| 1236 | unsigned int nr_hw_queues) |
| 1237 | { |
| 1238 | unsigned int i; |
| 1239 | |
| 1240 | for_each_possible_cpu(i) { |
| 1241 | struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); |
| 1242 | struct blk_mq_hw_ctx *hctx; |
| 1243 | |
| 1244 | memset(__ctx, 0, sizeof(*__ctx)); |
| 1245 | __ctx->cpu = i; |
| 1246 | spin_lock_init(&__ctx->lock); |
| 1247 | INIT_LIST_HEAD(&__ctx->rq_list); |
| 1248 | __ctx->queue = q; |
| 1249 | |
| 1250 | /* 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] | 1251 | if (!cpu_online(i)) |
| 1252 | continue; |
| 1253 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1254 | hctx = q->mq_ops->map_queue(q, i); |
| 1255 | cpumask_set_cpu(i, hctx->cpumask); |
| 1256 | hctx->nr_ctx++; |
| 1257 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1258 | /* |
| 1259 | * Set local node, IFF we have more than one hw queue. If |
| 1260 | * not, we remain on the home node of the device |
| 1261 | */ |
| 1262 | if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) |
| 1263 | hctx->numa_node = cpu_to_node(i); |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | static void blk_mq_map_swqueue(struct request_queue *q) |
| 1268 | { |
| 1269 | unsigned int i; |
| 1270 | struct blk_mq_hw_ctx *hctx; |
| 1271 | struct blk_mq_ctx *ctx; |
| 1272 | |
| 1273 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1274 | cpumask_clear(hctx->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1275 | hctx->nr_ctx = 0; |
| 1276 | } |
| 1277 | |
| 1278 | /* |
| 1279 | * Map software to hardware queues |
| 1280 | */ |
| 1281 | queue_for_each_ctx(q, ctx, i) { |
| 1282 | /* 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] | 1283 | if (!cpu_online(i)) |
| 1284 | continue; |
| 1285 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1286 | hctx = q->mq_ops->map_queue(q, i); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1287 | cpumask_set_cpu(i, hctx->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1288 | ctx->index_hw = hctx->nr_ctx; |
| 1289 | hctx->ctxs[hctx->nr_ctx++] = ctx; |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | struct request_queue *blk_mq_init_queue(struct blk_mq_reg *reg, |
| 1294 | void *driver_data) |
| 1295 | { |
| 1296 | struct blk_mq_hw_ctx **hctxs; |
| 1297 | struct blk_mq_ctx *ctx; |
| 1298 | struct request_queue *q; |
| 1299 | int i; |
| 1300 | |
| 1301 | if (!reg->nr_hw_queues || |
| 1302 | !reg->ops->queue_rq || !reg->ops->map_queue || |
| 1303 | !reg->ops->alloc_hctx || !reg->ops->free_hctx) |
| 1304 | return ERR_PTR(-EINVAL); |
| 1305 | |
| 1306 | if (!reg->queue_depth) |
| 1307 | reg->queue_depth = BLK_MQ_MAX_DEPTH; |
| 1308 | else if (reg->queue_depth > BLK_MQ_MAX_DEPTH) { |
| 1309 | pr_err("blk-mq: queuedepth too large (%u)\n", reg->queue_depth); |
| 1310 | reg->queue_depth = BLK_MQ_MAX_DEPTH; |
| 1311 | } |
| 1312 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1313 | if (reg->queue_depth < (reg->reserved_tags + BLK_MQ_TAG_MIN)) |
| 1314 | return ERR_PTR(-EINVAL); |
| 1315 | |
| 1316 | ctx = alloc_percpu(struct blk_mq_ctx); |
| 1317 | if (!ctx) |
| 1318 | return ERR_PTR(-ENOMEM); |
| 1319 | |
| 1320 | hctxs = kmalloc_node(reg->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL, |
| 1321 | reg->numa_node); |
| 1322 | |
| 1323 | if (!hctxs) |
| 1324 | goto err_percpu; |
| 1325 | |
| 1326 | for (i = 0; i < reg->nr_hw_queues; i++) { |
| 1327 | hctxs[i] = reg->ops->alloc_hctx(reg, i); |
| 1328 | if (!hctxs[i]) |
| 1329 | goto err_hctxs; |
| 1330 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1331 | if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL)) |
| 1332 | goto err_hctxs; |
| 1333 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1334 | hctxs[i]->numa_node = NUMA_NO_NODE; |
| 1335 | hctxs[i]->queue_num = i; |
| 1336 | } |
| 1337 | |
| 1338 | q = blk_alloc_queue_node(GFP_KERNEL, reg->numa_node); |
| 1339 | if (!q) |
| 1340 | goto err_hctxs; |
| 1341 | |
| 1342 | q->mq_map = blk_mq_make_queue_map(reg); |
| 1343 | if (!q->mq_map) |
| 1344 | goto err_map; |
| 1345 | |
| 1346 | setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q); |
| 1347 | blk_queue_rq_timeout(q, 30000); |
| 1348 | |
| 1349 | q->nr_queues = nr_cpu_ids; |
| 1350 | q->nr_hw_queues = reg->nr_hw_queues; |
| 1351 | |
| 1352 | q->queue_ctx = ctx; |
| 1353 | q->queue_hw_ctx = hctxs; |
| 1354 | |
| 1355 | q->mq_ops = reg->ops; |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 1356 | q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1357 | |
Christoph Hellwig | 1be036e | 2014-02-07 10:22:39 -0800 | [diff] [blame] | 1358 | q->sg_reserved_size = INT_MAX; |
| 1359 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1360 | blk_queue_make_request(q, blk_mq_make_request); |
| 1361 | blk_queue_rq_timed_out(q, reg->ops->timeout); |
| 1362 | if (reg->timeout) |
| 1363 | blk_queue_rq_timeout(q, reg->timeout); |
| 1364 | |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 1365 | if (reg->ops->complete) |
| 1366 | blk_queue_softirq_done(q, reg->ops->complete); |
| 1367 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1368 | blk_mq_init_flush(q); |
| 1369 | blk_mq_init_cpu_queues(q, reg->nr_hw_queues); |
| 1370 | |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1371 | q->flush_rq = kzalloc(round_up(sizeof(struct request) + reg->cmd_size, |
| 1372 | cache_line_size()), GFP_KERNEL); |
| 1373 | if (!q->flush_rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1374 | goto err_hw; |
| 1375 | |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1376 | if (blk_mq_init_hw_queues(q, reg, driver_data)) |
| 1377 | goto err_flush_rq; |
| 1378 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1379 | blk_mq_map_swqueue(q); |
| 1380 | |
| 1381 | mutex_lock(&all_q_mutex); |
| 1382 | list_add_tail(&q->all_q_node, &all_q_list); |
| 1383 | mutex_unlock(&all_q_mutex); |
| 1384 | |
| 1385 | return q; |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1386 | |
| 1387 | err_flush_rq: |
| 1388 | kfree(q->flush_rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1389 | err_hw: |
| 1390 | kfree(q->mq_map); |
| 1391 | err_map: |
| 1392 | blk_cleanup_queue(q); |
| 1393 | err_hctxs: |
| 1394 | for (i = 0; i < reg->nr_hw_queues; i++) { |
| 1395 | if (!hctxs[i]) |
| 1396 | break; |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1397 | free_cpumask_var(hctxs[i]->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1398 | reg->ops->free_hctx(hctxs[i], i); |
| 1399 | } |
| 1400 | kfree(hctxs); |
| 1401 | err_percpu: |
| 1402 | free_percpu(ctx); |
| 1403 | return ERR_PTR(-ENOMEM); |
| 1404 | } |
| 1405 | EXPORT_SYMBOL(blk_mq_init_queue); |
| 1406 | |
| 1407 | void blk_mq_free_queue(struct request_queue *q) |
| 1408 | { |
| 1409 | struct blk_mq_hw_ctx *hctx; |
| 1410 | int i; |
| 1411 | |
| 1412 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1413 | kfree(hctx->ctx_map); |
| 1414 | kfree(hctx->ctxs); |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1415 | blk_mq_free_rq_map(hctx, q->queuedata); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1416 | blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier); |
| 1417 | if (q->mq_ops->exit_hctx) |
| 1418 | q->mq_ops->exit_hctx(hctx, i); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1419 | free_cpumask_var(hctx->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1420 | q->mq_ops->free_hctx(hctx, i); |
| 1421 | } |
| 1422 | |
| 1423 | free_percpu(q->queue_ctx); |
| 1424 | kfree(q->queue_hw_ctx); |
| 1425 | kfree(q->mq_map); |
| 1426 | |
| 1427 | q->queue_ctx = NULL; |
| 1428 | q->queue_hw_ctx = NULL; |
| 1429 | q->mq_map = NULL; |
| 1430 | |
| 1431 | mutex_lock(&all_q_mutex); |
| 1432 | list_del_init(&q->all_q_node); |
| 1433 | mutex_unlock(&all_q_mutex); |
| 1434 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1435 | |
| 1436 | /* Basically redo blk_mq_init_queue with queue frozen */ |
Paul Gortmaker | f618ef7 | 2013-11-14 08:26:02 -0700 | [diff] [blame] | 1437 | static void blk_mq_queue_reinit(struct request_queue *q) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1438 | { |
| 1439 | blk_mq_freeze_queue(q); |
| 1440 | |
| 1441 | blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues); |
| 1442 | |
| 1443 | /* |
| 1444 | * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe |
| 1445 | * we should change hctx numa_node according to new topology (this |
| 1446 | * involves free and re-allocate memory, worthy doing?) |
| 1447 | */ |
| 1448 | |
| 1449 | blk_mq_map_swqueue(q); |
| 1450 | |
| 1451 | blk_mq_unfreeze_queue(q); |
| 1452 | } |
| 1453 | |
Paul Gortmaker | f618ef7 | 2013-11-14 08:26:02 -0700 | [diff] [blame] | 1454 | static int blk_mq_queue_reinit_notify(struct notifier_block *nb, |
| 1455 | unsigned long action, void *hcpu) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1456 | { |
| 1457 | struct request_queue *q; |
| 1458 | |
| 1459 | /* |
| 1460 | * Before new mapping is established, hotadded cpu might already start |
| 1461 | * handling requests. This doesn't break anything as we map offline |
| 1462 | * CPUs to first hardware queue. We will re-init queue below to get |
| 1463 | * optimal settings. |
| 1464 | */ |
| 1465 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN && |
| 1466 | action != CPU_ONLINE && action != CPU_ONLINE_FROZEN) |
| 1467 | return NOTIFY_OK; |
| 1468 | |
| 1469 | mutex_lock(&all_q_mutex); |
| 1470 | list_for_each_entry(q, &all_q_list, all_q_node) |
| 1471 | blk_mq_queue_reinit(q); |
| 1472 | mutex_unlock(&all_q_mutex); |
| 1473 | return NOTIFY_OK; |
| 1474 | } |
| 1475 | |
Jens Axboe | 676141e | 2014-03-20 13:29:18 -0600 | [diff] [blame] | 1476 | void blk_mq_disable_hotplug(void) |
| 1477 | { |
| 1478 | mutex_lock(&all_q_mutex); |
| 1479 | } |
| 1480 | |
| 1481 | void blk_mq_enable_hotplug(void) |
| 1482 | { |
| 1483 | mutex_unlock(&all_q_mutex); |
| 1484 | } |
| 1485 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1486 | static int __init blk_mq_init(void) |
| 1487 | { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1488 | blk_mq_cpu_init(); |
| 1489 | |
| 1490 | /* Must be called after percpu_counter_hotcpu_callback() */ |
| 1491 | hotcpu_notifier(blk_mq_queue_reinit_notify, -10); |
| 1492 | |
| 1493 | return 0; |
| 1494 | } |
| 1495 | subsys_initcall(blk_mq_init); |