fix(libkernel): move events reported by sceKernelWaitEqueue behind the waiting ones #9127

Merged
Retr0G28 merged 2 commits from upstream-pr/9127 into main 2026-10-24 15:17:49 +00:00
Retr0G28 commented 2026-10-21 12:42:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Retr0G28 on 2026-10-21T12:42:30Z.


What

sceKernelWaitEqueue scanned the event list in place, so when num was smaller than the number of ready events, the first ones in the list were reported on every call and the later ones were never reported. In FreeBSD 11 kqueue_scan (sys/kern/kern_event.c), a reported knote without EV_CLEAR or EV_DISPATCH goes back in at the tail of kq_head. A knote with EV_CLEAR or EV_DISPATCH leaves the queue, and when it fires again knote_enqueue puts it at the tail. Either way, the next scan starts with the knotes that were waiting and not yet reported.

KernelEqueuePrivate::CollectTriggered now moves each event it reports to a separate list and appends that list to the end of the event list after the scan, the same way CollectOncePerWait does for kqueues. Events that were not reported keep their order. An EV_ONESHOT event is still removed. Edge events (EV_CLEAR) are moved too: this list keeps every registered event, so the tail is where FreeBSD would put the event when it fires again. Without the move, an edge event that fires before every call is reported each time and the others starve. Each event is visited at most once per call, so none is reported twice by mistake. Several pending AMPR or VideoOut completions are still returned in one call, as before.

The sentence about the missing rotation is cut from the sceKernelWaitEqueue entry in docs/dev/debt/libkernel.md. It is replaced by the gap that remains: events that fire between two calls are reported in registration order, not in the order they fired.

Test change: guest_equeue_events gets VerifyReportedEventsMoveBehindWaitingOnes:

  • two triggered level user events with num 1 give ids 1, 2, then 1;
  • three with num 2 give {1, 2}, {3, 1}, then {2, 3};
  • two edge user events, with the first one triggered again after it is reported, give 7, 8, then 7, and then nothing.

Tested

Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, -DBUILD_TESTING=ON, on main 2f161d4f33 (rebased on 7280a17ec0 afterwards; the two new commits do not touch these files).

  • Before the fix, guest_equeue_events with the new test fails (exit 0xc0000409 from the failed Require). Every section fails: the level pair gives 1, 1, 1; the three events give {1, 2} three times; the edge pair gives 7, 7, 8. With this PR it passes.
  • guest_equeue_events, guest_kqueue, guest_socket_events, guest_kernel_errors, np_uds_events, guest_ampr_markers, guest_ampr_threads, guest_http2 and guest_video_out_hdr: 9 of 9 pass when run serially.
  • video_out_flip_*: video_out_flip_decode passes; the other 13 skip on this machine (exit code 77). The AGC driver tests, which also wait on equeues, were not run.
  • python tools/check_conventions.py --base origin/main reports no errors.
  • Linux: @xXPBSXx ran the PR head on WSL2 Ubuntu 24.04 (GCC 13.3.0): guest_equeue_events passes, and fails with main's Equeue.cpp (comment below). Not run on bare-metal Linux.
  • No title or homebrew run.

