From 4f4d21519635934f9e2dcc03dc28cd3889aed45b Mon Sep 17 00:00:00 2001 From: "Shweta.Khatri" Date: Tue, 26 Mar 2024 12:40:55 -0400 Subject: [PATCH] Fixing compilation errors related to MUSL libc Fix Musl libc NULL errors and unsupported pthread funcs for compatibility. Also ensures cleanup and error handling irrespective of CPU affinity override. Fix submitted by github dev - AngryLoki https://github.com/ROCm/ROCR-Runtime/issues/181 Change-Id: Ia487315e504112be5d3370756f23f6e23b9ae4be [ROCm/ROCR-Runtime commit: bc9cac97fe3623b4c4b8e61b6cfe9bb2c9df4377] --- .../runtime/hsa-runtime/core/inc/checked.h | 2 +- .../core/runtime/default_signal.cpp | 2 +- .../hsa-runtime/core/util/lnx/os_linux.cpp | 41 ++++++++++++++----- .../runtime/hsa-runtime/core/util/utils.h | 3 +- .../runtime/hsa-runtime/image/util.h | 2 +- 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/checked.h b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/checked.h index 93793bcc78..56497d120a 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/checked.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/checked.h @@ -58,7 +58,7 @@ template class Check final { Check(const Check&) { object_ = uintptr_t(this) ^ uintptr_t(code); } Check(Check&&) { object_ = uintptr_t(this) ^ uintptr_t(code); } - ~Check() { object_ = NULL; } + ~Check() { object_ = uintptr_t(NULL); } const Check& operator=(Check&& rhs) { return *this; } const Check& operator=(const Check& rhs) { return *this; } diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/default_signal.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/default_signal.cpp index 820fc75cab..b3e5a23f28 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/default_signal.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/default_signal.cpp @@ -57,7 +57,7 @@ int BusyWaitSignal::rtti_id_ = 0; BusyWaitSignal::BusyWaitSignal(SharedSignal* abi_block, bool enableIPC) : Signal(abi_block, enableIPC) { signal_.kind = AMD_SIGNAL_KIND_USER; - signal_.event_mailbox_ptr = NULL; + signal_.event_mailbox_ptr = uint64_t(NULL); } hsa_signal_value_t BusyWaitSignal::LoadRelaxed() { diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp index 7850490e22..fb70b81171 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp @@ -108,31 +108,44 @@ class os_thread { err = pthread_attr_setstacksize(&attrib, stackSize); if (err != 0) { fprintf(stderr, "pthread_attr_setstacksize failed: %s\n", strerror(err)); + err = pthread_attr_destroy(&attrib); + if (err != 0) { + fprintf(stderr, "pthread_attr_destroy failed: %s\n", strerror(err)); + } return; } } -\ - err = pthread_create(&thread, &attrib, ThreadTrampoline, args.get()); + + int create_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) { + if ((create_err == EINVAL) && stackSize != 0) { while (stackSize < 20 * 1024 * 1024) { stackSize *= 2; err = pthread_attr_setstacksize(&attrib, stackSize); if (err != 0) { fprintf(stderr, "pthread_attr_setstacksize failed: %s\n", strerror(err)); + err = pthread_attr_destroy(&attrib); + if (err != 0) { + fprintf(stderr, "pthread_attr_destroy failed: %s\n", strerror(err)); + } + return; } - err = pthread_create(&thread, &attrib, ThreadTrampoline, args.get()); - if (err != EINVAL) break; + + create_err = pthread_create(&thread, &attrib, ThreadTrampoline, args.get()); + if (create_err != EINVAL) break; debug_print("pthread_create returned EINVAL, doubling stack size\n"); } } + int cores = 0; + cpu_set_t* cpuset = nullptr; + if (core::Runtime::runtime_singleton_->flag().override_cpu_affinity()) { - int cores = get_nprocs_conf(); - cpu_set_t* cpuset = CPU_ALLOC(cores); + cores = get_nprocs_conf(); + cpuset = CPU_ALLOC(cores); if (cpuset == nullptr) { fprintf(stderr, "CPU_ALLOC failed: %s\n", strerror(errno)); return; @@ -144,12 +157,11 @@ class os_thread { err = pthread_setaffinity_np(thread, CPU_ALLOC_SIZE(cores), cpuset); CPU_FREE(cpuset); if (err != 0) { - fprintf(stderr, "pthread_attr_setaffinity_np failed: %s\n", strerror(err)); + fprintf(stderr, "pthread_setaffinity_np failed: %s\n", strerror(err)); return; } } - - if (err == 0) + if (create_err == 0) args.release(); else thread = 0; @@ -642,11 +654,20 @@ SharedMutex CreateSharedMutex() { fprintf(stderr, "rw lock attribute init failed: %s\n", strerror(err)); return nullptr; } + +#ifdef __GLIBC__ err = pthread_rwlockattr_setkind_np(&attrib, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); if (err != 0) { fprintf(stderr, "Set rw lock attribute failure: %s\n", strerror(err)); return nullptr; } +#else + err = pthread_rwlockattr_setkind(&attrib, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); + if (err != 0) { + fprintf(stderr, "Set rw lock attribute failure: %s\n", strerror(err)); + return nullptr; + } +#endif pthread_rwlock_t* lock = new pthread_rwlock_t; err = pthread_rwlock_init(lock, &attrib); diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/util/utils.h b/projects/rocr-runtime/runtime/hsa-runtime/core/util/utils.h index ab536ba796..1a454d7bd4 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/util/utils.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/util/utils.h @@ -74,8 +74,7 @@ static __forceinline void* _aligned_malloc(size_t size, size_t alignment) { return aligned_alloc(alignment, size); #else void *mem = NULL; - if (NULL != posix_memalign(&mem, alignment, size)) - return NULL; + if (0 != posix_memalign(&mem, alignment, size)) return NULL; return mem; #endif } diff --git a/projects/rocr-runtime/runtime/hsa-runtime/image/util.h b/projects/rocr-runtime/runtime/hsa-runtime/image/util.h index 8482e41a4b..88cdf4ccc9 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/image/util.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/image/util.h @@ -99,7 +99,7 @@ static __forceinline void* _aligned_malloc(size_t size, size_t alignment) { return aligned_alloc(alignment, size); #else void* mem = NULL; - if (NULL != posix_memalign(&mem, alignment, size)) return NULL; + if (0 != posix_memalign(&mem, alignment, size)) return NULL; return mem; #endif }