perf_counter.c 73 KB

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