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