perf_counter.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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/perf_counter.h>
  21. /*
  22. * Each CPU has a list of per CPU counters:
  23. */
  24. DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
  25. int perf_max_counters __read_mostly;
  26. static int perf_reserved_percpu __read_mostly;
  27. static int perf_overcommit __read_mostly = 1;
  28. /*
  29. * Mutex for (sysadmin-configurable) counter reservations:
  30. */
  31. static DEFINE_MUTEX(perf_resource_mutex);
  32. /*
  33. * Architecture provided APIs - weak aliases:
  34. */
  35. extern __weak const struct hw_perf_counter_ops *
  36. hw_perf_counter_init(struct perf_counter *counter)
  37. {
  38. return ERR_PTR(-EINVAL);
  39. }
  40. u64 __weak hw_perf_save_disable(void) { return 0; }
  41. void __weak hw_perf_restore(u64 ctrl) { }
  42. void __weak hw_perf_counter_setup(void) { }
  43. #if BITS_PER_LONG == 64
  44. /*
  45. * Read the cached counter in counter safe against cross CPU / NMI
  46. * modifications. 64 bit version - no complications.
  47. */
  48. static inline u64 perf_counter_read_safe(struct perf_counter *counter)
  49. {
  50. return (u64) atomic64_read(&counter->count);
  51. }
  52. void atomic64_counter_set(struct perf_counter *counter, u64 val)
  53. {
  54. atomic64_set(&counter->count, val);
  55. }
  56. u64 atomic64_counter_read(struct perf_counter *counter)
  57. {
  58. return atomic64_read(&counter->count);
  59. }
  60. #else
  61. /*
  62. * Read the cached counter in counter safe against cross CPU / NMI
  63. * modifications. 32 bit version.
  64. */
  65. static u64 perf_counter_read_safe(struct perf_counter *counter)
  66. {
  67. u32 cntl, cnth;
  68. local_irq_disable();
  69. do {
  70. cnth = atomic_read(&counter->count32[1]);
  71. cntl = atomic_read(&counter->count32[0]);
  72. } while (cnth != atomic_read(&counter->count32[1]));
  73. local_irq_enable();
  74. return cntl | ((u64) cnth) << 32;
  75. }
  76. void atomic64_counter_set(struct perf_counter *counter, u64 val64)
  77. {
  78. u32 *val32 = (void *)&val64;
  79. atomic_set(counter->count32 + 0, *(val32 + 0));
  80. atomic_set(counter->count32 + 1, *(val32 + 1));
  81. }
  82. u64 atomic64_counter_read(struct perf_counter *counter)
  83. {
  84. return atomic_read(counter->count32 + 0) |
  85. (u64) atomic_read(counter->count32 + 1) << 32;
  86. }
  87. #endif
  88. static void
  89. list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  90. {
  91. struct perf_counter *group_leader = counter->group_leader;
  92. /*
  93. * Depending on whether it is a standalone or sibling counter,
  94. * add it straight to the context's counter list, or to the group
  95. * leader's sibling list:
  96. */
  97. if (counter->group_leader == counter)
  98. list_add_tail(&counter->list_entry, &ctx->counter_list);
  99. else
  100. list_add_tail(&counter->list_entry, &group_leader->sibling_list);
  101. }
  102. static void
  103. list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  104. {
  105. struct perf_counter *sibling, *tmp;
  106. list_del_init(&counter->list_entry);
  107. /*
  108. * If this was a group counter with sibling counters then
  109. * upgrade the siblings to singleton counters by adding them
  110. * to the context list directly:
  111. */
  112. list_for_each_entry_safe(sibling, tmp,
  113. &counter->sibling_list, list_entry) {
  114. list_del_init(&sibling->list_entry);
  115. list_add_tail(&sibling->list_entry, &ctx->counter_list);
  116. WARN_ON_ONCE(!sibling->group_leader);
  117. WARN_ON_ONCE(sibling->group_leader == sibling);
  118. sibling->group_leader = sibling;
  119. }
  120. }
  121. /*
  122. * Cross CPU call to remove a performance counter
  123. *
  124. * We disable the counter on the hardware level first. After that we
  125. * remove it from the context list.
  126. */
  127. static void __perf_counter_remove_from_context(void *info)
  128. {
  129. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  130. struct perf_counter *counter = info;
  131. struct perf_counter_context *ctx = counter->ctx;
  132. u64 perf_flags;
  133. /*
  134. * If this is a task context, we need to check whether it is
  135. * the current task context of this cpu. If not it has been
  136. * scheduled out before the smp call arrived.
  137. */
  138. if (ctx->task && cpuctx->task_ctx != ctx)
  139. return;
  140. spin_lock(&ctx->lock);
  141. if (counter->active) {
  142. counter->hw_ops->hw_perf_counter_disable(counter);
  143. counter->active = 0;
  144. ctx->nr_active--;
  145. cpuctx->active_oncpu--;
  146. counter->task = NULL;
  147. }
  148. ctx->nr_counters--;
  149. /*
  150. * Protect the list operation against NMI by disabling the
  151. * counters on a global level. NOP for non NMI based counters.
  152. */
  153. perf_flags = hw_perf_save_disable();
  154. list_del_counter(counter, ctx);
  155. hw_perf_restore(perf_flags);
  156. if (!ctx->task) {
  157. /*
  158. * Allow more per task counters with respect to the
  159. * reservation:
  160. */
  161. cpuctx->max_pertask =
  162. min(perf_max_counters - ctx->nr_counters,
  163. perf_max_counters - perf_reserved_percpu);
  164. }
  165. spin_unlock(&ctx->lock);
  166. }
  167. /*
  168. * Remove the counter from a task's (or a CPU's) list of counters.
  169. *
  170. * Must be called with counter->mutex held.
  171. *
  172. * CPU counters are removed with a smp call. For task counters we only
  173. * call when the task is on a CPU.
  174. */
  175. static void perf_counter_remove_from_context(struct perf_counter *counter)
  176. {
  177. struct perf_counter_context *ctx = counter->ctx;
  178. struct task_struct *task = ctx->task;
  179. if (!task) {
  180. /*
  181. * Per cpu counters are removed via an smp call and
  182. * the removal is always sucessful.
  183. */
  184. smp_call_function_single(counter->cpu,
  185. __perf_counter_remove_from_context,
  186. counter, 1);
  187. return;
  188. }
  189. retry:
  190. task_oncpu_function_call(task, __perf_counter_remove_from_context,
  191. counter);
  192. spin_lock_irq(&ctx->lock);
  193. /*
  194. * If the context is active we need to retry the smp call.
  195. */
  196. if (ctx->nr_active && !list_empty(&counter->list_entry)) {
  197. spin_unlock_irq(&ctx->lock);
  198. goto retry;
  199. }
  200. /*
  201. * The lock prevents that this context is scheduled in so we
  202. * can remove the counter safely, if the call above did not
  203. * succeed.
  204. */
  205. if (!list_empty(&counter->list_entry)) {
  206. ctx->nr_counters--;
  207. list_del_counter(counter, ctx);
  208. counter->task = NULL;
  209. }
  210. spin_unlock_irq(&ctx->lock);
  211. }
  212. /*
  213. * Cross CPU call to install and enable a preformance counter
  214. */
  215. static void __perf_install_in_context(void *info)
  216. {
  217. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  218. struct perf_counter *counter = info;
  219. struct perf_counter_context *ctx = counter->ctx;
  220. int cpu = smp_processor_id();
  221. u64 perf_flags;
  222. /*
  223. * If this is a task context, we need to check whether it is
  224. * the current task context of this cpu. If not it has been
  225. * scheduled out before the smp call arrived.
  226. */
  227. if (ctx->task && cpuctx->task_ctx != ctx)
  228. return;
  229. spin_lock(&ctx->lock);
  230. /*
  231. * Protect the list operation against NMI by disabling the
  232. * counters on a global level. NOP for non NMI based counters.
  233. */
  234. perf_flags = hw_perf_save_disable();
  235. list_add_counter(counter, ctx);
  236. hw_perf_restore(perf_flags);
  237. ctx->nr_counters++;
  238. if (cpuctx->active_oncpu < perf_max_counters) {
  239. counter->hw_ops->hw_perf_counter_enable(counter);
  240. counter->active = 1;
  241. counter->oncpu = cpu;
  242. ctx->nr_active++;
  243. cpuctx->active_oncpu++;
  244. }
  245. if (!ctx->task && cpuctx->max_pertask)
  246. cpuctx->max_pertask--;
  247. spin_unlock(&ctx->lock);
  248. }
  249. /*
  250. * Attach a performance counter to a context
  251. *
  252. * First we add the counter to the list with the hardware enable bit
  253. * in counter->hw_config cleared.
  254. *
  255. * If the counter is attached to a task which is on a CPU we use a smp
  256. * call to enable it in the task context. The task might have been
  257. * scheduled away, but we check this in the smp call again.
  258. */
  259. static void
  260. perf_install_in_context(struct perf_counter_context *ctx,
  261. struct perf_counter *counter,
  262. int cpu)
  263. {
  264. struct task_struct *task = ctx->task;
  265. counter->ctx = ctx;
  266. if (!task) {
  267. /*
  268. * Per cpu counters are installed via an smp call and
  269. * the install is always sucessful.
  270. */
  271. smp_call_function_single(cpu, __perf_install_in_context,
  272. counter, 1);
  273. return;
  274. }
  275. counter->task = task;
  276. retry:
  277. task_oncpu_function_call(task, __perf_install_in_context,
  278. counter);
  279. spin_lock_irq(&ctx->lock);
  280. /*
  281. * If the context is active and the counter has not been added
  282. * we need to retry the smp call.
  283. */
  284. if (ctx->nr_active && list_empty(&counter->list_entry)) {
  285. spin_unlock_irq(&ctx->lock);
  286. goto retry;
  287. }
  288. /*
  289. * The lock prevents that this context is scheduled in so we
  290. * can add the counter safely, if it the call above did not
  291. * succeed.
  292. */
  293. if (list_empty(&counter->list_entry)) {
  294. list_add_counter(counter, ctx);
  295. ctx->nr_counters++;
  296. }
  297. spin_unlock_irq(&ctx->lock);
  298. }
  299. static void
  300. counter_sched_out(struct perf_counter *counter,
  301. struct perf_cpu_context *cpuctx,
  302. struct perf_counter_context *ctx)
  303. {
  304. if (!counter->active)
  305. return;
  306. counter->hw_ops->hw_perf_counter_disable(counter);
  307. counter->active = 0;
  308. counter->oncpu = -1;
  309. cpuctx->active_oncpu--;
  310. ctx->nr_active--;
  311. }
  312. static void
  313. group_sched_out(struct perf_counter *group_counter,
  314. struct perf_cpu_context *cpuctx,
  315. struct perf_counter_context *ctx)
  316. {
  317. struct perf_counter *counter;
  318. counter_sched_out(group_counter, cpuctx, ctx);
  319. /*
  320. * Schedule out siblings (if any):
  321. */
  322. list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
  323. counter_sched_out(counter, cpuctx, ctx);
  324. }
  325. /*
  326. * Called from scheduler to remove the counters of the current task,
  327. * with interrupts disabled.
  328. *
  329. * We stop each counter and update the counter value in counter->count.
  330. *
  331. * This does not protect us against NMI, but hw_perf_counter_disable()
  332. * sets the disabled bit in the control field of counter _before_
  333. * accessing the counter control register. If a NMI hits, then it will
  334. * not restart the counter.
  335. */
  336. void perf_counter_task_sched_out(struct task_struct *task, int cpu)
  337. {
  338. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  339. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  340. struct perf_counter *counter;
  341. if (likely(!cpuctx->task_ctx))
  342. return;
  343. spin_lock(&ctx->lock);
  344. if (ctx->nr_active) {
  345. list_for_each_entry(counter, &ctx->counter_list, list_entry)
  346. group_sched_out(counter, cpuctx, ctx);
  347. }
  348. spin_unlock(&ctx->lock);
  349. cpuctx->task_ctx = NULL;
  350. }
  351. static void
  352. counter_sched_in(struct perf_counter *counter,
  353. struct perf_cpu_context *cpuctx,
  354. struct perf_counter_context *ctx,
  355. int cpu)
  356. {
  357. if (counter->active == -1)
  358. return;
  359. counter->hw_ops->hw_perf_counter_enable(counter);
  360. counter->active = 1;
  361. counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
  362. cpuctx->active_oncpu++;
  363. ctx->nr_active++;
  364. }
  365. static void
  366. group_sched_in(struct perf_counter *group_counter,
  367. struct perf_cpu_context *cpuctx,
  368. struct perf_counter_context *ctx,
  369. int cpu)
  370. {
  371. struct perf_counter *counter;
  372. counter_sched_in(group_counter, cpuctx, ctx, cpu);
  373. /*
  374. * Schedule in siblings as one group (if any):
  375. */
  376. list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
  377. counter_sched_in(counter, cpuctx, ctx, cpu);
  378. }
  379. /*
  380. * Called from scheduler to add the counters of the current task
  381. * with interrupts disabled.
  382. *
  383. * We restore the counter value and then enable it.
  384. *
  385. * This does not protect us against NMI, but hw_perf_counter_enable()
  386. * sets the enabled bit in the control field of counter _before_
  387. * accessing the counter control register. If a NMI hits, then it will
  388. * keep the counter running.
  389. */
  390. void perf_counter_task_sched_in(struct task_struct *task, int cpu)
  391. {
  392. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  393. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  394. struct perf_counter *counter;
  395. if (likely(!ctx->nr_counters))
  396. return;
  397. spin_lock(&ctx->lock);
  398. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  399. if (ctx->nr_active == cpuctx->max_pertask)
  400. break;
  401. /*
  402. * Listen to the 'cpu' scheduling filter constraint
  403. * of counters:
  404. */
  405. if (counter->cpu != -1 && counter->cpu != cpu)
  406. continue;
  407. group_sched_in(counter, cpuctx, ctx, cpu);
  408. }
  409. spin_unlock(&ctx->lock);
  410. cpuctx->task_ctx = ctx;
  411. }
  412. int perf_counter_task_disable(void)
  413. {
  414. struct task_struct *curr = current;
  415. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  416. struct perf_counter *counter;
  417. u64 perf_flags;
  418. int cpu;
  419. if (likely(!ctx->nr_counters))
  420. return 0;
  421. local_irq_disable();
  422. cpu = smp_processor_id();
  423. perf_counter_task_sched_out(curr, cpu);
  424. spin_lock(&ctx->lock);
  425. /*
  426. * Disable all the counters:
  427. */
  428. perf_flags = hw_perf_save_disable();
  429. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  430. WARN_ON_ONCE(counter->active == 1);
  431. counter->active = -1;
  432. }
  433. hw_perf_restore(perf_flags);
  434. spin_unlock(&ctx->lock);
  435. local_irq_enable();
  436. return 0;
  437. }
  438. int perf_counter_task_enable(void)
  439. {
  440. struct task_struct *curr = current;
  441. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  442. struct perf_counter *counter;
  443. u64 perf_flags;
  444. int cpu;
  445. if (likely(!ctx->nr_counters))
  446. return 0;
  447. local_irq_disable();
  448. cpu = smp_processor_id();
  449. spin_lock(&ctx->lock);
  450. /*
  451. * Disable all the counters:
  452. */
  453. perf_flags = hw_perf_save_disable();
  454. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  455. if (counter->active != -1)
  456. continue;
  457. counter->active = 0;
  458. }
  459. hw_perf_restore(perf_flags);
  460. spin_unlock(&ctx->lock);
  461. perf_counter_task_sched_in(curr, cpu);
  462. local_irq_enable();
  463. return 0;
  464. }
  465. void perf_counter_task_tick(struct task_struct *curr, int cpu)
  466. {
  467. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  468. struct perf_counter *counter;
  469. u64 perf_flags;
  470. if (likely(!ctx->nr_counters))
  471. return;
  472. perf_counter_task_sched_out(curr, cpu);
  473. spin_lock(&ctx->lock);
  474. /*
  475. * Rotate the first entry last (works just fine for group counters too):
  476. */
  477. perf_flags = hw_perf_save_disable();
  478. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  479. list_del(&counter->list_entry);
  480. list_add_tail(&counter->list_entry, &ctx->counter_list);
  481. break;
  482. }
  483. hw_perf_restore(perf_flags);
  484. spin_unlock(&ctx->lock);
  485. perf_counter_task_sched_in(curr, cpu);
  486. }
  487. /*
  488. * Initialize the perf_counter context in a task_struct:
  489. */
  490. static void
  491. __perf_counter_init_context(struct perf_counter_context *ctx,
  492. struct task_struct *task)
  493. {
  494. spin_lock_init(&ctx->lock);
  495. INIT_LIST_HEAD(&ctx->counter_list);
  496. ctx->nr_counters = 0;
  497. ctx->task = task;
  498. }
  499. /*
  500. * Initialize the perf_counter context in task_struct
  501. */
  502. void perf_counter_init_task(struct task_struct *task)
  503. {
  504. __perf_counter_init_context(&task->perf_counter_ctx, task);
  505. }
  506. /*
  507. * Cross CPU call to read the hardware counter
  508. */
  509. static void __hw_perf_counter_read(void *info)
  510. {
  511. struct perf_counter *counter = info;
  512. counter->hw_ops->hw_perf_counter_read(counter);
  513. }
  514. static u64 perf_counter_read(struct perf_counter *counter)
  515. {
  516. /*
  517. * If counter is enabled and currently active on a CPU, update the
  518. * value in the counter structure:
  519. */
  520. if (counter->active) {
  521. smp_call_function_single(counter->oncpu,
  522. __hw_perf_counter_read, counter, 1);
  523. }
  524. return perf_counter_read_safe(counter);
  525. }
  526. /*
  527. * Cross CPU call to switch performance data pointers
  528. */
  529. static void __perf_switch_irq_data(void *info)
  530. {
  531. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  532. struct perf_counter *counter = info;
  533. struct perf_counter_context *ctx = counter->ctx;
  534. struct perf_data *oldirqdata = counter->irqdata;
  535. /*
  536. * If this is a task context, we need to check whether it is
  537. * the current task context of this cpu. If not it has been
  538. * scheduled out before the smp call arrived.
  539. */
  540. if (ctx->task) {
  541. if (cpuctx->task_ctx != ctx)
  542. return;
  543. spin_lock(&ctx->lock);
  544. }
  545. /* Change the pointer NMI safe */
  546. atomic_long_set((atomic_long_t *)&counter->irqdata,
  547. (unsigned long) counter->usrdata);
  548. counter->usrdata = oldirqdata;
  549. if (ctx->task)
  550. spin_unlock(&ctx->lock);
  551. }
  552. static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
  553. {
  554. struct perf_counter_context *ctx = counter->ctx;
  555. struct perf_data *oldirqdata = counter->irqdata;
  556. struct task_struct *task = ctx->task;
  557. if (!task) {
  558. smp_call_function_single(counter->cpu,
  559. __perf_switch_irq_data,
  560. counter, 1);
  561. return counter->usrdata;
  562. }
  563. retry:
  564. spin_lock_irq(&ctx->lock);
  565. if (!counter->active) {
  566. counter->irqdata = counter->usrdata;
  567. counter->usrdata = oldirqdata;
  568. spin_unlock_irq(&ctx->lock);
  569. return oldirqdata;
  570. }
  571. spin_unlock_irq(&ctx->lock);
  572. task_oncpu_function_call(task, __perf_switch_irq_data, counter);
  573. /* Might have failed, because task was scheduled out */
  574. if (counter->irqdata == oldirqdata)
  575. goto retry;
  576. return counter->usrdata;
  577. }
  578. static void put_context(struct perf_counter_context *ctx)
  579. {
  580. if (ctx->task)
  581. put_task_struct(ctx->task);
  582. }
  583. static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
  584. {
  585. struct perf_cpu_context *cpuctx;
  586. struct perf_counter_context *ctx;
  587. struct task_struct *task;
  588. /*
  589. * If cpu is not a wildcard then this is a percpu counter:
  590. */
  591. if (cpu != -1) {
  592. /* Must be root to operate on a CPU counter: */
  593. if (!capable(CAP_SYS_ADMIN))
  594. return ERR_PTR(-EACCES);
  595. if (cpu < 0 || cpu > num_possible_cpus())
  596. return ERR_PTR(-EINVAL);
  597. /*
  598. * We could be clever and allow to attach a counter to an
  599. * offline CPU and activate it when the CPU comes up, but
  600. * that's for later.
  601. */
  602. if (!cpu_isset(cpu, cpu_online_map))
  603. return ERR_PTR(-ENODEV);
  604. cpuctx = &per_cpu(perf_cpu_context, cpu);
  605. ctx = &cpuctx->ctx;
  606. WARN_ON_ONCE(ctx->task);
  607. return ctx;
  608. }
  609. rcu_read_lock();
  610. if (!pid)
  611. task = current;
  612. else
  613. task = find_task_by_vpid(pid);
  614. if (task)
  615. get_task_struct(task);
  616. rcu_read_unlock();
  617. if (!task)
  618. return ERR_PTR(-ESRCH);
  619. ctx = &task->perf_counter_ctx;
  620. ctx->task = task;
  621. /* Reuse ptrace permission checks for now. */
  622. if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
  623. put_context(ctx);
  624. return ERR_PTR(-EACCES);
  625. }
  626. return ctx;
  627. }
  628. /*
  629. * Called when the last reference to the file is gone.
  630. */
  631. static int perf_release(struct inode *inode, struct file *file)
  632. {
  633. struct perf_counter *counter = file->private_data;
  634. struct perf_counter_context *ctx = counter->ctx;
  635. file->private_data = NULL;
  636. mutex_lock(&counter->mutex);
  637. perf_counter_remove_from_context(counter);
  638. put_context(ctx);
  639. mutex_unlock(&counter->mutex);
  640. kfree(counter);
  641. return 0;
  642. }
  643. /*
  644. * Read the performance counter - simple non blocking version for now
  645. */
  646. static ssize_t
  647. perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
  648. {
  649. u64 cntval;
  650. if (count != sizeof(cntval))
  651. return -EINVAL;
  652. mutex_lock(&counter->mutex);
  653. cntval = perf_counter_read(counter);
  654. mutex_unlock(&counter->mutex);
  655. return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
  656. }
  657. static ssize_t
  658. perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
  659. {
  660. if (!usrdata->len)
  661. return 0;
  662. count = min(count, (size_t)usrdata->len);
  663. if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
  664. return -EFAULT;
  665. /* Adjust the counters */
  666. usrdata->len -= count;
  667. if (!usrdata->len)
  668. usrdata->rd_idx = 0;
  669. else
  670. usrdata->rd_idx += count;
  671. return count;
  672. }
  673. static ssize_t
  674. perf_read_irq_data(struct perf_counter *counter,
  675. char __user *buf,
  676. size_t count,
  677. int nonblocking)
  678. {
  679. struct perf_data *irqdata, *usrdata;
  680. DECLARE_WAITQUEUE(wait, current);
  681. ssize_t res;
  682. irqdata = counter->irqdata;
  683. usrdata = counter->usrdata;
  684. if (usrdata->len + irqdata->len >= count)
  685. goto read_pending;
  686. if (nonblocking)
  687. return -EAGAIN;
  688. spin_lock_irq(&counter->waitq.lock);
  689. __add_wait_queue(&counter->waitq, &wait);
  690. for (;;) {
  691. set_current_state(TASK_INTERRUPTIBLE);
  692. if (usrdata->len + irqdata->len >= count)
  693. break;
  694. if (signal_pending(current))
  695. break;
  696. spin_unlock_irq(&counter->waitq.lock);
  697. schedule();
  698. spin_lock_irq(&counter->waitq.lock);
  699. }
  700. __remove_wait_queue(&counter->waitq, &wait);
  701. __set_current_state(TASK_RUNNING);
  702. spin_unlock_irq(&counter->waitq.lock);
  703. if (usrdata->len + irqdata->len < count)
  704. return -ERESTARTSYS;
  705. read_pending:
  706. mutex_lock(&counter->mutex);
  707. /* Drain pending data first: */
  708. res = perf_copy_usrdata(usrdata, buf, count);
  709. if (res < 0 || res == count)
  710. goto out;
  711. /* Switch irq buffer: */
  712. usrdata = perf_switch_irq_data(counter);
  713. if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) {
  714. if (!res)
  715. res = -EFAULT;
  716. } else {
  717. res = count;
  718. }
  719. out:
  720. mutex_unlock(&counter->mutex);
  721. return res;
  722. }
  723. static ssize_t
  724. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  725. {
  726. struct perf_counter *counter = file->private_data;
  727. switch (counter->hw_event.record_type) {
  728. case PERF_RECORD_SIMPLE:
  729. return perf_read_hw(counter, buf, count);
  730. case PERF_RECORD_IRQ:
  731. case PERF_RECORD_GROUP:
  732. return perf_read_irq_data(counter, buf, count,
  733. file->f_flags & O_NONBLOCK);
  734. }
  735. return -EINVAL;
  736. }
  737. static unsigned int perf_poll(struct file *file, poll_table *wait)
  738. {
  739. struct perf_counter *counter = file->private_data;
  740. unsigned int events = 0;
  741. unsigned long flags;
  742. poll_wait(file, &counter->waitq, wait);
  743. spin_lock_irqsave(&counter->waitq.lock, flags);
  744. if (counter->usrdata->len || counter->irqdata->len)
  745. events |= POLLIN;
  746. spin_unlock_irqrestore(&counter->waitq.lock, flags);
  747. return events;
  748. }
  749. static const struct file_operations perf_fops = {
  750. .release = perf_release,
  751. .read = perf_read,
  752. .poll = perf_poll,
  753. };
  754. static void cpu_clock_perf_counter_enable(struct perf_counter *counter)
  755. {
  756. }
  757. static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
  758. {
  759. }
  760. static void cpu_clock_perf_counter_read(struct perf_counter *counter)
  761. {
  762. int cpu = raw_smp_processor_id();
  763. atomic64_counter_set(counter, cpu_clock(cpu));
  764. }
  765. static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
  766. .hw_perf_counter_enable = cpu_clock_perf_counter_enable,
  767. .hw_perf_counter_disable = cpu_clock_perf_counter_disable,
  768. .hw_perf_counter_read = cpu_clock_perf_counter_read,
  769. };
  770. static void task_clock_perf_counter_enable(struct perf_counter *counter)
  771. {
  772. }
  773. static void task_clock_perf_counter_disable(struct perf_counter *counter)
  774. {
  775. }
  776. static void task_clock_perf_counter_read(struct perf_counter *counter)
  777. {
  778. atomic64_counter_set(counter, current->se.sum_exec_runtime);
  779. }
  780. static const struct hw_perf_counter_ops perf_ops_task_clock = {
  781. .hw_perf_counter_enable = task_clock_perf_counter_enable,
  782. .hw_perf_counter_disable = task_clock_perf_counter_disable,
  783. .hw_perf_counter_read = task_clock_perf_counter_read,
  784. };
  785. static const struct hw_perf_counter_ops *
  786. sw_perf_counter_init(struct perf_counter *counter)
  787. {
  788. const struct hw_perf_counter_ops *hw_ops = NULL;
  789. switch (counter->hw_event.type) {
  790. case PERF_COUNT_CPU_CLOCK:
  791. hw_ops = &perf_ops_cpu_clock;
  792. break;
  793. case PERF_COUNT_TASK_CLOCK:
  794. hw_ops = &perf_ops_task_clock;
  795. break;
  796. default:
  797. break;
  798. }
  799. return hw_ops;
  800. }
  801. /*
  802. * Allocate and initialize a counter structure
  803. */
  804. static struct perf_counter *
  805. perf_counter_alloc(struct perf_counter_hw_event *hw_event,
  806. int cpu,
  807. struct perf_counter *group_leader)
  808. {
  809. const struct hw_perf_counter_ops *hw_ops;
  810. struct perf_counter *counter;
  811. counter = kzalloc(sizeof(*counter), GFP_KERNEL);
  812. if (!counter)
  813. return NULL;
  814. /*
  815. * Single counters are their own group leaders, with an
  816. * empty sibling list:
  817. */
  818. if (!group_leader)
  819. group_leader = counter;
  820. mutex_init(&counter->mutex);
  821. INIT_LIST_HEAD(&counter->list_entry);
  822. INIT_LIST_HEAD(&counter->sibling_list);
  823. init_waitqueue_head(&counter->waitq);
  824. counter->irqdata = &counter->data[0];
  825. counter->usrdata = &counter->data[1];
  826. counter->cpu = cpu;
  827. counter->hw_event = *hw_event;
  828. counter->wakeup_pending = 0;
  829. counter->group_leader = group_leader;
  830. counter->hw_ops = NULL;
  831. hw_ops = NULL;
  832. if (!hw_event->raw && hw_event->type < 0)
  833. hw_ops = sw_perf_counter_init(counter);
  834. if (!hw_ops) {
  835. hw_ops = hw_perf_counter_init(counter);
  836. }
  837. if (!hw_ops) {
  838. kfree(counter);
  839. return NULL;
  840. }
  841. counter->hw_ops = hw_ops;
  842. return counter;
  843. }
  844. /**
  845. * sys_perf_task_open - open a performance counter, associate it to a task/cpu
  846. *
  847. * @hw_event_uptr: event type attributes for monitoring/sampling
  848. * @pid: target pid
  849. * @cpu: target cpu
  850. * @group_fd: group leader counter fd
  851. */
  852. asmlinkage int
  853. sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user,
  854. pid_t pid, int cpu, int group_fd)
  855. {
  856. struct perf_counter *counter, *group_leader;
  857. struct perf_counter_hw_event hw_event;
  858. struct perf_counter_context *ctx;
  859. struct file *group_file = NULL;
  860. int fput_needed = 0;
  861. int ret;
  862. if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
  863. return -EFAULT;
  864. /*
  865. * Get the target context (task or percpu):
  866. */
  867. ctx = find_get_context(pid, cpu);
  868. if (IS_ERR(ctx))
  869. return PTR_ERR(ctx);
  870. /*
  871. * Look up the group leader (we will attach this counter to it):
  872. */
  873. group_leader = NULL;
  874. if (group_fd != -1) {
  875. ret = -EINVAL;
  876. group_file = fget_light(group_fd, &fput_needed);
  877. if (!group_file)
  878. goto err_put_context;
  879. if (group_file->f_op != &perf_fops)
  880. goto err_put_context;
  881. group_leader = group_file->private_data;
  882. /*
  883. * Do not allow a recursive hierarchy (this new sibling
  884. * becoming part of another group-sibling):
  885. */
  886. if (group_leader->group_leader != group_leader)
  887. goto err_put_context;
  888. /*
  889. * Do not allow to attach to a group in a different
  890. * task or CPU context:
  891. */
  892. if (group_leader->ctx != ctx)
  893. goto err_put_context;
  894. }
  895. ret = -EINVAL;
  896. counter = perf_counter_alloc(&hw_event, cpu, group_leader);
  897. if (!counter)
  898. goto err_put_context;
  899. perf_install_in_context(ctx, counter, cpu);
  900. ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
  901. if (ret < 0)
  902. goto err_remove_free_put_context;
  903. out_fput:
  904. fput_light(group_file, fput_needed);
  905. return ret;
  906. err_remove_free_put_context:
  907. mutex_lock(&counter->mutex);
  908. perf_counter_remove_from_context(counter);
  909. mutex_unlock(&counter->mutex);
  910. kfree(counter);
  911. err_put_context:
  912. put_context(ctx);
  913. goto out_fput;
  914. }
  915. static void __cpuinit perf_counter_init_cpu(int cpu)
  916. {
  917. struct perf_cpu_context *cpuctx;
  918. cpuctx = &per_cpu(perf_cpu_context, cpu);
  919. __perf_counter_init_context(&cpuctx->ctx, NULL);
  920. mutex_lock(&perf_resource_mutex);
  921. cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
  922. mutex_unlock(&perf_resource_mutex);
  923. hw_perf_counter_setup();
  924. }
  925. #ifdef CONFIG_HOTPLUG_CPU
  926. static void __perf_counter_exit_cpu(void *info)
  927. {
  928. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  929. struct perf_counter_context *ctx = &cpuctx->ctx;
  930. struct perf_counter *counter, *tmp;
  931. list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
  932. __perf_counter_remove_from_context(counter);
  933. }
  934. static void perf_counter_exit_cpu(int cpu)
  935. {
  936. smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
  937. }
  938. #else
  939. static inline void perf_counter_exit_cpu(int cpu) { }
  940. #endif
  941. static int __cpuinit
  942. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  943. {
  944. unsigned int cpu = (long)hcpu;
  945. switch (action) {
  946. case CPU_UP_PREPARE:
  947. case CPU_UP_PREPARE_FROZEN:
  948. perf_counter_init_cpu(cpu);
  949. break;
  950. case CPU_DOWN_PREPARE:
  951. case CPU_DOWN_PREPARE_FROZEN:
  952. perf_counter_exit_cpu(cpu);
  953. break;
  954. default:
  955. break;
  956. }
  957. return NOTIFY_OK;
  958. }
  959. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  960. .notifier_call = perf_cpu_notify,
  961. };
  962. static int __init perf_counter_init(void)
  963. {
  964. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  965. (void *)(long)smp_processor_id());
  966. register_cpu_notifier(&perf_cpu_nb);
  967. return 0;
  968. }
  969. early_initcall(perf_counter_init);
  970. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  971. {
  972. return sprintf(buf, "%d\n", perf_reserved_percpu);
  973. }
  974. static ssize_t
  975. perf_set_reserve_percpu(struct sysdev_class *class,
  976. const char *buf,
  977. size_t count)
  978. {
  979. struct perf_cpu_context *cpuctx;
  980. unsigned long val;
  981. int err, cpu, mpt;
  982. err = strict_strtoul(buf, 10, &val);
  983. if (err)
  984. return err;
  985. if (val > perf_max_counters)
  986. return -EINVAL;
  987. mutex_lock(&perf_resource_mutex);
  988. perf_reserved_percpu = val;
  989. for_each_online_cpu(cpu) {
  990. cpuctx = &per_cpu(perf_cpu_context, cpu);
  991. spin_lock_irq(&cpuctx->ctx.lock);
  992. mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
  993. perf_max_counters - perf_reserved_percpu);
  994. cpuctx->max_pertask = mpt;
  995. spin_unlock_irq(&cpuctx->ctx.lock);
  996. }
  997. mutex_unlock(&perf_resource_mutex);
  998. return count;
  999. }
  1000. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  1001. {
  1002. return sprintf(buf, "%d\n", perf_overcommit);
  1003. }
  1004. static ssize_t
  1005. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  1006. {
  1007. unsigned long val;
  1008. int err;
  1009. err = strict_strtoul(buf, 10, &val);
  1010. if (err)
  1011. return err;
  1012. if (val > 1)
  1013. return -EINVAL;
  1014. mutex_lock(&perf_resource_mutex);
  1015. perf_overcommit = val;
  1016. mutex_unlock(&perf_resource_mutex);
  1017. return count;
  1018. }
  1019. static SYSDEV_CLASS_ATTR(
  1020. reserve_percpu,
  1021. 0644,
  1022. perf_show_reserve_percpu,
  1023. perf_set_reserve_percpu
  1024. );
  1025. static SYSDEV_CLASS_ATTR(
  1026. overcommit,
  1027. 0644,
  1028. perf_show_overcommit,
  1029. perf_set_overcommit
  1030. );
  1031. static struct attribute *perfclass_attrs[] = {
  1032. &attr_reserve_percpu.attr,
  1033. &attr_overcommit.attr,
  1034. NULL
  1035. };
  1036. static struct attribute_group perfclass_attr_group = {
  1037. .attrs = perfclass_attrs,
  1038. .name = "perf_counters",
  1039. };
  1040. static int __init perf_counter_sysfs_init(void)
  1041. {
  1042. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  1043. &perfclass_attr_group);
  1044. }
  1045. device_initcall(perf_counter_sysfs_init);