This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 49d2559d550525fd7288d874fdfb2cb5fdeb5a9f
Author: likun17 <[email protected]>
AuthorDate: Sun Jan 4 17:06:19 2026 +0800

    drivers/sensors: Do not hold the upper lock across lower open/close.
    
    sensor_open() and sensor_close() call lower->ops->open() and
    lower->ops->close() with upper->lock held. For sensor_rpmsg these send
    broadcasts and may wait for a tx buffer, while the rptun thread that
    would release one is blocked in sensor_push_event() waiting for the
    same upper lock, so two cores deadlock under load.
    
    Take upper->lock only after lower->ops->open() / ->close() return; the
    lower half protects its own state. Release the lock only on the error
    paths that took it.
    
    Signed-off-by: likun17 <[email protected]>
---
 drivers/sensors/sensor.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/drivers/sensors/sensor.c b/drivers/sensors/sensor.c
index e11e369720f..76781acc0a1 100644
--- a/drivers/sensors/sensor.c
+++ b/drivers/sensors/sensor.c
@@ -762,14 +762,16 @@ static int sensor_open(FAR struct file *filep)
   FAR struct sensor_user_s *user;
   int ret = 0;
 
-  nxrmutex_lock(&upper->lock);
   user = kmm_zalloc(sizeof(struct sensor_user_s));
   if (user == NULL)
     {
-      ret = -ENOMEM;
-      goto errout_with_lock;
+      return -ENOMEM;
     }
 
+  /* The lower ops have upper lock protection, so no additional locking
+   * protection is needed.
+   */
+
   if ((filep->f_oflags & O_DIRECT) == 0 && lower->ops->open)
     {
       ret = lower->ops->open(lower, filep);
@@ -779,6 +781,8 @@ static int sensor_open(FAR struct file *filep)
         }
     }
 
+  nxrmutex_lock(&upper->lock);
+
   /* Using the O_DIRECT flag will prevent cross-core operations,
    * allowing for direct I/O operations.
    */
@@ -836,9 +840,12 @@ static int sensor_open(FAR struct file *filep)
   sensor_pollnotify(upper, POLLPRI, SENSOR_ROLE_WR);
 
   filep->f_priv = user;
-  goto errout_with_lock;
+
+  nxrmutex_unlock(&upper->lock);
+  return OK;
 
 errout_with_open:
+  nxrmutex_unlock(&upper->lock);
   if (lower->ops->close)
     {
       lower->ops->close(lower, filep);
@@ -846,8 +853,6 @@ errout_with_open:
 
 errout_with_user:
   kmm_free(user);
-errout_with_lock:
-  nxrmutex_unlock(&upper->lock);
   return ret;
 }
 
@@ -863,13 +868,17 @@ static int sensor_close(FAR struct file *filep)
   sensor_update_latency(filep, upper, user, UINT32_MAX);
   sensor_update_wakeup(filep, upper, user, false);
 
-  nxrmutex_lock(&upper->lock);
+  /* The lower ops have upper lock protection, so no additional locking
+   * protection is needed.
+   */
 
   if ((filep->f_oflags & O_DIRECT) == 0 && lower->ops->close)
     {
       lower->ops->close(lower, filep);
     }
 
+  nxrmutex_lock(&upper->lock);
+
   /* Using the O_DIRECT flag will prevent cross-core operations,
    * allowing for direct I/O operations.
    */

Reply via email to