[Openmp-commits] [PATCH] D142175: [OpenMP] Create a temp file in /tmp if /dev/shm is not accessible
Shilei Tian via Phabricator via Openmp-commits
openmp-commits at lists.llvm.org
Thu Jan 19 21:18:42 PST 2023
tianshilei1992 created this revision.
tianshilei1992 added reviewers: jdoerfert, AndreyChurbanov, tlwilmar, jlpeyton.
Herald added subscribers: guansong, yaxunl.
Herald added a project: All.
tianshilei1992 requested review of this revision.
Herald added subscribers: openmp-commits, sstefan1.
Herald added a project: OpenMP.
When `libomp` is initialized, it creates a temp file in `/dev/shm` to store
registration flag. Some systems, like Android, doesn't have `/dev/shm`, this
feature is disabled by the macro `KMP_USE_SHM`, though most Linux distribution
has that. However, some customized distribution, such as the one reported in
https://github.com/llvm/llvm-project/issues/53955, doesn't support it either.
It causes a core dump. In this patch, if it is the case, we will try to create a
temporary file in `/tmp`, and if it still doesn't make it, then we error out.
Note that we don't consider in this patch if the temporary directory has been
set to `TMPDIR` in this patch. If `/tmp` is not accessible, we error out.
Fix #53955.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D142175
Files:
openmp/runtime/src/kmp_runtime.cpp
Index: openmp/runtime/src/kmp_runtime.cpp
===================================================================
--- openmp/runtime/src/kmp_runtime.cpp
+++ openmp/runtime/src/kmp_runtime.cpp
@@ -24,6 +24,7 @@
#include "kmp_wait_release.h"
#include "kmp_wrapper_getpid.h"
#include "kmp_dispatch.h"
+#include <cstdio>
#if KMP_USE_HIER_SCHED
#include "kmp_dispatch_hier.h"
#endif
@@ -6739,6 +6740,11 @@
#endif
} // __kmp_reg_status_get
+#if defined(KMP_USE_SHM)
+// If /dev/shm is not accessible, we will create a temporary file under /tmp.
+char *temp_reg_status_file_name = nullptr;
+#endif
+
void __kmp_register_library_startup(void) {
char *name = __kmp_reg_status_name(); // Name of the environment variable.
@@ -6781,10 +6787,17 @@
shm_preexist = 1;
}
} else if (fd1 == -1) { // SHM didn't open; it was due to error other than
- // already exists.
- // error out here.
- __kmp_fatal(KMP_MSG(FunctionError, "Can't open SHM2"), KMP_ERR(errno),
- __kmp_msg_null);
+ // Already exists. Try to create a temp file under /tmp.
+ // TODO: /tmp might not always be the temporary directory. For now we will
+ // not consider TMPDIR. If /tmp is not accessible, we simply error out.
+ char *temp_file_name = __kmp_str_format("/tmp/%sXXXXXX", name);
+ fd1 = mkstemp(temp_file_name);
+ if (fd1 == -1) {
+ // error out here.
+ __kmp_fatal(KMP_MSG(FunctionError, "Can't open TEMP"), KMP_ERR(errno),
+ __kmp_msg_null);
+ }
+ temp_reg_status_file_name = temp_file_name;
}
if (shm_preexist == 0) {
// we created SHM now set size
@@ -6896,11 +6909,18 @@
char *value = NULL;
#if defined(KMP_USE_SHM)
+ bool use_shm = true;
char *shm_name = __kmp_str_format("/%s", name);
int fd1 = shm_open(shm_name, O_RDONLY, 0666);
if (fd1 == -1) {
- // file did not open. return.
- return;
+ // file did not open. Try the temporary file.
+ use_shm = false;
+ FILE *tf = fopen(temp_reg_status_file_name, O_RDONLY);
+ if (!tf) {
+ // give it up now.
+ return;
+ }
+ fd1 = fileno(tf);
}
char *data1 = (char *)mmap(0, SHM_SIZE, PROT_READ, MAP_SHARED, fd1, 0);
if (data1 != MAP_FAILED) {
@@ -6917,7 +6937,12 @@
if (value != NULL && strcmp(value, __kmp_registration_str) == 0) {
// Ok, this is our variable. Delete it.
#if defined(KMP_USE_SHM)
- shm_unlink(shm_name); // this removes file in /dev/shm
+ if (use_shm) {
+ shm_unlink(shm_name); // this removes file in /dev/shm
+ } else {
+ KMP_DEBUG_ASSERT(temp_reg_status_file_name);
+ unlink(temp_reg_status_file_name); // this removes the temp file
+ }
#else
__kmp_env_unset(name);
#endif
@@ -6925,6 +6950,10 @@
#if defined(KMP_USE_SHM)
KMP_INTERNAL_FREE(shm_name);
+ if (!use_shm) {
+ KMP_DEBUG_ASSERT(temp_reg_status_file_name);
+ KMP_INTERNAL_FREE(temp_reg_status_file_name);
+ }
#endif
KMP_INTERNAL_FREE(__kmp_registration_str);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142175.490715.patch
Type: text/x-patch
Size: 3021 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20230120/36594c22/attachment.bin>
More information about the Openmp-commits
mailing list