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