perf_counter.c 86 KB

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