Skip to contentSkip to conversation

tools/xenstore: try to get minimum thread stack size for watch thread

PATCH Superseded Juergen Gross 3 participants 8 messages Feb 22, 2018 – Feb 26, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/xenstore: try to get minimum thread stack size for watch thread

When creating a pthread in xs_watch() try to get the minimal needed size of the thread from glibc instead of using a constant. This avoids problems when the library is used in programs with large per-thread memory.

Use dlsym() to get the pointer to __pthread_get_minstack() in order to avoid linkage problems and fall back to the current constant size if not found.

---
Only compile tested. Jim, can you please verify this patch is solving your original problem?

---
 tools/xenstore/Makefile |  4 ++++
 tools/xenstore/xs.c     | 19 ++++++++++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)
tools/xenstore/Makefile +4 -0
index 2b99d2bc1b..fb6c73e297 100644
@@ -100,6 +100,10 @@ libxenstore.so.$(MAJOR): libxenstore.so.$(MAJOR).$(MINOR)
 	ln -sf $< $@
 
 xs.opic: CFLAGS += -DUSE_PTHREAD
+ifeq ($(CONFIG_Linux),y)
+xs.opic: CFLAGS += -DUSE_DLSYM
+xs.opic: LDFLAGS += -ldl
+endif
 
 libxenstore.so.$(MAJOR).$(MINOR): xs.opic xs_lib.opic
 	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenstore.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoolcore) $(SOCKET_LIBS) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
