feat(libkernel): accept MADV_PROTECT advisory hint in madvise #4154

Closed
jagrat04 wants to merge 1 commit from upstream-pr/4154 into main
jagrat04 commented 2026-10-12 00:06:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jagrat04 on 2026-10-12T00:06:45Z.


What

Accept MADV_PROTECT (10) as a valid advice parameter in madvise:

  • On FreeBSD/PS5, MADV_PROTECT informs the kernel to protect the process from being killed when memory is exhausted (OOM protection hint).
  • On the emulator host, this advisory hint has no effect and succeeds cleanly as a no-op, matching the handling of other advisory hints within the valid advice range.
  • Extended test in core/libs/tests/GuestMemory.cpp to verify advice up to 10 (MADV_PROTECT) returns success, while values > 10 return EINVAL.
  • Documented under Silent stubs in docs/dev/TechnicalDebt.md.

Tested

  • python tools/check_conventions.py --base origin/main passed with 0 violations.
  • Verified zero comments in C++ implementation files.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4823396064 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4154). Originally posted by [jagrat04](https://github.com/jagrat04) on 2026-10-12T00:06:45Z. --- ### What Accept `MADV_PROTECT` (10) as a valid advice parameter in `madvise`: - On FreeBSD/PS5, `MADV_PROTECT` informs the kernel to protect the process from being killed when memory is exhausted (OOM protection hint). - On the emulator host, this advisory hint has no effect and succeeds cleanly as a no-op, matching the handling of other advisory hints within the valid advice range. - Extended test in `core/libs/tests/GuestMemory.cpp` to verify advice up to 10 (`MADV_PROTECT`) returns success, while values > 10 return `EINVAL`. - Documented under Silent stubs in `docs/dev/TechnicalDebt.md`. ### Tested - `python tools/check_conventions.py --base origin/main` passed with 0 violations. - Verified zero comments in C++ implementation files. ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [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] AI-assisted: yes
github-actions[bot] commented 2026-10-12 00:19:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T00:19:13Z.


Progress report (bca7a29 → e6bf54d)

📈 System libraries: 86.5% (+0.03%, +1 implemented functions)

✅ 1 implemented
Library Name
libkernel madvise_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6115522570 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4154#issuecomment-6115522570). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T00:19:13Z. --- <!-- progress-report --> ### Progress report (bca7a29 → e6bf54d) 📈 **System libraries**: 86.5% (+0.03%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libkernel | `madvise_nid_postfix` | </details>
github-actions[bot] commented 2026-10-12 00:37:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T00:37:05Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#3795 core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp L228
#3820 docs/dev/TechnicalDebt.md L23-24
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6115682320 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4154#issuecomment-6115682320). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T00:37:05Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #3795 | `core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp` [L228](https://github.com/boykopovar/AnyPS5/blob/8c4a295ffb7bc471a5bcee2287fd38e499d003a2/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp#L228) | | | | #3820 | `docs/dev/TechnicalDebt.md` [L23-24](https://github.com/boykopovar/AnyPS5/blob/8c4a295ffb7bc471a5bcee2287fd38e499d003a2/docs/dev/TechnicalDebt.md#L23-L24) | | |
Dextroid17 commented 2026-10-12 01:29:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T01:29:19Z.


#4053: the fix applies a pattern that's already working elsewhere in this file rather than inventing new logic — close_nid_postfix, sceKernelRead, and sceKernelWrite already route guest-socket-range descriptors to GuestSockets::Close the same way this adds for sceKernelClose, and a closed or unknown socket still returns the same EBADF before and after, so there's no behavior change for anything that already worked.

The conflict with open #3764 is real but lower-severity than it might read: #3764 does add EqueueDescriptorClosed(d) as the very first line, at the same position this PR's socket check would occupy, so the two will textually conflict on merge. But tracing what EqueueDescriptorClosed actually does, calling it twice (once from #3764's line, once again inside GuestSockets::Close) is harmless — it's an idempotent erase-if over the equeue's event list, not a refcount decrement or a free. So whichever lands second just needs the socket check to run before the redundant equeue call, which is a one-line reordering, not a functional fix.

#4154: the no-op treatment is safe, and it's consistent with how this function already treats every other advisory-only hint in range — MADV_DONTNEED is already accepted as a pure no-op, and MADV_PROTECT's real FreeBSD semantics (don't kill this process under swap pressure) is exactly the kind of hint a host can't grant anyway, so ignoring it only forgoes a hint, not a requirement. The value 10 matches FreeBSD's own ordering for MADV_PROTECT. One cosmetic note: removing this throw leaves the GuestAdviceCore constant with no remaining reference — dead but harmless, since nothing in this build is -Werror=unused.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116140167 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4154#issuecomment-6116140167). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-12T01:29:19Z. --- #4053: the fix applies a pattern that's already working elsewhere in this file rather than inventing new logic — `close_nid_postfix`, `sceKernelRead`, and `sceKernelWrite` already route guest-socket-range descriptors to `GuestSockets::Close` the same way this adds for `sceKernelClose`, and a closed or unknown socket still returns the same `EBADF` before and after, so there's no behavior change for anything that already worked. The conflict with open #3764 is real but lower-severity than it might read: #3764 does add `EqueueDescriptorClosed(d)` as the very first line, at the same position this PR's socket check would occupy, so the two will textually conflict on merge. But tracing what `EqueueDescriptorClosed` actually does, calling it twice (once from #3764's line, once again inside `GuestSockets::Close`) is harmless — it's an idempotent erase-if over the equeue's event list, not a refcount decrement or a free. So whichever lands second just needs the socket check to run before the redundant equeue call, which is a one-line reordering, not a functional fix. #4154: the no-op treatment is safe, and it's consistent with how this function already treats every other advisory-only hint in range — `MADV_DONTNEED` is already accepted as a pure no-op, and `MADV_PROTECT`'s real FreeBSD semantics (don't kill this process under swap pressure) is exactly the kind of hint a host can't grant anyway, so ignoring it only forgoes a hint, not a requirement. The value 10 matches FreeBSD's own ordering for `MADV_PROTECT`. One cosmetic note: removing this throw leaves the `GuestAdviceCore` constant with no remaining reference — dead but harmless, since nothing in this build is `-Werror=unused`. Written with AI assistance.
oneandonlydean commented 2026-10-12 08:28:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T08:28:22Z.


Closing: no title is shown to call madvise with MADV_PROTECT, and FreeBSD passes it to PROC_SPROTECT, which fails with EPERM without PRIV_VM_MADV_PROTECT (see #3795), so returning 0 isn't shown to be what the console does. Reopen with the title ID and the log line where it stops on main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6120581108 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4154#issuecomment-6120581108). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-12T08:28:22Z. --- Closing: no title is shown to call `madvise` with `MADV_PROTECT`, and FreeBSD passes it to `PROC_SPROTECT`, which fails with `EPERM` without `PRIV_VM_MADV_PROTECT` (see #3795), so returning 0 isn't shown to be what the console does. Reopen with the title ID and the log line where it stops on main.

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!4154
No description provided.