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