tools/xenstore/xs.c +18 -1
index abffd9cd80..8372f5b1a4 100644
@@ -47,6 +47,11 @@ struct xs_stored_msg {
 
 #include <pthread.h>
 
+#ifdef USE_DLSYM
+#define __USE_GNU
+#include <dlfcn.h>
+#endif
+
 struct xs_handle {
 	/* Communications channel to xenstore daemon. */
 	int fd;
@@ -810,12 +815,24 @@ bool xs_watch(struct xs_handle *h, const char *path, const char *token)
 	if (!h->read_thr_exists) {
 		sigset_t set, old_set;
 		pthread_attr_t attr;
+		static size_t stack_size;
+#ifdef USE_DLSYM
+		size_t (*getsz)(void);
+#endif
 
+		if (!stack_size) {
+#ifdef USE_DLSYM
+			getsz = dlsym(RTLD_DEFAULT, "__pthread_get_minstack");
+			stack_size = getsz ? getsz() : READ_THREAD_STACKSIZE;
+#else
+			stack_size = READ_THREAD_STACKSIZE;
+#endif
+		}
 		if (pthread_attr_init(&attr) != 0) {
 			mutex_unlock(&h->request_mutex);
 			return false;
 		}
-		if (pthread_attr_setstacksize(&attr, READ_THREAD_STACKSIZE) != 0) {
+		if (pthread_attr_setstacksize(&attr, stack_size) != 0) {
 			pthread_attr_destroy(&attr);
 			mutex_unlock(&h->request_mutex);
 			return false;
Signature · 7 lines
2.13.6


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 38 lines · Juergen Gross

When creating a pthread in xs_watch() try to get the minimal needed size of the thread from glibc instead of using a constant. This avoids problems when the library is used in programs with large per-thread memory.

Use dlsym() to get the pointer to __pthread_get_minstack() in order to avoid linkage problems and fall back to the current constant size if not found.

---
Only compile tested. Jim, can you please verify this patch is solving your original problem?

---
 tools/xenstore/Makefile |  4 ++++
 tools/xenstore/xs.c     | 19 ++++++++++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)
tools/xenstore/Makefile +4 -0
index 2b99d2bc1b..fb6c73e297 100644
@@ -100,6 +100,10 @@ libxenstore.so.$(MAJOR): libxenstore.so.$(MAJOR).$(MINOR)
 	ln -sf $< $@
 
 xs.opic: CFLAGS += -DUSE_PTHREAD
+ifeq ($(CONFIG_Linux),y)
+xs.opic: CFLAGS += -DUSE_DLSYM
+xs.opic: LDFLAGS += -ldl
+endif
 
 libxenstore.so.$(MAJOR).$(MINOR): xs.opic xs_lib.opic
 	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenstore.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoolcore) $(SOCKET_LIBS) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
tools/xenstore/xs.c +0 -0
index abffd9cd80..8372f5b1a4 100644

tools/xenstore/xs.c struct xs_stored_msg { · line 51

Quoted 5 lines
tools/xenstore/xs.c +2 -0
@@ -47,6 +47,11 @@ struct xs_stored_msg {
 
 #include <pthread.h>
 
+#ifdef USE_DLSYM
+#define __USE_GNU

Where does this come from? DLSYM(3) says _GNU_SOURCE (which we already have).

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 40 lines · Wei Liu
Quoted 39 lines · Juergen Gross

When creating a pthread in xs_watch() try to get the minimal needed size of the thread from glibc instead of using a constant. This avoids problems when the library is used in programs with large per-thread memory.

Use dlsym() to get the pointer to __pthread_get_minstack() in order to avoid linkage problems and fall back to the current constant size if not found.

---
Only compile tested. Jim, can you please verify this patch is solving your original problem?

---
 tools/xenstore/Makefile |  4 ++++
 tools/xenstore/xs.c     | 19 ++++++++++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)
tools/xenstore/Makefile +4 -0
index 2b99d2bc1b..fb6c73e297 100644
@@ -100,6 +100,10 @@ libxenstore.so.$(MAJOR): libxenstore.so.$(MAJOR).$(MINOR)
 	ln -sf $< $@
 
 xs.opic: CFLAGS += -DUSE_PTHREAD
+ifeq ($(CONFIG_Linux),y)
+xs.opic: CFLAGS += -DUSE_DLSYM
+xs.opic: LDFLAGS += -ldl
+endif
 
 libxenstore.so.$(MAJOR).$(MINOR): xs.opic xs_lib.opic
 	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenstore.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoolcore) $(SOCKET_LIBS) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
tools/xenstore/xs.c +0 -0
index abffd9cd80..8372f5b1a4 100644
@@ -47,6 +47,11 @@ struct xs_stored_msg {
 
Quoted 7 lines

tools/xenstore/xs.c struct xs_stored_msg { · line 51

Quoted 4 lines
tools/xenstore/xs.c +2 -0
 #include <pthread.h>
 
+#ifdef USE_DLSYM
+#define __USE_GNU

Where does this come from? DLSYM(3) says _GNU_SOURCE (which we already have).

On my machine build failed, so I looked into the header...

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 43 lines · Juergen Gross
Quoted 42 lines · Wei Liu
Quoted 41 lines · Juergen Gross

When creating a pthread in xs_watch() try to get the minimal needed size of the thread from glibc instead of using a constant. This avoids problems when the library is used in programs with large per-thread memory.

Use dlsym() to get the pointer to __pthread_get_minstack() in order to avoid linkage problems and fall back to the current constant size if not found.

---
Only compile tested. Jim, can you please verify this patch is solving your original problem?

---
 tools/xenstore/Makefile |  4 ++++
 tools/xenstore/xs.c     | 19 ++++++++++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)
tools/xenstore/Makefile +4 -0
index 2b99d2bc1b..fb6c73e297 100644
@@ -100,6 +100,10 @@ libxenstore.so.$(MAJOR): libxenstore.so.$(MAJOR).$(MINOR)
 	ln -sf $< $@
 
 xs.opic: CFLAGS += -DUSE_PTHREAD
+ifeq ($(CONFIG_Linux),y)
+xs.opic: CFLAGS += -DUSE_DLSYM
+xs.opic: LDFLAGS += -ldl
+endif
 
 libxenstore.so.$(MAJOR).$(MINOR): xs.opic xs_lib.opic
 	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenstore.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoolcore) $(SOCKET_LIBS) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
tools/xenstore/xs.c +0 -0
index abffd9cd80..8372f5b1a4 100644
@@ -47,6 +47,11 @@ struct xs_stored_msg {
 
 #include <pthread.h>
 
Quoted 8 lines
Quoted 5 lines

tools/xenstore/xs.c struct xs_stored_msg { · line 51

Quoted 2 lines
tools/xenstore/xs.c +2 -0
+#ifdef USE_DLSYM
+#define __USE_GNU

Where does this come from? DLSYM(3) says _GNU_SOURCE (which we already have).

On my machine build failed, so I looked into the header...

Does putting _GNU_SOURCE at the beginning of that file before all headers solve the issue? I don't think we want to use an internal definition.

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 50 lines · Wei Liu
Quoted 49 lines · Juergen Gross
Quoted 47 lines · Wei Liu
Quoted 38 lines · Juergen Gross

When creating a pthread in xs_watch() try to get the minimal needed size of the thread from glibc instead of using a constant. This avoids problems when the library is used in programs with large per-thread memory.

Use dlsym() to get the pointer to __pthread_get_minstack() in order to avoid linkage problems and fall back to the current constant size if not found.

---
Only compile tested. Jim, can you please verify this patch is solving your original problem?

---
 tools/xenstore/Makefile |  4 ++++
 tools/xenstore/xs.c     | 19 ++++++++++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)
tools/xenstore/Makefile +4 -0
index 2b99d2bc1b..fb6c73e297 100644
@@ -100,6 +100,10 @@ libxenstore.so.$(MAJOR): libxenstore.so.$(MAJOR).$(MINOR)
 	ln -sf $< $@
 
 xs.opic: CFLAGS += -DUSE_PTHREAD
+ifeq ($(CONFIG_Linux),y)
+xs.opic: CFLAGS += -DUSE_DLSYM
+xs.opic: LDFLAGS += -ldl
+endif
 
 libxenstore.so.$(MAJOR).$(MINOR): xs.opic xs_lib.opic
 	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenstore.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoolcore) $(SOCKET_LIBS) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
tools/xenstore/xs.c +0 -0
index abffd9cd80..8372f5b1a4 100644

tools/xenstore/xs.c struct xs_stored_msg { · line 51

Quoted 5 lines
tools/xenstore/xs.c +2 -0
@@ -47,6 +47,11 @@ struct xs_stored_msg {
 
 #include <pthread.h>
 
+#ifdef USE_DLSYM
+#define __USE_GNU

Where does this come from? DLSYM(3) says _GNU_SOURCE (which we already have).

Quoted 6 lines
Quoted 2 lines

On my machine build failed, so I looked into the header...

Does putting _GNU_SOURCE at the beginning of that file before all headers solve the issue? I don't think we want to use an internal definition.

And I was wrong about "we already have _GNU_SOURCE" bit in my previous email. At least not in the file you modified.

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 51 lines · Wei Liu
Quoted 50 lines · Wei Liu
Quoted 49 lines · Juergen Gross
Quoted 47 lines · Wei Liu
Quoted 38 lines · Juergen Gross

When creating a pthread in xs_watch() try to get the minimal needed size of the thread from glibc instead of using a constant. This avoids problems when the library is used in programs with large per-thread memory.

Use dlsym() to get the pointer to __pthread_get_minstack() in order to avoid linkage problems and fall back to the current constant size if not found.

---
Only compile tested. Jim, can you please verify this patch is solving your original problem?

---
 tools/xenstore/Makefile |  4 ++++
 tools/xenstore/xs.c     | 19 ++++++++++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)
tools/xenstore/Makefile +4 -0
index 2b99d2bc1b..fb6c73e297 100644
@@ -100,6 +100,10 @@ libxenstore.so.$(MAJOR): libxenstore.so.$(MAJOR).$(MINOR)
 	ln -sf $< $@
 
 xs.opic: CFLAGS += -DUSE_PTHREAD
+ifeq ($(CONFIG_Linux),y)
+xs.opic: CFLAGS += -DUSE_DLSYM
+xs.opic: LDFLAGS += -ldl
+endif
 
 libxenstore.so.$(MAJOR).$(MINOR): xs.opic xs_lib.opic
 	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenstore.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoolcore) $(SOCKET_LIBS) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
tools/xenstore/xs.c +0 -0
index abffd9cd80..8372f5b1a4 100644

tools/xenstore/xs.c struct xs_stored_msg { · line 51

Quoted 5 lines
tools/xenstore/xs.c +2 -0
@@ -47,6 +47,11 @@ struct xs_stored_msg {
 
 #include <pthread.h>
 
+#ifdef USE_DLSYM
+#define __USE_GNU

Where does this come from? DLSYM(3) says _GNU_SOURCE (which we already have).

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

On my machine build failed, so I looked into the header...

Does putting _GNU_SOURCE at the beginning of that file before all headers solve the issue? I don't think we want to use an internal definition.

Aah, I put it just before including dlfcn.h and it didn't work. Putting it at the beginning solves the problem.

I'll send V2 after waiting a bit for other comments.

Quoted 2 lines

And I was wrong about "we already have _GNU_SOURCE" bit in my previous email. At least not in the file you modified.

Xenstore uses it already, too.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
In reply to Juergen Gross
Quoted 5 lines · Juergen Gross

When creating a pthread in xs_watch() try to get the minimal needed size of the thread from glibc instead of using a constant. This avoids problems when the library is used in programs with large per-thread memory.

Quoted 8 lines

Use dlsym() to get the pointer to __pthread_get_minstack() in order to avoid linkage problems and fall back to the current constant size if not found.

---
Only compile tested. Jim, can you please verify this patch is solving your original problem?

It didn't help, but it could be due to my buggy glibc

# gdb xl
...
(gdb) r create test-hvm.xl
Starting program: /usr/sbin/xl create test-hvm.xl
Parsing config from test-hvm.xl
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff72d51c2 in __pthread_get_minstack () from /lib64/libpthread.so.0
(gdb) thr a a bt

Thread 1 (Thread 0x7ffff7fd8780 (LWP 2568)):
#0 0x00007ffff72d51c2 in __pthread_get_minstack () from /lib64/libpthread.so.0 #1 0x00007ffff66ae259 in xs_watch (h=0x55555578fc90,

    path=path@entry=0x555555798fa0 "/local/domain/0/device-model/2/state",
    token=token@entry=0x5555557990b0 "3/0") at xs.c:826
#2  0x00007ffff79476f4 in libxl__ev_xswatch_register (gc=gc@entry=0x5555557955f0,
    w=w@entry=0x555555797468, func=func@entry=0x7ffff793dd10 
<xswait_xswatch_callback>,
    path=0x555555798fa0 "/local/domain/0/device-model/2/state") at 
libxl_event.c:638
#3  0x00007ffff793deb0 in libxl__xswait_start (gc=gc@entry=0x5555557955f0,
    xswa=xswa@entry=0x5555557973e0) at libxl_aoutils.c:53
#4  0x00007ffff79326b0 in libxl__spawn_spawn (egc=egc@entry=0x7fffffffd950,
    ss=ss@entry=0x555555797370) at libxl_exec.c:292
#5  0x00007ffff79258d3 in libxl__spawn_local_dm (egc=0x7fffffffd950, dmss=<optimized out>)
    at libxl_dm.c:2400
#6  0x00007ffff791d3a7 in domcreate_launch_dm (egc=0x7fffffffd950, multidev=0x555555798168,
    ret=<optimized out>) at libxl_create.c:1379
