perf_counter.c 86 KB

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