Skip to content

Commit

Permalink
fs_poll: race condition protection when modifying revents value
Browse files Browse the repository at this point in the history
pollfd revents value is occasionally zeroed by race condition when
poll_notify() is called. This results to false timeout return value
from poll().

This behaviour was found out when application called tcdrain() before
calling uart poll().

Signed-off-by: Tero Salminen <[email protected]>
  • Loading branch information
t-salminen committed Nov 4, 2024
1 parent 41be91d commit 529b896
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions fs/vfs/fs_poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ void poll_notify(FAR struct pollfd **afds, int nfds, pollevent_t eventset)
{
int i;
FAR struct pollfd *fds;
irqstate_t flags;

DEBUGASSERT(afds != NULL && nfds >= 1);

Expand All @@ -283,6 +284,9 @@ void poll_notify(FAR struct pollfd **afds, int nfds, pollevent_t eventset)
fds = afds[i];
if (fds != NULL)
{
/* race condition protection when modifying fds->revents */
flags = enter_critical_section();

/* The error event must be set in fds->revents */

fds->revents |= eventset & (fds->events | POLLERR | POLLHUP);
Expand All @@ -293,6 +297,8 @@ void poll_notify(FAR struct pollfd **afds, int nfds, pollevent_t eventset)
fds->revents &= ~POLLOUT;
}

leave_critical_section(flags);

if ((fds->revents != 0 || (fds->events & POLLALWAYS) != 0) &&
fds->cb != NULL)
{
Expand Down

0 comments on commit 529b896

Please sign in to comment.