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