[PATCH v4 07/23] perf rwsem: Add down_read_trylock() and down_write_trylock()
From: Ian Rogers
Date: Fri Nov 13 2026 - 12:59:37 EST
On-demand memory shrinking (perf_session__shrink(), dsos__shrink(),
symbols__shrink()) can be triggered from OOM recovery inside allocation
wrappers while the current thread already holds a rw_semaphore (such as
symbols->lock, dsos->lock, or machines->lock). Non-blocking trylock
primitives are needed so shrinking paths can safely skip any locked
container without deadlocking.
Add non-blocking down_read_trylock() and down_write_trylock() wrappers
around pthread_rwlock_tryrdlock() / pthread_rwlock_trywrlock() and
pthread_mutex_trylock() along with SHARED_TRYLOCK_FUNCTION and
EXCLUSIVE_TRYLOCK_FUNCTION thread-safety annotations.
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/mutex.h | 3 +++
tools/perf/util/rwsem.c | 20 ++++++++++++++++++++
tools/perf/util/rwsem.h | 11 +++++++++++
3 files changed, 34 insertions(+)
diff --git a/tools/perf/util/mutex.h b/tools/perf/util/mutex.h
index 5de1065ea2b9..de7dd6d8326c 100644
--- a/tools/perf/util/mutex.h
+++ b/tools/perf/util/mutex.h
@@ -57,6 +57,8 @@
/* Documents functions that try to acquire a lock, and return success or failure. */
#define EXCLUSIVE_TRYLOCK_FUNCTION(...) \
__attribute__((exclusive_trylock_function(__VA_ARGS__)))
+#define SHARED_TRYLOCK_FUNCTION(...) \
+ __attribute__((shared_trylock_function(__VA_ARGS__)))
/* Documents a function that expects a mutex to be held prior to entry. */
#define EXCLUSIVE_LOCKS_REQUIRED(...) __attribute__((exclusive_locks_required(__VA_ARGS__)))
@@ -78,6 +80,7 @@
#define SHARED_LOCK_FUNCTION(...)
#define UNLOCK_FUNCTION(...)
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
+#define SHARED_TRYLOCK_FUNCTION(...)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
#define SHARED_LOCKS_REQUIRED(...)
#define NO_THREAD_SAFETY_ANALYSIS
diff --git a/tools/perf/util/rwsem.c b/tools/perf/util/rwsem.c
index 1d15c0e3732b..9548a3b44f16 100644
--- a/tools/perf/util/rwsem.c
+++ b/tools/perf/util/rwsem.c
@@ -36,6 +36,16 @@ int down_read(struct rw_semaphore *sem)
#endif
}
+bool down_read_trylock(struct rw_semaphore *sem)
+ NO_THREAD_SAFETY_ANALYSIS
+{
+#if RWS_ERRORCHECK
+ return mutex_trylock(&sem->mtx);
+#else
+ return pthread_rwlock_tryrdlock(&sem->lock) == 0;
+#endif
+}
+
int up_read(struct rw_semaphore *sem)
NO_THREAD_SAFETY_ANALYSIS
{
@@ -58,6 +68,16 @@ int down_write(struct rw_semaphore *sem)
#endif
}
+bool down_write_trylock(struct rw_semaphore *sem)
+ NO_THREAD_SAFETY_ANALYSIS
+{
+#if RWS_ERRORCHECK
+ return mutex_trylock(&sem->mtx);
+#else
+ return pthread_rwlock_trywrlock(&sem->lock) == 0;
+#endif
+}
+
int up_write(struct rw_semaphore *sem)
NO_THREAD_SAFETY_ANALYSIS
{
diff --git a/tools/perf/util/rwsem.h b/tools/perf/util/rwsem.h
index b102d8143181..53c45990d9c9 100644
--- a/tools/perf/util/rwsem.h
+++ b/tools/perf/util/rwsem.h
@@ -22,9 +22,20 @@ int init_rwsem(struct rw_semaphore *sem);
int exit_rwsem(struct rw_semaphore *sem);
int down_read(struct rw_semaphore *sem) SHARED_LOCK_FUNCTION(sem);
+/*
+ * Tries to acquire a shared read lock without blocking. Per POSIX
+ * pthread_rwlock_tryrdlock(), succeeds when no writer holds or blocks on @sem
+ * (including when the calling thread already holds a read lock on @sem).
+ */
+bool down_read_trylock(struct rw_semaphore *sem) SHARED_TRYLOCK_FUNCTION(true, sem);
int up_read(struct rw_semaphore *sem) UNLOCK_FUNCTION(sem);
int down_write(struct rw_semaphore *sem) EXCLUSIVE_LOCK_FUNCTION(sem);
+/*
+ * Tries to acquire an exclusive write lock without blocking. Returns false if
+ * any thread (including the calling thread) holds a read or write lock on @sem.
+ */
+bool down_write_trylock(struct rw_semaphore *sem) EXCLUSIVE_TRYLOCK_FUNCTION(true, sem);
int up_write(struct rw_semaphore *sem) UNLOCK_FUNCTION(sem);
#endif /* _PERF_RWSEM_H */
--
2.56.1.703.gf848ed807c-goog