Skip to content

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

Closed
inmert wants to merge 4 commits into
boykopovar:mainfrom
inmert:feat/kevent-regular-file
Closed

inmert wants to merge 4 commits into
boykopovar:mainfrom
inmert:feat/kevent-regular-file

Conversation

@inmert

@inmert inmert commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

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

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

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 6149 docs/dev/debt/libkernel.md
PR 6936 docs/dev/debt/libkernel.md
PR 7121 core/libs/prx/libkernel/Equeue/Equeue.hpp
core/libs/prx/libkernel/Equeue/src/FileEvent.cpp
core/libs/prx/libkernel/Equeue/src/Kqueue.cpp
core/libs/tests/GuestKqueue.cpp
docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7447 docs/dev/debt/libkernel.md
PR 8605 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 9156 docs/dev/debt/libkernel.md
PR 9293 docs/dev/debt/libkernel.md
PR 9297 docs/dev/debt/libkernel.md
PR 9302 docs/dev/debt/libkernel.md
PR 9389 docs/dev/debt/libkernel.md
PR 9557 docs/dev/debt/libkernel.md
PR 9566 docs/dev/debt/libkernel.md
PR 9938 docs/dev/debt/libkernel.md
PR 10558 docs/dev/debt/libkernel.md
PR 11215 docs/dev/debt/libkernel.md
PR 11661 docs/dev/debt/libkernel.md
PR 11689 docs/dev/debt/libkernel.md
PR 11698 docs/dev/debt/libkernel.md
PR 11896 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 12682 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12688 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12700 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12965 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 14103 docs/dev/debt/libkernel.md
PR 15037 docs/dev/debt/libkernel.md
PR 15040 docs/dev/debt/libkernel.md
PR 16790 docs/dev/debt/libkernel.md
PR 16826 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 18190 core/libs/prx/libkernel/Equeue/src/Kqueue.cpp
core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 18197 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md

@blawesom

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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.

@adriadam10 adriadam10 self-assigned this Oct 19, 2026
@adriadam10
adriadam10 force-pushed the feat/kevent-regular-file branch from 8cbb8cc to 0906066 Compare October 19, 2026 13:17
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 19, 2026
@adriadam10
adriadam10 force-pushed the feat/kevent-regular-file branch from 0906066 to 8c9e2b2 Compare October 20, 2026 08:53
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 20, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 20, 2026
@B-Howell

B-Howell commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

For whoever picks up the rebase Dean asked for on 27 Oct: I merged 8c9e2b25fb onto main b28bb77217 locally to see how far the code is from landing. Nothing was pushed.

Code conflicts are small:

  • Equeue/src/Kqueue.cpp: main added the kqueue-on-kqueue helpers where this PR adds IsRegularFile, so I kept both.
  • Socket/src/Udp.cpp: both sides changed the same entries.push_back in GuestSockets::WaitAny, and this PR's line (the fstat gate plus interest.write ? WriteMask : ReadMask) already contains main's change.
  • tests/GuestKqueue.cpp: main replaced the extern "C" block with prx/libkernel/include/Api.hpp, which already declares lseek_nid_postfix. I took main's side.

The debt file is the real work: 940 commits on main have changed docs/dev/debt/libkernel.md since this branch's base, and this PR's edits to the kqueue and kevent entry overlap main's rewrite of it. For the test build I kept main's debt file, so the entry still has to be rewritten by hand.

On that merge: WSL2 Ubuntu 22.04 (GCC 11.4.0) and Windows 11 Pro 26300 (WinLibs GCC 15.2.0 r7), both Release. guest_kqueue, guest_kqueue_cleared_race, guest_equeue_events, guest_socket_events, guest_tcp and guest_udp pass 3 of 3 on both. Control on Windows: with main's Kqueue.cpp, FileEvent.cpp, Equeue.hpp, File.hpp and Udp.cpp, guest_kqueue aborts with kevent: EVFILT_READ on a descriptor that is neither a socket nor the read end of a pipe not implemented.

The Require(Throws(... EvfiltWrite, EvAdd | EvClear ...)) line that Dean asked to drop is still in RegularFile. thuanquang's timing point is fixed on this head: start is now taken before the writer thread is created.

The game threads that hit this message (Java 25 Networking, EA SPORTS FC 25 and FC 27) were fixed by #5973 and #6343, the pipe and standard-input cases. What is left is the homebrew import count in #7971 (four titles import kevent). So no known title is waiting on this PR.

AI-assisted: yes (Claude Code)

@adriadam10
adriadam10 force-pushed the feat/kevent-regular-file branch from 8c9e2b2 to f4155dd Compare November 8, 2026 01:39
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@inmert @adriadam10 Two things block this after main moved. On Windows, Udp.cpp WaitAny now slices only for FILE_TYPE_PIPE, but main's Windows EV_CLEAR edge watch (EdgeWatch.cpp:89) waits on a dummy descriptor INT_MAX and relies on that slicing; _get_osfhandle(INT_MAX) also hits the invalid parameter handler. Could you keep slicing for any descriptor that is not a regular file or directory? Second, PollFileRead calls sceKernelLseek under the equeue mutex, and on Windows that waits for the descriptor's position lock during a read or write. #9099 moved the same measurement outside the lock with MeasureFiles; could the kevent read event reuse it? The debt file then needs a rebase, and the new entry should drop "still refuse non-socket descriptors" and "as on sockets and pipes", which main no longer matches.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

I am closing this for now. The review asked for a rebase and for the new test line that pins the unimplemented EV_CLEAR throw on a regular file to be dropped, and there has been no push since 28 Oct. The branch also conflicts with main, and the GuestSockets::WaitAny change needs a fresh read on the rebased head. You are welcome to reopen or push a rebased branch at any time and we will look again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs libkernel needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants