perf_counter.c 25 KB

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