Marc-André Lureau | f04cf92 | 2015-10-09 17:17:19 +0200 | [diff] [blame] | 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 | |
Marc-André Lureau | 0f2956f | 2018-02-01 14:27:51 +0100 | [diff] [blame] | 30 | #include "qapi/error.h" |
Marc-André Lureau | f04cf92 | 2015-10-09 17:17:19 +0200 | [diff] [blame] | 31 | #include "qemu/memfd.h" |
Marc-André Lureau | 2ef8c0c | 2018-02-01 14:27:53 +0100 | [diff] [blame] | 32 | #include "qemu/host-utils.h" |
Marc-André Lureau | f04cf92 | 2015-10-09 17:17:19 +0200 | [diff] [blame] | 33 | |
Paolo Bonzini | 75e5b70 | 2017-11-28 11:51:27 +0100 | [diff] [blame] | 34 | #if defined CONFIG_LINUX && !defined CONFIG_MEMFD |
Marc-André Lureau | f04cf92 | 2015-10-09 17:17:19 +0200 | [diff] [blame] | 35 | #include <sys/syscall.h> |
| 36 | #include <asm/unistd.h> |
| 37 | |
Shu-Chun Weng | 9bdfa4d | 2019-08-19 11:09:47 -0700 | [diff] [blame] | 38 | int memfd_create(const char *name, unsigned int flags) |
Marc-André Lureau | f04cf92 | 2015-10-09 17:17:19 +0200 | [diff] [blame] | 39 | { |
| 40 | #ifdef __NR_memfd_create |
| 41 | return syscall(__NR_memfd_create, name, flags); |
| 42 | #else |
Ilya Maximets | df20819 | 2019-03-11 16:58:49 +0300 | [diff] [blame] | 43 | errno = ENOSYS; |
Marc-André Lureau | f04cf92 | 2015-10-09 17:17:19 +0200 | [diff] [blame] | 44 | return -1; |
| 45 | #endif |
| 46 | } |
| 47 | #endif |
| 48 | |
Marc-André Lureau | c5b2a9e | 2018-02-01 14:27:52 +0100 | [diff] [blame] | 49 | int qemu_memfd_create(const char *name, size_t size, bool hugetlb, |
Marc-André Lureau | 2ef8c0c | 2018-02-01 14:27:53 +0100 | [diff] [blame] | 50 | uint64_t hugetlbsize, unsigned int seals, Error **errp) |
Marc-André Lureau | dcff103 | 2017-10-23 15:18:07 +0100 | [diff] [blame] | 51 | { |
Marc-André Lureau | 2ef8c0c | 2018-02-01 14:27:53 +0100 | [diff] [blame] | 52 | int htsize = hugetlbsize ? ctz64(hugetlbsize) : 0; |
| 53 | |
Peter Maydell | 4f938cb | 2018-05-15 18:27:29 +0100 | [diff] [blame] | 54 | if (htsize && 1ULL << htsize != hugetlbsize) { |
Marc-André Lureau | 2ef8c0c | 2018-02-01 14:27:53 +0100 | [diff] [blame] | 55 | error_setg(errp, "Hugepage size must be a power of 2"); |
| 56 | return -1; |
| 57 | } |
| 58 | |
| 59 | htsize = htsize << MFD_HUGE_SHIFT; |
| 60 | |
Marc-André Lureau | dcff103 | 2017-10-23 15:18:07 +0100 | [diff] [blame] | 61 | #ifdef CONFIG_LINUX |
Marc-André Lureau | 0f2956f | 2018-02-01 14:27:51 +0100 | [diff] [blame] | 62 | int mfd = -1; |
Marc-André Lureau | dcff103 | 2017-10-23 15:18:07 +0100 | [diff] [blame] | 63 | unsigned int flags = MFD_CLOEXEC; |
| 64 | |
| 65 | if (seals) { |
| 66 | flags |= MFD_ALLOW_SEALING; |
| 67 | } |
Marc-André Lureau | c5b2a9e | 2018-02-01 14:27:52 +0100 | [diff] [blame] | 68 | if (hugetlb) { |
| 69 | flags |= MFD_HUGETLB; |
Marc-André Lureau | 2ef8c0c | 2018-02-01 14:27:53 +0100 | [diff] [blame] | 70 | flags |= htsize; |
Marc-André Lureau | c5b2a9e | 2018-02-01 14:27:52 +0100 | [diff] [blame] | 71 | } |
Marc-André Lureau | dcff103 | 2017-10-23 15:18:07 +0100 | [diff] [blame] | 72 | mfd = memfd_create(name, flags); |
| 73 | if (mfd < 0) { |
Ilya Maximets | edaed6c | 2019-03-11 16:58:50 +0300 | [diff] [blame] | 74 | error_setg_errno(errp, errno, |
| 75 | "failed to create memfd with flags 0x%x", flags); |
Marc-André Lureau | 0f2956f | 2018-02-01 14:27:51 +0100 | [diff] [blame] | 76 | goto err; |
Marc-André Lureau | dcff103 | 2017-10-23 15:18:07 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | if (ftruncate(mfd, size) == -1) { |
Ilya Maximets | edaed6c | 2019-03-11 16:58:50 +0300 | [diff] [blame] | 80 | error_setg_errno(errp, errno, "failed to resize memfd to %zu", size); |
Marc-André Lureau | 0f2956f | 2018-02-01 14:27:51 +0100 | [diff] [blame] | 81 | goto err; |
Marc-André Lureau | dcff103 | 2017-10-23 15:18:07 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | if (seals && fcntl(mfd, F_ADD_SEALS, seals) == -1) { |
Ilya Maximets | edaed6c | 2019-03-11 16:58:50 +0300 | [diff] [blame] | 85 | error_setg_errno(errp, errno, "failed to add seals 0x%x", seals); |
Marc-André Lureau | 0f2956f | 2018-02-01 14:27:51 +0100 | [diff] [blame] | 86 | goto err; |
Marc-André Lureau | dcff103 | 2017-10-23 15:18:07 +0100 | [diff] [blame] | 87 | } |
Marc-André Lureau | dcff103 | 2017-10-23 15:18:07 +0100 | [diff] [blame] | 88 | |
| 89 | return mfd; |
Marc-André Lureau | 0f2956f | 2018-02-01 14:27:51 +0100 | [diff] [blame] | 90 | |
| 91 | err: |
| 92 | if (mfd >= 0) { |
| 93 | close(mfd); |
| 94 | } |
Ilya Maximets | edaed6c | 2019-03-11 16:58:50 +0300 | [diff] [blame] | 95 | #else |
| 96 | error_setg_errno(errp, ENOSYS, "failed to create memfd"); |
Marc-André Lureau | 0f2956f | 2018-02-01 14:27:51 +0100 | [diff] [blame] | 97 | #endif |
Marc-André Lureau | 0f2956f | 2018-02-01 14:27:51 +0100 | [diff] [blame] | 98 | return -1; |
Marc-André Lureau | dcff103 | 2017-10-23 15:18:07 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Marc-André Lureau | d359219 | 2015-10-09 17:17:20 +0200 | [diff] [blame] | 101 | /* |
| 102 | * This is a best-effort helper for shared memory allocation, with |
| 103 | * optional sealing. The helper will do his best to allocate using |
| 104 | * memfd with sealing, but may fallback on other methods without |
| 105 | * sealing. |
| 106 | */ |
| 107 | void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals, |
Marc-André Lureau | 0f2956f | 2018-02-01 14:27:51 +0100 | [diff] [blame] | 108 | int *fd, Error **errp) |
Marc-André Lureau | d359219 | 2015-10-09 17:17:20 +0200 | [diff] [blame] | 109 | { |
| 110 | void *ptr; |
Marc-André Lureau | 2ef8c0c | 2018-02-01 14:27:53 +0100 | [diff] [blame] | 111 | int mfd = qemu_memfd_create(name, size, false, 0, seals, NULL); |
Marc-André Lureau | d359219 | 2015-10-09 17:17:20 +0200 | [diff] [blame] | 112 | |
Marc-André Lureau | dcff103 | 2017-10-23 15:18:07 +0100 | [diff] [blame] | 113 | /* some systems have memfd without sealing */ |
| 114 | if (mfd == -1) { |
Marc-André Lureau | 2ef8c0c | 2018-02-01 14:27:53 +0100 | [diff] [blame] | 115 | mfd = qemu_memfd_create(name, size, false, 0, 0, NULL); |
Marc-André Lureau | d359219 | 2015-10-09 17:17:20 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | if (mfd == -1) { |
Marc-André Lureau | 35f9b6e | 2015-10-09 17:17:21 +0200 | [diff] [blame] | 119 | const char *tmpdir = g_get_tmp_dir(); |
| 120 | gchar *fname; |
| 121 | |
| 122 | fname = g_strdup_printf("%s/memfd-XXXXXX", tmpdir); |
| 123 | mfd = mkstemp(fname); |
| 124 | unlink(fname); |
| 125 | g_free(fname); |
| 126 | |
Marc-André Lureau | 0f2956f | 2018-02-01 14:27:51 +0100 | [diff] [blame] | 127 | if (mfd == -1 || |
| 128 | ftruncate(mfd, size) == -1) { |
| 129 | goto err; |
Marc-André Lureau | 35f9b6e | 2015-10-09 17:17:21 +0200 | [diff] [blame] | 130 | } |
Marc-André Lureau | d359219 | 2015-10-09 17:17:20 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0); |
| 134 | if (ptr == MAP_FAILED) { |
Marc-André Lureau | 0f2956f | 2018-02-01 14:27:51 +0100 | [diff] [blame] | 135 | goto err; |
Marc-André Lureau | d359219 | 2015-10-09 17:17:20 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | *fd = mfd; |
| 139 | return ptr; |
Marc-André Lureau | 0f2956f | 2018-02-01 14:27:51 +0100 | [diff] [blame] | 140 | |
| 141 | err: |
| 142 | error_setg_errno(errp, errno, "failed to allocate shared memory"); |
| 143 | if (mfd >= 0) { |
| 144 | close(mfd); |
| 145 | } |
| 146 | return NULL; |
Marc-André Lureau | d359219 | 2015-10-09 17:17:20 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void qemu_memfd_free(void *ptr, size_t size, int fd) |
| 150 | { |
| 151 | if (ptr) { |
| 152 | munmap(ptr, size); |
| 153 | } |
| 154 | |
| 155 | if (fd != -1) { |
| 156 | close(fd); |
| 157 | } |
| 158 | } |
Marc-André Lureau | 31190ed | 2015-10-09 17:17:34 +0200 | [diff] [blame] | 159 | |
| 160 | enum { |
| 161 | MEMFD_KO, |
| 162 | MEMFD_OK, |
| 163 | MEMFD_TODO |
| 164 | }; |
| 165 | |
Marc-André Lureau | 648abbf | 2018-03-28 14:18:04 +0200 | [diff] [blame] | 166 | /** |
| 167 | * qemu_memfd_alloc_check(): |
| 168 | * |
| 169 | * Check if qemu_memfd_alloc() can allocate, including using a |
| 170 | * fallback implementation when host doesn't support memfd. |
| 171 | */ |
| 172 | bool qemu_memfd_alloc_check(void) |
Marc-André Lureau | 31190ed | 2015-10-09 17:17:34 +0200 | [diff] [blame] | 173 | { |
| 174 | static int memfd_check = MEMFD_TODO; |
| 175 | |
| 176 | if (memfd_check == MEMFD_TODO) { |
| 177 | int fd; |
| 178 | void *ptr; |
| 179 | |
Dima Stepanov | 1e7ec6c | 2018-06-13 11:19:54 +0300 | [diff] [blame] | 180 | fd = -1; |
Marc-André Lureau | 0f2956f | 2018-02-01 14:27:51 +0100 | [diff] [blame] | 181 | ptr = qemu_memfd_alloc("test", 4096, 0, &fd, NULL); |
Marc-André Lureau | 31190ed | 2015-10-09 17:17:34 +0200 | [diff] [blame] | 182 | memfd_check = ptr ? MEMFD_OK : MEMFD_KO; |
| 183 | qemu_memfd_free(ptr, 4096, fd); |
| 184 | } |
| 185 | |
| 186 | return memfd_check == MEMFD_OK; |
| 187 | } |
Marc-André Lureau | 648abbf | 2018-03-28 14:18:04 +0200 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * qemu_memfd_check(): |
| 191 | * |
| 192 | * Check if host supports memfd. |
| 193 | */ |
Marc-André Lureau | 3829640 | 2018-09-06 20:14:15 +0400 | [diff] [blame] | 194 | bool qemu_memfd_check(unsigned int flags) |
Marc-André Lureau | 648abbf | 2018-03-28 14:18:04 +0200 | [diff] [blame] | 195 | { |
| 196 | #ifdef CONFIG_LINUX |
Ilya Maximets | 92db922 | 2019-03-11 16:58:48 +0300 | [diff] [blame] | 197 | int mfd = memfd_create("test", flags | MFD_CLOEXEC); |
Marc-André Lureau | 648abbf | 2018-03-28 14:18:04 +0200 | [diff] [blame] | 198 | |
Marc-André Lureau | 3829640 | 2018-09-06 20:14:15 +0400 | [diff] [blame] | 199 | if (mfd >= 0) { |
| 200 | close(mfd); |
| 201 | return true; |
Marc-André Lureau | 648abbf | 2018-03-28 14:18:04 +0200 | [diff] [blame] | 202 | } |
Marc-André Lureau | 648abbf | 2018-03-28 14:18:04 +0200 | [diff] [blame] | 203 | #endif |
Marc-André Lureau | 3829640 | 2018-09-06 20:14:15 +0400 | [diff] [blame] | 204 | |
| 205 | return false; |
Marc-André Lureau | 648abbf | 2018-03-28 14:18:04 +0200 | [diff] [blame] | 206 | } |