fix(libkernel): fail msync MS_INVALIDATE with EBUSY as measured on a PS5 #8728

Merged
orezraey merged 3 commits from upstream-pr/8728 into main 2026-10-24 07:25:08 +00:00
orezraey commented 2026-10-20 22:37:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-20T22:37:36Z.


What

msync threw for MS_INVALIDATE on a mapped range. The debt entry gave the reason: FreeBSD 11 returns EBUSY only when the range holds mlocked pages, and locked pages are not tracked here.

In a payload process on a PS5 (firmware 13.60), MS_INVALIDATE on anonymous memory fails with EBUSY whether or not any page is locked, presumably because the console never pages memory out. This PR returns EBUSY there:

Call on a 64 KiB anonymous mmap Payload process, firmware 13.60 main before this PR here now
MS_SYNC, MS_ASYNC 0 0 0
MS_INVALIDATE, MS_SYNC | MS_INVALIDATE −1, EBUSY throws −1, EBUSY
the same after mlock of the first 16 KiB −1, EBUSY throws −1, EBUSY
MS_INVALIDATE with length 0, or at addr + 1 for 16 bytes −1, EBUSY throws −1, EBUSY
MS_INVALIDATE over the range after munmap of a page inside it −1, EBUSY throws −1, EBUSY
MS_ASYNC | MS_INVALIDATE −1, EINVAL −1, EINVAL unchanged
unknown flag 0x8 0 0 unchanged
MS_INVALIDATE or MS_SYNC after munmap of the whole range −1, ENOMEM −1, ENOMEM unchanged

The rows marked unchanged were already right. The range with a hole agrees with the existing rule that a page unmapped from inside a larger mapping still counts as mapped.

Debt: the msync entry in docs/dev/debt/libkernel.md no longer says the call is unmeasured. It records the EBUSY result and what was not measured: a title process, reservations, direct memory and flexible memory.

How the console was measured

  • Setup:
    • The console runs firmware 13.60 and is jailbroken.
    • The probe is an ELF payload built with ps5-payload-dev/sdk v0.44 and clang 18.1.8 and started through elfldr, so it ran in a payload process, not in a title.
  • Provenance: the resolve line shows that msync was taken from libkernel_sys.sprx by dlsym and called through that pointer.
  • Method:
    • The range is a 64 KiB private anonymous mmap, with its first byte written.
    • errno was cleared before each call.
    • The locked case calls mlock on the first 16 KiB, which succeeded.
    • The hole case unmaps 16 KiB at offset 32 KiB.
  • Files: the probe source and the full log are in this gist, with each probe's main.c renamed <probe>_main.c. The probe also measured sceKernelSyncOnAddressWait, which is #8725.
Console output (excerpt, lines copied unchanged)
kwait: meta fw=0x13600007 pid=113 sdk=v0.44 compiler=Clang 18.1.8 built=Oct 20 2026 18:37:00 log=/data/anyps5-probes/kwait.log
kwait: resolve sym=msync module=libkernel_sys.sprx exported=yes
kwait: case msync range=mapped flags=MS_SYNC ret=0 errno=0
kwait: case msync range=mapped flags=MS_ASYNC ret=0 errno=0
kwait: case msync range=mapped flags=MS_INVALIDATE ret=-1 errno=16
kwait: case msync range=mapped flags=MS_SYNC|MS_INVALIDATE ret=-1 errno=16
kwait: case msync range=mapped flags=MS_ASYNC|MS_INVALIDATE ret=-1 errno=22
kwait: case msync range=mapped flags=0x8 ret=0 errno=0
kwait: case msync range=locked mlock_ret=0 mlock_errno=0 invalidate_ret=-1 errno=16 sync_invalidate_ret=-1 errno=16
kwait: case msync len=0 flags=MS_INVALIDATE ret=-1 errno=16
kwait: case msync addr=+1 len=16 flags=MS_INVALIDATE ret=-1 errno=16
kwait: case msync range=with_hole flags=MS_INVALIDATE ret=-1 errno=16
kwait: case msync range=unmapped invalidate_ret=-1 errno=12 sync_ret=-1 errno=12

Tested

Build: Windows 11 x86-64, WinLibs MinGW-w64 GCC 15.2.0 (posix-seh, ucrt, r7), -DBUILD_TESTING=ON, on main e25333e39.

Tests: guest_msync passes. It now checks EBUSY for MS_INVALIDATE, for MS_SYNC | MS_INVALIDATE, for length 0 and for a misaligned start, and that flag 0x8 succeeds.

