]> git.ipfire.org Git - thirdparty/qemu.git/blame - util/memfd.c
memfd: set up correct errno if not supported
[thirdparty/qemu.git] / util / memfd.c
CommitLineData
f04cf923
MAL
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
0f2956f9 30#include "qapi/error.h"
f04cf923 31#include "qemu/memfd.h"
2ef8c0c9 32#include "qemu/host-utils.h"
f04cf923 33
75e5b70e 34#if defined CONFIG_LINUX && !defined CONFIG_MEMFD
f04cf923
MAL
35#include <sys/syscall.h>
36#include <asm/unistd.h>
37
d3592199 38static int memfd_create(const char *name, unsigned int flags)
f04cf923
MAL
39{
40#ifdef __NR_memfd_create
41 return syscall(__NR_memfd_create, name, flags);
42#else
df208193 43 errno = ENOSYS;
f04cf923
MAL
44 return -1;
45#endif
46}
47#endif
48
c5b2a9e0 49int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
2ef8c0c9 50 uint64_t hugetlbsize, unsigned int seals, Error **errp)
dcff1035 51{
2ef8c0c9
MAL
52 int htsize = hugetlbsize ? ctz64(hugetlbsize) : 0;
53
4f938cbd 54 if (htsize && 1ULL << htsize != hugetlbsize) {
2ef8c0c9
MAL
55 error_setg(errp, "Hugepage size must be a power of 2");
56 return -1;
57 }
58
59 htsize = htsize << MFD_HUGE_SHIFT;
60
dcff1035 61#ifdef CONFIG_LINUX
0f2956f9 62 int mfd = -1;
dcff1035
MAL
63 unsigned int flags = MFD_CLOEXEC;
64
65 if (seals) {
66 flags |= MFD_ALLOW_SEALING;
67 }
c5b2a9e0
MAL
68 if (hugetlb) {
69 flags |= MFD_HUGETLB;
2ef8c0c9 70 flags |= htsize;
c5b2a9e0 71 }
dcff1035
MAL
72 mfd = memfd_create(name, flags);
73 if (mfd < 0) {
0f2956f9 74 goto err;
dcff1035
MAL
75 }
76
77 if (ftruncate(mfd, size) == -1) {
0f2956f9 78 goto err;
dcff1035
MAL
79 }
80
81 if (seals && fcntl(mfd, F_ADD_SEALS, seals) == -1) {
0f2956f9 82 goto err;
dcff1035 83 }
dcff1035
MAL
84
85 return mfd;
0f2956f9
MAL
86
87err:
88 if (mfd >= 0) {
89 close(mfd);
90 }
91#endif
92 error_setg_errno(errp, errno, "failed to create memfd");
93 return -1;
dcff1035
MAL
94}
95
d3592199
MAL
96/*
97 * This is a best-effort helper for shared memory allocation, with
98 * optional sealing. The helper will do his best to allocate using
99 * memfd with sealing, but may fallback on other methods without
100 * sealing.
101 */
102void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
0f2956f9 103 int *fd, Error **errp)
d3592199
MAL
104{
105 void *ptr;
2ef8c0c9 106 int mfd = qemu_memfd_create(name, size, false, 0, seals, NULL);
d3592199 107
dcff1035 108 /* some systems have memfd without sealing */
d3592199 109 if (mfd == -1) {
2ef8c0c9 110 mfd = qemu_memfd_create(name, size, false, 0, 0, NULL);
d3592199 111 }
d3592199 112
dcff1035 113 if (mfd == -1) {
35f9b6ef
MAL
114 const char *tmpdir = g_get_tmp_dir();
115 gchar *fname;
116
117 fname = g_strdup_printf("%s/memfd-XXXXXX", tmpdir);
118 mfd = mkstemp(fname);
119 unlink(fname);
120 g_free(fname);
121
0f2956f9
MAL
122 if (mfd == -1 ||
123 ftruncate(mfd, size) == -1) {
124 goto err;
35f9b6ef 125 }
d3592199
MAL
126 }
127
128 ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
129 if (ptr == MAP_FAILED) {
0f2956f9 130 goto err;
d3592199
MAL
131 }
132
133 *fd = mfd;
134 return ptr;
0f2956f9
MAL
135
136err:
137 error_setg_errno(errp, errno, "failed to allocate shared memory");
138 if (mfd >= 0) {
139 close(mfd);
140 }
141 return NULL;
d3592199
MAL
142}
143
144void qemu_memfd_free(void *ptr, size_t size, int fd)
145{
146 if (ptr) {
147 munmap(ptr, size);
148 }
149
150 if (fd != -1) {
151 close(fd);
152 }
153}
31190ed7
MAL
154
155enum {
156 MEMFD_KO,
157 MEMFD_OK,
158 MEMFD_TODO
159};
160
648abbfb
MAL
161/**
162 * qemu_memfd_alloc_check():
163 *
164 * Check if qemu_memfd_alloc() can allocate, including using a
165 * fallback implementation when host doesn't support memfd.
166 */
167bool qemu_memfd_alloc_check(void)
31190ed7
MAL
168{
169 static int memfd_check = MEMFD_TODO;
170
171 if (memfd_check == MEMFD_TODO) {
172 int fd;
173 void *ptr;
174
1e7ec6cf 175 fd = -1;
0f2956f9 176 ptr = qemu_memfd_alloc("test", 4096, 0, &fd, NULL);
31190ed7
MAL
177 memfd_check = ptr ? MEMFD_OK : MEMFD_KO;
178 qemu_memfd_free(ptr, 4096, fd);
179 }
180
181 return memfd_check == MEMFD_OK;
182}
648abbfb
MAL
183
184/**
185 * qemu_memfd_check():
186 *
187 * Check if host supports memfd.
188 */
38296400 189bool qemu_memfd_check(unsigned int flags)
648abbfb
MAL
190{
191#ifdef CONFIG_LINUX
92db922f 192 int mfd = memfd_create("test", flags | MFD_CLOEXEC);
648abbfb 193
38296400
MAL
194 if (mfd >= 0) {
195 close(mfd);
196 return true;
648abbfb 197 }
38296400 198#endif
648abbfb 199
648abbfb 200 return false;
648abbfb 201}