[libc-commits] [libc] 6a7cd4a - [libc][NFC] Do not call mmap and munmap from thread functions.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Fri Apr 1 22:12:15 PDT 2022


Author: Siva Chandra Reddy
Date: 2022-04-02T05:12:08Z
New Revision: 6a7cd4a1df6c394f5d0dae93c9be26f8f777cd7f

URL: https://github.com/llvm/llvm-project/commit/6a7cd4a1df6c394f5d0dae93c9be26f8f777cd7f
DIFF: https://github.com/llvm/llvm-project/commit/6a7cd4a1df6c394f5d0dae93c9be26f8f777cd7f.diff

LOG: [libc][NFC] Do not call mmap and munmap from thread functions.

Instead, memory is allocated and deallocated using mmap and munmap
syscalls directly.

Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D122876

Added: 
    

Modified: 
    libc/src/threads/linux/CMakeLists.txt
    libc/src/threads/linux/thrd_create.cpp
    libc/src/threads/linux/thrd_join.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/threads/linux/CMakeLists.txt b/libc/src/threads/linux/CMakeLists.txt
index 0941711432c26..0abdf5d23363f 100644
--- a/libc/src/threads/linux/CMakeLists.txt
+++ b/libc/src/threads/linux/CMakeLists.txt
@@ -35,12 +35,11 @@ add_entrypoint_object(
   DEPENDS
     .threads_utils
     libc.include.errno
+    libc.include.sys_mman
     libc.include.sys_syscall
     libc.include.threads
     libc.src.__support.common
     libc.src.__support.OSUtil.osutil
-    libc.src.errno.errno
-    libc.src.sys.mman.mmap
   COMPILE_OPTIONS
     -O3
     -fno-omit-frame-pointer # This allows us to sniff out the thread args from
@@ -60,7 +59,6 @@ add_entrypoint_object(
     libc.src.__support.CPP.atomic
     libc.src.__support.common
     libc.src.__support.OSUtil.osutil
-    libc.src.sys.mman.munmap
 )
 
 add_entrypoint_object(

diff  --git a/libc/src/threads/linux/thrd_create.cpp b/libc/src/threads/linux/thrd_create.cpp
index e89337471fcab..5f25ded38bd39 100644
--- a/libc/src/threads/linux/thrd_create.cpp
+++ b/libc/src/threads/linux/thrd_create.cpp
@@ -8,21 +8,26 @@
 
 #include "Futex.h"
 
-#include "include/errno.h"                // For E* error values.
-#include "include/sys/mman.h"             // For PROT_* and MAP_* definitions.
-#include "include/sys/syscall.h"          // For syscall numbers.
-#include "include/threads.h"              // For thrd_* type definitions.
 #include "src/__support/OSUtil/syscall.h" // For syscall function.
 #include "src/__support/architectures.h"
 #include "src/__support/common.h"
-#include "src/errno/llvmlibc_errno.h"
-#include "src/sys/mman/mmap.h"
-#include "src/sys/mman/munmap.h"
 #include "src/threads/linux/Thread.h"
 #include "src/threads/thrd_create.h"
 
+#include <errno.h>       // For E* error values.
 #include <linux/sched.h> // For CLONE_* flags.
 #include <stdint.h>
+#include <sys/mman.h>    // For PROT_* and MAP_* definitions.
+#include <sys/syscall.h> // For syscall numbers.
+#include <threads.h>     // For thrd_* type definitions.
+
+#ifdef SYS_mmap2
+constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap2;
+#elif SYS_mmap
+constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap;
+#else
+#error "SYS_mmap or SYS_mmap2 not available on the target platform"
+#endif
 
 namespace __llvm_libc {
 
@@ -73,11 +78,21 @@ LLVM_LIBC_FUNCTION(int, thrd_create,
   // TODO: Add the CLONE_SETTLS flag and setup the TLS area correctly when
   // making the clone syscall.
 
-  void *stack = __llvm_libc::mmap(nullptr, ThreadParams::DEFAULT_STACK_SIZE,
-                                  PROT_READ | PROT_WRITE,
-                                  MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
-  if (stack == MAP_FAILED)
-    return llvmlibc_errno == ENOMEM ? thrd_nomem : thrd_error;
+  // Allocate thread stack.
+  long mmap_result =
+      __llvm_libc::syscall(MMAP_SYSCALL_NUMBER,
+                           0, // No special address
+                           ThreadParams::DEFAULT_STACK_SIZE,
+                           PROT_READ | PROT_WRITE,      // Read and write stack
+                           MAP_ANONYMOUS | MAP_PRIVATE, // Process private
+                           -1, // Not backed by any file
+                           0   // No offset
+      );
+  if (mmap_result < 0 && (uintptr_t(mmap_result) >=
+                          UINTPTR_MAX - ThreadParams::DEFAULT_STACK_SIZE)) {
+    return -mmap_result == ENOMEM ? thrd_nomem : thrd_error;
+  }
+  void *stack = reinterpret_cast<void *>(mmap_result);
 
   thread->__stack = stack;
   thread->__stack_size = ThreadParams::DEFAULT_STACK_SIZE;
@@ -126,7 +141,8 @@ LLVM_LIBC_FUNCTION(int, thrd_create,
   if (clone_result == 0) {
     start_thread();
   } else if (clone_result < 0) {
-    __llvm_libc::munmap(thread->__stack, thread->__stack_size);
+    __llvm_libc::syscall(SYS_munmap, mmap_result,
+                         ThreadParams::DEFAULT_STACK_SIZE);
     int error_val = -clone_result;
     return error_val == ENOMEM ? thrd_nomem : thrd_error;
   }

diff  --git a/libc/src/threads/linux/thrd_join.cpp b/libc/src/threads/linux/thrd_join.cpp
index 361c5877d86c8..30275ff737545 100644
--- a/libc/src/threads/linux/thrd_join.cpp
+++ b/libc/src/threads/linux/thrd_join.cpp
@@ -8,16 +8,15 @@
 
 #include "Futex.h"
 
-#include "include/sys/syscall.h" // For syscall numbers.
-#include "include/threads.h"     // For thrd_* type definitions.
 #include "src/__support/CPP/atomic.h"
 #include "src/__support/OSUtil/syscall.h" // For syscall function.
 #include "src/__support/common.h"
-#include "src/sys/mman/munmap.h"
 #include "src/threads/linux/Thread.h"
 #include "src/threads/thrd_join.h"
 
 #include <linux/futex.h> // For futex operations.
+#include <sys/syscall.h> // For syscall numbers.
+#include <threads.h>     // For thrd_* type definitions.
 
 namespace __llvm_libc {
 
@@ -37,7 +36,8 @@ LLVM_LIBC_FUNCTION(int, thrd_join, (thrd_t * thread, int *retval)) {
 
   *retval = thread->__retval;
 
-  if (__llvm_libc::munmap(thread->__stack, thread->__stack_size) == -1)
+  if (__llvm_libc::syscall(SYS_munmap, reinterpret_cast<long>(thread->__stack),
+                           thread->__stack_size) == -1)
     return thrd_error;
 
   return thrd_success;


        


More information about the libc-commits mailing list