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