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 #include -#if defined(__FreeBSD__) || defined(__OpenBSD__) +#if defined(HAVE_MEMFD_CREATE) || defined(__FreeBSD__) || defined(__OpenBSD__) #include -#elif defined(HAVE_MEMFD_CREATE) || defined(ANDROID) +#elif defined(ANDROID) #include #include #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 #include #include -#include +#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)