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 | cb96a42 | 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 | cb96a42 | 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 | cb96a42 | 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 | cb96a42 | 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 | cb96a42 | 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 | cb96a42 | 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 | cb96a42 | 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 | cb96a42 | 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 | cb96a42 | 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 | cb96a42 | 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 | |
Jens Axboe | 0e62f51 | 2014-06-04 10:23:49 -0600 | [diff] [blame] | 532 | static inline bool is_flush_request(struct request *rq, unsigned int tag) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 533 | { |
Jens Axboe | 0e62f51 | 2014-06-04 10:23:49 -0600 | [diff] [blame] | 534 | return ((rq->cmd_flags & REQ_FLUSH_SEQ) && |
| 535 | rq->q->flush_rq->tag == tag); |
| 536 | } |
Shaohua Li | 2230237 | 2014-05-30 08:06:42 -0600 | [diff] [blame] | 537 | |
Jens Axboe | 0e62f51 | 2014-06-04 10:23:49 -0600 | [diff] [blame] | 538 | struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag) |
| 539 | { |
| 540 | struct request *rq = tags->rqs[tag]; |
Shaohua Li | 2230237 | 2014-05-30 08:06:42 -0600 | [diff] [blame] | 541 | |
Jens Axboe | 0e62f51 | 2014-06-04 10:23:49 -0600 | [diff] [blame] | 542 | if (!is_flush_request(rq, tag)) |
| 543 | return rq; |
| 544 | |
| 545 | return rq->q->flush_rq; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 546 | } |
| 547 | EXPORT_SYMBOL(blk_mq_tag_to_rq); |
| 548 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 549 | struct blk_mq_timeout_data { |
| 550 | struct blk_mq_hw_ctx *hctx; |
| 551 | unsigned long *next; |
| 552 | unsigned int *next_set; |
| 553 | }; |
| 554 | |
| 555 | static void blk_mq_timeout_check(void *__data, unsigned long *free_tags) |
| 556 | { |
| 557 | struct blk_mq_timeout_data *data = __data; |
| 558 | struct blk_mq_hw_ctx *hctx = data->hctx; |
| 559 | unsigned int tag; |
| 560 | |
| 561 | /* It may not be in flight yet (this is where |
| 562 | * the REQ_ATOMIC_STARTED flag comes in). The requests are |
| 563 | * statically allocated, so we know it's always safe to access the |
| 564 | * memory associated with a bit offset into ->rqs[]. |
| 565 | */ |
| 566 | tag = 0; |
| 567 | do { |
| 568 | struct request *rq; |
| 569 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 570 | tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag); |
| 571 | if (tag >= hctx->tags->nr_tags) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 572 | break; |
| 573 | |
Jens Axboe | 0e62f51 | 2014-06-04 10:23:49 -0600 | [diff] [blame] | 574 | rq = blk_mq_tag_to_rq(hctx->tags, tag++); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 575 | if (rq->q != hctx->queue) |
| 576 | continue; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 577 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
| 578 | continue; |
| 579 | |
| 580 | blk_rq_check_expired(rq, data->next, data->next_set); |
| 581 | } while (1); |
| 582 | } |
| 583 | |
| 584 | static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx, |
| 585 | unsigned long *next, |
| 586 | unsigned int *next_set) |
| 587 | { |
| 588 | struct blk_mq_timeout_data data = { |
| 589 | .hctx = hctx, |
| 590 | .next = next, |
| 591 | .next_set = next_set, |
| 592 | }; |
| 593 | |
| 594 | /* |
| 595 | * Ask the tagging code to iterate busy requests, so we can |
| 596 | * check them for timeout. |
| 597 | */ |
| 598 | blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data); |
| 599 | } |
| 600 | |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 601 | static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq) |
| 602 | { |
| 603 | struct request_queue *q = rq->q; |
| 604 | |
| 605 | /* |
| 606 | * We know that complete is set at this point. If STARTED isn't set |
| 607 | * anymore, then the request isn't active and the "timeout" should |
| 608 | * just be ignored. This can happen due to the bitflag ordering. |
| 609 | * Timeout first checks if STARTED is set, and if it is, assumes |
| 610 | * the request is active. But if we race with completion, then |
| 611 | * we both flags will get cleared. So check here again, and ignore |
| 612 | * a timeout event with a request that isn't active. |
| 613 | */ |
| 614 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
| 615 | return BLK_EH_NOT_HANDLED; |
| 616 | |
| 617 | if (!q->mq_ops->timeout) |
| 618 | return BLK_EH_RESET_TIMER; |
| 619 | |
| 620 | return q->mq_ops->timeout(rq); |
| 621 | } |
| 622 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 623 | static void blk_mq_rq_timer(unsigned long data) |
| 624 | { |
| 625 | struct request_queue *q = (struct request_queue *) data; |
| 626 | struct blk_mq_hw_ctx *hctx; |
| 627 | unsigned long next = 0; |
| 628 | int i, next_set = 0; |
| 629 | |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 630 | queue_for_each_hw_ctx(q, hctx, i) { |
| 631 | /* |
| 632 | * If not software queues are currently mapped to this |
| 633 | * hardware queue, there's nothing to check |
| 634 | */ |
| 635 | if (!hctx->nr_ctx || !hctx->tags) |
| 636 | continue; |
| 637 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 638 | blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set); |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 639 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 640 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 641 | if (next_set) { |
| 642 | next = blk_rq_timeout(round_jiffies_up(next)); |
| 643 | mod_timer(&q->timeout, next); |
| 644 | } else { |
| 645 | queue_for_each_hw_ctx(q, hctx, i) |
| 646 | blk_mq_tag_idle(hctx); |
| 647 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /* |
| 651 | * Reverse check our software queue for entries that we could potentially |
| 652 | * merge with. Currently includes a hand-wavy stop count of 8, to not spend |
| 653 | * too much time checking for merges. |
| 654 | */ |
| 655 | static bool blk_mq_attempt_merge(struct request_queue *q, |
| 656 | struct blk_mq_ctx *ctx, struct bio *bio) |
| 657 | { |
| 658 | struct request *rq; |
| 659 | int checked = 8; |
| 660 | |
| 661 | list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) { |
| 662 | int el_ret; |
| 663 | |
| 664 | if (!checked--) |
| 665 | break; |
| 666 | |
| 667 | if (!blk_rq_merge_ok(rq, bio)) |
| 668 | continue; |
| 669 | |
| 670 | el_ret = blk_try_merge(rq, bio); |
| 671 | if (el_ret == ELEVATOR_BACK_MERGE) { |
| 672 | if (bio_attempt_back_merge(q, rq, bio)) { |
| 673 | ctx->rq_merged++; |
| 674 | return true; |
| 675 | } |
| 676 | break; |
| 677 | } else if (el_ret == ELEVATOR_FRONT_MERGE) { |
| 678 | if (bio_attempt_front_merge(q, rq, bio)) { |
| 679 | ctx->rq_merged++; |
| 680 | return true; |
| 681 | } |
| 682 | break; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | return false; |
| 687 | } |
| 688 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 689 | /* |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 690 | * Process software queues that have been marked busy, splicing them |
| 691 | * to the for-dispatch |
| 692 | */ |
| 693 | static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list) |
| 694 | { |
| 695 | struct blk_mq_ctx *ctx; |
| 696 | int i; |
| 697 | |
| 698 | for (i = 0; i < hctx->ctx_map.map_size; i++) { |
| 699 | struct blk_align_bitmap *bm = &hctx->ctx_map.map[i]; |
| 700 | unsigned int off, bit; |
| 701 | |
| 702 | if (!bm->word) |
| 703 | continue; |
| 704 | |
| 705 | bit = 0; |
| 706 | off = i * hctx->ctx_map.bits_per_word; |
| 707 | do { |
| 708 | bit = find_next_bit(&bm->word, bm->depth, bit); |
| 709 | if (bit >= bm->depth) |
| 710 | break; |
| 711 | |
| 712 | ctx = hctx->ctxs[bit + off]; |
| 713 | clear_bit(bit, &bm->word); |
| 714 | spin_lock(&ctx->lock); |
| 715 | list_splice_tail_init(&ctx->rq_list, list); |
| 716 | spin_unlock(&ctx->lock); |
| 717 | |
| 718 | bit++; |
| 719 | } while (1); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | /* |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 724 | * Run this hardware queue, pulling any software queues mapped to it in. |
| 725 | * Note that this function currently has various problems around ordering |
| 726 | * of IO. In particular, we'd like FIFO behaviour on handling existing |
| 727 | * items on the hctx->dispatch list. Ignore that for now. |
| 728 | */ |
| 729 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 730 | { |
| 731 | struct request_queue *q = hctx->queue; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 732 | struct request *rq; |
| 733 | LIST_HEAD(rq_list); |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 734 | int queued; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 735 | |
Jens Axboe | fd1270d | 2014-04-16 09:23:48 -0600 | [diff] [blame] | 736 | WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 737 | |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 738 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 739 | return; |
| 740 | |
| 741 | hctx->run++; |
| 742 | |
| 743 | /* |
| 744 | * Touch any software queue that has pending entries. |
| 745 | */ |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 746 | flush_busy_ctxs(hctx, &rq_list); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 747 | |
| 748 | /* |
| 749 | * If we have previous entries on our dispatch list, grab them |
| 750 | * and stuff them at the front for more fair dispatch. |
| 751 | */ |
| 752 | if (!list_empty_careful(&hctx->dispatch)) { |
| 753 | spin_lock(&hctx->lock); |
| 754 | if (!list_empty(&hctx->dispatch)) |
| 755 | list_splice_init(&hctx->dispatch, &rq_list); |
| 756 | spin_unlock(&hctx->lock); |
| 757 | } |
| 758 | |
| 759 | /* |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 760 | * Now process all the entries, sending them to the driver. |
| 761 | */ |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 762 | queued = 0; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 763 | while (!list_empty(&rq_list)) { |
| 764 | int ret; |
| 765 | |
| 766 | rq = list_first_entry(&rq_list, struct request, queuelist); |
| 767 | list_del_init(&rq->queuelist); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 768 | |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 769 | blk_mq_start_request(rq, list_empty(&rq_list)); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 770 | |
| 771 | ret = q->mq_ops->queue_rq(hctx, rq); |
| 772 | switch (ret) { |
| 773 | case BLK_MQ_RQ_QUEUE_OK: |
| 774 | queued++; |
| 775 | continue; |
| 776 | case BLK_MQ_RQ_QUEUE_BUSY: |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 777 | list_add(&rq->queuelist, &rq_list); |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 778 | __blk_mq_requeue_request(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 779 | break; |
| 780 | default: |
| 781 | pr_err("blk-mq: bad return on queue: %d\n", ret); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 782 | case BLK_MQ_RQ_QUEUE_ERROR: |
Christoph Hellwig | 1e93b8c | 2014-02-11 08:27:13 -0800 | [diff] [blame] | 783 | rq->errors = -EIO; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 784 | blk_mq_end_io(rq, rq->errors); |
| 785 | break; |
| 786 | } |
| 787 | |
| 788 | if (ret == BLK_MQ_RQ_QUEUE_BUSY) |
| 789 | break; |
| 790 | } |
| 791 | |
| 792 | if (!queued) |
| 793 | hctx->dispatched[0]++; |
| 794 | else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1))) |
| 795 | hctx->dispatched[ilog2(queued) + 1]++; |
| 796 | |
| 797 | /* |
| 798 | * Any items that need requeuing? Stuff them into hctx->dispatch, |
| 799 | * that is where we will continue on next queue run. |
| 800 | */ |
| 801 | if (!list_empty(&rq_list)) { |
| 802 | spin_lock(&hctx->lock); |
| 803 | list_splice(&rq_list, &hctx->dispatch); |
| 804 | spin_unlock(&hctx->lock); |
| 805 | } |
| 806 | } |
| 807 | |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 808 | /* |
| 809 | * It'd be great if the workqueue API had a way to pass |
| 810 | * in a mask and had some smarts for more clever placement. |
| 811 | * For now we just round-robin here, switching for every |
| 812 | * BLK_MQ_CPU_WORK_BATCH queued items. |
| 813 | */ |
| 814 | static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) |
| 815 | { |
| 816 | int cpu = hctx->next_cpu; |
| 817 | |
| 818 | if (--hctx->next_cpu_batch <= 0) { |
| 819 | int next_cpu; |
| 820 | |
| 821 | next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask); |
| 822 | if (next_cpu >= nr_cpu_ids) |
| 823 | next_cpu = cpumask_first(hctx->cpumask); |
| 824 | |
| 825 | hctx->next_cpu = next_cpu; |
| 826 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
| 827 | } |
| 828 | |
| 829 | return cpu; |
| 830 | } |
| 831 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 832 | void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) |
| 833 | { |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 834 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 835 | return; |
| 836 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 837 | if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 838 | __blk_mq_run_hw_queue(hctx); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 839 | else if (hctx->queue->nr_hw_queues == 1) |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 840 | kblockd_schedule_delayed_work(&hctx->run_work, 0); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 841 | else { |
| 842 | unsigned int cpu; |
| 843 | |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 844 | cpu = blk_mq_hctx_next_cpu(hctx); |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 845 | kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 846 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | void blk_mq_run_queues(struct request_queue *q, bool async) |
| 850 | { |
| 851 | struct blk_mq_hw_ctx *hctx; |
| 852 | int i; |
| 853 | |
| 854 | queue_for_each_hw_ctx(q, hctx, i) { |
| 855 | if ((!blk_mq_hctx_has_pending(hctx) && |
| 856 | list_empty_careful(&hctx->dispatch)) || |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 857 | test_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 858 | continue; |
| 859 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 860 | preempt_disable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 861 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 862 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | EXPORT_SYMBOL(blk_mq_run_queues); |
| 866 | |
| 867 | void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 868 | { |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 869 | cancel_delayed_work(&hctx->run_work); |
| 870 | cancel_delayed_work(&hctx->delay_work); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 871 | set_bit(BLK_MQ_S_STOPPED, &hctx->state); |
| 872 | } |
| 873 | EXPORT_SYMBOL(blk_mq_stop_hw_queue); |
| 874 | |
Christoph Hellwig | 280d45f | 2013-10-25 14:45:58 +0100 | [diff] [blame] | 875 | void blk_mq_stop_hw_queues(struct request_queue *q) |
| 876 | { |
| 877 | struct blk_mq_hw_ctx *hctx; |
| 878 | int i; |
| 879 | |
| 880 | queue_for_each_hw_ctx(q, hctx, i) |
| 881 | blk_mq_stop_hw_queue(hctx); |
| 882 | } |
| 883 | EXPORT_SYMBOL(blk_mq_stop_hw_queues); |
| 884 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 885 | void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 886 | { |
| 887 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 888 | |
| 889 | preempt_disable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 890 | __blk_mq_run_hw_queue(hctx); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 891 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 892 | } |
| 893 | EXPORT_SYMBOL(blk_mq_start_hw_queue); |
| 894 | |
Christoph Hellwig | 2f26855 | 2014-04-16 09:44:56 +0200 | [diff] [blame] | 895 | void blk_mq_start_hw_queues(struct request_queue *q) |
| 896 | { |
| 897 | struct blk_mq_hw_ctx *hctx; |
| 898 | int i; |
| 899 | |
| 900 | queue_for_each_hw_ctx(q, hctx, i) |
| 901 | blk_mq_start_hw_queue(hctx); |
| 902 | } |
| 903 | EXPORT_SYMBOL(blk_mq_start_hw_queues); |
| 904 | |
| 905 | |
Christoph Hellwig | 1b4a325 | 2014-04-16 09:44:54 +0200 | [diff] [blame] | 906 | 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] | 907 | { |
| 908 | struct blk_mq_hw_ctx *hctx; |
| 909 | int i; |
| 910 | |
| 911 | queue_for_each_hw_ctx(q, hctx, i) { |
| 912 | if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
| 913 | continue; |
| 914 | |
| 915 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 916 | preempt_disable(); |
Christoph Hellwig | 1b4a325 | 2014-04-16 09:44:54 +0200 | [diff] [blame] | 917 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 918 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); |
| 922 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 923 | static void blk_mq_run_work_fn(struct work_struct *work) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 924 | { |
| 925 | struct blk_mq_hw_ctx *hctx; |
| 926 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 927 | hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 928 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 929 | __blk_mq_run_hw_queue(hctx); |
| 930 | } |
| 931 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 932 | static void blk_mq_delay_work_fn(struct work_struct *work) |
| 933 | { |
| 934 | struct blk_mq_hw_ctx *hctx; |
| 935 | |
| 936 | hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work); |
| 937 | |
| 938 | if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
| 939 | __blk_mq_run_hw_queue(hctx); |
| 940 | } |
| 941 | |
| 942 | void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) |
| 943 | { |
| 944 | unsigned long tmo = msecs_to_jiffies(msecs); |
| 945 | |
| 946 | if (hctx->queue->nr_hw_queues == 1) |
| 947 | kblockd_schedule_delayed_work(&hctx->delay_work, tmo); |
| 948 | else { |
| 949 | unsigned int cpu; |
| 950 | |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 951 | cpu = blk_mq_hctx_next_cpu(hctx); |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 952 | kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo); |
| 953 | } |
| 954 | } |
| 955 | EXPORT_SYMBOL(blk_mq_delay_queue); |
| 956 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 957 | static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 958 | struct request *rq, bool at_head) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 959 | { |
| 960 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
| 961 | |
Jens Axboe | 01b983c | 2013-11-19 18:59:10 -0700 | [diff] [blame] | 962 | trace_block_rq_insert(hctx->queue, rq); |
| 963 | |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 964 | if (at_head) |
| 965 | list_add(&rq->queuelist, &ctx->rq_list); |
| 966 | else |
| 967 | list_add_tail(&rq->queuelist, &ctx->rq_list); |
Jens Axboe | 4bb659b | 2014-05-09 09:36:49 -0600 | [diff] [blame] | 968 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 969 | blk_mq_hctx_mark_pending(hctx, ctx); |
| 970 | |
| 971 | /* |
| 972 | * We do this early, to ensure we are on the right CPU. |
| 973 | */ |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 974 | blk_add_timer(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 975 | } |
| 976 | |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 977 | void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue, |
| 978 | bool async) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 979 | { |
| 980 | struct request_queue *q = rq->q; |
| 981 | struct blk_mq_hw_ctx *hctx; |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 982 | struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 983 | |
| 984 | current_ctx = blk_mq_get_ctx(q); |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 985 | if (!cpu_online(ctx->cpu)) |
| 986 | rq->mq_ctx = ctx = current_ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 987 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 988 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 989 | |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 990 | if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) && |
| 991 | !(rq->cmd_flags & (REQ_FLUSH_SEQ))) { |
| 992 | blk_insert_flush(rq); |
| 993 | } else { |
| 994 | spin_lock(&ctx->lock); |
| 995 | __blk_mq_insert_request(hctx, rq, at_head); |
| 996 | spin_unlock(&ctx->lock); |
| 997 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 998 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 999 | if (run_queue) |
| 1000 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1001 | |
| 1002 | blk_mq_put_ctx(current_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | static void blk_mq_insert_requests(struct request_queue *q, |
| 1006 | struct blk_mq_ctx *ctx, |
| 1007 | struct list_head *list, |
| 1008 | int depth, |
| 1009 | bool from_schedule) |
| 1010 | |
| 1011 | { |
| 1012 | struct blk_mq_hw_ctx *hctx; |
| 1013 | struct blk_mq_ctx *current_ctx; |
| 1014 | |
| 1015 | trace_block_unplug(q, depth, !from_schedule); |
| 1016 | |
| 1017 | current_ctx = blk_mq_get_ctx(q); |
| 1018 | |
| 1019 | if (!cpu_online(ctx->cpu)) |
| 1020 | ctx = current_ctx; |
| 1021 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 1022 | |
| 1023 | /* |
| 1024 | * preemption doesn't flush plug list, so it's possible ctx->cpu is |
| 1025 | * offline now |
| 1026 | */ |
| 1027 | spin_lock(&ctx->lock); |
| 1028 | while (!list_empty(list)) { |
| 1029 | struct request *rq; |
| 1030 | |
| 1031 | rq = list_first_entry(list, struct request, queuelist); |
| 1032 | list_del_init(&rq->queuelist); |
| 1033 | rq->mq_ctx = ctx; |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 1034 | __blk_mq_insert_request(hctx, rq, false); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1035 | } |
| 1036 | spin_unlock(&ctx->lock); |
| 1037 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1038 | blk_mq_run_hw_queue(hctx, from_schedule); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1039 | blk_mq_put_ctx(current_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1043 | { |
| 1044 | struct request *rqa = container_of(a, struct request, queuelist); |
| 1045 | struct request *rqb = container_of(b, struct request, queuelist); |
| 1046 | |
| 1047 | return !(rqa->mq_ctx < rqb->mq_ctx || |
| 1048 | (rqa->mq_ctx == rqb->mq_ctx && |
| 1049 | blk_rq_pos(rqa) < blk_rq_pos(rqb))); |
| 1050 | } |
| 1051 | |
| 1052 | void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) |
| 1053 | { |
| 1054 | struct blk_mq_ctx *this_ctx; |
| 1055 | struct request_queue *this_q; |
| 1056 | struct request *rq; |
| 1057 | LIST_HEAD(list); |
| 1058 | LIST_HEAD(ctx_list); |
| 1059 | unsigned int depth; |
| 1060 | |
| 1061 | list_splice_init(&plug->mq_list, &list); |
| 1062 | |
| 1063 | list_sort(NULL, &list, plug_ctx_cmp); |
| 1064 | |
| 1065 | this_q = NULL; |
| 1066 | this_ctx = NULL; |
| 1067 | depth = 0; |
| 1068 | |
| 1069 | while (!list_empty(&list)) { |
| 1070 | rq = list_entry_rq(list.next); |
| 1071 | list_del_init(&rq->queuelist); |
| 1072 | BUG_ON(!rq->q); |
| 1073 | if (rq->mq_ctx != this_ctx) { |
| 1074 | if (this_ctx) { |
| 1075 | blk_mq_insert_requests(this_q, this_ctx, |
| 1076 | &ctx_list, depth, |
| 1077 | from_schedule); |
| 1078 | } |
| 1079 | |
| 1080 | this_ctx = rq->mq_ctx; |
| 1081 | this_q = rq->q; |
| 1082 | depth = 0; |
| 1083 | } |
| 1084 | |
| 1085 | depth++; |
| 1086 | list_add_tail(&rq->queuelist, &ctx_list); |
| 1087 | } |
| 1088 | |
| 1089 | /* |
| 1090 | * If 'this_ctx' is set, we know we have entries to complete |
| 1091 | * on 'ctx_list'. Do those. |
| 1092 | */ |
| 1093 | if (this_ctx) { |
| 1094 | blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth, |
| 1095 | from_schedule); |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | static void blk_mq_bio_to_request(struct request *rq, struct bio *bio) |
| 1100 | { |
| 1101 | init_request_from_bio(rq, bio); |
Jens Axboe | 4b57052 | 2014-05-29 11:00:11 -0600 | [diff] [blame] | 1102 | |
| 1103 | if (blk_do_io_stat(rq)) { |
| 1104 | rq->start_time = jiffies; |
| 1105 | blk_account_io_start(rq, 1); |
| 1106 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1107 | } |
| 1108 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1109 | static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx, |
| 1110 | struct blk_mq_ctx *ctx, |
| 1111 | struct request *rq, struct bio *bio) |
| 1112 | { |
| 1113 | struct request_queue *q = hctx->queue; |
| 1114 | |
| 1115 | if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) { |
| 1116 | blk_mq_bio_to_request(rq, bio); |
| 1117 | spin_lock(&ctx->lock); |
| 1118 | insert_rq: |
| 1119 | __blk_mq_insert_request(hctx, rq, false); |
| 1120 | spin_unlock(&ctx->lock); |
| 1121 | return false; |
| 1122 | } else { |
| 1123 | spin_lock(&ctx->lock); |
| 1124 | if (!blk_mq_attempt_merge(q, ctx, bio)) { |
| 1125 | blk_mq_bio_to_request(rq, bio); |
| 1126 | goto insert_rq; |
| 1127 | } |
| 1128 | |
| 1129 | spin_unlock(&ctx->lock); |
| 1130 | __blk_mq_free_request(hctx, ctx, rq); |
| 1131 | return true; |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | struct blk_map_ctx { |
| 1136 | struct blk_mq_hw_ctx *hctx; |
| 1137 | struct blk_mq_ctx *ctx; |
| 1138 | }; |
| 1139 | |
| 1140 | static struct request *blk_mq_map_request(struct request_queue *q, |
| 1141 | struct bio *bio, |
| 1142 | struct blk_map_ctx *data) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1143 | { |
| 1144 | struct blk_mq_hw_ctx *hctx; |
| 1145 | struct blk_mq_ctx *ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1146 | struct request *rq; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1147 | int rw = bio_data_dir(bio); |
Ming Lei | cb96a42 | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 1148 | struct blk_mq_alloc_data alloc_data; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1149 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1150 | if (unlikely(blk_mq_queue_enter(q))) { |
Nicholas Bellinger | 14ec77f | 2014-02-07 13:45:39 -0700 | [diff] [blame] | 1151 | bio_endio(bio, -EIO); |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1152 | return NULL; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | ctx = blk_mq_get_ctx(q); |
| 1156 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 1157 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1158 | if (rw_is_sync(bio->bi_rw)) |
Shaohua Li | 27fbf4e | 2014-02-19 20:20:21 +0800 | [diff] [blame] | 1159 | rw |= REQ_SYNC; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1160 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1161 | trace_block_getrq(q, bio, rw); |
Ming Lei | cb96a42 | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 1162 | blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx, |
| 1163 | hctx); |
| 1164 | rq = __blk_mq_alloc_request(&alloc_data, rw); |
Christoph Hellwig | 5dee857 | 2014-05-27 20:59:47 +0200 | [diff] [blame] | 1165 | if (unlikely(!rq)) { |
Christoph Hellwig | 793597a | 2014-05-27 20:59:49 +0200 | [diff] [blame] | 1166 | __blk_mq_run_hw_queue(hctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1167 | blk_mq_put_ctx(ctx); |
| 1168 | trace_block_sleeprq(q, bio, rw); |
Christoph Hellwig | 793597a | 2014-05-27 20:59:49 +0200 | [diff] [blame] | 1169 | |
| 1170 | ctx = blk_mq_get_ctx(q); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1171 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
Ming Lei | cb96a42 | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 1172 | blk_mq_set_alloc_data(&alloc_data, q, |
| 1173 | __GFP_WAIT|GFP_ATOMIC, false, ctx, hctx); |
| 1174 | rq = __blk_mq_alloc_request(&alloc_data, rw); |
| 1175 | ctx = alloc_data.ctx; |
| 1176 | hctx = alloc_data.hctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | hctx->queued++; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1180 | data->hctx = hctx; |
| 1181 | data->ctx = ctx; |
| 1182 | return rq; |
| 1183 | } |
| 1184 | |
| 1185 | /* |
| 1186 | * Multiple hardware queue variant. This will not use per-process plugs, |
| 1187 | * but will attempt to bypass the hctx queueing if we can go straight to |
| 1188 | * hardware for SYNC IO. |
| 1189 | */ |
| 1190 | static void blk_mq_make_request(struct request_queue *q, struct bio *bio) |
| 1191 | { |
| 1192 | const int is_sync = rw_is_sync(bio->bi_rw); |
| 1193 | const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA); |
| 1194 | struct blk_map_ctx data; |
| 1195 | struct request *rq; |
| 1196 | |
| 1197 | blk_queue_bounce(q, &bio); |
| 1198 | |
| 1199 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { |
| 1200 | bio_endio(bio, -EIO); |
| 1201 | return; |
| 1202 | } |
| 1203 | |
| 1204 | rq = blk_mq_map_request(q, bio, &data); |
| 1205 | if (unlikely(!rq)) |
| 1206 | return; |
| 1207 | |
| 1208 | if (unlikely(is_flush_fua)) { |
| 1209 | blk_mq_bio_to_request(rq, bio); |
| 1210 | blk_insert_flush(rq); |
| 1211 | goto run_queue; |
| 1212 | } |
| 1213 | |
| 1214 | if (is_sync) { |
| 1215 | int ret; |
| 1216 | |
| 1217 | blk_mq_bio_to_request(rq, bio); |
| 1218 | blk_mq_start_request(rq, true); |
Jens Axboe | feff689 | 2014-05-30 15:42:56 -0600 | [diff] [blame] | 1219 | blk_add_timer(rq); |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1220 | |
| 1221 | /* |
| 1222 | * For OK queue, we are done. For error, kill it. Any other |
| 1223 | * error (busy), just add it to our list as we previously |
| 1224 | * would have done |
| 1225 | */ |
| 1226 | ret = q->mq_ops->queue_rq(data.hctx, rq); |
| 1227 | if (ret == BLK_MQ_RQ_QUEUE_OK) |
| 1228 | goto done; |
| 1229 | else { |
| 1230 | __blk_mq_requeue_request(rq); |
| 1231 | |
| 1232 | if (ret == BLK_MQ_RQ_QUEUE_ERROR) { |
| 1233 | rq->errors = -EIO; |
| 1234 | blk_mq_end_io(rq, rq->errors); |
| 1235 | goto done; |
| 1236 | } |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) { |
| 1241 | /* |
| 1242 | * For a SYNC request, send it to the hardware immediately. For |
| 1243 | * an ASYNC request, just ensure that we run it later on. The |
| 1244 | * latter allows for merging opportunities and more efficient |
| 1245 | * dispatching. |
| 1246 | */ |
| 1247 | run_queue: |
| 1248 | blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua); |
| 1249 | } |
| 1250 | done: |
| 1251 | blk_mq_put_ctx(data.ctx); |
| 1252 | } |
| 1253 | |
| 1254 | /* |
| 1255 | * Single hardware queue variant. This will attempt to use any per-process |
| 1256 | * plug for merging and IO deferral. |
| 1257 | */ |
| 1258 | static void blk_sq_make_request(struct request_queue *q, struct bio *bio) |
| 1259 | { |
| 1260 | const int is_sync = rw_is_sync(bio->bi_rw); |
| 1261 | const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA); |
| 1262 | unsigned int use_plug, request_count = 0; |
| 1263 | struct blk_map_ctx data; |
| 1264 | struct request *rq; |
| 1265 | |
| 1266 | /* |
| 1267 | * If we have multiple hardware queues, just go directly to |
| 1268 | * one of those for sync IO. |
| 1269 | */ |
| 1270 | use_plug = !is_flush_fua && !is_sync; |
| 1271 | |
| 1272 | blk_queue_bounce(q, &bio); |
| 1273 | |
| 1274 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { |
| 1275 | bio_endio(bio, -EIO); |
| 1276 | return; |
| 1277 | } |
| 1278 | |
| 1279 | if (use_plug && !blk_queue_nomerges(q) && |
| 1280 | blk_attempt_plug_merge(q, bio, &request_count)) |
| 1281 | return; |
| 1282 | |
| 1283 | rq = blk_mq_map_request(q, bio, &data); |
Jens Axboe | ff87bce | 2014-06-03 11:59:49 -0600 | [diff] [blame] | 1284 | if (unlikely(!rq)) |
| 1285 | return; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1286 | |
| 1287 | if (unlikely(is_flush_fua)) { |
| 1288 | blk_mq_bio_to_request(rq, bio); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1289 | blk_insert_flush(rq); |
| 1290 | goto run_queue; |
| 1291 | } |
| 1292 | |
| 1293 | /* |
| 1294 | * A task plug currently exists. Since this is completely lockless, |
| 1295 | * utilize that to temporarily store requests until the task is |
| 1296 | * either done or scheduled away. |
| 1297 | */ |
| 1298 | if (use_plug) { |
| 1299 | struct blk_plug *plug = current->plug; |
| 1300 | |
| 1301 | if (plug) { |
| 1302 | blk_mq_bio_to_request(rq, bio); |
Shaohua Li | 92f399c | 2013-10-29 12:01:03 -0600 | [diff] [blame] | 1303 | if (list_empty(&plug->mq_list)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1304 | trace_block_plug(q); |
| 1305 | else if (request_count >= BLK_MAX_REQUEST_COUNT) { |
| 1306 | blk_flush_plug_list(plug, false); |
| 1307 | trace_block_plug(q); |
| 1308 | } |
| 1309 | list_add_tail(&rq->queuelist, &plug->mq_list); |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1310 | blk_mq_put_ctx(data.ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1311 | return; |
| 1312 | } |
| 1313 | } |
| 1314 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1315 | if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) { |
| 1316 | /* |
| 1317 | * For a SYNC request, send it to the hardware immediately. For |
| 1318 | * an ASYNC request, just ensure that we run it later on. The |
| 1319 | * latter allows for merging opportunities and more efficient |
| 1320 | * dispatching. |
| 1321 | */ |
| 1322 | run_queue: |
| 1323 | blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1324 | } |
| 1325 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1326 | blk_mq_put_ctx(data.ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | /* |
| 1330 | * Default mapping to a software queue, since we use one per CPU. |
| 1331 | */ |
| 1332 | struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu) |
| 1333 | { |
| 1334 | return q->queue_hw_ctx[q->mq_map[cpu]]; |
| 1335 | } |
| 1336 | EXPORT_SYMBOL(blk_mq_map_queue); |
| 1337 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1338 | static void blk_mq_free_rq_map(struct blk_mq_tag_set *set, |
| 1339 | struct blk_mq_tags *tags, unsigned int hctx_idx) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1340 | { |
| 1341 | struct page *page; |
| 1342 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1343 | if (tags->rqs && set->ops->exit_request) { |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1344 | int i; |
| 1345 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1346 | for (i = 0; i < tags->nr_tags; i++) { |
| 1347 | if (!tags->rqs[i]) |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1348 | continue; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1349 | set->ops->exit_request(set->driver_data, tags->rqs[i], |
| 1350 | hctx_idx, i); |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1354 | while (!list_empty(&tags->page_list)) { |
| 1355 | page = list_first_entry(&tags->page_list, struct page, lru); |
Dave Hansen | 6753471 | 2014-01-08 20:17:46 -0700 | [diff] [blame] | 1356 | list_del_init(&page->lru); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1357 | __free_pages(page, page->private); |
| 1358 | } |
| 1359 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1360 | kfree(tags->rqs); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1361 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1362 | blk_mq_free_tags(tags); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | static size_t order_to_size(unsigned int order) |
| 1366 | { |
Ming Lei | 4ca0850 | 2014-04-19 18:00:18 +0800 | [diff] [blame] | 1367 | return (size_t)PAGE_SIZE << order; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1368 | } |
| 1369 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1370 | static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set, |
| 1371 | unsigned int hctx_idx) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1372 | { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1373 | struct blk_mq_tags *tags; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1374 | unsigned int i, j, entries_per_page, max_order = 4; |
| 1375 | size_t rq_size, left; |
| 1376 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1377 | tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags, |
| 1378 | set->numa_node); |
| 1379 | if (!tags) |
| 1380 | return NULL; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1381 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1382 | INIT_LIST_HEAD(&tags->page_list); |
| 1383 | |
| 1384 | tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *), |
| 1385 | GFP_KERNEL, set->numa_node); |
| 1386 | if (!tags->rqs) { |
| 1387 | blk_mq_free_tags(tags); |
| 1388 | return NULL; |
| 1389 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1390 | |
| 1391 | /* |
| 1392 | * rq_size is the size of the request plus driver payload, rounded |
| 1393 | * to the cacheline size |
| 1394 | */ |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1395 | rq_size = round_up(sizeof(struct request) + set->cmd_size, |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1396 | cache_line_size()); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1397 | left = rq_size * set->queue_depth; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1398 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1399 | for (i = 0; i < set->queue_depth; ) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1400 | int this_order = max_order; |
| 1401 | struct page *page; |
| 1402 | int to_do; |
| 1403 | void *p; |
| 1404 | |
| 1405 | while (left < order_to_size(this_order - 1) && this_order) |
| 1406 | this_order--; |
| 1407 | |
| 1408 | do { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1409 | page = alloc_pages_node(set->numa_node, GFP_KERNEL, |
| 1410 | this_order); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1411 | if (page) |
| 1412 | break; |
| 1413 | if (!this_order--) |
| 1414 | break; |
| 1415 | if (order_to_size(this_order) < rq_size) |
| 1416 | break; |
| 1417 | } while (1); |
| 1418 | |
| 1419 | if (!page) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1420 | goto fail; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1421 | |
| 1422 | page->private = this_order; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1423 | list_add_tail(&page->lru, &tags->page_list); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1424 | |
| 1425 | p = page_address(page); |
| 1426 | entries_per_page = order_to_size(this_order) / rq_size; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1427 | to_do = min(entries_per_page, set->queue_depth - i); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1428 | left -= to_do * rq_size; |
| 1429 | for (j = 0; j < to_do; j++) { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1430 | tags->rqs[i] = p; |
| 1431 | if (set->ops->init_request) { |
| 1432 | if (set->ops->init_request(set->driver_data, |
| 1433 | tags->rqs[i], hctx_idx, i, |
| 1434 | set->numa_node)) |
| 1435 | goto fail; |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1436 | } |
| 1437 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1438 | p += rq_size; |
| 1439 | i++; |
| 1440 | } |
| 1441 | } |
| 1442 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1443 | return tags; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1444 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1445 | fail: |
| 1446 | pr_warn("%s: failed to allocate requests\n", __func__); |
| 1447 | blk_mq_free_rq_map(set, tags, hctx_idx); |
| 1448 | return NULL; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1449 | } |
| 1450 | |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 1451 | static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap) |
| 1452 | { |
| 1453 | kfree(bitmap->map); |
| 1454 | } |
| 1455 | |
| 1456 | static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node) |
| 1457 | { |
| 1458 | unsigned int bpw = 8, total, num_maps, i; |
| 1459 | |
| 1460 | bitmap->bits_per_word = bpw; |
| 1461 | |
| 1462 | num_maps = ALIGN(nr_cpu_ids, bpw) / bpw; |
| 1463 | bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap), |
| 1464 | GFP_KERNEL, node); |
| 1465 | if (!bitmap->map) |
| 1466 | return -ENOMEM; |
| 1467 | |
| 1468 | bitmap->map_size = num_maps; |
| 1469 | |
| 1470 | total = nr_cpu_ids; |
| 1471 | for (i = 0; i < num_maps; i++) { |
| 1472 | bitmap->map[i].depth = min(total, bitmap->bits_per_word); |
| 1473 | total -= bitmap->map[i].depth; |
| 1474 | } |
| 1475 | |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1479 | static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu) |
| 1480 | { |
| 1481 | struct request_queue *q = hctx->queue; |
| 1482 | struct blk_mq_ctx *ctx; |
| 1483 | LIST_HEAD(tmp); |
| 1484 | |
| 1485 | /* |
| 1486 | * Move ctx entries to new CPU, if this one is going away. |
| 1487 | */ |
| 1488 | ctx = __blk_mq_get_ctx(q, cpu); |
| 1489 | |
| 1490 | spin_lock(&ctx->lock); |
| 1491 | if (!list_empty(&ctx->rq_list)) { |
| 1492 | list_splice_init(&ctx->rq_list, &tmp); |
| 1493 | blk_mq_hctx_clear_pending(hctx, ctx); |
| 1494 | } |
| 1495 | spin_unlock(&ctx->lock); |
| 1496 | |
| 1497 | if (list_empty(&tmp)) |
| 1498 | return NOTIFY_OK; |
| 1499 | |
| 1500 | ctx = blk_mq_get_ctx(q); |
| 1501 | spin_lock(&ctx->lock); |
| 1502 | |
| 1503 | while (!list_empty(&tmp)) { |
| 1504 | struct request *rq; |
| 1505 | |
| 1506 | rq = list_first_entry(&tmp, struct request, queuelist); |
| 1507 | rq->mq_ctx = ctx; |
| 1508 | list_move_tail(&rq->queuelist, &ctx->rq_list); |
| 1509 | } |
| 1510 | |
| 1511 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 1512 | blk_mq_hctx_mark_pending(hctx, ctx); |
| 1513 | |
| 1514 | spin_unlock(&ctx->lock); |
| 1515 | |
| 1516 | blk_mq_run_hw_queue(hctx, true); |
| 1517 | blk_mq_put_ctx(ctx); |
| 1518 | return NOTIFY_OK; |
| 1519 | } |
| 1520 | |
| 1521 | static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu) |
| 1522 | { |
| 1523 | struct request_queue *q = hctx->queue; |
| 1524 | struct blk_mq_tag_set *set = q->tag_set; |
| 1525 | |
| 1526 | if (set->tags[hctx->queue_num]) |
| 1527 | return NOTIFY_OK; |
| 1528 | |
| 1529 | set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num); |
| 1530 | if (!set->tags[hctx->queue_num]) |
| 1531 | return NOTIFY_STOP; |
| 1532 | |
| 1533 | hctx->tags = set->tags[hctx->queue_num]; |
| 1534 | return NOTIFY_OK; |
| 1535 | } |
| 1536 | |
| 1537 | static int blk_mq_hctx_notify(void *data, unsigned long action, |
| 1538 | unsigned int cpu) |
| 1539 | { |
| 1540 | struct blk_mq_hw_ctx *hctx = data; |
| 1541 | |
| 1542 | if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) |
| 1543 | return blk_mq_hctx_cpu_offline(hctx, cpu); |
| 1544 | else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) |
| 1545 | return blk_mq_hctx_cpu_online(hctx, cpu); |
| 1546 | |
| 1547 | return NOTIFY_OK; |
| 1548 | } |
| 1549 | |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1550 | static void blk_mq_exit_hw_queues(struct request_queue *q, |
| 1551 | struct blk_mq_tag_set *set, int nr_queue) |
| 1552 | { |
| 1553 | struct blk_mq_hw_ctx *hctx; |
| 1554 | unsigned int i; |
| 1555 | |
| 1556 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1557 | if (i == nr_queue) |
| 1558 | break; |
| 1559 | |
Jens Axboe | f899fed | 2014-06-04 09:11:53 -0600 | [diff] [blame] | 1560 | blk_mq_tag_idle(hctx); |
| 1561 | |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1562 | if (set->ops->exit_hctx) |
| 1563 | set->ops->exit_hctx(hctx, i); |
| 1564 | |
| 1565 | blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier); |
| 1566 | kfree(hctx->ctxs); |
| 1567 | blk_mq_free_bitmap(&hctx->ctx_map); |
| 1568 | } |
| 1569 | |
| 1570 | } |
| 1571 | |
| 1572 | static void blk_mq_free_hw_queues(struct request_queue *q, |
| 1573 | struct blk_mq_tag_set *set) |
| 1574 | { |
| 1575 | struct blk_mq_hw_ctx *hctx; |
| 1576 | unsigned int i; |
| 1577 | |
| 1578 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1579 | free_cpumask_var(hctx->cpumask); |
Christoph Hellwig | cdef54d | 2014-05-28 18:11:06 +0200 | [diff] [blame] | 1580 | kfree(hctx); |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1581 | } |
| 1582 | } |
| 1583 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1584 | static int blk_mq_init_hw_queues(struct request_queue *q, |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1585 | struct blk_mq_tag_set *set) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1586 | { |
| 1587 | struct blk_mq_hw_ctx *hctx; |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1588 | unsigned int i; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1589 | |
| 1590 | /* |
| 1591 | * Initialize hardware queues |
| 1592 | */ |
| 1593 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1594 | int node; |
| 1595 | |
| 1596 | node = hctx->numa_node; |
| 1597 | if (node == NUMA_NO_NODE) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1598 | node = hctx->numa_node = set->numa_node; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1599 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 1600 | INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn); |
| 1601 | INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1602 | spin_lock_init(&hctx->lock); |
| 1603 | INIT_LIST_HEAD(&hctx->dispatch); |
| 1604 | hctx->queue = q; |
| 1605 | hctx->queue_num = i; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1606 | hctx->flags = set->flags; |
| 1607 | hctx->cmd_size = set->cmd_size; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1608 | |
| 1609 | blk_mq_init_cpu_notifier(&hctx->cpu_notifier, |
| 1610 | blk_mq_hctx_notify, hctx); |
| 1611 | blk_mq_register_cpu_notifier(&hctx->cpu_notifier); |
| 1612 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1613 | hctx->tags = set->tags[i]; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1614 | |
| 1615 | /* |
| 1616 | * Allocate space for all possible cpus to avoid allocation in |
| 1617 | * runtime |
| 1618 | */ |
| 1619 | hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *), |
| 1620 | GFP_KERNEL, node); |
| 1621 | if (!hctx->ctxs) |
| 1622 | break; |
| 1623 | |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 1624 | if (blk_mq_alloc_bitmap(&hctx->ctx_map, node)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1625 | break; |
| 1626 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1627 | hctx->nr_ctx = 0; |
| 1628 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1629 | if (set->ops->init_hctx && |
| 1630 | set->ops->init_hctx(hctx, set->driver_data, i)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1631 | break; |
| 1632 | } |
| 1633 | |
| 1634 | if (i == q->nr_hw_queues) |
| 1635 | return 0; |
| 1636 | |
| 1637 | /* |
| 1638 | * Init failed |
| 1639 | */ |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1640 | blk_mq_exit_hw_queues(q, set, i); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1641 | |
| 1642 | return 1; |
| 1643 | } |
| 1644 | |
| 1645 | static void blk_mq_init_cpu_queues(struct request_queue *q, |
| 1646 | unsigned int nr_hw_queues) |
| 1647 | { |
| 1648 | unsigned int i; |
| 1649 | |
| 1650 | for_each_possible_cpu(i) { |
| 1651 | struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); |
| 1652 | struct blk_mq_hw_ctx *hctx; |
| 1653 | |
| 1654 | memset(__ctx, 0, sizeof(*__ctx)); |
| 1655 | __ctx->cpu = i; |
| 1656 | spin_lock_init(&__ctx->lock); |
| 1657 | INIT_LIST_HEAD(&__ctx->rq_list); |
| 1658 | __ctx->queue = q; |
| 1659 | |
| 1660 | /* 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] | 1661 | if (!cpu_online(i)) |
| 1662 | continue; |
| 1663 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1664 | hctx = q->mq_ops->map_queue(q, i); |
| 1665 | cpumask_set_cpu(i, hctx->cpumask); |
| 1666 | hctx->nr_ctx++; |
| 1667 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1668 | /* |
| 1669 | * Set local node, IFF we have more than one hw queue. If |
| 1670 | * not, we remain on the home node of the device |
| 1671 | */ |
| 1672 | if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) |
| 1673 | hctx->numa_node = cpu_to_node(i); |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | static void blk_mq_map_swqueue(struct request_queue *q) |
| 1678 | { |
| 1679 | unsigned int i; |
| 1680 | struct blk_mq_hw_ctx *hctx; |
| 1681 | struct blk_mq_ctx *ctx; |
| 1682 | |
| 1683 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1684 | cpumask_clear(hctx->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1685 | hctx->nr_ctx = 0; |
| 1686 | } |
| 1687 | |
| 1688 | /* |
| 1689 | * Map software to hardware queues |
| 1690 | */ |
| 1691 | queue_for_each_ctx(q, ctx, i) { |
| 1692 | /* 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] | 1693 | if (!cpu_online(i)) |
| 1694 | continue; |
| 1695 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1696 | hctx = q->mq_ops->map_queue(q, i); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1697 | cpumask_set_cpu(i, hctx->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1698 | ctx->index_hw = hctx->nr_ctx; |
| 1699 | hctx->ctxs[hctx->nr_ctx++] = ctx; |
| 1700 | } |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 1701 | |
| 1702 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1703 | /* |
| 1704 | * If not software queues are mapped to this hardware queue, |
| 1705 | * disable it and free the request entries |
| 1706 | */ |
| 1707 | if (!hctx->nr_ctx) { |
| 1708 | struct blk_mq_tag_set *set = q->tag_set; |
| 1709 | |
| 1710 | if (set->tags[i]) { |
| 1711 | blk_mq_free_rq_map(set, set->tags[i], i); |
| 1712 | set->tags[i] = NULL; |
| 1713 | hctx->tags = NULL; |
| 1714 | } |
| 1715 | continue; |
| 1716 | } |
| 1717 | |
| 1718 | /* |
| 1719 | * Initialize batch roundrobin counts |
| 1720 | */ |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 1721 | hctx->next_cpu = cpumask_first(hctx->cpumask); |
| 1722 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
| 1723 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1724 | } |
| 1725 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1726 | static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set) |
| 1727 | { |
| 1728 | struct blk_mq_hw_ctx *hctx; |
| 1729 | struct request_queue *q; |
| 1730 | bool shared; |
| 1731 | int i; |
| 1732 | |
| 1733 | if (set->tag_list.next == set->tag_list.prev) |
| 1734 | shared = false; |
| 1735 | else |
| 1736 | shared = true; |
| 1737 | |
| 1738 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
| 1739 | blk_mq_freeze_queue(q); |
| 1740 | |
| 1741 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1742 | if (shared) |
| 1743 | hctx->flags |= BLK_MQ_F_TAG_SHARED; |
| 1744 | else |
| 1745 | hctx->flags &= ~BLK_MQ_F_TAG_SHARED; |
| 1746 | } |
| 1747 | blk_mq_unfreeze_queue(q); |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | static void blk_mq_del_queue_tag_set(struct request_queue *q) |
| 1752 | { |
| 1753 | struct blk_mq_tag_set *set = q->tag_set; |
| 1754 | |
| 1755 | blk_mq_freeze_queue(q); |
| 1756 | |
| 1757 | mutex_lock(&set->tag_list_lock); |
| 1758 | list_del_init(&q->tag_set_list); |
| 1759 | blk_mq_update_tag_set_depth(set); |
| 1760 | mutex_unlock(&set->tag_list_lock); |
| 1761 | |
| 1762 | blk_mq_unfreeze_queue(q); |
| 1763 | } |
| 1764 | |
| 1765 | static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set, |
| 1766 | struct request_queue *q) |
| 1767 | { |
| 1768 | q->tag_set = set; |
| 1769 | |
| 1770 | mutex_lock(&set->tag_list_lock); |
| 1771 | list_add_tail(&q->tag_set_list, &set->tag_list); |
| 1772 | blk_mq_update_tag_set_depth(set); |
| 1773 | mutex_unlock(&set->tag_list_lock); |
| 1774 | } |
| 1775 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1776 | 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] | 1777 | { |
| 1778 | struct blk_mq_hw_ctx **hctxs; |
Ming Lei | e6cdb09 | 2014-06-03 11:24:06 +0800 | [diff] [blame] | 1779 | struct blk_mq_ctx __percpu *ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1780 | struct request_queue *q; |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1781 | unsigned int *map; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1782 | int i; |
| 1783 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1784 | ctx = alloc_percpu(struct blk_mq_ctx); |
| 1785 | if (!ctx) |
| 1786 | return ERR_PTR(-ENOMEM); |
| 1787 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1788 | hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL, |
| 1789 | set->numa_node); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1790 | |
| 1791 | if (!hctxs) |
| 1792 | goto err_percpu; |
| 1793 | |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1794 | map = blk_mq_make_queue_map(set); |
| 1795 | if (!map) |
| 1796 | goto err_map; |
| 1797 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1798 | for (i = 0; i < set->nr_hw_queues; i++) { |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1799 | int node = blk_mq_hw_queue_to_node(map, i); |
| 1800 | |
Christoph Hellwig | cdef54d | 2014-05-28 18:11:06 +0200 | [diff] [blame] | 1801 | hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx), |
| 1802 | GFP_KERNEL, node); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1803 | if (!hctxs[i]) |
| 1804 | goto err_hctxs; |
| 1805 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1806 | if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL)) |
| 1807 | goto err_hctxs; |
| 1808 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1809 | atomic_set(&hctxs[i]->nr_active, 0); |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1810 | hctxs[i]->numa_node = node; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1811 | hctxs[i]->queue_num = i; |
| 1812 | } |
| 1813 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1814 | q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1815 | if (!q) |
| 1816 | goto err_hctxs; |
| 1817 | |
Ming Lei | 3d2936f | 2014-05-27 23:35:14 +0800 | [diff] [blame] | 1818 | if (percpu_counter_init(&q->mq_usage_counter, 0)) |
| 1819 | goto err_map; |
| 1820 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1821 | setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q); |
| 1822 | blk_queue_rq_timeout(q, 30000); |
| 1823 | |
| 1824 | q->nr_queues = nr_cpu_ids; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1825 | q->nr_hw_queues = set->nr_hw_queues; |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1826 | q->mq_map = map; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1827 | |
| 1828 | q->queue_ctx = ctx; |
| 1829 | q->queue_hw_ctx = hctxs; |
| 1830 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1831 | q->mq_ops = set->ops; |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 1832 | q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1833 | |
Jens Axboe | 05f1dd5 | 2014-05-29 09:53:32 -0600 | [diff] [blame] | 1834 | if (!(set->flags & BLK_MQ_F_SG_MERGE)) |
| 1835 | q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE; |
| 1836 | |
Christoph Hellwig | 1be036e | 2014-02-07 10:22:39 -0800 | [diff] [blame] | 1837 | q->sg_reserved_size = INT_MAX; |
| 1838 | |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame] | 1839 | INIT_WORK(&q->requeue_work, blk_mq_requeue_work); |
| 1840 | INIT_LIST_HEAD(&q->requeue_list); |
| 1841 | spin_lock_init(&q->requeue_lock); |
| 1842 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1843 | if (q->nr_hw_queues > 1) |
| 1844 | blk_queue_make_request(q, blk_mq_make_request); |
| 1845 | else |
| 1846 | blk_queue_make_request(q, blk_sq_make_request); |
| 1847 | |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 1848 | blk_queue_rq_timed_out(q, blk_mq_rq_timed_out); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1849 | if (set->timeout) |
| 1850 | blk_queue_rq_timeout(q, set->timeout); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1851 | |
Jens Axboe | eba7176 | 2014-05-20 15:17:27 -0600 | [diff] [blame] | 1852 | /* |
| 1853 | * Do this after blk_queue_make_request() overrides it... |
| 1854 | */ |
| 1855 | q->nr_requests = set->queue_depth; |
| 1856 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1857 | if (set->ops->complete) |
| 1858 | blk_queue_softirq_done(q, set->ops->complete); |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 1859 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1860 | blk_mq_init_flush(q); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1861 | blk_mq_init_cpu_queues(q, set->nr_hw_queues); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1862 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1863 | q->flush_rq = kzalloc(round_up(sizeof(struct request) + |
| 1864 | set->cmd_size, cache_line_size()), |
| 1865 | GFP_KERNEL); |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1866 | if (!q->flush_rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1867 | goto err_hw; |
| 1868 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1869 | if (blk_mq_init_hw_queues(q, set)) |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1870 | goto err_flush_rq; |
| 1871 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1872 | mutex_lock(&all_q_mutex); |
| 1873 | list_add_tail(&q->all_q_node, &all_q_list); |
| 1874 | mutex_unlock(&all_q_mutex); |
| 1875 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1876 | blk_mq_add_queue_tag_set(set, q); |
| 1877 | |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1878 | blk_mq_map_swqueue(q); |
| 1879 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1880 | return q; |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1881 | |
| 1882 | err_flush_rq: |
| 1883 | kfree(q->flush_rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1884 | err_hw: |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1885 | blk_cleanup_queue(q); |
| 1886 | err_hctxs: |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1887 | kfree(map); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1888 | for (i = 0; i < set->nr_hw_queues; i++) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1889 | if (!hctxs[i]) |
| 1890 | break; |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1891 | free_cpumask_var(hctxs[i]->cpumask); |
Christoph Hellwig | cdef54d | 2014-05-28 18:11:06 +0200 | [diff] [blame] | 1892 | kfree(hctxs[i]); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1893 | } |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1894 | err_map: |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1895 | kfree(hctxs); |
| 1896 | err_percpu: |
| 1897 | free_percpu(ctx); |
| 1898 | return ERR_PTR(-ENOMEM); |
| 1899 | } |
| 1900 | EXPORT_SYMBOL(blk_mq_init_queue); |
| 1901 | |
| 1902 | void blk_mq_free_queue(struct request_queue *q) |
| 1903 | { |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1904 | struct blk_mq_tag_set *set = q->tag_set; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1905 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1906 | blk_mq_del_queue_tag_set(q); |
| 1907 | |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1908 | blk_mq_exit_hw_queues(q, set, set->nr_hw_queues); |
| 1909 | blk_mq_free_hw_queues(q, set); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1910 | |
Ming Lei | 3d2936f | 2014-05-27 23:35:14 +0800 | [diff] [blame] | 1911 | percpu_counter_destroy(&q->mq_usage_counter); |
| 1912 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1913 | free_percpu(q->queue_ctx); |
| 1914 | kfree(q->queue_hw_ctx); |
| 1915 | kfree(q->mq_map); |
| 1916 | |
| 1917 | q->queue_ctx = NULL; |
| 1918 | q->queue_hw_ctx = NULL; |
| 1919 | q->mq_map = NULL; |
| 1920 | |
| 1921 | mutex_lock(&all_q_mutex); |
| 1922 | list_del_init(&q->all_q_node); |
| 1923 | mutex_unlock(&all_q_mutex); |
| 1924 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1925 | |
| 1926 | /* Basically redo blk_mq_init_queue with queue frozen */ |
Paul Gortmaker | f618ef7 | 2013-11-14 08:26:02 -0700 | [diff] [blame] | 1927 | static void blk_mq_queue_reinit(struct request_queue *q) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1928 | { |
| 1929 | blk_mq_freeze_queue(q); |
| 1930 | |
Jens Axboe | 67aec14 | 2014-05-30 08:25:36 -0600 | [diff] [blame] | 1931 | blk_mq_sysfs_unregister(q); |
| 1932 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1933 | blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues); |
| 1934 | |
| 1935 | /* |
| 1936 | * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe |
| 1937 | * we should change hctx numa_node according to new topology (this |
| 1938 | * involves free and re-allocate memory, worthy doing?) |
| 1939 | */ |
| 1940 | |
| 1941 | blk_mq_map_swqueue(q); |
| 1942 | |
Jens Axboe | 67aec14 | 2014-05-30 08:25:36 -0600 | [diff] [blame] | 1943 | blk_mq_sysfs_register(q); |
| 1944 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1945 | blk_mq_unfreeze_queue(q); |
| 1946 | } |
| 1947 | |
Paul Gortmaker | f618ef7 | 2013-11-14 08:26:02 -0700 | [diff] [blame] | 1948 | static int blk_mq_queue_reinit_notify(struct notifier_block *nb, |
| 1949 | unsigned long action, void *hcpu) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1950 | { |
| 1951 | struct request_queue *q; |
| 1952 | |
| 1953 | /* |
Jens Axboe | 9fccfed | 2014-05-08 14:50:19 -0600 | [diff] [blame] | 1954 | * Before new mappings are established, hotadded cpu might already |
| 1955 | * start handling requests. This doesn't break anything as we map |
| 1956 | * offline CPUs to first hardware queue. We will re-init the queue |
| 1957 | * below to get optimal settings. |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1958 | */ |
| 1959 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN && |
| 1960 | action != CPU_ONLINE && action != CPU_ONLINE_FROZEN) |
| 1961 | return NOTIFY_OK; |
| 1962 | |
| 1963 | mutex_lock(&all_q_mutex); |
| 1964 | list_for_each_entry(q, &all_q_list, all_q_node) |
| 1965 | blk_mq_queue_reinit(q); |
| 1966 | mutex_unlock(&all_q_mutex); |
| 1967 | return NOTIFY_OK; |
| 1968 | } |
| 1969 | |
Jens Axboe | a4391c6 | 2014-06-05 15:21:56 -0600 | [diff] [blame^] | 1970 | /* |
| 1971 | * Alloc a tag set to be associated with one or more request queues. |
| 1972 | * May fail with EINVAL for various error conditions. May adjust the |
| 1973 | * requested depth down, if if it too large. In that case, the set |
| 1974 | * value will be stored in set->queue_depth. |
| 1975 | */ |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1976 | int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) |
| 1977 | { |
| 1978 | int i; |
| 1979 | |
| 1980 | if (!set->nr_hw_queues) |
| 1981 | return -EINVAL; |
Jens Axboe | a4391c6 | 2014-06-05 15:21:56 -0600 | [diff] [blame^] | 1982 | if (!set->queue_depth) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1983 | return -EINVAL; |
| 1984 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) |
| 1985 | return -EINVAL; |
| 1986 | |
Christoph Hellwig | cdef54d | 2014-05-28 18:11:06 +0200 | [diff] [blame] | 1987 | 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] | 1988 | return -EINVAL; |
| 1989 | |
Jens Axboe | a4391c6 | 2014-06-05 15:21:56 -0600 | [diff] [blame^] | 1990 | if (set->queue_depth > BLK_MQ_MAX_DEPTH) { |
| 1991 | pr_info("blk-mq: reduced tag depth to %u\n", |
| 1992 | BLK_MQ_MAX_DEPTH); |
| 1993 | set->queue_depth = BLK_MQ_MAX_DEPTH; |
| 1994 | } |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1995 | |
Ming Lei | 4847900 | 2014-04-19 18:00:17 +0800 | [diff] [blame] | 1996 | set->tags = kmalloc_node(set->nr_hw_queues * |
| 1997 | sizeof(struct blk_mq_tags *), |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1998 | GFP_KERNEL, set->numa_node); |
| 1999 | if (!set->tags) |
| 2000 | goto out; |
| 2001 | |
| 2002 | for (i = 0; i < set->nr_hw_queues; i++) { |
| 2003 | set->tags[i] = blk_mq_init_rq_map(set, i); |
| 2004 | if (!set->tags[i]) |
| 2005 | goto out_unwind; |
| 2006 | } |
| 2007 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 2008 | mutex_init(&set->tag_list_lock); |
| 2009 | INIT_LIST_HEAD(&set->tag_list); |
| 2010 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2011 | return 0; |
| 2012 | |
| 2013 | out_unwind: |
| 2014 | while (--i >= 0) |
| 2015 | blk_mq_free_rq_map(set, set->tags[i], i); |
| 2016 | out: |
| 2017 | return -ENOMEM; |
| 2018 | } |
| 2019 | EXPORT_SYMBOL(blk_mq_alloc_tag_set); |
| 2020 | |
| 2021 | void blk_mq_free_tag_set(struct blk_mq_tag_set *set) |
| 2022 | { |
| 2023 | int i; |
| 2024 | |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 2025 | for (i = 0; i < set->nr_hw_queues; i++) { |
| 2026 | if (set->tags[i]) |
| 2027 | blk_mq_free_rq_map(set, set->tags[i], i); |
| 2028 | } |
| 2029 | |
Ming Lei | 981bd18 | 2014-04-24 00:07:34 +0800 | [diff] [blame] | 2030 | kfree(set->tags); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2031 | } |
| 2032 | EXPORT_SYMBOL(blk_mq_free_tag_set); |
| 2033 | |
Jens Axboe | e3a2b3f | 2014-05-20 11:49:02 -0600 | [diff] [blame] | 2034 | int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) |
| 2035 | { |
| 2036 | struct blk_mq_tag_set *set = q->tag_set; |
| 2037 | struct blk_mq_hw_ctx *hctx; |
| 2038 | int i, ret; |
| 2039 | |
| 2040 | if (!set || nr > set->queue_depth) |
| 2041 | return -EINVAL; |
| 2042 | |
| 2043 | ret = 0; |
| 2044 | queue_for_each_hw_ctx(q, hctx, i) { |
| 2045 | ret = blk_mq_tag_update_depth(hctx->tags, nr); |
| 2046 | if (ret) |
| 2047 | break; |
| 2048 | } |
| 2049 | |
| 2050 | if (!ret) |
| 2051 | q->nr_requests = nr; |
| 2052 | |
| 2053 | return ret; |
| 2054 | } |
| 2055 | |
Jens Axboe | 676141e | 2014-03-20 13:29:18 -0600 | [diff] [blame] | 2056 | void blk_mq_disable_hotplug(void) |
| 2057 | { |
| 2058 | mutex_lock(&all_q_mutex); |
| 2059 | } |
| 2060 | |
| 2061 | void blk_mq_enable_hotplug(void) |
| 2062 | { |
| 2063 | mutex_unlock(&all_q_mutex); |
| 2064 | } |
| 2065 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2066 | static int __init blk_mq_init(void) |
| 2067 | { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2068 | blk_mq_cpu_init(); |
| 2069 | |
| 2070 | /* Must be called after percpu_counter_hotcpu_callback() */ |
| 2071 | hotcpu_notifier(blk_mq_queue_reinit_notify, -10); |
| 2072 | |
| 2073 | return 0; |
| 2074 | } |
| 2075 | subsys_initcall(blk_mq_init); |