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>
7 #include <asm-generic/errno-base.h>
11 /* for collect_lock_syms(). 4096 was rejected by the verifier */
14 /* lock contention flags from include/trace/events/lock.h */
15 #define LCB_F_SPIN (1U << 0)
16 #define LCB_F_READ (1U << 1)
17 #define LCB_F_WRITE (1U << 2)
18 #define LCB_F_RT (1U << 3)
19 #define LCB_F_PERCPU (1U << 4)
20 #define LCB_F_MUTEX (1U << 5)
29 /* callstack storage */
31 __uint(type, BPF_MAP_TYPE_STACK_TRACE);
32 __uint(key_size, sizeof(__u32));
33 __uint(value_size, sizeof(__u64));
34 __uint(max_entries, MAX_ENTRIES);
35 } stacks SEC(".maps");
37 /* maintain timestamp at the beginning of contention */
39 __uint(type, BPF_MAP_TYPE_HASH);
41 __type(value, struct tstamp_data);
42 __uint(max_entries, MAX_ENTRIES);
43 } tstamp SEC(".maps");
45 /* actual lock contention statistics */
47 __uint(type, BPF_MAP_TYPE_HASH);
48 __uint(key_size, sizeof(struct contention_key));
49 __uint(value_size, sizeof(struct contention_data));
50 __uint(max_entries, MAX_ENTRIES);
51 } lock_stat SEC(".maps");
54 __uint(type, BPF_MAP_TYPE_HASH);
55 __uint(key_size, sizeof(__u32));
56 __uint(value_size, sizeof(struct contention_task_data));
57 __uint(max_entries, MAX_ENTRIES);
58 } task_data SEC(".maps");
61 __uint(type, BPF_MAP_TYPE_HASH);
62 __uint(key_size, sizeof(__u64));
63 __uint(value_size, sizeof(__u32));
64 __uint(max_entries, MAX_ENTRIES);
65 } lock_syms SEC(".maps");
68 __uint(type, BPF_MAP_TYPE_HASH);
69 __uint(key_size, sizeof(__u32));
70 __uint(value_size, sizeof(__u8));
71 __uint(max_entries, 1);
72 } cpu_filter SEC(".maps");
75 __uint(type, BPF_MAP_TYPE_HASH);
76 __uint(key_size, sizeof(__u32));
77 __uint(value_size, sizeof(__u8));
78 __uint(max_entries, 1);
79 } task_filter SEC(".maps");
82 __uint(type, BPF_MAP_TYPE_HASH);
83 __uint(key_size, sizeof(__u32));
84 __uint(value_size, sizeof(__u8));
85 __uint(max_entries, 1);
86 } type_filter SEC(".maps");
89 __uint(type, BPF_MAP_TYPE_HASH);
90 __uint(key_size, sizeof(__u64));
91 __uint(value_size, sizeof(__u8));
92 __uint(max_entries, 1);
93 } addr_filter SEC(".maps");
95 struct rw_semaphore___old {
96 struct task_struct *owner;
97 } __attribute__((preserve_access_index));
99 struct rw_semaphore___new {
101 } __attribute__((preserve_access_index));
103 struct mm_struct___old {
104 struct rw_semaphore mmap_sem;
105 } __attribute__((preserve_access_index));
107 struct mm_struct___new {
108 struct rw_semaphore mmap_lock;
109 } __attribute__((preserve_access_index));
121 /* determine the key of lock stat */
133 static inline int can_record(u64 *ctx)
136 __u32 cpu = bpf_get_smp_processor_id();
139 ok = bpf_map_lookup_elem(&cpu_filter, &cpu);
146 __u32 pid = bpf_get_current_pid_tgid();
148 ok = bpf_map_lookup_elem(&task_filter, &pid);
155 __u32 flags = (__u32)ctx[1];
157 ok = bpf_map_lookup_elem(&type_filter, &flags);
166 ok = bpf_map_lookup_elem(&addr_filter, &addr);
174 static inline int update_task_data(struct task_struct *task)
176 struct contention_task_data *p;
179 err = bpf_core_read(&pid, sizeof(pid), &task->pid);
183 p = bpf_map_lookup_elem(&task_data, &pid);
184 if (p == NULL && !task_map_full) {
185 struct contention_task_data data = {};
187 BPF_CORE_READ_STR_INTO(&data.comm, task, comm);
188 if (bpf_map_update_elem(&task_data, &pid, &data, BPF_NOEXIST) == -E2BIG)
195 #ifndef __has_builtin
196 # define __has_builtin(x) 0
199 static inline struct task_struct *get_lock_owner(__u64 lock, __u32 flags)
201 struct task_struct *task;
204 if (flags & LCB_F_MUTEX) {
205 struct mutex *mutex = (void *)lock;
206 owner = BPF_CORE_READ(mutex, owner.counter);
207 } else if (flags == LCB_F_READ || flags == LCB_F_WRITE) {
209 * Support for the BPF_TYPE_MATCHES argument to the
210 * __builtin_preserve_type_info builtin was added at some point during
211 * development of clang 15 and it's what is needed for
212 * bpf_core_type_matches.
214 #if __has_builtin(__builtin_preserve_type_info) && __clang_major__ >= 15
215 if (bpf_core_type_matches(struct rw_semaphore___old)) {
216 struct rw_semaphore___old *rwsem = (void *)lock;
217 owner = (unsigned long)BPF_CORE_READ(rwsem, owner);
218 } else if (bpf_core_type_matches(struct rw_semaphore___new)) {
219 struct rw_semaphore___new *rwsem = (void *)lock;
220 owner = BPF_CORE_READ(rwsem, owner.counter);
223 /* assume new struct */
224 struct rw_semaphore *rwsem = (void *)lock;
225 owner = BPF_CORE_READ(rwsem, owner.counter);
232 task = (void *)(owner & ~7UL);
236 static inline __u32 check_lock_type(__u64 lock, __u32 flags)
238 struct task_struct *curr;
239 struct mm_struct___old *mm_old;
240 struct mm_struct___new *mm_new;
243 case LCB_F_READ: /* rwsem */
245 curr = bpf_get_current_task_btf();
246 if (curr->mm == NULL)
248 mm_new = (void *)curr->mm;
249 if (bpf_core_field_exists(mm_new->mmap_lock)) {
250 if (&mm_new->mmap_lock == (void *)lock)
251 return LCD_F_MMAP_LOCK;
254 mm_old = (void *)curr->mm;
255 if (bpf_core_field_exists(mm_old->mmap_sem)) {
256 if (&mm_old->mmap_sem == (void *)lock)
257 return LCD_F_MMAP_LOCK;
260 case LCB_F_SPIN: /* spinlock */
261 curr = bpf_get_current_task_btf();
262 if (&curr->sighand->siglock == (void *)lock)
263 return LCD_F_SIGHAND_LOCK;
271 SEC("tp_btf/contention_begin")
272 int contention_begin(u64 *ctx)
275 struct tstamp_data *pelem;
277 if (!enabled || !can_record(ctx))
280 pid = bpf_get_current_pid_tgid();
281 pelem = bpf_map_lookup_elem(&tstamp, &pid);
282 if (pelem && pelem->lock)
286 struct tstamp_data zero = {};
288 bpf_map_update_elem(&tstamp, &pid, &zero, BPF_ANY);
289 pelem = bpf_map_lookup_elem(&tstamp, &pid);
291 __sync_fetch_and_add(&task_fail, 1);
296 pelem->timestamp = bpf_ktime_get_ns();
297 pelem->lock = (__u64)ctx[0];
298 pelem->flags = (__u32)ctx[1];
300 if (needs_callstack) {
301 pelem->stack_id = bpf_get_stackid(ctx, &stacks,
302 BPF_F_FAST_STACK_CMP | stack_skip);
303 if (pelem->stack_id < 0)
304 __sync_fetch_and_add(&stack_fail, 1);
305 } else if (aggr_mode == LOCK_AGGR_TASK) {
306 struct task_struct *task;
309 task = get_lock_owner(pelem->lock, pelem->flags);
311 /* The flags is not used anymore. Pass the owner pid. */
313 pelem->flags = BPF_CORE_READ(task, pid);
318 task = bpf_get_current_task_btf();
322 if (update_task_data(task) < 0 && lock_owner)
330 SEC("tp_btf/contention_end")
331 int contention_end(u64 *ctx)
334 struct tstamp_data *pelem;
335 struct contention_key key = {};
336 struct contention_data *data;
342 pid = bpf_get_current_pid_tgid();
343 pelem = bpf_map_lookup_elem(&tstamp, &pid);
344 if (!pelem || pelem->lock != ctx[0])
347 duration = bpf_ktime_get_ns() - pelem->timestamp;
348 if ((__s64)duration < 0) {
349 bpf_map_delete_elem(&tstamp, &pid);
350 __sync_fetch_and_add(&time_fail, 1);
355 case LOCK_AGGR_CALLER:
356 key.stack_id = pelem->stack_id;
360 key.pid = pelem->flags;
364 key.stack_id = pelem->stack_id;
367 key.lock_addr = pelem->lock;
369 key.stack_id = pelem->stack_id;
372 /* should not happen */
376 data = bpf_map_lookup_elem(&lock_stat, &key);
379 bpf_map_delete_elem(&tstamp, &pid);
380 __sync_fetch_and_add(&data_fail, 1);
384 struct contention_data first = {
385 .total_time = duration,
386 .max_time = duration,
387 .min_time = duration,
389 .flags = pelem->flags,
393 if (aggr_mode == LOCK_AGGR_ADDR)
394 first.flags |= check_lock_type(pelem->lock, pelem->flags);
396 err = bpf_map_update_elem(&lock_stat, &key, &first, BPF_NOEXIST);
400 __sync_fetch_and_add(&data_fail, 1);
402 bpf_map_delete_elem(&tstamp, &pid);
406 __sync_fetch_and_add(&data->total_time, duration);
407 __sync_fetch_and_add(&data->count, 1);
409 /* FIXME: need atomic operations */
410 if (data->max_time < duration)
411 data->max_time = duration;
412 if (data->min_time > duration)
413 data->min_time = duration;
415 bpf_map_delete_elem(&tstamp, &pid);
419 extern struct rq runqueues __ksym;
423 } __attribute__((preserve_access_index));
426 raw_spinlock_t __lock;
427 } __attribute__((preserve_access_index));
429 SEC("raw_tp/bpf_test_finish")
430 int BPF_PROG(collect_lock_syms)
435 for (int i = 0; i < MAX_CPUS; i++) {
436 struct rq *rq = bpf_per_cpu_ptr(&runqueues, i);
437 struct rq___new *rq_new = (void *)rq;
438 struct rq___old *rq_old = (void *)rq;
443 if (bpf_core_field_exists(rq_new->__lock))
444 lock_addr = (__u64)&rq_new->__lock;
446 lock_addr = (__u64)&rq_old->lock;
447 lock_flag = LOCK_CLASS_RQLOCK;
448 bpf_map_update_elem(&lock_syms, &lock_addr, &lock_flag, BPF_ANY);
453 char LICENSE[] SEC("license") = "Dual BSD/GPL";