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