1 // SPDX-License-Identifier: GPL-2.0
2 #include "util/debug.h"
3 #include "util/evlist.h"
4 #include "util/machine.h"
5 #include "util/map.h"
6 #include "util/symbol.h"
7 #include "util/target.h"
8 #include "util/thread.h"
9 #include "util/thread_map.h"
10 #include "util/lock-contention.h"
11 #include <linux/zalloc.h>
12 #include <linux/string.h>
13 #include <bpf/bpf.h>
14 
15 #include "bpf_skel/lock_contention.skel.h"
16 #include "bpf_skel/lock_data.h"
17 
18 static struct lock_contention_bpf *skel;
19 
20 int lock_contention_prepare(struct lock_contention *con)
21 {
22 	int i, fd;
23 	int ncpus = 1, ntasks = 1, ntypes = 1, naddrs = 1;
24 	struct evlist *evlist = con->evlist;
25 	struct target *target = con->target;
26 
27 	skel = lock_contention_bpf__open();
28 	if (!skel) {
29 		pr_err("Failed to open lock-contention BPF skeleton\n");
30 		return -1;
31 	}
32 
33 	bpf_map__set_value_size(skel->maps.stacks, con->max_stack * sizeof(u64));
34 	bpf_map__set_max_entries(skel->maps.lock_stat, con->map_nr_entries);
35 	bpf_map__set_max_entries(skel->maps.tstamp, con->map_nr_entries);
36 
37 	if (con->aggr_mode == LOCK_AGGR_TASK) {
38 		bpf_map__set_max_entries(skel->maps.task_data, con->map_nr_entries);
39 		bpf_map__set_max_entries(skel->maps.stacks, 1);
40 	} else {
41 		bpf_map__set_max_entries(skel->maps.task_data, 1);
42 		bpf_map__set_max_entries(skel->maps.stacks, con->map_nr_entries);
43 	}
44 
45 	if (target__has_cpu(target))
46 		ncpus = perf_cpu_map__nr(evlist->core.user_requested_cpus);
47 	if (target__has_task(target))
48 		ntasks = perf_thread_map__nr(evlist->core.threads);
49 	if (con->filters->nr_types)
50 		ntypes = con->filters->nr_types;
51 
52 	/* resolve lock name filters to addr */
53 	if (con->filters->nr_syms) {
54 		struct symbol *sym;
55 		struct map *kmap;
56 		unsigned long *addrs;
57 
58 		for (i = 0; i < con->filters->nr_syms; i++) {
59 			sym = machine__find_kernel_symbol_by_name(con->machine,
60 								  con->filters->syms[i],
61 								  &kmap);
62 			if (sym == NULL) {
63 				pr_warning("ignore unknown symbol: %s\n",
64 					   con->filters->syms[i]);
65 				continue;
66 			}
67 
68 			addrs = realloc(con->filters->addrs,
69 					(con->filters->nr_addrs + 1) * sizeof(*addrs));
70 			if (addrs == NULL) {
71 				pr_warning("memory allocation failure\n");
72 				continue;
73 			}
74 
75 			addrs[con->filters->nr_addrs++] = kmap->unmap_ip(kmap, sym->start);
76 			con->filters->addrs = addrs;
77 		}
78 		naddrs = con->filters->nr_addrs;
79 	}
80 
81 	bpf_map__set_max_entries(skel->maps.cpu_filter, ncpus);
82 	bpf_map__set_max_entries(skel->maps.task_filter, ntasks);
83 	bpf_map__set_max_entries(skel->maps.type_filter, ntypes);
84 	bpf_map__set_max_entries(skel->maps.addr_filter, naddrs);
85 
86 	if (lock_contention_bpf__load(skel) < 0) {
87 		pr_err("Failed to load lock-contention BPF skeleton\n");
88 		return -1;
89 	}
90 
91 	if (target__has_cpu(target)) {
92 		u32 cpu;
93 		u8 val = 1;
94 
95 		skel->bss->has_cpu = 1;
96 		fd = bpf_map__fd(skel->maps.cpu_filter);
97 
98 		for (i = 0; i < ncpus; i++) {
99 			cpu = perf_cpu_map__cpu(evlist->core.user_requested_cpus, i).cpu;
100 			bpf_map_update_elem(fd, &cpu, &val, BPF_ANY);
101 		}
102 	}
103 
104 	if (target__has_task(target)) {
105 		u32 pid;
106 		u8 val = 1;
107 
108 		skel->bss->has_task = 1;
109 		fd = bpf_map__fd(skel->maps.task_filter);
110 
111 		for (i = 0; i < ntasks; i++) {
112 			pid = perf_thread_map__pid(evlist->core.threads, i);
113 			bpf_map_update_elem(fd, &pid, &val, BPF_ANY);
114 		}
115 	}
116 
117 	if (target__none(target) && evlist->workload.pid > 0) {
118 		u32 pid = evlist->workload.pid;
119 		u8 val = 1;
120 
121 		skel->bss->has_task = 1;
122 		fd = bpf_map__fd(skel->maps.task_filter);
123 		bpf_map_update_elem(fd, &pid, &val, BPF_ANY);
124 	}
125 
126 	if (con->filters->nr_types) {
127 		u8 val = 1;
128 
129 		skel->bss->has_type = 1;
130 		fd = bpf_map__fd(skel->maps.type_filter);
131 
132 		for (i = 0; i < con->filters->nr_types; i++)
133 			bpf_map_update_elem(fd, &con->filters->types[i], &val, BPF_ANY);
134 	}
135 
136 	if (con->filters->nr_addrs) {
137 		u8 val = 1;
138 
139 		skel->bss->has_addr = 1;
140 		fd = bpf_map__fd(skel->maps.addr_filter);
141 
142 		for (i = 0; i < con->filters->nr_addrs; i++)
143 			bpf_map_update_elem(fd, &con->filters->addrs[i], &val, BPF_ANY);
144 	}
145 
146 	/* these don't work well if in the rodata section */
147 	skel->bss->stack_skip = con->stack_skip;
148 	skel->bss->aggr_mode = con->aggr_mode;
149 
150 	lock_contention_bpf__attach(skel);
151 	return 0;
152 }
153 
154 int lock_contention_start(void)
155 {
156 	skel->bss->enabled = 1;
157 	return 0;
158 }
159 
160 int lock_contention_stop(void)
161 {
162 	skel->bss->enabled = 0;
163 	return 0;
164 }
165 
166 int lock_contention_read(struct lock_contention *con)
167 {
168 	int fd, stack, task_fd, err = 0;
169 	struct contention_key *prev_key, key;
170 	struct contention_data data = {};
171 	struct lock_stat *st = NULL;
172 	struct machine *machine = con->machine;
173 	u64 *stack_trace;
174 	size_t stack_size = con->max_stack * sizeof(*stack_trace);
175 
176 	fd = bpf_map__fd(skel->maps.lock_stat);
177 	stack = bpf_map__fd(skel->maps.stacks);
178 	task_fd = bpf_map__fd(skel->maps.task_data);
179 
180 	con->lost = skel->bss->lost;
181 
182 	stack_trace = zalloc(stack_size);
183 	if (stack_trace == NULL)
184 		return -1;
185 
186 	if (con->aggr_mode == LOCK_AGGR_TASK) {
187 		struct thread *idle = __machine__findnew_thread(machine,
188 								/*pid=*/0,
189 								/*tid=*/0);
190 		thread__set_comm(idle, "swapper", /*timestamp=*/0);
191 	}
192 
193 	/* make sure it loads the kernel map */
194 	map__load(maps__first(machine->kmaps));
195 
196 	prev_key = NULL;
197 	while (!bpf_map_get_next_key(fd, prev_key, &key)) {
198 		struct map *kmap;
199 		struct symbol *sym;
200 		int idx = 0;
201 		s32 stack_id;
202 
203 		/* to handle errors in the loop body */
204 		err = -1;
205 
206 		bpf_map_lookup_elem(fd, &key, &data);
207 		st = zalloc(sizeof(*st));
208 		if (st == NULL)
209 			break;
210 
211 		st->nr_contended = data.count;
212 		st->wait_time_total = data.total_time;
213 		st->wait_time_max = data.max_time;
214 		st->wait_time_min = data.min_time;
215 
216 		if (data.count)
217 			st->avg_wait_time = data.total_time / data.count;
218 
219 		st->flags = data.flags;
220 		st->addr = key.aggr_key;
221 
222 		if (con->aggr_mode == LOCK_AGGR_TASK) {
223 			struct contention_task_data task;
224 			struct thread *t;
225 			int pid = key.aggr_key;
226 
227 			/* do not update idle comm which contains CPU number */
228 			if (st->addr) {
229 				bpf_map_lookup_elem(task_fd, &pid, &task);
230 				t = __machine__findnew_thread(machine, /*pid=*/-1, pid);
231 				thread__set_comm(t, task.comm, /*timestamp=*/0);
232 			}
233 			goto next;
234 		}
235 
236 		if (con->aggr_mode == LOCK_AGGR_ADDR) {
237 			sym = machine__find_kernel_symbol(machine, st->addr, &kmap);
238 			if (sym)
239 				st->name = strdup(sym->name);
240 			goto next;
241 		}
242 
243 		stack_id = key.aggr_key;
244 		bpf_map_lookup_elem(stack, &stack_id, stack_trace);
245 
246 		/* skip lock internal functions */
247 		while (machine__is_lock_function(machine, stack_trace[idx]) &&
248 		       idx < con->max_stack - 1)
249 			idx++;
250 
251 		st->addr = stack_trace[idx];
252 		sym = machine__find_kernel_symbol(machine, st->addr, &kmap);
253 
254 		if (sym) {
255 			unsigned long offset;
256 			int ret = 0;
257 
258 			offset = kmap->map_ip(kmap, st->addr) - sym->start;
259 
260 			if (offset)
261 				ret = asprintf(&st->name, "%s+%#lx", sym->name, offset);
262 			else
263 				st->name = strdup(sym->name);
264 
265 			if (ret < 0 || st->name == NULL)
266 				break;
267 		} else if (asprintf(&st->name, "%#lx", (unsigned long)st->addr) < 0) {
268 			break;
269 		}
270 
271 		if (verbose > 0) {
272 			st->callstack = memdup(stack_trace, stack_size);
273 			if (st->callstack == NULL)
274 				break;
275 		}
276 next:
277 		hlist_add_head(&st->hash_entry, con->result);
278 		prev_key = &key;
279 
280 		/* we're fine now, reset the values */
281 		st = NULL;
282 		err = 0;
283 	}
284 
285 	free(stack_trace);
286 	if (st) {
287 		free(st->name);
288 		free(st);
289 	}
290 
291 	return err;
292 }
293 
294 int lock_contention_finish(void)
295 {
296 	if (skel) {
297 		skel->bss->enabled = 0;
298 		lock_contention_bpf__destroy(skel);
299 	}
300 
301 	return 0;
302 }
303