From 0c0e634458a63e7a64bcedf95f17d6918eb49082 Mon Sep 17 00:00:00 2001 From: Sean Keely Date: Mon, 17 Jun 2019 21:02:33 -0500 Subject: [PATCH] PTHREAD_STACK_MIN may differ from system parameters. Restrict stack adjustment to non-default stack requests and allow stack growth within reason (20MB cutoff). Change-Id: I320280c711402ac29683e94c7246b7c32c797611 --- runtime/hsa-runtime/core/util/lnx/os_linux.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/runtime/hsa-runtime/core/util/lnx/os_linux.cpp b/runtime/hsa-runtime/core/util/lnx/os_linux.cpp index c46282524d..24974185ad 100644 --- a/runtime/hsa-runtime/core/util/lnx/os_linux.cpp +++ b/runtime/hsa-runtime/core/util/lnx/os_linux.cpp @@ -88,17 +88,29 @@ class os_thread { args->entry_args = threadArgument; args->entry_function = function; - stackSize = Max(uint(PTHREAD_STACK_MIN), stackSize); - stackSize = AlignUp(stackSize, 4096); pthread_attr_t attrib; pthread_attr_init(&attrib); if (stackSize != 0) { + stackSize = Max(uint(PTHREAD_STACK_MIN), stackSize); + stackSize = AlignUp(stackSize, 4096); int err = pthread_attr_setstacksize(&attrib, stackSize); assert(err == 0 && "pthread_attr_setstacksize failed."); } int err = pthread_create(&thread, &attrib, ThreadTrampoline, args.get()); + + // Probably a stack size error since system limits can be different from PTHREAD_STACK_MIN + // Attempt to grow the stack within reason. + if ((err == EINVAL) && stackSize != 0) { + while (stackSize < 20 * 1024 * 1024) { + stackSize *= 2; + pthread_attr_setstacksize(&attrib, stackSize); + err = pthread_create(&thread, &attrib, ThreadTrampoline, args.get()); + if (err != EINVAL) break; + } + } + pthread_attr_destroy(&attrib); if (err == 0) args.release();