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.