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