Skip to content

!fs/poll: Give struct pollfd the POSIX layout. - #20796

Merged
acassis merged 3 commits into
apache:masterfrom
casaroli:pollfd-posix
Oct 27, 2026
Merged

acassis merged 3 commits into
apache:masterfrom
casaroli:pollfd-posix

Conversation

@casaroli

@casaroli casaroli commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

struct pollfd had 32-bit events and revents and three kernel fields (arg, cb, priv): 40 bytes on a 64-bit target and 24 bytes on a 32-bit target. POSIX gives it int fd, short events and short revents (8 bytes): <poll.h> in IEEE Std 1003.1-2017 (Revision of IEEE Std 1003.1-2008) / The Open Group Base Specifications Issue 7, 2018 edition. A program or library that builds its own array with the standard layout and calls poll() gets its neighbouring entries overwritten. The Rust libc crate, for example, already uses the 8-byte layout for NuttX.

In flat and protected builds, poll() also used the caller's array directly: it wrote a semaphore address and a callback pointer into it, and poll_notify() called that callback, often from an interrupt handler. In a protected build the array is user memory, so user code could change the function pointer that the kernel calls. Two threads could not poll the same array (REVISIT comment in poll.h).

This PR has three commits:

  1. fs/poll: Use struct pollfd_s for the kernel side of poll. A new kernel header, include/nuttx/poll.h, holds struct pollfd_s (the same fields as the old struct pollfd), pollevent_t, pollcb_t, POLLALWAYS, poll_notify() and poll_default_cb(). include/nuttx/fs/fs.h includes it. The poll methods of drivers, sockets and mount points, file_poll(), psock_poll(), the waiter arrays and the poll callbacks use struct pollfd_s (a mechanical rename in 147 files). poll() still hands the user entries to the drivers, so the behaviour does not change.
  2. !fs/poll: Give struct pollfd the POSIX layout. struct pollfd gets only the three POSIX fields. poll() copies fd and events of each entry into a struct pollfd_s array (on the stack for up to 4 entries, on the fs heap for more) and copies revents back, also when the wait is interrupted. The cancellation cleanup frees the heap copy. The kernel build already copied the array on every call.
  3. Documentation: Describe struct pollfd and struct pollfd_s.

select(), pselect(), ppoll(), epoll and the system call interface do not change. epoll already keeps its own kernel entry for each file descriptor.

The older nxstyle errors of the touched files were fixed first, in #20794 and #20795, which are merged.

BREAKING CHANGE

struct pollfd has the POSIX layout (int fd, short events, short revents) and no NuttX fields. The kernel-only declarations moved from sys/poll.h to nuttx/poll.h.

Quick fix:

Impact

  • Users: sizeof(struct pollfd) is 8 on every target. Applications must be rebuilt.
  • Size (qemu-armv8a, text): +168 bytes in a flat build (nsh), +48 bytes in a kernel build (knsh). The stack of poll() grows by 4 x sizeof(struct pollfd_s): 160 bytes on a 64-bit target, 96 bytes on a 32-bit target.
  • Documentation: Documentation/os/filesystem/index.rst (poll method) and Documentation/reference/user/10_filesystem.rst (poll()).

Testing

A test program (pollabi.c) on qemu-armv8a:knsh (QEMU with HVF, CONFIG_PIPES=y):

Check master this PR
sizeof(struct pollfd) == 8, offsets 0/4/6 FAIL (40 bytes) PASS
poll() on 3 entries with the standard layout (pipe read end, write end, fd = -1) FAIL (returns 1, revents not written) PASS (returns 2, revents 1/4/0)
memory before and after that array not written PASS PASS
poll() on 6 entries (heap copy) PASS PASS
ppoll() timeout clears revents FAIL PASS
select(), epoll PASS PASS
two threads poll() the same array PASS (kernel build copies) PASS

