[PR #2126] d3d12: decline file-backed/multi-VMA host imports under Wine and deduplicate import memory types (RT-WINE-HOST-IMPORT-POLICY) #2131

Open
opened 2026-11-26 21:20:09 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2126
Author: @djrolfes
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: djrolfes/rt-wine-mapped-host-import


📝 Commits (1)

  • 1431ac4 d3d12: decline file-backed/multi-VMA host imports under Wine and deduplicate import memory types

📊 Changes

5 files changed (+339 additions, -73 deletions)

View changed files

📝 runtime/CMakeLists.txt (+4 -0)
📝 runtime/shims/d3d12_vulkan_device.cpp (+105 -73)
➕ runtime/src/host_import_policy.h (+73 -0)
📝 runtime/test/d3d12_existing_heap_test.cpp (+44 -0)
➕ runtime/test/host_import_policy_test.cpp (+113 -0)

📄 Description

Claimed in the coordination issue as: RT-WINE-HOST-IMPORT-POLICY (#1608)

What and why

Under Wine on Linux, nvidia.ko rejects long-term pinning (pin_user_pages / FOLL_LONGTERM) of file-backed MAP_SHARED views (such as the sparse temp files created under LEONIDA_XMEM_BACKING_DIR) with kernel Cannot map memory with base addr ... errors and also rejects host-pointer imports spanning multiple VMAs. As d3d12_vulkan_device.cpp notes, bursts of those kernel rejections on NVIDIA 620.73 under Wine coincide with spurious VK_ERROR_OUT_OF_HOST_MEMORY (-1) responses on concurrent normal vkAllocateMemory calls, causing E_OUTOFMEMORY (0x8007000e) resource-creation failures even with gigabytes of VRAM and RAM free.

This change fixes four related issues in the host-import path without altering native Windows host-import eligibility or anonymous pagefile section imports (the four runtime-path invariants below — post-hold TOCTOU re-check, memory_mutex() around import vkAllocateMemory, single-pass memory-type deduplication, and stopping early on VK_ERROR_INVALID_EXTERNAL_HANDLE — apply on native Windows as well as Wine):

  1. Adds portable runtime/src/host_import_policy.h (HostImportRangeCheck and host_import_memory_types) and CPU unit test runtime/test/host_import_policy_test.cpp:
    • Under Wine (ntdll!wine_get_version), declines MEM_MAPPED regions backed by a named file (K32GetMappedFileNameW > 0) and ranges spanning multiple AllocationBase values before calling Vulkan, while keeping anonymous VirtualAlloc (MEM_PRIVATE) and anonymous pagefile sections (CreateFileMappingW(INVALID_HANDLE_VALUE, ...)) importable.
    • Deduplicates Vulkan memory types across the preferred (HOST_VISIBLE | HOST_COHERENT) and fallback passes so a failing import never probes the same memory type twice.
  2. Re-verifies host_range_importable after leonida::kernel_writes::hold in create_host_buffer and import_console_heap (closing the TOCTOU window between the writability check and the pin) and stops probing further memory types on VK_ERROR_INVALID_EXTERNAL_HANDLE.
  3. Holds DeviceState::memory_mutex() consistently around vkAllocateMemory in import_console_heap, OpenExistingHeapFromAddress, and the ImageMemory block allocator (matching DeviceState::memory_mutex()'s documented invariant).
  4. Adds a synthetic sparse disk-file-backed section view regression case to runtime/test/d3d12_existing_heap_test.cpp verifying shadow fallback without host import under Wine.

Opened as a draft because prior pull requests are currently open (one-open-PR limit).

Tested

  • python3 -I scripts/check_no_game_data.py on all 5 touched/added files: passed.
  • Portable native CTest (runtime/build/native, Linux / Clang 23.1.5): 100/100 passed (98 passed, 2 conditional skips: shader_library_corpus, vk_sync_validation), including new host_import_policy.
  • Synthetic Windows/Wine tests (clang-cl + xwin, Wine, network-isolated, no retail files):
    • host_import_policy_test: passed
    • d3d12_console_descriptor_heap_test: passed
    • d3d12_xmem_placed_test: passed
    • d3d12_existing_heap_test (including new sparse disk-file-backed section case): passed, 0 kernel Cannot map memory errors.
  • Offline --first-run-ui A/B check (no input, 300 s bounded, read-only package/shaders/BCPack predictor, network-isolated, counts/timings only):
    • Clean main (490f5b9ec): exit 126 at 28.42 s (slot 133 depth texture refused, hr=0x8007000e), 16 kernel Cannot map memory errors across 7 MEM_MAPPED addresses, 2 vkAllocateMemory failed (-1), 347 PresentX ok.
    • This branch (f5bc25cdc + fix): ran full 300.88 s time limit (exit 124), 0 kernel Cannot map memory errors, 0 vkAllocateMemory failed (-1), 0 depth-texture refusals, 7,990 PresentX ok, 200 MEM_PRIVATE slot-133 imports succeeded. Rebased cleanly onto 1fe5072af and re-verified host_import_policy_test + d3d12_existing_heap_test.

GPUs tested: NVIDIA GeForce RTX 3070 Ti (8 GB), driver 620.73.04, Linux (Arch/Omarchy) under Wine.

AI-assisted: yes.

Checklist

  • Based on current main (1fe5072af) and merges cleanly.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: runtime-only change)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2126 **Author:** [@djrolfes](https://github.com/djrolfes) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `djrolfes/rt-wine-mapped-host-import` --- ### 📝 Commits (1) - [`1431ac4`](https://github.com/ubiquitarianism/leonida/commit/1431ac4fb792d60cc0e6ba541f6f9543c05e613a) d3d12: decline file-backed/multi-VMA host imports under Wine and deduplicate import memory types ### 📊 Changes **5 files changed** (+339 additions, -73 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+4 -0) 📝 `runtime/shims/d3d12_vulkan_device.cpp` (+105 -73) ➕ `runtime/src/host_import_policy.h` (+73 -0) 📝 `runtime/test/d3d12_existing_heap_test.cpp` (+44 -0) ➕ `runtime/test/host_import_policy_test.cpp` (+113 -0) </details> ### 📄 Description Claimed in the coordination issue as: RT-WINE-HOST-IMPORT-POLICY (#1608) ### What and why Under Wine on Linux, `nvidia.ko` rejects long-term pinning (`pin_user_pages` / `FOLL_LONGTERM`) of file-backed `MAP_SHARED` views (such as the sparse temp files created under `LEONIDA_XMEM_BACKING_DIR`) with kernel `Cannot map memory with base addr ...` errors and also rejects host-pointer imports spanning multiple VMAs. As `d3d12_vulkan_device.cpp` notes, bursts of those kernel rejections on NVIDIA 620.73 under Wine coincide with spurious `VK_ERROR_OUT_OF_HOST_MEMORY` (`-1`) responses on concurrent normal `vkAllocateMemory` calls, causing `E_OUTOFMEMORY` (`0x8007000e`) resource-creation failures even with gigabytes of VRAM and RAM free. This change fixes four related issues in the host-import path without altering native Windows host-import eligibility or anonymous pagefile section imports (the four runtime-path invariants below — post-`hold` TOCTOU re-check, `memory_mutex()` around import `vkAllocateMemory`, single-pass memory-type deduplication, and stopping early on `VK_ERROR_INVALID_EXTERNAL_HANDLE` — apply on native Windows as well as Wine): 1. Adds portable `runtime/src/host_import_policy.h` (`HostImportRangeCheck` and `host_import_memory_types`) and CPU unit test `runtime/test/host_import_policy_test.cpp`: - Under Wine (`ntdll!wine_get_version`), declines `MEM_MAPPED` regions backed by a named file (`K32GetMappedFileNameW > 0`) and ranges spanning multiple `AllocationBase` values before calling Vulkan, while keeping anonymous `VirtualAlloc` (`MEM_PRIVATE`) and anonymous pagefile sections (`CreateFileMappingW(INVALID_HANDLE_VALUE, ...)`) importable. - Deduplicates Vulkan memory types across the preferred (`HOST_VISIBLE | HOST_COHERENT`) and fallback passes so a failing import never probes the same memory type twice. 2. Re-verifies `host_range_importable` after `leonida::kernel_writes::hold` in `create_host_buffer` and `import_console_heap` (closing the TOCTOU window between the writability check and the pin) and stops probing further memory types on `VK_ERROR_INVALID_EXTERNAL_HANDLE`. 3. Holds `DeviceState::memory_mutex()` consistently around `vkAllocateMemory` in `import_console_heap`, `OpenExistingHeapFromAddress`, and the `ImageMemory` block allocator (matching `DeviceState::memory_mutex()`'s documented invariant). 4. Adds a synthetic sparse disk-file-backed section view regression case to `runtime/test/d3d12_existing_heap_test.cpp` verifying shadow fallback without host import under Wine. Opened as a draft because prior pull requests are currently open (one-open-PR limit). ### Tested - `python3 -I scripts/check_no_game_data.py` on all 5 touched/added files: passed. - Portable native CTest (`runtime/build/native`, Linux / Clang 23.1.5): **100/100 passed** (98 passed, 2 conditional skips: `shader_library_corpus`, `vk_sync_validation`), including new `host_import_policy`. - Synthetic Windows/Wine tests (`clang-cl` + `xwin`, Wine, network-isolated, no retail files): - `host_import_policy_test`: passed - `d3d12_console_descriptor_heap_test`: passed - `d3d12_xmem_placed_test`: passed - `d3d12_existing_heap_test` (including new sparse disk-file-backed section case): passed, **0** kernel `Cannot map memory` errors. - Offline `--first-run-ui` A/B check (no input, 300 s bounded, read-only package/shaders/BCPack predictor, network-isolated, counts/timings only): - Clean `main` (`490f5b9ec`): exit 126 at 28.42 s (`slot 133 depth texture refused`, `hr=0x8007000e`), 16 kernel `Cannot map memory` errors across 7 `MEM_MAPPED` addresses, 2 `vkAllocateMemory failed (-1)`, 347 `PresentX ok`. - This branch (`f5bc25cdc` + fix): ran full 300.88 s time limit (exit 124), **0** kernel `Cannot map memory` errors, **0** `vkAllocateMemory failed (-1)`, **0** depth-texture refusals, **7,990** `PresentX ok`, 200 `MEM_PRIVATE` slot-133 imports succeeded. Rebased cleanly onto `1fe5072af` and re-verified `host_import_policy_test` + `d3d12_existing_heap_test`. GPUs tested: NVIDIA GeForce RTX 3070 Ti (8 GB), driver 620.73.04, Linux (Arch/Omarchy) under Wine. AI-assisted: yes. ### Checklist - [x] Based on current `main` (`1fe5072af`) and merges cleanly. - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: runtime-only change) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#2131
No description provided.