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 2f46721335b66d3981fa1b644c8a035b513a89a5 Author: Marco Casaroli <[email protected]> AuthorDate: Fri Nov 27 16:06:51 2026 +0100 sched/signal, fs/vfs: Call no poll method in the critical section of pselect(). pselect() entered the critical section, set the temporary mask and called select() in it. select() calls poll(), so the poll method of each file ran in the critical section. A poll method can take a spinlock of its driver: pipecommon_poll() takes d_bflock. The pipe driver also holds d_bflock while it calls poll_notify(), and the nxsem_post() of a poll waiter enters the critical section. With SMP, one CPU held the critical section in pselect() and waited for d_bflock, and another CPU held d_bflock and waited for the critical section. Both CPUs stopped, and the other CPUs stopped at their next critical section. ppoll() has the correct order since e321f58a9ef: nx_poll() sets the mask after the poll setup and holds the critical section only from the test of the pending signals to the wait. Do the same for pselect(): add nx_select(), which gives the mask to nx_poll(), and call it from select() and pselect(). A NULL mask keeps the mask of the caller, as in ppoll(). Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- fs/vfs/fs_select.c | 87 +++++++++++++++++++++++++++++++++------------- include/nuttx/fs/fs.h | 16 +++++++++ sched/signal/sig_pselect.c | 72 ++++++++++++-------------------------- 3 files changed, 101 insertions(+), 74 deletions(-) diff --git a/fs/vfs/fs_select.c b/fs/vfs/fs_select.c index b5ae12af85f..773148fc62a 100644 --- a/fs/vfs/fs_select.c +++ b/fs/vfs/fs_select.c @@ -59,20 +59,11 @@ ****************************************************************************/ /**************************************************************************** - * Name: select + * Name: nx_select * * Description: - * select() allows a program to monitor multiple file descriptors, waiting - * until one or more of the file descriptors become "ready" for some class - * of I/O operation (e.g., input possible). A file descriptor is - * considered ready if it is possible to perform the corresponding I/O - * operation (e.g., read(2)) without blocking. - * - * NOTE: poll() is the fundamental API for performing such monitoring - * operation under NuttX. select() is provided for compatibility and - * is simply a layer of added logic on top of poll(). As such, select() - * is more wasteful of resources and poll() is the recommended API to be - * used. + * nx_select() is the internal form of select() and pselect(). It + * returns a negated errno value on failure and does not set errno. * * Input Parameters: * nfds - the maximum fd number (+1) of any descriptor in any of the @@ -82,16 +73,18 @@ * exceptfds - the set of descriptions to monitor for error events * timeout - Return at this time if none of these events of interest * occur. + * sigmask - Optional temporary signal mask, given to nx_poll(). * * Returned Value: * 0: Timer expired * >0: The number of bits set in the three sets of descriptors - * -1: An error occurred (errno will be set appropriately) + * <0: A negated errno value * ****************************************************************************/ -int select(int nfds, FAR fd_set *readfds, FAR fd_set *writefds, - FAR fd_set *exceptfds, FAR struct timeval *timeout) +int nx_select(int nfds, FAR fd_set *readfds, FAR fd_set *writefds, + FAR fd_set *exceptfds, FAR struct timeval *timeout, + FAR const sigset_t *sigmask) { struct pollfd *pollset = NULL; int fd; @@ -102,8 +95,7 @@ int select(int nfds, FAR fd_set *readfds, FAR fd_set *writefds, if (nfds < 0) { - set_errno(EINVAL); - return ERROR; + return -EINVAL; } /* Convert the timeout to milliseconds */ @@ -113,8 +105,7 @@ int select(int nfds, FAR fd_set *readfds, FAR fd_set *writefds, if (timeout->tv_sec < 0 || timeout->tv_usec < 0 || timeout->tv_usec >= USEC_PER_SEC) { - set_errno(EINVAL); - return ERROR; + return -EINVAL; } /* Calculate the timeout in milliseconds, clamping to INT_MAX @@ -143,8 +134,7 @@ int select(int nfds, FAR fd_set *readfds, FAR fd_set *writefds, if (nfds < 0 || nfds > FD_SETSIZE) { - set_errno(EINVAL); - return ERROR; + return -EINVAL; } /* How many pollfd structures do we need to allocate? */ @@ -174,8 +164,7 @@ int select(int nfds, FAR fd_set *readfds, FAR fd_set *writefds, if (pollset == NULL) { - set_errno(ENOMEM); - return ERROR; + return -ENOMEM; } memset(pollset, 0, npfds * sizeof(struct pollfd)); @@ -239,9 +228,11 @@ int select(int nfds, FAR fd_set *readfds, FAR fd_set *writefds, DEBUGASSERT(ndx == npfds); - /* Then let poll do all of the real work. */ + /* Then let poll do all of the real work. nx_poll() sets the mask only + * after the poll setup, outside of the critical section. + */ - ret = poll(pollset, npfds, msec); + ret = nx_poll(pollset, npfds, msec, sigmask); /* Now set up the return values */ @@ -318,3 +309,49 @@ int select(int nfds, FAR fd_set *readfds, FAR fd_set *writefds, return ret; } + +/**************************************************************************** + * Name: select + * + * Description: + * select() allows a program to monitor multiple file descriptors, waiting + * until one or more of the file descriptors become "ready" for some class + * of I/O operation (e.g., input possible). A file descriptor is + * considered ready if it is possible to perform the corresponding I/O + * operation (e.g., read(2)) without blocking. + * + * NOTE: poll() is the fundamental API for performing such monitoring + * operation under NuttX. select() is provided for compatibility and + * is simply a layer of added logic on top of poll(). As such, select() + * is more wasteful of resources and poll() is the recommended API to be + * used. + * + * Input Parameters: + * nfds - the maximum fd number (+1) of any descriptor in any of the + * three sets. + * readfds - the set of descriptions to monitor for read-ready events + * writefds - the set of descriptions to monitor for write-ready events + * exceptfds - the set of descriptions to monitor for error events + * timeout - Return at this time if none of these events of interest + * occur. + * + * Returned Value: + * 0: Timer expired + * >0: The number of bits set in the three sets of descriptors + * -1: An error occurred (errno will be set appropriately) + * + ****************************************************************************/ + +int select(int nfds, FAR fd_set *readfds, FAR fd_set *writefds, + FAR fd_set *exceptfds, FAR struct timeval *timeout) +{ + int ret = nx_select(nfds, readfds, writefds, exceptfds, timeout, NULL); + + if (ret < 0) + { + set_errno(-ret); + return ERROR; + } + + return ret; +} diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h index a5945294b82..1c785d06313 100644 --- a/include/nuttx/fs/fs.h +++ b/include/nuttx/fs/fs.h @@ -224,6 +224,8 @@ struct file; struct inode; struct stat; +struct timeval; +struct fd_set_s; struct statfs; struct pollfd_s; struct mtd_dev_s; @@ -1897,6 +1899,20 @@ int file_poll(FAR struct file *filep, FAR struct pollfd_s *fds, bool setup); int nx_poll(FAR struct pollfd *fds, nfds_t nfds, int timeout, FAR const sigset_t *sigmask); +/**************************************************************************** + * Name: nx_select + * + * Description: + * Internal select implementation for select() and pselect(). Returns the + * number of bits set in the three sets or a negated errno value. The + * sigmask is given to nx_poll(); NULL leaves the signal mask unchanged. + * + ****************************************************************************/ + +int nx_select(int nfds, FAR struct fd_set_s *readfds, + FAR struct fd_set_s *writefds, FAR struct fd_set_s *exceptfds, + FAR struct timeval *timeout, FAR const sigset_t *sigmask); + /**************************************************************************** * Name: file_fstat * diff --git a/sched/signal/sig_pselect.c b/sched/signal/sig_pselect.c index 926c91606c6..a80fafd8ba1 100644 --- a/sched/signal/sig_pselect.c +++ b/sched/signal/sig_pselect.c @@ -29,8 +29,9 @@ #include <sys/select.h> #include <sys/time.h> +#include <nuttx/fs/fs.h> + #include "sched/sched.h" -#include "signal/signal.h" /**************************************************************************** * Public Functions @@ -67,61 +68,34 @@ int pselect(int nfds, FAR fd_set *readfds, FAR fd_set *writefds, FAR fd_set *exceptfds, FAR const struct timespec *timeout, FAR const sigset_t *sigmask) { - FAR struct tcb_s *rtcb = this_task(); - sigset_t saved_sigprocmask; - irqstate_t flags; - int ret = ERROR; - - /* Several operations must be performed below: We must determine if any - * signal is pending and, if not, wait for the signal. Since signals can - * be posted from the interrupt level, there is a race condition that - * can only be eliminated by disabling interrupts! - */ + FAR struct timeval *timeval = NULL; + struct timeval timeval_buf; + sigset_t mask; + int ret; - flags = enter_critical_section(); + if (timeout) + { + timeval_buf.tv_sec = timeout->tv_sec; + timeval_buf.tv_usec = timeout->tv_nsec / NSEC_PER_USEC; + timeval = &timeval_buf; + } - /* Save a copy of the old sigprocmask and install the new (temporary) - * sigprocmask. Check if there is a pending signal corresponding to one - * of the signals that will be unblocked by the new sigprocmask. + /* A NULL mask keeps the caller's mask but still checks pending signals. + * nx_poll() sets the mask after the poll setup: the poll methods do not + * run in the critical section that closes the window to the wait. */ - if (nxsig_waitmask_set(sigmask, &saved_sigprocmask)) + if (sigmask == NULL) { - /* Dispatching one or more of the signals is sufficient to cause - * us to not wait. Restore the original sigprocmask. - */ - - nxsig_restoremask(rtcb, &saved_sigprocmask); - leave_critical_section(flags); - set_errno(EINTR); + mask = this_task()->sigprocmask; + sigmask = &mask; } - else - { - FAR struct timeval *timeval = NULL; - struct timeval timeval_buf; - /* And call select to do the real work */ - - if (timeout) - { - timeval_buf.tv_sec = timeout->tv_sec; - timeval_buf.tv_usec = (long)(timeout->tv_nsec / NSEC_PER_USEC); - timeval = &timeval_buf; - } - - ret = select(nfds, readfds, writefds, exceptfds, timeval); - - /* We are running again, restore the original sigprocmask */ - - nxsig_restoremask(rtcb, &saved_sigprocmask); - leave_critical_section(flags); - - /* Now, handle the (rare?) case where (a) a blocked signal was received - * while the task was suspended but (b) restoring the original - * sigprocmask will unblock the signal. - */ - - nxsig_unmask_pendingsignal(); + ret = nx_select(nfds, readfds, writefds, exceptfds, timeval, sigmask); + if (ret < 0) + { + set_errno(-ret); + return ERROR; } return ret;
