Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1 | /* |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 2 | * Performance events core code: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3 | * |
| 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> |
| 7 | * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> |
| 8 | * |
Ingo Molnar | 57c0c15 | 2009-09-21 12:20:38 +0200 | [diff] [blame] | 9 | * For licensing details see kernel-base/COPYING |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/mm.h> |
| 14 | #include <linux/cpu.h> |
| 15 | #include <linux/smp.h> |
| 16 | #include <linux/file.h> |
| 17 | #include <linux/poll.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 19 | #include <linux/hash.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 20 | #include <linux/sysfs.h> |
| 21 | #include <linux/dcache.h> |
| 22 | #include <linux/percpu.h> |
| 23 | #include <linux/ptrace.h> |
| 24 | #include <linux/vmstat.h> |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 25 | #include <linux/vmalloc.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 26 | #include <linux/hardirq.h> |
| 27 | #include <linux/rculist.h> |
| 28 | #include <linux/uaccess.h> |
| 29 | #include <linux/syscalls.h> |
| 30 | #include <linux/anon_inodes.h> |
| 31 | #include <linux/kernel_stat.h> |
| 32 | #include <linux/perf_event.h> |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 33 | #include <linux/ftrace_event.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 34 | |
| 35 | #include <asm/irq_regs.h> |
| 36 | |
| 37 | /* |
| 38 | * Each CPU has a list of per CPU events: |
| 39 | */ |
Xiao Guangrong | aa5452d | 2009-12-09 11:28:13 +0800 | [diff] [blame] | 40 | static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 41 | |
| 42 | int perf_max_events __read_mostly = 1; |
| 43 | static int perf_reserved_percpu __read_mostly; |
| 44 | static int perf_overcommit __read_mostly = 1; |
| 45 | |
| 46 | static atomic_t nr_events __read_mostly; |
| 47 | static atomic_t nr_mmap_events __read_mostly; |
| 48 | static atomic_t nr_comm_events __read_mostly; |
| 49 | static atomic_t nr_task_events __read_mostly; |
| 50 | |
| 51 | /* |
| 52 | * perf event paranoia level: |
| 53 | * -1 - not paranoid at all |
| 54 | * 0 - disallow raw tracepoint access for unpriv |
| 55 | * 1 - disallow cpu events for unpriv |
| 56 | * 2 - disallow kernel profiling for unpriv |
| 57 | */ |
| 58 | int sysctl_perf_event_paranoid __read_mostly = 1; |
| 59 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 60 | int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */ |
| 61 | |
| 62 | /* |
| 63 | * max perf event sample rate |
| 64 | */ |
| 65 | int sysctl_perf_event_sample_rate __read_mostly = 100000; |
| 66 | |
| 67 | static atomic64_t perf_event_id; |
| 68 | |
| 69 | /* |
| 70 | * Lock for (sysadmin-configurable) event reservations: |
| 71 | */ |
| 72 | static DEFINE_SPINLOCK(perf_resource_lock); |
| 73 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 74 | void __weak hw_perf_disable(void) { barrier(); } |
| 75 | void __weak hw_perf_enable(void) { barrier(); } |
| 76 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 77 | void __weak perf_event_print_debug(void) { } |
| 78 | |
| 79 | static DEFINE_PER_CPU(int, perf_disable_count); |
| 80 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 81 | void perf_disable(void) |
| 82 | { |
Peter Zijlstra | 32975a4 | 2010-03-06 19:49:19 +0100 | [diff] [blame] | 83 | if (!__get_cpu_var(perf_disable_count)++) |
| 84 | hw_perf_disable(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void perf_enable(void) |
| 88 | { |
Peter Zijlstra | 32975a4 | 2010-03-06 19:49:19 +0100 | [diff] [blame] | 89 | if (!--__get_cpu_var(perf_disable_count)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 90 | hw_perf_enable(); |
| 91 | } |
| 92 | |
| 93 | static void get_ctx(struct perf_event_context *ctx) |
| 94 | { |
| 95 | WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); |
| 96 | } |
| 97 | |
| 98 | static void free_ctx(struct rcu_head *head) |
| 99 | { |
| 100 | struct perf_event_context *ctx; |
| 101 | |
| 102 | ctx = container_of(head, struct perf_event_context, rcu_head); |
| 103 | kfree(ctx); |
| 104 | } |
| 105 | |
| 106 | static void put_ctx(struct perf_event_context *ctx) |
| 107 | { |
| 108 | if (atomic_dec_and_test(&ctx->refcount)) { |
| 109 | if (ctx->parent_ctx) |
| 110 | put_ctx(ctx->parent_ctx); |
| 111 | if (ctx->task) |
| 112 | put_task_struct(ctx->task); |
| 113 | call_rcu(&ctx->rcu_head, free_ctx); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static void unclone_ctx(struct perf_event_context *ctx) |
| 118 | { |
| 119 | if (ctx->parent_ctx) { |
| 120 | put_ctx(ctx->parent_ctx); |
| 121 | ctx->parent_ctx = NULL; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * If we inherit events we want to return the parent event id |
| 127 | * to userspace. |
| 128 | */ |
| 129 | static u64 primary_event_id(struct perf_event *event) |
| 130 | { |
| 131 | u64 id = event->id; |
| 132 | |
| 133 | if (event->parent) |
| 134 | id = event->parent->id; |
| 135 | |
| 136 | return id; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Get the perf_event_context for a task and lock it. |
| 141 | * This has to cope with with the fact that until it is locked, |
| 142 | * the context could get moved to another task. |
| 143 | */ |
| 144 | static struct perf_event_context * |
| 145 | perf_lock_task_context(struct task_struct *task, unsigned long *flags) |
| 146 | { |
| 147 | struct perf_event_context *ctx; |
| 148 | |
| 149 | rcu_read_lock(); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 150 | retry: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 151 | ctx = rcu_dereference(task->perf_event_ctxp); |
| 152 | if (ctx) { |
| 153 | /* |
| 154 | * If this context is a clone of another, it might |
| 155 | * get swapped for another underneath us by |
| 156 | * perf_event_task_sched_out, though the |
| 157 | * rcu_read_lock() protects us from any context |
| 158 | * getting freed. Lock the context and check if it |
| 159 | * got swapped before we could get the lock, and retry |
| 160 | * if so. If we locked the right context, then it |
| 161 | * can't get swapped on us any more. |
| 162 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 163 | raw_spin_lock_irqsave(&ctx->lock, *flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 164 | if (ctx != rcu_dereference(task->perf_event_ctxp)) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 165 | raw_spin_unlock_irqrestore(&ctx->lock, *flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 166 | goto retry; |
| 167 | } |
| 168 | |
| 169 | if (!atomic_inc_not_zero(&ctx->refcount)) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 170 | raw_spin_unlock_irqrestore(&ctx->lock, *flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 171 | ctx = NULL; |
| 172 | } |
| 173 | } |
| 174 | rcu_read_unlock(); |
| 175 | return ctx; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Get the context for a task and increment its pin_count so it |
| 180 | * can't get swapped to another task. This also increments its |
| 181 | * reference count so that the context can't get freed. |
| 182 | */ |
| 183 | static struct perf_event_context *perf_pin_task_context(struct task_struct *task) |
| 184 | { |
| 185 | struct perf_event_context *ctx; |
| 186 | unsigned long flags; |
| 187 | |
| 188 | ctx = perf_lock_task_context(task, &flags); |
| 189 | if (ctx) { |
| 190 | ++ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 191 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 192 | } |
| 193 | return ctx; |
| 194 | } |
| 195 | |
| 196 | static void perf_unpin_context(struct perf_event_context *ctx) |
| 197 | { |
| 198 | unsigned long flags; |
| 199 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 200 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 201 | --ctx->pin_count; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 202 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 203 | put_ctx(ctx); |
| 204 | } |
| 205 | |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 206 | static inline u64 perf_clock(void) |
| 207 | { |
Peter Zijlstra | c676329 | 2010-05-25 10:48:51 +0200 | [diff] [blame] | 208 | return local_clock(); |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Update the record of the current time in a context. |
| 213 | */ |
| 214 | static void update_context_time(struct perf_event_context *ctx) |
| 215 | { |
| 216 | u64 now = perf_clock(); |
| 217 | |
| 218 | ctx->time += now - ctx->timestamp; |
| 219 | ctx->timestamp = now; |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Update the total_time_enabled and total_time_running fields for a event. |
| 224 | */ |
| 225 | static void update_event_times(struct perf_event *event) |
| 226 | { |
| 227 | struct perf_event_context *ctx = event->ctx; |
| 228 | u64 run_end; |
| 229 | |
| 230 | if (event->state < PERF_EVENT_STATE_INACTIVE || |
| 231 | event->group_leader->state < PERF_EVENT_STATE_INACTIVE) |
| 232 | return; |
| 233 | |
Peter Zijlstra | acd1d7c | 2009-11-23 15:00:36 +0100 | [diff] [blame] | 234 | if (ctx->is_active) |
| 235 | run_end = ctx->time; |
| 236 | else |
| 237 | run_end = event->tstamp_stopped; |
| 238 | |
| 239 | event->total_time_enabled = run_end - event->tstamp_enabled; |
Peter Zijlstra | f67218c | 2009-11-23 11:37:27 +0100 | [diff] [blame] | 240 | |
| 241 | if (event->state == PERF_EVENT_STATE_INACTIVE) |
| 242 | run_end = event->tstamp_stopped; |
| 243 | else |
| 244 | run_end = ctx->time; |
| 245 | |
| 246 | event->total_time_running = run_end - event->tstamp_running; |
| 247 | } |
| 248 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 249 | /* |
| 250 | * Update total_time_enabled and total_time_running for all events in a group. |
| 251 | */ |
| 252 | static void update_group_times(struct perf_event *leader) |
| 253 | { |
| 254 | struct perf_event *event; |
| 255 | |
| 256 | update_event_times(leader); |
| 257 | list_for_each_entry(event, &leader->sibling_list, group_entry) |
| 258 | update_event_times(event); |
| 259 | } |
| 260 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 261 | static struct list_head * |
| 262 | ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) |
| 263 | { |
| 264 | if (event->attr.pinned) |
| 265 | return &ctx->pinned_groups; |
| 266 | else |
| 267 | return &ctx->flexible_groups; |
| 268 | } |
| 269 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 270 | /* |
| 271 | * Add a event from the lists for its context. |
| 272 | * Must be called with ctx->mutex and ctx->lock held. |
| 273 | */ |
| 274 | static void |
| 275 | list_add_event(struct perf_event *event, struct perf_event_context *ctx) |
| 276 | { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 277 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); |
| 278 | event->attach_state |= PERF_ATTACH_CONTEXT; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 279 | |
| 280 | /* |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 281 | * If we're a stand alone event or group leader, we go to the context |
| 282 | * list, group events are kept attached to the group so that |
| 283 | * perf_group_detach can, at all times, locate all siblings. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 284 | */ |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 285 | if (event->group_leader == event) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 286 | struct list_head *list; |
| 287 | |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 288 | if (is_software_event(event)) |
| 289 | event->group_flags |= PERF_GROUP_SOFTWARE; |
| 290 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 291 | list = ctx_group_list(event, ctx); |
| 292 | list_add_tail(&event->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | list_add_rcu(&event->event_entry, &ctx->event_list); |
| 296 | ctx->nr_events++; |
| 297 | if (event->attr.inherit_stat) |
| 298 | ctx->nr_stat++; |
| 299 | } |
| 300 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 301 | static void perf_group_attach(struct perf_event *event) |
| 302 | { |
| 303 | struct perf_event *group_leader = event->group_leader; |
| 304 | |
| 305 | WARN_ON_ONCE(event->attach_state & PERF_ATTACH_GROUP); |
| 306 | event->attach_state |= PERF_ATTACH_GROUP; |
| 307 | |
| 308 | if (group_leader == event) |
| 309 | return; |
| 310 | |
| 311 | if (group_leader->group_flags & PERF_GROUP_SOFTWARE && |
| 312 | !is_software_event(event)) |
| 313 | group_leader->group_flags &= ~PERF_GROUP_SOFTWARE; |
| 314 | |
| 315 | list_add_tail(&event->group_entry, &group_leader->sibling_list); |
| 316 | group_leader->nr_siblings++; |
| 317 | } |
| 318 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 319 | /* |
| 320 | * Remove a event from the lists for its context. |
| 321 | * Must be called with ctx->mutex and ctx->lock held. |
| 322 | */ |
| 323 | static void |
| 324 | list_del_event(struct perf_event *event, struct perf_event_context *ctx) |
| 325 | { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 326 | /* |
| 327 | * We can have double detach due to exit/hot-unplug + close. |
| 328 | */ |
| 329 | if (!(event->attach_state & PERF_ATTACH_CONTEXT)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 330 | return; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 331 | |
| 332 | event->attach_state &= ~PERF_ATTACH_CONTEXT; |
| 333 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 334 | ctx->nr_events--; |
| 335 | if (event->attr.inherit_stat) |
| 336 | ctx->nr_stat--; |
| 337 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 338 | list_del_rcu(&event->event_entry); |
| 339 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 340 | if (event->group_leader == event) |
| 341 | list_del_init(&event->group_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 342 | |
Peter Zijlstra | 96c21a4 | 2010-05-11 16:19:10 +0200 | [diff] [blame] | 343 | update_group_times(event); |
Stephane Eranian | b2e74a2 | 2009-11-26 09:24:30 -0800 | [diff] [blame] | 344 | |
| 345 | /* |
| 346 | * If event was in error state, then keep it |
| 347 | * that way, otherwise bogus counts will be |
| 348 | * returned on read(). The only way to get out |
| 349 | * of error state is by explicit re-enabling |
| 350 | * of the event |
| 351 | */ |
| 352 | if (event->state > PERF_EVENT_STATE_OFF) |
| 353 | event->state = PERF_EVENT_STATE_OFF; |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 356 | static void perf_group_detach(struct perf_event *event) |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 357 | { |
| 358 | struct perf_event *sibling, *tmp; |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 359 | struct list_head *list = NULL; |
| 360 | |
| 361 | /* |
| 362 | * We can have double detach due to exit/hot-unplug + close. |
| 363 | */ |
| 364 | if (!(event->attach_state & PERF_ATTACH_GROUP)) |
| 365 | return; |
| 366 | |
| 367 | event->attach_state &= ~PERF_ATTACH_GROUP; |
| 368 | |
| 369 | /* |
| 370 | * If this is a sibling, remove it from its group. |
| 371 | */ |
| 372 | if (event->group_leader != event) { |
| 373 | list_del_init(&event->group_entry); |
| 374 | event->group_leader->nr_siblings--; |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | if (!list_empty(&event->group_entry)) |
| 379 | list = &event->group_entry; |
Peter Zijlstra | 2e2af50 | 2009-11-23 11:37:25 +0100 | [diff] [blame] | 380 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 381 | /* |
| 382 | * If this was a group event with sibling events then |
| 383 | * upgrade the siblings to singleton events by adding them |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 384 | * to whatever list we are on. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 385 | */ |
| 386 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 387 | if (list) |
| 388 | list_move_tail(&sibling->group_entry, list); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 389 | sibling->group_leader = sibling; |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 390 | |
| 391 | /* Inherit group flags from the previous leader */ |
| 392 | sibling->group_flags = event->group_flags; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 396 | static inline int |
| 397 | event_filter_match(struct perf_event *event) |
| 398 | { |
| 399 | return event->cpu == -1 || event->cpu == smp_processor_id(); |
| 400 | } |
| 401 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 402 | static void |
| 403 | event_sched_out(struct perf_event *event, |
| 404 | struct perf_cpu_context *cpuctx, |
| 405 | struct perf_event_context *ctx) |
| 406 | { |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 407 | u64 delta; |
| 408 | /* |
| 409 | * An event which could not be activated because of |
| 410 | * filter mismatch still needs to have its timings |
| 411 | * maintained, otherwise bogus information is return |
| 412 | * via read() for time_enabled, time_running: |
| 413 | */ |
| 414 | if (event->state == PERF_EVENT_STATE_INACTIVE |
| 415 | && !event_filter_match(event)) { |
| 416 | delta = ctx->time - event->tstamp_stopped; |
| 417 | event->tstamp_running += delta; |
| 418 | event->tstamp_stopped = ctx->time; |
| 419 | } |
| 420 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 421 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 422 | return; |
| 423 | |
| 424 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 425 | if (event->pending_disable) { |
| 426 | event->pending_disable = 0; |
| 427 | event->state = PERF_EVENT_STATE_OFF; |
| 428 | } |
| 429 | event->tstamp_stopped = ctx->time; |
| 430 | event->pmu->disable(event); |
| 431 | event->oncpu = -1; |
| 432 | |
| 433 | if (!is_software_event(event)) |
| 434 | cpuctx->active_oncpu--; |
| 435 | ctx->nr_active--; |
| 436 | if (event->attr.exclusive || !cpuctx->active_oncpu) |
| 437 | cpuctx->exclusive = 0; |
| 438 | } |
| 439 | |
| 440 | static void |
| 441 | group_sched_out(struct perf_event *group_event, |
| 442 | struct perf_cpu_context *cpuctx, |
| 443 | struct perf_event_context *ctx) |
| 444 | { |
| 445 | struct perf_event *event; |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 446 | int state = group_event->state; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 447 | |
| 448 | event_sched_out(group_event, cpuctx, ctx); |
| 449 | |
| 450 | /* |
| 451 | * Schedule out siblings (if any): |
| 452 | */ |
| 453 | list_for_each_entry(event, &group_event->sibling_list, group_entry) |
| 454 | event_sched_out(event, cpuctx, ctx); |
| 455 | |
Stephane Eranian | fa66f07 | 2010-08-26 16:40:01 +0200 | [diff] [blame] | 456 | if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 457 | cpuctx->exclusive = 0; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Cross CPU call to remove a performance event |
| 462 | * |
| 463 | * We disable the event on the hardware level first. After that we |
| 464 | * remove it from the context list. |
| 465 | */ |
| 466 | static void __perf_event_remove_from_context(void *info) |
| 467 | { |
| 468 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 469 | struct perf_event *event = info; |
| 470 | struct perf_event_context *ctx = event->ctx; |
| 471 | |
| 472 | /* |
| 473 | * If this is a task context, we need to check whether it is |
| 474 | * the current task context of this cpu. If not it has been |
| 475 | * scheduled out before the smp call arrived. |
| 476 | */ |
| 477 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 478 | return; |
| 479 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 480 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 481 | |
| 482 | event_sched_out(event, cpuctx, ctx); |
| 483 | |
| 484 | list_del_event(event, ctx); |
| 485 | |
| 486 | if (!ctx->task) { |
| 487 | /* |
| 488 | * Allow more per task events with respect to the |
| 489 | * reservation: |
| 490 | */ |
| 491 | cpuctx->max_pertask = |
| 492 | min(perf_max_events - ctx->nr_events, |
| 493 | perf_max_events - perf_reserved_percpu); |
| 494 | } |
| 495 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 496 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | |
| 500 | /* |
| 501 | * Remove the event from a task's (or a CPU's) list of events. |
| 502 | * |
| 503 | * Must be called with ctx->mutex held. |
| 504 | * |
| 505 | * CPU events are removed with a smp call. For task events we only |
| 506 | * call when the task is on a CPU. |
| 507 | * |
| 508 | * If event->ctx is a cloned context, callers must make sure that |
| 509 | * every task struct that event->ctx->task could possibly point to |
| 510 | * remains valid. This is OK when called from perf_release since |
| 511 | * that only calls us on the top-level context, which can't be a clone. |
| 512 | * When called from perf_event_exit_task, it's OK because the |
| 513 | * context has been detached from its task. |
| 514 | */ |
| 515 | static void perf_event_remove_from_context(struct perf_event *event) |
| 516 | { |
| 517 | struct perf_event_context *ctx = event->ctx; |
| 518 | struct task_struct *task = ctx->task; |
| 519 | |
| 520 | if (!task) { |
| 521 | /* |
| 522 | * Per cpu events are removed via an smp call and |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 523 | * the removal is always successful. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 524 | */ |
| 525 | smp_call_function_single(event->cpu, |
| 526 | __perf_event_remove_from_context, |
| 527 | event, 1); |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | retry: |
| 532 | task_oncpu_function_call(task, __perf_event_remove_from_context, |
| 533 | event); |
| 534 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 535 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 536 | /* |
| 537 | * If the context is active we need to retry the smp call. |
| 538 | */ |
| 539 | if (ctx->nr_active && !list_empty(&event->group_entry)) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 540 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 541 | goto retry; |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * The lock prevents that this context is scheduled in so we |
| 546 | * can remove the event safely, if the call above did not |
| 547 | * succeed. |
| 548 | */ |
Peter Zijlstra | 6c2bfcb | 2009-11-23 11:37:24 +0100 | [diff] [blame] | 549 | if (!list_empty(&event->group_entry)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 550 | list_del_event(event, ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 551 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 552 | } |
| 553 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 554 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 555 | * Cross CPU call to disable a performance event |
| 556 | */ |
| 557 | static void __perf_event_disable(void *info) |
| 558 | { |
| 559 | struct perf_event *event = info; |
| 560 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 561 | struct perf_event_context *ctx = event->ctx; |
| 562 | |
| 563 | /* |
| 564 | * If this is a per-task event, need to check whether this |
| 565 | * event's task is the current task on this cpu. |
| 566 | */ |
| 567 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 568 | return; |
| 569 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 570 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 571 | |
| 572 | /* |
| 573 | * If the event is on, turn it off. |
| 574 | * If it is in error state, leave it in error state. |
| 575 | */ |
| 576 | if (event->state >= PERF_EVENT_STATE_INACTIVE) { |
| 577 | update_context_time(ctx); |
| 578 | update_group_times(event); |
| 579 | if (event == event->group_leader) |
| 580 | group_sched_out(event, cpuctx, ctx); |
| 581 | else |
| 582 | event_sched_out(event, cpuctx, ctx); |
| 583 | event->state = PERF_EVENT_STATE_OFF; |
| 584 | } |
| 585 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 586 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Disable a event. |
| 591 | * |
| 592 | * If event->ctx is a cloned context, callers must make sure that |
| 593 | * every task struct that event->ctx->task could possibly point to |
| 594 | * remains valid. This condition is satisifed when called through |
| 595 | * perf_event_for_each_child or perf_event_for_each because they |
| 596 | * hold the top-level event's child_mutex, so any descendant that |
| 597 | * goes to exit will block in sync_child_event. |
| 598 | * When called from perf_pending_event it's OK because event->ctx |
| 599 | * is the current context on this CPU and preemption is disabled, |
| 600 | * hence we can't get into perf_event_task_sched_out for this context. |
| 601 | */ |
Frederic Weisbecker | 44234ad | 2009-12-09 09:25:48 +0100 | [diff] [blame] | 602 | void perf_event_disable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 603 | { |
| 604 | struct perf_event_context *ctx = event->ctx; |
| 605 | struct task_struct *task = ctx->task; |
| 606 | |
| 607 | if (!task) { |
| 608 | /* |
| 609 | * Disable the event on the cpu that it's on |
| 610 | */ |
| 611 | smp_call_function_single(event->cpu, __perf_event_disable, |
| 612 | event, 1); |
| 613 | return; |
| 614 | } |
| 615 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 616 | retry: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 617 | task_oncpu_function_call(task, __perf_event_disable, event); |
| 618 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 619 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 620 | /* |
| 621 | * If the event is still active, we need to retry the cross-call. |
| 622 | */ |
| 623 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 624 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 625 | goto retry; |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | * Since we have the lock this context can't be scheduled |
| 630 | * in, so we can change the state safely. |
| 631 | */ |
| 632 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 633 | update_group_times(event); |
| 634 | event->state = PERF_EVENT_STATE_OFF; |
| 635 | } |
| 636 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 637 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | static int |
| 641 | event_sched_in(struct perf_event *event, |
| 642 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 643 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 644 | { |
| 645 | if (event->state <= PERF_EVENT_STATE_OFF) |
| 646 | return 0; |
| 647 | |
| 648 | event->state = PERF_EVENT_STATE_ACTIVE; |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 649 | event->oncpu = smp_processor_id(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 650 | /* |
| 651 | * The new state must be visible before we turn it on in the hardware: |
| 652 | */ |
| 653 | smp_wmb(); |
| 654 | |
| 655 | if (event->pmu->enable(event)) { |
| 656 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 657 | event->oncpu = -1; |
| 658 | return -EAGAIN; |
| 659 | } |
| 660 | |
| 661 | event->tstamp_running += ctx->time - event->tstamp_stopped; |
| 662 | |
| 663 | if (!is_software_event(event)) |
| 664 | cpuctx->active_oncpu++; |
| 665 | ctx->nr_active++; |
| 666 | |
| 667 | if (event->attr.exclusive) |
| 668 | cpuctx->exclusive = 1; |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static int |
| 674 | group_sched_in(struct perf_event *group_event, |
| 675 | struct perf_cpu_context *cpuctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 676 | struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 677 | { |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 678 | struct perf_event *event, *partial_group = NULL; |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 679 | struct pmu *pmu = group_event->pmu; |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 680 | bool txn = false; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 681 | |
| 682 | if (group_event->state == PERF_EVENT_STATE_OFF) |
| 683 | return 0; |
| 684 | |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 685 | /* Check if group transaction availabe */ |
| 686 | if (pmu->start_txn) |
| 687 | txn = true; |
| 688 | |
| 689 | if (txn) |
| 690 | pmu->start_txn(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 691 | |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 692 | if (event_sched_in(group_event, cpuctx, ctx)) { |
| 693 | if (txn) |
| 694 | pmu->cancel_txn(pmu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 695 | return -EAGAIN; |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 696 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 697 | |
| 698 | /* |
| 699 | * Schedule in siblings as one group (if any): |
| 700 | */ |
| 701 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 702 | if (event_sched_in(event, cpuctx, ctx)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 703 | partial_group = event; |
| 704 | goto group_error; |
| 705 | } |
| 706 | } |
| 707 | |
Peter Zijlstra | 8d2cacb | 2010-05-25 17:49:05 +0200 | [diff] [blame] | 708 | if (!txn || !pmu->commit_txn(pmu)) |
Paul Mackerras | 6e85158 | 2010-05-08 20:58:00 +1000 | [diff] [blame] | 709 | return 0; |
Lin Ming | 6bde9b6 | 2010-04-23 13:56:00 +0800 | [diff] [blame] | 710 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 711 | group_error: |
| 712 | /* |
| 713 | * Groups can be scheduled in as one unit only, so undo any |
| 714 | * partial group before returning: |
| 715 | */ |
| 716 | list_for_each_entry(event, &group_event->sibling_list, group_entry) { |
| 717 | if (event == partial_group) |
| 718 | break; |
| 719 | event_sched_out(event, cpuctx, ctx); |
| 720 | } |
| 721 | event_sched_out(group_event, cpuctx, ctx); |
| 722 | |
Stephane Eranian | 90151c35 | 2010-05-25 16:23:10 +0200 | [diff] [blame] | 723 | if (txn) |
| 724 | pmu->cancel_txn(pmu); |
| 725 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 726 | return -EAGAIN; |
| 727 | } |
| 728 | |
| 729 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 730 | * Work out whether we can put this event group on the CPU now. |
| 731 | */ |
| 732 | static int group_can_go_on(struct perf_event *event, |
| 733 | struct perf_cpu_context *cpuctx, |
| 734 | int can_add_hw) |
| 735 | { |
| 736 | /* |
| 737 | * Groups consisting entirely of software events can always go on. |
| 738 | */ |
Frederic Weisbecker | d6f962b | 2010-01-10 01:25:51 +0100 | [diff] [blame] | 739 | if (event->group_flags & PERF_GROUP_SOFTWARE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 740 | return 1; |
| 741 | /* |
| 742 | * If an exclusive group is already on, no other hardware |
| 743 | * events can go on. |
| 744 | */ |
| 745 | if (cpuctx->exclusive) |
| 746 | return 0; |
| 747 | /* |
| 748 | * If this group is exclusive and there are already |
| 749 | * events on the CPU, it can't go on. |
| 750 | */ |
| 751 | if (event->attr.exclusive && cpuctx->active_oncpu) |
| 752 | return 0; |
| 753 | /* |
| 754 | * Otherwise, try to add it if all previous groups were able |
| 755 | * to go on. |
| 756 | */ |
| 757 | return can_add_hw; |
| 758 | } |
| 759 | |
| 760 | static void add_event_to_ctx(struct perf_event *event, |
| 761 | struct perf_event_context *ctx) |
| 762 | { |
| 763 | list_add_event(event, ctx); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 764 | perf_group_attach(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 765 | event->tstamp_enabled = ctx->time; |
| 766 | event->tstamp_running = ctx->time; |
| 767 | event->tstamp_stopped = ctx->time; |
| 768 | } |
| 769 | |
| 770 | /* |
| 771 | * Cross CPU call to install and enable a performance event |
| 772 | * |
| 773 | * Must be called with ctx->mutex held |
| 774 | */ |
| 775 | static void __perf_install_in_context(void *info) |
| 776 | { |
| 777 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 778 | struct perf_event *event = info; |
| 779 | struct perf_event_context *ctx = event->ctx; |
| 780 | struct perf_event *leader = event->group_leader; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 781 | int err; |
| 782 | |
| 783 | /* |
| 784 | * If this is a task context, we need to check whether it is |
| 785 | * the current task context of this cpu. If not it has been |
| 786 | * scheduled out before the smp call arrived. |
| 787 | * Or possibly this is the right context but it isn't |
| 788 | * on this cpu because it had no events. |
| 789 | */ |
| 790 | if (ctx->task && cpuctx->task_ctx != ctx) { |
| 791 | if (cpuctx->task_ctx || ctx->task != current) |
| 792 | return; |
| 793 | cpuctx->task_ctx = ctx; |
| 794 | } |
| 795 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 796 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 797 | ctx->is_active = 1; |
| 798 | update_context_time(ctx); |
| 799 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 800 | add_event_to_ctx(event, ctx); |
| 801 | |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 802 | if (event->cpu != -1 && event->cpu != smp_processor_id()) |
| 803 | goto unlock; |
| 804 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 805 | /* |
| 806 | * Don't put the event on if it is disabled or if |
| 807 | * it is in a group and the group isn't on. |
| 808 | */ |
| 809 | if (event->state != PERF_EVENT_STATE_INACTIVE || |
| 810 | (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)) |
| 811 | goto unlock; |
| 812 | |
| 813 | /* |
| 814 | * An exclusive event can't go on if there are already active |
| 815 | * hardware events, and no hardware event can go on if there |
| 816 | * is already an exclusive event on. |
| 817 | */ |
| 818 | if (!group_can_go_on(event, cpuctx, 1)) |
| 819 | err = -EEXIST; |
| 820 | else |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 821 | err = event_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 822 | |
| 823 | if (err) { |
| 824 | /* |
| 825 | * This event couldn't go on. If it is in a group |
| 826 | * then we have to pull the whole group off. |
| 827 | * If the event group is pinned then put it in error state. |
| 828 | */ |
| 829 | if (leader != event) |
| 830 | group_sched_out(leader, cpuctx, ctx); |
| 831 | if (leader->attr.pinned) { |
| 832 | update_group_times(leader); |
| 833 | leader->state = PERF_EVENT_STATE_ERROR; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | if (!err && !ctx->task && cpuctx->max_pertask) |
| 838 | cpuctx->max_pertask--; |
| 839 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 840 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 841 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | /* |
| 845 | * Attach a performance event to a context |
| 846 | * |
| 847 | * First we add the event to the list with the hardware enable bit |
| 848 | * in event->hw_config cleared. |
| 849 | * |
| 850 | * If the event is attached to a task which is on a CPU we use a smp |
| 851 | * call to enable it in the task context. The task might have been |
| 852 | * scheduled away, but we check this in the smp call again. |
| 853 | * |
| 854 | * Must be called with ctx->mutex held. |
| 855 | */ |
| 856 | static void |
| 857 | perf_install_in_context(struct perf_event_context *ctx, |
| 858 | struct perf_event *event, |
| 859 | int cpu) |
| 860 | { |
| 861 | struct task_struct *task = ctx->task; |
| 862 | |
| 863 | if (!task) { |
| 864 | /* |
| 865 | * Per cpu events are installed via an smp call and |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 866 | * the install is always successful. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 867 | */ |
| 868 | smp_call_function_single(cpu, __perf_install_in_context, |
| 869 | event, 1); |
| 870 | return; |
| 871 | } |
| 872 | |
| 873 | retry: |
| 874 | task_oncpu_function_call(task, __perf_install_in_context, |
| 875 | event); |
| 876 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 877 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 878 | /* |
| 879 | * we need to retry the smp call. |
| 880 | */ |
| 881 | if (ctx->is_active && list_empty(&event->group_entry)) { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 882 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 883 | goto retry; |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * The lock prevents that this context is scheduled in so we |
| 888 | * can add the event safely, if it the call above did not |
| 889 | * succeed. |
| 890 | */ |
| 891 | if (list_empty(&event->group_entry)) |
| 892 | add_event_to_ctx(event, ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 893 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | /* |
| 897 | * Put a event into inactive state and update time fields. |
| 898 | * Enabling the leader of a group effectively enables all |
| 899 | * the group members that aren't explicitly disabled, so we |
| 900 | * have to update their ->tstamp_enabled also. |
| 901 | * Note: this works for group members as well as group leaders |
| 902 | * since the non-leader members' sibling_lists will be empty. |
| 903 | */ |
| 904 | static void __perf_event_mark_enabled(struct perf_event *event, |
| 905 | struct perf_event_context *ctx) |
| 906 | { |
| 907 | struct perf_event *sub; |
| 908 | |
| 909 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 910 | event->tstamp_enabled = ctx->time - event->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 911 | list_for_each_entry(sub, &event->sibling_list, group_entry) { |
| 912 | if (sub->state >= PERF_EVENT_STATE_INACTIVE) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 913 | sub->tstamp_enabled = |
| 914 | ctx->time - sub->total_time_enabled; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 915 | } |
| 916 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | /* |
| 920 | * Cross CPU call to enable a performance event |
| 921 | */ |
| 922 | static void __perf_event_enable(void *info) |
| 923 | { |
| 924 | struct perf_event *event = info; |
| 925 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 926 | struct perf_event_context *ctx = event->ctx; |
| 927 | struct perf_event *leader = event->group_leader; |
| 928 | int err; |
| 929 | |
| 930 | /* |
| 931 | * If this is a per-task event, need to check whether this |
| 932 | * event's task is the current task on this cpu. |
| 933 | */ |
| 934 | if (ctx->task && cpuctx->task_ctx != ctx) { |
| 935 | if (cpuctx->task_ctx || ctx->task != current) |
| 936 | return; |
| 937 | cpuctx->task_ctx = ctx; |
| 938 | } |
| 939 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 940 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 941 | ctx->is_active = 1; |
| 942 | update_context_time(ctx); |
| 943 | |
| 944 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 945 | goto unlock; |
| 946 | __perf_event_mark_enabled(event, ctx); |
| 947 | |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 948 | if (event->cpu != -1 && event->cpu != smp_processor_id()) |
| 949 | goto unlock; |
| 950 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 951 | /* |
| 952 | * If the event is in a group and isn't the group leader, |
| 953 | * then don't put it on unless the group is on. |
| 954 | */ |
| 955 | if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) |
| 956 | goto unlock; |
| 957 | |
| 958 | if (!group_can_go_on(event, cpuctx, 1)) { |
| 959 | err = -EEXIST; |
| 960 | } else { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 961 | if (event == leader) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 962 | err = group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 963 | else |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 964 | err = event_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | if (err) { |
| 968 | /* |
| 969 | * If this event can't go on and it's part of a |
| 970 | * group, then the whole group has to come off. |
| 971 | */ |
| 972 | if (leader != event) |
| 973 | group_sched_out(leader, cpuctx, ctx); |
| 974 | if (leader->attr.pinned) { |
| 975 | update_group_times(leader); |
| 976 | leader->state = PERF_EVENT_STATE_ERROR; |
| 977 | } |
| 978 | } |
| 979 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 980 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 981 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | /* |
| 985 | * Enable a event. |
| 986 | * |
| 987 | * If event->ctx is a cloned context, callers must make sure that |
| 988 | * every task struct that event->ctx->task could possibly point to |
| 989 | * remains valid. This condition is satisfied when called through |
| 990 | * perf_event_for_each_child or perf_event_for_each as described |
| 991 | * for perf_event_disable. |
| 992 | */ |
Frederic Weisbecker | 44234ad | 2009-12-09 09:25:48 +0100 | [diff] [blame] | 993 | void perf_event_enable(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 994 | { |
| 995 | struct perf_event_context *ctx = event->ctx; |
| 996 | struct task_struct *task = ctx->task; |
| 997 | |
| 998 | if (!task) { |
| 999 | /* |
| 1000 | * Enable the event on the cpu that it's on |
| 1001 | */ |
| 1002 | smp_call_function_single(event->cpu, __perf_event_enable, |
| 1003 | event, 1); |
| 1004 | return; |
| 1005 | } |
| 1006 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1007 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1008 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 1009 | goto out; |
| 1010 | |
| 1011 | /* |
| 1012 | * If the event is in error state, clear that first. |
| 1013 | * That way, if we see the event in error state below, we |
| 1014 | * know that it has gone back into error state, as distinct |
| 1015 | * from the task having been scheduled away before the |
| 1016 | * cross-call arrived. |
| 1017 | */ |
| 1018 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 1019 | event->state = PERF_EVENT_STATE_OFF; |
| 1020 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1021 | retry: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1022 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1023 | task_oncpu_function_call(task, __perf_event_enable, event); |
| 1024 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1025 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1026 | |
| 1027 | /* |
| 1028 | * If the context is active and the event is still off, |
| 1029 | * we need to retry the cross-call. |
| 1030 | */ |
| 1031 | if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) |
| 1032 | goto retry; |
| 1033 | |
| 1034 | /* |
| 1035 | * Since we have the lock this context can't be scheduled |
| 1036 | * in, so we can change the state safely. |
| 1037 | */ |
| 1038 | if (event->state == PERF_EVENT_STATE_OFF) |
| 1039 | __perf_event_mark_enabled(event, ctx); |
| 1040 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1041 | out: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1042 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | static int perf_event_refresh(struct perf_event *event, int refresh) |
| 1046 | { |
| 1047 | /* |
| 1048 | * not supported on inherited events |
| 1049 | */ |
| 1050 | if (event->attr.inherit) |
| 1051 | return -EINVAL; |
| 1052 | |
| 1053 | atomic_add(refresh, &event->event_limit); |
| 1054 | perf_event_enable(event); |
| 1055 | |
| 1056 | return 0; |
| 1057 | } |
| 1058 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1059 | enum event_type_t { |
| 1060 | EVENT_FLEXIBLE = 0x1, |
| 1061 | EVENT_PINNED = 0x2, |
| 1062 | EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED, |
| 1063 | }; |
| 1064 | |
| 1065 | static void ctx_sched_out(struct perf_event_context *ctx, |
| 1066 | struct perf_cpu_context *cpuctx, |
| 1067 | enum event_type_t event_type) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1068 | { |
| 1069 | struct perf_event *event; |
| 1070 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1071 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1072 | ctx->is_active = 0; |
| 1073 | if (likely(!ctx->nr_events)) |
| 1074 | goto out; |
| 1075 | update_context_time(ctx); |
| 1076 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1077 | if (!ctx->nr_active) |
Peter Zijlstra | 24cd7f5 | 2010-06-11 17:32:03 +0200 | [diff] [blame^] | 1078 | goto out; |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1079 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1080 | if (event_type & EVENT_PINNED) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1081 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) |
| 1082 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1083 | } |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1084 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1085 | if (event_type & EVENT_FLEXIBLE) { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1086 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 1087 | group_sched_out(event, cpuctx, ctx); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1088 | } |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1089 | out: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1090 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | /* |
| 1094 | * Test whether two contexts are equivalent, i.e. whether they |
| 1095 | * have both been cloned from the same version of the same context |
| 1096 | * and they both have the same number of enabled events. |
| 1097 | * If the number of enabled events is the same, then the set |
| 1098 | * of enabled events should be the same, because these are both |
| 1099 | * inherited contexts, therefore we can't access individual events |
| 1100 | * in them directly with an fd; we can only enable/disable all |
| 1101 | * events via prctl, or enable/disable all events in a family |
| 1102 | * via ioctl, which will have the same effect on both contexts. |
| 1103 | */ |
| 1104 | static int context_equiv(struct perf_event_context *ctx1, |
| 1105 | struct perf_event_context *ctx2) |
| 1106 | { |
| 1107 | return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx |
| 1108 | && ctx1->parent_gen == ctx2->parent_gen |
| 1109 | && !ctx1->pin_count && !ctx2->pin_count; |
| 1110 | } |
| 1111 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1112 | static void __perf_event_sync_stat(struct perf_event *event, |
| 1113 | struct perf_event *next_event) |
| 1114 | { |
| 1115 | u64 value; |
| 1116 | |
| 1117 | if (!event->attr.inherit_stat) |
| 1118 | return; |
| 1119 | |
| 1120 | /* |
| 1121 | * Update the event value, we cannot use perf_event_read() |
| 1122 | * because we're in the middle of a context switch and have IRQs |
| 1123 | * disabled, which upsets smp_call_function_single(), however |
| 1124 | * we know the event must be on the current CPU, therefore we |
| 1125 | * don't need to use it. |
| 1126 | */ |
| 1127 | switch (event->state) { |
| 1128 | case PERF_EVENT_STATE_ACTIVE: |
Peter Zijlstra | 3dbebf1 | 2009-11-20 22:19:52 +0100 | [diff] [blame] | 1129 | event->pmu->read(event); |
| 1130 | /* fall-through */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1131 | |
| 1132 | case PERF_EVENT_STATE_INACTIVE: |
| 1133 | update_event_times(event); |
| 1134 | break; |
| 1135 | |
| 1136 | default: |
| 1137 | break; |
| 1138 | } |
| 1139 | |
| 1140 | /* |
| 1141 | * In order to keep per-task stats reliable we need to flip the event |
| 1142 | * values when we flip the contexts. |
| 1143 | */ |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1144 | value = local64_read(&next_event->count); |
| 1145 | value = local64_xchg(&event->count, value); |
| 1146 | local64_set(&next_event->count, value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1147 | |
| 1148 | swap(event->total_time_enabled, next_event->total_time_enabled); |
| 1149 | swap(event->total_time_running, next_event->total_time_running); |
| 1150 | |
| 1151 | /* |
| 1152 | * Since we swizzled the values, update the user visible data too. |
| 1153 | */ |
| 1154 | perf_event_update_userpage(event); |
| 1155 | perf_event_update_userpage(next_event); |
| 1156 | } |
| 1157 | |
| 1158 | #define list_next_entry(pos, member) \ |
| 1159 | list_entry(pos->member.next, typeof(*pos), member) |
| 1160 | |
| 1161 | static void perf_event_sync_stat(struct perf_event_context *ctx, |
| 1162 | struct perf_event_context *next_ctx) |
| 1163 | { |
| 1164 | struct perf_event *event, *next_event; |
| 1165 | |
| 1166 | if (!ctx->nr_stat) |
| 1167 | return; |
| 1168 | |
Peter Zijlstra | 02ffdbc | 2009-11-20 22:19:50 +0100 | [diff] [blame] | 1169 | update_context_time(ctx); |
| 1170 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1171 | event = list_first_entry(&ctx->event_list, |
| 1172 | struct perf_event, event_entry); |
| 1173 | |
| 1174 | next_event = list_first_entry(&next_ctx->event_list, |
| 1175 | struct perf_event, event_entry); |
| 1176 | |
| 1177 | while (&event->event_entry != &ctx->event_list && |
| 1178 | &next_event->event_entry != &next_ctx->event_list) { |
| 1179 | |
| 1180 | __perf_event_sync_stat(event, next_event); |
| 1181 | |
| 1182 | event = list_next_entry(event, event_entry); |
| 1183 | next_event = list_next_entry(next_event, event_entry); |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | /* |
| 1188 | * Called from scheduler to remove the events of the current task, |
| 1189 | * with interrupts disabled. |
| 1190 | * |
| 1191 | * We stop each event and update the event value in event->count. |
| 1192 | * |
| 1193 | * This does not protect us against NMI, but disable() |
| 1194 | * sets the disabled bit in the control field of event _before_ |
| 1195 | * accessing the event control register. If a NMI hits, then it will |
| 1196 | * not restart the event. |
| 1197 | */ |
| 1198 | void perf_event_task_sched_out(struct task_struct *task, |
Peter Zijlstra | 49f4743 | 2009-12-27 11:51:52 +0100 | [diff] [blame] | 1199 | struct task_struct *next) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1200 | { |
Peter Zijlstra | 49f4743 | 2009-12-27 11:51:52 +0100 | [diff] [blame] | 1201 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1202 | struct perf_event_context *ctx = task->perf_event_ctxp; |
| 1203 | struct perf_event_context *next_ctx; |
| 1204 | struct perf_event_context *parent; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1205 | int do_switch = 1; |
| 1206 | |
Frederic Weisbecker | e49a5bd | 2010-03-22 19:40:03 +0100 | [diff] [blame] | 1207 | perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1208 | |
| 1209 | if (likely(!ctx || !cpuctx->task_ctx)) |
| 1210 | return; |
| 1211 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1212 | rcu_read_lock(); |
| 1213 | parent = rcu_dereference(ctx->parent_ctx); |
| 1214 | next_ctx = next->perf_event_ctxp; |
| 1215 | if (parent && next_ctx && |
| 1216 | rcu_dereference(next_ctx->parent_ctx) == parent) { |
| 1217 | /* |
| 1218 | * Looks like the two contexts are clones, so we might be |
| 1219 | * able to optimize the context switch. We lock both |
| 1220 | * contexts and check that they are clones under the |
| 1221 | * lock (including re-checking that neither has been |
| 1222 | * uncloned in the meantime). It doesn't matter which |
| 1223 | * order we take the locks because no other cpu could |
| 1224 | * be trying to lock both of these tasks. |
| 1225 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1226 | raw_spin_lock(&ctx->lock); |
| 1227 | raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1228 | if (context_equiv(ctx, next_ctx)) { |
| 1229 | /* |
| 1230 | * XXX do we need a memory barrier of sorts |
| 1231 | * wrt to rcu_dereference() of perf_event_ctxp |
| 1232 | */ |
| 1233 | task->perf_event_ctxp = next_ctx; |
| 1234 | next->perf_event_ctxp = ctx; |
| 1235 | ctx->task = next; |
| 1236 | next_ctx->task = task; |
| 1237 | do_switch = 0; |
| 1238 | |
| 1239 | perf_event_sync_stat(ctx, next_ctx); |
| 1240 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1241 | raw_spin_unlock(&next_ctx->lock); |
| 1242 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1243 | } |
| 1244 | rcu_read_unlock(); |
| 1245 | |
| 1246 | if (do_switch) { |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1247 | ctx_sched_out(ctx, cpuctx, EVENT_ALL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1248 | cpuctx->task_ctx = NULL; |
| 1249 | } |
| 1250 | } |
| 1251 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1252 | static void task_ctx_sched_out(struct perf_event_context *ctx, |
| 1253 | enum event_type_t event_type) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1254 | { |
| 1255 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 1256 | |
| 1257 | if (!cpuctx->task_ctx) |
| 1258 | return; |
| 1259 | |
| 1260 | if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) |
| 1261 | return; |
| 1262 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1263 | ctx_sched_out(ctx, cpuctx, event_type); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1264 | cpuctx->task_ctx = NULL; |
| 1265 | } |
| 1266 | |
| 1267 | /* |
| 1268 | * Called with IRQs disabled |
| 1269 | */ |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1270 | static void __perf_event_task_sched_out(struct perf_event_context *ctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1271 | { |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1272 | task_ctx_sched_out(ctx, EVENT_ALL); |
| 1273 | } |
| 1274 | |
| 1275 | /* |
| 1276 | * Called with IRQs disabled |
| 1277 | */ |
| 1278 | static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, |
| 1279 | enum event_type_t event_type) |
| 1280 | { |
| 1281 | ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | static void |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1285 | ctx_pinned_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1286 | struct perf_cpu_context *cpuctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1287 | { |
| 1288 | struct perf_event *event; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1289 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1290 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
| 1291 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1292 | continue; |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1293 | if (event->cpu != -1 && event->cpu != smp_processor_id()) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1294 | continue; |
| 1295 | |
Xiao Guangrong | 8c9ed8e | 2009-09-25 13:51:17 +0800 | [diff] [blame] | 1296 | if (group_can_go_on(event, cpuctx, 1)) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1297 | group_sched_in(event, cpuctx, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1298 | |
| 1299 | /* |
| 1300 | * If this pinned group hasn't been scheduled, |
| 1301 | * put it in error state. |
| 1302 | */ |
| 1303 | if (event->state == PERF_EVENT_STATE_INACTIVE) { |
| 1304 | update_group_times(event); |
| 1305 | event->state = PERF_EVENT_STATE_ERROR; |
| 1306 | } |
| 1307 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | static void |
| 1311 | ctx_flexible_sched_in(struct perf_event_context *ctx, |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1312 | struct perf_cpu_context *cpuctx) |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1313 | { |
| 1314 | struct perf_event *event; |
| 1315 | int can_add_hw = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1316 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1317 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
| 1318 | /* Ignore events in OFF or ERROR state */ |
| 1319 | if (event->state <= PERF_EVENT_STATE_OFF) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1320 | continue; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1321 | /* |
| 1322 | * Listen to the 'cpu' scheduling filter constraint |
| 1323 | * of events: |
| 1324 | */ |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1325 | if (event->cpu != -1 && event->cpu != smp_processor_id()) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1326 | continue; |
| 1327 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1328 | if (group_can_go_on(event, cpuctx, can_add_hw)) { |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1329 | if (group_sched_in(event, cpuctx, ctx)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1330 | can_add_hw = 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1331 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1332 | } |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | static void |
| 1336 | ctx_sched_in(struct perf_event_context *ctx, |
| 1337 | struct perf_cpu_context *cpuctx, |
| 1338 | enum event_type_t event_type) |
| 1339 | { |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1340 | raw_spin_lock(&ctx->lock); |
| 1341 | ctx->is_active = 1; |
| 1342 | if (likely(!ctx->nr_events)) |
| 1343 | goto out; |
| 1344 | |
| 1345 | ctx->timestamp = perf_clock(); |
| 1346 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1347 | /* |
| 1348 | * First go through the list and put on any pinned groups |
| 1349 | * in order to give them the best chance of going on. |
| 1350 | */ |
| 1351 | if (event_type & EVENT_PINNED) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1352 | ctx_pinned_sched_in(ctx, cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1353 | |
| 1354 | /* Then walk through the lower prio flexible groups */ |
| 1355 | if (event_type & EVENT_FLEXIBLE) |
Peter Zijlstra | 6e37738 | 2010-02-11 13:21:58 +0100 | [diff] [blame] | 1356 | ctx_flexible_sched_in(ctx, cpuctx); |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1357 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1358 | out: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1359 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1360 | } |
| 1361 | |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 1362 | static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, |
| 1363 | enum event_type_t event_type) |
| 1364 | { |
| 1365 | struct perf_event_context *ctx = &cpuctx->ctx; |
| 1366 | |
| 1367 | ctx_sched_in(ctx, cpuctx, event_type); |
| 1368 | } |
| 1369 | |
Frederic Weisbecker | 5b0311e | 2010-01-17 11:59:13 +0100 | [diff] [blame] | 1370 | static void task_ctx_sched_in(struct task_struct *task, |
| 1371 | enum event_type_t event_type) |
| 1372 | { |
| 1373 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 1374 | struct perf_event_context *ctx = task->perf_event_ctxp; |
| 1375 | |
| 1376 | if (likely(!ctx)) |
| 1377 | return; |
| 1378 | if (cpuctx->task_ctx == ctx) |
| 1379 | return; |
| 1380 | ctx_sched_in(ctx, cpuctx, event_type); |
| 1381 | cpuctx->task_ctx = ctx; |
| 1382 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1383 | /* |
| 1384 | * Called from scheduler to add the events of the current task |
| 1385 | * with interrupts disabled. |
| 1386 | * |
| 1387 | * We restore the event value and then enable it. |
| 1388 | * |
| 1389 | * This does not protect us against NMI, but enable() |
| 1390 | * sets the enabled bit in the control field of event _before_ |
| 1391 | * accessing the event control register. If a NMI hits, then it will |
| 1392 | * keep the event running. |
| 1393 | */ |
Peter Zijlstra | 49f4743 | 2009-12-27 11:51:52 +0100 | [diff] [blame] | 1394 | void perf_event_task_sched_in(struct task_struct *task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1395 | { |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 1396 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 1397 | struct perf_event_context *ctx = task->perf_event_ctxp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1398 | |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 1399 | if (likely(!ctx)) |
| 1400 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1401 | |
Frederic Weisbecker | 329c0e0 | 2010-01-17 12:56:05 +0100 | [diff] [blame] | 1402 | if (cpuctx->task_ctx == ctx) |
| 1403 | return; |
| 1404 | |
| 1405 | /* |
| 1406 | * We want to keep the following priority order: |
| 1407 | * cpu pinned (that don't need to move), task pinned, |
| 1408 | * cpu flexible, task flexible. |
| 1409 | */ |
| 1410 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
| 1411 | |
| 1412 | ctx_sched_in(ctx, cpuctx, EVENT_PINNED); |
| 1413 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE); |
| 1414 | ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE); |
| 1415 | |
| 1416 | cpuctx->task_ctx = ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | #define MAX_INTERRUPTS (~0ULL) |
| 1420 | |
| 1421 | static void perf_log_throttle(struct perf_event *event, int enable); |
| 1422 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 1423 | static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) |
| 1424 | { |
| 1425 | u64 frequency = event->attr.sample_freq; |
| 1426 | u64 sec = NSEC_PER_SEC; |
| 1427 | u64 divisor, dividend; |
| 1428 | |
| 1429 | int count_fls, nsec_fls, frequency_fls, sec_fls; |
| 1430 | |
| 1431 | count_fls = fls64(count); |
| 1432 | nsec_fls = fls64(nsec); |
| 1433 | frequency_fls = fls64(frequency); |
| 1434 | sec_fls = 30; |
| 1435 | |
| 1436 | /* |
| 1437 | * We got @count in @nsec, with a target of sample_freq HZ |
| 1438 | * the target period becomes: |
| 1439 | * |
| 1440 | * @count * 10^9 |
| 1441 | * period = ------------------- |
| 1442 | * @nsec * sample_freq |
| 1443 | * |
| 1444 | */ |
| 1445 | |
| 1446 | /* |
| 1447 | * Reduce accuracy by one bit such that @a and @b converge |
| 1448 | * to a similar magnitude. |
| 1449 | */ |
| 1450 | #define REDUCE_FLS(a, b) \ |
| 1451 | do { \ |
| 1452 | if (a##_fls > b##_fls) { \ |
| 1453 | a >>= 1; \ |
| 1454 | a##_fls--; \ |
| 1455 | } else { \ |
| 1456 | b >>= 1; \ |
| 1457 | b##_fls--; \ |
| 1458 | } \ |
| 1459 | } while (0) |
| 1460 | |
| 1461 | /* |
| 1462 | * Reduce accuracy until either term fits in a u64, then proceed with |
| 1463 | * the other, so that finally we can do a u64/u64 division. |
| 1464 | */ |
| 1465 | while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { |
| 1466 | REDUCE_FLS(nsec, frequency); |
| 1467 | REDUCE_FLS(sec, count); |
| 1468 | } |
| 1469 | |
| 1470 | if (count_fls + sec_fls > 64) { |
| 1471 | divisor = nsec * frequency; |
| 1472 | |
| 1473 | while (count_fls + sec_fls > 64) { |
| 1474 | REDUCE_FLS(count, sec); |
| 1475 | divisor >>= 1; |
| 1476 | } |
| 1477 | |
| 1478 | dividend = count * sec; |
| 1479 | } else { |
| 1480 | dividend = count * sec; |
| 1481 | |
| 1482 | while (nsec_fls + frequency_fls > 64) { |
| 1483 | REDUCE_FLS(nsec, frequency); |
| 1484 | dividend >>= 1; |
| 1485 | } |
| 1486 | |
| 1487 | divisor = nsec * frequency; |
| 1488 | } |
| 1489 | |
Peter Zijlstra | f6ab91a | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 1490 | if (!divisor) |
| 1491 | return dividend; |
| 1492 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 1493 | return div64_u64(dividend, divisor); |
| 1494 | } |
| 1495 | |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 1496 | static void perf_event_stop(struct perf_event *event) |
| 1497 | { |
| 1498 | if (!event->pmu->stop) |
| 1499 | return event->pmu->disable(event); |
| 1500 | |
| 1501 | return event->pmu->stop(event); |
| 1502 | } |
| 1503 | |
| 1504 | static int perf_event_start(struct perf_event *event) |
| 1505 | { |
| 1506 | if (!event->pmu->start) |
| 1507 | return event->pmu->enable(event); |
| 1508 | |
| 1509 | return event->pmu->start(event); |
| 1510 | } |
| 1511 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 1512 | static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1513 | { |
| 1514 | struct hw_perf_event *hwc = &event->hw; |
Peter Zijlstra | f6ab91a | 2010-06-04 15:18:01 +0200 | [diff] [blame] | 1515 | s64 period, sample_period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1516 | s64 delta; |
| 1517 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 1518 | period = perf_calculate_period(event, nsec, count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1519 | |
| 1520 | delta = (s64)(period - hwc->sample_period); |
| 1521 | delta = (delta + 7) / 8; /* low pass filter */ |
| 1522 | |
| 1523 | sample_period = hwc->sample_period + delta; |
| 1524 | |
| 1525 | if (!sample_period) |
| 1526 | sample_period = 1; |
| 1527 | |
| 1528 | hwc->sample_period = sample_period; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 1529 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1530 | if (local64_read(&hwc->period_left) > 8*sample_period) { |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 1531 | perf_event_stop(event); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1532 | local64_set(&hwc->period_left, 0); |
Stephane Eranian | d76a081 | 2010-02-08 17:06:01 +0200 | [diff] [blame] | 1533 | perf_event_start(event); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 1534 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | static void perf_ctx_adjust_freq(struct perf_event_context *ctx) |
| 1538 | { |
| 1539 | struct perf_event *event; |
| 1540 | struct hw_perf_event *hwc; |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 1541 | u64 interrupts, now; |
| 1542 | s64 delta; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1543 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1544 | raw_spin_lock(&ctx->lock); |
Paul Mackerras | 03541f8 | 2009-10-14 16:58:03 +1100 | [diff] [blame] | 1545 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1546 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 1547 | continue; |
| 1548 | |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 1549 | if (event->cpu != -1 && event->cpu != smp_processor_id()) |
| 1550 | continue; |
| 1551 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1552 | hwc = &event->hw; |
| 1553 | |
| 1554 | interrupts = hwc->interrupts; |
| 1555 | hwc->interrupts = 0; |
| 1556 | |
| 1557 | /* |
| 1558 | * unthrottle events on the tick |
| 1559 | */ |
| 1560 | if (interrupts == MAX_INTERRUPTS) { |
| 1561 | perf_log_throttle(event, 1); |
| 1562 | event->pmu->unthrottle(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | if (!event->attr.freq || !event->attr.sample_freq) |
| 1566 | continue; |
| 1567 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 1568 | event->pmu->read(event); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1569 | now = local64_read(&event->count); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 1570 | delta = now - hwc->freq_count_stamp; |
| 1571 | hwc->freq_count_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1572 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 1573 | if (delta > 0) |
| 1574 | perf_adjust_period(event, TICK_NSEC, delta); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1575 | } |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1576 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | /* |
| 1580 | * Round-robin a context's events: |
| 1581 | */ |
| 1582 | static void rotate_ctx(struct perf_event_context *ctx) |
| 1583 | { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1584 | raw_spin_lock(&ctx->lock); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1585 | |
Frederic Weisbecker | e286417 | 2010-01-09 21:05:28 +0100 | [diff] [blame] | 1586 | /* Rotate the first entry last of non-pinned groups */ |
Frederic Weisbecker | e286417 | 2010-01-09 21:05:28 +0100 | [diff] [blame] | 1587 | list_rotate_left(&ctx->flexible_groups); |
| 1588 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1589 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1590 | } |
| 1591 | |
Peter Zijlstra | 49f4743 | 2009-12-27 11:51:52 +0100 | [diff] [blame] | 1592 | void perf_event_task_tick(struct task_struct *curr) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1593 | { |
| 1594 | struct perf_cpu_context *cpuctx; |
| 1595 | struct perf_event_context *ctx; |
Peter Zijlstra | d4944a0 | 2010-03-08 13:51:20 +0100 | [diff] [blame] | 1596 | int rotate = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1597 | |
| 1598 | if (!atomic_read(&nr_events)) |
| 1599 | return; |
| 1600 | |
Peter Zijlstra | 49f4743 | 2009-12-27 11:51:52 +0100 | [diff] [blame] | 1601 | cpuctx = &__get_cpu_var(perf_cpu_context); |
Peter Zijlstra | d4944a0 | 2010-03-08 13:51:20 +0100 | [diff] [blame] | 1602 | if (cpuctx->ctx.nr_events && |
| 1603 | cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) |
| 1604 | rotate = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1605 | |
Peter Zijlstra | d4944a0 | 2010-03-08 13:51:20 +0100 | [diff] [blame] | 1606 | ctx = curr->perf_event_ctxp; |
| 1607 | if (ctx && ctx->nr_events && ctx->nr_events != ctx->nr_active) |
| 1608 | rotate = 1; |
Peter Zijlstra | 9717e6c | 2010-01-28 13:57:44 +0100 | [diff] [blame] | 1609 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1610 | perf_ctx_adjust_freq(&cpuctx->ctx); |
| 1611 | if (ctx) |
| 1612 | perf_ctx_adjust_freq(ctx); |
| 1613 | |
Peter Zijlstra | d4944a0 | 2010-03-08 13:51:20 +0100 | [diff] [blame] | 1614 | if (!rotate) |
| 1615 | return; |
| 1616 | |
Frederic Weisbecker | 7defb0f | 2010-01-17 12:15:31 +0100 | [diff] [blame] | 1617 | cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1618 | if (ctx) |
Frederic Weisbecker | 7defb0f | 2010-01-17 12:15:31 +0100 | [diff] [blame] | 1619 | task_ctx_sched_out(ctx, EVENT_FLEXIBLE); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1620 | |
| 1621 | rotate_ctx(&cpuctx->ctx); |
| 1622 | if (ctx) |
| 1623 | rotate_ctx(ctx); |
| 1624 | |
Frederic Weisbecker | 7defb0f | 2010-01-17 12:15:31 +0100 | [diff] [blame] | 1625 | cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1626 | if (ctx) |
Frederic Weisbecker | 7defb0f | 2010-01-17 12:15:31 +0100 | [diff] [blame] | 1627 | task_ctx_sched_in(curr, EVENT_FLEXIBLE); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1628 | } |
| 1629 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1630 | static int event_enable_on_exec(struct perf_event *event, |
| 1631 | struct perf_event_context *ctx) |
| 1632 | { |
| 1633 | if (!event->attr.enable_on_exec) |
| 1634 | return 0; |
| 1635 | |
| 1636 | event->attr.enable_on_exec = 0; |
| 1637 | if (event->state >= PERF_EVENT_STATE_INACTIVE) |
| 1638 | return 0; |
| 1639 | |
| 1640 | __perf_event_mark_enabled(event, ctx); |
| 1641 | |
| 1642 | return 1; |
| 1643 | } |
| 1644 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1645 | /* |
| 1646 | * Enable all of a task's events that have been marked enable-on-exec. |
| 1647 | * This expects task == current. |
| 1648 | */ |
| 1649 | static void perf_event_enable_on_exec(struct task_struct *task) |
| 1650 | { |
| 1651 | struct perf_event_context *ctx; |
| 1652 | struct perf_event *event; |
| 1653 | unsigned long flags; |
| 1654 | int enabled = 0; |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1655 | int ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1656 | |
| 1657 | local_irq_save(flags); |
| 1658 | ctx = task->perf_event_ctxp; |
| 1659 | if (!ctx || !ctx->nr_events) |
| 1660 | goto out; |
| 1661 | |
| 1662 | __perf_event_task_sched_out(ctx); |
| 1663 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1664 | raw_spin_lock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1665 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1666 | list_for_each_entry(event, &ctx->pinned_groups, group_entry) { |
| 1667 | ret = event_enable_on_exec(event, ctx); |
| 1668 | if (ret) |
| 1669 | enabled = 1; |
| 1670 | } |
| 1671 | |
| 1672 | list_for_each_entry(event, &ctx->flexible_groups, group_entry) { |
| 1673 | ret = event_enable_on_exec(event, ctx); |
| 1674 | if (ret) |
| 1675 | enabled = 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | /* |
| 1679 | * Unclone this context if we enabled any event. |
| 1680 | */ |
| 1681 | if (enabled) |
| 1682 | unclone_ctx(ctx); |
| 1683 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1684 | raw_spin_unlock(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1685 | |
Peter Zijlstra | 49f4743 | 2009-12-27 11:51:52 +0100 | [diff] [blame] | 1686 | perf_event_task_sched_in(task); |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 1687 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1688 | local_irq_restore(flags); |
| 1689 | } |
| 1690 | |
| 1691 | /* |
| 1692 | * Cross CPU call to read the hardware event |
| 1693 | */ |
| 1694 | static void __perf_event_read(void *info) |
| 1695 | { |
| 1696 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 1697 | struct perf_event *event = info; |
| 1698 | struct perf_event_context *ctx = event->ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1699 | |
| 1700 | /* |
| 1701 | * If this is a task context, we need to check whether it is |
| 1702 | * the current task context of this cpu. If not it has been |
| 1703 | * scheduled out before the smp call arrived. In that case |
| 1704 | * event->count would have been updated to a recent sample |
| 1705 | * when the event was scheduled out. |
| 1706 | */ |
| 1707 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 1708 | return; |
| 1709 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1710 | raw_spin_lock(&ctx->lock); |
Peter Zijlstra | 58e5ad1 | 2009-11-20 22:19:53 +0100 | [diff] [blame] | 1711 | update_context_time(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1712 | update_event_times(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1713 | raw_spin_unlock(&ctx->lock); |
Peter Zijlstra | 2b8988c | 2009-11-20 22:19:54 +0100 | [diff] [blame] | 1714 | |
Peter Zijlstra | 58e5ad1 | 2009-11-20 22:19:53 +0100 | [diff] [blame] | 1715 | event->pmu->read(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1716 | } |
| 1717 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 1718 | static inline u64 perf_event_count(struct perf_event *event) |
| 1719 | { |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 1720 | return local64_read(&event->count) + atomic64_read(&event->child_count); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 1721 | } |
| 1722 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1723 | static u64 perf_event_read(struct perf_event *event) |
| 1724 | { |
| 1725 | /* |
| 1726 | * If event is enabled and currently active on a CPU, update the |
| 1727 | * value in the event structure: |
| 1728 | */ |
| 1729 | if (event->state == PERF_EVENT_STATE_ACTIVE) { |
| 1730 | smp_call_function_single(event->oncpu, |
| 1731 | __perf_event_read, event, 1); |
| 1732 | } else if (event->state == PERF_EVENT_STATE_INACTIVE) { |
Peter Zijlstra | 2b8988c | 2009-11-20 22:19:54 +0100 | [diff] [blame] | 1733 | struct perf_event_context *ctx = event->ctx; |
| 1734 | unsigned long flags; |
| 1735 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1736 | raw_spin_lock_irqsave(&ctx->lock, flags); |
Peter Zijlstra | 2b8988c | 2009-11-20 22:19:54 +0100 | [diff] [blame] | 1737 | update_context_time(ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1738 | update_event_times(event); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1739 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1740 | } |
| 1741 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 1742 | return perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | /* |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 1746 | * Callchain support |
| 1747 | */ |
| 1748 | |
| 1749 | struct callchain_cpus_entries { |
| 1750 | struct rcu_head rcu_head; |
| 1751 | struct perf_callchain_entry *cpu_entries[0]; |
| 1752 | }; |
| 1753 | |
Frederic Weisbecker | 7ae07ea | 2010-08-14 20:45:13 +0200 | [diff] [blame] | 1754 | static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 1755 | static atomic_t nr_callchain_events; |
| 1756 | static DEFINE_MUTEX(callchain_mutex); |
| 1757 | struct callchain_cpus_entries *callchain_cpus_entries; |
| 1758 | |
| 1759 | |
| 1760 | __weak void perf_callchain_kernel(struct perf_callchain_entry *entry, |
| 1761 | struct pt_regs *regs) |
| 1762 | { |
| 1763 | } |
| 1764 | |
| 1765 | __weak void perf_callchain_user(struct perf_callchain_entry *entry, |
| 1766 | struct pt_regs *regs) |
| 1767 | { |
| 1768 | } |
| 1769 | |
| 1770 | static void release_callchain_buffers_rcu(struct rcu_head *head) |
| 1771 | { |
| 1772 | struct callchain_cpus_entries *entries; |
| 1773 | int cpu; |
| 1774 | |
| 1775 | entries = container_of(head, struct callchain_cpus_entries, rcu_head); |
| 1776 | |
| 1777 | for_each_possible_cpu(cpu) |
| 1778 | kfree(entries->cpu_entries[cpu]); |
| 1779 | |
| 1780 | kfree(entries); |
| 1781 | } |
| 1782 | |
| 1783 | static void release_callchain_buffers(void) |
| 1784 | { |
| 1785 | struct callchain_cpus_entries *entries; |
| 1786 | |
| 1787 | entries = callchain_cpus_entries; |
| 1788 | rcu_assign_pointer(callchain_cpus_entries, NULL); |
| 1789 | call_rcu(&entries->rcu_head, release_callchain_buffers_rcu); |
| 1790 | } |
| 1791 | |
| 1792 | static int alloc_callchain_buffers(void) |
| 1793 | { |
| 1794 | int cpu; |
| 1795 | int size; |
| 1796 | struct callchain_cpus_entries *entries; |
| 1797 | |
| 1798 | /* |
| 1799 | * We can't use the percpu allocation API for data that can be |
| 1800 | * accessed from NMI. Use a temporary manual per cpu allocation |
| 1801 | * until that gets sorted out. |
| 1802 | */ |
| 1803 | size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) * |
| 1804 | num_possible_cpus(); |
| 1805 | |
| 1806 | entries = kzalloc(size, GFP_KERNEL); |
| 1807 | if (!entries) |
| 1808 | return -ENOMEM; |
| 1809 | |
Frederic Weisbecker | 7ae07ea | 2010-08-14 20:45:13 +0200 | [diff] [blame] | 1810 | size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS; |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 1811 | |
| 1812 | for_each_possible_cpu(cpu) { |
| 1813 | entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL, |
| 1814 | cpu_to_node(cpu)); |
| 1815 | if (!entries->cpu_entries[cpu]) |
| 1816 | goto fail; |
| 1817 | } |
| 1818 | |
| 1819 | rcu_assign_pointer(callchain_cpus_entries, entries); |
| 1820 | |
| 1821 | return 0; |
| 1822 | |
| 1823 | fail: |
| 1824 | for_each_possible_cpu(cpu) |
| 1825 | kfree(entries->cpu_entries[cpu]); |
| 1826 | kfree(entries); |
| 1827 | |
| 1828 | return -ENOMEM; |
| 1829 | } |
| 1830 | |
| 1831 | static int get_callchain_buffers(void) |
| 1832 | { |
| 1833 | int err = 0; |
| 1834 | int count; |
| 1835 | |
| 1836 | mutex_lock(&callchain_mutex); |
| 1837 | |
| 1838 | count = atomic_inc_return(&nr_callchain_events); |
| 1839 | if (WARN_ON_ONCE(count < 1)) { |
| 1840 | err = -EINVAL; |
| 1841 | goto exit; |
| 1842 | } |
| 1843 | |
| 1844 | if (count > 1) { |
| 1845 | /* If the allocation failed, give up */ |
| 1846 | if (!callchain_cpus_entries) |
| 1847 | err = -ENOMEM; |
| 1848 | goto exit; |
| 1849 | } |
| 1850 | |
| 1851 | err = alloc_callchain_buffers(); |
| 1852 | if (err) |
| 1853 | release_callchain_buffers(); |
| 1854 | exit: |
| 1855 | mutex_unlock(&callchain_mutex); |
| 1856 | |
| 1857 | return err; |
| 1858 | } |
| 1859 | |
| 1860 | static void put_callchain_buffers(void) |
| 1861 | { |
| 1862 | if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) { |
| 1863 | release_callchain_buffers(); |
| 1864 | mutex_unlock(&callchain_mutex); |
| 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | static int get_recursion_context(int *recursion) |
| 1869 | { |
| 1870 | int rctx; |
| 1871 | |
| 1872 | if (in_nmi()) |
| 1873 | rctx = 3; |
| 1874 | else if (in_irq()) |
| 1875 | rctx = 2; |
| 1876 | else if (in_softirq()) |
| 1877 | rctx = 1; |
| 1878 | else |
| 1879 | rctx = 0; |
| 1880 | |
| 1881 | if (recursion[rctx]) |
| 1882 | return -1; |
| 1883 | |
| 1884 | recursion[rctx]++; |
| 1885 | barrier(); |
| 1886 | |
| 1887 | return rctx; |
| 1888 | } |
| 1889 | |
| 1890 | static inline void put_recursion_context(int *recursion, int rctx) |
| 1891 | { |
| 1892 | barrier(); |
| 1893 | recursion[rctx]--; |
| 1894 | } |
| 1895 | |
| 1896 | static struct perf_callchain_entry *get_callchain_entry(int *rctx) |
| 1897 | { |
| 1898 | int cpu; |
| 1899 | struct callchain_cpus_entries *entries; |
| 1900 | |
| 1901 | *rctx = get_recursion_context(__get_cpu_var(callchain_recursion)); |
| 1902 | if (*rctx == -1) |
| 1903 | return NULL; |
| 1904 | |
| 1905 | entries = rcu_dereference(callchain_cpus_entries); |
| 1906 | if (!entries) |
| 1907 | return NULL; |
| 1908 | |
| 1909 | cpu = smp_processor_id(); |
| 1910 | |
| 1911 | return &entries->cpu_entries[cpu][*rctx]; |
| 1912 | } |
| 1913 | |
| 1914 | static void |
| 1915 | put_callchain_entry(int rctx) |
| 1916 | { |
| 1917 | put_recursion_context(__get_cpu_var(callchain_recursion), rctx); |
| 1918 | } |
| 1919 | |
| 1920 | static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs) |
| 1921 | { |
| 1922 | int rctx; |
| 1923 | struct perf_callchain_entry *entry; |
| 1924 | |
| 1925 | |
| 1926 | entry = get_callchain_entry(&rctx); |
| 1927 | if (rctx == -1) |
| 1928 | return NULL; |
| 1929 | |
| 1930 | if (!entry) |
| 1931 | goto exit_put; |
| 1932 | |
| 1933 | entry->nr = 0; |
| 1934 | |
| 1935 | if (!user_mode(regs)) { |
| 1936 | perf_callchain_store(entry, PERF_CONTEXT_KERNEL); |
| 1937 | perf_callchain_kernel(entry, regs); |
| 1938 | if (current->mm) |
| 1939 | regs = task_pt_regs(current); |
| 1940 | else |
| 1941 | regs = NULL; |
| 1942 | } |
| 1943 | |
| 1944 | if (regs) { |
| 1945 | perf_callchain_store(entry, PERF_CONTEXT_USER); |
| 1946 | perf_callchain_user(entry, regs); |
| 1947 | } |
| 1948 | |
| 1949 | exit_put: |
| 1950 | put_callchain_entry(rctx); |
| 1951 | |
| 1952 | return entry; |
| 1953 | } |
| 1954 | |
| 1955 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1956 | * Initialize the perf_event context in a task_struct: |
| 1957 | */ |
| 1958 | static void |
| 1959 | __perf_event_init_context(struct perf_event_context *ctx, |
| 1960 | struct task_struct *task) |
| 1961 | { |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 1962 | raw_spin_lock_init(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1963 | mutex_init(&ctx->mutex); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 1964 | INIT_LIST_HEAD(&ctx->pinned_groups); |
| 1965 | INIT_LIST_HEAD(&ctx->flexible_groups); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1966 | INIT_LIST_HEAD(&ctx->event_list); |
| 1967 | atomic_set(&ctx->refcount, 1); |
| 1968 | ctx->task = task; |
| 1969 | } |
| 1970 | |
| 1971 | static struct perf_event_context *find_get_context(pid_t pid, int cpu) |
| 1972 | { |
| 1973 | struct perf_event_context *ctx; |
| 1974 | struct perf_cpu_context *cpuctx; |
| 1975 | struct task_struct *task; |
| 1976 | unsigned long flags; |
| 1977 | int err; |
| 1978 | |
Peter Zijlstra | f4c4176 | 2009-12-16 17:55:54 +0100 | [diff] [blame] | 1979 | if (pid == -1 && cpu != -1) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1980 | /* Must be root to operate on a CPU event: */ |
| 1981 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) |
| 1982 | return ERR_PTR(-EACCES); |
| 1983 | |
Paul Mackerras | 0f624e7 | 2009-12-15 19:40:32 +1100 | [diff] [blame] | 1984 | if (cpu < 0 || cpu >= nr_cpumask_bits) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1985 | return ERR_PTR(-EINVAL); |
| 1986 | |
| 1987 | /* |
| 1988 | * We could be clever and allow to attach a event to an |
| 1989 | * offline CPU and activate it when the CPU comes up, but |
| 1990 | * that's for later. |
| 1991 | */ |
Rusty Russell | f6325e3 | 2009-12-17 11:43:08 -0600 | [diff] [blame] | 1992 | if (!cpu_online(cpu)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1993 | return ERR_PTR(-ENODEV); |
| 1994 | |
| 1995 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 1996 | ctx = &cpuctx->ctx; |
| 1997 | get_ctx(ctx); |
| 1998 | |
| 1999 | return ctx; |
| 2000 | } |
| 2001 | |
| 2002 | rcu_read_lock(); |
| 2003 | if (!pid) |
| 2004 | task = current; |
| 2005 | else |
| 2006 | task = find_task_by_vpid(pid); |
| 2007 | if (task) |
| 2008 | get_task_struct(task); |
| 2009 | rcu_read_unlock(); |
| 2010 | |
| 2011 | if (!task) |
| 2012 | return ERR_PTR(-ESRCH); |
| 2013 | |
| 2014 | /* |
| 2015 | * Can't attach events to a dying task. |
| 2016 | */ |
| 2017 | err = -ESRCH; |
| 2018 | if (task->flags & PF_EXITING) |
| 2019 | goto errout; |
| 2020 | |
| 2021 | /* Reuse ptrace permission checks for now. */ |
| 2022 | err = -EACCES; |
| 2023 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
| 2024 | goto errout; |
| 2025 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2026 | retry: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2027 | ctx = perf_lock_task_context(task, &flags); |
| 2028 | if (ctx) { |
| 2029 | unclone_ctx(ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2030 | raw_spin_unlock_irqrestore(&ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | if (!ctx) { |
Xiao Guangrong | aa5452d | 2009-12-09 11:28:13 +0800 | [diff] [blame] | 2034 | ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2035 | err = -ENOMEM; |
| 2036 | if (!ctx) |
| 2037 | goto errout; |
| 2038 | __perf_event_init_context(ctx, task); |
| 2039 | get_ctx(ctx); |
| 2040 | if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) { |
| 2041 | /* |
| 2042 | * We raced with some other task; use |
| 2043 | * the context they set. |
| 2044 | */ |
| 2045 | kfree(ctx); |
| 2046 | goto retry; |
| 2047 | } |
| 2048 | get_task_struct(task); |
| 2049 | } |
| 2050 | |
| 2051 | put_task_struct(task); |
| 2052 | return ctx; |
| 2053 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 2054 | errout: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2055 | put_task_struct(task); |
| 2056 | return ERR_PTR(err); |
| 2057 | } |
| 2058 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 2059 | static void perf_event_free_filter(struct perf_event *event); |
| 2060 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2061 | static void free_event_rcu(struct rcu_head *head) |
| 2062 | { |
| 2063 | struct perf_event *event; |
| 2064 | |
| 2065 | event = container_of(head, struct perf_event, rcu_head); |
| 2066 | if (event->ns) |
| 2067 | put_pid_ns(event->ns); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 2068 | perf_event_free_filter(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2069 | kfree(event); |
| 2070 | } |
| 2071 | |
| 2072 | static void perf_pending_sync(struct perf_event *event); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2073 | static void perf_buffer_put(struct perf_buffer *buffer); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2074 | |
| 2075 | static void free_event(struct perf_event *event) |
| 2076 | { |
| 2077 | perf_pending_sync(event); |
| 2078 | |
| 2079 | if (!event->parent) { |
| 2080 | atomic_dec(&nr_events); |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 2081 | if (event->attr.mmap || event->attr.mmap_data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2082 | atomic_dec(&nr_mmap_events); |
| 2083 | if (event->attr.comm) |
| 2084 | atomic_dec(&nr_comm_events); |
| 2085 | if (event->attr.task) |
| 2086 | atomic_dec(&nr_task_events); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 2087 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) |
| 2088 | put_callchain_buffers(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2089 | } |
| 2090 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2091 | if (event->buffer) { |
| 2092 | perf_buffer_put(event->buffer); |
| 2093 | event->buffer = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | if (event->destroy) |
| 2097 | event->destroy(event); |
| 2098 | |
| 2099 | put_ctx(event->ctx); |
| 2100 | call_rcu(&event->rcu_head, free_event_rcu); |
| 2101 | } |
| 2102 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 2103 | int perf_event_release_kernel(struct perf_event *event) |
| 2104 | { |
| 2105 | struct perf_event_context *ctx = event->ctx; |
| 2106 | |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 2107 | /* |
| 2108 | * Remove from the PMU, can't get re-enabled since we got |
| 2109 | * here because the last ref went. |
| 2110 | */ |
| 2111 | perf_event_disable(event); |
| 2112 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 2113 | WARN_ON_ONCE(ctx->parent_ctx); |
Peter Zijlstra | a0507c8 | 2010-05-06 15:42:53 +0200 | [diff] [blame] | 2114 | /* |
| 2115 | * There are two ways this annotation is useful: |
| 2116 | * |
| 2117 | * 1) there is a lock recursion from perf_event_exit_task |
| 2118 | * see the comment there. |
| 2119 | * |
| 2120 | * 2) there is a lock-inversion with mmap_sem through |
| 2121 | * perf_event_read_group(), which takes faults while |
| 2122 | * holding ctx->mutex, however this is called after |
| 2123 | * the last filedesc died, so there is no possibility |
| 2124 | * to trigger the AB-BA case. |
| 2125 | */ |
| 2126 | mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING); |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 2127 | raw_spin_lock_irq(&ctx->lock); |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 2128 | perf_group_detach(event); |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 2129 | list_del_event(event, ctx); |
Peter Zijlstra | 050735b | 2010-05-11 11:51:53 +0200 | [diff] [blame] | 2130 | raw_spin_unlock_irq(&ctx->lock); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 2131 | mutex_unlock(&ctx->mutex); |
| 2132 | |
| 2133 | mutex_lock(&event->owner->perf_event_mutex); |
| 2134 | list_del_init(&event->owner_entry); |
| 2135 | mutex_unlock(&event->owner->perf_event_mutex); |
| 2136 | put_task_struct(event->owner); |
| 2137 | |
| 2138 | free_event(event); |
| 2139 | |
| 2140 | return 0; |
| 2141 | } |
| 2142 | EXPORT_SYMBOL_GPL(perf_event_release_kernel); |
| 2143 | |
Peter Zijlstra | a66a305 | 2009-11-23 11:37:23 +0100 | [diff] [blame] | 2144 | /* |
| 2145 | * Called when the last reference to the file is gone. |
| 2146 | */ |
| 2147 | static int perf_release(struct inode *inode, struct file *file) |
| 2148 | { |
| 2149 | struct perf_event *event = file->private_data; |
| 2150 | |
| 2151 | file->private_data = NULL; |
| 2152 | |
| 2153 | return perf_event_release_kernel(event); |
| 2154 | } |
| 2155 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2156 | static int perf_event_read_size(struct perf_event *event) |
| 2157 | { |
| 2158 | int entry = sizeof(u64); /* value */ |
| 2159 | int size = 0; |
| 2160 | int nr = 1; |
| 2161 | |
| 2162 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 2163 | size += sizeof(u64); |
| 2164 | |
| 2165 | if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 2166 | size += sizeof(u64); |
| 2167 | |
| 2168 | if (event->attr.read_format & PERF_FORMAT_ID) |
| 2169 | entry += sizeof(u64); |
| 2170 | |
| 2171 | if (event->attr.read_format & PERF_FORMAT_GROUP) { |
| 2172 | nr += event->group_leader->nr_siblings; |
| 2173 | size += sizeof(u64); |
| 2174 | } |
| 2175 | |
| 2176 | size += entry * nr; |
| 2177 | |
| 2178 | return size; |
| 2179 | } |
| 2180 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 2181 | u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2182 | { |
| 2183 | struct perf_event *child; |
| 2184 | u64 total = 0; |
| 2185 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 2186 | *enabled = 0; |
| 2187 | *running = 0; |
| 2188 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 2189 | mutex_lock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2190 | total += perf_event_read(event); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 2191 | *enabled += event->total_time_enabled + |
| 2192 | atomic64_read(&event->child_total_time_enabled); |
| 2193 | *running += event->total_time_running + |
| 2194 | atomic64_read(&event->child_total_time_running); |
| 2195 | |
| 2196 | list_for_each_entry(child, &event->child_list, child_list) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2197 | total += perf_event_read(child); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 2198 | *enabled += child->total_time_enabled; |
| 2199 | *running += child->total_time_running; |
| 2200 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 2201 | mutex_unlock(&event->child_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2202 | |
| 2203 | return total; |
| 2204 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 2205 | EXPORT_SYMBOL_GPL(perf_event_read_value); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2206 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2207 | static int perf_event_read_group(struct perf_event *event, |
| 2208 | u64 read_format, char __user *buf) |
| 2209 | { |
| 2210 | struct perf_event *leader = event->group_leader, *sub; |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 2211 | int n = 0, size = 0, ret = -EFAULT; |
| 2212 | struct perf_event_context *ctx = leader->ctx; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 2213 | u64 values[5]; |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 2214 | u64 count, enabled, running; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 2215 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 2216 | mutex_lock(&ctx->mutex); |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 2217 | count = perf_event_read_value(leader, &enabled, &running); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2218 | |
| 2219 | values[n++] = 1 + leader->nr_siblings; |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 2220 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 2221 | values[n++] = enabled; |
| 2222 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 2223 | values[n++] = running; |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 2224 | values[n++] = count; |
| 2225 | if (read_format & PERF_FORMAT_ID) |
| 2226 | values[n++] = primary_event_id(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2227 | |
| 2228 | size = n * sizeof(u64); |
| 2229 | |
| 2230 | if (copy_to_user(buf, values, size)) |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 2231 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2232 | |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 2233 | ret = size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2234 | |
| 2235 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 2236 | n = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2237 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 2238 | values[n++] = perf_event_read_value(sub, &enabled, &running); |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 2239 | if (read_format & PERF_FORMAT_ID) |
| 2240 | values[n++] = primary_event_id(sub); |
| 2241 | |
| 2242 | size = n * sizeof(u64); |
| 2243 | |
Stephane Eranian | 184d3da | 2009-11-23 21:40:49 -0800 | [diff] [blame] | 2244 | if (copy_to_user(buf + ret, values, size)) { |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 2245 | ret = -EFAULT; |
| 2246 | goto unlock; |
| 2247 | } |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 2248 | |
| 2249 | ret += size; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2250 | } |
Peter Zijlstra | 6f10581 | 2009-11-20 22:19:56 +0100 | [diff] [blame] | 2251 | unlock: |
| 2252 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2253 | |
Peter Zijlstra | abf4868 | 2009-11-20 22:19:49 +0100 | [diff] [blame] | 2254 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | static int perf_event_read_one(struct perf_event *event, |
| 2258 | u64 read_format, char __user *buf) |
| 2259 | { |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 2260 | u64 enabled, running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2261 | u64 values[4]; |
| 2262 | int n = 0; |
| 2263 | |
Peter Zijlstra | 59ed446 | 2009-11-20 22:19:55 +0100 | [diff] [blame] | 2264 | values[n++] = perf_event_read_value(event, &enabled, &running); |
| 2265 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 2266 | values[n++] = enabled; |
| 2267 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 2268 | values[n++] = running; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2269 | if (read_format & PERF_FORMAT_ID) |
| 2270 | values[n++] = primary_event_id(event); |
| 2271 | |
| 2272 | if (copy_to_user(buf, values, n * sizeof(u64))) |
| 2273 | return -EFAULT; |
| 2274 | |
| 2275 | return n * sizeof(u64); |
| 2276 | } |
| 2277 | |
| 2278 | /* |
| 2279 | * Read the performance event - simple non blocking version for now |
| 2280 | */ |
| 2281 | static ssize_t |
| 2282 | perf_read_hw(struct perf_event *event, char __user *buf, size_t count) |
| 2283 | { |
| 2284 | u64 read_format = event->attr.read_format; |
| 2285 | int ret; |
| 2286 | |
| 2287 | /* |
| 2288 | * Return end-of-file for a read on a event that is in |
| 2289 | * error state (i.e. because it was pinned but it couldn't be |
| 2290 | * scheduled on to the CPU at some point). |
| 2291 | */ |
| 2292 | if (event->state == PERF_EVENT_STATE_ERROR) |
| 2293 | return 0; |
| 2294 | |
| 2295 | if (count < perf_event_read_size(event)) |
| 2296 | return -ENOSPC; |
| 2297 | |
| 2298 | WARN_ON_ONCE(event->ctx->parent_ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2299 | if (read_format & PERF_FORMAT_GROUP) |
| 2300 | ret = perf_event_read_group(event, read_format, buf); |
| 2301 | else |
| 2302 | ret = perf_event_read_one(event, read_format, buf); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2303 | |
| 2304 | return ret; |
| 2305 | } |
| 2306 | |
| 2307 | static ssize_t |
| 2308 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 2309 | { |
| 2310 | struct perf_event *event = file->private_data; |
| 2311 | |
| 2312 | return perf_read_hw(event, buf, count); |
| 2313 | } |
| 2314 | |
| 2315 | static unsigned int perf_poll(struct file *file, poll_table *wait) |
| 2316 | { |
| 2317 | struct perf_event *event = file->private_data; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2318 | struct perf_buffer *buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2319 | unsigned int events = POLL_HUP; |
| 2320 | |
| 2321 | rcu_read_lock(); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2322 | buffer = rcu_dereference(event->buffer); |
| 2323 | if (buffer) |
| 2324 | events = atomic_xchg(&buffer->poll, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2325 | rcu_read_unlock(); |
| 2326 | |
| 2327 | poll_wait(file, &event->waitq, wait); |
| 2328 | |
| 2329 | return events; |
| 2330 | } |
| 2331 | |
| 2332 | static void perf_event_reset(struct perf_event *event) |
| 2333 | { |
| 2334 | (void)perf_event_read(event); |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2335 | local64_set(&event->count, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2336 | perf_event_update_userpage(event); |
| 2337 | } |
| 2338 | |
| 2339 | /* |
| 2340 | * Holding the top-level event's child_mutex means that any |
| 2341 | * descendant process that has inherited this event will block |
| 2342 | * in sync_child_event if it goes to exit, thus satisfying the |
| 2343 | * task existence requirements of perf_event_enable/disable. |
| 2344 | */ |
| 2345 | static void perf_event_for_each_child(struct perf_event *event, |
| 2346 | void (*func)(struct perf_event *)) |
| 2347 | { |
| 2348 | struct perf_event *child; |
| 2349 | |
| 2350 | WARN_ON_ONCE(event->ctx->parent_ctx); |
| 2351 | mutex_lock(&event->child_mutex); |
| 2352 | func(event); |
| 2353 | list_for_each_entry(child, &event->child_list, child_list) |
| 2354 | func(child); |
| 2355 | mutex_unlock(&event->child_mutex); |
| 2356 | } |
| 2357 | |
| 2358 | static void perf_event_for_each(struct perf_event *event, |
| 2359 | void (*func)(struct perf_event *)) |
| 2360 | { |
| 2361 | struct perf_event_context *ctx = event->ctx; |
| 2362 | struct perf_event *sibling; |
| 2363 | |
| 2364 | WARN_ON_ONCE(ctx->parent_ctx); |
| 2365 | mutex_lock(&ctx->mutex); |
| 2366 | event = event->group_leader; |
| 2367 | |
| 2368 | perf_event_for_each_child(event, func); |
| 2369 | func(event); |
| 2370 | list_for_each_entry(sibling, &event->sibling_list, group_entry) |
| 2371 | perf_event_for_each_child(event, func); |
| 2372 | mutex_unlock(&ctx->mutex); |
| 2373 | } |
| 2374 | |
| 2375 | static int perf_event_period(struct perf_event *event, u64 __user *arg) |
| 2376 | { |
| 2377 | struct perf_event_context *ctx = event->ctx; |
| 2378 | unsigned long size; |
| 2379 | int ret = 0; |
| 2380 | u64 value; |
| 2381 | |
| 2382 | if (!event->attr.sample_period) |
| 2383 | return -EINVAL; |
| 2384 | |
| 2385 | size = copy_from_user(&value, arg, sizeof(value)); |
| 2386 | if (size != sizeof(value)) |
| 2387 | return -EFAULT; |
| 2388 | |
| 2389 | if (!value) |
| 2390 | return -EINVAL; |
| 2391 | |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2392 | raw_spin_lock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2393 | if (event->attr.freq) { |
| 2394 | if (value > sysctl_perf_event_sample_rate) { |
| 2395 | ret = -EINVAL; |
| 2396 | goto unlock; |
| 2397 | } |
| 2398 | |
| 2399 | event->attr.sample_freq = value; |
| 2400 | } else { |
| 2401 | event->attr.sample_period = value; |
| 2402 | event->hw.sample_period = value; |
| 2403 | } |
| 2404 | unlock: |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 2405 | raw_spin_unlock_irq(&ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2406 | |
| 2407 | return ret; |
| 2408 | } |
| 2409 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 2410 | static const struct file_operations perf_fops; |
| 2411 | |
| 2412 | static struct perf_event *perf_fget_light(int fd, int *fput_needed) |
| 2413 | { |
| 2414 | struct file *file; |
| 2415 | |
| 2416 | file = fget_light(fd, fput_needed); |
| 2417 | if (!file) |
| 2418 | return ERR_PTR(-EBADF); |
| 2419 | |
| 2420 | if (file->f_op != &perf_fops) { |
| 2421 | fput_light(file, *fput_needed); |
| 2422 | *fput_needed = 0; |
| 2423 | return ERR_PTR(-EBADF); |
| 2424 | } |
| 2425 | |
| 2426 | return file->private_data; |
| 2427 | } |
| 2428 | |
| 2429 | static int perf_event_set_output(struct perf_event *event, |
| 2430 | struct perf_event *output_event); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 2431 | static int perf_event_set_filter(struct perf_event *event, void __user *arg); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2432 | |
| 2433 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 2434 | { |
| 2435 | struct perf_event *event = file->private_data; |
| 2436 | void (*func)(struct perf_event *); |
| 2437 | u32 flags = arg; |
| 2438 | |
| 2439 | switch (cmd) { |
| 2440 | case PERF_EVENT_IOC_ENABLE: |
| 2441 | func = perf_event_enable; |
| 2442 | break; |
| 2443 | case PERF_EVENT_IOC_DISABLE: |
| 2444 | func = perf_event_disable; |
| 2445 | break; |
| 2446 | case PERF_EVENT_IOC_RESET: |
| 2447 | func = perf_event_reset; |
| 2448 | break; |
| 2449 | |
| 2450 | case PERF_EVENT_IOC_REFRESH: |
| 2451 | return perf_event_refresh(event, arg); |
| 2452 | |
| 2453 | case PERF_EVENT_IOC_PERIOD: |
| 2454 | return perf_event_period(event, (u64 __user *)arg); |
| 2455 | |
| 2456 | case PERF_EVENT_IOC_SET_OUTPUT: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 2457 | { |
| 2458 | struct perf_event *output_event = NULL; |
| 2459 | int fput_needed = 0; |
| 2460 | int ret; |
| 2461 | |
| 2462 | if (arg != -1) { |
| 2463 | output_event = perf_fget_light(arg, &fput_needed); |
| 2464 | if (IS_ERR(output_event)) |
| 2465 | return PTR_ERR(output_event); |
| 2466 | } |
| 2467 | |
| 2468 | ret = perf_event_set_output(event, output_event); |
| 2469 | if (output_event) |
| 2470 | fput_light(output_event->filp, fput_needed); |
| 2471 | |
| 2472 | return ret; |
| 2473 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2474 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 2475 | case PERF_EVENT_IOC_SET_FILTER: |
| 2476 | return perf_event_set_filter(event, (void __user *)arg); |
| 2477 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2478 | default: |
| 2479 | return -ENOTTY; |
| 2480 | } |
| 2481 | |
| 2482 | if (flags & PERF_IOC_FLAG_GROUP) |
| 2483 | perf_event_for_each(event, func); |
| 2484 | else |
| 2485 | perf_event_for_each_child(event, func); |
| 2486 | |
| 2487 | return 0; |
| 2488 | } |
| 2489 | |
| 2490 | int perf_event_task_enable(void) |
| 2491 | { |
| 2492 | struct perf_event *event; |
| 2493 | |
| 2494 | mutex_lock(¤t->perf_event_mutex); |
| 2495 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) |
| 2496 | perf_event_for_each_child(event, perf_event_enable); |
| 2497 | mutex_unlock(¤t->perf_event_mutex); |
| 2498 | |
| 2499 | return 0; |
| 2500 | } |
| 2501 | |
| 2502 | int perf_event_task_disable(void) |
| 2503 | { |
| 2504 | struct perf_event *event; |
| 2505 | |
| 2506 | mutex_lock(¤t->perf_event_mutex); |
| 2507 | list_for_each_entry(event, ¤t->perf_event_list, owner_entry) |
| 2508 | perf_event_for_each_child(event, perf_event_disable); |
| 2509 | mutex_unlock(¤t->perf_event_mutex); |
| 2510 | |
| 2511 | return 0; |
| 2512 | } |
| 2513 | |
| 2514 | #ifndef PERF_EVENT_INDEX_OFFSET |
| 2515 | # define PERF_EVENT_INDEX_OFFSET 0 |
| 2516 | #endif |
| 2517 | |
| 2518 | static int perf_event_index(struct perf_event *event) |
| 2519 | { |
| 2520 | if (event->state != PERF_EVENT_STATE_ACTIVE) |
| 2521 | return 0; |
| 2522 | |
| 2523 | return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET; |
| 2524 | } |
| 2525 | |
| 2526 | /* |
| 2527 | * Callers need to ensure there can be no nesting of this function, otherwise |
| 2528 | * the seqlock logic goes bad. We can not serialize this because the arch |
| 2529 | * code calls this from NMI context. |
| 2530 | */ |
| 2531 | void perf_event_update_userpage(struct perf_event *event) |
| 2532 | { |
| 2533 | struct perf_event_mmap_page *userpg; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2534 | struct perf_buffer *buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2535 | |
| 2536 | rcu_read_lock(); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2537 | buffer = rcu_dereference(event->buffer); |
| 2538 | if (!buffer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2539 | goto unlock; |
| 2540 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2541 | userpg = buffer->user_page; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2542 | |
| 2543 | /* |
| 2544 | * Disable preemption so as to not let the corresponding user-space |
| 2545 | * spin too long if we get preempted. |
| 2546 | */ |
| 2547 | preempt_disable(); |
| 2548 | ++userpg->lock; |
| 2549 | barrier(); |
| 2550 | userpg->index = perf_event_index(event); |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 2551 | userpg->offset = perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2552 | if (event->state == PERF_EVENT_STATE_ACTIVE) |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 2553 | userpg->offset -= local64_read(&event->hw.prev_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2554 | |
| 2555 | userpg->time_enabled = event->total_time_enabled + |
| 2556 | atomic64_read(&event->child_total_time_enabled); |
| 2557 | |
| 2558 | userpg->time_running = event->total_time_running + |
| 2559 | atomic64_read(&event->child_total_time_running); |
| 2560 | |
| 2561 | barrier(); |
| 2562 | ++userpg->lock; |
| 2563 | preempt_enable(); |
| 2564 | unlock: |
| 2565 | rcu_read_unlock(); |
| 2566 | } |
| 2567 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 2568 | static unsigned long perf_data_size(struct perf_buffer *buffer); |
| 2569 | |
| 2570 | static void |
| 2571 | perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags) |
| 2572 | { |
| 2573 | long max_size = perf_data_size(buffer); |
| 2574 | |
| 2575 | if (watermark) |
| 2576 | buffer->watermark = min(max_size, watermark); |
| 2577 | |
| 2578 | if (!buffer->watermark) |
| 2579 | buffer->watermark = max_size / 2; |
| 2580 | |
| 2581 | if (flags & PERF_BUFFER_WRITABLE) |
| 2582 | buffer->writable = 1; |
| 2583 | |
| 2584 | atomic_set(&buffer->refcount, 1); |
| 2585 | } |
| 2586 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2587 | #ifndef CONFIG_PERF_USE_VMALLOC |
| 2588 | |
| 2589 | /* |
| 2590 | * Back perf_mmap() with regular GFP_KERNEL-0 pages. |
| 2591 | */ |
| 2592 | |
| 2593 | static struct page * |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2594 | perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2595 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2596 | if (pgoff > buffer->nr_pages) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2597 | return NULL; |
| 2598 | |
| 2599 | if (pgoff == 0) |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2600 | return virt_to_page(buffer->user_page); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2601 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2602 | return virt_to_page(buffer->data_pages[pgoff - 1]); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2603 | } |
| 2604 | |
Peter Zijlstra | a19d35c | 2010-05-17 18:48:00 +0200 | [diff] [blame] | 2605 | static void *perf_mmap_alloc_page(int cpu) |
| 2606 | { |
| 2607 | struct page *page; |
| 2608 | int node; |
| 2609 | |
| 2610 | node = (cpu == -1) ? cpu : cpu_to_node(cpu); |
| 2611 | page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0); |
| 2612 | if (!page) |
| 2613 | return NULL; |
| 2614 | |
| 2615 | return page_address(page); |
| 2616 | } |
| 2617 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2618 | static struct perf_buffer * |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 2619 | perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2620 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2621 | struct perf_buffer *buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2622 | unsigned long size; |
| 2623 | int i; |
| 2624 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2625 | size = sizeof(struct perf_buffer); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2626 | size += nr_pages * sizeof(void *); |
| 2627 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2628 | buffer = kzalloc(size, GFP_KERNEL); |
| 2629 | if (!buffer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2630 | goto fail; |
| 2631 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 2632 | buffer->user_page = perf_mmap_alloc_page(cpu); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2633 | if (!buffer->user_page) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2634 | goto fail_user_page; |
| 2635 | |
| 2636 | for (i = 0; i < nr_pages; i++) { |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 2637 | buffer->data_pages[i] = perf_mmap_alloc_page(cpu); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2638 | if (!buffer->data_pages[i]) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2639 | goto fail_data_pages; |
| 2640 | } |
| 2641 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2642 | buffer->nr_pages = nr_pages; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2643 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 2644 | perf_buffer_init(buffer, watermark, flags); |
| 2645 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2646 | return buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2647 | |
| 2648 | fail_data_pages: |
| 2649 | for (i--; i >= 0; i--) |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2650 | free_page((unsigned long)buffer->data_pages[i]); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2651 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2652 | free_page((unsigned long)buffer->user_page); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2653 | |
| 2654 | fail_user_page: |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2655 | kfree(buffer); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2656 | |
| 2657 | fail: |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2658 | return NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2659 | } |
| 2660 | |
| 2661 | static void perf_mmap_free_page(unsigned long addr) |
| 2662 | { |
| 2663 | struct page *page = virt_to_page((void *)addr); |
| 2664 | |
| 2665 | page->mapping = NULL; |
| 2666 | __free_page(page); |
| 2667 | } |
| 2668 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2669 | static void perf_buffer_free(struct perf_buffer *buffer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2670 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2671 | int i; |
| 2672 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2673 | perf_mmap_free_page((unsigned long)buffer->user_page); |
| 2674 | for (i = 0; i < buffer->nr_pages; i++) |
| 2675 | perf_mmap_free_page((unsigned long)buffer->data_pages[i]); |
| 2676 | kfree(buffer); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2677 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2678 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2679 | static inline int page_order(struct perf_buffer *buffer) |
Peter Zijlstra | 3cafa9f | 2010-05-20 19:07:56 +0200 | [diff] [blame] | 2680 | { |
| 2681 | return 0; |
| 2682 | } |
| 2683 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2684 | #else |
| 2685 | |
| 2686 | /* |
| 2687 | * Back perf_mmap() with vmalloc memory. |
| 2688 | * |
| 2689 | * Required for architectures that have d-cache aliasing issues. |
| 2690 | */ |
| 2691 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2692 | static inline int page_order(struct perf_buffer *buffer) |
Peter Zijlstra | 3cafa9f | 2010-05-20 19:07:56 +0200 | [diff] [blame] | 2693 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2694 | return buffer->page_order; |
Peter Zijlstra | 3cafa9f | 2010-05-20 19:07:56 +0200 | [diff] [blame] | 2695 | } |
| 2696 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2697 | static struct page * |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2698 | perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2699 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2700 | if (pgoff > (1UL << page_order(buffer))) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2701 | return NULL; |
| 2702 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2703 | return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | static void perf_mmap_unmark_page(void *addr) |
| 2707 | { |
| 2708 | struct page *page = vmalloc_to_page(addr); |
| 2709 | |
| 2710 | page->mapping = NULL; |
| 2711 | } |
| 2712 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2713 | static void perf_buffer_free_work(struct work_struct *work) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2714 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2715 | struct perf_buffer *buffer; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2716 | void *base; |
| 2717 | int i, nr; |
| 2718 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2719 | buffer = container_of(work, struct perf_buffer, work); |
| 2720 | nr = 1 << page_order(buffer); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2721 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2722 | base = buffer->user_page; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2723 | for (i = 0; i < nr + 1; i++) |
| 2724 | perf_mmap_unmark_page(base + (i * PAGE_SIZE)); |
| 2725 | |
| 2726 | vfree(base); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2727 | kfree(buffer); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2728 | } |
| 2729 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2730 | static void perf_buffer_free(struct perf_buffer *buffer) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2731 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2732 | schedule_work(&buffer->work); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2733 | } |
| 2734 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2735 | static struct perf_buffer * |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 2736 | perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2737 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2738 | struct perf_buffer *buffer; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2739 | unsigned long size; |
| 2740 | void *all_buf; |
| 2741 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2742 | size = sizeof(struct perf_buffer); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2743 | size += sizeof(void *); |
| 2744 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2745 | buffer = kzalloc(size, GFP_KERNEL); |
| 2746 | if (!buffer) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2747 | goto fail; |
| 2748 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2749 | INIT_WORK(&buffer->work, perf_buffer_free_work); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2750 | |
| 2751 | all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE); |
| 2752 | if (!all_buf) |
| 2753 | goto fail_all_buf; |
| 2754 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2755 | buffer->user_page = all_buf; |
| 2756 | buffer->data_pages[0] = all_buf + PAGE_SIZE; |
| 2757 | buffer->page_order = ilog2(nr_pages); |
| 2758 | buffer->nr_pages = 1; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2759 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 2760 | perf_buffer_init(buffer, watermark, flags); |
| 2761 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2762 | return buffer; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2763 | |
| 2764 | fail_all_buf: |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2765 | kfree(buffer); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2766 | |
| 2767 | fail: |
| 2768 | return NULL; |
| 2769 | } |
| 2770 | |
| 2771 | #endif |
| 2772 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2773 | static unsigned long perf_data_size(struct perf_buffer *buffer) |
Peter Zijlstra | 3cafa9f | 2010-05-20 19:07:56 +0200 | [diff] [blame] | 2774 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2775 | return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer)); |
Peter Zijlstra | 3cafa9f | 2010-05-20 19:07:56 +0200 | [diff] [blame] | 2776 | } |
| 2777 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2778 | static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 2779 | { |
| 2780 | struct perf_event *event = vma->vm_file->private_data; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2781 | struct perf_buffer *buffer; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2782 | int ret = VM_FAULT_SIGBUS; |
| 2783 | |
| 2784 | if (vmf->flags & FAULT_FLAG_MKWRITE) { |
| 2785 | if (vmf->pgoff == 0) |
| 2786 | ret = 0; |
| 2787 | return ret; |
| 2788 | } |
| 2789 | |
| 2790 | rcu_read_lock(); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2791 | buffer = rcu_dereference(event->buffer); |
| 2792 | if (!buffer) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2793 | goto unlock; |
| 2794 | |
| 2795 | if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) |
| 2796 | goto unlock; |
| 2797 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2798 | vmf->page = perf_mmap_to_page(buffer, vmf->pgoff); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2799 | if (!vmf->page) |
| 2800 | goto unlock; |
| 2801 | |
| 2802 | get_page(vmf->page); |
| 2803 | vmf->page->mapping = vma->vm_file->f_mapping; |
| 2804 | vmf->page->index = vmf->pgoff; |
| 2805 | |
| 2806 | ret = 0; |
| 2807 | unlock: |
| 2808 | rcu_read_unlock(); |
| 2809 | |
| 2810 | return ret; |
| 2811 | } |
| 2812 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2813 | static void perf_buffer_free_rcu(struct rcu_head *rcu_head) |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2814 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2815 | struct perf_buffer *buffer; |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2816 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2817 | buffer = container_of(rcu_head, struct perf_buffer, rcu_head); |
| 2818 | perf_buffer_free(buffer); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2819 | } |
| 2820 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2821 | static struct perf_buffer *perf_buffer_get(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2822 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2823 | struct perf_buffer *buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2824 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 2825 | rcu_read_lock(); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2826 | buffer = rcu_dereference(event->buffer); |
| 2827 | if (buffer) { |
| 2828 | if (!atomic_inc_not_zero(&buffer->refcount)) |
| 2829 | buffer = NULL; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 2830 | } |
| 2831 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2832 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2833 | return buffer; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 2834 | } |
| 2835 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2836 | static void perf_buffer_put(struct perf_buffer *buffer) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 2837 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2838 | if (!atomic_dec_and_test(&buffer->refcount)) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 2839 | return; |
| 2840 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2841 | call_rcu(&buffer->rcu_head, perf_buffer_free_rcu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2842 | } |
| 2843 | |
| 2844 | static void perf_mmap_open(struct vm_area_struct *vma) |
| 2845 | { |
| 2846 | struct perf_event *event = vma->vm_file->private_data; |
| 2847 | |
| 2848 | atomic_inc(&event->mmap_count); |
| 2849 | } |
| 2850 | |
| 2851 | static void perf_mmap_close(struct vm_area_struct *vma) |
| 2852 | { |
| 2853 | struct perf_event *event = vma->vm_file->private_data; |
| 2854 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2855 | if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2856 | unsigned long size = perf_data_size(event->buffer); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 2857 | struct user_struct *user = event->mmap_user; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2858 | struct perf_buffer *buffer = event->buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2859 | |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2860 | atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 2861 | vma->vm_mm->locked_vm -= event->mmap_locked; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2862 | rcu_assign_pointer(event->buffer, NULL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2863 | mutex_unlock(&event->mmap_mutex); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 2864 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2865 | perf_buffer_put(buffer); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 2866 | free_uid(user); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2867 | } |
| 2868 | } |
| 2869 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 2870 | static const struct vm_operations_struct perf_mmap_vmops = { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2871 | .open = perf_mmap_open, |
| 2872 | .close = perf_mmap_close, |
| 2873 | .fault = perf_mmap_fault, |
| 2874 | .page_mkwrite = perf_mmap_fault, |
| 2875 | }; |
| 2876 | |
| 2877 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) |
| 2878 | { |
| 2879 | struct perf_event *event = file->private_data; |
| 2880 | unsigned long user_locked, user_lock_limit; |
| 2881 | struct user_struct *user = current_user(); |
| 2882 | unsigned long locked, lock_limit; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2883 | struct perf_buffer *buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2884 | unsigned long vma_size; |
| 2885 | unsigned long nr_pages; |
| 2886 | long user_extra, extra; |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 2887 | int ret = 0, flags = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2888 | |
Peter Zijlstra | c792061 | 2010-05-18 10:33:24 +0200 | [diff] [blame] | 2889 | /* |
| 2890 | * Don't allow mmap() of inherited per-task counters. This would |
| 2891 | * create a performance issue due to all children writing to the |
| 2892 | * same buffer. |
| 2893 | */ |
| 2894 | if (event->cpu == -1 && event->attr.inherit) |
| 2895 | return -EINVAL; |
| 2896 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2897 | if (!(vma->vm_flags & VM_SHARED)) |
| 2898 | return -EINVAL; |
| 2899 | |
| 2900 | vma_size = vma->vm_end - vma->vm_start; |
| 2901 | nr_pages = (vma_size / PAGE_SIZE) - 1; |
| 2902 | |
| 2903 | /* |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2904 | * If we have buffer pages ensure they're a power-of-two number, so we |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2905 | * can do bitmasks instead of modulo. |
| 2906 | */ |
| 2907 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
| 2908 | return -EINVAL; |
| 2909 | |
| 2910 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
| 2911 | return -EINVAL; |
| 2912 | |
| 2913 | if (vma->vm_pgoff != 0) |
| 2914 | return -EINVAL; |
| 2915 | |
| 2916 | WARN_ON_ONCE(event->ctx->parent_ctx); |
| 2917 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2918 | if (event->buffer) { |
| 2919 | if (event->buffer->nr_pages == nr_pages) |
| 2920 | atomic_inc(&event->buffer->refcount); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 2921 | else |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2922 | ret = -EINVAL; |
| 2923 | goto unlock; |
| 2924 | } |
| 2925 | |
| 2926 | user_extra = nr_pages + 1; |
| 2927 | user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); |
| 2928 | |
| 2929 | /* |
| 2930 | * Increase the limit linearly with more CPUs: |
| 2931 | */ |
| 2932 | user_lock_limit *= num_online_cpus(); |
| 2933 | |
| 2934 | user_locked = atomic_long_read(&user->locked_vm) + user_extra; |
| 2935 | |
| 2936 | extra = 0; |
| 2937 | if (user_locked > user_lock_limit) |
| 2938 | extra = user_locked - user_lock_limit; |
| 2939 | |
Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 2940 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2941 | lock_limit >>= PAGE_SHIFT; |
| 2942 | locked = vma->vm_mm->locked_vm + extra; |
| 2943 | |
| 2944 | if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && |
| 2945 | !capable(CAP_IPC_LOCK)) { |
| 2946 | ret = -EPERM; |
| 2947 | goto unlock; |
| 2948 | } |
| 2949 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2950 | WARN_ON(event->buffer); |
Peter Zijlstra | 906010b | 2009-09-21 16:08:49 +0200 | [diff] [blame] | 2951 | |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 2952 | if (vma->vm_flags & VM_WRITE) |
| 2953 | flags |= PERF_BUFFER_WRITABLE; |
| 2954 | |
| 2955 | buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark, |
| 2956 | event->cpu, flags); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 2957 | if (!buffer) { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 2958 | ret = -ENOMEM; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2959 | goto unlock; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 2960 | } |
Peter Zijlstra | d57e34f | 2010-05-28 19:41:35 +0200 | [diff] [blame] | 2961 | rcu_assign_pointer(event->buffer, buffer); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2962 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 2963 | atomic_long_add(user_extra, &user->locked_vm); |
| 2964 | event->mmap_locked = extra; |
| 2965 | event->mmap_user = get_current_user(); |
| 2966 | vma->vm_mm->locked_vm += event->mmap_locked; |
| 2967 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2968 | unlock: |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 2969 | if (!ret) |
| 2970 | atomic_inc(&event->mmap_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2971 | mutex_unlock(&event->mmap_mutex); |
| 2972 | |
| 2973 | vma->vm_flags |= VM_RESERVED; |
| 2974 | vma->vm_ops = &perf_mmap_vmops; |
| 2975 | |
| 2976 | return ret; |
| 2977 | } |
| 2978 | |
| 2979 | static int perf_fasync(int fd, struct file *filp, int on) |
| 2980 | { |
| 2981 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 2982 | struct perf_event *event = filp->private_data; |
| 2983 | int retval; |
| 2984 | |
| 2985 | mutex_lock(&inode->i_mutex); |
| 2986 | retval = fasync_helper(fd, filp, on, &event->fasync); |
| 2987 | mutex_unlock(&inode->i_mutex); |
| 2988 | |
| 2989 | if (retval < 0) |
| 2990 | return retval; |
| 2991 | |
| 2992 | return 0; |
| 2993 | } |
| 2994 | |
| 2995 | static const struct file_operations perf_fops = { |
Arnd Bergmann | 3326c1c | 2010-03-23 19:09:33 +0100 | [diff] [blame] | 2996 | .llseek = no_llseek, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2997 | .release = perf_release, |
| 2998 | .read = perf_read, |
| 2999 | .poll = perf_poll, |
| 3000 | .unlocked_ioctl = perf_ioctl, |
| 3001 | .compat_ioctl = perf_ioctl, |
| 3002 | .mmap = perf_mmap, |
| 3003 | .fasync = perf_fasync, |
| 3004 | }; |
| 3005 | |
| 3006 | /* |
| 3007 | * Perf event wakeup |
| 3008 | * |
| 3009 | * If there's data, ensure we set the poll() state and publish everything |
| 3010 | * to user-space before waking everybody up. |
| 3011 | */ |
| 3012 | |
| 3013 | void perf_event_wakeup(struct perf_event *event) |
| 3014 | { |
| 3015 | wake_up_all(&event->waitq); |
| 3016 | |
| 3017 | if (event->pending_kill) { |
| 3018 | kill_fasync(&event->fasync, SIGIO, event->pending_kill); |
| 3019 | event->pending_kill = 0; |
| 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | /* |
| 3024 | * Pending wakeups |
| 3025 | * |
| 3026 | * Handle the case where we need to wakeup up from NMI (or rq->lock) context. |
| 3027 | * |
| 3028 | * The NMI bit means we cannot possibly take locks. Therefore, maintain a |
| 3029 | * single linked list and use cmpxchg() to add entries lockless. |
| 3030 | */ |
| 3031 | |
| 3032 | static void perf_pending_event(struct perf_pending_entry *entry) |
| 3033 | { |
| 3034 | struct perf_event *event = container_of(entry, |
| 3035 | struct perf_event, pending); |
| 3036 | |
| 3037 | if (event->pending_disable) { |
| 3038 | event->pending_disable = 0; |
| 3039 | __perf_event_disable(event); |
| 3040 | } |
| 3041 | |
| 3042 | if (event->pending_wakeup) { |
| 3043 | event->pending_wakeup = 0; |
| 3044 | perf_event_wakeup(event); |
| 3045 | } |
| 3046 | } |
| 3047 | |
| 3048 | #define PENDING_TAIL ((struct perf_pending_entry *)-1UL) |
| 3049 | |
| 3050 | static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = { |
| 3051 | PENDING_TAIL, |
| 3052 | }; |
| 3053 | |
| 3054 | static void perf_pending_queue(struct perf_pending_entry *entry, |
| 3055 | void (*func)(struct perf_pending_entry *)) |
| 3056 | { |
| 3057 | struct perf_pending_entry **head; |
| 3058 | |
| 3059 | if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL) |
| 3060 | return; |
| 3061 | |
| 3062 | entry->func = func; |
| 3063 | |
| 3064 | head = &get_cpu_var(perf_pending_head); |
| 3065 | |
| 3066 | do { |
| 3067 | entry->next = *head; |
| 3068 | } while (cmpxchg(head, entry->next, entry) != entry->next); |
| 3069 | |
| 3070 | set_perf_event_pending(); |
| 3071 | |
| 3072 | put_cpu_var(perf_pending_head); |
| 3073 | } |
| 3074 | |
| 3075 | static int __perf_pending_run(void) |
| 3076 | { |
| 3077 | struct perf_pending_entry *list; |
| 3078 | int nr = 0; |
| 3079 | |
| 3080 | list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL); |
| 3081 | while (list != PENDING_TAIL) { |
| 3082 | void (*func)(struct perf_pending_entry *); |
| 3083 | struct perf_pending_entry *entry = list; |
| 3084 | |
| 3085 | list = list->next; |
| 3086 | |
| 3087 | func = entry->func; |
| 3088 | entry->next = NULL; |
| 3089 | /* |
| 3090 | * Ensure we observe the unqueue before we issue the wakeup, |
| 3091 | * so that we won't be waiting forever. |
| 3092 | * -- see perf_not_pending(). |
| 3093 | */ |
| 3094 | smp_wmb(); |
| 3095 | |
| 3096 | func(entry); |
| 3097 | nr++; |
| 3098 | } |
| 3099 | |
| 3100 | return nr; |
| 3101 | } |
| 3102 | |
| 3103 | static inline int perf_not_pending(struct perf_event *event) |
| 3104 | { |
| 3105 | /* |
| 3106 | * If we flush on whatever cpu we run, there is a chance we don't |
| 3107 | * need to wait. |
| 3108 | */ |
| 3109 | get_cpu(); |
| 3110 | __perf_pending_run(); |
| 3111 | put_cpu(); |
| 3112 | |
| 3113 | /* |
| 3114 | * Ensure we see the proper queue state before going to sleep |
| 3115 | * so that we do not miss the wakeup. -- see perf_pending_handle() |
| 3116 | */ |
| 3117 | smp_rmb(); |
| 3118 | return event->pending.next == NULL; |
| 3119 | } |
| 3120 | |
| 3121 | static void perf_pending_sync(struct perf_event *event) |
| 3122 | { |
| 3123 | wait_event(event->waitq, perf_not_pending(event)); |
| 3124 | } |
| 3125 | |
| 3126 | void perf_event_do_pending(void) |
| 3127 | { |
| 3128 | __perf_pending_run(); |
| 3129 | } |
| 3130 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3131 | /* |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 3132 | * We assume there is only KVM supporting the callbacks. |
| 3133 | * Later on, we might change it to a list if there is |
| 3134 | * another virtualization implementation supporting the callbacks. |
| 3135 | */ |
| 3136 | struct perf_guest_info_callbacks *perf_guest_cbs; |
| 3137 | |
| 3138 | int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 3139 | { |
| 3140 | perf_guest_cbs = cbs; |
| 3141 | return 0; |
| 3142 | } |
| 3143 | EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); |
| 3144 | |
| 3145 | int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) |
| 3146 | { |
| 3147 | perf_guest_cbs = NULL; |
| 3148 | return 0; |
| 3149 | } |
| 3150 | EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); |
| 3151 | |
| 3152 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3153 | * Output |
| 3154 | */ |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3155 | static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3156 | unsigned long offset, unsigned long head) |
| 3157 | { |
| 3158 | unsigned long mask; |
| 3159 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3160 | if (!buffer->writable) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3161 | return true; |
| 3162 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3163 | mask = perf_data_size(buffer) - 1; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3164 | |
| 3165 | offset = (offset - tail) & mask; |
| 3166 | head = (head - tail) & mask; |
| 3167 | |
| 3168 | if ((int)(head - offset) < 0) |
| 3169 | return false; |
| 3170 | |
| 3171 | return true; |
| 3172 | } |
| 3173 | |
| 3174 | static void perf_output_wakeup(struct perf_output_handle *handle) |
| 3175 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3176 | atomic_set(&handle->buffer->poll, POLL_IN); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3177 | |
| 3178 | if (handle->nmi) { |
| 3179 | handle->event->pending_wakeup = 1; |
| 3180 | perf_pending_queue(&handle->event->pending, |
| 3181 | perf_pending_event); |
| 3182 | } else |
| 3183 | perf_event_wakeup(handle->event); |
| 3184 | } |
| 3185 | |
| 3186 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3187 | * We need to ensure a later event_id doesn't publish a head when a former |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3188 | * event isn't done writing. However since we need to deal with NMIs we |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3189 | * cannot fully serialize things. |
| 3190 | * |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3191 | * We only publish the head (and generate a wakeup) when the outer-most |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3192 | * event completes. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3193 | */ |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3194 | static void perf_output_get_handle(struct perf_output_handle *handle) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3195 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3196 | struct perf_buffer *buffer = handle->buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3197 | |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3198 | preempt_disable(); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3199 | local_inc(&buffer->nest); |
| 3200 | handle->wakeup = local_read(&buffer->wakeup); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3201 | } |
| 3202 | |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3203 | static void perf_output_put_handle(struct perf_output_handle *handle) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3204 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3205 | struct perf_buffer *buffer = handle->buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3206 | unsigned long head; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3207 | |
| 3208 | again: |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3209 | head = local_read(&buffer->head); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3210 | |
| 3211 | /* |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3212 | * IRQ/NMI can happen here, which means we can miss a head update. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3213 | */ |
| 3214 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3215 | if (!local_dec_and_test(&buffer->nest)) |
Frederic Weisbecker | acd35a4 | 2010-05-20 21:28:34 +0200 | [diff] [blame] | 3216 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3217 | |
| 3218 | /* |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3219 | * Publish the known good head. Rely on the full barrier implied |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3220 | * by atomic_dec_and_test() order the buffer->head read and this |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3221 | * write. |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3222 | */ |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3223 | buffer->user_page->data_head = head; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3224 | |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3225 | /* |
| 3226 | * Now check if we missed an update, rely on the (compiler) |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3227 | * barrier in atomic_dec_and_test() to re-read buffer->head. |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3228 | */ |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3229 | if (unlikely(head != local_read(&buffer->head))) { |
| 3230 | local_inc(&buffer->nest); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3231 | goto again; |
| 3232 | } |
| 3233 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3234 | if (handle->wakeup != local_read(&buffer->wakeup)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3235 | perf_output_wakeup(handle); |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3236 | |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 3237 | out: |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3238 | preempt_enable(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3239 | } |
| 3240 | |
Peter Zijlstra | a94ffaa | 2010-05-20 19:50:07 +0200 | [diff] [blame] | 3241 | __always_inline void perf_output_copy(struct perf_output_handle *handle, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3242 | const void *buf, unsigned int len) |
| 3243 | { |
Peter Zijlstra | 5d967a8 | 2010-05-20 16:46:39 +0200 | [diff] [blame] | 3244 | do { |
Peter Zijlstra | a94ffaa | 2010-05-20 19:50:07 +0200 | [diff] [blame] | 3245 | unsigned long size = min_t(unsigned long, handle->size, len); |
Peter Zijlstra | 5d967a8 | 2010-05-20 16:46:39 +0200 | [diff] [blame] | 3246 | |
| 3247 | memcpy(handle->addr, buf, size); |
| 3248 | |
| 3249 | len -= size; |
| 3250 | handle->addr += size; |
Frederic Weisbecker | 74048f8 | 2010-05-27 21:34:58 +0200 | [diff] [blame] | 3251 | buf += size; |
Peter Zijlstra | 5d967a8 | 2010-05-20 16:46:39 +0200 | [diff] [blame] | 3252 | handle->size -= size; |
| 3253 | if (!handle->size) { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3254 | struct perf_buffer *buffer = handle->buffer; |
Peter Zijlstra | 3cafa9f | 2010-05-20 19:07:56 +0200 | [diff] [blame] | 3255 | |
Peter Zijlstra | 5d967a8 | 2010-05-20 16:46:39 +0200 | [diff] [blame] | 3256 | handle->page++; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3257 | handle->page &= buffer->nr_pages - 1; |
| 3258 | handle->addr = buffer->data_pages[handle->page]; |
| 3259 | handle->size = PAGE_SIZE << page_order(buffer); |
Peter Zijlstra | 5d967a8 | 2010-05-20 16:46:39 +0200 | [diff] [blame] | 3260 | } |
| 3261 | } while (len); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3262 | } |
| 3263 | |
| 3264 | int perf_output_begin(struct perf_output_handle *handle, |
| 3265 | struct perf_event *event, unsigned int size, |
| 3266 | int nmi, int sample) |
| 3267 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3268 | struct perf_buffer *buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3269 | unsigned long tail, offset, head; |
| 3270 | int have_lost; |
| 3271 | struct { |
| 3272 | struct perf_event_header header; |
| 3273 | u64 id; |
| 3274 | u64 lost; |
| 3275 | } lost_event; |
| 3276 | |
| 3277 | rcu_read_lock(); |
| 3278 | /* |
| 3279 | * For inherited events we send all the output towards the parent. |
| 3280 | */ |
| 3281 | if (event->parent) |
| 3282 | event = event->parent; |
| 3283 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3284 | buffer = rcu_dereference(event->buffer); |
| 3285 | if (!buffer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3286 | goto out; |
| 3287 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3288 | handle->buffer = buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3289 | handle->event = event; |
| 3290 | handle->nmi = nmi; |
| 3291 | handle->sample = sample; |
| 3292 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3293 | if (!buffer->nr_pages) |
Stephane Eranian | 00d1d0b | 2010-05-17 12:46:01 +0200 | [diff] [blame] | 3294 | goto out; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3295 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3296 | have_lost = local_read(&buffer->lost); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3297 | if (have_lost) |
| 3298 | size += sizeof(lost_event); |
| 3299 | |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3300 | perf_output_get_handle(handle); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3301 | |
| 3302 | do { |
| 3303 | /* |
| 3304 | * Userspace could choose to issue a mb() before updating the |
| 3305 | * tail pointer. So that all reads will be completed before the |
| 3306 | * write is issued. |
| 3307 | */ |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3308 | tail = ACCESS_ONCE(buffer->user_page->data_tail); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3309 | smp_rmb(); |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3310 | offset = head = local_read(&buffer->head); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3311 | head += size; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3312 | if (unlikely(!perf_output_space(buffer, tail, offset, head))) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3313 | goto fail; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3314 | } while (local_cmpxchg(&buffer->head, offset, head) != offset); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3315 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3316 | if (head - local_read(&buffer->wakeup) > buffer->watermark) |
| 3317 | local_add(buffer->watermark, &buffer->wakeup); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3318 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3319 | handle->page = offset >> (PAGE_SHIFT + page_order(buffer)); |
| 3320 | handle->page &= buffer->nr_pages - 1; |
| 3321 | handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1); |
| 3322 | handle->addr = buffer->data_pages[handle->page]; |
Peter Zijlstra | 5d967a8 | 2010-05-20 16:46:39 +0200 | [diff] [blame] | 3323 | handle->addr += handle->size; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3324 | handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size; |
Peter Zijlstra | 5d967a8 | 2010-05-20 16:46:39 +0200 | [diff] [blame] | 3325 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3326 | if (have_lost) { |
| 3327 | lost_event.header.type = PERF_RECORD_LOST; |
| 3328 | lost_event.header.misc = 0; |
| 3329 | lost_event.header.size = sizeof(lost_event); |
| 3330 | lost_event.id = event->id; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3331 | lost_event.lost = local_xchg(&buffer->lost, 0); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3332 | |
| 3333 | perf_output_put(handle, lost_event); |
| 3334 | } |
| 3335 | |
| 3336 | return 0; |
| 3337 | |
| 3338 | fail: |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3339 | local_inc(&buffer->lost); |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3340 | perf_output_put_handle(handle); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3341 | out: |
| 3342 | rcu_read_unlock(); |
| 3343 | |
| 3344 | return -ENOSPC; |
| 3345 | } |
| 3346 | |
| 3347 | void perf_output_end(struct perf_output_handle *handle) |
| 3348 | { |
| 3349 | struct perf_event *event = handle->event; |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3350 | struct perf_buffer *buffer = handle->buffer; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3351 | |
| 3352 | int wakeup_events = event->attr.wakeup_events; |
| 3353 | |
| 3354 | if (handle->sample && wakeup_events) { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3355 | int events = local_inc_return(&buffer->events); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3356 | if (events >= wakeup_events) { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 3357 | local_sub(wakeup_events, &buffer->events); |
| 3358 | local_inc(&buffer->wakeup); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3359 | } |
| 3360 | } |
| 3361 | |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3362 | perf_output_put_handle(handle); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3363 | rcu_read_unlock(); |
| 3364 | } |
| 3365 | |
| 3366 | static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) |
| 3367 | { |
| 3368 | /* |
| 3369 | * only top level events have the pid namespace they were created in |
| 3370 | */ |
| 3371 | if (event->parent) |
| 3372 | event = event->parent; |
| 3373 | |
| 3374 | return task_tgid_nr_ns(p, event->ns); |
| 3375 | } |
| 3376 | |
| 3377 | static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) |
| 3378 | { |
| 3379 | /* |
| 3380 | * only top level events have the pid namespace they were created in |
| 3381 | */ |
| 3382 | if (event->parent) |
| 3383 | event = event->parent; |
| 3384 | |
| 3385 | return task_pid_nr_ns(p, event->ns); |
| 3386 | } |
| 3387 | |
| 3388 | static void perf_output_read_one(struct perf_output_handle *handle, |
| 3389 | struct perf_event *event) |
| 3390 | { |
| 3391 | u64 read_format = event->attr.read_format; |
| 3392 | u64 values[4]; |
| 3393 | int n = 0; |
| 3394 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3395 | values[n++] = perf_event_count(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3396 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
| 3397 | values[n++] = event->total_time_enabled + |
| 3398 | atomic64_read(&event->child_total_time_enabled); |
| 3399 | } |
| 3400 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
| 3401 | values[n++] = event->total_time_running + |
| 3402 | atomic64_read(&event->child_total_time_running); |
| 3403 | } |
| 3404 | if (read_format & PERF_FORMAT_ID) |
| 3405 | values[n++] = primary_event_id(event); |
| 3406 | |
| 3407 | perf_output_copy(handle, values, n * sizeof(u64)); |
| 3408 | } |
| 3409 | |
| 3410 | /* |
| 3411 | * XXX PERF_FORMAT_GROUP vs inherited events seems difficult. |
| 3412 | */ |
| 3413 | static void perf_output_read_group(struct perf_output_handle *handle, |
| 3414 | struct perf_event *event) |
| 3415 | { |
| 3416 | struct perf_event *leader = event->group_leader, *sub; |
| 3417 | u64 read_format = event->attr.read_format; |
| 3418 | u64 values[5]; |
| 3419 | int n = 0; |
| 3420 | |
| 3421 | values[n++] = 1 + leader->nr_siblings; |
| 3422 | |
| 3423 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 3424 | values[n++] = leader->total_time_enabled; |
| 3425 | |
| 3426 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 3427 | values[n++] = leader->total_time_running; |
| 3428 | |
| 3429 | if (leader != event) |
| 3430 | leader->pmu->read(leader); |
| 3431 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3432 | values[n++] = perf_event_count(leader); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3433 | if (read_format & PERF_FORMAT_ID) |
| 3434 | values[n++] = primary_event_id(leader); |
| 3435 | |
| 3436 | perf_output_copy(handle, values, n * sizeof(u64)); |
| 3437 | |
| 3438 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { |
| 3439 | n = 0; |
| 3440 | |
| 3441 | if (sub != event) |
| 3442 | sub->pmu->read(sub); |
| 3443 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 3444 | values[n++] = perf_event_count(sub); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3445 | if (read_format & PERF_FORMAT_ID) |
| 3446 | values[n++] = primary_event_id(sub); |
| 3447 | |
| 3448 | perf_output_copy(handle, values, n * sizeof(u64)); |
| 3449 | } |
| 3450 | } |
| 3451 | |
| 3452 | static void perf_output_read(struct perf_output_handle *handle, |
| 3453 | struct perf_event *event) |
| 3454 | { |
| 3455 | if (event->attr.read_format & PERF_FORMAT_GROUP) |
| 3456 | perf_output_read_group(handle, event); |
| 3457 | else |
| 3458 | perf_output_read_one(handle, event); |
| 3459 | } |
| 3460 | |
| 3461 | void perf_output_sample(struct perf_output_handle *handle, |
| 3462 | struct perf_event_header *header, |
| 3463 | struct perf_sample_data *data, |
| 3464 | struct perf_event *event) |
| 3465 | { |
| 3466 | u64 sample_type = data->type; |
| 3467 | |
| 3468 | perf_output_put(handle, *header); |
| 3469 | |
| 3470 | if (sample_type & PERF_SAMPLE_IP) |
| 3471 | perf_output_put(handle, data->ip); |
| 3472 | |
| 3473 | if (sample_type & PERF_SAMPLE_TID) |
| 3474 | perf_output_put(handle, data->tid_entry); |
| 3475 | |
| 3476 | if (sample_type & PERF_SAMPLE_TIME) |
| 3477 | perf_output_put(handle, data->time); |
| 3478 | |
| 3479 | if (sample_type & PERF_SAMPLE_ADDR) |
| 3480 | perf_output_put(handle, data->addr); |
| 3481 | |
| 3482 | if (sample_type & PERF_SAMPLE_ID) |
| 3483 | perf_output_put(handle, data->id); |
| 3484 | |
| 3485 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 3486 | perf_output_put(handle, data->stream_id); |
| 3487 | |
| 3488 | if (sample_type & PERF_SAMPLE_CPU) |
| 3489 | perf_output_put(handle, data->cpu_entry); |
| 3490 | |
| 3491 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 3492 | perf_output_put(handle, data->period); |
| 3493 | |
| 3494 | if (sample_type & PERF_SAMPLE_READ) |
| 3495 | perf_output_read(handle, event); |
| 3496 | |
| 3497 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 3498 | if (data->callchain) { |
| 3499 | int size = 1; |
| 3500 | |
| 3501 | if (data->callchain) |
| 3502 | size += data->callchain->nr; |
| 3503 | |
| 3504 | size *= sizeof(u64); |
| 3505 | |
| 3506 | perf_output_copy(handle, data->callchain, size); |
| 3507 | } else { |
| 3508 | u64 nr = 0; |
| 3509 | perf_output_put(handle, nr); |
| 3510 | } |
| 3511 | } |
| 3512 | |
| 3513 | if (sample_type & PERF_SAMPLE_RAW) { |
| 3514 | if (data->raw) { |
| 3515 | perf_output_put(handle, data->raw->size); |
| 3516 | perf_output_copy(handle, data->raw->data, |
| 3517 | data->raw->size); |
| 3518 | } else { |
| 3519 | struct { |
| 3520 | u32 size; |
| 3521 | u32 data; |
| 3522 | } raw = { |
| 3523 | .size = sizeof(u32), |
| 3524 | .data = 0, |
| 3525 | }; |
| 3526 | perf_output_put(handle, raw); |
| 3527 | } |
| 3528 | } |
| 3529 | } |
| 3530 | |
| 3531 | void perf_prepare_sample(struct perf_event_header *header, |
| 3532 | struct perf_sample_data *data, |
| 3533 | struct perf_event *event, |
| 3534 | struct pt_regs *regs) |
| 3535 | { |
| 3536 | u64 sample_type = event->attr.sample_type; |
| 3537 | |
| 3538 | data->type = sample_type; |
| 3539 | |
| 3540 | header->type = PERF_RECORD_SAMPLE; |
| 3541 | header->size = sizeof(*header); |
| 3542 | |
| 3543 | header->misc = 0; |
| 3544 | header->misc |= perf_misc_flags(regs); |
| 3545 | |
| 3546 | if (sample_type & PERF_SAMPLE_IP) { |
| 3547 | data->ip = perf_instruction_pointer(regs); |
| 3548 | |
| 3549 | header->size += sizeof(data->ip); |
| 3550 | } |
| 3551 | |
| 3552 | if (sample_type & PERF_SAMPLE_TID) { |
| 3553 | /* namespace issues */ |
| 3554 | data->tid_entry.pid = perf_event_pid(event, current); |
| 3555 | data->tid_entry.tid = perf_event_tid(event, current); |
| 3556 | |
| 3557 | header->size += sizeof(data->tid_entry); |
| 3558 | } |
| 3559 | |
| 3560 | if (sample_type & PERF_SAMPLE_TIME) { |
| 3561 | data->time = perf_clock(); |
| 3562 | |
| 3563 | header->size += sizeof(data->time); |
| 3564 | } |
| 3565 | |
| 3566 | if (sample_type & PERF_SAMPLE_ADDR) |
| 3567 | header->size += sizeof(data->addr); |
| 3568 | |
| 3569 | if (sample_type & PERF_SAMPLE_ID) { |
| 3570 | data->id = primary_event_id(event); |
| 3571 | |
| 3572 | header->size += sizeof(data->id); |
| 3573 | } |
| 3574 | |
| 3575 | if (sample_type & PERF_SAMPLE_STREAM_ID) { |
| 3576 | data->stream_id = event->id; |
| 3577 | |
| 3578 | header->size += sizeof(data->stream_id); |
| 3579 | } |
| 3580 | |
| 3581 | if (sample_type & PERF_SAMPLE_CPU) { |
| 3582 | data->cpu_entry.cpu = raw_smp_processor_id(); |
| 3583 | data->cpu_entry.reserved = 0; |
| 3584 | |
| 3585 | header->size += sizeof(data->cpu_entry); |
| 3586 | } |
| 3587 | |
| 3588 | if (sample_type & PERF_SAMPLE_PERIOD) |
| 3589 | header->size += sizeof(data->period); |
| 3590 | |
| 3591 | if (sample_type & PERF_SAMPLE_READ) |
| 3592 | header->size += perf_event_read_size(event); |
| 3593 | |
| 3594 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 3595 | int size = 1; |
| 3596 | |
| 3597 | data->callchain = perf_callchain(regs); |
| 3598 | |
| 3599 | if (data->callchain) |
| 3600 | size += data->callchain->nr; |
| 3601 | |
| 3602 | header->size += size * sizeof(u64); |
| 3603 | } |
| 3604 | |
| 3605 | if (sample_type & PERF_SAMPLE_RAW) { |
| 3606 | int size = sizeof(u32); |
| 3607 | |
| 3608 | if (data->raw) |
| 3609 | size += data->raw->size; |
| 3610 | else |
| 3611 | size += sizeof(u32); |
| 3612 | |
| 3613 | WARN_ON_ONCE(size & (sizeof(u64)-1)); |
| 3614 | header->size += size; |
| 3615 | } |
| 3616 | } |
| 3617 | |
| 3618 | static void perf_event_output(struct perf_event *event, int nmi, |
| 3619 | struct perf_sample_data *data, |
| 3620 | struct pt_regs *regs) |
| 3621 | { |
| 3622 | struct perf_output_handle handle; |
| 3623 | struct perf_event_header header; |
| 3624 | |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 3625 | /* protect the callchain buffers */ |
| 3626 | rcu_read_lock(); |
| 3627 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3628 | perf_prepare_sample(&header, data, event, regs); |
| 3629 | |
| 3630 | if (perf_output_begin(&handle, event, header.size, nmi, 1)) |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 3631 | goto exit; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3632 | |
| 3633 | perf_output_sample(&handle, &header, data, event); |
| 3634 | |
| 3635 | perf_output_end(&handle); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 3636 | |
| 3637 | exit: |
| 3638 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | /* |
| 3642 | * read event_id |
| 3643 | */ |
| 3644 | |
| 3645 | struct perf_read_event { |
| 3646 | struct perf_event_header header; |
| 3647 | |
| 3648 | u32 pid; |
| 3649 | u32 tid; |
| 3650 | }; |
| 3651 | |
| 3652 | static void |
| 3653 | perf_event_read_event(struct perf_event *event, |
| 3654 | struct task_struct *task) |
| 3655 | { |
| 3656 | struct perf_output_handle handle; |
| 3657 | struct perf_read_event read_event = { |
| 3658 | .header = { |
| 3659 | .type = PERF_RECORD_READ, |
| 3660 | .misc = 0, |
| 3661 | .size = sizeof(read_event) + perf_event_read_size(event), |
| 3662 | }, |
| 3663 | .pid = perf_event_pid(event, task), |
| 3664 | .tid = perf_event_tid(event, task), |
| 3665 | }; |
| 3666 | int ret; |
| 3667 | |
| 3668 | ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0); |
| 3669 | if (ret) |
| 3670 | return; |
| 3671 | |
| 3672 | perf_output_put(&handle, read_event); |
| 3673 | perf_output_read(&handle, event); |
| 3674 | |
| 3675 | perf_output_end(&handle); |
| 3676 | } |
| 3677 | |
| 3678 | /* |
| 3679 | * task tracking -- fork/exit |
| 3680 | * |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 3681 | * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3682 | */ |
| 3683 | |
| 3684 | struct perf_task_event { |
| 3685 | struct task_struct *task; |
| 3686 | struct perf_event_context *task_ctx; |
| 3687 | |
| 3688 | struct { |
| 3689 | struct perf_event_header header; |
| 3690 | |
| 3691 | u32 pid; |
| 3692 | u32 ppid; |
| 3693 | u32 tid; |
| 3694 | u32 ptid; |
| 3695 | u64 time; |
| 3696 | } event_id; |
| 3697 | }; |
| 3698 | |
| 3699 | static void perf_event_task_output(struct perf_event *event, |
| 3700 | struct perf_task_event *task_event) |
| 3701 | { |
| 3702 | struct perf_output_handle handle; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3703 | struct task_struct *task = task_event->task; |
Mike Galbraith | 8bb39f9 | 2010-03-26 11:11:33 +0100 | [diff] [blame] | 3704 | int size, ret; |
| 3705 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3706 | size = task_event->event_id.header.size; |
| 3707 | ret = perf_output_begin(&handle, event, size, 0, 0); |
| 3708 | |
Peter Zijlstra | ef60777 | 2010-05-18 10:50:41 +0200 | [diff] [blame] | 3709 | if (ret) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3710 | return; |
| 3711 | |
| 3712 | task_event->event_id.pid = perf_event_pid(event, task); |
| 3713 | task_event->event_id.ppid = perf_event_pid(event, current); |
| 3714 | |
| 3715 | task_event->event_id.tid = perf_event_tid(event, task); |
| 3716 | task_event->event_id.ptid = perf_event_tid(event, current); |
| 3717 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3718 | perf_output_put(&handle, task_event->event_id); |
| 3719 | |
| 3720 | perf_output_end(&handle); |
| 3721 | } |
| 3722 | |
| 3723 | static int perf_event_task_match(struct perf_event *event) |
| 3724 | { |
Peter Zijlstra | 6f93d0a | 2010-02-14 11:12:04 +0100 | [diff] [blame] | 3725 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
Peter Zijlstra | 22e1908 | 2010-01-18 09:12:32 +0100 | [diff] [blame] | 3726 | return 0; |
| 3727 | |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 3728 | if (event->cpu != -1 && event->cpu != smp_processor_id()) |
| 3729 | return 0; |
| 3730 | |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 3731 | if (event->attr.comm || event->attr.mmap || |
| 3732 | event->attr.mmap_data || event->attr.task) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3733 | return 1; |
| 3734 | |
| 3735 | return 0; |
| 3736 | } |
| 3737 | |
| 3738 | static void perf_event_task_ctx(struct perf_event_context *ctx, |
| 3739 | struct perf_task_event *task_event) |
| 3740 | { |
| 3741 | struct perf_event *event; |
| 3742 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3743 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 3744 | if (perf_event_task_match(event)) |
| 3745 | perf_event_task_output(event, task_event); |
| 3746 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3747 | } |
| 3748 | |
| 3749 | static void perf_event_task_event(struct perf_task_event *task_event) |
| 3750 | { |
| 3751 | struct perf_cpu_context *cpuctx; |
| 3752 | struct perf_event_context *ctx = task_event->task_ctx; |
| 3753 | |
Peter Zijlstra | d6ff86c | 2009-11-20 22:19:46 +0100 | [diff] [blame] | 3754 | rcu_read_lock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3755 | cpuctx = &get_cpu_var(perf_cpu_context); |
| 3756 | perf_event_task_ctx(&cpuctx->ctx, task_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3757 | if (!ctx) |
Peter Zijlstra | 6f93d0a | 2010-02-14 11:12:04 +0100 | [diff] [blame] | 3758 | ctx = rcu_dereference(current->perf_event_ctxp); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3759 | if (ctx) |
| 3760 | perf_event_task_ctx(ctx, task_event); |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 3761 | put_cpu_var(perf_cpu_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3762 | rcu_read_unlock(); |
| 3763 | } |
| 3764 | |
| 3765 | static void perf_event_task(struct task_struct *task, |
| 3766 | struct perf_event_context *task_ctx, |
| 3767 | int new) |
| 3768 | { |
| 3769 | struct perf_task_event task_event; |
| 3770 | |
| 3771 | if (!atomic_read(&nr_comm_events) && |
| 3772 | !atomic_read(&nr_mmap_events) && |
| 3773 | !atomic_read(&nr_task_events)) |
| 3774 | return; |
| 3775 | |
| 3776 | task_event = (struct perf_task_event){ |
| 3777 | .task = task, |
| 3778 | .task_ctx = task_ctx, |
| 3779 | .event_id = { |
| 3780 | .header = { |
| 3781 | .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, |
| 3782 | .misc = 0, |
| 3783 | .size = sizeof(task_event.event_id), |
| 3784 | }, |
| 3785 | /* .pid */ |
| 3786 | /* .ppid */ |
| 3787 | /* .tid */ |
| 3788 | /* .ptid */ |
Peter Zijlstra | 6f93d0a | 2010-02-14 11:12:04 +0100 | [diff] [blame] | 3789 | .time = perf_clock(), |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3790 | }, |
| 3791 | }; |
| 3792 | |
| 3793 | perf_event_task_event(&task_event); |
| 3794 | } |
| 3795 | |
| 3796 | void perf_event_fork(struct task_struct *task) |
| 3797 | { |
| 3798 | perf_event_task(task, NULL, 1); |
| 3799 | } |
| 3800 | |
| 3801 | /* |
| 3802 | * comm tracking |
| 3803 | */ |
| 3804 | |
| 3805 | struct perf_comm_event { |
| 3806 | struct task_struct *task; |
| 3807 | char *comm; |
| 3808 | int comm_size; |
| 3809 | |
| 3810 | struct { |
| 3811 | struct perf_event_header header; |
| 3812 | |
| 3813 | u32 pid; |
| 3814 | u32 tid; |
| 3815 | } event_id; |
| 3816 | }; |
| 3817 | |
| 3818 | static void perf_event_comm_output(struct perf_event *event, |
| 3819 | struct perf_comm_event *comm_event) |
| 3820 | { |
| 3821 | struct perf_output_handle handle; |
| 3822 | int size = comm_event->event_id.header.size; |
| 3823 | int ret = perf_output_begin(&handle, event, size, 0, 0); |
| 3824 | |
| 3825 | if (ret) |
| 3826 | return; |
| 3827 | |
| 3828 | comm_event->event_id.pid = perf_event_pid(event, comm_event->task); |
| 3829 | comm_event->event_id.tid = perf_event_tid(event, comm_event->task); |
| 3830 | |
| 3831 | perf_output_put(&handle, comm_event->event_id); |
| 3832 | perf_output_copy(&handle, comm_event->comm, |
| 3833 | comm_event->comm_size); |
| 3834 | perf_output_end(&handle); |
| 3835 | } |
| 3836 | |
| 3837 | static int perf_event_comm_match(struct perf_event *event) |
| 3838 | { |
Peter Zijlstra | 6f93d0a | 2010-02-14 11:12:04 +0100 | [diff] [blame] | 3839 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
Peter Zijlstra | 22e1908 | 2010-01-18 09:12:32 +0100 | [diff] [blame] | 3840 | return 0; |
| 3841 | |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 3842 | if (event->cpu != -1 && event->cpu != smp_processor_id()) |
| 3843 | return 0; |
| 3844 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3845 | if (event->attr.comm) |
| 3846 | return 1; |
| 3847 | |
| 3848 | return 0; |
| 3849 | } |
| 3850 | |
| 3851 | static void perf_event_comm_ctx(struct perf_event_context *ctx, |
| 3852 | struct perf_comm_event *comm_event) |
| 3853 | { |
| 3854 | struct perf_event *event; |
| 3855 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3856 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
| 3857 | if (perf_event_comm_match(event)) |
| 3858 | perf_event_comm_output(event, comm_event); |
| 3859 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | static void perf_event_comm_event(struct perf_comm_event *comm_event) |
| 3863 | { |
| 3864 | struct perf_cpu_context *cpuctx; |
| 3865 | struct perf_event_context *ctx; |
| 3866 | unsigned int size; |
| 3867 | char comm[TASK_COMM_LEN]; |
| 3868 | |
| 3869 | memset(comm, 0, sizeof(comm)); |
Márton Németh | 96b02d7 | 2009-11-21 23:10:15 +0100 | [diff] [blame] | 3870 | strlcpy(comm, comm_event->task->comm, sizeof(comm)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3871 | size = ALIGN(strlen(comm)+1, sizeof(u64)); |
| 3872 | |
| 3873 | comm_event->comm = comm; |
| 3874 | comm_event->comm_size = size; |
| 3875 | |
| 3876 | comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; |
| 3877 | |
Peter Zijlstra | f6595f3 | 2009-11-20 22:19:47 +0100 | [diff] [blame] | 3878 | rcu_read_lock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3879 | cpuctx = &get_cpu_var(perf_cpu_context); |
| 3880 | perf_event_comm_ctx(&cpuctx->ctx, comm_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3881 | ctx = rcu_dereference(current->perf_event_ctxp); |
| 3882 | if (ctx) |
| 3883 | perf_event_comm_ctx(ctx, comm_event); |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 3884 | put_cpu_var(perf_cpu_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3885 | rcu_read_unlock(); |
| 3886 | } |
| 3887 | |
| 3888 | void perf_event_comm(struct task_struct *task) |
| 3889 | { |
| 3890 | struct perf_comm_event comm_event; |
| 3891 | |
| 3892 | if (task->perf_event_ctxp) |
| 3893 | perf_event_enable_on_exec(task); |
| 3894 | |
| 3895 | if (!atomic_read(&nr_comm_events)) |
| 3896 | return; |
| 3897 | |
| 3898 | comm_event = (struct perf_comm_event){ |
| 3899 | .task = task, |
| 3900 | /* .comm */ |
| 3901 | /* .comm_size */ |
| 3902 | .event_id = { |
| 3903 | .header = { |
| 3904 | .type = PERF_RECORD_COMM, |
| 3905 | .misc = 0, |
| 3906 | /* .size */ |
| 3907 | }, |
| 3908 | /* .pid */ |
| 3909 | /* .tid */ |
| 3910 | }, |
| 3911 | }; |
| 3912 | |
| 3913 | perf_event_comm_event(&comm_event); |
| 3914 | } |
| 3915 | |
| 3916 | /* |
| 3917 | * mmap tracking |
| 3918 | */ |
| 3919 | |
| 3920 | struct perf_mmap_event { |
| 3921 | struct vm_area_struct *vma; |
| 3922 | |
| 3923 | const char *file_name; |
| 3924 | int file_size; |
| 3925 | |
| 3926 | struct { |
| 3927 | struct perf_event_header header; |
| 3928 | |
| 3929 | u32 pid; |
| 3930 | u32 tid; |
| 3931 | u64 start; |
| 3932 | u64 len; |
| 3933 | u64 pgoff; |
| 3934 | } event_id; |
| 3935 | }; |
| 3936 | |
| 3937 | static void perf_event_mmap_output(struct perf_event *event, |
| 3938 | struct perf_mmap_event *mmap_event) |
| 3939 | { |
| 3940 | struct perf_output_handle handle; |
| 3941 | int size = mmap_event->event_id.header.size; |
| 3942 | int ret = perf_output_begin(&handle, event, size, 0, 0); |
| 3943 | |
| 3944 | if (ret) |
| 3945 | return; |
| 3946 | |
| 3947 | mmap_event->event_id.pid = perf_event_pid(event, current); |
| 3948 | mmap_event->event_id.tid = perf_event_tid(event, current); |
| 3949 | |
| 3950 | perf_output_put(&handle, mmap_event->event_id); |
| 3951 | perf_output_copy(&handle, mmap_event->file_name, |
| 3952 | mmap_event->file_size); |
| 3953 | perf_output_end(&handle); |
| 3954 | } |
| 3955 | |
| 3956 | static int perf_event_mmap_match(struct perf_event *event, |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 3957 | struct perf_mmap_event *mmap_event, |
| 3958 | int executable) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3959 | { |
Peter Zijlstra | 6f93d0a | 2010-02-14 11:12:04 +0100 | [diff] [blame] | 3960 | if (event->state < PERF_EVENT_STATE_INACTIVE) |
Peter Zijlstra | 22e1908 | 2010-01-18 09:12:32 +0100 | [diff] [blame] | 3961 | return 0; |
| 3962 | |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 3963 | if (event->cpu != -1 && event->cpu != smp_processor_id()) |
| 3964 | return 0; |
| 3965 | |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 3966 | if ((!executable && event->attr.mmap_data) || |
| 3967 | (executable && event->attr.mmap)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3968 | return 1; |
| 3969 | |
| 3970 | return 0; |
| 3971 | } |
| 3972 | |
| 3973 | static void perf_event_mmap_ctx(struct perf_event_context *ctx, |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 3974 | struct perf_mmap_event *mmap_event, |
| 3975 | int executable) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3976 | { |
| 3977 | struct perf_event *event; |
| 3978 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3979 | list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 3980 | if (perf_event_mmap_match(event, mmap_event, executable)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3981 | perf_event_mmap_output(event, mmap_event); |
| 3982 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 3983 | } |
| 3984 | |
| 3985 | static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) |
| 3986 | { |
| 3987 | struct perf_cpu_context *cpuctx; |
| 3988 | struct perf_event_context *ctx; |
| 3989 | struct vm_area_struct *vma = mmap_event->vma; |
| 3990 | struct file *file = vma->vm_file; |
| 3991 | unsigned int size; |
| 3992 | char tmp[16]; |
| 3993 | char *buf = NULL; |
| 3994 | const char *name; |
| 3995 | |
| 3996 | memset(tmp, 0, sizeof(tmp)); |
| 3997 | |
| 3998 | if (file) { |
| 3999 | /* |
| 4000 | * d_path works from the end of the buffer backwards, so we |
| 4001 | * need to add enough zero bytes after the string to handle |
| 4002 | * the 64bit alignment we do later. |
| 4003 | */ |
| 4004 | buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL); |
| 4005 | if (!buf) { |
| 4006 | name = strncpy(tmp, "//enomem", sizeof(tmp)); |
| 4007 | goto got_name; |
| 4008 | } |
| 4009 | name = d_path(&file->f_path, buf, PATH_MAX); |
| 4010 | if (IS_ERR(name)) { |
| 4011 | name = strncpy(tmp, "//toolong", sizeof(tmp)); |
| 4012 | goto got_name; |
| 4013 | } |
| 4014 | } else { |
| 4015 | if (arch_vma_name(mmap_event->vma)) { |
| 4016 | name = strncpy(tmp, arch_vma_name(mmap_event->vma), |
| 4017 | sizeof(tmp)); |
| 4018 | goto got_name; |
| 4019 | } |
| 4020 | |
| 4021 | if (!vma->vm_mm) { |
| 4022 | name = strncpy(tmp, "[vdso]", sizeof(tmp)); |
| 4023 | goto got_name; |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 4024 | } else if (vma->vm_start <= vma->vm_mm->start_brk && |
| 4025 | vma->vm_end >= vma->vm_mm->brk) { |
| 4026 | name = strncpy(tmp, "[heap]", sizeof(tmp)); |
| 4027 | goto got_name; |
| 4028 | } else if (vma->vm_start <= vma->vm_mm->start_stack && |
| 4029 | vma->vm_end >= vma->vm_mm->start_stack) { |
| 4030 | name = strncpy(tmp, "[stack]", sizeof(tmp)); |
| 4031 | goto got_name; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4032 | } |
| 4033 | |
| 4034 | name = strncpy(tmp, "//anon", sizeof(tmp)); |
| 4035 | goto got_name; |
| 4036 | } |
| 4037 | |
| 4038 | got_name: |
| 4039 | size = ALIGN(strlen(name)+1, sizeof(u64)); |
| 4040 | |
| 4041 | mmap_event->file_name = name; |
| 4042 | mmap_event->file_size = size; |
| 4043 | |
| 4044 | mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; |
| 4045 | |
Peter Zijlstra | f6d9dd2 | 2009-11-20 22:19:48 +0100 | [diff] [blame] | 4046 | rcu_read_lock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4047 | cpuctx = &get_cpu_var(perf_cpu_context); |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 4048 | perf_event_mmap_ctx(&cpuctx->ctx, mmap_event, vma->vm_flags & VM_EXEC); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4049 | ctx = rcu_dereference(current->perf_event_ctxp); |
| 4050 | if (ctx) |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 4051 | perf_event_mmap_ctx(ctx, mmap_event, vma->vm_flags & VM_EXEC); |
Peter Zijlstra | 5d27c23 | 2009-12-17 13:16:32 +0100 | [diff] [blame] | 4052 | put_cpu_var(perf_cpu_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4053 | rcu_read_unlock(); |
| 4054 | |
| 4055 | kfree(buf); |
| 4056 | } |
| 4057 | |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 4058 | void perf_event_mmap(struct vm_area_struct *vma) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4059 | { |
| 4060 | struct perf_mmap_event mmap_event; |
| 4061 | |
| 4062 | if (!atomic_read(&nr_mmap_events)) |
| 4063 | return; |
| 4064 | |
| 4065 | mmap_event = (struct perf_mmap_event){ |
| 4066 | .vma = vma, |
| 4067 | /* .file_name */ |
| 4068 | /* .file_size */ |
| 4069 | .event_id = { |
| 4070 | .header = { |
| 4071 | .type = PERF_RECORD_MMAP, |
Zhang, Yanmin | 39447b3 | 2010-04-19 13:32:41 +0800 | [diff] [blame] | 4072 | .misc = PERF_RECORD_MISC_USER, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4073 | /* .size */ |
| 4074 | }, |
| 4075 | /* .pid */ |
| 4076 | /* .tid */ |
| 4077 | .start = vma->vm_start, |
| 4078 | .len = vma->vm_end - vma->vm_start, |
Peter Zijlstra | 3a0304e | 2010-02-26 10:33:41 +0100 | [diff] [blame] | 4079 | .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4080 | }, |
| 4081 | }; |
| 4082 | |
| 4083 | perf_event_mmap_event(&mmap_event); |
| 4084 | } |
| 4085 | |
| 4086 | /* |
| 4087 | * IRQ throttle logging |
| 4088 | */ |
| 4089 | |
| 4090 | static void perf_log_throttle(struct perf_event *event, int enable) |
| 4091 | { |
| 4092 | struct perf_output_handle handle; |
| 4093 | int ret; |
| 4094 | |
| 4095 | struct { |
| 4096 | struct perf_event_header header; |
| 4097 | u64 time; |
| 4098 | u64 id; |
| 4099 | u64 stream_id; |
| 4100 | } throttle_event = { |
| 4101 | .header = { |
| 4102 | .type = PERF_RECORD_THROTTLE, |
| 4103 | .misc = 0, |
| 4104 | .size = sizeof(throttle_event), |
| 4105 | }, |
| 4106 | .time = perf_clock(), |
| 4107 | .id = primary_event_id(event), |
| 4108 | .stream_id = event->id, |
| 4109 | }; |
| 4110 | |
| 4111 | if (enable) |
| 4112 | throttle_event.header.type = PERF_RECORD_UNTHROTTLE; |
| 4113 | |
| 4114 | ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0); |
| 4115 | if (ret) |
| 4116 | return; |
| 4117 | |
| 4118 | perf_output_put(&handle, throttle_event); |
| 4119 | perf_output_end(&handle); |
| 4120 | } |
| 4121 | |
| 4122 | /* |
| 4123 | * Generic event overflow handling, sampling. |
| 4124 | */ |
| 4125 | |
| 4126 | static int __perf_event_overflow(struct perf_event *event, int nmi, |
| 4127 | int throttle, struct perf_sample_data *data, |
| 4128 | struct pt_regs *regs) |
| 4129 | { |
| 4130 | int events = atomic_read(&event->event_limit); |
| 4131 | struct hw_perf_event *hwc = &event->hw; |
| 4132 | int ret = 0; |
| 4133 | |
| 4134 | throttle = (throttle && event->pmu->unthrottle != NULL); |
| 4135 | |
| 4136 | if (!throttle) { |
| 4137 | hwc->interrupts++; |
| 4138 | } else { |
| 4139 | if (hwc->interrupts != MAX_INTERRUPTS) { |
| 4140 | hwc->interrupts++; |
| 4141 | if (HZ * hwc->interrupts > |
| 4142 | (u64)sysctl_perf_event_sample_rate) { |
| 4143 | hwc->interrupts = MAX_INTERRUPTS; |
| 4144 | perf_log_throttle(event, 0); |
| 4145 | ret = 1; |
| 4146 | } |
| 4147 | } else { |
| 4148 | /* |
| 4149 | * Keep re-disabling events even though on the previous |
| 4150 | * pass we disabled it - just in case we raced with a |
| 4151 | * sched-in and the event got enabled again: |
| 4152 | */ |
| 4153 | ret = 1; |
| 4154 | } |
| 4155 | } |
| 4156 | |
| 4157 | if (event->attr.freq) { |
| 4158 | u64 now = perf_clock(); |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 4159 | s64 delta = now - hwc->freq_time_stamp; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4160 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 4161 | hwc->freq_time_stamp = now; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4162 | |
Peter Zijlstra | abd5071 | 2010-01-26 18:50:16 +0100 | [diff] [blame] | 4163 | if (delta > 0 && delta < 2*TICK_NSEC) |
| 4164 | perf_adjust_period(event, delta, hwc->last_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4165 | } |
| 4166 | |
| 4167 | /* |
| 4168 | * XXX event_limit might not quite work as expected on inherited |
| 4169 | * events |
| 4170 | */ |
| 4171 | |
| 4172 | event->pending_kill = POLL_IN; |
| 4173 | if (events && atomic_dec_and_test(&event->event_limit)) { |
| 4174 | ret = 1; |
| 4175 | event->pending_kill = POLL_HUP; |
| 4176 | if (nmi) { |
| 4177 | event->pending_disable = 1; |
| 4178 | perf_pending_queue(&event->pending, |
| 4179 | perf_pending_event); |
| 4180 | } else |
| 4181 | perf_event_disable(event); |
| 4182 | } |
| 4183 | |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 4184 | if (event->overflow_handler) |
| 4185 | event->overflow_handler(event, nmi, data, regs); |
| 4186 | else |
| 4187 | perf_event_output(event, nmi, data, regs); |
| 4188 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4189 | return ret; |
| 4190 | } |
| 4191 | |
| 4192 | int perf_event_overflow(struct perf_event *event, int nmi, |
| 4193 | struct perf_sample_data *data, |
| 4194 | struct pt_regs *regs) |
| 4195 | { |
| 4196 | return __perf_event_overflow(event, nmi, 1, data, regs); |
| 4197 | } |
| 4198 | |
| 4199 | /* |
| 4200 | * Generic software event infrastructure |
| 4201 | */ |
| 4202 | |
| 4203 | /* |
| 4204 | * We directly increment event->count and keep a second value in |
| 4205 | * event->hw.period_left to count intervals. This period event |
| 4206 | * is kept in the range [-sample_period, 0] so that we can use the |
| 4207 | * sign as trigger. |
| 4208 | */ |
| 4209 | |
| 4210 | static u64 perf_swevent_set_period(struct perf_event *event) |
| 4211 | { |
| 4212 | struct hw_perf_event *hwc = &event->hw; |
| 4213 | u64 period = hwc->last_period; |
| 4214 | u64 nr, offset; |
| 4215 | s64 old, val; |
| 4216 | |
| 4217 | hwc->last_period = hwc->sample_period; |
| 4218 | |
| 4219 | again: |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 4220 | old = val = local64_read(&hwc->period_left); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4221 | if (val < 0) |
| 4222 | return 0; |
| 4223 | |
| 4224 | nr = div64_u64(period + val, period); |
| 4225 | offset = nr * period; |
| 4226 | val -= offset; |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 4227 | if (local64_cmpxchg(&hwc->period_left, old, val) != old) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4228 | goto again; |
| 4229 | |
| 4230 | return nr; |
| 4231 | } |
| 4232 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 4233 | static void perf_swevent_overflow(struct perf_event *event, u64 overflow, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4234 | int nmi, struct perf_sample_data *data, |
| 4235 | struct pt_regs *regs) |
| 4236 | { |
| 4237 | struct hw_perf_event *hwc = &event->hw; |
| 4238 | int throttle = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4239 | |
| 4240 | data->period = event->hw.last_period; |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 4241 | if (!overflow) |
| 4242 | overflow = perf_swevent_set_period(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4243 | |
| 4244 | if (hwc->interrupts == MAX_INTERRUPTS) |
| 4245 | return; |
| 4246 | |
| 4247 | for (; overflow; overflow--) { |
| 4248 | if (__perf_event_overflow(event, nmi, throttle, |
| 4249 | data, regs)) { |
| 4250 | /* |
| 4251 | * We inhibit the overflow from happening when |
| 4252 | * hwc->interrupts == MAX_INTERRUPTS. |
| 4253 | */ |
| 4254 | break; |
| 4255 | } |
| 4256 | throttle = 1; |
| 4257 | } |
| 4258 | } |
| 4259 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4260 | static void perf_swevent_add(struct perf_event *event, u64 nr, |
| 4261 | int nmi, struct perf_sample_data *data, |
| 4262 | struct pt_regs *regs) |
| 4263 | { |
| 4264 | struct hw_perf_event *hwc = &event->hw; |
| 4265 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 4266 | local64_add(nr, &event->count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4267 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4268 | if (!regs) |
| 4269 | return; |
| 4270 | |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 4271 | if (!hwc->sample_period) |
| 4272 | return; |
| 4273 | |
| 4274 | if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) |
| 4275 | return perf_swevent_overflow(event, 1, nmi, data, regs); |
| 4276 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 4277 | if (local64_add_negative(nr, &hwc->period_left)) |
Peter Zijlstra | 0cff784 | 2009-11-20 22:19:44 +0100 | [diff] [blame] | 4278 | return; |
| 4279 | |
| 4280 | perf_swevent_overflow(event, 0, nmi, data, regs); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4281 | } |
| 4282 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 4283 | static int perf_exclude_event(struct perf_event *event, |
| 4284 | struct pt_regs *regs) |
| 4285 | { |
| 4286 | if (regs) { |
| 4287 | if (event->attr.exclude_user && user_mode(regs)) |
| 4288 | return 1; |
| 4289 | |
| 4290 | if (event->attr.exclude_kernel && !user_mode(regs)) |
| 4291 | return 1; |
| 4292 | } |
| 4293 | |
| 4294 | return 0; |
| 4295 | } |
| 4296 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4297 | static int perf_swevent_match(struct perf_event *event, |
| 4298 | enum perf_type_id type, |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 4299 | u32 event_id, |
| 4300 | struct perf_sample_data *data, |
| 4301 | struct pt_regs *regs) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4302 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4303 | if (event->attr.type != type) |
| 4304 | return 0; |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 4305 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4306 | if (event->attr.config != event_id) |
| 4307 | return 0; |
| 4308 | |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 4309 | if (perf_exclude_event(event, regs)) |
| 4310 | return 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4311 | |
| 4312 | return 1; |
| 4313 | } |
| 4314 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4315 | static inline u64 swevent_hash(u64 type, u32 event_id) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4316 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4317 | u64 val = event_id | (type << 32); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4318 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4319 | return hash_64(val, SWEVENT_HLIST_BITS); |
| 4320 | } |
| 4321 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 4322 | static inline struct hlist_head * |
| 4323 | __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4324 | { |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 4325 | u64 hash = swevent_hash(type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4326 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 4327 | return &hlist->heads[hash]; |
| 4328 | } |
| 4329 | |
| 4330 | /* For the read side: events when they trigger */ |
| 4331 | static inline struct hlist_head * |
| 4332 | find_swevent_head_rcu(struct perf_cpu_context *ctx, u64 type, u32 event_id) |
| 4333 | { |
| 4334 | struct swevent_hlist *hlist; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4335 | |
| 4336 | hlist = rcu_dereference(ctx->swevent_hlist); |
| 4337 | if (!hlist) |
| 4338 | return NULL; |
| 4339 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 4340 | return __find_swevent_head(hlist, type, event_id); |
| 4341 | } |
| 4342 | |
| 4343 | /* For the event head insertion and removal in the hlist */ |
| 4344 | static inline struct hlist_head * |
| 4345 | find_swevent_head(struct perf_cpu_context *ctx, struct perf_event *event) |
| 4346 | { |
| 4347 | struct swevent_hlist *hlist; |
| 4348 | u32 event_id = event->attr.config; |
| 4349 | u64 type = event->attr.type; |
| 4350 | |
| 4351 | /* |
| 4352 | * Event scheduling is always serialized against hlist allocation |
| 4353 | * and release. Which makes the protected version suitable here. |
| 4354 | * The context lock guarantees that. |
| 4355 | */ |
| 4356 | hlist = rcu_dereference_protected(ctx->swevent_hlist, |
| 4357 | lockdep_is_held(&event->ctx->lock)); |
| 4358 | if (!hlist) |
| 4359 | return NULL; |
| 4360 | |
| 4361 | return __find_swevent_head(hlist, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4362 | } |
| 4363 | |
| 4364 | static void do_perf_sw_event(enum perf_type_id type, u32 event_id, |
| 4365 | u64 nr, int nmi, |
| 4366 | struct perf_sample_data *data, |
| 4367 | struct pt_regs *regs) |
| 4368 | { |
| 4369 | struct perf_cpu_context *cpuctx; |
| 4370 | struct perf_event *event; |
| 4371 | struct hlist_node *node; |
| 4372 | struct hlist_head *head; |
| 4373 | |
| 4374 | cpuctx = &__get_cpu_var(perf_cpu_context); |
| 4375 | |
| 4376 | rcu_read_lock(); |
| 4377 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 4378 | head = find_swevent_head_rcu(cpuctx, type, event_id); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4379 | |
| 4380 | if (!head) |
| 4381 | goto end; |
| 4382 | |
| 4383 | hlist_for_each_entry_rcu(event, node, head, hlist_entry) { |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 4384 | if (perf_swevent_match(event, type, event_id, data, regs)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4385 | perf_swevent_add(event, nr, nmi, data, regs); |
| 4386 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4387 | end: |
| 4388 | rcu_read_unlock(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4389 | } |
| 4390 | |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 4391 | int perf_swevent_get_recursion_context(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4392 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 4393 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 4394 | |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 4395 | return get_recursion_context(cpuctx->recursion); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4396 | } |
Ingo Molnar | 645e8cc | 2009-11-22 12:20:19 +0100 | [diff] [blame] | 4397 | EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4398 | |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 4399 | void inline perf_swevent_put_recursion_context(int rctx) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4400 | { |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 4401 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 4402 | |
| 4403 | put_recursion_context(cpuctx->recursion, rctx); |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 4404 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4405 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4406 | void __perf_sw_event(u32 event_id, u64 nr, int nmi, |
| 4407 | struct pt_regs *regs, u64 addr) |
| 4408 | { |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 4409 | struct perf_sample_data data; |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 4410 | int rctx; |
| 4411 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 4412 | preempt_disable_notrace(); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 4413 | rctx = perf_swevent_get_recursion_context(); |
| 4414 | if (rctx < 0) |
| 4415 | return; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4416 | |
Peter Zijlstra | dc1d628 | 2010-03-03 15:55:04 +0100 | [diff] [blame] | 4417 | perf_sample_data_init(&data, addr); |
Ingo Molnar | a4234bf | 2009-11-23 10:57:59 +0100 | [diff] [blame] | 4418 | |
| 4419 | do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs); |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 4420 | |
| 4421 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 4422 | preempt_enable_notrace(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4423 | } |
| 4424 | |
| 4425 | static void perf_swevent_read(struct perf_event *event) |
| 4426 | { |
| 4427 | } |
| 4428 | |
| 4429 | static int perf_swevent_enable(struct perf_event *event) |
| 4430 | { |
| 4431 | struct hw_perf_event *hwc = &event->hw; |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4432 | struct perf_cpu_context *cpuctx; |
| 4433 | struct hlist_head *head; |
| 4434 | |
| 4435 | cpuctx = &__get_cpu_var(perf_cpu_context); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4436 | |
| 4437 | if (hwc->sample_period) { |
| 4438 | hwc->last_period = hwc->sample_period; |
| 4439 | perf_swevent_set_period(event); |
| 4440 | } |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4441 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 4442 | head = find_swevent_head(cpuctx, event); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4443 | if (WARN_ON_ONCE(!head)) |
| 4444 | return -EINVAL; |
| 4445 | |
| 4446 | hlist_add_head_rcu(&event->hlist_entry, head); |
| 4447 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4448 | return 0; |
| 4449 | } |
| 4450 | |
| 4451 | static void perf_swevent_disable(struct perf_event *event) |
| 4452 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4453 | hlist_del_rcu(&event->hlist_entry); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4454 | } |
| 4455 | |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 4456 | static void perf_swevent_void(struct perf_event *event) |
| 4457 | { |
| 4458 | } |
| 4459 | |
| 4460 | static int perf_swevent_int(struct perf_event *event) |
| 4461 | { |
| 4462 | return 0; |
| 4463 | } |
| 4464 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 4465 | /* Deref the hlist from the update side */ |
| 4466 | static inline struct swevent_hlist * |
| 4467 | swevent_hlist_deref(struct perf_cpu_context *cpuctx) |
| 4468 | { |
| 4469 | return rcu_dereference_protected(cpuctx->swevent_hlist, |
| 4470 | lockdep_is_held(&cpuctx->hlist_mutex)); |
| 4471 | } |
| 4472 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4473 | static void swevent_hlist_release_rcu(struct rcu_head *rcu_head) |
| 4474 | { |
| 4475 | struct swevent_hlist *hlist; |
| 4476 | |
| 4477 | hlist = container_of(rcu_head, struct swevent_hlist, rcu_head); |
| 4478 | kfree(hlist); |
| 4479 | } |
| 4480 | |
| 4481 | static void swevent_hlist_release(struct perf_cpu_context *cpuctx) |
| 4482 | { |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 4483 | struct swevent_hlist *hlist = swevent_hlist_deref(cpuctx); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4484 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 4485 | if (!hlist) |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4486 | return; |
| 4487 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4488 | rcu_assign_pointer(cpuctx->swevent_hlist, NULL); |
| 4489 | call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu); |
| 4490 | } |
| 4491 | |
| 4492 | static void swevent_hlist_put_cpu(struct perf_event *event, int cpu) |
| 4493 | { |
| 4494 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 4495 | |
| 4496 | mutex_lock(&cpuctx->hlist_mutex); |
| 4497 | |
| 4498 | if (!--cpuctx->hlist_refcount) |
| 4499 | swevent_hlist_release(cpuctx); |
| 4500 | |
| 4501 | mutex_unlock(&cpuctx->hlist_mutex); |
| 4502 | } |
| 4503 | |
| 4504 | static void swevent_hlist_put(struct perf_event *event) |
| 4505 | { |
| 4506 | int cpu; |
| 4507 | |
| 4508 | if (event->cpu != -1) { |
| 4509 | swevent_hlist_put_cpu(event, event->cpu); |
| 4510 | return; |
| 4511 | } |
| 4512 | |
| 4513 | for_each_possible_cpu(cpu) |
| 4514 | swevent_hlist_put_cpu(event, cpu); |
| 4515 | } |
| 4516 | |
| 4517 | static int swevent_hlist_get_cpu(struct perf_event *event, int cpu) |
| 4518 | { |
| 4519 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 4520 | int err = 0; |
| 4521 | |
| 4522 | mutex_lock(&cpuctx->hlist_mutex); |
| 4523 | |
Frederic Weisbecker | 49f135e | 2010-05-20 10:17:46 +0200 | [diff] [blame] | 4524 | if (!swevent_hlist_deref(cpuctx) && cpu_online(cpu)) { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4525 | struct swevent_hlist *hlist; |
| 4526 | |
| 4527 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); |
| 4528 | if (!hlist) { |
| 4529 | err = -ENOMEM; |
| 4530 | goto exit; |
| 4531 | } |
| 4532 | rcu_assign_pointer(cpuctx->swevent_hlist, hlist); |
| 4533 | } |
| 4534 | cpuctx->hlist_refcount++; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 4535 | exit: |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4536 | mutex_unlock(&cpuctx->hlist_mutex); |
| 4537 | |
| 4538 | return err; |
| 4539 | } |
| 4540 | |
| 4541 | static int swevent_hlist_get(struct perf_event *event) |
| 4542 | { |
| 4543 | int err; |
| 4544 | int cpu, failed_cpu; |
| 4545 | |
| 4546 | if (event->cpu != -1) |
| 4547 | return swevent_hlist_get_cpu(event, event->cpu); |
| 4548 | |
| 4549 | get_online_cpus(); |
| 4550 | for_each_possible_cpu(cpu) { |
| 4551 | err = swevent_hlist_get_cpu(event, cpu); |
| 4552 | if (err) { |
| 4553 | failed_cpu = cpu; |
| 4554 | goto fail; |
| 4555 | } |
| 4556 | } |
| 4557 | put_online_cpus(); |
| 4558 | |
| 4559 | return 0; |
Peter Zijlstra | 9ed6060 | 2010-06-11 17:36:35 +0200 | [diff] [blame] | 4560 | fail: |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4561 | for_each_possible_cpu(cpu) { |
| 4562 | if (cpu == failed_cpu) |
| 4563 | break; |
| 4564 | swevent_hlist_put_cpu(event, cpu); |
| 4565 | } |
| 4566 | |
| 4567 | put_online_cpus(); |
| 4568 | return err; |
| 4569 | } |
| 4570 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4571 | atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX]; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 4572 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4573 | static void sw_perf_event_destroy(struct perf_event *event) |
| 4574 | { |
| 4575 | u64 event_id = event->attr.config; |
| 4576 | |
| 4577 | WARN_ON(event->parent); |
| 4578 | |
| 4579 | atomic_dec(&perf_swevent_enabled[event_id]); |
| 4580 | swevent_hlist_put(event); |
| 4581 | } |
| 4582 | |
| 4583 | static int perf_swevent_init(struct perf_event *event) |
| 4584 | { |
| 4585 | int event_id = event->attr.config; |
| 4586 | |
| 4587 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 4588 | return -ENOENT; |
| 4589 | |
| 4590 | switch (event_id) { |
| 4591 | case PERF_COUNT_SW_CPU_CLOCK: |
| 4592 | case PERF_COUNT_SW_TASK_CLOCK: |
| 4593 | return -ENOENT; |
| 4594 | |
| 4595 | default: |
| 4596 | break; |
| 4597 | } |
| 4598 | |
| 4599 | if (event_id > PERF_COUNT_SW_MAX) |
| 4600 | return -ENOENT; |
| 4601 | |
| 4602 | if (!event->parent) { |
| 4603 | int err; |
| 4604 | |
| 4605 | err = swevent_hlist_get(event); |
| 4606 | if (err) |
| 4607 | return err; |
| 4608 | |
| 4609 | atomic_inc(&perf_swevent_enabled[event_id]); |
| 4610 | event->destroy = sw_perf_event_destroy; |
| 4611 | } |
| 4612 | |
| 4613 | return 0; |
| 4614 | } |
| 4615 | |
| 4616 | static struct pmu perf_swevent = { |
| 4617 | .event_init = perf_swevent_init, |
| 4618 | .enable = perf_swevent_enable, |
| 4619 | .disable = perf_swevent_disable, |
Peter Zijlstra | c6df8d5 | 2010-06-03 11:21:20 +0200 | [diff] [blame] | 4620 | .start = perf_swevent_int, |
| 4621 | .stop = perf_swevent_void, |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 4622 | .read = perf_swevent_read, |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4623 | .unthrottle = perf_swevent_void, /* hwc->interrupts already reset */ |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 4624 | }; |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 4625 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4626 | #ifdef CONFIG_EVENT_TRACING |
| 4627 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 4628 | static int perf_tp_filter_match(struct perf_event *event, |
Frederic Weisbecker | 95476b6 | 2010-04-14 23:42:18 +0200 | [diff] [blame] | 4629 | struct perf_sample_data *data) |
| 4630 | { |
| 4631 | void *record = data->raw->data; |
| 4632 | |
| 4633 | if (likely(!event->filter) || filter_match_preds(event->filter, record)) |
| 4634 | return 1; |
| 4635 | return 0; |
| 4636 | } |
| 4637 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 4638 | static int perf_tp_event_match(struct perf_event *event, |
| 4639 | struct perf_sample_data *data, |
| 4640 | struct pt_regs *regs) |
| 4641 | { |
Peter Zijlstra | 580d607 | 2010-05-20 20:54:31 +0200 | [diff] [blame] | 4642 | /* |
| 4643 | * All tracepoints are from kernel-space. |
| 4644 | */ |
| 4645 | if (event->attr.exclude_kernel) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 4646 | return 0; |
| 4647 | |
| 4648 | if (!perf_tp_filter_match(event, data)) |
| 4649 | return 0; |
| 4650 | |
| 4651 | return 1; |
| 4652 | } |
| 4653 | |
| 4654 | void perf_tp_event(u64 addr, u64 count, void *record, int entry_size, |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 4655 | struct pt_regs *regs, struct hlist_head *head, int rctx) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 4656 | { |
| 4657 | struct perf_sample_data data; |
| 4658 | struct perf_event *event; |
| 4659 | struct hlist_node *node; |
| 4660 | |
| 4661 | struct perf_raw_record raw = { |
| 4662 | .size = entry_size, |
| 4663 | .data = record, |
| 4664 | }; |
| 4665 | |
| 4666 | perf_sample_data_init(&data, addr); |
| 4667 | data.raw = &raw; |
| 4668 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 4669 | hlist_for_each_entry_rcu(event, node, head, hlist_entry) { |
| 4670 | if (perf_tp_event_match(event, &data, regs)) |
| 4671 | perf_swevent_add(event, count, 1, &data, regs); |
| 4672 | } |
Peter Zijlstra | ecc55f8 | 2010-05-21 15:11:34 +0200 | [diff] [blame] | 4673 | |
| 4674 | perf_swevent_put_recursion_context(rctx); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 4675 | } |
| 4676 | EXPORT_SYMBOL_GPL(perf_tp_event); |
| 4677 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4678 | static void tp_perf_event_destroy(struct perf_event *event) |
| 4679 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 4680 | perf_trace_destroy(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4681 | } |
| 4682 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4683 | static int perf_tp_event_init(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4684 | { |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 4685 | int err; |
| 4686 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4687 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 4688 | return -ENOENT; |
| 4689 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4690 | /* |
| 4691 | * Raw tracepoint data is a severe data leak, only allow root to |
| 4692 | * have these. |
| 4693 | */ |
| 4694 | if ((event->attr.sample_type & PERF_SAMPLE_RAW) && |
| 4695 | perf_paranoid_tracepoint_raw() && |
| 4696 | !capable(CAP_SYS_ADMIN)) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4697 | return -EPERM; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4698 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 4699 | err = perf_trace_init(event); |
| 4700 | if (err) |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4701 | return err; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4702 | |
| 4703 | event->destroy = tp_perf_event_destroy; |
| 4704 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4705 | return 0; |
| 4706 | } |
| 4707 | |
| 4708 | static struct pmu perf_tracepoint = { |
| 4709 | .event_init = perf_tp_event_init, |
| 4710 | .enable = perf_trace_enable, |
| 4711 | .disable = perf_trace_disable, |
| 4712 | .start = perf_swevent_int, |
| 4713 | .stop = perf_swevent_void, |
| 4714 | .read = perf_swevent_read, |
| 4715 | .unthrottle = perf_swevent_void, |
| 4716 | }; |
| 4717 | |
| 4718 | static inline void perf_tp_register(void) |
| 4719 | { |
| 4720 | perf_pmu_register(&perf_tracepoint); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4721 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 4722 | |
| 4723 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 4724 | { |
| 4725 | char *filter_str; |
| 4726 | int ret; |
| 4727 | |
| 4728 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 4729 | return -EINVAL; |
| 4730 | |
| 4731 | filter_str = strndup_user(arg, PAGE_SIZE); |
| 4732 | if (IS_ERR(filter_str)) |
| 4733 | return PTR_ERR(filter_str); |
| 4734 | |
| 4735 | ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); |
| 4736 | |
| 4737 | kfree(filter_str); |
| 4738 | return ret; |
| 4739 | } |
| 4740 | |
| 4741 | static void perf_event_free_filter(struct perf_event *event) |
| 4742 | { |
| 4743 | ftrace_profile_free_filter(event); |
| 4744 | } |
| 4745 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4746 | #else |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 4747 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4748 | static inline void perf_tp_register(void) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4749 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4750 | } |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 4751 | |
| 4752 | static int perf_event_set_filter(struct perf_event *event, void __user *arg) |
| 4753 | { |
| 4754 | return -ENOENT; |
| 4755 | } |
| 4756 | |
| 4757 | static void perf_event_free_filter(struct perf_event *event) |
| 4758 | { |
| 4759 | } |
| 4760 | |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 4761 | #endif /* CONFIG_EVENT_TRACING */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4762 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 4763 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 4764 | void perf_bp_event(struct perf_event *bp, void *data) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 4765 | { |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 4766 | struct perf_sample_data sample; |
| 4767 | struct pt_regs *regs = data; |
| 4768 | |
Peter Zijlstra | dc1d628 | 2010-03-03 15:55:04 +0100 | [diff] [blame] | 4769 | perf_sample_data_init(&sample, bp->attr.bp_addr); |
Frederic Weisbecker | f5ffe02 | 2009-11-23 15:42:34 +0100 | [diff] [blame] | 4770 | |
| 4771 | if (!perf_exclude_event(bp, regs)) |
| 4772 | perf_swevent_add(bp, 1, 1, &sample, regs); |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 4773 | } |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 4774 | #endif |
| 4775 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4776 | /* |
| 4777 | * hrtimer based swevent callback |
| 4778 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4779 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4780 | static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4781 | { |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4782 | enum hrtimer_restart ret = HRTIMER_RESTART; |
| 4783 | struct perf_sample_data data; |
| 4784 | struct pt_regs *regs; |
| 4785 | struct perf_event *event; |
| 4786 | u64 period; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4787 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4788 | event = container_of(hrtimer, struct perf_event, hw.hrtimer); |
| 4789 | event->pmu->read(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4790 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4791 | perf_sample_data_init(&data, 0); |
| 4792 | data.period = event->hw.last_period; |
| 4793 | regs = get_irq_regs(); |
| 4794 | |
| 4795 | if (regs && !perf_exclude_event(event, regs)) { |
| 4796 | if (!(event->attr.exclude_idle && current->pid == 0)) |
| 4797 | if (perf_event_overflow(event, 0, &data, regs)) |
| 4798 | ret = HRTIMER_NORESTART; |
| 4799 | } |
| 4800 | |
| 4801 | period = max_t(u64, 10000, event->hw.sample_period); |
| 4802 | hrtimer_forward_now(hrtimer, ns_to_ktime(period)); |
| 4803 | |
| 4804 | return ret; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4805 | } |
| 4806 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4807 | static void perf_swevent_start_hrtimer(struct perf_event *event) |
| 4808 | { |
| 4809 | struct hw_perf_event *hwc = &event->hw; |
| 4810 | |
| 4811 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 4812 | hwc->hrtimer.function = perf_swevent_hrtimer; |
| 4813 | if (hwc->sample_period) { |
| 4814 | u64 period; |
| 4815 | |
| 4816 | if (hwc->remaining) { |
| 4817 | if (hwc->remaining < 0) |
| 4818 | period = 10000; |
| 4819 | else |
| 4820 | period = hwc->remaining; |
| 4821 | hwc->remaining = 0; |
| 4822 | } else { |
| 4823 | period = max_t(u64, 10000, hwc->sample_period); |
| 4824 | } |
| 4825 | __hrtimer_start_range_ns(&hwc->hrtimer, |
| 4826 | ns_to_ktime(period), 0, |
| 4827 | HRTIMER_MODE_REL, 0); |
| 4828 | } |
| 4829 | } |
| 4830 | |
| 4831 | static void perf_swevent_cancel_hrtimer(struct perf_event *event) |
| 4832 | { |
| 4833 | struct hw_perf_event *hwc = &event->hw; |
| 4834 | |
| 4835 | if (hwc->sample_period) { |
| 4836 | ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); |
| 4837 | hwc->remaining = ktime_to_ns(remaining); |
| 4838 | |
| 4839 | hrtimer_cancel(&hwc->hrtimer); |
| 4840 | } |
| 4841 | } |
| 4842 | |
| 4843 | /* |
| 4844 | * Software event: cpu wall time clock |
| 4845 | */ |
| 4846 | |
| 4847 | static void cpu_clock_event_update(struct perf_event *event) |
| 4848 | { |
| 4849 | int cpu = raw_smp_processor_id(); |
| 4850 | s64 prev; |
| 4851 | u64 now; |
| 4852 | |
| 4853 | now = cpu_clock(cpu); |
| 4854 | prev = local64_xchg(&event->hw.prev_count, now); |
| 4855 | local64_add(now - prev, &event->count); |
| 4856 | } |
| 4857 | |
| 4858 | static int cpu_clock_event_enable(struct perf_event *event) |
| 4859 | { |
| 4860 | struct hw_perf_event *hwc = &event->hw; |
| 4861 | int cpu = raw_smp_processor_id(); |
| 4862 | |
| 4863 | local64_set(&hwc->prev_count, cpu_clock(cpu)); |
| 4864 | perf_swevent_start_hrtimer(event); |
| 4865 | |
| 4866 | return 0; |
| 4867 | } |
| 4868 | |
| 4869 | static void cpu_clock_event_disable(struct perf_event *event) |
| 4870 | { |
| 4871 | perf_swevent_cancel_hrtimer(event); |
| 4872 | cpu_clock_event_update(event); |
| 4873 | } |
| 4874 | |
| 4875 | static void cpu_clock_event_read(struct perf_event *event) |
| 4876 | { |
| 4877 | cpu_clock_event_update(event); |
| 4878 | } |
| 4879 | |
| 4880 | static int cpu_clock_event_init(struct perf_event *event) |
| 4881 | { |
| 4882 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 4883 | return -ENOENT; |
| 4884 | |
| 4885 | if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) |
| 4886 | return -ENOENT; |
| 4887 | |
| 4888 | return 0; |
| 4889 | } |
| 4890 | |
| 4891 | static struct pmu perf_cpu_clock = { |
| 4892 | .event_init = cpu_clock_event_init, |
| 4893 | .enable = cpu_clock_event_enable, |
| 4894 | .disable = cpu_clock_event_disable, |
| 4895 | .read = cpu_clock_event_read, |
| 4896 | }; |
| 4897 | |
| 4898 | /* |
| 4899 | * Software event: task time clock |
| 4900 | */ |
| 4901 | |
| 4902 | static void task_clock_event_update(struct perf_event *event, u64 now) |
| 4903 | { |
| 4904 | u64 prev; |
| 4905 | s64 delta; |
| 4906 | |
| 4907 | prev = local64_xchg(&event->hw.prev_count, now); |
| 4908 | delta = now - prev; |
| 4909 | local64_add(delta, &event->count); |
| 4910 | } |
| 4911 | |
| 4912 | static int task_clock_event_enable(struct perf_event *event) |
| 4913 | { |
| 4914 | struct hw_perf_event *hwc = &event->hw; |
| 4915 | u64 now; |
| 4916 | |
| 4917 | now = event->ctx->time; |
| 4918 | |
| 4919 | local64_set(&hwc->prev_count, now); |
| 4920 | |
| 4921 | perf_swevent_start_hrtimer(event); |
| 4922 | |
| 4923 | return 0; |
| 4924 | } |
| 4925 | |
| 4926 | static void task_clock_event_disable(struct perf_event *event) |
| 4927 | { |
| 4928 | perf_swevent_cancel_hrtimer(event); |
| 4929 | task_clock_event_update(event, event->ctx->time); |
| 4930 | |
| 4931 | } |
| 4932 | |
| 4933 | static void task_clock_event_read(struct perf_event *event) |
| 4934 | { |
| 4935 | u64 time; |
| 4936 | |
| 4937 | if (!in_nmi()) { |
| 4938 | update_context_time(event->ctx); |
| 4939 | time = event->ctx->time; |
| 4940 | } else { |
| 4941 | u64 now = perf_clock(); |
| 4942 | u64 delta = now - event->ctx->timestamp; |
| 4943 | time = event->ctx->time + delta; |
| 4944 | } |
| 4945 | |
| 4946 | task_clock_event_update(event, time); |
| 4947 | } |
| 4948 | |
| 4949 | static int task_clock_event_init(struct perf_event *event) |
| 4950 | { |
| 4951 | if (event->attr.type != PERF_TYPE_SOFTWARE) |
| 4952 | return -ENOENT; |
| 4953 | |
| 4954 | if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) |
| 4955 | return -ENOENT; |
| 4956 | |
| 4957 | return 0; |
| 4958 | } |
| 4959 | |
| 4960 | static struct pmu perf_task_clock = { |
| 4961 | .event_init = task_clock_event_init, |
| 4962 | .enable = task_clock_event_enable, |
| 4963 | .disable = task_clock_event_disable, |
| 4964 | .read = task_clock_event_read, |
| 4965 | }; |
| 4966 | |
| 4967 | static LIST_HEAD(pmus); |
| 4968 | static DEFINE_MUTEX(pmus_lock); |
| 4969 | static struct srcu_struct pmus_srcu; |
| 4970 | |
| 4971 | int perf_pmu_register(struct pmu *pmu) |
| 4972 | { |
| 4973 | mutex_lock(&pmus_lock); |
| 4974 | list_add_rcu(&pmu->entry, &pmus); |
| 4975 | mutex_unlock(&pmus_lock); |
| 4976 | |
| 4977 | return 0; |
| 4978 | } |
| 4979 | |
| 4980 | void perf_pmu_unregister(struct pmu *pmu) |
| 4981 | { |
| 4982 | mutex_lock(&pmus_lock); |
| 4983 | list_del_rcu(&pmu->entry); |
| 4984 | mutex_unlock(&pmus_lock); |
| 4985 | |
| 4986 | synchronize_srcu(&pmus_srcu); |
| 4987 | } |
| 4988 | |
| 4989 | struct pmu *perf_init_event(struct perf_event *event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4990 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 4991 | struct pmu *pmu = NULL; |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4992 | int idx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 4993 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 4994 | idx = srcu_read_lock(&pmus_srcu); |
| 4995 | list_for_each_entry_rcu(pmu, &pmus, entry) { |
| 4996 | int ret = pmu->event_init(event); |
| 4997 | if (!ret) |
| 4998 | break; |
| 4999 | if (ret != -ENOENT) { |
| 5000 | pmu = ERR_PTR(ret); |
| 5001 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5002 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5003 | } |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5004 | srcu_read_unlock(&pmus_srcu, idx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5005 | |
| 5006 | return pmu; |
| 5007 | } |
| 5008 | |
| 5009 | /* |
| 5010 | * Allocate and initialize a event structure |
| 5011 | */ |
| 5012 | static struct perf_event * |
| 5013 | perf_event_alloc(struct perf_event_attr *attr, |
| 5014 | int cpu, |
| 5015 | struct perf_event_context *ctx, |
| 5016 | struct perf_event *group_leader, |
| 5017 | struct perf_event *parent_event, |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 5018 | perf_overflow_handler_t overflow_handler, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5019 | gfp_t gfpflags) |
| 5020 | { |
Peter Zijlstra | 51b0fe3 | 2010-06-11 13:35:57 +0200 | [diff] [blame] | 5021 | struct pmu *pmu; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5022 | struct perf_event *event; |
| 5023 | struct hw_perf_event *hwc; |
| 5024 | long err; |
| 5025 | |
| 5026 | event = kzalloc(sizeof(*event), gfpflags); |
| 5027 | if (!event) |
| 5028 | return ERR_PTR(-ENOMEM); |
| 5029 | |
| 5030 | /* |
| 5031 | * Single events are their own group leaders, with an |
| 5032 | * empty sibling list: |
| 5033 | */ |
| 5034 | if (!group_leader) |
| 5035 | group_leader = event; |
| 5036 | |
| 5037 | mutex_init(&event->child_mutex); |
| 5038 | INIT_LIST_HEAD(&event->child_list); |
| 5039 | |
| 5040 | INIT_LIST_HEAD(&event->group_entry); |
| 5041 | INIT_LIST_HEAD(&event->event_entry); |
| 5042 | INIT_LIST_HEAD(&event->sibling_list); |
| 5043 | init_waitqueue_head(&event->waitq); |
| 5044 | |
| 5045 | mutex_init(&event->mmap_mutex); |
| 5046 | |
| 5047 | event->cpu = cpu; |
| 5048 | event->attr = *attr; |
| 5049 | event->group_leader = group_leader; |
| 5050 | event->pmu = NULL; |
| 5051 | event->ctx = ctx; |
| 5052 | event->oncpu = -1; |
| 5053 | |
| 5054 | event->parent = parent_event; |
| 5055 | |
| 5056 | event->ns = get_pid_ns(current->nsproxy->pid_ns); |
| 5057 | event->id = atomic64_inc_return(&perf_event_id); |
| 5058 | |
| 5059 | event->state = PERF_EVENT_STATE_INACTIVE; |
| 5060 | |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 5061 | if (!overflow_handler && parent_event) |
| 5062 | overflow_handler = parent_event->overflow_handler; |
Frederic Weisbecker | 97eaf53 | 2009-10-18 15:33:50 +0200 | [diff] [blame] | 5063 | |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 5064 | event->overflow_handler = overflow_handler; |
Frederic Weisbecker | 97eaf53 | 2009-10-18 15:33:50 +0200 | [diff] [blame] | 5065 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5066 | if (attr->disabled) |
| 5067 | event->state = PERF_EVENT_STATE_OFF; |
| 5068 | |
| 5069 | pmu = NULL; |
| 5070 | |
| 5071 | hwc = &event->hw; |
| 5072 | hwc->sample_period = attr->sample_period; |
| 5073 | if (attr->freq && attr->sample_freq) |
| 5074 | hwc->sample_period = 1; |
| 5075 | hwc->last_period = hwc->sample_period; |
| 5076 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5077 | local64_set(&hwc->period_left, hwc->sample_period); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5078 | |
| 5079 | /* |
| 5080 | * we currently do not support PERF_FORMAT_GROUP on inherited events |
| 5081 | */ |
| 5082 | if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP)) |
| 5083 | goto done; |
| 5084 | |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5085 | pmu = perf_init_event(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5086 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5087 | done: |
| 5088 | err = 0; |
| 5089 | if (!pmu) |
| 5090 | err = -EINVAL; |
| 5091 | else if (IS_ERR(pmu)) |
| 5092 | err = PTR_ERR(pmu); |
| 5093 | |
| 5094 | if (err) { |
| 5095 | if (event->ns) |
| 5096 | put_pid_ns(event->ns); |
| 5097 | kfree(event); |
| 5098 | return ERR_PTR(err); |
| 5099 | } |
| 5100 | |
| 5101 | event->pmu = pmu; |
| 5102 | |
| 5103 | if (!event->parent) { |
| 5104 | atomic_inc(&nr_events); |
Eric B Munson | 3af9e85 | 2010-05-18 15:30:49 +0100 | [diff] [blame] | 5105 | if (event->attr.mmap || event->attr.mmap_data) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5106 | atomic_inc(&nr_mmap_events); |
| 5107 | if (event->attr.comm) |
| 5108 | atomic_inc(&nr_comm_events); |
| 5109 | if (event->attr.task) |
| 5110 | atomic_inc(&nr_task_events); |
Frederic Weisbecker | 927c7a9 | 2010-07-01 16:20:36 +0200 | [diff] [blame] | 5111 | if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 5112 | err = get_callchain_buffers(); |
| 5113 | if (err) { |
| 5114 | free_event(event); |
| 5115 | return ERR_PTR(err); |
| 5116 | } |
| 5117 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5118 | } |
| 5119 | |
| 5120 | return event; |
| 5121 | } |
| 5122 | |
| 5123 | static int perf_copy_attr(struct perf_event_attr __user *uattr, |
| 5124 | struct perf_event_attr *attr) |
| 5125 | { |
| 5126 | u32 size; |
| 5127 | int ret; |
| 5128 | |
| 5129 | if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) |
| 5130 | return -EFAULT; |
| 5131 | |
| 5132 | /* |
| 5133 | * zero the full structure, so that a short copy will be nice. |
| 5134 | */ |
| 5135 | memset(attr, 0, sizeof(*attr)); |
| 5136 | |
| 5137 | ret = get_user(size, &uattr->size); |
| 5138 | if (ret) |
| 5139 | return ret; |
| 5140 | |
| 5141 | if (size > PAGE_SIZE) /* silly large */ |
| 5142 | goto err_size; |
| 5143 | |
| 5144 | if (!size) /* abi compat */ |
| 5145 | size = PERF_ATTR_SIZE_VER0; |
| 5146 | |
| 5147 | if (size < PERF_ATTR_SIZE_VER0) |
| 5148 | goto err_size; |
| 5149 | |
| 5150 | /* |
| 5151 | * If we're handed a bigger struct than we know of, |
| 5152 | * ensure all the unknown bits are 0 - i.e. new |
| 5153 | * user-space does not rely on any kernel feature |
| 5154 | * extensions we dont know about yet. |
| 5155 | */ |
| 5156 | if (size > sizeof(*attr)) { |
| 5157 | unsigned char __user *addr; |
| 5158 | unsigned char __user *end; |
| 5159 | unsigned char val; |
| 5160 | |
| 5161 | addr = (void __user *)uattr + sizeof(*attr); |
| 5162 | end = (void __user *)uattr + size; |
| 5163 | |
| 5164 | for (; addr < end; addr++) { |
| 5165 | ret = get_user(val, addr); |
| 5166 | if (ret) |
| 5167 | return ret; |
| 5168 | if (val) |
| 5169 | goto err_size; |
| 5170 | } |
| 5171 | size = sizeof(*attr); |
| 5172 | } |
| 5173 | |
| 5174 | ret = copy_from_user(attr, uattr, size); |
| 5175 | if (ret) |
| 5176 | return -EFAULT; |
| 5177 | |
| 5178 | /* |
| 5179 | * If the type exists, the corresponding creation will verify |
| 5180 | * the attr->config. |
| 5181 | */ |
| 5182 | if (attr->type >= PERF_TYPE_MAX) |
| 5183 | return -EINVAL; |
| 5184 | |
Mahesh Salgaonkar | cd75764 | 2010-01-30 10:25:18 +0530 | [diff] [blame] | 5185 | if (attr->__reserved_1) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5186 | return -EINVAL; |
| 5187 | |
| 5188 | if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) |
| 5189 | return -EINVAL; |
| 5190 | |
| 5191 | if (attr->read_format & ~(PERF_FORMAT_MAX-1)) |
| 5192 | return -EINVAL; |
| 5193 | |
| 5194 | out: |
| 5195 | return ret; |
| 5196 | |
| 5197 | err_size: |
| 5198 | put_user(sizeof(*attr), &uattr->size); |
| 5199 | ret = -E2BIG; |
| 5200 | goto out; |
| 5201 | } |
| 5202 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5203 | static int |
| 5204 | perf_event_set_output(struct perf_event *event, struct perf_event *output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5205 | { |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 5206 | struct perf_buffer *buffer = NULL, *old_buffer = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5207 | int ret = -EINVAL; |
| 5208 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5209 | if (!output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5210 | goto set; |
| 5211 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5212 | /* don't allow circular references */ |
| 5213 | if (event == output_event) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5214 | goto out; |
| 5215 | |
Peter Zijlstra | 0f13930 | 2010-05-20 14:35:15 +0200 | [diff] [blame] | 5216 | /* |
| 5217 | * Don't allow cross-cpu buffers |
| 5218 | */ |
| 5219 | if (output_event->cpu != event->cpu) |
| 5220 | goto out; |
| 5221 | |
| 5222 | /* |
| 5223 | * If its not a per-cpu buffer, it must be the same task. |
| 5224 | */ |
| 5225 | if (output_event->cpu == -1 && output_event->ctx != event->ctx) |
| 5226 | goto out; |
| 5227 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5228 | set: |
| 5229 | mutex_lock(&event->mmap_mutex); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5230 | /* Can't redirect output if we've got an active mmap() */ |
| 5231 | if (atomic_read(&event->mmap_count)) |
| 5232 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5233 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5234 | if (output_event) { |
| 5235 | /* get the buffer we want to redirect to */ |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 5236 | buffer = perf_buffer_get(output_event); |
| 5237 | if (!buffer) |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5238 | goto unlock; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5239 | } |
| 5240 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 5241 | old_buffer = event->buffer; |
| 5242 | rcu_assign_pointer(event->buffer, buffer); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5243 | ret = 0; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5244 | unlock: |
| 5245 | mutex_unlock(&event->mmap_mutex); |
| 5246 | |
Peter Zijlstra | ca5135e | 2010-05-28 19:33:23 +0200 | [diff] [blame] | 5247 | if (old_buffer) |
| 5248 | perf_buffer_put(old_buffer); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5249 | out: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5250 | return ret; |
| 5251 | } |
| 5252 | |
| 5253 | /** |
| 5254 | * sys_perf_event_open - open a performance event, associate it to a task/cpu |
| 5255 | * |
| 5256 | * @attr_uptr: event_id type attributes for monitoring/sampling |
| 5257 | * @pid: target pid |
| 5258 | * @cpu: target cpu |
| 5259 | * @group_fd: group leader event fd |
| 5260 | */ |
| 5261 | SYSCALL_DEFINE5(perf_event_open, |
| 5262 | struct perf_event_attr __user *, attr_uptr, |
| 5263 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
| 5264 | { |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5265 | struct perf_event *event, *group_leader = NULL, *output_event = NULL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5266 | struct perf_event_attr attr; |
| 5267 | struct perf_event_context *ctx; |
| 5268 | struct file *event_file = NULL; |
| 5269 | struct file *group_file = NULL; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 5270 | int event_fd; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5271 | int fput_needed = 0; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5272 | int err; |
| 5273 | |
| 5274 | /* for future expandability... */ |
| 5275 | if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT)) |
| 5276 | return -EINVAL; |
| 5277 | |
| 5278 | err = perf_copy_attr(attr_uptr, &attr); |
| 5279 | if (err) |
| 5280 | return err; |
| 5281 | |
| 5282 | if (!attr.exclude_kernel) { |
| 5283 | if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) |
| 5284 | return -EACCES; |
| 5285 | } |
| 5286 | |
| 5287 | if (attr.freq) { |
| 5288 | if (attr.sample_freq > sysctl_perf_event_sample_rate) |
| 5289 | return -EINVAL; |
| 5290 | } |
| 5291 | |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 5292 | event_fd = get_unused_fd_flags(O_RDWR); |
| 5293 | if (event_fd < 0) |
| 5294 | return event_fd; |
| 5295 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5296 | /* |
| 5297 | * Get the target context (task or percpu): |
| 5298 | */ |
| 5299 | ctx = find_get_context(pid, cpu); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 5300 | if (IS_ERR(ctx)) { |
| 5301 | err = PTR_ERR(ctx); |
| 5302 | goto err_fd; |
| 5303 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5304 | |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5305 | if (group_fd != -1) { |
| 5306 | group_leader = perf_fget_light(group_fd, &fput_needed); |
| 5307 | if (IS_ERR(group_leader)) { |
| 5308 | err = PTR_ERR(group_leader); |
| 5309 | goto err_put_context; |
| 5310 | } |
| 5311 | group_file = group_leader->filp; |
| 5312 | if (flags & PERF_FLAG_FD_OUTPUT) |
| 5313 | output_event = group_leader; |
| 5314 | if (flags & PERF_FLAG_FD_NO_GROUP) |
| 5315 | group_leader = NULL; |
| 5316 | } |
| 5317 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5318 | /* |
| 5319 | * Look up the group leader (we will attach this event to it): |
| 5320 | */ |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5321 | if (group_leader) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5322 | err = -EINVAL; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5323 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5324 | /* |
| 5325 | * Do not allow a recursive hierarchy (this new sibling |
| 5326 | * becoming part of another group-sibling): |
| 5327 | */ |
| 5328 | if (group_leader->group_leader != group_leader) |
| 5329 | goto err_put_context; |
| 5330 | /* |
| 5331 | * Do not allow to attach to a group in a different |
| 5332 | * task or CPU context: |
| 5333 | */ |
| 5334 | if (group_leader->ctx != ctx) |
| 5335 | goto err_put_context; |
| 5336 | /* |
| 5337 | * Only a group leader can be exclusive or pinned |
| 5338 | */ |
| 5339 | if (attr.exclusive || attr.pinned) |
| 5340 | goto err_put_context; |
| 5341 | } |
| 5342 | |
| 5343 | event = perf_event_alloc(&attr, cpu, ctx, group_leader, |
Frederic Weisbecker | 97eaf53 | 2009-10-18 15:33:50 +0200 | [diff] [blame] | 5344 | NULL, NULL, GFP_KERNEL); |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5345 | if (IS_ERR(event)) { |
| 5346 | err = PTR_ERR(event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5347 | goto err_put_context; |
Peter Zijlstra | ac9721f | 2010-05-27 12:54:41 +0200 | [diff] [blame] | 5348 | } |
| 5349 | |
| 5350 | if (output_event) { |
| 5351 | err = perf_event_set_output(event, output_event); |
| 5352 | if (err) |
| 5353 | goto err_free_put_context; |
| 5354 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5355 | |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 5356 | event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR); |
| 5357 | if (IS_ERR(event_file)) { |
| 5358 | err = PTR_ERR(event_file); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5359 | goto err_free_put_context; |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 5360 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5361 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5362 | event->filp = event_file; |
| 5363 | WARN_ON_ONCE(ctx->parent_ctx); |
| 5364 | mutex_lock(&ctx->mutex); |
| 5365 | perf_install_in_context(ctx, event, cpu); |
| 5366 | ++ctx->generation; |
| 5367 | mutex_unlock(&ctx->mutex); |
| 5368 | |
| 5369 | event->owner = current; |
| 5370 | get_task_struct(current); |
| 5371 | mutex_lock(¤t->perf_event_mutex); |
| 5372 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); |
| 5373 | mutex_unlock(¤t->perf_event_mutex); |
| 5374 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 5375 | /* |
| 5376 | * Drop the reference on the group_event after placing the |
| 5377 | * new event on the sibling_list. This ensures destruction |
| 5378 | * of the group leader will find the pointer to itself in |
| 5379 | * perf_group_detach(). |
| 5380 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5381 | fput_light(group_file, fput_needed); |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 5382 | fd_install(event_fd, event_file); |
| 5383 | return event_fd; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5384 | |
Al Viro | ea635c6 | 2010-05-26 17:40:29 -0400 | [diff] [blame] | 5385 | err_free_put_context: |
| 5386 | free_event(event); |
| 5387 | err_put_context: |
| 5388 | fput_light(group_file, fput_needed); |
| 5389 | put_ctx(ctx); |
| 5390 | err_fd: |
| 5391 | put_unused_fd(event_fd); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5392 | return err; |
| 5393 | } |
| 5394 | |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 5395 | /** |
| 5396 | * perf_event_create_kernel_counter |
| 5397 | * |
| 5398 | * @attr: attributes of the counter to create |
| 5399 | * @cpu: cpu in which the counter is bound |
| 5400 | * @pid: task to profile |
| 5401 | */ |
| 5402 | struct perf_event * |
| 5403 | perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 5404 | pid_t pid, |
| 5405 | perf_overflow_handler_t overflow_handler) |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 5406 | { |
| 5407 | struct perf_event *event; |
| 5408 | struct perf_event_context *ctx; |
| 5409 | int err; |
| 5410 | |
| 5411 | /* |
| 5412 | * Get the target context (task or percpu): |
| 5413 | */ |
| 5414 | |
| 5415 | ctx = find_get_context(pid, cpu); |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 5416 | if (IS_ERR(ctx)) { |
| 5417 | err = PTR_ERR(ctx); |
| 5418 | goto err_exit; |
| 5419 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 5420 | |
| 5421 | event = perf_event_alloc(attr, cpu, ctx, NULL, |
Frederic Weisbecker | b326e95 | 2009-12-05 09:44:31 +0100 | [diff] [blame] | 5422 | NULL, overflow_handler, GFP_KERNEL); |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 5423 | if (IS_ERR(event)) { |
| 5424 | err = PTR_ERR(event); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 5425 | goto err_put_context; |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 5426 | } |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 5427 | |
| 5428 | event->filp = NULL; |
| 5429 | WARN_ON_ONCE(ctx->parent_ctx); |
| 5430 | mutex_lock(&ctx->mutex); |
| 5431 | perf_install_in_context(ctx, event, cpu); |
| 5432 | ++ctx->generation; |
| 5433 | mutex_unlock(&ctx->mutex); |
| 5434 | |
| 5435 | event->owner = current; |
| 5436 | get_task_struct(current); |
| 5437 | mutex_lock(¤t->perf_event_mutex); |
| 5438 | list_add_tail(&event->owner_entry, ¤t->perf_event_list); |
| 5439 | mutex_unlock(¤t->perf_event_mutex); |
| 5440 | |
| 5441 | return event; |
| 5442 | |
Frederic Weisbecker | c6567f6 | 2009-11-26 05:35:41 +0100 | [diff] [blame] | 5443 | err_put_context: |
| 5444 | put_ctx(ctx); |
| 5445 | err_exit: |
| 5446 | return ERR_PTR(err); |
Arjan van de Ven | fb0459d | 2009-09-25 12:25:56 +0200 | [diff] [blame] | 5447 | } |
| 5448 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); |
| 5449 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5450 | /* |
| 5451 | * inherit a event from parent task to child task: |
| 5452 | */ |
| 5453 | static struct perf_event * |
| 5454 | inherit_event(struct perf_event *parent_event, |
| 5455 | struct task_struct *parent, |
| 5456 | struct perf_event_context *parent_ctx, |
| 5457 | struct task_struct *child, |
| 5458 | struct perf_event *group_leader, |
| 5459 | struct perf_event_context *child_ctx) |
| 5460 | { |
| 5461 | struct perf_event *child_event; |
| 5462 | |
| 5463 | /* |
| 5464 | * Instead of creating recursive hierarchies of events, |
| 5465 | * we link inherited events back to the original parent, |
| 5466 | * which has a filp for sure, which we use as the reference |
| 5467 | * count: |
| 5468 | */ |
| 5469 | if (parent_event->parent) |
| 5470 | parent_event = parent_event->parent; |
| 5471 | |
| 5472 | child_event = perf_event_alloc(&parent_event->attr, |
| 5473 | parent_event->cpu, child_ctx, |
| 5474 | group_leader, parent_event, |
Frederic Weisbecker | 97eaf53 | 2009-10-18 15:33:50 +0200 | [diff] [blame] | 5475 | NULL, GFP_KERNEL); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5476 | if (IS_ERR(child_event)) |
| 5477 | return child_event; |
| 5478 | get_ctx(child_ctx); |
| 5479 | |
| 5480 | /* |
| 5481 | * Make the child state follow the state of the parent event, |
| 5482 | * not its attr.disabled bit. We hold the parent's mutex, |
| 5483 | * so we won't race with perf_event_{en, dis}able_family. |
| 5484 | */ |
| 5485 | if (parent_event->state >= PERF_EVENT_STATE_INACTIVE) |
| 5486 | child_event->state = PERF_EVENT_STATE_INACTIVE; |
| 5487 | else |
| 5488 | child_event->state = PERF_EVENT_STATE_OFF; |
| 5489 | |
Peter Zijlstra | 75c9f32 | 2010-01-29 09:04:26 +0100 | [diff] [blame] | 5490 | if (parent_event->attr.freq) { |
| 5491 | u64 sample_period = parent_event->hw.sample_period; |
| 5492 | struct hw_perf_event *hwc = &child_event->hw; |
| 5493 | |
| 5494 | hwc->sample_period = sample_period; |
| 5495 | hwc->last_period = sample_period; |
| 5496 | |
Peter Zijlstra | e785059 | 2010-05-21 14:43:08 +0200 | [diff] [blame] | 5497 | local64_set(&hwc->period_left, sample_period); |
Peter Zijlstra | 75c9f32 | 2010-01-29 09:04:26 +0100 | [diff] [blame] | 5498 | } |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5499 | |
Peter Zijlstra | 453f19e | 2009-11-20 22:19:43 +0100 | [diff] [blame] | 5500 | child_event->overflow_handler = parent_event->overflow_handler; |
| 5501 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5502 | /* |
| 5503 | * Link it up in the child's context: |
| 5504 | */ |
| 5505 | add_event_to_ctx(child_event, child_ctx); |
| 5506 | |
| 5507 | /* |
| 5508 | * Get a reference to the parent filp - we will fput it |
| 5509 | * when the child event exits. This is safe to do because |
| 5510 | * we are in the parent and we know that the filp still |
| 5511 | * exists and has a nonzero count: |
| 5512 | */ |
| 5513 | atomic_long_inc(&parent_event->filp->f_count); |
| 5514 | |
| 5515 | /* |
| 5516 | * Link this into the parent event's child list |
| 5517 | */ |
| 5518 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 5519 | mutex_lock(&parent_event->child_mutex); |
| 5520 | list_add_tail(&child_event->child_list, &parent_event->child_list); |
| 5521 | mutex_unlock(&parent_event->child_mutex); |
| 5522 | |
| 5523 | return child_event; |
| 5524 | } |
| 5525 | |
| 5526 | static int inherit_group(struct perf_event *parent_event, |
| 5527 | struct task_struct *parent, |
| 5528 | struct perf_event_context *parent_ctx, |
| 5529 | struct task_struct *child, |
| 5530 | struct perf_event_context *child_ctx) |
| 5531 | { |
| 5532 | struct perf_event *leader; |
| 5533 | struct perf_event *sub; |
| 5534 | struct perf_event *child_ctr; |
| 5535 | |
| 5536 | leader = inherit_event(parent_event, parent, parent_ctx, |
| 5537 | child, NULL, child_ctx); |
| 5538 | if (IS_ERR(leader)) |
| 5539 | return PTR_ERR(leader); |
| 5540 | list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { |
| 5541 | child_ctr = inherit_event(sub, parent, parent_ctx, |
| 5542 | child, leader, child_ctx); |
| 5543 | if (IS_ERR(child_ctr)) |
| 5544 | return PTR_ERR(child_ctr); |
| 5545 | } |
| 5546 | return 0; |
| 5547 | } |
| 5548 | |
| 5549 | static void sync_child_event(struct perf_event *child_event, |
| 5550 | struct task_struct *child) |
| 5551 | { |
| 5552 | struct perf_event *parent_event = child_event->parent; |
| 5553 | u64 child_val; |
| 5554 | |
| 5555 | if (child_event->attr.inherit_stat) |
| 5556 | perf_event_read_event(child_event, child); |
| 5557 | |
Peter Zijlstra | b5e5879 | 2010-05-21 14:43:12 +0200 | [diff] [blame] | 5558 | child_val = perf_event_count(child_event); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5559 | |
| 5560 | /* |
| 5561 | * Add back the child's count to the parent's count: |
| 5562 | */ |
Peter Zijlstra | a6e6dea | 2010-05-21 14:27:58 +0200 | [diff] [blame] | 5563 | atomic64_add(child_val, &parent_event->child_count); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5564 | atomic64_add(child_event->total_time_enabled, |
| 5565 | &parent_event->child_total_time_enabled); |
| 5566 | atomic64_add(child_event->total_time_running, |
| 5567 | &parent_event->child_total_time_running); |
| 5568 | |
| 5569 | /* |
| 5570 | * Remove this event from the parent's list |
| 5571 | */ |
| 5572 | WARN_ON_ONCE(parent_event->ctx->parent_ctx); |
| 5573 | mutex_lock(&parent_event->child_mutex); |
| 5574 | list_del_init(&child_event->child_list); |
| 5575 | mutex_unlock(&parent_event->child_mutex); |
| 5576 | |
| 5577 | /* |
| 5578 | * Release the parent event, if this was the last |
| 5579 | * reference to it. |
| 5580 | */ |
| 5581 | fput(parent_event->filp); |
| 5582 | } |
| 5583 | |
| 5584 | static void |
| 5585 | __perf_event_exit_task(struct perf_event *child_event, |
| 5586 | struct perf_event_context *child_ctx, |
| 5587 | struct task_struct *child) |
| 5588 | { |
| 5589 | struct perf_event *parent_event; |
| 5590 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5591 | perf_event_remove_from_context(child_event); |
| 5592 | |
| 5593 | parent_event = child_event->parent; |
| 5594 | /* |
| 5595 | * It can happen that parent exits first, and has events |
| 5596 | * that are still around due to the child reference. These |
| 5597 | * events need to be zapped - but otherwise linger. |
| 5598 | */ |
| 5599 | if (parent_event) { |
| 5600 | sync_child_event(child_event, child); |
| 5601 | free_event(child_event); |
| 5602 | } |
| 5603 | } |
| 5604 | |
| 5605 | /* |
| 5606 | * When a child task exits, feed back event values to parent events. |
| 5607 | */ |
| 5608 | void perf_event_exit_task(struct task_struct *child) |
| 5609 | { |
| 5610 | struct perf_event *child_event, *tmp; |
| 5611 | struct perf_event_context *child_ctx; |
| 5612 | unsigned long flags; |
| 5613 | |
| 5614 | if (likely(!child->perf_event_ctxp)) { |
| 5615 | perf_event_task(child, NULL, 0); |
| 5616 | return; |
| 5617 | } |
| 5618 | |
| 5619 | local_irq_save(flags); |
| 5620 | /* |
| 5621 | * We can't reschedule here because interrupts are disabled, |
| 5622 | * and either child is current or it is a task that can't be |
| 5623 | * scheduled, so we are now safe from rescheduling changing |
| 5624 | * our context. |
| 5625 | */ |
| 5626 | child_ctx = child->perf_event_ctxp; |
| 5627 | __perf_event_task_sched_out(child_ctx); |
| 5628 | |
| 5629 | /* |
| 5630 | * Take the context lock here so that if find_get_context is |
| 5631 | * reading child->perf_event_ctxp, we wait until it has |
| 5632 | * incremented the context's refcount before we do put_ctx below. |
| 5633 | */ |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 5634 | raw_spin_lock(&child_ctx->lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5635 | child->perf_event_ctxp = NULL; |
| 5636 | /* |
| 5637 | * If this context is a clone; unclone it so it can't get |
| 5638 | * swapped to another process while we're removing all |
| 5639 | * the events from it. |
| 5640 | */ |
| 5641 | unclone_ctx(child_ctx); |
Peter Zijlstra | 5e942bb | 2009-11-23 11:37:26 +0100 | [diff] [blame] | 5642 | update_context_time(child_ctx); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 5643 | raw_spin_unlock_irqrestore(&child_ctx->lock, flags); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5644 | |
| 5645 | /* |
| 5646 | * Report the task dead after unscheduling the events so that we |
| 5647 | * won't get any samples after PERF_RECORD_EXIT. We can however still |
| 5648 | * get a few PERF_RECORD_READ events. |
| 5649 | */ |
| 5650 | perf_event_task(child, child_ctx, 0); |
| 5651 | |
| 5652 | /* |
| 5653 | * We can recurse on the same lock type through: |
| 5654 | * |
| 5655 | * __perf_event_exit_task() |
| 5656 | * sync_child_event() |
| 5657 | * fput(parent_event->filp) |
| 5658 | * perf_release() |
| 5659 | * mutex_lock(&ctx->mutex) |
| 5660 | * |
| 5661 | * But since its the parent context it won't be the same instance. |
| 5662 | */ |
Peter Zijlstra | a0507c8 | 2010-05-06 15:42:53 +0200 | [diff] [blame] | 5663 | mutex_lock(&child_ctx->mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5664 | |
| 5665 | again: |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 5666 | list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups, |
| 5667 | group_entry) |
| 5668 | __perf_event_exit_task(child_event, child_ctx, child); |
| 5669 | |
| 5670 | list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5671 | group_entry) |
| 5672 | __perf_event_exit_task(child_event, child_ctx, child); |
| 5673 | |
| 5674 | /* |
| 5675 | * If the last event was a group event, it will have appended all |
| 5676 | * its siblings to the list, but we obtained 'tmp' before that which |
| 5677 | * will still point to the list head terminating the iteration. |
| 5678 | */ |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 5679 | if (!list_empty(&child_ctx->pinned_groups) || |
| 5680 | !list_empty(&child_ctx->flexible_groups)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5681 | goto again; |
| 5682 | |
| 5683 | mutex_unlock(&child_ctx->mutex); |
| 5684 | |
| 5685 | put_ctx(child_ctx); |
| 5686 | } |
| 5687 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 5688 | static void perf_free_event(struct perf_event *event, |
| 5689 | struct perf_event_context *ctx) |
| 5690 | { |
| 5691 | struct perf_event *parent = event->parent; |
| 5692 | |
| 5693 | if (WARN_ON_ONCE(!parent)) |
| 5694 | return; |
| 5695 | |
| 5696 | mutex_lock(&parent->child_mutex); |
| 5697 | list_del_init(&event->child_list); |
| 5698 | mutex_unlock(&parent->child_mutex); |
| 5699 | |
| 5700 | fput(parent->filp); |
| 5701 | |
Peter Zijlstra | 8a49542 | 2010-05-27 15:47:49 +0200 | [diff] [blame] | 5702 | perf_group_detach(event); |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 5703 | list_del_event(event, ctx); |
| 5704 | free_event(event); |
| 5705 | } |
| 5706 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5707 | /* |
| 5708 | * free an unexposed, unused context as created by inheritance by |
| 5709 | * init_task below, used by fork() in case of fail. |
| 5710 | */ |
| 5711 | void perf_event_free_task(struct task_struct *task) |
| 5712 | { |
| 5713 | struct perf_event_context *ctx = task->perf_event_ctxp; |
| 5714 | struct perf_event *event, *tmp; |
| 5715 | |
| 5716 | if (!ctx) |
| 5717 | return; |
| 5718 | |
| 5719 | mutex_lock(&ctx->mutex); |
| 5720 | again: |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 5721 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry) |
| 5722 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5723 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 5724 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, |
| 5725 | group_entry) |
| 5726 | perf_free_event(event, ctx); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5727 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 5728 | if (!list_empty(&ctx->pinned_groups) || |
| 5729 | !list_empty(&ctx->flexible_groups)) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5730 | goto again; |
| 5731 | |
| 5732 | mutex_unlock(&ctx->mutex); |
| 5733 | |
| 5734 | put_ctx(ctx); |
| 5735 | } |
| 5736 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 5737 | static int |
| 5738 | inherit_task_group(struct perf_event *event, struct task_struct *parent, |
| 5739 | struct perf_event_context *parent_ctx, |
| 5740 | struct task_struct *child, |
| 5741 | int *inherited_all) |
| 5742 | { |
| 5743 | int ret; |
| 5744 | struct perf_event_context *child_ctx = child->perf_event_ctxp; |
| 5745 | |
| 5746 | if (!event->attr.inherit) { |
| 5747 | *inherited_all = 0; |
| 5748 | return 0; |
| 5749 | } |
| 5750 | |
| 5751 | if (!child_ctx) { |
| 5752 | /* |
| 5753 | * This is executed from the parent task context, so |
| 5754 | * inherit events that have been marked for cloning. |
| 5755 | * First allocate and initialize a context for the |
| 5756 | * child. |
| 5757 | */ |
| 5758 | |
| 5759 | child_ctx = kzalloc(sizeof(struct perf_event_context), |
| 5760 | GFP_KERNEL); |
| 5761 | if (!child_ctx) |
| 5762 | return -ENOMEM; |
| 5763 | |
| 5764 | __perf_event_init_context(child_ctx, child); |
| 5765 | child->perf_event_ctxp = child_ctx; |
| 5766 | get_task_struct(child); |
| 5767 | } |
| 5768 | |
| 5769 | ret = inherit_group(event, parent, parent_ctx, |
| 5770 | child, child_ctx); |
| 5771 | |
| 5772 | if (ret) |
| 5773 | *inherited_all = 0; |
| 5774 | |
| 5775 | return ret; |
| 5776 | } |
| 5777 | |
| 5778 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5779 | /* |
| 5780 | * Initialize the perf_event context in task_struct |
| 5781 | */ |
| 5782 | int perf_event_init_task(struct task_struct *child) |
| 5783 | { |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 5784 | struct perf_event_context *child_ctx, *parent_ctx; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5785 | struct perf_event_context *cloned_ctx; |
| 5786 | struct perf_event *event; |
| 5787 | struct task_struct *parent = current; |
| 5788 | int inherited_all = 1; |
| 5789 | int ret = 0; |
| 5790 | |
| 5791 | child->perf_event_ctxp = NULL; |
| 5792 | |
| 5793 | mutex_init(&child->perf_event_mutex); |
| 5794 | INIT_LIST_HEAD(&child->perf_event_list); |
| 5795 | |
| 5796 | if (likely(!parent->perf_event_ctxp)) |
| 5797 | return 0; |
| 5798 | |
| 5799 | /* |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5800 | * If the parent's context is a clone, pin it so it won't get |
| 5801 | * swapped under us. |
| 5802 | */ |
| 5803 | parent_ctx = perf_pin_task_context(parent); |
| 5804 | |
| 5805 | /* |
| 5806 | * No need to check if parent_ctx != NULL here; since we saw |
| 5807 | * it non-NULL earlier, the only reason for it to become NULL |
| 5808 | * is if we exit, and since we're currently in the middle of |
| 5809 | * a fork we can't be exiting at the same time. |
| 5810 | */ |
| 5811 | |
| 5812 | /* |
| 5813 | * Lock the parent list. No need to lock the child - not PID |
| 5814 | * hashed yet and not running, so nobody can access it. |
| 5815 | */ |
| 5816 | mutex_lock(&parent_ctx->mutex); |
| 5817 | |
| 5818 | /* |
| 5819 | * We dont have to disable NMIs - we are only looking at |
| 5820 | * the list, not manipulating it: |
| 5821 | */ |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 5822 | list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { |
| 5823 | ret = inherit_task_group(event, parent, parent_ctx, child, |
| 5824 | &inherited_all); |
| 5825 | if (ret) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5826 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5827 | } |
| 5828 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 5829 | list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { |
| 5830 | ret = inherit_task_group(event, parent, parent_ctx, child, |
| 5831 | &inherited_all); |
| 5832 | if (ret) |
| 5833 | break; |
| 5834 | } |
| 5835 | |
| 5836 | child_ctx = child->perf_event_ctxp; |
| 5837 | |
Peter Zijlstra | 05cbaa2 | 2009-12-30 16:00:35 +0100 | [diff] [blame] | 5838 | if (child_ctx && inherited_all) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5839 | /* |
| 5840 | * Mark the child context as a clone of the parent |
| 5841 | * context, or of whatever the parent is a clone of. |
| 5842 | * Note that if the parent is a clone, it could get |
| 5843 | * uncloned at any point, but that doesn't matter |
| 5844 | * because the list of events and the generation |
| 5845 | * count can't have changed since we took the mutex. |
| 5846 | */ |
| 5847 | cloned_ctx = rcu_dereference(parent_ctx->parent_ctx); |
| 5848 | if (cloned_ctx) { |
| 5849 | child_ctx->parent_ctx = cloned_ctx; |
| 5850 | child_ctx->parent_gen = parent_ctx->parent_gen; |
| 5851 | } else { |
| 5852 | child_ctx->parent_ctx = parent_ctx; |
| 5853 | child_ctx->parent_gen = parent_ctx->generation; |
| 5854 | } |
| 5855 | get_ctx(child_ctx->parent_ctx); |
| 5856 | } |
| 5857 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5858 | mutex_unlock(&parent_ctx->mutex); |
| 5859 | |
| 5860 | perf_unpin_context(parent_ctx); |
| 5861 | |
| 5862 | return ret; |
| 5863 | } |
| 5864 | |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 5865 | static void __init perf_event_init_all_cpus(void) |
| 5866 | { |
| 5867 | int cpu; |
| 5868 | struct perf_cpu_context *cpuctx; |
| 5869 | |
| 5870 | for_each_possible_cpu(cpu) { |
| 5871 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5872 | mutex_init(&cpuctx->hlist_mutex); |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 5873 | __perf_event_init_context(&cpuctx->ctx, NULL); |
| 5874 | } |
| 5875 | } |
| 5876 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5877 | static void __cpuinit perf_event_init_cpu(int cpu) |
| 5878 | { |
| 5879 | struct perf_cpu_context *cpuctx; |
| 5880 | |
| 5881 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5882 | |
| 5883 | spin_lock(&perf_resource_lock); |
| 5884 | cpuctx->max_pertask = perf_max_events - perf_reserved_percpu; |
| 5885 | spin_unlock(&perf_resource_lock); |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5886 | |
| 5887 | mutex_lock(&cpuctx->hlist_mutex); |
| 5888 | if (cpuctx->hlist_refcount > 0) { |
| 5889 | struct swevent_hlist *hlist; |
| 5890 | |
| 5891 | hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); |
| 5892 | WARN_ON_ONCE(!hlist); |
| 5893 | rcu_assign_pointer(cpuctx->swevent_hlist, hlist); |
| 5894 | } |
| 5895 | mutex_unlock(&cpuctx->hlist_mutex); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5896 | } |
| 5897 | |
| 5898 | #ifdef CONFIG_HOTPLUG_CPU |
| 5899 | static void __perf_event_exit_cpu(void *info) |
| 5900 | { |
| 5901 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 5902 | struct perf_event_context *ctx = &cpuctx->ctx; |
| 5903 | struct perf_event *event, *tmp; |
| 5904 | |
Frederic Weisbecker | 889ff01 | 2010-01-09 20:04:47 +0100 | [diff] [blame] | 5905 | list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry) |
| 5906 | __perf_event_remove_from_context(event); |
| 5907 | list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5908 | __perf_event_remove_from_context(event); |
| 5909 | } |
| 5910 | static void perf_event_exit_cpu(int cpu) |
| 5911 | { |
| 5912 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 5913 | struct perf_event_context *ctx = &cpuctx->ctx; |
| 5914 | |
Frederic Weisbecker | 76e1d90 | 2010-04-05 15:35:57 +0200 | [diff] [blame] | 5915 | mutex_lock(&cpuctx->hlist_mutex); |
| 5916 | swevent_hlist_release(cpuctx); |
| 5917 | mutex_unlock(&cpuctx->hlist_mutex); |
| 5918 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5919 | mutex_lock(&ctx->mutex); |
| 5920 | smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1); |
| 5921 | mutex_unlock(&ctx->mutex); |
| 5922 | } |
| 5923 | #else |
| 5924 | static inline void perf_event_exit_cpu(int cpu) { } |
| 5925 | #endif |
| 5926 | |
| 5927 | static int __cpuinit |
| 5928 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
| 5929 | { |
| 5930 | unsigned int cpu = (long)hcpu; |
| 5931 | |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5932 | switch (action & ~CPU_TASKS_FROZEN) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5933 | |
| 5934 | case CPU_UP_PREPARE: |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5935 | case CPU_DOWN_FAILED: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5936 | perf_event_init_cpu(cpu); |
| 5937 | break; |
| 5938 | |
Peter Zijlstra | 5e11637 | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5939 | case CPU_UP_CANCELED: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5940 | case CPU_DOWN_PREPARE: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5941 | perf_event_exit_cpu(cpu); |
| 5942 | break; |
| 5943 | |
| 5944 | default: |
| 5945 | break; |
| 5946 | } |
| 5947 | |
| 5948 | return NOTIFY_OK; |
| 5949 | } |
| 5950 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5951 | void __init perf_event_init(void) |
| 5952 | { |
Paul Mackerras | 220b140 | 2010-03-10 20:45:52 +1100 | [diff] [blame] | 5953 | perf_event_init_all_cpus(); |
Peter Zijlstra | b0a873e | 2010-06-11 13:35:08 +0200 | [diff] [blame] | 5954 | init_srcu_struct(&pmus_srcu); |
| 5955 | perf_pmu_register(&perf_swevent); |
| 5956 | perf_pmu_register(&perf_cpu_clock); |
| 5957 | perf_pmu_register(&perf_task_clock); |
| 5958 | perf_tp_register(); |
| 5959 | perf_cpu_notifier(perf_cpu_notify); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5960 | } |
| 5961 | |
Andi Kleen | c9be0a3 | 2010-01-05 12:47:58 +0100 | [diff] [blame] | 5962 | static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, |
| 5963 | struct sysdev_class_attribute *attr, |
| 5964 | char *buf) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5965 | { |
| 5966 | return sprintf(buf, "%d\n", perf_reserved_percpu); |
| 5967 | } |
| 5968 | |
| 5969 | static ssize_t |
| 5970 | perf_set_reserve_percpu(struct sysdev_class *class, |
Andi Kleen | c9be0a3 | 2010-01-05 12:47:58 +0100 | [diff] [blame] | 5971 | struct sysdev_class_attribute *attr, |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5972 | const char *buf, |
| 5973 | size_t count) |
| 5974 | { |
| 5975 | struct perf_cpu_context *cpuctx; |
| 5976 | unsigned long val; |
| 5977 | int err, cpu, mpt; |
| 5978 | |
| 5979 | err = strict_strtoul(buf, 10, &val); |
| 5980 | if (err) |
| 5981 | return err; |
| 5982 | if (val > perf_max_events) |
| 5983 | return -EINVAL; |
| 5984 | |
| 5985 | spin_lock(&perf_resource_lock); |
| 5986 | perf_reserved_percpu = val; |
| 5987 | for_each_online_cpu(cpu) { |
| 5988 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 5989 | raw_spin_lock_irq(&cpuctx->ctx.lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5990 | mpt = min(perf_max_events - cpuctx->ctx.nr_events, |
| 5991 | perf_max_events - perf_reserved_percpu); |
| 5992 | cpuctx->max_pertask = mpt; |
Thomas Gleixner | e625cce1 | 2009-11-17 18:02:06 +0100 | [diff] [blame] | 5993 | raw_spin_unlock_irq(&cpuctx->ctx.lock); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5994 | } |
| 5995 | spin_unlock(&perf_resource_lock); |
| 5996 | |
| 5997 | return count; |
| 5998 | } |
| 5999 | |
Andi Kleen | c9be0a3 | 2010-01-05 12:47:58 +0100 | [diff] [blame] | 6000 | static ssize_t perf_show_overcommit(struct sysdev_class *class, |
| 6001 | struct sysdev_class_attribute *attr, |
| 6002 | char *buf) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6003 | { |
| 6004 | return sprintf(buf, "%d\n", perf_overcommit); |
| 6005 | } |
| 6006 | |
| 6007 | static ssize_t |
Andi Kleen | c9be0a3 | 2010-01-05 12:47:58 +0100 | [diff] [blame] | 6008 | perf_set_overcommit(struct sysdev_class *class, |
| 6009 | struct sysdev_class_attribute *attr, |
| 6010 | const char *buf, size_t count) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 6011 | { |
| 6012 | unsigned long val; |
| 6013 | int err; |
| 6014 | |
| 6015 | err = strict_strtoul(buf, 10, &val); |
| 6016 | if (err) |
| 6017 | return err; |
| 6018 | if (val > 1) |
| 6019 | return -EINVAL; |
| 6020 | |
| 6021 | spin_lock(&perf_resource_lock); |
| 6022 | perf_overcommit = val; |
| 6023 | spin_unlock(&perf_resource_lock); |
| 6024 | |
| 6025 | return count; |
| 6026 | } |
| 6027 | |
| 6028 | static SYSDEV_CLASS_ATTR( |
| 6029 | reserve_percpu, |
| 6030 | 0644, |
| 6031 | perf_show_reserve_percpu, |
| 6032 | perf_set_reserve_percpu |
| 6033 | ); |
| 6034 | |
| 6035 | static SYSDEV_CLASS_ATTR( |
| 6036 | overcommit, |
| 6037 | 0644, |
| 6038 | perf_show_overcommit, |
| 6039 | perf_set_overcommit |
| 6040 | ); |
| 6041 | |
| 6042 | static struct attribute *perfclass_attrs[] = { |
| 6043 | &attr_reserve_percpu.attr, |
| 6044 | &attr_overcommit.attr, |
| 6045 | NULL |
| 6046 | }; |
| 6047 | |
| 6048 | static struct attribute_group perfclass_attr_group = { |
| 6049 | .attrs = perfclass_attrs, |
| 6050 | .name = "perf_events", |
| 6051 | }; |
| 6052 | |
| 6053 | static int __init perf_event_sysfs_init(void) |
| 6054 | { |
| 6055 | return sysfs_create_group(&cpu_sysdev_class.kset.kobj, |
| 6056 | &perfclass_attr_group); |
| 6057 | } |
| 6058 | device_initcall(perf_event_sysfs_init); |