feat(libkernel): support EV_CLEAR on sockets and pipes in kevent on Linux #7129
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7129"
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 AlexOfTheForest on 2026-10-17T12:50:13Z.
What
keventthrewNotImplementedforEV_CLEARon a socket or the read end of a pipe. Boost.Asio's kqueue reactor, among others, registers every socket withEV_ADD | EV_CLEAR. This adds edge-triggered knotes on Linux, following FreeBSD 11.4'skern_event.c:kqueue_registerfinds it ready. That happens on add, and again on every laterEV_ADDorEV_ENABLEof an enabled knote.KNOTEthe object raises (sorwakeup,sowwakeup,pipeselwakeup).kqueue_scanre-runs the filter before reporting and drops a knote that is no longer ready.How it maps onto the host:
EV_CLEARknote registers its host socket or pipe with a private epoll instance in edge-triggered mode:EPOLLIN | EPOLLRDHUPforEVFILT_READ,EPOLLOUTforEVFILT_WRITE.KNOTEand arms the knote. When armed, it is checked with the existing level-triggered readiness test and reported with the currentdataandEV_EOF, or disarmed if it is no longer ready.Commits:
refactor(libkernel): let an equeue filter name the descriptor its waits block on:KernelFilter::waitDescriptor. The filter's owner is held for the length of the wait.feat(libkernel): add an epoll edge watch over a host descriptor: the newEqueue/{include,src}/EdgeWatch. Off Linux, opening a watch throws.feat(libkernel): support EV_CLEAR on sockets and pipes in kevent on Linux: wiring inKqueue.cpp,GuestSockets::NativeDescriptor, and a new libkernel debt entry.test(libkernel): cover EV_CLEAR reads on a socket: fires once per arrival; stays quiet while data sits unread, with a CPU-time check that the 150 ms wait does not spin; fires again on re-add; is dropped when an arrival is read before the next scan; wakes a blockingkevent; reportsEV_EOFonce.test(libkernel): cover EV_CLEAR writes on a socket: fires on add, not after the guest's own send, again when the peer drains and frees space, on re-add, and withEV_EOFwhen the peer closes.test(libkernel): cover EV_CLEAR on a pipe and with EV_DISPATCH: pipe reads and EOF;EV_DISPATCHreports once, andEV_ENABLEre-runs the filter askqueue_registerdoes.Known differences, written up in the debt entry:
EPOLLOUTwaiters only after a write found the socket full (SOCK_NOSPACE,tcp_check_space), so a TCP writer that never fills its buffer sees fewerEVFILT_WRITEevents than on FreeBSD.EV_CLEARon the timer, user and vnode filters is unchanged and works everywhere.Tested
Linux x86_64, as root in a container:
guest_kqueue,guest_socket_events,guest_equeue_events,guest_poll,guest_selectandguest_select_waitall passed 5 of 5 runs.guest_tcpandguest_named_shm_concurrencyfail identically on unmodifiedmainin the same container (the shm one expectsEACCES, which root bypasses), so they are environmental.waitDescriptorthe read test fails on its CPU-time check; without disarming, the "stays quiet" checks fail.tools/check_conventions.py --base upstream/mainpasses; the progress report is unchanged.This touches the same areas of
GuestKqueue.cppanddocs/dev/debt/libkernel.mdas my open pipe-write and socket-write-space PRs (the pipe-write one also touchesSocketRuntime.hpp). There's no logical dependency; whichever merges later gets a small rebase.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-17T13:08:31Z.
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/prx/libkernel/Equeue/src/Kqueue.cppL154docs/dev/debt/libkernel.mdL117, L181-182docs/dev/debt/libkernel.mdL117-118docs/dev/debt/libkernel.mdL181docs/dev/debt/libkernel.mdL117docs/dev/debt/libkernel.mdL116-117Some 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.
core/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/Socket/src/Udp.cppdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/CMakeLists.txtcore/libs/tests/GuestKqueue.cppdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/Socket/src/Udp.cppdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/Equeue/Equeue.cppcore/libs/prx/libkernel/Equeue/src/Kqueue.cppcore/libs/tests/GuestKqueue.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/Socket/src/Udp.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/Socket/src/Udp.cppdocs/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 blawesom on 2026-10-18T09:58:46Z.
Rebase handoff — rebased onto current
main(authorship unchanged). The conflicts reconcilemain's newer NOTE_LOWAT support with this PR's edge-triggered EV_CLEAR:Kqueue.cpp:main'sDescriptorLowWater(the NOTE_LOWAT validation) kept alongside yourPollEdge/ResetEdge/WatchEdgesmachinery, andAddDescriptorKnotenow computes the low-water mark and installs the edge watch forEV_CLEAR— yourNOTE_LOWATstub is superseded bymain's real supportSocketRuntime.hpp:main'sType/Listeningdeclarations kept alongside yourNativeDescriptorGuestKqueue.cpp:main's newer write-end low-water test kept (with its#ifndef __linux__structure intact)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
01db86eb8.Imported from boykopovar/AnyPS5. Originally posted by AlexOfTheForest on 2026-10-18T12:04:27Z.
@blawesom thanks for the handoff. I redid the rebase on my side so the commits stay under my identity, following your NOTE_LOWAT reconciliation: main's DescriptorLowWater is kept, AddDescriptorKnote sets the low-water mark and then installs the edge watch for EV_CLEAR. One addition: the edge poll now applies the mark too (a cleared knote below NOTE_LOWAT is dropped, like filt_soread returning false in kqueue_scan), with a test for EV_CLEAR + NOTE_LOWAT that fails without that check. The six kqueue/equeue/poll/select tests pass 5/5 on Linux.
Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-18T21:30:19Z.
The new
EdgeTriggeredSocketReadinGuestKqueue.cpptakesstartafter it creates the sender 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 send.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 sender here keeps the new check from flaking the same way.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T17:57:57Z.
Checked the diff against FreeBSD 11.4
kqueue_register/kqueue_scan: the edge watch arms on add/EV_ENABLE, re-checks readiness (includingNOTE_LOWAT) before reporting and disarms after a report; the wait blocks on the epoll descriptor so a still-readable socket doesn't spin. The debt entry covers the TCPEPOLLOUTand coalescing differences.Rebased on current
main(conflicts inKqueue.cppanddocs/dev/debt/libkernel.mdnext toAddStandardInputKnoteand the mprotect entry, both kept) and added one commit:EdgeTriggeredSocketReadnow takesstartbefore creating the sender thread, so a preempted main thread can't makewaited >= 40 msfail (the flake reported above). Libkernel tests built and run on the rebased tree; onlyguest_kernel_fcntlfailed under parallel ctest, it passes alone and doesn't touch this code; merged.