perf_counter.c 86 KB

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