[PATCH v3 3/7] EDAC/versalnet: Wait for RPMsg endpoint initialization
From: Shubhrajyoti Datta
Date: Fri Nov 27 2026 - 11:39:16 EST
From: Hayden Rinn <haydenrinn@xxxxxxxxxxxxx>
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@xxxxxxxxxxxxx>
Co-developed-by: Shubhrajyoti Datta <shubhrajyoti.datta@xxxxxxx>
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@xxxxxxx>
---
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