From: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
To: <linux-kernel@vger.kernel.org>, <linux-edac@vger.kernel.org>
Cc: <git@amd.com>, <shubhrajyoti.datta@gmail.com>,
Radhey Shyam Pandey <radhey.shyam.pandey@amd.com>,
Borislav Petkov <bp@alien8.de>, Tony Luck <tony.luck@intel.com>,
Sai Krishna Potthuri <sai.krishna.potthuri@amd.com>,
Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
Subject: [PATCH v3 3/7] EDAC/versalnet: Wait for RPMsg endpoint initialization
Date: Fri, 27 Nov 2026 21:59:12 +0530 [thread overview]
Message-ID: <20261127163853.3563991-4-shubhrajyoti.datta@amd.com> (raw)
In-Reply-To: <20261127163853.3563991-1-shubhrajyoti.datta@amd.com>
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
next prev parent reply other threads:[~2026-11-27 16:39 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2026-11-27 16:51 ` [PATCH v3 3/7] EDAC/versalnet: Wait for RPMsg endpoint initialization 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261127163853.3563991-4-shubhrajyoti.datta@amd.com \
--to=shubhrajyoti.datta@amd.com \
--cc=bp@alien8.de \
--cc=git@amd.com \
--cc=linux-edac@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=radhey.shyam.pandey@amd.com \
--cc=sai.krishna.potthuri@amd.com \
--cc=shubhrajyoti.datta@gmail.com \
--cc=tony.luck@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®