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