[PATCH 1/2] firmware: qcom: scm: Introduce new locking mechanism for SCM driver
From: Albert Esteve
Date: Mon Aug 24 2026 - 07:38:01 EST
From: Ninad Naik <quic_ninanaik@xxxxxxxxxxx>
With the existing global mutex lock, there is a possibility that it
could resulty into a deadlock when SMCInvoke and smc call go to
firmware in the same order and both wait on same WAITQ in firmware.
Replace the global mutex with a counting semaphore.
Signed-off-by: Murali Nalajala <quic_mnalajal@xxxxxxxxxxx>
Co-developed-by: Guru Das Srinagesh <quic_gurus@xxxxxxxxxxx>
Signed-off-by: Guru Das Srinagesh <quic_gurus@xxxxxxxxxxx>
Signed-off-by: Venkatakrishnaiah Pari <quic_vpari@xxxxxxxxxxx>
Signed-off-by: Jian Shu <quic_jianshu@xxxxxxxxxxx>
Signed-off-by: Ninad Naik <quic_ninanaik@xxxxxxxxxxx>
Signed-off-by: Albert Esteve <aesteve@xxxxxxxxxx>
---
drivers/firmware/qcom/qcom_scm-legacy.c | 8 ++------
drivers/firmware/qcom/qcom_scm-smc.c | 7 ++-----
drivers/firmware/qcom/qcom_scm.c | 4 ++++
drivers/firmware/qcom/qcom_scm.h | 3 +++
4 files changed, 11 insertions(+), 11 deletions(-)
diff --git a/drivers/firmware/qcom/qcom_scm-legacy.c b/drivers/firmware/qcom/qcom_scm-legacy.c
index 029e6d117cb8d..6afcbe6f7e5ed 100644
--- a/drivers/firmware/qcom/qcom_scm-legacy.c
+++ b/drivers/firmware/qcom/qcom_scm-legacy.c
@@ -6,7 +6,6 @@
#include <linux/slab.h>
#include <linux/io.h>
#include <linux/module.h>
-#include <linux/mutex.h>
#include <linux/errno.h>
#include <linux/err.h>
#include <linux/firmware/qcom/qcom_scm.h>
@@ -15,9 +14,6 @@
#include "qcom_scm.h"
-static DEFINE_MUTEX(qcom_scm_lock);
-
-
/**
* struct arm_smccc_args
* @args: The array of values used in registers in smc instruction
@@ -173,11 +169,11 @@ int scm_legacy_call(struct device *dev, const struct qcom_scm_desc *desc,
smc.args[1] = (unsigned long)&context_id;
smc.args[2] = cmd_phys;
- mutex_lock(&qcom_scm_lock);
+ down(&qcom_scm_sem_lock);
__scm_legacy_do(&smc, &smc_res);
if (smc_res.a0)
ret = qcom_scm_remap_error(smc_res.a0);
- mutex_unlock(&qcom_scm_lock);
+ up(&qcom_scm_sem_lock);
if (ret)
goto out;
diff --git a/drivers/firmware/qcom/qcom_scm-smc.c b/drivers/firmware/qcom/qcom_scm-smc.c
index 127365ab11fc2..1b51e0fb82924 100644
--- a/drivers/firmware/qcom/qcom_scm-smc.c
+++ b/drivers/firmware/qcom/qcom_scm-smc.c
@@ -6,7 +6,6 @@
#include <linux/io.h>
#include <linux/errno.h>
#include <linux/delay.h>
-#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/firmware/qcom/qcom_scm.h>
@@ -27,8 +26,6 @@ struct arm_smccc_args {
#define CREATE_TRACE_POINTS
#include "qcom_scm_trace.h"
-static DEFINE_MUTEX(qcom_scm_lock);
-
#define QCOM_SCM_EBUSY_WAIT_MS 30
#define QCOM_SCM_EBUSY_MAX_RETRY 20
@@ -135,11 +132,11 @@ static int __scm_smc_do(struct device *dev, struct arm_smccc_args *smc,
}
do {
- mutex_lock(&qcom_scm_lock);
+ down(&qcom_scm_sem_lock);
ret = __scm_smc_do_quirk_handle_waitq(dev, smc, res);
- mutex_unlock(&qcom_scm_lock);
+ up(&qcom_scm_sem_lock);
if (ret)
return ret;
diff --git a/drivers/firmware/qcom/qcom_scm.c b/drivers/firmware/qcom/qcom_scm.c
index 3eaa4c9ccf3cc..7a042273d6d5b 100644
--- a/drivers/firmware/qcom/qcom_scm.c
+++ b/drivers/firmware/qcom/qcom_scm.c
@@ -78,6 +78,8 @@ struct qcom_scm_mem_map_info {
__le64 mem_size;
};
+DEFINE_SEMAPHORE(qcom_scm_sem_lock, 1);
+
/**
* struct qcom_scm_qseecom_resp - QSEECOM SCM call response.
* @result: Result or status of the SCM call. See &enum qcom_scm_qseecom_result.
@@ -2893,6 +2895,8 @@ static int qcom_scm_probe(struct platform_device *pdev)
"Failed to request qcom-scm irq\n");
}
+ sema_init(&qcom_scm_sem_lock, scm->wq_cnt);
+
/*
* Paired with smp_load_acquire() in qcom_scm_is_available().
*
diff --git a/drivers/firmware/qcom/qcom_scm.h b/drivers/firmware/qcom/qcom_scm.h
index cf90a565fdfbd..06fdc5e56bea4 100644
--- a/drivers/firmware/qcom/qcom_scm.h
+++ b/drivers/firmware/qcom/qcom_scm.h
@@ -4,6 +4,8 @@
#ifndef __QCOM_SCM_INT_H
#define __QCOM_SCM_INT_H
+#include <linux/semaphore.h>
+
struct device;
struct qcom_tzmem_pool;
@@ -15,6 +17,7 @@ enum qcom_scm_convention {
};
extern enum qcom_scm_convention qcom_scm_convention;
+extern struct semaphore qcom_scm_sem_lock;
#define MAX_QCOM_SCM_ARGS 10
#define MAX_QCOM_SCM_RETS 3
--
2.55.0