Threads currently have 2-4 MiB stacks by default (depending on the
platform). This is fine on MMU platforms, where this stack space is not
actually allocated until it is used, but tends to waste a large amount
of memory on no-MMU platforms.

This patch adds a PTHREADS_STACK_DEFAULT_SIZE Kconfig option that allows
the user to override the default stack size at build time. This allows
the user to select a reasonable default stack size for the software that
runs on their system, and avoids the need to patch every package to add
calls to pthread_attr_setstacksize().

An alternative to this patch would be to change the hardcoded default
stack size on no-MMU platforms, but it is difficult to choose an
appropriate value because the minimum required stack depends on the
software in use. This would also be a breaking change.

Signed-off-by: Ben Wolsieffer <ben.wolsief...@hefring.com>
---

Changes in v2:
  * Entirely move default stack size configuration to Kconfig

 extra/Configs/Config.in                         |  9 +++++++++
 libpthread/nptl/init.c                          | 14 +++++++-------
 libpthread/nptl/sysdeps/aarch64/pthreaddef.h    |  3 ---
 libpthread/nptl/sysdeps/alpha/pthreaddef.h      |  3 ---
 libpthread/nptl/sysdeps/arc/pthreaddef.h        |  3 ---
 libpthread/nptl/sysdeps/arm/pthreaddef.h        |  3 ---
 libpthread/nptl/sysdeps/csky/pthreaddef.h       |  3 ---
 libpthread/nptl/sysdeps/i386/pthreaddef.h       |  3 ---
 libpthread/nptl/sysdeps/kvx/pthreaddef.h        |  3 ---
 libpthread/nptl/sysdeps/m68k/pthreaddef.h       |  3 ---
 libpthread/nptl/sysdeps/metag/pthreaddef.h      |  3 ---
 libpthread/nptl/sysdeps/microblaze/pthreaddef.h |  3 ---
 libpthread/nptl/sysdeps/mips/pthreaddef.h       |  3 ---
 libpthread/nptl/sysdeps/nds32/pthreaddef.h      |  3 ---
 libpthread/nptl/sysdeps/nios2/pthreaddef.h      |  3 ---
 libpthread/nptl/sysdeps/or1k/pthreaddef.h       |  3 ---
 libpthread/nptl/sysdeps/powerpc/pthreaddef.h    |  3 ---
 libpthread/nptl/sysdeps/riscv64/pthreaddef.h    |  3 ---
 libpthread/nptl/sysdeps/sh/pthreaddef.h         |  3 ---
 libpthread/nptl/sysdeps/sparc/pthreaddef.h      |  3 ---
 libpthread/nptl/sysdeps/x86_64/pthreaddef.h     |  3 ---
 libpthread/nptl/sysdeps/xtensa/pthreaddef.h     |  3 ---
 22 files changed, 16 insertions(+), 67 deletions(-)

diff --git a/extra/Configs/Config.in b/extra/Configs/Config.in
index 6bbb6f572..c2834f5ec 100644
--- a/extra/Configs/Config.in
+++ b/extra/Configs/Config.in
@@ -628,6 +628,15 @@ config PTHREADS_DEBUG_SUPPORT
          If you are doing development and want to debug applications using
          uClibc's pthread library, answer Y.  Otherwise, answer N.
 
+config PTHREADS_STACK_DEFAULT_SIZE
+       int "Default thread stack size"
+       default 4194304 if TARGET_alpha # 4 MiB
+       default 4194304 if TARGET_powerpc # 4 MiB
+       default 2097152 # 2 MiB
+       help
+         Set the default thread stack size. This option is useful on MMU-less
+         systems where the stack size is fixed and the default stack size may
+         be excessively large and waste memory.
 
 config UCLIBC_HAS_SYSLOG
        bool "Syslog support"
diff --git a/libpthread/nptl/init.c b/libpthread/nptl/init.c
index 5d25ded7d..ddc552f2e 100644
--- a/libpthread/nptl/init.c
+++ b/libpthread/nptl/init.c
@@ -278,17 +278,17 @@ __pthread_initialize_minimal_internal (void)
   struct rlimit limit;
   if (getrlimit (RLIMIT_STACK, &limit) != 0
       || limit.rlim_cur == RLIM_INFINITY)
-    /* The system limit is not usable.  Use an architecture-specific
-       default.  */
-    limit.rlim_cur = ARCH_STACK_DEFAULT_SIZE;
-  else if (limit.rlim_cur < PTHREAD_STACK_MIN)
+    /* The system limit is not usable.  Use a user-specified or
+       architecture-specific default.  */
+    limit.rlim_cur = __PTHREADS_STACK_DEFAULT_SIZE__;
+  if (limit.rlim_cur < PTHREAD_STACK_MIN)
     /* The system limit is unusably small.
        Use the minimal size acceptable.  */
     limit.rlim_cur = PTHREAD_STACK_MIN;
 
