mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Damien Le Moal <dlemoal@kernel.org>
To: Abinash Singh <abinashsinghlalotra@gmail.com>
Cc: James.Bottomley@HansenPartnership.com,
	linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org,
	martin.petersen@oracle.com
Subject: Re: [PATCH v2] scsi: sd: Fix build warning in sd_revalidate_disk()
Date: Fri, 8 Aug 2025 22:38:37 +0900	[thread overview]
Message-ID: <4786fbb9-5d25-4d86-b902-61cc78a9b138@kernel.org> (raw)
In-Reply-To: <20250808113019.20177-1-abinashsinghlalotra@gmail.com>

On 8/8/25 20:30, 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>
> ---
> 
> Hi,
> 
> Thank you very much for your comments.
> I have addressed all your suggestions from v1.
> 
> As you mentioned concerns regarding the readability of
> the __free(kfree) attribute, I have used the classic
> approach in v2. Additionally, I will also send v3
> where the __free() attribute is used instead.
> 
> We can proceed with patch version you
> find more suitable, and do let me know if you have
> any further feedback.
> 
> changelog v1->v2:
> 	moved declarations together
> 	avoided "unreadable" cleanup attribute
> 	splited long line
> 	changed the log message to diiferentiate with buffer allocation
> 
> Thanks,
> 
> ---
>  drivers/scsi/sd.c | 49 +++++++++++++++++++++++++++++------------------
>  1 file changed, 30 insertions(+), 19 deletions(-)
> 
> diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
> index 4a68b2ab2804..f5ab2a422df6 100644
> --- a/drivers/scsi/sd.c
> +++ b/drivers/scsi/sd.c
> @@ -3696,7 +3696,7 @@ 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;
> +	struct queue_limits *lim;
>  	unsigned char *buffer;
>  	unsigned int dev_max;
>  	int err;

If you change this to "int err = 0", then...

> @@ -3711,23 +3711,30 @@ static int 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 "
>  			  "allocation failure.\n");
> -		goto out;
> +		goto free_lim;

Yout can do a "goto out" here...

>  	}
>  

[...]

>  	set_capacity_and_notify(disk, logical_to_sectors(sdp, sdkp->capacity));
> -	sd_config_write_same(sdkp, &lim);
> +	sd_config_write_same(sdkp, lim);
>  	kfree(buffer);

Move this line after the "out" label...

>  
> -	err = queue_limits_commit_update_frozen(sdkp->disk->queue, &lim);
> -	if (err)
> +	err = queue_limits_commit_update_frozen(sdkp->disk->queue, lim);
> +	if (err) {

just do a goto out here...

> +		kfree(lim);
>  		return err;
> +	}
>  
>  	/*
>  	 * Query concurrent positioning ranges after
> @@ -3819,6 +3828,8 @@ static int sd_revalidate_disk(struct gendisk *disk)
>  	if (sd_zbc_revalidate_zones(sdkp))
>  		set_capacity_and_notify(disk, 0);
>  
> + free_lim:
> +	kfree(lim);
>   out:

And this becomes:

 out:
	kfree(lim);
	kfree(buffer)

	return err;

Much cleaner :)

>  	return 0;
>  }


-- 
Damien Le Moal
Western Digital Research

  parent reply	other threads:[~2025-08-08 13:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-07 18:30 [PATCH RFC] " Abinash Singh
2025-08-08  8:01 ` Damien Le Moal
2025-08-08 11:30   ` [PATCH v2] " Abinash Singh
2025-08-08 11:30     ` [PATCH v3] " Abinash Singh
2025-08-08 13:35       ` Damien Le Moal
2025-08-08 13:38     ` Damien Le Moal [this message]
2025-08-08 18:28       ` [PATCH v4] " Abinash Singh
2025-08-08 19:25         ` Bart Van Assche

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4786fbb9-5d25-4d86-b902-61cc78a9b138@kernel.org \
    --to=dlemoal@kernel.org \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=abinashsinghlalotra@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®