* [PATCH v3 0/7] EDAC/versalnet: Fix error handling, teardown, and robustness
@ 2026-11-27 16:29 Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 1/7] EDAC/versalnet: Add bounds validation in rpmsg_cb() Shubhrajyoti Datta
` (6 more replies)
0 siblings, 7 replies; 9+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-27 16:29 UTC (permalink / raw)
To: linux-kernel, linux-edac
Cc: git, shubhrajyoti.datta, Radhey Shyam Pandey, Borislav Petkov,
Tony Luck, Sai Krishna Potthuri, Shubhrajyoti Datta
This series addresses several correctness and robustness issues in
the VersalNet EDAC driver, including NULL pointer dereferences,
resource lifetime problems, shutdown handling, and probe/remove
path cleanup.
The main fixes are:
* Robustness fixes
- Add bounds validation in rpmsg_cb().
* Probe and initialization cleanup
- Move platform_set_drvdata() from init_one_mc() to mc_probe().
- Reorder probe flow to initialize MCDI before RPMsg registration.
- Remove unused pdev parameters from internal initialization
functions.
* Resource management fixes
- Fix error handling in init_one_mc() to avoid a potential
double-free by reordering device_register() and edac_mc_alloc().
Changes in v3:
If it is a split message tben discard both the messages
Updated the description to dtype
Update the memory allocation in edac_mc_alloc
Update the kfree sequence
Squash the "EDAC/versalnet: Wait for RPMsg endpoint initialization"
Add reviewed by tag
use sizeof outbuf
Add a print for the failure
Update the commit message
update the numctrl check
implement get_dtype
Changes in v2:
Add the codeveloped tag and remove the non standard signature
fix the name stack warning
Use a macro for channel name
Add reviewed by tag
Sashiko comment
"This is a pre-existing issue, but are offset and length validated before
being used as indices?"
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?".
Patch addition for the rpmsg endpoint race
Update the labels
Address 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?"
Patch addition for the rpmsg drvdata race
Add reviewed-by tag
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/
Adding the device tree parameter was suggested in the review in v1
https://lore.kernel.org/all/34ffba5a-8e9a-49b9-9e36-c31d0e4369f8@amd.com/
New patch added to bailout if the cdx_mcdi_rpc fails.
Hayden Rinn (1):
EDAC/versalnet: Wait for RPMsg endpoint initialization
Shubhrajyoti Datta (6):
EDAC/versalnet: Add bounds validation in rpmsg_cb()
EDAC/versalnet: Make dwidth per-controller
EDAC/versalnet: Guard against NULL drvdata in rpmsg_cb()
EDAC/versalnet: Make get_ddr_config() return errors
EDAC/versalnet: fix the remove controller crash
EDAC/versalnet: Move platform_set_drvdata() to mc_probe()
drivers/edac/versalnet_edac.c | 257 ++++++++++++++++++++++++++--------
1 file changed, 201 insertions(+), 56 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v3 1/7] EDAC/versalnet: Add bounds validation in rpmsg_cb()
2026-11-27 16:29 [PATCH v3 0/7] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
@ 2026-11-27 16:29 ` Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 2/7] EDAC/versalnet: Make dwidth per-controller Shubhrajyoti Datta
` (5 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-27 16:29 UTC (permalink / raw)
To: linux-kernel, linux-edac
Cc: git, shubhrajyoti.datta, Radhey Shyam Pandey, Borislav Petkov,
Tony Luck, Sai Krishna Potthuri, Shubhrajyoti Datta
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 v3:
If it is a split message tben discard both the messages
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 | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index 2019a0f102c6..405d994303c7 100644
--- a/drivers/edac/versalnet_edac.c
+++ b/drivers/edac/versalnet_edac.c
@@ -607,6 +607,22 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,
length = result[MSG_ERR_LENGTH];
offset = result[MSG_ERR_OFFSET];
+ /*
+ * For a message that is part of a split sequence, offset must
+ * continue exactly where the previously accumulated data
+ * (mc_priv->part_len) left off; otherwise either a fragment was
+ * dropped or this is an unrelated/replayed message. Reject it and
+ * reset part_len so a bad fragment cannot desync, or be used to
+ * smuggle data into, subsequent split messages.
+ */
+ if (length > REG_MAX || offset > REG_MAX - length ||
+ result[TOTAL_ERR_LENGTH] > REG_MAX || offset != mc_priv->part_len) {
+ dev_warn_ratelimited(&rpdev->dev,
+ "Rejecting the faulty message\n");
+ mc_priv->part_len = 0;
+ 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.34.1
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v3 2/7] EDAC/versalnet: Make dwidth per-controller
2026-11-27 16:29 [PATCH v3 0/7] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 1/7] EDAC/versalnet: Add bounds validation in rpmsg_cb() Shubhrajyoti Datta
@ 2026-11-27 16:29 ` Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 3/7] EDAC/versalnet: Wait for RPMsg endpoint initialization Shubhrajyoti Datta
` (4 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-27 16:29 UTC (permalink / raw)
To: linux-kernel, linux-edac
Cc: git, shubhrajyoti.datta, Radhey Shyam Pandey, Borislav Petkov,
Tony Luck, Sai Krishna Potthuri, Shubhrajyoti Datta
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 dtype.
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 v3:
Updated the description to dtype
Update the memory allocation in edac_mc_alloc as pvt info is not used
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 | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index 405d994303c7..8decaba19048 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;
}
}
}
@@ -846,7 +846,7 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i
if (rc)
goto err_put_dev;
- mci = edac_mc_alloc(i, ARRAY_SIZE(layers), layers, sizeof(struct mc_priv));
+ mci = edac_mc_alloc(i, ARRAY_SIZE(layers), layers, 0);
if (!mci) {
edac_printk(KERN_ERR, EDAC_MC, "Failed memory allocation for MC%d\n", i);
rc = -ENOMEM;
@@ -854,7 +854,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) {
@@ -863,7 +863,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.34.1
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v3 3/7] EDAC/versalnet: Wait for RPMsg endpoint initialization
2026-11-27 16:29 [PATCH v3 0/7] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 1/7] EDAC/versalnet: Add bounds validation in rpmsg_cb() Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 2/7] EDAC/versalnet: Make dwidth per-controller Shubhrajyoti Datta
@ 2026-11-27 16:29 ` Shubhrajyoti Datta
2026-11-27 16:51 ` Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 4/7] EDAC/versalnet: Guard against NULL drvdata in rpmsg_cb() Shubhrajyoti Datta
` (3 subsequent siblings)
6 siblings, 1 reply; 9+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-27 16:29 UTC (permalink / raw)
To: linux-kernel, linux-edac
Cc: git, shubhrajyoti.datta, Radhey Shyam Pandey, Borislav Petkov,
Tony Luck, Sai Krishna Potthuri, Shubhrajyoti Datta
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 if the channel is down.
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: 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 v3:
Update the kfree sequence
Squash the "EDAC/versalnet: Wait for RPMsg endpoint initialization"
Changes in v2:
Patch addition for the rpmsg endpoint race
drivers/edac/versalnet_edac.c | 140 ++++++++++++++++++++++++++++------
1 file changed, 115 insertions(+), 25 deletions(-)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index 8decaba19048..c43104a5d9f0 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,15 @@ 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.
+ * @ready: Flag set once VersalNet edac is initialised.
*/
struct mc_priv {
char message[256];
@@ -159,7 +169,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 +522,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 = 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,9 +537,27 @@ 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;
+ 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");
+ return;
+ }
+
send_buf = kzalloc(hdr_len + sdu_len, GFP_KERNEL);
if (!send_buf)
return;
@@ -559,7 +597,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(sizeof(*amd_mcdi), GFP_KERNEL);
if (!amd_mcdi)
@@ -572,12 +610,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;
}
@@ -601,6 +635,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];
@@ -730,12 +776,6 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,
return 0;
}
-static struct rpmsg_device_id amd_rpmsg_id_table[] = {
- { .name = RPMSG_CHAN_NAME },
- { },
-};
-MODULE_DEVICE_TABLE(rpmsg, amd_rpmsg_id_table);
-
static int rpmsg_probe(struct rpmsg_device *rpdev)
{
struct rpmsg_channel_info chinfo = {
@@ -743,25 +783,56 @@ static int rpmsg_probe(struct rpmsg_device *rpdev)
.dst = rpdev->dst,
.name = 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;
- rpmsg_destroy_ept(mc_priv->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(ept);
dev_set_drvdata(&rpdev->dev, NULL);
}
@@ -906,7 +977,7 @@ static int mc_probe(struct platform_device *pdev)
struct device_node *r5_core_node;
struct mc_priv *priv;
struct rproc *rp;
- int rc;
+ int rc, i;
r5_core_node = of_parse_phandle(pdev->dev.of_node, "amd,rproc", 0);
if (!r5_core_node) {
@@ -930,33 +1001,52 @@ 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 = 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;
+ goto err_init;
+ }
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;
+ /*
+ * 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:
+ 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.34.1
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v3 4/7] EDAC/versalnet: Guard against NULL drvdata in rpmsg_cb()
2026-11-27 16:29 [PATCH v3 0/7] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
` (2 preceding siblings ...)
2026-11-27 16:29 ` [PATCH v3 3/7] EDAC/versalnet: Wait for RPMsg endpoint initialization Shubhrajyoti Datta
@ 2026-11-27 16:29 ` Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 5/7] EDAC/versalnet: Make get_ddr_config() return errors Shubhrajyoti Datta
` (2 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-27 16:29 UTC (permalink / raw)
To: linux-kernel, linux-edac
Cc: git, shubhrajyoti.datta, Radhey Shyam Pandey, Borislav Petkov,
Tony Luck, Sai Krishna Potthuri, Shubhrajyoti Datta
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.
Reviewed-by: Radhey Shyam Pandey <radhey.shyam.pandey@amd.com>
Fixes: d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
---
Changes in v3:
Add reviewed by tag
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 c43104a5d9f0..8c3f8733c049 100644
--- a/drivers/edac/versalnet_edac.c
+++ b/drivers/edac/versalnet_edac.c
@@ -631,6 +631,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.34.1
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v3 5/7] EDAC/versalnet: Make get_ddr_config() return errors
2026-11-27 16:29 [PATCH v3 0/7] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
` (3 preceding siblings ...)
2026-11-27 16:29 ` [PATCH v3 4/7] EDAC/versalnet: Guard against NULL drvdata in rpmsg_cb() Shubhrajyoti Datta
@ 2026-11-27 16:29 ` Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 6/7] EDAC/versalnet: fix the remove controller crash Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 7/7] EDAC/versalnet: Move platform_set_drvdata() to mc_probe() Shubhrajyoti Datta
6 siblings, 0 replies; 9+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-27 16:29 UTC (permalink / raw)
To: linux-kernel, linux-edac
Cc: git, shubhrajyoti.datta, Radhey Shyam Pandey, Borislav Petkov,
Tony Luck, Sai Krishna Potthuri, Shubhrajyoti Datta
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 v3:
use sizeof outbuf
Add a print for the failure
Reorder the patch
Changes in v2:
New patch added to bailout if the cdx_mcdi_rpc fails.
drivers/edac/versalnet_edac.c | 23 +++++++++++++++++------
1 file changed, 17 insertions(+), 6 deletions(-)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index 8c3f8733c049..7d8b8f2ecaea 100644
--- a/drivers/edac/versalnet_edac.c
+++ b/drivers/edac/versalnet_edac.c
@@ -577,7 +577,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;
@@ -589,9 +589,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 < sizeof(outbuf))
+ return -EIO;
+
+ memcpy(buffer, MCDI_PTR(outbuf, GET_DDR_CONFIG), (ADEC_NUM * 4));
+
+ return 0;
}
static int setup_mcdi(struct mc_priv *mc_priv)
@@ -1030,8 +1036,13 @@ 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);
+ for (i = 0; i < NUM_CONTROLLERS; i++) {
+ rc = get_ddr_config(i, &priv->adec[ADEC_NUM * i], priv->mcdi);
+ if (rc) {
+ dev_err(&pdev->dev, "Failed to get DDR configuration\n");
+ goto err_init;
+ }
+ }
rc = init_versalnet(priv, pdev);
if (rc)
--
2.34.1
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v3 6/7] EDAC/versalnet: fix the remove controller crash
2026-11-27 16:29 [PATCH v3 0/7] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
` (4 preceding siblings ...)
2026-11-27 16:29 ` [PATCH v3 5/7] EDAC/versalnet: Make get_ddr_config() return errors Shubhrajyoti Datta
@ 2026-11-27 16:29 ` Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 7/7] EDAC/versalnet: Move platform_set_drvdata() to mc_probe() Shubhrajyoti Datta
6 siblings, 0 replies; 9+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-27 16:29 UTC (permalink / raw)
To: linux-kernel, linux-edac
Cc: git, shubhrajyoti.datta, Radhey Shyam Pandey, Borislav Petkov,
Tony Luck, Sai Krishna Potthuri, Shubhrajyoti Datta
Use the number of enabled controllers detected instead of registering
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 will avoid we attempting to remove a controller that is not
enabled.
Fixes: d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
---
Changes in v3:
Implement the detection instead of reading from device tree
update the numctrl check
implement get_dtype
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 | 49 +++++++++++++++++++++++++----------
1 file changed, 36 insertions(+), 13 deletions(-)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index 7d8b8f2ecaea..8a5da8fb3403 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.
@@ -161,6 +162,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];
@@ -449,7 +451,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];
@@ -720,7 +722,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);
@@ -731,7 +733,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);
@@ -874,18 +876,12 @@ 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 enum dev_type get_dtype(struct mc_priv *priv, int i)
{
- u32 num_chans, rank, dwidth, config;
- struct edac_mc_layer layers[2];
- struct mem_ctl_info *mci;
- struct device *dev;
+ u32 dwidth, config;
enum dev_type dt;
- int rc;
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) {
@@ -902,6 +898,23 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i
dt = DEV_UNKNOWN;
}
+ return dt;
+}
+
+static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i)
+{
+ struct edac_mc_layer layers[2];
+ u32 num_chans, rank, config;
+ struct mem_ctl_info *mci;
+ struct device *dev;
+ enum dev_type dt;
+ int rc;
+
+ config = priv->adec[CONF + i * ADEC_NUM];
+ num_chans = FIELD_GET(MC5_NUM_CHANS_MASK, config);
+ rank = 1 << FIELD_GET(MC5_RANK_MASK, config);
+
+ dt = get_dtype(priv, i);
if (dt == DEV_UNKNOWN)
return 0;
@@ -966,7 +979,7 @@ static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
{
int rc, i;
- for (i = 0; i < NUM_CONTROLLERS; i++) {
+ for (i = 0; i < priv->num_ctrl; i++) {
rc = init_one_mc(priv, pdev, i);
if (rc) {
while (i--)
@@ -980,7 +993,7 @@ static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
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);
}
@@ -989,6 +1002,7 @@ static int mc_probe(struct platform_device *pdev)
struct device_node *r5_core_node;
struct mc_priv *priv;
struct rproc *rp;
+ enum dev_type dt;
int rc, i;
r5_core_node = of_parse_phandle(pdev->dev.of_node, "amd,rproc", 0);
@@ -1042,6 +1056,15 @@ static int mc_probe(struct platform_device *pdev)
dev_err(&pdev->dev, "Failed to get DDR configuration\n");
goto err_init;
}
+ dt = get_dtype(priv, i);
+ if (dt != DEV_UNKNOWN)
+ priv->num_ctrl++;
+ }
+
+ if (!priv->num_ctrl) {
+ dev_err(&pdev->dev, "No DDR controllers enabled\n");
+ rc = -ENODEV;
+ goto err_init;
}
rc = init_versalnet(priv, pdev);
--
2.34.1
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v3 7/7] EDAC/versalnet: Move platform_set_drvdata() to mc_probe()
2026-11-27 16:29 [PATCH v3 0/7] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
` (5 preceding siblings ...)
2026-11-27 16:29 ` [PATCH v3 6/7] EDAC/versalnet: fix the remove controller crash Shubhrajyoti Datta
@ 2026-11-27 16:29 ` Shubhrajyoti Datta
6 siblings, 0 replies; 9+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-27 16:29 UTC (permalink / raw)
To: linux-kernel, linux-edac
Cc: git, shubhrajyoti.datta, Radhey Shyam Pandey, Borislav Petkov,
Tony Luck, Sai Krishna Potthuri, Shubhrajyoti Datta
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>
---
(no changes since v2)
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 8a5da8fb3403..143be2cfe6a8 100644
--- a/drivers/edac/versalnet_edac.c
+++ b/drivers/edac/versalnet_edac.c
@@ -901,7 +901,7 @@ static enum dev_type get_dtype(struct mc_priv *priv, int i)
return dt;
}
-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)
{
struct edac_mc_layer layers[2];
u32 num_chans, rank, config;
@@ -961,8 +961,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:
@@ -975,12 +973,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 < priv->num_ctrl; i++) {
- rc = init_one_mc(priv, pdev, i);
+ rc = init_one_mc(priv, i);
if (rc) {
while (i--)
remove_one_mc(priv, i);
@@ -1028,6 +1026,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;
@@ -1067,7 +1066,7 @@ static int mc_probe(struct platform_device *pdev)
goto err_init;
}
- rc = init_versalnet(priv, pdev);
+ rc = init_versalnet(priv);
if (rc)
goto err_init;
--
2.34.1
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v3 3/7] EDAC/versalnet: Wait for RPMsg endpoint initialization
2026-11-27 16:29 ` [PATCH v3 3/7] EDAC/versalnet: Wait for RPMsg endpoint initialization Shubhrajyoti Datta
@ 2026-11-27 16:51 ` Shubhrajyoti Datta
0 siblings, 0 replies; 9+ messages in thread
From: Shubhrajyoti Datta @ 2026-11-27 16:51 UTC (permalink / raw)
To: Shubhrajyoti Datta
Cc: linux-kernel, linux-edac, git, Radhey Shyam Pandey,
Borislav Petkov, Tony Luck, Sai Krishna Potthuri, haydenrinn
On Fri, Nov 27, 2026 at 10:09 PM Shubhrajyoti Datta
<shubhrajyoti.datta@amd.com> wrote:
>
> 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 if the channel is down.
>
> 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: 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 v3:
> Update the kfree sequence
> Squash the "EDAC/versalnet: Wait for RPMsg endpoint initialization"
>
> Changes in v2:
> Patch addition for the rpmsg endpoint race
>
> drivers/edac/versalnet_edac.c | 140 ++++++++++++++++++++++++++++------
> 1 file changed, 115 insertions(+), 25 deletions(-)
>
> diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
> index 8decaba19048..c43104a5d9f0 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,15 @@ 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.
> + * @ready: Flag set once VersalNet edac is initialised.
> */
> struct mc_priv {
> char message[256];
> @@ -159,7 +169,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 +522,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 = 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,9 +537,27 @@ 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;
>
> + 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");
> + return;
> + }
> +
> send_buf = kzalloc(hdr_len + sdu_len, GFP_KERNEL);
> if (!send_buf)
> return;
> @@ -559,7 +597,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(sizeof(*amd_mcdi), GFP_KERNEL);
> if (!amd_mcdi)
> @@ -572,12 +610,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;
> }
>
> @@ -601,6 +635,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];
> @@ -730,12 +776,6 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,
> return 0;
> }
>
> -static struct rpmsg_device_id amd_rpmsg_id_table[] = {
> - { .name = RPMSG_CHAN_NAME },
> - { },
> -};
> -MODULE_DEVICE_TABLE(rpmsg, amd_rpmsg_id_table);
> -
> static int rpmsg_probe(struct rpmsg_device *rpdev)
> {
> struct rpmsg_channel_info chinfo = {
> @@ -743,25 +783,56 @@ static int rpmsg_probe(struct rpmsg_device *rpdev)
> .dst = rpdev->dst,
> .name = 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;
>
> - rpmsg_destroy_ept(mc_priv->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(ept);
> dev_set_drvdata(&rpdev->dev, NULL);
> }
>
> @@ -906,7 +977,7 @@ static int mc_probe(struct platform_device *pdev)
> struct device_node *r5_core_node;
> struct mc_priv *priv;
> struct rproc *rp;
> - int rc;
> + int rc, i;
>
> r5_core_node = of_parse_phandle(pdev->dev.of_node, "amd,rproc", 0);
> if (!r5_core_node) {
> @@ -930,33 +1001,52 @@ 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 = 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;
> + goto err_init;
> + }
>
> 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;
>
> + /*
> + * 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:
> + 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.34.1
>
+ Hayden
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-11-27 16:51 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 16:29 [PATCH v3 0/7] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 1/7] EDAC/versalnet: Add bounds validation in rpmsg_cb() Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 2/7] EDAC/versalnet: Make dwidth per-controller Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 3/7] EDAC/versalnet: Wait for RPMsg endpoint initialization Shubhrajyoti Datta
2026-11-27 16:51 ` Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 4/7] EDAC/versalnet: Guard against NULL drvdata in rpmsg_cb() Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 5/7] EDAC/versalnet: Make get_ddr_config() return errors Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 6/7] EDAC/versalnet: fix the remove controller crash Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 7/7] EDAC/versalnet: Move platform_set_drvdata() to mc_probe() Shubhrajyoti Datta
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®