xref: /linux-6.15/include/linux/perf_event.h (revision 56799bc0)
1cdd6c482SIngo Molnar /*
2cdd6c482SIngo Molnar  * Performance events:
3cdd6c482SIngo Molnar  *
4cdd6c482SIngo Molnar  *    Copyright (C) 2008-2009, Thomas Gleixner <[email protected]>
5e7e7ee2eSIngo Molnar  *    Copyright (C) 2008-2011, Red Hat, Inc., Ingo Molnar
6e7e7ee2eSIngo Molnar  *    Copyright (C) 2008-2011, Red Hat, Inc., Peter Zijlstra
7cdd6c482SIngo Molnar  *
8cdd6c482SIngo Molnar  * Data type definitions, declarations, prototypes.
9cdd6c482SIngo Molnar  *
10cdd6c482SIngo Molnar  *    Started by: Thomas Gleixner and Ingo Molnar
11cdd6c482SIngo Molnar  *
12cdd6c482SIngo Molnar  * For licencing details see kernel-base/COPYING
13cdd6c482SIngo Molnar  */
14cdd6c482SIngo Molnar #ifndef _LINUX_PERF_EVENT_H
15cdd6c482SIngo Molnar #define _LINUX_PERF_EVENT_H
16cdd6c482SIngo Molnar 
17607ca46eSDavid Howells #include <uapi/linux/perf_event.h>
18c895f6f7SHendrik Brueckner #include <uapi/linux/bpf_perf_event.h>
19cdd6c482SIngo Molnar 
20cdd6c482SIngo Molnar /*
21cdd6c482SIngo Molnar  * Kernel-internal data types and definitions:
22cdd6c482SIngo Molnar  */
23cdd6c482SIngo Molnar 
24cdd6c482SIngo Molnar #ifdef CONFIG_PERF_EVENTS
25cdd6c482SIngo Molnar # include <asm/perf_event.h>
267be79236SPeter Zijlstra # include <asm/local64.h>
27cdd6c482SIngo Molnar #endif
28cdd6c482SIngo Molnar 
29b9f5621cSLike Xu #define PERF_GUEST_ACTIVE	0x01
30b9f5621cSLike Xu #define PERF_GUEST_USER	0x02
31b9f5621cSLike Xu 
3239447b38SZhang, Yanmin struct perf_guest_info_callbacks {
33b9f5621cSLike Xu 	unsigned int			(*state)(void);
34b9f5621cSLike Xu 	unsigned long			(*get_ip)(void);
35b9f5621cSLike Xu 	unsigned int			(*handle_intel_pt_intr)(void);
3639447b38SZhang, Yanmin };
3739447b38SZhang, Yanmin 
382ff6cfd7SArnd Bergmann #ifdef CONFIG_HAVE_HW_BREAKPOINT
390370dc31SMarco Elver #include <linux/rhashtable-types.h>
402ff6cfd7SArnd Bergmann #include <asm/hw_breakpoint.h>
412ff6cfd7SArnd Bergmann #endif
422ff6cfd7SArnd Bergmann 
43cdd6c482SIngo Molnar #include <linux/list.h>
44cdd6c482SIngo Molnar #include <linux/mutex.h>
45cdd6c482SIngo Molnar #include <linux/rculist.h>
46cdd6c482SIngo Molnar #include <linux/rcupdate.h>
47cdd6c482SIngo Molnar #include <linux/spinlock.h>
48cdd6c482SIngo Molnar #include <linux/hrtimer.h>
49cdd6c482SIngo Molnar #include <linux/fs.h>
50cdd6c482SIngo Molnar #include <linux/pid_namespace.h>
51906010b2SPeter Zijlstra #include <linux/workqueue.h>
525331d7b8SFrederic Weisbecker #include <linux/ftrace.h>
5385cfabbcSPeter Zijlstra #include <linux/cpu.h>
54e360adbeSPeter Zijlstra #include <linux/irq_work.h>
55c5905afbSIngo Molnar #include <linux/static_key.h>
56851cf6e7SAndrew Jones #include <linux/jump_label_ratelimit.h>
5760063497SArun Sharma #include <linux/atomic.h>
58641cc938SJiri Olsa #include <linux/sysfs.h>
594018994fSJiri Olsa #include <linux/perf_regs.h>
6039bed6cbSMatt Fleming #include <linux/cgroup.h>
618c94abbbSElena Reshetova #include <linux/refcount.h>
62da97e184SJoel Fernandes (Google) #include <linux/security.h>
63c22ac2a3SSong Liu #include <linux/static_call.h>
64f3c0eba2SPeter Zijlstra #include <linux/lockdep.h>
65fa588151SPeter Zijlstra #include <asm/local.h>
66cdd6c482SIngo Molnar 
67cdd6c482SIngo Molnar struct perf_callchain_entry {
68cdd6c482SIngo Molnar 	__u64				nr;
69c50c75e9SGustavo A. R. Silva 	__u64				ip[]; /* /proc/sys/kernel/perf_event_max_stack */
70cdd6c482SIngo Molnar };
71cdd6c482SIngo Molnar 
72cfbcf468SArnaldo Carvalho de Melo struct perf_callchain_entry_ctx {
73cfbcf468SArnaldo Carvalho de Melo 	struct perf_callchain_entry *entry;
74cfbcf468SArnaldo Carvalho de Melo 	u32			    max_stack;
753b1fff08SArnaldo Carvalho de Melo 	u32			    nr;
76c85b0334SArnaldo Carvalho de Melo 	short			    contexts;
77c85b0334SArnaldo Carvalho de Melo 	bool			    contexts_maxed;
78cfbcf468SArnaldo Carvalho de Melo };
79cfbcf468SArnaldo Carvalho de Melo 
807e3f977eSDaniel Borkmann typedef unsigned long (*perf_copy_f)(void *dst, const void *src,
81aa7145c1SDaniel Borkmann 				     unsigned long off, unsigned long len);
827e3f977eSDaniel Borkmann 
837e3f977eSDaniel Borkmann struct perf_raw_frag {
847e3f977eSDaniel Borkmann 	union {
857e3f977eSDaniel Borkmann 		struct perf_raw_frag	*next;
867e3f977eSDaniel Borkmann 		unsigned long		pad;
877e3f977eSDaniel Borkmann 	};
887e3f977eSDaniel Borkmann 	perf_copy_f			copy;
89cdd6c482SIngo Molnar 	void				*data;
907e3f977eSDaniel Borkmann 	u32				size;
917e3f977eSDaniel Borkmann } __packed;
927e3f977eSDaniel Borkmann 
937e3f977eSDaniel Borkmann struct perf_raw_record {
947e3f977eSDaniel Borkmann 	struct perf_raw_frag		frag;
957e3f977eSDaniel Borkmann 	u32				size;
96cdd6c482SIngo Molnar };
97cdd6c482SIngo Molnar 
perf_raw_frag_last(const struct perf_raw_frag * frag)980a9081cfSNamhyung Kim static __always_inline bool perf_raw_frag_last(const struct perf_raw_frag *frag)
990a9081cfSNamhyung Kim {
1000a9081cfSNamhyung Kim 	return frag->pad < sizeof(u64);
1010a9081cfSNamhyung Kim }
1020a9081cfSNamhyung Kim 
103bce38cd5SStephane Eranian /*
104bce38cd5SStephane Eranian  * branch stack layout:
105bce38cd5SStephane Eranian  *  nr: number of taken branches stored in entries[]
106bbfd5e4fSKan Liang  *  hw_idx: The low level index of raw branch records
107bbfd5e4fSKan Liang  *          for the most recent branch.
108bbfd5e4fSKan Liang  *          -1ULL means invalid/unknown.
109bce38cd5SStephane Eranian  *
110bce38cd5SStephane Eranian  * Note that nr can vary from sample to sample
111bce38cd5SStephane Eranian  * branches (to, from) are stored from most recent
112bce38cd5SStephane Eranian  * to least recent, i.e., entries[0] contains the most
113bce38cd5SStephane Eranian  * recent branch.
114bbfd5e4fSKan Liang  * The entries[] is an abstraction of raw branch records,
115bbfd5e4fSKan Liang  * which may not be stored in age order in HW, e.g. Intel LBR.
116bbfd5e4fSKan Liang  * The hw_idx is to expose the low level index of raw
117bbfd5e4fSKan Liang  * branch record for the most recent branch aka entries[0].
118bbfd5e4fSKan Liang  * The hw_idx index is between -1 (unknown) and max depth,
119bbfd5e4fSKan Liang  * which can be retrieved in /sys/devices/cpu/caps/branches.
120bbfd5e4fSKan Liang  * For the architectures whose raw branch records are
121bbfd5e4fSKan Liang  * already stored in age order, the hw_idx should be 0.
122bce38cd5SStephane Eranian  */
123caff2befSPeter Zijlstra struct perf_branch_stack {
124caff2befSPeter Zijlstra 	__u64				nr;
125bbfd5e4fSKan Liang 	__u64				hw_idx;
126c50c75e9SGustavo A. R. Silva 	struct perf_branch_entry	entries[];
127caff2befSPeter Zijlstra };
128caff2befSPeter Zijlstra 
129cdd6c482SIngo Molnar struct task_struct;
130cdd6c482SIngo Molnar 
131efc9f05dSStephane Eranian /*
132efc9f05dSStephane Eranian  * extra PMU register associated with an event
133efc9f05dSStephane Eranian  */
134efc9f05dSStephane Eranian struct hw_perf_event_extra {
135efc9f05dSStephane Eranian 	u64		config;	/* register value */
136efc9f05dSStephane Eranian 	unsigned int	reg;	/* register address or index */
137efc9f05dSStephane Eranian 	int		alloc;	/* extra register already allocated */
138efc9f05dSStephane Eranian 	int		idx;	/* index in shared_regs->regs[] */
139efc9f05dSStephane Eranian };
140efc9f05dSStephane Eranian 
141cdd6c482SIngo Molnar /**
142369461ceSRob Herring  * hw_perf_event::flag values
143369461ceSRob Herring  *
144369461ceSRob Herring  * PERF_EVENT_FLAG_ARCH bits are reserved for architecture-specific
145369461ceSRob Herring  * usage.
146369461ceSRob Herring  */
1477517f08bSAnshuman Khandual #define PERF_EVENT_FLAG_ARCH			0x000fffff
148369461ceSRob Herring #define PERF_EVENT_FLAG_USER_READ_CNT		0x80000000
149369461ceSRob Herring 
150f67dd218SAnshuman Khandual static_assert((PERF_EVENT_FLAG_USER_READ_CNT & PERF_EVENT_FLAG_ARCH) == 0);
151f67dd218SAnshuman Khandual 
152369461ceSRob Herring /**
153cdd6c482SIngo Molnar  * struct hw_perf_event - performance event hardware details:
154cdd6c482SIngo Molnar  */
155cdd6c482SIngo Molnar struct hw_perf_event {
156cdd6c482SIngo Molnar #ifdef CONFIG_PERF_EVENTS
157cdd6c482SIngo Molnar 	union {
158cdd6c482SIngo Molnar 		struct { /* hardware */
159cdd6c482SIngo Molnar 			u64		config;
160447a194bSStephane Eranian 			u64		last_tag;
161cdd6c482SIngo Molnar 			unsigned long	config_base;
162cdd6c482SIngo Molnar 			unsigned long	event_base;
163c48b6053SVince Weaver 			int		event_base_rdpmc;
164cdd6c482SIngo Molnar 			int		idx;
165447a194bSStephane Eranian 			int		last_cpu;
1669fac2cf3SStephane Eranian 			int		flags;
167bce38cd5SStephane Eranian 
168efc9f05dSStephane Eranian 			struct hw_perf_event_extra extra_reg;
169bce38cd5SStephane Eranian 			struct hw_perf_event_extra branch_reg;
170cdd6c482SIngo Molnar 		};
17152c3fb1aSPeter Zijlstra 		struct { /* aux / Intel-PT */
17252c3fb1aSPeter Zijlstra 			u64		aux_config;
17318d92bb5SAdrian Hunter 			/*
17418d92bb5SAdrian Hunter 			 * For AUX area events, aux_paused cannot be a state
17518d92bb5SAdrian Hunter 			 * flag because it can be updated asynchronously to
17618d92bb5SAdrian Hunter 			 * state.
17718d92bb5SAdrian Hunter 			 */
17818d92bb5SAdrian Hunter 			unsigned int	aux_paused;
17952c3fb1aSPeter Zijlstra 		};
180721a669bSSoeren Sandmann 		struct { /* software */
181cdd6c482SIngo Molnar 			struct hrtimer	hrtimer;
182cdd6c482SIngo Molnar 		};
183f22c1bb6SOleg Nesterov 		struct { /* tracepoint */
184f22c1bb6SOleg Nesterov 			/* for tp_event->class */
185f22c1bb6SOleg Nesterov 			struct list_head	tp_list;
186f22c1bb6SOleg Nesterov 		};
187c7ab62bfSHuang Rui 		struct { /* amd_power */
188c7ab62bfSHuang Rui 			u64	pwr_acc;
189c7ab62bfSHuang Rui 			u64	ptsc;
190c7ab62bfSHuang Rui 		};
19124f1e32cSFrederic Weisbecker #ifdef CONFIG_HAVE_HW_BREAKPOINT
19245a73372SFrederic Weisbecker 		struct { /* breakpoint */
193d580ff86SPeter Zijlstra 			/*
194d580ff86SPeter Zijlstra 			 * Crufty hack to avoid the chicken and egg
195d580ff86SPeter Zijlstra 			 * problem hw_breakpoint has with context
196d580ff86SPeter Zijlstra 			 * creation and event initalization.
197d580ff86SPeter Zijlstra 			 */
198f22c1bb6SOleg Nesterov 			struct arch_hw_breakpoint	info;
1990370dc31SMarco Elver 			struct rhlist_head		bp_list;
20045a73372SFrederic Weisbecker 		};
20124f1e32cSFrederic Weisbecker #endif
202cf25f904SSuravee Suthikulpanit 		struct { /* amd_iommu */
203cf25f904SSuravee Suthikulpanit 			u8	iommu_bank;
204cf25f904SSuravee Suthikulpanit 			u8	iommu_cntr;
205cf25f904SSuravee Suthikulpanit 			u16	padding;
206cf25f904SSuravee Suthikulpanit 			u64	conf;
207cf25f904SSuravee Suthikulpanit 			u64	conf1;
208cf25f904SSuravee Suthikulpanit 		};
209cdd6c482SIngo Molnar 	};
210b0e87875SPeter Zijlstra 	/*
211b0e87875SPeter Zijlstra 	 * If the event is a per task event, this will point to the task in
212b0e87875SPeter Zijlstra 	 * question. See the comment in perf_event_alloc().
213b0e87875SPeter Zijlstra 	 */
21450f16a8bSPeter Zijlstra 	struct task_struct		*target;
215cdd6c482SIngo Molnar 
216a4eaf7f1SPeter Zijlstra 	/*
217375637bcSAlexander Shishkin 	 * PMU would store hardware filter configuration
218375637bcSAlexander Shishkin 	 * here.
219375637bcSAlexander Shishkin 	 */
220375637bcSAlexander Shishkin 	void				*addr_filters;
221375637bcSAlexander Shishkin 
222375637bcSAlexander Shishkin 	/* Last sync'ed generation of filters */
223375637bcSAlexander Shishkin 	unsigned long			addr_filters_gen;
224375637bcSAlexander Shishkin 
225375637bcSAlexander Shishkin /*
226b0e87875SPeter Zijlstra  * hw_perf_event::state flags; used to track the PERF_EF_* state.
227a4eaf7f1SPeter Zijlstra  */
228a4eaf7f1SPeter Zijlstra #define PERF_HES_STOPPED	0x01 /* the counter is stopped */
229a4eaf7f1SPeter Zijlstra #define PERF_HES_UPTODATE	0x02 /* event->count up-to-date */
230a4eaf7f1SPeter Zijlstra #define PERF_HES_ARCH		0x04
231a4eaf7f1SPeter Zijlstra 
232b0e87875SPeter Zijlstra 	int				state;
233b0e87875SPeter Zijlstra 
234b0e87875SPeter Zijlstra 	/*
235b0e87875SPeter Zijlstra 	 * The last observed hardware counter value, updated with a
236b0e87875SPeter Zijlstra 	 * local64_cmpxchg() such that pmu::read() can be called nested.
237b0e87875SPeter Zijlstra 	 */
238b0e87875SPeter Zijlstra 	local64_t			prev_count;
239b0e87875SPeter Zijlstra 
240b0e87875SPeter Zijlstra 	/*
241b0e87875SPeter Zijlstra 	 * The period to start the next sample with.
242b0e87875SPeter Zijlstra 	 */
243b0e87875SPeter Zijlstra 	u64				sample_period;
244b0e87875SPeter Zijlstra 
2452cb5383bSKan Liang 	union {
2462cb5383bSKan Liang 		struct { /* Sampling */
247b0e87875SPeter Zijlstra 			/*
248b0e87875SPeter Zijlstra 			 * The period we started this sample with.
249b0e87875SPeter Zijlstra 			 */
250b0e87875SPeter Zijlstra 			u64				last_period;
251b0e87875SPeter Zijlstra 
252b0e87875SPeter Zijlstra 			/*
2532cb5383bSKan Liang 			 * However much is left of the current period;
2542cb5383bSKan Liang 			 * note that this is a full 64bit value and
2552cb5383bSKan Liang 			 * allows for generation of periods longer
256b0e87875SPeter Zijlstra 			 * than hardware might allow.
257b0e87875SPeter Zijlstra 			 */
258b0e87875SPeter Zijlstra 			local64_t			period_left;
2592cb5383bSKan Liang 		};
2602cb5383bSKan Liang 		struct { /* Topdown events counting for context switch */
2612cb5383bSKan Liang 			u64				saved_metric;
2622cb5383bSKan Liang 			u64				saved_slots;
2632cb5383bSKan Liang 		};
2642cb5383bSKan Liang 	};
265b0e87875SPeter Zijlstra 
266b0e87875SPeter Zijlstra 	/*
267b0e87875SPeter Zijlstra 	 * State for throttling the event, see __perf_event_overflow() and
268b0e87875SPeter Zijlstra 	 * perf_adjust_freq_unthr_context().
269b0e87875SPeter Zijlstra 	 */
270b0e87875SPeter Zijlstra 	u64                             interrupts_seq;
271b0e87875SPeter Zijlstra 	u64				interrupts;
272b0e87875SPeter Zijlstra 
273b0e87875SPeter Zijlstra 	/*
274b0e87875SPeter Zijlstra 	 * State for freq target events, see __perf_event_overflow() and
275b0e87875SPeter Zijlstra 	 * perf_adjust_freq_unthr_context().
276b0e87875SPeter Zijlstra 	 */
277b0e87875SPeter Zijlstra 	u64				freq_time_stamp;
278b0e87875SPeter Zijlstra 	u64				freq_count_stamp;
279b0e87875SPeter Zijlstra #endif
280b0e87875SPeter Zijlstra };
281b0e87875SPeter Zijlstra 
282cdd6c482SIngo Molnar struct perf_event;
283bd275681SPeter Zijlstra struct perf_event_pmu_context;
284cdd6c482SIngo Molnar 
2858d2cacbbSPeter Zijlstra /*
2868d2cacbbSPeter Zijlstra  * Common implementation detail of pmu::{start,commit,cancel}_txn
2878d2cacbbSPeter Zijlstra  */
288fbbe0701SSukadev Bhattiprolu #define PERF_PMU_TXN_ADD  0x1		/* txn to add/schedule event on PMU */
2894a00c16eSSukadev Bhattiprolu #define PERF_PMU_TXN_READ 0x2		/* txn to read event group from PMU */
290fbbe0701SSukadev Bhattiprolu 
291cdd6c482SIngo Molnar /**
29253b25335SVince Weaver  * pmu::capabilities flags
29353b25335SVince Weaver  */
29455bcf6efSKan Liang #define PERF_PMU_CAP_NO_INTERRUPT		0x0001
29555bcf6efSKan Liang #define PERF_PMU_CAP_NO_NMI			0x0002
29655bcf6efSKan Liang #define PERF_PMU_CAP_AUX_NO_SG			0x0004
29755bcf6efSKan Liang #define PERF_PMU_CAP_EXTENDED_REGS		0x0008
29855bcf6efSKan Liang #define PERF_PMU_CAP_EXCLUSIVE			0x0010
29955bcf6efSKan Liang #define PERF_PMU_CAP_ITRACE			0x0020
3000cb52ad7SJames Clark #define PERF_PMU_CAP_NO_EXCLUDE			0x0040
3010cb52ad7SJames Clark #define PERF_PMU_CAP_AUX_OUTPUT			0x0080
3020cb52ad7SJames Clark #define PERF_PMU_CAP_EXTENDED_HW_TYPE		0x0100
30318d92bb5SAdrian Hunter #define PERF_PMU_CAP_AUX_PAUSE			0x0200
30453b25335SVince Weaver 
3054ba4f1afSKan Liang /**
3064ba4f1afSKan Liang  * pmu::scope
3074ba4f1afSKan Liang  */
3084ba4f1afSKan Liang enum perf_pmu_scope {
3094ba4f1afSKan Liang 	PERF_PMU_SCOPE_NONE	= 0,
3104ba4f1afSKan Liang 	PERF_PMU_SCOPE_CORE,
3114ba4f1afSKan Liang 	PERF_PMU_SCOPE_DIE,
3124ba4f1afSKan Liang 	PERF_PMU_SCOPE_CLUSTER,
3134ba4f1afSKan Liang 	PERF_PMU_SCOPE_PKG,
3144ba4f1afSKan Liang 	PERF_PMU_SCOPE_SYS_WIDE,
3154ba4f1afSKan Liang 	PERF_PMU_MAX_SCOPE,
3164ba4f1afSKan Liang };
3174ba4f1afSKan Liang 
318a4faf00dSAlexander Shishkin struct perf_output_handle;
319a4faf00dSAlexander Shishkin 
3200d6d062cSRavi Bangoria #define PMU_NULL_DEV	((void *)(~0UL))
3210d6d062cSRavi Bangoria 
32253b25335SVince Weaver /**
323cdd6c482SIngo Molnar  * struct pmu - generic performance monitoring unit
324cdd6c482SIngo Molnar  */
325cdd6c482SIngo Molnar struct pmu {
326b0a873ebSPeter Zijlstra 	struct list_head		entry;
327b0a873ebSPeter Zijlstra 
328c464c76eSYan, Zheng 	struct module			*module;
329abe43400SPeter Zijlstra 	struct device			*dev;
330143f83e2SJonathan Cameron 	struct device			*parent;
3310c9d42edSPeter Zijlstra 	const struct attribute_group	**attr_groups;
332f3a3a825SJiri Olsa 	const struct attribute_group	**attr_update;
33303d8e80bSMischa Jonker 	const char			*name;
3342e80a82aSPeter Zijlstra 	int				type;
3352e80a82aSPeter Zijlstra 
33653b25335SVince Weaver 	/*
33753b25335SVince Weaver 	 * various common per-pmu feature flags
33853b25335SVince Weaver 	 */
33953b25335SVince Weaver 	int				capabilities;
34053b25335SVince Weaver 
3414ba4f1afSKan Liang 	/*
3424ba4f1afSKan Liang 	 * PMU scope
3434ba4f1afSKan Liang 	 */
3444ba4f1afSKan Liang 	unsigned int			scope;
3454ba4f1afSKan Liang 
346*12e766d1SPeter Zijlstra 	struct perf_cpu_pmu_context * __percpu *cpu_pmu_context;
347bed5b25aSAlexander Shishkin 	atomic_t			exclusive_cnt; /* < 0: cpu; > 0: tsk */
3488dc85d54SPeter Zijlstra 	int				task_ctx_nr;
34962b85639SStephane Eranian 	int				hrtimer_interval_ms;
35033696fc0SPeter Zijlstra 
351375637bcSAlexander Shishkin 	/* number of address filters this PMU can do */
352375637bcSAlexander Shishkin 	unsigned int			nr_addr_filters;
353375637bcSAlexander Shishkin 
354a4eaf7f1SPeter Zijlstra 	/*
355a4eaf7f1SPeter Zijlstra 	 * Fully disable/enable this PMU, can be used to protect from the PMI
356a4eaf7f1SPeter Zijlstra 	 * as well as for lazy/batch writing of the MSRs.
357a4eaf7f1SPeter Zijlstra 	 */
358ad5133b7SPeter Zijlstra 	void (*pmu_enable)		(struct pmu *pmu); /* optional */
359ad5133b7SPeter Zijlstra 	void (*pmu_disable)		(struct pmu *pmu); /* optional */
36033696fc0SPeter Zijlstra 
361b0a873ebSPeter Zijlstra 	/*
362a4eaf7f1SPeter Zijlstra 	 * Try and initialize the event for this PMU.
363b0e87875SPeter Zijlstra 	 *
364b0e87875SPeter Zijlstra 	 * Returns:
365b0e87875SPeter Zijlstra 	 *  -ENOENT	-- @event is not for this PMU
366b0e87875SPeter Zijlstra 	 *
367b0e87875SPeter Zijlstra 	 *  -ENODEV	-- @event is for this PMU but PMU not present
368b0e87875SPeter Zijlstra 	 *  -EBUSY	-- @event is for this PMU but PMU temporarily unavailable
369b0e87875SPeter Zijlstra 	 *  -EINVAL	-- @event is for this PMU but @event is not valid
370b0e87875SPeter Zijlstra 	 *  -EOPNOTSUPP -- @event is for this PMU, @event is valid, but not supported
371652521d4SGeert Uytterhoeven 	 *  -EACCES	-- @event is for this PMU, @event is valid, but no privileges
372b0e87875SPeter Zijlstra 	 *
373b0e87875SPeter Zijlstra 	 *  0		-- @event is for this PMU and valid
374b0e87875SPeter Zijlstra 	 *
375b0e87875SPeter Zijlstra 	 * Other error return values are allowed.
376b0a873ebSPeter Zijlstra 	 */
377b0a873ebSPeter Zijlstra 	int (*event_init)		(struct perf_event *event);
378b0a873ebSPeter Zijlstra 
3791e0fb9ecSAndy Lutomirski 	/*
3801e0fb9ecSAndy Lutomirski 	 * Notification that the event was mapped or unmapped.  Called
3811e0fb9ecSAndy Lutomirski 	 * in the context of the mapping task.
3821e0fb9ecSAndy Lutomirski 	 */
383bfe33492SPeter Zijlstra 	void (*event_mapped)		(struct perf_event *event, struct mm_struct *mm); /* optional */
384bfe33492SPeter Zijlstra 	void (*event_unmapped)		(struct perf_event *event, struct mm_struct *mm); /* optional */
3851e0fb9ecSAndy Lutomirski 
386b0e87875SPeter Zijlstra 	/*
387b0e87875SPeter Zijlstra 	 * Flags for ->add()/->del()/ ->start()/->stop(). There are
388b0e87875SPeter Zijlstra 	 * matching hw_perf_event::state flags.
389b0e87875SPeter Zijlstra 	 */
390a4eaf7f1SPeter Zijlstra #define PERF_EF_START	0x01		/* start the counter when adding    */
391a4eaf7f1SPeter Zijlstra #define PERF_EF_RELOAD	0x02		/* reload the counter when starting */
392a4eaf7f1SPeter Zijlstra #define PERF_EF_UPDATE	0x04		/* update the counter when stopping */
39318d92bb5SAdrian Hunter #define PERF_EF_PAUSE	0x08		/* AUX area event, pause tracing */
39418d92bb5SAdrian Hunter #define PERF_EF_RESUME	0x10		/* AUX area event, resume tracing */
395a4eaf7f1SPeter Zijlstra 
396a4eaf7f1SPeter Zijlstra 	/*
397b0e87875SPeter Zijlstra 	 * Adds/Removes a counter to/from the PMU, can be done inside a
398b0e87875SPeter Zijlstra 	 * transaction, see the ->*_txn() methods.
399b0e87875SPeter Zijlstra 	 *
400b0e87875SPeter Zijlstra 	 * The add/del callbacks will reserve all hardware resources required
401b0e87875SPeter Zijlstra 	 * to service the event, this includes any counter constraint
402b0e87875SPeter Zijlstra 	 * scheduling etc.
403b0e87875SPeter Zijlstra 	 *
404b0e87875SPeter Zijlstra 	 * Called with IRQs disabled and the PMU disabled on the CPU the event
405b0e87875SPeter Zijlstra 	 * is on.
406b0e87875SPeter Zijlstra 	 *
407b0e87875SPeter Zijlstra 	 * ->add() called without PERF_EF_START should result in the same state
408b0e87875SPeter Zijlstra 	 *  as ->add() followed by ->stop().
409b0e87875SPeter Zijlstra 	 *
410b0e87875SPeter Zijlstra 	 * ->del() must always PERF_EF_UPDATE stop an event. If it calls
411b0e87875SPeter Zijlstra 	 *  ->stop() that must deal with already being stopped without
412b0e87875SPeter Zijlstra 	 *  PERF_EF_UPDATE.
413a4eaf7f1SPeter Zijlstra 	 */
414a4eaf7f1SPeter Zijlstra 	int  (*add)			(struct perf_event *event, int flags);
415a4eaf7f1SPeter Zijlstra 	void (*del)			(struct perf_event *event, int flags);
416a4eaf7f1SPeter Zijlstra 
417a4eaf7f1SPeter Zijlstra 	/*
418b0e87875SPeter Zijlstra 	 * Starts/Stops a counter present on the PMU.
419b0e87875SPeter Zijlstra 	 *
420b0e87875SPeter Zijlstra 	 * The PMI handler should stop the counter when perf_event_overflow()
421b0e87875SPeter Zijlstra 	 * returns !0. ->start() will be used to continue.
422b0e87875SPeter Zijlstra 	 *
423b0e87875SPeter Zijlstra 	 * Also used to change the sample period.
424b0e87875SPeter Zijlstra 	 *
425b0e87875SPeter Zijlstra 	 * Called with IRQs disabled and the PMU disabled on the CPU the event
426b0e87875SPeter Zijlstra 	 * is on -- will be called from NMI context with the PMU generates
427b0e87875SPeter Zijlstra 	 * NMIs.
428b0e87875SPeter Zijlstra 	 *
429b0e87875SPeter Zijlstra 	 * ->stop() with PERF_EF_UPDATE will read the counter and update
430b0e87875SPeter Zijlstra 	 *  period/count values like ->read() would.
431b0e87875SPeter Zijlstra 	 *
432c2127e14SRandy Dunlap 	 * ->start() with PERF_EF_RELOAD will reprogram the counter
433b0e87875SPeter Zijlstra 	 *  value, must be preceded by a ->stop() with PERF_EF_UPDATE.
43418d92bb5SAdrian Hunter 	 *
43518d92bb5SAdrian Hunter 	 * ->stop() with PERF_EF_PAUSE will stop as simply as possible. Will not
43618d92bb5SAdrian Hunter 	 * overlap another ->stop() with PERF_EF_PAUSE nor ->start() with
43718d92bb5SAdrian Hunter 	 * PERF_EF_RESUME.
43818d92bb5SAdrian Hunter 	 *
43918d92bb5SAdrian Hunter 	 * ->start() with PERF_EF_RESUME will start as simply as possible but
44018d92bb5SAdrian Hunter 	 * only if the counter is not otherwise stopped. Will not overlap
44118d92bb5SAdrian Hunter 	 * another ->start() with PERF_EF_RESUME nor ->stop() with
44218d92bb5SAdrian Hunter 	 * PERF_EF_PAUSE.
44318d92bb5SAdrian Hunter 	 *
44418d92bb5SAdrian Hunter 	 * Notably, PERF_EF_PAUSE/PERF_EF_RESUME *can* be concurrent with other
44518d92bb5SAdrian Hunter 	 * ->stop()/->start() invocations, just not itself.
446a4eaf7f1SPeter Zijlstra 	 */
447a4eaf7f1SPeter Zijlstra 	void (*start)			(struct perf_event *event, int flags);
448a4eaf7f1SPeter Zijlstra 	void (*stop)			(struct perf_event *event, int flags);
449a4eaf7f1SPeter Zijlstra 
450a4eaf7f1SPeter Zijlstra 	/*
451a4eaf7f1SPeter Zijlstra 	 * Updates the counter value of the event.
452b0e87875SPeter Zijlstra 	 *
453b0e87875SPeter Zijlstra 	 * For sampling capable PMUs this will also update the software period
454b0e87875SPeter Zijlstra 	 * hw_perf_event::period_left field.
455a4eaf7f1SPeter Zijlstra 	 */
456cdd6c482SIngo Molnar 	void (*read)			(struct perf_event *event);
4576bde9b6cSLin Ming 
4586bde9b6cSLin Ming 	/*
45924cd7f54SPeter Zijlstra 	 * Group events scheduling is treated as a transaction, add
46024cd7f54SPeter Zijlstra 	 * group events as a whole and perform one schedulability test.
46124cd7f54SPeter Zijlstra 	 * If the test fails, roll back the whole group
462a4eaf7f1SPeter Zijlstra 	 *
463a4eaf7f1SPeter Zijlstra 	 * Start the transaction, after this ->add() doesn't need to
46424cd7f54SPeter Zijlstra 	 * do schedulability tests.
465fbbe0701SSukadev Bhattiprolu 	 *
466fbbe0701SSukadev Bhattiprolu 	 * Optional.
4676bde9b6cSLin Ming 	 */
468fbbe0701SSukadev Bhattiprolu 	void (*start_txn)		(struct pmu *pmu, unsigned int txn_flags);
4698d2cacbbSPeter Zijlstra 	/*
470a4eaf7f1SPeter Zijlstra 	 * If ->start_txn() disabled the ->add() schedulability test
4718d2cacbbSPeter Zijlstra 	 * then ->commit_txn() is required to perform one. On success
4728d2cacbbSPeter Zijlstra 	 * the transaction is closed. On error the transaction is kept
4738d2cacbbSPeter Zijlstra 	 * open until ->cancel_txn() is called.
474fbbe0701SSukadev Bhattiprolu 	 *
475fbbe0701SSukadev Bhattiprolu 	 * Optional.
4768d2cacbbSPeter Zijlstra 	 */
477fbbe0701SSukadev Bhattiprolu 	int  (*commit_txn)		(struct pmu *pmu);
4788d2cacbbSPeter Zijlstra 	/*
479a4eaf7f1SPeter Zijlstra 	 * Will cancel the transaction, assumes ->del() is called
48025985edcSLucas De Marchi 	 * for each successful ->add() during the transaction.
481fbbe0701SSukadev Bhattiprolu 	 *
482fbbe0701SSukadev Bhattiprolu 	 * Optional.
4838d2cacbbSPeter Zijlstra 	 */
484fbbe0701SSukadev Bhattiprolu 	void (*cancel_txn)		(struct pmu *pmu);
48535edc2a5SPeter Zijlstra 
48635edc2a5SPeter Zijlstra 	/*
48735edc2a5SPeter Zijlstra 	 * Will return the value for perf_event_mmap_page::index for this event,
488366d259fSAlexandre Ghiti 	 * if no implementation is provided it will default to 0 (see
489366d259fSAlexandre Ghiti 	 * perf_event_idx_default).
49035edc2a5SPeter Zijlstra 	 */
49135edc2a5SPeter Zijlstra 	int (*event_idx)		(struct perf_event *event); /*optional */
492d010b332SStephane Eranian 
493d010b332SStephane Eranian 	/*
494ba532500SYan, Zheng 	 * context-switches callback
495ba532500SYan, Zheng 	 */
496bd275681SPeter Zijlstra 	void (*sched_task)		(struct perf_event_pmu_context *pmu_ctx,
497d57e94f5SKan Liang 					 struct task_struct *task, bool sched_in);
498ba532500SYan, Zheng 
499fc1adfe3SAlexey Budankov 	/*
500217c2a63SKan Liang 	 * Kmem cache of PMU specific data
501217c2a63SKan Liang 	 */
502217c2a63SKan Liang 	struct kmem_cache		*task_ctx_cache;
503217c2a63SKan Liang 
504217c2a63SKan Liang 	/*
50545bfb2e5SPeter Zijlstra 	 * Set up pmu-private data structures for an AUX area
50645bfb2e5SPeter Zijlstra 	 */
50784001866SMathieu Poirier 	void *(*setup_aux)		(struct perf_event *event, void **pages,
50845bfb2e5SPeter Zijlstra 					 int nr_pages, bool overwrite);
50945bfb2e5SPeter Zijlstra 					/* optional */
51045bfb2e5SPeter Zijlstra 
51145bfb2e5SPeter Zijlstra 	/*
51245bfb2e5SPeter Zijlstra 	 * Free pmu-private AUX data structures
51345bfb2e5SPeter Zijlstra 	 */
51445bfb2e5SPeter Zijlstra 	void (*free_aux)		(void *aux); /* optional */
51566eb579eSMark Rutland 
51666eb579eSMark Rutland 	/*
517a4faf00dSAlexander Shishkin 	 * Take a snapshot of the AUX buffer without touching the event
518a4faf00dSAlexander Shishkin 	 * state, so that preempting ->start()/->stop() callbacks does
519a4faf00dSAlexander Shishkin 	 * not interfere with their logic. Called in PMI context.
520a4faf00dSAlexander Shishkin 	 *
521a4faf00dSAlexander Shishkin 	 * Returns the size of AUX data copied to the output handle.
522a4faf00dSAlexander Shishkin 	 *
523a4faf00dSAlexander Shishkin 	 * Optional.
524a4faf00dSAlexander Shishkin 	 */
525a4faf00dSAlexander Shishkin 	long (*snapshot_aux)		(struct perf_event *event,
526a4faf00dSAlexander Shishkin 					 struct perf_output_handle *handle,
527a4faf00dSAlexander Shishkin 					 unsigned long size);
528a4faf00dSAlexander Shishkin 
529a4faf00dSAlexander Shishkin 	/*
530375637bcSAlexander Shishkin 	 * Validate address range filters: make sure the HW supports the
531375637bcSAlexander Shishkin 	 * requested configuration and number of filters; return 0 if the
532375637bcSAlexander Shishkin 	 * supplied filters are valid, -errno otherwise.
533375637bcSAlexander Shishkin 	 *
534375637bcSAlexander Shishkin 	 * Runs in the context of the ioctl()ing process and is not serialized
535375637bcSAlexander Shishkin 	 * with the rest of the PMU callbacks.
536375637bcSAlexander Shishkin 	 */
537375637bcSAlexander Shishkin 	int (*addr_filters_validate)	(struct list_head *filters);
538375637bcSAlexander Shishkin 					/* optional */
539375637bcSAlexander Shishkin 
540375637bcSAlexander Shishkin 	/*
541375637bcSAlexander Shishkin 	 * Synchronize address range filter configuration:
542375637bcSAlexander Shishkin 	 * translate hw-agnostic filters into hardware configuration in
543375637bcSAlexander Shishkin 	 * event::hw::addr_filters.
544375637bcSAlexander Shishkin 	 *
545375637bcSAlexander Shishkin 	 * Runs as a part of filter sync sequence that is done in ->start()
546375637bcSAlexander Shishkin 	 * callback by calling perf_event_addr_filters_sync().
547375637bcSAlexander Shishkin 	 *
548375637bcSAlexander Shishkin 	 * May (and should) traverse event::addr_filters::list, for which its
549375637bcSAlexander Shishkin 	 * caller provides necessary serialization.
550375637bcSAlexander Shishkin 	 */
551375637bcSAlexander Shishkin 	void (*addr_filters_sync)	(struct perf_event *event);
552375637bcSAlexander Shishkin 					/* optional */
553375637bcSAlexander Shishkin 
554375637bcSAlexander Shishkin 	/*
555ab43762eSAlexander Shishkin 	 * Check if event can be used for aux_output purposes for
556ab43762eSAlexander Shishkin 	 * events of this PMU.
557ab43762eSAlexander Shishkin 	 *
558ab43762eSAlexander Shishkin 	 * Runs from perf_event_open(). Should return 0 for "no match"
559ab43762eSAlexander Shishkin 	 * or non-zero for "match".
560ab43762eSAlexander Shishkin 	 */
561ab43762eSAlexander Shishkin 	int (*aux_output_match)		(struct perf_event *event);
562ab43762eSAlexander Shishkin 					/* optional */
563ab43762eSAlexander Shishkin 
564ab43762eSAlexander Shishkin 	/*
565bd275681SPeter Zijlstra 	 * Skip programming this PMU on the given CPU. Typically needed for
566bd275681SPeter Zijlstra 	 * big.LITTLE things.
56766eb579eSMark Rutland 	 */
568bd275681SPeter Zijlstra 	bool (*filter)			(struct pmu *pmu, int cpu); /* optional */
56981ec3f3cSJiri Olsa 
57081ec3f3cSJiri Olsa 	/*
57181ec3f3cSJiri Olsa 	 * Check period value for PERF_EVENT_IOC_PERIOD ioctl.
57281ec3f3cSJiri Olsa 	 */
57381ec3f3cSJiri Olsa 	int (*check_period)		(struct perf_event *event, u64 value); /* optional */
574cdd6c482SIngo Molnar };
575cdd6c482SIngo Molnar 
5766ed70cf3SAlexander Shishkin enum perf_addr_filter_action_t {
5776ed70cf3SAlexander Shishkin 	PERF_ADDR_FILTER_ACTION_STOP = 0,
5786ed70cf3SAlexander Shishkin 	PERF_ADDR_FILTER_ACTION_START,
5796ed70cf3SAlexander Shishkin 	PERF_ADDR_FILTER_ACTION_FILTER,
5806ed70cf3SAlexander Shishkin };
5816ed70cf3SAlexander Shishkin 
582cdd6c482SIngo Molnar /**
583375637bcSAlexander Shishkin  * struct perf_addr_filter - address range filter definition
584375637bcSAlexander Shishkin  * @entry:	event's filter list linkage
5851279e41dSShaokun Zhang  * @path:	object file's path for file-based filters
586375637bcSAlexander Shishkin  * @offset:	filter range offset
5876ed70cf3SAlexander Shishkin  * @size:	filter range size (size==0 means single address trigger)
5886ed70cf3SAlexander Shishkin  * @action:	filter/start/stop
589375637bcSAlexander Shishkin  *
590375637bcSAlexander Shishkin  * This is a hardware-agnostic filter configuration as specified by the user.
591375637bcSAlexander Shishkin  */
592375637bcSAlexander Shishkin struct perf_addr_filter {
593375637bcSAlexander Shishkin 	struct list_head	entry;
5949511bce9SSong Liu 	struct path		path;
595375637bcSAlexander Shishkin 	unsigned long		offset;
596375637bcSAlexander Shishkin 	unsigned long		size;
5976ed70cf3SAlexander Shishkin 	enum perf_addr_filter_action_t	action;
598375637bcSAlexander Shishkin };
599375637bcSAlexander Shishkin 
600375637bcSAlexander Shishkin /**
601375637bcSAlexander Shishkin  * struct perf_addr_filters_head - container for address range filters
602375637bcSAlexander Shishkin  * @list:	list of filters for this event
603375637bcSAlexander Shishkin  * @lock:	spinlock that serializes accesses to the @list and event's
604375637bcSAlexander Shishkin  *		(and its children's) filter generations.
6056ce77bfdSAlexander Shishkin  * @nr_file_filters:	number of file-based filters
606375637bcSAlexander Shishkin  *
607375637bcSAlexander Shishkin  * A child event will use parent's @list (and therefore @lock), so they are
608375637bcSAlexander Shishkin  * bundled together; see perf_event_addr_filters().
609375637bcSAlexander Shishkin  */
610375637bcSAlexander Shishkin struct perf_addr_filters_head {
611375637bcSAlexander Shishkin 	struct list_head	list;
612375637bcSAlexander Shishkin 	raw_spinlock_t		lock;
6136ce77bfdSAlexander Shishkin 	unsigned int		nr_file_filters;
614375637bcSAlexander Shishkin };
615375637bcSAlexander Shishkin 
616c60f83b8SAlexander Shishkin struct perf_addr_filter_range {
617c60f83b8SAlexander Shishkin 	unsigned long		start;
618c60f83b8SAlexander Shishkin 	unsigned long		size;
619c60f83b8SAlexander Shishkin };
620c60f83b8SAlexander Shishkin 
621375637bcSAlexander Shishkin /**
622788faab7STobias Tefke  * enum perf_event_state - the states of an event:
623cdd6c482SIngo Molnar  */
6248ca2bd41SPeter Zijlstra enum perf_event_state {
625a69b0ca4SPeter Zijlstra 	PERF_EVENT_STATE_DEAD		= -4,
626179033b3SJiri Olsa 	PERF_EVENT_STATE_EXIT		= -3,
627cdd6c482SIngo Molnar 	PERF_EVENT_STATE_ERROR		= -2,
628cdd6c482SIngo Molnar 	PERF_EVENT_STATE_OFF		= -1,
629cdd6c482SIngo Molnar 	PERF_EVENT_STATE_INACTIVE	=  0,
630cdd6c482SIngo Molnar 	PERF_EVENT_STATE_ACTIVE		=  1,
631cdd6c482SIngo Molnar };
632cdd6c482SIngo Molnar 
633cdd6c482SIngo Molnar struct file;
634453f19eeSPeter Zijlstra struct perf_sample_data;
635453f19eeSPeter Zijlstra 
636a8b0ca17SPeter Zijlstra typedef void (*perf_overflow_handler_t)(struct perf_event *,
637b326e956SFrederic Weisbecker 					struct perf_sample_data *,
638b326e956SFrederic Weisbecker 					struct pt_regs *regs);
639b326e956SFrederic Weisbecker 
6404ff6a8deSDavid Carrillo-Cisneros /*
6414ff6a8deSDavid Carrillo-Cisneros  * Event capabilities. For event_caps and groups caps.
6424ff6a8deSDavid Carrillo-Cisneros  *
6434ff6a8deSDavid Carrillo-Cisneros  * PERF_EV_CAP_SOFTWARE: Is a software event.
644d6a2f903SDavid Carrillo-Cisneros  * PERF_EV_CAP_READ_ACTIVE_PKG: A CPU event (or cgroup event) that can be read
645d6a2f903SDavid Carrillo-Cisneros  * from any CPU in the package where it is active.
6469f0c4fa1SKan Liang  * PERF_EV_CAP_SIBLING: An event with this flag must be a group sibling and
6479f0c4fa1SKan Liang  * cannot be a group leader. If an event with this flag is detached from the
6489f0c4fa1SKan Liang  * group it is scheduled out and moved into an unrecoverable ERROR state.
649a48a36b3SKan Liang  * PERF_EV_CAP_READ_SCOPE: A CPU event that can be read from any CPU of the
650a48a36b3SKan Liang  * PMU scope where it is active.
6514ff6a8deSDavid Carrillo-Cisneros  */
6524ff6a8deSDavid Carrillo-Cisneros #define PERF_EV_CAP_SOFTWARE		BIT(0)
653d6a2f903SDavid Carrillo-Cisneros #define PERF_EV_CAP_READ_ACTIVE_PKG	BIT(1)
6549f0c4fa1SKan Liang #define PERF_EV_CAP_SIBLING		BIT(2)
655a48a36b3SKan Liang #define PERF_EV_CAP_READ_SCOPE		BIT(3)
656d6f962b5SFrederic Weisbecker 
65776e1d904SFrederic Weisbecker #define SWEVENT_HLIST_BITS		8
65876e1d904SFrederic Weisbecker #define SWEVENT_HLIST_SIZE		(1 << SWEVENT_HLIST_BITS)
65976e1d904SFrederic Weisbecker 
66076e1d904SFrederic Weisbecker struct swevent_hlist {
66176e1d904SFrederic Weisbecker 	struct hlist_head		heads[SWEVENT_HLIST_SIZE];
66276e1d904SFrederic Weisbecker 	struct rcu_head			rcu_head;
66376e1d904SFrederic Weisbecker };
66476e1d904SFrederic Weisbecker 
665c70ca298SPeter Zijlstra #define PERF_ATTACH_CONTEXT	0x0001
666c70ca298SPeter Zijlstra #define PERF_ATTACH_GROUP	0x0002
667c70ca298SPeter Zijlstra #define PERF_ATTACH_TASK	0x0004
668c70ca298SPeter Zijlstra #define PERF_ATTACH_TASK_DATA	0x0008
669506e64e7SKan Liang #define PERF_ATTACH_GLOBAL_DATA	0x0010
670c70ca298SPeter Zijlstra #define PERF_ATTACH_SCHED_CB	0x0020
671c70ca298SPeter Zijlstra #define PERF_ATTACH_CHILD	0x0040
672c70ca298SPeter Zijlstra #define PERF_ATTACH_EXCLUSIVE	0x0080
673c70ca298SPeter Zijlstra #define PERF_ATTACH_CALLCHAIN	0x0100
674506e64e7SKan Liang #define PERF_ATTACH_ITRACE	0x0200
6758a49542cSPeter Zijlstra 
676f7ea534aSJakub Kicinski struct bpf_prog;
677877c6856SLi Zefan struct perf_cgroup;
67856de4e8fSSteven Rostedt (VMware) struct perf_buffer;
67976369139SFrederic Weisbecker 
680f2fb6befSKan Liang struct pmu_event_list {
681f2fb6befSKan Liang 	raw_spinlock_t		lock;
682f2fb6befSKan Liang 	struct list_head	list;
683f2fb6befSKan Liang };
684f2fb6befSKan Liang 
685f3c0eba2SPeter Zijlstra /*
686f3c0eba2SPeter Zijlstra  * event->sibling_list is modified whole holding both ctx->lock and ctx->mutex
687f3c0eba2SPeter Zijlstra  * as such iteration must hold either lock. However, since ctx->lock is an IRQ
688f3c0eba2SPeter Zijlstra  * safe lock, and is only held by the CPU doing the modification, having IRQs
689f3c0eba2SPeter Zijlstra  * disabled is sufficient since it will hold-off the IPIs.
690f3c0eba2SPeter Zijlstra  */
691f3c0eba2SPeter Zijlstra #ifdef CONFIG_PROVE_LOCKING
692f3c0eba2SPeter Zijlstra #define lockdep_assert_event_ctx(event)				\
693f3c0eba2SPeter Zijlstra 	WARN_ON_ONCE(__lockdep_enabled &&			\
6940ce38047SPeter Zijlstra 		     (this_cpu_read(hardirqs_enabled) &&	\
695f3c0eba2SPeter Zijlstra 		      lockdep_is_held(&(event)->ctx->mutex) != LOCK_STATE_HELD))
696f3c0eba2SPeter Zijlstra #else
697f3c0eba2SPeter Zijlstra #define lockdep_assert_event_ctx(event)
698f3c0eba2SPeter Zijlstra #endif
699f3c0eba2SPeter Zijlstra 
700edb39592SPeter Zijlstra #define for_each_sibling_event(sibling, event)			\
701f3c0eba2SPeter Zijlstra 	lockdep_assert_event_ctx(event);			\
702edb39592SPeter Zijlstra 	if ((event)->group_leader == (event))			\
703edb39592SPeter Zijlstra 		list_for_each_entry((sibling), &(event)->sibling_list, sibling_list)
704edb39592SPeter Zijlstra 
705cdd6c482SIngo Molnar /**
706cdd6c482SIngo Molnar  * struct perf_event - performance event kernel representation:
707cdd6c482SIngo Molnar  */
708cdd6c482SIngo Molnar struct perf_event {
709cdd6c482SIngo Molnar #ifdef CONFIG_PERF_EVENTS
7109886167dSPeter Zijlstra 	/*
7119886167dSPeter Zijlstra 	 * entry onto perf_event_context::event_list;
7129886167dSPeter Zijlstra 	 *   modifications require ctx->lock
7139886167dSPeter Zijlstra 	 *   RCU safe iterations.
7149886167dSPeter Zijlstra 	 */
715cdd6c482SIngo Molnar 	struct list_head		event_entry;
7169886167dSPeter Zijlstra 
7179886167dSPeter Zijlstra 	/*
7189886167dSPeter Zijlstra 	 * Locked for modification by both ctx->mutex and ctx->lock; holding
7199886167dSPeter Zijlstra 	 * either sufficies for read.
7209886167dSPeter Zijlstra 	 */
721cdd6c482SIngo Molnar 	struct list_head		sibling_list;
7226668128aSPeter Zijlstra 	struct list_head		active_list;
7238e1a2031SAlexey Budankov 	/*
7248e1a2031SAlexey Budankov 	 * Node on the pinned or flexible tree located at the event context;
7258e1a2031SAlexey Budankov 	 */
7268e1a2031SAlexey Budankov 	struct rb_node			group_node;
7278e1a2031SAlexey Budankov 	u64				group_index;
7289886167dSPeter Zijlstra 	/*
7299886167dSPeter Zijlstra 	 * We need storage to track the entries in perf_pmu_migrate_context; we
7309886167dSPeter Zijlstra 	 * cannot use the event_entry because of RCU and we want to keep the
7319886167dSPeter Zijlstra 	 * group in tact which avoids us using the other two entries.
7329886167dSPeter Zijlstra 	 */
7339886167dSPeter Zijlstra 	struct list_head		migrate_entry;
7349886167dSPeter Zijlstra 
73576e1d904SFrederic Weisbecker 	struct hlist_node		hlist_entry;
73671ad88efSStephane Eranian 	struct list_head		active_entry;
737cdd6c482SIngo Molnar 	int				nr_siblings;
7384ff6a8deSDavid Carrillo-Cisneros 
7394ff6a8deSDavid Carrillo-Cisneros 	/* Not serialized. Only written during event initialization. */
7404ff6a8deSDavid Carrillo-Cisneros 	int				event_caps;
7414ff6a8deSDavid Carrillo-Cisneros 	/* The cumulative AND of all event_caps for events in this group. */
7424ff6a8deSDavid Carrillo-Cisneros 	int				group_caps;
7434ff6a8deSDavid Carrillo-Cisneros 
74432671e37SPeter Zijlstra 	unsigned int			group_generation;
745cdd6c482SIngo Molnar 	struct perf_event		*group_leader;
746bd275681SPeter Zijlstra 	/*
747bd275681SPeter Zijlstra 	 * event->pmu will always point to pmu in which this event belongs.
748bd275681SPeter Zijlstra 	 * Whereas event->pmu_ctx->pmu may point to other pmu when group of
749bd275681SPeter Zijlstra 	 * different pmu events is created.
750bd275681SPeter Zijlstra 	 */
75151b0fe39SPeter Zijlstra 	struct pmu			*pmu;
75254d751d4SThomas Gleixner 	void				*pmu_private;
753cdd6c482SIngo Molnar 
7548ca2bd41SPeter Zijlstra 	enum perf_event_state		state;
7558a49542cSPeter Zijlstra 	unsigned int			attach_state;
756e7850595SPeter Zijlstra 	local64_t			count;
757a6e6dea6SPeter Zijlstra 	atomic64_t			child_count;
758cdd6c482SIngo Molnar 
759cdd6c482SIngo Molnar 	/*
760cdd6c482SIngo Molnar 	 * These are the total time in nanoseconds that the event
761cdd6c482SIngo Molnar 	 * has been enabled (i.e. eligible to run, and the task has
762cdd6c482SIngo Molnar 	 * been scheduled in, if this is a per-task event)
763cdd6c482SIngo Molnar 	 * and running (scheduled onto the CPU), respectively.
764cdd6c482SIngo Molnar 	 */
765cdd6c482SIngo Molnar 	u64				total_time_enabled;
766cdd6c482SIngo Molnar 	u64				total_time_running;
7670d3d73aaSPeter Zijlstra 	u64				tstamp;
768cdd6c482SIngo Molnar 
769cdd6c482SIngo Molnar 	struct perf_event_attr		attr;
770c320c7b7SArnaldo Carvalho de Melo 	u16				header_size;
7716844c09dSArnaldo Carvalho de Melo 	u16				id_header_size;
772c320c7b7SArnaldo Carvalho de Melo 	u16				read_size;
773cdd6c482SIngo Molnar 	struct hw_perf_event		hw;
774cdd6c482SIngo Molnar 
775cdd6c482SIngo Molnar 	struct perf_event_context	*ctx;
776bd275681SPeter Zijlstra 	/*
777bd275681SPeter Zijlstra 	 * event->pmu_ctx points to perf_event_pmu_context in which the event
778bd275681SPeter Zijlstra 	 * is added. This pmu_ctx can be of other pmu for sw event when that
779bd275681SPeter Zijlstra 	 * sw event is part of a group which also contains non-sw events.
780bd275681SPeter Zijlstra 	 */
781bd275681SPeter Zijlstra 	struct perf_event_pmu_context	*pmu_ctx;
782a6fa941dSAl Viro 	atomic_long_t			refcount;
783cdd6c482SIngo Molnar 
784cdd6c482SIngo Molnar 	/*
785cdd6c482SIngo Molnar 	 * These accumulate total time (in nanoseconds) that children
786cdd6c482SIngo Molnar 	 * events have been enabled and running, respectively.
787cdd6c482SIngo Molnar 	 */
788cdd6c482SIngo Molnar 	atomic64_t			child_total_time_enabled;
789cdd6c482SIngo Molnar 	atomic64_t			child_total_time_running;
790cdd6c482SIngo Molnar 
791cdd6c482SIngo Molnar 	/*
792cdd6c482SIngo Molnar 	 * Protect attach/detach and child_list:
793cdd6c482SIngo Molnar 	 */
794cdd6c482SIngo Molnar 	struct mutex			child_mutex;
795cdd6c482SIngo Molnar 	struct list_head		child_list;
796cdd6c482SIngo Molnar 	struct perf_event		*parent;
797cdd6c482SIngo Molnar 
798cdd6c482SIngo Molnar 	int				oncpu;
799cdd6c482SIngo Molnar 	int				cpu;
800cdd6c482SIngo Molnar 
801cdd6c482SIngo Molnar 	struct list_head		owner_entry;
802cdd6c482SIngo Molnar 	struct task_struct		*owner;
803cdd6c482SIngo Molnar 
804cdd6c482SIngo Molnar 	/* mmap bits */
805cdd6c482SIngo Molnar 	struct mutex			mmap_mutex;
806cdd6c482SIngo Molnar 	atomic_t			mmap_count;
80726cb63adSPeter Zijlstra 
80856de4e8fSSteven Rostedt (VMware) 	struct perf_buffer		*rb;
80910c6db11SPeter Zijlstra 	struct list_head		rb_entry;
810b69cf536SPeter Zijlstra 	unsigned long			rcu_batches;
811b69cf536SPeter Zijlstra 	int				rcu_pending;
812cdd6c482SIngo Molnar 
813cdd6c482SIngo Molnar 	/* poll related */
814cdd6c482SIngo Molnar 	wait_queue_head_t		waitq;
815cdd6c482SIngo Molnar 	struct fasync_struct		*fasync;
816cdd6c482SIngo Molnar 
817cdd6c482SIngo Molnar 	/* delayed work for NMIs and such */
818ca6c2132SPeter Zijlstra 	unsigned int			pending_wakeup;
819ca6c2132SPeter Zijlstra 	unsigned int			pending_kill;
820ca6c2132SPeter Zijlstra 	unsigned int			pending_disable;
82197ba62b2SMarco Elver 	unsigned long			pending_addr;	/* SIGTRAP */
822ca6c2132SPeter Zijlstra 	struct irq_work			pending_irq;
8232b84def9SSebastian Andrzej Siewior 	struct irq_work			pending_disable_irq;
824ca6c2132SPeter Zijlstra 	struct callback_head		pending_task;
825ca6c2132SPeter Zijlstra 	unsigned int			pending_work;
826cdd6c482SIngo Molnar 
827cdd6c482SIngo Molnar 	atomic_t			event_limit;
828cdd6c482SIngo Molnar 
829375637bcSAlexander Shishkin 	/* address range filters */
830375637bcSAlexander Shishkin 	struct perf_addr_filters_head	addr_filters;
831375637bcSAlexander Shishkin 	/* vma address array for file-based filders */
832c60f83b8SAlexander Shishkin 	struct perf_addr_filter_range	*addr_filter_ranges;
833375637bcSAlexander Shishkin 	unsigned long			addr_filters_gen;
834375637bcSAlexander Shishkin 
835ab43762eSAlexander Shishkin 	/* for aux_output events */
836ab43762eSAlexander Shishkin 	struct perf_event		*aux_event;
837ab43762eSAlexander Shishkin 
838cdd6c482SIngo Molnar 	void (*destroy)(struct perf_event *);
839cdd6c482SIngo Molnar 	struct rcu_head			rcu_head;
840cdd6c482SIngo Molnar 
841cdd6c482SIngo Molnar 	struct pid_namespace		*ns;
842cdd6c482SIngo Molnar 	u64				id;
8436fb2915dSLi Zefan 
844119a784cSNamhyung Kim 	atomic64_t			lost_samples;
845119a784cSNamhyung Kim 
84634f43927SPeter Zijlstra 	u64				(*clock)(void);
847b326e956SFrederic Weisbecker 	perf_overflow_handler_t		overflow_handler;
8484dc0da86SAvi Kivity 	void				*overflow_handler_context;
849aa6a5f3cSAlexei Starovoitov 	struct bpf_prog			*prog;
85082e6b1eeSAndrii Nakryiko 	u64				bpf_cookie;
851453f19eeSPeter Zijlstra 
85207b139c8SLi Zefan #ifdef CONFIG_EVENT_TRACING
8532425bcb9SSteven Rostedt (Red Hat) 	struct trace_event_call		*tp_event;
8546fb2915dSLi Zefan 	struct event_filter		*filter;
855ced39002SJiri Olsa #ifdef CONFIG_FUNCTION_TRACER
856ced39002SJiri Olsa 	struct ftrace_ops               ftrace_ops;
857ced39002SJiri Olsa #endif
858cdd6c482SIngo Molnar #endif
8596fb2915dSLi Zefan 
860e5d1367fSStephane Eranian #ifdef CONFIG_CGROUP_PERF
861e5d1367fSStephane Eranian 	struct perf_cgroup		*cgrp; /* cgroup event is attach to */
862e5d1367fSStephane Eranian #endif
863e5d1367fSStephane Eranian 
864da97e184SJoel Fernandes (Google) #ifdef CONFIG_SECURITY
865da97e184SJoel Fernandes (Google) 	void *security;
866da97e184SJoel Fernandes (Google) #endif
867f2fb6befSKan Liang 	struct list_head		sb_list;
8680d6d062cSRavi Bangoria 
8690d6d062cSRavi Bangoria 	/*
8700d6d062cSRavi Bangoria 	 * Certain events gets forwarded to another pmu internally by over-
8710d6d062cSRavi Bangoria 	 * writing kernel copy of event->attr.type without user being aware
8720d6d062cSRavi Bangoria 	 * of it. event->orig_type contains original 'type' requested by
8730d6d062cSRavi Bangoria 	 * user.
8740d6d062cSRavi Bangoria 	 */
8750d6d062cSRavi Bangoria 	__u32				orig_type;
8766fb2915dSLi Zefan #endif /* CONFIG_PERF_EVENTS */
877cdd6c482SIngo Molnar };
878cdd6c482SIngo Molnar 
879bd275681SPeter Zijlstra /*
880889c58b3SPeter Zijlstra  *           ,-----------------------[1:n]------------------------.
881bd275681SPeter Zijlstra  *           V                                                    V
882889c58b3SPeter Zijlstra  * perf_event_context <-[1:n]-> perf_event_pmu_context <-[1:n]- perf_event
883889c58b3SPeter Zijlstra  *                                        |                       |
884889c58b3SPeter Zijlstra  *                                        `--[n:1]-> pmu <-[1:n]--'
885bd275681SPeter Zijlstra  *
886bd275681SPeter Zijlstra  *
887bd275681SPeter Zijlstra  * struct perf_event_pmu_context  lifetime is refcount based and RCU freed
888bd275681SPeter Zijlstra  * (similar to perf_event_context). Locking is as if it were a member of
889bd275681SPeter Zijlstra  * perf_event_context; specifically:
890bd275681SPeter Zijlstra  *
891bd275681SPeter Zijlstra  *   modification, both: ctx->mutex && ctx->lock
892bd275681SPeter Zijlstra  *   reading, either:    ctx->mutex || ctx->lock
893bd275681SPeter Zijlstra  *
894bd275681SPeter Zijlstra  * There is one exception to this; namely put_pmu_ctx() isn't always called
895bd275681SPeter Zijlstra  * with ctx->mutex held; this means that as long as we can guarantee the epc
896bd275681SPeter Zijlstra  * has events the above rules hold.
897bd275681SPeter Zijlstra  *
898bd275681SPeter Zijlstra  * Specificially, sys_perf_event_open()'s group_leader case depends on
899bd275681SPeter Zijlstra  * ctx->mutex pinning the configuration. Since we hold a reference on
900bd275681SPeter Zijlstra  * group_leader (through the filedesc) it can't go away, therefore it's
901bd275681SPeter Zijlstra  * associated pmu_ctx must exist and cannot change due to ctx->mutex.
902889c58b3SPeter Zijlstra  *
903889c58b3SPeter Zijlstra  * perf_event holds a refcount on perf_event_context
904889c58b3SPeter Zijlstra  * perf_event holds a refcount on perf_event_pmu_context
905bd275681SPeter Zijlstra  */
906bd275681SPeter Zijlstra struct perf_event_pmu_context {
907bd275681SPeter Zijlstra 	struct pmu			*pmu;
908bd275681SPeter Zijlstra 	struct perf_event_context       *ctx;
909bd275681SPeter Zijlstra 
910bd275681SPeter Zijlstra 	struct list_head		pmu_ctx_entry;
911bd275681SPeter Zijlstra 
912bd275681SPeter Zijlstra 	struct list_head		pinned_active;
913bd275681SPeter Zijlstra 	struct list_head		flexible_active;
914bd275681SPeter Zijlstra 
9154eabf533SPeter Zijlstra 	/* Used to identify the per-cpu perf_event_pmu_context */
916bd275681SPeter Zijlstra 	unsigned int			embedded : 1;
917bd275681SPeter Zijlstra 
918bd275681SPeter Zijlstra 	unsigned int			nr_events;
919f06cc667SPeter Zijlstra 	unsigned int			nr_cgroups;
9200259bf63SNamhyung Kim 	unsigned int			nr_freq;
921bd275681SPeter Zijlstra 
922bd275681SPeter Zijlstra 	atomic_t			refcount; /* event <-> epc */
923bd275681SPeter Zijlstra 	struct rcu_head			rcu_head;
924bd275681SPeter Zijlstra 
925bd275681SPeter Zijlstra 	/*
926bd275681SPeter Zijlstra 	 * Set when one or more (plausibly active) event can't be scheduled
927bd275681SPeter Zijlstra 	 * due to pmu overcommit or pmu constraints, except tolerant to
928bd275681SPeter Zijlstra 	 * events not necessary to be active due to scheduling constraints,
929bd275681SPeter Zijlstra 	 * such as cgroups.
930bd275681SPeter Zijlstra 	 */
931bd275681SPeter Zijlstra 	int				rotate_necessary;
932bd275681SPeter Zijlstra };
9338e1a2031SAlexey Budankov 
perf_pmu_ctx_is_active(struct perf_event_pmu_context * epc)9340259bf63SNamhyung Kim static inline bool perf_pmu_ctx_is_active(struct perf_event_pmu_context *epc)
9350259bf63SNamhyung Kim {
9360259bf63SNamhyung Kim 	return !list_empty(&epc->flexible_active) || !list_empty(&epc->pinned_active);
9370259bf63SNamhyung Kim }
9380259bf63SNamhyung Kim 
9398e1a2031SAlexey Budankov struct perf_event_groups {
9408e1a2031SAlexey Budankov 	struct rb_root	tree;
9418e1a2031SAlexey Budankov 	u64		index;
9428e1a2031SAlexey Budankov };
9438e1a2031SAlexey Budankov 
944bd275681SPeter Zijlstra 
945cdd6c482SIngo Molnar /**
946cdd6c482SIngo Molnar  * struct perf_event_context - event context structure
947cdd6c482SIngo Molnar  *
948cdd6c482SIngo Molnar  * Used as a container for task events and CPU events as well:
949cdd6c482SIngo Molnar  */
950cdd6c482SIngo Molnar struct perf_event_context {
951cdd6c482SIngo Molnar 	/*
952cdd6c482SIngo Molnar 	 * Protect the states of the events in the list,
953cdd6c482SIngo Molnar 	 * nr_active, and the list:
954cdd6c482SIngo Molnar 	 */
955e625cce1SThomas Gleixner 	raw_spinlock_t			lock;
956cdd6c482SIngo Molnar 	/*
957cdd6c482SIngo Molnar 	 * Protect the list of events.  Locking either mutex or lock
958cdd6c482SIngo Molnar 	 * is sufficient to ensure the list doesn't change; to change
959cdd6c482SIngo Molnar 	 * the list you need to lock both the mutex and the spinlock.
960cdd6c482SIngo Molnar 	 */
961cdd6c482SIngo Molnar 	struct mutex			mutex;
962cdd6c482SIngo Molnar 
963bd275681SPeter Zijlstra 	struct list_head		pmu_ctx_list;
9648e1a2031SAlexey Budankov 	struct perf_event_groups	pinned_groups;
9658e1a2031SAlexey Budankov 	struct perf_event_groups	flexible_groups;
966cdd6c482SIngo Molnar 	struct list_head		event_list;
9676668128aSPeter Zijlstra 
968cdd6c482SIngo Molnar 	int				nr_events;
96982ff0c02SRob Herring 	int				nr_user;
970cdd6c482SIngo Molnar 	int				is_active;
971bd275681SPeter Zijlstra 
972cdd6c482SIngo Molnar 	int				nr_stat;
9730f5a2601SPeter Zijlstra 	int				nr_freq;
974dddd3379SThomas Gleixner 	int				rotate_disable;
975bd275681SPeter Zijlstra 
976bd275681SPeter Zijlstra 	refcount_t			refcount; /* event <-> ctx */
977cdd6c482SIngo Molnar 	struct task_struct		*task;
978cdd6c482SIngo Molnar 
979cdd6c482SIngo Molnar 	/*
980cdd6c482SIngo Molnar 	 * Context clock, runs when context enabled.
981cdd6c482SIngo Molnar 	 */
982cdd6c482SIngo Molnar 	u64				time;
983cdd6c482SIngo Molnar 	u64				timestamp;
98409f5e7dcSPeter Zijlstra 	u64				timeoffset;
985cdd6c482SIngo Molnar 
986cdd6c482SIngo Molnar 	/*
987cdd6c482SIngo Molnar 	 * These fields let us detect when two contexts have both
988cdd6c482SIngo Molnar 	 * been cloned (inherited) from a common ancestor.
989cdd6c482SIngo Molnar 	 */
990cdd6c482SIngo Molnar 	struct perf_event_context	*parent_ctx;
991cdd6c482SIngo Molnar 	u64				parent_gen;
992cdd6c482SIngo Molnar 	u64				generation;
993cdd6c482SIngo Molnar 	int				pin_count;
994db4a8356SDavid Carrillo-Cisneros #ifdef CONFIG_CGROUP_PERF
995d010b332SStephane Eranian 	int				nr_cgroups;	 /* cgroup evts */
996db4a8356SDavid Carrillo-Cisneros #endif
99728009ce4SRichard Kennedy 	struct rcu_head			rcu_head;
998ca6c2132SPeter Zijlstra 
999ca6c2132SPeter Zijlstra 	/*
100079bd2330SBen Gainey 	 * The count of events for which using the switch-out fast path
100179bd2330SBen Gainey 	 * should be avoided.
1002ca6c2132SPeter Zijlstra 	 *
10037e8b2556SBen Gainey 	 * Sum (event->pending_work + events with
10047e8b2556SBen Gainey 	 *    (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ)))
1005ca6c2132SPeter Zijlstra 	 *
1006ca6c2132SPeter Zijlstra 	 * The SIGTRAP is targeted at ctx->task, as such it won't do changing
1007ca6c2132SPeter Zijlstra 	 * that until the signal is delivered.
1008ca6c2132SPeter Zijlstra 	 */
100979bd2330SBen Gainey 	local_t				nr_no_switch_fast;
1010cdd6c482SIngo Molnar };
1011cdd6c482SIngo Molnar 
1012cb436912SKan Liang /**
1013cb436912SKan Liang  * struct perf_ctx_data - PMU specific data for a task
1014cb436912SKan Liang  * @rcu_head:  To avoid the race on free PMU specific data
1015cb436912SKan Liang  * @refcount:  To track users
1016cb436912SKan Liang  * @global:    To track system-wide users
1017cb436912SKan Liang  * @ctx_cache: Kmem cache of PMU specific data
1018cb436912SKan Liang  * @data:      PMU specific data
1019cb436912SKan Liang  *
1020cb436912SKan Liang  * Currently, the struct is only used in Intel LBR call stack mode to
1021cb436912SKan Liang  * save/restore the call stack of a task on context switches.
1022cb436912SKan Liang  *
1023cb436912SKan Liang  * The rcu_head is used to prevent the race on free the data.
1024cb436912SKan Liang  * The data only be allocated when Intel LBR call stack mode is enabled.
1025cb436912SKan Liang  * The data will be freed when the mode is disabled.
1026cb436912SKan Liang  * The content of the data will only be accessed in context switch, which
1027cb436912SKan Liang  * should be protected by rcu_read_lock().
1028cb436912SKan Liang  *
1029cb436912SKan Liang  * Because of the alignment requirement of Intel Arch LBR, the Kmem cache
1030cb436912SKan Liang  * is used to allocate the PMU specific data. The ctx_cache is to track
1031cb436912SKan Liang  * the Kmem cache.
1032cb436912SKan Liang  *
1033cb436912SKan Liang  * Careful: Struct perf_ctx_data is added as a pointer in struct task_struct.
1034cb436912SKan Liang  * When system-wide Intel LBR call stack mode is enabled, a buffer with
1035cb436912SKan Liang  * constant size will be allocated for each task.
1036cb436912SKan Liang  * Also, system memory consumption can further grow when the size of
1037cb436912SKan Liang  * struct perf_ctx_data enlarges.
1038cb436912SKan Liang  */
1039cb436912SKan Liang struct perf_ctx_data {
1040cb436912SKan Liang 	struct rcu_head			rcu_head;
1041cb436912SKan Liang 	refcount_t			refcount;
1042cb436912SKan Liang 	int				global;
1043cb436912SKan Liang 	struct kmem_cache		*ctx_cache;
1044cb436912SKan Liang 	void				*data;
1045cb436912SKan Liang };
1046cb436912SKan Liang 
1047bd275681SPeter Zijlstra struct perf_cpu_pmu_context {
1048bd275681SPeter Zijlstra 	struct perf_event_pmu_context	epc;
1049bd275681SPeter Zijlstra 	struct perf_event_pmu_context	*task_epc;
1050bd275681SPeter Zijlstra 
1051bd275681SPeter Zijlstra 	struct list_head		sched_cb_entry;
1052bd275681SPeter Zijlstra 	int				sched_cb_usage;
1053bd275681SPeter Zijlstra 
1054cdd6c482SIngo Molnar 	int				active_oncpu;
1055cdd6c482SIngo Molnar 	int				exclusive;
10564baeb068SPeter Zijlstra 	int				pmu_disable_count;
10574cfafd30SPeter Zijlstra 
10584cfafd30SPeter Zijlstra 	raw_spinlock_t			hrtimer_lock;
10599e630205SStephane Eranian 	struct hrtimer			hrtimer;
10609e630205SStephane Eranian 	ktime_t				hrtimer_interval;
10614cfafd30SPeter Zijlstra 	unsigned int			hrtimer_active;
1062bd275681SPeter Zijlstra };
1063bd275681SPeter Zijlstra 
1064bd275681SPeter Zijlstra /**
1065bd275681SPeter Zijlstra  * struct perf_event_cpu_context - per cpu event context structure
1066bd275681SPeter Zijlstra  */
1067bd275681SPeter Zijlstra struct perf_cpu_context {
1068bd275681SPeter Zijlstra 	struct perf_event_context	ctx;
1069bd275681SPeter Zijlstra 	struct perf_event_context	*task_ctx;
1070bd275681SPeter Zijlstra 	int				online;
10714cfafd30SPeter Zijlstra 
1072db4a8356SDavid Carrillo-Cisneros #ifdef CONFIG_CGROUP_PERF
1073e5d1367fSStephane Eranian 	struct perf_cgroup		*cgrp;
1074db4a8356SDavid Carrillo-Cisneros #endif
1075e48c1788SPeter Zijlstra 
1076836196beSIan Rogers 	/*
1077836196beSIan Rogers 	 * Per-CPU storage for iterators used in visit_groups_merge. The default
1078836196beSIan Rogers 	 * storage is of size 2 to hold the CPU and any CPU event iterators.
1079836196beSIan Rogers 	 */
1080836196beSIan Rogers 	int				heap_size;
1081836196beSIan Rogers 	struct perf_event		**heap;
1082836196beSIan Rogers 	struct perf_event		*heap_default[2];
1083cdd6c482SIngo Molnar };
1084cdd6c482SIngo Molnar 
1085cdd6c482SIngo Molnar struct perf_output_handle {
1086cdd6c482SIngo Molnar 	struct perf_event		*event;
108756de4e8fSSteven Rostedt (VMware) 	struct perf_buffer		*rb;
10886d1acfd5SPeter Zijlstra 	unsigned long			wakeup;
10895d967a8bSPeter Zijlstra 	unsigned long			size;
10908ce939a0SPeter Zijlstra (Intel) 	union {
10918ce939a0SPeter Zijlstra (Intel) 		u64			flags;		/* perf_output*() */
10928ce939a0SPeter Zijlstra (Intel) 		u64			aux_flags;	/* perf_aux_output*() */
10938ce939a0SPeter Zijlstra (Intel) 		struct {
10948ce939a0SPeter Zijlstra (Intel) 			u64		skip_read : 1;
10958ce939a0SPeter Zijlstra (Intel) 		};
10968ce939a0SPeter Zijlstra (Intel) 	};
1097fdc26706SAlexander Shishkin 	union {
10985d967a8bSPeter Zijlstra 		void			*addr;
1099fdc26706SAlexander Shishkin 		unsigned long		head;
1100fdc26706SAlexander Shishkin 	};
11015d967a8bSPeter Zijlstra 	int				page;
1102cdd6c482SIngo Molnar };
1103cdd6c482SIngo Molnar 
11040515e599SAlexei Starovoitov struct bpf_perf_event_data_kern {
1105c895f6f7SHendrik Brueckner 	bpf_user_pt_regs_t *regs;
11060515e599SAlexei Starovoitov 	struct perf_sample_data *data;
11077d9285e8SYonghong Song 	struct perf_event *event;
11080515e599SAlexei Starovoitov };
11090515e599SAlexei Starovoitov 
111039bed6cbSMatt Fleming #ifdef CONFIG_CGROUP_PERF
111139bed6cbSMatt Fleming 
111239bed6cbSMatt Fleming /*
111339bed6cbSMatt Fleming  * perf_cgroup_info keeps track of time_enabled for a cgroup.
111439bed6cbSMatt Fleming  * This is a per-cpu dynamically allocated data structure.
111539bed6cbSMatt Fleming  */
111639bed6cbSMatt Fleming struct perf_cgroup_info {
111739bed6cbSMatt Fleming 	u64				time;
111839bed6cbSMatt Fleming 	u64				timestamp;
111909f5e7dcSPeter Zijlstra 	u64				timeoffset;
112009f5e7dcSPeter Zijlstra 	int				active;
112139bed6cbSMatt Fleming };
112239bed6cbSMatt Fleming 
112339bed6cbSMatt Fleming struct perf_cgroup {
112439bed6cbSMatt Fleming 	struct cgroup_subsys_state	css;
112539bed6cbSMatt Fleming 	struct perf_cgroup_info	__percpu *info;
112639bed6cbSMatt Fleming };
112739bed6cbSMatt Fleming 
112839bed6cbSMatt Fleming /*
112939bed6cbSMatt Fleming  * Must ensure cgroup is pinned (css_get) before calling
113039bed6cbSMatt Fleming  * this function. In other words, we cannot call this function
113139bed6cbSMatt Fleming  * if there is no cgroup event for the current CPU context.
113239bed6cbSMatt Fleming  */
113339bed6cbSMatt Fleming static inline struct perf_cgroup *
perf_cgroup_from_task(struct task_struct * task,struct perf_event_context * ctx)1134614e4c4eSStephane Eranian perf_cgroup_from_task(struct task_struct *task, struct perf_event_context *ctx)
113539bed6cbSMatt Fleming {
1136614e4c4eSStephane Eranian 	return container_of(task_css_check(task, perf_event_cgrp_id,
1137614e4c4eSStephane Eranian 					   ctx ? lockdep_is_held(&ctx->lock)
1138614e4c4eSStephane Eranian 					       : true),
113939bed6cbSMatt Fleming 			    struct perf_cgroup, css);
114039bed6cbSMatt Fleming }
114139bed6cbSMatt Fleming #endif /* CONFIG_CGROUP_PERF */
114239bed6cbSMatt Fleming 
1143cdd6c482SIngo Molnar #ifdef CONFIG_PERF_EVENTS
1144cdd6c482SIngo Molnar 
1145bd275681SPeter Zijlstra extern struct perf_event_context *perf_cpu_task_ctx(void);
1146bd275681SPeter Zijlstra 
1147fdc26706SAlexander Shishkin extern void *perf_aux_output_begin(struct perf_output_handle *handle,
1148fdc26706SAlexander Shishkin 				   struct perf_event *event);
1149fdc26706SAlexander Shishkin extern void perf_aux_output_end(struct perf_output_handle *handle,
1150f4c0b0aaSWill Deacon 				unsigned long size);
1151fdc26706SAlexander Shishkin extern int perf_aux_output_skip(struct perf_output_handle *handle,
1152fdc26706SAlexander Shishkin 				unsigned long size);
1153fdc26706SAlexander Shishkin extern void *perf_get_aux(struct perf_output_handle *handle);
1154f4c0b0aaSWill Deacon extern void perf_aux_output_flag(struct perf_output_handle *handle, u64 flags);
11558d4e6c4cSAlexander Shishkin extern void perf_event_itrace_started(struct perf_event *event);
1156fdc26706SAlexander Shishkin 
115703d8e80bSMischa Jonker extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
1158b0a873ebSPeter Zijlstra extern void perf_pmu_unregister(struct pmu *pmu);
1159cdd6c482SIngo Molnar 
1160ab0cce56SJiri Olsa extern void __perf_event_task_sched_in(struct task_struct *prev,
1161ab0cce56SJiri Olsa 				       struct task_struct *task);
1162ab0cce56SJiri Olsa extern void __perf_event_task_sched_out(struct task_struct *prev,
1163a8d757efSStephane Eranian 					struct task_struct *next);
11642b26f0aaSMarco Elver extern int perf_event_init_task(struct task_struct *child, u64 clone_flags);
1165cdd6c482SIngo Molnar extern void perf_event_exit_task(struct task_struct *child);
1166cdd6c482SIngo Molnar extern void perf_event_free_task(struct task_struct *task);
11674e231c79SPeter Zijlstra extern void perf_event_delayed_put(struct task_struct *task);
1168e03e7ee3SAlexei Starovoitov extern struct file *perf_event_get(unsigned int fd);
1169f8d959a5SYonghong Song extern const struct perf_event *perf_get_event(struct file *file);
1170ffe8690cSKaixu Xia extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event);
1171cdd6c482SIngo Molnar extern void perf_event_print_debug(void);
117233696fc0SPeter Zijlstra extern void perf_pmu_disable(struct pmu *pmu);
117333696fc0SPeter Zijlstra extern void perf_pmu_enable(struct pmu *pmu);
1174ba532500SYan, Zheng extern void perf_sched_cb_dec(struct pmu *pmu);
1175ba532500SYan, Zheng extern void perf_sched_cb_inc(struct pmu *pmu);
1176cdd6c482SIngo Molnar extern int perf_event_task_disable(void);
1177cdd6c482SIngo Molnar extern int perf_event_task_enable(void);
1178c68d224eSStephane Eranian 
1179c68d224eSStephane Eranian extern void perf_pmu_resched(struct pmu *pmu);
1180c68d224eSStephane Eranian 
118126ca5c11SAvi Kivity extern int perf_event_refresh(struct perf_event *event, int refresh);
1182cdd6c482SIngo Molnar extern void perf_event_update_userpage(struct perf_event *event);
1183fb0459d7SArjan van de Ven extern int perf_event_release_kernel(struct perf_event *event);
1184fb0459d7SArjan van de Ven extern struct perf_event *
1185fb0459d7SArjan van de Ven perf_event_create_kernel_counter(struct perf_event_attr *attr,
1186fb0459d7SArjan van de Ven 				int cpu,
118738a81da2SMatt Helsley 				struct task_struct *task,
11884dc0da86SAvi Kivity 				perf_overflow_handler_t callback,
11894dc0da86SAvi Kivity 				void *context);
11900cda4c02SYan, Zheng extern void perf_pmu_migrate_context(struct pmu *pmu,
11910cda4c02SYan, Zheng 				int src_cpu, int dst_cpu);
11927d9285e8SYonghong Song int perf_event_read_local(struct perf_event *event, u64 *value,
11937d9285e8SYonghong Song 			  u64 *enabled, u64 *running);
119459ed446fSPeter Zijlstra extern u64 perf_event_read_value(struct perf_event *event,
119559ed446fSPeter Zijlstra 				 u64 *enabled, u64 *running);
1196cdd6c482SIngo Molnar 
119731046500SNamhyung Kim extern struct perf_callchain_entry *perf_callchain(struct perf_event *event, struct pt_regs *regs);
119831046500SNamhyung Kim 
branch_sample_no_flags(const struct perf_event * event)1199eb55b455SNamhyung Kim static inline bool branch_sample_no_flags(const struct perf_event *event)
1200eb55b455SNamhyung Kim {
1201eb55b455SNamhyung Kim 	return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_NO_FLAGS;
1202eb55b455SNamhyung Kim }
1203eb55b455SNamhyung Kim 
branch_sample_no_cycles(const struct perf_event * event)1204eb55b455SNamhyung Kim static inline bool branch_sample_no_cycles(const struct perf_event *event)
1205eb55b455SNamhyung Kim {
1206eb55b455SNamhyung Kim 	return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_NO_CYCLES;
1207eb55b455SNamhyung Kim }
1208eb55b455SNamhyung Kim 
branch_sample_type(const struct perf_event * event)1209eb55b455SNamhyung Kim static inline bool branch_sample_type(const struct perf_event *event)
1210eb55b455SNamhyung Kim {
1211eb55b455SNamhyung Kim 	return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_TYPE_SAVE;
1212eb55b455SNamhyung Kim }
1213eb55b455SNamhyung Kim 
branch_sample_hw_index(const struct perf_event * event)1214eb55b455SNamhyung Kim static inline bool branch_sample_hw_index(const struct perf_event *event)
1215eb55b455SNamhyung Kim {
1216eb55b455SNamhyung Kim 	return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX;
1217eb55b455SNamhyung Kim }
1218eb55b455SNamhyung Kim 
branch_sample_priv(const struct perf_event * event)1219eb55b455SNamhyung Kim static inline bool branch_sample_priv(const struct perf_event *event)
1220eb55b455SNamhyung Kim {
1221eb55b455SNamhyung Kim 	return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_PRIV_SAVE;
1222eb55b455SNamhyung Kim }
1223eb55b455SNamhyung Kim 
branch_sample_counters(const struct perf_event * event)1224571d91dcSKan Liang static inline bool branch_sample_counters(const struct perf_event *event)
1225571d91dcSKan Liang {
1226571d91dcSKan Liang 	return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS;
1227571d91dcSKan Liang }
1228d010b332SStephane Eranian 
branch_sample_call_stack(const struct perf_event * event)12291f2376cdSKan Liang static inline bool branch_sample_call_stack(const struct perf_event *event)
12301f2376cdSKan Liang {
12311f2376cdSKan Liang 	return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_CALL_STACK;
12321f2376cdSKan Liang }
12331f2376cdSKan Liang 
1234cdd6c482SIngo Molnar struct perf_sample_data {
12352565711fSPeter Zijlstra 	/*
12367bdb1767SNamhyung Kim 	 * Fields set by perf_sample_data_init() unconditionally,
12377bdb1767SNamhyung Kim 	 * group so as to minimize the cachelines touched.
12382565711fSPeter Zijlstra 	 */
12393aac580dSKan Liang 	u64				sample_flags;
12402565711fSPeter Zijlstra 	u64				period;
12414cf7a136SNamhyung Kim 	u64				dyn_size;
1242cdd6c482SIngo Molnar 
12432565711fSPeter Zijlstra 	/*
12447bdb1767SNamhyung Kim 	 * Fields commonly set by __perf_event_header__init_id(),
12457bdb1767SNamhyung Kim 	 * group so as to minimize the cachelines touched.
12462565711fSPeter Zijlstra 	 */
12472565711fSPeter Zijlstra 	u64				type;
1248cdd6c482SIngo Molnar 	struct {
1249cdd6c482SIngo Molnar 		u32	pid;
1250cdd6c482SIngo Molnar 		u32	tid;
1251cdd6c482SIngo Molnar 	}				tid_entry;
1252cdd6c482SIngo Molnar 	u64				time;
1253cdd6c482SIngo Molnar 	u64				id;
1254cdd6c482SIngo Molnar 	struct {
1255cdd6c482SIngo Molnar 		u32	cpu;
1256cdd6c482SIngo Molnar 		u32	reserved;
1257cdd6c482SIngo Molnar 	}				cpu_entry;
12587bdb1767SNamhyung Kim 
12597bdb1767SNamhyung Kim 	/*
12607bdb1767SNamhyung Kim 	 * The other fields, optionally {set,used} by
12617bdb1767SNamhyung Kim 	 * perf_{prepare,output}_sample().
12627bdb1767SNamhyung Kim 	 */
12637bdb1767SNamhyung Kim 	u64				ip;
1264cdd6c482SIngo Molnar 	struct perf_callchain_entry	*callchain;
12657bdb1767SNamhyung Kim 	struct perf_raw_record		*raw;
12667bdb1767SNamhyung Kim 	struct perf_branch_stack	*br_stack;
1267571d91dcSKan Liang 	u64				*br_stack_cntr;
12687bdb1767SNamhyung Kim 	union perf_sample_weight	weight;
12697bdb1767SNamhyung Kim 	union  perf_mem_data_src	data_src;
12707bdb1767SNamhyung Kim 	u64				txn;
127188a7c26aSAndy Lutomirski 
127260e2364eSStephane Eranian 	struct perf_regs		regs_user;
127360e2364eSStephane Eranian 	struct perf_regs		regs_intr;
1274c5ebcedbSJiri Olsa 	u64				stack_user_size;
1275fc7ce9c7SKan Liang 
12767bdb1767SNamhyung Kim 	u64				stream_id;
12776546b19fSNamhyung Kim 	u64				cgroup;
12787bdb1767SNamhyung Kim 	u64				addr;
12797bdb1767SNamhyung Kim 	u64				phys_addr;
12808d97e718SKan Liang 	u64				data_page_size;
1281995f088eSStephane Eranian 	u64				code_page_size;
12827bdb1767SNamhyung Kim 	u64				aux_size;
12832565711fSPeter Zijlstra } ____cacheline_aligned;
1284cdd6c482SIngo Molnar 
1285770eee1fSStephane Eranian /* default value for data source */
1286770eee1fSStephane Eranian #define PERF_MEM_NA (PERF_MEM_S(OP, NA)   |\
1287770eee1fSStephane Eranian 		    PERF_MEM_S(LVL, NA)   |\
1288770eee1fSStephane Eranian 		    PERF_MEM_S(SNOOP, NA) |\
1289770eee1fSStephane Eranian 		    PERF_MEM_S(LOCK, NA)  |\
12905c6e623fSRavi Bangoria 		    PERF_MEM_S(TLB, NA)   |\
12915c6e623fSRavi Bangoria 		    PERF_MEM_S(LVLNUM, NA))
1292770eee1fSStephane Eranian 
perf_sample_data_init(struct perf_sample_data * data,u64 addr,u64 period)1293fd0d000bSRobert Richter static inline void perf_sample_data_init(struct perf_sample_data *data,
1294fd0d000bSRobert Richter 					 u64 addr, u64 period)
1295dc1d628aSPeter Zijlstra {
1296fd0d000bSRobert Richter 	/* remaining struct members initialized in perf_prepare_sample() */
1297838d9bb6SNamhyung Kim 	data->sample_flags = PERF_SAMPLE_PERIOD;
1298fd0d000bSRobert Richter 	data->period = period;
12994cf7a136SNamhyung Kim 	data->dyn_size = 0;
13007b084630SNamhyung Kim 
13017b084630SNamhyung Kim 	if (addr) {
13027b084630SNamhyung Kim 		data->addr = addr;
13037b084630SNamhyung Kim 		data->sample_flags |= PERF_SAMPLE_ADDR;
13047b084630SNamhyung Kim 	}
1305dc1d628aSPeter Zijlstra }
1306dc1d628aSPeter Zijlstra 
perf_sample_save_callchain(struct perf_sample_data * data,struct perf_event * event,struct pt_regs * regs)130731046500SNamhyung Kim static inline void perf_sample_save_callchain(struct perf_sample_data *data,
130831046500SNamhyung Kim 					      struct perf_event *event,
130931046500SNamhyung Kim 					      struct pt_regs *regs)
131031046500SNamhyung Kim {
131131046500SNamhyung Kim 	int size = 1;
131231046500SNamhyung Kim 
1313f226805bSYabin Cui 	if (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN))
1314f226805bSYabin Cui 		return;
1315f226805bSYabin Cui 	if (WARN_ON_ONCE(data->sample_flags & PERF_SAMPLE_CALLCHAIN))
1316f226805bSYabin Cui 		return;
1317f226805bSYabin Cui 
131831046500SNamhyung Kim 	data->callchain = perf_callchain(event, regs);
131931046500SNamhyung Kim 	size += data->callchain->nr;
132031046500SNamhyung Kim 
132131046500SNamhyung Kim 	data->dyn_size += size * sizeof(u64);
132231046500SNamhyung Kim 	data->sample_flags |= PERF_SAMPLE_CALLCHAIN;
132331046500SNamhyung Kim }
132431046500SNamhyung Kim 
perf_sample_save_raw_data(struct perf_sample_data * data,struct perf_event * event,struct perf_raw_record * raw)13250a9081cfSNamhyung Kim static inline void perf_sample_save_raw_data(struct perf_sample_data *data,
1326b9c44b91SYabin Cui 					     struct perf_event *event,
13270a9081cfSNamhyung Kim 					     struct perf_raw_record *raw)
13280a9081cfSNamhyung Kim {
13290a9081cfSNamhyung Kim 	struct perf_raw_frag *frag = &raw->frag;
13300a9081cfSNamhyung Kim 	u32 sum = 0;
13310a9081cfSNamhyung Kim 	int size;
13320a9081cfSNamhyung Kim 
1333b9c44b91SYabin Cui 	if (!(event->attr.sample_type & PERF_SAMPLE_RAW))
1334b9c44b91SYabin Cui 		return;
1335b9c44b91SYabin Cui 	if (WARN_ON_ONCE(data->sample_flags & PERF_SAMPLE_RAW))
1336b9c44b91SYabin Cui 		return;
1337b9c44b91SYabin Cui 
13380a9081cfSNamhyung Kim 	do {
13390a9081cfSNamhyung Kim 		sum += frag->size;
13400a9081cfSNamhyung Kim 		if (perf_raw_frag_last(frag))
13410a9081cfSNamhyung Kim 			break;
13420a9081cfSNamhyung Kim 		frag = frag->next;
13430a9081cfSNamhyung Kim 	} while (1);
13440a9081cfSNamhyung Kim 
13450a9081cfSNamhyung Kim 	size = round_up(sum + sizeof(u32), sizeof(u64));
13460a9081cfSNamhyung Kim 	raw->size = size - sizeof(u32);
13470a9081cfSNamhyung Kim 	frag->pad = raw->size - sum;
13480a9081cfSNamhyung Kim 
13490a9081cfSNamhyung Kim 	data->raw = raw;
13500a9081cfSNamhyung Kim 	data->dyn_size += size;
13510a9081cfSNamhyung Kim 	data->sample_flags |= PERF_SAMPLE_RAW;
13520a9081cfSNamhyung Kim }
13530a9081cfSNamhyung Kim 
has_branch_stack(struct perf_event * event)1354faac6f10SYabin Cui static inline bool has_branch_stack(struct perf_event *event)
1355faac6f10SYabin Cui {
1356faac6f10SYabin Cui 	return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
1357faac6f10SYabin Cui }
1358faac6f10SYabin Cui 
perf_sample_save_brstack(struct perf_sample_data * data,struct perf_event * event,struct perf_branch_stack * brs,u64 * brs_cntr)1359eb55b455SNamhyung Kim static inline void perf_sample_save_brstack(struct perf_sample_data *data,
1360eb55b455SNamhyung Kim 					    struct perf_event *event,
1361571d91dcSKan Liang 					    struct perf_branch_stack *brs,
1362571d91dcSKan Liang 					    u64 *brs_cntr)
1363eb55b455SNamhyung Kim {
1364eb55b455SNamhyung Kim 	int size = sizeof(u64); /* nr */
1365eb55b455SNamhyung Kim 
1366faac6f10SYabin Cui 	if (!has_branch_stack(event))
1367faac6f10SYabin Cui 		return;
1368faac6f10SYabin Cui 	if (WARN_ON_ONCE(data->sample_flags & PERF_SAMPLE_BRANCH_STACK))
1369faac6f10SYabin Cui 		return;
1370faac6f10SYabin Cui 
1371eb55b455SNamhyung Kim 	if (branch_sample_hw_index(event))
1372eb55b455SNamhyung Kim 		size += sizeof(u64);
1373c53e14f1SKan Liang 
1374c53e14f1SKan Liang 	brs->nr = min_t(u16, event->attr.sample_max_stack, brs->nr);
1375c53e14f1SKan Liang 
1376eb55b455SNamhyung Kim 	size += brs->nr * sizeof(struct perf_branch_entry);
1377eb55b455SNamhyung Kim 
1378571d91dcSKan Liang 	/*
1379571d91dcSKan Liang 	 * The extension space for counters is appended after the
1380571d91dcSKan Liang 	 * struct perf_branch_stack. It is used to store the occurrences
1381571d91dcSKan Liang 	 * of events of each branch.
1382571d91dcSKan Liang 	 */
1383571d91dcSKan Liang 	if (brs_cntr)
1384571d91dcSKan Liang 		size += brs->nr * sizeof(u64);
1385571d91dcSKan Liang 
1386eb55b455SNamhyung Kim 	data->br_stack = brs;
1387571d91dcSKan Liang 	data->br_stack_cntr = brs_cntr;
1388eb55b455SNamhyung Kim 	data->dyn_size += size;
1389eb55b455SNamhyung Kim 	data->sample_flags |= PERF_SAMPLE_BRANCH_STACK;
1390eb55b455SNamhyung Kim }
1391eb55b455SNamhyung Kim 
perf_sample_data_size(struct perf_sample_data * data,struct perf_event * event)1392f6e70715SNamhyung Kim static inline u32 perf_sample_data_size(struct perf_sample_data *data,
1393f6e70715SNamhyung Kim 					struct perf_event *event)
1394f6e70715SNamhyung Kim {
1395f6e70715SNamhyung Kim 	u32 size = sizeof(struct perf_event_header);
1396f6e70715SNamhyung Kim 
1397f6e70715SNamhyung Kim 	size += event->header_size + event->id_header_size;
1398f6e70715SNamhyung Kim 	size += data->dyn_size;
1399f6e70715SNamhyung Kim 
1400f6e70715SNamhyung Kim 	return size;
1401f6e70715SNamhyung Kim }
1402f6e70715SNamhyung Kim 
1403bfe4daf8SStephane Eranian /*
1404bfe4daf8SStephane Eranian  * Clear all bitfields in the perf_branch_entry.
1405bfe4daf8SStephane Eranian  * The to and from fields are not cleared because they are
1406bfe4daf8SStephane Eranian  * systematically modified by caller.
1407bfe4daf8SStephane Eranian  */
perf_clear_branch_entry_bitfields(struct perf_branch_entry * br)1408bfe4daf8SStephane Eranian static inline void perf_clear_branch_entry_bitfields(struct perf_branch_entry *br)
1409bfe4daf8SStephane Eranian {
1410bfe4daf8SStephane Eranian 	br->mispred = 0;
1411bfe4daf8SStephane Eranian 	br->predicted = 0;
1412bfe4daf8SStephane Eranian 	br->in_tx = 0;
1413bfe4daf8SStephane Eranian 	br->abort = 0;
1414bfe4daf8SStephane Eranian 	br->cycles = 0;
1415bfe4daf8SStephane Eranian 	br->type = 0;
141693315e46SSandipan Das 	br->spec = PERF_BR_SPEC_NA;
1417bfe4daf8SStephane Eranian 	br->reserved = 0;
1418bfe4daf8SStephane Eranian }
1419bfe4daf8SStephane Eranian 
1420cdd6c482SIngo Molnar extern void perf_output_sample(struct perf_output_handle *handle,
1421cdd6c482SIngo Molnar 			       struct perf_event_header *header,
1422cdd6c482SIngo Molnar 			       struct perf_sample_data *data,
1423cdd6c482SIngo Molnar 			       struct perf_event *event);
1424f6e70715SNamhyung Kim extern void perf_prepare_sample(struct perf_sample_data *data,
1425f6e70715SNamhyung Kim 				struct perf_event *event,
1426f6e70715SNamhyung Kim 				struct pt_regs *regs);
1427f6e70715SNamhyung Kim extern void perf_prepare_header(struct perf_event_header *header,
1428cdd6c482SIngo Molnar 				struct perf_sample_data *data,
1429cdd6c482SIngo Molnar 				struct perf_event *event,
1430cdd6c482SIngo Molnar 				struct pt_regs *regs);
1431cdd6c482SIngo Molnar 
1432a8b0ca17SPeter Zijlstra extern int perf_event_overflow(struct perf_event *event,
1433cdd6c482SIngo Molnar 				 struct perf_sample_data *data,
1434cdd6c482SIngo Molnar 				 struct pt_regs *regs);
1435cdd6c482SIngo Molnar 
14369ecda41aSWang Nan extern void perf_event_output_forward(struct perf_event *event,
14379ecda41aSWang Nan 				     struct perf_sample_data *data,
14389ecda41aSWang Nan 				     struct pt_regs *regs);
14399ecda41aSWang Nan extern void perf_event_output_backward(struct perf_event *event,
14409ecda41aSWang Nan 				       struct perf_sample_data *data,
14419ecda41aSWang Nan 				       struct pt_regs *regs);
144256201969SArnaldo Carvalho de Melo extern int perf_event_output(struct perf_event *event,
144321509084SYan, Zheng 			     struct perf_sample_data *data,
144421509084SYan, Zheng 			     struct pt_regs *regs);
144521509084SYan, Zheng 
14461879445dSWang Nan static inline bool
is_default_overflow_handler(struct perf_event * event)144776f6d588SKyle Huey is_default_overflow_handler(struct perf_event *event)
14481879445dSWang Nan {
144976f6d588SKyle Huey 	perf_overflow_handler_t overflow_handler = event->overflow_handler;
145076f6d588SKyle Huey 
1451d11a6987STomislav Novak 	if (likely(overflow_handler == perf_event_output_forward))
14529ecda41aSWang Nan 		return true;
1453d11a6987STomislav Novak 	if (unlikely(overflow_handler == perf_event_output_backward))
14549ecda41aSWang Nan 		return true;
14559ecda41aSWang Nan 	return false;
14561879445dSWang Nan }
14571879445dSWang Nan 
145821509084SYan, Zheng extern void
145921509084SYan, Zheng perf_event_header__init_id(struct perf_event_header *header,
146021509084SYan, Zheng 			   struct perf_sample_data *data,
146121509084SYan, Zheng 			   struct perf_event *event);
146221509084SYan, Zheng extern void
146321509084SYan, Zheng perf_event__output_id_sample(struct perf_event *event,
146421509084SYan, Zheng 			     struct perf_output_handle *handle,
146521509084SYan, Zheng 			     struct perf_sample_data *sample);
146621509084SYan, Zheng 
1467f38b0dbbSKan Liang extern void
1468f38b0dbbSKan Liang perf_log_lost_samples(struct perf_event *event, u64 lost);
1469f38b0dbbSKan Liang 
event_has_any_exclude_flag(struct perf_event * event)1470486efe9fSAndrew Murray static inline bool event_has_any_exclude_flag(struct perf_event *event)
1471486efe9fSAndrew Murray {
1472486efe9fSAndrew Murray 	struct perf_event_attr *attr = &event->attr;
1473486efe9fSAndrew Murray 
1474486efe9fSAndrew Murray 	return attr->exclude_idle || attr->exclude_user ||
1475486efe9fSAndrew Murray 	       attr->exclude_kernel || attr->exclude_hv ||
1476486efe9fSAndrew Murray 	       attr->exclude_guest || attr->exclude_host;
1477486efe9fSAndrew Murray }
1478486efe9fSAndrew Murray 
is_sampling_event(struct perf_event * event)14796c7e550fSFranck Bui-Huu static inline bool is_sampling_event(struct perf_event *event)
14806c7e550fSFranck Bui-Huu {
14816c7e550fSFranck Bui-Huu 	return event->attr.sample_period != 0;
14826c7e550fSFranck Bui-Huu }
14836c7e550fSFranck Bui-Huu 
1484cdd6c482SIngo Molnar /*
1485cdd6c482SIngo Molnar  * Return 1 for a software event, 0 for a hardware event
1486cdd6c482SIngo Molnar  */
is_software_event(struct perf_event * event)1487cdd6c482SIngo Molnar static inline int is_software_event(struct perf_event *event)
1488cdd6c482SIngo Molnar {
14894ff6a8deSDavid Carrillo-Cisneros 	return event->event_caps & PERF_EV_CAP_SOFTWARE;
1490cdd6c482SIngo Molnar }
1491cdd6c482SIngo Molnar 
1492a1150c20SSong Liu /*
1493a1150c20SSong Liu  * Return 1 for event in sw context, 0 for event in hw context
1494a1150c20SSong Liu  */
in_software_context(struct perf_event * event)1495a1150c20SSong Liu static inline int in_software_context(struct perf_event *event)
1496a1150c20SSong Liu {
1497bd275681SPeter Zijlstra 	return event->pmu_ctx->pmu->task_ctx_nr == perf_sw_context;
1498a1150c20SSong Liu }
1499a1150c20SSong Liu 
is_exclusive_pmu(struct pmu * pmu)15008a58ddaeSAlexander Shishkin static inline int is_exclusive_pmu(struct pmu *pmu)
15018a58ddaeSAlexander Shishkin {
15028a58ddaeSAlexander Shishkin 	return pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE;
15038a58ddaeSAlexander Shishkin }
15048a58ddaeSAlexander Shishkin 
1505c5905afbSIngo Molnar extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
1506cdd6c482SIngo Molnar 
150786038c5eSPeter Zijlstra (Intel) extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
1508a8b0ca17SPeter Zijlstra extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
1509cdd6c482SIngo Molnar 
1510b0f82b81SFrederic Weisbecker #ifndef perf_arch_fetch_caller_regs
perf_arch_fetch_caller_regs(struct pt_regs * regs,unsigned long ip)1511e7e7ee2eSIngo Molnar static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
1512b0f82b81SFrederic Weisbecker #endif
15135331d7b8SFrederic Weisbecker 
15145331d7b8SFrederic Weisbecker /*
1515d15d3568SKairui Song  * When generating a perf sample in-line, instead of from an interrupt /
1516d15d3568SKairui Song  * exception, we lack a pt_regs. This is typically used from software events
1517d15d3568SKairui Song  * like: SW_CONTEXT_SWITCHES, SW_MIGRATIONS and the tie-in with tracepoints.
1518d15d3568SKairui Song  *
1519d15d3568SKairui Song  * We typically don't need a full set, but (for x86) do require:
15205331d7b8SFrederic Weisbecker  * - ip for PERF_SAMPLE_IP
15215331d7b8SFrederic Weisbecker  * - cs for user_mode() tests
1522d15d3568SKairui Song  * - sp for PERF_SAMPLE_CALLCHAIN
1523d15d3568SKairui Song  * - eflags for MISC bits and CALLCHAIN (see: perf_hw_regs())
1524d15d3568SKairui Song  *
1525d15d3568SKairui Song  * NOTE: assumes @regs is otherwise already 0 filled; this is important for
1526d15d3568SKairui Song  * things like PERF_SAMPLE_REGS_INTR.
15275331d7b8SFrederic Weisbecker  */
perf_fetch_caller_regs(struct pt_regs * regs)1528b0f82b81SFrederic Weisbecker static inline void perf_fetch_caller_regs(struct pt_regs *regs)
15295331d7b8SFrederic Weisbecker {
1530b0f82b81SFrederic Weisbecker 	perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
15315331d7b8SFrederic Weisbecker }
15325331d7b8SFrederic Weisbecker 
15337e54a5a0SPeter Zijlstra static __always_inline void
perf_sw_event(u32 event_id,u64 nr,struct pt_regs * regs,u64 addr)1534a8b0ca17SPeter Zijlstra perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
1535e49a5bd3SFrederic Weisbecker {
153686038c5eSPeter Zijlstra (Intel) 	if (static_key_false(&perf_swevent_enabled[event_id]))
1537a8b0ca17SPeter Zijlstra 		__perf_sw_event(event_id, nr, regs, addr);
1538e49a5bd3SFrederic Weisbecker }
153986038c5eSPeter Zijlstra (Intel) 
154086038c5eSPeter Zijlstra (Intel) DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
154186038c5eSPeter Zijlstra (Intel) 
154286038c5eSPeter Zijlstra (Intel) /*
154386038c5eSPeter Zijlstra (Intel)  * 'Special' version for the scheduler, it hard assumes no recursion,
154486038c5eSPeter Zijlstra (Intel)  * which is guaranteed by us not actually scheduling inside other swevents
154586038c5eSPeter Zijlstra (Intel)  * because those disable preemption.
154686038c5eSPeter Zijlstra (Intel)  */
__perf_sw_event_sched(u32 event_id,u64 nr,u64 addr)15477c8056bbSNamhyung Kim static __always_inline void __perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
154886038c5eSPeter Zijlstra (Intel) {
154986038c5eSPeter Zijlstra (Intel) 	struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
155086038c5eSPeter Zijlstra (Intel) 
155186038c5eSPeter Zijlstra (Intel) 	perf_fetch_caller_regs(regs);
155286038c5eSPeter Zijlstra (Intel) 	___perf_sw_event(event_id, nr, regs, addr);
155386038c5eSPeter Zijlstra (Intel) }
1554e49a5bd3SFrederic Weisbecker 
15559107c89eSPeter Zijlstra extern struct static_key_false perf_sched_events;
1556ee6dcfa4SPeter Zijlstra 
__perf_sw_enabled(int swevt)15577c8056bbSNamhyung Kim static __always_inline bool __perf_sw_enabled(int swevt)
1558ff303e66SPeter Zijlstra {
15597c8056bbSNamhyung Kim 	return static_key_false(&perf_swevent_enabled[swevt]);
1560ff303e66SPeter Zijlstra }
1561ff303e66SPeter Zijlstra 
perf_event_task_migrate(struct task_struct * task)1562ff303e66SPeter Zijlstra static inline void perf_event_task_migrate(struct task_struct *task)
1563ff303e66SPeter Zijlstra {
15647c8056bbSNamhyung Kim 	if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS))
1565ff303e66SPeter Zijlstra 		task->sched_migrated = 1;
1566ff303e66SPeter Zijlstra }
1567ff303e66SPeter Zijlstra 
perf_event_task_sched_in(struct task_struct * prev,struct task_struct * task)1568ab0cce56SJiri Olsa static inline void perf_event_task_sched_in(struct task_struct *prev,
1569a8d757efSStephane Eranian 					    struct task_struct *task)
1570ee6dcfa4SPeter Zijlstra {
15719107c89eSPeter Zijlstra 	if (static_branch_unlikely(&perf_sched_events))
1572ab0cce56SJiri Olsa 		__perf_event_task_sched_in(prev, task);
1573ff303e66SPeter Zijlstra 
15747c8056bbSNamhyung Kim 	if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS) &&
15757c8056bbSNamhyung Kim 	    task->sched_migrated) {
15767c8056bbSNamhyung Kim 		__perf_sw_event_sched(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 0);
1577ff303e66SPeter Zijlstra 		task->sched_migrated = 0;
1578ff303e66SPeter Zijlstra 	}
1579ab0cce56SJiri Olsa }
1580ab0cce56SJiri Olsa 
perf_event_task_sched_out(struct task_struct * prev,struct task_struct * next)1581ab0cce56SJiri Olsa static inline void perf_event_task_sched_out(struct task_struct *prev,
1582ab0cce56SJiri Olsa 					     struct task_struct *next)
1583ab0cce56SJiri Olsa {
15847c8056bbSNamhyung Kim 	if (__perf_sw_enabled(PERF_COUNT_SW_CONTEXT_SWITCHES))
15857c8056bbSNamhyung Kim 		__perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
1586ee6dcfa4SPeter Zijlstra 
1587d0d1dd62SNamhyung Kim #ifdef CONFIG_CGROUP_PERF
1588d0d1dd62SNamhyung Kim 	if (__perf_sw_enabled(PERF_COUNT_SW_CGROUP_SWITCHES) &&
1589d0d1dd62SNamhyung Kim 	    perf_cgroup_from_task(prev, NULL) !=
1590d0d1dd62SNamhyung Kim 	    perf_cgroup_from_task(next, NULL))
1591d0d1dd62SNamhyung Kim 		__perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);
1592d0d1dd62SNamhyung Kim #endif
1593d0d1dd62SNamhyung Kim 
15949107c89eSPeter Zijlstra 	if (static_branch_unlikely(&perf_sched_events))
1595ab0cce56SJiri Olsa 		__perf_event_task_sched_out(prev, next);
1596ee6dcfa4SPeter Zijlstra }
1597ee6dcfa4SPeter Zijlstra 
15983af9e859SEric B Munson extern void perf_event_mmap(struct vm_area_struct *vma);
159976193a94SSong Liu 
160076193a94SSong Liu extern void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
160176193a94SSong Liu 			       bool unregister, const char *sym);
16026ee52e2aSSong Liu extern void perf_event_bpf_event(struct bpf_prog *prog,
16036ee52e2aSSong Liu 				 enum perf_bpf_event_type type,
16046ee52e2aSSong Liu 				 u16 flags);
160576193a94SSong Liu 
16062aef6f30SSean Christopherson #ifdef CONFIG_GUEST_PERF_EVENTS
1607ff083a2dSSean Christopherson extern struct perf_guest_info_callbacks __rcu *perf_guest_cbs;
160887b940a0SSean Christopherson 
160987b940a0SSean Christopherson DECLARE_STATIC_CALL(__perf_guest_state, *perf_guest_cbs->state);
161087b940a0SSean Christopherson DECLARE_STATIC_CALL(__perf_guest_get_ip, *perf_guest_cbs->get_ip);
161187b940a0SSean Christopherson DECLARE_STATIC_CALL(__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr);
161287b940a0SSean Christopherson 
perf_guest_state(void)16131c343051SSean Christopherson static inline unsigned int perf_guest_state(void)
16141c343051SSean Christopherson {
161587b940a0SSean Christopherson 	return static_call(__perf_guest_state)();
16161c343051SSean Christopherson }
perf_guest_get_ip(void)16171c343051SSean Christopherson static inline unsigned long perf_guest_get_ip(void)
16181c343051SSean Christopherson {
161987b940a0SSean Christopherson 	return static_call(__perf_guest_get_ip)();
16201c343051SSean Christopherson }
perf_guest_handle_intel_pt_intr(void)16211c343051SSean Christopherson static inline unsigned int perf_guest_handle_intel_pt_intr(void)
16221c343051SSean Christopherson {
162387b940a0SSean Christopherson 	return static_call(__perf_guest_handle_intel_pt_intr)();
16241c343051SSean Christopherson }
16252934e3d0SSean Christopherson extern void perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs);
16262934e3d0SSean Christopherson extern void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs);
16272aef6f30SSean Christopherson #else
perf_guest_state(void)16282aef6f30SSean Christopherson static inline unsigned int perf_guest_state(void)		 { return 0; }
perf_guest_get_ip(void)16292aef6f30SSean Christopherson static inline unsigned long perf_guest_get_ip(void)		 { return 0; }
perf_guest_handle_intel_pt_intr(void)16302aef6f30SSean Christopherson static inline unsigned int perf_guest_handle_intel_pt_intr(void) { return 0; }
16312aef6f30SSean Christopherson #endif /* CONFIG_GUEST_PERF_EVENTS */
163239447b38SZhang, Yanmin 
1633e041e328SPeter Zijlstra extern void perf_event_exec(void);
163482b89778SAdrian Hunter extern void perf_event_comm(struct task_struct *tsk, bool exec);
1635e4222673SHari Bathini extern void perf_event_namespaces(struct task_struct *tsk);
1636cdd6c482SIngo Molnar extern void perf_event_fork(struct task_struct *tsk);
1637e17d43b9SAdrian Hunter extern void perf_event_text_poke(const void *addr,
1638e17d43b9SAdrian Hunter 				 const void *old_bytes, size_t old_len,
1639e17d43b9SAdrian Hunter 				 const void *new_bytes, size_t new_len);
1640cdd6c482SIngo Molnar 
164156962b44SFrederic Weisbecker /* Callchains */
164256962b44SFrederic Weisbecker DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
164356962b44SFrederic Weisbecker 
1644cfbcf468SArnaldo Carvalho de Melo extern void perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs);
1645cfbcf468SArnaldo Carvalho de Melo extern void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs);
1646568b329aSAlexei Starovoitov extern struct perf_callchain_entry *
1647568b329aSAlexei Starovoitov get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
1648cfbcf468SArnaldo Carvalho de Melo 		   u32 max_stack, bool crosstask, bool add_mark);
164997c79a38SArnaldo Carvalho de Melo extern int get_callchain_buffers(int max_stack);
1650568b329aSAlexei Starovoitov extern void put_callchain_buffers(void);
1651d141b8bcSSong Liu extern struct perf_callchain_entry *get_callchain_entry(int *rctx);
1652d141b8bcSSong Liu extern void put_callchain_entry(int rctx);
165356962b44SFrederic Weisbecker 
1654c5dfd78eSArnaldo Carvalho de Melo extern int sysctl_perf_event_max_stack;
1655c85b0334SArnaldo Carvalho de Melo extern int sysctl_perf_event_max_contexts_per_stack;
1656c5dfd78eSArnaldo Carvalho de Melo 
perf_callchain_store_context(struct perf_callchain_entry_ctx * ctx,u64 ip)1657c85b0334SArnaldo Carvalho de Melo static inline int perf_callchain_store_context(struct perf_callchain_entry_ctx *ctx, u64 ip)
1658c85b0334SArnaldo Carvalho de Melo {
1659c85b0334SArnaldo Carvalho de Melo 	if (ctx->contexts < sysctl_perf_event_max_contexts_per_stack) {
1660c85b0334SArnaldo Carvalho de Melo 		struct perf_callchain_entry *entry = ctx->entry;
1661c85b0334SArnaldo Carvalho de Melo 		entry->ip[entry->nr++] = ip;
1662c85b0334SArnaldo Carvalho de Melo 		++ctx->contexts;
1663c85b0334SArnaldo Carvalho de Melo 		return 0;
1664c85b0334SArnaldo Carvalho de Melo 	} else {
1665c85b0334SArnaldo Carvalho de Melo 		ctx->contexts_maxed = true;
1666c85b0334SArnaldo Carvalho de Melo 		return -1; /* no more room, stop walking the stack */
1667c85b0334SArnaldo Carvalho de Melo 	}
1668c85b0334SArnaldo Carvalho de Melo }
16693e4de4ecSArnaldo Carvalho de Melo 
perf_callchain_store(struct perf_callchain_entry_ctx * ctx,u64 ip)1670cfbcf468SArnaldo Carvalho de Melo static inline int perf_callchain_store(struct perf_callchain_entry_ctx *ctx, u64 ip)
167170791ce9SFrederic Weisbecker {
1672c85b0334SArnaldo Carvalho de Melo 	if (ctx->nr < ctx->max_stack && !ctx->contexts_maxed) {
1673cfbcf468SArnaldo Carvalho de Melo 		struct perf_callchain_entry *entry = ctx->entry;
167470791ce9SFrederic Weisbecker 		entry->ip[entry->nr++] = ip;
16753b1fff08SArnaldo Carvalho de Melo 		++ctx->nr;
1676568b329aSAlexei Starovoitov 		return 0;
1677568b329aSAlexei Starovoitov 	} else {
1678568b329aSAlexei Starovoitov 		return -1; /* no more room, stop walking the stack */
1679568b329aSAlexei Starovoitov 	}
168070791ce9SFrederic Weisbecker }
1681cdd6c482SIngo Molnar 
1682cdd6c482SIngo Molnar extern int sysctl_perf_event_paranoid;
1683cdd6c482SIngo Molnar extern int sysctl_perf_event_sample_rate;
168414c63f17SDave Hansen 
168514c63f17SDave Hansen extern void perf_sample_event_took(u64 sample_len_ns);
1686cdd6c482SIngo Molnar 
1687da97e184SJoel Fernandes (Google) /* Access to perf_event_open(2) syscall. */
1688da97e184SJoel Fernandes (Google) #define PERF_SECURITY_OPEN		0
1689da97e184SJoel Fernandes (Google) 
1690da97e184SJoel Fernandes (Google) /* Finer grained perf_event_open(2) access control. */
1691da97e184SJoel Fernandes (Google) #define PERF_SECURITY_CPU		1
1692da97e184SJoel Fernandes (Google) #define PERF_SECURITY_KERNEL		2
1693da97e184SJoel Fernandes (Google) #define PERF_SECURITY_TRACEPOINT	3
1694da97e184SJoel Fernandes (Google) 
perf_is_paranoid(void)1695da97e184SJoel Fernandes (Google) static inline int perf_is_paranoid(void)
1696320ebf09SPeter Zijlstra {
1697320ebf09SPeter Zijlstra 	return sysctl_perf_event_paranoid > -1;
1698320ebf09SPeter Zijlstra }
1699320ebf09SPeter Zijlstra 
17009ec84f79SLuo Gengkun int perf_allow_kernel(void);
1701320ebf09SPeter Zijlstra 
perf_allow_cpu(void)17029ec84f79SLuo Gengkun static inline int perf_allow_cpu(void)
1703320ebf09SPeter Zijlstra {
170418aa1856SAlexey Budankov 	if (sysctl_perf_event_paranoid > 0 && !perfmon_capable())
1705da97e184SJoel Fernandes (Google) 		return -EACCES;
1706da97e184SJoel Fernandes (Google) 
17079ec84f79SLuo Gengkun 	return security_perf_event_open(PERF_SECURITY_CPU);
1708da97e184SJoel Fernandes (Google) }
1709da97e184SJoel Fernandes (Google) 
perf_allow_tracepoint(void)17109ec84f79SLuo Gengkun static inline int perf_allow_tracepoint(void)
1711da97e184SJoel Fernandes (Google) {
171218aa1856SAlexey Budankov 	if (sysctl_perf_event_paranoid > -1 && !perfmon_capable())
1713da97e184SJoel Fernandes (Google) 		return -EPERM;
1714da97e184SJoel Fernandes (Google) 
17159ec84f79SLuo Gengkun 	return security_perf_event_open(PERF_SECURITY_TRACEPOINT);
1716320ebf09SPeter Zijlstra }
1717320ebf09SPeter Zijlstra 
17186057b90eSNamhyung Kim extern int perf_exclude_event(struct perf_event *event, struct pt_regs *regs);
17196057b90eSNamhyung Kim 
1720cdd6c482SIngo Molnar extern void perf_event_init(void);
17211e1dcd93SAlexei Starovoitov extern void perf_tp_event(u16 event_type, u64 count, void *record,
17221c024ecaSPeter Zijlstra 			  int entry_size, struct pt_regs *regs,
1723e6dab5ffSAndrew Vagin 			  struct hlist_head *head, int rctx,
17248fd0fbbeSPeter Zijlstra 			  struct task_struct *task);
172524f1e32cSFrederic Weisbecker extern void perf_bp_event(struct perf_event *event, void *data);
1726cdd6c482SIngo Molnar 
17272c47e7a7SColton Lewis extern unsigned long perf_misc_flags(struct perf_event *event, struct pt_regs *regs);
17282c47e7a7SColton Lewis extern unsigned long perf_instruction_pointer(struct perf_event *event,
17292c47e7a7SColton Lewis 					      struct pt_regs *regs);
173004782e63SColton Lewis 
173104782e63SColton Lewis #ifndef perf_arch_misc_flags
173204782e63SColton Lewis # define perf_arch_misc_flags(regs) \
1733e7e7ee2eSIngo Molnar 		(user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
173404782e63SColton Lewis # define perf_arch_instruction_pointer(regs)	instruction_pointer(regs)
1735cdd6c482SIngo Molnar #endif
1736c895f6f7SHendrik Brueckner #ifndef perf_arch_bpf_user_pt_regs
1737c895f6f7SHendrik Brueckner # define perf_arch_bpf_user_pt_regs(regs) regs
1738c895f6f7SHendrik Brueckner #endif
1739cdd6c482SIngo Molnar 
17402c47e7a7SColton Lewis #ifndef perf_arch_guest_misc_flags
perf_arch_guest_misc_flags(struct pt_regs * regs)17412c47e7a7SColton Lewis static inline unsigned long perf_arch_guest_misc_flags(struct pt_regs *regs)
17422c47e7a7SColton Lewis {
17432c47e7a7SColton Lewis 	unsigned long guest_state = perf_guest_state();
17442c47e7a7SColton Lewis 
17452c47e7a7SColton Lewis 	if (!(guest_state & PERF_GUEST_ACTIVE))
17462c47e7a7SColton Lewis 		return 0;
17472c47e7a7SColton Lewis 
17482c47e7a7SColton Lewis 	if (guest_state & PERF_GUEST_USER)
17492c47e7a7SColton Lewis 		return PERF_RECORD_MISC_GUEST_USER;
17502c47e7a7SColton Lewis 	else
17512c47e7a7SColton Lewis 		return PERF_RECORD_MISC_GUEST_KERNEL;
17522c47e7a7SColton Lewis }
17532c47e7a7SColton Lewis # define perf_arch_guest_misc_flags(regs)	perf_arch_guest_misc_flags(regs)
17542c47e7a7SColton Lewis #endif
17552c47e7a7SColton Lewis 
needs_branch_stack(struct perf_event * event)1756a46a2300SYan, Zheng static inline bool needs_branch_stack(struct perf_event *event)
1757a46a2300SYan, Zheng {
1758a46a2300SYan, Zheng 	return event->attr.branch_sample_type != 0;
1759a46a2300SYan, Zheng }
1760a46a2300SYan, Zheng 
has_aux(struct perf_event * event)176145bfb2e5SPeter Zijlstra static inline bool has_aux(struct perf_event *event)
176245bfb2e5SPeter Zijlstra {
176345bfb2e5SPeter Zijlstra 	return event->pmu->setup_aux;
176445bfb2e5SPeter Zijlstra }
176545bfb2e5SPeter Zijlstra 
has_aux_action(struct perf_event * event)176618d92bb5SAdrian Hunter static inline bool has_aux_action(struct perf_event *event)
176718d92bb5SAdrian Hunter {
176818d92bb5SAdrian Hunter 	return event->attr.aux_sample_size ||
176918d92bb5SAdrian Hunter 	       event->attr.aux_pause ||
177018d92bb5SAdrian Hunter 	       event->attr.aux_resume;
177118d92bb5SAdrian Hunter }
177218d92bb5SAdrian Hunter 
is_write_backward(struct perf_event * event)17739ecda41aSWang Nan static inline bool is_write_backward(struct perf_event *event)
17749ecda41aSWang Nan {
17759ecda41aSWang Nan 	return !!event->attr.write_backward;
17769ecda41aSWang Nan }
17779ecda41aSWang Nan 
has_addr_filter(struct perf_event * event)1778375637bcSAlexander Shishkin static inline bool has_addr_filter(struct perf_event *event)
1779375637bcSAlexander Shishkin {
1780375637bcSAlexander Shishkin 	return event->pmu->nr_addr_filters;
1781375637bcSAlexander Shishkin }
1782375637bcSAlexander Shishkin 
1783375637bcSAlexander Shishkin /*
1784375637bcSAlexander Shishkin  * An inherited event uses parent's filters
1785375637bcSAlexander Shishkin  */
1786375637bcSAlexander Shishkin static inline struct perf_addr_filters_head *
perf_event_addr_filters(struct perf_event * event)1787375637bcSAlexander Shishkin perf_event_addr_filters(struct perf_event *event)
1788375637bcSAlexander Shishkin {
1789375637bcSAlexander Shishkin 	struct perf_addr_filters_head *ifh = &event->addr_filters;
1790375637bcSAlexander Shishkin 
1791375637bcSAlexander Shishkin 	if (event->parent)
1792375637bcSAlexander Shishkin 		ifh = &event->parent->addr_filters;
1793375637bcSAlexander Shishkin 
1794375637bcSAlexander Shishkin 	return ifh;
1795375637bcSAlexander Shishkin }
1796375637bcSAlexander Shishkin 
perf_event_fasync(struct perf_event * event)17974a013980SKyle Huey static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
17984a013980SKyle Huey {
17994a013980SKyle Huey 	/* Only the parent has fasync state */
18004a013980SKyle Huey 	if (event->parent)
18014a013980SKyle Huey 		event = event->parent;
18024a013980SKyle Huey 	return &event->fasync;
18034a013980SKyle Huey }
18044a013980SKyle Huey 
1805375637bcSAlexander Shishkin extern void perf_event_addr_filters_sync(struct perf_event *event);
18068b8ff8ccSAdrian Hunter extern void perf_report_aux_output_id(struct perf_event *event, u64 hw_id);
1807375637bcSAlexander Shishkin 
1808cdd6c482SIngo Molnar extern int perf_output_begin(struct perf_output_handle *handle,
1809267fb273SPeter Zijlstra 			     struct perf_sample_data *data,
1810a7ac67eaSPeter Zijlstra 			     struct perf_event *event, unsigned int size);
18119ecda41aSWang Nan extern int perf_output_begin_forward(struct perf_output_handle *handle,
1812267fb273SPeter Zijlstra 				     struct perf_sample_data *data,
18139ecda41aSWang Nan 				     struct perf_event *event,
18149ecda41aSWang Nan 				     unsigned int size);
18159ecda41aSWang Nan extern int perf_output_begin_backward(struct perf_output_handle *handle,
1816267fb273SPeter Zijlstra 				      struct perf_sample_data *data,
18179ecda41aSWang Nan 				      struct perf_event *event,
18189ecda41aSWang Nan 				      unsigned int size);
18199ecda41aSWang Nan 
1820cdd6c482SIngo Molnar extern void perf_output_end(struct perf_output_handle *handle);
182191d7753aSFrederic Weisbecker extern unsigned int perf_output_copy(struct perf_output_handle *handle,
1822cdd6c482SIngo Molnar 			     const void *buf, unsigned int len);
18235685e0ffSJiri Olsa extern unsigned int perf_output_skip(struct perf_output_handle *handle,
18245685e0ffSJiri Olsa 				     unsigned int len);
1825a4faf00dSAlexander Shishkin extern long perf_output_copy_aux(struct perf_output_handle *aux_handle,
1826a4faf00dSAlexander Shishkin 				 struct perf_output_handle *handle,
1827a4faf00dSAlexander Shishkin 				 unsigned long from, unsigned long to);
18284ed7c92dSPeter Zijlstra extern int perf_swevent_get_recursion_context(void);
18294ed7c92dSPeter Zijlstra extern void perf_swevent_put_recursion_context(int rctx);
1830ab573844SJiri Olsa extern u64 perf_swevent_set_period(struct perf_event *event);
183144234adcSFrederic Weisbecker extern void perf_event_enable(struct perf_event *event);
183244234adcSFrederic Weisbecker extern void perf_event_disable(struct perf_event *event);
1833fae3fde6SPeter Zijlstra extern void perf_event_disable_local(struct perf_event *event);
18345aab90ceSJiri Olsa extern void perf_event_disable_inatomic(struct perf_event *event);
1835e9d2b064SPeter Zijlstra extern void perf_event_task_tick(void);
1836475113d9SJiri Olsa extern int perf_event_account_interrupt(struct perf_event *event);
18373ca270fcSLike Xu extern int perf_event_period(struct perf_event *event, u64 value);
183852ba4b0bSLike Xu extern u64 perf_event_pause(struct perf_event *event, bool reset);
1839e041e328SPeter Zijlstra #else /* !CONFIG_PERF_EVENTS: */
1840fdc26706SAlexander Shishkin static inline void *
perf_aux_output_begin(struct perf_output_handle * handle,struct perf_event * event)1841fdc26706SAlexander Shishkin perf_aux_output_begin(struct perf_output_handle *handle,
1842fdc26706SAlexander Shishkin 		      struct perf_event *event)				{ return NULL; }
1843fdc26706SAlexander Shishkin static inline void
perf_aux_output_end(struct perf_output_handle * handle,unsigned long size)1844f4c0b0aaSWill Deacon perf_aux_output_end(struct perf_output_handle *handle, unsigned long size)
1845f4c0b0aaSWill Deacon 									{ }
1846fdc26706SAlexander Shishkin static inline int
perf_aux_output_skip(struct perf_output_handle * handle,unsigned long size)1847fdc26706SAlexander Shishkin perf_aux_output_skip(struct perf_output_handle *handle,
1848fdc26706SAlexander Shishkin 		     unsigned long size)				{ return -EINVAL; }
1849fdc26706SAlexander Shishkin static inline void *
perf_get_aux(struct perf_output_handle * handle)1850fdc26706SAlexander Shishkin perf_get_aux(struct perf_output_handle *handle)				{ return NULL; }
1851cdd6c482SIngo Molnar static inline void
perf_event_task_migrate(struct task_struct * task)1852ff303e66SPeter Zijlstra perf_event_task_migrate(struct task_struct *task)			{ }
1853ff303e66SPeter Zijlstra static inline void
perf_event_task_sched_in(struct task_struct * prev,struct task_struct * task)1854ab0cce56SJiri Olsa perf_event_task_sched_in(struct task_struct *prev,
1855a8d757efSStephane Eranian 			 struct task_struct *task)			{ }
1856ab0cce56SJiri Olsa static inline void
perf_event_task_sched_out(struct task_struct * prev,struct task_struct * next)1857ab0cce56SJiri Olsa perf_event_task_sched_out(struct task_struct *prev,
1858ab0cce56SJiri Olsa 			  struct task_struct *next)			{ }
perf_event_init_task(struct task_struct * child,u64 clone_flags)18592b26f0aaSMarco Elver static inline int perf_event_init_task(struct task_struct *child,
18602b26f0aaSMarco Elver 				       u64 clone_flags)			{ return 0; }
perf_event_exit_task(struct task_struct * child)1861cdd6c482SIngo Molnar static inline void perf_event_exit_task(struct task_struct *child)	{ }
perf_event_free_task(struct task_struct * task)1862cdd6c482SIngo Molnar static inline void perf_event_free_task(struct task_struct *task)	{ }
perf_event_delayed_put(struct task_struct * task)18634e231c79SPeter Zijlstra static inline void perf_event_delayed_put(struct task_struct *task)	{ }
perf_event_get(unsigned int fd)1864e03e7ee3SAlexei Starovoitov static inline struct file *perf_event_get(unsigned int fd)	{ return ERR_PTR(-EINVAL); }
perf_get_event(struct file * file)1865f8d959a5SYonghong Song static inline const struct perf_event *perf_get_event(struct file *file)
1866f8d959a5SYonghong Song {
1867f8d959a5SYonghong Song 	return ERR_PTR(-EINVAL);
1868f8d959a5SYonghong Song }
perf_event_attrs(struct perf_event * event)1869ffe8690cSKaixu Xia static inline const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
1870ffe8690cSKaixu Xia {
1871ffe8690cSKaixu Xia 	return ERR_PTR(-EINVAL);
1872ffe8690cSKaixu Xia }
perf_event_read_local(struct perf_event * event,u64 * value,u64 * enabled,u64 * running)18737d9285e8SYonghong Song static inline int perf_event_read_local(struct perf_event *event, u64 *value,
18747d9285e8SYonghong Song 					u64 *enabled, u64 *running)
1875f91840a3SAlexei Starovoitov {
1876f91840a3SAlexei Starovoitov 	return -EINVAL;
1877f91840a3SAlexei Starovoitov }
perf_event_print_debug(void)1878cdd6c482SIngo Molnar static inline void perf_event_print_debug(void)				{ }
perf_event_task_disable(void)1879cdd6c482SIngo Molnar static inline int perf_event_task_disable(void)				{ return -EINVAL; }
perf_event_task_enable(void)1880cdd6c482SIngo Molnar static inline int perf_event_task_enable(void)				{ return -EINVAL; }
perf_event_refresh(struct perf_event * event,int refresh)188126ca5c11SAvi Kivity static inline int perf_event_refresh(struct perf_event *event, int refresh)
188226ca5c11SAvi Kivity {
188326ca5c11SAvi Kivity 	return -EINVAL;
188426ca5c11SAvi Kivity }
1885cdd6c482SIngo Molnar 
1886cdd6c482SIngo Molnar static inline void
perf_sw_event(u32 event_id,u64 nr,struct pt_regs * regs,u64 addr)1887a8b0ca17SPeter Zijlstra perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)	{ }
188824f1e32cSFrederic Weisbecker static inline void
perf_bp_event(struct perf_event * event,void * data)188924f1e32cSFrederic Weisbecker perf_bp_event(struct perf_event *event, void *data)			{ }
1890cdd6c482SIngo Molnar 
perf_event_mmap(struct vm_area_struct * vma)1891cdd6c482SIngo Molnar static inline void perf_event_mmap(struct vm_area_struct *vma)		{ }
189276193a94SSong Liu 
189376193a94SSong Liu typedef int (perf_ksymbol_get_name_f)(char *name, int name_len, void *data);
perf_event_ksymbol(u16 ksym_type,u64 addr,u32 len,bool unregister,const char * sym)189476193a94SSong Liu static inline void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
189576193a94SSong Liu 				      bool unregister, const char *sym)	{ }
perf_event_bpf_event(struct bpf_prog * prog,enum perf_bpf_event_type type,u16 flags)18966ee52e2aSSong Liu static inline void perf_event_bpf_event(struct bpf_prog *prog,
18976ee52e2aSSong Liu 					enum perf_bpf_event_type type,
18986ee52e2aSSong Liu 					u16 flags)			{ }
perf_event_exec(void)1899e041e328SPeter Zijlstra static inline void perf_event_exec(void)				{ }
perf_event_comm(struct task_struct * tsk,bool exec)190082b89778SAdrian Hunter static inline void perf_event_comm(struct task_struct *tsk, bool exec)	{ }
perf_event_namespaces(struct task_struct * tsk)1901e4222673SHari Bathini static inline void perf_event_namespaces(struct task_struct *tsk)	{ }
perf_event_fork(struct task_struct * tsk)1902cdd6c482SIngo Molnar static inline void perf_event_fork(struct task_struct *tsk)		{ }
perf_event_text_poke(const void * addr,const void * old_bytes,size_t old_len,const void * new_bytes,size_t new_len)1903e17d43b9SAdrian Hunter static inline void perf_event_text_poke(const void *addr,
1904e17d43b9SAdrian Hunter 					const void *old_bytes,
1905e17d43b9SAdrian Hunter 					size_t old_len,
1906e17d43b9SAdrian Hunter 					const void *new_bytes,
1907e17d43b9SAdrian Hunter 					size_t new_len)			{ }
perf_event_init(void)1908cdd6c482SIngo Molnar static inline void perf_event_init(void)				{ }
perf_swevent_get_recursion_context(void)19094ed7c92dSPeter Zijlstra static inline int  perf_swevent_get_recursion_context(void)		{ return -1; }
perf_swevent_put_recursion_context(int rctx)19104ed7c92dSPeter Zijlstra static inline void perf_swevent_put_recursion_context(int rctx)		{ }
perf_swevent_set_period(struct perf_event * event)1911ab573844SJiri Olsa static inline u64 perf_swevent_set_period(struct perf_event *event)	{ return 0; }
perf_event_enable(struct perf_event * event)191244234adcSFrederic Weisbecker static inline void perf_event_enable(struct perf_event *event)		{ }
perf_event_disable(struct perf_event * event)191344234adcSFrederic Weisbecker static inline void perf_event_disable(struct perf_event *event)		{ }
__perf_event_disable(void * info)1914500ad2d8SK.Prasad static inline int __perf_event_disable(void *info)			{ return -1; }
perf_event_task_tick(void)1915e9d2b064SPeter Zijlstra static inline void perf_event_task_tick(void)				{ }
perf_event_release_kernel(struct perf_event * event)1916ffe8690cSKaixu Xia static inline int perf_event_release_kernel(struct perf_event *event)	{ return 0; }
perf_event_period(struct perf_event * event,u64 value)19173ca270fcSLike Xu static inline int perf_event_period(struct perf_event *event, u64 value)
19183ca270fcSLike Xu {
19193ca270fcSLike Xu 	return -EINVAL;
19203ca270fcSLike Xu }
perf_event_pause(struct perf_event * event,bool reset)192152ba4b0bSLike Xu static inline u64 perf_event_pause(struct perf_event *event, bool reset)
192252ba4b0bSLike Xu {
192352ba4b0bSLike Xu 	return 0;
192452ba4b0bSLike Xu }
perf_exclude_event(struct perf_event * event,struct pt_regs * regs)19256057b90eSNamhyung Kim static inline int perf_exclude_event(struct perf_event *event, struct pt_regs *regs)
19266057b90eSNamhyung Kim {
19276057b90eSNamhyung Kim 	return 0;
19286057b90eSNamhyung Kim }
1929cdd6c482SIngo Molnar #endif
1930cdd6c482SIngo Molnar 
19316c4d3bc9SDavid Rientjes #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
19326c4d3bc9SDavid Rientjes extern void perf_restore_debug_store(void);
19336c4d3bc9SDavid Rientjes #else
perf_restore_debug_store(void)19341d9d8639SStephane Eranian static inline void perf_restore_debug_store(void)			{ }
1935cdd6c482SIngo Molnar #endif
1936cdd6c482SIngo Molnar 
1937e7e7ee2eSIngo Molnar #define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
1938cdd6c482SIngo Molnar 
19392663960cSSukadev Bhattiprolu struct perf_pmu_events_attr {
19402663960cSSukadev Bhattiprolu 	struct device_attribute attr;
19412663960cSSukadev Bhattiprolu 	u64 id;
19423a54aaa0SStephane Eranian 	const char *event_str;
19432663960cSSukadev Bhattiprolu };
19442663960cSSukadev Bhattiprolu 
1945fc07e9f9SAndi Kleen struct perf_pmu_events_ht_attr {
1946fc07e9f9SAndi Kleen 	struct device_attribute			attr;
1947fc07e9f9SAndi Kleen 	u64					id;
1948fc07e9f9SAndi Kleen 	const char				*event_str_ht;
1949fc07e9f9SAndi Kleen 	const char				*event_str_noht;
1950fc07e9f9SAndi Kleen };
1951fc07e9f9SAndi Kleen 
1952a9c81ccdSKan Liang struct perf_pmu_events_hybrid_attr {
1953a9c81ccdSKan Liang 	struct device_attribute			attr;
1954a9c81ccdSKan Liang 	u64					id;
1955a9c81ccdSKan Liang 	const char				*event_str;
1956a9c81ccdSKan Liang 	u64					pmu_type;
1957a9c81ccdSKan Liang };
1958a9c81ccdSKan Liang 
1959a9c81ccdSKan Liang struct perf_pmu_format_hybrid_attr {
1960a9c81ccdSKan Liang 	struct device_attribute			attr;
1961a9c81ccdSKan Liang 	u64					pmu_type;
1962a9c81ccdSKan Liang };
1963a9c81ccdSKan Liang 
1964fd979c01SCody P Schafer ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
1965fd979c01SCody P Schafer 			      char *page);
1966fd979c01SCody P Schafer 
19672663960cSSukadev Bhattiprolu #define PMU_EVENT_ATTR(_name, _var, _id, _show)				\
19682663960cSSukadev Bhattiprolu static struct perf_pmu_events_attr _var = {				\
19692663960cSSukadev Bhattiprolu 	.attr = __ATTR(_name, 0444, _show, NULL),			\
19702663960cSSukadev Bhattiprolu 	.id   =  _id,							\
19712663960cSSukadev Bhattiprolu };
19722663960cSSukadev Bhattiprolu 
1973f0405b81SCody P Schafer #define PMU_EVENT_ATTR_STRING(_name, _var, _str)			    \
1974f0405b81SCody P Schafer static struct perf_pmu_events_attr _var = {				    \
1975f0405b81SCody P Schafer 	.attr		= __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
1976f0405b81SCody P Schafer 	.id		= 0,						    \
1977f0405b81SCody P Schafer 	.event_str	= _str,						    \
1978f0405b81SCody P Schafer };
1979f0405b81SCody P Schafer 
1980f8e6d241SQi Liu #define PMU_EVENT_ATTR_ID(_name, _show, _id)				\
1981f8e6d241SQi Liu 	(&((struct perf_pmu_events_attr[]) {				\
1982f8e6d241SQi Liu 		{ .attr = __ATTR(_name, 0444, _show, NULL),		\
1983f8e6d241SQi Liu 		  .id = _id, }						\
1984f8e6d241SQi Liu 	})[0].attr.attr)
1985f8e6d241SQi Liu 
1986b6c00fb9SKan Liang #define PMU_FORMAT_ATTR_SHOW(_name, _format)				\
1987641cc938SJiri Olsa static ssize_t								\
1988641cc938SJiri Olsa _name##_show(struct device *dev,					\
1989641cc938SJiri Olsa 			       struct device_attribute *attr,		\
1990641cc938SJiri Olsa 			       char *page)				\
1991641cc938SJiri Olsa {									\
1992641cc938SJiri Olsa 	BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE);			\
1993641cc938SJiri Olsa 	return sprintf(page, _format "\n");				\
1994641cc938SJiri Olsa }									\
1995b6c00fb9SKan Liang 
1996b6c00fb9SKan Liang #define PMU_FORMAT_ATTR(_name, _format)					\
1997b6c00fb9SKan Liang 	PMU_FORMAT_ATTR_SHOW(_name, _format)				\
1998641cc938SJiri Olsa 									\
1999641cc938SJiri Olsa static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
2000641cc938SJiri Olsa 
200100e16c3dSThomas Gleixner /* Performance counter hotplug functions */
200200e16c3dSThomas Gleixner #ifdef CONFIG_PERF_EVENTS
200300e16c3dSThomas Gleixner int perf_event_init_cpu(unsigned int cpu);
200400e16c3dSThomas Gleixner int perf_event_exit_cpu(unsigned int cpu);
200500e16c3dSThomas Gleixner #else
200600e16c3dSThomas Gleixner #define perf_event_init_cpu	NULL
200700e16c3dSThomas Gleixner #define perf_event_exit_cpu	NULL
200800e16c3dSThomas Gleixner #endif
200900e16c3dSThomas Gleixner 
2010b50f26a4SMarc Zyngier extern void arch_perf_update_userpage(struct perf_event *event,
2011f1ec3a51SBenjamin Thiel 				      struct perf_event_mmap_page *userpg,
2012f1ec3a51SBenjamin Thiel 				      u64 now);
2013f1ec3a51SBenjamin Thiel 
2014c22ac2a3SSong Liu /*
2015c22ac2a3SSong Liu  * Snapshot branch stack on software events.
2016c22ac2a3SSong Liu  *
2017c22ac2a3SSong Liu  * Branch stack can be very useful in understanding software events. For
2018c22ac2a3SSong Liu  * example, when a long function, e.g. sys_perf_event_open, returns an
2019c22ac2a3SSong Liu  * errno, it is not obvious why the function failed. Branch stack could
2020c22ac2a3SSong Liu  * provide very helpful information in this type of scenarios.
2021c22ac2a3SSong Liu  *
2022c22ac2a3SSong Liu  * On software event, it is necessary to stop the hardware branch recorder
2023c22ac2a3SSong Liu  * fast. Otherwise, the hardware register/buffer will be flushed with
2024c22ac2a3SSong Liu  * entries of the triggering event. Therefore, static call is used to
2025c22ac2a3SSong Liu  * stop the hardware recorder.
2026c22ac2a3SSong Liu  */
2027c22ac2a3SSong Liu 
2028c22ac2a3SSong Liu /*
2029c22ac2a3SSong Liu  * cnt is the number of entries allocated for entries.
2030c22ac2a3SSong Liu  * Return number of entries copied to .
2031c22ac2a3SSong Liu  */
2032c22ac2a3SSong Liu typedef int (perf_snapshot_branch_stack_t)(struct perf_branch_entry *entries,
2033c22ac2a3SSong Liu 					   unsigned int cnt);
2034c22ac2a3SSong Liu DECLARE_STATIC_CALL(perf_snapshot_branch_stack, perf_snapshot_branch_stack_t);
2035c22ac2a3SSong Liu 
20362a606a18SStephane Eranian #ifndef PERF_NEEDS_LOPWR_CB
perf_lopwr_cb(bool mode)20372a606a18SStephane Eranian static inline void perf_lopwr_cb(bool mode)
20382a606a18SStephane Eranian {
20392a606a18SStephane Eranian }
20402a606a18SStephane Eranian #endif
20412a606a18SStephane Eranian 
2042cdd6c482SIngo Molnar #endif /* _LINUX_PERF_EVENT_H */
2043