Postcopy: Add stats on page requests
[qemu.git] / util / memfd.c
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 <glib/gprintf.h>
31
32 #include <sys/mman.h>
33
34 #include "qemu/memfd.h"
35
36 #ifdef CONFIG_MEMFD
37 #include <sys/memfd.h>
38 #elif defined CONFIG_LINUX
39 #include <sys/syscall.h>
40 #include <asm/unistd.h>
41
42 static int memfd_create(const char *name, unsigned int flags)
43 {
44 #ifdef __NR_memfd_create
45 return syscall(__NR_memfd_create, name, flags);
46 #else
47 return -1;
48 #endif
49 }
50 #endif
51
52 #ifndef MFD_CLOEXEC
53 #define MFD_CLOEXEC 0x0001U
54 #endif
55
56 #ifndef MFD_ALLOW_SEALING
57 #define MFD_ALLOW_SEALING 0x0002U
58 #endif
59
60 /*
61 * This is a best-effort helper for shared memory allocation, with
62 * optional sealing. The helper will do his best to allocate using
63 * memfd with sealing, but may fallback on other methods without
64 * sealing.
65 */
66 void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
67 int *fd)
68 {
69 void *ptr;
70 int mfd = -1;
71
72 *fd = -1;
73
74 #ifdef CONFIG_LINUX
75 if (seals) {
76 mfd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
77 }
78
79 if (mfd == -1) {
80 /* some systems have memfd without sealing */
81 mfd = memfd_create(name, MFD_CLOEXEC);
82 seals = 0;
83 }
84 #endif
85
86 if (mfd != -1) {
87 if (ftruncate(mfd, size) == -1) {
88 perror("ftruncate");
89 close(mfd);
90 return NULL;
91 }
92
93 if (seals && fcntl(mfd, F_ADD_SEALS, seals) == -1) {
94 perror("fcntl");
95 close(mfd);
96 return NULL;
97 }
98 } else {
99 const char *tmpdir = g_get_tmp_dir();
100 gchar *fname;
101
102 fname = g_strdup_printf("%s/memfd-XXXXXX", tmpdir);
103 mfd = mkstemp(fname);
104 unlink(fname);
105 g_free(fname);
106
107 if (mfd == -1) {
108 perror("mkstemp");
109 return NULL;
110 }
111
112 if (ftruncate(mfd, size) == -1) {
113 perror("ftruncate");
114 close(mfd);
115 return NULL;
116 }
117 }
118
119 ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
120 if (ptr == MAP_FAILED) {
121 perror("mmap");
122 close(mfd);
123 return NULL;
124 }
125
126 *fd = mfd;
127 return ptr;
128 }
129
130 void qemu_memfd_free(void *ptr, size_t size, int fd)
131 {
132 if (ptr) {
133 munmap(ptr, size);
134 }
135
136 if (fd != -1) {
137 close(fd);
138 }
139 }
140
141 enum {
142 MEMFD_KO,
143 MEMFD_OK,
144 MEMFD_TODO
145 };
146
147 bool qemu_memfd_check(void)
148 {
149 static int memfd_check = MEMFD_TODO;
150
151 if (memfd_check == MEMFD_TODO) {
152 int fd;
153 void *ptr;
154
155 ptr = qemu_memfd_alloc("test", 4096, 0, &fd);
156 memfd_check = ptr ? MEMFD_OK : MEMFD_KO;
157 qemu_memfd_free(ptr, 4096, fd);
158 }
159
160 return memfd_check == MEMFD_OK;
161 }