Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Borislav Petkov <bp@alien8.de>
To: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
Cc: linux-edac@vger.kernel.org, git@amd.com,
	shubhrajyoti.datta@gmail.com, dan.carpenter@linaro.org,
	Michal Simek <michal.simek@amd.com>,
	Tony Luck <tony.luck@intel.com>,
	James Morse <james.morse@arm.com>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	Robert Richter <rric@kernel.org>,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] EDAC/versalnet: Refactor memory controller initialization and cleanup
Date: Thu, 23 Oct 2025 17:44:05 +0200	[thread overview]
Message-ID: <20251023154405.GRaPpNRQRyvfpZjfex@fat_crate.local> (raw)
In-Reply-To: <20251016052839.2650517-1-shubhrajyoti.datta@amd.com>

Thanks for doing this!

On Thu, Oct 16, 2025 at 10:58:39AM +0530, Shubhrajyoti Datta wrote:
> Simplify the initialization and cleanup flow for Versal Net DDRMC
> controllers in the EDAC driver. Key changes include:
> 
> * Introduced `init_single_versalnet()` for per-controller setup and
>   `init_versalnet()` for looping through NUM_CONTROLLERS.
> * Added proper rollback logic using `remove_single_versalnet()` when
>   partial initialization fails.
> * Improved readability and maintainability by reducing duplicated code and
>   consolidating error handling.

"Describe your changes in imperative mood, e.g. “make xyzzy do frotz” instead
of “[This patch] makes xyzzy do frotz” or “[I] changed xyzzy to do frotz”, as
if you are giving orders to the codebase to change its behaviour."

IOW:

"Introduce per-controller setup and teardown functions..."

and so on.

> Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
> ---
> 
>  drivers/edac/versalnet_edac.c | 158 +++++++++++++++++++---------------
>  1 file changed, 87 insertions(+), 71 deletions(-)
> 
> diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
> index 1ded4c3f0213..fc7e4c43b387 100644
> --- a/drivers/edac/versalnet_edac.c
> +++ b/drivers/edac/versalnet_edac.c
> @@ -758,92 +758,111 @@ static void versal_edac_release(struct device *dev)
>  	kfree(dev);
>  }
>  
> -static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
> +static void  remove_single_versalnet(struct mc_priv *priv, int i)

remove_mc(). You don't need to stick "versalnet" in static function names.

> +{
> +	struct mem_ctl_info *mci;
> +
> +	mci = priv->mci[i];
> +	device_unregister(mci->pdev);
> +	edac_mc_del_mc(mci->pdev);
> +	edac_mc_free(mci);
> +}
> +
> +static int init_single_versalnet(struct mc_priv *priv, struct platform_device *pdev, int i)

init_mc() is fine.