Real hardware, flat builds, the same pollabi.c as an NSH app (logs in #20796 (comment)):

Board master this PR
Raspberry Pi RP2350 (Cortex-M33), pimoroni-pico-2-plus:nsh + CONFIG_PIPES pollabi FAIL (layout, 3 standard entries, ppoll timeout); ostest status 0 pollabi PASS; ostest status 0
ESP32-S3 (Xtensa LX7, WROOM-2 N32R8V), esp32s3-devkit:nsh + octal flash + CONFIG_PIPES pollabi FAIL (layout), then the two-thread test crashes the board: store to address 0 in pipecommon_poll() (drivers/pipes/pipe_common.c:773) pollabi PASS, no crash

On the ESP32-S3, ostest stops with an assertion in its libc tests on master and with this PR alike (status 256): an older failure on this board, not related to poll().

ostest ends with ostest_main: Exiting with status 0 on qemu-armv8a:knsh and qemu-armv8a:nsh. The two-thread case passes on master in a kernel build (poll() copies the array there) and crashes on master in a flat build (ESP32-S3 above). tools/checkpatch.sh -c -u -m -g passes.

Builds with -Werror: qemu-armv8a:nsh, knsh, citest, netnsh, rpserver, rpproxy, fb, and a qemu-armv8a:rpserver configuration with about 100 more options (rpmsg device client and server, usrsock rpmsg server, local sockets, eventfd, timerfd, signalfd, inotify, pty, packet, CAN, sensors, input and touchscreen drivers, ramlog, noteram, GPIO, procfs pressure, tun, cryptodev, lirc, timer, ADC, comparator, audio, MTD config, SPI/I2C slave, SLIP, battery, Bluetooth UART shim, IPCC, USB, LCD, modem and wireless drivers). That configuration compiles 1560 objects and gives the same compiler errors before and after the PR; all of them are older errors in drivers that no defconfig enables.

@github-actions github-actions Bot added Arch: arm Issues related to ARM (32-bit) architecture Arch: renesas Issues related to the Renesas chips Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. Area: Audio Board: arm Board: mips labels Oct 18, 2026
@github-actions

Copy link
Copy Markdown

❌ Cross-repo dependency could not be applied

The Build report says the declared dependency PR(s) could not be applied, so CI did not run against the combined code:

Reason: cherry-pick failed (if your PR has merge commits, rebase instead)

CI run: https://github.com/apache/nuttx/actions/runs/39176208076

@casaroli casaroli closed this Oct 18, 2026
@casaroli casaroli reopened this Oct 18, 2026
@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text +332 B (+0.5%, 64,585 B / 262,144 B, total: 25% used)

qemu-armv8a

  • Code: .text.init_main +8 B, .text.poll +160 B (+0.0%, 352,392 B)

qemu-intel64

  • Code: .text +78 B (+0.0%, 8,686,167 B)

rx65n-rsk2mb

  • ROM: .text +96 B (+0.1%, 89,244 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +464 B (+0.1%, 380,224 B)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@anchao

anchao commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Would it be possible to gather all nxstyle changes into a standalone PR? As a matter of fact, you already know which files need modifications from the current commits.

@github-actions

Copy link
Copy Markdown

@casaroli

casaroli commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Would it be possible to gather all nxstyle changes into a standalone PR? As a matter of fact, you already know which files need modifications from the current commits.

Yes. They are on #20794 and #20795.

When those merge, we rebase this one. There will be only 2 commits here: one of them is just a rename, and the other is the real work (which is small). I hope it will be much easier to review by then.

@cederom

cederom commented Oct 19, 2026

Copy link
Copy Markdown
Contributor
  • Thank you @casaroli good work! :-)
  • Could you please provide reference to the exact POSIX standard version and location that defines correct structures? :-)
  • We need some real world hardware runtime testing :-)

@casaroli

casaroli commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author
  • Thank you @casaroli good work! :-)
  • Could you please provide reference to the exact POSIX standard version and location that defines correct structures? :-)
  • We need some real world hardware runtime testing :-)

Sure, here it is (the 2018 edition, as Greg wrote on dev@): https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/basedefs/poll.h.html

I tested on RP2350 and ESP32-S3. For me, that is enough real-world 😅. I also tested on QEMU (arm64)

But I would love to have other people test on their own real worlds.

The logs (pollabi is the same test program on both boards, flat nsh build with CONFIG_PIPES; the PR build e1672486b32 has the same code as the current head; master is ddb09bb):

Raspberry Pi RP2350 (Cortex-M33), pimoroni-pico-2-plus:nsh

master:

NuttX 13.0.1-RC0 ddb09bb4719 Oct 19 2026 00:11:12 arm pimoroni-pico-2-plus
pollabi: sizeof(struct pollfd) == 8 FAIL
pollabi: offsets 0/4/6 FAIL
pollabi: poll ret=2 revents=a5a5/0/0
pollabi: poll 3 Linux-layout entries FAIL
pollabi: guards intact PASS
pollabi: poll 6 entries PASS
pollabi: ppoll timeout FAIL
pollabi: select PASS
pollabi: epoll PASS
pollabi: shared ret=1 thread=1
pollabi: two threads share one array PASS
pollabi: FAIL
ostest_main: Exiting with status 0

this PR:

NuttX 13.0.1-RC0 e1672486b32 Oct 19 2026 00:57:34 arm pimoroni-pico-2-plus
pollabi: sizeof(struct pollfd) == 8 PASS
pollabi: offsets 0/4/6 PASS
pollabi: poll ret=2 revents=1/4/0
pollabi: poll 3 Linux-layout entries PASS
pollabi: guards intact PASS
pollabi: poll 6 entries PASS
pollabi: ppoll timeout PASS
pollabi: select PASS
pollabi: epoll PASS
pollabi: shared ret=1 thread=1
pollabi: two threads share one array PASS
pollabi: PASS
ostest_main: Exiting with status 0
ESP32-S3 (Xtensa LX7, WROOM-2 N32R8V), esp32s3-devkit:nsh

master: the two-thread test crashes the board (store to address 0 in pipecommon_poll()):

