[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v3 7/9] memfd: split qemu_memfd_alloc()
From: |
Marc-André Lureau |
Subject: |
[Qemu-devel] [PATCH v3 7/9] memfd: split qemu_memfd_alloc() |
Date: |
Fri, 2 Jun 2017 18:12:27 +0400 |
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 <address@hidden>
---
include/qemu/memfd.h | 2 ++
util/memfd.c | 42 +++++++++++++++++++++++++-----------------
2 files changed, 27 insertions(+), 17 deletions(-)
diff --git a/include/qemu/memfd.h b/include/qemu/memfd.h
index 745a8c501e..30c1ab1d91 100644
--- a/include/qemu/memfd.h
+++ b/include/qemu/memfd.h
@@ -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);
diff --git a/util/memfd.c b/util/memfd.c
index 4571d1aba8..e6476df083 100644
--- a/util/memfd.c
+++ b/util/memfd.c
@@ -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;
--
2.13.0.91.g00982b8dd
- [Qemu-devel] [PATCH v3 0/9] Add memfd memory backend, Marc-André Lureau, 2017/06/02
- [Qemu-devel] [PATCH v3 1/9] exec: check kvm mmu notifiers earlier, Marc-André Lureau, 2017/06/02
- [Qemu-devel] [PATCH v3 2/9] exec: split file_ram_alloc(), Marc-André Lureau, 2017/06/02
- [Qemu-devel] [PATCH v3 3/9] exec: split qemu_ram_alloc_from_file(), Marc-André Lureau, 2017/06/02
- [Qemu-devel] [PATCH v3 4/9] Add memory_region_init_ram_from_fd(), Marc-André Lureau, 2017/06/02
- [Qemu-devel] [PATCH v3 5/9] ivshmem: use ram_from_fd(), Marc-André Lureau, 2017/06/02
- [Qemu-devel] [PATCH v3 6/9] memory: remove memory_region_set_fd, Marc-André Lureau, 2017/06/02
- [Qemu-devel] [PATCH v3 7/9] memfd: split qemu_memfd_alloc(),
Marc-André Lureau <=
- [Qemu-devel] [PATCH v3 8/9] Add memfd based hostmem, Marc-André Lureau, 2017/06/02
- [Qemu-devel] [PATCH v3 9/9] tests: use memfd in vhost-user-test, Marc-André Lureau, 2017/06/02
- Re: [Qemu-devel] [PATCH v3 0/9] Add memfd memory backend, Daniel P. Berrange, 2017/06/02