xref: /openbmc/linux/tools/perf/arch/arm64/util/arm-spe.c (revision fe7498ef)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Arm Statistical Profiling Extensions (SPE) support
4  * Copyright (c) 2017-2018, Arm Ltd.
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/bitops.h>
10 #include <linux/log2.h>
11 #include <linux/zalloc.h>
12 #include <time.h>
13 
14 #include "../../../util/cpumap.h"
15 #include "../../../util/event.h"
16 #include "../../../util/evsel.h"
17 #include "../../../util/evsel_config.h"
18 #include "../../../util/evlist.h"
19 #include "../../../util/session.h"
20 #include <internal/lib.h> // page_size
21 #include "../../../util/pmu.h"
22 #include "../../../util/debug.h"
23 #include "../../../util/auxtrace.h"
24 #include "../../../util/record.h"
25 #include "../../../util/arm-spe.h"
26 #include <tools/libc_compat.h> // reallocarray
27 
28 #define KiB(x) ((x) * 1024)
29 #define MiB(x) ((x) * 1024 * 1024)
30 
31 struct arm_spe_recording {
32 	struct auxtrace_record		itr;
33 	struct perf_pmu			*arm_spe_pmu;
34 	struct evlist		*evlist;
35 	int			wrapped_cnt;
36 	bool			*wrapped;
37 };
38 
39 static void arm_spe_set_timestamp(struct auxtrace_record *itr,
40 				  struct evsel *evsel)
41 {
42 	struct arm_spe_recording *ptr;
43 	struct perf_pmu *arm_spe_pmu;
44 	struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG);
45 	u64 user_bits = 0, bit;
46 
47 	ptr = container_of(itr, struct arm_spe_recording, itr);
48 	arm_spe_pmu = ptr->arm_spe_pmu;
49 
50 	if (term)
51 		user_bits = term->val.cfg_chg;
52 
53 	bit = perf_pmu__format_bits(&arm_spe_pmu->format, "ts_enable");
54 
55 	/* Skip if user has set it */
56 	if (bit & user_bits)
57 		return;
58 
59 	evsel->core.attr.config |= bit;
60 }
61 
62 static size_t
63 arm_spe_info_priv_size(struct auxtrace_record *itr __maybe_unused,
64 		       struct evlist *evlist __maybe_unused)
65 {
66 	return ARM_SPE_AUXTRACE_PRIV_SIZE;
67 }
68 
69 static int arm_spe_info_fill(struct auxtrace_record *itr,
70 			     struct perf_session *session,
71 			     struct perf_record_auxtrace_info *auxtrace_info,
72 			     size_t priv_size)
73 {
74 	struct arm_spe_recording *sper =
75 			container_of(itr, struct arm_spe_recording, itr);
76 	struct perf_pmu *arm_spe_pmu = sper->arm_spe_pmu;
77 
78 	if (priv_size != ARM_SPE_AUXTRACE_PRIV_SIZE)
79 		return -EINVAL;
80 
81 	if (!session->evlist->core.nr_mmaps)
82 		return -EINVAL;
83 
84 	auxtrace_info->type = PERF_AUXTRACE_ARM_SPE;
85 	auxtrace_info->priv[ARM_SPE_PMU_TYPE] = arm_spe_pmu->type;
86 
87 	return 0;
88 }
89 
90 static void
91 arm_spe_snapshot_resolve_auxtrace_defaults(struct record_opts *opts,
92 					   bool privileged)
93 {
94 	/*
95 	 * The default snapshot size is the auxtrace mmap size. If neither auxtrace mmap size nor
96 	 * snapshot size is specified, then the default is 4MiB for privileged users, 128KiB for
97 	 * unprivileged users.
98 	 *
99 	 * The default auxtrace mmap size is 4MiB/page_size for privileged users, 128KiB for
100 	 * unprivileged users. If an unprivileged user does not specify mmap pages, the mmap pages
101 	 * will be reduced from the default 512KiB/page_size to 256KiB/page_size, otherwise the
102 	 * user is likely to get an error as they exceed their mlock limmit.
103 	 */
104 
105 	/*
106 	 * No size were given to '-S' or '-m,', so go with the default
107 	 */
108 	if (!opts->auxtrace_snapshot_size && !opts->auxtrace_mmap_pages) {
109 		if (privileged) {
110 			opts->auxtrace_mmap_pages = MiB(4) / page_size;
111 		} else {
112 			opts->auxtrace_mmap_pages = KiB(128) / page_size;
113 			if (opts->mmap_pages == UINT_MAX)
114 				opts->mmap_pages = KiB(256) / page_size;
115 		}
116 	} else if (!opts->auxtrace_mmap_pages && !privileged && opts->mmap_pages == UINT_MAX) {
117 		opts->mmap_pages = KiB(256) / page_size;
118 	}
119 
120 	/*
121 	 * '-m,xyz' was specified but no snapshot size, so make the snapshot size as big as the
122 	 * auxtrace mmap area.
123 	 */
124 	if (!opts->auxtrace_snapshot_size)
125 		opts->auxtrace_snapshot_size = opts->auxtrace_mmap_pages * (size_t)page_size;
126 
127 	/*
128 	 * '-Sxyz' was specified but no auxtrace mmap area, so make the auxtrace mmap area big
129 	 * enough to fit the requested snapshot size.
130 	 */
131 	if (!opts->auxtrace_mmap_pages) {
132 		size_t sz = opts->auxtrace_snapshot_size;
133 
134 		sz = round_up(sz, page_size) / page_size;
135 		opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
136 	}
137 }
138 
139 static int arm_spe_recording_options(struct auxtrace_record *itr,
140 				     struct evlist *evlist,
141 				     struct record_opts *opts)
142 {
143 	struct arm_spe_recording *sper =
144 			container_of(itr, struct arm_spe_recording, itr);
145 	struct perf_pmu *arm_spe_pmu = sper->arm_spe_pmu;
146 	struct evsel *evsel, *arm_spe_evsel = NULL;
147 	struct perf_cpu_map *cpus = evlist->core.cpus;
148 	bool privileged = perf_event_paranoid_check(-1);
149 	struct evsel *tracking_evsel;
150 	int err;
151 
152 	sper->evlist = evlist;
153 
154 	evlist__for_each_entry(evlist, evsel) {
155 		if (evsel->core.attr.type == arm_spe_pmu->type) {
156 			if (arm_spe_evsel) {
157 				pr_err("There may be only one " ARM_SPE_PMU_NAME "x event\n");
158 				return -EINVAL;
159 			}
160 			evsel->core.attr.freq = 0;
161 			evsel->core.attr.sample_period = 1;
162 			arm_spe_evsel = evsel;
163 			opts->full_auxtrace = true;
164 		}
165 	}
166 
167 	if (!opts->full_auxtrace)
168 		return 0;
169 
170 	/*
171 	 * we are in snapshot mode.
172 	 */
173 	if (opts->auxtrace_snapshot_mode) {
174 		/*
175 		 * Command arguments '-Sxyz' and/or '-m,xyz' are missing, so fill those in with
176 		 * default values.
177 		 */
178 		if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages)
179 			arm_spe_snapshot_resolve_auxtrace_defaults(opts, privileged);
180 
181 		/*
182 		 * Snapshot size can't be bigger than the auxtrace area.
183 		 */
184 		if (opts->auxtrace_snapshot_size > opts->auxtrace_mmap_pages * (size_t)page_size) {
185 			pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
186 			       opts->auxtrace_snapshot_size,
187 			       opts->auxtrace_mmap_pages * (size_t)page_size);
188 			return -EINVAL;
189 		}
190 
191 		/*
192 		 * Something went wrong somewhere - this shouldn't happen.
193 		 */
194 		if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages) {
195 			pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
196 			return -EINVAL;
197 		}
198 	}
199 
200 	/* We are in full trace mode but '-m,xyz' wasn't specified */
201 	if (!opts->auxtrace_mmap_pages) {
202 		if (privileged) {
203 			opts->auxtrace_mmap_pages = MiB(4) / page_size;
204 		} else {
205 			opts->auxtrace_mmap_pages = KiB(128) / page_size;
206 			if (opts->mmap_pages == UINT_MAX)
207 				opts->mmap_pages = KiB(256) / page_size;
208 		}
209 	}
210 
211 	/* Validate auxtrace_mmap_pages */
212 	if (opts->auxtrace_mmap_pages) {
213 		size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
214 		size_t min_sz = KiB(8);
215 
216 		if (sz < min_sz || !is_power_of_2(sz)) {
217 			pr_err("Invalid mmap size for ARM SPE: must be at least %zuKiB and a power of 2\n",
218 			       min_sz / 1024);
219 			return -EINVAL;
220 		}
221 	}
222 
223 	if (opts->auxtrace_snapshot_mode)
224 		pr_debug2("%sx snapshot size: %zu\n", ARM_SPE_PMU_NAME,
225 			  opts->auxtrace_snapshot_size);
226 
227 	/*
228 	 * To obtain the auxtrace buffer file descriptor, the auxtrace event
229 	 * must come first.
230 	 */
231 	evlist__to_front(evlist, arm_spe_evsel);
232 
233 	/*
234 	 * In the case of per-cpu mmaps, sample CPU for AUX event;
235 	 * also enable the timestamp tracing for samples correlation.
236 	 */
237 	if (!perf_cpu_map__empty(cpus)) {
238 		evsel__set_sample_bit(arm_spe_evsel, CPU);
239 		arm_spe_set_timestamp(itr, arm_spe_evsel);
240 	}
241 
242 	/* Add dummy event to keep tracking */
243 	err = parse_events(evlist, "dummy:u", NULL);
244 	if (err)
245 		return err;
246 
247 	tracking_evsel = evlist__last(evlist);
248 	evlist__set_tracking_event(evlist, tracking_evsel);
249 
250 	tracking_evsel->core.attr.freq = 0;
251 	tracking_evsel->core.attr.sample_period = 1;
252 
253 	/* In per-cpu case, always need the time of mmap events etc */
254 	if (!perf_cpu_map__empty(cpus)) {
255 		evsel__set_sample_bit(tracking_evsel, TIME);
256 		evsel__set_sample_bit(tracking_evsel, CPU);
257 
258 		/* also track task context switch */
259 		if (!record_opts__no_switch_events(opts))
260 			tracking_evsel->core.attr.context_switch = 1;
261 	}
262 
263 	return 0;
264 }
265 
266 static int arm_spe_parse_snapshot_options(struct auxtrace_record *itr __maybe_unused,
267 					 struct record_opts *opts,
268 					 const char *str)
269 {
270 	unsigned long long snapshot_size = 0;
271 	char *endptr;
272 
273 	if (str) {
274 		snapshot_size = strtoull(str, &endptr, 0);
275 		if (*endptr || snapshot_size > SIZE_MAX)
276 			return -1;
277 	}
278 
279 	opts->auxtrace_snapshot_mode = true;
280 	opts->auxtrace_snapshot_size = snapshot_size;
281 
282 	return 0;
283 }
284 
285 static int arm_spe_snapshot_start(struct auxtrace_record *itr)
286 {
287 	struct arm_spe_recording *ptr =
288 			container_of(itr, struct arm_spe_recording, itr);
289 	struct evsel *evsel;
290 
291 	evlist__for_each_entry(ptr->evlist, evsel) {
292 		if (evsel->core.attr.type == ptr->arm_spe_pmu->type)
293 			return evsel__disable(evsel);
294 	}
295 	return -EINVAL;
296 }
297 
298 static int arm_spe_snapshot_finish(struct auxtrace_record *itr)
299 {
300 	struct arm_spe_recording *ptr =
301 			container_of(itr, struct arm_spe_recording, itr);
302 	struct evsel *evsel;
303 
304 	evlist__for_each_entry(ptr->evlist, evsel) {
305 		if (evsel->core.attr.type == ptr->arm_spe_pmu->type)
306 			return evsel__enable(evsel);
307 	}
308 	return -EINVAL;
309 }
310 
311 static int arm_spe_alloc_wrapped_array(struct arm_spe_recording *ptr, int idx)
312 {
313 	bool *wrapped;
314 	int cnt = ptr->wrapped_cnt, new_cnt, i;
315 
316 	/*
317 	 * No need to allocate, so return early.
318 	 */
319 	if (idx < cnt)
320 		return 0;
321 
322 	/*
323 	 * Make ptr->wrapped as big as idx.
324 	 */
325 	new_cnt = idx + 1;
326 
327 	/*
328 	 * Free'ed in arm_spe_recording_free().
329 	 */
330 	wrapped = reallocarray(ptr->wrapped, new_cnt, sizeof(bool));
331 	if (!wrapped)
332 		return -ENOMEM;
333 
334 	/*
335 	 * init new allocated values.
336 	 */
337 	for (i = cnt; i < new_cnt; i++)
338 		wrapped[i] = false;
339 
340 	ptr->wrapped_cnt = new_cnt;
341 	ptr->wrapped = wrapped;
342 
343 	return 0;
344 }
345 
346 static bool arm_spe_buffer_has_wrapped(unsigned char *buffer,
347 				      size_t buffer_size, u64 head)
348 {
349 	u64 i, watermark;
350 	u64 *buf = (u64 *)buffer;
351 	size_t buf_size = buffer_size;
352 
353 	/*
354 	 * Defensively handle the case where head might be continually increasing - if its value is
355 	 * equal or greater than the size of the ring buffer, then we can safely determine it has
356 	 * wrapped around. Otherwise, continue to detect if head might have wrapped.
357 	 */
358 	if (head >= buffer_size)
359 		return true;
360 
361 	/*
362 	 * We want to look the very last 512 byte (chosen arbitrarily) in the ring buffer.
363 	 */
364 	watermark = buf_size - 512;
365 
366 	/*
367 	 * The value of head is somewhere within the size of the ring buffer. This can be that there
368 	 * hasn't been enough data to fill the ring buffer yet or the trace time was so long that
369 	 * head has numerically wrapped around.  To find we need to check if we have data at the
370 	 * very end of the ring buffer.  We can reliably do this because mmap'ed pages are zeroed
371 	 * out and there is a fresh mapping with every new session.
372 	 */
373 
374 	/*
375 	 * head is less than 512 byte from the end of the ring buffer.
376 	 */
377 	if (head > watermark)
378 		watermark = head;
379 
380 	/*
381 	 * Speed things up by using 64 bit transactions (see "u64 *buf" above)
382 	 */
383 	watermark /= sizeof(u64);
384 	buf_size /= sizeof(u64);
385 
386 	/*
387 	 * If we find trace data at the end of the ring buffer, head has been there and has
388 	 * numerically wrapped around at least once.
389 	 */
390 	for (i = watermark; i < buf_size; i++)
391 		if (buf[i])
392 			return true;
393 
394 	return false;
395 }
396 
397 static int arm_spe_find_snapshot(struct auxtrace_record *itr, int idx,
398 				  struct auxtrace_mmap *mm, unsigned char *data,
399 				  u64 *head, u64 *old)
400 {
401 	int err;
402 	bool wrapped;
403 	struct arm_spe_recording *ptr =
404 			container_of(itr, struct arm_spe_recording, itr);
405 
406 	/*
407 	 * Allocate memory to keep track of wrapping if this is the first
408 	 * time we deal with this *mm.
409 	 */
410 	if (idx >= ptr->wrapped_cnt) {
411 		err = arm_spe_alloc_wrapped_array(ptr, idx);
412 		if (err)
413 			return err;
414 	}
415 
416 	/*
417 	 * Check to see if *head has wrapped around.  If it hasn't only the
418 	 * amount of data between *head and *old is snapshot'ed to avoid
419 	 * bloating the perf.data file with zeros.  But as soon as *head has
420 	 * wrapped around the entire size of the AUX ring buffer it taken.
421 	 */
422 	wrapped = ptr->wrapped[idx];
423 	if (!wrapped && arm_spe_buffer_has_wrapped(data, mm->len, *head)) {
424 		wrapped = true;
425 		ptr->wrapped[idx] = true;
426 	}
427 
428 	pr_debug3("%s: mmap index %d old head %zu new head %zu size %zu\n",
429 		  __func__, idx, (size_t)*old, (size_t)*head, mm->len);
430 
431 	/*
432 	 * No wrap has occurred, we can just use *head and *old.
433 	 */
434 	if (!wrapped)
435 		return 0;
436 
437 	/*
438 	 * *head has wrapped around - adjust *head and *old to pickup the
439 	 * entire content of the AUX buffer.
440 	 */
441 	if (*head >= mm->len) {
442 		*old = *head - mm->len;
443 	} else {
444 		*head += mm->len;
445 		*old = *head - mm->len;
446 	}
447 
448 	return 0;
449 }
450 
451 static u64 arm_spe_reference(struct auxtrace_record *itr __maybe_unused)
452 {
453 	struct timespec ts;
454 
455 	clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
456 
457 	return ts.tv_sec ^ ts.tv_nsec;
458 }
459 
460 static void arm_spe_recording_free(struct auxtrace_record *itr)
461 {
462 	struct arm_spe_recording *sper =
463 			container_of(itr, struct arm_spe_recording, itr);
464 
465 	free(sper->wrapped);
466 	free(sper);
467 }
468 
469 struct auxtrace_record *arm_spe_recording_init(int *err,
470 					       struct perf_pmu *arm_spe_pmu)
471 {
472 	struct arm_spe_recording *sper;
473 
474 	if (!arm_spe_pmu) {
475 		*err = -ENODEV;
476 		return NULL;
477 	}
478 
479 	sper = zalloc(sizeof(struct arm_spe_recording));
480 	if (!sper) {
481 		*err = -ENOMEM;
482 		return NULL;
483 	}
484 
485 	sper->arm_spe_pmu = arm_spe_pmu;
486 	sper->itr.pmu = arm_spe_pmu;
487 	sper->itr.snapshot_start = arm_spe_snapshot_start;
488 	sper->itr.snapshot_finish = arm_spe_snapshot_finish;
489 	sper->itr.find_snapshot = arm_spe_find_snapshot;
490 	sper->itr.parse_snapshot_options = arm_spe_parse_snapshot_options;
491 	sper->itr.recording_options = arm_spe_recording_options;
492 	sper->itr.info_priv_size = arm_spe_info_priv_size;
493 	sper->itr.info_fill = arm_spe_info_fill;
494 	sper->itr.free = arm_spe_recording_free;
495 	sper->itr.reference = arm_spe_reference;
496 	sper->itr.read_finish = auxtrace_record__read_finish;
497 	sper->itr.alignment = 0;
498 
499 	*err = 0;
500 	return &sper->itr;
501 }
502 
503 struct perf_event_attr
504 *arm_spe_pmu_default_config(struct perf_pmu *arm_spe_pmu)
505 {
506 	struct perf_event_attr *attr;
507 
508 	attr = zalloc(sizeof(struct perf_event_attr));
509 	if (!attr) {
510 		pr_err("arm_spe default config cannot allocate a perf_event_attr\n");
511 		return NULL;
512 	}
513 
514 	/*
515 	 * If kernel driver doesn't advertise a minimum,
516 	 * use max allowable by PMSIDR_EL1.INTERVAL
517 	 */
518 	if (perf_pmu__scan_file(arm_spe_pmu, "caps/min_interval", "%llu",
519 				  &attr->sample_period) != 1) {
520 		pr_debug("arm_spe driver doesn't advertise a min. interval. Using 4096\n");
521 		attr->sample_period = 4096;
522 	}
523 
524 	arm_spe_pmu->selectable = true;
525 	arm_spe_pmu->is_uncore = false;
526 
527 	return attr;
528 }
529