1
2
3
4
5
6
7
8
9
10
11
12
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
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 <asm/hw_breakpoint.h>
40#endif
41
42#include <linux/list.h>
43#include <linux/mutex.h>
44#include <linux/rculist.h>
45#include <linux/rcupdate.h>
46#include <linux/spinlock.h>
47#include <linux/hrtimer.h>
48#include <linux/fs.h>
49#include <linux/pid_namespace.h>
50#include <linux/workqueue.h>
51#include <linux/ftrace.h>
52#include <linux/cpu.h>
53#include <linux/irq_work.h>
54#include <linux/static_key.h>
55#include <linux/jump_label_ratelimit.h>
56#include <linux/atomic.h>
57#include <linux/sysfs.h>
58#include <linux/perf_regs.h>
59#include <linux/cgroup.h>
60#include <linux/refcount.h>
61#include <linux/security.h>
62#include <linux/static_call.h>
63#include <asm/local.h>
64
65struct perf_callchain_entry {
66 __u64 nr;
67 __u64 ip[];
68};
69
70struct perf_callchain_entry_ctx {
71 struct perf_callchain_entry *entry;
72 u32 max_stack;
73 u32 nr;
74 short contexts;
75 bool contexts_maxed;
76};
77
78typedef unsigned long (*perf_copy_f)(void *dst, const void *src,
79 unsigned long off, unsigned long len);
80
81struct perf_raw_frag {
82 union {
83 struct perf_raw_frag *next;
84 unsigned long pad;
85 };
86 perf_copy_f copy;
87 void *data;
88 u32 size;
89} __packed;
90
91struct perf_raw_record {
92 struct perf_raw_frag frag;
93 u32 size;
94};
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116struct perf_branch_stack {
117 __u64 nr;
118 __u64 hw_idx;
119 struct perf_branch_entry entries[];
120};
121
122struct task_struct;
123
124
125
126
127struct hw_perf_event_extra {
128 u64 config;
129 unsigned int reg;
130 int alloc;
131 int idx;
132};
133
134
135
136
137
138
139
140#define PERF_EVENT_FLAG_ARCH 0x0000ffff
141#define PERF_EVENT_FLAG_USER_READ_CNT 0x80000000
142
143
144
145
146struct hw_perf_event {
147#ifdef CONFIG_PERF_EVENTS
148 union {
149 struct {
150 u64 config;
151 u64 last_tag;
152 unsigned long config_base;
153 unsigned long event_base;
154 int event_base_rdpmc;
155 int idx;
156 int last_cpu;
157 int flags;
158
159 struct hw_perf_event_extra extra_reg;
160 struct hw_perf_event_extra branch_reg;
161 };
162 struct {
163 struct hrtimer hrtimer;
164 };
165 struct {
166
167 struct list_head tp_list;
168 };
169 struct {
170 u64 pwr_acc;
171 u64 ptsc;
172 };
173#ifdef CONFIG_HAVE_HW_BREAKPOINT
174 struct {
175
176
177
178
179
180 struct arch_hw_breakpoint info;
181 struct list_head bp_list;
182 };
183#endif
184 struct {
185 u8 iommu_bank;
186 u8 iommu_cntr;
187 u16 padding;
188 u64 conf;
189 u64 conf1;
190 };
191 };
192
193
194
195
196 struct task_struct *target;
197
198
199
200
201
202 void *addr_filters;
203
204
205 unsigned long addr_filters_gen;
206
207
208
209
210#define PERF_HES_STOPPED 0x01
211#define PERF_HES_UPTODATE 0x02
212#define PERF_HES_ARCH 0x04
213
214 int state;
215
216
217
218
219
220 local64_t prev_count;
221
222
223
224
225 u64 sample_period;
226
227 union {
228 struct {
229
230
231
232 u64 last_period;
233
234
235
236
237
238
239
240 local64_t period_left;
241 };
242 struct {
243 u64 saved_metric;
244 u64 saved_slots;
245 };
246 };
247
248
249
250
251
252 u64 interrupts_seq;
253 u64 interrupts;
254
255
256
257
258
259 u64 freq_time_stamp;
260 u64 freq_count_stamp;
261#endif
262};
263
264struct perf_event;
265
266
267
268
269#define PERF_PMU_TXN_ADD 0x1
270#define PERF_PMU_TXN_READ 0x2
271
272
273
274
275#define PERF_PMU_CAP_NO_INTERRUPT 0x0001
276#define PERF_PMU_CAP_NO_NMI 0x0002
277#define PERF_PMU_CAP_AUX_NO_SG 0x0004
278#define PERF_PMU_CAP_EXTENDED_REGS 0x0008
279#define PERF_PMU_CAP_EXCLUSIVE 0x0010
280#define PERF_PMU_CAP_ITRACE 0x0020
281#define PERF_PMU_CAP_HETEROGENEOUS_CPUS 0x0040
282#define PERF_PMU_CAP_NO_EXCLUDE 0x0080
283#define PERF_PMU_CAP_AUX_OUTPUT 0x0100
284#define PERF_PMU_CAP_EXTENDED_HW_TYPE 0x0200
285
286struct perf_output_handle;
287
288
289
290
291struct pmu {
292 struct list_head entry;
293
294 struct module *module;
295 struct device *dev;
296 const struct attribute_group **attr_groups;
297 const struct attribute_group **attr_update;
298 const char *name;
299 int type;
300
301
302
303
304 int capabilities;
305
306 int __percpu *pmu_disable_count;
307 struct perf_cpu_context __percpu *pmu_cpu_context;
308 atomic_t exclusive_cnt;
309 int task_ctx_nr;
310 int hrtimer_interval_ms;
311
312
313 unsigned int nr_addr_filters;
314
315
316
317
318
319 void (*pmu_enable) (struct pmu *pmu);
320 void (*pmu_disable) (struct pmu *pmu);
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338 int (*event_init) (struct perf_event *event);
339
340
341
342
343
344 void (*event_mapped) (struct perf_event *event, struct mm_struct *mm);
345 void (*event_unmapped) (struct perf_event *event, struct mm_struct *mm);
346
347
348
349
350
351#define PERF_EF_START 0x01
352#define PERF_EF_RELOAD 0x02
353#define PERF_EF_UPDATE 0x04
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373 int (*add) (struct perf_event *event, int flags);
374 void (*del) (struct perf_event *event, int flags);
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394 void (*start) (struct perf_event *event, int flags);
395 void (*stop) (struct perf_event *event, int flags);
396
397
398
399
400
401
402
403 void (*read) (struct perf_event *event);
404
405
406
407
408
409
410
411
412
413
414
415 void (*start_txn) (struct pmu *pmu, unsigned int txn_flags);
416
417
418
419
420
421
422
423
424 int (*commit_txn) (struct pmu *pmu);
425
426
427
428
429
430
431 void (*cancel_txn) (struct pmu *pmu);
432
433
434
435
436
437 int (*event_idx) (struct perf_event *event);
438
439
440
441
442 void (*sched_task) (struct perf_event_context *ctx,
443 bool sched_in);
444
445
446
447
448 struct kmem_cache *task_ctx_cache;
449
450
451
452
453
454
455
456 void (*swap_task_ctx) (struct perf_event_context *prev,
457 struct perf_event_context *next);
458
459
460
461
462
463 void *(*setup_aux) (struct perf_event *event, void **pages,
464 int nr_pages, bool overwrite);
465
466
467
468
469
470 void (*free_aux) (void *aux);
471
472
473
474
475
476
477
478
479
480
481 long (*snapshot_aux) (struct perf_event *event,
482 struct perf_output_handle *handle,
483 unsigned long size);
484
485
486
487
488
489
490
491
492
493 int (*addr_filters_validate) (struct list_head *filters);
494
495
496
497
498
499
500
501
502
503
504
505
506
507 void (*addr_filters_sync) (struct perf_event *event);
508
509
510
511
512
513
514
515
516
517 int (*aux_output_match) (struct perf_event *event);
518
519
520
521
522
523 int (*filter_match) (struct perf_event *event);
524
525
526
527
528 int (*check_period) (struct perf_event *event, u64 value);
529};
530
531enum perf_addr_filter_action_t {
532 PERF_ADDR_FILTER_ACTION_STOP = 0,
533 PERF_ADDR_FILTER_ACTION_START,
534 PERF_ADDR_FILTER_ACTION_FILTER,
535};
536
537
538
539
540
541
542
543
544
545
546
547struct perf_addr_filter {
548 struct list_head entry;
549 struct path path;
550 unsigned long offset;
551 unsigned long size;
552 enum perf_addr_filter_action_t action;
553};
554
555
556
557
558
559
560
561
562
563
564
565struct perf_addr_filters_head {
566 struct list_head list;
567 raw_spinlock_t lock;
568 unsigned int nr_file_filters;
569};
570
571struct perf_addr_filter_range {
572 unsigned long start;
573 unsigned long size;
574};
575
576
577
578
579enum perf_event_state {
580 PERF_EVENT_STATE_DEAD = -4,
581 PERF_EVENT_STATE_EXIT = -3,
582 PERF_EVENT_STATE_ERROR = -2,
583 PERF_EVENT_STATE_OFF = -1,
584 PERF_EVENT_STATE_INACTIVE = 0,
585 PERF_EVENT_STATE_ACTIVE = 1,
586};
587
588struct file;
589struct perf_sample_data;
590
591typedef void (*perf_overflow_handler_t)(struct perf_event *,
592 struct perf_sample_data *,
593 struct pt_regs *regs);
594
595
596
597
598
599
600
601
602
603
604
605#define PERF_EV_CAP_SOFTWARE BIT(0)
606#define PERF_EV_CAP_READ_ACTIVE_PKG BIT(1)
607#define PERF_EV_CAP_SIBLING BIT(2)
608
609#define SWEVENT_HLIST_BITS 8
610#define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
611
612struct swevent_hlist {
613 struct hlist_head heads[SWEVENT_HLIST_SIZE];
614 struct rcu_head rcu_head;
615};
616
617#define PERF_ATTACH_CONTEXT 0x01
618#define PERF_ATTACH_GROUP 0x02
619#define PERF_ATTACH_TASK 0x04
620#define PERF_ATTACH_TASK_DATA 0x08
621#define PERF_ATTACH_ITRACE 0x10
622#define PERF_ATTACH_SCHED_CB 0x20
623#define PERF_ATTACH_CHILD 0x40
624
625struct bpf_prog;
626struct perf_cgroup;
627struct perf_buffer;
628
629struct pmu_event_list {
630 raw_spinlock_t lock;
631 struct list_head list;
632};
633
634#define for_each_sibling_event(sibling, event) \
635 if ((event)->group_leader == (event)) \
636 list_for_each_entry((sibling), &(event)->sibling_list, sibling_list)
637
638
639
640
641struct perf_event {
642#ifdef CONFIG_PERF_EVENTS
643
644
645
646
647
648 struct list_head event_entry;
649
650
651
652
653
654 struct list_head sibling_list;
655 struct list_head active_list;
656
657
658
659 struct rb_node group_node;
660 u64 group_index;
661
662
663
664
665
666 struct list_head migrate_entry;
667
668 struct hlist_node hlist_entry;
669 struct list_head active_entry;
670 int nr_siblings;
671
672
673 int event_caps;
674
675 int group_caps;
676
677 struct perf_event *group_leader;
678 struct pmu *pmu;
679 void *pmu_private;
680
681 enum perf_event_state state;
682 unsigned int attach_state;
683 local64_t count;
684 atomic64_t child_count;
685
686
687
688
689
690
691
692 u64 total_time_enabled;
693 u64 total_time_running;
694 u64 tstamp;
695
696 struct perf_event_attr attr;
697 u16 header_size;
698 u16 id_header_size;
699 u16 read_size;
700 struct hw_perf_event hw;
701
702 struct perf_event_context *ctx;
703 atomic_long_t refcount;
704
705
706
707
708
709 atomic64_t child_total_time_enabled;
710 atomic64_t child_total_time_running;
711
712
713
714
715 struct mutex child_mutex;
716 struct list_head child_list;
717 struct perf_event *parent;
718
719 int oncpu;
720 int cpu;
721
722 struct list_head owner_entry;
723 struct task_struct *owner;
724
725
726 struct mutex mmap_mutex;
727 atomic_t mmap_count;
728
729 struct perf_buffer *rb;
730 struct list_head rb_entry;
731 unsigned long rcu_batches;
732 int rcu_pending;
733
734
735 wait_queue_head_t waitq;
736 struct fasync_struct *fasync;
737
738
739 int pending_wakeup;
740 int pending_kill;
741 int pending_disable;
742 unsigned long pending_addr;
743 struct irq_work pending;
744
745 atomic_t event_limit;
746
747
748 struct perf_addr_filters_head addr_filters;
749
750 struct perf_addr_filter_range *addr_filter_ranges;
751 unsigned long addr_filters_gen;
752
753
754 struct perf_event *aux_event;
755
756 void (*destroy)(struct perf_event *);
757 struct rcu_head rcu_head;
758
759 struct pid_namespace *ns;
760 u64 id;
761
762 u64 (*clock)(void);
763 perf_overflow_handler_t overflow_handler;
764 void *overflow_handler_context;
765#ifdef CONFIG_BPF_SYSCALL
766 perf_overflow_handler_t orig_overflow_handler;
767 struct bpf_prog *prog;
768 u64 bpf_cookie;
769#endif
770
771#ifdef CONFIG_EVENT_TRACING
772 struct trace_event_call *tp_event;
773 struct event_filter *filter;
774#ifdef CONFIG_FUNCTION_TRACER
775 struct ftrace_ops ftrace_ops;
776#endif
777#endif
778
779#ifdef CONFIG_CGROUP_PERF
780 struct perf_cgroup *cgrp;
781#endif
782
783#ifdef CONFIG_SECURITY
784 void *security;
785#endif
786 struct list_head sb_list;
787#endif
788};
789
790
791struct perf_event_groups {
792 struct rb_root tree;
793 u64 index;
794};
795
796
797
798
799
800
801struct perf_event_context {
802 struct pmu *pmu;
803
804
805
806
807 raw_spinlock_t lock;
808
809
810
811
812
813 struct mutex mutex;
814
815 struct list_head active_ctx_list;
816 struct perf_event_groups pinned_groups;
817 struct perf_event_groups flexible_groups;
818 struct list_head event_list;
819
820 struct list_head pinned_active;
821 struct list_head flexible_active;
822
823 int nr_events;
824 int nr_active;
825 int nr_user;
826 int is_active;
827 int nr_stat;
828 int nr_freq;
829 int rotate_disable;
830
831
832
833
834 int rotate_necessary;
835 refcount_t refcount;
836 struct task_struct *task;
837
838
839
840
841 u64 time;
842 u64 timestamp;
843 u64 timeoffset;
844
845
846
847
848
849 struct perf_event_context *parent_ctx;
850 u64 parent_gen;
851 u64 generation;
852 int pin_count;
853#ifdef CONFIG_CGROUP_PERF
854 int nr_cgroups;
855#endif
856 void *task_ctx_data;
857 struct rcu_head rcu_head;
858};
859
860
861
862
863
864#define PERF_NR_CONTEXTS 4
865
866
867
868
869struct perf_cpu_context {
870 struct perf_event_context ctx;
871 struct perf_event_context *task_ctx;
872 int active_oncpu;
873 int exclusive;
874
875 raw_spinlock_t hrtimer_lock;
876 struct hrtimer hrtimer;
877 ktime_t hrtimer_interval;
878 unsigned int hrtimer_active;
879
880#ifdef CONFIG_CGROUP_PERF
881 struct perf_cgroup *cgrp;
882 struct list_head cgrp_cpuctx_entry;
883#endif
884
885 struct list_head sched_cb_entry;
886 int sched_cb_usage;
887
888 int online;
889
890
891
892
893 int heap_size;
894 struct perf_event **heap;
895 struct perf_event *heap_default[2];
896};
897
898struct perf_output_handle {
899 struct perf_event *event;
900 struct perf_buffer *rb;
901 unsigned long wakeup;
902 unsigned long size;
903 u64 aux_flags;
904 union {
905 void *addr;
906 unsigned long head;
907 };
908 int page;
909};
910
911struct bpf_perf_event_data_kern {
912 bpf_user_pt_regs_t *regs;
913 struct perf_sample_data *data;
914 struct perf_event *event;
915};
916
917#ifdef CONFIG_CGROUP_PERF
918
919
920
921
922
923struct perf_cgroup_info {
924 u64 time;
925 u64 timestamp;
926 u64 timeoffset;
927 int active;
928};
929
930struct perf_cgroup {
931 struct cgroup_subsys_state css;
932 struct perf_cgroup_info __percpu *info;
933};
934
935
936
937
938
939
940static inline struct perf_cgroup *
941perf_cgroup_from_task(struct task_struct *task, struct perf_event_context *ctx)
942{
943 return container_of(task_css_check(task, perf_event_cgrp_id,
944 ctx ? lockdep_is_held(&ctx->lock)
945 : true),
946 struct perf_cgroup, css);
947}
948#endif
949
950#ifdef CONFIG_PERF_EVENTS
951
952extern void *perf_aux_output_begin(struct perf_output_handle *handle,
953 struct perf_event *event);
954extern void perf_aux_output_end(struct perf_output_handle *handle,
955 unsigned long size);
956extern int perf_aux_output_skip(struct perf_output_handle *handle,
957 unsigned long size);
958extern void *perf_get_aux(struct perf_output_handle *handle);
959extern void perf_aux_output_flag(struct perf_output_handle *handle, u64 flags);
960extern void perf_event_itrace_started(struct perf_event *event);
961
962extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
963extern void perf_pmu_unregister(struct pmu *pmu);
964
965extern void __perf_event_task_sched_in(struct task_struct *prev,
966 struct task_struct *task);
967extern void __perf_event_task_sched_out(struct task_struct *prev,
968 struct task_struct *next);
969extern int perf_event_init_task(struct task_struct *child, u64 clone_flags);
970extern void perf_event_exit_task(struct task_struct *child);
971extern void perf_event_free_task(struct task_struct *task);
972extern void perf_event_delayed_put(struct task_struct *task);
973extern struct file *perf_event_get(unsigned int fd);
974extern const struct perf_event *perf_get_event(struct file *file);
975extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event);
976extern void perf_event_print_debug(void);
977extern void perf_pmu_disable(struct pmu *pmu);
978extern void perf_pmu_enable(struct pmu *pmu);
979extern void perf_sched_cb_dec(struct pmu *pmu);
980extern void perf_sched_cb_inc(struct pmu *pmu);
981extern int perf_event_task_disable(void);
982extern int perf_event_task_enable(void);
983
984extern void perf_pmu_resched(struct pmu *pmu);
985
986extern int perf_event_refresh(struct perf_event *event, int refresh);
987extern void perf_event_update_userpage(struct perf_event *event);
988extern int perf_event_release_kernel(struct perf_event *event);
989extern struct perf_event *
990perf_event_create_kernel_counter(struct perf_event_attr *attr,
991 int cpu,
992 struct task_struct *task,
993 perf_overflow_handler_t callback,
994 void *context);
995extern void perf_pmu_migrate_context(struct pmu *pmu,
996 int src_cpu, int dst_cpu);
997int perf_event_read_local(struct perf_event *event, u64 *value,
998 u64 *enabled, u64 *running);
999extern u64 perf_event_read_value(struct perf_event *event,
1000 u64 *enabled, u64 *running);
1001
1002
1003struct perf_sample_data {
1004
1005
1006
1007
1008 u64 addr;
1009 struct perf_raw_record *raw;
1010 struct perf_branch_stack *br_stack;
1011 u64 period;
1012 union perf_sample_weight weight;
1013 u64 txn;
1014 union perf_mem_data_src data_src;
1015
1016
1017
1018
1019
1020 u64 type;
1021 u64 ip;
1022 struct {
1023 u32 pid;
1024 u32 tid;
1025 } tid_entry;
1026 u64 time;
1027 u64 id;
1028 u64 stream_id;
1029 struct {
1030 u32 cpu;
1031 u32 reserved;
1032 } cpu_entry;
1033 struct perf_callchain_entry *callchain;
1034 u64 aux_size;
1035
1036 struct perf_regs regs_user;
1037 struct perf_regs regs_intr;
1038 u64 stack_user_size;
1039
1040 u64 phys_addr;
1041 u64 cgroup;
1042 u64 data_page_size;
1043 u64 code_page_size;
1044} ____cacheline_aligned;
1045
1046
1047#define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
1048 PERF_MEM_S(LVL, NA) |\
1049 PERF_MEM_S(SNOOP, NA) |\
1050 PERF_MEM_S(LOCK, NA) |\
1051 PERF_MEM_S(TLB, NA))
1052
1053static inline void perf_sample_data_init(struct perf_sample_data *data,
1054 u64 addr, u64 period)
1055{
1056
1057 data->addr = addr;
1058 data->raw = NULL;
1059 data->br_stack = NULL;
1060 data->period = period;
1061 data->weight.full = 0;
1062 data->data_src.val = PERF_MEM_NA;
1063 data->txn = 0;
1064}
1065
1066extern void perf_output_sample(struct perf_output_handle *handle,
1067 struct perf_event_header *header,
1068 struct perf_sample_data *data,
1069 struct perf_event *event);
1070extern void perf_prepare_sample(struct perf_event_header *header,
1071 struct perf_sample_data *data,
1072 struct perf_event *event,
1073 struct pt_regs *regs);
1074
1075extern int perf_event_overflow(struct perf_event *event,
1076 struct perf_sample_data *data,
1077 struct pt_regs *regs);
1078
1079extern void perf_event_output_forward(struct perf_event *event,
1080 struct perf_sample_data *data,
1081 struct pt_regs *regs);
1082extern void perf_event_output_backward(struct perf_event *event,
1083 struct perf_sample_data *data,
1084 struct pt_regs *regs);
1085extern int perf_event_output(struct perf_event *event,
1086 struct perf_sample_data *data,
1087 struct pt_regs *regs);
1088
1089static inline bool
1090is_default_overflow_handler(struct perf_event *event)
1091{
1092 if (likely(event->overflow_handler == perf_event_output_forward))
1093 return true;
1094 if (unlikely(event->overflow_handler == perf_event_output_backward))
1095 return true;
1096 return false;
1097}
1098
1099extern void
1100perf_event_header__init_id(struct perf_event_header *header,
1101 struct perf_sample_data *data,
1102 struct perf_event *event);
1103extern void
1104perf_event__output_id_sample(struct perf_event *event,
1105 struct perf_output_handle *handle,
1106 struct perf_sample_data *sample);
1107
1108extern void
1109perf_log_lost_samples(struct perf_event *event, u64 lost);
1110
1111static inline bool event_has_any_exclude_flag(struct perf_event *event)
1112{
1113 struct perf_event_attr *attr = &event->attr;
1114
1115 return attr->exclude_idle || attr->exclude_user ||
1116 attr->exclude_kernel || attr->exclude_hv ||
1117 attr->exclude_guest || attr->exclude_host;
1118}
1119
1120static inline bool is_sampling_event(struct perf_event *event)
1121{
1122 return event->attr.sample_period != 0;
1123}
1124
1125
1126
1127
1128static inline int is_software_event(struct perf_event *event)
1129{
1130 return event->event_caps & PERF_EV_CAP_SOFTWARE;
1131}
1132
1133
1134
1135
1136static inline int in_software_context(struct perf_event *event)
1137{
1138 return event->ctx->pmu->task_ctx_nr == perf_sw_context;
1139}
1140
1141static inline int is_exclusive_pmu(struct pmu *pmu)
1142{
1143 return pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE;
1144}
1145
1146extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
1147
1148extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
1149extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
1150
1151#ifndef perf_arch_fetch_caller_regs
1152static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
1153#endif
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169static inline void perf_fetch_caller_regs(struct pt_regs *regs)
1170{
1171 perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
1172}
1173
1174static __always_inline void
1175perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
1176{
1177 if (static_key_false(&perf_swevent_enabled[event_id]))
1178 __perf_sw_event(event_id, nr, regs, addr);
1179}
1180
1181DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
1182
1183
1184
1185
1186
1187
1188static __always_inline void __perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
1189{
1190 struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
1191
1192 perf_fetch_caller_regs(regs);
1193 ___perf_sw_event(event_id, nr, regs, addr);
1194}
1195
1196extern struct static_key_false perf_sched_events;
1197
1198static __always_inline bool __perf_sw_enabled(int swevt)
1199{
1200 return static_key_false(&perf_swevent_enabled[swevt]);
1201}
1202
1203static inline void perf_event_task_migrate(struct task_struct *task)
1204{
1205 if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS))
1206 task->sched_migrated = 1;
1207}
1208
1209static inline void perf_event_task_sched_in(struct task_struct *prev,
1210 struct task_struct *task)
1211{
1212 if (static_branch_unlikely(&perf_sched_events))
1213 __perf_event_task_sched_in(prev, task);
1214
1215 if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS) &&
1216 task->sched_migrated) {
1217 __perf_sw_event_sched(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 0);
1218 task->sched_migrated = 0;
1219 }
1220}
1221
1222static inline void perf_event_task_sched_out(struct task_struct *prev,
1223 struct task_struct *next)
1224{
1225 if (__perf_sw_enabled(PERF_COUNT_SW_CONTEXT_SWITCHES))
1226 __perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
1227
1228#ifdef CONFIG_CGROUP_PERF
1229 if (__perf_sw_enabled(PERF_COUNT_SW_CGROUP_SWITCHES) &&
1230 perf_cgroup_from_task(prev, NULL) !=
1231 perf_cgroup_from_task(next, NULL))
1232 __perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);
1233#endif
1234
1235 if (static_branch_unlikely(&perf_sched_events))
1236 __perf_event_task_sched_out(prev, next);
1237}
1238
1239extern void perf_event_mmap(struct vm_area_struct *vma);
1240
1241extern void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
1242 bool unregister, const char *sym);
1243extern void perf_event_bpf_event(struct bpf_prog *prog,
1244 enum perf_bpf_event_type type,
1245 u16 flags);
1246
1247#ifdef CONFIG_GUEST_PERF_EVENTS
1248extern struct perf_guest_info_callbacks __rcu *perf_guest_cbs;
1249
1250DECLARE_STATIC_CALL(__perf_guest_state, *perf_guest_cbs->state);
1251DECLARE_STATIC_CALL(__perf_guest_get_ip, *perf_guest_cbs->get_ip);
1252DECLARE_STATIC_CALL(__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr);
1253
1254static inline unsigned int perf_guest_state(void)
1255{
1256 return static_call(__perf_guest_state)();
1257}
1258static inline unsigned long perf_guest_get_ip(void)
1259{
1260 return static_call(__perf_guest_get_ip)();
1261}
1262static inline unsigned int perf_guest_handle_intel_pt_intr(void)
1263{
1264 return static_call(__perf_guest_handle_intel_pt_intr)();
1265}
1266extern void perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs);
1267extern void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs);
1268#else
1269static inline unsigned int perf_guest_state(void) { return 0; }
1270static inline unsigned long perf_guest_get_ip(void) { return 0; }
1271static inline unsigned int perf_guest_handle_intel_pt_intr(void) { return 0; }
1272#endif
1273
1274extern void perf_event_exec(void);
1275extern void perf_event_comm(struct task_struct *tsk, bool exec);
1276extern void perf_event_namespaces(struct task_struct *tsk);
1277extern void perf_event_fork(struct task_struct *tsk);
1278extern void perf_event_text_poke(const void *addr,
1279 const void *old_bytes, size_t old_len,
1280 const void *new_bytes, size_t new_len);
1281
1282
1283DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
1284
1285extern void perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs);
1286extern void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs);
1287extern struct perf_callchain_entry *
1288get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
1289 u32 max_stack, bool crosstask, bool add_mark);
1290extern struct perf_callchain_entry *perf_callchain(struct perf_event *event, struct pt_regs *regs);
1291extern int get_callchain_buffers(int max_stack);
1292extern void put_callchain_buffers(void);
1293extern struct perf_callchain_entry *get_callchain_entry(int *rctx);
1294extern void put_callchain_entry(int rctx);
1295
1296extern int sysctl_perf_event_max_stack;
1297extern int sysctl_perf_event_max_contexts_per_stack;
1298
1299static inline int perf_callchain_store_context(struct perf_callchain_entry_ctx *ctx, u64 ip)
1300{
1301 if (ctx->contexts < sysctl_perf_event_max_contexts_per_stack) {
1302 struct perf_callchain_entry *entry = ctx->entry;
1303 entry->ip[entry->nr++] = ip;
1304 ++ctx->contexts;
1305 return 0;
1306 } else {
1307 ctx->contexts_maxed = true;
1308 return -1;
1309 }
1310}
1311
1312static inline int perf_callchain_store(struct perf_callchain_entry_ctx *ctx, u64 ip)
1313{
1314 if (ctx->nr < ctx->max_stack && !ctx->contexts_maxed) {
1315 struct perf_callchain_entry *entry = ctx->entry;
1316 entry->ip[entry->nr++] = ip;
1317 ++ctx->nr;
1318 return 0;
1319 } else {
1320 return -1;
1321 }
1322}
1323
1324extern int sysctl_perf_event_paranoid;
1325extern int sysctl_perf_event_mlock;
1326extern int sysctl_perf_event_sample_rate;
1327extern int sysctl_perf_cpu_time_max_percent;
1328
1329extern void perf_sample_event_took(u64 sample_len_ns);
1330
1331int perf_proc_update_handler(struct ctl_table *table, int write,
1332 void *buffer, size_t *lenp, loff_t *ppos);
1333int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
1334 void *buffer, size_t *lenp, loff_t *ppos);
1335int perf_event_max_stack_handler(struct ctl_table *table, int write,
1336 void *buffer, size_t *lenp, loff_t *ppos);
1337
1338
1339#define PERF_SECURITY_OPEN 0
1340
1341
1342#define PERF_SECURITY_CPU 1
1343#define PERF_SECURITY_KERNEL 2
1344#define PERF_SECURITY_TRACEPOINT 3
1345
1346static inline int perf_is_paranoid(void)
1347{
1348 return sysctl_perf_event_paranoid > -1;
1349}
1350
1351static inline int perf_allow_kernel(struct perf_event_attr *attr)
1352{
1353 if (sysctl_perf_event_paranoid > 1 && !perfmon_capable())
1354 return -EACCES;
1355
1356 return security_perf_event_open(attr, PERF_SECURITY_KERNEL);
1357}
1358
1359static inline int perf_allow_cpu(struct perf_event_attr *attr)
1360{
1361 if (sysctl_perf_event_paranoid > 0 && !perfmon_capable())
1362 return -EACCES;
1363
1364 return security_perf_event_open(attr, PERF_SECURITY_CPU);
1365}
1366
1367static inline int perf_allow_tracepoint(struct perf_event_attr *attr)
1368{
1369 if (sysctl_perf_event_paranoid > -1 && !perfmon_capable())
1370 return -EPERM;
1371
1372 return security_perf_event_open(attr, PERF_SECURITY_TRACEPOINT);
1373}
1374
1375extern void perf_event_init(void);
1376extern void perf_tp_event(u16 event_type, u64 count, void *record,
1377 int entry_size, struct pt_regs *regs,
1378 struct hlist_head *head, int rctx,
1379 struct task_struct *task);
1380extern void perf_bp_event(struct perf_event *event, void *data);
1381
1382#ifndef perf_misc_flags
1383# define perf_misc_flags(regs) \
1384 (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
1385# define perf_instruction_pointer(regs) instruction_pointer(regs)
1386#endif
1387#ifndef perf_arch_bpf_user_pt_regs
1388# define perf_arch_bpf_user_pt_regs(regs) regs
1389#endif
1390
1391static inline bool has_branch_stack(struct perf_event *event)
1392{
1393 return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
1394}
1395
1396static inline bool needs_branch_stack(struct perf_event *event)
1397{
1398 return event->attr.branch_sample_type != 0;
1399}
1400
1401static inline bool has_aux(struct perf_event *event)
1402{
1403 return event->pmu->setup_aux;
1404}
1405
1406static inline bool is_write_backward(struct perf_event *event)
1407{
1408 return !!event->attr.write_backward;
1409}
1410
1411static inline bool has_addr_filter(struct perf_event *event)
1412{
1413 return event->pmu->nr_addr_filters;
1414}
1415
1416
1417
1418
1419static inline struct perf_addr_filters_head *
1420perf_event_addr_filters(struct perf_event *event)
1421{
1422 struct perf_addr_filters_head *ifh = &event->addr_filters;
1423
1424 if (event->parent)
1425 ifh = &event->parent->addr_filters;
1426
1427 return ifh;
1428}
1429
1430extern void perf_event_addr_filters_sync(struct perf_event *event);
1431extern void perf_report_aux_output_id(struct perf_event *event, u64 hw_id);
1432
1433extern int perf_output_begin(struct perf_output_handle *handle,
1434 struct perf_sample_data *data,
1435 struct perf_event *event, unsigned int size);
1436extern int perf_output_begin_forward(struct perf_output_handle *handle,
1437 struct perf_sample_data *data,
1438 struct perf_event *event,
1439 unsigned int size);
1440extern int perf_output_begin_backward(struct perf_output_handle *handle,
1441 struct perf_sample_data *data,
1442 struct perf_event *event,
1443 unsigned int size);
1444
1445extern void perf_output_end(struct perf_output_handle *handle);
1446extern unsigned int perf_output_copy(struct perf_output_handle *handle,
1447 const void *buf, unsigned int len);
1448extern unsigned int perf_output_skip(struct perf_output_handle *handle,
1449 unsigned int len);
1450extern long perf_output_copy_aux(struct perf_output_handle *aux_handle,
1451 struct perf_output_handle *handle,
1452 unsigned long from, unsigned long to);
1453extern int perf_swevent_get_recursion_context(void);
1454extern void perf_swevent_put_recursion_context(int rctx);
1455extern u64 perf_swevent_set_period(struct perf_event *event);
1456extern void perf_event_enable(struct perf_event *event);
1457extern void perf_event_disable(struct perf_event *event);
1458extern void perf_event_disable_local(struct perf_event *event);
1459extern void perf_event_disable_inatomic(struct perf_event *event);
1460extern void perf_event_task_tick(void);
1461extern int perf_event_account_interrupt(struct perf_event *event);
1462extern int perf_event_period(struct perf_event *event, u64 value);
1463extern u64 perf_event_pause(struct perf_event *event, bool reset);
1464#else
1465static inline void *
1466perf_aux_output_begin(struct perf_output_handle *handle,
1467 struct perf_event *event) { return NULL; }
1468static inline void
1469perf_aux_output_end(struct perf_output_handle *handle, unsigned long size)
1470 { }
1471static inline int
1472perf_aux_output_skip(struct perf_output_handle *handle,
1473 unsigned long size) { return -EINVAL; }
1474static inline void *
1475perf_get_aux(struct perf_output_handle *handle) { return NULL; }
1476static inline void
1477perf_event_task_migrate(struct task_struct *task) { }
1478static inline void
1479perf_event_task_sched_in(struct task_struct *prev,
1480 struct task_struct *task) { }
1481static inline void
1482perf_event_task_sched_out(struct task_struct *prev,
1483 struct task_struct *next) { }
1484static inline int perf_event_init_task(struct task_struct *child,
1485 u64 clone_flags) { return 0; }
1486static inline void perf_event_exit_task(struct task_struct *child) { }
1487static inline void perf_event_free_task(struct task_struct *task) { }
1488static inline void perf_event_delayed_put(struct task_struct *task) { }
1489static inline struct file *perf_event_get(unsigned int fd) { return ERR_PTR(-EINVAL); }
1490static inline const struct perf_event *perf_get_event(struct file *file)
1491{
1492 return ERR_PTR(-EINVAL);
1493}
1494static inline const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
1495{
1496 return ERR_PTR(-EINVAL);
1497}
1498static inline int perf_event_read_local(struct perf_event *event, u64 *value,
1499 u64 *enabled, u64 *running)
1500{
1501 return -EINVAL;
1502}
1503static inline void perf_event_print_debug(void) { }
1504static inline int perf_event_task_disable(void) { return -EINVAL; }
1505static inline int perf_event_task_enable(void) { return -EINVAL; }
1506static inline int perf_event_refresh(struct perf_event *event, int refresh)
1507{
1508 return -EINVAL;
1509}
1510
1511static inline void
1512perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
1513static inline void
1514perf_bp_event(struct perf_event *event, void *data) { }
1515
1516static inline void perf_event_mmap(struct vm_area_struct *vma) { }
1517
1518typedef int (perf_ksymbol_get_name_f)(char *name, int name_len, void *data);
1519static inline void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
1520 bool unregister, const char *sym) { }
1521static inline void perf_event_bpf_event(struct bpf_prog *prog,
1522 enum perf_bpf_event_type type,
1523 u16 flags) { }
1524static inline void perf_event_exec(void) { }
1525static inline void perf_event_comm(struct task_struct *tsk, bool exec) { }
1526static inline void perf_event_namespaces(struct task_struct *tsk) { }
1527static inline void perf_event_fork(struct task_struct *tsk) { }
1528static inline void perf_event_text_poke(const void *addr,
1529 const void *old_bytes,
1530 size_t old_len,
1531 const void *new_bytes,
1532 size_t new_len) { }
1533static inline void perf_event_init(void) { }
1534static inline int perf_swevent_get_recursion_context(void) { return -1; }
1535static inline void perf_swevent_put_recursion_context(int rctx) { }
1536static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; }
1537static inline void perf_event_enable(struct perf_event *event) { }
1538static inline void perf_event_disable(struct perf_event *event) { }
1539static inline int __perf_event_disable(void *info) { return -1; }
1540static inline void perf_event_task_tick(void) { }
1541static inline int perf_event_release_kernel(struct perf_event *event) { return 0; }
1542static inline int perf_event_period(struct perf_event *event, u64 value)
1543{
1544 return -EINVAL;
1545}
1546static inline u64 perf_event_pause(struct perf_event *event, bool reset)
1547{
1548 return 0;
1549}
1550#endif
1551
1552#if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
1553extern void perf_restore_debug_store(void);
1554#else
1555static inline void perf_restore_debug_store(void) { }
1556#endif
1557
1558static __always_inline bool perf_raw_frag_last(const struct perf_raw_frag *frag)
1559{
1560 return frag->pad < sizeof(u64);
1561}
1562
1563#define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
1564
1565struct perf_pmu_events_attr {
1566 struct device_attribute attr;
1567 u64 id;
1568 const char *event_str;
1569};
1570
1571struct perf_pmu_events_ht_attr {
1572 struct device_attribute attr;
1573 u64 id;
1574 const char *event_str_ht;
1575 const char *event_str_noht;
1576};
1577
1578struct perf_pmu_events_hybrid_attr {
1579 struct device_attribute attr;
1580 u64 id;
1581 const char *event_str;
1582 u64 pmu_type;
1583};
1584
1585struct perf_pmu_format_hybrid_attr {
1586 struct device_attribute attr;
1587 u64 pmu_type;
1588};
1589
1590ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
1591 char *page);
1592
1593#define PMU_EVENT_ATTR(_name, _var, _id, _show) \
1594static struct perf_pmu_events_attr _var = { \
1595 .attr = __ATTR(_name, 0444, _show, NULL), \
1596 .id = _id, \
1597};
1598
1599#define PMU_EVENT_ATTR_STRING(_name, _var, _str) \
1600static struct perf_pmu_events_attr _var = { \
1601 .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
1602 .id = 0, \
1603 .event_str = _str, \
1604};
1605
1606#define PMU_EVENT_ATTR_ID(_name, _show, _id) \
1607 (&((struct perf_pmu_events_attr[]) { \
1608 { .attr = __ATTR(_name, 0444, _show, NULL), \
1609 .id = _id, } \
1610 })[0].attr.attr)
1611
1612#define PMU_FORMAT_ATTR(_name, _format) \
1613static ssize_t \
1614_name##_show(struct device *dev, \
1615 struct device_attribute *attr, \
1616 char *page) \
1617{ \
1618 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
1619 return sprintf(page, _format "\n"); \
1620} \
1621 \
1622static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
1623
1624
1625#ifdef CONFIG_PERF_EVENTS
1626int perf_event_init_cpu(unsigned int cpu);
1627int perf_event_exit_cpu(unsigned int cpu);
1628#else
1629#define perf_event_init_cpu NULL
1630#define perf_event_exit_cpu NULL
1631#endif
1632
1633extern void __weak arch_perf_update_userpage(struct perf_event *event,
1634 struct perf_event_mmap_page *userpg,
1635 u64 now);
1636
1637#ifdef CONFIG_MMU
1638extern __weak u64 arch_perf_get_page_size(struct mm_struct *mm, unsigned long addr);
1639#endif
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659typedef int (perf_snapshot_branch_stack_t)(struct perf_branch_entry *entries,
1660 unsigned int cnt);
1661DECLARE_STATIC_CALL(perf_snapshot_branch_stack, perf_snapshot_branch_stack_t);
1662
1663#endif
1664