fix(libkernel_sys): retain files across asynchronous I/O requests #12868

Closed
PrivacyOnion wants to merge 3 commits from upstream-pr/12868 into main
PrivacyOnion commented 2026-10-29 13:17:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-10-29T13:17:19Z.


What

  • libkernel_sys: Retains underlying open-file resources across asynchronous I/O requests from submission until completion consumption (aio_return or aio_waitcomplete), following FreeBSD 11.4 vfs_aio.c (referencing an underlying file object rather than a guest-visible descriptor slot).
  • libkernel:
    • Retained file resources: encapsulates private host file descriptors and handles (RetainedFile.cpp), keeping retained I/O isolated from guest-visible descriptor mutations.
    • Ownership protocol serialization: serializes private descriptor allocation, publication, and evacuation with guest descriptor-table mutations (open, close, dup, dup2, fcntl(F_DUPFD, F_SETFL), ioctl, pipe) via a unified ownership lock (LockDescriptorOwnership_nid_no_patch), preventing race conditions where guest mutations could interleave with retention or publication.
    • Synchronized open-file status: statusFlags across open file descriptors and retained references are stored as std::shared_ptr<std::atomic<int>>, ensuring live O_APPEND and O_FSYNC (O_SYNC) updates via fcntl(F_SETFL) are immediately and safely visible to concurrent asynchronous writes.
  • Tests: guest_kernel_sys_aio covering retained request lifetime across guest close/dup2, private slot reuse and isolation, descriptor cancellation, boundary allocation and evacuation race controls using deterministic stage hooks, and concurrent F_SETFL with live append/sync write controls.

Tested

Windows 11, Ryzen 5 5600, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON.

Commit 6872caa9db
$ ctest --test-dir build -R '^guest_kernel_sys_aio$' --output-on-failure
Test project C:/Users/playl/Desktop/dashy/data/worktrees/1029-202555-d29b/build
Start 470: guest_kernel_sys_aio
1/1 Test #470: guest_kernel_sys_aio ............. Passed 0.17 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) = 0.53 sec

Ran locally on 6872caa9db:

$ ctest --test-dir build -R '^guest_kernel_sys_aio$' --output-on-failure
    Start 470: guest_kernel_sys_aio
1/1 Test #470: guest_kernel_sys_aio .............   Passed    0.16 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =   0.29 sec

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5068717545 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12868). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-10-29T13:17:19Z. --- ### What - **libkernel_sys**: Retains underlying open-file resources across asynchronous I/O requests from submission until completion consumption (`aio_return` or `aio_waitcomplete`), following FreeBSD 11.4 `vfs_aio.c` (referencing an underlying file object rather than a guest-visible descriptor slot). - **libkernel**: - Retained file resources: encapsulates private host file descriptors and handles (`RetainedFile.cpp`), keeping retained I/O isolated from guest-visible descriptor mutations. - Ownership protocol serialization: serializes private descriptor allocation, publication, and evacuation with guest descriptor-table mutations (`open`, `close`, `dup`, `dup2`, `fcntl(F_DUPFD, F_SETFL)`, `ioctl`, `pipe`) via a unified ownership lock (`LockDescriptorOwnership_nid_no_patch`), preventing race conditions where guest mutations could interleave with retention or publication. - Synchronized open-file status: `statusFlags` across open file descriptors and retained references are stored as `std::shared_ptr<std::atomic<int>>`, ensuring live `O_APPEND` and `O_FSYNC` (`O_SYNC`) updates via `fcntl(F_SETFL)` are immediately and safely visible to concurrent asynchronous writes. - **Tests**: `guest_kernel_sys_aio` covering retained request lifetime across guest close/dup2, private slot reuse and isolation, descriptor cancellation, boundary allocation and evacuation race controls using deterministic stage hooks, and concurrent `F_SETFL` with live append/sync write controls. ### Tested Windows 11, Ryzen 5 5600, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON. Commit 6872caa9db $ ctest --test-dir build -R '^guest_kernel_sys_aio$' --output-on-failure Test project C:/Users/playl/Desktop/dashy/data/worktrees/1029-202555-d29b/build Start 470: guest_kernel_sys_aio 1/1 Test #470: guest_kernel_sys_aio ............. Passed 0.17 sec 100% tests passed, 0 tests failed out of 1 Total Test time (real) = 0.53 sec Ran locally on `6872caa9db`: ``` $ ctest --test-dir build -R '^guest_kernel_sys_aio$' --output-on-failure Start 470: guest_kernel_sys_aio 1/1 Test #470: guest_kernel_sys_aio ............. Passed 0.16 sec 100% tests passed, 0 tests failed out of 1 Total Test time (real) = 0.29 sec ``` ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!12868
No description provided.