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