[Openmp-commits] [openmp] Fix re-locking hang found in issue 86684 (PR #88539)

Jonathan Peyton via Openmp-commits openmp-commits at lists.llvm.org
Fri Apr 12 10:26:22 PDT 2024


https://github.com/jpeyton52 created https://github.com/llvm/llvm-project/pull/88539

This was initially reported here (including stacktraces): https://stackoverflow.com/questions/78183545/does-compiling-imagick-with-openmp-enabled-in-freebsd-13-2-cause-sched-yield

If `__kmp_register_library_startup()` detects that another instance of the library is present, `__kmp_is_address_mapped()` is eventually called. which uses `kmpc_alloc()` to allocate memory. This function calls `__kmp_entry_thread()` to access the thread-local memory pool, which is a bad idea during initialization. This macro internally calls `__kmp_get_global_thread_id_reg()` which sets the bootstrap lock at the beginning (before calling `__kmp_register_library_startup()`).

The fix is to use `KMP_INTERNAL_MALLOC()`/`KMP_INTERNAL_FREE()` instead of `kmpc_malloc()`/`kmpc_free()`. `KMP_INTERNAL_MALLOC` and `KMP_INTERNAL_FREE` do not use any bootstrap locks. They just translate to `malloc()`/`free()` and are meant to be used during library initialization before other library-specific allocators have been initialized.

Fixes: #86684

>From e9d5bf9dd6e876755ed1a152c7631e5239d44e0f Mon Sep 17 00:00:00 2001
From: Jonathan Peyton <jonathan.l.peyton at intel.com>
Date: Fri, 12 Apr 2024 11:17:57 -0500
Subject: [PATCH] Fix re-locking hang found in issue 86684

This was initially reported here (including stacktraces):
https://stackoverflow.com/questions/78183545/does-compiling-
imagick-with-openmp-enabled-in-freebsd-13-2-cause-sched-yield

If __kmp_register_library_startup detects that another instance of the
library is present, __kmp_is_address_mapped is eventually called. It
calls __kmp_str_format, which uses kmpc_alloc to allocate memory for
the string. This function calls __kmp_entry_thread to access the
thread-local memory pool, which seems a bad idea during initialization.
This macro internally calls __kmp_get_global_thread_id_reg which set the
bootstrap look at the beginning (before calling
__kmp_register_library_startup).

The fix is to use KMP_INTERNAL_MALLOC/FREE instead of kmpc_malloc/free.
KMP_INTERNAL_MALLOC and KMP_INTERNAL_FREE do not use any bootstrap lock.
They just translate to malloc()/free().

Fixes: #86684
---
 openmp/runtime/src/z_Linux_util.cpp | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/openmp/runtime/src/z_Linux_util.cpp b/openmp/runtime/src/z_Linux_util.cpp
index be47be0dcec2af..11ce083f4801d3 100644
--- a/openmp/runtime/src/z_Linux_util.cpp
+++ b/openmp/runtime/src/z_Linux_util.cpp
@@ -2141,10 +2141,10 @@ int __kmp_is_address_mapped(void *addr) {
   // We pass from number of vm entry's semantic
   // to size of whole entry map list.
   lstsz = lstsz * 4 / 3;
-  buf = reinterpret_cast<char *>(kmpc_malloc(lstsz));
+  buf = reinterpret_cast<char *>(KMP_INTERNAL_MALLOC(lstsz));
   rc = sysctl(mib, 4, buf, &lstsz, NULL, 0);
   if (rc < 0) {
-    kmpc_free(buf);
+    KMP_INTERNAL_FREE(buf);
     return 0;
   }
 
@@ -2168,7 +2168,7 @@ int __kmp_is_address_mapped(void *addr) {
     }
     lw += cursz;
   }
-  kmpc_free(buf);
+  KMP_INTERNAL_FREE(buf);
 #elif KMP_OS_DRAGONFLY
   char err[_POSIX2_LINE_MAX];
   kinfo_proc *proc;
@@ -2234,12 +2234,12 @@ int __kmp_is_address_mapped(void *addr) {
     return 0;
   }
 
-  buf = kmpc_malloc(sz);
+  buf = KMP_INTERNAL_MALLOC(sz);
 
   while (sz > 0 && (rd = pread(file, buf, sz, 0)) == sz) {
     void *newbuf;
     sz <<= 1;
-    newbuf = kmpc_realloc(buf, sz);
+    newbuf = KMP_INTERNAL_REALLOC(buf, sz);
     buf = newbuf;
   }
 
@@ -2255,7 +2255,7 @@ int __kmp_is_address_mapped(void *addr) {
     }
   }
 
-  kmpc_free(map);
+  KMP_INTERNAL_FREE(map);
   close(file);
   KMP_INTERNAL_FREE(name);
 #elif KMP_OS_DARWIN



More information about the Openmp-commits mailing list