perf_counter.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462
  1. /*
  2. * Performance counter core code
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  7. * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  8. *
  9. * For licensing details see kernel-base/COPYING
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/mm.h>
  13. #include <linux/cpu.h>
  14. #include <linux/smp.h>
  15. #include <linux/file.h>
  16. #include <linux/poll.h>
  17. #include <linux/sysfs.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/percpu.h>
  20. #include <linux/vmstat.h>
  21. #include <linux/hardirq.h>
  22. #include <linux/rculist.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/syscalls.h>
  25. #include <linux/anon_inodes.h>
  26. #include <linux/kernel_stat.h>
  27. #include <linux/perf_counter.h>
  28. #include <linux/dcache.h>
  29. #include <asm/irq_regs.h>
  30. /*
  31. * Each CPU has a list of per CPU counters:
  32. */
  33. DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
  34. int perf_max_counters __read_mostly = 1;
  35. static int perf_reserved_percpu __read_mostly;
  36. static int perf_overcommit __read_mostly = 1;
  37. static atomic_t nr_counters __read_mostly;
  38. static atomic_t nr_mmap_tracking __read_mostly;
  39. static atomic_t nr_munmap_tracking __read_mostly;
  40. static atomic_t nr_comm_tracking __read_mostly;
  41. int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
  42. int sysctl_perf_counter_mlock __read_mostly = 128; /* 'free' kb per counter */
  43. /*
  44. * Lock for (sysadmin-configurable) counter reservations:
  45. */
  46. static DEFINE_SPINLOCK(perf_resource_lock);
  47. /*
  48. * Architecture provided APIs - weak aliases:
  49. */
  50. extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
  51. {
  52. return NULL;
  53. }
  54. u64 __weak hw_perf_save_disable(void) { return 0; }
  55. void __weak hw_perf_restore(u64 ctrl) { barrier(); }
  56. void __weak hw_perf_counter_setup(int cpu) { barrier(); }
  57. int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
  58. struct perf_cpu_context *cpuctx,
  59. struct perf_counter_context *ctx, int cpu)
  60. {
  61. return 0;
  62. }
  63. void __weak perf_counter_print_debug(void) { }
  64. static void
  65. list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  66. {
  67. struct perf_counter *group_leader = counter->group_leader;
  68. /*
  69. * Depending on whether it is a standalone or sibling counter,
  70. * add it straight to the context's counter list, or to the group
  71. * leader's sibling list:
  72. */
  73. if (group_leader == counter)
  74. list_add_tail(&counter->list_entry, &ctx->counter_list);
  75. else {
  76. list_add_tail(&counter->list_entry, &group_leader->sibling_list);
  77. group_leader->nr_siblings++;
  78. }
  79. list_add_rcu(&counter->event_entry, &ctx->event_list);
  80. }
  81. static void
  82. list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  83. {
  84. struct perf_counter *sibling, *tmp;
  85. list_del_init(&counter->list_entry);
  86. list_del_rcu(&counter->event_entry);
  87. if (counter->group_leader != counter)
  88. counter->group_leader->nr_siblings--;
  89. /*
  90. * If this was a group counter with sibling counters then
  91. * upgrade the siblings to singleton counters by adding them
  92. * to the context list directly:
  93. */
  94. list_for_each_entry_safe(sibling, tmp,
  95. &counter->sibling_list, list_entry) {
  96. list_move_tail(&sibling->list_entry, &ctx->counter_list);
  97. sibling->group_leader = sibling;
  98. }
  99. }
  100. static void
  101. counter_sched_out(struct perf_counter *counter,
  102. struct perf_cpu_context *cpuctx,
  103. struct perf_counter_context *ctx)
  104. {
  105. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  106. return;
  107. counter->state = PERF_COUNTER_STATE_INACTIVE;
  108. counter->tstamp_stopped = ctx->time;
  109. counter->pmu->disable(counter);
  110. counter->oncpu = -1;
  111. if (!is_software_counter(counter))
  112. cpuctx->active_oncpu--;
  113. ctx->nr_active--;
  114. if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
  115. cpuctx->exclusive = 0;
  116. }
  117. static void
  118. group_sched_out(struct perf_counter *group_counter,
  119. struct perf_cpu_context *cpuctx,
  120. struct perf_counter_context *ctx)
  121. {
  122. struct perf_counter *counter;
  123. if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
  124. return;
  125. counter_sched_out(group_counter, cpuctx, ctx);
  126. /*
  127. * Schedule out siblings (if any):
  128. */
  129. list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
  130. counter_sched_out(counter, cpuctx, ctx);
  131. if (group_counter->hw_event.exclusive)
  132. cpuctx->exclusive = 0;
  133. }
  134. /*
  135. * Cross CPU call to remove a performance counter
  136. *
  137. * We disable the counter on the hardware level first. After that we
  138. * remove it from the context list.
  139. */
  140. static void __perf_counter_remove_from_context(void *info)
  141. {
  142. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  143. struct perf_counter *counter = info;
  144. struct perf_counter_context *ctx = counter->ctx;
  145. unsigned long flags;
  146. u64 perf_flags;
  147. /*
  148. * If this is a task context, we need to check whether it is
  149. * the current task context of this cpu. If not it has been
  150. * scheduled out before the smp call arrived.
  151. */
  152. if (ctx->task && cpuctx->task_ctx != ctx)
  153. return;
  154. spin_lock_irqsave(&ctx->lock, flags);
  155. counter_sched_out(counter, cpuctx, ctx);
  156. counter->task = NULL;
  157. ctx->nr_counters--;
  158. /*
  159. * Protect the list operation against NMI by disabling the
  160. * counters on a global level. NOP for non NMI based counters.
  161. */
  162. perf_flags = hw_perf_save_disable();
  163. list_del_counter(counter, ctx);
  164. hw_perf_restore(perf_flags);
  165. if (!ctx->task) {
  166. /*
  167. * Allow more per task counters with respect to the
  168. * reservation:
  169. */
  170. cpuctx->max_pertask =
  171. min(perf_max_counters - ctx->nr_counters,
  172. perf_max_counters - perf_reserved_percpu);
  173. }
  174. spin_unlock_irqrestore(&ctx->lock, flags);
  175. }
  176. /*
  177. * Remove the counter from a task's (or a CPU's) list of counters.
  178. *
  179. * Must be called with counter->mutex and ctx->mutex held.
  180. *
  181. * CPU counters are removed with a smp call. For task counters we only
  182. * call when the task is on a CPU.
  183. */
  184. static void perf_counter_remove_from_context(struct perf_counter *counter)
  185. {
  186. struct perf_counter_context *ctx = counter->ctx;
  187. struct task_struct *task = ctx->task;
  188. if (!task) {
  189. /*
  190. * Per cpu counters are removed via an smp call and
  191. * the removal is always sucessful.
  192. */
  193. smp_call_function_single(counter->cpu,
  194. __perf_counter_remove_from_context,
  195. counter, 1);
  196. return;
  197. }
  198. retry:
  199. task_oncpu_function_call(task, __perf_counter_remove_from_context,
  200. counter);
  201. spin_lock_irq(&ctx->lock);
  202. /*
  203. * If the context is active we need to retry the smp call.
  204. */
  205. if (ctx->nr_active && !list_empty(&counter->list_entry)) {
  206. spin_unlock_irq(&ctx->lock);
  207. goto retry;
  208. }
  209. /*
  210. * The lock prevents that this context is scheduled in so we
  211. * can remove the counter safely, if the call above did not
  212. * succeed.
  213. */
  214. if (!list_empty(&counter->list_entry)) {
  215. ctx->nr_counters--;
  216. list_del_counter(counter, ctx);
  217. counter->task = NULL;
  218. }
  219. spin_unlock_irq(&ctx->lock);
  220. }
  221. static inline u64 perf_clock(void)
  222. {
  223. return cpu_clock(smp_processor_id());
  224. }
  225. /*
  226. * Update the record of the current time in a context.
  227. */
  228. static void update_context_time(struct perf_counter_context *ctx)
  229. {
  230. u64 now = perf_clock();
  231. ctx->time += now - ctx->timestamp;
  232. ctx->timestamp = now;
  233. }
  234. /*
  235. * Update the total_time_enabled and total_time_running fields for a counter.
  236. */
  237. static void update_counter_times(struct perf_counter *counter)
  238. {
  239. struct perf_counter_context *ctx = counter->ctx;
  240. u64 run_end;
  241. if (counter->state < PERF_COUNTER_STATE_INACTIVE)
  242. return;
  243. counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
  244. if (counter->state == PERF_COUNTER_STATE_INACTIVE)
  245. run_end = counter->tstamp_stopped;
  246. else
  247. run_end = ctx->time;
  248. counter->total_time_running = run_end - counter->tstamp_running;
  249. }
  250. /*
  251. * Update total_time_enabled and total_time_running for all counters in a group.
  252. */
  253. static void update_group_times(struct perf_counter *leader)
  254. {
  255. struct perf_counter *counter;
  256. update_counter_times(leader);
  257. list_for_each_entry(counter, &leader->sibling_list, list_entry)
  258. update_counter_times(counter);
  259. }
  260. /*
  261. * Cross CPU call to disable a performance counter
  262. */
  263. static void __perf_counter_disable(void *info)
  264. {
  265. struct perf_counter *counter = info;
  266. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  267. struct perf_counter_context *ctx = counter->ctx;
  268. unsigned long flags;
  269. /*
  270. * If this is a per-task counter, need to check whether this
  271. * counter's task is the current task on this cpu.
  272. */
  273. if (ctx->task && cpuctx->task_ctx != ctx)
  274. return;
  275. spin_lock_irqsave(&ctx->lock, flags);
  276. /*
  277. * If the counter is on, turn it off.
  278. * If it is in error state, leave it in error state.
  279. */
  280. if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
  281. update_context_time(ctx);
  282. update_counter_times(counter);
  283. if (counter == counter->group_leader)
  284. group_sched_out(counter, cpuctx, ctx);
  285. else
  286. counter_sched_out(counter, cpuctx, ctx);
  287. counter->state = PERF_COUNTER_STATE_OFF;
  288. }
  289. spin_unlock_irqrestore(&ctx->lock, flags);
  290. }
  291. /*
  292. * Disable a counter.
  293. */
  294. static void perf_counter_disable(struct perf_counter *counter)
  295. {
  296. struct perf_counter_context *ctx = counter->ctx;
  297. struct task_struct *task = ctx->task;
  298. if (!task) {
  299. /*
  300. * Disable the counter on the cpu that it's on
  301. */
  302. smp_call_function_single(counter->cpu, __perf_counter_disable,
  303. counter, 1);
  304. return;
  305. }
  306. retry:
  307. task_oncpu_function_call(task, __perf_counter_disable, counter);
  308. spin_lock_irq(&ctx->lock);
  309. /*
  310. * If the counter is still active, we need to retry the cross-call.
  311. */
  312. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  313. spin_unlock_irq(&ctx->lock);
  314. goto retry;
  315. }
  316. /*
  317. * Since we have the lock this context can't be scheduled
  318. * in, so we can change the state safely.
  319. */
  320. if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
  321. update_counter_times(counter);
  322. counter->state = PERF_COUNTER_STATE_OFF;
  323. }
  324. spin_unlock_irq(&ctx->lock);
  325. }
  326. static int
  327. counter_sched_in(struct perf_counter *counter,
  328. struct perf_cpu_context *cpuctx,
  329. struct perf_counter_context *ctx,
  330. int cpu)
  331. {
  332. if (counter->state <= PERF_COUNTER_STATE_OFF)
  333. return 0;
  334. counter->state = PERF_COUNTER_STATE_ACTIVE;
  335. counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
  336. /*
  337. * The new state must be visible before we turn it on in the hardware:
  338. */
  339. smp_wmb();
  340. if (counter->pmu->enable(counter)) {
  341. counter->state = PERF_COUNTER_STATE_INACTIVE;
  342. counter->oncpu = -1;
  343. return -EAGAIN;
  344. }
  345. counter->tstamp_running += ctx->time - counter->tstamp_stopped;
  346. if (!is_software_counter(counter))
  347. cpuctx->active_oncpu++;
  348. ctx->nr_active++;
  349. if (counter->hw_event.exclusive)
  350. cpuctx->exclusive = 1;
  351. return 0;
  352. }
  353. static int
  354. group_sched_in(struct perf_counter *group_counter,
  355. struct perf_cpu_context *cpuctx,
  356. struct perf_counter_context *ctx,
  357. int cpu)
  358. {
  359. struct perf_counter *counter, *partial_group;
  360. int ret;
  361. if (group_counter->state == PERF_COUNTER_STATE_OFF)
  362. return 0;
  363. ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
  364. if (ret)
  365. return ret < 0 ? ret : 0;
  366. group_counter->prev_state = group_counter->state;
  367. if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
  368. return -EAGAIN;
  369. /*
  370. * Schedule in siblings as one group (if any):
  371. */
  372. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  373. counter->prev_state = counter->state;
  374. if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
  375. partial_group = counter;
  376. goto group_error;
  377. }
  378. }
  379. return 0;
  380. group_error:
  381. /*
  382. * Groups can be scheduled in as one unit only, so undo any
  383. * partial group before returning:
  384. */
  385. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  386. if (counter == partial_group)
  387. break;
  388. counter_sched_out(counter, cpuctx, ctx);
  389. }
  390. counter_sched_out(group_counter, cpuctx, ctx);
  391. return -EAGAIN;
  392. }
  393. /*
  394. * Return 1 for a group consisting entirely of software counters,
  395. * 0 if the group contains any hardware counters.
  396. */
  397. static int is_software_only_group(struct perf_counter *leader)
  398. {
  399. struct perf_counter *counter;
  400. if (!is_software_counter(leader))
  401. return 0;
  402. list_for_each_entry(counter, &leader->sibling_list, list_entry)
  403. if (!is_software_counter(counter))
  404. return 0;
  405. return 1;
  406. }
  407. /*
  408. * Work out whether we can put this counter group on the CPU now.
  409. */
  410. static int group_can_go_on(struct perf_counter *counter,
  411. struct perf_cpu_context *cpuctx,
  412. int can_add_hw)
  413. {
  414. /*
  415. * Groups consisting entirely of software counters can always go on.
  416. */
  417. if (is_software_only_group(counter))
  418. return 1;
  419. /*
  420. * If an exclusive group is already on, no other hardware
  421. * counters can go on.
  422. */
  423. if (cpuctx->exclusive)
  424. return 0;
  425. /*
  426. * If this group is exclusive and there are already
  427. * counters on the CPU, it can't go on.
  428. */
  429. if (counter->hw_event.exclusive && cpuctx->active_oncpu)
  430. return 0;
  431. /*
  432. * Otherwise, try to add it if all previous groups were able
  433. * to go on.
  434. */
  435. return can_add_hw;
  436. }
  437. static void add_counter_to_ctx(struct perf_counter *counter,
  438. struct perf_counter_context *ctx)
  439. {
  440. list_add_counter(counter, ctx);
  441. ctx->nr_counters++;
  442. counter->prev_state = PERF_COUNTER_STATE_OFF;
  443. counter->tstamp_enabled = ctx->time;
  444. counter->tstamp_running = ctx->time;
  445. counter->tstamp_stopped = ctx->time;
  446. }
  447. /*
  448. * Cross CPU call to install and enable a performance counter
  449. */
  450. static void __perf_install_in_context(void *info)
  451. {
  452. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  453. struct perf_counter *counter = info;
  454. struct perf_counter_context *ctx = counter->ctx;
  455. struct perf_counter *leader = counter->group_leader;
  456. int cpu = smp_processor_id();
  457. unsigned long flags;
  458. u64 perf_flags;
  459. int err;
  460. /*
  461. * If this is a task context, we need to check whether it is
  462. * the current task context of this cpu. If not it has been
  463. * scheduled out before the smp call arrived.
  464. */
  465. if (ctx->task && cpuctx->task_ctx != ctx)
  466. return;
  467. spin_lock_irqsave(&ctx->lock, flags);
  468. update_context_time(ctx);
  469. /*
  470. * Protect the list operation against NMI by disabling the
  471. * counters on a global level. NOP for non NMI based counters.
  472. */
  473. perf_flags = hw_perf_save_disable();
  474. add_counter_to_ctx(counter, ctx);
  475. /*
  476. * Don't put the counter on if it is disabled or if
  477. * it is in a group and the group isn't on.
  478. */
  479. if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
  480. (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
  481. goto unlock;
  482. /*
  483. * An exclusive counter can't go on if there are already active
  484. * hardware counters, and no hardware counter can go on if there
  485. * is already an exclusive counter on.
  486. */
  487. if (!group_can_go_on(counter, cpuctx, 1))
  488. err = -EEXIST;
  489. else
  490. err = counter_sched_in(counter, cpuctx, ctx, cpu);
  491. if (err) {
  492. /*
  493. * This counter couldn't go on. If it is in a group
  494. * then we have to pull the whole group off.
  495. * If the counter group is pinned then put it in error state.
  496. */
  497. if (leader != counter)
  498. group_sched_out(leader, cpuctx, ctx);
  499. if (leader->hw_event.pinned) {
  500. update_group_times(leader);
  501. leader->state = PERF_COUNTER_STATE_ERROR;
  502. }
  503. }
  504. if (!err && !ctx->task && cpuctx->max_pertask)
  505. cpuctx->max_pertask--;
  506. unlock:
  507. hw_perf_restore(perf_flags);
  508. spin_unlock_irqrestore(&ctx->lock, flags);
  509. }
  510. /*
  511. * Attach a performance counter to a context
  512. *
  513. * First we add the counter to the list with the hardware enable bit
  514. * in counter->hw_config cleared.
  515. *
  516. * If the counter is attached to a task which is on a CPU we use a smp
  517. * call to enable it in the task context. The task might have been
  518. * scheduled away, but we check this in the smp call again.
  519. *
  520. * Must be called with ctx->mutex held.
  521. */
  522. static void
  523. perf_install_in_context(struct perf_counter_context *ctx,
  524. struct perf_counter *counter,
  525. int cpu)
  526. {
  527. struct task_struct *task = ctx->task;
  528. if (!task) {
  529. /*
  530. * Per cpu counters are installed via an smp call and
  531. * the install is always sucessful.
  532. */
  533. smp_call_function_single(cpu, __perf_install_in_context,
  534. counter, 1);
  535. return;
  536. }
  537. counter->task = task;
  538. retry:
  539. task_oncpu_function_call(task, __perf_install_in_context,
  540. counter);
  541. spin_lock_irq(&ctx->lock);
  542. /*
  543. * we need to retry the smp call.
  544. */
  545. if (ctx->is_active && list_empty(&counter->list_entry)) {
  546. spin_unlock_irq(&ctx->lock);
  547. goto retry;
  548. }
  549. /*
  550. * The lock prevents that this context is scheduled in so we
  551. * can add the counter safely, if it the call above did not
  552. * succeed.
  553. */
  554. if (list_empty(&counter->list_entry))
  555. add_counter_to_ctx(counter, ctx);
  556. spin_unlock_irq(&ctx->lock);
  557. }
  558. /*
  559. * Cross CPU call to enable a performance counter
  560. */
  561. static void __perf_counter_enable(void *info)
  562. {
  563. struct perf_counter *counter = info;
  564. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  565. struct perf_counter_context *ctx = counter->ctx;
  566. struct perf_counter *leader = counter->group_leader;
  567. unsigned long pmuflags;
  568. unsigned long flags;
  569. int err;
  570. /*
  571. * If this is a per-task counter, need to check whether this
  572. * counter's task is the current task on this cpu.
  573. */
  574. if (ctx->task && cpuctx->task_ctx != ctx)
  575. return;
  576. spin_lock_irqsave(&ctx->lock, flags);
  577. update_context_time(ctx);
  578. counter->prev_state = counter->state;
  579. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  580. goto unlock;
  581. counter->state = PERF_COUNTER_STATE_INACTIVE;
  582. counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
  583. /*
  584. * If the counter is in a group and isn't the group leader,
  585. * then don't put it on unless the group is on.
  586. */
  587. if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
  588. goto unlock;
  589. if (!group_can_go_on(counter, cpuctx, 1)) {
  590. err = -EEXIST;
  591. } else {
  592. pmuflags = hw_perf_save_disable();
  593. if (counter == leader)
  594. err = group_sched_in(counter, cpuctx, ctx,
  595. smp_processor_id());
  596. else
  597. err = counter_sched_in(counter, cpuctx, ctx,
  598. smp_processor_id());
  599. hw_perf_restore(pmuflags);
  600. }
  601. if (err) {
  602. /*
  603. * If this counter can't go on and it's part of a
  604. * group, then the whole group has to come off.
  605. */
  606. if (leader != counter)
  607. group_sched_out(leader, cpuctx, ctx);
  608. if (leader->hw_event.pinned) {
  609. update_group_times(leader);
  610. leader->state = PERF_COUNTER_STATE_ERROR;
  611. }
  612. }
  613. unlock:
  614. spin_unlock_irqrestore(&ctx->lock, flags);
  615. }
  616. /*
  617. * Enable a counter.
  618. */
  619. static void perf_counter_enable(struct perf_counter *counter)
  620. {
  621. struct perf_counter_context *ctx = counter->ctx;
  622. struct task_struct *task = ctx->task;
  623. if (!task) {
  624. /*
  625. * Enable the counter on the cpu that it's on
  626. */
  627. smp_call_function_single(counter->cpu, __perf_counter_enable,
  628. counter, 1);
  629. return;
  630. }
  631. spin_lock_irq(&ctx->lock);
  632. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  633. goto out;
  634. /*
  635. * If the counter is in error state, clear that first.
  636. * That way, if we see the counter in error state below, we
  637. * know that it has gone back into error state, as distinct
  638. * from the task having been scheduled away before the
  639. * cross-call arrived.
  640. */
  641. if (counter->state == PERF_COUNTER_STATE_ERROR)
  642. counter->state = PERF_COUNTER_STATE_OFF;
  643. retry:
  644. spin_unlock_irq(&ctx->lock);
  645. task_oncpu_function_call(task, __perf_counter_enable, counter);
  646. spin_lock_irq(&ctx->lock);
  647. /*
  648. * If the context is active and the counter is still off,
  649. * we need to retry the cross-call.
  650. */
  651. if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
  652. goto retry;
  653. /*
  654. * Since we have the lock this context can't be scheduled
  655. * in, so we can change the state safely.
  656. */
  657. if (counter->state == PERF_COUNTER_STATE_OFF) {
  658. counter->state = PERF_COUNTER_STATE_INACTIVE;
  659. counter->tstamp_enabled =
  660. ctx->time - counter->total_time_enabled;
  661. }
  662. out:
  663. spin_unlock_irq(&ctx->lock);
  664. }
  665. static int perf_counter_refresh(struct perf_counter *counter, int refresh)
  666. {
  667. /*
  668. * not supported on inherited counters
  669. */
  670. if (counter->hw_event.inherit)
  671. return -EINVAL;
  672. atomic_add(refresh, &counter->event_limit);
  673. perf_counter_enable(counter);
  674. return 0;
  675. }
  676. void __perf_counter_sched_out(struct perf_counter_context *ctx,
  677. struct perf_cpu_context *cpuctx)
  678. {
  679. struct perf_counter *counter;
  680. u64 flags;
  681. spin_lock(&ctx->lock);
  682. ctx->is_active = 0;
  683. if (likely(!ctx->nr_counters))
  684. goto out;
  685. update_context_time(ctx);
  686. flags = hw_perf_save_disable();
  687. if (ctx->nr_active) {
  688. list_for_each_entry(counter, &ctx->counter_list, list_entry)
  689. group_sched_out(counter, cpuctx, ctx);
  690. }
  691. hw_perf_restore(flags);
  692. out:
  693. spin_unlock(&ctx->lock);
  694. }
  695. /*
  696. * Called from scheduler to remove the counters of the current task,
  697. * with interrupts disabled.
  698. *
  699. * We stop each counter and update the counter value in counter->count.
  700. *
  701. * This does not protect us against NMI, but disable()
  702. * sets the disabled bit in the control field of counter _before_
  703. * accessing the counter control register. If a NMI hits, then it will
  704. * not restart the counter.
  705. */
  706. void perf_counter_task_sched_out(struct task_struct *task, int cpu)
  707. {
  708. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  709. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  710. struct pt_regs *regs;
  711. if (likely(!cpuctx->task_ctx))
  712. return;
  713. update_context_time(ctx);
  714. regs = task_pt_regs(task);
  715. perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
  716. __perf_counter_sched_out(ctx, cpuctx);
  717. cpuctx->task_ctx = NULL;
  718. }
  719. static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
  720. {
  721. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  722. __perf_counter_sched_out(ctx, cpuctx);
  723. cpuctx->task_ctx = NULL;
  724. }
  725. static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
  726. {
  727. __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
  728. }
  729. static void
  730. __perf_counter_sched_in(struct perf_counter_context *ctx,
  731. struct perf_cpu_context *cpuctx, int cpu)
  732. {
  733. struct perf_counter *counter;
  734. u64 flags;
  735. int can_add_hw = 1;
  736. spin_lock(&ctx->lock);
  737. ctx->is_active = 1;
  738. if (likely(!ctx->nr_counters))
  739. goto out;
  740. ctx->timestamp = perf_clock();
  741. flags = hw_perf_save_disable();
  742. /*
  743. * First go through the list and put on any pinned groups
  744. * in order to give them the best chance of going on.
  745. */
  746. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  747. if (counter->state <= PERF_COUNTER_STATE_OFF ||
  748. !counter->hw_event.pinned)
  749. continue;
  750. if (counter->cpu != -1 && counter->cpu != cpu)
  751. continue;
  752. if (group_can_go_on(counter, cpuctx, 1))
  753. group_sched_in(counter, cpuctx, ctx, cpu);
  754. /*
  755. * If this pinned group hasn't been scheduled,
  756. * put it in error state.
  757. */
  758. if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
  759. update_group_times(counter);
  760. counter->state = PERF_COUNTER_STATE_ERROR;
  761. }
  762. }
  763. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  764. /*
  765. * Ignore counters in OFF or ERROR state, and
  766. * ignore pinned counters since we did them already.
  767. */
  768. if (counter->state <= PERF_COUNTER_STATE_OFF ||
  769. counter->hw_event.pinned)
  770. continue;
  771. /*
  772. * Listen to the 'cpu' scheduling filter constraint
  773. * of counters:
  774. */
  775. if (counter->cpu != -1 && counter->cpu != cpu)
  776. continue;
  777. if (group_can_go_on(counter, cpuctx, can_add_hw)) {
  778. if (group_sched_in(counter, cpuctx, ctx, cpu))
  779. can_add_hw = 0;
  780. }
  781. }
  782. hw_perf_restore(flags);
  783. out:
  784. spin_unlock(&ctx->lock);
  785. }
  786. /*
  787. * Called from scheduler to add the counters of the current task
  788. * with interrupts disabled.
  789. *
  790. * We restore the counter value and then enable it.
  791. *
  792. * This does not protect us against NMI, but enable()
  793. * sets the enabled bit in the control field of counter _before_
  794. * accessing the counter control register. If a NMI hits, then it will
  795. * keep the counter running.
  796. */
  797. void perf_counter_task_sched_in(struct task_struct *task, int cpu)
  798. {
  799. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  800. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  801. __perf_counter_sched_in(ctx, cpuctx, cpu);
  802. cpuctx->task_ctx = ctx;
  803. }
  804. static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
  805. {
  806. struct perf_counter_context *ctx = &cpuctx->ctx;
  807. __perf_counter_sched_in(ctx, cpuctx, cpu);
  808. }
  809. int perf_counter_task_disable(void)
  810. {
  811. struct task_struct *curr = current;
  812. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  813. struct perf_counter *counter;
  814. unsigned long flags;
  815. u64 perf_flags;
  816. if (likely(!ctx->nr_counters))
  817. return 0;
  818. local_irq_save(flags);
  819. __perf_counter_task_sched_out(ctx);
  820. spin_lock(&ctx->lock);
  821. /*
  822. * Disable all the counters:
  823. */
  824. perf_flags = hw_perf_save_disable();
  825. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  826. if (counter->state != PERF_COUNTER_STATE_ERROR) {
  827. update_group_times(counter);
  828. counter->state = PERF_COUNTER_STATE_OFF;
  829. }
  830. }
  831. hw_perf_restore(perf_flags);
  832. spin_unlock_irqrestore(&ctx->lock, flags);
  833. return 0;
  834. }
  835. int perf_counter_task_enable(void)
  836. {
  837. struct task_struct *curr = current;
  838. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  839. struct perf_counter *counter;
  840. unsigned long flags;
  841. u64 perf_flags;
  842. int cpu;
  843. if (likely(!ctx->nr_counters))
  844. return 0;
  845. local_irq_save(flags);
  846. cpu = smp_processor_id();
  847. __perf_counter_task_sched_out(ctx);
  848. spin_lock(&ctx->lock);
  849. /*
  850. * Disable all the counters:
  851. */
  852. perf_flags = hw_perf_save_disable();
  853. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  854. if (counter->state > PERF_COUNTER_STATE_OFF)
  855. continue;
  856. counter->state = PERF_COUNTER_STATE_INACTIVE;
  857. counter->tstamp_enabled =
  858. ctx->time - counter->total_time_enabled;
  859. counter->hw_event.disabled = 0;
  860. }
  861. hw_perf_restore(perf_flags);
  862. spin_unlock(&ctx->lock);
  863. perf_counter_task_sched_in(curr, cpu);
  864. local_irq_restore(flags);
  865. return 0;
  866. }
  867. /*
  868. * Round-robin a context's counters:
  869. */
  870. static void rotate_ctx(struct perf_counter_context *ctx)
  871. {
  872. struct perf_counter *counter;
  873. u64 perf_flags;
  874. if (!ctx->nr_counters)
  875. return;
  876. spin_lock(&ctx->lock);
  877. /*
  878. * Rotate the first entry last (works just fine for group counters too):
  879. */
  880. perf_flags = hw_perf_save_disable();
  881. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  882. list_move_tail(&counter->list_entry, &ctx->counter_list);
  883. break;
  884. }
  885. hw_perf_restore(perf_flags);
  886. spin_unlock(&ctx->lock);
  887. }
  888. void perf_counter_task_tick(struct task_struct *curr, int cpu)
  889. {
  890. struct perf_cpu_context *cpuctx;
  891. struct perf_counter_context *ctx;
  892. if (!atomic_read(&nr_counters))
  893. return;
  894. cpuctx = &per_cpu(perf_cpu_context, cpu);
  895. ctx = &curr->perf_counter_ctx;
  896. perf_counter_cpu_sched_out(cpuctx);
  897. __perf_counter_task_sched_out(ctx);
  898. rotate_ctx(&cpuctx->ctx);
  899. rotate_ctx(ctx);
  900. perf_counter_cpu_sched_in(cpuctx, cpu);
  901. perf_counter_task_sched_in(curr, cpu);
  902. }
  903. /*
  904. * Cross CPU call to read the hardware counter
  905. */
  906. static void __read(void *info)
  907. {
  908. struct perf_counter *counter = info;
  909. struct perf_counter_context *ctx = counter->ctx;
  910. unsigned long flags;
  911. local_irq_save(flags);
  912. if (ctx->is_active)
  913. update_context_time(ctx);
  914. counter->pmu->read(counter);
  915. update_counter_times(counter);
  916. local_irq_restore(flags);
  917. }
  918. static u64 perf_counter_read(struct perf_counter *counter)
  919. {
  920. /*
  921. * If counter is enabled and currently active on a CPU, update the
  922. * value in the counter structure:
  923. */
  924. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  925. smp_call_function_single(counter->oncpu,
  926. __read, counter, 1);
  927. } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
  928. update_counter_times(counter);
  929. }
  930. return atomic64_read(&counter->count);
  931. }
  932. static void put_context(struct perf_counter_context *ctx)
  933. {
  934. if (ctx->task)
  935. put_task_struct(ctx->task);
  936. }
  937. static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
  938. {
  939. struct perf_cpu_context *cpuctx;
  940. struct perf_counter_context *ctx;
  941. struct task_struct *task;
  942. /*
  943. * If cpu is not a wildcard then this is a percpu counter:
  944. */
  945. if (cpu != -1) {
  946. /* Must be root to operate on a CPU counter: */
  947. if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
  948. return ERR_PTR(-EACCES);
  949. if (cpu < 0 || cpu > num_possible_cpus())
  950. return ERR_PTR(-EINVAL);
  951. /*
  952. * We could be clever and allow to attach a counter to an
  953. * offline CPU and activate it when the CPU comes up, but
  954. * that's for later.
  955. */
  956. if (!cpu_isset(cpu, cpu_online_map))
  957. return ERR_PTR(-ENODEV);
  958. cpuctx = &per_cpu(perf_cpu_context, cpu);
  959. ctx = &cpuctx->ctx;
  960. return ctx;
  961. }
  962. rcu_read_lock();
  963. if (!pid)
  964. task = current;
  965. else
  966. task = find_task_by_vpid(pid);
  967. if (task)
  968. get_task_struct(task);
  969. rcu_read_unlock();
  970. if (!task)
  971. return ERR_PTR(-ESRCH);
  972. ctx = &task->perf_counter_ctx;
  973. ctx->task = task;
  974. /* Reuse ptrace permission checks for now. */
  975. if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
  976. put_context(ctx);
  977. return ERR_PTR(-EACCES);
  978. }
  979. return ctx;
  980. }
  981. static void free_counter_rcu(struct rcu_head *head)
  982. {
  983. struct perf_counter *counter;
  984. counter = container_of(head, struct perf_counter, rcu_head);
  985. kfree(counter);
  986. }
  987. static void perf_pending_sync(struct perf_counter *counter);
  988. static void free_counter(struct perf_counter *counter)
  989. {
  990. perf_pending_sync(counter);
  991. atomic_dec(&nr_counters);
  992. if (counter->hw_event.mmap)
  993. atomic_dec(&nr_mmap_tracking);
  994. if (counter->hw_event.munmap)
  995. atomic_dec(&nr_munmap_tracking);
  996. if (counter->hw_event.comm)
  997. atomic_dec(&nr_comm_tracking);
  998. if (counter->destroy)
  999. counter->destroy(counter);
  1000. call_rcu(&counter->rcu_head, free_counter_rcu);
  1001. }
  1002. /*
  1003. * Called when the last reference to the file is gone.
  1004. */
  1005. static int perf_release(struct inode *inode, struct file *file)
  1006. {
  1007. struct perf_counter *counter = file->private_data;
  1008. struct perf_counter_context *ctx = counter->ctx;
  1009. file->private_data = NULL;
  1010. mutex_lock(&ctx->mutex);
  1011. mutex_lock(&counter->mutex);
  1012. perf_counter_remove_from_context(counter);
  1013. mutex_unlock(&counter->mutex);
  1014. mutex_unlock(&ctx->mutex);
  1015. free_counter(counter);
  1016. put_context(ctx);
  1017. return 0;
  1018. }
  1019. /*
  1020. * Read the performance counter - simple non blocking version for now
  1021. */
  1022. static ssize_t
  1023. perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
  1024. {
  1025. u64 values[3];
  1026. int n;
  1027. /*
  1028. * Return end-of-file for a read on a counter that is in
  1029. * error state (i.e. because it was pinned but it couldn't be
  1030. * scheduled on to the CPU at some point).
  1031. */
  1032. if (counter->state == PERF_COUNTER_STATE_ERROR)
  1033. return 0;
  1034. mutex_lock(&counter->mutex);
  1035. values[0] = perf_counter_read(counter);
  1036. n = 1;
  1037. if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1038. values[n++] = counter->total_time_enabled +
  1039. atomic64_read(&counter->child_total_time_enabled);
  1040. if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1041. values[n++] = counter->total_time_running +
  1042. atomic64_read(&counter->child_total_time_running);
  1043. mutex_unlock(&counter->mutex);
  1044. if (count < n * sizeof(u64))
  1045. return -EINVAL;
  1046. count = n * sizeof(u64);
  1047. if (copy_to_user(buf, values, count))
  1048. return -EFAULT;
  1049. return count;
  1050. }
  1051. static ssize_t
  1052. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  1053. {
  1054. struct perf_counter *counter = file->private_data;
  1055. return perf_read_hw(counter, buf, count);
  1056. }
  1057. static unsigned int perf_poll(struct file *file, poll_table *wait)
  1058. {
  1059. struct perf_counter *counter = file->private_data;
  1060. struct perf_mmap_data *data;
  1061. unsigned int events = POLL_HUP;
  1062. rcu_read_lock();
  1063. data = rcu_dereference(counter->data);
  1064. if (data)
  1065. events = atomic_xchg(&data->poll, 0);
  1066. rcu_read_unlock();
  1067. poll_wait(file, &counter->waitq, wait);
  1068. return events;
  1069. }
  1070. static void perf_counter_reset(struct perf_counter *counter)
  1071. {
  1072. (void)perf_counter_read(counter);
  1073. atomic64_set(&counter->count, 0);
  1074. perf_counter_update_userpage(counter);
  1075. }
  1076. static void perf_counter_for_each_sibling(struct perf_counter *counter,
  1077. void (*func)(struct perf_counter *))
  1078. {
  1079. struct perf_counter_context *ctx = counter->ctx;
  1080. struct perf_counter *sibling;
  1081. spin_lock_irq(&ctx->lock);
  1082. counter = counter->group_leader;
  1083. func(counter);
  1084. list_for_each_entry(sibling, &counter->sibling_list, list_entry)
  1085. func(sibling);
  1086. spin_unlock_irq(&ctx->lock);
  1087. }
  1088. static void perf_counter_for_each_child(struct perf_counter *counter,
  1089. void (*func)(struct perf_counter *))
  1090. {
  1091. struct perf_counter *child;
  1092. mutex_lock(&counter->mutex);
  1093. func(counter);
  1094. list_for_each_entry(child, &counter->child_list, child_list)
  1095. func(child);
  1096. mutex_unlock(&counter->mutex);
  1097. }
  1098. static void perf_counter_for_each(struct perf_counter *counter,
  1099. void (*func)(struct perf_counter *))
  1100. {
  1101. struct perf_counter *child;
  1102. mutex_lock(&counter->mutex);
  1103. perf_counter_for_each_sibling(counter, func);
  1104. list_for_each_entry(child, &counter->child_list, child_list)
  1105. perf_counter_for_each_sibling(child, func);
  1106. mutex_unlock(&counter->mutex);
  1107. }
  1108. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1109. {
  1110. struct perf_counter *counter = file->private_data;
  1111. void (*func)(struct perf_counter *);
  1112. u32 flags = arg;
  1113. switch (cmd) {
  1114. case PERF_COUNTER_IOC_ENABLE:
  1115. func = perf_counter_enable;
  1116. break;
  1117. case PERF_COUNTER_IOC_DISABLE:
  1118. func = perf_counter_disable;
  1119. break;
  1120. case PERF_COUNTER_IOC_RESET:
  1121. func = perf_counter_reset;
  1122. break;
  1123. case PERF_COUNTER_IOC_REFRESH:
  1124. return perf_counter_refresh(counter, arg);
  1125. default:
  1126. return -ENOTTY;
  1127. }
  1128. if (flags & PERF_IOC_FLAG_GROUP)
  1129. perf_counter_for_each(counter, func);
  1130. else
  1131. perf_counter_for_each_child(counter, func);
  1132. return 0;
  1133. }
  1134. /*
  1135. * Callers need to ensure there can be no nesting of this function, otherwise
  1136. * the seqlock logic goes bad. We can not serialize this because the arch
  1137. * code calls this from NMI context.
  1138. */
  1139. void perf_counter_update_userpage(struct perf_counter *counter)
  1140. {
  1141. struct perf_mmap_data *data;
  1142. struct perf_counter_mmap_page *userpg;
  1143. rcu_read_lock();
  1144. data = rcu_dereference(counter->data);
  1145. if (!data)
  1146. goto unlock;
  1147. userpg = data->user_page;
  1148. /*
  1149. * Disable preemption so as to not let the corresponding user-space
  1150. * spin too long if we get preempted.
  1151. */
  1152. preempt_disable();
  1153. ++userpg->lock;
  1154. barrier();
  1155. userpg->index = counter->hw.idx;
  1156. userpg->offset = atomic64_read(&counter->count);
  1157. if (counter->state == PERF_COUNTER_STATE_ACTIVE)
  1158. userpg->offset -= atomic64_read(&counter->hw.prev_count);
  1159. barrier();
  1160. ++userpg->lock;
  1161. preempt_enable();
  1162. unlock:
  1163. rcu_read_unlock();
  1164. }
  1165. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1166. {
  1167. struct perf_counter *counter = vma->vm_file->private_data;
  1168. struct perf_mmap_data *data;
  1169. int ret = VM_FAULT_SIGBUS;
  1170. rcu_read_lock();
  1171. data = rcu_dereference(counter->data);
  1172. if (!data)
  1173. goto unlock;
  1174. if (vmf->pgoff == 0) {
  1175. vmf->page = virt_to_page(data->user_page);
  1176. } else {
  1177. int nr = vmf->pgoff - 1;
  1178. if ((unsigned)nr > data->nr_pages)
  1179. goto unlock;
  1180. vmf->page = virt_to_page(data->data_pages[nr]);
  1181. }
  1182. get_page(vmf->page);
  1183. ret = 0;
  1184. unlock:
  1185. rcu_read_unlock();
  1186. return ret;
  1187. }
  1188. static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
  1189. {
  1190. struct perf_mmap_data *data;
  1191. unsigned long size;
  1192. int i;
  1193. WARN_ON(atomic_read(&counter->mmap_count));
  1194. size = sizeof(struct perf_mmap_data);
  1195. size += nr_pages * sizeof(void *);
  1196. data = kzalloc(size, GFP_KERNEL);
  1197. if (!data)
  1198. goto fail;
  1199. data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
  1200. if (!data->user_page)
  1201. goto fail_user_page;
  1202. for (i = 0; i < nr_pages; i++) {
  1203. data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
  1204. if (!data->data_pages[i])
  1205. goto fail_data_pages;
  1206. }
  1207. data->nr_pages = nr_pages;
  1208. atomic_set(&data->lock, -1);
  1209. rcu_assign_pointer(counter->data, data);
  1210. return 0;
  1211. fail_data_pages:
  1212. for (i--; i >= 0; i--)
  1213. free_page((unsigned long)data->data_pages[i]);
  1214. free_page((unsigned long)data->user_page);
  1215. fail_user_page:
  1216. kfree(data);
  1217. fail:
  1218. return -ENOMEM;
  1219. }
  1220. static void __perf_mmap_data_free(struct rcu_head *rcu_head)
  1221. {
  1222. struct perf_mmap_data *data = container_of(rcu_head,
  1223. struct perf_mmap_data, rcu_head);
  1224. int i;
  1225. free_page((unsigned long)data->user_page);
  1226. for (i = 0; i < data->nr_pages; i++)
  1227. free_page((unsigned long)data->data_pages[i]);
  1228. kfree(data);
  1229. }
  1230. static void perf_mmap_data_free(struct perf_counter *counter)
  1231. {
  1232. struct perf_mmap_data *data = counter->data;
  1233. WARN_ON(atomic_read(&counter->mmap_count));
  1234. rcu_assign_pointer(counter->data, NULL);
  1235. call_rcu(&data->rcu_head, __perf_mmap_data_free);
  1236. }
  1237. static void perf_mmap_open(struct vm_area_struct *vma)
  1238. {
  1239. struct perf_counter *counter = vma->vm_file->private_data;
  1240. atomic_inc(&counter->mmap_count);
  1241. }
  1242. static void perf_mmap_close(struct vm_area_struct *vma)
  1243. {
  1244. struct perf_counter *counter = vma->vm_file->private_data;
  1245. if (atomic_dec_and_mutex_lock(&counter->mmap_count,
  1246. &counter->mmap_mutex)) {
  1247. vma->vm_mm->locked_vm -= counter->data->nr_locked;
  1248. perf_mmap_data_free(counter);
  1249. mutex_unlock(&counter->mmap_mutex);
  1250. }
  1251. }
  1252. static struct vm_operations_struct perf_mmap_vmops = {
  1253. .open = perf_mmap_open,
  1254. .close = perf_mmap_close,
  1255. .fault = perf_mmap_fault,
  1256. };
  1257. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  1258. {
  1259. struct perf_counter *counter = file->private_data;
  1260. unsigned long vma_size;
  1261. unsigned long nr_pages;
  1262. unsigned long locked, lock_limit;
  1263. int ret = 0;
  1264. long extra;
  1265. if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
  1266. return -EINVAL;
  1267. vma_size = vma->vm_end - vma->vm_start;
  1268. nr_pages = (vma_size / PAGE_SIZE) - 1;
  1269. /*
  1270. * If we have data pages ensure they're a power-of-two number, so we
  1271. * can do bitmasks instead of modulo.
  1272. */
  1273. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  1274. return -EINVAL;
  1275. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  1276. return -EINVAL;
  1277. if (vma->vm_pgoff != 0)
  1278. return -EINVAL;
  1279. mutex_lock(&counter->mmap_mutex);
  1280. if (atomic_inc_not_zero(&counter->mmap_count)) {
  1281. if (nr_pages != counter->data->nr_pages)
  1282. ret = -EINVAL;
  1283. goto unlock;
  1284. }
  1285. extra = nr_pages /* + 1 only account the data pages */;
  1286. extra -= sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
  1287. if (extra < 0)
  1288. extra = 0;
  1289. locked = vma->vm_mm->locked_vm + extra;
  1290. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  1291. lock_limit >>= PAGE_SHIFT;
  1292. if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
  1293. ret = -EPERM;
  1294. goto unlock;
  1295. }
  1296. WARN_ON(counter->data);
  1297. ret = perf_mmap_data_alloc(counter, nr_pages);
  1298. if (ret)
  1299. goto unlock;
  1300. atomic_set(&counter->mmap_count, 1);
  1301. vma->vm_mm->locked_vm += extra;
  1302. counter->data->nr_locked = extra;
  1303. unlock:
  1304. mutex_unlock(&counter->mmap_mutex);
  1305. vma->vm_flags &= ~VM_MAYWRITE;
  1306. vma->vm_flags |= VM_RESERVED;
  1307. vma->vm_ops = &perf_mmap_vmops;
  1308. return ret;
  1309. }
  1310. static int perf_fasync(int fd, struct file *filp, int on)
  1311. {
  1312. struct perf_counter *counter = filp->private_data;
  1313. struct inode *inode = filp->f_path.dentry->d_inode;
  1314. int retval;
  1315. mutex_lock(&inode->i_mutex);
  1316. retval = fasync_helper(fd, filp, on, &counter->fasync);
  1317. mutex_unlock(&inode->i_mutex);
  1318. if (retval < 0)
  1319. return retval;
  1320. return 0;
  1321. }
  1322. static const struct file_operations perf_fops = {
  1323. .release = perf_release,
  1324. .read = perf_read,
  1325. .poll = perf_poll,
  1326. .unlocked_ioctl = perf_ioctl,
  1327. .compat_ioctl = perf_ioctl,
  1328. .mmap = perf_mmap,
  1329. .fasync = perf_fasync,
  1330. };
  1331. /*
  1332. * Perf counter wakeup
  1333. *
  1334. * If there's data, ensure we set the poll() state and publish everything
  1335. * to user-space before waking everybody up.
  1336. */
  1337. void perf_counter_wakeup(struct perf_counter *counter)
  1338. {
  1339. wake_up_all(&counter->waitq);
  1340. if (counter->pending_kill) {
  1341. kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
  1342. counter->pending_kill = 0;
  1343. }
  1344. }
  1345. /*
  1346. * Pending wakeups
  1347. *
  1348. * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
  1349. *
  1350. * The NMI bit means we cannot possibly take locks. Therefore, maintain a
  1351. * single linked list and use cmpxchg() to add entries lockless.
  1352. */
  1353. static void perf_pending_counter(struct perf_pending_entry *entry)
  1354. {
  1355. struct perf_counter *counter = container_of(entry,
  1356. struct perf_counter, pending);
  1357. if (counter->pending_disable) {
  1358. counter->pending_disable = 0;
  1359. perf_counter_disable(counter);
  1360. }
  1361. if (counter->pending_wakeup) {
  1362. counter->pending_wakeup = 0;
  1363. perf_counter_wakeup(counter);
  1364. }
  1365. }
  1366. #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
  1367. static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
  1368. PENDING_TAIL,
  1369. };
  1370. static void perf_pending_queue(struct perf_pending_entry *entry,
  1371. void (*func)(struct perf_pending_entry *))
  1372. {
  1373. struct perf_pending_entry **head;
  1374. if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
  1375. return;
  1376. entry->func = func;
  1377. head = &get_cpu_var(perf_pending_head);
  1378. do {
  1379. entry->next = *head;
  1380. } while (cmpxchg(head, entry->next, entry) != entry->next);
  1381. set_perf_counter_pending();
  1382. put_cpu_var(perf_pending_head);
  1383. }
  1384. static int __perf_pending_run(void)
  1385. {
  1386. struct perf_pending_entry *list;
  1387. int nr = 0;
  1388. list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
  1389. while (list != PENDING_TAIL) {
  1390. void (*func)(struct perf_pending_entry *);
  1391. struct perf_pending_entry *entry = list;
  1392. list = list->next;
  1393. func = entry->func;
  1394. entry->next = NULL;
  1395. /*
  1396. * Ensure we observe the unqueue before we issue the wakeup,
  1397. * so that we won't be waiting forever.
  1398. * -- see perf_not_pending().
  1399. */
  1400. smp_wmb();
  1401. func(entry);
  1402. nr++;
  1403. }
  1404. return nr;
  1405. }
  1406. static inline int perf_not_pending(struct perf_counter *counter)
  1407. {
  1408. /*
  1409. * If we flush on whatever cpu we run, there is a chance we don't
  1410. * need to wait.
  1411. */
  1412. get_cpu();
  1413. __perf_pending_run();
  1414. put_cpu();
  1415. /*
  1416. * Ensure we see the proper queue state before going to sleep
  1417. * so that we do not miss the wakeup. -- see perf_pending_handle()
  1418. */
  1419. smp_rmb();
  1420. return counter->pending.next == NULL;
  1421. }
  1422. static void perf_pending_sync(struct perf_counter *counter)
  1423. {
  1424. wait_event(counter->waitq, perf_not_pending(counter));
  1425. }
  1426. void perf_counter_do_pending(void)
  1427. {
  1428. __perf_pending_run();
  1429. }
  1430. /*
  1431. * Callchain support -- arch specific
  1432. */
  1433. __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1434. {
  1435. return NULL;
  1436. }
  1437. /*
  1438. * Output
  1439. */
  1440. struct perf_output_handle {
  1441. struct perf_counter *counter;
  1442. struct perf_mmap_data *data;
  1443. unsigned int offset;
  1444. unsigned int head;
  1445. int nmi;
  1446. int overflow;
  1447. int locked;
  1448. unsigned long flags;
  1449. };
  1450. static void perf_output_wakeup(struct perf_output_handle *handle)
  1451. {
  1452. atomic_set(&handle->data->poll, POLL_IN);
  1453. if (handle->nmi) {
  1454. handle->counter->pending_wakeup = 1;
  1455. perf_pending_queue(&handle->counter->pending,
  1456. perf_pending_counter);
  1457. } else
  1458. perf_counter_wakeup(handle->counter);
  1459. }
  1460. /*
  1461. * Curious locking construct.
  1462. *
  1463. * We need to ensure a later event doesn't publish a head when a former
  1464. * event isn't done writing. However since we need to deal with NMIs we
  1465. * cannot fully serialize things.
  1466. *
  1467. * What we do is serialize between CPUs so we only have to deal with NMI
  1468. * nesting on a single CPU.
  1469. *
  1470. * We only publish the head (and generate a wakeup) when the outer-most
  1471. * event completes.
  1472. */
  1473. static void perf_output_lock(struct perf_output_handle *handle)
  1474. {
  1475. struct perf_mmap_data *data = handle->data;
  1476. int cpu;
  1477. handle->locked = 0;
  1478. local_irq_save(handle->flags);
  1479. cpu = smp_processor_id();
  1480. if (in_nmi() && atomic_read(&data->lock) == cpu)
  1481. return;
  1482. while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
  1483. cpu_relax();
  1484. handle->locked = 1;
  1485. }
  1486. static void perf_output_unlock(struct perf_output_handle *handle)
  1487. {
  1488. struct perf_mmap_data *data = handle->data;
  1489. int head, cpu;
  1490. data->done_head = data->head;
  1491. if (!handle->locked)
  1492. goto out;
  1493. again:
  1494. /*
  1495. * The xchg implies a full barrier that ensures all writes are done
  1496. * before we publish the new head, matched by a rmb() in userspace when
  1497. * reading this position.
  1498. */
  1499. while ((head = atomic_xchg(&data->done_head, 0)))
  1500. data->user_page->data_head = head;
  1501. /*
  1502. * NMI can happen here, which means we can miss a done_head update.
  1503. */
  1504. cpu = atomic_xchg(&data->lock, -1);
  1505. WARN_ON_ONCE(cpu != smp_processor_id());
  1506. /*
  1507. * Therefore we have to validate we did not indeed do so.
  1508. */
  1509. if (unlikely(atomic_read(&data->done_head))) {
  1510. /*
  1511. * Since we had it locked, we can lock it again.
  1512. */
  1513. while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
  1514. cpu_relax();
  1515. goto again;
  1516. }
  1517. if (atomic_xchg(&data->wakeup, 0))
  1518. perf_output_wakeup(handle);
  1519. out:
  1520. local_irq_restore(handle->flags);
  1521. }
  1522. static int perf_output_begin(struct perf_output_handle *handle,
  1523. struct perf_counter *counter, unsigned int size,
  1524. int nmi, int overflow)
  1525. {
  1526. struct perf_mmap_data *data;
  1527. unsigned int offset, head;
  1528. /*
  1529. * For inherited counters we send all the output towards the parent.
  1530. */
  1531. if (counter->parent)
  1532. counter = counter->parent;
  1533. rcu_read_lock();
  1534. data = rcu_dereference(counter->data);
  1535. if (!data)
  1536. goto out;
  1537. handle->data = data;
  1538. handle->counter = counter;
  1539. handle->nmi = nmi;
  1540. handle->overflow = overflow;
  1541. if (!data->nr_pages)
  1542. goto fail;
  1543. perf_output_lock(handle);
  1544. do {
  1545. offset = head = atomic_read(&data->head);
  1546. head += size;
  1547. } while (atomic_cmpxchg(&data->head, offset, head) != offset);
  1548. handle->offset = offset;
  1549. handle->head = head;
  1550. if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
  1551. atomic_set(&data->wakeup, 1);
  1552. return 0;
  1553. fail:
  1554. perf_output_wakeup(handle);
  1555. out:
  1556. rcu_read_unlock();
  1557. return -ENOSPC;
  1558. }
  1559. static void perf_output_copy(struct perf_output_handle *handle,
  1560. void *buf, unsigned int len)
  1561. {
  1562. unsigned int pages_mask;
  1563. unsigned int offset;
  1564. unsigned int size;
  1565. void **pages;
  1566. offset = handle->offset;
  1567. pages_mask = handle->data->nr_pages - 1;
  1568. pages = handle->data->data_pages;
  1569. do {
  1570. unsigned int page_offset;
  1571. int nr;
  1572. nr = (offset >> PAGE_SHIFT) & pages_mask;
  1573. page_offset = offset & (PAGE_SIZE - 1);
  1574. size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
  1575. memcpy(pages[nr] + page_offset, buf, size);
  1576. len -= size;
  1577. buf += size;
  1578. offset += size;
  1579. } while (len);
  1580. handle->offset = offset;
  1581. WARN_ON_ONCE(handle->offset > handle->head);
  1582. }
  1583. #define perf_output_put(handle, x) \
  1584. perf_output_copy((handle), &(x), sizeof(x))
  1585. static void perf_output_end(struct perf_output_handle *handle)
  1586. {
  1587. struct perf_counter *counter = handle->counter;
  1588. struct perf_mmap_data *data = handle->data;
  1589. int wakeup_events = counter->hw_event.wakeup_events;
  1590. if (handle->overflow && wakeup_events) {
  1591. int events = atomic_inc_return(&data->events);
  1592. if (events >= wakeup_events) {
  1593. atomic_sub(wakeup_events, &data->events);
  1594. atomic_set(&data->wakeup, 1);
  1595. }
  1596. }
  1597. perf_output_unlock(handle);
  1598. rcu_read_unlock();
  1599. }
  1600. static void perf_counter_output(struct perf_counter *counter,
  1601. int nmi, struct pt_regs *regs, u64 addr)
  1602. {
  1603. int ret;
  1604. u64 record_type = counter->hw_event.record_type;
  1605. struct perf_output_handle handle;
  1606. struct perf_event_header header;
  1607. u64 ip;
  1608. struct {
  1609. u32 pid, tid;
  1610. } tid_entry;
  1611. struct {
  1612. u64 event;
  1613. u64 counter;
  1614. } group_entry;
  1615. struct perf_callchain_entry *callchain = NULL;
  1616. int callchain_size = 0;
  1617. u64 time;
  1618. struct {
  1619. u32 cpu, reserved;
  1620. } cpu_entry;
  1621. header.type = 0;
  1622. header.size = sizeof(header);
  1623. header.misc = PERF_EVENT_MISC_OVERFLOW;
  1624. header.misc |= user_mode(regs) ?
  1625. PERF_EVENT_MISC_USER : PERF_EVENT_MISC_KERNEL;
  1626. if (record_type & PERF_RECORD_IP) {
  1627. ip = instruction_pointer(regs);
  1628. header.type |= PERF_RECORD_IP;
  1629. header.size += sizeof(ip);
  1630. }
  1631. if (record_type & PERF_RECORD_TID) {
  1632. /* namespace issues */
  1633. tid_entry.pid = current->group_leader->pid;
  1634. tid_entry.tid = current->pid;
  1635. header.type |= PERF_RECORD_TID;
  1636. header.size += sizeof(tid_entry);
  1637. }
  1638. if (record_type & PERF_RECORD_TIME) {
  1639. /*
  1640. * Maybe do better on x86 and provide cpu_clock_nmi()
  1641. */
  1642. time = sched_clock();
  1643. header.type |= PERF_RECORD_TIME;
  1644. header.size += sizeof(u64);
  1645. }
  1646. if (record_type & PERF_RECORD_ADDR) {
  1647. header.type |= PERF_RECORD_ADDR;
  1648. header.size += sizeof(u64);
  1649. }
  1650. if (record_type & PERF_RECORD_CONFIG) {
  1651. header.type |= PERF_RECORD_CONFIG;
  1652. header.size += sizeof(u64);
  1653. }
  1654. if (record_type & PERF_RECORD_CPU) {
  1655. header.type |= PERF_RECORD_CPU;
  1656. header.size += sizeof(cpu_entry);
  1657. cpu_entry.cpu = raw_smp_processor_id();
  1658. }
  1659. if (record_type & PERF_RECORD_GROUP) {
  1660. header.type |= PERF_RECORD_GROUP;
  1661. header.size += sizeof(u64) +
  1662. counter->nr_siblings * sizeof(group_entry);
  1663. }
  1664. if (record_type & PERF_RECORD_CALLCHAIN) {
  1665. callchain = perf_callchain(regs);
  1666. if (callchain) {
  1667. callchain_size = (1 + callchain->nr) * sizeof(u64);
  1668. header.type |= PERF_RECORD_CALLCHAIN;
  1669. header.size += callchain_size;
  1670. }
  1671. }
  1672. ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
  1673. if (ret)
  1674. return;
  1675. perf_output_put(&handle, header);
  1676. if (record_type & PERF_RECORD_IP)
  1677. perf_output_put(&handle, ip);
  1678. if (record_type & PERF_RECORD_TID)
  1679. perf_output_put(&handle, tid_entry);
  1680. if (record_type & PERF_RECORD_TIME)
  1681. perf_output_put(&handle, time);
  1682. if (record_type & PERF_RECORD_ADDR)
  1683. perf_output_put(&handle, addr);
  1684. if (record_type & PERF_RECORD_CONFIG)
  1685. perf_output_put(&handle, counter->hw_event.config);
  1686. if (record_type & PERF_RECORD_CPU)
  1687. perf_output_put(&handle, cpu_entry);
  1688. /*
  1689. * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
  1690. */
  1691. if (record_type & PERF_RECORD_GROUP) {
  1692. struct perf_counter *leader, *sub;
  1693. u64 nr = counter->nr_siblings;
  1694. perf_output_put(&handle, nr);
  1695. leader = counter->group_leader;
  1696. list_for_each_entry(sub, &leader->sibling_list, list_entry) {
  1697. if (sub != counter)
  1698. sub->pmu->read(sub);
  1699. group_entry.event = sub->hw_event.config;
  1700. group_entry.counter = atomic64_read(&sub->count);
  1701. perf_output_put(&handle, group_entry);
  1702. }
  1703. }
  1704. if (callchain)
  1705. perf_output_copy(&handle, callchain, callchain_size);
  1706. perf_output_end(&handle);
  1707. }
  1708. /*
  1709. * comm tracking
  1710. */
  1711. struct perf_comm_event {
  1712. struct task_struct *task;
  1713. char *comm;
  1714. int comm_size;
  1715. struct {
  1716. struct perf_event_header header;
  1717. u32 pid;
  1718. u32 tid;
  1719. } event;
  1720. };
  1721. static void perf_counter_comm_output(struct perf_counter *counter,
  1722. struct perf_comm_event *comm_event)
  1723. {
  1724. struct perf_output_handle handle;
  1725. int size = comm_event->event.header.size;
  1726. int ret = perf_output_begin(&handle, counter, size, 0, 0);
  1727. if (ret)
  1728. return;
  1729. perf_output_put(&handle, comm_event->event);
  1730. perf_output_copy(&handle, comm_event->comm,
  1731. comm_event->comm_size);
  1732. perf_output_end(&handle);
  1733. }
  1734. static int perf_counter_comm_match(struct perf_counter *counter,
  1735. struct perf_comm_event *comm_event)
  1736. {
  1737. if (counter->hw_event.comm &&
  1738. comm_event->event.header.type == PERF_EVENT_COMM)
  1739. return 1;
  1740. return 0;
  1741. }
  1742. static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
  1743. struct perf_comm_event *comm_event)
  1744. {
  1745. struct perf_counter *counter;
  1746. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  1747. return;
  1748. rcu_read_lock();
  1749. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  1750. if (perf_counter_comm_match(counter, comm_event))
  1751. perf_counter_comm_output(counter, comm_event);
  1752. }
  1753. rcu_read_unlock();
  1754. }
  1755. static void perf_counter_comm_event(struct perf_comm_event *comm_event)
  1756. {
  1757. struct perf_cpu_context *cpuctx;
  1758. unsigned int size;
  1759. char *comm = comm_event->task->comm;
  1760. size = ALIGN(strlen(comm)+1, sizeof(u64));
  1761. comm_event->comm = comm;
  1762. comm_event->comm_size = size;
  1763. comm_event->event.header.size = sizeof(comm_event->event) + size;
  1764. cpuctx = &get_cpu_var(perf_cpu_context);
  1765. perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
  1766. put_cpu_var(perf_cpu_context);
  1767. perf_counter_comm_ctx(&current->perf_counter_ctx, comm_event);
  1768. }
  1769. void perf_counter_comm(struct task_struct *task)
  1770. {
  1771. struct perf_comm_event comm_event;
  1772. if (!atomic_read(&nr_comm_tracking))
  1773. return;
  1774. comm_event = (struct perf_comm_event){
  1775. .task = task,
  1776. .event = {
  1777. .header = { .type = PERF_EVENT_COMM, },
  1778. .pid = task->group_leader->pid,
  1779. .tid = task->pid,
  1780. },
  1781. };
  1782. perf_counter_comm_event(&comm_event);
  1783. }
  1784. /*
  1785. * mmap tracking
  1786. */
  1787. struct perf_mmap_event {
  1788. struct file *file;
  1789. char *file_name;
  1790. int file_size;
  1791. struct {
  1792. struct perf_event_header header;
  1793. u32 pid;
  1794. u32 tid;
  1795. u64 start;
  1796. u64 len;
  1797. u64 pgoff;
  1798. } event;
  1799. };
  1800. static void perf_counter_mmap_output(struct perf_counter *counter,
  1801. struct perf_mmap_event *mmap_event)
  1802. {
  1803. struct perf_output_handle handle;
  1804. int size = mmap_event->event.header.size;
  1805. int ret = perf_output_begin(&handle, counter, size, 0, 0);
  1806. if (ret)
  1807. return;
  1808. perf_output_put(&handle, mmap_event->event);
  1809. perf_output_copy(&handle, mmap_event->file_name,
  1810. mmap_event->file_size);
  1811. perf_output_end(&handle);
  1812. }
  1813. static int perf_counter_mmap_match(struct perf_counter *counter,
  1814. struct perf_mmap_event *mmap_event)
  1815. {
  1816. if (counter->hw_event.mmap &&
  1817. mmap_event->event.header.type == PERF_EVENT_MMAP)
  1818. return 1;
  1819. if (counter->hw_event.munmap &&
  1820. mmap_event->event.header.type == PERF_EVENT_MUNMAP)
  1821. return 1;
  1822. return 0;
  1823. }
  1824. static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
  1825. struct perf_mmap_event *mmap_event)
  1826. {
  1827. struct perf_counter *counter;
  1828. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  1829. return;
  1830. rcu_read_lock();
  1831. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  1832. if (perf_counter_mmap_match(counter, mmap_event))
  1833. perf_counter_mmap_output(counter, mmap_event);
  1834. }
  1835. rcu_read_unlock();
  1836. }
  1837. static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
  1838. {
  1839. struct perf_cpu_context *cpuctx;
  1840. struct file *file = mmap_event->file;
  1841. unsigned int size;
  1842. char tmp[16];
  1843. char *buf = NULL;
  1844. char *name;
  1845. if (file) {
  1846. buf = kzalloc(PATH_MAX, GFP_KERNEL);
  1847. if (!buf) {
  1848. name = strncpy(tmp, "//enomem", sizeof(tmp));
  1849. goto got_name;
  1850. }
  1851. name = d_path(&file->f_path, buf, PATH_MAX);
  1852. if (IS_ERR(name)) {
  1853. name = strncpy(tmp, "//toolong", sizeof(tmp));
  1854. goto got_name;
  1855. }
  1856. } else {
  1857. name = strncpy(tmp, "//anon", sizeof(tmp));
  1858. goto got_name;
  1859. }
  1860. got_name:
  1861. size = ALIGN(strlen(name)+1, sizeof(u64));
  1862. mmap_event->file_name = name;
  1863. mmap_event->file_size = size;
  1864. mmap_event->event.header.size = sizeof(mmap_event->event) + size;
  1865. cpuctx = &get_cpu_var(perf_cpu_context);
  1866. perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
  1867. put_cpu_var(perf_cpu_context);
  1868. perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
  1869. kfree(buf);
  1870. }
  1871. void perf_counter_mmap(unsigned long addr, unsigned long len,
  1872. unsigned long pgoff, struct file *file)
  1873. {
  1874. struct perf_mmap_event mmap_event;
  1875. if (!atomic_read(&nr_mmap_tracking))
  1876. return;
  1877. mmap_event = (struct perf_mmap_event){
  1878. .file = file,
  1879. .event = {
  1880. .header = { .type = PERF_EVENT_MMAP, },
  1881. .pid = current->group_leader->pid,
  1882. .tid = current->pid,
  1883. .start = addr,
  1884. .len = len,
  1885. .pgoff = pgoff,
  1886. },
  1887. };
  1888. perf_counter_mmap_event(&mmap_event);
  1889. }
  1890. void perf_counter_munmap(unsigned long addr, unsigned long len,
  1891. unsigned long pgoff, struct file *file)
  1892. {
  1893. struct perf_mmap_event mmap_event;
  1894. if (!atomic_read(&nr_munmap_tracking))
  1895. return;
  1896. mmap_event = (struct perf_mmap_event){
  1897. .file = file,
  1898. .event = {
  1899. .header = { .type = PERF_EVENT_MUNMAP, },
  1900. .pid = current->group_leader->pid,
  1901. .tid = current->pid,
  1902. .start = addr,
  1903. .len = len,
  1904. .pgoff = pgoff,
  1905. },
  1906. };
  1907. perf_counter_mmap_event(&mmap_event);
  1908. }
  1909. /*
  1910. * Generic counter overflow handling.
  1911. */
  1912. int perf_counter_overflow(struct perf_counter *counter,
  1913. int nmi, struct pt_regs *regs, u64 addr)
  1914. {
  1915. int events = atomic_read(&counter->event_limit);
  1916. int ret = 0;
  1917. /*
  1918. * XXX event_limit might not quite work as expected on inherited
  1919. * counters
  1920. */
  1921. counter->pending_kill = POLL_IN;
  1922. if (events && atomic_dec_and_test(&counter->event_limit)) {
  1923. ret = 1;
  1924. counter->pending_kill = POLL_HUP;
  1925. if (nmi) {
  1926. counter->pending_disable = 1;
  1927. perf_pending_queue(&counter->pending,
  1928. perf_pending_counter);
  1929. } else
  1930. perf_counter_disable(counter);
  1931. }
  1932. perf_counter_output(counter, nmi, regs, addr);
  1933. return ret;
  1934. }
  1935. /*
  1936. * Generic software counter infrastructure
  1937. */
  1938. static void perf_swcounter_update(struct perf_counter *counter)
  1939. {
  1940. struct hw_perf_counter *hwc = &counter->hw;
  1941. u64 prev, now;
  1942. s64 delta;
  1943. again:
  1944. prev = atomic64_read(&hwc->prev_count);
  1945. now = atomic64_read(&hwc->count);
  1946. if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
  1947. goto again;
  1948. delta = now - prev;
  1949. atomic64_add(delta, &counter->count);
  1950. atomic64_sub(delta, &hwc->period_left);
  1951. }
  1952. static void perf_swcounter_set_period(struct perf_counter *counter)
  1953. {
  1954. struct hw_perf_counter *hwc = &counter->hw;
  1955. s64 left = atomic64_read(&hwc->period_left);
  1956. s64 period = hwc->irq_period;
  1957. if (unlikely(left <= -period)) {
  1958. left = period;
  1959. atomic64_set(&hwc->period_left, left);
  1960. }
  1961. if (unlikely(left <= 0)) {
  1962. left += period;
  1963. atomic64_add(period, &hwc->period_left);
  1964. }
  1965. atomic64_set(&hwc->prev_count, -left);
  1966. atomic64_set(&hwc->count, -left);
  1967. }
  1968. static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
  1969. {
  1970. enum hrtimer_restart ret = HRTIMER_RESTART;
  1971. struct perf_counter *counter;
  1972. struct pt_regs *regs;
  1973. counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
  1974. counter->pmu->read(counter);
  1975. regs = get_irq_regs();
  1976. /*
  1977. * In case we exclude kernel IPs or are somehow not in interrupt
  1978. * context, provide the next best thing, the user IP.
  1979. */
  1980. if ((counter->hw_event.exclude_kernel || !regs) &&
  1981. !counter->hw_event.exclude_user)
  1982. regs = task_pt_regs(current);
  1983. if (regs) {
  1984. if (perf_counter_overflow(counter, 0, regs, 0))
  1985. ret = HRTIMER_NORESTART;
  1986. }
  1987. hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
  1988. return ret;
  1989. }
  1990. static void perf_swcounter_overflow(struct perf_counter *counter,
  1991. int nmi, struct pt_regs *regs, u64 addr)
  1992. {
  1993. perf_swcounter_update(counter);
  1994. perf_swcounter_set_period(counter);
  1995. if (perf_counter_overflow(counter, nmi, regs, addr))
  1996. /* soft-disable the counter */
  1997. ;
  1998. }
  1999. static int perf_swcounter_match(struct perf_counter *counter,
  2000. enum perf_event_types type,
  2001. u32 event, struct pt_regs *regs)
  2002. {
  2003. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  2004. return 0;
  2005. if (perf_event_raw(&counter->hw_event))
  2006. return 0;
  2007. if (perf_event_type(&counter->hw_event) != type)
  2008. return 0;
  2009. if (perf_event_id(&counter->hw_event) != event)
  2010. return 0;
  2011. if (counter->hw_event.exclude_user && user_mode(regs))
  2012. return 0;
  2013. if (counter->hw_event.exclude_kernel && !user_mode(regs))
  2014. return 0;
  2015. return 1;
  2016. }
  2017. static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
  2018. int nmi, struct pt_regs *regs, u64 addr)
  2019. {
  2020. int neg = atomic64_add_negative(nr, &counter->hw.count);
  2021. if (counter->hw.irq_period && !neg)
  2022. perf_swcounter_overflow(counter, nmi, regs, addr);
  2023. }
  2024. static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
  2025. enum perf_event_types type, u32 event,
  2026. u64 nr, int nmi, struct pt_regs *regs,
  2027. u64 addr)
  2028. {
  2029. struct perf_counter *counter;
  2030. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  2031. return;
  2032. rcu_read_lock();
  2033. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  2034. if (perf_swcounter_match(counter, type, event, regs))
  2035. perf_swcounter_add(counter, nr, nmi, regs, addr);
  2036. }
  2037. rcu_read_unlock();
  2038. }
  2039. static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
  2040. {
  2041. if (in_nmi())
  2042. return &cpuctx->recursion[3];
  2043. if (in_irq())
  2044. return &cpuctx->recursion[2];
  2045. if (in_softirq())
  2046. return &cpuctx->recursion[1];
  2047. return &cpuctx->recursion[0];
  2048. }
  2049. static void __perf_swcounter_event(enum perf_event_types type, u32 event,
  2050. u64 nr, int nmi, struct pt_regs *regs,
  2051. u64 addr)
  2052. {
  2053. struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
  2054. int *recursion = perf_swcounter_recursion_context(cpuctx);
  2055. if (*recursion)
  2056. goto out;
  2057. (*recursion)++;
  2058. barrier();
  2059. perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
  2060. nr, nmi, regs, addr);
  2061. if (cpuctx->task_ctx) {
  2062. perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
  2063. nr, nmi, regs, addr);
  2064. }
  2065. barrier();
  2066. (*recursion)--;
  2067. out:
  2068. put_cpu_var(perf_cpu_context);
  2069. }
  2070. void
  2071. perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
  2072. {
  2073. __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
  2074. }
  2075. static void perf_swcounter_read(struct perf_counter *counter)
  2076. {
  2077. perf_swcounter_update(counter);
  2078. }
  2079. static int perf_swcounter_enable(struct perf_counter *counter)
  2080. {
  2081. perf_swcounter_set_period(counter);
  2082. return 0;
  2083. }
  2084. static void perf_swcounter_disable(struct perf_counter *counter)
  2085. {
  2086. perf_swcounter_update(counter);
  2087. }
  2088. static const struct pmu perf_ops_generic = {
  2089. .enable = perf_swcounter_enable,
  2090. .disable = perf_swcounter_disable,
  2091. .read = perf_swcounter_read,
  2092. };
  2093. /*
  2094. * Software counter: cpu wall time clock
  2095. */
  2096. static void cpu_clock_perf_counter_update(struct perf_counter *counter)
  2097. {
  2098. int cpu = raw_smp_processor_id();
  2099. s64 prev;
  2100. u64 now;
  2101. now = cpu_clock(cpu);
  2102. prev = atomic64_read(&counter->hw.prev_count);
  2103. atomic64_set(&counter->hw.prev_count, now);
  2104. atomic64_add(now - prev, &counter->count);
  2105. }
  2106. static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
  2107. {
  2108. struct hw_perf_counter *hwc = &counter->hw;
  2109. int cpu = raw_smp_processor_id();
  2110. atomic64_set(&hwc->prev_count, cpu_clock(cpu));
  2111. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2112. hwc->hrtimer.function = perf_swcounter_hrtimer;
  2113. if (hwc->irq_period) {
  2114. __hrtimer_start_range_ns(&hwc->hrtimer,
  2115. ns_to_ktime(hwc->irq_period), 0,
  2116. HRTIMER_MODE_REL, 0);
  2117. }
  2118. return 0;
  2119. }
  2120. static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
  2121. {
  2122. hrtimer_cancel(&counter->hw.hrtimer);
  2123. cpu_clock_perf_counter_update(counter);
  2124. }
  2125. static void cpu_clock_perf_counter_read(struct perf_counter *counter)
  2126. {
  2127. cpu_clock_perf_counter_update(counter);
  2128. }
  2129. static const struct pmu perf_ops_cpu_clock = {
  2130. .enable = cpu_clock_perf_counter_enable,
  2131. .disable = cpu_clock_perf_counter_disable,
  2132. .read = cpu_clock_perf_counter_read,
  2133. };
  2134. /*
  2135. * Software counter: task time clock
  2136. */
  2137. static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
  2138. {
  2139. u64 prev;
  2140. s64 delta;
  2141. prev = atomic64_xchg(&counter->hw.prev_count, now);
  2142. delta = now - prev;
  2143. atomic64_add(delta, &counter->count);
  2144. }
  2145. static int task_clock_perf_counter_enable(struct perf_counter *counter)
  2146. {
  2147. struct hw_perf_counter *hwc = &counter->hw;
  2148. u64 now;
  2149. now = counter->ctx->time;
  2150. atomic64_set(&hwc->prev_count, now);
  2151. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2152. hwc->hrtimer.function = perf_swcounter_hrtimer;
  2153. if (hwc->irq_period) {
  2154. __hrtimer_start_range_ns(&hwc->hrtimer,
  2155. ns_to_ktime(hwc->irq_period), 0,
  2156. HRTIMER_MODE_REL, 0);
  2157. }
  2158. return 0;
  2159. }
  2160. static void task_clock_perf_counter_disable(struct perf_counter *counter)
  2161. {
  2162. hrtimer_cancel(&counter->hw.hrtimer);
  2163. task_clock_perf_counter_update(counter, counter->ctx->time);
  2164. }
  2165. static void task_clock_perf_counter_read(struct perf_counter *counter)
  2166. {
  2167. u64 time;
  2168. if (!in_nmi()) {
  2169. update_context_time(counter->ctx);
  2170. time = counter->ctx->time;
  2171. } else {
  2172. u64 now = perf_clock();
  2173. u64 delta = now - counter->ctx->timestamp;
  2174. time = counter->ctx->time + delta;
  2175. }
  2176. task_clock_perf_counter_update(counter, time);
  2177. }
  2178. static const struct pmu perf_ops_task_clock = {
  2179. .enable = task_clock_perf_counter_enable,
  2180. .disable = task_clock_perf_counter_disable,
  2181. .read = task_clock_perf_counter_read,
  2182. };
  2183. /*
  2184. * Software counter: cpu migrations
  2185. */
  2186. static inline u64 get_cpu_migrations(struct perf_counter *counter)
  2187. {
  2188. struct task_struct *curr = counter->ctx->task;
  2189. if (curr)
  2190. return curr->se.nr_migrations;
  2191. return cpu_nr_migrations(smp_processor_id());
  2192. }
  2193. static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
  2194. {
  2195. u64 prev, now;
  2196. s64 delta;
  2197. prev = atomic64_read(&counter->hw.prev_count);
  2198. now = get_cpu_migrations(counter);
  2199. atomic64_set(&counter->hw.prev_count, now);
  2200. delta = now - prev;
  2201. atomic64_add(delta, &counter->count);
  2202. }
  2203. static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
  2204. {
  2205. cpu_migrations_perf_counter_update(counter);
  2206. }
  2207. static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
  2208. {
  2209. if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
  2210. atomic64_set(&counter->hw.prev_count,
  2211. get_cpu_migrations(counter));
  2212. return 0;
  2213. }
  2214. static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
  2215. {
  2216. cpu_migrations_perf_counter_update(counter);
  2217. }
  2218. static const struct pmu perf_ops_cpu_migrations = {
  2219. .enable = cpu_migrations_perf_counter_enable,
  2220. .disable = cpu_migrations_perf_counter_disable,
  2221. .read = cpu_migrations_perf_counter_read,
  2222. };
  2223. #ifdef CONFIG_EVENT_PROFILE
  2224. void perf_tpcounter_event(int event_id)
  2225. {
  2226. struct pt_regs *regs = get_irq_regs();
  2227. if (!regs)
  2228. regs = task_pt_regs(current);
  2229. __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
  2230. }
  2231. EXPORT_SYMBOL_GPL(perf_tpcounter_event);
  2232. extern int ftrace_profile_enable(int);
  2233. extern void ftrace_profile_disable(int);
  2234. static void tp_perf_counter_destroy(struct perf_counter *counter)
  2235. {
  2236. ftrace_profile_disable(perf_event_id(&counter->hw_event));
  2237. }
  2238. static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
  2239. {
  2240. int event_id = perf_event_id(&counter->hw_event);
  2241. int ret;
  2242. ret = ftrace_profile_enable(event_id);
  2243. if (ret)
  2244. return NULL;
  2245. counter->destroy = tp_perf_counter_destroy;
  2246. counter->hw.irq_period = counter->hw_event.irq_period;
  2247. return &perf_ops_generic;
  2248. }
  2249. #else
  2250. static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
  2251. {
  2252. return NULL;
  2253. }
  2254. #endif
  2255. static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
  2256. {
  2257. struct perf_counter_hw_event *hw_event = &counter->hw_event;
  2258. const struct pmu *pmu = NULL;
  2259. struct hw_perf_counter *hwc = &counter->hw;
  2260. /*
  2261. * Software counters (currently) can't in general distinguish
  2262. * between user, kernel and hypervisor events.
  2263. * However, context switches and cpu migrations are considered
  2264. * to be kernel events, and page faults are never hypervisor
  2265. * events.
  2266. */
  2267. switch (perf_event_id(&counter->hw_event)) {
  2268. case PERF_COUNT_CPU_CLOCK:
  2269. pmu = &perf_ops_cpu_clock;
  2270. if (hw_event->irq_period && hw_event->irq_period < 10000)
  2271. hw_event->irq_period = 10000;
  2272. break;
  2273. case PERF_COUNT_TASK_CLOCK:
  2274. /*
  2275. * If the user instantiates this as a per-cpu counter,
  2276. * use the cpu_clock counter instead.
  2277. */
  2278. if (counter->ctx->task)
  2279. pmu = &perf_ops_task_clock;
  2280. else
  2281. pmu = &perf_ops_cpu_clock;
  2282. if (hw_event->irq_period && hw_event->irq_period < 10000)
  2283. hw_event->irq_period = 10000;
  2284. break;
  2285. case PERF_COUNT_PAGE_FAULTS:
  2286. case PERF_COUNT_PAGE_FAULTS_MIN:
  2287. case PERF_COUNT_PAGE_FAULTS_MAJ:
  2288. case PERF_COUNT_CONTEXT_SWITCHES:
  2289. pmu = &perf_ops_generic;
  2290. break;
  2291. case PERF_COUNT_CPU_MIGRATIONS:
  2292. if (!counter->hw_event.exclude_kernel)
  2293. pmu = &perf_ops_cpu_migrations;
  2294. break;
  2295. }
  2296. if (pmu)
  2297. hwc->irq_period = hw_event->irq_period;
  2298. return pmu;
  2299. }
  2300. /*
  2301. * Allocate and initialize a counter structure
  2302. */
  2303. static struct perf_counter *
  2304. perf_counter_alloc(struct perf_counter_hw_event *hw_event,
  2305. int cpu,
  2306. struct perf_counter_context *ctx,
  2307. struct perf_counter *group_leader,
  2308. gfp_t gfpflags)
  2309. {
  2310. const struct pmu *pmu;
  2311. struct perf_counter *counter;
  2312. long err;
  2313. counter = kzalloc(sizeof(*counter), gfpflags);
  2314. if (!counter)
  2315. return ERR_PTR(-ENOMEM);
  2316. /*
  2317. * Single counters are their own group leaders, with an
  2318. * empty sibling list:
  2319. */
  2320. if (!group_leader)
  2321. group_leader = counter;
  2322. mutex_init(&counter->mutex);
  2323. INIT_LIST_HEAD(&counter->list_entry);
  2324. INIT_LIST_HEAD(&counter->event_entry);
  2325. INIT_LIST_HEAD(&counter->sibling_list);
  2326. init_waitqueue_head(&counter->waitq);
  2327. mutex_init(&counter->mmap_mutex);
  2328. INIT_LIST_HEAD(&counter->child_list);
  2329. counter->cpu = cpu;
  2330. counter->hw_event = *hw_event;
  2331. counter->group_leader = group_leader;
  2332. counter->pmu = NULL;
  2333. counter->ctx = ctx;
  2334. counter->state = PERF_COUNTER_STATE_INACTIVE;
  2335. if (hw_event->disabled)
  2336. counter->state = PERF_COUNTER_STATE_OFF;
  2337. pmu = NULL;
  2338. /*
  2339. * we currently do not support PERF_RECORD_GROUP on inherited counters
  2340. */
  2341. if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
  2342. goto done;
  2343. if (perf_event_raw(hw_event)) {
  2344. pmu = hw_perf_counter_init(counter);
  2345. goto done;
  2346. }
  2347. switch (perf_event_type(hw_event)) {
  2348. case PERF_TYPE_HARDWARE:
  2349. pmu = hw_perf_counter_init(counter);
  2350. break;
  2351. case PERF_TYPE_SOFTWARE:
  2352. pmu = sw_perf_counter_init(counter);
  2353. break;
  2354. case PERF_TYPE_TRACEPOINT:
  2355. pmu = tp_perf_counter_init(counter);
  2356. break;
  2357. }
  2358. done:
  2359. err = 0;
  2360. if (!pmu)
  2361. err = -EINVAL;
  2362. else if (IS_ERR(pmu))
  2363. err = PTR_ERR(pmu);
  2364. if (err) {
  2365. kfree(counter);
  2366. return ERR_PTR(err);
  2367. }
  2368. counter->pmu = pmu;
  2369. atomic_inc(&nr_counters);
  2370. if (counter->hw_event.mmap)
  2371. atomic_inc(&nr_mmap_tracking);
  2372. if (counter->hw_event.munmap)
  2373. atomic_inc(&nr_munmap_tracking);
  2374. if (counter->hw_event.comm)
  2375. atomic_inc(&nr_comm_tracking);
  2376. return counter;
  2377. }
  2378. /**
  2379. * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
  2380. *
  2381. * @hw_event_uptr: event type attributes for monitoring/sampling
  2382. * @pid: target pid
  2383. * @cpu: target cpu
  2384. * @group_fd: group leader counter fd
  2385. */
  2386. SYSCALL_DEFINE5(perf_counter_open,
  2387. const struct perf_counter_hw_event __user *, hw_event_uptr,
  2388. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  2389. {
  2390. struct perf_counter *counter, *group_leader;
  2391. struct perf_counter_hw_event hw_event;
  2392. struct perf_counter_context *ctx;
  2393. struct file *counter_file = NULL;
  2394. struct file *group_file = NULL;
  2395. int fput_needed = 0;
  2396. int fput_needed2 = 0;
  2397. int ret;
  2398. /* for future expandability... */
  2399. if (flags)
  2400. return -EINVAL;
  2401. if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
  2402. return -EFAULT;
  2403. /*
  2404. * Get the target context (task or percpu):
  2405. */
  2406. ctx = find_get_context(pid, cpu);
  2407. if (IS_ERR(ctx))
  2408. return PTR_ERR(ctx);
  2409. /*
  2410. * Look up the group leader (we will attach this counter to it):
  2411. */
  2412. group_leader = NULL;
  2413. if (group_fd != -1) {
  2414. ret = -EINVAL;
  2415. group_file = fget_light(group_fd, &fput_needed);
  2416. if (!group_file)
  2417. goto err_put_context;
  2418. if (group_file->f_op != &perf_fops)
  2419. goto err_put_context;
  2420. group_leader = group_file->private_data;
  2421. /*
  2422. * Do not allow a recursive hierarchy (this new sibling
  2423. * becoming part of another group-sibling):
  2424. */
  2425. if (group_leader->group_leader != group_leader)
  2426. goto err_put_context;
  2427. /*
  2428. * Do not allow to attach to a group in a different
  2429. * task or CPU context:
  2430. */
  2431. if (group_leader->ctx != ctx)
  2432. goto err_put_context;
  2433. /*
  2434. * Only a group leader can be exclusive or pinned
  2435. */
  2436. if (hw_event.exclusive || hw_event.pinned)
  2437. goto err_put_context;
  2438. }
  2439. counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
  2440. GFP_KERNEL);
  2441. ret = PTR_ERR(counter);
  2442. if (IS_ERR(counter))
  2443. goto err_put_context;
  2444. ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
  2445. if (ret < 0)
  2446. goto err_free_put_context;
  2447. counter_file = fget_light(ret, &fput_needed2);
  2448. if (!counter_file)
  2449. goto err_free_put_context;
  2450. counter->filp = counter_file;
  2451. mutex_lock(&ctx->mutex);
  2452. perf_install_in_context(ctx, counter, cpu);
  2453. mutex_unlock(&ctx->mutex);
  2454. fput_light(counter_file, fput_needed2);
  2455. out_fput:
  2456. fput_light(group_file, fput_needed);
  2457. return ret;
  2458. err_free_put_context:
  2459. kfree(counter);
  2460. err_put_context:
  2461. put_context(ctx);
  2462. goto out_fput;
  2463. }
  2464. /*
  2465. * Initialize the perf_counter context in a task_struct:
  2466. */
  2467. static void
  2468. __perf_counter_init_context(struct perf_counter_context *ctx,
  2469. struct task_struct *task)
  2470. {
  2471. memset(ctx, 0, sizeof(*ctx));
  2472. spin_lock_init(&ctx->lock);
  2473. mutex_init(&ctx->mutex);
  2474. INIT_LIST_HEAD(&ctx->counter_list);
  2475. INIT_LIST_HEAD(&ctx->event_list);
  2476. ctx->task = task;
  2477. }
  2478. /*
  2479. * inherit a counter from parent task to child task:
  2480. */
  2481. static struct perf_counter *
  2482. inherit_counter(struct perf_counter *parent_counter,
  2483. struct task_struct *parent,
  2484. struct perf_counter_context *parent_ctx,
  2485. struct task_struct *child,
  2486. struct perf_counter *group_leader,
  2487. struct perf_counter_context *child_ctx)
  2488. {
  2489. struct perf_counter *child_counter;
  2490. /*
  2491. * Instead of creating recursive hierarchies of counters,
  2492. * we link inherited counters back to the original parent,
  2493. * which has a filp for sure, which we use as the reference
  2494. * count:
  2495. */
  2496. if (parent_counter->parent)
  2497. parent_counter = parent_counter->parent;
  2498. child_counter = perf_counter_alloc(&parent_counter->hw_event,
  2499. parent_counter->cpu, child_ctx,
  2500. group_leader, GFP_KERNEL);
  2501. if (IS_ERR(child_counter))
  2502. return child_counter;
  2503. /*
  2504. * Link it up in the child's context:
  2505. */
  2506. child_counter->task = child;
  2507. add_counter_to_ctx(child_counter, child_ctx);
  2508. child_counter->parent = parent_counter;
  2509. /*
  2510. * inherit into child's child as well:
  2511. */
  2512. child_counter->hw_event.inherit = 1;
  2513. /*
  2514. * Get a reference to the parent filp - we will fput it
  2515. * when the child counter exits. This is safe to do because
  2516. * we are in the parent and we know that the filp still
  2517. * exists and has a nonzero count:
  2518. */
  2519. atomic_long_inc(&parent_counter->filp->f_count);
  2520. /*
  2521. * Link this into the parent counter's child list
  2522. */
  2523. mutex_lock(&parent_counter->mutex);
  2524. list_add_tail(&child_counter->child_list, &parent_counter->child_list);
  2525. /*
  2526. * Make the child state follow the state of the parent counter,
  2527. * not its hw_event.disabled bit. We hold the parent's mutex,
  2528. * so we won't race with perf_counter_{en,dis}able_family.
  2529. */
  2530. if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
  2531. child_counter->state = PERF_COUNTER_STATE_INACTIVE;
  2532. else
  2533. child_counter->state = PERF_COUNTER_STATE_OFF;
  2534. mutex_unlock(&parent_counter->mutex);
  2535. return child_counter;
  2536. }
  2537. static int inherit_group(struct perf_counter *parent_counter,
  2538. struct task_struct *parent,
  2539. struct perf_counter_context *parent_ctx,
  2540. struct task_struct *child,
  2541. struct perf_counter_context *child_ctx)
  2542. {
  2543. struct perf_counter *leader;
  2544. struct perf_counter *sub;
  2545. struct perf_counter *child_ctr;
  2546. leader = inherit_counter(parent_counter, parent, parent_ctx,
  2547. child, NULL, child_ctx);
  2548. if (IS_ERR(leader))
  2549. return PTR_ERR(leader);
  2550. list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
  2551. child_ctr = inherit_counter(sub, parent, parent_ctx,
  2552. child, leader, child_ctx);
  2553. if (IS_ERR(child_ctr))
  2554. return PTR_ERR(child_ctr);
  2555. }
  2556. return 0;
  2557. }
  2558. static void sync_child_counter(struct perf_counter *child_counter,
  2559. struct perf_counter *parent_counter)
  2560. {
  2561. u64 parent_val, child_val;
  2562. parent_val = atomic64_read(&parent_counter->count);
  2563. child_val = atomic64_read(&child_counter->count);
  2564. /*
  2565. * Add back the child's count to the parent's count:
  2566. */
  2567. atomic64_add(child_val, &parent_counter->count);
  2568. atomic64_add(child_counter->total_time_enabled,
  2569. &parent_counter->child_total_time_enabled);
  2570. atomic64_add(child_counter->total_time_running,
  2571. &parent_counter->child_total_time_running);
  2572. /*
  2573. * Remove this counter from the parent's list
  2574. */
  2575. mutex_lock(&parent_counter->mutex);
  2576. list_del_init(&child_counter->child_list);
  2577. mutex_unlock(&parent_counter->mutex);
  2578. /*
  2579. * Release the parent counter, if this was the last
  2580. * reference to it.
  2581. */
  2582. fput(parent_counter->filp);
  2583. }
  2584. static void
  2585. __perf_counter_exit_task(struct task_struct *child,
  2586. struct perf_counter *child_counter,
  2587. struct perf_counter_context *child_ctx)
  2588. {
  2589. struct perf_counter *parent_counter;
  2590. struct perf_counter *sub, *tmp;
  2591. /*
  2592. * If we do not self-reap then we have to wait for the
  2593. * child task to unschedule (it will happen for sure),
  2594. * so that its counter is at its final count. (This
  2595. * condition triggers rarely - child tasks usually get
  2596. * off their CPU before the parent has a chance to
  2597. * get this far into the reaping action)
  2598. */
  2599. if (child != current) {
  2600. wait_task_inactive(child, 0);
  2601. list_del_init(&child_counter->list_entry);
  2602. update_counter_times(child_counter);
  2603. } else {
  2604. struct perf_cpu_context *cpuctx;
  2605. unsigned long flags;
  2606. u64 perf_flags;
  2607. /*
  2608. * Disable and unlink this counter.
  2609. *
  2610. * Be careful about zapping the list - IRQ/NMI context
  2611. * could still be processing it:
  2612. */
  2613. local_irq_save(flags);
  2614. perf_flags = hw_perf_save_disable();
  2615. cpuctx = &__get_cpu_var(perf_cpu_context);
  2616. group_sched_out(child_counter, cpuctx, child_ctx);
  2617. update_counter_times(child_counter);
  2618. list_del_init(&child_counter->list_entry);
  2619. child_ctx->nr_counters--;
  2620. hw_perf_restore(perf_flags);
  2621. local_irq_restore(flags);
  2622. }
  2623. parent_counter = child_counter->parent;
  2624. /*
  2625. * It can happen that parent exits first, and has counters
  2626. * that are still around due to the child reference. These
  2627. * counters need to be zapped - but otherwise linger.
  2628. */
  2629. if (parent_counter) {
  2630. sync_child_counter(child_counter, parent_counter);
  2631. list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
  2632. list_entry) {
  2633. if (sub->parent) {
  2634. sync_child_counter(sub, sub->parent);
  2635. free_counter(sub);
  2636. }
  2637. }
  2638. free_counter(child_counter);
  2639. }
  2640. }
  2641. /*
  2642. * When a child task exits, feed back counter values to parent counters.
  2643. *
  2644. * Note: we may be running in child context, but the PID is not hashed
  2645. * anymore so new counters will not be added.
  2646. */
  2647. void perf_counter_exit_task(struct task_struct *child)
  2648. {
  2649. struct perf_counter *child_counter, *tmp;
  2650. struct perf_counter_context *child_ctx;
  2651. child_ctx = &child->perf_counter_ctx;
  2652. if (likely(!child_ctx->nr_counters))
  2653. return;
  2654. list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
  2655. list_entry)
  2656. __perf_counter_exit_task(child, child_counter, child_ctx);
  2657. }
  2658. /*
  2659. * Initialize the perf_counter context in task_struct
  2660. */
  2661. void perf_counter_init_task(struct task_struct *child)
  2662. {
  2663. struct perf_counter_context *child_ctx, *parent_ctx;
  2664. struct perf_counter *counter;
  2665. struct task_struct *parent = current;
  2666. child_ctx = &child->perf_counter_ctx;
  2667. parent_ctx = &parent->perf_counter_ctx;
  2668. __perf_counter_init_context(child_ctx, child);
  2669. /*
  2670. * This is executed from the parent task context, so inherit
  2671. * counters that have been marked for cloning:
  2672. */
  2673. if (likely(!parent_ctx->nr_counters))
  2674. return;
  2675. /*
  2676. * Lock the parent list. No need to lock the child - not PID
  2677. * hashed yet and not running, so nobody can access it.
  2678. */
  2679. mutex_lock(&parent_ctx->mutex);
  2680. /*
  2681. * We dont have to disable NMIs - we are only looking at
  2682. * the list, not manipulating it:
  2683. */
  2684. list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
  2685. if (!counter->hw_event.inherit)
  2686. continue;
  2687. if (inherit_group(counter, parent,
  2688. parent_ctx, child, child_ctx))
  2689. break;
  2690. }
  2691. mutex_unlock(&parent_ctx->mutex);
  2692. }
  2693. static void __cpuinit perf_counter_init_cpu(int cpu)
  2694. {
  2695. struct perf_cpu_context *cpuctx;
  2696. cpuctx = &per_cpu(perf_cpu_context, cpu);
  2697. __perf_counter_init_context(&cpuctx->ctx, NULL);
  2698. spin_lock(&perf_resource_lock);
  2699. cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
  2700. spin_unlock(&perf_resource_lock);
  2701. hw_perf_counter_setup(cpu);
  2702. }
  2703. #ifdef CONFIG_HOTPLUG_CPU
  2704. static void __perf_counter_exit_cpu(void *info)
  2705. {
  2706. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  2707. struct perf_counter_context *ctx = &cpuctx->ctx;
  2708. struct perf_counter *counter, *tmp;
  2709. list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
  2710. __perf_counter_remove_from_context(counter);
  2711. }
  2712. static void perf_counter_exit_cpu(int cpu)
  2713. {
  2714. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  2715. struct perf_counter_context *ctx = &cpuctx->ctx;
  2716. mutex_lock(&ctx->mutex);
  2717. smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
  2718. mutex_unlock(&ctx->mutex);
  2719. }
  2720. #else
  2721. static inline void perf_counter_exit_cpu(int cpu) { }
  2722. #endif
  2723. static int __cpuinit
  2724. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  2725. {
  2726. unsigned int cpu = (long)hcpu;
  2727. switch (action) {
  2728. case CPU_UP_PREPARE:
  2729. case CPU_UP_PREPARE_FROZEN:
  2730. perf_counter_init_cpu(cpu);
  2731. break;
  2732. case CPU_DOWN_PREPARE:
  2733. case CPU_DOWN_PREPARE_FROZEN:
  2734. perf_counter_exit_cpu(cpu);
  2735. break;
  2736. default:
  2737. break;
  2738. }
  2739. return NOTIFY_OK;
  2740. }
  2741. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  2742. .notifier_call = perf_cpu_notify,
  2743. };
  2744. void __init perf_counter_init(void)
  2745. {
  2746. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  2747. (void *)(long)smp_processor_id());
  2748. register_cpu_notifier(&perf_cpu_nb);
  2749. }
  2750. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  2751. {
  2752. return sprintf(buf, "%d\n", perf_reserved_percpu);
  2753. }
  2754. static ssize_t
  2755. perf_set_reserve_percpu(struct sysdev_class *class,
  2756. const char *buf,
  2757. size_t count)
  2758. {
  2759. struct perf_cpu_context *cpuctx;
  2760. unsigned long val;
  2761. int err, cpu, mpt;
  2762. err = strict_strtoul(buf, 10, &val);
  2763. if (err)
  2764. return err;
  2765. if (val > perf_max_counters)
  2766. return -EINVAL;
  2767. spin_lock(&perf_resource_lock);
  2768. perf_reserved_percpu = val;
  2769. for_each_online_cpu(cpu) {
  2770. cpuctx = &per_cpu(perf_cpu_context, cpu);
  2771. spin_lock_irq(&cpuctx->ctx.lock);
  2772. mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
  2773. perf_max_counters - perf_reserved_percpu);
  2774. cpuctx->max_pertask = mpt;
  2775. spin_unlock_irq(&cpuctx->ctx.lock);
  2776. }
  2777. spin_unlock(&perf_resource_lock);
  2778. return count;
  2779. }
  2780. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  2781. {
  2782. return sprintf(buf, "%d\n", perf_overcommit);
  2783. }
  2784. static ssize_t
  2785. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  2786. {
  2787. unsigned long val;
  2788. int err;
  2789. err = strict_strtoul(buf, 10, &val);
  2790. if (err)
  2791. return err;
  2792. if (val > 1)
  2793. return -EINVAL;
  2794. spin_lock(&perf_resource_lock);
  2795. perf_overcommit = val;
  2796. spin_unlock(&perf_resource_lock);
  2797. return count;
  2798. }
  2799. static SYSDEV_CLASS_ATTR(
  2800. reserve_percpu,
  2801. 0644,
  2802. perf_show_reserve_percpu,
  2803. perf_set_reserve_percpu
  2804. );
  2805. static SYSDEV_CLASS_ATTR(
  2806. overcommit,
  2807. 0644,
  2808. perf_show_overcommit,
  2809. perf_set_overcommit
  2810. );
  2811. static struct attribute *perfclass_attrs[] = {
  2812. &attr_reserve_percpu.attr,
  2813. &attr_overcommit.attr,
  2814. NULL
  2815. };
  2816. static struct attribute_group perfclass_attr_group = {
  2817. .attrs = perfclass_attrs,
  2818. .name = "perf_counters",
  2819. };
  2820. static int __init perf_counter_sysfs_init(void)
  2821. {
  2822. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  2823. &perfclass_attr_group);
  2824. }
  2825. device_initcall(perf_counter_sysfs_init);