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