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