perf_counter.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  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. /*
  23. * Each CPU has a list of per CPU counters:
  24. */
  25. DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
  26. int perf_max_counters __read_mostly = 1;
  27. static int perf_reserved_percpu __read_mostly;
  28. static int perf_overcommit __read_mostly = 1;
  29. /*
  30. * Mutex for (sysadmin-configurable) counter reservations:
  31. */
  32. static DEFINE_MUTEX(perf_resource_mutex);
  33. /*
  34. * Architecture provided APIs - weak aliases:
  35. */
  36. extern __weak const struct hw_perf_counter_ops *
  37. hw_perf_counter_init(struct perf_counter *counter)
  38. {
  39. return NULL;
  40. }
  41. u64 __weak hw_perf_save_disable(void) { return 0; }
  42. void __weak hw_perf_restore(u64 ctrl) { barrier(); }
  43. void __weak hw_perf_counter_setup(void) { barrier(); }
  44. int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
  45. struct perf_cpu_context *cpuctx,
  46. struct perf_counter_context *ctx, int cpu)
  47. {
  48. return 0;
  49. }
  50. void __weak perf_counter_print_debug(void) { }
  51. static void
  52. list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  53. {
  54. struct perf_counter *group_leader = counter->group_leader;
  55. /*
  56. * Depending on whether it is a standalone or sibling counter,
  57. * add it straight to the context's counter list, or to the group
  58. * leader's sibling list:
  59. */
  60. if (counter->group_leader == counter)
  61. list_add_tail(&counter->list_entry, &ctx->counter_list);
  62. else
  63. list_add_tail(&counter->list_entry, &group_leader->sibling_list);
  64. }
  65. static void
  66. list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  67. {
  68. struct perf_counter *sibling, *tmp;
  69. list_del_init(&counter->list_entry);
  70. /*
  71. * If this was a group counter with sibling counters then
  72. * upgrade the siblings to singleton counters by adding them
  73. * to the context list directly:
  74. */
  75. list_for_each_entry_safe(sibling, tmp,
  76. &counter->sibling_list, list_entry) {
  77. list_del_init(&sibling->list_entry);
  78. list_add_tail(&sibling->list_entry, &ctx->counter_list);
  79. sibling->group_leader = sibling;
  80. }
  81. }
  82. /*
  83. * Cross CPU call to remove a performance counter
  84. *
  85. * We disable the counter on the hardware level first. After that we
  86. * remove it from the context list.
  87. */
  88. static void __perf_counter_remove_from_context(void *info)
  89. {
  90. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  91. struct perf_counter *counter = info;
  92. struct perf_counter_context *ctx = counter->ctx;
  93. unsigned long flags;
  94. u64 perf_flags;
  95. /*
  96. * If this is a task context, we need to check whether it is
  97. * the current task context of this cpu. If not it has been
  98. * scheduled out before the smp call arrived.
  99. */
  100. if (ctx->task && cpuctx->task_ctx != ctx)
  101. return;
  102. curr_rq_lock_irq_save(&flags);
  103. spin_lock(&ctx->lock);
  104. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  105. counter->state = PERF_COUNTER_STATE_INACTIVE;
  106. counter->hw_ops->disable(counter);
  107. ctx->nr_active--;
  108. cpuctx->active_oncpu--;
  109. counter->task = NULL;
  110. counter->oncpu = -1;
  111. }
  112. ctx->nr_counters--;
  113. /*
  114. * Protect the list operation against NMI by disabling the
  115. * counters on a global level. NOP for non NMI based counters.
  116. */
  117. perf_flags = hw_perf_save_disable();
  118. list_del_counter(counter, ctx);
  119. hw_perf_restore(perf_flags);
  120. if (!ctx->task) {
  121. /*
  122. * Allow more per task counters with respect to the
  123. * reservation:
  124. */
  125. cpuctx->max_pertask =
  126. min(perf_max_counters - ctx->nr_counters,
  127. perf_max_counters - perf_reserved_percpu);
  128. }
  129. spin_unlock(&ctx->lock);
  130. curr_rq_unlock_irq_restore(&flags);
  131. }
  132. /*
  133. * Remove the counter from a task's (or a CPU's) list of counters.
  134. *
  135. * Must be called with counter->mutex held.
  136. *
  137. * CPU counters are removed with a smp call. For task counters we only
  138. * call when the task is on a CPU.
  139. */
  140. static void perf_counter_remove_from_context(struct perf_counter *counter)
  141. {
  142. struct perf_counter_context *ctx = counter->ctx;
  143. struct task_struct *task = ctx->task;
  144. if (!task) {
  145. /*
  146. * Per cpu counters are removed via an smp call and
  147. * the removal is always sucessful.
  148. */
  149. smp_call_function_single(counter->cpu,
  150. __perf_counter_remove_from_context,
  151. counter, 1);
  152. return;
  153. }
  154. retry:
  155. task_oncpu_function_call(task, __perf_counter_remove_from_context,
  156. counter);
  157. spin_lock_irq(&ctx->lock);
  158. /*
  159. * If the context is active we need to retry the smp call.
  160. */
  161. if (ctx->nr_active && !list_empty(&counter->list_entry)) {
  162. spin_unlock_irq(&ctx->lock);
  163. goto retry;
  164. }
  165. /*
  166. * The lock prevents that this context is scheduled in so we
  167. * can remove the counter safely, if the call above did not
  168. * succeed.
  169. */
  170. if (!list_empty(&counter->list_entry)) {
  171. ctx->nr_counters--;
  172. list_del_counter(counter, ctx);
  173. counter->task = NULL;
  174. }
  175. spin_unlock_irq(&ctx->lock);
  176. }
  177. static int
  178. counter_sched_in(struct perf_counter *counter,
  179. struct perf_cpu_context *cpuctx,
  180. struct perf_counter_context *ctx,
  181. int cpu)
  182. {
  183. if (counter->state == PERF_COUNTER_STATE_OFF)
  184. return 0;
  185. counter->state = PERF_COUNTER_STATE_ACTIVE;
  186. counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
  187. /*
  188. * The new state must be visible before we turn it on in the hardware:
  189. */
  190. smp_wmb();
  191. if (counter->hw_ops->enable(counter)) {
  192. counter->state = PERF_COUNTER_STATE_INACTIVE;
  193. counter->oncpu = -1;
  194. return -EAGAIN;
  195. }
  196. cpuctx->active_oncpu++;
  197. ctx->nr_active++;
  198. return 0;
  199. }
  200. /*
  201. * Cross CPU call to install and enable a performance counter
  202. */
  203. static void __perf_install_in_context(void *info)
  204. {
  205. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  206. struct perf_counter *counter = info;
  207. struct perf_counter_context *ctx = counter->ctx;
  208. int cpu = smp_processor_id();
  209. unsigned long flags;
  210. u64 perf_flags;
  211. /*
  212. * If this is a task context, we need to check whether it is
  213. * the current task context of this cpu. If not it has been
  214. * scheduled out before the smp call arrived.
  215. */
  216. if (ctx->task && cpuctx->task_ctx != ctx)
  217. return;
  218. curr_rq_lock_irq_save(&flags);
  219. spin_lock(&ctx->lock);
  220. /*
  221. * Protect the list operation against NMI by disabling the
  222. * counters on a global level. NOP for non NMI based counters.
  223. */
  224. perf_flags = hw_perf_save_disable();
  225. list_add_counter(counter, ctx);
  226. ctx->nr_counters++;
  227. counter_sched_in(counter, cpuctx, ctx, cpu);
  228. if (!ctx->task && cpuctx->max_pertask)
  229. cpuctx->max_pertask--;
  230. hw_perf_restore(perf_flags);
  231. spin_unlock(&ctx->lock);
  232. curr_rq_unlock_irq_restore(&flags);
  233. }
  234. /*
  235. * Attach a performance counter to a context
  236. *
  237. * First we add the counter to the list with the hardware enable bit
  238. * in counter->hw_config cleared.
  239. *
  240. * If the counter is attached to a task which is on a CPU we use a smp
  241. * call to enable it in the task context. The task might have been
  242. * scheduled away, but we check this in the smp call again.
  243. */
  244. static void
  245. perf_install_in_context(struct perf_counter_context *ctx,
  246. struct perf_counter *counter,
  247. int cpu)
  248. {
  249. struct task_struct *task = ctx->task;
  250. counter->ctx = ctx;
  251. if (!task) {
  252. /*
  253. * Per cpu counters are installed via an smp call and
  254. * the install is always sucessful.
  255. */
  256. smp_call_function_single(cpu, __perf_install_in_context,
  257. counter, 1);
  258. return;
  259. }
  260. counter->task = task;
  261. retry:
  262. task_oncpu_function_call(task, __perf_install_in_context,
  263. counter);
  264. spin_lock_irq(&ctx->lock);
  265. /*
  266. * we need to retry the smp call.
  267. */
  268. if (ctx->nr_active && list_empty(&counter->list_entry)) {
  269. spin_unlock_irq(&ctx->lock);
  270. goto retry;
  271. }
  272. /*
  273. * The lock prevents that this context is scheduled in so we
  274. * can add the counter safely, if it the call above did not
  275. * succeed.
  276. */
  277. if (list_empty(&counter->list_entry)) {
  278. list_add_counter(counter, ctx);
  279. ctx->nr_counters++;
  280. }
  281. spin_unlock_irq(&ctx->lock);
  282. }
  283. static void
  284. counter_sched_out(struct perf_counter *counter,
  285. struct perf_cpu_context *cpuctx,
  286. struct perf_counter_context *ctx)
  287. {
  288. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  289. return;
  290. counter->state = PERF_COUNTER_STATE_INACTIVE;
  291. counter->hw_ops->disable(counter);
  292. counter->oncpu = -1;
  293. cpuctx->active_oncpu--;
  294. ctx->nr_active--;
  295. }
  296. static void
  297. group_sched_out(struct perf_counter *group_counter,
  298. struct perf_cpu_context *cpuctx,
  299. struct perf_counter_context *ctx)
  300. {
  301. struct perf_counter *counter;
  302. if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
  303. return;
  304. counter_sched_out(group_counter, cpuctx, ctx);
  305. /*
  306. * Schedule out siblings (if any):
  307. */
  308. list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
  309. counter_sched_out(counter, cpuctx, ctx);
  310. }
  311. void __perf_counter_sched_out(struct perf_counter_context *ctx,
  312. struct perf_cpu_context *cpuctx)
  313. {
  314. struct perf_counter *counter;
  315. u64 flags;
  316. if (likely(!ctx->nr_counters))
  317. return;
  318. spin_lock(&ctx->lock);
  319. flags = hw_perf_save_disable();
  320. if (ctx->nr_active) {
  321. list_for_each_entry(counter, &ctx->counter_list, list_entry)
  322. group_sched_out(counter, cpuctx, ctx);
  323. }
  324. hw_perf_restore(flags);
  325. spin_unlock(&ctx->lock);
  326. }
  327. /*
  328. * Called from scheduler to remove the counters of the current task,
  329. * with interrupts disabled.
  330. *
  331. * We stop each counter and update the counter value in counter->count.
  332. *
  333. * This does not protect us against NMI, but disable()
  334. * sets the disabled bit in the control field of counter _before_
  335. * accessing the counter control register. If a NMI hits, then it will
  336. * not restart the counter.
  337. */
  338. void perf_counter_task_sched_out(struct task_struct *task, int cpu)
  339. {
  340. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  341. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  342. if (likely(!cpuctx->task_ctx))
  343. return;
  344. __perf_counter_sched_out(ctx, cpuctx);
  345. cpuctx->task_ctx = NULL;
  346. }
  347. static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
  348. {
  349. __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
  350. }
  351. static int
  352. group_sched_in(struct perf_counter *group_counter,
  353. struct perf_cpu_context *cpuctx,
  354. struct perf_counter_context *ctx,
  355. int cpu)
  356. {
  357. struct perf_counter *counter, *partial_group;
  358. int ret;
  359. if (group_counter->state == PERF_COUNTER_STATE_OFF)
  360. return 0;
  361. ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
  362. if (ret)
  363. return ret < 0 ? ret : 0;
  364. if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
  365. return -EAGAIN;
  366. /*
  367. * Schedule in siblings as one group (if any):
  368. */
  369. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  370. if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
  371. partial_group = counter;
  372. goto group_error;
  373. }
  374. }
  375. return 0;
  376. group_error:
  377. /*
  378. * Groups can be scheduled in as one unit only, so undo any
  379. * partial group before returning:
  380. */
  381. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  382. if (counter == partial_group)
  383. break;
  384. counter_sched_out(counter, cpuctx, ctx);
  385. }
  386. counter_sched_out(group_counter, cpuctx, ctx);
  387. return -EAGAIN;
  388. }
  389. static void
  390. __perf_counter_sched_in(struct perf_counter_context *ctx,
  391. struct perf_cpu_context *cpuctx, int cpu)
  392. {
  393. struct perf_counter *counter;
  394. u64 flags;
  395. if (likely(!ctx->nr_counters))
  396. return;
  397. spin_lock(&ctx->lock);
  398. flags = hw_perf_save_disable();
  399. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  400. /*
  401. * Listen to the 'cpu' scheduling filter constraint
  402. * of counters:
  403. */
  404. if (counter->cpu != -1 && counter->cpu != cpu)
  405. continue;
  406. /*
  407. * If we scheduled in a group atomically and exclusively,
  408. * or if this group can't go on, break out:
  409. */
  410. if (group_sched_in(counter, cpuctx, ctx, cpu))
  411. break;
  412. }
  413. hw_perf_restore(flags);
  414. spin_unlock(&ctx->lock);
  415. }
  416. /*
  417. * Called from scheduler to add the counters of the current task
  418. * with interrupts disabled.
  419. *
  420. * We restore the counter value and then enable it.
  421. *
  422. * This does not protect us against NMI, but enable()
  423. * sets the enabled bit in the control field of counter _before_
  424. * accessing the counter control register. If a NMI hits, then it will
  425. * keep the counter running.
  426. */
  427. void perf_counter_task_sched_in(struct task_struct *task, int cpu)
  428. {
  429. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  430. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  431. __perf_counter_sched_in(ctx, cpuctx, cpu);
  432. cpuctx->task_ctx = ctx;
  433. }
  434. static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
  435. {
  436. struct perf_counter_context *ctx = &cpuctx->ctx;
  437. __perf_counter_sched_in(ctx, cpuctx, cpu);
  438. }
  439. int perf_counter_task_disable(void)
  440. {
  441. struct task_struct *curr = current;
  442. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  443. struct perf_counter *counter;
  444. unsigned long flags;
  445. u64 perf_flags;
  446. int cpu;
  447. if (likely(!ctx->nr_counters))
  448. return 0;
  449. curr_rq_lock_irq_save(&flags);
  450. cpu = smp_processor_id();
  451. /* force the update of the task clock: */
  452. __task_delta_exec(curr, 1);
  453. perf_counter_task_sched_out(curr, cpu);
  454. spin_lock(&ctx->lock);
  455. /*
  456. * Disable all the counters:
  457. */
  458. perf_flags = hw_perf_save_disable();
  459. list_for_each_entry(counter, &ctx->counter_list, list_entry)
  460. counter->state = PERF_COUNTER_STATE_OFF;
  461. hw_perf_restore(perf_flags);
  462. spin_unlock(&ctx->lock);
  463. curr_rq_unlock_irq_restore(&flags);
  464. return 0;
  465. }
  466. int perf_counter_task_enable(void)
  467. {
  468. struct task_struct *curr = current;
  469. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  470. struct perf_counter *counter;
  471. unsigned long flags;
  472. u64 perf_flags;
  473. int cpu;
  474. if (likely(!ctx->nr_counters))
  475. return 0;
  476. curr_rq_lock_irq_save(&flags);
  477. cpu = smp_processor_id();
  478. /* force the update of the task clock: */
  479. __task_delta_exec(curr, 1);
  480. perf_counter_task_sched_out(curr, cpu);
  481. spin_lock(&ctx->lock);
  482. /*
  483. * Disable all the counters:
  484. */
  485. perf_flags = hw_perf_save_disable();
  486. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  487. if (counter->state != PERF_COUNTER_STATE_OFF)
  488. continue;
  489. counter->state = PERF_COUNTER_STATE_INACTIVE;
  490. counter->hw_event.disabled = 0;
  491. }
  492. hw_perf_restore(perf_flags);
  493. spin_unlock(&ctx->lock);
  494. perf_counter_task_sched_in(curr, cpu);
  495. curr_rq_unlock_irq_restore(&flags);
  496. return 0;
  497. }
  498. /*
  499. * Round-robin a context's counters:
  500. */
  501. static void rotate_ctx(struct perf_counter_context *ctx)
  502. {
  503. struct perf_counter *counter;
  504. u64 perf_flags;
  505. if (!ctx->nr_counters)
  506. return;
  507. spin_lock(&ctx->lock);
  508. /*
  509. * Rotate the first entry last (works just fine for group counters too):
  510. */
  511. perf_flags = hw_perf_save_disable();
  512. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  513. list_del(&counter->list_entry);
  514. list_add_tail(&counter->list_entry, &ctx->counter_list);
  515. break;
  516. }
  517. hw_perf_restore(perf_flags);
  518. spin_unlock(&ctx->lock);
  519. }
  520. void perf_counter_task_tick(struct task_struct *curr, int cpu)
  521. {
  522. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  523. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  524. const int rotate_percpu = 0;
  525. if (rotate_percpu)
  526. perf_counter_cpu_sched_out(cpuctx);
  527. perf_counter_task_sched_out(curr, cpu);
  528. if (rotate_percpu)
  529. rotate_ctx(&cpuctx->ctx);
  530. rotate_ctx(ctx);
  531. if (rotate_percpu)
  532. perf_counter_cpu_sched_in(cpuctx, cpu);
  533. perf_counter_task_sched_in(curr, cpu);
  534. }
  535. /*
  536. * Cross CPU call to read the hardware counter
  537. */
  538. static void __read(void *info)
  539. {
  540. struct perf_counter *counter = info;
  541. unsigned long flags;
  542. curr_rq_lock_irq_save(&flags);
  543. counter->hw_ops->read(counter);
  544. curr_rq_unlock_irq_restore(&flags);
  545. }
  546. static u64 perf_counter_read(struct perf_counter *counter)
  547. {
  548. /*
  549. * If counter is enabled and currently active on a CPU, update the
  550. * value in the counter structure:
  551. */
  552. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  553. smp_call_function_single(counter->oncpu,
  554. __read, counter, 1);
  555. }
  556. return atomic64_read(&counter->count);
  557. }
  558. /*
  559. * Cross CPU call to switch performance data pointers
  560. */
  561. static void __perf_switch_irq_data(void *info)
  562. {
  563. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  564. struct perf_counter *counter = info;
  565. struct perf_counter_context *ctx = counter->ctx;
  566. struct perf_data *oldirqdata = counter->irqdata;
  567. /*
  568. * If this is a task context, we need to check whether it is
  569. * the current task context of this cpu. If not it has been
  570. * scheduled out before the smp call arrived.
  571. */
  572. if (ctx->task) {
  573. if (cpuctx->task_ctx != ctx)
  574. return;
  575. spin_lock(&ctx->lock);
  576. }
  577. /* Change the pointer NMI safe */
  578. atomic_long_set((atomic_long_t *)&counter->irqdata,
  579. (unsigned long) counter->usrdata);
  580. counter->usrdata = oldirqdata;
  581. if (ctx->task)
  582. spin_unlock(&ctx->lock);
  583. }
  584. static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
  585. {
  586. struct perf_counter_context *ctx = counter->ctx;
  587. struct perf_data *oldirqdata = counter->irqdata;
  588. struct task_struct *task = ctx->task;
  589. if (!task) {
  590. smp_call_function_single(counter->cpu,
  591. __perf_switch_irq_data,
  592. counter, 1);
  593. return counter->usrdata;
  594. }
  595. retry:
  596. spin_lock_irq(&ctx->lock);
  597. if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
  598. counter->irqdata = counter->usrdata;
  599. counter->usrdata = oldirqdata;
  600. spin_unlock_irq(&ctx->lock);
  601. return oldirqdata;
  602. }
  603. spin_unlock_irq(&ctx->lock);
  604. task_oncpu_function_call(task, __perf_switch_irq_data, counter);
  605. /* Might have failed, because task was scheduled out */
  606. if (counter->irqdata == oldirqdata)
  607. goto retry;
  608. return counter->usrdata;
  609. }
  610. static void put_context(struct perf_counter_context *ctx)
  611. {
  612. if (ctx->task)
  613. put_task_struct(ctx->task);
  614. }
  615. static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
  616. {
  617. struct perf_cpu_context *cpuctx;
  618. struct perf_counter_context *ctx;
  619. struct task_struct *task;
  620. /*
  621. * If cpu is not a wildcard then this is a percpu counter:
  622. */
  623. if (cpu != -1) {
  624. /* Must be root to operate on a CPU counter: */
  625. if (!capable(CAP_SYS_ADMIN))
  626. return ERR_PTR(-EACCES);
  627. if (cpu < 0 || cpu > num_possible_cpus())
  628. return ERR_PTR(-EINVAL);
  629. /*
  630. * We could be clever and allow to attach a counter to an
  631. * offline CPU and activate it when the CPU comes up, but
  632. * that's for later.
  633. */
  634. if (!cpu_isset(cpu, cpu_online_map))
  635. return ERR_PTR(-ENODEV);
  636. cpuctx = &per_cpu(perf_cpu_context, cpu);
  637. ctx = &cpuctx->ctx;
  638. return ctx;
  639. }
  640. rcu_read_lock();
  641. if (!pid)
  642. task = current;
  643. else
  644. task = find_task_by_vpid(pid);
  645. if (task)
  646. get_task_struct(task);
  647. rcu_read_unlock();
  648. if (!task)
  649. return ERR_PTR(-ESRCH);
  650. ctx = &task->perf_counter_ctx;
  651. ctx->task = task;
  652. /* Reuse ptrace permission checks for now. */
  653. if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
  654. put_context(ctx);
  655. return ERR_PTR(-EACCES);
  656. }
  657. return ctx;
  658. }
  659. /*
  660. * Called when the last reference to the file is gone.
  661. */
  662. static int perf_release(struct inode *inode, struct file *file)
  663. {
  664. struct perf_counter *counter = file->private_data;
  665. struct perf_counter_context *ctx = counter->ctx;
  666. file->private_data = NULL;
  667. mutex_lock(&counter->mutex);
  668. perf_counter_remove_from_context(counter);
  669. put_context(ctx);
  670. mutex_unlock(&counter->mutex);
  671. kfree(counter);
  672. return 0;
  673. }
  674. /*
  675. * Read the performance counter - simple non blocking version for now
  676. */
  677. static ssize_t
  678. perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
  679. {
  680. u64 cntval;
  681. if (count != sizeof(cntval))
  682. return -EINVAL;
  683. mutex_lock(&counter->mutex);
  684. cntval = perf_counter_read(counter);
  685. mutex_unlock(&counter->mutex);
  686. return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
  687. }
  688. static ssize_t
  689. perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
  690. {
  691. if (!usrdata->len)
  692. return 0;
  693. count = min(count, (size_t)usrdata->len);
  694. if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
  695. return -EFAULT;
  696. /* Adjust the counters */
  697. usrdata->len -= count;
  698. if (!usrdata->len)
  699. usrdata->rd_idx = 0;
  700. else
  701. usrdata->rd_idx += count;
  702. return count;
  703. }
  704. static ssize_t
  705. perf_read_irq_data(struct perf_counter *counter,
  706. char __user *buf,
  707. size_t count,
  708. int nonblocking)
  709. {
  710. struct perf_data *irqdata, *usrdata;
  711. DECLARE_WAITQUEUE(wait, current);
  712. ssize_t res;
  713. irqdata = counter->irqdata;
  714. usrdata = counter->usrdata;
  715. if (usrdata->len + irqdata->len >= count)
  716. goto read_pending;
  717. if (nonblocking)
  718. return -EAGAIN;
  719. spin_lock_irq(&counter->waitq.lock);
  720. __add_wait_queue(&counter->waitq, &wait);
  721. for (;;) {
  722. set_current_state(TASK_INTERRUPTIBLE);
  723. if (usrdata->len + irqdata->len >= count)
  724. break;
  725. if (signal_pending(current))
  726. break;
  727. spin_unlock_irq(&counter->waitq.lock);
  728. schedule();
  729. spin_lock_irq(&counter->waitq.lock);
  730. }
  731. __remove_wait_queue(&counter->waitq, &wait);
  732. __set_current_state(TASK_RUNNING);
  733. spin_unlock_irq(&counter->waitq.lock);
  734. if (usrdata->len + irqdata->len < count)
  735. return -ERESTARTSYS;
  736. read_pending:
  737. mutex_lock(&counter->mutex);
  738. /* Drain pending data first: */
  739. res = perf_copy_usrdata(usrdata, buf, count);
  740. if (res < 0 || res == count)
  741. goto out;
  742. /* Switch irq buffer: */
  743. usrdata = perf_switch_irq_data(counter);
  744. if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) {
  745. if (!res)
  746. res = -EFAULT;
  747. } else {
  748. res = count;
  749. }
  750. out:
  751. mutex_unlock(&counter->mutex);
  752. return res;
  753. }
  754. static ssize_t
  755. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  756. {
  757. struct perf_counter *counter = file->private_data;
  758. switch (counter->hw_event.record_type) {
  759. case PERF_RECORD_SIMPLE:
  760. return perf_read_hw(counter, buf, count);
  761. case PERF_RECORD_IRQ:
  762. case PERF_RECORD_GROUP:
  763. return perf_read_irq_data(counter, buf, count,
  764. file->f_flags & O_NONBLOCK);
  765. }
  766. return -EINVAL;
  767. }
  768. static unsigned int perf_poll(struct file *file, poll_table *wait)
  769. {
  770. struct perf_counter *counter = file->private_data;
  771. unsigned int events = 0;
  772. unsigned long flags;
  773. poll_wait(file, &counter->waitq, wait);
  774. spin_lock_irqsave(&counter->waitq.lock, flags);
  775. if (counter->usrdata->len || counter->irqdata->len)
  776. events |= POLLIN;
  777. spin_unlock_irqrestore(&counter->waitq.lock, flags);
  778. return events;
  779. }
  780. static const struct file_operations perf_fops = {
  781. .release = perf_release,
  782. .read = perf_read,
  783. .poll = perf_poll,
  784. };
  785. static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
  786. {
  787. int cpu = raw_smp_processor_id();
  788. atomic64_set(&counter->hw.prev_count, cpu_clock(cpu));
  789. return 0;
  790. }
  791. static void cpu_clock_perf_counter_update(struct perf_counter *counter)
  792. {
  793. int cpu = raw_smp_processor_id();
  794. s64 prev;
  795. u64 now;
  796. now = cpu_clock(cpu);
  797. prev = atomic64_read(&counter->hw.prev_count);
  798. atomic64_set(&counter->hw.prev_count, now);
  799. atomic64_add(now - prev, &counter->count);
  800. }
  801. static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
  802. {
  803. cpu_clock_perf_counter_update(counter);
  804. }
  805. static void cpu_clock_perf_counter_read(struct perf_counter *counter)
  806. {
  807. cpu_clock_perf_counter_update(counter);
  808. }
  809. static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
  810. .enable = cpu_clock_perf_counter_enable,
  811. .disable = cpu_clock_perf_counter_disable,
  812. .read = cpu_clock_perf_counter_read,
  813. };
  814. /*
  815. * Called from within the scheduler:
  816. */
  817. static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
  818. {
  819. struct task_struct *curr = counter->task;
  820. u64 delta;
  821. delta = __task_delta_exec(curr, update);
  822. return curr->se.sum_exec_runtime + delta;
  823. }
  824. static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
  825. {
  826. u64 prev;
  827. s64 delta;
  828. prev = atomic64_read(&counter->hw.prev_count);
  829. atomic64_set(&counter->hw.prev_count, now);
  830. delta = now - prev;
  831. atomic64_add(delta, &counter->count);
  832. }
  833. static void task_clock_perf_counter_read(struct perf_counter *counter)
  834. {
  835. u64 now = task_clock_perf_counter_val(counter, 1);
  836. task_clock_perf_counter_update(counter, now);
  837. }
  838. static int task_clock_perf_counter_enable(struct perf_counter *counter)
  839. {
  840. u64 now = task_clock_perf_counter_val(counter, 0);
  841. atomic64_set(&counter->hw.prev_count, now);
  842. return 0;
  843. }
  844. static void task_clock_perf_counter_disable(struct perf_counter *counter)
  845. {
  846. u64 now = task_clock_perf_counter_val(counter, 0);
  847. task_clock_perf_counter_update(counter, now);
  848. }
  849. static const struct hw_perf_counter_ops perf_ops_task_clock = {
  850. .enable = task_clock_perf_counter_enable,
  851. .disable = task_clock_perf_counter_disable,
  852. .read = task_clock_perf_counter_read,
  853. };
  854. static u64 get_page_faults(void)
  855. {
  856. struct task_struct *curr = current;
  857. return curr->maj_flt + curr->min_flt;
  858. }
  859. static void page_faults_perf_counter_update(struct perf_counter *counter)
  860. {
  861. u64 prev, now;
  862. s64 delta;
  863. prev = atomic64_read(&counter->hw.prev_count);
  864. now = get_page_faults();
  865. atomic64_set(&counter->hw.prev_count, now);
  866. delta = now - prev;
  867. atomic64_add(delta, &counter->count);
  868. }
  869. static void page_faults_perf_counter_read(struct perf_counter *counter)
  870. {
  871. page_faults_perf_counter_update(counter);
  872. }
  873. static int page_faults_perf_counter_enable(struct perf_counter *counter)
  874. {
  875. /*
  876. * page-faults is a per-task value already,
  877. * so we dont have to clear it on switch-in.
  878. */
  879. return 0;
  880. }
  881. static void page_faults_perf_counter_disable(struct perf_counter *counter)
  882. {
  883. page_faults_perf_counter_update(counter);
  884. }
  885. static const struct hw_perf_counter_ops perf_ops_page_faults = {
  886. .enable = page_faults_perf_counter_enable,
  887. .disable = page_faults_perf_counter_disable,
  888. .read = page_faults_perf_counter_read,
  889. };
  890. static u64 get_context_switches(void)
  891. {
  892. struct task_struct *curr = current;
  893. return curr->nvcsw + curr->nivcsw;
  894. }
  895. static void context_switches_perf_counter_update(struct perf_counter *counter)
  896. {
  897. u64 prev, now;
  898. s64 delta;
  899. prev = atomic64_read(&counter->hw.prev_count);
  900. now = get_context_switches();
  901. atomic64_set(&counter->hw.prev_count, now);
  902. delta = now - prev;
  903. atomic64_add(delta, &counter->count);
  904. }
  905. static void context_switches_perf_counter_read(struct perf_counter *counter)
  906. {
  907. context_switches_perf_counter_update(counter);
  908. }
  909. static int context_switches_perf_counter_enable(struct perf_counter *counter)
  910. {
  911. /*
  912. * ->nvcsw + curr->nivcsw is a per-task value already,
  913. * so we dont have to clear it on switch-in.
  914. */
  915. return 0;
  916. }
  917. static void context_switches_perf_counter_disable(struct perf_counter *counter)
  918. {
  919. context_switches_perf_counter_update(counter);
  920. }
  921. static const struct hw_perf_counter_ops perf_ops_context_switches = {
  922. .enable = context_switches_perf_counter_enable,
  923. .disable = context_switches_perf_counter_disable,
  924. .read = context_switches_perf_counter_read,
  925. };
  926. static inline u64 get_cpu_migrations(void)
  927. {
  928. return current->se.nr_migrations;
  929. }
  930. static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
  931. {
  932. u64 prev, now;
  933. s64 delta;
  934. prev = atomic64_read(&counter->hw.prev_count);
  935. now = get_cpu_migrations();
  936. atomic64_set(&counter->hw.prev_count, now);
  937. delta = now - prev;
  938. atomic64_add(delta, &counter->count);
  939. }
  940. static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
  941. {
  942. cpu_migrations_perf_counter_update(counter);
  943. }
  944. static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
  945. {
  946. /*
  947. * se.nr_migrations is a per-task value already,
  948. * so we dont have to clear it on switch-in.
  949. */
  950. return 0;
  951. }
  952. static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
  953. {
  954. cpu_migrations_perf_counter_update(counter);
  955. }
  956. static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
  957. .enable = cpu_migrations_perf_counter_enable,
  958. .disable = cpu_migrations_perf_counter_disable,
  959. .read = cpu_migrations_perf_counter_read,
  960. };
  961. static const struct hw_perf_counter_ops *
  962. sw_perf_counter_init(struct perf_counter *counter)
  963. {
  964. const struct hw_perf_counter_ops *hw_ops = NULL;
  965. switch (counter->hw_event.type) {
  966. case PERF_COUNT_CPU_CLOCK:
  967. hw_ops = &perf_ops_cpu_clock;
  968. break;
  969. case PERF_COUNT_TASK_CLOCK:
  970. hw_ops = &perf_ops_task_clock;
  971. break;
  972. case PERF_COUNT_PAGE_FAULTS:
  973. hw_ops = &perf_ops_page_faults;
  974. break;
  975. case PERF_COUNT_CONTEXT_SWITCHES:
  976. hw_ops = &perf_ops_context_switches;
  977. break;
  978. case PERF_COUNT_CPU_MIGRATIONS:
  979. hw_ops = &perf_ops_cpu_migrations;
  980. break;
  981. default:
  982. break;
  983. }
  984. return hw_ops;
  985. }
  986. /*
  987. * Allocate and initialize a counter structure
  988. */
  989. static struct perf_counter *
  990. perf_counter_alloc(struct perf_counter_hw_event *hw_event,
  991. int cpu,
  992. struct perf_counter *group_leader,
  993. gfp_t gfpflags)
  994. {
  995. const struct hw_perf_counter_ops *hw_ops;
  996. struct perf_counter *counter;
  997. counter = kzalloc(sizeof(*counter), gfpflags);
  998. if (!counter)
  999. return NULL;
  1000. /*
  1001. * Single counters are their own group leaders, with an
  1002. * empty sibling list:
  1003. */
  1004. if (!group_leader)
  1005. group_leader = counter;
  1006. mutex_init(&counter->mutex);
  1007. INIT_LIST_HEAD(&counter->list_entry);
  1008. INIT_LIST_HEAD(&counter->sibling_list);
  1009. init_waitqueue_head(&counter->waitq);
  1010. counter->irqdata = &counter->data[0];
  1011. counter->usrdata = &counter->data[1];
  1012. counter->cpu = cpu;
  1013. counter->hw_event = *hw_event;
  1014. counter->wakeup_pending = 0;
  1015. counter->group_leader = group_leader;
  1016. counter->hw_ops = NULL;
  1017. counter->state = PERF_COUNTER_STATE_INACTIVE;
  1018. if (hw_event->disabled)
  1019. counter->state = PERF_COUNTER_STATE_OFF;
  1020. hw_ops = NULL;
  1021. if (!hw_event->raw && hw_event->type < 0)
  1022. hw_ops = sw_perf_counter_init(counter);
  1023. if (!hw_ops)
  1024. hw_ops = hw_perf_counter_init(counter);
  1025. if (!hw_ops) {
  1026. kfree(counter);
  1027. return NULL;
  1028. }
  1029. counter->hw_ops = hw_ops;
  1030. return counter;
  1031. }
  1032. /**
  1033. * sys_perf_task_open - open a performance counter, associate it to a task/cpu
  1034. *
  1035. * @hw_event_uptr: event type attributes for monitoring/sampling
  1036. * @pid: target pid
  1037. * @cpu: target cpu
  1038. * @group_fd: group leader counter fd
  1039. */
  1040. asmlinkage int
  1041. sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user,
  1042. pid_t pid, int cpu, int group_fd)
  1043. {
  1044. struct perf_counter *counter, *group_leader;
  1045. struct perf_counter_hw_event hw_event;
  1046. struct perf_counter_context *ctx;
  1047. struct file *counter_file = NULL;
  1048. struct file *group_file = NULL;
  1049. int fput_needed = 0;
  1050. int fput_needed2 = 0;
  1051. int ret;
  1052. if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
  1053. return -EFAULT;
  1054. /*
  1055. * Get the target context (task or percpu):
  1056. */
  1057. ctx = find_get_context(pid, cpu);
  1058. if (IS_ERR(ctx))
  1059. return PTR_ERR(ctx);
  1060. /*
  1061. * Look up the group leader (we will attach this counter to it):
  1062. */
  1063. group_leader = NULL;
  1064. if (group_fd != -1) {
  1065. ret = -EINVAL;
  1066. group_file = fget_light(group_fd, &fput_needed);
  1067. if (!group_file)
  1068. goto err_put_context;
  1069. if (group_file->f_op != &perf_fops)
  1070. goto err_put_context;
  1071. group_leader = group_file->private_data;
  1072. /*
  1073. * Do not allow a recursive hierarchy (this new sibling
  1074. * becoming part of another group-sibling):
  1075. */
  1076. if (group_leader->group_leader != group_leader)
  1077. goto err_put_context;
  1078. /*
  1079. * Do not allow to attach to a group in a different
  1080. * task or CPU context:
  1081. */
  1082. if (group_leader->ctx != ctx)
  1083. goto err_put_context;
  1084. }
  1085. ret = -EINVAL;
  1086. counter = perf_counter_alloc(&hw_event, cpu, group_leader, GFP_KERNEL);
  1087. if (!counter)
  1088. goto err_put_context;
  1089. ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
  1090. if (ret < 0)
  1091. goto err_free_put_context;
  1092. counter_file = fget_light(ret, &fput_needed2);
  1093. if (!counter_file)
  1094. goto err_free_put_context;
  1095. counter->filp = counter_file;
  1096. perf_install_in_context(ctx, counter, cpu);
  1097. fput_light(counter_file, fput_needed2);
  1098. out_fput:
  1099. fput_light(group_file, fput_needed);
  1100. return ret;
  1101. err_free_put_context:
  1102. kfree(counter);
  1103. err_put_context:
  1104. put_context(ctx);
  1105. goto out_fput;
  1106. }
  1107. /*
  1108. * Initialize the perf_counter context in a task_struct:
  1109. */
  1110. static void
  1111. __perf_counter_init_context(struct perf_counter_context *ctx,
  1112. struct task_struct *task)
  1113. {
  1114. memset(ctx, 0, sizeof(*ctx));
  1115. spin_lock_init(&ctx->lock);
  1116. INIT_LIST_HEAD(&ctx->counter_list);
  1117. ctx->task = task;
  1118. }
  1119. /*
  1120. * inherit a counter from parent task to child task:
  1121. */
  1122. static int
  1123. inherit_counter(struct perf_counter *parent_counter,
  1124. struct task_struct *parent,
  1125. struct perf_counter_context *parent_ctx,
  1126. struct task_struct *child,
  1127. struct perf_counter_context *child_ctx)
  1128. {
  1129. struct perf_counter *child_counter;
  1130. child_counter = perf_counter_alloc(&parent_counter->hw_event,
  1131. parent_counter->cpu, NULL,
  1132. GFP_ATOMIC);
  1133. if (!child_counter)
  1134. return -ENOMEM;
  1135. /*
  1136. * Link it up in the child's context:
  1137. */
  1138. child_counter->ctx = child_ctx;
  1139. child_counter->task = child;
  1140. list_add_counter(child_counter, child_ctx);
  1141. child_ctx->nr_counters++;
  1142. child_counter->parent = parent_counter;
  1143. /*
  1144. * inherit into child's child as well:
  1145. */
  1146. child_counter->hw_event.inherit = 1;
  1147. /*
  1148. * Get a reference to the parent filp - we will fput it
  1149. * when the child counter exits. This is safe to do because
  1150. * we are in the parent and we know that the filp still
  1151. * exists and has a nonzero count:
  1152. */
  1153. atomic_long_inc(&parent_counter->filp->f_count);
  1154. return 0;
  1155. }
  1156. static void
  1157. __perf_counter_exit_task(struct task_struct *child,
  1158. struct perf_counter *child_counter,
  1159. struct perf_counter_context *child_ctx)
  1160. {
  1161. struct perf_counter *parent_counter;
  1162. u64 parent_val, child_val;
  1163. /*
  1164. * If we do not self-reap then we have to wait for the
  1165. * child task to unschedule (it will happen for sure),
  1166. * so that its counter is at its final count. (This
  1167. * condition triggers rarely - child tasks usually get
  1168. * off their CPU before the parent has a chance to
  1169. * get this far into the reaping action)
  1170. */
  1171. if (child != current) {
  1172. wait_task_inactive(child, 0);
  1173. list_del_init(&child_counter->list_entry);
  1174. } else {
  1175. struct perf_cpu_context *cpuctx;
  1176. unsigned long flags;
  1177. u64 perf_flags;
  1178. /*
  1179. * Disable and unlink this counter.
  1180. *
  1181. * Be careful about zapping the list - IRQ/NMI context
  1182. * could still be processing it:
  1183. */
  1184. curr_rq_lock_irq_save(&flags);
  1185. perf_flags = hw_perf_save_disable();
  1186. cpuctx = &__get_cpu_var(perf_cpu_context);
  1187. if (child_counter->state == PERF_COUNTER_STATE_ACTIVE) {
  1188. child_counter->state = PERF_COUNTER_STATE_INACTIVE;
  1189. child_counter->hw_ops->disable(child_counter);
  1190. cpuctx->active_oncpu--;
  1191. child_ctx->nr_active--;
  1192. child_counter->oncpu = -1;
  1193. }
  1194. list_del_init(&child_counter->list_entry);
  1195. child_ctx->nr_counters--;
  1196. hw_perf_restore(perf_flags);
  1197. curr_rq_unlock_irq_restore(&flags);
  1198. }
  1199. parent_counter = child_counter->parent;
  1200. /*
  1201. * It can happen that parent exits first, and has counters
  1202. * that are still around due to the child reference. These
  1203. * counters need to be zapped - but otherwise linger.
  1204. */
  1205. if (!parent_counter)
  1206. return;
  1207. parent_val = atomic64_read(&parent_counter->count);
  1208. child_val = atomic64_read(&child_counter->count);
  1209. /*
  1210. * Add back the child's count to the parent's count:
  1211. */
  1212. atomic64_add(child_val, &parent_counter->count);
  1213. fput(parent_counter->filp);
  1214. kfree(child_counter);
  1215. }
  1216. /*
  1217. * When a child task exist, feed back counter values to parent counters.
  1218. *
  1219. * Note: we are running in child context, but the PID is not hashed
  1220. * anymore so new counters will not be added.
  1221. */
  1222. void perf_counter_exit_task(struct task_struct *child)
  1223. {
  1224. struct perf_counter *child_counter, *tmp;
  1225. struct perf_counter_context *child_ctx;
  1226. child_ctx = &child->perf_counter_ctx;
  1227. if (likely(!child_ctx->nr_counters))
  1228. return;
  1229. list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
  1230. list_entry)
  1231. __perf_counter_exit_task(child, child_counter, child_ctx);
  1232. }
  1233. /*
  1234. * Initialize the perf_counter context in task_struct
  1235. */
  1236. void perf_counter_init_task(struct task_struct *child)
  1237. {
  1238. struct perf_counter_context *child_ctx, *parent_ctx;
  1239. struct perf_counter *counter, *parent_counter;
  1240. struct task_struct *parent = current;
  1241. unsigned long flags;
  1242. child_ctx = &child->perf_counter_ctx;
  1243. parent_ctx = &parent->perf_counter_ctx;
  1244. __perf_counter_init_context(child_ctx, child);
  1245. /*
  1246. * This is executed from the parent task context, so inherit
  1247. * counters that have been marked for cloning:
  1248. */
  1249. if (likely(!parent_ctx->nr_counters))
  1250. return;
  1251. /*
  1252. * Lock the parent list. No need to lock the child - not PID
  1253. * hashed yet and not running, so nobody can access it.
  1254. */
  1255. spin_lock_irqsave(&parent_ctx->lock, flags);
  1256. /*
  1257. * We dont have to disable NMIs - we are only looking at
  1258. * the list, not manipulating it:
  1259. */
  1260. list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
  1261. if (!counter->hw_event.inherit || counter->group_leader != counter)
  1262. continue;
  1263. /*
  1264. * Instead of creating recursive hierarchies of counters,
  1265. * we link inheritd counters back to the original parent,
  1266. * which has a filp for sure, which we use as the reference
  1267. * count:
  1268. */
  1269. parent_counter = counter;
  1270. if (counter->parent)
  1271. parent_counter = counter->parent;
  1272. if (inherit_counter(parent_counter, parent,
  1273. parent_ctx, child, child_ctx))
  1274. break;
  1275. }
  1276. spin_unlock_irqrestore(&parent_ctx->lock, flags);
  1277. }
  1278. static void __cpuinit perf_counter_init_cpu(int cpu)
  1279. {
  1280. struct perf_cpu_context *cpuctx;
  1281. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1282. __perf_counter_init_context(&cpuctx->ctx, NULL);
  1283. mutex_lock(&perf_resource_mutex);
  1284. cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
  1285. mutex_unlock(&perf_resource_mutex);
  1286. hw_perf_counter_setup();
  1287. }
  1288. #ifdef CONFIG_HOTPLUG_CPU
  1289. static void __perf_counter_exit_cpu(void *info)
  1290. {
  1291. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1292. struct perf_counter_context *ctx = &cpuctx->ctx;
  1293. struct perf_counter *counter, *tmp;
  1294. list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
  1295. __perf_counter_remove_from_context(counter);
  1296. }
  1297. static void perf_counter_exit_cpu(int cpu)
  1298. {
  1299. smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
  1300. }
  1301. #else
  1302. static inline void perf_counter_exit_cpu(int cpu) { }
  1303. #endif
  1304. static int __cpuinit
  1305. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  1306. {
  1307. unsigned int cpu = (long)hcpu;
  1308. switch (action) {
  1309. case CPU_UP_PREPARE:
  1310. case CPU_UP_PREPARE_FROZEN:
  1311. perf_counter_init_cpu(cpu);
  1312. break;
  1313. case CPU_DOWN_PREPARE:
  1314. case CPU_DOWN_PREPARE_FROZEN:
  1315. perf_counter_exit_cpu(cpu);
  1316. break;
  1317. default:
  1318. break;
  1319. }
  1320. return NOTIFY_OK;
  1321. }
  1322. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  1323. .notifier_call = perf_cpu_notify,
  1324. };
  1325. static int __init perf_counter_init(void)
  1326. {
  1327. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  1328. (void *)(long)smp_processor_id());
  1329. register_cpu_notifier(&perf_cpu_nb);
  1330. return 0;
  1331. }
  1332. early_initcall(perf_counter_init);
  1333. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  1334. {
  1335. return sprintf(buf, "%d\n", perf_reserved_percpu);
  1336. }
  1337. static ssize_t
  1338. perf_set_reserve_percpu(struct sysdev_class *class,
  1339. const char *buf,
  1340. size_t count)
  1341. {
  1342. struct perf_cpu_context *cpuctx;
  1343. unsigned long val;
  1344. int err, cpu, mpt;
  1345. err = strict_strtoul(buf, 10, &val);
  1346. if (err)
  1347. return err;
  1348. if (val > perf_max_counters)
  1349. return -EINVAL;
  1350. mutex_lock(&perf_resource_mutex);
  1351. perf_reserved_percpu = val;
  1352. for_each_online_cpu(cpu) {
  1353. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1354. spin_lock_irq(&cpuctx->ctx.lock);
  1355. mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
  1356. perf_max_counters - perf_reserved_percpu);
  1357. cpuctx->max_pertask = mpt;
  1358. spin_unlock_irq(&cpuctx->ctx.lock);
  1359. }
  1360. mutex_unlock(&perf_resource_mutex);
  1361. return count;
  1362. }
  1363. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  1364. {
  1365. return sprintf(buf, "%d\n", perf_overcommit);
  1366. }
  1367. static ssize_t
  1368. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  1369. {
  1370. unsigned long val;
  1371. int err;
  1372. err = strict_strtoul(buf, 10, &val);
  1373. if (err)
  1374. return err;
  1375. if (val > 1)
  1376. return -EINVAL;
  1377. mutex_lock(&perf_resource_mutex);
  1378. perf_overcommit = val;
  1379. mutex_unlock(&perf_resource_mutex);
  1380. return count;
  1381. }
  1382. static SYSDEV_CLASS_ATTR(
  1383. reserve_percpu,
  1384. 0644,
  1385. perf_show_reserve_percpu,
  1386. perf_set_reserve_percpu
  1387. );
  1388. static SYSDEV_CLASS_ATTR(
  1389. overcommit,
  1390. 0644,
  1391. perf_show_overcommit,
  1392. perf_set_overcommit
  1393. );
  1394. static struct attribute *perfclass_attrs[] = {
  1395. &attr_reserve_percpu.attr,
  1396. &attr_overcommit.attr,
  1397. NULL
  1398. };
  1399. static struct attribute_group perfclass_attr_group = {
  1400. .attrs = perfclass_attrs,
  1401. .name = "perf_counters",
  1402. };
  1403. static int __init perf_counter_sysfs_init(void)
  1404. {
  1405. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  1406. &perfclass_attr_group);
  1407. }
  1408. device_initcall(perf_counter_sysfs_init);