Skip to content

Commit 415ca24

Browse files
Revert "[libc] implement cached process/thread identity" (#99559)
Reverts #98989
1 parent 5c9fc3c commit 415ca24

30 files changed

+40
-363
lines changed

libc/config/config.json

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,6 @@
7575
"LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE": {
7676
"value": 1073741824,
7777
"doc": "Default size for the constinit freelist buffer used for the freelist malloc implementation (default 1o 1GB)."
78-
},
79-
},
80-
"unistd": {
81-
"LIBC_CONF_ENABLE_TID_CACHE": {
82-
"value": true,
83-
"doc": "Enable caching mechanism for gettid to avoid syscall (only effective in fullbuild mode, default to true). Please refer to Undefined Behavior documentation for implications."
84-
},
85-
"LIBC_CONF_ENABLE_PID_CACHE": {
86-
"value": true,
87-
"doc": "Enable caching mechanism for getpid to avoid syscall (default to true). Please refer to Undefined Behavior documentation for implications."
8878
}
8979
},
9080
"math": {

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ set(TARGET_LIBC_ENTRYPOINTS
297297
libc.src.unistd.geteuid
298298
libc.src.unistd.getpid
299299
libc.src.unistd.getppid
300-
libc.src.unistd.gettid
301300
libc.src.unistd.getuid
302301
libc.src.unistd.isatty
303302
libc.src.unistd.link

libc/config/linux/riscv/entrypoints.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ set(TARGET_LIBC_ENTRYPOINTS
296296
libc.src.unistd.geteuid
297297
libc.src.unistd.getpid
298298
libc.src.unistd.getppid
299-
libc.src.unistd.gettid
300299
libc.src.unistd.getuid
301300
libc.src.unistd.isatty
302301
libc.src.unistd.link

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,6 @@ set(TARGET_LIBC_ENTRYPOINTS
315315
libc.src.unistd.geteuid
316316
libc.src.unistd.getpid
317317
libc.src.unistd.getppid
318-
libc.src.unistd.gettid
319318
libc.src.unistd.getuid
320319
libc.src.unistd.isatty
321320
libc.src.unistd.link

libc/docs/configure.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,3 @@ to learn about the defaults for your platform and target.
5252
* **"string" options**
5353
- ``LIBC_CONF_MEMSET_X86_USE_SOFTWARE_PREFETCHING``: Inserts prefetch for write instructions (PREFETCHW) for memset on x86 to recover performance when hardware prefetcher is disabled.
5454
- ``LIBC_CONF_STRING_UNSAFE_WIDE_READ``: Read more than a byte at a time to perform byte-string operations like strlen.
55-
* **"unistd" options**
56-
- ``LIBC_CONF_ENABLE_PID_CACHE``: Enable caching mechanism for getpid to avoid syscall (default to true). Please refer to Undefined Behavior documentation for implications.
57-
- ``LIBC_CONF_ENABLE_TID_CACHE``: Enable caching mechanism for gettid to avoid syscall (only effective in fullbuild mode, default to true). Please refer to Undefined Behavior documentation for implications.

libc/docs/dev/undefined_behavior.rst

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,3 @@ direction in this case.
9393
Non-const Constant Return Values
9494
--------------------------------
9595
Some libc functions, like ``dlerror()``, return ``char *`` instead of ``const char *`` and then tell the caller they promise not to to modify this value. Any modification of this value is undefined behavior.
96-
97-
Cached ``getpid/gettid``
98-
------------------------
99-
Since version ``2.25``, glibc removes its cache mechanism for ``getpid/gettid``
100-
(See the history section in https://man7.org/linux/man-pages/man2/getpid.2.html).
101-
LLVM's libc still implements the cache as it is useful for fast deadlock detection.
102-
The cache mechanism is also implemented in MUSL and bionic. The tid/pid cache can
103-
be disabled by setting ``LIBC_CONF_ENABLE_TID_CACHE`` and ``LIBC_CONF_ENABLE_PID_CACHE``
104-
to ``false`` respectively.
105-
106-
Unwrapped ``SYS_clone/SYS_fork/SYS_vfork``
107-
------------------------------------------
108-
It is highly discouraged to use unwrapped ``SYS_clone/SYS_fork/SYS_vfork``.
109-
First, calling such syscalls without provided libc wrappers ignores
110-
all the ``pthread_atfork`` entries as libc can no longer detect the ``fork``.
111-
Second, libc relies on the ``fork/clone`` wrappers to correctly maintain cache for
112-
process id and thread id, and other important process-specific states such as the list
113-
of robust mutexes. Third, even if the user is to call ``exec*`` functions immediately,
114-
there can still be other unexpected issues. For instance, there can be signal handlers
115-
inherited from parent process triggered inside the instruction window between ``fork``
116-
and ``exec*``. As libc failed to maintain its internal states correctly, even though the
117-
functions used inside the signal handlers are marked as ``async-signal-safe`` (such as
118-
``getpid``), they will still return wrong values or lead to other even worse situations.

libc/spec/posix.td

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -546,11 +546,6 @@ def POSIX : StandardSpec<"POSIX"> {
546546
RetValSpec<PidT>,
547547
[ArgSpec<VoidType>]
548548
>,
549-
FunctionSpec<
550-
"gettid",
551-
RetValSpec<PidT>,
552-
[ArgSpec<VoidType>]
553-
>,
554549
FunctionSpec<
555550
"getuid",
556551
RetValSpec<UidT>,
@@ -606,6 +601,16 @@ def POSIX : StandardSpec<"POSIX"> {
606601
RetValSpec<IntType>,
607602
[ArgSpec<ConstCharPtr>]
608603
>,
604+
FunctionSpec<
605+
"getpid",
606+
RetValSpec<IntType>,
607+
[ArgSpec<VoidType>]
608+
>,
609+
FunctionSpec<
610+
"getppid",
611+
RetValSpec<IntType>,
612+
[ArgSpec<VoidType>]
613+
>,
609614
FunctionSpec<
610615
"link",
611616
RetValSpec<IntType>,

libc/src/__support/OSUtil/CMakeLists.txt

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,3 @@ add_object_library(
1515
DEPENDS
1616
${target_os_util}
1717
)
18-
19-
if (LIBC_CONF_ENABLE_PID_CACHE)
20-
set(libc_copt_enable_pid_cache 1)
21-
else()
22-
set(libc_copt_enable_pid_cache 0)
23-
endif()
24-
25-
if(TARGET libc.src.__support.OSUtil.${LIBC_TARGET_OS}.pid)
26-
add_object_library(
27-
pid
28-
ALIAS
29-
DEPENDS
30-
.${LIBC_TARGET_OS}.pid
31-
COMPILE_OPTIONS
32-
-DLIBC_COPT_ENABLE_PID_CACHE=${libc_copt_enable_pid_cache}
33-
)
34-
endif()

libc/src/__support/OSUtil/linux/CMakeLists.txt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,3 @@ add_object_library(
2323
libc.hdr.types.struct_f_owner_ex
2424
libc.hdr.types.off_t
2525
)
26-
27-
add_object_library(
28-
pid
29-
SRCS
30-
pid.cpp
31-
HDRS
32-
../pid.h
33-
DEPENDS
34-
libc.src.__support.OSUtil.osutil
35-
libc.src.__support.common
36-
libc.hdr.types.pid_t
37-
libc.include.sys_syscall
38-
)

libc/src/__support/OSUtil/linux/pid.cpp

Lines changed: 0 additions & 20 deletions
This file was deleted.

libc/src/__support/OSUtil/pid.h

Lines changed: 0 additions & 41 deletions
This file was deleted.

libc/src/__support/threads/CMakeLists.txt

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ if(TARGET libc.src.__support.threads.${LIBC_TARGET_OS}.mutex)
4444
)
4545
endif()
4646

47-
if (LIBC_CONF_ENABLE_TID_CACHE)
48-
set(libc_copt_enable_tid_cache 1)
49-
else()
50-
set(libc_copt_enable_tid_cache 0)
51-
endif()
52-
5347
add_header_library(
5448
thread_common
5549
HDRS
@@ -60,9 +54,6 @@ add_header_library(
6054
libc.src.__support.CPP.optional
6155
libc.src.__support.CPP.string_view
6256
libc.src.__support.CPP.stringstream
63-
libc.hdr.types.pid_t
64-
COMPILE_OPTIONS
65-
-DLIBC_COPT_ENABLE_TID_CACHE=${libc_copt_enable_tid_cache}
6657
)
6758

6859
if(TARGET libc.src.__support.threads.${LIBC_TARGET_OS}.thread)
@@ -98,21 +89,3 @@ if(TARGET libc.src.__support.threads.${LIBC_TARGET_OS}.CndVar)
9889
.${LIBC_TARGET_OS}.CndVar
9990
)
10091
endif()
101-
102-
set(tid_dep)
103-
if (LLVM_LIBC_FULL_BUILD)
104-
list(APPEND tid_dep libc.src.__support.thread)
105-
else()
106-
list(APPEND tid_dep libc.src.__support.OSUtil.osutil)
107-
list(APPEND tid_dep libc.include.sys_syscall)
108-
endif()
109-
110-
add_header_library(
111-
tid
112-
HDRS
113-
tid.h
114-
DEPENDS
115-
libc.src.__support.common
116-
libc.hdr.types.pid_t
117-
${tid_dep}
118-
)

libc/src/__support/threads/linux/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ add_header_library(
5555
libc.src.__support.common
5656
libc.src.__support.OSUtil.osutil
5757
libc.src.__support.CPP.limits
58-
libc.src.__support.threads.tid
5958
COMPILE_OPTIONS
6059
-DLIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT=${LIBC_CONF_RWLOCK_DEFAULT_SPIN_COUNT}
6160
${monotonicity_flags}

libc/src/__support/threads/linux/rwlock.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "src/__support/threads/linux/futex_word.h"
2424
#include "src/__support/threads/linux/raw_mutex.h"
2525
#include "src/__support/threads/sleep.h"
26-
#include "src/__support/threads/tid.h"
2726

2827
#ifndef LIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT
2928
#define LIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT 100
@@ -337,6 +336,8 @@ class RwLock {
337336
LIBC_INLINE Role get_preference() const {
338337
return static_cast<Role>(preference);
339338
}
339+
// TODO: use cached thread id once implemented.
340+
LIBC_INLINE static pid_t gettid() { return syscall_impl<pid_t>(SYS_gettid); }
340341

341342
template <Role role> LIBC_INLINE LockResult try_lock(RwState &old) {
342343
if constexpr (role == Role::Reader) {
@@ -358,7 +359,7 @@ class RwLock {
358359
if (LIBC_LIKELY(old.compare_exchange_weak_with(
359360
state, old.set_writer_bit(), cpp::MemoryOrder::ACQUIRE,
360361
cpp::MemoryOrder::RELAXED))) {
361-
writer_tid.store(gettid_inline(), cpp::MemoryOrder::RELAXED);
362+
writer_tid.store(gettid(), cpp::MemoryOrder::RELAXED);
362363
return LockResult::Success;
363364
}
364365
// Notice that old is updated by the compare_exchange_weak_with
@@ -393,7 +394,7 @@ class RwLock {
393394
unsigned spin_count = LIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT) {
394395
// Phase 1: deadlock detection.
395396
// A deadlock happens if this is a RAW/WAW lock in the same thread.
396-
if (writer_tid.load(cpp::MemoryOrder::RELAXED) == gettid_inline())
397+
if (writer_tid.load(cpp::MemoryOrder::RELAXED) == gettid())
397398
return LockResult::Deadlock;
398399

399400
#if LIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY
@@ -519,7 +520,7 @@ class RwLock {
519520
if (old.has_active_writer()) {
520521
// The lock is held by a writer.
521522
// Check if we are the owner of the lock.
522-
if (writer_tid.load(cpp::MemoryOrder::RELAXED) != gettid_inline())
523+
if (writer_tid.load(cpp::MemoryOrder::RELAXED) != gettid())
523524
return LockResult::PermissionDenied;
524525
// clear writer tid.
525526
writer_tid.store(0, cpp::MemoryOrder::RELAXED);

libc/src/__support/threads/linux/thread.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,4 @@ void thread_exit(ThreadReturnValue retval, ThreadStyle style) {
518518
__builtin_unreachable();
519519
}
520520

521-
pid_t Thread::get_uncached_tid() { return syscall_impl<pid_t>(SYS_gettid); }
522-
523521
} // namespace LIBC_NAMESPACE_DECL

libc/src/__support/threads/thread.h

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_THREAD_H
1010
#define LLVM_LIBC_SRC___SUPPORT_THREADS_THREAD_H
1111

12-
#ifndef LIBC_COPT_ENABLE_TID_CACHE
13-
#define LIBC_COPT_ENABLE_TID_CACHE 1
14-
#endif
15-
16-
#include "hdr/types/pid_t.h"
1712
#include "src/__support/CPP/atomic.h"
1813
#include "src/__support/CPP/optional.h"
1914
#include "src/__support/CPP/string_view.h"
@@ -108,7 +103,7 @@ struct alignas(STACK_ALIGNMENT) ThreadAttributes {
108103
uintptr_t tls; // Address to the thread TLS memory
109104
uintptr_t tls_size; // The size of area pointed to by |tls|.
110105
unsigned char owned_stack; // Indicates if the thread owns this stack memory
111-
pid_t tid;
106+
int tid;
112107
ThreadStyle style;
113108
ThreadReturnValue retval;
114109
ThreadAtExitCallbackMgr *atexit_callback_mgr;
@@ -233,26 +228,6 @@ struct Thread {
233228

234229
// Return the name of the thread in |name|. Return the error number of error.
235230
int get_name(cpp::StringStream &name) const;
236-
237-
static pid_t get_uncached_tid();
238-
239-
LIBC_INLINE void refresh_tid(pid_t cached = -1) {
240-
if (cached >= 0)
241-
this->attrib->tid = cached;
242-
else
243-
this->attrib->tid = get_uncached_tid();
244-
}
245-
LIBC_INLINE void invalidate_tid() { this->attrib->tid = -1; }
246-
247-
LIBC_INLINE pid_t get_tid() {
248-
#if LIBC_COPT_ENABLE_TID_CACHE
249-
if (LIBC_UNLIKELY(this->attrib->tid < 0))
250-
return get_uncached_tid();
251-
return this->attrib->tid;
252-
#else
253-
return get_uncached_tid();
254-
#endif
255-
}
256231
};
257232

258233
extern LIBC_THREAD_LOCAL Thread self;

libc/src/__support/threads/tid.h

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)