1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Block multiqueue core code 4 * 5 * Copyright (C) 2013-2014 Jens Axboe 6 * Copyright (C) 2013-2014 Christoph Hellwig 7 */ 8 #include <linux/kernel.h> 9 #include <linux/module.h> 10 #include <linux/backing-dev.h> 11 #include <linux/bio.h> 12 #include <linux/blkdev.h> 13 #include <linux/kmemleak.h> 14 #include <linux/mm.h> 15 #include <linux/init.h> 16 #include <linux/slab.h> 17 #include <linux/workqueue.h> 18 #include <linux/smp.h> 19 #include <linux/llist.h> 20 #include <linux/list_sort.h> 21 #include <linux/cpu.h> 22 #include <linux/cache.h> 23 #include <linux/sched/sysctl.h> 24 #include <linux/sched/topology.h> 25 #include <linux/sched/signal.h> 26 #include <linux/delay.h> 27 #include <linux/crash_dump.h> 28 #include <linux/prefetch.h> 29 #include <linux/blk-crypto.h> 30 31 #include <trace/events/block.h> 32 33 #include <linux/blk-mq.h> 34 #include <linux/t10-pi.h> 35 #include "blk.h" 36 #include "blk-mq.h" 37 #include "blk-mq-debugfs.h" 38 #include "blk-mq-tag.h" 39 #include "blk-pm.h" 40 #include "blk-stat.h" 41 #include "blk-mq-sched.h" 42 #include "blk-rq-qos.h" 43 44 static DEFINE_PER_CPU(struct llist_head, blk_cpu_done); 45 46 static void blk_mq_poll_stats_start(struct request_queue *q); 47 static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb); 48 49 static int blk_mq_poll_stats_bkt(const struct request *rq) 50 { 51 int ddir, sectors, bucket; 52 53 ddir = rq_data_dir(rq); 54 sectors = blk_rq_stats_sectors(rq); 55 56 bucket = ddir + 2 * ilog2(sectors); 57 58 if (bucket < 0) 59 return -1; 60 else if (bucket >= BLK_MQ_POLL_STATS_BKTS) 61 return ddir + BLK_MQ_POLL_STATS_BKTS - 2; 62 63 return bucket; 64 } 65 66 /* 67 * Check if any of the ctx, dispatch list or elevator 68 * have pending work in this hardware queue. 69 */ 70 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx) 71 { 72 return !list_empty_careful(&hctx->dispatch) || 73 sbitmap_any_bit_set(&hctx->ctx_map) || 74 blk_mq_sched_has_work(hctx); 75 } 76 77 /* 78 * Mark this ctx as having pending work in this hardware queue 79 */ 80 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx, 81 struct blk_mq_ctx *ctx) 82 { 83 const int bit = ctx->index_hw[hctx->type]; 84 85 if (!sbitmap_test_bit(&hctx->ctx_map, bit)) 86 sbitmap_set_bit(&hctx->ctx_map, bit); 87 } 88 89 static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx, 90 struct blk_mq_ctx *ctx) 91 { 92 const int bit = ctx->index_hw[hctx->type]; 93 94 sbitmap_clear_bit(&hctx->ctx_map, bit); 95 } 96 97 struct mq_inflight { 98 struct block_device *part; 99 unsigned int inflight[2]; 100 }; 101 102 static bool blk_mq_check_inflight(struct blk_mq_hw_ctx *hctx, 103 struct request *rq, void *priv, 104 bool reserved) 105 { 106 struct mq_inflight *mi = priv; 107 108 if ((!mi->part->bd_partno || rq->part == mi->part) && 109 blk_mq_rq_state(rq) == MQ_RQ_IN_FLIGHT) 110 mi->inflight[rq_data_dir(rq)]++; 111 112 return true; 113 } 114 115 unsigned int blk_mq_in_flight(struct request_queue *q, 116 struct block_device *part) 117 { 118 struct mq_inflight mi = { .part = part }; 119 120 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi); 121 122 return mi.inflight[0] + mi.inflight[1]; 123 } 124 125 void blk_mq_in_flight_rw(struct request_queue *q, struct block_device *part, 126 unsigned int inflight[2]) 127 { 128 struct mq_inflight mi = { .part = part }; 129 130 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi); 131 inflight[0] = mi.inflight[0]; 132 inflight[1] = mi.inflight[1]; 133 } 134 135 void blk_freeze_queue_start(struct request_queue *q) 136 { 137 mutex_lock(&q->mq_freeze_lock); 138 if (++q->mq_freeze_depth == 1) { 139 percpu_ref_kill(&q->q_usage_counter); 140 mutex_unlock(&q->mq_freeze_lock); 141 if (queue_is_mq(q)) 142 blk_mq_run_hw_queues(q, false); 143 } else { 144 mutex_unlock(&q->mq_freeze_lock); 145 } 146 } 147 EXPORT_SYMBOL_GPL(blk_freeze_queue_start); 148 149 void blk_mq_freeze_queue_wait(struct request_queue *q) 150 { 151 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter)); 152 } 153 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait); 154 155 int blk_mq_freeze_queue_wait_timeout(struct request_queue *q, 156 unsigned long timeout) 157 { 158 return wait_event_timeout(q->mq_freeze_wq, 159 percpu_ref_is_zero(&q->q_usage_counter), 160 timeout); 161 } 162 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout); 163 164 /* 165 * Guarantee no request is in use, so we can change any data structure of 166 * the queue afterward. 167 */ 168 void blk_freeze_queue(struct request_queue *q) 169 { 170 /* 171 * In the !blk_mq case we are only calling this to kill the 172 * q_usage_counter, otherwise this increases the freeze depth 173 * and waits for it to return to zero. For this reason there is 174 * no blk_unfreeze_queue(), and blk_freeze_queue() is not 175 * exported to drivers as the only user for unfreeze is blk_mq. 176 */ 177 blk_freeze_queue_start(q); 178 blk_mq_freeze_queue_wait(q); 179 } 180 181 void blk_mq_freeze_queue(struct request_queue *q) 182 { 183 /* 184 * ...just an alias to keep freeze and unfreeze actions balanced 185 * in the blk_mq_* namespace 186 */ 187 blk_freeze_queue(q); 188 } 189 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue); 190 191 void blk_mq_unfreeze_queue(struct request_queue *q) 192 { 193 mutex_lock(&q->mq_freeze_lock); 194 q->mq_freeze_depth--; 195 WARN_ON_ONCE(q->mq_freeze_depth < 0); 196 if (!q->mq_freeze_depth) { 197 percpu_ref_resurrect(&q->q_usage_counter); 198 wake_up_all(&q->mq_freeze_wq); 199 } 200 mutex_unlock(&q->mq_freeze_lock); 201 } 202 EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue); 203 204 /* 205 * FIXME: replace the scsi_internal_device_*block_nowait() calls in the 206 * mpt3sas driver such that this function can be removed. 207 */ 208 void blk_mq_quiesce_queue_nowait(struct request_queue *q) 209 { 210 blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q); 211 } 212 EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait); 213 214 /** 215 * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished 216 * @q: request queue. 217 * 218 * Note: this function does not prevent that the struct request end_io() 219 * callback function is invoked. Once this function is returned, we make 220 * sure no dispatch can happen until the queue is unquiesced via 221 * blk_mq_unquiesce_queue(). 222 */ 223 void blk_mq_quiesce_queue(struct request_queue *q) 224 { 225 struct blk_mq_hw_ctx *hctx; 226 unsigned int i; 227 bool rcu = false; 228 229 blk_mq_quiesce_queue_nowait(q); 230 231 queue_for_each_hw_ctx(q, hctx, i) { 232 if (hctx->flags & BLK_MQ_F_BLOCKING) 233 synchronize_srcu(hctx->srcu); 234 else 235 rcu = true; 236 } 237 if (rcu) 238 synchronize_rcu(); 239 } 240 EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue); 241 242 /* 243 * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue() 244 * @q: request queue. 245 * 246 * This function recovers queue into the state before quiescing 247 * which is done by blk_mq_quiesce_queue. 248 */ 249 void blk_mq_unquiesce_queue(struct request_queue *q) 250 { 251 blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q); 252 253 /* dispatch requests which are inserted during quiescing */ 254 blk_mq_run_hw_queues(q, true); 255 } 256 EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue); 257 258 void blk_mq_wake_waiters(struct request_queue *q) 259 { 260 struct blk_mq_hw_ctx *hctx; 261 unsigned int i; 262 263 queue_for_each_hw_ctx(q, hctx, i) 264 if (blk_mq_hw_queue_mapped(hctx)) 265 blk_mq_tag_wakeup_all(hctx->tags, true); 266 } 267 268 /* 269 * Only need start/end time stamping if we have iostat or 270 * blk stats enabled, or using an IO scheduler. 271 */ 272 static inline bool blk_mq_need_time_stamp(struct request *rq) 273 { 274 return (rq->rq_flags & (RQF_IO_STAT | RQF_STATS)) || rq->q->elevator; 275 } 276 277 static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data, 278 unsigned int tag, u64 alloc_time_ns) 279 { 280 struct blk_mq_tags *tags = blk_mq_tags_from_data(data); 281 struct request *rq = tags->static_rqs[tag]; 282 283 if (data->q->elevator) { 284 rq->tag = BLK_MQ_NO_TAG; 285 rq->internal_tag = tag; 286 } else { 287 rq->tag = tag; 288 rq->internal_tag = BLK_MQ_NO_TAG; 289 } 290 291 /* csd/requeue_work/fifo_time is initialized before use */ 292 rq->q = data->q; 293 rq->mq_ctx = data->ctx; 294 rq->mq_hctx = data->hctx; 295 rq->rq_flags = 0; 296 rq->cmd_flags = data->cmd_flags; 297 if (data->flags & BLK_MQ_REQ_PM) 298 rq->rq_flags |= RQF_PM; 299 if (blk_queue_io_stat(data->q)) 300 rq->rq_flags |= RQF_IO_STAT; 301 INIT_LIST_HEAD(&rq->queuelist); 302 INIT_HLIST_NODE(&rq->hash); 303 RB_CLEAR_NODE(&rq->rb_node); 304 rq->rq_disk = NULL; 305 rq->part = NULL; 306 #ifdef CONFIG_BLK_RQ_ALLOC_TIME 307 rq->alloc_time_ns = alloc_time_ns; 308 #endif 309 if (blk_mq_need_time_stamp(rq)) 310 rq->start_time_ns = ktime_get_ns(); 311 else 312 rq->start_time_ns = 0; 313 rq->io_start_time_ns = 0; 314 rq->stats_sectors = 0; 315 rq->nr_phys_segments = 0; 316 #if defined(CONFIG_BLK_DEV_INTEGRITY) 317 rq->nr_integrity_segments = 0; 318 #endif 319 blk_crypto_rq_set_defaults(rq); 320 /* tag was already set */ 321 WRITE_ONCE(rq->deadline, 0); 322 323 rq->timeout = 0; 324 325 rq->end_io = NULL; 326 rq->end_io_data = NULL; 327 328 data->ctx->rq_dispatched[op_is_sync(data->cmd_flags)]++; 329 refcount_set(&rq->ref, 1); 330 331 if (!op_is_flush(data->cmd_flags)) { 332 struct elevator_queue *e = data->q->elevator; 333 334 rq->elv.icq = NULL; 335 if (e && e->type->ops.prepare_request) { 336 if (e->type->icq_cache) 337 blk_mq_sched_assign_ioc(rq); 338 339 e->type->ops.prepare_request(rq); 340 rq->rq_flags |= RQF_ELVPRIV; 341 } 342 } 343 344 data->hctx->queued++; 345 return rq; 346 } 347 348 static struct request *__blk_mq_alloc_request(struct blk_mq_alloc_data *data) 349 { 350 struct request_queue *q = data->q; 351 struct elevator_queue *e = q->elevator; 352 u64 alloc_time_ns = 0; 353 unsigned int tag; 354 355 /* alloc_time includes depth and tag waits */ 356 if (blk_queue_rq_alloc_time(q)) 357 alloc_time_ns = ktime_get_ns(); 358 359 if (data->cmd_flags & REQ_NOWAIT) 360 data->flags |= BLK_MQ_REQ_NOWAIT; 361 362 if (e) { 363 /* 364 * Flush requests are special and go directly to the 365 * dispatch list. Don't include reserved tags in the 366 * limiting, as it isn't useful. 367 */ 368 if (!op_is_flush(data->cmd_flags) && 369 e->type->ops.limit_depth && 370 !(data->flags & BLK_MQ_REQ_RESERVED)) 371 e->type->ops.limit_depth(data->cmd_flags, data); 372 } 373 374 retry: 375 data->ctx = blk_mq_get_ctx(q); 376 data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx); 377 if (!e) 378 blk_mq_tag_busy(data->hctx); 379 380 /* 381 * Waiting allocations only fail because of an inactive hctx. In that 382 * case just retry the hctx assignment and tag allocation as CPU hotplug 383 * should have migrated us to an online CPU by now. 384 */ 385 tag = blk_mq_get_tag(data); 386 if (tag == BLK_MQ_NO_TAG) { 387 if (data->flags & BLK_MQ_REQ_NOWAIT) 388 return NULL; 389 390 /* 391 * Give up the CPU and sleep for a random short time to ensure 392 * that thread using a realtime scheduling class are migrated 393 * off the CPU, and thus off the hctx that is going away. 394 */ 395 msleep(3); 396 goto retry; 397 } 398 return blk_mq_rq_ctx_init(data, tag, alloc_time_ns); 399 } 400 401 struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op, 402 blk_mq_req_flags_t flags) 403 { 404 struct blk_mq_alloc_data data = { 405 .q = q, 406 .flags = flags, 407 .cmd_flags = op, 408 }; 409 struct request *rq; 410 int ret; 411 412 ret = blk_queue_enter(q, flags); 413 if (ret) 414 return ERR_PTR(ret); 415 416 rq = __blk_mq_alloc_request(&data); 417 if (!rq) 418 goto out_queue_exit; 419 rq->__data_len = 0; 420 rq->__sector = (sector_t) -1; 421 rq->bio = rq->biotail = NULL; 422 return rq; 423 out_queue_exit: 424 blk_queue_exit(q); 425 return ERR_PTR(-EWOULDBLOCK); 426 } 427 EXPORT_SYMBOL(blk_mq_alloc_request); 428 429 struct request *blk_mq_alloc_request_hctx(struct request_queue *q, 430 unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx) 431 { 432 struct blk_mq_alloc_data data = { 433 .q = q, 434 .flags = flags, 435 .cmd_flags = op, 436 }; 437 u64 alloc_time_ns = 0; 438 unsigned int cpu; 439 unsigned int tag; 440 int ret; 441 442 /* alloc_time includes depth and tag waits */ 443 if (blk_queue_rq_alloc_time(q)) 444 alloc_time_ns = ktime_get_ns(); 445 446 /* 447 * If the tag allocator sleeps we could get an allocation for a 448 * different hardware context. No need to complicate the low level 449 * allocator for this for the rare use case of a command tied to 450 * a specific queue. 451 */ 452 if (WARN_ON_ONCE(!(flags & (BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED)))) 453 return ERR_PTR(-EINVAL); 454 455 if (hctx_idx >= q->nr_hw_queues) 456 return ERR_PTR(-EIO); 457 458 ret = blk_queue_enter(q, flags); 459 if (ret) 460 return ERR_PTR(ret); 461 462 /* 463 * Check if the hardware context is actually mapped to anything. 464 * If not tell the caller that it should skip this queue. 465 */ 466 ret = -EXDEV; 467 data.hctx = q->queue_hw_ctx[hctx_idx]; 468 if (!blk_mq_hw_queue_mapped(data.hctx)) 469 goto out_queue_exit; 470 cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask); 471 data.ctx = __blk_mq_get_ctx(q, cpu); 472 473 if (!q->elevator) 474 blk_mq_tag_busy(data.hctx); 475 476 ret = -EWOULDBLOCK; 477 tag = blk_mq_get_tag(&data); 478 if (tag == BLK_MQ_NO_TAG) 479 goto out_queue_exit; 480 return blk_mq_rq_ctx_init(&data, tag, alloc_time_ns); 481 482 out_queue_exit: 483 blk_queue_exit(q); 484 return ERR_PTR(ret); 485 } 486 EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx); 487 488 static void __blk_mq_free_request(struct request *rq) 489 { 490 struct request_queue *q = rq->q; 491 struct blk_mq_ctx *ctx = rq->mq_ctx; 492 struct blk_mq_hw_ctx *hctx = rq->mq_hctx; 493 const int sched_tag = rq->internal_tag; 494 495 blk_crypto_free_request(rq); 496 blk_pm_mark_last_busy(rq); 497 rq->mq_hctx = NULL; 498 if (rq->tag != BLK_MQ_NO_TAG) 499 blk_mq_put_tag(hctx->tags, ctx, rq->tag); 500 if (sched_tag != BLK_MQ_NO_TAG) 501 blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag); 502 blk_mq_sched_restart(hctx); 503 blk_queue_exit(q); 504 } 505 506 void blk_mq_free_request(struct request *rq) 507 { 508 struct request_queue *q = rq->q; 509 struct elevator_queue *e = q->elevator; 510 struct blk_mq_ctx *ctx = rq->mq_ctx; 511 struct blk_mq_hw_ctx *hctx = rq->mq_hctx; 512 513 if (rq->rq_flags & RQF_ELVPRIV) { 514 if (e && e->type->ops.finish_request) 515 e->type->ops.finish_request(rq); 516 if (rq->elv.icq) { 517 put_io_context(rq->elv.icq->ioc); 518 rq->elv.icq = NULL; 519 } 520 } 521 522 ctx->rq_completed[rq_is_sync(rq)]++; 523 if (rq->rq_flags & RQF_MQ_INFLIGHT) 524 __blk_mq_dec_active_requests(hctx); 525 526 if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq))) 527 laptop_io_completion(q->backing_dev_info); 528 529 rq_qos_done(q, rq); 530 531 WRITE_ONCE(rq->state, MQ_RQ_IDLE); 532 if (refcount_dec_and_test(&rq->ref)) 533 __blk_mq_free_request(rq); 534 } 535 EXPORT_SYMBOL_GPL(blk_mq_free_request); 536 537 inline void __blk_mq_end_request(struct request *rq, blk_status_t error) 538 { 539 u64 now = 0; 540 541 if (blk_mq_need_time_stamp(rq)) 542 now = ktime_get_ns(); 543 544 if (rq->rq_flags & RQF_STATS) { 545 blk_mq_poll_stats_start(rq->q); 546 blk_stat_add(rq, now); 547 } 548 549 blk_mq_sched_completed_request(rq, now); 550 551 blk_account_io_done(rq, now); 552 553 if (rq->end_io) { 554 rq_qos_done(rq->q, rq); 555 rq->end_io(rq, error); 556 } else { 557 blk_mq_free_request(rq); 558 } 559 } 560 EXPORT_SYMBOL(__blk_mq_end_request); 561 562 void blk_mq_end_request(struct request *rq, blk_status_t error) 563 { 564 if (blk_update_request(rq, error, blk_rq_bytes(rq))) 565 BUG(); 566 __blk_mq_end_request(rq, error); 567 } 568 EXPORT_SYMBOL(blk_mq_end_request); 569 570 static void blk_complete_reqs(struct llist_head *list) 571 { 572 struct llist_node *entry = llist_reverse_order(llist_del_all(list)); 573 struct request *rq, *next; 574 575 llist_for_each_entry_safe(rq, next, entry, ipi_list) 576 rq->q->mq_ops->complete(rq); 577 } 578 579 static __latent_entropy void blk_done_softirq(struct softirq_action *h) 580 { 581 blk_complete_reqs(this_cpu_ptr(&blk_cpu_done)); 582 } 583 584 static int blk_softirq_cpu_dead(unsigned int cpu) 585 { 586 blk_complete_reqs(&per_cpu(blk_cpu_done, cpu)); 587 return 0; 588 } 589 590 static void __blk_mq_complete_request_remote(void *data) 591 { 592 __raise_softirq_irqoff(BLOCK_SOFTIRQ); 593 } 594 595 static inline bool blk_mq_complete_need_ipi(struct request *rq) 596 { 597 int cpu = raw_smp_processor_id(); 598 599 if (!IS_ENABLED(CONFIG_SMP) || 600 !test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) 601 return false; 602 /* 603 * With force threaded interrupts enabled, raising softirq from an SMP 604 * function call will always result in waking the ksoftirqd thread. 605 * This is probably worse than completing the request on a different 606 * cache domain. 607 */ 608 if (force_irqthreads) 609 return false; 610 611 /* same CPU or cache domain? Complete locally */ 612 if (cpu == rq->mq_ctx->cpu || 613 (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags) && 614 cpus_share_cache(cpu, rq->mq_ctx->cpu))) 615 return false; 616 617 /* don't try to IPI to an offline CPU */ 618 return cpu_online(rq->mq_ctx->cpu); 619 } 620 621 static void blk_mq_complete_send_ipi(struct request *rq) 622 { 623 struct llist_head *list; 624 unsigned int cpu; 625 626 cpu = rq->mq_ctx->cpu; 627 list = &per_cpu(blk_cpu_done, cpu); 628 if (llist_add(&rq->ipi_list, list)) { 629 INIT_CSD(&rq->csd, __blk_mq_complete_request_remote, rq); 630 smp_call_function_single_async(cpu, &rq->csd); 631 } 632 } 633 634 static void blk_mq_raise_softirq(struct request *rq) 635 { 636 struct llist_head *list; 637 638 preempt_disable(); 639 list = this_cpu_ptr(&blk_cpu_done); 640 if (llist_add(&rq->ipi_list, list)) 641 raise_softirq(BLOCK_SOFTIRQ); 642 preempt_enable(); 643 } 644 645 bool blk_mq_complete_request_remote(struct request *rq) 646 { 647 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE); 648 649 /* 650 * For a polled request, always complete locallly, it's pointless 651 * to redirect the completion. 652 */ 653 if (rq->cmd_flags & REQ_HIPRI) 654 return false; 655 656 if (blk_mq_complete_need_ipi(rq)) { 657 blk_mq_complete_send_ipi(rq); 658 return true; 659 } 660 661 if (rq->q->nr_hw_queues == 1) { 662 blk_mq_raise_softirq(rq); 663 return true; 664 } 665 return false; 666 } 667 EXPORT_SYMBOL_GPL(blk_mq_complete_request_remote); 668 669 /** 670 * blk_mq_complete_request - end I/O on a request 671 * @rq: the request being processed 672 * 673 * Description: 674 * Complete a request by scheduling the ->complete_rq operation. 675 **/ 676 void blk_mq_complete_request(struct request *rq) 677 { 678 if (!blk_mq_complete_request_remote(rq)) 679 rq->q->mq_ops->complete(rq); 680 } 681 EXPORT_SYMBOL(blk_mq_complete_request); 682 683 static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx) 684 __releases(hctx->srcu) 685 { 686 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) 687 rcu_read_unlock(); 688 else 689 srcu_read_unlock(hctx->srcu, srcu_idx); 690 } 691 692 static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx) 693 __acquires(hctx->srcu) 694 { 695 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) { 696 /* shut up gcc false positive */ 697 *srcu_idx = 0; 698 rcu_read_lock(); 699 } else 700 *srcu_idx = srcu_read_lock(hctx->srcu); 701 } 702 703 /** 704 * blk_mq_start_request - Start processing a request 705 * @rq: Pointer to request to be started 706 * 707 * Function used by device drivers to notify the block layer that a request 708 * is going to be processed now, so blk layer can do proper initializations 709 * such as starting the timeout timer. 710 */ 711 void blk_mq_start_request(struct request *rq) 712 { 713 struct request_queue *q = rq->q; 714 715 trace_block_rq_issue(rq); 716 717 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) { 718 rq->io_start_time_ns = ktime_get_ns(); 719 rq->stats_sectors = blk_rq_sectors(rq); 720 rq->rq_flags |= RQF_STATS; 721 rq_qos_issue(q, rq); 722 } 723 724 WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE); 725 726 blk_add_timer(rq); 727 WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT); 728 729 #ifdef CONFIG_BLK_DEV_INTEGRITY 730 if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE) 731 q->integrity.profile->prepare_fn(rq); 732 #endif 733 } 734 EXPORT_SYMBOL(blk_mq_start_request); 735 736 static void __blk_mq_requeue_request(struct request *rq) 737 { 738 struct request_queue *q = rq->q; 739 740 blk_mq_put_driver_tag(rq); 741 742 trace_block_rq_requeue(rq); 743 rq_qos_requeue(q, rq); 744 745 if (blk_mq_request_started(rq)) { 746 WRITE_ONCE(rq->state, MQ_RQ_IDLE); 747 rq->rq_flags &= ~RQF_TIMED_OUT; 748 } 749 } 750 751 void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list) 752 { 753 __blk_mq_requeue_request(rq); 754 755 /* this request will be re-inserted to io scheduler queue */ 756 blk_mq_sched_requeue_request(rq); 757 758 BUG_ON(!list_empty(&rq->queuelist)); 759 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list); 760 } 761 EXPORT_SYMBOL(blk_mq_requeue_request); 762 763 static void blk_mq_requeue_work(struct work_struct *work) 764 { 765 struct request_queue *q = 766 container_of(work, struct request_queue, requeue_work.work); 767 LIST_HEAD(rq_list); 768 struct request *rq, *next; 769 770 spin_lock_irq(&q->requeue_lock); 771 list_splice_init(&q->requeue_list, &rq_list); 772 spin_unlock_irq(&q->requeue_lock); 773 774 list_for_each_entry_safe(rq, next, &rq_list, queuelist) { 775 if (!(rq->rq_flags & (RQF_SOFTBARRIER | RQF_DONTPREP))) 776 continue; 777 778 rq->rq_flags &= ~RQF_SOFTBARRIER; 779 list_del_init(&rq->queuelist); 780 /* 781 * If RQF_DONTPREP, rq has contained some driver specific 782 * data, so insert it to hctx dispatch list to avoid any 783 * merge. 784 */ 785 if (rq->rq_flags & RQF_DONTPREP) 786 blk_mq_request_bypass_insert(rq, false, false); 787 else 788 blk_mq_sched_insert_request(rq, true, false, false); 789 } 790 791 while (!list_empty(&rq_list)) { 792 rq = list_entry(rq_list.next, struct request, queuelist); 793 list_del_init(&rq->queuelist); 794 blk_mq_sched_insert_request(rq, false, false, false); 795 } 796 797 blk_mq_run_hw_queues(q, false); 798 } 799 800 void blk_mq_add_to_requeue_list(struct request *rq, bool at_head, 801 bool kick_requeue_list) 802 { 803 struct request_queue *q = rq->q; 804 unsigned long flags; 805 806 /* 807 * We abuse this flag that is otherwise used by the I/O scheduler to 808 * request head insertion from the workqueue. 809 */ 810 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER); 811 812 spin_lock_irqsave(&q->requeue_lock, flags); 813 if (at_head) { 814 rq->rq_flags |= RQF_SOFTBARRIER; 815 list_add(&rq->queuelist, &q->requeue_list); 816 } else { 817 list_add_tail(&rq->queuelist, &q->requeue_list); 818 } 819 spin_unlock_irqrestore(&q->requeue_lock, flags); 820 821 if (kick_requeue_list) 822 blk_mq_kick_requeue_list(q); 823 } 824 825 void blk_mq_kick_requeue_list(struct request_queue *q) 826 { 827 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0); 828 } 829 EXPORT_SYMBOL(blk_mq_kick_requeue_list); 830 831 void blk_mq_delay_kick_requeue_list(struct request_queue *q, 832 unsigned long msecs) 833 { 834 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 835 msecs_to_jiffies(msecs)); 836 } 837 EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list); 838 839 struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag) 840 { 841 if (tag < tags->nr_tags) { 842 prefetch(tags->rqs[tag]); 843 return tags->rqs[tag]; 844 } 845 846 return NULL; 847 } 848 EXPORT_SYMBOL(blk_mq_tag_to_rq); 849 850 static bool blk_mq_rq_inflight(struct blk_mq_hw_ctx *hctx, struct request *rq, 851 void *priv, bool reserved) 852 { 853 /* 854 * If we find a request that isn't idle and the queue matches, 855 * we know the queue is busy. Return false to stop the iteration. 856 */ 857 if (blk_mq_request_started(rq) && rq->q == hctx->queue) { 858 bool *busy = priv; 859 860 *busy = true; 861 return false; 862 } 863 864 return true; 865 } 866 867 bool blk_mq_queue_inflight(struct request_queue *q) 868 { 869 bool busy = false; 870 871 blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy); 872 return busy; 873 } 874 EXPORT_SYMBOL_GPL(blk_mq_queue_inflight); 875 876 static void blk_mq_rq_timed_out(struct request *req, bool reserved) 877 { 878 req->rq_flags |= RQF_TIMED_OUT; 879 if (req->q->mq_ops->timeout) { 880 enum blk_eh_timer_return ret; 881 882 ret = req->q->mq_ops->timeout(req, reserved); 883 if (ret == BLK_EH_DONE) 884 return; 885 WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER); 886 } 887 888 blk_add_timer(req); 889 } 890 891 static bool blk_mq_req_expired(struct request *rq, unsigned long *next) 892 { 893 unsigned long deadline; 894 895 if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT) 896 return false; 897 if (rq->rq_flags & RQF_TIMED_OUT) 898 return false; 899 900 deadline = READ_ONCE(rq->deadline); 901 if (time_after_eq(jiffies, deadline)) 902 return true; 903 904 if (*next == 0) 905 *next = deadline; 906 else if (time_after(*next, deadline)) 907 *next = deadline; 908 return false; 909 } 910 911 static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx, 912 struct request *rq, void *priv, bool reserved) 913 { 914 unsigned long *next = priv; 915 916 /* 917 * Just do a quick check if it is expired before locking the request in 918 * so we're not unnecessarilly synchronizing across CPUs. 919 */ 920 if (!blk_mq_req_expired(rq, next)) 921 return true; 922 923 /* 924 * We have reason to believe the request may be expired. Take a 925 * reference on the request to lock this request lifetime into its 926 * currently allocated context to prevent it from being reallocated in 927 * the event the completion by-passes this timeout handler. 928 * 929 * If the reference was already released, then the driver beat the 930 * timeout handler to posting a natural completion. 931 */ 932 if (!refcount_inc_not_zero(&rq->ref)) 933 return true; 934 935 /* 936 * The request is now locked and cannot be reallocated underneath the 937 * timeout handler's processing. Re-verify this exact request is truly 938 * expired; if it is not expired, then the request was completed and 939 * reallocated as a new request. 940 */ 941 if (blk_mq_req_expired(rq, next)) 942 blk_mq_rq_timed_out(rq, reserved); 943 944 if (is_flush_rq(rq, hctx)) 945 rq->end_io(rq, 0); 946 else if (refcount_dec_and_test(&rq->ref)) 947 __blk_mq_free_request(rq); 948 949 return true; 950 } 951 952 static void blk_mq_timeout_work(struct work_struct *work) 953 { 954 struct request_queue *q = 955 container_of(work, struct request_queue, timeout_work); 956 unsigned long next = 0; 957 struct blk_mq_hw_ctx *hctx; 958 int i; 959 960 /* A deadlock might occur if a request is stuck requiring a 961 * timeout at the same time a queue freeze is waiting 962 * completion, since the timeout code would not be able to 963 * acquire the queue reference here. 964 * 965 * That's why we don't use blk_queue_enter here; instead, we use 966 * percpu_ref_tryget directly, because we need to be able to 967 * obtain a reference even in the short window between the queue 968 * starting to freeze, by dropping the first reference in 969 * blk_freeze_queue_start, and the moment the last request is 970 * consumed, marked by the instant q_usage_counter reaches 971 * zero. 972 */ 973 if (!percpu_ref_tryget(&q->q_usage_counter)) 974 return; 975 976 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next); 977 978 if (next != 0) { 979 mod_timer(&q->timeout, next); 980 } else { 981 /* 982 * Request timeouts are handled as a forward rolling timer. If 983 * we end up here it means that no requests are pending and 984 * also that no request has been pending for a while. Mark 985 * each hctx as idle. 986 */ 987 queue_for_each_hw_ctx(q, hctx, i) { 988 /* the hctx may be unmapped, so check it here */ 989 if (blk_mq_hw_queue_mapped(hctx)) 990 blk_mq_tag_idle(hctx); 991 } 992 } 993 blk_queue_exit(q); 994 } 995 996 struct flush_busy_ctx_data { 997 struct blk_mq_hw_ctx *hctx; 998 struct list_head *list; 999 }; 1000 1001 static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data) 1002 { 1003 struct flush_busy_ctx_data *flush_data = data; 1004 struct blk_mq_hw_ctx *hctx = flush_data->hctx; 1005 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr]; 1006 enum hctx_type type = hctx->type; 1007 1008 spin_lock(&ctx->lock); 1009 list_splice_tail_init(&ctx->rq_lists[type], flush_data->list); 1010 sbitmap_clear_bit(sb, bitnr); 1011 spin_unlock(&ctx->lock); 1012 return true; 1013 } 1014 1015 /* 1016 * Process software queues that have been marked busy, splicing them 1017 * to the for-dispatch 1018 */ 1019 void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list) 1020 { 1021 struct flush_busy_ctx_data data = { 1022 .hctx = hctx, 1023 .list = list, 1024 }; 1025 1026 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data); 1027 } 1028 EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs); 1029 1030 struct dispatch_rq_data { 1031 struct blk_mq_hw_ctx *hctx; 1032 struct request *rq; 1033 }; 1034 1035 static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr, 1036 void *data) 1037 { 1038 struct dispatch_rq_data *dispatch_data = data; 1039 struct blk_mq_hw_ctx *hctx = dispatch_data->hctx; 1040 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr]; 1041 enum hctx_type type = hctx->type; 1042 1043 spin_lock(&ctx->lock); 1044 if (!list_empty(&ctx->rq_lists[type])) { 1045 dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next); 1046 list_del_init(&dispatch_data->rq->queuelist); 1047 if (list_empty(&ctx->rq_lists[type])) 1048 sbitmap_clear_bit(sb, bitnr); 1049 } 1050 spin_unlock(&ctx->lock); 1051 1052 return !dispatch_data->rq; 1053 } 1054 1055 struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx, 1056 struct blk_mq_ctx *start) 1057 { 1058 unsigned off = start ? start->index_hw[hctx->type] : 0; 1059 struct dispatch_rq_data data = { 1060 .hctx = hctx, 1061 .rq = NULL, 1062 }; 1063 1064 __sbitmap_for_each_set(&hctx->ctx_map, off, 1065 dispatch_rq_from_ctx, &data); 1066 1067 return data.rq; 1068 } 1069 1070 static inline unsigned int queued_to_index(unsigned int queued) 1071 { 1072 if (!queued) 1073 return 0; 1074 1075 return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1); 1076 } 1077 1078 static bool __blk_mq_get_driver_tag(struct request *rq) 1079 { 1080 struct sbitmap_queue *bt = rq->mq_hctx->tags->bitmap_tags; 1081 unsigned int tag_offset = rq->mq_hctx->tags->nr_reserved_tags; 1082 int tag; 1083 1084 blk_mq_tag_busy(rq->mq_hctx); 1085 1086 if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag)) { 1087 bt = rq->mq_hctx->tags->breserved_tags; 1088 tag_offset = 0; 1089 } else { 1090 if (!hctx_may_queue(rq->mq_hctx, bt)) 1091 return false; 1092 } 1093 1094 tag = __sbitmap_queue_get(bt); 1095 if (tag == BLK_MQ_NO_TAG) 1096 return false; 1097 1098 rq->tag = tag + tag_offset; 1099 return true; 1100 } 1101 1102 static bool blk_mq_get_driver_tag(struct request *rq) 1103 { 1104 struct blk_mq_hw_ctx *hctx = rq->mq_hctx; 1105 1106 if (rq->tag == BLK_MQ_NO_TAG && !__blk_mq_get_driver_tag(rq)) 1107 return false; 1108 1109 if ((hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) && 1110 !(rq->rq_flags & RQF_MQ_INFLIGHT)) { 1111 rq->rq_flags |= RQF_MQ_INFLIGHT; 1112 __blk_mq_inc_active_requests(hctx); 1113 } 1114 hctx->tags->rqs[rq->tag] = rq; 1115 return true; 1116 } 1117 1118 static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode, 1119 int flags, void *key) 1120 { 1121 struct blk_mq_hw_ctx *hctx; 1122 1123 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait); 1124 1125 spin_lock(&hctx->dispatch_wait_lock); 1126 if (!list_empty(&wait->entry)) { 1127 struct sbitmap_queue *sbq; 1128 1129 list_del_init(&wait->entry); 1130 sbq = hctx->tags->bitmap_tags; 1131 atomic_dec(&sbq->ws_active); 1132 } 1133 spin_unlock(&hctx->dispatch_wait_lock); 1134 1135 blk_mq_run_hw_queue(hctx, true); 1136 return 1; 1137 } 1138 1139 /* 1140 * Mark us waiting for a tag. For shared tags, this involves hooking us into 1141 * the tag wakeups. For non-shared tags, we can simply mark us needing a 1142 * restart. For both cases, take care to check the condition again after 1143 * marking us as waiting. 1144 */ 1145 static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx, 1146 struct request *rq) 1147 { 1148 struct sbitmap_queue *sbq = hctx->tags->bitmap_tags; 1149 struct wait_queue_head *wq; 1150 wait_queue_entry_t *wait; 1151 bool ret; 1152 1153 if (!(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) { 1154 blk_mq_sched_mark_restart_hctx(hctx); 1155 1156 /* 1157 * It's possible that a tag was freed in the window between the 1158 * allocation failure and adding the hardware queue to the wait 1159 * queue. 1160 * 1161 * Don't clear RESTART here, someone else could have set it. 1162 * At most this will cost an extra queue run. 1163 */ 1164 return blk_mq_get_driver_tag(rq); 1165 } 1166 1167 wait = &hctx->dispatch_wait; 1168 if (!list_empty_careful(&wait->entry)) 1169 return false; 1170 1171 wq = &bt_wait_ptr(sbq, hctx)->wait; 1172 1173 spin_lock_irq(&wq->lock); 1174 spin_lock(&hctx->dispatch_wait_lock); 1175 if (!list_empty(&wait->entry)) { 1176 spin_unlock(&hctx->dispatch_wait_lock); 1177 spin_unlock_irq(&wq->lock); 1178 return false; 1179 } 1180 1181 atomic_inc(&sbq->ws_active); 1182 wait->flags &= ~WQ_FLAG_EXCLUSIVE; 1183 __add_wait_queue(wq, wait); 1184 1185 /* 1186 * It's possible that a tag was freed in the window between the 1187 * allocation failure and adding the hardware queue to the wait 1188 * queue. 1189 */ 1190 ret = blk_mq_get_driver_tag(rq); 1191 if (!ret) { 1192 spin_unlock(&hctx->dispatch_wait_lock); 1193 spin_unlock_irq(&wq->lock); 1194 return false; 1195 } 1196 1197 /* 1198 * We got a tag, remove ourselves from the wait queue to ensure 1199 * someone else gets the wakeup. 1200 */ 1201 list_del_init(&wait->entry); 1202 atomic_dec(&sbq->ws_active); 1203 spin_unlock(&hctx->dispatch_wait_lock); 1204 spin_unlock_irq(&wq->lock); 1205 1206 return true; 1207 } 1208 1209 #define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8 1210 #define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4 1211 /* 1212 * Update dispatch busy with the Exponential Weighted Moving Average(EWMA): 1213 * - EWMA is one simple way to compute running average value 1214 * - weight(7/8 and 1/8) is applied so that it can decrease exponentially 1215 * - take 4 as factor for avoiding to get too small(0) result, and this 1216 * factor doesn't matter because EWMA decreases exponentially 1217 */ 1218 static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy) 1219 { 1220 unsigned int ewma; 1221 1222 if (hctx->queue->elevator) 1223 return; 1224 1225 ewma = hctx->dispatch_busy; 1226 1227 if (!ewma && !busy) 1228 return; 1229 1230 ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1; 1231 if (busy) 1232 ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR; 1233 ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT; 1234 1235 hctx->dispatch_busy = ewma; 1236 } 1237 1238 #define BLK_MQ_RESOURCE_DELAY 3 /* ms units */ 1239 1240 static void blk_mq_handle_dev_resource(struct request *rq, 1241 struct list_head *list) 1242 { 1243 struct request *next = 1244 list_first_entry_or_null(list, struct request, queuelist); 1245 1246 /* 1247 * If an I/O scheduler has been configured and we got a driver tag for 1248 * the next request already, free it. 1249 */ 1250 if (next) 1251 blk_mq_put_driver_tag(next); 1252 1253 list_add(&rq->queuelist, list); 1254 __blk_mq_requeue_request(rq); 1255 } 1256 1257 static void blk_mq_handle_zone_resource(struct request *rq, 1258 struct list_head *zone_list) 1259 { 1260 /* 1261 * If we end up here it is because we cannot dispatch a request to a 1262 * specific zone due to LLD level zone-write locking or other zone 1263 * related resource not being available. In this case, set the request 1264 * aside in zone_list for retrying it later. 1265 */ 1266 list_add(&rq->queuelist, zone_list); 1267 __blk_mq_requeue_request(rq); 1268 } 1269 1270 enum prep_dispatch { 1271 PREP_DISPATCH_OK, 1272 PREP_DISPATCH_NO_TAG, 1273 PREP_DISPATCH_NO_BUDGET, 1274 }; 1275 1276 static enum prep_dispatch blk_mq_prep_dispatch_rq(struct request *rq, 1277 bool need_budget) 1278 { 1279 struct blk_mq_hw_ctx *hctx = rq->mq_hctx; 1280 1281 if (need_budget && !blk_mq_get_dispatch_budget(rq->q)) { 1282 blk_mq_put_driver_tag(rq); 1283 return PREP_DISPATCH_NO_BUDGET; 1284 } 1285 1286 if (!blk_mq_get_driver_tag(rq)) { 1287 /* 1288 * The initial allocation attempt failed, so we need to 1289 * rerun the hardware queue when a tag is freed. The 1290 * waitqueue takes care of that. If the queue is run 1291 * before we add this entry back on the dispatch list, 1292 * we'll re-run it below. 1293 */ 1294 if (!blk_mq_mark_tag_wait(hctx, rq)) { 1295 /* 1296 * All budgets not got from this function will be put 1297 * together during handling partial dispatch 1298 */ 1299 if (need_budget) 1300 blk_mq_put_dispatch_budget(rq->q); 1301 return PREP_DISPATCH_NO_TAG; 1302 } 1303 } 1304 1305 return PREP_DISPATCH_OK; 1306 } 1307 1308 /* release all allocated budgets before calling to blk_mq_dispatch_rq_list */ 1309 static void blk_mq_release_budgets(struct request_queue *q, 1310 unsigned int nr_budgets) 1311 { 1312 int i; 1313 1314 for (i = 0; i < nr_budgets; i++) 1315 blk_mq_put_dispatch_budget(q); 1316 } 1317 1318 /* 1319 * Returns true if we did some work AND can potentially do more. 1320 */ 1321 bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list, 1322 unsigned int nr_budgets) 1323 { 1324 enum prep_dispatch prep; 1325 struct request_queue *q = hctx->queue; 1326 struct request *rq, *nxt; 1327 int errors, queued; 1328 blk_status_t ret = BLK_STS_OK; 1329 LIST_HEAD(zone_list); 1330 1331 if (list_empty(list)) 1332 return false; 1333 1334 /* 1335 * Now process all the entries, sending them to the driver. 1336 */ 1337 errors = queued = 0; 1338 do { 1339 struct blk_mq_queue_data bd; 1340 1341 rq = list_first_entry(list, struct request, queuelist); 1342 1343 WARN_ON_ONCE(hctx != rq->mq_hctx); 1344 prep = blk_mq_prep_dispatch_rq(rq, !nr_budgets); 1345 if (prep != PREP_DISPATCH_OK) 1346 break; 1347 1348 list_del_init(&rq->queuelist); 1349 1350 bd.rq = rq; 1351 1352 /* 1353 * Flag last if we have no more requests, or if we have more 1354 * but can't assign a driver tag to it. 1355 */ 1356 if (list_empty(list)) 1357 bd.last = true; 1358 else { 1359 nxt = list_first_entry(list, struct request, queuelist); 1360 bd.last = !blk_mq_get_driver_tag(nxt); 1361 } 1362 1363 /* 1364 * once the request is queued to lld, no need to cover the 1365 * budget any more 1366 */ 1367 if (nr_budgets) 1368 nr_budgets--; 1369 ret = q->mq_ops->queue_rq(hctx, &bd); 1370 switch (ret) { 1371 case BLK_STS_OK: 1372 queued++; 1373 break; 1374 case BLK_STS_RESOURCE: 1375 case BLK_STS_DEV_RESOURCE: 1376 blk_mq_handle_dev_resource(rq, list); 1377 goto out; 1378 case BLK_STS_ZONE_RESOURCE: 1379 /* 1380 * Move the request to zone_list and keep going through 1381 * the dispatch list to find more requests the drive can 1382 * accept. 1383 */ 1384 blk_mq_handle_zone_resource(rq, &zone_list); 1385 break; 1386 default: 1387 errors++; 1388 blk_mq_end_request(rq, ret); 1389 } 1390 } while (!list_empty(list)); 1391 out: 1392 if (!list_empty(&zone_list)) 1393 list_splice_tail_init(&zone_list, list); 1394 1395 hctx->dispatched[queued_to_index(queued)]++; 1396 1397 /* If we didn't flush the entire list, we could have told the driver 1398 * there was more coming, but that turned out to be a lie. 1399 */ 1400 if ((!list_empty(list) || errors) && q->mq_ops->commit_rqs && queued) 1401 q->mq_ops->commit_rqs(hctx); 1402 /* 1403 * Any items that need requeuing? Stuff them into hctx->dispatch, 1404 * that is where we will continue on next queue run. 1405 */ 1406 if (!list_empty(list)) { 1407 bool needs_restart; 1408 /* For non-shared tags, the RESTART check will suffice */ 1409 bool no_tag = prep == PREP_DISPATCH_NO_TAG && 1410 (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED); 1411 bool no_budget_avail = prep == PREP_DISPATCH_NO_BUDGET; 1412 1413 blk_mq_release_budgets(q, nr_budgets); 1414 1415 spin_lock(&hctx->lock); 1416 list_splice_tail_init(list, &hctx->dispatch); 1417 spin_unlock(&hctx->lock); 1418 1419 /* 1420 * Order adding requests to hctx->dispatch and checking 1421 * SCHED_RESTART flag. The pair of this smp_mb() is the one 1422 * in blk_mq_sched_restart(). Avoid restart code path to 1423 * miss the new added requests to hctx->dispatch, meantime 1424 * SCHED_RESTART is observed here. 1425 */ 1426 smp_mb(); 1427 1428 /* 1429 * If SCHED_RESTART was set by the caller of this function and 1430 * it is no longer set that means that it was cleared by another 1431 * thread and hence that a queue rerun is needed. 1432 * 1433 * If 'no_tag' is set, that means that we failed getting 1434 * a driver tag with an I/O scheduler attached. If our dispatch 1435 * waitqueue is no longer active, ensure that we run the queue 1436 * AFTER adding our entries back to the list. 1437 * 1438 * If no I/O scheduler has been configured it is possible that 1439 * the hardware queue got stopped and restarted before requests 1440 * were pushed back onto the dispatch list. Rerun the queue to 1441 * avoid starvation. Notes: 1442 * - blk_mq_run_hw_queue() checks whether or not a queue has 1443 * been stopped before rerunning a queue. 1444 * - Some but not all block drivers stop a queue before 1445 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq 1446 * and dm-rq. 1447 * 1448 * If driver returns BLK_STS_RESOURCE and SCHED_RESTART 1449 * bit is set, run queue after a delay to avoid IO stalls 1450 * that could otherwise occur if the queue is idle. We'll do 1451 * similar if we couldn't get budget and SCHED_RESTART is set. 1452 */ 1453 needs_restart = blk_mq_sched_needs_restart(hctx); 1454 if (!needs_restart || 1455 (no_tag && list_empty_careful(&hctx->dispatch_wait.entry))) 1456 blk_mq_run_hw_queue(hctx, true); 1457 else if (needs_restart && (ret == BLK_STS_RESOURCE || 1458 no_budget_avail)) 1459 blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY); 1460 1461 blk_mq_update_dispatch_busy(hctx, true); 1462 return false; 1463 } else 1464 blk_mq_update_dispatch_busy(hctx, false); 1465 1466 return (queued + errors) != 0; 1467 } 1468 1469 /** 1470 * __blk_mq_run_hw_queue - Run a hardware queue. 1471 * @hctx: Pointer to the hardware queue to run. 1472 * 1473 * Send pending requests to the hardware. 1474 */ 1475 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) 1476 { 1477 int srcu_idx; 1478 1479 /* 1480 * We can't run the queue inline with ints disabled. Ensure that 1481 * we catch bad users of this early. 1482 */ 1483 WARN_ON_ONCE(in_interrupt()); 1484 1485 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING); 1486 1487 hctx_lock(hctx, &srcu_idx); 1488 blk_mq_sched_dispatch_requests(hctx); 1489 hctx_unlock(hctx, srcu_idx); 1490 } 1491 1492 static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx) 1493 { 1494 int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask); 1495 1496 if (cpu >= nr_cpu_ids) 1497 cpu = cpumask_first(hctx->cpumask); 1498 return cpu; 1499 } 1500 1501 /* 1502 * It'd be great if the workqueue API had a way to pass 1503 * in a mask and had some smarts for more clever placement. 1504 * For now we just round-robin here, switching for every 1505 * BLK_MQ_CPU_WORK_BATCH queued items. 1506 */ 1507 static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) 1508 { 1509 bool tried = false; 1510 int next_cpu = hctx->next_cpu; 1511 1512 if (hctx->queue->nr_hw_queues == 1) 1513 return WORK_CPU_UNBOUND; 1514 1515 if (--hctx->next_cpu_batch <= 0) { 1516 select_cpu: 1517 next_cpu = cpumask_next_and(next_cpu, hctx->cpumask, 1518 cpu_online_mask); 1519 if (next_cpu >= nr_cpu_ids) 1520 next_cpu = blk_mq_first_mapped_cpu(hctx); 1521 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; 1522 } 1523 1524 /* 1525 * Do unbound schedule if we can't find a online CPU for this hctx, 1526 * and it should only happen in the path of handling CPU DEAD. 1527 */ 1528 if (!cpu_online(next_cpu)) { 1529 if (!tried) { 1530 tried = true; 1531 goto select_cpu; 1532 } 1533 1534 /* 1535 * Make sure to re-select CPU next time once after CPUs 1536 * in hctx->cpumask become online again. 1537 */ 1538 hctx->next_cpu = next_cpu; 1539 hctx->next_cpu_batch = 1; 1540 return WORK_CPU_UNBOUND; 1541 } 1542 1543 hctx->next_cpu = next_cpu; 1544 return next_cpu; 1545 } 1546 1547 /** 1548 * __blk_mq_delay_run_hw_queue - Run (or schedule to run) a hardware queue. 1549 * @hctx: Pointer to the hardware queue to run. 1550 * @async: If we want to run the queue asynchronously. 1551 * @msecs: Milliseconds of delay to wait before running the queue. 1552 * 1553 * If !@async, try to run the queue now. Else, run the queue asynchronously and 1554 * with a delay of @msecs. 1555 */ 1556 static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async, 1557 unsigned long msecs) 1558 { 1559 if (unlikely(blk_mq_hctx_stopped(hctx))) 1560 return; 1561 1562 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) { 1563 int cpu = get_cpu(); 1564 if (cpumask_test_cpu(cpu, hctx->cpumask)) { 1565 __blk_mq_run_hw_queue(hctx); 1566 put_cpu(); 1567 return; 1568 } 1569 1570 put_cpu(); 1571 } 1572 1573 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work, 1574 msecs_to_jiffies(msecs)); 1575 } 1576 1577 /** 1578 * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously. 1579 * @hctx: Pointer to the hardware queue to run. 1580 * @msecs: Milliseconds of delay to wait before running the queue. 1581 * 1582 * Run a hardware queue asynchronously with a delay of @msecs. 1583 */ 1584 void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) 1585 { 1586 __blk_mq_delay_run_hw_queue(hctx, true, msecs); 1587 } 1588 EXPORT_SYMBOL(blk_mq_delay_run_hw_queue); 1589 1590 /** 1591 * blk_mq_run_hw_queue - Start to run a hardware queue. 1592 * @hctx: Pointer to the hardware queue to run. 1593 * @async: If we want to run the queue asynchronously. 1594 * 1595 * Check if the request queue is not in a quiesced state and if there are 1596 * pending requests to be sent. If this is true, run the queue to send requests 1597 * to hardware. 1598 */ 1599 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) 1600 { 1601 int srcu_idx; 1602 bool need_run; 1603 1604 /* 1605 * When queue is quiesced, we may be switching io scheduler, or 1606 * updating nr_hw_queues, or other things, and we can't run queue 1607 * any more, even __blk_mq_hctx_has_pending() can't be called safely. 1608 * 1609 * And queue will be rerun in blk_mq_unquiesce_queue() if it is 1610 * quiesced. 1611 */ 1612 hctx_lock(hctx, &srcu_idx); 1613 need_run = !blk_queue_quiesced(hctx->queue) && 1614 blk_mq_hctx_has_pending(hctx); 1615 hctx_unlock(hctx, srcu_idx); 1616 1617 if (need_run) 1618 __blk_mq_delay_run_hw_queue(hctx, async, 0); 1619 } 1620 EXPORT_SYMBOL(blk_mq_run_hw_queue); 1621 1622 /* 1623 * Is the request queue handled by an IO scheduler that does not respect 1624 * hardware queues when dispatching? 1625 */ 1626 static bool blk_mq_has_sqsched(struct request_queue *q) 1627 { 1628 struct elevator_queue *e = q->elevator; 1629 1630 if (e && e->type->ops.dispatch_request && 1631 !(e->type->elevator_features & ELEVATOR_F_MQ_AWARE)) 1632 return true; 1633 return false; 1634 } 1635 1636 /* 1637 * Return prefered queue to dispatch from (if any) for non-mq aware IO 1638 * scheduler. 1639 */ 1640 static struct blk_mq_hw_ctx *blk_mq_get_sq_hctx(struct request_queue *q) 1641 { 1642 struct blk_mq_hw_ctx *hctx; 1643 1644 /* 1645 * If the IO scheduler does not respect hardware queues when 1646 * dispatching, we just don't bother with multiple HW queues and 1647 * dispatch from hctx for the current CPU since running multiple queues 1648 * just causes lock contention inside the scheduler and pointless cache 1649 * bouncing. 1650 */ 1651 hctx = blk_mq_map_queue_type(q, HCTX_TYPE_DEFAULT, 1652 raw_smp_processor_id()); 1653 if (!blk_mq_hctx_stopped(hctx)) 1654 return hctx; 1655 return NULL; 1656 } 1657 1658 /** 1659 * blk_mq_run_hw_queues - Run all hardware queues in a request queue. 1660 * @q: Pointer to the request queue to run. 1661 * @async: If we want to run the queue asynchronously. 1662 */ 1663 void blk_mq_run_hw_queues(struct request_queue *q, bool async) 1664 { 1665 struct blk_mq_hw_ctx *hctx, *sq_hctx; 1666 int i; 1667 1668 sq_hctx = NULL; 1669 if (blk_mq_has_sqsched(q)) 1670 sq_hctx = blk_mq_get_sq_hctx(q); 1671 queue_for_each_hw_ctx(q, hctx, i) { 1672 if (blk_mq_hctx_stopped(hctx)) 1673 continue; 1674 /* 1675 * Dispatch from this hctx either if there's no hctx preferred 1676 * by IO scheduler or if it has requests that bypass the 1677 * scheduler. 1678 */ 1679 if (!sq_hctx || sq_hctx == hctx || 1680 !list_empty_careful(&hctx->dispatch)) 1681 blk_mq_run_hw_queue(hctx, async); 1682 } 1683 } 1684 EXPORT_SYMBOL(blk_mq_run_hw_queues); 1685 1686 /** 1687 * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously. 1688 * @q: Pointer to the request queue to run. 1689 * @msecs: Milliseconds of delay to wait before running the queues. 1690 */ 1691 void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs) 1692 { 1693 struct blk_mq_hw_ctx *hctx, *sq_hctx; 1694 int i; 1695 1696 sq_hctx = NULL; 1697 if (blk_mq_has_sqsched(q)) 1698 sq_hctx = blk_mq_get_sq_hctx(q); 1699 queue_for_each_hw_ctx(q, hctx, i) { 1700 if (blk_mq_hctx_stopped(hctx)) 1701 continue; 1702 /* 1703 * Dispatch from this hctx either if there's no hctx preferred 1704 * by IO scheduler or if it has requests that bypass the 1705 * scheduler. 1706 */ 1707 if (!sq_hctx || sq_hctx == hctx || 1708 !list_empty_careful(&hctx->dispatch)) 1709 blk_mq_delay_run_hw_queue(hctx, msecs); 1710 } 1711 } 1712 EXPORT_SYMBOL(blk_mq_delay_run_hw_queues); 1713 1714 /** 1715 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped 1716 * @q: request queue. 1717 * 1718 * The caller is responsible for serializing this function against 1719 * blk_mq_{start,stop}_hw_queue(). 1720 */ 1721 bool blk_mq_queue_stopped(struct request_queue *q) 1722 { 1723 struct blk_mq_hw_ctx *hctx; 1724 int i; 1725 1726 queue_for_each_hw_ctx(q, hctx, i) 1727 if (blk_mq_hctx_stopped(hctx)) 1728 return true; 1729 1730 return false; 1731 } 1732 EXPORT_SYMBOL(blk_mq_queue_stopped); 1733 1734 /* 1735 * This function is often used for pausing .queue_rq() by driver when 1736 * there isn't enough resource or some conditions aren't satisfied, and 1737 * BLK_STS_RESOURCE is usually returned. 1738 * 1739 * We do not guarantee that dispatch can be drained or blocked 1740 * after blk_mq_stop_hw_queue() returns. Please use 1741 * blk_mq_quiesce_queue() for that requirement. 1742 */ 1743 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) 1744 { 1745 cancel_delayed_work(&hctx->run_work); 1746 1747 set_bit(BLK_MQ_S_STOPPED, &hctx->state); 1748 } 1749 EXPORT_SYMBOL(blk_mq_stop_hw_queue); 1750 1751 /* 1752 * This function is often used for pausing .queue_rq() by driver when 1753 * there isn't enough resource or some conditions aren't satisfied, and 1754 * BLK_STS_RESOURCE is usually returned. 1755 * 1756 * We do not guarantee that dispatch can be drained or blocked 1757 * after blk_mq_stop_hw_queues() returns. Please use 1758 * blk_mq_quiesce_queue() for that requirement. 1759 */ 1760 void blk_mq_stop_hw_queues(struct request_queue *q) 1761 { 1762 struct blk_mq_hw_ctx *hctx; 1763 int i; 1764 1765 queue_for_each_hw_ctx(q, hctx, i) 1766 blk_mq_stop_hw_queue(hctx); 1767 } 1768 EXPORT_SYMBOL(blk_mq_stop_hw_queues); 1769 1770 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) 1771 { 1772 clear_bit(BLK_MQ_S_STOPPED, &hctx->state); 1773 1774 blk_mq_run_hw_queue(hctx, false); 1775 } 1776 EXPORT_SYMBOL(blk_mq_start_hw_queue); 1777 1778 void blk_mq_start_hw_queues(struct request_queue *q) 1779 { 1780 struct blk_mq_hw_ctx *hctx; 1781 int i; 1782 1783 queue_for_each_hw_ctx(q, hctx, i) 1784 blk_mq_start_hw_queue(hctx); 1785 } 1786 EXPORT_SYMBOL(blk_mq_start_hw_queues); 1787 1788 void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) 1789 { 1790 if (!blk_mq_hctx_stopped(hctx)) 1791 return; 1792 1793 clear_bit(BLK_MQ_S_STOPPED, &hctx->state); 1794 blk_mq_run_hw_queue(hctx, async); 1795 } 1796 EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue); 1797 1798 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async) 1799 { 1800 struct blk_mq_hw_ctx *hctx; 1801 int i; 1802 1803 queue_for_each_hw_ctx(q, hctx, i) 1804 blk_mq_start_stopped_hw_queue(hctx, async); 1805 } 1806 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); 1807 1808 static void blk_mq_run_work_fn(struct work_struct *work) 1809 { 1810 struct blk_mq_hw_ctx *hctx; 1811 1812 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work); 1813 1814 /* 1815 * If we are stopped, don't run the queue. 1816 */ 1817 if (blk_mq_hctx_stopped(hctx)) 1818 return; 1819 1820 __blk_mq_run_hw_queue(hctx); 1821 } 1822 1823 static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx, 1824 struct request *rq, 1825 bool at_head) 1826 { 1827 struct blk_mq_ctx *ctx = rq->mq_ctx; 1828 enum hctx_type type = hctx->type; 1829 1830 lockdep_assert_held(&ctx->lock); 1831 1832 trace_block_rq_insert(rq); 1833 1834 if (at_head) 1835 list_add(&rq->queuelist, &ctx->rq_lists[type]); 1836 else 1837 list_add_tail(&rq->queuelist, &ctx->rq_lists[type]); 1838 } 1839 1840 void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, 1841 bool at_head) 1842 { 1843 struct blk_mq_ctx *ctx = rq->mq_ctx; 1844 1845 lockdep_assert_held(&ctx->lock); 1846 1847 __blk_mq_insert_req_list(hctx, rq, at_head); 1848 blk_mq_hctx_mark_pending(hctx, ctx); 1849 } 1850 1851 /** 1852 * blk_mq_request_bypass_insert - Insert a request at dispatch list. 1853 * @rq: Pointer to request to be inserted. 1854 * @at_head: true if the request should be inserted at the head of the list. 1855 * @run_queue: If we should run the hardware queue after inserting the request. 1856 * 1857 * Should only be used carefully, when the caller knows we want to 1858 * bypass a potential IO scheduler on the target device. 1859 */ 1860 void blk_mq_request_bypass_insert(struct request *rq, bool at_head, 1861 bool run_queue) 1862 { 1863 struct blk_mq_hw_ctx *hctx = rq->mq_hctx; 1864 1865 spin_lock(&hctx->lock); 1866 if (at_head) 1867 list_add(&rq->queuelist, &hctx->dispatch); 1868 else 1869 list_add_tail(&rq->queuelist, &hctx->dispatch); 1870 spin_unlock(&hctx->lock); 1871 1872 if (run_queue) 1873 blk_mq_run_hw_queue(hctx, false); 1874 } 1875 1876 void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx, 1877 struct list_head *list) 1878 1879 { 1880 struct request *rq; 1881 enum hctx_type type = hctx->type; 1882 1883 /* 1884 * preemption doesn't flush plug list, so it's possible ctx->cpu is 1885 * offline now 1886 */ 1887 list_for_each_entry(rq, list, queuelist) { 1888 BUG_ON(rq->mq_ctx != ctx); 1889 trace_block_rq_insert(rq); 1890 } 1891 1892 spin_lock(&ctx->lock); 1893 list_splice_tail_init(list, &ctx->rq_lists[type]); 1894 blk_mq_hctx_mark_pending(hctx, ctx); 1895 spin_unlock(&ctx->lock); 1896 } 1897 1898 static int plug_rq_cmp(void *priv, const struct list_head *a, 1899 const struct list_head *b) 1900 { 1901 struct request *rqa = container_of(a, struct request, queuelist); 1902 struct request *rqb = container_of(b, struct request, queuelist); 1903 1904 if (rqa->mq_ctx != rqb->mq_ctx) 1905 return rqa->mq_ctx > rqb->mq_ctx; 1906 if (rqa->mq_hctx != rqb->mq_hctx) 1907 return rqa->mq_hctx > rqb->mq_hctx; 1908 1909 return blk_rq_pos(rqa) > blk_rq_pos(rqb); 1910 } 1911 1912 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) 1913 { 1914 LIST_HEAD(list); 1915 1916 if (list_empty(&plug->mq_list)) 1917 return; 1918 list_splice_init(&plug->mq_list, &list); 1919 1920 if (plug->rq_count > 2 && plug->multiple_queues) 1921 list_sort(NULL, &list, plug_rq_cmp); 1922 1923 plug->rq_count = 0; 1924 1925 do { 1926 struct list_head rq_list; 1927 struct request *rq, *head_rq = list_entry_rq(list.next); 1928 struct list_head *pos = &head_rq->queuelist; /* skip first */ 1929 struct blk_mq_hw_ctx *this_hctx = head_rq->mq_hctx; 1930 struct blk_mq_ctx *this_ctx = head_rq->mq_ctx; 1931 unsigned int depth = 1; 1932 1933 list_for_each_continue(pos, &list) { 1934 rq = list_entry_rq(pos); 1935 BUG_ON(!rq->q); 1936 if (rq->mq_hctx != this_hctx || rq->mq_ctx != this_ctx) 1937 break; 1938 depth++; 1939 } 1940 1941 list_cut_before(&rq_list, &list, pos); 1942 trace_block_unplug(head_rq->q, depth, !from_schedule); 1943 blk_mq_sched_insert_requests(this_hctx, this_ctx, &rq_list, 1944 from_schedule); 1945 } while(!list_empty(&list)); 1946 } 1947 1948 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio, 1949 unsigned int nr_segs) 1950 { 1951 int err; 1952 1953 if (bio->bi_opf & REQ_RAHEAD) 1954 rq->cmd_flags |= REQ_FAILFAST_MASK; 1955 1956 rq->__sector = bio->bi_iter.bi_sector; 1957 rq->write_hint = bio->bi_write_hint; 1958 blk_rq_bio_prep(rq, bio, nr_segs); 1959 1960 /* This can't fail, since GFP_NOIO includes __GFP_DIRECT_RECLAIM. */ 1961 err = blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO); 1962 WARN_ON_ONCE(err); 1963 1964 blk_account_io_start(rq); 1965 } 1966 1967 static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx, 1968 struct request *rq, 1969 blk_qc_t *cookie, bool last) 1970 { 1971 struct request_queue *q = rq->q; 1972 struct blk_mq_queue_data bd = { 1973 .rq = rq, 1974 .last = last, 1975 }; 1976 blk_qc_t new_cookie; 1977 blk_status_t ret; 1978 1979 new_cookie = request_to_qc_t(hctx, rq); 1980 1981 /* 1982 * For OK queue, we are done. For error, caller may kill it. 1983 * Any other error (busy), just add it to our list as we 1984 * previously would have done. 1985 */ 1986 ret = q->mq_ops->queue_rq(hctx, &bd); 1987 switch (ret) { 1988 case BLK_STS_OK: 1989 blk_mq_update_dispatch_busy(hctx, false); 1990 *cookie = new_cookie; 1991 break; 1992 case BLK_STS_RESOURCE: 1993 case BLK_STS_DEV_RESOURCE: 1994 blk_mq_update_dispatch_busy(hctx, true); 1995 __blk_mq_requeue_request(rq); 1996 break; 1997 default: 1998 blk_mq_update_dispatch_busy(hctx, false); 1999 *cookie = BLK_QC_T_NONE; 2000 break; 2001 } 2002 2003 return ret; 2004 } 2005 2006 static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, 2007 struct request *rq, 2008 blk_qc_t *cookie, 2009 bool bypass_insert, bool last) 2010 { 2011 struct request_queue *q = rq->q; 2012 bool run_queue = true; 2013 2014 /* 2015 * RCU or SRCU read lock is needed before checking quiesced flag. 2016 * 2017 * When queue is stopped or quiesced, ignore 'bypass_insert' from 2018 * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller, 2019 * and avoid driver to try to dispatch again. 2020 */ 2021 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) { 2022 run_queue = false; 2023 bypass_insert = false; 2024 goto insert; 2025 } 2026 2027 if (q->elevator && !bypass_insert) 2028 goto insert; 2029 2030 if (!blk_mq_get_dispatch_budget(q)) 2031 goto insert; 2032 2033 if (!blk_mq_get_driver_tag(rq)) { 2034 blk_mq_put_dispatch_budget(q); 2035 goto insert; 2036 } 2037 2038 return __blk_mq_issue_directly(hctx, rq, cookie, last); 2039 insert: 2040 if (bypass_insert) 2041 return BLK_STS_RESOURCE; 2042 2043 blk_mq_sched_insert_request(rq, false, run_queue, false); 2044 2045 return BLK_STS_OK; 2046 } 2047 2048 /** 2049 * blk_mq_try_issue_directly - Try to send a request directly to device driver. 2050 * @hctx: Pointer of the associated hardware queue. 2051 * @rq: Pointer to request to be sent. 2052 * @cookie: Request queue cookie. 2053 * 2054 * If the device has enough resources to accept a new request now, send the 2055 * request directly to device driver. Else, insert at hctx->dispatch queue, so 2056 * we can try send it another time in the future. Requests inserted at this 2057 * queue have higher priority. 2058 */ 2059 static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, 2060 struct request *rq, blk_qc_t *cookie) 2061 { 2062 blk_status_t ret; 2063 int srcu_idx; 2064 2065 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING); 2066 2067 hctx_lock(hctx, &srcu_idx); 2068 2069 ret = __blk_mq_try_issue_directly(hctx, rq, cookie, false, true); 2070 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) 2071 blk_mq_request_bypass_insert(rq, false, true); 2072 else if (ret != BLK_STS_OK) 2073 blk_mq_end_request(rq, ret); 2074 2075 hctx_unlock(hctx, srcu_idx); 2076 } 2077 2078 blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last) 2079 { 2080 blk_status_t ret; 2081 int srcu_idx; 2082 blk_qc_t unused_cookie; 2083 struct blk_mq_hw_ctx *hctx = rq->mq_hctx; 2084 2085 hctx_lock(hctx, &srcu_idx); 2086 ret = __blk_mq_try_issue_directly(hctx, rq, &unused_cookie, true, last); 2087 hctx_unlock(hctx, srcu_idx); 2088 2089 return ret; 2090 } 2091 2092 void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx, 2093 struct list_head *list) 2094 { 2095 int queued = 0; 2096 int errors = 0; 2097 2098 while (!list_empty(list)) { 2099 blk_status_t ret; 2100 struct request *rq = list_first_entry(list, struct request, 2101 queuelist); 2102 2103 list_del_init(&rq->queuelist); 2104 ret = blk_mq_request_issue_directly(rq, list_empty(list)); 2105 if (ret != BLK_STS_OK) { 2106 if (ret == BLK_STS_RESOURCE || 2107 ret == BLK_STS_DEV_RESOURCE) { 2108 blk_mq_request_bypass_insert(rq, false, 2109 list_empty(list)); 2110 break; 2111 } 2112 blk_mq_end_request(rq, ret); 2113 errors++; 2114 } else 2115 queued++; 2116 } 2117 2118 /* 2119 * If we didn't flush the entire list, we could have told 2120 * the driver there was more coming, but that turned out to 2121 * be a lie. 2122 */ 2123 if ((!list_empty(list) || errors) && 2124 hctx->queue->mq_ops->commit_rqs && queued) 2125 hctx->queue->mq_ops->commit_rqs(hctx); 2126 } 2127 2128 static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq) 2129 { 2130 list_add_tail(&rq->queuelist, &plug->mq_list); 2131 plug->rq_count++; 2132 if (!plug->multiple_queues && !list_is_singular(&plug->mq_list)) { 2133 struct request *tmp; 2134 2135 tmp = list_first_entry(&plug->mq_list, struct request, 2136 queuelist); 2137 if (tmp->q != rq->q) 2138 plug->multiple_queues = true; 2139 } 2140 } 2141 2142 /** 2143 * blk_mq_submit_bio - Create and send a request to block device. 2144 * @bio: Bio pointer. 2145 * 2146 * Builds up a request structure from @q and @bio and send to the device. The 2147 * request may not be queued directly to hardware if: 2148 * * This request can be merged with another one 2149 * * We want to place request at plug queue for possible future merging 2150 * * There is an IO scheduler active at this queue 2151 * 2152 * It will not queue the request if there is an error with the bio, or at the 2153 * request creation. 2154 * 2155 * Returns: Request queue cookie. 2156 */ 2157 blk_qc_t blk_mq_submit_bio(struct bio *bio) 2158 { 2159 struct request_queue *q = bio->bi_bdev->bd_disk->queue; 2160 const int is_sync = op_is_sync(bio->bi_opf); 2161 const int is_flush_fua = op_is_flush(bio->bi_opf); 2162 struct blk_mq_alloc_data data = { 2163 .q = q, 2164 }; 2165 struct request *rq; 2166 struct blk_plug *plug; 2167 struct request *same_queue_rq = NULL; 2168 unsigned int nr_segs; 2169 blk_qc_t cookie; 2170 blk_status_t ret; 2171 bool hipri; 2172 2173 blk_queue_bounce(q, &bio); 2174 __blk_queue_split(&bio, &nr_segs); 2175 2176 if (!bio_integrity_prep(bio)) 2177 goto queue_exit; 2178 2179 if (!is_flush_fua && !blk_queue_nomerges(q) && 2180 blk_attempt_plug_merge(q, bio, nr_segs, &same_queue_rq)) 2181 goto queue_exit; 2182 2183 if (blk_mq_sched_bio_merge(q, bio, nr_segs)) 2184 goto queue_exit; 2185 2186 rq_qos_throttle(q, bio); 2187 2188 hipri = bio->bi_opf & REQ_HIPRI; 2189 2190 data.cmd_flags = bio->bi_opf; 2191 rq = __blk_mq_alloc_request(&data); 2192 if (unlikely(!rq)) { 2193 rq_qos_cleanup(q, bio); 2194 if (bio->bi_opf & REQ_NOWAIT) 2195 bio_wouldblock_error(bio); 2196 goto queue_exit; 2197 } 2198 2199 trace_block_getrq(bio); 2200 2201 rq_qos_track(q, rq, bio); 2202 2203 cookie = request_to_qc_t(data.hctx, rq); 2204 2205 blk_mq_bio_to_request(rq, bio, nr_segs); 2206 2207 ret = blk_crypto_init_request(rq); 2208 if (ret != BLK_STS_OK) { 2209 bio->bi_status = ret; 2210 bio_endio(bio); 2211 blk_mq_free_request(rq); 2212 return BLK_QC_T_NONE; 2213 } 2214 2215 plug = blk_mq_plug(q, bio); 2216 if (unlikely(is_flush_fua)) { 2217 /* Bypass scheduler for flush requests */ 2218 blk_insert_flush(rq); 2219 blk_mq_run_hw_queue(data.hctx, true); 2220 } else if (plug && (q->nr_hw_queues == 1 || q->mq_ops->commit_rqs || 2221 !blk_queue_nonrot(q))) { 2222 /* 2223 * Use plugging if we have a ->commit_rqs() hook as well, as 2224 * we know the driver uses bd->last in a smart fashion. 2225 * 2226 * Use normal plugging if this disk is slow HDD, as sequential 2227 * IO may benefit a lot from plug merging. 2228 */ 2229 unsigned int request_count = plug->rq_count; 2230 struct request *last = NULL; 2231 2232 if (!request_count) 2233 trace_block_plug(q); 2234 else 2235 last = list_entry_rq(plug->mq_list.prev); 2236 2237 if (request_count >= BLK_MAX_REQUEST_COUNT || (last && 2238 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) { 2239 blk_flush_plug_list(plug, false); 2240 trace_block_plug(q); 2241 } 2242 2243 blk_add_rq_to_plug(plug, rq); 2244 } else if (q->elevator) { 2245 /* Insert the request at the IO scheduler queue */ 2246 blk_mq_sched_insert_request(rq, false, true, true); 2247 } else if (plug && !blk_queue_nomerges(q)) { 2248 /* 2249 * We do limited plugging. If the bio can be merged, do that. 2250 * Otherwise the existing request in the plug list will be 2251 * issued. So the plug list will have one request at most 2252 * The plug list might get flushed before this. If that happens, 2253 * the plug list is empty, and same_queue_rq is invalid. 2254 */ 2255 if (list_empty(&plug->mq_list)) 2256 same_queue_rq = NULL; 2257 if (same_queue_rq) { 2258 list_del_init(&same_queue_rq->queuelist); 2259 plug->rq_count--; 2260 } 2261 blk_add_rq_to_plug(plug, rq); 2262 trace_block_plug(q); 2263 2264 if (same_queue_rq) { 2265 data.hctx = same_queue_rq->mq_hctx; 2266 trace_block_unplug(q, 1, true); 2267 blk_mq_try_issue_directly(data.hctx, same_queue_rq, 2268 &cookie); 2269 } 2270 } else if ((q->nr_hw_queues > 1 && is_sync) || 2271 !data.hctx->dispatch_busy) { 2272 /* 2273 * There is no scheduler and we can try to send directly 2274 * to the hardware. 2275 */ 2276 blk_mq_try_issue_directly(data.hctx, rq, &cookie); 2277 } else { 2278 /* Default case. */ 2279 blk_mq_sched_insert_request(rq, false, true, true); 2280 } 2281 2282 if (!hipri) 2283 return BLK_QC_T_NONE; 2284 return cookie; 2285 queue_exit: 2286 blk_queue_exit(q); 2287 return BLK_QC_T_NONE; 2288 } 2289 2290 void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags, 2291 unsigned int hctx_idx) 2292 { 2293 struct page *page; 2294 2295 if (tags->rqs && set->ops->exit_request) { 2296 int i; 2297 2298 for (i = 0; i < tags->nr_tags; i++) { 2299 struct request *rq = tags->static_rqs[i]; 2300 2301 if (!rq) 2302 continue; 2303 set->ops->exit_request(set, rq, hctx_idx); 2304 tags->static_rqs[i] = NULL; 2305 } 2306 } 2307 2308 while (!list_empty(&tags->page_list)) { 2309 page = list_first_entry(&tags->page_list, struct page, lru); 2310 list_del_init(&page->lru); 2311 /* 2312 * Remove kmemleak object previously allocated in 2313 * blk_mq_alloc_rqs(). 2314 */ 2315 kmemleak_free(page_address(page)); 2316 __free_pages(page, page->private); 2317 } 2318 } 2319 2320 void blk_mq_free_rq_map(struct blk_mq_tags *tags, unsigned int flags) 2321 { 2322 kfree(tags->rqs); 2323 tags->rqs = NULL; 2324 kfree(tags->static_rqs); 2325 tags->static_rqs = NULL; 2326 2327 blk_mq_free_tags(tags, flags); 2328 } 2329 2330 struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, 2331 unsigned int hctx_idx, 2332 unsigned int nr_tags, 2333 unsigned int reserved_tags, 2334 unsigned int flags) 2335 { 2336 struct blk_mq_tags *tags; 2337 int node; 2338 2339 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx); 2340 if (node == NUMA_NO_NODE) 2341 node = set->numa_node; 2342 2343 tags = blk_mq_init_tags(nr_tags, reserved_tags, node, flags); 2344 if (!tags) 2345 return NULL; 2346 2347 tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *), 2348 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, 2349 node); 2350 if (!tags->rqs) { 2351 blk_mq_free_tags(tags, flags); 2352 return NULL; 2353 } 2354 2355 tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *), 2356 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, 2357 node); 2358 if (!tags->static_rqs) { 2359 kfree(tags->rqs); 2360 blk_mq_free_tags(tags, flags); 2361 return NULL; 2362 } 2363 2364 return tags; 2365 } 2366 2367 static size_t order_to_size(unsigned int order) 2368 { 2369 return (size_t)PAGE_SIZE << order; 2370 } 2371 2372 static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq, 2373 unsigned int hctx_idx, int node) 2374 { 2375 int ret; 2376 2377 if (set->ops->init_request) { 2378 ret = set->ops->init_request(set, rq, hctx_idx, node); 2379 if (ret) 2380 return ret; 2381 } 2382 2383 WRITE_ONCE(rq->state, MQ_RQ_IDLE); 2384 return 0; 2385 } 2386 2387 int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags, 2388 unsigned int hctx_idx, unsigned int depth) 2389 { 2390 unsigned int i, j, entries_per_page, max_order = 4; 2391 size_t rq_size, left; 2392 int node; 2393 2394 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx); 2395 if (node == NUMA_NO_NODE) 2396 node = set->numa_node; 2397 2398 INIT_LIST_HEAD(&tags->page_list); 2399 2400 /* 2401 * rq_size is the size of the request plus driver payload, rounded 2402 * to the cacheline size 2403 */ 2404 rq_size = round_up(sizeof(struct request) + set->cmd_size, 2405 cache_line_size()); 2406 left = rq_size * depth; 2407 2408 for (i = 0; i < depth; ) { 2409 int this_order = max_order; 2410 struct page *page; 2411 int to_do; 2412 void *p; 2413 2414 while (this_order && left < order_to_size(this_order - 1)) 2415 this_order--; 2416 2417 do { 2418 page = alloc_pages_node(node, 2419 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO, 2420 this_order); 2421 if (page) 2422 break; 2423 if (!this_order--) 2424 break; 2425 if (order_to_size(this_order) < rq_size) 2426 break; 2427 } while (1); 2428 2429 if (!page) 2430 goto fail; 2431 2432 page->private = this_order; 2433 list_add_tail(&page->lru, &tags->page_list); 2434 2435 p = page_address(page); 2436 /* 2437 * Allow kmemleak to scan these pages as they contain pointers 2438 * to additional allocations like via ops->init_request(). 2439 */ 2440 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO); 2441 entries_per_page = order_to_size(this_order) / rq_size; 2442 to_do = min(entries_per_page, depth - i); 2443 left -= to_do * rq_size; 2444 for (j = 0; j < to_do; j++) { 2445 struct request *rq = p; 2446 2447 tags->static_rqs[i] = rq; 2448 if (blk_mq_init_request(set, rq, hctx_idx, node)) { 2449 tags->static_rqs[i] = NULL; 2450 goto fail; 2451 } 2452 2453 p += rq_size; 2454 i++; 2455 } 2456 } 2457 return 0; 2458 2459 fail: 2460 blk_mq_free_rqs(set, tags, hctx_idx); 2461 return -ENOMEM; 2462 } 2463 2464 struct rq_iter_data { 2465 struct blk_mq_hw_ctx *hctx; 2466 bool has_rq; 2467 }; 2468 2469 static bool blk_mq_has_request(struct request *rq, void *data, bool reserved) 2470 { 2471 struct rq_iter_data *iter_data = data; 2472 2473 if (rq->mq_hctx != iter_data->hctx) 2474 return true; 2475 iter_data->has_rq = true; 2476 return false; 2477 } 2478 2479 static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx) 2480 { 2481 struct blk_mq_tags *tags = hctx->sched_tags ? 2482 hctx->sched_tags : hctx->tags; 2483 struct rq_iter_data data = { 2484 .hctx = hctx, 2485 }; 2486 2487 blk_mq_all_tag_iter(tags, blk_mq_has_request, &data); 2488 return data.has_rq; 2489 } 2490 2491 static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu, 2492 struct blk_mq_hw_ctx *hctx) 2493 { 2494 if (cpumask_next_and(-1, hctx->cpumask, cpu_online_mask) != cpu) 2495 return false; 2496 if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids) 2497 return false; 2498 return true; 2499 } 2500 2501 static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node) 2502 { 2503 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node, 2504 struct blk_mq_hw_ctx, cpuhp_online); 2505 2506 if (!cpumask_test_cpu(cpu, hctx->cpumask) || 2507 !blk_mq_last_cpu_in_hctx(cpu, hctx)) 2508 return 0; 2509 2510 /* 2511 * Prevent new request from being allocated on the current hctx. 2512 * 2513 * The smp_mb__after_atomic() Pairs with the implied barrier in 2514 * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is 2515 * seen once we return from the tag allocator. 2516 */ 2517 set_bit(BLK_MQ_S_INACTIVE, &hctx->state); 2518 smp_mb__after_atomic(); 2519 2520 /* 2521 * Try to grab a reference to the queue and wait for any outstanding 2522 * requests. If we could not grab a reference the queue has been 2523 * frozen and there are no requests. 2524 */ 2525 if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) { 2526 while (blk_mq_hctx_has_requests(hctx)) 2527 msleep(5); 2528 percpu_ref_put(&hctx->queue->q_usage_counter); 2529 } 2530 2531 return 0; 2532 } 2533 2534 static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node) 2535 { 2536 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node, 2537 struct blk_mq_hw_ctx, cpuhp_online); 2538 2539 if (cpumask_test_cpu(cpu, hctx->cpumask)) 2540 clear_bit(BLK_MQ_S_INACTIVE, &hctx->state); 2541 return 0; 2542 } 2543 2544 /* 2545 * 'cpu' is going away. splice any existing rq_list entries from this 2546 * software queue to the hw queue dispatch list, and ensure that it 2547 * gets run. 2548 */ 2549 static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node) 2550 { 2551 struct blk_mq_hw_ctx *hctx; 2552 struct blk_mq_ctx *ctx; 2553 LIST_HEAD(tmp); 2554 enum hctx_type type; 2555 2556 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead); 2557 if (!cpumask_test_cpu(cpu, hctx->cpumask)) 2558 return 0; 2559 2560 ctx = __blk_mq_get_ctx(hctx->queue, cpu); 2561 type = hctx->type; 2562 2563 spin_lock(&ctx->lock); 2564 if (!list_empty(&ctx->rq_lists[type])) { 2565 list_splice_init(&ctx->rq_lists[type], &tmp); 2566 blk_mq_hctx_clear_pending(hctx, ctx); 2567 } 2568 spin_unlock(&ctx->lock); 2569 2570 if (list_empty(&tmp)) 2571 return 0; 2572 2573 spin_lock(&hctx->lock); 2574 list_splice_tail_init(&tmp, &hctx->dispatch); 2575 spin_unlock(&hctx->lock); 2576 2577 blk_mq_run_hw_queue(hctx, true); 2578 return 0; 2579 } 2580 2581 static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx) 2582 { 2583 if (!(hctx->flags & BLK_MQ_F_STACKING)) 2584 cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE, 2585 &hctx->cpuhp_online); 2586 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD, 2587 &hctx->cpuhp_dead); 2588 } 2589 2590 /* hctx->ctxs will be freed in queue's release handler */ 2591 static void blk_mq_exit_hctx(struct request_queue *q, 2592 struct blk_mq_tag_set *set, 2593 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx) 2594 { 2595 if (blk_mq_hw_queue_mapped(hctx)) 2596 blk_mq_tag_idle(hctx); 2597 2598 if (set->ops->exit_request) 2599 set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx); 2600 2601 if (set->ops->exit_hctx) 2602 set->ops->exit_hctx(hctx, hctx_idx); 2603 2604 blk_mq_remove_cpuhp(hctx); 2605 2606 spin_lock(&q->unused_hctx_lock); 2607 list_add(&hctx->hctx_list, &q->unused_hctx_list); 2608 spin_unlock(&q->unused_hctx_lock); 2609 } 2610 2611 static void blk_mq_exit_hw_queues(struct request_queue *q, 2612 struct blk_mq_tag_set *set, int nr_queue) 2613 { 2614 struct blk_mq_hw_ctx *hctx; 2615 unsigned int i; 2616 2617 queue_for_each_hw_ctx(q, hctx, i) { 2618 if (i == nr_queue) 2619 break; 2620 blk_mq_debugfs_unregister_hctx(hctx); 2621 blk_mq_exit_hctx(q, set, hctx, i); 2622 } 2623 } 2624 2625 static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set) 2626 { 2627 int hw_ctx_size = sizeof(struct blk_mq_hw_ctx); 2628 2629 BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu), 2630 __alignof__(struct blk_mq_hw_ctx)) != 2631 sizeof(struct blk_mq_hw_ctx)); 2632 2633 if (tag_set->flags & BLK_MQ_F_BLOCKING) 2634 hw_ctx_size += sizeof(struct srcu_struct); 2635 2636 return hw_ctx_size; 2637 } 2638 2639 static int blk_mq_init_hctx(struct request_queue *q, 2640 struct blk_mq_tag_set *set, 2641 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx) 2642 { 2643 hctx->queue_num = hctx_idx; 2644 2645 if (!(hctx->flags & BLK_MQ_F_STACKING)) 2646 cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE, 2647 &hctx->cpuhp_online); 2648 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead); 2649 2650 hctx->tags = set->tags[hctx_idx]; 2651 2652 if (set->ops->init_hctx && 2653 set->ops->init_hctx(hctx, set->driver_data, hctx_idx)) 2654 goto unregister_cpu_notifier; 2655 2656 if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx, 2657 hctx->numa_node)) 2658 goto exit_hctx; 2659 return 0; 2660 2661 exit_hctx: 2662 if (set->ops->exit_hctx) 2663 set->ops->exit_hctx(hctx, hctx_idx); 2664 unregister_cpu_notifier: 2665 blk_mq_remove_cpuhp(hctx); 2666 return -1; 2667 } 2668 2669 static struct blk_mq_hw_ctx * 2670 blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set, 2671 int node) 2672 { 2673 struct blk_mq_hw_ctx *hctx; 2674 gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY; 2675 2676 hctx = kzalloc_node(blk_mq_hw_ctx_size(set), gfp, node); 2677 if (!hctx) 2678 goto fail_alloc_hctx; 2679 2680 if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node)) 2681 goto free_hctx; 2682 2683 atomic_set(&hctx->nr_active, 0); 2684 if (node == NUMA_NO_NODE) 2685 node = set->numa_node; 2686 hctx->numa_node = node; 2687 2688 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn); 2689 spin_lock_init(&hctx->lock); 2690 INIT_LIST_HEAD(&hctx->dispatch); 2691 hctx->queue = q; 2692 hctx->flags = set->flags & ~BLK_MQ_F_TAG_QUEUE_SHARED; 2693 2694 INIT_LIST_HEAD(&hctx->hctx_list); 2695 2696 /* 2697 * Allocate space for all possible cpus to avoid allocation at 2698 * runtime 2699 */ 2700 hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *), 2701 gfp, node); 2702 if (!hctx->ctxs) 2703 goto free_cpumask; 2704 2705 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8), 2706 gfp, node)) 2707 goto free_ctxs; 2708 hctx->nr_ctx = 0; 2709 2710 spin_lock_init(&hctx->dispatch_wait_lock); 2711 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake); 2712 INIT_LIST_HEAD(&hctx->dispatch_wait.entry); 2713 2714 hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp); 2715 if (!hctx->fq) 2716 goto free_bitmap; 2717 2718 if (hctx->flags & BLK_MQ_F_BLOCKING) 2719 init_srcu_struct(hctx->srcu); 2720 blk_mq_hctx_kobj_init(hctx); 2721 2722 return hctx; 2723 2724 free_bitmap: 2725 sbitmap_free(&hctx->ctx_map); 2726 free_ctxs: 2727 kfree(hctx->ctxs); 2728 free_cpumask: 2729 free_cpumask_var(hctx->cpumask); 2730 free_hctx: 2731 kfree(hctx); 2732 fail_alloc_hctx: 2733 return NULL; 2734 } 2735 2736 static void blk_mq_init_cpu_queues(struct request_queue *q, 2737 unsigned int nr_hw_queues) 2738 { 2739 struct blk_mq_tag_set *set = q->tag_set; 2740 unsigned int i, j; 2741 2742 for_each_possible_cpu(i) { 2743 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); 2744 struct blk_mq_hw_ctx *hctx; 2745 int k; 2746 2747 __ctx->cpu = i; 2748 spin_lock_init(&__ctx->lock); 2749 for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++) 2750 INIT_LIST_HEAD(&__ctx->rq_lists[k]); 2751 2752 __ctx->queue = q; 2753 2754 /* 2755 * Set local node, IFF we have more than one hw queue. If 2756 * not, we remain on the home node of the device 2757 */ 2758 for (j = 0; j < set->nr_maps; j++) { 2759 hctx = blk_mq_map_queue_type(q, j, i); 2760 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) 2761 hctx->numa_node = cpu_to_node(i); 2762 } 2763 } 2764 } 2765 2766 static bool __blk_mq_alloc_map_and_request(struct blk_mq_tag_set *set, 2767 int hctx_idx) 2768 { 2769 unsigned int flags = set->flags; 2770 int ret = 0; 2771 2772 set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx, 2773 set->queue_depth, set->reserved_tags, flags); 2774 if (!set->tags[hctx_idx]) 2775 return false; 2776 2777 ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx, 2778 set->queue_depth); 2779 if (!ret) 2780 return true; 2781 2782 blk_mq_free_rq_map(set->tags[hctx_idx], flags); 2783 set->tags[hctx_idx] = NULL; 2784 return false; 2785 } 2786 2787 static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set, 2788 unsigned int hctx_idx) 2789 { 2790 unsigned int flags = set->flags; 2791 2792 if (set->tags && set->tags[hctx_idx]) { 2793 blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx); 2794 blk_mq_free_rq_map(set->tags[hctx_idx], flags); 2795 set->tags[hctx_idx] = NULL; 2796 } 2797 } 2798 2799 static void blk_mq_map_swqueue(struct request_queue *q) 2800 { 2801 unsigned int i, j, hctx_idx; 2802 struct blk_mq_hw_ctx *hctx; 2803 struct blk_mq_ctx *ctx; 2804 struct blk_mq_tag_set *set = q->tag_set; 2805 2806 queue_for_each_hw_ctx(q, hctx, i) { 2807 cpumask_clear(hctx->cpumask); 2808 hctx->nr_ctx = 0; 2809 hctx->dispatch_from = NULL; 2810 } 2811 2812 /* 2813 * Map software to hardware queues. 2814 * 2815 * If the cpu isn't present, the cpu is mapped to first hctx. 2816 */ 2817 for_each_possible_cpu(i) { 2818 2819 ctx = per_cpu_ptr(q->queue_ctx, i); 2820 for (j = 0; j < set->nr_maps; j++) { 2821 if (!set->map[j].nr_queues) { 2822 ctx->hctxs[j] = blk_mq_map_queue_type(q, 2823 HCTX_TYPE_DEFAULT, i); 2824 continue; 2825 } 2826 hctx_idx = set->map[j].mq_map[i]; 2827 /* unmapped hw queue can be remapped after CPU topo changed */ 2828 if (!set->tags[hctx_idx] && 2829 !__blk_mq_alloc_map_and_request(set, hctx_idx)) { 2830 /* 2831 * If tags initialization fail for some hctx, 2832 * that hctx won't be brought online. In this 2833 * case, remap the current ctx to hctx[0] which 2834 * is guaranteed to always have tags allocated 2835 */ 2836 set->map[j].mq_map[i] = 0; 2837 } 2838 2839 hctx = blk_mq_map_queue_type(q, j, i); 2840 ctx->hctxs[j] = hctx; 2841 /* 2842 * If the CPU is already set in the mask, then we've 2843 * mapped this one already. This can happen if 2844 * devices share queues across queue maps. 2845 */ 2846 if (cpumask_test_cpu(i, hctx->cpumask)) 2847 continue; 2848 2849 cpumask_set_cpu(i, hctx->cpumask); 2850 hctx->type = j; 2851 ctx->index_hw[hctx->type] = hctx->nr_ctx; 2852 hctx->ctxs[hctx->nr_ctx++] = ctx; 2853 2854 /* 2855 * If the nr_ctx type overflows, we have exceeded the 2856 * amount of sw queues we can support. 2857 */ 2858 BUG_ON(!hctx->nr_ctx); 2859 } 2860 2861 for (; j < HCTX_MAX_TYPES; j++) 2862 ctx->hctxs[j] = blk_mq_map_queue_type(q, 2863 HCTX_TYPE_DEFAULT, i); 2864 } 2865 2866 queue_for_each_hw_ctx(q, hctx, i) { 2867 /* 2868 * If no software queues are mapped to this hardware queue, 2869 * disable it and free the request entries. 2870 */ 2871 if (!hctx->nr_ctx) { 2872 /* Never unmap queue 0. We need it as a 2873 * fallback in case of a new remap fails 2874 * allocation 2875 */ 2876 if (i && set->tags[i]) 2877 blk_mq_free_map_and_requests(set, i); 2878 2879 hctx->tags = NULL; 2880 continue; 2881 } 2882 2883 hctx->tags = set->tags[i]; 2884 WARN_ON(!hctx->tags); 2885 2886 /* 2887 * Set the map size to the number of mapped software queues. 2888 * This is more accurate and more efficient than looping 2889 * over all possibly mapped software queues. 2890 */ 2891 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx); 2892 2893 /* 2894 * Initialize batch roundrobin counts 2895 */ 2896 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx); 2897 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; 2898 } 2899 } 2900 2901 /* 2902 * Caller needs to ensure that we're either frozen/quiesced, or that 2903 * the queue isn't live yet. 2904 */ 2905 static void queue_set_hctx_shared(struct request_queue *q, bool shared) 2906 { 2907 struct blk_mq_hw_ctx *hctx; 2908 int i; 2909 2910 queue_for_each_hw_ctx(q, hctx, i) { 2911 if (shared) 2912 hctx->flags |= BLK_MQ_F_TAG_QUEUE_SHARED; 2913 else 2914 hctx->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED; 2915 } 2916 } 2917 2918 static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set, 2919 bool shared) 2920 { 2921 struct request_queue *q; 2922 2923 lockdep_assert_held(&set->tag_list_lock); 2924 2925 list_for_each_entry(q, &set->tag_list, tag_set_list) { 2926 blk_mq_freeze_queue(q); 2927 queue_set_hctx_shared(q, shared); 2928 blk_mq_unfreeze_queue(q); 2929 } 2930 } 2931 2932 static void blk_mq_del_queue_tag_set(struct request_queue *q) 2933 { 2934 struct blk_mq_tag_set *set = q->tag_set; 2935 2936 mutex_lock(&set->tag_list_lock); 2937 list_del(&q->tag_set_list); 2938 if (list_is_singular(&set->tag_list)) { 2939 /* just transitioned to unshared */ 2940 set->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED; 2941 /* update existing queue */ 2942 blk_mq_update_tag_set_shared(set, false); 2943 } 2944 mutex_unlock(&set->tag_list_lock); 2945 INIT_LIST_HEAD(&q->tag_set_list); 2946 } 2947 2948 static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set, 2949 struct request_queue *q) 2950 { 2951 mutex_lock(&set->tag_list_lock); 2952 2953 /* 2954 * Check to see if we're transitioning to shared (from 1 to 2 queues). 2955 */ 2956 if (!list_empty(&set->tag_list) && 2957 !(set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) { 2958 set->flags |= BLK_MQ_F_TAG_QUEUE_SHARED; 2959 /* update existing queue */ 2960 blk_mq_update_tag_set_shared(set, true); 2961 } 2962 if (set->flags & BLK_MQ_F_TAG_QUEUE_SHARED) 2963 queue_set_hctx_shared(q, true); 2964 list_add_tail(&q->tag_set_list, &set->tag_list); 2965 2966 mutex_unlock(&set->tag_list_lock); 2967 } 2968 2969 /* All allocations will be freed in release handler of q->mq_kobj */ 2970 static int blk_mq_alloc_ctxs(struct request_queue *q) 2971 { 2972 struct blk_mq_ctxs *ctxs; 2973 int cpu; 2974 2975 ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL); 2976 if (!ctxs) 2977 return -ENOMEM; 2978 2979 ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx); 2980 if (!ctxs->queue_ctx) 2981 goto fail; 2982 2983 for_each_possible_cpu(cpu) { 2984 struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu); 2985 ctx->ctxs = ctxs; 2986 } 2987 2988 q->mq_kobj = &ctxs->kobj; 2989 q->queue_ctx = ctxs->queue_ctx; 2990 2991 return 0; 2992 fail: 2993 kfree(ctxs); 2994 return -ENOMEM; 2995 } 2996 2997 /* 2998 * It is the actual release handler for mq, but we do it from 2999 * request queue's release handler for avoiding use-after-free 3000 * and headache because q->mq_kobj shouldn't have been introduced, 3001 * but we can't group ctx/kctx kobj without it. 3002 */ 3003 void blk_mq_release(struct request_queue *q) 3004 { 3005 struct blk_mq_hw_ctx *hctx, *next; 3006 int i; 3007 3008 queue_for_each_hw_ctx(q, hctx, i) 3009 WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list)); 3010 3011 /* all hctx are in .unused_hctx_list now */ 3012 list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) { 3013 list_del_init(&hctx->hctx_list); 3014 kobject_put(&hctx->kobj); 3015 } 3016 3017 kfree(q->queue_hw_ctx); 3018 3019 /* 3020 * release .mq_kobj and sw queue's kobject now because 3021 * both share lifetime with request queue. 3022 */ 3023 blk_mq_sysfs_deinit(q); 3024 } 3025 3026 struct request_queue *blk_mq_init_queue_data(struct blk_mq_tag_set *set, 3027 void *queuedata) 3028 { 3029 struct request_queue *uninit_q, *q; 3030 3031 uninit_q = blk_alloc_queue(set->numa_node); 3032 if (!uninit_q) 3033 return ERR_PTR(-ENOMEM); 3034 uninit_q->queuedata = queuedata; 3035 3036 /* 3037 * Initialize the queue without an elevator. device_add_disk() will do 3038 * the initialization. 3039 */ 3040 q = blk_mq_init_allocated_queue(set, uninit_q, false); 3041 if (IS_ERR(q)) 3042 blk_cleanup_queue(uninit_q); 3043 3044 return q; 3045 } 3046 EXPORT_SYMBOL_GPL(blk_mq_init_queue_data); 3047 3048 struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set) 3049 { 3050 return blk_mq_init_queue_data(set, NULL); 3051 } 3052 EXPORT_SYMBOL(blk_mq_init_queue); 3053 3054 /* 3055 * Helper for setting up a queue with mq ops, given queue depth, and 3056 * the passed in mq ops flags. 3057 */ 3058 struct request_queue *blk_mq_init_sq_queue(struct blk_mq_tag_set *set, 3059 const struct blk_mq_ops *ops, 3060 unsigned int queue_depth, 3061 unsigned int set_flags) 3062 { 3063 struct request_queue *q; 3064 int ret; 3065 3066 memset(set, 0, sizeof(*set)); 3067 set->ops = ops; 3068 set->nr_hw_queues = 1; 3069 set->nr_maps = 1; 3070 set->queue_depth = queue_depth; 3071 set->numa_node = NUMA_NO_NODE; 3072 set->flags = set_flags; 3073 3074 ret = blk_mq_alloc_tag_set(set); 3075 if (ret) 3076 return ERR_PTR(ret); 3077 3078 q = blk_mq_init_queue(set); 3079 if (IS_ERR(q)) { 3080 blk_mq_free_tag_set(set); 3081 return q; 3082 } 3083 3084 return q; 3085 } 3086 EXPORT_SYMBOL(blk_mq_init_sq_queue); 3087 3088 static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx( 3089 struct blk_mq_tag_set *set, struct request_queue *q, 3090 int hctx_idx, int node) 3091 { 3092 struct blk_mq_hw_ctx *hctx = NULL, *tmp; 3093 3094 /* reuse dead hctx first */ 3095 spin_lock(&q->unused_hctx_lock); 3096 list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) { 3097 if (tmp->numa_node == node) { 3098 hctx = tmp; 3099 break; 3100 } 3101 } 3102 if (hctx) 3103 list_del_init(&hctx->hctx_list); 3104 spin_unlock(&q->unused_hctx_lock); 3105 3106 if (!hctx) 3107 hctx = blk_mq_alloc_hctx(q, set, node); 3108 if (!hctx) 3109 goto fail; 3110 3111 if (blk_mq_init_hctx(q, set, hctx, hctx_idx)) 3112 goto free_hctx; 3113 3114 return hctx; 3115 3116 free_hctx: 3117 kobject_put(&hctx->kobj); 3118 fail: 3119 return NULL; 3120 } 3121 3122 static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set, 3123 struct request_queue *q) 3124 { 3125 int i, j, end; 3126 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx; 3127 3128 if (q->nr_hw_queues < set->nr_hw_queues) { 3129 struct blk_mq_hw_ctx **new_hctxs; 3130 3131 new_hctxs = kcalloc_node(set->nr_hw_queues, 3132 sizeof(*new_hctxs), GFP_KERNEL, 3133 set->numa_node); 3134 if (!new_hctxs) 3135 return; 3136 if (hctxs) 3137 memcpy(new_hctxs, hctxs, q->nr_hw_queues * 3138 sizeof(*hctxs)); 3139 q->queue_hw_ctx = new_hctxs; 3140 kfree(hctxs); 3141 hctxs = new_hctxs; 3142 } 3143 3144 /* protect against switching io scheduler */ 3145 mutex_lock(&q->sysfs_lock); 3146 for (i = 0; i < set->nr_hw_queues; i++) { 3147 int node; 3148 struct blk_mq_hw_ctx *hctx; 3149 3150 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], i); 3151 /* 3152 * If the hw queue has been mapped to another numa node, 3153 * we need to realloc the hctx. If allocation fails, fallback 3154 * to use the previous one. 3155 */ 3156 if (hctxs[i] && (hctxs[i]->numa_node == node)) 3157 continue; 3158 3159 hctx = blk_mq_alloc_and_init_hctx(set, q, i, node); 3160 if (hctx) { 3161 if (hctxs[i]) 3162 blk_mq_exit_hctx(q, set, hctxs[i], i); 3163 hctxs[i] = hctx; 3164 } else { 3165 if (hctxs[i]) 3166 pr_warn("Allocate new hctx on node %d fails,\ 3167 fallback to previous one on node %d\n", 3168 node, hctxs[i]->numa_node); 3169 else 3170 break; 3171 } 3172 } 3173 /* 3174 * Increasing nr_hw_queues fails. Free the newly allocated 3175 * hctxs and keep the previous q->nr_hw_queues. 3176 */ 3177 if (i != set->nr_hw_queues) { 3178 j = q->nr_hw_queues; 3179 end = i; 3180 } else { 3181 j = i; 3182 end = q->nr_hw_queues; 3183 q->nr_hw_queues = set->nr_hw_queues; 3184 } 3185 3186 for (; j < end; j++) { 3187 struct blk_mq_hw_ctx *hctx = hctxs[j]; 3188 3189 if (hctx) { 3190 if (hctx->tags) 3191 blk_mq_free_map_and_requests(set, j); 3192 blk_mq_exit_hctx(q, set, hctx, j); 3193 hctxs[j] = NULL; 3194 } 3195 } 3196 mutex_unlock(&q->sysfs_lock); 3197 } 3198 3199 struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set, 3200 struct request_queue *q, 3201 bool elevator_init) 3202 { 3203 /* mark the queue as mq asap */ 3204 q->mq_ops = set->ops; 3205 3206 q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn, 3207 blk_mq_poll_stats_bkt, 3208 BLK_MQ_POLL_STATS_BKTS, q); 3209 if (!q->poll_cb) 3210 goto err_exit; 3211 3212 if (blk_mq_alloc_ctxs(q)) 3213 goto err_poll; 3214 3215 /* init q->mq_kobj and sw queues' kobjects */ 3216 blk_mq_sysfs_init(q); 3217 3218 INIT_LIST_HEAD(&q->unused_hctx_list); 3219 spin_lock_init(&q->unused_hctx_lock); 3220 3221 blk_mq_realloc_hw_ctxs(set, q); 3222 if (!q->nr_hw_queues) 3223 goto err_hctxs; 3224 3225 INIT_WORK(&q->timeout_work, blk_mq_timeout_work); 3226 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ); 3227 3228 q->tag_set = set; 3229 3230 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; 3231 if (set->nr_maps > HCTX_TYPE_POLL && 3232 set->map[HCTX_TYPE_POLL].nr_queues) 3233 blk_queue_flag_set(QUEUE_FLAG_POLL, q); 3234 3235 q->sg_reserved_size = INT_MAX; 3236 3237 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work); 3238 INIT_LIST_HEAD(&q->requeue_list); 3239 spin_lock_init(&q->requeue_lock); 3240 3241 q->nr_requests = set->queue_depth; 3242 3243 /* 3244 * Default to classic polling 3245 */ 3246 q->poll_nsec = BLK_MQ_POLL_CLASSIC; 3247 3248 blk_mq_init_cpu_queues(q, set->nr_hw_queues); 3249 blk_mq_add_queue_tag_set(set, q); 3250 blk_mq_map_swqueue(q); 3251 3252 if (elevator_init) 3253 elevator_init_mq(q); 3254 3255 return q; 3256 3257 err_hctxs: 3258 kfree(q->queue_hw_ctx); 3259 q->nr_hw_queues = 0; 3260 blk_mq_sysfs_deinit(q); 3261 err_poll: 3262 blk_stat_free_callback(q->poll_cb); 3263 q->poll_cb = NULL; 3264 err_exit: 3265 q->mq_ops = NULL; 3266 return ERR_PTR(-ENOMEM); 3267 } 3268 EXPORT_SYMBOL(blk_mq_init_allocated_queue); 3269 3270 /* tags can _not_ be used after returning from blk_mq_exit_queue */ 3271 void blk_mq_exit_queue(struct request_queue *q) 3272 { 3273 struct blk_mq_tag_set *set = q->tag_set; 3274 3275 blk_mq_del_queue_tag_set(q); 3276 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues); 3277 } 3278 3279 static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set) 3280 { 3281 int i; 3282 3283 for (i = 0; i < set->nr_hw_queues; i++) { 3284 if (!__blk_mq_alloc_map_and_request(set, i)) 3285 goto out_unwind; 3286 cond_resched(); 3287 } 3288 3289 return 0; 3290 3291 out_unwind: 3292 while (--i >= 0) 3293 blk_mq_free_map_and_requests(set, i); 3294 3295 return -ENOMEM; 3296 } 3297 3298 /* 3299 * Allocate the request maps associated with this tag_set. Note that this 3300 * may reduce the depth asked for, if memory is tight. set->queue_depth 3301 * will be updated to reflect the allocated depth. 3302 */ 3303 static int blk_mq_alloc_map_and_requests(struct blk_mq_tag_set *set) 3304 { 3305 unsigned int depth; 3306 int err; 3307 3308 depth = set->queue_depth; 3309 do { 3310 err = __blk_mq_alloc_rq_maps(set); 3311 if (!err) 3312 break; 3313 3314 set->queue_depth >>= 1; 3315 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) { 3316 err = -ENOMEM; 3317 break; 3318 } 3319 } while (set->queue_depth); 3320 3321 if (!set->queue_depth || err) { 3322 pr_err("blk-mq: failed to allocate request map\n"); 3323 return -ENOMEM; 3324 } 3325 3326 if (depth != set->queue_depth) 3327 pr_info("blk-mq: reduced tag depth (%u -> %u)\n", 3328 depth, set->queue_depth); 3329 3330 return 0; 3331 } 3332 3333 static int blk_mq_update_queue_map(struct blk_mq_tag_set *set) 3334 { 3335 /* 3336 * blk_mq_map_queues() and multiple .map_queues() implementations 3337 * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the 3338 * number of hardware queues. 3339 */ 3340 if (set->nr_maps == 1) 3341 set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues; 3342 3343 if (set->ops->map_queues && !is_kdump_kernel()) { 3344 int i; 3345 3346 /* 3347 * transport .map_queues is usually done in the following 3348 * way: 3349 * 3350 * for (queue = 0; queue < set->nr_hw_queues; queue++) { 3351 * mask = get_cpu_mask(queue) 3352 * for_each_cpu(cpu, mask) 3353 * set->map[x].mq_map[cpu] = queue; 3354 * } 3355 * 3356 * When we need to remap, the table has to be cleared for 3357 * killing stale mapping since one CPU may not be mapped 3358 * to any hw queue. 3359 */ 3360 for (i = 0; i < set->nr_maps; i++) 3361 blk_mq_clear_mq_map(&set->map[i]); 3362 3363 return set->ops->map_queues(set); 3364 } else { 3365 BUG_ON(set->nr_maps > 1); 3366 return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]); 3367 } 3368 } 3369 3370 static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set, 3371 int cur_nr_hw_queues, int new_nr_hw_queues) 3372 { 3373 struct blk_mq_tags **new_tags; 3374 3375 if (cur_nr_hw_queues >= new_nr_hw_queues) 3376 return 0; 3377 3378 new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *), 3379 GFP_KERNEL, set->numa_node); 3380 if (!new_tags) 3381 return -ENOMEM; 3382 3383 if (set->tags) 3384 memcpy(new_tags, set->tags, cur_nr_hw_queues * 3385 sizeof(*set->tags)); 3386 kfree(set->tags); 3387 set->tags = new_tags; 3388 set->nr_hw_queues = new_nr_hw_queues; 3389 3390 return 0; 3391 } 3392 3393 static int blk_mq_alloc_tag_set_tags(struct blk_mq_tag_set *set, 3394 int new_nr_hw_queues) 3395 { 3396 return blk_mq_realloc_tag_set_tags(set, 0, new_nr_hw_queues); 3397 } 3398 3399 /* 3400 * Alloc a tag set to be associated with one or more request queues. 3401 * May fail with EINVAL for various error conditions. May adjust the 3402 * requested depth down, if it's too large. In that case, the set 3403 * value will be stored in set->queue_depth. 3404 */ 3405 int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) 3406 { 3407 int i, ret; 3408 3409 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS); 3410 3411 if (!set->nr_hw_queues) 3412 return -EINVAL; 3413 if (!set->queue_depth) 3414 return -EINVAL; 3415 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) 3416 return -EINVAL; 3417 3418 if (!set->ops->queue_rq) 3419 return -EINVAL; 3420 3421 if (!set->ops->get_budget ^ !set->ops->put_budget) 3422 return -EINVAL; 3423 3424 if (set->queue_depth > BLK_MQ_MAX_DEPTH) { 3425 pr_info("blk-mq: reduced tag depth to %u\n", 3426 BLK_MQ_MAX_DEPTH); 3427 set->queue_depth = BLK_MQ_MAX_DEPTH; 3428 } 3429 3430 if (!set->nr_maps) 3431 set->nr_maps = 1; 3432 else if (set->nr_maps > HCTX_MAX_TYPES) 3433 return -EINVAL; 3434 3435 /* 3436 * If a crashdump is active, then we are potentially in a very 3437 * memory constrained environment. Limit us to 1 queue and 3438 * 64 tags to prevent using too much memory. 3439 */ 3440 if (is_kdump_kernel()) { 3441 set->nr_hw_queues = 1; 3442 set->nr_maps = 1; 3443 set->queue_depth = min(64U, set->queue_depth); 3444 } 3445 /* 3446 * There is no use for more h/w queues than cpus if we just have 3447 * a single map 3448 */ 3449 if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids) 3450 set->nr_hw_queues = nr_cpu_ids; 3451 3452 if (blk_mq_alloc_tag_set_tags(set, set->nr_hw_queues) < 0) 3453 return -ENOMEM; 3454 3455 ret = -ENOMEM; 3456 for (i = 0; i < set->nr_maps; i++) { 3457 set->map[i].mq_map = kcalloc_node(nr_cpu_ids, 3458 sizeof(set->map[i].mq_map[0]), 3459 GFP_KERNEL, set->numa_node); 3460 if (!set->map[i].mq_map) 3461 goto out_free_mq_map; 3462 set->map[i].nr_queues = is_kdump_kernel() ? 1 : set->nr_hw_queues; 3463 } 3464 3465 ret = blk_mq_update_queue_map(set); 3466 if (ret) 3467 goto out_free_mq_map; 3468 3469 ret = blk_mq_alloc_map_and_requests(set); 3470 if (ret) 3471 goto out_free_mq_map; 3472 3473 if (blk_mq_is_sbitmap_shared(set->flags)) { 3474 atomic_set(&set->active_queues_shared_sbitmap, 0); 3475 3476 if (blk_mq_init_shared_sbitmap(set, set->flags)) { 3477 ret = -ENOMEM; 3478 goto out_free_mq_rq_maps; 3479 } 3480 } 3481 3482 mutex_init(&set->tag_list_lock); 3483 INIT_LIST_HEAD(&set->tag_list); 3484 3485 return 0; 3486 3487 out_free_mq_rq_maps: 3488 for (i = 0; i < set->nr_hw_queues; i++) 3489 blk_mq_free_map_and_requests(set, i); 3490 out_free_mq_map: 3491 for (i = 0; i < set->nr_maps; i++) { 3492 kfree(set->map[i].mq_map); 3493 set->map[i].mq_map = NULL; 3494 } 3495 kfree(set->tags); 3496 set->tags = NULL; 3497 return ret; 3498 } 3499 EXPORT_SYMBOL(blk_mq_alloc_tag_set); 3500 3501 void blk_mq_free_tag_set(struct blk_mq_tag_set *set) 3502 { 3503 int i, j; 3504 3505 for (i = 0; i < set->nr_hw_queues; i++) 3506 blk_mq_free_map_and_requests(set, i); 3507 3508 if (blk_mq_is_sbitmap_shared(set->flags)) 3509 blk_mq_exit_shared_sbitmap(set); 3510 3511 for (j = 0; j < set->nr_maps; j++) { 3512 kfree(set->map[j].mq_map); 3513 set->map[j].mq_map = NULL; 3514 } 3515 3516 kfree(set->tags); 3517 set->tags = NULL; 3518 } 3519 EXPORT_SYMBOL(blk_mq_free_tag_set); 3520 3521 int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) 3522 { 3523 struct blk_mq_tag_set *set = q->tag_set; 3524 struct blk_mq_hw_ctx *hctx; 3525 int i, ret; 3526 3527 if (!set) 3528 return -EINVAL; 3529 3530 if (q->nr_requests == nr) 3531 return 0; 3532 3533 blk_mq_freeze_queue(q); 3534 blk_mq_quiesce_queue(q); 3535 3536 ret = 0; 3537 queue_for_each_hw_ctx(q, hctx, i) { 3538 if (!hctx->tags) 3539 continue; 3540 /* 3541 * If we're using an MQ scheduler, just update the scheduler 3542 * queue depth. This is similar to what the old code would do. 3543 */ 3544 if (!hctx->sched_tags) { 3545 ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr, 3546 false); 3547 if (!ret && blk_mq_is_sbitmap_shared(set->flags)) 3548 blk_mq_tag_resize_shared_sbitmap(set, nr); 3549 } else { 3550 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags, 3551 nr, true); 3552 } 3553 if (ret) 3554 break; 3555 if (q->elevator && q->elevator->type->ops.depth_updated) 3556 q->elevator->type->ops.depth_updated(hctx); 3557 } 3558 3559 if (!ret) 3560 q->nr_requests = nr; 3561 3562 blk_mq_unquiesce_queue(q); 3563 blk_mq_unfreeze_queue(q); 3564 3565 return ret; 3566 } 3567 3568 /* 3569 * request_queue and elevator_type pair. 3570 * It is just used by __blk_mq_update_nr_hw_queues to cache 3571 * the elevator_type associated with a request_queue. 3572 */ 3573 struct blk_mq_qe_pair { 3574 struct list_head node; 3575 struct request_queue *q; 3576 struct elevator_type *type; 3577 }; 3578 3579 /* 3580 * Cache the elevator_type in qe pair list and switch the 3581 * io scheduler to 'none' 3582 */ 3583 static bool blk_mq_elv_switch_none(struct list_head *head, 3584 struct request_queue *q) 3585 { 3586 struct blk_mq_qe_pair *qe; 3587 3588 if (!q->elevator) 3589 return true; 3590 3591 qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY); 3592 if (!qe) 3593 return false; 3594 3595 INIT_LIST_HEAD(&qe->node); 3596 qe->q = q; 3597 qe->type = q->elevator->type; 3598 list_add(&qe->node, head); 3599 3600 mutex_lock(&q->sysfs_lock); 3601 /* 3602 * After elevator_switch_mq, the previous elevator_queue will be 3603 * released by elevator_release. The reference of the io scheduler 3604 * module get by elevator_get will also be put. So we need to get 3605 * a reference of the io scheduler module here to prevent it to be 3606 * removed. 3607 */ 3608 __module_get(qe->type->elevator_owner); 3609 elevator_switch_mq(q, NULL); 3610 mutex_unlock(&q->sysfs_lock); 3611 3612 return true; 3613 } 3614 3615 static void blk_mq_elv_switch_back(struct list_head *head, 3616 struct request_queue *q) 3617 { 3618 struct blk_mq_qe_pair *qe; 3619 struct elevator_type *t = NULL; 3620 3621 list_for_each_entry(qe, head, node) 3622 if (qe->q == q) { 3623 t = qe->type; 3624 break; 3625 } 3626 3627 if (!t) 3628 return; 3629 3630 list_del(&qe->node); 3631 kfree(qe); 3632 3633 mutex_lock(&q->sysfs_lock); 3634 elevator_switch_mq(q, t); 3635 mutex_unlock(&q->sysfs_lock); 3636 } 3637 3638 static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, 3639 int nr_hw_queues) 3640 { 3641 struct request_queue *q; 3642 LIST_HEAD(head); 3643 int prev_nr_hw_queues; 3644 3645 lockdep_assert_held(&set->tag_list_lock); 3646 3647 if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids) 3648 nr_hw_queues = nr_cpu_ids; 3649 if (nr_hw_queues < 1) 3650 return; 3651 if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues) 3652 return; 3653 3654 list_for_each_entry(q, &set->tag_list, tag_set_list) 3655 blk_mq_freeze_queue(q); 3656 /* 3657 * Switch IO scheduler to 'none', cleaning up the data associated 3658 * with the previous scheduler. We will switch back once we are done 3659 * updating the new sw to hw queue mappings. 3660 */ 3661 list_for_each_entry(q, &set->tag_list, tag_set_list) 3662 if (!blk_mq_elv_switch_none(&head, q)) 3663 goto switch_back; 3664 3665 list_for_each_entry(q, &set->tag_list, tag_set_list) { 3666 blk_mq_debugfs_unregister_hctxs(q); 3667 blk_mq_sysfs_unregister(q); 3668 } 3669 3670 prev_nr_hw_queues = set->nr_hw_queues; 3671 if (blk_mq_realloc_tag_set_tags(set, set->nr_hw_queues, nr_hw_queues) < 3672 0) 3673 goto reregister; 3674 3675 set->nr_hw_queues = nr_hw_queues; 3676 fallback: 3677 blk_mq_update_queue_map(set); 3678 list_for_each_entry(q, &set->tag_list, tag_set_list) { 3679 blk_mq_realloc_hw_ctxs(set, q); 3680 if (q->nr_hw_queues != set->nr_hw_queues) { 3681 pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n", 3682 nr_hw_queues, prev_nr_hw_queues); 3683 set->nr_hw_queues = prev_nr_hw_queues; 3684 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]); 3685 goto fallback; 3686 } 3687 blk_mq_map_swqueue(q); 3688 } 3689 3690 reregister: 3691 list_for_each_entry(q, &set->tag_list, tag_set_list) { 3692 blk_mq_sysfs_register(q); 3693 blk_mq_debugfs_register_hctxs(q); 3694 } 3695 3696 switch_back: 3697 list_for_each_entry(q, &set->tag_list, tag_set_list) 3698 blk_mq_elv_switch_back(&head, q); 3699 3700 list_for_each_entry(q, &set->tag_list, tag_set_list) 3701 blk_mq_unfreeze_queue(q); 3702 } 3703 3704 void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues) 3705 { 3706 mutex_lock(&set->tag_list_lock); 3707 __blk_mq_update_nr_hw_queues(set, nr_hw_queues); 3708 mutex_unlock(&set->tag_list_lock); 3709 } 3710 EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues); 3711 3712 /* Enable polling stats and return whether they were already enabled. */ 3713 static bool blk_poll_stats_enable(struct request_queue *q) 3714 { 3715 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) || 3716 blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q)) 3717 return true; 3718 blk_stat_add_callback(q, q->poll_cb); 3719 return false; 3720 } 3721 3722 static void blk_mq_poll_stats_start(struct request_queue *q) 3723 { 3724 /* 3725 * We don't arm the callback if polling stats are not enabled or the 3726 * callback is already active. 3727 */ 3728 if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) || 3729 blk_stat_is_active(q->poll_cb)) 3730 return; 3731 3732 blk_stat_activate_msecs(q->poll_cb, 100); 3733 } 3734 3735 static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb) 3736 { 3737 struct request_queue *q = cb->data; 3738 int bucket; 3739 3740 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) { 3741 if (cb->stat[bucket].nr_samples) 3742 q->poll_stat[bucket] = cb->stat[bucket]; 3743 } 3744 } 3745 3746 static unsigned long blk_mq_poll_nsecs(struct request_queue *q, 3747 struct request *rq) 3748 { 3749 unsigned long ret = 0; 3750 int bucket; 3751 3752 /* 3753 * If stats collection isn't on, don't sleep but turn it on for 3754 * future users 3755 */ 3756 if (!blk_poll_stats_enable(q)) 3757 return 0; 3758 3759 /* 3760 * As an optimistic guess, use half of the mean service time 3761 * for this type of request. We can (and should) make this smarter. 3762 * For instance, if the completion latencies are tight, we can 3763 * get closer than just half the mean. This is especially 3764 * important on devices where the completion latencies are longer 3765 * than ~10 usec. We do use the stats for the relevant IO size 3766 * if available which does lead to better estimates. 3767 */ 3768 bucket = blk_mq_poll_stats_bkt(rq); 3769 if (bucket < 0) 3770 return ret; 3771 3772 if (q->poll_stat[bucket].nr_samples) 3773 ret = (q->poll_stat[bucket].mean + 1) / 2; 3774 3775 return ret; 3776 } 3777 3778 static bool blk_mq_poll_hybrid_sleep(struct request_queue *q, 3779 struct request *rq) 3780 { 3781 struct hrtimer_sleeper hs; 3782 enum hrtimer_mode mode; 3783 unsigned int nsecs; 3784 ktime_t kt; 3785 3786 if (rq->rq_flags & RQF_MQ_POLL_SLEPT) 3787 return false; 3788 3789 /* 3790 * If we get here, hybrid polling is enabled. Hence poll_nsec can be: 3791 * 3792 * 0: use half of prev avg 3793 * >0: use this specific value 3794 */ 3795 if (q->poll_nsec > 0) 3796 nsecs = q->poll_nsec; 3797 else 3798 nsecs = blk_mq_poll_nsecs(q, rq); 3799 3800 if (!nsecs) 3801 return false; 3802 3803 rq->rq_flags |= RQF_MQ_POLL_SLEPT; 3804 3805 /* 3806 * This will be replaced with the stats tracking code, using 3807 * 'avg_completion_time / 2' as the pre-sleep target. 3808 */ 3809 kt = nsecs; 3810 3811 mode = HRTIMER_MODE_REL; 3812 hrtimer_init_sleeper_on_stack(&hs, CLOCK_MONOTONIC, mode); 3813 hrtimer_set_expires(&hs.timer, kt); 3814 3815 do { 3816 if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE) 3817 break; 3818 set_current_state(TASK_UNINTERRUPTIBLE); 3819 hrtimer_sleeper_start_expires(&hs, mode); 3820 if (hs.task) 3821 io_schedule(); 3822 hrtimer_cancel(&hs.timer); 3823 mode = HRTIMER_MODE_ABS; 3824 } while (hs.task && !signal_pending(current)); 3825 3826 __set_current_state(TASK_RUNNING); 3827 destroy_hrtimer_on_stack(&hs.timer); 3828 return true; 3829 } 3830 3831 static bool blk_mq_poll_hybrid(struct request_queue *q, 3832 struct blk_mq_hw_ctx *hctx, blk_qc_t cookie) 3833 { 3834 struct request *rq; 3835 3836 if (q->poll_nsec == BLK_MQ_POLL_CLASSIC) 3837 return false; 3838 3839 if (!blk_qc_t_is_internal(cookie)) 3840 rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie)); 3841 else { 3842 rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie)); 3843 /* 3844 * With scheduling, if the request has completed, we'll 3845 * get a NULL return here, as we clear the sched tag when 3846 * that happens. The request still remains valid, like always, 3847 * so we should be safe with just the NULL check. 3848 */ 3849 if (!rq) 3850 return false; 3851 } 3852 3853 return blk_mq_poll_hybrid_sleep(q, rq); 3854 } 3855 3856 /** 3857 * blk_poll - poll for IO completions 3858 * @q: the queue 3859 * @cookie: cookie passed back at IO submission time 3860 * @spin: whether to spin for completions 3861 * 3862 * Description: 3863 * Poll for completions on the passed in queue. Returns number of 3864 * completed entries found. If @spin is true, then blk_poll will continue 3865 * looping until at least one completion is found, unless the task is 3866 * otherwise marked running (or we need to reschedule). 3867 */ 3868 int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin) 3869 { 3870 struct blk_mq_hw_ctx *hctx; 3871 long state; 3872 3873 if (!blk_qc_t_valid(cookie) || 3874 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags)) 3875 return 0; 3876 3877 if (current->plug) 3878 blk_flush_plug_list(current->plug, false); 3879 3880 hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)]; 3881 3882 /* 3883 * If we sleep, have the caller restart the poll loop to reset 3884 * the state. Like for the other success return cases, the 3885 * caller is responsible for checking if the IO completed. If 3886 * the IO isn't complete, we'll get called again and will go 3887 * straight to the busy poll loop. If specified not to spin, 3888 * we also should not sleep. 3889 */ 3890 if (spin && blk_mq_poll_hybrid(q, hctx, cookie)) 3891 return 1; 3892 3893 hctx->poll_considered++; 3894 3895 state = current->state; 3896 do { 3897 int ret; 3898 3899 hctx->poll_invoked++; 3900 3901 ret = q->mq_ops->poll(hctx); 3902 if (ret > 0) { 3903 hctx->poll_success++; 3904 __set_current_state(TASK_RUNNING); 3905 return ret; 3906 } 3907 3908 if (signal_pending_state(state, current)) 3909 __set_current_state(TASK_RUNNING); 3910 3911 if (current->state == TASK_RUNNING) 3912 return 1; 3913 if (ret < 0 || !spin) 3914 break; 3915 cpu_relax(); 3916 } while (!need_resched()); 3917 3918 __set_current_state(TASK_RUNNING); 3919 return 0; 3920 } 3921 EXPORT_SYMBOL_GPL(blk_poll); 3922 3923 unsigned int blk_mq_rq_cpu(struct request *rq) 3924 { 3925 return rq->mq_ctx->cpu; 3926 } 3927 EXPORT_SYMBOL(blk_mq_rq_cpu); 3928 3929 static int __init blk_mq_init(void) 3930 { 3931 int i; 3932 3933 for_each_possible_cpu(i) 3934 init_llist_head(&per_cpu(blk_cpu_done, i)); 3935 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq); 3936 3937 cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD, 3938 "block/softirq:dead", NULL, 3939 blk_softirq_cpu_dead); 3940 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL, 3941 blk_mq_hctx_notify_dead); 3942 cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online", 3943 blk_mq_hctx_notify_online, 3944 blk_mq_hctx_notify_offline); 3945 return 0; 3946 } 3947 subsys_initcall(blk_mq_init); 3948