feat(libkernel_sys): implement cpuset, cpuset_getid and cpuset_setid #10741

Merged
camezaro-intc merged 2 commits from upstream-pr/10741 into main 2026-10-27 05:13:50 +00:00
camezaro-intc commented 2026-10-24 20:25:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by camezaro-intc on 2026-10-24T20:25:07Z.


What

Implement cpuset, cpuset_getid and cpuset_setid in libkernel_sys following FreeBSD 11.4 kern_cpuset.c:

  • cpuset: allocates a sequential CPU set ID from 2 upwards, assigns the process to it, and stores the ID.
  • cpuset_setid: supports CPU_WHICH_PID to assign known set IDs (or root 0 and default 1) to the calling process.
  • cpuset_getid: implements CPU_WHICH_PID: returns root 0 for CPU_LEVEL_ROOT and the assigned set ID for CPU_LEVEL_CPUSET. Invalid levels or which specifiers (IRQ, DOMAIN) and non-cpuset CPU_LEVEL_WHICH fail with EINVAL; unknown process IDs fail with ESRCH; out-of-range set IDs fail with ESRCH without integer wrapping; null pointers fail with EFAULT. Unmodeled scopes (CPU_WHICH_TID, CPU_WHICH_CPUSET, CPU_WHICH_JAIL) throw NotImplemented_nid_no_patch.
  • Removes the three stubs from Export.Discovery.cpp, registers unit tests in core/libs/tests/GuestKernelSysCpuset.cpp, and records technical debt in docs/dev/debt/libkernel_sys.md.

Tested

