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