From 1ffacb669899bdf2bd2dc0f8b6bad2cd741aba5b Mon Sep 17 00:00:00 2001 From: LarzVader <118484383+LarzVader@users.noreply.github.com> Date: Tue, 24 Nov 2026 21:22:30 +0100 Subject: [PATCH] fix(mesa): Zink no longer leaks 2 MB of GPU memory every few frames until Android kills the session Add a Mesa patch that clears the batch usage of freed Zink slab entries. Stale usage kept entries out of slab reclaim for good, so every small buffer got a fresh 2 MB slab. Upstream fixed this in Mesa 26.2 (fa867cf18); the patch is a minimal alternative for 26.0.8. --- ...ar-batch-usage-of-freed-slab-entries.patch | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tools/ubuntu/patches/mesa/0006-zink-clear-batch-usage-of-freed-slab-entries.patch diff --git a/tools/ubuntu/patches/mesa/0006-zink-clear-batch-usage-of-freed-slab-entries.patch b/tools/ubuntu/patches/mesa/0006-zink-clear-batch-usage-of-freed-slab-entries.patch new file mode 100644 index 0000000000..043ca5619e --- /dev/null +++ b/tools/ubuntu/patches/mesa/0006-zink-clear-batch-usage-of-freed-slab-entries.patch @@ -0,0 +1,44 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +Subject: [PATCH] zink: clear batch usage of freed slab entries + +bo_destroy_or_cache() drops a real BO's batch usage before the BO goes into +the cache, but bo_slab_destroy() hands slab entries to pb_slab_free() with +their reads/writes usage still set. Zink only frees a BO once no batch holds a +reference any more, so whatever usage is left at that point is stale: it +points at a batch state the BO is no longer tracked by. + +A stale pointer can name a batch state that is never submitted again, for +example the recording batch of a context that was destroyed (its unflushed +flag stays set while it waits in the screen's free list) or of a context that +another thread keeps current without ever flushing. bo_can_reclaim() then +returns false for that entry forever. pb_slabs_reclaim_locked() walks the +reclaim list from the oldest entry and stops after MAX_FAILED_RECLAIMS (2) +failures, so two such entries at the head stop all slab reclaim for the +screen: every later small buffer gets a fresh 2 MB slab and nothing is ever +freed. + +Wine's WineD3D (Gothic II through Proton 11, Zink on Venus) can hit this +as soon as it enters the 3D world: about 0.2 new slabs per frame, +gigabytes of GPU memory after a few minutes, until Android kills the +session. Clear the usage the same way bo_destroy_or_cache() does. + +Upstream fixed the same ballooning in Mesa 26.2 with commit fa867cf18 +("zink/bo: check for usage before completion when reclaiming bos", +MR !41936), which relies on the per-BO submit_count tracking added in +26.1 and does not apply to 26.0. This is a minimal alternative for the +26.0 series; drop it once DroidDeck moves to Mesa >= 26.2. +--- +diff --git a/src/gallium/drivers/zink/zink_bo.c b/src/gallium/drivers/zink/zink_bo.c +--- a/src/gallium/drivers/zink/zink_bo.c ++++ b/src/gallium/drivers/zink/zink_bo.c +@@ -185,6 +185,10 @@ + struct zink_bo *bo = zink_bo(pbuf); + + assert(!bo->mem); ++ /* no batch references the entry any more: any usage left is stale and ++ * would keep it out of reclaim forever (see bo_destroy_or_cache) */ ++ bo->reads.u = NULL; ++ bo->writes.u = NULL; + + //if (bo->base.usage & RADEON_FLAG_ENCRYPTED) + //pb_slab_free(get_slabs(screen, bo->base.size, RADEON_FLAG_ENCRYPTED), &bo->u.slab.entry);