at v4.7 39 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 19/* 20 * Kernel-internal data types and definitions: 21 */ 22 23#ifdef CONFIG_PERF_EVENTS 24# include <asm/perf_event.h> 25# include <asm/local64.h> 26#endif 27 28struct perf_guest_info_callbacks { 29 int (*is_in_guest)(void); 30 int (*is_user_mode)(void); 31 unsigned long (*get_guest_ip)(void); 32}; 33 34#ifdef CONFIG_HAVE_HW_BREAKPOINT 35#include <asm/hw_breakpoint.h> 36#endif 37 38#include <linux/list.h> 39#include <linux/mutex.h> 40#include <linux/rculist.h> 41#include <linux/rcupdate.h> 42#include <linux/spinlock.h> 43#include <linux/hrtimer.h> 44#include <linux/fs.h> 45#include <linux/pid_namespace.h> 46#include <linux/workqueue.h> 47#include <linux/ftrace.h> 48#include <linux/cpu.h> 49#include <linux/irq_work.h> 50#include <linux/static_key.h> 51#include <linux/jump_label_ratelimit.h> 52#include <linux/atomic.h> 53#include <linux/sysfs.h> 54#include <linux/perf_regs.h> 55#include <linux/workqueue.h> 56#include <linux/cgroup.h> 57#include <asm/local.h> 58 59struct perf_callchain_entry { 60 __u64 nr; 61 __u64 ip[0]; /* /proc/sys/kernel/perf_event_max_stack */ 62}; 63 64struct perf_callchain_entry_ctx { 65 struct perf_callchain_entry *entry; 66 u32 max_stack; 67 u32 nr; 68 short contexts; 69 bool contexts_maxed; 70}; 71 72struct perf_raw_record { 73 u32 size; 74 void *data; 75}; 76 77/* 78 * branch stack layout: 79 * nr: number of taken branches stored in entries[] 80 * 81 * Note that nr can vary from sample to sample 82 * branches (to, from) are stored from most recent 83 * to least recent, i.e., entries[0] contains the most 84 * recent branch. 85 */ 86struct perf_branch_stack { 87 __u64 nr; 88 struct perf_branch_entry entries[0]; 89}; 90 91struct task_struct; 92 93/* 94 * extra PMU register associated with an event 95 */ 96struct hw_perf_event_extra { 97 u64 config; /* register value */ 98 unsigned int reg; /* register address or index */ 99 int alloc; /* extra register already allocated */ 100 int idx; /* index in shared_regs->regs[] */ 101}; 102 103/** 104 * struct hw_perf_event - performance event hardware details: 105 */ 106struct hw_perf_event { 107#ifdef CONFIG_PERF_EVENTS 108 union { 109 struct { /* hardware */ 110 u64 config; 111 u64 last_tag; 112 unsigned long config_base; 113 unsigned long event_base; 114 int event_base_rdpmc; 115 int idx; 116 int last_cpu; 117 int flags; 118 119 struct hw_perf_event_extra extra_reg; 120 struct hw_perf_event_extra branch_reg; 121 }; 122 struct { /* software */ 123 struct hrtimer hrtimer; 124 }; 125 struct { /* tracepoint */ 126 /* for tp_event->class */ 127 struct list_head tp_list; 128 }; 129 struct { /* intel_cqm */ 130 int cqm_state; 131 u32 cqm_rmid; 132 int is_group_event; 133 struct list_head cqm_events_entry; 134 struct list_head cqm_groups_entry; 135 struct list_head cqm_group_entry; 136 }; 137 struct { /* itrace */ 138 int itrace_started; 139 }; 140 struct { /* amd_power */ 141 u64 pwr_acc; 142 u64 ptsc; 143 }; 144#ifdef CONFIG_HAVE_HW_BREAKPOINT 145 struct { /* breakpoint */ 146 /* 147 * Crufty hack to avoid the chicken and egg 148 * problem hw_breakpoint has with context 149 * creation and event initalization. 150 */ 151 struct arch_hw_breakpoint info; 152 struct list_head bp_list; 153 }; 154#endif 155 }; 156 /* 157 * If the event is a per task event, this will point to the task in 158 * question. See the comment in perf_event_alloc(). 159 */ 160 struct task_struct *target; 161 162 /* 163 * PMU would store hardware filter configuration 164 * here. 165 */ 166 void *addr_filters; 167 168 /* Last sync'ed generation of filters */ 169 unsigned long addr_filters_gen; 170 171/* 172 * hw_perf_event::state flags; used to track the PERF_EF_* state. 173 */ 174#define PERF_HES_STOPPED 0x01 /* the counter is stopped */ 175#define PERF_HES_UPTODATE 0x02 /* event->count up-to-date */ 176#define PERF_HES_ARCH 0x04 177 178 int state; 179 180 /* 181 * The last observed hardware counter value, updated with a 182 * local64_cmpxchg() such that pmu::read() can be called nested. 183 */ 184 local64_t prev_count; 185 186 /* 187 * The period to start the next sample with. 188 */ 189 u64 sample_period; 190 191 /* 192 * The period we started this sample with. 193 */ 194 u64 last_period; 195 196 /* 197 * However much is left of the current period; note that this is 198 * a full 64bit value and allows for generation of periods longer 199 * than hardware might allow. 200 */ 201 local64_t period_left; 202 203 /* 204 * State for throttling the event, see __perf_event_overflow() and 205 * perf_adjust_freq_unthr_context(). 206 */ 207 u64 interrupts_seq; 208 u64 interrupts; 209 210 /* 211 * State for freq target events, see __perf_event_overflow() and 212 * perf_adjust_freq_unthr_context(). 213 */ 214 u64 freq_time_stamp; 215 u64 freq_count_stamp; 216#endif 217}; 218 219struct perf_event; 220 221/* 222 * Common implementation detail of pmu::{start,commit,cancel}_txn 223 */ 224#define PERF_PMU_TXN_ADD 0x1 /* txn to add/schedule event on PMU */ 225#define PERF_PMU_TXN_READ 0x2 /* txn to read event group from PMU */ 226 227/** 228 * pmu::capabilities flags 229 */ 230#define PERF_PMU_CAP_NO_INTERRUPT 0x01 231#define PERF_PMU_CAP_NO_NMI 0x02 232#define PERF_PMU_CAP_AUX_NO_SG 0x04 233#define PERF_PMU_CAP_AUX_SW_DOUBLEBUF 0x08 234#define PERF_PMU_CAP_EXCLUSIVE 0x10 235#define PERF_PMU_CAP_ITRACE 0x20 236#define PERF_PMU_CAP_HETEROGENEOUS_CPUS 0x40 237 238/** 239 * struct pmu - generic performance monitoring unit 240 */ 241struct pmu { 242 struct list_head entry; 243 244 struct module *module; 245 struct device *dev; 246 const struct attribute_group **attr_groups; 247 const char *name; 248 int type; 249 250 /* 251 * various common per-pmu feature flags 252 */ 253 int capabilities; 254 255 int * __percpu pmu_disable_count; 256 struct perf_cpu_context * __percpu pmu_cpu_context; 257 atomic_t exclusive_cnt; /* < 0: cpu; > 0: tsk */ 258 int task_ctx_nr; 259 int hrtimer_interval_ms; 260 261 /* number of address filters this PMU can do */ 262 unsigned int nr_addr_filters; 263 264 /* 265 * Fully disable/enable this PMU, can be used to protect from the PMI 266 * as well as for lazy/batch writing of the MSRs. 267 */ 268 void (*pmu_enable) (struct pmu *pmu); /* optional */ 269 void (*pmu_disable) (struct pmu *pmu); /* optional */ 270 271 /* 272 * Try and initialize the event for this PMU. 273 * 274 * Returns: 275 * -ENOENT -- @event is not for this PMU 276 * 277 * -ENODEV -- @event is for this PMU but PMU not present 278 * -EBUSY -- @event is for this PMU but PMU temporarily unavailable 279 * -EINVAL -- @event is for this PMU but @event is not valid 280 * -EOPNOTSUPP -- @event is for this PMU, @event is valid, but not supported 281 * -EACCESS -- @event is for this PMU, @event is valid, but no privilidges 282 * 283 * 0 -- @event is for this PMU and valid 284 * 285 * Other error return values are allowed. 286 */ 287 int (*event_init) (struct perf_event *event); 288 289 /* 290 * Notification that the event was mapped or unmapped. Called 291 * in the context of the mapping task. 292 */ 293 void (*event_mapped) (struct perf_event *event); /*optional*/ 294 void (*event_unmapped) (struct perf_event *event); /*optional*/ 295 296 /* 297 * Flags for ->add()/->del()/ ->start()/->stop(). There are 298 * matching hw_perf_event::state flags. 299 */ 300#define PERF_EF_START 0x01 /* start the counter when adding */ 301#define PERF_EF_RELOAD 0x02 /* reload the counter when starting */ 302#define PERF_EF_UPDATE 0x04 /* update the counter when stopping */ 303 304 /* 305 * Adds/Removes a counter to/from the PMU, can be done inside a 306 * transaction, see the ->*_txn() methods. 307 * 308 * The add/del callbacks will reserve all hardware resources required 309 * to service the event, this includes any counter constraint 310 * scheduling etc. 311 * 312 * Called with IRQs disabled and the PMU disabled on the CPU the event 313 * is on. 314 * 315 * ->add() called without PERF_EF_START should result in the same state 316 * as ->add() followed by ->stop(). 317 * 318 * ->del() must always PERF_EF_UPDATE stop an event. If it calls 319 * ->stop() that must deal with already being stopped without 320 * PERF_EF_UPDATE. 321 */ 322 int (*add) (struct perf_event *event, int flags); 323 void (*del) (struct perf_event *event, int flags); 324 325 /* 326 * Starts/Stops a counter present on the PMU. 327 * 328 * The PMI handler should stop the counter when perf_event_overflow() 329 * returns !0. ->start() will be used to continue. 330 * 331 * Also used to change the sample period. 332 * 333 * Called with IRQs disabled and the PMU disabled on the CPU the event 334 * is on -- will be called from NMI context with the PMU generates 335 * NMIs. 336 * 337 * ->stop() with PERF_EF_UPDATE will read the counter and update 338 * period/count values like ->read() would. 339 * 340 * ->start() with PERF_EF_RELOAD will reprogram the the counter 341 * value, must be preceded by a ->stop() with PERF_EF_UPDATE. 342 */ 343 void (*start) (struct perf_event *event, int flags); 344 void (*stop) (struct perf_event *event, int flags); 345 346 /* 347 * Updates the counter value of the event. 348 * 349 * For sampling capable PMUs this will also update the software period 350 * hw_perf_event::period_left field. 351 */ 352 void (*read) (struct perf_event *event); 353 354 /* 355 * Group events scheduling is treated as a transaction, add 356 * group events as a whole and perform one schedulability test. 357 * If the test fails, roll back the whole group 358 * 359 * Start the transaction, after this ->add() doesn't need to 360 * do schedulability tests. 361 * 362 * Optional. 363 */ 364 void (*start_txn) (struct pmu *pmu, unsigned int txn_flags); 365 /* 366 * If ->start_txn() disabled the ->add() schedulability test 367 * then ->commit_txn() is required to perform one. On success 368 * the transaction is closed. On error the transaction is kept 369 * open until ->cancel_txn() is called. 370 * 371 * Optional. 372 */ 373 int (*commit_txn) (struct pmu *pmu); 374 /* 375 * Will cancel the transaction, assumes ->del() is called 376 * for each successful ->add() during the transaction. 377 * 378 * Optional. 379 */ 380 void (*cancel_txn) (struct pmu *pmu); 381 382 /* 383 * Will return the value for perf_event_mmap_page::index for this event, 384 * if no implementation is provided it will default to: event->hw.idx + 1. 385 */ 386 int (*event_idx) (struct perf_event *event); /*optional */ 387 388 /* 389 * context-switches callback 390 */ 391 void (*sched_task) (struct perf_event_context *ctx, 392 bool sched_in); 393 /* 394 * PMU specific data size 395 */ 396 size_t task_ctx_size; 397 398 399 /* 400 * Return the count value for a counter. 401 */ 402 u64 (*count) (struct perf_event *event); /*optional*/ 403 404 /* 405 * Set up pmu-private data structures for an AUX area 406 */ 407 void *(*setup_aux) (int cpu, void **pages, 408 int nr_pages, bool overwrite); 409 /* optional */ 410 411 /* 412 * Free pmu-private AUX data structures 413 */ 414 void (*free_aux) (void *aux); /* optional */ 415 416 /* 417 * Validate address range filters: make sure the HW supports the 418 * requested configuration and number of filters; return 0 if the 419 * supplied filters are valid, -errno otherwise. 420 * 421 * Runs in the context of the ioctl()ing process and is not serialized 422 * with the rest of the PMU callbacks. 423 */ 424 int (*addr_filters_validate) (struct list_head *filters); 425 /* optional */ 426 427 /* 428 * Synchronize address range filter configuration: 429 * translate hw-agnostic filters into hardware configuration in 430 * event::hw::addr_filters. 431 * 432 * Runs as a part of filter sync sequence that is done in ->start() 433 * callback by calling perf_event_addr_filters_sync(). 434 * 435 * May (and should) traverse event::addr_filters::list, for which its 436 * caller provides necessary serialization. 437 */ 438 void (*addr_filters_sync) (struct perf_event *event); 439 /* optional */ 440 441 /* 442 * Filter events for PMU-specific reasons. 443 */ 444 int (*filter_match) (struct perf_event *event); /* optional */ 445}; 446 447/** 448 * struct perf_addr_filter - address range filter definition 449 * @entry: event's filter list linkage 450 * @inode: object file's inode for file-based filters 451 * @offset: filter range offset 452 * @size: filter range size 453 * @range: 1: range, 0: address 454 * @filter: 1: filter/start, 0: stop 455 * 456 * This is a hardware-agnostic filter configuration as specified by the user. 457 */ 458struct perf_addr_filter { 459 struct list_head entry; 460 struct inode *inode; 461 unsigned long offset; 462 unsigned long size; 463 unsigned int range : 1, 464 filter : 1; 465}; 466 467/** 468 * struct perf_addr_filters_head - container for address range filters 469 * @list: list of filters for this event 470 * @lock: spinlock that serializes accesses to the @list and event's 471 * (and its children's) filter generations. 472 * 473 * A child event will use parent's @list (and therefore @lock), so they are 474 * bundled together; see perf_event_addr_filters(). 475 */ 476struct perf_addr_filters_head { 477 struct list_head list; 478 raw_spinlock_t lock; 479}; 480 481/** 482 * enum perf_event_active_state - the states of a event 483 */ 484enum perf_event_active_state { 485 PERF_EVENT_STATE_DEAD = -4, 486 PERF_EVENT_STATE_EXIT = -3, 487 PERF_EVENT_STATE_ERROR = -2, 488 PERF_EVENT_STATE_OFF = -1, 489 PERF_EVENT_STATE_INACTIVE = 0, 490 PERF_EVENT_STATE_ACTIVE = 1, 491}; 492 493struct file; 494struct perf_sample_data; 495 496typedef void (*perf_overflow_handler_t)(struct perf_event *, 497 struct perf_sample_data *, 498 struct pt_regs *regs); 499 500enum perf_group_flag { 501 PERF_GROUP_SOFTWARE = 0x1, 502}; 503 504#define SWEVENT_HLIST_BITS 8 505#define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS) 506 507struct swevent_hlist { 508 struct hlist_head heads[SWEVENT_HLIST_SIZE]; 509 struct rcu_head rcu_head; 510}; 511 512#define PERF_ATTACH_CONTEXT 0x01 513#define PERF_ATTACH_GROUP 0x02 514#define PERF_ATTACH_TASK 0x04 515#define PERF_ATTACH_TASK_DATA 0x08 516 517struct perf_cgroup; 518struct ring_buffer; 519 520/** 521 * struct perf_event - performance event kernel representation: 522 */ 523struct perf_event { 524#ifdef CONFIG_PERF_EVENTS 525 /* 526 * entry onto perf_event_context::event_list; 527 * modifications require ctx->lock 528 * RCU safe iterations. 529 */ 530 struct list_head event_entry; 531 532 /* 533 * XXX: group_entry and sibling_list should be mutually exclusive; 534 * either you're a sibling on a group, or you're the group leader. 535 * Rework the code to always use the same list element. 536 * 537 * Locked for modification by both ctx->mutex and ctx->lock; holding 538 * either sufficies for read. 539 */ 540 struct list_head group_entry; 541 struct list_head sibling_list; 542 543 /* 544 * We need storage to track the entries in perf_pmu_migrate_context; we 545 * cannot use the event_entry because of RCU and we want to keep the 546 * group in tact which avoids us using the other two entries. 547 */ 548 struct list_head migrate_entry; 549 550 struct hlist_node hlist_entry; 551 struct list_head active_entry; 552 int nr_siblings; 553 int group_flags; 554 struct perf_event *group_leader; 555 struct pmu *pmu; 556 void *pmu_private; 557 558 enum perf_event_active_state state; 559 unsigned int attach_state; 560 local64_t count; 561 atomic64_t child_count; 562 563 /* 564 * These are the total time in nanoseconds that the event 565 * has been enabled (i.e. eligible to run, and the task has 566 * been scheduled in, if this is a per-task event) 567 * and running (scheduled onto the CPU), respectively. 568 * 569 * They are computed from tstamp_enabled, tstamp_running and 570 * tstamp_stopped when the event is in INACTIVE or ACTIVE state. 571 */ 572 u64 total_time_enabled; 573 u64 total_time_running; 574 575 /* 576 * These are timestamps used for computing total_time_enabled 577 * and total_time_running when the event is in INACTIVE or 578 * ACTIVE state, measured in nanoseconds from an arbitrary point 579 * in time. 580 * tstamp_enabled: the notional time when the event was enabled 581 * tstamp_running: the notional time when the event was scheduled on 582 * tstamp_stopped: in INACTIVE state, the notional time when the 583 * event was scheduled off. 584 */ 585 u64 tstamp_enabled; 586 u64 tstamp_running; 587 u64 tstamp_stopped; 588 589 /* 590 * timestamp shadows the actual context timing but it can 591 * be safely used in NMI interrupt context. It reflects the 592 * context time as it was when the event was last scheduled in. 593 * 594 * ctx_time already accounts for ctx->timestamp. Therefore to 595 * compute ctx_time for a sample, simply add perf_clock(). 596 */ 597 u64 shadow_ctx_time; 598 599 struct perf_event_attr attr; 600 u16 header_size; 601 u16 id_header_size; 602 u16 read_size; 603 struct hw_perf_event hw; 604 605 struct perf_event_context *ctx; 606 atomic_long_t refcount; 607 608 /* 609 * These accumulate total time (in nanoseconds) that children 610 * events have been enabled and running, respectively. 611 */ 612 atomic64_t child_total_time_enabled; 613 atomic64_t child_total_time_running; 614 615 /* 616 * Protect attach/detach and child_list: 617 */ 618 struct mutex child_mutex; 619 struct list_head child_list; 620 struct perf_event *parent; 621 622 int oncpu; 623 int cpu; 624 625 struct list_head owner_entry; 626 struct task_struct *owner; 627 628 /* mmap bits */ 629 struct mutex mmap_mutex; 630 atomic_t mmap_count; 631 632 struct ring_buffer *rb; 633 struct list_head rb_entry; 634 unsigned long rcu_batches; 635 int rcu_pending; 636 637 /* poll related */ 638 wait_queue_head_t waitq; 639 struct fasync_struct *fasync; 640 641 /* delayed work for NMIs and such */ 642 int pending_wakeup; 643 int pending_kill; 644 int pending_disable; 645 struct irq_work pending; 646 647 atomic_t event_limit; 648 649 /* address range filters */ 650 struct perf_addr_filters_head addr_filters; 651 /* vma address array for file-based filders */ 652 unsigned long *addr_filters_offs; 653 unsigned long addr_filters_gen; 654 655 void (*destroy)(struct perf_event *); 656 struct rcu_head rcu_head; 657 658 struct pid_namespace *ns; 659 u64 id; 660 661 u64 (*clock)(void); 662 perf_overflow_handler_t overflow_handler; 663 void *overflow_handler_context; 664 665#ifdef CONFIG_EVENT_TRACING 666 struct trace_event_call *tp_event; 667 struct event_filter *filter; 668#ifdef CONFIG_FUNCTION_TRACER 669 struct ftrace_ops ftrace_ops; 670#endif 671#endif 672 673#ifdef CONFIG_CGROUP_PERF 674 struct perf_cgroup *cgrp; /* cgroup event is attach to */ 675 int cgrp_defer_enabled; 676#endif 677 678#endif /* CONFIG_PERF_EVENTS */ 679}; 680 681/** 682 * struct perf_event_context - event context structure 683 * 684 * Used as a container for task events and CPU events as well: 685 */ 686struct perf_event_context { 687 struct pmu *pmu; 688 /* 689 * Protect the states of the events in the list, 690 * nr_active, and the list: 691 */ 692 raw_spinlock_t lock; 693 /* 694 * Protect the list of events. Locking either mutex or lock 695 * is sufficient to ensure the list doesn't change; to change 696 * the list you need to lock both the mutex and the spinlock. 697 */ 698 struct mutex mutex; 699 700 struct list_head active_ctx_list; 701 struct list_head pinned_groups; 702 struct list_head flexible_groups; 703 struct list_head event_list; 704 int nr_events; 705 int nr_active; 706 int is_active; 707 int nr_stat; 708 int nr_freq; 709 int rotate_disable; 710 atomic_t refcount; 711 struct task_struct *task; 712 713 /* 714 * Context clock, runs when context enabled. 715 */ 716 u64 time; 717 u64 timestamp; 718 719 /* 720 * These fields let us detect when two contexts have both 721 * been cloned (inherited) from a common ancestor. 722 */ 723 struct perf_event_context *parent_ctx; 724 u64 parent_gen; 725 u64 generation; 726 int pin_count; 727 int nr_cgroups; /* cgroup evts */ 728 void *task_ctx_data; /* pmu specific data */ 729 struct rcu_head rcu_head; 730}; 731 732/* 733 * Number of contexts where an event can trigger: 734 * task, softirq, hardirq, nmi. 735 */ 736#define PERF_NR_CONTEXTS 4 737 738/** 739 * struct perf_event_cpu_context - per cpu event context structure 740 */ 741struct perf_cpu_context { 742 struct perf_event_context ctx; 743 struct perf_event_context *task_ctx; 744 int active_oncpu; 745 int exclusive; 746 747 raw_spinlock_t hrtimer_lock; 748 struct hrtimer hrtimer; 749 ktime_t hrtimer_interval; 750 unsigned int hrtimer_active; 751 752 struct pmu *unique_pmu; 753 struct perf_cgroup *cgrp; 754}; 755 756struct perf_output_handle { 757 struct perf_event *event; 758 struct ring_buffer *rb; 759 unsigned long wakeup; 760 unsigned long size; 761 union { 762 void *addr; 763 unsigned long head; 764 }; 765 int page; 766}; 767 768#ifdef CONFIG_CGROUP_PERF 769 770/* 771 * perf_cgroup_info keeps track of time_enabled for a cgroup. 772 * This is a per-cpu dynamically allocated data structure. 773 */ 774struct perf_cgroup_info { 775 u64 time; 776 u64 timestamp; 777}; 778 779struct perf_cgroup { 780 struct cgroup_subsys_state css; 781 struct perf_cgroup_info __percpu *info; 782}; 783 784/* 785 * Must ensure cgroup is pinned (css_get) before calling 786 * this function. In other words, we cannot call this function 787 * if there is no cgroup event for the current CPU context. 788 */ 789static inline struct perf_cgroup * 790perf_cgroup_from_task(struct task_struct *task, struct perf_event_context *ctx) 791{ 792 return container_of(task_css_check(task, perf_event_cgrp_id, 793 ctx ? lockdep_is_held(&ctx->lock) 794 : true), 795 struct perf_cgroup, css); 796} 797#endif /* CONFIG_CGROUP_PERF */ 798 799#ifdef CONFIG_PERF_EVENTS 800 801extern void *perf_aux_output_begin(struct perf_output_handle *handle, 802 struct perf_event *event); 803extern void perf_aux_output_end(struct perf_output_handle *handle, 804 unsigned long size, bool truncated); 805extern int perf_aux_output_skip(struct perf_output_handle *handle, 806 unsigned long size); 807extern void *perf_get_aux(struct perf_output_handle *handle); 808 809extern int perf_pmu_register(struct pmu *pmu, const char *name, int type); 810extern void perf_pmu_unregister(struct pmu *pmu); 811 812extern int perf_num_counters(void); 813extern const char *perf_pmu_name(void); 814extern void __perf_event_task_sched_in(struct task_struct *prev, 815 struct task_struct *task); 816extern void __perf_event_task_sched_out(struct task_struct *prev, 817 struct task_struct *next); 818extern int perf_event_init_task(struct task_struct *child); 819extern void perf_event_exit_task(struct task_struct *child); 820extern void perf_event_free_task(struct task_struct *task); 821extern void perf_event_delayed_put(struct task_struct *task); 822extern struct file *perf_event_get(unsigned int fd); 823extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event); 824extern void perf_event_print_debug(void); 825extern void perf_pmu_disable(struct pmu *pmu); 826extern void perf_pmu_enable(struct pmu *pmu); 827extern void perf_sched_cb_dec(struct pmu *pmu); 828extern void perf_sched_cb_inc(struct pmu *pmu); 829extern int perf_event_task_disable(void); 830extern int perf_event_task_enable(void); 831extern int perf_event_refresh(struct perf_event *event, int refresh); 832extern void perf_event_update_userpage(struct perf_event *event); 833extern int perf_event_release_kernel(struct perf_event *event); 834extern struct perf_event * 835perf_event_create_kernel_counter(struct perf_event_attr *attr, 836 int cpu, 837 struct task_struct *task, 838 perf_overflow_handler_t callback, 839 void *context); 840extern void perf_pmu_migrate_context(struct pmu *pmu, 841 int src_cpu, int dst_cpu); 842extern u64 perf_event_read_local(struct perf_event *event); 843extern u64 perf_event_read_value(struct perf_event *event, 844 u64 *enabled, u64 *running); 845 846 847struct perf_sample_data { 848 /* 849 * Fields set by perf_sample_data_init(), group so as to 850 * minimize the cachelines touched. 851 */ 852 u64 addr; 853 struct perf_raw_record *raw; 854 struct perf_branch_stack *br_stack; 855 u64 period; 856 u64 weight; 857 u64 txn; 858 union perf_mem_data_src data_src; 859 860 /* 861 * The other fields, optionally {set,used} by 862 * perf_{prepare,output}_sample(). 863 */ 864 u64 type; 865 u64 ip; 866 struct { 867 u32 pid; 868 u32 tid; 869 } tid_entry; 870 u64 time; 871 u64 id; 872 u64 stream_id; 873 struct { 874 u32 cpu; 875 u32 reserved; 876 } cpu_entry; 877 struct perf_callchain_entry *callchain; 878 879 /* 880 * regs_user may point to task_pt_regs or to regs_user_copy, depending 881 * on arch details. 882 */ 883 struct perf_regs regs_user; 884 struct pt_regs regs_user_copy; 885 886 struct perf_regs regs_intr; 887 u64 stack_user_size; 888} ____cacheline_aligned; 889 890/* default value for data source */ 891#define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\ 892 PERF_MEM_S(LVL, NA) |\ 893 PERF_MEM_S(SNOOP, NA) |\ 894 PERF_MEM_S(LOCK, NA) |\ 895 PERF_MEM_S(TLB, NA)) 896 897static inline void perf_sample_data_init(struct perf_sample_data *data, 898 u64 addr, u64 period) 899{ 900 /* remaining struct members initialized in perf_prepare_sample() */ 901 data->addr = addr; 902 data->raw = NULL; 903 data->br_stack = NULL; 904 data->period = period; 905 data->weight = 0; 906 data->data_src.val = PERF_MEM_NA; 907 data->txn = 0; 908} 909 910extern void perf_output_sample(struct perf_output_handle *handle, 911 struct perf_event_header *header, 912 struct perf_sample_data *data, 913 struct perf_event *event); 914extern void perf_prepare_sample(struct perf_event_header *header, 915 struct perf_sample_data *data, 916 struct perf_event *event, 917 struct pt_regs *regs); 918 919extern int perf_event_overflow(struct perf_event *event, 920 struct perf_sample_data *data, 921 struct pt_regs *regs); 922 923extern void perf_event_output_forward(struct perf_event *event, 924 struct perf_sample_data *data, 925 struct pt_regs *regs); 926extern void perf_event_output_backward(struct perf_event *event, 927 struct perf_sample_data *data, 928 struct pt_regs *regs); 929extern void perf_event_output(struct perf_event *event, 930 struct perf_sample_data *data, 931 struct pt_regs *regs); 932 933static inline bool 934is_default_overflow_handler(struct perf_event *event) 935{ 936 if (likely(event->overflow_handler == perf_event_output_forward)) 937 return true; 938 if (unlikely(event->overflow_handler == perf_event_output_backward)) 939 return true; 940 return false; 941} 942 943extern void 944perf_event_header__init_id(struct perf_event_header *header, 945 struct perf_sample_data *data, 946 struct perf_event *event); 947extern void 948perf_event__output_id_sample(struct perf_event *event, 949 struct perf_output_handle *handle, 950 struct perf_sample_data *sample); 951 952extern void 953perf_log_lost_samples(struct perf_event *event, u64 lost); 954 955static inline bool is_sampling_event(struct perf_event *event) 956{ 957 return event->attr.sample_period != 0; 958} 959 960/* 961 * Return 1 for a software event, 0 for a hardware event 962 */ 963static inline int is_software_event(struct perf_event *event) 964{ 965 return event->pmu->task_ctx_nr == perf_sw_context; 966} 967 968extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; 969 970extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64); 971extern void __perf_sw_event(u32, u64, struct pt_regs *, u64); 972 973#ifndef perf_arch_fetch_caller_regs 974static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { } 975#endif 976 977/* 978 * Take a snapshot of the regs. Skip ip and frame pointer to 979 * the nth caller. We only need a few of the regs: 980 * - ip for PERF_SAMPLE_IP 981 * - cs for user_mode() tests 982 * - bp for callchains 983 * - eflags, for future purposes, just in case 984 */ 985static inline void perf_fetch_caller_regs(struct pt_regs *regs) 986{ 987 perf_arch_fetch_caller_regs(regs, CALLER_ADDR0); 988} 989 990static __always_inline void 991perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) 992{ 993 if (static_key_false(&perf_swevent_enabled[event_id])) 994 __perf_sw_event(event_id, nr, regs, addr); 995} 996 997DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]); 998 999/* 1000 * 'Special' version for the scheduler, it hard assumes no recursion, 1001 * which is guaranteed by us not actually scheduling inside other swevents 1002 * because those disable preemption. 1003 */ 1004static __always_inline void 1005perf_sw_event_sched(u32 event_id, u64 nr, u64 addr) 1006{ 1007 if (static_key_false(&perf_swevent_enabled[event_id])) { 1008 struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]); 1009 1010 perf_fetch_caller_regs(regs); 1011 ___perf_sw_event(event_id, nr, regs, addr); 1012 } 1013} 1014 1015extern struct static_key_false perf_sched_events; 1016 1017static __always_inline bool 1018perf_sw_migrate_enabled(void) 1019{ 1020 if (static_key_false(&perf_swevent_enabled[PERF_COUNT_SW_CPU_MIGRATIONS])) 1021 return true; 1022 return false; 1023} 1024 1025static inline void perf_event_task_migrate(struct task_struct *task) 1026{ 1027 if (perf_sw_migrate_enabled()) 1028 task->sched_migrated = 1; 1029} 1030 1031static inline void perf_event_task_sched_in(struct task_struct *prev, 1032 struct task_struct *task) 1033{ 1034 if (static_branch_unlikely(&perf_sched_events)) 1035 __perf_event_task_sched_in(prev, task); 1036 1037 if (perf_sw_migrate_enabled() && task->sched_migrated) { 1038 struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]); 1039 1040 perf_fetch_caller_regs(regs); 1041 ___perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, regs, 0); 1042 task->sched_migrated = 0; 1043 } 1044} 1045 1046static inline void perf_event_task_sched_out(struct task_struct *prev, 1047 struct task_struct *next) 1048{ 1049 perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0); 1050 1051 if (static_branch_unlikely(&perf_sched_events)) 1052 __perf_event_task_sched_out(prev, next); 1053} 1054 1055static inline u64 __perf_event_count(struct perf_event *event) 1056{ 1057 return local64_read(&event->count) + atomic64_read(&event->child_count); 1058} 1059 1060extern void perf_event_mmap(struct vm_area_struct *vma); 1061extern struct perf_guest_info_callbacks *perf_guest_cbs; 1062extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks); 1063extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks); 1064 1065extern void perf_event_exec(void); 1066extern void perf_event_comm(struct task_struct *tsk, bool exec); 1067extern void perf_event_fork(struct task_struct *tsk); 1068 1069/* Callchains */ 1070DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry); 1071 1072extern void perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs); 1073extern void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs); 1074extern struct perf_callchain_entry * 1075get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user, 1076 u32 max_stack, bool crosstask, bool add_mark); 1077extern int get_callchain_buffers(void); 1078extern void put_callchain_buffers(void); 1079 1080extern int sysctl_perf_event_max_stack; 1081extern int sysctl_perf_event_max_contexts_per_stack; 1082 1083static inline int perf_callchain_store_context(struct perf_callchain_entry_ctx *ctx, u64 ip) 1084{ 1085 if (ctx->contexts < sysctl_perf_event_max_contexts_per_stack) { 1086 struct perf_callchain_entry *entry = ctx->entry; 1087 entry->ip[entry->nr++] = ip; 1088 ++ctx->contexts; 1089 return 0; 1090 } else { 1091 ctx->contexts_maxed = true; 1092 return -1; /* no more room, stop walking the stack */ 1093 } 1094} 1095 1096static inline int perf_callchain_store(struct perf_callchain_entry_ctx *ctx, u64 ip) 1097{ 1098 if (ctx->nr < ctx->max_stack && !ctx->contexts_maxed) { 1099 struct perf_callchain_entry *entry = ctx->entry; 1100 entry->ip[entry->nr++] = ip; 1101 ++ctx->nr; 1102 return 0; 1103 } else { 1104 return -1; /* no more room, stop walking the stack */ 1105 } 1106} 1107 1108extern int sysctl_perf_event_paranoid; 1109extern int sysctl_perf_event_mlock; 1110extern int sysctl_perf_event_sample_rate; 1111extern int sysctl_perf_cpu_time_max_percent; 1112 1113extern void perf_sample_event_took(u64 sample_len_ns); 1114 1115extern int perf_proc_update_handler(struct ctl_table *table, int write, 1116 void __user *buffer, size_t *lenp, 1117 loff_t *ppos); 1118extern int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write, 1119 void __user *buffer, size_t *lenp, 1120 loff_t *ppos); 1121 1122int perf_event_max_stack_handler(struct ctl_table *table, int write, 1123 void __user *buffer, size_t *lenp, loff_t *ppos); 1124 1125static inline bool perf_paranoid_tracepoint_raw(void) 1126{ 1127 return sysctl_perf_event_paranoid > -1; 1128} 1129 1130static inline bool perf_paranoid_cpu(void) 1131{ 1132 return sysctl_perf_event_paranoid > 0; 1133} 1134 1135static inline bool perf_paranoid_kernel(void) 1136{ 1137 return sysctl_perf_event_paranoid > 1; 1138} 1139 1140extern void perf_event_init(void); 1141extern void perf_tp_event(u16 event_type, u64 count, void *record, 1142 int entry_size, struct pt_regs *regs, 1143 struct hlist_head *head, int rctx, 1144 struct task_struct *task); 1145extern void perf_bp_event(struct perf_event *event, void *data); 1146 1147#ifndef perf_misc_flags 1148# define perf_misc_flags(regs) \ 1149 (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL) 1150# define perf_instruction_pointer(regs) instruction_pointer(regs) 1151#endif 1152 1153static inline bool has_branch_stack(struct perf_event *event) 1154{ 1155 return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK; 1156} 1157 1158static inline bool needs_branch_stack(struct perf_event *event) 1159{ 1160 return event->attr.branch_sample_type != 0; 1161} 1162 1163static inline bool has_aux(struct perf_event *event) 1164{ 1165 return event->pmu->setup_aux; 1166} 1167 1168static inline bool is_write_backward(struct perf_event *event) 1169{ 1170 return !!event->attr.write_backward; 1171} 1172 1173static inline bool has_addr_filter(struct perf_event *event) 1174{ 1175 return event->pmu->nr_addr_filters; 1176} 1177 1178/* 1179 * An inherited event uses parent's filters 1180 */ 1181static inline struct perf_addr_filters_head * 1182perf_event_addr_filters(struct perf_event *event) 1183{ 1184 struct perf_addr_filters_head *ifh = &event->addr_filters; 1185 1186 if (event->parent) 1187 ifh = &event->parent->addr_filters; 1188 1189 return ifh; 1190} 1191 1192extern void perf_event_addr_filters_sync(struct perf_event *event); 1193 1194extern int perf_output_begin(struct perf_output_handle *handle, 1195 struct perf_event *event, unsigned int size); 1196extern int perf_output_begin_forward(struct perf_output_handle *handle, 1197 struct perf_event *event, 1198 unsigned int size); 1199extern int perf_output_begin_backward(struct perf_output_handle *handle, 1200 struct perf_event *event, 1201 unsigned int size); 1202 1203extern void perf_output_end(struct perf_output_handle *handle); 1204extern unsigned int perf_output_copy(struct perf_output_handle *handle, 1205 const void *buf, unsigned int len); 1206extern unsigned int perf_output_skip(struct perf_output_handle *handle, 1207 unsigned int len); 1208extern int perf_swevent_get_recursion_context(void); 1209extern void perf_swevent_put_recursion_context(int rctx); 1210extern u64 perf_swevent_set_period(struct perf_event *event); 1211extern void perf_event_enable(struct perf_event *event); 1212extern void perf_event_disable(struct perf_event *event); 1213extern void perf_event_disable_local(struct perf_event *event); 1214extern void perf_event_task_tick(void); 1215#else /* !CONFIG_PERF_EVENTS: */ 1216static inline void * 1217perf_aux_output_begin(struct perf_output_handle *handle, 1218 struct perf_event *event) { return NULL; } 1219static inline void 1220perf_aux_output_end(struct perf_output_handle *handle, unsigned long size, 1221 bool truncated) { } 1222static inline int 1223perf_aux_output_skip(struct perf_output_handle *handle, 1224 unsigned long size) { return -EINVAL; } 1225static inline void * 1226perf_get_aux(struct perf_output_handle *handle) { return NULL; } 1227static inline void 1228perf_event_task_migrate(struct task_struct *task) { } 1229static inline void 1230perf_event_task_sched_in(struct task_struct *prev, 1231 struct task_struct *task) { } 1232static inline void 1233perf_event_task_sched_out(struct task_struct *prev, 1234 struct task_struct *next) { } 1235static inline int perf_event_init_task(struct task_struct *child) { return 0; } 1236static inline void perf_event_exit_task(struct task_struct *child) { } 1237static inline void perf_event_free_task(struct task_struct *task) { } 1238static inline void perf_event_delayed_put(struct task_struct *task) { } 1239static inline struct file *perf_event_get(unsigned int fd) { return ERR_PTR(-EINVAL); } 1240static inline const struct perf_event_attr *perf_event_attrs(struct perf_event *event) 1241{ 1242 return ERR_PTR(-EINVAL); 1243} 1244static inline u64 perf_event_read_local(struct perf_event *event) { return -EINVAL; } 1245static inline void perf_event_print_debug(void) { } 1246static inline int perf_event_task_disable(void) { return -EINVAL; } 1247static inline int perf_event_task_enable(void) { return -EINVAL; } 1248static inline int perf_event_refresh(struct perf_event *event, int refresh) 1249{ 1250 return -EINVAL; 1251} 1252 1253static inline void 1254perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { } 1255static inline void 1256perf_sw_event_sched(u32 event_id, u64 nr, u64 addr) { } 1257static inline void 1258perf_bp_event(struct perf_event *event, void *data) { } 1259 1260static inline int perf_register_guest_info_callbacks 1261(struct perf_guest_info_callbacks *callbacks) { return 0; } 1262static inline int perf_unregister_guest_info_callbacks 1263(struct perf_guest_info_callbacks *callbacks) { return 0; } 1264 1265static inline void perf_event_mmap(struct vm_area_struct *vma) { } 1266static inline void perf_event_exec(void) { } 1267static inline void perf_event_comm(struct task_struct *tsk, bool exec) { } 1268static inline void perf_event_fork(struct task_struct *tsk) { } 1269static inline void perf_event_init(void) { } 1270static inline int perf_swevent_get_recursion_context(void) { return -1; } 1271static inline void perf_swevent_put_recursion_context(int rctx) { } 1272static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; } 1273static inline void perf_event_enable(struct perf_event *event) { } 1274static inline void perf_event_disable(struct perf_event *event) { } 1275static inline int __perf_event_disable(void *info) { return -1; } 1276static inline void perf_event_task_tick(void) { } 1277static inline int perf_event_release_kernel(struct perf_event *event) { return 0; } 1278#endif 1279 1280#if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL) 1281extern void perf_restore_debug_store(void); 1282#else 1283static inline void perf_restore_debug_store(void) { } 1284#endif 1285 1286#define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x)) 1287 1288/* 1289 * This has to have a higher priority than migration_notifier in sched/core.c. 1290 */ 1291#define perf_cpu_notifier(fn) \ 1292do { \ 1293 static struct notifier_block fn##_nb = \ 1294 { .notifier_call = fn, .priority = CPU_PRI_PERF }; \ 1295 unsigned long cpu = smp_processor_id(); \ 1296 unsigned long flags; \ 1297 \ 1298 cpu_notifier_register_begin(); \ 1299 fn(&fn##_nb, (unsigned long)CPU_UP_PREPARE, \ 1300 (void *)(unsigned long)cpu); \ 1301 local_irq_save(flags); \ 1302 fn(&fn##_nb, (unsigned long)CPU_STARTING, \ 1303 (void *)(unsigned long)cpu); \ 1304 local_irq_restore(flags); \ 1305 fn(&fn##_nb, (unsigned long)CPU_ONLINE, \ 1306 (void *)(unsigned long)cpu); \ 1307 __register_cpu_notifier(&fn##_nb); \ 1308 cpu_notifier_register_done(); \ 1309} while (0) 1310 1311/* 1312 * Bare-bones version of perf_cpu_notifier(), which doesn't invoke the 1313 * callback for already online CPUs. 1314 */ 1315#define __perf_cpu_notifier(fn) \ 1316do { \ 1317 static struct notifier_block fn##_nb = \ 1318 { .notifier_call = fn, .priority = CPU_PRI_PERF }; \ 1319 \ 1320 __register_cpu_notifier(&fn##_nb); \ 1321} while (0) 1322 1323struct perf_pmu_events_attr { 1324 struct device_attribute attr; 1325 u64 id; 1326 const char *event_str; 1327}; 1328 1329ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr, 1330 char *page); 1331 1332#define PMU_EVENT_ATTR(_name, _var, _id, _show) \ 1333static struct perf_pmu_events_attr _var = { \ 1334 .attr = __ATTR(_name, 0444, _show, NULL), \ 1335 .id = _id, \ 1336}; 1337 1338#define PMU_EVENT_ATTR_STRING(_name, _var, _str) \ 1339static struct perf_pmu_events_attr _var = { \ 1340 .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \ 1341 .id = 0, \ 1342 .event_str = _str, \ 1343}; 1344 1345#define PMU_FORMAT_ATTR(_name, _format) \ 1346static ssize_t \ 1347_name##_show(struct device *dev, \ 1348 struct device_attribute *attr, \ 1349 char *page) \ 1350{ \ 1351 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \ 1352 return sprintf(page, _format "\n"); \ 1353} \ 1354 \ 1355static struct device_attribute format_attr_##_name = __ATTR_RO(_name) 1356 1357#endif /* _LINUX_PERF_EVENT_H */