1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Zoned block device handling 4 * 5 * Copyright (c) 2015, Hannes Reinecke 6 * Copyright (c) 2015, SUSE Linux GmbH 7 * 8 * Copyright (c) 2016, Damien Le Moal 9 * Copyright (c) 2016, Western Digital 10 * Copyright (c) 2024, Western Digital Corporation or its affiliates. 11 */ 12 13 #include <linux/kernel.h> 14 #include <linux/module.h> 15 #include <linux/blkdev.h> 16 #include <linux/blk-mq.h> 17 #include <linux/mm.h> 18 #include <linux/vmalloc.h> 19 #include <linux/sched/mm.h> 20 #include <linux/spinlock.h> 21 #include <linux/refcount.h> 22 #include <linux/mempool.h> 23 24 #include "blk.h" 25 #include "blk-mq-sched.h" 26 #include "blk-mq-debugfs.h" 27 28 #define ZONE_COND_NAME(name) [BLK_ZONE_COND_##name] = #name 29 static const char *const zone_cond_name[] = { 30 ZONE_COND_NAME(NOT_WP), 31 ZONE_COND_NAME(EMPTY), 32 ZONE_COND_NAME(IMP_OPEN), 33 ZONE_COND_NAME(EXP_OPEN), 34 ZONE_COND_NAME(CLOSED), 35 ZONE_COND_NAME(READONLY), 36 ZONE_COND_NAME(FULL), 37 ZONE_COND_NAME(OFFLINE), 38 }; 39 #undef ZONE_COND_NAME 40 41 /* 42 * Per-zone write plug. 43 * @node: hlist_node structure for managing the plug using a hash table. 44 * @ref: Zone write plug reference counter. A zone write plug reference is 45 * always at least 1 when the plug is hashed in the disk plug hash table. 46 * The reference is incremented whenever a new BIO needing plugging is 47 * submitted and when a function needs to manipulate a plug. The 48 * reference count is decremented whenever a plugged BIO completes and 49 * when a function that referenced the plug returns. The initial 50 * reference is dropped whenever the zone of the zone write plug is reset, 51 * finished and when the zone becomes full (last write BIO to the zone 52 * completes). 53 * @lock: Spinlock to atomically manipulate the plug. 54 * @flags: Flags indicating the plug state. 55 * @zone_no: The number of the zone the plug is managing. 56 * @wp_offset: The zone write pointer location relative to the start of the zone 57 * as a number of 512B sectors. 58 * @bio_list: The list of BIOs that are currently plugged. 59 * @bio_work: Work struct to handle issuing of plugged BIOs 60 * @rcu_head: RCU head to free zone write plugs with an RCU grace period. 61 * @disk: The gendisk the plug belongs to. 62 */ 63 struct blk_zone_wplug { 64 struct hlist_node node; 65 refcount_t ref; 66 spinlock_t lock; 67 unsigned int flags; 68 unsigned int zone_no; 69 unsigned int wp_offset; 70 struct bio_list bio_list; 71 struct work_struct bio_work; 72 struct rcu_head rcu_head; 73 struct gendisk *disk; 74 }; 75 76 /* 77 * Zone write plug flags bits: 78 * - BLK_ZONE_WPLUG_PLUGGED: Indicates that the zone write plug is plugged, 79 * that is, that write BIOs are being throttled due to a write BIO already 80 * being executed or the zone write plug bio list is not empty. 81 * - BLK_ZONE_WPLUG_NEED_WP_UPDATE: Indicates that we lost track of a zone 82 * write pointer offset and need to update it. 83 * - BLK_ZONE_WPLUG_UNHASHED: Indicates that the zone write plug was removed 84 * from the disk hash table and that the initial reference to the zone 85 * write plug set when the plug was first added to the hash table has been 86 * dropped. This flag is set when a zone is reset, finished or become full, 87 * to prevent new references to the zone write plug to be taken for 88 * newly incoming BIOs. A zone write plug flagged with this flag will be 89 * freed once all remaining references from BIOs or functions are dropped. 90 */ 91 #define BLK_ZONE_WPLUG_PLUGGED (1U << 0) 92 #define BLK_ZONE_WPLUG_NEED_WP_UPDATE (1U << 1) 93 #define BLK_ZONE_WPLUG_UNHASHED (1U << 2) 94 95 /** 96 * blk_zone_cond_str - Return string XXX in BLK_ZONE_COND_XXX. 97 * @zone_cond: BLK_ZONE_COND_XXX. 98 * 99 * Description: Centralize block layer function to convert BLK_ZONE_COND_XXX 100 * into string format. Useful in the debugging and tracing zone conditions. For 101 * invalid BLK_ZONE_COND_XXX it returns string "UNKNOWN". 102 */ 103 const char *blk_zone_cond_str(enum blk_zone_cond zone_cond) 104 { 105 static const char *zone_cond_str = "UNKNOWN"; 106 107 if (zone_cond < ARRAY_SIZE(zone_cond_name) && zone_cond_name[zone_cond]) 108 zone_cond_str = zone_cond_name[zone_cond]; 109 110 return zone_cond_str; 111 } 112 EXPORT_SYMBOL_GPL(blk_zone_cond_str); 113 114 struct disk_report_zones_cb_args { 115 struct gendisk *disk; 116 report_zones_cb user_cb; 117 void *user_data; 118 }; 119 120 static void disk_zone_wplug_sync_wp_offset(struct gendisk *disk, 121 struct blk_zone *zone); 122 123 static int disk_report_zones_cb(struct blk_zone *zone, unsigned int idx, 124 void *data) 125 { 126 struct disk_report_zones_cb_args *args = data; 127 struct gendisk *disk = args->disk; 128 129 if (disk->zone_wplugs_hash) 130 disk_zone_wplug_sync_wp_offset(disk, zone); 131 132 if (!args->user_cb) 133 return 0; 134 135 return args->user_cb(zone, idx, args->user_data); 136 } 137 138 /** 139 * blkdev_report_zones - Get zones information 140 * @bdev: Target block device 141 * @sector: Sector from which to report zones 142 * @nr_zones: Maximum number of zones to report 143 * @cb: Callback function called for each reported zone 144 * @data: Private data for the callback 145 * 146 * Description: 147 * Get zone information starting from the zone containing @sector for at most 148 * @nr_zones, and call @cb for each zone reported by the device. 149 * To report all zones in a device starting from @sector, the BLK_ALL_ZONES 150 * constant can be passed to @nr_zones. 151 * Returns the number of zones reported by the device, or a negative errno 152 * value in case of failure. 153 * 154 * Note: The caller must use memalloc_noXX_save/restore() calls to control 155 * memory allocations done within this function. 156 */ 157 int blkdev_report_zones(struct block_device *bdev, sector_t sector, 158 unsigned int nr_zones, report_zones_cb cb, void *data) 159 { 160 struct gendisk *disk = bdev->bd_disk; 161 sector_t capacity = get_capacity(disk); 162 struct disk_report_zones_cb_args args = { 163 .disk = disk, 164 .user_cb = cb, 165 .user_data = data, 166 }; 167 168 if (!bdev_is_zoned(bdev) || WARN_ON_ONCE(!disk->fops->report_zones)) 169 return -EOPNOTSUPP; 170 171 if (!nr_zones || sector >= capacity) 172 return 0; 173 174 return disk->fops->report_zones(disk, sector, nr_zones, 175 disk_report_zones_cb, &args); 176 } 177 EXPORT_SYMBOL_GPL(blkdev_report_zones); 178 179 static int blkdev_zone_reset_all(struct block_device *bdev) 180 { 181 struct bio bio; 182 183 bio_init(&bio, bdev, NULL, 0, REQ_OP_ZONE_RESET_ALL | REQ_SYNC); 184 return submit_bio_wait(&bio); 185 } 186 187 /** 188 * blkdev_zone_mgmt - Execute a zone management operation on a range of zones 189 * @bdev: Target block device 190 * @op: Operation to be performed on the zones 191 * @sector: Start sector of the first zone to operate on 192 * @nr_sectors: Number of sectors, should be at least the length of one zone and 193 * must be zone size aligned. 194 * 195 * Description: 196 * Perform the specified operation on the range of zones specified by 197 * @sector..@sector+@nr_sectors. Specifying the entire disk sector range 198 * is valid, but the specified range should not contain conventional zones. 199 * The operation to execute on each zone can be a zone reset, open, close 200 * or finish request. 201 */ 202 int blkdev_zone_mgmt(struct block_device *bdev, enum req_op op, 203 sector_t sector, sector_t nr_sectors) 204 { 205 sector_t zone_sectors = bdev_zone_sectors(bdev); 206 sector_t capacity = bdev_nr_sectors(bdev); 207 sector_t end_sector = sector + nr_sectors; 208 struct bio *bio = NULL; 209 int ret = 0; 210 211 if (!bdev_is_zoned(bdev)) 212 return -EOPNOTSUPP; 213 214 if (bdev_read_only(bdev)) 215 return -EPERM; 216 217 if (!op_is_zone_mgmt(op)) 218 return -EOPNOTSUPP; 219 220 if (end_sector <= sector || end_sector > capacity) 221 /* Out of range */ 222 return -EINVAL; 223 224 /* Check alignment (handle eventual smaller last zone) */ 225 if (!bdev_is_zone_start(bdev, sector)) 226 return -EINVAL; 227 228 if (!bdev_is_zone_start(bdev, nr_sectors) && end_sector != capacity) 229 return -EINVAL; 230 231 /* 232 * In the case of a zone reset operation over all zones, use 233 * REQ_OP_ZONE_RESET_ALL. 234 */ 235 if (op == REQ_OP_ZONE_RESET && sector == 0 && nr_sectors == capacity) 236 return blkdev_zone_reset_all(bdev); 237 238 while (sector < end_sector) { 239 bio = blk_next_bio(bio, bdev, 0, op | REQ_SYNC, GFP_KERNEL); 240 bio->bi_iter.bi_sector = sector; 241 sector += zone_sectors; 242 243 /* This may take a while, so be nice to others */ 244 cond_resched(); 245 } 246 247 ret = submit_bio_wait(bio); 248 bio_put(bio); 249 250 return ret; 251 } 252 EXPORT_SYMBOL_GPL(blkdev_zone_mgmt); 253 254 struct zone_report_args { 255 struct blk_zone __user *zones; 256 }; 257 258 static int blkdev_copy_zone_to_user(struct blk_zone *zone, unsigned int idx, 259 void *data) 260 { 261 struct zone_report_args *args = data; 262 263 if (copy_to_user(&args->zones[idx], zone, sizeof(struct blk_zone))) 264 return -EFAULT; 265 return 0; 266 } 267 268 /* 269 * BLKREPORTZONE ioctl processing. 270 * Called from blkdev_ioctl. 271 */ 272 int blkdev_report_zones_ioctl(struct block_device *bdev, unsigned int cmd, 273 unsigned long arg) 274 { 275 void __user *argp = (void __user *)arg; 276 struct zone_report_args args; 277 struct blk_zone_report rep; 278 int ret; 279 280 if (!argp) 281 return -EINVAL; 282 283 if (!bdev_is_zoned(bdev)) 284 return -ENOTTY; 285 286 if (copy_from_user(&rep, argp, sizeof(struct blk_zone_report))) 287 return -EFAULT; 288 289 if (!rep.nr_zones) 290 return -EINVAL; 291 292 args.zones = argp + sizeof(struct blk_zone_report); 293 ret = blkdev_report_zones(bdev, rep.sector, rep.nr_zones, 294 blkdev_copy_zone_to_user, &args); 295 if (ret < 0) 296 return ret; 297 298 rep.nr_zones = ret; 299 rep.flags = BLK_ZONE_REP_CAPACITY; 300 if (copy_to_user(argp, &rep, sizeof(struct blk_zone_report))) 301 return -EFAULT; 302 return 0; 303 } 304 305 static int blkdev_truncate_zone_range(struct block_device *bdev, 306 blk_mode_t mode, const struct blk_zone_range *zrange) 307 { 308 loff_t start, end; 309 310 if (zrange->sector + zrange->nr_sectors <= zrange->sector || 311 zrange->sector + zrange->nr_sectors > get_capacity(bdev->bd_disk)) 312 /* Out of range */ 313 return -EINVAL; 314 315 start = zrange->sector << SECTOR_SHIFT; 316 end = ((zrange->sector + zrange->nr_sectors) << SECTOR_SHIFT) - 1; 317 318 return truncate_bdev_range(bdev, mode, start, end); 319 } 320 321 /* 322 * BLKRESETZONE, BLKOPENZONE, BLKCLOSEZONE and BLKFINISHZONE ioctl processing. 323 * Called from blkdev_ioctl. 324 */ 325 int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode, 326 unsigned int cmd, unsigned long arg) 327 { 328 void __user *argp = (void __user *)arg; 329 struct blk_zone_range zrange; 330 enum req_op op; 331 int ret; 332 333 if (!argp) 334 return -EINVAL; 335 336 if (!bdev_is_zoned(bdev)) 337 return -ENOTTY; 338 339 if (!(mode & BLK_OPEN_WRITE)) 340 return -EBADF; 341 342 if (copy_from_user(&zrange, argp, sizeof(struct blk_zone_range))) 343 return -EFAULT; 344 345 switch (cmd) { 346 case BLKRESETZONE: 347 op = REQ_OP_ZONE_RESET; 348 349 /* Invalidate the page cache, including dirty pages. */ 350 filemap_invalidate_lock(bdev->bd_mapping); 351 ret = blkdev_truncate_zone_range(bdev, mode, &zrange); 352 if (ret) 353 goto fail; 354 break; 355 case BLKOPENZONE: 356 op = REQ_OP_ZONE_OPEN; 357 break; 358 case BLKCLOSEZONE: 359 op = REQ_OP_ZONE_CLOSE; 360 break; 361 case BLKFINISHZONE: 362 op = REQ_OP_ZONE_FINISH; 363 break; 364 default: 365 return -ENOTTY; 366 } 367 368 ret = blkdev_zone_mgmt(bdev, op, zrange.sector, zrange.nr_sectors); 369 370 fail: 371 if (cmd == BLKRESETZONE) 372 filemap_invalidate_unlock(bdev->bd_mapping); 373 374 return ret; 375 } 376 377 static bool disk_zone_is_last(struct gendisk *disk, struct blk_zone *zone) 378 { 379 return zone->start + zone->len >= get_capacity(disk); 380 } 381 382 static bool disk_zone_is_full(struct gendisk *disk, 383 unsigned int zno, unsigned int offset_in_zone) 384 { 385 if (zno < disk->nr_zones - 1) 386 return offset_in_zone >= disk->zone_capacity; 387 return offset_in_zone >= disk->last_zone_capacity; 388 } 389 390 static bool disk_zone_wplug_is_full(struct gendisk *disk, 391 struct blk_zone_wplug *zwplug) 392 { 393 return disk_zone_is_full(disk, zwplug->zone_no, zwplug->wp_offset); 394 } 395 396 static bool disk_insert_zone_wplug(struct gendisk *disk, 397 struct blk_zone_wplug *zwplug) 398 { 399 struct blk_zone_wplug *zwplg; 400 unsigned long flags; 401 unsigned int idx = 402 hash_32(zwplug->zone_no, disk->zone_wplugs_hash_bits); 403 404 /* 405 * Add the new zone write plug to the hash table, but carefully as we 406 * are racing with other submission context, so we may already have a 407 * zone write plug for the same zone. 408 */ 409 spin_lock_irqsave(&disk->zone_wplugs_lock, flags); 410 hlist_for_each_entry_rcu(zwplg, &disk->zone_wplugs_hash[idx], node) { 411 if (zwplg->zone_no == zwplug->zone_no) { 412 spin_unlock_irqrestore(&disk->zone_wplugs_lock, flags); 413 return false; 414 } 415 } 416 hlist_add_head_rcu(&zwplug->node, &disk->zone_wplugs_hash[idx]); 417 spin_unlock_irqrestore(&disk->zone_wplugs_lock, flags); 418 419 return true; 420 } 421 422 static struct blk_zone_wplug *disk_get_zone_wplug(struct gendisk *disk, 423 sector_t sector) 424 { 425 unsigned int zno = disk_zone_no(disk, sector); 426 unsigned int idx = hash_32(zno, disk->zone_wplugs_hash_bits); 427 struct blk_zone_wplug *zwplug; 428 429 rcu_read_lock(); 430 431 hlist_for_each_entry_rcu(zwplug, &disk->zone_wplugs_hash[idx], node) { 432 if (zwplug->zone_no == zno && 433 refcount_inc_not_zero(&zwplug->ref)) { 434 rcu_read_unlock(); 435 return zwplug; 436 } 437 } 438 439 rcu_read_unlock(); 440 441 return NULL; 442 } 443 444 static void disk_free_zone_wplug_rcu(struct rcu_head *rcu_head) 445 { 446 struct blk_zone_wplug *zwplug = 447 container_of(rcu_head, struct blk_zone_wplug, rcu_head); 448 449 mempool_free(zwplug, zwplug->disk->zone_wplugs_pool); 450 } 451 452 static inline void disk_put_zone_wplug(struct blk_zone_wplug *zwplug) 453 { 454 if (refcount_dec_and_test(&zwplug->ref)) { 455 WARN_ON_ONCE(!bio_list_empty(&zwplug->bio_list)); 456 WARN_ON_ONCE(zwplug->flags & BLK_ZONE_WPLUG_PLUGGED); 457 WARN_ON_ONCE(!(zwplug->flags & BLK_ZONE_WPLUG_UNHASHED)); 458 459 call_rcu(&zwplug->rcu_head, disk_free_zone_wplug_rcu); 460 } 461 } 462 463 static inline bool disk_should_remove_zone_wplug(struct gendisk *disk, 464 struct blk_zone_wplug *zwplug) 465 { 466 /* If the zone write plug was already removed, we are done. */ 467 if (zwplug->flags & BLK_ZONE_WPLUG_UNHASHED) 468 return false; 469 470 /* If the zone write plug is still plugged, it cannot be removed. */ 471 if (zwplug->flags & BLK_ZONE_WPLUG_PLUGGED) 472 return false; 473 474 /* 475 * Completions of BIOs with blk_zone_write_plug_bio_endio() may 476 * happen after handling a request completion with 477 * blk_zone_write_plug_finish_request() (e.g. with split BIOs 478 * that are chained). In such case, disk_zone_wplug_unplug_bio() 479 * should not attempt to remove the zone write plug until all BIO 480 * completions are seen. Check by looking at the zone write plug 481 * reference count, which is 2 when the plug is unused (one reference 482 * taken when the plug was allocated and another reference taken by the 483 * caller context). 484 */ 485 if (refcount_read(&zwplug->ref) > 2) 486 return false; 487 488 /* We can remove zone write plugs for zones that are empty or full. */ 489 return !zwplug->wp_offset || disk_zone_wplug_is_full(disk, zwplug); 490 } 491 492 static void disk_remove_zone_wplug(struct gendisk *disk, 493 struct blk_zone_wplug *zwplug) 494 { 495 unsigned long flags; 496 497 /* If the zone write plug was already removed, we have nothing to do. */ 498 if (zwplug->flags & BLK_ZONE_WPLUG_UNHASHED) 499 return; 500 501 /* 502 * Mark the zone write plug as unhashed and drop the extra reference we 503 * took when the plug was inserted in the hash table. 504 */ 505 zwplug->flags |= BLK_ZONE_WPLUG_UNHASHED; 506 spin_lock_irqsave(&disk->zone_wplugs_lock, flags); 507 hlist_del_init_rcu(&zwplug->node); 508 spin_unlock_irqrestore(&disk->zone_wplugs_lock, flags); 509 disk_put_zone_wplug(zwplug); 510 } 511 512 static void blk_zone_wplug_bio_work(struct work_struct *work); 513 514 /* 515 * Get a reference on the write plug for the zone containing @sector. 516 * If the plug does not exist, it is allocated and hashed. 517 * Return a pointer to the zone write plug with the plug spinlock held. 518 */ 519 static struct blk_zone_wplug *disk_get_and_lock_zone_wplug(struct gendisk *disk, 520 sector_t sector, gfp_t gfp_mask, 521 unsigned long *flags) 522 { 523 unsigned int zno = disk_zone_no(disk, sector); 524 struct blk_zone_wplug *zwplug; 525 526 again: 527 zwplug = disk_get_zone_wplug(disk, sector); 528 if (zwplug) { 529 /* 530 * Check that a BIO completion or a zone reset or finish 531 * operation has not already removed the zone write plug from 532 * the hash table and dropped its reference count. In such case, 533 * we need to get a new plug so start over from the beginning. 534 */ 535 spin_lock_irqsave(&zwplug->lock, *flags); 536 if (zwplug->flags & BLK_ZONE_WPLUG_UNHASHED) { 537 spin_unlock_irqrestore(&zwplug->lock, *flags); 538 disk_put_zone_wplug(zwplug); 539 goto again; 540 } 541 return zwplug; 542 } 543 544 /* 545 * Allocate and initialize a zone write plug with an extra reference 546 * so that it is not freed when the zone write plug becomes idle without 547 * the zone being full. 548 */ 549 zwplug = mempool_alloc(disk->zone_wplugs_pool, gfp_mask); 550 if (!zwplug) 551 return NULL; 552 553 INIT_HLIST_NODE(&zwplug->node); 554 refcount_set(&zwplug->ref, 2); 555 spin_lock_init(&zwplug->lock); 556 zwplug->flags = 0; 557 zwplug->zone_no = zno; 558 zwplug->wp_offset = bdev_offset_from_zone_start(disk->part0, sector); 559 bio_list_init(&zwplug->bio_list); 560 INIT_WORK(&zwplug->bio_work, blk_zone_wplug_bio_work); 561 zwplug->disk = disk; 562 563 spin_lock_irqsave(&zwplug->lock, *flags); 564 565 /* 566 * Insert the new zone write plug in the hash table. This can fail only 567 * if another context already inserted a plug. Retry from the beginning 568 * in such case. 569 */ 570 if (!disk_insert_zone_wplug(disk, zwplug)) { 571 spin_unlock_irqrestore(&zwplug->lock, *flags); 572 mempool_free(zwplug, disk->zone_wplugs_pool); 573 goto again; 574 } 575 576 return zwplug; 577 } 578 579 static inline void blk_zone_wplug_bio_io_error(struct blk_zone_wplug *zwplug, 580 struct bio *bio) 581 { 582 struct request_queue *q = zwplug->disk->queue; 583 584 bio_clear_flag(bio, BIO_ZONE_WRITE_PLUGGING); 585 bio_io_error(bio); 586 disk_put_zone_wplug(zwplug); 587 blk_queue_exit(q); 588 } 589 590 /* 591 * Abort (fail) all plugged BIOs of a zone write plug. 592 */ 593 static void disk_zone_wplug_abort(struct blk_zone_wplug *zwplug) 594 { 595 struct bio *bio; 596 597 while ((bio = bio_list_pop(&zwplug->bio_list))) 598 blk_zone_wplug_bio_io_error(zwplug, bio); 599 } 600 601 /* 602 * Set a zone write plug write pointer offset to the specified value. 603 * This aborts all plugged BIOs, which is fine as this function is called for 604 * a zone reset operation, a zone finish operation or if the zone needs a wp 605 * update from a report zone after a write error. 606 */ 607 static void disk_zone_wplug_set_wp_offset(struct gendisk *disk, 608 struct blk_zone_wplug *zwplug, 609 unsigned int wp_offset) 610 { 611 lockdep_assert_held(&zwplug->lock); 612 613 /* Update the zone write pointer and abort all plugged BIOs. */ 614 zwplug->flags &= ~BLK_ZONE_WPLUG_NEED_WP_UPDATE; 615 zwplug->wp_offset = wp_offset; 616 disk_zone_wplug_abort(zwplug); 617 618 /* 619 * The zone write plug now has no BIO plugged: remove it from the 620 * hash table so that it cannot be seen. The plug will be freed 621 * when the last reference is dropped. 622 */ 623 if (disk_should_remove_zone_wplug(disk, zwplug)) 624 disk_remove_zone_wplug(disk, zwplug); 625 } 626 627 static unsigned int blk_zone_wp_offset(struct blk_zone *zone) 628 { 629 switch (zone->cond) { 630 case BLK_ZONE_COND_IMP_OPEN: 631 case BLK_ZONE_COND_EXP_OPEN: 632 case BLK_ZONE_COND_CLOSED: 633 return zone->wp - zone->start; 634 case BLK_ZONE_COND_FULL: 635 return zone->len; 636 case BLK_ZONE_COND_EMPTY: 637 return 0; 638 case BLK_ZONE_COND_NOT_WP: 639 case BLK_ZONE_COND_OFFLINE: 640 case BLK_ZONE_COND_READONLY: 641 default: 642 /* 643 * Conventional, offline and read-only zones do not have a valid 644 * write pointer. 645 */ 646 return UINT_MAX; 647 } 648 } 649 650 static void disk_zone_wplug_sync_wp_offset(struct gendisk *disk, 651 struct blk_zone *zone) 652 { 653 struct blk_zone_wplug *zwplug; 654 unsigned long flags; 655 656 zwplug = disk_get_zone_wplug(disk, zone->start); 657 if (!zwplug) 658 return; 659 660 spin_lock_irqsave(&zwplug->lock, flags); 661 if (zwplug->flags & BLK_ZONE_WPLUG_NEED_WP_UPDATE) 662 disk_zone_wplug_set_wp_offset(disk, zwplug, 663 blk_zone_wp_offset(zone)); 664 spin_unlock_irqrestore(&zwplug->lock, flags); 665 666 disk_put_zone_wplug(zwplug); 667 } 668 669 static int disk_zone_sync_wp_offset(struct gendisk *disk, sector_t sector) 670 { 671 struct disk_report_zones_cb_args args = { 672 .disk = disk, 673 }; 674 675 return disk->fops->report_zones(disk, sector, 1, 676 disk_report_zones_cb, &args); 677 } 678 679 static bool blk_zone_wplug_handle_reset_or_finish(struct bio *bio, 680 unsigned int wp_offset) 681 { 682 struct gendisk *disk = bio->bi_bdev->bd_disk; 683 sector_t sector = bio->bi_iter.bi_sector; 684 struct blk_zone_wplug *zwplug; 685 unsigned long flags; 686 687 /* Conventional zones cannot be reset nor finished. */ 688 if (!bdev_zone_is_seq(bio->bi_bdev, sector)) { 689 bio_io_error(bio); 690 return true; 691 } 692 693 /* 694 * No-wait reset or finish BIOs do not make much sense as the callers 695 * issue these as blocking operations in most cases. To avoid issues 696 * the BIO execution potentially failing with BLK_STS_AGAIN, warn about 697 * REQ_NOWAIT being set and ignore that flag. 698 */ 699 if (WARN_ON_ONCE(bio->bi_opf & REQ_NOWAIT)) 700 bio->bi_opf &= ~REQ_NOWAIT; 701 702 /* 703 * If we have a zone write plug, set its write pointer offset to 0 704 * (reset case) or to the zone size (finish case). This will abort all 705 * BIOs plugged for the target zone. It is fine as resetting or 706 * finishing zones while writes are still in-flight will result in the 707 * writes failing anyway. 708 */ 709 zwplug = disk_get_zone_wplug(disk, sector); 710 if (zwplug) { 711 spin_lock_irqsave(&zwplug->lock, flags); 712 disk_zone_wplug_set_wp_offset(disk, zwplug, wp_offset); 713 spin_unlock_irqrestore(&zwplug->lock, flags); 714 disk_put_zone_wplug(zwplug); 715 } 716 717 return false; 718 } 719 720 static bool blk_zone_wplug_handle_reset_all(struct bio *bio) 721 { 722 struct gendisk *disk = bio->bi_bdev->bd_disk; 723 struct blk_zone_wplug *zwplug; 724 unsigned long flags; 725 sector_t sector; 726 727 /* 728 * Set the write pointer offset of all zone write plugs to 0. This will 729 * abort all plugged BIOs. It is fine as resetting zones while writes 730 * are still in-flight will result in the writes failing anyway. 731 */ 732 for (sector = 0; sector < get_capacity(disk); 733 sector += disk->queue->limits.chunk_sectors) { 734 zwplug = disk_get_zone_wplug(disk, sector); 735 if (zwplug) { 736 spin_lock_irqsave(&zwplug->lock, flags); 737 disk_zone_wplug_set_wp_offset(disk, zwplug, 0); 738 spin_unlock_irqrestore(&zwplug->lock, flags); 739 disk_put_zone_wplug(zwplug); 740 } 741 } 742 743 return false; 744 } 745 746 static void disk_zone_wplug_schedule_bio_work(struct gendisk *disk, 747 struct blk_zone_wplug *zwplug) 748 { 749 /* 750 * Take a reference on the zone write plug and schedule the submission 751 * of the next plugged BIO. blk_zone_wplug_bio_work() will release the 752 * reference we take here. 753 */ 754 WARN_ON_ONCE(!(zwplug->flags & BLK_ZONE_WPLUG_PLUGGED)); 755 refcount_inc(&zwplug->ref); 756 queue_work(disk->zone_wplugs_wq, &zwplug->bio_work); 757 } 758 759 static inline void disk_zone_wplug_add_bio(struct gendisk *disk, 760 struct blk_zone_wplug *zwplug, 761 struct bio *bio, unsigned int nr_segs) 762 { 763 bool schedule_bio_work = false; 764 765 /* 766 * Grab an extra reference on the BIO request queue usage counter. 767 * This reference will be reused to submit a request for the BIO for 768 * blk-mq devices and dropped when the BIO is failed and after 769 * it is issued in the case of BIO-based devices. 770 */ 771 percpu_ref_get(&bio->bi_bdev->bd_disk->queue->q_usage_counter); 772 773 /* 774 * The BIO is being plugged and thus will have to wait for the on-going 775 * write and for all other writes already plugged. So polling makes 776 * no sense. 777 */ 778 bio_clear_polled(bio); 779 780 /* 781 * REQ_NOWAIT BIOs are always handled using the zone write plug BIO 782 * work, which can block. So clear the REQ_NOWAIT flag and schedule the 783 * work if this is the first BIO we are plugging. 784 */ 785 if (bio->bi_opf & REQ_NOWAIT) { 786 schedule_bio_work = !(zwplug->flags & BLK_ZONE_WPLUG_PLUGGED); 787 bio->bi_opf &= ~REQ_NOWAIT; 788 } 789 790 /* 791 * Reuse the poll cookie field to store the number of segments when 792 * split to the hardware limits. 793 */ 794 bio->__bi_nr_segments = nr_segs; 795 796 /* 797 * We always receive BIOs after they are split and ready to be issued. 798 * The block layer passes the parts of a split BIO in order, and the 799 * user must also issue write sequentially. So simply add the new BIO 800 * at the tail of the list to preserve the sequential write order. 801 */ 802 bio_list_add(&zwplug->bio_list, bio); 803 804 zwplug->flags |= BLK_ZONE_WPLUG_PLUGGED; 805 806 if (schedule_bio_work) 807 disk_zone_wplug_schedule_bio_work(disk, zwplug); 808 } 809 810 /* 811 * Called from bio_attempt_back_merge() when a BIO was merged with a request. 812 */ 813 void blk_zone_write_plug_bio_merged(struct bio *bio) 814 { 815 struct blk_zone_wplug *zwplug; 816 unsigned long flags; 817 818 /* 819 * If the BIO was already plugged, then we were called through 820 * blk_zone_write_plug_init_request() -> blk_attempt_bio_merge(). 821 * For this case, we already hold a reference on the zone write plug for 822 * the BIO and blk_zone_write_plug_init_request() will handle the 823 * zone write pointer offset update. 824 */ 825 if (bio_flagged(bio, BIO_ZONE_WRITE_PLUGGING)) 826 return; 827 828 bio_set_flag(bio, BIO_ZONE_WRITE_PLUGGING); 829 830 /* 831 * Get a reference on the zone write plug of the target zone and advance 832 * the zone write pointer offset. Given that this is a merge, we already 833 * have at least one request and one BIO referencing the zone write 834 * plug. So this should not fail. 835 */ 836 zwplug = disk_get_zone_wplug(bio->bi_bdev->bd_disk, 837 bio->bi_iter.bi_sector); 838 if (WARN_ON_ONCE(!zwplug)) 839 return; 840 841 spin_lock_irqsave(&zwplug->lock, flags); 842 zwplug->wp_offset += bio_sectors(bio); 843 spin_unlock_irqrestore(&zwplug->lock, flags); 844 } 845 846 /* 847 * Attempt to merge plugged BIOs with a newly prepared request for a BIO that 848 * already went through zone write plugging (either a new BIO or one that was 849 * unplugged). 850 */ 851 void blk_zone_write_plug_init_request(struct request *req) 852 { 853 sector_t req_back_sector = blk_rq_pos(req) + blk_rq_sectors(req); 854 struct request_queue *q = req->q; 855 struct gendisk *disk = q->disk; 856 struct blk_zone_wplug *zwplug = 857 disk_get_zone_wplug(disk, blk_rq_pos(req)); 858 unsigned long flags; 859 struct bio *bio; 860 861 if (WARN_ON_ONCE(!zwplug)) 862 return; 863 864 /* 865 * Indicate that completion of this request needs to be handled with 866 * blk_zone_write_plug_finish_request(), which will drop the reference 867 * on the zone write plug we took above on entry to this function. 868 */ 869 req->rq_flags |= RQF_ZONE_WRITE_PLUGGING; 870 871 if (blk_queue_nomerges(q)) 872 return; 873 874 /* 875 * Walk through the list of plugged BIOs to check if they can be merged 876 * into the back of the request. 877 */ 878 spin_lock_irqsave(&zwplug->lock, flags); 879 while (!disk_zone_wplug_is_full(disk, zwplug)) { 880 bio = bio_list_peek(&zwplug->bio_list); 881 if (!bio) 882 break; 883 884 if (bio->bi_iter.bi_sector != req_back_sector || 885 !blk_rq_merge_ok(req, bio)) 886 break; 887 888 WARN_ON_ONCE(bio_op(bio) != REQ_OP_WRITE_ZEROES && 889 !bio->__bi_nr_segments); 890 891 bio_list_pop(&zwplug->bio_list); 892 if (bio_attempt_back_merge(req, bio, bio->__bi_nr_segments) != 893 BIO_MERGE_OK) { 894 bio_list_add_head(&zwplug->bio_list, bio); 895 break; 896 } 897 898 /* 899 * Drop the extra reference on the queue usage we got when 900 * plugging the BIO and advance the write pointer offset. 901 */ 902 blk_queue_exit(q); 903 zwplug->wp_offset += bio_sectors(bio); 904 905 req_back_sector += bio_sectors(bio); 906 } 907 spin_unlock_irqrestore(&zwplug->lock, flags); 908 } 909 910 /* 911 * Check and prepare a BIO for submission by incrementing the write pointer 912 * offset of its zone write plug and changing zone append operations into 913 * regular write when zone append emulation is needed. 914 */ 915 static bool blk_zone_wplug_prepare_bio(struct blk_zone_wplug *zwplug, 916 struct bio *bio) 917 { 918 struct gendisk *disk = bio->bi_bdev->bd_disk; 919 920 /* 921 * If we lost track of the zone write pointer due to a write error, 922 * the user must either execute a report zones, reset the zone or finish 923 * the to recover a reliable write pointer position. Fail BIOs if the 924 * user did not do that as we cannot handle emulated zone append 925 * otherwise. 926 */ 927 if (zwplug->flags & BLK_ZONE_WPLUG_NEED_WP_UPDATE) 928 return false; 929 930 /* 931 * Check that the user is not attempting to write to a full zone. 932 * We know such BIO will fail, and that would potentially overflow our 933 * write pointer offset beyond the end of the zone. 934 */ 935 if (disk_zone_wplug_is_full(disk, zwplug)) 936 return false; 937 938 if (bio_op(bio) == REQ_OP_ZONE_APPEND) { 939 /* 940 * Use a regular write starting at the current write pointer. 941 * Similarly to native zone append operations, do not allow 942 * merging. 943 */ 944 bio->bi_opf &= ~REQ_OP_MASK; 945 bio->bi_opf |= REQ_OP_WRITE | REQ_NOMERGE; 946 bio->bi_iter.bi_sector += zwplug->wp_offset; 947 948 /* 949 * Remember that this BIO is in fact a zone append operation 950 * so that we can restore its operation code on completion. 951 */ 952 bio_set_flag(bio, BIO_EMULATES_ZONE_APPEND); 953 } else { 954 /* 955 * Check for non-sequential writes early as we know that BIOs 956 * with a start sector not unaligned to the zone write pointer 957 * will fail. 958 */ 959 if (bio_offset_from_zone_start(bio) != zwplug->wp_offset) 960 return false; 961 } 962 963 /* Advance the zone write pointer offset. */ 964 zwplug->wp_offset += bio_sectors(bio); 965 966 return true; 967 } 968 969 static bool blk_zone_wplug_handle_write(struct bio *bio, unsigned int nr_segs) 970 { 971 struct gendisk *disk = bio->bi_bdev->bd_disk; 972 sector_t sector = bio->bi_iter.bi_sector; 973 struct blk_zone_wplug *zwplug; 974 gfp_t gfp_mask = GFP_NOIO; 975 unsigned long flags; 976 977 /* 978 * BIOs must be fully contained within a zone so that we use the correct 979 * zone write plug for the entire BIO. For blk-mq devices, the block 980 * layer should already have done any splitting required to ensure this 981 * and this BIO should thus not be straddling zone boundaries. For 982 * BIO-based devices, it is the responsibility of the driver to split 983 * the bio before submitting it. 984 */ 985 if (WARN_ON_ONCE(bio_straddles_zones(bio))) { 986 bio_io_error(bio); 987 return true; 988 } 989 990 /* Conventional zones do not need write plugging. */ 991 if (!bdev_zone_is_seq(bio->bi_bdev, sector)) { 992 /* Zone append to conventional zones is not allowed. */ 993 if (bio_op(bio) == REQ_OP_ZONE_APPEND) { 994 bio_io_error(bio); 995 return true; 996 } 997 return false; 998 } 999 1000 if (bio->bi_opf & REQ_NOWAIT) 1001 gfp_mask = GFP_NOWAIT; 1002 1003 zwplug = disk_get_and_lock_zone_wplug(disk, sector, gfp_mask, &flags); 1004 if (!zwplug) { 1005 if (bio->bi_opf & REQ_NOWAIT) 1006 bio_wouldblock_error(bio); 1007 else 1008 bio_io_error(bio); 1009 return true; 1010 } 1011 1012 /* Indicate that this BIO is being handled using zone write plugging. */ 1013 bio_set_flag(bio, BIO_ZONE_WRITE_PLUGGING); 1014 1015 /* 1016 * If the zone is already plugged, add the BIO to the plug BIO list. 1017 * Do the same for REQ_NOWAIT BIOs to ensure that we will not see a 1018 * BLK_STS_AGAIN failure if we let the BIO execute. 1019 * Otherwise, plug and let the BIO execute. 1020 */ 1021 if ((zwplug->flags & BLK_ZONE_WPLUG_PLUGGED) || 1022 (bio->bi_opf & REQ_NOWAIT)) 1023 goto plug; 1024 1025 if (!blk_zone_wplug_prepare_bio(zwplug, bio)) { 1026 spin_unlock_irqrestore(&zwplug->lock, flags); 1027 bio_io_error(bio); 1028 return true; 1029 } 1030 1031 zwplug->flags |= BLK_ZONE_WPLUG_PLUGGED; 1032 1033 spin_unlock_irqrestore(&zwplug->lock, flags); 1034 1035 return false; 1036 1037 plug: 1038 disk_zone_wplug_add_bio(disk, zwplug, bio, nr_segs); 1039 1040 spin_unlock_irqrestore(&zwplug->lock, flags); 1041 1042 return true; 1043 } 1044 1045 /** 1046 * blk_zone_plug_bio - Handle a zone write BIO with zone write plugging 1047 * @bio: The BIO being submitted 1048 * @nr_segs: The number of physical segments of @bio 1049 * 1050 * Handle write, write zeroes and zone append operations requiring emulation 1051 * using zone write plugging. 1052 * 1053 * Return true whenever @bio execution needs to be delayed through the zone 1054 * write plug. Otherwise, return false to let the submission path process 1055 * @bio normally. 1056 */ 1057 bool blk_zone_plug_bio(struct bio *bio, unsigned int nr_segs) 1058 { 1059 struct block_device *bdev = bio->bi_bdev; 1060 1061 if (!bdev->bd_disk->zone_wplugs_hash) 1062 return false; 1063 1064 /* 1065 * If the BIO already has the plugging flag set, then it was already 1066 * handled through this path and this is a submission from the zone 1067 * plug bio submit work. 1068 */ 1069 if (bio_flagged(bio, BIO_ZONE_WRITE_PLUGGING)) 1070 return false; 1071 1072 /* 1073 * We do not need to do anything special for empty flush BIOs, e.g 1074 * BIOs such as issued by blkdev_issue_flush(). The is because it is 1075 * the responsibility of the user to first wait for the completion of 1076 * write operations for flush to have any effect on the persistence of 1077 * the written data. 1078 */ 1079 if (op_is_flush(bio->bi_opf) && !bio_sectors(bio)) 1080 return false; 1081 1082 /* 1083 * Regular writes and write zeroes need to be handled through the target 1084 * zone write plug. This includes writes with REQ_FUA | REQ_PREFLUSH 1085 * which may need to go through the flush machinery depending on the 1086 * target device capabilities. Plugging such writes is fine as the flush 1087 * machinery operates at the request level, below the plug, and 1088 * completion of the flush sequence will go through the regular BIO 1089 * completion, which will handle zone write plugging. 1090 * Zone append operations for devices that requested emulation must 1091 * also be plugged so that these BIOs can be changed into regular 1092 * write BIOs. 1093 * Zone reset, reset all and finish commands need special treatment 1094 * to correctly track the write pointer offset of zones. These commands 1095 * are not plugged as we do not need serialization with write 1096 * operations. It is the responsibility of the user to not issue reset 1097 * and finish commands when write operations are in flight. 1098 */ 1099 switch (bio_op(bio)) { 1100 case REQ_OP_ZONE_APPEND: 1101 if (!bdev_emulates_zone_append(bdev)) 1102 return false; 1103 fallthrough; 1104 case REQ_OP_WRITE: 1105 case REQ_OP_WRITE_ZEROES: 1106 return blk_zone_wplug_handle_write(bio, nr_segs); 1107 case REQ_OP_ZONE_RESET: 1108 return blk_zone_wplug_handle_reset_or_finish(bio, 0); 1109 case REQ_OP_ZONE_FINISH: 1110 return blk_zone_wplug_handle_reset_or_finish(bio, 1111 bdev_zone_sectors(bdev)); 1112 case REQ_OP_ZONE_RESET_ALL: 1113 return blk_zone_wplug_handle_reset_all(bio); 1114 default: 1115 return false; 1116 } 1117 1118 return false; 1119 } 1120 EXPORT_SYMBOL_GPL(blk_zone_plug_bio); 1121 1122 static void disk_zone_wplug_unplug_bio(struct gendisk *disk, 1123 struct blk_zone_wplug *zwplug) 1124 { 1125 unsigned long flags; 1126 1127 spin_lock_irqsave(&zwplug->lock, flags); 1128 1129 /* Schedule submission of the next plugged BIO if we have one. */ 1130 if (!bio_list_empty(&zwplug->bio_list)) { 1131 disk_zone_wplug_schedule_bio_work(disk, zwplug); 1132 spin_unlock_irqrestore(&zwplug->lock, flags); 1133 return; 1134 } 1135 1136 zwplug->flags &= ~BLK_ZONE_WPLUG_PLUGGED; 1137 1138 /* 1139 * If the zone is full (it was fully written or finished, or empty 1140 * (it was reset), remove its zone write plug from the hash table. 1141 */ 1142 if (disk_should_remove_zone_wplug(disk, zwplug)) 1143 disk_remove_zone_wplug(disk, zwplug); 1144 1145 spin_unlock_irqrestore(&zwplug->lock, flags); 1146 } 1147 1148 void blk_zone_write_plug_bio_endio(struct bio *bio) 1149 { 1150 struct gendisk *disk = bio->bi_bdev->bd_disk; 1151 struct blk_zone_wplug *zwplug = 1152 disk_get_zone_wplug(disk, bio->bi_iter.bi_sector); 1153 unsigned long flags; 1154 1155 if (WARN_ON_ONCE(!zwplug)) 1156 return; 1157 1158 /* Make sure we do not see this BIO again by clearing the plug flag. */ 1159 bio_clear_flag(bio, BIO_ZONE_WRITE_PLUGGING); 1160 1161 /* 1162 * If this is a regular write emulating a zone append operation, 1163 * restore the original operation code. 1164 */ 1165 if (bio_flagged(bio, BIO_EMULATES_ZONE_APPEND)) { 1166 bio->bi_opf &= ~REQ_OP_MASK; 1167 bio->bi_opf |= REQ_OP_ZONE_APPEND; 1168 } 1169 1170 /* 1171 * If the BIO failed, abort all plugged BIOs and mark the plug as 1172 * needing a write pointer update. 1173 */ 1174 if (bio->bi_status != BLK_STS_OK) { 1175 spin_lock_irqsave(&zwplug->lock, flags); 1176 disk_zone_wplug_abort(zwplug); 1177 zwplug->flags |= BLK_ZONE_WPLUG_NEED_WP_UPDATE; 1178 spin_unlock_irqrestore(&zwplug->lock, flags); 1179 } 1180 1181 /* Drop the reference we took when the BIO was issued. */ 1182 disk_put_zone_wplug(zwplug); 1183 1184 /* 1185 * For BIO-based devices, blk_zone_write_plug_finish_request() 1186 * is not called. So we need to schedule execution of the next 1187 * plugged BIO here. 1188 */ 1189 if (bdev_test_flag(bio->bi_bdev, BD_HAS_SUBMIT_BIO)) 1190 disk_zone_wplug_unplug_bio(disk, zwplug); 1191 1192 /* Drop the reference we took when entering this function. */ 1193 disk_put_zone_wplug(zwplug); 1194 } 1195 1196 void blk_zone_write_plug_finish_request(struct request *req) 1197 { 1198 struct gendisk *disk = req->q->disk; 1199 struct blk_zone_wplug *zwplug; 1200 1201 zwplug = disk_get_zone_wplug(disk, req->__sector); 1202 if (WARN_ON_ONCE(!zwplug)) 1203 return; 1204 1205 req->rq_flags &= ~RQF_ZONE_WRITE_PLUGGING; 1206 1207 /* 1208 * Drop the reference we took when the request was initialized in 1209 * blk_zone_write_plug_init_request(). 1210 */ 1211 disk_put_zone_wplug(zwplug); 1212 1213 disk_zone_wplug_unplug_bio(disk, zwplug); 1214 1215 /* Drop the reference we took when entering this function. */ 1216 disk_put_zone_wplug(zwplug); 1217 } 1218 1219 static void blk_zone_wplug_bio_work(struct work_struct *work) 1220 { 1221 struct blk_zone_wplug *zwplug = 1222 container_of(work, struct blk_zone_wplug, bio_work); 1223 struct block_device *bdev; 1224 unsigned long flags; 1225 struct bio *bio; 1226 1227 /* 1228 * Submit the next plugged BIO. If we do not have any, clear 1229 * the plugged flag. 1230 */ 1231 spin_lock_irqsave(&zwplug->lock, flags); 1232 1233 again: 1234 bio = bio_list_pop(&zwplug->bio_list); 1235 if (!bio) { 1236 zwplug->flags &= ~BLK_ZONE_WPLUG_PLUGGED; 1237 spin_unlock_irqrestore(&zwplug->lock, flags); 1238 goto put_zwplug; 1239 } 1240 1241 if (!blk_zone_wplug_prepare_bio(zwplug, bio)) { 1242 blk_zone_wplug_bio_io_error(zwplug, bio); 1243 goto again; 1244 } 1245 1246 spin_unlock_irqrestore(&zwplug->lock, flags); 1247 1248 bdev = bio->bi_bdev; 1249 submit_bio_noacct_nocheck(bio); 1250 1251 /* 1252 * blk-mq devices will reuse the extra reference on the request queue 1253 * usage counter we took when the BIO was plugged, but the submission 1254 * path for BIO-based devices will not do that. So drop this extra 1255 * reference here. 1256 */ 1257 if (bdev_test_flag(bdev, BD_HAS_SUBMIT_BIO)) 1258 blk_queue_exit(bdev->bd_disk->queue); 1259 1260 put_zwplug: 1261 /* Drop the reference we took in disk_zone_wplug_schedule_bio_work(). */ 1262 disk_put_zone_wplug(zwplug); 1263 } 1264 1265 static inline unsigned int disk_zone_wplugs_hash_size(struct gendisk *disk) 1266 { 1267 return 1U << disk->zone_wplugs_hash_bits; 1268 } 1269 1270 void disk_init_zone_resources(struct gendisk *disk) 1271 { 1272 spin_lock_init(&disk->zone_wplugs_lock); 1273 } 1274 1275 /* 1276 * For the size of a disk zone write plug hash table, use the size of the 1277 * zone write plug mempool, which is the maximum of the disk open zones and 1278 * active zones limits. But do not exceed 4KB (512 hlist head entries), that is, 1279 * 9 bits. For a disk that has no limits, mempool size defaults to 128. 1280 */ 1281 #define BLK_ZONE_WPLUG_MAX_HASH_BITS 9 1282 #define BLK_ZONE_WPLUG_DEFAULT_POOL_SIZE 128 1283 1284 static int disk_alloc_zone_resources(struct gendisk *disk, 1285 unsigned int pool_size) 1286 { 1287 unsigned int i; 1288 1289 disk->zone_wplugs_hash_bits = 1290 min(ilog2(pool_size) + 1, BLK_ZONE_WPLUG_MAX_HASH_BITS); 1291 1292 disk->zone_wplugs_hash = 1293 kcalloc(disk_zone_wplugs_hash_size(disk), 1294 sizeof(struct hlist_head), GFP_KERNEL); 1295 if (!disk->zone_wplugs_hash) 1296 return -ENOMEM; 1297 1298 for (i = 0; i < disk_zone_wplugs_hash_size(disk); i++) 1299 INIT_HLIST_HEAD(&disk->zone_wplugs_hash[i]); 1300 1301 disk->zone_wplugs_pool = mempool_create_kmalloc_pool(pool_size, 1302 sizeof(struct blk_zone_wplug)); 1303 if (!disk->zone_wplugs_pool) 1304 goto free_hash; 1305 1306 disk->zone_wplugs_wq = 1307 alloc_workqueue("%s_zwplugs", WQ_MEM_RECLAIM | WQ_HIGHPRI, 1308 pool_size, disk->disk_name); 1309 if (!disk->zone_wplugs_wq) 1310 goto destroy_pool; 1311 1312 return 0; 1313 1314 destroy_pool: 1315 mempool_destroy(disk->zone_wplugs_pool); 1316 disk->zone_wplugs_pool = NULL; 1317 free_hash: 1318 kfree(disk->zone_wplugs_hash); 1319 disk->zone_wplugs_hash = NULL; 1320 disk->zone_wplugs_hash_bits = 0; 1321 return -ENOMEM; 1322 } 1323 1324 static void disk_destroy_zone_wplugs_hash_table(struct gendisk *disk) 1325 { 1326 struct blk_zone_wplug *zwplug; 1327 unsigned int i; 1328 1329 if (!disk->zone_wplugs_hash) 1330 return; 1331 1332 /* Free all the zone write plugs we have. */ 1333 for (i = 0; i < disk_zone_wplugs_hash_size(disk); i++) { 1334 while (!hlist_empty(&disk->zone_wplugs_hash[i])) { 1335 zwplug = hlist_entry(disk->zone_wplugs_hash[i].first, 1336 struct blk_zone_wplug, node); 1337 refcount_inc(&zwplug->ref); 1338 disk_remove_zone_wplug(disk, zwplug); 1339 disk_put_zone_wplug(zwplug); 1340 } 1341 } 1342 1343 kfree(disk->zone_wplugs_hash); 1344 disk->zone_wplugs_hash = NULL; 1345 disk->zone_wplugs_hash_bits = 0; 1346 } 1347 1348 static unsigned int disk_set_conv_zones_bitmap(struct gendisk *disk, 1349 unsigned long *bitmap) 1350 { 1351 unsigned int nr_conv_zones = 0; 1352 unsigned long flags; 1353 1354 spin_lock_irqsave(&disk->zone_wplugs_lock, flags); 1355 if (bitmap) 1356 nr_conv_zones = bitmap_weight(bitmap, disk->nr_zones); 1357 bitmap = rcu_replace_pointer(disk->conv_zones_bitmap, bitmap, 1358 lockdep_is_held(&disk->zone_wplugs_lock)); 1359 spin_unlock_irqrestore(&disk->zone_wplugs_lock, flags); 1360 1361 kfree_rcu_mightsleep(bitmap); 1362 1363 return nr_conv_zones; 1364 } 1365 1366 void disk_free_zone_resources(struct gendisk *disk) 1367 { 1368 if (!disk->zone_wplugs_pool) 1369 return; 1370 1371 if (disk->zone_wplugs_wq) { 1372 destroy_workqueue(disk->zone_wplugs_wq); 1373 disk->zone_wplugs_wq = NULL; 1374 } 1375 1376 disk_destroy_zone_wplugs_hash_table(disk); 1377 1378 /* 1379 * Wait for the zone write plugs to be RCU-freed before 1380 * destorying the mempool. 1381 */ 1382 rcu_barrier(); 1383 1384 mempool_destroy(disk->zone_wplugs_pool); 1385 disk->zone_wplugs_pool = NULL; 1386 1387 disk_set_conv_zones_bitmap(disk, NULL); 1388 disk->zone_capacity = 0; 1389 disk->last_zone_capacity = 0; 1390 disk->nr_zones = 0; 1391 } 1392 1393 static inline bool disk_need_zone_resources(struct gendisk *disk) 1394 { 1395 /* 1396 * All mq zoned devices need zone resources so that the block layer 1397 * can automatically handle write BIO plugging. BIO-based device drivers 1398 * (e.g. DM devices) are normally responsible for handling zone write 1399 * ordering and do not need zone resources, unless the driver requires 1400 * zone append emulation. 1401 */ 1402 return queue_is_mq(disk->queue) || 1403 queue_emulates_zone_append(disk->queue); 1404 } 1405 1406 static int disk_revalidate_zone_resources(struct gendisk *disk, 1407 unsigned int nr_zones) 1408 { 1409 struct queue_limits *lim = &disk->queue->limits; 1410 unsigned int pool_size; 1411 1412 if (!disk_need_zone_resources(disk)) 1413 return 0; 1414 1415 /* 1416 * If the device has no limit on the maximum number of open and active 1417 * zones, use BLK_ZONE_WPLUG_DEFAULT_POOL_SIZE. 1418 */ 1419 pool_size = max(lim->max_open_zones, lim->max_active_zones); 1420 if (!pool_size) 1421 pool_size = min(BLK_ZONE_WPLUG_DEFAULT_POOL_SIZE, nr_zones); 1422 1423 if (!disk->zone_wplugs_hash) 1424 return disk_alloc_zone_resources(disk, pool_size); 1425 1426 return 0; 1427 } 1428 1429 struct blk_revalidate_zone_args { 1430 struct gendisk *disk; 1431 unsigned long *conv_zones_bitmap; 1432 unsigned int nr_zones; 1433 unsigned int zone_capacity; 1434 unsigned int last_zone_capacity; 1435 sector_t sector; 1436 }; 1437 1438 /* 1439 * Update the disk zone resources information and device queue limits. 1440 * The disk queue is frozen when this is executed. 1441 */ 1442 static int disk_update_zone_resources(struct gendisk *disk, 1443 struct blk_revalidate_zone_args *args) 1444 { 1445 struct request_queue *q = disk->queue; 1446 unsigned int nr_seq_zones, nr_conv_zones; 1447 unsigned int pool_size; 1448 struct queue_limits lim; 1449 int ret; 1450 1451 disk->nr_zones = args->nr_zones; 1452 disk->zone_capacity = args->zone_capacity; 1453 disk->last_zone_capacity = args->last_zone_capacity; 1454 nr_conv_zones = 1455 disk_set_conv_zones_bitmap(disk, args->conv_zones_bitmap); 1456 if (nr_conv_zones >= disk->nr_zones) { 1457 pr_warn("%s: Invalid number of conventional zones %u / %u\n", 1458 disk->disk_name, nr_conv_zones, disk->nr_zones); 1459 return -ENODEV; 1460 } 1461 1462 lim = queue_limits_start_update(q); 1463 1464 /* 1465 * Some devices can advertize zone resource limits that are larger than 1466 * the number of sequential zones of the zoned block device, e.g. a 1467 * small ZNS namespace. For such case, assume that the zoned device has 1468 * no zone resource limits. 1469 */ 1470 nr_seq_zones = disk->nr_zones - nr_conv_zones; 1471 if (lim.max_open_zones >= nr_seq_zones) 1472 lim.max_open_zones = 0; 1473 if (lim.max_active_zones >= nr_seq_zones) 1474 lim.max_active_zones = 0; 1475 1476 if (!disk->zone_wplugs_pool) 1477 goto commit; 1478 1479 /* 1480 * If the device has no limit on the maximum number of open and active 1481 * zones, set its max open zone limit to the mempool size to indicate 1482 * to the user that there is a potential performance impact due to 1483 * dynamic zone write plug allocation when simultaneously writing to 1484 * more zones than the size of the mempool. 1485 */ 1486 pool_size = max(lim.max_open_zones, lim.max_active_zones); 1487 if (!pool_size) 1488 pool_size = min(BLK_ZONE_WPLUG_DEFAULT_POOL_SIZE, nr_seq_zones); 1489 1490 mempool_resize(disk->zone_wplugs_pool, pool_size); 1491 1492 if (!lim.max_open_zones && !lim.max_active_zones) { 1493 if (pool_size < nr_seq_zones) 1494 lim.max_open_zones = pool_size; 1495 else 1496 lim.max_open_zones = 0; 1497 } 1498 1499 commit: 1500 blk_mq_freeze_queue(q); 1501 ret = queue_limits_commit_update(q, &lim); 1502 blk_mq_unfreeze_queue(q); 1503 1504 return ret; 1505 } 1506 1507 static int blk_revalidate_conv_zone(struct blk_zone *zone, unsigned int idx, 1508 struct blk_revalidate_zone_args *args) 1509 { 1510 struct gendisk *disk = args->disk; 1511 1512 if (zone->capacity != zone->len) { 1513 pr_warn("%s: Invalid conventional zone capacity\n", 1514 disk->disk_name); 1515 return -ENODEV; 1516 } 1517 1518 if (disk_zone_is_last(disk, zone)) 1519 args->last_zone_capacity = zone->capacity; 1520 1521 if (!disk_need_zone_resources(disk)) 1522 return 0; 1523 1524 if (!args->conv_zones_bitmap) { 1525 args->conv_zones_bitmap = 1526 bitmap_zalloc(args->nr_zones, GFP_NOIO); 1527 if (!args->conv_zones_bitmap) 1528 return -ENOMEM; 1529 } 1530 1531 set_bit(idx, args->conv_zones_bitmap); 1532 1533 return 0; 1534 } 1535 1536 static int blk_revalidate_seq_zone(struct blk_zone *zone, unsigned int idx, 1537 struct blk_revalidate_zone_args *args) 1538 { 1539 struct gendisk *disk = args->disk; 1540 struct blk_zone_wplug *zwplug; 1541 unsigned int wp_offset; 1542 unsigned long flags; 1543 1544 /* 1545 * Remember the capacity of the first sequential zone and check 1546 * if it is constant for all zones, ignoring the last zone as it can be 1547 * smaller. 1548 */ 1549 if (!args->zone_capacity) 1550 args->zone_capacity = zone->capacity; 1551 if (disk_zone_is_last(disk, zone)) { 1552 args->last_zone_capacity = zone->capacity; 1553 } else if (zone->capacity != args->zone_capacity) { 1554 pr_warn("%s: Invalid variable zone capacity\n", 1555 disk->disk_name); 1556 return -ENODEV; 1557 } 1558 1559 /* 1560 * We need to track the write pointer of all zones that are not 1561 * empty nor full. So make sure we have a zone write plug for 1562 * such zone if the device has a zone write plug hash table. 1563 */ 1564 if (!disk->zone_wplugs_hash) 1565 return 0; 1566 1567 disk_zone_wplug_sync_wp_offset(disk, zone); 1568 1569 wp_offset = blk_zone_wp_offset(zone); 1570 if (!wp_offset || wp_offset >= zone->capacity) 1571 return 0; 1572 1573 zwplug = disk_get_and_lock_zone_wplug(disk, zone->wp, GFP_NOIO, &flags); 1574 if (!zwplug) 1575 return -ENOMEM; 1576 spin_unlock_irqrestore(&zwplug->lock, flags); 1577 disk_put_zone_wplug(zwplug); 1578 1579 return 0; 1580 } 1581 1582 /* 1583 * Helper function to check the validity of zones of a zoned block device. 1584 */ 1585 static int blk_revalidate_zone_cb(struct blk_zone *zone, unsigned int idx, 1586 void *data) 1587 { 1588 struct blk_revalidate_zone_args *args = data; 1589 struct gendisk *disk = args->disk; 1590 sector_t zone_sectors = disk->queue->limits.chunk_sectors; 1591 int ret; 1592 1593 /* Check for bad zones and holes in the zone report */ 1594 if (zone->start != args->sector) { 1595 pr_warn("%s: Zone gap at sectors %llu..%llu\n", 1596 disk->disk_name, args->sector, zone->start); 1597 return -ENODEV; 1598 } 1599 1600 if (zone->start >= get_capacity(disk) || !zone->len) { 1601 pr_warn("%s: Invalid zone start %llu, length %llu\n", 1602 disk->disk_name, zone->start, zone->len); 1603 return -ENODEV; 1604 } 1605 1606 /* 1607 * All zones must have the same size, with the exception on an eventual 1608 * smaller last zone. 1609 */ 1610 if (!disk_zone_is_last(disk, zone)) { 1611 if (zone->len != zone_sectors) { 1612 pr_warn("%s: Invalid zoned device with non constant zone size\n", 1613 disk->disk_name); 1614 return -ENODEV; 1615 } 1616 } else if (zone->len > zone_sectors) { 1617 pr_warn("%s: Invalid zoned device with larger last zone size\n", 1618 disk->disk_name); 1619 return -ENODEV; 1620 } 1621 1622 if (!zone->capacity || zone->capacity > zone->len) { 1623 pr_warn("%s: Invalid zone capacity\n", 1624 disk->disk_name); 1625 return -ENODEV; 1626 } 1627 1628 /* Check zone type */ 1629 switch (zone->type) { 1630 case BLK_ZONE_TYPE_CONVENTIONAL: 1631 ret = blk_revalidate_conv_zone(zone, idx, args); 1632 break; 1633 case BLK_ZONE_TYPE_SEQWRITE_REQ: 1634 ret = blk_revalidate_seq_zone(zone, idx, args); 1635 break; 1636 case BLK_ZONE_TYPE_SEQWRITE_PREF: 1637 default: 1638 pr_warn("%s: Invalid zone type 0x%x at sectors %llu\n", 1639 disk->disk_name, (int)zone->type, zone->start); 1640 ret = -ENODEV; 1641 } 1642 1643 if (!ret) 1644 args->sector += zone->len; 1645 1646 return ret; 1647 } 1648 1649 /** 1650 * blk_revalidate_disk_zones - (re)allocate and initialize zone write plugs 1651 * @disk: Target disk 1652 * 1653 * Helper function for low-level device drivers to check, (re) allocate and 1654 * initialize resources used for managing zoned disks. This function should 1655 * normally be called by blk-mq based drivers when a zoned gendisk is probed 1656 * and when the zone configuration of the gendisk changes (e.g. after a format). 1657 * Before calling this function, the device driver must already have set the 1658 * device zone size (chunk_sector limit) and the max zone append limit. 1659 * BIO based drivers can also use this function as long as the device queue 1660 * can be safely frozen. 1661 */ 1662 int blk_revalidate_disk_zones(struct gendisk *disk) 1663 { 1664 struct request_queue *q = disk->queue; 1665 sector_t zone_sectors = q->limits.chunk_sectors; 1666 sector_t capacity = get_capacity(disk); 1667 struct blk_revalidate_zone_args args = { }; 1668 unsigned int noio_flag; 1669 int ret = -ENOMEM; 1670 1671 if (WARN_ON_ONCE(!blk_queue_is_zoned(q))) 1672 return -EIO; 1673 1674 if (!capacity) 1675 return -ENODEV; 1676 1677 /* 1678 * Checks that the device driver indicated a valid zone size and that 1679 * the max zone append limit is set. 1680 */ 1681 if (!zone_sectors || !is_power_of_2(zone_sectors)) { 1682 pr_warn("%s: Invalid non power of two zone size (%llu)\n", 1683 disk->disk_name, zone_sectors); 1684 return -ENODEV; 1685 } 1686 1687 /* 1688 * Ensure that all memory allocations in this context are done as if 1689 * GFP_NOIO was specified. 1690 */ 1691 args.disk = disk; 1692 args.nr_zones = (capacity + zone_sectors - 1) >> ilog2(zone_sectors); 1693 noio_flag = memalloc_noio_save(); 1694 ret = disk_revalidate_zone_resources(disk, args.nr_zones); 1695 if (ret) { 1696 memalloc_noio_restore(noio_flag); 1697 return ret; 1698 } 1699 1700 ret = disk->fops->report_zones(disk, 0, UINT_MAX, 1701 blk_revalidate_zone_cb, &args); 1702 if (!ret) { 1703 pr_warn("%s: No zones reported\n", disk->disk_name); 1704 ret = -ENODEV; 1705 } 1706 memalloc_noio_restore(noio_flag); 1707 1708 /* 1709 * If zones where reported, make sure that the entire disk capacity 1710 * has been checked. 1711 */ 1712 if (ret > 0 && args.sector != capacity) { 1713 pr_warn("%s: Missing zones from sector %llu\n", 1714 disk->disk_name, args.sector); 1715 ret = -ENODEV; 1716 } 1717 1718 /* 1719 * Set the new disk zone parameters only once the queue is frozen and 1720 * all I/Os are completed. 1721 */ 1722 if (ret > 0) 1723 ret = disk_update_zone_resources(disk, &args); 1724 else 1725 pr_warn("%s: failed to revalidate zones\n", disk->disk_name); 1726 if (ret) { 1727 blk_mq_freeze_queue(q); 1728 disk_free_zone_resources(disk); 1729 blk_mq_unfreeze_queue(q); 1730 } 1731 1732 return ret; 1733 } 1734 EXPORT_SYMBOL_GPL(blk_revalidate_disk_zones); 1735 1736 /** 1737 * blk_zone_issue_zeroout - zero-fill a block range in a zone 1738 * @bdev: blockdev to write 1739 * @sector: start sector 1740 * @nr_sects: number of sectors to write 1741 * @gfp_mask: memory allocation flags (for bio_alloc) 1742 * 1743 * Description: 1744 * Zero-fill a block range in a zone (@sector must be equal to the zone write 1745 * pointer), handling potential errors due to the (initially unknown) lack of 1746 * hardware offload (See blkdev_issue_zeroout()). 1747 */ 1748 int blk_zone_issue_zeroout(struct block_device *bdev, sector_t sector, 1749 sector_t nr_sects, gfp_t gfp_mask) 1750 { 1751 int ret; 1752 1753 if (WARN_ON_ONCE(!bdev_is_zoned(bdev))) 1754 return -EIO; 1755 1756 ret = blkdev_issue_zeroout(bdev, sector, nr_sects, gfp_mask, 1757 BLKDEV_ZERO_NOFALLBACK); 1758 if (ret != -EOPNOTSUPP) 1759 return ret; 1760 1761 /* 1762 * The failed call to blkdev_issue_zeroout() advanced the zone write 1763 * pointer. Undo this using a report zone to update the zone write 1764 * pointer to the correct current value. 1765 */ 1766 ret = disk_zone_sync_wp_offset(bdev->bd_disk, sector); 1767 if (ret != 1) 1768 return ret < 0 ? ret : -EIO; 1769 1770 /* 1771 * Retry without BLKDEV_ZERO_NOFALLBACK to force the fallback to a 1772 * regular write with zero-pages. 1773 */ 1774 return blkdev_issue_zeroout(bdev, sector, nr_sects, gfp_mask, 0); 1775 } 1776 EXPORT_SYMBOL_GPL(blk_zone_issue_zeroout); 1777 1778 #ifdef CONFIG_BLK_DEBUG_FS 1779 1780 int queue_zone_wplugs_show(void *data, struct seq_file *m) 1781 { 1782 struct request_queue *q = data; 1783 struct gendisk *disk = q->disk; 1784 struct blk_zone_wplug *zwplug; 1785 unsigned int zwp_wp_offset, zwp_flags; 1786 unsigned int zwp_zone_no, zwp_ref; 1787 unsigned int zwp_bio_list_size, i; 1788 unsigned long flags; 1789 1790 if (!disk->zone_wplugs_hash) 1791 return 0; 1792 1793 rcu_read_lock(); 1794 for (i = 0; i < disk_zone_wplugs_hash_size(disk); i++) { 1795 hlist_for_each_entry_rcu(zwplug, 1796 &disk->zone_wplugs_hash[i], node) { 1797 spin_lock_irqsave(&zwplug->lock, flags); 1798 zwp_zone_no = zwplug->zone_no; 1799 zwp_flags = zwplug->flags; 1800 zwp_ref = refcount_read(&zwplug->ref); 1801 zwp_wp_offset = zwplug->wp_offset; 1802 zwp_bio_list_size = bio_list_size(&zwplug->bio_list); 1803 spin_unlock_irqrestore(&zwplug->lock, flags); 1804 1805 seq_printf(m, "%u 0x%x %u %u %u\n", 1806 zwp_zone_no, zwp_flags, zwp_ref, 1807 zwp_wp_offset, zwp_bio_list_size); 1808 } 1809 } 1810 rcu_read_unlock(); 1811 1812 return 0; 1813 } 1814 1815 #endif 1816