mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Borislav Petkov <bp@alien8.de>
To: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
Cc: linux-kernel@vger.kernel.org, linux-edac@vger.kernel.org,
	shubhrajyoti.datta@gmail.com, Tony Luck <tony.luck@intel.com>,
	James Morse <james.morse@arm.com>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	Robert Richter <rric@kernel.org>
Subject: Re: [PATCH v2] EDAC/versalnet: Refactor memory controller initialization and cleanup
Date: Sun, 9 Nov 2025 16:58:44 +0100	[thread overview]
Message-ID: <20251109155844.GUaRC6NHP2x4oO2Dk0@fat_crate.local> (raw)
In-Reply-To: <20251104093932.3838876-1-shubhrajyoti.datta@amd.com>

On Tue, Nov 04, 2025 at 03:09:20PM +0530, Shubhrajyoti Datta wrote:
> Simplify the initialization and cleanup flow for Versal Net DDRMC
> controllers in the EDAC driver.
> 
> Introduce `init_single_versalnet()` for per-controller setup and
>   `init_versalnet()` for looping through NUM_CONTROLLERS, also add
> rollback logic to handle  partial init failures.
> 
> Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
> ---
> 
> Changes in v2:
> - Rename init_single_versalnet() to init_mc() for clarity.
> - Rename remove_single_versalnet() to remove_mc() to match naming convention.
> - Simplify error handling in init_versalnet() by replacing goto with a rollback loop.
> - Reduce indentation and consolidate cleanup logic.

Better, here's some more improvements and cleanups ontop. You probably should
apply the diff to better see what I mean:

- do the kzalloc allocations first
- publish the structures only after they've been initialized properly so that
  you don't need to unwind unnecessarily when it fails later
- remove_versalnet() is now trivial

Do run it on the hw and have the code fail at certain places on purpose to
make sure the unwinding happens properly.

HTH.

---

diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index 01edc7408a5c..dc6108f7cee3 100644
--- a/drivers/edac/versalnet_edac.c
+++ b/drivers/edac/versalnet_edac.c
@@ -70,6 +70,8 @@
 #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.
@@ -758,7 +760,7 @@ static void versal_edac_release(struct device *dev)
 	kfree(dev);
 }
 
-static void remove_mc(struct mc_priv *priv, int i)
+static void remove_one_mc(struct mc_priv *priv, int i)
 {
 	struct mem_ctl_info *mci;
 
@@ -768,7 +770,7 @@ static void remove_mc(struct mc_priv *priv, int i)
 	edac_mc_free(mci);
 }
 
-static int init_mc(struct mc_priv *priv, struct platform_device *pdev, int i)
+static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i)
 {
 	u32 num_chans, rank, dwidth, config;
 	struct edac_mc_layer layers[2];
@@ -809,41 +811,54 @@ static int init_mc(struct mc_priv *priv, struct platform_device *pdev, int i)
 	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);
+	name = kzalloc(MC_NAME_LEN, GFP_KERNEL);
+	if (!name)
 		return rc;
-	}
-	priv->mci[i] = mci;
-	priv->dwidth = dt;
 
 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 	if (!dev)
-		goto err_mc_free;
-	dev->release = versal_edac_release;
-	name = kmalloc(32, GFP_KERNEL);
+		goto err_name_free;
+
+	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_dev_free;
+	}
+
 	sprintf(name, "versal-net-ddrmc5-edac-%d", i);
+
 	dev->init_name = name;
+	dev->release = versal_edac_release;
+
 	rc = device_register(dev);
 	if (rc)
 		goto err_mc_free;
 
 	mci->pdev = dev;
-
-	platform_set_drvdata(pdev, priv);
-
 	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;
 	}
+
+	priv->mci[i] = mci;
+	priv->dwidth = dt;
+
+	platform_set_drvdata(pdev, priv);
+
 	return 0;
+
 err_unreg:
 	device_unregister(mci->pdev);
 err_mc_free:
 	edac_mc_free(mci);
+err_dev_free:
+	kfree(dev);
+err_name_free:
+	kfree(name);
+
 	return rc;
 }
 
@@ -852,10 +867,10 @@ static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
 	int rc, i;
 
 	for (i = 0; i < NUM_CONTROLLERS; i++) {
-		rc = init_mc(priv, pdev, i);
+		rc = init_one_mc(priv, pdev, i);
 		if (rc) {
 			while (i--)
-				remove_mc(priv, i);
+				remove_one_mc(priv, i);
 			return rc;
 		}
 	}
@@ -864,14 +879,8 @@ static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
 
 static void remove_versalnet(struct mc_priv *priv)
 {
-	struct mem_ctl_info *mci;
-	int i;
-
-	for (i = 0; i < NUM_CONTROLLERS; i++) {
-		device_unregister(priv->mci[i]->pdev);
-		mci = edac_mc_del_mc(priv->mci[i]->pdev);
-		edac_mc_free(mci);
-	}
+	for (int i = 0; i < NUM_CONTROLLERS; i++)
+		remove_one_mc(priv, i);
 }
 
 static int mc_probe(struct platform_device *pdev)

-- 
Regards/Gruss,
    Boris.

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

  reply	other threads:[~2025-11-09 15:59 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-04  9:39 Shubhrajyoti Datta
2025-11-09 15:58 ` Borislav Petkov [this message]
2026-02-26 16:50   ` Datta, Shubhrajyoti
2026-02-28  9:09     ` Borislav Petkov
2026-03-02 11:13       ` Datta, Shubhrajyoti
2026-03-02 13:24         ` Borislav Petkov

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=20251109155844.GUaRC6NHP2x4oO2Dk0@fat_crate.local \
    --to=bp@alien8.de \
    --cc=james.morse@arm.com \
    --cc=linux-edac@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --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

all inboxes | Powered by JetHome®