chore(mesa): move the zink idle reclaim patch to its own pull request

This commit is contained in:
Kurt Himebauch committed 2026-11-27 01:02:52 -05:00
1 parent c9f73b32ae
commit f8d76688fc
1 file changed
-143
@@ -1,143 +0,0 @@
Subject: zink: reclaim freed slab entries and cached buffers while the client is idle
zink only reclaims slab entries and expired cached buffers during its next
allocation, and pb_slabs_reclaim stops at the first entries still in use. A
client that frees a lot and then goes idle keeps all of it allocated: Steam's web helper held 500-700 MiB of KGSL memory behind a
fullscreen game after scrolling its library, and released it the moment the
overlay drew a frame. A thread now reclaims once a second after a second
without allocations.
--- a/src/util/pb_slab.h
+++ b/src/util/pb_slab.h
@@ -145,6 +145,9 @@
unsigned
pb_slabs_reclaim(struct pb_slabs *slabs);
+unsigned
+pb_slabs_reclaim_all(struct pb_slabs *slabs);
+
bool
pb_slabs_init(struct pb_slabs *slabs,
unsigned min_order, unsigned max_order,
--- a/src/util/pb_slab.c
+++ b/src/util/pb_slab.c
@@ -231,6 +231,19 @@
return num_reclaims;
}
+/* Like pb_slabs_reclaim, but skips entries that are still in use instead of
+ * stopping at the first ones, so entries freed behind them are reclaimed too.
+ */
+unsigned
+pb_slabs_reclaim_all(struct pb_slabs *slabs)
+{
+ unsigned num_reclaims;
+ simple_mtx_lock(&slabs->mutex);
+ num_reclaims = pb_slabs_reclaim_all_locked(slabs);
+ simple_mtx_unlock(&slabs->mutex);
+ return num_reclaims;
+}
+
/* Initialize the slabs manager.
*
* The minimum and maximum size of slab entries are 2^min_order and
--- a/src/gallium/drivers/zink/zink_types.h
+++ b/src/gallium/drivers/zink/zink_types.h
@@ -1427,6 +1427,12 @@
struct pb_slabs bo_slabs[NUM_SLAB_ALLOCATORS];
unsigned min_alloc_size;
uint32_t next_bo_unique_id;
+ uint32_t alloc_serial;
+ thrd_t reclaim_thread;
+ mtx_t reclaim_lock;
+ cnd_t reclaim_cond;
+ bool reclaim_stop;
+ bool reclaim_running;
} pb;
uint8_t heap_map[ZINK_HEAP_MAX][VK_MAX_MEMORY_TYPES]; // mapping from zink heaps to memory type indices
uint8_t heap_count[ZINK_HEAP_MAX]; // number of memory types per zink heap
--- a/src/gallium/drivers/zink/zink_bo.c
+++ b/src/gallium/drivers/zink/zink_bo.c
@@ -34,6 +34,7 @@
#include "zink_resource.h"
#include "zink_screen.h"
#include "util/u_hash_table.h"
+#include "util/u_thread.h"
#ifdef HAVE_LIBDRM
#define ZINK_USE_DMABUF
@@ -577,6 +578,7 @@
zink_bo_create(struct zink_screen *screen, uint64_t size, unsigned alignment, enum zink_heap heap, enum zink_alloc_flag flags, unsigned mem_type_idx, const void *pNext)
{
struct zink_bo *bo;
+ p_atomic_inc(&screen->pb.alloc_serial);
/* pull in sparse flag */
flags |= zink_alloc_flags_from_heap(heap);
@@ -1390,6 +1392,40 @@
return bo_slab_alloc(priv, mem_type_idx, entry_size, group_index, false);
}
+/* Slab entries and cached buffers a client frees are otherwise only reclaimed by its next
+ * allocation, so a client that goes idle (a browser UI behind a fullscreen game) keeps all of
+ * that memory allocated. On UMA devices it is system RAM that cannot be swapped. */
+static int
+bo_reclaim_thread(void *data)
+{
+ struct zink_screen *screen = data;
+ uint32_t seen = p_atomic_read(&screen->pb.alloc_serial);
+
+ u_thread_setname("zink_reclaim");
+ mtx_lock(&screen->pb.reclaim_lock);
+ while (!screen->pb.reclaim_stop) {
+ struct timespec deadline;
+ timespec_get(&deadline, TIME_UTC);
+ deadline.tv_sec += 1;
+ cnd_timedwait(&screen->pb.reclaim_cond, &screen->pb.reclaim_lock, &deadline);
+ if (screen->pb.reclaim_stop)
+ break;
+ mtx_unlock(&screen->pb.reclaim_lock);
+
+ uint32_t serial = p_atomic_read(&screen->pb.alloc_serial);
+ if (serial == seen) {
+ for (unsigned i = 0; i < NUM_SLAB_ALLOCATORS; i++)
+ pb_slabs_reclaim_all(&screen->pb.bo_slabs[i]);
+ pb_cache_release_all_buffers(&screen->pb.bo_cache);
+ }
+ seen = serial;
+
+ mtx_lock(&screen->pb.reclaim_lock);
+ }
+ mtx_unlock(&screen->pb.reclaim_lock);
+ return 0;
+}
+
bool
zink_bo_init(struct zink_screen *screen)
{
@@ -1425,12 +1461,25 @@
min_slab_order = max_order + 1;
}
screen->pb.min_alloc_size = 1 << screen->pb.bo_slabs[0].min_order;
+
+ mtx_init(&screen->pb.reclaim_lock, mtx_plain);
+ cnd_init(&screen->pb.reclaim_cond);
+ screen->pb.reclaim_running =
+ u_thread_create(&screen->pb.reclaim_thread, bo_reclaim_thread, screen) == thrd_success;
return true;
}
void
zink_bo_deinit(struct zink_screen *screen)
{
+ if (screen->pb.reclaim_running) {
+ mtx_lock(&screen->pb.reclaim_lock);
+ screen->pb.reclaim_stop = true;
+ cnd_signal(&screen->pb.reclaim_cond);
+ mtx_unlock(&screen->pb.reclaim_lock);
+ thrd_join(screen->pb.reclaim_thread, NULL);
+ screen->pb.reclaim_running = false;
+ }
for (unsigned i = 0; i < NUM_SLAB_ALLOCATORS; i++) {
if (screen->pb.bo_slabs[i].groups)
pb_slabs_deinit(&screen->pb.bo_slabs[i]);