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