feat(libkernel): support EVFILT_WRITE on the write end of a pipe #7075

Merged
AlexOfTheForest merged 3 commits from upstream-pr/7075 into main 2026-10-20 15:15:04 +00:00
AlexOfTheForest commented 2026-10-17 10:25:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlexOfTheForest on 2026-10-17T10:25:07Z.


What

kevent threw NotImplemented for EVFILT_WRITE on any descriptor that is not a socket. On the write end of a pipe it now follows FreeBSD 11's filt_pipewrite and pipe_kqfilter:

  • data is the free space in the host pipe buffer: F_GETPIPE_SZ minus FIONREAD on Linux, the write quota on Windows.
  • Once the read end is closed, the event reports EV_EOF with data 0.
  • Adding the event after the read end is closed fails with EPIPE.

Two commits:

  • feat(libkernel): report the write end of a pipe to equeue descriptor polls: GuestSockets::Ready only knew the read end of a pipe, and WaitAny asked the host for POLLIN on every descriptor that is not a socket, so a write interest on a pipe could never fire or wake a wait. Adds PipeWriteState; Ready reports the write end when it is writable or at EOF, and WaitAny asks for POLLOUT on it.
  • feat(libkernel): support EVFILT_WRITE on the write end of a pipe: the kevent side, the libkernel debt entries, and a PipeWrite case in guest_kqueue. The test covers: free space before and after a write, no event while the pipe is full (filled with O_NONBLOCK writes until EAGAIN), a blocking wait woken when another thread drains the pipe, EV_EOF with data 0 after the read end closes, and EPIPE when the event is added after that.

Not changed: EVFILT_WRITE on the read end still throws, because FreeBSD's pipe ends are bidirectional and host pipes are one-way. EV_CLEAR and NOTE_LOWAT on a pipe still throw. On macOS EVFILT_WRITE on a pipe throws, because the host has no call for a pipe's capacity. Readiness on Linux follows the host's POLLOUT rather than FreeBSD's 512 free bytes, so a guest is never told to write into a pipe that would block. This is documented in the debt file.

Tested

