host_gpu: asynchronous write watches for write-hot regions on Linux #1147

Closed
github-bot wants to merge 1 commit from gh-pr/1147/head into gh-pr/1147/base
Owner

@⁠abokhalill on GitHub · 2026-10-07 15:34 UTC · original
abokhalill:async-write-watch → main · +732 −0 · 8 files · 1 commits · closed without merging

CPU writes to GPU-cached memory are tracked with mprotect and SIGSEGV. Every first write takes a signal and an mprotect under mmap_lock, and every re-arm on upload takes another mprotect with a TLB shootdown. These faults serialize across threads, so the more guest threads write, the slower each write gets (#1081).

This PR moves the write watches of regions that keep faulting to userfaultfd async write protection (UFFD_FEATURE_WP_ASYNC). The kernel lifts the protection on the first write without a signal, and PAGEMAP_SCAN collects and re-arms the written pages at each submission and satisfied WAIT_REG_MEM. Access watches stay on mprotect. Idle regions go back to mprotect. Opt-in with KYTY_ASYNC_WRITE_WATCH=1

PageManager + MemoryTracker, N threads writing 384 pages each per frame, then a re-upload:

  • 1 thread: 1.51ms -> 0.36ms
  • 6 threads: 13.0ms -> 1.19ms
  • 23 threads: 77.1ms -> 5.93ms

Tested on Linux 6.8 with RTX 6000 Ada.

**@⁠abokhalill** on GitHub · 2026-10-07 15:34 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1147) `abokhalill:async-write-watch` → `main` · +732 −0 · 8 files · 1 commits · closed without merging CPU writes to GPU-cached memory are tracked with `mprotect` and `SIGSEGV`. Every first write takes a signal and an `mprotect` under `mmap_lock`, and every re-arm on upload takes another `mprotect` with a TLB shootdown. These faults serialize across threads, so the more guest threads write, the slower each write gets (#1081). This PR moves the write watches of regions that keep faulting to userfaultfd async write protection (`UFFD_FEATURE_WP_ASYNC`). The kernel lifts the protection on the first write without a signal, and `PAGEMAP_SCAN` collects and re-arms the written pages at each submission and satisfied `WAIT_REG_MEM`. Access watches stay on `mprotect`. Idle regions go back to `mprotect`. Opt-in with `KYTY_ASYNC_WRITE_WATCH=1` `PageManager` + `MemoryTracker`, N threads writing 384 pages each per frame, then a re-upload: - 1 thread: 1.51ms -> 0.36ms - 6 threads: 13.0ms -> 1.19ms - 23 threads: 77.1ms -> 5.93ms Tested on Linux 6.8 with RTX 6000 Ada.
CPU write tracking for GPU-cached guest memory write-protects pages with
mprotect and learns about writes from SIGSEGV. Every first write costs a
signal, a 4 KB mprotect under the process-wide address-space mutex and the
kernel's mmap_lock, and the re-arm on upload costs another mprotect with a
TLB shootdown. The faults serialize, so the cost grows with the number of
guest threads that write.

Regions that take 64 write faults within 64 harvests now switch their write
watches to userfaultfd write protection in asynchronous mode
(UFFD_FEATURE_WP_ASYNC): the kernel lifts the protection on the first write
without a signal, and PAGEMAP_SCAN collects and re-arms the written pages
in one call. Access watches (GPU-modified pages) stay synchronous. A region
whose harvests find no write 512 times in a row returns to mprotect.

Writes are delivered to the buffer and texture caches as if they had
faulted, at each submission slice and at each satisfied WAIT_REG_MEM, the
points where the guest orders CPU writes before GPU work. Ranges the GPU is
about to own are harvested first, so a pending CPU write reaches the GPU
copy before a later download could revert it.

Opt-in with KYTY_ASYNC_WRITE_WATCH=1; requires Linux 6.7+.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:45:00 +00:00
github-bot closed this pull request 2026-11-26 09:45:00 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-07 15:43 UTC · original · edited 2026-10-07 15:46 UTC

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 23ab6c46-d851-4853-81e3-1abfa0704e01
📥 Commits

Reviewing files that changed from the base of the PR and between 5da5200677f8bd12ff137d323c6b407df4121164 and d1830999f7.

📒 Files selected for processing (3)
  • src/graphics/host_gpu/memoryTracker.h
  • src/graphics/host_gpu/pageManager.h
  • src/graphics/host_gpu/renderer/renderContext.h

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.


📝 Walkthrough

Walkthrough

Linux PageManager adds optional asynchronous write watches, write harvesting, and idle demotion. RenderContext and MemoryTracker use harvested writes to update caches and CPU-modified memory state. GuestGpu harvests writes before submissions and after satisfied waits. Linux tests cover harvesting and watch lifecycle behavior.

Suggested reviewers: nmzik

Priority: ➖ Normal

Change: Feature

Merge Risk: ⚪ Minimal · up to d1830

The change adds an opt-in Linux write-watch feature. No concrete merge-blocking issue was identified in the supplied context.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 17.39% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 46 functions across 8 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: asynchronous write watches for write-hot regions on Linux.
Description check ✅ Passed The description directly explains the performance problem, implementation, configuration, benchmark results, and test environment for the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-07 15:43 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1147#issuecomment-6041377444) · edited 2026-10-07 15:46 UTC <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/1147?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=2" alt="Review in Change Stack →" width="220" height="32"></a> <!-- review_stack_entry_end --> <!-- recent_review_start --> No actionable comments were generated in the recent review. 🎉 <details> <summary>ℹ️ Recent review info</summary> <details> <summary>⚙️ Run configuration</summary> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `23ab6c46-d851-4853-81e3-1abfa0704e01` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between 5da5200677f8bd12ff137d323c6b407df4121164 and d1830999f78b99b66a1d50114a2c0e0de254d880. </details> <details> <summary>📒 Files selected for processing (3)</summary> * `src/graphics/host_gpu/memoryTracker.h` * `src/graphics/host_gpu/pageManager.h` * `src/graphics/host_gpu/renderer/renderContext.h` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review. </details> --- <!-- recent_review_end --> <!-- walkthrough_start --> <details> <summary>📝 Walkthrough</summary> ## Walkthrough Linux `PageManager` adds optional asynchronous write watches, write harvesting, and idle demotion. `RenderContext` and `MemoryTracker` use harvested writes to update caches and CPU-modified memory state. `GuestGpu` harvests writes before submissions and after satisfied waits. Linux tests cover harvesting and watch lifecycle behavior. **Suggested reviewers:** `nmzik` </details> <!-- walkthrough_end --> <!-- change_assessment_start --> **Priority:** ➖ Normal <!-- change_assessment_commit:"d1830999f78b99b66a1d50114a2c0e0de254d880" --> **Change:** Feature <!-- change_assessment_end --> <!-- final_review_risk_start --> **Merge Risk:** _⚪ Minimal_ · up to `d1830` <!-- final_review_risk_coverage:{"sourceCommitId":"d1830999f78b99b66a1d50114a2c0e0de254d880","coveredCommitId":"d1830999f78b99b66a1d50114a2c0e0de254d880","kind":"reviewed"} --> The change adds an opt-in Linux write-watch feature. No concrete merge-blocking issue was identified in the supplied context. <!-- final_review_risk_end --> <!-- pre_merge_checks_walkthrough_start --> <details> <summary>🚥 Pre-merge checks | ✅ 4 | ❌ 1</summary> ### ❌ Failed checks (1 warning) | Check name | Status | Explanation | Resolution | | :----------------: | :--------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :--------------------------------------------------------------------------------- | | Docstring Coverage | ⚠️ Warning | Docstring coverage is 17.39% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 46 functions across 8 files. | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary>✅ Passed checks (4 passed)</summary> | Check name | Status | Explanation | | :------------------------: | :------- | :--------------------------------------------------------------------------------------------------------------------------------------------------- | | Title check | ✅ Passed | The title clearly and concisely describes the main change: asynchronous write watches for write-hot regions on Linux. | | Description check | ✅ Passed | The description directly explains the performance problem, implementation, configuration, benchmark results, and test environment for the changeset. | | Linked Issues check | ✅ Passed | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ✅ Passed | Check skipped because no linked issues were found for this pull request. | </details> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- autopilot:start --> - [ ] <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autopilot</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts <!-- autopilot:end --> <!-- tips_start --> --- Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=1147)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary>❤️ Share</summary> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </details> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.