Skip to contentSkip to conversation

scsi: sd: Fix build warning in sd_revalidate_disk()

PATCH Superseded Abinash Singh 2 participants 4 messages Aug 9, 2025 – Aug 11, 2025 SCSI

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 cover scsi: sd: Fix build warning in sd_revalidate_disk()

This v6 series follows up on v5 of the "scsi: sd: Fix build warning in sd_revalidate_disk()" patches.
In v5, the change was split into two
patches: the first patch refactors sd_revalidate_disk() to return void, and the second patch addresses excessive stack usage by allocating certain structures dynamically.

Apologies for the oversight in v5 — although I built and tested the kernel locally with the correct changes, I mistakenly sent an older version of patch 2/2 that still had size(*lim) instead of sizeof(*lim). This caused a build error reported by the kernel test robot when building with clang.

Changes in v6:
- Replace size(*lim) with sizeof(*lim) in patch 2/2

Now there are no build errors.
I am very sorry for that .

Abinash Singh (2):
scsi: sd: make sd_revalidate_disk() return void
scsi: sd: Fix build warning in sd_revalidate_disk()

 drivers/scsi/sd.c | 54 +++++++++++++++++++++++++++--------------------
 1 file changed, 31 insertions(+), 23 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH v6 1/2 scsi: sd: make sd_revalidate_disk() return void

The sd_revalidate_disk() function currently returns 0 for both success and memory allocation failure.Since none of its callers use the return value, this return code is both unnecessary and potentially misleading.

Change the return type of sd_revalidate_disk() from int to void and remove all return value handling. This makes the function semantics clearer and avoids confusion about unused return codes.

---
 drivers/scsi/sd.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/scsi/sd.c +4 -4
index 5b8668accf8e..9d5963de8c82 100644
@@ -106,7 +106,7 @@ static void sd_config_discard(struct scsi_disk *sdkp, struct queue_limits *lim,
 		unsigned int mode);
 static void sd_config_write_same(struct scsi_disk *sdkp,
 		struct queue_limits *lim);
-static int  sd_revalidate_disk(struct gendisk *);
+static void  sd_revalidate_disk(struct gendisk *);
 static void sd_unlock_native_capacity(struct gendisk *disk);
 static void sd_shutdown(struct device *);
 static void scsi_disk_release(struct device *cdev);
@@ -3691,7 +3691,7 @@ static void sd_read_block_zero(struct scsi_disk *sdkp)
  *	performs disk spin up, read_capacity, etc.
  *	@disk: struct gendisk we care about
  **/
