feat(libkernel): support kevent read and write events on regular files #7088
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7088"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by inmert on 2026-10-17T10:59:58Z.
What
keventthrew forEVFILT_READandEVFILT_WRITEon a regular file:Both now follow FreeBSD 11's
vfs_kqfilter,filt_vfsreadandfilt_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 withNOTE_FILE_POLL.NOTE_LOWATanddataare ignored, and adding the event again replaces itsfflags, askqueue_registerdoes for a filter withoutf_touch. A write event always fires withdata0. Size and offset come fromsceKernelFstatandsceKernelLseek, so files served fromapp0.zarwork as well.A waiting
keventis woken by the host watchEVFILT_VNODEalready uses, so a write from another descriptor or process wakes it. Files fromapp0.zarnever change and get no watch.GuestSockets::WaitAnyalso needed a change. It passed every interest that is not a socket toppoll, and the host reports a regular file (and the null device behind an archived file) ready at once, so akeventwaiting 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_CLEARon a regular file still throws, as it does on sockets and pipes, and so do directories (theirva_sizeis 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 itsinterest.write ? WriteMask : ReadMaskthere, so whichever lands second only rebases that line. #7049 changes the lines next to mine inModifyKnote; the two don't interact. #7092 (NOTE_LOWATon sockets) conflicts inModifyKnoteand in the kqueue debt sentence (git merge-treeof the two heads). They don't clash in behaviour: itsDescriptorLowWateronly applies to sockets, and a regular file ignores the mark asfilt_vfsreaddoes, so the resolution is to keep itsDescriptorLowWatercall in place of my!fileguard, and to keep both edits to the debt sentence. #6343 (descriptor 0) touches the same branch ofAddDescriptorKnote; 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
mainca82d7d88.RegularFilecase inguest_kqueue:datain the middle and at the end of the file, negativedatapast the end,NOTE_FILE_POLLturned on and off by adding the event again,NOTE_LOWATignored, a blockingkeventwoken by a write through a second descriptor 50 ms later, a truncation,EV_CLEARthrowing, the write event, and the events going away on close. It aborts onmainwith the error above and passes with this change.WaitAnychange 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, theguest_socket_*tests,guest_tcp,guest_udp,guest_sce_net, theguest_rudp_*tests andguest_net_ctl: 22/22 pass.python3 tools/check_conventions.py --base origin/mainpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-18T10:09:46Z.
Rebase handoff — rebased onto current
main(authorship unchanged). The conflicts reconcilemain's NOTE_LOWAT evolution with this PR's regular-file read/write events:Kqueue.cpp:main'sDescriptorLowWaterkept alongside yourIsRegularFile, andModifyKnotecarries both the file classification and the low-water mark; yourNOTE_LOWATstub is superseded bymain's real supportGuestKqueue.cpp: yourNoteFilePollconstant added (the duplicateNoteLowatfrom the merge removed —mainalready 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 knownmainregression (agc_driver_tessellationsegfault — Mesa, #5444), not this PR.To pick it up:
Head is
7e4d7a696.Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-18T21:30:27Z.
The new
RegularFileinGuestKqueue.cpptakesstartafter it creates the writer thread that acts after 50 ms, then requireswaited >= 40 ms. Under parallel load the main thread can be preempted between creating that thread and reading the clock, sowaitedcomes out under 40 ms althoughkeventdid wait for the write.PipeReadin 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 tookstart39 to 68 ms after creating the writer, withkeventreturning 0.3 to 1.4 ms after the write. #7670 movesstartbefore the thread there and inguest_socket_events. Takingstartbefore creating the writer here keeps the new check from flaking the same way.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 reconcilemain's kqueue evolution with this PR's regular-file events:Kqueue.cpp:main'sPollKqueue/KqueueReaches/AddKqueueKnote(kevent on a kqueue) kept alongside yourIsRegularFile;AddDescriptorKnotecarries both your regular-file branch andmain'sDescriptorLowWaterpath.Udp.cpp(GuestSockets::WaitAny): yourfstatgate 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 keepsmain'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 theEV_CLEARthrow list; your kevent on a regular file entry followsmain's new kevent on a kqueue entry, and the older pipe-entry wording your base carried is dropped.Also for @thuanquang: this head takes
startbefore creating the writer thread inRegularFile(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 newRegularFilecase, 3.8 s),guest_equeue_events,guest_poll,guest_select,guest_select_wait, theguest_socket_*set,guest_tcp,guest_udp,guest_sce_net, theguest_rudp_*set andguest_net_ctl.To pick it up:
Head is
052ad87d56.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
f4155ddf0711db1cb09dd64459065c46ac6dbe8eusingrepo_read.pyto 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
Findings
Blocking
None.
Non-blocking
None.
Not verified
The six changed files listed as reviewed in
manifest.jsonwere read:Equeue.hpp,FileEvent.cpp,Kqueue.cpp,Udp.cpp,GuestKqueue.cpp, anddocs/dev/debt/libkernel.md.files.jsonwas 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 indescription.md.To get this to a good state
Nothing is required.
Pull request closed