fix qemu exit on memory hotplug when allocation fails at prealloc time
PATCH 1/1 fix qemu exit on memory hotplug when allocation fails at prealloc time
When adding hostmem backend at runtime, QEMU might exit with error:
"os_mem_prealloc: Insufficient free host memory pages available to allocate guest RAM"
It happens due to os_mem_prealloc() not handling errors gracefully.
Fix it by passing errp argument so that os_mem_prealloc() could report error to callers and undo performed allocation when os_mem_prealloc() fails.
--- include/qemu/osdep.h | 2 +- backends/hostmem.c | 18 ++++++++++++++---- exec.c | 10 ++++++++-- util/oslib-posix.c | 25 ++++++++++++------------- util/oslib-win32.c | 2 +- 5 files changed, 36 insertions(+), 21 deletions(-)
include/qemu/osdep.h +1 -1
@@ -379,7 +379,7 @@ unsigned long qemu_getauxval(unsigned long type);void qemu_set_tty_echo (int fd,bool echo); -void os_mem_prealloc (int fd,char *area,size_t sz); +void os_mem_prealloc (int fd,char *area,size_t sz,Error **errp);int qemu_read_password (char *buf,int buf_size);
backends/hostmem.c +14 -4
@@ -203,6 +203,7 @@ static bool host_memory_backend_get_prealloc(Object *obj, Error **errp)static void host_memory_backend_set_prealloc (Object *obj,bool value,Error **errp) { +Error *local_err =NULL ;HostMemoryBackend *backend =MEMORY_BACKEND (obj);if (backend->force_prealloc) {
@@ -223,7 +224,11 @@ static void host_memory_backend_set_prealloc(Object *obj, bool value,void *ptr =memory_region_get_ram_ptr (&backend->mr);uint64_t sz =memory_region_size (&backend->mr); -os_mem_prealloc (fd, ptr, sz); +os_mem_prealloc (fd, ptr, sz, &local_err); +if (local_err) { +error_propagate (errp, local_err); +return ; + } backend->prealloc = true; } }
@@ -286,8 +291,7 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)if (bc->alloc) { bc->alloc (backend, &local_err);if (local_err) { -error_propagate (errp, local_err); -return ; + goto out; } ptr =memory_region_get_ram_ptr (&backend->mr);
@@ -343,9 +347,15 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)* specified NUMA policy in place. */ if (backend->prealloc) { -os_mem_prealloc (memory_region_get_fd (&backend->mr), ptr, sz); +os_mem_prealloc (memory_region_get_fd (&backend->mr), ptr, sz, + &local_err); +if (local_err) { + goto out; + } } } +out: +error_propagate (errp, local_err); } static bool
exec.c +8 -2
@@ -1268,7 +1268,7 @@ static void *file_ram_alloc(RAMBlock *block,char *filename;char *sanitized_name;char *c; -void *area; +void *area =MAP_FAILED ;int fd =-1 ;int64_t page_size;
@@ -1356,13 +1356,19 @@ static void *file_ram_alloc(RAMBlock *block, }if (mem_prealloc) { -os_mem_prealloc (fd, area, memory); +os_mem_prealloc (fd, area, memory, errp); +if (errp && *errp) { + goto error; + } } block->fd = fd;return area; error: +if (area !=MAP_FAILED ) { +qemu_ram_munmap (area, memory); + }if (unlink_on_error) {unlink (path); }
util/oslib-posix.c +12 -13
@@ -318,7 +318,7 @@ static void sigbus_handler(int signal)siglongjmp (sigjump,1 ); } -void os_mem_prealloc (int fd,char *area,size_t memory) +void os_mem_prealloc (int fd,char *area,size_t memory,Error **errp) {int ret;struct sigaction act, oldact;
@@ -330,8 +330,9 @@ void os_mem_prealloc(int fd, char *area, size_t memory) ret =sigaction (SIGBUS , &act, &oldact);if (ret) { -perror ("os_mem_prealloc: failed to install signal handler" ); -exit (1 ); +error_setg_errno (errp, errno, +"os_mem_prealloc: failed to install signal handler" ); +return ; }/* unblock SIGBUS */
@@ -340,9 +341,8 @@ void os_mem_prealloc(int fd, char *area, size_t memory)pthread_sigmask (SIG_UNBLOCK , &set, &oldset);if (sigsetjmp (sigjump,1 )) { -fprintf (stderr,"os_mem_prealloc: Insufficient free host memory " -"pages available to allocate guest RAM\n" ); -exit (1 ); +error_setg (errp,"os_mem_prealloc: Insufficient free host memory " +"pages available to allocate guest RAM\n" ); }else {int i;size_t hpagesize =qemu_fd_getpagesize (fd);
@@ -352,15 +352,14 @@ void os_mem_prealloc(int fd, char *area, size_t memory)for (i =0 ; i < numpages; i++) {memset (area + (hpagesize * i),0 ,1 ); } + } - ret =sigaction (SIGBUS , &oldact,NULL ); -if (ret) { -perror ("os_mem_prealloc: failed to reinstall signal handler" ); -exit (1 ); - } - -pthread_sigmask (SIG_SETMASK , &oldset,NULL ); + ret =sigaction (SIGBUS , &oldact,NULL ); +if (ret) { +perror ("os_mem_prealloc: failed to reinstall signal handler" ); +exit (1 ); } +pthread_sigmask (SIG_SETMASK , &oldset,NULL ); }
util/oslib-win32.c +1 -1
@@ -539,7 +539,7 @@ int getpagesize(void)return system_info.dwPageSize; } -void os_mem_prealloc (int fd,char *area,size_t memory) +void os_mem_prealloc (int fd,char *area,size_t memory,Error **errp) {int i;size_t pagesize =getpagesize ();
Signature · 1 line
2.7.4
Quoted 57 lines · Igor Mammedov
When adding hostmem backend at runtime, QEMU might exit with error:
"os_mem_prealloc: Insufficient free host memory pages available to allocate guest RAM"It happens due to
os_mem_prealloc()not handling errors gracefully.Fix it by passing errp argument so that
os_mem_prealloc()could report error to callers and undo performed allocation whenos_mem_prealloc()fails.Signed-off-by Igor Mammedov <[email protected]>--- include/qemu/osdep.h | 2 +- backends/hostmem.c | 18 ++++++++++++++---- exec.c | 10 ++++++++-- util/oslib-posix.c | 25 ++++++++++++------------- util/oslib-win32.c | 2 +- 5 files changed, 36 insertions(+), 21 deletions(-)include/qemu/osdep.h +1
-1@@ -379,7 +379,7 @@ unsigned long qemu_getauxval(unsigned long type);void qemu_set_tty_echo (int fd,bool echo); -void os_mem_prealloc (int fd,char *area,size_t sz); +void os_mem_prealloc (int fd,char *area,size_t sz,Error **errp);int qemu_read_password (char *buf,int buf_size);backends/hostmem.c +6
-1@@ -203,6 +203,7 @@ static bool host_memory_backend_get_prealloc(Object *obj, Error **errp)static void host_memory_backend_set_prealloc (Object *obj,bool value,Error **errp) { +Error *local_err =NULL ;HostMemoryBackend *backend =MEMORY_BACKEND (obj);if (backend->force_prealloc) {@@ -223,7 +224,11 @@ static void host_memory_backend_set_prealloc(Object *obj, bool value,void *ptr =memory_region_get_ram_ptr (&backend->mr);uint64_t sz =memory_region_size (&backend->mr); -os_mem_prealloc (fd, ptr, sz); +os_mem_prealloc (fd, ptr, sz, &local_err); +if (local_err) { +error_propagate (errp, local_err); +return ; + } backend->prealloc = true; } }
backends/hostmem.c host_memory_backend_memory_complete(UserCreatable *uc, Error **errp) · line 294
Quoted 6 lines
backends/hostmem.c +1
-2@@ -286,8 +291,7 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)if (bc->alloc) { bc->alloc (backend, &local_err);if (local_err) { -error_propagate (errp, local_err); -return ; + goto out;
I'd leave the tail merging optimization to the compiler, i.e. don't touch the code here, and ...
backends/hostmem.c host_memory_backend_memory_complete(UserCreatable *uc, Error **errp) · line 353
Quoted 12 lines
backends/hostmem.c +4
-1} ptr =memory_region_get_ram_ptr (&backend->mr);@@ -343,9 +347,15 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)* specified NUMA policy in place. */ if (backend->prealloc) { -os_mem_prealloc (memory_region_get_fd (&backend->mr), ptr, sz); +os_mem_prealloc (memory_region_get_fd (&backend->mr), ptr, sz, + &local_err); +if (local_err) { + goto out;
... have the obvious error_propagate() + return here. Matter of taste, between you and the maintainer. Except there is none. Inexcusable for a file created in 2014. Suggest you appoint yourself.
Quoted 86 lines
backends/hostmem.c +3
-0+ } } } +out: +error_propagate (errp, local_err); } static boolexec.c +8
-2@@ -1268,7 +1268,7 @@ static void *file_ram_alloc(RAMBlock *block,char *filename;char *sanitized_name;char *c; -void *area; +void *area =MAP_FAILED ;int fd =-1 ;int64_t page_size;@@ -1356,13 +1356,19 @@ static void *file_ram_alloc(RAMBlock *block, }if (mem_prealloc) { -os_mem_prealloc (fd, area, memory); +os_mem_prealloc (fd, area, memory, errp); +if (errp && *errp) { + goto error; + } } block->fd = fd;return area; error: +if (area !=MAP_FAILED ) { +qemu_ram_munmap (area, memory); + }if (unlink_on_error) {unlink (path); }util/oslib-posix.c +7
-7@@ -318,7 +318,7 @@ static void sigbus_handler(int signal)siglongjmp (sigjump,1 ); } -void os_mem_prealloc (int fd,char *area,size_t memory) +void os_mem_prealloc (int fd,char *area,size_t memory,Error **errp) {int ret;struct sigaction act, oldact;@@ -330,8 +330,9 @@ void os_mem_prealloc(int fd, char *area, size_t memory) ret =sigaction (SIGBUS , &act, &oldact);if (ret) { -perror ("os_mem_prealloc: failed to install signal handler" ); -exit (1 ); +error_setg_errno (errp, errno, +"os_mem_prealloc: failed to install signal handler" ); +return ; }/* unblock SIGBUS */ @@ -340,9 +341,8 @@ void os_mem_prealloc(int fd, char *area, size_t memory)pthread_sigmask (SIG_UNBLOCK , &set, &oldset);if (sigsetjmp (sigjump,1 )) { -fprintf (stderr,"os_mem_prealloc: Insufficient free host memory " -"pages available to allocate guest RAM\n" ); -exit (1 ); +error_setg (errp,"os_mem_prealloc: Insufficient free host memory " +"pages available to allocate guest RAM\n" ); }else {int i;size_t hpagesize =qemu_fd_getpagesize (fd);@@ -352,15 +352,14 @@ void os_mem_prealloc(int fd, char *area, size_t memory)for (i =0 ; i < numpages; i++) {memset (area + (hpagesize * i),0 ,1 ); } + } - ret =sigaction (SIGBUS , &oldact,NULL );
util/oslib-posix.c void os_mem_prealloc(int fd, char *area, size_t memory) · line 360
Quoted 10 lines
util/oslib-posix.c +4
-6-if (ret) { -perror ("os_mem_prealloc: failed to reinstall signal handler" ); -exit (1 ); - } - -pthread_sigmask (SIG_SETMASK , &oldset,NULL ); + ret =sigaction (SIGBUS , &oldact,NULL ); +if (ret) { +perror ("os_mem_prealloc: failed to reinstall signal handler" ); +exit (1 );
I guess you're keeping the exit() here, because you can't recover cleanly from this error. I should never happen anyway. Worth a comment, I think.
Quoted 10 lines
util/oslib-posix.c +1
-0} +pthread_sigmask (SIG_SETMASK , &oldset,NULL ); }util/oslib-win32.c +0
-0
util/oslib-win32.c int getpagesize(void) · line 545
Quoted 8 lines
util/oslib-win32.c +1
-1@@ -539,7 +539,7 @@ int getpagesize(void)return system_info.dwPageSize; } -void os_mem_prealloc (int fd,char *area,size_t memory) +void os_mem_prealloc (int fd,char *area,size_t memory,Error **errp) {int i;size_t pagesize =getpagesize ();
Quoted 3 lines · Markus Armbruster
... have the obvious
error_propagate()+ return here. Matter of taste, between you and the maintainer. Except there is none. Inexcusable for a file created in 2014. Suggest you appoint yourself.
For now I've queued the patch, but I suggest that memory backends go together with numa.c.
Paolo
Quoted 6 lines · On Thu, 21 Jul 2016 11:05:05 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 3 lines · Markus Armbruster
... have the obvious
error_propagate()+ return here. Matter of taste, between you and the maintainer. Except there is none. Inexcusable for a file created in 2014. Suggest you appoint yourself.For now I've queued the patch
thanks
Quoted 2 lines
, but I suggest that memory backends go
together withnuma.c.
Do you mean via Eduardo tree since here is numa maintainer?
Quoted 3 lines
Paolo
Quoted 6 lines
Quoted 6 lines · Markus Armbruster
Quoted 3 lines
... have the obvious
error_propagate()+ return here. Matter of taste, between you and the maintainer. Except there is none. Inexcusable for a file created in 2014. Suggest you appoint yourself.
Quoted 6 lines
Quoted 1 line
For now I've queued the patch
thanks
Quoted 2 lines
, but I suggest that memory backends go
together withnuma.c.Do you mean via Eduardo tree since here is numa maintainer?
Yes, for the future.
Paolo
Quoted 6 lines · Paolo Bonzini
Quoted 6 lines
Quoted 6 lines · Markus Armbruster
Quoted 3 lines
... have the obvious
error_propagate()+ return here. Matter of taste, between you and the maintainer. Except there is none. Inexcusable for a file created in 2014. Suggest you appoint yourself.
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
For now I've queued the patch
thanks
Quoted 2 lines
, but I suggest that memory backends go
together withnuma.c.Do you mean via Eduardo tree since here is numa maintainer?
Yes, for the future.
Works for me. I will submit a MAINTAINERS patch later, making hostmem*.c part of the NUMA section.
Signature · 1 line
Eduardo
Quoted 1 line · On Thu, 21 Jul 2016 10:36:40 +0200 Markus Armbruster <[email protected]> wrote:
Igor Mammedov <[email protected]> writes:
[...]
Quoted 13 lines
backends/hostmem.c host_memory_backend_memory_complete(UserCreatable *uc, Error **errp) · line 294
Quoted 7 lines
backends/hostmem.c +1
-2@@ -286,8 +291,7 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)if (bc->alloc) { bc->alloc (backend, &local_err);if (local_err) { -error_propagate (errp, local_err); -return ; + goto out;I'd leave the tail merging optimization to the compiler, i.e. don't touch the code here, and ...
Quoted 2 lines
(quoted diff) +0
-0}
Quoted 16 lines
backends/hostmem.c host_memory_backend_memory_complete(UserCreatable *uc, Error **errp) · line 353
Quoted 10 lines
backends/hostmem.c +4
-1ptr =memory_region_get_ram_ptr (&backend->mr);@@ -343,9 +347,15 @@ host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)* specified NUMA policy in place. */ if (backend->prealloc) { -os_mem_prealloc (memory_region_get_fd (&backend->mr), ptr, sz); +os_mem_prealloc (memory_region_get_fd (&backend->mr), ptr, sz, + &local_err); +if (local_err) { + goto out;... have the obvious
error_propagate()+ return here. Matter of taste, between you and the maintainer. Except there is none. Inexcusable for a file created in 2014. Suggest you appoint yourself.Quoted 1 line
(quoted diff) +1
-0+ }
I might if Eduardo declines or I could be and additional one.
wrt consolidating error_propagate(), it's what we were doing in target-i386/cpu.c any time we touched similar code pathes to reduce code duplication.
Quoted 5 lines
Quoted 5 lines
backends/hostmem.c +2
-0} } +out: +error_propagate (errp, local_err); }
[...]
Quoted 6 lines
Quoted 6 lines
util/oslib-posix.c +1
-0@@ -352,15 +352,14 @@ void os_mem_prealloc(int fd, char *area, size_t memory)for (i =0 ; i < numpages; i++) {memset (area + (hpagesize * i),0 ,1 ); } + }
Quoted 15 lines
util/oslib-posix.c void os_mem_prealloc(int fd, char *area, size_t memory) · line 360
Quoted 11 lines
util/oslib-posix.c +4
-7- ret =sigaction (SIGBUS , &oldact,NULL ); -if (ret) { -perror ("os_mem_prealloc: failed to reinstall signal handler" ); -exit (1 ); - } - -pthread_sigmask (SIG_SETMASK , &oldset,NULL ); + ret =sigaction (SIGBUS , &oldact,NULL ); +if (ret) { +perror ("os_mem_prealloc: failed to reinstall signal handler" ); +exit (1 );I guess you're keeping the
exit()here, because you can't recover cleanly from this error. I should never happen anyway. Worth a comment, I think.
I didn't added comment because it's obvious that it's not possible to recover and also because it's just the same old code that haven't had a comment to begin with (probably also due to its obviousness)
Quoted 13 lines
Quoted 13 lines
util/oslib-posix.c +1
-0} +pthread_sigmask (SIG_SETMASK , &oldset,NULL ); }util/oslib-win32.c +0
-0@@ -539,7 +539,7 @@ int getpagesize(void)return system_info.dwPageSize; }
Quoted 7 lines
util/oslib-win32.c int getpagesize(void) · line 545
Quoted 5 lines
util/oslib-win32.c +1
-1-void os_mem_prealloc (int fd,char *area,size_t memory) +void os_mem_prealloc (int fd,char *area,size_t memory,Error **errp) {int i;size_t pagesize =getpagesize ();Reviewed-by Markus Armbruster <[email protected]>
Thanks
Quoted 4 lines · Igor Mammedov
Quoted 1 line · On Thu, 21 Jul 2016 10:36:40 +0200 Markus Armbruster <[email protected]> wrote:
Igor Mammedov <[email protected]> writes:
[...]
Quoted 21 lines
Quoted 6 lines
Quoted 6 lines
util/oslib-posix.c +1
-0@@ -352,15 +352,14 @@ void os_mem_prealloc(int fd, char *area, size_t memory)for (i =0 ; i < numpages; i++) {memset (area + (hpagesize * i),0 ,1 ); } + }Quoted 15 lines
util/oslib-posix.c void os_mem_prealloc(int fd, char *area, size_t memory) · line 360
Quoted 11 lines
util/oslib-posix.c +4
-7- ret =sigaction (SIGBUS , &oldact,NULL ); -if (ret) { -perror ("os_mem_prealloc: failed to reinstall signal handler" ); -exit (1 ); - } - -pthread_sigmask (SIG_SETMASK , &oldset,NULL ); + ret =sigaction (SIGBUS , &oldact,NULL ); +if (ret) { +perror ("os_mem_prealloc: failed to reinstall signal handler" ); +exit (1 );I guess you're keeping the
exit()here, because you can't recover cleanly from this error. I should never happen anyway. Worth a comment, I think.
Quoted 3 lines
I didn't added comment because it's obvious that it's not possible to recover and also because it's just the same old code that haven't had a comment to begin with (probably also due to its obviousness)
It's obvious enough once you read closely enough to see what the failing code is doing.
The old code uses perror() + exit() consistently. The new code mixes Error with them, which is an anti-pattern. That's okay, the exit() is perfectly fine here. The issue for me is that the anti-pattern triggers the WTF-detector at a glance, but then I have to read closely to see #1 it's intentional, and #2 it's okay. That wasn't the case before.
[...]
Quoted 35 lines · On Fri, 22 Jul 2016 18:19:44 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 4 lines · Igor Mammedov
Quoted 1 line · On Thu, 21 Jul 2016 10:36:40 +0200 Markus Armbruster <[email protected]> wrote:
Igor Mammedov <[email protected]> writes:
[...]
Quoted 21 lines
Quoted 6 lines
Quoted 6 lines
util/oslib-posix.c +1
-0@@ -352,15 +352,14 @@ void os_mem_prealloc(int fd, char *area, size_t memory)for (i =0 ; i < numpages; i++) {memset (area + (hpagesize * i),0 ,1 ); } + }Quoted 15 lines
util/oslib-posix.c void os_mem_prealloc(int fd, char *area, size_t memory) · line 360
Quoted 11 lines
util/oslib-posix.c +4
-7- ret =sigaction (SIGBUS , &oldact,NULL ); -if (ret) { -perror ("os_mem_prealloc: failed to reinstall signal handler" ); -exit (1 ); - } - -pthread_sigmask (SIG_SETMASK , &oldset,NULL ); + ret =sigaction (SIGBUS , &oldact,NULL ); +if (ret) { +perror ("os_mem_prealloc: failed to reinstall signal handler" ); +exit (1 );I guess you're keeping the
exit()here, because you can't recover cleanly from this error. I should never happen anyway. Worth a comment, I think.Quoted 3 lines
I didn't added comment because it's obvious that it's not possible to recover and also because it's just the same old code that haven't had a comment to begin with (probably also due to its obviousness)
It's obvious enough once you read closely enough to see what the failing code is doing.
Quoted 7 lines
The old code uses
perror()+exit()consistently. The new code mixes Error with them, which is an anti-pattern. That's okay, theexit()is perfectly fine here. The issue for me is that the anti-pattern triggers the WTF-detector at a glance, but then I have to read closely to see #1 it's intentional, and #2 it's okay. That wasn't the case before.[...]
I'll post a patch to add comment on top of this one.
PATCH 1/1 fixup! fix qemu exit on memory hotplug when allocation fails at prealloc time
In reply to Igor Mammedov---
- add comment o exit(1) path
--- util/oslib-posix.c | 1 + 1 file changed, 1 insertion(+)
util/oslib-posix.c +1 -0
@@ -356,6 +356,7 @@ void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp) ret =sigaction (SIGBUS , &oldact,NULL );if (ret) { +/* Terminate QEMU since it can't recover from error */ perror ("os_mem_prealloc: failed to reinstall signal handler" );exit (1 ); }
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH] fixup! fix qemu exit on memory hotplug when allocation fails at prealloc time
Quoted 12 lines · Igor Mammedov
Signed-off-by Igor Mammedov <[email protected]>---
- add comment o exit(1) path--- util/oslib-posix.c | 1 + 1 file changed, 1 insertion(+)util/oslib-posix.c +0
-0
util/oslib-posix.c void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp) · line 362
Quoted 7 lines
util/oslib-posix.c +1
-0@@ -356,6 +356,7 @@ void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp) ret =sigaction (SIGBUS , &oldact,NULL );if (ret) { +/* Terminate QEMU since it can't recover from error */ perror ("os_mem_prealloc: failed to reinstall signal handler" );exit (1 ); }
Thanks! My R-by stands, of course.