#7  0x00007ffff7967275 in libxl__bootloader_run (egc=egc@entry=0x7fffffffd950,
    bl=bl@entry=0x555555796cc0) at libxl_bootloader.c:403
#8  0x00007ffff791ffe3 in initiate_domain_create (egc=egc@entry=0x7fffffffd950,
    dcs=dcs@entry=0x555555796610) at libxl_create.c:997
#9  0x00007ffff79201a1 in do_domain_create (ctx=ctx@entry=0x55555578f2a0,
    d_config=d_config@entry=0x7fffffffdb70, domid=domid@entry=0x7fffffffdaa8,
    restore_fd=restore_fd@entry=-1, send_back_fd=send_back_fd@entry=-1, 
params=params@entry=0x0,
    ao_how=0x0, aop_console_how=0x0) at libxl_create.c:1682
#10 0x00007ffff79204b6 in libxl_domain_create_new (ctx=0x55555578f2a0,
    d_config=d_config@entry=0x7fffffffdb70, domid=domid@entry=0x7fffffffdaa8,
    ao_how=ao_how@entry=0x0, aop_console_how=aop_console_how@entry=0x0) at 
libxl_create.c:1885
#11 0x00005555555780b4 in create_domain (dom_info=dom_info@entry=0x7fffffffe0b0)
    at xl_vmcontrol.c:902

