Skip to content

fs/poll: Tear down a poll through the file of its setup. - #21490

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
casaroli:fb-poll-close-up
Nov 6, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
casaroli:fb-poll-close-up

Conversation

@casaroli

@casaroli casaroli commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

poll_teardown() looked up the file again by its descriptor. If another thread closes the descriptor during poll(), the lookup fails, so the driver is not told that the poll ended and the reference of the setup is not dropped. The file stays open, and the driver keeps a pointer to the struct pollfd_s, which is gone when poll() returns. The next event of that driver uses this pointer.

In a kernel build this panics the kernel from an ordinary program: a pipe, one thread in poll() on the read end, and another thread that calls close() on it. Then the close of the write end posts an event through the old pointer:

arm64_el1_undef: Undefined instruction at 0x402edc10
dump_assert_info: Assertion failed panic: at file: common/arm64_fatal.c:696

This PR keeps the file of the setup in struct pollfd_s and uses it in poll_teardown(). The file stays open until poll() ends, and then it closes. epoll already keeps the file this way.

Impact

All drivers and all build modes: poll() and select(). The poll method of a driver does not change. struct pollfd_s is kernel only; the user struct pollfd does not change. No ABI change. Not a breaking change.

A descriptor that another thread closes during poll() now closes when poll() ends. Before, it never closed.

Size, image text from master 6e1126a, float and numbered arguments off: Cortex-M0 nucleo-f072rb:nsh 0, Cortex-M3 nucleo-f103rb:nsh 0, rv-virt:nsh -10, qemu-armv8a:nsh -8. Data and bss do not change.

RAM: struct pollfd_s grows by one pointer. poll() keeps 4 entries on its stack (+16 bytes on a 32-bit CPU, +32 on a 64-bit CPU), a Unix domain socket has 4 entries, and an epoll entry has one. This comes from the types and was not measured.

Documentation: Documentation/os/filesystem/index.rst names the new field, in this commit.

Testing

Test: poll_close_test in ostest, apache/nuttx-apps#3980, a draft that depends on this PR. Another thread closes the read end of a pipe during poll(). When poll() ends, the write end must report POLLERR, because no reader is left.

Config master 6e1126a this PR
qemu-armv8a:knsh (kernel) ERROR: no POLLERR, then kernel panic PASS, ostest status 0
qemu-armv8a:nsh (flat) ERROR: no POLLERR, ostest status 256 PASS, ostest status 0
rv-virt:knsh64 (kernel) not run PASS, ostest status 0
mps2-an500:knsh (protected) not run PASS, ostest status 0

The tests ran before the rebase on bf029a2; the new commits touch no file of this PR. The commit is the same, apart from one line in the documentation.

tools/checkpatch.sh -c -u -m -g passes.

poll_teardown() looked up the file again by its descriptor.  If another
thread closed the descriptor during the poll, the lookup failed, so the
driver was not told and the reference of the setup was not dropped.  The
file stayed open, and the driver kept a pointer to the struct pollfd_s,
which is gone when poll() returns.  The next event of that driver used
this pointer.

Keep the file of the setup in struct pollfd_s and use it in
poll_teardown().  The file stays open until poll() ends, and then it
closes.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: S The size of the change in this PR is small labels Nov 5, 2026
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text -26 B (-0.0%, 66,329 B / 262,144 B, total: 25% used)

qemu-armv8a

  • Code: .text.poll +8 B, .text.poll_teardown -24 B (-0.0%, 360,412 B)

qemu-intel64

  • Code: .text -31 B (-0.0%, 8,693,362 B)

s698pm-dkit

  • Code: .text +96 B (+0.0%, 389,456 B)

No memory changes detected for:


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

@xiaoxiang781216
xiaoxiang781216 merged commit 7be533e into apache:master Nov 6, 2026
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants