Skip to content

Commit 1bece13

Browse files
namhyungacmel
authored andcommitted
perf lock contention: Support old rw_semaphore type
The old kernel has a different type of the owner field in rwsem. We can check it using bpf_core_type_matches() builtin in clang but it also needs its own version check since it's available on recent versions. Signed-off-by: Namhyung Kim <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Boqun Feng <[email protected]> Cc: Davidlohr Bueso <[email protected]> Cc: Hao Luo <[email protected]> Cc: Ian Rogers <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Song Liu <[email protected]> Cc: Waiman Long <[email protected]> Cc: Will Deacon <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 3477f07 commit 1bece13

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

tools/perf/util/bpf_skel/lock_contention.bpf.c

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ struct {
8484
__uint(max_entries, 1);
8585
} addr_filter SEC(".maps");
8686

87+
struct rw_semaphore___old {
88+
struct task_struct *owner;
89+
} __attribute__((preserve_access_index));
90+
91+
struct rw_semaphore___new {
92+
atomic_long_t owner;
93+
} __attribute__((preserve_access_index));
94+
8795
/* control flags */
8896
int enabled;
8997
int has_cpu;
@@ -161,6 +169,41 @@ static inline int update_task_data(struct task_struct *task)
161169
return 0;
162170
}
163171

172+
#ifndef __has_builtin
173+
# define __has_builtin(x) 0
174+
#endif
175+
176+
static inline struct task_struct *get_lock_owner(__u64 lock, __u32 flags)
177+
{
178+
struct task_struct *task;
179+
__u64 owner = 0;
180+
181+
if (flags & LCB_F_MUTEX) {
182+
struct mutex *mutex = (void *)lock;
183+
owner = BPF_CORE_READ(mutex, owner.counter);
184+
} else if (flags == LCB_F_READ || flags == LCB_F_WRITE) {
185+
#if __has_builtin(bpf_core_type_matches)
186+
if (bpf_core_type_matches(struct rw_semaphore___old)) {
187+
struct rw_semaphore___old *rwsem = (void *)lock;
188+
owner = (unsigned long)BPF_CORE_READ(rwsem, owner);
189+
} else if (bpf_core_type_matches(struct rw_semaphore___new)) {
190+
struct rw_semaphore___new *rwsem = (void *)lock;
191+
owner = BPF_CORE_READ(rwsem, owner.counter);
192+
}
193+
#else
194+
/* assume new struct */
195+
struct rw_semaphore *rwsem = (void *)lock;
196+
owner = BPF_CORE_READ(rwsem, owner.counter);
197+
#endif
198+
}
199+
200+
if (!owner)
201+
return NULL;
202+
203+
task = (void *)(owner & ~7UL);
204+
return task;
205+
}
206+
164207
SEC("tp_btf/contention_begin")
165208
int contention_begin(u64 *ctx)
166209
{
@@ -199,19 +242,7 @@ int contention_begin(u64 *ctx)
199242
struct task_struct *task;
200243

201244
if (lock_owner) {
202-
if (pelem->flags & LCB_F_MUTEX) {
203-
struct mutex *lock = (void *)pelem->lock;
204-
unsigned long owner = BPF_CORE_READ(lock, owner.counter);
205-
206-
task = (void *)(owner & ~7UL);
207-
} else if (pelem->flags == LCB_F_READ || pelem->flags == LCB_F_WRITE) {
208-
struct rw_semaphore *lock = (void *)pelem->lock;
209-
unsigned long owner = BPF_CORE_READ(lock, owner.counter);
210-
211-
task = (void *)(owner & ~7UL);
212-
} else {
213-
task = NULL;
214-
}
245+
task = get_lock_owner(pelem->lock, pelem->flags);
215246

216247
/* The flags is not used anymore. Pass the owner pid. */
217248
if (task)

0 commit comments

Comments
 (0)