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