feat(libkernel): answer sceKernelAddReadEvent and sceKernelAddWriteEvent on regular files #9099
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9099"
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 aarushkha on 2026-10-21T12:14:40Z.
What
A copy of #8584, which the open PR limit bot closed and its author cannot reopen. It includes the fixes for oneandonlydean's review there.
sceKernelAddReadEventandsceKernelAddWriteEventthrew for a regular file: they only accepted guest sockets and the read end of a pipe. They now follow FreeBSD 11.4's vnode filtersfilt_vfsreadandfilt_vfswrite:data(negative past the end) and fires while that is not 0.data0.FreeBSD activates these knotes when the vnode changes. No host call reports every file change, so
WaitForDescriptorsre-polls an equeue that holds a regular-file event every millisecond (a newKernelFilter::repollflag), instead of handing the file topoll/WSAPoll. A write made through another descriptor is therefore seen up to 1 ms late. A size argument above 1, directories and other vnode types still throw, as recorded indocs/dev/debt/libkernel.md. Whether the console's own file systems answer these filters asvfs_kqfilterdoes was not measured.Changes from the review of #8584:
sceKernelFstatandsceKernelLseekno longer run under the equeue'sm_mutex. BeforePollEvents,MeasureFilescopies the read events to measure, releasesm_mutex, measures them, then stores the results underm_mutex. On Windows,lseekwaits for the descriptor's position lock, so a long read or write on the file used to block every operation on the equeue, and aclosetaking the two locks in the opposite order could deadlock. Each read event keeps its result in its own state object (filter.owner). A descriptor closed and reused during the measurement gets a new object, so the old file's size is never reported for it.sceKernelCloseandclosenow callEqueueDescriptorClosedbefore they takeLockPositionedTransfer, asdup2already did, so the equeue mutex is always taken before the position lock.keventon regular files is left to #7088, which implements the same FreeBSD filters there. This PR only touches the equeue path (Equeue.cpp), which #7088 does not change. If #7088 lands first, its file classification could replaceRegularFilehere.#8523 edits the same lines of
WaitForDescriptors, so whichever merges second needs a small rebase.Tested
Windows 10 Pro (10.0.19045), WinLibs GCC 15.2.0 posix UCRT r7, Release,
-DBUILD_TESTING=ON, onmain27c88a8d75.guest_socket_eventshas two new cases.RegularFileEventscovers a read event with the offset mid-file (data8) and at the end (times out), woken when a second descriptor appends 3 bytes (data3); a write event (data0); a size of 64 throws; afterclosethe event is gone (ENOENT) and a new add fails withEBADF.CloseWhileWaitingOnFilehas one thread wait on the equeue while another opens a file at end of file, adds a read event and closes it, 2000 times.main'sEqueue.cpp,guest_socket_eventsaborts (0xc0000409). With the first version of #8584 (before the review fixes),CloseWhileWaitingOnFiledeadlocked and hit the 20 s ctest timeout 3 of 3 times. With this PR it passes in about 0.6 s (6 of 6 runs on this base).guest_socket_events,guest_equeue_events,guest_kqueue,guest_positioned_race,guest_positioned_close,guest_dup,guest_directory_close,guest_file_vector,guest_aio,guest_aio_bad_descriptors,guest_filesystem,guest_posix_filesystem,guest_socket_descriptors,guest_sendfile,file_positionandfile_streams.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:39:29Z.
Review: COMMENTED
Checked against main
522e32e3b6and FreeBSD 11.4 sys/kern/vfs_subr.c (filt_vfsread/filt_vfswrite, vop_*_post) and sys/kern/kern_event.c (kqueue_scan):AddDescriptorEvent(Equeue.cpp:710-725) still throws for files, so this isn't duplicated.filt_vfsread/filt_vfswriteexactly (vfs_subr.c:5123-5168), and the close/dup2 lock order now agrees with main's WindowsNativeLseek(Open.cpp:50) taking the position lock.kqueue_scan(kern_event.c:1782-1790), a level-triggered read knote that finds data 0 is deactivated, and only a vnode KNOTE re-activates it (read, write, open, close, setattr…); lseek posts none. So with a 10-byte file at offset 10, a read event, thensceKernelLseek(fd, 0, SEEK_SET), FreeBSD keeps waiting until something touches the vnode, while the 1 ms repoll here reportsdata10. The entry currently describes the polling as only costing up to 1 ms of latency. Console behaviour is unmeasured either way, so documenting it seems enough.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.