4 * Copyright (c) 2015 Red Hat, Inc.
6 * QEMU library functions on POSIX which are shared between QEMU and
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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
28 #include "qemu/osdep.h"
30 #include "qapi/error.h"
31 #include "qemu/memfd.h"
32 #include "qemu/host-utils.h"
34 #if defined CONFIG_LINUX && !defined CONFIG_MEMFD
35 #include <sys/syscall.h>
36 #include <asm/unistd.h>
38 static int memfd_create(const char *name, unsigned int flags)
40 #ifdef __NR_memfd_create
41 return syscall(__NR_memfd_create, name, flags);
48 int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
49 uint64_t hugetlbsize, unsigned int seals, Error **errp)
51 int htsize = hugetlbsize ? ctz64(hugetlbsize) : 0;
53 if (htsize && 1ULL << htsize != hugetlbsize) {
54 error_setg(errp, "Hugepage size must be a power of 2");
58 htsize = htsize << MFD_HUGE_SHIFT;
62 unsigned int flags = MFD_CLOEXEC;
65 flags |= MFD_ALLOW_SEALING;
71 mfd = memfd_create(name, flags);
76 if (ftruncate(mfd, size) == -1) {
80 if (seals && fcntl(mfd, F_ADD_SEALS, seals) == -1) {
91 error_setg_errno(errp, errno, "failed to create memfd");
96 * This is a best-effort helper for shared memory allocation, with
97 * optional sealing. The helper will do his best to allocate using
98 * memfd with sealing, but may fallback on other methods without
101 void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
102 int *fd, Error **errp)
105 int mfd = qemu_memfd_create(name, size, false, 0, seals, NULL);
107 /* some systems have memfd without sealing */
109 mfd = qemu_memfd_create(name, size, false, 0, 0, NULL);
113 const char *tmpdir = g_get_tmp_dir();
116 fname = g_strdup_printf("%s/memfd-XXXXXX", tmpdir);
117 mfd = mkstemp(fname);
122 ftruncate(mfd, size) == -1) {
127 ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
128 if (ptr == MAP_FAILED) {
136 error_setg_errno(errp, errno, "failed to allocate shared memory");
143 void qemu_memfd_free(void *ptr, size_t size, int fd)
161 * qemu_memfd_alloc_check():
163 * Check if qemu_memfd_alloc() can allocate, including using a
164 * fallback implementation when host doesn't support memfd.
166 bool qemu_memfd_alloc_check(void)
168 static int memfd_check = MEMFD_TODO;
170 if (memfd_check == MEMFD_TODO) {
175 ptr = qemu_memfd_alloc("test", 4096, 0, &fd, NULL);
176 memfd_check = ptr ? MEMFD_OK : MEMFD_KO;
177 qemu_memfd_free(ptr, 4096, fd);
180 return memfd_check == MEMFD_OK;
184 * qemu_memfd_check():
186 * Check if host supports memfd.
188 bool qemu_memfd_check(unsigned int flags)
191 int mfd = memfd_create("test", flags);