[Openmp-commits] [PATCH] D128466: [OpenMP] Use /proc/self instead of /proc/<getpid()> to get mapped addresses

Leon Yang via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Thu Jun 23 14:28:18 PDT 2022


lnyng created this revision.
lnyng added reviewers: AndreyChurbanov, jdoerfert.
lnyng added a project: LLVM.
Herald added subscribers: guansong, yaxunl.
Herald added a project: All.
lnyng requested review of this revision.
Herald added subscribers: openmp-commits, sstefan1.
Herald added a project: OpenMP.

The original code may fail on permission deny if it's called by a process that just enters a new pid namespace but before the procfs is remounted. Changing to use /proc/self fixes it because the magic symlink always points to the right pid directory regardless of the process's current pid namespace.

For example, to create a container, we may want to have the init process enter some new namespaces like pid and mount, and remount procfs. Say its host pid is 10000 and container pid is 1. It may see the procfs from the host for a short period of time while getpid() returns 1. If openmp is used during that period, it will access /proc/1 and get permission deny. Using /proc/self works before (points to /proc/10000) and after (to /proc/1) remounting procfs.

There is no strong requirement to use openmp before procfs is remounted, but it's good to cover more edge cases without compromising common cases.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128466

Files:
  openmp/runtime/src/z_Linux_util.cpp


Index: openmp/runtime/src/z_Linux_util.cpp
===================================================================
--- openmp/runtime/src/z_Linux_util.cpp
+++ openmp/runtime/src/z_Linux_util.cpp
@@ -2018,13 +2018,12 @@

 #if KMP_OS_LINUX || KMP_OS_HURD

-  /* On GNUish OSes, read the /proc/<pid>/maps pseudo-file to get all the
+  /* On GNUish OSes, read the /proc/self/maps pseudo-file to get all the
      address ranges mapped into the address space. */

-  char *name = __kmp_str_format("/proc/%d/maps", getpid());
   FILE *file = NULL;

-  file = fopen(name, "r");
+  file = fopen("/proc/self/maps", "r");
   KMP_ASSERT(file != NULL);

   for (;;) {
@@ -2053,7 +2052,6 @@

   // Free resources.
   fclose(file);
-  KMP_INTERNAL_FREE(name);
 #elif KMP_OS_FREEBSD
   char *buf;
   size_t lstsz;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128466.439527.patch
Type: text/x-patch
Size: 795 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20220623/737cbc9c/attachment.bin>


More information about the Openmp-commits mailing list