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