coroutine: mmap stack memory and stack size
v4 cover coroutine: mmap stack memory and stack size
I decided to split this from the rest of the Qemu RSS usage series as it contains the more or less non contentious patches.
I omitted the MAP_GROWSDOWN flag in mmap as we are not 100% sure which side effects it has.
I kept the guard page which is now nicely makes the stacks visible in smaps. The old version of the relevent patch lacked the MAP_FIXED flag in the second call to mmap.
The last patch which reduces the stack size of coroutines to 64kB may be omitted if its found to risky.
v3->v4:
- Patch 1: add a static function to adjust the stack size [Richard]
- Patch 1: round up the stack size to multiple of the pagesize.
v2->v3:
- Patch 1,6: adjusted commit message to mention the guard page [Markus]
v1->v2:
- Patch 1: added an architecture dependend guard page [Richard]
- Patch 1: avoid stacks smaller than _SC_THREAD_STACK_MIN [Richard]
- Patch 1: use mmap+mprotect instead of mmap+mmap [Richard]
- Patch 5: u_int32_t -> uint32_t [Richard]
- Patch 5: only available if stack grows down
Peter Lieven (6):
oslib-posix: add helpers for stack alloc and free
coroutine: add a macro for the coroutine stack size
coroutine-ucontext: use helper for allocating stack memory
coroutine-sigaltstack: use helper for allocating stack memory
oslib-posix: add a configure switch to debug stack usage
coroutine: reduce stack size to 64kB
configure | 19 +++++++++++ include/qemu/coroutine_int.h | 2 ++ include/sysemu/os-posix.h | 23 +++++++++++++ util/coroutine-sigaltstack.c | 7 ++-- util/coroutine-ucontext.c | 9 +++-- util/coroutine-win32.c | 2 +- util/oslib-posix.c | 81 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 133 insertions(+), 10 deletions(-)
Signature · 1 line
1.9.1
PATCH v4 1/6 oslib-posix: add helpers for stack alloc and free
the allocated stack will be adjusted to the minimum supported stack size by the OS and rounded up to be a multiple of the system pagesize. Additionally an architecture dependent guard page is added to the stack to catch stack overflows.
--- include/sysemu/os-posix.h | 23 +++++++++++++++++++++++ util/oslib-posix.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)
include/sysemu/os-posix.h +23 -0
@@ -60,4 +60,27 @@ int qemu_utimens(const char *path, const qemu_timespec *times);bool is_daemonized (void ); +/** +* qemu_alloc_stack: +* @sz: size of required stack in bytes +* +* Allocate memory that can be used as a stack, for instance for +* coroutines. If the memory cannot be allocated, this function +* will abort (like g_malloc()). +* +* The allocated stack must be freed with qemu_free_stack(). +* +* Returns: pointer to (the lowest address of) the stack memory. +*/ +void *qemu_alloc_stack (size_t sz); + +/** +* qemu_free_stack: +* @stack: stack to free +* @sz: size of stack in bytes +* +* Free a stack allocated via qemu_alloc_stack(). +*/ +void qemu_free_stack (void *stack,size_t sz); +#endif
util/oslib-posix.c +44 -0
47 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v4 6/6 coroutine: reduce stack size to 64kB
In reply to Peter Lievenevaluation with the recently introduced maximum stack usage monitoring revealed that the actual used stack size was never above 4kB so allocating 1MB stack for each coroutine is a lot of wasted memory. So reduce the stack size to 64kB which should still give enough head room. The guard page added in qemu_alloc_stack will catch a potential stack overflow introduced by this commit.
--- include/qemu/coroutine_int.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/qemu/coroutine_int.h +1 -1
@@ -28,7 +28,7 @@#include "qemu/queue.h" #include "qemu/coroutine.h" -#define COROUTINE_STACK_SIZE (1 << 20) +#define COROUTINE_STACK_SIZE (1 << 16) typedef enum { COROUTINE_YIELD = 1,
Signature · 1 line
1.9.1
PATCH v4 2/6 coroutine: add a macro for the coroutine stack size
In reply to Peter Lieven--- include/qemu/coroutine_int.h | 2 ++ util/coroutine-sigaltstack.c | 2 +- util/coroutine-ucontext.c | 2 +- util/coroutine-win32.c | 2 +- 4 files changed, 5 insertions(+), 3 deletions(-)
include/qemu/coroutine_int.h +2 -0
@@ -28,6 +28,8 @@#include "qemu/queue.h" #include "qemu/coroutine.h" +#define COROUTINE_STACK_SIZE (1 << 20) + typedef enum { COROUTINE_YIELD = 1, COROUTINE_TERMINATE = 2,
util/coroutine-sigaltstack.c +1 -1
@@ -143,7 +143,7 @@ static void coroutine_trampoline(int signal)Coroutine *qemu_coroutine_new (void ) { -const size_t stack_size =1 <<20 ; +const size_t stack_size =COROUTINE_STACK_SIZE ;CoroutineUContext *co;CoroutineThreadState *coTS;struct sigaction sa;
util/coroutine-ucontext.c +1 -1
@@ -82,7 +82,7 @@ static void coroutine_trampoline(int i0, int i1)Coroutine *qemu_coroutine_new (void ) { -const size_t stack_size =1 <<20 ; +const size_t stack_size =COROUTINE_STACK_SIZE ;CoroutineUContext *co;ucontext_t old_uc, uc;sigjmp_buf old_env;
util/coroutine-win32.c +1 -1
@@ -71,7 +71,7 @@ static void CALLBACK coroutine_trampoline(void *co_)Coroutine *qemu_coroutine_new (void ) { -const size_t stack_size =1 <<20 ; +const size_t stack_size =COROUTINE_STACK_SIZE ;CoroutineWin32 *co; co =g_malloc0 (sizeof (*co));
Signature · 1 line
1.9.1
PATCH v4 4/6 coroutine-sigaltstack: use helper for allocating stack memory
In reply to Peter Lieven--- util/coroutine-sigaltstack.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
util/coroutine-sigaltstack.c +3 -4
@@ -143,7 +143,6 @@ static void coroutine_trampoline(int signal)Coroutine *qemu_coroutine_new (void ) { -const size_t stack_size =COROUTINE_STACK_SIZE ;CoroutineUContext *co;CoroutineThreadState *coTS;struct sigaction sa;
@@ -164,7 +163,7 @@ Coroutine *qemu_coroutine_new(void)*/ co =g_malloc0 (sizeof (*co)); - co->stack =g_malloc (stack_size); + co->stack =qemu_alloc_stack (COROUTINE_STACK_SIZE ); co->base.entry_arg = &old_env;/* stash away our jmp_buf */ coTS =coroutine_get_thread_state ();
@@ -189,7 +188,7 @@ Coroutine *qemu_coroutine_new(void)* Set the new stack. */ ss.ss_sp = co->stack; - ss.ss_size = stack_size; + ss.ss_size =COROUTINE_STACK_SIZE ; ss.ss_flags =0 ;if (sigaltstack (&ss, &oss) <0 ) {abort ();
@@ -253,7 +252,7 @@ void qemu_coroutine_delete(Coroutine *co_) {CoroutineUContext *co =DO_UPCAST (CoroutineUContext, base, co_); -g_free (co->stack); +qemu_free_stack (co->stack,COROUTINE_STACK_SIZE );g_free (co); }
Signature · 1 line
1.9.1
PATCH v4 5/6 oslib-posix: add a configure switch to debug stack usage
In reply to Peter Lieventhis adds a knob to track the maximum stack usage of stacks created by qemu_alloc_stack.
--- configure | 19 +++++++++++++++++++ util/oslib-posix.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+)
configure +19 -0
@@ -296,6 +296,7 @@ libiscsi="" libnfs="" coroutine="" coroutine_pool="" +debug_stack_usage="no" seccomp="" glusterfs="" glusterfs_xlator_opt="no"
@@ -1004,6 +1005,8 @@ for opt do ;; --enable-coroutine-pool) coroutine_pool="yes" ;; + --enable-debug-stack-usage) debug_stack_usage="yes" + ;; --disable-docs) docs="no" ;; --enable-docs) docs="yes"
@@ -4301,6 +4304,17 @@ if test "$coroutine" = "gthread" -a "$coroutine_pool" = "yes"; then error_exit "'gthread' coroutine backend does not support pool (use --disable-coroutine-pool)" fi +if test "$debug_stack_usage" = "yes"; then + if test "$cpu" = "ia64" -o "$cpu" = "hppa"; then + error_exit "stack usage debugging is not supported for $cpu" + fi + if test "$coroutine_pool" = "yes"; then + echo "WARN: disabling coroutine pool for stack usage debugging" + coroutine_pool=no + fi +fi + + ########################################## # check if we have open_by_handle_at
@@ -4885,6 +4899,7 @@ echo "QGA MSI support $guest_agent_msi" echo "seccomp support $seccomp" echo "coroutine backend $coroutine" echo "coroutine pool $coroutine_pool" +echo "debug stack usage $debug_stack_usage" echo "GlusterFS support $glusterfs" echo "Archipelago support $archipelago" echo "gcov $gcov_tool"
@@ -5353,6 +5368,10 @@ else echo "CONFIG_COROUTINE_POOL=0" >> $config_host_mak fi +if test "$debug_stack_usage" = "yes" ; then + echo "CONFIG_DEBUG_STACK_USAGE=y" >> $config_host_mak +fi + if test "$open_by_handle_at" = "yes" ; then echo "CONFIG_OPEN_BY_HANDLE=y" >> $config_host_mak fi
util/oslib-posix.c +37 -0
@@ -50,6 +50,10 @@#include <qemu/mmap-alloc.h> +#ifdef CONFIG_DEBUG_STACK_USAGE +#include "qemu/error-report.h" +#endif +int qemu_get_thread_id (void ) {#if defined(__linux__)
@@ -510,6 +514,9 @@ static size_t adjust_stack_size(size_t sz)void *qemu_alloc_stack (size_t sz) {void *ptr, *guardpage; +#ifdef CONFIG_DEBUG_STACK_USAGE +void *ptr2; +#endif size_t pagesz =getpagesize (); sz =adjust_stack_size (sz);
@@ -533,11 +540,41 @@ void *qemu_alloc_stack(size_t sz)abort (); } +#ifdef CONFIG_DEBUG_STACK_USAGE +for (ptr2 = ptr + pagesz; ptr2 < ptr + sz; ptr2 +=sizeof (uint32_t )) { + *(uint32_t *)ptr2 =0xdeadbeaf ; + } +#endif +return ptr; } +#ifdef CONFIG_DEBUG_STACK_USAGE +static __threadunsigned int max_stack_usage; +#endif +void qemu_free_stack (void *stack,size_t sz) { +#ifdef CONFIG_DEBUG_STACK_USAGE +unsigned int usage; +void *ptr; +#endif sz =adjust_stack_size (sz); + +#ifdef CONFIG_DEBUG_STACK_USAGE +for (ptr = stack +getpagesize (); ptr < stack + sz; + ptr +=sizeof (uint32_t )) { +if (*(uint32_t *)ptr !=0xdeadbeaf ) { +break ; + } + } + usage = sz - (uintptr_t ) (ptr - stack); +if (usage > max_stack_usage) { +error_report ("thread %d max stack usage increased from %u to %u" , +qemu_get_thread_id (), max_stack_usage, usage); + max_stack_usage = usage; + } +#endif +munmap (stack, sz); }
Signature · 1 line
1.9.1
PATCH v4 3/6 coroutine-ucontext: use helper for allocating stack memory
In reply to Peter Lieven--- util/coroutine-ucontext.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
util/coroutine-ucontext.c +4 -5
@@ -82,7 +82,6 @@ static void coroutine_trampoline(int i0, int i1)Coroutine *qemu_coroutine_new (void ) { -const size_t stack_size =COROUTINE_STACK_SIZE ;CoroutineUContext *co;ucontext_t old_uc, uc;sigjmp_buf old_env;
@@ -101,17 +100,17 @@ Coroutine *qemu_coroutine_new(void) } co =g_malloc0 (sizeof (*co)); - co->stack =g_malloc (stack_size); + co->stack =qemu_alloc_stack (COROUTINE_STACK_SIZE ); co->base.entry_arg = &old_env;/* stash away our jmp_buf */ uc.uc_link = &old_uc; uc.uc_stack.ss_sp = co->stack; - uc.uc_stack.ss_size = stack_size; + uc.uc_stack.ss_size =COROUTINE_STACK_SIZE ; uc.uc_stack.ss_flags =0 ;#ifdef CONFIG_VALGRIND_H co->valgrind_stack_id = -VALGRIND_STACK_REGISTER (co->stack, co->stack + stack_size); +VALGRIND_STACK_REGISTER (co->stack, co->stack +COROUTINE_STACK_SIZE );#endif arg.p = co;
@@ -149,7 +148,7 @@ void qemu_coroutine_delete(Coroutine *co_)valgrind_stack_deregister (co);#endif -g_free (co->stack); +qemu_free_stack (co->stack,COROUTINE_STACK_SIZE );g_free (co); }
Signature · 1 line
1.9.1
Re: [Qemu-devel] [PATCH V4 1/6] oslib-posix: add helpers for stack alloc and free
In reply to Peter LievenQuoted 10 lines · Peter Lieven
the allocated stack will be adjusted to the minimum supported stack size by the OS and rounded up to be a multiple of the system pagesize. Additionally an architecture dependent guard page is added to the stack to catch stack overflows.
Signed-off-by Peter Lieven <[email protected]>--- include/sysemu/os-posix.h | 23 +++++++++++++++++++++++ util/oslib-posix.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)
r~
Re: [Qemu-devel] [PATCH V4 1/6] oslib-posix: add helpers for stack alloc and free
In reply to Peter LievenQuoted 12 lines · Peter Lieven
the allocated stack will be adjusted to the minimum supported stack size by the OS and rounded up to be a multiple of the system pagesize. Additionally an architecture dependent guard page is added to the stack to catch stack overflows.
Signed-off-by Peter Lieven <[email protected]>--- include/sysemu/os-posix.h | 23 +++++++++++++++++++++++ util/oslib-posix.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)
util/oslib-posix.c pid_t qemu_fork(Error **errp) · line 504
Quoted 5 lines
util/oslib-posix.c +5
-0+ +static size_t adjust_stack_size (size_t sz) +{ +/* avoid stacks smaller than _SC_THREAD_STACK_MIN */ + sz =MAX (sz,sysconf (_SC_THREAD_STACK_MIN));
sz is unsigned, but sysconf() is signed. Furthermore, sysconf() is permitted to return -1 if there is no such minimum. MAX() would then operate on the common integral promotion between the two arguments, which may treat (unsigned)(-1) as the larger of the two values, and give you the wrong results.
I think it is theoretical (all platforms that we compile on have a working sysconf(_SC_THREAD_STACK_MIN), right?), but still may be worth being sure that sysconf() returned a positive value before computing MAX().
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH V4 1/6] oslib-posix: add helpers for stack alloc and free
Am 11.07.2016 um 18:39 schrieb Eric Blake:
Quoted 23 lines · Peter Lieven
Quoted 11 lines
the allocated stack will be adjusted to the minimum supported stack size by the OS and rounded up to be a multiple of the system pagesize. Additionally an architecture dependent guard page is added to the stack to catch stack overflows.
Signed-off-by Peter Lieven <[email protected]>--- include/sysemu/os-posix.h | 23 +++++++++++++++++++++++ util/oslib-posix.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)util/oslib-posix.c pid_t qemu_fork(Error **errp) · line 504
Quoted 5 lines
util/oslib-posix.c +5
-0+ +static size_t adjust_stack_size (size_t sz) +{ +/* avoid stacks smaller than _SC_THREAD_STACK_MIN */ + sz =MAX (sz,sysconf (_SC_THREAD_STACK_MIN));sz is unsigned, but
sysconf()is signed. Furthermore,sysconf()is permitted to return -1 if there is no such minimum.MAX()would then operate on the common integral promotion between the two arguments, which may treat (unsigned)(-1) as the larger of the two values, and give you the wrong results.
Quoted 4 lines
I think it is theoretical (all platforms that we compile on have a working
sysconf(_SC_THREAD_STACK_MIN), right?), but still may be worth being sure thatsysconf()returned a positive value before computingMAX().
If you feel more comfortable I can surround it by a
if (sysconf(_SC_THREAD_STACK_MIN) > 0) { }
I wonder if the _SC_THREAD_STACK_MIN constant exists if there is no minimum?
Peter
Re: [Qemu-devel] [PATCH V4 1/6] oslib-posix: add helpers for stack alloc and free
Am 12.07.2016 um 16:36 schrieb Peter Lieven:
Quoted 28 lines
Am 11.07.2016 um 18:39 schrieb Eric Blake:
Quoted 23 lines · Peter Lieven
Quoted 11 lines
the allocated stack will be adjusted to the minimum supported stack size by the OS and rounded up to be a multiple of the system pagesize. Additionally an architecture dependent guard page is added to the stack to catch stack overflows.
Signed-off-by Peter Lieven <[email protected]>--- include/sysemu/os-posix.h | 23 +++++++++++++++++++++++ util/oslib-posix.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)util/oslib-posix.c pid_t qemu_fork(Error **errp) · line 504
Quoted 5 lines
util/oslib-posix.c +5
-0+ +static size_t adjust_stack_size (size_t sz) +{ +/* avoid stacks smaller than _SC_THREAD_STACK_MIN */ + sz =MAX (sz,sysconf (_SC_THREAD_STACK_MIN));sz is unsigned, but
sysconf()is signed. Furthermore,sysconf()is permitted to return -1 if there is no such minimum.MAX()would then operate on the common integral promotion between the two arguments, which may treat (unsigned)(-1) as the larger of the two values, and give you the wrong results.Quoted 4 lines
I think it is theoretical (all platforms that we compile on have a working
sysconf(_SC_THREAD_STACK_MIN), right?), but still may be worth being sure thatsysconf()returned a positive value before computingMAX().
Quoted 5 lines
If you feel more comfortable I can surround it by a
if (
sysconf(_SC_THREAD_STACK_MIN)> 0) { }I wonder if the
_SC_THREAD_STACK_MINconstant exists if there is no minimum?
Update:
glibc basically does the following:
static gulong g_thread_min_stack_size = 0;
#ifdef _SC_THREAD_STACK_MINg_thread_min_stack_size = MAX (sysconf (_SC_THREAD_STACK_MIN), 0); #endif /* _SC_THREAD_STACK_MIN */
stack_size = MAX (g_thread_min_stack_size, stack_size);
So we should do sth similar, I think?!
Peter
Re: [Qemu-devel] [PATCH V4 1/6] oslib-posix: add helpers for stack alloc and free
Quoted 8 lines · Peter Lieven
Quoted 1 line
I wonder if the
_SC_THREAD_STACK_MINconstant exists if there is no minimum?Update:
glibc basically does the following:
static gulong
g_thread_min_stack_size= 0;
Quoted 8 lines
#ifdef
_SC_THREAD_STACK_MINg_thread_min_stack_size= MAX (sysconf (_SC_THREAD_STACK_MIN), 0); #endif /*_SC_THREAD_STACK_MIN*/
stack_size= MAX (g_thread_min_stack_size,stack_size);So we should do sth similar, I think?!
Yes, that would be a good pattern to copy.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org