* [PATCH v2 01/12] EDAC/versalnet: Fix device_register() error handling in init_one_mc()
2026-11-18 14:20 [PATCH v2 00/12] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
@ 2026-11-18 14:20 ` Shubhrajyoti Datta
2026-11-18 14:20 ` [PATCH v2 02/12] EDAC/versalnet: Use designated initializer for rpmsg_channel_info Shubhrajyoti Datta
` (10 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-18 14:20 UTC (permalink / raw)
To: linux-edac
Cc: git, shubhrajyoti.datta, michal.simek, Shubhrajyoti Datta,
Radhey Shyam Pandey, Krzysztof Kozlowski, Rob Herring,
Conor Dooley, Borislav Petkov, Tony Luck, linux-kernel,
devicetree
From: Prasanna Kumar T S M <ptsm@linux.microsoft.com>
When device_register() fails, it must be followed by put_device()
rather than kfree(), because device_register() calls
device_initialize() which sets up the device refcount. The matching
release function versal_edac_release() handles the actual kfree().
To simplify error handling and avoid complex unwinding, split
device_register() into device_initialize() and device_add().
Initialize the device early so put_device() can be used in all
error paths.
Fixes: d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")
Cc: stable@vger.kernel.org
Signed-off-by: Prasanna Kumar T S M <ptsm@linux.microsoft.com>
Co-developed-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
---
Changes in v2:
Add the codeveloped tag and remove the non standard signature
fix the name stack warning
drivers/edac/versalnet_edac.c | 36 +++++++++++++++++------------------
1 file changed, 18 insertions(+), 18 deletions(-)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index eae9fea60db1..89672d4d17c9 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.
@@ -777,7 +775,6 @@ 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];
struct mem_ctl_info *mci;
- char name[MC_NAME_LEN];
struct device *dev;
enum dev_type dt;
int rc;
@@ -817,28 +814,31 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i
if (!dev)
return rc;
+ dev->release = versal_edac_release;
+ device_initialize(dev);
+
+ rc = dev_set_name(dev, "versal-net-ddrmc5-edac-%d", i);
+ if (rc)
+ goto err_put_dev;
+
+ rc = device_add(dev);
+ if (rc)
+ goto err_put_dev;
+
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;
+ rc = -ENOMEM;
+ goto err_dev_del;
}
- 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;
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;
+ goto err_mc_free;
}
priv->mci[i] = mci;
@@ -848,12 +848,12 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i
return 0;
-err_unreg:
- device_unregister(mci->pdev);
err_mc_free:
edac_mc_free(mci);
-err_dev_free:
- kfree(dev);
+err_dev_del:
+ device_del(dev);
+err_put_dev:
+ put_device(dev);
return rc;
}
--
2.44.4
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 02/12] EDAC/versalnet: Use designated initializer for rpmsg_channel_info
2026-11-18 14:20 [PATCH v2 00/12] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
2026-11-18 14:20 ` [PATCH v2 01/12] EDAC/versalnet: Fix device_register() error handling in init_one_mc() Shubhrajyoti Datta
@ 2026-11-18 14:20 ` Shubhrajyoti Datta
2026-11-18 14:20 ` [PATCH v2 03/12] EDAC/versalnet: Fix use-after-free in remove_one_mc() Shubhrajyoti Datta
` (9 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-18 14:20 UTC (permalink / raw)
To: linux-edac
Cc: git, shubhrajyoti.datta, michal.simek, Shubhrajyoti Datta,
Radhey Shyam Pandey, Krzysztof Kozlowski, Rob Herring,
Conor Dooley, Borislav Petkov, Tony Luck, linux-kernel,
devicetree
Replace field-by-field assignment of struct rpmsg_channel_info with a
designated initializer. The old strscpy() call used strlen() as the
destination size, which truncated the channel name. Use the
string literal (via EDAC_RPMSG_CHAN_NAME) in the initializer so the
full name is passed to rpmsg_create_ept().
Fixes: d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
---
Changes in v2:
Use a macro for channel name
drivers/edac/versalnet_edac.c | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index 89672d4d17c9..de97b2ac45aa 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 EDAC_RPMSG_CHAN_NAME "error_ipc"
+
/**
* struct ecc_error_info - ECC error log information.
* @burstpos: Burst position.
@@ -713,21 +715,21 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,
}
static struct rpmsg_device_id amd_rpmsg_id_table[] = {
- { .name = "error_ipc" },
+ { .name = EDAC_RPMSG_CHAN_NAME },
{ },
};
MODULE_DEVICE_TABLE(rpmsg, amd_rpmsg_id_table);
static int rpmsg_probe(struct rpmsg_device *rpdev)
{
- struct rpmsg_channel_info chinfo;
+ struct rpmsg_channel_info chinfo = {
+ .src = RPMSG_ADDR_ANY,
+ .dst = rpdev->dst,
+ .name = EDAC_RPMSG_CHAN_NAME,
+ };
struct mc_priv *pg;
pg = (struct mc_priv *)amd_rpmsg_id_table[0].driver_data;
- chinfo.src = RPMSG_ADDR_ANY;
- chinfo.dst = rpdev->dst;
- strscpy(chinfo.name, amd_rpmsg_id_table[0].name,
- strlen(amd_rpmsg_id_table[0].name));
pg->ept = rpmsg_create_ept(rpdev, rpmsg_cb, NULL, chinfo);
if (!pg->ept)
--
2.44.4
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 03/12] EDAC/versalnet: Fix use-after-free in remove_one_mc()
2026-11-18 14:20 [PATCH v2 00/12] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
2026-11-18 14:20 ` [PATCH v2 01/12] EDAC/versalnet: Fix device_register() error handling in init_one_mc() Shubhrajyoti Datta
2026-11-18 14:20 ` [PATCH v2 02/12] EDAC/versalnet: Use designated initializer for rpmsg_channel_info Shubhrajyoti Datta
@ 2026-11-18 14:20 ` Shubhrajyoti Datta
2026-11-18 14:20 ` [PATCH v2 04/12] EDAC/versalnet: Add bounds validation in rpmsg_cb() Shubhrajyoti Datta
` (8 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-18 14:20 UTC (permalink / raw)
To: linux-edac
Cc: git, shubhrajyoti.datta, michal.simek, Shubhrajyoti Datta,
Radhey Shyam Pandey, Krzysztof Kozlowski, Rob Herring,
Conor Dooley, Borislav Petkov, Tony Luck, linux-kernel,
devicetree
device_unregister() drops the last reference on the device and invokes
versal_edac_release() which calls kfree(dev). The subsequent call to
edac_mc_del_mc(mci->pdev) then dereferences the freed pointer.
Fix by saving the device pointer, calling edac_mc_del_mc() and
edac_mc_free() first, then device_unregister() last so the device
is freed only after all users are done with it.
Fixes: d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
Reviewed-by: Radhey Shyam Pandey <radhey.shyam.pandey@amd.com>
---
Changes in v2:
Add reviewed by tag
drivers/edac/versalnet_edac.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index de97b2ac45aa..2d1417135e42 100644
--- a/drivers/edac/versalnet_edac.c
+++ b/drivers/edac/versalnet_edac.c
@@ -765,11 +765,13 @@ static void versal_edac_release(struct device *dev)
static void remove_one_mc(struct mc_priv *priv, int i)
{
struct mem_ctl_info *mci;
+ struct device *dev;
mci = priv->mci[i];
- device_unregister(mci->pdev);
- edac_mc_del_mc(mci->pdev);
+ dev = mci->pdev;
+ edac_mc_del_mc(dev);
edac_mc_free(mci);
+ device_unregister(dev);
}
static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i)
--
2.44.4
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 04/12] EDAC/versalnet: Add bounds validation in rpmsg_cb()
2026-11-18 14:20 [PATCH v2 00/12] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
` (2 preceding siblings ...)
2026-11-18 14:20 ` [PATCH v2 03/12] EDAC/versalnet: Fix use-after-free in remove_one_mc() Shubhrajyoti Datta
@ 2026-11-18 14:20 ` Shubhrajyoti Datta
2026-11-18 14:33 ` sashiko-bot
2026-11-18 14:20 ` [PATCH v2 05/12] EDAC/versalnet: Make dwidth per-controller Shubhrajyoti Datta
` (7 subsequent siblings)
11 siblings, 1 reply; 14+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-18 14:20 UTC (permalink / raw)
To: linux-edac
Cc: git, shubhrajyoti.datta, michal.simek, Shubhrajyoti Datta,
Radhey Shyam Pandey, Krzysztof Kozlowski, Rob Herring,
Conor Dooley, Borislav Petkov, Tony Luck, linux-kernel,
devicetree
The firmware-supplied offset and length values from the RPMsg payload
are used without validation to index mc_priv->regs[] (REG_MAX entries).
A malformed or buggy firmware message could access past the end of the
array, corrupting adjacent structure members or kernel memory.
Add bounds checks for the offset and length before accessing the array.
rpmsg_cb() supports messages split across two RPMsg callbacks. If
validation fails for either part of a split message, discard the entire
message.
Fixes: d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
---
Changes in v2:
Sashiko comment
"This is a pre-existing issue, but are offset and length validated before
being used as indices?"
drivers/edac/versalnet_edac.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index 2d1417135e42..0364648d91a1 100644
--- a/drivers/edac/versalnet_edac.c
+++ b/drivers/edac/versalnet_edac.c
@@ -607,6 +607,13 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,
length = result[MSG_ERR_LENGTH];
offset = result[MSG_ERR_OFFSET];
+ if (length > REG_MAX || offset > REG_MAX - length ||
+ result[TOTAL_ERR_LENGTH] > REG_MAX) {
+ dev_warn_ratelimited(&rpdev->dev,
+ "Rejecting the faulty message\n");
+ return -EINVAL;
+ }
+
/*
* The data can come in two stretches. Construct the regs from two
* messages. The offset indicates the offset from which the data is to
--
2.44.4
^ permalink raw reply related [flat|nested] 14+ messages in thread* Re: [PATCH v2 04/12] EDAC/versalnet: Add bounds validation in rpmsg_cb()
2026-11-18 14:20 ` [PATCH v2 04/12] EDAC/versalnet: Add bounds validation in rpmsg_cb() Shubhrajyoti Datta
@ 2026-11-18 14:33 ` sashiko-bot
0 siblings, 0 replies; 14+ messages in thread
From: sashiko-bot @ 2026-11-18 14:33 UTC (permalink / raw)
To: Shubhrajyoti Datta; +Cc: devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] EDAC/versalnet: stale part_len on split-message failure in
rpmsg_cb()
--
commit a43cb655a4c7983a62adb71672a8f139d96e6c3c
Author: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
EDAC/versalnet: Add bounds validation in rpmsg_cb()
This commit adds bounds checks on the RPMsg payload's offset, length, and
total error length before indexing mc_priv->regs[] in rpmsg_cb().
> diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
> index 2d1417135e42a..0364648d91a1e 100644
> --- a/drivers/edac/versalnet_edac.c
> +++ b/drivers/edac/versalnet_edac.c
> @@ -607,6 +607,13 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,
> length = result[MSG_ERR_LENGTH];
> offset = result[MSG_ERR_OFFSET];
>
> + if (length > REG_MAX || offset > REG_MAX - length ||
> + result[TOTAL_ERR_LENGTH] > REG_MAX) {
> + dev_warn_ratelimited(&rpdev->dev,
> + "Rejecting the faulty message\n");
> + return -EINVAL;
> + }
> +
> /*
> * The data can come in two stretches. Construct the regs from two
> * messages. The offset indicates the offset from which the data is to
[Severity: Medium]
Does rejecting one part of a split message leave mc_priv->part_len out of
sync instead of discarding the entire message?
When the first part of a split message succeeds, rpmsg_cb() stores its
length in mc_priv->part_len and returns 0:
drivers/edac/versalnet_edac.c:rpmsg_cb() {
...
if (result[TOTAL_ERR_LENGTH] > length) {
if (!mc_priv->part_len)
mc_priv->part_len = length;
else
mc_priv->part_len += length;
if (mc_priv->part_len < result[TOTAL_ERR_LENGTH])
return 0;
mc_priv->part_len = 0;
}
...
}
If the second part then fails the bounds check and returns -EINVAL,
mc_priv->part_len is not reset to 0. When the first part of the next
valid split message arrives, its length is added to the stale
mc_priv->part_len, causing rpmsg_cb() to report an error prematurely from
incomplete and stale data in mc_priv->regs[] and leaving mc_priv->part_len
desynchronized for subsequent split messages.
Conversely, if the first part of a split message is rejected (leaving
mc_priv->part_len at 0) and the second part arrives with a valid non-zero
offset, rpmsg_cb() does not check that offset matches mc_priv->part_len
and accepts the orphaned second fragment.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118142213.100581-1-shubhrajyoti.datta@amd.com?part=4
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v2 05/12] EDAC/versalnet: Make dwidth per-controller
2026-11-18 14:20 [PATCH v2 00/12] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
` (3 preceding siblings ...)
2026-11-18 14:20 ` [PATCH v2 04/12] EDAC/versalnet: Add bounds validation in rpmsg_cb() Shubhrajyoti Datta
@ 2026-11-18 14:20 ` Shubhrajyoti Datta
2026-11-18 14:20 ` [PATCH v2 06/12] EDAC/versalnet: Wait for RPMsg endpoint initialization Shubhrajyoti Datta
` (6 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-18 14:20 UTC (permalink / raw)
To: linux-edac
Cc: git, shubhrajyoti.datta, michal.simek, Shubhrajyoti Datta,
Radhey Shyam Pandey, Krzysztof Kozlowski, Rob Herring,
Conor Dooley, Borislav Petkov, Tony Luck, linux-kernel,
devicetree
Currently dwidth is a single scalar in mc_priv, so each call to
init_one_mc() overwrites the previous controller's value.
Fix this by converting dwidth to a per-controller array and passing
the device type directly to mc_init() from the detected dt.
This also updates the DIMM device type reported in EDAC.
Fixes: d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
---
Changes in v2:
https://sashiko.dev/#/patchset/20260724171945.2812749-1-shubhrajyoti.datta%40amd.com
New patch to fix shashiko review comment "this is a pre-existing
issue, but is the per-controller width overwriting the shared
state for all controllers?".
drivers/edac/versalnet_edac.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index 0364648d91a1..ea90d3ce3e70 100644
--- a/drivers/edac/versalnet_edac.c
+++ b/drivers/edac/versalnet_edac.c
@@ -140,7 +140,7 @@ struct ecc_status {
* @stat: ECC status information.
* @error_id: The error id.
* @error_level: The error level.
- * @dwidth: Width of data bus excluding ECC bits.
+ * @dwidth: Per-controller width of data bus excluding ECC bits.
* @part_len: The support of the message received.
* @regs: The registers sent on the rpmsg.
* @adec: Address decode registers.
@@ -153,7 +153,7 @@ struct mc_priv {
struct ecc_status stat;
u32 error_id;
u32 error_level;
- u32 dwidth;
+ enum dev_type dwidth[NUM_CONTROLLERS];
u32 part_len;
u32 regs[REG_MAX];
u32 adec[ADEC_MAX];
@@ -399,7 +399,7 @@ static unsigned long convert_to_physical(struct mc_priv *priv,
else
ilc_base_ctrl_add = ilcmem_base - low_mem_offset;
- if (priv->dwidth == DEV_X16) {
+ if (priv->dwidth[controller] == DEV_X16) {
blk = err_addr / MC5_X16_SIZE;
rsh_req_addr = (blk << 8) + ilc_base_ctrl_add;
err_addr = rsh_req_addr * interleave * 2;
@@ -473,9 +473,9 @@ static void handle_error(struct mc_priv *priv, struct ecc_status *stat,
}
}
-static void mc_init(struct mem_ctl_info *mci, struct device *dev)
+static void mc_init(struct mem_ctl_info *mci, struct device *dev,
+ enum dev_type dtype)
{
- struct mc_priv *priv = mci->pvt_info;
struct csrow_info *csi;
struct dimm_info *dimm;
u32 row;
@@ -501,7 +501,7 @@ static void mc_init(struct mem_ctl_info *mci, struct device *dev)
dimm->edac_mode = EDAC_SECDED;
dimm->mtype = MEM_DDR5;
dimm->grain = MC5_ERR_GRAIN;
- dimm->dtype = priv->dwidth;
+ dimm->dtype = dtype;
}
}
}
@@ -844,7 +844,7 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i
}
mci->pdev = dev;
- mc_init(mci, dev);
+ mc_init(mci, dev, dt);
rc = edac_mc_add_mc(mci);
if (rc) {
@@ -853,7 +853,7 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i
}
priv->mci[i] = mci;
- priv->dwidth = dt;
+ priv->dwidth[i] = dt;
platform_set_drvdata(pdev, priv);
--
2.44.4
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 06/12] EDAC/versalnet: Wait for RPMsg endpoint initialization
2026-11-18 14:20 [PATCH v2 00/12] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
` (4 preceding siblings ...)
2026-11-18 14:20 ` [PATCH v2 05/12] EDAC/versalnet: Make dwidth per-controller Shubhrajyoti Datta
@ 2026-11-18 14:20 ` Shubhrajyoti Datta
2026-11-18 14:20 ` [PATCH v2 07/12] EDAC/versalnet: Initialize MCDI before RPMsg registration Shubhrajyoti Datta
` (5 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-18 14:20 UTC (permalink / raw)
To: linux-edac
Cc: git, shubhrajyoti.datta, michal.simek, Shubhrajyoti Datta,
Radhey Shyam Pandey, Krzysztof Kozlowski, Rob Herring,
Conor Dooley, Borislav Petkov, Tony Luck, linux-kernel,
devicetree
From: Hayden Rinn <haydenrinn@microsoft.com>
The rpmsg endpoint can be used before rpmsg_probe() completes, and
can race with asynchronous rpmsg_remove() teardown (e.g. R5 crash
or shutdown), both leading to use of an uninitialized or freed
endpoint.
Add a completion so mc_probe() waits for the endpoint, and a
"ready" flag so rpmsg_cb() drops events until mci/adec are
set up. Add ept_lock to serialize priv->ept and priv->mcdi->ept
across mcdi_request(), rpmsg_probe(), and rpmsg_remove(), clearing
them on removal and failing mcdi_request() if the channel is down.
Fixes: d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")
Signed-off-by: Hayden Rinn <haydenrinn@microsoft.com>
Co-developed-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
---
Changes in v2:
Patch addition for the rpmsg endpoint race
drivers/edac/versalnet_edac.c | 123 ++++++++++++++++++++++++++++++----
1 file changed, 111 insertions(+), 12 deletions(-)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index ea90d3ce3e70..90918293f76d 100644
--- a/drivers/edac/versalnet_edac.c
+++ b/drivers/edac/versalnet_edac.c
@@ -5,8 +5,10 @@
*/
#include <linux/cdx/edac_cdx_pcol.h>
+#include <linux/completion.h>
#include <linux/edac.h>
#include <linux/module.h>
+#include <linux/mutex.h>
#include <linux/of_device.h>
#include <linux/ras.h>
#include <linux/remoteproc.h>
@@ -146,7 +148,22 @@ struct ecc_status {
* @adec: Address decode registers.
* @mci: Memory controller interface.
* @ept: rpmsg endpoint.
+ * @ept_lock: Protects @ept and @mcdi->ept against concurrent teardown
+ * by rpmsg_remove() (e.g. remote processor crash/shutdown)
+ * while mcdi_request() is sending on them.
+ * @rpmsg_ready: Completion signaled once rpmsg_probe() has run to
+ * completion success. @ept is only valid to check
+ * once this is signaled.
* @mcdi: The mcdi handle.
+ * @dev: Platform device backing this driver instance. Stays
+ * valid for as long as mcdi_request() can be called,
+ * unlike @mcdi->rpdev, which is torn down and cleared
+ * by rpmsg_remove(). Used for logging when there is no
+ * live rpmsg channel to hang the message off of.
+ * @ready: Set once edac priv data is initialised. Published with a
+ * release store and read with an acquire load so that rpmsg_cb
+ * never calls handle_error() against a @mci entry that hasn't
+ * been allocated yet.
*/
struct mc_priv {
char message[256];
@@ -159,7 +176,11 @@ struct mc_priv {
u32 adec[ADEC_MAX];
struct mem_ctl_info *mci[NUM_CONTROLLERS];
struct rpmsg_endpoint *ept;
+ struct mutex ept_lock; /* Protects endpoint */
+ struct completion rpmsg_ready;
struct cdx_mcdi *mcdi;
+ struct device *dev;
+ bool ready;
};
/*
@@ -508,6 +529,12 @@ static void mc_init(struct mem_ctl_info *mci, struct device *dev,
#define to_mci(k) container_of(k, struct mem_ctl_info, dev)
+static struct rpmsg_device_id amd_rpmsg_id_table[] = {
+ { .name = EDAC_RPMSG_CHAN_NAME },
+ { },
+};
+MODULE_DEVICE_TABLE(rpmsg, amd_rpmsg_id_table);
+
static unsigned int mcdi_rpc_timeout(struct cdx_mcdi *cdx, unsigned int cmd)
{
return MCDI_RPC_TIMEOUT;
@@ -517,6 +544,7 @@ static void mcdi_request(struct cdx_mcdi *cdx,
const struct cdx_dword *hdr, size_t hdr_len,
const struct cdx_dword *sdu, size_t sdu_len)
{
+ struct mc_priv *mc_priv = (struct mc_priv *)amd_rpmsg_id_table[0].driver_data;
void *send_buf;
int ret;
@@ -527,6 +555,24 @@ static void mcdi_request(struct cdx_mcdi *cdx,
memcpy(send_buf, hdr, hdr_len);
memcpy(send_buf + hdr_len, sdu, sdu_len);
+ guard(mutex)(&mc_priv->ept_lock);
+
+ /*
+ * The rpmsg channel can be torn down asynchronously (e.g. the R5
+ * remote processor crashing or being shut down) by rpmsg_remove(),
+ * which clears both @cdx->ept and @mc_priv->ept under this same
+ * lock. Bail out instead of sending on a destroyed endpoint.
+ *
+ * @cdx->rpdev is cleared by rpmsg_remove() together with @cdx->ept,
+ * so it cannot be used for logging here; use the stable platform
+ * device instead.
+ */
+ if (!cdx->ept) {
+ dev_err(mc_priv->dev, "Dropping rpmsg send, channel is down\n");
+ kfree(send_buf);
+ return;
+ }
+
ret = rpmsg_send(cdx->ept, send_buf, hdr_len + sdu_len);
if (ret)
dev_err(&cdx->rpdev->dev, "Failed to send rpmsg data: %d\n", ret);
@@ -601,6 +647,18 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,
cdx_mcdi_process_cmd(mc_priv->mcdi, (struct cdx_dword *)data, len);
return 0;
}
+ /*
+ * The rpmsg channel (and thus this callback) can become live
+ * before init_versalnet() has allocated priv->mci[] and
+ * populated priv->adec[]. Drop reports that race ahead
+ * of it instead of calling handle_error() against incomplete
+ * state.
+ */
+ if (!smp_load_acquire(&mc_priv->ready)) {
+ dev_warn_ratelimited(&rpdev->dev,
+ "Dropping event, driver not ready yet\n");
+ return 0;
+ }
sec_sev = result[ERROR_LEVEL];
error_id = result[ERROR_ID];
@@ -721,12 +779,6 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,
return 0;
}
-static struct rpmsg_device_id amd_rpmsg_id_table[] = {
- { .name = EDAC_RPMSG_CHAN_NAME },
- { },
-};
-MODULE_DEVICE_TABLE(rpmsg, amd_rpmsg_id_table);
-
static int rpmsg_probe(struct rpmsg_device *rpdev)
{
struct rpmsg_channel_info chinfo = {
@@ -734,25 +786,56 @@ static int rpmsg_probe(struct rpmsg_device *rpdev)
.dst = rpdev->dst,
.name = EDAC_RPMSG_CHAN_NAME,
};
+ struct rpmsg_endpoint *ept;
struct mc_priv *pg;
pg = (struct mc_priv *)amd_rpmsg_id_table[0].driver_data;
- pg->ept = rpmsg_create_ept(rpdev, rpmsg_cb, NULL, chinfo);
- if (!pg->ept)
- return dev_err_probe(&rpdev->dev, -ENXIO, "Failed to create ept for channel %s\n",
- chinfo.name);
-
dev_set_drvdata(&rpdev->dev, pg);
+ ept = rpmsg_create_ept(rpdev, rpmsg_cb, NULL, chinfo);
+ if (!ept) {
+ return dev_err_probe(&rpdev->dev, -ENXIO,
+ "Failed to create ept for channel %s\n",
+ chinfo.name);
+ }
+
+ /*
+ * Link the new endpoint in under the lock. This also re-syncs
+ * mcdi->ept on reconnection after the remote processor recovers
+ * from a crash/shutdown, which previously only happened once,
+ * during the initial mc_probe().
+ */
+ scoped_guard(mutex, &pg->ept_lock) {
+ pg->ept = ept;
+ pg->mcdi->ept = ept;
+ pg->mcdi->rpdev = rpdev;
+ }
+
+ complete_all(&pg->rpmsg_ready);
+
return 0;
}
static void rpmsg_remove(struct rpmsg_device *rpdev)
{
struct mc_priv *mc_priv = dev_get_drvdata(&rpdev->dev);
+ struct rpmsg_endpoint *ept;
+
+ /*
+ * Clear the shared endpoint pointers before destroying the
+ * endpoint, so that mcdi_request() (serialised on the same lock)
+ * cannot observe or send on a stale/freed endpoint if the remote
+ * processor goes down concurrently.
+ */
+ scoped_guard(mutex, &mc_priv->ept_lock) {
+ ept = mc_priv->ept;
+ mc_priv->ept = NULL;
+ mc_priv->mcdi->ept = NULL;
+ mc_priv->mcdi->rpdev = NULL;
+ }
- rpmsg_destroy_ept(mc_priv->ept);
+ rpmsg_destroy_ept(ept);
dev_set_drvdata(&rpdev->dev, NULL);
}
@@ -920,6 +1003,9 @@ static int mc_probe(struct platform_device *pdev)
goto err_alloc;
}
+ priv->dev = &pdev->dev;
+ mutex_init(&priv->ept_lock);
+ init_completion(&priv->rpmsg_ready);
amd_rpmsg_id_table[0].driver_data = (kernel_ulong_t)priv;
rc = register_rpmsg_driver(&amd_rpmsg_driver);
@@ -932,12 +1018,25 @@ static int mc_probe(struct platform_device *pdev)
if (rc)
goto err_unreg;
+ if (!wait_for_completion_timeout(&priv->rpmsg_ready, MCDI_RPC_TIMEOUT)) {
+ dev_err(&pdev->dev, "Timed out waiting for RPMsg endpoint\n");
+ rc = -ENODEV;
+ goto err_init;
+ }
+
priv->mcdi->r5_rproc = rp;
rc = init_versalnet(priv, pdev);
if (rc)
goto err_init;
+ /*
+ * edac is initialised. Publish this with a release store so rpmsg_cb
+ * running concurrently on the rpmsg worker thread, only calls
+ * handle_error() once it observes @ready via a matching acquire load.
+ */
+ smp_store_release(&priv->ready, true);
+
return 0;
err_init:
--
2.44.4
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 07/12] EDAC/versalnet: Initialize MCDI before RPMsg registration
2026-11-18 14:20 [PATCH v2 00/12] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
` (5 preceding siblings ...)
2026-11-18 14:20 ` [PATCH v2 06/12] EDAC/versalnet: Wait for RPMsg endpoint initialization Shubhrajyoti Datta
@ 2026-11-18 14:20 ` Shubhrajyoti Datta
2026-11-18 14:20 ` [PATCH v2 08/12] EDAC/versalnet: Guard against NULL drvdata in rpmsg_cb() Shubhrajyoti Datta
` (4 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-18 14:20 UTC (permalink / raw)
To: linux-edac
Cc: git, shubhrajyoti.datta, michal.simek, Shubhrajyoti Datta,
Radhey Shyam Pandey, Krzysztof Kozlowski, Rob Herring,
Conor Dooley, Borislav Petkov, Tony Luck, linux-kernel,
devicetree
setup_mcdi() currently allocates the MCDI instance, assigns the RPMsg
endpoint, and retrieves DDR configuration data. The DDR configuration
path invokes cdx_mcdi_rpc(), which requires a functional RPMsg endpoint.
Split setup_mcdi() so that only MCDI allocation and initialization are
performed before RPMsg registration. Update the error paths to match
the new initialization order, ensuring resources are released in
reverse order of acquisition.
Fixes: d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
---
Changes in v2:
Update the labels
this fixes the sashiko comment
"register_rpmsg_driver() exposes the callback endpoint before setup_mcdi()
initializes the mcdi pointer. Could this lead to a NULL pointer dereference
if a response arrives early?"
drivers/edac/versalnet_edac.c | 27 +++++++++++++--------------
1 file changed, 13 insertions(+), 14 deletions(-)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index 90918293f76d..046293d54d40 100644
--- a/drivers/edac/versalnet_edac.c
+++ b/drivers/edac/versalnet_edac.c
@@ -605,7 +605,7 @@ static void get_ddr_config(u32 index, u32 *buffer, struct cdx_mcdi *amd_mcdi)
static int setup_mcdi(struct mc_priv *mc_priv)
{
struct cdx_mcdi *amd_mcdi;
- int ret, i;
+ int ret;
amd_mcdi = kzalloc_obj(*amd_mcdi);
if (!amd_mcdi)
@@ -618,12 +618,8 @@ static int setup_mcdi(struct mc_priv *mc_priv)
return ret;
}
- amd_mcdi->ept = mc_priv->ept;
mc_priv->mcdi = amd_mcdi;
- for (i = 0; i < NUM_CONTROLLERS; i++)
- get_ddr_config(i, &mc_priv->adec[ADEC_NUM * i], amd_mcdi);
-
return 0;
}
@@ -978,7 +974,7 @@ static int mc_probe(struct platform_device *pdev)
{
struct mc_priv *priv;
struct rproc *rp;
- int rc;
+ int rc, i;
struct device_node *r5_core_node __free(device_node) =
of_parse_phandle(pdev->dev.of_node, "amd,rproc", 0);
@@ -1008,16 +1004,16 @@ static int mc_probe(struct platform_device *pdev)
init_completion(&priv->rpmsg_ready);
amd_rpmsg_id_table[0].driver_data = (kernel_ulong_t)priv;
+ rc = setup_mcdi(priv);
+ if (rc)
+ goto err_alloc;
+
rc = register_rpmsg_driver(&amd_rpmsg_driver);
if (rc) {
edac_printk(KERN_ERR, EDAC_MC, "Failed to register RPMsg driver: %d\n", rc);
- goto err_alloc;
+ goto err_reg;
}
- rc = setup_mcdi(priv);
- if (rc)
- goto err_unreg;
-
if (!wait_for_completion_timeout(&priv->rpmsg_ready, MCDI_RPC_TIMEOUT)) {
dev_err(&pdev->dev, "Timed out waiting for RPMsg endpoint\n");
rc = -ENODEV;
@@ -1026,6 +1022,9 @@ static int mc_probe(struct platform_device *pdev)
priv->mcdi->r5_rproc = rp;
+ for (i = 0; i < NUM_CONTROLLERS; i++)
+ get_ddr_config(i, &priv->adec[ADEC_NUM * i], priv->mcdi);
+
rc = init_versalnet(priv, pdev);
if (rc)
goto err_init;
@@ -1040,12 +1039,12 @@ static int mc_probe(struct platform_device *pdev)
return 0;
err_init:
+ unregister_rpmsg_driver(&amd_rpmsg_driver);
+
+err_reg:
cdx_mcdi_finish(priv->mcdi);
kfree(priv->mcdi);
-err_unreg:
- unregister_rpmsg_driver(&amd_rpmsg_driver);
-
err_alloc:
rproc_shutdown(rp);
--
2.44.4
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 08/12] EDAC/versalnet: Guard against NULL drvdata in rpmsg_cb()
2026-11-18 14:20 [PATCH v2 00/12] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
` (6 preceding siblings ...)
2026-11-18 14:20 ` [PATCH v2 07/12] EDAC/versalnet: Initialize MCDI before RPMsg registration Shubhrajyoti Datta
@ 2026-11-18 14:20 ` Shubhrajyoti Datta
2026-11-18 14:20 ` [PATCH v2 09/12] EDAC/versalnet: Move platform_set_drvdata() to mc_probe() Shubhrajyoti Datta
` (3 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-18 14:20 UTC (permalink / raw)
To: linux-edac
Cc: git, shubhrajyoti.datta, michal.simek, Shubhrajyoti Datta,
Radhey Shyam Pandey, Krzysztof Kozlowski, Rob Herring,
Conor Dooley, Borislav Petkov, Tony Luck, linux-kernel,
devicetree
The rpmsg core creates its own endpoint bound to rpmsg_cb() in
rpmsg_dev_probe(), before rpmsg_probe() runs and attaches mc_priv via
dev_set_drvdata(). A message dispatched to that endpoint in that
window would dereference a NULL mc_priv and crash.
The same race exists on the teardown side: rpmsg_dev_remove() calls
rpmsg_remove() first, which clears drvdata back to NULL, and only
destroys the core's default endpoint afterwards. A message arriving
in that window reaches rpmsg_cb() with drvdata already NULL.
Check for a NULL mc_priv in rpmsg_cb() and drop the message instead
of crashing, in either window.
Fixes: d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
---
Changes in v2:
Patch addition for the rpmsg drvdata race
drivers/edac/versalnet_edac.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index 046293d54d40..1a327a2dbda7 100644
--- a/drivers/edac/versalnet_edac.c
+++ b/drivers/edac/versalnet_edac.c
@@ -639,6 +639,12 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,
const char *err_str;
u32 *adec_data;
+ if (!mc_priv) {
+ dev_warn_ratelimited(&rpdev->dev,
+ "Dropping rpmsg message, driver not ready or being removed\n");
+ return 0;
+ }
+
if (*(u8 *)data == MCDI_RESPONSE) {
cdx_mcdi_process_cmd(mc_priv->mcdi, (struct cdx_dword *)data, len);
return 0;
--
2.44.4
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 09/12] EDAC/versalnet: Move platform_set_drvdata() to mc_probe()
2026-11-18 14:20 [PATCH v2 00/12] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
` (7 preceding siblings ...)
2026-11-18 14:20 ` [PATCH v2 08/12] EDAC/versalnet: Guard against NULL drvdata in rpmsg_cb() Shubhrajyoti Datta
@ 2026-11-18 14:20 ` Shubhrajyoti Datta
2026-11-18 14:20 ` [PATCH v2 10/12] dt-bindings: memory: xlnx,versal-net-ddrmc5: Add amd,num-ctrl property Shubhrajyoti Datta
` (2 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-18 14:20 UTC (permalink / raw)
To: linux-edac
Cc: git, shubhrajyoti.datta, michal.simek, Shubhrajyoti Datta,
Radhey Shyam Pandey, Krzysztof Kozlowski, Rob Herring,
Conor Dooley, Borislav Petkov, Tony Luck, linux-kernel,
devicetree
Move platform_set_drvdata() out of init_one_mc() and into mc_probe()
so that the driver data is set once during probe rather than being
redundantly set on each memory controller initialization.
The pdev parameter in init_one_mc() and init_versalnet() is no longer
referenced. Remove it from both function signatures.
Reviewed-by: Radhey Shyam Pandey <radhey.shyam.pandey@amd.com>
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
---
Changes in v2:
Add reviewed-by tag
drivers/edac/versalnet_edac.c | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index 1a327a2dbda7..7c14f1e78da1 100644
--- a/drivers/edac/versalnet_edac.c
+++ b/drivers/edac/versalnet_edac.c
@@ -866,7 +866,7 @@ static void remove_one_mc(struct mc_priv *priv, int i)
device_unregister(dev);
}
-static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i)
+static int init_one_mc(struct mc_priv *priv, int i)
{
u32 num_chans, rank, dwidth, config;
struct edac_mc_layer layers[2];
@@ -940,8 +940,6 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i
priv->mci[i] = mci;
priv->dwidth[i] = dt;
- platform_set_drvdata(pdev, priv);
-
return 0;
err_mc_free:
@@ -954,12 +952,12 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i
return rc;
}
-static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
+static int init_versalnet(struct mc_priv *priv)
{
int rc, i;
for (i = 0; i < NUM_CONTROLLERS; i++) {
- rc = init_one_mc(priv, pdev, i);
+ rc = init_one_mc(priv, i);
if (rc) {
while (i--)
remove_one_mc(priv, i);
@@ -1006,6 +1004,7 @@ static int mc_probe(struct platform_device *pdev)
}
priv->dev = &pdev->dev;
+ platform_set_drvdata(pdev, priv);
mutex_init(&priv->ept_lock);
init_completion(&priv->rpmsg_ready);
amd_rpmsg_id_table[0].driver_data = (kernel_ulong_t)priv;
@@ -1031,7 +1030,7 @@ static int mc_probe(struct platform_device *pdev)
for (i = 0; i < NUM_CONTROLLERS; i++)
get_ddr_config(i, &priv->adec[ADEC_NUM * i], priv->mcdi);
- rc = init_versalnet(priv, pdev);
+ rc = init_versalnet(priv);
if (rc)
goto err_init;
--
2.44.4
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 10/12] dt-bindings: memory: xlnx,versal-net-ddrmc5: Add amd,num-ctrl property
2026-11-18 14:20 [PATCH v2 00/12] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
` (8 preceding siblings ...)
2026-11-18 14:20 ` [PATCH v2 09/12] EDAC/versalnet: Move platform_set_drvdata() to mc_probe() Shubhrajyoti Datta
@ 2026-11-18 14:20 ` Shubhrajyoti Datta
2026-11-18 14:20 ` [PATCH v2 11/12] EDAC/versalnet: use device tree property for number of controllers Shubhrajyoti Datta
2026-11-18 14:20 ` [PATCH v2 12/12] EDAC/versalnet: Make get_ddr_config() return errors Shubhrajyoti Datta
11 siblings, 0 replies; 14+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-18 14:20 UTC (permalink / raw)
To: linux-edac
Cc: git, shubhrajyoti.datta, michal.simek, Shubhrajyoti Datta,
Radhey Shyam Pandey, Krzysztof Kozlowski, Rob Herring,
Conor Dooley, Borislav Petkov, Tony Luck, linux-kernel,
devicetree
Add the amd,num-ctrl property to describe the number of DDR memory
controllers enabled in the design. Valid range is 1 to 8.
The driver then can enumerate only the enabled controllers.
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
---
Changes in v2:
Add amd,num-ctrl property to device tree
The property was requested in the discussion below
https://lore.kernel.org/all/34ffba5a-8e9a-49b9-9e36-c31d0e4369f8@amd.com/
.../memory-controllers/xlnx,versal-net-ddrmc5.yaml | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/Documentation/devicetree/bindings/memory-controllers/xlnx,versal-net-ddrmc5.yaml b/Documentation/devicetree/bindings/memory-controllers/xlnx,versal-net-ddrmc5.yaml
index 479288567d0b..65feacd007a1 100644
--- a/Documentation/devicetree/bindings/memory-controllers/xlnx,versal-net-ddrmc5.yaml
+++ b/Documentation/devicetree/bindings/memory-controllers/xlnx,versal-net-ddrmc5.yaml
@@ -27,6 +27,15 @@ properties:
with remote processor. APU primarily communicates with the RPU for
accessing the DDRMC address space and getting error notification.
+ amd,num-ctrl:
+ description:
+ Number of DDR memory controller (DDRMC) instances enabled in the
+ design.
+ $ref: /schemas/types.yaml#/definitions/uint32
+ minimum: 1
+ maximum: 8
+ default: 8
+
required:
- compatible
- amd,rproc
--
2.44.4
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 11/12] EDAC/versalnet: use device tree property for number of controllers
2026-11-18 14:20 [PATCH v2 00/12] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
` (9 preceding siblings ...)
2026-11-18 14:20 ` [PATCH v2 10/12] dt-bindings: memory: xlnx,versal-net-ddrmc5: Add amd,num-ctrl property Shubhrajyoti Datta
@ 2026-11-18 14:20 ` Shubhrajyoti Datta
2026-11-18 14:20 ` [PATCH v2 12/12] EDAC/versalnet: Make get_ddr_config() return errors Shubhrajyoti Datta
11 siblings, 0 replies; 14+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-18 14:20 UTC (permalink / raw)
To: linux-edac
Cc: git, shubhrajyoti.datta, michal.simek, Shubhrajyoti Datta,
Radhey Shyam Pandey, Krzysztof Kozlowski, Rob Herring,
Conor Dooley, Borislav Petkov, Tony Luck, linux-kernel,
devicetree
Use the number of enabled controllers read from the "amd,num-ctrl"
device tree property for registering instead of all the controllers.
The enabled controllers are contiguous. Add a
num_ctrl field to mc_priv and use it in probe, init, remove,
error handling, and the rpmsg callback, allowing the driver to
support platforms with varying numbers of memory controllers enabled.
This keeps the old behaviour for older device trees. On a platform with
whose device tree lacks the property, the old iteration over all 8 indices,
remains in effect.
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
---
Changes in v2:
Adding the device tree parameter was suggested in the review in v1
https://lore.kernel.org/all/34ffba5a-8e9a-49b9-9e36-c31d0e4369f8@amd.com/
drivers/edac/versalnet_edac.c | 28 ++++++++++++++++++++++------
1 file changed, 22 insertions(+), 6 deletions(-)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index 7c14f1e78da1..c2025f5744cc 100644
--- a/drivers/edac/versalnet_edac.c
+++ b/drivers/edac/versalnet_edac.c
@@ -140,6 +140,7 @@ struct ecc_status {
* struct mc_priv - DDR memory controller private instance data.
* @message: Buffer for framing the event specific info.
* @stat: ECC status information.
+ * @num_ctrl: Number of DDR memory controllers.
* @error_id: The error id.
* @error_level: The error level.
* @dwidth: Per-controller width of data bus excluding ECC bits.
@@ -168,6 +169,7 @@ struct ecc_status {
struct mc_priv {
char message[256];
struct ecc_status stat;
+ u32 num_ctrl;
u32 error_id;
u32 error_level;
enum dev_type dwidth[NUM_CONTROLLERS];
@@ -456,7 +458,7 @@ static void handle_error(struct mc_priv *priv, struct ecc_status *stat,
phys_addr_t pfn;
int err;
- if (WARN_ON_ONCE(ctl_num >= NUM_CONTROLLERS))
+ if (WARN_ON_ONCE(ctl_num >= priv->num_ctrl))
return;
mci = priv->mci[ctl_num];
@@ -713,7 +715,7 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,
p = &mc_priv->stat;
memset(p, 0, sizeof(struct ecc_status));
p->error_type = MC5_ERR_TYPE_CE;
- for (i = 0 ; i < NUM_CONTROLLERS; i++) {
+ for (i = 0 ; i < mc_priv->num_ctrl; i++) {
if (get_ddr_info(&mc_priv->regs[i * REGS_PER_CONTROLLER], mc_priv)) {
adec_data = mc_priv->adec + ADEC_NUM * i;
handle_error(mc_priv, &mc_priv->stat, i, adec_data);
@@ -724,7 +726,7 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,
p = &mc_priv->stat;
memset(p, 0, sizeof(struct ecc_status));
p->error_type = MC5_ERR_TYPE_UE;
- for (i = 0 ; i < NUM_CONTROLLERS; i++) {
+ for (i = 0 ; i < mc_priv->num_ctrl; i++) {
if (get_ddr_info(&mc_priv->regs[i * REGS_PER_CONTROLLER], mc_priv)) {
adec_data = mc_priv->adec + ADEC_NUM * i;
handle_error(mc_priv, &mc_priv->stat, i, adec_data);
@@ -956,7 +958,7 @@ static int init_versalnet(struct mc_priv *priv)
{
int rc, i;
- for (i = 0; i < NUM_CONTROLLERS; i++) {
+ for (i = 0; i < priv->num_ctrl; i++) {
rc = init_one_mc(priv, i);
if (rc) {
while (i--)
@@ -970,7 +972,7 @@ static int init_versalnet(struct mc_priv *priv)
static void remove_versalnet(struct mc_priv *priv)
{
- for (int i = 0; i < NUM_CONTROLLERS; i++)
+ for (int i = 0; i < priv->num_ctrl; i++)
remove_one_mc(priv, i);
}
@@ -978,6 +980,7 @@ static int mc_probe(struct platform_device *pdev)
{
struct mc_priv *priv;
struct rproc *rp;
+ u32 num_ctrl;
int rc, i;
struct device_node *r5_core_node __free(device_node) =
@@ -987,6 +990,18 @@ static int mc_probe(struct platform_device *pdev)
return -EINVAL;
}
+ rc = of_property_read_u32(pdev->dev.of_node, "amd,num-ctrl", &num_ctrl);
+ if (rc == -EINVAL) {
+ num_ctrl = NUM_CONTROLLERS;
+ } else if (rc) {
+ dev_err(&pdev->dev, "amd,num-ctrl: failed to read property: %d\n", rc);
+ return rc;
+ }
+
+ if (!num_ctrl || num_ctrl > NUM_CONTROLLERS)
+ return dev_err_probe(&pdev->dev, -EINVAL,
+ "amd,num-ctrl: invalid value %u\n", num_ctrl);
+
rp = rproc_get_by_phandle(r5_core_node->phandle);
if (!rp)
return -EPROBE_DEFER;
@@ -1003,6 +1018,7 @@ static int mc_probe(struct platform_device *pdev)
goto err_alloc;
}
+ priv->num_ctrl = num_ctrl;
priv->dev = &pdev->dev;
platform_set_drvdata(pdev, priv);
mutex_init(&priv->ept_lock);
@@ -1027,7 +1043,7 @@ static int mc_probe(struct platform_device *pdev)
priv->mcdi->r5_rproc = rp;
- for (i = 0; i < NUM_CONTROLLERS; i++)
+ for (i = 0; i < priv->num_ctrl; i++)
get_ddr_config(i, &priv->adec[ADEC_NUM * i], priv->mcdi);
rc = init_versalnet(priv);
--
2.44.4
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 12/12] EDAC/versalnet: Make get_ddr_config() return errors
2026-11-18 14:20 [PATCH v2 00/12] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
` (10 preceding siblings ...)
2026-11-18 14:20 ` [PATCH v2 11/12] EDAC/versalnet: use device tree property for number of controllers Shubhrajyoti Datta
@ 2026-11-18 14:20 ` Shubhrajyoti Datta
11 siblings, 0 replies; 14+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-18 14:20 UTC (permalink / raw)
To: linux-edac
Cc: git, shubhrajyoti.datta, michal.simek, Shubhrajyoti Datta,
Radhey Shyam Pandey, Krzysztof Kozlowski, Rob Herring,
Conor Dooley, Borislav Petkov, Tony Luck, linux-kernel,
devicetree
get_ddr_config() invokes cdx_mcdi_rpc() but ignores its return value,
and proceeds even if the cdx_mcdi_rpc returned errors. Also it
copies the data out of the response buffer without checking that
the firmware actually returned that much data.
Make get_ddr_config() return an int: propagate cdx_mcdi_rpc()'s
return code on failure, and return -EIO if the response is shorter
than expected. Update mc_probe() to check the return value and bail
out through the existing error path on failure.
Fixes: d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
---
Changes in v2:
New patch added to bailout if the cdx_mcdi_rpc fails.
drivers/edac/versalnet_edac.c | 21 +++++++++++++++------
1 file changed, 15 insertions(+), 6 deletions(-)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index c2025f5744cc..ac6e926a1918 100644
--- a/drivers/edac/versalnet_edac.c
+++ b/drivers/edac/versalnet_edac.c
@@ -587,7 +587,7 @@ static const struct cdx_mcdi_ops mcdi_ops = {
.mcdi_request = mcdi_request,
};
-static void get_ddr_config(u32 index, u32 *buffer, struct cdx_mcdi *amd_mcdi)
+static int get_ddr_config(u32 index, u32 *buffer, struct cdx_mcdi *amd_mcdi)
{
size_t outlen;
int ret;
@@ -599,9 +599,15 @@ static void get_ddr_config(u32 index, u32 *buffer, struct cdx_mcdi *amd_mcdi)
ret = cdx_mcdi_rpc(amd_mcdi, MC_CMD_EDAC_GET_DDR_CONFIG, inbuf, sizeof(inbuf),
outbuf, sizeof(outbuf), &outlen);
- if (!ret)
- memcpy(buffer, MCDI_PTR(outbuf, GET_DDR_CONFIG),
- (ADEC_NUM * 4));
+ if (ret)
+ return ret;
+
+ if (outlen < MC_CMD_GET_DDR_CONFIG_OFST + ADEC_NUM * 4)
+ return -EIO;
+
+ memcpy(buffer, MCDI_PTR(outbuf, GET_DDR_CONFIG), (ADEC_NUM * 4));
+
+ return 0;
}
static int setup_mcdi(struct mc_priv *mc_priv)
@@ -1043,8 +1049,11 @@ static int mc_probe(struct platform_device *pdev)
priv->mcdi->r5_rproc = rp;
- for (i = 0; i < priv->num_ctrl; i++)
- get_ddr_config(i, &priv->adec[ADEC_NUM * i], priv->mcdi);
+ for (i = 0; i < priv->num_ctrl; i++) {
+ rc = get_ddr_config(i, &priv->adec[ADEC_NUM * i], priv->mcdi);
+ if (rc)
+ goto err_init;
+ }
rc = init_versalnet(priv);
if (rc)
--
2.44.4
^ permalink raw reply related [flat|nested] 14+ messages in thread