Initialize backend memory objects in parallel
v3 cover Initialize backend memory objects in parallel
v3:
- squash into a single patch
- use global context list for async handling only (MT capability)
- add BQL asserts to guard against concurrent async prealloc requests
- clean up qemu_finish_async_mem_prealloc() error handling
Includes David's suggested restructuring [1] (with David's SoB).
[1] https://lore.kernel.org/qemu-devel/[email protected]/
v2:
- require MADV_POPULATE_WRITE (simplify the implementation)
- require prealloc context threads to ensure optimal thread placement
- use machine phase 'initialized' to determine when to allow parallel init
QEMU initializes preallocated backend memory when parsing the corresponding objects from the command line. In certain scenarios, such as memory being preallocated across multiple numa nodes, this approach is not optimal due to the unnecessary serialization.
This series addresses this issue by initializing the backend memory objects in parallel.
Mark Kanda (1):
oslib-posix: initialize backend memory objects in parallel
backends/hostmem.c | 8 ++- hw/virtio/virtio-mem.c | 4 +- include/qemu/osdep.h | 18 +++++- system/vl.c | 8 +++ util/oslib-posix.c | 131 +++++++++++++++++++++++++++++++---------- util/oslib-win32.c | 8 ++- 6 files changed, 140 insertions(+), 37 deletions(-)
Signature · 1 line
2.39.3
PATCH v3 1/1 oslib-posix: initialize backend memory objects in parallel
QEMU initializes preallocated backend memory as the objects are parsed from the command line. This is not optimal in some cases (e.g. memory spanning multiple NUMA nodes) because the memory objects are initialized in series.
Allow the initialization to occur in parallel (asynchronously). In order to ensure optimal thread placement, asynchronous initialization requires prealloc context threads to be in use.
--- backends/hostmem.c | 8 ++- hw/virtio/virtio-mem.c | 4 +- include/qemu/osdep.h | 18 +++++- system/vl.c | 8 +++ util/oslib-posix.c | 131 +++++++++++++++++++++++++++++++---------- util/oslib-win32.c | 8 ++- 6 files changed, 140 insertions(+), 37 deletions(-)
backends/hostmem.c +6 -2
@@ -20,6 +20,7 @@#include "qom/object_interfaces.h" #include "qemu/mmap-alloc.h" #include "qemu/madvise.h" +#include "hw/qdev-core.h" #ifdef CONFIG_NUMA #include <numaif.h>
@@ -235,9 +236,10 @@ static void host_memory_backend_set_prealloc(Object *obj, bool value,int fd =memory_region_get_fd (&backend->mr);void *ptr =memory_region_get_ram_ptr (&backend->mr);uint64_t sz =memory_region_size (&backend->mr); +bool async = !phase_check (PHASE_MACHINE_INITIALIZED );if (!qemu_prealloc_mem (fd, ptr, sz, backend->prealloc_threads, - backend->prealloc_context, errp)) { + backend->prealloc_context, async, errp)) {return ; } backend->prealloc = true;
@@ -323,6 +325,7 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)HostMemoryBackendClass *bc =MEMORY_BACKEND_GET_CLASS (uc);void *ptr;uint64_t sz; +bool async = !phase_check (PHASE_MACHINE_INITIALIZED );if (!bc->alloc) {return ;
@@ -398,7 +401,8 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)if (backend->prealloc && !qemu_prealloc_mem (memory_region_get_fd (&backend->mr), ptr, sz, backend->prealloc_threads, - backend->prealloc_context, errp)) { + backend->prealloc_context, + async, errp)) {return ; } }
hw/virtio/virtio-mem.c +2 -2
@@ -605,7 +605,7 @@ static int virtio_mem_set_block_state(VirtIOMEM *vmem, uint64_t start_gpa,int fd =memory_region_get_fd (&vmem->memdev->mr);Error *local_err =NULL ; -if (!qemu_prealloc_mem (fd, area, size,1 ,NULL , &local_err)) { +if (!qemu_prealloc_mem (fd, area, size,1 ,NULL , false, &local_err)) {static bool warned;/*
@@ -1248,7 +1248,7 @@ static int virtio_mem_prealloc_range_cb(VirtIOMEM *vmem, void *arg,int fd =memory_region_get_fd (&vmem->memdev->mr);Error *local_err =NULL ; -if (!qemu_prealloc_mem (fd, area, size,1 ,NULL , &local_err)) { +if (!qemu_prealloc_mem (fd, area, size,1 ,NULL , false, &local_err)) {error_report_err (local_err);return -ENOMEM ; }
include/qemu/osdep.h +17 -1
@@ -680,6 +680,8 @@ typedef struct ThreadContext ThreadContext;* @area: start address of the are to preallocate * @sz: the size of the area to preallocate * @max_threads: maximum number of threads to use +* @tc: prealloc context threads pointer, NULL if not in use +* @async: request asynchronous preallocation, requires @tc * @errp: returns an error if this function fails * * Preallocate memory (populate/prefault page tables writable) for the virtual
@@ -687,10 +689,24 @@ typedef struct ThreadContext ThreadContext;* each page in the area was faulted in writable at least once, for example, * after allocating file blocks for mapped files. * +* When setting @async, allocation might be performed asynchronously. +* qemu_finish_async_mem_prealloc() must be called to finish any asynchronous +* preallocation. +* * Return: true on success, else false setting @errp with error. */ bool qemu_prealloc_mem (int fd,char *area,size_t sz,int max_threads, -ThreadContext *tc,Error **errp); +ThreadContext *tc,bool async,Error **errp); + +/** +* qemu_finish_async_mem_prealloc: +* @errp: returns an error if this function fails +* +* Finish all outstanding asynchronous memory preallocation. +* +* Return: true on success, else false setting @errp with error. +*/ +bool qemu_finish_async_mem_prealloc (Error **errp);/** * qemu_get_pid_name:
system/vl.c +8 -0
@@ -2009,6 +2009,14 @@ static void qemu_create_late_backends(void)object_option_foreach_add (object_create_late); +/* +* Wait for any outstanding memory prealloc from created memory +* backends to complete. +*/ +if (!qemu_finish_async_mem_prealloc (&error_fatal)) { +exit (1 ); + } +if (tpm_init () <0 ) {exit (1 ); }
util/oslib-posix.c +100 -31
208 lines not on this page · raw message
util/oslib-win32.c +7 -1
@@ -265,7 +265,7 @@ int getpagesize(void) }bool qemu_prealloc_mem (int fd,char *area,size_t sz,int max_threads, -ThreadContext *tc,Error **errp) +ThreadContext *tc,bool async,Error **errp) {int i;size_t pagesize =qemu_real_host_page_size ();
@@ -278,6 +278,12 @@ bool qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,return true; } +bool qemu_finish_async_mem_prealloc (Error **errp) +{ +/* async prealloc not supported, there is nothing to finish */ +return true; +} +char *qemu_get_pid_name (pid_t pid) {/* XXX Implement me */
Signature · 1 line
2.39.3
Re: [PATCH v3 1/1] oslib-posix: initialize backend memory objects in parallel
Quoted 32 lines · Mark Kanda
QEMU initializes preallocated backend memory as the objects are parsed from the command line. This is not optimal in some cases (e.g. memory spanning multiple NUMA nodes) because the memory objects are initialized in series.
Allow the initialization to occur in parallel (asynchronously). In order to ensure optimal thread placement, asynchronous initialization requires prealloc context threads to be in use.
Signed-off-by Mark Kanda <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- backends/hostmem.c | 8 ++- hw/virtio/virtio-mem.c | 4 +- include/qemu/osdep.h | 18 +++++- system/vl.c | 8 +++ util/oslib-posix.c | 131 +++++++++++++++++++++++++++++++---------- util/oslib-win32.c | 8 ++- 6 files changed, 140 insertions(+), 37 deletions(-)backends/hostmem.c +1
-0@@ -20,6 +20,7 @@#include "qom/object_interfaces.h" #include "qemu/mmap-alloc.h" #include "qemu/madvise.h" +#include "hw/qdev-core.h" #ifdef CONFIG_NUMA #include <numaif.h>
backends/hostmem.c static void host_memory_backend_set_prealloc(Object *obj, bool value, · line 244
Quoted 10 lines
backends/hostmem.c +2
-1@@ -235,9 +236,10 @@ static void host_memory_backend_set_prealloc(Object *obj, bool value,int fd =memory_region_get_fd (&backend->mr);void *ptr =memory_region_get_ram_ptr (&backend->mr);uint64_t sz =memory_region_size (&backend->mr); +bool async = !phase_check (PHASE_MACHINE_INITIALIZED );if (!qemu_prealloc_mem (fd, ptr, sz, backend->prealloc_threads, - backend->prealloc_context, errp)) { + backend->prealloc_context, async, errp)) {return ; }
I think we will never trigger that case: we would have to set the propertly after the device was already initialized, which shouldn't happen.
So I guess we can simplify and drop that.
Quoted 1 line
backends/hostmem.c +0
-0backend->prealloc = true;
[...]
Quoted 23 lines
+++
b/include/qemu/osdep.hbackends/hostmem.c +7
-1@@ -680,6 +680,8 @@ typedef struct ThreadContext ThreadContext;* @area: start address of the are to preallocate * @sz: the size of the area to preallocate * @max_threads: maximum number of threads to use +* @tc: prealloc context threads pointer, NULL if not in use +* @async: request asynchronous preallocation, requires @tc * @errp: returns an error if this function fails * * Preallocate memory (populate/prefault page tables writable) for the virtual @@ -687,10 +689,24 @@ typedef struct ThreadContext ThreadContext;* each page in the area was faulted in writable at least once, for example, * after allocating file blocks for mapped files. * +* When setting @async, allocation might be performed asynchronously. +* qemu_finish_async_mem_prealloc() must be called to finish any asynchronous +* preallocation. +* * Return: true on success, else false setting @errp with error. */ bool qemu_prealloc_mem (int fd,char *area,size_t sz,int max_threads, -ThreadContext *tc,Error **errp); +ThreadContext *tc,bool async,Error **errp);
backends/hostmem.c typedef struct ThreadContext ThreadContext; · line 709
Quoted 10 lines
backends/hostmem.c +10
-0+ +/** +* qemu_finish_async_mem_prealloc: +* @errp: returns an error if this function fails +* +* Finish all outstanding asynchronous memory preallocation. +* +* Return: true on success, else false setting @errp with error. +*/ +bool qemu_finish_async_mem_prealloc (Error **errp);
Suboptimal suggestion from my side, guess it woud be better to call this
"qemu_finish_async_prealloc_mem" to match "qemu_prealloc_mem"
Quoted 9 lines
backends/hostmem.c +0
-0/** * qemu_get_pid_name: system/vl.c +0
-0@@ -2009,6 +2009,14 @@ static void qemu_create_late_backends(void)
system/vl.c static void qemu_create_late_backends(void) · line 2019
Quoted 10 lines
system/vl.c +8
-0object_option_foreach_add (object_create_late); +/* +* Wait for any outstanding memory prealloc from created memory +* backends to complete. +*/ +if (!qemu_finish_async_mem_prealloc (&error_fatal)) { +exit (1 ); + } +
I'm wondering if we should have a new phase instead, like
PHASE_LATE_OBJECTS_CREATED.
and do here
phase_advance(PHASE_LATE_OBJECTS_CREATED);
and use that instead. Currently, there is a "gap" between both things. I don't think anything is actually broken right now (because any internal memory abckend wouldn't have a thread context), but it might be much cleaner and obvious that way.
Apart from that LGTM!
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v3 1/1] oslib-posix: initialize backend memory objects in parallel
Quoted 46 lines · David Hildenbrand
Quoted 32 lines · Mark Kanda
QEMU initializes preallocated backend memory as the objects are parsed from the command line. This is not optimal in some cases (e.g. memory spanning multiple NUMA nodes) because the memory objects are initialized in series.
Allow the initialization to occur in parallel (asynchronously). In order to ensure optimal thread placement, asynchronous initialization requires prealloc context threads to be in use.
Signed-off-by Mark Kanda <[email protected]>Signed-off-by David Hildenbrand <[email protected]>---
backends/hostmem.c | 8 ++-
hw/virtio/virtio-mem.c| 4 +-
include/qemu/osdep.h | 18 +++++-
system/vl.c | 8 +++
util/oslib-posix.c | 131 +++++++++++++++++++++++++++++++---------- util/oslib-win32.c | 8 ++-6 files changed, 140 insertions(+), 37 deletions(-)backends/hostmem.c +1
-0@@ -20,6 +20,7 @@ #include "qom/object_interfaces.h" #include "qemu/mmap-alloc.h" #include "qemu/madvise.h" +#include "hw/qdev-core.h" #ifdef CONFIG_NUMA #include <numaif.h>@@ -235,9 +236,10 @@ static void host_memory_backend_set_prealloc(Object *obj, bool value,backends/hostmem.c static void · line 245
Quoted 10 lines
backends/hostmem.c +2
-1int fd = memory_region_get_fd(&backend->mr); void *ptr = memory_region_get_ram_ptr(&backend->mr); uint64_t sz = memory_region_size(&backend->mr); + bool async = !phase_check(PHASE_MACHINE_INITIALIZED); if (!qemu_prealloc_mem(fd, ptr, sz, backend->prealloc_threads, - backend->prealloc_context, errp)) { + backend->prealloc_context, async, errp)) { return; }I think we will never trigger that case: we would have to set the propertly after the device was already initialized, which shouldn't happen.
Quoted 2 lines
So I guess we can simplify and drop that.
Will fix.
Quoted 42 lines
Quoted 1 line
backend->prealloc= true;[...]
Quoted 24 lines
+++
b/include/qemu/osdep.hbackends/hostmem.c +4
-0@@ -680,6 +680,8 @@ typedef struct ThreadContext ThreadContext; * @area: start address of the are to preallocate * @sz: the size of the area to preallocate * @max_threads: maximum number of threads to use + * @tc: prealloc context threads pointer, NULL if not in use + * @async: request asynchronous preallocation, requires @tc * @errp: returns an error if this function fails * * Preallocate memory (populate/prefault page tables writable) for the virtual@@ -687,10 +689,24 @@ typedef struct ThreadContext ThreadContext; * each page in the area was faulted in writable at least once, for example, * after allocating file blocks for mapped files. * + * When setting @async, allocation might be performed asynchronously. + *qemu_finish_async_mem_prealloc () must be called to finish anyasynchronous
+ * preallocation.
+ *
* Return: true on success, else false setting @errp with error. */
bool qemu_prealloc_mem(int fd, char *area,size_tsz, intmax_threads, - ThreadContext *tc, Error **errp);
+ ThreadContext *tc, bool async, Error **errp);backends/hostmem.c
Quoted 10 lines
backends/hostmem.c +10
-0+ +/** +* qemu_finish_async_mem_prealloc: +* @errp: returns an error if this function fails +* +* Finish all outstanding asynchronous memory preallocation. +* +* Return: true on success, else false setting @errp with error. +*/ +bool qemu_finish_async_mem_prealloc (Error **errp);Suboptimal suggestion from my side, guess it woud be better to call this
Quoted 2 lines
"
qemu_finish_async_prealloc_mem" to match "qemu_prealloc_mem"
Will fix.
Quoted 19 lines
Quoted 8 lines
/**
*qemu_get_pid_name:system/vl.c +0
-0@@ -2009,6 +2009,14 @@ static void qemu_create_late_backends(void) object_option_foreach_add(object_create_late);Quoted 8 lines
+ /*
+ * Wait for any outstanding memory prealloc from created memory + * backends to complete.
+ */
+ if (!qemu_finish_async_mem_prealloc(&error_fatal)) { + exit(1);
+ }
+I'm wondering if we should have a new phase instead, like
Quoted 8 lines
PHASE_LATE_OBJECTS_CREATED.and do here
phase_advance(PHASE_LATE_OBJECTS_CREATED);and use that instead. Currently, there is a "gap" between both things. I don't think anything is actually broken right now (because any internal memory abckend wouldn't have a thread context), but it might be much cleaner and obvious that way.
OK. I'll call it 'PHASE_LATE_BACKENDS_CREATED' (to make it consistent with code comments/function name).
Quoted 2 lines
Apart from that LGTM!
Thanks/regards,
-Mark
Re: [PATCH v3 1/1] oslib-posix: initialize backend memory objects in parallel
Quoted 3 lines
OK. I'll call it '
PHASE_LATE_BACKENDS_CREATED' (to make it consistent with code comments/function name).
But then, you should set it at the very end of the function (not sure if that would be a problem with the other devices that are getting created in between -- if they would be using one of these memory backends; likely not).
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v3 1/1] oslib-posix: initialize backend memory objects in parallel
Quoted 6 lines · David Hildenbrand
Quoted 3 lines
OK. I'll call it '
PHASE_LATE_BACKENDS_CREATED' (to make it consistent with code comments/function name).But then, you should set it at the very end of the function (not sure if that would be a problem with the other devices that are getting created in between -- if they would be using one of these memory backends; likely not).
I think I misunderstood your suggestion. I was planning to add it a 'phase_advance(PHASE_LATE_BACKENDS_CREATED)' to qemu_init():
@@ -3703,6 +3703,7 @@ void qemu_init(int argc, char **argv)
* over memory-backend-file objects).
*/
qemu_create_late_backends();
+ phase_advance(PHASE_LATE_BACKENDS_CREATED);And use PHASE_LATE_BACKENDS_CREATED (instead of PHASE_MACHINE_INITIALIZED) for the async bool in host_memory_backend_memory_complete().
I was planning to leave this call where it is:
@@ -2009,6 +2009,14 @@ static void qemu_create_late_backends(void)
object_option_foreach_add(object_create_late);
+ /*
+ * Wait for any outstanding memory prealloc from created memory
+ * backends to complete.
+ */
+ if (!qemu_finish_async_mem_prealloc(&error_fatal)) {
+ exit(1);
+ }
+
if (tpm_init() < 0) {
exit(1);
}Is this what you had in mind?
Thanks/regards,
-Mark
Re: [PATCH v3 1/1] oslib-posix: initialize backend memory objects in parallel
Quoted 26 lines · Mark Kanda
Quoted 9 lines · David Hildenbrand
Quoted 3 lines
OK. I'll call it '
PHASE_LATE_BACKENDS_CREATED' (to make it consistent with code comments/function name).But then, you should set it at the very end of the function (not sure if that would be a problem with the other devices that are getting created in between -- if they would be using one of these memory backends; likely not).
I think I misunderstood your suggestion. I was planning to add it a '
phase_advance(PHASE_LATE_BACKENDS_CREATED)' toqemu_init():@@ -3703,6 +3703,7 @@ void qemu_init(int argc, char **argv) * over memory-backend-file objects). */ qemu_create_late_backends(); + phase_advance(PHASE_LATE_BACKENDS_CREATED);And use
PHASE_LATE_BACKENDS_CREATED(instead ofPHASE_MACHINE_INITIALIZED) for the async bool inhost_memory_backend_memory_complete().I was planning to leave this call where it is:
Quoted 16 lines
@@ -2009,6 +2009,14 @@ static void qemu_create_late_backends(void)
object_option_foreach_add(object_create_late);
+ /*
+ * Wait for any outstanding memory prealloc from created memory
+ * backends to complete.
+ */
+ if (!qemu_finish_async_mem_prealloc(&error_fatal)) {
+ exit(1);
+ }
+
if (tpm_init() < 0) {
exit(1);
}Yes. The only "suboptimal" things is that if someone where to create a memory backend between qemu_finish_async_mem_prealloc() and phase_advance(PHASE_LATE_BACKENDS_CREATED), it would never get preallocated.
That likely won't ever happen by any of the remaining stuff in qemu_create_late_backends(), especially not with "prealloc=on" and thread-contexts set.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v3 1/1] oslib-posix: initialize backend memory objects in parallel
Quoted 44 lines · David Hildenbrand
Quoted 30 lines · Mark Kanda
Quoted 9 lines · David Hildenbrand
Quoted 3 lines
OK. I'll call it '
PHASE_LATE_BACKENDS_CREATED' (to make it consistent with code comments/function name).But then, you should set it at the very end of the function (not sure if that would be a problem with the other devices that are getting created in between -- if they would be using one of these memory backends; likely not).
I think I misunderstood your suggestion. I was planning to add it a '
phase_advance(PHASE_LATE_BACKENDS_CREATED)' toqemu_init():@@ -3703,6 +3703,7 @@ void
qemu_init(int argc, char **argv)
* over memory-backend-file objects).
*/
qemu_create_late_backends();
+phase_advance(PHASE_LATE_BACKENDS_CREATED);And use
PHASE_LATE_BACKENDS_CREATED(instead ofPHASE_MACHINE_INITIALIZED) for the async bool inhost_memory_backend_memory_complete().I was planning to leave this call where it is:
@@ -2009,6 +2009,14 @@ static void
qemu_create_late_backends(void)
object_option_foreach_add(object_create_late);Quoted 12 lines
+ /*
+ * Wait for any outstanding memory prealloc from created memory + * backends to complete.
+ */
+ if (!qemu_finish_async_mem_prealloc(&error_fatal)) { + exit(1);
+ }
+
if (tpm_init()< 0) {
exit(1);
}
Quoted 4 lines
Yes. The only "suboptimal" things is that if someone where to create a memory backend between
qemu_finish_async_mem_prealloc()andphase_advance(PHASE_LATE_BACKENDS_CREATED), it would never get preallocated.That likely won't ever happen by any of the remaining stuff in
qemu_create_late_backends(), especially not with "prealloc=on" and thread-contexts set.
Yep. OK, I'll go with that.
Thanks again!