perf(agc): share one dma-buf import per 64 MiB of Linux direct memory #1152

Merged
alfalfa-star merged 1 commit from upstream-pr/1152 into main 2026-10-10 06:14:41 +00:00
alfalfa-star commented 2026-10-07 14:57:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-07T14:57:22Z.


What

With #943, every Linux direct-memory allocation the GPU uses becomes its own udmabuf and its own imported buffer object, because each allocation had a memfd of its own. Imported buffer objects can't be "always valid" in the GPU VM the way ordinary allocations are, so amdgpu validates every one of them at every command submission. PPSA14632 imports about 7000 allocations (about 5800 of them 64 KiB), and each vkQueueSubmit took about 2.3 ms, around 5 s per 10 s, all held under the GPU mutex.

A standalone test on RADV (RX 9070 XT, kernel 7.2.9) measured submits after the first ones:

imported per submit
1000 ordinary allocations 3.5 us
64 dma-buf imports of 64 MiB 14 us
~7000 dma-buf imports (PPSA14632) ~2300 us

So the cost follows the number of imported buffer objects. Their size and the number of ordinary allocations don't matter.

  • libkernel: on Linux all direct memory lives in one sparse memfd of DIRECT_MEMORY_SIZE, at file offset = physical address. PhysicalBacking keeps the file and its offset, and SharedBacking returns that offset. A range allocated again is zeroed through a temporary mapping, as the old per-allocation memfd was zero-filled.
  • Driver: an import of shared direct memory that lies inside one 64 MiB chunk of that file creates its own VkBuffer and binds it into the chunk's memory, so there is one udmabuf import per chunk. 64 MiB fits udmabuf's default size_limit_mb.
    • HostImport::chunk holds the chunk, so it lives until the last buffer bound to it retires.
    • Consumers still see one buffer and one device address per import.
    • Ranges that cross a chunk, or whose offset doesn't meet the buffer alignment, are imported alone as before.
  • TechnicalDebt: released direct memory is not punched out of the file. A live chunk import pins the pages, and the GPU would keep reading the old ones. The process therefore keeps the high-water mark of the direct memory it used.

Tested

