[Mesa-dev] [PATCH] vc4: Mark BOs as purgeable when they enter the BO cache

Boris Brezillon boris.brezillon at free-electrons.com
Wed Sep 27 13:45:17 UTC 2017


This patch makes use of the DRM_IOCTL_VC4_GEM_MADVISE ioctl to mark all
BOs placed in the mesa BO cache as purgeable so that the system can
reclaim this memory under memory pressure.

Signed-off-by: Boris Brezillon <boris.brezillon at free-electrons.com>
---
Hello,

Note that this series depends on kernel code that has not been accepted
yet and is just provided to show reviewers how the ioctl can be used
and what to expect from it.

Please do not consider this for inclusion in MESA until the kernel part
has been accepted.

I also lack a check to make sure the DRM_IOCTL_VC4_GEM_MADVISE ioctl is
available before using it.

Thanks,

Boris
---
 src/gallium/drivers/vc4/vc4_bufmgr.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/src/gallium/drivers/vc4/vc4_bufmgr.c b/src/gallium/drivers/vc4/vc4_bufmgr.c
index 0653f8823232..8ee37ac7010d 100644
--- a/src/gallium/drivers/vc4/vc4_bufmgr.c
+++ b/src/gallium/drivers/vc4/vc4_bufmgr.c
@@ -87,6 +87,16 @@ vc4_bo_remove_from_cache(struct vc4_bo_cache *cache, struct vc4_bo *bo)
         cache->bo_size -= bo->size;
 }
 
+static int vc4_bo_purgeable(struct vc4_bo *bo, bool purgeable)
+{
+        struct drm_vc4_gem_madvise arg = {
+                .handle = bo->handle,
+                .madv = purgeable ? VC4_MADV_DONTNEED : VC4_MADV_WILLNEED,
+        };
+
+        return vc4_ioctl(bo->screen->fd, DRM_IOCTL_VC4_GEM_MADVISE, &arg);
+}
+
 static struct vc4_bo *
 vc4_bo_from_cache(struct vc4_screen *screen, uint32_t size, const char *name)
 {
@@ -111,6 +121,11 @@ vc4_bo_from_cache(struct vc4_screen *screen, uint32_t size, const char *name)
                         return NULL;
                 }
 
+                if (vc4_bo_purgeable(bo, false)) {
+                        mtx_unlock(&cache->lock);
+                        return NULL;
+                }
+
                 pipe_reference_init(&bo->reference, 1);
                 vc4_bo_remove_from_cache(cache, bo);
 
@@ -296,6 +311,7 @@ vc4_bo_last_unreference_locked_timed(struct vc4_bo *bo, time_t time)
                 cache->size_list_size = page_index + 1;
         }
 
+        vc4_bo_purgeable(bo, true);
         bo->free_time = time;
         list_addtail(&bo->size_list, &cache->size_list[page_index]);
         list_addtail(&bo->time_list, &cache->time_list);
-- 
2.11.0



More information about the mesa-dev mailing list