diff options
author | Thomas Wagner <twagner@snap.com> | 2021-10-13 09:24:42 +0200 |
---|---|---|
committer | Eric Engestrom <eric@engestrom.ch> | 2021-10-27 19:58:10 +0100 |
commit | f199962c4d0d08bb63490c7abaece8977f92189a (patch) | |
tree | b111699fca7a6505a9855b599908a8f5d6ba9528 | |
parent | bf2e5336886c25abae51f1358e4be2b0d0f297d7 (diff) |
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@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13331>
(cherry picked from commit 4856586ac605e89ee6c128b1a190f000311b49ba)
-rw-r--r-- | .pick_status.json | 2 | ||||
-rw-r--r-- | src/util/anon_file.c | 18 | ||||
-rw-r--r-- | src/util/os_memory_fd.c | 16 |
3 files changed, 20 insertions, 16 deletions
diff --git a/.pick_status.json b/.pick_status.json index 6a97cfeeb56..0e4dd2e7c6b 100644 --- a/.pick_status.json +++ b/.pick_status.json @@ -4,7 +4,7 @@ "description": "util: use anonymous file for memory fd creation", "nominated": true, "nomination_type": 1, - "resolution": 0, + "resolution": 1, "main_sha": null, "because_sha": "1166ee9caf3ef8412c1897d926040035981cb4cf" }, 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) |