NuttX 13.0.1-RC0 ddb09bb4719 Oct 19 2026 00:53:35 xtensa esp32s3-devkit
pollabi: sizeof(struct pollfd) == 8 FAIL
pollabi: offsets 0/4/6 FAIL
pollabi: poll ret=1 revents=a5a5/0/0
pollabi: poll 3 Linux-layout entries FAIL
pollabi: guards intact PASS
pollabi: poll 6 entries PASS
pollabi: ppoll timeout FAIL
pollabi: select PASS
pollabi: epoll PASS
xtensa_user_panic: User Exception: EXCCAUSE=001d task: pollabi
dump_assert_info: Assertion failed user panic: at file: :0 task: pollabi process: pollabi 0x42029974

this PR:

NuttX 13.0.1-RC0 e1672486b32 Oct 19 2026 01:06:03 xtensa esp32s3-devkit
pollabi: sizeof(struct pollfd) == 8 PASS
pollabi: offsets 0/4/6 PASS
pollabi: poll ret=2 revents=1/4/0
pollabi: poll 3 Linux-layout entries PASS
pollabi: guards intact PASS
pollabi: poll 6 entries PASS
pollabi: ppoll timeout PASS
pollabi: select PASS
pollabi: epoll PASS
pollabi: shared ret=1 thread=1
pollabi: two threads share one array PASS
pollabi: PASS

ostest ends with Exiting with status 256 on this board on master (ddb09bb) and with this PR alike: an older failure in its libc tests, not related to poll().

BR

@casaroli casaroli closed this Oct 19, 2026
@casaroli casaroli reopened this Oct 19, 2026
@github-actions

Copy link
Copy Markdown

❌ Cross-repo dependency could not be applied

The Build report says the declared dependency PR(s) could not be applied, so CI did not run against the combined code:

Reason: cherry-pick failed (if your PR has merge commits, rebase instead)

CI run: https://github.com/apache/nuttx/actions/runs/39233488400

The kernel side of poll() used struct pollfd, the type of the user
array.  Add include/nuttx/poll.h with struct pollfd_s, a kernel type
with the same fields, and move the kernel-only declarations there:
pollevent_t, pollcb_t, POLLALWAYS, poll_notify() and poll_default_cb().
include/nuttx/fs/fs.h includes it.

The poll() methods of drivers, sockets and mount points, file_poll(),
psock_poll(), the waiter arrays and the poll callbacks now use struct
pollfd_s.  poll() gives the drivers the entries of the user array as
struct pollfd_s; the two structures have the same layout, so the
behaviour does not change.

BREAKING CHANGE: the poll() method of a driver, a socket or a mount
point takes a struct pollfd_s, not a struct pollfd.  Quick fix: in
out-of-tree drivers, rename struct pollfd to struct pollfd_s in the
poll methods, waiter arrays and poll callbacks, and include
nuttx/poll.h (nuttx/fs/fs.h includes it).

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
Comment thread fs/vfs/fs_poll.c Resolved
Comment thread fs/vfs/fs_poll.c Resolved
struct pollfd had 32-bit events and revents and three kernel fields
(arg, cb and priv): 40 bytes on a 64-bit target, 24 bytes on a 32-bit
target.  POSIX gives it fd, short events and short revents: <poll.h>
in IEEE Std 1003.1-2017 (Revision of IEEE Std 1003.1-2008) / The Open
Group Base Specifications Issue 7, 2018 edition.  A program that
builds its own array with the standard layout and calls poll() gets
its neighbouring entries overwritten.

struct pollfd now has only the three POSIX fields.  poll() copies fd
and events of each entry into a struct pollfd_s array, on the stack for
up to 4 entries and on the fs heap for more, and copies revents back,
also when the wait is interrupted.  The cancellation cleanup frees the
heap copy.

This also stops the kernel from keeping its poll callback and semaphore
in the caller's array in flat and protected builds, where poll_notify()
called a function pointer that user code could change.  Two threads can
now poll the same array.

BREAKING CHANGE: struct pollfd has the POSIX layout (int fd, short
events, short revents) and no NuttX fields.  Quick fix: rebuild the
applications; code that uses only fd, events and revents needs no
change.  Print events and revents with %x, not PRIx32.  Out-of-tree
drivers use struct pollfd_s from nuttx/poll.h in their poll methods,
waiter arrays and poll callbacks.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
The poll() method of a driver takes struct pollfd_s now.  Say what
struct pollfd_s holds, and give the POSIX fields of struct pollfd in
the poll() reference, with a link to <poll.h> in IEEE Std 1003.1-2017
(Revision of IEEE Std 1003.1-2008) / The Open Group Base Specifications
Issue 7, 2018 edition.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
@hartmannathan

Copy link
Copy Markdown
Contributor

Reviewing...

@hartmannathan hartmannathan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for improving POSIX compliance!

@acassis
acassis merged commit 97b5dde into apache:master Oct 27, 2026
64 checks passed
acassis pushed a commit to apache/nuttx-apps that referenced this pull request Oct 28, 2026
The events and revents fields of struct pollfd now have the type short,
as POSIX gives them.  A short value is promoted to int, so %x prints it
as is.  Remove the casts to unsigned int that were added while the
fields were a 32-bit pollevent_t.

This change was in #3888, which was reverted by #3909 because it merged
before apache/nuttx#20796 changed the type.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Arch: renesas Issues related to the Renesas chips Area: Audio Board: arm Board: mips Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants