Repository navigation
!fs/poll: Give struct pollfd the POSIX layout. - #20796
Conversation
❌ Cross-repo dependency could not be appliedThe 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) |
|
|
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. |
🔗 Cross-repo PR dependenciesThe read-only Build run reported the following dependent PR(s) and fetched head SHA(s): |
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. |
26a40f9 to
324d700
Compare
|
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 ( Raspberry Pi RP2350 (Cortex-M33), pimoroni-pico-2-plus:nshmaster: this PR: ESP32-S3 (Xtensa LX7, WROOM-2 N32R8V), esp32s3-devkit:nshmaster: the two-thread test crashes the board (store to address 0 in this PR:
BR |
❌ Cross-repo dependency could not be appliedThe 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) |
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]>
324d700 to
b02ec1e
Compare
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]>
|
Reviewing... |
hartmannathan
left a comment
There was a problem hiding this comment.
Thank you for improving POSIX compliance!
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]>
Summary
struct pollfdhad 32-biteventsandreventsand three kernel fields (arg,cb,priv): 40 bytes on a 64-bit target and 24 bytes on a 32-bit target. POSIX gives itint fd,short eventsandshort 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 callspoll()gets its neighbouring entries overwritten. The Rustlibccrate, 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, andpoll_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 inpoll.h).This PR has three commits:
fs/poll: Use struct pollfd_s for the kernel side of poll.A new kernel header,include/nuttx/poll.h, holdsstruct pollfd_s(the same fields as the oldstruct pollfd),pollevent_t,pollcb_t,POLLALWAYS,poll_notify()andpoll_default_cb().include/nuttx/fs/fs.hincludes it. The poll methods of drivers, sockets and mount points,file_poll(),psock_poll(), the waiter arrays and the poll callbacks usestruct pollfd_s(a mechanical rename in 147 files).poll()still hands the user entries to the drivers, so the behaviour does not change.!fs/poll: Give struct pollfd the POSIX layout.struct pollfdgets only the three POSIX fields.poll()copiesfdandeventsof each entry into astruct pollfd_sarray (on the stack for up to 4 entries, on the fs heap for more) and copiesreventsback, also when the wait is interrupted. The cancellation cleanup frees the heap copy. The kernel build already copied the array on every call.Documentation: Describe struct pollfd and struct pollfd_s.select(),pselect(),ppoll(),epolland the system call interface do not change.epollalready 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 pollfdhas the POSIX layout (int fd,short events,short revents) and no NuttX fields. The kernel-only declarations moved fromsys/poll.htonuttx/poll.h.Quick fix:
fd,eventsandreventsneeds no change. Printeventsandreventswith%x, notPRIx32(apps: Print the revents field of struct pollfd with %x. nuttx-apps#3878, merged, fixed the programs in nuttx-apps).struct pollfdtostruct pollfd_sin the poll methods, the waiter arrays and the poll callbacks. Include<nuttx/poll.h>if the file does not include<nuttx/fs/fs.h>. The compiler reports every place that is not renamed.Impact
sizeof(struct pollfd)is 8 on every target. Applications must be rebuilt.qemu-armv8a, text): +168 bytes in a flat build (nsh), +48 bytes in a kernel build (knsh). The stack ofpoll()grows by 4 xsizeof(struct pollfd_s): 160 bytes on a 64-bit target, 96 bytes on a 32-bit target.Documentation/os/filesystem/index.rst(poll method) andDocumentation/reference/user/10_filesystem.rst(poll()).Testing
A test program (
pollabi.c) onqemu-armv8a:knsh(QEMU with HVF,CONFIG_PIPES=y):sizeof(struct pollfd) == 8, offsets 0/4/6poll()on 3 entries with the standard layout (pipe read end, write end,fd = -1)reventsnot written)revents1/4/0)poll()on 6 entries (heap copy)ppoll()timeout clearsreventsselect(),epollpoll()the same arrayReal hardware, flat builds, the same
pollabi.cas an NSH app (logs in #20796 (comment)):pimoroni-pico-2-plus:nsh+CONFIG_PIPESpollabiFAIL (layout, 3 standard entries,ppolltimeout);osteststatus 0pollabiPASS;osteststatus 0esp32s3-devkit:nsh+ octal flash +CONFIG_PIPESpollabiFAIL (layout), then the two-thread test crashes the board: store to address 0 inpipecommon_poll()(drivers/pipes/pipe_common.c:773)pollabiPASS, no crashOn the ESP32-S3,
osteststops with an assertion in its libc tests on master and with this PR alike (status 256): an older failure on this board, not related topoll().ostestends withostest_main: Exiting with status 0onqemu-armv8a:knshandqemu-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 -gpasses.Builds with
-Werror:qemu-armv8a:nsh,knsh,citest,netnsh,rpserver,rpproxy,fb, and aqemu-armv8a:rpserverconfiguration 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.