Skip to content

Commit 7679205

Browse files
Christoph Hellwigaxboe
authored andcommitted
block: add a ->free_disk method
Add a method to notify the driver that the gendisk is about to be freed. This allows drivers to tie the lifetime of their private data to that of the gendisk and thus deal with device removal races without expensive synchronization and boilerplate code. A new flag is added so that ->free_disk is only called after a successful call to add_disk, which significantly simplifies the error handling path during probing. Signed-off-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20220215094514.3828912-2-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 34841e6 commit 7679205

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

block/genhd.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
526526

527527
disk_update_readahead(disk);
528528
disk_add_events(disk);
529+
set_bit(GD_ADDED, &disk->state);
529530
return 0;
530531

531532
out_unregister_bdi:
@@ -1119,6 +1120,10 @@ static void disk_release(struct device *dev)
11191120
xa_destroy(&disk->part_tbl);
11201121
disk->queue->disk = NULL;
11211122
blk_put_queue(disk->queue);
1123+
1124+
if (test_bit(GD_ADDED, &disk->state) && disk->fops->free_disk)
1125+
disk->fops->free_disk(disk);
1126+
11221127
iput(disk->part0->bd_inode); /* frees the disk */
11231128
}
11241129

include/linux/blkdev.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ struct gendisk {
146146
#define GD_READ_ONLY 1
147147
#define GD_DEAD 2
148148
#define GD_NATIVE_CAPACITY 3
149+
#define GD_ADDED 4
149150

150151
struct mutex open_mutex; /* open/close mutex */
151152
unsigned open_partitions; /* number of open partitions */
@@ -1464,6 +1465,7 @@ struct block_device_operations {
14641465
void (*unlock_native_capacity) (struct gendisk *);
14651466
int (*getgeo)(struct block_device *, struct hd_geometry *);
14661467
int (*set_read_only)(struct block_device *bdev, bool ro);
1468+
void (*free_disk)(struct gendisk *disk);
14671469
/* this callback is with swap_lock and sometimes page table lock held */
14681470
void (*swap_slot_free_notify) (struct block_device *, unsigned long);
14691471
int (*report_zones)(struct gendisk *, sector_t sector,

0 commit comments

Comments
 (0)