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: 1166ee9caf
("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>
This commit is contained in:
parent
49d290bcf7
commit
4856586ac6
|
@ -35,9 +35,9 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#if defined(__FreeBSD__) || defined(__OpenBSD__)
|
#if defined(HAVE_MEMFD_CREATE) || defined(__FreeBSD__) || defined(__OpenBSD__)
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#elif defined(HAVE_MEMFD_CREATE) || defined(ANDROID)
|
#elif defined(ANDROID)
|
||||||
#include <sys/syscall.h>
|
#include <sys/syscall.h>
|
||||||
#include <linux/memfd.h>
|
#include <linux/memfd.h>
|
||||||
#else
|
#else
|
||||||
|
@ -115,17 +115,21 @@ int
|
||||||
os_create_anonymous_file(off_t size, const char *debug_name)
|
os_create_anonymous_file(off_t size, const char *debug_name)
|
||||||
{
|
{
|
||||||
int fd, ret;
|
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);
|
fd = shm_open(SHM_ANON, O_CREAT | O_RDWR | O_CLOEXEC, 0600);
|
||||||
#elif defined(__OpenBSD__)
|
#elif defined(__OpenBSD__)
|
||||||
char template[] = "/tmp/mesa-XXXXXXXXXX";
|
char template[] = "/tmp/mesa-XXXXXXXXXX";
|
||||||
fd = shm_mkstemp(template);
|
fd = shm_mkstemp(template);
|
||||||
if (fd != -1)
|
if (fd != -1)
|
||||||
shm_unlink(template);
|
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
|
#else
|
||||||
const char *path;
|
const char *path;
|
||||||
char *name;
|
char *name;
|
||||||
|
|
|
@ -36,8 +36,8 @@
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <sys/syscall.h>
|
|
||||||
|
|
||||||
|
#include "anon_file.h"
|
||||||
#include "mesa-sha1.h"
|
#include "mesa-sha1.h"
|
||||||
#include "u_math.h"
|
#include "u_math.h"
|
||||||
#include "os_memory.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;
|
size_t alloc_size, offset;
|
||||||
|
|
||||||
*fd = -1;
|
*fd = -1;
|
||||||
mem_fd = syscall(__NR_memfd_create, fd_name, MFD_CLOEXEC | MFD_ALLOW_SEALING);
|
|
||||||
|
|
||||||
if(mem_fd < 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Calculate
|
* 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);
|
const size_t header_size = sizeof(struct memory_header) + sizeof(size_t);
|
||||||
if (add_overflow_size_t(size, alignment, &alloc_size) ||
|
if (add_overflow_size_t(size, alignment, &alloc_size) ||
|
||||||
add_overflow_size_t(alloc_size, header_size, &alloc_size))
|
add_overflow_size_t(alloc_size, header_size, &alloc_size))
|
||||||
goto fail;
|
return NULL;
|
||||||
|
|
||||||
if (ftruncate(mem_fd, alloc_size) != 0)
|
mem_fd = os_create_anonymous_file(alloc_size, fd_name);
|
||||||
goto fail;
|
|
||||||
|
|
||||||
|
if(mem_fd < 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
#if defined(HAVE_MEMFD_CREATE) || defined(ANDROID)
|
||||||
// Seal fd, so no one can grow or shrink the memory.
|
// 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)
|
if (fcntl(mem_fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_SEAL) != 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
#endif
|
||||||
|
|
||||||
ptr = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0);
|
ptr = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0);
|
||||||
if (ptr == MAP_FAILED)
|
if (ptr == MAP_FAILED)
|
||||||
|
|
Loading…
Reference in New Issue