hostmem-memfd: add checks before adding hostmem-memfd & properties
PATCH 1/1 hostmem-memfd: add checks before adding hostmem-memfd & properties
Run some memfd-related checks before registering hostmem-memfd & various properties. This will help libvirt to figure out what the host is supposed to be capable of.
qemu_memfd_check() is changed to a less optimized version, since it is used with various flags, it no longer caches the result.
Signed-off-by Marc-André Lureau <[email protected]>
--- include/qemu/memfd.h | 18 +++++++++++++++++- backends/hostmem-memfd.c | 32 +++++++++++++++++++------------- tests/vhost-user-test.c | 6 +++--- util/memfd.c | 35 ++++++----------------------------- 4 files changed, 45 insertions(+), 46 deletions(-)
include/qemu/memfd.h +17 -1
@@ -16,12 +16,28 @@#define F_SEAL_WRITE 0x0008/* prevent writes */ #endif +#ifndef MFD_CLOEXEC +#define MFD_CLOEXEC 0x0001U +#endif + +#ifndef MFD_ALLOW_SEALING +#define MFD_ALLOW_SEALING 0x0002U +#endif + +#ifndef MFD_HUGETLB +#define MFD_HUGETLB 0x0004U +#endif + +#ifndef MFD_HUGE_SHIFT +#define MFD_HUGE_SHIFT 26 +#endif +int qemu_memfd_create (const char *name,size_t size,bool hugetlb,uint64_t hugetlbsize,unsigned int seals,Error **errp);bool qemu_memfd_alloc_check (void );void *qemu_memfd_alloc (const char *name,size_t size,unsigned int seals,int *fd,Error **errp);void qemu_memfd_free (void *ptr,size_t size,int fd); -bool qemu_memfd_check (void ); +bool qemu_memfd_check (unsigned int flags);#endif /* QEMU_MEMFD_H */
backends/hostmem-memfd.c +19 -13
@@ -138,18 +138,22 @@ memfd_backend_class_init(ObjectClass *oc, void *data) bc->alloc = memfd_backend_memory_alloc; -object_class_property_add_bool (oc,"hugetlb" , - memfd_backend_get_hugetlb, - memfd_backend_set_hugetlb, - &error_abort); -object_class_property_add (oc,"hugetlbsize" ,"int" , - memfd_backend_get_hugetlbsize, - memfd_backend_set_hugetlbsize, -NULL ,NULL , &error_abort); -object_class_property_add_bool (oc,"seal" , - memfd_backend_get_seal, - memfd_backend_set_seal, - &error_abort); +if (qemu_memfd_check (MFD_HUGETLB )) { +object_class_property_add_bool (oc,"hugetlb" , + memfd_backend_get_hugetlb, + memfd_backend_set_hugetlb, + &error_abort); +object_class_property_add (oc,"hugetlbsize" ,"int" , + memfd_backend_get_hugetlbsize, + memfd_backend_set_hugetlbsize, +NULL ,NULL , &error_abort); + } +if (qemu_memfd_check (MFD_ALLOW_SEALING )) { +object_class_property_add_bool (oc,"seal" , + memfd_backend_get_seal, + memfd_backend_set_seal, + &error_abort); + } }static const TypeInfo memfd_backend_info = {
@@ -162,7 +166,9 @@ static const TypeInfo memfd_backend_info = {static void register_types (void ) { -type_register_static (&memfd_backend_info); +if (qemu_memfd_check (0 )) { +type_register_static (&memfd_backend_info); + } }type_init (register_types);
tests/vhost-user-test.c +3 -3
@@ -169,7 +169,7 @@ static char *get_qemu_cmd(TestServer *s, int mem, enum test_memfd memfd, const char *mem_path, const char *chr_opts, const char *extra) { - if (memfd == TEST_MEMFD_AUTO && qemu_memfd_check()) { + if (memfd == TEST_MEMFD_AUTO && qemu_memfd_check(0)) { memfd =TEST_MEMFD_YES ; }
@@ -903,7 +903,7 @@ static void test_multiqueue(void) s->queues =2 ;test_server_listen (s); -if (qemu_memfd_check ()) { +if (qemu_memfd_check (0 )) { cmd = g_strdup_printf( QEMU_CMD_MEMFD QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d " "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
@@ -963,7 +963,7 @@ int main(int argc, char **argv)/* run the main loop thread so the chardev may operate */ thread =g_thread_new (NULL , thread_function, loop); -if (qemu_memfd_check ()) { +if (qemu_memfd_check (0 )) {qtest_add_data_func ("/vhost-user/read-guest-mem/memfd" ,GINT_TO_POINTER (TEST_MEMFD_YES ), test_read_guest_mem);
util/memfd.c +6 -29
@@ -45,22 +45,6 @@ static int memfd_create(const char *name, unsigned int flags) }#endif -#ifndef MFD_CLOEXEC -#define MFD_CLOEXEC 0x0001U -#endif - -#ifndef MFD_ALLOW_SEALING -#define MFD_ALLOW_SEALING 0x0002U -#endif - -#ifndef MFD_HUGETLB -#define MFD_HUGETLB 0x0004U -#endif - -#ifndef MFD_HUGE_SHIFT -#define MFD_HUGE_SHIFT 26 -#endif -int qemu_memfd_create (const char *name,size_t size,bool hugetlb,uint64_t hugetlbsize,unsigned int seals,Error **errp) {
@@ -200,23 +184,16 @@ bool qemu_memfd_alloc_check(void)* * Check if host supports memfd. */ -bool qemu_memfd_check (void ) +bool qemu_memfd_check (unsigned int flags) {#ifdef CONFIG_LINUX -static int memfd_check =MEMFD_TODO ; +int mfd =memfd_create ("test" , flags); -if (memfd_check ==MEMFD_TODO ) { -int mfd =memfd_create ("test" ,0 ); -if (mfd >=0 ) { - memfd_check =MEMFD_OK ; -close (mfd); - }else { - memfd_check =MEMFD_KO ; - } +if (mfd >=0 ) { +close (mfd); +return true; } +#endif -return memfd_check ==MEMFD_OK ; -#else return false; -#endif }
Signature · 1 line
2.19.0.rc1
Paolo Bonzini
AppliedQuoted 178 lines · Marc-André Lureau
Run some memfd-related checks before registering hostmem-memfd & various properties. This will help libvirt to figure out what the host is supposed to be capable of.
qemu_memfd_check()is changed to a less optimized version, since it is used with various flags, it no longer caches the result.Signed-off-by Marc-André Lureau <[email protected]>--- include/qemu/memfd.h | 18 +++++++++++++++++- backends/hostmem-memfd.c | 32 +++++++++++++++++++------------- tests/vhost-user-test.c | 6 +++--- util/memfd.c | 35 ++++++----------------------------- 4 files changed, 45 insertions(+), 46 deletions(-)include/qemu/memfd.h +17
-1@@ -16,12 +16,28 @@#define F_SEAL_WRITE 0x0008/* prevent writes */ #endif +#ifndef MFD_CLOEXEC +#define MFD_CLOEXEC 0x0001U +#endif + +#ifndef MFD_ALLOW_SEALING +#define MFD_ALLOW_SEALING 0x0002U +#endif + +#ifndef MFD_HUGETLB +#define MFD_HUGETLB 0x0004U +#endif + +#ifndef MFD_HUGE_SHIFT +#define MFD_HUGE_SHIFT 26 +#endif +int qemu_memfd_create (const char *name,size_t size,bool hugetlb,uint64_t hugetlbsize,unsigned int seals,Error **errp);bool qemu_memfd_alloc_check (void );void *qemu_memfd_alloc (const char *name,size_t size,unsigned int seals,int *fd,Error **errp);void qemu_memfd_free (void *ptr,size_t size,int fd); -bool qemu_memfd_check (void ); +bool qemu_memfd_check (unsigned int flags);#endif /* QEMU_MEMFD_H */ backends/hostmem-memfd.c +19
-13@@ -138,18 +138,22 @@ memfd_backend_class_init(ObjectClass *oc, void *data) bc->alloc = memfd_backend_memory_alloc; -object_class_property_add_bool (oc,"hugetlb" , - memfd_backend_get_hugetlb, - memfd_backend_set_hugetlb, - &error_abort); -object_class_property_add (oc,"hugetlbsize" ,"int" , - memfd_backend_get_hugetlbsize, - memfd_backend_set_hugetlbsize, -NULL ,NULL , &error_abort); -object_class_property_add_bool (oc,"seal" , - memfd_backend_get_seal, - memfd_backend_set_seal, - &error_abort); +if (qemu_memfd_check (MFD_HUGETLB )) { +object_class_property_add_bool (oc,"hugetlb" , + memfd_backend_get_hugetlb, + memfd_backend_set_hugetlb, + &error_abort); +object_class_property_add (oc,"hugetlbsize" ,"int" , + memfd_backend_get_hugetlbsize, + memfd_backend_set_hugetlbsize, +NULL ,NULL , &error_abort); + } +if (qemu_memfd_check (MFD_ALLOW_SEALING )) { +object_class_property_add_bool (oc,"seal" , + memfd_backend_get_seal, + memfd_backend_set_seal, + &error_abort); + } }static const TypeInfo memfd_backend_info = {@@ -162,7 +166,9 @@ static const TypeInfo memfd_backend_info = {static void register_types (void ) { -type_register_static (&memfd_backend_info); +if (qemu_memfd_check (0 )) { +type_register_static (&memfd_backend_info); + } }type_init (register_types);tests/vhost-user-test.c +3
-3@@ -169,7 +169,7 @@ static char *get_qemu_cmd(TestServer *s, int mem, enum test_memfd memfd, const char *mem_path, const char *chr_opts, const char *extra) { - if (memfd == TEST_MEMFD_AUTO && qemu_memfd_check()) { + if (memfd == TEST_MEMFD_AUTO && qemu_memfd_check(0)) { memfd =TEST_MEMFD_YES ; }@@ -903,7 +903,7 @@ static void test_multiqueue(void) s->queues =2 ;test_server_listen (s); -if (qemu_memfd_check ()) { +if (qemu_memfd_check (0 )) { cmd = g_strdup_printf( QEMU_CMD_MEMFD QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d " "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",@@ -963,7 +963,7 @@ int main(int argc, char **argv)/* run the main loop thread so the chardev may operate */ thread =g_thread_new (NULL , thread_function, loop); -if (qemu_memfd_check ()) { +if (qemu_memfd_check (0 )) {qtest_add_data_func ("/vhost-user/read-guest-mem/memfd" ,GINT_TO_POINTER (TEST_MEMFD_YES ), test_read_guest_mem);util/memfd.c +3
-26@@ -45,22 +45,6 @@ static int memfd_create(const char *name, unsigned int flags) }#endif -#ifndef MFD_CLOEXEC -#define MFD_CLOEXEC 0x0001U -#endif - -#ifndef MFD_ALLOW_SEALING -#define MFD_ALLOW_SEALING 0x0002U -#endif - -#ifndef MFD_HUGETLB -#define MFD_HUGETLB 0x0004U -#endif - -#ifndef MFD_HUGE_SHIFT -#define MFD_HUGE_SHIFT 26 -#endif -int qemu_memfd_create (const char *name,size_t size,bool hugetlb,uint64_t hugetlbsize,unsigned int seals,Error **errp) {@@ -200,23 +184,16 @@ bool qemu_memfd_alloc_check(void)* * Check if host supports memfd. */ -bool qemu_memfd_check (void ) +bool qemu_memfd_check (unsigned int flags) {#ifdef CONFIG_LINUX -static int memfd_check =MEMFD_TODO ; +int mfd =memfd_create ("test" , flags); -if (memfd_check ==MEMFD_TODO ) { -int mfd =memfd_create ("test" ,0 ); -if (mfd >=0 ) { - memfd_check =MEMFD_OK ; -close (mfd); - }else { - memfd_check =MEMFD_KO ; - } +if (mfd >=0 ) {
util/memfd.c bool qemu_memfd_alloc_check(void) · line 199
Quoted 10 lines
util/memfd.c +3
-3+close (mfd); + return true; } +#endif -return memfd_check ==MEMFD_OK ; -#else return false; -#endif }
Queued, thanks.
Paolo