[glibc] support: Add use_stack_min option to support_small_thread_stack_size

Adhemerval Zanella via Glibc-cvs <[email protected]> Wed, 20 May 2026 18:17:53 +0000 (GMT)
Newsgroups gmane.comp.lib.glibc.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=f86b51dd6afa868fe75e114c7c004d708d096a7a

commit f86b51dd6afa868fe75e114c7c004d708d096a7a
Author: Adhemerval Zanella <[email protected]>
Date:   Tue May 19 10:23:52 2026 -0300

    support: Add use_stack_min option to support_small_thread_stack_size
    
    It allows it to return PTHREAD_STACK_MIN if defined.
    
    Checked on x86_64-linux-gnu and with a build for i686-gnu.
    
    Suggested-by: H.J. Lu <[email protected]>
    Reviewed-by: H.J. Lu <[email protected]>

Diff:
---
 elf/tst-bz26577-minstack.c                             |  7 +------
 elf/tst-decorate-maps.c                                |  2 +-
 nptl/tst-guard1.c                                      |  4 ++--
 stdlib/tst-canon-bz26341.c                             |  2 +-
 support/support_set_small_thread_stack_size.c          | 13 +++++++++----
 support/support_small_stack_thread_attribute.c         |  2 +-
 support/xthread.h                                      | 12 +++++++-----
 sysdeps/unix/sysv/linux/tst-sem_getvalue-affinity.c    |  2 +-
 sysdeps/unix/sysv/linux/tst-skeleton-thread-affinity.c |  2 +-
 9 files changed, 24 insertions(+), 22 deletions(-)

