Skip to content

borealis: address sparse images of 4 GiB or more in 64 bits - #1528

Merged
DjDeveloperr merged 1 commit into
aurora-silicon:aurora-wipfrom
joske:fix/borealis-sparse-image-4gib
Dec 2, 2026
Merged

DjDeveloperr merged 1 commit into
aurora-silicon:aurora-wipfrom
joske:fix/borealis-sparse-image-4gib

Conversation

@joske

@joske joske commented Dec 2, 2026 •

Copy link
Copy Markdown

What: Sparse images of 4 GiB or more were addressed with 32-bit sizes and offsets in three places: their address range, the offsets of their layers, and the byte-to-page conversion that records residency. Exactly 4 GiB fails to create. A larger image reserves only its size modulo 4 GiB, so its binds past that map over other objects, and tiles past 4 GiB read as not resident. This makes all three 64-bit and adds hk-vulkan-sparse-image-4gib. Mario Kart World creates a 5803 MiB sparse image; with this it runs on the M4 Max, in both Mario Kart World Recomp and Lichii.
Tested: J614c (M4 Max) and J493 (M2) at 4464bb5cbd5: hk-vulkan-sparse-image-4gib passes on both (fails on 7fe256e0360 on the M4 and on the repo's 36623c0a4a5 on the M2); MKW Recomp 0.1.21 finishes a race on both, and Lichii on the M4, with no GPU fault; before, Recomp lost the device during start-up on the M4, and on the M2 it ran into GPU faults.
Risk: two-way door · all Macs (shared layout helpers and sparse images; G13 to M5 use the same code)
Not yet: M1, M3, M5; a CTS sparse run.

Details
  1. hk_image_plane_alloc_vma() sized a sparse image's VA reservation with align(plane->layout.size_B, HK_SPARSE_ALIGN_B). align() takes and returns uint32_t (util/u_math.h). An image of exactly 4 GiB got a range of 0: VM_BIND refuses it (range=0, EINVAL) and vkCreateImage fails. A 5 GiB image got 1 GiB. Its sparse binds still cover the whole image, so those past the reservation map over whatever the allocator placed there next: other images, buffers, or the driver's own objects. imageMipTailSize had the same align(), and its mip_tail_stride * depth_px product is 32-bit too. Both now use align64().
  2. ail_get_layer_offset_B() and ail_get_layer_level_B() computed z_px * layer_stride_B in 64 bits and returned it as uint32_t. Nearly every caller adds the result to a 64-bit base, so a clear or copy of a layer that starts at 4 GiB or more addressed a layer near the start of the image instead. Both now return uint64_t. The four host-image-copy locals in hk_image.c that held the result in uint32_t are uint64_t now too.
  3. ail_bytes_to_pages() took unsigned. hk_sparse_image_map_commit() passes the tile's 64-bit resource offset, so the residency of a tile past 4 GiB was recorded at a tile near the start of the image. The bound tile then read as not resident (zeros), and the other tile's entry was overwritten. It now takes and returns uint64_t.

The test needs all three fixes. With only (1) and (2), or only (1) and (3), the tile past 4 GiB still reads back wrong (table below).

Mario Kart World's array image is 8192x8192 with 68 layers (format 143) and needs 5803 MiB, of which 1707 MiB were reserved. The binds into the rest overwrote other objects, which is what the earlier reports of this game on M4 showed: shader reads through a null base (UL1C 0x10100), dVDM reads at 0x3f000001c0, render and compute hangs, and purple or stale blocks on screen. The crash point moved with whatever was allocated past the reservation. Turning off compression (ASAHI_MESA_DEBUG=nocompress) or soft faults only moved it.

Evidence

Hardware: J614c (MacBook Pro 14", M4 Max, G16C B1), kernel 7.3.0-1-43.6-ARCH: aurora linux main b98bf00b32 + aurora-silicon/linux#2170 + aurora-silicon/linux#3200 + the VM_BIND kvmalloc fix (aurora-silicon/linux#3920). That fix matters only for single batches of more than about 210,000 bind operations: bigbind at 100,000 blocks and more needs it, hk-vulkan-sparse-image-4gib does not.

Builds: baseline w1202 = 7fe256e0360; the fix applied to 689b2fe3dbf = 4464bb5cbd5, driver code identical (built as package w1202i). Intermediate builds show which fix the test needs:

Build VA align64 layer offsets 64-bit pages 64-bit hk-vulkan-sparse-image-4gib
baseline - - - FAIL: vkCreateImage -13 (VM_BIND range=0)
(1) only yes - - FAIL: 16384 of 16384 texels wrong
(1)+(2) yes yes - FAIL: 16384 of 16384 texels wrong
(1)+(3) yes - yes FAIL: 16384 of 16384 texels wrong
this PR yes yes yes ok

The same test on RADV (RX 6800, Mesa 26.3): ok. With a wrong expected value (negative check): FAIL, as it should.

J493 (MacBook Pro 13", M2, G14G B0), kernel linux-aurora 7.3.0.aurora1-46 from the repo: with the installed Borealis 36623c0a4a5, hk-vulkan-sparse-image-4gib fails (vkCreateImage -13); with this PR's driver (loaded through VK_DRIVER_FILES) it passes. bigbind at 20,000, 48,000, 100,000 and 200,000 blocks completes with this PR's driver, with no kernel message. With this PR's driver installed, MKW Recomp 0.1.21 gets through the intro and attract and finishes a full race with no GPU fault.

Tile past 4 GiB, by image size (layers of 16384x16384 RGBA8 = 1 GiB each), this PR: tiles in layers 1, 3, 4, 5 and 7 of 2- to 8-layer images all read back correctly. On the baseline with only (1), layers 1 and 3 are correct and layers 4, 5 and 7 are wrong.

bigbind (one vkQueueBindSparse aliasing N 64 KiB blocks of a 16384x16384 RGBA8 array to one 64 KiB allocation; source in aurora-silicon/linux#3920):

N blocks (image size) baseline this PR
1,000 (1 GiB), 48,000 (3 GiB) ok ok
1,000 in a 4 GiB image vkCreateImage -13 ok
100,000 (7 GiB) bind ok, then the SSTT flush command hangs (watchdog), vkQueueWaitIdle -4 ok
200,000 (13 GiB) same ok

Mario Kart World, all with mesh_compute and per-draw breadcrumbs:

baseline (w1202) this PR
MKW Recomp 0.1.21 every run lost the device during start-up, before the title screen: 5 of 5 without workarounds between frames 117 and 125, and at most frame 183 with any of the diagnosis switches tried attract, menus, a full race, no GPU fault or recovery, no purple blocks
Lichii (nx2 with #2669) races possible, but purple and stale blocks on screen, and GPU recoveries in some runs (aurora-silicon/linux#2259) attract, menus, a full race at about 30 FPS, no GPU fault, no purple blocks

Still seen with this PR, not caused by it: occasional flashing in Lichii, and a black instead of blurred background in its character and kart selection.

Commands:

cc -O2 -o hk-vulkan-sparse-image-4gib src/asahi/vulkan/tests/hk_vulkan_sparse_image_4gib.c -lvulkan
./hk-vulkan-sparse-image-4gib
./bigbind 100000      # and 200000; BIG_LAYERS=4 ./bigbind 1000 for the 4 GiB case

Three 32-bit computations break sparse images of 4 GiB or more:

- hk_image_plane_alloc_vma() sizes the image's address range with
  align(), which takes and returns uint32_t. An image of exactly 4 GiB
  gets a range of 0, which VM_BIND refuses (vkCreateImage fails); a
  larger one gets its size modulo 4 GiB, and its sparse binds past that
  map over whatever was allocated next: other images, buffers or the
  driver's own objects. The same applies to imageMipTailSize.
- ail_get_layer_offset_B() and ail_get_layer_level_B() return uint32_t,
  so a clear or copy of a layer that starts at 4 GiB or more addresses a
  layer near the start of the image instead.
- ail_bytes_to_pages() takes unsigned, so hk_sparse_image_map_commit()
  records the residency of a tile past 4 GiB at a tile near the start of
  the image: the bound tile reads as not resident, and the other tile's
  entry is overwritten.

Mario Kart World creates an 8192x8192 array image of 68 layers that
needs 5803 MiB: 1707 MiB of it were reserved, the game's binds into the
rest overwrote other objects, and the GPU faulted (reads through a null
base) or hung a few seconds after start-up, at varying places, with
purple and stale blocks on screen until then. With these fixes it runs.

Use align64() for the range and the mip tail size, and 64 bits for the
layer offsets and the byte-to-page conversion. Add
hk-vulkan-sparse-image-4gib, which creates a 4 GiB sparse image and
reads back a tile bound past 4 GiB in a 5 GiB one.

Signed-off-by: Jos Dehaes <jos.dehaes@gmail.com>

@satchlj satchlj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 4464bb5cbd5fc49edd7f83400116a61921de87a3

Blockers: none

Exact-head M5 Max (J775c, G17C B1) hardware verification + full sparse Vulkan CTS sweep at 4464bb5cbd5fc49edd7f83400116a61921de87a3 vs merge-base 689b2fe3dbfbae73beee6243ebcdca5655da7bfd: PASS, 0 regressions, 0 GPU faults.

  • Environment: Apple M5 Max (J775c, G17C B1), stock linux@main 5a870d0f0b3f (7.3.0-1-50.20261202-ARCH, boot aa051bcd-760d-499b-8add-5a5ba194f043), default kernel command line. Both ICDs built natively (--prefix /usr -Dbuildtype=release -Db_ndebug=false) and selected via VK_ICD_FILENAMES (vulkaninfo confirmed git-689b2fe3db vs git-4464bb5cbd).
  • Unit tests (on-device arm64, PR head): meson test --suite asahi --suite vulkan --suite poly — 243/243 OK, 0 Fail.
  • hk-vulkan-sparse-image-4gib (same-boot base vs PR):
    • Base 689b2fe3dbfb: FAIL (rc=1, DRM_IOCTL_ASAHI_VM_BIND failed: Invalid argument ... range=0, vkCreateImage: -13).
    • PR 4464bb5cbd5f: PASS (rc=0, sparse images of 4 GiB and more: ok).
  • Full Vulkan sparse CTS (dEQP-VK.sparse_resources.*, 19,402 cases, cold shader cache MESA_SHADER_CACHE_DISABLE=true, same-boot base vs PR):
    • Base 689b2fe3dbfb: 10,973 Pass, 0 Fail, 8,429 NotSupported (total=19,402).
    • PR 4464bb5cbd5f: 10,973 Pass, 0 Fail, 8,429 NotSupported (total=19,402).
    • Case-by-case QPA diff: 0 status changes (DIFF count: 0).
  • GPU faults (dmesg): 0 before, 0 after.

Checked:

  • src/asahi/layout/layout.h: ail_get_layer_offset_B(), ail_get_layer_level_B(), and ail_bytes_to_pages() widened cleanly to uint64_t (layer_stride_B, level_offsets_B[], and size_B are already uint64_t).
  • src/asahi/vulkan/hk_image.c: hk_image_plane_alloc_vma() and hk_fill_sparse_image_memory_reqs() use align64() and 64-bit mip_tail_stride * depth_px; host-image-copy layer/level offsets widened to uint64_t.
  • Verified downstream callers in hk_queue.c (hk_sparse_image_map_commit() already stores ail_bytes_to_pages() outputs in uint64_t) and hk_sparse.h (hk_sparse_image_tile_offset_B() multiplies z * layout->layer_stride_B in uint64_t).
  • Non-blocking note: ail_get_linear_pixel_B(), ail_get_twiddled_block_B(), and ail_get_tiled_element_B() still return uint32_t — harmless for hk_sparse_image_tile_offset_B(..., z=0) unless a single 2D layer itself exceeds 4 GiB, so fine to leave out of scope here.

@satchlj satchlj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: FINISH, then merge

Blockers:

  • No CI build/test receipt recorded at 4464bb5cbd5f yet (statusCheckRollup is empty while hosted Actions is disabled, and no local/fork meson test build pass is posted on the PR). Ready to flip to APPROVE once a CI or local/fork build+test receipt is posted.

Checked:

  • src/asahi/vulkan/hk_image.c: hk_image_plane_alloc_vma() now uses align64(plane->layout.size_B, HK_SPARSE_ALIGN_B) (preventing 32-bit VA wrap at >= 4 GiB); hk_fill_sparse_image_memory_reqs() widens both imageMipTailSize calculations (align64(layout->size_B, ...) and align64((uint64_t)layout->mip_tail_stride * layout->depth_px, ...)).
  • src/asahi/layout/layout.h: ail_get_layer_offset_B() (z_px * layout->layer_stride_B, where layer_stride_B is uint64_t) and ail_get_layer_level_B() now return uint64_t; ail_bytes_to_pages() now takes and returns uint64_t. All four host-image-copy offset locals in hk_image.c are widened to uint64_t.
  • src/asahi/vulkan/tests/hk_vulkan_sparse_image_4gib.c: clean regression test covering exact 4 GiB sparse image creation and 5 GiB last-layer tile bind, clear, and readback, wired into src/asahi/vulkan/meson.build.
  • Non-blocking note: hk_copy_memory_to_image() and hk_copy_image_to_memory() still hold layout->layer_stride_B in uint32_t dst_layer_stride / src_layer_stride; safe for any layer under 4 GiB, though widening those locals to uint64_t in a follow-up would keep the host-copy path uniformly 64-bit.
  • Exact-head 4464bb5cbd5 hardware PASS rows on J614c (M4 Max) and J493 (M2): hk-vulkan-sparse-image-4gib, bigbind up to 200,000 blocks (13 GiB), and full races in MKW Recomp and Lichii with 0 GPU faults; MERGEABLE, zero conflicts.
Evidence
  • Audited full 4-file diff (+349/-11) at 4464bb5cbd5f and checked struct ail_layout field types (layer_stride_B, size_B uint64_t; mip_tail_stride uint32_t) and host-image-copy loops in hk_image.c:2452-2675.
  • Verified statusCheckRollup is empty at 4464bb5cbd5f and PR body/comments contain exact-head J614c and J493 hardware rows but no full Mesa build/unit-test CI run yet.

@DjDeveloperr

Copy link
Copy Markdown

Local CI on the merge tree (this is the build and test receipt the review asked for; hosted Actions is off). Head 4464bb5cbd5f merged onto aurora-wip 056b5efd7e42 gives merge 03c6b4b91d51, tree 4bc5bff85f22, with no conflicts. It ran in the Fedora 44 CI image on x86_64 (pc):

Step Result
.github/ci/mesa-arm64.sh (asahi + poly suites) Ok 387, Fail 0, Skipped 1
Same script with -march=x86-64-v3 -funsigned-char Ok 387, Fail 0, Skipped 1
hk-vulkan-sparse-image-4gib, both builds builds
meson test --suite util Ok 19, Fail 0
meson test --suite asahi --suite vulkan --suite poly Ok 443, Fail 0, Skipped 1, Timeout 2

The layout tests pass (asahi_layout_tests, agx3_layout, agx_m5_layout, asahi-scs-layout, asahi-i386-layout). The two timeouts, agx_tests and borealis-sync-runner, hit the 30 s limit while the build host's load was above 100. The same binaries passed in the first two steps on this tree, in 24.9 s and 10.0 s. The tip has since moved only by #1438 (src/util), which touches none of this PR's files.

layout.h across Gallium and every generation: the three helpers already computed in 64 bits and then truncated the result to 32 bits on return. Below 4 GiB that truncation never changed a value, so it now returns the same value as before. Each caller adds the result to a 64-bit GPU address or a CPU pointer, stores it in a 64-bit variable, or (in hk_image_view.c) divides it into a page number. None prints it, subtracts it below zero or treats it as signed, and Gallium never calls ail_bytes_to_pages(). The behaviour changes only for images of 4 GiB or more, which this PR fixes. With exact-head rows on G14G (J493), G16C (J614c) and G17C (J775c), no further generation row is needed. Merging at 4464bb5cbd5f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants