aboutsummaryrefslogtreecommitdiff
path: root/libc/src/threads/linux
diff options
context:
space:
mode:
authorSiva Chandra Reddy <sivachandra@google.com>2022-04-01 07:33:34 +0000
committerSiva Chandra Reddy <sivachandra@google.com>2022-04-02 05:12:08 +0000
commit6a7cd4a1df6c394f5d0dae93c9be26f8f777cd7f (patch)
tree85dfcc0cfe2d7c20692e5395b0b866b63d109387 /libc/src/threads/linux
parentbca96760f75871d8dc0d868042a6417a65c1a18a (diff)
downloadllvm-6a7cd4a1df6c394f5d0dae93c9be26f8f777cd7f.zip
llvm-6a7cd4a1df6c394f5d0dae93c9be26f8f777cd7f.tar.gz
llvm-6a7cd4a1df6c394f5d0dae93c9be26f8f777cd7f.tar.bz2
[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
Diffstat (limited to 'libc/src/threads/linux')
-rw-r--r--libc/src/threads/linux/CMakeLists.txt4
-rw-r--r--libc/src/threads/linux/thrd_create.cpp42
-rw-r--r--libc/src/threads/linux/thrd_join.cpp8
3 files changed, 34 insertions, 20 deletions
diff --git a/libc/src/threads/linux/CMakeLists.txt b/libc/src/threads/linux/CMakeLists.txt
index 0941711..0abdf5d 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 e893374..5f25ded 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 361c587..30275ff 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;