feat(libkernel): answer sceKernelAddReadEvent and sceKernelAddWriteEvent on regular files #9099

Merged
aarushkha merged 9 commits from upstream-pr/9099 into main 2026-10-24 17:33:30 +00:00
aarushkha commented 2026-10-21 12:14:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by aarushkha on 2026-10-21T12:14:40Z.


What

A copy of #8584, which the open PR limit bot closed and its author cannot reopen. It includes the fixes for oneandonlydean's review there.

sceKernelAddReadEvent and sceKernelAddWriteEvent threw for a regular file: they only accepted guest sockets and the read end of a pipe. They now follow FreeBSD 11.4's vnode filters filt_vfsread and filt_vfswrite:

  • A read event reports the file's size less the descriptor's offset in data (negative past the end) and fires while that is not 0.
  • A write event always fires, with data 0.

FreeBSD activates these knotes when the vnode changes. No host call reports every file change, so WaitForDescriptors re-polls an equeue that holds a regular-file event every millisecond (a new KernelFilter::repoll flag), instead of handing the file to poll/WSAPoll. A write made through another descriptor is therefore seen up to 1 ms late. A size argument above 1, directories and other vnode types still throw, as recorded in docs/dev/debt/libkernel.md. Whether the console's own file systems answer these filters as vfs_kqfilter does was not measured.

Changes from the review of #8584:

  • The read event's sceKernelFstat and sceKernelLseek no longer run under the equeue's m_mutex. Before PollEvents, MeasureFiles copies the read events to measure, releases m_mutex, measures them, then stores the results under m_mutex. On Windows, lseek waits for the descriptor's position lock, so a long read or write on the file used to block every operation on the equeue, and a close taking the two locks in the opposite order could deadlock. Each read event keeps its result in its own state object (filter.owner). A descriptor closed and reused during the measurement gets a new object, so the old file's size is never reported for it.
  • sceKernelClose and close now call EqueueDescriptorClosed before they take LockPositionedTransfer, as dup2 already did, so the equeue mutex is always taken before the position lock.

kevent on regular files is left to #7088, which implements the same FreeBSD filters there. This PR only touches the equeue path (Equeue.cpp), which #7088 does not change. If #7088 lands first, its file classification could replace RegularFile here.

#8523 edits the same lines of WaitForDescriptors, so whichever merges second needs a small rebase.

Tested

