1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 // Copyright (c) 2022 Google
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_tracing.h>
6 #include <bpf/bpf_core_read.h>
10 /* for collect_lock_syms(). 4096 was rejected by the verifier */
13 /* lock contention flags from include/trace/events/lock.h */
14 #define LCB_F_SPIN (1U << 0)
15 #define LCB_F_READ (1U << 1)
16 #define LCB_F_WRITE (1U << 2)
17 #define LCB_F_RT (1U << 3)
18 #define LCB_F_PERCPU (1U << 4)
19 #define LCB_F_MUTEX (1U << 5)
28 /* callstack storage */
30 __uint(type, BPF_MAP_TYPE_STACK_TRACE);
31 __uint(key_size, sizeof(__u32));
32 __uint(value_size, sizeof(__u64));
33 __uint(max_entries, MAX_ENTRIES);
34 } stacks SEC(".maps");
36 /* maintain timestamp at the beginning of contention */
38 __uint(type, BPF_MAP_TYPE_HASH);
40 __type(value, struct tstamp_data);
41 __uint(max_entries, MAX_ENTRIES);
42 } tstamp SEC(".maps");
44 /* actual lock contention statistics */
46 __uint(type, BPF_MAP_TYPE_HASH);
47 __uint(key_size, sizeof(struct contention_key));
48 __uint(value_size, sizeof(struct contention_data));
49 __uint(max_entries, MAX_ENTRIES);
50 } lock_stat SEC(".maps");
53 __uint(type, BPF_MAP_TYPE_HASH);
54 __uint(key_size, sizeof(__u32));
55 __uint(value_size, sizeof(struct contention_task_data));
56 __uint(max_entries, MAX_ENTRIES);
57 } task_data SEC(".maps");
60 __uint(type, BPF_MAP_TYPE_HASH);
61 __uint(key_size, sizeof(__u64));
62 __uint(value_size, sizeof(__u32));
63 __uint(max_entries, MAX_ENTRIES);
64 } lock_syms SEC(".maps");
67 __uint(type, BPF_MAP_TYPE_HASH);
68 __uint(key_size, sizeof(__u32));
69 __uint(value_size, sizeof(__u8));
70 __uint(max_entries, 1);
71 } cpu_filter SEC(".maps");
74 __uint(type, BPF_MAP_TYPE_HASH);
75 __uint(key_size, sizeof(__u32));
76 __uint(value_size, sizeof(__u8));
77 __uint(max_entries, 1);
78 } task_filter SEC(".maps");
81 __uint(type, BPF_MAP_TYPE_HASH);
82 __uint(key_size, sizeof(__u32));
83 __uint(value_size, sizeof(__u8));
84 __uint(max_entries, 1);
85 } type_filter SEC(".maps");
88 __uint(type, BPF_MAP_TYPE_HASH);
89 __uint(key_size, sizeof(__u64));
90 __uint(value_size, sizeof(__u8));
91 __uint(max_entries, 1);
92 } addr_filter SEC(".maps");
94 struct rw_semaphore___old {
95 struct task_struct *owner;
96 } __attribute__((preserve_access_index));
98 struct rw_semaphore___new {
100 } __attribute__((preserve_access_index));
102 struct mm_struct___old {
103 struct rw_semaphore mmap_sem;
104 } __attribute__((preserve_access_index));
106 struct mm_struct___new {
107 struct rw_semaphore mmap_lock;
108 } __attribute__((preserve_access_index));
120 /* determine the key of lock stat */
128 static inline int can_record(u64 *ctx)
131 __u32 cpu = bpf_get_smp_processor_id();
134 ok = bpf_map_lookup_elem(&cpu_filter, &cpu);
141 __u32 pid = bpf_get_current_pid_tgid();
143 ok = bpf_map_lookup_elem(&task_filter, &pid);
150 __u32 flags = (__u32)ctx[1];
152 ok = bpf_map_lookup_elem(&type_filter, &flags);
161 ok = bpf_map_lookup_elem(&addr_filter, &addr);
169 static inline int update_task_data(struct task_struct *task)
171 struct contention_task_data *p;
174 err = bpf_core_read(&pid, sizeof(pid), &task->pid);
178 p = bpf_map_lookup_elem(&task_data, &pid);
180 struct contention_task_data data = {};
182 BPF_CORE_READ_STR_INTO(&data.comm, task, comm);
183 bpf_map_update_elem(&task_data, &pid, &data, BPF_NOEXIST);
189 #ifndef __has_builtin
190 # define __has_builtin(x) 0
193 static inline struct task_struct *get_lock_owner(__u64 lock, __u32 flags)
195 struct task_struct *task;
198 if (flags & LCB_F_MUTEX) {
199 struct mutex *mutex = (void *)lock;
200 owner = BPF_CORE_READ(mutex, owner.counter);
201 } else if (flags == LCB_F_READ || flags == LCB_F_WRITE) {
203 * Support for the BPF_TYPE_MATCHES argument to the
204 * __builtin_preserve_type_info builtin was added at some point during
205 * development of clang 15 and it's what is needed for
206 * bpf_core_type_matches.
208 #if __has_builtin(__builtin_preserve_type_info) && __clang_major__ >= 15
209 if (bpf_core_type_matches(struct rw_semaphore___old)) {
210 struct rw_semaphore___old *rwsem = (void *)lock;
211 owner = (unsigned long)BPF_CORE_READ(rwsem, owner);
212 } else if (bpf_core_type_matches(struct rw_semaphore___new)) {
213 struct rw_semaphore___new *rwsem = (void *)lock;
214 owner = BPF_CORE_READ(rwsem, owner.counter);
217 /* assume new struct */
218 struct rw_semaphore *rwsem = (void *)lock;
219 owner = BPF_CORE_READ(rwsem, owner.counter);
226 task = (void *)(owner & ~7UL);
230 static inline __u32 check_lock_type(__u64 lock, __u32 flags)
232 struct task_struct *curr;
233 struct mm_struct___old *mm_old;
234 struct mm_struct___new *mm_new;
237 case LCB_F_READ: /* rwsem */
239 curr = bpf_get_current_task_btf();
240 if (curr->mm == NULL)
242 mm_new = (void *)curr->mm;
243 if (bpf_core_field_exists(mm_new->mmap_lock)) {
244 if (&mm_new->mmap_lock == (void *)lock)
245 return LCD_F_MMAP_LOCK;
248 mm_old = (void *)curr->mm;
249 if (bpf_core_field_exists(mm_old->mmap_sem)) {
250 if (&mm_old->mmap_sem == (void *)lock)
251 return LCD_F_MMAP_LOCK;
254 case LCB_F_SPIN: /* spinlock */
255 curr = bpf_get_current_task_btf();
256 if (&curr->sighand->siglock == (void *)lock)
257 return LCD_F_SIGHAND_LOCK;
265 SEC("tp_btf/contention_begin")
266 int contention_begin(u64 *ctx)
269 struct tstamp_data *pelem;
271 if (!enabled || !can_record(ctx))
274 pid = bpf_get_current_pid_tgid();
275 pelem = bpf_map_lookup_elem(&tstamp, &pid);
276 if (pelem && pelem->lock)
280 struct tstamp_data zero = {};
282 bpf_map_update_elem(&tstamp, &pid, &zero, BPF_ANY);
283 pelem = bpf_map_lookup_elem(&tstamp, &pid);
285 __sync_fetch_and_add(&task_fail, 1);
290 pelem->timestamp = bpf_ktime_get_ns();
291 pelem->lock = (__u64)ctx[0];
292 pelem->flags = (__u32)ctx[1];
294 if (needs_callstack) {
295 pelem->stack_id = bpf_get_stackid(ctx, &stacks,
296 BPF_F_FAST_STACK_CMP | stack_skip);
297 if (pelem->stack_id < 0)
298 __sync_fetch_and_add(&stack_fail, 1);
299 } else if (aggr_mode == LOCK_AGGR_TASK) {
300 struct task_struct *task;
303 task = get_lock_owner(pelem->lock, pelem->flags);
305 /* The flags is not used anymore. Pass the owner pid. */
307 pelem->flags = BPF_CORE_READ(task, pid);
312 task = bpf_get_current_task_btf();
316 if (update_task_data(task) < 0 && lock_owner)
324 SEC("tp_btf/contention_end")
325 int contention_end(u64 *ctx)
328 struct tstamp_data *pelem;
329 struct contention_key key = {};
330 struct contention_data *data;
336 pid = bpf_get_current_pid_tgid();
337 pelem = bpf_map_lookup_elem(&tstamp, &pid);
338 if (!pelem || pelem->lock != ctx[0])
341 duration = bpf_ktime_get_ns() - pelem->timestamp;
342 if ((__s64)duration < 0) {
343 bpf_map_delete_elem(&tstamp, &pid);
344 __sync_fetch_and_add(&time_fail, 1);
349 case LOCK_AGGR_CALLER:
350 key.stack_id = pelem->stack_id;
354 key.pid = pelem->flags;
358 key.stack_id = pelem->stack_id;
361 key.lock_addr = pelem->lock;
363 key.stack_id = pelem->stack_id;
366 /* should not happen */
370 data = bpf_map_lookup_elem(&lock_stat, &key);
372 struct contention_data first = {
373 .total_time = duration,
374 .max_time = duration,
375 .min_time = duration,
377 .flags = pelem->flags,
380 if (aggr_mode == LOCK_AGGR_ADDR)
381 first.flags |= check_lock_type(pelem->lock, pelem->flags);
383 bpf_map_update_elem(&lock_stat, &key, &first, BPF_NOEXIST);
384 bpf_map_delete_elem(&tstamp, &pid);
388 __sync_fetch_and_add(&data->total_time, duration);
389 __sync_fetch_and_add(&data->count, 1);
391 /* FIXME: need atomic operations */
392 if (data->max_time < duration)
393 data->max_time = duration;
394 if (data->min_time > duration)
395 data->min_time = duration;
397 bpf_map_delete_elem(&tstamp, &pid);
401 extern struct rq runqueues __ksym;
403 SEC("raw_tp/bpf_test_finish")
404 int BPF_PROG(collect_lock_syms)
409 for (int i = 0; i < MAX_CPUS; i++) {
410 struct rq *rq = bpf_per_cpu_ptr(&runqueues, i);
415 lock_addr = (__u64)&rq->__lock;
416 lock_flag = LOCK_CLASS_RQLOCK;
417 bpf_map_update_elem(&lock_syms, &lock_addr, &lock_flag, BPF_ANY);
422 char LICENSE[] SEC("license") = "Dual BSD/GPL";