perf_event.h 46 KB

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