blob: 011dcbdbccc2ae28618cddaa82aa978142db4a66 [file] [log] [blame]
Thomas Gleixner0793a612008-12-04 20:12:29 +01001/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02002 * Performance events:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003 *
Ingo Molnara3084442009-06-11 14:44:26 +02004 * Copyright (C) 2008-2009, Thomas Gleixner <tglx@linutronix.de>
Ingo Molnare7e7ee22011-05-04 08:42:29 +02005 * Copyright (C) 2008-2011, Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011, Red Hat, Inc., Peter Zijlstra
Thomas Gleixner0793a612008-12-04 20:12:29 +01007 *
Ingo Molnar57c0c152009-09-21 12:20:38 +02008 * Data type definitions, declarations, prototypes.
Thomas Gleixner0793a612008-12-04 20:12:29 +01009 *
Ingo Molnara3084442009-06-11 14:44:26 +020010 * Started by: Thomas Gleixner and Ingo Molnar
Thomas Gleixner0793a612008-12-04 20:12:29 +010011 *
Ingo Molnar57c0c152009-09-21 12:20:38 +020012 * For licencing details see kernel-base/COPYING
Thomas Gleixner0793a612008-12-04 20:12:29 +010013 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020014#ifndef _LINUX_PERF_EVENT_H
15#define _LINUX_PERF_EVENT_H
Thomas Gleixner0793a612008-12-04 20:12:29 +010016
David Howells607ca462012-10-13 10:46:48 +010017#include <uapi/linux/perf_event.h>
Hendrik Bruecknerc895f6f2017-12-04 10:56:44 +010018#include <uapi/linux/bpf_perf_event.h>
Peter Zijlstraa4be7c22009-08-19 11:18:27 +020019
Paul Mackerrasd859e292009-01-17 18:10:22 +110020/*
Paul Mackerrasf3dfd262009-02-26 22:43:46 +110021 * Kernel-internal data types and definitions:
Ingo Molnar9f66a382008-12-10 12:33:23 +010022 */
23
Ingo Molnarcdd6c482009-09-21 12:02:48 +020024#ifdef CONFIG_PERF_EVENTS
25# include <asm/perf_event.h>
Peter Zijlstra7be79232010-06-09 11:57:23 +020026# include <asm/local64.h>
Paul Mackerrasf3dfd262009-02-26 22:43:46 +110027#endif
28
Zhang, Yanmin39447b32010-04-19 13:32:41 +080029struct perf_guest_info_callbacks {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020030 int (*is_in_guest)(void);
31 int (*is_user_mode)(void);
32 unsigned long (*get_guest_ip)(void);
Luwei Kang8479e042019-02-18 19:26:07 -050033 void (*handle_intel_pt_intr)(void);
Zhang, Yanmin39447b32010-04-19 13:32:41 +080034};
35
Arnd Bergmann2ff6cfd2009-12-07 17:12:58 +010036#ifdef CONFIG_HAVE_HW_BREAKPOINT
37#include <asm/hw_breakpoint.h>
38#endif
39
Paul Mackerrasf3dfd262009-02-26 22:43:46 +110040#include <linux/list.h>
41#include <linux/mutex.h>
42#include <linux/rculist.h>
43#include <linux/rcupdate.h>
44#include <linux/spinlock.h>
Peter Zijlstrad6d020e2009-03-13 12:21:35 +010045#include <linux/hrtimer.h>
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +020046#include <linux/fs.h>
Peter Zijlstra709e50c2009-06-02 14:13:15 +020047#include <linux/pid_namespace.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020048#include <linux/workqueue.h>
Frederic Weisbecker5331d7b2010-03-04 21:15:56 +010049#include <linux/ftrace.h>
Peter Zijlstra85cfabb2010-03-11 13:06:56 +010050#include <linux/cpu.h>
Peter Zijlstrae360adb2010-10-14 14:01:34 +080051#include <linux/irq_work.h>
Ingo Molnarc5905af2012-02-24 08:31:31 +010052#include <linux/static_key.h>
Andrew Jones851cf6e2013-08-09 19:51:57 +053053#include <linux/jump_label_ratelimit.h>
Arun Sharma600634972011-07-26 16:09:06 -070054#include <linux/atomic.h>
Jiri Olsa641cc932012-03-15 20:09:14 +010055#include <linux/sysfs.h>
Jiri Olsa40189942012-08-07 15:20:37 +020056#include <linux/perf_regs.h>
Matt Fleming39bed6c2015-01-23 18:45:40 +000057#include <linux/cgroup.h>
Elena Reshetova8c94abb2019-01-28 14:27:26 +020058#include <linux/refcount.h>
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040059#include <linux/security.h>
Peter Zijlstrafa588152010-05-18 10:54:20 +020060#include <asm/local.h>
Paul Mackerrasf3dfd262009-02-26 22:43:46 +110061
Peter Zijlstraf9188e02009-06-18 22:20:52 +020062struct perf_callchain_entry {
63 __u64 nr;
Arnaldo Carvalho de Meloc5dfd782016-04-21 12:28:50 -030064 __u64 ip[0]; /* /proc/sys/kernel/perf_event_max_stack */
Peter Zijlstraf9188e02009-06-18 22:20:52 +020065};
66
Arnaldo Carvalho de Melocfbcf462016-04-28 12:30:53 -030067struct perf_callchain_entry_ctx {
68 struct perf_callchain_entry *entry;
69 u32 max_stack;
Arnaldo Carvalho de Melo3b1fff02016-05-10 18:08:32 -030070 u32 nr;
Arnaldo Carvalho de Meloc85b0332016-05-12 13:06:21 -030071 short contexts;
72 bool contexts_maxed;
Arnaldo Carvalho de Melocfbcf462016-04-28 12:30:53 -030073};
74
Daniel Borkmann7e3f9772016-07-14 18:08:03 +020075typedef unsigned long (*perf_copy_f)(void *dst, const void *src,
Daniel Borkmannaa7145c2016-07-22 01:19:42 +020076 unsigned long off, unsigned long len);
Daniel Borkmann7e3f9772016-07-14 18:08:03 +020077
78struct perf_raw_frag {
79 union {
80 struct perf_raw_frag *next;
81 unsigned long pad;
82 };
83 perf_copy_f copy;
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +020084 void *data;
Daniel Borkmann7e3f9772016-07-14 18:08:03 +020085 u32 size;
86} __packed;
87
88struct perf_raw_record {
89 struct perf_raw_frag frag;
90 u32 size;
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +020091};
92
Stephane Eranianbce38cd2012-02-09 23:20:51 +010093/*
Stephane Eranianbce38cd2012-02-09 23:20:51 +010094 * branch stack layout:
95 * nr: number of taken branches stored in entries[]
96 *
97 * Note that nr can vary from sample to sample
98 * branches (to, from) are stored from most recent
99 * to least recent, i.e., entries[0] contains the most
100 * recent branch.
101 */
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100102struct perf_branch_stack {
103 __u64 nr;
104 struct perf_branch_entry entries[0];
105};
106
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100107struct task_struct;
108
Stephane Eranianefc9f052011-06-06 16:57:03 +0200109/*
110 * extra PMU register associated with an event
111 */
112struct hw_perf_event_extra {
113 u64 config; /* register value */
114 unsigned int reg; /* register address or index */
115 int alloc; /* extra register already allocated */
116 int idx; /* index in shared_regs->regs[] */
117};
118
Thomas Gleixner0793a612008-12-04 20:12:29 +0100119/**
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200120 * struct hw_perf_event - performance event hardware details:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100121 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200122struct hw_perf_event {
123#ifdef CONFIG_PERF_EVENTS
Peter Zijlstrad6d020e2009-03-13 12:21:35 +0100124 union {
125 struct { /* hardware */
Ingo Molnara3084442009-06-11 14:44:26 +0200126 u64 config;
Stephane Eranian447a1942010-02-01 14:50:01 +0200127 u64 last_tag;
Ingo Molnara3084442009-06-11 14:44:26 +0200128 unsigned long config_base;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200129 unsigned long event_base;
Vince Weaverc48b6052012-03-01 17:28:14 -0500130 int event_base_rdpmc;
Ingo Molnara3084442009-06-11 14:44:26 +0200131 int idx;
Stephane Eranian447a1942010-02-01 14:50:01 +0200132 int last_cpu;
Stephane Eranian9fac2cf2013-01-24 16:10:27 +0100133 int flags;
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100134
Stephane Eranianefc9f052011-06-06 16:57:03 +0200135 struct hw_perf_event_extra extra_reg;
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100136 struct hw_perf_event_extra branch_reg;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +0100137 };
Soeren Sandmann721a6692009-09-15 14:33:08 +0200138 struct { /* software */
Ingo Molnara3084442009-06-11 14:44:26 +0200139 struct hrtimer hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +0100140 };
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +0100141 struct { /* tracepoint */
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +0100142 /* for tp_event->class */
143 struct list_head tp_list;
144 };
Huang Ruic7ab62b2016-03-09 13:45:06 +0800145 struct { /* amd_power */
146 u64 pwr_acc;
147 u64 ptsc;
148 };
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200149#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbecker45a73372010-06-23 23:00:37 +0200150 struct { /* breakpoint */
Peter Zijlstrad580ff82010-10-14 17:43:23 +0200151 /*
152 * Crufty hack to avoid the chicken and egg
153 * problem hw_breakpoint has with context
154 * creation and event initalization.
155 */
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +0100156 struct arch_hw_breakpoint info;
157 struct list_head bp_list;
Frederic Weisbecker45a73372010-06-23 23:00:37 +0200158 };
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200159#endif
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600160 struct { /* amd_iommu */
161 u8 iommu_bank;
162 u8 iommu_cntr;
163 u16 padding;
164 u64 conf;
165 u64 conf1;
166 };
Peter Zijlstrad6d020e2009-03-13 12:21:35 +0100167 };
Peter Zijlstrab0e87872015-08-28 14:06:07 +0200168 /*
169 * If the event is a per task event, this will point to the task in
170 * question. See the comment in perf_event_alloc().
171 */
Peter Zijlstra50f16a82015-03-05 22:10:19 +0100172 struct task_struct *target;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100173
Alexander Shishkin375637b2016-04-27 18:44:46 +0300174 /*
175 * PMU would store hardware filter configuration
176 * here.
177 */
178 void *addr_filters;
179
180 /* Last sync'ed generation of filters */
181 unsigned long addr_filters_gen;
182
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200183/*
Peter Zijlstrab0e87872015-08-28 14:06:07 +0200184 * hw_perf_event::state flags; used to track the PERF_EF_* state.
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200185 */
186#define PERF_HES_STOPPED 0x01 /* the counter is stopped */
187#define PERF_HES_UPTODATE 0x02 /* event->count up-to-date */
188#define PERF_HES_ARCH 0x04
189
Peter Zijlstrab0e87872015-08-28 14:06:07 +0200190 int state;
191
192 /*
193 * The last observed hardware counter value, updated with a
194 * local64_cmpxchg() such that pmu::read() can be called nested.
195 */
196 local64_t prev_count;
197
198 /*
199 * The period to start the next sample with.
200 */
201 u64 sample_period;
202
203 /*
204 * The period we started this sample with.
205 */
206 u64 last_period;
207
208 /*
209 * However much is left of the current period; note that this is
210 * a full 64bit value and allows for generation of periods longer
211 * than hardware might allow.
212 */
213 local64_t period_left;
214
215 /*
216 * State for throttling the event, see __perf_event_overflow() and
217 * perf_adjust_freq_unthr_context().
218 */
219 u64 interrupts_seq;
220 u64 interrupts;
221
222 /*
223 * State for freq target events, see __perf_event_overflow() and
224 * perf_adjust_freq_unthr_context().
225 */
226 u64 freq_time_stamp;
227 u64 freq_count_stamp;
228#endif
229};
230
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200231struct perf_event;
Ingo Molnar621a01e2008-12-11 12:46:46 +0100232
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +0200233/*
234 * Common implementation detail of pmu::{start,commit,cancel}_txn
235 */
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -0700236#define PERF_PMU_TXN_ADD 0x1 /* txn to add/schedule event on PMU */
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -0700237#define PERF_PMU_TXN_READ 0x2 /* txn to read event group from PMU */
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -0700238
Ingo Molnar621a01e2008-12-11 12:46:46 +0100239/**
Vince Weaver53b25332014-05-16 17:12:12 -0400240 * pmu::capabilities flags
241 */
242#define PERF_PMU_CAP_NO_INTERRUPT 0x01
Peter Zijlstra34f43922015-02-20 14:05:38 +0100243#define PERF_PMU_CAP_NO_NMI 0x02
Alexander Shishkin0a4e38e2015-01-14 14:18:12 +0200244#define PERF_PMU_CAP_AUX_NO_SG 0x04
Kan Liange321d022019-05-28 15:08:30 -0700245#define PERF_PMU_CAP_EXTENDED_REGS 0x08
Alexander Shishkinbed5b252015-01-30 12:31:06 +0200246#define PERF_PMU_CAP_EXCLUSIVE 0x10
Alexander Shishkinec0d7722015-01-14 14:18:23 +0200247#define PERF_PMU_CAP_ITRACE 0x20
Mark Rutland5101ef22016-04-26 11:33:46 +0100248#define PERF_PMU_CAP_HETEROGENEOUS_CPUS 0x40
Andrew Murraycc6795a2019-01-10 13:53:25 +0000249#define PERF_PMU_CAP_NO_EXCLUDE 0x80
Alexander Shishkinab437622019-08-06 11:46:00 +0300250#define PERF_PMU_CAP_AUX_OUTPUT 0x100
Vince Weaver53b25332014-05-16 17:12:12 -0400251
252/**
Robert Richter4aeb0b42009-04-29 12:47:03 +0200253 * struct pmu - generic performance monitoring unit
Ingo Molnar621a01e2008-12-11 12:46:46 +0100254 */
Robert Richter4aeb0b42009-04-29 12:47:03 +0200255struct pmu {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200256 struct list_head entry;
257
Yan, Zhengc464c762014-03-18 16:56:41 +0800258 struct module *module;
Peter Zijlstraabe43402010-11-17 23:17:37 +0100259 struct device *dev;
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +0100260 const struct attribute_group **attr_groups;
Jiri Olsaf3a3a822019-05-12 17:55:11 +0200261 const struct attribute_group **attr_update;
Mischa Jonker03d8e802013-06-04 11:45:48 +0200262 const char *name;
Peter Zijlstra2e80a822010-11-17 23:17:36 +0100263 int type;
264
Vince Weaver53b25332014-05-16 17:12:12 -0400265 /*
266 * various common per-pmu feature flags
267 */
268 int capabilities;
269
Mukesh Ojha43b9e4f2018-11-27 14:43:32 +0530270 int __percpu *pmu_disable_count;
271 struct perf_cpu_context __percpu *pmu_cpu_context;
Alexander Shishkinbed5b252015-01-30 12:31:06 +0200272 atomic_t exclusive_cnt; /* < 0: cpu; > 0: tsk */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200273 int task_ctx_nr;
Stephane Eranian62b85632013-04-03 14:21:34 +0200274 int hrtimer_interval_ms;
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200275
Alexander Shishkin375637b2016-04-27 18:44:46 +0300276 /* number of address filters this PMU can do */
277 unsigned int nr_addr_filters;
278
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200279 /*
280 * Fully disable/enable this PMU, can be used to protect from the PMI
281 * as well as for lazy/batch writing of the MSRs.
282 */
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200283 void (*pmu_enable) (struct pmu *pmu); /* optional */
284 void (*pmu_disable) (struct pmu *pmu); /* optional */
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200285
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200286 /*
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200287 * Try and initialize the event for this PMU.
Peter Zijlstrab0e87872015-08-28 14:06:07 +0200288 *
289 * Returns:
290 * -ENOENT -- @event is not for this PMU
291 *
292 * -ENODEV -- @event is for this PMU but PMU not present
293 * -EBUSY -- @event is for this PMU but PMU temporarily unavailable
294 * -EINVAL -- @event is for this PMU but @event is not valid
295 * -EOPNOTSUPP -- @event is for this PMU, @event is valid, but not supported
Geert Uytterhoeven652521d2019-10-24 14:29:04 +0200296 * -EACCES -- @event is for this PMU, @event is valid, but no privileges
Peter Zijlstrab0e87872015-08-28 14:06:07 +0200297 *
298 * 0 -- @event is for this PMU and valid
299 *
300 * Other error return values are allowed.
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200301 */
302 int (*event_init) (struct perf_event *event);
303
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -0700304 /*
305 * Notification that the event was mapped or unmapped. Called
306 * in the context of the mapping task.
307 */
Peter Zijlstrabfe334922017-08-02 19:39:30 +0200308 void (*event_mapped) (struct perf_event *event, struct mm_struct *mm); /* optional */
309 void (*event_unmapped) (struct perf_event *event, struct mm_struct *mm); /* optional */
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -0700310
Peter Zijlstrab0e87872015-08-28 14:06:07 +0200311 /*
312 * Flags for ->add()/->del()/ ->start()/->stop(). There are
313 * matching hw_perf_event::state flags.
314 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200315#define PERF_EF_START 0x01 /* start the counter when adding */
316#define PERF_EF_RELOAD 0x02 /* reload the counter when starting */
317#define PERF_EF_UPDATE 0x04 /* update the counter when stopping */
318
319 /*
Peter Zijlstrab0e87872015-08-28 14:06:07 +0200320 * Adds/Removes a counter to/from the PMU, can be done inside a
321 * transaction, see the ->*_txn() methods.
322 *
323 * The add/del callbacks will reserve all hardware resources required
324 * to service the event, this includes any counter constraint
325 * scheduling etc.
326 *
327 * Called with IRQs disabled and the PMU disabled on the CPU the event
328 * is on.
329 *
330 * ->add() called without PERF_EF_START should result in the same state
331 * as ->add() followed by ->stop().
332 *
333 * ->del() must always PERF_EF_UPDATE stop an event. If it calls
334 * ->stop() that must deal with already being stopped without
335 * PERF_EF_UPDATE.
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200336 */
337 int (*add) (struct perf_event *event, int flags);
338 void (*del) (struct perf_event *event, int flags);
339
340 /*
Peter Zijlstrab0e87872015-08-28 14:06:07 +0200341 * Starts/Stops a counter present on the PMU.
342 *
343 * The PMI handler should stop the counter when perf_event_overflow()
344 * returns !0. ->start() will be used to continue.
345 *
346 * Also used to change the sample period.
347 *
348 * Called with IRQs disabled and the PMU disabled on the CPU the event
349 * is on -- will be called from NMI context with the PMU generates
350 * NMIs.
351 *
352 * ->stop() with PERF_EF_UPDATE will read the counter and update
353 * period/count values like ->read() would.
354 *
355 * ->start() with PERF_EF_RELOAD will reprogram the the counter
356 * value, must be preceded by a ->stop() with PERF_EF_UPDATE.
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200357 */
358 void (*start) (struct perf_event *event, int flags);
359 void (*stop) (struct perf_event *event, int flags);
360
361 /*
362 * Updates the counter value of the event.
Peter Zijlstrab0e87872015-08-28 14:06:07 +0200363 *
364 * For sampling capable PMUs this will also update the software period
365 * hw_perf_event::period_left field.
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200366 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200367 void (*read) (struct perf_event *event);
Lin Ming6bde9b62010-04-23 13:56:00 +0800368
369 /*
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200370 * Group events scheduling is treated as a transaction, add
371 * group events as a whole and perform one schedulability test.
372 * If the test fails, roll back the whole group
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200373 *
374 * Start the transaction, after this ->add() doesn't need to
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200375 * do schedulability tests.
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -0700376 *
377 * Optional.
Lin Ming6bde9b62010-04-23 13:56:00 +0800378 */
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -0700379 void (*start_txn) (struct pmu *pmu, unsigned int txn_flags);
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +0200380 /*
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200381 * If ->start_txn() disabled the ->add() schedulability test
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +0200382 * then ->commit_txn() is required to perform one. On success
383 * the transaction is closed. On error the transaction is kept
384 * open until ->cancel_txn() is called.
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -0700385 *
386 * Optional.
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +0200387 */
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -0700388 int (*commit_txn) (struct pmu *pmu);
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +0200389 /*
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200390 * Will cancel the transaction, assumes ->del() is called
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300391 * for each successful ->add() during the transaction.
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -0700392 *
393 * Optional.
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +0200394 */
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -0700395 void (*cancel_txn) (struct pmu *pmu);
Peter Zijlstra35edc2a2011-11-20 20:36:02 +0100396
397 /*
398 * Will return the value for perf_event_mmap_page::index for this event,
399 * if no implementation is provided it will default to: event->hw.idx + 1.
400 */
401 int (*event_idx) (struct perf_event *event); /*optional */
Stephane Eraniand010b332012-02-09 23:21:00 +0100402
403 /*
Yan, Zhengba532502014-11-04 21:55:58 -0500404 * context-switches callback
405 */
406 void (*sched_task) (struct perf_event_context *ctx,
407 bool sched_in);
Yan, Zheng4af57ef2014-11-04 21:56:01 -0500408 /*
409 * PMU specific data size
410 */
411 size_t task_ctx_size;
Yan, Zhengba532502014-11-04 21:55:58 -0500412
Alexey Budankovfc1adfe2019-10-23 10:11:04 +0300413 /*
414 * PMU specific parts of task perf event context (i.e. ctx->task_ctx_data)
415 * can be synchronized using this function. See Intel LBR callstack support
416 * implementation and Perf core context switch handling callbacks for usage
417 * examples.
418 */
419 void (*swap_task_ctx) (struct perf_event_context *prev,
420 struct perf_event_context *next);
421 /* optional */
Matt Flemingeacd3ec2015-01-23 18:45:41 +0000422
423 /*
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +0200424 * Set up pmu-private data structures for an AUX area
425 */
Mathieu Poirier84001862019-01-31 11:47:08 -0700426 void *(*setup_aux) (struct perf_event *event, void **pages,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +0200427 int nr_pages, bool overwrite);
428 /* optional */
429
430 /*
431 * Free pmu-private AUX data structures
432 */
433 void (*free_aux) (void *aux); /* optional */
Mark Rutland66eb5792015-05-13 17:12:23 +0100434
435 /*
Alexander Shishkin375637b2016-04-27 18:44:46 +0300436 * Validate address range filters: make sure the HW supports the
437 * requested configuration and number of filters; return 0 if the
438 * supplied filters are valid, -errno otherwise.
439 *
440 * Runs in the context of the ioctl()ing process and is not serialized
441 * with the rest of the PMU callbacks.
442 */
443 int (*addr_filters_validate) (struct list_head *filters);
444 /* optional */
445
446 /*
447 * Synchronize address range filter configuration:
448 * translate hw-agnostic filters into hardware configuration in
449 * event::hw::addr_filters.
450 *
451 * Runs as a part of filter sync sequence that is done in ->start()
452 * callback by calling perf_event_addr_filters_sync().
453 *
454 * May (and should) traverse event::addr_filters::list, for which its
455 * caller provides necessary serialization.
456 */
457 void (*addr_filters_sync) (struct perf_event *event);
458 /* optional */
459
460 /*
Alexander Shishkinab437622019-08-06 11:46:00 +0300461 * Check if event can be used for aux_output purposes for
462 * events of this PMU.
463 *
464 * Runs from perf_event_open(). Should return 0 for "no match"
465 * or non-zero for "match".
466 */
467 int (*aux_output_match) (struct perf_event *event);
468 /* optional */
469
470 /*
Mark Rutland66eb5792015-05-13 17:12:23 +0100471 * Filter events for PMU-specific reasons.
472 */
473 int (*filter_match) (struct perf_event *event); /* optional */
Jiri Olsa81ec3f32019-02-04 13:35:32 +0100474
475 /*
476 * Check period value for PERF_EVENT_IOC_PERIOD ioctl.
477 */
478 int (*check_period) (struct perf_event *event, u64 value); /* optional */
Ingo Molnar621a01e2008-12-11 12:46:46 +0100479};
480
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +0300481enum perf_addr_filter_action_t {
482 PERF_ADDR_FILTER_ACTION_STOP = 0,
483 PERF_ADDR_FILTER_ACTION_START,
484 PERF_ADDR_FILTER_ACTION_FILTER,
485};
486
Thomas Gleixner0793a612008-12-04 20:12:29 +0100487/**
Alexander Shishkin375637b2016-04-27 18:44:46 +0300488 * struct perf_addr_filter - address range filter definition
489 * @entry: event's filter list linkage
Shaokun Zhang1279e412019-04-03 14:54:24 +0800490 * @path: object file's path for file-based filters
Alexander Shishkin375637b2016-04-27 18:44:46 +0300491 * @offset: filter range offset
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +0300492 * @size: filter range size (size==0 means single address trigger)
493 * @action: filter/start/stop
Alexander Shishkin375637b2016-04-27 18:44:46 +0300494 *
495 * This is a hardware-agnostic filter configuration as specified by the user.
496 */
497struct perf_addr_filter {
498 struct list_head entry;
Song Liu9511bce2018-04-17 23:29:07 -0700499 struct path path;
Alexander Shishkin375637b2016-04-27 18:44:46 +0300500 unsigned long offset;
501 unsigned long size;
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +0300502 enum perf_addr_filter_action_t action;
Alexander Shishkin375637b2016-04-27 18:44:46 +0300503};
504
505/**
506 * struct perf_addr_filters_head - container for address range filters
507 * @list: list of filters for this event
508 * @lock: spinlock that serializes accesses to the @list and event's
509 * (and its children's) filter generations.
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +0200510 * @nr_file_filters: number of file-based filters
Alexander Shishkin375637b2016-04-27 18:44:46 +0300511 *
512 * A child event will use parent's @list (and therefore @lock), so they are
513 * bundled together; see perf_event_addr_filters().
514 */
515struct perf_addr_filters_head {
516 struct list_head list;
517 raw_spinlock_t lock;
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +0200518 unsigned int nr_file_filters;
Alexander Shishkin375637b2016-04-27 18:44:46 +0300519};
520
Alexander Shishkinc60f83b2019-02-15 13:56:55 +0200521struct perf_addr_filter_range {
522 unsigned long start;
523 unsigned long size;
524};
525
Alexander Shishkin375637b2016-04-27 18:44:46 +0300526/**
Tobias Tefke788faab2018-07-09 12:57:15 +0200527 * enum perf_event_state - the states of an event:
Ingo Molnar6a930702008-12-11 15:17:03 +0100528 */
Peter Zijlstra8ca2bd42017-09-05 14:12:35 +0200529enum perf_event_state {
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +0100530 PERF_EVENT_STATE_DEAD = -4,
Jiri Olsa179033b2014-08-07 11:48:26 -0400531 PERF_EVENT_STATE_EXIT = -3,
Ingo Molnar57c0c152009-09-21 12:20:38 +0200532 PERF_EVENT_STATE_ERROR = -2,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200533 PERF_EVENT_STATE_OFF = -1,
534 PERF_EVENT_STATE_INACTIVE = 0,
Ingo Molnar57c0c152009-09-21 12:20:38 +0200535 PERF_EVENT_STATE_ACTIVE = 1,
Ingo Molnar6a930702008-12-11 15:17:03 +0100536};
537
Ingo Molnar9b51f662008-12-12 13:49:45 +0100538struct file;
Peter Zijlstra453f19e2009-11-20 22:19:43 +0100539struct perf_sample_data;
540
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200541typedef void (*perf_overflow_handler_t)(struct perf_event *,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100542 struct perf_sample_data *,
543 struct pt_regs *regs);
544
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -0700545/*
546 * Event capabilities. For event_caps and groups caps.
547 *
548 * PERF_EV_CAP_SOFTWARE: Is a software event.
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -0700549 * PERF_EV_CAP_READ_ACTIVE_PKG: A CPU event (or cgroup event) that can be read
550 * from any CPU in the package where it is active.
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -0700551 */
552#define PERF_EV_CAP_SOFTWARE BIT(0)
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -0700553#define PERF_EV_CAP_READ_ACTIVE_PKG BIT(1)
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100554
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200555#define SWEVENT_HLIST_BITS 8
556#define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +0200557
558struct swevent_hlist {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200559 struct hlist_head heads[SWEVENT_HLIST_SIZE];
560 struct rcu_head rcu_head;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +0200561};
562
Peter Zijlstra8a495422010-05-27 15:47:49 +0200563#define PERF_ATTACH_CONTEXT 0x01
564#define PERF_ATTACH_GROUP 0x02
Peter Zijlstrad580ff82010-10-14 17:43:23 +0200565#define PERF_ATTACH_TASK 0x04
Yan, Zheng4af57ef2014-11-04 21:56:01 -0500566#define PERF_ATTACH_TASK_DATA 0x08
Alexander Shishkin8d4e6c42017-03-30 18:39:56 +0300567#define PERF_ATTACH_ITRACE 0x10
Peter Zijlstra8a495422010-05-27 15:47:49 +0200568
Li Zefan877c6852013-03-05 11:38:08 +0800569struct perf_cgroup;
Frederic Weisbecker76369132011-05-19 19:55:04 +0200570struct ring_buffer;
571
Kan Liangf2fb6be2016-03-23 11:24:37 -0700572struct pmu_event_list {
573 raw_spinlock_t lock;
574 struct list_head list;
575};
576
Peter Zijlstraedb39592018-03-15 17:36:56 +0100577#define for_each_sibling_event(sibling, event) \
578 if ((event)->group_leader == (event)) \
579 list_for_each_entry((sibling), &(event)->sibling_list, sibling_list)
580
Ingo Molnar6a930702008-12-11 15:17:03 +0100581/**
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200582 * struct perf_event - performance event kernel representation:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100583 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200584struct perf_event {
585#ifdef CONFIG_PERF_EVENTS
Peter Zijlstra98861672013-10-03 16:02:23 +0200586 /*
587 * entry onto perf_event_context::event_list;
588 * modifications require ctx->lock
589 * RCU safe iterations.
590 */
Peter Zijlstra592903c2009-03-13 12:21:36 +0100591 struct list_head event_entry;
Peter Zijlstra98861672013-10-03 16:02:23 +0200592
593 /*
Peter Zijlstra98861672013-10-03 16:02:23 +0200594 * Locked for modification by both ctx->mutex and ctx->lock; holding
595 * either sufficies for read.
596 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100597 struct list_head sibling_list;
Peter Zijlstra66681282017-11-13 14:28:38 +0100598 struct list_head active_list;
Alexey Budankov8e1a2032017-09-08 11:47:03 +0300599 /*
600 * Node on the pinned or flexible tree located at the event context;
601 */
602 struct rb_node group_node;
603 u64 group_index;
Peter Zijlstra98861672013-10-03 16:02:23 +0200604 /*
605 * We need storage to track the entries in perf_pmu_migrate_context; we
606 * cannot use the event_entry because of RCU and we want to keep the
607 * group in tact which avoids us using the other two entries.
608 */
609 struct list_head migrate_entry;
610
Stephane Eranianf3ae75d2014-01-08 11:15:52 +0100611 struct hlist_node hlist_entry;
612 struct list_head active_entry;
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200613 int nr_siblings;
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -0700614
615 /* Not serialized. Only written during event initialization. */
616 int event_caps;
617 /* The cumulative AND of all event_caps for events in this group. */
618 int group_caps;
619
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200620 struct perf_event *group_leader;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200621 struct pmu *pmu;
Thomas Gleixner54d751d2016-02-22 22:19:14 +0000622 void *pmu_private;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100623
Peter Zijlstra8ca2bd42017-09-05 14:12:35 +0200624 enum perf_event_state state;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200625 unsigned int attach_state;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200626 local64_t count;
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +0200627 atomic64_t child_count;
Ingo Molnaree060942008-12-13 09:00:03 +0100628
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100629 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200630 * These are the total time in nanoseconds that the event
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100631 * has been enabled (i.e. eligible to run, and the task has
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200632 * been scheduled in, if this is a per-task event)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100633 * and running (scheduled onto the CPU), respectively.
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100634 */
635 u64 total_time_enabled;
636 u64 total_time_running;
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +0200637 u64 tstamp;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100638
Stephane Eranianeed01522010-10-26 16:08:01 +0200639 /*
640 * timestamp shadows the actual context timing but it can
641 * be safely used in NMI interrupt context. It reflects the
642 * context time as it was when the event was last scheduled in.
643 *
644 * ctx_time already accounts for ctx->timestamp. Therefore to
645 * compute ctx_time for a sample, simply add perf_clock().
646 */
647 u64 shadow_ctx_time;
648
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200649 struct perf_event_attr attr;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200650 u16 header_size;
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200651 u16 id_header_size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200652 u16 read_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200653 struct hw_perf_event hw;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100654
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200655 struct perf_event_context *ctx;
Al Viroa6fa9412012-08-20 14:59:25 +0100656 atomic_long_t refcount;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100657
658 /*
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100659 * These accumulate total time (in nanoseconds) that children
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200660 * events have been enabled and running, respectively.
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100661 */
662 atomic64_t child_total_time_enabled;
663 atomic64_t child_total_time_running;
664
665 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100666 * Protect attach/detach and child_list:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100667 */
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200668 struct mutex child_mutex;
669 struct list_head child_list;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200670 struct perf_event *parent;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100671
672 int oncpu;
673 int cpu;
674
Peter Zijlstra082ff5a2009-05-23 18:29:00 +0200675 struct list_head owner_entry;
676 struct task_struct *owner;
677
Peter Zijlstra7b732a72009-03-23 18:22:10 +0100678 /* mmap bits */
679 struct mutex mmap_mutex;
680 atomic_t mmap_count;
Peter Zijlstra26cb63a2013-05-28 10:55:48 +0200681
Frederic Weisbecker76369132011-05-19 19:55:04 +0200682 struct ring_buffer *rb;
Peter Zijlstra10c6db12011-11-26 02:47:31 +0100683 struct list_head rb_entry;
Peter Zijlstrab69cf532014-03-14 10:50:33 +0100684 unsigned long rcu_batches;
685 int rcu_pending;
Paul Mackerras37d81822009-03-23 18:22:08 +0100686
Peter Zijlstra7b732a72009-03-23 18:22:10 +0100687 /* poll related */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100688 wait_queue_head_t waitq;
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +0200689 struct fasync_struct *fasync;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200690
691 /* delayed work for NMIs and such */
692 int pending_wakeup;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +0200693 int pending_kill;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200694 int pending_disable;
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800695 struct irq_work pending;
Peter Zijlstra592903c2009-03-13 12:21:36 +0100696
Peter Zijlstra79f14642009-04-06 11:45:07 +0200697 atomic_t event_limit;
698
Alexander Shishkin375637b2016-04-27 18:44:46 +0300699 /* address range filters */
700 struct perf_addr_filters_head addr_filters;
701 /* vma address array for file-based filders */
Alexander Shishkinc60f83b2019-02-15 13:56:55 +0200702 struct perf_addr_filter_range *addr_filter_ranges;
Alexander Shishkin375637b2016-04-27 18:44:46 +0300703 unsigned long addr_filters_gen;
704
Alexander Shishkinab437622019-08-06 11:46:00 +0300705 /* for aux_output events */
706 struct perf_event *aux_event;
707
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200708 void (*destroy)(struct perf_event *);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100709 struct rcu_head rcu_head;
Peter Zijlstra709e50c2009-06-02 14:13:15 +0200710
711 struct pid_namespace *ns;
Peter Zijlstra8e5799b2009-06-02 15:08:15 +0200712 u64 id;
Li Zefan6fb29152009-10-15 11:21:42 +0800713
Peter Zijlstra34f43922015-02-20 14:05:38 +0100714 u64 (*clock)(void);
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100715 perf_overflow_handler_t overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +0300716 void *overflow_handler_context;
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -0700717#ifdef CONFIG_BPF_SYSCALL
718 perf_overflow_handler_t orig_overflow_handler;
719 struct bpf_prog *prog;
720#endif
Peter Zijlstra453f19e2009-11-20 22:19:43 +0100721
Li Zefan07b139c2009-12-21 14:27:35 +0800722#ifdef CONFIG_EVENT_TRACING
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400723 struct trace_event_call *tp_event;
Li Zefan6fb29152009-10-15 11:21:42 +0800724 struct event_filter *filter;
Jiri Olsaced39002012-02-15 15:51:52 +0100725#ifdef CONFIG_FUNCTION_TRACER
726 struct ftrace_ops ftrace_ops;
727#endif
Ingo Molnaree060942008-12-13 09:00:03 +0100728#endif
Li Zefan6fb29152009-10-15 11:21:42 +0800729
Stephane Eraniane5d13672011-02-14 11:20:01 +0200730#ifdef CONFIG_CGROUP_PERF
731 struct perf_cgroup *cgrp; /* cgroup event is attach to */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200732#endif
733
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -0400734#ifdef CONFIG_SECURITY
735 void *security;
736#endif
Kan Liangf2fb6be2016-03-23 11:24:37 -0700737 struct list_head sb_list;
Li Zefan6fb29152009-10-15 11:21:42 +0800738#endif /* CONFIG_PERF_EVENTS */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100739};
740
Alexey Budankov8e1a2032017-09-08 11:47:03 +0300741
742struct perf_event_groups {
743 struct rb_root tree;
744 u64 index;
745};
746
Thomas Gleixner0793a612008-12-04 20:12:29 +0100747/**
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200748 * struct perf_event_context - event context structure
Thomas Gleixner0793a612008-12-04 20:12:29 +0100749 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200750 * Used as a container for task events and CPU events as well:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100751 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200752struct perf_event_context {
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200753 struct pmu *pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100754 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200755 * Protect the states of the events in the list,
Paul Mackerrasd859e292009-01-17 18:10:22 +1100756 * nr_active, and the list:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100757 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100758 raw_spinlock_t lock;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100759 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200760 * Protect the list of events. Locking either mutex or lock
Paul Mackerrasd859e292009-01-17 18:10:22 +1100761 * is sufficient to ensure the list doesn't change; to change
762 * the list you need to lock both the mutex and the spinlock.
763 */
Ingo Molnara3084442009-06-11 14:44:26 +0200764 struct mutex mutex;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100765
Mark Rutland2fde4f92015-01-07 15:01:54 +0000766 struct list_head active_ctx_list;
Alexey Budankov8e1a2032017-09-08 11:47:03 +0300767 struct perf_event_groups pinned_groups;
768 struct perf_event_groups flexible_groups;
Ingo Molnara3084442009-06-11 14:44:26 +0200769 struct list_head event_list;
Peter Zijlstra66681282017-11-13 14:28:38 +0100770
771 struct list_head pinned_active;
772 struct list_head flexible_active;
773
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200774 int nr_events;
Ingo Molnara3084442009-06-11 14:44:26 +0200775 int nr_active;
776 int is_active;
Peter Zijlstrabfbd3382009-06-24 21:11:59 +0200777 int nr_stat;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +0100778 int nr_freq;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +0100779 int rotate_disable;
Ian Rogersfd7d5512019-06-01 01:27:22 -0700780 /*
781 * Set when nr_events != nr_active, except tolerant to events not
782 * necessary to be active due to scheduling constraints, such as cgroups.
783 */
784 int rotate_necessary;
Elena Reshetova8c94abb2019-01-28 14:27:26 +0200785 refcount_t refcount;
Ingo Molnara3084442009-06-11 14:44:26 +0200786 struct task_struct *task;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100787
788 /*
Peter Zijlstra4af49982009-04-06 11:45:10 +0200789 * Context clock, runs when context enabled.
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100790 */
Ingo Molnara3084442009-06-11 14:44:26 +0200791 u64 time;
792 u64 timestamp;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000793
794 /*
795 * These fields let us detect when two contexts have both
796 * been cloned (inherited) from a common ancestor.
797 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200798 struct perf_event_context *parent_ctx;
Ingo Molnara3084442009-06-11 14:44:26 +0200799 u64 parent_gen;
800 u64 generation;
801 int pin_count;
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700802#ifdef CONFIG_CGROUP_PERF
Stephane Eraniand010b332012-02-09 23:21:00 +0100803 int nr_cgroups; /* cgroup evts */
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700804#endif
Yan, Zheng4af57ef2014-11-04 21:56:01 -0500805 void *task_ctx_data; /* pmu specific data */
Richard Kennedy28009ce2011-06-07 16:33:38 +0100806 struct rcu_head rcu_head;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100807};
808
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +0200809/*
810 * Number of contexts where an event can trigger:
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200811 * task, softirq, hardirq, nmi.
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +0200812 */
813#define PERF_NR_CONTEXTS 4
814
Thomas Gleixner0793a612008-12-04 20:12:29 +0100815/**
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200816 * struct perf_event_cpu_context - per cpu event context structure
Thomas Gleixner0793a612008-12-04 20:12:29 +0100817 */
818struct perf_cpu_context {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200819 struct perf_event_context ctx;
820 struct perf_event_context *task_ctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100821 int active_oncpu;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100822 int exclusive;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200823
824 raw_spinlock_t hrtimer_lock;
Stephane Eranian9e630202013-04-03 14:21:33 +0200825 struct hrtimer hrtimer;
826 ktime_t hrtimer_interval;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200827 unsigned int hrtimer_active;
828
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700829#ifdef CONFIG_CGROUP_PERF
Stephane Eraniane5d13672011-02-14 11:20:01 +0200830 struct perf_cgroup *cgrp;
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800831 struct list_head cgrp_cpuctx_entry;
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700832#endif
Peter Zijlstrae48c1782016-07-06 09:18:30 +0200833
834 struct list_head sched_cb_entry;
835 int sched_cb_usage;
Thomas Gleixnera63fbed2017-05-24 10:15:34 +0200836
837 int online;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100838};
839
Markus Metzger5622f292009-09-15 13:00:23 +0200840struct perf_output_handle {
Ingo Molnar57c0c152009-09-21 12:20:38 +0200841 struct perf_event *event;
Frederic Weisbecker76369132011-05-19 19:55:04 +0200842 struct ring_buffer *rb;
Peter Zijlstra6d1acfd2010-05-18 11:12:48 +0200843 unsigned long wakeup;
Peter Zijlstra5d967a82010-05-20 16:46:39 +0200844 unsigned long size;
Will Deaconf4c0b0a2017-02-20 15:33:50 +0200845 u64 aux_flags;
Alexander Shishkinfdc26702015-01-14 14:18:16 +0200846 union {
847 void *addr;
848 unsigned long head;
849 };
Peter Zijlstra5d967a82010-05-20 16:46:39 +0200850 int page;
Markus Metzger5622f292009-09-15 13:00:23 +0200851};
852
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700853struct bpf_perf_event_data_kern {
Hendrik Bruecknerc895f6f2017-12-04 10:56:44 +0100854 bpf_user_pt_regs_t *regs;
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700855 struct perf_sample_data *data;
Yonghong Song7d9285e2017-10-05 09:19:19 -0700856 struct perf_event *event;
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700857};
858
Matt Fleming39bed6c2015-01-23 18:45:40 +0000859#ifdef CONFIG_CGROUP_PERF
860
861/*
862 * perf_cgroup_info keeps track of time_enabled for a cgroup.
863 * This is a per-cpu dynamically allocated data structure.
864 */
865struct perf_cgroup_info {
866 u64 time;
867 u64 timestamp;
868};
869
870struct perf_cgroup {
871 struct cgroup_subsys_state css;
872 struct perf_cgroup_info __percpu *info;
873};
874
875/*
876 * Must ensure cgroup is pinned (css_get) before calling
877 * this function. In other words, we cannot call this function
878 * if there is no cgroup event for the current CPU context.
879 */
880static inline struct perf_cgroup *
Stephane Eranian614e4c42015-11-12 11:00:04 +0100881perf_cgroup_from_task(struct task_struct *task, struct perf_event_context *ctx)
Matt Fleming39bed6c2015-01-23 18:45:40 +0000882{
Stephane Eranian614e4c42015-11-12 11:00:04 +0100883 return container_of(task_css_check(task, perf_event_cgrp_id,
884 ctx ? lockdep_is_held(&ctx->lock)
885 : true),
Matt Fleming39bed6c2015-01-23 18:45:40 +0000886 struct perf_cgroup, css);
887}
888#endif /* CONFIG_CGROUP_PERF */
889
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200890#ifdef CONFIG_PERF_EVENTS
Robert Richter829b42d2009-04-29 12:46:59 +0200891
Alexander Shishkinfdc26702015-01-14 14:18:16 +0200892extern void *perf_aux_output_begin(struct perf_output_handle *handle,
893 struct perf_event *event);
894extern void perf_aux_output_end(struct perf_output_handle *handle,
Will Deaconf4c0b0a2017-02-20 15:33:50 +0200895 unsigned long size);
Alexander Shishkinfdc26702015-01-14 14:18:16 +0200896extern int perf_aux_output_skip(struct perf_output_handle *handle,
897 unsigned long size);
898extern void *perf_get_aux(struct perf_output_handle *handle);
Will Deaconf4c0b0a2017-02-20 15:33:50 +0200899extern void perf_aux_output_flag(struct perf_output_handle *handle, u64 flags);
Alexander Shishkin8d4e6c42017-03-30 18:39:56 +0300900extern void perf_event_itrace_started(struct perf_event *event);
Alexander Shishkinfdc26702015-01-14 14:18:16 +0200901
Mischa Jonker03d8e802013-06-04 11:45:48 +0200902extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200903extern void perf_pmu_unregister(struct pmu *pmu);
Ingo Molnar621a01e2008-12-11 12:46:46 +0100904
Matt Fleming3bf101b2010-09-27 20:22:24 +0100905extern int perf_num_counters(void);
Matt Fleming84c79912010-10-03 21:41:13 +0100906extern const char *perf_pmu_name(void);
Jiri Olsaab0cce52012-05-23 13:13:02 +0200907extern void __perf_event_task_sched_in(struct task_struct *prev,
908 struct task_struct *task);
909extern void __perf_event_task_sched_out(struct task_struct *prev,
910 struct task_struct *next);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200911extern int perf_event_init_task(struct task_struct *child);
912extern void perf_event_exit_task(struct task_struct *child);
913extern void perf_event_free_task(struct task_struct *task);
Peter Zijlstra4e231c72010-09-09 21:01:59 +0200914extern void perf_event_delayed_put(struct task_struct *task);
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -0800915extern struct file *perf_event_get(unsigned int fd);
Yonghong Songf8d959a2018-05-24 11:21:08 -0700916extern const struct perf_event *perf_get_event(struct file *file);
Kaixu Xiaffe86902015-08-06 07:02:32 +0000917extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200918extern void perf_event_print_debug(void);
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200919extern void perf_pmu_disable(struct pmu *pmu);
920extern void perf_pmu_enable(struct pmu *pmu);
Yan, Zhengba532502014-11-04 21:55:58 -0500921extern void perf_sched_cb_dec(struct pmu *pmu);
922extern void perf_sched_cb_inc(struct pmu *pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200923extern int perf_event_task_disable(void);
924extern int perf_event_task_enable(void);
Stephane Eranianc68d2242019-04-08 10:32:51 -0700925
926extern void perf_pmu_resched(struct pmu *pmu);
927
Avi Kivity26ca5c12011-06-29 18:42:37 +0300928extern int perf_event_refresh(struct perf_event *event, int refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200929extern void perf_event_update_userpage(struct perf_event *event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +0200930extern int perf_event_release_kernel(struct perf_event *event);
931extern struct perf_event *
932perf_event_create_kernel_counter(struct perf_event_attr *attr,
933 int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -0700934 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +0300935 perf_overflow_handler_t callback,
936 void *context);
Yan, Zheng0cda4c02012-06-15 14:31:33 +0800937extern void perf_pmu_migrate_context(struct pmu *pmu,
938 int src_cpu, int dst_cpu);
Yonghong Song7d9285e2017-10-05 09:19:19 -0700939int perf_event_read_local(struct perf_event *event, u64 *value,
940 u64 *enabled, u64 *running);
Peter Zijlstra59ed4462009-11-20 22:19:55 +0100941extern u64 perf_event_read_value(struct perf_event *event,
942 u64 *enabled, u64 *running);
Ingo Molnar5c92d122008-12-11 13:21:10 +0100943
Stephane Eraniand010b332012-02-09 23:21:00 +0100944
Peter Zijlstradf1a1322009-06-10 21:02:22 +0200945struct perf_sample_data {
Peter Zijlstra25657112014-09-24 13:48:42 +0200946 /*
947 * Fields set by perf_sample_data_init(), group so as to
948 * minimize the cachelines touched.
949 */
950 u64 addr;
951 struct perf_raw_record *raw;
952 struct perf_branch_stack *br_stack;
953 u64 period;
954 u64 weight;
955 u64 txn;
956 union perf_mem_data_src data_src;
Markus Metzger5622f292009-09-15 13:00:23 +0200957
Peter Zijlstra25657112014-09-24 13:48:42 +0200958 /*
959 * The other fields, optionally {set,used} by
960 * perf_{prepare,output}_sample().
961 */
962 u64 type;
Markus Metzger5622f292009-09-15 13:00:23 +0200963 u64 ip;
964 struct {
965 u32 pid;
966 u32 tid;
967 } tid_entry;
968 u64 time;
Markus Metzger5622f292009-09-15 13:00:23 +0200969 u64 id;
970 u64 stream_id;
971 struct {
972 u32 cpu;
973 u32 reserved;
974 } cpu_entry;
Markus Metzger5622f292009-09-15 13:00:23 +0200975 struct perf_callchain_entry *callchain;
Andy Lutomirski88a7c262015-01-04 10:36:19 -0800976
977 /*
978 * regs_user may point to task_pt_regs or to regs_user_copy, depending
979 * on arch details.
980 */
Stephane Eranian60e23642014-09-24 13:48:37 +0200981 struct perf_regs regs_user;
Andy Lutomirski88a7c262015-01-04 10:36:19 -0800982 struct pt_regs regs_user_copy;
983
Stephane Eranian60e23642014-09-24 13:48:37 +0200984 struct perf_regs regs_intr;
Jiri Olsac5ebced2012-08-07 15:20:40 +0200985 u64 stack_user_size;
Kan Liangfc7ce9c2017-08-28 20:52:49 -0400986
987 u64 phys_addr;
Peter Zijlstra25657112014-09-24 13:48:42 +0200988} ____cacheline_aligned;
Peter Zijlstradf1a1322009-06-10 21:02:22 +0200989
Stephane Eranian770eee12014-08-11 21:27:12 +0200990/* default value for data source */
991#define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
992 PERF_MEM_S(LVL, NA) |\
993 PERF_MEM_S(SNOOP, NA) |\
994 PERF_MEM_S(LOCK, NA) |\
995 PERF_MEM_S(TLB, NA))
996
Robert Richterfd0d0002012-04-02 20:19:08 +0200997static inline void perf_sample_data_init(struct perf_sample_data *data,
998 u64 addr, u64 period)
Peter Zijlstradc1d6282010-03-03 15:55:04 +0100999{
Robert Richterfd0d0002012-04-02 20:19:08 +02001000 /* remaining struct members initialized in perf_prepare_sample() */
Peter Zijlstradc1d6282010-03-03 15:55:04 +01001001 data->addr = addr;
1002 data->raw = NULL;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01001003 data->br_stack = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02001004 data->period = period;
Andi Kleenc3feedf2013-01-24 16:10:28 +01001005 data->weight = 0;
Stephane Eranian770eee12014-08-11 21:27:12 +02001006 data->data_src.val = PERF_MEM_NA;
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001007 data->txn = 0;
Peter Zijlstradc1d6282010-03-03 15:55:04 +01001008}
1009
Markus Metzger5622f292009-09-15 13:00:23 +02001010extern void perf_output_sample(struct perf_output_handle *handle,
1011 struct perf_event_header *header,
1012 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001013 struct perf_event *event);
Markus Metzger5622f292009-09-15 13:00:23 +02001014extern void perf_prepare_sample(struct perf_event_header *header,
1015 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001016 struct perf_event *event,
Markus Metzger5622f292009-09-15 13:00:23 +02001017 struct pt_regs *regs);
1018
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001019extern int perf_event_overflow(struct perf_event *event,
Markus Metzger5622f292009-09-15 13:00:23 +02001020 struct perf_sample_data *data,
1021 struct pt_regs *regs);
Peter Zijlstradf1a1322009-06-10 21:02:22 +02001022
Wang Nan9ecda412016-04-05 14:11:18 +00001023extern void perf_event_output_forward(struct perf_event *event,
1024 struct perf_sample_data *data,
1025 struct pt_regs *regs);
1026extern void perf_event_output_backward(struct perf_event *event,
1027 struct perf_sample_data *data,
1028 struct pt_regs *regs);
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03001029extern int perf_event_output(struct perf_event *event,
1030 struct perf_sample_data *data,
1031 struct pt_regs *regs);
Yan, Zheng21509082015-05-06 15:33:49 -04001032
Wang Nan18794452016-03-28 06:41:30 +00001033static inline bool
1034is_default_overflow_handler(struct perf_event *event)
1035{
Wang Nan9ecda412016-04-05 14:11:18 +00001036 if (likely(event->overflow_handler == perf_event_output_forward))
1037 return true;
1038 if (unlikely(event->overflow_handler == perf_event_output_backward))
1039 return true;
1040 return false;
Wang Nan18794452016-03-28 06:41:30 +00001041}
1042
Yan, Zheng21509082015-05-06 15:33:49 -04001043extern void
1044perf_event_header__init_id(struct perf_event_header *header,
1045 struct perf_sample_data *data,
1046 struct perf_event *event);
1047extern void
1048perf_event__output_id_sample(struct perf_event *event,
1049 struct perf_output_handle *handle,
1050 struct perf_sample_data *sample);
1051
Kan Liangf38b0db2015-05-10 15:13:14 -04001052extern void
1053perf_log_lost_samples(struct perf_event *event, u64 lost);
1054
Andrew Murray486efe92019-01-10 13:53:24 +00001055static inline bool event_has_any_exclude_flag(struct perf_event *event)
1056{
1057 struct perf_event_attr *attr = &event->attr;
1058
1059 return attr->exclude_idle || attr->exclude_user ||
1060 attr->exclude_kernel || attr->exclude_hv ||
1061 attr->exclude_guest || attr->exclude_host;
1062}
1063
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01001064static inline bool is_sampling_event(struct perf_event *event)
1065{
1066 return event->attr.sample_period != 0;
1067}
1068
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001069/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001070 * Return 1 for a software event, 0 for a hardware event
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001071 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001072static inline int is_software_event(struct perf_event *event)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001073{
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001074 return event->event_caps & PERF_EV_CAP_SOFTWARE;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001075}
1076
Song Liua1150c22018-05-03 12:47:16 -07001077/*
1078 * Return 1 for event in sw context, 0 for event in hw context
1079 */
1080static inline int in_software_context(struct perf_event *event)
1081{
1082 return event->ctx->pmu->task_ctx_nr == perf_sw_context;
1083}
1084
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03001085static inline int is_exclusive_pmu(struct pmu *pmu)
1086{
1087 return pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE;
1088}
1089
Ingo Molnarc5905af2012-02-24 08:31:31 +01001090extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Peter Zijlstraf29ac752009-06-19 18:27:26 +02001091
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01001092extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001093extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
Peter Zijlstraf29ac752009-06-19 18:27:26 +02001094
Frederic Weisbeckerb0f82b82010-05-20 07:47:21 +02001095#ifndef perf_arch_fetch_caller_regs
Ingo Molnare7e7ee22011-05-04 08:42:29 +02001096static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
Frederic Weisbeckerb0f82b82010-05-20 07:47:21 +02001097#endif
Frederic Weisbecker5331d7b2010-03-04 21:15:56 +01001098
1099/*
Kairui Songd15d3562019-04-23 00:26:52 +08001100 * When generating a perf sample in-line, instead of from an interrupt /
1101 * exception, we lack a pt_regs. This is typically used from software events
1102 * like: SW_CONTEXT_SWITCHES, SW_MIGRATIONS and the tie-in with tracepoints.
1103 *
1104 * We typically don't need a full set, but (for x86) do require:
Frederic Weisbecker5331d7b2010-03-04 21:15:56 +01001105 * - ip for PERF_SAMPLE_IP
1106 * - cs for user_mode() tests
Kairui Songd15d3562019-04-23 00:26:52 +08001107 * - sp for PERF_SAMPLE_CALLCHAIN
1108 * - eflags for MISC bits and CALLCHAIN (see: perf_hw_regs())
1109 *
1110 * NOTE: assumes @regs is otherwise already 0 filled; this is important for
1111 * things like PERF_SAMPLE_REGS_INTR.
Frederic Weisbecker5331d7b2010-03-04 21:15:56 +01001112 */
Frederic Weisbeckerb0f82b82010-05-20 07:47:21 +02001113static inline void perf_fetch_caller_regs(struct pt_regs *regs)
Frederic Weisbecker5331d7b2010-03-04 21:15:56 +01001114{
Frederic Weisbeckerb0f82b82010-05-20 07:47:21 +02001115 perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
Frederic Weisbecker5331d7b2010-03-04 21:15:56 +01001116}
1117
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02001118static __always_inline void
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001119perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Frederic Weisbeckere49a5bd2010-03-22 19:40:03 +01001120{
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01001121 if (static_key_false(&perf_swevent_enabled[event_id]))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001122 __perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01001123}
1124
1125DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
1126
1127/*
1128 * 'Special' version for the scheduler, it hard assumes no recursion,
1129 * which is guaranteed by us not actually scheduling inside other swevents
1130 * because those disable preemption.
1131 */
1132static __always_inline void
1133perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
1134{
1135 if (static_key_false(&perf_swevent_enabled[event_id])) {
1136 struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
1137
1138 perf_fetch_caller_regs(regs);
1139 ___perf_sw_event(event_id, nr, regs, addr);
Frederic Weisbeckere49a5bd2010-03-22 19:40:03 +01001140 }
1141}
1142
Peter Zijlstra9107c892016-02-24 18:45:45 +01001143extern struct static_key_false perf_sched_events;
Peter Zijlstraee6dcfa2010-11-26 13:49:04 +01001144
Peter Zijlstraff303e62015-04-17 20:05:30 +02001145static __always_inline bool
1146perf_sw_migrate_enabled(void)
1147{
1148 if (static_key_false(&perf_swevent_enabled[PERF_COUNT_SW_CPU_MIGRATIONS]))
1149 return true;
1150 return false;
1151}
1152
1153static inline void perf_event_task_migrate(struct task_struct *task)
1154{
1155 if (perf_sw_migrate_enabled())
1156 task->sched_migrated = 1;
1157}
1158
Jiri Olsaab0cce52012-05-23 13:13:02 +02001159static inline void perf_event_task_sched_in(struct task_struct *prev,
Stephane Eraniana8d757e2011-08-25 15:58:03 +02001160 struct task_struct *task)
Peter Zijlstraee6dcfa2010-11-26 13:49:04 +01001161{
Peter Zijlstra9107c892016-02-24 18:45:45 +01001162 if (static_branch_unlikely(&perf_sched_events))
Jiri Olsaab0cce52012-05-23 13:13:02 +02001163 __perf_event_task_sched_in(prev, task);
Peter Zijlstraff303e62015-04-17 20:05:30 +02001164
1165 if (perf_sw_migrate_enabled() && task->sched_migrated) {
1166 struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
1167
1168 perf_fetch_caller_regs(regs);
1169 ___perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, regs, 0);
1170 task->sched_migrated = 0;
1171 }
Jiri Olsaab0cce52012-05-23 13:13:02 +02001172}
1173
1174static inline void perf_event_task_sched_out(struct task_struct *prev,
1175 struct task_struct *next)
1176{
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01001177 perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
Peter Zijlstraee6dcfa2010-11-26 13:49:04 +01001178
Peter Zijlstra9107c892016-02-24 18:45:45 +01001179 if (static_branch_unlikely(&perf_sched_events))
Jiri Olsaab0cce52012-05-23 13:13:02 +02001180 __perf_event_task_sched_out(prev, next);
Peter Zijlstraee6dcfa2010-11-26 13:49:04 +01001181}
1182
Eric B Munson3af9e852010-05-18 15:30:49 +01001183extern void perf_event_mmap(struct vm_area_struct *vma);
Song Liu76193a92019-01-17 08:15:13 -08001184
1185extern void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
1186 bool unregister, const char *sym);
Song Liu6ee52e22019-01-17 08:15:15 -08001187extern void perf_event_bpf_event(struct bpf_prog *prog,
1188 enum perf_bpf_event_type type,
1189 u16 flags);
Song Liu76193a92019-01-17 08:15:13 -08001190
Zhang, Yanmin39447b32010-04-19 13:32:41 +08001191extern struct perf_guest_info_callbacks *perf_guest_cbs;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08001192extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
1193extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
Zhang, Yanmin39447b32010-04-19 13:32:41 +08001194
Peter Zijlstrae041e322014-05-21 17:32:19 +02001195extern void perf_event_exec(void);
Adrian Hunter82b89772014-05-28 11:45:04 +03001196extern void perf_event_comm(struct task_struct *tsk, bool exec);
Hari Bathinie4222672017-03-08 02:11:36 +05301197extern void perf_event_namespaces(struct task_struct *tsk);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001198extern void perf_event_fork(struct task_struct *tsk);
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02001199
Frederic Weisbecker56962b4442010-06-30 23:03:51 +02001200/* Callchains */
1201DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
1202
Arnaldo Carvalho de Melocfbcf462016-04-28 12:30:53 -03001203extern void perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs);
1204extern void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs);
Alexei Starovoitov568b3292016-02-17 19:58:57 -08001205extern struct perf_callchain_entry *
1206get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
Arnaldo Carvalho de Melocfbcf462016-04-28 12:30:53 -03001207 u32 max_stack, bool crosstask, bool add_mark);
Peter Zijlstra6cbc3042018-05-10 15:48:41 +02001208extern struct perf_callchain_entry *perf_callchain(struct perf_event *event, struct pt_regs *regs);
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03001209extern int get_callchain_buffers(int max_stack);
Alexei Starovoitov568b3292016-02-17 19:58:57 -08001210extern void put_callchain_buffers(void);
Frederic Weisbecker56962b4442010-06-30 23:03:51 +02001211
Arnaldo Carvalho de Meloc5dfd782016-04-21 12:28:50 -03001212extern int sysctl_perf_event_max_stack;
Arnaldo Carvalho de Meloc85b0332016-05-12 13:06:21 -03001213extern int sysctl_perf_event_max_contexts_per_stack;
Arnaldo Carvalho de Meloc5dfd782016-04-21 12:28:50 -03001214
Arnaldo Carvalho de Meloc85b0332016-05-12 13:06:21 -03001215static inline int perf_callchain_store_context(struct perf_callchain_entry_ctx *ctx, u64 ip)
1216{
1217 if (ctx->contexts < sysctl_perf_event_max_contexts_per_stack) {
1218 struct perf_callchain_entry *entry = ctx->entry;
1219 entry->ip[entry->nr++] = ip;
1220 ++ctx->contexts;
1221 return 0;
1222 } else {
1223 ctx->contexts_maxed = true;
1224 return -1; /* no more room, stop walking the stack */
1225 }
1226}
Arnaldo Carvalho de Melo3e4de4e2016-05-12 13:01:50 -03001227
Arnaldo Carvalho de Melocfbcf462016-04-28 12:30:53 -03001228static inline int perf_callchain_store(struct perf_callchain_entry_ctx *ctx, u64 ip)
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02001229{
Arnaldo Carvalho de Meloc85b0332016-05-12 13:06:21 -03001230 if (ctx->nr < ctx->max_stack && !ctx->contexts_maxed) {
Arnaldo Carvalho de Melo3b1fff02016-05-10 18:08:32 -03001231 struct perf_callchain_entry *entry = ctx->entry;
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02001232 entry->ip[entry->nr++] = ip;
Arnaldo Carvalho de Melo3b1fff02016-05-10 18:08:32 -03001233 ++ctx->nr;
Alexei Starovoitov568b3292016-02-17 19:58:57 -08001234 return 0;
1235 } else {
1236 return -1; /* no more room, stop walking the stack */
1237 }
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02001238}
Peter Zijlstra394ee072009-03-30 19:07:14 +02001239
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001240extern int sysctl_perf_event_paranoid;
1241extern int sysctl_perf_event_mlock;
1242extern int sysctl_perf_event_sample_rate;
Dave Hansen14c63f12013-06-21 08:51:36 -07001243extern int sysctl_perf_cpu_time_max_percent;
1244
1245extern void perf_sample_event_took(u64 sample_len_ns);
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001246
Peter Zijlstra163ec432011-02-16 11:22:34 +01001247extern int perf_proc_update_handler(struct ctl_table *table, int write,
1248 void __user *buffer, size_t *lenp,
1249 loff_t *ppos);
Dave Hansen14c63f12013-06-21 08:51:36 -07001250extern int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
1251 void __user *buffer, size_t *lenp,
1252 loff_t *ppos);
1253
Arnaldo Carvalho de Meloc5dfd782016-04-21 12:28:50 -03001254int perf_event_max_stack_handler(struct ctl_table *table, int write,
1255 void __user *buffer, size_t *lenp, loff_t *ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +01001256
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04001257/* Access to perf_event_open(2) syscall. */
1258#define PERF_SECURITY_OPEN 0
1259
1260/* Finer grained perf_event_open(2) access control. */
1261#define PERF_SECURITY_CPU 1
1262#define PERF_SECURITY_KERNEL 2
1263#define PERF_SECURITY_TRACEPOINT 3
1264
1265static inline int perf_is_paranoid(void)
Peter Zijlstra320ebf02010-03-02 12:35:37 +01001266{
1267 return sysctl_perf_event_paranoid > -1;
1268}
1269
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04001270static inline int perf_allow_kernel(struct perf_event_attr *attr)
Peter Zijlstra320ebf02010-03-02 12:35:37 +01001271{
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04001272 if (sysctl_perf_event_paranoid > 1 && !capable(CAP_SYS_ADMIN))
1273 return -EACCES;
1274
1275 return security_perf_event_open(attr, PERF_SECURITY_KERNEL);
Peter Zijlstra320ebf02010-03-02 12:35:37 +01001276}
1277
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04001278static inline int perf_allow_cpu(struct perf_event_attr *attr)
Peter Zijlstra320ebf02010-03-02 12:35:37 +01001279{
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04001280 if (sysctl_perf_event_paranoid > 0 && !capable(CAP_SYS_ADMIN))
1281 return -EACCES;
1282
1283 return security_perf_event_open(attr, PERF_SECURITY_CPU);
1284}
1285
1286static inline int perf_allow_tracepoint(struct perf_event_attr *attr)
1287{
1288 if (sysctl_perf_event_paranoid > -1 && !capable(CAP_SYS_ADMIN))
1289 return -EPERM;
1290
1291 return security_perf_event_open(attr, PERF_SECURITY_TRACEPOINT);
Peter Zijlstra320ebf02010-03-02 12:35:37 +01001292}
1293
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001294extern void perf_event_init(void);
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001295extern void perf_tp_event(u16 event_type, u64 count, void *record,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02001296 int entry_size, struct pt_regs *regs,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04001297 struct hlist_head *head, int rctx,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02001298 struct task_struct *task);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02001299extern void perf_bp_event(struct perf_event *event, void *data);
Ingo Molnar0d905bc2009-05-04 19:13:30 +02001300
Paul Mackerras9d23a902009-05-14 21:48:08 +10001301#ifndef perf_misc_flags
Ingo Molnare7e7ee22011-05-04 08:42:29 +02001302# define perf_misc_flags(regs) \
1303 (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
1304# define perf_instruction_pointer(regs) instruction_pointer(regs)
Paul Mackerras9d23a902009-05-14 21:48:08 +10001305#endif
Hendrik Bruecknerc895f6f2017-12-04 10:56:44 +01001306#ifndef perf_arch_bpf_user_pt_regs
1307# define perf_arch_bpf_user_pt_regs(regs) regs
1308#endif
Paul Mackerras9d23a902009-05-14 21:48:08 +10001309
Stephane Eranianbce38cd2012-02-09 23:20:51 +01001310static inline bool has_branch_stack(struct perf_event *event)
1311{
1312 return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
1313}
1314
Yan, Zhenga46a2302014-11-04 21:56:06 -05001315static inline bool needs_branch_stack(struct perf_event *event)
1316{
1317 return event->attr.branch_sample_type != 0;
1318}
1319
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02001320static inline bool has_aux(struct perf_event *event)
1321{
1322 return event->pmu->setup_aux;
1323}
1324
Wang Nan9ecda412016-04-05 14:11:18 +00001325static inline bool is_write_backward(struct perf_event *event)
1326{
1327 return !!event->attr.write_backward;
1328}
1329
Alexander Shishkin375637b2016-04-27 18:44:46 +03001330static inline bool has_addr_filter(struct perf_event *event)
1331{
1332 return event->pmu->nr_addr_filters;
1333}
1334
1335/*
1336 * An inherited event uses parent's filters
1337 */
1338static inline struct perf_addr_filters_head *
1339perf_event_addr_filters(struct perf_event *event)
1340{
1341 struct perf_addr_filters_head *ifh = &event->addr_filters;
1342
1343 if (event->parent)
1344 ifh = &event->parent->addr_filters;
1345
1346 return ifh;
1347}
1348
1349extern void perf_event_addr_filters_sync(struct perf_event *event);
1350
Markus Metzger5622f292009-09-15 13:00:23 +02001351extern int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02001352 struct perf_event *event, unsigned int size);
Wang Nan9ecda412016-04-05 14:11:18 +00001353extern int perf_output_begin_forward(struct perf_output_handle *handle,
1354 struct perf_event *event,
1355 unsigned int size);
1356extern int perf_output_begin_backward(struct perf_output_handle *handle,
1357 struct perf_event *event,
1358 unsigned int size);
1359
Markus Metzger5622f292009-09-15 13:00:23 +02001360extern void perf_output_end(struct perf_output_handle *handle);
Frederic Weisbecker91d77532012-08-07 15:20:38 +02001361extern unsigned int perf_output_copy(struct perf_output_handle *handle,
Markus Metzger5622f292009-09-15 13:00:23 +02001362 const void *buf, unsigned int len);
Jiri Olsa5685e0f2012-08-07 15:20:39 +02001363extern unsigned int perf_output_skip(struct perf_output_handle *handle,
1364 unsigned int len);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001365extern int perf_swevent_get_recursion_context(void);
1366extern void perf_swevent_put_recursion_context(int rctx);
Jiri Olsaab573842013-05-01 17:25:44 +02001367extern u64 perf_swevent_set_period(struct perf_event *event);
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001368extern void perf_event_enable(struct perf_event *event);
1369extern void perf_event_disable(struct perf_event *event);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001370extern void perf_event_disable_local(struct perf_event *event);
Jiri Olsa5aab90c2016-10-26 11:48:24 +02001371extern void perf_event_disable_inatomic(struct perf_event *event);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001372extern void perf_event_task_tick(void);
Jiri Olsa475113d2016-12-28 14:31:03 +01001373extern int perf_event_account_interrupt(struct perf_event *event);
Peter Zijlstrae041e322014-05-21 17:32:19 +02001374#else /* !CONFIG_PERF_EVENTS: */
Alexander Shishkinfdc26702015-01-14 14:18:16 +02001375static inline void *
1376perf_aux_output_begin(struct perf_output_handle *handle,
1377 struct perf_event *event) { return NULL; }
1378static inline void
Will Deaconf4c0b0a2017-02-20 15:33:50 +02001379perf_aux_output_end(struct perf_output_handle *handle, unsigned long size)
1380 { }
Alexander Shishkinfdc26702015-01-14 14:18:16 +02001381static inline int
1382perf_aux_output_skip(struct perf_output_handle *handle,
1383 unsigned long size) { return -EINVAL; }
1384static inline void *
1385perf_get_aux(struct perf_output_handle *handle) { return NULL; }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001386static inline void
Peter Zijlstraff303e62015-04-17 20:05:30 +02001387perf_event_task_migrate(struct task_struct *task) { }
1388static inline void
Jiri Olsaab0cce52012-05-23 13:13:02 +02001389perf_event_task_sched_in(struct task_struct *prev,
1390 struct task_struct *task) { }
1391static inline void
1392perf_event_task_sched_out(struct task_struct *prev,
1393 struct task_struct *next) { }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001394static inline int perf_event_init_task(struct task_struct *child) { return 0; }
1395static inline void perf_event_exit_task(struct task_struct *child) { }
1396static inline void perf_event_free_task(struct task_struct *task) { }
Peter Zijlstra4e231c72010-09-09 21:01:59 +02001397static inline void perf_event_delayed_put(struct task_struct *task) { }
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -08001398static inline struct file *perf_event_get(unsigned int fd) { return ERR_PTR(-EINVAL); }
Yonghong Songf8d959a2018-05-24 11:21:08 -07001399static inline const struct perf_event *perf_get_event(struct file *file)
1400{
1401 return ERR_PTR(-EINVAL);
1402}
Kaixu Xiaffe86902015-08-06 07:02:32 +00001403static inline const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
1404{
1405 return ERR_PTR(-EINVAL);
1406}
Yonghong Song7d9285e2017-10-05 09:19:19 -07001407static inline int perf_event_read_local(struct perf_event *event, u64 *value,
1408 u64 *enabled, u64 *running)
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07001409{
1410 return -EINVAL;
1411}
Ingo Molnar57c0c152009-09-21 12:20:38 +02001412static inline void perf_event_print_debug(void) { }
Ingo Molnar57c0c152009-09-21 12:20:38 +02001413static inline int perf_event_task_disable(void) { return -EINVAL; }
1414static inline int perf_event_task_enable(void) { return -EINVAL; }
Avi Kivity26ca5c12011-06-29 18:42:37 +03001415static inline int perf_event_refresh(struct perf_event *event, int refresh)
1416{
1417 return -EINVAL;
1418}
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001419
Peter Zijlstra925d5192009-03-30 19:07:02 +02001420static inline void
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001421perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02001422static inline void
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01001423perf_sw_event_sched(u32 event_id, u64 nr, u64 addr) { }
1424static inline void
Ingo Molnar184f4122010-01-27 08:39:39 +01001425perf_bp_event(struct perf_event *event, void *data) { }
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001426
Zhang, Yanmin39447b32010-04-19 13:32:41 +08001427static inline int perf_register_guest_info_callbacks
Ingo Molnare7e7ee22011-05-04 08:42:29 +02001428(struct perf_guest_info_callbacks *callbacks) { return 0; }
Zhang, Yanmin39447b32010-04-19 13:32:41 +08001429static inline int perf_unregister_guest_info_callbacks
Ingo Molnare7e7ee22011-05-04 08:42:29 +02001430(struct perf_guest_info_callbacks *callbacks) { return 0; }
Zhang, Yanmin39447b32010-04-19 13:32:41 +08001431
Ingo Molnar57c0c152009-09-21 12:20:38 +02001432static inline void perf_event_mmap(struct vm_area_struct *vma) { }
Song Liu76193a92019-01-17 08:15:13 -08001433
1434typedef int (perf_ksymbol_get_name_f)(char *name, int name_len, void *data);
1435static inline void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
1436 bool unregister, const char *sym) { }
Song Liu6ee52e22019-01-17 08:15:15 -08001437static inline void perf_event_bpf_event(struct bpf_prog *prog,
1438 enum perf_bpf_event_type type,
1439 u16 flags) { }
Peter Zijlstrae041e322014-05-21 17:32:19 +02001440static inline void perf_event_exec(void) { }
Adrian Hunter82b89772014-05-28 11:45:04 +03001441static inline void perf_event_comm(struct task_struct *tsk, bool exec) { }
Hari Bathinie4222672017-03-08 02:11:36 +05301442static inline void perf_event_namespaces(struct task_struct *tsk) { }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001443static inline void perf_event_fork(struct task_struct *tsk) { }
1444static inline void perf_event_init(void) { }
Ingo Molnar184f4122010-01-27 08:39:39 +01001445static inline int perf_swevent_get_recursion_context(void) { return -1; }
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001446static inline void perf_swevent_put_recursion_context(int rctx) { }
Jiri Olsaab573842013-05-01 17:25:44 +02001447static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; }
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001448static inline void perf_event_enable(struct perf_event *event) { }
1449static inline void perf_event_disable(struct perf_event *event) { }
K.Prasad500ad2d2012-08-02 13:46:35 +05301450static inline int __perf_event_disable(void *info) { return -1; }
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001451static inline void perf_event_task_tick(void) { }
Kaixu Xiaffe86902015-08-06 07:02:32 +00001452static inline int perf_event_release_kernel(struct perf_event *event) { return 0; }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001453#endif
1454
David Rientjes6c4d3bc2013-03-17 15:49:10 -07001455#if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
1456extern void perf_restore_debug_store(void);
1457#else
Stephane Eranian1d9d8632013-03-15 14:26:07 +01001458static inline void perf_restore_debug_store(void) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001459#endif
1460
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02001461static __always_inline bool perf_raw_frag_last(const struct perf_raw_frag *frag)
1462{
1463 return frag->pad < sizeof(u64);
1464}
1465
Ingo Molnare7e7ee22011-05-04 08:42:29 +02001466#define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
Markus Metzger5622f292009-09-15 13:00:23 +02001467
Sukadev Bhattiprolu26639602013-01-22 22:24:23 -08001468struct perf_pmu_events_attr {
1469 struct device_attribute attr;
1470 u64 id;
Stephane Eranian3a54aaa2013-01-24 16:10:26 +01001471 const char *event_str;
Sukadev Bhattiprolu26639602013-01-22 22:24:23 -08001472};
1473
Andi Kleenfc07e9f2016-05-19 17:09:56 -07001474struct perf_pmu_events_ht_attr {
1475 struct device_attribute attr;
1476 u64 id;
1477 const char *event_str_ht;
1478 const char *event_str_noht;
1479};
1480
Cody P Schaferfd979c02015-01-30 13:45:57 -08001481ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
1482 char *page);
1483
Sukadev Bhattiprolu26639602013-01-22 22:24:23 -08001484#define PMU_EVENT_ATTR(_name, _var, _id, _show) \
1485static struct perf_pmu_events_attr _var = { \
1486 .attr = __ATTR(_name, 0444, _show, NULL), \
1487 .id = _id, \
1488};
1489
Cody P Schaferf0405b82015-01-30 13:45:58 -08001490#define PMU_EVENT_ATTR_STRING(_name, _var, _str) \
1491static struct perf_pmu_events_attr _var = { \
1492 .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
1493 .id = 0, \
1494 .event_str = _str, \
1495};
1496
Jiri Olsa641cc932012-03-15 20:09:14 +01001497#define PMU_FORMAT_ATTR(_name, _format) \
1498static ssize_t \
1499_name##_show(struct device *dev, \
1500 struct device_attribute *attr, \
1501 char *page) \
1502{ \
1503 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
1504 return sprintf(page, _format "\n"); \
1505} \
1506 \
1507static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
1508
Thomas Gleixner00e16c32016-07-13 17:16:09 +00001509/* Performance counter hotplug functions */
1510#ifdef CONFIG_PERF_EVENTS
1511int perf_event_init_cpu(unsigned int cpu);
1512int perf_event_exit_cpu(unsigned int cpu);
1513#else
1514#define perf_event_init_cpu NULL
1515#define perf_event_exit_cpu NULL
1516#endif
1517
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001518#endif /* _LINUX_PERF_EVENT_H */