perf_event.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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 <asm/local.h>
  51. struct perf_callchain_entry {
  52. __u64 nr;
  53. __u64 ip[PERF_MAX_STACK_DEPTH];
  54. };
  55. struct perf_raw_record {
  56. u32 size;
  57. void *data;
  58. };
  59. /*
  60. * branch stack layout:
  61. * nr: number of taken branches stored in entries[]
  62. *
  63. * Note that nr can vary from sample to sample
  64. * branches (to, from) are stored from most recent
  65. * to least recent, i.e., entries[0] contains the most
  66. * recent branch.
  67. */
  68. struct perf_branch_stack {
  69. __u64 nr;
  70. struct perf_branch_entry entries[0];
  71. };
  72. struct task_struct;
  73. /*
  74. * extra PMU register associated with an event
  75. */
  76. struct hw_perf_event_extra {
  77. u64 config; /* register value */
  78. unsigned int reg; /* register address or index */
  79. int alloc; /* extra register already allocated */
  80. int idx; /* index in shared_regs->regs[] */
  81. };
  82. struct event_constraint;
  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. struct event_constraint *constraint;
  101. };
  102. struct { /* software */
  103. struct hrtimer hrtimer;
  104. };
  105. struct { /* tracepoint */
  106. struct task_struct *tp_target;
  107. /* for tp_event->class */
  108. struct list_head tp_list;
  109. };
  110. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  111. struct { /* breakpoint */
  112. /*
  113. * Crufty hack to avoid the chicken and egg
  114. * problem hw_breakpoint has with context
  115. * creation and event initalization.
  116. */
  117. struct task_struct *bp_target;
  118. struct arch_hw_breakpoint info;
  119. struct list_head bp_list;
  120. };
  121. #endif
  122. };
  123. int state;
  124. local64_t prev_count;
  125. u64 sample_period;
  126. u64 last_period;
  127. local64_t period_left;
  128. u64 interrupts_seq;
  129. u64 interrupts;
  130. u64 freq_time_stamp;
  131. u64 freq_count_stamp;
  132. #endif
  133. };
  134. /*
  135. * hw_perf_event::state flags
  136. */
  137. #define PERF_HES_STOPPED 0x01 /* the counter is stopped */
  138. #define PERF_HES_UPTODATE 0x02 /* event->count up-to-date */
  139. #define PERF_HES_ARCH 0x04
  140. struct perf_event;
  141. /*
  142. * Common implementation detail of pmu::{start,commit,cancel}_txn
  143. */
  144. #define PERF_EVENT_TXN 0x1
  145. /**
  146. * pmu::capabilities flags
  147. */
  148. #define PERF_PMU_CAP_NO_INTERRUPT 0x01
  149. /**
  150. * struct pmu - generic performance monitoring unit
  151. */
  152. struct pmu {
  153. struct list_head entry;
  154. struct module *module;
  155. struct device *dev;
  156. const struct attribute_group **attr_groups;
  157. const char *name;
  158. int type;
  159. /*
  160. * various common per-pmu feature flags
  161. */
  162. int capabilities;
  163. int * __percpu pmu_disable_count;
  164. struct perf_cpu_context * __percpu pmu_cpu_context;
  165. int task_ctx_nr;
  166. int hrtimer_interval_ms;
  167. /*
  168. * Fully disable/enable this PMU, can be used to protect from the PMI
  169. * as well as for lazy/batch writing of the MSRs.
  170. */
  171. void (*pmu_enable) (struct pmu *pmu); /* optional */
  172. void (*pmu_disable) (struct pmu *pmu); /* optional */
  173. /*
  174. * Try and initialize the event for this PMU.
  175. * Should return -ENOENT when the @event doesn't match this PMU.
  176. */
  177. int (*event_init) (struct perf_event *event);
  178. #define PERF_EF_START 0x01 /* start the counter when adding */
  179. #define PERF_EF_RELOAD 0x02 /* reload the counter when starting */
  180. #define PERF_EF_UPDATE 0x04 /* update the counter when stopping */
  181. /*
  182. * Adds/Removes a counter to/from the PMU, can be done inside
  183. * a transaction, see the ->*_txn() methods.
  184. */
  185. int (*add) (struct perf_event *event, int flags);
  186. void (*del) (struct perf_event *event, int flags);
  187. /*
  188. * Starts/Stops a counter present on the PMU. The PMI handler
  189. * should stop the counter when perf_event_overflow() returns
  190. * !0. ->start() will be used to continue.
  191. */
  192. void (*start) (struct perf_event *event, int flags);
  193. void (*stop) (struct perf_event *event, int flags);
  194. /*
  195. * Updates the counter value of the event.
  196. */
  197. void (*read) (struct perf_event *event);
  198. /*
  199. * Group events scheduling is treated as a transaction, add
  200. * group events as a whole and perform one schedulability test.
  201. * If the test fails, roll back the whole group
  202. *
  203. * Start the transaction, after this ->add() doesn't need to
  204. * do schedulability tests.
  205. */
  206. void (*start_txn) (struct pmu *pmu); /* optional */
  207. /*
  208. * If ->start_txn() disabled the ->add() schedulability test
  209. * then ->commit_txn() is required to perform one. On success
  210. * the transaction is closed. On error the transaction is kept
  211. * open until ->cancel_txn() is called.
  212. */
  213. int (*commit_txn) (struct pmu *pmu); /* optional */
  214. /*
  215. * Will cancel the transaction, assumes ->del() is called
  216. * for each successful ->add() during the transaction.
  217. */
  218. void (*cancel_txn) (struct pmu *pmu); /* optional */
  219. /*
  220. * Will return the value for perf_event_mmap_page::index for this event,
  221. * if no implementation is provided it will default to: event->hw.idx + 1.
  222. */
  223. int (*event_idx) (struct perf_event *event); /*optional */
  224. /*
  225. * flush branch stack on context-switches (needed in cpu-wide mode)
  226. */
  227. void (*flush_branch_stack) (void);
  228. };
  229. /**
  230. * enum perf_event_active_state - the states of a event
  231. */
  232. enum perf_event_active_state {
  233. PERF_EVENT_STATE_EXIT = -3,
  234. PERF_EVENT_STATE_ERROR = -2,
  235. PERF_EVENT_STATE_OFF = -1,
  236. PERF_EVENT_STATE_INACTIVE = 0,
  237. PERF_EVENT_STATE_ACTIVE = 1,
  238. };
  239. struct file;
  240. struct perf_sample_data;
  241. typedef void (*perf_overflow_handler_t)(struct perf_event *,
  242. struct perf_sample_data *,
  243. struct pt_regs *regs);
  244. enum perf_group_flag {
  245. PERF_GROUP_SOFTWARE = 0x1,
  246. };
  247. #define SWEVENT_HLIST_BITS 8
  248. #define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
  249. struct swevent_hlist {
  250. struct hlist_head heads[SWEVENT_HLIST_SIZE];
  251. struct rcu_head rcu_head;
  252. };
  253. #define PERF_ATTACH_CONTEXT 0x01
  254. #define PERF_ATTACH_GROUP 0x02
  255. #define PERF_ATTACH_TASK 0x04
  256. struct perf_cgroup;
  257. struct ring_buffer;
  258. /**
  259. * struct perf_event - performance event kernel representation:
  260. */
  261. struct perf_event {
  262. #ifdef CONFIG_PERF_EVENTS
  263. /*
  264. * entry onto perf_event_context::event_list;
  265. * modifications require ctx->lock
  266. * RCU safe iterations.
  267. */
  268. struct list_head event_entry;
  269. /*
  270. * XXX: group_entry and sibling_list should be mutually exclusive;
  271. * either you're a sibling on a group, or you're the group leader.
  272. * Rework the code to always use the same list element.
  273. *
  274. * Locked for modification by both ctx->mutex and ctx->lock; holding
  275. * either sufficies for read.
  276. */
  277. struct list_head group_entry;
  278. struct list_head sibling_list;
  279. /*
  280. * We need storage to track the entries in perf_pmu_migrate_context; we
  281. * cannot use the event_entry because of RCU and we want to keep the
  282. * group in tact which avoids us using the other two entries.
  283. */
  284. struct list_head migrate_entry;
  285. struct hlist_node hlist_entry;
  286. struct list_head active_entry;
  287. int nr_siblings;
  288. int group_flags;
  289. struct perf_event *group_leader;
  290. struct pmu *pmu;
  291. enum perf_event_active_state state;
  292. unsigned int attach_state;
  293. local64_t count;
  294. atomic64_t child_count;
  295. /*
  296. * These are the total time in nanoseconds that the event
  297. * has been enabled (i.e. eligible to run, and the task has
  298. * been scheduled in, if this is a per-task event)
  299. * and running (scheduled onto the CPU), respectively.
  300. *
  301. * They are computed from tstamp_enabled, tstamp_running and
  302. * tstamp_stopped when the event is in INACTIVE or ACTIVE state.
  303. */
  304. u64 total_time_enabled;
  305. u64 total_time_running;
  306. /*
  307. * These are timestamps used for computing total_time_enabled
  308. * and total_time_running when the event is in INACTIVE or
  309. * ACTIVE state, measured in nanoseconds from an arbitrary point
  310. * in time.
  311. * tstamp_enabled: the notional time when the event was enabled
  312. * tstamp_running: the notional time when the event was scheduled on
  313. * tstamp_stopped: in INACTIVE state, the notional time when the
  314. * event was scheduled off.
  315. */
  316. u64 tstamp_enabled;
  317. u64 tstamp_running;
  318. u64 tstamp_stopped;
  319. /*
  320. * timestamp shadows the actual context timing but it can
  321. * be safely used in NMI interrupt context. It reflects the
  322. * context time as it was when the event was last scheduled in.
  323. *
  324. * ctx_time already accounts for ctx->timestamp. Therefore to
  325. * compute ctx_time for a sample, simply add perf_clock().
  326. */
  327. u64 shadow_ctx_time;
  328. struct perf_event_attr attr;
  329. u16 header_size;
  330. u16 id_header_size;
  331. u16 read_size;
  332. struct hw_perf_event hw;
  333. struct perf_event_context *ctx;
  334. atomic_long_t refcount;
  335. /*
  336. * These accumulate total time (in nanoseconds) that children
  337. * events have been enabled and running, respectively.
  338. */
  339. atomic64_t child_total_time_enabled;
  340. atomic64_t child_total_time_running;
  341. /*
  342. * Protect attach/detach and child_list:
  343. */
  344. struct mutex child_mutex;
  345. struct list_head child_list;
  346. struct perf_event *parent;
  347. int oncpu;
  348. int cpu;
  349. struct list_head owner_entry;
  350. struct task_struct *owner;
  351. /* mmap bits */
  352. struct mutex mmap_mutex;
  353. atomic_t mmap_count;
  354. struct ring_buffer *rb;
  355. struct list_head rb_entry;
  356. unsigned long rcu_batches;
  357. int rcu_pending;
  358. /* poll related */
  359. wait_queue_head_t waitq;
  360. struct fasync_struct *fasync;
  361. /* delayed work for NMIs and such */
  362. int pending_wakeup;
  363. int pending_kill;
  364. int pending_disable;
  365. struct irq_work pending;
  366. atomic_t event_limit;
  367. void (*destroy)(struct perf_event *);
  368. struct rcu_head rcu_head;
  369. struct pid_namespace *ns;
  370. u64 id;
  371. perf_overflow_handler_t overflow_handler;
  372. void *overflow_handler_context;
  373. #ifdef CONFIG_EVENT_TRACING
  374. struct ftrace_event_call *tp_event;
  375. struct event_filter *filter;
  376. #ifdef CONFIG_FUNCTION_TRACER
  377. struct ftrace_ops ftrace_ops;
  378. #endif
  379. #endif
  380. #ifdef CONFIG_CGROUP_PERF
  381. struct perf_cgroup *cgrp; /* cgroup event is attach to */
  382. int cgrp_defer_enabled;
  383. #endif
  384. #endif /* CONFIG_PERF_EVENTS */
  385. };
  386. /**
  387. * struct perf_event_context - event context structure
  388. *
  389. * Used as a container for task events and CPU events as well:
  390. */
  391. struct perf_event_context {
  392. struct pmu *pmu;
  393. /*
  394. * Protect the states of the events in the list,
  395. * nr_active, and the list:
  396. */
  397. raw_spinlock_t lock;
  398. /*
  399. * Protect the list of events. Locking either mutex or lock
  400. * is sufficient to ensure the list doesn't change; to change
  401. * the list you need to lock both the mutex and the spinlock.
  402. */
  403. struct mutex mutex;
  404. struct list_head active_ctx_list;
  405. struct list_head pinned_groups;
  406. struct list_head flexible_groups;
  407. struct list_head event_list;
  408. int nr_events;
  409. int nr_active;
  410. int is_active;
  411. int nr_stat;
  412. int nr_freq;
  413. int rotate_disable;
  414. atomic_t refcount;
  415. struct task_struct *task;
  416. /*
  417. * Context clock, runs when context enabled.
  418. */
  419. u64 time;
  420. u64 timestamp;
  421. /*
  422. * These fields let us detect when two contexts have both
  423. * been cloned (inherited) from a common ancestor.
  424. */
  425. struct perf_event_context *parent_ctx;
  426. u64 parent_gen;
  427. u64 generation;
  428. int pin_count;
  429. int nr_cgroups; /* cgroup evts */
  430. int nr_branch_stack; /* branch_stack evt */
  431. struct rcu_head rcu_head;
  432. struct delayed_work orphans_remove;
  433. bool orphans_remove_sched;
  434. };
  435. /*
  436. * Number of contexts where an event can trigger:
  437. * task, softirq, hardirq, nmi.
  438. */
  439. #define PERF_NR_CONTEXTS 4
  440. /**
  441. * struct perf_event_cpu_context - per cpu event context structure
  442. */
  443. struct perf_cpu_context {
  444. struct perf_event_context ctx;
  445. struct perf_event_context *task_ctx;
  446. int active_oncpu;
  447. int exclusive;
  448. struct hrtimer hrtimer;
  449. ktime_t hrtimer_interval;
  450. struct pmu *unique_pmu;
  451. struct perf_cgroup *cgrp;
  452. };
  453. struct perf_output_handle {
  454. struct perf_event *event;
  455. struct ring_buffer *rb;
  456. unsigned long wakeup;
  457. unsigned long size;
  458. void *addr;
  459. int page;
  460. };
  461. #ifdef CONFIG_PERF_EVENTS
  462. extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
  463. extern void perf_pmu_unregister(struct pmu *pmu);
  464. extern int perf_num_counters(void);
  465. extern const char *perf_pmu_name(void);
  466. extern void __perf_event_task_sched_in(struct task_struct *prev,
  467. struct task_struct *task);
  468. extern void __perf_event_task_sched_out(struct task_struct *prev,
  469. struct task_struct *next);
  470. extern int perf_event_init_task(struct task_struct *child);
  471. extern void perf_event_exit_task(struct task_struct *child);
  472. extern void perf_event_free_task(struct task_struct *task);
  473. extern void perf_event_delayed_put(struct task_struct *task);
  474. extern void perf_event_print_debug(void);
  475. extern void perf_pmu_disable(struct pmu *pmu);
  476. extern void perf_pmu_enable(struct pmu *pmu);
  477. extern int perf_event_task_disable(void);
  478. extern int perf_event_task_enable(void);
  479. extern int perf_event_refresh(struct perf_event *event, int refresh);
  480. extern void perf_event_update_userpage(struct perf_event *event);
  481. extern int perf_event_release_kernel(struct perf_event *event);
  482. extern struct perf_event *
  483. perf_event_create_kernel_counter(struct perf_event_attr *attr,
  484. int cpu,
  485. struct task_struct *task,
  486. perf_overflow_handler_t callback,
  487. void *context);
  488. extern void perf_pmu_migrate_context(struct pmu *pmu,
  489. int src_cpu, int dst_cpu);
  490. extern u64 perf_event_read_value(struct perf_event *event,
  491. u64 *enabled, u64 *running);
  492. struct perf_sample_data {
  493. /*
  494. * Fields set by perf_sample_data_init(), group so as to
  495. * minimize the cachelines touched.
  496. */
  497. u64 addr;
  498. struct perf_raw_record *raw;
  499. struct perf_branch_stack *br_stack;
  500. u64 period;
  501. u64 weight;
  502. u64 txn;
  503. union perf_mem_data_src data_src;
  504. /*
  505. * The other fields, optionally {set,used} by
  506. * perf_{prepare,output}_sample().
  507. */
  508. u64 type;
  509. u64 ip;
  510. struct {
  511. u32 pid;
  512. u32 tid;
  513. } tid_entry;
  514. u64 time;
  515. u64 id;
  516. u64 stream_id;
  517. struct {
  518. u32 cpu;
  519. u32 reserved;
  520. } cpu_entry;
  521. struct perf_callchain_entry *callchain;
  522. /*
  523. * regs_user may point to task_pt_regs or to regs_user_copy, depending
  524. * on arch details.
  525. */
  526. struct perf_regs regs_user;
  527. struct pt_regs regs_user_copy;
  528. struct perf_regs regs_intr;
  529. u64 stack_user_size;
  530. } ____cacheline_aligned;
  531. /* default value for data source */
  532. #define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
  533. PERF_MEM_S(LVL, NA) |\
  534. PERF_MEM_S(SNOOP, NA) |\
  535. PERF_MEM_S(LOCK, NA) |\
  536. PERF_MEM_S(TLB, NA))
  537. static inline void perf_sample_data_init(struct perf_sample_data *data,
  538. u64 addr, u64 period)
  539. {
  540. /* remaining struct members initialized in perf_prepare_sample() */
  541. data->addr = addr;
  542. data->raw = NULL;
  543. data->br_stack = NULL;
  544. data->period = period;
  545. data->weight = 0;
  546. data->data_src.val = PERF_MEM_NA;
  547. data->txn = 0;
  548. }
  549. extern void perf_output_sample(struct perf_output_handle *handle,
  550. struct perf_event_header *header,
  551. struct perf_sample_data *data,
  552. struct perf_event *event);
  553. extern void perf_prepare_sample(struct perf_event_header *header,
  554. struct perf_sample_data *data,
  555. struct perf_event *event,
  556. struct pt_regs *regs);
  557. extern int perf_event_overflow(struct perf_event *event,
  558. struct perf_sample_data *data,
  559. struct pt_regs *regs);
  560. static inline bool is_sampling_event(struct perf_event *event)
  561. {
  562. return event->attr.sample_period != 0;
  563. }
  564. /*
  565. * Return 1 for a software event, 0 for a hardware event
  566. */
  567. static inline int is_software_event(struct perf_event *event)
  568. {
  569. return event->pmu->task_ctx_nr == perf_sw_context;
  570. }
  571. extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
  572. extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
  573. extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
  574. #ifndef perf_arch_fetch_caller_regs
  575. static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
  576. #endif
  577. /*
  578. * Take a snapshot of the regs. Skip ip and frame pointer to
  579. * the nth caller. We only need a few of the regs:
  580. * - ip for PERF_SAMPLE_IP
  581. * - cs for user_mode() tests
  582. * - bp for callchains
  583. * - eflags, for future purposes, just in case
  584. */
  585. static inline void perf_fetch_caller_regs(struct pt_regs *regs)
  586. {
  587. memset(regs, 0, sizeof(*regs));
  588. perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
  589. }
  590. static __always_inline void
  591. perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  592. {
  593. if (static_key_false(&perf_swevent_enabled[event_id]))
  594. __perf_sw_event(event_id, nr, regs, addr);
  595. }
  596. DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
  597. /*
  598. * 'Special' version for the scheduler, it hard assumes no recursion,
  599. * which is guaranteed by us not actually scheduling inside other swevents
  600. * because those disable preemption.
  601. */
  602. static __always_inline void
  603. perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
  604. {
  605. if (static_key_false(&perf_swevent_enabled[event_id])) {
  606. struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
  607. perf_fetch_caller_regs(regs);
  608. ___perf_sw_event(event_id, nr, regs, addr);
  609. }
  610. }
  611. extern struct static_key_deferred perf_sched_events;
  612. static inline void perf_event_task_sched_in(struct task_struct *prev,
  613. struct task_struct *task)
  614. {
  615. if (static_key_false(&perf_sched_events.key))
  616. __perf_event_task_sched_in(prev, task);
  617. }
  618. static inline void perf_event_task_sched_out(struct task_struct *prev,
  619. struct task_struct *next)
  620. {
  621. perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
  622. if (static_key_false(&perf_sched_events.key))
  623. __perf_event_task_sched_out(prev, next);
  624. }
  625. extern void perf_event_mmap(struct vm_area_struct *vma);
  626. extern struct perf_guest_info_callbacks *perf_guest_cbs;
  627. extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  628. extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  629. extern void perf_event_exec(void);
  630. extern void perf_event_comm(struct task_struct *tsk, bool exec);
  631. extern void perf_event_fork(struct task_struct *tsk);
  632. /* Callchains */
  633. DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
  634. extern void perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs);
  635. extern void perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs);
  636. static inline void perf_callchain_store(struct perf_callchain_entry *entry, u64 ip)
  637. {
  638. if (entry->nr < PERF_MAX_STACK_DEPTH)
  639. entry->ip[entry->nr++] = ip;
  640. }
  641. extern int sysctl_perf_event_paranoid;
  642. extern int sysctl_perf_event_mlock;
  643. extern int sysctl_perf_event_sample_rate;
  644. extern int sysctl_perf_cpu_time_max_percent;
  645. extern void perf_sample_event_took(u64 sample_len_ns);
  646. extern int perf_proc_update_handler(struct ctl_table *table, int write,
  647. void __user *buffer, size_t *lenp,
  648. loff_t *ppos);
  649. extern int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
  650. void __user *buffer, size_t *lenp,
  651. loff_t *ppos);
  652. static inline bool perf_paranoid_tracepoint_raw(void)
  653. {
  654. return sysctl_perf_event_paranoid > -1;
  655. }
  656. static inline bool perf_paranoid_cpu(void)
  657. {
  658. return sysctl_perf_event_paranoid > 0;
  659. }
  660. static inline bool perf_paranoid_kernel(void)
  661. {
  662. return sysctl_perf_event_paranoid > 1;
  663. }
  664. extern void perf_event_init(void);
  665. extern void perf_tp_event(u64 addr, u64 count, void *record,
  666. int entry_size, struct pt_regs *regs,
  667. struct hlist_head *head, int rctx,
  668. struct task_struct *task);
  669. extern void perf_bp_event(struct perf_event *event, void *data);
  670. #ifndef perf_misc_flags
  671. # define perf_misc_flags(regs) \
  672. (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
  673. # define perf_instruction_pointer(regs) instruction_pointer(regs)
  674. #endif
  675. static inline bool has_branch_stack(struct perf_event *event)
  676. {
  677. return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
  678. }
  679. extern int perf_output_begin(struct perf_output_handle *handle,
  680. struct perf_event *event, unsigned int size);
  681. extern void perf_output_end(struct perf_output_handle *handle);
  682. extern unsigned int perf_output_copy(struct perf_output_handle *handle,
  683. const void *buf, unsigned int len);
  684. extern unsigned int perf_output_skip(struct perf_output_handle *handle,
  685. unsigned int len);
  686. extern int perf_swevent_get_recursion_context(void);
  687. extern void perf_swevent_put_recursion_context(int rctx);
  688. extern u64 perf_swevent_set_period(struct perf_event *event);
  689. extern void perf_event_enable(struct perf_event *event);
  690. extern void perf_event_disable(struct perf_event *event);
  691. extern int __perf_event_disable(void *info);
  692. extern void perf_event_task_tick(void);
  693. #else /* !CONFIG_PERF_EVENTS: */
  694. static inline void
  695. perf_event_task_sched_in(struct task_struct *prev,
  696. struct task_struct *task) { }
  697. static inline void
  698. perf_event_task_sched_out(struct task_struct *prev,
  699. struct task_struct *next) { }
  700. static inline int perf_event_init_task(struct task_struct *child) { return 0; }
  701. static inline void perf_event_exit_task(struct task_struct *child) { }
  702. static inline void perf_event_free_task(struct task_struct *task) { }
  703. static inline void perf_event_delayed_put(struct task_struct *task) { }
  704. static inline void perf_event_print_debug(void) { }
  705. static inline int perf_event_task_disable(void) { return -EINVAL; }
  706. static inline int perf_event_task_enable(void) { return -EINVAL; }
  707. static inline int perf_event_refresh(struct perf_event *event, int refresh)
  708. {
  709. return -EINVAL;
  710. }
  711. static inline void
  712. perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
  713. static inline void
  714. perf_sw_event_sched(u32 event_id, u64 nr, u64 addr) { }
  715. static inline void
  716. perf_bp_event(struct perf_event *event, void *data) { }
  717. static inline int perf_register_guest_info_callbacks
  718. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  719. static inline int perf_unregister_guest_info_callbacks
  720. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  721. static inline void perf_event_mmap(struct vm_area_struct *vma) { }
  722. static inline void perf_event_exec(void) { }
  723. static inline void perf_event_comm(struct task_struct *tsk, bool exec) { }
  724. static inline void perf_event_fork(struct task_struct *tsk) { }
  725. static inline void perf_event_init(void) { }
  726. static inline int perf_swevent_get_recursion_context(void) { return -1; }
  727. static inline void perf_swevent_put_recursion_context(int rctx) { }
  728. static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; }
  729. static inline void perf_event_enable(struct perf_event *event) { }
  730. static inline void perf_event_disable(struct perf_event *event) { }
  731. static inline int __perf_event_disable(void *info) { return -1; }
  732. static inline void perf_event_task_tick(void) { }
  733. #endif
  734. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_NO_HZ_FULL)
  735. extern bool perf_event_can_stop_tick(void);
  736. #else
  737. static inline bool perf_event_can_stop_tick(void) { return true; }
  738. #endif
  739. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
  740. extern void perf_restore_debug_store(void);
  741. #else
  742. static inline void perf_restore_debug_store(void) { }
  743. #endif
  744. #define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
  745. /*
  746. * This has to have a higher priority than migration_notifier in sched/core.c.
  747. */
  748. #define perf_cpu_notifier(fn) \
  749. do { \
  750. static struct notifier_block fn##_nb = \
  751. { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
  752. unsigned long cpu = smp_processor_id(); \
  753. unsigned long flags; \
  754. \
  755. cpu_notifier_register_begin(); \
  756. fn(&fn##_nb, (unsigned long)CPU_UP_PREPARE, \
  757. (void *)(unsigned long)cpu); \
  758. local_irq_save(flags); \
  759. fn(&fn##_nb, (unsigned long)CPU_STARTING, \
  760. (void *)(unsigned long)cpu); \
  761. local_irq_restore(flags); \
  762. fn(&fn##_nb, (unsigned long)CPU_ONLINE, \
  763. (void *)(unsigned long)cpu); \
  764. __register_cpu_notifier(&fn##_nb); \
  765. cpu_notifier_register_done(); \
  766. } while (0)
  767. /*
  768. * Bare-bones version of perf_cpu_notifier(), which doesn't invoke the
  769. * callback for already online CPUs.
  770. */
  771. #define __perf_cpu_notifier(fn) \
  772. do { \
  773. static struct notifier_block fn##_nb = \
  774. { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
  775. \
  776. __register_cpu_notifier(&fn##_nb); \
  777. } while (0)
  778. struct perf_pmu_events_attr {
  779. struct device_attribute attr;
  780. u64 id;
  781. const char *event_str;
  782. };
  783. #define PMU_EVENT_ATTR(_name, _var, _id, _show) \
  784. static struct perf_pmu_events_attr _var = { \
  785. .attr = __ATTR(_name, 0444, _show, NULL), \
  786. .id = _id, \
  787. };
  788. #define PMU_FORMAT_ATTR(_name, _format) \
  789. static ssize_t \
  790. _name##_show(struct device *dev, \
  791. struct device_attribute *attr, \
  792. char *page) \
  793. { \
  794. BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
  795. return sprintf(page, _format "\n"); \
  796. } \
  797. \
  798. static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
  799. #endif /* _LINUX_PERF_EVENT_H */