feat(libkernel): support kevent read and write events on regular files #7088

Closed
inmert wants to merge 4 commits from upstream-pr/7088 into main
inmert commented 2026-10-17 10:59:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by inmert on 2026-10-17T10:59:58Z.


What

kevent threw for EVFILT_READ and EVFILT_WRITE on a regular file:

kevent: EVFILT_READ on a descriptor that is neither a socket nor the read end of a pipe not implemented

Both now follow FreeBSD 11's vfs_kqfilter, filt_vfsread and filt_vfswrite, from the same commit the kqueue debt entry already links. A read event reports the file size minus the descriptor's offset (negative past the end) and fires when that is not zero, or always with NOTE_FILE_POLL. NOTE_LOWAT and data are ignored, and adding the event again replaces its fflags, as kqueue_register does for a filter without f_touch. A write event always fires with data 0. Size and offset come from sceKernelFstat and sceKernelLseek, so files served from app0.zar work as well.

A waiting kevent is woken by the host watch EVFILT_VNODE already uses, so a write from another descriptor or process wakes it. Files from app0.zar never change and get no watch.

GuestSockets::WaitAny also needed a change. It passed every interest that is not a socket to ppoll, and the host reports a regular file (and the null device behind an archived file) ready at once, so a kevent waiting on a file at its end spun on the CPU. It now passes only pipes to the host, and on Windows the 1 ms pipe slicing is only turned on when a pipe is actually in the set.

EV_CLEAR on a regular file still throws, as it does on sockets and pipes, and so do directories (their va_size is not modelled) and the other descriptor types. The debt entry also notes the one difference I know of: the event is re-evaluated at every scan, so after reading to the end a seek back is reported straight away, where FreeBSD waits for the next write, truncation or attribute change.

Overlap: #7075 changes the same line of WaitAny. I used its interest.write ? WriteMask : ReadMask there, so whichever lands second only rebases that line. #7049 changes the lines next to mine in ModifyKnote; the two don't interact. #7092 (NOTE_LOWAT on sockets) conflicts in ModifyKnote and in the kqueue debt sentence (git merge-tree of the two heads). They don't clash in behaviour: its DescriptorLowWater only applies to sockets, and a regular file ignores the mark as filt_vfsread does, so the resolution is to keep its DescriptorLowWater call in place of my !file guard, and to keep both edits to the debt sentence. #6343 (descriptor 0) touches the same branch of AddDescriptorKnote; with this PR a stdin redirected from a regular file takes the new path, which is what FreeBSD does too.

Tested

