Merge remote-tracking branch 'remotes/ehabkost/tags/x86-pull-request' into staging
[qemu.git] / util / memfd.c
blob7c406914c592a422a026a363c00ce2977a7154ad
1 /*
2 * memfd.c
4 * Copyright (c) 2015 Red Hat, Inc.
6 * QEMU library functions on POSIX which are shared between QEMU and
7 * the QEMU tools.
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
25 * THE SOFTWARE.
28 #include "qemu/osdep.h"
30 #include <glib.h>
31 #include <glib/gprintf.h>
33 #include <sys/mman.h>
35 #include "qemu/memfd.h"
37 #ifdef CONFIG_MEMFD
38 #include <sys/memfd.h>
39 #elif defined CONFIG_LINUX
40 #include <sys/syscall.h>
41 #include <asm/unistd.h>
43 static int memfd_create(const char *name, unsigned int flags)
45 #ifdef __NR_memfd_create
46 return syscall(__NR_memfd_create, name, flags);
47 #else
48 return -1;
49 #endif
51 #endif
53 #ifndef MFD_CLOEXEC
54 #define MFD_CLOEXEC 0x0001U
55 #endif
57 #ifndef MFD_ALLOW_SEALING
58 #define MFD_ALLOW_SEALING 0x0002U
59 #endif
62 * This is a best-effort helper for shared memory allocation, with
63 * optional sealing. The helper will do his best to allocate using
64 * memfd with sealing, but may fallback on other methods without
65 * sealing.
67 void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
68 int *fd)
70 void *ptr;
71 int mfd = -1;
73 *fd = -1;
75 #ifdef CONFIG_LINUX
76 if (seals) {
77 mfd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
80 if (mfd == -1) {
81 /* some systems have memfd without sealing */
82 mfd = memfd_create(name, MFD_CLOEXEC);
83 seals = 0;
85 #endif
87 if (mfd != -1) {
88 if (ftruncate(mfd, size) == -1) {
89 perror("ftruncate");
90 close(mfd);
91 return NULL;
94 if (seals && fcntl(mfd, F_ADD_SEALS, seals) == -1) {
95 perror("fcntl");
96 close(mfd);
97 return NULL;
99 } else {
100 const char *tmpdir = g_get_tmp_dir();
101 gchar *fname;
103 fname = g_strdup_printf("%s/memfd-XXXXXX", tmpdir);
104 mfd = mkstemp(fname);
105 unlink(fname);
106 g_free(fname);
108 if (mfd == -1) {
109 perror("mkstemp");
110 return NULL;
113 if (ftruncate(mfd, size) == -1) {
114 perror("ftruncate");
115 close(mfd);
116 return NULL;
120 ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
121 if (ptr == MAP_FAILED) {
122 perror("mmap");
123 close(mfd);
124 return NULL;
127 *fd = mfd;
128 return ptr;
131 void qemu_memfd_free(void *ptr, size_t size, int fd)
133 if (ptr) {
134 munmap(ptr, size);
137 if (fd != -1) {
138 close(fd);
142 enum {
143 MEMFD_KO,
144 MEMFD_OK,
145 MEMFD_TODO
148 bool qemu_memfd_check(void)
150 static int memfd_check = MEMFD_TODO;
152 if (memfd_check == MEMFD_TODO) {
153 int fd;
154 void *ptr;
156 ptr = qemu_memfd_alloc("test", 4096, 0, &fd);
157 memfd_check = ptr ? MEMFD_OK : MEMFD_KO;
158 qemu_memfd_free(ptr, 4096, fd);
161 return memfd_check == MEMFD_OK;