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