feat(libkernel): return ESRCH and EINVAL for unsupported cpuset affinity targets #4585

Merged
jagrat04 merged 1 commit from upstream-pr/4585 into main 2026-10-13 15:23:02 +00:00
jagrat04 commented 2026-10-12 16:13:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jagrat04 on 2026-10-12T16:13:53Z.


What

Return ESRCH (3) for thread or process IDs other than the caller, and EINVAL (22) for invalid level/which arguments in cpuset_getaffinity and cpuset_setaffinity. Also support querying and setting affinity when the calling thread passes its own thread ID (scePthreadGetthreadid() / self->tid), matching FreeBSD 11.4 cpuset semantics instead of throwing NotImplemented.

Tested

tools/check_conventions.py --base origin/main
Unit test assertions in core/libs/tests/GuestAvailableCpumask.cpp

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
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4832063675 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4585). Originally posted by [jagrat04](https://github.com/jagrat04) on 2026-10-12T16:13:53Z. --- ### What Return `ESRCH` (3) for thread or process IDs other than the caller, and `EINVAL` (22) for invalid level/which arguments in `cpuset_getaffinity` and `cpuset_setaffinity`. Also support querying and setting affinity when the calling thread passes its own thread ID (`scePthreadGetthreadid()` / `self->tid`), matching FreeBSD 11.4 cpuset semantics instead of throwing `NotImplemented`. ### Tested tools/check_conventions.py --base origin/main Unit test assertions in core/libs/tests/GuestAvailableCpumask.cpp ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] Unblocks: none - [x] AI-assisted: yes
adriadam10 commented 2026-10-12 20:42:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-12T20:42:44Z.

Review: CHANGES_REQUESTED


Thanks for the PR. Checked against FreeBSD 11.4 kern_cpuset_getaffinity/kern_cpuset_setaffinity (sys/kern/kern_cpuset.c); build and guest_available_cpumask pass, but a few things don't match it:

  • Thread.cpp (new level != LevelWhich || (which != WhichThread && which != WhichProcess) check, both functions): CPU_LEVEL_ROOT (1) and CPU_LEVEL_CPUSET (2) are valid levels, and CPU_WHICH_CPUSET/CPU_WHICH_JAIL are valid scopes; only levels outside 1..3 hit the default: EINVAL. Returning EINVAL for the valid combinations is a plausible-but-wrong result where main threw. Return EINVAL only for levels outside 1..3 and keep throwing for valid combinations that aren't modelled.
  • Same lines: FreeBSD calls cpuset_which(which, id, ...) before the level switch, so an unknown id gives ESRCH even when the level is bad. Check the id first.
  • id != self->tid → ESRCH: another live thread of the process is a valid target in FreeBSD (cpuset_which finds it by tid). Either handle other guest threads (we know their tids) or keep throwing for them; ESRCH is only right for tids that don't exist.
  • docs/dev/debt/libkernel.md conflicts with main (the two cpuset lines moved); rebase and update the lines to say what is still unmodelled (other levels/scopes, other threads).
  • Tests: add rows for level 1/2 and which 3/4 (must not return EINVAL once handled, or must throw), and a bad level with an unknown id (ESRCH).
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5495756783 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4585#pullrequestreview-5495756783). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-12T20:42:44Z. **Review:** CHANGES_REQUESTED --- Thanks for the PR. Checked against FreeBSD 11.4 `kern_cpuset_getaffinity`/`kern_cpuset_setaffinity` (`sys/kern/kern_cpuset.c`); build and `guest_available_cpumask` pass, but a few things don't match it: - `Thread.cpp` (new `level != LevelWhich || (which != WhichThread && which != WhichProcess)` check, both functions): `CPU_LEVEL_ROOT` (1) and `CPU_LEVEL_CPUSET` (2) are valid levels, and `CPU_WHICH_CPUSET`/`CPU_WHICH_JAIL` are valid scopes; only levels outside 1..3 hit the `default: EINVAL`. Returning `EINVAL` for the valid combinations is a plausible-but-wrong result where `main` threw. Return `EINVAL` only for levels outside 1..3 and keep throwing for valid combinations that aren't modelled. - Same lines: FreeBSD calls `cpuset_which(which, id, ...)` before the level switch, so an unknown id gives `ESRCH` even when the level is bad. Check the id first. - `id != self->tid` → `ESRCH`: another live thread of the process is a valid target in FreeBSD (`cpuset_which` finds it by tid). Either handle other guest threads (we know their tids) or keep throwing for them; `ESRCH` is only right for tids that don't exist. - `docs/dev/debt/libkernel.md` conflicts with `main` (the two cpuset lines moved); rebase and update the lines to say what is still unmodelled (other levels/scopes, other threads). - Tests: add rows for level 1/2 and which 3/4 (must not return `EINVAL` once handled, or must throw), and a bad level with an unknown id (`ESRCH`).
jagrat04 commented 2026-10-13 01:03:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jagrat04 on 2026-10-13T01:03:14Z.


@adriadam10 Thanks for the detailed review against FreeBSD 11.4 kern_cpuset.c! Updated as requested:

  • Rebased cleanly on latest origin/main resolving the debt doc conflict.
  • Target ID validation: checked target ID before the level switch, returning ESRCH for unknown IDs even when the level is invalid (e.g. level 99 with unknown ID returns ESRCH).
  • Supported levels: level check only returns EINVAL for levels outside 1..3 (level < 1 || level > 3); valid levels 1 (CPU_LEVEL_ROOT) and 2 (CPU_LEVEL_CPUSET) as well as valid scopes 3..6 throw NotImplemented_nid_no_patch as unmodelled rather than returning EINVAL.
  • Other live threads: other live guest threads check via LiveGuestThreadExists(id) and throw as unmodelled rather than returning ESRCH; ESRCH is only returned for tids that do not exist.
  • Unit tests: added test cases for valid levels 1 and 2, scopes 3 and 4, invalid level with unknown ID (ESRCH), and live secondary thread throwing as unmodelled.
  • Technical debt: updated docs/dev/debt/libkernel.md to document the exact unmodelled levels, scopes, and secondary threads.
  • Verified locally with ctest -R guest_available_cpumask (passed) and tools/check_conventions.py --base origin/main.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6135805944 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4585#issuecomment-6135805944). Originally posted by [jagrat04](https://github.com/jagrat04) on 2026-10-13T01:03:14Z. --- @adriadam10 Thanks for the detailed review against FreeBSD 11.4 `kern_cpuset.c`! Updated as requested: - Rebased cleanly on latest `origin/main` resolving the debt doc conflict. - Target ID validation: checked target ID before the level switch, returning `ESRCH` for unknown IDs even when the level is invalid (e.g. level 99 with unknown ID returns `ESRCH`). - Supported levels: level check only returns `EINVAL` for levels outside 1..3 (`level < 1 || level > 3`); valid levels 1 (`CPU_LEVEL_ROOT`) and 2 (`CPU_LEVEL_CPUSET`) as well as valid scopes 3..6 throw `NotImplemented_nid_no_patch` as unmodelled rather than returning `EINVAL`. - Other live threads: other live guest threads check via `LiveGuestThreadExists(id)` and throw as unmodelled rather than returning `ESRCH`; `ESRCH` is only returned for tids that do not exist. - Unit tests: added test cases for valid levels 1 and 2, scopes 3 and 4, invalid level with unknown ID (`ESRCH`), and live secondary thread throwing as unmodelled. - Technical debt: updated `docs/dev/debt/libkernel.md` to document the exact unmodelled levels, scopes, and secondary threads. - Verified locally with `ctest -R guest_available_cpumask` (passed) and `tools/check_conventions.py --base origin/main`.
nicolasfalesy commented 2026-10-13 07:36:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nicolasfalesy on 2026-10-13T07:36:13Z.


Linux test (Arch, GCC 16.2.1, glibc, Release), this PR merged onto main 7e181fcc3 together with #4725, #4719, #4585 and #4582 (no conflicts): guest_available_cpumask passes. With this PR's source change reverted (test kept), it fails on Linux, so the check guards the change on Linux too.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140682677 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4585#issuecomment-6140682677). Originally posted by [nicolasfalesy](https://github.com/nicolasfalesy) on 2026-10-13T07:36:13Z. --- Linux test (Arch, GCC 16.2.1, glibc, Release), this PR merged onto `main` 7e181fcc3 together with #4725, #4719, #4585 and #4582 (no conflicts): `guest_available_cpumask` passes. With this PR's source change reverted (test kept), it fails on Linux, so the check guards the change on Linux too.
github-actions[bot] commented 2026-10-13 10:32:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T10:32:55Z.


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
#4901 docs/dev/debt/libkernel.md L128-130
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6143613924 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4585#issuecomment-6143613924). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T10:32:55Z. --- <!-- 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 | |---|---|---|---| | #4901 | `docs/dev/debt/libkernel.md` [L128-130](https://github.com/boykopovar/AnyPS5/blob/5a76fd716f8f88e67e9652bd77f5bfdc91fc01d0/docs/dev/debt/libkernel.md#L128-L130) | | |
adriadam10 commented 2026-10-13 14:46:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T14:46:28Z.

Review: APPROVED


Re-checked against FreeBSD 11.4 kern_cpuset_{get,set}affinity: target ID is validated before the level, valid levels 1..3 and scopes 3..6 throw as unmodelled, unknown IDs return ESRCH, invalid levels/scopes EINVAL. guest_available_cpumask passes and fails without the source change; debt lines updated. Merging once the full build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5505383277 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4585#pullrequestreview-5505383277). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T14:46:28Z. **Review:** APPROVED --- Re-checked against FreeBSD 11.4 `kern_cpuset_{get,set}affinity`: target ID is validated before the level, valid levels 1..3 and scopes 3..6 throw as unmodelled, unknown IDs return `ESRCH`, invalid levels/scopes `EINVAL`. `guest_available_cpumask` passes and fails without the source change; debt lines updated. Merging once the full build is green.
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!4585
No description provided.