perf_counter.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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/poll.h>
  13. #include <linux/sysfs.h>
  14. #include <linux/ptrace.h>
  15. #include <linux/percpu.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/anon_inodes.h>
  19. #include <linux/perf_counter.h>
  20. /*
  21. * Each CPU has a list of per CPU counters:
  22. */
  23. DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
  24. int perf_max_counters __read_mostly;
  25. static int perf_reserved_percpu __read_mostly;
  26. static int perf_overcommit __read_mostly = 1;
  27. /*
  28. * Mutex for (sysadmin-configurable) counter reservations:
  29. */
  30. static DEFINE_MUTEX(perf_resource_mutex);
  31. /*
  32. * Architecture provided APIs - weak aliases:
  33. */
  34. int __weak hw_perf_counter_init(struct perf_counter *counter, u32 hw_event_type)
  35. {
  36. return -EINVAL;
  37. }
  38. void __weak hw_perf_counter_enable(struct perf_counter *counter) { }
  39. void __weak hw_perf_counter_disable(struct perf_counter *counter) { }
  40. void __weak hw_perf_counter_read(struct perf_counter *counter) { }
  41. void __weak hw_perf_disable_all(void) { }
  42. void __weak hw_perf_enable_all(void) { }
  43. void __weak hw_perf_counter_setup(void) { }
  44. #if BITS_PER_LONG == 64
  45. /*
  46. * Read the cached counter in counter safe against cross CPU / NMI
  47. * modifications. 64 bit version - no complications.
  48. */
  49. static inline u64 perf_read_counter_safe(struct perf_counter *counter)
  50. {
  51. return (u64) atomic64_read(&counter->count);
  52. }
  53. #else
  54. /*
  55. * Read the cached counter in counter safe against cross CPU / NMI
  56. * modifications. 32 bit version.
  57. */
  58. static u64 perf_read_counter_safe(struct perf_counter *counter)
  59. {
  60. u32 cntl, cnth;
  61. local_irq_disable();
  62. do {
  63. cnth = atomic_read(&counter->count32[1]);
  64. cntl = atomic_read(&counter->count32[0]);
  65. } while (cnth != atomic_read(&counter->count32[1]));
  66. local_irq_enable();
  67. return cntl | ((u64) cnth) << 32;
  68. }
  69. #endif
  70. /*
  71. * Cross CPU call to remove a performance counter
  72. *
  73. * We disable the counter on the hardware level first. After that we
  74. * remove it from the context list.
  75. */
  76. static void __perf_remove_from_context(void *info)
  77. {
  78. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  79. struct perf_counter *counter = info;
  80. struct perf_counter_context *ctx = counter->ctx;
  81. /*
  82. * If this is a task context, we need to check whether it is
  83. * the current task context of this cpu. If not it has been
  84. * scheduled out before the smp call arrived.
  85. */
  86. if (ctx->task && cpuctx->task_ctx != ctx)
  87. return;
  88. spin_lock(&ctx->lock);
  89. if (counter->active) {
  90. hw_perf_counter_disable(counter);
  91. counter->active = 0;
  92. ctx->nr_active--;
  93. cpuctx->active_oncpu--;
  94. counter->task = NULL;
  95. }
  96. ctx->nr_counters--;
  97. /*
  98. * Protect the list operation against NMI by disabling the
  99. * counters on a global level. NOP for non NMI based counters.
  100. */
  101. hw_perf_disable_all();
  102. list_del_init(&counter->list);
  103. hw_perf_enable_all();
  104. if (!ctx->task) {
  105. /*
  106. * Allow more per task counters with respect to the
  107. * reservation:
  108. */
  109. cpuctx->max_pertask =
  110. min(perf_max_counters - ctx->nr_counters,
  111. perf_max_counters - perf_reserved_percpu);
  112. }
  113. spin_unlock(&ctx->lock);
  114. }
  115. /*
  116. * Remove the counter from a task's (or a CPU's) list of counters.
  117. *
  118. * Must be called with counter->mutex held.
  119. *
  120. * CPU counters are removed with a smp call. For task counters we only
  121. * call when the task is on a CPU.
  122. */
  123. static void perf_remove_from_context(struct perf_counter *counter)
  124. {
  125. struct perf_counter_context *ctx = counter->ctx;
  126. struct task_struct *task = ctx->task;
  127. if (!task) {
  128. /*
  129. * Per cpu counters are removed via an smp call and
  130. * the removal is always sucessful.
  131. */
  132. smp_call_function_single(counter->cpu,
  133. __perf_remove_from_context,
  134. counter, 1);
  135. return;
  136. }
  137. retry:
  138. task_oncpu_function_call(task, __perf_remove_from_context,
  139. counter);
  140. spin_lock_irq(&ctx->lock);
  141. /*
  142. * If the context is active we need to retry the smp call.
  143. */
  144. if (ctx->nr_active && !list_empty(&counter->list)) {
  145. spin_unlock_irq(&ctx->lock);
  146. goto retry;
  147. }
  148. /*
  149. * The lock prevents that this context is scheduled in so we
  150. * can remove the counter safely, if it the call above did not
  151. * succeed.
  152. */
  153. if (!list_empty(&counter->list)) {
  154. ctx->nr_counters--;
  155. list_del_init(&counter->list);
  156. counter->task = NULL;
  157. }
  158. spin_unlock_irq(&ctx->lock);
  159. }
  160. /*
  161. * Cross CPU call to install and enable a preformance counter
  162. */
  163. static void __perf_install_in_context(void *info)
  164. {
  165. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  166. struct perf_counter *counter = info;
  167. struct perf_counter_context *ctx = counter->ctx;
  168. int cpu = smp_processor_id();
  169. /*
  170. * If this is a task context, we need to check whether it is
  171. * the current task context of this cpu. If not it has been
  172. * scheduled out before the smp call arrived.
  173. */
  174. if (ctx->task && cpuctx->task_ctx != ctx)
  175. return;
  176. spin_lock(&ctx->lock);
  177. /*
  178. * Protect the list operation against NMI by disabling the
  179. * counters on a global level. NOP for non NMI based counters.
  180. */
  181. hw_perf_disable_all();
  182. list_add_tail(&counter->list, &ctx->counters);
  183. hw_perf_enable_all();
  184. ctx->nr_counters++;
  185. if (cpuctx->active_oncpu < perf_max_counters) {
  186. hw_perf_counter_enable(counter);
  187. counter->active = 1;
  188. counter->oncpu = cpu;
  189. ctx->nr_active++;
  190. cpuctx->active_oncpu++;
  191. }
  192. if (!ctx->task && cpuctx->max_pertask)
  193. cpuctx->max_pertask--;
  194. spin_unlock(&ctx->lock);
  195. }
  196. /*
  197. * Attach a performance counter to a context
  198. *
  199. * First we add the counter to the list with the hardware enable bit
  200. * in counter->hw_config cleared.
  201. *
  202. * If the counter is attached to a task which is on a CPU we use a smp
  203. * call to enable it in the task context. The task might have been
  204. * scheduled away, but we check this in the smp call again.
  205. */
  206. static void
  207. perf_install_in_context(struct perf_counter_context *ctx,
  208. struct perf_counter *counter,
  209. int cpu)
  210. {
  211. struct task_struct *task = ctx->task;
  212. counter->ctx = ctx;
  213. if (!task) {
  214. /*
  215. * Per cpu counters are installed via an smp call and
  216. * the install is always sucessful.
  217. */
  218. smp_call_function_single(cpu, __perf_install_in_context,
  219. counter, 1);
  220. return;
  221. }
  222. counter->task = task;
  223. retry:
  224. task_oncpu_function_call(task, __perf_install_in_context,
  225. counter);
  226. spin_lock_irq(&ctx->lock);
  227. /*
  228. * If the context is active and the counter has not been added
  229. * we need to retry the smp call.
  230. */
  231. if (ctx->nr_active && list_empty(&counter->list)) {
  232. spin_unlock_irq(&ctx->lock);
  233. goto retry;
  234. }
  235. /*
  236. * The lock prevents that this context is scheduled in so we
  237. * can add the counter safely, if it the call above did not
  238. * succeed.
  239. */
  240. if (list_empty(&counter->list)) {
  241. list_add_tail(&counter->list, &ctx->counters);
  242. ctx->nr_counters++;
  243. }
  244. spin_unlock_irq(&ctx->lock);
  245. }
  246. /*
  247. * Called from scheduler to remove the counters of the current task,
  248. * with interrupts disabled.
  249. *
  250. * We stop each counter and update the counter value in counter->count.
  251. *
  252. * This does not protect us against NMI, but hw_perf_counter_disable()
  253. * sets the disabled bit in the control field of counter _before_
  254. * accessing the counter control register. If a NMI hits, then it will
  255. * not restart the counter.
  256. */
  257. void perf_counter_task_sched_out(struct task_struct *task, int cpu)
  258. {
  259. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  260. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  261. struct perf_counter *counter;
  262. if (likely(!cpuctx->task_ctx))
  263. return;
  264. spin_lock(&ctx->lock);
  265. list_for_each_entry(counter, &ctx->counters, list) {
  266. if (!ctx->nr_active)
  267. break;
  268. if (counter->active) {
  269. hw_perf_counter_disable(counter);
  270. counter->active = 0;
  271. counter->oncpu = -1;
  272. ctx->nr_active--;
  273. cpuctx->active_oncpu--;
  274. }
  275. }
  276. spin_unlock(&ctx->lock);
  277. cpuctx->task_ctx = NULL;
  278. }
  279. /*
  280. * Called from scheduler to add the counters of the current task
  281. * with interrupts disabled.
  282. *
  283. * We restore the counter value and then enable it.
  284. *
  285. * This does not protect us against NMI, but hw_perf_counter_enable()
  286. * sets the enabled bit in the control field of counter _before_
  287. * accessing the counter control register. If a NMI hits, then it will
  288. * keep the counter running.
  289. */
  290. void perf_counter_task_sched_in(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(!ctx->nr_counters))
  296. return;
  297. spin_lock(&ctx->lock);
  298. list_for_each_entry(counter, &ctx->counters, list) {
  299. if (ctx->nr_active == cpuctx->max_pertask)
  300. break;
  301. if (counter->cpu != -1 && counter->cpu != cpu)
  302. continue;
  303. hw_perf_counter_enable(counter);
  304. counter->active = 1;
  305. counter->oncpu = cpu;
  306. ctx->nr_active++;
  307. cpuctx->active_oncpu++;
  308. }
  309. spin_unlock(&ctx->lock);
  310. cpuctx->task_ctx = ctx;
  311. }
  312. void perf_counter_task_tick(struct task_struct *curr, int cpu)
  313. {
  314. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  315. struct perf_counter *counter;
  316. if (likely(!ctx->nr_counters))
  317. return;
  318. perf_counter_task_sched_out(curr, cpu);
  319. spin_lock(&ctx->lock);
  320. /*
  321. * Rotate the first entry last:
  322. */
  323. hw_perf_disable_all();
  324. list_for_each_entry(counter, &ctx->counters, list) {
  325. list_del(&counter->list);
  326. list_add_tail(&counter->list, &ctx->counters);
  327. break;
  328. }
  329. hw_perf_enable_all();
  330. spin_unlock(&ctx->lock);
  331. perf_counter_task_sched_in(curr, cpu);
  332. }
  333. /*
  334. * Initialize the perf_counter context in task_struct
  335. */
  336. void perf_counter_init_task(struct task_struct *task)
  337. {
  338. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  339. spin_lock_init(&ctx->lock);
  340. INIT_LIST_HEAD(&ctx->counters);
  341. ctx->nr_counters = 0;
  342. ctx->task = task;
  343. }
  344. /*
  345. * Cross CPU call to read the hardware counter
  346. */
  347. static void __hw_perf_counter_read(void *info)
  348. {
  349. hw_perf_counter_read(info);
  350. }
  351. static u64 perf_read_counter(struct perf_counter *counter)
  352. {
  353. /*
  354. * If counter is enabled and currently active on a CPU, update the
  355. * value in the counter structure:
  356. */
  357. if (counter->active) {
  358. smp_call_function_single(counter->oncpu,
  359. __hw_perf_counter_read, counter, 1);
  360. }
  361. return perf_read_counter_safe(counter);
  362. }
  363. /*
  364. * Cross CPU call to switch performance data pointers
  365. */
  366. static void __perf_switch_irq_data(void *info)
  367. {
  368. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  369. struct perf_counter *counter = info;
  370. struct perf_counter_context *ctx = counter->ctx;
  371. struct perf_data *oldirqdata = counter->irqdata;
  372. /*
  373. * If this is a task context, we need to check whether it is
  374. * the current task context of this cpu. If not it has been
  375. * scheduled out before the smp call arrived.
  376. */
  377. if (ctx->task) {
  378. if (cpuctx->task_ctx != ctx)
  379. return;
  380. spin_lock(&ctx->lock);
  381. }
  382. /* Change the pointer NMI safe */
  383. atomic_long_set((atomic_long_t *)&counter->irqdata,
  384. (unsigned long) counter->usrdata);
  385. counter->usrdata = oldirqdata;
  386. if (ctx->task)
  387. spin_unlock(&ctx->lock);
  388. }
  389. static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
  390. {
  391. struct perf_counter_context *ctx = counter->ctx;
  392. struct perf_data *oldirqdata = counter->irqdata;
  393. struct task_struct *task = ctx->task;
  394. if (!task) {
  395. smp_call_function_single(counter->cpu,
  396. __perf_switch_irq_data,
  397. counter, 1);
  398. return counter->usrdata;
  399. }
  400. retry:
  401. spin_lock_irq(&ctx->lock);
  402. if (!counter->active) {
  403. counter->irqdata = counter->usrdata;
  404. counter->usrdata = oldirqdata;
  405. spin_unlock_irq(&ctx->lock);
  406. return oldirqdata;
  407. }
  408. spin_unlock_irq(&ctx->lock);
  409. task_oncpu_function_call(task, __perf_switch_irq_data, counter);
  410. /* Might have failed, because task was scheduled out */
  411. if (counter->irqdata == oldirqdata)
  412. goto retry;
  413. return counter->usrdata;
  414. }
  415. static void put_context(struct perf_counter_context *ctx)
  416. {
  417. if (ctx->task)
  418. put_task_struct(ctx->task);
  419. }
  420. static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
  421. {
  422. struct perf_cpu_context *cpuctx;
  423. struct perf_counter_context *ctx;
  424. struct task_struct *task;
  425. /*
  426. * If cpu is not a wildcard then this is a percpu counter:
  427. */
  428. if (cpu != -1) {
  429. /* Must be root to operate on a CPU counter: */
  430. if (!capable(CAP_SYS_ADMIN))
  431. return ERR_PTR(-EACCES);
  432. if (cpu < 0 || cpu > num_possible_cpus())
  433. return ERR_PTR(-EINVAL);
  434. /*
  435. * We could be clever and allow to attach a counter to an
  436. * offline CPU and activate it when the CPU comes up, but
  437. * that's for later.
  438. */
  439. if (!cpu_isset(cpu, cpu_online_map))
  440. return ERR_PTR(-ENODEV);
  441. cpuctx = &per_cpu(perf_cpu_context, cpu);
  442. ctx = &cpuctx->ctx;
  443. WARN_ON_ONCE(ctx->task);
  444. return ctx;
  445. }
  446. rcu_read_lock();
  447. if (!pid)
  448. task = current;
  449. else
  450. task = find_task_by_vpid(pid);
  451. if (task)
  452. get_task_struct(task);
  453. rcu_read_unlock();
  454. if (!task)
  455. return ERR_PTR(-ESRCH);
  456. ctx = &task->perf_counter_ctx;
  457. ctx->task = task;
  458. /* Reuse ptrace permission checks for now. */
  459. if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
  460. put_context(ctx);
  461. return ERR_PTR(-EACCES);
  462. }
  463. return ctx;
  464. }
  465. /*
  466. * Called when the last reference to the file is gone.
  467. */
  468. static int perf_release(struct inode *inode, struct file *file)
  469. {
  470. struct perf_counter *counter = file->private_data;
  471. struct perf_counter_context *ctx = counter->ctx;
  472. file->private_data = NULL;
  473. mutex_lock(&counter->mutex);
  474. perf_remove_from_context(counter);
  475. put_context(ctx);
  476. mutex_unlock(&counter->mutex);
  477. kfree(counter);
  478. return 0;
  479. }
  480. /*
  481. * Read the performance counter - simple non blocking version for now
  482. */
  483. static ssize_t
  484. perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
  485. {
  486. u64 cntval;
  487. if (count != sizeof(cntval))
  488. return -EINVAL;
  489. mutex_lock(&counter->mutex);
  490. cntval = perf_read_counter(counter);
  491. mutex_unlock(&counter->mutex);
  492. return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
  493. }
  494. static ssize_t
  495. perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
  496. {
  497. if (!usrdata->len)
  498. return 0;
  499. count = min(count, (size_t)usrdata->len);
  500. if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
  501. return -EFAULT;
  502. /* Adjust the counters */
  503. usrdata->len -= count;
  504. if (!usrdata->len)
  505. usrdata->rd_idx = 0;
  506. else
  507. usrdata->rd_idx += count;
  508. return count;
  509. }
  510. static ssize_t
  511. perf_read_irq_data(struct perf_counter *counter,
  512. char __user *buf,
  513. size_t count,
  514. int nonblocking)
  515. {
  516. struct perf_data *irqdata, *usrdata;
  517. DECLARE_WAITQUEUE(wait, current);
  518. ssize_t res;
  519. irqdata = counter->irqdata;
  520. usrdata = counter->usrdata;
  521. if (usrdata->len + irqdata->len >= count)
  522. goto read_pending;
  523. if (nonblocking)
  524. return -EAGAIN;
  525. spin_lock_irq(&counter->waitq.lock);
  526. __add_wait_queue(&counter->waitq, &wait);
  527. for (;;) {
  528. set_current_state(TASK_INTERRUPTIBLE);
  529. if (usrdata->len + irqdata->len >= count)
  530. break;
  531. if (signal_pending(current))
  532. break;
  533. spin_unlock_irq(&counter->waitq.lock);
  534. schedule();
  535. spin_lock_irq(&counter->waitq.lock);
  536. }
  537. __remove_wait_queue(&counter->waitq, &wait);
  538. __set_current_state(TASK_RUNNING);
  539. spin_unlock_irq(&counter->waitq.lock);
  540. if (usrdata->len + irqdata->len < count)
  541. return -ERESTARTSYS;
  542. read_pending:
  543. mutex_lock(&counter->mutex);
  544. /* Drain pending data first: */
  545. res = perf_copy_usrdata(usrdata, buf, count);
  546. if (res < 0 || res == count)
  547. goto out;
  548. /* Switch irq buffer: */
  549. usrdata = perf_switch_irq_data(counter);
  550. if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) {
  551. if (!res)
  552. res = -EFAULT;
  553. } else {
  554. res = count;
  555. }
  556. out:
  557. mutex_unlock(&counter->mutex);
  558. return res;
  559. }
  560. static ssize_t
  561. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  562. {
  563. struct perf_counter *counter = file->private_data;
  564. switch (counter->record_type) {
  565. case PERF_RECORD_SIMPLE:
  566. return perf_read_hw(counter, buf, count);
  567. case PERF_RECORD_IRQ:
  568. case PERF_RECORD_GROUP:
  569. return perf_read_irq_data(counter, buf, count,
  570. file->f_flags & O_NONBLOCK);
  571. }
  572. return -EINVAL;
  573. }
  574. static unsigned int perf_poll(struct file *file, poll_table *wait)
  575. {
  576. struct perf_counter *counter = file->private_data;
  577. unsigned int events = 0;
  578. unsigned long flags;
  579. poll_wait(file, &counter->waitq, wait);
  580. spin_lock_irqsave(&counter->waitq.lock, flags);
  581. if (counter->usrdata->len || counter->irqdata->len)
  582. events |= POLLIN;
  583. spin_unlock_irqrestore(&counter->waitq.lock, flags);
  584. return events;
  585. }
  586. static const struct file_operations perf_fops = {
  587. .release = perf_release,
  588. .read = perf_read,
  589. .poll = perf_poll,
  590. };
  591. /*
  592. * Allocate and initialize a counter structure
  593. */
  594. static struct perf_counter *
  595. perf_counter_alloc(u32 hw_event_period, int cpu, u32 record_type)
  596. {
  597. struct perf_counter *counter = kzalloc(sizeof(*counter), GFP_KERNEL);
  598. if (!counter)
  599. return NULL;
  600. mutex_init(&counter->mutex);
  601. INIT_LIST_HEAD(&counter->list);
  602. init_waitqueue_head(&counter->waitq);
  603. counter->irqdata = &counter->data[0];
  604. counter->usrdata = &counter->data[1];
  605. counter->cpu = cpu;
  606. counter->record_type = record_type;
  607. counter->__irq_period = hw_event_period;
  608. counter->wakeup_pending = 0;
  609. return counter;
  610. }
  611. /**
  612. * sys_perf_task_open - open a performance counter associate it to a task
  613. * @hw_event_type: event type for monitoring/sampling...
  614. * @pid: target pid
  615. */
  616. asmlinkage int
  617. sys_perf_counter_open(u32 hw_event_type,
  618. u32 hw_event_period,
  619. u32 record_type,
  620. pid_t pid,
  621. int cpu)
  622. {
  623. struct perf_counter_context *ctx;
  624. struct perf_counter *counter;
  625. int ret;
  626. ctx = find_get_context(pid, cpu);
  627. if (IS_ERR(ctx))
  628. return PTR_ERR(ctx);
  629. ret = -ENOMEM;
  630. counter = perf_counter_alloc(hw_event_period, cpu, record_type);
  631. if (!counter)
  632. goto err_put_context;
  633. ret = hw_perf_counter_init(counter, hw_event_type);
  634. if (ret)
  635. goto err_free_put_context;
  636. perf_install_in_context(ctx, counter, cpu);
  637. ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
  638. if (ret < 0)
  639. goto err_remove_free_put_context;
  640. return ret;
  641. err_remove_free_put_context:
  642. mutex_lock(&counter->mutex);
  643. perf_remove_from_context(counter);
  644. mutex_unlock(&counter->mutex);
  645. err_free_put_context:
  646. kfree(counter);
  647. err_put_context:
  648. put_context(ctx);
  649. return ret;
  650. }
  651. static void __cpuinit perf_init_cpu(int cpu)
  652. {
  653. struct perf_cpu_context *ctx;
  654. ctx = &per_cpu(perf_cpu_context, cpu);
  655. spin_lock_init(&ctx->ctx.lock);
  656. INIT_LIST_HEAD(&ctx->ctx.counters);
  657. mutex_lock(&perf_resource_mutex);
  658. ctx->max_pertask = perf_max_counters - perf_reserved_percpu;
  659. mutex_unlock(&perf_resource_mutex);
  660. hw_perf_counter_setup();
  661. }
  662. #ifdef CONFIG_HOTPLUG_CPU
  663. static void __perf_exit_cpu(void *info)
  664. {
  665. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  666. struct perf_counter_context *ctx = &cpuctx->ctx;
  667. struct perf_counter *counter, *tmp;
  668. list_for_each_entry_safe(counter, tmp, &ctx->counters, list)
  669. __perf_remove_from_context(counter);
  670. }
  671. static void perf_exit_cpu(int cpu)
  672. {
  673. smp_call_function_single(cpu, __perf_exit_cpu, NULL, 1);
  674. }
  675. #else
  676. static inline void perf_exit_cpu(int cpu) { }
  677. #endif
  678. static int __cpuinit
  679. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  680. {
  681. unsigned int cpu = (long)hcpu;
  682. switch (action) {
  683. case CPU_UP_PREPARE:
  684. case CPU_UP_PREPARE_FROZEN:
  685. perf_init_cpu(cpu);
  686. break;
  687. case CPU_DOWN_PREPARE:
  688. case CPU_DOWN_PREPARE_FROZEN:
  689. perf_exit_cpu(cpu);
  690. break;
  691. default:
  692. break;
  693. }
  694. return NOTIFY_OK;
  695. }
  696. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  697. .notifier_call = perf_cpu_notify,
  698. };
  699. static int __init perf_counter_init(void)
  700. {
  701. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  702. (void *)(long)smp_processor_id());
  703. register_cpu_notifier(&perf_cpu_nb);
  704. return 0;
  705. }
  706. early_initcall(perf_counter_init);
  707. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  708. {
  709. return sprintf(buf, "%d\n", perf_reserved_percpu);
  710. }
  711. static ssize_t
  712. perf_set_reserve_percpu(struct sysdev_class *class,
  713. const char *buf,
  714. size_t count)
  715. {
  716. struct perf_cpu_context *cpuctx;
  717. unsigned long val;
  718. int err, cpu, mpt;
  719. err = strict_strtoul(buf, 10, &val);
  720. if (err)
  721. return err;
  722. if (val > perf_max_counters)
  723. return -EINVAL;
  724. mutex_lock(&perf_resource_mutex);
  725. perf_reserved_percpu = val;
  726. for_each_online_cpu(cpu) {
  727. cpuctx = &per_cpu(perf_cpu_context, cpu);
  728. spin_lock_irq(&cpuctx->ctx.lock);
  729. mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
  730. perf_max_counters - perf_reserved_percpu);
  731. cpuctx->max_pertask = mpt;
  732. spin_unlock_irq(&cpuctx->ctx.lock);
  733. }
  734. mutex_unlock(&perf_resource_mutex);
  735. return count;
  736. }
  737. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  738. {
  739. return sprintf(buf, "%d\n", perf_overcommit);
  740. }
  741. static ssize_t
  742. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  743. {
  744. unsigned long val;
  745. int err;
  746. err = strict_strtoul(buf, 10, &val);
  747. if (err)
  748. return err;
  749. if (val > 1)
  750. return -EINVAL;
  751. mutex_lock(&perf_resource_mutex);
  752. perf_overcommit = val;
  753. mutex_unlock(&perf_resource_mutex);
  754. return count;
  755. }
  756. static SYSDEV_CLASS_ATTR(
  757. reserve_percpu,
  758. 0644,
  759. perf_show_reserve_percpu,
  760. perf_set_reserve_percpu
  761. );
  762. static SYSDEV_CLASS_ATTR(
  763. overcommit,
  764. 0644,
  765. perf_show_overcommit,
  766. perf_set_overcommit
  767. );
  768. static struct attribute *perfclass_attrs[] = {
  769. &attr_reserve_percpu.attr,
  770. &attr_overcommit.attr,
  771. NULL
  772. };
  773. static struct attribute_group perfclass_attr_group = {
  774. .attrs = perfclass_attrs,
  775. .name = "perf_counters",
  776. };
  777. static int __init perf_counter_sysfs_init(void)
  778. {
  779. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  780. &perfclass_attr_group);
  781. }
  782. device_initcall(perf_counter_sysfs_init);