Mesa (master): drm-shim: don't create a memfd per BO

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Thu Apr 30 08:54:40 UTC 2020


Module: Mesa
Branch: master
Commit: 764ef4bf1a6fe0c256859d275c5f922d46217dbc
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=764ef4bf1a6fe0c256859d275c5f922d46217dbc

Author: Lionel Landwerlin <lionel.g.landwerlin at intel.com>
Date:   Sat Apr 25 12:47:04 2020 +0300

drm-shim: don't create a memfd per BO

Running shader-db on big servers with many cores, we're running out of
file descriptors.

Use a single 4Gb memfd instead and allocate from it using a VMA.

v2: Align VMA allocation to 4096 (Eric)

Signed-off-by: Lionel Landwerlin <lionel.g.landwerlin at intel.com>
Acked-by: Matt Turner <mattst88 at gmail.com>
Reviewed-by: Eric Anholt <eric at anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4594>

---

 src/drm-shim/device.c   | 34 ++++++++++++++++++++++------------
 src/drm-shim/drm_shim.h |  9 ++++++++-
 2 files changed, 30 insertions(+), 13 deletions(-)

diff --git a/src/drm-shim/device.c b/src/drm-shim/device.c
index 39a7c2107ef..2ad25344087 100644
--- a/src/drm-shim/device.c
+++ b/src/drm-shim/device.c
@@ -42,6 +42,7 @@
 #include "util/hash_table.h"
 #include "util/u_atomic.h"
 
+#define SHIM_MEM_SIZE (4ull * 1024 * 1024 * 1024)
 
 #ifndef HAVE_MEMFD_CREATE
 #include <sys/syscall.h>
@@ -79,6 +80,16 @@ drm_shim_device_init(void)
                                                 uint_key_hash,
                                                 uint_key_compare);
 
+   mtx_init(&shim_device.mem_lock, mtx_plain);
+
+   shim_device.mem_fd = memfd_create("shim mem", MFD_CLOEXEC);
+   assert(shim_device.mem_fd != -1);
+
+   int ret = ftruncate(shim_device.mem_fd, SHIM_MEM_SIZE);
+   assert(ret == 0);
+
+   util_vma_heap_init(&shim_device.mem_heap, 4096, SHIM_MEM_SIZE - 4096);
+
    drm_shim_driver_init();
 }
 
@@ -256,17 +267,13 @@ drm_shim_ioctl(int fd, unsigned long request, void *arg)
 void
 drm_shim_bo_init(struct shim_bo *bo, size_t size)
 {
-   bo->size = size;
-   bo->fd = memfd_create("shim bo", MFD_CLOEXEC);
-   if (bo->fd == -1) {
-      fprintf(stderr, "Failed to create BO: %s\n", strerror(errno));
-      abort();
-   }
 
-   if (ftruncate(bo->fd, size) == -1) {
-      fprintf(stderr, "Failed to size BO: %s\n", strerror(errno));
-      abort();
-   }
+   mtx_lock(&shim_device.mem_lock);
+   bo->mem_addr = util_vma_heap_alloc(&shim_device.mem_heap, size, 4096);
+   mtx_unlock(&shim_device.mem_lock);
+   assert(bo->mem_addr);
+
+   bo->size = size;
 }
 
 struct shim_bo *
@@ -301,7 +308,10 @@ drm_shim_bo_put(struct shim_bo *bo)
 
    if (shim_device.driver_bo_free)
       shim_device.driver_bo_free(bo);
-   close(bo->fd);
+
+   mtx_lock(&shim_device.mem_lock);
+   util_vma_heap_free(&shim_device.mem_heap, bo->mem_addr, bo->size);
+   mtx_unlock(&shim_device.mem_lock);
    free(bo);
 }
 
@@ -350,5 +360,5 @@ drm_shim_mmap(struct shim_fd *shim_fd, size_t length, int prot, int flags,
 {
    struct shim_bo *bo = (void *)(uintptr_t)offset;
 
-   return mmap(NULL, length, prot, flags, bo->fd, 0);
+   return mmap(NULL, length, prot, flags, shim_device.mem_fd, bo->mem_addr);
 }
diff --git a/src/drm-shim/drm_shim.h b/src/drm-shim/drm_shim.h
index 4d359dd7f1a..cb3951afa82 100644
--- a/src/drm-shim/drm_shim.h
+++ b/src/drm-shim/drm_shim.h
@@ -25,6 +25,7 @@
 
 #include "util/macros.h"
 #include "util/hash_table.h"
+#include "util/vma.h"
 
 #include <xf86drm.h>
 
@@ -40,6 +41,12 @@ struct shim_device {
    /* Mapping from int fd to struct shim_fd *. */
    struct hash_table *fd_map;
 
+   mtx_t mem_lock;
+   /* Heap from which shim_bo are allocated */
+   struct util_vma_heap mem_heap;
+
+   int mem_fd;
+
    int (**driver_ioctls)(int fd, unsigned long request, void *arg);
    int driver_ioctl_count;
 
@@ -61,7 +68,7 @@ struct shim_fd {
 };
 
 struct shim_bo {
-   int fd;
+   uint64_t mem_addr;
    void *map;
    int refcount;
    uint32_t size;



More information about the mesa-commit mailing list