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