Linux 7.2.9 (Arch), Mesa 26.2.4 RADV, RX 9070 XT.

  • PPSA14632 (LEGO Horizon Adventures), 180 s:
    • vkQueueSubmit drops from ~2400 us to 46 us on average;
    • the GPU idles 36 ms instead of ~172 ms per present;
    • presents go from ~40 to 80-97 per 10 s;
    • the title reaches content past the boot movies that it had not reached before (new shaders and draws, which are separate issues).
  • guest_memory: CheckDirectMemorySharedBacking now checks that two allocations share one file at offsets equal to their physical addresses, and that a released and reallocated range reads back zeroed. The existing offset checks are relative to the allocation's physical address. I checked that the new checks fail when broken.
  • ctest: 359 of 359 pass on this branch; 463 of 463 after the rebase onto current main.

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 in TechnicalDebt
  • 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 (#943 is merged)
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4774478380 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1152). Originally posted by [alfalfa-star](https://github.com/alfalfa-star) on 2026-10-07T14:57:22Z. --- ### What With #943, every Linux direct-memory allocation the GPU uses becomes its own udmabuf and its own imported buffer object, because each allocation had a memfd of its own. Imported buffer objects can't be "always valid" in the GPU VM the way ordinary allocations are, so amdgpu validates every one of them at every command submission. PPSA14632 imports about 7000 allocations (about 5800 of them 64 KiB), and each `vkQueueSubmit` took about 2.3 ms, around 5 s per 10 s, all held under the GPU mutex. A standalone test on RADV (RX 9070 XT, kernel 7.2.9) measured submits after the first ones: | imported | per submit | |---|---| | 1000 ordinary allocations | 3.5 us | | 64 dma-buf imports of 64 MiB | 14 us | | ~7000 dma-buf imports (PPSA14632) | ~2300 us | So the cost follows the number of imported buffer objects. Their size and the number of ordinary allocations don't matter. - **libkernel:** on Linux all direct memory lives in one sparse memfd of `DIRECT_MEMORY_SIZE`, at file offset = physical address. `PhysicalBacking` keeps the file and its offset, and `SharedBacking` returns that offset. A range allocated again is zeroed through a temporary mapping, as the old per-allocation memfd was zero-filled. - **Driver:** an import of shared direct memory that lies inside one 64 MiB chunk of that file creates its own `VkBuffer` and binds it into the chunk's memory, so there is one udmabuf import per chunk. 64 MiB fits udmabuf's default `size_limit_mb`. - `HostImport::chunk` holds the chunk, so it lives until the last buffer bound to it retires. - Consumers still see one buffer and one device address per import. - Ranges that cross a chunk, or whose offset doesn't meet the buffer alignment, are imported alone as before. - **TechnicalDebt:** released direct memory is not punched out of the file. A live chunk import pins the pages, and the GPU would keep reading the old ones. The process therefore keeps the high-water mark of the direct memory it used. ### Tested Linux 7.2.9 (Arch), Mesa 26.2.4 RADV, RX 9070 XT. - PPSA14632 (LEGO Horizon Adventures), 180 s: - `vkQueueSubmit` drops from ~2400 us to 46 us on average; - the GPU idles 36 ms instead of ~172 ms per present; - presents go from ~40 to 80-97 per 10 s; - the title reaches content past the boot movies that it had not reached before (new shaders and draws, which are separate issues). - `guest_memory`: `CheckDirectMemorySharedBacking` now checks that two allocations share one file at offsets equal to their physical addresses, and that a released and reallocated range reads back zeroed. The existing offset checks are relative to the allocation's physical address. I checked that the new checks fail when broken. - `ctest`: 359 of 359 pass on this branch; 463 of 463 after the rebase onto current `main`. ### Checklist - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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 (#943 is merged) - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
oneandonlydean commented 2026-10-08 03:25:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-08T03:25:47Z.


#943 is on main now, so this conflicts with main in GuestBufferMemory.hpp, GuestBufferMemory.cpp, DirectMemory.cpp, tests/GuestMemory.cpp and TechnicalDebt.md. Please rebase onto main so only your commit is left.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6051539823 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1152#issuecomment-6051539823). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-08T03:25:47Z. --- #943 is on main now, so this conflicts with main in `GuestBufferMemory.hpp`, `GuestBufferMemory.cpp`, `DirectMemory.cpp`, `tests/GuestMemory.cpp` and `TechnicalDebt.md`. Please rebase onto main so only your commit is left.
alfalfa-star commented 2026-10-08 16:53:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-08T16:53:59Z.


Rebased onto current main (be2b6d3e). Only this PR's commit is left. The code diff is unchanged; the only conflict was the TechnicalDebt line. ctest: 463/463.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6064833994 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1152#issuecomment-6064833994). Originally posted by [alfalfa-star](https://github.com/alfalfa-star) on 2026-10-08T16:53:59Z. --- Rebased onto current `main` (be2b6d3e). Only this PR's commit is left. The code diff is unchanged; the only conflict was the TechnicalDebt line. `ctest`: 463/463.
popcoder565 commented 2026-10-09 19:43:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T19:43:29Z.


Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head be2b6d3e, merged onto main 8b393b6e in one build with 31 other PRs that had no Windows run.

  • The change is Linux-only. guest_memory passes on Windows, so this only checks that the Windows build and test are unaffected.
  • Full ctest in that build: 520 of 521. The one failure is guest_offline_imports, broken by #2359 (reported there).

No title was run.

🤖 Addressed by Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6088034158 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1152#issuecomment-6088034158). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-09T19:43:29Z. --- Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head `be2b6d3e`, merged onto `main` 8b393b6e in one build with 31 other PRs that had no Windows run. - The change is Linux-only. `guest_memory` passes on Windows, so this only checks that the Windows build and test are unaffected. - Full ctest in that build: 520 of 521. The one failure is `guest_offline_imports`, broken by #2359 (reported there). No title was run. _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
adriadam10 commented 2026-10-09 22:32:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T22:32:13Z.


Rebased onto current main (only conflict: the TechnicalDebt line next to its neighbours) and rebuilt. Full ctest passes (548/548), and guest_memory fails when the non-test changes are reverted, so the new checks do exercise the change. The chunk import logic (key by dev/ino/chunk index, buffer bound at an aligned offset, fallback to the individual import for ranges that cross a chunk) and the zeroing of reallocated direct memory look right to me. Merging once CI is green on the rebased head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090354161 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1152#issuecomment-6090354161). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-09T22:32:13Z. --- Rebased onto current `main` (only conflict: the TechnicalDebt line next to its neighbours) and rebuilt. Full ctest passes (548/548), and `guest_memory` fails when the non-test changes are reverted, so the new checks do exercise the change. The chunk import logic (key by dev/ino/chunk index, buffer bound at an aligned offset, fallback to the individual import for ranges that cross a chunk) and the zeroing of reallocated direct memory look right to me. Merging once CI is green on the rebased head.
adriadam10 commented 2026-10-09 23:03:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T23:03:22Z.


Rebased again onto current main: main moved after the previous rebase and the PR conflicted again. Only conflict was the TechnicalDebt line next to the libSceZlib one; both kept. Rebuilt and ran the scoped ctest (libs, AgcDriver, libkernel): 332/332 pass. Merging once CI is green on the rebased head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090745232 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1152#issuecomment-6090745232). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-09T23:03:22Z. --- Rebased again onto current `main`: `main` moved after the previous rebase and the PR conflicted again. Only conflict was the TechnicalDebt line next to the libSceZlib one; both kept. Rebuilt and ran the scoped ctest (libs, AgcDriver, libkernel): 332/332 pass. Merging once CI is green on the rebased head.
adriadam10 commented 2026-10-10 06:04:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:04:35Z.


Rebased onto current main again. This time the conflict was in DirectMemory.cpp as well as the TechnicalDebt line: main added PhysicalBacking::HostWriteData, which mapped the backing's own memfd at offset 0 and closed it in the destructor.

  • HostWriteData now maps its view at fileOffset of the shared file; the destructor only unmaps the view (the file is shared and not closed per backing).
  • TechnicalDebt: both lines kept.

Scoped ctest (libs, AgcDriver, libkernel) passes, including guest_memory. Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094477160 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1152#issuecomment-6094477160). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T06:04:35Z. --- Rebased onto current `main` again. This time the conflict was in `DirectMemory.cpp` as well as the TechnicalDebt line: `main` added `PhysicalBacking::HostWriteData`, which mapped the backing's own memfd at offset 0 and closed it in the destructor. - `HostWriteData` now maps its view at `fileOffset` of the shared file; the destructor only unmaps the view (the file is shared and not closed per backing). - TechnicalDebt: both lines kept. Scoped ctest (libs, AgcDriver, libkernel) passes, including `guest_memory`. Merging once CI is green.
github-actions[bot] commented 2026-10-10 06:07:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T06:07:07Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#1873 docs/dev/TechnicalDebt.md L264
#1924 docs/dev/TechnicalDebt.md L264
#2072 docs/dev/TechnicalDebt.md L264
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094495932 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1152#issuecomment-6094495932). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T06:07:07Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #1873 | `docs/dev/TechnicalDebt.md` [L264](https://github.com/boykopovar/AnyPS5/blob/9bb5619c838709d3d2087f1982365bcb201d60f3/docs/dev/TechnicalDebt.md#L264) | | | | #1924 | `docs/dev/TechnicalDebt.md` [L264](https://github.com/boykopovar/AnyPS5/blob/9bb5619c838709d3d2087f1982365bcb201d60f3/docs/dev/TechnicalDebt.md#L264) | | | | #2072 | `docs/dev/TechnicalDebt.md` [L264](https://github.com/boykopovar/AnyPS5/blob/9bb5619c838709d3d2087f1982365bcb201d60f3/docs/dev/TechnicalDebt.md#L264) | | |
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!1152
No description provided.