1 /* 2 * Performance events: 3 * 4 * Copyright (C) 2008-2009, Linutronix GmbH, Thomas Gleixner <tglx@kernel.org> 5 * Copyright (C) 2008-2011, Red Hat, Inc., Ingo Molnar 6 * Copyright (C) 2008-2011, Red Hat, Inc., Peter Zijlstra 7 * 8 * Data type definitions, declarations, prototypes. 9 * 10 * Started by: Thomas Gleixner and Ingo Molnar 11 * 12 * For licencing details see kernel-base/COPYING 13 */ 14 #ifndef _LINUX_PERF_EVENT_H 15 #define _LINUX_PERF_EVENT_H 16 17 #include <uapi/linux/perf_event.h> 18 #include <uapi/linux/bpf_perf_event.h> 19 20 /* 21 * Kernel-internal data types and definitions: 22 */ 23 24 #ifdef CONFIG_PERF_EVENTS 25 # include <asm/perf_event.h> 26 # include <asm/local64.h> 27 #endif 28 29 #ifdef CONFIG_HAVE_HW_BREAKPOINT 30 # include <linux/rhashtable-types.h> 31 # include <asm/hw_breakpoint.h> 32 #endif 33 34 #include <linux/list.h> 35 #include <linux/mutex.h> 36 #include <linux/rculist.h> 37 #include <linux/rcupdate.h> 38 #include <linux/spinlock.h> 39 #include <linux/hrtimer.h> 40 #include <linux/fs.h> 41 #include <linux/pid_namespace.h> 42 #include <linux/workqueue.h> 43 #include <linux/ftrace.h> 44 #include <linux/cpu.h> 45 #include <linux/irq_work.h> 46 #include <linux/static_key.h> 47 #include <linux/jump_label_ratelimit.h> 48 #include <linux/atomic.h> 49 #include <linux/sysfs.h> 50 #include <linux/perf_regs.h> 51 #include <linux/cgroup.h> 52 #include <linux/refcount.h> 53 #include <linux/security.h> 54 #include <linux/static_call.h> 55 #include <linux/lockdep.h> 56 57 #include <asm/local.h> 58 59 struct perf_callchain_entry { 60 u64 nr; 61 u64 ip[]; /* /proc/sys/kernel/perf_event_max_stack */ 62 }; 63 64 struct perf_callchain_entry_ctx { 65 struct perf_callchain_entry *entry; 66 u32 max_stack; 67 u32 nr; 68 short contexts; 69 bool contexts_maxed; 70 }; 71 72 typedef unsigned long (*perf_copy_f)(void *dst, const void *src, 73 unsigned long off, unsigned long len); 74 75 struct perf_raw_frag { 76 union { 77 struct perf_raw_frag *next; 78 unsigned long pad; 79 }; 80 perf_copy_f copy; 81 void *data; 82 u32 size; 83 } __packed; 84 85 struct perf_raw_record { 86 struct perf_raw_frag frag; 87 u32 size; 88 }; 89 90 static __always_inline bool perf_raw_frag_last(const struct perf_raw_frag *frag) 91 { 92 return frag->pad < sizeof(u64); 93 } 94 95 /* 96 * branch stack layout: 97 * nr: number of taken branches stored in entries[] 98 * hw_idx: The low level index of raw branch records 99 * for the most recent branch. 100 * -1ULL means invalid/unknown. 101 * 102 * Note that nr can vary from sample to sample 103 * branches (to, from) are stored from most recent 104 * to least recent, i.e., entries[0] contains the most 105 * recent branch. 106 * The entries[] is an abstraction of raw branch records, 107 * which may not be stored in age order in HW, e.g. Intel LBR. 108 * The hw_idx is to expose the low level index of raw 109 * branch record for the most recent branch aka entries[0]. 110 * The hw_idx index is between -1 (unknown) and max depth, 111 * which can be retrieved in /sys/devices/cpu/caps/branches. 112 * For the architectures whose raw branch records are 113 * already stored in age order, the hw_idx should be 0. 114 */ 115 struct perf_branch_stack { 116 u64 nr; 117 u64 hw_idx; 118 struct perf_branch_entry entries[]; 119 }; 120 121 struct task_struct; 122 123 /* 124 * extra PMU register associated with an event 125 */ 126 struct hw_perf_event_extra { 127 u64 config; /* register value */ 128 unsigned int reg; /* register address or index */ 129 int alloc; /* extra register already allocated */ 130 int idx; /* index in shared_regs->regs[] */ 131 }; 132 133 /** 134 * hw_perf_event::flag values 135 * 136 * PERF_EVENT_FLAG_ARCH bits are reserved for architecture-specific 137 * usage. 138 */ 139 #define PERF_EVENT_FLAG_ARCH 0x0fffffff 140 #define PERF_EVENT_FLAG_USER_READ_CNT 0x80000000 141 142 static_assert((PERF_EVENT_FLAG_USER_READ_CNT & PERF_EVENT_FLAG_ARCH) == 0); 143 144 /** 145 * struct hw_perf_event - performance event hardware details: 146 */ 147 struct hw_perf_event { 148 #ifdef CONFIG_PERF_EVENTS 149 union { 150 struct { /* hardware */ 151 u64 config; 152 u64 config1; 153 u64 last_tag; 154 u64 dyn_constraint; 155 unsigned long config_base; 156 unsigned long event_base; 157 int event_base_rdpmc; 158 int idx; 159 int last_cpu; 160 int flags; 161 162 struct hw_perf_event_extra extra_reg; 163 struct hw_perf_event_extra branch_reg; 164 }; 165 struct { /* aux / Intel-PT */ 166 u64 aux_config; 167 /* 168 * For AUX area events, aux_paused cannot be a state 169 * flag because it can be updated asynchronously to 170 * state. 171 */ 172 unsigned int aux_paused; 173 }; 174 struct { /* software */ 175 struct hrtimer hrtimer; 176 }; 177 struct { /* tracepoint */ 178 /* for tp_event->class */ 179 struct list_head tp_list; 180 }; 181 struct { /* amd_power */ 182 u64 pwr_acc; 183 u64 ptsc; 184 }; 185 #ifdef CONFIG_HAVE_HW_BREAKPOINT 186 struct { /* breakpoint */ 187 /* 188 * Crufty hack to avoid the chicken and egg 189 * problem hw_breakpoint has with context 190 * creation and event initalization. 191 */ 192 struct arch_hw_breakpoint info; 193 struct rhlist_head bp_list; 194 }; 195 #endif 196 struct { /* amd_iommu */ 197 u8 iommu_bank; 198 u8 iommu_cntr; 199 u16 padding; 200 u64 conf; 201 u64 conf1; 202 }; 203 }; 204 /* 205 * If the event is a per task event, this will point to the task in 206 * question. See the comment in perf_event_alloc(). 207 */ 208 struct task_struct *target; 209 210 /* 211 * PMU would store hardware filter configuration 212 * here. 213 */ 214 void *addr_filters; 215 216 /* Last sync'ed generation of filters */ 217 unsigned long addr_filters_gen; 218 219 /* 220 * hw_perf_event::state flags; used to track the PERF_EF_* state. 221 */ 222 223 /* the counter is stopped */ 224 #define PERF_HES_STOPPED 0x01 225 226 /* event->count up-to-date */ 227 #define PERF_HES_UPTODATE 0x02 228 229 #define PERF_HES_ARCH 0x04 230 231 int state; 232 233 /* 234 * The last observed hardware counter value, updated with a 235 * local64_cmpxchg() such that pmu::read() can be called nested. 236 */ 237 local64_t prev_count; 238 239 /* 240 * The period to start the next sample with. 241 */ 242 u64 sample_period; 243 244 union { 245 struct { /* Sampling */ 246 /* 247 * The period we started this sample with. 248 */ 249 u64 last_period; 250 251 /* 252 * However much is left of the current period; 253 * note that this is a full 64bit value and 254 * allows for generation of periods longer 255 * than hardware might allow. 256 */ 257 local64_t period_left; 258 }; 259 struct { /* Topdown events counting for context switch */ 260 u64 saved_metric; 261 u64 saved_slots; 262 }; 263 }; 264 265 /* 266 * State for throttling the event, see __perf_event_overflow() and 267 * perf_adjust_freq_unthr_context(). 268 */ 269 u64 interrupts_seq; 270 u64 interrupts; 271 272 /* 273 * State for freq target events, see __perf_event_overflow() and 274 * perf_adjust_freq_unthr_context(). 275 */ 276 u64 freq_time_stamp; 277 u64 freq_count_stamp; 278 #endif /* CONFIG_PERF_EVENTS */ 279 }; 280 281 struct perf_event; 282 struct perf_event_pmu_context; 283 284 /* 285 * Common implementation detail of pmu::{start,commit,cancel}_txn 286 */ 287 288 /* txn to add/schedule event on PMU */ 289 #define PERF_PMU_TXN_ADD 0x1 290 291 /* txn to read event group from PMU */ 292 #define PERF_PMU_TXN_READ 0x2 293 294 /** 295 * pmu::capabilities flags 296 */ 297 #define PERF_PMU_CAP_NO_INTERRUPT 0x0001 298 #define PERF_PMU_CAP_NO_NMI 0x0002 299 #define PERF_PMU_CAP_AUX_NO_SG 0x0004 300 #define PERF_PMU_CAP_EXTENDED_REGS 0x0008 301 #define PERF_PMU_CAP_EXCLUSIVE 0x0010 302 #define PERF_PMU_CAP_ITRACE 0x0020 303 #define PERF_PMU_CAP_NO_EXCLUDE 0x0040 304 #define PERF_PMU_CAP_AUX_OUTPUT 0x0080 305 #define PERF_PMU_CAP_EXTENDED_HW_TYPE 0x0100 306 #define PERF_PMU_CAP_AUX_PAUSE 0x0200 307 #define PERF_PMU_CAP_AUX_PREFER_LARGE 0x0400 308 #define PERF_PMU_CAP_MEDIATED_VPMU 0x0800 309 310 /** 311 * pmu::scope 312 */ 313 enum perf_pmu_scope { 314 PERF_PMU_SCOPE_NONE = 0, 315 PERF_PMU_SCOPE_CORE, 316 PERF_PMU_SCOPE_DIE, 317 PERF_PMU_SCOPE_CLUSTER, 318 PERF_PMU_SCOPE_PKG, 319 PERF_PMU_SCOPE_SYS_WIDE, 320 PERF_PMU_MAX_SCOPE, 321 }; 322 323 struct perf_output_handle; 324 325 #define PMU_NULL_DEV ((void *)(~0UL)) 326 327 /** 328 * struct pmu - generic performance monitoring unit 329 */ 330 struct pmu { 331 struct list_head entry; 332 333 spinlock_t events_lock; 334 struct list_head events; 335 336 struct module *module; 337 struct device *dev; 338 struct device *parent; 339 const struct attribute_group **attr_groups; 340 const struct attribute_group **attr_update; 341 const char *name; 342 int type; 343 344 /* 345 * various common per-pmu feature flags 346 */ 347 int capabilities; 348 349 /* 350 * PMU scope 351 */ 352 unsigned int scope; 353 354 struct perf_cpu_pmu_context * __percpu *cpu_pmu_context; 355 atomic_t exclusive_cnt; /* < 0: cpu; > 0: tsk */ 356 int task_ctx_nr; 357 int hrtimer_interval_ms; 358 359 /* number of address filters this PMU can do */ 360 unsigned int nr_addr_filters; 361 362 /* 363 * Fully disable/enable this PMU, can be used to protect from the PMI 364 * as well as for lazy/batch writing of the MSRs. 365 */ 366 void (*pmu_enable) (struct pmu *pmu); /* optional */ 367 void (*pmu_disable) (struct pmu *pmu); /* optional */ 368 369 /* 370 * Try and initialize the event for this PMU. 371 * 372 * Returns: 373 * -ENOENT -- @event is not for this PMU 374 * 375 * -ENODEV -- @event is for this PMU but PMU not present 376 * -EBUSY -- @event is for this PMU but PMU temporarily unavailable 377 * -EINVAL -- @event is for this PMU but @event is not valid 378 * -EOPNOTSUPP -- @event is for this PMU, @event is valid, but not supported 379 * -EACCES -- @event is for this PMU, @event is valid, but no privileges 380 * 381 * 0 -- @event is for this PMU and valid 382 * 383 * Other error return values are allowed. 384 */ 385 int (*event_init) (struct perf_event *event); 386 387 /* 388 * Notification that the event was mapped or unmapped. Called 389 * in the context of the mapping task. 390 */ 391 void (*event_mapped) (struct perf_event *event, struct mm_struct *mm); /* optional */ 392 void (*event_unmapped) (struct perf_event *event, struct mm_struct *mm); /* optional */ 393 394 /* 395 * Flags for ->add()/->del()/ ->start()/->stop(). There are 396 * matching hw_perf_event::state flags. 397 */ 398 399 /* start the counter when adding */ 400 #define PERF_EF_START 0x01 401 402 /* reload the counter when starting */ 403 #define PERF_EF_RELOAD 0x02 404 405 /* update the counter when stopping */ 406 #define PERF_EF_UPDATE 0x04 407 408 /* AUX area event, pause tracing */ 409 #define PERF_EF_PAUSE 0x08 410 411 /* AUX area event, resume tracing */ 412 #define PERF_EF_RESUME 0x10 413 414 /* 415 * Adds/Removes a counter to/from the PMU, can be done inside a 416 * transaction, see the ->*_txn() methods. 417 * 418 * The add/del callbacks will reserve all hardware resources required 419 * to service the event, this includes any counter constraint 420 * scheduling etc. 421 * 422 * Called with IRQs disabled and the PMU disabled on the CPU the event 423 * is on. 424 * 425 * ->add() called without PERF_EF_START should result in the same state 426 * as ->add() followed by ->stop(). 427 * 428 * ->del() must always PERF_EF_UPDATE stop an event. If it calls 429 * ->stop() that must deal with already being stopped without 430 * PERF_EF_UPDATE. 431 */ 432 int (*add) (struct perf_event *event, int flags); 433 void (*del) (struct perf_event *event, int flags); 434 435 /* 436 * Starts/Stops a counter present on the PMU. 437 * 438 * The PMI handler should stop the counter when perf_event_overflow() 439 * returns !0. ->start() will be used to continue. 440 * 441 * Also used to change the sample period. 442 * 443 * Called with IRQs disabled and the PMU disabled on the CPU the event 444 * is on -- will be called from NMI context with the PMU generates 445 * NMIs. 446 * 447 * ->stop() with PERF_EF_UPDATE will read the counter and update 448 * period/count values like ->read() would. 449 * 450 * ->start() with PERF_EF_RELOAD will reprogram the counter 451 * value, must be preceded by a ->stop() with PERF_EF_UPDATE. 452 * 453 * ->stop() with PERF_EF_PAUSE will stop as simply as possible. Will not 454 * overlap another ->stop() with PERF_EF_PAUSE nor ->start() with 455 * PERF_EF_RESUME. 456 * 457 * ->start() with PERF_EF_RESUME will start as simply as possible but 458 * only if the counter is not otherwise stopped. Will not overlap 459 * another ->start() with PERF_EF_RESUME nor ->stop() with 460 * PERF_EF_PAUSE. 461 * 462 * Notably, PERF_EF_PAUSE/PERF_EF_RESUME *can* be concurrent with other 463 * ->stop()/->start() invocations, just not itself. 464 */ 465 void (*start) (struct perf_event *event, int flags); 466 void (*stop) (struct perf_event *event, int flags); 467 468 /* 469 * Updates the counter value of the event. 470 * 471 * For sampling capable PMUs this will also update the software period 472 * hw_perf_event::period_left field. 473 */ 474 void (*read) (struct perf_event *event); 475 476 /* 477 * Group events scheduling is treated as a transaction, add 478 * group events as a whole and perform one schedulability test. 479 * If the test fails, roll back the whole group 480 * 481 * Start the transaction, after this ->add() doesn't need to 482 * do schedulability tests. 483 * 484 * Optional. 485 */ 486 void (*start_txn) (struct pmu *pmu, unsigned int txn_flags); 487 /* 488 * If ->start_txn() disabled the ->add() schedulability test 489 * then ->commit_txn() is required to perform one. On success 490 * the transaction is closed. On error the transaction is kept 491 * open until ->cancel_txn() is called. 492 * 493 * Optional. 494 */ 495 int (*commit_txn) (struct pmu *pmu); 496 /* 497 * Will cancel the transaction, assumes ->del() is called 498 * for each successful ->add() during the transaction. 499 * 500 * Optional. 501 */ 502 void (*cancel_txn) (struct pmu *pmu); 503 504 /* 505 * Will return the value for perf_event_mmap_page::index for this event, 506 * if no implementation is provided it will default to 0 (see 507 * perf_event_idx_default). 508 */ 509 int (*event_idx) (struct perf_event *event); /*optional */ 510 511 /* 512 * context-switches callback 513 */ 514 void (*sched_task) (struct perf_event_pmu_context *pmu_ctx, 515 struct task_struct *task, bool sched_in); 516 517 /* 518 * Kmem cache of PMU specific data 519 */ 520 struct kmem_cache *task_ctx_cache; 521 522 /* 523 * Set up pmu-private data structures for an AUX area 524 */ 525 void *(*setup_aux) (struct perf_event *event, void **pages, 526 int nr_pages, bool overwrite); 527 /* optional */ 528 529 /* 530 * Free pmu-private AUX data structures 531 */ 532 void (*free_aux) (void *aux); /* optional */ 533 534 /* 535 * Take a snapshot of the AUX buffer without touching the event 536 * state, so that preempting ->start()/->stop() callbacks does 537 * not interfere with their logic. Called in PMI context. 538 * 539 * Returns the size of AUX data copied to the output handle. 540 * 541 * Optional. 542 */ 543 long (*snapshot_aux) (struct perf_event *event, 544 struct perf_output_handle *handle, 545 unsigned long size); 546 547 /* 548 * Validate address range filters: make sure the HW supports the 549 * requested configuration and number of filters; return 0 if the 550 * supplied filters are valid, -errno otherwise. 551 * 552 * Runs in the context of the ioctl()ing process and is not serialized 553 * with the rest of the PMU callbacks. 554 */ 555 int (*addr_filters_validate) (struct list_head *filters); 556 /* optional */ 557 558 /* 559 * Synchronize address range filter configuration: 560 * translate hw-agnostic filters into hardware configuration in 561 * event::hw::addr_filters. 562 * 563 * Runs as a part of filter sync sequence that is done in ->start() 564 * callback by calling perf_event_addr_filters_sync(). 565 * 566 * May (and should) traverse event::addr_filters::list, for which its 567 * caller provides necessary serialization. 568 */ 569 void (*addr_filters_sync) (struct perf_event *event); 570 /* optional */ 571 572 /* 573 * Check if event can be used for aux_output purposes for 574 * events of this PMU. 575 * 576 * Runs from perf_event_open(). Should return 0 for "no match" 577 * or non-zero for "match". 578 */ 579 int (*aux_output_match) (struct perf_event *event); 580 /* optional */ 581 582 /* 583 * Skip programming this PMU on the given CPU. Typically needed for 584 * big.LITTLE things. 585 */ 586 bool (*filter) (struct pmu *pmu, int cpu); /* optional */ 587 588 /* 589 * Check period value for PERF_EVENT_IOC_PERIOD ioctl. 590 */ 591 int (*check_period) (struct perf_event *event, u64 value); /* optional */ 592 }; 593 594 enum perf_addr_filter_action_t { 595 PERF_ADDR_FILTER_ACTION_STOP = 0, 596 PERF_ADDR_FILTER_ACTION_START, 597 PERF_ADDR_FILTER_ACTION_FILTER, 598 }; 599 600 /** 601 * struct perf_addr_filter - address range filter definition 602 * @entry: event's filter list linkage 603 * @path: object file's path for file-based filters 604 * @offset: filter range offset 605 * @size: filter range size (size==0 means single address trigger) 606 * @action: filter/start/stop 607 * 608 * This is a hardware-agnostic filter configuration as specified by the user. 609 */ 610 struct perf_addr_filter { 611 struct list_head entry; 612 struct path path; 613 unsigned long offset; 614 unsigned long size; 615 enum perf_addr_filter_action_t action; 616 }; 617 618 /** 619 * struct perf_addr_filters_head - container for address range filters 620 * @list: list of filters for this event 621 * @lock: spinlock that serializes accesses to the @list and event's 622 * (and its children's) filter generations. 623 * @nr_file_filters: number of file-based filters 624 * 625 * A child event will use parent's @list (and therefore @lock), so they are 626 * bundled together; see perf_event_addr_filters(). 627 */ 628 struct perf_addr_filters_head { 629 struct list_head list; 630 raw_spinlock_t lock; 631 unsigned int nr_file_filters; 632 }; 633 634 struct perf_addr_filter_range { 635 unsigned long start; 636 unsigned long size; 637 }; 638 639 /* 640 * The normal states are: 641 * 642 * ACTIVE --. 643 * ^ | 644 * | | 645 * sched_{in,out}() | 646 * | | 647 * v | 648 * ,---> INACTIVE --+ <-. 649 * | | | 650 * | {dis,en}able() 651 * sched_in() | | 652 * | OFF <--' --+ 653 * | | 654 * `---> ERROR ------' 655 * 656 * That is: 657 * 658 * sched_in: INACTIVE -> {ACTIVE,ERROR} 659 * sched_out: ACTIVE -> INACTIVE 660 * disable: {ACTIVE,INACTIVE} -> OFF 661 * enable: {OFF,ERROR} -> INACTIVE 662 * 663 * Where {OFF,ERROR} are disabled states. 664 * 665 * Then we have the {EXIT,REVOKED,DEAD} states which are various shades of 666 * defunct events: 667 * 668 * - EXIT means task that the even was assigned to died, but child events 669 * still live, and further children can still be created. But the event 670 * itself will never be active again. It can only transition to 671 * {REVOKED,DEAD}; 672 * 673 * - REVOKED means the PMU the event was associated with is gone; all 674 * functionality is stopped but the event is still alive. Can only 675 * transition to DEAD; 676 * 677 * - DEAD event really is DYING tearing down state and freeing bits. 678 * 679 */ 680 enum perf_event_state { 681 PERF_EVENT_STATE_DEAD = -5, 682 PERF_EVENT_STATE_REVOKED = -4, /* pmu gone, must not touch */ 683 PERF_EVENT_STATE_EXIT = -3, /* task died, still inherit */ 684 PERF_EVENT_STATE_ERROR = -2, /* scheduling error, can enable */ 685 PERF_EVENT_STATE_OFF = -1, 686 PERF_EVENT_STATE_INACTIVE = 0, 687 PERF_EVENT_STATE_ACTIVE = 1, 688 }; 689 690 struct file; 691 struct perf_sample_data; 692 693 typedef void (*perf_overflow_handler_t)(struct perf_event *, 694 struct perf_sample_data *, 695 struct pt_regs *regs); 696 697 /* 698 * Event capabilities. For event_caps and groups caps. 699 * 700 * PERF_EV_CAP_SOFTWARE: Is a software event. 701 * PERF_EV_CAP_READ_ACTIVE_PKG: A CPU event (or cgroup event) that can be read 702 * from any CPU in the package where it is active. 703 * PERF_EV_CAP_SIBLING: An event with this flag must be a group sibling and 704 * cannot be a group leader. If an event with this flag is detached from the 705 * group it is scheduled out and moved into an unrecoverable ERROR state. 706 * PERF_EV_CAP_READ_SCOPE: A CPU event that can be read from any CPU of the 707 * PMU scope where it is active. 708 */ 709 #define PERF_EV_CAP_SOFTWARE BIT(0) 710 #define PERF_EV_CAP_READ_ACTIVE_PKG BIT(1) 711 #define PERF_EV_CAP_SIBLING BIT(2) 712 #define PERF_EV_CAP_READ_SCOPE BIT(3) 713 714 #define SWEVENT_HLIST_BITS 8 715 #define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS) 716 717 struct swevent_hlist { 718 struct hlist_head heads[SWEVENT_HLIST_SIZE]; 719 struct rcu_head rcu_head; 720 }; 721 722 #define PERF_ATTACH_CONTEXT 0x0001 723 #define PERF_ATTACH_GROUP 0x0002 724 #define PERF_ATTACH_TASK 0x0004 725 #define PERF_ATTACH_TASK_DATA 0x0008 726 #define PERF_ATTACH_GLOBAL_DATA 0x0010 727 #define PERF_ATTACH_SCHED_CB 0x0020 728 #define PERF_ATTACH_CHILD 0x0040 729 #define PERF_ATTACH_EXCLUSIVE 0x0080 730 #define PERF_ATTACH_CALLCHAIN 0x0100 731 #define PERF_ATTACH_ITRACE 0x0200 732 733 struct bpf_prog; 734 struct perf_cgroup; 735 struct perf_buffer; 736 737 struct pmu_event_list { 738 raw_spinlock_t lock; 739 struct list_head list; 740 }; 741 742 /* 743 * event->sibling_list is modified whole holding both ctx->lock and ctx->mutex 744 * as such iteration must hold either lock. However, since ctx->lock is an IRQ 745 * safe lock, and is only held by the CPU doing the modification, having IRQs 746 * disabled is sufficient since it will hold-off the IPIs. 747 */ 748 #ifdef CONFIG_PROVE_LOCKING 749 # define lockdep_assert_event_ctx(event) \ 750 WARN_ON_ONCE(__lockdep_enabled && \ 751 (this_cpu_read(hardirqs_enabled) && \ 752 lockdep_is_held(&(event)->ctx->mutex) != LOCK_STATE_HELD)) 753 #else 754 # define lockdep_assert_event_ctx(event) 755 #endif 756 757 #define for_each_sibling_event(sibling, event) \ 758 lockdep_assert_event_ctx(event); \ 759 if ((event)->group_leader == (event)) \ 760 list_for_each_entry((sibling), &(event)->sibling_list, sibling_list) 761 762 /** 763 * struct perf_event - performance event kernel representation: 764 */ 765 struct perf_event { 766 #ifdef CONFIG_PERF_EVENTS 767 /* 768 * entry onto perf_event_context::event_list; 769 * modifications require ctx->lock 770 * RCU safe iterations. 771 */ 772 struct list_head event_entry; 773 774 /* 775 * Locked for modification by both ctx->mutex and ctx->lock; holding 776 * either sufficies for read. 777 */ 778 struct list_head sibling_list; 779 struct list_head active_list; 780 /* 781 * Node on the pinned or flexible tree located at the event context; 782 */ 783 struct rb_node group_node; 784 u64 group_index; 785 /* 786 * We need storage to track the entries in perf_pmu_migrate_context; we 787 * cannot use the event_entry because of RCU and we want to keep the 788 * group in tact which avoids us using the other two entries. 789 */ 790 struct list_head migrate_entry; 791 792 struct hlist_node hlist_entry; 793 struct list_head active_entry; 794 int nr_siblings; 795 796 /* Not serialized. Only written during event initialization. */ 797 int event_caps; 798 /* The cumulative AND of all event_caps for events in this group. */ 799 int group_caps; 800 801 unsigned int group_generation; 802 struct perf_event *group_leader; 803 /* 804 * event->pmu will always point to pmu in which this event belongs. 805 * Whereas event->pmu_ctx->pmu may point to other pmu when group of 806 * different pmu events is created. 807 */ 808 struct pmu *pmu; 809 void *pmu_private; 810 811 enum perf_event_state state; 812 unsigned int attach_state; 813 local64_t count; 814 atomic64_t child_count; 815 816 /* 817 * These are the total time in nanoseconds that the event 818 * has been enabled (i.e. eligible to run, and the task has 819 * been scheduled in, if this is a per-task event) 820 * and running (scheduled onto the CPU), respectively. 821 */ 822 u64 total_time_enabled; 823 u64 total_time_running; 824 u64 tstamp; 825 826 struct perf_event_attr attr; 827 u16 header_size; 828 u16 id_header_size; 829 u16 read_size; 830 struct hw_perf_event hw; 831 832 struct perf_event_context *ctx; 833 /* 834 * event->pmu_ctx points to perf_event_pmu_context in which the event 835 * is added. This pmu_ctx can be of other pmu for sw event when that 836 * sw event is part of a group which also contains non-sw events. 837 */ 838 struct perf_event_pmu_context *pmu_ctx; 839 atomic_long_t refcount; 840 841 /* 842 * These accumulate total time (in nanoseconds) that children 843 * events have been enabled and running, respectively. 844 */ 845 atomic64_t child_total_time_enabled; 846 atomic64_t child_total_time_running; 847 848 /* 849 * Protect attach/detach and child_list: 850 */ 851 struct mutex child_mutex; 852 struct list_head child_list; 853 struct perf_event *parent; 854 855 int oncpu; 856 int cpu; 857 858 struct list_head owner_entry; 859 struct task_struct *owner; 860 861 /* mmap bits */ 862 struct mutex mmap_mutex; 863 refcount_t mmap_count; 864 865 struct perf_buffer *rb; 866 struct list_head rb_entry; 867 unsigned long rcu_batches; 868 int rcu_pending; 869 870 /* poll related */ 871 wait_queue_head_t waitq; 872 struct fasync_struct *fasync; 873 874 /* delayed work for NMIs and such */ 875 unsigned int pending_wakeup; 876 unsigned int pending_kill; 877 unsigned int pending_disable; 878 unsigned long pending_addr; /* SIGTRAP */ 879 struct irq_work pending_irq; 880 struct irq_work pending_disable_irq; 881 struct callback_head pending_task; 882 unsigned int pending_work; 883 884 atomic_t event_limit; 885 886 /* address range filters */ 887 struct perf_addr_filters_head addr_filters; 888 /* vma address array for file-based filders */ 889 struct perf_addr_filter_range *addr_filter_ranges; 890 unsigned long addr_filters_gen; 891 892 /* for aux_output events */ 893 struct perf_event *aux_event; 894 895 void (*destroy)(struct perf_event *); 896 struct rcu_head rcu_head; 897 898 struct pid_namespace *ns; 899 u64 id; 900 901 atomic64_t lost_samples; 902 903 u64 (*clock)(void); 904 perf_overflow_handler_t overflow_handler; 905 void *overflow_handler_context; 906 struct bpf_prog *prog; 907 u64 bpf_cookie; 908 909 #ifdef CONFIG_EVENT_TRACING 910 struct trace_event_call *tp_event; 911 struct event_filter *filter; 912 # ifdef CONFIG_FUNCTION_TRACER 913 struct ftrace_ops ftrace_ops; 914 # endif 915 #endif 916 917 #ifdef CONFIG_CGROUP_PERF 918 struct perf_cgroup *cgrp; /* cgroup event is attach to */ 919 #endif 920 921 #ifdef CONFIG_SECURITY 922 void *security; 923 #endif 924 struct list_head sb_list; 925 struct list_head pmu_list; 926 927 /* 928 * Certain events gets forwarded to another pmu internally by over- 929 * writing kernel copy of event->attr.type without user being aware 930 * of it. event->orig_type contains original 'type' requested by 931 * user. 932 */ 933 u32 orig_type; 934 #endif /* CONFIG_PERF_EVENTS */ 935 }; 936 937 /* 938 * ,-----------------------[1:n]------------------------. 939 * V V 940 * perf_event_context <-[1:n]-> perf_event_pmu_context <-[1:n]- perf_event 941 * | | 942 * `--[n:1]-> pmu <-[1:n]--' 943 * 944 * 945 * struct perf_event_pmu_context lifetime is refcount based and RCU freed 946 * (similar to perf_event_context). Locking is as if it were a member of 947 * perf_event_context; specifically: 948 * 949 * modification, both: ctx->mutex && ctx->lock 950 * reading, either: ctx->mutex || ctx->lock 951 * 952 * There is one exception to this; namely put_pmu_ctx() isn't always called 953 * with ctx->mutex held; this means that as long as we can guarantee the epc 954 * has events the above rules hold. 955 * 956 * Specificially, sys_perf_event_open()'s group_leader case depends on 957 * ctx->mutex pinning the configuration. Since we hold a reference on 958 * group_leader (through the filedesc) it can't go away, therefore it's 959 * associated pmu_ctx must exist and cannot change due to ctx->mutex. 960 * 961 * perf_event holds a refcount on perf_event_context 962 * perf_event holds a refcount on perf_event_pmu_context 963 */ 964 struct perf_event_pmu_context { 965 struct pmu *pmu; 966 struct perf_event_context *ctx; 967 968 struct list_head pmu_ctx_entry; 969 970 struct list_head pinned_active; 971 struct list_head flexible_active; 972 973 /* Used to identify the per-cpu perf_event_pmu_context */ 974 unsigned int embedded : 1; 975 976 unsigned int nr_events; 977 unsigned int nr_cgroups; 978 unsigned int nr_freq; 979 980 atomic_t refcount; /* event <-> epc */ 981 struct rcu_head rcu_head; 982 983 /* 984 * Set when one or more (plausibly active) event can't be scheduled 985 * due to pmu overcommit or pmu constraints, except tolerant to 986 * events not necessary to be active due to scheduling constraints, 987 * such as cgroups. 988 */ 989 int rotate_necessary; 990 }; 991 992 static inline bool perf_pmu_ctx_is_active(struct perf_event_pmu_context *epc) 993 { 994 return !list_empty(&epc->flexible_active) || !list_empty(&epc->pinned_active); 995 } 996 997 struct perf_event_groups { 998 struct rb_root tree; 999 u64 index; 1000 }; 1001 1002 struct perf_time_ctx { 1003 u64 time; 1004 u64 stamp; 1005 u64 offset; 1006 }; 1007 1008 /** 1009 * struct perf_event_context - event context structure 1010 * 1011 * Used as a container for task events and CPU events as well: 1012 */ 1013 struct perf_event_context { 1014 /* 1015 * Protect the states of the events in the list, 1016 * nr_active, and the list: 1017 */ 1018 raw_spinlock_t lock; 1019 /* 1020 * Protect the list of events. Locking either mutex or lock 1021 * is sufficient to ensure the list doesn't change; to change 1022 * the list you need to lock both the mutex and the spinlock. 1023 */ 1024 struct mutex mutex; 1025 1026 struct list_head pmu_ctx_list; 1027 struct perf_event_groups pinned_groups; 1028 struct perf_event_groups flexible_groups; 1029 struct list_head event_list; 1030 1031 int nr_events; 1032 int nr_user; 1033 int is_active; 1034 1035 int nr_stat; 1036 int nr_freq; 1037 int rotate_disable; 1038 1039 refcount_t refcount; /* event <-> ctx */ 1040 struct task_struct *task; 1041 1042 /* 1043 * Context clock, runs when context enabled. 1044 */ 1045 struct perf_time_ctx time; 1046 1047 /* 1048 * Context clock, runs when in the guest mode. 1049 */ 1050 struct perf_time_ctx timeguest; 1051 1052 /* 1053 * These fields let us detect when two contexts have both 1054 * been cloned (inherited) from a common ancestor. 1055 */ 1056 struct perf_event_context *parent_ctx; 1057 u64 parent_gen; 1058 u64 generation; 1059 int pin_count; 1060 #ifdef CONFIG_CGROUP_PERF 1061 int nr_cgroups; /* cgroup evts */ 1062 #endif 1063 struct rcu_head rcu_head; 1064 1065 /* 1066 * The count of events for which using the switch-out fast path 1067 * should be avoided. 1068 * 1069 * Sum (event->pending_work + events with 1070 * (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ))) 1071 * 1072 * The SIGTRAP is targeted at ctx->task, as such it won't do changing 1073 * that until the signal is delivered. 1074 */ 1075 local_t nr_no_switch_fast; 1076 }; 1077 1078 /** 1079 * struct perf_ctx_data - PMU specific data for a task 1080 * @rcu_head: To avoid the race on free PMU specific data 1081 * @refcount: To track users 1082 * @global: To track system-wide users 1083 * @ctx_cache: Kmem cache of PMU specific data 1084 * @data: PMU specific data 1085 * 1086 * Currently, the struct is only used in Intel LBR call stack mode to 1087 * save/restore the call stack of a task on context switches. 1088 * 1089 * The rcu_head is used to prevent the race on free the data. 1090 * The data only be allocated when Intel LBR call stack mode is enabled. 1091 * The data will be freed when the mode is disabled. 1092 * The content of the data will only be accessed in context switch, which 1093 * should be protected by rcu_read_lock(). 1094 * 1095 * Because of the alignment requirement of Intel Arch LBR, the Kmem cache 1096 * is used to allocate the PMU specific data. The ctx_cache is to track 1097 * the Kmem cache. 1098 * 1099 * Careful: Struct perf_ctx_data is added as a pointer in struct task_struct. 1100 * When system-wide Intel LBR call stack mode is enabled, a buffer with 1101 * constant size will be allocated for each task. 1102 * Also, system memory consumption can further grow when the size of 1103 * struct perf_ctx_data enlarges. 1104 */ 1105 struct perf_ctx_data { 1106 struct rcu_head rcu_head; 1107 refcount_t refcount; 1108 int global; 1109 struct kmem_cache *ctx_cache; 1110 void *data; 1111 }; 1112 1113 struct perf_cpu_pmu_context { 1114 struct perf_event_pmu_context epc; 1115 struct perf_event_pmu_context *task_epc; 1116 1117 struct list_head sched_cb_entry; 1118 int sched_cb_usage; 1119 1120 int active_oncpu; 1121 int exclusive; 1122 int pmu_disable_count; 1123 1124 raw_spinlock_t hrtimer_lock; 1125 struct hrtimer hrtimer; 1126 ktime_t hrtimer_interval; 1127 unsigned int hrtimer_active; 1128 }; 1129 1130 /** 1131 * struct perf_event_cpu_context - per cpu event context structure 1132 */ 1133 struct perf_cpu_context { 1134 struct perf_event_context ctx; 1135 struct perf_event_context *task_ctx; 1136 int online; 1137 1138 #ifdef CONFIG_CGROUP_PERF 1139 struct perf_cgroup *cgrp; 1140 #endif 1141 1142 /* 1143 * Per-CPU storage for iterators used in visit_groups_merge. The default 1144 * storage is of size 2 to hold the CPU and any CPU event iterators. 1145 */ 1146 int heap_size; 1147 struct perf_event **heap; 1148 struct perf_event *heap_default[2]; 1149 }; 1150 1151 struct perf_output_handle { 1152 struct perf_event *event; 1153 struct perf_buffer *rb; 1154 unsigned long wakeup; 1155 unsigned long size; 1156 union { 1157 u64 flags; /* perf_output*() */ 1158 u64 aux_flags; /* perf_aux_output*() */ 1159 struct { 1160 u64 skip_read : 1; 1161 }; 1162 }; 1163 union { 1164 void *addr; 1165 unsigned long head; 1166 }; 1167 int page; 1168 }; 1169 1170 struct bpf_perf_event_data_kern { 1171 bpf_user_pt_regs_t *regs; 1172 struct perf_sample_data *data; 1173 struct perf_event *event; 1174 }; 1175 1176 #ifdef CONFIG_CGROUP_PERF 1177 1178 /* 1179 * perf_cgroup_info keeps track of time_enabled for a cgroup. 1180 * This is a per-cpu dynamically allocated data structure. 1181 */ 1182 struct perf_cgroup_info { 1183 struct perf_time_ctx time; 1184 struct perf_time_ctx timeguest; 1185 int active; 1186 }; 1187 1188 struct perf_cgroup { 1189 struct cgroup_subsys_state css; 1190 struct perf_cgroup_info __percpu *info; 1191 }; 1192 1193 /* 1194 * Must ensure cgroup is pinned (css_get) before calling 1195 * this function. In other words, we cannot call this function 1196 * if there is no cgroup event for the current CPU context. 1197 */ 1198 static inline struct perf_cgroup * 1199 perf_cgroup_from_task(struct task_struct *task, struct perf_event_context *ctx) 1200 { 1201 return container_of(task_css_check(task, perf_event_cgrp_id, 1202 ctx ? lockdep_is_held(&ctx->lock) 1203 : true), 1204 struct perf_cgroup, css); 1205 } 1206 #endif /* CONFIG_CGROUP_PERF */ 1207 1208 #ifdef CONFIG_PERF_EVENTS 1209 1210 extern struct perf_event_context *perf_cpu_task_ctx(void); 1211 1212 extern void *perf_aux_output_begin(struct perf_output_handle *handle, 1213 struct perf_event *event); 1214 extern void perf_aux_output_end(struct perf_output_handle *handle, 1215 unsigned long size); 1216 extern int perf_aux_output_skip(struct perf_output_handle *handle, 1217 unsigned long size); 1218 extern void *perf_get_aux(struct perf_output_handle *handle); 1219 extern void perf_aux_output_flag(struct perf_output_handle *handle, u64 flags); 1220 extern void perf_event_itrace_started(struct perf_event *event); 1221 1222 extern int perf_pmu_register(struct pmu *pmu, const char *name, int type); 1223 extern int perf_pmu_unregister(struct pmu *pmu); 1224 1225 extern void __perf_event_task_sched_in(struct task_struct *prev, 1226 struct task_struct *task); 1227 extern void __perf_event_task_sched_out(struct task_struct *prev, 1228 struct task_struct *next); 1229 extern int perf_event_init_task(struct task_struct *child, u64 clone_flags); 1230 extern void perf_event_exit_task(struct task_struct *child); 1231 extern void perf_event_free_task(struct task_struct *task); 1232 extern void perf_event_delayed_put(struct task_struct *task); 1233 extern struct file *perf_event_get(unsigned int fd); 1234 extern const struct perf_event *perf_get_event(struct file *file); 1235 extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event); 1236 extern void perf_event_print_debug(void); 1237 extern void perf_pmu_disable(struct pmu *pmu); 1238 extern void perf_pmu_enable(struct pmu *pmu); 1239 extern void perf_sched_cb_dec(struct pmu *pmu); 1240 extern void perf_sched_cb_inc(struct pmu *pmu); 1241 extern int perf_event_task_disable(void); 1242 extern int perf_event_task_enable(void); 1243 1244 extern void perf_pmu_resched(struct pmu *pmu); 1245 1246 extern int perf_event_refresh(struct perf_event *event, int refresh); 1247 extern void perf_event_update_userpage(struct perf_event *event); 1248 extern int perf_event_release_kernel(struct perf_event *event); 1249 1250 extern struct perf_event * 1251 perf_event_create_kernel_counter(struct perf_event_attr *attr, 1252 int cpu, 1253 struct task_struct *task, 1254 perf_overflow_handler_t callback, 1255 void *context); 1256 1257 extern void perf_pmu_migrate_context(struct pmu *pmu, 1258 int src_cpu, int dst_cpu); 1259 extern int perf_event_read_local(struct perf_event *event, u64 *value, 1260 u64 *enabled, u64 *running); 1261 extern u64 perf_event_read_value(struct perf_event *event, 1262 u64 *enabled, u64 *running); 1263 1264 extern struct perf_callchain_entry *perf_callchain(struct perf_event *event, struct pt_regs *regs); 1265 1266 static inline bool branch_sample_no_flags(const struct perf_event *event) 1267 { 1268 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_NO_FLAGS; 1269 } 1270 1271 static inline bool branch_sample_no_cycles(const struct perf_event *event) 1272 { 1273 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_NO_CYCLES; 1274 } 1275 1276 static inline bool branch_sample_type(const struct perf_event *event) 1277 { 1278 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_TYPE_SAVE; 1279 } 1280 1281 static inline bool branch_sample_hw_index(const struct perf_event *event) 1282 { 1283 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX; 1284 } 1285 1286 static inline bool branch_sample_priv(const struct perf_event *event) 1287 { 1288 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_PRIV_SAVE; 1289 } 1290 1291 static inline bool branch_sample_counters(const struct perf_event *event) 1292 { 1293 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS; 1294 } 1295 1296 static inline bool branch_sample_call_stack(const struct perf_event *event) 1297 { 1298 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_CALL_STACK; 1299 } 1300 1301 struct perf_sample_data { 1302 /* 1303 * Fields set by perf_sample_data_init() unconditionally, 1304 * group so as to minimize the cachelines touched. 1305 */ 1306 u64 sample_flags; 1307 u64 period; 1308 u64 dyn_size; 1309 1310 /* 1311 * Fields commonly set by __perf_event_header__init_id(), 1312 * group so as to minimize the cachelines touched. 1313 */ 1314 u64 type; 1315 struct { 1316 u32 pid; 1317 u32 tid; 1318 } tid_entry; 1319 u64 time; 1320 u64 id; 1321 struct { 1322 u32 cpu; 1323 u32 reserved; 1324 } cpu_entry; 1325 1326 /* 1327 * The other fields, optionally {set,used} by 1328 * perf_{prepare,output}_sample(). 1329 */ 1330 u64 ip; 1331 struct perf_callchain_entry *callchain; 1332 struct perf_raw_record *raw; 1333 struct perf_branch_stack *br_stack; 1334 u64 *br_stack_cntr; 1335 union perf_sample_weight weight; 1336 union perf_mem_data_src data_src; 1337 u64 txn; 1338 1339 struct perf_regs regs_user; 1340 struct perf_regs regs_intr; 1341 u64 stack_user_size; 1342 1343 u64 stream_id; 1344 u64 cgroup; 1345 u64 addr; 1346 u64 phys_addr; 1347 u64 data_page_size; 1348 u64 code_page_size; 1349 u64 aux_size; 1350 } ____cacheline_aligned; 1351 1352 /* default value for data source */ 1353 #define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\ 1354 PERF_MEM_S(LVL, NA) |\ 1355 PERF_MEM_S(SNOOP, NA) |\ 1356 PERF_MEM_S(LOCK, NA) |\ 1357 PERF_MEM_S(TLB, NA) |\ 1358 PERF_MEM_S(LVLNUM, NA)) 1359 1360 static inline void perf_sample_data_init(struct perf_sample_data *data, 1361 u64 addr, u64 period) 1362 { 1363 /* remaining struct members initialized in perf_prepare_sample() */ 1364 data->sample_flags = PERF_SAMPLE_PERIOD; 1365 data->period = period; 1366 data->dyn_size = 0; 1367 1368 if (addr) { 1369 data->addr = addr; 1370 data->sample_flags |= PERF_SAMPLE_ADDR; 1371 } 1372 } 1373 1374 static inline void perf_sample_save_callchain(struct perf_sample_data *data, 1375 struct perf_event *event, 1376 struct pt_regs *regs) 1377 { 1378 int size = 1; 1379 1380 if (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)) 1381 return; 1382 if (WARN_ON_ONCE(data->sample_flags & PERF_SAMPLE_CALLCHAIN)) 1383 return; 1384 1385 data->callchain = perf_callchain(event, regs); 1386 size += data->callchain->nr; 1387 1388 data->dyn_size += size * sizeof(u64); 1389 data->sample_flags |= PERF_SAMPLE_CALLCHAIN; 1390 } 1391 1392 static inline void perf_sample_save_raw_data(struct perf_sample_data *data, 1393 struct perf_event *event, 1394 struct perf_raw_record *raw) 1395 { 1396 struct perf_raw_frag *frag = &raw->frag; 1397 u32 sum = 0; 1398 int size; 1399 1400 if (!(event->attr.sample_type & PERF_SAMPLE_RAW)) 1401 return; 1402 if (WARN_ON_ONCE(data->sample_flags & PERF_SAMPLE_RAW)) 1403 return; 1404 1405 do { 1406 sum += frag->size; 1407 if (perf_raw_frag_last(frag)) 1408 break; 1409 frag = frag->next; 1410 } while (1); 1411 1412 size = round_up(sum + sizeof(u32), sizeof(u64)); 1413 raw->size = size - sizeof(u32); 1414 frag->pad = raw->size - sum; 1415 1416 data->raw = raw; 1417 data->dyn_size += size; 1418 data->sample_flags |= PERF_SAMPLE_RAW; 1419 } 1420 1421 static inline bool has_branch_stack(struct perf_event *event) 1422 { 1423 return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK; 1424 } 1425 1426 static inline void perf_sample_save_brstack(struct perf_sample_data *data, 1427 struct perf_event *event, 1428 struct perf_branch_stack *brs, 1429 u64 *brs_cntr) 1430 { 1431 int size = sizeof(u64); /* nr */ 1432 1433 if (!has_branch_stack(event)) 1434 return; 1435 if (WARN_ON_ONCE(data->sample_flags & PERF_SAMPLE_BRANCH_STACK)) 1436 return; 1437 1438 if (branch_sample_hw_index(event)) 1439 size += sizeof(u64); 1440 1441 brs->nr = min_t(u16, event->attr.sample_max_stack, brs->nr); 1442 1443 size += brs->nr * sizeof(struct perf_branch_entry); 1444 1445 /* 1446 * The extension space for counters is appended after the 1447 * struct perf_branch_stack. It is used to store the occurrences 1448 * of events of each branch. 1449 */ 1450 if (brs_cntr) 1451 size += brs->nr * sizeof(u64); 1452 1453 data->br_stack = brs; 1454 data->br_stack_cntr = brs_cntr; 1455 data->dyn_size += size; 1456 data->sample_flags |= PERF_SAMPLE_BRANCH_STACK; 1457 } 1458 1459 static inline u32 perf_sample_data_size(struct perf_sample_data *data, 1460 struct perf_event *event) 1461 { 1462 u32 size = sizeof(struct perf_event_header); 1463 1464 size += event->header_size + event->id_header_size; 1465 size += data->dyn_size; 1466 1467 return size; 1468 } 1469 1470 /* 1471 * Clear all bitfields in the perf_branch_entry. 1472 * The to and from fields are not cleared because they are 1473 * systematically modified by caller. 1474 */ 1475 static inline void perf_clear_branch_entry_bitfields(struct perf_branch_entry *br) 1476 { 1477 br->mispred = 0; 1478 br->predicted = 0; 1479 br->in_tx = 0; 1480 br->abort = 0; 1481 br->cycles = 0; 1482 br->type = 0; 1483 br->spec = PERF_BR_SPEC_NA; 1484 br->reserved = 0; 1485 } 1486 1487 extern void perf_output_sample(struct perf_output_handle *handle, 1488 struct perf_event_header *header, 1489 struct perf_sample_data *data, 1490 struct perf_event *event); 1491 extern void perf_prepare_sample(struct perf_sample_data *data, 1492 struct perf_event *event, 1493 struct pt_regs *regs); 1494 extern void perf_prepare_header(struct perf_event_header *header, 1495 struct perf_sample_data *data, 1496 struct perf_event *event, 1497 struct pt_regs *regs); 1498 1499 extern int perf_event_overflow(struct perf_event *event, 1500 struct perf_sample_data *data, 1501 struct pt_regs *regs); 1502 1503 extern void perf_event_output_forward(struct perf_event *event, 1504 struct perf_sample_data *data, 1505 struct pt_regs *regs); 1506 extern void perf_event_output_backward(struct perf_event *event, 1507 struct perf_sample_data *data, 1508 struct pt_regs *regs); 1509 extern int perf_event_output(struct perf_event *event, 1510 struct perf_sample_data *data, 1511 struct pt_regs *regs); 1512 1513 static inline bool 1514 is_default_overflow_handler(struct perf_event *event) 1515 { 1516 perf_overflow_handler_t overflow_handler = event->overflow_handler; 1517 1518 if (likely(overflow_handler == perf_event_output_forward)) 1519 return true; 1520 if (unlikely(overflow_handler == perf_event_output_backward)) 1521 return true; 1522 return false; 1523 } 1524 1525 extern void 1526 perf_event_header__init_id(struct perf_event_header *header, 1527 struct perf_sample_data *data, 1528 struct perf_event *event); 1529 extern void 1530 perf_event__output_id_sample(struct perf_event *event, 1531 struct perf_output_handle *handle, 1532 struct perf_sample_data *sample); 1533 1534 extern void 1535 perf_log_lost_samples(struct perf_event *event, u64 lost); 1536 1537 static inline bool event_has_any_exclude_flag(struct perf_event *event) 1538 { 1539 struct perf_event_attr *attr = &event->attr; 1540 1541 return attr->exclude_idle || attr->exclude_user || 1542 attr->exclude_kernel || attr->exclude_hv || 1543 attr->exclude_guest || attr->exclude_host; 1544 } 1545 1546 static inline bool is_sampling_event(struct perf_event *event) 1547 { 1548 return event->attr.sample_period != 0; 1549 } 1550 1551 /* 1552 * Return 1 for a software event, 0 for a hardware event 1553 */ 1554 static inline int is_software_event(struct perf_event *event) 1555 { 1556 return event->event_caps & PERF_EV_CAP_SOFTWARE; 1557 } 1558 1559 /* 1560 * Return 1 for event in sw context, 0 for event in hw context 1561 */ 1562 static inline int in_software_context(struct perf_event *event) 1563 { 1564 return event->pmu_ctx->pmu->task_ctx_nr == perf_sw_context; 1565 } 1566 1567 static inline int is_exclusive_pmu(struct pmu *pmu) 1568 { 1569 return pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE; 1570 } 1571 1572 extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; 1573 1574 extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64); 1575 extern void __perf_sw_event(u32, u64, struct pt_regs *, u64); 1576 1577 #ifndef perf_arch_fetch_caller_regs 1578 static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { } 1579 #endif 1580 1581 /* 1582 * When generating a perf sample in-line, instead of from an interrupt / 1583 * exception, we lack a pt_regs. This is typically used from software events 1584 * like: SW_CONTEXT_SWITCHES, SW_MIGRATIONS and the tie-in with tracepoints. 1585 * 1586 * We typically don't need a full set, but (for x86) do require: 1587 * - ip for PERF_SAMPLE_IP 1588 * - cs for user_mode() tests 1589 * - sp for PERF_SAMPLE_CALLCHAIN 1590 * - eflags for MISC bits and CALLCHAIN (see: perf_hw_regs()) 1591 * 1592 * NOTE: assumes @regs is otherwise already 0 filled; this is important for 1593 * things like PERF_SAMPLE_REGS_INTR. 1594 */ 1595 static inline void perf_fetch_caller_regs(struct pt_regs *regs) 1596 { 1597 perf_arch_fetch_caller_regs(regs, CALLER_ADDR0); 1598 } 1599 1600 static __always_inline void 1601 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) 1602 { 1603 if (static_key_false(&perf_swevent_enabled[event_id])) 1604 __perf_sw_event(event_id, nr, regs, addr); 1605 } 1606 1607 DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]); 1608 1609 /* 1610 * 'Special' version for the scheduler, it hard assumes no recursion, 1611 * which is guaranteed by us not actually scheduling inside other swevents 1612 * because those disable preemption. 1613 */ 1614 static __always_inline void __perf_sw_event_sched(u32 event_id, u64 nr, u64 addr) 1615 { 1616 struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]); 1617 1618 perf_fetch_caller_regs(regs); 1619 ___perf_sw_event(event_id, nr, regs, addr); 1620 } 1621 1622 extern struct static_key_false perf_sched_events; 1623 1624 static __always_inline bool __perf_sw_enabled(int swevt) 1625 { 1626 return static_key_false(&perf_swevent_enabled[swevt]); 1627 } 1628 1629 static inline void perf_event_task_migrate(struct task_struct *task) 1630 { 1631 if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS)) 1632 task->sched_migrated = 1; 1633 } 1634 1635 static inline void perf_event_task_sched_in(struct task_struct *prev, 1636 struct task_struct *task) 1637 { 1638 if (static_branch_unlikely(&perf_sched_events)) 1639 __perf_event_task_sched_in(prev, task); 1640 1641 if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS) && 1642 task->sched_migrated) { 1643 __perf_sw_event_sched(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 0); 1644 task->sched_migrated = 0; 1645 } 1646 } 1647 1648 static inline void perf_event_task_sched_out(struct task_struct *prev, 1649 struct task_struct *next) 1650 { 1651 if (__perf_sw_enabled(PERF_COUNT_SW_CONTEXT_SWITCHES)) 1652 __perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0); 1653 1654 #ifdef CONFIG_CGROUP_PERF 1655 if (__perf_sw_enabled(PERF_COUNT_SW_CGROUP_SWITCHES) && 1656 perf_cgroup_from_task(prev, NULL) != 1657 perf_cgroup_from_task(next, NULL)) 1658 __perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0); 1659 #endif 1660 1661 if (static_branch_unlikely(&perf_sched_events)) 1662 __perf_event_task_sched_out(prev, next); 1663 } 1664 1665 extern void perf_event_mmap(struct vm_area_struct *vma); 1666 1667 extern void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len, 1668 bool unregister, const char *sym); 1669 extern void perf_event_bpf_event(struct bpf_prog *prog, 1670 enum perf_bpf_event_type type, 1671 u16 flags); 1672 1673 #define PERF_GUEST_ACTIVE 0x01 1674 #define PERF_GUEST_USER 0x02 1675 1676 struct perf_guest_info_callbacks { 1677 unsigned int (*state)(void); 1678 unsigned long (*get_ip)(void); 1679 unsigned int (*handle_intel_pt_intr)(void); 1680 1681 void (*handle_mediated_pmi)(void); 1682 }; 1683 1684 #ifdef CONFIG_GUEST_PERF_EVENTS 1685 1686 extern struct perf_guest_info_callbacks __rcu *perf_guest_cbs; 1687 1688 DECLARE_STATIC_CALL(__perf_guest_state, *perf_guest_cbs->state); 1689 DECLARE_STATIC_CALL(__perf_guest_get_ip, *perf_guest_cbs->get_ip); 1690 DECLARE_STATIC_CALL(__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr); 1691 DECLARE_STATIC_CALL(__perf_guest_handle_mediated_pmi, *perf_guest_cbs->handle_mediated_pmi); 1692 1693 static inline unsigned int perf_guest_state(void) 1694 { 1695 return static_call(__perf_guest_state)(); 1696 } 1697 1698 static inline unsigned long perf_guest_get_ip(void) 1699 { 1700 return static_call(__perf_guest_get_ip)(); 1701 } 1702 1703 static inline unsigned int perf_guest_handle_intel_pt_intr(void) 1704 { 1705 return static_call(__perf_guest_handle_intel_pt_intr)(); 1706 } 1707 1708 static inline void perf_guest_handle_mediated_pmi(void) 1709 { 1710 static_call(__perf_guest_handle_mediated_pmi)(); 1711 } 1712 1713 extern void perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs); 1714 extern void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs); 1715 1716 #else /* !CONFIG_GUEST_PERF_EVENTS: */ 1717 1718 static inline unsigned int perf_guest_state(void) { return 0; } 1719 static inline unsigned long perf_guest_get_ip(void) { return 0; } 1720 static inline unsigned int perf_guest_handle_intel_pt_intr(void) { return 0; } 1721 1722 #endif /* !CONFIG_GUEST_PERF_EVENTS */ 1723 1724 extern void perf_event_exec(void); 1725 extern void perf_event_comm(struct task_struct *tsk, bool exec); 1726 extern void perf_event_namespaces(struct task_struct *tsk); 1727 extern void perf_event_fork(struct task_struct *tsk); 1728 extern void perf_event_text_poke(const void *addr, 1729 const void *old_bytes, size_t old_len, 1730 const void *new_bytes, size_t new_len); 1731 1732 /* Callchains */ 1733 DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry); 1734 1735 extern void perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs); 1736 extern void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs); 1737 extern struct perf_callchain_entry * 1738 get_perf_callchain(struct pt_regs *regs, bool kernel, bool user, 1739 u32 max_stack, bool crosstask, bool add_mark, u64 defer_cookie); 1740 extern int get_callchain_buffers(int max_stack); 1741 extern void put_callchain_buffers(void); 1742 extern struct perf_callchain_entry *get_callchain_entry(int *rctx); 1743 extern void put_callchain_entry(int rctx); 1744 1745 extern int sysctl_perf_event_max_stack; 1746 extern int sysctl_perf_event_max_contexts_per_stack; 1747 1748 static inline int perf_callchain_store_context(struct perf_callchain_entry_ctx *ctx, u64 ip) 1749 { 1750 if (ctx->contexts < sysctl_perf_event_max_contexts_per_stack) { 1751 struct perf_callchain_entry *entry = ctx->entry; 1752 1753 entry->ip[entry->nr++] = ip; 1754 ++ctx->contexts; 1755 return 0; 1756 } else { 1757 ctx->contexts_maxed = true; 1758 return -1; /* no more room, stop walking the stack */ 1759 } 1760 } 1761 1762 static inline int perf_callchain_store(struct perf_callchain_entry_ctx *ctx, u64 ip) 1763 { 1764 if (ctx->nr < ctx->max_stack && !ctx->contexts_maxed) { 1765 struct perf_callchain_entry *entry = ctx->entry; 1766 1767 entry->ip[entry->nr++] = ip; 1768 ++ctx->nr; 1769 return 0; 1770 } else { 1771 return -1; /* no more room, stop walking the stack */ 1772 } 1773 } 1774 1775 extern int sysctl_perf_event_paranoid; 1776 extern int sysctl_perf_event_sample_rate; 1777 1778 extern void perf_sample_event_took(u64 sample_len_ns); 1779 1780 /* Access to perf_event_open(2) syscall. */ 1781 #define PERF_SECURITY_OPEN 0 1782 1783 /* Finer grained perf_event_open(2) access control. */ 1784 #define PERF_SECURITY_CPU 1 1785 #define PERF_SECURITY_KERNEL 2 1786 #define PERF_SECURITY_TRACEPOINT 3 1787 1788 static inline int perf_is_paranoid(void) 1789 { 1790 return sysctl_perf_event_paranoid > -1; 1791 } 1792 1793 extern int perf_allow_kernel(void); 1794 1795 static inline int perf_allow_cpu(void) 1796 { 1797 if (sysctl_perf_event_paranoid > 0 && !perfmon_capable()) 1798 return -EACCES; 1799 1800 return security_perf_event_open(PERF_SECURITY_CPU); 1801 } 1802 1803 static inline int perf_allow_tracepoint(void) 1804 { 1805 if (sysctl_perf_event_paranoid > -1 && !perfmon_capable()) 1806 return -EPERM; 1807 1808 return security_perf_event_open(PERF_SECURITY_TRACEPOINT); 1809 } 1810 1811 extern int perf_exclude_event(struct perf_event *event, struct pt_regs *regs); 1812 1813 extern void perf_event_init(void); 1814 extern void perf_tp_event(u16 event_type, u64 count, void *record, 1815 int entry_size, struct pt_regs *regs, 1816 struct hlist_head *head, int rctx, 1817 struct task_struct *task); 1818 extern void perf_bp_event(struct perf_event *event, void *data); 1819 1820 extern unsigned long perf_misc_flags(struct perf_event *event, struct pt_regs *regs); 1821 extern unsigned long perf_instruction_pointer(struct perf_event *event, 1822 struct pt_regs *regs); 1823 1824 #ifndef perf_arch_misc_flags 1825 # define perf_arch_misc_flags(regs) \ 1826 (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL) 1827 # define perf_arch_instruction_pointer(regs) instruction_pointer(regs) 1828 #endif 1829 #ifndef perf_arch_bpf_user_pt_regs 1830 # define perf_arch_bpf_user_pt_regs(regs) regs 1831 #endif 1832 1833 #ifndef perf_arch_guest_misc_flags 1834 static inline unsigned long perf_arch_guest_misc_flags(struct pt_regs *regs) 1835 { 1836 unsigned long guest_state = perf_guest_state(); 1837 1838 if (!(guest_state & PERF_GUEST_ACTIVE)) 1839 return 0; 1840 1841 if (guest_state & PERF_GUEST_USER) 1842 return PERF_RECORD_MISC_GUEST_USER; 1843 else 1844 return PERF_RECORD_MISC_GUEST_KERNEL; 1845 } 1846 # define perf_arch_guest_misc_flags(regs) perf_arch_guest_misc_flags(regs) 1847 #endif 1848 1849 static inline bool needs_branch_stack(struct perf_event *event) 1850 { 1851 return event->attr.branch_sample_type != 0; 1852 } 1853 1854 static inline bool has_aux(struct perf_event *event) 1855 { 1856 return event->pmu && event->pmu->setup_aux; 1857 } 1858 1859 static inline bool has_aux_action(struct perf_event *event) 1860 { 1861 return event->attr.aux_sample_size || 1862 event->attr.aux_pause || 1863 event->attr.aux_resume; 1864 } 1865 1866 static inline bool is_write_backward(struct perf_event *event) 1867 { 1868 return !!event->attr.write_backward; 1869 } 1870 1871 static inline bool has_addr_filter(struct perf_event *event) 1872 { 1873 return event->pmu->nr_addr_filters; 1874 } 1875 1876 /* 1877 * An inherited event uses parent's filters 1878 */ 1879 static inline struct perf_addr_filters_head * 1880 perf_event_addr_filters(struct perf_event *event) 1881 { 1882 struct perf_addr_filters_head *ifh = &event->addr_filters; 1883 1884 if (event->parent) 1885 ifh = &event->parent->addr_filters; 1886 1887 return ifh; 1888 } 1889 1890 static inline struct fasync_struct **perf_event_fasync(struct perf_event *event) 1891 { 1892 /* Only the parent has fasync state */ 1893 if (event->parent) 1894 event = event->parent; 1895 return &event->fasync; 1896 } 1897 1898 extern void perf_event_addr_filters_sync(struct perf_event *event); 1899 extern void perf_report_aux_output_id(struct perf_event *event, u64 hw_id); 1900 1901 extern int perf_output_begin(struct perf_output_handle *handle, 1902 struct perf_sample_data *data, 1903 struct perf_event *event, unsigned int size); 1904 extern int perf_output_begin_forward(struct perf_output_handle *handle, 1905 struct perf_sample_data *data, 1906 struct perf_event *event, 1907 unsigned int size); 1908 extern int perf_output_begin_backward(struct perf_output_handle *handle, 1909 struct perf_sample_data *data, 1910 struct perf_event *event, 1911 unsigned int size); 1912 1913 extern void perf_output_end(struct perf_output_handle *handle); 1914 extern unsigned int perf_output_copy(struct perf_output_handle *handle, 1915 const void *buf, unsigned int len); 1916 extern unsigned int perf_output_skip(struct perf_output_handle *handle, 1917 unsigned int len); 1918 extern long perf_output_copy_aux(struct perf_output_handle *aux_handle, 1919 struct perf_output_handle *handle, 1920 unsigned long from, unsigned long to); 1921 extern int perf_swevent_get_recursion_context(void); 1922 extern void perf_swevent_put_recursion_context(int rctx); 1923 extern u64 perf_swevent_set_period(struct perf_event *event); 1924 extern void perf_event_enable(struct perf_event *event); 1925 extern void perf_event_disable(struct perf_event *event); 1926 extern void perf_event_disable_local(struct perf_event *event); 1927 extern void perf_event_disable_inatomic(struct perf_event *event); 1928 extern void perf_event_task_tick(void); 1929 extern int perf_event_account_interrupt(struct perf_event *event); 1930 extern int perf_event_period(struct perf_event *event, u64 value); 1931 extern u64 perf_event_pause(struct perf_event *event, bool reset); 1932 1933 #ifdef CONFIG_PERF_GUEST_MEDIATED_PMU 1934 int perf_create_mediated_pmu(void); 1935 void perf_release_mediated_pmu(void); 1936 void perf_load_guest_context(void); 1937 void perf_put_guest_context(void); 1938 #endif 1939 1940 #else /* !CONFIG_PERF_EVENTS: */ 1941 1942 static inline void * 1943 perf_aux_output_begin(struct perf_output_handle *handle, 1944 struct perf_event *event) { return NULL; } 1945 static inline void 1946 perf_aux_output_end(struct perf_output_handle *handle, unsigned long size) 1947 { } 1948 static inline int 1949 perf_aux_output_skip(struct perf_output_handle *handle, 1950 unsigned long size) { return -EINVAL; } 1951 static inline void * 1952 perf_get_aux(struct perf_output_handle *handle) { return NULL; } 1953 static inline void 1954 perf_event_task_migrate(struct task_struct *task) { } 1955 static inline void 1956 perf_event_task_sched_in(struct task_struct *prev, 1957 struct task_struct *task) { } 1958 static inline void 1959 perf_event_task_sched_out(struct task_struct *prev, 1960 struct task_struct *next) { } 1961 static inline int perf_event_init_task(struct task_struct *child, 1962 u64 clone_flags) { return 0; } 1963 static inline void perf_event_exit_task(struct task_struct *child) { } 1964 static inline void perf_event_free_task(struct task_struct *task) { } 1965 static inline void perf_event_delayed_put(struct task_struct *task) { } 1966 static inline struct file *perf_event_get(unsigned int fd) { return ERR_PTR(-EINVAL); } 1967 static inline const struct perf_event *perf_get_event(struct file *file) 1968 { 1969 return ERR_PTR(-EINVAL); 1970 } 1971 static inline const struct perf_event_attr *perf_event_attrs(struct perf_event *event) 1972 { 1973 return ERR_PTR(-EINVAL); 1974 } 1975 static inline int perf_event_read_local(struct perf_event *event, u64 *value, 1976 u64 *enabled, u64 *running) 1977 { 1978 return -EINVAL; 1979 } 1980 static inline void perf_event_print_debug(void) { } 1981 static inline int perf_event_task_disable(void) { return -EINVAL; } 1982 static inline int perf_event_task_enable(void) { return -EINVAL; } 1983 static inline int perf_event_refresh(struct perf_event *event, int refresh) 1984 { 1985 return -EINVAL; 1986 } 1987 1988 static inline void 1989 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { } 1990 static inline void 1991 perf_bp_event(struct perf_event *event, void *data) { } 1992 1993 static inline void perf_event_mmap(struct vm_area_struct *vma) { } 1994 1995 typedef int (perf_ksymbol_get_name_f)(char *name, int name_len, void *data); 1996 static inline void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len, 1997 bool unregister, const char *sym) { } 1998 static inline void perf_event_bpf_event(struct bpf_prog *prog, 1999 enum perf_bpf_event_type type, 2000 u16 flags) { } 2001 static inline void perf_event_exec(void) { } 2002 static inline void perf_event_comm(struct task_struct *tsk, bool exec) { } 2003 static inline void perf_event_namespaces(struct task_struct *tsk) { } 2004 static inline void perf_event_fork(struct task_struct *tsk) { } 2005 static inline void perf_event_text_poke(const void *addr, 2006 const void *old_bytes, 2007 size_t old_len, 2008 const void *new_bytes, 2009 size_t new_len) { } 2010 static inline void perf_event_init(void) { } 2011 static inline int perf_swevent_get_recursion_context(void) { return -1; } 2012 static inline void perf_swevent_put_recursion_context(int rctx) { } 2013 static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; } 2014 static inline void perf_event_enable(struct perf_event *event) { } 2015 static inline void perf_event_disable(struct perf_event *event) { } 2016 static inline int __perf_event_disable(void *info) { return -1; } 2017 static inline void perf_event_task_tick(void) { } 2018 static inline int perf_event_release_kernel(struct perf_event *event) { return 0; } 2019 static inline int 2020 perf_event_period(struct perf_event *event, u64 value) { return -EINVAL; } 2021 static inline u64 2022 perf_event_pause(struct perf_event *event, bool reset) { return 0; } 2023 static inline int 2024 perf_exclude_event(struct perf_event *event, struct pt_regs *regs) { return 0; } 2025 2026 #endif /* !CONFIG_PERF_EVENTS */ 2027 2028 #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL) 2029 extern void perf_restore_debug_store(void); 2030 #else 2031 static inline void perf_restore_debug_store(void) { } 2032 #endif 2033 2034 #define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x)) 2035 2036 struct perf_pmu_events_attr { 2037 struct device_attribute attr; 2038 u64 id; 2039 const char *event_str; 2040 }; 2041 2042 struct perf_pmu_events_ht_attr { 2043 struct device_attribute attr; 2044 u64 id; 2045 const char *event_str_ht; 2046 const char *event_str_noht; 2047 }; 2048 2049 struct perf_pmu_events_hybrid_attr { 2050 struct device_attribute attr; 2051 u64 id; 2052 const char *event_str; 2053 u64 pmu_type; 2054 }; 2055 2056 struct perf_pmu_format_hybrid_attr { 2057 struct device_attribute attr; 2058 u64 pmu_type; 2059 }; 2060 2061 ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr, 2062 char *page); 2063 2064 #define PMU_EVENT_ATTR(_name, _var, _id, _show) \ 2065 static struct perf_pmu_events_attr _var = { \ 2066 .attr = __ATTR(_name, 0444, _show, NULL), \ 2067 .id = _id, \ 2068 }; 2069 2070 #define PMU_EVENT_ATTR_STRING(_name, _var, _str) \ 2071 static struct perf_pmu_events_attr _var = { \ 2072 .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \ 2073 .id = 0, \ 2074 .event_str = _str, \ 2075 }; 2076 2077 #define PMU_EVENT_ATTR_ID(_name, _show, _id) \ 2078 (&((struct perf_pmu_events_attr[]) { \ 2079 { .attr = __ATTR(_name, 0444, _show, NULL), \ 2080 .id = _id, } \ 2081 })[0].attr.attr) 2082 2083 #define PMU_FORMAT_ATTR_SHOW(_name, _format) \ 2084 static ssize_t \ 2085 _name##_show(struct device *dev, \ 2086 struct device_attribute *attr, \ 2087 char *page) \ 2088 { \ 2089 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \ 2090 return sprintf(page, _format "\n"); \ 2091 } \ 2092 2093 #define PMU_FORMAT_ATTR(_name, _format) \ 2094 PMU_FORMAT_ATTR_SHOW(_name, _format) \ 2095 \ 2096 static struct device_attribute format_attr_##_name = __ATTR_RO(_name) 2097 2098 /* Performance counter hotplug functions */ 2099 #ifdef CONFIG_PERF_EVENTS 2100 extern int perf_event_init_cpu(unsigned int cpu); 2101 extern int perf_event_exit_cpu(unsigned int cpu); 2102 #else 2103 # define perf_event_init_cpu NULL 2104 # define perf_event_exit_cpu NULL 2105 #endif 2106 2107 extern void arch_perf_update_userpage(struct perf_event *event, 2108 struct perf_event_mmap_page *userpg, 2109 u64 now); 2110 2111 /* 2112 * Snapshot branch stack on software events. 2113 * 2114 * Branch stack can be very useful in understanding software events. For 2115 * example, when a long function, e.g. sys_perf_event_open, returns an 2116 * errno, it is not obvious why the function failed. Branch stack could 2117 * provide very helpful information in this type of scenarios. 2118 * 2119 * On software event, it is necessary to stop the hardware branch recorder 2120 * fast. Otherwise, the hardware register/buffer will be flushed with 2121 * entries of the triggering event. Therefore, static call is used to 2122 * stop the hardware recorder. 2123 */ 2124 2125 /* 2126 * cnt is the number of entries allocated for entries. 2127 * Return number of entries copied to . 2128 */ 2129 typedef int (perf_snapshot_branch_stack_t)(struct perf_branch_entry *entries, 2130 unsigned int cnt); 2131 DECLARE_STATIC_CALL(perf_snapshot_branch_stack, perf_snapshot_branch_stack_t); 2132 2133 #ifndef PERF_NEEDS_LOPWR_CB 2134 static inline void perf_lopwr_cb(bool mode) 2135 { 2136 } 2137 #endif 2138 2139 #endif /* _LINUX_PERF_EVENT_H */ 2140