Windows 11 (AMD Ryzen 5 3600X, MinGW-w64 GCC 15.2.0):

  • guest_kernel_sys_cpuset unit test suite passes 100% exercising cpuset, process-level cpuset_getid (valid transitions, root/cpuset levels, error conditions, out-of-range IDs), and cpuset_setid (valid transitions, invalid which, non-existent PID, invalid set IDs).
  • Verified python tools/check_conventions.py --base upstream/main passes with zero errors and zero warnings.

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:4998845686 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10741). Originally posted by [camezaro-intc](https://github.com/camezaro-intc) on 2026-10-24T20:25:07Z. --- ### What Implement `cpuset`, `cpuset_getid` and `cpuset_setid` in `libkernel_sys` following FreeBSD 11.4 `kern_cpuset.c`: - `cpuset`: allocates a sequential CPU set ID from 2 upwards, assigns the process to it, and stores the ID. - `cpuset_setid`: supports `CPU_WHICH_PID` to assign known set IDs (or root 0 and default 1) to the calling process. - `cpuset_getid`: implements `CPU_WHICH_PID`: returns root 0 for `CPU_LEVEL_ROOT` and the assigned set ID for `CPU_LEVEL_CPUSET`. Invalid levels or `which` specifiers (`IRQ`, `DOMAIN`) and non-cpuset `CPU_LEVEL_WHICH` fail with `EINVAL`; unknown process IDs fail with `ESRCH`; out-of-range set IDs fail with `ESRCH` without integer wrapping; null pointers fail with `EFAULT`. Unmodeled scopes (`CPU_WHICH_TID`, `CPU_WHICH_CPUSET`, `CPU_WHICH_JAIL`) throw `NotImplemented_nid_no_patch`. - Removes the three stubs from `Export.Discovery.cpp`, registers unit tests in `core/libs/tests/GuestKernelSysCpuset.cpp`, and records technical debt in `docs/dev/debt/libkernel_sys.md`. ### Tested Windows 11 (AMD Ryzen 5 3600X, MinGW-w64 GCC 15.2.0): - `guest_kernel_sys_cpuset` unit test suite passes 100% exercising `cpuset`, process-level `cpuset_getid` (valid transitions, root/cpuset levels, error conditions, out-of-range IDs), and `cpuset_setid` (valid transitions, invalid which, non-existent PID, invalid set IDs). - Verified `python tools/check_conventions.py --base upstream/main` passes with zero errors and zero warnings. ### 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-24 20:25:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T20:25:57Z.

Review: CHANGES_REQUESTED


Reviewed exact head 2e875b85062935f7af7b2322eb1779750b8ad75e. The new tests cover normal set creation, process/thread lookups, invalid levels and set IDs, and the reported Windows focused suite passes. I found one correctness gap in cpuset_getid_nid_postfix: for which == WhichCpuset, it converts the 64-bit id to int before checking membership. Values outside the int range can wrap to an existing ID; for example, on the usual 32-bit int, id == 4294967296 becomes 0 and can report the root set successfully instead of ESRCH. Please reject IDs that cannot be represented by the guest cpuset ID type before the cast, and add a regression asserting that an out-of-range ID fails without changing the output. I am requesting changes for this malformed-ID behavior. Hosted eligibility, conventions, intake and labels pass; product build jobs were skipped, and I could not run the focused test locally because this checkout has no configured build directory.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5639818672 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10741#pullrequestreview-5639818672). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T20:25:57Z. **Review:** CHANGES_REQUESTED --- Reviewed exact head `2e875b85062935f7af7b2322eb1779750b8ad75e`. The new tests cover normal set creation, process/thread lookups, invalid levels and set IDs, and the reported Windows focused suite passes. I found one correctness gap in `cpuset_getid_nid_postfix`: for `which == WhichCpuset`, it converts the 64-bit `id` to `int` before checking membership. Values outside the `int` range can wrap to an existing ID; for example, on the usual 32-bit `int`, `id == 4294967296` becomes 0 and can report the root set successfully instead of `ESRCH`. Please reject IDs that cannot be represented by the guest cpuset ID type before the cast, and add a regression asserting that an out-of-range ID fails without changing the output. I am requesting changes for this malformed-ID behavior. Hosted eligibility, conventions, intake and labels pass; product build jobs were skipped, and I could not run the focused test locally because this checkout has no configured build directory.
MarcZX10 commented 2026-10-24 20:35:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T20:35:18Z.

Review: APPROVED


Reviewed updated head 82b0d7d1b9e3e69c7fd299a9f3bee5317d7214de. The new representability check runs before the int cast for WhichCpuset, and the regression cases cover positive and negative out-of-range IDs while asserting that *setid remains untouched for all three levels. The author reports the focused Windows guest_kernel_sys_cpuset suite and conventions check passing. I did not run the C++ test locally; the hosted workflow skipped product test jobs, so that result remains author-reported. The exact-head conventions, intake, eligibility, and labels checks pass.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5639870100 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10741#pullrequestreview-5639870100). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T20:35:18Z. **Review:** APPROVED --- Reviewed updated head `82b0d7d1b9e3e69c7fd299a9f3bee5317d7214de`. The new representability check runs before the `int` cast for `WhichCpuset`, and the regression cases cover positive and negative out-of-range IDs while asserting that `*setid` remains untouched for all three levels. The author reports the focused Windows `guest_kernel_sys_cpuset` suite and conventions check passing. I did not run the C++ test locally; the hosted workflow skipped product test jobs, so that result remains author-reported. The exact-head conventions, intake, eligibility, and labels checks pass.
MarcZX10 commented 2026-10-25 00:09:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T00:09:18Z.

Review: CHANGES_REQUESTED


The process-level model is internally consistent for the tested paths, but cpuset_getid reports support for CPU_WHICH_TID and CPU_WHICH_CPUSET that the existing affinity implementation does not currently provide. For TIDs, it calls cpuset_getaffinity_nid_postfix(LevelWhich, WhichThread, ...); that function can resolve a live guest thread, but this PR still returns currentProcessSetId for every TID instead of tracking each thread’s assigned cpuset. cpuset() and cpuset_setid(CPU_WHICH_PID, ...) update only the process-wide ID, so an existing thread-specific model/affinity change can make the result incorrect. For CPUSET lookups, newly allocated set IDs are global here, while FreeBSD resolves them relative to the caller’s visible set namespace; the current test expects querying set1 while joined to set2 to succeed, which may not match the 11.4 reference. Please either limit claims/behavior to the process-level operations modeled here or add per-thread assignment and verify CPUSET visibility and CPU_LEVEL_ROOT semantics against the target/reference. I inspected the tests and FreeBSD 11.4 kernel code; I could not run the Windows-only target locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640711988 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10741#pullrequestreview-5640711988). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T00:09:18Z. **Review:** CHANGES_REQUESTED --- The process-level model is internally consistent for the tested paths, but `cpuset_getid` reports support for `CPU_WHICH_TID` and `CPU_WHICH_CPUSET` that the existing affinity implementation does not currently provide. For TIDs, it calls `cpuset_getaffinity_nid_postfix(LevelWhich, WhichThread, ...)`; that function can resolve a live guest thread, but this PR still returns `currentProcessSetId` for every TID instead of tracking each thread’s assigned cpuset. `cpuset()` and `cpuset_setid(CPU_WHICH_PID, ...)` update only the process-wide ID, so an existing thread-specific model/affinity change can make the result incorrect. For CPUSET lookups, newly allocated set IDs are global here, while FreeBSD resolves them relative to the caller’s visible set namespace; the current test expects querying set1 while joined to set2 to succeed, which may not match the 11.4 reference. Please either limit claims/behavior to the process-level operations modeled here or add per-thread assignment and verify CPUSET visibility and `CPU_LEVEL_ROOT` semantics against the target/reference. I inspected the tests and FreeBSD 11.4 kernel code; I could not run the Windows-only target locally.
MarcZX10 commented 2026-10-25 02:52:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T02:52:05Z.

Review: DISMISSED


Reviewed updated head 9c34140bd3bfcb5d19602fb5548445fc7724698d. The implementation and debt entry now limit cpuset_getid to CPU_WHICH_PID, and the new tests expect the other scopes to throw. However, the PR description still claims CPU_LEVEL_CPUSET supports CPU_WHICH_TID and CPU_WHICH_CPUSET, and CPU_LEVEL_WHICH supports CPU_WHICH_CPUSET; its Tested section likewise says the suite exercises all scopes and set lookups. In Cpuset.cpp, those requests reach if (which != WhichProcess) NotImplemented_nid_no_patch(...), so those claims are false for this head. Please align the What and Tested sections with the intentionally process-only behavior. The earlier representability fix is present in the code and tests. This branch is currently DIRTY against main, so it will also need to be made mergeable before landing; I did not rebase another contributor’s branch.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641241215 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10741#pullrequestreview-5641241215). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T02:52:05Z. **Review:** DISMISSED --- Reviewed updated head `9c34140bd3bfcb5d19602fb5548445fc7724698d`. The implementation and debt entry now limit `cpuset_getid` to `CPU_WHICH_PID`, and the new tests expect the other scopes to throw. However, the PR description still claims `CPU_LEVEL_CPUSET` supports `CPU_WHICH_TID` and `CPU_WHICH_CPUSET`, and `CPU_LEVEL_WHICH` supports `CPU_WHICH_CPUSET`; its Tested section likewise says the suite exercises all scopes and set lookups. In `Cpuset.cpp`, those requests reach `if (which != WhichProcess) NotImplemented_nid_no_patch(...)`, so those claims are false for this head. Please align the What and Tested sections with the intentionally process-only behavior. The earlier representability fix is present in the code and tests. This branch is currently DIRTY against main, so it will also need to be made mergeable before landing; I did not rebase another contributor’s branch.
MarcZX10 commented 2026-10-25 04:15:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T04:15:56Z.

Review: COMMENTED


Re-reviewed the rebased head ea3de34867ec76bf3080437e0bbd0efc154a13c5. The description now matches the implementation’s process-only scope, the out-of-range ID check and tests remain present, and the branch is clean against main. Hosted conventions, eligibility, and labels pass; product builds/tests are skipped by the PR workflow, and I did not rerun the Windows-only focused suite locally. No further issue found in this update. I am withdrawing my prior change request.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641461982 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10741#pullrequestreview-5641461982). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T04:15:56Z. **Review:** COMMENTED --- Re-reviewed the rebased head `ea3de34867ec76bf3080437e0bbd0efc154a13c5`. The description now matches the implementation’s process-only scope, the out-of-range ID check and tests remain present, and the branch is clean against `main`. Hosted conventions, eligibility, and labels pass; product builds/tests are skipped by the PR workflow, and I did not rerun the Windows-only focused suite locally. No further issue found in this update. I am withdrawing my prior change request.
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!10741
No description provided.