[PATCH v5 18/25] soundwire: amd: delay *sdw_ctx publication until after startup succeeds
From: Vijendar Mukunda
Date: Sat Nov 07 2026 - 11:02:47 EST
sdw_amd_probe() published *sdw_ctx immediately after probe_controller()
returned, before sdw_amd_startup() ran. Two problems follow from this:
1. If sdw_amd_startup() fails, the error was returned directly, leaving
*sdw_ctx pointing to an allocated context whose platform devices were
already registered. The caller has no way to distinguish this from a
genuine probe success, so the context and devices leak.
2. A concurrent ACP IRQ handler on another CPU can read the non-NULL
adata->sdw pointer between the sdw_amd_exit() teardown and the
*sdw_ctx = NULL assignment, and dereference a freed context.
Fix both by using a local variable for the context, running
sdw_amd_startup() against it, and only writing *sdw_ctx after startup
succeeds. On failure sdw_amd_exit() is called on the local pointer and
the function returns without touching *sdw_ctx, so callers never observe
a partially-initialised or freed context.
Fixes: ed5e8741b8db ("soundwire: amd: refactor amd soundwire manager device node creation")
Signed-off-by: Vijendar Mukunda <Vijendar.Mukunda@xxxxxxx>
---
drivers/soundwire/amd_init.c | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/drivers/soundwire/amd_init.c b/drivers/soundwire/amd_init.c
index 032697ea3590..ce40b1e5dbf3 100644
--- a/drivers/soundwire/amd_init.c
+++ b/drivers/soundwire/amd_init.c
@@ -176,11 +176,20 @@ static int sdw_amd_startup(struct sdw_amd_ctx *ctx)
int sdw_amd_probe(struct sdw_amd_res *res, struct sdw_amd_ctx **sdw_ctx)
{
- *sdw_ctx = sdw_amd_probe_controller(res);
- if (!*sdw_ctx)
+ struct sdw_amd_ctx *ctx;
+ int ret;
+
+ ctx = sdw_amd_probe_controller(res);
+ if (!ctx)
return -ENODEV;
- return sdw_amd_startup(*sdw_ctx);
+ ret = sdw_amd_startup(ctx);
+ if (ret) {
+ sdw_amd_exit(ctx);
+ return ret;
+ }
+ *sdw_ctx = ctx;
+ return 0;
}
EXPORT_SYMBOL_NS(sdw_amd_probe, "SOUNDWIRE_AMD_INIT");
--
2.53.0