fix(libkernel): handle kevent timer re-adds and the AIO, LIO, SENDFILE and PROCDESC filters as measured on a PS5 #7653
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7653"
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 ionut27001 on 2026-10-18T20:28:16Z.
What
Two
keventpaths that threw now answer the way payload processes on a PS5 (firmware 12.70) got them.Re-adding an existing timer. In the payload process,
EV_ADDon an existing periodicEVFILT_TIMER, withfflags0 and a positive period (milliseconds), kept the armed expiry and the expirations already counted, and applied the new period after that expiry. The debt entry already named the two FreeBSD candidates (11.0 keeps the expiry, 11.4'sfilt_timertouchrestarts the timer); the re-added timers behaved like 11.0's.mainbefore this PREV_ADD | EV_ONESHOTTo keep the counted expirations,
EditEventnow counts the expired periods of a timer before it edits it, asAddEventalready did. Re-adding a one-shot or disabled timer (anEV_DISPATCHtimer that has reported is disabled), a re-add with nonzerofflags(NOTE_MSECONDSincluded), a period of 0 or less, a period too large to add to the armed expiry or flags other thanEV_ONESHOTwere not measured and still throw, and every other change to an existing timer exceptEV_DELETEstill throws.Filters with no attach here. These filters threw
NotImplemented:EV_ADDofEVFILT_AIO,EVFILT_LIOorEVFILT_SENDFILEat idents 0, 5, 999, 2^40 and an open pipe, and at ident 5 also withEV_CLEAR,EV_ONESHOTorEV_DISABLEEPERMEV_ADDofEVFILT_PROCDESCon a descriptor that is not open (999 and a closed one) or an ident aboveINT_MAXEBADFEV_ADDofEVFILT_PROCDESCon Unix-domain stream and datagram sockets, descriptor 0 (a socket), anAF_INETUDP socket, both ends of a pipe, a regular file, a directory, another kqueue and the kqueue itselfEINVALEINVALon these kinds of descriptor (sockets, pipes, regular files, directories, kqueues), since there are no process descriptors here, and on shared memory and JIT shared memory, which are host files here (not measured; FreeBSD 11.0 answersEINVALforshm_openobjects); any other kind, such as/dev/randomand/dev/urandom, throws, and so do descriptors 0 to 2 that are not sockets or kqueuesEV_DELETEandEV_ENABLEof the first three at ident 5,EV_DELETEofEVFILT_PROCDESCon a socketENOENTENOENTfor every other change withoutEV_ADD, after the descriptor check forEVFILT_PROCDESC, as for any other filter without a knote;EV_ENABLEtogether withEV_DISABLEisEINVALbefore that, as for every filterWhen
EV_ADDof one of these filters fails withEPERM, or withEINVALon a descriptor other than the kqueue itself, its error event hasfflags0 and the errno indata, as FreeBSD 11.0'skqueue_registerclearsfflagsanddatabefore the attach andkqueue_keventthen stores the error indata; a descriptor that is not open, the kqueue itself andEV_ENABLEwithEV_DISABLEfail before that and keep theirfflags. That was not measured.EVFILT_PROCon the probe's own process withNOTE_EXIT,EVFILT_SIGNALforSIGUSR1andEVFILT_FSat ident 0 were accepted in the payload process and are not changed here; they still throw.Debt: in
docs/dev/debt/libkernel.md, a newkevent measurementsentry records the measurements above, and thekqueue and kevententry points to it: it now says that nothing here is taken from a console except what that entry lists, and it no longer lists these filters and the timer re-adds that entry describes among the cases that throw. The new entry also records what the payload process got in cases that still throw here, all on Unix-domain socketpairs except the last: aNOTE_LOWATmark of 0 on an empty stream socket (a read event at once,data0, and the same after re-adding that knote with -1), a write mark of 2 on the other end of that socketpair (data8192) and a re-add of that knote with 2^30 (no event), a mark of 5 on a datagram socket (no event after a 1-byte datagram, an event withdata11 after a second, 10-byte one), andEVFILT_READon another kqueue (datais the number of events pending in it: 1, then 2). Thekqueue and kevententry also listed aNOTE_NSECONDSperiod above one second with a fraction among the cases that throw;d71a1c577made it work, so that clause now says how such a period is used.Overlaps: none of the open PRs changes the timer re-add or these filters, but several touch the same lines:
mainfd423f9b2conflicted with it in the include block ofKqueue.cppand in thekqueue and keventdebt line; the resolution keeps itsHostStreams.hppinclude and its sentence about a read event on the host stream on descriptor 0, next to this PR's changes.maineaae7e069conflicted with it in the debt file, where it changed theJIT shared memoryline right before thekqueue and keventline this PR changes; the resolution keeps itsJIT shared memoryline and this PR'skqueue and keventline, so this PR's change to the debt file is the same as before the rebase.JIT shared memorydebt line. Merged withmaineaae7e069it conflicts in the debt file and three other files, and merged with this PR instead it conflicts in the same files and no others.ModifyKnoteso that a timer change withoutEV_ADDand withdata0 no longer throws. This PR handles theEV_ADDre-add inRegister, beforeModifyKnote. Its check that another change to an existing timer throws passesdata5, which throws with or without that change. Whichever lands second rewords "every change to an existing timer exceptEV_DELETEand theEV_ADDre-adds that the kevent measurements entry describes" in thekqueue and kevententry and "other changes to an existing timer" in thekevent measurementsentry.main: #7075, #7121 and #7509 in the debt file, #7049 inGuestKqueue.cppand the debt file, #7129 inKqueue.cppand the debt file, and #7088 inKqueue.cpp,GuestKqueue.cppand the debt file. Merged with this PR instead ofmain, each conflicts in the same files and no others.How the console was measured
resolvelines show that thekeventall three probes call, and thekqueuep72 and p75 call, are the oneslibkernel_sys.sprxexports; p73 did not checkkqueue.EVFILT_PROCDESCon descriptor 0, which the payload manager opened, andEVFILT_FS,EVFILT_AIO,EVFILT_LIOandEVFILT_SENDFILEat ident 0. The cases of one group share a kqueue, and some re-add a knote that an earlier case registered. p72 logs the return value anderrnoof everykeventcall and every returned event.first_at_msis when the first event came, counted from the start of that window: the 400 ms window starts right after the re-add and the 200 ms window right after the 400 ms one.fstattype of descriptor 0 and of one descriptor of each kind it creates (sockfor descriptor 0 and the sockets,fifofor the pipe and for a kqueue,regfor the file,dirfor the directory). It then addsEVFILT_PROCDESCon descriptor 0, one end of a Unix-domain stream and of a datagram socketpair, anAF_INETUDP socket, both pipe ends, a regular file, a directory, a second kqueue, the kqueue itself, a closed descriptor and ident 2^40, and deletes it on the stream socket. It adds the other three filters at idents 0, 5, 999, 2^40 and an open pipe, at ident 5 also withEV_CLEAR,EV_ONESHOTandEV_DISABLE, and deletes and enables them at ident 5. It removes its temporary file at the end, and its UDP socket is never bound or used.EVFILT_PROCwas registered on the probe's own process forNOTE_EXITandEVFILT_SIGNALonSIGUSR1; no signal was sent. No filter below -12 other than -1000 was tried.Console output (excerpt, lines copied unchanged)
The
pNN:prefix names the probe; the rest of each line is copied unchanged.Tested
Build: Windows 11 x86-64, full build with WinLibs MinGW-w64 GCC 15.2.0 (posix-seh, ucrt, r7) and
-DBUILD_TESTING=ON.Tests: after #6542 landed, the branch was rebased onto
maineaae7e069(the conflict is described under Overlaps). With this branch on thatmain, theguest_*tests pass: 498 of 499, andguest_video_out_wqhd_detection_no_vulkanis skipped on this machine. That is the second of two full runs. The first, right after the build, took 898 s instead of the second run's 69 s; in it seven tests timed out, none of them a kqueue or kevent test, andguest_itimerfailed;guest_itimerpassed in the second run and then 5 runs in a row.guest_kqueuepassed in both full runs and in 3 more runs in a row.Temporary file names:
TemporaryNameinGuestKqueue.cppnow puts the process id in each name. A reviewer saw one Linux copy ofProcdescOnFilesfail to remove its temporary file while 8 copies ran at once; with the process id in the name, copies that run at the same time no longer share a file. Since its timer checks depend on timing,guest_kqueuealso ran as 8 copies at once, three times: the timer and filter checks passed in all 24 copies. Five copies stopped later, in the existing check that watches a deleted file, where Windows refused the watch on$Deleted(FindFirstChangeNotificationerror 5) while the 8 copies ran, with a different file name in each copy.Old code, new test: with
Kqueue.cppandEqueue.cppreverted to thatmainand the new tests kept,guest_kqueueaborts withkevent: changing an existing EVFILT_TIMER not implemented.Console: behaviour measured on a PS5 (firmware 12.70), as described above.
Not tested here: Linux (a reviewer reported a Linux run in the comments), and no title.
Rebased onto main
6696def6e(the debt entry merged with main's pipe write-end and write low-water mark sentences; no code conflict). Re-tested on Windows 11, WinLibs GCC 15.2.0 r7:ctest -R "guest_kqueue|guest_kevent|guest_equeue"twice, 2 of 2 pass both times (guest_kqueue,guest_equeue_events); revert check withEqueue.cppandKqueue.cpprestored from the previous commit:guest_kqueueaborts.Checklist
Based on current
main; no other open PR implements the same functionsOne 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 fileNo notes, investigation
.mdfiles 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
Rebase: rebased onto
main1e774a3b22after the conflicts reported in the comments. The only conflict was thekqueue and keventdebt line, wheremain(#6343) added the sentence about a read event on the host stream on descriptor 0 and this PR rewrites the same line; the resolution keeps both, next to theHostStreams.hppinclude, which now merges without a conflict. The code and test changes are otherwise the same as before the rebase. Re-tested on the new base: full build with WinLibs GCC 15.2.0 (Windows 11), exit 0;guest_kqueuepassed twice (3.56 s, 3.55 s) andguest_equeue_eventstwice (1.67 s, 0.48 s); withEqueue.cppandKqueue.cpprestored frommainthe test aborts withkevent: changing an existing EVFILT_TIMER not implemented.Depends on: none
Fixes: none
Unblocks: none
GPUs tested: n/a
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T01:09:56Z.
mainafter #6343 merged. The conflicts are in the include block ofKqueue.cpp(HostStreams.hppagainst this PR's added includes; keeping both works) and in thekqueue and keventdebt line, which #6343 extended for descriptor 0.a373e85e5:guest_kqueuepasses, and allguest_*tests pass (474/474). WithKqueue.cppandEqueue.cppreverted it aborts withkevent: changing an existing EVFILT_TIMER not implemented.FileWatchPoll(...) == 0afterftruncate, which also fails onmainunder the same load (3 of 128).ProcdescOnFiles, atRequire(std::filesystem::remove(path))for its temporary file. I haven't found the cause, and it never failed in serial runs.kern_event.c#L1345-L1357only copiesfflagsanddata, and doesn't applyEV_ONESHOT.filt_timerexpire(#L635-L651) adds the new period to the armedp_nexttime, which gives the console's 46 ms / 5 ms first event and the kept count.filt_timertouch(#L744-L794) drains the callout, setskn_data = 0and restarts.EV_ADD | EV_ENABLE, a common idiom, throws here, since onlyEV_ONESHOTis allowed.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-19T08:57:50Z.
Rebase handoff — rebased onto current
maincleanly (authorship unchanged).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; full suite green apart from the four knownmainregressions (#5444 Mesa, #7584 BDA, #7766, #7784), none from this PR.To pick it up:
Head is
5c31f9772.Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-19T19:19:33Z.
Rebase handoff — rebased onto current
main(585075cb7, authorship unchanged). One debt-entry conflict: your rewrite of the kqueue entry vsmain'scb9a4c741(EV_CLEAR on sockets and pipes on Linux), which postdates your branch — I kept your rewrite (the AIO/LIO/SENDFILE/PROCDESC filters, the timer re-add rules, the nanosecond period) and restoredmain's "off Linux (see the kevent EV_CLEAR entry)" clause, which your base predates.Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release,
BUILD_TESTING=ON, AMD RX 9070 XT / RADV): builds;guest_kqueue(3.9 s, including your 121 new test lines) andguest_aiopass; full suite green apart from the four knownmainregressions (#5444 Mesa, #7584 BDA, #7766, #7784), none from this PR; conventions pass (two[debt-length]findings on the rewritten entries, warnings only).One observation for reviewers: your
NotImplementedfor "kevent: re-adding a disabled EVFILT_TIMER" is the same corner I flagged on #7049's review (a timer'sEV_ENABLEneeds to re-arm the expiry); whatever fills that gap can supersede both throw sites.To pick it up:
Head is
4e12b0d86.Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-22T06:34:36Z.
Rebase handoff — rebased onto current
main(d6eafa54d0, authorship unchanged). No code conflict this time; the one conflict is the kqueue and kevent debt entry, resolved as follows:main's current text: the "nothing here is taken from a console except what the kevent measurements entry lists" opener, theEVFILT_AIO/EVFILT_LIO/EVFILT_SENDFILE/EVFILT_PROCDESCexceptions with the measurements cross-reference, and the timer re-add sentence with the nanosecond-period note.6696def6e) predatesmain's6ead14f9(kevent on a kqueue), so your throw list still named "EVFILT_READon another kqueue"; that clause is dropped, since the rebased tree keepsmain'sAddKqueueKnotesupport and its own kevent on a kqueue entry. Everything else frommain's evolved entry (pipe write end, write low-water marks) is kept.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);guest_kqueue(4.3 s, including your 121 test lines),guest_equeue_eventsandguest_aiopass.The earlier observation stands: your
NotImplementedfor the disabled-timer re-arm is the same corner flagged on #7049 (whose timer modify relaxation kept a throw for it), so whatever fills that gap can supersede both throw sites.To pick it up:
Head is
c67f21d56a.