@@ -16,6 +16,8 @@
#define F_SEAL_WRITE 0x0008 /* prevent writes */
#endif
+int qemu_memfd_create(const char *name, size_t size, unsigned int seals,
+ bool must_seal);
void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
int *fd);
void qemu_memfd_free(void *ptr, size_t size, int fd);
@@ -55,45 +55,53 @@ static int memfd_create(const char *name, unsigned int flags)
#define MFD_ALLOW_SEALING 0x0002U
#endif
-/*
- * This is a best-effort helper for shared memory allocation, with
- * optional sealing. The helper will do his best to allocate using
- * memfd with sealing, but may fallback on other methods without
- * sealing.
- */
-void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
- int *fd)
+int qemu_memfd_create(const char *name, size_t size, unsigned int seals,
+ bool must_seal)
{
- void *ptr;
int mfd = -1;
- *fd = -1;
-
#ifdef CONFIG_LINUX
if (seals) {
mfd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
}
- if (mfd == -1) {
+ if (mfd == -1 && !must_seal) {
/* some systems have memfd without sealing */
mfd = memfd_create(name, MFD_CLOEXEC);
seals = 0;
}
-#endif
- if (mfd != -1) {
+ if (mfd >= 0) {
if (ftruncate(mfd, size) == -1) {
perror("ftruncate");
close(mfd);
- return NULL;
+ return -1;
}
if (seals && fcntl(mfd, F_ADD_SEALS, seals) == -1) {
perror("fcntl");
close(mfd);
- return NULL;
+ return -1;
}
- } else {
+ }
+#endif
+
+ return mfd;
+}
+
+/*
+ * This is a best-effort helper for shared memory allocation, with
+ * optional sealing. The helper will do his best to allocate using
+ * memfd with sealing, but may fallback on other methods without
+ * sealing.
+ */
+void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
+ int *fd)
+{
+ void *ptr;
+ int mfd = qemu_memfd_create(name, size, seals, false);
+
+ if (mfd == -1) {
const char *tmpdir = g_get_tmp_dir();
gchar *fname;
Add a function to only create a memfd, without mmap. The function is used in the following memory backend. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> --- include/qemu/memfd.h | 2 ++ util/memfd.c | 42 +++++++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 17 deletions(-)