Ubuntu 24.04, GCC 13.3.0, Release, on main ca82d7d88.

  • New RegularFile case in guest_kqueue: data in the middle and at the end of the file, negative data past the end, NOTE_FILE_POLL turned on and off by adding the event again, NOTE_LOWAT ignored, a blocking kevent woken by a write through a second descriptor 50 ms later, a truncation, EV_CLEAR throwing, the write event, and the events going away on close. It aborts on main with the error above and passes with this change.
  • Without the WaitAny change the test still passes, but takes about 80 ms of CPU instead of 10 ms (/usr/bin/time, 3 runs each) because the 50 ms wait spins.
  • guest_equeue_events, guest_kqueue, guest_poll, guest_select, guest_select_wait, the guest_socket_* tests, guest_tcp, guest_udp, guest_sce_net, the guest_rudp_* tests and guest_net_ctl: 22/22 pass.
  • python3 tools/check_conventions.py --base origin/main passes.
  • Not tested: Windows and macOS (only Linux was built here), a console, a title.

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:4899508288 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7088). Originally posted by [inmert](https://github.com/inmert) on 2026-10-17T10:59:58Z. --- ### What `kevent` threw for `EVFILT_READ` and `EVFILT_WRITE` on a regular file: ``` kevent: EVFILT_READ on a descriptor that is neither a socket nor the read end of a pipe not implemented ``` Both now follow FreeBSD 11's [`vfs_kqfilter`](https://github.com/freebsd/freebsd-src/blob/34bd34750187bae02a0086402c78c031c59dd8b3/sys/kern/vfs_subr.c#L4936-L4967), [`filt_vfsread`](https://github.com/freebsd/freebsd-src/blob/34bd34750187bae02a0086402c78c031c59dd8b3/sys/kern/vfs_subr.c#L4983-L5009) and [`filt_vfswrite`](https://github.com/freebsd/freebsd-src/blob/34bd34750187bae02a0086402c78c031c59dd8b3/sys/kern/vfs_subr.c#L5012-L5029), from the same commit the kqueue debt entry already links. A read event reports the file size minus the descriptor's offset (negative past the end) and fires when that is not zero, or always with `NOTE_FILE_POLL`. `NOTE_LOWAT` and `data` are ignored, and adding the event again replaces its `fflags`, as `kqueue_register` does for a filter without `f_touch`. A write event always fires with `data` 0. Size and offset come from `sceKernelFstat` and `sceKernelLseek`, so files served from `app0.zar` work as well. A waiting `kevent` is woken by the host watch `EVFILT_VNODE` already uses, so a write from another descriptor or process wakes it. Files from `app0.zar` never change and get no watch. `GuestSockets::WaitAny` also needed a change. It passed every interest that is not a socket to `ppoll`, and the host reports a regular file (and the null device behind an archived file) ready at once, so a `kevent` waiting on a file at its end spun on the CPU. It now passes only pipes to the host, and on Windows the 1 ms pipe slicing is only turned on when a pipe is actually in the set. `EV_CLEAR` on a regular file still throws, as it does on sockets and pipes, and so do directories (their `va_size` is not modelled) and the other descriptor types. The debt entry also notes the one difference I know of: the event is re-evaluated at every scan, so after reading to the end a seek back is reported straight away, where FreeBSD waits for the next write, truncation or attribute change. Overlap: #7075 changes the same line of `WaitAny`. I used its `interest.write ? WriteMask : ReadMask` there, so whichever lands second only rebases that line. #7049 changes the lines next to mine in `ModifyKnote`; the two don't interact. #7092 (`NOTE_LOWAT` on sockets) conflicts in `ModifyKnote` and in the kqueue debt sentence (`git merge-tree` of the two heads). They don't clash in behaviour: its `DescriptorLowWater` only applies to sockets, and a regular file ignores the mark as `filt_vfsread` does, so the resolution is to keep its `DescriptorLowWater` call in place of my `!file` guard, and to keep both edits to the debt sentence. #6343 (descriptor 0) touches the same branch of `AddDescriptorKnote`; with this PR a stdin redirected from a regular file takes the new path, which is what FreeBSD does too. ### Tested Ubuntu 24.04, GCC 13.3.0, Release, on `main` `ca82d7d88`. - New `RegularFile` case in `guest_kqueue`: `data` in the middle and at the end of the file, negative `data` past the end, `NOTE_FILE_POLL` turned on and off by adding the event again, `NOTE_LOWAT` ignored, a blocking `kevent` woken by a write through a second descriptor 50 ms later, a truncation, `EV_CLEAR` throwing, the write event, and the events going away on close. It aborts on `main` with the error above and passes with this change. - Without the `WaitAny` change the test still passes, but takes about 80 ms of CPU instead of 10 ms (`/usr/bin/time`, 3 runs each) because the 50 ms wait spins. - `guest_equeue_events`, `guest_kqueue`, `guest_poll`, `guest_select`, `guest_select_wait`, the `guest_socket_*` tests, `guest_tcp`, `guest_udp`, `guest_sce_net`, the `guest_rudp_*` tests and `guest_net_ctl`: 22/22 pass. - `python3 tools/check_conventions.py --base origin/main` passes. - Not tested: Windows and macOS (only Linux was built here), a console, a title. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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
blawesom commented 2026-10-18 10:09:46 +00:00 (Migrated from github.com)

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


Rebase handoff — rebased onto current main (authorship unchanged). The conflicts reconcile main's NOTE_LOWAT evolution with this PR's regular-file read/write events:

  • Kqueue.cpp: main's DescriptorLowWater kept alongside your IsRegularFile, and ModifyKnote carries both the file classification and the low-water mark; your NOTE_LOWAT stub is superseded by main's real support
  • GuestKqueue.cpp: your NoteFilePoll constant added (the duplicate NoteLowat from the merge removed — main already had it)

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/7088
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is 7e4d7a696.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6248599804 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7088#issuecomment-6248599804). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-18T10:09:46Z. --- **Rebase handoff** — rebased onto current `main` (authorship unchanged). The conflicts reconcile `main`'s NOTE_LOWAT evolution with this PR's regular-file read/write events: - `Kqueue.cpp`: `main`'s `DescriptorLowWater` kept alongside your `IsRegularFile`, and `ModifyKnote` carries both the file classification and the low-water mark; your `NOTE_LOWAT` stub is superseded by `main`'s real support - `GuestKqueue.cpp`: your `NoteFilePoll` constant added (the duplicate `NoteLowat` from the merge removed — `main` already had it) 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/7088 git checkout <your branch> git merge --ff-only FETCH_HEAD ``` Head is `7e4d7a696`.
thuanquang commented 2026-10-18 21:30:27 +00:00 (Migrated from github.com)

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


The new RegularFile in GuestKqueue.cpp takes start after it creates the writer 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 write. 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 writer here keeps the new check from flaking the same way.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6255383294 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7088#issuecomment-6255383294). Originally posted by [thuanquang](https://github.com/thuanquang) on 2026-10-18T21:30:27Z. --- The new `RegularFile` in `GuestKqueue.cpp` takes `start` after it creates the writer 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 write. `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 writer here keeps the new check from flaking the same way.
blawesom commented 2026-10-22 06:33:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-22T06:33:35Z.


Rebase handoff — rebased onto current main (d6eafa54d0, authorship unchanged; @adriadam10's fixup replayed cleanly on top). The conflicts reconcile main's kqueue evolution with this PR's regular-file events:

  • Kqueue.cpp: main's PollKqueue/KqueueReaches/AddKqueueKnote (kevent on a kqueue) kept alongside your IsRegularFile; AddDescriptorKnote carries both your regular-file branch and main's DescriptorLowWater path.
  • Udp.cpp (GuestSockets::WaitAny): your fstat gate in the non-Windows branch with the fixup's final condition (fstat != 0 || (!S_ISREG && !S_ISDIR)).
  • docs/dev/debt/libkernel.md: the kqueue and kevent entry keeps main's current text (kevent on a kqueue, the pipe write end, the write-mark wording) with your two edits applied — "guest sockets and regular files (see the kevent on a regular file entry)" and "and on a regular file" in the EV_CLEAR throw list; your kevent on a regular file entry follows main's new kevent on a kqueue entry, and the older pipe-entry wording your base carried is dropped.

Also for @thuanquang: this head takes start before creating the writer thread in RegularFile (the ordering #7670 applies), so the timing-flake concern from your comment is addressed.

Verified on Linux (CachyOS, kernel 7.2.10, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, Ryzen 7 9800X3D): builds; conventions pass (two pre-existing [debt-length] warnings on the kqueue entries); the tested set passes 27/27 — guest_kqueue (including the new RegularFile case, 3.8 s), guest_equeue_events, guest_poll, guest_select, guest_select_wait, the guest_socket_* set, guest_tcp, guest_udp, guest_sce_net, the guest_rudp_* set and guest_net_ctl.

To pick it up:

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

Head is 052ad87d56.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6334400036 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7088#issuecomment-6334400036). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-22T06:33:35Z. --- **Rebase handoff** — rebased onto current `main` (`d6eafa54d0`, authorship unchanged; @adriadam10's fixup replayed cleanly on top). The conflicts reconcile `main`'s kqueue evolution with this PR's regular-file events: - `Kqueue.cpp`: `main`'s `PollKqueue`/`KqueueReaches`/`AddKqueueKnote` (kevent on a kqueue) kept alongside your `IsRegularFile`; `AddDescriptorKnote` carries both your regular-file branch and `main`'s `DescriptorLowWater` path. - `Udp.cpp` (`GuestSockets::WaitAny`): your `fstat` gate in the non-Windows branch with the fixup's final condition (`fstat != 0 || (!S_ISREG && !S_ISDIR)`). - `docs/dev/debt/libkernel.md`: the *kqueue and kevent* entry keeps `main`'s current text (kevent on a kqueue, the pipe write end, the write-mark wording) with your two edits applied — "guest sockets and regular files (see the kevent on a regular file entry)" and "and on a regular file" in the `EV_CLEAR` throw list; your *kevent on a regular file* entry follows `main`'s new *kevent on a kqueue* entry, and the older pipe-entry wording your base carried is dropped. Also for @thuanquang: this head takes `start` before creating the writer thread in `RegularFile` (the ordering #7670 applies), so the timing-flake concern from your comment is addressed. Verified on Linux (CachyOS, kernel 7.2.10, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`, Ryzen 7 9800X3D): builds; conventions pass (two pre-existing `[debt-length]` warnings on the kqueue entries); the tested set passes **27/27** — `guest_kqueue` (including the new `RegularFile` case, 3.8 s), `guest_equeue_events`, `guest_poll`, `guest_select`, `guest_select_wait`, the `guest_socket_*` set, `guest_tcp`, `guest_udp`, `guest_sce_net`, the `guest_rudp_*` set and `guest_net_ctl`. **To pick it up:** ``` git fetch https://github.com/blawesom/AnyPS5 rebase/7088 git checkout <your branch> git merge --ff-only FETCH_HEAD ``` Head is `052ad87d56`.
MarcZX10 commented 2026-11-23 02:50:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T02:50:16Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the review is complete for all included changed files, and the inspected code supports merging.

What I ran

Reviewed head f4155ddf0711db1cb09dd64459065c46ac6dbe8e using repo_read.py to read the prepared diff and targeted repository source. No build or test commands were run. The author reports Linux tests and a conventions check in the PR description; these were not run by me.

Results

Check Result Note
Diff and targeted source review passed All six changed files read
Build and tests not run Author-reported results only

Findings

Blocking

None.

Non-blocking

None.

Not verified

The six changed files listed as reviewed in manifest.json were read: Equeue.hpp, FileEvent.cpp, Kqueue.cpp, Udp.cpp, GuestKqueue.cpp, and docs/dev/debt/libkernel.md. files.json was not available through the reader, so excluded-file details were not verified. Windows, macOS, console, and title behavior were not verified; no build or tests were run. Author-reported Linux test results are in description.md.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6016033344 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7088#pullrequestreview-6016033344). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-23T02:50:16Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the review is complete for all included changed files, and the inspected code supports merging. ## What I ran Reviewed head `f4155ddf0711db1cb09dd64459065c46ac6dbe8e` using `repo_read.py` to read the prepared diff and targeted repository source. No build or test commands were run. The author reports Linux tests and a conventions check in the PR description; these were not run by me. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted source review | passed | All six changed files read | | Build and tests | not run | Author-reported results only | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The six changed files listed as reviewed in `manifest.json` were read: `Equeue.hpp`, `FileEvent.cpp`, `Kqueue.cpp`, `Udp.cpp`, `GuestKqueue.cpp`, and `docs/dev/debt/libkernel.md`. `files.json` was not available through the reader, so excluded-file details were not verified. Windows, macOS, console, and title behavior were not verified; no build or tests were run. Author-reported Linux test results are in `description.md`. ## To get this to a good state Nothing is required.

Pull request closed

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!7088
No description provided.