perf_counter.c 32 KB

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