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 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 = arm_spe_pmu->default_config->sample_period; 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 /* 243 * Set this only so that perf report knows that SPE generates memory info. It has no effect 244 * on the opening of the event or the SPE data produced. 245 */ 246 evsel__set_sample_bit(arm_spe_evsel, DATA_SRC); 247 248 /* Add dummy event to keep tracking */ 249 err = parse_events(evlist, "dummy:u", NULL); 250 if (err) 251 return err; 252 253 tracking_evsel = evlist__last(evlist); 254 evlist__set_tracking_event(evlist, tracking_evsel); 255 256 tracking_evsel->core.attr.freq = 0; 257 tracking_evsel->core.attr.sample_period = 1; 258 259 /* In per-cpu case, always need the time of mmap events etc */ 260 if (!perf_cpu_map__empty(cpus)) { 261 evsel__set_sample_bit(tracking_evsel, TIME); 262 evsel__set_sample_bit(tracking_evsel, CPU); 263 264 /* also track task context switch */ 265 if (!record_opts__no_switch_events(opts)) 266 tracking_evsel->core.attr.context_switch = 1; 267 } 268 269 return 0; 270 } 271 272 static int arm_spe_parse_snapshot_options(struct auxtrace_record *itr __maybe_unused, 273 struct record_opts *opts, 274 const char *str) 275 { 276 unsigned long long snapshot_size = 0; 277 char *endptr; 278 279 if (str) { 280 snapshot_size = strtoull(str, &endptr, 0); 281 if (*endptr || snapshot_size > SIZE_MAX) 282 return -1; 283 } 284 285 opts->auxtrace_snapshot_mode = true; 286 opts->auxtrace_snapshot_size = snapshot_size; 287 288 return 0; 289 } 290 291 static int arm_spe_snapshot_start(struct auxtrace_record *itr) 292 { 293 struct arm_spe_recording *ptr = 294 container_of(itr, struct arm_spe_recording, itr); 295 struct evsel *evsel; 296 297 evlist__for_each_entry(ptr->evlist, evsel) { 298 if (evsel->core.attr.type == ptr->arm_spe_pmu->type) 299 return evsel__disable(evsel); 300 } 301 return -EINVAL; 302 } 303 304 static int arm_spe_snapshot_finish(struct auxtrace_record *itr) 305 { 306 struct arm_spe_recording *ptr = 307 container_of(itr, struct arm_spe_recording, itr); 308 struct evsel *evsel; 309 310 evlist__for_each_entry(ptr->evlist, evsel) { 311 if (evsel->core.attr.type == ptr->arm_spe_pmu->type) 312 return evsel__enable(evsel); 313 } 314 return -EINVAL; 315 } 316 317 static int arm_spe_alloc_wrapped_array(struct arm_spe_recording *ptr, int idx) 318 { 319 bool *wrapped; 320 int cnt = ptr->wrapped_cnt, new_cnt, i; 321 322 /* 323 * No need to allocate, so return early. 324 */ 325 if (idx < cnt) 326 return 0; 327 328 /* 329 * Make ptr->wrapped as big as idx. 330 */ 331 new_cnt = idx + 1; 332 333 /* 334 * Free'ed in arm_spe_recording_free(). 335 */ 336 wrapped = reallocarray(ptr->wrapped, new_cnt, sizeof(bool)); 337 if (!wrapped) 338 return -ENOMEM; 339 340 /* 341 * init new allocated values. 342 */ 343 for (i = cnt; i < new_cnt; i++) 344 wrapped[i] = false; 345 346 ptr->wrapped_cnt = new_cnt; 347 ptr->wrapped = wrapped; 348 349 return 0; 350 } 351 352 static bool arm_spe_buffer_has_wrapped(unsigned char *buffer, 353 size_t buffer_size, u64 head) 354 { 355 u64 i, watermark; 356 u64 *buf = (u64 *)buffer; 357 size_t buf_size = buffer_size; 358 359 /* 360 * Defensively handle the case where head might be continually increasing - if its value is 361 * equal or greater than the size of the ring buffer, then we can safely determine it has 362 * wrapped around. Otherwise, continue to detect if head might have wrapped. 363 */ 364 if (head >= buffer_size) 365 return true; 366 367 /* 368 * We want to look the very last 512 byte (chosen arbitrarily) in the ring buffer. 369 */ 370 watermark = buf_size - 512; 371 372 /* 373 * The value of head is somewhere within the size of the ring buffer. This can be that there 374 * hasn't been enough data to fill the ring buffer yet or the trace time was so long that 375 * head has numerically wrapped around. To find we need to check if we have data at the 376 * very end of the ring buffer. We can reliably do this because mmap'ed pages are zeroed 377 * out and there is a fresh mapping with every new session. 378 */ 379 380 /* 381 * head is less than 512 byte from the end of the ring buffer. 382 */ 383 if (head > watermark) 384 watermark = head; 385 386 /* 387 * Speed things up by using 64 bit transactions (see "u64 *buf" above) 388 */ 389 watermark /= sizeof(u64); 390 buf_size /= sizeof(u64); 391 392 /* 393 * If we find trace data at the end of the ring buffer, head has been there and has 394 * numerically wrapped around at least once. 395 */ 396 for (i = watermark; i < buf_size; i++) 397 if (buf[i]) 398 return true; 399 400 return false; 401 } 402 403 static int arm_spe_find_snapshot(struct auxtrace_record *itr, int idx, 404 struct auxtrace_mmap *mm, unsigned char *data, 405 u64 *head, u64 *old) 406 { 407 int err; 408 bool wrapped; 409 struct arm_spe_recording *ptr = 410 container_of(itr, struct arm_spe_recording, itr); 411 412 /* 413 * Allocate memory to keep track of wrapping if this is the first 414 * time we deal with this *mm. 415 */ 416 if (idx >= ptr->wrapped_cnt) { 417 err = arm_spe_alloc_wrapped_array(ptr, idx); 418 if (err) 419 return err; 420 } 421 422 /* 423 * Check to see if *head has wrapped around. If it hasn't only the 424 * amount of data between *head and *old is snapshot'ed to avoid 425 * bloating the perf.data file with zeros. But as soon as *head has 426 * wrapped around the entire size of the AUX ring buffer it taken. 427 */ 428 wrapped = ptr->wrapped[idx]; 429 if (!wrapped && arm_spe_buffer_has_wrapped(data, mm->len, *head)) { 430 wrapped = true; 431 ptr->wrapped[idx] = true; 432 } 433 434 pr_debug3("%s: mmap index %d old head %zu new head %zu size %zu\n", 435 __func__, idx, (size_t)*old, (size_t)*head, mm->len); 436 437 /* 438 * No wrap has occurred, we can just use *head and *old. 439 */ 440 if (!wrapped) 441 return 0; 442 443 /* 444 * *head has wrapped around - adjust *head and *old to pickup the 445 * entire content of the AUX buffer. 446 */ 447 if (*head >= mm->len) { 448 *old = *head - mm->len; 449 } else { 450 *head += mm->len; 451 *old = *head - mm->len; 452 } 453 454 return 0; 455 } 456 457 static u64 arm_spe_reference(struct auxtrace_record *itr __maybe_unused) 458 { 459 struct timespec ts; 460 461 clock_gettime(CLOCK_MONOTONIC_RAW, &ts); 462 463 return ts.tv_sec ^ ts.tv_nsec; 464 } 465 466 static void arm_spe_recording_free(struct auxtrace_record *itr) 467 { 468 struct arm_spe_recording *sper = 469 container_of(itr, struct arm_spe_recording, itr); 470 471 free(sper->wrapped); 472 free(sper); 473 } 474 475 struct auxtrace_record *arm_spe_recording_init(int *err, 476 struct perf_pmu *arm_spe_pmu) 477 { 478 struct arm_spe_recording *sper; 479 480 if (!arm_spe_pmu) { 481 *err = -ENODEV; 482 return NULL; 483 } 484 485 sper = zalloc(sizeof(struct arm_spe_recording)); 486 if (!sper) { 487 *err = -ENOMEM; 488 return NULL; 489 } 490 491 sper->arm_spe_pmu = arm_spe_pmu; 492 sper->itr.pmu = arm_spe_pmu; 493 sper->itr.snapshot_start = arm_spe_snapshot_start; 494 sper->itr.snapshot_finish = arm_spe_snapshot_finish; 495 sper->itr.find_snapshot = arm_spe_find_snapshot; 496 sper->itr.parse_snapshot_options = arm_spe_parse_snapshot_options; 497 sper->itr.recording_options = arm_spe_recording_options; 498 sper->itr.info_priv_size = arm_spe_info_priv_size; 499 sper->itr.info_fill = arm_spe_info_fill; 500 sper->itr.free = arm_spe_recording_free; 501 sper->itr.reference = arm_spe_reference; 502 sper->itr.read_finish = auxtrace_record__read_finish; 503 sper->itr.alignment = 0; 504 505 *err = 0; 506 return &sper->itr; 507 } 508 509 struct perf_event_attr 510 *arm_spe_pmu_default_config(struct perf_pmu *arm_spe_pmu) 511 { 512 struct perf_event_attr *attr; 513 514 attr = zalloc(sizeof(struct perf_event_attr)); 515 if (!attr) { 516 pr_err("arm_spe default config cannot allocate a perf_event_attr\n"); 517 return NULL; 518 } 519 520 /* 521 * If kernel driver doesn't advertise a minimum, 522 * use max allowable by PMSIDR_EL1.INTERVAL 523 */ 524 if (perf_pmu__scan_file(arm_spe_pmu, "caps/min_interval", "%llu", 525 &attr->sample_period) != 1) { 526 pr_debug("arm_spe driver doesn't advertise a min. interval. Using 4096\n"); 527 attr->sample_period = 4096; 528 } 529 530 arm_spe_pmu->selectable = true; 531 arm_spe_pmu->is_uncore = false; 532 533 return attr; 534 } 535