memory: exclude AGC driver reservation from Demon's Souls guest budget #1399
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!1399
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1399"
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?
@kewalker on GitHub · 2026-10-11 03:00 UTC · original
kewalker:codex/demons-souls-agc-startup→main· +376 −340 · 5 files · 12 commits · openDemon's Souls can claim all advertised direct memory before calling
sce::Agc::init(). The lazy 2 MiB AGC driver allocation introduced in165cd884then fails with0x80020023(KERNEL_ERROR_EAGAIN), and the game hits its fatalint 0x41assertion during startup.Build on upstream
9a5dd9fa, which reserves and initializes AGC driver memory before guest execution. Allocate the tracked block at the end of direct memory and exclude it from the guest's advertised memory budget. Preserve upstream function-table initialization and release the budget reservation during graphics shutdown.The regression test reserves the driver block, lets the guest exhaust the remaining direct-memory pool, and verifies that AGC initialization and the existing system-table checks still succeed. A focused
--agc-system-table-onlyoption runs this test independently.Fixes #1392.
Validation:
shader_recompiler_compute_tests.exe --agc-system-table-onlypassed on the local fixed build.git diff --checkpassed. Formatting changes in the four production files come from the required hook.AI assistance and approval: Codex assisted with diagnosis, implementation, regression testing, and this PR description. I confirm that I reviewed the changes and tested them myself.
This PR contains the startup fix and its regression coverage.
Merged current main (
c0ebf224), preserving upstream AGC initialization and its Windows test-variable fix.The allocation explicitly aligns the physical pool end down; the budget excludes the driver block plus any tail alignment padding. The unaligned-pool regression verifies this contiguous guest budget. Restored AGC system-table initialization after repairing a misapplied GitHub suggestion.
@coderabbitai[bot] on GitHub · 2026-10-11 03:06 UTC · original · edited 2026-10-21 15:37 UTC
📝 Walkthrough
The kernel now tracks a system direct-memory reserve and subtracts it from the guest direct-memory size. The AGC driver allocates its DMEM region from the aligned end of direct memory and clears the reserve when it releases the allocation. Compute tests check the reduced guest budget, allocation exhaustion, the
EAGAINresult, and sparse-image buffer retention. Command-line selectors run the AGC test with standard and unaligned memory pools.Suggested reviewers:
brandostrongPriority: ➖ Normal
Change: Bug fix · Severity of issue fixed: Medium
Merge Risk: 🔵 Low · up to
5d81cThe change fixes the AGC startup allocation failure. In a narrow unaligned-pool case, a guest can still allocate slightly beyond its reported memory budget. This is a small gap to address before or shortly after merge.
Pre-merge checks:
4 ·
1
#1392reports the Demon’s Souls startup failure but provides no explicit coding acceptance criteria. The PR addresses the reported failure mechanism by reserving the AGC driver block before gues…#1392. The kernel reserve API, AGC allocation and release logic, regression coverage, test selectors, and required formatting support the startup fix or its valid…Full details: Linked Issues check
Explanation
Issue
#1392reports the Demon’s Souls startup failure but provides no explicit coding acceptance criteria. The PR addresses the reported failure mechanism by reserving the AGC driver block before guest execution and excluding the block and tail padding from the guest direct-memory budget. The regression test exhausts the guest budget before AGC initialization and checks the existing system-table behavior. The focused test selector supports independent validation.Full details: Out of Scope Changes check
Explanation
The changes remain connected to issue
#1392. The kernel reserve API, AGC allocation and release logic, regression coverage, test selectors, and required formatting support the startup fix or its validation. The image-upload test adjustment does not demonstrate unrelated production behavior. No concrete out-of-scope change is established.Full details: Docstring Coverage
Explanation
Docstring coverage is 4.23% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 71 functions across 4 files. (1 skipped: 1 too large.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
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.@ -4092,2 +4092,4 @@g_driver_dmem.physical_offset = physical_offset;// Hide the driver block and any unaligned tail so the guest budget ends before the mapping.LibKernel::Memory::SetSystemDirectMemoryReserve(reported_end - start);return OK;@coderabbitai[bot] on GitHub · 2026-10-11 03:06 UTC · original · edited 2026-10-11 03:29 UTC
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
end - startcan over-reserve and conflicts with the AGC mapping on a retry.startisAlignDown(end - DriverDmemSize, DriverDmemSize). Ifendis not 2 MiB aligned, the reserve (end - start) is larger than 2 MiB. The allocation covers[start, start + 2 MiB). Memory betweenstart + 2 MiBandendis excluded from the guest budget but not allocated.The effect is small and bounded. Record the exact allocated size, or align
enddown first, so the reserve matches the block.Proposed fix
The reserve must still cover
reported_end - startif the tail should be hidden. Choose one behavior deliberately.📝 Committable suggestion
✅ Addressed in commit
9a6ed5c@coderabbitai[bot] on GitHub · 2026-10-11 03:06 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
89903cc0-068b-47f3-ba9d-e218b21bf25a📥 Commits
Reviewing files that changed from the base of the PR and between
730e1a3cf3anda35ae61e84.📒 Files selected for processing (5)
src/kernel/memory.cppsrc/kernel/memory.hsrc/libs/agc.cppsrc/libs/agc.htests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
@coderabbitai[bot] on GitHub · 2026-10-11 03:34 UTC · original · edited 2026-10-11 07:53 UTC
On
src/libs/agc.cppline 4199 (outdated):🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 3297
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 7221
Remove the first allocation block.
Gen5Driver::Initializedeclaresphysical_offset,end,start,result, andaddressat lines 4155–4172. Lines 4178–4196 redeclare the same identifiers in the same scope. The C++ compiler rejects these redeclarations, so builds that compilesrc/libs/agc.cppfail.Suggested fix
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Restore AGC system-table initialization after mapping.
When the mapped driver block is available,
Gen5Driver::Initializecurrently clears it and returns without writing the descriptor, eight function records, or validator shader.Gen5::AgcInitdoes not populate these entries.CheckAgcSystemTablerequires all of them, so AGC initialization leaves an invalid system table and the system-table test can fail.Suggested fix
📝 Committable suggestion
✅ Confirmed as addressed by @kewalker
@coderabbitai[bot] on GitHub · 2026-10-11 03:34 UTC · original · reviewed
Actionable comments posted: 2
ℹ️ Review info
⚙️ Run configuration
51acd021-6ed4-4214-8ccd-26626a20dfa8📥 Commits
Reviewing files that changed from the base of the PR and between
9a4f004fcaand9a6ed5c84b.📒 Files selected for processing (1)
src/libs/agc.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.
@kewalker on GitHub · 2026-10-11 07:52 UTC · original
On
src/libs/agc.cppline 4199 (outdated):Resolved the new test-variable conflict using upstream's
small_arrayname. Repaired the misapplied AGC suggestion by removing the duplicate allocation block and restoring the descriptor, eight function records, and validator shader.The allocation now explicitly aligns the physical end down before choosing the 2 MiB block. The advertised guest budget ends at the block's start: any remaining alignment padding above the block is deliberately hidden as well. Reporting only a 2 MiB deduction for an unaligned pool would expose part of the driver mapping inside the guest budget. Added
--agc-unaligned-pool-onlycoverage alongside the original exhaustion/system-table test.The docstring coverage threshold remains unchanged as requested.
Validation passed: --agc-system-table-only and --agc-unaligned-pool-only, the formatting hook, and git diff --check.
@coderabbitai[bot] on GitHub · 2026-10-11 07:53 UTC · original
On
src/libs/agc.cppline 4199 (outdated):@kewalkerThanks for the fixes and the clarification.The supplied
Gen5Driver::Initializenow has one allocation block and writes the descriptor, eight function records, and validator shader after mapping. This addresses both findings.The reserve calculation,
reported_end - start, also hides the alignment padding so the advertised guest budget ends before the driver block. Thanks for adding the unaligned-pool test and reporting that both test selectors passed.✏️ Learnings added
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.
@kewalker on GitHub · 2026-10-11 14:06 UTC · original
@itsmemac CodeRabbit suggested you as a reviewer for this AGC startup fix. Would you be able to review it when you have time? The normal and unaligned-pool AGC regression tests passed locally, and CodeRabbit's actionable findings have been addressed. Thank you.
@theantipopau on GitHub · 2026-10-12 11:14 UTC · original
Verified the mechanism against main:
g_system_direct_memory_reserveis set by the AGC driver's reserve path and subtracted inside the reportedPhysicalMemory::Size(), so guest flexible-memory sizing (DEFAULT_FLEXIBLE_MEMORY_SIZEaccounting inMapFlexibleMemory/antisize checks) no longer counts the driver's own backing that was allocated after the size was published. TheInitialize()reset-once at line ~1013 plus atomic store/load makes ordering safe: the reserve store happens afterPhysicalMemoryexists and before grow callbacks that read it. The two test exits ("driver memory was not reserved before guest allocation", "guest memory size still includes the reserved driver block") lock the failure mode in, good.Holding off a formal approve purely because CI is behind the
action_requiredmaintainer gate on this fork (unverified-green locally visible only to maintainers); no code objection. If the Demon's Souls title area changes again after this, the interleaving betweenReplaceFixedRangeWithReservedand this reserve is worth one more look before merge — the reserve lands in system direct memory whose rangesReplaceSpancan touch withVirtualRangeType::Reservedpending before replacement.@ -2636,3 +2642,3 @@PRINT_NAME();return PhysicalMemory::Size();return PhysicalMemory::Size() - g_system_direct_memory_reserve.load(std::memory_order_relaxed);@coderabbitai[bot] on GitHub · 2026-10-14 00:53 UTC · original · edited 2026-10-14 00:58 UTC
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Apply the guest budget to main direct-memory allocations.
If
PhysicalMemory::Size()has an unaligned tail, AGC excludes that tail fromKernelGetDirectMemorySize()but does not allocate it.KernelAllocateMainDirectMemorystill searches throughPhysicalMemory::Size(). A guest can therefore allocate memory beyond its reported budget. Use the reported guest limit for main allocations, while keeping AGC’s driver allocation able to use the physical limit.✅ Addressed in commit
f3fd301@coderabbitai[bot] on GitHub · 2026-10-14 00:54 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
612b512e-adc9-422d-a655-fc8f9a030316📥 Commits
Reviewing files that changed from the base of the PR and between
b8939cc5baand5d81c42ff0.📒 Files selected for processing: 3
src/kernel/memory.cppsrc/kernel/memory.htests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
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.