aboutsummaryrefslogtreecommitdiff
path: root/openmp/runtime
diff options
context:
space:
mode:
authorAndreyChurbanov <andrey.churbanov@intel.com>2022-05-19 12:06:01 -0500
committerJonathan Peyton <jonathan.l.peyton@intel.com>2022-07-19 12:59:58 -0500
commita01d274fbd44b49bf6c5af9cce49f706b31fab76 (patch)
treea4faf69c16b31d5bc909c8612088b5ef60c995f4 /openmp/runtime
parent6fb27bc2e3eac45d597bfc9285e5e1cb8ca16a60 (diff)
downloadllvm-a01d274fbd44b49bf6c5af9cce49f706b31fab76.zip
llvm-a01d274fbd44b49bf6c5af9cce49f706b31fab76.tar.gz
llvm-a01d274fbd44b49bf6c5af9cce49f706b31fab76.tar.bz2
[OpenMP][libomp] Fix /dev/shm pollution after forked child process terminates
Made library registration conditional and skip it in the __kmp_atfork_child handler, postponed it till middle initialization in the child. This fixes the problem of applications those use e.g. popen/pclose which terminate the forked child process. Differential Revision: https://reviews.llvm.org/D125996
Diffstat (limited to 'openmp/runtime')
-rw-r--r--openmp/runtime/src/kmp.h1
-rw-r--r--openmp/runtime/src/kmp_global.cpp1
-rw-r--r--openmp/runtime/src/kmp_runtime.cpp16
-rw-r--r--openmp/runtime/src/z_Linux_util.cpp8
4 files changed, 21 insertions, 5 deletions
diff --git a/openmp/runtime/src/kmp.h b/openmp/runtime/src/kmp.h
index 61ec737..2efe120 100644
--- a/openmp/runtime/src/kmp.h
+++ b/openmp/runtime/src/kmp.h
@@ -3070,6 +3070,7 @@ static inline bool __kmp_is_hybrid_cpu() { return false; }
extern volatile int __kmp_init_serial;
extern volatile int __kmp_init_gtid;
extern volatile int __kmp_init_common;
+extern volatile int __kmp_need_register_serial;
extern volatile int __kmp_init_middle;
extern volatile int __kmp_init_parallel;
#if KMP_USE_MONITOR
diff --git a/openmp/runtime/src/kmp_global.cpp b/openmp/runtime/src/kmp_global.cpp
index 99a6e88..7a3e3b0 100644
--- a/openmp/runtime/src/kmp_global.cpp
+++ b/openmp/runtime/src/kmp_global.cpp
@@ -44,6 +44,7 @@ tsc_tick_count __kmp_stats_start_time;
volatile int __kmp_init_serial = FALSE;
volatile int __kmp_init_gtid = FALSE;
volatile int __kmp_init_common = FALSE;
+volatile int __kmp_need_register_serial = TRUE;
volatile int __kmp_init_middle = FALSE;
volatile int __kmp_init_parallel = FALSE;
volatile int __kmp_init_hidden_helper = FALSE;
diff --git a/openmp/runtime/src/kmp_runtime.cpp b/openmp/runtime/src/kmp_runtime.cpp
index 2b339fd..744a241f 100644
--- a/openmp/runtime/src/kmp_runtime.cpp
+++ b/openmp/runtime/src/kmp_runtime.cpp
@@ -6965,10 +6965,12 @@ static void __kmp_do_serial_initialize(void) {
/* Initialize internal memory allocator */
__kmp_init_allocator();
- /* Register the library startup via an environment variable and check to see
- whether another copy of the library is already registered. */
-
- __kmp_register_library_startup();
+ /* Register the library startup via an environment variable or via mapped
+ shared memory file and check to see whether another copy of the library is
+ already registered. Since forked child process is often terminated, we
+ postpone the registration till middle initialization in the child */
+ if (__kmp_need_register_serial)
+ __kmp_register_library_startup();
/* TODO reinitialization of library */
if (TCR_4(__kmp_global.g.g_done)) {
@@ -7255,6 +7257,12 @@ static void __kmp_do_middle_initialize(void) {
KA_TRACE(10, ("__kmp_middle_initialize: enter\n"));
+ if (UNLIKELY(!__kmp_need_register_serial)) {
+ // We are in a forked child process. The registration was skipped during
+ // serial initialization in __kmp_atfork_child handler. Do it here.
+ __kmp_register_library_startup();
+ }
+
// Save the previous value for the __kmp_dflt_team_nth so that
// we can avoid some reinitialization if it hasn't changed.
prev_dflt_team_nth = __kmp_dflt_team_nth;
diff --git a/openmp/runtime/src/z_Linux_util.cpp b/openmp/runtime/src/z_Linux_util.cpp
index 5cd6ad6..91edf025 100644
--- a/openmp/runtime/src/z_Linux_util.cpp
+++ b/openmp/runtime/src/z_Linux_util.cpp
@@ -1297,7 +1297,13 @@ static void __kmp_atfork_child(void) {
__kmp_itt_reset(); // reset ITT's global state
#endif /* USE_ITT_BUILD */
- __kmp_serial_initialize();
+ {
+ // Child process often get terminated without any use of OpenMP. That might
+ // cause mapped shared memory file to be left unattended. Thus we postpone
+ // library registration till middle initialization in the child process.
+ __kmp_need_register_serial = FALSE;
+ __kmp_serial_initialize();
+ }
/* This is necessary to make sure no stale data is left around */
/* AC: customers complain that we use unsafe routines in the atfork