perf_counter.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367
  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. * For licencing details see kernel-base/COPYING
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/cpu.h>
  11. #include <linux/smp.h>
  12. #include <linux/file.h>
  13. #include <linux/poll.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/percpu.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/anon_inodes.h>
  20. #include <linux/kernel_stat.h>
  21. #include <linux/perf_counter.h>
  22. #include <linux/mm.h>
  23. #include <linux/vmstat.h>
  24. #include <linux/rculist.h>
  25. #include <asm/irq_regs.h>
  26. /*
  27. * Each CPU has a list of per CPU counters:
  28. */
  29. DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
  30. int perf_max_counters __read_mostly = 1;
  31. static int perf_reserved_percpu __read_mostly;
  32. static int perf_overcommit __read_mostly = 1;
  33. /*
  34. * Mutex for (sysadmin-configurable) counter reservations:
  35. */
  36. static DEFINE_MUTEX(perf_resource_mutex);
  37. /*
  38. * Architecture provided APIs - weak aliases:
  39. */
  40. extern __weak const struct hw_perf_counter_ops *
  41. hw_perf_counter_init(struct perf_counter *counter)
  42. {
  43. return NULL;
  44. }
  45. u64 __weak hw_perf_save_disable(void) { return 0; }
  46. void __weak hw_perf_restore(u64 ctrl) { barrier(); }
  47. void __weak hw_perf_counter_setup(int cpu) { barrier(); }
  48. int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
  49. struct perf_cpu_context *cpuctx,
  50. struct perf_counter_context *ctx, int cpu)
  51. {
  52. return 0;
  53. }
  54. void __weak perf_counter_print_debug(void) { }
  55. static void
  56. list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  57. {
  58. struct perf_counter *group_leader = counter->group_leader;
  59. /*
  60. * Depending on whether it is a standalone or sibling counter,
  61. * add it straight to the context's counter list, or to the group
  62. * leader's sibling list:
  63. */
  64. if (counter->group_leader == counter)
  65. list_add_tail(&counter->list_entry, &ctx->counter_list);
  66. else
  67. list_add_tail(&counter->list_entry, &group_leader->sibling_list);
  68. list_add_rcu(&counter->event_entry, &ctx->event_list);
  69. }
  70. static void
  71. list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  72. {
  73. struct perf_counter *sibling, *tmp;
  74. list_del_init(&counter->list_entry);
  75. list_del_rcu(&counter->event_entry);
  76. /*
  77. * If this was a group counter with sibling counters then
  78. * upgrade the siblings to singleton counters by adding them
  79. * to the context list directly:
  80. */
  81. list_for_each_entry_safe(sibling, tmp,
  82. &counter->sibling_list, list_entry) {
  83. list_move_tail(&sibling->list_entry, &ctx->counter_list);
  84. sibling->group_leader = sibling;
  85. }
  86. }
  87. static void
  88. counter_sched_out(struct perf_counter *counter,
  89. struct perf_cpu_context *cpuctx,
  90. struct perf_counter_context *ctx)
  91. {
  92. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  93. return;
  94. counter->state = PERF_COUNTER_STATE_INACTIVE;
  95. counter->hw_ops->disable(counter);
  96. counter->oncpu = -1;
  97. if (!is_software_counter(counter))
  98. cpuctx->active_oncpu--;
  99. ctx->nr_active--;
  100. if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
  101. cpuctx->exclusive = 0;
  102. }
  103. static void
  104. group_sched_out(struct perf_counter *group_counter,
  105. struct perf_cpu_context *cpuctx,
  106. struct perf_counter_context *ctx)
  107. {
  108. struct perf_counter *counter;
  109. if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
  110. return;
  111. counter_sched_out(group_counter, cpuctx, ctx);
  112. /*
  113. * Schedule out siblings (if any):
  114. */
  115. list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
  116. counter_sched_out(counter, cpuctx, ctx);
  117. if (group_counter->hw_event.exclusive)
  118. cpuctx->exclusive = 0;
  119. }
  120. /*
  121. * Cross CPU call to remove a performance counter
  122. *
  123. * We disable the counter on the hardware level first. After that we
  124. * remove it from the context list.
  125. */
  126. static void __perf_counter_remove_from_context(void *info)
  127. {
  128. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  129. struct perf_counter *counter = info;
  130. struct perf_counter_context *ctx = counter->ctx;
  131. unsigned long flags;
  132. u64 perf_flags;
  133. /*
  134. * If this is a task context, we need to check whether it is
  135. * the current task context of this cpu. If not it has been
  136. * scheduled out before the smp call arrived.
  137. */
  138. if (ctx->task && cpuctx->task_ctx != ctx)
  139. return;
  140. curr_rq_lock_irq_save(&flags);
  141. spin_lock(&ctx->lock);
  142. counter_sched_out(counter, cpuctx, ctx);
  143. counter->task = NULL;
  144. ctx->nr_counters--;
  145. /*
  146. * Protect the list operation against NMI by disabling the
  147. * counters on a global level. NOP for non NMI based counters.
  148. */
  149. perf_flags = hw_perf_save_disable();
  150. list_del_counter(counter, ctx);
  151. hw_perf_restore(perf_flags);
  152. if (!ctx->task) {
  153. /*
  154. * Allow more per task counters with respect to the
  155. * reservation:
  156. */
  157. cpuctx->max_pertask =
  158. min(perf_max_counters - ctx->nr_counters,
  159. perf_max_counters - perf_reserved_percpu);
  160. }
  161. spin_unlock(&ctx->lock);
  162. curr_rq_unlock_irq_restore(&flags);
  163. }
  164. /*
  165. * Remove the counter from a task's (or a CPU's) list of counters.
  166. *
  167. * Must be called with counter->mutex and ctx->mutex held.
  168. *
  169. * CPU counters are removed with a smp call. For task counters we only
  170. * call when the task is on a CPU.
  171. */
  172. static void perf_counter_remove_from_context(struct perf_counter *counter)
  173. {
  174. struct perf_counter_context *ctx = counter->ctx;
  175. struct task_struct *task = ctx->task;
  176. if (!task) {
  177. /*
  178. * Per cpu counters are removed via an smp call and
  179. * the removal is always sucessful.
  180. */
  181. smp_call_function_single(counter->cpu,
  182. __perf_counter_remove_from_context,
  183. counter, 1);
  184. return;
  185. }
  186. retry:
  187. task_oncpu_function_call(task, __perf_counter_remove_from_context,
  188. counter);
  189. spin_lock_irq(&ctx->lock);
  190. /*
  191. * If the context is active we need to retry the smp call.
  192. */
  193. if (ctx->nr_active && !list_empty(&counter->list_entry)) {
  194. spin_unlock_irq(&ctx->lock);
  195. goto retry;
  196. }
  197. /*
  198. * The lock prevents that this context is scheduled in so we
  199. * can remove the counter safely, if the call above did not
  200. * succeed.
  201. */
  202. if (!list_empty(&counter->list_entry)) {
  203. ctx->nr_counters--;
  204. list_del_counter(counter, ctx);
  205. counter->task = NULL;
  206. }
  207. spin_unlock_irq(&ctx->lock);
  208. }
  209. /*
  210. * Cross CPU call to disable a performance counter
  211. */
  212. static void __perf_counter_disable(void *info)
  213. {
  214. struct perf_counter *counter = info;
  215. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  216. struct perf_counter_context *ctx = counter->ctx;
  217. unsigned long flags;
  218. /*
  219. * If this is a per-task counter, need to check whether this
  220. * counter's task is the current task on this cpu.
  221. */
  222. if (ctx->task && cpuctx->task_ctx != ctx)
  223. return;
  224. curr_rq_lock_irq_save(&flags);
  225. spin_lock(&ctx->lock);
  226. /*
  227. * If the counter is on, turn it off.
  228. * If it is in error state, leave it in error state.
  229. */
  230. if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
  231. if (counter == counter->group_leader)
  232. group_sched_out(counter, cpuctx, ctx);
  233. else
  234. counter_sched_out(counter, cpuctx, ctx);
  235. counter->state = PERF_COUNTER_STATE_OFF;
  236. }
  237. spin_unlock(&ctx->lock);
  238. curr_rq_unlock_irq_restore(&flags);
  239. }
  240. /*
  241. * Disable a counter.
  242. */
  243. static void perf_counter_disable(struct perf_counter *counter)
  244. {
  245. struct perf_counter_context *ctx = counter->ctx;
  246. struct task_struct *task = ctx->task;
  247. if (!task) {
  248. /*
  249. * Disable the counter on the cpu that it's on
  250. */
  251. smp_call_function_single(counter->cpu, __perf_counter_disable,
  252. counter, 1);
  253. return;
  254. }
  255. retry:
  256. task_oncpu_function_call(task, __perf_counter_disable, counter);
  257. spin_lock_irq(&ctx->lock);
  258. /*
  259. * If the counter is still active, we need to retry the cross-call.
  260. */
  261. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  262. spin_unlock_irq(&ctx->lock);
  263. goto retry;
  264. }
  265. /*
  266. * Since we have the lock this context can't be scheduled
  267. * in, so we can change the state safely.
  268. */
  269. if (counter->state == PERF_COUNTER_STATE_INACTIVE)
  270. counter->state = PERF_COUNTER_STATE_OFF;
  271. spin_unlock_irq(&ctx->lock);
  272. }
  273. /*
  274. * Disable a counter and all its children.
  275. */
  276. static void perf_counter_disable_family(struct perf_counter *counter)
  277. {
  278. struct perf_counter *child;
  279. perf_counter_disable(counter);
  280. /*
  281. * Lock the mutex to protect the list of children
  282. */
  283. mutex_lock(&counter->mutex);
  284. list_for_each_entry(child, &counter->child_list, child_list)
  285. perf_counter_disable(child);
  286. mutex_unlock(&counter->mutex);
  287. }
  288. static int
  289. counter_sched_in(struct perf_counter *counter,
  290. struct perf_cpu_context *cpuctx,
  291. struct perf_counter_context *ctx,
  292. int cpu)
  293. {
  294. if (counter->state <= PERF_COUNTER_STATE_OFF)
  295. return 0;
  296. counter->state = PERF_COUNTER_STATE_ACTIVE;
  297. counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
  298. /*
  299. * The new state must be visible before we turn it on in the hardware:
  300. */
  301. smp_wmb();
  302. if (counter->hw_ops->enable(counter)) {
  303. counter->state = PERF_COUNTER_STATE_INACTIVE;
  304. counter->oncpu = -1;
  305. return -EAGAIN;
  306. }
  307. if (!is_software_counter(counter))
  308. cpuctx->active_oncpu++;
  309. ctx->nr_active++;
  310. if (counter->hw_event.exclusive)
  311. cpuctx->exclusive = 1;
  312. return 0;
  313. }
  314. /*
  315. * Return 1 for a group consisting entirely of software counters,
  316. * 0 if the group contains any hardware counters.
  317. */
  318. static int is_software_only_group(struct perf_counter *leader)
  319. {
  320. struct perf_counter *counter;
  321. if (!is_software_counter(leader))
  322. return 0;
  323. list_for_each_entry(counter, &leader->sibling_list, list_entry)
  324. if (!is_software_counter(counter))
  325. return 0;
  326. return 1;
  327. }
  328. /*
  329. * Work out whether we can put this counter group on the CPU now.
  330. */
  331. static int group_can_go_on(struct perf_counter *counter,
  332. struct perf_cpu_context *cpuctx,
  333. int can_add_hw)
  334. {
  335. /*
  336. * Groups consisting entirely of software counters can always go on.
  337. */
  338. if (is_software_only_group(counter))
  339. return 1;
  340. /*
  341. * If an exclusive group is already on, no other hardware
  342. * counters can go on.
  343. */
  344. if (cpuctx->exclusive)
  345. return 0;
  346. /*
  347. * If this group is exclusive and there are already
  348. * counters on the CPU, it can't go on.
  349. */
  350. if (counter->hw_event.exclusive && cpuctx->active_oncpu)
  351. return 0;
  352. /*
  353. * Otherwise, try to add it if all previous groups were able
  354. * to go on.
  355. */
  356. return can_add_hw;
  357. }
  358. /*
  359. * Cross CPU call to install and enable a performance counter
  360. */
  361. static void __perf_install_in_context(void *info)
  362. {
  363. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  364. struct perf_counter *counter = info;
  365. struct perf_counter_context *ctx = counter->ctx;
  366. struct perf_counter *leader = counter->group_leader;
  367. int cpu = smp_processor_id();
  368. unsigned long flags;
  369. u64 perf_flags;
  370. int err;
  371. /*
  372. * If this is a task context, we need to check whether it is
  373. * the current task context of this cpu. If not it has been
  374. * scheduled out before the smp call arrived.
  375. */
  376. if (ctx->task && cpuctx->task_ctx != ctx)
  377. return;
  378. curr_rq_lock_irq_save(&flags);
  379. spin_lock(&ctx->lock);
  380. /*
  381. * Protect the list operation against NMI by disabling the
  382. * counters on a global level. NOP for non NMI based counters.
  383. */
  384. perf_flags = hw_perf_save_disable();
  385. list_add_counter(counter, ctx);
  386. ctx->nr_counters++;
  387. counter->prev_state = PERF_COUNTER_STATE_OFF;
  388. /*
  389. * Don't put the counter on if it is disabled or if
  390. * it is in a group and the group isn't on.
  391. */
  392. if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
  393. (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
  394. goto unlock;
  395. /*
  396. * An exclusive counter can't go on if there are already active
  397. * hardware counters, and no hardware counter can go on if there
  398. * is already an exclusive counter on.
  399. */
  400. if (!group_can_go_on(counter, cpuctx, 1))
  401. err = -EEXIST;
  402. else
  403. err = counter_sched_in(counter, cpuctx, ctx, cpu);
  404. if (err) {
  405. /*
  406. * This counter couldn't go on. If it is in a group
  407. * then we have to pull the whole group off.
  408. * If the counter group is pinned then put it in error state.
  409. */
  410. if (leader != counter)
  411. group_sched_out(leader, cpuctx, ctx);
  412. if (leader->hw_event.pinned)
  413. leader->state = PERF_COUNTER_STATE_ERROR;
  414. }
  415. if (!err && !ctx->task && cpuctx->max_pertask)
  416. cpuctx->max_pertask--;
  417. unlock:
  418. hw_perf_restore(perf_flags);
  419. spin_unlock(&ctx->lock);
  420. curr_rq_unlock_irq_restore(&flags);
  421. }
  422. /*
  423. * Attach a performance counter to a context
  424. *
  425. * First we add the counter to the list with the hardware enable bit
  426. * in counter->hw_config cleared.
  427. *
  428. * If the counter is attached to a task which is on a CPU we use a smp
  429. * call to enable it in the task context. The task might have been
  430. * scheduled away, but we check this in the smp call again.
  431. *
  432. * Must be called with ctx->mutex held.
  433. */
  434. static void
  435. perf_install_in_context(struct perf_counter_context *ctx,
  436. struct perf_counter *counter,
  437. int cpu)
  438. {
  439. struct task_struct *task = ctx->task;
  440. if (!task) {
  441. /*
  442. * Per cpu counters are installed via an smp call and
  443. * the install is always sucessful.
  444. */
  445. smp_call_function_single(cpu, __perf_install_in_context,
  446. counter, 1);
  447. return;
  448. }
  449. counter->task = task;
  450. retry:
  451. task_oncpu_function_call(task, __perf_install_in_context,
  452. counter);
  453. spin_lock_irq(&ctx->lock);
  454. /*
  455. * we need to retry the smp call.
  456. */
  457. if (ctx->is_active && list_empty(&counter->list_entry)) {
  458. spin_unlock_irq(&ctx->lock);
  459. goto retry;
  460. }
  461. /*
  462. * The lock prevents that this context is scheduled in so we
  463. * can add the counter safely, if it the call above did not
  464. * succeed.
  465. */
  466. if (list_empty(&counter->list_entry)) {
  467. list_add_counter(counter, ctx);
  468. ctx->nr_counters++;
  469. }
  470. spin_unlock_irq(&ctx->lock);
  471. }
  472. /*
  473. * Cross CPU call to enable a performance counter
  474. */
  475. static void __perf_counter_enable(void *info)
  476. {
  477. struct perf_counter *counter = info;
  478. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  479. struct perf_counter_context *ctx = counter->ctx;
  480. struct perf_counter *leader = counter->group_leader;
  481. unsigned long flags;
  482. int err;
  483. /*
  484. * If this is a per-task counter, need to check whether this
  485. * counter's task is the current task on this cpu.
  486. */
  487. if (ctx->task && cpuctx->task_ctx != ctx)
  488. return;
  489. curr_rq_lock_irq_save(&flags);
  490. spin_lock(&ctx->lock);
  491. counter->prev_state = counter->state;
  492. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  493. goto unlock;
  494. counter->state = PERF_COUNTER_STATE_INACTIVE;
  495. /*
  496. * If the counter is in a group and isn't the group leader,
  497. * then don't put it on unless the group is on.
  498. */
  499. if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
  500. goto unlock;
  501. if (!group_can_go_on(counter, cpuctx, 1))
  502. err = -EEXIST;
  503. else
  504. err = counter_sched_in(counter, cpuctx, ctx,
  505. smp_processor_id());
  506. if (err) {
  507. /*
  508. * If this counter can't go on and it's part of a
  509. * group, then the whole group has to come off.
  510. */
  511. if (leader != counter)
  512. group_sched_out(leader, cpuctx, ctx);
  513. if (leader->hw_event.pinned)
  514. leader->state = PERF_COUNTER_STATE_ERROR;
  515. }
  516. unlock:
  517. spin_unlock(&ctx->lock);
  518. curr_rq_unlock_irq_restore(&flags);
  519. }
  520. /*
  521. * Enable a counter.
  522. */
  523. static void perf_counter_enable(struct perf_counter *counter)
  524. {
  525. struct perf_counter_context *ctx = counter->ctx;
  526. struct task_struct *task = ctx->task;
  527. if (!task) {
  528. /*
  529. * Enable the counter on the cpu that it's on
  530. */
  531. smp_call_function_single(counter->cpu, __perf_counter_enable,
  532. counter, 1);
  533. return;
  534. }
  535. spin_lock_irq(&ctx->lock);
  536. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  537. goto out;
  538. /*
  539. * If the counter is in error state, clear that first.
  540. * That way, if we see the counter in error state below, we
  541. * know that it has gone back into error state, as distinct
  542. * from the task having been scheduled away before the
  543. * cross-call arrived.
  544. */
  545. if (counter->state == PERF_COUNTER_STATE_ERROR)
  546. counter->state = PERF_COUNTER_STATE_OFF;
  547. retry:
  548. spin_unlock_irq(&ctx->lock);
  549. task_oncpu_function_call(task, __perf_counter_enable, counter);
  550. spin_lock_irq(&ctx->lock);
  551. /*
  552. * If the context is active and the counter is still off,
  553. * we need to retry the cross-call.
  554. */
  555. if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
  556. goto retry;
  557. /*
  558. * Since we have the lock this context can't be scheduled
  559. * in, so we can change the state safely.
  560. */
  561. if (counter->state == PERF_COUNTER_STATE_OFF)
  562. counter->state = PERF_COUNTER_STATE_INACTIVE;
  563. out:
  564. spin_unlock_irq(&ctx->lock);
  565. }
  566. /*
  567. * Enable a counter and all its children.
  568. */
  569. static void perf_counter_enable_family(struct perf_counter *counter)
  570. {
  571. struct perf_counter *child;
  572. perf_counter_enable(counter);
  573. /*
  574. * Lock the mutex to protect the list of children
  575. */
  576. mutex_lock(&counter->mutex);
  577. list_for_each_entry(child, &counter->child_list, child_list)
  578. perf_counter_enable(child);
  579. mutex_unlock(&counter->mutex);
  580. }
  581. void __perf_counter_sched_out(struct perf_counter_context *ctx,
  582. struct perf_cpu_context *cpuctx)
  583. {
  584. struct perf_counter *counter;
  585. u64 flags;
  586. spin_lock(&ctx->lock);
  587. ctx->is_active = 0;
  588. if (likely(!ctx->nr_counters))
  589. goto out;
  590. flags = hw_perf_save_disable();
  591. if (ctx->nr_active) {
  592. list_for_each_entry(counter, &ctx->counter_list, list_entry)
  593. group_sched_out(counter, cpuctx, ctx);
  594. }
  595. hw_perf_restore(flags);
  596. out:
  597. spin_unlock(&ctx->lock);
  598. }
  599. /*
  600. * Called from scheduler to remove the counters of the current task,
  601. * with interrupts disabled.
  602. *
  603. * We stop each counter and update the counter value in counter->count.
  604. *
  605. * This does not protect us against NMI, but disable()
  606. * sets the disabled bit in the control field of counter _before_
  607. * accessing the counter control register. If a NMI hits, then it will
  608. * not restart the counter.
  609. */
  610. void perf_counter_task_sched_out(struct task_struct *task, int cpu)
  611. {
  612. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  613. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  614. struct pt_regs *regs;
  615. if (likely(!cpuctx->task_ctx))
  616. return;
  617. regs = task_pt_regs(task);
  618. perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs);
  619. __perf_counter_sched_out(ctx, cpuctx);
  620. cpuctx->task_ctx = NULL;
  621. }
  622. static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
  623. {
  624. __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
  625. }
  626. static int
  627. group_sched_in(struct perf_counter *group_counter,
  628. struct perf_cpu_context *cpuctx,
  629. struct perf_counter_context *ctx,
  630. int cpu)
  631. {
  632. struct perf_counter *counter, *partial_group;
  633. int ret;
  634. if (group_counter->state == PERF_COUNTER_STATE_OFF)
  635. return 0;
  636. ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
  637. if (ret)
  638. return ret < 0 ? ret : 0;
  639. group_counter->prev_state = group_counter->state;
  640. if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
  641. return -EAGAIN;
  642. /*
  643. * Schedule in siblings as one group (if any):
  644. */
  645. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  646. counter->prev_state = counter->state;
  647. if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
  648. partial_group = counter;
  649. goto group_error;
  650. }
  651. }
  652. return 0;
  653. group_error:
  654. /*
  655. * Groups can be scheduled in as one unit only, so undo any
  656. * partial group before returning:
  657. */
  658. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  659. if (counter == partial_group)
  660. break;
  661. counter_sched_out(counter, cpuctx, ctx);
  662. }
  663. counter_sched_out(group_counter, cpuctx, ctx);
  664. return -EAGAIN;
  665. }
  666. static void
  667. __perf_counter_sched_in(struct perf_counter_context *ctx,
  668. struct perf_cpu_context *cpuctx, int cpu)
  669. {
  670. struct perf_counter *counter;
  671. u64 flags;
  672. int can_add_hw = 1;
  673. spin_lock(&ctx->lock);
  674. ctx->is_active = 1;
  675. if (likely(!ctx->nr_counters))
  676. goto out;
  677. flags = hw_perf_save_disable();
  678. /*
  679. * First go through the list and put on any pinned groups
  680. * in order to give them the best chance of going on.
  681. */
  682. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  683. if (counter->state <= PERF_COUNTER_STATE_OFF ||
  684. !counter->hw_event.pinned)
  685. continue;
  686. if (counter->cpu != -1 && counter->cpu != cpu)
  687. continue;
  688. if (group_can_go_on(counter, cpuctx, 1))
  689. group_sched_in(counter, cpuctx, ctx, cpu);
  690. /*
  691. * If this pinned group hasn't been scheduled,
  692. * put it in error state.
  693. */
  694. if (counter->state == PERF_COUNTER_STATE_INACTIVE)
  695. counter->state = PERF_COUNTER_STATE_ERROR;
  696. }
  697. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  698. /*
  699. * Ignore counters in OFF or ERROR state, and
  700. * ignore pinned counters since we did them already.
  701. */
  702. if (counter->state <= PERF_COUNTER_STATE_OFF ||
  703. counter->hw_event.pinned)
  704. continue;
  705. /*
  706. * Listen to the 'cpu' scheduling filter constraint
  707. * of counters:
  708. */
  709. if (counter->cpu != -1 && counter->cpu != cpu)
  710. continue;
  711. if (group_can_go_on(counter, cpuctx, can_add_hw)) {
  712. if (group_sched_in(counter, cpuctx, ctx, cpu))
  713. can_add_hw = 0;
  714. }
  715. }
  716. hw_perf_restore(flags);
  717. out:
  718. spin_unlock(&ctx->lock);
  719. }
  720. /*
  721. * Called from scheduler to add the counters of the current task
  722. * with interrupts disabled.
  723. *
  724. * We restore the counter value and then enable it.
  725. *
  726. * This does not protect us against NMI, but enable()
  727. * sets the enabled bit in the control field of counter _before_
  728. * accessing the counter control register. If a NMI hits, then it will
  729. * keep the counter running.
  730. */
  731. void perf_counter_task_sched_in(struct task_struct *task, int cpu)
  732. {
  733. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  734. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  735. __perf_counter_sched_in(ctx, cpuctx, cpu);
  736. cpuctx->task_ctx = ctx;
  737. }
  738. static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
  739. {
  740. struct perf_counter_context *ctx = &cpuctx->ctx;
  741. __perf_counter_sched_in(ctx, cpuctx, cpu);
  742. }
  743. int perf_counter_task_disable(void)
  744. {
  745. struct task_struct *curr = current;
  746. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  747. struct perf_counter *counter;
  748. unsigned long flags;
  749. u64 perf_flags;
  750. int cpu;
  751. if (likely(!ctx->nr_counters))
  752. return 0;
  753. curr_rq_lock_irq_save(&flags);
  754. cpu = smp_processor_id();
  755. /* force the update of the task clock: */
  756. __task_delta_exec(curr, 1);
  757. perf_counter_task_sched_out(curr, cpu);
  758. spin_lock(&ctx->lock);
  759. /*
  760. * Disable all the counters:
  761. */
  762. perf_flags = hw_perf_save_disable();
  763. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  764. if (counter->state != PERF_COUNTER_STATE_ERROR)
  765. counter->state = PERF_COUNTER_STATE_OFF;
  766. }
  767. hw_perf_restore(perf_flags);
  768. spin_unlock(&ctx->lock);
  769. curr_rq_unlock_irq_restore(&flags);
  770. return 0;
  771. }
  772. int perf_counter_task_enable(void)
  773. {
  774. struct task_struct *curr = current;
  775. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  776. struct perf_counter *counter;
  777. unsigned long flags;
  778. u64 perf_flags;
  779. int cpu;
  780. if (likely(!ctx->nr_counters))
  781. return 0;
  782. curr_rq_lock_irq_save(&flags);
  783. cpu = smp_processor_id();
  784. /* force the update of the task clock: */
  785. __task_delta_exec(curr, 1);
  786. perf_counter_task_sched_out(curr, cpu);
  787. spin_lock(&ctx->lock);
  788. /*
  789. * Disable all the counters:
  790. */
  791. perf_flags = hw_perf_save_disable();
  792. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  793. if (counter->state > PERF_COUNTER_STATE_OFF)
  794. continue;
  795. counter->state = PERF_COUNTER_STATE_INACTIVE;
  796. counter->hw_event.disabled = 0;
  797. }
  798. hw_perf_restore(perf_flags);
  799. spin_unlock(&ctx->lock);
  800. perf_counter_task_sched_in(curr, cpu);
  801. curr_rq_unlock_irq_restore(&flags);
  802. return 0;
  803. }
  804. /*
  805. * Round-robin a context's counters:
  806. */
  807. static void rotate_ctx(struct perf_counter_context *ctx)
  808. {
  809. struct perf_counter *counter;
  810. u64 perf_flags;
  811. if (!ctx->nr_counters)
  812. return;
  813. spin_lock(&ctx->lock);
  814. /*
  815. * Rotate the first entry last (works just fine for group counters too):
  816. */
  817. perf_flags = hw_perf_save_disable();
  818. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  819. list_move_tail(&counter->list_entry, &ctx->counter_list);
  820. break;
  821. }
  822. hw_perf_restore(perf_flags);
  823. spin_unlock(&ctx->lock);
  824. }
  825. void perf_counter_task_tick(struct task_struct *curr, int cpu)
  826. {
  827. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  828. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  829. const int rotate_percpu = 0;
  830. if (rotate_percpu)
  831. perf_counter_cpu_sched_out(cpuctx);
  832. perf_counter_task_sched_out(curr, cpu);
  833. if (rotate_percpu)
  834. rotate_ctx(&cpuctx->ctx);
  835. rotate_ctx(ctx);
  836. if (rotate_percpu)
  837. perf_counter_cpu_sched_in(cpuctx, cpu);
  838. perf_counter_task_sched_in(curr, cpu);
  839. }
  840. /*
  841. * Cross CPU call to read the hardware counter
  842. */
  843. static void __read(void *info)
  844. {
  845. struct perf_counter *counter = info;
  846. unsigned long flags;
  847. curr_rq_lock_irq_save(&flags);
  848. counter->hw_ops->read(counter);
  849. curr_rq_unlock_irq_restore(&flags);
  850. }
  851. static u64 perf_counter_read(struct perf_counter *counter)
  852. {
  853. /*
  854. * If counter is enabled and currently active on a CPU, update the
  855. * value in the counter structure:
  856. */
  857. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  858. smp_call_function_single(counter->oncpu,
  859. __read, counter, 1);
  860. }
  861. return atomic64_read(&counter->count);
  862. }
  863. /*
  864. * Cross CPU call to switch performance data pointers
  865. */
  866. static void __perf_switch_irq_data(void *info)
  867. {
  868. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  869. struct perf_counter *counter = info;
  870. struct perf_counter_context *ctx = counter->ctx;
  871. struct perf_data *oldirqdata = counter->irqdata;
  872. /*
  873. * If this is a task context, we need to check whether it is
  874. * the current task context of this cpu. If not it has been
  875. * scheduled out before the smp call arrived.
  876. */
  877. if (ctx->task) {
  878. if (cpuctx->task_ctx != ctx)
  879. return;
  880. spin_lock(&ctx->lock);
  881. }
  882. /* Change the pointer NMI safe */
  883. atomic_long_set((atomic_long_t *)&counter->irqdata,
  884. (unsigned long) counter->usrdata);
  885. counter->usrdata = oldirqdata;
  886. if (ctx->task)
  887. spin_unlock(&ctx->lock);
  888. }
  889. static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
  890. {
  891. struct perf_counter_context *ctx = counter->ctx;
  892. struct perf_data *oldirqdata = counter->irqdata;
  893. struct task_struct *task = ctx->task;
  894. if (!task) {
  895. smp_call_function_single(counter->cpu,
  896. __perf_switch_irq_data,
  897. counter, 1);
  898. return counter->usrdata;
  899. }
  900. retry:
  901. spin_lock_irq(&ctx->lock);
  902. if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
  903. counter->irqdata = counter->usrdata;
  904. counter->usrdata = oldirqdata;
  905. spin_unlock_irq(&ctx->lock);
  906. return oldirqdata;
  907. }
  908. spin_unlock_irq(&ctx->lock);
  909. task_oncpu_function_call(task, __perf_switch_irq_data, counter);
  910. /* Might have failed, because task was scheduled out */
  911. if (counter->irqdata == oldirqdata)
  912. goto retry;
  913. return counter->usrdata;
  914. }
  915. static void put_context(struct perf_counter_context *ctx)
  916. {
  917. if (ctx->task)
  918. put_task_struct(ctx->task);
  919. }
  920. static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
  921. {
  922. struct perf_cpu_context *cpuctx;
  923. struct perf_counter_context *ctx;
  924. struct task_struct *task;
  925. /*
  926. * If cpu is not a wildcard then this is a percpu counter:
  927. */
  928. if (cpu != -1) {
  929. /* Must be root to operate on a CPU counter: */
  930. if (!capable(CAP_SYS_ADMIN))
  931. return ERR_PTR(-EACCES);
  932. if (cpu < 0 || cpu > num_possible_cpus())
  933. return ERR_PTR(-EINVAL);
  934. /*
  935. * We could be clever and allow to attach a counter to an
  936. * offline CPU and activate it when the CPU comes up, but
  937. * that's for later.
  938. */
  939. if (!cpu_isset(cpu, cpu_online_map))
  940. return ERR_PTR(-ENODEV);
  941. cpuctx = &per_cpu(perf_cpu_context, cpu);
  942. ctx = &cpuctx->ctx;
  943. return ctx;
  944. }
  945. rcu_read_lock();
  946. if (!pid)
  947. task = current;
  948. else
  949. task = find_task_by_vpid(pid);
  950. if (task)
  951. get_task_struct(task);
  952. rcu_read_unlock();
  953. if (!task)
  954. return ERR_PTR(-ESRCH);
  955. ctx = &task->perf_counter_ctx;
  956. ctx->task = task;
  957. /* Reuse ptrace permission checks for now. */
  958. if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
  959. put_context(ctx);
  960. return ERR_PTR(-EACCES);
  961. }
  962. return ctx;
  963. }
  964. static void free_counter_rcu(struct rcu_head *head)
  965. {
  966. struct perf_counter *counter;
  967. counter = container_of(head, struct perf_counter, rcu_head);
  968. kfree(counter);
  969. }
  970. /*
  971. * Called when the last reference to the file is gone.
  972. */
  973. static int perf_release(struct inode *inode, struct file *file)
  974. {
  975. struct perf_counter *counter = file->private_data;
  976. struct perf_counter_context *ctx = counter->ctx;
  977. file->private_data = NULL;
  978. mutex_lock(&ctx->mutex);
  979. mutex_lock(&counter->mutex);
  980. perf_counter_remove_from_context(counter);
  981. mutex_unlock(&counter->mutex);
  982. mutex_unlock(&ctx->mutex);
  983. call_rcu(&counter->rcu_head, free_counter_rcu);
  984. put_context(ctx);
  985. return 0;
  986. }
  987. /*
  988. * Read the performance counter - simple non blocking version for now
  989. */
  990. static ssize_t
  991. perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
  992. {
  993. u64 cntval;
  994. if (count != sizeof(cntval))
  995. return -EINVAL;
  996. /*
  997. * Return end-of-file for a read on a counter that is in
  998. * error state (i.e. because it was pinned but it couldn't be
  999. * scheduled on to the CPU at some point).
  1000. */
  1001. if (counter->state == PERF_COUNTER_STATE_ERROR)
  1002. return 0;
  1003. mutex_lock(&counter->mutex);
  1004. cntval = perf_counter_read(counter);
  1005. mutex_unlock(&counter->mutex);
  1006. return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
  1007. }
  1008. static ssize_t
  1009. perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
  1010. {
  1011. if (!usrdata->len)
  1012. return 0;
  1013. count = min(count, (size_t)usrdata->len);
  1014. if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
  1015. return -EFAULT;
  1016. /* Adjust the counters */
  1017. usrdata->len -= count;
  1018. if (!usrdata->len)
  1019. usrdata->rd_idx = 0;
  1020. else
  1021. usrdata->rd_idx += count;
  1022. return count;
  1023. }
  1024. static ssize_t
  1025. perf_read_irq_data(struct perf_counter *counter,
  1026. char __user *buf,
  1027. size_t count,
  1028. int nonblocking)
  1029. {
  1030. struct perf_data *irqdata, *usrdata;
  1031. DECLARE_WAITQUEUE(wait, current);
  1032. ssize_t res, res2;
  1033. irqdata = counter->irqdata;
  1034. usrdata = counter->usrdata;
  1035. if (usrdata->len + irqdata->len >= count)
  1036. goto read_pending;
  1037. if (nonblocking)
  1038. return -EAGAIN;
  1039. spin_lock_irq(&counter->waitq.lock);
  1040. __add_wait_queue(&counter->waitq, &wait);
  1041. for (;;) {
  1042. set_current_state(TASK_INTERRUPTIBLE);
  1043. if (usrdata->len + irqdata->len >= count)
  1044. break;
  1045. if (signal_pending(current))
  1046. break;
  1047. if (counter->state == PERF_COUNTER_STATE_ERROR)
  1048. break;
  1049. spin_unlock_irq(&counter->waitq.lock);
  1050. schedule();
  1051. spin_lock_irq(&counter->waitq.lock);
  1052. }
  1053. __remove_wait_queue(&counter->waitq, &wait);
  1054. __set_current_state(TASK_RUNNING);
  1055. spin_unlock_irq(&counter->waitq.lock);
  1056. if (usrdata->len + irqdata->len < count &&
  1057. counter->state != PERF_COUNTER_STATE_ERROR)
  1058. return -ERESTARTSYS;
  1059. read_pending:
  1060. mutex_lock(&counter->mutex);
  1061. /* Drain pending data first: */
  1062. res = perf_copy_usrdata(usrdata, buf, count);
  1063. if (res < 0 || res == count)
  1064. goto out;
  1065. /* Switch irq buffer: */
  1066. usrdata = perf_switch_irq_data(counter);
  1067. res2 = perf_copy_usrdata(usrdata, buf + res, count - res);
  1068. if (res2 < 0) {
  1069. if (!res)
  1070. res = -EFAULT;
  1071. } else {
  1072. res += res2;
  1073. }
  1074. out:
  1075. mutex_unlock(&counter->mutex);
  1076. return res;
  1077. }
  1078. static ssize_t
  1079. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  1080. {
  1081. struct perf_counter *counter = file->private_data;
  1082. switch (counter->hw_event.record_type) {
  1083. case PERF_RECORD_SIMPLE:
  1084. return perf_read_hw(counter, buf, count);
  1085. case PERF_RECORD_IRQ:
  1086. case PERF_RECORD_GROUP:
  1087. return perf_read_irq_data(counter, buf, count,
  1088. file->f_flags & O_NONBLOCK);
  1089. }
  1090. return -EINVAL;
  1091. }
  1092. static unsigned int perf_poll(struct file *file, poll_table *wait)
  1093. {
  1094. struct perf_counter *counter = file->private_data;
  1095. unsigned int events = 0;
  1096. unsigned long flags;
  1097. poll_wait(file, &counter->waitq, wait);
  1098. spin_lock_irqsave(&counter->waitq.lock, flags);
  1099. if (counter->usrdata->len || counter->irqdata->len)
  1100. events |= POLLIN;
  1101. spin_unlock_irqrestore(&counter->waitq.lock, flags);
  1102. return events;
  1103. }
  1104. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1105. {
  1106. struct perf_counter *counter = file->private_data;
  1107. int err = 0;
  1108. switch (cmd) {
  1109. case PERF_COUNTER_IOC_ENABLE:
  1110. perf_counter_enable_family(counter);
  1111. break;
  1112. case PERF_COUNTER_IOC_DISABLE:
  1113. perf_counter_disable_family(counter);
  1114. break;
  1115. default:
  1116. err = -ENOTTY;
  1117. }
  1118. return err;
  1119. }
  1120. static const struct file_operations perf_fops = {
  1121. .release = perf_release,
  1122. .read = perf_read,
  1123. .poll = perf_poll,
  1124. .unlocked_ioctl = perf_ioctl,
  1125. .compat_ioctl = perf_ioctl,
  1126. };
  1127. /*
  1128. * Generic software counter infrastructure
  1129. */
  1130. static void perf_swcounter_update(struct perf_counter *counter)
  1131. {
  1132. struct hw_perf_counter *hwc = &counter->hw;
  1133. u64 prev, now;
  1134. s64 delta;
  1135. again:
  1136. prev = atomic64_read(&hwc->prev_count);
  1137. now = atomic64_read(&hwc->count);
  1138. if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
  1139. goto again;
  1140. delta = now - prev;
  1141. atomic64_add(delta, &counter->count);
  1142. atomic64_sub(delta, &hwc->period_left);
  1143. }
  1144. static void perf_swcounter_set_period(struct perf_counter *counter)
  1145. {
  1146. struct hw_perf_counter *hwc = &counter->hw;
  1147. s64 left = atomic64_read(&hwc->period_left);
  1148. s64 period = hwc->irq_period;
  1149. if (unlikely(left <= -period)) {
  1150. left = period;
  1151. atomic64_set(&hwc->period_left, left);
  1152. }
  1153. if (unlikely(left <= 0)) {
  1154. left += period;
  1155. atomic64_add(period, &hwc->period_left);
  1156. }
  1157. atomic64_set(&hwc->prev_count, -left);
  1158. atomic64_set(&hwc->count, -left);
  1159. }
  1160. static void perf_swcounter_save_and_restart(struct perf_counter *counter)
  1161. {
  1162. perf_swcounter_update(counter);
  1163. perf_swcounter_set_period(counter);
  1164. }
  1165. static void perf_swcounter_store_irq(struct perf_counter *counter, u64 data)
  1166. {
  1167. struct perf_data *irqdata = counter->irqdata;
  1168. if (irqdata->len > PERF_DATA_BUFLEN - sizeof(u64)) {
  1169. irqdata->overrun++;
  1170. } else {
  1171. u64 *p = (u64 *) &irqdata->data[irqdata->len];
  1172. *p = data;
  1173. irqdata->len += sizeof(u64);
  1174. }
  1175. }
  1176. static void perf_swcounter_handle_group(struct perf_counter *sibling)
  1177. {
  1178. struct perf_counter *counter, *group_leader = sibling->group_leader;
  1179. list_for_each_entry(counter, &group_leader->sibling_list, list_entry) {
  1180. counter->hw_ops->read(counter);
  1181. perf_swcounter_store_irq(sibling, counter->hw_event.type);
  1182. perf_swcounter_store_irq(sibling, atomic64_read(&counter->count));
  1183. }
  1184. }
  1185. static void perf_swcounter_interrupt(struct perf_counter *counter,
  1186. int nmi, struct pt_regs *regs)
  1187. {
  1188. switch (counter->hw_event.record_type) {
  1189. case PERF_RECORD_SIMPLE:
  1190. break;
  1191. case PERF_RECORD_IRQ:
  1192. perf_swcounter_store_irq(counter, instruction_pointer(regs));
  1193. break;
  1194. case PERF_RECORD_GROUP:
  1195. perf_swcounter_handle_group(counter);
  1196. break;
  1197. }
  1198. if (nmi) {
  1199. counter->wakeup_pending = 1;
  1200. set_perf_counter_pending();
  1201. } else
  1202. wake_up(&counter->waitq);
  1203. }
  1204. static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
  1205. {
  1206. struct perf_counter *counter;
  1207. struct pt_regs *regs;
  1208. counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
  1209. counter->hw_ops->read(counter);
  1210. regs = get_irq_regs();
  1211. /*
  1212. * In case we exclude kernel IPs or are somehow not in interrupt
  1213. * context, provide the next best thing, the user IP.
  1214. */
  1215. if ((counter->hw_event.exclude_kernel || !regs) &&
  1216. !counter->hw_event.exclude_user)
  1217. regs = task_pt_regs(current);
  1218. if (regs)
  1219. perf_swcounter_interrupt(counter, 0, regs);
  1220. hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
  1221. return HRTIMER_RESTART;
  1222. }
  1223. static void perf_swcounter_overflow(struct perf_counter *counter,
  1224. int nmi, struct pt_regs *regs)
  1225. {
  1226. perf_swcounter_save_and_restart(counter);
  1227. perf_swcounter_interrupt(counter, nmi, regs);
  1228. }
  1229. static int perf_swcounter_match(struct perf_counter *counter,
  1230. enum hw_event_types event,
  1231. struct pt_regs *regs)
  1232. {
  1233. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  1234. return 0;
  1235. if (counter->hw_event.raw)
  1236. return 0;
  1237. if (counter->hw_event.type != event)
  1238. return 0;
  1239. if (counter->hw_event.exclude_user && user_mode(regs))
  1240. return 0;
  1241. if (counter->hw_event.exclude_kernel && !user_mode(regs))
  1242. return 0;
  1243. return 1;
  1244. }
  1245. static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
  1246. int nmi, struct pt_regs *regs)
  1247. {
  1248. int neg = atomic64_add_negative(nr, &counter->hw.count);
  1249. if (counter->hw.irq_period && !neg)
  1250. perf_swcounter_overflow(counter, nmi, regs);
  1251. }
  1252. static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
  1253. enum hw_event_types event, u64 nr,
  1254. int nmi, struct pt_regs *regs)
  1255. {
  1256. struct perf_counter *counter;
  1257. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  1258. return;
  1259. rcu_read_lock();
  1260. list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
  1261. if (perf_swcounter_match(counter, event, regs))
  1262. perf_swcounter_add(counter, nr, nmi, regs);
  1263. }
  1264. rcu_read_unlock();
  1265. }
  1266. void perf_swcounter_event(enum hw_event_types event, u64 nr,
  1267. int nmi, struct pt_regs *regs)
  1268. {
  1269. struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
  1270. perf_swcounter_ctx_event(&cpuctx->ctx, event, nr, nmi, regs);
  1271. if (cpuctx->task_ctx)
  1272. perf_swcounter_ctx_event(cpuctx->task_ctx, event, nr, nmi, regs);
  1273. put_cpu_var(perf_cpu_context);
  1274. }
  1275. static void perf_swcounter_read(struct perf_counter *counter)
  1276. {
  1277. perf_swcounter_update(counter);
  1278. }
  1279. static int perf_swcounter_enable(struct perf_counter *counter)
  1280. {
  1281. perf_swcounter_set_period(counter);
  1282. return 0;
  1283. }
  1284. static void perf_swcounter_disable(struct perf_counter *counter)
  1285. {
  1286. perf_swcounter_update(counter);
  1287. }
  1288. static const struct hw_perf_counter_ops perf_ops_generic = {
  1289. .enable = perf_swcounter_enable,
  1290. .disable = perf_swcounter_disable,
  1291. .read = perf_swcounter_read,
  1292. };
  1293. /*
  1294. * Software counter: cpu wall time clock
  1295. */
  1296. static void cpu_clock_perf_counter_update(struct perf_counter *counter)
  1297. {
  1298. int cpu = raw_smp_processor_id();
  1299. s64 prev;
  1300. u64 now;
  1301. now = cpu_clock(cpu);
  1302. prev = atomic64_read(&counter->hw.prev_count);
  1303. atomic64_set(&counter->hw.prev_count, now);
  1304. atomic64_add(now - prev, &counter->count);
  1305. }
  1306. static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
  1307. {
  1308. struct hw_perf_counter *hwc = &counter->hw;
  1309. int cpu = raw_smp_processor_id();
  1310. atomic64_set(&hwc->prev_count, cpu_clock(cpu));
  1311. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1312. hwc->hrtimer.function = perf_swcounter_hrtimer;
  1313. if (hwc->irq_period) {
  1314. __hrtimer_start_range_ns(&hwc->hrtimer,
  1315. ns_to_ktime(hwc->irq_period), 0,
  1316. HRTIMER_MODE_REL, 0);
  1317. }
  1318. return 0;
  1319. }
  1320. static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
  1321. {
  1322. hrtimer_cancel(&counter->hw.hrtimer);
  1323. cpu_clock_perf_counter_update(counter);
  1324. }
  1325. static void cpu_clock_perf_counter_read(struct perf_counter *counter)
  1326. {
  1327. cpu_clock_perf_counter_update(counter);
  1328. }
  1329. static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
  1330. .enable = cpu_clock_perf_counter_enable,
  1331. .disable = cpu_clock_perf_counter_disable,
  1332. .read = cpu_clock_perf_counter_read,
  1333. };
  1334. /*
  1335. * Software counter: task time clock
  1336. */
  1337. /*
  1338. * Called from within the scheduler:
  1339. */
  1340. static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
  1341. {
  1342. struct task_struct *curr = counter->task;
  1343. u64 delta;
  1344. delta = __task_delta_exec(curr, update);
  1345. return curr->se.sum_exec_runtime + delta;
  1346. }
  1347. static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
  1348. {
  1349. u64 prev;
  1350. s64 delta;
  1351. prev = atomic64_read(&counter->hw.prev_count);
  1352. atomic64_set(&counter->hw.prev_count, now);
  1353. delta = now - prev;
  1354. atomic64_add(delta, &counter->count);
  1355. }
  1356. static int task_clock_perf_counter_enable(struct perf_counter *counter)
  1357. {
  1358. struct hw_perf_counter *hwc = &counter->hw;
  1359. atomic64_set(&hwc->prev_count, task_clock_perf_counter_val(counter, 0));
  1360. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1361. hwc->hrtimer.function = perf_swcounter_hrtimer;
  1362. if (hwc->irq_period) {
  1363. __hrtimer_start_range_ns(&hwc->hrtimer,
  1364. ns_to_ktime(hwc->irq_period), 0,
  1365. HRTIMER_MODE_REL, 0);
  1366. }
  1367. return 0;
  1368. }
  1369. static void task_clock_perf_counter_disable(struct perf_counter *counter)
  1370. {
  1371. hrtimer_cancel(&counter->hw.hrtimer);
  1372. task_clock_perf_counter_update(counter,
  1373. task_clock_perf_counter_val(counter, 0));
  1374. }
  1375. static void task_clock_perf_counter_read(struct perf_counter *counter)
  1376. {
  1377. task_clock_perf_counter_update(counter,
  1378. task_clock_perf_counter_val(counter, 1));
  1379. }
  1380. static const struct hw_perf_counter_ops perf_ops_task_clock = {
  1381. .enable = task_clock_perf_counter_enable,
  1382. .disable = task_clock_perf_counter_disable,
  1383. .read = task_clock_perf_counter_read,
  1384. };
  1385. /*
  1386. * Software counter: cpu migrations
  1387. */
  1388. static inline u64 get_cpu_migrations(struct perf_counter *counter)
  1389. {
  1390. struct task_struct *curr = counter->ctx->task;
  1391. if (curr)
  1392. return curr->se.nr_migrations;
  1393. return cpu_nr_migrations(smp_processor_id());
  1394. }
  1395. static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
  1396. {
  1397. u64 prev, now;
  1398. s64 delta;
  1399. prev = atomic64_read(&counter->hw.prev_count);
  1400. now = get_cpu_migrations(counter);
  1401. atomic64_set(&counter->hw.prev_count, now);
  1402. delta = now - prev;
  1403. atomic64_add(delta, &counter->count);
  1404. }
  1405. static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
  1406. {
  1407. cpu_migrations_perf_counter_update(counter);
  1408. }
  1409. static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
  1410. {
  1411. if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
  1412. atomic64_set(&counter->hw.prev_count,
  1413. get_cpu_migrations(counter));
  1414. return 0;
  1415. }
  1416. static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
  1417. {
  1418. cpu_migrations_perf_counter_update(counter);
  1419. }
  1420. static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
  1421. .enable = cpu_migrations_perf_counter_enable,
  1422. .disable = cpu_migrations_perf_counter_disable,
  1423. .read = cpu_migrations_perf_counter_read,
  1424. };
  1425. static const struct hw_perf_counter_ops *
  1426. sw_perf_counter_init(struct perf_counter *counter)
  1427. {
  1428. struct perf_counter_hw_event *hw_event = &counter->hw_event;
  1429. const struct hw_perf_counter_ops *hw_ops = NULL;
  1430. struct hw_perf_counter *hwc = &counter->hw;
  1431. /*
  1432. * Software counters (currently) can't in general distinguish
  1433. * between user, kernel and hypervisor events.
  1434. * However, context switches and cpu migrations are considered
  1435. * to be kernel events, and page faults are never hypervisor
  1436. * events.
  1437. */
  1438. switch (counter->hw_event.type) {
  1439. case PERF_COUNT_CPU_CLOCK:
  1440. hw_ops = &perf_ops_cpu_clock;
  1441. if (hw_event->irq_period && hw_event->irq_period < 10000)
  1442. hw_event->irq_period = 10000;
  1443. break;
  1444. case PERF_COUNT_TASK_CLOCK:
  1445. /*
  1446. * If the user instantiates this as a per-cpu counter,
  1447. * use the cpu_clock counter instead.
  1448. */
  1449. if (counter->ctx->task)
  1450. hw_ops = &perf_ops_task_clock;
  1451. else
  1452. hw_ops = &perf_ops_cpu_clock;
  1453. if (hw_event->irq_period && hw_event->irq_period < 10000)
  1454. hw_event->irq_period = 10000;
  1455. break;
  1456. case PERF_COUNT_PAGE_FAULTS:
  1457. case PERF_COUNT_PAGE_FAULTS_MIN:
  1458. case PERF_COUNT_PAGE_FAULTS_MAJ:
  1459. case PERF_COUNT_CONTEXT_SWITCHES:
  1460. hw_ops = &perf_ops_generic;
  1461. break;
  1462. case PERF_COUNT_CPU_MIGRATIONS:
  1463. if (!counter->hw_event.exclude_kernel)
  1464. hw_ops = &perf_ops_cpu_migrations;
  1465. break;
  1466. default:
  1467. break;
  1468. }
  1469. if (hw_ops)
  1470. hwc->irq_period = hw_event->irq_period;
  1471. return hw_ops;
  1472. }
  1473. /*
  1474. * Allocate and initialize a counter structure
  1475. */
  1476. static struct perf_counter *
  1477. perf_counter_alloc(struct perf_counter_hw_event *hw_event,
  1478. int cpu,
  1479. struct perf_counter_context *ctx,
  1480. struct perf_counter *group_leader,
  1481. gfp_t gfpflags)
  1482. {
  1483. const struct hw_perf_counter_ops *hw_ops;
  1484. struct perf_counter *counter;
  1485. counter = kzalloc(sizeof(*counter), gfpflags);
  1486. if (!counter)
  1487. return NULL;
  1488. /*
  1489. * Single counters are their own group leaders, with an
  1490. * empty sibling list:
  1491. */
  1492. if (!group_leader)
  1493. group_leader = counter;
  1494. mutex_init(&counter->mutex);
  1495. INIT_LIST_HEAD(&counter->list_entry);
  1496. INIT_LIST_HEAD(&counter->event_entry);
  1497. INIT_LIST_HEAD(&counter->sibling_list);
  1498. init_waitqueue_head(&counter->waitq);
  1499. INIT_LIST_HEAD(&counter->child_list);
  1500. counter->irqdata = &counter->data[0];
  1501. counter->usrdata = &counter->data[1];
  1502. counter->cpu = cpu;
  1503. counter->hw_event = *hw_event;
  1504. counter->wakeup_pending = 0;
  1505. counter->group_leader = group_leader;
  1506. counter->hw_ops = NULL;
  1507. counter->ctx = ctx;
  1508. counter->state = PERF_COUNTER_STATE_INACTIVE;
  1509. if (hw_event->disabled)
  1510. counter->state = PERF_COUNTER_STATE_OFF;
  1511. hw_ops = NULL;
  1512. if (!hw_event->raw && hw_event->type < 0)
  1513. hw_ops = sw_perf_counter_init(counter);
  1514. else
  1515. hw_ops = hw_perf_counter_init(counter);
  1516. if (!hw_ops) {
  1517. kfree(counter);
  1518. return NULL;
  1519. }
  1520. counter->hw_ops = hw_ops;
  1521. return counter;
  1522. }
  1523. /**
  1524. * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
  1525. *
  1526. * @hw_event_uptr: event type attributes for monitoring/sampling
  1527. * @pid: target pid
  1528. * @cpu: target cpu
  1529. * @group_fd: group leader counter fd
  1530. */
  1531. SYSCALL_DEFINE5(perf_counter_open,
  1532. const struct perf_counter_hw_event __user *, hw_event_uptr,
  1533. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  1534. {
  1535. struct perf_counter *counter, *group_leader;
  1536. struct perf_counter_hw_event hw_event;
  1537. struct perf_counter_context *ctx;
  1538. struct file *counter_file = NULL;
  1539. struct file *group_file = NULL;
  1540. int fput_needed = 0;
  1541. int fput_needed2 = 0;
  1542. int ret;
  1543. /* for future expandability... */
  1544. if (flags)
  1545. return -EINVAL;
  1546. if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
  1547. return -EFAULT;
  1548. /*
  1549. * Get the target context (task or percpu):
  1550. */
  1551. ctx = find_get_context(pid, cpu);
  1552. if (IS_ERR(ctx))
  1553. return PTR_ERR(ctx);
  1554. /*
  1555. * Look up the group leader (we will attach this counter to it):
  1556. */
  1557. group_leader = NULL;
  1558. if (group_fd != -1) {
  1559. ret = -EINVAL;
  1560. group_file = fget_light(group_fd, &fput_needed);
  1561. if (!group_file)
  1562. goto err_put_context;
  1563. if (group_file->f_op != &perf_fops)
  1564. goto err_put_context;
  1565. group_leader = group_file->private_data;
  1566. /*
  1567. * Do not allow a recursive hierarchy (this new sibling
  1568. * becoming part of another group-sibling):
  1569. */
  1570. if (group_leader->group_leader != group_leader)
  1571. goto err_put_context;
  1572. /*
  1573. * Do not allow to attach to a group in a different
  1574. * task or CPU context:
  1575. */
  1576. if (group_leader->ctx != ctx)
  1577. goto err_put_context;
  1578. /*
  1579. * Only a group leader can be exclusive or pinned
  1580. */
  1581. if (hw_event.exclusive || hw_event.pinned)
  1582. goto err_put_context;
  1583. }
  1584. ret = -EINVAL;
  1585. counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
  1586. GFP_KERNEL);
  1587. if (!counter)
  1588. goto err_put_context;
  1589. ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
  1590. if (ret < 0)
  1591. goto err_free_put_context;
  1592. counter_file = fget_light(ret, &fput_needed2);
  1593. if (!counter_file)
  1594. goto err_free_put_context;
  1595. counter->filp = counter_file;
  1596. mutex_lock(&ctx->mutex);
  1597. perf_install_in_context(ctx, counter, cpu);
  1598. mutex_unlock(&ctx->mutex);
  1599. fput_light(counter_file, fput_needed2);
  1600. out_fput:
  1601. fput_light(group_file, fput_needed);
  1602. return ret;
  1603. err_free_put_context:
  1604. kfree(counter);
  1605. err_put_context:
  1606. put_context(ctx);
  1607. goto out_fput;
  1608. }
  1609. /*
  1610. * Initialize the perf_counter context in a task_struct:
  1611. */
  1612. static void
  1613. __perf_counter_init_context(struct perf_counter_context *ctx,
  1614. struct task_struct *task)
  1615. {
  1616. memset(ctx, 0, sizeof(*ctx));
  1617. spin_lock_init(&ctx->lock);
  1618. mutex_init(&ctx->mutex);
  1619. INIT_LIST_HEAD(&ctx->counter_list);
  1620. INIT_LIST_HEAD(&ctx->event_list);
  1621. ctx->task = task;
  1622. }
  1623. /*
  1624. * inherit a counter from parent task to child task:
  1625. */
  1626. static struct perf_counter *
  1627. inherit_counter(struct perf_counter *parent_counter,
  1628. struct task_struct *parent,
  1629. struct perf_counter_context *parent_ctx,
  1630. struct task_struct *child,
  1631. struct perf_counter *group_leader,
  1632. struct perf_counter_context *child_ctx)
  1633. {
  1634. struct perf_counter *child_counter;
  1635. /*
  1636. * Instead of creating recursive hierarchies of counters,
  1637. * we link inherited counters back to the original parent,
  1638. * which has a filp for sure, which we use as the reference
  1639. * count:
  1640. */
  1641. if (parent_counter->parent)
  1642. parent_counter = parent_counter->parent;
  1643. child_counter = perf_counter_alloc(&parent_counter->hw_event,
  1644. parent_counter->cpu, child_ctx,
  1645. group_leader, GFP_KERNEL);
  1646. if (!child_counter)
  1647. return NULL;
  1648. /*
  1649. * Link it up in the child's context:
  1650. */
  1651. child_counter->task = child;
  1652. list_add_counter(child_counter, child_ctx);
  1653. child_ctx->nr_counters++;
  1654. child_counter->parent = parent_counter;
  1655. /*
  1656. * inherit into child's child as well:
  1657. */
  1658. child_counter->hw_event.inherit = 1;
  1659. /*
  1660. * Get a reference to the parent filp - we will fput it
  1661. * when the child counter exits. This is safe to do because
  1662. * we are in the parent and we know that the filp still
  1663. * exists and has a nonzero count:
  1664. */
  1665. atomic_long_inc(&parent_counter->filp->f_count);
  1666. /*
  1667. * Link this into the parent counter's child list
  1668. */
  1669. mutex_lock(&parent_counter->mutex);
  1670. list_add_tail(&child_counter->child_list, &parent_counter->child_list);
  1671. /*
  1672. * Make the child state follow the state of the parent counter,
  1673. * not its hw_event.disabled bit. We hold the parent's mutex,
  1674. * so we won't race with perf_counter_{en,dis}able_family.
  1675. */
  1676. if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
  1677. child_counter->state = PERF_COUNTER_STATE_INACTIVE;
  1678. else
  1679. child_counter->state = PERF_COUNTER_STATE_OFF;
  1680. mutex_unlock(&parent_counter->mutex);
  1681. return child_counter;
  1682. }
  1683. static int inherit_group(struct perf_counter *parent_counter,
  1684. struct task_struct *parent,
  1685. struct perf_counter_context *parent_ctx,
  1686. struct task_struct *child,
  1687. struct perf_counter_context *child_ctx)
  1688. {
  1689. struct perf_counter *leader;
  1690. struct perf_counter *sub;
  1691. leader = inherit_counter(parent_counter, parent, parent_ctx,
  1692. child, NULL, child_ctx);
  1693. if (!leader)
  1694. return -ENOMEM;
  1695. list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
  1696. if (!inherit_counter(sub, parent, parent_ctx,
  1697. child, leader, child_ctx))
  1698. return -ENOMEM;
  1699. }
  1700. return 0;
  1701. }
  1702. static void sync_child_counter(struct perf_counter *child_counter,
  1703. struct perf_counter *parent_counter)
  1704. {
  1705. u64 parent_val, child_val;
  1706. parent_val = atomic64_read(&parent_counter->count);
  1707. child_val = atomic64_read(&child_counter->count);
  1708. /*
  1709. * Add back the child's count to the parent's count:
  1710. */
  1711. atomic64_add(child_val, &parent_counter->count);
  1712. /*
  1713. * Remove this counter from the parent's list
  1714. */
  1715. mutex_lock(&parent_counter->mutex);
  1716. list_del_init(&child_counter->child_list);
  1717. mutex_unlock(&parent_counter->mutex);
  1718. /*
  1719. * Release the parent counter, if this was the last
  1720. * reference to it.
  1721. */
  1722. fput(parent_counter->filp);
  1723. }
  1724. static void
  1725. __perf_counter_exit_task(struct task_struct *child,
  1726. struct perf_counter *child_counter,
  1727. struct perf_counter_context *child_ctx)
  1728. {
  1729. struct perf_counter *parent_counter;
  1730. struct perf_counter *sub, *tmp;
  1731. /*
  1732. * If we do not self-reap then we have to wait for the
  1733. * child task to unschedule (it will happen for sure),
  1734. * so that its counter is at its final count. (This
  1735. * condition triggers rarely - child tasks usually get
  1736. * off their CPU before the parent has a chance to
  1737. * get this far into the reaping action)
  1738. */
  1739. if (child != current) {
  1740. wait_task_inactive(child, 0);
  1741. list_del_init(&child_counter->list_entry);
  1742. } else {
  1743. struct perf_cpu_context *cpuctx;
  1744. unsigned long flags;
  1745. u64 perf_flags;
  1746. /*
  1747. * Disable and unlink this counter.
  1748. *
  1749. * Be careful about zapping the list - IRQ/NMI context
  1750. * could still be processing it:
  1751. */
  1752. curr_rq_lock_irq_save(&flags);
  1753. perf_flags = hw_perf_save_disable();
  1754. cpuctx = &__get_cpu_var(perf_cpu_context);
  1755. group_sched_out(child_counter, cpuctx, child_ctx);
  1756. list_del_init(&child_counter->list_entry);
  1757. child_ctx->nr_counters--;
  1758. hw_perf_restore(perf_flags);
  1759. curr_rq_unlock_irq_restore(&flags);
  1760. }
  1761. parent_counter = child_counter->parent;
  1762. /*
  1763. * It can happen that parent exits first, and has counters
  1764. * that are still around due to the child reference. These
  1765. * counters need to be zapped - but otherwise linger.
  1766. */
  1767. if (parent_counter) {
  1768. sync_child_counter(child_counter, parent_counter);
  1769. list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
  1770. list_entry) {
  1771. if (sub->parent) {
  1772. sync_child_counter(sub, sub->parent);
  1773. kfree(sub);
  1774. }
  1775. }
  1776. kfree(child_counter);
  1777. }
  1778. }
  1779. /*
  1780. * When a child task exits, feed back counter values to parent counters.
  1781. *
  1782. * Note: we may be running in child context, but the PID is not hashed
  1783. * anymore so new counters will not be added.
  1784. */
  1785. void perf_counter_exit_task(struct task_struct *child)
  1786. {
  1787. struct perf_counter *child_counter, *tmp;
  1788. struct perf_counter_context *child_ctx;
  1789. child_ctx = &child->perf_counter_ctx;
  1790. if (likely(!child_ctx->nr_counters))
  1791. return;
  1792. list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
  1793. list_entry)
  1794. __perf_counter_exit_task(child, child_counter, child_ctx);
  1795. }
  1796. /*
  1797. * Initialize the perf_counter context in task_struct
  1798. */
  1799. void perf_counter_init_task(struct task_struct *child)
  1800. {
  1801. struct perf_counter_context *child_ctx, *parent_ctx;
  1802. struct perf_counter *counter;
  1803. struct task_struct *parent = current;
  1804. child_ctx = &child->perf_counter_ctx;
  1805. parent_ctx = &parent->perf_counter_ctx;
  1806. __perf_counter_init_context(child_ctx, child);
  1807. /*
  1808. * This is executed from the parent task context, so inherit
  1809. * counters that have been marked for cloning:
  1810. */
  1811. if (likely(!parent_ctx->nr_counters))
  1812. return;
  1813. /*
  1814. * Lock the parent list. No need to lock the child - not PID
  1815. * hashed yet and not running, so nobody can access it.
  1816. */
  1817. mutex_lock(&parent_ctx->mutex);
  1818. /*
  1819. * We dont have to disable NMIs - we are only looking at
  1820. * the list, not manipulating it:
  1821. */
  1822. list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
  1823. if (!counter->hw_event.inherit)
  1824. continue;
  1825. if (inherit_group(counter, parent,
  1826. parent_ctx, child, child_ctx))
  1827. break;
  1828. }
  1829. mutex_unlock(&parent_ctx->mutex);
  1830. }
  1831. static void __cpuinit perf_counter_init_cpu(int cpu)
  1832. {
  1833. struct perf_cpu_context *cpuctx;
  1834. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1835. __perf_counter_init_context(&cpuctx->ctx, NULL);
  1836. mutex_lock(&perf_resource_mutex);
  1837. cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
  1838. mutex_unlock(&perf_resource_mutex);
  1839. hw_perf_counter_setup(cpu);
  1840. }
  1841. #ifdef CONFIG_HOTPLUG_CPU
  1842. static void __perf_counter_exit_cpu(void *info)
  1843. {
  1844. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1845. struct perf_counter_context *ctx = &cpuctx->ctx;
  1846. struct perf_counter *counter, *tmp;
  1847. list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
  1848. __perf_counter_remove_from_context(counter);
  1849. }
  1850. static void perf_counter_exit_cpu(int cpu)
  1851. {
  1852. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  1853. struct perf_counter_context *ctx = &cpuctx->ctx;
  1854. mutex_lock(&ctx->mutex);
  1855. smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
  1856. mutex_unlock(&ctx->mutex);
  1857. }
  1858. #else
  1859. static inline void perf_counter_exit_cpu(int cpu) { }
  1860. #endif
  1861. static int __cpuinit
  1862. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  1863. {
  1864. unsigned int cpu = (long)hcpu;
  1865. switch (action) {
  1866. case CPU_UP_PREPARE:
  1867. case CPU_UP_PREPARE_FROZEN:
  1868. perf_counter_init_cpu(cpu);
  1869. break;
  1870. case CPU_DOWN_PREPARE:
  1871. case CPU_DOWN_PREPARE_FROZEN:
  1872. perf_counter_exit_cpu(cpu);
  1873. break;
  1874. default:
  1875. break;
  1876. }
  1877. return NOTIFY_OK;
  1878. }
  1879. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  1880. .notifier_call = perf_cpu_notify,
  1881. };
  1882. static int __init perf_counter_init(void)
  1883. {
  1884. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  1885. (void *)(long)smp_processor_id());
  1886. register_cpu_notifier(&perf_cpu_nb);
  1887. return 0;
  1888. }
  1889. early_initcall(perf_counter_init);
  1890. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  1891. {
  1892. return sprintf(buf, "%d\n", perf_reserved_percpu);
  1893. }
  1894. static ssize_t
  1895. perf_set_reserve_percpu(struct sysdev_class *class,
  1896. const char *buf,
  1897. size_t count)
  1898. {
  1899. struct perf_cpu_context *cpuctx;
  1900. unsigned long val;
  1901. int err, cpu, mpt;
  1902. err = strict_strtoul(buf, 10, &val);
  1903. if (err)
  1904. return err;
  1905. if (val > perf_max_counters)
  1906. return -EINVAL;
  1907. mutex_lock(&perf_resource_mutex);
  1908. perf_reserved_percpu = val;
  1909. for_each_online_cpu(cpu) {
  1910. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1911. spin_lock_irq(&cpuctx->ctx.lock);
  1912. mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
  1913. perf_max_counters - perf_reserved_percpu);
  1914. cpuctx->max_pertask = mpt;
  1915. spin_unlock_irq(&cpuctx->ctx.lock);
  1916. }
  1917. mutex_unlock(&perf_resource_mutex);
  1918. return count;
  1919. }
  1920. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  1921. {
  1922. return sprintf(buf, "%d\n", perf_overcommit);
  1923. }
  1924. static ssize_t
  1925. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  1926. {
  1927. unsigned long val;
  1928. int err;
  1929. err = strict_strtoul(buf, 10, &val);
  1930. if (err)
  1931. return err;
  1932. if (val > 1)
  1933. return -EINVAL;
  1934. mutex_lock(&perf_resource_mutex);
  1935. perf_overcommit = val;
  1936. mutex_unlock(&perf_resource_mutex);
  1937. return count;
  1938. }
  1939. static SYSDEV_CLASS_ATTR(
  1940. reserve_percpu,
  1941. 0644,
  1942. perf_show_reserve_percpu,
  1943. perf_set_reserve_percpu
  1944. );
  1945. static SYSDEV_CLASS_ATTR(
  1946. overcommit,
  1947. 0644,
  1948. perf_show_overcommit,
  1949. perf_set_overcommit
  1950. );
  1951. static struct attribute *perfclass_attrs[] = {
  1952. &attr_reserve_percpu.attr,
  1953. &attr_overcommit.attr,
  1954. NULL
  1955. };
  1956. static struct attribute_group perfclass_attr_group = {
  1957. .attrs = perfclass_attrs,
  1958. .name = "perf_counters",
  1959. };
  1960. static int __init perf_counter_sysfs_init(void)
  1961. {
  1962. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  1963. &perfclass_attr_group);
  1964. }
  1965. device_initcall(perf_counter_sysfs_init);