perf_event.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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[0]; /* /proc/sys/kernel/perf_event_max_stack */
  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_forward(struct perf_event *event,
  735. struct perf_sample_data *data,
  736. struct pt_regs *regs);
  737. extern void perf_event_output_backward(struct perf_event *event,
  738. struct perf_sample_data *data,
  739. struct pt_regs *regs);
  740. extern void perf_event_output(struct perf_event *event,
  741. struct perf_sample_data *data,
  742. struct pt_regs *regs);
  743. static inline bool
  744. is_default_overflow_handler(struct perf_event *event)
  745. {
  746. if (likely(event->overflow_handler == perf_event_output_forward))
  747. return true;
  748. if (unlikely(event->overflow_handler == perf_event_output_backward))
  749. return true;
  750. return false;
  751. }
  752. extern void
  753. perf_event_header__init_id(struct perf_event_header *header,
  754. struct perf_sample_data *data,
  755. struct perf_event *event);
  756. extern void
  757. perf_event__output_id_sample(struct perf_event *event,
  758. struct perf_output_handle *handle,
  759. struct perf_sample_data *sample);
  760. extern void
  761. perf_log_lost_samples(struct perf_event *event, u64 lost);
  762. static inline bool is_sampling_event(struct perf_event *event)
  763. {
  764. return event->attr.sample_period != 0;
  765. }
  766. /*
  767. * Return 1 for a software event, 0 for a hardware event
  768. */
  769. static inline int is_software_event(struct perf_event *event)
  770. {
  771. return event->pmu->task_ctx_nr == perf_sw_context;
  772. }
  773. extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
  774. extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
  775. extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
  776. #ifndef perf_arch_fetch_caller_regs
  777. static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
  778. #endif
  779. /*
  780. * Take a snapshot of the regs. Skip ip and frame pointer to
  781. * the nth caller. We only need a few of the regs:
  782. * - ip for PERF_SAMPLE_IP
  783. * - cs for user_mode() tests
  784. * - bp for callchains
  785. * - eflags, for future purposes, just in case
  786. */
  787. static inline void perf_fetch_caller_regs(struct pt_regs *regs)
  788. {
  789. memset(regs, 0, sizeof(*regs));
  790. perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
  791. }
  792. static __always_inline void
  793. perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  794. {
  795. if (static_key_false(&perf_swevent_enabled[event_id]))
  796. __perf_sw_event(event_id, nr, regs, addr);
  797. }
  798. DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
  799. /*
  800. * 'Special' version for the scheduler, it hard assumes no recursion,
  801. * which is guaranteed by us not actually scheduling inside other swevents
  802. * because those disable preemption.
  803. */
  804. static __always_inline void
  805. perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
  806. {
  807. if (static_key_false(&perf_swevent_enabled[event_id])) {
  808. struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
  809. perf_fetch_caller_regs(regs);
  810. ___perf_sw_event(event_id, nr, regs, addr);
  811. }
  812. }
  813. extern struct static_key_false perf_sched_events;
  814. static __always_inline bool
  815. perf_sw_migrate_enabled(void)
  816. {
  817. if (static_key_false(&perf_swevent_enabled[PERF_COUNT_SW_CPU_MIGRATIONS]))
  818. return true;
  819. return false;
  820. }
  821. static inline void perf_event_task_migrate(struct task_struct *task)
  822. {
  823. if (perf_sw_migrate_enabled())
  824. task->sched_migrated = 1;
  825. }
  826. static inline void perf_event_task_sched_in(struct task_struct *prev,
  827. struct task_struct *task)
  828. {
  829. if (static_branch_unlikely(&perf_sched_events))
  830. __perf_event_task_sched_in(prev, task);
  831. if (perf_sw_migrate_enabled() && task->sched_migrated) {
  832. struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
  833. perf_fetch_caller_regs(regs);
  834. ___perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, regs, 0);
  835. task->sched_migrated = 0;
  836. }
  837. }
  838. static inline void perf_event_task_sched_out(struct task_struct *prev,
  839. struct task_struct *next)
  840. {
  841. perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
  842. if (static_branch_unlikely(&perf_sched_events))
  843. __perf_event_task_sched_out(prev, next);
  844. }
  845. static inline u64 __perf_event_count(struct perf_event *event)
  846. {
  847. return local64_read(&event->count) + atomic64_read(&event->child_count);
  848. }
  849. extern void perf_event_mmap(struct vm_area_struct *vma);
  850. extern struct perf_guest_info_callbacks *perf_guest_cbs;
  851. extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  852. extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  853. extern void perf_event_exec(void);
  854. extern void perf_event_comm(struct task_struct *tsk, bool exec);
  855. extern void perf_event_fork(struct task_struct *tsk);
  856. /* Callchains */
  857. DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
  858. extern void perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs);
  859. extern void perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs);
  860. extern struct perf_callchain_entry *
  861. get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
  862. bool crosstask, bool add_mark);
  863. extern int get_callchain_buffers(void);
  864. extern void put_callchain_buffers(void);
  865. extern int sysctl_perf_event_max_stack;
  866. static inline int perf_callchain_store(struct perf_callchain_entry *entry, u64 ip)
  867. {
  868. if (entry->nr < sysctl_perf_event_max_stack) {
  869. entry->ip[entry->nr++] = ip;
  870. return 0;
  871. } else {
  872. return -1; /* no more room, stop walking the stack */
  873. }
  874. }
  875. extern int sysctl_perf_event_paranoid;
  876. extern int sysctl_perf_event_mlock;
  877. extern int sysctl_perf_event_sample_rate;
  878. extern int sysctl_perf_cpu_time_max_percent;
  879. extern void perf_sample_event_took(u64 sample_len_ns);
  880. extern int perf_proc_update_handler(struct ctl_table *table, int write,
  881. void __user *buffer, size_t *lenp,
  882. loff_t *ppos);
  883. extern int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
  884. void __user *buffer, size_t *lenp,
  885. loff_t *ppos);
  886. int perf_event_max_stack_handler(struct ctl_table *table, int write,
  887. void __user *buffer, size_t *lenp, loff_t *ppos);
  888. static inline bool perf_paranoid_tracepoint_raw(void)
  889. {
  890. return sysctl_perf_event_paranoid > -1;
  891. }
  892. static inline bool perf_paranoid_cpu(void)
  893. {
  894. return sysctl_perf_event_paranoid > 0;
  895. }
  896. static inline bool perf_paranoid_kernel(void)
  897. {
  898. return sysctl_perf_event_paranoid > 1;
  899. }
  900. extern void perf_event_init(void);
  901. extern void perf_tp_event(u64 addr, u64 count, void *record,
  902. int entry_size, struct pt_regs *regs,
  903. struct hlist_head *head, int rctx,
  904. struct task_struct *task);
  905. extern void perf_bp_event(struct perf_event *event, void *data);
  906. #ifndef perf_misc_flags
  907. # define perf_misc_flags(regs) \
  908. (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
  909. # define perf_instruction_pointer(regs) instruction_pointer(regs)
  910. #endif
  911. static inline bool has_branch_stack(struct perf_event *event)
  912. {
  913. return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
  914. }
  915. static inline bool needs_branch_stack(struct perf_event *event)
  916. {
  917. return event->attr.branch_sample_type != 0;
  918. }
  919. static inline bool has_aux(struct perf_event *event)
  920. {
  921. return event->pmu->setup_aux;
  922. }
  923. static inline bool is_write_backward(struct perf_event *event)
  924. {
  925. return !!event->attr.write_backward;
  926. }
  927. extern int perf_output_begin(struct perf_output_handle *handle,
  928. struct perf_event *event, unsigned int size);
  929. extern int perf_output_begin_forward(struct perf_output_handle *handle,
  930. struct perf_event *event,
  931. unsigned int size);
  932. extern int perf_output_begin_backward(struct perf_output_handle *handle,
  933. struct perf_event *event,
  934. unsigned int size);
  935. extern void perf_output_end(struct perf_output_handle *handle);
  936. extern unsigned int perf_output_copy(struct perf_output_handle *handle,
  937. const void *buf, unsigned int len);
  938. extern unsigned int perf_output_skip(struct perf_output_handle *handle,
  939. unsigned int len);
  940. extern int perf_swevent_get_recursion_context(void);
  941. extern void perf_swevent_put_recursion_context(int rctx);
  942. extern u64 perf_swevent_set_period(struct perf_event *event);
  943. extern void perf_event_enable(struct perf_event *event);
  944. extern void perf_event_disable(struct perf_event *event);
  945. extern void perf_event_disable_local(struct perf_event *event);
  946. extern void perf_event_task_tick(void);
  947. #else /* !CONFIG_PERF_EVENTS: */
  948. static inline void *
  949. perf_aux_output_begin(struct perf_output_handle *handle,
  950. struct perf_event *event) { return NULL; }
  951. static inline void
  952. perf_aux_output_end(struct perf_output_handle *handle, unsigned long size,
  953. bool truncated) { }
  954. static inline int
  955. perf_aux_output_skip(struct perf_output_handle *handle,
  956. unsigned long size) { return -EINVAL; }
  957. static inline void *
  958. perf_get_aux(struct perf_output_handle *handle) { return NULL; }
  959. static inline void
  960. perf_event_task_migrate(struct task_struct *task) { }
  961. static inline void
  962. perf_event_task_sched_in(struct task_struct *prev,
  963. struct task_struct *task) { }
  964. static inline void
  965. perf_event_task_sched_out(struct task_struct *prev,
  966. struct task_struct *next) { }
  967. static inline int perf_event_init_task(struct task_struct *child) { return 0; }
  968. static inline void perf_event_exit_task(struct task_struct *child) { }
  969. static inline void perf_event_free_task(struct task_struct *task) { }
  970. static inline void perf_event_delayed_put(struct task_struct *task) { }
  971. static inline struct file *perf_event_get(unsigned int fd) { return ERR_PTR(-EINVAL); }
  972. static inline const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
  973. {
  974. return ERR_PTR(-EINVAL);
  975. }
  976. static inline u64 perf_event_read_local(struct perf_event *event) { return -EINVAL; }
  977. static inline void perf_event_print_debug(void) { }
  978. static inline int perf_event_task_disable(void) { return -EINVAL; }
  979. static inline int perf_event_task_enable(void) { return -EINVAL; }
  980. static inline int perf_event_refresh(struct perf_event *event, int refresh)
  981. {
  982. return -EINVAL;
  983. }
  984. static inline void
  985. perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
  986. static inline void
  987. perf_sw_event_sched(u32 event_id, u64 nr, u64 addr) { }
  988. static inline void
  989. perf_bp_event(struct perf_event *event, void *data) { }
  990. static inline int perf_register_guest_info_callbacks
  991. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  992. static inline int perf_unregister_guest_info_callbacks
  993. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  994. static inline void perf_event_mmap(struct vm_area_struct *vma) { }
  995. static inline void perf_event_exec(void) { }
  996. static inline void perf_event_comm(struct task_struct *tsk, bool exec) { }
  997. static inline void perf_event_fork(struct task_struct *tsk) { }
  998. static inline void perf_event_init(void) { }
  999. static inline int perf_swevent_get_recursion_context(void) { return -1; }
  1000. static inline void perf_swevent_put_recursion_context(int rctx) { }
  1001. static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; }
  1002. static inline void perf_event_enable(struct perf_event *event) { }
  1003. static inline void perf_event_disable(struct perf_event *event) { }
  1004. static inline int __perf_event_disable(void *info) { return -1; }
  1005. static inline void perf_event_task_tick(void) { }
  1006. static inline int perf_event_release_kernel(struct perf_event *event) { return 0; }
  1007. #endif
  1008. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
  1009. extern void perf_restore_debug_store(void);
  1010. #else
  1011. static inline void perf_restore_debug_store(void) { }
  1012. #endif
  1013. #define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
  1014. /*
  1015. * This has to have a higher priority than migration_notifier in sched/core.c.
  1016. */
  1017. #define perf_cpu_notifier(fn) \
  1018. do { \
  1019. static struct notifier_block fn##_nb = \
  1020. { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
  1021. unsigned long cpu = smp_processor_id(); \
  1022. unsigned long flags; \
  1023. \
  1024. cpu_notifier_register_begin(); \
  1025. fn(&fn##_nb, (unsigned long)CPU_UP_PREPARE, \
  1026. (void *)(unsigned long)cpu); \
  1027. local_irq_save(flags); \
  1028. fn(&fn##_nb, (unsigned long)CPU_STARTING, \
  1029. (void *)(unsigned long)cpu); \
  1030. local_irq_restore(flags); \
  1031. fn(&fn##_nb, (unsigned long)CPU_ONLINE, \
  1032. (void *)(unsigned long)cpu); \
  1033. __register_cpu_notifier(&fn##_nb); \
  1034. cpu_notifier_register_done(); \
  1035. } while (0)
  1036. /*
  1037. * Bare-bones version of perf_cpu_notifier(), which doesn't invoke the
  1038. * callback for already online CPUs.
  1039. */
  1040. #define __perf_cpu_notifier(fn) \
  1041. do { \
  1042. static struct notifier_block fn##_nb = \
  1043. { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
  1044. \
  1045. __register_cpu_notifier(&fn##_nb); \
  1046. } while (0)
  1047. struct perf_pmu_events_attr {
  1048. struct device_attribute attr;
  1049. u64 id;
  1050. const char *event_str;
  1051. };
  1052. ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
  1053. char *page);
  1054. #define PMU_EVENT_ATTR(_name, _var, _id, _show) \
  1055. static struct perf_pmu_events_attr _var = { \
  1056. .attr = __ATTR(_name, 0444, _show, NULL), \
  1057. .id = _id, \
  1058. };
  1059. #define PMU_EVENT_ATTR_STRING(_name, _var, _str) \
  1060. static struct perf_pmu_events_attr _var = { \
  1061. .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
  1062. .id = 0, \
  1063. .event_str = _str, \
  1064. };
  1065. #define PMU_FORMAT_ATTR(_name, _format) \
  1066. static ssize_t \
  1067. _name##_show(struct device *dev, \
  1068. struct device_attribute *attr, \
  1069. char *page) \
  1070. { \
  1071. BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
  1072. return sprintf(page, _format "\n"); \
  1073. } \
  1074. \
  1075. static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
  1076. #endif /* _LINUX_PERF_EVENT_H */