-  /* Do not exceed architecture specific default */
-  if (limit.rlim_cur > ARCH_STACK_DEFAULT_SIZE)
-    limit.rlim_cur = ARCH_STACK_DEFAULT_SIZE;
+  /* Do not exceed the user-specified or architecture-specific default */
+  if (limit.rlim_cur > __PTHREADS_STACK_DEFAULT_SIZE__)
+    limit.rlim_cur = __PTHREADS_STACK_DEFAULT_SIZE__;
 
   /* Make sure it meets the minimum size that allocate_stack
      (allocatestack.c) will demand, which depends on the page size.  */
diff --git a/libpthread/nptl/sysdeps/aarch64/pthreaddef.h 
b/libpthread/nptl/sysdeps/aarch64/pthreaddef.h
index d9495f9cb..7172f406b 100644
--- a/libpthread/nptl/sysdeps/aarch64/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/aarch64/pthreaddef.h
@@ -14,9 +14,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE        (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN 16
 
diff --git a/libpthread/nptl/sysdeps/alpha/pthreaddef.h 
b/libpthread/nptl/sysdeps/alpha/pthreaddef.h
index 72a311c33..6b99f3b4f 100644
--- a/libpthread/nptl/sysdeps/alpha/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/alpha/pthreaddef.h
@@ -15,9 +15,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE        (4 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  The ABI requires 16.  */
 #define STACK_ALIGN            16
 
diff --git a/libpthread/nptl/sysdeps/arc/pthreaddef.h 
b/libpthread/nptl/sysdeps/arc/pthreaddef.h
index bf4f0f29a..0fb28dc48 100644
--- a/libpthread/nptl/sysdeps/arc/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/arc/pthreaddef.h
@@ -17,9 +17,6 @@
 
 #include <sysdep.h>
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE        (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN            8
 
diff --git a/libpthread/nptl/sysdeps/arm/pthreaddef.h 
b/libpthread/nptl/sysdeps/arm/pthreaddef.h
index a05ac879d..f790a6dca 100644
--- a/libpthread/nptl/sysdeps/arm/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/arm/pthreaddef.h
@@ -15,9 +15,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE        (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  SSE requires 16
    bytes.  */
 #define STACK_ALIGN            16
diff --git a/libpthread/nptl/sysdeps/csky/pthreaddef.h 
b/libpthread/nptl/sysdeps/csky/pthreaddef.h
index 992fced01..4aa97ee27 100644
--- a/libpthread/nptl/sysdeps/csky/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/csky/pthreaddef.h
@@ -5,9 +5,6 @@
  * in this tarball.
  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE        (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  SSE requires 16
    bytes.  */
 #define STACK_ALIGN            16
diff --git a/libpthread/nptl/sysdeps/i386/pthreaddef.h 
b/libpthread/nptl/sysdeps/i386/pthreaddef.h
index a0659039d..2fd27113c 100644
--- a/libpthread/nptl/sysdeps/i386/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/i386/pthreaddef.h
@@ -16,9 +16,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE        (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  SSE requires 16
    bytes.  */
 #define STACK_ALIGN            16
diff --git a/libpthread/nptl/sysdeps/kvx/pthreaddef.h 
b/libpthread/nptl/sysdeps/kvx/pthreaddef.h
index 6e1485998..03945bc5f 100644
--- a/libpthread/nptl/sysdeps/kvx/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/kvx/pthreaddef.h
@@ -6,9 +6,6 @@
  * Copyright (C) 2019 Kalray Inc.
  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE        (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN 32
 
diff --git a/libpthread/nptl/sysdeps/m68k/pthreaddef.h 
b/libpthread/nptl/sysdeps/m68k/pthreaddef.h
index 1651b3d5f..04d565191 100644
--- a/libpthread/nptl/sysdeps/m68k/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/m68k/pthreaddef.h
@@ -15,9 +15,6 @@
    License along with the GNU C Library.  If not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE        (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN            16
 
diff --git a/libpthread/nptl/sysdeps/metag/pthreaddef.h 
b/libpthread/nptl/sysdeps/metag/pthreaddef.h
index bf4f0f29a..0fb28dc48 100644
--- a/libpthread/nptl/sysdeps/metag/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/metag/pthreaddef.h
@@ -17,9 +17,6 @@
 
 #include <sysdep.h>
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE        (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN            8
 
diff --git a/libpthread/nptl/sysdeps/microblaze/pthreaddef.h 
b/libpthread/nptl/sysdeps/microblaze/pthreaddef.h
index 47e87dd71..a01b59fab 100644
--- a/libpthread/nptl/sysdeps/microblaze/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/microblaze/pthreaddef.h
@@ -19,9 +19,6 @@
 #include <stdlib.h>
 #include <string.h>
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE  (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN         16
 
diff --git a/libpthread/nptl/sysdeps/mips/pthreaddef.h 
b/libpthread/nptl/sysdeps/mips/pthreaddef.h
index 692988205..adedd7715 100644
--- a/libpthread/nptl/sysdeps/mips/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/mips/pthreaddef.h
@@ -15,9 +15,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE        (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN            16
 
diff --git a/libpthread/nptl/sysdeps/nds32/pthreaddef.h 
b/libpthread/nptl/sysdeps/nds32/pthreaddef.h
index c9d3f7781..0dca16c34 100644
--- a/libpthread/nptl/sysdeps/nds32/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/nds32/pthreaddef.h
@@ -14,9 +14,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE        (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  SSE requires 16
    bytes.  */
 #define STACK_ALIGN            16
diff --git a/libpthread/nptl/sysdeps/nios2/pthreaddef.h 
b/libpthread/nptl/sysdeps/nios2/pthreaddef.h
index 4268252dd..5be435237 100644
--- a/libpthread/nptl/sysdeps/nios2/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/nios2/pthreaddef.h
@@ -16,9 +16,6 @@
    License along with the GNU C Library.  If not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE        (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN            4
 
diff --git a/libpthread/nptl/sysdeps/or1k/pthreaddef.h 
b/libpthread/nptl/sysdeps/or1k/pthreaddef.h
index e8da3d965..394962172 100644
--- a/libpthread/nptl/sysdeps/or1k/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/or1k/pthreaddef.h
@@ -16,9 +16,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN 16
 
diff --git a/libpthread/nptl/sysdeps/powerpc/pthreaddef.h 
b/libpthread/nptl/sysdeps/powerpc/pthreaddef.h
index 36bf76404..a46c094b6 100644
--- a/libpthread/nptl/sysdeps/powerpc/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/powerpc/pthreaddef.h
@@ -15,9 +15,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE        (4 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  The ABI requires 16
    bytes (for both 32-bit and 64-bit PowerPC).  */
 #define STACK_ALIGN            16
diff --git a/libpthread/nptl/sysdeps/riscv64/pthreaddef.h 
b/libpthread/nptl/sysdeps/riscv64/pthreaddef.h
index fbd40a74f..5a929bc77 100644
--- a/libpthread/nptl/sysdeps/riscv64/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/riscv64/pthreaddef.h
@@ -14,9 +14,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN 16
 
diff --git a/libpthread/nptl/sysdeps/sh/pthreaddef.h 
b/libpthread/nptl/sysdeps/sh/pthreaddef.h
index fc3ae6029..918aaf54f 100644
--- a/libpthread/nptl/sysdeps/sh/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/sh/pthreaddef.h
@@ -17,9 +17,6 @@
 
 #include <sysdep.h>
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE        (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN            8
 
diff --git a/libpthread/nptl/sysdeps/sparc/pthreaddef.h 
b/libpthread/nptl/sysdeps/sparc/pthreaddef.h
index 435fedcf3..65f6655a4 100644
--- a/libpthread/nptl/sysdeps/sparc/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/sparc/pthreaddef.h
@@ -15,9 +15,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE        (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN            16
 
diff --git a/libpthread/nptl/sysdeps/x86_64/pthreaddef.h 
b/libpthread/nptl/sysdeps/x86_64/pthreaddef.h
index 2b2285285..98ab8cfb1 100644
--- a/libpthread/nptl/sysdeps/x86_64/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/x86_64/pthreaddef.h
@@ -16,9 +16,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE        (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  SSE requires 16
    bytes.  */
 #define STACK_ALIGN            16
diff --git a/libpthread/nptl/sysdeps/xtensa/pthreaddef.h 
b/libpthread/nptl/sysdeps/xtensa/pthreaddef.h
index 34c1851c3..13d8fef4c 100644
--- a/libpthread/nptl/sysdeps/xtensa/pthreaddef.h
+++ b/libpthread/nptl/sysdeps/xtensa/pthreaddef.h
@@ -15,9 +15,6 @@
    License along with the GNU C Library; see the file COPYING.LIB.  If
    not, see <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE        (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN            16
 
-- 
2.42.0

_______________________________________________
devel mailing list -- devel@uclibc-ng.org
To unsubscribe send an email to devel-le...@uclibc-ng.org

Reply via email to