Linux x86_64: guest_kqueue, guest_equeue_events, guest_poll, guest_select and guest_select_wait all passed 5 of 5 runs. With the WaitAny change reverted, guest_kqueue fails: the wait runs out its 5 s limit instead of waking when the pipe drains. tools/check_conventions.py --base upstream/main passes; the progress report is unchanged. The Windows path (NtQueryInformationFile write quota, the same check poll already uses) is written but not run. On macOS this case still throws, and the test keeps checking that it throws there.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4899293541 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7075). Originally posted by [AlexOfTheForest](https://github.com/AlexOfTheForest) on 2026-10-17T10:25:07Z. --- ### What `kevent` threw `NotImplemented` for `EVFILT_WRITE` on any descriptor that is not a socket. On the write end of a pipe it now follows FreeBSD 11's [`filt_pipewrite`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/sys_pipe.c#L1809-L1828) and [`pipe_kqfilter`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/kern/sys_pipe.c#L1755-L1761): - `data` is the free space in the host pipe buffer: `F_GETPIPE_SZ` minus `FIONREAD` on Linux, the write quota on Windows. - Once the read end is closed, the event reports `EV_EOF` with `data` 0. - Adding the event after the read end is closed fails with `EPIPE`. Two commits: - `feat(libkernel): report the write end of a pipe to equeue descriptor polls`: `GuestSockets::Ready` only knew the read end of a pipe, and `WaitAny` asked the host for `POLLIN` on every descriptor that is not a socket, so a write interest on a pipe could never fire or wake a wait. Adds `PipeWriteState`; `Ready` reports the write end when it is writable or at EOF, and `WaitAny` asks for `POLLOUT` on it. - `feat(libkernel): support EVFILT_WRITE on the write end of a pipe`: the `kevent` side, the libkernel debt entries, and a `PipeWrite` case in `guest_kqueue`. The test covers: free space before and after a write, no event while the pipe is full (filled with `O_NONBLOCK` writes until `EAGAIN`), a blocking wait woken when another thread drains the pipe, `EV_EOF` with `data` 0 after the read end closes, and `EPIPE` when the event is added after that. Not changed: `EVFILT_WRITE` on the read end still throws, because FreeBSD's pipe ends are bidirectional and host pipes are one-way. `EV_CLEAR` and `NOTE_LOWAT` on a pipe still throw. On macOS `EVFILT_WRITE` on a pipe throws, because the host has no call for a pipe's capacity. Readiness on Linux follows the host's `POLLOUT` rather than FreeBSD's 512 free bytes, so a guest is never told to write into a pipe that would block. This is documented in the debt file. ### Tested Linux x86_64: `guest_kqueue`, `guest_equeue_events`, `guest_poll`, `guest_select` and `guest_select_wait` all passed 5 of 5 runs. With the `WaitAny` change reverted, `guest_kqueue` fails: the wait runs out its 5 s limit instead of waking when the pipe drains. `tools/check_conventions.py --base upstream/main` passes; the progress report is unchanged. The Windows path (`NtQueryInformationFile` write quota, the same check `poll` already uses) is written but not run. On macOS this case still throws, and the test keeps checking that it throws there. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-17 10:31:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-17T10:31:30Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 7088 core/libs/prx/libkernel/Socket/src/Udp.cpp L379
docs/dev/debt/libkernel.md L117, L189
PR 7121 docs/dev/debt/libkernel.md L117-118
PR 7653 docs/dev/debt/libkernel.md L117
PR 7937 docs/dev/debt/libkernel.md L116-117

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4630 docs/dev/debt/libkernel.md
PR 5021 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 6044 core/libs/tests/GuestKqueue.cpp
PR 6149 docs/dev/debt/libkernel.md
PR 6469 docs/dev/debt/libkernel.md
PR 6519 docs/dev/debt/libkernel.md
PR 6658 docs/dev/debt/libkernel.md
PR 6740 docs/dev/debt/libkernel.md
PR 6893 docs/dev/debt/libkernel.md
PR 6977 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 7049 core/libs/prx/libkernel/Equeue/src/Kqueue.cpp
core/libs/tests/GuestKqueue.cpp
docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 7464 docs/dev/debt/libkernel.md
PR 7509 docs/dev/debt/libkernel.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6232845750 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7075#issuecomment-6232845750). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-17T10:31:30Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 7088](https://www.github.com/boykopovar/AnyPS5/pull/7088) | `core/libs/prx/libkernel/Socket/src/Udp.cpp` [L379](https://github.com/boykopovar/AnyPS5/blob/cabd80f472b86b5ffa3410fc91db4b83df278d27/core/libs/prx/libkernel/Socket/src/Udp.cpp#L379)<br>`docs/dev/debt/libkernel.md` [L117](https://github.com/boykopovar/AnyPS5/blob/cabd80f472b86b5ffa3410fc91db4b83df278d27/docs/dev/debt/libkernel.md#L117), [L189](https://github.com/boykopovar/AnyPS5/blob/cabd80f472b86b5ffa3410fc91db4b83df278d27/docs/dev/debt/libkernel.md#L189) | | | | [PR 7121](https://www.github.com/boykopovar/AnyPS5/pull/7121) | `docs/dev/debt/libkernel.md` [L117-118](https://github.com/boykopovar/AnyPS5/blob/cabd80f472b86b5ffa3410fc91db4b83df278d27/docs/dev/debt/libkernel.md#L117-L118) | | | | [PR 7653](https://www.github.com/boykopovar/AnyPS5/pull/7653) | `docs/dev/debt/libkernel.md` [L117](https://github.com/boykopovar/AnyPS5/blob/cabd80f472b86b5ffa3410fc91db4b83df278d27/docs/dev/debt/libkernel.md#L117) | | | | [PR 7937](https://www.github.com/boykopovar/AnyPS5/pull/7937) | `docs/dev/debt/libkernel.md` [L116-117](https://github.com/boykopovar/AnyPS5/blob/cabd80f472b86b5ffa3410fc91db4b83df278d27/docs/dev/debt/libkernel.md#L116-L117) | | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 4630](https://www.github.com/boykopovar/AnyPS5/pull/4630) | `docs/dev/debt/libkernel.md` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/Socket/src/Udp.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestKqueue.cpp` | | [PR 6149](https://www.github.com/boykopovar/AnyPS5/pull/6149) | `docs/dev/debt/libkernel.md` | | [PR 6469](https://www.github.com/boykopovar/AnyPS5/pull/6469) | `docs/dev/debt/libkernel.md` | | [PR 6519](https://www.github.com/boykopovar/AnyPS5/pull/6519) | `docs/dev/debt/libkernel.md` | | [PR 6658](https://www.github.com/boykopovar/AnyPS5/pull/6658) | `docs/dev/debt/libkernel.md` | | [PR 6740](https://www.github.com/boykopovar/AnyPS5/pull/6740) | `docs/dev/debt/libkernel.md` | | [PR 6893](https://www.github.com/boykopovar/AnyPS5/pull/6893) | `docs/dev/debt/libkernel.md` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `core/libs/prx/libkernel/Socket/src/Udp.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 7049](https://www.github.com/boykopovar/AnyPS5/pull/7049) | `core/libs/prx/libkernel/Equeue/src/Kqueue.cpp`<br>`core/libs/tests/GuestKqueue.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 7105](https://www.github.com/boykopovar/AnyPS5/pull/7105) | `docs/dev/debt/libkernel.md` | | [PR 7149](https://www.github.com/boykopovar/AnyPS5/pull/7149) | `docs/dev/debt/libkernel.md` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/Socket/src/Udp.cpp` | | [PR 7464](https://www.github.com/boykopovar/AnyPS5/pull/7464) | `docs/dev/debt/libkernel.md` | | [PR 7509](https://www.github.com/boykopovar/AnyPS5/pull/7509) | `docs/dev/debt/libkernel.md` |
blawesom commented 2026-10-18 10:12:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-18T10:12:57Z.


Rebase handoff — rebased onto current main (authorship unchanged). One docs-debt conflict, unioned.

Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV): builds; conventions pass; the kqueue tests pass; full suite green apart from the known main regression (agc_driver_tessellation segfault — Mesa, #5444), not this PR.

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/7075
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is d7023cce2.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6248626535 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7075#issuecomment-6248626535). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-18T10:12:57Z. --- **Rebase handoff** — rebased onto current `main` (authorship unchanged). One docs-debt conflict, unioned. Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`, AMD RX 9070 XT / RADV): builds; conventions pass; the kqueue tests pass; **full suite green apart from the known `main` regression** (`agc_driver_tessellation` segfault — Mesa, #5444), not this PR. **To pick it up:** ``` git fetch https://github.com/blawesom/AnyPS5 rebase/7075 git checkout <your branch> git merge --ff-only FETCH_HEAD ``` Head is `d7023cce2`.
AlexOfTheForest commented 2026-10-18 12:04:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlexOfTheForest on 2026-10-18T12:04:13Z.


@blawesom thanks for the rebase. I redid it on my side so the commits stay under my identity as committer too, using your docs resolution as the reference. The branch is now rebased on current main; guest_kqueue, guest_socket_events, guest_equeue_events, guest_poll and guest_select pass 3/3 on Linux.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6249692572 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7075#issuecomment-6249692572). Originally posted by [AlexOfTheForest](https://github.com/AlexOfTheForest) on 2026-10-18T12:04:13Z. --- @blawesom thanks for the rebase. I redid it on my side so the commits stay under my identity as committer too, using your docs resolution as the reference. The branch is now rebased on current main; guest_kqueue, guest_socket_events, guest_equeue_events, guest_poll and guest_select pass 3/3 on Linux.
thuanquang commented 2026-10-18 21:30:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-18T21:30:23Z.


The new PipeWrite in GuestKqueue.cpp takes start after it creates the reader thread that acts after 50 ms, then requires waited >= 40 ms. Under parallel load the main thread can be preempted between creating that thread and reading the clock, so waited comes out under 40 ms although kevent did wait for the read. PipeRead in the same file has this order and aborts that way on Windows: on our RX 6700 XT PC, 9 of 120 runs under load. A timing probe caught 4 such aborts, and in each the main thread took start 39 to 68 ms after creating the writer, with kevent returning 0.3 to 1.4 ms after the write. #7670 moves start before the thread there and in guest_socket_events. Taking start before creating the reader here keeps the new check from flaking the same way.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6255382631 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7075#issuecomment-6255382631). Originally posted by [thuanquang](https://github.com/thuanquang) on 2026-10-18T21:30:23Z. --- The new `PipeWrite` in `GuestKqueue.cpp` takes `start` after it creates the reader thread that acts after 50 ms, then requires `waited >= 40 ms`. Under parallel load the main thread can be preempted between creating that thread and reading the clock, so `waited` comes out under 40 ms although `kevent` did wait for the read. `PipeRead` in the same file has this order and aborts that way on Windows: on our RX 6700 XT PC, 9 of 120 runs under load. A timing probe caught 4 such aborts, and in each the main thread took `start` 39 to 68 ms after creating the writer, with `kevent` returning 0.3 to 1.4 ms after the write. #7670 moves `start` before the thread there and in `guest_socket_events`. Taking `start` before creating the reader here keeps the new check from flaking the same way.
adriadam10 commented 2026-10-19 17:51:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T17:51:47Z.

Review: CHANGES_REQUESTED


The full Build's Windows job fails guest_kqueue on the current head (run 39295565110, main is green there):

  • PipeWrite in core/libs/tests/GuestKqueue.cpp (the fcntl_nid_postfix(pipe[1], SetStatusFlags, NonBlocking) line) throws on Windows: fcntl: cannot change the mode of pipe descriptor 5, error=231 (SetNamedPipeHandleState on the write end, DescriptorControl.cpp:169). The test never reaches the kevent code.
  • Fix: on Windows, fill the pipe without setting O_NONBLOCK on the write end (or make fcntl support it there), or limit the full-pipe/drain part of the test to Linux and keep the free-space, EV_EOF and EPIPE checks on both. If the write-end path of PipeWriteState (NtQueryInformationFile write quota) cannot be exercised on Windows, say so in the debt entry.

The rest of the libkernel scope passes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5575530068 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7075#pullrequestreview-5575530068). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T17:51:47Z. **Review:** CHANGES_REQUESTED --- The full Build's Windows job fails `guest_kqueue` on the current head (run 39295565110, `main` is green there): - `PipeWrite` in `core/libs/tests/GuestKqueue.cpp` (the `fcntl_nid_postfix(pipe[1], SetStatusFlags, NonBlocking)` line) throws on Windows: `fcntl: cannot change the mode of pipe descriptor 5, error=231` (`SetNamedPipeHandleState` on the write end, `DescriptorControl.cpp:169`). The test never reaches the `kevent` code. - Fix: on Windows, fill the pipe without setting `O_NONBLOCK` on the write end (or make `fcntl` support it there), or limit the full-pipe/drain part of the test to Linux and keep the free-space, `EV_EOF` and `EPIPE` checks on both. If the write-end path of `PipeWriteState` (`NtQueryInformationFile` write quota) cannot be exercised on Windows, say so in the debt entry. The rest of the libkernel scope passes.
adriadam10 commented 2026-10-20 15:16:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-20T15:16:41Z.


Rebased onto current main and added one commit of ours for the Windows failure of guest_kqueue (fcntl cannot set O_NONBLOCK on a pipe write end there, error=231):

  • PipeWrite: the full-pipe and drain part now runs off Windows only; on Windows the test reads back what it wrote and still checks the free space, EV_EOF and EPIPE.
  • start is taken before the reader thread is created, as @thuanquang pointed out.
  • The pipe debt entry says the full-pipe case is Linux-only.

Checked the diff against FreeBSD 11's filt_pipewrite / pipe_kqfilter; guest_kqueue and the libkernel scope pass; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6294280257 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7075#issuecomment-6294280257). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-20T15:16:41Z. --- Rebased onto current `main` and added one commit of ours for the Windows failure of `guest_kqueue` (`fcntl` cannot set `O_NONBLOCK` on a pipe write end there, error=231): - `PipeWrite`: the full-pipe and drain part now runs off Windows only; on Windows the test reads back what it wrote and still checks the free space, `EV_EOF` and `EPIPE`. - `start` is taken before the reader thread is created, as @thuanquang pointed out. - The pipe debt entry says the full-pipe case is Linux-only. Checked the diff against FreeBSD 11's `filt_pipewrite` / `pipe_kqfilter`; `guest_kqueue` and the libkernel scope pass; merged.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!7075
No description provided.