perf_counter.c 78 KB

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