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