1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Functions related to setting various queue properties from drivers 4 */ 5 #include <linux/kernel.h> 6 #include <linux/module.h> 7 #include <linux/init.h> 8 #include <linux/bio.h> 9 #include <linux/blk-integrity.h> 10 #include <linux/pagemap.h> 11 #include <linux/backing-dev-defs.h> 12 #include <linux/gcd.h> 13 #include <linux/lcm.h> 14 #include <linux/jiffies.h> 15 #include <linux/gfp.h> 16 #include <linux/dma-mapping.h> 17 18 #include "blk.h" 19 #include "blk-rq-qos.h" 20 #include "blk-wbt.h" 21 22 void blk_queue_rq_timeout(struct request_queue *q, unsigned int timeout) 23 { 24 WRITE_ONCE(q->rq_timeout, timeout); 25 } 26 EXPORT_SYMBOL_GPL(blk_queue_rq_timeout); 27 28 /** 29 * blk_set_stacking_limits - set default limits for stacking devices 30 * @lim: the queue_limits structure to reset 31 * 32 * Prepare queue limits for applying limits from underlying devices using 33 * blk_stack_limits(). 34 */ 35 void blk_set_stacking_limits(struct queue_limits *lim) 36 { 37 memset(lim, 0, sizeof(*lim)); 38 lim->logical_block_size = SECTOR_SIZE; 39 lim->physical_block_size = SECTOR_SIZE; 40 lim->io_min = SECTOR_SIZE; 41 lim->discard_granularity = SECTOR_SIZE; 42 lim->dma_alignment = SECTOR_SIZE - 1; 43 lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK; 44 45 /* Inherit limits from component devices */ 46 lim->max_segments = USHRT_MAX; 47 lim->max_discard_segments = USHRT_MAX; 48 lim->max_hw_sectors = UINT_MAX; 49 lim->max_segment_size = UINT_MAX; 50 lim->max_sectors = UINT_MAX; 51 lim->max_dev_sectors = UINT_MAX; 52 lim->max_write_zeroes_sectors = UINT_MAX; 53 lim->max_hw_zone_append_sectors = UINT_MAX; 54 lim->max_user_discard_sectors = UINT_MAX; 55 } 56 EXPORT_SYMBOL(blk_set_stacking_limits); 57 58 void blk_apply_bdi_limits(struct backing_dev_info *bdi, 59 struct queue_limits *lim) 60 { 61 /* 62 * For read-ahead of large files to be effective, we need to read ahead 63 * at least twice the optimal I/O size. 64 */ 65 bdi->ra_pages = max(lim->io_opt * 2 / PAGE_SIZE, VM_READAHEAD_PAGES); 66 bdi->io_pages = lim->max_sectors >> PAGE_SECTORS_SHIFT; 67 } 68 69 static int blk_validate_zoned_limits(struct queue_limits *lim) 70 { 71 if (!(lim->features & BLK_FEAT_ZONED)) { 72 if (WARN_ON_ONCE(lim->max_open_zones) || 73 WARN_ON_ONCE(lim->max_active_zones) || 74 WARN_ON_ONCE(lim->zone_write_granularity) || 75 WARN_ON_ONCE(lim->max_zone_append_sectors)) 76 return -EINVAL; 77 return 0; 78 } 79 80 if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_BLK_DEV_ZONED))) 81 return -EINVAL; 82 83 /* 84 * Given that active zones include open zones, the maximum number of 85 * open zones cannot be larger than the maximum number of active zones. 86 */ 87 if (lim->max_active_zones && 88 lim->max_open_zones > lim->max_active_zones) 89 return -EINVAL; 90 91 if (lim->zone_write_granularity < lim->logical_block_size) 92 lim->zone_write_granularity = lim->logical_block_size; 93 94 /* 95 * The Zone Append size is limited by the maximum I/O size and the zone 96 * size given that it can't span zones. 97 * 98 * If no max_hw_zone_append_sectors limit is provided, the block layer 99 * will emulated it, else we're also bound by the hardware limit. 100 */ 101 lim->max_zone_append_sectors = 102 min_not_zero(lim->max_hw_zone_append_sectors, 103 min(lim->chunk_sectors, lim->max_hw_sectors)); 104 return 0; 105 } 106 107 static int blk_validate_integrity_limits(struct queue_limits *lim) 108 { 109 struct blk_integrity *bi = &lim->integrity; 110 111 if (!bi->tuple_size) { 112 if (bi->csum_type != BLK_INTEGRITY_CSUM_NONE || 113 bi->tag_size || ((bi->flags & BLK_INTEGRITY_REF_TAG))) { 114 pr_warn("invalid PI settings.\n"); 115 return -EINVAL; 116 } 117 bi->flags |= BLK_INTEGRITY_NOGENERATE | BLK_INTEGRITY_NOVERIFY; 118 return 0; 119 } 120 121 if (lim->features & BLK_FEAT_BOUNCE_HIGH) { 122 pr_warn("no bounce buffer support for integrity metadata\n"); 123 return -EINVAL; 124 } 125 126 if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)) { 127 pr_warn("integrity support disabled.\n"); 128 return -EINVAL; 129 } 130 131 if (bi->csum_type == BLK_INTEGRITY_CSUM_NONE && 132 (bi->flags & BLK_INTEGRITY_REF_TAG)) { 133 pr_warn("ref tag not support without checksum.\n"); 134 return -EINVAL; 135 } 136 137 if (!bi->interval_exp) 138 bi->interval_exp = ilog2(lim->logical_block_size); 139 140 return 0; 141 } 142 143 /* 144 * Returns max guaranteed bytes which we can fit in a bio. 145 * 146 * We request that an atomic_write is ITER_UBUF iov_iter (so a single vector), 147 * so we assume that we can fit in at least PAGE_SIZE in a segment, apart from 148 * the first and last segments. 149 */ 150 static unsigned int blk_queue_max_guaranteed_bio(struct queue_limits *lim) 151 { 152 unsigned int max_segments = min(BIO_MAX_VECS, lim->max_segments); 153 unsigned int length; 154 155 length = min(max_segments, 2) * lim->logical_block_size; 156 if (max_segments > 2) 157 length += (max_segments - 2) * PAGE_SIZE; 158 159 return length; 160 } 161 162 static void blk_atomic_writes_update_limits(struct queue_limits *lim) 163 { 164 unsigned int unit_limit = min(lim->max_hw_sectors << SECTOR_SHIFT, 165 blk_queue_max_guaranteed_bio(lim)); 166 167 unit_limit = rounddown_pow_of_two(unit_limit); 168 169 lim->atomic_write_max_sectors = 170 min(lim->atomic_write_hw_max >> SECTOR_SHIFT, 171 lim->max_hw_sectors); 172 lim->atomic_write_unit_min = 173 min(lim->atomic_write_hw_unit_min, unit_limit); 174 lim->atomic_write_unit_max = 175 min(lim->atomic_write_hw_unit_max, unit_limit); 176 lim->atomic_write_boundary_sectors = 177 lim->atomic_write_hw_boundary >> SECTOR_SHIFT; 178 } 179 180 static void blk_validate_atomic_write_limits(struct queue_limits *lim) 181 { 182 unsigned int boundary_sectors; 183 184 if (!(lim->features & BLK_FEAT_ATOMIC_WRITES)) 185 goto unsupported; 186 187 if (!lim->atomic_write_hw_max) 188 goto unsupported; 189 190 if (WARN_ON_ONCE(!is_power_of_2(lim->atomic_write_hw_unit_min))) 191 goto unsupported; 192 193 if (WARN_ON_ONCE(!is_power_of_2(lim->atomic_write_hw_unit_max))) 194 goto unsupported; 195 196 if (WARN_ON_ONCE(lim->atomic_write_hw_unit_min > 197 lim->atomic_write_hw_unit_max)) 198 goto unsupported; 199 200 if (WARN_ON_ONCE(lim->atomic_write_hw_unit_max > 201 lim->atomic_write_hw_max)) 202 goto unsupported; 203 204 boundary_sectors = lim->atomic_write_hw_boundary >> SECTOR_SHIFT; 205 206 if (boundary_sectors) { 207 if (WARN_ON_ONCE(lim->atomic_write_hw_max > 208 lim->atomic_write_hw_boundary)) 209 goto unsupported; 210 /* 211 * A feature of boundary support is that it disallows bios to 212 * be merged which would result in a merged request which 213 * crosses either a chunk sector or atomic write HW boundary, 214 * even though chunk sectors may be just set for performance. 215 * For simplicity, disallow atomic writes for a chunk sector 216 * which is non-zero and smaller than atomic write HW boundary. 217 * Furthermore, chunk sectors must be a multiple of atomic 218 * write HW boundary. Otherwise boundary support becomes 219 * complicated. 220 * Devices which do not conform to these rules can be dealt 221 * with if and when they show up. 222 */ 223 if (WARN_ON_ONCE(lim->chunk_sectors % boundary_sectors)) 224 goto unsupported; 225 226 /* 227 * The boundary size just needs to be a multiple of unit_max 228 * (and not necessarily a power-of-2), so this following check 229 * could be relaxed in future. 230 * Furthermore, if needed, unit_max could even be reduced so 231 * that it is compliant with a !power-of-2 boundary. 232 */ 233 if (!is_power_of_2(boundary_sectors)) 234 goto unsupported; 235 } 236 237 blk_atomic_writes_update_limits(lim); 238 return; 239 240 unsupported: 241 lim->atomic_write_max_sectors = 0; 242 lim->atomic_write_boundary_sectors = 0; 243 lim->atomic_write_unit_min = 0; 244 lim->atomic_write_unit_max = 0; 245 } 246 247 /* 248 * Check that the limits in lim are valid, initialize defaults for unset 249 * values, and cap values based on others where needed. 250 */ 251 int blk_validate_limits(struct queue_limits *lim) 252 { 253 unsigned int max_hw_sectors; 254 unsigned int logical_block_sectors; 255 unsigned long seg_size; 256 int err; 257 258 /* 259 * Unless otherwise specified, default to 512 byte logical blocks and a 260 * physical block size equal to the logical block size. 261 */ 262 if (!lim->logical_block_size) 263 lim->logical_block_size = SECTOR_SIZE; 264 else if (blk_validate_block_size(lim->logical_block_size)) { 265 pr_warn("Invalid logical block size (%d)\n", lim->logical_block_size); 266 return -EINVAL; 267 } 268 if (lim->physical_block_size < lim->logical_block_size) 269 lim->physical_block_size = lim->logical_block_size; 270 271 /* 272 * The minimum I/O size defaults to the physical block size unless 273 * explicitly overridden. 274 */ 275 if (lim->io_min < lim->physical_block_size) 276 lim->io_min = lim->physical_block_size; 277 278 /* 279 * The optimal I/O size may not be aligned to physical block size 280 * (because it may be limited by dma engines which have no clue about 281 * block size of the disks attached to them), so we round it down here. 282 */ 283 lim->io_opt = round_down(lim->io_opt, lim->physical_block_size); 284 285 /* 286 * max_hw_sectors has a somewhat weird default for historical reason, 287 * but driver really should set their own instead of relying on this 288 * value. 289 * 290 * The block layer relies on the fact that every driver can 291 * handle at lest a page worth of data per I/O, and needs the value 292 * aligned to the logical block size. 293 */ 294 if (!lim->max_hw_sectors) 295 lim->max_hw_sectors = BLK_SAFE_MAX_SECTORS; 296 if (WARN_ON_ONCE(lim->max_hw_sectors < PAGE_SECTORS)) 297 return -EINVAL; 298 logical_block_sectors = lim->logical_block_size >> SECTOR_SHIFT; 299 if (WARN_ON_ONCE(logical_block_sectors > lim->max_hw_sectors)) 300 return -EINVAL; 301 lim->max_hw_sectors = round_down(lim->max_hw_sectors, 302 logical_block_sectors); 303 304 /* 305 * The actual max_sectors value is a complex beast and also takes the 306 * max_dev_sectors value (set by SCSI ULPs) and a user configurable 307 * value into account. The ->max_sectors value is always calculated 308 * from these, so directly setting it won't have any effect. 309 */ 310 max_hw_sectors = min_not_zero(lim->max_hw_sectors, 311 lim->max_dev_sectors); 312 if (lim->max_user_sectors) { 313 if (lim->max_user_sectors < BLK_MIN_SEGMENT_SIZE / SECTOR_SIZE) 314 return -EINVAL; 315 lim->max_sectors = min(max_hw_sectors, lim->max_user_sectors); 316 } else if (lim->io_opt > (BLK_DEF_MAX_SECTORS_CAP << SECTOR_SHIFT)) { 317 lim->max_sectors = 318 min(max_hw_sectors, lim->io_opt >> SECTOR_SHIFT); 319 } else if (lim->io_min > (BLK_DEF_MAX_SECTORS_CAP << SECTOR_SHIFT)) { 320 lim->max_sectors = 321 min(max_hw_sectors, lim->io_min >> SECTOR_SHIFT); 322 } else { 323 lim->max_sectors = min(max_hw_sectors, BLK_DEF_MAX_SECTORS_CAP); 324 } 325 lim->max_sectors = round_down(lim->max_sectors, 326 logical_block_sectors); 327 328 /* 329 * Random default for the maximum number of segments. Driver should not 330 * rely on this and set their own. 331 */ 332 if (!lim->max_segments) 333 lim->max_segments = BLK_MAX_SEGMENTS; 334 335 lim->max_discard_sectors = 336 min(lim->max_hw_discard_sectors, lim->max_user_discard_sectors); 337 338 if (!lim->max_discard_segments) 339 lim->max_discard_segments = 1; 340 341 if (lim->discard_granularity < lim->physical_block_size) 342 lim->discard_granularity = lim->physical_block_size; 343 344 /* 345 * By default there is no limit on the segment boundary alignment, 346 * but if there is one it can't be smaller than the page size as 347 * that would break all the normal I/O patterns. 348 */ 349 if (!lim->seg_boundary_mask) 350 lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK; 351 if (WARN_ON_ONCE(lim->seg_boundary_mask < BLK_MIN_SEGMENT_SIZE - 1)) 352 return -EINVAL; 353 354 /* 355 * Stacking device may have both virtual boundary and max segment 356 * size limit, so allow this setting now, and long-term the two 357 * might need to move out of stacking limits since we have immutable 358 * bvec and lower layer bio splitting is supposed to handle the two 359 * correctly. 360 */ 361 if (lim->virt_boundary_mask) { 362 if (!lim->max_segment_size) 363 lim->max_segment_size = UINT_MAX; 364 } else { 365 /* 366 * The maximum segment size has an odd historic 64k default that 367 * drivers probably should override. Just like the I/O size we 368 * require drivers to at least handle a full page per segment. 369 */ 370 if (!lim->max_segment_size) 371 lim->max_segment_size = BLK_MAX_SEGMENT_SIZE; 372 if (WARN_ON_ONCE(lim->max_segment_size < BLK_MIN_SEGMENT_SIZE)) 373 return -EINVAL; 374 } 375 376 /* setup min segment size for building new segment in fast path */ 377 if (lim->seg_boundary_mask > lim->max_segment_size - 1) 378 seg_size = lim->max_segment_size; 379 else 380 seg_size = lim->seg_boundary_mask + 1; 381 lim->min_segment_size = min_t(unsigned int, seg_size, PAGE_SIZE); 382 383 /* 384 * We require drivers to at least do logical block aligned I/O, but 385 * historically could not check for that due to the separate calls 386 * to set the limits. Once the transition is finished the check 387 * below should be narrowed down to check the logical block size. 388 */ 389 if (!lim->dma_alignment) 390 lim->dma_alignment = SECTOR_SIZE - 1; 391 if (WARN_ON_ONCE(lim->dma_alignment > PAGE_SIZE)) 392 return -EINVAL; 393 394 if (lim->alignment_offset) { 395 lim->alignment_offset &= (lim->physical_block_size - 1); 396 lim->flags &= ~BLK_FLAG_MISALIGNED; 397 } 398 399 if (!(lim->features & BLK_FEAT_WRITE_CACHE)) 400 lim->features &= ~BLK_FEAT_FUA; 401 402 blk_validate_atomic_write_limits(lim); 403 404 err = blk_validate_integrity_limits(lim); 405 if (err) 406 return err; 407 return blk_validate_zoned_limits(lim); 408 } 409 EXPORT_SYMBOL_GPL(blk_validate_limits); 410 411 /* 412 * Set the default limits for a newly allocated queue. @lim contains the 413 * initial limits set by the driver, which could be no limit in which case 414 * all fields are cleared to zero. 415 */ 416 int blk_set_default_limits(struct queue_limits *lim) 417 { 418 /* 419 * Most defaults are set by capping the bounds in blk_validate_limits, 420 * but max_user_discard_sectors is special and needs an explicit 421 * initialization to the max value here. 422 */ 423 lim->max_user_discard_sectors = UINT_MAX; 424 return blk_validate_limits(lim); 425 } 426 427 /** 428 * queue_limits_commit_update - commit an atomic update of queue limits 429 * @q: queue to update 430 * @lim: limits to apply 431 * 432 * Apply the limits in @lim that were obtained from queue_limits_start_update() 433 * and updated by the caller to @q. The caller must have frozen the queue or 434 * ensure that there are no outstanding I/Os by other means. 435 * 436 * Returns 0 if successful, else a negative error code. 437 */ 438 int queue_limits_commit_update(struct request_queue *q, 439 struct queue_limits *lim) 440 { 441 int error; 442 443 error = blk_validate_limits(lim); 444 if (error) 445 goto out_unlock; 446 447 #ifdef CONFIG_BLK_INLINE_ENCRYPTION 448 if (q->crypto_profile && lim->integrity.tag_size) { 449 pr_warn("blk-integrity: Integrity and hardware inline encryption are not supported together.\n"); 450 error = -EINVAL; 451 goto out_unlock; 452 } 453 #endif 454 455 q->limits = *lim; 456 if (q->disk) 457 blk_apply_bdi_limits(q->disk->bdi, lim); 458 out_unlock: 459 mutex_unlock(&q->limits_lock); 460 return error; 461 } 462 EXPORT_SYMBOL_GPL(queue_limits_commit_update); 463 464 /** 465 * queue_limits_commit_update_frozen - commit an atomic update of queue limits 466 * @q: queue to update 467 * @lim: limits to apply 468 * 469 * Apply the limits in @lim that were obtained from queue_limits_start_update() 470 * and updated with the new values by the caller to @q. Freezes the queue 471 * before the update and unfreezes it after. 472 * 473 * Returns 0 if successful, else a negative error code. 474 */ 475 int queue_limits_commit_update_frozen(struct request_queue *q, 476 struct queue_limits *lim) 477 { 478 unsigned int memflags; 479 int ret; 480 481 memflags = blk_mq_freeze_queue(q); 482 ret = queue_limits_commit_update(q, lim); 483 blk_mq_unfreeze_queue(q, memflags); 484 485 return ret; 486 } 487 EXPORT_SYMBOL_GPL(queue_limits_commit_update_frozen); 488 489 /** 490 * queue_limits_set - apply queue limits to queue 491 * @q: queue to update 492 * @lim: limits to apply 493 * 494 * Apply the limits in @lim that were freshly initialized to @q. 495 * To update existing limits use queue_limits_start_update() and 496 * queue_limits_commit_update() instead. 497 * 498 * Returns 0 if successful, else a negative error code. 499 */ 500 int queue_limits_set(struct request_queue *q, struct queue_limits *lim) 501 { 502 mutex_lock(&q->limits_lock); 503 return queue_limits_commit_update(q, lim); 504 } 505 EXPORT_SYMBOL_GPL(queue_limits_set); 506 507 static int queue_limit_alignment_offset(const struct queue_limits *lim, 508 sector_t sector) 509 { 510 unsigned int granularity = max(lim->physical_block_size, lim->io_min); 511 unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT) 512 << SECTOR_SHIFT; 513 514 return (granularity + lim->alignment_offset - alignment) % granularity; 515 } 516 517 static unsigned int queue_limit_discard_alignment( 518 const struct queue_limits *lim, sector_t sector) 519 { 520 unsigned int alignment, granularity, offset; 521 522 if (!lim->max_discard_sectors) 523 return 0; 524 525 /* Why are these in bytes, not sectors? */ 526 alignment = lim->discard_alignment >> SECTOR_SHIFT; 527 granularity = lim->discard_granularity >> SECTOR_SHIFT; 528 529 /* Offset of the partition start in 'granularity' sectors */ 530 offset = sector_div(sector, granularity); 531 532 /* And why do we do this modulus *again* in blkdev_issue_discard()? */ 533 offset = (granularity + alignment - offset) % granularity; 534 535 /* Turn it back into bytes, gaah */ 536 return offset << SECTOR_SHIFT; 537 } 538 539 static unsigned int blk_round_down_sectors(unsigned int sectors, unsigned int lbs) 540 { 541 sectors = round_down(sectors, lbs >> SECTOR_SHIFT); 542 if (sectors < PAGE_SIZE >> SECTOR_SHIFT) 543 sectors = PAGE_SIZE >> SECTOR_SHIFT; 544 return sectors; 545 } 546 547 /* Check if second and later bottom devices are compliant */ 548 static bool blk_stack_atomic_writes_tail(struct queue_limits *t, 549 struct queue_limits *b) 550 { 551 /* We're not going to support different boundary sizes.. yet */ 552 if (t->atomic_write_hw_boundary != b->atomic_write_hw_boundary) 553 return false; 554 555 /* Can't support this */ 556 if (t->atomic_write_hw_unit_min > b->atomic_write_hw_unit_max) 557 return false; 558 559 /* Or this */ 560 if (t->atomic_write_hw_unit_max < b->atomic_write_hw_unit_min) 561 return false; 562 563 t->atomic_write_hw_max = min(t->atomic_write_hw_max, 564 b->atomic_write_hw_max); 565 t->atomic_write_hw_unit_min = max(t->atomic_write_hw_unit_min, 566 b->atomic_write_hw_unit_min); 567 t->atomic_write_hw_unit_max = min(t->atomic_write_hw_unit_max, 568 b->atomic_write_hw_unit_max); 569 return true; 570 } 571 572 /* Check for valid boundary of first bottom device */ 573 static bool blk_stack_atomic_writes_boundary_head(struct queue_limits *t, 574 struct queue_limits *b) 575 { 576 /* 577 * Ensure atomic write boundary is aligned with chunk sectors. Stacked 578 * devices store chunk sectors in t->io_min. 579 */ 580 if (b->atomic_write_hw_boundary > t->io_min && 581 b->atomic_write_hw_boundary % t->io_min) 582 return false; 583 if (t->io_min > b->atomic_write_hw_boundary && 584 t->io_min % b->atomic_write_hw_boundary) 585 return false; 586 587 t->atomic_write_hw_boundary = b->atomic_write_hw_boundary; 588 return true; 589 } 590 591 592 /* Check stacking of first bottom device */ 593 static bool blk_stack_atomic_writes_head(struct queue_limits *t, 594 struct queue_limits *b) 595 { 596 if (b->atomic_write_hw_boundary && 597 !blk_stack_atomic_writes_boundary_head(t, b)) 598 return false; 599 600 if (t->io_min <= SECTOR_SIZE) { 601 /* No chunk sectors, so use bottom device values directly */ 602 t->atomic_write_hw_unit_max = b->atomic_write_hw_unit_max; 603 t->atomic_write_hw_unit_min = b->atomic_write_hw_unit_min; 604 t->atomic_write_hw_max = b->atomic_write_hw_max; 605 return true; 606 } 607 608 /* 609 * Find values for limits which work for chunk size. 610 * b->atomic_write_hw_unit_{min, max} may not be aligned with chunk 611 * size (t->io_min), as chunk size is not restricted to a power-of-2. 612 * So we need to find highest power-of-2 which works for the chunk 613 * size. 614 * As an example scenario, we could have b->unit_max = 16K and 615 * t->io_min = 24K. For this case, reduce t->unit_max to a value 616 * aligned with both limits, i.e. 8K in this example. 617 */ 618 t->atomic_write_hw_unit_max = b->atomic_write_hw_unit_max; 619 while (t->io_min % t->atomic_write_hw_unit_max) 620 t->atomic_write_hw_unit_max /= 2; 621 622 t->atomic_write_hw_unit_min = min(b->atomic_write_hw_unit_min, 623 t->atomic_write_hw_unit_max); 624 t->atomic_write_hw_max = min(b->atomic_write_hw_max, t->io_min); 625 626 return true; 627 } 628 629 static void blk_stack_atomic_writes_limits(struct queue_limits *t, 630 struct queue_limits *b, sector_t start) 631 { 632 if (!(b->features & BLK_FEAT_ATOMIC_WRITES)) 633 goto unsupported; 634 635 if (!b->atomic_write_hw_unit_min) 636 goto unsupported; 637 638 if (!blk_atomic_write_start_sect_aligned(start, b)) 639 goto unsupported; 640 641 /* 642 * If atomic_write_hw_max is set, we have already stacked 1x bottom 643 * device, so check for compliance. 644 */ 645 if (t->atomic_write_hw_max) { 646 if (!blk_stack_atomic_writes_tail(t, b)) 647 goto unsupported; 648 return; 649 } 650 651 if (!blk_stack_atomic_writes_head(t, b)) 652 goto unsupported; 653 return; 654 655 unsupported: 656 t->atomic_write_hw_max = 0; 657 t->atomic_write_hw_unit_max = 0; 658 t->atomic_write_hw_unit_min = 0; 659 t->atomic_write_hw_boundary = 0; 660 } 661 662 /** 663 * blk_stack_limits - adjust queue_limits for stacked devices 664 * @t: the stacking driver limits (top device) 665 * @b: the underlying queue limits (bottom, component device) 666 * @start: first data sector within component device 667 * 668 * Description: 669 * This function is used by stacking drivers like MD and DM to ensure 670 * that all component devices have compatible block sizes and 671 * alignments. The stacking driver must provide a queue_limits 672 * struct (top) and then iteratively call the stacking function for 673 * all component (bottom) devices. The stacking function will 674 * attempt to combine the values and ensure proper alignment. 675 * 676 * Returns 0 if the top and bottom queue_limits are compatible. The 677 * top device's block sizes and alignment offsets may be adjusted to 678 * ensure alignment with the bottom device. If no compatible sizes 679 * and alignments exist, -1 is returned and the resulting top 680 * queue_limits will have the misaligned flag set to indicate that 681 * the alignment_offset is undefined. 682 */ 683 int blk_stack_limits(struct queue_limits *t, struct queue_limits *b, 684 sector_t start) 685 { 686 unsigned int top, bottom, alignment, ret = 0; 687 688 t->features |= (b->features & BLK_FEAT_INHERIT_MASK); 689 690 /* 691 * Some feaures need to be supported both by the stacking driver and all 692 * underlying devices. The stacking driver sets these flags before 693 * stacking the limits, and this will clear the flags if any of the 694 * underlying devices does not support it. 695 */ 696 if (!(b->features & BLK_FEAT_NOWAIT)) 697 t->features &= ~BLK_FEAT_NOWAIT; 698 if (!(b->features & BLK_FEAT_POLL)) 699 t->features &= ~BLK_FEAT_POLL; 700 701 t->flags |= (b->flags & BLK_FLAG_MISALIGNED); 702 703 t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors); 704 t->max_user_sectors = min_not_zero(t->max_user_sectors, 705 b->max_user_sectors); 706 t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors); 707 t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors); 708 t->max_write_zeroes_sectors = min(t->max_write_zeroes_sectors, 709 b->max_write_zeroes_sectors); 710 t->max_hw_zone_append_sectors = min(t->max_hw_zone_append_sectors, 711 b->max_hw_zone_append_sectors); 712 713 t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask, 714 b->seg_boundary_mask); 715 t->virt_boundary_mask = min_not_zero(t->virt_boundary_mask, 716 b->virt_boundary_mask); 717 718 t->max_segments = min_not_zero(t->max_segments, b->max_segments); 719 t->max_discard_segments = min_not_zero(t->max_discard_segments, 720 b->max_discard_segments); 721 t->max_integrity_segments = min_not_zero(t->max_integrity_segments, 722 b->max_integrity_segments); 723 724 t->max_segment_size = min_not_zero(t->max_segment_size, 725 b->max_segment_size); 726 727 alignment = queue_limit_alignment_offset(b, start); 728 729 /* Bottom device has different alignment. Check that it is 730 * compatible with the current top alignment. 731 */ 732 if (t->alignment_offset != alignment) { 733 734 top = max(t->physical_block_size, t->io_min) 735 + t->alignment_offset; 736 bottom = max(b->physical_block_size, b->io_min) + alignment; 737 738 /* Verify that top and bottom intervals line up */ 739 if (max(top, bottom) % min(top, bottom)) { 740 t->flags |= BLK_FLAG_MISALIGNED; 741 ret = -1; 742 } 743 } 744 745 t->logical_block_size = max(t->logical_block_size, 746 b->logical_block_size); 747 748 t->physical_block_size = max(t->physical_block_size, 749 b->physical_block_size); 750 751 t->io_min = max(t->io_min, b->io_min); 752 t->io_opt = lcm_not_zero(t->io_opt, b->io_opt); 753 t->dma_alignment = max(t->dma_alignment, b->dma_alignment); 754 755 /* Set non-power-of-2 compatible chunk_sectors boundary */ 756 if (b->chunk_sectors) 757 t->chunk_sectors = gcd(t->chunk_sectors, b->chunk_sectors); 758 759 /* Physical block size a multiple of the logical block size? */ 760 if (t->physical_block_size & (t->logical_block_size - 1)) { 761 t->physical_block_size = t->logical_block_size; 762 t->flags |= BLK_FLAG_MISALIGNED; 763 ret = -1; 764 } 765 766 /* Minimum I/O a multiple of the physical block size? */ 767 if (t->io_min & (t->physical_block_size - 1)) { 768 t->io_min = t->physical_block_size; 769 t->flags |= BLK_FLAG_MISALIGNED; 770 ret = -1; 771 } 772 773 /* Optimal I/O a multiple of the physical block size? */ 774 if (t->io_opt & (t->physical_block_size - 1)) { 775 t->io_opt = 0; 776 t->flags |= BLK_FLAG_MISALIGNED; 777 ret = -1; 778 } 779 780 /* chunk_sectors a multiple of the physical block size? */ 781 if ((t->chunk_sectors << 9) & (t->physical_block_size - 1)) { 782 t->chunk_sectors = 0; 783 t->flags |= BLK_FLAG_MISALIGNED; 784 ret = -1; 785 } 786 787 /* Find lowest common alignment_offset */ 788 t->alignment_offset = lcm_not_zero(t->alignment_offset, alignment) 789 % max(t->physical_block_size, t->io_min); 790 791 /* Verify that new alignment_offset is on a logical block boundary */ 792 if (t->alignment_offset & (t->logical_block_size - 1)) { 793 t->flags |= BLK_FLAG_MISALIGNED; 794 ret = -1; 795 } 796 797 t->max_sectors = blk_round_down_sectors(t->max_sectors, t->logical_block_size); 798 t->max_hw_sectors = blk_round_down_sectors(t->max_hw_sectors, t->logical_block_size); 799 t->max_dev_sectors = blk_round_down_sectors(t->max_dev_sectors, t->logical_block_size); 800 801 /* Discard alignment and granularity */ 802 if (b->discard_granularity) { 803 alignment = queue_limit_discard_alignment(b, start); 804 805 t->max_discard_sectors = min_not_zero(t->max_discard_sectors, 806 b->max_discard_sectors); 807 t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors, 808 b->max_hw_discard_sectors); 809 t->discard_granularity = max(t->discard_granularity, 810 b->discard_granularity); 811 t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) % 812 t->discard_granularity; 813 } 814 t->max_secure_erase_sectors = min_not_zero(t->max_secure_erase_sectors, 815 b->max_secure_erase_sectors); 816 t->zone_write_granularity = max(t->zone_write_granularity, 817 b->zone_write_granularity); 818 if (!(t->features & BLK_FEAT_ZONED)) { 819 t->zone_write_granularity = 0; 820 t->max_zone_append_sectors = 0; 821 } 822 blk_stack_atomic_writes_limits(t, b, start); 823 824 return ret; 825 } 826 EXPORT_SYMBOL(blk_stack_limits); 827 828 /** 829 * queue_limits_stack_bdev - adjust queue_limits for stacked devices 830 * @t: the stacking driver limits (top device) 831 * @bdev: the underlying block device (bottom) 832 * @offset: offset to beginning of data within component device 833 * @pfx: prefix to use for warnings logged 834 * 835 * Description: 836 * This function is used by stacking drivers like MD and DM to ensure 837 * that all component devices have compatible block sizes and 838 * alignments. The stacking driver must provide a queue_limits 839 * struct (top) and then iteratively call the stacking function for 840 * all component (bottom) devices. The stacking function will 841 * attempt to combine the values and ensure proper alignment. 842 */ 843 void queue_limits_stack_bdev(struct queue_limits *t, struct block_device *bdev, 844 sector_t offset, const char *pfx) 845 { 846 if (blk_stack_limits(t, bdev_limits(bdev), 847 get_start_sect(bdev) + offset)) 848 pr_notice("%s: Warning: Device %pg is misaligned\n", 849 pfx, bdev); 850 } 851 EXPORT_SYMBOL_GPL(queue_limits_stack_bdev); 852 853 /** 854 * queue_limits_stack_integrity - stack integrity profile 855 * @t: target queue limits 856 * @b: base queue limits 857 * 858 * Check if the integrity profile in the @b can be stacked into the 859 * target @t. Stacking is possible if either: 860 * 861 * a) does not have any integrity information stacked into it yet 862 * b) the integrity profile in @b is identical to the one in @t 863 * 864 * If @b can be stacked into @t, return %true. Else return %false and clear the 865 * integrity information in @t. 866 */ 867 bool queue_limits_stack_integrity(struct queue_limits *t, 868 struct queue_limits *b) 869 { 870 struct blk_integrity *ti = &t->integrity; 871 struct blk_integrity *bi = &b->integrity; 872 873 if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)) 874 return true; 875 876 if (ti->flags & BLK_INTEGRITY_STACKED) { 877 if (ti->tuple_size != bi->tuple_size) 878 goto incompatible; 879 if (ti->interval_exp != bi->interval_exp) 880 goto incompatible; 881 if (ti->tag_size != bi->tag_size) 882 goto incompatible; 883 if (ti->csum_type != bi->csum_type) 884 goto incompatible; 885 if ((ti->flags & BLK_INTEGRITY_REF_TAG) != 886 (bi->flags & BLK_INTEGRITY_REF_TAG)) 887 goto incompatible; 888 } else { 889 ti->flags = BLK_INTEGRITY_STACKED; 890 ti->flags |= (bi->flags & BLK_INTEGRITY_DEVICE_CAPABLE) | 891 (bi->flags & BLK_INTEGRITY_REF_TAG); 892 ti->csum_type = bi->csum_type; 893 ti->tuple_size = bi->tuple_size; 894 ti->pi_offset = bi->pi_offset; 895 ti->interval_exp = bi->interval_exp; 896 ti->tag_size = bi->tag_size; 897 } 898 return true; 899 900 incompatible: 901 memset(ti, 0, sizeof(*ti)); 902 return false; 903 } 904 EXPORT_SYMBOL_GPL(queue_limits_stack_integrity); 905 906 /** 907 * blk_set_queue_depth - tell the block layer about the device queue depth 908 * @q: the request queue for the device 909 * @depth: queue depth 910 * 911 */ 912 void blk_set_queue_depth(struct request_queue *q, unsigned int depth) 913 { 914 q->queue_depth = depth; 915 rq_qos_queue_depth_changed(q); 916 } 917 EXPORT_SYMBOL(blk_set_queue_depth); 918 919 int bdev_alignment_offset(struct block_device *bdev) 920 { 921 struct request_queue *q = bdev_get_queue(bdev); 922 923 if (q->limits.flags & BLK_FLAG_MISALIGNED) 924 return -1; 925 if (bdev_is_partition(bdev)) 926 return queue_limit_alignment_offset(&q->limits, 927 bdev->bd_start_sect); 928 return q->limits.alignment_offset; 929 } 930 EXPORT_SYMBOL_GPL(bdev_alignment_offset); 931 932 unsigned int bdev_discard_alignment(struct block_device *bdev) 933 { 934 struct request_queue *q = bdev_get_queue(bdev); 935 936 if (bdev_is_partition(bdev)) 937 return queue_limit_discard_alignment(&q->limits, 938 bdev->bd_start_sect); 939 return q->limits.discard_alignment; 940 } 941 EXPORT_SYMBOL_GPL(bdev_discard_alignment); 942