Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Performance counter core code |
| 3 | * |
Ingo Molnar | 9814451 | 2009-04-29 14:52:50 +0200 | [diff] [blame] | 4 | * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> |
| 5 | * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar |
| 6 | * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> |
Paul Mackerras | c5dd016 | 2009-04-30 09:48:16 +1000 | [diff] [blame] | 7 | * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 8 | * |
| 9 | * For licensing details see kernel-base/COPYING |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/fs.h> |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 13 | #include <linux/mm.h> |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 14 | #include <linux/cpu.h> |
| 15 | #include <linux/smp.h> |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 16 | #include <linux/file.h> |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 17 | #include <linux/poll.h> |
| 18 | #include <linux/sysfs.h> |
| 19 | #include <linux/ptrace.h> |
| 20 | #include <linux/percpu.h> |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 21 | #include <linux/vmstat.h> |
| 22 | #include <linux/hardirq.h> |
| 23 | #include <linux/rculist.h> |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 24 | #include <linux/uaccess.h> |
| 25 | #include <linux/syscalls.h> |
| 26 | #include <linux/anon_inodes.h> |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 27 | #include <linux/kernel_stat.h> |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 28 | #include <linux/perf_counter.h> |
Peter Zijlstra | 0a4a939 | 2009-03-30 19:07:05 +0200 | [diff] [blame] | 29 | #include <linux/dcache.h> |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 30 | |
Tim Blechmann | 4e193bd | 2009-03-14 14:29:25 +0100 | [diff] [blame] | 31 | #include <asm/irq_regs.h> |
| 32 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 33 | /* |
| 34 | * Each CPU has a list of per CPU counters: |
| 35 | */ |
| 36 | DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context); |
| 37 | |
Ingo Molnar | 088e285 | 2008-12-14 20:21:00 +0100 | [diff] [blame] | 38 | int perf_max_counters __read_mostly = 1; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 39 | static int perf_reserved_percpu __read_mostly; |
| 40 | static int perf_overcommit __read_mostly = 1; |
| 41 | |
Peter Zijlstra | 7fc23a5 | 2009-05-08 18:52:21 +0200 | [diff] [blame] | 42 | static atomic_t nr_counters __read_mostly; |
Peter Zijlstra | 9ee318a | 2009-04-09 10:53:44 +0200 | [diff] [blame] | 43 | static atomic_t nr_mmap_tracking __read_mostly; |
| 44 | static atomic_t nr_munmap_tracking __read_mostly; |
| 45 | static atomic_t nr_comm_tracking __read_mostly; |
| 46 | |
Peter Zijlstra | 1ccd154 | 2009-04-09 10:53:45 +0200 | [diff] [blame] | 47 | int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */ |
Peter Zijlstra | 789f90f | 2009-05-15 15:19:27 +0200 | [diff] [blame] | 48 | int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */ |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 49 | int sysctl_perf_counter_limit __read_mostly = 100000; /* max NMIs per second */ |
Peter Zijlstra | 1ccd154 | 2009-04-09 10:53:45 +0200 | [diff] [blame] | 50 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 51 | /* |
Ingo Molnar | 1dce8d9 | 2009-05-04 19:23:18 +0200 | [diff] [blame] | 52 | * Lock for (sysadmin-configurable) counter reservations: |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 53 | */ |
Ingo Molnar | 1dce8d9 | 2009-05-04 19:23:18 +0200 | [diff] [blame] | 54 | static DEFINE_SPINLOCK(perf_resource_lock); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * Architecture provided APIs - weak aliases: |
| 58 | */ |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 59 | extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 60 | { |
Paul Mackerras | ff6f054 | 2009-01-09 16:19:25 +1100 | [diff] [blame] | 61 | return NULL; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 64 | void __weak hw_perf_disable(void) { barrier(); } |
| 65 | void __weak hw_perf_enable(void) { barrier(); } |
| 66 | |
Paul Mackerras | 01d0287 | 2009-01-14 13:44:19 +1100 | [diff] [blame] | 67 | void __weak hw_perf_counter_setup(int cpu) { barrier(); } |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 68 | int __weak hw_perf_group_sched_in(struct perf_counter *group_leader, |
| 69 | struct perf_cpu_context *cpuctx, |
| 70 | struct perf_counter_context *ctx, int cpu) |
| 71 | { |
| 72 | return 0; |
| 73 | } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 74 | |
Paul Mackerras | 4eb96fc | 2009-01-09 17:24:34 +1100 | [diff] [blame] | 75 | void __weak perf_counter_print_debug(void) { } |
| 76 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 77 | static DEFINE_PER_CPU(int, disable_count); |
| 78 | |
| 79 | void __perf_disable(void) |
| 80 | { |
| 81 | __get_cpu_var(disable_count)++; |
| 82 | } |
| 83 | |
| 84 | bool __perf_enable(void) |
| 85 | { |
| 86 | return !--__get_cpu_var(disable_count); |
| 87 | } |
| 88 | |
| 89 | void perf_disable(void) |
| 90 | { |
| 91 | __perf_disable(); |
| 92 | hw_perf_disable(); |
| 93 | } |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 94 | |
| 95 | void perf_enable(void) |
| 96 | { |
| 97 | if (__perf_enable()) |
| 98 | hw_perf_enable(); |
| 99 | } |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 100 | |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 101 | static void get_ctx(struct perf_counter_context *ctx) |
| 102 | { |
| 103 | atomic_inc(&ctx->refcount); |
| 104 | } |
| 105 | |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 106 | static void free_ctx(struct rcu_head *head) |
| 107 | { |
| 108 | struct perf_counter_context *ctx; |
| 109 | |
| 110 | ctx = container_of(head, struct perf_counter_context, rcu_head); |
| 111 | kfree(ctx); |
| 112 | } |
| 113 | |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 114 | static void put_ctx(struct perf_counter_context *ctx) |
| 115 | { |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 116 | if (atomic_dec_and_test(&ctx->refcount)) { |
| 117 | if (ctx->parent_ctx) |
| 118 | put_ctx(ctx->parent_ctx); |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 119 | if (ctx->task) |
| 120 | put_task_struct(ctx->task); |
| 121 | call_rcu(&ctx->rcu_head, free_ctx); |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 122 | } |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 123 | } |
| 124 | |
Peter Zijlstra | fccc714 | 2009-05-23 18:28:56 +0200 | [diff] [blame] | 125 | /* |
| 126 | * Add a counter from the lists for its context. |
| 127 | * Must be called with ctx->mutex and ctx->lock held. |
| 128 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 129 | static void |
| 130 | list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx) |
| 131 | { |
| 132 | struct perf_counter *group_leader = counter->group_leader; |
| 133 | |
| 134 | /* |
| 135 | * Depending on whether it is a standalone or sibling counter, |
| 136 | * add it straight to the context's counter list, or to the group |
| 137 | * leader's sibling list: |
| 138 | */ |
Peter Zijlstra | 3df5eda | 2009-05-08 18:52:22 +0200 | [diff] [blame] | 139 | if (group_leader == counter) |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 140 | list_add_tail(&counter->list_entry, &ctx->counter_list); |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 141 | else { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 142 | list_add_tail(&counter->list_entry, &group_leader->sibling_list); |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 143 | group_leader->nr_siblings++; |
| 144 | } |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 145 | |
| 146 | list_add_rcu(&counter->event_entry, &ctx->event_list); |
Peter Zijlstra | 8bc2095 | 2009-05-15 20:45:59 +0200 | [diff] [blame] | 147 | ctx->nr_counters++; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 150 | /* |
| 151 | * Remove a counter from the lists for its context. |
Peter Zijlstra | fccc714 | 2009-05-23 18:28:56 +0200 | [diff] [blame] | 152 | * Must be called with ctx->mutex and ctx->lock held. |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 153 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 154 | static void |
| 155 | list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx) |
| 156 | { |
| 157 | struct perf_counter *sibling, *tmp; |
| 158 | |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 159 | if (list_empty(&counter->list_entry)) |
| 160 | return; |
Peter Zijlstra | 8bc2095 | 2009-05-15 20:45:59 +0200 | [diff] [blame] | 161 | ctx->nr_counters--; |
| 162 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 163 | list_del_init(&counter->list_entry); |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 164 | list_del_rcu(&counter->event_entry); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 165 | |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 166 | if (counter->group_leader != counter) |
| 167 | counter->group_leader->nr_siblings--; |
| 168 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 169 | /* |
| 170 | * If this was a group counter with sibling counters then |
| 171 | * upgrade the siblings to singleton counters by adding them |
| 172 | * to the context list directly: |
| 173 | */ |
| 174 | list_for_each_entry_safe(sibling, tmp, |
| 175 | &counter->sibling_list, list_entry) { |
| 176 | |
Peter Zijlstra | 7556423 | 2009-03-13 12:21:29 +0100 | [diff] [blame] | 177 | list_move_tail(&sibling->list_entry, &ctx->counter_list); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 178 | sibling->group_leader = sibling; |
| 179 | } |
| 180 | } |
| 181 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 182 | static void |
| 183 | counter_sched_out(struct perf_counter *counter, |
| 184 | struct perf_cpu_context *cpuctx, |
| 185 | struct perf_counter_context *ctx) |
| 186 | { |
| 187 | if (counter->state != PERF_COUNTER_STATE_ACTIVE) |
| 188 | return; |
| 189 | |
| 190 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Peter Zijlstra | 4af4998 | 2009-04-06 11:45:10 +0200 | [diff] [blame] | 191 | counter->tstamp_stopped = ctx->time; |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 192 | counter->pmu->disable(counter); |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 193 | counter->oncpu = -1; |
| 194 | |
| 195 | if (!is_software_counter(counter)) |
| 196 | cpuctx->active_oncpu--; |
| 197 | ctx->nr_active--; |
| 198 | if (counter->hw_event.exclusive || !cpuctx->active_oncpu) |
| 199 | cpuctx->exclusive = 0; |
| 200 | } |
| 201 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 202 | static void |
| 203 | group_sched_out(struct perf_counter *group_counter, |
| 204 | struct perf_cpu_context *cpuctx, |
| 205 | struct perf_counter_context *ctx) |
| 206 | { |
| 207 | struct perf_counter *counter; |
| 208 | |
| 209 | if (group_counter->state != PERF_COUNTER_STATE_ACTIVE) |
| 210 | return; |
| 211 | |
| 212 | counter_sched_out(group_counter, cpuctx, ctx); |
| 213 | |
| 214 | /* |
| 215 | * Schedule out siblings (if any): |
| 216 | */ |
| 217 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) |
| 218 | counter_sched_out(counter, cpuctx, ctx); |
| 219 | |
| 220 | if (group_counter->hw_event.exclusive) |
| 221 | cpuctx->exclusive = 0; |
| 222 | } |
| 223 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 224 | /* |
| 225 | * Cross CPU call to remove a performance counter |
| 226 | * |
| 227 | * We disable the counter on the hardware level first. After that we |
| 228 | * remove it from the context list. |
| 229 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 230 | static void __perf_counter_remove_from_context(void *info) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 231 | { |
| 232 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 233 | struct perf_counter *counter = info; |
| 234 | struct perf_counter_context *ctx = counter->ctx; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 235 | unsigned long flags; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 236 | |
| 237 | /* |
| 238 | * If this is a task context, we need to check whether it is |
| 239 | * the current task context of this cpu. If not it has been |
| 240 | * scheduled out before the smp call arrived. |
| 241 | */ |
| 242 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 243 | return; |
| 244 | |
Peter Zijlstra | 849691a | 2009-04-06 11:45:12 +0200 | [diff] [blame] | 245 | spin_lock_irqsave(&ctx->lock, flags); |
Ingo Molnar | 34adc80 | 2009-05-20 20:13:28 +0200 | [diff] [blame] | 246 | /* |
| 247 | * Protect the list operation against NMI by disabling the |
| 248 | * counters on a global level. |
| 249 | */ |
| 250 | perf_disable(); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 251 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 252 | counter_sched_out(counter, cpuctx, ctx); |
| 253 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 254 | list_del_counter(counter, ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 255 | |
| 256 | if (!ctx->task) { |
| 257 | /* |
| 258 | * Allow more per task counters with respect to the |
| 259 | * reservation: |
| 260 | */ |
| 261 | cpuctx->max_pertask = |
| 262 | min(perf_max_counters - ctx->nr_counters, |
| 263 | perf_max_counters - perf_reserved_percpu); |
| 264 | } |
| 265 | |
Ingo Molnar | 34adc80 | 2009-05-20 20:13:28 +0200 | [diff] [blame] | 266 | perf_enable(); |
Peter Zijlstra | 849691a | 2009-04-06 11:45:12 +0200 | [diff] [blame] | 267 | spin_unlock_irqrestore(&ctx->lock, flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | |
| 271 | /* |
| 272 | * Remove the counter from a task's (or a CPU's) list of counters. |
| 273 | * |
Peter Zijlstra | fccc714 | 2009-05-23 18:28:56 +0200 | [diff] [blame] | 274 | * Must be called with ctx->mutex held. |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 275 | * |
| 276 | * CPU counters are removed with a smp call. For task counters we only |
| 277 | * call when the task is on a CPU. |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 278 | * |
| 279 | * If counter->ctx is a cloned context, callers must make sure that |
| 280 | * every task struct that counter->ctx->task could possibly point to |
| 281 | * remains valid. This is OK when called from perf_release since |
| 282 | * that only calls us on the top-level context, which can't be a clone. |
| 283 | * When called from perf_counter_exit_task, it's OK because the |
| 284 | * context has been detached from its task. |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 285 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 286 | static void perf_counter_remove_from_context(struct perf_counter *counter) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 287 | { |
| 288 | struct perf_counter_context *ctx = counter->ctx; |
| 289 | struct task_struct *task = ctx->task; |
| 290 | |
| 291 | if (!task) { |
| 292 | /* |
| 293 | * Per cpu counters are removed via an smp call and |
| 294 | * the removal is always sucessful. |
| 295 | */ |
| 296 | smp_call_function_single(counter->cpu, |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 297 | __perf_counter_remove_from_context, |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 298 | counter, 1); |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | retry: |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 303 | task_oncpu_function_call(task, __perf_counter_remove_from_context, |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 304 | counter); |
| 305 | |
| 306 | spin_lock_irq(&ctx->lock); |
| 307 | /* |
| 308 | * If the context is active we need to retry the smp call. |
| 309 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 310 | if (ctx->nr_active && !list_empty(&counter->list_entry)) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 311 | spin_unlock_irq(&ctx->lock); |
| 312 | goto retry; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * The lock prevents that this context is scheduled in so we |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 317 | * can remove the counter safely, if the call above did not |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 318 | * succeed. |
| 319 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 320 | if (!list_empty(&counter->list_entry)) { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 321 | list_del_counter(counter, ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 322 | } |
| 323 | spin_unlock_irq(&ctx->lock); |
| 324 | } |
| 325 | |
Peter Zijlstra | 4af4998 | 2009-04-06 11:45:10 +0200 | [diff] [blame] | 326 | static inline u64 perf_clock(void) |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 327 | { |
Peter Zijlstra | 4af4998 | 2009-04-06 11:45:10 +0200 | [diff] [blame] | 328 | return cpu_clock(smp_processor_id()); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Update the record of the current time in a context. |
| 333 | */ |
Peter Zijlstra | 4af4998 | 2009-04-06 11:45:10 +0200 | [diff] [blame] | 334 | static void update_context_time(struct perf_counter_context *ctx) |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 335 | { |
Peter Zijlstra | 4af4998 | 2009-04-06 11:45:10 +0200 | [diff] [blame] | 336 | u64 now = perf_clock(); |
| 337 | |
| 338 | ctx->time += now - ctx->timestamp; |
| 339 | ctx->timestamp = now; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /* |
| 343 | * Update the total_time_enabled and total_time_running fields for a counter. |
| 344 | */ |
| 345 | static void update_counter_times(struct perf_counter *counter) |
| 346 | { |
| 347 | struct perf_counter_context *ctx = counter->ctx; |
| 348 | u64 run_end; |
| 349 | |
Peter Zijlstra | 4af4998 | 2009-04-06 11:45:10 +0200 | [diff] [blame] | 350 | if (counter->state < PERF_COUNTER_STATE_INACTIVE) |
| 351 | return; |
| 352 | |
| 353 | counter->total_time_enabled = ctx->time - counter->tstamp_enabled; |
| 354 | |
| 355 | if (counter->state == PERF_COUNTER_STATE_INACTIVE) |
| 356 | run_end = counter->tstamp_stopped; |
| 357 | else |
| 358 | run_end = ctx->time; |
| 359 | |
| 360 | counter->total_time_running = run_end - counter->tstamp_running; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Update total_time_enabled and total_time_running for all counters in a group. |
| 365 | */ |
| 366 | static void update_group_times(struct perf_counter *leader) |
| 367 | { |
| 368 | struct perf_counter *counter; |
| 369 | |
| 370 | update_counter_times(leader); |
| 371 | list_for_each_entry(counter, &leader->sibling_list, list_entry) |
| 372 | update_counter_times(counter); |
| 373 | } |
| 374 | |
| 375 | /* |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 376 | * Cross CPU call to disable a performance counter |
| 377 | */ |
| 378 | static void __perf_counter_disable(void *info) |
| 379 | { |
| 380 | struct perf_counter *counter = info; |
| 381 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 382 | struct perf_counter_context *ctx = counter->ctx; |
| 383 | unsigned long flags; |
| 384 | |
| 385 | /* |
| 386 | * If this is a per-task counter, need to check whether this |
| 387 | * counter's task is the current task on this cpu. |
| 388 | */ |
| 389 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 390 | return; |
| 391 | |
Peter Zijlstra | 849691a | 2009-04-06 11:45:12 +0200 | [diff] [blame] | 392 | spin_lock_irqsave(&ctx->lock, flags); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 393 | |
| 394 | /* |
| 395 | * If the counter is on, turn it off. |
| 396 | * If it is in error state, leave it in error state. |
| 397 | */ |
| 398 | if (counter->state >= PERF_COUNTER_STATE_INACTIVE) { |
Peter Zijlstra | 4af4998 | 2009-04-06 11:45:10 +0200 | [diff] [blame] | 399 | update_context_time(ctx); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 400 | update_counter_times(counter); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 401 | if (counter == counter->group_leader) |
| 402 | group_sched_out(counter, cpuctx, ctx); |
| 403 | else |
| 404 | counter_sched_out(counter, cpuctx, ctx); |
| 405 | counter->state = PERF_COUNTER_STATE_OFF; |
| 406 | } |
| 407 | |
Peter Zijlstra | 849691a | 2009-04-06 11:45:12 +0200 | [diff] [blame] | 408 | spin_unlock_irqrestore(&ctx->lock, flags); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Disable a counter. |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 413 | * |
| 414 | * If counter->ctx is a cloned context, callers must make sure that |
| 415 | * every task struct that counter->ctx->task could possibly point to |
| 416 | * remains valid. This condition is satisifed when called through |
| 417 | * perf_counter_for_each_child or perf_counter_for_each because they |
| 418 | * hold the top-level counter's child_mutex, so any descendant that |
| 419 | * goes to exit will block in sync_child_counter. |
| 420 | * When called from perf_pending_counter it's OK because counter->ctx |
| 421 | * is the current context on this CPU and preemption is disabled, |
| 422 | * hence we can't get into perf_counter_task_sched_out for this context. |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 423 | */ |
| 424 | static void perf_counter_disable(struct perf_counter *counter) |
| 425 | { |
| 426 | struct perf_counter_context *ctx = counter->ctx; |
| 427 | struct task_struct *task = ctx->task; |
| 428 | |
| 429 | if (!task) { |
| 430 | /* |
| 431 | * Disable the counter on the cpu that it's on |
| 432 | */ |
| 433 | smp_call_function_single(counter->cpu, __perf_counter_disable, |
| 434 | counter, 1); |
| 435 | return; |
| 436 | } |
| 437 | |
| 438 | retry: |
| 439 | task_oncpu_function_call(task, __perf_counter_disable, counter); |
| 440 | |
| 441 | spin_lock_irq(&ctx->lock); |
| 442 | /* |
| 443 | * If the counter is still active, we need to retry the cross-call. |
| 444 | */ |
| 445 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) { |
| 446 | spin_unlock_irq(&ctx->lock); |
| 447 | goto retry; |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Since we have the lock this context can't be scheduled |
| 452 | * in, so we can change the state safely. |
| 453 | */ |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 454 | if (counter->state == PERF_COUNTER_STATE_INACTIVE) { |
| 455 | update_counter_times(counter); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 456 | counter->state = PERF_COUNTER_STATE_OFF; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 457 | } |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 458 | |
| 459 | spin_unlock_irq(&ctx->lock); |
| 460 | } |
| 461 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 462 | static int |
| 463 | counter_sched_in(struct perf_counter *counter, |
| 464 | struct perf_cpu_context *cpuctx, |
| 465 | struct perf_counter_context *ctx, |
| 466 | int cpu) |
| 467 | { |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 468 | if (counter->state <= PERF_COUNTER_STATE_OFF) |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 469 | return 0; |
| 470 | |
| 471 | counter->state = PERF_COUNTER_STATE_ACTIVE; |
| 472 | counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */ |
| 473 | /* |
| 474 | * The new state must be visible before we turn it on in the hardware: |
| 475 | */ |
| 476 | smp_wmb(); |
| 477 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 478 | if (counter->pmu->enable(counter)) { |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 479 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
| 480 | counter->oncpu = -1; |
| 481 | return -EAGAIN; |
| 482 | } |
| 483 | |
Peter Zijlstra | 4af4998 | 2009-04-06 11:45:10 +0200 | [diff] [blame] | 484 | counter->tstamp_running += ctx->time - counter->tstamp_stopped; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 485 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 486 | if (!is_software_counter(counter)) |
| 487 | cpuctx->active_oncpu++; |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 488 | ctx->nr_active++; |
| 489 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 490 | if (counter->hw_event.exclusive) |
| 491 | cpuctx->exclusive = 1; |
| 492 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
Paul Mackerras | 6751b71 | 2009-05-11 12:08:02 +1000 | [diff] [blame] | 496 | static int |
| 497 | group_sched_in(struct perf_counter *group_counter, |
| 498 | struct perf_cpu_context *cpuctx, |
| 499 | struct perf_counter_context *ctx, |
| 500 | int cpu) |
| 501 | { |
| 502 | struct perf_counter *counter, *partial_group; |
| 503 | int ret; |
| 504 | |
| 505 | if (group_counter->state == PERF_COUNTER_STATE_OFF) |
| 506 | return 0; |
| 507 | |
| 508 | ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu); |
| 509 | if (ret) |
| 510 | return ret < 0 ? ret : 0; |
| 511 | |
| 512 | group_counter->prev_state = group_counter->state; |
| 513 | if (counter_sched_in(group_counter, cpuctx, ctx, cpu)) |
| 514 | return -EAGAIN; |
| 515 | |
| 516 | /* |
| 517 | * Schedule in siblings as one group (if any): |
| 518 | */ |
| 519 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) { |
| 520 | counter->prev_state = counter->state; |
| 521 | if (counter_sched_in(counter, cpuctx, ctx, cpu)) { |
| 522 | partial_group = counter; |
| 523 | goto group_error; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | return 0; |
| 528 | |
| 529 | group_error: |
| 530 | /* |
| 531 | * Groups can be scheduled in as one unit only, so undo any |
| 532 | * partial group before returning: |
| 533 | */ |
| 534 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) { |
| 535 | if (counter == partial_group) |
| 536 | break; |
| 537 | counter_sched_out(counter, cpuctx, ctx); |
| 538 | } |
| 539 | counter_sched_out(group_counter, cpuctx, ctx); |
| 540 | |
| 541 | return -EAGAIN; |
| 542 | } |
| 543 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 544 | /* |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 545 | * Return 1 for a group consisting entirely of software counters, |
| 546 | * 0 if the group contains any hardware counters. |
| 547 | */ |
| 548 | static int is_software_only_group(struct perf_counter *leader) |
| 549 | { |
| 550 | struct perf_counter *counter; |
| 551 | |
| 552 | if (!is_software_counter(leader)) |
| 553 | return 0; |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 554 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 555 | list_for_each_entry(counter, &leader->sibling_list, list_entry) |
| 556 | if (!is_software_counter(counter)) |
| 557 | return 0; |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 558 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 559 | return 1; |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * Work out whether we can put this counter group on the CPU now. |
| 564 | */ |
| 565 | static int group_can_go_on(struct perf_counter *counter, |
| 566 | struct perf_cpu_context *cpuctx, |
| 567 | int can_add_hw) |
| 568 | { |
| 569 | /* |
| 570 | * Groups consisting entirely of software counters can always go on. |
| 571 | */ |
| 572 | if (is_software_only_group(counter)) |
| 573 | return 1; |
| 574 | /* |
| 575 | * If an exclusive group is already on, no other hardware |
| 576 | * counters can go on. |
| 577 | */ |
| 578 | if (cpuctx->exclusive) |
| 579 | return 0; |
| 580 | /* |
| 581 | * If this group is exclusive and there are already |
| 582 | * counters on the CPU, it can't go on. |
| 583 | */ |
| 584 | if (counter->hw_event.exclusive && cpuctx->active_oncpu) |
| 585 | return 0; |
| 586 | /* |
| 587 | * Otherwise, try to add it if all previous groups were able |
| 588 | * to go on. |
| 589 | */ |
| 590 | return can_add_hw; |
| 591 | } |
| 592 | |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 593 | static void add_counter_to_ctx(struct perf_counter *counter, |
| 594 | struct perf_counter_context *ctx) |
| 595 | { |
| 596 | list_add_counter(counter, ctx); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 597 | counter->prev_state = PERF_COUNTER_STATE_OFF; |
Peter Zijlstra | 4af4998 | 2009-04-06 11:45:10 +0200 | [diff] [blame] | 598 | counter->tstamp_enabled = ctx->time; |
| 599 | counter->tstamp_running = ctx->time; |
| 600 | counter->tstamp_stopped = ctx->time; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 601 | } |
| 602 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 603 | /* |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 604 | * Cross CPU call to install and enable a performance counter |
Peter Zijlstra | 682076a | 2009-05-23 18:28:57 +0200 | [diff] [blame] | 605 | * |
| 606 | * Must be called with ctx->mutex held |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 607 | */ |
| 608 | static void __perf_install_in_context(void *info) |
| 609 | { |
| 610 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 611 | struct perf_counter *counter = info; |
| 612 | struct perf_counter_context *ctx = counter->ctx; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 613 | struct perf_counter *leader = counter->group_leader; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 614 | int cpu = smp_processor_id(); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 615 | unsigned long flags; |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 616 | int err; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 617 | |
| 618 | /* |
| 619 | * If this is a task context, we need to check whether it is |
| 620 | * the current task context of this cpu. If not it has been |
| 621 | * scheduled out before the smp call arrived. |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 622 | * Or possibly this is the right context but it isn't |
| 623 | * on this cpu because it had no counters. |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 624 | */ |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 625 | if (ctx->task && cpuctx->task_ctx != ctx) { |
| 626 | if (cpuctx->task_ctx || ctx->task != current) |
| 627 | return; |
| 628 | cpuctx->task_ctx = ctx; |
| 629 | } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 630 | |
Peter Zijlstra | 849691a | 2009-04-06 11:45:12 +0200 | [diff] [blame] | 631 | spin_lock_irqsave(&ctx->lock, flags); |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 632 | ctx->is_active = 1; |
Peter Zijlstra | 4af4998 | 2009-04-06 11:45:10 +0200 | [diff] [blame] | 633 | update_context_time(ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 634 | |
| 635 | /* |
| 636 | * Protect the list operation against NMI by disabling the |
| 637 | * counters on a global level. NOP for non NMI based counters. |
| 638 | */ |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 639 | perf_disable(); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 640 | |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 641 | add_counter_to_ctx(counter, ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 642 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 643 | /* |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 644 | * Don't put the counter on if it is disabled or if |
| 645 | * it is in a group and the group isn't on. |
| 646 | */ |
| 647 | if (counter->state != PERF_COUNTER_STATE_INACTIVE || |
| 648 | (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)) |
| 649 | goto unlock; |
| 650 | |
| 651 | /* |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 652 | * An exclusive counter can't go on if there are already active |
| 653 | * hardware counters, and no hardware counter can go on if there |
| 654 | * is already an exclusive counter on. |
| 655 | */ |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 656 | if (!group_can_go_on(counter, cpuctx, 1)) |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 657 | err = -EEXIST; |
| 658 | else |
| 659 | err = counter_sched_in(counter, cpuctx, ctx, cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 660 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 661 | if (err) { |
| 662 | /* |
| 663 | * This counter couldn't go on. If it is in a group |
| 664 | * then we have to pull the whole group off. |
| 665 | * If the counter group is pinned then put it in error state. |
| 666 | */ |
| 667 | if (leader != counter) |
| 668 | group_sched_out(leader, cpuctx, ctx); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 669 | if (leader->hw_event.pinned) { |
| 670 | update_group_times(leader); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 671 | leader->state = PERF_COUNTER_STATE_ERROR; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 672 | } |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 673 | } |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 674 | |
| 675 | if (!err && !ctx->task && cpuctx->max_pertask) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 676 | cpuctx->max_pertask--; |
| 677 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 678 | unlock: |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 679 | perf_enable(); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 680 | |
Peter Zijlstra | 849691a | 2009-04-06 11:45:12 +0200 | [diff] [blame] | 681 | spin_unlock_irqrestore(&ctx->lock, flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /* |
| 685 | * Attach a performance counter to a context |
| 686 | * |
| 687 | * First we add the counter to the list with the hardware enable bit |
| 688 | * in counter->hw_config cleared. |
| 689 | * |
| 690 | * If the counter is attached to a task which is on a CPU we use a smp |
| 691 | * call to enable it in the task context. The task might have been |
| 692 | * scheduled away, but we check this in the smp call again. |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 693 | * |
| 694 | * Must be called with ctx->mutex held. |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 695 | */ |
| 696 | static void |
| 697 | perf_install_in_context(struct perf_counter_context *ctx, |
| 698 | struct perf_counter *counter, |
| 699 | int cpu) |
| 700 | { |
| 701 | struct task_struct *task = ctx->task; |
| 702 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 703 | if (!task) { |
| 704 | /* |
| 705 | * Per cpu counters are installed via an smp call and |
| 706 | * the install is always sucessful. |
| 707 | */ |
| 708 | smp_call_function_single(cpu, __perf_install_in_context, |
| 709 | counter, 1); |
| 710 | return; |
| 711 | } |
| 712 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 713 | retry: |
| 714 | task_oncpu_function_call(task, __perf_install_in_context, |
| 715 | counter); |
| 716 | |
| 717 | spin_lock_irq(&ctx->lock); |
| 718 | /* |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 719 | * we need to retry the smp call. |
| 720 | */ |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 721 | if (ctx->is_active && list_empty(&counter->list_entry)) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 722 | spin_unlock_irq(&ctx->lock); |
| 723 | goto retry; |
| 724 | } |
| 725 | |
| 726 | /* |
| 727 | * The lock prevents that this context is scheduled in so we |
| 728 | * can add the counter safely, if it the call above did not |
| 729 | * succeed. |
| 730 | */ |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 731 | if (list_empty(&counter->list_entry)) |
| 732 | add_counter_to_ctx(counter, ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 733 | spin_unlock_irq(&ctx->lock); |
| 734 | } |
| 735 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 736 | /* |
| 737 | * Cross CPU call to enable a performance counter |
| 738 | */ |
| 739 | static void __perf_counter_enable(void *info) |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 740 | { |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 741 | struct perf_counter *counter = info; |
| 742 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 743 | struct perf_counter_context *ctx = counter->ctx; |
| 744 | struct perf_counter *leader = counter->group_leader; |
| 745 | unsigned long flags; |
| 746 | int err; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 747 | |
| 748 | /* |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 749 | * If this is a per-task counter, need to check whether this |
| 750 | * counter's task is the current task on this cpu. |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 751 | */ |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 752 | if (ctx->task && cpuctx->task_ctx != ctx) { |
| 753 | if (cpuctx->task_ctx || ctx->task != current) |
| 754 | return; |
| 755 | cpuctx->task_ctx = ctx; |
| 756 | } |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 757 | |
Peter Zijlstra | 849691a | 2009-04-06 11:45:12 +0200 | [diff] [blame] | 758 | spin_lock_irqsave(&ctx->lock, flags); |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 759 | ctx->is_active = 1; |
Peter Zijlstra | 4af4998 | 2009-04-06 11:45:10 +0200 | [diff] [blame] | 760 | update_context_time(ctx); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 761 | |
Paul Mackerras | c07c99b | 2009-02-13 22:10:34 +1100 | [diff] [blame] | 762 | counter->prev_state = counter->state; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 763 | if (counter->state >= PERF_COUNTER_STATE_INACTIVE) |
| 764 | goto unlock; |
| 765 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Peter Zijlstra | 4af4998 | 2009-04-06 11:45:10 +0200 | [diff] [blame] | 766 | counter->tstamp_enabled = ctx->time - counter->total_time_enabled; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 767 | |
| 768 | /* |
| 769 | * If the counter is in a group and isn't the group leader, |
| 770 | * then don't put it on unless the group is on. |
| 771 | */ |
| 772 | if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE) |
| 773 | goto unlock; |
| 774 | |
Paul Mackerras | e758a33 | 2009-05-12 21:59:01 +1000 | [diff] [blame] | 775 | if (!group_can_go_on(counter, cpuctx, 1)) { |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 776 | err = -EEXIST; |
Paul Mackerras | e758a33 | 2009-05-12 21:59:01 +1000 | [diff] [blame] | 777 | } else { |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 778 | perf_disable(); |
Paul Mackerras | e758a33 | 2009-05-12 21:59:01 +1000 | [diff] [blame] | 779 | if (counter == leader) |
| 780 | err = group_sched_in(counter, cpuctx, ctx, |
| 781 | smp_processor_id()); |
| 782 | else |
| 783 | err = counter_sched_in(counter, cpuctx, ctx, |
| 784 | smp_processor_id()); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 785 | perf_enable(); |
Paul Mackerras | e758a33 | 2009-05-12 21:59:01 +1000 | [diff] [blame] | 786 | } |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 787 | |
| 788 | if (err) { |
| 789 | /* |
| 790 | * If this counter can't go on and it's part of a |
| 791 | * group, then the whole group has to come off. |
| 792 | */ |
| 793 | if (leader != counter) |
| 794 | group_sched_out(leader, cpuctx, ctx); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 795 | if (leader->hw_event.pinned) { |
| 796 | update_group_times(leader); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 797 | leader->state = PERF_COUNTER_STATE_ERROR; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 798 | } |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | unlock: |
Peter Zijlstra | 849691a | 2009-04-06 11:45:12 +0200 | [diff] [blame] | 802 | spin_unlock_irqrestore(&ctx->lock, flags); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /* |
| 806 | * Enable a counter. |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 807 | * |
| 808 | * If counter->ctx is a cloned context, callers must make sure that |
| 809 | * every task struct that counter->ctx->task could possibly point to |
| 810 | * remains valid. This condition is satisfied when called through |
| 811 | * perf_counter_for_each_child or perf_counter_for_each as described |
| 812 | * for perf_counter_disable. |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 813 | */ |
| 814 | static void perf_counter_enable(struct perf_counter *counter) |
| 815 | { |
| 816 | struct perf_counter_context *ctx = counter->ctx; |
| 817 | struct task_struct *task = ctx->task; |
| 818 | |
| 819 | if (!task) { |
| 820 | /* |
| 821 | * Enable the counter on the cpu that it's on |
| 822 | */ |
| 823 | smp_call_function_single(counter->cpu, __perf_counter_enable, |
| 824 | counter, 1); |
| 825 | return; |
| 826 | } |
| 827 | |
| 828 | spin_lock_irq(&ctx->lock); |
| 829 | if (counter->state >= PERF_COUNTER_STATE_INACTIVE) |
| 830 | goto out; |
| 831 | |
| 832 | /* |
| 833 | * If the counter is in error state, clear that first. |
| 834 | * That way, if we see the counter in error state below, we |
| 835 | * know that it has gone back into error state, as distinct |
| 836 | * from the task having been scheduled away before the |
| 837 | * cross-call arrived. |
| 838 | */ |
| 839 | if (counter->state == PERF_COUNTER_STATE_ERROR) |
| 840 | counter->state = PERF_COUNTER_STATE_OFF; |
| 841 | |
| 842 | retry: |
| 843 | spin_unlock_irq(&ctx->lock); |
| 844 | task_oncpu_function_call(task, __perf_counter_enable, counter); |
| 845 | |
| 846 | spin_lock_irq(&ctx->lock); |
| 847 | |
| 848 | /* |
| 849 | * If the context is active and the counter is still off, |
| 850 | * we need to retry the cross-call. |
| 851 | */ |
| 852 | if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF) |
| 853 | goto retry; |
| 854 | |
| 855 | /* |
| 856 | * Since we have the lock this context can't be scheduled |
| 857 | * in, so we can change the state safely. |
| 858 | */ |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 859 | if (counter->state == PERF_COUNTER_STATE_OFF) { |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 860 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Peter Zijlstra | 4af4998 | 2009-04-06 11:45:10 +0200 | [diff] [blame] | 861 | counter->tstamp_enabled = |
| 862 | ctx->time - counter->total_time_enabled; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 863 | } |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 864 | out: |
| 865 | spin_unlock_irq(&ctx->lock); |
| 866 | } |
| 867 | |
Peter Zijlstra | 2023b35 | 2009-05-05 17:50:26 +0200 | [diff] [blame] | 868 | static int perf_counter_refresh(struct perf_counter *counter, int refresh) |
Peter Zijlstra | 79f1464 | 2009-04-06 11:45:07 +0200 | [diff] [blame] | 869 | { |
Peter Zijlstra | 2023b35 | 2009-05-05 17:50:26 +0200 | [diff] [blame] | 870 | /* |
| 871 | * not supported on inherited counters |
| 872 | */ |
| 873 | if (counter->hw_event.inherit) |
| 874 | return -EINVAL; |
| 875 | |
Peter Zijlstra | 79f1464 | 2009-04-06 11:45:07 +0200 | [diff] [blame] | 876 | atomic_add(refresh, &counter->event_limit); |
| 877 | perf_counter_enable(counter); |
Peter Zijlstra | 2023b35 | 2009-05-05 17:50:26 +0200 | [diff] [blame] | 878 | |
| 879 | return 0; |
Peter Zijlstra | 79f1464 | 2009-04-06 11:45:07 +0200 | [diff] [blame] | 880 | } |
| 881 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 882 | void __perf_counter_sched_out(struct perf_counter_context *ctx, |
| 883 | struct perf_cpu_context *cpuctx) |
| 884 | { |
| 885 | struct perf_counter *counter; |
| 886 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 887 | spin_lock(&ctx->lock); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 888 | ctx->is_active = 0; |
| 889 | if (likely(!ctx->nr_counters)) |
| 890 | goto out; |
Peter Zijlstra | 4af4998 | 2009-04-06 11:45:10 +0200 | [diff] [blame] | 891 | update_context_time(ctx); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 892 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 893 | perf_disable(); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 894 | if (ctx->nr_active) { |
Peter Zijlstra | afedadf | 2009-05-20 12:21:22 +0200 | [diff] [blame] | 895 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
| 896 | if (counter != counter->group_leader) |
| 897 | counter_sched_out(counter, cpuctx, ctx); |
| 898 | else |
| 899 | group_sched_out(counter, cpuctx, ctx); |
| 900 | } |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 901 | } |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 902 | perf_enable(); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 903 | out: |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 904 | spin_unlock(&ctx->lock); |
| 905 | } |
| 906 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 907 | /* |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 908 | * Test whether two contexts are equivalent, i.e. whether they |
| 909 | * have both been cloned from the same version of the same context |
| 910 | * and they both have the same number of enabled counters. |
| 911 | * If the number of enabled counters is the same, then the set |
| 912 | * of enabled counters should be the same, because these are both |
| 913 | * inherited contexts, therefore we can't access individual counters |
| 914 | * in them directly with an fd; we can only enable/disable all |
| 915 | * counters via prctl, or enable/disable all counters in a family |
| 916 | * via ioctl, which will have the same effect on both contexts. |
| 917 | */ |
| 918 | static int context_equiv(struct perf_counter_context *ctx1, |
| 919 | struct perf_counter_context *ctx2) |
| 920 | { |
| 921 | return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 922 | && ctx1->parent_gen == ctx2->parent_gen |
| 923 | && ctx1->parent_gen != ~0ull; |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | /* |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 927 | * Called from scheduler to remove the counters of the current task, |
| 928 | * with interrupts disabled. |
| 929 | * |
| 930 | * We stop each counter and update the counter value in counter->count. |
| 931 | * |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 932 | * This does not protect us against NMI, but disable() |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 933 | * sets the disabled bit in the control field of counter _before_ |
| 934 | * accessing the counter control register. If a NMI hits, then it will |
| 935 | * not restart the counter. |
| 936 | */ |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 937 | void perf_counter_task_sched_out(struct task_struct *task, |
| 938 | struct task_struct *next, int cpu) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 939 | { |
| 940 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 941 | struct perf_counter_context *ctx = task->perf_counter_ctxp; |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 942 | struct perf_counter_context *next_ctx; |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 943 | struct perf_counter_context *parent; |
Peter Zijlstra | 4a0deca | 2009-03-19 20:26:12 +0100 | [diff] [blame] | 944 | struct pt_regs *regs; |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 945 | int do_switch = 1; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 946 | |
Peter Zijlstra | 10989fb | 2009-05-25 14:45:28 +0200 | [diff] [blame] | 947 | regs = task_pt_regs(task); |
| 948 | perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0); |
| 949 | |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 950 | if (likely(!ctx || !cpuctx->task_ctx)) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 951 | return; |
| 952 | |
Peter Zijlstra | bce379b | 2009-04-06 11:45:13 +0200 | [diff] [blame] | 953 | update_context_time(ctx); |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 954 | |
| 955 | rcu_read_lock(); |
| 956 | parent = rcu_dereference(ctx->parent_ctx); |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 957 | next_ctx = next->perf_counter_ctxp; |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 958 | if (parent && next_ctx && |
| 959 | rcu_dereference(next_ctx->parent_ctx) == parent) { |
| 960 | /* |
| 961 | * Looks like the two contexts are clones, so we might be |
| 962 | * able to optimize the context switch. We lock both |
| 963 | * contexts and check that they are clones under the |
| 964 | * lock (including re-checking that neither has been |
| 965 | * uncloned in the meantime). It doesn't matter which |
| 966 | * order we take the locks because no other cpu could |
| 967 | * be trying to lock both of these tasks. |
| 968 | */ |
| 969 | spin_lock(&ctx->lock); |
| 970 | spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); |
| 971 | if (context_equiv(ctx, next_ctx)) { |
| 972 | task->perf_counter_ctxp = next_ctx; |
| 973 | next->perf_counter_ctxp = ctx; |
| 974 | ctx->task = next; |
| 975 | next_ctx->task = task; |
| 976 | do_switch = 0; |
| 977 | } |
| 978 | spin_unlock(&next_ctx->lock); |
| 979 | spin_unlock(&ctx->lock); |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 980 | } |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 981 | rcu_read_unlock(); |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 982 | |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 983 | if (do_switch) { |
| 984 | __perf_counter_sched_out(ctx, cpuctx); |
| 985 | cpuctx->task_ctx = NULL; |
| 986 | } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 987 | } |
| 988 | |
Paul Mackerras | a08b159 | 2009-05-11 15:46:10 +1000 | [diff] [blame] | 989 | static void __perf_counter_task_sched_out(struct perf_counter_context *ctx) |
| 990 | { |
| 991 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 992 | |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 993 | if (!cpuctx->task_ctx) |
| 994 | return; |
Paul Mackerras | a08b159 | 2009-05-11 15:46:10 +1000 | [diff] [blame] | 995 | __perf_counter_sched_out(ctx, cpuctx); |
| 996 | cpuctx->task_ctx = NULL; |
| 997 | } |
| 998 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 999 | static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx) |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1000 | { |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1001 | __perf_counter_sched_out(&cpuctx->ctx, cpuctx); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1002 | } |
| 1003 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1004 | static void |
| 1005 | __perf_counter_sched_in(struct perf_counter_context *ctx, |
| 1006 | struct perf_cpu_context *cpuctx, int cpu) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1007 | { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1008 | struct perf_counter *counter; |
Paul Mackerras | dd0e6ba | 2009-01-12 15:11:00 +1100 | [diff] [blame] | 1009 | int can_add_hw = 1; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1010 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1011 | spin_lock(&ctx->lock); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1012 | ctx->is_active = 1; |
| 1013 | if (likely(!ctx->nr_counters)) |
| 1014 | goto out; |
| 1015 | |
Peter Zijlstra | 4af4998 | 2009-04-06 11:45:10 +0200 | [diff] [blame] | 1016 | ctx->timestamp = perf_clock(); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1017 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1018 | perf_disable(); |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 1019 | |
| 1020 | /* |
| 1021 | * First go through the list and put on any pinned groups |
| 1022 | * in order to give them the best chance of going on. |
| 1023 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1024 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 1025 | if (counter->state <= PERF_COUNTER_STATE_OFF || |
| 1026 | !counter->hw_event.pinned) |
| 1027 | continue; |
| 1028 | if (counter->cpu != -1 && counter->cpu != cpu) |
| 1029 | continue; |
| 1030 | |
Peter Zijlstra | afedadf | 2009-05-20 12:21:22 +0200 | [diff] [blame] | 1031 | if (counter != counter->group_leader) |
| 1032 | counter_sched_in(counter, cpuctx, ctx, cpu); |
| 1033 | else { |
| 1034 | if (group_can_go_on(counter, cpuctx, 1)) |
| 1035 | group_sched_in(counter, cpuctx, ctx, cpu); |
| 1036 | } |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 1037 | |
| 1038 | /* |
| 1039 | * If this pinned group hasn't been scheduled, |
| 1040 | * put it in error state. |
| 1041 | */ |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1042 | if (counter->state == PERF_COUNTER_STATE_INACTIVE) { |
| 1043 | update_group_times(counter); |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 1044 | counter->state = PERF_COUNTER_STATE_ERROR; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1045 | } |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
| 1049 | /* |
| 1050 | * Ignore counters in OFF or ERROR state, and |
| 1051 | * ignore pinned counters since we did them already. |
| 1052 | */ |
| 1053 | if (counter->state <= PERF_COUNTER_STATE_OFF || |
| 1054 | counter->hw_event.pinned) |
| 1055 | continue; |
| 1056 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1057 | /* |
| 1058 | * Listen to the 'cpu' scheduling filter constraint |
| 1059 | * of counters: |
| 1060 | */ |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1061 | if (counter->cpu != -1 && counter->cpu != cpu) |
| 1062 | continue; |
| 1063 | |
Peter Zijlstra | afedadf | 2009-05-20 12:21:22 +0200 | [diff] [blame] | 1064 | if (counter != counter->group_leader) { |
| 1065 | if (counter_sched_in(counter, cpuctx, ctx, cpu)) |
Paul Mackerras | dd0e6ba | 2009-01-12 15:11:00 +1100 | [diff] [blame] | 1066 | can_add_hw = 0; |
Peter Zijlstra | afedadf | 2009-05-20 12:21:22 +0200 | [diff] [blame] | 1067 | } else { |
| 1068 | if (group_can_go_on(counter, cpuctx, can_add_hw)) { |
| 1069 | if (group_sched_in(counter, cpuctx, ctx, cpu)) |
| 1070 | can_add_hw = 0; |
| 1071 | } |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 1072 | } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1073 | } |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1074 | perf_enable(); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1075 | out: |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1076 | spin_unlock(&ctx->lock); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1077 | } |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1078 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1079 | /* |
| 1080 | * Called from scheduler to add the counters of the current task |
| 1081 | * with interrupts disabled. |
| 1082 | * |
| 1083 | * We restore the counter value and then enable it. |
| 1084 | * |
| 1085 | * This does not protect us against NMI, but enable() |
| 1086 | * sets the enabled bit in the control field of counter _before_ |
| 1087 | * accessing the counter control register. If a NMI hits, then it will |
| 1088 | * keep the counter running. |
| 1089 | */ |
| 1090 | void perf_counter_task_sched_in(struct task_struct *task, int cpu) |
| 1091 | { |
| 1092 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 1093 | struct perf_counter_context *ctx = task->perf_counter_ctxp; |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1094 | |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 1095 | if (likely(!ctx)) |
| 1096 | return; |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 1097 | if (cpuctx->task_ctx == ctx) |
| 1098 | return; |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1099 | __perf_counter_sched_in(ctx, cpuctx, cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1100 | cpuctx->task_ctx = ctx; |
| 1101 | } |
| 1102 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1103 | static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu) |
| 1104 | { |
| 1105 | struct perf_counter_context *ctx = &cpuctx->ctx; |
| 1106 | |
| 1107 | __perf_counter_sched_in(ctx, cpuctx, cpu); |
| 1108 | } |
| 1109 | |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 1110 | #define MAX_INTERRUPTS (~0ULL) |
| 1111 | |
| 1112 | static void perf_log_throttle(struct perf_counter *counter, int enable); |
Peter Zijlstra | 26b119b | 2009-05-20 12:21:20 +0200 | [diff] [blame] | 1113 | static void perf_log_period(struct perf_counter *counter, u64 period); |
| 1114 | |
| 1115 | static void perf_adjust_freq(struct perf_counter_context *ctx) |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 1116 | { |
| 1117 | struct perf_counter *counter; |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 1118 | u64 interrupts, irq_period; |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 1119 | u64 events, period; |
| 1120 | s64 delta; |
| 1121 | |
| 1122 | spin_lock(&ctx->lock); |
| 1123 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
| 1124 | if (counter->state != PERF_COUNTER_STATE_ACTIVE) |
| 1125 | continue; |
| 1126 | |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 1127 | interrupts = counter->hw.interrupts; |
| 1128 | counter->hw.interrupts = 0; |
| 1129 | |
| 1130 | if (interrupts == MAX_INTERRUPTS) { |
| 1131 | perf_log_throttle(counter, 1); |
| 1132 | counter->pmu->unthrottle(counter); |
| 1133 | interrupts = 2*sysctl_perf_counter_limit/HZ; |
| 1134 | } |
| 1135 | |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 1136 | if (!counter->hw_event.freq || !counter->hw_event.irq_freq) |
| 1137 | continue; |
| 1138 | |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 1139 | events = HZ * interrupts * counter->hw.irq_period; |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 1140 | period = div64_u64(events, counter->hw_event.irq_freq); |
| 1141 | |
| 1142 | delta = (s64)(1 + period - counter->hw.irq_period); |
| 1143 | delta >>= 1; |
| 1144 | |
| 1145 | irq_period = counter->hw.irq_period + delta; |
| 1146 | |
| 1147 | if (!irq_period) |
| 1148 | irq_period = 1; |
| 1149 | |
Peter Zijlstra | 26b119b | 2009-05-20 12:21:20 +0200 | [diff] [blame] | 1150 | perf_log_period(counter, irq_period); |
| 1151 | |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 1152 | counter->hw.irq_period = irq_period; |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 1153 | } |
| 1154 | spin_unlock(&ctx->lock); |
| 1155 | } |
| 1156 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1157 | /* |
| 1158 | * Round-robin a context's counters: |
| 1159 | */ |
| 1160 | static void rotate_ctx(struct perf_counter_context *ctx) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1161 | { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1162 | struct perf_counter *counter; |
| 1163 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1164 | if (!ctx->nr_counters) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1165 | return; |
| 1166 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1167 | spin_lock(&ctx->lock); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1168 | /* |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1169 | * Rotate the first entry last (works just fine for group counters too): |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1170 | */ |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1171 | perf_disable(); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1172 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
Peter Zijlstra | 7556423 | 2009-03-13 12:21:29 +0100 | [diff] [blame] | 1173 | list_move_tail(&counter->list_entry, &ctx->counter_list); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1174 | break; |
| 1175 | } |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 1176 | perf_enable(); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1177 | |
| 1178 | spin_unlock(&ctx->lock); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1179 | } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1180 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1181 | void perf_counter_task_tick(struct task_struct *curr, int cpu) |
| 1182 | { |
Peter Zijlstra | 7fc23a5 | 2009-05-08 18:52:21 +0200 | [diff] [blame] | 1183 | struct perf_cpu_context *cpuctx; |
| 1184 | struct perf_counter_context *ctx; |
| 1185 | |
| 1186 | if (!atomic_read(&nr_counters)) |
| 1187 | return; |
| 1188 | |
| 1189 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 1190 | ctx = curr->perf_counter_ctxp; |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1191 | |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 1192 | perf_adjust_freq(&cpuctx->ctx); |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 1193 | if (ctx) |
| 1194 | perf_adjust_freq(ctx); |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 1195 | |
Ingo Molnar | b82914c | 2009-05-04 18:54:32 +0200 | [diff] [blame] | 1196 | perf_counter_cpu_sched_out(cpuctx); |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 1197 | if (ctx) |
| 1198 | __perf_counter_task_sched_out(ctx); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1199 | |
Ingo Molnar | b82914c | 2009-05-04 18:54:32 +0200 | [diff] [blame] | 1200 | rotate_ctx(&cpuctx->ctx); |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 1201 | if (ctx) |
| 1202 | rotate_ctx(ctx); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1203 | |
Ingo Molnar | b82914c | 2009-05-04 18:54:32 +0200 | [diff] [blame] | 1204 | perf_counter_cpu_sched_in(cpuctx, cpu); |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 1205 | if (ctx) |
| 1206 | perf_counter_task_sched_in(curr, cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | /* |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1210 | * Cross CPU call to read the hardware counter |
| 1211 | */ |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 1212 | static void __read(void *info) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1213 | { |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1214 | struct perf_counter *counter = info; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1215 | struct perf_counter_context *ctx = counter->ctx; |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1216 | unsigned long flags; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1217 | |
Peter Zijlstra | 849691a | 2009-04-06 11:45:12 +0200 | [diff] [blame] | 1218 | local_irq_save(flags); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1219 | if (ctx->is_active) |
Peter Zijlstra | 4af4998 | 2009-04-06 11:45:10 +0200 | [diff] [blame] | 1220 | update_context_time(ctx); |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 1221 | counter->pmu->read(counter); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1222 | update_counter_times(counter); |
Peter Zijlstra | 849691a | 2009-04-06 11:45:12 +0200 | [diff] [blame] | 1223 | local_irq_restore(flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1224 | } |
| 1225 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1226 | static u64 perf_counter_read(struct perf_counter *counter) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1227 | { |
| 1228 | /* |
| 1229 | * If counter is enabled and currently active on a CPU, update the |
| 1230 | * value in the counter structure: |
| 1231 | */ |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 1232 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1233 | smp_call_function_single(counter->oncpu, |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 1234 | __read, counter, 1); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1235 | } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) { |
| 1236 | update_counter_times(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1237 | } |
| 1238 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1239 | return atomic64_read(&counter->count); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1240 | } |
| 1241 | |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 1242 | /* |
| 1243 | * Initialize the perf_counter context in a task_struct: |
| 1244 | */ |
| 1245 | static void |
| 1246 | __perf_counter_init_context(struct perf_counter_context *ctx, |
| 1247 | struct task_struct *task) |
| 1248 | { |
| 1249 | memset(ctx, 0, sizeof(*ctx)); |
| 1250 | spin_lock_init(&ctx->lock); |
| 1251 | mutex_init(&ctx->mutex); |
| 1252 | INIT_LIST_HEAD(&ctx->counter_list); |
| 1253 | INIT_LIST_HEAD(&ctx->event_list); |
| 1254 | atomic_set(&ctx->refcount, 1); |
| 1255 | ctx->task = task; |
| 1256 | } |
| 1257 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1258 | static struct perf_counter_context *find_get_context(pid_t pid, int cpu) |
| 1259 | { |
| 1260 | struct perf_cpu_context *cpuctx; |
| 1261 | struct perf_counter_context *ctx; |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 1262 | struct perf_counter_context *parent_ctx; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1263 | struct task_struct *task; |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 1264 | int err; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1265 | |
| 1266 | /* |
| 1267 | * If cpu is not a wildcard then this is a percpu counter: |
| 1268 | */ |
| 1269 | if (cpu != -1) { |
| 1270 | /* Must be root to operate on a CPU counter: */ |
Peter Zijlstra | 1ccd154 | 2009-04-09 10:53:45 +0200 | [diff] [blame] | 1271 | if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN)) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1272 | return ERR_PTR(-EACCES); |
| 1273 | |
| 1274 | if (cpu < 0 || cpu > num_possible_cpus()) |
| 1275 | return ERR_PTR(-EINVAL); |
| 1276 | |
| 1277 | /* |
| 1278 | * We could be clever and allow to attach a counter to an |
| 1279 | * offline CPU and activate it when the CPU comes up, but |
| 1280 | * that's for later. |
| 1281 | */ |
| 1282 | if (!cpu_isset(cpu, cpu_online_map)) |
| 1283 | return ERR_PTR(-ENODEV); |
| 1284 | |
| 1285 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 1286 | ctx = &cpuctx->ctx; |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 1287 | get_ctx(ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1288 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1289 | return ctx; |
| 1290 | } |
| 1291 | |
| 1292 | rcu_read_lock(); |
| 1293 | if (!pid) |
| 1294 | task = current; |
| 1295 | else |
| 1296 | task = find_task_by_vpid(pid); |
| 1297 | if (task) |
| 1298 | get_task_struct(task); |
| 1299 | rcu_read_unlock(); |
| 1300 | |
| 1301 | if (!task) |
| 1302 | return ERR_PTR(-ESRCH); |
| 1303 | |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 1304 | /* |
| 1305 | * Can't attach counters to a dying task. |
| 1306 | */ |
| 1307 | err = -ESRCH; |
| 1308 | if (task->flags & PF_EXITING) |
| 1309 | goto errout; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1310 | |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 1311 | /* Reuse ptrace permission checks for now. */ |
| 1312 | err = -EACCES; |
| 1313 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
| 1314 | goto errout; |
| 1315 | |
| 1316 | retry_lock: |
| 1317 | rcu_read_lock(); |
| 1318 | retry: |
| 1319 | ctx = rcu_dereference(task->perf_counter_ctxp); |
| 1320 | if (ctx) { |
| 1321 | /* |
| 1322 | * If this context is a clone of another, it might |
| 1323 | * get swapped for another underneath us by |
| 1324 | * perf_counter_task_sched_out, though the |
| 1325 | * rcu_read_lock() protects us from any context |
| 1326 | * getting freed. Lock the context and check if it |
| 1327 | * got swapped before we could get the lock, and retry |
| 1328 | * if so. If we locked the right context, then it |
| 1329 | * can't get swapped on us any more and we can |
| 1330 | * unclone it if necessary. |
| 1331 | * Once it's not a clone things will be stable. |
| 1332 | */ |
| 1333 | spin_lock_irq(&ctx->lock); |
| 1334 | if (ctx != rcu_dereference(task->perf_counter_ctxp)) { |
| 1335 | spin_unlock_irq(&ctx->lock); |
| 1336 | goto retry; |
| 1337 | } |
| 1338 | parent_ctx = ctx->parent_ctx; |
| 1339 | if (parent_ctx) { |
| 1340 | put_ctx(parent_ctx); |
| 1341 | ctx->parent_ctx = NULL; /* no longer a clone */ |
| 1342 | } |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 1343 | /* |
| 1344 | * Get an extra reference before dropping the lock so that |
| 1345 | * this context won't get freed if the task exits. |
| 1346 | */ |
| 1347 | get_ctx(ctx); |
| 1348 | spin_unlock_irq(&ctx->lock); |
| 1349 | } |
| 1350 | rcu_read_unlock(); |
| 1351 | |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 1352 | if (!ctx) { |
| 1353 | ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL); |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 1354 | err = -ENOMEM; |
| 1355 | if (!ctx) |
| 1356 | goto errout; |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 1357 | __perf_counter_init_context(ctx, task); |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 1358 | get_ctx(ctx); |
| 1359 | if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) { |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 1360 | /* |
| 1361 | * We raced with some other task; use |
| 1362 | * the context they set. |
| 1363 | */ |
| 1364 | kfree(ctx); |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 1365 | goto retry_lock; |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 1366 | } |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 1367 | get_task_struct(task); |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 1368 | } |
| 1369 | |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 1370 | put_task_struct(task); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1371 | return ctx; |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 1372 | |
| 1373 | errout: |
| 1374 | put_task_struct(task); |
| 1375 | return ERR_PTR(err); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1376 | } |
| 1377 | |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 1378 | static void free_counter_rcu(struct rcu_head *head) |
| 1379 | { |
| 1380 | struct perf_counter *counter; |
| 1381 | |
| 1382 | counter = container_of(head, struct perf_counter, rcu_head); |
| 1383 | kfree(counter); |
| 1384 | } |
| 1385 | |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1386 | static void perf_pending_sync(struct perf_counter *counter); |
| 1387 | |
Peter Zijlstra | f160095 | 2009-03-19 20:26:16 +0100 | [diff] [blame] | 1388 | static void free_counter(struct perf_counter *counter) |
| 1389 | { |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1390 | perf_pending_sync(counter); |
| 1391 | |
Peter Zijlstra | 7fc23a5 | 2009-05-08 18:52:21 +0200 | [diff] [blame] | 1392 | atomic_dec(&nr_counters); |
Peter Zijlstra | 9ee318a | 2009-04-09 10:53:44 +0200 | [diff] [blame] | 1393 | if (counter->hw_event.mmap) |
| 1394 | atomic_dec(&nr_mmap_tracking); |
| 1395 | if (counter->hw_event.munmap) |
| 1396 | atomic_dec(&nr_munmap_tracking); |
| 1397 | if (counter->hw_event.comm) |
| 1398 | atomic_dec(&nr_comm_tracking); |
| 1399 | |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 1400 | if (counter->destroy) |
| 1401 | counter->destroy(counter); |
| 1402 | |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 1403 | put_ctx(counter->ctx); |
Peter Zijlstra | f160095 | 2009-03-19 20:26:16 +0100 | [diff] [blame] | 1404 | call_rcu(&counter->rcu_head, free_counter_rcu); |
| 1405 | } |
| 1406 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1407 | /* |
| 1408 | * Called when the last reference to the file is gone. |
| 1409 | */ |
| 1410 | static int perf_release(struct inode *inode, struct file *file) |
| 1411 | { |
| 1412 | struct perf_counter *counter = file->private_data; |
| 1413 | struct perf_counter_context *ctx = counter->ctx; |
| 1414 | |
| 1415 | file->private_data = NULL; |
| 1416 | |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 1417 | WARN_ON_ONCE(ctx->parent_ctx); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1418 | mutex_lock(&ctx->mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1419 | perf_counter_remove_from_context(counter); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1420 | mutex_unlock(&ctx->mutex); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1421 | |
Peter Zijlstra | 082ff5a | 2009-05-23 18:29:00 +0200 | [diff] [blame] | 1422 | mutex_lock(&counter->owner->perf_counter_mutex); |
| 1423 | list_del_init(&counter->owner_entry); |
| 1424 | mutex_unlock(&counter->owner->perf_counter_mutex); |
| 1425 | put_task_struct(counter->owner); |
| 1426 | |
Peter Zijlstra | f160095 | 2009-03-19 20:26:16 +0100 | [diff] [blame] | 1427 | free_counter(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1428 | |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | /* |
| 1433 | * Read the performance counter - simple non blocking version for now |
| 1434 | */ |
| 1435 | static ssize_t |
| 1436 | perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count) |
| 1437 | { |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1438 | u64 values[3]; |
| 1439 | int n; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1440 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 1441 | /* |
| 1442 | * Return end-of-file for a read on a counter that is in |
| 1443 | * error state (i.e. because it was pinned but it couldn't be |
| 1444 | * scheduled on to the CPU at some point). |
| 1445 | */ |
| 1446 | if (counter->state == PERF_COUNTER_STATE_ERROR) |
| 1447 | return 0; |
| 1448 | |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 1449 | WARN_ON_ONCE(counter->ctx->parent_ctx); |
Peter Zijlstra | fccc714 | 2009-05-23 18:28:56 +0200 | [diff] [blame] | 1450 | mutex_lock(&counter->child_mutex); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1451 | values[0] = perf_counter_read(counter); |
| 1452 | n = 1; |
| 1453 | if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 1454 | values[n++] = counter->total_time_enabled + |
| 1455 | atomic64_read(&counter->child_total_time_enabled); |
| 1456 | if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 1457 | values[n++] = counter->total_time_running + |
| 1458 | atomic64_read(&counter->child_total_time_running); |
Peter Zijlstra | fccc714 | 2009-05-23 18:28:56 +0200 | [diff] [blame] | 1459 | mutex_unlock(&counter->child_mutex); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1460 | |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1461 | if (count < n * sizeof(u64)) |
| 1462 | return -EINVAL; |
| 1463 | count = n * sizeof(u64); |
| 1464 | |
| 1465 | if (copy_to_user(buf, values, count)) |
| 1466 | return -EFAULT; |
| 1467 | |
| 1468 | return count; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | static ssize_t |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1472 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 1473 | { |
| 1474 | struct perf_counter *counter = file->private_data; |
| 1475 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1476 | return perf_read_hw(counter, buf, count); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | static unsigned int perf_poll(struct file *file, poll_table *wait) |
| 1480 | { |
| 1481 | struct perf_counter *counter = file->private_data; |
Peter Zijlstra | c7138f3 | 2009-03-24 13:18:16 +0100 | [diff] [blame] | 1482 | struct perf_mmap_data *data; |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 1483 | unsigned int events = POLL_HUP; |
Peter Zijlstra | c7138f3 | 2009-03-24 13:18:16 +0100 | [diff] [blame] | 1484 | |
| 1485 | rcu_read_lock(); |
| 1486 | data = rcu_dereference(counter->data); |
| 1487 | if (data) |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 1488 | events = atomic_xchg(&data->poll, 0); |
Peter Zijlstra | c7138f3 | 2009-03-24 13:18:16 +0100 | [diff] [blame] | 1489 | rcu_read_unlock(); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1490 | |
| 1491 | poll_wait(file, &counter->waitq, wait); |
| 1492 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1493 | return events; |
| 1494 | } |
| 1495 | |
Peter Zijlstra | 6de6a7b | 2009-05-05 17:50:23 +0200 | [diff] [blame] | 1496 | static void perf_counter_reset(struct perf_counter *counter) |
| 1497 | { |
Peter Zijlstra | 3df5eda | 2009-05-08 18:52:22 +0200 | [diff] [blame] | 1498 | (void)perf_counter_read(counter); |
Paul Mackerras | 615a3f1 | 2009-05-11 15:50:21 +1000 | [diff] [blame] | 1499 | atomic64_set(&counter->count, 0); |
Peter Zijlstra | 3df5eda | 2009-05-08 18:52:22 +0200 | [diff] [blame] | 1500 | perf_counter_update_userpage(counter); |
| 1501 | } |
| 1502 | |
| 1503 | static void perf_counter_for_each_sibling(struct perf_counter *counter, |
| 1504 | void (*func)(struct perf_counter *)) |
| 1505 | { |
| 1506 | struct perf_counter_context *ctx = counter->ctx; |
| 1507 | struct perf_counter *sibling; |
| 1508 | |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 1509 | WARN_ON_ONCE(ctx->parent_ctx); |
Peter Zijlstra | 682076a | 2009-05-23 18:28:57 +0200 | [diff] [blame] | 1510 | mutex_lock(&ctx->mutex); |
Peter Zijlstra | 3df5eda | 2009-05-08 18:52:22 +0200 | [diff] [blame] | 1511 | counter = counter->group_leader; |
| 1512 | |
| 1513 | func(counter); |
| 1514 | list_for_each_entry(sibling, &counter->sibling_list, list_entry) |
| 1515 | func(sibling); |
Peter Zijlstra | 682076a | 2009-05-23 18:28:57 +0200 | [diff] [blame] | 1516 | mutex_unlock(&ctx->mutex); |
Peter Zijlstra | 3df5eda | 2009-05-08 18:52:22 +0200 | [diff] [blame] | 1517 | } |
| 1518 | |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 1519 | /* |
| 1520 | * Holding the top-level counter's child_mutex means that any |
| 1521 | * descendant process that has inherited this counter will block |
| 1522 | * in sync_child_counter if it goes to exit, thus satisfying the |
| 1523 | * task existence requirements of perf_counter_enable/disable. |
| 1524 | */ |
Peter Zijlstra | 3df5eda | 2009-05-08 18:52:22 +0200 | [diff] [blame] | 1525 | static void perf_counter_for_each_child(struct perf_counter *counter, |
| 1526 | void (*func)(struct perf_counter *)) |
| 1527 | { |
| 1528 | struct perf_counter *child; |
| 1529 | |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 1530 | WARN_ON_ONCE(counter->ctx->parent_ctx); |
Peter Zijlstra | fccc714 | 2009-05-23 18:28:56 +0200 | [diff] [blame] | 1531 | mutex_lock(&counter->child_mutex); |
Peter Zijlstra | 3df5eda | 2009-05-08 18:52:22 +0200 | [diff] [blame] | 1532 | func(counter); |
| 1533 | list_for_each_entry(child, &counter->child_list, child_list) |
| 1534 | func(child); |
Peter Zijlstra | fccc714 | 2009-05-23 18:28:56 +0200 | [diff] [blame] | 1535 | mutex_unlock(&counter->child_mutex); |
Peter Zijlstra | 3df5eda | 2009-05-08 18:52:22 +0200 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | static void perf_counter_for_each(struct perf_counter *counter, |
| 1539 | void (*func)(struct perf_counter *)) |
| 1540 | { |
| 1541 | struct perf_counter *child; |
| 1542 | |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 1543 | WARN_ON_ONCE(counter->ctx->parent_ctx); |
Peter Zijlstra | fccc714 | 2009-05-23 18:28:56 +0200 | [diff] [blame] | 1544 | mutex_lock(&counter->child_mutex); |
Peter Zijlstra | 3df5eda | 2009-05-08 18:52:22 +0200 | [diff] [blame] | 1545 | perf_counter_for_each_sibling(counter, func); |
| 1546 | list_for_each_entry(child, &counter->child_list, child_list) |
| 1547 | perf_counter_for_each_sibling(child, func); |
Peter Zijlstra | fccc714 | 2009-05-23 18:28:56 +0200 | [diff] [blame] | 1548 | mutex_unlock(&counter->child_mutex); |
Peter Zijlstra | 6de6a7b | 2009-05-05 17:50:23 +0200 | [diff] [blame] | 1549 | } |
| 1550 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1551 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 1552 | { |
| 1553 | struct perf_counter *counter = file->private_data; |
Peter Zijlstra | 3df5eda | 2009-05-08 18:52:22 +0200 | [diff] [blame] | 1554 | void (*func)(struct perf_counter *); |
| 1555 | u32 flags = arg; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1556 | |
| 1557 | switch (cmd) { |
| 1558 | case PERF_COUNTER_IOC_ENABLE: |
Peter Zijlstra | 3df5eda | 2009-05-08 18:52:22 +0200 | [diff] [blame] | 1559 | func = perf_counter_enable; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1560 | break; |
| 1561 | case PERF_COUNTER_IOC_DISABLE: |
Peter Zijlstra | 3df5eda | 2009-05-08 18:52:22 +0200 | [diff] [blame] | 1562 | func = perf_counter_disable; |
Peter Zijlstra | 79f1464 | 2009-04-06 11:45:07 +0200 | [diff] [blame] | 1563 | break; |
Peter Zijlstra | 6de6a7b | 2009-05-05 17:50:23 +0200 | [diff] [blame] | 1564 | case PERF_COUNTER_IOC_RESET: |
Peter Zijlstra | 3df5eda | 2009-05-08 18:52:22 +0200 | [diff] [blame] | 1565 | func = perf_counter_reset; |
Peter Zijlstra | 6de6a7b | 2009-05-05 17:50:23 +0200 | [diff] [blame] | 1566 | break; |
Peter Zijlstra | 3df5eda | 2009-05-08 18:52:22 +0200 | [diff] [blame] | 1567 | |
| 1568 | case PERF_COUNTER_IOC_REFRESH: |
| 1569 | return perf_counter_refresh(counter, arg); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1570 | default: |
Peter Zijlstra | 3df5eda | 2009-05-08 18:52:22 +0200 | [diff] [blame] | 1571 | return -ENOTTY; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1572 | } |
Peter Zijlstra | 3df5eda | 2009-05-08 18:52:22 +0200 | [diff] [blame] | 1573 | |
| 1574 | if (flags & PERF_IOC_FLAG_GROUP) |
| 1575 | perf_counter_for_each(counter, func); |
| 1576 | else |
| 1577 | perf_counter_for_each_child(counter, func); |
| 1578 | |
| 1579 | return 0; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1580 | } |
| 1581 | |
Peter Zijlstra | 771d7cd | 2009-05-25 14:45:26 +0200 | [diff] [blame] | 1582 | int perf_counter_task_enable(void) |
| 1583 | { |
| 1584 | struct perf_counter *counter; |
| 1585 | |
| 1586 | mutex_lock(¤t->perf_counter_mutex); |
| 1587 | list_for_each_entry(counter, ¤t->perf_counter_list, owner_entry) |
| 1588 | perf_counter_for_each_child(counter, perf_counter_enable); |
| 1589 | mutex_unlock(¤t->perf_counter_mutex); |
| 1590 | |
| 1591 | return 0; |
| 1592 | } |
| 1593 | |
| 1594 | int perf_counter_task_disable(void) |
| 1595 | { |
| 1596 | struct perf_counter *counter; |
| 1597 | |
| 1598 | mutex_lock(¤t->perf_counter_mutex); |
| 1599 | list_for_each_entry(counter, ¤t->perf_counter_list, owner_entry) |
| 1600 | perf_counter_for_each_child(counter, perf_counter_disable); |
| 1601 | mutex_unlock(¤t->perf_counter_mutex); |
| 1602 | |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
Peter Zijlstra | 38ff667 | 2009-03-30 19:07:03 +0200 | [diff] [blame] | 1606 | /* |
| 1607 | * Callers need to ensure there can be no nesting of this function, otherwise |
| 1608 | * the seqlock logic goes bad. We can not serialize this because the arch |
| 1609 | * code calls this from NMI context. |
| 1610 | */ |
| 1611 | void perf_counter_update_userpage(struct perf_counter *counter) |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1612 | { |
Peter Zijlstra | 38ff667 | 2009-03-30 19:07:03 +0200 | [diff] [blame] | 1613 | struct perf_mmap_data *data; |
| 1614 | struct perf_counter_mmap_page *userpg; |
| 1615 | |
| 1616 | rcu_read_lock(); |
| 1617 | data = rcu_dereference(counter->data); |
| 1618 | if (!data) |
| 1619 | goto unlock; |
| 1620 | |
| 1621 | userpg = data->user_page; |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1622 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1623 | /* |
| 1624 | * Disable preemption so as to not let the corresponding user-space |
| 1625 | * spin too long if we get preempted. |
| 1626 | */ |
| 1627 | preempt_disable(); |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1628 | ++userpg->lock; |
Peter Zijlstra | 92f22a3 | 2009-04-02 11:12:04 +0200 | [diff] [blame] | 1629 | barrier(); |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1630 | userpg->index = counter->hw.idx; |
| 1631 | userpg->offset = atomic64_read(&counter->count); |
| 1632 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) |
| 1633 | userpg->offset -= atomic64_read(&counter->hw.prev_count); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1634 | |
Peter Zijlstra | 92f22a3 | 2009-04-02 11:12:04 +0200 | [diff] [blame] | 1635 | barrier(); |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1636 | ++userpg->lock; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1637 | preempt_enable(); |
Peter Zijlstra | 38ff667 | 2009-03-30 19:07:03 +0200 | [diff] [blame] | 1638 | unlock: |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1639 | rcu_read_unlock(); |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 1643 | { |
| 1644 | struct perf_counter *counter = vma->vm_file->private_data; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1645 | struct perf_mmap_data *data; |
| 1646 | int ret = VM_FAULT_SIGBUS; |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1647 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1648 | rcu_read_lock(); |
| 1649 | data = rcu_dereference(counter->data); |
| 1650 | if (!data) |
| 1651 | goto unlock; |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1652 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1653 | if (vmf->pgoff == 0) { |
| 1654 | vmf->page = virt_to_page(data->user_page); |
| 1655 | } else { |
| 1656 | int nr = vmf->pgoff - 1; |
| 1657 | |
| 1658 | if ((unsigned)nr > data->nr_pages) |
| 1659 | goto unlock; |
| 1660 | |
| 1661 | vmf->page = virt_to_page(data->data_pages[nr]); |
| 1662 | } |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1663 | get_page(vmf->page); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1664 | ret = 0; |
| 1665 | unlock: |
| 1666 | rcu_read_unlock(); |
| 1667 | |
| 1668 | return ret; |
| 1669 | } |
| 1670 | |
| 1671 | static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages) |
| 1672 | { |
| 1673 | struct perf_mmap_data *data; |
| 1674 | unsigned long size; |
| 1675 | int i; |
| 1676 | |
| 1677 | WARN_ON(atomic_read(&counter->mmap_count)); |
| 1678 | |
| 1679 | size = sizeof(struct perf_mmap_data); |
| 1680 | size += nr_pages * sizeof(void *); |
| 1681 | |
| 1682 | data = kzalloc(size, GFP_KERNEL); |
| 1683 | if (!data) |
| 1684 | goto fail; |
| 1685 | |
| 1686 | data->user_page = (void *)get_zeroed_page(GFP_KERNEL); |
| 1687 | if (!data->user_page) |
| 1688 | goto fail_user_page; |
| 1689 | |
| 1690 | for (i = 0; i < nr_pages; i++) { |
| 1691 | data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL); |
| 1692 | if (!data->data_pages[i]) |
| 1693 | goto fail_data_pages; |
| 1694 | } |
| 1695 | |
| 1696 | data->nr_pages = nr_pages; |
Peter Zijlstra | 22c1558 | 2009-05-05 17:50:25 +0200 | [diff] [blame] | 1697 | atomic_set(&data->lock, -1); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1698 | |
| 1699 | rcu_assign_pointer(counter->data, data); |
| 1700 | |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1701 | return 0; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1702 | |
| 1703 | fail_data_pages: |
| 1704 | for (i--; i >= 0; i--) |
| 1705 | free_page((unsigned long)data->data_pages[i]); |
| 1706 | |
| 1707 | free_page((unsigned long)data->user_page); |
| 1708 | |
| 1709 | fail_user_page: |
| 1710 | kfree(data); |
| 1711 | |
| 1712 | fail: |
| 1713 | return -ENOMEM; |
| 1714 | } |
| 1715 | |
| 1716 | static void __perf_mmap_data_free(struct rcu_head *rcu_head) |
| 1717 | { |
| 1718 | struct perf_mmap_data *data = container_of(rcu_head, |
| 1719 | struct perf_mmap_data, rcu_head); |
| 1720 | int i; |
| 1721 | |
| 1722 | free_page((unsigned long)data->user_page); |
| 1723 | for (i = 0; i < data->nr_pages; i++) |
| 1724 | free_page((unsigned long)data->data_pages[i]); |
| 1725 | kfree(data); |
| 1726 | } |
| 1727 | |
| 1728 | static void perf_mmap_data_free(struct perf_counter *counter) |
| 1729 | { |
| 1730 | struct perf_mmap_data *data = counter->data; |
| 1731 | |
| 1732 | WARN_ON(atomic_read(&counter->mmap_count)); |
| 1733 | |
| 1734 | rcu_assign_pointer(counter->data, NULL); |
| 1735 | call_rcu(&data->rcu_head, __perf_mmap_data_free); |
| 1736 | } |
| 1737 | |
| 1738 | static void perf_mmap_open(struct vm_area_struct *vma) |
| 1739 | { |
| 1740 | struct perf_counter *counter = vma->vm_file->private_data; |
| 1741 | |
| 1742 | atomic_inc(&counter->mmap_count); |
| 1743 | } |
| 1744 | |
| 1745 | static void perf_mmap_close(struct vm_area_struct *vma) |
| 1746 | { |
| 1747 | struct perf_counter *counter = vma->vm_file->private_data; |
| 1748 | |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 1749 | WARN_ON_ONCE(counter->ctx->parent_ctx); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1750 | if (atomic_dec_and_mutex_lock(&counter->mmap_count, |
| 1751 | &counter->mmap_mutex)) { |
Peter Zijlstra | 789f90f | 2009-05-15 15:19:27 +0200 | [diff] [blame] | 1752 | struct user_struct *user = current_user(); |
| 1753 | |
| 1754 | atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm); |
Peter Zijlstra | c5078f7 | 2009-05-05 17:50:24 +0200 | [diff] [blame] | 1755 | vma->vm_mm->locked_vm -= counter->data->nr_locked; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1756 | perf_mmap_data_free(counter); |
| 1757 | mutex_unlock(&counter->mmap_mutex); |
| 1758 | } |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | static struct vm_operations_struct perf_mmap_vmops = { |
Peter Zijlstra | ebb3c4c | 2009-04-06 11:45:05 +0200 | [diff] [blame] | 1762 | .open = perf_mmap_open, |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1763 | .close = perf_mmap_close, |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1764 | .fault = perf_mmap_fault, |
| 1765 | }; |
| 1766 | |
| 1767 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) |
| 1768 | { |
| 1769 | struct perf_counter *counter = file->private_data; |
Peter Zijlstra | 789f90f | 2009-05-15 15:19:27 +0200 | [diff] [blame] | 1770 | struct user_struct *user = current_user(); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1771 | unsigned long vma_size; |
| 1772 | unsigned long nr_pages; |
Peter Zijlstra | 789f90f | 2009-05-15 15:19:27 +0200 | [diff] [blame] | 1773 | unsigned long user_locked, user_lock_limit; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1774 | unsigned long locked, lock_limit; |
Peter Zijlstra | 789f90f | 2009-05-15 15:19:27 +0200 | [diff] [blame] | 1775 | long user_extra, extra; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1776 | int ret = 0; |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1777 | |
| 1778 | if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE)) |
| 1779 | return -EINVAL; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1780 | |
| 1781 | vma_size = vma->vm_end - vma->vm_start; |
| 1782 | nr_pages = (vma_size / PAGE_SIZE) - 1; |
| 1783 | |
Peter Zijlstra | 7730d86 | 2009-03-25 12:48:31 +0100 | [diff] [blame] | 1784 | /* |
| 1785 | * If we have data pages ensure they're a power-of-two number, so we |
| 1786 | * can do bitmasks instead of modulo. |
| 1787 | */ |
| 1788 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1789 | return -EINVAL; |
| 1790 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1791 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1792 | return -EINVAL; |
| 1793 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1794 | if (vma->vm_pgoff != 0) |
| 1795 | return -EINVAL; |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1796 | |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 1797 | WARN_ON_ONCE(counter->ctx->parent_ctx); |
Peter Zijlstra | ebb3c4c | 2009-04-06 11:45:05 +0200 | [diff] [blame] | 1798 | mutex_lock(&counter->mmap_mutex); |
| 1799 | if (atomic_inc_not_zero(&counter->mmap_count)) { |
| 1800 | if (nr_pages != counter->data->nr_pages) |
| 1801 | ret = -EINVAL; |
| 1802 | goto unlock; |
| 1803 | } |
| 1804 | |
Peter Zijlstra | 789f90f | 2009-05-15 15:19:27 +0200 | [diff] [blame] | 1805 | user_extra = nr_pages + 1; |
| 1806 | user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10); |
Ingo Molnar | a3862d3 | 2009-05-24 09:02:37 +0200 | [diff] [blame] | 1807 | |
| 1808 | /* |
| 1809 | * Increase the limit linearly with more CPUs: |
| 1810 | */ |
| 1811 | user_lock_limit *= num_online_cpus(); |
| 1812 | |
Peter Zijlstra | 789f90f | 2009-05-15 15:19:27 +0200 | [diff] [blame] | 1813 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
Peter Zijlstra | c5078f7 | 2009-05-05 17:50:24 +0200 | [diff] [blame] | 1814 | |
Peter Zijlstra | 789f90f | 2009-05-15 15:19:27 +0200 | [diff] [blame] | 1815 | extra = 0; |
| 1816 | if (user_locked > user_lock_limit) |
| 1817 | extra = user_locked - user_lock_limit; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1818 | |
| 1819 | lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur; |
| 1820 | lock_limit >>= PAGE_SHIFT; |
Peter Zijlstra | 789f90f | 2009-05-15 15:19:27 +0200 | [diff] [blame] | 1821 | locked = vma->vm_mm->locked_vm + extra; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1822 | |
Peter Zijlstra | ebb3c4c | 2009-04-06 11:45:05 +0200 | [diff] [blame] | 1823 | if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) { |
| 1824 | ret = -EPERM; |
| 1825 | goto unlock; |
| 1826 | } |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1827 | |
| 1828 | WARN_ON(counter->data); |
| 1829 | ret = perf_mmap_data_alloc(counter, nr_pages); |
Peter Zijlstra | ebb3c4c | 2009-04-06 11:45:05 +0200 | [diff] [blame] | 1830 | if (ret) |
| 1831 | goto unlock; |
| 1832 | |
| 1833 | atomic_set(&counter->mmap_count, 1); |
Peter Zijlstra | 789f90f | 2009-05-15 15:19:27 +0200 | [diff] [blame] | 1834 | atomic_long_add(user_extra, &user->locked_vm); |
Peter Zijlstra | c5078f7 | 2009-05-05 17:50:24 +0200 | [diff] [blame] | 1835 | vma->vm_mm->locked_vm += extra; |
| 1836 | counter->data->nr_locked = extra; |
Peter Zijlstra | ebb3c4c | 2009-04-06 11:45:05 +0200 | [diff] [blame] | 1837 | unlock: |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1838 | mutex_unlock(&counter->mmap_mutex); |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1839 | |
| 1840 | vma->vm_flags &= ~VM_MAYWRITE; |
| 1841 | vma->vm_flags |= VM_RESERVED; |
| 1842 | vma->vm_ops = &perf_mmap_vmops; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1843 | |
| 1844 | return ret; |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1845 | } |
| 1846 | |
Peter Zijlstra | 3c446b3 | 2009-04-06 11:45:01 +0200 | [diff] [blame] | 1847 | static int perf_fasync(int fd, struct file *filp, int on) |
| 1848 | { |
| 1849 | struct perf_counter *counter = filp->private_data; |
| 1850 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 1851 | int retval; |
| 1852 | |
| 1853 | mutex_lock(&inode->i_mutex); |
| 1854 | retval = fasync_helper(fd, filp, on, &counter->fasync); |
| 1855 | mutex_unlock(&inode->i_mutex); |
| 1856 | |
| 1857 | if (retval < 0) |
| 1858 | return retval; |
| 1859 | |
| 1860 | return 0; |
| 1861 | } |
| 1862 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1863 | static const struct file_operations perf_fops = { |
| 1864 | .release = perf_release, |
| 1865 | .read = perf_read, |
| 1866 | .poll = perf_poll, |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1867 | .unlocked_ioctl = perf_ioctl, |
| 1868 | .compat_ioctl = perf_ioctl, |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1869 | .mmap = perf_mmap, |
Peter Zijlstra | 3c446b3 | 2009-04-06 11:45:01 +0200 | [diff] [blame] | 1870 | .fasync = perf_fasync, |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1871 | }; |
| 1872 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1873 | /* |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1874 | * Perf counter wakeup |
| 1875 | * |
| 1876 | * If there's data, ensure we set the poll() state and publish everything |
| 1877 | * to user-space before waking everybody up. |
| 1878 | */ |
| 1879 | |
| 1880 | void perf_counter_wakeup(struct perf_counter *counter) |
| 1881 | { |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1882 | wake_up_all(&counter->waitq); |
Peter Zijlstra | 4c9e254 | 2009-04-06 11:45:09 +0200 | [diff] [blame] | 1883 | |
| 1884 | if (counter->pending_kill) { |
| 1885 | kill_fasync(&counter->fasync, SIGIO, counter->pending_kill); |
| 1886 | counter->pending_kill = 0; |
| 1887 | } |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | /* |
| 1891 | * Pending wakeups |
| 1892 | * |
| 1893 | * Handle the case where we need to wakeup up from NMI (or rq->lock) context. |
| 1894 | * |
| 1895 | * The NMI bit means we cannot possibly take locks. Therefore, maintain a |
| 1896 | * single linked list and use cmpxchg() to add entries lockless. |
| 1897 | */ |
| 1898 | |
Peter Zijlstra | 79f1464 | 2009-04-06 11:45:07 +0200 | [diff] [blame] | 1899 | static void perf_pending_counter(struct perf_pending_entry *entry) |
| 1900 | { |
| 1901 | struct perf_counter *counter = container_of(entry, |
| 1902 | struct perf_counter, pending); |
| 1903 | |
| 1904 | if (counter->pending_disable) { |
| 1905 | counter->pending_disable = 0; |
| 1906 | perf_counter_disable(counter); |
| 1907 | } |
| 1908 | |
| 1909 | if (counter->pending_wakeup) { |
| 1910 | counter->pending_wakeup = 0; |
| 1911 | perf_counter_wakeup(counter); |
| 1912 | } |
| 1913 | } |
| 1914 | |
Peter Zijlstra | 671dec5 | 2009-04-06 11:45:02 +0200 | [diff] [blame] | 1915 | #define PENDING_TAIL ((struct perf_pending_entry *)-1UL) |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1916 | |
Peter Zijlstra | 671dec5 | 2009-04-06 11:45:02 +0200 | [diff] [blame] | 1917 | static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = { |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1918 | PENDING_TAIL, |
| 1919 | }; |
| 1920 | |
Peter Zijlstra | 671dec5 | 2009-04-06 11:45:02 +0200 | [diff] [blame] | 1921 | static void perf_pending_queue(struct perf_pending_entry *entry, |
| 1922 | void (*func)(struct perf_pending_entry *)) |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1923 | { |
Peter Zijlstra | 671dec5 | 2009-04-06 11:45:02 +0200 | [diff] [blame] | 1924 | struct perf_pending_entry **head; |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1925 | |
Peter Zijlstra | 671dec5 | 2009-04-06 11:45:02 +0200 | [diff] [blame] | 1926 | if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL) |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1927 | return; |
| 1928 | |
Peter Zijlstra | 671dec5 | 2009-04-06 11:45:02 +0200 | [diff] [blame] | 1929 | entry->func = func; |
| 1930 | |
| 1931 | head = &get_cpu_var(perf_pending_head); |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1932 | |
| 1933 | do { |
Peter Zijlstra | 671dec5 | 2009-04-06 11:45:02 +0200 | [diff] [blame] | 1934 | entry->next = *head; |
| 1935 | } while (cmpxchg(head, entry->next, entry) != entry->next); |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1936 | |
| 1937 | set_perf_counter_pending(); |
| 1938 | |
Peter Zijlstra | 671dec5 | 2009-04-06 11:45:02 +0200 | [diff] [blame] | 1939 | put_cpu_var(perf_pending_head); |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | static int __perf_pending_run(void) |
| 1943 | { |
Peter Zijlstra | 671dec5 | 2009-04-06 11:45:02 +0200 | [diff] [blame] | 1944 | struct perf_pending_entry *list; |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1945 | int nr = 0; |
| 1946 | |
Peter Zijlstra | 671dec5 | 2009-04-06 11:45:02 +0200 | [diff] [blame] | 1947 | list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL); |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1948 | while (list != PENDING_TAIL) { |
Peter Zijlstra | 671dec5 | 2009-04-06 11:45:02 +0200 | [diff] [blame] | 1949 | void (*func)(struct perf_pending_entry *); |
| 1950 | struct perf_pending_entry *entry = list; |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1951 | |
| 1952 | list = list->next; |
| 1953 | |
Peter Zijlstra | 671dec5 | 2009-04-06 11:45:02 +0200 | [diff] [blame] | 1954 | func = entry->func; |
| 1955 | entry->next = NULL; |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1956 | /* |
| 1957 | * Ensure we observe the unqueue before we issue the wakeup, |
| 1958 | * so that we won't be waiting forever. |
| 1959 | * -- see perf_not_pending(). |
| 1960 | */ |
| 1961 | smp_wmb(); |
| 1962 | |
Peter Zijlstra | 671dec5 | 2009-04-06 11:45:02 +0200 | [diff] [blame] | 1963 | func(entry); |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1964 | nr++; |
| 1965 | } |
| 1966 | |
| 1967 | return nr; |
| 1968 | } |
| 1969 | |
| 1970 | static inline int perf_not_pending(struct perf_counter *counter) |
| 1971 | { |
| 1972 | /* |
| 1973 | * If we flush on whatever cpu we run, there is a chance we don't |
| 1974 | * need to wait. |
| 1975 | */ |
| 1976 | get_cpu(); |
| 1977 | __perf_pending_run(); |
| 1978 | put_cpu(); |
| 1979 | |
| 1980 | /* |
| 1981 | * Ensure we see the proper queue state before going to sleep |
| 1982 | * so that we do not miss the wakeup. -- see perf_pending_handle() |
| 1983 | */ |
| 1984 | smp_rmb(); |
Peter Zijlstra | 671dec5 | 2009-04-06 11:45:02 +0200 | [diff] [blame] | 1985 | return counter->pending.next == NULL; |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | static void perf_pending_sync(struct perf_counter *counter) |
| 1989 | { |
| 1990 | wait_event(counter->waitq, perf_not_pending(counter)); |
| 1991 | } |
| 1992 | |
| 1993 | void perf_counter_do_pending(void) |
| 1994 | { |
| 1995 | __perf_pending_run(); |
| 1996 | } |
| 1997 | |
| 1998 | /* |
Peter Zijlstra | 394ee07 | 2009-03-30 19:07:14 +0200 | [diff] [blame] | 1999 | * Callchain support -- arch specific |
| 2000 | */ |
| 2001 | |
Peter Zijlstra | 9c03d88 | 2009-04-06 11:45:00 +0200 | [diff] [blame] | 2002 | __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs) |
Peter Zijlstra | 394ee07 | 2009-03-30 19:07:14 +0200 | [diff] [blame] | 2003 | { |
| 2004 | return NULL; |
| 2005 | } |
| 2006 | |
| 2007 | /* |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 2008 | * Output |
| 2009 | */ |
| 2010 | |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 2011 | struct perf_output_handle { |
| 2012 | struct perf_counter *counter; |
| 2013 | struct perf_mmap_data *data; |
| 2014 | unsigned int offset; |
Peter Zijlstra | 63e35b2 | 2009-03-25 12:30:24 +0100 | [diff] [blame] | 2015 | unsigned int head; |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 2016 | int nmi; |
Peter Zijlstra | 4c9e254 | 2009-04-06 11:45:09 +0200 | [diff] [blame] | 2017 | int overflow; |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2018 | int locked; |
| 2019 | unsigned long flags; |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 2020 | }; |
| 2021 | |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2022 | static void perf_output_wakeup(struct perf_output_handle *handle) |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 2023 | { |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2024 | atomic_set(&handle->data->poll, POLL_IN); |
| 2025 | |
Peter Zijlstra | 671dec5 | 2009-04-06 11:45:02 +0200 | [diff] [blame] | 2026 | if (handle->nmi) { |
Peter Zijlstra | 79f1464 | 2009-04-06 11:45:07 +0200 | [diff] [blame] | 2027 | handle->counter->pending_wakeup = 1; |
Peter Zijlstra | 671dec5 | 2009-04-06 11:45:02 +0200 | [diff] [blame] | 2028 | perf_pending_queue(&handle->counter->pending, |
Peter Zijlstra | 79f1464 | 2009-04-06 11:45:07 +0200 | [diff] [blame] | 2029 | perf_pending_counter); |
Peter Zijlstra | 671dec5 | 2009-04-06 11:45:02 +0200 | [diff] [blame] | 2030 | } else |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 2031 | perf_counter_wakeup(handle->counter); |
| 2032 | } |
| 2033 | |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2034 | /* |
| 2035 | * Curious locking construct. |
| 2036 | * |
| 2037 | * We need to ensure a later event doesn't publish a head when a former |
| 2038 | * event isn't done writing. However since we need to deal with NMIs we |
| 2039 | * cannot fully serialize things. |
| 2040 | * |
| 2041 | * What we do is serialize between CPUs so we only have to deal with NMI |
| 2042 | * nesting on a single CPU. |
| 2043 | * |
| 2044 | * We only publish the head (and generate a wakeup) when the outer-most |
| 2045 | * event completes. |
| 2046 | */ |
| 2047 | static void perf_output_lock(struct perf_output_handle *handle) |
| 2048 | { |
| 2049 | struct perf_mmap_data *data = handle->data; |
| 2050 | int cpu; |
| 2051 | |
| 2052 | handle->locked = 0; |
| 2053 | |
| 2054 | local_irq_save(handle->flags); |
| 2055 | cpu = smp_processor_id(); |
| 2056 | |
| 2057 | if (in_nmi() && atomic_read(&data->lock) == cpu) |
| 2058 | return; |
| 2059 | |
Peter Zijlstra | 22c1558 | 2009-05-05 17:50:25 +0200 | [diff] [blame] | 2060 | while (atomic_cmpxchg(&data->lock, -1, cpu) != -1) |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2061 | cpu_relax(); |
| 2062 | |
| 2063 | handle->locked = 1; |
| 2064 | } |
| 2065 | |
| 2066 | static void perf_output_unlock(struct perf_output_handle *handle) |
| 2067 | { |
| 2068 | struct perf_mmap_data *data = handle->data; |
| 2069 | int head, cpu; |
| 2070 | |
Peter Zijlstra | c66de4a | 2009-05-05 17:50:22 +0200 | [diff] [blame] | 2071 | data->done_head = data->head; |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2072 | |
| 2073 | if (!handle->locked) |
| 2074 | goto out; |
| 2075 | |
| 2076 | again: |
| 2077 | /* |
| 2078 | * The xchg implies a full barrier that ensures all writes are done |
| 2079 | * before we publish the new head, matched by a rmb() in userspace when |
| 2080 | * reading this position. |
| 2081 | */ |
Peter Zijlstra | c66de4a | 2009-05-05 17:50:22 +0200 | [diff] [blame] | 2082 | while ((head = atomic_xchg(&data->done_head, 0))) |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2083 | data->user_page->data_head = head; |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2084 | |
| 2085 | /* |
Peter Zijlstra | c66de4a | 2009-05-05 17:50:22 +0200 | [diff] [blame] | 2086 | * NMI can happen here, which means we can miss a done_head update. |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2087 | */ |
| 2088 | |
Peter Zijlstra | 22c1558 | 2009-05-05 17:50:25 +0200 | [diff] [blame] | 2089 | cpu = atomic_xchg(&data->lock, -1); |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2090 | WARN_ON_ONCE(cpu != smp_processor_id()); |
| 2091 | |
| 2092 | /* |
| 2093 | * Therefore we have to validate we did not indeed do so. |
| 2094 | */ |
Peter Zijlstra | c66de4a | 2009-05-05 17:50:22 +0200 | [diff] [blame] | 2095 | if (unlikely(atomic_read(&data->done_head))) { |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2096 | /* |
| 2097 | * Since we had it locked, we can lock it again. |
| 2098 | */ |
Peter Zijlstra | 22c1558 | 2009-05-05 17:50:25 +0200 | [diff] [blame] | 2099 | while (atomic_cmpxchg(&data->lock, -1, cpu) != -1) |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2100 | cpu_relax(); |
| 2101 | |
| 2102 | goto again; |
| 2103 | } |
| 2104 | |
Peter Zijlstra | c66de4a | 2009-05-05 17:50:22 +0200 | [diff] [blame] | 2105 | if (atomic_xchg(&data->wakeup, 0)) |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2106 | perf_output_wakeup(handle); |
| 2107 | out: |
| 2108 | local_irq_restore(handle->flags); |
| 2109 | } |
| 2110 | |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 2111 | static int perf_output_begin(struct perf_output_handle *handle, |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 2112 | struct perf_counter *counter, unsigned int size, |
Peter Zijlstra | 4c9e254 | 2009-04-06 11:45:09 +0200 | [diff] [blame] | 2113 | int nmi, int overflow) |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 2114 | { |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 2115 | struct perf_mmap_data *data; |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 2116 | unsigned int offset, head; |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 2117 | |
Peter Zijlstra | 2023b35 | 2009-05-05 17:50:26 +0200 | [diff] [blame] | 2118 | /* |
| 2119 | * For inherited counters we send all the output towards the parent. |
| 2120 | */ |
| 2121 | if (counter->parent) |
| 2122 | counter = counter->parent; |
| 2123 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 2124 | rcu_read_lock(); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 2125 | data = rcu_dereference(counter->data); |
| 2126 | if (!data) |
| 2127 | goto out; |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 2128 | |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2129 | handle->data = data; |
Peter Zijlstra | 4c9e254 | 2009-04-06 11:45:09 +0200 | [diff] [blame] | 2130 | handle->counter = counter; |
| 2131 | handle->nmi = nmi; |
| 2132 | handle->overflow = overflow; |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 2133 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 2134 | if (!data->nr_pages) |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 2135 | goto fail; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 2136 | |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2137 | perf_output_lock(handle); |
| 2138 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 2139 | do { |
| 2140 | offset = head = atomic_read(&data->head); |
Peter Zijlstra | c7138f3 | 2009-03-24 13:18:16 +0100 | [diff] [blame] | 2141 | head += size; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 2142 | } while (atomic_cmpxchg(&data->head, offset, head) != offset); |
| 2143 | |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 2144 | handle->offset = offset; |
Peter Zijlstra | 63e35b2 | 2009-03-25 12:30:24 +0100 | [diff] [blame] | 2145 | handle->head = head; |
Peter Zijlstra | c66de4a | 2009-05-05 17:50:22 +0200 | [diff] [blame] | 2146 | |
| 2147 | if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT)) |
| 2148 | atomic_set(&data->wakeup, 1); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 2149 | |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 2150 | return 0; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 2151 | |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 2152 | fail: |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2153 | perf_output_wakeup(handle); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 2154 | out: |
| 2155 | rcu_read_unlock(); |
| 2156 | |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 2157 | return -ENOSPC; |
| 2158 | } |
| 2159 | |
| 2160 | static void perf_output_copy(struct perf_output_handle *handle, |
| 2161 | void *buf, unsigned int len) |
| 2162 | { |
| 2163 | unsigned int pages_mask; |
| 2164 | unsigned int offset; |
| 2165 | unsigned int size; |
| 2166 | void **pages; |
| 2167 | |
| 2168 | offset = handle->offset; |
| 2169 | pages_mask = handle->data->nr_pages - 1; |
| 2170 | pages = handle->data->data_pages; |
| 2171 | |
| 2172 | do { |
| 2173 | unsigned int page_offset; |
| 2174 | int nr; |
| 2175 | |
| 2176 | nr = (offset >> PAGE_SHIFT) & pages_mask; |
| 2177 | page_offset = offset & (PAGE_SIZE - 1); |
| 2178 | size = min_t(unsigned int, PAGE_SIZE - page_offset, len); |
| 2179 | |
| 2180 | memcpy(pages[nr] + page_offset, buf, size); |
| 2181 | |
| 2182 | len -= size; |
| 2183 | buf += size; |
| 2184 | offset += size; |
| 2185 | } while (len); |
| 2186 | |
| 2187 | handle->offset = offset; |
Peter Zijlstra | 63e35b2 | 2009-03-25 12:30:24 +0100 | [diff] [blame] | 2188 | |
Peter Zijlstra | 53020fe | 2009-05-13 21:26:19 +0200 | [diff] [blame] | 2189 | /* |
| 2190 | * Check we didn't copy past our reservation window, taking the |
| 2191 | * possible unsigned int wrap into account. |
| 2192 | */ |
| 2193 | WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0); |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 2194 | } |
| 2195 | |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 2196 | #define perf_output_put(handle, x) \ |
| 2197 | perf_output_copy((handle), &(x), sizeof(x)) |
| 2198 | |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 2199 | static void perf_output_end(struct perf_output_handle *handle) |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 2200 | { |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2201 | struct perf_counter *counter = handle->counter; |
| 2202 | struct perf_mmap_data *data = handle->data; |
| 2203 | |
| 2204 | int wakeup_events = counter->hw_event.wakeup_events; |
Peter Zijlstra | c457810 | 2009-04-02 11:12:01 +0200 | [diff] [blame] | 2205 | |
Peter Zijlstra | 4c9e254 | 2009-04-06 11:45:09 +0200 | [diff] [blame] | 2206 | if (handle->overflow && wakeup_events) { |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2207 | int events = atomic_inc_return(&data->events); |
Peter Zijlstra | c457810 | 2009-04-02 11:12:01 +0200 | [diff] [blame] | 2208 | if (events >= wakeup_events) { |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2209 | atomic_sub(wakeup_events, &data->events); |
Peter Zijlstra | c66de4a | 2009-05-05 17:50:22 +0200 | [diff] [blame] | 2210 | atomic_set(&data->wakeup, 1); |
Peter Zijlstra | c457810 | 2009-04-02 11:12:01 +0200 | [diff] [blame] | 2211 | } |
Peter Zijlstra | c33a0bc | 2009-05-01 12:23:16 +0200 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | perf_output_unlock(handle); |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 2215 | rcu_read_unlock(); |
| 2216 | } |
| 2217 | |
Peter Zijlstra | f6c7d5f | 2009-04-06 11:45:04 +0200 | [diff] [blame] | 2218 | static void perf_counter_output(struct perf_counter *counter, |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 2219 | int nmi, struct pt_regs *regs, u64 addr) |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 2220 | { |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 2221 | int ret; |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 2222 | u64 record_type = counter->hw_event.record_type; |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 2223 | struct perf_output_handle handle; |
| 2224 | struct perf_event_header header; |
| 2225 | u64 ip; |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 2226 | struct { |
Peter Zijlstra | ea5d20c | 2009-03-25 12:30:25 +0100 | [diff] [blame] | 2227 | u32 pid, tid; |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 2228 | } tid_entry; |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 2229 | struct { |
| 2230 | u64 event; |
| 2231 | u64 counter; |
| 2232 | } group_entry; |
Peter Zijlstra | 394ee07 | 2009-03-30 19:07:14 +0200 | [diff] [blame] | 2233 | struct perf_callchain_entry *callchain = NULL; |
| 2234 | int callchain_size = 0; |
Peter Zijlstra | 339f7c9 | 2009-04-06 11:45:06 +0200 | [diff] [blame] | 2235 | u64 time; |
Peter Zijlstra | f370e1e | 2009-05-08 18:52:24 +0200 | [diff] [blame] | 2236 | struct { |
| 2237 | u32 cpu, reserved; |
| 2238 | } cpu_entry; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 2239 | |
Peter Zijlstra | 6b6e548 | 2009-04-08 15:01:27 +0200 | [diff] [blame] | 2240 | header.type = 0; |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 2241 | header.size = sizeof(header); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 2242 | |
Peter Zijlstra | 6b6e548 | 2009-04-08 15:01:27 +0200 | [diff] [blame] | 2243 | header.misc = PERF_EVENT_MISC_OVERFLOW; |
Paul Mackerras | 9d23a90 | 2009-05-14 21:48:08 +1000 | [diff] [blame] | 2244 | header.misc |= perf_misc_flags(regs); |
Peter Zijlstra | 6fab019 | 2009-04-08 15:01:26 +0200 | [diff] [blame] | 2245 | |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 2246 | if (record_type & PERF_RECORD_IP) { |
Paul Mackerras | 9d23a90 | 2009-05-14 21:48:08 +1000 | [diff] [blame] | 2247 | ip = perf_instruction_pointer(regs); |
Peter Zijlstra | 6b6e548 | 2009-04-08 15:01:27 +0200 | [diff] [blame] | 2248 | header.type |= PERF_RECORD_IP; |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 2249 | header.size += sizeof(ip); |
| 2250 | } |
Peter Zijlstra | ea5d20c | 2009-03-25 12:30:25 +0100 | [diff] [blame] | 2251 | |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 2252 | if (record_type & PERF_RECORD_TID) { |
Peter Zijlstra | ea5d20c | 2009-03-25 12:30:25 +0100 | [diff] [blame] | 2253 | /* namespace issues */ |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 2254 | tid_entry.pid = current->group_leader->pid; |
| 2255 | tid_entry.tid = current->pid; |
Peter Zijlstra | ea5d20c | 2009-03-25 12:30:25 +0100 | [diff] [blame] | 2256 | |
Peter Zijlstra | 6b6e548 | 2009-04-08 15:01:27 +0200 | [diff] [blame] | 2257 | header.type |= PERF_RECORD_TID; |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 2258 | header.size += sizeof(tid_entry); |
| 2259 | } |
Peter Zijlstra | ea5d20c | 2009-03-25 12:30:25 +0100 | [diff] [blame] | 2260 | |
Peter Zijlstra | 4d85545 | 2009-04-08 15:01:32 +0200 | [diff] [blame] | 2261 | if (record_type & PERF_RECORD_TIME) { |
| 2262 | /* |
| 2263 | * Maybe do better on x86 and provide cpu_clock_nmi() |
| 2264 | */ |
| 2265 | time = sched_clock(); |
| 2266 | |
| 2267 | header.type |= PERF_RECORD_TIME; |
| 2268 | header.size += sizeof(u64); |
| 2269 | } |
| 2270 | |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 2271 | if (record_type & PERF_RECORD_ADDR) { |
| 2272 | header.type |= PERF_RECORD_ADDR; |
| 2273 | header.size += sizeof(u64); |
| 2274 | } |
| 2275 | |
Peter Zijlstra | a85f61a | 2009-05-08 18:52:23 +0200 | [diff] [blame] | 2276 | if (record_type & PERF_RECORD_CONFIG) { |
| 2277 | header.type |= PERF_RECORD_CONFIG; |
| 2278 | header.size += sizeof(u64); |
| 2279 | } |
| 2280 | |
Peter Zijlstra | f370e1e | 2009-05-08 18:52:24 +0200 | [diff] [blame] | 2281 | if (record_type & PERF_RECORD_CPU) { |
| 2282 | header.type |= PERF_RECORD_CPU; |
| 2283 | header.size += sizeof(cpu_entry); |
| 2284 | |
| 2285 | cpu_entry.cpu = raw_smp_processor_id(); |
| 2286 | } |
| 2287 | |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 2288 | if (record_type & PERF_RECORD_GROUP) { |
Peter Zijlstra | 6b6e548 | 2009-04-08 15:01:27 +0200 | [diff] [blame] | 2289 | header.type |= PERF_RECORD_GROUP; |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 2290 | header.size += sizeof(u64) + |
| 2291 | counter->nr_siblings * sizeof(group_entry); |
| 2292 | } |
| 2293 | |
| 2294 | if (record_type & PERF_RECORD_CALLCHAIN) { |
Peter Zijlstra | 394ee07 | 2009-03-30 19:07:14 +0200 | [diff] [blame] | 2295 | callchain = perf_callchain(regs); |
| 2296 | |
| 2297 | if (callchain) { |
Peter Zijlstra | 9c03d88 | 2009-04-06 11:45:00 +0200 | [diff] [blame] | 2298 | callchain_size = (1 + callchain->nr) * sizeof(u64); |
Peter Zijlstra | 394ee07 | 2009-03-30 19:07:14 +0200 | [diff] [blame] | 2299 | |
Peter Zijlstra | 6b6e548 | 2009-04-08 15:01:27 +0200 | [diff] [blame] | 2300 | header.type |= PERF_RECORD_CALLCHAIN; |
Peter Zijlstra | 394ee07 | 2009-03-30 19:07:14 +0200 | [diff] [blame] | 2301 | header.size += callchain_size; |
| 2302 | } |
| 2303 | } |
| 2304 | |
Peter Zijlstra | 4c9e254 | 2009-04-06 11:45:09 +0200 | [diff] [blame] | 2305 | ret = perf_output_begin(&handle, counter, header.size, nmi, 1); |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 2306 | if (ret) |
| 2307 | return; |
Peter Zijlstra | ea5d20c | 2009-03-25 12:30:25 +0100 | [diff] [blame] | 2308 | |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 2309 | perf_output_put(&handle, header); |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 2310 | |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 2311 | if (record_type & PERF_RECORD_IP) |
| 2312 | perf_output_put(&handle, ip); |
| 2313 | |
| 2314 | if (record_type & PERF_RECORD_TID) |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 2315 | perf_output_put(&handle, tid_entry); |
| 2316 | |
Peter Zijlstra | 4d85545 | 2009-04-08 15:01:32 +0200 | [diff] [blame] | 2317 | if (record_type & PERF_RECORD_TIME) |
| 2318 | perf_output_put(&handle, time); |
| 2319 | |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 2320 | if (record_type & PERF_RECORD_ADDR) |
| 2321 | perf_output_put(&handle, addr); |
| 2322 | |
Peter Zijlstra | a85f61a | 2009-05-08 18:52:23 +0200 | [diff] [blame] | 2323 | if (record_type & PERF_RECORD_CONFIG) |
| 2324 | perf_output_put(&handle, counter->hw_event.config); |
| 2325 | |
Peter Zijlstra | f370e1e | 2009-05-08 18:52:24 +0200 | [diff] [blame] | 2326 | if (record_type & PERF_RECORD_CPU) |
| 2327 | perf_output_put(&handle, cpu_entry); |
| 2328 | |
Peter Zijlstra | 2023b35 | 2009-05-05 17:50:26 +0200 | [diff] [blame] | 2329 | /* |
| 2330 | * XXX PERF_RECORD_GROUP vs inherited counters seems difficult. |
| 2331 | */ |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 2332 | if (record_type & PERF_RECORD_GROUP) { |
| 2333 | struct perf_counter *leader, *sub; |
| 2334 | u64 nr = counter->nr_siblings; |
| 2335 | |
| 2336 | perf_output_put(&handle, nr); |
| 2337 | |
| 2338 | leader = counter->group_leader; |
| 2339 | list_for_each_entry(sub, &leader->sibling_list, list_entry) { |
| 2340 | if (sub != counter) |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 2341 | sub->pmu->read(sub); |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 2342 | |
| 2343 | group_entry.event = sub->hw_event.config; |
| 2344 | group_entry.counter = atomic64_read(&sub->count); |
| 2345 | |
| 2346 | perf_output_put(&handle, group_entry); |
| 2347 | } |
| 2348 | } |
| 2349 | |
Peter Zijlstra | 394ee07 | 2009-03-30 19:07:14 +0200 | [diff] [blame] | 2350 | if (callchain) |
| 2351 | perf_output_copy(&handle, callchain, callchain_size); |
| 2352 | |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 2353 | perf_output_end(&handle); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 2354 | } |
| 2355 | |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 2356 | /* |
Peter Zijlstra | 8d1b2d9 | 2009-04-08 15:01:30 +0200 | [diff] [blame] | 2357 | * comm tracking |
| 2358 | */ |
| 2359 | |
| 2360 | struct perf_comm_event { |
| 2361 | struct task_struct *task; |
| 2362 | char *comm; |
| 2363 | int comm_size; |
| 2364 | |
| 2365 | struct { |
| 2366 | struct perf_event_header header; |
| 2367 | |
| 2368 | u32 pid; |
| 2369 | u32 tid; |
| 2370 | } event; |
| 2371 | }; |
| 2372 | |
| 2373 | static void perf_counter_comm_output(struct perf_counter *counter, |
| 2374 | struct perf_comm_event *comm_event) |
| 2375 | { |
| 2376 | struct perf_output_handle handle; |
| 2377 | int size = comm_event->event.header.size; |
| 2378 | int ret = perf_output_begin(&handle, counter, size, 0, 0); |
| 2379 | |
| 2380 | if (ret) |
| 2381 | return; |
| 2382 | |
| 2383 | perf_output_put(&handle, comm_event->event); |
| 2384 | perf_output_copy(&handle, comm_event->comm, |
| 2385 | comm_event->comm_size); |
| 2386 | perf_output_end(&handle); |
| 2387 | } |
| 2388 | |
| 2389 | static int perf_counter_comm_match(struct perf_counter *counter, |
| 2390 | struct perf_comm_event *comm_event) |
| 2391 | { |
| 2392 | if (counter->hw_event.comm && |
| 2393 | comm_event->event.header.type == PERF_EVENT_COMM) |
| 2394 | return 1; |
| 2395 | |
| 2396 | return 0; |
| 2397 | } |
| 2398 | |
| 2399 | static void perf_counter_comm_ctx(struct perf_counter_context *ctx, |
| 2400 | struct perf_comm_event *comm_event) |
| 2401 | { |
| 2402 | struct perf_counter *counter; |
| 2403 | |
| 2404 | if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list)) |
| 2405 | return; |
| 2406 | |
| 2407 | rcu_read_lock(); |
| 2408 | list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) { |
| 2409 | if (perf_counter_comm_match(counter, comm_event)) |
| 2410 | perf_counter_comm_output(counter, comm_event); |
| 2411 | } |
| 2412 | rcu_read_unlock(); |
| 2413 | } |
| 2414 | |
| 2415 | static void perf_counter_comm_event(struct perf_comm_event *comm_event) |
| 2416 | { |
| 2417 | struct perf_cpu_context *cpuctx; |
| 2418 | unsigned int size; |
| 2419 | char *comm = comm_event->task->comm; |
| 2420 | |
Ingo Molnar | 888fcee | 2009-04-09 09:48:22 +0200 | [diff] [blame] | 2421 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
Peter Zijlstra | 8d1b2d9 | 2009-04-08 15:01:30 +0200 | [diff] [blame] | 2422 | |
| 2423 | comm_event->comm = comm; |
| 2424 | comm_event->comm_size = size; |
| 2425 | |
| 2426 | comm_event->event.header.size = sizeof(comm_event->event) + size; |
| 2427 | |
| 2428 | cpuctx = &get_cpu_var(perf_cpu_context); |
| 2429 | perf_counter_comm_ctx(&cpuctx->ctx, comm_event); |
| 2430 | put_cpu_var(perf_cpu_context); |
| 2431 | |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 2432 | perf_counter_comm_ctx(current->perf_counter_ctxp, comm_event); |
Peter Zijlstra | 8d1b2d9 | 2009-04-08 15:01:30 +0200 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | void perf_counter_comm(struct task_struct *task) |
| 2436 | { |
Peter Zijlstra | 9ee318a | 2009-04-09 10:53:44 +0200 | [diff] [blame] | 2437 | struct perf_comm_event comm_event; |
| 2438 | |
| 2439 | if (!atomic_read(&nr_comm_tracking)) |
| 2440 | return; |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 2441 | if (!current->perf_counter_ctxp) |
| 2442 | return; |
| 2443 | |
Peter Zijlstra | 9ee318a | 2009-04-09 10:53:44 +0200 | [diff] [blame] | 2444 | comm_event = (struct perf_comm_event){ |
Peter Zijlstra | 8d1b2d9 | 2009-04-08 15:01:30 +0200 | [diff] [blame] | 2445 | .task = task, |
| 2446 | .event = { |
| 2447 | .header = { .type = PERF_EVENT_COMM, }, |
| 2448 | .pid = task->group_leader->pid, |
| 2449 | .tid = task->pid, |
| 2450 | }, |
| 2451 | }; |
| 2452 | |
| 2453 | perf_counter_comm_event(&comm_event); |
| 2454 | } |
| 2455 | |
| 2456 | /* |
Peter Zijlstra | 0a4a939 | 2009-03-30 19:07:05 +0200 | [diff] [blame] | 2457 | * mmap tracking |
| 2458 | */ |
| 2459 | |
| 2460 | struct perf_mmap_event { |
| 2461 | struct file *file; |
| 2462 | char *file_name; |
| 2463 | int file_size; |
| 2464 | |
| 2465 | struct { |
| 2466 | struct perf_event_header header; |
| 2467 | |
| 2468 | u32 pid; |
| 2469 | u32 tid; |
| 2470 | u64 start; |
| 2471 | u64 len; |
| 2472 | u64 pgoff; |
| 2473 | } event; |
| 2474 | }; |
| 2475 | |
| 2476 | static void perf_counter_mmap_output(struct perf_counter *counter, |
| 2477 | struct perf_mmap_event *mmap_event) |
| 2478 | { |
| 2479 | struct perf_output_handle handle; |
| 2480 | int size = mmap_event->event.header.size; |
Peter Zijlstra | 4c9e254 | 2009-04-06 11:45:09 +0200 | [diff] [blame] | 2481 | int ret = perf_output_begin(&handle, counter, size, 0, 0); |
Peter Zijlstra | 0a4a939 | 2009-03-30 19:07:05 +0200 | [diff] [blame] | 2482 | |
| 2483 | if (ret) |
| 2484 | return; |
| 2485 | |
| 2486 | perf_output_put(&handle, mmap_event->event); |
| 2487 | perf_output_copy(&handle, mmap_event->file_name, |
| 2488 | mmap_event->file_size); |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 2489 | perf_output_end(&handle); |
Peter Zijlstra | 0a4a939 | 2009-03-30 19:07:05 +0200 | [diff] [blame] | 2490 | } |
| 2491 | |
| 2492 | static int perf_counter_mmap_match(struct perf_counter *counter, |
| 2493 | struct perf_mmap_event *mmap_event) |
| 2494 | { |
| 2495 | if (counter->hw_event.mmap && |
| 2496 | mmap_event->event.header.type == PERF_EVENT_MMAP) |
| 2497 | return 1; |
| 2498 | |
| 2499 | if (counter->hw_event.munmap && |
| 2500 | mmap_event->event.header.type == PERF_EVENT_MUNMAP) |
| 2501 | return 1; |
| 2502 | |
| 2503 | return 0; |
| 2504 | } |
| 2505 | |
| 2506 | static void perf_counter_mmap_ctx(struct perf_counter_context *ctx, |
| 2507 | struct perf_mmap_event *mmap_event) |
| 2508 | { |
| 2509 | struct perf_counter *counter; |
| 2510 | |
| 2511 | if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list)) |
| 2512 | return; |
| 2513 | |
| 2514 | rcu_read_lock(); |
| 2515 | list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) { |
| 2516 | if (perf_counter_mmap_match(counter, mmap_event)) |
| 2517 | perf_counter_mmap_output(counter, mmap_event); |
| 2518 | } |
| 2519 | rcu_read_unlock(); |
| 2520 | } |
| 2521 | |
| 2522 | static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event) |
| 2523 | { |
| 2524 | struct perf_cpu_context *cpuctx; |
| 2525 | struct file *file = mmap_event->file; |
| 2526 | unsigned int size; |
| 2527 | char tmp[16]; |
| 2528 | char *buf = NULL; |
| 2529 | char *name; |
| 2530 | |
| 2531 | if (file) { |
| 2532 | buf = kzalloc(PATH_MAX, GFP_KERNEL); |
| 2533 | if (!buf) { |
| 2534 | name = strncpy(tmp, "//enomem", sizeof(tmp)); |
| 2535 | goto got_name; |
| 2536 | } |
Peter Zijlstra | d3d21c4 | 2009-04-09 10:53:46 +0200 | [diff] [blame] | 2537 | name = d_path(&file->f_path, buf, PATH_MAX); |
Peter Zijlstra | 0a4a939 | 2009-03-30 19:07:05 +0200 | [diff] [blame] | 2538 | if (IS_ERR(name)) { |
| 2539 | name = strncpy(tmp, "//toolong", sizeof(tmp)); |
| 2540 | goto got_name; |
| 2541 | } |
| 2542 | } else { |
| 2543 | name = strncpy(tmp, "//anon", sizeof(tmp)); |
| 2544 | goto got_name; |
| 2545 | } |
| 2546 | |
| 2547 | got_name: |
Ingo Molnar | 888fcee | 2009-04-09 09:48:22 +0200 | [diff] [blame] | 2548 | size = ALIGN(strlen(name)+1, sizeof(u64)); |
Peter Zijlstra | 0a4a939 | 2009-03-30 19:07:05 +0200 | [diff] [blame] | 2549 | |
| 2550 | mmap_event->file_name = name; |
| 2551 | mmap_event->file_size = size; |
| 2552 | |
| 2553 | mmap_event->event.header.size = sizeof(mmap_event->event) + size; |
| 2554 | |
| 2555 | cpuctx = &get_cpu_var(perf_cpu_context); |
| 2556 | perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event); |
| 2557 | put_cpu_var(perf_cpu_context); |
| 2558 | |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 2559 | perf_counter_mmap_ctx(current->perf_counter_ctxp, mmap_event); |
Peter Zijlstra | 0a4a939 | 2009-03-30 19:07:05 +0200 | [diff] [blame] | 2560 | |
| 2561 | kfree(buf); |
| 2562 | } |
| 2563 | |
| 2564 | void perf_counter_mmap(unsigned long addr, unsigned long len, |
| 2565 | unsigned long pgoff, struct file *file) |
| 2566 | { |
Peter Zijlstra | 9ee318a | 2009-04-09 10:53:44 +0200 | [diff] [blame] | 2567 | struct perf_mmap_event mmap_event; |
| 2568 | |
| 2569 | if (!atomic_read(&nr_mmap_tracking)) |
| 2570 | return; |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 2571 | if (!current->perf_counter_ctxp) |
| 2572 | return; |
Peter Zijlstra | 9ee318a | 2009-04-09 10:53:44 +0200 | [diff] [blame] | 2573 | |
| 2574 | mmap_event = (struct perf_mmap_event){ |
Peter Zijlstra | 0a4a939 | 2009-03-30 19:07:05 +0200 | [diff] [blame] | 2575 | .file = file, |
| 2576 | .event = { |
| 2577 | .header = { .type = PERF_EVENT_MMAP, }, |
| 2578 | .pid = current->group_leader->pid, |
| 2579 | .tid = current->pid, |
| 2580 | .start = addr, |
| 2581 | .len = len, |
| 2582 | .pgoff = pgoff, |
| 2583 | }, |
| 2584 | }; |
| 2585 | |
| 2586 | perf_counter_mmap_event(&mmap_event); |
| 2587 | } |
| 2588 | |
| 2589 | void perf_counter_munmap(unsigned long addr, unsigned long len, |
| 2590 | unsigned long pgoff, struct file *file) |
| 2591 | { |
Peter Zijlstra | 9ee318a | 2009-04-09 10:53:44 +0200 | [diff] [blame] | 2592 | struct perf_mmap_event mmap_event; |
| 2593 | |
| 2594 | if (!atomic_read(&nr_munmap_tracking)) |
| 2595 | return; |
| 2596 | |
| 2597 | mmap_event = (struct perf_mmap_event){ |
Peter Zijlstra | 0a4a939 | 2009-03-30 19:07:05 +0200 | [diff] [blame] | 2598 | .file = file, |
| 2599 | .event = { |
| 2600 | .header = { .type = PERF_EVENT_MUNMAP, }, |
| 2601 | .pid = current->group_leader->pid, |
| 2602 | .tid = current->pid, |
| 2603 | .start = addr, |
| 2604 | .len = len, |
| 2605 | .pgoff = pgoff, |
| 2606 | }, |
| 2607 | }; |
| 2608 | |
| 2609 | perf_counter_mmap_event(&mmap_event); |
| 2610 | } |
| 2611 | |
| 2612 | /* |
Peter Zijlstra | e220d2d | 2009-05-23 18:28:55 +0200 | [diff] [blame] | 2613 | * Log irq_period changes so that analyzing tools can re-normalize the |
| 2614 | * event flow. |
Peter Zijlstra | 26b119b | 2009-05-20 12:21:20 +0200 | [diff] [blame] | 2615 | */ |
| 2616 | |
| 2617 | static void perf_log_period(struct perf_counter *counter, u64 period) |
| 2618 | { |
| 2619 | struct perf_output_handle handle; |
| 2620 | int ret; |
| 2621 | |
| 2622 | struct { |
| 2623 | struct perf_event_header header; |
| 2624 | u64 time; |
| 2625 | u64 period; |
| 2626 | } freq_event = { |
| 2627 | .header = { |
| 2628 | .type = PERF_EVENT_PERIOD, |
| 2629 | .misc = 0, |
| 2630 | .size = sizeof(freq_event), |
| 2631 | }, |
| 2632 | .time = sched_clock(), |
| 2633 | .period = period, |
| 2634 | }; |
| 2635 | |
| 2636 | if (counter->hw.irq_period == period) |
| 2637 | return; |
| 2638 | |
| 2639 | ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0); |
| 2640 | if (ret) |
| 2641 | return; |
| 2642 | |
| 2643 | perf_output_put(&handle, freq_event); |
| 2644 | perf_output_end(&handle); |
| 2645 | } |
| 2646 | |
| 2647 | /* |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 2648 | * IRQ throttle logging |
| 2649 | */ |
| 2650 | |
| 2651 | static void perf_log_throttle(struct perf_counter *counter, int enable) |
| 2652 | { |
| 2653 | struct perf_output_handle handle; |
| 2654 | int ret; |
| 2655 | |
| 2656 | struct { |
| 2657 | struct perf_event_header header; |
| 2658 | u64 time; |
| 2659 | } throttle_event = { |
| 2660 | .header = { |
| 2661 | .type = PERF_EVENT_THROTTLE + 1, |
| 2662 | .misc = 0, |
| 2663 | .size = sizeof(throttle_event), |
| 2664 | }, |
| 2665 | .time = sched_clock(), |
| 2666 | }; |
| 2667 | |
Ingo Molnar | 0127c3e | 2009-05-25 22:03:26 +0200 | [diff] [blame] | 2668 | ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0); |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 2669 | if (ret) |
| 2670 | return; |
| 2671 | |
| 2672 | perf_output_put(&handle, throttle_event); |
| 2673 | perf_output_end(&handle); |
| 2674 | } |
| 2675 | |
| 2676 | /* |
Peter Zijlstra | f6c7d5f | 2009-04-06 11:45:04 +0200 | [diff] [blame] | 2677 | * Generic counter overflow handling. |
| 2678 | */ |
| 2679 | |
| 2680 | int perf_counter_overflow(struct perf_counter *counter, |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 2681 | int nmi, struct pt_regs *regs, u64 addr) |
Peter Zijlstra | f6c7d5f | 2009-04-06 11:45:04 +0200 | [diff] [blame] | 2682 | { |
Peter Zijlstra | 79f1464 | 2009-04-06 11:45:07 +0200 | [diff] [blame] | 2683 | int events = atomic_read(&counter->event_limit); |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 2684 | int throttle = counter->pmu->unthrottle != NULL; |
Peter Zijlstra | 79f1464 | 2009-04-06 11:45:07 +0200 | [diff] [blame] | 2685 | int ret = 0; |
| 2686 | |
Peter Zijlstra | a78ac32 | 2009-05-25 17:39:05 +0200 | [diff] [blame] | 2687 | if (!throttle) { |
| 2688 | counter->hw.interrupts++; |
| 2689 | } else if (counter->hw.interrupts != MAX_INTERRUPTS) { |
| 2690 | counter->hw.interrupts++; |
| 2691 | if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) { |
| 2692 | counter->hw.interrupts = MAX_INTERRUPTS; |
| 2693 | perf_log_throttle(counter, 0); |
| 2694 | ret = 1; |
| 2695 | } |
| 2696 | } |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 2697 | |
Peter Zijlstra | 2023b35 | 2009-05-05 17:50:26 +0200 | [diff] [blame] | 2698 | /* |
| 2699 | * XXX event_limit might not quite work as expected on inherited |
| 2700 | * counters |
| 2701 | */ |
| 2702 | |
Peter Zijlstra | 4c9e254 | 2009-04-06 11:45:09 +0200 | [diff] [blame] | 2703 | counter->pending_kill = POLL_IN; |
Peter Zijlstra | 79f1464 | 2009-04-06 11:45:07 +0200 | [diff] [blame] | 2704 | if (events && atomic_dec_and_test(&counter->event_limit)) { |
| 2705 | ret = 1; |
Peter Zijlstra | 4c9e254 | 2009-04-06 11:45:09 +0200 | [diff] [blame] | 2706 | counter->pending_kill = POLL_HUP; |
Peter Zijlstra | 79f1464 | 2009-04-06 11:45:07 +0200 | [diff] [blame] | 2707 | if (nmi) { |
| 2708 | counter->pending_disable = 1; |
| 2709 | perf_pending_queue(&counter->pending, |
| 2710 | perf_pending_counter); |
| 2711 | } else |
| 2712 | perf_counter_disable(counter); |
| 2713 | } |
| 2714 | |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 2715 | perf_counter_output(counter, nmi, regs, addr); |
Peter Zijlstra | 79f1464 | 2009-04-06 11:45:07 +0200 | [diff] [blame] | 2716 | return ret; |
Peter Zijlstra | f6c7d5f | 2009-04-06 11:45:04 +0200 | [diff] [blame] | 2717 | } |
| 2718 | |
| 2719 | /* |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2720 | * Generic software counter infrastructure |
| 2721 | */ |
| 2722 | |
| 2723 | static void perf_swcounter_update(struct perf_counter *counter) |
| 2724 | { |
| 2725 | struct hw_perf_counter *hwc = &counter->hw; |
| 2726 | u64 prev, now; |
| 2727 | s64 delta; |
| 2728 | |
| 2729 | again: |
| 2730 | prev = atomic64_read(&hwc->prev_count); |
| 2731 | now = atomic64_read(&hwc->count); |
| 2732 | if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev) |
| 2733 | goto again; |
| 2734 | |
| 2735 | delta = now - prev; |
| 2736 | |
| 2737 | atomic64_add(delta, &counter->count); |
| 2738 | atomic64_sub(delta, &hwc->period_left); |
| 2739 | } |
| 2740 | |
| 2741 | static void perf_swcounter_set_period(struct perf_counter *counter) |
| 2742 | { |
| 2743 | struct hw_perf_counter *hwc = &counter->hw; |
| 2744 | s64 left = atomic64_read(&hwc->period_left); |
| 2745 | s64 period = hwc->irq_period; |
| 2746 | |
| 2747 | if (unlikely(left <= -period)) { |
| 2748 | left = period; |
| 2749 | atomic64_set(&hwc->period_left, left); |
| 2750 | } |
| 2751 | |
| 2752 | if (unlikely(left <= 0)) { |
| 2753 | left += period; |
| 2754 | atomic64_add(period, &hwc->period_left); |
| 2755 | } |
| 2756 | |
| 2757 | atomic64_set(&hwc->prev_count, -left); |
| 2758 | atomic64_set(&hwc->count, -left); |
| 2759 | } |
| 2760 | |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2761 | static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer) |
| 2762 | { |
Peter Zijlstra | f6c7d5f | 2009-04-06 11:45:04 +0200 | [diff] [blame] | 2763 | enum hrtimer_restart ret = HRTIMER_RESTART; |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2764 | struct perf_counter *counter; |
| 2765 | struct pt_regs *regs; |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 2766 | u64 period; |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2767 | |
| 2768 | counter = container_of(hrtimer, struct perf_counter, hw.hrtimer); |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 2769 | counter->pmu->read(counter); |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2770 | |
| 2771 | regs = get_irq_regs(); |
| 2772 | /* |
| 2773 | * In case we exclude kernel IPs or are somehow not in interrupt |
| 2774 | * context, provide the next best thing, the user IP. |
| 2775 | */ |
| 2776 | if ((counter->hw_event.exclude_kernel || !regs) && |
| 2777 | !counter->hw_event.exclude_user) |
| 2778 | regs = task_pt_regs(current); |
| 2779 | |
Peter Zijlstra | f6c7d5f | 2009-04-06 11:45:04 +0200 | [diff] [blame] | 2780 | if (regs) { |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 2781 | if (perf_counter_overflow(counter, 0, regs, 0)) |
Peter Zijlstra | f6c7d5f | 2009-04-06 11:45:04 +0200 | [diff] [blame] | 2782 | ret = HRTIMER_NORESTART; |
| 2783 | } |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2784 | |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 2785 | period = max_t(u64, 10000, counter->hw.irq_period); |
| 2786 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2787 | |
Peter Zijlstra | f6c7d5f | 2009-04-06 11:45:04 +0200 | [diff] [blame] | 2788 | return ret; |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2789 | } |
| 2790 | |
| 2791 | static void perf_swcounter_overflow(struct perf_counter *counter, |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 2792 | int nmi, struct pt_regs *regs, u64 addr) |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2793 | { |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2794 | perf_swcounter_update(counter); |
| 2795 | perf_swcounter_set_period(counter); |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 2796 | if (perf_counter_overflow(counter, nmi, regs, addr)) |
Peter Zijlstra | f6c7d5f | 2009-04-06 11:45:04 +0200 | [diff] [blame] | 2797 | /* soft-disable the counter */ |
| 2798 | ; |
| 2799 | |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2800 | } |
| 2801 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2802 | static int perf_swcounter_match(struct perf_counter *counter, |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2803 | enum perf_event_types type, |
| 2804 | u32 event, struct pt_regs *regs) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2805 | { |
| 2806 | if (counter->state != PERF_COUNTER_STATE_ACTIVE) |
| 2807 | return 0; |
| 2808 | |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2809 | if (perf_event_raw(&counter->hw_event)) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2810 | return 0; |
| 2811 | |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2812 | if (perf_event_type(&counter->hw_event) != type) |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2813 | return 0; |
| 2814 | |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2815 | if (perf_event_id(&counter->hw_event) != event) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2816 | return 0; |
| 2817 | |
| 2818 | if (counter->hw_event.exclude_user && user_mode(regs)) |
| 2819 | return 0; |
| 2820 | |
| 2821 | if (counter->hw_event.exclude_kernel && !user_mode(regs)) |
| 2822 | return 0; |
| 2823 | |
| 2824 | return 1; |
| 2825 | } |
| 2826 | |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2827 | static void perf_swcounter_add(struct perf_counter *counter, u64 nr, |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 2828 | int nmi, struct pt_regs *regs, u64 addr) |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2829 | { |
| 2830 | int neg = atomic64_add_negative(nr, &counter->hw.count); |
| 2831 | if (counter->hw.irq_period && !neg) |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 2832 | perf_swcounter_overflow(counter, nmi, regs, addr); |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2833 | } |
| 2834 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2835 | static void perf_swcounter_ctx_event(struct perf_counter_context *ctx, |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2836 | enum perf_event_types type, u32 event, |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 2837 | u64 nr, int nmi, struct pt_regs *regs, |
| 2838 | u64 addr) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2839 | { |
| 2840 | struct perf_counter *counter; |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2841 | |
Peter Zijlstra | 01ef09d | 2009-03-19 20:26:11 +0100 | [diff] [blame] | 2842 | if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list)) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2843 | return; |
| 2844 | |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 2845 | rcu_read_lock(); |
| 2846 | list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) { |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2847 | if (perf_swcounter_match(counter, type, event, regs)) |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 2848 | perf_swcounter_add(counter, nr, nmi, regs, addr); |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2849 | } |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 2850 | rcu_read_unlock(); |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2851 | } |
| 2852 | |
Peter Zijlstra | 96f6d44 | 2009-03-23 18:22:07 +0100 | [diff] [blame] | 2853 | static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx) |
| 2854 | { |
| 2855 | if (in_nmi()) |
| 2856 | return &cpuctx->recursion[3]; |
| 2857 | |
| 2858 | if (in_irq()) |
| 2859 | return &cpuctx->recursion[2]; |
| 2860 | |
| 2861 | if (in_softirq()) |
| 2862 | return &cpuctx->recursion[1]; |
| 2863 | |
| 2864 | return &cpuctx->recursion[0]; |
| 2865 | } |
| 2866 | |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2867 | static void __perf_swcounter_event(enum perf_event_types type, u32 event, |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 2868 | u64 nr, int nmi, struct pt_regs *regs, |
| 2869 | u64 addr) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2870 | { |
| 2871 | struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context); |
Peter Zijlstra | 96f6d44 | 2009-03-23 18:22:07 +0100 | [diff] [blame] | 2872 | int *recursion = perf_swcounter_recursion_context(cpuctx); |
| 2873 | |
| 2874 | if (*recursion) |
| 2875 | goto out; |
| 2876 | |
| 2877 | (*recursion)++; |
| 2878 | barrier(); |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2879 | |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 2880 | perf_swcounter_ctx_event(&cpuctx->ctx, type, event, |
| 2881 | nr, nmi, regs, addr); |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2882 | if (cpuctx->task_ctx) { |
| 2883 | perf_swcounter_ctx_event(cpuctx->task_ctx, type, event, |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 2884 | nr, nmi, regs, addr); |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2885 | } |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2886 | |
Peter Zijlstra | 96f6d44 | 2009-03-23 18:22:07 +0100 | [diff] [blame] | 2887 | barrier(); |
| 2888 | (*recursion)--; |
| 2889 | |
| 2890 | out: |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2891 | put_cpu_var(perf_cpu_context); |
| 2892 | } |
| 2893 | |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 2894 | void |
| 2895 | perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr) |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2896 | { |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 2897 | __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr); |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2898 | } |
| 2899 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2900 | static void perf_swcounter_read(struct perf_counter *counter) |
| 2901 | { |
| 2902 | perf_swcounter_update(counter); |
| 2903 | } |
| 2904 | |
| 2905 | static int perf_swcounter_enable(struct perf_counter *counter) |
| 2906 | { |
| 2907 | perf_swcounter_set_period(counter); |
| 2908 | return 0; |
| 2909 | } |
| 2910 | |
| 2911 | static void perf_swcounter_disable(struct perf_counter *counter) |
| 2912 | { |
| 2913 | perf_swcounter_update(counter); |
| 2914 | } |
| 2915 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 2916 | static const struct pmu perf_ops_generic = { |
Peter Zijlstra | ac17dc8 | 2009-03-13 12:21:34 +0100 | [diff] [blame] | 2917 | .enable = perf_swcounter_enable, |
| 2918 | .disable = perf_swcounter_disable, |
| 2919 | .read = perf_swcounter_read, |
| 2920 | }; |
| 2921 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2922 | /* |
| 2923 | * Software counter: cpu wall time clock |
| 2924 | */ |
| 2925 | |
Paul Mackerras | 9abf8a0 | 2009-01-09 16:26:43 +1100 | [diff] [blame] | 2926 | static void cpu_clock_perf_counter_update(struct perf_counter *counter) |
| 2927 | { |
| 2928 | int cpu = raw_smp_processor_id(); |
| 2929 | s64 prev; |
| 2930 | u64 now; |
| 2931 | |
| 2932 | now = cpu_clock(cpu); |
| 2933 | prev = atomic64_read(&counter->hw.prev_count); |
| 2934 | atomic64_set(&counter->hw.prev_count, now); |
| 2935 | atomic64_add(now - prev, &counter->count); |
| 2936 | } |
| 2937 | |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2938 | static int cpu_clock_perf_counter_enable(struct perf_counter *counter) |
| 2939 | { |
| 2940 | struct hw_perf_counter *hwc = &counter->hw; |
| 2941 | int cpu = raw_smp_processor_id(); |
| 2942 | |
| 2943 | atomic64_set(&hwc->prev_count, cpu_clock(cpu)); |
Peter Zijlstra | 039fc91 | 2009-03-13 16:43:47 +0100 | [diff] [blame] | 2944 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 2945 | hwc->hrtimer.function = perf_swcounter_hrtimer; |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2946 | if (hwc->irq_period) { |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 2947 | u64 period = max_t(u64, 10000, hwc->irq_period); |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2948 | __hrtimer_start_range_ns(&hwc->hrtimer, |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 2949 | ns_to_ktime(period), 0, |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2950 | HRTIMER_MODE_REL, 0); |
| 2951 | } |
| 2952 | |
| 2953 | return 0; |
| 2954 | } |
| 2955 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2956 | static void cpu_clock_perf_counter_disable(struct perf_counter *counter) |
| 2957 | { |
Peter Zijlstra | b986d7e | 2009-05-20 12:21:21 +0200 | [diff] [blame] | 2958 | if (counter->hw.irq_period) |
| 2959 | hrtimer_cancel(&counter->hw.hrtimer); |
Paul Mackerras | 9abf8a0 | 2009-01-09 16:26:43 +1100 | [diff] [blame] | 2960 | cpu_clock_perf_counter_update(counter); |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | static void cpu_clock_perf_counter_read(struct perf_counter *counter) |
| 2964 | { |
Paul Mackerras | 9abf8a0 | 2009-01-09 16:26:43 +1100 | [diff] [blame] | 2965 | cpu_clock_perf_counter_update(counter); |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2966 | } |
| 2967 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 2968 | static const struct pmu perf_ops_cpu_clock = { |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 2969 | .enable = cpu_clock_perf_counter_enable, |
| 2970 | .disable = cpu_clock_perf_counter_disable, |
| 2971 | .read = cpu_clock_perf_counter_read, |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2972 | }; |
| 2973 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 2974 | /* |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2975 | * Software counter: task time clock |
| 2976 | */ |
| 2977 | |
Peter Zijlstra | e30e08f | 2009-04-08 15:01:25 +0200 | [diff] [blame] | 2978 | static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now) |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 2979 | { |
Peter Zijlstra | e30e08f | 2009-04-08 15:01:25 +0200 | [diff] [blame] | 2980 | u64 prev; |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 2981 | s64 delta; |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 2982 | |
Peter Zijlstra | a39d6f2 | 2009-04-06 11:45:11 +0200 | [diff] [blame] | 2983 | prev = atomic64_xchg(&counter->hw.prev_count, now); |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 2984 | delta = now - prev; |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 2985 | atomic64_add(delta, &counter->count); |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 2986 | } |
| 2987 | |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 2988 | static int task_clock_perf_counter_enable(struct perf_counter *counter) |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 2989 | { |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2990 | struct hw_perf_counter *hwc = &counter->hw; |
Peter Zijlstra | a39d6f2 | 2009-04-06 11:45:11 +0200 | [diff] [blame] | 2991 | u64 now; |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2992 | |
Peter Zijlstra | a39d6f2 | 2009-04-06 11:45:11 +0200 | [diff] [blame] | 2993 | now = counter->ctx->time; |
| 2994 | |
| 2995 | atomic64_set(&hwc->prev_count, now); |
Peter Zijlstra | 039fc91 | 2009-03-13 16:43:47 +0100 | [diff] [blame] | 2996 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 2997 | hwc->hrtimer.function = perf_swcounter_hrtimer; |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2998 | if (hwc->irq_period) { |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 2999 | u64 period = max_t(u64, 10000, hwc->irq_period); |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 3000 | __hrtimer_start_range_ns(&hwc->hrtimer, |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 3001 | ns_to_ktime(period), 0, |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 3002 | HRTIMER_MODE_REL, 0); |
| 3003 | } |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 3004 | |
| 3005 | return 0; |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | static void task_clock_perf_counter_disable(struct perf_counter *counter) |
| 3009 | { |
Peter Zijlstra | b986d7e | 2009-05-20 12:21:21 +0200 | [diff] [blame] | 3010 | if (counter->hw.irq_period) |
| 3011 | hrtimer_cancel(&counter->hw.hrtimer); |
Peter Zijlstra | e30e08f | 2009-04-08 15:01:25 +0200 | [diff] [blame] | 3012 | task_clock_perf_counter_update(counter, counter->ctx->time); |
| 3013 | |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 3014 | } |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 3015 | |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 3016 | static void task_clock_perf_counter_read(struct perf_counter *counter) |
| 3017 | { |
Peter Zijlstra | e30e08f | 2009-04-08 15:01:25 +0200 | [diff] [blame] | 3018 | u64 time; |
| 3019 | |
| 3020 | if (!in_nmi()) { |
| 3021 | update_context_time(counter->ctx); |
| 3022 | time = counter->ctx->time; |
| 3023 | } else { |
| 3024 | u64 now = perf_clock(); |
| 3025 | u64 delta = now - counter->ctx->timestamp; |
| 3026 | time = counter->ctx->time + delta; |
| 3027 | } |
| 3028 | |
| 3029 | task_clock_perf_counter_update(counter, time); |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 3030 | } |
| 3031 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3032 | static const struct pmu perf_ops_task_clock = { |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 3033 | .enable = task_clock_perf_counter_enable, |
| 3034 | .disable = task_clock_perf_counter_disable, |
| 3035 | .read = task_clock_perf_counter_read, |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 3036 | }; |
| 3037 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 3038 | /* |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 3039 | * Software counter: cpu migrations |
| 3040 | */ |
| 3041 | |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 3042 | static inline u64 get_cpu_migrations(struct perf_counter *counter) |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 3043 | { |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 3044 | struct task_struct *curr = counter->ctx->task; |
| 3045 | |
| 3046 | if (curr) |
| 3047 | return curr->se.nr_migrations; |
| 3048 | return cpu_nr_migrations(smp_processor_id()); |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 3049 | } |
| 3050 | |
| 3051 | static void cpu_migrations_perf_counter_update(struct perf_counter *counter) |
| 3052 | { |
| 3053 | u64 prev, now; |
| 3054 | s64 delta; |
| 3055 | |
| 3056 | prev = atomic64_read(&counter->hw.prev_count); |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 3057 | now = get_cpu_migrations(counter); |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 3058 | |
| 3059 | atomic64_set(&counter->hw.prev_count, now); |
| 3060 | |
| 3061 | delta = now - prev; |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 3062 | |
| 3063 | atomic64_add(delta, &counter->count); |
| 3064 | } |
| 3065 | |
| 3066 | static void cpu_migrations_perf_counter_read(struct perf_counter *counter) |
| 3067 | { |
| 3068 | cpu_migrations_perf_counter_update(counter); |
| 3069 | } |
| 3070 | |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 3071 | static int cpu_migrations_perf_counter_enable(struct perf_counter *counter) |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 3072 | { |
Paul Mackerras | c07c99b | 2009-02-13 22:10:34 +1100 | [diff] [blame] | 3073 | if (counter->prev_state <= PERF_COUNTER_STATE_OFF) |
| 3074 | atomic64_set(&counter->hw.prev_count, |
| 3075 | get_cpu_migrations(counter)); |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 3076 | return 0; |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | static void cpu_migrations_perf_counter_disable(struct perf_counter *counter) |
| 3080 | { |
| 3081 | cpu_migrations_perf_counter_update(counter); |
| 3082 | } |
| 3083 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3084 | static const struct pmu perf_ops_cpu_migrations = { |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 3085 | .enable = cpu_migrations_perf_counter_enable, |
| 3086 | .disable = cpu_migrations_perf_counter_disable, |
| 3087 | .read = cpu_migrations_perf_counter_read, |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 3088 | }; |
| 3089 | |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 3090 | #ifdef CONFIG_EVENT_PROFILE |
| 3091 | void perf_tpcounter_event(int event_id) |
| 3092 | { |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 3093 | struct pt_regs *regs = get_irq_regs(); |
| 3094 | |
| 3095 | if (!regs) |
| 3096 | regs = task_pt_regs(current); |
| 3097 | |
Peter Zijlstra | 78f13e9 | 2009-04-08 15:01:33 +0200 | [diff] [blame] | 3098 | __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0); |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 3099 | } |
Steven Whitehouse | ff7b1b4 | 2009-04-15 16:55:05 +0100 | [diff] [blame] | 3100 | EXPORT_SYMBOL_GPL(perf_tpcounter_event); |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 3101 | |
| 3102 | extern int ftrace_profile_enable(int); |
| 3103 | extern void ftrace_profile_disable(int); |
| 3104 | |
| 3105 | static void tp_perf_counter_destroy(struct perf_counter *counter) |
| 3106 | { |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 3107 | ftrace_profile_disable(perf_event_id(&counter->hw_event)); |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 3108 | } |
| 3109 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3110 | static const struct pmu *tp_perf_counter_init(struct perf_counter *counter) |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 3111 | { |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 3112 | int event_id = perf_event_id(&counter->hw_event); |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 3113 | int ret; |
| 3114 | |
| 3115 | ret = ftrace_profile_enable(event_id); |
| 3116 | if (ret) |
| 3117 | return NULL; |
| 3118 | |
| 3119 | counter->destroy = tp_perf_counter_destroy; |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 3120 | counter->hw.irq_period = counter->hw_event.irq_period; |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 3121 | |
| 3122 | return &perf_ops_generic; |
| 3123 | } |
| 3124 | #else |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3125 | static const struct pmu *tp_perf_counter_init(struct perf_counter *counter) |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 3126 | { |
| 3127 | return NULL; |
| 3128 | } |
| 3129 | #endif |
| 3130 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3131 | static const struct pmu *sw_perf_counter_init(struct perf_counter *counter) |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 3132 | { |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3133 | const struct pmu *pmu = NULL; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 3134 | |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 3135 | /* |
| 3136 | * Software counters (currently) can't in general distinguish |
| 3137 | * between user, kernel and hypervisor events. |
| 3138 | * However, context switches and cpu migrations are considered |
| 3139 | * to be kernel events, and page faults are never hypervisor |
| 3140 | * events. |
| 3141 | */ |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 3142 | switch (perf_event_id(&counter->hw_event)) { |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 3143 | case PERF_COUNT_CPU_CLOCK: |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3144 | pmu = &perf_ops_cpu_clock; |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 3145 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 3146 | break; |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 3147 | case PERF_COUNT_TASK_CLOCK: |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 3148 | /* |
| 3149 | * If the user instantiates this as a per-cpu counter, |
| 3150 | * use the cpu_clock counter instead. |
| 3151 | */ |
| 3152 | if (counter->ctx->task) |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3153 | pmu = &perf_ops_task_clock; |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 3154 | else |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3155 | pmu = &perf_ops_cpu_clock; |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 3156 | |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 3157 | break; |
Ingo Molnar | e06c61a | 2008-12-14 14:44:31 +0100 | [diff] [blame] | 3158 | case PERF_COUNT_PAGE_FAULTS: |
Peter Zijlstra | ac17dc8 | 2009-03-13 12:21:34 +0100 | [diff] [blame] | 3159 | case PERF_COUNT_PAGE_FAULTS_MIN: |
| 3160 | case PERF_COUNT_PAGE_FAULTS_MAJ: |
Ingo Molnar | 5d6a27d | 2008-12-14 12:28:33 +0100 | [diff] [blame] | 3161 | case PERF_COUNT_CONTEXT_SWITCHES: |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3162 | pmu = &perf_ops_generic; |
Ingo Molnar | 5d6a27d | 2008-12-14 12:28:33 +0100 | [diff] [blame] | 3163 | break; |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 3164 | case PERF_COUNT_CPU_MIGRATIONS: |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 3165 | if (!counter->hw_event.exclude_kernel) |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3166 | pmu = &perf_ops_cpu_migrations; |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 3167 | break; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 3168 | } |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 3169 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3170 | return pmu; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 3171 | } |
| 3172 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3173 | /* |
| 3174 | * Allocate and initialize a counter structure |
| 3175 | */ |
| 3176 | static struct perf_counter * |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3177 | perf_counter_alloc(struct perf_counter_hw_event *hw_event, |
| 3178 | int cpu, |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 3179 | struct perf_counter_context *ctx, |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3180 | struct perf_counter *group_leader, |
| 3181 | gfp_t gfpflags) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3182 | { |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3183 | const struct pmu *pmu; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 3184 | struct perf_counter *counter; |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 3185 | struct hw_perf_counter *hwc; |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 3186 | long err; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3187 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3188 | counter = kzalloc(sizeof(*counter), gfpflags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3189 | if (!counter) |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 3190 | return ERR_PTR(-ENOMEM); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3191 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3192 | /* |
| 3193 | * Single counters are their own group leaders, with an |
| 3194 | * empty sibling list: |
| 3195 | */ |
| 3196 | if (!group_leader) |
| 3197 | group_leader = counter; |
| 3198 | |
Peter Zijlstra | fccc714 | 2009-05-23 18:28:56 +0200 | [diff] [blame] | 3199 | mutex_init(&counter->child_mutex); |
| 3200 | INIT_LIST_HEAD(&counter->child_list); |
| 3201 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3202 | INIT_LIST_HEAD(&counter->list_entry); |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 3203 | INIT_LIST_HEAD(&counter->event_entry); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3204 | INIT_LIST_HEAD(&counter->sibling_list); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3205 | init_waitqueue_head(&counter->waitq); |
| 3206 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 3207 | mutex_init(&counter->mmap_mutex); |
| 3208 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 3209 | counter->cpu = cpu; |
| 3210 | counter->hw_event = *hw_event; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3211 | counter->group_leader = group_leader; |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3212 | counter->pmu = NULL; |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 3213 | counter->ctx = ctx; |
Ingo Molnar | 329d876 | 2009-05-26 08:10:00 +0200 | [diff] [blame] | 3214 | counter->oncpu = -1; |
| 3215 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 3216 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Ingo Molnar | a86ed50 | 2008-12-17 00:43:10 +0100 | [diff] [blame] | 3217 | if (hw_event->disabled) |
| 3218 | counter->state = PERF_COUNTER_STATE_OFF; |
| 3219 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3220 | pmu = NULL; |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 3221 | |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 3222 | hwc = &counter->hw; |
| 3223 | if (hw_event->freq && hw_event->irq_freq) |
Peter Zijlstra | 2e569d3 | 2009-05-15 15:37:47 +0200 | [diff] [blame] | 3224 | hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq); |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 3225 | else |
| 3226 | hwc->irq_period = hw_event->irq_period; |
| 3227 | |
Peter Zijlstra | 2023b35 | 2009-05-05 17:50:26 +0200 | [diff] [blame] | 3228 | /* |
| 3229 | * we currently do not support PERF_RECORD_GROUP on inherited counters |
| 3230 | */ |
| 3231 | if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP)) |
| 3232 | goto done; |
| 3233 | |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 3234 | if (perf_event_raw(hw_event)) { |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3235 | pmu = hw_perf_counter_init(counter); |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 3236 | goto done; |
| 3237 | } |
| 3238 | |
| 3239 | switch (perf_event_type(hw_event)) { |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 3240 | case PERF_TYPE_HARDWARE: |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3241 | pmu = hw_perf_counter_init(counter); |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 3242 | break; |
| 3243 | |
| 3244 | case PERF_TYPE_SOFTWARE: |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3245 | pmu = sw_perf_counter_init(counter); |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 3246 | break; |
| 3247 | |
| 3248 | case PERF_TYPE_TRACEPOINT: |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3249 | pmu = tp_perf_counter_init(counter); |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 3250 | break; |
| 3251 | } |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 3252 | done: |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 3253 | err = 0; |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3254 | if (!pmu) |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 3255 | err = -EINVAL; |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3256 | else if (IS_ERR(pmu)) |
| 3257 | err = PTR_ERR(pmu); |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 3258 | |
| 3259 | if (err) { |
| 3260 | kfree(counter); |
| 3261 | return ERR_PTR(err); |
| 3262 | } |
| 3263 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 3264 | counter->pmu = pmu; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3265 | |
Peter Zijlstra | 7fc23a5 | 2009-05-08 18:52:21 +0200 | [diff] [blame] | 3266 | atomic_inc(&nr_counters); |
Peter Zijlstra | 9ee318a | 2009-04-09 10:53:44 +0200 | [diff] [blame] | 3267 | if (counter->hw_event.mmap) |
| 3268 | atomic_inc(&nr_mmap_tracking); |
| 3269 | if (counter->hw_event.munmap) |
| 3270 | atomic_inc(&nr_munmap_tracking); |
| 3271 | if (counter->hw_event.comm) |
| 3272 | atomic_inc(&nr_comm_tracking); |
| 3273 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3274 | return counter; |
| 3275 | } |
| 3276 | |
| 3277 | /** |
Paul Mackerras | 2743a5b | 2009-03-04 20:36:51 +1100 | [diff] [blame] | 3278 | * sys_perf_counter_open - open a performance counter, associate it to a task/cpu |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 3279 | * |
| 3280 | * @hw_event_uptr: event type attributes for monitoring/sampling |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3281 | * @pid: target pid |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 3282 | * @cpu: target cpu |
| 3283 | * @group_fd: group leader counter fd |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3284 | */ |
Paul Mackerras | 2743a5b | 2009-03-04 20:36:51 +1100 | [diff] [blame] | 3285 | SYSCALL_DEFINE5(perf_counter_open, |
Paul Mackerras | f3dfd26 | 2009-02-26 22:43:46 +1100 | [diff] [blame] | 3286 | const struct perf_counter_hw_event __user *, hw_event_uptr, |
Paul Mackerras | 2743a5b | 2009-03-04 20:36:51 +1100 | [diff] [blame] | 3287 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3288 | { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3289 | struct perf_counter *counter, *group_leader; |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 3290 | struct perf_counter_hw_event hw_event; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3291 | struct perf_counter_context *ctx; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3292 | struct file *counter_file = NULL; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3293 | struct file *group_file = NULL; |
| 3294 | int fput_needed = 0; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3295 | int fput_needed2 = 0; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3296 | int ret; |
| 3297 | |
Paul Mackerras | 2743a5b | 2009-03-04 20:36:51 +1100 | [diff] [blame] | 3298 | /* for future expandability... */ |
| 3299 | if (flags) |
| 3300 | return -EINVAL; |
| 3301 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 3302 | if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0) |
Thomas Gleixner | eab656a | 2008-12-08 19:26:59 +0100 | [diff] [blame] | 3303 | return -EFAULT; |
| 3304 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3305 | /* |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 3306 | * Get the target context (task or percpu): |
| 3307 | */ |
| 3308 | ctx = find_get_context(pid, cpu); |
| 3309 | if (IS_ERR(ctx)) |
| 3310 | return PTR_ERR(ctx); |
| 3311 | |
| 3312 | /* |
| 3313 | * Look up the group leader (we will attach this counter to it): |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3314 | */ |
| 3315 | group_leader = NULL; |
| 3316 | if (group_fd != -1) { |
| 3317 | ret = -EINVAL; |
| 3318 | group_file = fget_light(group_fd, &fput_needed); |
| 3319 | if (!group_file) |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 3320 | goto err_put_context; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3321 | if (group_file->f_op != &perf_fops) |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 3322 | goto err_put_context; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3323 | |
| 3324 | group_leader = group_file->private_data; |
| 3325 | /* |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 3326 | * Do not allow a recursive hierarchy (this new sibling |
| 3327 | * becoming part of another group-sibling): |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3328 | */ |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 3329 | if (group_leader->group_leader != group_leader) |
| 3330 | goto err_put_context; |
| 3331 | /* |
| 3332 | * Do not allow to attach to a group in a different |
| 3333 | * task or CPU context: |
| 3334 | */ |
| 3335 | if (group_leader->ctx != ctx) |
| 3336 | goto err_put_context; |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 3337 | /* |
| 3338 | * Only a group leader can be exclusive or pinned |
| 3339 | */ |
| 3340 | if (hw_event.exclusive || hw_event.pinned) |
| 3341 | goto err_put_context; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3342 | } |
| 3343 | |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 3344 | counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader, |
| 3345 | GFP_KERNEL); |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 3346 | ret = PTR_ERR(counter); |
| 3347 | if (IS_ERR(counter)) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3348 | goto err_put_context; |
| 3349 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3350 | ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0); |
| 3351 | if (ret < 0) |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3352 | goto err_free_put_context; |
| 3353 | |
| 3354 | counter_file = fget_light(ret, &fput_needed2); |
| 3355 | if (!counter_file) |
| 3356 | goto err_free_put_context; |
| 3357 | |
| 3358 | counter->filp = counter_file; |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 3359 | WARN_ON_ONCE(ctx->parent_ctx); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3360 | mutex_lock(&ctx->mutex); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3361 | perf_install_in_context(ctx, counter, cpu); |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 3362 | ++ctx->generation; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3363 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3364 | |
Peter Zijlstra | 082ff5a | 2009-05-23 18:29:00 +0200 | [diff] [blame] | 3365 | counter->owner = current; |
| 3366 | get_task_struct(current); |
| 3367 | mutex_lock(¤t->perf_counter_mutex); |
| 3368 | list_add_tail(&counter->owner_entry, ¤t->perf_counter_list); |
| 3369 | mutex_unlock(¤t->perf_counter_mutex); |
| 3370 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3371 | fput_light(counter_file, fput_needed2); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3372 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3373 | out_fput: |
| 3374 | fput_light(group_file, fput_needed); |
| 3375 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3376 | return ret; |
| 3377 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3378 | err_free_put_context: |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3379 | kfree(counter); |
| 3380 | |
| 3381 | err_put_context: |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 3382 | put_ctx(ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3383 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3384 | goto out_fput; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3385 | } |
| 3386 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3387 | /* |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3388 | * inherit a counter from parent task to child task: |
| 3389 | */ |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3390 | static struct perf_counter * |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3391 | inherit_counter(struct perf_counter *parent_counter, |
| 3392 | struct task_struct *parent, |
| 3393 | struct perf_counter_context *parent_ctx, |
| 3394 | struct task_struct *child, |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3395 | struct perf_counter *group_leader, |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3396 | struct perf_counter_context *child_ctx) |
| 3397 | { |
| 3398 | struct perf_counter *child_counter; |
| 3399 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3400 | /* |
| 3401 | * Instead of creating recursive hierarchies of counters, |
| 3402 | * we link inherited counters back to the original parent, |
| 3403 | * which has a filp for sure, which we use as the reference |
| 3404 | * count: |
| 3405 | */ |
| 3406 | if (parent_counter->parent) |
| 3407 | parent_counter = parent_counter->parent; |
| 3408 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3409 | child_counter = perf_counter_alloc(&parent_counter->hw_event, |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 3410 | parent_counter->cpu, child_ctx, |
| 3411 | group_leader, GFP_KERNEL); |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 3412 | if (IS_ERR(child_counter)) |
| 3413 | return child_counter; |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 3414 | get_ctx(child_ctx); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3415 | |
| 3416 | /* |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 3417 | * Make the child state follow the state of the parent counter, |
| 3418 | * not its hw_event.disabled bit. We hold the parent's mutex, |
| 3419 | * so we won't race with perf_counter_{en,dis}able_family. |
| 3420 | */ |
| 3421 | if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE) |
| 3422 | child_counter->state = PERF_COUNTER_STATE_INACTIVE; |
| 3423 | else |
| 3424 | child_counter->state = PERF_COUNTER_STATE_OFF; |
| 3425 | |
| 3426 | /* |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3427 | * Link it up in the child's context: |
| 3428 | */ |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 3429 | add_counter_to_ctx(child_counter, child_ctx); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3430 | |
| 3431 | child_counter->parent = parent_counter; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3432 | /* |
| 3433 | * inherit into child's child as well: |
| 3434 | */ |
| 3435 | child_counter->hw_event.inherit = 1; |
| 3436 | |
| 3437 | /* |
| 3438 | * Get a reference to the parent filp - we will fput it |
| 3439 | * when the child counter exits. This is safe to do because |
| 3440 | * we are in the parent and we know that the filp still |
| 3441 | * exists and has a nonzero count: |
| 3442 | */ |
| 3443 | atomic_long_inc(&parent_counter->filp->f_count); |
| 3444 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3445 | /* |
| 3446 | * Link this into the parent counter's child list |
| 3447 | */ |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 3448 | WARN_ON_ONCE(parent_counter->ctx->parent_ctx); |
Peter Zijlstra | fccc714 | 2009-05-23 18:28:56 +0200 | [diff] [blame] | 3449 | mutex_lock(&parent_counter->child_mutex); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3450 | list_add_tail(&child_counter->child_list, &parent_counter->child_list); |
Peter Zijlstra | fccc714 | 2009-05-23 18:28:56 +0200 | [diff] [blame] | 3451 | mutex_unlock(&parent_counter->child_mutex); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3452 | |
| 3453 | return child_counter; |
| 3454 | } |
| 3455 | |
| 3456 | static int inherit_group(struct perf_counter *parent_counter, |
| 3457 | struct task_struct *parent, |
| 3458 | struct perf_counter_context *parent_ctx, |
| 3459 | struct task_struct *child, |
| 3460 | struct perf_counter_context *child_ctx) |
| 3461 | { |
| 3462 | struct perf_counter *leader; |
| 3463 | struct perf_counter *sub; |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 3464 | struct perf_counter *child_ctr; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3465 | |
| 3466 | leader = inherit_counter(parent_counter, parent, parent_ctx, |
| 3467 | child, NULL, child_ctx); |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 3468 | if (IS_ERR(leader)) |
| 3469 | return PTR_ERR(leader); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3470 | list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) { |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 3471 | child_ctr = inherit_counter(sub, parent, parent_ctx, |
| 3472 | child, leader, child_ctx); |
| 3473 | if (IS_ERR(child_ctr)) |
| 3474 | return PTR_ERR(child_ctr); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3475 | } |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3476 | return 0; |
| 3477 | } |
| 3478 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3479 | static void sync_child_counter(struct perf_counter *child_counter, |
| 3480 | struct perf_counter *parent_counter) |
| 3481 | { |
Peter Zijlstra | 8bc2095 | 2009-05-15 20:45:59 +0200 | [diff] [blame] | 3482 | u64 child_val; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3483 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3484 | child_val = atomic64_read(&child_counter->count); |
| 3485 | |
| 3486 | /* |
| 3487 | * Add back the child's count to the parent's count: |
| 3488 | */ |
| 3489 | atomic64_add(child_val, &parent_counter->count); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 3490 | atomic64_add(child_counter->total_time_enabled, |
| 3491 | &parent_counter->child_total_time_enabled); |
| 3492 | atomic64_add(child_counter->total_time_running, |
| 3493 | &parent_counter->child_total_time_running); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3494 | |
| 3495 | /* |
| 3496 | * Remove this counter from the parent's list |
| 3497 | */ |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 3498 | WARN_ON_ONCE(parent_counter->ctx->parent_ctx); |
Peter Zijlstra | fccc714 | 2009-05-23 18:28:56 +0200 | [diff] [blame] | 3499 | mutex_lock(&parent_counter->child_mutex); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3500 | list_del_init(&child_counter->child_list); |
Peter Zijlstra | fccc714 | 2009-05-23 18:28:56 +0200 | [diff] [blame] | 3501 | mutex_unlock(&parent_counter->child_mutex); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3502 | |
| 3503 | /* |
| 3504 | * Release the parent counter, if this was the last |
| 3505 | * reference to it. |
| 3506 | */ |
| 3507 | fput(parent_counter->filp); |
| 3508 | } |
| 3509 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3510 | static void |
| 3511 | __perf_counter_exit_task(struct task_struct *child, |
| 3512 | struct perf_counter *child_counter, |
| 3513 | struct perf_counter_context *child_ctx) |
| 3514 | { |
| 3515 | struct perf_counter *parent_counter; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3516 | |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 3517 | update_counter_times(child_counter); |
Peter Zijlstra | aa9c67f | 2009-05-23 18:28:59 +0200 | [diff] [blame] | 3518 | perf_counter_remove_from_context(child_counter); |
Ingo Molnar | 0cc0c02 | 2008-12-14 23:20:36 +0100 | [diff] [blame] | 3519 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3520 | parent_counter = child_counter->parent; |
| 3521 | /* |
| 3522 | * It can happen that parent exits first, and has counters |
| 3523 | * that are still around due to the child reference. These |
| 3524 | * counters need to be zapped - but otherwise linger. |
| 3525 | */ |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3526 | if (parent_counter) { |
| 3527 | sync_child_counter(child_counter, parent_counter); |
Peter Zijlstra | f160095 | 2009-03-19 20:26:16 +0100 | [diff] [blame] | 3528 | free_counter(child_counter); |
Paul Mackerras | 4bcf349 | 2009-02-11 13:53:19 +0100 | [diff] [blame] | 3529 | } |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | /* |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3533 | * When a child task exits, feed back counter values to parent counters. |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3534 | */ |
| 3535 | void perf_counter_exit_task(struct task_struct *child) |
| 3536 | { |
| 3537 | struct perf_counter *child_counter, *tmp; |
| 3538 | struct perf_counter_context *child_ctx; |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 3539 | unsigned long flags; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3540 | |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 3541 | if (likely(!child->perf_counter_ctxp)) |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3542 | return; |
| 3543 | |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 3544 | local_irq_save(flags); |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 3545 | /* |
| 3546 | * We can't reschedule here because interrupts are disabled, |
| 3547 | * and either child is current or it is a task that can't be |
| 3548 | * scheduled, so we are now safe from rescheduling changing |
| 3549 | * our context. |
| 3550 | */ |
| 3551 | child_ctx = child->perf_counter_ctxp; |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 3552 | __perf_counter_task_sched_out(child_ctx); |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 3553 | |
| 3554 | /* |
| 3555 | * Take the context lock here so that if find_get_context is |
| 3556 | * reading child->perf_counter_ctxp, we wait until it has |
| 3557 | * incremented the context's refcount before we do put_ctx below. |
| 3558 | */ |
| 3559 | spin_lock(&child_ctx->lock); |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 3560 | child->perf_counter_ctxp = NULL; |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 3561 | if (child_ctx->parent_ctx) { |
| 3562 | /* |
| 3563 | * This context is a clone; unclone it so it can't get |
| 3564 | * swapped to another process while we're removing all |
| 3565 | * the counters from it. |
| 3566 | */ |
| 3567 | put_ctx(child_ctx->parent_ctx); |
| 3568 | child_ctx->parent_ctx = NULL; |
| 3569 | } |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 3570 | spin_unlock(&child_ctx->lock); |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 3571 | local_irq_restore(flags); |
| 3572 | |
| 3573 | mutex_lock(&child_ctx->mutex); |
| 3574 | |
Peter Zijlstra | 8bc2095 | 2009-05-15 20:45:59 +0200 | [diff] [blame] | 3575 | again: |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3576 | list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list, |
| 3577 | list_entry) |
| 3578 | __perf_counter_exit_task(child, child_counter, child_ctx); |
Peter Zijlstra | 8bc2095 | 2009-05-15 20:45:59 +0200 | [diff] [blame] | 3579 | |
| 3580 | /* |
| 3581 | * If the last counter was a group counter, it will have appended all |
| 3582 | * its siblings to the list, but we obtained 'tmp' before that which |
| 3583 | * will still point to the list head terminating the iteration. |
| 3584 | */ |
| 3585 | if (!list_empty(&child_ctx->counter_list)) |
| 3586 | goto again; |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 3587 | |
| 3588 | mutex_unlock(&child_ctx->mutex); |
| 3589 | |
| 3590 | put_ctx(child_ctx); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | /* |
| 3594 | * Initialize the perf_counter context in task_struct |
| 3595 | */ |
Peter Zijlstra | 6ab423e | 2009-05-25 14:45:27 +0200 | [diff] [blame] | 3596 | int perf_counter_init_task(struct task_struct *child) |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3597 | { |
| 3598 | struct perf_counter_context *child_ctx, *parent_ctx; |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 3599 | struct perf_counter_context *cloned_ctx; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3600 | struct perf_counter *counter; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3601 | struct task_struct *parent = current; |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 3602 | int inherited_all = 1; |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 3603 | u64 cloned_gen; |
Peter Zijlstra | 6ab423e | 2009-05-25 14:45:27 +0200 | [diff] [blame] | 3604 | int ret = 0; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3605 | |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 3606 | child->perf_counter_ctxp = NULL; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3607 | |
Peter Zijlstra | 082ff5a | 2009-05-23 18:29:00 +0200 | [diff] [blame] | 3608 | mutex_init(&child->perf_counter_mutex); |
| 3609 | INIT_LIST_HEAD(&child->perf_counter_list); |
| 3610 | |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 3611 | if (likely(!parent->perf_counter_ctxp)) |
Peter Zijlstra | 6ab423e | 2009-05-25 14:45:27 +0200 | [diff] [blame] | 3612 | return 0; |
| 3613 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3614 | /* |
| 3615 | * This is executed from the parent task context, so inherit |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 3616 | * counters that have been marked for cloning. |
| 3617 | * First allocate and initialize a context for the child. |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3618 | */ |
| 3619 | |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 3620 | child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL); |
| 3621 | if (!child_ctx) |
Peter Zijlstra | 6ab423e | 2009-05-25 14:45:27 +0200 | [diff] [blame] | 3622 | return -ENOMEM; |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 3623 | |
| 3624 | __perf_counter_init_context(child_ctx, child); |
| 3625 | child->perf_counter_ctxp = child_ctx; |
Paul Mackerras | c93f766 | 2009-05-28 22:18:17 +1000 | [diff] [blame] | 3626 | get_task_struct(child); |
Paul Mackerras | a63eaf3 | 2009-05-22 14:17:31 +1000 | [diff] [blame] | 3627 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3628 | /* |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 3629 | * If the parent's context is a clone, temporarily set its |
| 3630 | * parent_gen to an impossible value (all 1s) so it won't get |
| 3631 | * swapped under us. The rcu_read_lock makes sure that |
| 3632 | * parent_ctx continues to exist even if it gets swapped to |
| 3633 | * another process and then freed while we are trying to get |
| 3634 | * its lock. |
| 3635 | */ |
| 3636 | rcu_read_lock(); |
| 3637 | retry: |
| 3638 | parent_ctx = rcu_dereference(parent->perf_counter_ctxp); |
| 3639 | /* |
| 3640 | * No need to check if parent_ctx != NULL here; since we saw |
| 3641 | * it non-NULL earlier, the only reason for it to become NULL |
| 3642 | * is if we exit, and since we're currently in the middle of |
| 3643 | * a fork we can't be exiting at the same time. |
| 3644 | */ |
| 3645 | spin_lock_irq(&parent_ctx->lock); |
| 3646 | if (parent_ctx != rcu_dereference(parent->perf_counter_ctxp)) { |
| 3647 | spin_unlock_irq(&parent_ctx->lock); |
| 3648 | goto retry; |
| 3649 | } |
| 3650 | cloned_gen = parent_ctx->parent_gen; |
| 3651 | if (parent_ctx->parent_ctx) |
| 3652 | parent_ctx->parent_gen = ~0ull; |
| 3653 | spin_unlock_irq(&parent_ctx->lock); |
| 3654 | rcu_read_unlock(); |
| 3655 | |
| 3656 | /* |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3657 | * Lock the parent list. No need to lock the child - not PID |
| 3658 | * hashed yet and not running, so nobody can access it. |
| 3659 | */ |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3660 | mutex_lock(&parent_ctx->mutex); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3661 | |
| 3662 | /* |
| 3663 | * We dont have to disable NMIs - we are only looking at |
| 3664 | * the list, not manipulating it: |
| 3665 | */ |
Peter Zijlstra | d7b629a | 2009-05-20 12:21:19 +0200 | [diff] [blame] | 3666 | list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) { |
| 3667 | if (counter != counter->group_leader) |
| 3668 | continue; |
| 3669 | |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 3670 | if (!counter->hw_event.inherit) { |
| 3671 | inherited_all = 0; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3672 | continue; |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 3673 | } |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3674 | |
Peter Zijlstra | 6ab423e | 2009-05-25 14:45:27 +0200 | [diff] [blame] | 3675 | ret = inherit_group(counter, parent, parent_ctx, |
| 3676 | child, child_ctx); |
| 3677 | if (ret) { |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 3678 | inherited_all = 0; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3679 | break; |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 3680 | } |
| 3681 | } |
| 3682 | |
| 3683 | if (inherited_all) { |
| 3684 | /* |
| 3685 | * Mark the child context as a clone of the parent |
| 3686 | * context, or of whatever the parent is a clone of. |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 3687 | * Note that if the parent is a clone, it could get |
| 3688 | * uncloned at any point, but that doesn't matter |
| 3689 | * because the list of counters and the generation |
| 3690 | * count can't have changed since we took the mutex. |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 3691 | */ |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 3692 | cloned_ctx = rcu_dereference(parent_ctx->parent_ctx); |
| 3693 | if (cloned_ctx) { |
| 3694 | child_ctx->parent_ctx = cloned_ctx; |
| 3695 | child_ctx->parent_gen = cloned_gen; |
Paul Mackerras | 564c2b2 | 2009-05-22 14:27:22 +1000 | [diff] [blame] | 3696 | } else { |
| 3697 | child_ctx->parent_ctx = parent_ctx; |
| 3698 | child_ctx->parent_gen = parent_ctx->generation; |
| 3699 | } |
| 3700 | get_ctx(child_ctx->parent_ctx); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3701 | } |
| 3702 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3703 | mutex_unlock(&parent_ctx->mutex); |
Peter Zijlstra | 6ab423e | 2009-05-25 14:45:27 +0200 | [diff] [blame] | 3704 | |
Paul Mackerras | ad3a37d | 2009-05-29 16:06:20 +1000 | [diff] [blame^] | 3705 | /* |
| 3706 | * Restore the clone status of the parent. |
| 3707 | */ |
| 3708 | if (parent_ctx->parent_ctx) { |
| 3709 | spin_lock_irq(&parent_ctx->lock); |
| 3710 | if (parent_ctx->parent_ctx) |
| 3711 | parent_ctx->parent_gen = cloned_gen; |
| 3712 | spin_unlock_irq(&parent_ctx->lock); |
| 3713 | } |
| 3714 | |
Peter Zijlstra | 6ab423e | 2009-05-25 14:45:27 +0200 | [diff] [blame] | 3715 | return ret; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 3716 | } |
| 3717 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3718 | static void __cpuinit perf_counter_init_cpu(int cpu) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3719 | { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3720 | struct perf_cpu_context *cpuctx; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3721 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3722 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 3723 | __perf_counter_init_context(&cpuctx->ctx, NULL); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3724 | |
Ingo Molnar | 1dce8d9 | 2009-05-04 19:23:18 +0200 | [diff] [blame] | 3725 | spin_lock(&perf_resource_lock); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3726 | cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu; |
Ingo Molnar | 1dce8d9 | 2009-05-04 19:23:18 +0200 | [diff] [blame] | 3727 | spin_unlock(&perf_resource_lock); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3728 | |
Paul Mackerras | 01d0287 | 2009-01-14 13:44:19 +1100 | [diff] [blame] | 3729 | hw_perf_counter_setup(cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3730 | } |
| 3731 | |
| 3732 | #ifdef CONFIG_HOTPLUG_CPU |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3733 | static void __perf_counter_exit_cpu(void *info) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3734 | { |
| 3735 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 3736 | struct perf_counter_context *ctx = &cpuctx->ctx; |
| 3737 | struct perf_counter *counter, *tmp; |
| 3738 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3739 | list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) |
| 3740 | __perf_counter_remove_from_context(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3741 | } |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3742 | static void perf_counter_exit_cpu(int cpu) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3743 | { |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3744 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 3745 | struct perf_counter_context *ctx = &cpuctx->ctx; |
| 3746 | |
| 3747 | mutex_lock(&ctx->mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3748 | smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 3749 | mutex_unlock(&ctx->mutex); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3750 | } |
| 3751 | #else |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3752 | static inline void perf_counter_exit_cpu(int cpu) { } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3753 | #endif |
| 3754 | |
| 3755 | static int __cpuinit |
| 3756 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
| 3757 | { |
| 3758 | unsigned int cpu = (long)hcpu; |
| 3759 | |
| 3760 | switch (action) { |
| 3761 | |
| 3762 | case CPU_UP_PREPARE: |
| 3763 | case CPU_UP_PREPARE_FROZEN: |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3764 | perf_counter_init_cpu(cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3765 | break; |
| 3766 | |
| 3767 | case CPU_DOWN_PREPARE: |
| 3768 | case CPU_DOWN_PREPARE_FROZEN: |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 3769 | perf_counter_exit_cpu(cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3770 | break; |
| 3771 | |
| 3772 | default: |
| 3773 | break; |
| 3774 | } |
| 3775 | |
| 3776 | return NOTIFY_OK; |
| 3777 | } |
| 3778 | |
| 3779 | static struct notifier_block __cpuinitdata perf_cpu_nb = { |
| 3780 | .notifier_call = perf_cpu_notify, |
| 3781 | }; |
| 3782 | |
Ingo Molnar | 0d905bc | 2009-05-04 19:13:30 +0200 | [diff] [blame] | 3783 | void __init perf_counter_init(void) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3784 | { |
| 3785 | perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE, |
| 3786 | (void *)(long)smp_processor_id()); |
| 3787 | register_cpu_notifier(&perf_cpu_nb); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3788 | } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3789 | |
| 3790 | static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf) |
| 3791 | { |
| 3792 | return sprintf(buf, "%d\n", perf_reserved_percpu); |
| 3793 | } |
| 3794 | |
| 3795 | static ssize_t |
| 3796 | perf_set_reserve_percpu(struct sysdev_class *class, |
| 3797 | const char *buf, |
| 3798 | size_t count) |
| 3799 | { |
| 3800 | struct perf_cpu_context *cpuctx; |
| 3801 | unsigned long val; |
| 3802 | int err, cpu, mpt; |
| 3803 | |
| 3804 | err = strict_strtoul(buf, 10, &val); |
| 3805 | if (err) |
| 3806 | return err; |
| 3807 | if (val > perf_max_counters) |
| 3808 | return -EINVAL; |
| 3809 | |
Ingo Molnar | 1dce8d9 | 2009-05-04 19:23:18 +0200 | [diff] [blame] | 3810 | spin_lock(&perf_resource_lock); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3811 | perf_reserved_percpu = val; |
| 3812 | for_each_online_cpu(cpu) { |
| 3813 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 3814 | spin_lock_irq(&cpuctx->ctx.lock); |
| 3815 | mpt = min(perf_max_counters - cpuctx->ctx.nr_counters, |
| 3816 | perf_max_counters - perf_reserved_percpu); |
| 3817 | cpuctx->max_pertask = mpt; |
| 3818 | spin_unlock_irq(&cpuctx->ctx.lock); |
| 3819 | } |
Ingo Molnar | 1dce8d9 | 2009-05-04 19:23:18 +0200 | [diff] [blame] | 3820 | spin_unlock(&perf_resource_lock); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3821 | |
| 3822 | return count; |
| 3823 | } |
| 3824 | |
| 3825 | static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf) |
| 3826 | { |
| 3827 | return sprintf(buf, "%d\n", perf_overcommit); |
| 3828 | } |
| 3829 | |
| 3830 | static ssize_t |
| 3831 | perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count) |
| 3832 | { |
| 3833 | unsigned long val; |
| 3834 | int err; |
| 3835 | |
| 3836 | err = strict_strtoul(buf, 10, &val); |
| 3837 | if (err) |
| 3838 | return err; |
| 3839 | if (val > 1) |
| 3840 | return -EINVAL; |
| 3841 | |
Ingo Molnar | 1dce8d9 | 2009-05-04 19:23:18 +0200 | [diff] [blame] | 3842 | spin_lock(&perf_resource_lock); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3843 | perf_overcommit = val; |
Ingo Molnar | 1dce8d9 | 2009-05-04 19:23:18 +0200 | [diff] [blame] | 3844 | spin_unlock(&perf_resource_lock); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 3845 | |
| 3846 | return count; |
| 3847 | } |
| 3848 | |
| 3849 | static SYSDEV_CLASS_ATTR( |
| 3850 | reserve_percpu, |
| 3851 | 0644, |
| 3852 | perf_show_reserve_percpu, |
| 3853 | perf_set_reserve_percpu |
| 3854 | ); |
| 3855 | |
| 3856 | static SYSDEV_CLASS_ATTR( |
| 3857 | overcommit, |
| 3858 | 0644, |
| 3859 | perf_show_overcommit, |
| 3860 | perf_set_overcommit |
| 3861 | ); |
| 3862 | |
| 3863 | static struct attribute *perfclass_attrs[] = { |
| 3864 | &attr_reserve_percpu.attr, |
| 3865 | &attr_overcommit.attr, |
| 3866 | NULL |
| 3867 | }; |
| 3868 | |
| 3869 | static struct attribute_group perfclass_attr_group = { |
| 3870 | .attrs = perfclass_attrs, |
| 3871 | .name = "perf_counters", |
| 3872 | }; |
| 3873 | |
| 3874 | static int __init perf_counter_sysfs_init(void) |
| 3875 | { |
| 3876 | return sysfs_create_group(&cpu_sysdev_class.kset.kobj, |
| 3877 | &perfclass_attr_group); |
| 3878 | } |
| 3879 | device_initcall(perf_counter_sysfs_init); |