feat(libkernel): answer sceKernelAddReadEvent and sceKernelAddWriteEvent on regular files #8584
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8584"
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-20T17:25:57Z.
What
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.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, full build with
-DBUILD_TESTING=ON, onmain83751168dd.guest_socket_eventshas a newRegularFileEventscase: 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.guest_socket_events,guest_kqueueandguest_equeue_eventspassed 3 of 3 runs each.select_tests,guest_select,guest_kernel_errors,guest_http2,guest_ampr_markers,guest_video_out_hdrandvideo_out_flip_decode(every other test program that uses equeues or kqueues) pass. The VideoOut flip tests that need a display were skipped, as on CI.main'sEqueue.cpp,guest_socket_eventsaborts (0xc0000409).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 github-actions[bot] on 2026-10-20T17:26:17Z.
Thank you for your contribution! Authors with no pull requests merged into
maincan have 1 open pull request at a time, authors with at least 1 merged can have 20, and from 20 merged there is no limit. This PR has been automatically closed and labeled "open PR limit" because that limit was reached. This is not a judgment of the quality of your changes: reopen it once one of your open pull requests is merged or closed.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T17:34:46Z.
Thanks, the filters match FreeBSD's
filt_vfsread/filt_vfswrite, and the repoll doesn't shorten the caller's own timeout. There's one problem on Windows: a lock-order inversion between the new poll andclose.m_mutexinWaitForEventsUntil→PollEvents→UnreadFileBytes→sceKernelLseek. On Windows,NativeLseek(File/src/Open.cpp, around line 50) takesFile::LockPositionedTransfer(fd).sceKernelClose(Open.cpp, around line 293) andclose_nid_postfix(Stdio.cpp, around line 695) holdLockPositionedTransfer(d)and then callEqueueDescriptorClosed(d)→RemoveDescriptorEvents, which locksm_mutex.So closing a regular file while another thread waits on an equeue with a read event on it can deadlock both threads. The 1 ms repoll re-enters that order a thousand times a second. A long read or write on the same descriptor also blocks every equeue operation for its duration, because the poll waits for the positioned lock while holding
m_mutex. Linux'sNativeLseektakes no lock, so neither a Linux run nor a run without a concurrent close shows it.Could you avoid calling into the File layer while holding
m_mutex? For example, poll the file events after releasing it, or query the size and offset without the positioned lock. Or have the close paths callEqueueDescriptorClosedbefore they takeLockPositionedTransfer. A test where one thread waits at end of file while another closes the descriptor (it should return, not hang) would cover it.Separately, this conflicts with #8523 in
WaitForDescriptors, as you noted.Imported from boykopovar/AnyPS5. Originally posted by aarushkha on 2026-10-20T18:01:02Z.
@oneandonlydean Thanks for the review. Both problems are fixed on the branch:
Lock-order deadlock with
close(ca3277fe49):sceKernelCloseandclose_nid_postfixnow callEqueueDescriptorClosedbefore they takeLockPositionedTransfer, asdup2already did.CloseWhileWaitingOnFileinGuestSocketEvents.cppcovers it: a thread keeps waiting on the equeue while another thread opens a file at end of file, adds a read event and closes it, 2000 times. Without the fix it hit ctest's 20 s timeout 3 of 3 times on Windows. With it, it passes in about 0.6 s (10 of 10 runs).File calls under
m_mutex(2d0dbf0fc8): the file read event no longer calls into the File layer while holdingm_mutex. BeforePollEvents,MeasureFilescopies the read events to measure, releasesm_mutex, runssceKernelFstatandsceKernelLseek, then stores the results underm_mutex. So a long read or write on the descriptor only delays the waiting thread, not other equeue operations. Each file read event keeps its result in its own state object held throughfilter.owner. A descriptor closed and reused during the measurement gets a new object, so the old file's size is never reported for it. Write events don't touch the file.With fix 2, the close reorder from fix 1 isn't needed to avoid the deadlock any more. I kept it because it does no harm, but I can drop it if you'd prefer a smaller diff.
Tested on Windows 10 (WinLibs GCC 15.2):
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_streamsall pass.check_conventions.py --base origin/mainpasses. There is no automated test for the long-transfer case, since I couldn't hold a transfer in progress for a set time without making the test unreliable.The conflict with #8523 in
WaitForDescriptorsis still there. I'll rebase onto whichever of the two lands first. This PR was closed by the open-PR limit, so I'll reopen it once #8554 is merged or closed.Imported from boykopovar/AnyPS5. Originally posted by aarushkha on 2026-10-21T09:31:36Z.
#8554 is merged, so this is under the open PR limit now. GitHub won't let me reopen a PR the bot closed. Could a maintainer reopen it? The branch already has both review fixes (ca3277fe49, 2d0dbf0fc8).
Imported from boykopovar/AnyPS5. Originally posted by aarushkha on 2026-10-21T12:14:48Z.
Since this can't be reopened, I opened a copy as #9099: the same change with both review fixes (
MeasureFilesoutsidem_mutex, andEqueueDescriptorClosedbefore the position lock in the close paths), rebased on currentmainand retested. Review can continue there.Pull request closed