Mesa (main): util: use anonymous file for memory fd creation

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Wed Oct 27 16:06:28 UTC 2021


Module: Mesa
Branch: main
Commit: 4856586ac605e89ee6c128b1a190f000311b49ba
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=4856586ac605e89ee6c128b1a190f000311b49ba

Author: Thomas Wagner <twagner at snap.com>
Date:   Wed Oct 13 09:24:42 2021 +0200

util: use anonymous file for memory fd creation

The original implementation in os_memory_fd.c always uses memfds.
Replace this by using the already existing os_create_anonymous_file in
order to support older systems or systems without memfd.

Fixes: 1166ee9caf3 ("gallium: add utility and interface for memory fd allocations")

Reviewed-by: Marek Olšák <marek.olsak at amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13331>

---

 src/util/anon_file.c    | 18 +++++++++++-------
 src/util/os_memory_fd.c | 16 ++++++++--------
 2 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/src/util/anon_file.c b/src/util/anon_file.c
index f8d9ef204c4..e2d9265194d 100644
--- a/src/util/anon_file.c
+++ b/src/util/anon_file.c
@@ -35,9 +35,9 @@
 #include <errno.h>
 #include <stdlib.h>
 
-#if defined(__FreeBSD__) || defined(__OpenBSD__)
+#if defined(HAVE_MEMFD_CREATE) || defined(__FreeBSD__) || defined(__OpenBSD__)
 #include <sys/mman.h>
-#elif defined(HAVE_MEMFD_CREATE) || defined(ANDROID)
+#elif defined(ANDROID)
 #include <sys/syscall.h>
 #include <linux/memfd.h>
 #else
@@ -115,17 +115,21 @@ int
 os_create_anonymous_file(off_t size, const char *debug_name)
 {
    int fd, ret;
-#ifdef __FreeBSD__
+#if defined(HAVE_MEMFD_CREATE)
+   if (!debug_name)
+      debug_name = "mesa-shared";
+   fd = memfd_create(debug_name, MFD_CLOEXEC | MFD_ALLOW_SEALING);
+#elif defined(ANDROID)
+   if (!debug_name)
+      debug_name = "mesa-shared";
+   fd = syscall(SYS_memfd_create, debug_name, MFD_CLOEXEC | MFD_ALLOW_SEALING);
+#elif defined(__FreeBSD__)
    fd = shm_open(SHM_ANON, O_CREAT | O_RDWR | O_CLOEXEC, 0600);
 #elif defined(__OpenBSD__)
    char template[] = "/tmp/mesa-XXXXXXXXXX";
    fd = shm_mkstemp(template);
    if (fd != -1)
       shm_unlink(template);
-#elif defined(HAVE_MEMFD_CREATE) || defined(ANDROID)
-   if (!debug_name)
-      debug_name = "mesa-shared";
-   fd = syscall(SYS_memfd_create, debug_name, MFD_CLOEXEC);
 #else
    const char *path;
    char *name;
diff --git a/src/util/os_memory_fd.c b/src/util/os_memory_fd.c
index 37b698e788e..b644042ef12 100644
--- a/src/util/os_memory_fd.c
+++ b/src/util/os_memory_fd.c
@@ -36,8 +36,8 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/mman.h>
-#include <sys/syscall.h>
 
+#include "anon_file.h"
 #include "mesa-sha1.h"
 #include "u_math.h"
 #include "os_memory.h"
@@ -106,10 +106,6 @@ os_malloc_aligned_fd(size_t size, size_t alignment, int *fd, char const *fd_name
    size_t alloc_size, offset;
 
    *fd = -1;
-   mem_fd = syscall(__NR_memfd_create, fd_name, MFD_CLOEXEC | MFD_ALLOW_SEALING);
-
-   if(mem_fd < 0)
-      return NULL;
 
    /*
     * Calculate
@@ -121,14 +117,18 @@ os_malloc_aligned_fd(size_t size, size_t alignment, int *fd, char const *fd_name
    const size_t header_size = sizeof(struct memory_header) + sizeof(size_t);
    if (add_overflow_size_t(size, alignment, &alloc_size) ||
        add_overflow_size_t(alloc_size, header_size, &alloc_size))
-      goto fail;
+      return NULL;
 
-   if (ftruncate(mem_fd, alloc_size) != 0)
-      goto fail;
+   mem_fd = os_create_anonymous_file(alloc_size, fd_name);
 
+   if(mem_fd < 0)
+      return NULL;
+
+#if defined(HAVE_MEMFD_CREATE) || defined(ANDROID)
    // Seal fd, so no one can grow or shrink the memory.
    if (fcntl(mem_fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_SEAL) != 0)
       goto fail;
+#endif
 
    ptr = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0);
    if (ptr == MAP_FAILED)



More information about the mesa-commit mailing list