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