aboutsummaryrefslogtreecommitdiff
path: root/util/memfd.c
diff options
context:
space:
mode:
authorPeter Maydell <peter.maydell@linaro.org>2018-01-19 16:35:25 +0000
committerPeter Maydell <peter.maydell@linaro.org>2018-01-19 16:35:25 +0000
commitb384cd95eb9c6f73ad84ed1bb0717a26e29cc78f (patch)
treebbd0e7a39efd262c9196edea6d75a88d47557d8a /util/memfd.c
parent3e5bdc6573edf0585e4085e6a4e349b135abf3b4 (diff)
parentd6b6abc51dda79a97f2c7bd6652c1940c068f1ec (diff)
Merge remote-tracking branch 'remotes/ehabkost/tags/machine-next-pull-request' into staging
machine queue, 2018-01-19 # gpg: Signature made Fri 19 Jan 2018 16:30:19 GMT # gpg: using RSA key 0x2807936F984DC5A6 # gpg: Good signature from "Eduardo Habkost <ehabkost@redhat.com>" # Primary key fingerprint: 5A32 2FD5 ABC4 D3DB ACCF D1AA 2807 936F 984D C5A6 * remotes/ehabkost/tags/machine-next-pull-request: fw_cfg: fix memory corruption when all fw_cfg slots are used possible_cpus: add CPUArchId::type field nvdimm: add 'unarmed' option nvdimm: add a macro for property "label-size" hostmem-file: add "align" option scripts: Remove fixed entries from the device-crash-test qdev: Check for the availability of a hotplug controller before adding a device qdev_monitor: Simplify error handling in qdev_device_add() q35: Allow only supported dynamic sysbus devices xen: Add only xen-sysdev to dynamic sysbus device list spapr: Allow only supported dynamic sysbus devices ppc: e500: Allow only supported dynamic sysbus devices hw/arm/virt: Allow only supported dynamic sysbus devices machine: Replace has_dynamic_sysbus with list of allowed devices numa: fix missing '-numa cpu' in '-help' output qemu-options: document memory-backend-ram qemu-options: document missing memory-backend-file options memfd: remove needless include memfd: split qemu_memfd_alloc() Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Diffstat (limited to 'util/memfd.c')
-rw-r--r--util/memfd.c63
1 files changed, 36 insertions, 27 deletions
diff --git a/util/memfd.c b/util/memfd.c
index 412e94a405..dce61f9d21 100644
--- a/util/memfd.c
+++ b/util/memfd.c
@@ -27,8 +27,6 @@
#include "qemu/osdep.h"
-#include <glib/gprintf.h>
-
#include "qemu/memfd.h"
#if defined CONFIG_LINUX && !defined CONFIG_MEMFD
@@ -53,6 +51,38 @@ static int memfd_create(const char *name, unsigned int flags)
#define MFD_ALLOW_SEALING 0x0002U
#endif
+int qemu_memfd_create(const char *name, size_t size, unsigned int seals)
+{
+ int mfd = -1;
+
+#ifdef CONFIG_LINUX
+ unsigned int flags = MFD_CLOEXEC;
+
+ if (seals) {
+ flags |= MFD_ALLOW_SEALING;
+ }
+
+ mfd = memfd_create(name, flags);
+ if (mfd < 0) {
+ return -1;
+ }
+
+ if (ftruncate(mfd, size) == -1) {
+ perror("ftruncate");
+ close(mfd);
+ return -1;
+ }
+
+ if (seals && fcntl(mfd, F_ADD_SEALS, seals) == -1) {
+ perror("fcntl");
+ close(mfd);
+ return -1;
+ }
+#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
@@ -63,35 +93,14 @@ void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
int *fd)
{
void *ptr;
- int mfd = -1;
-
- *fd = -1;
-
-#ifdef CONFIG_LINUX
- if (seals) {
- mfd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
- }
+ int mfd = qemu_memfd_create(name, size, seals);
+ /* some systems have memfd without sealing */
if (mfd == -1) {
- /* some systems have memfd without sealing */
- mfd = memfd_create(name, MFD_CLOEXEC);
- seals = 0;
+ mfd = qemu_memfd_create(name, size, 0);
}
-#endif
-
- if (mfd != -1) {
- if (ftruncate(mfd, size) == -1) {
- perror("ftruncate");
- close(mfd);
- return NULL;
- }
- if (seals && fcntl(mfd, F_ADD_SEALS, seals) == -1) {
- perror("fcntl");
- close(mfd);
- return NULL;
- }
- } else {
+ if (mfd == -1) {
const char *tmpdir = g_get_tmp_dir();
gchar *fname;