1 /* 2 * memfd.c 3 * 4 * Copyright (c) 2015 Red Hat, Inc. 5 * 6 * QEMU library functions on POSIX which are shared between QEMU and 7 * the QEMU tools. 8 * 9 * Permission is hereby granted, free of charge, to any person obtaining a copy 10 * of this software and associated documentation files (the "Software"), to deal 11 * in the Software without restriction, including without limitation the rights 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 * copies of the Software, and to permit persons to whom the Software is 14 * furnished to do so, subject to the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be included in 17 * all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 * THE SOFTWARE. 26 */ 27 28 #include "qemu/osdep.h" 29 30 #include "qemu/memfd.h" 31 32 #if defined CONFIG_LINUX && !defined CONFIG_MEMFD 33 #include <sys/syscall.h> 34 #include <asm/unistd.h> 35 36 static int memfd_create(const char *name, unsigned int flags) 37 { 38 #ifdef __NR_memfd_create 39 return syscall(__NR_memfd_create, name, flags); 40 #else 41 return -1; 42 #endif 43 } 44 #endif 45 46 #ifndef MFD_CLOEXEC 47 #define MFD_CLOEXEC 0x0001U 48 #endif 49 50 #ifndef MFD_ALLOW_SEALING 51 #define MFD_ALLOW_SEALING 0x0002U 52 #endif 53 54 int qemu_memfd_create(const char *name, size_t size, unsigned int seals) 55 { 56 int mfd = -1; 57 58 #ifdef CONFIG_LINUX 59 unsigned int flags = MFD_CLOEXEC; 60 61 if (seals) { 62 flags |= MFD_ALLOW_SEALING; 63 } 64 65 mfd = memfd_create(name, flags); 66 if (mfd < 0) { 67 return -1; 68 } 69 70 if (ftruncate(mfd, size) == -1) { 71 perror("ftruncate"); 72 close(mfd); 73 return -1; 74 } 75 76 if (seals && fcntl(mfd, F_ADD_SEALS, seals) == -1) { 77 perror("fcntl"); 78 close(mfd); 79 return -1; 80 } 81 #endif 82 83 return mfd; 84 } 85 86 /* 87 * This is a best-effort helper for shared memory allocation, with 88 * optional sealing. The helper will do his best to allocate using 89 * memfd with sealing, but may fallback on other methods without 90 * sealing. 91 */ 92 void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals, 93 int *fd) 94 { 95 void *ptr; 96 int mfd = qemu_memfd_create(name, size, seals); 97 98 /* some systems have memfd without sealing */ 99 if (mfd == -1) { 100 mfd = qemu_memfd_create(name, size, 0); 101 } 102 103 if (mfd == -1) { 104 const char *tmpdir = g_get_tmp_dir(); 105 gchar *fname; 106 107 fname = g_strdup_printf("%s/memfd-XXXXXX", tmpdir); 108 mfd = mkstemp(fname); 109 unlink(fname); 110 g_free(fname); 111 112 if (mfd == -1) { 113 perror("mkstemp"); 114 return NULL; 115 } 116 117 if (ftruncate(mfd, size) == -1) { 118 perror("ftruncate"); 119 close(mfd); 120 return NULL; 121 } 122 } 123 124 ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0); 125 if (ptr == MAP_FAILED) { 126 perror("mmap"); 127 close(mfd); 128 return NULL; 129 } 130 131 *fd = mfd; 132 return ptr; 133 } 134 135 void qemu_memfd_free(void *ptr, size_t size, int fd) 136 { 137 if (ptr) { 138 munmap(ptr, size); 139 } 140 141 if (fd != -1) { 142 close(fd); 143 } 144 } 145 146 enum { 147 MEMFD_KO, 148 MEMFD_OK, 149 MEMFD_TODO 150 }; 151 152 bool qemu_memfd_check(void) 153 { 154 static int memfd_check = MEMFD_TODO; 155 156 if (memfd_check == MEMFD_TODO) { 157 int fd; 158 void *ptr; 159 160 ptr = qemu_memfd_alloc("test", 4096, 0, &fd); 161 memfd_check = ptr ? MEMFD_OK : MEMFD_KO; 162 qemu_memfd_free(ptr, 4096, fd); 163 } 164 165 return memfd_check == MEMFD_OK; 166 } 167