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