kernel: return released guest memory to the host #1269
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1269
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1269"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@msfvn on GitHub · 2026-10-09 02:25 UTC · original
msfvn:release-guest-memory→main· +96 −1 · 3 files · 2 commits · openProblem
Direct and flexible memory share one memfd.
ReleaseDirectMemoryand flexibleUnmaponly update the emulator's bookkeeping, so the host keeps (or swaps) every page the game has ever written.FlexibleMemory::Mapalsomemsets the whole new range, committing pages the game may never touch. Astro Bot streams through gigabytes of direct memory while loading; on a machine with 8 GB of RAM the host ran out of memory during the first loading screen.Change
GuestBackingStore::DiscardRangepunches the range out of the memfd (fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)); called for released direct memory and unmapped flexible memory.ZeroRange(used when mapping flexible memory) punches a hole instead ofmemset, falling back tomemsetwhere unsupported (Windows, macOS keep their current behavior).Both read back as zeros, as before.
Testing
ReleasedMemoryReturnsToHostcase invirtual_memory_allocation_tests(Linux): checks that released direct memory and unmapped flexible memory leave the backing (via the memfd'sst_blocks) and that mapping flexible memory does not commit it. It fails without this change; all 63 cases pass with it.AI involvement: written with Claude Code (AI assistant) at my request and reviewed by a C++ developer I know; tested on my machine (RTX 3050 Laptop 4 GB, CachyOS) as described above, including playing Astro Bot on this build. Happy to adjust anything you point out.
@coderabbitai[bot] on GitHub · 2026-10-09 02:29 UTC · original · edited 2026-10-11 01:22 UTC
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
960ae3bf-a6fd-47c7-b0b1-8ff8228a2956📥 Commits
Reviewing files that changed from the base of the PR and between
9e925ee152and266eba7276.📒 Files selected for processing (1)
tests/VirtualMemoryAllocationTests.cpp🚧 Files skipped from review as they are similar to previous changes (1)
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
Walkthrough
GuestBackingStorenow attempts to punch holes in the backing file when it zeroes ranges. It falls back tomemsetif hole punching fails. Direct-memory release and flexible-memory unmapping now request backing-range discards. Linux tests measure backing-file allocation after release and check flexible-memory mapping behavior.Priority: ➖ Normal
Change: Bug fix
Merge Risk: ⚪ Minimal · up to
266ebThe change returns released guest memory to the host on Linux, with a fallback where hole punching is unsupported. No actionable merge-blocking risk is evident from the supplied context.
Pre-merge checks |
4 |
1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
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 helpto get the list of available commands.@ -1493,0 +1509,4 @@}}return 0;}@coderabbitai[bot] on GitHub · 2026-10-09 02:29 UTC · original · edited 2026-10-11 02:00 UTC
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 11663
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 26136
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 11528
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 2157
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 2746
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 2633
Handle iterator-construction errors in
GuestBackingKiB.When
/proc/self/fdcannot be opened,std::filesystem::directory_iterator("/proc/self/fd")throwsstd::filesystem::filesystem_error.RunTestcatches onlyTestFailure, so this exception can terminate the test process. Pass an error code to the iterator constructor. The helper will then return zero, and the existing checks will report a handled test failure.Suggested fix
📝 Committable suggestion
✅ Addressed in commit
266eba7@coderabbitai[bot] on GitHub · 2026-10-09 02:29 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
ea210989-1f0f-41ed-a51f-aa4a525eb994📥 Commits
Reviewing files that changed from the base of the PR and between
7b9997baeaand9e925ee152.📒 Files selected for processing (3)
src/kernel/memory.cppsrc/kernel/memoryAddressSpace.inctests/VirtualMemoryAllocationTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
@ -1493,0 +1509,4 @@}}return 0;}@msfvn on GitHub · 2026-10-11 02:00 UTC · original
Fixed in
266eba72: the helper uses theerror_codeoverload ofdirectory_iterator, so a missing/proc/self/fdreturns 0 and the checks report a normal failure.@ -1493,0 +1509,4 @@}}return 0;}@coderabbitai[bot] on GitHub · 2026-10-11 02:01 UTC · original
@msfvnThanks for the fix. Theerror_codeoverload handles a missing/proc/self/fdwithout afilesystem_error, soGuestBackingKiB()returns 0.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.