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

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

commit 9a6f9b55e5a30b97b2dbbb8b3b77947954c5eac6
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Nov 27 16:06:51 2026 +0100

    fs/vfs/epoll: Call no poll method in the critical section of epoll_pwait().
    
    Since 457531ef741, epoll_pwait() enters the critical section before
    epoll_setup() and leaves it after epoll_teardown().  These call the poll
    method of each file in the set, and a poll method can take a spinlock of
    its driver.  With a pipe in the set, this gives the same SMP deadlock as
    pselect(): one CPU holds the critical section and waits for d_bflock in
    pipecommon_poll(), and another CPU holds d_bflock and waits for the
    critical section in the nxsem_post() of poll_notify().
    
    Use the order of nx_poll(): set up the poll first, then enter the critical
    section, set the mask and test the pending signals, wait, restore the mask
    and leave the section, then tear down.  The retry after a wakeup with no
    event does the same steps again.  The old mask is saved only the first
    time: in a system call, the temporary mask stays set after
    nxsig_waitmask_restore(), and a second save would keep the temporary mask
    as the mask to restore.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 fs/vfs/fs_epoll.c | 44 +++++++++++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 17 deletions(-)

diff --git a/fs/vfs/fs_epoll.c b/fs/vfs/fs_epoll.c
index 16acead23fb..3e121350d24 100644
--- a/fs/vfs/fs_epoll.c
+++ b/fs/vfs/fs_epoll.c
@@ -1004,6 +1004,8 @@ int epoll_pwait(int epfd, FAR struct epoll_event *evs,
   FAR epoll_head_t *eph;
 #ifndef CONFIG_DISABLE_ALL_SIGNALS
   sigset_t oldsigmask;
+  sigset_t tmpsigmask;
+  FAR sigset_t *saved = &oldsigmask;
   irqstate_t flags;
 #endif
   int ret;
@@ -1026,26 +1028,31 @@ int epoll_pwait(int epfd, FAR struct epoll_event *evs,
       goto out;
     }
 
-  /* Set the mask for the whole wait.  Without signals, there is no mask to
-   * set.  A pending signal that the mask unblocks stops the call at once.
-   * The critical section closes the window from this test to the wait.
+retry:
+  ret = epoll_setup(eph);
+  if (ret < 0)
+    {
+      goto err;
+    }
+
+  /* Set the mask after the setup, as nx_poll() does: the poll methods can
+   * take a spinlock of their driver, so they must not run in the critical
+   * section.  The section closes the window from the test of the pending
+   * signals to the wait.  Without signals, there is no mask to set.  Save
+   * the old mask only once: in a system call, the temporary mask stays set
+   * after nxsig_waitmask_restore().
    */
 
 #ifndef CONFIG_DISABLE_ALL_SIGNALS
   flags = enter_critical_section();
-  if (nxsig_waitmask_set(sigmask, &oldsigmask))
+  if (nxsig_waitmask_set(sigmask, saved))
     {
       ret = -EINTR;
-      goto err;
+      goto err_with_mask;
     }
-#endif
 
-retry:
-  ret = epoll_setup(eph);
-  if (ret < 0)
-    {
-      goto err;
-    }
+  saved = &tmpsigmask;
+#endif
 
   /* Wait the poll ready */
 
@@ -1062,6 +1069,11 @@ retry:
       ret = nxsem_wait(&eph->sem);
     }
 
+#ifndef CONFIG_DISABLE_ALL_SIGNALS
+  nxsig_waitmask_restore(&oldsigmask);
+  leave_critical_section(flags);
+#endif
+
   if (ret < 0 && ret != -ETIMEDOUT)
     {
       goto err;
@@ -1078,19 +1090,17 @@ retry:
       ret = num;
     }
 
-#ifndef CONFIG_DISABLE_ALL_SIGNALS
-  nxsig_waitmask_restore(&oldsigmask);
-  leave_critical_section(flags);
-#endif
   epoll_filep_close(filep);
 
   return ret;
 
-err:
 #ifndef CONFIG_DISABLE_ALL_SIGNALS
+err_with_mask:
   nxsig_waitmask_restore(&oldsigmask);
   leave_critical_section(flags);
 #endif
+
+err:
   epoll_filep_close(filep);
   set_errno(-ret);
 out:

Reply via email to