Old code, new test: with PosixMemory.cpp reverted to main, guest_msync aborts with msync: MS_INVALIDATE is not implemented.

Not run here: Linux and macOS.

Console: behaviour measured on a PS5 (firmware 13.60), as described above.

Overlaps: tools/pr_overlap.py lists #8499, which adds an entry next to the msync entry in docs/dev/debt/libkernel.md. No open PR changes msync.

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:4943458678 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8728). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-20T22:37:36Z. --- ### What `msync` threw for `MS_INVALIDATE` on a mapped range. The debt entry gave the reason: FreeBSD 11 returns `EBUSY` only when the range holds `mlock`ed pages, and locked pages are not tracked here. In a payload process on a PS5 (firmware 13.60), `MS_INVALIDATE` on anonymous memory fails with `EBUSY` whether or not any page is locked, presumably because the console never pages memory out. This PR returns `EBUSY` there: | Call on a 64 KiB anonymous `mmap` | Payload process, firmware 13.60 | `main` before this PR | here now | |---|---|---|---| | `MS_SYNC`, `MS_ASYNC` | 0 | 0 | 0 | | `MS_INVALIDATE`, `MS_SYNC \| MS_INVALIDATE` | −1, `EBUSY` | throws | −1, `EBUSY` | | the same after `mlock` of the first 16 KiB | −1, `EBUSY` | throws | −1, `EBUSY` | | `MS_INVALIDATE` with length 0, or at `addr + 1` for 16 bytes | −1, `EBUSY` | throws | −1, `EBUSY` | | `MS_INVALIDATE` over the range after `munmap` of a page inside it | −1, `EBUSY` | throws | −1, `EBUSY` | | `MS_ASYNC \| MS_INVALIDATE` | −1, `EINVAL` | −1, `EINVAL` | unchanged | | unknown flag `0x8` | 0 | 0 | unchanged | | `MS_INVALIDATE` or `MS_SYNC` after `munmap` of the whole range | −1, `ENOMEM` | −1, `ENOMEM` | unchanged | The rows marked unchanged were already right. The range with a hole agrees with the existing rule that a page unmapped from inside a larger mapping still counts as mapped. **Debt:** the `msync` entry in `docs/dev/debt/libkernel.md` no longer says the call is unmeasured. It records the `EBUSY` result and what was not measured: a title process, reservations, direct memory and flexible memory. #### How the console was measured - **Setup:** - The console runs firmware 13.60 and is jailbroken. - The probe is an ELF payload built with [ps5-payload-dev/sdk](https://github.com/ps5-payload-dev/sdk) v0.44 and clang 18.1.8 and started through elfldr, so it ran in a payload process, not in a title. - **Provenance:** the `resolve` line shows that `msync` was taken from `libkernel_sys.sprx` by `dlsym` and called through that pointer. - **Method:** - The range is a 64 KiB private anonymous `mmap`, with its first byte written. - `errno` was cleared before each call. - The locked case calls `mlock` on the first 16 KiB, which succeeded. - The hole case unmaps 16 KiB at offset 32 KiB. - **Files:** the probe source and the full log are in [this gist](https://gist.github.com/orezraey/5d872bc1dab56eb96d3ffda901d09ad1), with each probe's `main.c` renamed `<probe>_main.c`. The probe also measured `sceKernelSyncOnAddressWait`, which is #8725. <details><summary>Console output (excerpt, lines copied unchanged)</summary> ``` kwait: meta fw=0x13600007 pid=113 sdk=v0.44 compiler=Clang 18.1.8 built=Oct 20 2026 18:37:00 log=/data/anyps5-probes/kwait.log kwait: resolve sym=msync module=libkernel_sys.sprx exported=yes kwait: case msync range=mapped flags=MS_SYNC ret=0 errno=0 kwait: case msync range=mapped flags=MS_ASYNC ret=0 errno=0 kwait: case msync range=mapped flags=MS_INVALIDATE ret=-1 errno=16 kwait: case msync range=mapped flags=MS_SYNC|MS_INVALIDATE ret=-1 errno=16 kwait: case msync range=mapped flags=MS_ASYNC|MS_INVALIDATE ret=-1 errno=22 kwait: case msync range=mapped flags=0x8 ret=0 errno=0 kwait: case msync range=locked mlock_ret=0 mlock_errno=0 invalidate_ret=-1 errno=16 sync_invalidate_ret=-1 errno=16 kwait: case msync len=0 flags=MS_INVALIDATE ret=-1 errno=16 kwait: case msync addr=+1 len=16 flags=MS_INVALIDATE ret=-1 errno=16 kwait: case msync range=with_hole flags=MS_INVALIDATE ret=-1 errno=16 kwait: case msync range=unmapped invalidate_ret=-1 errno=12 sync_ret=-1 errno=12 ``` </details> ### Tested **Build:** Windows 11 x86-64, WinLibs MinGW-w64 GCC 15.2.0 (posix-seh, ucrt, r7), `-DBUILD_TESTING=ON`, on `main` e25333e39. **Tests:** `guest_msync` passes. It now checks `EBUSY` for `MS_INVALIDATE`, for `MS_SYNC | MS_INVALIDATE`, for length 0 and for a misaligned start, and that flag `0x8` succeeds. **Old code, new test:** with `PosixMemory.cpp` reverted to `main`, `guest_msync` aborts with `msync: MS_INVALIDATE is not implemented`. **Not run here:** Linux and macOS. **Console:** behaviour measured on a PS5 (firmware 13.60), as described above. **Overlaps:** `tools/pr_overlap.py` lists #8499, which adds an entry next to the `msync` entry in `docs/dev/debt/libkernel.md`. No open PR changes `msync`. ### Checklist - [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
MarcZX10 commented 2026-10-20 22:43:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T22:43:21Z.


