blob: 288ce43de57c3111fc07364a3cfa880fe3808cbc [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02002 * Performance events core code:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003 *
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 Molnar57c0c152009-09-21 12:20:38 +02009 * For licensing details see kernel-base/COPYING
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010 */
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 Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020019#include <linux/hash.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020020#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 Zijlstra906010b2009-09-21 16:08:49 +020025#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020026#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 Zefan6fb29152009-10-15 11:21:42 +080033#include <linux/ftrace_event.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020034
35#include <asm/irq_regs.h>
36
37/*
38 * Each CPU has a list of per CPU events:
39 */
Xiao Guangrongaa5452d2009-12-09 11:28:13 +080040static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020041
42int perf_max_events __read_mostly = 1;
43static int perf_reserved_percpu __read_mostly;
44static int perf_overcommit __read_mostly = 1;
45
46static atomic_t nr_events __read_mostly;
47static atomic_t nr_mmap_events __read_mostly;
48static atomic_t nr_comm_events __read_mostly;
49static 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 */
58int sysctl_perf_event_paranoid __read_mostly = 1;
59
Ingo Molnarcdd6c482009-09-21 12:02:48 +020060int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
61
62/*
63 * max perf event sample rate
64 */
65int sysctl_perf_event_sample_rate __read_mostly = 100000;
66
67static atomic64_t perf_event_id;
68
69/*
70 * Lock for (sysadmin-configurable) event reservations:
71 */
72static DEFINE_SPINLOCK(perf_resource_lock);
73
Ingo Molnarcdd6c482009-09-21 12:02:48 +020074void __weak hw_perf_disable(void) { barrier(); }
75void __weak hw_perf_enable(void) { barrier(); }
76
Ingo Molnarcdd6c482009-09-21 12:02:48 +020077void __weak perf_event_print_debug(void) { }
78
79static DEFINE_PER_CPU(int, perf_disable_count);
80
Ingo Molnarcdd6c482009-09-21 12:02:48 +020081void perf_disable(void)
82{
Peter Zijlstra32975a42010-03-06 19:49:19 +010083 if (!__get_cpu_var(perf_disable_count)++)
84 hw_perf_disable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +020085}
86
87void perf_enable(void)
88{
Peter Zijlstra32975a42010-03-06 19:49:19 +010089 if (!--__get_cpu_var(perf_disable_count))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020090 hw_perf_enable();
91}
92
93static void get_ctx(struct perf_event_context *ctx)
94{
95 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
96}
97
98static 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
106static 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
117static 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 */
129static 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 */
144static struct perf_event_context *
145perf_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 Gleixnere625cce12009-11-17 18:02:06 +0100163 raw_spin_lock_irqsave(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200164 if (ctx != rcu_dereference(task->perf_event_ctxp)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100165 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200166 goto retry;
167 }
168
169 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100170 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200171 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 */
183static 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 Gleixnere625cce12009-11-17 18:02:06 +0100191 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200192 }
193 return ctx;
194}
195
196static void perf_unpin_context(struct perf_event_context *ctx)
197{
198 unsigned long flags;
199
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100200 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200201 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100202 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200203 put_ctx(ctx);
204}
205
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100206static inline u64 perf_clock(void)
207{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200208 return local_clock();
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100209}
210
211/*
212 * Update the record of the current time in a context.
213 */
214static 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 */
225static 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 Zijlstraacd1d7c2009-11-23 15:00:36 +0100234 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 Zijlstraf67218c2009-11-23 11:37:27 +0100240
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 Zijlstra96c21a42010-05-11 16:19:10 +0200249/*
250 * Update total_time_enabled and total_time_running for all events in a group.
251 */
252static 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 Weisbecker889ff012010-01-09 20:04:47 +0100261static struct list_head *
262ctx_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 Molnarcdd6c482009-09-21 12:02:48 +0200270/*
271 * Add a event from the lists for its context.
272 * Must be called with ctx->mutex and ctx->lock held.
273 */
274static void
275list_add_event(struct perf_event *event, struct perf_event_context *ctx)
276{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200277 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
278 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200279
280 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200281 * 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 Molnarcdd6c482009-09-21 12:02:48 +0200284 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200285 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100286 struct list_head *list;
287
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100288 if (is_software_event(event))
289 event->group_flags |= PERF_GROUP_SOFTWARE;
290
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100291 list = ctx_group_list(event, ctx);
292 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200293 }
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 Zijlstra8a495422010-05-27 15:47:49 +0200301static 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 Molnarcdd6c482009-09-21 12:02:48 +0200319/*
320 * Remove a event from the lists for its context.
321 * Must be called with ctx->mutex and ctx->lock held.
322 */
323static void
324list_del_event(struct perf_event *event, struct perf_event_context *ctx)
325{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200326 /*
327 * We can have double detach due to exit/hot-unplug + close.
328 */
329 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200330 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200331
332 event->attach_state &= ~PERF_ATTACH_CONTEXT;
333
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200334 ctx->nr_events--;
335 if (event->attr.inherit_stat)
336 ctx->nr_stat--;
337
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200338 list_del_rcu(&event->event_entry);
339
Peter Zijlstra8a495422010-05-27 15:47:49 +0200340 if (event->group_leader == event)
341 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200342
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200343 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800344
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 Zijlstra050735b2010-05-11 11:51:53 +0200354}
355
Peter Zijlstra8a495422010-05-27 15:47:49 +0200356static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +0200357{
358 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200359 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 Zijlstra2e2af502009-11-23 11:37:25 +0100380
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200381 /*
382 * If this was a group event with sibling events then
383 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +0200384 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200385 */
386 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +0200387 if (list)
388 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200389 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100390
391 /* Inherit group flags from the previous leader */
392 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200393 }
394}
395
Stephane Eranianfa66f072010-08-26 16:40:01 +0200396static inline int
397event_filter_match(struct perf_event *event)
398{
399 return event->cpu == -1 || event->cpu == smp_processor_id();
400}
401
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200402static void
403event_sched_out(struct perf_event *event,
404 struct perf_cpu_context *cpuctx,
405 struct perf_event_context *ctx)
406{
Stephane Eranianfa66f072010-08-26 16:40:01 +0200407 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 Molnarcdd6c482009-09-21 12:02:48 +0200421 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
440static void
441group_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 Eranianfa66f072010-08-26 16:40:01 +0200446 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200447
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 Eranianfa66f072010-08-26 16:40:01 +0200456 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200457 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 */
466static 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 Gleixnere625cce12009-11-17 18:02:06 +0100480 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200481 /*
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 Gleixnere625cce12009-11-17 18:02:06 +0100502 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200503}
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 */
521static 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 Rosaaf901ca2009-11-14 13:09:05 -0200529 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200530 */
531 smp_call_function_single(event->cpu,
532 __perf_event_remove_from_context,
533 event, 1);
534 return;
535 }
536
537retry:
538 task_oncpu_function_call(task, __perf_event_remove_from_context,
539 event);
540
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100541 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200542 /*
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 Gleixnere625cce12009-11-17 18:02:06 +0100546 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200547 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 Zijlstra6c2bfcb2009-11-23 11:37:24 +0100555 if (!list_empty(&event->group_entry))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200556 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100557 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200558}
559
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200560/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200561 * Cross CPU call to disable a performance event
562 */
563static 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 Gleixnere625cce12009-11-17 18:02:06 +0100576 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200577
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 Gleixnere625cce12009-11-17 18:02:06 +0100592 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200593}
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 Weisbecker44234ad2009-12-09 09:25:48 +0100608void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200609{
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 Gleixnere625cce12009-11-17 18:02:06 +0100625 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200626 /*
627 * If the event is still active, we need to retry the cross-call.
628 */
629 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100630 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200631 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 Gleixnere625cce12009-11-17 18:02:06 +0100643 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200644}
645
646static int
647event_sched_in(struct perf_event *event,
648 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100649 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200650{
651 if (event->state <= PERF_EVENT_STATE_OFF)
652 return 0;
653
654 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +0100655 event->oncpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200656 /*
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
679static int
680group_sched_in(struct perf_event *group_event,
681 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100682 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200683{
Lin Ming6bde9b62010-04-23 13:56:00 +0800684 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +0200685 struct pmu *pmu = group_event->pmu;
Lin Ming6bde9b62010-04-23 13:56:00 +0800686 bool txn = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200687
688 if (group_event->state == PERF_EVENT_STATE_OFF)
689 return 0;
690
Lin Ming6bde9b62010-04-23 13:56:00 +0800691 /* Check if group transaction availabe */
692 if (pmu->start_txn)
693 txn = true;
694
695 if (txn)
696 pmu->start_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200697
Stephane Eranian90151c352010-05-25 16:23:10 +0200698 if (event_sched_in(group_event, cpuctx, ctx)) {
699 if (txn)
700 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200701 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +0200702 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200703
704 /*
705 * Schedule in siblings as one group (if any):
706 */
707 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Peter Zijlstra6e377382010-02-11 13:21:58 +0100708 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200709 partial_group = event;
710 goto group_error;
711 }
712 }
713
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +0200714 if (!txn || !pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +1000715 return 0;
Lin Ming6bde9b62010-04-23 13:56:00 +0800716
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200717group_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 Eranian90151c352010-05-25 16:23:10 +0200729 if (txn)
730 pmu->cancel_txn(pmu);
731
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200732 return -EAGAIN;
733}
734
735/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200736 * Work out whether we can put this event group on the CPU now.
737 */
738static 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 Weisbeckerd6f962b2010-01-10 01:25:51 +0100745 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200746 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
766static void add_event_to_ctx(struct perf_event *event,
767 struct perf_event_context *ctx)
768{
769 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200770 perf_group_attach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200771 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 */
781static 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 Molnarcdd6c482009-09-21 12:02:48 +0200787 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 Gleixnere625cce12009-11-17 18:02:06 +0100802 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200803 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 Zijlstraf4c41762009-12-16 17:55:54 +0100814 if (event->cpu != -1 && event->cpu != smp_processor_id())
815 goto unlock;
816
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200817 /*
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 Zijlstra6e377382010-02-11 13:21:58 +0100833 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200834
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 Gleixnere625cce12009-11-17 18:02:06 +0100855 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200856}
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 */
870static void
871perf_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 Rosaaf901ca2009-11-14 13:09:05 -0200880 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200881 */
882 smp_call_function_single(cpu, __perf_install_in_context,
883 event, 1);
884 return;
885 }
886
887retry:
888 task_oncpu_function_call(task, __perf_install_in_context,
889 event);
890
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100891 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200892 /*
893 * we need to retry the smp call.
894 */
895 if (ctx->is_active && list_empty(&event->group_entry)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100896 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200897 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 Gleixnere625cce12009-11-17 18:02:06 +0100907 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200908}
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 */
918static 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 */
934static 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 Gleixnere625cce12009-11-17 18:02:06 +0100952 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200953 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 Zijlstraf4c41762009-12-16 17:55:54 +0100960 if (event->cpu != -1 && event->cpu != smp_processor_id())
961 goto unlock;
962
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200963 /*
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 Zijlstra6e377382010-02-11 13:21:58 +0100975 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200976 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100977 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200978 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 Gleixnere625cce12009-11-17 18:02:06 +0100995 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200996}
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 Weisbecker44234ad2009-12-09 09:25:48 +01001007void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001008{
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 Gleixnere625cce12009-11-17 18:02:06 +01001021 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001022 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 Gleixnere625cce12009-11-17 18:02:06 +01001036 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001037 task_oncpu_function_call(task, __perf_event_enable, event);
1038
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001039 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001040
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 Gleixnere625cce12009-11-17 18:02:06 +01001056 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001057}
1058
1059static 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 Weisbecker5b0311e2010-01-17 11:59:13 +01001073enum event_type_t {
1074 EVENT_FLEXIBLE = 0x1,
1075 EVENT_PINNED = 0x2,
1076 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1077};
1078
1079static void ctx_sched_out(struct perf_event_context *ctx,
1080 struct perf_cpu_context *cpuctx,
1081 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001082{
1083 struct perf_event *event;
1084
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001085 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001086 ctx->is_active = 0;
1087 if (likely(!ctx->nr_events))
1088 goto out;
1089 update_context_time(ctx);
1090
1091 perf_disable();
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001092 if (!ctx->nr_active)
1093 goto out_enable;
1094
1095 if (event_type & EVENT_PINNED)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001096 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1097 group_sched_out(event, cpuctx, ctx);
1098
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001099 if (event_type & EVENT_FLEXIBLE)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001100 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001101 group_sched_out(event, cpuctx, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001102
1103 out_enable:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001104 perf_enable();
1105 out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001106 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001107}
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 */
1120static 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 Molnarcdd6c482009-09-21 12:02:48 +02001128static 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 Zijlstra3dbebf12009-11-20 22:19:52 +01001145 event->pmu->read(event);
1146 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001147
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 Zijlstrae7850592010-05-21 14:43:08 +02001160 value = local64_read(&next_event->count);
1161 value = local64_xchg(&event->count, value);
1162 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001163
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
1177static 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 Zijlstra02ffdbc2009-11-20 22:19:50 +01001185 update_context_time(ctx);
1186
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001187 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 */
1214void perf_event_task_sched_out(struct task_struct *task,
Peter Zijlstra49f47432009-12-27 11:51:52 +01001215 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001216{
Peter Zijlstra49f47432009-12-27 11:51:52 +01001217 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001218 struct perf_event_context *ctx = task->perf_event_ctxp;
1219 struct perf_event_context *next_ctx;
1220 struct perf_event_context *parent;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001221 int do_switch = 1;
1222
Frederic Weisbeckere49a5bd2010-03-22 19:40:03 +01001223 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001224
1225 if (likely(!ctx || !cpuctx->task_ctx))
1226 return;
1227
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001228 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 Gleixnere625cce12009-11-17 18:02:06 +01001242 raw_spin_lock(&ctx->lock);
1243 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001244 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 Gleixnere625cce12009-11-17 18:02:06 +01001257 raw_spin_unlock(&next_ctx->lock);
1258 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001259 }
1260 rcu_read_unlock();
1261
1262 if (do_switch) {
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001263 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001264 cpuctx->task_ctx = NULL;
1265 }
1266}
1267
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001268static void task_ctx_sched_out(struct perf_event_context *ctx,
1269 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001270{
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 Weisbecker5b0311e2010-01-17 11:59:13 +01001279 ctx_sched_out(ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001280 cpuctx->task_ctx = NULL;
1281}
1282
1283/*
1284 * Called with IRQs disabled
1285 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001286static void __perf_event_task_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001287{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001288 task_ctx_sched_out(ctx, EVENT_ALL);
1289}
1290
1291/*
1292 * Called with IRQs disabled
1293 */
1294static 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 Molnarcdd6c482009-09-21 12:02:48 +02001298}
1299
1300static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001301ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001302 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001303{
1304 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001305
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001306 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1307 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001308 continue;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001309 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001310 continue;
1311
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001312 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001313 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001314
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 Weisbecker5b0311e2010-01-17 11:59:13 +01001324}
1325
1326static void
1327ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001328 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001329{
1330 struct perf_event *event;
1331 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001332
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001333 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 Molnarcdd6c482009-09-21 12:02:48 +02001336 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001337 /*
1338 * Listen to the 'cpu' scheduling filter constraint
1339 * of events:
1340 */
Peter Zijlstra6e377382010-02-11 13:21:58 +01001341 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001342 continue;
1343
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001344 if (group_can_go_on(event, cpuctx, can_add_hw))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001345 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001346 can_add_hw = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001347 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001348}
1349
1350static void
1351ctx_sched_in(struct perf_event_context *ctx,
1352 struct perf_cpu_context *cpuctx,
1353 enum event_type_t event_type)
1354{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001355 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 Zijlstra6e377382010-02-11 13:21:58 +01001369 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001370
1371 /* Then walk through the lower prio flexible groups */
1372 if (event_type & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001373 ctx_flexible_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001374
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001375 perf_enable();
1376 out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001377 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001378}
1379
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001380static 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 Weisbecker5b0311e2010-01-17 11:59:13 +01001388static 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 Molnarcdd6c482009-09-21 12:02:48 +02001401/*
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 Zijlstra49f47432009-12-27 11:51:52 +01001412void perf_event_task_sched_in(struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001413{
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001414 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1415 struct perf_event_context *ctx = task->perf_event_ctxp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001416
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001417 if (likely(!ctx))
1418 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001419
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001420 if (cpuctx->task_ctx == ctx)
1421 return;
1422
eranian@google.com9b33fa62010-03-10 22:26:05 -08001423 perf_disable();
1424
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001425 /*
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.com9b33fa62010-03-10 22:26:05 -08001437
1438 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001439}
1440
1441#define MAX_INTERRUPTS (~0ULL)
1442
1443static void perf_log_throttle(struct perf_event *event, int enable);
1444
Peter Zijlstraabd50712010-01-26 18:50:16 +01001445static 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) \
1473do { \
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 Zijlstraf6ab91a2010-06-04 15:18:01 +02001512 if (!divisor)
1513 return dividend;
1514
Peter Zijlstraabd50712010-01-26 18:50:16 +01001515 return div64_u64(dividend, divisor);
1516}
1517
Stephane Eraniand76a0812010-02-08 17:06:01 +02001518static 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
1526static 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 Zijlstraabd50712010-01-26 18:50:16 +01001534static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001535{
1536 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001537 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001538 s64 delta;
1539
Peter Zijlstraabd50712010-01-26 18:50:16 +01001540 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001541
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 Zijlstraabd50712010-01-26 18:50:16 +01001551
Peter Zijlstrae7850592010-05-21 14:43:08 +02001552 if (local64_read(&hwc->period_left) > 8*sample_period) {
Peter Zijlstraabd50712010-01-26 18:50:16 +01001553 perf_disable();
Stephane Eraniand76a0812010-02-08 17:06:01 +02001554 perf_event_stop(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001555 local64_set(&hwc->period_left, 0);
Stephane Eraniand76a0812010-02-08 17:06:01 +02001556 perf_event_start(event);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001557 perf_enable();
1558 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001559}
1560
1561static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
1562{
1563 struct perf_event *event;
1564 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001565 u64 interrupts, now;
1566 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001567
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001568 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11001569 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001570 if (event->state != PERF_EVENT_STATE_ACTIVE)
1571 continue;
1572
Peter Zijlstra5d27c232009-12-17 13:16:32 +01001573 if (event->cpu != -1 && event->cpu != smp_processor_id())
1574 continue;
1575
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001576 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 Zijlstrad4944a02010-03-08 13:51:20 +01001586 perf_disable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001587 event->pmu->unthrottle(event);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001588 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001589 }
1590
1591 if (!event->attr.freq || !event->attr.sample_freq)
1592 continue;
1593
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001594 perf_disable();
Peter Zijlstraabd50712010-01-26 18:50:16 +01001595 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001596 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001597 delta = now - hwc->freq_count_stamp;
1598 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001599
Peter Zijlstraabd50712010-01-26 18:50:16 +01001600 if (delta > 0)
1601 perf_adjust_period(event, TICK_NSEC, delta);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001602 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001603 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001604 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001605}
1606
1607/*
1608 * Round-robin a context's events:
1609 */
1610static void rotate_ctx(struct perf_event_context *ctx)
1611{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001612 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001613
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001614 /* Rotate the first entry last of non-pinned groups */
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001615 list_rotate_left(&ctx->flexible_groups);
1616
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001617 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001618}
1619
Peter Zijlstra49f47432009-12-27 11:51:52 +01001620void perf_event_task_tick(struct task_struct *curr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001621{
1622 struct perf_cpu_context *cpuctx;
1623 struct perf_event_context *ctx;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001624 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001625
1626 if (!atomic_read(&nr_events))
1627 return;
1628
Peter Zijlstra49f47432009-12-27 11:51:52 +01001629 cpuctx = &__get_cpu_var(perf_cpu_context);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001630 if (cpuctx->ctx.nr_events &&
1631 cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1632 rotate = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001633
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001634 ctx = curr->perf_event_ctxp;
1635 if (ctx && ctx->nr_events && ctx->nr_events != ctx->nr_active)
1636 rotate = 1;
Peter Zijlstra9717e6c2010-01-28 13:57:44 +01001637
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001638 perf_ctx_adjust_freq(&cpuctx->ctx);
1639 if (ctx)
1640 perf_ctx_adjust_freq(ctx);
1641
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001642 if (!rotate)
1643 return;
1644
1645 perf_disable();
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001646 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001647 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001648 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001649
1650 rotate_ctx(&cpuctx->ctx);
1651 if (ctx)
1652 rotate_ctx(ctx);
1653
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001654 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001655 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001656 task_ctx_sched_in(curr, EVENT_FLEXIBLE);
Peter Zijlstra9717e6c2010-01-28 13:57:44 +01001657 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001658}
1659
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001660static 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 Molnarcdd6c482009-09-21 12:02:48 +02001675/*
1676 * Enable all of a task's events that have been marked enable-on-exec.
1677 * This expects task == current.
1678 */
1679static 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 Weisbecker889ff012010-01-09 20:04:47 +01001685 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001686
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 Gleixnere625cce12009-11-17 18:02:06 +01001694 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001695
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001696 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 Molnarcdd6c482009-09-21 12:02:48 +02001706 }
1707
1708 /*
1709 * Unclone this context if we enabled any event.
1710 */
1711 if (enabled)
1712 unclone_ctx(ctx);
1713
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001714 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001715
Peter Zijlstra49f47432009-12-27 11:51:52 +01001716 perf_event_task_sched_in(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001717 out:
1718 local_irq_restore(flags);
1719}
1720
1721/*
1722 * Cross CPU call to read the hardware event
1723 */
1724static 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 Molnarcdd6c482009-09-21 12:02:48 +02001729
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 Gleixnere625cce12009-11-17 18:02:06 +01001740 raw_spin_lock(&ctx->lock);
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001741 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001742 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001743 raw_spin_unlock(&ctx->lock);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001744
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001745 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001746}
1747
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001748static inline u64 perf_event_count(struct perf_event *event)
1749{
Peter Zijlstrae7850592010-05-21 14:43:08 +02001750 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001751}
1752
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001753static 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 Zijlstra2b8988c2009-11-20 22:19:54 +01001763 struct perf_event_context *ctx = event->ctx;
1764 unsigned long flags;
1765
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001766 raw_spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001767 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001768 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001769 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001770 }
1771
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001772 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001773}
1774
1775/*
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001776 * Callchain support
1777 */
1778
1779struct callchain_cpus_entries {
1780 struct rcu_head rcu_head;
1781 struct perf_callchain_entry *cpu_entries[0];
1782};
1783
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001784static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001785static atomic_t nr_callchain_events;
1786static DEFINE_MUTEX(callchain_mutex);
1787struct 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
1800static 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
1813static 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
1822static 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 Weisbecker7ae07ea2010-08-14 20:45:13 +02001840 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001841
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
1853fail:
1854 for_each_possible_cpu(cpu)
1855 kfree(entries->cpu_entries[cpu]);
1856 kfree(entries);
1857
1858 return -ENOMEM;
1859}
1860
1861static 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();
1884exit:
1885 mutex_unlock(&callchain_mutex);
1886
1887 return err;
1888}
1889
1890static 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
1898static 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
1920static inline void put_recursion_context(int *recursion, int rctx)
1921{
1922 barrier();
1923 recursion[rctx]--;
1924}
1925
1926static 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
1944static void
1945put_callchain_entry(int rctx)
1946{
1947 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
1948}
1949
1950static 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
1979exit_put:
1980 put_callchain_entry(rctx);
1981
1982 return entry;
1983}
1984
1985/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001986 * Initialize the perf_event context in a task_struct:
1987 */
1988static void
1989__perf_event_init_context(struct perf_event_context *ctx,
1990 struct task_struct *task)
1991{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001992 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001993 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001994 INIT_LIST_HEAD(&ctx->pinned_groups);
1995 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001996 INIT_LIST_HEAD(&ctx->event_list);
1997 atomic_set(&ctx->refcount, 1);
1998 ctx->task = task;
1999}
2000
2001static 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 Zijlstraf4c41762009-12-16 17:55:54 +01002009 if (pid == -1 && cpu != -1) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002010 /* 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 Mackerras0f624e72009-12-15 19:40:32 +11002014 if (cpu < 0 || cpu >= nr_cpumask_bits)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002015 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 Russellf6325e32009-12-17 11:43:08 -06002022 if (!cpu_online(cpu))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002023 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 Gleixnere625cce12009-11-17 18:02:06 +01002060 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002061 }
2062
2063 if (!ctx) {
Xiao Guangrongaa5452d2009-12-09 11:28:13 +08002064 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002065 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 Zefan6fb29152009-10-15 11:21:42 +08002089static void perf_event_free_filter(struct perf_event *event);
2090
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002091static 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 Zefan6fb29152009-10-15 11:21:42 +08002098 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002099 kfree(event);
2100}
2101
2102static void perf_pending_sync(struct perf_event *event);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002103static void perf_buffer_put(struct perf_buffer *buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002104
2105static 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 Munson3af9e852010-05-18 15:30:49 +01002111 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002112 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 Weisbecker927c7a92010-07-01 16:20:36 +02002117 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2118 put_callchain_buffers();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002119 }
2120
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002121 if (event->buffer) {
2122 perf_buffer_put(event->buffer);
2123 event->buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002124 }
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 Venfb0459d2009-09-25 12:25:56 +02002133int perf_event_release_kernel(struct perf_event *event)
2134{
2135 struct perf_event_context *ctx = event->ctx;
2136
Peter Zijlstra050735b2010-05-11 11:51:53 +02002137 /*
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 Venfb0459d2009-09-25 12:25:56 +02002143 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002144 /*
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 Zijlstra050735b2010-05-11 11:51:53 +02002157 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002158 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002159 list_del_event(event, ctx);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002160 raw_spin_unlock_irq(&ctx->lock);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002161 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}
2172EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2173
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002174/*
2175 * Called when the last reference to the file is gone.
2176 */
2177static 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 Molnarcdd6c482009-09-21 12:02:48 +02002186static 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 Zijlstra59ed4462009-11-20 22:19:55 +01002211u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002212{
2213 struct perf_event *child;
2214 u64 total = 0;
2215
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002216 *enabled = 0;
2217 *running = 0;
2218
Peter Zijlstra6f105812009-11-20 22:19:56 +01002219 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002220 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002221 *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 Molnarcdd6c482009-09-21 12:02:48 +02002227 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002228 *enabled += child->total_time_enabled;
2229 *running += child->total_time_running;
2230 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002231 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002232
2233 return total;
2234}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002235EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002236
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002237static 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 Zijlstra6f105812009-11-20 22:19:56 +01002241 int n = 0, size = 0, ret = -EFAULT;
2242 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002243 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002244 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002245
Peter Zijlstra6f105812009-11-20 22:19:56 +01002246 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002247 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002248
2249 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002250 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 Zijlstraabf48682009-11-20 22:19:49 +01002254 values[n++] = count;
2255 if (read_format & PERF_FORMAT_ID)
2256 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002257
2258 size = n * sizeof(u64);
2259
2260 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01002261 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002262
Peter Zijlstra6f105812009-11-20 22:19:56 +01002263 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002264
2265 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01002266 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002267
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002268 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01002269 if (read_format & PERF_FORMAT_ID)
2270 values[n++] = primary_event_id(sub);
2271
2272 size = n * sizeof(u64);
2273
Stephane Eranian184d3da2009-11-23 21:40:49 -08002274 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01002275 ret = -EFAULT;
2276 goto unlock;
2277 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01002278
2279 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002280 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002281unlock:
2282 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002283
Peter Zijlstraabf48682009-11-20 22:19:49 +01002284 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002285}
2286
2287static int perf_event_read_one(struct perf_event *event,
2288 u64 read_format, char __user *buf)
2289{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002290 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002291 u64 values[4];
2292 int n = 0;
2293
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002294 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 Molnarcdd6c482009-09-21 12:02:48 +02002299 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 */
2311static ssize_t
2312perf_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 Molnarcdd6c482009-09-21 12:02:48 +02002329 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 Molnarcdd6c482009-09-21 12:02:48 +02002333
2334 return ret;
2335}
2336
2337static ssize_t
2338perf_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
2345static unsigned int perf_poll(struct file *file, poll_table *wait)
2346{
2347 struct perf_event *event = file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002348 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002349 unsigned int events = POLL_HUP;
2350
2351 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002352 buffer = rcu_dereference(event->buffer);
2353 if (buffer)
2354 events = atomic_xchg(&buffer->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002355 rcu_read_unlock();
2356
2357 poll_wait(file, &event->waitq, wait);
2358
2359 return events;
2360}
2361
2362static void perf_event_reset(struct perf_event *event)
2363{
2364 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002365 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002366 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 */
2375static 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
2388static 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
2405static 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 Gleixnere625cce12009-11-17 18:02:06 +01002422 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002423 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 }
2434unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002435 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002436
2437 return ret;
2438}
2439
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002440static const struct file_operations perf_fops;
2441
2442static 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
2459static int perf_event_set_output(struct perf_event *event,
2460 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08002461static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002462
2463static 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 Zijlstraac9721f2010-05-27 12:54:41 +02002487 {
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 Molnarcdd6c482009-09-21 12:02:48 +02002504
Li Zefan6fb29152009-10-15 11:21:42 +08002505 case PERF_EVENT_IOC_SET_FILTER:
2506 return perf_event_set_filter(event, (void __user *)arg);
2507
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002508 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
2520int perf_event_task_enable(void)
2521{
2522 struct perf_event *event;
2523
2524 mutex_lock(&current->perf_event_mutex);
2525 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2526 perf_event_for_each_child(event, perf_event_enable);
2527 mutex_unlock(&current->perf_event_mutex);
2528
2529 return 0;
2530}
2531
2532int perf_event_task_disable(void)
2533{
2534 struct perf_event *event;
2535
2536 mutex_lock(&current->perf_event_mutex);
2537 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2538 perf_event_for_each_child(event, perf_event_disable);
2539 mutex_unlock(&current->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
2548static 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 */
2561void perf_event_update_userpage(struct perf_event *event)
2562{
2563 struct perf_event_mmap_page *userpg;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002564 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002565
2566 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002567 buffer = rcu_dereference(event->buffer);
2568 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002569 goto unlock;
2570
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002571 userpg = buffer->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002572
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 Zijlstrab5e58792010-05-21 14:43:12 +02002581 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002582 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02002583 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002584
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();
2594unlock:
2595 rcu_read_unlock();
2596}
2597
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002598static unsigned long perf_data_size(struct perf_buffer *buffer);
2599
2600static void
2601perf_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 Zijlstra906010b2009-09-21 16:08:49 +02002617#ifndef CONFIG_PERF_USE_VMALLOC
2618
2619/*
2620 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2621 */
2622
2623static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002624perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002625{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002626 if (pgoff > buffer->nr_pages)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002627 return NULL;
2628
2629 if (pgoff == 0)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002630 return virt_to_page(buffer->user_page);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002631
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002632 return virt_to_page(buffer->data_pages[pgoff - 1]);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002633}
2634
Peter Zijlstraa19d35c2010-05-17 18:48:00 +02002635static 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 Zijlstraca5135e2010-05-28 19:33:23 +02002648static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002649perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002650{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002651 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002652 unsigned long size;
2653 int i;
2654
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002655 size = sizeof(struct perf_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002656 size += nr_pages * sizeof(void *);
2657
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002658 buffer = kzalloc(size, GFP_KERNEL);
2659 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002660 goto fail;
2661
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002662 buffer->user_page = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002663 if (!buffer->user_page)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002664 goto fail_user_page;
2665
2666 for (i = 0; i < nr_pages; i++) {
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002667 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002668 if (!buffer->data_pages[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002669 goto fail_data_pages;
2670 }
2671
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002672 buffer->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002673
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002674 perf_buffer_init(buffer, watermark, flags);
2675
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002676 return buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002677
2678fail_data_pages:
2679 for (i--; i >= 0; i--)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002680 free_page((unsigned long)buffer->data_pages[i]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002681
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002682 free_page((unsigned long)buffer->user_page);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002683
2684fail_user_page:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002685 kfree(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002686
2687fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02002688 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002689}
2690
2691static 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 Zijlstraca5135e2010-05-28 19:33:23 +02002699static void perf_buffer_free(struct perf_buffer *buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002700{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002701 int i;
2702
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002703 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 Zijlstra906010b2009-09-21 16:08:49 +02002707}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002708
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002709static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002710{
2711 return 0;
2712}
2713
Peter Zijlstra906010b2009-09-21 16:08:49 +02002714#else
2715
2716/*
2717 * Back perf_mmap() with vmalloc memory.
2718 *
2719 * Required for architectures that have d-cache aliasing issues.
2720 */
2721
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002722static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002723{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002724 return buffer->page_order;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002725}
2726
Peter Zijlstra906010b2009-09-21 16:08:49 +02002727static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002728perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002729{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002730 if (pgoff > (1UL << page_order(buffer)))
Peter Zijlstra906010b2009-09-21 16:08:49 +02002731 return NULL;
2732
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002733 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002734}
2735
2736static void perf_mmap_unmark_page(void *addr)
2737{
2738 struct page *page = vmalloc_to_page(addr);
2739
2740 page->mapping = NULL;
2741}
2742
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002743static void perf_buffer_free_work(struct work_struct *work)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002744{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002745 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002746 void *base;
2747 int i, nr;
2748
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002749 buffer = container_of(work, struct perf_buffer, work);
2750 nr = 1 << page_order(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002751
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002752 base = buffer->user_page;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002753 for (i = 0; i < nr + 1; i++)
2754 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2755
2756 vfree(base);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002757 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002758}
2759
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002760static void perf_buffer_free(struct perf_buffer *buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002761{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002762 schedule_work(&buffer->work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002763}
2764
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002765static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002766perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002767{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002768 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002769 unsigned long size;
2770 void *all_buf;
2771
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002772 size = sizeof(struct perf_buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002773 size += sizeof(void *);
2774
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002775 buffer = kzalloc(size, GFP_KERNEL);
2776 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002777 goto fail;
2778
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002779 INIT_WORK(&buffer->work, perf_buffer_free_work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002780
2781 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2782 if (!all_buf)
2783 goto fail_all_buf;
2784
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002785 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 Zijlstra906010b2009-09-21 16:08:49 +02002789
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002790 perf_buffer_init(buffer, watermark, flags);
2791
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002792 return buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002793
2794fail_all_buf:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002795 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002796
2797fail:
2798 return NULL;
2799}
2800
2801#endif
2802
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002803static unsigned long perf_data_size(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002804{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002805 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002806}
2807
Peter Zijlstra906010b2009-09-21 16:08:49 +02002808static 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 Zijlstraca5135e2010-05-28 19:33:23 +02002811 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002812 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 Zijlstraca5135e2010-05-28 19:33:23 +02002821 buffer = rcu_dereference(event->buffer);
2822 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002823 goto unlock;
2824
2825 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2826 goto unlock;
2827
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002828 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002829 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;
2837unlock:
2838 rcu_read_unlock();
2839
2840 return ret;
2841}
2842
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002843static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002844{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002845 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002846
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002847 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
2848 perf_buffer_free(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002849}
2850
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002851static struct perf_buffer *perf_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002852{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002853 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002854
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002855 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002856 buffer = rcu_dereference(event->buffer);
2857 if (buffer) {
2858 if (!atomic_inc_not_zero(&buffer->refcount))
2859 buffer = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002860 }
2861 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002862
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002863 return buffer;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002864}
2865
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002866static void perf_buffer_put(struct perf_buffer *buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002867{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002868 if (!atomic_dec_and_test(&buffer->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002869 return;
2870
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002871 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002872}
2873
2874static 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
2881static void perf_mmap_close(struct vm_area_struct *vma)
2882{
2883 struct perf_event *event = vma->vm_file->private_data;
2884
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002885 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002886 unsigned long size = perf_data_size(event->buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002887 struct user_struct *user = event->mmap_user;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002888 struct perf_buffer *buffer = event->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002889
Peter Zijlstra906010b2009-09-21 16:08:49 +02002890 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002891 vma->vm_mm->locked_vm -= event->mmap_locked;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002892 rcu_assign_pointer(event->buffer, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002893 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002894
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002895 perf_buffer_put(buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002896 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002897 }
2898}
2899
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002900static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002901 .open = perf_mmap_open,
2902 .close = perf_mmap_close,
2903 .fault = perf_mmap_fault,
2904 .page_mkwrite = perf_mmap_fault,
2905};
2906
2907static 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 Zijlstraca5135e2010-05-28 19:33:23 +02002913 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002914 unsigned long vma_size;
2915 unsigned long nr_pages;
2916 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002917 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002918
Peter Zijlstrac7920612010-05-18 10:33:24 +02002919 /*
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 Molnarcdd6c482009-09-21 12:02:48 +02002927 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 Zijlstraca5135e2010-05-28 19:33:23 +02002934 * If we have buffer pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002935 * 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 Zijlstraca5135e2010-05-28 19:33:23 +02002948 if (event->buffer) {
2949 if (event->buffer->nr_pages == nr_pages)
2950 atomic_inc(&event->buffer->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002951 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002952 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 Slaby78d7d402010-03-05 13:42:54 -08002970 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002971 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 Zijlstraca5135e2010-05-28 19:33:23 +02002980 WARN_ON(event->buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002981
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002982 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 Zijlstraca5135e2010-05-28 19:33:23 +02002987 if (!buffer) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002988 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002989 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002990 }
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002991 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002992
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002993 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 Molnarcdd6c482009-09-21 12:02:48 +02002998unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002999 if (!ret)
3000 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003001 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
3009static 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
3025static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003026 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003027 .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
3043void 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
3062static 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
3080static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
3081 PENDING_TAIL,
3082};
3083
3084static 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
3105static 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
3133static 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
3151static void perf_pending_sync(struct perf_event *event)
3152{
3153 wait_event(event->waitq, perf_not_pending(event));
3154}
3155
3156void perf_event_do_pending(void)
3157{
3158 __perf_pending_run();
3159}
3160
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003161/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003162 * 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 */
3166struct perf_guest_info_callbacks *perf_guest_cbs;
3167
3168int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3169{
3170 perf_guest_cbs = cbs;
3171 return 0;
3172}
3173EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3174
3175int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3176{
3177 perf_guest_cbs = NULL;
3178 return 0;
3179}
3180EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3181
3182/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003183 * Output
3184 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003185static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003186 unsigned long offset, unsigned long head)
3187{
3188 unsigned long mask;
3189
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003190 if (!buffer->writable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003191 return true;
3192
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003193 mask = perf_data_size(buffer) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003194
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
3204static void perf_output_wakeup(struct perf_output_handle *handle)
3205{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003206 atomic_set(&handle->buffer->poll, POLL_IN);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003207
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 Molnarcdd6c482009-09-21 12:02:48 +02003217 * We need to ensure a later event_id doesn't publish a head when a former
Peter Zijlstraef607772010-05-18 10:50:41 +02003218 * event isn't done writing. However since we need to deal with NMIs we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003219 * cannot fully serialize things.
3220 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003221 * We only publish the head (and generate a wakeup) when the outer-most
Peter Zijlstraef607772010-05-18 10:50:41 +02003222 * event completes.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003223 */
Peter Zijlstraef607772010-05-18 10:50:41 +02003224static void perf_output_get_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003225{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003226 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003227
Peter Zijlstraef607772010-05-18 10:50:41 +02003228 preempt_disable();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003229 local_inc(&buffer->nest);
3230 handle->wakeup = local_read(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003231}
3232
Peter Zijlstraef607772010-05-18 10:50:41 +02003233static void perf_output_put_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003234{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003235 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003236 unsigned long head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003237
3238again:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003239 head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003240
3241 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003242 * IRQ/NMI can happen here, which means we can miss a head update.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003243 */
3244
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003245 if (!local_dec_and_test(&buffer->nest))
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003246 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003247
3248 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003249 * Publish the known good head. Rely on the full barrier implied
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003250 * by atomic_dec_and_test() order the buffer->head read and this
Peter Zijlstraef607772010-05-18 10:50:41 +02003251 * write.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003252 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003253 buffer->user_page->data_head = head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003254
Peter Zijlstraef607772010-05-18 10:50:41 +02003255 /*
3256 * Now check if we missed an update, rely on the (compiler)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003257 * barrier in atomic_dec_and_test() to re-read buffer->head.
Peter Zijlstraef607772010-05-18 10:50:41 +02003258 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003259 if (unlikely(head != local_read(&buffer->head))) {
3260 local_inc(&buffer->nest);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003261 goto again;
3262 }
3263
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003264 if (handle->wakeup != local_read(&buffer->wakeup))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003265 perf_output_wakeup(handle);
Peter Zijlstraef607772010-05-18 10:50:41 +02003266
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003267 out:
Peter Zijlstraef607772010-05-18 10:50:41 +02003268 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003269}
3270
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003271__always_inline void perf_output_copy(struct perf_output_handle *handle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003272 const void *buf, unsigned int len)
3273{
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003274 do {
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003275 unsigned long size = min_t(unsigned long, handle->size, len);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003276
3277 memcpy(handle->addr, buf, size);
3278
3279 len -= size;
3280 handle->addr += size;
Frederic Weisbecker74048f82010-05-27 21:34:58 +02003281 buf += size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003282 handle->size -= size;
3283 if (!handle->size) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003284 struct perf_buffer *buffer = handle->buffer;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003285
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003286 handle->page++;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003287 handle->page &= buffer->nr_pages - 1;
3288 handle->addr = buffer->data_pages[handle->page];
3289 handle->size = PAGE_SIZE << page_order(buffer);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003290 }
3291 } while (len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003292}
3293
3294int perf_output_begin(struct perf_output_handle *handle,
3295 struct perf_event *event, unsigned int size,
3296 int nmi, int sample)
3297{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003298 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003299 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 Zijlstraca5135e2010-05-28 19:33:23 +02003314 buffer = rcu_dereference(event->buffer);
3315 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003316 goto out;
3317
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003318 handle->buffer = buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003319 handle->event = event;
3320 handle->nmi = nmi;
3321 handle->sample = sample;
3322
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003323 if (!buffer->nr_pages)
Stephane Eranian00d1d0b2010-05-17 12:46:01 +02003324 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003325
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003326 have_lost = local_read(&buffer->lost);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003327 if (have_lost)
3328 size += sizeof(lost_event);
3329
Peter Zijlstraef607772010-05-18 10:50:41 +02003330 perf_output_get_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003331
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 Zijlstraca5135e2010-05-28 19:33:23 +02003338 tail = ACCESS_ONCE(buffer->user_page->data_tail);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003339 smp_rmb();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003340 offset = head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003341 head += size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003342 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003343 goto fail;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003344 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003345
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003346 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3347 local_add(buffer->watermark, &buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003348
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003349 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 Zijlstra5d967a82010-05-20 16:46:39 +02003353 handle->addr += handle->size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003354 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003355
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003356 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 Zijlstraca5135e2010-05-28 19:33:23 +02003361 lost_event.lost = local_xchg(&buffer->lost, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003362
3363 perf_output_put(handle, lost_event);
3364 }
3365
3366 return 0;
3367
3368fail:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003369 local_inc(&buffer->lost);
Peter Zijlstraef607772010-05-18 10:50:41 +02003370 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003371out:
3372 rcu_read_unlock();
3373
3374 return -ENOSPC;
3375}
3376
3377void perf_output_end(struct perf_output_handle *handle)
3378{
3379 struct perf_event *event = handle->event;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003380 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003381
3382 int wakeup_events = event->attr.wakeup_events;
3383
3384 if (handle->sample && wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003385 int events = local_inc_return(&buffer->events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003386 if (events >= wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003387 local_sub(wakeup_events, &buffer->events);
3388 local_inc(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003389 }
3390 }
3391
Peter Zijlstraef607772010-05-18 10:50:41 +02003392 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003393 rcu_read_unlock();
3394}
3395
3396static 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
3407static 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
3418static 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 Zijlstrab5e58792010-05-21 14:43:12 +02003425 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003426 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 */
3443static 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 Zijlstrab5e58792010-05-21 14:43:12 +02003462 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003463 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 Zijlstrab5e58792010-05-21 14:43:12 +02003474 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003475 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
3482static 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
3491void 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
3561void 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
3648static 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 Weisbecker927c7a92010-07-01 16:20:36 +02003655 /* protect the callchain buffers */
3656 rcu_read_lock();
3657
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003658 perf_prepare_sample(&header, data, event, regs);
3659
3660 if (perf_output_begin(&handle, event, header.size, nmi, 1))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003661 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003662
3663 perf_output_sample(&handle, &header, data, event);
3664
3665 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003666
3667exit:
3668 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003669}
3670
3671/*
3672 * read event_id
3673 */
3674
3675struct perf_read_event {
3676 struct perf_event_header header;
3677
3678 u32 pid;
3679 u32 tid;
3680};
3681
3682static void
3683perf_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 Munson3af9e852010-05-18 15:30:49 +01003711 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003712 */
3713
3714struct 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
3729static void perf_event_task_output(struct perf_event *event,
3730 struct perf_task_event *task_event)
3731{
3732 struct perf_output_handle handle;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003733 struct task_struct *task = task_event->task;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01003734 int size, ret;
3735
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003736 size = task_event->event_id.header.size;
3737 ret = perf_output_begin(&handle, event, size, 0, 0);
3738
Peter Zijlstraef607772010-05-18 10:50:41 +02003739 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003740 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 Molnarcdd6c482009-09-21 12:02:48 +02003748 perf_output_put(&handle, task_event->event_id);
3749
3750 perf_output_end(&handle);
3751}
3752
3753static int perf_event_task_match(struct perf_event *event)
3754{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003755 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003756 return 0;
3757
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003758 if (event->cpu != -1 && event->cpu != smp_processor_id())
3759 return 0;
3760
Eric B Munson3af9e852010-05-18 15:30:49 +01003761 if (event->attr.comm || event->attr.mmap ||
3762 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003763 return 1;
3764
3765 return 0;
3766}
3767
3768static 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 Molnarcdd6c482009-09-21 12:02:48 +02003773 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 Molnarcdd6c482009-09-21 12:02:48 +02003777}
3778
3779static 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 Zijlstrad6ff86c2009-11-20 22:19:46 +01003784 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003785 cpuctx = &get_cpu_var(perf_cpu_context);
3786 perf_event_task_ctx(&cpuctx->ctx, task_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003787 if (!ctx)
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003788 ctx = rcu_dereference(current->perf_event_ctxp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003789 if (ctx)
3790 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003791 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003792 rcu_read_unlock();
3793}
3794
3795static 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 Zijlstra6f93d0a2010-02-14 11:12:04 +01003819 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003820 },
3821 };
3822
3823 perf_event_task_event(&task_event);
3824}
3825
3826void perf_event_fork(struct task_struct *task)
3827{
3828 perf_event_task(task, NULL, 1);
3829}
3830
3831/*
3832 * comm tracking
3833 */
3834
3835struct 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
3848static 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
3867static int perf_event_comm_match(struct perf_event *event)
3868{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003869 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003870 return 0;
3871
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003872 if (event->cpu != -1 && event->cpu != smp_processor_id())
3873 return 0;
3874
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003875 if (event->attr.comm)
3876 return 1;
3877
3878 return 0;
3879}
3880
3881static 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 Molnarcdd6c482009-09-21 12:02:48 +02003886 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 Molnarcdd6c482009-09-21 12:02:48 +02003890}
3891
3892static 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émeth96b02d72009-11-21 23:10:15 +01003900 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003901 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 Zijlstraf6595f32009-11-20 22:19:47 +01003908 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003909 cpuctx = &get_cpu_var(perf_cpu_context);
3910 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003911 ctx = rcu_dereference(current->perf_event_ctxp);
3912 if (ctx)
3913 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003914 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003915 rcu_read_unlock();
3916}
3917
3918void 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
3950struct 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
3967static 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
3986static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01003987 struct perf_mmap_event *mmap_event,
3988 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003989{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003990 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003991 return 0;
3992
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003993 if (event->cpu != -1 && event->cpu != smp_processor_id())
3994 return 0;
3995
Eric B Munson3af9e852010-05-18 15:30:49 +01003996 if ((!executable && event->attr.mmap_data) ||
3997 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003998 return 1;
3999
4000 return 0;
4001}
4002
4003static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004004 struct perf_mmap_event *mmap_event,
4005 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004006{
4007 struct perf_event *event;
4008
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004009 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004010 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004011 perf_event_mmap_output(event, mmap_event);
4012 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004013}
4014
4015static 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 Munson3af9e852010-05-18 15:30:49 +01004054 } 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 Molnarcdd6c482009-09-21 12:02:48 +02004062 }
4063
4064 name = strncpy(tmp, "//anon", sizeof(tmp));
4065 goto got_name;
4066 }
4067
4068got_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 Zijlstraf6d9dd22009-11-20 22:19:48 +01004076 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004077 cpuctx = &get_cpu_var(perf_cpu_context);
Eric B Munson3af9e852010-05-18 15:30:49 +01004078 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event, vma->vm_flags & VM_EXEC);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004079 ctx = rcu_dereference(current->perf_event_ctxp);
4080 if (ctx)
Eric B Munson3af9e852010-05-18 15:30:49 +01004081 perf_event_mmap_ctx(ctx, mmap_event, vma->vm_flags & VM_EXEC);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004082 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004083 rcu_read_unlock();
4084
4085 kfree(buf);
4086}
4087
Eric B Munson3af9e852010-05-18 15:30:49 +01004088void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004089{
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, Yanmin39447b32010-04-19 13:32:41 +08004102 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004103 /* .size */
4104 },
4105 /* .pid */
4106 /* .tid */
4107 .start = vma->vm_start,
4108 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004109 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004110 },
4111 };
4112
4113 perf_event_mmap_event(&mmap_event);
4114}
4115
4116/*
4117 * IRQ throttle logging
4118 */
4119
4120static 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
4156static 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 Zijlstraabd50712010-01-26 18:50:16 +01004189 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004190
Peter Zijlstraabd50712010-01-26 18:50:16 +01004191 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004192
Peter Zijlstraabd50712010-01-26 18:50:16 +01004193 if (delta > 0 && delta < 2*TICK_NSEC)
4194 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004195 }
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 Zijlstra453f19e2009-11-20 22:19:43 +01004214 if (event->overflow_handler)
4215 event->overflow_handler(event, nmi, data, regs);
4216 else
4217 perf_event_output(event, nmi, data, regs);
4218
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004219 return ret;
4220}
4221
4222int 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
4240static 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
4249again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004250 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004251 if (val < 0)
4252 return 0;
4253
4254 nr = div64_u64(period + val, period);
4255 offset = nr * period;
4256 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004257 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004258 goto again;
4259
4260 return nr;
4261}
4262
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004263static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004264 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 Molnarcdd6c482009-09-21 12:02:48 +02004269
4270 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004271 if (!overflow)
4272 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004273
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 Molnarcdd6c482009-09-21 12:02:48 +02004290static 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 Zijlstrae7850592010-05-21 14:43:08 +02004296 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004297
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004298 if (!regs)
4299 return;
4300
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004301 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 Zijlstrae7850592010-05-21 14:43:08 +02004307 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004308 return;
4309
4310 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004311}
4312
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004313static 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 Molnarcdd6c482009-09-21 12:02:48 +02004327static int perf_swevent_match(struct perf_event *event,
4328 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004329 u32 event_id,
4330 struct perf_sample_data *data,
4331 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004332{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004333 if (event->attr.type != type)
4334 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004335
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004336 if (event->attr.config != event_id)
4337 return 0;
4338
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004339 if (perf_exclude_event(event, regs))
4340 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004341
4342 return 1;
4343}
4344
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004345static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004346{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004347 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004348
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004349 return hash_64(val, SWEVENT_HLIST_BITS);
4350}
4351
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004352static inline struct hlist_head *
4353__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004354{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004355 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004356
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004357 return &hlist->heads[hash];
4358}
4359
4360/* For the read side: events when they trigger */
4361static inline struct hlist_head *
4362find_swevent_head_rcu(struct perf_cpu_context *ctx, u64 type, u32 event_id)
4363{
4364 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004365
4366 hlist = rcu_dereference(ctx->swevent_hlist);
4367 if (!hlist)
4368 return NULL;
4369
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004370 return __find_swevent_head(hlist, type, event_id);
4371}
4372
4373/* For the event head insertion and removal in the hlist */
4374static inline struct hlist_head *
4375find_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 Weisbecker76e1d902010-04-05 15:35:57 +02004392}
4393
4394static 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 Weisbecker49f135e2010-05-20 10:17:46 +02004408 head = find_swevent_head_rcu(cpuctx, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004409
4410 if (!head)
4411 goto end;
4412
4413 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004414 if (perf_swevent_match(event, type, event_id, data, regs))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004415 perf_swevent_add(event, nr, nmi, data, regs);
4416 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004417end:
4418 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004419}
4420
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004421int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004422{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004423 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004424
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004425 return get_recursion_context(cpuctx->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004426}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004427EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004428
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004429void inline perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004430{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004431 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004432
4433 put_recursion_context(cpuctx->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004434}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004435
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004436void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4437 struct pt_regs *regs, u64 addr)
4438{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004439 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004440 int rctx;
4441
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004442 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004443 rctx = perf_swevent_get_recursion_context();
4444 if (rctx < 0)
4445 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004446
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004447 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004448
4449 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004450
4451 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004452 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004453}
4454
4455static void perf_swevent_read(struct perf_event *event)
4456{
4457}
4458
4459static int perf_swevent_enable(struct perf_event *event)
4460{
4461 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004462 struct perf_cpu_context *cpuctx;
4463 struct hlist_head *head;
4464
4465 cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004466
4467 if (hwc->sample_period) {
4468 hwc->last_period = hwc->sample_period;
4469 perf_swevent_set_period(event);
4470 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004471
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004472 head = find_swevent_head(cpuctx, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004473 if (WARN_ON_ONCE(!head))
4474 return -EINVAL;
4475
4476 hlist_add_head_rcu(&event->hlist_entry, head);
4477
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004478 return 0;
4479}
4480
4481static void perf_swevent_disable(struct perf_event *event)
4482{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004483 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004484}
4485
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004486static void perf_swevent_void(struct perf_event *event)
4487{
4488}
4489
4490static int perf_swevent_int(struct perf_event *event)
4491{
4492 return 0;
4493}
4494
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004495/* Deref the hlist from the update side */
4496static inline struct swevent_hlist *
4497swevent_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 Weisbecker76e1d902010-04-05 15:35:57 +02004503static 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
4511static void swevent_hlist_release(struct perf_cpu_context *cpuctx)
4512{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004513 struct swevent_hlist *hlist = swevent_hlist_deref(cpuctx);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004514
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004515 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004516 return;
4517
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004518 rcu_assign_pointer(cpuctx->swevent_hlist, NULL);
4519 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4520}
4521
4522static 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
4534static 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
4547static 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 Weisbecker49f135e2010-05-20 10:17:46 +02004554 if (!swevent_hlist_deref(cpuctx) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004555 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
4571static 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 Zijlstrab0a873e2010-06-11 13:35:08 +02004601atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004602
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004603static 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
4613static 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
4646static struct pmu perf_swevent = {
4647 .event_init = perf_swevent_init,
4648 .enable = perf_swevent_enable,
4649 .disable = perf_swevent_disable,
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004650 .start = perf_swevent_int,
4651 .stop = perf_swevent_void,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004652 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004653 .unthrottle = perf_swevent_void, /* hwc->interrupts already reset */
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004654};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004655
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004656#ifdef CONFIG_EVENT_TRACING
4657
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004658static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004659 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 Zijlstra1c024eca2010-05-19 14:02:22 +02004668static int perf_tp_event_match(struct perf_event *event,
4669 struct perf_sample_data *data,
4670 struct pt_regs *regs)
4671{
Peter Zijlstra580d6072010-05-20 20:54:31 +02004672 /*
4673 * All tracepoints are from kernel-space.
4674 */
4675 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004676 return 0;
4677
4678 if (!perf_tp_filter_match(event, data))
4679 return 0;
4680
4681 return 1;
4682}
4683
4684void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004685 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004686{
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 Zijlstra1c024eca2010-05-19 14:02:22 +02004699 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 Zijlstraecc55f82010-05-21 15:11:34 +02004703
4704 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004705}
4706EXPORT_SYMBOL_GPL(perf_tp_event);
4707
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004708static void tp_perf_event_destroy(struct perf_event *event)
4709{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004710 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004711}
4712
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004713static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004714{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004715 int err;
4716
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004717 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4718 return -ENOENT;
4719
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004720 /*
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 Zijlstrab0a873e2010-06-11 13:35:08 +02004727 return -EPERM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004728
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004729 err = perf_trace_init(event);
4730 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004731 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004732
4733 event->destroy = tp_perf_event_destroy;
4734
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004735 return 0;
4736}
4737
4738static 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
4748static inline void perf_tp_register(void)
4749{
4750 perf_pmu_register(&perf_tracepoint);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004751}
Li Zefan6fb29152009-10-15 11:21:42 +08004752
4753static 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
4771static void perf_event_free_filter(struct perf_event *event)
4772{
4773 ftrace_profile_free_filter(event);
4774}
4775
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004776#else
Li Zefan6fb29152009-10-15 11:21:42 +08004777
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004778static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004779{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004780}
Li Zefan6fb29152009-10-15 11:21:42 +08004781
4782static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4783{
4784 return -ENOENT;
4785}
4786
4787static void perf_event_free_filter(struct perf_event *event)
4788{
4789}
4790
Li Zefan07b139c2009-12-21 14:27:35 +08004791#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004792
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004793#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004794void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004795{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004796 struct perf_sample_data sample;
4797 struct pt_regs *regs = data;
4798
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004799 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004800
4801 if (!perf_exclude_event(bp, regs))
4802 perf_swevent_add(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004803}
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004804#endif
4805
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004806/*
4807 * hrtimer based swevent callback
4808 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004809
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004810static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004811{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004812 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 Molnarcdd6c482009-09-21 12:02:48 +02004817
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004818 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4819 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004820
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004821 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 Molnarcdd6c482009-09-21 12:02:48 +02004835}
4836
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004837static 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
4861static 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
4877static 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
4888static 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
4899static void cpu_clock_event_disable(struct perf_event *event)
4900{
4901 perf_swevent_cancel_hrtimer(event);
4902 cpu_clock_event_update(event);
4903}
4904
4905static void cpu_clock_event_read(struct perf_event *event)
4906{
4907 cpu_clock_event_update(event);
4908}
4909
4910static 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
4921static 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
4932static 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
4942static 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
4956static 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
4963static 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
4979static 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
4990static 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
4997static LIST_HEAD(pmus);
4998static DEFINE_MUTEX(pmus_lock);
4999static struct srcu_struct pmus_srcu;
5000
5001int 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
5010void 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
5019struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005020{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005021 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005022 int idx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005023
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005024 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 Molnarcdd6c482009-09-21 12:02:48 +02005032 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005033 }
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005034 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005035
5036 return pmu;
5037}
5038
5039/*
5040 * Allocate and initialize a event structure
5041 */
5042static struct perf_event *
5043perf_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 Weisbeckerb326e952009-12-05 09:44:31 +01005048 perf_overflow_handler_t overflow_handler,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005049 gfp_t gfpflags)
5050{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005051 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005052 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 Weisbeckerb326e952009-12-05 09:44:31 +01005091 if (!overflow_handler && parent_event)
5092 overflow_handler = parent_event->overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005093
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005094 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005095
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005096 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 Zijlstrae7850592010-05-21 14:43:08 +02005107 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005108
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 Zijlstrab0a873e2010-06-11 13:35:08 +02005115 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005116
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005117done:
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 Munson3af9e852010-05-18 15:30:49 +01005135 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005136 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 Weisbecker927c7a92010-07-01 16:20:36 +02005141 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 Molnarcdd6c482009-09-21 12:02:48 +02005148 }
5149
5150 return event;
5151}
5152
5153static 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 Salgaonkarcd757642010-01-30 10:25:18 +05305215 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005216 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
5224out:
5225 return ret;
5226
5227err_size:
5228 put_user(sizeof(*attr), &uattr->size);
5229 ret = -E2BIG;
5230 goto out;
5231}
5232
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005233static int
5234perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005235{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005236 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005237 int ret = -EINVAL;
5238
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005239 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005240 goto set;
5241
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005242 /* don't allow circular references */
5243 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005244 goto out;
5245
Peter Zijlstra0f139302010-05-20 14:35:15 +02005246 /*
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 Molnarcdd6c482009-09-21 12:02:48 +02005258set:
5259 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005260 /* Can't redirect output if we've got an active mmap() */
5261 if (atomic_read(&event->mmap_count))
5262 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005263
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005264 if (output_event) {
5265 /* get the buffer we want to redirect to */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005266 buffer = perf_buffer_get(output_event);
5267 if (!buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005268 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005269 }
5270
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005271 old_buffer = event->buffer;
5272 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005273 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005274unlock:
5275 mutex_unlock(&event->mmap_mutex);
5276
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005277 if (old_buffer)
5278 perf_buffer_put(old_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005279out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005280 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 */
5291SYSCALL_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 Zijlstraac9721f2010-05-27 12:54:41 +02005295 struct perf_event *event, *group_leader = NULL, *output_event = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005296 struct perf_event_attr attr;
5297 struct perf_event_context *ctx;
5298 struct file *event_file = NULL;
5299 struct file *group_file = NULL;
Al Viroea635c62010-05-26 17:40:29 -04005300 int event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005301 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005302 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 Viroea635c62010-05-26 17:40:29 -04005322 event_fd = get_unused_fd_flags(O_RDWR);
5323 if (event_fd < 0)
5324 return event_fd;
5325
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005326 /*
5327 * Get the target context (task or percpu):
5328 */
5329 ctx = find_get_context(pid, cpu);
Al Viroea635c62010-05-26 17:40:29 -04005330 if (IS_ERR(ctx)) {
5331 err = PTR_ERR(ctx);
5332 goto err_fd;
5333 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005334
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005335 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 Molnarcdd6c482009-09-21 12:02:48 +02005348 /*
5349 * Look up the group leader (we will attach this event to it):
5350 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005351 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005352 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005353
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005354 /*
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 Weisbecker97eaf532009-10-18 15:33:50 +02005374 NULL, NULL, GFP_KERNEL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005375 if (IS_ERR(event)) {
5376 err = PTR_ERR(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005377 goto err_put_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005378 }
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 Molnarcdd6c482009-09-21 12:02:48 +02005385
Al Viroea635c62010-05-26 17:40:29 -04005386 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 Molnarcdd6c482009-09-21 12:02:48 +02005389 goto err_free_put_context;
Al Viroea635c62010-05-26 17:40:29 -04005390 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005391
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005392 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(&current->perf_event_mutex);
5402 list_add_tail(&event->owner_entry, &current->perf_event_list);
5403 mutex_unlock(&current->perf_event_mutex);
5404
Peter Zijlstra8a495422010-05-27 15:47:49 +02005405 /*
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 Molnarcdd6c482009-09-21 12:02:48 +02005411 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04005412 fd_install(event_fd, event_file);
5413 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005414
Al Viroea635c62010-05-26 17:40:29 -04005415err_free_put_context:
5416 free_event(event);
5417err_put_context:
5418 fput_light(group_file, fput_needed);
5419 put_ctx(ctx);
5420err_fd:
5421 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005422 return err;
5423}
5424
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005425/**
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 */
5432struct perf_event *
5433perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005434 pid_t pid,
5435 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005436{
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 Weisbeckerc6567f62009-11-26 05:35:41 +01005446 if (IS_ERR(ctx)) {
5447 err = PTR_ERR(ctx);
5448 goto err_exit;
5449 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005450
5451 event = perf_event_alloc(attr, cpu, ctx, NULL,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005452 NULL, overflow_handler, GFP_KERNEL);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005453 if (IS_ERR(event)) {
5454 err = PTR_ERR(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005455 goto err_put_context;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005456 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005457
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(&current->perf_event_mutex);
5468 list_add_tail(&event->owner_entry, &current->perf_event_list);
5469 mutex_unlock(&current->perf_event_mutex);
5470
5471 return event;
5472
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005473 err_put_context:
5474 put_ctx(ctx);
5475 err_exit:
5476 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005477}
5478EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5479
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005480/*
5481 * inherit a event from parent task to child task:
5482 */
5483static struct perf_event *
5484inherit_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 Weisbecker97eaf532009-10-18 15:33:50 +02005505 NULL, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005506 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 Zijlstra75c9f322010-01-29 09:04:26 +01005520 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 Zijlstrae7850592010-05-21 14:43:08 +02005527 local64_set(&hwc->period_left, sample_period);
Peter Zijlstra75c9f322010-01-29 09:04:26 +01005528 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005529
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005530 child_event->overflow_handler = parent_event->overflow_handler;
5531
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005532 /*
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
5556static 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
5579static 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 Zijlstrab5e58792010-05-21 14:43:12 +02005588 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005589
5590 /*
5591 * Add back the child's count to the parent's count:
5592 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02005593 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005594 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
5614static 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 Molnarcdd6c482009-09-21 12:02:48 +02005621 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 */
5638void 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 Gleixnere625cce12009-11-17 18:02:06 +01005664 raw_spin_lock(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005665 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 Zijlstra5e942bb2009-11-23 11:37:26 +01005672 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01005673 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005674
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 Zijlstraa0507c82010-05-06 15:42:53 +02005693 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005694
5695again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005696 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 Molnarcdd6c482009-09-21 12:02:48 +02005701 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 Weisbecker889ff012010-01-09 20:04:47 +01005709 if (!list_empty(&child_ctx->pinned_groups) ||
5710 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005711 goto again;
5712
5713 mutex_unlock(&child_ctx->mutex);
5714
5715 put_ctx(child_ctx);
5716}
5717
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005718static 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 Zijlstra8a495422010-05-27 15:47:49 +02005732 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005733 list_del_event(event, ctx);
5734 free_event(event);
5735}
5736
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005737/*
5738 * free an unexposed, unused context as created by inheritance by
5739 * init_task below, used by fork() in case of fail.
5740 */
5741void 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);
5750again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005751 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5752 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005753
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005754 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5755 group_entry)
5756 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005757
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005758 if (!list_empty(&ctx->pinned_groups) ||
5759 !list_empty(&ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005760 goto again;
5761
5762 mutex_unlock(&ctx->mutex);
5763
5764 put_ctx(ctx);
5765}
5766
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005767static int
5768inherit_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 Molnarcdd6c482009-09-21 12:02:48 +02005809/*
5810 * Initialize the perf_event context in task_struct
5811 */
5812int perf_event_init_task(struct task_struct *child)
5813{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005814 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005815 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 Molnarcdd6c482009-09-21 12:02:48 +02005830 * 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 Weisbecker889ff012010-01-09 20:04:47 +01005852 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 Molnarcdd6c482009-09-21 12:02:48 +02005856 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005857 }
5858
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005859 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 Zijlstra05cbaa22009-12-30 16:00:35 +01005868 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005869 /*
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 Molnarcdd6c482009-09-21 12:02:48 +02005888 mutex_unlock(&parent_ctx->mutex);
5889
5890 perf_unpin_context(parent_ctx);
5891
5892 return ret;
5893}
5894
Paul Mackerras220b1402010-03-10 20:45:52 +11005895static 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 Weisbecker76e1d902010-04-05 15:35:57 +02005902 mutex_init(&cpuctx->hlist_mutex);
Paul Mackerras220b1402010-03-10 20:45:52 +11005903 __perf_event_init_context(&cpuctx->ctx, NULL);
5904 }
5905}
5906
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005907static 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 Molnarcdd6c482009-09-21 12:02:48 +02005912
5913 spin_lock(&perf_resource_lock);
5914 cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
5915 spin_unlock(&perf_resource_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005916
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 Molnarcdd6c482009-09-21 12:02:48 +02005926}
5927
5928#ifdef CONFIG_HOTPLUG_CPU
5929static 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 Weisbecker889ff012010-01-09 20:04:47 +01005935 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 Molnarcdd6c482009-09-21 12:02:48 +02005938 __perf_event_remove_from_context(event);
5939}
5940static 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 Weisbecker76e1d902010-04-05 15:35:57 +02005945 mutex_lock(&cpuctx->hlist_mutex);
5946 swevent_hlist_release(cpuctx);
5947 mutex_unlock(&cpuctx->hlist_mutex);
5948
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005949 mutex_lock(&ctx->mutex);
5950 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
5951 mutex_unlock(&ctx->mutex);
5952}
5953#else
5954static inline void perf_event_exit_cpu(int cpu) { }
5955#endif
5956
5957static int __cpuinit
5958perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
5959{
5960 unsigned int cpu = (long)hcpu;
5961
Peter Zijlstra5e116372010-06-11 13:35:08 +02005962 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005963
5964 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02005965 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005966 perf_event_init_cpu(cpu);
5967 break;
5968
Peter Zijlstra5e116372010-06-11 13:35:08 +02005969 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005970 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005971 perf_event_exit_cpu(cpu);
5972 break;
5973
5974 default:
5975 break;
5976 }
5977
5978 return NOTIFY_OK;
5979}
5980
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005981void __init perf_event_init(void)
5982{
Paul Mackerras220b1402010-03-10 20:45:52 +11005983 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005984 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 Molnarcdd6c482009-09-21 12:02:48 +02005990}
5991
Andi Kleenc9be0a32010-01-05 12:47:58 +01005992static ssize_t perf_show_reserve_percpu(struct sysdev_class *class,
5993 struct sysdev_class_attribute *attr,
5994 char *buf)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005995{
5996 return sprintf(buf, "%d\n", perf_reserved_percpu);
5997}
5998
5999static ssize_t
6000perf_set_reserve_percpu(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +01006001 struct sysdev_class_attribute *attr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006002 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 Gleixnere625cce12009-11-17 18:02:06 +01006019 raw_spin_lock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006020 mpt = min(perf_max_events - cpuctx->ctx.nr_events,
6021 perf_max_events - perf_reserved_percpu);
6022 cpuctx->max_pertask = mpt;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01006023 raw_spin_unlock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006024 }
6025 spin_unlock(&perf_resource_lock);
6026
6027 return count;
6028}
6029
Andi Kleenc9be0a32010-01-05 12:47:58 +01006030static ssize_t perf_show_overcommit(struct sysdev_class *class,
6031 struct sysdev_class_attribute *attr,
6032 char *buf)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006033{
6034 return sprintf(buf, "%d\n", perf_overcommit);
6035}
6036
6037static ssize_t
Andi Kleenc9be0a32010-01-05 12:47:58 +01006038perf_set_overcommit(struct sysdev_class *class,
6039 struct sysdev_class_attribute *attr,
6040 const char *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006041{
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
6058static SYSDEV_CLASS_ATTR(
6059 reserve_percpu,
6060 0644,
6061 perf_show_reserve_percpu,
6062 perf_set_reserve_percpu
6063 );
6064
6065static SYSDEV_CLASS_ATTR(
6066 overcommit,
6067 0644,
6068 perf_show_overcommit,
6069 perf_set_overcommit
6070 );
6071
6072static struct attribute *perfclass_attrs[] = {
6073 &attr_reserve_percpu.attr,
6074 &attr_overcommit.attr,
6075 NULL
6076};
6077
6078static struct attribute_group perfclass_attr_group = {
6079 .attrs = perfclass_attrs,
6080 .name = "perf_events",
6081};
6082
6083static int __init perf_event_sysfs_init(void)
6084{
6085 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
6086 &perfclass_attr_group);
6087}
6088device_initcall(perf_event_sysfs_init);