#12 0x00005555555790c4 in main_create (argc=1, argv=0x7fffffffe378) at xl_vmcontrol.c:1207 #13 0x0000555555560c5b in main (argc=2, argv=0x7fffffffe370) at xl.c:384

If you like, I can try a patched glibc after the weekend :-).

Regards,
Jim

Quoted 53 lines
---
  tools/xenstore/Makefile |  4 ++++
  tools/xenstore/xs.c     | 19 ++++++++++++++++++-
  2 files changed, 22 insertions(+), 1 deletion(-)
tools/xenstore/Makefile +4 -0
index 2b99d2bc1b..fb6c73e297 100644
@@ -100,6 +100,10 @@ libxenstore.so.$(MAJOR): libxenstore.so.$(MAJOR).$(MINOR)
  	ln -sf $< $@
 
  xs.opic: CFLAGS += -DUSE_PTHREAD
+ifeq ($(CONFIG_Linux),y)
+xs.opic: CFLAGS += -DUSE_DLSYM
+xs.opic: LDFLAGS += -ldl
+endif
 
  libxenstore.so.$(MAJOR).$(MINOR): xs.opic xs_lib.opic
  	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenstore.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoolcore) $(SOCKET_LIBS) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
tools/xenstore/xs.c +16 -0
index abffd9cd80..8372f5b1a4 100644
@@ -47,6 +47,11 @@ struct xs_stored_msg {
 
  #include <pthread.h>
 
+#ifdef USE_DLSYM
+#define __USE_GNU
+#include <dlfcn.h>
+#endif
+
  struct xs_handle {
  	/* Communications channel to xenstore daemon. */
  	int fd;
@@ -810,12 +815,24 @@ bool xs_watch(struct xs_handle *h, const char *path, const char *token)
  	if (!h->read_thr_exists) {
  		sigset_t set, old_set;
  		pthread_attr_t attr;
+		static size_t stack_size;
+#ifdef USE_DLSYM
+		size_t (*getsz)(void);
+#endif
 
+		if (!stack_size) {
+#ifdef USE_DLSYM
+			getsz = dlsym(RTLD_DEFAULT, "__pthread_get_minstack");
+			stack_size = getsz ? getsz() : READ_THREAD_STACKSIZE;
+#else
+			stack_size = READ_THREAD_STACKSIZE;
+#endif

tools/xenstore/xs.c bool xs_watch(struct xs_handle *h, const char *path, const char *token) · line 838

Quoted 10 lines
tools/xenstore/xs.c +2 -1
+		}
  		if (pthread_attr_init(&attr) != 0) {
  			mutex_unlock(&h->request_mutex);
  			return false;
  		}
-		if (pthread_attr_setstacksize(&attr, READ_THREAD_STACKSIZE) != 0) {
+		if (pthread_attr_setstacksize(&attr, stack_size) != 0) {
  			pthread_attr_destroy(&attr);
  			mutex_unlock(&h->request_mutex);
  			return false;

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw
Quoted 10 lines · Jim Fehlig
Quoted 9 lines · Juergen Gross

When creating a pthread in xs_watch() try to get the minimal needed size of the thread from glibc instead of using a constant. This avoids problems when the library is used in programs with large per-thread memory.

Use dlsym() to get the pointer to __pthread_get_minstack() in order to avoid linkage problems and fall back to the current constant size if not found.

Quoted 6 lines
Quoted 4 lines

---
Only compile tested. Jim, can you please verify this patch is solving your original problem?

It didn't help, but it could be due to my buggy glibc

No, it was just a silly mistake in my patch.

Sending V2 soon...

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel