We add class properties "hugetlb" and "hugetlbsize" only when a test
of memfd_create() succeeds. Such conditional registration is unusual
and can be problematic:
1. Management applications use introspection interfaces to find out
what a certain QEMU can do. Conditional property addition makes QOM
introspection results depend on the condition, which generally makes
them valid only for the process that produced them. Undesirable.
2. For user-creatable objects like "memory-backend-memfd", it also
makes QAPI introspection unreliable: query-qmp-schema claims the
properties exist (object-add takes arguments @hugetlb and @hugetlbsize
when argument @qom-type is "memory-backend-memfd") even when it
doesn't.
Conditional registration actually serves no purpose here, because we
still have to memfd_create() for real when we create the object, and
need and do handle errors then.
Moreover, we actually KVM ioctl KVM_CREATE_GUEST_MEMFD instead of
memfd_create() there when @guest-memfd is on. If the memfd_create()
check can fail when the ioctl could still succeed, the condition is
counter-productive.
Add the properties unconditionally.
To test, I hacked the qemu_memfd() call to fail. The error for
-object memory-backend-memfd,id=mem0,size=1m,hugetlb=off,hugetlbsize=512,seal=on
changes from
qemu-system-x86_64: Property 'memory-backend-memfd.hugetlbsize' not found
to
qemu-system-x86_64: can't create 'memory-backend-memfd': failed to create
memfd with sealing and without huge pages: Invalid argument
Signed-off-by: Markus Armbruster <[email protected]>
---
backends/hostmem-memfd.c | 26 ++++++++++++--------------
1 file changed, 12 insertions(+), 14 deletions(-)
diff --git a/backends/hostmem-memfd.c b/backends/hostmem-memfd.c
index 54f84ae331a..b945fc46021 100644
--- a/backends/hostmem-memfd.c
+++ b/backends/hostmem-memfd.c
@@ -184,20 +184,18 @@ memfd_backend_class_init(ObjectClass *oc, const void
*data)
bc->alloc = memfd_backend_memory_alloc;
- if (qemu_memfd_check(MFD_HUGETLB)) {
- object_class_property_add_bool(oc, "hugetlb",
- memfd_backend_get_hugetlb,
- memfd_backend_set_hugetlb);
- object_class_property_set_description(oc, "hugetlb",
- "Use huge pages");
- object_class_property_add(oc, "hugetlbsize", "uint64",
- &uint64_type_info,
- memfd_backend_get_hugetlbsize,
- memfd_backend_set_hugetlbsize,
- NULL, NULL);
- object_class_property_set_description(oc, "hugetlbsize",
- "Huge pages size (ex: 2M, 1G)");
- }
+ object_class_property_add_bool(oc, "hugetlb",
+ memfd_backend_get_hugetlb,
+ memfd_backend_set_hugetlb);
+ object_class_property_set_description(oc, "hugetlb",
+ "Use huge pages");
+ object_class_property_add(oc, "hugetlbsize", "uint64",
+ &uint64_type_info,
+ memfd_backend_get_hugetlbsize,
+ memfd_backend_set_hugetlbsize,
+ NULL, NULL);
+ object_class_property_set_description(oc, "hugetlbsize",
+ "Huge pages size (ex: 2M, 1G)");
object_class_property_add_enum(oc, "guest-memfd", "OnOffAuto",
&OnOffAuto_type_info,
--
2.55.0