* [PATCH v9 0/3] scsi: sd: Cleanups and warning fixes in sd_revalidate_disk() @ 2025-08-24 18:02 Abinash Singh 2025-08-24 18:02 ` [PATCH v9 1/3] scsi: sd: Remove redundant printk after kmalloc failure Abinash Singh ` (2 more replies) 0 siblings, 3 replies; 7+ messages in thread From: Abinash Singh @ 2025-08-24 18:02 UTC (permalink / raw) To: bvanassche Cc: James.Bottomley, abinashsinghlalotra, dlemoal, linux-kernel, linux-scsi, martin.petersen Hi all, Sorry for making mess in previous replies. My gmail app caused that. This v9 series contains small cleanups and fixes in sd_revalidate_disk(). On Sat, Aug 23, 2025 at 1:17 AM Bart Van Assche wrote: > From Documentation/process/coding-style.rst: > These generic allocation functions all emit a stack dump on failure when > used without __GFP_NOWARN so there is no use in emitting an additional > failure message when NULL is returned. > > Has this example perhaps been followed? I think it is safe to remove > this sd_printk() statement. checkpatch.pl also emits the following warning for this code: WARNING: Possible unnecessary 'out of memory' message #52: FILE: drivers/scsi/sd.c:3716: + if (!lim) { + sd_printk(KERN_WARNING, sdkp, So I agree with Bart — this printk is redundant and should be removed. In v9, I have split this into a separate patch for clarity. Summary of changes in this series: Removed the redundant 'out of memory' printk after kmalloc() failure in already existing code. > buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL); > if (!buffer) { > sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory " Added Bart Van Assche’s Reviewed-by tag in: [3/3] scsi: sd: make sd_revalidate_disk() return void Changes since v8: - Split removal of the redundant printk into its own patch (1/3), instead of keeping it inside the warning fix. - Kept the build warning fix and return type change as separate patches. - Updated changelogs accordingly. Thanks Abinash Abinash Singh (3): scsi: sd: Remove redundant printk after kmalloc failure scsi: sd: Fix build warning in sd_revalidate_disk() scsi: sd: make sd_revalidate_disk() return void drivers/scsi/sd.c | 58 ++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 28 deletions(-) -- 2.43.0 ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v9 1/3] scsi: sd: Remove redundant printk after kmalloc failure 2025-08-24 18:02 [PATCH v9 0/3] scsi: sd: Cleanups and warning fixes in sd_revalidate_disk() Abinash Singh @ 2025-08-24 18:02 ` Abinash Singh 2025-08-25 1:56 ` Damien Le Moal 2025-08-24 18:02 ` [PATCH v9 2/3] scsi: sd: Fix build warning in sd_revalidate_disk() Abinash Singh 2025-08-24 18:02 ` [PATCH v9 3/3] scsi: sd: make sd_revalidate_disk() return void Abinash Singh 2 siblings, 1 reply; 7+ messages in thread From: Abinash Singh @ 2025-08-24 18:02 UTC (permalink / raw) To: bvanassche Cc: James.Bottomley, abinashsinghlalotra, dlemoal, linux-kernel, linux-scsi, martin.petersen The SCSI disk driver prints a warning when kmalloc() fails in sd_revalidate_disk(). This is redundant because the page allocator already reports failures unless __GFP_NOWARN is used. Keeping the extra message only adds noise to the kernel log. Remove the unnecessary sd_printk() call. Control flow is unchanged. Fixes: e73aec824703 ("[SCSI] sd: make printing use a common prefix") Signed-off-by: Abinash Singh <abinashsinghlalotra@gmail.com> --- drivers/scsi/sd.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index 5b8668accf8e..aa9d944e27c5 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -3712,11 +3712,8 @@ static int sd_revalidate_disk(struct gendisk *disk) goto out; buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL); - if (!buffer) { - sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory " - "allocation failure.\n"); + if (!buffer) goto out; - } sd_spinup_disk(sdkp); -- 2.43.0 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v9 1/3] scsi: sd: Remove redundant printk after kmalloc failure 2025-08-24 18:02 ` [PATCH v9 1/3] scsi: sd: Remove redundant printk after kmalloc failure Abinash Singh @ 2025-08-25 1:56 ` Damien Le Moal 0 siblings, 0 replies; 7+ messages in thread From: Damien Le Moal @ 2025-08-25 1:56 UTC (permalink / raw) To: Abinash Singh, bvanassche Cc: James.Bottomley, linux-kernel, linux-scsi, martin.petersen On 8/25/25 3:02 AM, Abinash Singh wrote: > The SCSI disk driver prints a warning when kmalloc() fails in > sd_revalidate_disk(). This is redundant because the page allocator > already reports failures unless __GFP_NOWARN is used. Keeping the > extra message only adds noise to the kernel log. > > Remove the unnecessary sd_printk() call. Control flow is unchanged. > > Fixes: e73aec824703 ("[SCSI] sd: make printing use a common prefix") I do not think this is necessary. Having the message is not a bug. > Signed-off-by: Abinash Singh <abinashsinghlalotra@gmail.com> As commented on patch 2, move this patch as patch 2 in the series as it does not need backporting. With that, feel free to add: Reviewed-by: Damien Le Moal <dlemoal@kernel.org> > --- > drivers/scsi/sd.c | 5 +---- > 1 file changed, 1 insertion(+), 4 deletions(-) > > diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c > index 5b8668accf8e..aa9d944e27c5 100644 > --- a/drivers/scsi/sd.c > +++ b/drivers/scsi/sd.c > @@ -3712,11 +3712,8 @@ static int sd_revalidate_disk(struct gendisk *disk) > goto out; > > buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL); > - if (!buffer) { > - sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory " > - "allocation failure.\n"); > + if (!buffer) > goto out; Nit: you can return 0 directly here. No need for the goto. > - } > > sd_spinup_disk(sdkp); > -- Damien Le Moal Western Digital Research ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v9 2/3] scsi: sd: Fix build warning in sd_revalidate_disk() 2025-08-24 18:02 [PATCH v9 0/3] scsi: sd: Cleanups and warning fixes in sd_revalidate_disk() Abinash Singh 2025-08-24 18:02 ` [PATCH v9 1/3] scsi: sd: Remove redundant printk after kmalloc failure Abinash Singh @ 2025-08-24 18:02 ` Abinash Singh 2025-08-25 1:53 ` Damien Le Moal 2025-08-24 18:02 ` [PATCH v9 3/3] scsi: sd: make sd_revalidate_disk() return void Abinash Singh 2 siblings, 1 reply; 7+ messages in thread From: Abinash Singh @ 2025-08-24 18:02 UTC (permalink / raw) To: bvanassche Cc: James.Bottomley, abinashsinghlalotra, dlemoal, linux-kernel, linux-scsi, martin.petersen 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. Signed-off-by: Abinash Singh <abinashsinghlalotra@gmail.com> Reviewed-by: Bart Van Assche <bvanassche@acm.org> --- drivers/scsi/sd.c | 50 ++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index aa9d944e27c5..35856685d7fa 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -3696,10 +3696,10 @@ static int 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; + int err = 0; SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_revalidate_disk\n")); @@ -3711,20 +3711,24 @@ static int sd_revalidate_disk(struct gendisk *disk) if (!scsi_device_online(sdp)) goto out; + lim = kmalloc(sizeof(*lim), GFP_KERNEL); + if (!lim) + goto out; + buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL); if (!buffer) goto out; 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 @@ -3738,17 +3742,17 @@ static int 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); @@ -3758,47 +3762,46 @@ static int 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) - return err; + goto out; /* * Query concurrent positioning ranges after @@ -3817,7 +3820,10 @@ static int sd_revalidate_disk(struct gendisk *disk) set_capacity_and_notify(disk, 0); out: - return 0; + kfree(buffer); + kfree(lim); + + return err; } /** -- 2.43.0 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v9 2/3] scsi: sd: Fix build warning in sd_revalidate_disk() 2025-08-24 18:02 ` [PATCH v9 2/3] scsi: sd: Fix build warning in sd_revalidate_disk() Abinash Singh @ 2025-08-25 1:53 ` Damien Le Moal 0 siblings, 0 replies; 7+ messages in thread From: Damien Le Moal @ 2025-08-25 1:53 UTC (permalink / raw) To: Abinash Singh, bvanassche Cc: James.Bottomley, linux-kernel, linux-scsi, martin.petersen On 8/25/25 3:02 AM, Abinash Singh wrote: > 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. > > Signed-off-by: Abinash Singh <abinashsinghlalotra@gmail.com> > Reviewed-by: Bart Van Assche <bvanassche@acm.org> Looks good, but please move this patch as patch 1 in the series to make sure it can be easily backported. And please add: Fixes: 804e498e0496 ("sd: convert to the atomic queue limits API") Cc: stable@vger.kernel.org before your signed-off. -- Damien Le Moal Western Digital Research ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v9 3/3] scsi: sd: make sd_revalidate_disk() return void 2025-08-24 18:02 [PATCH v9 0/3] scsi: sd: Cleanups and warning fixes in sd_revalidate_disk() Abinash Singh 2025-08-24 18:02 ` [PATCH v9 1/3] scsi: sd: Remove redundant printk after kmalloc failure Abinash Singh 2025-08-24 18:02 ` [PATCH v9 2/3] scsi: sd: Fix build warning in sd_revalidate_disk() Abinash Singh @ 2025-08-24 18:02 ` Abinash Singh 2025-08-25 1:58 ` Damien Le Moal 2 siblings, 1 reply; 7+ messages in thread From: Abinash Singh @ 2025-08-24 18:02 UTC (permalink / raw) To: bvanassche Cc: James.Bottomley, abinashsinghlalotra, dlemoal, linux-kernel, linux-scsi, martin.petersen 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. Signed-off-by: Abinash Singh <abinashsinghlalotra@gmail.com> Reviewed-by: Bart Van Assche <bvanassche@acm.org> --- drivers/scsi/sd.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index 35856685d7fa..b3926c43e700 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -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; @@ -3699,7 +3699,7 @@ static int sd_revalidate_disk(struct gendisk *disk) struct queue_limits *lim = NULL; unsigned char *buffer = NULL; unsigned int dev_max; - int err = 0; + int err; SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_revalidate_disk\n")); @@ -3709,11 +3709,11 @@ static int sd_revalidate_disk(struct gendisk *disk) * of the other niceties. */ if (!scsi_device_online(sdp)) - goto out; + return; lim = kmalloc(sizeof(*lim), GFP_KERNEL); if (!lim) - goto out; + return; buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL); if (!buffer) @@ -3823,7 +3823,6 @@ static int sd_revalidate_disk(struct gendisk *disk) kfree(buffer); kfree(lim); - return err; } /** -- 2.43.0 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v9 3/3] scsi: sd: make sd_revalidate_disk() return void 2025-08-24 18:02 ` [PATCH v9 3/3] scsi: sd: make sd_revalidate_disk() return void Abinash Singh @ 2025-08-25 1:58 ` Damien Le Moal 0 siblings, 0 replies; 7+ messages in thread From: Damien Le Moal @ 2025-08-25 1:58 UTC (permalink / raw) To: Abinash Singh, bvanassche Cc: James.Bottomley, linux-kernel, linux-scsi, martin.petersen On 8/25/25 3:02 AM, Abinash Singh wrote: > 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. > > Signed-off-by: Abinash Singh <abinashsinghlalotra@gmail.com> > Reviewed-by: Bart Van Assche <bvanassche@acm.org> > --- > drivers/scsi/sd.c | 11 +++++------ > 1 file changed, 5 insertions(+), 6 deletions(-) > > diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c > index 35856685d7fa..b3926c43e700 100644 > --- a/drivers/scsi/sd.c > +++ b/drivers/scsi/sd.c > @@ -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; > @@ -3699,7 +3699,7 @@ static int sd_revalidate_disk(struct gendisk *disk) > struct queue_limits *lim = NULL; > unsigned char *buffer = NULL; > unsigned int dev_max; > - int err = 0; > + int err; > > SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, > "sd_revalidate_disk\n")); > @@ -3709,11 +3709,11 @@ static int sd_revalidate_disk(struct gendisk *disk) > * of the other niceties. > */ > if (!scsi_device_online(sdp)) > - goto out; > + return; > > lim = kmalloc(sizeof(*lim), GFP_KERNEL); > if (!lim) > - goto out; > + return; > > buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL); > if (!buffer) OK. So you can ignore the nit I commented on patch 1. Looks good. Reviewed-by: Damien Le Moal <dlemoal@kernel.org> > @@ -3823,7 +3823,6 @@ static int sd_revalidate_disk(struct gendisk *disk) > kfree(buffer); > kfree(lim); > > - return err; > } > > /** -- Damien Le Moal Western Digital Research ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2025-08-25 2:01 UTC | newest] Thread overview: 7+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2025-08-24 18:02 [PATCH v9 0/3] scsi: sd: Cleanups and warning fixes in sd_revalidate_disk() Abinash Singh 2025-08-24 18:02 ` [PATCH v9 1/3] scsi: sd: Remove redundant printk after kmalloc failure Abinash Singh 2025-08-25 1:56 ` Damien Le Moal 2025-08-24 18:02 ` [PATCH v9 2/3] scsi: sd: Fix build warning in sd_revalidate_disk() Abinash Singh 2025-08-25 1:53 ` Damien Le Moal 2025-08-24 18:02 ` [PATCH v9 3/3] scsi: sd: make sd_revalidate_disk() return void Abinash Singh 2025-08-25 1:58 ` Damien Le Moal
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).