1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * BTS PMU driver for perf 4 * Copyright (c) 2013-2014, Intel Corporation. 5 */ 6 7 #undef DEBUG 8 9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 11 #include <linux/bitops.h> 12 #include <linux/types.h> 13 #include <linux/slab.h> 14 #include <linux/debugfs.h> 15 #include <linux/device.h> 16 #include <linux/coredump.h> 17 18 #include <linux/sizes.h> 19 #include <asm/perf_event.h> 20 #include <asm/msr.h> 21 22 #include "../perf_event.h" 23 24 struct bts_ctx { 25 struct perf_output_handle handle; 26 struct debug_store ds_back; 27 int state; 28 }; 29 30 /* BTS context states: */ 31 enum { 32 /* no ongoing AUX transactions */ 33 BTS_STATE_STOPPED = 0, 34 /* AUX transaction is on, BTS tracing is disabled */ 35 BTS_STATE_INACTIVE, 36 /* AUX transaction is on, BTS tracing is running */ 37 BTS_STATE_ACTIVE, 38 }; 39 40 static struct bts_ctx __percpu *bts_ctx; 41 42 #define BTS_RECORD_SIZE 24 43 #define BTS_SAFETY_MARGIN 4080 44 45 struct bts_phys { 46 struct page *page; 47 unsigned long size; 48 unsigned long offset; 49 unsigned long displacement; 50 }; 51 52 struct bts_buffer { 53 size_t real_size; /* multiple of BTS_RECORD_SIZE */ 54 unsigned int nr_pages; 55 unsigned int nr_bufs; 56 unsigned int cur_buf; 57 bool snapshot; 58 local_t data_size; 59 local_t head; 60 unsigned long end; 61 void **data_pages; 62 struct bts_phys buf[] __counted_by(nr_bufs); 63 }; 64 65 static struct pmu bts_pmu; 66 67 static int buf_nr_pages(struct page *page) 68 { 69 return 1 << page_private(page); 70 } 71 72 static size_t buf_size(struct page *page) 73 { 74 return buf_nr_pages(page) * PAGE_SIZE; 75 } 76 77 static void * 78 bts_buffer_setup_aux(struct perf_event *event, void **pages, 79 int nr_pages, bool overwrite) 80 { 81 struct bts_buffer *bb; 82 struct page *page; 83 int cpu = event->cpu; 84 int node = (cpu == -1) ? cpu : cpu_to_node(cpu); 85 unsigned long offset; 86 size_t size = nr_pages << PAGE_SHIFT; 87 int pg, nr_buf, pad; 88 89 /* count all the high order buffers */ 90 for (pg = 0, nr_buf = 0; pg < nr_pages;) { 91 page = virt_to_page(pages[pg]); 92 pg += buf_nr_pages(page); 93 nr_buf++; 94 } 95 96 /* 97 * to avoid interrupts in overwrite mode, only allow one physical 98 */ 99 if (overwrite && nr_buf > 1) 100 return NULL; 101 102 bb = kzalloc_node(struct_size(bb, buf, nr_buf), GFP_KERNEL, node); 103 if (!bb) 104 return NULL; 105 106 bb->nr_pages = nr_pages; 107 bb->nr_bufs = nr_buf; 108 bb->snapshot = overwrite; 109 bb->data_pages = pages; 110 bb->real_size = size - size % BTS_RECORD_SIZE; 111 112 for (pg = 0, nr_buf = 0, offset = 0, pad = 0; nr_buf < bb->nr_bufs; nr_buf++) { 113 unsigned int __nr_pages; 114 115 page = virt_to_page(pages[pg]); 116 __nr_pages = buf_nr_pages(page); 117 bb->buf[nr_buf].page = page; 118 bb->buf[nr_buf].offset = offset; 119 bb->buf[nr_buf].displacement = (pad ? BTS_RECORD_SIZE - pad : 0); 120 bb->buf[nr_buf].size = buf_size(page) - bb->buf[nr_buf].displacement; 121 pad = bb->buf[nr_buf].size % BTS_RECORD_SIZE; 122 bb->buf[nr_buf].size -= pad; 123 124 pg += __nr_pages; 125 offset += __nr_pages << PAGE_SHIFT; 126 } 127 128 return bb; 129 } 130 131 static void bts_buffer_free_aux(void *data) 132 { 133 kfree(data); 134 } 135 136 static unsigned long bts_buffer_offset(struct bts_buffer *bb, unsigned int idx) 137 { 138 return bb->buf[idx].offset + bb->buf[idx].displacement; 139 } 140 141 static void 142 bts_config_buffer(struct bts_buffer *bb) 143 { 144 int cpu = raw_smp_processor_id(); 145 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; 146 struct bts_phys *phys = &bb->buf[bb->cur_buf]; 147 unsigned long index, thresh = 0, end = phys->size; 148 struct page *page = phys->page; 149 150 index = local_read(&bb->head); 151 152 if (!bb->snapshot) { 153 if (bb->end < phys->offset + buf_size(page)) 154 end = bb->end - phys->offset - phys->displacement; 155 156 index -= phys->offset + phys->displacement; 157 158 if (end - index > BTS_SAFETY_MARGIN) 159 thresh = end - BTS_SAFETY_MARGIN; 160 else if (end - index > BTS_RECORD_SIZE) 161 thresh = end - BTS_RECORD_SIZE; 162 else 163 thresh = end; 164 } 165 166 ds->bts_buffer_base = (u64)(long)page_address(page) + phys->displacement; 167 ds->bts_index = ds->bts_buffer_base + index; 168 ds->bts_absolute_maximum = ds->bts_buffer_base + end; 169 ds->bts_interrupt_threshold = !bb->snapshot 170 ? ds->bts_buffer_base + thresh 171 : ds->bts_absolute_maximum + BTS_RECORD_SIZE; 172 } 173 174 static void bts_buffer_pad_out(struct bts_phys *phys, unsigned long head) 175 { 176 unsigned long index = head - phys->offset; 177 178 memset(page_address(phys->page) + index, 0, phys->size - index); 179 } 180 181 static void bts_update(struct bts_ctx *bts) 182 { 183 int cpu = raw_smp_processor_id(); 184 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; 185 struct bts_buffer *bb = perf_get_aux(&bts->handle); 186 unsigned long index = ds->bts_index - ds->bts_buffer_base, old, head; 187 188 if (!bb) 189 return; 190 191 head = index + bts_buffer_offset(bb, bb->cur_buf); 192 old = local_xchg(&bb->head, head); 193 194 if (!bb->snapshot) { 195 if (old == head) 196 return; 197 198 if (ds->bts_index >= ds->bts_absolute_maximum) 199 perf_aux_output_flag(&bts->handle, 200 PERF_AUX_FLAG_TRUNCATED); 201 202 /* 203 * old and head are always in the same physical buffer, so we 204 * can subtract them to get the data size. 205 */ 206 local_add(head - old, &bb->data_size); 207 } else { 208 local_set(&bb->data_size, head); 209 } 210 211 /* 212 * Since BTS is coherent, just add compiler barrier to ensure 213 * BTS updating is ordered against bts::handle::event. 214 */ 215 barrier(); 216 } 217 218 static int 219 bts_buffer_reset(struct bts_buffer *bb, struct perf_output_handle *handle); 220 221 /* 222 * Ordering PMU callbacks wrt themselves and the PMI is done by means 223 * of bts::state, which: 224 * - is set when bts::handle::event is valid, that is, between 225 * perf_aux_output_begin() and perf_aux_output_end(); 226 * - is zero otherwise; 227 * - is ordered against bts::handle::event with a compiler barrier. 228 */ 229 230 static void __bts_event_start(struct perf_event *event) 231 { 232 struct bts_ctx *bts = this_cpu_ptr(bts_ctx); 233 struct bts_buffer *bb = perf_get_aux(&bts->handle); 234 u64 config = 0; 235 236 if (!bb->snapshot) 237 config |= ARCH_PERFMON_EVENTSEL_INT; 238 if (!event->attr.exclude_kernel) 239 config |= ARCH_PERFMON_EVENTSEL_OS; 240 if (!event->attr.exclude_user) 241 config |= ARCH_PERFMON_EVENTSEL_USR; 242 243 bts_config_buffer(bb); 244 245 /* 246 * local barrier to make sure that ds configuration made it 247 * before we enable BTS and bts::state goes ACTIVE 248 */ 249 wmb(); 250 251 /* INACTIVE/STOPPED -> ACTIVE */ 252 WRITE_ONCE(bts->state, BTS_STATE_ACTIVE); 253 254 intel_pmu_enable_bts(config); 255 256 } 257 258 static void bts_event_start(struct perf_event *event, int flags) 259 { 260 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 261 struct bts_ctx *bts = this_cpu_ptr(bts_ctx); 262 struct bts_buffer *bb; 263 264 bb = perf_aux_output_begin(&bts->handle, event); 265 if (!bb) 266 goto fail_stop; 267 268 if (bts_buffer_reset(bb, &bts->handle)) 269 goto fail_end_stop; 270 271 bts->ds_back.bts_buffer_base = cpuc->ds->bts_buffer_base; 272 bts->ds_back.bts_absolute_maximum = cpuc->ds->bts_absolute_maximum; 273 bts->ds_back.bts_interrupt_threshold = cpuc->ds->bts_interrupt_threshold; 274 275 perf_event_itrace_started(event); 276 event->hw.state = 0; 277 278 __bts_event_start(event); 279 280 return; 281 282 fail_end_stop: 283 perf_aux_output_end(&bts->handle, 0); 284 285 fail_stop: 286 event->hw.state = PERF_HES_STOPPED; 287 } 288 289 static void __bts_event_stop(struct perf_event *event, int state) 290 { 291 struct bts_ctx *bts = this_cpu_ptr(bts_ctx); 292 293 /* ACTIVE -> INACTIVE(PMI)/STOPPED(->stop()) */ 294 WRITE_ONCE(bts->state, state); 295 296 /* 297 * No extra synchronization is mandated by the documentation to have 298 * BTS data stores globally visible. 299 */ 300 intel_pmu_disable_bts(); 301 } 302 303 static void bts_event_stop(struct perf_event *event, int flags) 304 { 305 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 306 struct bts_ctx *bts = this_cpu_ptr(bts_ctx); 307 struct bts_buffer *bb = NULL; 308 int state = READ_ONCE(bts->state); 309 310 if (state == BTS_STATE_ACTIVE) 311 __bts_event_stop(event, BTS_STATE_STOPPED); 312 313 if (state != BTS_STATE_STOPPED) 314 bb = perf_get_aux(&bts->handle); 315 316 event->hw.state |= PERF_HES_STOPPED; 317 318 if (flags & PERF_EF_UPDATE) { 319 bts_update(bts); 320 321 if (bb) { 322 if (bb->snapshot) 323 bts->handle.head = 324 local_xchg(&bb->data_size, 325 bb->nr_pages << PAGE_SHIFT); 326 perf_aux_output_end(&bts->handle, 327 local_xchg(&bb->data_size, 0)); 328 } 329 330 cpuc->ds->bts_index = bts->ds_back.bts_buffer_base; 331 cpuc->ds->bts_buffer_base = bts->ds_back.bts_buffer_base; 332 cpuc->ds->bts_absolute_maximum = bts->ds_back.bts_absolute_maximum; 333 cpuc->ds->bts_interrupt_threshold = bts->ds_back.bts_interrupt_threshold; 334 } 335 } 336 337 void intel_bts_enable_local(void) 338 { 339 struct bts_ctx *bts; 340 int state; 341 342 if (!bts_ctx) 343 return; 344 345 bts = this_cpu_ptr(bts_ctx); 346 state = READ_ONCE(bts->state); 347 /* 348 * Here we transition from INACTIVE to ACTIVE; 349 * if we instead are STOPPED from the interrupt handler, 350 * stay that way. Can't be ACTIVE here though. 351 */ 352 if (WARN_ON_ONCE(state == BTS_STATE_ACTIVE)) 353 return; 354 355 if (state == BTS_STATE_STOPPED) 356 return; 357 358 if (bts->handle.event) 359 __bts_event_start(bts->handle.event); 360 } 361 362 void intel_bts_disable_local(void) 363 { 364 struct bts_ctx *bts; 365 366 if (!bts_ctx) 367 return; 368 369 bts = this_cpu_ptr(bts_ctx); 370 371 /* 372 * Here we transition from ACTIVE to INACTIVE; 373 * do nothing for STOPPED or INACTIVE. 374 */ 375 if (READ_ONCE(bts->state) != BTS_STATE_ACTIVE) 376 return; 377 378 if (bts->handle.event) 379 __bts_event_stop(bts->handle.event, BTS_STATE_INACTIVE); 380 } 381 382 static int 383 bts_buffer_reset(struct bts_buffer *bb, struct perf_output_handle *handle) 384 { 385 unsigned long head, space, next_space, pad, gap, skip, wakeup; 386 unsigned int next_buf; 387 struct bts_phys *phys, *next_phys; 388 int ret; 389 390 if (bb->snapshot) 391 return 0; 392 393 head = handle->head & ((bb->nr_pages << PAGE_SHIFT) - 1); 394 395 phys = &bb->buf[bb->cur_buf]; 396 space = phys->offset + phys->displacement + phys->size - head; 397 pad = space; 398 if (space > handle->size) { 399 space = handle->size; 400 space -= space % BTS_RECORD_SIZE; 401 } 402 if (space <= BTS_SAFETY_MARGIN) { 403 /* See if next phys buffer has more space */ 404 next_buf = bb->cur_buf + 1; 405 if (next_buf >= bb->nr_bufs) 406 next_buf = 0; 407 next_phys = &bb->buf[next_buf]; 408 gap = buf_size(phys->page) - phys->displacement - phys->size + 409 next_phys->displacement; 410 skip = pad + gap; 411 if (handle->size >= skip) { 412 next_space = next_phys->size; 413 if (next_space + skip > handle->size) { 414 next_space = handle->size - skip; 415 next_space -= next_space % BTS_RECORD_SIZE; 416 } 417 if (next_space > space || !space) { 418 if (pad) 419 bts_buffer_pad_out(phys, head); 420 ret = perf_aux_output_skip(handle, skip); 421 if (ret) 422 return ret; 423 /* Advance to next phys buffer */ 424 phys = next_phys; 425 space = next_space; 426 head = phys->offset + phys->displacement; 427 /* 428 * After this, cur_buf and head won't match ds 429 * anymore, so we must not be racing with 430 * bts_update(). 431 */ 432 bb->cur_buf = next_buf; 433 local_set(&bb->head, head); 434 } 435 } 436 } 437 438 /* Don't go far beyond wakeup watermark */ 439 wakeup = BTS_SAFETY_MARGIN + BTS_RECORD_SIZE + handle->wakeup - 440 handle->head; 441 if (space > wakeup) { 442 space = wakeup; 443 space -= space % BTS_RECORD_SIZE; 444 } 445 446 bb->end = head + space; 447 448 /* 449 * If we have no space, the lost notification would have been sent when 450 * we hit absolute_maximum - see bts_update() 451 */ 452 if (!space) 453 return -ENOSPC; 454 455 return 0; 456 } 457 458 int intel_bts_interrupt(void) 459 { 460 struct debug_store *ds = this_cpu_ptr(&cpu_hw_events)->ds; 461 struct bts_ctx *bts; 462 struct perf_event *event; 463 struct bts_buffer *bb; 464 s64 old_head; 465 int err = -ENOSPC, handled = 0; 466 467 if (!bts_ctx) 468 return 0; 469 470 bts = this_cpu_ptr(bts_ctx); 471 event = bts->handle.event; 472 /* 473 * The only surefire way of knowing if this NMI is ours is by checking 474 * the write ptr against the PMI threshold. 475 */ 476 if (ds && (ds->bts_index >= ds->bts_interrupt_threshold)) 477 handled = 1; 478 479 /* 480 * this is wrapped in intel_bts_enable_local/intel_bts_disable_local, 481 * so we can only be INACTIVE or STOPPED 482 */ 483 if (READ_ONCE(bts->state) == BTS_STATE_STOPPED) 484 return handled; 485 486 bb = perf_get_aux(&bts->handle); 487 if (!bb) 488 return handled; 489 490 /* 491 * Skip snapshot counters: they don't use the interrupt, but 492 * there's no other way of telling, because the pointer will 493 * keep moving 494 */ 495 if (bb->snapshot) 496 return 0; 497 498 old_head = local_read(&bb->head); 499 bts_update(bts); 500 501 /* no new data */ 502 if (old_head == local_read(&bb->head)) 503 return handled; 504 505 perf_aux_output_end(&bts->handle, local_xchg(&bb->data_size, 0)); 506 507 bb = perf_aux_output_begin(&bts->handle, event); 508 if (bb) 509 err = bts_buffer_reset(bb, &bts->handle); 510 511 if (err) { 512 WRITE_ONCE(bts->state, BTS_STATE_STOPPED); 513 514 if (bb) { 515 /* 516 * BTS_STATE_STOPPED should be visible before 517 * cleared handle::event 518 */ 519 barrier(); 520 perf_aux_output_end(&bts->handle, 0); 521 } 522 } 523 524 return 1; 525 } 526 527 static void bts_event_del(struct perf_event *event, int mode) 528 { 529 bts_event_stop(event, PERF_EF_UPDATE); 530 } 531 532 static int bts_event_add(struct perf_event *event, int mode) 533 { 534 struct bts_ctx *bts = this_cpu_ptr(bts_ctx); 535 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 536 struct hw_perf_event *hwc = &event->hw; 537 538 event->hw.state = PERF_HES_STOPPED; 539 540 if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask)) 541 return -EBUSY; 542 543 if (bts->handle.event) 544 return -EBUSY; 545 546 if (mode & PERF_EF_START) { 547 bts_event_start(event, 0); 548 if (hwc->state & PERF_HES_STOPPED) 549 return -EINVAL; 550 } 551 552 return 0; 553 } 554 555 static void bts_event_destroy(struct perf_event *event) 556 { 557 x86_release_hardware(); 558 x86_del_exclusive(x86_lbr_exclusive_bts); 559 } 560 561 static int bts_event_init(struct perf_event *event) 562 { 563 int ret; 564 565 if (event->attr.type != bts_pmu.type) 566 return -ENOENT; 567 568 /* 569 * BTS leaks kernel addresses even when CPL0 tracing is 570 * disabled, so disallow intel_bts driver for unprivileged 571 * users on paranoid systems since it provides trace data 572 * to the user in a zero-copy fashion. 573 */ 574 if (event->attr.exclude_kernel) { 575 ret = perf_allow_kernel(); 576 if (ret) 577 return ret; 578 } 579 580 if (x86_add_exclusive(x86_lbr_exclusive_bts)) 581 return -EBUSY; 582 583 ret = x86_reserve_hardware(); 584 if (ret) { 585 x86_del_exclusive(x86_lbr_exclusive_bts); 586 return ret; 587 } 588 589 event->destroy = bts_event_destroy; 590 591 return 0; 592 } 593 594 static void bts_event_read(struct perf_event *event) 595 { 596 } 597 598 static __init int bts_init(void) 599 { 600 if (!boot_cpu_has(X86_FEATURE_DTES64)) 601 return -ENODEV; 602 603 x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS); 604 if (!x86_pmu.bts) 605 return -ENODEV; 606 607 if (boot_cpu_has(X86_FEATURE_PTI)) { 608 /* 609 * BTS hardware writes through a virtual memory map we must 610 * either use the kernel physical map, or the user mapping of 611 * the AUX buffer. 612 * 613 * However, since this driver supports per-CPU and per-task inherit 614 * we cannot use the user mapping since it will not be available 615 * if we're not running the owning process. 616 * 617 * With PTI we can't use the kernel map either, because its not 618 * there when we run userspace. 619 * 620 * For now, disable this driver when using PTI. 621 */ 622 return -ENODEV; 623 } 624 625 bts_ctx = alloc_percpu(struct bts_ctx); 626 if (!bts_ctx) 627 return -ENOMEM; 628 629 bts_pmu.capabilities = PERF_PMU_CAP_AUX_NO_SG | PERF_PMU_CAP_ITRACE | 630 PERF_PMU_CAP_EXCLUSIVE; 631 bts_pmu.task_ctx_nr = perf_sw_context; 632 bts_pmu.event_init = bts_event_init; 633 bts_pmu.add = bts_event_add; 634 bts_pmu.del = bts_event_del; 635 bts_pmu.start = bts_event_start; 636 bts_pmu.stop = bts_event_stop; 637 bts_pmu.read = bts_event_read; 638 bts_pmu.setup_aux = bts_buffer_setup_aux; 639 bts_pmu.free_aux = bts_buffer_free_aux; 640 641 return perf_pmu_register(&bts_pmu, "intel_bts", -1); 642 } 643 early_initcall(bts_init); 644