fix(libkernel): move every knote kevent reports behind the waiting ones #9780

Merged
Retr0G28 merged 1 commit from upstream-pr/9780 into main 2026-10-26 01:12:11 +00:00
Retr0G28 commented 2026-10-22 14:43:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Retr0G28 on 2026-10-22T14:43:48Z.


What

kevent could keep returning the same knote and never reach the ones after it when nevents was smaller than the number of ready knotes. In FreeBSD 11.4, kqueue_scan (sys/kern/kern_event.c) puts a reported knote without EV_CLEAR or EV_DISPATCH back at the tail of the queue. It takes an EV_CLEAR or EV_DISPATCH knote off the queue, and knote_enqueue (from knote or from kqueue_register on EV_ENABLE) puts it back at the tail when it fires again. An EV_ONESHOT knote is dropped.

CollectOncePerWait, the collection loop behind kevent, moved a reported knote to the tail only if it was still triggered after its reset. A level EVFILT_READ or EVFILT_WRITE knote on a socket or pipe clears triggered until the next poll, and an EV_CLEAR or EV_DISPATCH knote clears it until it fires again. These knotes kept their registration position, so with nevents smaller than the number of ready knotes, the first of them was reported on every call. Now every reported knote that is not deleted moves behind the knotes that were not reported, as in kqueue_scan. A knote is still reported at most once per call, and EV_ONESHOT knotes are still deleted.

There is no gap sentence about this order in docs/dev/debt/libkernel.md. The kqueue entry already says that a reported level event moves behind the other events, and that is now true for socket and pipe knotes too.

#9127 is the companion change for sceKernelWaitEqueue (CollectTriggered). The two PRs are independent and merge without conflicts (git merge-tree of both branches is clean).

Test changes:

  • guest_kqueue (ReportedKnotesMoveToTail): level EVFILT_READ knotes on two pipes and a socketpair, all readable, are reported one per kevent(nevents = 1) call, each in turn, over two rounds. Two EV_CLEAR user knotes: after the first one is reported and triggered again, the second one comes first. Two EV_DISPATCH user knotes: after the first one is reported and then enabled and triggered in one change, the second one comes first.

Tested

Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, -DBUILD_TESTING=ON, rebased on main 11f976b27c.

  • Before the fix, guest_kqueue with the new test fails (exit 0xc0000409 from the failed Require). With temporary logging, each of the three cases fails on main: the level knotes return the first pipe on every call (expected 4, 6, 8; got 4, 4, 4), the EV_CLEAR case returns 11 where 12 is expected, and the EV_DISPATCH case returns 21 where 22 is expected. With this PR it passes.
  • guest_kqueue, guest_equeue_events, guest_socket_events, guest_poll, guest_select and guest_select_wait: 6 of 6 pass when run serially. After the rebase onto 11f976b27c, guest_kqueue passes again.
  • python tools/check_conventions.py --base origin/main reports no errors.
  • Linux, run by Dextroid17 on head 3383d303a7 (CachyOS, kernel 7.3.0-4, GCC 16.2.1, Release), alone and merged into main e2b9700a98. guest_kqueue passes 3 of 3 on both trees, including the Linux-only EdgeTriggered* and SocketWriteLowWaterWhileFilling cases, and guest_equeue_events passes 3 of 3. With only this PR's Equeue.cpp change reverted, guest_kqueue aborts in ReportedKnotesMoveToTail. The full suite on the merge has 3 failures, the agc_driver_sample_order* tests on an RTX 3080 Ti, and they fail on plain main too.
  • No title or homebrew run.

