From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Cc: david@kernel.org, imammedo@redhat.com, berrange@redhat.com,
peter.maydell@linaro.org
Subject: Use of qemu_memfd_check() in system/physmem.c (was: [PATCH 0/7] backends/hostmem: Make QOM types and properties unconditional)
Date: Wed, 11 Nov 2026 13:36:52 +0100 [thread overview]
Message-ID: <87qzgrv923.fsf@pond.sub.org> (raw)
In-Reply-To: <20261111122342.634370-1-armbru@redhat.com> (Markus Armbruster's message of "Wed, 11 Nov 2026 13:23:35 +0100")
After this series, there is just one use of qemu_memfd_check() outside
tests/ left:
/*
* Create MAP_SHARED RAMBlocks by mmap'ing a file descriptor, so it can be
* shared with another process if CPR is being used. Use memfd if available
* because it has no size limits, else use POSIX shm.
*/
static int qemu_ram_get_shared_fd(const char *name, bool *reused, Error **errp)
{
int fd = cpr_find_fd(name, 0);
if (fd >= 0) {
*reused = true;
return fd;
}
---> if (qemu_memfd_check(0)) {
fd = qemu_memfd_create(name, 0, 0, 0, 0, errp);
} else {
fd = qemu_shm_alloc(0, errp);
}
if (fd >= 0 && !cpr_save_fd(name, 0, fd, errp)) {
close(fd);
fd = -1;
}
*reused = false;
return fd;
}
We test whether memfd_create("test", MFD_CLOEXEC) succeeds to decide
whether to use memfd_create(name, MFD_CLOEXEC) or POSIX shared memory.
This is anti-pattern "predict which method should succed, then try that
method". A better way is "try methods one after the other until one
succeeds".
Doing that here would require refactoring, because both
qemu_memfd_create() and qemu_shm_alloc() do additional work that can
also fail: ftruncate().
Doubt: qemu_memfd_create(0) caches its result[*]. Is that a mere
optimization, or does something depend on "we use the same method
always"?
I don't plan to mess with this myself, I'm merely reporting something I
found.
[*] Only when the argument is 0.
prev parent reply other threads:[~2026-11-11 12:37 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-11 12:23 [PATCH 0/7] backends/hostmem: Make QOM types and properties unconditional Markus Armbruster
2026-11-11 12:23 ` [PATCH 1/7] backends/hostmem: Drop redundant validation of property "size" Markus Armbruster
2026-11-11 12:45 ` Daniel P. Berrangé
2026-11-12 7:44 ` Markus Armbruster
2026-11-12 8:12 ` Daniel P. Berrangé
2026-11-12 11:49 ` Markus Armbruster
2026-11-11 16:42 ` Mark Cave-Ayland
2026-11-11 12:23 ` [PATCH 2/7] backends/hostmem-file: Drop useless validation of property "mem-path" Markus Armbruster
2026-11-11 12:50 ` Daniel P. Berrangé
2026-11-11 16:48 ` Mark Cave-Ayland
2026-11-11 12:23 ` [PATCH 3/7] backends/hostmem: Improve error reporting when method alloc() fails Markus Armbruster
2026-11-11 12:51 ` Daniel P. Berrangé
2026-11-11 16:49 ` Mark Cave-Ayland
2026-11-11 12:23 ` [PATCH 4/7] backends/hostmem-epc: Register QOM type unconditionally Markus Armbruster
2026-11-11 12:53 ` Daniel P. Berrangé
2026-11-11 16:50 ` Mark Cave-Ayland
2026-11-11 12:23 ` [PATCH 5/7] backends/hostmem-memfd: " Markus Armbruster
2026-11-11 12:54 ` Daniel P. Berrangé
2026-11-11 16:51 ` Mark Cave-Ayland
2026-11-11 12:23 ` [PATCH 6/7] util/memfd: Improve error message when memfd_create() fails Markus Armbruster
2026-11-11 12:56 ` Daniel P. Berrangé
2026-11-11 16:53 ` Mark Cave-Ayland
2026-11-11 12:23 ` [PATCH 7/7] backends/hostmem-memfd: Add huge TLB properties unconditionally Markus Armbruster
2026-11-11 12:56 ` Daniel P. Berrangé
2026-11-11 16:54 ` Mark Cave-Ayland
2026-11-11 12:36 ` Markus Armbruster [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87qzgrv923.fsf@pond.sub.org \
--to=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=david@kernel.org \
--cc=imammedo@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.