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 return 0; 210 } 211 212 /* 213 * Returns max guaranteed bytes which we can fit in a bio. 214 * 215 * We request that an atomic_write is ITER_UBUF iov_iter (so a single vector), 216 * so we assume that we can fit in at least PAGE_SIZE in a segment, apart from 217 * the first and last segments. 218 */ 219 static unsigned int blk_queue_max_guaranteed_bio(struct queue_limits *lim) 220 { 221 unsigned int max_segments = min(BIO_MAX_VECS, lim->max_segments); 222 unsigned int length; 223 224 length = min(max_segments, 2) * lim->logical_block_size; 225 if (max_segments > 2) 226 length += (max_segments - 2) * PAGE_SIZE; 227 228 return length; 229 } 230 231 static void blk_atomic_writes_update_limits(struct queue_limits *lim) 232 { 233 unsigned int integrity_max = lim->integrity.metadata_size ? 234 max_integrity_io_size(lim) : 0; 235 unsigned int unit_limit = min(lim->max_hw_sectors << SECTOR_SHIFT, 236 blk_queue_max_guaranteed_bio(lim)); 237 238 unit_limit = min_not_zero(unit_limit, integrity_max); 239 unit_limit = rounddown_pow_of_two(unit_limit); 240 241 lim->atomic_write_max_sectors = 242 min(lim->atomic_write_hw_max >> SECTOR_SHIFT, 243 lim->max_hw_sectors); 244 lim->atomic_write_max_sectors = 245 min_not_zero(lim->atomic_write_max_sectors, 246 integrity_max >> SECTOR_SHIFT); 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 err = blk_validate_integrity_limits(lim); 515 if (err) 516 return err; 517 518 /* atomics limits depend on integrity limits */ 519 blk_validate_atomic_write_limits(lim); 520 521 return blk_validate_zoned_limits(lim); 522 } 523 EXPORT_SYMBOL_GPL(blk_validate_limits); 524 525 /* 526 * Set the default limits for a newly allocated queue. @lim contains the 527 * initial limits set by the driver, which could be no limit in which case 528 * all fields are cleared to zero. 529 */ 530 int blk_set_default_limits(struct queue_limits *lim) 531 { 532 /* 533 * Most defaults are set by capping the bounds in blk_validate_limits, 534 * but these limits are special and need an explicit initialization to 535 * the max value here. 536 */ 537 lim->max_user_discard_sectors = UINT_MAX; 538 lim->max_user_wzeroes_unmap_sectors = UINT_MAX; 539 return blk_validate_limits(lim); 540 } 541 542 /** 543 * queue_limits_commit_update - commit an atomic update of queue limits 544 * @q: queue to update 545 * @lim: limits to apply 546 * 547 * Apply the limits in @lim that were obtained from queue_limits_start_update() 548 * and updated by the caller to @q. The caller must have frozen the queue or 549 * ensure that there are no outstanding I/Os by other means. 550 * 551 * Returns 0 if successful, else a negative error code. 552 */ 553 int queue_limits_commit_update(struct request_queue *q, 554 struct queue_limits *lim) 555 { 556 int error; 557 558 lockdep_assert_held(&q->limits_lock); 559 560 error = blk_validate_limits(lim); 561 if (error) 562 goto out_unlock; 563 564 #ifdef CONFIG_BLK_INLINE_ENCRYPTION 565 if (q->crypto_profile && lim->integrity.tag_size) { 566 pr_warn("blk-integrity: Integrity and hardware inline encryption are not supported together.\n"); 567 error = -EINVAL; 568 goto out_unlock; 569 } 570 #endif 571 572 q->limits = *lim; 573 if (q->disk) 574 blk_apply_bdi_limits(q->disk->bdi, lim); 575 out_unlock: 576 mutex_unlock(&q->limits_lock); 577 return error; 578 } 579 EXPORT_SYMBOL_GPL(queue_limits_commit_update); 580 581 /** 582 * queue_limits_commit_update_frozen - commit an atomic update of queue limits 583 * @q: queue to update 584 * @lim: limits to apply 585 * 586 * Apply the limits in @lim that were obtained from queue_limits_start_update() 587 * and updated with the new values by the caller to @q. Freezes the queue 588 * before the update and unfreezes it after. 589 * 590 * Returns 0 if successful, else a negative error code. 591 */ 592 int queue_limits_commit_update_frozen(struct request_queue *q, 593 struct queue_limits *lim) 594 { 595 unsigned int memflags; 596 int ret; 597 598 memflags = blk_mq_freeze_queue(q); 599 ret = queue_limits_commit_update(q, lim); 600 blk_mq_unfreeze_queue(q, memflags); 601 602 return ret; 603 } 604 EXPORT_SYMBOL_GPL(queue_limits_commit_update_frozen); 605 606 /** 607 * queue_limits_set - apply queue limits to queue 608 * @q: queue to update 609 * @lim: limits to apply 610 * 611 * Apply the limits in @lim that were freshly initialized to @q. 612 * To update existing limits use queue_limits_start_update() and 613 * queue_limits_commit_update() instead. 614 * 615 * Returns 0 if successful, else a negative error code. 616 */ 617 int queue_limits_set(struct request_queue *q, struct queue_limits *lim) 618 { 619 mutex_lock(&q->limits_lock); 620 return queue_limits_commit_update(q, lim); 621 } 622 EXPORT_SYMBOL_GPL(queue_limits_set); 623 624 static int queue_limit_alignment_offset(const struct queue_limits *lim, 625 sector_t sector) 626 { 627 unsigned int granularity = max(lim->physical_block_size, lim->io_min); 628 unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT) 629 << SECTOR_SHIFT; 630 631 return (granularity + lim->alignment_offset - alignment) % granularity; 632 } 633 634 static unsigned int queue_limit_discard_alignment( 635 const struct queue_limits *lim, sector_t sector) 636 { 637 unsigned int alignment, granularity, offset; 638 639 if (!lim->max_discard_sectors) 640 return 0; 641 642 /* Why are these in bytes, not sectors? */ 643 alignment = lim->discard_alignment >> SECTOR_SHIFT; 644 granularity = lim->discard_granularity >> SECTOR_SHIFT; 645 646 /* Offset of the partition start in 'granularity' sectors */ 647 offset = sector_div(sector, granularity); 648 649 /* And why do we do this modulus *again* in blkdev_issue_discard()? */ 650 offset = (granularity + alignment - offset) % granularity; 651 652 /* Turn it back into bytes, gaah */ 653 return offset << SECTOR_SHIFT; 654 } 655 656 static unsigned int blk_round_down_sectors(unsigned int sectors, unsigned int lbs) 657 { 658 sectors = round_down(sectors, lbs >> SECTOR_SHIFT); 659 if (sectors < PAGE_SIZE >> SECTOR_SHIFT) 660 sectors = PAGE_SIZE >> SECTOR_SHIFT; 661 return sectors; 662 } 663 664 /* Check if second and later bottom devices are compliant */ 665 static bool blk_stack_atomic_writes_tail(struct queue_limits *t, 666 struct queue_limits *b) 667 { 668 /* We're not going to support different boundary sizes.. yet */ 669 if (t->atomic_write_hw_boundary != b->atomic_write_hw_boundary) 670 return false; 671 672 /* Can't support this */ 673 if (t->atomic_write_hw_unit_min > b->atomic_write_hw_unit_max) 674 return false; 675 676 /* Or this */ 677 if (t->atomic_write_hw_unit_max < b->atomic_write_hw_unit_min) 678 return false; 679 680 t->atomic_write_hw_max = min(t->atomic_write_hw_max, 681 b->atomic_write_hw_max); 682 t->atomic_write_hw_unit_min = max(t->atomic_write_hw_unit_min, 683 b->atomic_write_hw_unit_min); 684 t->atomic_write_hw_unit_max = min(t->atomic_write_hw_unit_max, 685 b->atomic_write_hw_unit_max); 686 return true; 687 } 688 689 static void blk_stack_atomic_writes_chunk_sectors(struct queue_limits *t) 690 { 691 unsigned int chunk_bytes; 692 693 if (!t->chunk_sectors) 694 return; 695 696 /* 697 * If chunk sectors is so large that its value in bytes overflows 698 * UINT_MAX, then just shift it down so it definitely will fit. 699 * We don't support atomic writes of such a large size anyway. 700 */ 701 if (check_shl_overflow(t->chunk_sectors, SECTOR_SHIFT, &chunk_bytes)) 702 chunk_bytes = t->chunk_sectors; 703 704 /* 705 * Find values for limits which work for chunk size. 706 * b->atomic_write_hw_unit_{min, max} may not be aligned with chunk 707 * size, as the chunk size is not restricted to a power-of-2. 708 * So we need to find highest power-of-2 which works for the chunk 709 * size. 710 * As an example scenario, we could have t->unit_max = 16K and 711 * t->chunk_sectors = 24KB. For this case, reduce t->unit_max to a 712 * value aligned with both limits, i.e. 8K in this example. 713 */ 714 t->atomic_write_hw_unit_max = min(t->atomic_write_hw_unit_max, 715 max_pow_of_two_factor(chunk_bytes)); 716 717 t->atomic_write_hw_unit_min = min(t->atomic_write_hw_unit_min, 718 t->atomic_write_hw_unit_max); 719 t->atomic_write_hw_max = min(t->atomic_write_hw_max, chunk_bytes); 720 } 721 722 /* Check stacking of first bottom device */ 723 static bool blk_stack_atomic_writes_head(struct queue_limits *t, 724 struct queue_limits *b) 725 { 726 if (!blk_valid_atomic_writes_boundary(t->chunk_sectors, 727 b->atomic_write_hw_boundary >> SECTOR_SHIFT)) 728 return false; 729 730 t->atomic_write_hw_unit_max = b->atomic_write_hw_unit_max; 731 t->atomic_write_hw_unit_min = b->atomic_write_hw_unit_min; 732 t->atomic_write_hw_max = b->atomic_write_hw_max; 733 t->atomic_write_hw_boundary = b->atomic_write_hw_boundary; 734 return true; 735 } 736 737 static void blk_stack_atomic_writes_limits(struct queue_limits *t, 738 struct queue_limits *b, sector_t start) 739 { 740 if (!(b->features & BLK_FEAT_ATOMIC_WRITES)) 741 goto unsupported; 742 743 if (!b->atomic_write_hw_unit_min) 744 goto unsupported; 745 746 if (!blk_atomic_write_start_sect_aligned(start, b)) 747 goto unsupported; 748 749 /* UINT_MAX indicates no stacking of bottom devices yet */ 750 if (t->atomic_write_hw_max == UINT_MAX) { 751 if (!blk_stack_atomic_writes_head(t, b)) 752 goto unsupported; 753 } else { 754 if (!blk_stack_atomic_writes_tail(t, b)) 755 goto unsupported; 756 } 757 blk_stack_atomic_writes_chunk_sectors(t); 758 return; 759 760 unsupported: 761 t->atomic_write_hw_max = 0; 762 t->atomic_write_hw_unit_max = 0; 763 t->atomic_write_hw_unit_min = 0; 764 t->atomic_write_hw_boundary = 0; 765 } 766 767 /** 768 * blk_stack_limits - adjust queue_limits for stacked devices 769 * @t: the stacking driver limits (top device) 770 * @b: the underlying queue limits (bottom, component device) 771 * @start: first data sector within component device 772 * 773 * Description: 774 * This function is used by stacking drivers like MD and DM to ensure 775 * that all component devices have compatible block sizes and 776 * alignments. The stacking driver must provide a queue_limits 777 * struct (top) and then iteratively call the stacking function for 778 * all component (bottom) devices. The stacking function will 779 * attempt to combine the values and ensure proper alignment. 780 * 781 * Returns 0 if the top and bottom queue_limits are compatible. The 782 * top device's block sizes and alignment offsets may be adjusted to 783 * ensure alignment with the bottom device. If no compatible sizes 784 * and alignments exist, -1 is returned and the resulting top 785 * queue_limits will have the misaligned flag set to indicate that 786 * the alignment_offset is undefined. 787 */ 788 int blk_stack_limits(struct queue_limits *t, struct queue_limits *b, 789 sector_t start) 790 { 791 unsigned int top, bottom, alignment; 792 int ret = 0; 793 794 t->features |= (b->features & BLK_FEAT_INHERIT_MASK); 795 796 /* 797 * Some feaures need to be supported both by the stacking driver and all 798 * underlying devices. The stacking driver sets these flags before 799 * stacking the limits, and this will clear the flags if any of the 800 * underlying devices does not support it. 801 */ 802 if (!(b->features & BLK_FEAT_NOWAIT)) 803 t->features &= ~BLK_FEAT_NOWAIT; 804 if (!(b->features & BLK_FEAT_POLL)) 805 t->features &= ~BLK_FEAT_POLL; 806 if (!(b->features & BLK_FEAT_PCI_P2PDMA)) 807 t->features &= ~BLK_FEAT_PCI_P2PDMA; 808 809 t->flags |= (b->flags & BLK_FLAG_MISALIGNED); 810 811 t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors); 812 t->max_user_sectors = min_not_zero(t->max_user_sectors, 813 b->max_user_sectors); 814 t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors); 815 t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors); 816 t->max_write_zeroes_sectors = min(t->max_write_zeroes_sectors, 817 b->max_write_zeroes_sectors); 818 t->max_user_wzeroes_unmap_sectors = 819 min(t->max_user_wzeroes_unmap_sectors, 820 b->max_user_wzeroes_unmap_sectors); 821 t->max_hw_wzeroes_unmap_sectors = 822 min(t->max_hw_wzeroes_unmap_sectors, 823 b->max_hw_wzeroes_unmap_sectors); 824 825 t->max_hw_zone_append_sectors = min(t->max_hw_zone_append_sectors, 826 b->max_hw_zone_append_sectors); 827 828 t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask, 829 b->seg_boundary_mask); 830 t->virt_boundary_mask = min_not_zero(t->virt_boundary_mask, 831 b->virt_boundary_mask); 832 833 t->max_segments = min_not_zero(t->max_segments, b->max_segments); 834 t->max_discard_segments = min_not_zero(t->max_discard_segments, 835 b->max_discard_segments); 836 t->max_integrity_segments = min_not_zero(t->max_integrity_segments, 837 b->max_integrity_segments); 838 839 t->max_segment_size = min_not_zero(t->max_segment_size, 840 b->max_segment_size); 841 842 alignment = queue_limit_alignment_offset(b, start); 843 844 /* Bottom device has different alignment. Check that it is 845 * compatible with the current top alignment. 846 */ 847 if (t->alignment_offset != alignment) { 848 849 top = max(t->physical_block_size, t->io_min) 850 + t->alignment_offset; 851 bottom = max(b->physical_block_size, b->io_min) + alignment; 852 853 /* Verify that top and bottom intervals line up */ 854 if (max(top, bottom) % min(top, bottom)) { 855 t->flags |= BLK_FLAG_MISALIGNED; 856 ret = -1; 857 } 858 } 859 860 t->logical_block_size = max(t->logical_block_size, 861 b->logical_block_size); 862 863 t->physical_block_size = max(t->physical_block_size, 864 b->physical_block_size); 865 866 t->io_min = max(t->io_min, b->io_min); 867 t->io_opt = lcm_not_zero(t->io_opt, b->io_opt); 868 t->dma_alignment = max(t->dma_alignment, b->dma_alignment); 869 870 /* Set non-power-of-2 compatible chunk_sectors boundary */ 871 if (b->chunk_sectors) 872 t->chunk_sectors = gcd(t->chunk_sectors, b->chunk_sectors); 873 874 /* Physical block size a multiple of the logical block size? */ 875 if (t->physical_block_size & (t->logical_block_size - 1)) { 876 t->physical_block_size = t->logical_block_size; 877 t->flags |= BLK_FLAG_MISALIGNED; 878 ret = -1; 879 } 880 881 /* Minimum I/O a multiple of the physical block size? */ 882 if (t->io_min & (t->physical_block_size - 1)) { 883 t->io_min = t->physical_block_size; 884 t->flags |= BLK_FLAG_MISALIGNED; 885 ret = -1; 886 } 887 888 /* Optimal I/O a multiple of the physical block size? */ 889 if (t->io_opt & (t->physical_block_size - 1)) { 890 t->io_opt = 0; 891 t->flags |= BLK_FLAG_MISALIGNED; 892 ret = -1; 893 } 894 895 /* chunk_sectors a multiple of the physical block size? */ 896 if (t->chunk_sectors % (t->physical_block_size >> SECTOR_SHIFT)) { 897 t->chunk_sectors = 0; 898 t->flags |= BLK_FLAG_MISALIGNED; 899 ret = -1; 900 } 901 902 /* Find lowest common alignment_offset */ 903 t->alignment_offset = lcm_not_zero(t->alignment_offset, alignment) 904 % max(t->physical_block_size, t->io_min); 905 906 /* Verify that new alignment_offset is on a logical block boundary */ 907 if (t->alignment_offset & (t->logical_block_size - 1)) { 908 t->flags |= BLK_FLAG_MISALIGNED; 909 ret = -1; 910 } 911 912 t->max_sectors = blk_round_down_sectors(t->max_sectors, t->logical_block_size); 913 t->max_hw_sectors = blk_round_down_sectors(t->max_hw_sectors, t->logical_block_size); 914 t->max_dev_sectors = blk_round_down_sectors(t->max_dev_sectors, t->logical_block_size); 915 916 /* Discard alignment and granularity */ 917 if (b->discard_granularity) { 918 alignment = queue_limit_discard_alignment(b, start); 919 920 t->max_discard_sectors = min_not_zero(t->max_discard_sectors, 921 b->max_discard_sectors); 922 t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors, 923 b->max_hw_discard_sectors); 924 t->discard_granularity = max(t->discard_granularity, 925 b->discard_granularity); 926 t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) % 927 t->discard_granularity; 928 } 929 t->max_secure_erase_sectors = min_not_zero(t->max_secure_erase_sectors, 930 b->max_secure_erase_sectors); 931 t->zone_write_granularity = max(t->zone_write_granularity, 932 b->zone_write_granularity); 933 if (!(t->features & BLK_FEAT_ZONED)) { 934 t->zone_write_granularity = 0; 935 t->max_zone_append_sectors = 0; 936 } 937 blk_stack_atomic_writes_limits(t, b, start); 938 939 return ret; 940 } 941 EXPORT_SYMBOL(blk_stack_limits); 942 943 /** 944 * queue_limits_stack_bdev - adjust queue_limits for stacked devices 945 * @t: the stacking driver limits (top device) 946 * @bdev: the underlying block device (bottom) 947 * @offset: offset to beginning of data within component device 948 * @pfx: prefix to use for warnings logged 949 * 950 * Description: 951 * This function is used by stacking drivers like MD and DM to ensure 952 * that all component devices have compatible block sizes and 953 * alignments. The stacking driver must provide a queue_limits 954 * struct (top) and then iteratively call the stacking function for 955 * all component (bottom) devices. The stacking function will 956 * attempt to combine the values and ensure proper alignment. 957 */ 958 void queue_limits_stack_bdev(struct queue_limits *t, struct block_device *bdev, 959 sector_t offset, const char *pfx) 960 { 961 if (blk_stack_limits(t, bdev_limits(bdev), 962 get_start_sect(bdev) + offset)) 963 pr_notice("%s: Warning: Device %pg is misaligned\n", 964 pfx, bdev); 965 } 966 EXPORT_SYMBOL_GPL(queue_limits_stack_bdev); 967 968 /** 969 * queue_limits_stack_integrity - stack integrity profile 970 * @t: target queue limits 971 * @b: base queue limits 972 * 973 * Check if the integrity profile in the @b can be stacked into the 974 * target @t. Stacking is possible if either: 975 * 976 * a) does not have any integrity information stacked into it yet 977 * b) the integrity profile in @b is identical to the one in @t 978 * 979 * If @b can be stacked into @t, return %true. Else return %false and clear the 980 * integrity information in @t. 981 */ 982 bool queue_limits_stack_integrity(struct queue_limits *t, 983 struct queue_limits *b) 984 { 985 struct blk_integrity *ti = &t->integrity; 986 struct blk_integrity *bi = &b->integrity; 987 988 if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)) 989 return true; 990 991 if (ti->flags & BLK_INTEGRITY_STACKED) { 992 if (ti->metadata_size != bi->metadata_size) 993 goto incompatible; 994 if (ti->interval_exp != bi->interval_exp) 995 goto incompatible; 996 if (ti->tag_size != bi->tag_size) 997 goto incompatible; 998 if (ti->csum_type != bi->csum_type) 999 goto incompatible; 1000 if (ti->pi_tuple_size != bi->pi_tuple_size) 1001 goto incompatible; 1002 if ((ti->flags & BLK_INTEGRITY_REF_TAG) != 1003 (bi->flags & BLK_INTEGRITY_REF_TAG)) 1004 goto incompatible; 1005 if ((ti->flags & BLK_SPLIT_INTERVAL_CAPABLE) && 1006 !(bi->flags & BLK_SPLIT_INTERVAL_CAPABLE)) 1007 ti->flags &= ~BLK_SPLIT_INTERVAL_CAPABLE; 1008 } else { 1009 ti->flags = BLK_INTEGRITY_STACKED; 1010 ti->flags |= (bi->flags & BLK_INTEGRITY_DEVICE_CAPABLE) | 1011 (bi->flags & BLK_INTEGRITY_REF_TAG) | 1012 (bi->flags & BLK_SPLIT_INTERVAL_CAPABLE); 1013 ti->csum_type = bi->csum_type; 1014 ti->pi_tuple_size = bi->pi_tuple_size; 1015 ti->metadata_size = bi->metadata_size; 1016 ti->pi_offset = bi->pi_offset; 1017 ti->interval_exp = bi->interval_exp; 1018 ti->tag_size = bi->tag_size; 1019 } 1020 return true; 1021 1022 incompatible: 1023 memset(ti, 0, sizeof(*ti)); 1024 return false; 1025 } 1026 EXPORT_SYMBOL_GPL(queue_limits_stack_integrity); 1027 1028 /** 1029 * blk_set_queue_depth - tell the block layer about the device queue depth 1030 * @q: the request queue for the device 1031 * @depth: queue depth 1032 * 1033 */ 1034 void blk_set_queue_depth(struct request_queue *q, unsigned int depth) 1035 { 1036 q->queue_depth = depth; 1037 rq_qos_queue_depth_changed(q); 1038 } 1039 EXPORT_SYMBOL(blk_set_queue_depth); 1040 1041 int bdev_alignment_offset(struct block_device *bdev) 1042 { 1043 struct request_queue *q = bdev_get_queue(bdev); 1044 1045 if (q->limits.flags & BLK_FLAG_MISALIGNED) 1046 return -1; 1047 if (bdev_is_partition(bdev)) 1048 return queue_limit_alignment_offset(&q->limits, 1049 bdev->bd_start_sect); 1050 return q->limits.alignment_offset; 1051 } 1052 EXPORT_SYMBOL_GPL(bdev_alignment_offset); 1053 1054 unsigned int bdev_discard_alignment(struct block_device *bdev) 1055 { 1056 struct request_queue *q = bdev_get_queue(bdev); 1057 1058 if (bdev_is_partition(bdev)) 1059 return queue_limit_discard_alignment(&q->limits, 1060 bdev->bd_start_sect); 1061 return q->limits.discard_alignment; 1062 } 1063 EXPORT_SYMBOL_GPL(bdev_discard_alignment); 1064