-static int sd_revalidate_disk(struct gendisk *disk)
+static void sd_revalidate_disk(struct gendisk *disk)
 {
 	struct scsi_disk *sdkp = scsi_disk(disk);
 	struct scsi_device *sdp = sdkp->device;
@@ -3801,7 +3801,7 @@ static int sd_revalidate_disk(struct gendisk *disk)
 
 	err = queue_limits_commit_update_frozen(sdkp->disk->queue, &lim);
 	if (err)
-		return err;
+		goto out;
 
 	/*
 	 * Query concurrent positioning ranges after
@@ -3820,7 +3820,7 @@ static int sd_revalidate_disk(struct gendisk *disk)
 		set_capacity_and_notify(disk, 0);
 
  out:
-	return 0;
+	/* Placeholder for future cleanup */
 }
 
 /**
Signature · 1 line
2.50.1
#2 lore raw

PATCH v6 2/2 scsi: sd: Fix build warning in sd_revalidate_disk()

In reply to Abinash Singh

A build warning was triggered due to excessive stack usage in sd_revalidate_disk():

drivers/scsi/sd.c: In function ‘sd_revalidate_disk.isra’:
drivers/scsi/sd.c:3824:1: warning: the frame size of 1160 bytes is larger than 1024 bytes [-Wframe-larger-than=]

This is caused by a large local struct queue_limits (~400B) allocated on the stack. Replacing it with a heap allocation using kmalloc() significantly reduces frame usage. Kernel stack is limited (~8 KB), and allocating large structs on the stack is discouraged. As the function already performs heap allocations (e.g. for buffer), this change fits well.

---
 drivers/scsi/sd.c | 48 +++++++++++++++++++++++++++--------------------
 1 file changed, 28 insertions(+), 20 deletions(-)
drivers/scsi/sd.c +28 -20
index 9d5963de8c82..bc54884e910a 100644
@@ -3696,8 +3696,8 @@ static void sd_revalidate_disk(struct gendisk *disk)
 	struct scsi_disk *sdkp = scsi_disk(disk);
 	struct scsi_device *sdp = sdkp->device;
 	sector_t old_capacity = sdkp->capacity;
-	struct queue_limits lim;
-	unsigned char *buffer;
+	struct queue_limits *lim = NULL;
+	unsigned char *buffer = NULL;
 	unsigned int dev_max;
 	int err;
 
@@ -3711,6 +3711,13 @@ static void sd_revalidate_disk(struct gendisk *disk)
 	if (!scsi_device_online(sdp))
 		goto out;
 
+	lim = kmalloc(sizeof(*lim), GFP_KERNEL);
+	if (!lim) {
+		sd_printk(KERN_WARNING, sdkp,
+			"sd_revalidate_disk: Disk limit allocation failure.\n");
+		goto out;
+	}
+
 	buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
 	if (!buffer) {
 		sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
@@ -3720,14 +3727,14 @@ static void sd_revalidate_disk(struct gendisk *disk)
 
 	sd_spinup_disk(sdkp);
 
-	lim = queue_limits_start_update(sdkp->disk->queue);
+	*lim = queue_limits_start_update(sdkp->disk->queue);
 
 	/*
 	 * Without media there is no reason to ask; moreover, some devices
 	 * react badly if we do.
 	 */
 	if (sdkp->media_present) {
-		sd_read_capacity(sdkp, &lim, buffer);
+		sd_read_capacity(sdkp, lim, buffer);
 		/*
 		 * Some USB/UAS devices return generic values for mode pages
 		 * until the media has been accessed. Trigger a READ operation
@@ -3741,17 +3748,17 @@ static void sd_revalidate_disk(struct gendisk *disk)
 		 * cause this to be updated correctly and any device which
 		 * doesn't support it should be treated as rotational.
 		 */
-		lim.features |= (BLK_FEAT_ROTATIONAL | BLK_FEAT_ADD_RANDOM);
+		lim->features |= (BLK_FEAT_ROTATIONAL | BLK_FEAT_ADD_RANDOM);
 
 		if (scsi_device_supports_vpd(sdp)) {
 			sd_read_block_provisioning(sdkp);
-			sd_read_block_limits(sdkp, &lim);
+			sd_read_block_limits(sdkp, lim);
 			sd_read_block_limits_ext(sdkp);
-			sd_read_block_characteristics(sdkp, &lim);
-			sd_zbc_read_zones(sdkp, &lim, buffer);
+			sd_read_block_characteristics(sdkp, lim);
+			sd_zbc_read_zones(sdkp, lim, buffer);
 		}
 
-		sd_config_discard(sdkp, &lim, sd_discard_mode(sdkp));
+		sd_config_discard(sdkp, lim, sd_discard_mode(sdkp));
 
 		sd_print_capacity(sdkp, old_capacity);
 
@@ -3761,45 +3768,44 @@ static void sd_revalidate_disk(struct gendisk *disk)
 		sd_read_app_tag_own(sdkp, buffer);
 		sd_read_write_same(sdkp, buffer);
 		sd_read_security(sdkp, buffer);
-		sd_config_protection(sdkp, &lim);
+		sd_config_protection(sdkp, lim);
 	}
 
 	/*
 	 * We now have all cache related info, determine how we deal
 	 * with flush requests.
 	 */
