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 /* default buffer size */
11 #define MAX_ENTRIES 10240
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(__u32));
62 __uint(value_size, sizeof(__u8));
63 __uint(max_entries, 1);
64 } cpu_filter 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 } task_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 } type_filter SEC(".maps");
81 __uint(type, BPF_MAP_TYPE_HASH);
82 __uint(key_size, sizeof(__u64));
83 __uint(value_size, sizeof(__u8));
84 __uint(max_entries, 1);
85 } addr_filter SEC(".maps");
87 struct rw_semaphore___old {
88 struct task_struct *owner;
89 } __attribute__((preserve_access_index));
91 struct rw_semaphore___new {
93 } __attribute__((preserve_access_index));
105 /* determine the key of lock stat */
111 static inline int can_record(u64 *ctx)
114 __u32 cpu = bpf_get_smp_processor_id();
117 ok = bpf_map_lookup_elem(&cpu_filter, &cpu);
124 __u32 pid = bpf_get_current_pid_tgid();
126 ok = bpf_map_lookup_elem(&task_filter, &pid);
133 __u32 flags = (__u32)ctx[1];
135 ok = bpf_map_lookup_elem(&type_filter, &flags);
144 ok = bpf_map_lookup_elem(&addr_filter, &addr);
152 static inline int update_task_data(struct task_struct *task)
154 struct contention_task_data *p;
157 err = bpf_core_read(&pid, sizeof(pid), &task->pid);
161 p = bpf_map_lookup_elem(&task_data, &pid);
163 struct contention_task_data data = {};
165 BPF_CORE_READ_STR_INTO(&data.comm, task, comm);
166 bpf_map_update_elem(&task_data, &pid, &data, BPF_NOEXIST);
172 #ifndef __has_builtin
173 # define __has_builtin(x) 0
176 static inline struct task_struct *get_lock_owner(__u64 lock, __u32 flags)
178 struct task_struct *task;
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);
194 /* assume new struct */
195 struct rw_semaphore *rwsem = (void *)lock;
196 owner = BPF_CORE_READ(rwsem, owner.counter);
203 task = (void *)(owner & ~7UL);
207 SEC("tp_btf/contention_begin")
208 int contention_begin(u64 *ctx)
211 struct tstamp_data *pelem;
213 if (!enabled || !can_record(ctx))
216 pid = bpf_get_current_pid_tgid();
217 pelem = bpf_map_lookup_elem(&tstamp, &pid);
218 if (pelem && pelem->lock)
222 struct tstamp_data zero = {};
224 bpf_map_update_elem(&tstamp, &pid, &zero, BPF_ANY);
225 pelem = bpf_map_lookup_elem(&tstamp, &pid);
232 pelem->timestamp = bpf_ktime_get_ns();
233 pelem->lock = (__u64)ctx[0];
234 pelem->flags = (__u32)ctx[1];
236 if (needs_callstack) {
237 pelem->stack_id = bpf_get_stackid(ctx, &stacks,
238 BPF_F_FAST_STACK_CMP | stack_skip);
239 if (pelem->stack_id < 0)
241 } else if (aggr_mode == LOCK_AGGR_TASK) {
242 struct task_struct *task;
245 task = get_lock_owner(pelem->lock, pelem->flags);
247 /* The flags is not used anymore. Pass the owner pid. */
249 pelem->flags = BPF_CORE_READ(task, pid);
254 task = bpf_get_current_task_btf();
258 if (update_task_data(task) < 0 && lock_owner)
266 SEC("tp_btf/contention_end")
267 int contention_end(u64 *ctx)
270 struct tstamp_data *pelem;
271 struct contention_key key = {};
272 struct contention_data *data;
278 pid = bpf_get_current_pid_tgid();
279 pelem = bpf_map_lookup_elem(&tstamp, &pid);
280 if (!pelem || pelem->lock != ctx[0])
283 duration = bpf_ktime_get_ns() - pelem->timestamp;
286 case LOCK_AGGR_CALLER:
287 key.stack_id = pelem->stack_id;
291 key.pid = pelem->flags;
295 key.stack_id = pelem->stack_id;
298 key.lock_addr = pelem->lock;
300 key.stack_id = pelem->stack_id;
303 /* should not happen */
307 data = bpf_map_lookup_elem(&lock_stat, &key);
309 struct contention_data first = {
310 .total_time = duration,
311 .max_time = duration,
312 .min_time = duration,
314 .flags = pelem->flags,
317 bpf_map_update_elem(&lock_stat, &key, &first, BPF_NOEXIST);
318 bpf_map_delete_elem(&tstamp, &pid);
322 __sync_fetch_and_add(&data->total_time, duration);
323 __sync_fetch_and_add(&data->count, 1);
325 /* FIXME: need atomic operations */
326 if (data->max_time < duration)
327 data->max_time = duration;
328 if (data->min_time > duration)
329 data->min_time = duration;
331 bpf_map_delete_elem(&tstamp, &pid);
335 char LICENSE[] SEC("license") = "Dual BSD/GPL";