perf_counter.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  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. /*
  390. * Return 1 for a software counter, 0 for a hardware counter
  391. */
  392. static inline int is_software_counter(struct perf_counter *counter)
  393. {
  394. return !counter->hw_event.raw && counter->hw_event.type < 0;
  395. }
  396. /*
  397. * Return 1 for a group consisting entirely of software counters,
  398. * 0 if the group contains any hardware counters.
  399. */
  400. static int is_software_only_group(struct perf_counter *leader)
  401. {
  402. struct perf_counter *counter;
  403. if (!is_software_counter(leader))
  404. return 0;
  405. list_for_each_entry(counter, &leader->sibling_list, list_entry)
  406. if (!is_software_counter(counter))
  407. return 0;
  408. return 1;
  409. }
  410. static void
  411. __perf_counter_sched_in(struct perf_counter_context *ctx,
  412. struct perf_cpu_context *cpuctx, int cpu)
  413. {
  414. struct perf_counter *counter;
  415. u64 flags;
  416. int can_add_hw = 1;
  417. if (likely(!ctx->nr_counters))
  418. return;
  419. spin_lock(&ctx->lock);
  420. flags = hw_perf_save_disable();
  421. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  422. /*
  423. * Listen to the 'cpu' scheduling filter constraint
  424. * of counters:
  425. */
  426. if (counter->cpu != -1 && counter->cpu != cpu)
  427. continue;
  428. /*
  429. * If we scheduled in a group atomically and exclusively,
  430. * or if this group can't go on, don't add any more
  431. * hardware counters.
  432. */
  433. if (can_add_hw || is_software_only_group(counter))
  434. if (group_sched_in(counter, cpuctx, ctx, cpu))
  435. can_add_hw = 0;
  436. }
  437. hw_perf_restore(flags);
  438. spin_unlock(&ctx->lock);
  439. }
  440. /*
  441. * Called from scheduler to add the counters of the current task
  442. * with interrupts disabled.
  443. *
  444. * We restore the counter value and then enable it.
  445. *
  446. * This does not protect us against NMI, but enable()
  447. * sets the enabled bit in the control field of counter _before_
  448. * accessing the counter control register. If a NMI hits, then it will
  449. * keep the counter running.
  450. */
  451. void perf_counter_task_sched_in(struct task_struct *task, int cpu)
  452. {
  453. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  454. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  455. __perf_counter_sched_in(ctx, cpuctx, cpu);
  456. cpuctx->task_ctx = ctx;
  457. }
  458. static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
  459. {
  460. struct perf_counter_context *ctx = &cpuctx->ctx;
  461. __perf_counter_sched_in(ctx, cpuctx, cpu);
  462. }
  463. int perf_counter_task_disable(void)
  464. {
  465. struct task_struct *curr = current;
  466. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  467. struct perf_counter *counter;
  468. unsigned long flags;
  469. u64 perf_flags;
  470. int cpu;
  471. if (likely(!ctx->nr_counters))
  472. return 0;
  473. curr_rq_lock_irq_save(&flags);
  474. cpu = smp_processor_id();
  475. /* force the update of the task clock: */
  476. __task_delta_exec(curr, 1);
  477. perf_counter_task_sched_out(curr, cpu);
  478. spin_lock(&ctx->lock);
  479. /*
  480. * Disable all the counters:
  481. */
  482. perf_flags = hw_perf_save_disable();
  483. list_for_each_entry(counter, &ctx->counter_list, list_entry)
  484. counter->state = PERF_COUNTER_STATE_OFF;
  485. hw_perf_restore(perf_flags);
  486. spin_unlock(&ctx->lock);
  487. curr_rq_unlock_irq_restore(&flags);
  488. return 0;
  489. }
  490. int perf_counter_task_enable(void)
  491. {
  492. struct task_struct *curr = current;
  493. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  494. struct perf_counter *counter;
  495. unsigned long flags;
  496. u64 perf_flags;
  497. int cpu;
  498. if (likely(!ctx->nr_counters))
  499. return 0;
  500. curr_rq_lock_irq_save(&flags);
  501. cpu = smp_processor_id();
  502. /* force the update of the task clock: */
  503. __task_delta_exec(curr, 1);
  504. perf_counter_task_sched_out(curr, cpu);
  505. spin_lock(&ctx->lock);
  506. /*
  507. * Disable all the counters:
  508. */
  509. perf_flags = hw_perf_save_disable();
  510. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  511. if (counter->state != PERF_COUNTER_STATE_OFF)
  512. continue;
  513. counter->state = PERF_COUNTER_STATE_INACTIVE;
  514. counter->hw_event.disabled = 0;
  515. }
  516. hw_perf_restore(perf_flags);
  517. spin_unlock(&ctx->lock);
  518. perf_counter_task_sched_in(curr, cpu);
  519. curr_rq_unlock_irq_restore(&flags);
  520. return 0;
  521. }
  522. /*
  523. * Round-robin a context's counters:
  524. */
  525. static void rotate_ctx(struct perf_counter_context *ctx)
  526. {
  527. struct perf_counter *counter;
  528. u64 perf_flags;
  529. if (!ctx->nr_counters)
  530. return;
  531. spin_lock(&ctx->lock);
  532. /*
  533. * Rotate the first entry last (works just fine for group counters too):
  534. */
  535. perf_flags = hw_perf_save_disable();
  536. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  537. list_del(&counter->list_entry);
  538. list_add_tail(&counter->list_entry, &ctx->counter_list);
  539. break;
  540. }
  541. hw_perf_restore(perf_flags);
  542. spin_unlock(&ctx->lock);
  543. }
  544. void perf_counter_task_tick(struct task_struct *curr, int cpu)
  545. {
  546. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  547. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  548. const int rotate_percpu = 0;
  549. if (rotate_percpu)
  550. perf_counter_cpu_sched_out(cpuctx);
  551. perf_counter_task_sched_out(curr, cpu);
  552. if (rotate_percpu)
  553. rotate_ctx(&cpuctx->ctx);
  554. rotate_ctx(ctx);
  555. if (rotate_percpu)
  556. perf_counter_cpu_sched_in(cpuctx, cpu);
  557. perf_counter_task_sched_in(curr, cpu);
  558. }
  559. /*
  560. * Cross CPU call to read the hardware counter
  561. */
  562. static void __read(void *info)
  563. {
  564. struct perf_counter *counter = info;
  565. unsigned long flags;
  566. curr_rq_lock_irq_save(&flags);
  567. counter->hw_ops->read(counter);
  568. curr_rq_unlock_irq_restore(&flags);
  569. }
  570. static u64 perf_counter_read(struct perf_counter *counter)
  571. {
  572. /*
  573. * If counter is enabled and currently active on a CPU, update the
  574. * value in the counter structure:
  575. */
  576. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  577. smp_call_function_single(counter->oncpu,
  578. __read, counter, 1);
  579. }
  580. return atomic64_read(&counter->count);
  581. }
  582. /*
  583. * Cross CPU call to switch performance data pointers
  584. */
  585. static void __perf_switch_irq_data(void *info)
  586. {
  587. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  588. struct perf_counter *counter = info;
  589. struct perf_counter_context *ctx = counter->ctx;
  590. struct perf_data *oldirqdata = counter->irqdata;
  591. /*
  592. * If this is a task context, we need to check whether it is
  593. * the current task context of this cpu. If not it has been
  594. * scheduled out before the smp call arrived.
  595. */
  596. if (ctx->task) {
  597. if (cpuctx->task_ctx != ctx)
  598. return;
  599. spin_lock(&ctx->lock);
  600. }
  601. /* Change the pointer NMI safe */
  602. atomic_long_set((atomic_long_t *)&counter->irqdata,
  603. (unsigned long) counter->usrdata);
  604. counter->usrdata = oldirqdata;
  605. if (ctx->task)
  606. spin_unlock(&ctx->lock);
  607. }
  608. static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
  609. {
  610. struct perf_counter_context *ctx = counter->ctx;
  611. struct perf_data *oldirqdata = counter->irqdata;
  612. struct task_struct *task = ctx->task;
  613. if (!task) {
  614. smp_call_function_single(counter->cpu,
  615. __perf_switch_irq_data,
  616. counter, 1);
  617. return counter->usrdata;
  618. }
  619. retry:
  620. spin_lock_irq(&ctx->lock);
  621. if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
  622. counter->irqdata = counter->usrdata;
  623. counter->usrdata = oldirqdata;
  624. spin_unlock_irq(&ctx->lock);
  625. return oldirqdata;
  626. }
  627. spin_unlock_irq(&ctx->lock);
  628. task_oncpu_function_call(task, __perf_switch_irq_data, counter);
  629. /* Might have failed, because task was scheduled out */
  630. if (counter->irqdata == oldirqdata)
  631. goto retry;
  632. return counter->usrdata;
  633. }
  634. static void put_context(struct perf_counter_context *ctx)
  635. {
  636. if (ctx->task)
  637. put_task_struct(ctx->task);
  638. }
  639. static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
  640. {
  641. struct perf_cpu_context *cpuctx;
  642. struct perf_counter_context *ctx;
  643. struct task_struct *task;
  644. /*
  645. * If cpu is not a wildcard then this is a percpu counter:
  646. */
  647. if (cpu != -1) {
  648. /* Must be root to operate on a CPU counter: */
  649. if (!capable(CAP_SYS_ADMIN))
  650. return ERR_PTR(-EACCES);
  651. if (cpu < 0 || cpu > num_possible_cpus())
  652. return ERR_PTR(-EINVAL);
  653. /*
  654. * We could be clever and allow to attach a counter to an
  655. * offline CPU and activate it when the CPU comes up, but
  656. * that's for later.
  657. */
  658. if (!cpu_isset(cpu, cpu_online_map))
  659. return ERR_PTR(-ENODEV);
  660. cpuctx = &per_cpu(perf_cpu_context, cpu);
  661. ctx = &cpuctx->ctx;
  662. return ctx;
  663. }
  664. rcu_read_lock();
  665. if (!pid)
  666. task = current;
  667. else
  668. task = find_task_by_vpid(pid);
  669. if (task)
  670. get_task_struct(task);
  671. rcu_read_unlock();
  672. if (!task)
  673. return ERR_PTR(-ESRCH);
  674. ctx = &task->perf_counter_ctx;
  675. ctx->task = task;
  676. /* Reuse ptrace permission checks for now. */
  677. if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
  678. put_context(ctx);
  679. return ERR_PTR(-EACCES);
  680. }
  681. return ctx;
  682. }
  683. /*
  684. * Called when the last reference to the file is gone.
  685. */
  686. static int perf_release(struct inode *inode, struct file *file)
  687. {
  688. struct perf_counter *counter = file->private_data;
  689. struct perf_counter_context *ctx = counter->ctx;
  690. file->private_data = NULL;
  691. mutex_lock(&counter->mutex);
  692. perf_counter_remove_from_context(counter);
  693. put_context(ctx);
  694. mutex_unlock(&counter->mutex);
  695. kfree(counter);
  696. return 0;
  697. }
  698. /*
  699. * Read the performance counter - simple non blocking version for now
  700. */
  701. static ssize_t
  702. perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
  703. {
  704. u64 cntval;
  705. if (count != sizeof(cntval))
  706. return -EINVAL;
  707. mutex_lock(&counter->mutex);
  708. cntval = perf_counter_read(counter);
  709. mutex_unlock(&counter->mutex);
  710. return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
  711. }
  712. static ssize_t
  713. perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
  714. {
  715. if (!usrdata->len)
  716. return 0;
  717. count = min(count, (size_t)usrdata->len);
  718. if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
  719. return -EFAULT;
  720. /* Adjust the counters */
  721. usrdata->len -= count;
  722. if (!usrdata->len)
  723. usrdata->rd_idx = 0;
  724. else
  725. usrdata->rd_idx += count;
  726. return count;
  727. }
  728. static ssize_t
  729. perf_read_irq_data(struct perf_counter *counter,
  730. char __user *buf,
  731. size_t count,
  732. int nonblocking)
  733. {
  734. struct perf_data *irqdata, *usrdata;
  735. DECLARE_WAITQUEUE(wait, current);
  736. ssize_t res;
  737. irqdata = counter->irqdata;
  738. usrdata = counter->usrdata;
  739. if (usrdata->len + irqdata->len >= count)
  740. goto read_pending;
  741. if (nonblocking)
  742. return -EAGAIN;
  743. spin_lock_irq(&counter->waitq.lock);
  744. __add_wait_queue(&counter->waitq, &wait);
  745. for (;;) {
  746. set_current_state(TASK_INTERRUPTIBLE);
  747. if (usrdata->len + irqdata->len >= count)
  748. break;
  749. if (signal_pending(current))
  750. break;
  751. spin_unlock_irq(&counter->waitq.lock);
  752. schedule();
  753. spin_lock_irq(&counter->waitq.lock);
  754. }
  755. __remove_wait_queue(&counter->waitq, &wait);
  756. __set_current_state(TASK_RUNNING);
  757. spin_unlock_irq(&counter->waitq.lock);
  758. if (usrdata->len + irqdata->len < count)
  759. return -ERESTARTSYS;
  760. read_pending:
  761. mutex_lock(&counter->mutex);
  762. /* Drain pending data first: */
  763. res = perf_copy_usrdata(usrdata, buf, count);
  764. if (res < 0 || res == count)
  765. goto out;
  766. /* Switch irq buffer: */
  767. usrdata = perf_switch_irq_data(counter);
  768. if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) {
  769. if (!res)
  770. res = -EFAULT;
  771. } else {
  772. res = count;
  773. }
  774. out:
  775. mutex_unlock(&counter->mutex);
  776. return res;
  777. }
  778. static ssize_t
  779. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  780. {
  781. struct perf_counter *counter = file->private_data;
  782. switch (counter->hw_event.record_type) {
  783. case PERF_RECORD_SIMPLE:
  784. return perf_read_hw(counter, buf, count);
  785. case PERF_RECORD_IRQ:
  786. case PERF_RECORD_GROUP:
  787. return perf_read_irq_data(counter, buf, count,
  788. file->f_flags & O_NONBLOCK);
  789. }
  790. return -EINVAL;
  791. }
  792. static unsigned int perf_poll(struct file *file, poll_table *wait)
  793. {
  794. struct perf_counter *counter = file->private_data;
  795. unsigned int events = 0;
  796. unsigned long flags;
  797. poll_wait(file, &counter->waitq, wait);
  798. spin_lock_irqsave(&counter->waitq.lock, flags);
  799. if (counter->usrdata->len || counter->irqdata->len)
  800. events |= POLLIN;
  801. spin_unlock_irqrestore(&counter->waitq.lock, flags);
  802. return events;
  803. }
  804. static const struct file_operations perf_fops = {
  805. .release = perf_release,
  806. .read = perf_read,
  807. .poll = perf_poll,
  808. };
  809. static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
  810. {
  811. int cpu = raw_smp_processor_id();
  812. atomic64_set(&counter->hw.prev_count, cpu_clock(cpu));
  813. return 0;
  814. }
  815. static void cpu_clock_perf_counter_update(struct perf_counter *counter)
  816. {
  817. int cpu = raw_smp_processor_id();
  818. s64 prev;
  819. u64 now;
  820. now = cpu_clock(cpu);
  821. prev = atomic64_read(&counter->hw.prev_count);
  822. atomic64_set(&counter->hw.prev_count, now);
  823. atomic64_add(now - prev, &counter->count);
  824. }
  825. static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
  826. {
  827. cpu_clock_perf_counter_update(counter);
  828. }
  829. static void cpu_clock_perf_counter_read(struct perf_counter *counter)
  830. {
  831. cpu_clock_perf_counter_update(counter);
  832. }
  833. static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
  834. .enable = cpu_clock_perf_counter_enable,
  835. .disable = cpu_clock_perf_counter_disable,
  836. .read = cpu_clock_perf_counter_read,
  837. };
  838. /*
  839. * Called from within the scheduler:
  840. */
  841. static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
  842. {
  843. struct task_struct *curr = counter->task;
  844. u64 delta;
  845. delta = __task_delta_exec(curr, update);
  846. return curr->se.sum_exec_runtime + delta;
  847. }
  848. static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
  849. {
  850. u64 prev;
  851. s64 delta;
  852. prev = atomic64_read(&counter->hw.prev_count);
  853. atomic64_set(&counter->hw.prev_count, now);
  854. delta = now - prev;
  855. atomic64_add(delta, &counter->count);
  856. }
  857. static void task_clock_perf_counter_read(struct perf_counter *counter)
  858. {
  859. u64 now = task_clock_perf_counter_val(counter, 1);
  860. task_clock_perf_counter_update(counter, now);
  861. }
  862. static int task_clock_perf_counter_enable(struct perf_counter *counter)
  863. {
  864. u64 now = task_clock_perf_counter_val(counter, 0);
  865. atomic64_set(&counter->hw.prev_count, now);
  866. return 0;
  867. }
  868. static void task_clock_perf_counter_disable(struct perf_counter *counter)
  869. {
  870. u64 now = task_clock_perf_counter_val(counter, 0);
  871. task_clock_perf_counter_update(counter, now);
  872. }
  873. static const struct hw_perf_counter_ops perf_ops_task_clock = {
  874. .enable = task_clock_perf_counter_enable,
  875. .disable = task_clock_perf_counter_disable,
  876. .read = task_clock_perf_counter_read,
  877. };
  878. static u64 get_page_faults(void)
  879. {
  880. struct task_struct *curr = current;
  881. return curr->maj_flt + curr->min_flt;
  882. }
  883. static void page_faults_perf_counter_update(struct perf_counter *counter)
  884. {
  885. u64 prev, now;
  886. s64 delta;
  887. prev = atomic64_read(&counter->hw.prev_count);
  888. now = get_page_faults();
  889. atomic64_set(&counter->hw.prev_count, now);
  890. delta = now - prev;
  891. atomic64_add(delta, &counter->count);
  892. }
  893. static void page_faults_perf_counter_read(struct perf_counter *counter)
  894. {
  895. page_faults_perf_counter_update(counter);
  896. }
  897. static int page_faults_perf_counter_enable(struct perf_counter *counter)
  898. {
  899. /*
  900. * page-faults is a per-task value already,
  901. * so we dont have to clear it on switch-in.
  902. */
  903. return 0;
  904. }
  905. static void page_faults_perf_counter_disable(struct perf_counter *counter)
  906. {
  907. page_faults_perf_counter_update(counter);
  908. }
  909. static const struct hw_perf_counter_ops perf_ops_page_faults = {
  910. .enable = page_faults_perf_counter_enable,
  911. .disable = page_faults_perf_counter_disable,
  912. .read = page_faults_perf_counter_read,
  913. };
  914. static u64 get_context_switches(void)
  915. {
  916. struct task_struct *curr = current;
  917. return curr->nvcsw + curr->nivcsw;
  918. }
  919. static void context_switches_perf_counter_update(struct perf_counter *counter)
  920. {
  921. u64 prev, now;
  922. s64 delta;
  923. prev = atomic64_read(&counter->hw.prev_count);
  924. now = get_context_switches();
  925. atomic64_set(&counter->hw.prev_count, now);
  926. delta = now - prev;
  927. atomic64_add(delta, &counter->count);
  928. }
  929. static void context_switches_perf_counter_read(struct perf_counter *counter)
  930. {
  931. context_switches_perf_counter_update(counter);
  932. }
  933. static int context_switches_perf_counter_enable(struct perf_counter *counter)
  934. {
  935. /*
  936. * ->nvcsw + curr->nivcsw is a per-task value already,
  937. * so we dont have to clear it on switch-in.
  938. */
  939. return 0;
  940. }
  941. static void context_switches_perf_counter_disable(struct perf_counter *counter)
  942. {
  943. context_switches_perf_counter_update(counter);
  944. }
  945. static const struct hw_perf_counter_ops perf_ops_context_switches = {
  946. .enable = context_switches_perf_counter_enable,
  947. .disable = context_switches_perf_counter_disable,
  948. .read = context_switches_perf_counter_read,
  949. };
  950. static inline u64 get_cpu_migrations(void)
  951. {
  952. return current->se.nr_migrations;
  953. }
  954. static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
  955. {
  956. u64 prev, now;
  957. s64 delta;
  958. prev = atomic64_read(&counter->hw.prev_count);
  959. now = get_cpu_migrations();
  960. atomic64_set(&counter->hw.prev_count, now);
  961. delta = now - prev;
  962. atomic64_add(delta, &counter->count);
  963. }
  964. static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
  965. {
  966. cpu_migrations_perf_counter_update(counter);
  967. }
  968. static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
  969. {
  970. /*
  971. * se.nr_migrations is a per-task value already,
  972. * so we dont have to clear it on switch-in.
  973. */
  974. return 0;
  975. }
  976. static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
  977. {
  978. cpu_migrations_perf_counter_update(counter);
  979. }
  980. static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
  981. .enable = cpu_migrations_perf_counter_enable,
  982. .disable = cpu_migrations_perf_counter_disable,
  983. .read = cpu_migrations_perf_counter_read,
  984. };
  985. static const struct hw_perf_counter_ops *
  986. sw_perf_counter_init(struct perf_counter *counter)
  987. {
  988. const struct hw_perf_counter_ops *hw_ops = NULL;
  989. switch (counter->hw_event.type) {
  990. case PERF_COUNT_CPU_CLOCK:
  991. hw_ops = &perf_ops_cpu_clock;
  992. break;
  993. case PERF_COUNT_TASK_CLOCK:
  994. hw_ops = &perf_ops_task_clock;
  995. break;
  996. case PERF_COUNT_PAGE_FAULTS:
  997. hw_ops = &perf_ops_page_faults;
  998. break;
  999. case PERF_COUNT_CONTEXT_SWITCHES:
  1000. hw_ops = &perf_ops_context_switches;
  1001. break;
  1002. case PERF_COUNT_CPU_MIGRATIONS:
  1003. hw_ops = &perf_ops_cpu_migrations;
  1004. break;
  1005. default:
  1006. break;
  1007. }
  1008. return hw_ops;
  1009. }
  1010. /*
  1011. * Allocate and initialize a counter structure
  1012. */
  1013. static struct perf_counter *
  1014. perf_counter_alloc(struct perf_counter_hw_event *hw_event,
  1015. int cpu,
  1016. struct perf_counter *group_leader,
  1017. gfp_t gfpflags)
  1018. {
  1019. const struct hw_perf_counter_ops *hw_ops;
  1020. struct perf_counter *counter;
  1021. counter = kzalloc(sizeof(*counter), gfpflags);
  1022. if (!counter)
  1023. return NULL;
  1024. /*
  1025. * Single counters are their own group leaders, with an
  1026. * empty sibling list:
  1027. */
  1028. if (!group_leader)
  1029. group_leader = counter;
  1030. mutex_init(&counter->mutex);
  1031. INIT_LIST_HEAD(&counter->list_entry);
  1032. INIT_LIST_HEAD(&counter->sibling_list);
  1033. init_waitqueue_head(&counter->waitq);
  1034. counter->irqdata = &counter->data[0];
  1035. counter->usrdata = &counter->data[1];
  1036. counter->cpu = cpu;
  1037. counter->hw_event = *hw_event;
  1038. counter->wakeup_pending = 0;
  1039. counter->group_leader = group_leader;
  1040. counter->hw_ops = NULL;
  1041. counter->state = PERF_COUNTER_STATE_INACTIVE;
  1042. if (hw_event->disabled)
  1043. counter->state = PERF_COUNTER_STATE_OFF;
  1044. hw_ops = NULL;
  1045. if (!hw_event->raw && hw_event->type < 0)
  1046. hw_ops = sw_perf_counter_init(counter);
  1047. if (!hw_ops)
  1048. hw_ops = hw_perf_counter_init(counter);
  1049. if (!hw_ops) {
  1050. kfree(counter);
  1051. return NULL;
  1052. }
  1053. counter->hw_ops = hw_ops;
  1054. return counter;
  1055. }
  1056. /**
  1057. * sys_perf_task_open - open a performance counter, associate it to a task/cpu
  1058. *
  1059. * @hw_event_uptr: event type attributes for monitoring/sampling
  1060. * @pid: target pid
  1061. * @cpu: target cpu
  1062. * @group_fd: group leader counter fd
  1063. */
  1064. asmlinkage int
  1065. sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user,
  1066. pid_t pid, int cpu, int group_fd)
  1067. {
  1068. struct perf_counter *counter, *group_leader;
  1069. struct perf_counter_hw_event hw_event;
  1070. struct perf_counter_context *ctx;
  1071. struct file *counter_file = NULL;
  1072. struct file *group_file = NULL;
  1073. int fput_needed = 0;
  1074. int fput_needed2 = 0;
  1075. int ret;
  1076. if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
  1077. return -EFAULT;
  1078. /*
  1079. * Get the target context (task or percpu):
  1080. */
  1081. ctx = find_get_context(pid, cpu);
  1082. if (IS_ERR(ctx))
  1083. return PTR_ERR(ctx);
  1084. /*
  1085. * Look up the group leader (we will attach this counter to it):
  1086. */
  1087. group_leader = NULL;
  1088. if (group_fd != -1) {
  1089. ret = -EINVAL;
  1090. group_file = fget_light(group_fd, &fput_needed);
  1091. if (!group_file)
  1092. goto err_put_context;
  1093. if (group_file->f_op != &perf_fops)
  1094. goto err_put_context;
  1095. group_leader = group_file->private_data;
  1096. /*
  1097. * Do not allow a recursive hierarchy (this new sibling
  1098. * becoming part of another group-sibling):
  1099. */
  1100. if (group_leader->group_leader != group_leader)
  1101. goto err_put_context;
  1102. /*
  1103. * Do not allow to attach to a group in a different
  1104. * task or CPU context:
  1105. */
  1106. if (group_leader->ctx != ctx)
  1107. goto err_put_context;
  1108. }
  1109. ret = -EINVAL;
  1110. counter = perf_counter_alloc(&hw_event, cpu, group_leader, GFP_KERNEL);
  1111. if (!counter)
  1112. goto err_put_context;
  1113. ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
  1114. if (ret < 0)
  1115. goto err_free_put_context;
  1116. counter_file = fget_light(ret, &fput_needed2);
  1117. if (!counter_file)
  1118. goto err_free_put_context;
  1119. counter->filp = counter_file;
  1120. perf_install_in_context(ctx, counter, cpu);
  1121. fput_light(counter_file, fput_needed2);
  1122. out_fput:
  1123. fput_light(group_file, fput_needed);
  1124. return ret;
  1125. err_free_put_context:
  1126. kfree(counter);
  1127. err_put_context:
  1128. put_context(ctx);
  1129. goto out_fput;
  1130. }
  1131. /*
  1132. * Initialize the perf_counter context in a task_struct:
  1133. */
  1134. static void
  1135. __perf_counter_init_context(struct perf_counter_context *ctx,
  1136. struct task_struct *task)
  1137. {
  1138. memset(ctx, 0, sizeof(*ctx));
  1139. spin_lock_init(&ctx->lock);
  1140. INIT_LIST_HEAD(&ctx->counter_list);
  1141. ctx->task = task;
  1142. }
  1143. /*
  1144. * inherit a counter from parent task to child task:
  1145. */
  1146. static int
  1147. inherit_counter(struct perf_counter *parent_counter,
  1148. struct task_struct *parent,
  1149. struct perf_counter_context *parent_ctx,
  1150. struct task_struct *child,
  1151. struct perf_counter_context *child_ctx)
  1152. {
  1153. struct perf_counter *child_counter;
  1154. child_counter = perf_counter_alloc(&parent_counter->hw_event,
  1155. parent_counter->cpu, NULL,
  1156. GFP_ATOMIC);
  1157. if (!child_counter)
  1158. return -ENOMEM;
  1159. /*
  1160. * Link it up in the child's context:
  1161. */
  1162. child_counter->ctx = child_ctx;
  1163. child_counter->task = child;
  1164. list_add_counter(child_counter, child_ctx);
  1165. child_ctx->nr_counters++;
  1166. child_counter->parent = parent_counter;
  1167. /*
  1168. * inherit into child's child as well:
  1169. */
  1170. child_counter->hw_event.inherit = 1;
  1171. /*
  1172. * Get a reference to the parent filp - we will fput it
  1173. * when the child counter exits. This is safe to do because
  1174. * we are in the parent and we know that the filp still
  1175. * exists and has a nonzero count:
  1176. */
  1177. atomic_long_inc(&parent_counter->filp->f_count);
  1178. return 0;
  1179. }
  1180. static void
  1181. __perf_counter_exit_task(struct task_struct *child,
  1182. struct perf_counter *child_counter,
  1183. struct perf_counter_context *child_ctx)
  1184. {
  1185. struct perf_counter *parent_counter;
  1186. u64 parent_val, child_val;
  1187. /*
  1188. * If we do not self-reap then we have to wait for the
  1189. * child task to unschedule (it will happen for sure),
  1190. * so that its counter is at its final count. (This
  1191. * condition triggers rarely - child tasks usually get
  1192. * off their CPU before the parent has a chance to
  1193. * get this far into the reaping action)
  1194. */
  1195. if (child != current) {
  1196. wait_task_inactive(child, 0);
  1197. list_del_init(&child_counter->list_entry);
  1198. } else {
  1199. struct perf_cpu_context *cpuctx;
  1200. unsigned long flags;
  1201. u64 perf_flags;
  1202. /*
  1203. * Disable and unlink this counter.
  1204. *
  1205. * Be careful about zapping the list - IRQ/NMI context
  1206. * could still be processing it:
  1207. */
  1208. curr_rq_lock_irq_save(&flags);
  1209. perf_flags = hw_perf_save_disable();
  1210. cpuctx = &__get_cpu_var(perf_cpu_context);
  1211. if (child_counter->state == PERF_COUNTER_STATE_ACTIVE) {
  1212. child_counter->state = PERF_COUNTER_STATE_INACTIVE;
  1213. child_counter->hw_ops->disable(child_counter);
  1214. cpuctx->active_oncpu--;
  1215. child_ctx->nr_active--;
  1216. child_counter->oncpu = -1;
  1217. }
  1218. list_del_init(&child_counter->list_entry);
  1219. child_ctx->nr_counters--;
  1220. hw_perf_restore(perf_flags);
  1221. curr_rq_unlock_irq_restore(&flags);
  1222. }
  1223. parent_counter = child_counter->parent;
  1224. /*
  1225. * It can happen that parent exits first, and has counters
  1226. * that are still around due to the child reference. These
  1227. * counters need to be zapped - but otherwise linger.
  1228. */
  1229. if (!parent_counter)
  1230. return;
  1231. parent_val = atomic64_read(&parent_counter->count);
  1232. child_val = atomic64_read(&child_counter->count);
  1233. /*
  1234. * Add back the child's count to the parent's count:
  1235. */
  1236. atomic64_add(child_val, &parent_counter->count);
  1237. fput(parent_counter->filp);
  1238. kfree(child_counter);
  1239. }
  1240. /*
  1241. * When a child task exist, feed back counter values to parent counters.
  1242. *
  1243. * Note: we are running in child context, but the PID is not hashed
  1244. * anymore so new counters will not be added.
  1245. */
  1246. void perf_counter_exit_task(struct task_struct *child)
  1247. {
  1248. struct perf_counter *child_counter, *tmp;
  1249. struct perf_counter_context *child_ctx;
  1250. child_ctx = &child->perf_counter_ctx;
  1251. if (likely(!child_ctx->nr_counters))
  1252. return;
  1253. list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
  1254. list_entry)
  1255. __perf_counter_exit_task(child, child_counter, child_ctx);
  1256. }
  1257. /*
  1258. * Initialize the perf_counter context in task_struct
  1259. */
  1260. void perf_counter_init_task(struct task_struct *child)
  1261. {
  1262. struct perf_counter_context *child_ctx, *parent_ctx;
  1263. struct perf_counter *counter, *parent_counter;
  1264. struct task_struct *parent = current;
  1265. unsigned long flags;
  1266. child_ctx = &child->perf_counter_ctx;
  1267. parent_ctx = &parent->perf_counter_ctx;
  1268. __perf_counter_init_context(child_ctx, child);
  1269. /*
  1270. * This is executed from the parent task context, so inherit
  1271. * counters that have been marked for cloning:
  1272. */
  1273. if (likely(!parent_ctx->nr_counters))
  1274. return;
  1275. /*
  1276. * Lock the parent list. No need to lock the child - not PID
  1277. * hashed yet and not running, so nobody can access it.
  1278. */
  1279. spin_lock_irqsave(&parent_ctx->lock, flags);
  1280. /*
  1281. * We dont have to disable NMIs - we are only looking at
  1282. * the list, not manipulating it:
  1283. */
  1284. list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
  1285. if (!counter->hw_event.inherit || counter->group_leader != counter)
  1286. continue;
  1287. /*
  1288. * Instead of creating recursive hierarchies of counters,
  1289. * we link inheritd counters back to the original parent,
  1290. * which has a filp for sure, which we use as the reference
  1291. * count:
  1292. */
  1293. parent_counter = counter;
  1294. if (counter->parent)
  1295. parent_counter = counter->parent;
  1296. if (inherit_counter(parent_counter, parent,
  1297. parent_ctx, child, child_ctx))
  1298. break;
  1299. }
  1300. spin_unlock_irqrestore(&parent_ctx->lock, flags);
  1301. }
  1302. static void __cpuinit perf_counter_init_cpu(int cpu)
  1303. {
  1304. struct perf_cpu_context *cpuctx;
  1305. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1306. __perf_counter_init_context(&cpuctx->ctx, NULL);
  1307. mutex_lock(&perf_resource_mutex);
  1308. cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
  1309. mutex_unlock(&perf_resource_mutex);
  1310. hw_perf_counter_setup();
  1311. }
  1312. #ifdef CONFIG_HOTPLUG_CPU
  1313. static void __perf_counter_exit_cpu(void *info)
  1314. {
  1315. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1316. struct perf_counter_context *ctx = &cpuctx->ctx;
  1317. struct perf_counter *counter, *tmp;
  1318. list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
  1319. __perf_counter_remove_from_context(counter);
  1320. }
  1321. static void perf_counter_exit_cpu(int cpu)
  1322. {
  1323. smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
  1324. }
  1325. #else
  1326. static inline void perf_counter_exit_cpu(int cpu) { }
  1327. #endif
  1328. static int __cpuinit
  1329. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  1330. {
  1331. unsigned int cpu = (long)hcpu;
  1332. switch (action) {
  1333. case CPU_UP_PREPARE:
  1334. case CPU_UP_PREPARE_FROZEN:
  1335. perf_counter_init_cpu(cpu);
  1336. break;
  1337. case CPU_DOWN_PREPARE:
  1338. case CPU_DOWN_PREPARE_FROZEN:
  1339. perf_counter_exit_cpu(cpu);
  1340. break;
  1341. default:
  1342. break;
  1343. }
  1344. return NOTIFY_OK;
  1345. }
  1346. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  1347. .notifier_call = perf_cpu_notify,
  1348. };
  1349. static int __init perf_counter_init(void)
  1350. {
  1351. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  1352. (void *)(long)smp_processor_id());
  1353. register_cpu_notifier(&perf_cpu_nb);
  1354. return 0;
  1355. }
  1356. early_initcall(perf_counter_init);
  1357. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  1358. {
  1359. return sprintf(buf, "%d\n", perf_reserved_percpu);
  1360. }
  1361. static ssize_t
  1362. perf_set_reserve_percpu(struct sysdev_class *class,
  1363. const char *buf,
  1364. size_t count)
  1365. {
  1366. struct perf_cpu_context *cpuctx;
  1367. unsigned long val;
  1368. int err, cpu, mpt;
  1369. err = strict_strtoul(buf, 10, &val);
  1370. if (err)
  1371. return err;
  1372. if (val > perf_max_counters)
  1373. return -EINVAL;
  1374. mutex_lock(&perf_resource_mutex);
  1375. perf_reserved_percpu = val;
  1376. for_each_online_cpu(cpu) {
  1377. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1378. spin_lock_irq(&cpuctx->ctx.lock);
  1379. mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
  1380. perf_max_counters - perf_reserved_percpu);
  1381. cpuctx->max_pertask = mpt;
  1382. spin_unlock_irq(&cpuctx->ctx.lock);
  1383. }
  1384. mutex_unlock(&perf_resource_mutex);
  1385. return count;
  1386. }
  1387. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  1388. {
  1389. return sprintf(buf, "%d\n", perf_overcommit);
  1390. }
  1391. static ssize_t
  1392. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  1393. {
  1394. unsigned long val;
  1395. int err;
  1396. err = strict_strtoul(buf, 10, &val);
  1397. if (err)
  1398. return err;
  1399. if (val > 1)
  1400. return -EINVAL;
  1401. mutex_lock(&perf_resource_mutex);
  1402. perf_overcommit = val;
  1403. mutex_unlock(&perf_resource_mutex);
  1404. return count;
  1405. }
  1406. static SYSDEV_CLASS_ATTR(
  1407. reserve_percpu,
  1408. 0644,
  1409. perf_show_reserve_percpu,
  1410. perf_set_reserve_percpu
  1411. );
  1412. static SYSDEV_CLASS_ATTR(
  1413. overcommit,
  1414. 0644,
  1415. perf_show_overcommit,
  1416. perf_set_overcommit
  1417. );
  1418. static struct attribute *perfclass_attrs[] = {
  1419. &attr_reserve_percpu.attr,
  1420. &attr_overcommit.attr,
  1421. NULL
  1422. };
  1423. static struct attribute_group perfclass_attr_group = {
  1424. .attrs = perfclass_attrs,
  1425. .name = "perf_counters",
  1426. };
  1427. static int __init perf_counter_sysfs_init(void)
  1428. {
  1429. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  1430. &perfclass_attr_group);
  1431. }
  1432. device_initcall(perf_counter_sysfs_init);