>  {
>  	u32 num_chans, rank, dwidth, config;
> -	struct edac_mc_layer layers[2];
>  	struct mem_ctl_info *mci;
> +	struct edac_mc_layer layers[2];
>  	struct device *dev;
>  	enum dev_type dt;
>  	char *name;
> -	int rc, i;
> +	int rc;
>  
> -	for (i = 0; i < NUM_CONTROLLERS; i++) {
> -		config = priv->adec[CONF + i * ADEC_NUM];
> -		num_chans = FIELD_GET(MC5_NUM_CHANS_MASK, config);
> -		rank = 1 << FIELD_GET(MC5_RANK_MASK, config);
> -		dwidth = FIELD_GET(MC5_BUS_WIDTH_MASK, config);
> -
> -		switch (dwidth) {
> -		case XDDR5_BUS_WIDTH_16:
> -			dt = DEV_X16;
> -			break;
> -		case XDDR5_BUS_WIDTH_32:
> -			dt = DEV_X32;
> -			break;
> -		case XDDR5_BUS_WIDTH_64:
> -			dt = DEV_X64;
> -			break;
> -		default:
> -			dt = DEV_UNKNOWN;
> -		}
> +	config = priv->adec[CONF + i * ADEC_NUM];
> +	num_chans = FIELD_GET(MC5_NUM_CHANS_MASK, config);
> +	rank = 1 << FIELD_GET(MC5_RANK_MASK, config);
> +	dwidth = FIELD_GET(MC5_BUS_WIDTH_MASK, config);
> +
> +	switch (dwidth) {
> +	case XDDR5_BUS_WIDTH_16:
> +		dt = DEV_X16;
> +		break;
> +	case XDDR5_BUS_WIDTH_32:
> +		dt = DEV_X32;
> +		break;
> +	case XDDR5_BUS_WIDTH_64:
> +		dt = DEV_X64;
> +		break;
> +	default:
> +		dt = DEV_UNKNOWN;
> +	}
>  
> -		if (dt == DEV_UNKNOWN)
> -			continue;
> +	if (dt == DEV_UNKNOWN)
> +		return 0;
>  
> -		/* Find the first enabled device and register that one. */
> -		layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
> -		layers[0].size = rank;
> -		layers[0].is_virt_csrow = true;
> -		layers[1].type = EDAC_MC_LAYER_CHANNEL;
> -		layers[1].size = num_chans;
> -		layers[1].is_virt_csrow = false;
> +	/* Find the first enabled device and register that one. */
> +	layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
> +	layers[0].size = rank;
> +	layers[0].is_virt_csrow = true;
> +	layers[1].type = EDAC_MC_LAYER_CHANNEL;
> +	layers[1].size = num_chans;
> +	layers[1].is_virt_csrow = false;
> +
> +	rc = -ENOMEM;
> +	mci = edac_mc_alloc(i, ARRAY_SIZE(layers), layers,
> +			    sizeof(struct mc_priv));
> +	if (!mci) {
> +		edac_printk(KERN_ERR, EDAC_MC, "Failed memory allocation for MC%d\n", i);
> +		return rc;
> +	}
> +	priv->mci[i] = mci;
> +	priv->dwidth = dt;
> +
> +	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
> +	if (!dev)
> +		return rc;

No, do not return here but jump to the end of the function which unwinds
everything that's been setup so far. In this case, it should do
edac_mc_del_mc() what edac_mc_alloc() allocated.

This makes the unwinding loop easier in the remove function, see below.

> +	dev->release = versal_edac_release;
> +	name = kmalloc(32, GFP_KERNEL);
> +	sprintf(name, "versal-net-ddrmc5-edac-%d", i);
> +	dev->init_name = name;
> +	rc = device_register(dev);
> +	if (rc)
> +		goto err_mc_free;
>  
> -		rc = -ENOMEM;
> -		mci = edac_mc_alloc(i, ARRAY_SIZE(layers), layers,
> -				    sizeof(struct mc_priv));
> -		if (!mci) {
> -			edac_printk(KERN_ERR, EDAC_MC, "Failed memory allocation for MC%d\n", i);
> -			goto err_alloc;
> -		}
> +	mci->pdev = dev;
>  
> -		priv->mci[i] = mci;
> -		priv->dwidth = dt;
> +	platform_set_drvdata(pdev, priv);
>  
> -		dev = kzalloc(sizeof(*dev), GFP_KERNEL);
> -		dev->release = versal_edac_release;
> -		name = kmalloc(32, GFP_KERNEL);
> -		sprintf(name, "versal-net-ddrmc5-edac-%d", i);
> -		dev->init_name = name;
> -		rc = device_register(dev);
> -		if (rc)
> -			goto err_alloc;
> +	mc_init(mci, dev);
> +	rc = edac_mc_add_mc(mci);
> +	if (rc) {
> +		edac_printk(KERN_ERR, EDAC_MC, "Failed to register MC%d with EDAC core\n", i);
> +		goto err_unreg;
> +	}
> +	return 0;
> +err_unreg:
> +	device_unregister(mci->pdev);
> +err_mc_free:
> +	edac_mc_free(mci);
> +	return rc;
> +}
>  
> -		mci->pdev = dev;
>  
> -		platform_set_drvdata(pdev, priv);
> +static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
> +{
> +	int rc, i;
>  
> -		mc_init(mci, dev);
> -		rc = edac_mc_add_mc(mci);
> -		if (rc) {
> -			edac_printk(KERN_ERR, EDAC_MC, "Failed to register MC%d with EDAC core\n", i);
> -			goto err_alloc;
> -		}
> +	for (i = 0; i < NUM_CONTROLLERS; i++) {
> +		rc = init_single_versalnet(priv, pdev, i);
> +		if (rc)
> +			goto err_rm_versalnet;

No need for that - just unwind here and drop the label.

>  	}
>  	return 0;
>  
> -err_alloc:
> -	while (i--) {
> -		mci = priv->mci[i];
> -		if (!mci)
> -			continue;
> -
> -		if (mci->pdev) {
> -			device_unregister(mci->pdev);
> -			edac_mc_del_mc(mci->pdev);
> -		}
> -
> -		edac_mc_free(mci);
> +err_rm_versalnet:
> +	while (i) {
> +		i--;

That should be:

	while (i--)

so that when you get to remove here, you remove only fully initialized memory
controllers.

> +		remove_single_versalnet(priv, i);
>  	}
>  
>  	return rc;

In any case, yes, do you see how it all becomes a lot simpler this way?

Thx.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette


      reply	other threads:[~2025-10-23 15:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-16  5:28 [PATCH] EDAC/versalnet: Refactor memory controller initialization and cleanup Shubhrajyoti Datta
2025-10-23 15:44 ` Borislav Petkov [this message]

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=20251023154405.GRaPpNRQRyvfpZjfex@fat_crate.local \
    --to=bp@alien8.de \
    --cc=dan.carpenter@linaro.org \
    --cc=git@amd.com \
    --cc=james.morse@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-edac@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=michal.simek@amd.com \
    --cc=rric@kernel.org \
    --cc=shubhrajyoti.datta@amd.com \
    --cc=shubhrajyoti.datta@gmail.com \
    --cc=tony.luck@intel.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