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