feat(libkernel): report open, read and close notes on watched files #7121
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7121"
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 GitamarT045T3R on 2026-10-17T12:20:46Z.
What
EVFILT_VNODE(kevent) andsceKernelAddFileEventthrew forNOTE_OPEN,NOTE_CLOSE,NOTE_CLOSE_WRITE,NOTE_READ,NOTE_REVOKEand unknown bits, and for a descriptor that is not a file, a directory, a pipe or a socket. They now follow FreeBSD 11:vop_open_postNOTE_OPENafter a successful open of the file (open,openat,sceKernelOpen)vop_read_post,vop_readdir_postNOTE_READafter a successfulread,readv,pread,preadv, theirsceKernel*forms, AIO read orgetdents/getdirentries, zero bytes includedvop_close_postNOTE_CLOSE, orNOTE_CLOSE_WRITEwhen it was opened for writing;dup2onto it counts; adupstill open defers itclosefpcallsknote_fdclosefirstNOTE_REVOKE, unknown bitsvfs_kqfilterdoes not checkfflagsno_kqfilterENODEV(/dev/urandom, the null device)ttydev_kqfilterEINVALWindows and macOS have no host notification for opens, reads or closes, so these notes come from libkernel's own calls on every host: each vnode watch is recorded with the
st_devandst_inothatfstatreports, and the open, read and close paths deliver to the watches of the same file. Nothing is looked up while no watch exists. The last-descriptor test uses the open-file status thatdupalready shares; the write test asks the host access mode asfcntl(F_GETFL)does. A file read from app0.zar is watched for these notes only, as the archive never changes.Gaps, in the libkernel debt entry: libc streams (
fopen,fread) read through the host C runtime and raise none of these notes; other host processes neither; JIT shared memory is watched as its host file, where FreeBSD shared memory answersEINVAL.Overlaps: #7088 (kevent read and write on regular files) changes the
VnodeWatchconstructor, and #7092 (NOTE_LOWAT) the lines next to the removedfflagscheck inKqueue.cpp; both merge into this with one-line conflicts, whichever lands second.Tested
Windows 10 Pro 22H2 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Ninja, Release,
BUILD_TESTING=ON, onmaine09dbc2b4:guest_kqueuewith two new cases.AccessNotes: open, read, read at end of file andpreadof another descriptor reportNOTE_OPENthenNOTE_READ; closing aduped descriptor reports nothing until the last copy closes (NOTE_CLOSE); a read-write descriptor closes withNOTE_CLOSE_WRITE;dup2onto an open descriptor reportsNOTE_CLOSE;sceKernelAddFileEventgetsNOTE_OPENwith itsudata; closing the watched descriptor removes its own watch while a watch through another descriptor getsNOTE_CLOSE;/dev/urandomfails withENODEVfrom both calls.DirectoryAccessNotes: open,getdentsand close of a directory.FileWatchnow checks thatNOTE_REVOKEwith an unknown bit is accepted and reports nothing. Passes 20 of 20 repeated runs.main's libkernel fails withEVFILT_VNODE: fflags 1048640 not implemented.ctest -j 12: 1379 of 1382 pass. The three others are unrelated:select_testsneeds symbolic link privilege on Windows, andagc_driver_f16_fma_denormal_modesandagc_driver_fma_mix_f16_denormal_modesget NVIDIA's f16 NaN (0x7fff) where they expect 0xfe00.python tools/check_conventions.py --base origin/mainpasses.isatty, and the notes come from the same shared code.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 jotitadev on 2026-10-17T15:16:34Z.
Review: COMMENTED
Linux run, since the description notes the change was not built or run on Linux or macOS.
Machine: Linux x86_64, i7-8700, Ubuntu 26.04, GCC 15.2, Release,
BUILD_TESTING=ON, full build.Head
47f1ea309merged ontomain40315fcf8(clean merge).guest_kqueue, with the newAccessNotesandDirectoryAccessNotescases, passes on Linux. The related event, poll, select, AIO and filesystem tests (ctest -R "kqueue|equeue|poll|select|aio|file_event|vnode|filesystem", 18 tests) also pass.main:guest_kqueueaborts withEVFILT_VNODE: fflags 1048640 not implemented, the same failure as on Windows.ctest -E "^agc|gpu|vulkan|video_out"): 682/682, same asmainon this machine.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by GitamarT045T3R on 2026-10-17T15:40:43Z.
@jotitadev thank you for the Linux run; it covers the
isattydevice check and the inotify host I could not run.On the overlap table: none of the listed pull requests implements the same notes or exports, and each conflict is two additions next to each other, resolved by keeping both:
VnodeWatchconstructor; whichever lands second passesEVFILT_VNODEin the other's call.NOTE_LOWAT) rewrites the lines next to thefflagscheck this removes fromKqueue.cpp.IsVnodewhere this appendsLastDescriptorOfOpenFileandOpenFileWritesinDescriptorControl.EVFILT_VNODEentry this edits.Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-18T10:05:37Z.
Rebase handoff — rebased onto current
main(authorship unchanged). The conflicts reconcilemain's newer NOTE_LOWAT/RequireVnodeNotes evolution with this PR's open/read/close note reporting:Kqueue.cpp:main's lowWater-awareModifyKnotekept; yourNOTE_LOWATstub is superseded bymain's real supportFileEvent.cpp/Equeue.hpp:main'sRequireVnodeNotesvalidation removed, per this PR's design — yourAddVnodeEventtakes any fflags and passes them to the watch (your test'sNoteRevoke | 0x00100000acceptance documents that), and the auto-merge had half-restoredmain's validation which broke itDescriptorControl.cpp/.hpp:main'sIsVnodekept alongside yourLastDescriptorOfOpenFile/OpenFileWritesVerified 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 including the open/read/close coverage; full suite green apart from the knownmainregression (agc_driver_tessellationsegfault — Mesa, #5444), not this PR.To pick it up:
Head is
c5c56b51c.Imported from boykopovar/AnyPS5. Originally posted by GitamarT045T3R on 2026-10-18T12:20:43Z.
@blawesom thank you for the rebase and the Linux run. I rebased onto
main0d078f75dmyself as 702ba97dd: the code matches your c5c56b51c line for line (main's low-waterModifyKnotekept without theRequireVnodeNotescall,IsVnodekept next toLastDescriptorOfOpenFileandOpenFileWrites), but in yourdocs/dev/debt/libkernel.mdtheEVFILT_VNODEentry lost its line break and runs into thesem_timedwaitentry, so I kept mine.On Windows after the rebase:
guest_kqueue,guest_select, the AIO and poll tests andguest_filesystempass 5 of 5; the fullctestis 1476 of 1478, the two others (guest_math,guest_err, "vwarnx stack and floating arguments") failing the same way withmain's libkernel.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-18T16:56:55Z.
Review: COMMENTED
macOS run of
702ba97dd.maincannot build the libraries on macOS on its own, so I ran it on top of #7332, which includes @mugurc's macOS file code.Machine: Apple M2 Max, macOS 27.2 (26B5112a), Apple clang 21, full x86-64 build under Rosetta 2. Tree:
maind3d8d79c5+ #7332690c044a6+ this PR (clean merge, no build errors).guest_kqueuefails 5/5. On #7332 alone it passes. FileAccessNotespasses.DirectoryAccessNotesstops at thegetdentscheck:getdentsreturns 24 bytes, but the knote has noNOTE_READ(fflags 0).Cause: this PR wraps the Windows and Linux
sceKernelGetdirentriesreturns inEqueueNotedRead. The macOS branch of that function (#elif defined(__APPLE__)inFile/src/Stdio.cpp, from mugurc'sfeat/libkernel-files-macos, carried by #7332 and not onmainyet) still ends withreturn static_cast<int>(written);. Wrapping that return as well fixes it:With that line,
guest_kqueuepasses 5/5 and the non-GPU suite is 706/708 (ctest -E "^agc|video_?out|flip|display" -j4). The two failures areprecise_waitandguest_refused_connect, the same host-timing and XNU-backlog failures #7332 has without this PR. Nothing to change here while the macOS branch is not onmain. Whichever of this PR and the macOS file code lands second needs the line above. cc @ycarogabriel-lgtm @mugurcWritten with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by ycarogabriel-lgtm on 2026-10-19T00:13:34Z.
Ran this on macOS, since it was not built there: Apple M1, macOS 26.6.2 (25G83), x86-64 build under Rosetta 2, AppleClang 21. Merged into the macOS host branch of #7332 (
57bccfbfc,mainatd71853cf2) together with #7121, #7475, #7490, #7507 and #7612;Releasebuild.guest_kqueuefailed inDirectoryAccessNotes:getdentson the listing descriptor delivered noNOTE_READ.sceKernelGetdirentriesfor macOS (#elif defined(__APPLE__)inStdio.cpp), and only the Windows and Linux versions route their result throughEqueueNotedRead.returnthe same way makesguest_kqueuepass, 3/3. The rest of the suite without the GPU tests passes as well.main. Nothing is needed here.AI-assisted: yes (Claude Code). I ran the build and tests on my machine.
Imported from boykopovar/AnyPS5. Originally posted by GitamarT045T3R on 2026-10-21T16:59:24Z.
Rebased onto
maincecb540a6(a5eeb38ff) on top of @adriadam10's rebase.sceKernelGetdirentrieson Windows now passesbaseptoReadDirectoryDescriptor, and its result still goes throughEqueueNotedRead.The debt change now cuts instead of rewriting, as CONTRIBUTING asks: the
EVFILT_VNODEentry only loses the sentence that said these notes throw, and what stays open (libc streams and other host processes raise no access notes, the foldedst_inoon Windows,NOTE_REVOKEnever reported, JIT shared memory) is a new 779-character entry.check_conventionsstill warns about theEVFILT_VNODEentry itself, which is 4405 characters onmainand 4245 after this; splitting it is not part of this pull request.macOS (@jotitadev, @ycarogabriel-lgtm): the macOS
sceKernelGetdirentriesis not onmainyet, so nothing changes here; whichever of this and #7332 lands second wraps its return inEqueueNotedRead. On Windowsguest_kqueue,guest_select,guest_poll,guest_aioandguest_filesystempass 5 of 5.Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T20:03:24Z.
Review: COMMENTED
Correction (2026-11-01): the title-ID/log demand below is not a CONTRIBUTING requirement;
Unblocks: noneis allowed. The technical points below are unaffected.1. No title reaches this on
main. "Unblocks: none", and the body gives no title ID with a log line showing it registersNOTE_OPEN,NOTE_READorNOTE_CLOSEon anEVFILT_VNODEwatch. The change touchesOpen.cpp,Stdio.cpp,Aio.cppandDescriptorControl.cpp, i.e. the read, open and close paths, so it needs a title that makes it matter.2. Cost on the read path once any watch exists.
EqueueNoteReadis cheap whileg_accessWatchCountis zero. As soon as one vnode watch is registered anywhere (for any file), everyread,pread,readv, AIO read andgetdentson every descriptor doesDescriptorIdentity(a descriptor stat viaFillFileStatFromDescriptor), then takesg_accessMutexand walks the wholeg_accessWatchesvector. A title that watches one config file would pay a stat plus a global lock on all streaming reads. Could the watch keep the descriptors (or the identity) it cares about so unrelated descriptors return without a stat, or at least filter by a per-descriptor "has an access watch" flag set at open?Imported from boykopovar/AnyPS5. Originally posted by einfachama on 2026-11-20T12:24:34Z.
Review: COMMENTED
Thanks for this. One concern about reachability with the new
ENODEVbranch inOpenHostWatch, checked against upstream/main94dd5d81dd:/dev/nullor/dev/zerois never a host device here.Resolve(core/libs/prx/libc/src/General.cpp:400, root join at L421) maps it to an ordinary sandbox path. Only/dev/randomand/dev/urandomare special-cased (core/libs/prx/libkernel/File/src/Open.cpp:401)./dev/null(NULon Windows) underneath: random devices (File/src/RandomDevice.cpp:30-32), archived directories (File/src/DirectoryDescriptor.cpp:292-296), ksem (Semaphore/src/Ksem.cpp:60-62) and mqueue (libkernel_sys/Mqueue.cpp:88-90). Archived files (File/src/ArchivedFile.cpp:25-27) skipOpenHostWatchin this PR.On Linux these reach the
S_ISCHRbranch and getENODEV, which FreeBSD does not give them. Could the answer come from the guest-side descriptor type instead, with unknown kinds still throwing?Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T12:39:12Z.
Review: COMMENTED
Reviewed — comments
Nothing blocks this review; the points below are optional.
What I ran
Reviewed head
cb750ff85695ea65ff493d321c3242407536895bfrom the prepared patch and source reader. No build or tests were run in this review.Results
Findings
Blocking
None.
Non-blocking
LastDescriptorOfOpenFiletreats an untracked descriptor as the last descriptor (DescriptorControl.cpp:672).RememberDescriptoris only called along selected libkernel open paths, while standard descriptors and descriptors opened by other host code can be watched and closed through libkernel. In those cases a duplicate may exist without an entry ing_entries, so a close can reportNOTE_CLOSE/NOTE_CLOSE_WRITEbefore the last host descriptor closes. Consider determining last-descriptor status from the host open-file description for untracked descriptors, or making this predicate conservative when the descriptor is not tracked.Not verified
No commands, build, or tests were run. Author-reported Windows results were not independently verified; Linux/macOS and console behavior were not run. All ten changed files were listed as included in the manifest, but full-file review was limited to the relevant source ranges.
review/files.jsonwas not read; the file listing showed the changed files as included and listed no exclusions.To get this to a good state
Nothing is required. Optional: cover a watched untracked descriptor with a duplicate and confirm that close notes wait for its final close.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.