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