[PATCH v7 16/23] soundwire: amd: fix *sdw_ctx leak when sdw_amd_startup() fails

From: Vijendar Mukunda

Date: Wed Nov 18 2026 - 05:46:37 EST


sdw_amd_probe() published *sdw_ctx immediately after
sdw_amd_probe_controller() returned, before sdw_amd_startup() ran.
If sdw_amd_startup() failed, 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.

Fix this by publishing *sdw_ctx = ctx right after
sdw_amd_probe_controller() succeeds, then calling sdw_amd_startup(). On
failure reset *sdw_ctx = NULL and call sdw_amd_exit(ctx) before returning
the error. The caller always observes *sdw_ctx as either NULL (startup did
not complete) or a pointer to a fully-started, valid 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 | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/drivers/soundwire/amd_init.c b/drivers/soundwire/amd_init.c
index 8019a4faeae5..50e487e409a9 100644
--- a/drivers/soundwire/amd_init.c
+++ b/drivers/soundwire/amd_init.c
@@ -182,11 +182,21 @@ 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);
+ *sdw_ctx = ctx;
+ ret = sdw_amd_startup(ctx);
+ if (ret) {
+ *sdw_ctx = NULL;
+ sdw_amd_exit(ctx);
+ return ret;
+ }
+ return 0;
}
EXPORT_SYMBOL_NS(sdw_amd_probe, "SOUNDWIRE_AMD_INIT");

--
2.53.0