From d75952f9939d9be9d9960dacaee89e226b55df52 Mon Sep 17 00:00:00 2001 From: Robin Gong Date: Sat, 8 Feb 2020 03:50:27 +0800 Subject: [PATCH] LF-857-1: char: imx_amp: imx_sema4: enlarge lock protect Currently, there is only spin lock added in imx_sema4_mutex_lock(),but sema4 driver assume any mutex should be handled one by one and complain when mutex locked once again before unlocked. Hence, if another lock happens indeed after the last imx_sema4_mutex_lock(),the complains "imx_sema4_ mutex_lock 135 already locked" would come out over again and again as below. So delay lock released into imx_sema4_mutex_unlock() to elimate such race contition. Thread1: Thread2: .... .... ------------------------------------------ imx_sema4_mutex_lock ---->grabbed->|imx_sema4_mutex_lock | |"imx_sema4_mutex_lock 135 already locked"| ------------------------------------------ imx_sema4_mutex_unlock imx_sema4_mutex_unlock .... ..... Signed-off-by: Robin Gong Reviewed-by: Anson Huang Signed-off-by: Dong Aisheng (cherry picked from commit 362fdbb91f1c2e386adc6147c07f714be6134907) --- drivers/char/imx_amp/imx_sema4.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/drivers/char/imx_amp/imx_sema4.c b/drivers/char/imx_amp/imx_sema4.c index 412202f11cbb..46a178de41f5 100644 --- a/drivers/char/imx_amp/imx_sema4.c +++ b/drivers/char/imx_amp/imx_sema4.c @@ -23,6 +23,8 @@ #include static struct imx_sema4_mutex_device *imx6_sema4; +static unsigned long sema4_flags; + /*! * \brief mutex create function. @@ -203,15 +205,11 @@ EXPORT_SYMBOL(imx_sema4_mutex_trylock); int imx_sema4_mutex_lock(struct imx_sema4_mutex *mutex_ptr) { int ret = 0; - unsigned long flags; - spin_lock_irqsave(&imx6_sema4->lock, flags); + spin_lock_irqsave(&imx6_sema4->lock, sema4_flags); ret = _imx_sema4_mutex_lock(mutex_ptr); - spin_unlock_irqrestore(&imx6_sema4->lock, flags); while (-EBUSY == ret) { - spin_lock_irqsave(&imx6_sema4->lock, flags); ret = _imx_sema4_mutex_lock(mutex_ptr); - spin_unlock_irqrestore(&imx6_sema4->lock, flags); if (ret == 0) break; } @@ -259,6 +257,7 @@ int imx_sema4_mutex_unlock(struct imx_sema4_mutex *mutex_ptr) if (mutex_ptr->gate_val == SEMA4_A9_LOCK) pr_err("%d ERROR, failed to unlock the mutex.\n", __LINE__); + spin_unlock_irqrestore(&imx6_sema4->lock, sema4_flags); out: return ret; }