I reviewed the exact diff. The new EBUSY path is after the existing wrap, invalid flag combination, and mapped-range checks, so the tested ENOMEM/EINVAL behavior remains intact; the updated test covers mapped invalidate, sync+invalidate, zero length, misaligned start, and the accepted unknown flag. The console measurement and debt entry also clearly limit what was observed. I found no issue in this focused change. I did not run a local build here; the hosted build jobs are skipped, so the Windows test and console probe remain the available runtime evidence.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6303427556 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8728#issuecomment-6303427556). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-20T22:43:21Z. --- I reviewed the exact diff. The new `EBUSY` path is after the existing wrap, invalid flag combination, and mapped-range checks, so the tested `ENOMEM`/`EINVAL` behavior remains intact; the updated test covers mapped invalidate, sync+invalidate, zero length, misaligned start, and the accepted unknown flag. The console measurement and debt entry also clearly limit what was observed. I found no issue in this focused change. I did not run a local build here; the hosted build jobs are skipped, so the Windows test and console probe remain the available runtime evidence.
MarcZX10 commented 2026-10-20 22:53:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T22:53:14Z.

Review: APPROVED


Reviewed the current diff at 692cb4af. The EBUSY result is returned after existing flag, range, and alignment validation, preserving the tested EINVAL/ENOMEM cases; regression coverage includes mapped invalidation and relevant validation edges. The PR reports PS5 payload measurements and its limitations. The metadata checks pass; platform build jobs are skipped, and I did not run a local platform build.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5592796354 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8728#pullrequestreview-5592796354). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-20T22:53:14Z. **Review:** APPROVED --- Reviewed the current diff at 692cb4af. The EBUSY result is returned after existing flag, range, and alignment validation, preserving the tested EINVAL/ENOMEM cases; regression coverage includes mapped invalidation and relevant validation edges. The PR reports PS5 payload measurements and its limitations. The metadata checks pass; platform build jobs are skipped, and I did not run a local platform build.
xXPBSXx commented 2026-10-21 08:45:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T08:45:30Z.


Linux data point for 692cb4af09: WSL2 Ubuntu 24.04 (virtualised kernel, not bare-metal Linux), GCC 13.3.0, Release, BUILD_TESTING=ON, PR head alone (not merged onto main).

  • With the PR: ctest -R '^guest_msync$' passes 3 of 3.
  • With core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp restored to the merge-base (new test kept): fails (Subprocess aborted).
    Not tested: macOS, Windows, a title run.
    AI-assisted (Claude Code).
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6311872770 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8728#issuecomment-6311872770). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T08:45:30Z. --- Linux data point for `692cb4af09`: WSL2 Ubuntu 24.04 (virtualised kernel, not bare-metal Linux), GCC 13.3.0, Release, `BUILD_TESTING=ON`, PR head alone (not merged onto `main`). - With the PR: `ctest -R '^guest_msync$'` passes 3 of 3. - With `core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp` restored to the merge-base (new test kept): fails (`Subprocess aborted`). Not tested: macOS, Windows, a title run. AI-assisted (Claude Code).
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!8728
No description provided.