feat(libkernel): watch a kqueue with EVFILT_READ #8523
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8523"
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 MarrasFederico on 2026-10-20T14:56:19Z.
What
keventthrewnot implementedforEVFILT_READon a kqueue descriptor. FreeBSD 11 lets a kqueue watch another one (kqueue_kqfilterandfilt_kqueue): the watched queue is readable while it has pending events, anddatais their count. This is now supported:datais that count. It is evaluated on every poll or wait, after the watched queue's timers and descriptors are refreshed.keventon the outer queue wakes when the inner one gets an event. The waiter registers with every watched queue, recursively, and rechecks after registering so no wake is lost. It also takes on their nearest timer and their descriptor interests, so an inner timer or socket wakes it too.EV_DISABLE/EV_ENABLE,EV_DISPATCH,EV_ONESHOT,EV_DELETE, and closing the watched queue, which drops the note.EINVAL, askqueue_kqfilterdoes. A note on the queue itself staysEINVAL(L1300-L1303).EV_CLEARon such a note, since FreeBSD re-arms it only when the watched queue queues a note, which is not tracked here. Also a note that would close a cycle (A watches B, B watches A), which no source defines and which would otherwise deadlock.The debt file drops this case from the kqueue entry's throw list and gets a
kevent on a kqueueentry with the throwing cases and how the count differs from FreeBSD. Heredatais the watched queue's enabled notes that are ready when the note is evaluated; FreeBSD'skq_countkeeps counting a queued note until that kqueue is scanned, even once it is disabled or no longer ready, and reports a queuedEV_ONESHOTnote with its last count before deleting it, where here it is evaluated again and, if the watched queue is empty by then, neither reported nor deleted.Tested
Ubuntu 26.04.1, kernel 7.0.0-38, GCC 15.2.0, Release, full build with
BUILD_TESTING=ON. Windows was not compiled. No title was run.guest_kqueue, newWatchKqueuecases:data1 and 2, level-triggered, and partial consumption lowers the count.keventis woken by an inner user trigger (about 50 ms), an inner timer, an inner socket send, and a three-level chain.EV_DISPATCH,EV_ONESHOTandEV_DELETE(thenENOENT) work.EVFILT_WRITEandEVFILT_VNODEon a kqueue returnEINVAL, andEV_CLEARand a cycle throw.EBADFon delete.The test passed five runs in a row. It fails on
main, where the registration throws. Reportingdata1 instead of the count, not registering the waiter with the watched queue, or ignoring the inner queue's timers makes it fail.All 79 tests matching
equeue|kqueue|kevent|pipe|aio|socket|select|pollpass.tools/check_conventions.py --base origin/mainpasses, with one warning: the existingkqueue and keventdebt entry was already over 1000 characters before this edit.tools/pr_overlap.pyfinds no shared function. It reports textual conflicts:Kqueue.cppline 193, next to itsAddDescriptorKnotechange;main()list.Whichever is merged second needs a small rebase, and I will rebase this one if they go first. #4630 could not be compared, as it conflicts with
main; it does not touch kqueue.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 github-actions[bot] on 2026-10-20T15:32:10Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/tests/GuestKqueue.cppL1014docs/dev/debt/libkernel.mdL124docs/dev/debt/libkernel.mdL198-199core/libs/prx/libkernel/Equeue/Equeue.cppL100-102, L107-109, L113-114, L222-223core/libs/prx/libkernel/Equeue/Equeue.hppL58Some 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.
docs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/tests/GuestKqueue.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/Equeue/Equeue.cppcore/libs/prx/libkernel/Equeue/src/Kqueue.cppcore/libs/tests/GuestKqueue.cppdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/Equeue/Equeue.hppcore/libs/prx/libkernel/Equeue/src/Kqueue.cppcore/libs/tests/GuestKqueue.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/Equeue/Equeue.hppcore/libs/prx/libkernel/Equeue/src/Kqueue.cppcore/libs/tests/GuestKqueue.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/Equeue/Equeue.cppcore/libs/prx/libkernel/Equeue/Equeue.hppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdImported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-20T19:04:50Z.
The
kevent on a kqueueentry and the PR body list two differences from FreeBSD. There is a broader one, and the new test pins it.PollKqueuepolls the watched queue again on every evaluation, so a note there stops counting as soon as it is no longer ready. FreeBSD'sfilt_kqueuereportskq_count, which is the number of queued notes.knoteonly ever activates a note. A queued note leaves the queue only when that kqueue is scanned:kqueue_scandrops it once its filter returns 0 and puts a reported level-triggered note back at the tail. Until that scan, a note that is no longer ready still counts.The socket case in
WatchKqueueshows this:kevent(outer)at L444 reportsinnerwithdata1 and puts outer's note back in the queue. Thenrecvemptiespair[0].innerwas not scanned, so its socket note is still queued. On FreeBSDPoll(outer)then returns 1 withdata1, but L449 expects 0.Poll(outer)at L463 puts its note back, so afterPoll(inner)consumes note 5, thekq_countofouteris still 1 andPoll(top)returns 1.This also affects
EV_ONESHOTin a way the entry does not cover.kqueue_scanreports a queuedEV_ONESHOTnote fromkn_keventwithout calling the filter, then deletes it (L1761-L1775). Take a note added while note 5 ofinneris triggered, then callPoll(inner)and thenPoll(outer). On FreeBSD the note is reported withdata1 and deleted. HerePollEventsevaluates it again and gets 0. So it is not reported and stays registered, and a laterEV_DELETEreturns 0 instead ofENOENT.If the live count is meant to stay, I think widening the entry is enough, for example:
The other option is a per-note queued flag so the count follows
kq_count. L449 and L465 would then expect 1.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T23:03:46Z.
@MarrasFederico Thanks, this is a careful piece of work. Two things before the full review:
mainhas moved underdocs/dev/debt/libkernel.md, so the PR now conflicts there (the code itself still merges cleanly). Please rebase.kq_countdecides what the code should be, so it's worth settling first: either keep the live count and widen the debt entry (their suggested text covers the socket andEV_ONESHOTcases), or track a per-note queued flag sodatafollows FreeBSD'skq_count(then the checks atGuestKqueue.cppL449 and L465 would expect 1). Either is fine; the debt entry just needs to match what the code does.Once that's in, I'll review the waiter registration (lock order across nested queues) and check the Windows build, which hasn't been compiled yet.
Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T07:35:25Z.
Windows run of
5f6c30547emerged ontomainb77bd58806(05fafdd7d1), the build the description lists as not done. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release,BUILD_TESTING=ON.The merge conflicts only in
docs/dev/debt/libkernel.md, as GitHub shows; for this run I took this PR's side of that, and the code merged without conflicts. Builtlibkernelandguest_kqueue_testswith no errors or warnings;ctest --repeat until-fail:2onguest_kqueue: passes on both repeats. Not run: the negative control againstmain's library, a title.Imported from boykopovar/AnyPS5. Originally posted by MarrasFederico on 2026-10-21T12:33:22Z.
@oneandonlydean @nikowolf123 thanks. Merged current
main(the conflict was only indocs/dev/debt/libkernel.md) and kept the live count. Thekevent on a kqueueentry now states the difference as nikowolf123 described:datacounts the watched queue's enabled notes that are ready when it is evaluated;kq_countkeeps a queued note until that kqueue is scanned, even once it is disabled or no longer ready;EV_ONESHOTnote is reported there with its last count and deleted, while here it is evaluated again and, if the watched queue is empty by then, neither reported nor deleted.The code and the test are unchanged.
guest_kqueuepasses three runs in a row and the 80 equeue/kqueue/pipe/socket/poll tests pass. The PR text says the same.@ycbuzi thanks for the Windows run.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-21T14:05:28Z.
Checked the nested wait registration (lock order is always outer to inner, cycles are refused at registration), the
kq_countdifference now written in the debt entry, and thatWatchKqueuepins the counts and the timer/socket/chain wakeups and fails onmain; Windows build confirmed by @ycbuzi; merged.