Re: [PATCH 5/9] EDAC/versalnet: Use dev_set_name() instead of sprintf with init_name

"Pandey, Radhey Shyam" <[email protected]> Fri, 31 Jul 2026 16:52:32 +0530
Newsgroups org.kernel.vger.linux-edac,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
On 7/24/2026 10:49 PM, Shubhrajyoti Datta wrote:
> The previous code used sprintf() to format a device name into a local
> stack buffer and then assigned it to dev->init_name. Since kobject
> cleanup can be deferred asynchronously (e.g. when
> CONFIG_DEBUG_KOBJECT_RELEASE is enabled), dev_name(dev) could be
> accessed after init_one_mc() returns and the stack frame containing the
> name buffer is gone, resulting in a use-after-free.
> 
> This is fixed by switching to dev_set_name(), which dynamically
> allocates and manages the name string internally, but the now-unused
> local char name[MC_NAME_LEN] buffer and the MC_NAME_LEN macro are
> not needed so remove them.

If previous commit introduced regression then this has to be merged
to previous commit. Each patch should be correct on its own.
> 
> Signed-off-by: Shubhrajyoti Datta <[email protected]>
> ---
> 
>   drivers/edac/versalnet_edac.c | 9 ++++-----
>   1 file changed, 4 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
> index 3c9eaea5a106..1caaba653fc0 100644
> --- a/drivers/edac/versalnet_edac.c
> +++ b/drivers/edac/versalnet_edac.c
> @@ -70,8 +70,6 @@
>   #define XDDR5_BUS_WIDTH_32		1
>   #define XDDR5_BUS_WIDTH_16		2
>   
> -#define MC_NAME_LEN			32
> -
>   /**
>    * struct ecc_error_info - ECC error log information.
>    * @burstpos:		Burst position.
> @@ -782,7 +780,6 @@ static int init_one_mc(struct mc_priv *priv, int i)
>   	u32 num_chans, rank, dwidth, config;
>   	struct edac_mc_layer layers[2];
>   	struct mem_ctl_info *mci;
> -	char name[MC_NAME_LEN];
>   	struct device *dev;
>   	enum dev_type dt;
>   	int rc = -ENOMEM;
> @@ -821,9 +818,7 @@ static int init_one_mc(struct mc_priv *priv, int i)
>   	if (!dev)
>   		return rc;
>   
> -	sprintf(name, "versal-net-ddrmc5-edac-%d", i);
>   
> -	dev->init_name = name;
>   	dev->release = versal_edac_release;
>   	device_initialize(dev);
>   
> @@ -833,6 +828,10 @@ static int init_one_mc(struct mc_priv *priv, int i)
>   		goto err_put_dev;
>   	}
>   
> +	rc = dev_set_name(dev, "versal-net-ddrmc5-edac-%d", i);
> +	if (rc)
> +		goto err_mc_free;
> +
>   	rc = device_add(dev);
>   	if (rc)
>   		goto err_mc_free;