Windows 10 Pro (10.0.19045), WinLibs GCC 15.2.0 posix UCRT r7, Release, -DBUILD_TESTING=ON, on main 27c88a8d75.

  • guest_socket_events has two new cases. RegularFileEvents covers a read event with the offset mid-file (data 8) and at the end (times out), woken when a second descriptor appends 3 bytes (data 3); a write event (data 0); a size of 64 throws; after close the event is gone (ENOENT) and a new add fails with EBADF. CloseWhileWaitingOnFile has one thread wait on the equeue while another opens a file at end of file, adds a read event and closes it, 2000 times.
  • With only the new tests on main's Equeue.cpp, guest_socket_events aborts (0xc0000409). With the first version of #8584 (before the review fixes), CloseWhileWaitingOnFile deadlocked and hit the 20 s ctest timeout 3 of 3 times. With this PR it passes in about 0.6 s (6 of 6 runs on this base).
  • With this PR, these pass: guest_socket_events, guest_equeue_events, guest_kqueue, guest_positioned_race, guest_positioned_close, guest_dup, guest_directory_close, guest_file_vector, guest_aio, guest_aio_bad_descriptors, guest_filesystem, guest_posix_filesystem, guest_socket_descriptors, guest_sendfile, file_position and file_streams. tools/check_conventions.py --base origin/main passes.
  • Not tested: a long read or write on the descriptor while an equeue waits (holding a transfer in progress for a set time in a test would be unreliable), Linux and macOS, and 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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4951365365 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9099). Originally posted by [aarushkha](https://github.com/aarushkha) on 2026-10-21T12:14:40Z. --- ### What A copy of #8584, which the open PR limit bot closed and its author cannot reopen. It includes the fixes for oneandonlydean's review there. `sceKernelAddReadEvent` and `sceKernelAddWriteEvent` threw for a regular file: they only accepted guest sockets and the read end of a pipe. They now follow FreeBSD 11.4's vnode filters [`filt_vfsread` and `filt_vfswrite`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_subr.c#L5122-L5168): - A read event reports the file's size less the descriptor's offset in `data` (negative past the end) and fires while that is not 0. - A write event always fires, with `data` 0. FreeBSD activates these knotes when the vnode changes. No host call reports every file change, so `WaitForDescriptors` re-polls an equeue that holds a regular-file event every millisecond (a new `KernelFilter::repoll` flag), instead of handing the file to `poll`/`WSAPoll`. A write made through another descriptor is therefore seen up to 1 ms late. A size argument above 1, directories and other vnode types still throw, as recorded in `docs/dev/debt/libkernel.md`. Whether the console's own file systems answer these filters as `vfs_kqfilter` does was not measured. Changes from the review of #8584: - The read event's `sceKernelFstat` and `sceKernelLseek` no longer run under the equeue's `m_mutex`. Before `PollEvents`, `MeasureFiles` copies the read events to measure, releases `m_mutex`, measures them, then stores the results under `m_mutex`. On Windows, `lseek` waits for the descriptor's position lock, so a long read or write on the file used to block every operation on the equeue, and a `close` taking the two locks in the opposite order could deadlock. Each read event keeps its result in its own state object (`filter.owner`). A descriptor closed and reused during the measurement gets a new object, so the old file's size is never reported for it. - `sceKernelClose` and `close` now call `EqueueDescriptorClosed` before they take `LockPositionedTransfer`, as `dup2` already did, so the equeue mutex is always taken before the position lock. `kevent` on regular files is left to #7088, which implements the same FreeBSD filters there. This PR only touches the equeue path (`Equeue.cpp`), which #7088 does not change. If #7088 lands first, its file classification could replace `RegularFile` here. #8523 edits the same lines of `WaitForDescriptors`, so whichever merges second needs a small rebase. ### Tested Windows 10 Pro (10.0.19045), WinLibs GCC 15.2.0 posix UCRT r7, Release, `-DBUILD_TESTING=ON`, on `main` `27c88a8d75`. - `guest_socket_events` has two new cases. `RegularFileEvents` covers a read event with the offset mid-file (`data` 8) and at the end (times out), woken when a second descriptor appends 3 bytes (`data` 3); a write event (`data` 0); a size of 64 throws; after `close` the event is gone (`ENOENT`) and a new add fails with `EBADF`. `CloseWhileWaitingOnFile` has one thread wait on the equeue while another opens a file at end of file, adds a read event and closes it, 2000 times. - With only the new tests on `main`'s `Equeue.cpp`, `guest_socket_events` aborts (`0xc0000409`). With the first version of #8584 (before the review fixes), `CloseWhileWaitingOnFile` deadlocked and hit the 20 s ctest timeout 3 of 3 times. With this PR it passes in about 0.6 s (6 of 6 runs on this base). - With this PR, these pass: `guest_socket_events`, `guest_equeue_events`, `guest_kqueue`, `guest_positioned_race`, `guest_positioned_close`, `guest_dup`, `guest_directory_close`, `guest_file_vector`, `guest_aio`, `guest_aio_bad_descriptors`, `guest_filesystem`, `guest_posix_filesystem`, `guest_socket_descriptors`, `guest_sendfile`, `file_position` and `file_streams`. `tools/check_conventions.py --base origin/main` passes. - Not tested: a long read or write on the descriptor while an equeue waits (holding a transfer in progress for a set time in a test would be unreliable), Linux and macOS, and 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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
harisibrar1 commented 2026-10-24 13:39:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:39:29Z.

Review: COMMENTED


Checked against main 522e32e3b6 and FreeBSD 11.4 sys/kern/vfs_subr.c (filt_vfsread/filt_vfswrite, vop_*_post) and sys/kern/kern_event.c (kqueue_scan):

  • It still merges cleanly onto main (3-way merge-tree, no conflicts), and main's AddDescriptorEvent (Equeue.cpp:710-725) still throws for files, so this isn't duplicated.
  • The data values match filt_vfsread/filt_vfswrite exactly (vfs_subr.c:5123-5168), and the close/dup2 lock order now agrees with main's Windows NativeLseek (Open.cpp:50) taking the position lock.
  • One difference from FreeBSD worth adding to the debt entry: in kqueue_scan (kern_event.c:1782-1790), a level-triggered read knote that finds data 0 is deactivated, and only a vnode KNOTE re-activates it (read, write, open, close, setattr…); lseek posts none. So with a 10-byte file at offset 10, a read event, then sceKernelLseek(fd, 0, SEEK_SET), FreeBSD keeps waiting until something touches the vnode, while the 1 ms repoll here reports data 10. The entry currently describes the polling as only costing up to 1 ms of latency. Console behaviour is unmeasured either way, so documenting it seems enough.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638442676 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9099#pullrequestreview-5638442676). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T13:39:29Z. **Review:** COMMENTED --- Checked against main 522e32e3b6 and FreeBSD 11.4 sys/kern/vfs_subr.c (filt_vfsread/filt_vfswrite, vop_*_post) and sys/kern/kern_event.c (kqueue_scan): - It still merges cleanly onto main (3-way merge-tree, no conflicts), and main's `AddDescriptorEvent` (Equeue.cpp:710-725) still throws for files, so this isn't duplicated. - The data values match `filt_vfsread`/`filt_vfswrite` exactly (vfs_subr.c:5123-5168), and the close/dup2 lock order now agrees with main's Windows `NativeLseek` (Open.cpp:50) taking the position lock. - One difference from FreeBSD worth adding to the debt entry: in `kqueue_scan` (kern_event.c:1782-1790), a level-triggered read knote that finds data 0 is deactivated, and only a vnode KNOTE re-activates it (read, write, open, close, setattr…); lseek posts none. So with a 10-byte file at offset 10, a read event, then `sceKernelLseek(fd, 0, SEEK_SET)`, FreeBSD keeps waiting until something touches the vnode, while the 1 ms repoll here reports `data` 10. The entry currently describes the polling as only costing up to 1 ms of latency. Console behaviour is unmeasured either way, so documenting it seems enough. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!9099
No description provided.