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