Skip to content

Commit 7a8f349

Browse files
captain5050namhyung
authored andcommitted
perf rwsem: Add debug mode that uses a mutex
Mutex error check will capture trying to take the lock recursively and other problems that rwlock won't. At the expense of concurrency, adda debug mode that uses a mutex in place of a rwsem. Signed-off-by: Ian Rogers <irogers@google.com> Cc: K Prateek Nayak <kprateek.nayak@amd.com> Cc: Ravi Bangoria <ravi.bangoria@amd.com> Cc: Sandipan Das <sandipan.das@amd.com> Cc: Anshuman Khandual <anshuman.khandual@arm.com> Cc: German Gomez <german.gomez@arm.com> Cc: James Clark <james.clark@arm.com> Cc: Nick Terrell <terrelln@fb.com> Cc: Sean Christopherson <seanjc@google.com> Cc: Changbin Du <changbin.du@huawei.com> Cc: liuwenyu <liuwenyu7@huawei.com> Cc: Yang Jihong <yangjihong1@huawei.com> Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Miguel Ojeda <ojeda@kernel.org> Cc: Song Liu <song@kernel.org> Cc: Leo Yan <leo.yan@linaro.org> Cc: Kajol Jain <kjain@linux.ibm.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com> Cc: Yanteng Si <siyanteng@loongson.cn> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Paolo Bonzini <pbonzini@redhat.com> Link: https://lore.kernel.org/r/20231024222353.3024098-2-irogers@google.com Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent b27778e commit 7a8f349

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

tools/perf/util/rwsem.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,66 @@
22
#include "util.h"
33
#include "rwsem.h"
44

5+
#if RWS_ERRORCHECK
6+
#include "mutex.h"
7+
#endif
8+
59
int init_rwsem(struct rw_semaphore *sem)
610
{
11+
#if RWS_ERRORCHECK
12+
mutex_init(&sem->mtx);
13+
return 0;
14+
#else
715
return pthread_rwlock_init(&sem->lock, NULL);
16+
#endif
817
}
918

1019
int exit_rwsem(struct rw_semaphore *sem)
1120
{
21+
#if RWS_ERRORCHECK
22+
mutex_destroy(&sem->mtx);
23+
return 0;
24+
#else
1225
return pthread_rwlock_destroy(&sem->lock);
26+
#endif
1327
}
1428

1529
int down_read(struct rw_semaphore *sem)
1630
{
31+
#if RWS_ERRORCHECK
32+
mutex_lock(&sem->mtx);
33+
return 0;
34+
#else
1735
return perf_singlethreaded ? 0 : pthread_rwlock_rdlock(&sem->lock);
36+
#endif
1837
}
1938

2039
int up_read(struct rw_semaphore *sem)
2140
{
41+
#if RWS_ERRORCHECK
42+
mutex_unlock(&sem->mtx);
43+
return 0;
44+
#else
2245
return perf_singlethreaded ? 0 : pthread_rwlock_unlock(&sem->lock);
46+
#endif
2347
}
2448

2549
int down_write(struct rw_semaphore *sem)
2650
{
51+
#if RWS_ERRORCHECK
52+
mutex_lock(&sem->mtx);
53+
return 0;
54+
#else
2755
return perf_singlethreaded ? 0 : pthread_rwlock_wrlock(&sem->lock);
56+
#endif
2857
}
2958

3059
int up_write(struct rw_semaphore *sem)
3160
{
61+
#if RWS_ERRORCHECK
62+
mutex_unlock(&sem->mtx);
63+
return 0;
64+
#else
3265
return perf_singlethreaded ? 0 : pthread_rwlock_unlock(&sem->lock);
66+
#endif
3367
}

tools/perf/util/rwsem.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22
#define _PERF_RWSEM_H
33

44
#include <pthread.h>
5+
#include "mutex.h"
6+
7+
/*
8+
* Mutexes have additional error checking. Enable to use a mutex rather than a
9+
* rwlock for debugging.
10+
*/
11+
#define RWS_ERRORCHECK 0
512

613
struct rw_semaphore {
14+
#if RWS_ERRORCHECK
15+
struct mutex mtx;
16+
#else
717
pthread_rwlock_t lock;
18+
#endif
819
};
920

1021
int init_rwsem(struct rw_semaphore *sem);

0 commit comments

Comments
 (0)