perf_event.h 34 KB

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