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> |
| 28 | |
Tim Blechmann | 4e193bd | 2009-03-14 14:29:25 +0100 | [diff] [blame] | 29 | #include <asm/irq_regs.h> |
| 30 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 31 | /* |
| 32 | * Each CPU has a list of per CPU counters: |
| 33 | */ |
| 34 | DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context); |
| 35 | |
Ingo Molnar | 088e285 | 2008-12-14 20:21:00 +0100 | [diff] [blame] | 36 | int perf_max_counters __read_mostly = 1; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 37 | static int perf_reserved_percpu __read_mostly; |
| 38 | static int perf_overcommit __read_mostly = 1; |
| 39 | |
| 40 | /* |
| 41 | * Mutex for (sysadmin-configurable) counter reservations: |
| 42 | */ |
| 43 | static DEFINE_MUTEX(perf_resource_mutex); |
| 44 | |
| 45 | /* |
| 46 | * Architecture provided APIs - weak aliases: |
| 47 | */ |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 48 | extern __weak const struct hw_perf_counter_ops * |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 49 | hw_perf_counter_init(struct perf_counter *counter) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 50 | { |
Paul Mackerras | ff6f054 | 2009-01-09 16:19:25 +1100 | [diff] [blame] | 51 | return NULL; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 54 | u64 __weak hw_perf_save_disable(void) { return 0; } |
Yinghai Lu | 01ea1cc | 2008-12-26 21:05:06 -0800 | [diff] [blame] | 55 | void __weak hw_perf_restore(u64 ctrl) { barrier(); } |
Paul Mackerras | 01d0287 | 2009-01-14 13:44:19 +1100 | [diff] [blame] | 56 | void __weak hw_perf_counter_setup(int cpu) { barrier(); } |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 57 | int __weak hw_perf_group_sched_in(struct perf_counter *group_leader, |
| 58 | struct perf_cpu_context *cpuctx, |
| 59 | struct perf_counter_context *ctx, int cpu) |
| 60 | { |
| 61 | return 0; |
| 62 | } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 63 | |
Paul Mackerras | 4eb96fc | 2009-01-09 17:24:34 +1100 | [diff] [blame] | 64 | void __weak perf_counter_print_debug(void) { } |
| 65 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 66 | static void |
| 67 | list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx) |
| 68 | { |
| 69 | struct perf_counter *group_leader = counter->group_leader; |
| 70 | |
| 71 | /* |
| 72 | * Depending on whether it is a standalone or sibling counter, |
| 73 | * add it straight to the context's counter list, or to the group |
| 74 | * leader's sibling list: |
| 75 | */ |
| 76 | if (counter->group_leader == counter) |
| 77 | list_add_tail(&counter->list_entry, &ctx->counter_list); |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 78 | else { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 79 | list_add_tail(&counter->list_entry, &group_leader->sibling_list); |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 80 | group_leader->nr_siblings++; |
| 81 | } |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 82 | |
| 83 | list_add_rcu(&counter->event_entry, &ctx->event_list); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static void |
| 87 | list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx) |
| 88 | { |
| 89 | struct perf_counter *sibling, *tmp; |
| 90 | |
| 91 | list_del_init(&counter->list_entry); |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 92 | list_del_rcu(&counter->event_entry); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 93 | |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 94 | if (counter->group_leader != counter) |
| 95 | counter->group_leader->nr_siblings--; |
| 96 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 97 | /* |
| 98 | * If this was a group counter with sibling counters then |
| 99 | * upgrade the siblings to singleton counters by adding them |
| 100 | * to the context list directly: |
| 101 | */ |
| 102 | list_for_each_entry_safe(sibling, tmp, |
| 103 | &counter->sibling_list, list_entry) { |
| 104 | |
Peter Zijlstra | 7556423 | 2009-03-13 12:21:29 +0100 | [diff] [blame] | 105 | list_move_tail(&sibling->list_entry, &ctx->counter_list); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 106 | sibling->group_leader = sibling; |
| 107 | } |
| 108 | } |
| 109 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 110 | static void |
| 111 | counter_sched_out(struct perf_counter *counter, |
| 112 | struct perf_cpu_context *cpuctx, |
| 113 | struct perf_counter_context *ctx) |
| 114 | { |
| 115 | if (counter->state != PERF_COUNTER_STATE_ACTIVE) |
| 116 | return; |
| 117 | |
| 118 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 119 | counter->tstamp_stopped = ctx->time_now; |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 120 | counter->hw_ops->disable(counter); |
| 121 | counter->oncpu = -1; |
| 122 | |
| 123 | if (!is_software_counter(counter)) |
| 124 | cpuctx->active_oncpu--; |
| 125 | ctx->nr_active--; |
| 126 | if (counter->hw_event.exclusive || !cpuctx->active_oncpu) |
| 127 | cpuctx->exclusive = 0; |
| 128 | } |
| 129 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 130 | static void |
| 131 | group_sched_out(struct perf_counter *group_counter, |
| 132 | struct perf_cpu_context *cpuctx, |
| 133 | struct perf_counter_context *ctx) |
| 134 | { |
| 135 | struct perf_counter *counter; |
| 136 | |
| 137 | if (group_counter->state != PERF_COUNTER_STATE_ACTIVE) |
| 138 | return; |
| 139 | |
| 140 | counter_sched_out(group_counter, cpuctx, ctx); |
| 141 | |
| 142 | /* |
| 143 | * Schedule out siblings (if any): |
| 144 | */ |
| 145 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) |
| 146 | counter_sched_out(counter, cpuctx, ctx); |
| 147 | |
| 148 | if (group_counter->hw_event.exclusive) |
| 149 | cpuctx->exclusive = 0; |
| 150 | } |
| 151 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 152 | /* |
| 153 | * Cross CPU call to remove a performance counter |
| 154 | * |
| 155 | * We disable the counter on the hardware level first. After that we |
| 156 | * remove it from the context list. |
| 157 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 158 | static void __perf_counter_remove_from_context(void *info) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 159 | { |
| 160 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 161 | struct perf_counter *counter = info; |
| 162 | struct perf_counter_context *ctx = counter->ctx; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 163 | unsigned long flags; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 164 | u64 perf_flags; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * If this is a task context, we need to check whether it is |
| 168 | * the current task context of this cpu. If not it has been |
| 169 | * scheduled out before the smp call arrived. |
| 170 | */ |
| 171 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 172 | return; |
| 173 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 174 | curr_rq_lock_irq_save(&flags); |
| 175 | spin_lock(&ctx->lock); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 176 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 177 | counter_sched_out(counter, cpuctx, ctx); |
| 178 | |
| 179 | counter->task = NULL; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 180 | ctx->nr_counters--; |
| 181 | |
| 182 | /* |
| 183 | * Protect the list operation against NMI by disabling the |
| 184 | * counters on a global level. NOP for non NMI based counters. |
| 185 | */ |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 186 | perf_flags = hw_perf_save_disable(); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 187 | list_del_counter(counter, ctx); |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 188 | hw_perf_restore(perf_flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 189 | |
| 190 | if (!ctx->task) { |
| 191 | /* |
| 192 | * Allow more per task counters with respect to the |
| 193 | * reservation: |
| 194 | */ |
| 195 | cpuctx->max_pertask = |
| 196 | min(perf_max_counters - ctx->nr_counters, |
| 197 | perf_max_counters - perf_reserved_percpu); |
| 198 | } |
| 199 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 200 | spin_unlock(&ctx->lock); |
| 201 | curr_rq_unlock_irq_restore(&flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
| 205 | /* |
| 206 | * Remove the counter from a task's (or a CPU's) list of counters. |
| 207 | * |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 208 | * Must be called with counter->mutex and ctx->mutex held. |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 209 | * |
| 210 | * CPU counters are removed with a smp call. For task counters we only |
| 211 | * call when the task is on a CPU. |
| 212 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 213 | static void perf_counter_remove_from_context(struct perf_counter *counter) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 214 | { |
| 215 | struct perf_counter_context *ctx = counter->ctx; |
| 216 | struct task_struct *task = ctx->task; |
| 217 | |
| 218 | if (!task) { |
| 219 | /* |
| 220 | * Per cpu counters are removed via an smp call and |
| 221 | * the removal is always sucessful. |
| 222 | */ |
| 223 | smp_call_function_single(counter->cpu, |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 224 | __perf_counter_remove_from_context, |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 225 | counter, 1); |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | retry: |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 230 | task_oncpu_function_call(task, __perf_counter_remove_from_context, |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 231 | counter); |
| 232 | |
| 233 | spin_lock_irq(&ctx->lock); |
| 234 | /* |
| 235 | * If the context is active we need to retry the smp call. |
| 236 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 237 | if (ctx->nr_active && !list_empty(&counter->list_entry)) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 238 | spin_unlock_irq(&ctx->lock); |
| 239 | goto retry; |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * The lock prevents that this context is scheduled in so we |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 244 | * can remove the counter safely, if the call above did not |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 245 | * succeed. |
| 246 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 247 | if (!list_empty(&counter->list_entry)) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 248 | ctx->nr_counters--; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 249 | list_del_counter(counter, ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 250 | counter->task = NULL; |
| 251 | } |
| 252 | spin_unlock_irq(&ctx->lock); |
| 253 | } |
| 254 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 255 | /* |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 256 | * Get the current time for this context. |
| 257 | * If this is a task context, we use the task's task clock, |
| 258 | * or for a per-cpu context, we use the cpu clock. |
| 259 | */ |
| 260 | static u64 get_context_time(struct perf_counter_context *ctx, int update) |
| 261 | { |
| 262 | struct task_struct *curr = ctx->task; |
| 263 | |
| 264 | if (!curr) |
| 265 | return cpu_clock(smp_processor_id()); |
| 266 | |
| 267 | return __task_delta_exec(curr, update) + curr->se.sum_exec_runtime; |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Update the record of the current time in a context. |
| 272 | */ |
| 273 | static void update_context_time(struct perf_counter_context *ctx, int update) |
| 274 | { |
| 275 | ctx->time_now = get_context_time(ctx, update) - ctx->time_lost; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Update the total_time_enabled and total_time_running fields for a counter. |
| 280 | */ |
| 281 | static void update_counter_times(struct perf_counter *counter) |
| 282 | { |
| 283 | struct perf_counter_context *ctx = counter->ctx; |
| 284 | u64 run_end; |
| 285 | |
| 286 | if (counter->state >= PERF_COUNTER_STATE_INACTIVE) { |
| 287 | counter->total_time_enabled = ctx->time_now - |
| 288 | counter->tstamp_enabled; |
| 289 | if (counter->state == PERF_COUNTER_STATE_INACTIVE) |
| 290 | run_end = counter->tstamp_stopped; |
| 291 | else |
| 292 | run_end = ctx->time_now; |
| 293 | counter->total_time_running = run_end - counter->tstamp_running; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Update total_time_enabled and total_time_running for all counters in a group. |
| 299 | */ |
| 300 | static void update_group_times(struct perf_counter *leader) |
| 301 | { |
| 302 | struct perf_counter *counter; |
| 303 | |
| 304 | update_counter_times(leader); |
| 305 | list_for_each_entry(counter, &leader->sibling_list, list_entry) |
| 306 | update_counter_times(counter); |
| 307 | } |
| 308 | |
| 309 | /* |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 310 | * Cross CPU call to disable a performance counter |
| 311 | */ |
| 312 | static void __perf_counter_disable(void *info) |
| 313 | { |
| 314 | struct perf_counter *counter = info; |
| 315 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 316 | struct perf_counter_context *ctx = counter->ctx; |
| 317 | unsigned long flags; |
| 318 | |
| 319 | /* |
| 320 | * If this is a per-task counter, need to check whether this |
| 321 | * counter's task is the current task on this cpu. |
| 322 | */ |
| 323 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 324 | return; |
| 325 | |
| 326 | curr_rq_lock_irq_save(&flags); |
| 327 | spin_lock(&ctx->lock); |
| 328 | |
| 329 | /* |
| 330 | * If the counter is on, turn it off. |
| 331 | * If it is in error state, leave it in error state. |
| 332 | */ |
| 333 | if (counter->state >= PERF_COUNTER_STATE_INACTIVE) { |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 334 | update_context_time(ctx, 1); |
| 335 | update_counter_times(counter); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 336 | if (counter == counter->group_leader) |
| 337 | group_sched_out(counter, cpuctx, ctx); |
| 338 | else |
| 339 | counter_sched_out(counter, cpuctx, ctx); |
| 340 | counter->state = PERF_COUNTER_STATE_OFF; |
| 341 | } |
| 342 | |
| 343 | spin_unlock(&ctx->lock); |
| 344 | curr_rq_unlock_irq_restore(&flags); |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Disable a counter. |
| 349 | */ |
| 350 | static void perf_counter_disable(struct perf_counter *counter) |
| 351 | { |
| 352 | struct perf_counter_context *ctx = counter->ctx; |
| 353 | struct task_struct *task = ctx->task; |
| 354 | |
| 355 | if (!task) { |
| 356 | /* |
| 357 | * Disable the counter on the cpu that it's on |
| 358 | */ |
| 359 | smp_call_function_single(counter->cpu, __perf_counter_disable, |
| 360 | counter, 1); |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | retry: |
| 365 | task_oncpu_function_call(task, __perf_counter_disable, counter); |
| 366 | |
| 367 | spin_lock_irq(&ctx->lock); |
| 368 | /* |
| 369 | * If the counter is still active, we need to retry the cross-call. |
| 370 | */ |
| 371 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) { |
| 372 | spin_unlock_irq(&ctx->lock); |
| 373 | goto retry; |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Since we have the lock this context can't be scheduled |
| 378 | * in, so we can change the state safely. |
| 379 | */ |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 380 | if (counter->state == PERF_COUNTER_STATE_INACTIVE) { |
| 381 | update_counter_times(counter); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 382 | counter->state = PERF_COUNTER_STATE_OFF; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 383 | } |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 384 | |
| 385 | spin_unlock_irq(&ctx->lock); |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * Disable a counter and all its children. |
| 390 | */ |
| 391 | static void perf_counter_disable_family(struct perf_counter *counter) |
| 392 | { |
| 393 | struct perf_counter *child; |
| 394 | |
| 395 | perf_counter_disable(counter); |
| 396 | |
| 397 | /* |
| 398 | * Lock the mutex to protect the list of children |
| 399 | */ |
| 400 | mutex_lock(&counter->mutex); |
| 401 | list_for_each_entry(child, &counter->child_list, child_list) |
| 402 | perf_counter_disable(child); |
| 403 | mutex_unlock(&counter->mutex); |
| 404 | } |
| 405 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 406 | static int |
| 407 | counter_sched_in(struct perf_counter *counter, |
| 408 | struct perf_cpu_context *cpuctx, |
| 409 | struct perf_counter_context *ctx, |
| 410 | int cpu) |
| 411 | { |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 412 | if (counter->state <= PERF_COUNTER_STATE_OFF) |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 413 | return 0; |
| 414 | |
| 415 | counter->state = PERF_COUNTER_STATE_ACTIVE; |
| 416 | counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */ |
| 417 | /* |
| 418 | * The new state must be visible before we turn it on in the hardware: |
| 419 | */ |
| 420 | smp_wmb(); |
| 421 | |
| 422 | if (counter->hw_ops->enable(counter)) { |
| 423 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
| 424 | counter->oncpu = -1; |
| 425 | return -EAGAIN; |
| 426 | } |
| 427 | |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 428 | counter->tstamp_running += ctx->time_now - counter->tstamp_stopped; |
| 429 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 430 | if (!is_software_counter(counter)) |
| 431 | cpuctx->active_oncpu++; |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 432 | ctx->nr_active++; |
| 433 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 434 | if (counter->hw_event.exclusive) |
| 435 | cpuctx->exclusive = 1; |
| 436 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 437 | return 0; |
| 438 | } |
| 439 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 440 | /* |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 441 | * Return 1 for a group consisting entirely of software counters, |
| 442 | * 0 if the group contains any hardware counters. |
| 443 | */ |
| 444 | static int is_software_only_group(struct perf_counter *leader) |
| 445 | { |
| 446 | struct perf_counter *counter; |
| 447 | |
| 448 | if (!is_software_counter(leader)) |
| 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 | list_for_each_entry(counter, &leader->sibling_list, list_entry) |
| 452 | if (!is_software_counter(counter)) |
| 453 | return 0; |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 454 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 455 | return 1; |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Work out whether we can put this counter group on the CPU now. |
| 460 | */ |
| 461 | static int group_can_go_on(struct perf_counter *counter, |
| 462 | struct perf_cpu_context *cpuctx, |
| 463 | int can_add_hw) |
| 464 | { |
| 465 | /* |
| 466 | * Groups consisting entirely of software counters can always go on. |
| 467 | */ |
| 468 | if (is_software_only_group(counter)) |
| 469 | return 1; |
| 470 | /* |
| 471 | * If an exclusive group is already on, no other hardware |
| 472 | * counters can go on. |
| 473 | */ |
| 474 | if (cpuctx->exclusive) |
| 475 | return 0; |
| 476 | /* |
| 477 | * If this group is exclusive and there are already |
| 478 | * counters on the CPU, it can't go on. |
| 479 | */ |
| 480 | if (counter->hw_event.exclusive && cpuctx->active_oncpu) |
| 481 | return 0; |
| 482 | /* |
| 483 | * Otherwise, try to add it if all previous groups were able |
| 484 | * to go on. |
| 485 | */ |
| 486 | return can_add_hw; |
| 487 | } |
| 488 | |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 489 | static void add_counter_to_ctx(struct perf_counter *counter, |
| 490 | struct perf_counter_context *ctx) |
| 491 | { |
| 492 | list_add_counter(counter, ctx); |
| 493 | ctx->nr_counters++; |
| 494 | counter->prev_state = PERF_COUNTER_STATE_OFF; |
| 495 | counter->tstamp_enabled = ctx->time_now; |
| 496 | counter->tstamp_running = ctx->time_now; |
| 497 | counter->tstamp_stopped = ctx->time_now; |
| 498 | } |
| 499 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 500 | /* |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 501 | * Cross CPU call to install and enable a performance counter |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 502 | */ |
| 503 | static void __perf_install_in_context(void *info) |
| 504 | { |
| 505 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 506 | struct perf_counter *counter = info; |
| 507 | struct perf_counter_context *ctx = counter->ctx; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 508 | struct perf_counter *leader = counter->group_leader; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 509 | int cpu = smp_processor_id(); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 510 | unsigned long flags; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 511 | u64 perf_flags; |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 512 | int err; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 513 | |
| 514 | /* |
| 515 | * If this is a task context, we need to check whether it is |
| 516 | * the current task context of this cpu. If not it has been |
| 517 | * scheduled out before the smp call arrived. |
| 518 | */ |
| 519 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 520 | return; |
| 521 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 522 | curr_rq_lock_irq_save(&flags); |
| 523 | spin_lock(&ctx->lock); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 524 | update_context_time(ctx, 1); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 525 | |
| 526 | /* |
| 527 | * Protect the list operation against NMI by disabling the |
| 528 | * counters on a global level. NOP for non NMI based counters. |
| 529 | */ |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 530 | perf_flags = hw_perf_save_disable(); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 531 | |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 532 | add_counter_to_ctx(counter, ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 533 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 534 | /* |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 535 | * Don't put the counter on if it is disabled or if |
| 536 | * it is in a group and the group isn't on. |
| 537 | */ |
| 538 | if (counter->state != PERF_COUNTER_STATE_INACTIVE || |
| 539 | (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)) |
| 540 | goto unlock; |
| 541 | |
| 542 | /* |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 543 | * An exclusive counter can't go on if there are already active |
| 544 | * hardware counters, and no hardware counter can go on if there |
| 545 | * is already an exclusive counter on. |
| 546 | */ |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 547 | if (!group_can_go_on(counter, cpuctx, 1)) |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 548 | err = -EEXIST; |
| 549 | else |
| 550 | err = counter_sched_in(counter, cpuctx, ctx, cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 551 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 552 | if (err) { |
| 553 | /* |
| 554 | * This counter couldn't go on. If it is in a group |
| 555 | * then we have to pull the whole group off. |
| 556 | * If the counter group is pinned then put it in error state. |
| 557 | */ |
| 558 | if (leader != counter) |
| 559 | group_sched_out(leader, cpuctx, ctx); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 560 | if (leader->hw_event.pinned) { |
| 561 | update_group_times(leader); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 562 | leader->state = PERF_COUNTER_STATE_ERROR; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 563 | } |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 564 | } |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 565 | |
| 566 | if (!err && !ctx->task && cpuctx->max_pertask) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 567 | cpuctx->max_pertask--; |
| 568 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 569 | unlock: |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 570 | hw_perf_restore(perf_flags); |
| 571 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 572 | spin_unlock(&ctx->lock); |
| 573 | curr_rq_unlock_irq_restore(&flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Attach a performance counter to a context |
| 578 | * |
| 579 | * First we add the counter to the list with the hardware enable bit |
| 580 | * in counter->hw_config cleared. |
| 581 | * |
| 582 | * If the counter is attached to a task which is on a CPU we use a smp |
| 583 | * call to enable it in the task context. The task might have been |
| 584 | * scheduled away, but we check this in the smp call again. |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 585 | * |
| 586 | * Must be called with ctx->mutex held. |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 587 | */ |
| 588 | static void |
| 589 | perf_install_in_context(struct perf_counter_context *ctx, |
| 590 | struct perf_counter *counter, |
| 591 | int cpu) |
| 592 | { |
| 593 | struct task_struct *task = ctx->task; |
| 594 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 595 | if (!task) { |
| 596 | /* |
| 597 | * Per cpu counters are installed via an smp call and |
| 598 | * the install is always sucessful. |
| 599 | */ |
| 600 | smp_call_function_single(cpu, __perf_install_in_context, |
| 601 | counter, 1); |
| 602 | return; |
| 603 | } |
| 604 | |
| 605 | counter->task = task; |
| 606 | retry: |
| 607 | task_oncpu_function_call(task, __perf_install_in_context, |
| 608 | counter); |
| 609 | |
| 610 | spin_lock_irq(&ctx->lock); |
| 611 | /* |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 612 | * we need to retry the smp call. |
| 613 | */ |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 614 | if (ctx->is_active && list_empty(&counter->list_entry)) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 615 | spin_unlock_irq(&ctx->lock); |
| 616 | goto retry; |
| 617 | } |
| 618 | |
| 619 | /* |
| 620 | * The lock prevents that this context is scheduled in so we |
| 621 | * can add the counter safely, if it the call above did not |
| 622 | * succeed. |
| 623 | */ |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 624 | if (list_empty(&counter->list_entry)) |
| 625 | add_counter_to_ctx(counter, ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 626 | spin_unlock_irq(&ctx->lock); |
| 627 | } |
| 628 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 629 | /* |
| 630 | * Cross CPU call to enable a performance counter |
| 631 | */ |
| 632 | static void __perf_counter_enable(void *info) |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 633 | { |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 634 | struct perf_counter *counter = info; |
| 635 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 636 | struct perf_counter_context *ctx = counter->ctx; |
| 637 | struct perf_counter *leader = counter->group_leader; |
| 638 | unsigned long flags; |
| 639 | int err; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 640 | |
| 641 | /* |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 642 | * If this is a per-task counter, need to check whether this |
| 643 | * counter's task is the current task on this cpu. |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 644 | */ |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 645 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 646 | return; |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 647 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 648 | curr_rq_lock_irq_save(&flags); |
| 649 | spin_lock(&ctx->lock); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 650 | update_context_time(ctx, 1); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 651 | |
Paul Mackerras | c07c99b | 2009-02-13 22:10:34 +1100 | [diff] [blame] | 652 | counter->prev_state = counter->state; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 653 | if (counter->state >= PERF_COUNTER_STATE_INACTIVE) |
| 654 | goto unlock; |
| 655 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 656 | counter->tstamp_enabled = ctx->time_now - counter->total_time_enabled; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 657 | |
| 658 | /* |
| 659 | * If the counter is in a group and isn't the group leader, |
| 660 | * then don't put it on unless the group is on. |
| 661 | */ |
| 662 | if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE) |
| 663 | goto unlock; |
| 664 | |
| 665 | if (!group_can_go_on(counter, cpuctx, 1)) |
| 666 | err = -EEXIST; |
| 667 | else |
| 668 | err = counter_sched_in(counter, cpuctx, ctx, |
| 669 | smp_processor_id()); |
| 670 | |
| 671 | if (err) { |
| 672 | /* |
| 673 | * If this counter can't go on and it's part of a |
| 674 | * group, then the whole group has to come off. |
| 675 | */ |
| 676 | if (leader != counter) |
| 677 | group_sched_out(leader, cpuctx, ctx); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 678 | if (leader->hw_event.pinned) { |
| 679 | update_group_times(leader); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 680 | leader->state = PERF_COUNTER_STATE_ERROR; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 681 | } |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | unlock: |
| 685 | spin_unlock(&ctx->lock); |
| 686 | curr_rq_unlock_irq_restore(&flags); |
| 687 | } |
| 688 | |
| 689 | /* |
| 690 | * Enable a counter. |
| 691 | */ |
| 692 | static void perf_counter_enable(struct perf_counter *counter) |
| 693 | { |
| 694 | struct perf_counter_context *ctx = counter->ctx; |
| 695 | struct task_struct *task = ctx->task; |
| 696 | |
| 697 | if (!task) { |
| 698 | /* |
| 699 | * Enable the counter on the cpu that it's on |
| 700 | */ |
| 701 | smp_call_function_single(counter->cpu, __perf_counter_enable, |
| 702 | counter, 1); |
| 703 | return; |
| 704 | } |
| 705 | |
| 706 | spin_lock_irq(&ctx->lock); |
| 707 | if (counter->state >= PERF_COUNTER_STATE_INACTIVE) |
| 708 | goto out; |
| 709 | |
| 710 | /* |
| 711 | * If the counter is in error state, clear that first. |
| 712 | * That way, if we see the counter in error state below, we |
| 713 | * know that it has gone back into error state, as distinct |
| 714 | * from the task having been scheduled away before the |
| 715 | * cross-call arrived. |
| 716 | */ |
| 717 | if (counter->state == PERF_COUNTER_STATE_ERROR) |
| 718 | counter->state = PERF_COUNTER_STATE_OFF; |
| 719 | |
| 720 | retry: |
| 721 | spin_unlock_irq(&ctx->lock); |
| 722 | task_oncpu_function_call(task, __perf_counter_enable, counter); |
| 723 | |
| 724 | spin_lock_irq(&ctx->lock); |
| 725 | |
| 726 | /* |
| 727 | * If the context is active and the counter is still off, |
| 728 | * we need to retry the cross-call. |
| 729 | */ |
| 730 | if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF) |
| 731 | goto retry; |
| 732 | |
| 733 | /* |
| 734 | * Since we have the lock this context can't be scheduled |
| 735 | * in, so we can change the state safely. |
| 736 | */ |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 737 | if (counter->state == PERF_COUNTER_STATE_OFF) { |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 738 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 739 | counter->tstamp_enabled = ctx->time_now - |
| 740 | counter->total_time_enabled; |
| 741 | } |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 742 | out: |
| 743 | spin_unlock_irq(&ctx->lock); |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * Enable a counter and all its children. |
| 748 | */ |
| 749 | static void perf_counter_enable_family(struct perf_counter *counter) |
| 750 | { |
| 751 | struct perf_counter *child; |
| 752 | |
| 753 | perf_counter_enable(counter); |
| 754 | |
| 755 | /* |
| 756 | * Lock the mutex to protect the list of children |
| 757 | */ |
| 758 | mutex_lock(&counter->mutex); |
| 759 | list_for_each_entry(child, &counter->child_list, child_list) |
| 760 | perf_counter_enable(child); |
| 761 | mutex_unlock(&counter->mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 762 | } |
| 763 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 764 | void __perf_counter_sched_out(struct perf_counter_context *ctx, |
| 765 | struct perf_cpu_context *cpuctx) |
| 766 | { |
| 767 | struct perf_counter *counter; |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 768 | u64 flags; |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 769 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 770 | spin_lock(&ctx->lock); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 771 | ctx->is_active = 0; |
| 772 | if (likely(!ctx->nr_counters)) |
| 773 | goto out; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 774 | update_context_time(ctx, 0); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 775 | |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 776 | flags = hw_perf_save_disable(); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 777 | if (ctx->nr_active) { |
| 778 | list_for_each_entry(counter, &ctx->counter_list, list_entry) |
| 779 | group_sched_out(counter, cpuctx, ctx); |
| 780 | } |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 781 | hw_perf_restore(flags); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 782 | out: |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 783 | spin_unlock(&ctx->lock); |
| 784 | } |
| 785 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 786 | /* |
| 787 | * Called from scheduler to remove the counters of the current task, |
| 788 | * with interrupts disabled. |
| 789 | * |
| 790 | * We stop each counter and update the counter value in counter->count. |
| 791 | * |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 792 | * This does not protect us against NMI, but disable() |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 793 | * sets the disabled bit in the control field of counter _before_ |
| 794 | * accessing the counter control register. If a NMI hits, then it will |
| 795 | * not restart the counter. |
| 796 | */ |
| 797 | void perf_counter_task_sched_out(struct task_struct *task, int cpu) |
| 798 | { |
| 799 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 800 | struct perf_counter_context *ctx = &task->perf_counter_ctx; |
Peter Zijlstra | 4a0deca | 2009-03-19 20:26:12 +0100 | [diff] [blame] | 801 | struct pt_regs *regs; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 802 | |
| 803 | if (likely(!cpuctx->task_ctx)) |
| 804 | return; |
| 805 | |
Peter Zijlstra | 4a0deca | 2009-03-19 20:26:12 +0100 | [diff] [blame] | 806 | regs = task_pt_regs(task); |
| 807 | perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 808 | __perf_counter_sched_out(ctx, cpuctx); |
| 809 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 810 | cpuctx->task_ctx = NULL; |
| 811 | } |
| 812 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 813 | static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx) |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 814 | { |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 815 | __perf_counter_sched_out(&cpuctx->ctx, cpuctx); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 816 | } |
| 817 | |
Ingo Molnar | 7995888 | 2008-12-17 08:54:56 +0100 | [diff] [blame] | 818 | static int |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 819 | group_sched_in(struct perf_counter *group_counter, |
| 820 | struct perf_cpu_context *cpuctx, |
| 821 | struct perf_counter_context *ctx, |
| 822 | int cpu) |
| 823 | { |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 824 | struct perf_counter *counter, *partial_group; |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 825 | int ret; |
| 826 | |
| 827 | if (group_counter->state == PERF_COUNTER_STATE_OFF) |
| 828 | return 0; |
| 829 | |
| 830 | ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu); |
| 831 | if (ret) |
| 832 | return ret < 0 ? ret : 0; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 833 | |
Paul Mackerras | c07c99b | 2009-02-13 22:10:34 +1100 | [diff] [blame] | 834 | group_counter->prev_state = group_counter->state; |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 835 | if (counter_sched_in(group_counter, cpuctx, ctx, cpu)) |
| 836 | return -EAGAIN; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 837 | |
| 838 | /* |
| 839 | * Schedule in siblings as one group (if any): |
| 840 | */ |
Ingo Molnar | 7995888 | 2008-12-17 08:54:56 +0100 | [diff] [blame] | 841 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) { |
Paul Mackerras | c07c99b | 2009-02-13 22:10:34 +1100 | [diff] [blame] | 842 | counter->prev_state = counter->state; |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 843 | if (counter_sched_in(counter, cpuctx, ctx, cpu)) { |
| 844 | partial_group = counter; |
| 845 | goto group_error; |
| 846 | } |
Ingo Molnar | 7995888 | 2008-12-17 08:54:56 +0100 | [diff] [blame] | 847 | } |
| 848 | |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 849 | return 0; |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 850 | |
| 851 | group_error: |
| 852 | /* |
| 853 | * Groups can be scheduled in as one unit only, so undo any |
| 854 | * partial group before returning: |
| 855 | */ |
| 856 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) { |
| 857 | if (counter == partial_group) |
| 858 | break; |
| 859 | counter_sched_out(counter, cpuctx, ctx); |
| 860 | } |
| 861 | counter_sched_out(group_counter, cpuctx, ctx); |
| 862 | |
| 863 | return -EAGAIN; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 864 | } |
| 865 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 866 | static void |
| 867 | __perf_counter_sched_in(struct perf_counter_context *ctx, |
| 868 | struct perf_cpu_context *cpuctx, int cpu) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 869 | { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 870 | struct perf_counter *counter; |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 871 | u64 flags; |
Paul Mackerras | dd0e6ba | 2009-01-12 15:11:00 +1100 | [diff] [blame] | 872 | int can_add_hw = 1; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 873 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 874 | spin_lock(&ctx->lock); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 875 | ctx->is_active = 1; |
| 876 | if (likely(!ctx->nr_counters)) |
| 877 | goto out; |
| 878 | |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 879 | /* |
| 880 | * Add any time since the last sched_out to the lost time |
| 881 | * so it doesn't get included in the total_time_enabled and |
| 882 | * total_time_running measures for counters in the context. |
| 883 | */ |
| 884 | ctx->time_lost = get_context_time(ctx, 0) - ctx->time_now; |
| 885 | |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 886 | flags = hw_perf_save_disable(); |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 887 | |
| 888 | /* |
| 889 | * First go through the list and put on any pinned groups |
| 890 | * in order to give them the best chance of going on. |
| 891 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 892 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 893 | if (counter->state <= PERF_COUNTER_STATE_OFF || |
| 894 | !counter->hw_event.pinned) |
| 895 | continue; |
| 896 | if (counter->cpu != -1 && counter->cpu != cpu) |
| 897 | continue; |
| 898 | |
| 899 | if (group_can_go_on(counter, cpuctx, 1)) |
| 900 | group_sched_in(counter, cpuctx, ctx, cpu); |
| 901 | |
| 902 | /* |
| 903 | * If this pinned group hasn't been scheduled, |
| 904 | * put it in error state. |
| 905 | */ |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 906 | if (counter->state == PERF_COUNTER_STATE_INACTIVE) { |
| 907 | update_group_times(counter); |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 908 | counter->state = PERF_COUNTER_STATE_ERROR; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 909 | } |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
| 913 | /* |
| 914 | * Ignore counters in OFF or ERROR state, and |
| 915 | * ignore pinned counters since we did them already. |
| 916 | */ |
| 917 | if (counter->state <= PERF_COUNTER_STATE_OFF || |
| 918 | counter->hw_event.pinned) |
| 919 | continue; |
| 920 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 921 | /* |
| 922 | * Listen to the 'cpu' scheduling filter constraint |
| 923 | * of counters: |
| 924 | */ |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 925 | if (counter->cpu != -1 && counter->cpu != cpu) |
| 926 | continue; |
| 927 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 928 | if (group_can_go_on(counter, cpuctx, can_add_hw)) { |
Paul Mackerras | dd0e6ba | 2009-01-12 15:11:00 +1100 | [diff] [blame] | 929 | if (group_sched_in(counter, cpuctx, ctx, cpu)) |
| 930 | can_add_hw = 0; |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 931 | } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 932 | } |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 933 | hw_perf_restore(flags); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 934 | out: |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 935 | spin_unlock(&ctx->lock); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 936 | } |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 937 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 938 | /* |
| 939 | * Called from scheduler to add the counters of the current task |
| 940 | * with interrupts disabled. |
| 941 | * |
| 942 | * We restore the counter value and then enable it. |
| 943 | * |
| 944 | * This does not protect us against NMI, but enable() |
| 945 | * sets the enabled bit in the control field of counter _before_ |
| 946 | * accessing the counter control register. If a NMI hits, then it will |
| 947 | * keep the counter running. |
| 948 | */ |
| 949 | void perf_counter_task_sched_in(struct task_struct *task, int cpu) |
| 950 | { |
| 951 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 952 | struct perf_counter_context *ctx = &task->perf_counter_ctx; |
| 953 | |
| 954 | __perf_counter_sched_in(ctx, cpuctx, cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 955 | cpuctx->task_ctx = ctx; |
| 956 | } |
| 957 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 958 | static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu) |
| 959 | { |
| 960 | struct perf_counter_context *ctx = &cpuctx->ctx; |
| 961 | |
| 962 | __perf_counter_sched_in(ctx, cpuctx, cpu); |
| 963 | } |
| 964 | |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 965 | int perf_counter_task_disable(void) |
| 966 | { |
| 967 | struct task_struct *curr = current; |
| 968 | struct perf_counter_context *ctx = &curr->perf_counter_ctx; |
| 969 | struct perf_counter *counter; |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 970 | unsigned long flags; |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 971 | u64 perf_flags; |
| 972 | int cpu; |
| 973 | |
| 974 | if (likely(!ctx->nr_counters)) |
| 975 | return 0; |
| 976 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 977 | curr_rq_lock_irq_save(&flags); |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 978 | cpu = smp_processor_id(); |
| 979 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 980 | /* force the update of the task clock: */ |
| 981 | __task_delta_exec(curr, 1); |
| 982 | |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 983 | perf_counter_task_sched_out(curr, cpu); |
| 984 | |
| 985 | spin_lock(&ctx->lock); |
| 986 | |
| 987 | /* |
| 988 | * Disable all the counters: |
| 989 | */ |
| 990 | perf_flags = hw_perf_save_disable(); |
| 991 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 992 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 993 | if (counter->state != PERF_COUNTER_STATE_ERROR) { |
| 994 | update_group_times(counter); |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 995 | counter->state = PERF_COUNTER_STATE_OFF; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 996 | } |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 997 | } |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 998 | |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 999 | hw_perf_restore(perf_flags); |
| 1000 | |
| 1001 | spin_unlock(&ctx->lock); |
| 1002 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1003 | curr_rq_unlock_irq_restore(&flags); |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 1004 | |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
| 1008 | int perf_counter_task_enable(void) |
| 1009 | { |
| 1010 | struct task_struct *curr = current; |
| 1011 | struct perf_counter_context *ctx = &curr->perf_counter_ctx; |
| 1012 | struct perf_counter *counter; |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1013 | unsigned long flags; |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 1014 | u64 perf_flags; |
| 1015 | int cpu; |
| 1016 | |
| 1017 | if (likely(!ctx->nr_counters)) |
| 1018 | return 0; |
| 1019 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1020 | curr_rq_lock_irq_save(&flags); |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 1021 | cpu = smp_processor_id(); |
| 1022 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1023 | /* force the update of the task clock: */ |
| 1024 | __task_delta_exec(curr, 1); |
| 1025 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1026 | perf_counter_task_sched_out(curr, cpu); |
| 1027 | |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 1028 | spin_lock(&ctx->lock); |
| 1029 | |
| 1030 | /* |
| 1031 | * Disable all the counters: |
| 1032 | */ |
| 1033 | perf_flags = hw_perf_save_disable(); |
| 1034 | |
| 1035 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 1036 | if (counter->state > PERF_COUNTER_STATE_OFF) |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 1037 | continue; |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 1038 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 1039 | counter->tstamp_enabled = ctx->time_now - |
| 1040 | counter->total_time_enabled; |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1041 | counter->hw_event.disabled = 0; |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 1042 | } |
| 1043 | hw_perf_restore(perf_flags); |
| 1044 | |
| 1045 | spin_unlock(&ctx->lock); |
| 1046 | |
| 1047 | perf_counter_task_sched_in(curr, cpu); |
| 1048 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1049 | curr_rq_unlock_irq_restore(&flags); |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 1050 | |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1054 | /* |
| 1055 | * Round-robin a context's counters: |
| 1056 | */ |
| 1057 | static void rotate_ctx(struct perf_counter_context *ctx) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1058 | { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1059 | struct perf_counter *counter; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 1060 | u64 perf_flags; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1061 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1062 | if (!ctx->nr_counters) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1063 | return; |
| 1064 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1065 | spin_lock(&ctx->lock); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1066 | /* |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1067 | * Rotate the first entry last (works just fine for group counters too): |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1068 | */ |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 1069 | perf_flags = hw_perf_save_disable(); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1070 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
Peter Zijlstra | 7556423 | 2009-03-13 12:21:29 +0100 | [diff] [blame] | 1071 | list_move_tail(&counter->list_entry, &ctx->counter_list); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1072 | break; |
| 1073 | } |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 1074 | hw_perf_restore(perf_flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1075 | |
| 1076 | spin_unlock(&ctx->lock); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1077 | } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1078 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1079 | void perf_counter_task_tick(struct task_struct *curr, int cpu) |
| 1080 | { |
| 1081 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 1082 | struct perf_counter_context *ctx = &curr->perf_counter_ctx; |
| 1083 | const int rotate_percpu = 0; |
| 1084 | |
| 1085 | if (rotate_percpu) |
| 1086 | perf_counter_cpu_sched_out(cpuctx); |
| 1087 | perf_counter_task_sched_out(curr, cpu); |
| 1088 | |
| 1089 | if (rotate_percpu) |
| 1090 | rotate_ctx(&cpuctx->ctx); |
| 1091 | rotate_ctx(ctx); |
| 1092 | |
| 1093 | if (rotate_percpu) |
| 1094 | perf_counter_cpu_sched_in(cpuctx, cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1095 | perf_counter_task_sched_in(curr, cpu); |
| 1096 | } |
| 1097 | |
| 1098 | /* |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1099 | * Cross CPU call to read the hardware counter |
| 1100 | */ |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 1101 | static void __read(void *info) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1102 | { |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1103 | struct perf_counter *counter = info; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 1104 | struct perf_counter_context *ctx = counter->ctx; |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1105 | unsigned long flags; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1106 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1107 | curr_rq_lock_irq_save(&flags); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 1108 | if (ctx->is_active) |
| 1109 | update_context_time(ctx, 1); |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 1110 | counter->hw_ops->read(counter); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 1111 | update_counter_times(counter); |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1112 | curr_rq_unlock_irq_restore(&flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1113 | } |
| 1114 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1115 | static u64 perf_counter_read(struct perf_counter *counter) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1116 | { |
| 1117 | /* |
| 1118 | * If counter is enabled and currently active on a CPU, update the |
| 1119 | * value in the counter structure: |
| 1120 | */ |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 1121 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1122 | smp_call_function_single(counter->oncpu, |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 1123 | __read, counter, 1); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 1124 | } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) { |
| 1125 | update_counter_times(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1126 | } |
| 1127 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1128 | return atomic64_read(&counter->count); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1129 | } |
| 1130 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1131 | static void put_context(struct perf_counter_context *ctx) |
| 1132 | { |
| 1133 | if (ctx->task) |
| 1134 | put_task_struct(ctx->task); |
| 1135 | } |
| 1136 | |
| 1137 | static struct perf_counter_context *find_get_context(pid_t pid, int cpu) |
| 1138 | { |
| 1139 | struct perf_cpu_context *cpuctx; |
| 1140 | struct perf_counter_context *ctx; |
| 1141 | struct task_struct *task; |
| 1142 | |
| 1143 | /* |
| 1144 | * If cpu is not a wildcard then this is a percpu counter: |
| 1145 | */ |
| 1146 | if (cpu != -1) { |
| 1147 | /* Must be root to operate on a CPU counter: */ |
| 1148 | if (!capable(CAP_SYS_ADMIN)) |
| 1149 | return ERR_PTR(-EACCES); |
| 1150 | |
| 1151 | if (cpu < 0 || cpu > num_possible_cpus()) |
| 1152 | return ERR_PTR(-EINVAL); |
| 1153 | |
| 1154 | /* |
| 1155 | * We could be clever and allow to attach a counter to an |
| 1156 | * offline CPU and activate it when the CPU comes up, but |
| 1157 | * that's for later. |
| 1158 | */ |
| 1159 | if (!cpu_isset(cpu, cpu_online_map)) |
| 1160 | return ERR_PTR(-ENODEV); |
| 1161 | |
| 1162 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 1163 | ctx = &cpuctx->ctx; |
| 1164 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1165 | return ctx; |
| 1166 | } |
| 1167 | |
| 1168 | rcu_read_lock(); |
| 1169 | if (!pid) |
| 1170 | task = current; |
| 1171 | else |
| 1172 | task = find_task_by_vpid(pid); |
| 1173 | if (task) |
| 1174 | get_task_struct(task); |
| 1175 | rcu_read_unlock(); |
| 1176 | |
| 1177 | if (!task) |
| 1178 | return ERR_PTR(-ESRCH); |
| 1179 | |
| 1180 | ctx = &task->perf_counter_ctx; |
| 1181 | ctx->task = task; |
| 1182 | |
| 1183 | /* Reuse ptrace permission checks for now. */ |
| 1184 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) { |
| 1185 | put_context(ctx); |
| 1186 | return ERR_PTR(-EACCES); |
| 1187 | } |
| 1188 | |
| 1189 | return ctx; |
| 1190 | } |
| 1191 | |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 1192 | static void free_counter_rcu(struct rcu_head *head) |
| 1193 | { |
| 1194 | struct perf_counter *counter; |
| 1195 | |
| 1196 | counter = container_of(head, struct perf_counter, rcu_head); |
| 1197 | kfree(counter); |
| 1198 | } |
| 1199 | |
Peter Zijlstra | f160095 | 2009-03-19 20:26:16 +0100 | [diff] [blame] | 1200 | static void free_counter(struct perf_counter *counter) |
| 1201 | { |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 1202 | if (counter->destroy) |
| 1203 | counter->destroy(counter); |
| 1204 | |
Peter Zijlstra | f160095 | 2009-03-19 20:26:16 +0100 | [diff] [blame] | 1205 | call_rcu(&counter->rcu_head, free_counter_rcu); |
| 1206 | } |
| 1207 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1208 | /* |
| 1209 | * Called when the last reference to the file is gone. |
| 1210 | */ |
| 1211 | static int perf_release(struct inode *inode, struct file *file) |
| 1212 | { |
| 1213 | struct perf_counter *counter = file->private_data; |
| 1214 | struct perf_counter_context *ctx = counter->ctx; |
| 1215 | |
| 1216 | file->private_data = NULL; |
| 1217 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1218 | mutex_lock(&ctx->mutex); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1219 | mutex_lock(&counter->mutex); |
| 1220 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1221 | perf_counter_remove_from_context(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1222 | |
| 1223 | mutex_unlock(&counter->mutex); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1224 | mutex_unlock(&ctx->mutex); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1225 | |
Peter Zijlstra | f160095 | 2009-03-19 20:26:16 +0100 | [diff] [blame] | 1226 | free_counter(counter); |
Mike Galbraith | 5af7591 | 2009-02-11 10:53:37 +0100 | [diff] [blame] | 1227 | put_context(ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1228 | |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | /* |
| 1233 | * Read the performance counter - simple non blocking version for now |
| 1234 | */ |
| 1235 | static ssize_t |
| 1236 | perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count) |
| 1237 | { |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 1238 | u64 values[3]; |
| 1239 | int n; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1240 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 1241 | /* |
| 1242 | * Return end-of-file for a read on a counter that is in |
| 1243 | * error state (i.e. because it was pinned but it couldn't be |
| 1244 | * scheduled on to the CPU at some point). |
| 1245 | */ |
| 1246 | if (counter->state == PERF_COUNTER_STATE_ERROR) |
| 1247 | return 0; |
| 1248 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1249 | mutex_lock(&counter->mutex); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 1250 | values[0] = perf_counter_read(counter); |
| 1251 | n = 1; |
| 1252 | if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 1253 | values[n++] = counter->total_time_enabled + |
| 1254 | atomic64_read(&counter->child_total_time_enabled); |
| 1255 | if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 1256 | values[n++] = counter->total_time_running + |
| 1257 | atomic64_read(&counter->child_total_time_running); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1258 | mutex_unlock(&counter->mutex); |
| 1259 | |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 1260 | if (count < n * sizeof(u64)) |
| 1261 | return -EINVAL; |
| 1262 | count = n * sizeof(u64); |
| 1263 | |
| 1264 | if (copy_to_user(buf, values, count)) |
| 1265 | return -EFAULT; |
| 1266 | |
| 1267 | return count; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | static ssize_t |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1271 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 1272 | { |
| 1273 | struct perf_counter *counter = file->private_data; |
| 1274 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1275 | return perf_read_hw(counter, buf, count); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | static unsigned int perf_poll(struct file *file, poll_table *wait) |
| 1279 | { |
| 1280 | struct perf_counter *counter = file->private_data; |
Peter Zijlstra | c7138f3 | 2009-03-24 13:18:16 +0100 | [diff] [blame] | 1281 | struct perf_mmap_data *data; |
| 1282 | unsigned int events; |
| 1283 | |
| 1284 | rcu_read_lock(); |
| 1285 | data = rcu_dereference(counter->data); |
| 1286 | if (data) |
| 1287 | events = atomic_xchg(&data->wakeup, 0); |
| 1288 | else |
| 1289 | events = POLL_HUP; |
| 1290 | rcu_read_unlock(); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1291 | |
| 1292 | poll_wait(file, &counter->waitq, wait); |
| 1293 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1294 | return events; |
| 1295 | } |
| 1296 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1297 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 1298 | { |
| 1299 | struct perf_counter *counter = file->private_data; |
| 1300 | int err = 0; |
| 1301 | |
| 1302 | switch (cmd) { |
| 1303 | case PERF_COUNTER_IOC_ENABLE: |
| 1304 | perf_counter_enable_family(counter); |
| 1305 | break; |
| 1306 | case PERF_COUNTER_IOC_DISABLE: |
| 1307 | perf_counter_disable_family(counter); |
| 1308 | break; |
| 1309 | default: |
| 1310 | err = -ENOTTY; |
| 1311 | } |
| 1312 | return err; |
| 1313 | } |
| 1314 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1315 | static void __perf_counter_update_userpage(struct perf_counter *counter, |
| 1316 | struct perf_mmap_data *data) |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1317 | { |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1318 | struct perf_counter_mmap_page *userpg = data->user_page; |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1319 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1320 | /* |
| 1321 | * Disable preemption so as to not let the corresponding user-space |
| 1322 | * spin too long if we get preempted. |
| 1323 | */ |
| 1324 | preempt_disable(); |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1325 | ++userpg->lock; |
| 1326 | smp_wmb(); |
| 1327 | userpg->index = counter->hw.idx; |
| 1328 | userpg->offset = atomic64_read(&counter->count); |
| 1329 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) |
| 1330 | userpg->offset -= atomic64_read(&counter->hw.prev_count); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1331 | |
| 1332 | userpg->data_head = atomic_read(&data->head); |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1333 | smp_wmb(); |
| 1334 | ++userpg->lock; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1335 | preempt_enable(); |
| 1336 | } |
| 1337 | |
| 1338 | void perf_counter_update_userpage(struct perf_counter *counter) |
| 1339 | { |
| 1340 | struct perf_mmap_data *data; |
| 1341 | |
| 1342 | rcu_read_lock(); |
| 1343 | data = rcu_dereference(counter->data); |
| 1344 | if (data) |
| 1345 | __perf_counter_update_userpage(counter, data); |
| 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)) { |
| 1457 | perf_mmap_data_free(counter); |
| 1458 | mutex_unlock(&counter->mmap_mutex); |
| 1459 | } |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | static struct vm_operations_struct perf_mmap_vmops = { |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1463 | .open = perf_mmap_open, |
| 1464 | .close = perf_mmap_close, |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1465 | .fault = perf_mmap_fault, |
| 1466 | }; |
| 1467 | |
| 1468 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) |
| 1469 | { |
| 1470 | struct perf_counter *counter = file->private_data; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1471 | unsigned long vma_size; |
| 1472 | unsigned long nr_pages; |
| 1473 | unsigned long locked, lock_limit; |
| 1474 | int ret = 0; |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1475 | |
| 1476 | if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE)) |
| 1477 | return -EINVAL; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1478 | |
| 1479 | vma_size = vma->vm_end - vma->vm_start; |
| 1480 | nr_pages = (vma_size / PAGE_SIZE) - 1; |
| 1481 | |
Peter Zijlstra | 7730d86 | 2009-03-25 12:48:31 +0100 | [diff] [blame] | 1482 | /* |
| 1483 | * If we have data pages ensure they're a power-of-two number, so we |
| 1484 | * can do bitmasks instead of modulo. |
| 1485 | */ |
| 1486 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1487 | return -EINVAL; |
| 1488 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1489 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1490 | return -EINVAL; |
| 1491 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1492 | if (vma->vm_pgoff != 0) |
| 1493 | return -EINVAL; |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1494 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1495 | locked = vma_size >> PAGE_SHIFT; |
| 1496 | locked += vma->vm_mm->locked_vm; |
| 1497 | |
| 1498 | lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur; |
| 1499 | lock_limit >>= PAGE_SHIFT; |
| 1500 | |
| 1501 | if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) |
| 1502 | return -EPERM; |
| 1503 | |
| 1504 | mutex_lock(&counter->mmap_mutex); |
| 1505 | if (atomic_inc_not_zero(&counter->mmap_count)) |
| 1506 | goto out; |
| 1507 | |
| 1508 | WARN_ON(counter->data); |
| 1509 | ret = perf_mmap_data_alloc(counter, nr_pages); |
| 1510 | if (!ret) |
| 1511 | atomic_set(&counter->mmap_count, 1); |
| 1512 | out: |
| 1513 | mutex_unlock(&counter->mmap_mutex); |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1514 | |
| 1515 | vma->vm_flags &= ~VM_MAYWRITE; |
| 1516 | vma->vm_flags |= VM_RESERVED; |
| 1517 | vma->vm_ops = &perf_mmap_vmops; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1518 | |
| 1519 | return ret; |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1520 | } |
| 1521 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1522 | static const struct file_operations perf_fops = { |
| 1523 | .release = perf_release, |
| 1524 | .read = perf_read, |
| 1525 | .poll = perf_poll, |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1526 | .unlocked_ioctl = perf_ioctl, |
| 1527 | .compat_ioctl = perf_ioctl, |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1528 | .mmap = perf_mmap, |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1529 | }; |
| 1530 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1531 | /* |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1532 | * Output |
| 1533 | */ |
| 1534 | |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1535 | struct perf_output_handle { |
| 1536 | struct perf_counter *counter; |
| 1537 | struct perf_mmap_data *data; |
| 1538 | unsigned int offset; |
Peter Zijlstra | 63e35b2 | 2009-03-25 12:30:24 +0100 | [diff] [blame] | 1539 | unsigned int head; |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1540 | int wakeup; |
| 1541 | }; |
| 1542 | |
| 1543 | static int perf_output_begin(struct perf_output_handle *handle, |
| 1544 | struct perf_counter *counter, unsigned int size) |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1545 | { |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1546 | struct perf_mmap_data *data; |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1547 | unsigned int offset, head; |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1548 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1549 | rcu_read_lock(); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1550 | data = rcu_dereference(counter->data); |
| 1551 | if (!data) |
| 1552 | goto out; |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1553 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1554 | if (!data->nr_pages) |
| 1555 | goto out; |
| 1556 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1557 | do { |
| 1558 | offset = head = atomic_read(&data->head); |
Peter Zijlstra | c7138f3 | 2009-03-24 13:18:16 +0100 | [diff] [blame] | 1559 | head += size; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1560 | } while (atomic_cmpxchg(&data->head, offset, head) != offset); |
| 1561 | |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1562 | handle->counter = counter; |
| 1563 | handle->data = data; |
| 1564 | handle->offset = offset; |
Peter Zijlstra | 63e35b2 | 2009-03-25 12:30:24 +0100 | [diff] [blame] | 1565 | handle->head = head; |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1566 | handle->wakeup = (offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1567 | |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1568 | return 0; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1569 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1570 | out: |
| 1571 | rcu_read_unlock(); |
| 1572 | |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1573 | return -ENOSPC; |
| 1574 | } |
| 1575 | |
| 1576 | static void perf_output_copy(struct perf_output_handle *handle, |
| 1577 | void *buf, unsigned int len) |
| 1578 | { |
| 1579 | unsigned int pages_mask; |
| 1580 | unsigned int offset; |
| 1581 | unsigned int size; |
| 1582 | void **pages; |
| 1583 | |
| 1584 | offset = handle->offset; |
| 1585 | pages_mask = handle->data->nr_pages - 1; |
| 1586 | pages = handle->data->data_pages; |
| 1587 | |
| 1588 | do { |
| 1589 | unsigned int page_offset; |
| 1590 | int nr; |
| 1591 | |
| 1592 | nr = (offset >> PAGE_SHIFT) & pages_mask; |
| 1593 | page_offset = offset & (PAGE_SIZE - 1); |
| 1594 | size = min_t(unsigned int, PAGE_SIZE - page_offset, len); |
| 1595 | |
| 1596 | memcpy(pages[nr] + page_offset, buf, size); |
| 1597 | |
| 1598 | len -= size; |
| 1599 | buf += size; |
| 1600 | offset += size; |
| 1601 | } while (len); |
| 1602 | |
| 1603 | handle->offset = offset; |
Peter Zijlstra | 63e35b2 | 2009-03-25 12:30:24 +0100 | [diff] [blame] | 1604 | |
| 1605 | WARN_ON_ONCE(handle->offset > handle->head); |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1606 | } |
| 1607 | |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 1608 | #define perf_output_put(handle, x) \ |
| 1609 | perf_output_copy((handle), &(x), sizeof(x)) |
| 1610 | |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1611 | static void perf_output_end(struct perf_output_handle *handle, int nmi) |
| 1612 | { |
| 1613 | if (handle->wakeup) { |
| 1614 | (void)atomic_xchg(&handle->data->wakeup, POLL_IN); |
| 1615 | __perf_counter_update_userpage(handle->counter, handle->data); |
| 1616 | if (nmi) { |
| 1617 | handle->counter->wakeup_pending = 1; |
| 1618 | set_perf_counter_pending(); |
| 1619 | } else |
| 1620 | wake_up(&handle->counter->waitq); |
| 1621 | } |
| 1622 | rcu_read_unlock(); |
| 1623 | } |
| 1624 | |
| 1625 | static int perf_output_write(struct perf_counter *counter, int nmi, |
| 1626 | void *buf, ssize_t size) |
| 1627 | { |
| 1628 | struct perf_output_handle handle; |
| 1629 | int ret; |
| 1630 | |
| 1631 | ret = perf_output_begin(&handle, counter, size); |
| 1632 | if (ret) |
| 1633 | goto out; |
| 1634 | |
| 1635 | perf_output_copy(&handle, buf, size); |
| 1636 | perf_output_end(&handle, nmi); |
| 1637 | |
| 1638 | out: |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1639 | return ret; |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1640 | } |
| 1641 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1642 | static void perf_output_simple(struct perf_counter *counter, |
| 1643 | int nmi, struct pt_regs *regs) |
| 1644 | { |
Peter Zijlstra | ea5d20c | 2009-03-25 12:30:25 +0100 | [diff] [blame] | 1645 | unsigned int size; |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 1646 | struct { |
| 1647 | struct perf_event_header header; |
| 1648 | u64 ip; |
Peter Zijlstra | ea5d20c | 2009-03-25 12:30:25 +0100 | [diff] [blame] | 1649 | u32 pid, tid; |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 1650 | } event; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1651 | |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 1652 | event.header.type = PERF_EVENT_IP; |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 1653 | event.ip = instruction_pointer(regs); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1654 | |
Peter Zijlstra | ea5d20c | 2009-03-25 12:30:25 +0100 | [diff] [blame] | 1655 | size = sizeof(event); |
| 1656 | |
| 1657 | if (counter->hw_event.include_tid) { |
| 1658 | /* namespace issues */ |
| 1659 | event.pid = current->group_leader->pid; |
| 1660 | event.tid = current->pid; |
| 1661 | |
| 1662 | event.header.type |= __PERF_EVENT_TID; |
| 1663 | } else |
| 1664 | size -= sizeof(u64); |
| 1665 | |
| 1666 | event.header.size = size; |
| 1667 | |
| 1668 | perf_output_write(counter, nmi, &event, size); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1669 | } |
| 1670 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1671 | static void perf_output_group(struct perf_counter *counter, int nmi) |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1672 | { |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 1673 | struct perf_output_handle handle; |
| 1674 | struct perf_event_header header; |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1675 | struct perf_counter *leader, *sub; |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 1676 | unsigned int size; |
| 1677 | struct { |
| 1678 | u64 event; |
| 1679 | u64 counter; |
| 1680 | } entry; |
| 1681 | int ret; |
| 1682 | |
| 1683 | size = sizeof(header) + counter->nr_siblings * sizeof(entry); |
| 1684 | |
| 1685 | ret = perf_output_begin(&handle, counter, size); |
| 1686 | if (ret) |
| 1687 | return; |
| 1688 | |
| 1689 | header.type = PERF_EVENT_GROUP; |
| 1690 | header.size = size; |
| 1691 | |
| 1692 | perf_output_put(&handle, header); |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1693 | |
| 1694 | leader = counter->group_leader; |
| 1695 | list_for_each_entry(sub, &leader->sibling_list, list_entry) { |
| 1696 | if (sub != counter) |
| 1697 | sub->hw_ops->read(sub); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1698 | |
| 1699 | entry.event = sub->hw_event.config; |
| 1700 | entry.counter = atomic64_read(&sub->count); |
| 1701 | |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 1702 | perf_output_put(&handle, entry); |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1703 | } |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 1704 | |
| 1705 | perf_output_end(&handle, nmi); |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | void perf_counter_output(struct perf_counter *counter, |
| 1709 | int nmi, struct pt_regs *regs) |
| 1710 | { |
| 1711 | switch (counter->hw_event.record_type) { |
| 1712 | case PERF_RECORD_SIMPLE: |
| 1713 | return; |
| 1714 | |
| 1715 | case PERF_RECORD_IRQ: |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1716 | perf_output_simple(counter, nmi, regs); |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1717 | break; |
| 1718 | |
| 1719 | case PERF_RECORD_GROUP: |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1720 | perf_output_group(counter, nmi); |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1721 | break; |
| 1722 | } |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | /* |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1726 | * Generic software counter infrastructure |
| 1727 | */ |
| 1728 | |
| 1729 | static void perf_swcounter_update(struct perf_counter *counter) |
| 1730 | { |
| 1731 | struct hw_perf_counter *hwc = &counter->hw; |
| 1732 | u64 prev, now; |
| 1733 | s64 delta; |
| 1734 | |
| 1735 | again: |
| 1736 | prev = atomic64_read(&hwc->prev_count); |
| 1737 | now = atomic64_read(&hwc->count); |
| 1738 | if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev) |
| 1739 | goto again; |
| 1740 | |
| 1741 | delta = now - prev; |
| 1742 | |
| 1743 | atomic64_add(delta, &counter->count); |
| 1744 | atomic64_sub(delta, &hwc->period_left); |
| 1745 | } |
| 1746 | |
| 1747 | static void perf_swcounter_set_period(struct perf_counter *counter) |
| 1748 | { |
| 1749 | struct hw_perf_counter *hwc = &counter->hw; |
| 1750 | s64 left = atomic64_read(&hwc->period_left); |
| 1751 | s64 period = hwc->irq_period; |
| 1752 | |
| 1753 | if (unlikely(left <= -period)) { |
| 1754 | left = period; |
| 1755 | atomic64_set(&hwc->period_left, left); |
| 1756 | } |
| 1757 | |
| 1758 | if (unlikely(left <= 0)) { |
| 1759 | left += period; |
| 1760 | atomic64_add(period, &hwc->period_left); |
| 1761 | } |
| 1762 | |
| 1763 | atomic64_set(&hwc->prev_count, -left); |
| 1764 | atomic64_set(&hwc->count, -left); |
| 1765 | } |
| 1766 | |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 1767 | static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer) |
| 1768 | { |
| 1769 | struct perf_counter *counter; |
| 1770 | struct pt_regs *regs; |
| 1771 | |
| 1772 | counter = container_of(hrtimer, struct perf_counter, hw.hrtimer); |
| 1773 | counter->hw_ops->read(counter); |
| 1774 | |
| 1775 | regs = get_irq_regs(); |
| 1776 | /* |
| 1777 | * In case we exclude kernel IPs or are somehow not in interrupt |
| 1778 | * context, provide the next best thing, the user IP. |
| 1779 | */ |
| 1780 | if ((counter->hw_event.exclude_kernel || !regs) && |
| 1781 | !counter->hw_event.exclude_user) |
| 1782 | regs = task_pt_regs(current); |
| 1783 | |
| 1784 | if (regs) |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1785 | perf_counter_output(counter, 0, regs); |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 1786 | |
| 1787 | hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period)); |
| 1788 | |
| 1789 | return HRTIMER_RESTART; |
| 1790 | } |
| 1791 | |
| 1792 | static void perf_swcounter_overflow(struct perf_counter *counter, |
| 1793 | int nmi, struct pt_regs *regs) |
| 1794 | { |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 1795 | perf_swcounter_update(counter); |
| 1796 | perf_swcounter_set_period(counter); |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1797 | perf_counter_output(counter, nmi, regs); |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 1798 | } |
| 1799 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1800 | static int perf_swcounter_match(struct perf_counter *counter, |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 1801 | enum perf_event_types type, |
| 1802 | u32 event, struct pt_regs *regs) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1803 | { |
| 1804 | if (counter->state != PERF_COUNTER_STATE_ACTIVE) |
| 1805 | return 0; |
| 1806 | |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 1807 | if (perf_event_raw(&counter->hw_event)) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1808 | return 0; |
| 1809 | |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 1810 | if (perf_event_type(&counter->hw_event) != type) |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 1811 | return 0; |
| 1812 | |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 1813 | if (perf_event_id(&counter->hw_event) != event) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1814 | return 0; |
| 1815 | |
| 1816 | if (counter->hw_event.exclude_user && user_mode(regs)) |
| 1817 | return 0; |
| 1818 | |
| 1819 | if (counter->hw_event.exclude_kernel && !user_mode(regs)) |
| 1820 | return 0; |
| 1821 | |
| 1822 | return 1; |
| 1823 | } |
| 1824 | |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 1825 | static void perf_swcounter_add(struct perf_counter *counter, u64 nr, |
| 1826 | int nmi, struct pt_regs *regs) |
| 1827 | { |
| 1828 | int neg = atomic64_add_negative(nr, &counter->hw.count); |
| 1829 | if (counter->hw.irq_period && !neg) |
| 1830 | perf_swcounter_overflow(counter, nmi, regs); |
| 1831 | } |
| 1832 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1833 | static void perf_swcounter_ctx_event(struct perf_counter_context *ctx, |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 1834 | enum perf_event_types type, u32 event, |
| 1835 | u64 nr, int nmi, struct pt_regs *regs) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1836 | { |
| 1837 | struct perf_counter *counter; |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1838 | |
Peter Zijlstra | 01ef09d | 2009-03-19 20:26:11 +0100 | [diff] [blame] | 1839 | if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list)) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1840 | return; |
| 1841 | |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 1842 | rcu_read_lock(); |
| 1843 | list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) { |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 1844 | if (perf_swcounter_match(counter, type, event, regs)) |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 1845 | perf_swcounter_add(counter, nr, nmi, regs); |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1846 | } |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 1847 | rcu_read_unlock(); |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1848 | } |
| 1849 | |
Peter Zijlstra | 96f6d44 | 2009-03-23 18:22:07 +0100 | [diff] [blame] | 1850 | static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx) |
| 1851 | { |
| 1852 | if (in_nmi()) |
| 1853 | return &cpuctx->recursion[3]; |
| 1854 | |
| 1855 | if (in_irq()) |
| 1856 | return &cpuctx->recursion[2]; |
| 1857 | |
| 1858 | if (in_softirq()) |
| 1859 | return &cpuctx->recursion[1]; |
| 1860 | |
| 1861 | return &cpuctx->recursion[0]; |
| 1862 | } |
| 1863 | |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 1864 | static void __perf_swcounter_event(enum perf_event_types type, u32 event, |
| 1865 | u64 nr, int nmi, struct pt_regs *regs) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1866 | { |
| 1867 | struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context); |
Peter Zijlstra | 96f6d44 | 2009-03-23 18:22:07 +0100 | [diff] [blame] | 1868 | int *recursion = perf_swcounter_recursion_context(cpuctx); |
| 1869 | |
| 1870 | if (*recursion) |
| 1871 | goto out; |
| 1872 | |
| 1873 | (*recursion)++; |
| 1874 | barrier(); |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1875 | |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 1876 | perf_swcounter_ctx_event(&cpuctx->ctx, type, event, nr, nmi, regs); |
| 1877 | if (cpuctx->task_ctx) { |
| 1878 | perf_swcounter_ctx_event(cpuctx->task_ctx, type, event, |
| 1879 | nr, nmi, regs); |
| 1880 | } |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1881 | |
Peter Zijlstra | 96f6d44 | 2009-03-23 18:22:07 +0100 | [diff] [blame] | 1882 | barrier(); |
| 1883 | (*recursion)--; |
| 1884 | |
| 1885 | out: |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1886 | put_cpu_var(perf_cpu_context); |
| 1887 | } |
| 1888 | |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 1889 | void perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs) |
| 1890 | { |
| 1891 | __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs); |
| 1892 | } |
| 1893 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1894 | static void perf_swcounter_read(struct perf_counter *counter) |
| 1895 | { |
| 1896 | perf_swcounter_update(counter); |
| 1897 | } |
| 1898 | |
| 1899 | static int perf_swcounter_enable(struct perf_counter *counter) |
| 1900 | { |
| 1901 | perf_swcounter_set_period(counter); |
| 1902 | return 0; |
| 1903 | } |
| 1904 | |
| 1905 | static void perf_swcounter_disable(struct perf_counter *counter) |
| 1906 | { |
| 1907 | perf_swcounter_update(counter); |
| 1908 | } |
| 1909 | |
Peter Zijlstra | ac17dc8 | 2009-03-13 12:21:34 +0100 | [diff] [blame] | 1910 | static const struct hw_perf_counter_ops perf_ops_generic = { |
| 1911 | .enable = perf_swcounter_enable, |
| 1912 | .disable = perf_swcounter_disable, |
| 1913 | .read = perf_swcounter_read, |
| 1914 | }; |
| 1915 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1916 | /* |
| 1917 | * Software counter: cpu wall time clock |
| 1918 | */ |
| 1919 | |
Paul Mackerras | 9abf8a0 | 2009-01-09 16:26:43 +1100 | [diff] [blame] | 1920 | static void cpu_clock_perf_counter_update(struct perf_counter *counter) |
| 1921 | { |
| 1922 | int cpu = raw_smp_processor_id(); |
| 1923 | s64 prev; |
| 1924 | u64 now; |
| 1925 | |
| 1926 | now = cpu_clock(cpu); |
| 1927 | prev = atomic64_read(&counter->hw.prev_count); |
| 1928 | atomic64_set(&counter->hw.prev_count, now); |
| 1929 | atomic64_add(now - prev, &counter->count); |
| 1930 | } |
| 1931 | |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 1932 | static int cpu_clock_perf_counter_enable(struct perf_counter *counter) |
| 1933 | { |
| 1934 | struct hw_perf_counter *hwc = &counter->hw; |
| 1935 | int cpu = raw_smp_processor_id(); |
| 1936 | |
| 1937 | atomic64_set(&hwc->prev_count, cpu_clock(cpu)); |
Peter Zijlstra | 039fc91 | 2009-03-13 16:43:47 +0100 | [diff] [blame] | 1938 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1939 | hwc->hrtimer.function = perf_swcounter_hrtimer; |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 1940 | if (hwc->irq_period) { |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 1941 | __hrtimer_start_range_ns(&hwc->hrtimer, |
| 1942 | ns_to_ktime(hwc->irq_period), 0, |
| 1943 | HRTIMER_MODE_REL, 0); |
| 1944 | } |
| 1945 | |
| 1946 | return 0; |
| 1947 | } |
| 1948 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 1949 | static void cpu_clock_perf_counter_disable(struct perf_counter *counter) |
| 1950 | { |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 1951 | hrtimer_cancel(&counter->hw.hrtimer); |
Paul Mackerras | 9abf8a0 | 2009-01-09 16:26:43 +1100 | [diff] [blame] | 1952 | cpu_clock_perf_counter_update(counter); |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | static void cpu_clock_perf_counter_read(struct perf_counter *counter) |
| 1956 | { |
Paul Mackerras | 9abf8a0 | 2009-01-09 16:26:43 +1100 | [diff] [blame] | 1957 | cpu_clock_perf_counter_update(counter); |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | static const struct hw_perf_counter_ops perf_ops_cpu_clock = { |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 1961 | .enable = cpu_clock_perf_counter_enable, |
| 1962 | .disable = cpu_clock_perf_counter_disable, |
| 1963 | .read = cpu_clock_perf_counter_read, |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 1964 | }; |
| 1965 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1966 | /* |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1967 | * Software counter: task time clock |
| 1968 | */ |
| 1969 | |
| 1970 | /* |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1971 | * Called from within the scheduler: |
| 1972 | */ |
| 1973 | static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update) |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 1974 | { |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1975 | struct task_struct *curr = counter->task; |
| 1976 | u64 delta; |
| 1977 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1978 | delta = __task_delta_exec(curr, update); |
| 1979 | |
| 1980 | return curr->se.sum_exec_runtime + delta; |
| 1981 | } |
| 1982 | |
| 1983 | static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now) |
| 1984 | { |
| 1985 | u64 prev; |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 1986 | s64 delta; |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 1987 | |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 1988 | prev = atomic64_read(&counter->hw.prev_count); |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 1989 | |
| 1990 | atomic64_set(&counter->hw.prev_count, now); |
| 1991 | |
| 1992 | delta = now - prev; |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 1993 | |
| 1994 | atomic64_add(delta, &counter->count); |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 1995 | } |
| 1996 | |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 1997 | static int task_clock_perf_counter_enable(struct perf_counter *counter) |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 1998 | { |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 1999 | struct hw_perf_counter *hwc = &counter->hw; |
| 2000 | |
| 2001 | atomic64_set(&hwc->prev_count, task_clock_perf_counter_val(counter, 0)); |
Peter Zijlstra | 039fc91 | 2009-03-13 16:43:47 +0100 | [diff] [blame] | 2002 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 2003 | hwc->hrtimer.function = perf_swcounter_hrtimer; |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2004 | if (hwc->irq_period) { |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2005 | __hrtimer_start_range_ns(&hwc->hrtimer, |
| 2006 | ns_to_ktime(hwc->irq_period), 0, |
| 2007 | HRTIMER_MODE_REL, 0); |
| 2008 | } |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 2009 | |
| 2010 | return 0; |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | static void task_clock_perf_counter_disable(struct perf_counter *counter) |
| 2014 | { |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2015 | hrtimer_cancel(&counter->hw.hrtimer); |
| 2016 | task_clock_perf_counter_update(counter, |
| 2017 | task_clock_perf_counter_val(counter, 0)); |
| 2018 | } |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 2019 | |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2020 | static void task_clock_perf_counter_read(struct perf_counter *counter) |
| 2021 | { |
| 2022 | task_clock_perf_counter_update(counter, |
| 2023 | task_clock_perf_counter_val(counter, 1)); |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | static const struct hw_perf_counter_ops perf_ops_task_clock = { |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 2027 | .enable = task_clock_perf_counter_enable, |
| 2028 | .disable = task_clock_perf_counter_disable, |
| 2029 | .read = task_clock_perf_counter_read, |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 2030 | }; |
| 2031 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2032 | /* |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2033 | * Software counter: cpu migrations |
| 2034 | */ |
| 2035 | |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 2036 | static inline u64 get_cpu_migrations(struct perf_counter *counter) |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2037 | { |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 2038 | struct task_struct *curr = counter->ctx->task; |
| 2039 | |
| 2040 | if (curr) |
| 2041 | return curr->se.nr_migrations; |
| 2042 | return cpu_nr_migrations(smp_processor_id()); |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | static void cpu_migrations_perf_counter_update(struct perf_counter *counter) |
| 2046 | { |
| 2047 | u64 prev, now; |
| 2048 | s64 delta; |
| 2049 | |
| 2050 | prev = atomic64_read(&counter->hw.prev_count); |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 2051 | now = get_cpu_migrations(counter); |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2052 | |
| 2053 | atomic64_set(&counter->hw.prev_count, now); |
| 2054 | |
| 2055 | delta = now - prev; |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2056 | |
| 2057 | atomic64_add(delta, &counter->count); |
| 2058 | } |
| 2059 | |
| 2060 | static void cpu_migrations_perf_counter_read(struct perf_counter *counter) |
| 2061 | { |
| 2062 | cpu_migrations_perf_counter_update(counter); |
| 2063 | } |
| 2064 | |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 2065 | static int cpu_migrations_perf_counter_enable(struct perf_counter *counter) |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2066 | { |
Paul Mackerras | c07c99b | 2009-02-13 22:10:34 +1100 | [diff] [blame] | 2067 | if (counter->prev_state <= PERF_COUNTER_STATE_OFF) |
| 2068 | atomic64_set(&counter->hw.prev_count, |
| 2069 | get_cpu_migrations(counter)); |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 2070 | return 0; |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | static void cpu_migrations_perf_counter_disable(struct perf_counter *counter) |
| 2074 | { |
| 2075 | cpu_migrations_perf_counter_update(counter); |
| 2076 | } |
| 2077 | |
| 2078 | static const struct hw_perf_counter_ops perf_ops_cpu_migrations = { |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 2079 | .enable = cpu_migrations_perf_counter_enable, |
| 2080 | .disable = cpu_migrations_perf_counter_disable, |
| 2081 | .read = cpu_migrations_perf_counter_read, |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2082 | }; |
| 2083 | |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 2084 | #ifdef CONFIG_EVENT_PROFILE |
| 2085 | void perf_tpcounter_event(int event_id) |
| 2086 | { |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2087 | struct pt_regs *regs = get_irq_regs(); |
| 2088 | |
| 2089 | if (!regs) |
| 2090 | regs = task_pt_regs(current); |
| 2091 | |
| 2092 | __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs); |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 2093 | } |
| 2094 | |
| 2095 | extern int ftrace_profile_enable(int); |
| 2096 | extern void ftrace_profile_disable(int); |
| 2097 | |
| 2098 | static void tp_perf_counter_destroy(struct perf_counter *counter) |
| 2099 | { |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2100 | ftrace_profile_disable(perf_event_id(&counter->hw_event)); |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | static const struct hw_perf_counter_ops * |
| 2104 | tp_perf_counter_init(struct perf_counter *counter) |
| 2105 | { |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2106 | int event_id = perf_event_id(&counter->hw_event); |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 2107 | int ret; |
| 2108 | |
| 2109 | ret = ftrace_profile_enable(event_id); |
| 2110 | if (ret) |
| 2111 | return NULL; |
| 2112 | |
| 2113 | counter->destroy = tp_perf_counter_destroy; |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2114 | counter->hw.irq_period = counter->hw_event.irq_period; |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 2115 | |
| 2116 | return &perf_ops_generic; |
| 2117 | } |
| 2118 | #else |
| 2119 | static const struct hw_perf_counter_ops * |
| 2120 | tp_perf_counter_init(struct perf_counter *counter) |
| 2121 | { |
| 2122 | return NULL; |
| 2123 | } |
| 2124 | #endif |
| 2125 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2126 | static const struct hw_perf_counter_ops * |
| 2127 | sw_perf_counter_init(struct perf_counter *counter) |
| 2128 | { |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2129 | struct perf_counter_hw_event *hw_event = &counter->hw_event; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2130 | const struct hw_perf_counter_ops *hw_ops = NULL; |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2131 | struct hw_perf_counter *hwc = &counter->hw; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2132 | |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 2133 | /* |
| 2134 | * Software counters (currently) can't in general distinguish |
| 2135 | * between user, kernel and hypervisor events. |
| 2136 | * However, context switches and cpu migrations are considered |
| 2137 | * to be kernel events, and page faults are never hypervisor |
| 2138 | * events. |
| 2139 | */ |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2140 | switch (perf_event_id(&counter->hw_event)) { |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2141 | case PERF_COUNT_CPU_CLOCK: |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2142 | hw_ops = &perf_ops_cpu_clock; |
| 2143 | |
| 2144 | if (hw_event->irq_period && hw_event->irq_period < 10000) |
| 2145 | hw_event->irq_period = 10000; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2146 | break; |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 2147 | case PERF_COUNT_TASK_CLOCK: |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 2148 | /* |
| 2149 | * If the user instantiates this as a per-cpu counter, |
| 2150 | * use the cpu_clock counter instead. |
| 2151 | */ |
| 2152 | if (counter->ctx->task) |
| 2153 | hw_ops = &perf_ops_task_clock; |
| 2154 | else |
| 2155 | hw_ops = &perf_ops_cpu_clock; |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2156 | |
| 2157 | if (hw_event->irq_period && hw_event->irq_period < 10000) |
| 2158 | hw_event->irq_period = 10000; |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 2159 | break; |
Ingo Molnar | e06c61a | 2008-12-14 14:44:31 +0100 | [diff] [blame] | 2160 | case PERF_COUNT_PAGE_FAULTS: |
Peter Zijlstra | ac17dc8 | 2009-03-13 12:21:34 +0100 | [diff] [blame] | 2161 | case PERF_COUNT_PAGE_FAULTS_MIN: |
| 2162 | case PERF_COUNT_PAGE_FAULTS_MAJ: |
Ingo Molnar | 5d6a27d | 2008-12-14 12:28:33 +0100 | [diff] [blame] | 2163 | case PERF_COUNT_CONTEXT_SWITCHES: |
Peter Zijlstra | 4a0deca | 2009-03-19 20:26:12 +0100 | [diff] [blame] | 2164 | hw_ops = &perf_ops_generic; |
Ingo Molnar | 5d6a27d | 2008-12-14 12:28:33 +0100 | [diff] [blame] | 2165 | break; |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2166 | case PERF_COUNT_CPU_MIGRATIONS: |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 2167 | if (!counter->hw_event.exclude_kernel) |
| 2168 | hw_ops = &perf_ops_cpu_migrations; |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2169 | break; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2170 | } |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2171 | |
| 2172 | if (hw_ops) |
| 2173 | hwc->irq_period = hw_event->irq_period; |
| 2174 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2175 | return hw_ops; |
| 2176 | } |
| 2177 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2178 | /* |
| 2179 | * Allocate and initialize a counter structure |
| 2180 | */ |
| 2181 | static struct perf_counter * |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2182 | perf_counter_alloc(struct perf_counter_hw_event *hw_event, |
| 2183 | int cpu, |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 2184 | struct perf_counter_context *ctx, |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2185 | struct perf_counter *group_leader, |
| 2186 | gfp_t gfpflags) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2187 | { |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2188 | const struct hw_perf_counter_ops *hw_ops; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2189 | struct perf_counter *counter; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2190 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2191 | counter = kzalloc(sizeof(*counter), gfpflags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2192 | if (!counter) |
| 2193 | return NULL; |
| 2194 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2195 | /* |
| 2196 | * Single counters are their own group leaders, with an |
| 2197 | * empty sibling list: |
| 2198 | */ |
| 2199 | if (!group_leader) |
| 2200 | group_leader = counter; |
| 2201 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2202 | mutex_init(&counter->mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2203 | INIT_LIST_HEAD(&counter->list_entry); |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 2204 | INIT_LIST_HEAD(&counter->event_entry); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2205 | INIT_LIST_HEAD(&counter->sibling_list); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2206 | init_waitqueue_head(&counter->waitq); |
| 2207 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 2208 | mutex_init(&counter->mmap_mutex); |
| 2209 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2210 | INIT_LIST_HEAD(&counter->child_list); |
| 2211 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 2212 | counter->cpu = cpu; |
| 2213 | counter->hw_event = *hw_event; |
| 2214 | counter->wakeup_pending = 0; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2215 | counter->group_leader = group_leader; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2216 | counter->hw_ops = NULL; |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 2217 | counter->ctx = ctx; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2218 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 2219 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Ingo Molnar | a86ed50 | 2008-12-17 00:43:10 +0100 | [diff] [blame] | 2220 | if (hw_event->disabled) |
| 2221 | counter->state = PERF_COUNTER_STATE_OFF; |
| 2222 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2223 | hw_ops = NULL; |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2224 | |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2225 | if (perf_event_raw(hw_event)) { |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2226 | hw_ops = hw_perf_counter_init(counter); |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2227 | goto done; |
| 2228 | } |
| 2229 | |
| 2230 | switch (perf_event_type(hw_event)) { |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2231 | case PERF_TYPE_HARDWARE: |
| 2232 | hw_ops = hw_perf_counter_init(counter); |
| 2233 | break; |
| 2234 | |
| 2235 | case PERF_TYPE_SOFTWARE: |
| 2236 | hw_ops = sw_perf_counter_init(counter); |
| 2237 | break; |
| 2238 | |
| 2239 | case PERF_TYPE_TRACEPOINT: |
| 2240 | hw_ops = tp_perf_counter_init(counter); |
| 2241 | break; |
| 2242 | } |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2243 | |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2244 | if (!hw_ops) { |
| 2245 | kfree(counter); |
| 2246 | return NULL; |
| 2247 | } |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2248 | done: |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2249 | counter->hw_ops = hw_ops; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2250 | |
| 2251 | return counter; |
| 2252 | } |
| 2253 | |
| 2254 | /** |
Paul Mackerras | 2743a5b | 2009-03-04 20:36:51 +1100 | [diff] [blame] | 2255 | * 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] | 2256 | * |
| 2257 | * @hw_event_uptr: event type attributes for monitoring/sampling |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2258 | * @pid: target pid |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 2259 | * @cpu: target cpu |
| 2260 | * @group_fd: group leader counter fd |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2261 | */ |
Paul Mackerras | 2743a5b | 2009-03-04 20:36:51 +1100 | [diff] [blame] | 2262 | SYSCALL_DEFINE5(perf_counter_open, |
Paul Mackerras | f3dfd26 | 2009-02-26 22:43:46 +1100 | [diff] [blame] | 2263 | const struct perf_counter_hw_event __user *, hw_event_uptr, |
Paul Mackerras | 2743a5b | 2009-03-04 20:36:51 +1100 | [diff] [blame] | 2264 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2265 | { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2266 | struct perf_counter *counter, *group_leader; |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 2267 | struct perf_counter_hw_event hw_event; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2268 | struct perf_counter_context *ctx; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2269 | struct file *counter_file = NULL; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2270 | struct file *group_file = NULL; |
| 2271 | int fput_needed = 0; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2272 | int fput_needed2 = 0; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2273 | int ret; |
| 2274 | |
Paul Mackerras | 2743a5b | 2009-03-04 20:36:51 +1100 | [diff] [blame] | 2275 | /* for future expandability... */ |
| 2276 | if (flags) |
| 2277 | return -EINVAL; |
| 2278 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 2279 | 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] | 2280 | return -EFAULT; |
| 2281 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2282 | /* |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 2283 | * Get the target context (task or percpu): |
| 2284 | */ |
| 2285 | ctx = find_get_context(pid, cpu); |
| 2286 | if (IS_ERR(ctx)) |
| 2287 | return PTR_ERR(ctx); |
| 2288 | |
| 2289 | /* |
| 2290 | * Look up the group leader (we will attach this counter to it): |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2291 | */ |
| 2292 | group_leader = NULL; |
| 2293 | if (group_fd != -1) { |
| 2294 | ret = -EINVAL; |
| 2295 | group_file = fget_light(group_fd, &fput_needed); |
| 2296 | if (!group_file) |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 2297 | goto err_put_context; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2298 | if (group_file->f_op != &perf_fops) |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 2299 | goto err_put_context; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2300 | |
| 2301 | group_leader = group_file->private_data; |
| 2302 | /* |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 2303 | * Do not allow a recursive hierarchy (this new sibling |
| 2304 | * becoming part of another group-sibling): |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2305 | */ |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 2306 | if (group_leader->group_leader != group_leader) |
| 2307 | goto err_put_context; |
| 2308 | /* |
| 2309 | * Do not allow to attach to a group in a different |
| 2310 | * task or CPU context: |
| 2311 | */ |
| 2312 | if (group_leader->ctx != ctx) |
| 2313 | goto err_put_context; |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 2314 | /* |
| 2315 | * Only a group leader can be exclusive or pinned |
| 2316 | */ |
| 2317 | if (hw_event.exclusive || hw_event.pinned) |
| 2318 | goto err_put_context; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2319 | } |
| 2320 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2321 | ret = -EINVAL; |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 2322 | counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader, |
| 2323 | GFP_KERNEL); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2324 | if (!counter) |
| 2325 | goto err_put_context; |
| 2326 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2327 | ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0); |
| 2328 | if (ret < 0) |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2329 | goto err_free_put_context; |
| 2330 | |
| 2331 | counter_file = fget_light(ret, &fput_needed2); |
| 2332 | if (!counter_file) |
| 2333 | goto err_free_put_context; |
| 2334 | |
| 2335 | counter->filp = counter_file; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2336 | mutex_lock(&ctx->mutex); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2337 | perf_install_in_context(ctx, counter, cpu); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2338 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2339 | |
| 2340 | fput_light(counter_file, fput_needed2); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2341 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2342 | out_fput: |
| 2343 | fput_light(group_file, fput_needed); |
| 2344 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2345 | return ret; |
| 2346 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2347 | err_free_put_context: |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2348 | kfree(counter); |
| 2349 | |
| 2350 | err_put_context: |
| 2351 | put_context(ctx); |
| 2352 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2353 | goto out_fput; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2354 | } |
| 2355 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2356 | /* |
| 2357 | * Initialize the perf_counter context in a task_struct: |
| 2358 | */ |
| 2359 | static void |
| 2360 | __perf_counter_init_context(struct perf_counter_context *ctx, |
| 2361 | struct task_struct *task) |
| 2362 | { |
| 2363 | memset(ctx, 0, sizeof(*ctx)); |
| 2364 | spin_lock_init(&ctx->lock); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2365 | mutex_init(&ctx->mutex); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2366 | INIT_LIST_HEAD(&ctx->counter_list); |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 2367 | INIT_LIST_HEAD(&ctx->event_list); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2368 | ctx->task = task; |
| 2369 | } |
| 2370 | |
| 2371 | /* |
| 2372 | * inherit a counter from parent task to child task: |
| 2373 | */ |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2374 | static struct perf_counter * |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2375 | inherit_counter(struct perf_counter *parent_counter, |
| 2376 | struct task_struct *parent, |
| 2377 | struct perf_counter_context *parent_ctx, |
| 2378 | struct task_struct *child, |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2379 | struct perf_counter *group_leader, |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2380 | struct perf_counter_context *child_ctx) |
| 2381 | { |
| 2382 | struct perf_counter *child_counter; |
| 2383 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2384 | /* |
| 2385 | * Instead of creating recursive hierarchies of counters, |
| 2386 | * we link inherited counters back to the original parent, |
| 2387 | * which has a filp for sure, which we use as the reference |
| 2388 | * count: |
| 2389 | */ |
| 2390 | if (parent_counter->parent) |
| 2391 | parent_counter = parent_counter->parent; |
| 2392 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2393 | child_counter = perf_counter_alloc(&parent_counter->hw_event, |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 2394 | parent_counter->cpu, child_ctx, |
| 2395 | group_leader, GFP_KERNEL); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2396 | if (!child_counter) |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2397 | return NULL; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2398 | |
| 2399 | /* |
| 2400 | * Link it up in the child's context: |
| 2401 | */ |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2402 | child_counter->task = child; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 2403 | add_counter_to_ctx(child_counter, child_ctx); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2404 | |
| 2405 | child_counter->parent = parent_counter; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2406 | /* |
| 2407 | * inherit into child's child as well: |
| 2408 | */ |
| 2409 | child_counter->hw_event.inherit = 1; |
| 2410 | |
| 2411 | /* |
| 2412 | * Get a reference to the parent filp - we will fput it |
| 2413 | * when the child counter exits. This is safe to do because |
| 2414 | * we are in the parent and we know that the filp still |
| 2415 | * exists and has a nonzero count: |
| 2416 | */ |
| 2417 | atomic_long_inc(&parent_counter->filp->f_count); |
| 2418 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2419 | /* |
| 2420 | * Link this into the parent counter's child list |
| 2421 | */ |
| 2422 | mutex_lock(&parent_counter->mutex); |
| 2423 | list_add_tail(&child_counter->child_list, &parent_counter->child_list); |
| 2424 | |
| 2425 | /* |
| 2426 | * Make the child state follow the state of the parent counter, |
| 2427 | * not its hw_event.disabled bit. We hold the parent's mutex, |
| 2428 | * so we won't race with perf_counter_{en,dis}able_family. |
| 2429 | */ |
| 2430 | if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE) |
| 2431 | child_counter->state = PERF_COUNTER_STATE_INACTIVE; |
| 2432 | else |
| 2433 | child_counter->state = PERF_COUNTER_STATE_OFF; |
| 2434 | |
| 2435 | mutex_unlock(&parent_counter->mutex); |
| 2436 | |
| 2437 | return child_counter; |
| 2438 | } |
| 2439 | |
| 2440 | static int inherit_group(struct perf_counter *parent_counter, |
| 2441 | struct task_struct *parent, |
| 2442 | struct perf_counter_context *parent_ctx, |
| 2443 | struct task_struct *child, |
| 2444 | struct perf_counter_context *child_ctx) |
| 2445 | { |
| 2446 | struct perf_counter *leader; |
| 2447 | struct perf_counter *sub; |
| 2448 | |
| 2449 | leader = inherit_counter(parent_counter, parent, parent_ctx, |
| 2450 | child, NULL, child_ctx); |
| 2451 | if (!leader) |
| 2452 | return -ENOMEM; |
| 2453 | list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) { |
| 2454 | if (!inherit_counter(sub, parent, parent_ctx, |
| 2455 | child, leader, child_ctx)) |
| 2456 | return -ENOMEM; |
| 2457 | } |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2458 | return 0; |
| 2459 | } |
| 2460 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2461 | static void sync_child_counter(struct perf_counter *child_counter, |
| 2462 | struct perf_counter *parent_counter) |
| 2463 | { |
| 2464 | u64 parent_val, child_val; |
| 2465 | |
| 2466 | parent_val = atomic64_read(&parent_counter->count); |
| 2467 | child_val = atomic64_read(&child_counter->count); |
| 2468 | |
| 2469 | /* |
| 2470 | * Add back the child's count to the parent's count: |
| 2471 | */ |
| 2472 | atomic64_add(child_val, &parent_counter->count); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 2473 | atomic64_add(child_counter->total_time_enabled, |
| 2474 | &parent_counter->child_total_time_enabled); |
| 2475 | atomic64_add(child_counter->total_time_running, |
| 2476 | &parent_counter->child_total_time_running); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2477 | |
| 2478 | /* |
| 2479 | * Remove this counter from the parent's list |
| 2480 | */ |
| 2481 | mutex_lock(&parent_counter->mutex); |
| 2482 | list_del_init(&child_counter->child_list); |
| 2483 | mutex_unlock(&parent_counter->mutex); |
| 2484 | |
| 2485 | /* |
| 2486 | * Release the parent counter, if this was the last |
| 2487 | * reference to it. |
| 2488 | */ |
| 2489 | fput(parent_counter->filp); |
| 2490 | } |
| 2491 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2492 | static void |
| 2493 | __perf_counter_exit_task(struct task_struct *child, |
| 2494 | struct perf_counter *child_counter, |
| 2495 | struct perf_counter_context *child_ctx) |
| 2496 | { |
| 2497 | struct perf_counter *parent_counter; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2498 | struct perf_counter *sub, *tmp; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2499 | |
| 2500 | /* |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 2501 | * If we do not self-reap then we have to wait for the |
| 2502 | * child task to unschedule (it will happen for sure), |
| 2503 | * so that its counter is at its final count. (This |
| 2504 | * condition triggers rarely - child tasks usually get |
| 2505 | * off their CPU before the parent has a chance to |
| 2506 | * get this far into the reaping action) |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2507 | */ |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 2508 | if (child != current) { |
| 2509 | wait_task_inactive(child, 0); |
| 2510 | list_del_init(&child_counter->list_entry); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 2511 | update_counter_times(child_counter); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 2512 | } else { |
Ingo Molnar | 0cc0c02 | 2008-12-14 23:20:36 +0100 | [diff] [blame] | 2513 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 2514 | unsigned long flags; |
| 2515 | u64 perf_flags; |
| 2516 | |
| 2517 | /* |
| 2518 | * Disable and unlink this counter. |
| 2519 | * |
| 2520 | * Be careful about zapping the list - IRQ/NMI context |
| 2521 | * could still be processing it: |
| 2522 | */ |
| 2523 | curr_rq_lock_irq_save(&flags); |
| 2524 | perf_flags = hw_perf_save_disable(); |
Ingo Molnar | 0cc0c02 | 2008-12-14 23:20:36 +0100 | [diff] [blame] | 2525 | |
| 2526 | cpuctx = &__get_cpu_var(perf_cpu_context); |
| 2527 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2528 | group_sched_out(child_counter, cpuctx, child_ctx); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame^] | 2529 | update_counter_times(child_counter); |
Ingo Molnar | 0cc0c02 | 2008-12-14 23:20:36 +0100 | [diff] [blame] | 2530 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 2531 | list_del_init(&child_counter->list_entry); |
| 2532 | |
| 2533 | child_ctx->nr_counters--; |
| 2534 | |
| 2535 | hw_perf_restore(perf_flags); |
| 2536 | curr_rq_unlock_irq_restore(&flags); |
Ingo Molnar | 0cc0c02 | 2008-12-14 23:20:36 +0100 | [diff] [blame] | 2537 | } |
| 2538 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2539 | parent_counter = child_counter->parent; |
| 2540 | /* |
| 2541 | * It can happen that parent exits first, and has counters |
| 2542 | * that are still around due to the child reference. These |
| 2543 | * counters need to be zapped - but otherwise linger. |
| 2544 | */ |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2545 | if (parent_counter) { |
| 2546 | sync_child_counter(child_counter, parent_counter); |
| 2547 | list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list, |
| 2548 | list_entry) { |
Paul Mackerras | 4bcf349 | 2009-02-11 13:53:19 +0100 | [diff] [blame] | 2549 | if (sub->parent) { |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2550 | sync_child_counter(sub, sub->parent); |
Peter Zijlstra | f160095 | 2009-03-19 20:26:16 +0100 | [diff] [blame] | 2551 | free_counter(sub); |
Paul Mackerras | 4bcf349 | 2009-02-11 13:53:19 +0100 | [diff] [blame] | 2552 | } |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2553 | } |
Peter Zijlstra | f160095 | 2009-03-19 20:26:16 +0100 | [diff] [blame] | 2554 | free_counter(child_counter); |
Paul Mackerras | 4bcf349 | 2009-02-11 13:53:19 +0100 | [diff] [blame] | 2555 | } |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2556 | } |
| 2557 | |
| 2558 | /* |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2559 | * When a child task exits, feed back counter values to parent counters. |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2560 | * |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2561 | * 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] | 2562 | * anymore so new counters will not be added. |
| 2563 | */ |
| 2564 | void perf_counter_exit_task(struct task_struct *child) |
| 2565 | { |
| 2566 | struct perf_counter *child_counter, *tmp; |
| 2567 | struct perf_counter_context *child_ctx; |
| 2568 | |
| 2569 | child_ctx = &child->perf_counter_ctx; |
| 2570 | |
| 2571 | if (likely(!child_ctx->nr_counters)) |
| 2572 | return; |
| 2573 | |
| 2574 | list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list, |
| 2575 | list_entry) |
| 2576 | __perf_counter_exit_task(child, child_counter, child_ctx); |
| 2577 | } |
| 2578 | |
| 2579 | /* |
| 2580 | * Initialize the perf_counter context in task_struct |
| 2581 | */ |
| 2582 | void perf_counter_init_task(struct task_struct *child) |
| 2583 | { |
| 2584 | struct perf_counter_context *child_ctx, *parent_ctx; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2585 | struct perf_counter *counter; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2586 | struct task_struct *parent = current; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2587 | |
| 2588 | child_ctx = &child->perf_counter_ctx; |
| 2589 | parent_ctx = &parent->perf_counter_ctx; |
| 2590 | |
| 2591 | __perf_counter_init_context(child_ctx, child); |
| 2592 | |
| 2593 | /* |
| 2594 | * This is executed from the parent task context, so inherit |
| 2595 | * counters that have been marked for cloning: |
| 2596 | */ |
| 2597 | |
| 2598 | if (likely(!parent_ctx->nr_counters)) |
| 2599 | return; |
| 2600 | |
| 2601 | /* |
| 2602 | * Lock the parent list. No need to lock the child - not PID |
| 2603 | * hashed yet and not running, so nobody can access it. |
| 2604 | */ |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2605 | mutex_lock(&parent_ctx->mutex); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2606 | |
| 2607 | /* |
| 2608 | * We dont have to disable NMIs - we are only looking at |
| 2609 | * the list, not manipulating it: |
| 2610 | */ |
| 2611 | list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) { |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2612 | if (!counter->hw_event.inherit) |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2613 | continue; |
| 2614 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2615 | if (inherit_group(counter, parent, |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2616 | parent_ctx, child, child_ctx)) |
| 2617 | break; |
| 2618 | } |
| 2619 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2620 | mutex_unlock(&parent_ctx->mutex); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2621 | } |
| 2622 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2623 | static void __cpuinit perf_counter_init_cpu(int cpu) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2624 | { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2625 | struct perf_cpu_context *cpuctx; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2626 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2627 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 2628 | __perf_counter_init_context(&cpuctx->ctx, NULL); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2629 | |
| 2630 | mutex_lock(&perf_resource_mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2631 | cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2632 | mutex_unlock(&perf_resource_mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2633 | |
Paul Mackerras | 01d0287 | 2009-01-14 13:44:19 +1100 | [diff] [blame] | 2634 | hw_perf_counter_setup(cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2635 | } |
| 2636 | |
| 2637 | #ifdef CONFIG_HOTPLUG_CPU |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2638 | static void __perf_counter_exit_cpu(void *info) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2639 | { |
| 2640 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 2641 | struct perf_counter_context *ctx = &cpuctx->ctx; |
| 2642 | struct perf_counter *counter, *tmp; |
| 2643 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2644 | list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) |
| 2645 | __perf_counter_remove_from_context(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2646 | } |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2647 | static void perf_counter_exit_cpu(int cpu) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2648 | { |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2649 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 2650 | struct perf_counter_context *ctx = &cpuctx->ctx; |
| 2651 | |
| 2652 | mutex_lock(&ctx->mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2653 | smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2654 | mutex_unlock(&ctx->mutex); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2655 | } |
| 2656 | #else |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2657 | static inline void perf_counter_exit_cpu(int cpu) { } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2658 | #endif |
| 2659 | |
| 2660 | static int __cpuinit |
| 2661 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
| 2662 | { |
| 2663 | unsigned int cpu = (long)hcpu; |
| 2664 | |
| 2665 | switch (action) { |
| 2666 | |
| 2667 | case CPU_UP_PREPARE: |
| 2668 | case CPU_UP_PREPARE_FROZEN: |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2669 | perf_counter_init_cpu(cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2670 | break; |
| 2671 | |
| 2672 | case CPU_DOWN_PREPARE: |
| 2673 | case CPU_DOWN_PREPARE_FROZEN: |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2674 | perf_counter_exit_cpu(cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2675 | break; |
| 2676 | |
| 2677 | default: |
| 2678 | break; |
| 2679 | } |
| 2680 | |
| 2681 | return NOTIFY_OK; |
| 2682 | } |
| 2683 | |
| 2684 | static struct notifier_block __cpuinitdata perf_cpu_nb = { |
| 2685 | .notifier_call = perf_cpu_notify, |
| 2686 | }; |
| 2687 | |
| 2688 | static int __init perf_counter_init(void) |
| 2689 | { |
| 2690 | perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE, |
| 2691 | (void *)(long)smp_processor_id()); |
| 2692 | register_cpu_notifier(&perf_cpu_nb); |
| 2693 | |
| 2694 | return 0; |
| 2695 | } |
| 2696 | early_initcall(perf_counter_init); |
| 2697 | |
| 2698 | static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf) |
| 2699 | { |
| 2700 | return sprintf(buf, "%d\n", perf_reserved_percpu); |
| 2701 | } |
| 2702 | |
| 2703 | static ssize_t |
| 2704 | perf_set_reserve_percpu(struct sysdev_class *class, |
| 2705 | const char *buf, |
| 2706 | size_t count) |
| 2707 | { |
| 2708 | struct perf_cpu_context *cpuctx; |
| 2709 | unsigned long val; |
| 2710 | int err, cpu, mpt; |
| 2711 | |
| 2712 | err = strict_strtoul(buf, 10, &val); |
| 2713 | if (err) |
| 2714 | return err; |
| 2715 | if (val > perf_max_counters) |
| 2716 | return -EINVAL; |
| 2717 | |
| 2718 | mutex_lock(&perf_resource_mutex); |
| 2719 | perf_reserved_percpu = val; |
| 2720 | for_each_online_cpu(cpu) { |
| 2721 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 2722 | spin_lock_irq(&cpuctx->ctx.lock); |
| 2723 | mpt = min(perf_max_counters - cpuctx->ctx.nr_counters, |
| 2724 | perf_max_counters - perf_reserved_percpu); |
| 2725 | cpuctx->max_pertask = mpt; |
| 2726 | spin_unlock_irq(&cpuctx->ctx.lock); |
| 2727 | } |
| 2728 | mutex_unlock(&perf_resource_mutex); |
| 2729 | |
| 2730 | return count; |
| 2731 | } |
| 2732 | |
| 2733 | static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf) |
| 2734 | { |
| 2735 | return sprintf(buf, "%d\n", perf_overcommit); |
| 2736 | } |
| 2737 | |
| 2738 | static ssize_t |
| 2739 | perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count) |
| 2740 | { |
| 2741 | unsigned long val; |
| 2742 | int err; |
| 2743 | |
| 2744 | err = strict_strtoul(buf, 10, &val); |
| 2745 | if (err) |
| 2746 | return err; |
| 2747 | if (val > 1) |
| 2748 | return -EINVAL; |
| 2749 | |
| 2750 | mutex_lock(&perf_resource_mutex); |
| 2751 | perf_overcommit = val; |
| 2752 | mutex_unlock(&perf_resource_mutex); |
| 2753 | |
| 2754 | return count; |
| 2755 | } |
| 2756 | |
| 2757 | static SYSDEV_CLASS_ATTR( |
| 2758 | reserve_percpu, |
| 2759 | 0644, |
| 2760 | perf_show_reserve_percpu, |
| 2761 | perf_set_reserve_percpu |
| 2762 | ); |
| 2763 | |
| 2764 | static SYSDEV_CLASS_ATTR( |
| 2765 | overcommit, |
| 2766 | 0644, |
| 2767 | perf_show_overcommit, |
| 2768 | perf_set_overcommit |
| 2769 | ); |
| 2770 | |
| 2771 | static struct attribute *perfclass_attrs[] = { |
| 2772 | &attr_reserve_percpu.attr, |
| 2773 | &attr_overcommit.attr, |
| 2774 | NULL |
| 2775 | }; |
| 2776 | |
| 2777 | static struct attribute_group perfclass_attr_group = { |
| 2778 | .attrs = perfclass_attrs, |
| 2779 | .name = "perf_counters", |
| 2780 | }; |
| 2781 | |
| 2782 | static int __init perf_counter_sysfs_init(void) |
| 2783 | { |
| 2784 | return sysfs_create_group(&cpu_sysdev_class.kset.kobj, |
| 2785 | &perfclass_attr_group); |
| 2786 | } |
| 2787 | device_initcall(perf_counter_sysfs_init); |