perf_counter.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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_disable_all(void) { return 0; }
  41. void __weak hw_perf_restore_ctrl(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_disable_all();
  154. list_del_counter(counter, ctx);
  155. hw_perf_restore_ctrl(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_disable_all();
  235. list_add_counter(counter, ctx);
  236. hw_perf_restore_ctrl(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. counter->hw_ops->hw_perf_counter_enable(counter);
  358. counter->active = 1;
  359. counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
  360. cpuctx->active_oncpu++;
  361. ctx->nr_active++;
  362. }
  363. static void
  364. group_sched_in(struct perf_counter *group_counter,
  365. struct perf_cpu_context *cpuctx,
  366. struct perf_counter_context *ctx,
  367. int cpu)
  368. {
  369. struct perf_counter *counter;
  370. counter_sched_in(group_counter, cpuctx, ctx, cpu);
  371. /*
  372. * Schedule in siblings as one group (if any):
  373. */
  374. list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
  375. counter_sched_in(counter, cpuctx, ctx, cpu);
  376. }
  377. /*
  378. * Called from scheduler to add the counters of the current task
  379. * with interrupts disabled.
  380. *
  381. * We restore the counter value and then enable it.
  382. *
  383. * This does not protect us against NMI, but hw_perf_counter_enable()
  384. * sets the enabled bit in the control field of counter _before_
  385. * accessing the counter control register. If a NMI hits, then it will
  386. * keep the counter running.
  387. */
  388. void perf_counter_task_sched_in(struct task_struct *task, int cpu)
  389. {
  390. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  391. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  392. struct perf_counter *counter;
  393. if (likely(!ctx->nr_counters))
  394. return;
  395. spin_lock(&ctx->lock);
  396. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  397. if (ctx->nr_active == cpuctx->max_pertask)
  398. break;
  399. /*
  400. * Listen to the 'cpu' scheduling filter constraint
  401. * of counters:
  402. */
  403. if (counter->cpu != -1 && counter->cpu != cpu)
  404. continue;
  405. group_sched_in(counter, cpuctx, ctx, cpu);
  406. }
  407. spin_unlock(&ctx->lock);
  408. cpuctx->task_ctx = ctx;
  409. }
  410. void perf_counter_task_tick(struct task_struct *curr, int cpu)
  411. {
  412. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  413. struct perf_counter *counter;
  414. u64 perf_flags;
  415. if (likely(!ctx->nr_counters))
  416. return;
  417. perf_counter_task_sched_out(curr, cpu);
  418. spin_lock(&ctx->lock);
  419. /*
  420. * Rotate the first entry last (works just fine for group counters too):
  421. */
  422. perf_flags = hw_perf_disable_all();
  423. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  424. list_del(&counter->list_entry);
  425. list_add_tail(&counter->list_entry, &ctx->counter_list);
  426. break;
  427. }
  428. hw_perf_restore_ctrl(perf_flags);
  429. spin_unlock(&ctx->lock);
  430. perf_counter_task_sched_in(curr, cpu);
  431. }
  432. /*
  433. * Initialize the perf_counter context in a task_struct:
  434. */
  435. static void
  436. __perf_counter_init_context(struct perf_counter_context *ctx,
  437. struct task_struct *task)
  438. {
  439. spin_lock_init(&ctx->lock);
  440. INIT_LIST_HEAD(&ctx->counter_list);
  441. ctx->nr_counters = 0;
  442. ctx->task = task;
  443. }
  444. /*
  445. * Initialize the perf_counter context in task_struct
  446. */
  447. void perf_counter_init_task(struct task_struct *task)
  448. {
  449. __perf_counter_init_context(&task->perf_counter_ctx, task);
  450. }
  451. /*
  452. * Cross CPU call to read the hardware counter
  453. */
  454. static void __hw_perf_counter_read(void *info)
  455. {
  456. struct perf_counter *counter = info;
  457. counter->hw_ops->hw_perf_counter_read(counter);
  458. }
  459. static u64 perf_counter_read(struct perf_counter *counter)
  460. {
  461. /*
  462. * If counter is enabled and currently active on a CPU, update the
  463. * value in the counter structure:
  464. */
  465. if (counter->active) {
  466. smp_call_function_single(counter->oncpu,
  467. __hw_perf_counter_read, counter, 1);
  468. }
  469. return perf_counter_read_safe(counter);
  470. }
  471. /*
  472. * Cross CPU call to switch performance data pointers
  473. */
  474. static void __perf_switch_irq_data(void *info)
  475. {
  476. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  477. struct perf_counter *counter = info;
  478. struct perf_counter_context *ctx = counter->ctx;
  479. struct perf_data *oldirqdata = counter->irqdata;
  480. /*
  481. * If this is a task context, we need to check whether it is
  482. * the current task context of this cpu. If not it has been
  483. * scheduled out before the smp call arrived.
  484. */
  485. if (ctx->task) {
  486. if (cpuctx->task_ctx != ctx)
  487. return;
  488. spin_lock(&ctx->lock);
  489. }
  490. /* Change the pointer NMI safe */
  491. atomic_long_set((atomic_long_t *)&counter->irqdata,
  492. (unsigned long) counter->usrdata);
  493. counter->usrdata = oldirqdata;
  494. if (ctx->task)
  495. spin_unlock(&ctx->lock);
  496. }
  497. static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
  498. {
  499. struct perf_counter_context *ctx = counter->ctx;
  500. struct perf_data *oldirqdata = counter->irqdata;
  501. struct task_struct *task = ctx->task;
  502. if (!task) {
  503. smp_call_function_single(counter->cpu,
  504. __perf_switch_irq_data,
  505. counter, 1);
  506. return counter->usrdata;
  507. }
  508. retry:
  509. spin_lock_irq(&ctx->lock);
  510. if (!counter->active) {
  511. counter->irqdata = counter->usrdata;
  512. counter->usrdata = oldirqdata;
  513. spin_unlock_irq(&ctx->lock);
  514. return oldirqdata;
  515. }
  516. spin_unlock_irq(&ctx->lock);
  517. task_oncpu_function_call(task, __perf_switch_irq_data, counter);
  518. /* Might have failed, because task was scheduled out */
  519. if (counter->irqdata == oldirqdata)
  520. goto retry;
  521. return counter->usrdata;
  522. }
  523. static void put_context(struct perf_counter_context *ctx)
  524. {
  525. if (ctx->task)
  526. put_task_struct(ctx->task);
  527. }
  528. static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
  529. {
  530. struct perf_cpu_context *cpuctx;
  531. struct perf_counter_context *ctx;
  532. struct task_struct *task;
  533. /*
  534. * If cpu is not a wildcard then this is a percpu counter:
  535. */
  536. if (cpu != -1) {
  537. /* Must be root to operate on a CPU counter: */
  538. if (!capable(CAP_SYS_ADMIN))
  539. return ERR_PTR(-EACCES);
  540. if (cpu < 0 || cpu > num_possible_cpus())
  541. return ERR_PTR(-EINVAL);
  542. /*
  543. * We could be clever and allow to attach a counter to an
  544. * offline CPU and activate it when the CPU comes up, but
  545. * that's for later.
  546. */
  547. if (!cpu_isset(cpu, cpu_online_map))
  548. return ERR_PTR(-ENODEV);
  549. cpuctx = &per_cpu(perf_cpu_context, cpu);
  550. ctx = &cpuctx->ctx;
  551. WARN_ON_ONCE(ctx->task);
  552. return ctx;
  553. }
  554. rcu_read_lock();
  555. if (!pid)
  556. task = current;
  557. else
  558. task = find_task_by_vpid(pid);
  559. if (task)
  560. get_task_struct(task);
  561. rcu_read_unlock();
  562. if (!task)
  563. return ERR_PTR(-ESRCH);
  564. ctx = &task->perf_counter_ctx;
  565. ctx->task = task;
  566. /* Reuse ptrace permission checks for now. */
  567. if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
  568. put_context(ctx);
  569. return ERR_PTR(-EACCES);
  570. }
  571. return ctx;
  572. }
  573. /*
  574. * Called when the last reference to the file is gone.
  575. */
  576. static int perf_release(struct inode *inode, struct file *file)
  577. {
  578. struct perf_counter *counter = file->private_data;
  579. struct perf_counter_context *ctx = counter->ctx;
  580. file->private_data = NULL;
  581. mutex_lock(&counter->mutex);
  582. perf_counter_remove_from_context(counter);
  583. put_context(ctx);
  584. mutex_unlock(&counter->mutex);
  585. kfree(counter);
  586. return 0;
  587. }
  588. /*
  589. * Read the performance counter - simple non blocking version for now
  590. */
  591. static ssize_t
  592. perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
  593. {
  594. u64 cntval;
  595. if (count != sizeof(cntval))
  596. return -EINVAL;
  597. mutex_lock(&counter->mutex);
  598. cntval = perf_counter_read(counter);
  599. mutex_unlock(&counter->mutex);
  600. return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
  601. }
  602. static ssize_t
  603. perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
  604. {
  605. if (!usrdata->len)
  606. return 0;
  607. count = min(count, (size_t)usrdata->len);
  608. if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
  609. return -EFAULT;
  610. /* Adjust the counters */
  611. usrdata->len -= count;
  612. if (!usrdata->len)
  613. usrdata->rd_idx = 0;
  614. else
  615. usrdata->rd_idx += count;
  616. return count;
  617. }
  618. static ssize_t
  619. perf_read_irq_data(struct perf_counter *counter,
  620. char __user *buf,
  621. size_t count,
  622. int nonblocking)
  623. {
  624. struct perf_data *irqdata, *usrdata;
  625. DECLARE_WAITQUEUE(wait, current);
  626. ssize_t res;
  627. irqdata = counter->irqdata;
  628. usrdata = counter->usrdata;
  629. if (usrdata->len + irqdata->len >= count)
  630. goto read_pending;
  631. if (nonblocking)
  632. return -EAGAIN;
  633. spin_lock_irq(&counter->waitq.lock);
  634. __add_wait_queue(&counter->waitq, &wait);
  635. for (;;) {
  636. set_current_state(TASK_INTERRUPTIBLE);
  637. if (usrdata->len + irqdata->len >= count)
  638. break;
  639. if (signal_pending(current))
  640. break;
  641. spin_unlock_irq(&counter->waitq.lock);
  642. schedule();
  643. spin_lock_irq(&counter->waitq.lock);
  644. }
  645. __remove_wait_queue(&counter->waitq, &wait);
  646. __set_current_state(TASK_RUNNING);
  647. spin_unlock_irq(&counter->waitq.lock);
  648. if (usrdata->len + irqdata->len < count)
  649. return -ERESTARTSYS;
  650. read_pending:
  651. mutex_lock(&counter->mutex);
  652. /* Drain pending data first: */
  653. res = perf_copy_usrdata(usrdata, buf, count);
  654. if (res < 0 || res == count)
  655. goto out;
  656. /* Switch irq buffer: */
  657. usrdata = perf_switch_irq_data(counter);
  658. if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) {
  659. if (!res)
  660. res = -EFAULT;
  661. } else {
  662. res = count;
  663. }
  664. out:
  665. mutex_unlock(&counter->mutex);
  666. return res;
  667. }
  668. static ssize_t
  669. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  670. {
  671. struct perf_counter *counter = file->private_data;
  672. switch (counter->hw_event.record_type) {
  673. case PERF_RECORD_SIMPLE:
  674. return perf_read_hw(counter, buf, count);
  675. case PERF_RECORD_IRQ:
  676. case PERF_RECORD_GROUP:
  677. return perf_read_irq_data(counter, buf, count,
  678. file->f_flags & O_NONBLOCK);
  679. }
  680. return -EINVAL;
  681. }
  682. static unsigned int perf_poll(struct file *file, poll_table *wait)
  683. {
  684. struct perf_counter *counter = file->private_data;
  685. unsigned int events = 0;
  686. unsigned long flags;
  687. poll_wait(file, &counter->waitq, wait);
  688. spin_lock_irqsave(&counter->waitq.lock, flags);
  689. if (counter->usrdata->len || counter->irqdata->len)
  690. events |= POLLIN;
  691. spin_unlock_irqrestore(&counter->waitq.lock, flags);
  692. return events;
  693. }
  694. static const struct file_operations perf_fops = {
  695. .release = perf_release,
  696. .read = perf_read,
  697. .poll = perf_poll,
  698. };
  699. static void cpu_clock_perf_counter_enable(struct perf_counter *counter)
  700. {
  701. }
  702. static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
  703. {
  704. }
  705. static void cpu_clock_perf_counter_read(struct perf_counter *counter)
  706. {
  707. int cpu = raw_smp_processor_id();
  708. atomic64_counter_set(counter, cpu_clock(cpu));
  709. }
  710. static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
  711. .hw_perf_counter_enable = cpu_clock_perf_counter_enable,
  712. .hw_perf_counter_disable = cpu_clock_perf_counter_disable,
  713. .hw_perf_counter_read = cpu_clock_perf_counter_read,
  714. };
  715. static const struct hw_perf_counter_ops *
  716. sw_perf_counter_init(struct perf_counter *counter)
  717. {
  718. const struct hw_perf_counter_ops *hw_ops = NULL;
  719. switch (counter->hw_event.type) {
  720. case PERF_COUNT_CPU_CLOCK:
  721. hw_ops = &perf_ops_cpu_clock;
  722. break;
  723. default:
  724. break;
  725. }
  726. return hw_ops;
  727. }
  728. /*
  729. * Allocate and initialize a counter structure
  730. */
  731. static struct perf_counter *
  732. perf_counter_alloc(struct perf_counter_hw_event *hw_event,
  733. int cpu,
  734. struct perf_counter *group_leader)
  735. {
  736. const struct hw_perf_counter_ops *hw_ops;
  737. struct perf_counter *counter;
  738. counter = kzalloc(sizeof(*counter), GFP_KERNEL);
  739. if (!counter)
  740. return NULL;
  741. /*
  742. * Single counters are their own group leaders, with an
  743. * empty sibling list:
  744. */
  745. if (!group_leader)
  746. group_leader = counter;
  747. mutex_init(&counter->mutex);
  748. INIT_LIST_HEAD(&counter->list_entry);
  749. INIT_LIST_HEAD(&counter->sibling_list);
  750. init_waitqueue_head(&counter->waitq);
  751. counter->irqdata = &counter->data[0];
  752. counter->usrdata = &counter->data[1];
  753. counter->cpu = cpu;
  754. counter->hw_event = *hw_event;
  755. counter->wakeup_pending = 0;
  756. counter->group_leader = group_leader;
  757. counter->hw_ops = NULL;
  758. hw_ops = NULL;
  759. if (!hw_event->raw && hw_event->type < 0)
  760. hw_ops = sw_perf_counter_init(counter);
  761. if (!hw_ops) {
  762. hw_ops = hw_perf_counter_init(counter);
  763. }
  764. if (!hw_ops) {
  765. kfree(counter);
  766. return NULL;
  767. }
  768. counter->hw_ops = hw_ops;
  769. return counter;
  770. }
  771. /**
  772. * sys_perf_task_open - open a performance counter, associate it to a task/cpu
  773. *
  774. * @hw_event_uptr: event type attributes for monitoring/sampling
  775. * @pid: target pid
  776. * @cpu: target cpu
  777. * @group_fd: group leader counter fd
  778. */
  779. asmlinkage int sys_perf_counter_open(
  780. struct perf_counter_hw_event *hw_event_uptr __user,
  781. pid_t pid,
  782. int cpu,
  783. int group_fd)
  784. {
  785. struct perf_counter *counter, *group_leader;
  786. struct perf_counter_hw_event hw_event;
  787. struct perf_counter_context *ctx;
  788. struct file *group_file = NULL;
  789. int fput_needed = 0;
  790. int ret;
  791. if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
  792. return -EFAULT;
  793. /*
  794. * Get the target context (task or percpu):
  795. */
  796. ctx = find_get_context(pid, cpu);
  797. if (IS_ERR(ctx))
  798. return PTR_ERR(ctx);
  799. /*
  800. * Look up the group leader (we will attach this counter to it):
  801. */
  802. group_leader = NULL;
  803. if (group_fd != -1) {
  804. ret = -EINVAL;
  805. group_file = fget_light(group_fd, &fput_needed);
  806. if (!group_file)
  807. goto err_put_context;
  808. if (group_file->f_op != &perf_fops)
  809. goto err_put_context;
  810. group_leader = group_file->private_data;
  811. /*
  812. * Do not allow a recursive hierarchy (this new sibling
  813. * becoming part of another group-sibling):
  814. */
  815. if (group_leader->group_leader != group_leader)
  816. goto err_put_context;
  817. /*
  818. * Do not allow to attach to a group in a different
  819. * task or CPU context:
  820. */
  821. if (group_leader->ctx != ctx)
  822. goto err_put_context;
  823. }
  824. ret = -EINVAL;
  825. counter = perf_counter_alloc(&hw_event, cpu, group_leader);
  826. if (!counter)
  827. goto err_put_context;
  828. perf_install_in_context(ctx, counter, cpu);
  829. ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
  830. if (ret < 0)
  831. goto err_remove_free_put_context;
  832. out_fput:
  833. fput_light(group_file, fput_needed);
  834. return ret;
  835. err_remove_free_put_context:
  836. mutex_lock(&counter->mutex);
  837. perf_counter_remove_from_context(counter);
  838. mutex_unlock(&counter->mutex);
  839. kfree(counter);
  840. err_put_context:
  841. put_context(ctx);
  842. goto out_fput;
  843. }
  844. static void __cpuinit perf_counter_init_cpu(int cpu)
  845. {
  846. struct perf_cpu_context *cpuctx;
  847. cpuctx = &per_cpu(perf_cpu_context, cpu);
  848. __perf_counter_init_context(&cpuctx->ctx, NULL);
  849. mutex_lock(&perf_resource_mutex);
  850. cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
  851. mutex_unlock(&perf_resource_mutex);
  852. hw_perf_counter_setup();
  853. }
  854. #ifdef CONFIG_HOTPLUG_CPU
  855. static void __perf_counter_exit_cpu(void *info)
  856. {
  857. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  858. struct perf_counter_context *ctx = &cpuctx->ctx;
  859. struct perf_counter *counter, *tmp;
  860. list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
  861. __perf_counter_remove_from_context(counter);
  862. }
  863. static void perf_counter_exit_cpu(int cpu)
  864. {
  865. smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
  866. }
  867. #else
  868. static inline void perf_counter_exit_cpu(int cpu) { }
  869. #endif
  870. static int __cpuinit
  871. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  872. {
  873. unsigned int cpu = (long)hcpu;
  874. switch (action) {
  875. case CPU_UP_PREPARE:
  876. case CPU_UP_PREPARE_FROZEN:
  877. perf_counter_init_cpu(cpu);
  878. break;
  879. case CPU_DOWN_PREPARE:
  880. case CPU_DOWN_PREPARE_FROZEN:
  881. perf_counter_exit_cpu(cpu);
  882. break;
  883. default:
  884. break;
  885. }
  886. return NOTIFY_OK;
  887. }
  888. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  889. .notifier_call = perf_cpu_notify,
  890. };
  891. static int __init perf_counter_init(void)
  892. {
  893. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  894. (void *)(long)smp_processor_id());
  895. register_cpu_notifier(&perf_cpu_nb);
  896. return 0;
  897. }
  898. early_initcall(perf_counter_init);
  899. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  900. {
  901. return sprintf(buf, "%d\n", perf_reserved_percpu);
  902. }
  903. static ssize_t
  904. perf_set_reserve_percpu(struct sysdev_class *class,
  905. const char *buf,
  906. size_t count)
  907. {
  908. struct perf_cpu_context *cpuctx;
  909. unsigned long val;
  910. int err, cpu, mpt;
  911. err = strict_strtoul(buf, 10, &val);
  912. if (err)
  913. return err;
  914. if (val > perf_max_counters)
  915. return -EINVAL;
  916. mutex_lock(&perf_resource_mutex);
  917. perf_reserved_percpu = val;
  918. for_each_online_cpu(cpu) {
  919. cpuctx = &per_cpu(perf_cpu_context, cpu);
  920. spin_lock_irq(&cpuctx->ctx.lock);
  921. mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
  922. perf_max_counters - perf_reserved_percpu);
  923. cpuctx->max_pertask = mpt;
  924. spin_unlock_irq(&cpuctx->ctx.lock);
  925. }
  926. mutex_unlock(&perf_resource_mutex);
  927. return count;
  928. }
  929. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  930. {
  931. return sprintf(buf, "%d\n", perf_overcommit);
  932. }
  933. static ssize_t
  934. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  935. {
  936. unsigned long val;
  937. int err;
  938. err = strict_strtoul(buf, 10, &val);
  939. if (err)
  940. return err;
  941. if (val > 1)
  942. return -EINVAL;
  943. mutex_lock(&perf_resource_mutex);
  944. perf_overcommit = val;
  945. mutex_unlock(&perf_resource_mutex);
  946. return count;
  947. }
  948. static SYSDEV_CLASS_ATTR(
  949. reserve_percpu,
  950. 0644,
  951. perf_show_reserve_percpu,
  952. perf_set_reserve_percpu
  953. );
  954. static SYSDEV_CLASS_ATTR(
  955. overcommit,
  956. 0644,
  957. perf_show_overcommit,
  958. perf_set_overcommit
  959. );
  960. static struct attribute *perfclass_attrs[] = {
  961. &attr_reserve_percpu.attr,
  962. &attr_overcommit.attr,
  963. NULL
  964. };
  965. static struct attribute_group perfclass_attr_group = {
  966. .attrs = perfclass_attrs,
  967. .name = "perf_counters",
  968. };
  969. static int __init perf_counter_sysfs_init(void)
  970. {
  971. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  972. &perfclass_attr_group);
  973. }
  974. device_initcall(perf_counter_sysfs_init);