perf_event.h 51 KB

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