Overlap: tools/pr_overlap.py could not compare some open PRs, so I checked the ones that touch the same code files. #7049 adds a delete callback inside the EV_ONESHOT branch of CollectTriggered. #7509 caps the pending events of one entry and adds a test. #7492 changes NotifyOne to NotifyAll and adds a test after VerifyEdgeUserEventReportedOnce; the new test here goes after VerifyRepeatedAmprCompletionsAreNotLost, away from that insertion. #6044 moves the test file. None of them changes the order in which events are reported. The rest touch only other lines of docs/dev/debt/libkernel.md.

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:4951724311 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9127). Originally posted by [Retr0G28](https://github.com/Retr0G28) on 2026-10-21T12:42:30Z. --- ### What `sceKernelWaitEqueue` scanned the event list in place, so when `num` was smaller than the number of ready events, the first ones in the list were reported on every call and the later ones were never reported. In FreeBSD 11 `kqueue_scan` (`sys/kern/kern_event.c`), a reported knote without `EV_CLEAR` or `EV_DISPATCH` goes back in at the tail of `kq_head`. A knote with `EV_CLEAR` or `EV_DISPATCH` leaves the queue, and when it fires again `knote_enqueue` puts it at the tail. Either way, the next scan starts with the knotes that were waiting and not yet reported. `KernelEqueuePrivate::CollectTriggered` now moves each event it reports to a separate list and appends that list to the end of the event list after the scan, the same way `CollectOncePerWait` does for kqueues. Events that were not reported keep their order. An `EV_ONESHOT` event is still removed. Edge events (`EV_CLEAR`) are moved too: this list keeps every registered event, so the tail is where FreeBSD would put the event when it fires again. Without the move, an edge event that fires before every call is reported each time and the others starve. Each event is visited at most once per call, so none is reported twice by mistake. Several pending AMPR or VideoOut completions are still returned in one call, as before. The sentence about the missing rotation is cut from the `sceKernelWaitEqueue` entry in `docs/dev/debt/libkernel.md`. It is replaced by the gap that remains: events that fire between two calls are reported in registration order, not in the order they fired. Test change: `guest_equeue_events` gets `VerifyReportedEventsMoveBehindWaitingOnes`: - two triggered level user events with `num` 1 give ids 1, 2, then 1; - three with `num` 2 give {1, 2}, {3, 1}, then {2, 3}; - two edge user events, with the first one triggered again after it is reported, give 7, 8, then 7, and then nothing. ### Tested Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, `-DBUILD_TESTING=ON`, on `main` 2f161d4f33 (rebased on 7280a17ec0 afterwards; the two new commits do not touch these files). - Before the fix, `guest_equeue_events` with the new test fails (exit 0xc0000409 from the failed `Require`). Every section fails: the level pair gives 1, 1, 1; the three events give {1, 2} three times; the edge pair gives 7, 7, 8. With this PR it passes. - `guest_equeue_events`, `guest_kqueue`, `guest_socket_events`, `guest_kernel_errors`, `np_uds_events`, `guest_ampr_markers`, `guest_ampr_threads`, `guest_http2` and `guest_video_out_hdr`: 9 of 9 pass when run serially. - `video_out_flip_*`: `video_out_flip_decode` passes; the other 13 skip on this machine (exit code 77). The AGC driver tests, which also wait on equeues, were not run. - `python tools/check_conventions.py --base origin/main` reports no errors. - Linux: @xXPBSXx ran the PR head on WSL2 Ubuntu 24.04 (GCC 13.3.0): `guest_equeue_events` passes, and fails with `main`'s `Equeue.cpp` (comment below). Not run on bare-metal Linux. - No title or homebrew run. Overlap: `tools/pr_overlap.py` could not compare some open PRs, so I checked the ones that touch the same code files. #7049 adds a delete callback inside the `EV_ONESHOT` branch of `CollectTriggered`. #7509 caps the pending events of one entry and adds a test. #7492 changes `NotifyOne` to `NotifyAll` and adds a test after `VerifyEdgeUserEventReportedOnce`; the new test here goes after `VerifyRepeatedAmprCompletionsAreNotLost`, away from that insertion. #6044 moves the test file. None of them changes the order in which events are reported. The rest touch only other lines of `docs/dev/debt/libkernel.md`. ### 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
xXPBSXx commented 2026-10-21 16:44:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T16:44:31Z.


Linux data point for 41709ae50e: 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; its merge-base is 7280a17ec).

  • With the PR: ctest -R '^guest_equeue_events$' passes 3 of 3.
  • With libkernel/Equeue/Equeue.cpp restored to the merge-base (the PR's tests kept): fails (Subprocess aborted).
    Not tested: macOS, Windows, a title run.
    AI-assisted (Claude Code).
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6321378606 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9127#issuecomment-6321378606). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T16:44:31Z. --- Linux data point for `41709ae50e`: 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`; its merge-base is `7280a17ec`). - With the PR: `ctest -R '^guest_equeue_events$'` passes 3 of 3. - With `libkernel/Equeue/Equeue.cpp` restored to the merge-base (the PR's tests kept): fails (`Subprocess aborted`). Not tested: macOS, Windows, a title run. AI-assisted (Claude Code).
harisibrar1 commented 2026-10-24 12:56:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T12:56:21Z.

Review: COMMENTED


Checked against main 99feaf0eb8 and FreeBSD 11.4 sys/kern/kern_event.c (8c639945):

  • It still merges cleanly onto main, and main's CollectTriggered (Equeue.cpp:185-227) is unchanged from your base, so the rotation isn't on main yet; only CollectOncePerWait has it.
  • The FreeBSD behaviour you cite checks out: reported non-EV_CLEAR/EV_DISPATCH knotes are reinserted at the tail (L1815), clear/dispatch ones are dequeued (L1801-1812), and knote_enqueue appends at the tail (L2542). Tracing kqueue_scan gives the same {1,2},{3,1},{2,3} and 7, 8, 7 sequences your test expects.
  • Small debt-wording fix: "events that fire between two calls are reported in registration order" isn't quite what the code now does, since after a report the list order is last-report order. Example: two edge user events 1 and 2; trigger 1 and wait (list becomes [2,1]); then trigger 1, then 2, and wait with num=4. This reports 2, 1, where "registration order" predicts 1, 2 (and FreeBSD gives 1, 2, firing order). Maybe: "events are reported in the order they were last reported (registration order for those never reported), where FreeBSD knote_enqueue queues them in the order they fire".

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638318399 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9127#pullrequestreview-5638318399). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T12:56:21Z. **Review:** COMMENTED --- Checked against main 99feaf0eb8 and FreeBSD 11.4 sys/kern/kern_event.c (8c639945): - It still merges cleanly onto main, and main's `CollectTriggered` (Equeue.cpp:185-227) is unchanged from your base, so the rotation isn't on main yet; only `CollectOncePerWait` has it. - The FreeBSD behaviour you cite checks out: reported non-`EV_CLEAR`/`EV_DISPATCH` knotes are reinserted at the tail (L1815), clear/dispatch ones are dequeued (L1801-1812), and `knote_enqueue` appends at the tail (L2542). Tracing `kqueue_scan` gives the same {1,2},{3,1},{2,3} and 7, 8, 7 sequences your test expects. - Small debt-wording fix: "events that fire between two calls are reported in registration order" isn't quite what the code now does, since after a report the list order is last-report order. Example: two edge user events 1 and 2; trigger 1 and wait (list becomes [2,1]); then trigger 1, then 2, and wait with num=4. This reports 2, 1, where "registration order" predicts 1, 2 (and FreeBSD gives 1, 2, firing order). Maybe: "events are reported in the order they were last reported (registration order for those never reported), where FreeBSD `knote_enqueue` queues them in the order they fire". Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
harisibrar1 commented 2026-10-24 13:16:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:16:26Z.

Review: APPROVED


Re-checked head f389bcce9c against main 6cd36e6517:

  • The debt wording now matches the code: each reported event is spliced to the end of the list (Equeue.cpp:169/173 and 209/213), so the report order is last-report order, with events never reported first in registration order, and the contrast with FreeBSD's knote_enqueue firing order is stated correctly.
  • The fix touches only docs/dev/debt/libkernel.md; the code and tests are unchanged from the version I traced before.
  • Main hasn't touched Equeue.cpp or the tests since, and it still merges cleanly. Thanks, LGTM.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638382698 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9127#pullrequestreview-5638382698). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T13:16:26Z. **Review:** APPROVED --- Re-checked head f389bcce9c against main 6cd36e6517: - The debt wording now matches the code: each reported event is spliced to the end of the list (Equeue.cpp:169/173 and 209/213), so the report order is last-report order, with events never reported first in registration order, and the contrast with FreeBSD's `knote_enqueue` firing order is stated correctly. - The fix touches only `docs/dev/debt/libkernel.md`; the code and tests are unchanged from the version I traced before. - Main hasn't touched `Equeue.cpp` or the tests since, and it still merges cleanly. Thanks, LGTM. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!9127
No description provided.