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