Skip to content

Commit 30c4fdc

Browse files
Damien Le Moalmartinkpetersen
authored andcommitted
scsi: sd_zbc: Prevent zone information memory leak
Make sure to always free a scsi disk zone information, even for regular disks. This ensures that there is no memory leak, even in the case of a zoned disk changing type to a regular disk (e.g. with a reformat using the FORMAT WITH PRESET command or other vendor proprietary command). To do this, rename sd_zbc_clear_zone_info() to sd_zbc_free_zone_info() and remove sd_zbc_release_disk(). A call to sd_zbc_free_zone_info() is added to sd_zbc_read_zones() for drives for which sd_is_zoned() returns false. Furthermore, sd_zbc_free_zone_info() code make s sure that the sdkp rev_mutex is never used while not being initialized by gating the cleanup code with a a check on the zone_wp_update_buf field as it is never NULL when rev_mutex has been initialized. Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Johannes Thumshirn <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Damien Le Moal <[email protected]> Signed-off-by: Martin K. Petersen <[email protected]>
1 parent 05fbde3 commit 30c4fdc

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

drivers/scsi/sd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3580,7 +3580,7 @@ static void scsi_disk_release(struct device *dev)
35803580
struct scsi_disk *sdkp = to_scsi_disk(dev);
35813581

35823582
ida_free(&sd_index_ida, sdkp->index);
3583-
sd_zbc_release_disk(sdkp);
3583+
sd_zbc_free_zone_info(sdkp);
35843584
put_device(&sdkp->device->sdev_gendev);
35853585
free_opal_dev(sdkp->opal_dev);
35863586

drivers/scsi/sd.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ static inline int sd_is_zoned(struct scsi_disk *sdkp)
241241

242242
#ifdef CONFIG_BLK_DEV_ZONED
243243

244-
void sd_zbc_release_disk(struct scsi_disk *sdkp);
244+
void sd_zbc_free_zone_info(struct scsi_disk *sdkp);
245245
int sd_zbc_read_zones(struct scsi_disk *sdkp, u8 buf[SD_BUF_SIZE]);
246246
int sd_zbc_revalidate_zones(struct scsi_disk *sdkp);
247247
blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
@@ -256,7 +256,7 @@ blk_status_t sd_zbc_prepare_zone_append(struct scsi_cmnd *cmd, sector_t *lba,
256256

257257
#else /* CONFIG_BLK_DEV_ZONED */
258258

259-
static inline void sd_zbc_release_disk(struct scsi_disk *sdkp) {}
259+
static inline void sd_zbc_free_zone_info(struct scsi_disk *sdkp) {}
260260

261261
static inline int sd_zbc_read_zones(struct scsi_disk *sdkp, u8 buf[SD_BUF_SIZE])
262262
{

drivers/scsi/sd_zbc.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,11 @@ static int sd_zbc_init_disk(struct scsi_disk *sdkp)
786786
return 0;
787787
}
788788

789-
static void sd_zbc_clear_zone_info(struct scsi_disk *sdkp)
789+
void sd_zbc_free_zone_info(struct scsi_disk *sdkp)
790790
{
791+
if (!sdkp->zone_wp_update_buf)
792+
return;
793+
791794
/* Serialize against revalidate zones */
792795
mutex_lock(&sdkp->rev_mutex);
793796

@@ -802,12 +805,6 @@ static void sd_zbc_clear_zone_info(struct scsi_disk *sdkp)
802805
mutex_unlock(&sdkp->rev_mutex);
803806
}
804807

805-
void sd_zbc_release_disk(struct scsi_disk *sdkp)
806-
{
807-
if (sd_is_zoned(sdkp))
808-
sd_zbc_clear_zone_info(sdkp);
809-
}
810-
811808
static void sd_zbc_revalidate_zones_cb(struct gendisk *disk)
812809
{
813810
struct scsi_disk *sdkp = scsi_disk(disk);
@@ -914,12 +911,15 @@ int sd_zbc_read_zones(struct scsi_disk *sdkp, u8 buf[SD_BUF_SIZE])
914911
u32 zone_blocks = 0;
915912
int ret;
916913

917-
if (!sd_is_zoned(sdkp))
914+
if (!sd_is_zoned(sdkp)) {
918915
/*
919-
* Device managed or normal SCSI disk,
920-
* no special handling required
916+
* Device managed or normal SCSI disk, no special handling
917+
* required. Nevertheless, free the disk zone information in
918+
* case the device type changed.
921919
*/
920+
sd_zbc_free_zone_info(sdkp);
922921
return 0;
922+
}
923923

924924
/* READ16/WRITE16 is mandatory for ZBC disks */
925925
sdkp->device->use_16_for_rw = 1;
@@ -928,11 +928,11 @@ int sd_zbc_read_zones(struct scsi_disk *sdkp, u8 buf[SD_BUF_SIZE])
928928
if (!blk_queue_is_zoned(q)) {
929929
/*
930930
* This can happen for a host aware disk with partitions.
931-
* The block device zone information was already cleared
932-
* by blk_queue_set_zoned(). Only clear the scsi disk zone
931+
* The block device zone model was already cleared by
932+
* blk_queue_set_zoned(). Only free the scsi disk zone
933933
* information and exit early.
934934
*/
935-
sd_zbc_clear_zone_info(sdkp);
935+
sd_zbc_free_zone_info(sdkp);
936936
return 0;
937937
}
938938

0 commit comments

Comments
 (0)