diff --git a/elf/tst-bz26577-minstack.c b/elf/tst-bz26577-minstack.c
index 741b307392..9d5ecd8593 100644
--- a/elf/tst-bz26577-minstack.c
+++ b/elf/tst-bz26577-minstack.c
@@ -51,12 +51,7 @@ static int
 do_test (void)
 {
   char *path = xasprintf ("%s/elf/tst-bz26577-mod.so", support_objdir_root);
-  size_t stacksize =
-#ifdef PTHREAD_STACK_MIN
-    PTHREAD_STACK_MIN;
-#else
-    support_small_thread_stack_size ();
-#endif
+  size_t stacksize = support_small_thread_stack_size (true);
 
   pthread_attr_t attr;
   xpthread_attr_init (&attr);
diff --git a/elf/tst-decorate-maps.c b/elf/tst-decorate-maps.c
index fa3637af89..b6af31c727 100644
--- a/elf/tst-decorate-maps.c
+++ b/elf/tst-decorate-maps.c
@@ -128,7 +128,7 @@ do_test_threads (bool set_guard)
       {
 	pthread_attr_t attr;
 	xpthread_attr_init (&attr);
-	size_t stacksize = support_small_thread_stack_size ();
+	size_t stacksize = support_small_thread_stack_size (false);
 	void *stack = xmmap (0,
 			     stacksize,
 			     PROT_READ | PROT_WRITE,
diff --git a/nptl/tst-guard1.c b/nptl/tst-guard1.c
index b97ad23de4..3486cba9e4 100644
--- a/nptl/tst-guard1.c
+++ b/nptl/tst-guard1.c
@@ -176,7 +176,7 @@ do_test1 (void *closure)
   pthread_attr_t attr;
   xpthread_attr_init (&attr);
 
-  size_t stacksize = support_small_thread_stack_size ();
+  size_t stacksize = support_small_thread_stack_size (false);
   void *stack = xmmap (0,
 		       stacksize,
 		       PROT_READ | PROT_WRITE,
@@ -201,7 +201,7 @@ do_test2 (void *closure)
   pthread_attr_t attr;
   xpthread_attr_init (&attr);
 
-  size_t stacksize = support_small_thread_stack_size ();
+  size_t stacksize = support_small_thread_stack_size (false);
   void *stack = xmmap (0,
 		       stacksize,
 		       PROT_READ | PROT_WRITE,
diff --git a/stdlib/tst-canon-bz26341.c b/stdlib/tst-canon-bz26341.c
index 4860818a4e..e94c924ac1 100644
--- a/stdlib/tst-canon-bz26341.c
+++ b/stdlib/tst-canon-bz26341.c
@@ -81,7 +81,7 @@ do_realpath (void *arg)
   const size_t syscall_usage = 1 * PATH_MAX + 1024;
   const size_t realpath_usage = 2 * PATH_MAX + 1024;
   const size_t thread_usage = 1 * PATH_MAX + 1024;
-  size_t stack_size = support_small_thread_stack_size ()
+  size_t stack_size = support_small_thread_stack_size (false)
 		      - syscall_usage - realpath_usage - thread_usage;
   char stack[stack_size];
   char *resolved = stack + stack_size - thread_usage + 1024;
diff --git a/support/support_set_small_thread_stack_size.c b/support/support_set_small_thread_stack_size.c
index 6c2cd4f92b..51ec05b322 100644
--- a/support/support_set_small_thread_stack_size.c
+++ b/support/support_set_small_thread_stack_size.c
@@ -21,13 +21,16 @@
 #include <support/xthread.h>
 
 size_t
-support_small_thread_stack_size (void)
+support_small_thread_stack_size (bool use_stack_min)
 {
   /* Some architectures have too small values for PTHREAD_STACK_MIN
      which cannot be used for creating threads.  Ensure that the stack
-     size is at least 256 KiB.  */
+     size is at least 256 KiB if USE_STACK_MIN is false.  */
   size_t stack_size = 256 * 1024;
 #ifdef PTHREAD_STACK_MIN
+  if (use_stack_min)
+    return PTHREAD_STACK_MIN;
+
   if (stack_size < PTHREAD_STACK_MIN)
     stack_size = PTHREAD_STACK_MIN;
 #endif
@@ -35,7 +38,9 @@ support_small_thread_stack_size (void)
 }
 
 void
-support_set_small_thread_stack_size (pthread_attr_t *attr)
+support_set_small_thread_stack_size (pthread_attr_t *attr,
+				     bool use_stack_min)
 {
-  xpthread_attr_setstacksize (attr, support_small_thread_stack_size ());
+  xpthread_attr_setstacksize
+    (attr, support_small_thread_stack_size (use_stack_min));
 }
diff --git a/support/support_small_stack_thread_attribute.c b/support/support_small_stack_thread_attribute.c
index dd97e42146..229deabe9e 100644
--- a/support/support_small_stack_thread_attribute.c
+++ b/support/support_small_stack_thread_attribute.c
@@ -24,7 +24,7 @@ allocate (void *closure)
 {
   pthread_attr_t *result = malloc (sizeof (*result));
   xpthread_attr_init (result);
-  support_set_small_thread_stack_size (result);
+  support_set_small_thread_stack_size (result, false);
   return result;
 }
 
diff --git a/support/xthread.h b/support/xthread.h
index d585bd0c3c..3bdc6907aa 100644
--- a/support/xthread.h
+++ b/support/xthread.h
@@ -90,11 +90,13 @@ void xpthread_attr_setguardsize (pthread_attr_t *attr,
 
 void xpthread_kill (pthread_t thr, int signo);
 
-/* Return the stack size used on support_set_small_thread_stack_size.  */
-size_t support_small_thread_stack_size (void);
-/* Set the stack size in ATTR to a small value, but still large enough
-   to cover most internal glibc stack usage.  */
-void support_set_small_thread_stack_size (pthread_attr_t *attr);
+/* Return the stack size used on support_set_small_thread_stack_size,
+   or PTHREAD_STACK_MIN (if defined) is USE_STACK_MIN is set.  */
+size_t support_small_thread_stack_size (bool use_stack_min);
+/* Set the stack size in ATTR to a small value.  Use PTHREAD_STACK_MIN (if
+   defined) or a large enough to cover most internal glibc stack usage.  */
+void support_set_small_thread_stack_size (pthread_attr_t *attr,
+					  bool use_stack_min);
 
 /* Return a pointer to a thread attribute which requests a small
    stack.  The caller must not free this pointer.  */
diff --git a/sysdeps/unix/sysv/linux/tst-sem_getvalue-affinity.c b/sysdeps/unix/sysv/linux/tst-sem_getvalue-affinity.c
index c4c430edeb..f30e0bbed5 100644
--- a/sysdeps/unix/sysv/linux/tst-sem_getvalue-affinity.c
+++ b/sysdeps/unix/sysv/linux/tst-sem_getvalue-affinity.c
@@ -136,7 +136,7 @@ early_test (struct conf *conf)
       printf ("error: pthread_attr_init failed: %s\n", strerror (ret));
       return false;
     }
-  support_set_small_thread_stack_size (&attr);
+  support_set_small_thread_stack_size (&attr, false);
 
   /* Spawn a thread pinned to each available CPU.  */
   for (int cpu = 0; cpu <= conf->last_cpu; ++cpu)
diff --git a/sysdeps/unix/sysv/linux/tst-skeleton-thread-affinity.c b/sysdeps/unix/sysv/linux/tst-skeleton-thread-affinity.c
index 323b2e0ffc..4477fde150 100644
--- a/sysdeps/unix/sysv/linux/tst-skeleton-thread-affinity.c
+++ b/sysdeps/unix/sysv/linux/tst-skeleton-thread-affinity.c
@@ -208,7 +208,7 @@ early_test (struct conf *conf)
       printf ("error: pthread_attr_init failed: %s\n", strerror (ret));
       return false;
     }
-  support_set_small_thread_stack_size (&attr);
+  support_set_small_thread_stack_size (&attr, false);
 
   /* This count assumes that all the threads below are created
      successfully, and call pthread_barrier_wait().  If any threads