feat(libkernel): sync each write on a file whose O_FSYNC flag is set #9561
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9561"
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 DavidSamir on 2026-10-22T02:23:42Z.
What
fcntl(F_SETFL)threw when it changedO_FSYNCon a file. FreeBSD 11 allows the change, becauseFCNTLFLAGSincludesO_FSYNC(sys/kern/kern_descrip.c:558-559). While the flag is set,vn_writewrites withIO_SYNC(sys/kern/vfs_vnops.c:871-873), so each write reaches the disk before the call returns.How it works now:
dup,dup2andF_DUPFDshare it, as for the other status flags.F_SETFLsets and clears it on a file, andF_GETFLreports it.File::SyncWrites(fd)syncs a regular file (sceKernelFsync) while its open file hasO_FSYNCset. A failed host sync throws rather than passing silently. Pipes and other descriptors are never synced.File::WriteSyncguard calls it after each write entry point, placed just before the host write:sceKernelWrite, whichwriteand_writereach;pwrite, andsceKernelPwriteon each platform;TransferIovecs(Windowswritev/pwritev);sceKernelWritev/sceKernelPwritevon Linux.O_FSYNC: a file opened with the flag behaves the same.O_SYNCkeeps the host'sO_SYNCafterF_SETFLclears the flag, because Linux can't drop it. That's only extra durability, not a visible difference, and the debt line says so.A sync can't be observed from the guest, so
FileSynchronousWritesForTest_nid_no_patch()counts syncs for the test. It follows the existing*ForTestseams.Tested
Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. This is CPU-side, so no GPU is involved.
New test
guest_kernel_fcntl_sync(core/libs/tests/GuestKernelFcntlSync.cpp), which covers:F_SETFLsetting and clearingO_FSYNC, withF_GETFLreporting it;write,writev,pwriteandpwritev;dupsharing the flag, and no sync after it is cleared;O_FSYNCsyncing its writes;O_FSYNCnever syncing.guest_kernel_fcntl's check that changingO_FSYNCthrows now expects success instead.Results:
It passes, 3 of 3 runs. The 15 tests matching
guest_kernel_fcntl,guest_append,guest_dup,guest_file,guest_writevand iovec pass.Negative controls:
SyncWritesreturning early, the test fails at line 62;writevguard, it fails at line 64.tools/check_conventions.py: 0 errors. Its one warning is the length of thefcntldebt entry, which was already over 1000 characters onmain.Overlap:
tools/pr_overlap.py --branchplusgit merge-tree. The code merges cleanly with #9131, #7121, #8520 and #9019. The guards sit next to the host write calls, away from the lines those PRs change.Conflicts that remain, all textual:
fcntldebt line,docs/dev/debt/libkernel.md:218, which #9256, #8544, #9109 and #9557 also rewrite;core/libs/tests/GuestKernelFcntl.cpp(itsO_APPENDchange sits next to thisO_FSYNCcheck).Whichever merges second keeps both changes.
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-22T03:39:05Z.
Review: COMMENTED
First run on Linux (the PR is tested on Windows only). Ubuntu 26.04, x86_64, Intel i7-8700, GCC 15.2.0, Release,
BUILD_TESTING=ON, head b86238f6e merged--no-commitonto currentorigin/main(6ef5a1c4c).ctest -R "guest_kernel_fcntl_sync|guest_kernel_fcntl$"with the PR: both pass.Open.cppandStdio.cpptoorigin/main, keepingDescriptorControl.cppso the*ForTestcounter still links):guest_kernel_fcntl_syncfails at the first O_FSYNC writethat is,
Synced() == before + 1afterwriteon anO_FSYNCfile no longer holds once theWriteSynccall sites are gone. This matches the line the PR description predicts for theSyncWrites-early control.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T04:02:32Z.
Review: CHANGES_REQUESTED
The implementation covers the
O_FSYNCflag and the reported Linux negative-control run confirms the focused test fails when the sync call path is removed. The Windows test results are author-reported; an independent Linux build/test report is present. Hosted conventions, eligibility, intake, labels and report checks pass; product/relinker jobs are skipped. I did not run tests locally.Before approval, please resolve the integration requirements recorded in the PR itself. The test changes overlap #9256 in
GuestKernelFcntl.cpp, and the changedfcntldebt row overlaps #9557, #9256, #8544 and #9109. Please agree the merge/rebase order with the affected authors, preserve both test/debt changes, and addDepends onwherever another PR must land first. “Whichever merges second keeps both changes” is not yet recorded author agreement.Also update this branch from current
main: its base is6ef5a1c4, while upstreammainisd9c99c9. The intervening merge appears unrelated, but CONTRIBUTING requires branches to stay current. Rerun the focused fcntl/write tests after the update and identify the tested head.Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T06:01:31Z.
Review: COMMENTED
Checked against main
4a17c7175and libstdc++ 15.2 (WinLibs)<optional>:TransferIovecs(Stdio.cpp:1312-1313) the guard sits in astd::optional<File::WriteSync>. libstdc++'s_Optional_payload_base::_M_destroy()isnoexcept(<optional>:304-308), so whenSyncWritesthrows from~WriteSync() noexcept(false)there, the process hitsstd::terminateinstead of the intendedruntime_error. That affects Windowswritev/pwritevon anO_FSYNCfile whoseFlushFileBuffersfails. I reproduced it with a minimal copy of the guard built with GCC 15.2: the plainconst G g;form propagates the exception, while the optional-wrapped one prints "terminate called after throwing an instance of 'std::runtime_error'" and exits with code 3. A fix with no other changes would beconst File::WriteSync sync(write ? d : -1);, sinceSyncWrites(-1)already returns early.vn_writeaddingIO_SYNCto the write itself.sceKernelAioSubmitWriteCommands*writes throughAio.cpp'sNativePwritewithout the guard. Since it is unknown whether the console's AIO honoursO_FSYNC, the new debt sentence ("each write on a file") could be narrowed, or AIO noted there.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-22T07:51:43Z.
Thanks to both of you. New commits, no force-push:
896de840eb: fixes thestd::optionalguard @harisibrar1 found (thanks for the reproduction).TransferIovecsnow uses a plainconst File::WriteSync sync(write ? d : -1);, so a failing sync reaches the caller as theruntime_errorinstead ofstd::terminate.SyncWrites(-1)returns early because-1has no entry.write,pwrite,writev,pwritev) and says thesceKernelAioSubmitWriteCommandswrites are not synced, since whether the console's AIO honours the flag is unknown.aad3acf9ab: merges currentmain(d6eafa54d0), as @MarcZX10 asked.Re-run on the merge (Windows 11, WinLibs GCC 15.2):
guest_kernel_fcntl,guest_kernel_fcntl_syncandguest_append_writespass, andcheck_conventions.pyreports 0 errors. Its one warning is thefcntldebt entry, which is already 4143 characters onmain.On the overlaps (#9256 in
GuestKernelFcntl.cpp; thefcntldebt row with #9557, #9256, #8544 and #9109), I'm asking those authors for an agreed order and will record it in the checklist.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T11:21:51Z.
The latest head includes the fixes for the optional-destructor exception path and narrows the AIO claim as discussed. The overlap report still calls out #9256's adjacent test and shared debt edits with #9557/#9256/#8544/#9109. I found DavidSamir's proposal to Dom3005 on #9256, but no reply confirming the order yet; please record the affected authors' agreement in the checklist and add if an order is required. Also, current upstream/main is
4777caca34and this head is 3 ahead/11 behind (merge based6eafa54d), so CONTRIBUTING's current-main requirement still applies. Please update and rerun the focused fcntl/write tests on that resulting head. Hosted eligibility/conventions/labels/report succeeded, product/relinker jobs are skipped. I did not run the C++ tests locally: there is no reusable test binary in the available checkouts and the filesystem is full, so I am treating the Windows/Linux test reports in this PR as reported evidence, not my own run.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T11:21:52Z.
Review: DISMISSED
The functional issues raised in review appear addressed on aad3acf9ab, including the Windows optional-guard exception issue and explicit AIO limitation. I cannot approve while two CONTRIBUTING integration requirements remain unverified: (1) update from current upstream/main
4777caca34(the branch is 3 ahead/11 behind, merge based6eafa54d), then rerun focused tests and update Tested; (2) record agreement with affected authors for the adjacent #9256 fcntl test and shared debt-row changes, including a Depends on entry if any PR must land first. The hosted product and relinker jobs are skipped. I did not run tests locally due to a full filesystem and absence of a reusable focused test binary.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T11:24:27Z.
For clarity on the overlap details: the adjacent test edit is
core/libs/tests/GuestKernelFcntl.cppwith #9256, and the shared technical-debt entry isdocs/dev/debt/libkernel.mdwith #9256, #9557, #8544 and #9109. I found DavidSamir’s merge-order proposal on #9256 but no confirmation from the affected author yet, so the checklist should record agreement once it is obtained; list Depends on if ordering is required.Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T12:54:56Z.
Review: APPROVED
Rechecked at aad3acf9a:
TransferIovecsnow uses a plainconst File::WriteSync sync(write ? d : -1);(Stdio.cpp:1312). A standalone copy of the guard built with GCC 15.2 now passes the syncruntime_errorto the caller instead of callingstd::terminate, and the read path (-1) does nothing.O_RDONLY|O_FSYNCfile ends in a sync thatNativeSynctreats as success on the access-denied reopen, so no errno turns into a throw.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.