perf_counter.c 81 KB

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