xref: /linux-6.15/include/linux/perf_event.h (revision eea9507a)
1 /*
2  * Performance events:
3  *
4  *    Copyright (C) 2008-2009, Thomas Gleixner <[email protected]>
5  *    Copyright (C) 2008-2011, Red Hat, Inc., Ingo Molnar
6  *    Copyright (C) 2008-2011, Red Hat, Inc., Peter Zijlstra
7  *
8  * Data type definitions, declarations, prototypes.
9  *
10  *    Started by: Thomas Gleixner and Ingo Molnar
11  *
12  * For licencing details see kernel-base/COPYING
13  */
14 #ifndef _LINUX_PERF_EVENT_H
15 #define _LINUX_PERF_EVENT_H
16 
17 #include <uapi/linux/perf_event.h>
18 
19 /*
20  * Kernel-internal data types and definitions:
21  */
22 
23 #ifdef CONFIG_PERF_EVENTS
24 # include <asm/perf_event.h>
25 # include <asm/local64.h>
26 #endif
27 
28 struct perf_guest_info_callbacks {
29 	int				(*is_in_guest)(void);
30 	int				(*is_user_mode)(void);
31 	unsigned long			(*get_guest_ip)(void);
32 };
33 
34 #ifdef CONFIG_HAVE_HW_BREAKPOINT
35 #include <asm/hw_breakpoint.h>
36 #endif
37 
38 #include <linux/list.h>
39 #include <linux/mutex.h>
40 #include <linux/rculist.h>
41 #include <linux/rcupdate.h>
42 #include <linux/spinlock.h>
43 #include <linux/hrtimer.h>
44 #include <linux/fs.h>
45 #include <linux/pid_namespace.h>
46 #include <linux/workqueue.h>
47 #include <linux/ftrace.h>
48 #include <linux/cpu.h>
49 #include <linux/irq_work.h>
50 #include <linux/static_key.h>
51 #include <linux/jump_label_ratelimit.h>
52 #include <linux/atomic.h>
53 #include <linux/sysfs.h>
54 #include <linux/perf_regs.h>
55 #include <linux/workqueue.h>
56 #include <asm/local.h>
57 
58 struct perf_callchain_entry {
59 	__u64				nr;
60 	__u64				ip[PERF_MAX_STACK_DEPTH];
61 };
62 
63 struct perf_raw_record {
64 	u32				size;
65 	void				*data;
66 };
67 
68 /*
69  * branch stack layout:
70  *  nr: number of taken branches stored in entries[]
71  *
72  * Note that nr can vary from sample to sample
73  * branches (to, from) are stored from most recent
74  * to least recent, i.e., entries[0] contains the most
75  * recent branch.
76  */
77 struct perf_branch_stack {
78 	__u64				nr;
79 	struct perf_branch_entry	entries[0];
80 };
81 
82 struct perf_regs_user {
83 	__u64		abi;
84 	struct pt_regs	*regs;
85 };
86 
87 struct task_struct;
88 
89 /*
90  * extra PMU register associated with an event
91  */
92 struct hw_perf_event_extra {
93 	u64		config;	/* register value */
94 	unsigned int	reg;	/* register address or index */
95 	int		alloc;	/* extra register already allocated */
96 	int		idx;	/* index in shared_regs->regs[] */
97 };
98 
99 struct event_constraint;
100 
101 /**
102  * struct hw_perf_event - performance event hardware details:
103  */
104 struct hw_perf_event {
105 #ifdef CONFIG_PERF_EVENTS
106 	union {
107 		struct { /* hardware */
108 			u64		config;
109 			u64		last_tag;
110 			unsigned long	config_base;
111 			unsigned long	event_base;
112 			int		event_base_rdpmc;
113 			int		idx;
114 			int		last_cpu;
115 			int		flags;
116 
117 			struct hw_perf_event_extra extra_reg;
118 			struct hw_perf_event_extra branch_reg;
119 
120 			struct event_constraint *constraint;
121 		};
122 		struct { /* software */
123 			struct hrtimer	hrtimer;
124 		};
125 		struct { /* tracepoint */
126 			struct task_struct	*tp_target;
127 			/* for tp_event->class */
128 			struct list_head	tp_list;
129 		};
130 #ifdef CONFIG_HAVE_HW_BREAKPOINT
131 		struct { /* breakpoint */
132 			/*
133 			 * Crufty hack to avoid the chicken and egg
134 			 * problem hw_breakpoint has with context
135 			 * creation and event initalization.
136 			 */
137 			struct task_struct		*bp_target;
138 			struct arch_hw_breakpoint	info;
139 			struct list_head		bp_list;
140 		};
141 #endif
142 	};
143 	int				state;
144 	local64_t			prev_count;
145 	u64				sample_period;
146 	u64				last_period;
147 	local64_t			period_left;
148 	u64                             interrupts_seq;
149 	u64				interrupts;
150 
151 	u64				freq_time_stamp;
152 	u64				freq_count_stamp;
153 #endif
154 };
155 
156 /*
157  * hw_perf_event::state flags
158  */
159 #define PERF_HES_STOPPED	0x01 /* the counter is stopped */
160 #define PERF_HES_UPTODATE	0x02 /* event->count up-to-date */
161 #define PERF_HES_ARCH		0x04
162 
163 struct perf_event;
164 
165 /*
166  * Common implementation detail of pmu::{start,commit,cancel}_txn
167  */
168 #define PERF_EVENT_TXN 0x1
169 
170 /**
171  * pmu::capabilities flags
172  */
173 #define PERF_PMU_CAP_NO_INTERRUPT		0x01
174 
175 /**
176  * struct pmu - generic performance monitoring unit
177  */
178 struct pmu {
179 	struct list_head		entry;
180 
181 	struct module			*module;
182 	struct device			*dev;
183 	const struct attribute_group	**attr_groups;
184 	const char			*name;
185 	int				type;
186 
187 	/*
188 	 * various common per-pmu feature flags
189 	 */
190 	int				capabilities;
191 
192 	int * __percpu			pmu_disable_count;
193 	struct perf_cpu_context * __percpu pmu_cpu_context;
194 	int				task_ctx_nr;
195 	int				hrtimer_interval_ms;
196 
197 	/*
198 	 * Fully disable/enable this PMU, can be used to protect from the PMI
199 	 * as well as for lazy/batch writing of the MSRs.
200 	 */
201 	void (*pmu_enable)		(struct pmu *pmu); /* optional */
202 	void (*pmu_disable)		(struct pmu *pmu); /* optional */
203 
204 	/*
205 	 * Try and initialize the event for this PMU.
206 	 * Should return -ENOENT when the @event doesn't match this PMU.
207 	 */
208 	int (*event_init)		(struct perf_event *event);
209 
210 #define PERF_EF_START	0x01		/* start the counter when adding    */
211 #define PERF_EF_RELOAD	0x02		/* reload the counter when starting */
212 #define PERF_EF_UPDATE	0x04		/* update the counter when stopping */
213 
214 	/*
215 	 * Adds/Removes a counter to/from the PMU, can be done inside
216 	 * a transaction, see the ->*_txn() methods.
217 	 */
218 	int  (*add)			(struct perf_event *event, int flags);
219 	void (*del)			(struct perf_event *event, int flags);
220 
221 	/*
222 	 * Starts/Stops a counter present on the PMU. The PMI handler
223 	 * should stop the counter when perf_event_overflow() returns
224 	 * !0. ->start() will be used to continue.
225 	 */
226 	void (*start)			(struct perf_event *event, int flags);
227 	void (*stop)			(struct perf_event *event, int flags);
228 
229 	/*
230 	 * Updates the counter value of the event.
231 	 */
232 	void (*read)			(struct perf_event *event);
233 
234 	/*
235 	 * Group events scheduling is treated as a transaction, add
236 	 * group events as a whole and perform one schedulability test.
237 	 * If the test fails, roll back the whole group
238 	 *
239 	 * Start the transaction, after this ->add() doesn't need to
240 	 * do schedulability tests.
241 	 */
242 	void (*start_txn)		(struct pmu *pmu); /* optional */
243 	/*
244 	 * If ->start_txn() disabled the ->add() schedulability test
245 	 * then ->commit_txn() is required to perform one. On success
246 	 * the transaction is closed. On error the transaction is kept
247 	 * open until ->cancel_txn() is called.
248 	 */
249 	int  (*commit_txn)		(struct pmu *pmu); /* optional */
250 	/*
251 	 * Will cancel the transaction, assumes ->del() is called
252 	 * for each successful ->add() during the transaction.
253 	 */
254 	void (*cancel_txn)		(struct pmu *pmu); /* optional */
255 
256 	/*
257 	 * Will return the value for perf_event_mmap_page::index for this event,
258 	 * if no implementation is provided it will default to: event->hw.idx + 1.
259 	 */
260 	int (*event_idx)		(struct perf_event *event); /*optional */
261 
262 	/*
263 	 * flush branch stack on context-switches (needed in cpu-wide mode)
264 	 */
265 	void (*flush_branch_stack)	(void);
266 };
267 
268 /**
269  * enum perf_event_active_state - the states of a event
270  */
271 enum perf_event_active_state {
272 	PERF_EVENT_STATE_EXIT		= -3,
273 	PERF_EVENT_STATE_ERROR		= -2,
274 	PERF_EVENT_STATE_OFF		= -1,
275 	PERF_EVENT_STATE_INACTIVE	=  0,
276 	PERF_EVENT_STATE_ACTIVE		=  1,
277 };
278 
279 struct file;
280 struct perf_sample_data;
281 
282 typedef void (*perf_overflow_handler_t)(struct perf_event *,
283 					struct perf_sample_data *,
284 					struct pt_regs *regs);
285 
286 enum perf_group_flag {
287 	PERF_GROUP_SOFTWARE		= 0x1,
288 };
289 
290 #define SWEVENT_HLIST_BITS		8
291 #define SWEVENT_HLIST_SIZE		(1 << SWEVENT_HLIST_BITS)
292 
293 struct swevent_hlist {
294 	struct hlist_head		heads[SWEVENT_HLIST_SIZE];
295 	struct rcu_head			rcu_head;
296 };
297 
298 #define PERF_ATTACH_CONTEXT	0x01
299 #define PERF_ATTACH_GROUP	0x02
300 #define PERF_ATTACH_TASK	0x04
301 
302 struct perf_cgroup;
303 struct ring_buffer;
304 
305 /**
306  * struct perf_event - performance event kernel representation:
307  */
308 struct perf_event {
309 #ifdef CONFIG_PERF_EVENTS
310 	/*
311 	 * entry onto perf_event_context::event_list;
312 	 *   modifications require ctx->lock
313 	 *   RCU safe iterations.
314 	 */
315 	struct list_head		event_entry;
316 
317 	/*
318 	 * XXX: group_entry and sibling_list should be mutually exclusive;
319 	 * either you're a sibling on a group, or you're the group leader.
320 	 * Rework the code to always use the same list element.
321 	 *
322 	 * Locked for modification by both ctx->mutex and ctx->lock; holding
323 	 * either sufficies for read.
324 	 */
325 	struct list_head		group_entry;
326 	struct list_head		sibling_list;
327 
328 	/*
329 	 * We need storage to track the entries in perf_pmu_migrate_context; we
330 	 * cannot use the event_entry because of RCU and we want to keep the
331 	 * group in tact which avoids us using the other two entries.
332 	 */
333 	struct list_head		migrate_entry;
334 
335 	struct hlist_node		hlist_entry;
336 	struct list_head		active_entry;
337 	int				nr_siblings;
338 	int				group_flags;
339 	struct perf_event		*group_leader;
340 	struct pmu			*pmu;
341 
342 	enum perf_event_active_state	state;
343 	unsigned int			attach_state;
344 	local64_t			count;
345 	atomic64_t			child_count;
346 
347 	/*
348 	 * These are the total time in nanoseconds that the event
349 	 * has been enabled (i.e. eligible to run, and the task has
350 	 * been scheduled in, if this is a per-task event)
351 	 * and running (scheduled onto the CPU), respectively.
352 	 *
353 	 * They are computed from tstamp_enabled, tstamp_running and
354 	 * tstamp_stopped when the event is in INACTIVE or ACTIVE state.
355 	 */
356 	u64				total_time_enabled;
357 	u64				total_time_running;
358 
359 	/*
360 	 * These are timestamps used for computing total_time_enabled
361 	 * and total_time_running when the event is in INACTIVE or
362 	 * ACTIVE state, measured in nanoseconds from an arbitrary point
363 	 * in time.
364 	 * tstamp_enabled: the notional time when the event was enabled
365 	 * tstamp_running: the notional time when the event was scheduled on
366 	 * tstamp_stopped: in INACTIVE state, the notional time when the
367 	 *	event was scheduled off.
368 	 */
369 	u64				tstamp_enabled;
370 	u64				tstamp_running;
371 	u64				tstamp_stopped;
372 
373 	/*
374 	 * timestamp shadows the actual context timing but it can
375 	 * be safely used in NMI interrupt context. It reflects the
376 	 * context time as it was when the event was last scheduled in.
377 	 *
378 	 * ctx_time already accounts for ctx->timestamp. Therefore to
379 	 * compute ctx_time for a sample, simply add perf_clock().
380 	 */
381 	u64				shadow_ctx_time;
382 
383 	struct perf_event_attr		attr;
384 	u16				header_size;
385 	u16				id_header_size;
386 	u16				read_size;
387 	struct hw_perf_event		hw;
388 
389 	struct perf_event_context	*ctx;
390 	atomic_long_t			refcount;
391 
392 	/*
393 	 * These accumulate total time (in nanoseconds) that children
394 	 * events have been enabled and running, respectively.
395 	 */
396 	atomic64_t			child_total_time_enabled;
397 	atomic64_t			child_total_time_running;
398 
399 	/*
400 	 * Protect attach/detach and child_list:
401 	 */
402 	struct mutex			child_mutex;
403 	struct list_head		child_list;
404 	struct perf_event		*parent;
405 
406 	int				oncpu;
407 	int				cpu;
408 
409 	struct list_head		owner_entry;
410 	struct task_struct		*owner;
411 
412 	/* mmap bits */
413 	struct mutex			mmap_mutex;
414 	atomic_t			mmap_count;
415 
416 	struct ring_buffer		*rb;
417 	struct list_head		rb_entry;
418 	unsigned long			rcu_batches;
419 	int				rcu_pending;
420 
421 	/* poll related */
422 	wait_queue_head_t		waitq;
423 	struct fasync_struct		*fasync;
424 
425 	/* delayed work for NMIs and such */
426 	int				pending_wakeup;
427 	int				pending_kill;
428 	int				pending_disable;
429 	struct irq_work			pending;
430 
431 	atomic_t			event_limit;
432 
433 	void (*destroy)(struct perf_event *);
434 	struct rcu_head			rcu_head;
435 
436 	struct pid_namespace		*ns;
437 	u64				id;
438 
439 	perf_overflow_handler_t		overflow_handler;
440 	void				*overflow_handler_context;
441 
442 #ifdef CONFIG_EVENT_TRACING
443 	struct ftrace_event_call	*tp_event;
444 	struct event_filter		*filter;
445 #ifdef CONFIG_FUNCTION_TRACER
446 	struct ftrace_ops               ftrace_ops;
447 #endif
448 #endif
449 
450 #ifdef CONFIG_CGROUP_PERF
451 	struct perf_cgroup		*cgrp; /* cgroup event is attach to */
452 	int				cgrp_defer_enabled;
453 #endif
454 
455 #endif /* CONFIG_PERF_EVENTS */
456 };
457 
458 enum perf_event_context_type {
459 	task_context,
460 	cpu_context,
461 };
462 
463 /**
464  * struct perf_event_context - event context structure
465  *
466  * Used as a container for task events and CPU events as well:
467  */
468 struct perf_event_context {
469 	struct pmu			*pmu;
470 	enum perf_event_context_type	type;
471 	/*
472 	 * Protect the states of the events in the list,
473 	 * nr_active, and the list:
474 	 */
475 	raw_spinlock_t			lock;
476 	/*
477 	 * Protect the list of events.  Locking either mutex or lock
478 	 * is sufficient to ensure the list doesn't change; to change
479 	 * the list you need to lock both the mutex and the spinlock.
480 	 */
481 	struct mutex			mutex;
482 
483 	struct list_head		pinned_groups;
484 	struct list_head		flexible_groups;
485 	struct list_head		event_list;
486 	int				nr_events;
487 	int				nr_active;
488 	int				is_active;
489 	int				nr_stat;
490 	int				nr_freq;
491 	int				rotate_disable;
492 	atomic_t			refcount;
493 	struct task_struct		*task;
494 
495 	/*
496 	 * Context clock, runs when context enabled.
497 	 */
498 	u64				time;
499 	u64				timestamp;
500 
501 	/*
502 	 * These fields let us detect when two contexts have both
503 	 * been cloned (inherited) from a common ancestor.
504 	 */
505 	struct perf_event_context	*parent_ctx;
506 	u64				parent_gen;
507 	u64				generation;
508 	int				pin_count;
509 	int				nr_cgroups;	 /* cgroup evts */
510 	int				nr_branch_stack; /* branch_stack evt */
511 	struct rcu_head			rcu_head;
512 
513 	struct delayed_work		orphans_remove;
514 	bool				orphans_remove_sched;
515 };
516 
517 /*
518  * Number of contexts where an event can trigger:
519  *	task, softirq, hardirq, nmi.
520  */
521 #define PERF_NR_CONTEXTS	4
522 
523 /**
524  * struct perf_event_cpu_context - per cpu event context structure
525  */
526 struct perf_cpu_context {
527 	struct perf_event_context	ctx;
528 	struct perf_event_context	*task_ctx;
529 	int				active_oncpu;
530 	int				exclusive;
531 	struct hrtimer			hrtimer;
532 	ktime_t				hrtimer_interval;
533 	struct list_head		rotation_list;
534 	struct pmu			*unique_pmu;
535 	struct perf_cgroup		*cgrp;
536 };
537 
538 struct perf_output_handle {
539 	struct perf_event		*event;
540 	struct ring_buffer		*rb;
541 	unsigned long			wakeup;
542 	unsigned long			size;
543 	void				*addr;
544 	int				page;
545 };
546 
547 #ifdef CONFIG_PERF_EVENTS
548 
549 extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
550 extern void perf_pmu_unregister(struct pmu *pmu);
551 
552 extern int perf_num_counters(void);
553 extern const char *perf_pmu_name(void);
554 extern void __perf_event_task_sched_in(struct task_struct *prev,
555 				       struct task_struct *task);
556 extern void __perf_event_task_sched_out(struct task_struct *prev,
557 					struct task_struct *next);
558 extern int perf_event_init_task(struct task_struct *child);
559 extern void perf_event_exit_task(struct task_struct *child);
560 extern void perf_event_free_task(struct task_struct *task);
561 extern void perf_event_delayed_put(struct task_struct *task);
562 extern void perf_event_print_debug(void);
563 extern void perf_pmu_disable(struct pmu *pmu);
564 extern void perf_pmu_enable(struct pmu *pmu);
565 extern int perf_event_task_disable(void);
566 extern int perf_event_task_enable(void);
567 extern int perf_event_refresh(struct perf_event *event, int refresh);
568 extern void perf_event_update_userpage(struct perf_event *event);
569 extern int perf_event_release_kernel(struct perf_event *event);
570 extern struct perf_event *
571 perf_event_create_kernel_counter(struct perf_event_attr *attr,
572 				int cpu,
573 				struct task_struct *task,
574 				perf_overflow_handler_t callback,
575 				void *context);
576 extern void perf_pmu_migrate_context(struct pmu *pmu,
577 				int src_cpu, int dst_cpu);
578 extern u64 perf_event_read_value(struct perf_event *event,
579 				 u64 *enabled, u64 *running);
580 
581 
582 struct perf_sample_data {
583 	u64				type;
584 
585 	u64				ip;
586 	struct {
587 		u32	pid;
588 		u32	tid;
589 	}				tid_entry;
590 	u64				time;
591 	u64				addr;
592 	u64				id;
593 	u64				stream_id;
594 	struct {
595 		u32	cpu;
596 		u32	reserved;
597 	}				cpu_entry;
598 	u64				period;
599 	union  perf_mem_data_src	data_src;
600 	struct perf_callchain_entry	*callchain;
601 	struct perf_raw_record		*raw;
602 	struct perf_branch_stack	*br_stack;
603 	struct perf_regs_user		regs_user;
604 	u64				stack_user_size;
605 	u64				weight;
606 	/*
607 	 * Transaction flags for abort events:
608 	 */
609 	u64				txn;
610 };
611 
612 /* default value for data source */
613 #define PERF_MEM_NA (PERF_MEM_S(OP, NA)   |\
614 		    PERF_MEM_S(LVL, NA)   |\
615 		    PERF_MEM_S(SNOOP, NA) |\
616 		    PERF_MEM_S(LOCK, NA)  |\
617 		    PERF_MEM_S(TLB, NA))
618 
619 static inline void perf_sample_data_init(struct perf_sample_data *data,
620 					 u64 addr, u64 period)
621 {
622 	/* remaining struct members initialized in perf_prepare_sample() */
623 	data->addr = addr;
624 	data->raw  = NULL;
625 	data->br_stack = NULL;
626 	data->period = period;
627 	data->regs_user.abi = PERF_SAMPLE_REGS_ABI_NONE;
628 	data->regs_user.regs = NULL;
629 	data->stack_user_size = 0;
630 	data->weight = 0;
631 	data->data_src.val = PERF_MEM_NA;
632 	data->txn = 0;
633 }
634 
635 extern void perf_output_sample(struct perf_output_handle *handle,
636 			       struct perf_event_header *header,
637 			       struct perf_sample_data *data,
638 			       struct perf_event *event);
639 extern void perf_prepare_sample(struct perf_event_header *header,
640 				struct perf_sample_data *data,
641 				struct perf_event *event,
642 				struct pt_regs *regs);
643 
644 extern int perf_event_overflow(struct perf_event *event,
645 				 struct perf_sample_data *data,
646 				 struct pt_regs *regs);
647 
648 static inline bool is_sampling_event(struct perf_event *event)
649 {
650 	return event->attr.sample_period != 0;
651 }
652 
653 /*
654  * Return 1 for a software event, 0 for a hardware event
655  */
656 static inline int is_software_event(struct perf_event *event)
657 {
658 	return event->pmu->task_ctx_nr == perf_sw_context;
659 }
660 
661 extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
662 
663 extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
664 
665 #ifndef perf_arch_fetch_caller_regs
666 static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
667 #endif
668 
669 /*
670  * Take a snapshot of the regs. Skip ip and frame pointer to
671  * the nth caller. We only need a few of the regs:
672  * - ip for PERF_SAMPLE_IP
673  * - cs for user_mode() tests
674  * - bp for callchains
675  * - eflags, for future purposes, just in case
676  */
677 static inline void perf_fetch_caller_regs(struct pt_regs *regs)
678 {
679 	memset(regs, 0, sizeof(*regs));
680 
681 	perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
682 }
683 
684 static __always_inline void
685 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
686 {
687 	struct pt_regs hot_regs;
688 
689 	if (static_key_false(&perf_swevent_enabled[event_id])) {
690 		if (!regs) {
691 			perf_fetch_caller_regs(&hot_regs);
692 			regs = &hot_regs;
693 		}
694 		__perf_sw_event(event_id, nr, regs, addr);
695 	}
696 }
697 
698 extern struct static_key_deferred perf_sched_events;
699 
700 static inline void perf_event_task_sched_in(struct task_struct *prev,
701 					    struct task_struct *task)
702 {
703 	if (static_key_false(&perf_sched_events.key))
704 		__perf_event_task_sched_in(prev, task);
705 }
706 
707 static inline void perf_event_task_sched_out(struct task_struct *prev,
708 					     struct task_struct *next)
709 {
710 	perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, NULL, 0);
711 
712 	if (static_key_false(&perf_sched_events.key))
713 		__perf_event_task_sched_out(prev, next);
714 }
715 
716 extern void perf_event_mmap(struct vm_area_struct *vma);
717 extern struct perf_guest_info_callbacks *perf_guest_cbs;
718 extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
719 extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
720 
721 extern void perf_event_exec(void);
722 extern void perf_event_comm(struct task_struct *tsk, bool exec);
723 extern void perf_event_fork(struct task_struct *tsk);
724 
725 /* Callchains */
726 DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
727 
728 extern void perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs);
729 extern void perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs);
730 
731 static inline void perf_callchain_store(struct perf_callchain_entry *entry, u64 ip)
732 {
733 	if (entry->nr < PERF_MAX_STACK_DEPTH)
734 		entry->ip[entry->nr++] = ip;
735 }
736 
737 extern int sysctl_perf_event_paranoid;
738 extern int sysctl_perf_event_mlock;
739 extern int sysctl_perf_event_sample_rate;
740 extern int sysctl_perf_cpu_time_max_percent;
741 
742 extern void perf_sample_event_took(u64 sample_len_ns);
743 
744 extern int perf_proc_update_handler(struct ctl_table *table, int write,
745 		void __user *buffer, size_t *lenp,
746 		loff_t *ppos);
747 extern int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
748 		void __user *buffer, size_t *lenp,
749 		loff_t *ppos);
750 
751 
752 static inline bool perf_paranoid_tracepoint_raw(void)
753 {
754 	return sysctl_perf_event_paranoid > -1;
755 }
756 
757 static inline bool perf_paranoid_cpu(void)
758 {
759 	return sysctl_perf_event_paranoid > 0;
760 }
761 
762 static inline bool perf_paranoid_kernel(void)
763 {
764 	return sysctl_perf_event_paranoid > 1;
765 }
766 
767 extern void perf_event_init(void);
768 extern void perf_tp_event(u64 addr, u64 count, void *record,
769 			  int entry_size, struct pt_regs *regs,
770 			  struct hlist_head *head, int rctx,
771 			  struct task_struct *task);
772 extern void perf_bp_event(struct perf_event *event, void *data);
773 
774 #ifndef perf_misc_flags
775 # define perf_misc_flags(regs) \
776 		(user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
777 # define perf_instruction_pointer(regs)	instruction_pointer(regs)
778 #endif
779 
780 static inline bool has_branch_stack(struct perf_event *event)
781 {
782 	return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
783 }
784 
785 extern int perf_output_begin(struct perf_output_handle *handle,
786 			     struct perf_event *event, unsigned int size);
787 extern void perf_output_end(struct perf_output_handle *handle);
788 extern unsigned int perf_output_copy(struct perf_output_handle *handle,
789 			     const void *buf, unsigned int len);
790 extern unsigned int perf_output_skip(struct perf_output_handle *handle,
791 				     unsigned int len);
792 extern int perf_swevent_get_recursion_context(void);
793 extern void perf_swevent_put_recursion_context(int rctx);
794 extern u64 perf_swevent_set_period(struct perf_event *event);
795 extern void perf_event_enable(struct perf_event *event);
796 extern void perf_event_disable(struct perf_event *event);
797 extern int __perf_event_disable(void *info);
798 extern void perf_event_task_tick(void);
799 #else /* !CONFIG_PERF_EVENTS: */
800 static inline void
801 perf_event_task_sched_in(struct task_struct *prev,
802 			 struct task_struct *task)			{ }
803 static inline void
804 perf_event_task_sched_out(struct task_struct *prev,
805 			  struct task_struct *next)			{ }
806 static inline int perf_event_init_task(struct task_struct *child)	{ return 0; }
807 static inline void perf_event_exit_task(struct task_struct *child)	{ }
808 static inline void perf_event_free_task(struct task_struct *task)	{ }
809 static inline void perf_event_delayed_put(struct task_struct *task)	{ }
810 static inline void perf_event_print_debug(void)				{ }
811 static inline int perf_event_task_disable(void)				{ return -EINVAL; }
812 static inline int perf_event_task_enable(void)				{ return -EINVAL; }
813 static inline int perf_event_refresh(struct perf_event *event, int refresh)
814 {
815 	return -EINVAL;
816 }
817 
818 static inline void
819 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)	{ }
820 static inline void
821 perf_bp_event(struct perf_event *event, void *data)			{ }
822 
823 static inline int perf_register_guest_info_callbacks
824 (struct perf_guest_info_callbacks *callbacks)				{ return 0; }
825 static inline int perf_unregister_guest_info_callbacks
826 (struct perf_guest_info_callbacks *callbacks)				{ return 0; }
827 
828 static inline void perf_event_mmap(struct vm_area_struct *vma)		{ }
829 static inline void perf_event_exec(void)				{ }
830 static inline void perf_event_comm(struct task_struct *tsk, bool exec)	{ }
831 static inline void perf_event_fork(struct task_struct *tsk)		{ }
832 static inline void perf_event_init(void)				{ }
833 static inline int  perf_swevent_get_recursion_context(void)		{ return -1; }
834 static inline void perf_swevent_put_recursion_context(int rctx)		{ }
835 static inline u64 perf_swevent_set_period(struct perf_event *event)	{ return 0; }
836 static inline void perf_event_enable(struct perf_event *event)		{ }
837 static inline void perf_event_disable(struct perf_event *event)		{ }
838 static inline int __perf_event_disable(void *info)			{ return -1; }
839 static inline void perf_event_task_tick(void)				{ }
840 #endif
841 
842 #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_NO_HZ_FULL)
843 extern bool perf_event_can_stop_tick(void);
844 #else
845 static inline bool perf_event_can_stop_tick(void)			{ return true; }
846 #endif
847 
848 #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
849 extern void perf_restore_debug_store(void);
850 #else
851 static inline void perf_restore_debug_store(void)			{ }
852 #endif
853 
854 #define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
855 
856 /*
857  * This has to have a higher priority than migration_notifier in sched/core.c.
858  */
859 #define perf_cpu_notifier(fn)						\
860 do {									\
861 	static struct notifier_block fn##_nb =				\
862 		{ .notifier_call = fn, .priority = CPU_PRI_PERF };	\
863 	unsigned long cpu = smp_processor_id();				\
864 	unsigned long flags;						\
865 									\
866 	cpu_notifier_register_begin();					\
867 	fn(&fn##_nb, (unsigned long)CPU_UP_PREPARE,			\
868 		(void *)(unsigned long)cpu);				\
869 	local_irq_save(flags);						\
870 	fn(&fn##_nb, (unsigned long)CPU_STARTING,			\
871 		(void *)(unsigned long)cpu);				\
872 	local_irq_restore(flags);					\
873 	fn(&fn##_nb, (unsigned long)CPU_ONLINE,				\
874 		(void *)(unsigned long)cpu);				\
875 	__register_cpu_notifier(&fn##_nb);				\
876 	cpu_notifier_register_done();					\
877 } while (0)
878 
879 /*
880  * Bare-bones version of perf_cpu_notifier(), which doesn't invoke the
881  * callback for already online CPUs.
882  */
883 #define __perf_cpu_notifier(fn)						\
884 do {									\
885 	static struct notifier_block fn##_nb =				\
886 		{ .notifier_call = fn, .priority = CPU_PRI_PERF };	\
887 									\
888 	__register_cpu_notifier(&fn##_nb);				\
889 } while (0)
890 
891 struct perf_pmu_events_attr {
892 	struct device_attribute attr;
893 	u64 id;
894 	const char *event_str;
895 };
896 
897 #define PMU_EVENT_ATTR(_name, _var, _id, _show)				\
898 static struct perf_pmu_events_attr _var = {				\
899 	.attr = __ATTR(_name, 0444, _show, NULL),			\
900 	.id   =  _id,							\
901 };
902 
903 #define PMU_FORMAT_ATTR(_name, _format)					\
904 static ssize_t								\
905 _name##_show(struct device *dev,					\
906 			       struct device_attribute *attr,		\
907 			       char *page)				\
908 {									\
909 	BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE);			\
910 	return sprintf(page, _format "\n");				\
911 }									\
912 									\
913 static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
914 
915 #endif /* _LINUX_PERF_EVENT_H */
916