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