xref: /openbmc/linux/tools/perf/arch/arm64/util/arm-spe.c (revision 53f9cd5c)
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.user_requested_cpus;
148 	bool privileged = perf_event_paranoid_check(-1);
149 	struct evsel *tracking_evsel;
150 	int err;
151 	u64 bit;
152 
153 	sper->evlist = evlist;
154 
155 	evlist__for_each_entry(evlist, evsel) {
156 		if (evsel->core.attr.type == arm_spe_pmu->type) {
157 			if (arm_spe_evsel) {
158 				pr_err("There may be only one " ARM_SPE_PMU_NAME "x event\n");
159 				return -EINVAL;
160 			}
161 			evsel->core.attr.freq = 0;
162 			evsel->core.attr.sample_period = arm_spe_pmu->default_config->sample_period;
163 			evsel->needs_auxtrace_mmap = true;
164 			arm_spe_evsel = evsel;
165 			opts->full_auxtrace = true;
166 		}
167 	}
168 
169 	if (!opts->full_auxtrace)
170 		return 0;
171 
172 	/*
173 	 * we are in snapshot mode.
174 	 */
175 	if (opts->auxtrace_snapshot_mode) {
176 		/*
177 		 * Command arguments '-Sxyz' and/or '-m,xyz' are missing, so fill those in with
178 		 * default values.
179 		 */
180 		if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages)
181 			arm_spe_snapshot_resolve_auxtrace_defaults(opts, privileged);
182 
183 		/*
184 		 * Snapshot size can't be bigger than the auxtrace area.
185 		 */
186 		if (opts->auxtrace_snapshot_size > opts->auxtrace_mmap_pages * (size_t)page_size) {
187 			pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
188 			       opts->auxtrace_snapshot_size,
189 			       opts->auxtrace_mmap_pages * (size_t)page_size);
190 			return -EINVAL;
191 		}
192 
193 		/*
194 		 * Something went wrong somewhere - this shouldn't happen.
195 		 */
196 		if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages) {
197 			pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
198 			return -EINVAL;
199 		}
200 	}
201 
202 	/* We are in full trace mode but '-m,xyz' wasn't specified */
203 	if (!opts->auxtrace_mmap_pages) {
204 		if (privileged) {
205 			opts->auxtrace_mmap_pages = MiB(4) / page_size;
206 		} else {
207 			opts->auxtrace_mmap_pages = KiB(128) / page_size;
208 			if (opts->mmap_pages == UINT_MAX)
209 				opts->mmap_pages = KiB(256) / page_size;
210 		}
211 	}
212 
213 	/* Validate auxtrace_mmap_pages */
214 	if (opts->auxtrace_mmap_pages) {
215 		size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
216 		size_t min_sz = KiB(8);
217 
218 		if (sz < min_sz || !is_power_of_2(sz)) {
219 			pr_err("Invalid mmap size for ARM SPE: must be at least %zuKiB and a power of 2\n",
220 			       min_sz / 1024);
221 			return -EINVAL;
222 		}
223 	}
224 
225 	if (opts->auxtrace_snapshot_mode)
226 		pr_debug2("%sx snapshot size: %zu\n", ARM_SPE_PMU_NAME,
227 			  opts->auxtrace_snapshot_size);
228 
229 	/*
230 	 * To obtain the auxtrace buffer file descriptor, the auxtrace event
231 	 * must come first.
232 	 */
233 	evlist__to_front(evlist, arm_spe_evsel);
234 
235 	/*
236 	 * In the case of per-cpu mmaps, sample CPU for AUX event;
237 	 * also enable the timestamp tracing for samples correlation.
238 	 */
239 	if (!perf_cpu_map__empty(cpus)) {
240 		evsel__set_sample_bit(arm_spe_evsel, CPU);
241 		arm_spe_set_timestamp(itr, arm_spe_evsel);
242 	}
243 
244 	/*
245 	 * Set this only so that perf report knows that SPE generates memory info. It has no effect
246 	 * on the opening of the event or the SPE data produced.
247 	 */
248 	evsel__set_sample_bit(arm_spe_evsel, DATA_SRC);
249 
250 	/*
251 	 * The PHYS_ADDR flag does not affect the driver behaviour, it is used to
252 	 * inform that the resulting output's SPE samples contain physical addresses
253 	 * where applicable.
254 	 */
255 	bit = perf_pmu__format_bits(&arm_spe_pmu->format, "pa_enable");
256 	if (arm_spe_evsel->core.attr.config & bit)
257 		evsel__set_sample_bit(arm_spe_evsel, PHYS_ADDR);
258 
259 	/* Add dummy event to keep tracking */
260 	err = parse_event(evlist, "dummy:u");
261 	if (err)
262 		return err;
263 
264 	tracking_evsel = evlist__last(evlist);
265 	evlist__set_tracking_event(evlist, tracking_evsel);
266 
267 	tracking_evsel->core.attr.freq = 0;
268 	tracking_evsel->core.attr.sample_period = 1;
269 
270 	/* In per-cpu case, always need the time of mmap events etc */
271 	if (!perf_cpu_map__empty(cpus)) {
272 		evsel__set_sample_bit(tracking_evsel, TIME);
273 		evsel__set_sample_bit(tracking_evsel, CPU);
274 
275 		/* also track task context switch */
276 		if (!record_opts__no_switch_events(opts))
277 			tracking_evsel->core.attr.context_switch = 1;
278 	}
279 
280 	return 0;
281 }
282 
283 static int arm_spe_parse_snapshot_options(struct auxtrace_record *itr __maybe_unused,
284 					 struct record_opts *opts,
285 					 const char *str)
286 {
287 	unsigned long long snapshot_size = 0;
288 	char *endptr;
289 
290 	if (str) {
291 		snapshot_size = strtoull(str, &endptr, 0);
292 		if (*endptr || snapshot_size > SIZE_MAX)
293 			return -1;
294 	}
295 
296 	opts->auxtrace_snapshot_mode = true;
297 	opts->auxtrace_snapshot_size = snapshot_size;
298 
299 	return 0;
300 }
301 
302 static int arm_spe_snapshot_start(struct auxtrace_record *itr)
303 {
304 	struct arm_spe_recording *ptr =
305 			container_of(itr, struct arm_spe_recording, itr);
306 	struct evsel *evsel;
307 
308 	evlist__for_each_entry(ptr->evlist, evsel) {
309 		if (evsel->core.attr.type == ptr->arm_spe_pmu->type)
310 			return evsel__disable(evsel);
311 	}
312 	return -EINVAL;
313 }
314 
315 static int arm_spe_snapshot_finish(struct auxtrace_record *itr)
316 {
317 	struct arm_spe_recording *ptr =
318 			container_of(itr, struct arm_spe_recording, itr);
319 	struct evsel *evsel;
320 
321 	evlist__for_each_entry(ptr->evlist, evsel) {
322 		if (evsel->core.attr.type == ptr->arm_spe_pmu->type)
323 			return evsel__enable(evsel);
324 	}
325 	return -EINVAL;
326 }
327 
328 static int arm_spe_alloc_wrapped_array(struct arm_spe_recording *ptr, int idx)
329 {
330 	bool *wrapped;
331 	int cnt = ptr->wrapped_cnt, new_cnt, i;
332 
333 	/*
334 	 * No need to allocate, so return early.
335 	 */
336 	if (idx < cnt)
337 		return 0;
338 
339 	/*
340 	 * Make ptr->wrapped as big as idx.
341 	 */
342 	new_cnt = idx + 1;
343 
344 	/*
345 	 * Free'ed in arm_spe_recording_free().
346 	 */
347 	wrapped = reallocarray(ptr->wrapped, new_cnt, sizeof(bool));
348 	if (!wrapped)
349 		return -ENOMEM;
350 
351 	/*
352 	 * init new allocated values.
353 	 */
354 	for (i = cnt; i < new_cnt; i++)
355 		wrapped[i] = false;
356 
357 	ptr->wrapped_cnt = new_cnt;
358 	ptr->wrapped = wrapped;
359 
360 	return 0;
361 }
362 
363 static bool arm_spe_buffer_has_wrapped(unsigned char *buffer,
364 				      size_t buffer_size, u64 head)
365 {
366 	u64 i, watermark;
367 	u64 *buf = (u64 *)buffer;
368 	size_t buf_size = buffer_size;
369 
370 	/*
371 	 * Defensively handle the case where head might be continually increasing - if its value is
372 	 * equal or greater than the size of the ring buffer, then we can safely determine it has
373 	 * wrapped around. Otherwise, continue to detect if head might have wrapped.
374 	 */
375 	if (head >= buffer_size)
376 		return true;
377 
378 	/*
379 	 * We want to look the very last 512 byte (chosen arbitrarily) in the ring buffer.
380 	 */
381 	watermark = buf_size - 512;
382 
383 	/*
384 	 * The value of head is somewhere within the size of the ring buffer. This can be that there
385 	 * hasn't been enough data to fill the ring buffer yet or the trace time was so long that
386 	 * head has numerically wrapped around.  To find we need to check if we have data at the
387 	 * very end of the ring buffer.  We can reliably do this because mmap'ed pages are zeroed
388 	 * out and there is a fresh mapping with every new session.
389 	 */
390 
391 	/*
392 	 * head is less than 512 byte from the end of the ring buffer.
393 	 */
394 	if (head > watermark)
395 		watermark = head;
396 
397 	/*
398 	 * Speed things up by using 64 bit transactions (see "u64 *buf" above)
399 	 */
400 	watermark /= sizeof(u64);
401 	buf_size /= sizeof(u64);
402 
403 	/*
404 	 * If we find trace data at the end of the ring buffer, head has been there and has
405 	 * numerically wrapped around at least once.
406 	 */
407 	for (i = watermark; i < buf_size; i++)
408 		if (buf[i])
409 			return true;
410 
411 	return false;
412 }
413 
414 static int arm_spe_find_snapshot(struct auxtrace_record *itr, int idx,
415 				  struct auxtrace_mmap *mm, unsigned char *data,
416 				  u64 *head, u64 *old)
417 {
418 	int err;
419 	bool wrapped;
420 	struct arm_spe_recording *ptr =
421 			container_of(itr, struct arm_spe_recording, itr);
422 
423 	/*
424 	 * Allocate memory to keep track of wrapping if this is the first
425 	 * time we deal with this *mm.
426 	 */
427 	if (idx >= ptr->wrapped_cnt) {
428 		err = arm_spe_alloc_wrapped_array(ptr, idx);
429 		if (err)
430 			return err;
431 	}
432 
433 	/*
434 	 * Check to see if *head has wrapped around.  If it hasn't only the
435 	 * amount of data between *head and *old is snapshot'ed to avoid
436 	 * bloating the perf.data file with zeros.  But as soon as *head has
437 	 * wrapped around the entire size of the AUX ring buffer it taken.
438 	 */
439 	wrapped = ptr->wrapped[idx];
440 	if (!wrapped && arm_spe_buffer_has_wrapped(data, mm->len, *head)) {
441 		wrapped = true;
442 		ptr->wrapped[idx] = true;
443 	}
444 
445 	pr_debug3("%s: mmap index %d old head %zu new head %zu size %zu\n",
446 		  __func__, idx, (size_t)*old, (size_t)*head, mm->len);
447 
448 	/*
449 	 * No wrap has occurred, we can just use *head and *old.
450 	 */
451 	if (!wrapped)
452 		return 0;
453 
454 	/*
455 	 * *head has wrapped around - adjust *head and *old to pickup the
456 	 * entire content of the AUX buffer.
457 	 */
458 	if (*head >= mm->len) {
459 		*old = *head - mm->len;
460 	} else {
461 		*head += mm->len;
462 		*old = *head - mm->len;
463 	}
464 
465 	return 0;
466 }
467 
468 static u64 arm_spe_reference(struct auxtrace_record *itr __maybe_unused)
469 {
470 	struct timespec ts;
471 
472 	clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
473 
474 	return ts.tv_sec ^ ts.tv_nsec;
475 }
476 
477 static void arm_spe_recording_free(struct auxtrace_record *itr)
478 {
479 	struct arm_spe_recording *sper =
480 			container_of(itr, struct arm_spe_recording, itr);
481 
482 	free(sper->wrapped);
483 	free(sper);
484 }
485 
486 struct auxtrace_record *arm_spe_recording_init(int *err,
487 					       struct perf_pmu *arm_spe_pmu)
488 {
489 	struct arm_spe_recording *sper;
490 
491 	if (!arm_spe_pmu) {
492 		*err = -ENODEV;
493 		return NULL;
494 	}
495 
496 	sper = zalloc(sizeof(struct arm_spe_recording));
497 	if (!sper) {
498 		*err = -ENOMEM;
499 		return NULL;
500 	}
501 
502 	sper->arm_spe_pmu = arm_spe_pmu;
503 	sper->itr.pmu = arm_spe_pmu;
504 	sper->itr.snapshot_start = arm_spe_snapshot_start;
505 	sper->itr.snapshot_finish = arm_spe_snapshot_finish;
506 	sper->itr.find_snapshot = arm_spe_find_snapshot;
507 	sper->itr.parse_snapshot_options = arm_spe_parse_snapshot_options;
508 	sper->itr.recording_options = arm_spe_recording_options;
509 	sper->itr.info_priv_size = arm_spe_info_priv_size;
510 	sper->itr.info_fill = arm_spe_info_fill;
511 	sper->itr.free = arm_spe_recording_free;
512 	sper->itr.reference = arm_spe_reference;
513 	sper->itr.read_finish = auxtrace_record__read_finish;
514 	sper->itr.alignment = 0;
515 
516 	*err = 0;
517 	return &sper->itr;
518 }
519 
520 struct perf_event_attr
521 *arm_spe_pmu_default_config(struct perf_pmu *arm_spe_pmu)
522 {
523 	struct perf_event_attr *attr;
524 
525 	attr = zalloc(sizeof(struct perf_event_attr));
526 	if (!attr) {
527 		pr_err("arm_spe default config cannot allocate a perf_event_attr\n");
528 		return NULL;
529 	}
530 
531 	/*
532 	 * If kernel driver doesn't advertise a minimum,
533 	 * use max allowable by PMSIDR_EL1.INTERVAL
534 	 */
535 	if (perf_pmu__scan_file(arm_spe_pmu, "caps/min_interval", "%llu",
536 				  &attr->sample_period) != 1) {
537 		pr_debug("arm_spe driver doesn't advertise a min. interval. Using 4096\n");
538 		attr->sample_period = 4096;
539 	}
540 
541 	arm_spe_pmu->selectable = true;
542 	arm_spe_pmu->is_uncore = false;
543 
544 	return attr;
545 }
546