Overlap: tools/pr_overlap.py could not compare #6044, #7088, #7509 and #7653, which conflict with main. I checked their diffs. #6044 reorganizes the test tree, #7088 adds kevent events on regular files, #7509 bounds pending equeue events in AddEvent and TriggerEvent, and #7653 changes kevent timer re-adds and adds filters. None of them changes CollectOncePerWait or the order in which knotes are reported. #9127 changes CollectTriggered and the sceKernelWaitEqueue debt entry, and git merge-tree with this branch is clean. #9823 changes sceKernelDeleteAmprSystemEvent in Equeue.cpp, not the kqueue collection loop.

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:4969257748 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9780). Originally posted by [Retr0G28](https://github.com/Retr0G28) on 2026-10-22T14:43:48Z. --- ### What `kevent` could keep returning the same knote and never reach the ones after it when `nevents` was smaller than the number of ready knotes. In FreeBSD 11.4, `kqueue_scan` (`sys/kern/kern_event.c`) puts a reported knote without `EV_CLEAR` or `EV_DISPATCH` back at the tail of the queue. It takes an `EV_CLEAR` or `EV_DISPATCH` knote off the queue, and `knote_enqueue` (from `knote` or from `kqueue_register` on `EV_ENABLE`) puts it back at the tail when it fires again. An `EV_ONESHOT` knote is dropped. `CollectOncePerWait`, the collection loop behind `kevent`, moved a reported knote to the tail only if it was still triggered after its reset. A level `EVFILT_READ` or `EVFILT_WRITE` knote on a socket or pipe clears `triggered` until the next poll, and an `EV_CLEAR` or `EV_DISPATCH` knote clears it until it fires again. These knotes kept their registration position, so with `nevents` smaller than the number of ready knotes, the first of them was reported on every call. Now every reported knote that is not deleted moves behind the knotes that were not reported, as in `kqueue_scan`. A knote is still reported at most once per call, and `EV_ONESHOT` knotes are still deleted. There is no gap sentence about this order in `docs/dev/debt/libkernel.md`. The kqueue entry already says that a reported level event moves behind the other events, and that is now true for socket and pipe knotes too. #9127 is the companion change for `sceKernelWaitEqueue` (`CollectTriggered`). The two PRs are independent and merge without conflicts (`git merge-tree` of both branches is clean). Test changes: - `guest_kqueue` (`ReportedKnotesMoveToTail`): level `EVFILT_READ` knotes on two pipes and a socketpair, all readable, are reported one per `kevent(nevents = 1)` call, each in turn, over two rounds. Two `EV_CLEAR` user knotes: after the first one is reported and triggered again, the second one comes first. Two `EV_DISPATCH` user knotes: after the first one is reported and then enabled and triggered in one change, the second one comes first. ### Tested Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, `-DBUILD_TESTING=ON`, rebased on `main` 11f976b27c. - Before the fix, `guest_kqueue` with the new test fails (exit 0xc0000409 from the failed `Require`). With temporary logging, each of the three cases fails on `main`: the level knotes return the first pipe on every call (expected 4, 6, 8; got 4, 4, 4), the `EV_CLEAR` case returns 11 where 12 is expected, and the `EV_DISPATCH` case returns 21 where 22 is expected. With this PR it passes. - `guest_kqueue`, `guest_equeue_events`, `guest_socket_events`, `guest_poll`, `guest_select` and `guest_select_wait`: 6 of 6 pass when run serially. After the rebase onto 11f976b27c, `guest_kqueue` passes again. - `python tools/check_conventions.py --base origin/main` reports no errors. - Linux, run by Dextroid17 on head 3383d303a7 (CachyOS, kernel 7.3.0-4, GCC 16.2.1, Release), alone and merged into `main` e2b9700a98. `guest_kqueue` passes 3 of 3 on both trees, including the Linux-only `EdgeTriggered*` and `SocketWriteLowWaterWhileFilling` cases, and `guest_equeue_events` passes 3 of 3. With only this PR's `Equeue.cpp` change reverted, `guest_kqueue` aborts in `ReportedKnotesMoveToTail`. The full suite on the merge has 3 failures, the `agc_driver_sample_order*` tests on an RTX 3080 Ti, and they fail on plain `main` too. - No title or homebrew run. Overlap: `tools/pr_overlap.py` could not compare #6044, #7088, #7509 and #7653, which conflict with `main`. I checked their diffs. #6044 reorganizes the test tree, #7088 adds kevent events on regular files, #7509 bounds pending equeue events in `AddEvent` and `TriggerEvent`, and #7653 changes kevent timer re-adds and adds filters. None of them changes `CollectOncePerWait` or the order in which knotes are reported. #9127 changes `CollectTriggered` and the `sceKernelWaitEqueue` debt entry, and `git merge-tree` with this branch is clean. #9823 changes `sceKernelDeleteAmprSystemEvent` in `Equeue.cpp`, not the kqueue collection loop. ### 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
MarcZX10 commented 2026-10-22 16:34:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T16:34:03Z.

Review: DISMISSED


The collection-loop change moves each returned, non-deleted knote behind events that were not returned, matching the queue-order behavior described for FreeBSD kqueue_scan. The new tests cover level-triggered pipe/socket reads over multiple nevents=1 calls, plus retriggered EV_CLEAR and reenabled EV_DISPATCH user events. Current-main conventions and git diff --check pass. I did not rerun guest_kqueue locally. The PR reports the before/after failures and Windows test suite; upstream product jobs are skipped, and Linux is not reported as tested.

This branch is 13 commits behind live main (499b1cf9fc44da935fe6d63813675ad5810e8402). CONTRIBUTING.md requires branches to be based on and kept up to date with main. Please sync it, then rerun guest_kqueue on Linux as well as Windows on the final head; the changed event-collection loop is shared across platforms.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5617605762 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9780#pullrequestreview-5617605762). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T16:34:03Z. **Review:** DISMISSED --- The collection-loop change moves each returned, non-deleted knote behind events that were not returned, matching the queue-order behavior described for FreeBSD `kqueue_scan`. The new tests cover level-triggered pipe/socket reads over multiple `nevents=1` calls, plus retriggered `EV_CLEAR` and reenabled `EV_DISPATCH` user events. Current-main conventions and `git diff --check` pass. I did not rerun `guest_kqueue` locally. The PR reports the before/after failures and Windows test suite; upstream product jobs are skipped, and Linux is not reported as tested. This branch is 13 commits behind live `main` (`499b1cf9fc44da935fe6d63813675ad5810e8402`). CONTRIBUTING.md requires branches to be based on and kept up to date with `main`. Please sync it, then rerun `guest_kqueue` on Linux as well as Windows on the final head; the changed event-collection loop is shared across platforms.
MarcZX10 commented 2026-10-22 16:57:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T16:57:37Z.

Review: COMMENTED


The exact head c3e795b7daf659335617ada2c0ea4870aafbc5d3 has a clean merge tree with current main (11f976b27c16ed91633f00039dba5dfd97dd7cc1); conventions and diff-check pass. The queue-order change is covered by level-triggered pipe/socket and retriggered EV_CLEAR/EV_DISPATCH cases. The PR reports Windows tests but no Linux run; hosted product builds are skipped. The branch is 21 commits behind current main, so please rebase and rerun guest_kqueue on a supported host before merge. I did not rerun libkernel tests locally because this VM’s CPU lacks the repository-required feature set.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5617868008 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9780#pullrequestreview-5617868008). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T16:57:37Z. **Review:** COMMENTED --- The exact head `c3e795b7daf659335617ada2c0ea4870aafbc5d3` has a clean merge tree with current `main` (`11f976b27c16ed91633f00039dba5dfd97dd7cc1`); conventions and diff-check pass. The queue-order change is covered by level-triggered pipe/socket and retriggered EV_CLEAR/EV_DISPATCH cases. The PR reports Windows tests but no Linux run; hosted product builds are skipped. The branch is 21 commits behind current main, so please rebase and rerun `guest_kqueue` on a supported host before merge. I did not rerun libkernel tests locally because this VM’s CPU lacks the repository-required feature set.
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!9780
No description provided.