perf_event.h 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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. /*
  18. * Kernel-internal data types and definitions:
  19. */
  20. #ifdef CONFIG_PERF_EVENTS
  21. # include <asm/perf_event.h>
  22. # include <asm/local64.h>
  23. #endif
  24. struct perf_guest_info_callbacks {
  25. int (*is_in_guest)(void);
  26. int (*is_user_mode)(void);
  27. unsigned long (*get_guest_ip)(void);
  28. };
  29. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  30. #include <asm/hw_breakpoint.h>
  31. #endif
  32. #include <linux/list.h>
  33. #include <linux/mutex.h>
  34. #include <linux/rculist.h>
  35. #include <linux/rcupdate.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/hrtimer.h>
  38. #include <linux/fs.h>
  39. #include <linux/pid_namespace.h>
  40. #include <linux/workqueue.h>
  41. #include <linux/ftrace.h>
  42. #include <linux/cpu.h>
  43. #include <linux/irq_work.h>
  44. #include <linux/static_key.h>
  45. #include <linux/jump_label_ratelimit.h>
  46. #include <linux/atomic.h>
  47. #include <linux/sysfs.h>
  48. #include <linux/perf_regs.h>
  49. #include <linux/workqueue.h>
  50. #include <linux/cgroup.h>
  51. #include <asm/local.h>
  52. struct perf_callchain_entry {
  53. __u64 nr;
  54. __u64 ip[PERF_MAX_STACK_DEPTH];
  55. };
  56. struct perf_raw_record {
  57. u32 size;
  58. void *data;
  59. };
  60. /*
  61. * branch stack layout:
  62. * nr: number of taken branches stored in entries[]
  63. *
  64. * Note that nr can vary from sample to sample
  65. * branches (to, from) are stored from most recent
  66. * to least recent, i.e., entries[0] contains the most
  67. * recent branch.
  68. */
  69. struct perf_branch_stack {
  70. __u64 nr;
  71. struct perf_branch_entry entries[0];
  72. };
  73. struct task_struct;
  74. /*
  75. * extra PMU register associated with an event
  76. */
  77. struct hw_perf_event_extra {
  78. u64 config; /* register value */
  79. unsigned int reg; /* register address or index */
  80. int alloc; /* extra register already allocated */
  81. int idx; /* index in shared_regs->regs[] */
  82. };
  83. /**
  84. * struct hw_perf_event - performance event hardware details:
  85. */
  86. struct hw_perf_event {
  87. #ifdef CONFIG_PERF_EVENTS
  88. union {
  89. struct { /* hardware */
  90. u64 config;
  91. u64 last_tag;
  92. unsigned long config_base;
  93. unsigned long event_base;
  94. int event_base_rdpmc;
  95. int idx;
  96. int last_cpu;
  97. int flags;
  98. struct hw_perf_event_extra extra_reg;
  99. struct hw_perf_event_extra branch_reg;
  100. };
  101. struct { /* software */
  102. struct hrtimer hrtimer;
  103. };
  104. struct { /* tracepoint */
  105. /* for tp_event->class */
  106. struct list_head tp_list;
  107. };
  108. struct { /* intel_cqm */
  109. int cqm_state;
  110. u32 cqm_rmid;
  111. int is_group_event;
  112. struct list_head cqm_events_entry;
  113. struct list_head cqm_groups_entry;
  114. struct list_head cqm_group_entry;
  115. };
  116. struct { /* itrace */
  117. int itrace_started;
  118. };
  119. struct { /* amd_power */
  120. u64 pwr_acc;
  121. u64 ptsc;
  122. };
  123. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  124. struct { /* breakpoint */
  125. /*
  126. * Crufty hack to avoid the chicken and egg
  127. * problem hw_breakpoint has with context
  128. * creation and event initalization.
  129. */
  130. struct arch_hw_breakpoint info;
  131. struct list_head bp_list;
  132. };
  133. #endif
  134. };
  135. /*
  136. * If the event is a per task event, this will point to the task in
  137. * question. See the comment in perf_event_alloc().
  138. */
  139. struct task_struct *target;
  140. /*
  141. * hw_perf_event::state flags; used to track the PERF_EF_* state.
  142. */
  143. #define PERF_HES_STOPPED 0x01 /* the counter is stopped */
  144. #define PERF_HES_UPTODATE 0x02 /* event->count up-to-date */
  145. #define PERF_HES_ARCH 0x04
  146. int state;
  147. /*
  148. * The last observed hardware counter value, updated with a
  149. * local64_cmpxchg() such that pmu::read() can be called nested.
  150. */
  151. local64_t prev_count;
  152. /*
  153. * The period to start the next sample with.
  154. */
  155. u64 sample_period;
  156. /*
  157. * The period we started this sample with.
  158. */
  159. u64 last_period;
  160. /*
  161. * However much is left of the current period; note that this is
  162. * a full 64bit value and allows for generation of periods longer
  163. * than hardware might allow.
  164. */
  165. local64_t period_left;
  166. /*
  167. * State for throttling the event, see __perf_event_overflow() and
  168. * perf_adjust_freq_unthr_context().
  169. */
  170. u64 interrupts_seq;
  171. u64 interrupts;
  172. /*
  173. * State for freq target events, see __perf_event_overflow() and
  174. * perf_adjust_freq_unthr_context().
  175. */
  176. u64 freq_time_stamp;
  177. u64 freq_count_stamp;
  178. #endif
  179. };
  180. struct perf_event;
  181. /*
  182. * Common implementation detail of pmu::{start,commit,cancel}_txn
  183. */
  184. #define PERF_PMU_TXN_ADD 0x1 /* txn to add/schedule event on PMU */
  185. #define PERF_PMU_TXN_READ 0x2 /* txn to read event group from PMU */
  186. /**
  187. * pmu::capabilities flags
  188. */
  189. #define PERF_PMU_CAP_NO_INTERRUPT 0x01
  190. #define PERF_PMU_CAP_NO_NMI 0x02
  191. #define PERF_PMU_CAP_AUX_NO_SG 0x04
  192. #define PERF_PMU_CAP_AUX_SW_DOUBLEBUF 0x08
  193. #define PERF_PMU_CAP_EXCLUSIVE 0x10
  194. #define PERF_PMU_CAP_ITRACE 0x20
  195. /**
  196. * struct pmu - generic performance monitoring unit
  197. */
  198. struct pmu {
  199. struct list_head entry;
  200. struct module *module;
  201. struct device *dev;
  202. const struct attribute_group **attr_groups;
  203. const char *name;
  204. int type;
  205. /*
  206. * various common per-pmu feature flags
  207. */
  208. int capabilities;
  209. int * __percpu pmu_disable_count;
  210. struct perf_cpu_context * __percpu pmu_cpu_context;
  211. atomic_t exclusive_cnt; /* < 0: cpu; > 0: tsk */
  212. int task_ctx_nr;
  213. int hrtimer_interval_ms;
  214. /*
  215. * Fully disable/enable this PMU, can be used to protect from the PMI
  216. * as well as for lazy/batch writing of the MSRs.
  217. */
  218. void (*pmu_enable) (struct pmu *pmu); /* optional */
  219. void (*pmu_disable) (struct pmu *pmu); /* optional */
  220. /*
  221. * Try and initialize the event for this PMU.
  222. *
  223. * Returns:
  224. * -ENOENT -- @event is not for this PMU
  225. *
  226. * -ENODEV -- @event is for this PMU but PMU not present
  227. * -EBUSY -- @event is for this PMU but PMU temporarily unavailable
  228. * -EINVAL -- @event is for this PMU but @event is not valid
  229. * -EOPNOTSUPP -- @event is for this PMU, @event is valid, but not supported
  230. * -EACCESS -- @event is for this PMU, @event is valid, but no privilidges
  231. *
  232. * 0 -- @event is for this PMU and valid
  233. *
  234. * Other error return values are allowed.
  235. */
  236. int (*event_init) (struct perf_event *event);
  237. /*
  238. * Notification that the event was mapped or unmapped. Called
  239. * in the context of the mapping task.
  240. */
  241. void (*event_mapped) (struct perf_event *event); /*optional*/
  242. void (*event_unmapped) (struct perf_event *event); /*optional*/
  243. /*
  244. * Flags for ->add()/->del()/ ->start()/->stop(). There are
  245. * matching hw_perf_event::state flags.
  246. */
  247. #define PERF_EF_START 0x01 /* start the counter when adding */
  248. #define PERF_EF_RELOAD 0x02 /* reload the counter when starting */
  249. #define PERF_EF_UPDATE 0x04 /* update the counter when stopping */
  250. /*
  251. * Adds/Removes a counter to/from the PMU, can be done inside a
  252. * transaction, see the ->*_txn() methods.
  253. *
  254. * The add/del callbacks will reserve all hardware resources required
  255. * to service the event, this includes any counter constraint
  256. * scheduling etc.
  257. *
  258. * Called with IRQs disabled and the PMU disabled on the CPU the event
  259. * is on.
  260. *
  261. * ->add() called without PERF_EF_START should result in the same state
  262. * as ->add() followed by ->stop().
  263. *
  264. * ->del() must always PERF_EF_UPDATE stop an event. If it calls
  265. * ->stop() that must deal with already being stopped without
  266. * PERF_EF_UPDATE.
  267. */
  268. int (*add) (struct perf_event *event, int flags);
  269. void (*del) (struct perf_event *event, int flags);
  270. /*
  271. * Starts/Stops a counter present on the PMU.
  272. *
  273. * The PMI handler should stop the counter when perf_event_overflow()
  274. * returns !0. ->start() will be used to continue.
  275. *
  276. * Also used to change the sample period.
  277. *
  278. * Called with IRQs disabled and the PMU disabled on the CPU the event
  279. * is on -- will be called from NMI context with the PMU generates
  280. * NMIs.
  281. *
  282. * ->stop() with PERF_EF_UPDATE will read the counter and update
  283. * period/count values like ->read() would.
  284. *
  285. * ->start() with PERF_EF_RELOAD will reprogram the the counter
  286. * value, must be preceded by a ->stop() with PERF_EF_UPDATE.
  287. */
  288. void (*start) (struct perf_event *event, int flags);
  289. void (*stop) (struct perf_event *event, int flags);
  290. /*
  291. * Updates the counter value of the event.
  292. *
  293. * For sampling capable PMUs this will also update the software period
  294. * hw_perf_event::period_left field.
  295. */
  296. void (*read) (struct perf_event *event);
  297. /*
  298. * Group events scheduling is treated as a transaction, add
  299. * group events as a whole and perform one schedulability test.
  300. * If the test fails, roll back the whole group
  301. *
  302. * Start the transaction, after this ->add() doesn't need to
  303. * do schedulability tests.
  304. *
  305. * Optional.
  306. */
  307. void (*start_txn) (struct pmu *pmu, unsigned int txn_flags);
  308. /*
  309. * If ->start_txn() disabled the ->add() schedulability test
  310. * then ->commit_txn() is required to perform one. On success
  311. * the transaction is closed. On error the transaction is kept
  312. * open until ->cancel_txn() is called.
  313. *
  314. * Optional.
  315. */
  316. int (*commit_txn) (struct pmu *pmu);
  317. /*
  318. * Will cancel the transaction, assumes ->del() is called
  319. * for each successful ->add() during the transaction.
  320. *
  321. * Optional.
  322. */
  323. void (*cancel_txn) (struct pmu *pmu);
  324. /*
  325. * Will return the value for perf_event_mmap_page::index for this event,
  326. * if no implementation is provided it will default to: event->hw.idx + 1.
  327. */
  328. int (*event_idx) (struct perf_event *event); /*optional */
  329. /*
  330. * context-switches callback
  331. */
  332. void (*sched_task) (struct perf_event_context *ctx,
  333. bool sched_in);
  334. /*
  335. * PMU specific data size
  336. */
  337. size_t task_ctx_size;
  338. /*
  339. * Return the count value for a counter.
  340. */
  341. u64 (*count) (struct perf_event *event); /*optional*/
  342. /*
  343. * Set up pmu-private data structures for an AUX area
  344. */
  345. void *(*setup_aux) (int cpu, void **pages,
  346. int nr_pages, bool overwrite);
  347. /* optional */
  348. /*
  349. * Free pmu-private AUX data structures
  350. */
  351. void (*free_aux) (void *aux); /* optional */
  352. /*
  353. * Filter events for PMU-specific reasons.
  354. */
  355. int (*filter_match) (struct perf_event *event); /* optional */
  356. };
  357. /**
  358. * enum perf_event_active_state - the states of a event
  359. */
  360. enum perf_event_active_state {
  361. PERF_EVENT_STATE_DEAD = -4,
  362. PERF_EVENT_STATE_EXIT = -3,
  363. PERF_EVENT_STATE_ERROR = -2,
  364. PERF_EVENT_STATE_OFF = -1,
  365. PERF_EVENT_STATE_INACTIVE = 0,
  366. PERF_EVENT_STATE_ACTIVE = 1,
  367. };
  368. struct file;
  369. struct perf_sample_data;
  370. typedef void (*perf_overflow_handler_t)(struct perf_event *,
  371. struct perf_sample_data *,
  372. struct pt_regs *regs);
  373. enum perf_group_flag {
  374. PERF_GROUP_SOFTWARE = 0x1,
  375. };
  376. #define SWEVENT_HLIST_BITS 8
  377. #define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
  378. struct swevent_hlist {
  379. struct hlist_head heads[SWEVENT_HLIST_SIZE];
  380. struct rcu_head rcu_head;
  381. };
  382. #define PERF_ATTACH_CONTEXT 0x01
  383. #define PERF_ATTACH_GROUP 0x02
  384. #define PERF_ATTACH_TASK 0x04
  385. #define PERF_ATTACH_TASK_DATA 0x08
  386. struct perf_cgroup;
  387. struct ring_buffer;
  388. /**
  389. * struct perf_event - performance event kernel representation:
  390. */
  391. struct perf_event {
  392. #ifdef CONFIG_PERF_EVENTS
  393. /*
  394. * entry onto perf_event_context::event_list;
  395. * modifications require ctx->lock
  396. * RCU safe iterations.
  397. */
  398. struct list_head event_entry;
  399. /*
  400. * XXX: group_entry and sibling_list should be mutually exclusive;
  401. * either you're a sibling on a group, or you're the group leader.
  402. * Rework the code to always use the same list element.
  403. *
  404. * Locked for modification by both ctx->mutex and ctx->lock; holding
  405. * either sufficies for read.
  406. */
  407. struct list_head group_entry;
  408. struct list_head sibling_list;
  409. /*
  410. * We need storage to track the entries in perf_pmu_migrate_context; we
  411. * cannot use the event_entry because of RCU and we want to keep the
  412. * group in tact which avoids us using the other two entries.
  413. */
  414. struct list_head migrate_entry;
  415. struct hlist_node hlist_entry;
  416. struct list_head active_entry;
  417. int nr_siblings;
  418. int group_flags;
  419. struct perf_event *group_leader;
  420. struct pmu *pmu;
  421. void *pmu_private;
  422. enum perf_event_active_state state;
  423. unsigned int attach_state;
  424. local64_t count;
  425. atomic64_t child_count;
  426. /*
  427. * These are the total time in nanoseconds that the event
  428. * has been enabled (i.e. eligible to run, and the task has
  429. * been scheduled in, if this is a per-task event)
  430. * and running (scheduled onto the CPU), respectively.
  431. *
  432. * They are computed from tstamp_enabled, tstamp_running and
  433. * tstamp_stopped when the event is in INACTIVE or ACTIVE state.
  434. */
  435. u64 total_time_enabled;
  436. u64 total_time_running;
  437. /*
  438. * These are timestamps used for computing total_time_enabled
  439. * and total_time_running when the event is in INACTIVE or
  440. * ACTIVE state, measured in nanoseconds from an arbitrary point
  441. * in time.
  442. * tstamp_enabled: the notional time when the event was enabled
  443. * tstamp_running: the notional time when the event was scheduled on
  444. * tstamp_stopped: in INACTIVE state, the notional time when the
  445. * event was scheduled off.
  446. */
  447. u64 tstamp_enabled;
  448. u64 tstamp_running;
  449. u64 tstamp_stopped;
  450. /*
  451. * timestamp shadows the actual context timing but it can
  452. * be safely used in NMI interrupt context. It reflects the
  453. * context time as it was when the event was last scheduled in.
  454. *
  455. * ctx_time already accounts for ctx->timestamp. Therefore to
  456. * compute ctx_time for a sample, simply add perf_clock().
  457. */
  458. u64 shadow_ctx_time;
  459. struct perf_event_attr attr;
  460. u16 header_size;
  461. u16 id_header_size;
  462. u16 read_size;
  463. struct hw_perf_event hw;
  464. struct perf_event_context *ctx;
  465. atomic_long_t refcount;
  466. /*
  467. * These accumulate total time (in nanoseconds) that children
  468. * events have been enabled and running, respectively.
  469. */
  470. atomic64_t child_total_time_enabled;
  471. atomic64_t child_total_time_running;
  472. /*
  473. * Protect attach/detach and child_list:
  474. */
  475. struct mutex child_mutex;
  476. struct list_head child_list;
  477. struct perf_event *parent;
  478. int oncpu;
  479. int cpu;
  480. struct list_head owner_entry;
  481. struct task_struct *owner;
  482. /* mmap bits */
  483. struct mutex mmap_mutex;
  484. atomic_t mmap_count;
  485. struct ring_buffer *rb;
  486. struct list_head rb_entry;
  487. unsigned long rcu_batches;
  488. int rcu_pending;
  489. /* poll related */
  490. wait_queue_head_t waitq;
  491. struct fasync_struct *fasync;
  492. /* delayed work for NMIs and such */
  493. int pending_wakeup;
  494. int pending_kill;
  495. int pending_disable;
  496. struct irq_work pending;
  497. atomic_t event_limit;
  498. void (*destroy)(struct perf_event *);
  499. struct rcu_head rcu_head;
  500. struct pid_namespace *ns;
  501. u64 id;
  502. u64 (*clock)(void);
  503. perf_overflow_handler_t overflow_handler;
  504. void *overflow_handler_context;
  505. #ifdef CONFIG_EVENT_TRACING
  506. struct trace_event_call *tp_event;
  507. struct event_filter *filter;
  508. #ifdef CONFIG_FUNCTION_TRACER
  509. struct ftrace_ops ftrace_ops;
  510. #endif
  511. #endif
  512. #ifdef CONFIG_CGROUP_PERF
  513. struct perf_cgroup *cgrp; /* cgroup event is attach to */
  514. int cgrp_defer_enabled;
  515. #endif
  516. #endif /* CONFIG_PERF_EVENTS */
  517. };
  518. /**
  519. * struct perf_event_context - event context structure
  520. *
  521. * Used as a container for task events and CPU events as well:
  522. */
  523. struct perf_event_context {
  524. struct pmu *pmu;
  525. /*
  526. * Protect the states of the events in the list,
  527. * nr_active, and the list:
  528. */
  529. raw_spinlock_t lock;
  530. /*
  531. * Protect the list of events. Locking either mutex or lock
  532. * is sufficient to ensure the list doesn't change; to change
  533. * the list you need to lock both the mutex and the spinlock.
  534. */
  535. struct mutex mutex;
  536. struct list_head active_ctx_list;
  537. struct list_head pinned_groups;
  538. struct list_head flexible_groups;
  539. struct list_head event_list;
  540. int nr_events;
  541. int nr_active;
  542. int is_active;
  543. int nr_stat;
  544. int nr_freq;
  545. int rotate_disable;
  546. atomic_t refcount;
  547. struct task_struct *task;
  548. /*
  549. * Context clock, runs when context enabled.
  550. */
  551. u64 time;
  552. u64 timestamp;
  553. /*
  554. * These fields let us detect when two contexts have both
  555. * been cloned (inherited) from a common ancestor.
  556. */
  557. struct perf_event_context *parent_ctx;
  558. u64 parent_gen;
  559. u64 generation;
  560. int pin_count;
  561. int nr_cgroups; /* cgroup evts */
  562. void *task_ctx_data; /* pmu specific data */
  563. struct rcu_head rcu_head;
  564. };
  565. /*
  566. * Number of contexts where an event can trigger:
  567. * task, softirq, hardirq, nmi.
  568. */
  569. #define PERF_NR_CONTEXTS 4
  570. /**
  571. * struct perf_event_cpu_context - per cpu event context structure
  572. */
  573. struct perf_cpu_context {
  574. struct perf_event_context ctx;
  575. struct perf_event_context *task_ctx;
  576. int active_oncpu;
  577. int exclusive;
  578. raw_spinlock_t hrtimer_lock;
  579. struct hrtimer hrtimer;
  580. ktime_t hrtimer_interval;
  581. unsigned int hrtimer_active;
  582. struct pmu *unique_pmu;
  583. struct perf_cgroup *cgrp;
  584. };
  585. struct perf_output_handle {
  586. struct perf_event *event;
  587. struct ring_buffer *rb;
  588. unsigned long wakeup;
  589. unsigned long size;
  590. union {
  591. void *addr;
  592. unsigned long head;
  593. };
  594. int page;
  595. };
  596. #ifdef CONFIG_CGROUP_PERF
  597. /*
  598. * perf_cgroup_info keeps track of time_enabled for a cgroup.
  599. * This is a per-cpu dynamically allocated data structure.
  600. */
  601. struct perf_cgroup_info {
  602. u64 time;
  603. u64 timestamp;
  604. };
  605. struct perf_cgroup {
  606. struct cgroup_subsys_state css;
  607. struct perf_cgroup_info __percpu *info;
  608. };
  609. /*
  610. * Must ensure cgroup is pinned (css_get) before calling
  611. * this function. In other words, we cannot call this function
  612. * if there is no cgroup event for the current CPU context.
  613. */
  614. static inline struct perf_cgroup *
  615. perf_cgroup_from_task(struct task_struct *task, struct perf_event_context *ctx)
  616. {
  617. return container_of(task_css_check(task, perf_event_cgrp_id,
  618. ctx ? lockdep_is_held(&ctx->lock)
  619. : true),
  620. struct perf_cgroup, css);
  621. }
  622. #endif /* CONFIG_CGROUP_PERF */
  623. #ifdef CONFIG_PERF_EVENTS
  624. extern void *perf_aux_output_begin(struct perf_output_handle *handle,
  625. struct perf_event *event);
  626. extern void perf_aux_output_end(struct perf_output_handle *handle,
  627. unsigned long size, bool truncated);
  628. extern int perf_aux_output_skip(struct perf_output_handle *handle,
  629. unsigned long size);
  630. extern void *perf_get_aux(struct perf_output_handle *handle);
  631. extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
  632. extern void perf_pmu_unregister(struct pmu *pmu);
  633. extern int perf_num_counters(void);
  634. extern const char *perf_pmu_name(void);
  635. extern void __perf_event_task_sched_in(struct task_struct *prev,
  636. struct task_struct *task);
  637. extern void __perf_event_task_sched_out(struct task_struct *prev,
  638. struct task_struct *next);
  639. extern int perf_event_init_task(struct task_struct *child);
  640. extern void perf_event_exit_task(struct task_struct *child);
  641. extern void perf_event_free_task(struct task_struct *task);
  642. extern void perf_event_delayed_put(struct task_struct *task);
  643. extern struct file *perf_event_get(unsigned int fd);
  644. extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event);
  645. extern void perf_event_print_debug(void);
  646. extern void perf_pmu_disable(struct pmu *pmu);
  647. extern void perf_pmu_enable(struct pmu *pmu);
  648. extern void perf_sched_cb_dec(struct pmu *pmu);
  649. extern void perf_sched_cb_inc(struct pmu *pmu);
  650. extern int perf_event_task_disable(void);
  651. extern int perf_event_task_enable(void);
  652. extern int perf_event_refresh(struct perf_event *event, int refresh);
  653. extern void perf_event_update_userpage(struct perf_event *event);
  654. extern int perf_event_release_kernel(struct perf_event *event);
  655. extern struct perf_event *
  656. perf_event_create_kernel_counter(struct perf_event_attr *attr,
  657. int cpu,
  658. struct task_struct *task,
  659. perf_overflow_handler_t callback,
  660. void *context);
  661. extern void perf_pmu_migrate_context(struct pmu *pmu,
  662. int src_cpu, int dst_cpu);
  663. extern u64 perf_event_read_local(struct perf_event *event);
  664. extern u64 perf_event_read_value(struct perf_event *event,
  665. u64 *enabled, u64 *running);
  666. struct perf_sample_data {
  667. /*
  668. * Fields set by perf_sample_data_init(), group so as to
  669. * minimize the cachelines touched.
  670. */
  671. u64 addr;
  672. struct perf_raw_record *raw;
  673. struct perf_branch_stack *br_stack;
  674. u64 period;
  675. u64 weight;
  676. u64 txn;
  677. union perf_mem_data_src data_src;
  678. /*
  679. * The other fields, optionally {set,used} by
  680. * perf_{prepare,output}_sample().
  681. */
  682. u64 type;
  683. u64 ip;
  684. struct {
  685. u32 pid;
  686. u32 tid;
  687. } tid_entry;
  688. u64 time;
  689. u64 id;
  690. u64 stream_id;
  691. struct {
  692. u32 cpu;
  693. u32 reserved;
  694. } cpu_entry;
  695. struct perf_callchain_entry *callchain;
  696. /*
  697. * regs_user may point to task_pt_regs or to regs_user_copy, depending
  698. * on arch details.
  699. */
  700. struct perf_regs regs_user;
  701. struct pt_regs regs_user_copy;
  702. struct perf_regs regs_intr;
  703. u64 stack_user_size;
  704. } ____cacheline_aligned;
  705. /* default value for data source */
  706. #define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
  707. PERF_MEM_S(LVL, NA) |\
  708. PERF_MEM_S(SNOOP, NA) |\
  709. PERF_MEM_S(LOCK, NA) |\
  710. PERF_MEM_S(TLB, NA))
  711. static inline void perf_sample_data_init(struct perf_sample_data *data,
  712. u64 addr, u64 period)
  713. {
  714. /* remaining struct members initialized in perf_prepare_sample() */
  715. data->addr = addr;
  716. data->raw = NULL;
  717. data->br_stack = NULL;
  718. data->period = period;
  719. data->weight = 0;
  720. data->data_src.val = PERF_MEM_NA;
  721. data->txn = 0;
  722. }
  723. extern void perf_output_sample(struct perf_output_handle *handle,
  724. struct perf_event_header *header,
  725. struct perf_sample_data *data,
  726. struct perf_event *event);
  727. extern void perf_prepare_sample(struct perf_event_header *header,
  728. struct perf_sample_data *data,
  729. struct perf_event *event,
  730. struct pt_regs *regs);
  731. extern int perf_event_overflow(struct perf_event *event,
  732. struct perf_sample_data *data,
  733. struct pt_regs *regs);
  734. extern void perf_event_output(struct perf_event *event,
  735. struct perf_sample_data *data,
  736. struct pt_regs *regs);
  737. extern void
  738. perf_event_header__init_id(struct perf_event_header *header,
  739. struct perf_sample_data *data,
  740. struct perf_event *event);
  741. extern void
  742. perf_event__output_id_sample(struct perf_event *event,
  743. struct perf_output_handle *handle,
  744. struct perf_sample_data *sample);
  745. extern void
  746. perf_log_lost_samples(struct perf_event *event, u64 lost);
  747. static inline bool is_sampling_event(struct perf_event *event)
  748. {
  749. return event->attr.sample_period != 0;
  750. }
  751. /*
  752. * Return 1 for a software event, 0 for a hardware event
  753. */
  754. static inline int is_software_event(struct perf_event *event)
  755. {
  756. return event->pmu->task_ctx_nr == perf_sw_context;
  757. }
  758. extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
  759. extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
  760. extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
  761. #ifndef perf_arch_fetch_caller_regs
  762. static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
  763. #endif
  764. /*
  765. * Take a snapshot of the regs. Skip ip and frame pointer to
  766. * the nth caller. We only need a few of the regs:
  767. * - ip for PERF_SAMPLE_IP
  768. * - cs for user_mode() tests
  769. * - bp for callchains
  770. * - eflags, for future purposes, just in case
  771. */
  772. static inline void perf_fetch_caller_regs(struct pt_regs *regs)
  773. {
  774. memset(regs, 0, sizeof(*regs));
  775. perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
  776. }
  777. static __always_inline void
  778. perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  779. {
  780. if (static_key_false(&perf_swevent_enabled[event_id]))
  781. __perf_sw_event(event_id, nr, regs, addr);
  782. }
  783. DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
  784. /*
  785. * 'Special' version for the scheduler, it hard assumes no recursion,
  786. * which is guaranteed by us not actually scheduling inside other swevents
  787. * because those disable preemption.
  788. */
  789. static __always_inline void
  790. perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
  791. {
  792. if (static_key_false(&perf_swevent_enabled[event_id])) {
  793. struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
  794. perf_fetch_caller_regs(regs);
  795. ___perf_sw_event(event_id, nr, regs, addr);
  796. }
  797. }
  798. extern struct static_key_false perf_sched_events;
  799. static __always_inline bool
  800. perf_sw_migrate_enabled(void)
  801. {
  802. if (static_key_false(&perf_swevent_enabled[PERF_COUNT_SW_CPU_MIGRATIONS]))
  803. return true;
  804. return false;
  805. }
  806. static inline void perf_event_task_migrate(struct task_struct *task)
  807. {
  808. if (perf_sw_migrate_enabled())
  809. task->sched_migrated = 1;
  810. }
  811. static inline void perf_event_task_sched_in(struct task_struct *prev,
  812. struct task_struct *task)
  813. {
  814. if (static_branch_unlikely(&perf_sched_events))
  815. __perf_event_task_sched_in(prev, task);
  816. if (perf_sw_migrate_enabled() && task->sched_migrated) {
  817. struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
  818. perf_fetch_caller_regs(regs);
  819. ___perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, regs, 0);
  820. task->sched_migrated = 0;
  821. }
  822. }
  823. static inline void perf_event_task_sched_out(struct task_struct *prev,
  824. struct task_struct *next)
  825. {
  826. perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
  827. if (static_branch_unlikely(&perf_sched_events))
  828. __perf_event_task_sched_out(prev, next);
  829. }
  830. static inline u64 __perf_event_count(struct perf_event *event)
  831. {
  832. return local64_read(&event->count) + atomic64_read(&event->child_count);
  833. }
  834. extern void perf_event_mmap(struct vm_area_struct *vma);
  835. extern struct perf_guest_info_callbacks *perf_guest_cbs;
  836. extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  837. extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  838. extern void perf_event_exec(void);
  839. extern void perf_event_comm(struct task_struct *tsk, bool exec);
  840. extern void perf_event_fork(struct task_struct *tsk);
  841. /* Callchains */
  842. DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
  843. extern void perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs);
  844. extern void perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs);
  845. extern struct perf_callchain_entry *
  846. get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
  847. bool crosstask, bool add_mark);
  848. extern int get_callchain_buffers(void);
  849. extern void put_callchain_buffers(void);
  850. static inline int perf_callchain_store(struct perf_callchain_entry *entry, u64 ip)
  851. {
  852. if (entry->nr < PERF_MAX_STACK_DEPTH) {
  853. entry->ip[entry->nr++] = ip;
  854. return 0;
  855. } else {
  856. return -1; /* no more room, stop walking the stack */
  857. }
  858. }
  859. extern int sysctl_perf_event_paranoid;
  860. extern int sysctl_perf_event_mlock;
  861. extern int sysctl_perf_event_sample_rate;
  862. extern int sysctl_perf_cpu_time_max_percent;
  863. extern void perf_sample_event_took(u64 sample_len_ns);
  864. extern int perf_proc_update_handler(struct ctl_table *table, int write,
  865. void __user *buffer, size_t *lenp,
  866. loff_t *ppos);
  867. extern int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
  868. void __user *buffer, size_t *lenp,
  869. loff_t *ppos);
  870. static inline bool perf_paranoid_tracepoint_raw(void)
  871. {
  872. return sysctl_perf_event_paranoid > -1;
  873. }
  874. static inline bool perf_paranoid_cpu(void)
  875. {
  876. return sysctl_perf_event_paranoid > 0;
  877. }
  878. static inline bool perf_paranoid_kernel(void)
  879. {
  880. return sysctl_perf_event_paranoid > 1;
  881. }
  882. extern void perf_event_init(void);
  883. extern void perf_tp_event(u64 addr, u64 count, void *record,
  884. int entry_size, struct pt_regs *regs,
  885. struct hlist_head *head, int rctx,
  886. struct task_struct *task);
  887. extern void perf_bp_event(struct perf_event *event, void *data);
  888. #ifndef perf_misc_flags
  889. # define perf_misc_flags(regs) \
  890. (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
  891. # define perf_instruction_pointer(regs) instruction_pointer(regs)
  892. #endif
  893. static inline bool has_branch_stack(struct perf_event *event)
  894. {
  895. return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
  896. }
  897. static inline bool needs_branch_stack(struct perf_event *event)
  898. {
  899. return event->attr.branch_sample_type != 0;
  900. }
  901. static inline bool has_aux(struct perf_event *event)
  902. {
  903. return event->pmu->setup_aux;
  904. }
  905. extern int perf_output_begin(struct perf_output_handle *handle,
  906. struct perf_event *event, unsigned int size);
  907. extern void perf_output_end(struct perf_output_handle *handle);
  908. extern unsigned int perf_output_copy(struct perf_output_handle *handle,
  909. const void *buf, unsigned int len);
  910. extern unsigned int perf_output_skip(struct perf_output_handle *handle,
  911. unsigned int len);
  912. extern int perf_swevent_get_recursion_context(void);
  913. extern void perf_swevent_put_recursion_context(int rctx);
  914. extern u64 perf_swevent_set_period(struct perf_event *event);
  915. extern void perf_event_enable(struct perf_event *event);
  916. extern void perf_event_disable(struct perf_event *event);
  917. extern void perf_event_disable_local(struct perf_event *event);
  918. extern void perf_event_task_tick(void);
  919. #else /* !CONFIG_PERF_EVENTS: */
  920. static inline void *
  921. perf_aux_output_begin(struct perf_output_handle *handle,
  922. struct perf_event *event) { return NULL; }
  923. static inline void
  924. perf_aux_output_end(struct perf_output_handle *handle, unsigned long size,
  925. bool truncated) { }
  926. static inline int
  927. perf_aux_output_skip(struct perf_output_handle *handle,
  928. unsigned long size) { return -EINVAL; }
  929. static inline void *
  930. perf_get_aux(struct perf_output_handle *handle) { return NULL; }
  931. static inline void
  932. perf_event_task_migrate(struct task_struct *task) { }
  933. static inline void
  934. perf_event_task_sched_in(struct task_struct *prev,
  935. struct task_struct *task) { }
  936. static inline void
  937. perf_event_task_sched_out(struct task_struct *prev,
  938. struct task_struct *next) { }
  939. static inline int perf_event_init_task(struct task_struct *child) { return 0; }
  940. static inline void perf_event_exit_task(struct task_struct *child) { }
  941. static inline void perf_event_free_task(struct task_struct *task) { }
  942. static inline void perf_event_delayed_put(struct task_struct *task) { }
  943. static inline struct file *perf_event_get(unsigned int fd) { return ERR_PTR(-EINVAL); }
  944. static inline const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
  945. {
  946. return ERR_PTR(-EINVAL);
  947. }
  948. static inline u64 perf_event_read_local(struct perf_event *event) { return -EINVAL; }
  949. static inline void perf_event_print_debug(void) { }
  950. static inline int perf_event_task_disable(void) { return -EINVAL; }
  951. static inline int perf_event_task_enable(void) { return -EINVAL; }
  952. static inline int perf_event_refresh(struct perf_event *event, int refresh)
  953. {
  954. return -EINVAL;
  955. }
  956. static inline void
  957. perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
  958. static inline void
  959. perf_sw_event_sched(u32 event_id, u64 nr, u64 addr) { }
  960. static inline void
  961. perf_bp_event(struct perf_event *event, void *data) { }
  962. static inline int perf_register_guest_info_callbacks
  963. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  964. static inline int perf_unregister_guest_info_callbacks
  965. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  966. static inline void perf_event_mmap(struct vm_area_struct *vma) { }
  967. static inline void perf_event_exec(void) { }
  968. static inline void perf_event_comm(struct task_struct *tsk, bool exec) { }
  969. static inline void perf_event_fork(struct task_struct *tsk) { }
  970. static inline void perf_event_init(void) { }
  971. static inline int perf_swevent_get_recursion_context(void) { return -1; }
  972. static inline void perf_swevent_put_recursion_context(int rctx) { }
  973. static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; }
  974. static inline void perf_event_enable(struct perf_event *event) { }
  975. static inline void perf_event_disable(struct perf_event *event) { }
  976. static inline int __perf_event_disable(void *info) { return -1; }
  977. static inline void perf_event_task_tick(void) { }
  978. static inline int perf_event_release_kernel(struct perf_event *event) { return 0; }
  979. #endif
  980. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
  981. extern void perf_restore_debug_store(void);
  982. #else
  983. static inline void perf_restore_debug_store(void) { }
  984. #endif
  985. #define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
  986. /*
  987. * This has to have a higher priority than migration_notifier in sched/core.c.
  988. */
  989. #define perf_cpu_notifier(fn) \
  990. do { \
  991. static struct notifier_block fn##_nb = \
  992. { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
  993. unsigned long cpu = smp_processor_id(); \
  994. unsigned long flags; \
  995. \
  996. cpu_notifier_register_begin(); \
  997. fn(&fn##_nb, (unsigned long)CPU_UP_PREPARE, \
  998. (void *)(unsigned long)cpu); \
  999. local_irq_save(flags); \
  1000. fn(&fn##_nb, (unsigned long)CPU_STARTING, \
  1001. (void *)(unsigned long)cpu); \
  1002. local_irq_restore(flags); \
  1003. fn(&fn##_nb, (unsigned long)CPU_ONLINE, \
  1004. (void *)(unsigned long)cpu); \
  1005. __register_cpu_notifier(&fn##_nb); \
  1006. cpu_notifier_register_done(); \
  1007. } while (0)
  1008. /*
  1009. * Bare-bones version of perf_cpu_notifier(), which doesn't invoke the
  1010. * callback for already online CPUs.
  1011. */
  1012. #define __perf_cpu_notifier(fn) \
  1013. do { \
  1014. static struct notifier_block fn##_nb = \
  1015. { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
  1016. \
  1017. __register_cpu_notifier(&fn##_nb); \
  1018. } while (0)
  1019. struct perf_pmu_events_attr {
  1020. struct device_attribute attr;
  1021. u64 id;
  1022. const char *event_str;
  1023. };
  1024. ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
  1025. char *page);
  1026. #define PMU_EVENT_ATTR(_name, _var, _id, _show) \
  1027. static struct perf_pmu_events_attr _var = { \
  1028. .attr = __ATTR(_name, 0444, _show, NULL), \
  1029. .id = _id, \
  1030. };
  1031. #define PMU_EVENT_ATTR_STRING(_name, _var, _str) \
  1032. static struct perf_pmu_events_attr _var = { \
  1033. .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
  1034. .id = 0, \
  1035. .event_str = _str, \
  1036. };
  1037. #define PMU_FORMAT_ATTR(_name, _format) \
  1038. static ssize_t \
  1039. _name##_show(struct device *dev, \
  1040. struct device_attribute *attr, \
  1041. char *page) \
  1042. { \
  1043. BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
  1044. return sprintf(page, _format "\n"); \
  1045. } \
  1046. \
  1047. static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
  1048. #endif /* _LINUX_PERF_EVENT_H */