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