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