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