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