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/blk-integrity.h> 14 #include <linux/kmemleak.h> 15 #include <linux/mm.h> 16 #include <linux/init.h> 17 #include <linux/slab.h> 18 #include <linux/workqueue.h> 19 #include <linux/smp.h> 20 #include <linux/interrupt.h> 21 #include <linux/llist.h> 22 #include <linux/cpu.h> 23 #include <linux/cache.h> 24 #include <linux/sched/topology.h> 25 #include <linux/sched/signal.h> 26 #include <linux/suspend.h> 27 #include <linux/delay.h> 28 #include <linux/crash_dump.h> 29 #include <linux/prefetch.h> 30 #include <linux/blk-crypto.h> 31 #include <linux/part_stat.h> 32 #include <linux/sched/isolation.h> 33 34 #include <trace/events/block.h> 35 36 #include <linux/t10-pi.h> 37 #include "blk.h" 38 #include "blk-mq.h" 39 #include "blk-mq-debugfs.h" 40 #include "blk-pm.h" 41 #include "blk-stat.h" 42 #include "blk-mq-sched.h" 43 #include "blk-rq-qos.h" 44 45 static DEFINE_PER_CPU(struct llist_head, blk_cpu_done); 46 static DEFINE_PER_CPU(call_single_data_t, blk_cpu_csd); 47 static DEFINE_MUTEX(blk_mq_cpuhp_lock); 48 49 static void blk_mq_insert_request(struct request *rq, blk_insert_t flags); 50 static void blk_mq_request_bypass_insert(struct request *rq, 51 blk_insert_t flags); 52 static void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx, 53 struct list_head *list); 54 static int blk_hctx_poll(struct request_queue *q, struct blk_mq_hw_ctx *hctx, 55 struct io_comp_batch *iob, unsigned int flags); 56 57 /* 58 * Check if any of the ctx, dispatch list or elevator 59 * have pending work in this hardware queue. 60 */ 61 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx) 62 { 63 return !list_empty_careful(&hctx->dispatch) || 64 sbitmap_any_bit_set(&hctx->ctx_map) || 65 blk_mq_sched_has_work(hctx); 66 } 67 68 /* 69 * Mark this ctx as having pending work in this hardware queue 70 */ 71 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx, 72 struct blk_mq_ctx *ctx) 73 { 74 const int bit = ctx->index_hw[hctx->type]; 75 76 if (!sbitmap_test_bit(&hctx->ctx_map, bit)) 77 sbitmap_set_bit(&hctx->ctx_map, bit); 78 } 79 80 static void blk_mq_hctx_clear_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 sbitmap_clear_bit(&hctx->ctx_map, bit); 86 } 87 88 struct mq_inflight { 89 struct block_device *part; 90 unsigned int inflight[2]; 91 }; 92 93 static bool blk_mq_check_in_driver(struct request *rq, void *priv) 94 { 95 struct mq_inflight *mi = priv; 96 97 if (rq->rq_flags & RQF_IO_STAT && 98 (!bdev_is_partition(mi->part) || rq->part == mi->part) && 99 blk_mq_rq_state(rq) == MQ_RQ_IN_FLIGHT) 100 mi->inflight[rq_data_dir(rq)]++; 101 102 return true; 103 } 104 105 void blk_mq_in_driver_rw(struct block_device *part, unsigned int inflight[2]) 106 { 107 struct mq_inflight mi = { .part = part }; 108 109 blk_mq_queue_tag_busy_iter(bdev_get_queue(part), blk_mq_check_in_driver, 110 &mi); 111 inflight[READ] = mi.inflight[READ]; 112 inflight[WRITE] = mi.inflight[WRITE]; 113 } 114 115 #ifdef CONFIG_LOCKDEP 116 static bool blk_freeze_set_owner(struct request_queue *q, 117 struct task_struct *owner) 118 { 119 if (!owner) 120 return false; 121 122 if (!q->mq_freeze_depth) { 123 q->mq_freeze_owner = owner; 124 q->mq_freeze_owner_depth = 1; 125 q->mq_freeze_disk_dead = !q->disk || 126 test_bit(GD_DEAD, &q->disk->state) || 127 !blk_queue_registered(q); 128 q->mq_freeze_queue_dying = blk_queue_dying(q); 129 return true; 130 } 131 132 if (owner == q->mq_freeze_owner) 133 q->mq_freeze_owner_depth += 1; 134 return false; 135 } 136 137 /* verify the last unfreeze in owner context */ 138 static bool blk_unfreeze_check_owner(struct request_queue *q) 139 { 140 if (q->mq_freeze_owner != current) 141 return false; 142 if (--q->mq_freeze_owner_depth == 0) { 143 q->mq_freeze_owner = NULL; 144 return true; 145 } 146 return false; 147 } 148 149 #else 150 151 static bool blk_freeze_set_owner(struct request_queue *q, 152 struct task_struct *owner) 153 { 154 return false; 155 } 156 157 static bool blk_unfreeze_check_owner(struct request_queue *q) 158 { 159 return false; 160 } 161 #endif 162 163 bool __blk_freeze_queue_start(struct request_queue *q, 164 struct task_struct *owner) 165 { 166 bool freeze; 167 168 mutex_lock(&q->mq_freeze_lock); 169 freeze = blk_freeze_set_owner(q, owner); 170 if (++q->mq_freeze_depth == 1) { 171 percpu_ref_kill(&q->q_usage_counter); 172 mutex_unlock(&q->mq_freeze_lock); 173 if (queue_is_mq(q)) 174 blk_mq_run_hw_queues(q, false); 175 } else { 176 mutex_unlock(&q->mq_freeze_lock); 177 } 178 179 return freeze; 180 } 181 182 void blk_freeze_queue_start(struct request_queue *q) 183 { 184 if (__blk_freeze_queue_start(q, current)) 185 blk_freeze_acquire_lock(q); 186 } 187 EXPORT_SYMBOL_GPL(blk_freeze_queue_start); 188 189 void blk_mq_freeze_queue_wait(struct request_queue *q) 190 { 191 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter)); 192 } 193 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait); 194 195 int blk_mq_freeze_queue_wait_timeout(struct request_queue *q, 196 unsigned long timeout) 197 { 198 return wait_event_timeout(q->mq_freeze_wq, 199 percpu_ref_is_zero(&q->q_usage_counter), 200 timeout); 201 } 202 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout); 203 204 void blk_mq_freeze_queue_nomemsave(struct request_queue *q) 205 { 206 blk_freeze_queue_start(q); 207 blk_mq_freeze_queue_wait(q); 208 } 209 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_nomemsave); 210 211 bool __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic) 212 { 213 bool unfreeze; 214 215 mutex_lock(&q->mq_freeze_lock); 216 if (force_atomic) 217 q->q_usage_counter.data->force_atomic = true; 218 q->mq_freeze_depth--; 219 WARN_ON_ONCE(q->mq_freeze_depth < 0); 220 if (!q->mq_freeze_depth) { 221 percpu_ref_resurrect(&q->q_usage_counter); 222 wake_up_all(&q->mq_freeze_wq); 223 } 224 unfreeze = blk_unfreeze_check_owner(q); 225 mutex_unlock(&q->mq_freeze_lock); 226 227 return unfreeze; 228 } 229 230 void blk_mq_unfreeze_queue_nomemrestore(struct request_queue *q) 231 { 232 if (__blk_mq_unfreeze_queue(q, false)) 233 blk_unfreeze_release_lock(q); 234 } 235 EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue_nomemrestore); 236 237 /* 238 * non_owner variant of blk_freeze_queue_start 239 * 240 * Unlike blk_freeze_queue_start, the queue doesn't need to be unfrozen 241 * by the same task. This is fragile and should not be used if at all 242 * possible. 243 */ 244 void blk_freeze_queue_start_non_owner(struct request_queue *q) 245 { 246 __blk_freeze_queue_start(q, NULL); 247 } 248 EXPORT_SYMBOL_GPL(blk_freeze_queue_start_non_owner); 249 250 /* non_owner variant of blk_mq_unfreeze_queue */ 251 void blk_mq_unfreeze_queue_non_owner(struct request_queue *q) 252 { 253 __blk_mq_unfreeze_queue(q, false); 254 } 255 EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue_non_owner); 256 257 /* 258 * FIXME: replace the scsi_internal_device_*block_nowait() calls in the 259 * mpt3sas driver such that this function can be removed. 260 */ 261 void blk_mq_quiesce_queue_nowait(struct request_queue *q) 262 { 263 unsigned long flags; 264 265 spin_lock_irqsave(&q->queue_lock, flags); 266 if (!q->quiesce_depth++) 267 blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q); 268 spin_unlock_irqrestore(&q->queue_lock, flags); 269 } 270 EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait); 271 272 /** 273 * blk_mq_wait_quiesce_done() - wait until in-progress quiesce is done 274 * @set: tag_set to wait on 275 * 276 * Note: it is driver's responsibility for making sure that quiesce has 277 * been started on or more of the request_queues of the tag_set. This 278 * function only waits for the quiesce on those request_queues that had 279 * the quiesce flag set using blk_mq_quiesce_queue_nowait. 280 */ 281 void blk_mq_wait_quiesce_done(struct blk_mq_tag_set *set) 282 { 283 if (set->flags & BLK_MQ_F_BLOCKING) 284 synchronize_srcu(set->srcu); 285 else 286 synchronize_rcu(); 287 } 288 EXPORT_SYMBOL_GPL(blk_mq_wait_quiesce_done); 289 290 /** 291 * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished 292 * @q: request queue. 293 * 294 * Note: this function does not prevent that the struct request end_io() 295 * callback function is invoked. Once this function is returned, we make 296 * sure no dispatch can happen until the queue is unquiesced via 297 * blk_mq_unquiesce_queue(). 298 */ 299 void blk_mq_quiesce_queue(struct request_queue *q) 300 { 301 blk_mq_quiesce_queue_nowait(q); 302 /* nothing to wait for non-mq queues */ 303 if (queue_is_mq(q)) 304 blk_mq_wait_quiesce_done(q->tag_set); 305 } 306 EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue); 307 308 /* 309 * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue() 310 * @q: request queue. 311 * 312 * This function recovers queue into the state before quiescing 313 * which is done by blk_mq_quiesce_queue. 314 */ 315 void blk_mq_unquiesce_queue(struct request_queue *q) 316 { 317 unsigned long flags; 318 bool run_queue = false; 319 320 spin_lock_irqsave(&q->queue_lock, flags); 321 if (WARN_ON_ONCE(q->quiesce_depth <= 0)) { 322 ; 323 } else if (!--q->quiesce_depth) { 324 blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q); 325 run_queue = true; 326 } 327 spin_unlock_irqrestore(&q->queue_lock, flags); 328 329 /* dispatch requests which are inserted during quiescing */ 330 if (run_queue) 331 blk_mq_run_hw_queues(q, true); 332 } 333 EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue); 334 335 void blk_mq_quiesce_tagset(struct blk_mq_tag_set *set) 336 { 337 struct request_queue *q; 338 339 rcu_read_lock(); 340 list_for_each_entry_rcu(q, &set->tag_list, tag_set_list) { 341 if (!blk_queue_skip_tagset_quiesce(q)) 342 blk_mq_quiesce_queue_nowait(q); 343 } 344 rcu_read_unlock(); 345 346 blk_mq_wait_quiesce_done(set); 347 } 348 EXPORT_SYMBOL_GPL(blk_mq_quiesce_tagset); 349 350 void blk_mq_unquiesce_tagset(struct blk_mq_tag_set *set) 351 { 352 struct request_queue *q; 353 354 rcu_read_lock(); 355 list_for_each_entry_rcu(q, &set->tag_list, tag_set_list) { 356 if (!blk_queue_skip_tagset_quiesce(q)) 357 blk_mq_unquiesce_queue(q); 358 } 359 rcu_read_unlock(); 360 } 361 EXPORT_SYMBOL_GPL(blk_mq_unquiesce_tagset); 362 363 void blk_mq_wake_waiters(struct request_queue *q) 364 { 365 struct blk_mq_hw_ctx *hctx; 366 unsigned long i; 367 368 queue_for_each_hw_ctx(q, hctx, i) 369 if (blk_mq_hw_queue_mapped(hctx)) 370 blk_mq_tag_wakeup_all(hctx->tags, true); 371 } 372 373 void blk_rq_init(struct request_queue *q, struct request *rq) 374 { 375 memset(rq, 0, sizeof(*rq)); 376 377 INIT_LIST_HEAD(&rq->queuelist); 378 rq->q = q; 379 rq->__sector = (sector_t) -1; 380 rq->phys_gap_bit = 0; 381 INIT_HLIST_NODE(&rq->hash); 382 RB_CLEAR_NODE(&rq->rb_node); 383 rq->tag = BLK_MQ_NO_TAG; 384 rq->internal_tag = BLK_MQ_NO_TAG; 385 rq->start_time_ns = blk_time_get_ns(); 386 blk_crypto_rq_set_defaults(rq); 387 } 388 EXPORT_SYMBOL(blk_rq_init); 389 390 /* Set start and alloc time when the allocated request is actually used */ 391 static inline void blk_mq_rq_time_init(struct request *rq, u64 alloc_time_ns) 392 { 393 #ifdef CONFIG_BLK_RQ_ALLOC_TIME 394 if (blk_queue_rq_alloc_time(rq->q)) 395 rq->alloc_time_ns = alloc_time_ns; 396 else 397 rq->alloc_time_ns = 0; 398 #endif 399 } 400 401 static inline void blk_mq_bio_issue_init(struct request_queue *q, 402 struct bio *bio) 403 { 404 #ifdef CONFIG_BLK_CGROUP 405 if (test_bit(QUEUE_FLAG_BIO_ISSUE_TIME, &q->queue_flags)) 406 bio->issue_time_ns = blk_time_get_ns(); 407 #endif 408 } 409 410 static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data, 411 struct blk_mq_tags *tags, unsigned int tag) 412 { 413 struct blk_mq_ctx *ctx = data->ctx; 414 struct blk_mq_hw_ctx *hctx = data->hctx; 415 struct request_queue *q = data->q; 416 struct request *rq = tags->static_rqs[tag]; 417 418 rq->q = q; 419 rq->mq_ctx = ctx; 420 rq->mq_hctx = hctx; 421 rq->cmd_flags = data->cmd_flags; 422 423 if (data->flags & BLK_MQ_REQ_PM) 424 data->rq_flags |= RQF_PM; 425 rq->rq_flags = data->rq_flags; 426 427 if (data->rq_flags & RQF_SCHED_TAGS) { 428 rq->tag = BLK_MQ_NO_TAG; 429 rq->internal_tag = tag; 430 } else { 431 rq->tag = tag; 432 rq->internal_tag = BLK_MQ_NO_TAG; 433 } 434 rq->timeout = 0; 435 436 rq->part = NULL; 437 rq->io_start_time_ns = 0; 438 rq->stats_sectors = 0; 439 rq->nr_phys_segments = 0; 440 rq->nr_integrity_segments = 0; 441 rq->end_io = NULL; 442 rq->end_io_data = NULL; 443 444 blk_crypto_rq_set_defaults(rq); 445 INIT_LIST_HEAD(&rq->queuelist); 446 /* tag was already set */ 447 WRITE_ONCE(rq->deadline, 0); 448 req_ref_set(rq, 1); 449 450 if (rq->rq_flags & RQF_USE_SCHED) { 451 struct elevator_queue *e = data->q->elevator; 452 453 INIT_HLIST_NODE(&rq->hash); 454 RB_CLEAR_NODE(&rq->rb_node); 455 456 if (e->type->ops.prepare_request) 457 e->type->ops.prepare_request(rq); 458 } 459 460 return rq; 461 } 462 463 static inline struct request * 464 __blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data) 465 { 466 unsigned int tag, tag_offset; 467 struct blk_mq_tags *tags; 468 struct request *rq; 469 unsigned long tag_mask; 470 int i, nr = 0; 471 472 do { 473 tag_mask = blk_mq_get_tags(data, data->nr_tags - nr, &tag_offset); 474 if (unlikely(!tag_mask)) { 475 if (nr == 0) 476 return NULL; 477 break; 478 } 479 tags = blk_mq_tags_from_data(data); 480 for (i = 0; tag_mask; i++) { 481 if (!(tag_mask & (1UL << i))) 482 continue; 483 tag = tag_offset + i; 484 prefetch(tags->static_rqs[tag]); 485 tag_mask &= ~(1UL << i); 486 rq = blk_mq_rq_ctx_init(data, tags, tag); 487 rq_list_add_head(data->cached_rqs, rq); 488 nr++; 489 } 490 } while (data->nr_tags > nr); 491 492 if (!(data->rq_flags & RQF_SCHED_TAGS)) 493 blk_mq_add_active_requests(data->hctx, nr); 494 /* caller already holds a reference, add for remainder */ 495 percpu_ref_get_many(&data->q->q_usage_counter, nr - 1); 496 data->nr_tags -= nr; 497 498 return rq_list_pop(data->cached_rqs); 499 } 500 501 static void blk_mq_limit_depth(struct blk_mq_alloc_data *data) 502 { 503 struct elevator_mq_ops *ops; 504 505 /* If no I/O scheduler has been configured, don't limit requests */ 506 if (!data->q->elevator) { 507 blk_mq_tag_busy(data->hctx); 508 return; 509 } 510 511 /* 512 * All requests use scheduler tags when an I/O scheduler is 513 * enabled for the queue. 514 */ 515 data->rq_flags |= RQF_SCHED_TAGS; 516 517 /* 518 * Flush/passthrough requests are special and go directly to the 519 * dispatch list, they are not subject to the async_depth limit. 520 */ 521 if ((data->cmd_flags & REQ_OP_MASK) == REQ_OP_FLUSH || 522 blk_op_is_passthrough(data->cmd_flags)) 523 return; 524 525 WARN_ON_ONCE(data->flags & BLK_MQ_REQ_RESERVED); 526 data->rq_flags |= RQF_USE_SCHED; 527 528 /* 529 * By default, sync requests have no limit, and async requests are 530 * limited to async_depth. 531 */ 532 ops = &data->q->elevator->type->ops; 533 if (ops->limit_depth) 534 ops->limit_depth(data->cmd_flags, data); 535 } 536 537 static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data) 538 { 539 struct request_queue *q = data->q; 540 u64 alloc_time_ns = 0; 541 struct request *rq; 542 unsigned int tag; 543 544 /* alloc_time includes depth and tag waits */ 545 if (blk_queue_rq_alloc_time(q)) 546 alloc_time_ns = blk_time_get_ns(); 547 548 if (data->cmd_flags & REQ_NOWAIT) 549 data->flags |= BLK_MQ_REQ_NOWAIT; 550 551 retry: 552 data->ctx = blk_mq_get_ctx(q); 553 data->hctx = blk_mq_map_queue(data->cmd_flags, data->ctx); 554 555 blk_mq_limit_depth(data); 556 if (data->flags & BLK_MQ_REQ_RESERVED) 557 data->rq_flags |= RQF_RESV; 558 559 /* 560 * Try batched alloc if we want more than 1 tag. 561 */ 562 if (data->nr_tags > 1) { 563 rq = __blk_mq_alloc_requests_batch(data); 564 if (rq) { 565 blk_mq_rq_time_init(rq, alloc_time_ns); 566 return rq; 567 } 568 data->nr_tags = 1; 569 } 570 571 /* 572 * Waiting allocations only fail because of an inactive hctx. In that 573 * case just retry the hctx assignment and tag allocation as CPU hotplug 574 * should have migrated us to an online CPU by now. 575 */ 576 tag = blk_mq_get_tag(data); 577 if (tag == BLK_MQ_NO_TAG) { 578 if (data->flags & BLK_MQ_REQ_NOWAIT) 579 return NULL; 580 /* 581 * Give up the CPU and sleep for a random short time to 582 * ensure that thread using a realtime scheduling class 583 * are migrated off the CPU, and thus off the hctx that 584 * is going away. 585 */ 586 msleep(3); 587 goto retry; 588 } 589 590 if (!(data->rq_flags & RQF_SCHED_TAGS)) 591 blk_mq_inc_active_requests(data->hctx); 592 rq = blk_mq_rq_ctx_init(data, blk_mq_tags_from_data(data), tag); 593 blk_mq_rq_time_init(rq, alloc_time_ns); 594 return rq; 595 } 596 597 static struct request *blk_mq_rq_cache_fill(struct request_queue *q, 598 struct blk_plug *plug, 599 blk_opf_t opf, 600 blk_mq_req_flags_t flags) 601 { 602 struct blk_mq_alloc_data data = { 603 .q = q, 604 .flags = flags, 605 .shallow_depth = 0, 606 .cmd_flags = opf, 607 .rq_flags = 0, 608 .nr_tags = plug->nr_ios, 609 .cached_rqs = &plug->cached_rqs, 610 .ctx = NULL, 611 .hctx = NULL 612 }; 613 struct request *rq; 614 615 if (blk_queue_enter(q, flags)) 616 return NULL; 617 618 plug->nr_ios = 1; 619 620 rq = __blk_mq_alloc_requests(&data); 621 if (unlikely(!rq)) 622 blk_queue_exit(q); 623 return rq; 624 } 625 626 static struct request *blk_mq_alloc_cached_request(struct request_queue *q, 627 blk_opf_t opf, 628 blk_mq_req_flags_t flags) 629 { 630 struct blk_plug *plug = current->plug; 631 struct request *rq; 632 633 if (!plug) 634 return NULL; 635 636 if (rq_list_empty(&plug->cached_rqs)) { 637 if (plug->nr_ios == 1) 638 return NULL; 639 rq = blk_mq_rq_cache_fill(q, plug, opf, flags); 640 if (!rq) 641 return NULL; 642 } else { 643 rq = rq_list_peek(&plug->cached_rqs); 644 if (!rq || rq->q != q) 645 return NULL; 646 647 if (blk_mq_get_hctx_type(opf) != rq->mq_hctx->type) 648 return NULL; 649 if (op_is_flush(rq->cmd_flags) != op_is_flush(opf)) 650 return NULL; 651 652 rq_list_pop(&plug->cached_rqs); 653 blk_mq_rq_time_init(rq, blk_time_get_ns()); 654 } 655 656 rq->cmd_flags = opf; 657 INIT_LIST_HEAD(&rq->queuelist); 658 return rq; 659 } 660 661 struct request *blk_mq_alloc_request(struct request_queue *q, blk_opf_t opf, 662 blk_mq_req_flags_t flags) 663 { 664 struct request *rq; 665 666 rq = blk_mq_alloc_cached_request(q, opf, flags); 667 if (!rq) { 668 struct blk_mq_alloc_data data = { 669 .q = q, 670 .flags = flags, 671 .shallow_depth = 0, 672 .cmd_flags = opf, 673 .rq_flags = 0, 674 .nr_tags = 1, 675 .cached_rqs = NULL, 676 .ctx = NULL, 677 .hctx = NULL 678 }; 679 int ret; 680 681 ret = blk_queue_enter(q, flags); 682 if (ret) 683 return ERR_PTR(ret); 684 685 rq = __blk_mq_alloc_requests(&data); 686 if (!rq) 687 goto out_queue_exit; 688 } 689 rq->__data_len = 0; 690 rq->phys_gap_bit = 0; 691 rq->__sector = (sector_t) -1; 692 rq->bio = rq->biotail = NULL; 693 return rq; 694 out_queue_exit: 695 blk_queue_exit(q); 696 return ERR_PTR(-EWOULDBLOCK); 697 } 698 EXPORT_SYMBOL(blk_mq_alloc_request); 699 700 struct request *blk_mq_alloc_request_hctx(struct request_queue *q, 701 blk_opf_t opf, blk_mq_req_flags_t flags, unsigned int hctx_idx) 702 { 703 struct blk_mq_alloc_data data = { 704 .q = q, 705 .flags = flags, 706 .shallow_depth = 0, 707 .cmd_flags = opf, 708 .rq_flags = 0, 709 .nr_tags = 1, 710 .cached_rqs = NULL, 711 .ctx = NULL, 712 .hctx = NULL 713 }; 714 u64 alloc_time_ns = 0; 715 struct request *rq; 716 unsigned int cpu; 717 unsigned int tag; 718 int ret; 719 720 /* alloc_time includes depth and tag waits */ 721 if (blk_queue_rq_alloc_time(q)) 722 alloc_time_ns = blk_time_get_ns(); 723 724 /* 725 * If the tag allocator sleeps we could get an allocation for a 726 * different hardware context. No need to complicate the low level 727 * allocator for this for the rare use case of a command tied to 728 * a specific queue. 729 */ 730 if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)) || 731 WARN_ON_ONCE(!(flags & BLK_MQ_REQ_RESERVED))) 732 return ERR_PTR(-EINVAL); 733 734 if (hctx_idx >= q->nr_hw_queues) 735 return ERR_PTR(-EIO); 736 737 ret = blk_queue_enter(q, flags); 738 if (ret) 739 return ERR_PTR(ret); 740 741 /* 742 * Check if the hardware context is actually mapped to anything. 743 * If not tell the caller that it should skip this queue. 744 */ 745 ret = -EXDEV; 746 data.hctx = q->queue_hw_ctx[hctx_idx]; 747 if (!blk_mq_hw_queue_mapped(data.hctx)) 748 goto out_queue_exit; 749 cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask); 750 if (cpu >= nr_cpu_ids) 751 goto out_queue_exit; 752 data.ctx = __blk_mq_get_ctx(q, cpu); 753 754 if (q->elevator) 755 data.rq_flags |= RQF_SCHED_TAGS; 756 else 757 blk_mq_tag_busy(data.hctx); 758 759 if (flags & BLK_MQ_REQ_RESERVED) 760 data.rq_flags |= RQF_RESV; 761 762 ret = -EWOULDBLOCK; 763 tag = blk_mq_get_tag(&data); 764 if (tag == BLK_MQ_NO_TAG) 765 goto out_queue_exit; 766 if (!(data.rq_flags & RQF_SCHED_TAGS)) 767 blk_mq_inc_active_requests(data.hctx); 768 rq = blk_mq_rq_ctx_init(&data, blk_mq_tags_from_data(&data), tag); 769 blk_mq_rq_time_init(rq, alloc_time_ns); 770 rq->__data_len = 0; 771 rq->phys_gap_bit = 0; 772 rq->__sector = (sector_t) -1; 773 rq->bio = rq->biotail = NULL; 774 return rq; 775 776 out_queue_exit: 777 blk_queue_exit(q); 778 return ERR_PTR(ret); 779 } 780 EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx); 781 782 static void blk_mq_finish_request(struct request *rq) 783 { 784 struct request_queue *q = rq->q; 785 786 blk_zone_finish_request(rq); 787 788 if (rq->rq_flags & RQF_USE_SCHED) { 789 q->elevator->type->ops.finish_request(rq); 790 /* 791 * For postflush request that may need to be 792 * completed twice, we should clear this flag 793 * to avoid double finish_request() on the rq. 794 */ 795 rq->rq_flags &= ~RQF_USE_SCHED; 796 } 797 } 798 799 static void __blk_mq_free_request(struct request *rq) 800 { 801 struct request_queue *q = rq->q; 802 struct blk_mq_ctx *ctx = rq->mq_ctx; 803 struct blk_mq_hw_ctx *hctx = rq->mq_hctx; 804 const int sched_tag = rq->internal_tag; 805 806 blk_crypto_free_request(rq); 807 blk_pm_mark_last_busy(rq); 808 rq->mq_hctx = NULL; 809 810 if (rq->tag != BLK_MQ_NO_TAG) { 811 blk_mq_dec_active_requests(hctx); 812 blk_mq_put_tag(hctx->tags, ctx, rq->tag); 813 } 814 if (sched_tag != BLK_MQ_NO_TAG) 815 blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag); 816 blk_mq_sched_restart(hctx); 817 blk_queue_exit(q); 818 } 819 820 void blk_mq_free_request(struct request *rq) 821 { 822 struct request_queue *q = rq->q; 823 824 blk_mq_finish_request(rq); 825 826 if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq))) 827 laptop_io_completion(q->disk->bdi); 828 829 rq_qos_done(q, rq); 830 831 WRITE_ONCE(rq->state, MQ_RQ_IDLE); 832 if (req_ref_put_and_test(rq)) 833 __blk_mq_free_request(rq); 834 } 835 EXPORT_SYMBOL_GPL(blk_mq_free_request); 836 837 void blk_mq_free_plug_rqs(struct blk_plug *plug) 838 { 839 struct request *rq; 840 841 while ((rq = rq_list_pop(&plug->cached_rqs)) != NULL) 842 blk_mq_free_request(rq); 843 } 844 845 void blk_dump_rq_flags(struct request *rq, char *msg) 846 { 847 printk(KERN_INFO "%s: dev %s: flags=%llx\n", msg, 848 rq->q->disk ? rq->q->disk->disk_name : "?", 849 (__force unsigned long long) rq->cmd_flags); 850 851 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n", 852 (unsigned long long)blk_rq_pos(rq), 853 blk_rq_sectors(rq), blk_rq_cur_sectors(rq)); 854 printk(KERN_INFO " bio %p, biotail %p, len %u\n", 855 rq->bio, rq->biotail, blk_rq_bytes(rq)); 856 } 857 EXPORT_SYMBOL(blk_dump_rq_flags); 858 859 static void blk_account_io_completion(struct request *req, unsigned int bytes) 860 { 861 if (req->rq_flags & RQF_IO_STAT) { 862 const int sgrp = op_stat_group(req_op(req)); 863 864 part_stat_lock(); 865 part_stat_add(req->part, sectors[sgrp], bytes >> 9); 866 part_stat_unlock(); 867 } 868 } 869 870 static void blk_print_req_error(struct request *req, blk_status_t status) 871 { 872 printk_ratelimited(KERN_ERR 873 "%s error, dev %s, sector %llu op 0x%x:(%s) flags 0x%x " 874 "phys_seg %u prio class %u\n", 875 blk_status_to_str(status), 876 req->q->disk ? req->q->disk->disk_name : "?", 877 blk_rq_pos(req), (__force u32)req_op(req), 878 blk_op_str(req_op(req)), 879 (__force u32)(req->cmd_flags & ~REQ_OP_MASK), 880 req->nr_phys_segments, 881 IOPRIO_PRIO_CLASS(req_get_ioprio(req))); 882 } 883 884 /* 885 * Fully end IO on a request. Does not support partial completions, or 886 * errors. 887 */ 888 static void blk_complete_request(struct request *req) 889 { 890 const bool is_flush = (req->rq_flags & RQF_FLUSH_SEQ) != 0; 891 int total_bytes = blk_rq_bytes(req); 892 struct bio *bio = req->bio; 893 894 trace_block_rq_complete(req, BLK_STS_OK, total_bytes); 895 896 if (!bio) 897 return; 898 899 if (blk_integrity_rq(req) && req_op(req) == REQ_OP_READ) 900 blk_integrity_complete(req, total_bytes); 901 902 /* 903 * Upper layers may call blk_crypto_evict_key() anytime after the last 904 * bio_endio(). Therefore, the keyslot must be released before that. 905 */ 906 blk_crypto_rq_put_keyslot(req); 907 908 blk_account_io_completion(req, total_bytes); 909 910 do { 911 struct bio *next = bio->bi_next; 912 913 /* Completion has already been traced */ 914 bio_clear_flag(bio, BIO_TRACE_COMPLETION); 915 916 if (blk_req_bio_is_zone_append(req, bio)) 917 blk_zone_append_update_request_bio(req, bio); 918 919 if (!is_flush) 920 bio_endio(bio); 921 bio = next; 922 } while (bio); 923 924 /* 925 * Reset counters so that the request stacking driver 926 * can find how many bytes remain in the request 927 * later. 928 */ 929 if (!req->end_io) { 930 req->bio = NULL; 931 req->__data_len = 0; 932 } 933 } 934 935 /** 936 * blk_update_request - Complete multiple bytes without completing the request 937 * @req: the request being processed 938 * @error: block status code 939 * @nr_bytes: number of bytes to complete for @req 940 * 941 * Description: 942 * Ends I/O on a number of bytes attached to @req, but doesn't complete 943 * the request structure even if @req doesn't have leftover. 944 * If @req has leftover, sets it up for the next range of segments. 945 * 946 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees 947 * %false return from this function. 948 * 949 * Note: 950 * The RQF_SPECIAL_PAYLOAD flag is ignored on purpose in this function 951 * except in the consistency check at the end of this function. 952 * 953 * Return: 954 * %false - this request doesn't have any more data 955 * %true - this request has more data 956 **/ 957 bool blk_update_request(struct request *req, blk_status_t error, 958 unsigned int nr_bytes) 959 { 960 bool is_flush = req->rq_flags & RQF_FLUSH_SEQ; 961 bool quiet = req->rq_flags & RQF_QUIET; 962 int total_bytes; 963 964 trace_block_rq_complete(req, error, nr_bytes); 965 966 if (!req->bio) 967 return false; 968 969 if (blk_integrity_rq(req) && req_op(req) == REQ_OP_READ && 970 error == BLK_STS_OK) 971 blk_integrity_complete(req, nr_bytes); 972 973 /* 974 * Upper layers may call blk_crypto_evict_key() anytime after the last 975 * bio_endio(). Therefore, the keyslot must be released before that. 976 */ 977 if (blk_crypto_rq_has_keyslot(req) && nr_bytes >= blk_rq_bytes(req)) 978 __blk_crypto_rq_put_keyslot(req); 979 980 if (unlikely(error && !blk_rq_is_passthrough(req) && !quiet) && 981 !test_bit(GD_DEAD, &req->q->disk->state)) { 982 blk_print_req_error(req, error); 983 trace_block_rq_error(req, error, nr_bytes); 984 } 985 986 blk_account_io_completion(req, nr_bytes); 987 988 total_bytes = 0; 989 while (req->bio) { 990 struct bio *bio = req->bio; 991 unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes); 992 993 if (unlikely(error)) 994 bio->bi_status = error; 995 996 if (bio_bytes == bio->bi_iter.bi_size) { 997 req->bio = bio->bi_next; 998 } else if (bio_is_zone_append(bio) && error == BLK_STS_OK) { 999 /* 1000 * Partial zone append completions cannot be supported 1001 * as the BIO fragments may end up not being written 1002 * sequentially. 1003 */ 1004 bio->bi_status = BLK_STS_IOERR; 1005 } 1006 1007 /* Completion has already been traced */ 1008 bio_clear_flag(bio, BIO_TRACE_COMPLETION); 1009 if (unlikely(quiet)) 1010 bio_set_flag(bio, BIO_QUIET); 1011 1012 bio_advance(bio, bio_bytes); 1013 1014 /* Don't actually finish bio if it's part of flush sequence */ 1015 if (!bio->bi_iter.bi_size) { 1016 if (blk_req_bio_is_zone_append(req, bio)) 1017 blk_zone_append_update_request_bio(req, bio); 1018 if (!is_flush) 1019 bio_endio(bio); 1020 } 1021 1022 total_bytes += bio_bytes; 1023 nr_bytes -= bio_bytes; 1024 1025 if (!nr_bytes) 1026 break; 1027 } 1028 1029 /* 1030 * completely done 1031 */ 1032 if (!req->bio) { 1033 /* 1034 * Reset counters so that the request stacking driver 1035 * can find how many bytes remain in the request 1036 * later. 1037 */ 1038 req->__data_len = 0; 1039 return false; 1040 } 1041 1042 req->__data_len -= total_bytes; 1043 1044 /* update sector only for requests with clear definition of sector */ 1045 if (!blk_rq_is_passthrough(req)) 1046 req->__sector += total_bytes >> 9; 1047 1048 /* mixed attributes always follow the first bio */ 1049 if (req->rq_flags & RQF_MIXED_MERGE) { 1050 req->cmd_flags &= ~REQ_FAILFAST_MASK; 1051 req->cmd_flags |= req->bio->bi_opf & REQ_FAILFAST_MASK; 1052 } 1053 1054 if (!(req->rq_flags & RQF_SPECIAL_PAYLOAD)) { 1055 /* 1056 * If total number of sectors is less than the first segment 1057 * size, something has gone terribly wrong. 1058 */ 1059 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) { 1060 blk_dump_rq_flags(req, "request botched"); 1061 req->__data_len = blk_rq_cur_bytes(req); 1062 } 1063 1064 /* recalculate the number of segments */ 1065 req->nr_phys_segments = blk_recalc_rq_segments(req); 1066 } 1067 1068 return true; 1069 } 1070 EXPORT_SYMBOL_GPL(blk_update_request); 1071 1072 static inline void blk_account_io_done(struct request *req, u64 now) 1073 { 1074 trace_block_io_done(req); 1075 1076 /* 1077 * Account IO completion. flush_rq isn't accounted as a 1078 * normal IO on queueing nor completion. Accounting the 1079 * containing request is enough. 1080 */ 1081 if ((req->rq_flags & (RQF_IO_STAT|RQF_FLUSH_SEQ)) == RQF_IO_STAT) { 1082 const int sgrp = op_stat_group(req_op(req)); 1083 1084 part_stat_lock(); 1085 update_io_ticks(req->part, jiffies, true); 1086 part_stat_inc(req->part, ios[sgrp]); 1087 part_stat_add(req->part, nsecs[sgrp], now - req->start_time_ns); 1088 part_stat_local_dec(req->part, 1089 in_flight[op_is_write(req_op(req))]); 1090 part_stat_unlock(); 1091 } 1092 } 1093 1094 static inline bool blk_rq_passthrough_stats(struct request *req) 1095 { 1096 struct bio *bio = req->bio; 1097 1098 if (!blk_queue_passthrough_stat(req->q)) 1099 return false; 1100 1101 /* Requests without a bio do not transfer data. */ 1102 if (!bio) 1103 return false; 1104 1105 /* 1106 * Stats are accumulated in the bdev, so must have one attached to a 1107 * bio to track stats. Most drivers do not set the bdev for passthrough 1108 * requests, but nvme is one that will set it. 1109 */ 1110 if (!bio->bi_bdev) 1111 return false; 1112 1113 /* 1114 * We don't know what a passthrough command does, but we know the 1115 * payload size and data direction. Ensuring the size is aligned to the 1116 * block size filters out most commands with payloads that don't 1117 * represent sector access. 1118 */ 1119 if (blk_rq_bytes(req) & (bdev_logical_block_size(bio->bi_bdev) - 1)) 1120 return false; 1121 return true; 1122 } 1123 1124 static inline void blk_account_io_start(struct request *req) 1125 { 1126 trace_block_io_start(req); 1127 1128 if (!blk_queue_io_stat(req->q)) 1129 return; 1130 if (blk_rq_is_passthrough(req) && !blk_rq_passthrough_stats(req)) 1131 return; 1132 1133 req->rq_flags |= RQF_IO_STAT; 1134 req->start_time_ns = blk_time_get_ns(); 1135 1136 /* 1137 * All non-passthrough requests are created from a bio with one 1138 * exception: when a flush command that is part of a flush sequence 1139 * generated by the state machine in blk-flush.c is cloned onto the 1140 * lower device by dm-multipath we can get here without a bio. 1141 */ 1142 if (req->bio) 1143 req->part = req->bio->bi_bdev; 1144 else 1145 req->part = req->q->disk->part0; 1146 1147 part_stat_lock(); 1148 update_io_ticks(req->part, jiffies, false); 1149 part_stat_local_inc(req->part, in_flight[op_is_write(req_op(req))]); 1150 part_stat_unlock(); 1151 } 1152 1153 static inline void __blk_mq_end_request_acct(struct request *rq, u64 now) 1154 { 1155 if (rq->rq_flags & RQF_STATS) 1156 blk_stat_add(rq, now); 1157 1158 blk_mq_sched_completed_request(rq, now); 1159 blk_account_io_done(rq, now); 1160 } 1161 1162 inline void __blk_mq_end_request(struct request *rq, blk_status_t error) 1163 { 1164 if (blk_mq_need_time_stamp(rq)) 1165 __blk_mq_end_request_acct(rq, blk_time_get_ns()); 1166 1167 blk_mq_finish_request(rq); 1168 1169 if (rq->end_io) { 1170 rq_qos_done(rq->q, rq); 1171 if (rq->end_io(rq, error, NULL) == RQ_END_IO_FREE) 1172 blk_mq_free_request(rq); 1173 } else { 1174 blk_mq_free_request(rq); 1175 } 1176 } 1177 EXPORT_SYMBOL(__blk_mq_end_request); 1178 1179 void blk_mq_end_request(struct request *rq, blk_status_t error) 1180 { 1181 if (blk_update_request(rq, error, blk_rq_bytes(rq))) 1182 BUG(); 1183 __blk_mq_end_request(rq, error); 1184 } 1185 EXPORT_SYMBOL(blk_mq_end_request); 1186 1187 #define TAG_COMP_BATCH 32 1188 1189 static inline void blk_mq_flush_tag_batch(struct blk_mq_hw_ctx *hctx, 1190 int *tag_array, int nr_tags) 1191 { 1192 struct request_queue *q = hctx->queue; 1193 1194 blk_mq_sub_active_requests(hctx, nr_tags); 1195 1196 blk_mq_put_tags(hctx->tags, tag_array, nr_tags); 1197 percpu_ref_put_many(&q->q_usage_counter, nr_tags); 1198 } 1199 1200 void blk_mq_end_request_batch(struct io_comp_batch *iob) 1201 { 1202 int tags[TAG_COMP_BATCH], nr_tags = 0; 1203 struct blk_mq_hw_ctx *cur_hctx = NULL; 1204 struct request *rq; 1205 u64 now = 0; 1206 1207 if (iob->need_ts) 1208 now = blk_time_get_ns(); 1209 1210 while ((rq = rq_list_pop(&iob->req_list)) != NULL) { 1211 prefetch(rq->bio); 1212 prefetch(rq->rq_next); 1213 1214 blk_complete_request(rq); 1215 if (iob->need_ts) 1216 __blk_mq_end_request_acct(rq, now); 1217 1218 blk_mq_finish_request(rq); 1219 1220 rq_qos_done(rq->q, rq); 1221 1222 /* 1223 * If end_io handler returns NONE, then it still has 1224 * ownership of the request. 1225 */ 1226 if (rq->end_io && rq->end_io(rq, 0, iob) == RQ_END_IO_NONE) 1227 continue; 1228 1229 WRITE_ONCE(rq->state, MQ_RQ_IDLE); 1230 if (!req_ref_put_and_test(rq)) 1231 continue; 1232 1233 blk_crypto_free_request(rq); 1234 blk_pm_mark_last_busy(rq); 1235 1236 if (nr_tags == TAG_COMP_BATCH || cur_hctx != rq->mq_hctx) { 1237 if (cur_hctx) 1238 blk_mq_flush_tag_batch(cur_hctx, tags, nr_tags); 1239 nr_tags = 0; 1240 cur_hctx = rq->mq_hctx; 1241 } 1242 tags[nr_tags++] = rq->tag; 1243 } 1244 1245 if (nr_tags) 1246 blk_mq_flush_tag_batch(cur_hctx, tags, nr_tags); 1247 } 1248 EXPORT_SYMBOL_GPL(blk_mq_end_request_batch); 1249 1250 static void blk_complete_reqs(struct llist_head *list) 1251 { 1252 struct llist_node *entry = llist_reverse_order(llist_del_all(list)); 1253 struct request *rq, *next; 1254 1255 llist_for_each_entry_safe(rq, next, entry, ipi_list) 1256 rq->q->mq_ops->complete(rq); 1257 } 1258 1259 static __latent_entropy void blk_done_softirq(void) 1260 { 1261 blk_complete_reqs(this_cpu_ptr(&blk_cpu_done)); 1262 } 1263 1264 static int blk_softirq_cpu_dead(unsigned int cpu) 1265 { 1266 blk_complete_reqs(&per_cpu(blk_cpu_done, cpu)); 1267 return 0; 1268 } 1269 1270 static void __blk_mq_complete_request_remote(void *data) 1271 { 1272 __raise_softirq_irqoff(BLOCK_SOFTIRQ); 1273 } 1274 1275 static inline bool blk_mq_complete_need_ipi(struct request *rq) 1276 { 1277 int cpu = raw_smp_processor_id(); 1278 1279 if (!IS_ENABLED(CONFIG_SMP) || 1280 !test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) 1281 return false; 1282 /* 1283 * With force threaded interrupts enabled, raising softirq from an SMP 1284 * function call will always result in waking the ksoftirqd thread. 1285 * This is probably worse than completing the request on a different 1286 * cache domain. 1287 */ 1288 if (force_irqthreads()) 1289 return false; 1290 1291 /* same CPU or cache domain and capacity? Complete locally */ 1292 if (cpu == rq->mq_ctx->cpu || 1293 (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags) && 1294 cpus_share_cache(cpu, rq->mq_ctx->cpu) && 1295 cpus_equal_capacity(cpu, rq->mq_ctx->cpu))) 1296 return false; 1297 1298 /* don't try to IPI to an offline CPU */ 1299 return cpu_online(rq->mq_ctx->cpu); 1300 } 1301 1302 static void blk_mq_complete_send_ipi(struct request *rq) 1303 { 1304 unsigned int cpu; 1305 1306 cpu = rq->mq_ctx->cpu; 1307 if (llist_add(&rq->ipi_list, &per_cpu(blk_cpu_done, cpu))) 1308 smp_call_function_single_async(cpu, &per_cpu(blk_cpu_csd, cpu)); 1309 } 1310 1311 static void blk_mq_raise_softirq(struct request *rq) 1312 { 1313 struct llist_head *list; 1314 1315 preempt_disable(); 1316 list = this_cpu_ptr(&blk_cpu_done); 1317 if (llist_add(&rq->ipi_list, list)) 1318 raise_softirq(BLOCK_SOFTIRQ); 1319 preempt_enable(); 1320 } 1321 1322 bool blk_mq_complete_request_remote(struct request *rq) 1323 { 1324 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE); 1325 1326 /* 1327 * For request which hctx has only one ctx mapping, 1328 * or a polled request, always complete locally, 1329 * it's pointless to redirect the completion. 1330 */ 1331 if ((rq->mq_hctx->nr_ctx == 1 && 1332 rq->mq_ctx->cpu == raw_smp_processor_id()) || 1333 rq->cmd_flags & REQ_POLLED) 1334 return false; 1335 1336 if (blk_mq_complete_need_ipi(rq)) { 1337 blk_mq_complete_send_ipi(rq); 1338 return true; 1339 } 1340 1341 if (rq->q->nr_hw_queues == 1) { 1342 blk_mq_raise_softirq(rq); 1343 return true; 1344 } 1345 return false; 1346 } 1347 EXPORT_SYMBOL_GPL(blk_mq_complete_request_remote); 1348 1349 /** 1350 * blk_mq_complete_request - end I/O on a request 1351 * @rq: the request being processed 1352 * 1353 * Description: 1354 * Complete a request by scheduling the ->complete_rq operation. 1355 **/ 1356 void blk_mq_complete_request(struct request *rq) 1357 { 1358 if (!blk_mq_complete_request_remote(rq)) 1359 rq->q->mq_ops->complete(rq); 1360 } 1361 EXPORT_SYMBOL(blk_mq_complete_request); 1362 1363 /** 1364 * blk_mq_start_request - Start processing a request 1365 * @rq: Pointer to request to be started 1366 * 1367 * Function used by device drivers to notify the block layer that a request 1368 * is going to be processed now, so blk layer can do proper initializations 1369 * such as starting the timeout timer. 1370 */ 1371 void blk_mq_start_request(struct request *rq) 1372 { 1373 struct request_queue *q = rq->q; 1374 1375 trace_block_rq_issue(rq); 1376 1377 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags) && 1378 !blk_rq_is_passthrough(rq)) { 1379 rq->io_start_time_ns = blk_time_get_ns(); 1380 rq->stats_sectors = blk_rq_sectors(rq); 1381 rq->rq_flags |= RQF_STATS; 1382 rq_qos_issue(q, rq); 1383 } 1384 1385 WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE); 1386 1387 blk_add_timer(rq); 1388 WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT); 1389 rq->mq_hctx->tags->rqs[rq->tag] = rq; 1390 1391 if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE) 1392 blk_integrity_prepare(rq); 1393 1394 if (rq->bio && rq->bio->bi_opf & REQ_POLLED) 1395 WRITE_ONCE(rq->bio->bi_cookie, rq->mq_hctx->queue_num); 1396 } 1397 EXPORT_SYMBOL(blk_mq_start_request); 1398 1399 /* 1400 * Allow 2x BLK_MAX_REQUEST_COUNT requests on plug queue for multiple 1401 * queues. This is important for md arrays to benefit from merging 1402 * requests. 1403 */ 1404 static inline unsigned short blk_plug_max_rq_count(struct blk_plug *plug) 1405 { 1406 if (plug->multiple_queues) 1407 return BLK_MAX_REQUEST_COUNT * 2; 1408 return BLK_MAX_REQUEST_COUNT; 1409 } 1410 1411 static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq) 1412 { 1413 struct request *last = rq_list_peek(&plug->mq_list); 1414 1415 if (!plug->rq_count) { 1416 trace_block_plug(rq->q); 1417 } else if (plug->rq_count >= blk_plug_max_rq_count(plug) || 1418 (!blk_queue_nomerges(rq->q) && 1419 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) { 1420 blk_mq_flush_plug_list(plug, false); 1421 last = NULL; 1422 trace_block_plug(rq->q); 1423 } 1424 1425 if (!plug->multiple_queues && last && last->q != rq->q) 1426 plug->multiple_queues = true; 1427 /* 1428 * Any request allocated from sched tags can't be issued to 1429 * ->queue_rqs() directly 1430 */ 1431 if (!plug->has_elevator && (rq->rq_flags & RQF_SCHED_TAGS)) 1432 plug->has_elevator = true; 1433 rq_list_add_tail(&plug->mq_list, rq); 1434 plug->rq_count++; 1435 } 1436 1437 /** 1438 * blk_execute_rq_nowait - insert a request to I/O scheduler for execution 1439 * @rq: request to insert 1440 * @at_head: insert request at head or tail of queue 1441 * 1442 * Description: 1443 * Insert a fully prepared request at the back of the I/O scheduler queue 1444 * for execution. Don't wait for completion. 1445 * 1446 * Note: 1447 * This function will invoke @done directly if the queue is dead. 1448 */ 1449 void blk_execute_rq_nowait(struct request *rq, bool at_head) 1450 { 1451 struct blk_mq_hw_ctx *hctx = rq->mq_hctx; 1452 1453 WARN_ON(irqs_disabled()); 1454 WARN_ON(!blk_rq_is_passthrough(rq)); 1455 1456 blk_account_io_start(rq); 1457 1458 if (current->plug && !at_head) { 1459 blk_add_rq_to_plug(current->plug, rq); 1460 return; 1461 } 1462 1463 blk_mq_insert_request(rq, at_head ? BLK_MQ_INSERT_AT_HEAD : 0); 1464 blk_mq_run_hw_queue(hctx, hctx->flags & BLK_MQ_F_BLOCKING); 1465 } 1466 EXPORT_SYMBOL_GPL(blk_execute_rq_nowait); 1467 1468 struct blk_rq_wait { 1469 struct completion done; 1470 blk_status_t ret; 1471 }; 1472 1473 static enum rq_end_io_ret blk_end_sync_rq(struct request *rq, blk_status_t ret, 1474 const struct io_comp_batch *iob) 1475 { 1476 struct blk_rq_wait *wait = rq->end_io_data; 1477 1478 wait->ret = ret; 1479 complete(&wait->done); 1480 return RQ_END_IO_NONE; 1481 } 1482 1483 bool blk_rq_is_poll(struct request *rq) 1484 { 1485 if (!rq->mq_hctx) 1486 return false; 1487 if (rq->mq_hctx->type != HCTX_TYPE_POLL) 1488 return false; 1489 return true; 1490 } 1491 EXPORT_SYMBOL_GPL(blk_rq_is_poll); 1492 1493 static void blk_rq_poll_completion(struct request *rq, struct completion *wait) 1494 { 1495 do { 1496 blk_hctx_poll(rq->q, rq->mq_hctx, NULL, BLK_POLL_ONESHOT); 1497 cond_resched(); 1498 } while (!completion_done(wait)); 1499 } 1500 1501 /** 1502 * blk_execute_rq - insert a request into queue for execution 1503 * @rq: request to insert 1504 * @at_head: insert request at head or tail of queue 1505 * 1506 * Description: 1507 * Insert a fully prepared request at the back of the I/O scheduler queue 1508 * for execution and wait for completion. 1509 * Return: The blk_status_t result provided to blk_mq_end_request(). 1510 */ 1511 blk_status_t blk_execute_rq(struct request *rq, bool at_head) 1512 { 1513 struct blk_mq_hw_ctx *hctx = rq->mq_hctx; 1514 struct blk_rq_wait wait = { 1515 .done = COMPLETION_INITIALIZER_ONSTACK(wait.done), 1516 }; 1517 1518 WARN_ON(irqs_disabled()); 1519 WARN_ON(!blk_rq_is_passthrough(rq)); 1520 1521 rq->end_io_data = &wait; 1522 rq->end_io = blk_end_sync_rq; 1523 1524 blk_account_io_start(rq); 1525 blk_mq_insert_request(rq, at_head ? BLK_MQ_INSERT_AT_HEAD : 0); 1526 blk_mq_run_hw_queue(hctx, false); 1527 1528 if (blk_rq_is_poll(rq)) 1529 blk_rq_poll_completion(rq, &wait.done); 1530 else 1531 blk_wait_io(&wait.done); 1532 1533 return wait.ret; 1534 } 1535 EXPORT_SYMBOL(blk_execute_rq); 1536 1537 static void __blk_mq_requeue_request(struct request *rq) 1538 { 1539 struct request_queue *q = rq->q; 1540 1541 blk_mq_put_driver_tag(rq); 1542 1543 trace_block_rq_requeue(rq); 1544 rq_qos_requeue(q, rq); 1545 1546 if (blk_mq_request_started(rq)) { 1547 WRITE_ONCE(rq->state, MQ_RQ_IDLE); 1548 rq->rq_flags &= ~RQF_TIMED_OUT; 1549 } 1550 } 1551 1552 void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list) 1553 { 1554 struct request_queue *q = rq->q; 1555 unsigned long flags; 1556 1557 __blk_mq_requeue_request(rq); 1558 1559 /* this request will be re-inserted to io scheduler queue */ 1560 blk_mq_sched_requeue_request(rq); 1561 1562 spin_lock_irqsave(&q->requeue_lock, flags); 1563 list_add_tail(&rq->queuelist, &q->requeue_list); 1564 spin_unlock_irqrestore(&q->requeue_lock, flags); 1565 1566 if (kick_requeue_list) 1567 blk_mq_kick_requeue_list(q); 1568 } 1569 EXPORT_SYMBOL(blk_mq_requeue_request); 1570 1571 static void blk_mq_requeue_work(struct work_struct *work) 1572 { 1573 struct request_queue *q = 1574 container_of(work, struct request_queue, requeue_work.work); 1575 LIST_HEAD(rq_list); 1576 LIST_HEAD(flush_list); 1577 struct request *rq; 1578 1579 spin_lock_irq(&q->requeue_lock); 1580 list_splice_init(&q->requeue_list, &rq_list); 1581 list_splice_init(&q->flush_list, &flush_list); 1582 spin_unlock_irq(&q->requeue_lock); 1583 1584 while (!list_empty(&rq_list)) { 1585 rq = list_entry(rq_list.next, struct request, queuelist); 1586 list_del_init(&rq->queuelist); 1587 /* 1588 * If RQF_DONTPREP is set, the request has been started by the 1589 * driver already and might have driver-specific data allocated 1590 * already. Insert it into the hctx dispatch list to avoid 1591 * block layer merges for the request. 1592 */ 1593 if (rq->rq_flags & RQF_DONTPREP) 1594 blk_mq_request_bypass_insert(rq, 0); 1595 else 1596 blk_mq_insert_request(rq, BLK_MQ_INSERT_AT_HEAD); 1597 } 1598 1599 while (!list_empty(&flush_list)) { 1600 rq = list_entry(flush_list.next, struct request, queuelist); 1601 list_del_init(&rq->queuelist); 1602 blk_mq_insert_request(rq, 0); 1603 } 1604 1605 blk_mq_run_hw_queues(q, false); 1606 } 1607 1608 void blk_mq_kick_requeue_list(struct request_queue *q) 1609 { 1610 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0); 1611 } 1612 EXPORT_SYMBOL(blk_mq_kick_requeue_list); 1613 1614 void blk_mq_delay_kick_requeue_list(struct request_queue *q, 1615 unsigned long msecs) 1616 { 1617 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 1618 msecs_to_jiffies(msecs)); 1619 } 1620 EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list); 1621 1622 static bool blk_is_flush_data_rq(struct request *rq) 1623 { 1624 return (rq->rq_flags & RQF_FLUSH_SEQ) && !is_flush_rq(rq); 1625 } 1626 1627 static bool blk_mq_rq_inflight(struct request *rq, void *priv) 1628 { 1629 /* 1630 * If we find a request that isn't idle we know the queue is busy 1631 * as it's checked in the iter. 1632 * Return false to stop the iteration. 1633 * 1634 * In case of queue quiesce, if one flush data request is completed, 1635 * don't count it as inflight given the flush sequence is suspended, 1636 * and the original flush data request is invisible to driver, just 1637 * like other pending requests because of quiesce 1638 */ 1639 if (blk_mq_request_started(rq) && !(blk_queue_quiesced(rq->q) && 1640 blk_is_flush_data_rq(rq) && 1641 blk_mq_request_completed(rq))) { 1642 bool *busy = priv; 1643 1644 *busy = true; 1645 return false; 1646 } 1647 1648 return true; 1649 } 1650 1651 bool blk_mq_queue_inflight(struct request_queue *q) 1652 { 1653 bool busy = false; 1654 1655 blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy); 1656 return busy; 1657 } 1658 EXPORT_SYMBOL_GPL(blk_mq_queue_inflight); 1659 1660 static void blk_mq_rq_timed_out(struct request *req) 1661 { 1662 req->rq_flags |= RQF_TIMED_OUT; 1663 if (req->q->mq_ops->timeout) { 1664 enum blk_eh_timer_return ret; 1665 1666 ret = req->q->mq_ops->timeout(req); 1667 if (ret == BLK_EH_DONE) 1668 return; 1669 WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER); 1670 } 1671 1672 blk_add_timer(req); 1673 } 1674 1675 struct blk_expired_data { 1676 bool has_timedout_rq; 1677 unsigned long next; 1678 unsigned long timeout_start; 1679 }; 1680 1681 static bool blk_mq_req_expired(struct request *rq, struct blk_expired_data *expired) 1682 { 1683 unsigned long deadline; 1684 1685 if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT) 1686 return false; 1687 if (rq->rq_flags & RQF_TIMED_OUT) 1688 return false; 1689 1690 deadline = READ_ONCE(rq->deadline); 1691 if (time_after_eq(expired->timeout_start, deadline)) 1692 return true; 1693 1694 if (expired->next == 0) 1695 expired->next = deadline; 1696 else if (time_after(expired->next, deadline)) 1697 expired->next = deadline; 1698 return false; 1699 } 1700 1701 void blk_mq_put_rq_ref(struct request *rq) 1702 { 1703 if (is_flush_rq(rq)) { 1704 if (rq->end_io(rq, 0, NULL) == RQ_END_IO_FREE) 1705 blk_mq_free_request(rq); 1706 } else if (req_ref_put_and_test(rq)) { 1707 __blk_mq_free_request(rq); 1708 } 1709 } 1710 1711 static bool blk_mq_check_expired(struct request *rq, void *priv) 1712 { 1713 struct blk_expired_data *expired = priv; 1714 1715 /* 1716 * blk_mq_queue_tag_busy_iter() has locked the request, so it cannot 1717 * be reallocated underneath the timeout handler's processing, then 1718 * the expire check is reliable. If the request is not expired, then 1719 * it was completed and reallocated as a new request after returning 1720 * from blk_mq_check_expired(). 1721 */ 1722 if (blk_mq_req_expired(rq, expired)) { 1723 expired->has_timedout_rq = true; 1724 return false; 1725 } 1726 return true; 1727 } 1728 1729 static bool blk_mq_handle_expired(struct request *rq, void *priv) 1730 { 1731 struct blk_expired_data *expired = priv; 1732 1733 if (blk_mq_req_expired(rq, expired)) 1734 blk_mq_rq_timed_out(rq); 1735 return true; 1736 } 1737 1738 static void blk_mq_timeout_work(struct work_struct *work) 1739 { 1740 struct request_queue *q = 1741 container_of(work, struct request_queue, timeout_work); 1742 struct blk_expired_data expired = { 1743 .timeout_start = jiffies, 1744 }; 1745 struct blk_mq_hw_ctx *hctx; 1746 unsigned long i; 1747 1748 /* A deadlock might occur if a request is stuck requiring a 1749 * timeout at the same time a queue freeze is waiting 1750 * completion, since the timeout code would not be able to 1751 * acquire the queue reference here. 1752 * 1753 * That's why we don't use blk_queue_enter here; instead, we use 1754 * percpu_ref_tryget directly, because we need to be able to 1755 * obtain a reference even in the short window between the queue 1756 * starting to freeze, by dropping the first reference in 1757 * blk_freeze_queue_start, and the moment the last request is 1758 * consumed, marked by the instant q_usage_counter reaches 1759 * zero. 1760 */ 1761 if (!percpu_ref_tryget(&q->q_usage_counter)) 1762 return; 1763 1764 /* check if there is any timed-out request */ 1765 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &expired); 1766 if (expired.has_timedout_rq) { 1767 /* 1768 * Before walking tags, we must ensure any submit started 1769 * before the current time has finished. Since the submit 1770 * uses srcu or rcu, wait for a synchronization point to 1771 * ensure all running submits have finished 1772 */ 1773 blk_mq_wait_quiesce_done(q->tag_set); 1774 1775 expired.next = 0; 1776 blk_mq_queue_tag_busy_iter(q, blk_mq_handle_expired, &expired); 1777 } 1778 1779 if (expired.next != 0) { 1780 mod_timer(&q->timeout, expired.next); 1781 } else { 1782 /* 1783 * Request timeouts are handled as a forward rolling timer. If 1784 * we end up here it means that no requests are pending and 1785 * also that no request has been pending for a while. Mark 1786 * each hctx as idle. 1787 */ 1788 queue_for_each_hw_ctx(q, hctx, i) { 1789 /* the hctx may be unmapped, so check it here */ 1790 if (blk_mq_hw_queue_mapped(hctx)) 1791 blk_mq_tag_idle(hctx); 1792 } 1793 } 1794 blk_queue_exit(q); 1795 } 1796 1797 struct flush_busy_ctx_data { 1798 struct blk_mq_hw_ctx *hctx; 1799 struct list_head *list; 1800 }; 1801 1802 static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data) 1803 { 1804 struct flush_busy_ctx_data *flush_data = data; 1805 struct blk_mq_hw_ctx *hctx = flush_data->hctx; 1806 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr]; 1807 enum hctx_type type = hctx->type; 1808 1809 spin_lock(&ctx->lock); 1810 list_splice_tail_init(&ctx->rq_lists[type], flush_data->list); 1811 sbitmap_clear_bit(sb, bitnr); 1812 spin_unlock(&ctx->lock); 1813 return true; 1814 } 1815 1816 /* 1817 * Process software queues that have been marked busy, splicing them 1818 * to the for-dispatch 1819 */ 1820 void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list) 1821 { 1822 struct flush_busy_ctx_data data = { 1823 .hctx = hctx, 1824 .list = list, 1825 }; 1826 1827 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data); 1828 } 1829 1830 struct dispatch_rq_data { 1831 struct blk_mq_hw_ctx *hctx; 1832 struct request *rq; 1833 }; 1834 1835 static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr, 1836 void *data) 1837 { 1838 struct dispatch_rq_data *dispatch_data = data; 1839 struct blk_mq_hw_ctx *hctx = dispatch_data->hctx; 1840 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr]; 1841 enum hctx_type type = hctx->type; 1842 1843 spin_lock(&ctx->lock); 1844 if (!list_empty(&ctx->rq_lists[type])) { 1845 dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next); 1846 list_del_init(&dispatch_data->rq->queuelist); 1847 if (list_empty(&ctx->rq_lists[type])) 1848 sbitmap_clear_bit(sb, bitnr); 1849 } 1850 spin_unlock(&ctx->lock); 1851 1852 return !dispatch_data->rq; 1853 } 1854 1855 struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx, 1856 struct blk_mq_ctx *start) 1857 { 1858 unsigned off = start ? start->index_hw[hctx->type] : 0; 1859 struct dispatch_rq_data data = { 1860 .hctx = hctx, 1861 .rq = NULL, 1862 }; 1863 1864 __sbitmap_for_each_set(&hctx->ctx_map, off, 1865 dispatch_rq_from_ctx, &data); 1866 1867 return data.rq; 1868 } 1869 1870 bool __blk_mq_alloc_driver_tag(struct request *rq) 1871 { 1872 struct sbitmap_queue *bt = &rq->mq_hctx->tags->bitmap_tags; 1873 unsigned int tag_offset = rq->mq_hctx->tags->nr_reserved_tags; 1874 int tag; 1875 1876 blk_mq_tag_busy(rq->mq_hctx); 1877 1878 if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag)) { 1879 bt = &rq->mq_hctx->tags->breserved_tags; 1880 tag_offset = 0; 1881 } else { 1882 if (!hctx_may_queue(rq->mq_hctx, bt)) 1883 return false; 1884 } 1885 1886 tag = __sbitmap_queue_get(bt); 1887 if (tag == BLK_MQ_NO_TAG) 1888 return false; 1889 1890 rq->tag = tag + tag_offset; 1891 blk_mq_inc_active_requests(rq->mq_hctx); 1892 return true; 1893 } 1894 1895 static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode, 1896 int flags, void *key) 1897 { 1898 struct blk_mq_hw_ctx *hctx; 1899 1900 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait); 1901 1902 spin_lock(&hctx->dispatch_wait_lock); 1903 if (!list_empty(&wait->entry)) { 1904 struct sbitmap_queue *sbq; 1905 1906 list_del_init(&wait->entry); 1907 sbq = &hctx->tags->bitmap_tags; 1908 atomic_dec(&sbq->ws_active); 1909 } 1910 spin_unlock(&hctx->dispatch_wait_lock); 1911 1912 blk_mq_run_hw_queue(hctx, true); 1913 return 1; 1914 } 1915 1916 /* 1917 * Mark us waiting for a tag. For shared tags, this involves hooking us into 1918 * the tag wakeups. For non-shared tags, we can simply mark us needing a 1919 * restart. For both cases, take care to check the condition again after 1920 * marking us as waiting. 1921 */ 1922 static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx, 1923 struct request *rq) 1924 { 1925 struct sbitmap_queue *sbq; 1926 struct wait_queue_head *wq; 1927 wait_queue_entry_t *wait; 1928 bool ret; 1929 1930 if (!(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) && 1931 !(blk_mq_is_shared_tags(hctx->flags))) { 1932 blk_mq_sched_mark_restart_hctx(hctx); 1933 1934 /* 1935 * It's possible that a tag was freed in the window between the 1936 * allocation failure and adding the hardware queue to the wait 1937 * queue. 1938 * 1939 * Don't clear RESTART here, someone else could have set it. 1940 * At most this will cost an extra queue run. 1941 */ 1942 return blk_mq_get_driver_tag(rq); 1943 } 1944 1945 wait = &hctx->dispatch_wait; 1946 if (!list_empty_careful(&wait->entry)) 1947 return false; 1948 1949 if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag)) 1950 sbq = &hctx->tags->breserved_tags; 1951 else 1952 sbq = &hctx->tags->bitmap_tags; 1953 wq = &bt_wait_ptr(sbq, hctx)->wait; 1954 1955 spin_lock_irq(&wq->lock); 1956 spin_lock(&hctx->dispatch_wait_lock); 1957 if (!list_empty(&wait->entry)) { 1958 spin_unlock(&hctx->dispatch_wait_lock); 1959 spin_unlock_irq(&wq->lock); 1960 return false; 1961 } 1962 1963 atomic_inc(&sbq->ws_active); 1964 wait->flags &= ~WQ_FLAG_EXCLUSIVE; 1965 __add_wait_queue(wq, wait); 1966 1967 /* 1968 * Add one explicit barrier since blk_mq_get_driver_tag() may 1969 * not imply barrier in case of failure. 1970 * 1971 * Order adding us to wait queue and allocating driver tag. 1972 * 1973 * The pair is the one implied in sbitmap_queue_wake_up() which 1974 * orders clearing sbitmap tag bits and waitqueue_active() in 1975 * __sbitmap_queue_wake_up(), since waitqueue_active() is lockless 1976 * 1977 * Otherwise, re-order of adding wait queue and getting driver tag 1978 * may cause __sbitmap_queue_wake_up() to wake up nothing because 1979 * the waitqueue_active() may not observe us in wait queue. 1980 */ 1981 smp_mb(); 1982 1983 /* 1984 * It's possible that a tag was freed in the window between the 1985 * allocation failure and adding the hardware queue to the wait 1986 * queue. 1987 */ 1988 ret = blk_mq_get_driver_tag(rq); 1989 if (!ret) { 1990 spin_unlock(&hctx->dispatch_wait_lock); 1991 spin_unlock_irq(&wq->lock); 1992 return false; 1993 } 1994 1995 /* 1996 * We got a tag, remove ourselves from the wait queue to ensure 1997 * someone else gets the wakeup. 1998 */ 1999 list_del_init(&wait->entry); 2000 atomic_dec(&sbq->ws_active); 2001 spin_unlock(&hctx->dispatch_wait_lock); 2002 spin_unlock_irq(&wq->lock); 2003 2004 return true; 2005 } 2006 2007 #define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8 2008 #define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4 2009 /* 2010 * Update dispatch busy with the Exponential Weighted Moving Average(EWMA): 2011 * - EWMA is one simple way to compute running average value 2012 * - weight(7/8 and 1/8) is applied so that it can decrease exponentially 2013 * - take 4 as factor for avoiding to get too small(0) result, and this 2014 * factor doesn't matter because EWMA decreases exponentially 2015 */ 2016 static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy) 2017 { 2018 unsigned int ewma; 2019 2020 ewma = hctx->dispatch_busy; 2021 2022 if (!ewma && !busy) 2023 return; 2024 2025 ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1; 2026 if (busy) 2027 ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR; 2028 ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT; 2029 2030 hctx->dispatch_busy = ewma; 2031 } 2032 2033 #define BLK_MQ_RESOURCE_DELAY 3 /* ms units */ 2034 2035 static void blk_mq_handle_dev_resource(struct request *rq, 2036 struct list_head *list) 2037 { 2038 list_add(&rq->queuelist, list); 2039 __blk_mq_requeue_request(rq); 2040 } 2041 2042 enum prep_dispatch { 2043 PREP_DISPATCH_OK, 2044 PREP_DISPATCH_NO_TAG, 2045 PREP_DISPATCH_NO_BUDGET, 2046 }; 2047 2048 static enum prep_dispatch blk_mq_prep_dispatch_rq(struct request *rq, 2049 bool need_budget) 2050 { 2051 struct blk_mq_hw_ctx *hctx = rq->mq_hctx; 2052 int budget_token = -1; 2053 2054 if (need_budget) { 2055 budget_token = blk_mq_get_dispatch_budget(rq->q); 2056 if (budget_token < 0) { 2057 blk_mq_put_driver_tag(rq); 2058 return PREP_DISPATCH_NO_BUDGET; 2059 } 2060 blk_mq_set_rq_budget_token(rq, budget_token); 2061 } 2062 2063 if (!blk_mq_get_driver_tag(rq)) { 2064 /* 2065 * The initial allocation attempt failed, so we need to 2066 * rerun the hardware queue when a tag is freed. The 2067 * waitqueue takes care of that. If the queue is run 2068 * before we add this entry back on the dispatch list, 2069 * we'll re-run it below. 2070 */ 2071 if (!blk_mq_mark_tag_wait(hctx, rq)) { 2072 /* 2073 * All budgets not got from this function will be put 2074 * together during handling partial dispatch 2075 */ 2076 if (need_budget) 2077 blk_mq_put_dispatch_budget(rq->q, budget_token); 2078 return PREP_DISPATCH_NO_TAG; 2079 } 2080 } 2081 2082 return PREP_DISPATCH_OK; 2083 } 2084 2085 /* release all allocated budgets before calling to blk_mq_dispatch_rq_list */ 2086 static void blk_mq_release_budgets(struct request_queue *q, 2087 struct list_head *list) 2088 { 2089 struct request *rq; 2090 2091 list_for_each_entry(rq, list, queuelist) { 2092 int budget_token = blk_mq_get_rq_budget_token(rq); 2093 2094 if (budget_token >= 0) 2095 blk_mq_put_dispatch_budget(q, budget_token); 2096 } 2097 } 2098 2099 /* 2100 * blk_mq_commit_rqs will notify driver using bd->last that there is no 2101 * more requests. (See comment in struct blk_mq_ops for commit_rqs for 2102 * details) 2103 * Attention, we should explicitly call this in unusual cases: 2104 * 1) did not queue everything initially scheduled to queue 2105 * 2) the last attempt to queue a request failed 2106 */ 2107 static void blk_mq_commit_rqs(struct blk_mq_hw_ctx *hctx, int queued, 2108 bool from_schedule) 2109 { 2110 if (hctx->queue->mq_ops->commit_rqs && queued) { 2111 trace_block_unplug(hctx->queue, queued, !from_schedule); 2112 hctx->queue->mq_ops->commit_rqs(hctx); 2113 } 2114 } 2115 2116 /* 2117 * Returns true if we did some work AND can potentially do more. 2118 */ 2119 bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list, 2120 bool get_budget) 2121 { 2122 enum prep_dispatch prep; 2123 struct request_queue *q = hctx->queue; 2124 struct request *rq; 2125 int queued; 2126 blk_status_t ret = BLK_STS_OK; 2127 bool needs_resource = false; 2128 2129 if (list_empty(list)) 2130 return false; 2131 2132 /* 2133 * Now process all the entries, sending them to the driver. 2134 */ 2135 queued = 0; 2136 do { 2137 struct blk_mq_queue_data bd; 2138 2139 rq = list_first_entry(list, struct request, queuelist); 2140 2141 WARN_ON_ONCE(hctx != rq->mq_hctx); 2142 prep = blk_mq_prep_dispatch_rq(rq, get_budget); 2143 if (prep != PREP_DISPATCH_OK) 2144 break; 2145 2146 list_del_init(&rq->queuelist); 2147 2148 bd.rq = rq; 2149 bd.last = list_empty(list); 2150 2151 ret = q->mq_ops->queue_rq(hctx, &bd); 2152 switch (ret) { 2153 case BLK_STS_OK: 2154 queued++; 2155 break; 2156 case BLK_STS_RESOURCE: 2157 needs_resource = true; 2158 fallthrough; 2159 case BLK_STS_DEV_RESOURCE: 2160 blk_mq_handle_dev_resource(rq, list); 2161 goto out; 2162 default: 2163 blk_mq_end_request(rq, ret); 2164 } 2165 } while (!list_empty(list)); 2166 out: 2167 /* If we didn't flush the entire list, we could have told the driver 2168 * there was more coming, but that turned out to be a lie. 2169 */ 2170 if (!list_empty(list) || ret != BLK_STS_OK) 2171 blk_mq_commit_rqs(hctx, queued, false); 2172 2173 /* 2174 * Any items that need requeuing? Stuff them into hctx->dispatch, 2175 * that is where we will continue on next queue run. 2176 */ 2177 if (!list_empty(list)) { 2178 bool needs_restart; 2179 /* For non-shared tags, the RESTART check will suffice */ 2180 bool no_tag = prep == PREP_DISPATCH_NO_TAG && 2181 ((hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) || 2182 blk_mq_is_shared_tags(hctx->flags)); 2183 2184 /* 2185 * If the caller allocated budgets, free the budgets of the 2186 * requests that have not yet been passed to the block driver. 2187 */ 2188 if (!get_budget) 2189 blk_mq_release_budgets(q, list); 2190 2191 spin_lock(&hctx->lock); 2192 list_splice_tail_init(list, &hctx->dispatch); 2193 spin_unlock(&hctx->lock); 2194 2195 /* 2196 * Order adding requests to hctx->dispatch and checking 2197 * SCHED_RESTART flag. The pair of this smp_mb() is the one 2198 * in blk_mq_sched_restart(). Avoid restart code path to 2199 * miss the new added requests to hctx->dispatch, meantime 2200 * SCHED_RESTART is observed here. 2201 */ 2202 smp_mb(); 2203 2204 /* 2205 * If SCHED_RESTART was set by the caller of this function and 2206 * it is no longer set that means that it was cleared by another 2207 * thread and hence that a queue rerun is needed. 2208 * 2209 * If 'no_tag' is set, that means that we failed getting 2210 * a driver tag with an I/O scheduler attached. If our dispatch 2211 * waitqueue is no longer active, ensure that we run the queue 2212 * AFTER adding our entries back to the list. 2213 * 2214 * If no I/O scheduler has been configured it is possible that 2215 * the hardware queue got stopped and restarted before requests 2216 * were pushed back onto the dispatch list. Rerun the queue to 2217 * avoid starvation. Notes: 2218 * - blk_mq_run_hw_queue() checks whether or not a queue has 2219 * been stopped before rerunning a queue. 2220 * - Some but not all block drivers stop a queue before 2221 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq 2222 * and dm-rq. 2223 * 2224 * If driver returns BLK_STS_RESOURCE and SCHED_RESTART 2225 * bit is set, run queue after a delay to avoid IO stalls 2226 * that could otherwise occur if the queue is idle. We'll do 2227 * similar if we couldn't get budget or couldn't lock a zone 2228 * and SCHED_RESTART is set. 2229 */ 2230 needs_restart = blk_mq_sched_needs_restart(hctx); 2231 if (prep == PREP_DISPATCH_NO_BUDGET) 2232 needs_resource = true; 2233 if (!needs_restart || 2234 (no_tag && list_empty_careful(&hctx->dispatch_wait.entry))) 2235 blk_mq_run_hw_queue(hctx, true); 2236 else if (needs_resource) 2237 blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY); 2238 2239 blk_mq_update_dispatch_busy(hctx, true); 2240 return false; 2241 } 2242 2243 blk_mq_update_dispatch_busy(hctx, false); 2244 return true; 2245 } 2246 2247 static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx) 2248 { 2249 int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask); 2250 2251 if (cpu >= nr_cpu_ids) 2252 cpu = cpumask_first(hctx->cpumask); 2253 return cpu; 2254 } 2255 2256 /* 2257 * ->next_cpu is always calculated from hctx->cpumask, so simply use 2258 * it for speeding up the check 2259 */ 2260 static bool blk_mq_hctx_empty_cpumask(struct blk_mq_hw_ctx *hctx) 2261 { 2262 return hctx->next_cpu >= nr_cpu_ids; 2263 } 2264 2265 /* 2266 * It'd be great if the workqueue API had a way to pass 2267 * in a mask and had some smarts for more clever placement. 2268 * For now we just round-robin here, switching for every 2269 * BLK_MQ_CPU_WORK_BATCH queued items. 2270 */ 2271 static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) 2272 { 2273 bool tried = false; 2274 int next_cpu = hctx->next_cpu; 2275 2276 /* Switch to unbound if no allowable CPUs in this hctx */ 2277 if (hctx->queue->nr_hw_queues == 1 || blk_mq_hctx_empty_cpumask(hctx)) 2278 return WORK_CPU_UNBOUND; 2279 2280 if (--hctx->next_cpu_batch <= 0) { 2281 select_cpu: 2282 next_cpu = cpumask_next_and(next_cpu, hctx->cpumask, 2283 cpu_online_mask); 2284 if (next_cpu >= nr_cpu_ids) 2285 next_cpu = blk_mq_first_mapped_cpu(hctx); 2286 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; 2287 } 2288 2289 /* 2290 * Do unbound schedule if we can't find a online CPU for this hctx, 2291 * and it should only happen in the path of handling CPU DEAD. 2292 */ 2293 if (!cpu_online(next_cpu)) { 2294 if (!tried) { 2295 tried = true; 2296 goto select_cpu; 2297 } 2298 2299 /* 2300 * Make sure to re-select CPU next time once after CPUs 2301 * in hctx->cpumask become online again. 2302 */ 2303 hctx->next_cpu = next_cpu; 2304 hctx->next_cpu_batch = 1; 2305 return WORK_CPU_UNBOUND; 2306 } 2307 2308 hctx->next_cpu = next_cpu; 2309 return next_cpu; 2310 } 2311 2312 /** 2313 * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously. 2314 * @hctx: Pointer to the hardware queue to run. 2315 * @msecs: Milliseconds of delay to wait before running the queue. 2316 * 2317 * Run a hardware queue asynchronously with a delay of @msecs. 2318 */ 2319 void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) 2320 { 2321 if (unlikely(blk_mq_hctx_stopped(hctx))) 2322 return; 2323 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work, 2324 msecs_to_jiffies(msecs)); 2325 } 2326 EXPORT_SYMBOL(blk_mq_delay_run_hw_queue); 2327 2328 static inline bool blk_mq_hw_queue_need_run(struct blk_mq_hw_ctx *hctx) 2329 { 2330 bool need_run; 2331 2332 /* 2333 * When queue is quiesced, we may be switching io scheduler, or 2334 * updating nr_hw_queues, or other things, and we can't run queue 2335 * any more, even blk_mq_hctx_has_pending() can't be called safely. 2336 * 2337 * And queue will be rerun in blk_mq_unquiesce_queue() if it is 2338 * quiesced. 2339 */ 2340 __blk_mq_run_dispatch_ops(hctx->queue, false, 2341 need_run = !blk_queue_quiesced(hctx->queue) && 2342 blk_mq_hctx_has_pending(hctx)); 2343 return need_run; 2344 } 2345 2346 /** 2347 * blk_mq_run_hw_queue - Start to run a hardware queue. 2348 * @hctx: Pointer to the hardware queue to run. 2349 * @async: If we want to run the queue asynchronously. 2350 * 2351 * Check if the request queue is not in a quiesced state and if there are 2352 * pending requests to be sent. If this is true, run the queue to send requests 2353 * to hardware. 2354 */ 2355 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) 2356 { 2357 bool need_run; 2358 2359 /* 2360 * We can't run the queue inline with interrupts disabled. 2361 */ 2362 WARN_ON_ONCE(!async && in_interrupt()); 2363 2364 might_sleep_if(!async && hctx->flags & BLK_MQ_F_BLOCKING); 2365 2366 need_run = blk_mq_hw_queue_need_run(hctx); 2367 if (!need_run) { 2368 unsigned long flags; 2369 2370 /* 2371 * Synchronize with blk_mq_unquiesce_queue(), because we check 2372 * if hw queue is quiesced locklessly above, we need the use 2373 * ->queue_lock to make sure we see the up-to-date status to 2374 * not miss rerunning the hw queue. 2375 */ 2376 spin_lock_irqsave(&hctx->queue->queue_lock, flags); 2377 need_run = blk_mq_hw_queue_need_run(hctx); 2378 spin_unlock_irqrestore(&hctx->queue->queue_lock, flags); 2379 2380 if (!need_run) 2381 return; 2382 } 2383 2384 if (async || !cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)) { 2385 blk_mq_delay_run_hw_queue(hctx, 0); 2386 return; 2387 } 2388 2389 blk_mq_run_dispatch_ops(hctx->queue, 2390 blk_mq_sched_dispatch_requests(hctx)); 2391 } 2392 EXPORT_SYMBOL(blk_mq_run_hw_queue); 2393 2394 /* 2395 * Return prefered queue to dispatch from (if any) for non-mq aware IO 2396 * scheduler. 2397 */ 2398 static struct blk_mq_hw_ctx *blk_mq_get_sq_hctx(struct request_queue *q) 2399 { 2400 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q); 2401 /* 2402 * If the IO scheduler does not respect hardware queues when 2403 * dispatching, we just don't bother with multiple HW queues and 2404 * dispatch from hctx for the current CPU since running multiple queues 2405 * just causes lock contention inside the scheduler and pointless cache 2406 * bouncing. 2407 */ 2408 struct blk_mq_hw_ctx *hctx = ctx->hctxs[HCTX_TYPE_DEFAULT]; 2409 2410 if (!blk_mq_hctx_stopped(hctx)) 2411 return hctx; 2412 return NULL; 2413 } 2414 2415 /** 2416 * blk_mq_run_hw_queues - Run all hardware queues in a request queue. 2417 * @q: Pointer to the request queue to run. 2418 * @async: If we want to run the queue asynchronously. 2419 */ 2420 void blk_mq_run_hw_queues(struct request_queue *q, bool async) 2421 { 2422 struct blk_mq_hw_ctx *hctx, *sq_hctx; 2423 unsigned long i; 2424 2425 sq_hctx = NULL; 2426 if (blk_queue_sq_sched(q)) 2427 sq_hctx = blk_mq_get_sq_hctx(q); 2428 queue_for_each_hw_ctx(q, hctx, i) { 2429 if (blk_mq_hctx_stopped(hctx)) 2430 continue; 2431 /* 2432 * Dispatch from this hctx either if there's no hctx preferred 2433 * by IO scheduler or if it has requests that bypass the 2434 * scheduler. 2435 */ 2436 if (!sq_hctx || sq_hctx == hctx || 2437 !list_empty_careful(&hctx->dispatch)) 2438 blk_mq_run_hw_queue(hctx, async); 2439 } 2440 } 2441 EXPORT_SYMBOL(blk_mq_run_hw_queues); 2442 2443 /** 2444 * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously. 2445 * @q: Pointer to the request queue to run. 2446 * @msecs: Milliseconds of delay to wait before running the queues. 2447 */ 2448 void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs) 2449 { 2450 struct blk_mq_hw_ctx *hctx, *sq_hctx; 2451 unsigned long i; 2452 2453 sq_hctx = NULL; 2454 if (blk_queue_sq_sched(q)) 2455 sq_hctx = blk_mq_get_sq_hctx(q); 2456 queue_for_each_hw_ctx(q, hctx, i) { 2457 if (blk_mq_hctx_stopped(hctx)) 2458 continue; 2459 /* 2460 * If there is already a run_work pending, leave the 2461 * pending delay untouched. Otherwise, a hctx can stall 2462 * if another hctx is re-delaying the other's work 2463 * before the work executes. 2464 */ 2465 if (delayed_work_pending(&hctx->run_work)) 2466 continue; 2467 /* 2468 * Dispatch from this hctx either if there's no hctx preferred 2469 * by IO scheduler or if it has requests that bypass the 2470 * scheduler. 2471 */ 2472 if (!sq_hctx || sq_hctx == hctx || 2473 !list_empty_careful(&hctx->dispatch)) 2474 blk_mq_delay_run_hw_queue(hctx, msecs); 2475 } 2476 } 2477 EXPORT_SYMBOL(blk_mq_delay_run_hw_queues); 2478 2479 /* 2480 * This function is often used for pausing .queue_rq() by driver when 2481 * there isn't enough resource or some conditions aren't satisfied, and 2482 * BLK_STS_RESOURCE is usually returned. 2483 * 2484 * We do not guarantee that dispatch can be drained or blocked 2485 * after blk_mq_stop_hw_queue() returns. Please use 2486 * blk_mq_quiesce_queue() for that requirement. 2487 */ 2488 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) 2489 { 2490 cancel_delayed_work(&hctx->run_work); 2491 2492 set_bit(BLK_MQ_S_STOPPED, &hctx->state); 2493 } 2494 EXPORT_SYMBOL(blk_mq_stop_hw_queue); 2495 2496 /* 2497 * This function is often used for pausing .queue_rq() by driver when 2498 * there isn't enough resource or some conditions aren't satisfied, and 2499 * BLK_STS_RESOURCE is usually returned. 2500 * 2501 * We do not guarantee that dispatch can be drained or blocked 2502 * after blk_mq_stop_hw_queues() returns. Please use 2503 * blk_mq_quiesce_queue() for that requirement. 2504 */ 2505 void blk_mq_stop_hw_queues(struct request_queue *q) 2506 { 2507 struct blk_mq_hw_ctx *hctx; 2508 unsigned long i; 2509 2510 queue_for_each_hw_ctx(q, hctx, i) 2511 blk_mq_stop_hw_queue(hctx); 2512 } 2513 EXPORT_SYMBOL(blk_mq_stop_hw_queues); 2514 2515 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) 2516 { 2517 clear_bit(BLK_MQ_S_STOPPED, &hctx->state); 2518 2519 blk_mq_run_hw_queue(hctx, hctx->flags & BLK_MQ_F_BLOCKING); 2520 } 2521 EXPORT_SYMBOL(blk_mq_start_hw_queue); 2522 2523 void blk_mq_start_hw_queues(struct request_queue *q) 2524 { 2525 struct blk_mq_hw_ctx *hctx; 2526 unsigned long i; 2527 2528 queue_for_each_hw_ctx(q, hctx, i) 2529 blk_mq_start_hw_queue(hctx); 2530 } 2531 EXPORT_SYMBOL(blk_mq_start_hw_queues); 2532 2533 void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) 2534 { 2535 if (!blk_mq_hctx_stopped(hctx)) 2536 return; 2537 2538 clear_bit(BLK_MQ_S_STOPPED, &hctx->state); 2539 /* 2540 * Pairs with the smp_mb() in blk_mq_hctx_stopped() to order the 2541 * clearing of BLK_MQ_S_STOPPED above and the checking of dispatch 2542 * list in the subsequent routine. 2543 */ 2544 smp_mb__after_atomic(); 2545 blk_mq_run_hw_queue(hctx, async); 2546 } 2547 EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue); 2548 2549 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async) 2550 { 2551 struct blk_mq_hw_ctx *hctx; 2552 unsigned long i; 2553 2554 queue_for_each_hw_ctx(q, hctx, i) 2555 blk_mq_start_stopped_hw_queue(hctx, async || 2556 (hctx->flags & BLK_MQ_F_BLOCKING)); 2557 } 2558 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); 2559 2560 static void blk_mq_run_work_fn(struct work_struct *work) 2561 { 2562 struct blk_mq_hw_ctx *hctx = 2563 container_of(work, struct blk_mq_hw_ctx, run_work.work); 2564 2565 blk_mq_run_dispatch_ops(hctx->queue, 2566 blk_mq_sched_dispatch_requests(hctx)); 2567 } 2568 2569 /** 2570 * blk_mq_request_bypass_insert - Insert a request at dispatch list. 2571 * @rq: Pointer to request to be inserted. 2572 * @flags: BLK_MQ_INSERT_* 2573 * 2574 * Should only be used carefully, when the caller knows we want to 2575 * bypass a potential IO scheduler on the target device. 2576 */ 2577 static void blk_mq_request_bypass_insert(struct request *rq, blk_insert_t flags) 2578 { 2579 struct blk_mq_hw_ctx *hctx = rq->mq_hctx; 2580 2581 spin_lock(&hctx->lock); 2582 if (flags & BLK_MQ_INSERT_AT_HEAD) 2583 list_add(&rq->queuelist, &hctx->dispatch); 2584 else 2585 list_add_tail(&rq->queuelist, &hctx->dispatch); 2586 spin_unlock(&hctx->lock); 2587 } 2588 2589 static void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, 2590 struct blk_mq_ctx *ctx, struct list_head *list, 2591 bool run_queue_async) 2592 { 2593 struct request *rq; 2594 enum hctx_type type = hctx->type; 2595 2596 /* 2597 * Try to issue requests directly if the hw queue isn't busy to save an 2598 * extra enqueue & dequeue to the sw queue. 2599 */ 2600 if (!hctx->dispatch_busy && !run_queue_async) { 2601 blk_mq_run_dispatch_ops(hctx->queue, 2602 blk_mq_try_issue_list_directly(hctx, list)); 2603 if (list_empty(list)) 2604 goto out; 2605 } 2606 2607 /* 2608 * preemption doesn't flush plug list, so it's possible ctx->cpu is 2609 * offline now 2610 */ 2611 list_for_each_entry(rq, list, queuelist) { 2612 BUG_ON(rq->mq_ctx != ctx); 2613 trace_block_rq_insert(rq); 2614 if (rq->cmd_flags & REQ_NOWAIT) 2615 run_queue_async = true; 2616 } 2617 2618 spin_lock(&ctx->lock); 2619 list_splice_tail_init(list, &ctx->rq_lists[type]); 2620 blk_mq_hctx_mark_pending(hctx, ctx); 2621 spin_unlock(&ctx->lock); 2622 out: 2623 blk_mq_run_hw_queue(hctx, run_queue_async); 2624 } 2625 2626 static void blk_mq_insert_request(struct request *rq, blk_insert_t flags) 2627 { 2628 struct request_queue *q = rq->q; 2629 struct blk_mq_ctx *ctx = rq->mq_ctx; 2630 struct blk_mq_hw_ctx *hctx = rq->mq_hctx; 2631 2632 if (blk_rq_is_passthrough(rq)) { 2633 /* 2634 * Passthrough request have to be added to hctx->dispatch 2635 * directly. The device may be in a situation where it can't 2636 * handle FS request, and always returns BLK_STS_RESOURCE for 2637 * them, which gets them added to hctx->dispatch. 2638 * 2639 * If a passthrough request is required to unblock the queues, 2640 * and it is added to the scheduler queue, there is no chance to 2641 * dispatch it given we prioritize requests in hctx->dispatch. 2642 */ 2643 blk_mq_request_bypass_insert(rq, flags); 2644 } else if (req_op(rq) == REQ_OP_FLUSH) { 2645 /* 2646 * Firstly normal IO request is inserted to scheduler queue or 2647 * sw queue, meantime we add flush request to dispatch queue( 2648 * hctx->dispatch) directly and there is at most one in-flight 2649 * flush request for each hw queue, so it doesn't matter to add 2650 * flush request to tail or front of the dispatch queue. 2651 * 2652 * Secondly in case of NCQ, flush request belongs to non-NCQ 2653 * command, and queueing it will fail when there is any 2654 * in-flight normal IO request(NCQ command). When adding flush 2655 * rq to the front of hctx->dispatch, it is easier to introduce 2656 * extra time to flush rq's latency because of S_SCHED_RESTART 2657 * compared with adding to the tail of dispatch queue, then 2658 * chance of flush merge is increased, and less flush requests 2659 * will be issued to controller. It is observed that ~10% time 2660 * is saved in blktests block/004 on disk attached to AHCI/NCQ 2661 * drive when adding flush rq to the front of hctx->dispatch. 2662 * 2663 * Simply queue flush rq to the front of hctx->dispatch so that 2664 * intensive flush workloads can benefit in case of NCQ HW. 2665 */ 2666 blk_mq_request_bypass_insert(rq, BLK_MQ_INSERT_AT_HEAD); 2667 } else if (q->elevator) { 2668 LIST_HEAD(list); 2669 2670 WARN_ON_ONCE(rq->tag != BLK_MQ_NO_TAG); 2671 2672 list_add(&rq->queuelist, &list); 2673 q->elevator->type->ops.insert_requests(hctx, &list, flags); 2674 } else { 2675 trace_block_rq_insert(rq); 2676 2677 spin_lock(&ctx->lock); 2678 if (flags & BLK_MQ_INSERT_AT_HEAD) 2679 list_add(&rq->queuelist, &ctx->rq_lists[hctx->type]); 2680 else 2681 list_add_tail(&rq->queuelist, 2682 &ctx->rq_lists[hctx->type]); 2683 blk_mq_hctx_mark_pending(hctx, ctx); 2684 spin_unlock(&ctx->lock); 2685 } 2686 } 2687 2688 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio, 2689 unsigned int nr_segs) 2690 { 2691 int err; 2692 2693 if (bio->bi_opf & REQ_RAHEAD) 2694 rq->cmd_flags |= REQ_FAILFAST_MASK; 2695 2696 rq->bio = rq->biotail = bio; 2697 rq->__sector = bio->bi_iter.bi_sector; 2698 rq->__data_len = bio->bi_iter.bi_size; 2699 rq->phys_gap_bit = bio->bi_bvec_gap_bit; 2700 2701 rq->nr_phys_segments = nr_segs; 2702 if (bio_integrity(bio)) 2703 rq->nr_integrity_segments = blk_rq_count_integrity_sg(rq->q, 2704 bio); 2705 2706 /* This can't fail, since GFP_NOIO includes __GFP_DIRECT_RECLAIM. */ 2707 err = blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO); 2708 WARN_ON_ONCE(err); 2709 2710 blk_account_io_start(rq); 2711 } 2712 2713 static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx, 2714 struct request *rq, bool last) 2715 { 2716 struct request_queue *q = rq->q; 2717 struct blk_mq_queue_data bd = { 2718 .rq = rq, 2719 .last = last, 2720 }; 2721 blk_status_t ret; 2722 2723 /* 2724 * For OK queue, we are done. For error, caller may kill it. 2725 * Any other error (busy), just add it to our list as we 2726 * previously would have done. 2727 */ 2728 ret = q->mq_ops->queue_rq(hctx, &bd); 2729 switch (ret) { 2730 case BLK_STS_OK: 2731 blk_mq_update_dispatch_busy(hctx, false); 2732 break; 2733 case BLK_STS_RESOURCE: 2734 case BLK_STS_DEV_RESOURCE: 2735 blk_mq_update_dispatch_busy(hctx, true); 2736 __blk_mq_requeue_request(rq); 2737 break; 2738 default: 2739 blk_mq_update_dispatch_busy(hctx, false); 2740 break; 2741 } 2742 2743 return ret; 2744 } 2745 2746 static bool blk_mq_get_budget_and_tag(struct request *rq) 2747 { 2748 int budget_token; 2749 2750 budget_token = blk_mq_get_dispatch_budget(rq->q); 2751 if (budget_token < 0) 2752 return false; 2753 blk_mq_set_rq_budget_token(rq, budget_token); 2754 if (!blk_mq_get_driver_tag(rq)) { 2755 blk_mq_put_dispatch_budget(rq->q, budget_token); 2756 return false; 2757 } 2758 return true; 2759 } 2760 2761 /** 2762 * blk_mq_try_issue_directly - Try to send a request directly to device driver. 2763 * @hctx: Pointer of the associated hardware queue. 2764 * @rq: Pointer to request to be sent. 2765 * 2766 * If the device has enough resources to accept a new request now, send the 2767 * request directly to device driver. Else, insert at hctx->dispatch queue, so 2768 * we can try send it another time in the future. Requests inserted at this 2769 * queue have higher priority. 2770 */ 2771 static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, 2772 struct request *rq) 2773 { 2774 blk_status_t ret; 2775 2776 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(rq->q)) { 2777 blk_mq_insert_request(rq, 0); 2778 blk_mq_run_hw_queue(hctx, false); 2779 return; 2780 } 2781 2782 if ((rq->rq_flags & RQF_USE_SCHED) || !blk_mq_get_budget_and_tag(rq)) { 2783 blk_mq_insert_request(rq, 0); 2784 blk_mq_run_hw_queue(hctx, rq->cmd_flags & REQ_NOWAIT); 2785 return; 2786 } 2787 2788 ret = __blk_mq_issue_directly(hctx, rq, true); 2789 switch (ret) { 2790 case BLK_STS_OK: 2791 break; 2792 case BLK_STS_RESOURCE: 2793 case BLK_STS_DEV_RESOURCE: 2794 blk_mq_request_bypass_insert(rq, 0); 2795 blk_mq_run_hw_queue(hctx, false); 2796 break; 2797 default: 2798 blk_mq_end_request(rq, ret); 2799 break; 2800 } 2801 } 2802 2803 static blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last) 2804 { 2805 struct blk_mq_hw_ctx *hctx = rq->mq_hctx; 2806 2807 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(rq->q)) { 2808 blk_mq_insert_request(rq, 0); 2809 blk_mq_run_hw_queue(hctx, false); 2810 return BLK_STS_OK; 2811 } 2812 2813 if (!blk_mq_get_budget_and_tag(rq)) 2814 return BLK_STS_RESOURCE; 2815 return __blk_mq_issue_directly(hctx, rq, last); 2816 } 2817 2818 static void blk_mq_issue_direct(struct rq_list *rqs) 2819 { 2820 struct blk_mq_hw_ctx *hctx = NULL; 2821 struct request *rq; 2822 int queued = 0; 2823 blk_status_t ret = BLK_STS_OK; 2824 2825 while ((rq = rq_list_pop(rqs))) { 2826 bool last = rq_list_empty(rqs); 2827 2828 if (hctx != rq->mq_hctx) { 2829 if (hctx) { 2830 blk_mq_commit_rqs(hctx, queued, false); 2831 queued = 0; 2832 } 2833 hctx = rq->mq_hctx; 2834 } 2835 2836 ret = blk_mq_request_issue_directly(rq, last); 2837 switch (ret) { 2838 case BLK_STS_OK: 2839 queued++; 2840 break; 2841 case BLK_STS_RESOURCE: 2842 case BLK_STS_DEV_RESOURCE: 2843 blk_mq_request_bypass_insert(rq, 0); 2844 blk_mq_run_hw_queue(hctx, false); 2845 goto out; 2846 default: 2847 blk_mq_end_request(rq, ret); 2848 break; 2849 } 2850 } 2851 2852 out: 2853 if (ret != BLK_STS_OK) 2854 blk_mq_commit_rqs(hctx, queued, false); 2855 } 2856 2857 static void __blk_mq_flush_list(struct request_queue *q, struct rq_list *rqs) 2858 { 2859 if (blk_queue_quiesced(q)) 2860 return; 2861 q->mq_ops->queue_rqs(rqs); 2862 } 2863 2864 static unsigned blk_mq_extract_queue_requests(struct rq_list *rqs, 2865 struct rq_list *queue_rqs) 2866 { 2867 struct request *rq = rq_list_pop(rqs); 2868 struct request_queue *this_q = rq->q; 2869 struct request **prev = &rqs->head; 2870 struct rq_list matched_rqs = {}; 2871 struct request *last = NULL; 2872 unsigned depth = 1; 2873 2874 rq_list_add_tail(&matched_rqs, rq); 2875 while ((rq = *prev)) { 2876 if (rq->q == this_q) { 2877 /* move rq from rqs to matched_rqs */ 2878 *prev = rq->rq_next; 2879 rq_list_add_tail(&matched_rqs, rq); 2880 depth++; 2881 } else { 2882 /* leave rq in rqs */ 2883 prev = &rq->rq_next; 2884 last = rq; 2885 } 2886 } 2887 2888 rqs->tail = last; 2889 *queue_rqs = matched_rqs; 2890 return depth; 2891 } 2892 2893 static void blk_mq_dispatch_queue_requests(struct rq_list *rqs, unsigned depth) 2894 { 2895 struct request_queue *q = rq_list_peek(rqs)->q; 2896 2897 trace_block_unplug(q, depth, true); 2898 2899 /* 2900 * Peek first request and see if we have a ->queue_rqs() hook. 2901 * If we do, we can dispatch the whole list in one go. 2902 * We already know at this point that all requests belong to the 2903 * same queue, caller must ensure that's the case. 2904 */ 2905 if (q->mq_ops->queue_rqs) { 2906 blk_mq_run_dispatch_ops(q, __blk_mq_flush_list(q, rqs)); 2907 if (rq_list_empty(rqs)) 2908 return; 2909 } 2910 2911 blk_mq_run_dispatch_ops(q, blk_mq_issue_direct(rqs)); 2912 } 2913 2914 static void blk_mq_dispatch_list(struct rq_list *rqs, bool from_sched) 2915 { 2916 struct blk_mq_hw_ctx *this_hctx = NULL; 2917 struct blk_mq_ctx *this_ctx = NULL; 2918 struct rq_list requeue_list = {}; 2919 unsigned int depth = 0; 2920 bool is_passthrough = false; 2921 LIST_HEAD(list); 2922 2923 do { 2924 struct request *rq = rq_list_pop(rqs); 2925 2926 if (!this_hctx) { 2927 this_hctx = rq->mq_hctx; 2928 this_ctx = rq->mq_ctx; 2929 is_passthrough = blk_rq_is_passthrough(rq); 2930 } else if (this_hctx != rq->mq_hctx || this_ctx != rq->mq_ctx || 2931 is_passthrough != blk_rq_is_passthrough(rq)) { 2932 rq_list_add_tail(&requeue_list, rq); 2933 continue; 2934 } 2935 list_add_tail(&rq->queuelist, &list); 2936 depth++; 2937 } while (!rq_list_empty(rqs)); 2938 2939 *rqs = requeue_list; 2940 trace_block_unplug(this_hctx->queue, depth, !from_sched); 2941 2942 percpu_ref_get(&this_hctx->queue->q_usage_counter); 2943 /* passthrough requests should never be issued to the I/O scheduler */ 2944 if (is_passthrough) { 2945 spin_lock(&this_hctx->lock); 2946 list_splice_tail_init(&list, &this_hctx->dispatch); 2947 spin_unlock(&this_hctx->lock); 2948 blk_mq_run_hw_queue(this_hctx, from_sched); 2949 } else if (this_hctx->queue->elevator) { 2950 this_hctx->queue->elevator->type->ops.insert_requests(this_hctx, 2951 &list, 0); 2952 blk_mq_run_hw_queue(this_hctx, from_sched); 2953 } else { 2954 blk_mq_insert_requests(this_hctx, this_ctx, &list, from_sched); 2955 } 2956 percpu_ref_put(&this_hctx->queue->q_usage_counter); 2957 } 2958 2959 static void blk_mq_dispatch_multiple_queue_requests(struct rq_list *rqs) 2960 { 2961 do { 2962 struct rq_list queue_rqs; 2963 unsigned depth; 2964 2965 depth = blk_mq_extract_queue_requests(rqs, &queue_rqs); 2966 blk_mq_dispatch_queue_requests(&queue_rqs, depth); 2967 while (!rq_list_empty(&queue_rqs)) 2968 blk_mq_dispatch_list(&queue_rqs, false); 2969 } while (!rq_list_empty(rqs)); 2970 } 2971 2972 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) 2973 { 2974 unsigned int depth; 2975 2976 /* 2977 * We may have been called recursively midway through handling 2978 * plug->mq_list via a schedule() in the driver's queue_rq() callback. 2979 * To avoid mq_list changing under our feet, clear rq_count early and 2980 * bail out specifically if rq_count is 0 rather than checking 2981 * whether the mq_list is empty. 2982 */ 2983 if (plug->rq_count == 0) 2984 return; 2985 depth = plug->rq_count; 2986 plug->rq_count = 0; 2987 2988 if (!plug->has_elevator && !from_schedule) { 2989 if (plug->multiple_queues) { 2990 blk_mq_dispatch_multiple_queue_requests(&plug->mq_list); 2991 return; 2992 } 2993 2994 blk_mq_dispatch_queue_requests(&plug->mq_list, depth); 2995 if (rq_list_empty(&plug->mq_list)) 2996 return; 2997 } 2998 2999 do { 3000 blk_mq_dispatch_list(&plug->mq_list, from_schedule); 3001 } while (!rq_list_empty(&plug->mq_list)); 3002 } 3003 3004 static void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx, 3005 struct list_head *list) 3006 { 3007 int queued = 0; 3008 blk_status_t ret = BLK_STS_OK; 3009 3010 while (!list_empty(list)) { 3011 struct request *rq = list_first_entry(list, struct request, 3012 queuelist); 3013 3014 list_del_init(&rq->queuelist); 3015 ret = blk_mq_request_issue_directly(rq, list_empty(list)); 3016 switch (ret) { 3017 case BLK_STS_OK: 3018 queued++; 3019 break; 3020 case BLK_STS_RESOURCE: 3021 case BLK_STS_DEV_RESOURCE: 3022 blk_mq_request_bypass_insert(rq, 0); 3023 if (list_empty(list)) 3024 blk_mq_run_hw_queue(hctx, false); 3025 goto out; 3026 default: 3027 blk_mq_end_request(rq, ret); 3028 break; 3029 } 3030 } 3031 3032 out: 3033 if (ret != BLK_STS_OK) 3034 blk_mq_commit_rqs(hctx, queued, false); 3035 } 3036 3037 static bool blk_mq_attempt_bio_merge(struct request_queue *q, 3038 struct bio *bio, unsigned int nr_segs) 3039 { 3040 if (!blk_queue_nomerges(q) && bio_mergeable(bio)) { 3041 if (blk_attempt_plug_merge(q, bio, nr_segs)) 3042 return true; 3043 if (blk_mq_sched_bio_merge(q, bio, nr_segs)) 3044 return true; 3045 } 3046 return false; 3047 } 3048 3049 static struct request *blk_mq_get_new_requests(struct request_queue *q, 3050 struct blk_plug *plug, 3051 struct bio *bio) 3052 { 3053 struct blk_mq_alloc_data data = { 3054 .q = q, 3055 .flags = 0, 3056 .shallow_depth = 0, 3057 .cmd_flags = bio->bi_opf, 3058 .rq_flags = 0, 3059 .nr_tags = 1, 3060 .cached_rqs = NULL, 3061 .ctx = NULL, 3062 .hctx = NULL 3063 }; 3064 struct request *rq; 3065 3066 rq_qos_throttle(q, bio); 3067 3068 if (plug) { 3069 data.nr_tags = plug->nr_ios; 3070 plug->nr_ios = 1; 3071 data.cached_rqs = &plug->cached_rqs; 3072 } 3073 3074 rq = __blk_mq_alloc_requests(&data); 3075 if (unlikely(!rq)) 3076 rq_qos_cleanup(q, bio); 3077 return rq; 3078 } 3079 3080 /* 3081 * Check if there is a suitable cached request and return it. 3082 */ 3083 static struct request *blk_mq_peek_cached_request(struct blk_plug *plug, 3084 struct request_queue *q, blk_opf_t opf) 3085 { 3086 enum hctx_type type = blk_mq_get_hctx_type(opf); 3087 struct request *rq; 3088 3089 if (!plug) 3090 return NULL; 3091 rq = rq_list_peek(&plug->cached_rqs); 3092 if (!rq || rq->q != q) 3093 return NULL; 3094 if (type != rq->mq_hctx->type && 3095 (type != HCTX_TYPE_READ || rq->mq_hctx->type != HCTX_TYPE_DEFAULT)) 3096 return NULL; 3097 if (op_is_flush(rq->cmd_flags) != op_is_flush(opf)) 3098 return NULL; 3099 return rq; 3100 } 3101 3102 static void blk_mq_use_cached_rq(struct request *rq, struct blk_plug *plug, 3103 struct bio *bio) 3104 { 3105 if (rq_list_pop(&plug->cached_rqs) != rq) 3106 WARN_ON_ONCE(1); 3107 3108 /* 3109 * If any qos ->throttle() end up blocking, we will have flushed the 3110 * plug and hence killed the cached_rq list as well. Pop this entry 3111 * before we throttle. 3112 */ 3113 rq_qos_throttle(rq->q, bio); 3114 3115 blk_mq_rq_time_init(rq, blk_time_get_ns()); 3116 rq->cmd_flags = bio->bi_opf; 3117 INIT_LIST_HEAD(&rq->queuelist); 3118 } 3119 3120 static bool bio_unaligned(const struct bio *bio, struct request_queue *q) 3121 { 3122 unsigned int bs_mask = queue_logical_block_size(q) - 1; 3123 3124 /* .bi_sector of any zero sized bio need to be initialized */ 3125 if ((bio->bi_iter.bi_size & bs_mask) || 3126 ((bio->bi_iter.bi_sector << SECTOR_SHIFT) & bs_mask)) 3127 return true; 3128 return false; 3129 } 3130 3131 /** 3132 * blk_mq_submit_bio - Create and send a request to block device. 3133 * @bio: Bio pointer. 3134 * 3135 * Builds up a request structure from @q and @bio and send to the device. The 3136 * request may not be queued directly to hardware if: 3137 * * This request can be merged with another one 3138 * * We want to place request at plug queue for possible future merging 3139 * * There is an IO scheduler active at this queue 3140 * 3141 * It will not queue the request if there is an error with the bio, or at the 3142 * request creation. 3143 */ 3144 void blk_mq_submit_bio(struct bio *bio) 3145 { 3146 struct request_queue *q = bdev_get_queue(bio->bi_bdev); 3147 struct blk_plug *plug = current->plug; 3148 const int is_sync = op_is_sync(bio->bi_opf); 3149 struct blk_mq_hw_ctx *hctx; 3150 unsigned int nr_segs; 3151 struct request *rq; 3152 blk_status_t ret; 3153 3154 /* 3155 * If the plug has a cached request for this queue, try to use it. 3156 */ 3157 rq = blk_mq_peek_cached_request(plug, q, bio->bi_opf); 3158 3159 /* 3160 * A BIO that was released from a zone write plug has already been 3161 * through the preparation in this function, already holds a reference 3162 * on the queue usage counter, and is the only write BIO in-flight for 3163 * the target zone. Go straight to preparing a request for it. 3164 */ 3165 if (bio_zone_write_plugging(bio)) { 3166 nr_segs = bio->__bi_nr_segments; 3167 if (rq) 3168 blk_queue_exit(q); 3169 goto new_request; 3170 } 3171 3172 /* 3173 * The cached request already holds a q_usage_counter reference and we 3174 * don't have to acquire a new one if we use it. 3175 */ 3176 if (!rq) { 3177 if (unlikely(bio_queue_enter(bio))) 3178 return; 3179 } 3180 3181 /* 3182 * Device reconfiguration may change logical block size or reduce the 3183 * number of poll queues, so the checks for alignment and poll support 3184 * have to be done with queue usage counter held. 3185 */ 3186 if (unlikely(bio_unaligned(bio, q))) { 3187 bio_io_error(bio); 3188 goto queue_exit; 3189 } 3190 3191 if ((bio->bi_opf & REQ_POLLED) && !blk_mq_can_poll(q)) { 3192 bio->bi_status = BLK_STS_NOTSUPP; 3193 bio_endio(bio); 3194 goto queue_exit; 3195 } 3196 3197 bio = __bio_split_to_limits(bio, &q->limits, &nr_segs); 3198 if (!bio) 3199 goto queue_exit; 3200 3201 if (!bio_integrity_prep(bio)) 3202 goto queue_exit; 3203 3204 blk_mq_bio_issue_init(q, bio); 3205 if (blk_mq_attempt_bio_merge(q, bio, nr_segs)) 3206 goto queue_exit; 3207 3208 if (bio_needs_zone_write_plugging(bio)) { 3209 if (blk_zone_plug_bio(bio, nr_segs)) 3210 goto queue_exit; 3211 } 3212 3213 new_request: 3214 if (rq) { 3215 blk_mq_use_cached_rq(rq, plug, bio); 3216 } else { 3217 rq = blk_mq_get_new_requests(q, plug, bio); 3218 if (unlikely(!rq)) { 3219 if (bio->bi_opf & REQ_NOWAIT) 3220 bio_wouldblock_error(bio); 3221 goto queue_exit; 3222 } 3223 } 3224 3225 trace_block_getrq(bio); 3226 3227 rq_qos_track(q, rq, bio); 3228 3229 blk_mq_bio_to_request(rq, bio, nr_segs); 3230 3231 ret = blk_crypto_rq_get_keyslot(rq); 3232 if (ret != BLK_STS_OK) { 3233 bio->bi_status = ret; 3234 bio_endio(bio); 3235 blk_mq_free_request(rq); 3236 return; 3237 } 3238 3239 if (bio_zone_write_plugging(bio)) 3240 blk_zone_write_plug_init_request(rq); 3241 3242 if (op_is_flush(bio->bi_opf) && blk_insert_flush(rq)) 3243 return; 3244 3245 if (plug) { 3246 blk_add_rq_to_plug(plug, rq); 3247 return; 3248 } 3249 3250 hctx = rq->mq_hctx; 3251 if ((rq->rq_flags & RQF_USE_SCHED) || 3252 (hctx->dispatch_busy && (q->nr_hw_queues == 1 || !is_sync))) { 3253 blk_mq_insert_request(rq, 0); 3254 blk_mq_run_hw_queue(hctx, true); 3255 } else { 3256 blk_mq_run_dispatch_ops(q, blk_mq_try_issue_directly(hctx, rq)); 3257 } 3258 return; 3259 3260 queue_exit: 3261 /* 3262 * Don't drop the queue reference if we were trying to use a cached 3263 * request and thus didn't acquire one. 3264 */ 3265 if (!rq) 3266 blk_queue_exit(q); 3267 } 3268 3269 #ifdef CONFIG_BLK_MQ_STACKING 3270 /** 3271 * blk_insert_cloned_request - Helper for stacking drivers to submit a request 3272 * @rq: the request being queued 3273 */ 3274 blk_status_t blk_insert_cloned_request(struct request *rq) 3275 { 3276 struct request_queue *q = rq->q; 3277 unsigned int max_sectors = blk_queue_get_max_sectors(rq); 3278 unsigned int max_segments = blk_rq_get_max_segments(rq); 3279 blk_status_t ret; 3280 3281 if (blk_rq_sectors(rq) > max_sectors) { 3282 /* 3283 * SCSI device does not have a good way to return if 3284 * Write Same/Zero is actually supported. If a device rejects 3285 * a non-read/write command (discard, write same,etc.) the 3286 * low-level device driver will set the relevant queue limit to 3287 * 0 to prevent blk-lib from issuing more of the offending 3288 * operations. Commands queued prior to the queue limit being 3289 * reset need to be completed with BLK_STS_NOTSUPP to avoid I/O 3290 * errors being propagated to upper layers. 3291 */ 3292 if (max_sectors == 0) 3293 return BLK_STS_NOTSUPP; 3294 3295 printk(KERN_ERR "%s: over max size limit. (%u > %u)\n", 3296 __func__, blk_rq_sectors(rq), max_sectors); 3297 return BLK_STS_IOERR; 3298 } 3299 3300 /* 3301 * The queue settings related to segment counting may differ from the 3302 * original queue. 3303 */ 3304 rq->nr_phys_segments = blk_recalc_rq_segments(rq); 3305 if (rq->nr_phys_segments > max_segments) { 3306 printk(KERN_ERR "%s: over max segments limit. (%u > %u)\n", 3307 __func__, rq->nr_phys_segments, max_segments); 3308 return BLK_STS_IOERR; 3309 } 3310 3311 if (q->disk && should_fail_request(q->disk->part0, blk_rq_bytes(rq))) 3312 return BLK_STS_IOERR; 3313 3314 ret = blk_crypto_rq_get_keyslot(rq); 3315 if (ret != BLK_STS_OK) 3316 return ret; 3317 3318 blk_account_io_start(rq); 3319 3320 /* 3321 * Since we have a scheduler attached on the top device, 3322 * bypass a potential scheduler on the bottom device for 3323 * insert. 3324 */ 3325 blk_mq_run_dispatch_ops(q, 3326 ret = blk_mq_request_issue_directly(rq, true)); 3327 if (ret) 3328 blk_account_io_done(rq, blk_time_get_ns()); 3329 return ret; 3330 } 3331 EXPORT_SYMBOL_GPL(blk_insert_cloned_request); 3332 3333 /** 3334 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request 3335 * @rq: the clone request to be cleaned up 3336 * 3337 * Description: 3338 * Free all bios in @rq for a cloned request. 3339 */ 3340 void blk_rq_unprep_clone(struct request *rq) 3341 { 3342 struct bio *bio; 3343 3344 while ((bio = rq->bio) != NULL) { 3345 rq->bio = bio->bi_next; 3346 3347 bio_put(bio); 3348 } 3349 } 3350 EXPORT_SYMBOL_GPL(blk_rq_unprep_clone); 3351 3352 /** 3353 * blk_rq_prep_clone - Helper function to setup clone request 3354 * @rq: the request to be setup 3355 * @rq_src: original request to be cloned 3356 * @bs: bio_set that bios for clone are allocated from 3357 * @gfp_mask: memory allocation mask for bio 3358 * @bio_ctr: setup function to be called for each clone bio. 3359 * Returns %0 for success, non %0 for failure. 3360 * @data: private data to be passed to @bio_ctr 3361 * 3362 * Description: 3363 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq. 3364 * Also, pages which the original bios are pointing to are not copied 3365 * and the cloned bios just point same pages. 3366 * So cloned bios must be completed before original bios, which means 3367 * the caller must complete @rq before @rq_src. 3368 */ 3369 int blk_rq_prep_clone(struct request *rq, struct request *rq_src, 3370 struct bio_set *bs, gfp_t gfp_mask, 3371 int (*bio_ctr)(struct bio *, struct bio *, void *), 3372 void *data) 3373 { 3374 struct bio *bio_src; 3375 3376 if (!bs) 3377 bs = &fs_bio_set; 3378 3379 __rq_for_each_bio(bio_src, rq_src) { 3380 struct bio *bio = bio_alloc_clone(rq->q->disk->part0, bio_src, 3381 gfp_mask, bs); 3382 if (!bio) 3383 goto free_and_out; 3384 3385 if (bio_ctr && bio_ctr(bio, bio_src, data)) { 3386 bio_put(bio); 3387 goto free_and_out; 3388 } 3389 3390 if (rq->bio) { 3391 rq->biotail->bi_next = bio; 3392 rq->biotail = bio; 3393 } else { 3394 rq->bio = rq->biotail = bio; 3395 } 3396 } 3397 3398 /* Copy attributes of the original request to the clone request. */ 3399 rq->__sector = blk_rq_pos(rq_src); 3400 rq->__data_len = blk_rq_bytes(rq_src); 3401 if (rq_src->rq_flags & RQF_SPECIAL_PAYLOAD) { 3402 rq->rq_flags |= RQF_SPECIAL_PAYLOAD; 3403 rq->special_vec = rq_src->special_vec; 3404 } 3405 rq->nr_phys_segments = rq_src->nr_phys_segments; 3406 rq->nr_integrity_segments = rq_src->nr_integrity_segments; 3407 rq->phys_gap_bit = rq_src->phys_gap_bit; 3408 3409 if (rq->bio && blk_crypto_rq_bio_prep(rq, rq->bio, gfp_mask) < 0) 3410 goto free_and_out; 3411 3412 return 0; 3413 3414 free_and_out: 3415 blk_rq_unprep_clone(rq); 3416 3417 return -ENOMEM; 3418 } 3419 EXPORT_SYMBOL_GPL(blk_rq_prep_clone); 3420 #endif /* CONFIG_BLK_MQ_STACKING */ 3421 3422 /* 3423 * Steal bios from a request and add them to a bio list. 3424 * The request must not have been partially completed before. 3425 */ 3426 void blk_steal_bios(struct bio_list *list, struct request *rq) 3427 { 3428 if (rq->bio) { 3429 if (list->tail) 3430 list->tail->bi_next = rq->bio; 3431 else 3432 list->head = rq->bio; 3433 list->tail = rq->biotail; 3434 3435 rq->bio = NULL; 3436 rq->biotail = NULL; 3437 } 3438 3439 rq->__data_len = 0; 3440 } 3441 EXPORT_SYMBOL_GPL(blk_steal_bios); 3442 3443 static size_t order_to_size(unsigned int order) 3444 { 3445 return (size_t)PAGE_SIZE << order; 3446 } 3447 3448 /* called before freeing request pool in @tags */ 3449 static void blk_mq_clear_rq_mapping(struct blk_mq_tags *drv_tags, 3450 struct blk_mq_tags *tags) 3451 { 3452 struct page *page; 3453 3454 /* 3455 * There is no need to clear mapping if driver tags is not initialized 3456 * or the mapping belongs to the driver tags. 3457 */ 3458 if (!drv_tags || drv_tags == tags) 3459 return; 3460 3461 list_for_each_entry(page, &tags->page_list, lru) { 3462 unsigned long start = (unsigned long)page_address(page); 3463 unsigned long end = start + order_to_size(page->private); 3464 int i; 3465 3466 for (i = 0; i < drv_tags->nr_tags; i++) { 3467 struct request *rq = drv_tags->rqs[i]; 3468 unsigned long rq_addr = (unsigned long)rq; 3469 3470 if (rq_addr >= start && rq_addr < end) { 3471 WARN_ON_ONCE(req_ref_read(rq) != 0); 3472 cmpxchg(&drv_tags->rqs[i], rq, NULL); 3473 } 3474 } 3475 } 3476 } 3477 3478 void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags, 3479 unsigned int hctx_idx) 3480 { 3481 struct blk_mq_tags *drv_tags; 3482 3483 if (list_empty(&tags->page_list)) 3484 return; 3485 3486 if (blk_mq_is_shared_tags(set->flags)) 3487 drv_tags = set->shared_tags; 3488 else 3489 drv_tags = set->tags[hctx_idx]; 3490 3491 if (tags->static_rqs && set->ops->exit_request) { 3492 int i; 3493 3494 for (i = 0; i < tags->nr_tags; i++) { 3495 struct request *rq = tags->static_rqs[i]; 3496 3497 if (!rq) 3498 continue; 3499 set->ops->exit_request(set, rq, hctx_idx); 3500 tags->static_rqs[i] = NULL; 3501 } 3502 } 3503 3504 blk_mq_clear_rq_mapping(drv_tags, tags); 3505 /* 3506 * Free request pages in SRCU callback, which is called from 3507 * blk_mq_free_tags(). 3508 */ 3509 } 3510 3511 void blk_mq_free_rq_map(struct blk_mq_tag_set *set, struct blk_mq_tags *tags) 3512 { 3513 kfree(tags->rqs); 3514 tags->rqs = NULL; 3515 kfree(tags->static_rqs); 3516 tags->static_rqs = NULL; 3517 3518 blk_mq_free_tags(set, tags); 3519 } 3520 3521 static enum hctx_type hctx_idx_to_type(struct blk_mq_tag_set *set, 3522 unsigned int hctx_idx) 3523 { 3524 int i; 3525 3526 for (i = 0; i < set->nr_maps; i++) { 3527 unsigned int start = set->map[i].queue_offset; 3528 unsigned int end = start + set->map[i].nr_queues; 3529 3530 if (hctx_idx >= start && hctx_idx < end) 3531 break; 3532 } 3533 3534 if (i >= set->nr_maps) 3535 i = HCTX_TYPE_DEFAULT; 3536 3537 return i; 3538 } 3539 3540 static int blk_mq_get_hctx_node(struct blk_mq_tag_set *set, 3541 unsigned int hctx_idx) 3542 { 3543 enum hctx_type type = hctx_idx_to_type(set, hctx_idx); 3544 3545 return blk_mq_hw_queue_to_node(&set->map[type], hctx_idx); 3546 } 3547 3548 static struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, 3549 unsigned int hctx_idx, 3550 unsigned int nr_tags, 3551 unsigned int reserved_tags) 3552 { 3553 int node = blk_mq_get_hctx_node(set, hctx_idx); 3554 struct blk_mq_tags *tags; 3555 3556 if (node == NUMA_NO_NODE) 3557 node = set->numa_node; 3558 3559 tags = blk_mq_init_tags(nr_tags, reserved_tags, set->flags, node); 3560 if (!tags) 3561 return NULL; 3562 3563 tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *), 3564 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, 3565 node); 3566 if (!tags->rqs) 3567 goto err_free_tags; 3568 3569 tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *), 3570 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, 3571 node); 3572 if (!tags->static_rqs) 3573 goto err_free_rqs; 3574 3575 return tags; 3576 3577 err_free_rqs: 3578 kfree(tags->rqs); 3579 err_free_tags: 3580 blk_mq_free_tags(set, tags); 3581 return NULL; 3582 } 3583 3584 static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq, 3585 unsigned int hctx_idx, int node) 3586 { 3587 int ret; 3588 3589 if (set->ops->init_request) { 3590 ret = set->ops->init_request(set, rq, hctx_idx, node); 3591 if (ret) 3592 return ret; 3593 } 3594 3595 WRITE_ONCE(rq->state, MQ_RQ_IDLE); 3596 return 0; 3597 } 3598 3599 static int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, 3600 struct blk_mq_tags *tags, 3601 unsigned int hctx_idx, unsigned int depth) 3602 { 3603 unsigned int i, j, entries_per_page, max_order = 4; 3604 int node = blk_mq_get_hctx_node(set, hctx_idx); 3605 size_t rq_size, left; 3606 3607 if (node == NUMA_NO_NODE) 3608 node = set->numa_node; 3609 3610 /* 3611 * rq_size is the size of the request plus driver payload, rounded 3612 * to the cacheline size 3613 */ 3614 rq_size = round_up(sizeof(struct request) + set->cmd_size, 3615 cache_line_size()); 3616 left = rq_size * depth; 3617 3618 for (i = 0; i < depth; ) { 3619 int this_order = max_order; 3620 struct page *page; 3621 int to_do; 3622 void *p; 3623 3624 while (this_order && left < order_to_size(this_order - 1)) 3625 this_order--; 3626 3627 do { 3628 page = alloc_pages_node(node, 3629 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO, 3630 this_order); 3631 if (page) 3632 break; 3633 if (!this_order--) 3634 break; 3635 if (order_to_size(this_order) < rq_size) 3636 break; 3637 } while (1); 3638 3639 if (!page) 3640 goto fail; 3641 3642 page->private = this_order; 3643 list_add_tail(&page->lru, &tags->page_list); 3644 3645 p = page_address(page); 3646 /* 3647 * Allow kmemleak to scan these pages as they contain pointers 3648 * to additional allocations like via ops->init_request(). 3649 */ 3650 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO); 3651 entries_per_page = order_to_size(this_order) / rq_size; 3652 to_do = min(entries_per_page, depth - i); 3653 left -= to_do * rq_size; 3654 for (j = 0; j < to_do; j++) { 3655 struct request *rq = p; 3656 3657 tags->static_rqs[i] = rq; 3658 if (blk_mq_init_request(set, rq, hctx_idx, node)) { 3659 tags->static_rqs[i] = NULL; 3660 goto fail; 3661 } 3662 3663 p += rq_size; 3664 i++; 3665 } 3666 } 3667 return 0; 3668 3669 fail: 3670 blk_mq_free_rqs(set, tags, hctx_idx); 3671 return -ENOMEM; 3672 } 3673 3674 struct rq_iter_data { 3675 struct blk_mq_hw_ctx *hctx; 3676 bool has_rq; 3677 }; 3678 3679 static bool blk_mq_has_request(struct request *rq, void *data) 3680 { 3681 struct rq_iter_data *iter_data = data; 3682 3683 if (rq->mq_hctx != iter_data->hctx) 3684 return true; 3685 iter_data->has_rq = true; 3686 return false; 3687 } 3688 3689 static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx) 3690 { 3691 struct blk_mq_tags *tags = hctx->sched_tags ? 3692 hctx->sched_tags : hctx->tags; 3693 struct rq_iter_data data = { 3694 .hctx = hctx, 3695 }; 3696 int srcu_idx; 3697 3698 srcu_idx = srcu_read_lock(&hctx->queue->tag_set->tags_srcu); 3699 blk_mq_all_tag_iter(tags, blk_mq_has_request, &data); 3700 srcu_read_unlock(&hctx->queue->tag_set->tags_srcu, srcu_idx); 3701 3702 return data.has_rq; 3703 } 3704 3705 static bool blk_mq_hctx_has_online_cpu(struct blk_mq_hw_ctx *hctx, 3706 unsigned int this_cpu) 3707 { 3708 enum hctx_type type = hctx->type; 3709 int cpu; 3710 3711 /* 3712 * hctx->cpumask has to rule out isolated CPUs, but userspace still 3713 * might submit IOs on these isolated CPUs, so use the queue map to 3714 * check if all CPUs mapped to this hctx are offline 3715 */ 3716 for_each_online_cpu(cpu) { 3717 struct blk_mq_hw_ctx *h = blk_mq_map_queue_type(hctx->queue, 3718 type, cpu); 3719 3720 if (h != hctx) 3721 continue; 3722 3723 /* this hctx has at least one online CPU */ 3724 if (this_cpu != cpu) 3725 return true; 3726 } 3727 3728 return false; 3729 } 3730 3731 static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node) 3732 { 3733 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node, 3734 struct blk_mq_hw_ctx, cpuhp_online); 3735 int ret = 0; 3736 3737 if (!hctx->nr_ctx || blk_mq_hctx_has_online_cpu(hctx, cpu)) 3738 return 0; 3739 3740 /* 3741 * Prevent new request from being allocated on the current hctx. 3742 * 3743 * The smp_mb__after_atomic() Pairs with the implied barrier in 3744 * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is 3745 * seen once we return from the tag allocator. 3746 */ 3747 set_bit(BLK_MQ_S_INACTIVE, &hctx->state); 3748 smp_mb__after_atomic(); 3749 3750 /* 3751 * Try to grab a reference to the queue and wait for any outstanding 3752 * requests. If we could not grab a reference the queue has been 3753 * frozen and there are no requests. 3754 */ 3755 if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) { 3756 while (blk_mq_hctx_has_requests(hctx)) { 3757 /* 3758 * The wakeup capable IRQ handler of block device is 3759 * not called during suspend. Skip the loop by checking 3760 * pm_wakeup_pending to prevent the deadlock and improve 3761 * suspend latency. 3762 */ 3763 if (pm_wakeup_pending()) { 3764 clear_bit(BLK_MQ_S_INACTIVE, &hctx->state); 3765 ret = -EBUSY; 3766 break; 3767 } 3768 msleep(5); 3769 } 3770 percpu_ref_put(&hctx->queue->q_usage_counter); 3771 } 3772 3773 return ret; 3774 } 3775 3776 /* 3777 * Check if one CPU is mapped to the specified hctx 3778 * 3779 * Isolated CPUs have been ruled out from hctx->cpumask, which is supposed 3780 * to be used for scheduling kworker only. For other usage, please call this 3781 * helper for checking if one CPU belongs to the specified hctx 3782 */ 3783 static bool blk_mq_cpu_mapped_to_hctx(unsigned int cpu, 3784 const struct blk_mq_hw_ctx *hctx) 3785 { 3786 struct blk_mq_hw_ctx *mapped_hctx = blk_mq_map_queue_type(hctx->queue, 3787 hctx->type, cpu); 3788 3789 return mapped_hctx == hctx; 3790 } 3791 3792 static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node) 3793 { 3794 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node, 3795 struct blk_mq_hw_ctx, cpuhp_online); 3796 3797 if (blk_mq_cpu_mapped_to_hctx(cpu, hctx)) 3798 clear_bit(BLK_MQ_S_INACTIVE, &hctx->state); 3799 return 0; 3800 } 3801 3802 /* 3803 * 'cpu' is going away. splice any existing rq_list entries from this 3804 * software queue to the hw queue dispatch list, and ensure that it 3805 * gets run. 3806 */ 3807 static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node) 3808 { 3809 struct blk_mq_hw_ctx *hctx; 3810 struct blk_mq_ctx *ctx; 3811 LIST_HEAD(tmp); 3812 enum hctx_type type; 3813 3814 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead); 3815 if (!blk_mq_cpu_mapped_to_hctx(cpu, hctx)) 3816 return 0; 3817 3818 ctx = __blk_mq_get_ctx(hctx->queue, cpu); 3819 type = hctx->type; 3820 3821 spin_lock(&ctx->lock); 3822 if (!list_empty(&ctx->rq_lists[type])) { 3823 list_splice_init(&ctx->rq_lists[type], &tmp); 3824 blk_mq_hctx_clear_pending(hctx, ctx); 3825 } 3826 spin_unlock(&ctx->lock); 3827 3828 if (list_empty(&tmp)) 3829 return 0; 3830 3831 spin_lock(&hctx->lock); 3832 list_splice_tail_init(&tmp, &hctx->dispatch); 3833 spin_unlock(&hctx->lock); 3834 3835 blk_mq_run_hw_queue(hctx, true); 3836 return 0; 3837 } 3838 3839 static void __blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx) 3840 { 3841 lockdep_assert_held(&blk_mq_cpuhp_lock); 3842 3843 if (!(hctx->flags & BLK_MQ_F_STACKING) && 3844 !hlist_unhashed(&hctx->cpuhp_online)) { 3845 cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE, 3846 &hctx->cpuhp_online); 3847 INIT_HLIST_NODE(&hctx->cpuhp_online); 3848 } 3849 3850 if (!hlist_unhashed(&hctx->cpuhp_dead)) { 3851 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD, 3852 &hctx->cpuhp_dead); 3853 INIT_HLIST_NODE(&hctx->cpuhp_dead); 3854 } 3855 } 3856 3857 static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx) 3858 { 3859 mutex_lock(&blk_mq_cpuhp_lock); 3860 __blk_mq_remove_cpuhp(hctx); 3861 mutex_unlock(&blk_mq_cpuhp_lock); 3862 } 3863 3864 static void __blk_mq_add_cpuhp(struct blk_mq_hw_ctx *hctx) 3865 { 3866 lockdep_assert_held(&blk_mq_cpuhp_lock); 3867 3868 if (!(hctx->flags & BLK_MQ_F_STACKING) && 3869 hlist_unhashed(&hctx->cpuhp_online)) 3870 cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE, 3871 &hctx->cpuhp_online); 3872 3873 if (hlist_unhashed(&hctx->cpuhp_dead)) 3874 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, 3875 &hctx->cpuhp_dead); 3876 } 3877 3878 static void __blk_mq_remove_cpuhp_list(struct list_head *head) 3879 { 3880 struct blk_mq_hw_ctx *hctx; 3881 3882 lockdep_assert_held(&blk_mq_cpuhp_lock); 3883 3884 list_for_each_entry(hctx, head, hctx_list) 3885 __blk_mq_remove_cpuhp(hctx); 3886 } 3887 3888 /* 3889 * Unregister cpuhp callbacks from exited hw queues 3890 * 3891 * Safe to call if this `request_queue` is live 3892 */ 3893 static void blk_mq_remove_hw_queues_cpuhp(struct request_queue *q) 3894 { 3895 LIST_HEAD(hctx_list); 3896 3897 spin_lock(&q->unused_hctx_lock); 3898 list_splice_init(&q->unused_hctx_list, &hctx_list); 3899 spin_unlock(&q->unused_hctx_lock); 3900 3901 mutex_lock(&blk_mq_cpuhp_lock); 3902 __blk_mq_remove_cpuhp_list(&hctx_list); 3903 mutex_unlock(&blk_mq_cpuhp_lock); 3904 3905 spin_lock(&q->unused_hctx_lock); 3906 list_splice(&hctx_list, &q->unused_hctx_list); 3907 spin_unlock(&q->unused_hctx_lock); 3908 } 3909 3910 /* 3911 * Register cpuhp callbacks from all hw queues 3912 * 3913 * Safe to call if this `request_queue` is live 3914 */ 3915 static void blk_mq_add_hw_queues_cpuhp(struct request_queue *q) 3916 { 3917 struct blk_mq_hw_ctx *hctx; 3918 unsigned long i; 3919 3920 mutex_lock(&blk_mq_cpuhp_lock); 3921 queue_for_each_hw_ctx(q, hctx, i) 3922 __blk_mq_add_cpuhp(hctx); 3923 mutex_unlock(&blk_mq_cpuhp_lock); 3924 } 3925 3926 /* 3927 * Before freeing hw queue, clearing the flush request reference in 3928 * tags->rqs[] for avoiding potential UAF. 3929 */ 3930 static void blk_mq_clear_flush_rq_mapping(struct blk_mq_tags *tags, 3931 unsigned int queue_depth, struct request *flush_rq) 3932 { 3933 int i; 3934 3935 /* The hw queue may not be mapped yet */ 3936 if (!tags) 3937 return; 3938 3939 WARN_ON_ONCE(req_ref_read(flush_rq) != 0); 3940 3941 for (i = 0; i < queue_depth; i++) 3942 cmpxchg(&tags->rqs[i], flush_rq, NULL); 3943 } 3944 3945 static void blk_free_flush_queue_callback(struct rcu_head *head) 3946 { 3947 struct blk_flush_queue *fq = 3948 container_of(head, struct blk_flush_queue, rcu_head); 3949 3950 blk_free_flush_queue(fq); 3951 } 3952 3953 /* hctx->ctxs will be freed in queue's release handler */ 3954 static void blk_mq_exit_hctx(struct request_queue *q, 3955 struct blk_mq_tag_set *set, 3956 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx) 3957 { 3958 struct request *flush_rq = hctx->fq->flush_rq; 3959 3960 if (blk_mq_hw_queue_mapped(hctx)) 3961 blk_mq_tag_idle(hctx); 3962 3963 if (blk_queue_init_done(q)) 3964 blk_mq_clear_flush_rq_mapping(set->tags[hctx_idx], 3965 set->queue_depth, flush_rq); 3966 if (set->ops->exit_request) 3967 set->ops->exit_request(set, flush_rq, hctx_idx); 3968 3969 if (set->ops->exit_hctx) 3970 set->ops->exit_hctx(hctx, hctx_idx); 3971 3972 call_srcu(&set->tags_srcu, &hctx->fq->rcu_head, 3973 blk_free_flush_queue_callback); 3974 hctx->fq = NULL; 3975 3976 spin_lock(&q->unused_hctx_lock); 3977 list_add(&hctx->hctx_list, &q->unused_hctx_list); 3978 spin_unlock(&q->unused_hctx_lock); 3979 } 3980 3981 static void blk_mq_exit_hw_queues(struct request_queue *q, 3982 struct blk_mq_tag_set *set, int nr_queue) 3983 { 3984 struct blk_mq_hw_ctx *hctx; 3985 unsigned long i; 3986 3987 queue_for_each_hw_ctx(q, hctx, i) { 3988 if (i == nr_queue) 3989 break; 3990 blk_mq_remove_cpuhp(hctx); 3991 blk_mq_exit_hctx(q, set, hctx, i); 3992 } 3993 } 3994 3995 static int blk_mq_init_hctx(struct request_queue *q, 3996 struct blk_mq_tag_set *set, 3997 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx) 3998 { 3999 gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY; 4000 4001 hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp); 4002 if (!hctx->fq) 4003 goto fail; 4004 4005 hctx->queue_num = hctx_idx; 4006 4007 hctx->tags = set->tags[hctx_idx]; 4008 4009 if (set->ops->init_hctx && 4010 set->ops->init_hctx(hctx, set->driver_data, hctx_idx)) 4011 goto fail_free_fq; 4012 4013 if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx, 4014 hctx->numa_node)) 4015 goto exit_hctx; 4016 4017 return 0; 4018 4019 exit_hctx: 4020 if (set->ops->exit_hctx) 4021 set->ops->exit_hctx(hctx, hctx_idx); 4022 fail_free_fq: 4023 blk_free_flush_queue(hctx->fq); 4024 hctx->fq = NULL; 4025 fail: 4026 return -1; 4027 } 4028 4029 static struct blk_mq_hw_ctx * 4030 blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set, 4031 int node) 4032 { 4033 struct blk_mq_hw_ctx *hctx; 4034 gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY; 4035 4036 hctx = kzalloc_node(sizeof(struct blk_mq_hw_ctx), gfp, node); 4037 if (!hctx) 4038 goto fail_alloc_hctx; 4039 4040 if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node)) 4041 goto free_hctx; 4042 4043 atomic_set(&hctx->nr_active, 0); 4044 if (node == NUMA_NO_NODE) 4045 node = set->numa_node; 4046 hctx->numa_node = node; 4047 4048 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn); 4049 spin_lock_init(&hctx->lock); 4050 INIT_LIST_HEAD(&hctx->dispatch); 4051 INIT_HLIST_NODE(&hctx->cpuhp_dead); 4052 INIT_HLIST_NODE(&hctx->cpuhp_online); 4053 hctx->queue = q; 4054 hctx->flags = set->flags & ~BLK_MQ_F_TAG_QUEUE_SHARED; 4055 4056 INIT_LIST_HEAD(&hctx->hctx_list); 4057 4058 /* 4059 * Allocate space for all possible cpus to avoid allocation at 4060 * runtime 4061 */ 4062 hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *), 4063 gfp, node); 4064 if (!hctx->ctxs) 4065 goto free_cpumask; 4066 4067 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8), 4068 gfp, node, false, false)) 4069 goto free_ctxs; 4070 hctx->nr_ctx = 0; 4071 4072 spin_lock_init(&hctx->dispatch_wait_lock); 4073 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake); 4074 INIT_LIST_HEAD(&hctx->dispatch_wait.entry); 4075 4076 blk_mq_hctx_kobj_init(hctx); 4077 4078 return hctx; 4079 4080 free_ctxs: 4081 kfree(hctx->ctxs); 4082 free_cpumask: 4083 free_cpumask_var(hctx->cpumask); 4084 free_hctx: 4085 kfree(hctx); 4086 fail_alloc_hctx: 4087 return NULL; 4088 } 4089 4090 static void blk_mq_init_cpu_queues(struct request_queue *q, 4091 unsigned int nr_hw_queues) 4092 { 4093 struct blk_mq_tag_set *set = q->tag_set; 4094 unsigned int i, j; 4095 4096 for_each_possible_cpu(i) { 4097 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); 4098 struct blk_mq_hw_ctx *hctx; 4099 int k; 4100 4101 __ctx->cpu = i; 4102 spin_lock_init(&__ctx->lock); 4103 for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++) 4104 INIT_LIST_HEAD(&__ctx->rq_lists[k]); 4105 4106 __ctx->queue = q; 4107 4108 /* 4109 * Set local node, IFF we have more than one hw queue. If 4110 * not, we remain on the home node of the device 4111 */ 4112 for (j = 0; j < set->nr_maps; j++) { 4113 hctx = blk_mq_map_queue_type(q, j, i); 4114 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) 4115 hctx->numa_node = cpu_to_node(i); 4116 } 4117 } 4118 } 4119 4120 struct blk_mq_tags *blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set, 4121 unsigned int hctx_idx, 4122 unsigned int depth) 4123 { 4124 struct blk_mq_tags *tags; 4125 int ret; 4126 4127 tags = blk_mq_alloc_rq_map(set, hctx_idx, depth, set->reserved_tags); 4128 if (!tags) 4129 return NULL; 4130 4131 ret = blk_mq_alloc_rqs(set, tags, hctx_idx, depth); 4132 if (ret) { 4133 blk_mq_free_rq_map(set, tags); 4134 return NULL; 4135 } 4136 4137 return tags; 4138 } 4139 4140 static bool __blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set, 4141 int hctx_idx) 4142 { 4143 if (blk_mq_is_shared_tags(set->flags)) { 4144 set->tags[hctx_idx] = set->shared_tags; 4145 4146 return true; 4147 } 4148 4149 set->tags[hctx_idx] = blk_mq_alloc_map_and_rqs(set, hctx_idx, 4150 set->queue_depth); 4151 4152 return set->tags[hctx_idx]; 4153 } 4154 4155 void blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set, 4156 struct blk_mq_tags *tags, 4157 unsigned int hctx_idx) 4158 { 4159 if (tags) { 4160 blk_mq_free_rqs(set, tags, hctx_idx); 4161 blk_mq_free_rq_map(set, tags); 4162 } 4163 } 4164 4165 static void __blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set, 4166 unsigned int hctx_idx) 4167 { 4168 if (!blk_mq_is_shared_tags(set->flags)) 4169 blk_mq_free_map_and_rqs(set, set->tags[hctx_idx], hctx_idx); 4170 4171 set->tags[hctx_idx] = NULL; 4172 } 4173 4174 static void blk_mq_map_swqueue(struct request_queue *q) 4175 { 4176 unsigned int j, hctx_idx; 4177 unsigned long i; 4178 struct blk_mq_hw_ctx *hctx; 4179 struct blk_mq_ctx *ctx; 4180 struct blk_mq_tag_set *set = q->tag_set; 4181 4182 queue_for_each_hw_ctx(q, hctx, i) { 4183 cpumask_clear(hctx->cpumask); 4184 hctx->nr_ctx = 0; 4185 hctx->dispatch_from = NULL; 4186 } 4187 4188 /* 4189 * Map software to hardware queues. 4190 * 4191 * If the cpu isn't present, the cpu is mapped to first hctx. 4192 */ 4193 for_each_possible_cpu(i) { 4194 4195 ctx = per_cpu_ptr(q->queue_ctx, i); 4196 for (j = 0; j < set->nr_maps; j++) { 4197 if (!set->map[j].nr_queues) { 4198 ctx->hctxs[j] = blk_mq_map_queue_type(q, 4199 HCTX_TYPE_DEFAULT, i); 4200 continue; 4201 } 4202 hctx_idx = set->map[j].mq_map[i]; 4203 /* unmapped hw queue can be remapped after CPU topo changed */ 4204 if (!set->tags[hctx_idx] && 4205 !__blk_mq_alloc_map_and_rqs(set, hctx_idx)) { 4206 /* 4207 * If tags initialization fail for some hctx, 4208 * that hctx won't be brought online. In this 4209 * case, remap the current ctx to hctx[0] which 4210 * is guaranteed to always have tags allocated 4211 */ 4212 set->map[j].mq_map[i] = 0; 4213 } 4214 4215 hctx = blk_mq_map_queue_type(q, j, i); 4216 ctx->hctxs[j] = hctx; 4217 /* 4218 * If the CPU is already set in the mask, then we've 4219 * mapped this one already. This can happen if 4220 * devices share queues across queue maps. 4221 */ 4222 if (cpumask_test_cpu(i, hctx->cpumask)) 4223 continue; 4224 4225 cpumask_set_cpu(i, hctx->cpumask); 4226 hctx->type = j; 4227 ctx->index_hw[hctx->type] = hctx->nr_ctx; 4228 hctx->ctxs[hctx->nr_ctx++] = ctx; 4229 4230 /* 4231 * If the nr_ctx type overflows, we have exceeded the 4232 * amount of sw queues we can support. 4233 */ 4234 BUG_ON(!hctx->nr_ctx); 4235 } 4236 4237 for (; j < HCTX_MAX_TYPES; j++) 4238 ctx->hctxs[j] = blk_mq_map_queue_type(q, 4239 HCTX_TYPE_DEFAULT, i); 4240 } 4241 4242 queue_for_each_hw_ctx(q, hctx, i) { 4243 int cpu; 4244 4245 /* 4246 * If no software queues are mapped to this hardware queue, 4247 * disable it and free the request entries. 4248 */ 4249 if (!hctx->nr_ctx) { 4250 /* Never unmap queue 0. We need it as a 4251 * fallback in case of a new remap fails 4252 * allocation 4253 */ 4254 if (i) 4255 __blk_mq_free_map_and_rqs(set, i); 4256 4257 hctx->tags = NULL; 4258 continue; 4259 } 4260 4261 hctx->tags = set->tags[i]; 4262 WARN_ON(!hctx->tags); 4263 4264 /* 4265 * Set the map size to the number of mapped software queues. 4266 * This is more accurate and more efficient than looping 4267 * over all possibly mapped software queues. 4268 */ 4269 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx); 4270 4271 /* 4272 * Rule out isolated CPUs from hctx->cpumask to avoid 4273 * running block kworker on isolated CPUs. 4274 * FIXME: cpuset should propagate further changes to isolated CPUs 4275 * here. 4276 */ 4277 rcu_read_lock(); 4278 for_each_cpu(cpu, hctx->cpumask) { 4279 if (cpu_is_isolated(cpu)) 4280 cpumask_clear_cpu(cpu, hctx->cpumask); 4281 } 4282 rcu_read_unlock(); 4283 4284 /* 4285 * Initialize batch roundrobin counts 4286 */ 4287 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx); 4288 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; 4289 } 4290 } 4291 4292 /* 4293 * Caller needs to ensure that we're either frozen/quiesced, or that 4294 * the queue isn't live yet. 4295 */ 4296 static void queue_set_hctx_shared(struct request_queue *q, bool shared) 4297 { 4298 struct blk_mq_hw_ctx *hctx; 4299 unsigned long i; 4300 4301 queue_for_each_hw_ctx(q, hctx, i) { 4302 if (shared) { 4303 hctx->flags |= BLK_MQ_F_TAG_QUEUE_SHARED; 4304 } else { 4305 blk_mq_tag_idle(hctx); 4306 hctx->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED; 4307 } 4308 } 4309 } 4310 4311 static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set, 4312 bool shared) 4313 { 4314 struct request_queue *q; 4315 unsigned int memflags; 4316 4317 lockdep_assert_held(&set->tag_list_lock); 4318 4319 list_for_each_entry(q, &set->tag_list, tag_set_list) { 4320 memflags = blk_mq_freeze_queue(q); 4321 queue_set_hctx_shared(q, shared); 4322 blk_mq_unfreeze_queue(q, memflags); 4323 } 4324 } 4325 4326 static void blk_mq_del_queue_tag_set(struct request_queue *q) 4327 { 4328 struct blk_mq_tag_set *set = q->tag_set; 4329 4330 mutex_lock(&set->tag_list_lock); 4331 list_del_rcu(&q->tag_set_list); 4332 if (list_is_singular(&set->tag_list)) { 4333 /* just transitioned to unshared */ 4334 set->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED; 4335 /* update existing queue */ 4336 blk_mq_update_tag_set_shared(set, false); 4337 } 4338 mutex_unlock(&set->tag_list_lock); 4339 } 4340 4341 static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set, 4342 struct request_queue *q) 4343 { 4344 mutex_lock(&set->tag_list_lock); 4345 4346 /* 4347 * Check to see if we're transitioning to shared (from 1 to 2 queues). 4348 */ 4349 if (!list_empty(&set->tag_list) && 4350 !(set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) { 4351 set->flags |= BLK_MQ_F_TAG_QUEUE_SHARED; 4352 /* update existing queue */ 4353 blk_mq_update_tag_set_shared(set, true); 4354 } 4355 if (set->flags & BLK_MQ_F_TAG_QUEUE_SHARED) 4356 queue_set_hctx_shared(q, true); 4357 list_add_tail_rcu(&q->tag_set_list, &set->tag_list); 4358 4359 mutex_unlock(&set->tag_list_lock); 4360 } 4361 4362 /* All allocations will be freed in release handler of q->mq_kobj */ 4363 static int blk_mq_alloc_ctxs(struct request_queue *q) 4364 { 4365 struct blk_mq_ctxs *ctxs; 4366 int cpu; 4367 4368 ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL); 4369 if (!ctxs) 4370 return -ENOMEM; 4371 4372 ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx); 4373 if (!ctxs->queue_ctx) 4374 goto fail; 4375 4376 for_each_possible_cpu(cpu) { 4377 struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu); 4378 ctx->ctxs = ctxs; 4379 } 4380 4381 q->mq_kobj = &ctxs->kobj; 4382 q->queue_ctx = ctxs->queue_ctx; 4383 4384 return 0; 4385 fail: 4386 kfree(ctxs); 4387 return -ENOMEM; 4388 } 4389 4390 /* 4391 * It is the actual release handler for mq, but we do it from 4392 * request queue's release handler for avoiding use-after-free 4393 * and headache because q->mq_kobj shouldn't have been introduced, 4394 * but we can't group ctx/kctx kobj without it. 4395 */ 4396 void blk_mq_release(struct request_queue *q) 4397 { 4398 struct blk_mq_hw_ctx *hctx, *next; 4399 unsigned long i; 4400 4401 queue_for_each_hw_ctx(q, hctx, i) 4402 WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list)); 4403 4404 /* all hctx are in .unused_hctx_list now */ 4405 list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) { 4406 list_del_init(&hctx->hctx_list); 4407 kobject_put(&hctx->kobj); 4408 } 4409 4410 kfree(q->queue_hw_ctx); 4411 4412 /* 4413 * release .mq_kobj and sw queue's kobject now because 4414 * both share lifetime with request queue. 4415 */ 4416 blk_mq_sysfs_deinit(q); 4417 } 4418 4419 struct request_queue *blk_mq_alloc_queue(struct blk_mq_tag_set *set, 4420 struct queue_limits *lim, void *queuedata) 4421 { 4422 struct queue_limits default_lim = { }; 4423 struct request_queue *q; 4424 int ret; 4425 4426 if (!lim) 4427 lim = &default_lim; 4428 lim->features |= BLK_FEAT_IO_STAT | BLK_FEAT_NOWAIT; 4429 if (set->nr_maps > HCTX_TYPE_POLL) 4430 lim->features |= BLK_FEAT_POLL; 4431 4432 q = blk_alloc_queue(lim, set->numa_node); 4433 if (IS_ERR(q)) 4434 return q; 4435 q->queuedata = queuedata; 4436 ret = blk_mq_init_allocated_queue(set, q); 4437 if (ret) { 4438 blk_put_queue(q); 4439 return ERR_PTR(ret); 4440 } 4441 return q; 4442 } 4443 EXPORT_SYMBOL(blk_mq_alloc_queue); 4444 4445 /** 4446 * blk_mq_destroy_queue - shutdown a request queue 4447 * @q: request queue to shutdown 4448 * 4449 * This shuts down a request queue allocated by blk_mq_alloc_queue(). All future 4450 * requests will be failed with -ENODEV. The caller is responsible for dropping 4451 * the reference from blk_mq_alloc_queue() by calling blk_put_queue(). 4452 * 4453 * Context: can sleep 4454 */ 4455 void blk_mq_destroy_queue(struct request_queue *q) 4456 { 4457 WARN_ON_ONCE(!queue_is_mq(q)); 4458 WARN_ON_ONCE(blk_queue_registered(q)); 4459 4460 might_sleep(); 4461 4462 blk_queue_flag_set(QUEUE_FLAG_DYING, q); 4463 blk_queue_start_drain(q); 4464 blk_mq_freeze_queue_wait(q); 4465 4466 blk_sync_queue(q); 4467 blk_mq_cancel_work_sync(q); 4468 blk_mq_exit_queue(q); 4469 } 4470 EXPORT_SYMBOL(blk_mq_destroy_queue); 4471 4472 struct gendisk *__blk_mq_alloc_disk(struct blk_mq_tag_set *set, 4473 struct queue_limits *lim, void *queuedata, 4474 struct lock_class_key *lkclass) 4475 { 4476 struct request_queue *q; 4477 struct gendisk *disk; 4478 4479 q = blk_mq_alloc_queue(set, lim, queuedata); 4480 if (IS_ERR(q)) 4481 return ERR_CAST(q); 4482 4483 disk = __alloc_disk_node(q, set->numa_node, lkclass); 4484 if (!disk) { 4485 blk_mq_destroy_queue(q); 4486 blk_put_queue(q); 4487 return ERR_PTR(-ENOMEM); 4488 } 4489 set_bit(GD_OWNS_QUEUE, &disk->state); 4490 return disk; 4491 } 4492 EXPORT_SYMBOL(__blk_mq_alloc_disk); 4493 4494 struct gendisk *blk_mq_alloc_disk_for_queue(struct request_queue *q, 4495 struct lock_class_key *lkclass) 4496 { 4497 struct gendisk *disk; 4498 4499 if (!blk_get_queue(q)) 4500 return NULL; 4501 disk = __alloc_disk_node(q, NUMA_NO_NODE, lkclass); 4502 if (!disk) 4503 blk_put_queue(q); 4504 return disk; 4505 } 4506 EXPORT_SYMBOL(blk_mq_alloc_disk_for_queue); 4507 4508 /* 4509 * Only hctx removed from cpuhp list can be reused 4510 */ 4511 static bool blk_mq_hctx_is_reusable(struct blk_mq_hw_ctx *hctx) 4512 { 4513 return hlist_unhashed(&hctx->cpuhp_online) && 4514 hlist_unhashed(&hctx->cpuhp_dead); 4515 } 4516 4517 static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx( 4518 struct blk_mq_tag_set *set, struct request_queue *q, 4519 int hctx_idx, int node) 4520 { 4521 struct blk_mq_hw_ctx *hctx = NULL, *tmp; 4522 4523 /* reuse dead hctx first */ 4524 spin_lock(&q->unused_hctx_lock); 4525 list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) { 4526 if (tmp->numa_node == node && blk_mq_hctx_is_reusable(tmp)) { 4527 hctx = tmp; 4528 break; 4529 } 4530 } 4531 if (hctx) 4532 list_del_init(&hctx->hctx_list); 4533 spin_unlock(&q->unused_hctx_lock); 4534 4535 if (!hctx) 4536 hctx = blk_mq_alloc_hctx(q, set, node); 4537 if (!hctx) 4538 goto fail; 4539 4540 if (blk_mq_init_hctx(q, set, hctx, hctx_idx)) 4541 goto free_hctx; 4542 4543 return hctx; 4544 4545 free_hctx: 4546 kobject_put(&hctx->kobj); 4547 fail: 4548 return NULL; 4549 } 4550 4551 static void __blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set, 4552 struct request_queue *q) 4553 { 4554 int i, j, end; 4555 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx; 4556 4557 if (q->nr_hw_queues < set->nr_hw_queues) { 4558 struct blk_mq_hw_ctx **new_hctxs; 4559 4560 new_hctxs = kcalloc_node(set->nr_hw_queues, 4561 sizeof(*new_hctxs), GFP_KERNEL, 4562 set->numa_node); 4563 if (!new_hctxs) 4564 return; 4565 if (hctxs) 4566 memcpy(new_hctxs, hctxs, q->nr_hw_queues * 4567 sizeof(*hctxs)); 4568 rcu_assign_pointer(q->queue_hw_ctx, new_hctxs); 4569 /* 4570 * Make sure reading the old queue_hw_ctx from other 4571 * context concurrently won't trigger uaf. 4572 */ 4573 kfree_rcu_mightsleep(hctxs); 4574 hctxs = new_hctxs; 4575 } 4576 4577 for (i = 0; i < set->nr_hw_queues; i++) { 4578 int old_node; 4579 int node = blk_mq_get_hctx_node(set, i); 4580 struct blk_mq_hw_ctx *old_hctx = hctxs[i]; 4581 4582 if (old_hctx) { 4583 old_node = old_hctx->numa_node; 4584 blk_mq_exit_hctx(q, set, old_hctx, i); 4585 } 4586 4587 hctxs[i] = blk_mq_alloc_and_init_hctx(set, q, i, node); 4588 if (!hctxs[i]) { 4589 if (!old_hctx) 4590 break; 4591 pr_warn("Allocate new hctx on node %d fails, fallback to previous one on node %d\n", 4592 node, old_node); 4593 hctxs[i] = blk_mq_alloc_and_init_hctx(set, q, i, 4594 old_node); 4595 WARN_ON_ONCE(!hctxs[i]); 4596 } 4597 } 4598 /* 4599 * Increasing nr_hw_queues fails. Free the newly allocated 4600 * hctxs and keep the previous q->nr_hw_queues. 4601 */ 4602 if (i != set->nr_hw_queues) { 4603 j = q->nr_hw_queues; 4604 end = i; 4605 } else { 4606 j = i; 4607 end = q->nr_hw_queues; 4608 q->nr_hw_queues = set->nr_hw_queues; 4609 } 4610 4611 for (; j < end; j++) { 4612 struct blk_mq_hw_ctx *hctx = hctxs[j]; 4613 4614 if (hctx) { 4615 blk_mq_exit_hctx(q, set, hctx, j); 4616 hctxs[j] = NULL; 4617 } 4618 } 4619 } 4620 4621 static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set, 4622 struct request_queue *q) 4623 { 4624 __blk_mq_realloc_hw_ctxs(set, q); 4625 4626 /* unregister cpuhp callbacks for exited hctxs */ 4627 blk_mq_remove_hw_queues_cpuhp(q); 4628 4629 /* register cpuhp for new initialized hctxs */ 4630 blk_mq_add_hw_queues_cpuhp(q); 4631 } 4632 4633 int blk_mq_init_allocated_queue(struct blk_mq_tag_set *set, 4634 struct request_queue *q) 4635 { 4636 /* mark the queue as mq asap */ 4637 q->mq_ops = set->ops; 4638 4639 /* 4640 * ->tag_set has to be setup before initialize hctx, which cpuphp 4641 * handler needs it for checking queue mapping 4642 */ 4643 q->tag_set = set; 4644 4645 if (blk_mq_alloc_ctxs(q)) 4646 goto err_exit; 4647 4648 /* init q->mq_kobj and sw queues' kobjects */ 4649 blk_mq_sysfs_init(q); 4650 4651 INIT_LIST_HEAD(&q->unused_hctx_list); 4652 spin_lock_init(&q->unused_hctx_lock); 4653 4654 blk_mq_realloc_hw_ctxs(set, q); 4655 if (!q->nr_hw_queues) 4656 goto err_hctxs; 4657 4658 INIT_WORK(&q->timeout_work, blk_mq_timeout_work); 4659 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ); 4660 4661 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; 4662 4663 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work); 4664 INIT_LIST_HEAD(&q->flush_list); 4665 INIT_LIST_HEAD(&q->requeue_list); 4666 spin_lock_init(&q->requeue_lock); 4667 4668 q->nr_requests = set->queue_depth; 4669 q->async_depth = set->queue_depth; 4670 4671 blk_mq_init_cpu_queues(q, set->nr_hw_queues); 4672 blk_mq_map_swqueue(q); 4673 blk_mq_add_queue_tag_set(set, q); 4674 return 0; 4675 4676 err_hctxs: 4677 blk_mq_release(q); 4678 err_exit: 4679 q->mq_ops = NULL; 4680 return -ENOMEM; 4681 } 4682 EXPORT_SYMBOL(blk_mq_init_allocated_queue); 4683 4684 /* tags can _not_ be used after returning from blk_mq_exit_queue */ 4685 void blk_mq_exit_queue(struct request_queue *q) 4686 { 4687 struct blk_mq_tag_set *set = q->tag_set; 4688 4689 /* Checks hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED. */ 4690 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues); 4691 /* May clear BLK_MQ_F_TAG_QUEUE_SHARED in hctx->flags. */ 4692 blk_mq_del_queue_tag_set(q); 4693 } 4694 4695 static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set) 4696 { 4697 int i; 4698 4699 if (blk_mq_is_shared_tags(set->flags)) { 4700 set->shared_tags = blk_mq_alloc_map_and_rqs(set, 4701 BLK_MQ_NO_HCTX_IDX, 4702 set->queue_depth); 4703 if (!set->shared_tags) 4704 return -ENOMEM; 4705 } 4706 4707 for (i = 0; i < set->nr_hw_queues; i++) { 4708 if (!__blk_mq_alloc_map_and_rqs(set, i)) 4709 goto out_unwind; 4710 cond_resched(); 4711 } 4712 4713 return 0; 4714 4715 out_unwind: 4716 while (--i >= 0) 4717 __blk_mq_free_map_and_rqs(set, i); 4718 4719 if (blk_mq_is_shared_tags(set->flags)) { 4720 blk_mq_free_map_and_rqs(set, set->shared_tags, 4721 BLK_MQ_NO_HCTX_IDX); 4722 } 4723 4724 return -ENOMEM; 4725 } 4726 4727 /* 4728 * Allocate the request maps associated with this tag_set. Note that this 4729 * may reduce the depth asked for, if memory is tight. set->queue_depth 4730 * will be updated to reflect the allocated depth. 4731 */ 4732 static int blk_mq_alloc_set_map_and_rqs(struct blk_mq_tag_set *set) 4733 { 4734 unsigned int depth; 4735 int err; 4736 4737 depth = set->queue_depth; 4738 do { 4739 err = __blk_mq_alloc_rq_maps(set); 4740 if (!err) 4741 break; 4742 4743 set->queue_depth >>= 1; 4744 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) { 4745 err = -ENOMEM; 4746 break; 4747 } 4748 } while (set->queue_depth); 4749 4750 if (!set->queue_depth || err) { 4751 pr_err("blk-mq: failed to allocate request map\n"); 4752 return -ENOMEM; 4753 } 4754 4755 if (depth != set->queue_depth) 4756 pr_info("blk-mq: reduced tag depth (%u -> %u)\n", 4757 depth, set->queue_depth); 4758 4759 return 0; 4760 } 4761 4762 static void blk_mq_update_queue_map(struct blk_mq_tag_set *set) 4763 { 4764 /* 4765 * blk_mq_map_queues() and multiple .map_queues() implementations 4766 * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the 4767 * number of hardware queues. 4768 */ 4769 if (set->nr_maps == 1) 4770 set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues; 4771 4772 if (set->ops->map_queues) { 4773 int i; 4774 4775 /* 4776 * transport .map_queues is usually done in the following 4777 * way: 4778 * 4779 * for (queue = 0; queue < set->nr_hw_queues; queue++) { 4780 * mask = get_cpu_mask(queue) 4781 * for_each_cpu(cpu, mask) 4782 * set->map[x].mq_map[cpu] = queue; 4783 * } 4784 * 4785 * When we need to remap, the table has to be cleared for 4786 * killing stale mapping since one CPU may not be mapped 4787 * to any hw queue. 4788 */ 4789 for (i = 0; i < set->nr_maps; i++) 4790 blk_mq_clear_mq_map(&set->map[i]); 4791 4792 set->ops->map_queues(set); 4793 } else { 4794 BUG_ON(set->nr_maps > 1); 4795 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]); 4796 } 4797 } 4798 4799 static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set, 4800 int new_nr_hw_queues) 4801 { 4802 struct blk_mq_tags **new_tags; 4803 int i; 4804 4805 if (set->nr_hw_queues >= new_nr_hw_queues) 4806 goto done; 4807 4808 new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *), 4809 GFP_KERNEL, set->numa_node); 4810 if (!new_tags) 4811 return -ENOMEM; 4812 4813 if (set->tags) 4814 memcpy(new_tags, set->tags, set->nr_hw_queues * 4815 sizeof(*set->tags)); 4816 kfree(set->tags); 4817 set->tags = new_tags; 4818 4819 for (i = set->nr_hw_queues; i < new_nr_hw_queues; i++) { 4820 if (!__blk_mq_alloc_map_and_rqs(set, i)) { 4821 while (--i >= set->nr_hw_queues) 4822 __blk_mq_free_map_and_rqs(set, i); 4823 return -ENOMEM; 4824 } 4825 cond_resched(); 4826 } 4827 4828 done: 4829 set->nr_hw_queues = new_nr_hw_queues; 4830 return 0; 4831 } 4832 4833 /* 4834 * Alloc a tag set to be associated with one or more request queues. 4835 * May fail with EINVAL for various error conditions. May adjust the 4836 * requested depth down, if it's too large. In that case, the set 4837 * value will be stored in set->queue_depth. 4838 */ 4839 int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) 4840 { 4841 int i, ret; 4842 4843 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS); 4844 4845 if (!set->nr_hw_queues) 4846 return -EINVAL; 4847 if (!set->queue_depth) 4848 return -EINVAL; 4849 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) 4850 return -EINVAL; 4851 4852 if (!set->ops->queue_rq) 4853 return -EINVAL; 4854 4855 if (!set->ops->get_budget ^ !set->ops->put_budget) 4856 return -EINVAL; 4857 4858 if (set->queue_depth > BLK_MQ_MAX_DEPTH) { 4859 pr_info("blk-mq: reduced tag depth to %u\n", 4860 BLK_MQ_MAX_DEPTH); 4861 set->queue_depth = BLK_MQ_MAX_DEPTH; 4862 } 4863 4864 if (!set->nr_maps) 4865 set->nr_maps = 1; 4866 else if (set->nr_maps > HCTX_MAX_TYPES) 4867 return -EINVAL; 4868 4869 /* 4870 * If a crashdump is active, then we are potentially in a very 4871 * memory constrained environment. Limit us to 64 tags to prevent 4872 * using too much memory. 4873 */ 4874 if (is_kdump_kernel()) 4875 set->queue_depth = min(64U, set->queue_depth); 4876 4877 /* 4878 * There is no use for more h/w queues than cpus if we just have 4879 * a single map 4880 */ 4881 if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids) 4882 set->nr_hw_queues = nr_cpu_ids; 4883 4884 if (set->flags & BLK_MQ_F_BLOCKING) { 4885 set->srcu = kmalloc(sizeof(*set->srcu), GFP_KERNEL); 4886 if (!set->srcu) 4887 return -ENOMEM; 4888 ret = init_srcu_struct(set->srcu); 4889 if (ret) 4890 goto out_free_srcu; 4891 } 4892 ret = init_srcu_struct(&set->tags_srcu); 4893 if (ret) 4894 goto out_cleanup_srcu; 4895 4896 init_rwsem(&set->update_nr_hwq_lock); 4897 4898 ret = -ENOMEM; 4899 set->tags = kcalloc_node(set->nr_hw_queues, 4900 sizeof(struct blk_mq_tags *), GFP_KERNEL, 4901 set->numa_node); 4902 if (!set->tags) 4903 goto out_cleanup_tags_srcu; 4904 4905 for (i = 0; i < set->nr_maps; i++) { 4906 set->map[i].mq_map = kcalloc_node(nr_cpu_ids, 4907 sizeof(set->map[i].mq_map[0]), 4908 GFP_KERNEL, set->numa_node); 4909 if (!set->map[i].mq_map) 4910 goto out_free_mq_map; 4911 set->map[i].nr_queues = set->nr_hw_queues; 4912 } 4913 4914 blk_mq_update_queue_map(set); 4915 4916 ret = blk_mq_alloc_set_map_and_rqs(set); 4917 if (ret) 4918 goto out_free_mq_map; 4919 4920 mutex_init(&set->tag_list_lock); 4921 INIT_LIST_HEAD(&set->tag_list); 4922 4923 return 0; 4924 4925 out_free_mq_map: 4926 for (i = 0; i < set->nr_maps; i++) { 4927 kfree(set->map[i].mq_map); 4928 set->map[i].mq_map = NULL; 4929 } 4930 kfree(set->tags); 4931 set->tags = NULL; 4932 out_cleanup_tags_srcu: 4933 cleanup_srcu_struct(&set->tags_srcu); 4934 out_cleanup_srcu: 4935 if (set->flags & BLK_MQ_F_BLOCKING) 4936 cleanup_srcu_struct(set->srcu); 4937 out_free_srcu: 4938 if (set->flags & BLK_MQ_F_BLOCKING) 4939 kfree(set->srcu); 4940 return ret; 4941 } 4942 EXPORT_SYMBOL(blk_mq_alloc_tag_set); 4943 4944 /* allocate and initialize a tagset for a simple single-queue device */ 4945 int blk_mq_alloc_sq_tag_set(struct blk_mq_tag_set *set, 4946 const struct blk_mq_ops *ops, unsigned int queue_depth, 4947 unsigned int set_flags) 4948 { 4949 memset(set, 0, sizeof(*set)); 4950 set->ops = ops; 4951 set->nr_hw_queues = 1; 4952 set->nr_maps = 1; 4953 set->queue_depth = queue_depth; 4954 set->numa_node = NUMA_NO_NODE; 4955 set->flags = set_flags; 4956 return blk_mq_alloc_tag_set(set); 4957 } 4958 EXPORT_SYMBOL_GPL(blk_mq_alloc_sq_tag_set); 4959 4960 void blk_mq_free_tag_set(struct blk_mq_tag_set *set) 4961 { 4962 int i, j; 4963 4964 for (i = 0; i < set->nr_hw_queues; i++) 4965 __blk_mq_free_map_and_rqs(set, i); 4966 4967 if (blk_mq_is_shared_tags(set->flags)) { 4968 blk_mq_free_map_and_rqs(set, set->shared_tags, 4969 BLK_MQ_NO_HCTX_IDX); 4970 } 4971 4972 for (j = 0; j < set->nr_maps; j++) { 4973 kfree(set->map[j].mq_map); 4974 set->map[j].mq_map = NULL; 4975 } 4976 4977 kfree(set->tags); 4978 set->tags = NULL; 4979 4980 srcu_barrier(&set->tags_srcu); 4981 cleanup_srcu_struct(&set->tags_srcu); 4982 if (set->flags & BLK_MQ_F_BLOCKING) { 4983 cleanup_srcu_struct(set->srcu); 4984 kfree(set->srcu); 4985 } 4986 } 4987 EXPORT_SYMBOL(blk_mq_free_tag_set); 4988 4989 struct elevator_tags *blk_mq_update_nr_requests(struct request_queue *q, 4990 struct elevator_tags *et, 4991 unsigned int nr) 4992 { 4993 struct blk_mq_tag_set *set = q->tag_set; 4994 struct elevator_tags *old_et = NULL; 4995 struct blk_mq_hw_ctx *hctx; 4996 unsigned long i; 4997 4998 blk_mq_quiesce_queue(q); 4999 5000 if (blk_mq_is_shared_tags(set->flags)) { 5001 /* 5002 * Shared tags, for sched tags, we allocate max initially hence 5003 * tags can't grow, see blk_mq_alloc_sched_tags(). 5004 */ 5005 if (q->elevator) 5006 blk_mq_tag_update_sched_shared_tags(q, nr); 5007 else 5008 blk_mq_tag_resize_shared_tags(set, nr); 5009 } else if (!q->elevator) { 5010 /* 5011 * Non-shared hardware tags, nr is already checked from 5012 * queue_requests_store() and tags can't grow. 5013 */ 5014 queue_for_each_hw_ctx(q, hctx, i) { 5015 if (!hctx->tags) 5016 continue; 5017 sbitmap_queue_resize(&hctx->tags->bitmap_tags, 5018 nr - hctx->tags->nr_reserved_tags); 5019 } 5020 } else if (nr <= q->elevator->et->nr_requests) { 5021 /* Non-shared sched tags, and tags don't grow. */ 5022 queue_for_each_hw_ctx(q, hctx, i) { 5023 if (!hctx->sched_tags) 5024 continue; 5025 sbitmap_queue_resize(&hctx->sched_tags->bitmap_tags, 5026 nr - hctx->sched_tags->nr_reserved_tags); 5027 } 5028 } else { 5029 /* Non-shared sched tags, and tags grow */ 5030 queue_for_each_hw_ctx(q, hctx, i) 5031 hctx->sched_tags = et->tags[i]; 5032 old_et = q->elevator->et; 5033 q->elevator->et = et; 5034 } 5035 5036 /* 5037 * Preserve relative value, both nr and async_depth are at most 16 bit 5038 * value, no need to worry about overflow. 5039 */ 5040 q->async_depth = max(q->async_depth * nr / q->nr_requests, 1); 5041 q->nr_requests = nr; 5042 if (q->elevator && q->elevator->type->ops.depth_updated) 5043 q->elevator->type->ops.depth_updated(q); 5044 5045 blk_mq_unquiesce_queue(q); 5046 return old_et; 5047 } 5048 5049 /* 5050 * Switch back to the elevator type stored in the xarray. 5051 */ 5052 static void blk_mq_elv_switch_back(struct request_queue *q, 5053 struct xarray *elv_tbl) 5054 { 5055 struct elv_change_ctx *ctx = xa_load(elv_tbl, q->id); 5056 5057 if (WARN_ON_ONCE(!ctx)) 5058 return; 5059 5060 /* The elv_update_nr_hw_queues unfreezes the queue. */ 5061 elv_update_nr_hw_queues(q, ctx); 5062 5063 /* Drop the reference acquired in blk_mq_elv_switch_none. */ 5064 if (ctx->type) 5065 elevator_put(ctx->type); 5066 } 5067 5068 /* 5069 * Stores elevator name and type in ctx and set current elevator to none. 5070 */ 5071 static int blk_mq_elv_switch_none(struct request_queue *q, 5072 struct xarray *elv_tbl) 5073 { 5074 struct elv_change_ctx *ctx; 5075 5076 lockdep_assert_held_write(&q->tag_set->update_nr_hwq_lock); 5077 5078 /* 5079 * Accessing q->elevator without holding q->elevator_lock is safe here 5080 * because we're called from nr_hw_queue update which is protected by 5081 * set->update_nr_hwq_lock in the writer context. So, scheduler update/ 5082 * switch code (which acquires the same lock in the reader context) 5083 * can't run concurrently. 5084 */ 5085 if (q->elevator) { 5086 ctx = xa_load(elv_tbl, q->id); 5087 if (WARN_ON_ONCE(!ctx)) 5088 return -ENOENT; 5089 5090 ctx->name = q->elevator->type->elevator_name; 5091 5092 /* 5093 * Before we switch elevator to 'none', take a reference to 5094 * the elevator module so that while nr_hw_queue update is 5095 * running, no one can remove elevator module. We'd put the 5096 * reference to elevator module later when we switch back 5097 * elevator. 5098 */ 5099 __elevator_get(q->elevator->type); 5100 5101 /* 5102 * Store elevator type so that we can release the reference 5103 * taken above later. 5104 */ 5105 ctx->type = q->elevator->type; 5106 elevator_set_none(q); 5107 } 5108 return 0; 5109 } 5110 5111 static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, 5112 int nr_hw_queues) 5113 { 5114 struct request_queue *q; 5115 int prev_nr_hw_queues = set->nr_hw_queues; 5116 unsigned int memflags; 5117 int i; 5118 struct xarray elv_tbl; 5119 bool queues_frozen = false; 5120 5121 lockdep_assert_held(&set->tag_list_lock); 5122 5123 if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids) 5124 nr_hw_queues = nr_cpu_ids; 5125 if (nr_hw_queues < 1) 5126 return; 5127 if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues) 5128 return; 5129 5130 memflags = memalloc_noio_save(); 5131 5132 xa_init(&elv_tbl); 5133 if (blk_mq_alloc_sched_ctx_batch(&elv_tbl, set) < 0) 5134 goto out_free_ctx; 5135 5136 if (blk_mq_alloc_sched_res_batch(&elv_tbl, set, nr_hw_queues) < 0) 5137 goto out_free_ctx; 5138 5139 list_for_each_entry(q, &set->tag_list, tag_set_list) { 5140 blk_mq_debugfs_unregister_hctxs(q); 5141 blk_mq_sysfs_unregister_hctxs(q); 5142 } 5143 5144 /* 5145 * Switch IO scheduler to 'none', cleaning up the data associated 5146 * with the previous scheduler. We will switch back once we are done 5147 * updating the new sw to hw queue mappings. 5148 */ 5149 list_for_each_entry(q, &set->tag_list, tag_set_list) 5150 if (blk_mq_elv_switch_none(q, &elv_tbl)) 5151 goto switch_back; 5152 5153 list_for_each_entry(q, &set->tag_list, tag_set_list) 5154 blk_mq_freeze_queue_nomemsave(q); 5155 queues_frozen = true; 5156 if (blk_mq_realloc_tag_set_tags(set, nr_hw_queues) < 0) 5157 goto switch_back; 5158 5159 fallback: 5160 blk_mq_update_queue_map(set); 5161 list_for_each_entry(q, &set->tag_list, tag_set_list) { 5162 __blk_mq_realloc_hw_ctxs(set, q); 5163 5164 if (q->nr_hw_queues != set->nr_hw_queues) { 5165 int i = prev_nr_hw_queues; 5166 5167 pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n", 5168 nr_hw_queues, prev_nr_hw_queues); 5169 for (; i < set->nr_hw_queues; i++) 5170 __blk_mq_free_map_and_rqs(set, i); 5171 5172 set->nr_hw_queues = prev_nr_hw_queues; 5173 goto fallback; 5174 } 5175 blk_mq_map_swqueue(q); 5176 } 5177 switch_back: 5178 /* The blk_mq_elv_switch_back unfreezes queue for us. */ 5179 list_for_each_entry(q, &set->tag_list, tag_set_list) { 5180 /* switch_back expects queue to be frozen */ 5181 if (!queues_frozen) 5182 blk_mq_freeze_queue_nomemsave(q); 5183 blk_mq_elv_switch_back(q, &elv_tbl); 5184 } 5185 5186 list_for_each_entry(q, &set->tag_list, tag_set_list) { 5187 blk_mq_sysfs_register_hctxs(q); 5188 blk_mq_debugfs_register_hctxs(q); 5189 5190 blk_mq_remove_hw_queues_cpuhp(q); 5191 blk_mq_add_hw_queues_cpuhp(q); 5192 } 5193 5194 out_free_ctx: 5195 blk_mq_free_sched_ctx_batch(&elv_tbl); 5196 xa_destroy(&elv_tbl); 5197 memalloc_noio_restore(memflags); 5198 5199 /* Free the excess tags when nr_hw_queues shrink. */ 5200 for (i = set->nr_hw_queues; i < prev_nr_hw_queues; i++) 5201 __blk_mq_free_map_and_rqs(set, i); 5202 } 5203 5204 void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues) 5205 { 5206 down_write(&set->update_nr_hwq_lock); 5207 mutex_lock(&set->tag_list_lock); 5208 __blk_mq_update_nr_hw_queues(set, nr_hw_queues); 5209 mutex_unlock(&set->tag_list_lock); 5210 up_write(&set->update_nr_hwq_lock); 5211 } 5212 EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues); 5213 5214 static int blk_hctx_poll(struct request_queue *q, struct blk_mq_hw_ctx *hctx, 5215 struct io_comp_batch *iob, unsigned int flags) 5216 { 5217 int ret; 5218 5219 do { 5220 ret = q->mq_ops->poll(hctx, iob); 5221 if (ret > 0) 5222 return ret; 5223 if (task_sigpending(current)) 5224 return 1; 5225 if (ret < 0 || (flags & BLK_POLL_ONESHOT)) 5226 break; 5227 cpu_relax(); 5228 } while (!need_resched()); 5229 5230 return 0; 5231 } 5232 5233 int blk_mq_poll(struct request_queue *q, blk_qc_t cookie, 5234 struct io_comp_batch *iob, unsigned int flags) 5235 { 5236 if (!blk_mq_can_poll(q)) 5237 return 0; 5238 return blk_hctx_poll(q, q->queue_hw_ctx[cookie], iob, flags); 5239 } 5240 5241 int blk_rq_poll(struct request *rq, struct io_comp_batch *iob, 5242 unsigned int poll_flags) 5243 { 5244 struct request_queue *q = rq->q; 5245 int ret; 5246 5247 if (!blk_rq_is_poll(rq)) 5248 return 0; 5249 if (!percpu_ref_tryget(&q->q_usage_counter)) 5250 return 0; 5251 5252 ret = blk_hctx_poll(q, rq->mq_hctx, iob, poll_flags); 5253 blk_queue_exit(q); 5254 5255 return ret; 5256 } 5257 EXPORT_SYMBOL_GPL(blk_rq_poll); 5258 5259 unsigned int blk_mq_rq_cpu(struct request *rq) 5260 { 5261 return rq->mq_ctx->cpu; 5262 } 5263 EXPORT_SYMBOL(blk_mq_rq_cpu); 5264 5265 void blk_mq_cancel_work_sync(struct request_queue *q) 5266 { 5267 struct blk_mq_hw_ctx *hctx; 5268 unsigned long i; 5269 5270 cancel_delayed_work_sync(&q->requeue_work); 5271 5272 queue_for_each_hw_ctx(q, hctx, i) 5273 cancel_delayed_work_sync(&hctx->run_work); 5274 } 5275 5276 static int __init blk_mq_init(void) 5277 { 5278 int i; 5279 5280 for_each_possible_cpu(i) 5281 init_llist_head(&per_cpu(blk_cpu_done, i)); 5282 for_each_possible_cpu(i) 5283 INIT_CSD(&per_cpu(blk_cpu_csd, i), 5284 __blk_mq_complete_request_remote, NULL); 5285 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq); 5286 5287 cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD, 5288 "block/softirq:dead", NULL, 5289 blk_softirq_cpu_dead); 5290 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL, 5291 blk_mq_hctx_notify_dead); 5292 cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online", 5293 blk_mq_hctx_notify_online, 5294 blk_mq_hctx_notify_offline); 5295 return 0; 5296 } 5297 subsys_initcall(blk_mq_init); 5298