-	sd_set_flush_flag(sdkp, &lim);
+	sd_set_flush_flag(sdkp, lim);
 
 	/* Initial block count limit based on CDB TRANSFER LENGTH field size. */
 	dev_max = sdp->use_16_for_rw ? SD_MAX_XFER_BLOCKS : SD_DEF_XFER_BLOCKS;
 
 	/* Some devices report a maximum block count for READ/WRITE requests. */
 	dev_max = min_not_zero(dev_max, sdkp->max_xfer_blocks);
-	lim.max_dev_sectors = logical_to_sectors(sdp, dev_max);
+	lim->max_dev_sectors = logical_to_sectors(sdp, dev_max);
 
 	if (sd_validate_min_xfer_size(sdkp))
-		lim.io_min = logical_to_bytes(sdp, sdkp->min_xfer_blocks);
+		lim->io_min = logical_to_bytes(sdp, sdkp->min_xfer_blocks);
 	else
-		lim.io_min = 0;
+		lim->io_min = 0;
 
 	/*
 	 * Limit default to SCSI host optimal sector limit if set. There may be
 	 * an impact on performance for when the size of a request exceeds this
 	 * host limit.
 	 */
-	lim.io_opt = sdp->host->opt_sectors << SECTOR_SHIFT;
+	lim->io_opt = sdp->host->opt_sectors << SECTOR_SHIFT;
 	if (sd_validate_opt_xfer_size(sdkp, dev_max)) {
-		lim.io_opt = min_not_zero(lim.io_opt,
+		lim->io_opt = min_not_zero(lim->io_opt,
 				logical_to_bytes(sdp, sdkp->opt_xfer_blocks));
 	}
 
 	sdkp->first_scan = 0;
 
 	set_capacity_and_notify(disk, logical_to_sectors(sdp, sdkp->capacity));
-	sd_config_write_same(sdkp, &lim);
-	kfree(buffer);
+	sd_config_write_same(sdkp, lim);
 
-	err = queue_limits_commit_update_frozen(sdkp->disk->queue, &lim);
+	err = queue_limits_commit_update_frozen(sdkp->disk->queue, lim);
 	if (err)
 		goto out;
 
@@ -3820,7 +3826,9 @@ static void sd_revalidate_disk(struct gendisk *disk)
 		set_capacity_and_notify(disk, 0);
 
  out:
-	/* Placeholder for future cleanup */
+	kfree(lim);
+	kfree(buffer);
+
 }
 
 /**
Signature · 1 line
2.50.1
#3 lore raw
In reply to Abinash Singh
Quoted 20 lines · Abinash Singh

This v6 series follows up on v5 of the "scsi: sd: Fix build warning in sd_revalidate_disk()" patches.
In v5, the change was split into two
patches: the first patch refactors sd_revalidate_disk() to return void, and the second patch addresses excessive stack usage by allocating certain structures dynamically.

Apologies for the oversight in v5 — although I built and tested the kernel locally with the correct changes, I mistakenly sent an older version of patch 2/2 that still had size(*lim) instead of sizeof(*lim). This caused a build error reported by the kernel test robot when building with clang.

Changes in v6:
- Replace size(*lim) with sizeof(*lim) in patch 2/2

Now there are no build errors.
I am very sorry for that .

Quoted 7 lines

Abinash Singh (2):
scsi: sd: make sd_revalidate_disk() return void
scsi: sd: Fix build warning in sd_revalidate_disk()

The order of the patches must be reversed because the fix build warning patch must be CC-ed to stable with a Fixes tag. The cleanup making sd_revalidate_disk() a void function must come after the fix. That will avoid the weird placeholder comment and also will allow a more extensive cleanup to replace several "goto out" with a simple return.

Quoted 4 lines
 drivers/scsi/sd.c | 54 +++++++++++++++++++++++++++--------------------
 1 file changed, 31 insertions(+), 23 deletions(-)
Signature · 2 lines
Damien Le Moal
Western Digital Research