perf_counter.c 73 KB

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