util/oslib-posix: Support MADV_POPULATE_WRITE for os_mem_prealloc()
v3 cover util/oslib-posix: Support MADV_POPULATE_WRITE for os_mem_prealloc()
#1 is a preparation for improved error reporting, #2 adds support for MADV_POPULATE_WRITE, #3 cleans up the code to avoid global variables and prepare for concurrency, #4 and #5 optimize thread handling, #6 makes os_mem_prealloc() safe to be called from multiple threads concurrently and #7 makes the SIGBUS handler coexist cleanly with the MCE SIGBUS handler under Linux.
Details regarding MADV_POPULATE_WRITE can be found in introducing upstream Linux commits 4ca9b3859dac ("mm/madvise: introduce MADV_POPULATE_(READ|WRITE) to prefault page tables") and eb2faa513c24 ("mm/madvise: report SIGBUS as -EFAULT for MADV_POPULATE_(READ|WRITE)"), and in the man page proposal [1].
v2 -> v3:
- "util/oslib-posix: Let touch_all_pages() return an error" -- Added
- Added ACKs/RBs
- "util/oslib-posix: Support MADV_POPULATE_WRITE for os_mem_prealloc()" -- Set error code accordingly
v1 -> v2:
- "util/oslib-posix: Support MADV_POPULATE_WRITE for os_mem_prealloc()" -- Handle thread with no data to initialize
-- Always set use_madv_populate_write properly
-- Add comment regarding future fallocate() optimization
- "util/oslib-posix: Don't create too many threads with small memory or little pages"
-- Added
- "util/oslib-posix: Avoid creating a single thread with MADV_POPULATE_WRITE"
-- Added
- "util/oslib-posix: Support concurrent os_mem_prealloc() invocation" -- Add missing g_once_init_leave()
-- Move g_once_init_enter() to the place where it is actually needed
- "util/oslib-posix: Forward SIGBUS to MCE handler under Linux" -- Added
[1] https://lkml.kernel.org/r/[email protected]
David Hildenbrand (7):
util/oslib-posix: Let touch_all_pages() return an error util/oslib-posix: Support MADV_POPULATE_WRITE for os_mem_prealloc() util/oslib-posix: Introduce and use MemsetContext for
touch_all_pages()
util/oslib-posix: Don't create too many threads with small memory or
little pages
util/oslib-posix: Avoid creating a single thread with
MADV_POPULATE_WRITE
util/oslib-posix: Support concurrent os_mem_prealloc() invocation util/oslib-posix: Forward SIGBUS to MCE handler under Linux
include/qemu/osdep.h | 7 ++ softmmu/cpus.c | 4 + util/oslib-posix.c | 231 ++++++++++++++++++++++++++++++++----------- 3 files changed, 187 insertions(+), 55 deletions(-)
Signature · 1 line
2.31.1
PATCH v3 1/7 util/oslib-posix: Let touch_all_pages() return an error
Let's prepare touch_all_pages() for returning differing errors. Return an error from the thread and report the last processed error.
Translate SIGBUS to -EFAULT, as a SIGBUS can mean all different kind of things (memory error, read error, out of memory). When allocating memory fails via the current SIGBUS-based mechanism, we'll get:
os_mem_prealloc: preallocating memory failed: Bad address
--- util/oslib-posix.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-)
util/oslib-posix.c +16 -12
@@ -84,7 +84,6 @@ typedef struct MemsetThread MemsetThread;static MemsetThread *memset_thread;static int memset_num_threads; -static bool memset_thread_failed;static QemuMutex page_mutex;static QemuCond page_cond;
@@ -452,6 +451,7 @@ static void *do_touch_pages(void *arg) {MemsetThread *memset_args = (MemsetThread *)arg;sigset_t set, oldset; +int ret =0 ;/* * On Linux, the page faults from the loop below can cause mmap_sem
@@ -470,7 +470,7 @@ static void *do_touch_pages(void *arg)pthread_sigmask (SIG_UNBLOCK , &set, &oldset);if (sigsetjmp (memset_args->env,1 )) { - memset_thread_failed = true; + ret = -EFAULT ; }else {char *addr = memset_args->addr;size_t numpages = memset_args->numpages;
@@ -494,7 +494,7 @@ static void *do_touch_pages(void *arg) } }pthread_sigmask (SIG_SETMASK , &oldset,NULL ); -return NULL ; +return (void *)(uintptr_t )ret; }static inline int get_memset_num_threads (int smp_cpus)
@@ -509,13 +509,13 @@ static inline int get_memset_num_threads(int smp_cpus)return ret; } -static bool touch_all_pages (char *area,size_t hpagesize,size_t numpages, -int smp_cpus) +static int touch_all_pages (char *area,size_t hpagesize,size_t numpages, +int smp_cpus) {static gsize initialized =0 ;size_t numpages_per_thread, leftover; +int ret =0 , i =0 ;char *addr = area; -int i =0 ;if (g_once_init_enter (&initialized)) {qemu_mutex_init (&page_mutex);
@@ -523,7 +523,6 @@ static bool touch_all_pages(char *area, size_t hpagesize, size_t numpages,g_once_init_leave (&initialized,1 ); } - memset_thread_failed = false; threads_created_flag = false; memset_num_threads =get_memset_num_threads (smp_cpus); memset_thread =g_new0 (MemsetThread, memset_num_threads);
@@ -545,12 +544,16 @@ static bool touch_all_pages(char *area, size_t hpagesize, size_t numpages,qemu_mutex_unlock (&page_mutex);for (i =0 ; i < memset_num_threads; i++) { -qemu_thread_join (&memset_thread[i].pgthread); +int tmp = (uintptr_t )qemu_thread_join (&memset_thread[i].pgthread); + +if (tmp) { + ret = tmp; + } }g_free (memset_thread); memset_thread =NULL ; -return memset_thread_failed; +return ret; } void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
@@ -573,9 +576,10 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus, }/* touch pages simultaneously */ -if (touch_all_pages (area, hpagesize, numpages, smp_cpus)) { -error_setg (errp,"os_mem_prealloc: Insufficient free host memory " -"pages available to allocate guest RAM" ); + ret =touch_all_pages (area, hpagesize, numpages, smp_cpus); +if (ret) { +error_setg_errno (errp, -ret, +"os_mem_prealloc: preallocating memory failed" ); } ret =sigaction (SIGBUS , &oldact,NULL );
Signature · 1 line
2.31.1
Re: [PATCH v3 1/7] util/oslib-posix: Let touch_all_pages() return an error
Quoted 8 lines · David Hildenbrand
Let's prepare
touch_all_pages()for returning differing errors. Return an error from the thread and report the last processed error.Translate SIGBUS to
-EFAULT, as a SIGBUS can mean all different kind of things (memory error, read error, out of memory). When allocating memory fails via the current SIGBUS-based mechanism, we'll get:
os_mem_prealloc: preallocating memory failed: Bad address
Quoted 4 lines
Signed-off-by David Hildenbrand <[email protected]>--- util/oslib-posix.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-)
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH v3 2/7 util/oslib-posix: Support MADV_POPULATE_WRITE for os_mem_prealloc()
In reply to David HildenbrandLet's sense support and use it for preallocation. MADV_POPULATE_WRITE does not require a SIGBUS handler, doesn't actually touch page content, and avoids context switches; it is, therefore, faster and easier to handle than our current approach.
While MADV_POPULATE_WRITE is, in general, faster than manual prefaulting, and especially faster with 4k pages, there is still value in prefaulting using multiple threads to speed up preallocation.
More details on MADV_POPULATE_WRITE can be found in the Linux commits 4ca9b3859dac ("mm/madvise: introduce MADV_POPULATE_(READ|WRITE) to prefault page tables") and eb2faa513c24 ("mm/madvise: report SIGBUS as -EFAULT for MADV_POPULATE_(READ|WRITE)"), and in the man page proposal [1].
This resolves the TODO in do_touch_pages().
In the future, we might want to look into using fallocate(), eventually combined with MADV_POPULATE_READ, when dealing with shared file/fd mappings and not caring about memory bindings.
[1] https://lkml.kernel.org/r/[email protected]
--- include/qemu/osdep.h | 7 ++++ util/oslib-posix.c | 83 +++++++++++++++++++++++++++++++++----------- 2 files changed, 69 insertions(+), 21 deletions(-)
include/qemu/osdep.h +7 -0
@@ -471,6 +471,11 @@ static inline void qemu_cleanup_generic_vfree(void *p)#else #define QEMU_MADV_REMOVE QEMU_MADV_DONTNEED#endif +#ifdef MADV_POPULATE_WRITE +#define QEMU_MADV_POPULATE_WRITE MADV_POPULATE_WRITE +#else +#define QEMU_MADV_POPULATE_WRITE QEMU_MADV_INVALID +#endif #elif defined(CONFIG_POSIX_MADVISE)
@@ -484,6 +489,7 @@ static inline void qemu_cleanup_generic_vfree(void *p)#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID#define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID#define QEMU_MADV_REMOVE QEMU_MADV_DONTNEED +#define QEMU_MADV_POPULATE_WRITE QEMU_MADV_INVALID#else /* no-op */
@@ -497,6 +503,7 @@ static inline void qemu_cleanup_generic_vfree(void *p)#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID#define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID#define QEMU_MADV_REMOVE QEMU_MADV_INVALID +#define QEMU_MADV_POPULATE_WRITE QEMU_MADV_INVALID#endif
util/oslib-posix.c +62 -21
137 lines not on this page · raw message
Signature · 1 line
2.31.1
Quoted 20 lines · David Hildenbrand
Let's sense support and use it for preallocation.
MADV_POPULATE_WRITEdoes not require a SIGBUS handler, doesn't actually touch page content, and avoids context switches; it is, therefore, faster and easier to handle than our current approach.While
MADV_POPULATE_WRITEis, in general, faster than manual prefaulting, and especially faster with 4k pages, there is still value in prefaulting using multiple threads to speed up preallocation.More details on
MADV_POPULATE_WRITEcan be found in the Linux commits4ca9b3859dac("mm/madvise: introduceMADV_POPULATE_(READ|WRITE)to prefault page tables") andeb2faa513c24("mm/madvise: report SIGBUS as-EFAULTforMADV_POPULATE_(READ|WRITE)"), and in the man page proposal [1].This resolves the TODO in
do_touch_pages().In the future, we might want to look into using
fallocate(), eventually combined withMADV_POPULATE_READ, when dealing with shared file/fd mappings and not caring about memory bindings.
Quoted 8 lines
[1] https://lkml.kernel.org/r/[email protected]
Reviewed-by Pankaj Gupta <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- include/qemu/osdep.h | 7 ++++ util/oslib-posix.c | 83 +++++++++++++++++++++++++++++++++----------- 2 files changed, 69 insertions(+), 21 deletions(-)
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH v3 3/7 util/oslib-posix: Introduce and use MemsetContext for touch_all_pages()
In reply to David HildenbrandLet's minimize the number of global variables to prepare for os_mem_prealloc() getting called concurrently and make the code a bit easier to read.
The only consumer that really needs a global variable is the sigbus handler, which will require protection via a mutex in the future either way as we cannot concurrently mess with the SIGBUS handler.
--- util/oslib-posix.c | 73 +++++++++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 26 deletions(-)
util/oslib-posix.c +47 -26
@@ -73,21 +73,30 @@#define MAX_MEM_PREALLOC_THREAD_COUNT 16 +struct MemsetThread ; + +typedef struct MemsetContext { +bool all_threads_created; +bool any_thread_failed; +struct MemsetThread *threads; +int num_threads; +}MemsetContext ; +struct MemsetThread {char *addr;size_t numpages;size_t hpagesize;QemuThread pgthread;sigjmp_buf env; +MemsetContext *context; };typedef struct MemsetThread MemsetThread ; -static MemsetThread *memset_thread; -static int memset_num_threads; +/* used by sigbus_handler() */ +static MemsetContext *sigbus_memset_context;static QemuMutex page_mutex;static QemuCond page_cond; -static bool threads_created_flag;int qemu_get_thread_id (void ) {
@@ -438,10 +447,13 @@ const char *qemu_get_exec_dir(void)static void sigbus_handler (int signal) {int i; -if (memset_thread) { -for (i =0 ; i < memset_num_threads; i++) { -if (qemu_thread_is_self (&memset_thread[i].pgthread)) { -siglongjmp (memset_thread[i].env,1 ); + +if (sigbus_memset_context) { +for (i =0 ; i < sigbus_memset_context->num_threads; i++) { +MemsetThread *thread = &sigbus_memset_context->threads[i]; + +if (qemu_thread_is_self (&thread->pgthread)) { +siglongjmp (thread->env,1 ); } } }
@@ -459,7 +471,7 @@ static void *do_touch_pages(void *arg)* clearing until all threads have been created. */ qemu_mutex_lock (&page_mutex); -while (!threads_created_flag){ +while (!memset_args->context->all_threads_created) {qemu_cond_wait (&page_cond, &page_mutex); }qemu_mutex_unlock (&page_mutex);
@@ -502,7 +514,7 @@ static void *do_madv_populate_write_pages(void *arg)/* See do_touch_pages(). */ qemu_mutex_lock (&page_mutex); -while (!threads_created_flag) { +while (!memset_args->context->all_threads_created) {qemu_cond_wait (&page_cond, &page_mutex); }qemu_mutex_unlock (&page_mutex);
@@ -529,6 +541,9 @@ static int touch_all_pages(char *area, size_t hpagesize, size_t numpages, int smp_cpus, bool use_madv_populate_write) {static gsize initialized =0 ; +MemsetContext context = { + .num_threads =get_memset_num_threads (smp_cpus), + };size_t numpages_per_thread, leftover;void *(*touch_fn)(void *);int ret =0 , i =0 ;
@@ -546,35 +561,41 @@ static int touch_all_pages(char *area, size_t hpagesize, size_t numpages, touch_fn = do_touch_pages; } - threads_created_flag = false; - memset_num_threads =get_memset_num_threads (smp_cpus); - memset_thread =g_new0 (MemsetThread, memset_num_threads); - numpages_per_thread = numpages / memset_num_threads; - leftover = numpages % memset_num_threads; -for (i =0 ; i < memset_num_threads; i++) { - memset_thread[i].addr = addr; - memset_thread[i].numpages = numpages_per_thread + (i < leftover); - memset_thread[i].hpagesize = hpagesize; -qemu_thread_create (&memset_thread[i].pgthread,"touch_pages" , - touch_fn, &memset_thread[i], + context.threads =g_new0 (MemsetThread, context.num_threads); + numpages_per_thread = numpages / context.num_threads; + leftover = numpages % context.num_threads; +for (i =0 ; i < context.num_threads; i++) { + context.threads[i].addr = addr; + context.threads[i].numpages = numpages_per_thread + (i < leftover); + context.threads[i].hpagesize = hpagesize; + context.threads[i].context = &context; +qemu_thread_create (&context.threads[i].pgthread,"touch_pages" , + touch_fn, &context.threads[i],QEMU_THREAD_JOINABLE ); - addr += memset_thread[i].numpages * hpagesize; + addr += context.threads[i].numpages * hpagesize; + } + +if (!use_madv_populate_write) { + sigbus_memset_context = &context; }qemu_mutex_lock (&page_mutex); - threads_created_flag = true; + context.all_threads_created = true;qemu_cond_broadcast (&page_cond);qemu_mutex_unlock (&page_mutex); -for (i =0 ; i < memset_num_threads; i++) { -int tmp = (uintptr_t )qemu_thread_join (&memset_thread[i].pgthread); +for (i =0 ; i < context.num_threads; i++) { +int tmp = (uintptr_t )qemu_thread_join (&context.threads[i].pgthread);if (tmp) { ret = tmp; } } -g_free (memset_thread); - memset_thread =NULL ; + +if (!use_madv_populate_write) { + sigbus_memset_context =NULL ; + } +g_free (context.threads);return ret; }
Signature · 1 line
2.31.1
PATCH v3 4/7 util/oslib-posix: Don't create too many threads with small memory or little pages
In reply to David HildenbrandLet's limit the number of threads to something sane, especially that
- We don't have more threads than the number of pages we have
- We don't have threads that initialize small (< 64 MiB) memory
--- util/oslib-posix.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
util/oslib-posix.c +10 -2
@@ -40,6 +40,7 @@#include <libgen.h> #include "qemu/cutils.h" #include "qemu/compiler.h" +#include "qemu/units.h" #ifdef CONFIG_LINUX #include <sys/syscall.h>
@@ -525,7 +526,8 @@ static void *do_madv_populate_write_pages(void *arg)return (void *)(uintptr_t )ret; } -static inline int get_memset_num_threads (int smp_cpus) +static inline int get_memset_num_threads (size_t hpagesize,size_t numpages, +int smp_cpus) {long host_procs =sysconf (_SC_NPROCESSORS_ONLN);int ret =1 ;
@@ -533,6 +535,12 @@ static inline int get_memset_num_threads(int smp_cpus)if (host_procs >0 ) { ret =MIN (MIN (host_procs,MAX_MEM_PREALLOC_THREAD_COUNT ), smp_cpus); } + +/* Especially with gigantic pages, don't create more threads than pages. */ + ret =MIN (ret, numpages); +/* Don't start threads to prealloc comparatively little memory. */ + ret =MIN (ret,MAX (1 , hpagesize * numpages / (64 * MiB))); +/* In case sysconf() fails, we fall back to single threaded */ return ret; }
@@ -542,7 +550,7 @@ static int touch_all_pages(char *area, size_t hpagesize, size_t numpages, {static gsize initialized =0 ;MemsetContext context = { - .num_threads =get_memset_num_threads (smp_cpus), + .num_threads =get_memset_num_threads (hpagesize, numpages, smp_cpus), };size_t numpages_per_thread, leftover;void *(*touch_fn)(void *);
Signature · 1 line
2.31.1
Re: [PATCH v3 4/7] util/oslib-posix: Don't create too many threads with small memory or little pages
Quoted 10 lines · David Hildenbrand
Let's limit the number of threads to something sane, especially that
- We don't have more threads than the number of pages we have
- We don't have threads that initialize small (< 64 MiB) memoryReviewed-by Pankaj Gupta <[email protected]>Reviewed-by Dr. David Alan Gilbert <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- util/oslib-posix.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH v3 5/7 util/oslib-posix: Avoid creating a single thread with MADV_POPULATE_WRITE
In reply to David HildenbrandLet's simplify the case when we only want a single thread and don't have to mess with signal handlers.
--- util/oslib-posix.c | 8 ++++++++ 1 file changed, 8 insertions(+)
util/oslib-posix.c +8 -0
@@ -564,6 +564,14 @@ static int touch_all_pages(char *area, size_t hpagesize, size_t numpages, }if (use_madv_populate_write) { +/* Avoid creating a single thread for MADV_POPULATE_WRITE */ +if (context.num_threads ==1 ) { +if (qemu_madvise (area, hpagesize * numpages, +QEMU_MADV_POPULATE_WRITE )) { +return -errno; + } +return 0 ; + } touch_fn = do_madv_populate_write_pages; }else { touch_fn = do_touch_pages;
Signature · 1 line
2.31.1
Re: [PATCH v3 5/7] util/oslib-posix: Avoid creating a single thread with MADV_POPULATE_WRITE
Quoted 8 lines · David Hildenbrand
Let's simplify the case when we only want a single thread and don't have to mess with signal handlers.
Reviewed-by Pankaj Gupta <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- util/oslib-posix.c | 8 ++++++++ 1 file changed, 8 insertions(+)
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH v3 6/7 util/oslib-posix: Support concurrent os_mem_prealloc() invocation
In reply to David HildenbrandAdd a mutex to protect the SIGBUS case, as we cannot mess concurrently with the sigbus handler and we have to manage the global variable sigbus_memset_context. The MADV_POPULATE_WRITE path can run concurrently.
Note that page_mutex and page_cond are shared between concurrent invocations, which shouldn't be a problem.
This is a preparation for future virtio-mem prealloc code, which will call os_mem_prealloc() asynchronously from an iothread when handling guest requests.
Add a comment that messing with the SIGBUS handler is frowned upon and can result in problems we fortunately haven't seen so far. Note that forwarding signals to the already installed SIGBUS handler isn't clean either, as that one might modify the SIGBUS handler again.
--- util/oslib-posix.c | 9 +++++++++ 1 file changed, 9 insertions(+)
util/oslib-posix.c +9 -0
@@ -95,6 +95,7 @@ typedef struct MemsetThread MemsetThread;/* used by sigbus_handler() */ static MemsetContext *sigbus_memset_context; +static QemuMutex sigbus_mutex;static QemuMutex page_mutex;static QemuCond page_cond;
@@ -625,6 +626,7 @@ static bool madv_populate_write_possible(char *area, size_t pagesize)void os_mem_prealloc (int fd,char *area,size_t memory,int smp_cpus,Error **errp) { +static gsize initialized;int ret;struct sigaction act, oldact;size_t hpagesize =qemu_fd_getpagesize (fd);
@@ -638,6 +640,12 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus, use_madv_populate_write =madv_populate_write_possible (area, hpagesize);if (!use_madv_populate_write) { +if (g_once_init_enter (&initialized)) { +qemu_mutex_init (&sigbus_mutex); +g_once_init_leave (&initialized,1 ); + } + +qemu_mutex_lock (&sigbus_mutex);memset (&act,0 ,sizeof (act)); act.sa_handler = &sigbus_handler; act.sa_flags =0 ;
@@ -665,6 +673,7 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,perror ("os_mem_prealloc: failed to reinstall signal handler" );exit (1 ); } +qemu_mutex_unlock (&sigbus_mutex); } }
Signature · 1 line
2.31.1
Re: [PATCH v3 6/7] util/oslib-posix: Support concurrent os_mem_prealloc() invocation
Quoted 8 lines · David Hildenbrand
Add a mutex to protect the SIGBUS case, as we cannot mess concurrently with the sigbus handler and we have to manage the global variable
sigbus_memset_context. TheMADV_POPULATE_WRITEpath can run concurrently.Note that
page_mutexandpage_condare shared between concurrent invocations, which shouldn't be a problem.
Quoted 8 lines
This is a preparation for future virtio-mem prealloc code, which will call
os_mem_prealloc()asynchronously from an iothread when handling guest requests.Add a comment that messing with the SIGBUS handler is frowned upon and can result in problems we fortunately haven't seen so far. Note that forwarding signals to the already installed SIGBUS handler isn't clean either, as that one might modify the SIGBUS handler again.
Even with the mutex, messing with SIGBUS post-startup still isn't safe as we're clashing with SIGBUS usage in softmmu/cpus.c
IIUC, the virtio-mem prealloc code is something new that we've not shipped yet. With this in mind, how about we simply enforce that usage of this new feature is dependant on kernel support for MADV_POPULATE_WRITE ? If users want this feature they'll simply need to update to a modern kernel. This shouldn't break any existing deployed QEMU guests IIUC
Quoted 12 lines
Reviewed-by Pankaj Gupta <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- util/oslib-posix.c | 9 +++++++++ 1 file changed, 9 insertions(+)util/oslib-posix.c +0
-0
util/oslib-posix.c typedef struct MemsetThread MemsetThread; · line 100
Quoted 6 lines
util/oslib-posix.c +1
-0@@ -95,6 +95,7 @@ typedef struct MemsetThread MemsetThread;/* used by sigbus_handler() */ static MemsetContext *sigbus_memset_context; +static QemuMutex sigbus_mutex;static QemuMutex page_mutex;
b> static QemuCond page_cond;
Quoted 32 lines
util/oslib-posix.c +8
-0@@ -625,6 +626,7 @@ static bool madv_populate_write_possible(char *area, size_t pagesize)void os_mem_prealloc (int fd,char *area,size_t memory,int smp_cpus,Error **errp) { +static gsize initialized;int ret;struct sigaction act, oldact;size_t hpagesize =qemu_fd_getpagesize (fd);@@ -638,6 +640,12 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus, use_madv_populate_write =madv_populate_write_possible (area, hpagesize);if (!use_madv_populate_write) { +if (g_once_init_enter (&initialized)) { +qemu_mutex_init (&sigbus_mutex); +g_once_init_leave (&initialized,1 ); + } + +qemu_mutex_lock (&sigbus_mutex);memset (&act,0 ,sizeof (act)); act.sa_handler = &sigbus_handler; act.sa_flags =0 ;@@ -665,6 +673,7 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,perror ("os_mem_prealloc: failed to reinstall signal handler" );exit (1 ); } +qemu_mutex_unlock (&sigbus_mutex); } }Signature · 1 line
2.31.1
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Daniel P. Berrangé
Reviewed-byRe: [PATCH v3 6/7] util/oslib-posix: Support concurrent os_mem_prealloc() invocation
Quoted 21 lines · Daniel P. Berrangé
Quoted 8 lines · David Hildenbrand
Add a mutex to protect the SIGBUS case, as we cannot mess concurrently with the sigbus handler and we have to manage the global variable
sigbus_memset_context. TheMADV_POPULATE_WRITEpath can run concurrently.Note that
page_mutexandpage_condare shared between concurrent invocations, which shouldn't be a problem.Quoted 8 lines
This is a preparation for future virtio-mem prealloc code, which will call
os_mem_prealloc()asynchronously from an iothread when handling guest requests.Add a comment that messing with the SIGBUS handler is frowned upon and can result in problems we fortunately haven't seen so far. Note that forwarding signals to the already installed SIGBUS handler isn't clean either, as that one might modify the SIGBUS handler again.
Even with the mutex, messing with SIGBUS post-startup still isn't safe as we're clashing with SIGBUS usage in
softmmu/cpus.c
Quoted 6 lines
IIUC, the virtio-mem prealloc code is something new that we've not shipped yet. With this in mind, how about we simply enforce that usage of this new feature is dependant on kernel support for
MADV_POPULATE_WRITE? If users want this feature they'll simply need to update to a modern kernel. This shouldn't break any existing deployed QEMU guests IIUC
Oh, I should have read ahead to the next patch where you address this issue.
With that in mind I'm ok adding
Quoted 49 lines
Quoted 13 lines
Reviewed-by Pankaj Gupta <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- util/oslib-posix.c | 9 +++++++++ 1 file changed, 9 insertions(+)util/oslib-posix.c +0
-0util/oslib-posix.c typedef struct MemsetThread MemsetThread; · line 100
Quoted 6 lines
util/oslib-posix.c +1
-0@@ -95,6 +95,7 @@ typedef struct MemsetThread MemsetThread;/* used by sigbus_handler() */ static MemsetContext *sigbus_memset_context; +static QemuMutex sigbus_mutex;static QemuMutex page_mutex;b> static QemuCond
page_cond;Quoted 29 lines
util/oslib-posix.c +8
-0@@ -625,6 +626,7 @@ static bool madv_populate_write_possible(char *area, size_t pagesize)void os_mem_prealloc (int fd,char *area,size_t memory,int smp_cpus,Error **errp) { +static gsize initialized;int ret;struct sigaction act, oldact;size_t hpagesize =qemu_fd_getpagesize (fd);@@ -638,6 +640,12 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus, use_madv_populate_write =madv_populate_write_possible (area, hpagesize);if (!use_madv_populate_write) { +if (g_once_init_enter (&initialized)) { +qemu_mutex_init (&sigbus_mutex); +g_once_init_leave (&initialized,1 ); + } + +qemu_mutex_lock (&sigbus_mutex);memset (&act,0 ,sizeof (act)); act.sa_handler = &sigbus_handler; act.sa_flags =0 ;@@ -665,6 +673,7 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,perror ("os_mem_prealloc: failed to reinstall signal handler" );exit (1 ); } +qemu_mutex_unlock (&sigbus_mutex); } }
Quoted 12 lines
Quoted 3 lines
Signature · 1 line
2.31.1Regards,
DanielSignature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 6/7] util/oslib-posix: Support concurrent os_mem_prealloc() invocation
In reply to Daniel P. BerrangéQuoted 13 lines · Daniel P. Berrangé
Quoted 12 lines · David Hildenbrand
Add a mutex to protect the SIGBUS case, as we cannot mess concurrently with the sigbus handler and we have to manage the global variable
sigbus_memset_context. TheMADV_POPULATE_WRITEpath can run concurrently.Note that
page_mutexandpage_condare shared between concurrent invocations, which shouldn't be a problem.This is a preparation for future virtio-mem prealloc code, which will call
os_mem_prealloc()asynchronously from an iothread when handling guest requests.
Quoted 7 lines
Quoted 4 lines
Add a comment that messing with the SIGBUS handler is frowned upon and can result in problems we fortunately haven't seen so far. Note that forwarding signals to the already installed SIGBUS handler isn't clean either, as that one might modify the SIGBUS handler again.
Even with the mutex, messing with SIGBUS post-startup still isn't safe as we're clashing with SIGBUS usage in
softmmu/cpus.c
You raise a valid point, though: this node is stale (there is no such comment) and shall be removed :)
Thanks!
Signature · 3 lines
Thanks, David / dhildenb
PATCH v3 7/7 util/oslib-posix: Forward SIGBUS to MCE handler under Linux
In reply to David HildenbrandTemporarily modifying the SIGBUS handler is really nasty, as we might be unlucky and receive an MCE SIGBUS while having our handler registered. Unfortunately, there is no way around messing with SIGBUS when MADV_POPULATE_WRITE is not applicable or not around.
Let's forward SIGBUS that don't belong to us to the already registered handler and document the situation.
--- softmmu/cpus.c | 4 ++++ util/oslib-posix.c | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-)
softmmu/cpus.c +4 -0
@@ -352,6 +352,10 @@ static void qemu_init_sigbus(void) {struct sigaction action; +/* +* ALERT: when modifying this, take care that SIGBUS forwarding in +* os_mem_prealloc() will continue working as expected. +*/ memset (&action,0 ,sizeof (action)); action.sa_flags =SA_SIGINFO ; action.sa_sigaction = sigbus_handler;
util/oslib-posix.c +33 -3
@@ -95,6 +95,7 @@ typedef struct MemsetThread MemsetThread;/* used by sigbus_handler() */ static MemsetContext *sigbus_memset_context; +struct sigaction sigbus_oldact;static QemuMutex sigbus_mutex;static QemuMutex page_mutex;
@@ -446,7 +447,11 @@ const char *qemu_get_exec_dir(void)return exec_dir; } +#ifdef CONFIG_LINUX +static void sigbus_handler(int signal, siginfo_t *siginfo, void *ctx) +#else /* CONFIG_LINUX */ static void sigbus_handler(int signal) +#endif /* CONFIG_LINUX */ {int i;
@@ -459,6 +464,26 @@ static void sigbus_handler(int signal) } } } + +#ifdef CONFIG_LINUX +/* +* We assume that the MCE SIGBUS handler could have been registered. We +* should never receive BUS_MCEERR_AO on any of our threads, but only on +* the main thread registered for PR_MCE_KILL_EARLY. Further, we should not +* receive BUS_MCEERR_AR triggered by action of other threads on one of +* our threads. So, no need to check for unrelated SIGBUS when seeing one +* for our threads. +* +* We will forward to the MCE handler, which will either handle the SIGBUS +* or reinstall the default SIGBUS handler and reraise the SIGBUS. The +* default SIGBUS handler will crash the process, so we don't care. +*/ +if (sigbus_oldact.sa_flags &SA_SIGINFO ) { + sigbus_oldact.sa_sigaction (signal, siginfo, ctx); +return ; + } +#endif /* CONFIG_LINUX */ +warn_report ("os_mem_prealloc: unrelated SIGBUS detected and ignored" ); }static void *do_touch_pages (void *arg)
@@ -628,10 +653,10 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus, {static gsize initialized;int ret; -struct sigaction act, oldact;size_t hpagesize =qemu_fd_getpagesize (fd);size_t numpages =DIV_ROUND_UP (memory, hpagesize);bool use_madv_populate_write; +struct sigaction act;/* * Sense on every invocation, as MADV_POPULATE_WRITE cannot be used for
@@ -647,10 +672,15 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,qemu_mutex_lock (&sigbus_mutex);memset (&act,0 ,sizeof (act)); +#ifdef CONFIG_LINUX + act.sa_sigaction = &sigbus_handler; + act.sa_flags =SA_SIGINFO ; +#else /* CONFIG_LINUX */ act.sa_handler = &sigbus_handler; act.sa_flags =0 ; +#endif /* CONFIG_LINUX */ - ret =sigaction (SIGBUS , &act, &oldact); + ret =sigaction (SIGBUS , &act, &sigbus_oldact);if (ret) {error_setg_errno (errp, errno,"os_mem_prealloc: failed to install signal handler" );
@@ -667,7 +697,7 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus, }if (!use_madv_populate_write) { - ret =sigaction (SIGBUS , &oldact,NULL ); + ret =sigaction (SIGBUS , &sigbus_oldact,NULL );if (ret) {/* Terminate QEMU since it can't recover from error */ perror ("os_mem_prealloc: failed to reinstall signal handler" );
Signature · 1 line
2.31.1
Re: [PATCH v3 7/7] util/oslib-posix: Forward SIGBUS to MCE handler under Linux
Quoted 5 lines · David Hildenbrand
Temporarily modifying the SIGBUS handler is really nasty, as we might be unlucky and receive an MCE SIGBUS while having our handler registered. Unfortunately, there is no way around messing with SIGBUS when
MADV_POPULATE_WRITEis not applicable or not around.
Quoted 8 lines
Let's forward SIGBUS that don't belong to us to the already registered handler and document the situation.
Signed-off-by David Hildenbrand <[email protected]>--- softmmu/cpus.c | 4 ++++ util/oslib-posix.c | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-)
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 7/7] util/oslib-posix: Forward SIGBUS to MCE handler under Linux
Quoted 9 lines · Daniel P. Berrangé
Quoted 8 lines · David Hildenbrand
Temporarily modifying the SIGBUS handler is really nasty, as we might be unlucky and receive an MCE SIGBUS while having our handler registered. Unfortunately, there is no way around messing with SIGBUS when
MADV_POPULATE_WRITEis not applicable or not around.Let's forward SIGBUS that don't belong to us to the already registered handler and document the situation.
Quoted 7 lines
Quoted 5 lines
Signed-off-by David Hildenbrand <[email protected]>--- softmmu/cpus.c | 4 ++++ util/oslib-posix.c | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-)Reviewed-by Daniel P. Berrangé <[email protected]>
Thanks Daniel!
Signature · 3 lines
Thanks, David / dhildenb
Quoted 7 lines · David Hildenbrand
#1 is a preparation for improved error reporting, #2 adds support for
MADV_POPULATE_WRITE, #3 cleans up the code to avoid global variables and prepare for concurrency, #4 and #5 optimize thread handling, #6 makesos_mem_prealloc()safe to be called from multiple threads concurrently and #7 makes the SIGBUS handler coexist cleanly with the MCE SIGBUS handler under Linux.
Quoted 5 lines
Details regarding
MADV_POPULATE_WRITEcan be found in introducing upstream Linux commits4ca9b3859dac("mm/madvise: introduceMADV_POPULATE_(READ|WRITE)to prefault page tables") andeb2faa513c24("mm/madvise: report SIGBUS as-EFAULTforMADV_POPULATE_(READ|WRITE)"), and in the man page proposal [1].
Gentle ping.
Signature · 3 lines
Thanks, David / dhildenb