1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Tag allocation using scalable bitmaps. Uses active queue tracking to support 4 * fairer distribution of tags between multiple submitters when a shared tag map 5 * is used. 6 * 7 * Copyright (C) 2013-2014 Jens Axboe 8 */ 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 #include <linux/slab.h> 12 #include <linux/mm.h> 13 #include <linux/kmemleak.h> 14 15 #include <linux/delay.h> 16 #include "blk.h" 17 #include "blk-mq.h" 18 #include "blk-mq-sched.h" 19 20 /* 21 * Recalculate wakeup batch when tag is shared by hctx. 22 */ 23 static void blk_mq_update_wake_batch(struct blk_mq_tags *tags, 24 unsigned int users) 25 { 26 if (!users) 27 return; 28 29 sbitmap_queue_recalculate_wake_batch(&tags->bitmap_tags, 30 users); 31 sbitmap_queue_recalculate_wake_batch(&tags->breserved_tags, 32 users); 33 } 34 35 /* 36 * If a previously inactive queue goes active, bump the active user count. 37 * We need to do this before try to allocate driver tag, then even if fail 38 * to get tag when first time, the other shared-tag users could reserve 39 * budget for it. 40 */ 41 void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx) 42 { 43 unsigned int users; 44 unsigned long flags; 45 struct blk_mq_tags *tags = hctx->tags; 46 47 /* 48 * calling test_bit() prior to test_and_set_bit() is intentional, 49 * it avoids dirtying the cacheline if the queue is already active. 50 */ 51 if (blk_mq_is_shared_tags(hctx->flags)) { 52 struct request_queue *q = hctx->queue; 53 54 if (test_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags) || 55 test_and_set_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags)) 56 return; 57 } else { 58 if (test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state) || 59 test_and_set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state)) 60 return; 61 } 62 63 spin_lock_irqsave(&tags->lock, flags); 64 users = tags->active_queues + 1; 65 WRITE_ONCE(tags->active_queues, users); 66 blk_mq_update_wake_batch(tags, users); 67 spin_unlock_irqrestore(&tags->lock, flags); 68 } 69 70 /* 71 * Wakeup all potentially sleeping on tags 72 */ 73 void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool include_reserve) 74 { 75 sbitmap_queue_wake_all(&tags->bitmap_tags); 76 if (include_reserve) 77 sbitmap_queue_wake_all(&tags->breserved_tags); 78 } 79 80 /* 81 * If a previously busy queue goes inactive, potential waiters could now 82 * be allowed to queue. Wake them up and check. 83 */ 84 void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx) 85 { 86 struct blk_mq_tags *tags = hctx->tags; 87 unsigned int users; 88 89 if (blk_mq_is_shared_tags(hctx->flags)) { 90 struct request_queue *q = hctx->queue; 91 92 if (!test_and_clear_bit(QUEUE_FLAG_HCTX_ACTIVE, 93 &q->queue_flags)) 94 return; 95 } else { 96 if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state)) 97 return; 98 } 99 100 spin_lock_irq(&tags->lock); 101 users = tags->active_queues - 1; 102 WRITE_ONCE(tags->active_queues, users); 103 blk_mq_update_wake_batch(tags, users); 104 spin_unlock_irq(&tags->lock); 105 106 blk_mq_tag_wakeup_all(tags, false); 107 } 108 109 static int __blk_mq_get_tag(struct blk_mq_alloc_data *data, 110 struct sbitmap_queue *bt) 111 { 112 if (!data->q->elevator && !(data->flags & BLK_MQ_REQ_RESERVED) && 113 !hctx_may_queue(data->hctx, bt)) 114 return BLK_MQ_NO_TAG; 115 116 if (data->shallow_depth) 117 return sbitmap_queue_get_shallow(bt, data->shallow_depth); 118 else 119 return __sbitmap_queue_get(bt); 120 } 121 122 unsigned long blk_mq_get_tags(struct blk_mq_alloc_data *data, int nr_tags, 123 unsigned int *offset) 124 { 125 struct blk_mq_tags *tags = blk_mq_tags_from_data(data); 126 struct sbitmap_queue *bt = &tags->bitmap_tags; 127 unsigned long ret; 128 129 if (data->shallow_depth ||data->flags & BLK_MQ_REQ_RESERVED || 130 data->hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) 131 return 0; 132 ret = __sbitmap_queue_get_batch(bt, nr_tags, offset); 133 *offset += tags->nr_reserved_tags; 134 return ret; 135 } 136 137 unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data) 138 { 139 struct blk_mq_tags *tags = blk_mq_tags_from_data(data); 140 struct sbitmap_queue *bt; 141 struct sbq_wait_state *ws; 142 DEFINE_SBQ_WAIT(wait); 143 unsigned int tag_offset; 144 int tag; 145 146 if (data->flags & BLK_MQ_REQ_RESERVED) { 147 if (unlikely(!tags->nr_reserved_tags)) { 148 WARN_ON_ONCE(1); 149 return BLK_MQ_NO_TAG; 150 } 151 bt = &tags->breserved_tags; 152 tag_offset = 0; 153 } else { 154 bt = &tags->bitmap_tags; 155 tag_offset = tags->nr_reserved_tags; 156 } 157 158 tag = __blk_mq_get_tag(data, bt); 159 if (tag != BLK_MQ_NO_TAG) 160 goto found_tag; 161 162 if (data->flags & BLK_MQ_REQ_NOWAIT) 163 return BLK_MQ_NO_TAG; 164 165 ws = bt_wait_ptr(bt, data->hctx); 166 do { 167 struct sbitmap_queue *bt_prev; 168 169 /* 170 * We're out of tags on this hardware queue, kick any 171 * pending IO submits before going to sleep waiting for 172 * some to complete. 173 */ 174 blk_mq_run_hw_queue(data->hctx, false); 175 176 /* 177 * Retry tag allocation after running the hardware queue, 178 * as running the queue may also have found completions. 179 */ 180 tag = __blk_mq_get_tag(data, bt); 181 if (tag != BLK_MQ_NO_TAG) 182 break; 183 184 sbitmap_prepare_to_wait(bt, ws, &wait, TASK_UNINTERRUPTIBLE); 185 186 tag = __blk_mq_get_tag(data, bt); 187 if (tag != BLK_MQ_NO_TAG) 188 break; 189 190 bt_prev = bt; 191 io_schedule(); 192 193 sbitmap_finish_wait(bt, ws, &wait); 194 195 data->ctx = blk_mq_get_ctx(data->q); 196 data->hctx = blk_mq_map_queue(data->cmd_flags, data->ctx); 197 tags = blk_mq_tags_from_data(data); 198 if (data->flags & BLK_MQ_REQ_RESERVED) 199 bt = &tags->breserved_tags; 200 else 201 bt = &tags->bitmap_tags; 202 203 /* 204 * If destination hw queue is changed, fake wake up on 205 * previous queue for compensating the wake up miss, so 206 * other allocations on previous queue won't be starved. 207 */ 208 if (bt != bt_prev) 209 sbitmap_queue_wake_up(bt_prev, 1); 210 211 ws = bt_wait_ptr(bt, data->hctx); 212 } while (1); 213 214 sbitmap_finish_wait(bt, ws, &wait); 215 216 found_tag: 217 /* 218 * Give up this allocation if the hctx is inactive. The caller will 219 * retry on an active hctx. 220 */ 221 if (unlikely(test_bit(BLK_MQ_S_INACTIVE, &data->hctx->state))) { 222 blk_mq_put_tag(tags, data->ctx, tag + tag_offset); 223 return BLK_MQ_NO_TAG; 224 } 225 return tag + tag_offset; 226 } 227 228 void blk_mq_put_tag(struct blk_mq_tags *tags, struct blk_mq_ctx *ctx, 229 unsigned int tag) 230 { 231 if (!blk_mq_tag_is_reserved(tags, tag)) { 232 const int real_tag = tag - tags->nr_reserved_tags; 233 234 BUG_ON(real_tag >= tags->nr_tags); 235 sbitmap_queue_clear(&tags->bitmap_tags, real_tag, ctx->cpu); 236 } else { 237 sbitmap_queue_clear(&tags->breserved_tags, tag, ctx->cpu); 238 } 239 } 240 241 void blk_mq_put_tags(struct blk_mq_tags *tags, int *tag_array, int nr_tags) 242 { 243 sbitmap_queue_clear_batch(&tags->bitmap_tags, tags->nr_reserved_tags, 244 tag_array, nr_tags); 245 } 246 247 struct bt_iter_data { 248 struct blk_mq_hw_ctx *hctx; 249 struct request_queue *q; 250 busy_tag_iter_fn *fn; 251 void *data; 252 bool reserved; 253 }; 254 255 static struct request *blk_mq_find_and_get_req(struct blk_mq_tags *tags, 256 unsigned int bitnr) 257 { 258 struct request *rq; 259 260 rq = tags->rqs[bitnr]; 261 if (!rq || rq->tag != bitnr || !req_ref_inc_not_zero(rq)) 262 rq = NULL; 263 return rq; 264 } 265 266 static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data) 267 { 268 struct bt_iter_data *iter_data = data; 269 struct blk_mq_hw_ctx *hctx = iter_data->hctx; 270 struct request_queue *q = iter_data->q; 271 struct blk_mq_tag_set *set = q->tag_set; 272 struct blk_mq_tags *tags; 273 struct request *rq; 274 bool ret = true; 275 276 if (blk_mq_is_shared_tags(set->flags)) 277 tags = set->shared_tags; 278 else 279 tags = hctx->tags; 280 281 if (!iter_data->reserved) 282 bitnr += tags->nr_reserved_tags; 283 /* 284 * We can hit rq == NULL here, because the tagging functions 285 * test and set the bit before assigning ->rqs[]. 286 */ 287 rq = blk_mq_find_and_get_req(tags, bitnr); 288 if (!rq) 289 return true; 290 291 if (rq->q == q && (!hctx || rq->mq_hctx == hctx)) 292 ret = iter_data->fn(rq, iter_data->data); 293 blk_mq_put_rq_ref(rq); 294 return ret; 295 } 296 297 /** 298 * bt_for_each - iterate over the requests associated with a hardware queue 299 * @hctx: Hardware queue to examine. 300 * @q: Request queue @hctx is associated with (@hctx->queue). 301 * @bt: sbitmap to examine. This is either the breserved_tags member 302 * or the bitmap_tags member of struct blk_mq_tags. 303 * @fn: Pointer to the function that will be called for each request 304 * associated with @hctx that has been assigned a driver tag. 305 * @fn will be called as follows: @fn(rq, @data) where rq is a 306 * pointer to a request. Return %true to continue iterating tags; 307 * %false to stop. 308 * @data: Will be passed as second argument to @fn. 309 * @reserved: Indicates whether @bt is the breserved_tags member or the 310 * bitmap_tags member of struct blk_mq_tags. 311 */ 312 static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct request_queue *q, 313 struct sbitmap_queue *bt, busy_tag_iter_fn *fn, 314 void *data, bool reserved) 315 { 316 struct bt_iter_data iter_data = { 317 .hctx = hctx, 318 .fn = fn, 319 .data = data, 320 .reserved = reserved, 321 .q = q, 322 }; 323 324 sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data); 325 } 326 327 struct bt_tags_iter_data { 328 struct blk_mq_tags *tags; 329 busy_tag_iter_fn *fn; 330 void *data; 331 unsigned int flags; 332 }; 333 334 #define BT_TAG_ITER_RESERVED (1 << 0) 335 #define BT_TAG_ITER_STARTED (1 << 1) 336 #define BT_TAG_ITER_STATIC_RQS (1 << 2) 337 338 static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data) 339 { 340 struct bt_tags_iter_data *iter_data = data; 341 struct blk_mq_tags *tags = iter_data->tags; 342 struct request *rq; 343 bool ret = true; 344 bool iter_static_rqs = !!(iter_data->flags & BT_TAG_ITER_STATIC_RQS); 345 346 if (!(iter_data->flags & BT_TAG_ITER_RESERVED)) 347 bitnr += tags->nr_reserved_tags; 348 349 /* 350 * We can hit rq == NULL here, because the tagging functions 351 * test and set the bit before assigning ->rqs[]. 352 */ 353 if (iter_static_rqs) 354 rq = tags->static_rqs[bitnr]; 355 else 356 rq = blk_mq_find_and_get_req(tags, bitnr); 357 if (!rq) 358 return true; 359 360 if (!(iter_data->flags & BT_TAG_ITER_STARTED) || 361 blk_mq_request_started(rq)) 362 ret = iter_data->fn(rq, iter_data->data); 363 if (!iter_static_rqs) 364 blk_mq_put_rq_ref(rq); 365 return ret; 366 } 367 368 /** 369 * bt_tags_for_each - iterate over the requests in a tag map 370 * @tags: Tag map to iterate over. 371 * @bt: sbitmap to examine. This is either the breserved_tags member 372 * or the bitmap_tags member of struct blk_mq_tags. 373 * @fn: Pointer to the function that will be called for each started 374 * request. @fn will be called as follows: @fn(rq, @data) where rq 375 * is a pointer to a request. Return %true to continue iterating 376 * tags; %false to stop. 377 * @data: Will be passed as second argument to @fn. 378 * @flags: BT_TAG_ITER_* 379 */ 380 static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt, 381 busy_tag_iter_fn *fn, void *data, unsigned int flags) 382 { 383 struct bt_tags_iter_data iter_data = { 384 .tags = tags, 385 .fn = fn, 386 .data = data, 387 .flags = flags, 388 }; 389 390 if (tags->rqs) 391 sbitmap_for_each_set(&bt->sb, bt_tags_iter, &iter_data); 392 } 393 394 static void __blk_mq_all_tag_iter(struct blk_mq_tags *tags, 395 busy_tag_iter_fn *fn, void *priv, unsigned int flags) 396 { 397 WARN_ON_ONCE(flags & BT_TAG_ITER_RESERVED); 398 399 if (tags->nr_reserved_tags) 400 bt_tags_for_each(tags, &tags->breserved_tags, fn, priv, 401 flags | BT_TAG_ITER_RESERVED); 402 bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, flags); 403 } 404 405 /** 406 * blk_mq_all_tag_iter - iterate over all requests in a tag map 407 * @tags: Tag map to iterate over. 408 * @fn: Pointer to the function that will be called for each 409 * request. @fn will be called as follows: @fn(rq, @priv) where rq 410 * is a pointer to a request. Return %true to continue iterating 411 * tags; %false to stop. 412 * @priv: Will be passed as second argument to @fn. 413 * 414 * Caller has to pass the tag map from which requests are allocated. 415 */ 416 void blk_mq_all_tag_iter(struct blk_mq_tags *tags, busy_tag_iter_fn *fn, 417 void *priv) 418 { 419 __blk_mq_all_tag_iter(tags, fn, priv, BT_TAG_ITER_STATIC_RQS); 420 } 421 422 /** 423 * blk_mq_tagset_busy_iter - iterate over all started requests in a tag set 424 * @tagset: Tag set to iterate over. 425 * @fn: Pointer to the function that will be called for each started 426 * request. @fn will be called as follows: @fn(rq, @priv) where 427 * rq is a pointer to a request. Return true to continue iterating 428 * tags, false to stop. 429 * @priv: Will be passed as second argument to @fn. 430 * 431 * We grab one request reference before calling @fn and release it after 432 * @fn returns. 433 */ 434 void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset, 435 busy_tag_iter_fn *fn, void *priv) 436 { 437 unsigned int flags = tagset->flags; 438 int i, nr_tags, srcu_idx; 439 440 srcu_idx = srcu_read_lock(&tagset->tags_srcu); 441 442 nr_tags = blk_mq_is_shared_tags(flags) ? 1 : tagset->nr_hw_queues; 443 444 for (i = 0; i < nr_tags; i++) { 445 if (tagset->tags && tagset->tags[i]) 446 __blk_mq_all_tag_iter(tagset->tags[i], fn, priv, 447 BT_TAG_ITER_STARTED); 448 } 449 srcu_read_unlock(&tagset->tags_srcu, srcu_idx); 450 } 451 EXPORT_SYMBOL(blk_mq_tagset_busy_iter); 452 453 static bool blk_mq_tagset_count_completed_rqs(struct request *rq, void *data) 454 { 455 unsigned *count = data; 456 457 if (blk_mq_request_completed(rq)) 458 (*count)++; 459 return true; 460 } 461 462 /** 463 * blk_mq_tagset_wait_completed_request - Wait until all scheduled request 464 * completions have finished. 465 * @tagset: Tag set to drain completed request 466 * 467 * Note: This function has to be run after all IO queues are shutdown 468 */ 469 void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset) 470 { 471 while (true) { 472 unsigned count = 0; 473 474 blk_mq_tagset_busy_iter(tagset, 475 blk_mq_tagset_count_completed_rqs, &count); 476 if (!count) 477 break; 478 msleep(5); 479 } 480 } 481 EXPORT_SYMBOL(blk_mq_tagset_wait_completed_request); 482 483 /** 484 * blk_mq_queue_tag_busy_iter - iterate over all requests with a driver tag 485 * @q: Request queue to examine. 486 * @fn: Pointer to the function that will be called for each request 487 * on @q. @fn will be called as follows: @fn(rq, @priv) where rq 488 * is a pointer to a request and hctx points to the hardware queue 489 * associated with the request. 490 * @priv: Will be passed as second argument to @fn. 491 * 492 * Note: if @q->tag_set is shared with other request queues then @fn will be 493 * called for all requests on all queues that share that tag set and not only 494 * for requests associated with @q. 495 */ 496 void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn, 497 void *priv) 498 { 499 int srcu_idx; 500 501 /* 502 * __blk_mq_update_nr_hw_queues() updates nr_hw_queues and hctx_table 503 * while the queue is frozen. So we can use q_usage_counter to avoid 504 * racing with it. 505 */ 506 if (!percpu_ref_tryget(&q->q_usage_counter)) 507 return; 508 509 srcu_idx = srcu_read_lock(&q->tag_set->tags_srcu); 510 if (blk_mq_is_shared_tags(q->tag_set->flags)) { 511 struct blk_mq_tags *tags = q->tag_set->shared_tags; 512 struct sbitmap_queue *bresv = &tags->breserved_tags; 513 struct sbitmap_queue *btags = &tags->bitmap_tags; 514 515 if (tags->nr_reserved_tags) 516 bt_for_each(NULL, q, bresv, fn, priv, true); 517 bt_for_each(NULL, q, btags, fn, priv, false); 518 } else { 519 struct blk_mq_hw_ctx *hctx; 520 unsigned long i; 521 522 queue_for_each_hw_ctx(q, hctx, i) { 523 struct blk_mq_tags *tags = hctx->tags; 524 struct sbitmap_queue *bresv = &tags->breserved_tags; 525 struct sbitmap_queue *btags = &tags->bitmap_tags; 526 527 /* 528 * If no software queues are currently mapped to this 529 * hardware queue, there's nothing to check 530 */ 531 if (!blk_mq_hw_queue_mapped(hctx)) 532 continue; 533 534 if (tags->nr_reserved_tags) 535 bt_for_each(hctx, q, bresv, fn, priv, true); 536 bt_for_each(hctx, q, btags, fn, priv, false); 537 } 538 } 539 srcu_read_unlock(&q->tag_set->tags_srcu, srcu_idx); 540 blk_queue_exit(q); 541 } 542 543 static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth, 544 bool round_robin, int node) 545 { 546 return sbitmap_queue_init_node(bt, depth, -1, round_robin, GFP_KERNEL, 547 node); 548 } 549 550 struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags, 551 unsigned int reserved_tags, unsigned int flags, int node) 552 { 553 unsigned int depth = total_tags - reserved_tags; 554 bool round_robin = flags & BLK_MQ_F_TAG_RR; 555 struct blk_mq_tags *tags; 556 557 if (total_tags > BLK_MQ_TAG_MAX) { 558 pr_err("blk-mq: tag depth too large\n"); 559 return NULL; 560 } 561 562 tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node); 563 if (!tags) 564 return NULL; 565 566 tags->nr_tags = total_tags; 567 tags->nr_reserved_tags = reserved_tags; 568 spin_lock_init(&tags->lock); 569 INIT_LIST_HEAD(&tags->page_list); 570 571 if (bt_alloc(&tags->bitmap_tags, depth, round_robin, node)) 572 goto out_free_tags; 573 if (bt_alloc(&tags->breserved_tags, reserved_tags, round_robin, node)) 574 goto out_free_bitmap_tags; 575 576 return tags; 577 578 out_free_bitmap_tags: 579 sbitmap_queue_free(&tags->bitmap_tags); 580 out_free_tags: 581 kfree(tags); 582 return NULL; 583 } 584 585 static void blk_mq_free_tags_callback(struct rcu_head *head) 586 { 587 struct blk_mq_tags *tags = container_of(head, struct blk_mq_tags, 588 rcu_head); 589 struct page *page; 590 591 while (!list_empty(&tags->page_list)) { 592 page = list_first_entry(&tags->page_list, struct page, lru); 593 list_del_init(&page->lru); 594 /* 595 * Remove kmemleak object previously allocated in 596 * blk_mq_alloc_rqs(). 597 */ 598 kmemleak_free(page_address(page)); 599 __free_pages(page, page->private); 600 } 601 kfree(tags); 602 } 603 604 void blk_mq_free_tags(struct blk_mq_tag_set *set, struct blk_mq_tags *tags) 605 { 606 sbitmap_queue_free(&tags->bitmap_tags); 607 sbitmap_queue_free(&tags->breserved_tags); 608 609 /* if tags pages is not allocated yet, free tags directly */ 610 if (list_empty(&tags->page_list)) { 611 kfree(tags); 612 return; 613 } 614 615 call_srcu(&set->tags_srcu, &tags->rcu_head, blk_mq_free_tags_callback); 616 } 617 618 void blk_mq_tag_resize_shared_tags(struct blk_mq_tag_set *set, unsigned int size) 619 { 620 struct blk_mq_tags *tags = set->shared_tags; 621 622 sbitmap_queue_resize(&tags->bitmap_tags, size - set->reserved_tags); 623 } 624 625 void blk_mq_tag_update_sched_shared_tags(struct request_queue *q) 626 { 627 sbitmap_queue_resize(&q->sched_shared_tags->bitmap_tags, 628 q->nr_requests - q->tag_set->reserved_tags); 629 } 630 631 /** 632 * blk_mq_unique_tag() - return a tag that is unique queue-wide 633 * @rq: request for which to compute a unique tag 634 * 635 * The tag field in struct request is unique per hardware queue but not over 636 * all hardware queues. Hence this function that returns a tag with the 637 * hardware context index in the upper bits and the per hardware queue tag in 638 * the lower bits. 639 * 640 * Note: When called for a request that is queued on a non-multiqueue request 641 * queue, the hardware context index is set to zero. 642 */ 643 u32 blk_mq_unique_tag(struct request *rq) 644 { 645 return (rq->mq_hctx->queue_num << BLK_MQ_UNIQUE_TAG_BITS) | 646 (rq->tag & BLK_MQ_UNIQUE_TAG_MASK); 647 } 648 EXPORT_SYMBOL(blk_mq_unique_tag); 649