diff --git a/src/gallium/include/pipe/p_screen.h b/src/gallium/include/pipe/p_screen.h index 9176bf2b586..bb0b53764ed 100644 --- a/src/gallium/include/pipe/p_screen.h +++ b/src/gallium/include/pipe/p_screen.h @@ -561,6 +561,27 @@ struct pipe_screen { void (*free_memory)(struct pipe_screen *screen, struct pipe_memory_allocation *); + /** + * Allocate fd-based memory to be bound to resources. + */ + struct pipe_memory_allocation *(*allocate_memory_fd)(struct pipe_screen *screen, + uint64_t size, + int *fd); + + /** + * Import memory from an fd-handle. + */ + bool (*import_memory_fd)(struct pipe_screen *screen, + int fd, + struct pipe_memory_allocation **pmem, + uint64_t *size); + + /** + * Free previously allocated fd-based memory. + */ + void (*free_memory_fd)(struct pipe_screen *screen, + struct pipe_memory_allocation *pmem); + /** * Bind memory to a resource. */ diff --git a/src/util/meson.build b/src/util/meson.build index b8c2267071b..f377b34daa1 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -73,6 +73,7 @@ files_mesa_util = files( 'os_time.c', 'os_time.h', 'os_file.c', + 'os_memory_fd.c', 'os_misc.c', 'os_misc.h', 'os_socket.c', diff --git a/src/util/os_memory_fd.c b/src/util/os_memory_fd.c new file mode 100644 index 00000000000..37b698e788e --- /dev/null +++ b/src/util/os_memory_fd.c @@ -0,0 +1,182 @@ +/************************************************************************** + * + * Copyright 2021 Snap Inc. + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + **************************************************************************/ + +/* + * Memory fd wrappers. + */ + +#include "detect_os.h" + +#if DETECT_OS_UNIX + +#include +#include +#include +#include +#include + +#include "mesa-sha1.h" +#include "u_math.h" +#include "os_memory.h" + +/* (Re)define UUID_SIZE to avoid including vulkan.h (or p_defines.h) here. */ +#define UUID_SIZE 16 + +struct memory_header { + size_t size; + size_t offset; + uint8_t uuid[UUID_SIZE]; +}; + +static void +get_driver_id_sha1_hash(uint8_t sha1[SHA1_DIGEST_LENGTH], const char *driver_id) { + struct mesa_sha1 sha1_ctx; + _mesa_sha1_init(&sha1_ctx); + + _mesa_sha1_update(&sha1_ctx, driver_id, strlen(driver_id)); + + _mesa_sha1_final(&sha1_ctx, sha1); +} + +/** + * Imports memory from a file descriptor + */ +bool +os_import_memory_fd(int fd, void **ptr, uint64_t *size, char const *driver_id) +{ + void *mapped_ptr; + struct memory_header header; + + lseek(fd, 0, SEEK_SET); + int bytes_read = read(fd, &header, sizeof(header)); + if(bytes_read != sizeof(header)) + return false; + + // Check the uuid we put after the sizes in order to verify that the fd + // is a memfd that we created and not some random fd. + uint8_t sha1[SHA1_DIGEST_LENGTH]; + get_driver_id_sha1_hash(sha1, driver_id); + + assert(SHA1_DIGEST_LENGTH >= UUID_SIZE); + if (memcmp(header.uuid, sha1, UUID_SIZE)) { + return false; + } + + mapped_ptr = mmap(NULL, header.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (mapped_ptr == MAP_FAILED) { + return false; + } + *ptr = (void*)((uintptr_t)mapped_ptr + header.offset); + // the offset does not count as part of the size + *size = header.size - header.offset; + return true; +} + +/** + * Return memory on given byte alignment + */ +void * +os_malloc_aligned_fd(size_t size, size_t alignment, int *fd, char const *fd_name, char const *driver_id) +{ + void *ptr, *buf; + int mem_fd; + 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 + * + * alloc_size = size + alignment + sizeof(struct memory_header) + sizeof(size_t) + * + * while checking for overflow. + */ + 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; + + if (ftruncate(mem_fd, alloc_size) != 0) + goto fail; + + // 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; + + ptr = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0); + if (ptr == MAP_FAILED) + goto fail; + + // Save the size and offset at the start, so we have all we need to unmap the memory + // and we are able to find the start of the actual data-section. Also save the + // offset directly before the data-section, so we can find the start of the mapped memory. + // | size | offset | ... padding ... | offset | ... data ... | + // ^ ^ ^ + // 0 offset size + buf = (char *)(((uintptr_t)ptr + header_size + alignment - 1) & ~((uintptr_t)(alignment - 1))); + offset = (size_t)((uintptr_t)buf - (uintptr_t)ptr); + struct memory_header* header = (struct memory_header*)ptr; + header->size = alloc_size; + header->offset = offset; + ((size_t*)buf)[-1] = offset; + + // Add the hash of the driver_id as a uuid to the header in order to identify the memory + // when importing. + uint8_t sha1[SHA1_DIGEST_LENGTH]; + get_driver_id_sha1_hash(sha1, driver_id); + + assert(SHA1_DIGEST_LENGTH >= UUID_SIZE); + memcpy(header->uuid, sha1, UUID_SIZE); + + *fd = mem_fd; + return buf; + +fail: + close(mem_fd); + return NULL; +} + +/** + * Free memory returned by os_malloc_aligned_fd(). + */ +void +os_free_fd(void *ptr) +{ + if (ptr) { + size_t offset = ((size_t*)ptr)[-1]; + struct memory_header* header = (struct memory_header*)((uintptr_t)ptr - offset); + // check if the offset at the beginning of the memory + // is the same as the one we saved directly before the data. + assert(offset == header->offset); + munmap(header, header->size); + } +} + +#endif diff --git a/src/util/os_memory_fd.h b/src/util/os_memory_fd.h new file mode 100644 index 00000000000..2fd1d2687f6 --- /dev/null +++ b/src/util/os_memory_fd.h @@ -0,0 +1,53 @@ +/************************************************************************** + * + * Copyright 2021 Snap Inc. + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + **************************************************************************/ + + +/* + * Memory fd wrappers. + */ + + +#ifndef _OS_MEMORY_H_ +#error "Must not be included directly. Include os_memory.h instead" +#endif + +/** + * Imports memory from a file descriptor + */ +bool +os_import_memory_fd(int fd, void **ptr, uint64_t *size, char const *driver_id); + +/** + * Return memory on given byte alignment + */ +void * +os_malloc_aligned_fd(size_t size, size_t alignment, int *fd, char const *fd_name, char const *driver_id); + +/** + * Free memory returned by os_malloc_aligned_fd(). + */ +void +os_free_fd(void *ptr); diff --git a/src/util/os_memory_stdc.h b/src/util/os_memory_stdc.h index bda5715998f..62412b5ea22 100644 --- a/src/util/os_memory_stdc.h +++ b/src/util/os_memory_stdc.h @@ -57,4 +57,10 @@ #include "os_memory_aligned.h" +#if DETECT_OS_UNIX + +#include "os_memory_fd.h" + +#endif + #endif