[PATCH] coresight: syscfg: fix deadlock on device registration failure

yingchao <[email protected]>
Newsgroups org.infradead.lists.linux-arm-kernel,org.kernel.vger.linux-kernel
Message-ID <604ED754D4669F40+20260825014717.2346-1-dengyingchao@kylinsec.com.cn>
From: Yingchao Deng <[email protected]>

cscfg_create_device() calls put_device() while holding cscfg_mutex.  If
device_register() failed, put_device() drops the last reference and invokes
cscfg_dev_release(), which takes cscfg_mutex again, deadlocking.

Unlock cscfg_mutex before calling put_device().

Fixes: cfa5dbcdd7ae ("coresight: syscfg: Fix memleak on registration failure in cscfg_create_device")
Signed-off-by: Yingchao Deng <[email protected]>
---
 drivers/hwtracing/coresight/coresight-syscfg.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/hwtracing/coresight/coresight-syscfg.c b/drivers/hwtracing/coresight/coresight-syscfg.c
index 2bfdd7b45e49..d0e7e4720e46 100644
--- a/drivers/hwtracing/coresight/coresight-syscfg.c
+++ b/drivers/hwtracing/coresight/coresight-syscfg.c
@@ -1210,8 +1210,14 @@ static int cscfg_create_device(void)
 	dev->init_name = "cs_system_cfg";
 
 	err = device_register(dev);
-	if (err)
+	if (err) {
+		/* put_device() triggers cscfg_dev_release() which takes
+		 * cscfg_mutex, so drop the lock first to avoid deadlocking.
+		 */
+		mutex_unlock(&cscfg_mutex);
 		put_device(dev);
+		return err;
+	}
 
 create_dev_exit_unlock:
 	mutex_unlock(&cscfg_mutex);
-- 
2.33.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.