perf_counter.c 74 KB

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