blob: fc512684423f9636336f1ca2642c564c13e409e8 [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>
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020034#include <linux/hw_breakpoint.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020035
36#include <asm/irq_regs.h>
37
38/*
39 * Each CPU has a list of per CPU events:
40 */
Xiao Guangrongaa5452d2009-12-09 11:28:13 +080041static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020042
43int perf_max_events __read_mostly = 1;
44static int perf_reserved_percpu __read_mostly;
45static int perf_overcommit __read_mostly = 1;
46
47static atomic_t nr_events __read_mostly;
48static atomic_t nr_mmap_events __read_mostly;
49static atomic_t nr_comm_events __read_mostly;
50static atomic_t nr_task_events __read_mostly;
51
52/*
53 * perf event paranoia level:
54 * -1 - not paranoid at all
55 * 0 - disallow raw tracepoint access for unpriv
56 * 1 - disallow cpu events for unpriv
57 * 2 - disallow kernel profiling for unpriv
58 */
59int sysctl_perf_event_paranoid __read_mostly = 1;
60
Ingo Molnarcdd6c482009-09-21 12:02:48 +020061int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
62
63/*
64 * max perf event sample rate
65 */
66int sysctl_perf_event_sample_rate __read_mostly = 100000;
67
68static atomic64_t perf_event_id;
69
70/*
71 * Lock for (sysadmin-configurable) event reservations:
72 */
73static DEFINE_SPINLOCK(perf_resource_lock);
74
75/*
76 * Architecture provided APIs - weak aliases:
77 */
78extern __weak const struct pmu *hw_perf_event_init(struct perf_event *event)
79{
80 return NULL;
81}
82
83void __weak hw_perf_disable(void) { barrier(); }
84void __weak hw_perf_enable(void) { barrier(); }
85
Ingo Molnarcdd6c482009-09-21 12:02:48 +020086void __weak perf_event_print_debug(void) { }
87
Matt Fleming84c79912010-10-03 21:41:13 +010088extern __weak const char *perf_pmu_name(void)
89{
90 return "pmu";
91}
92
Ingo Molnarcdd6c482009-09-21 12:02:48 +020093static DEFINE_PER_CPU(int, perf_disable_count);
94
Ingo Molnarcdd6c482009-09-21 12:02:48 +020095void perf_disable(void)
96{
Peter Zijlstra32975a42010-03-06 19:49:19 +010097 if (!__get_cpu_var(perf_disable_count)++)
98 hw_perf_disable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +020099}
100
101void perf_enable(void)
102{
Peter Zijlstra32975a42010-03-06 19:49:19 +0100103 if (!--__get_cpu_var(perf_disable_count))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200104 hw_perf_enable();
105}
106
107static void get_ctx(struct perf_event_context *ctx)
108{
109 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
110}
111
112static void free_ctx(struct rcu_head *head)
113{
114 struct perf_event_context *ctx;
115
116 ctx = container_of(head, struct perf_event_context, rcu_head);
117 kfree(ctx);
118}
119
120static void put_ctx(struct perf_event_context *ctx)
121{
122 if (atomic_dec_and_test(&ctx->refcount)) {
123 if (ctx->parent_ctx)
124 put_ctx(ctx->parent_ctx);
125 if (ctx->task)
126 put_task_struct(ctx->task);
127 call_rcu(&ctx->rcu_head, free_ctx);
128 }
129}
130
131static void unclone_ctx(struct perf_event_context *ctx)
132{
133 if (ctx->parent_ctx) {
134 put_ctx(ctx->parent_ctx);
135 ctx->parent_ctx = NULL;
136 }
137}
138
139/*
140 * If we inherit events we want to return the parent event id
141 * to userspace.
142 */
143static u64 primary_event_id(struct perf_event *event)
144{
145 u64 id = event->id;
146
147 if (event->parent)
148 id = event->parent->id;
149
150 return id;
151}
152
153/*
154 * Get the perf_event_context for a task and lock it.
155 * This has to cope with with the fact that until it is locked,
156 * the context could get moved to another task.
157 */
158static struct perf_event_context *
159perf_lock_task_context(struct task_struct *task, unsigned long *flags)
160{
161 struct perf_event_context *ctx;
162
163 rcu_read_lock();
164 retry:
165 ctx = rcu_dereference(task->perf_event_ctxp);
166 if (ctx) {
167 /*
168 * If this context is a clone of another, it might
169 * get swapped for another underneath us by
170 * perf_event_task_sched_out, though the
171 * rcu_read_lock() protects us from any context
172 * getting freed. Lock the context and check if it
173 * got swapped before we could get the lock, and retry
174 * if so. If we locked the right context, then it
175 * can't get swapped on us any more.
176 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100177 raw_spin_lock_irqsave(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200178 if (ctx != rcu_dereference(task->perf_event_ctxp)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100179 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200180 goto retry;
181 }
182
183 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100184 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200185 ctx = NULL;
186 }
187 }
188 rcu_read_unlock();
189 return ctx;
190}
191
192/*
193 * Get the context for a task and increment its pin_count so it
194 * can't get swapped to another task. This also increments its
195 * reference count so that the context can't get freed.
196 */
197static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
198{
199 struct perf_event_context *ctx;
200 unsigned long flags;
201
202 ctx = perf_lock_task_context(task, &flags);
203 if (ctx) {
204 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100205 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200206 }
207 return ctx;
208}
209
210static void perf_unpin_context(struct perf_event_context *ctx)
211{
212 unsigned long flags;
213
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100214 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200215 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100216 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200217 put_ctx(ctx);
218}
219
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100220static inline u64 perf_clock(void)
221{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200222 return local_clock();
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100223}
224
225/*
226 * Update the record of the current time in a context.
227 */
228static void update_context_time(struct perf_event_context *ctx)
229{
230 u64 now = perf_clock();
231
232 ctx->time += now - ctx->timestamp;
233 ctx->timestamp = now;
234}
235
236/*
237 * Update the total_time_enabled and total_time_running fields for a event.
238 */
239static void update_event_times(struct perf_event *event)
240{
241 struct perf_event_context *ctx = event->ctx;
242 u64 run_end;
243
244 if (event->state < PERF_EVENT_STATE_INACTIVE ||
245 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
246 return;
247
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100248 if (ctx->is_active)
249 run_end = ctx->time;
250 else
251 run_end = event->tstamp_stopped;
252
253 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100254
255 if (event->state == PERF_EVENT_STATE_INACTIVE)
256 run_end = event->tstamp_stopped;
257 else
258 run_end = ctx->time;
259
260 event->total_time_running = run_end - event->tstamp_running;
261}
262
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200263/*
264 * Update total_time_enabled and total_time_running for all events in a group.
265 */
266static void update_group_times(struct perf_event *leader)
267{
268 struct perf_event *event;
269
270 update_event_times(leader);
271 list_for_each_entry(event, &leader->sibling_list, group_entry)
272 update_event_times(event);
273}
274
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100275static struct list_head *
276ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
277{
278 if (event->attr.pinned)
279 return &ctx->pinned_groups;
280 else
281 return &ctx->flexible_groups;
282}
283
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200284/*
285 * Add a event from the lists for its context.
286 * Must be called with ctx->mutex and ctx->lock held.
287 */
288static void
289list_add_event(struct perf_event *event, struct perf_event_context *ctx)
290{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200291 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
292 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200293
294 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200295 * If we're a stand alone event or group leader, we go to the context
296 * list, group events are kept attached to the group so that
297 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200298 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200299 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100300 struct list_head *list;
301
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100302 if (is_software_event(event))
303 event->group_flags |= PERF_GROUP_SOFTWARE;
304
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100305 list = ctx_group_list(event, ctx);
306 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200307 }
308
309 list_add_rcu(&event->event_entry, &ctx->event_list);
310 ctx->nr_events++;
311 if (event->attr.inherit_stat)
312 ctx->nr_stat++;
313}
314
Peter Zijlstra8a495422010-05-27 15:47:49 +0200315static void perf_group_attach(struct perf_event *event)
316{
317 struct perf_event *group_leader = event->group_leader;
318
319 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_GROUP);
320 event->attach_state |= PERF_ATTACH_GROUP;
321
322 if (group_leader == event)
323 return;
324
325 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
326 !is_software_event(event))
327 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
328
329 list_add_tail(&event->group_entry, &group_leader->sibling_list);
330 group_leader->nr_siblings++;
331}
332
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200333/*
334 * Remove a event from the lists for its context.
335 * Must be called with ctx->mutex and ctx->lock held.
336 */
337static void
338list_del_event(struct perf_event *event, struct perf_event_context *ctx)
339{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200340 /*
341 * We can have double detach due to exit/hot-unplug + close.
342 */
343 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200344 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200345
346 event->attach_state &= ~PERF_ATTACH_CONTEXT;
347
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200348 ctx->nr_events--;
349 if (event->attr.inherit_stat)
350 ctx->nr_stat--;
351
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200352 list_del_rcu(&event->event_entry);
353
Peter Zijlstra8a495422010-05-27 15:47:49 +0200354 if (event->group_leader == event)
355 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200356
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200357 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800358
359 /*
360 * If event was in error state, then keep it
361 * that way, otherwise bogus counts will be
362 * returned on read(). The only way to get out
363 * of error state is by explicit re-enabling
364 * of the event
365 */
366 if (event->state > PERF_EVENT_STATE_OFF)
367 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +0200368}
369
Peter Zijlstra8a495422010-05-27 15:47:49 +0200370static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +0200371{
372 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200373 struct list_head *list = NULL;
374
375 /*
376 * We can have double detach due to exit/hot-unplug + close.
377 */
378 if (!(event->attach_state & PERF_ATTACH_GROUP))
379 return;
380
381 event->attach_state &= ~PERF_ATTACH_GROUP;
382
383 /*
384 * If this is a sibling, remove it from its group.
385 */
386 if (event->group_leader != event) {
387 list_del_init(&event->group_entry);
388 event->group_leader->nr_siblings--;
389 return;
390 }
391
392 if (!list_empty(&event->group_entry))
393 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +0100394
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200395 /*
396 * If this was a group event with sibling events then
397 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +0200398 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200399 */
400 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +0200401 if (list)
402 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200403 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100404
405 /* Inherit group flags from the previous leader */
406 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200407 }
408}
409
Stephane Eranianfa66f072010-08-26 16:40:01 +0200410static inline int
411event_filter_match(struct perf_event *event)
412{
413 return event->cpu == -1 || event->cpu == smp_processor_id();
414}
415
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200416static void
417event_sched_out(struct perf_event *event,
418 struct perf_cpu_context *cpuctx,
419 struct perf_event_context *ctx)
420{
Stephane Eranianfa66f072010-08-26 16:40:01 +0200421 u64 delta;
422 /*
423 * An event which could not be activated because of
424 * filter mismatch still needs to have its timings
425 * maintained, otherwise bogus information is return
426 * via read() for time_enabled, time_running:
427 */
428 if (event->state == PERF_EVENT_STATE_INACTIVE
429 && !event_filter_match(event)) {
430 delta = ctx->time - event->tstamp_stopped;
431 event->tstamp_running += delta;
432 event->tstamp_stopped = ctx->time;
433 }
434
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200435 if (event->state != PERF_EVENT_STATE_ACTIVE)
436 return;
437
438 event->state = PERF_EVENT_STATE_INACTIVE;
439 if (event->pending_disable) {
440 event->pending_disable = 0;
441 event->state = PERF_EVENT_STATE_OFF;
442 }
443 event->tstamp_stopped = ctx->time;
444 event->pmu->disable(event);
445 event->oncpu = -1;
446
447 if (!is_software_event(event))
448 cpuctx->active_oncpu--;
449 ctx->nr_active--;
450 if (event->attr.exclusive || !cpuctx->active_oncpu)
451 cpuctx->exclusive = 0;
452}
453
454static void
455group_sched_out(struct perf_event *group_event,
456 struct perf_cpu_context *cpuctx,
457 struct perf_event_context *ctx)
458{
459 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +0200460 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200461
462 event_sched_out(group_event, cpuctx, ctx);
463
464 /*
465 * Schedule out siblings (if any):
466 */
467 list_for_each_entry(event, &group_event->sibling_list, group_entry)
468 event_sched_out(event, cpuctx, ctx);
469
Stephane Eranianfa66f072010-08-26 16:40:01 +0200470 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200471 cpuctx->exclusive = 0;
472}
473
474/*
475 * Cross CPU call to remove a performance event
476 *
477 * We disable the event on the hardware level first. After that we
478 * remove it from the context list.
479 */
480static void __perf_event_remove_from_context(void *info)
481{
482 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
483 struct perf_event *event = info;
484 struct perf_event_context *ctx = event->ctx;
485
486 /*
487 * If this is a task context, we need to check whether it is
488 * the current task context of this cpu. If not it has been
489 * scheduled out before the smp call arrived.
490 */
491 if (ctx->task && cpuctx->task_ctx != ctx)
492 return;
493
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100494 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200495 /*
496 * Protect the list operation against NMI by disabling the
497 * events on a global level.
498 */
499 perf_disable();
500
501 event_sched_out(event, cpuctx, ctx);
502
503 list_del_event(event, ctx);
504
505 if (!ctx->task) {
506 /*
507 * Allow more per task events with respect to the
508 * reservation:
509 */
510 cpuctx->max_pertask =
511 min(perf_max_events - ctx->nr_events,
512 perf_max_events - perf_reserved_percpu);
513 }
514
515 perf_enable();
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100516 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200517}
518
519
520/*
521 * Remove the event from a task's (or a CPU's) list of events.
522 *
523 * Must be called with ctx->mutex held.
524 *
525 * CPU events are removed with a smp call. For task events we only
526 * call when the task is on a CPU.
527 *
528 * If event->ctx is a cloned context, callers must make sure that
529 * every task struct that event->ctx->task could possibly point to
530 * remains valid. This is OK when called from perf_release since
531 * that only calls us on the top-level context, which can't be a clone.
532 * When called from perf_event_exit_task, it's OK because the
533 * context has been detached from its task.
534 */
535static void perf_event_remove_from_context(struct perf_event *event)
536{
537 struct perf_event_context *ctx = event->ctx;
538 struct task_struct *task = ctx->task;
539
540 if (!task) {
541 /*
542 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200543 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200544 */
545 smp_call_function_single(event->cpu,
546 __perf_event_remove_from_context,
547 event, 1);
548 return;
549 }
550
551retry:
552 task_oncpu_function_call(task, __perf_event_remove_from_context,
553 event);
554
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100555 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200556 /*
557 * If the context is active we need to retry the smp call.
558 */
559 if (ctx->nr_active && !list_empty(&event->group_entry)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100560 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200561 goto retry;
562 }
563
564 /*
565 * The lock prevents that this context is scheduled in so we
566 * can remove the event safely, if the call above did not
567 * succeed.
568 */
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +0100569 if (!list_empty(&event->group_entry))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200570 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100571 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200572}
573
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200574/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200575 * Cross CPU call to disable a performance event
576 */
577static void __perf_event_disable(void *info)
578{
579 struct perf_event *event = info;
580 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
581 struct perf_event_context *ctx = event->ctx;
582
583 /*
584 * If this is a per-task event, need to check whether this
585 * event's task is the current task on this cpu.
586 */
587 if (ctx->task && cpuctx->task_ctx != ctx)
588 return;
589
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100590 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200591
592 /*
593 * If the event is on, turn it off.
594 * If it is in error state, leave it in error state.
595 */
596 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
597 update_context_time(ctx);
598 update_group_times(event);
599 if (event == event->group_leader)
600 group_sched_out(event, cpuctx, ctx);
601 else
602 event_sched_out(event, cpuctx, ctx);
603 event->state = PERF_EVENT_STATE_OFF;
604 }
605
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100606 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200607}
608
609/*
610 * Disable a event.
611 *
612 * If event->ctx is a cloned context, callers must make sure that
613 * every task struct that event->ctx->task could possibly point to
614 * remains valid. This condition is satisifed when called through
615 * perf_event_for_each_child or perf_event_for_each because they
616 * hold the top-level event's child_mutex, so any descendant that
617 * goes to exit will block in sync_child_event.
618 * When called from perf_pending_event it's OK because event->ctx
619 * is the current context on this CPU and preemption is disabled,
620 * hence we can't get into perf_event_task_sched_out for this context.
621 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100622void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200623{
624 struct perf_event_context *ctx = event->ctx;
625 struct task_struct *task = ctx->task;
626
627 if (!task) {
628 /*
629 * Disable the event on the cpu that it's on
630 */
631 smp_call_function_single(event->cpu, __perf_event_disable,
632 event, 1);
633 return;
634 }
635
636 retry:
637 task_oncpu_function_call(task, __perf_event_disable, event);
638
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100639 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200640 /*
641 * If the event is still active, we need to retry the cross-call.
642 */
643 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100644 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200645 goto retry;
646 }
647
648 /*
649 * Since we have the lock this context can't be scheduled
650 * in, so we can change the state safely.
651 */
652 if (event->state == PERF_EVENT_STATE_INACTIVE) {
653 update_group_times(event);
654 event->state = PERF_EVENT_STATE_OFF;
655 }
656
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100657 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200658}
659
660static int
661event_sched_in(struct perf_event *event,
662 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100663 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200664{
665 if (event->state <= PERF_EVENT_STATE_OFF)
666 return 0;
667
668 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +0100669 event->oncpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200670 /*
671 * The new state must be visible before we turn it on in the hardware:
672 */
673 smp_wmb();
674
675 if (event->pmu->enable(event)) {
676 event->state = PERF_EVENT_STATE_INACTIVE;
677 event->oncpu = -1;
678 return -EAGAIN;
679 }
680
681 event->tstamp_running += ctx->time - event->tstamp_stopped;
682
683 if (!is_software_event(event))
684 cpuctx->active_oncpu++;
685 ctx->nr_active++;
686
687 if (event->attr.exclusive)
688 cpuctx->exclusive = 1;
689
690 return 0;
691}
692
693static int
694group_sched_in(struct perf_event *group_event,
695 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100696 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200697{
Lin Ming6bde9b62010-04-23 13:56:00 +0800698 struct perf_event *event, *partial_group = NULL;
699 const struct pmu *pmu = group_event->pmu;
700 bool txn = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200701
702 if (group_event->state == PERF_EVENT_STATE_OFF)
703 return 0;
704
Lin Ming6bde9b62010-04-23 13:56:00 +0800705 /* Check if group transaction availabe */
706 if (pmu->start_txn)
707 txn = true;
708
709 if (txn)
710 pmu->start_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200711
Stephane Eranian90151c352010-05-25 16:23:10 +0200712 if (event_sched_in(group_event, cpuctx, ctx)) {
713 if (txn)
714 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200715 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +0200716 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200717
718 /*
719 * Schedule in siblings as one group (if any):
720 */
721 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Peter Zijlstra6e377382010-02-11 13:21:58 +0100722 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200723 partial_group = event;
724 goto group_error;
725 }
726 }
727
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +0200728 if (!txn || !pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +1000729 return 0;
Lin Ming6bde9b62010-04-23 13:56:00 +0800730
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200731group_error:
732 /*
733 * Groups can be scheduled in as one unit only, so undo any
734 * partial group before returning:
735 */
736 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
737 if (event == partial_group)
738 break;
739 event_sched_out(event, cpuctx, ctx);
740 }
741 event_sched_out(group_event, cpuctx, ctx);
742
Stephane Eranian90151c352010-05-25 16:23:10 +0200743 if (txn)
744 pmu->cancel_txn(pmu);
745
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200746 return -EAGAIN;
747}
748
749/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200750 * Work out whether we can put this event group on the CPU now.
751 */
752static int group_can_go_on(struct perf_event *event,
753 struct perf_cpu_context *cpuctx,
754 int can_add_hw)
755{
756 /*
757 * Groups consisting entirely of software events can always go on.
758 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100759 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200760 return 1;
761 /*
762 * If an exclusive group is already on, no other hardware
763 * events can go on.
764 */
765 if (cpuctx->exclusive)
766 return 0;
767 /*
768 * If this group is exclusive and there are already
769 * events on the CPU, it can't go on.
770 */
771 if (event->attr.exclusive && cpuctx->active_oncpu)
772 return 0;
773 /*
774 * Otherwise, try to add it if all previous groups were able
775 * to go on.
776 */
777 return can_add_hw;
778}
779
780static void add_event_to_ctx(struct perf_event *event,
781 struct perf_event_context *ctx)
782{
783 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200784 perf_group_attach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200785 event->tstamp_enabled = ctx->time;
786 event->tstamp_running = ctx->time;
787 event->tstamp_stopped = ctx->time;
788}
789
790/*
791 * Cross CPU call to install and enable a performance event
792 *
793 * Must be called with ctx->mutex held
794 */
795static void __perf_install_in_context(void *info)
796{
797 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
798 struct perf_event *event = info;
799 struct perf_event_context *ctx = event->ctx;
800 struct perf_event *leader = event->group_leader;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200801 int err;
802
803 /*
804 * If this is a task context, we need to check whether it is
805 * the current task context of this cpu. If not it has been
806 * scheduled out before the smp call arrived.
807 * Or possibly this is the right context but it isn't
808 * on this cpu because it had no events.
809 */
810 if (ctx->task && cpuctx->task_ctx != ctx) {
811 if (cpuctx->task_ctx || ctx->task != current)
812 return;
813 cpuctx->task_ctx = ctx;
814 }
815
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100816 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200817 ctx->is_active = 1;
818 update_context_time(ctx);
819
820 /*
821 * Protect the list operation against NMI by disabling the
822 * events on a global level. NOP for non NMI based events.
823 */
824 perf_disable();
825
826 add_event_to_ctx(event, ctx);
827
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100828 if (event->cpu != -1 && event->cpu != smp_processor_id())
829 goto unlock;
830
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200831 /*
832 * Don't put the event on if it is disabled or if
833 * it is in a group and the group isn't on.
834 */
835 if (event->state != PERF_EVENT_STATE_INACTIVE ||
836 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
837 goto unlock;
838
839 /*
840 * An exclusive event can't go on if there are already active
841 * hardware events, and no hardware event can go on if there
842 * is already an exclusive event on.
843 */
844 if (!group_can_go_on(event, cpuctx, 1))
845 err = -EEXIST;
846 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100847 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200848
849 if (err) {
850 /*
851 * This event couldn't go on. If it is in a group
852 * then we have to pull the whole group off.
853 * If the event group is pinned then put it in error state.
854 */
855 if (leader != event)
856 group_sched_out(leader, cpuctx, ctx);
857 if (leader->attr.pinned) {
858 update_group_times(leader);
859 leader->state = PERF_EVENT_STATE_ERROR;
860 }
861 }
862
863 if (!err && !ctx->task && cpuctx->max_pertask)
864 cpuctx->max_pertask--;
865
866 unlock:
867 perf_enable();
868
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100869 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200870}
871
872/*
873 * Attach a performance event to a context
874 *
875 * First we add the event to the list with the hardware enable bit
876 * in event->hw_config cleared.
877 *
878 * If the event is attached to a task which is on a CPU we use a smp
879 * call to enable it in the task context. The task might have been
880 * scheduled away, but we check this in the smp call again.
881 *
882 * Must be called with ctx->mutex held.
883 */
884static void
885perf_install_in_context(struct perf_event_context *ctx,
886 struct perf_event *event,
887 int cpu)
888{
889 struct task_struct *task = ctx->task;
890
891 if (!task) {
892 /*
893 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200894 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200895 */
896 smp_call_function_single(cpu, __perf_install_in_context,
897 event, 1);
898 return;
899 }
900
901retry:
902 task_oncpu_function_call(task, __perf_install_in_context,
903 event);
904
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100905 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200906 /*
907 * we need to retry the smp call.
908 */
909 if (ctx->is_active && list_empty(&event->group_entry)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100910 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200911 goto retry;
912 }
913
914 /*
915 * The lock prevents that this context is scheduled in so we
916 * can add the event safely, if it the call above did not
917 * succeed.
918 */
919 if (list_empty(&event->group_entry))
920 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100921 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200922}
923
924/*
925 * Put a event into inactive state and update time fields.
926 * Enabling the leader of a group effectively enables all
927 * the group members that aren't explicitly disabled, so we
928 * have to update their ->tstamp_enabled also.
929 * Note: this works for group members as well as group leaders
930 * since the non-leader members' sibling_lists will be empty.
931 */
932static void __perf_event_mark_enabled(struct perf_event *event,
933 struct perf_event_context *ctx)
934{
935 struct perf_event *sub;
936
937 event->state = PERF_EVENT_STATE_INACTIVE;
938 event->tstamp_enabled = ctx->time - event->total_time_enabled;
939 list_for_each_entry(sub, &event->sibling_list, group_entry)
940 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
941 sub->tstamp_enabled =
942 ctx->time - sub->total_time_enabled;
943}
944
945/*
946 * Cross CPU call to enable a performance event
947 */
948static void __perf_event_enable(void *info)
949{
950 struct perf_event *event = info;
951 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
952 struct perf_event_context *ctx = event->ctx;
953 struct perf_event *leader = event->group_leader;
954 int err;
955
956 /*
957 * If this is a per-task event, need to check whether this
958 * event's task is the current task on this cpu.
959 */
960 if (ctx->task && cpuctx->task_ctx != ctx) {
961 if (cpuctx->task_ctx || ctx->task != current)
962 return;
963 cpuctx->task_ctx = ctx;
964 }
965
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100966 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200967 ctx->is_active = 1;
968 update_context_time(ctx);
969
970 if (event->state >= PERF_EVENT_STATE_INACTIVE)
971 goto unlock;
972 __perf_event_mark_enabled(event, ctx);
973
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100974 if (event->cpu != -1 && event->cpu != smp_processor_id())
975 goto unlock;
976
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200977 /*
978 * If the event is in a group and isn't the group leader,
979 * then don't put it on unless the group is on.
980 */
981 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
982 goto unlock;
983
984 if (!group_can_go_on(event, cpuctx, 1)) {
985 err = -EEXIST;
986 } else {
987 perf_disable();
988 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +0100989 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200990 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100991 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200992 perf_enable();
993 }
994
995 if (err) {
996 /*
997 * If this event can't go on and it's part of a
998 * group, then the whole group has to come off.
999 */
1000 if (leader != event)
1001 group_sched_out(leader, cpuctx, ctx);
1002 if (leader->attr.pinned) {
1003 update_group_times(leader);
1004 leader->state = PERF_EVENT_STATE_ERROR;
1005 }
1006 }
1007
1008 unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001009 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001010}
1011
1012/*
1013 * Enable a event.
1014 *
1015 * If event->ctx is a cloned context, callers must make sure that
1016 * every task struct that event->ctx->task could possibly point to
1017 * remains valid. This condition is satisfied when called through
1018 * perf_event_for_each_child or perf_event_for_each as described
1019 * for perf_event_disable.
1020 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001021void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001022{
1023 struct perf_event_context *ctx = event->ctx;
1024 struct task_struct *task = ctx->task;
1025
1026 if (!task) {
1027 /*
1028 * Enable the event on the cpu that it's on
1029 */
1030 smp_call_function_single(event->cpu, __perf_event_enable,
1031 event, 1);
1032 return;
1033 }
1034
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001035 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001036 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1037 goto out;
1038
1039 /*
1040 * If the event is in error state, clear that first.
1041 * That way, if we see the event in error state below, we
1042 * know that it has gone back into error state, as distinct
1043 * from the task having been scheduled away before the
1044 * cross-call arrived.
1045 */
1046 if (event->state == PERF_EVENT_STATE_ERROR)
1047 event->state = PERF_EVENT_STATE_OFF;
1048
1049 retry:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001050 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001051 task_oncpu_function_call(task, __perf_event_enable, event);
1052
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001053 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001054
1055 /*
1056 * If the context is active and the event is still off,
1057 * we need to retry the cross-call.
1058 */
1059 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1060 goto retry;
1061
1062 /*
1063 * Since we have the lock this context can't be scheduled
1064 * in, so we can change the state safely.
1065 */
1066 if (event->state == PERF_EVENT_STATE_OFF)
1067 __perf_event_mark_enabled(event, ctx);
1068
1069 out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001070 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001071}
1072
1073static int perf_event_refresh(struct perf_event *event, int refresh)
1074{
1075 /*
1076 * not supported on inherited events
1077 */
1078 if (event->attr.inherit)
1079 return -EINVAL;
1080
1081 atomic_add(refresh, &event->event_limit);
1082 perf_event_enable(event);
1083
1084 return 0;
1085}
1086
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001087enum event_type_t {
1088 EVENT_FLEXIBLE = 0x1,
1089 EVENT_PINNED = 0x2,
1090 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1091};
1092
1093static void ctx_sched_out(struct perf_event_context *ctx,
1094 struct perf_cpu_context *cpuctx,
1095 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001096{
1097 struct perf_event *event;
1098
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001099 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001100 ctx->is_active = 0;
1101 if (likely(!ctx->nr_events))
1102 goto out;
1103 update_context_time(ctx);
1104
1105 perf_disable();
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001106 if (!ctx->nr_active)
1107 goto out_enable;
1108
1109 if (event_type & EVENT_PINNED)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001110 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1111 group_sched_out(event, cpuctx, ctx);
1112
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001113 if (event_type & EVENT_FLEXIBLE)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001114 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001115 group_sched_out(event, cpuctx, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001116
1117 out_enable:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001118 perf_enable();
1119 out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001120 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001121}
1122
1123/*
1124 * Test whether two contexts are equivalent, i.e. whether they
1125 * have both been cloned from the same version of the same context
1126 * and they both have the same number of enabled events.
1127 * If the number of enabled events is the same, then the set
1128 * of enabled events should be the same, because these are both
1129 * inherited contexts, therefore we can't access individual events
1130 * in them directly with an fd; we can only enable/disable all
1131 * events via prctl, or enable/disable all events in a family
1132 * via ioctl, which will have the same effect on both contexts.
1133 */
1134static int context_equiv(struct perf_event_context *ctx1,
1135 struct perf_event_context *ctx2)
1136{
1137 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1138 && ctx1->parent_gen == ctx2->parent_gen
1139 && !ctx1->pin_count && !ctx2->pin_count;
1140}
1141
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001142static void __perf_event_sync_stat(struct perf_event *event,
1143 struct perf_event *next_event)
1144{
1145 u64 value;
1146
1147 if (!event->attr.inherit_stat)
1148 return;
1149
1150 /*
1151 * Update the event value, we cannot use perf_event_read()
1152 * because we're in the middle of a context switch and have IRQs
1153 * disabled, which upsets smp_call_function_single(), however
1154 * we know the event must be on the current CPU, therefore we
1155 * don't need to use it.
1156 */
1157 switch (event->state) {
1158 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001159 event->pmu->read(event);
1160 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001161
1162 case PERF_EVENT_STATE_INACTIVE:
1163 update_event_times(event);
1164 break;
1165
1166 default:
1167 break;
1168 }
1169
1170 /*
1171 * In order to keep per-task stats reliable we need to flip the event
1172 * values when we flip the contexts.
1173 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001174 value = local64_read(&next_event->count);
1175 value = local64_xchg(&event->count, value);
1176 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001177
1178 swap(event->total_time_enabled, next_event->total_time_enabled);
1179 swap(event->total_time_running, next_event->total_time_running);
1180
1181 /*
1182 * Since we swizzled the values, update the user visible data too.
1183 */
1184 perf_event_update_userpage(event);
1185 perf_event_update_userpage(next_event);
1186}
1187
1188#define list_next_entry(pos, member) \
1189 list_entry(pos->member.next, typeof(*pos), member)
1190
1191static void perf_event_sync_stat(struct perf_event_context *ctx,
1192 struct perf_event_context *next_ctx)
1193{
1194 struct perf_event *event, *next_event;
1195
1196 if (!ctx->nr_stat)
1197 return;
1198
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001199 update_context_time(ctx);
1200
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001201 event = list_first_entry(&ctx->event_list,
1202 struct perf_event, event_entry);
1203
1204 next_event = list_first_entry(&next_ctx->event_list,
1205 struct perf_event, event_entry);
1206
1207 while (&event->event_entry != &ctx->event_list &&
1208 &next_event->event_entry != &next_ctx->event_list) {
1209
1210 __perf_event_sync_stat(event, next_event);
1211
1212 event = list_next_entry(event, event_entry);
1213 next_event = list_next_entry(next_event, event_entry);
1214 }
1215}
1216
1217/*
1218 * Called from scheduler to remove the events of the current task,
1219 * with interrupts disabled.
1220 *
1221 * We stop each event and update the event value in event->count.
1222 *
1223 * This does not protect us against NMI, but disable()
1224 * sets the disabled bit in the control field of event _before_
1225 * accessing the event control register. If a NMI hits, then it will
1226 * not restart the event.
1227 */
1228void perf_event_task_sched_out(struct task_struct *task,
Peter Zijlstra49f47432009-12-27 11:51:52 +01001229 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001230{
Peter Zijlstra49f47432009-12-27 11:51:52 +01001231 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001232 struct perf_event_context *ctx = task->perf_event_ctxp;
1233 struct perf_event_context *next_ctx;
1234 struct perf_event_context *parent;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001235 int do_switch = 1;
1236
Frederic Weisbeckere49a5bd2010-03-22 19:40:03 +01001237 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001238
1239 if (likely(!ctx || !cpuctx->task_ctx))
1240 return;
1241
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001242 rcu_read_lock();
1243 parent = rcu_dereference(ctx->parent_ctx);
1244 next_ctx = next->perf_event_ctxp;
1245 if (parent && next_ctx &&
1246 rcu_dereference(next_ctx->parent_ctx) == parent) {
1247 /*
1248 * Looks like the two contexts are clones, so we might be
1249 * able to optimize the context switch. We lock both
1250 * contexts and check that they are clones under the
1251 * lock (including re-checking that neither has been
1252 * uncloned in the meantime). It doesn't matter which
1253 * order we take the locks because no other cpu could
1254 * be trying to lock both of these tasks.
1255 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001256 raw_spin_lock(&ctx->lock);
1257 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001258 if (context_equiv(ctx, next_ctx)) {
1259 /*
1260 * XXX do we need a memory barrier of sorts
1261 * wrt to rcu_dereference() of perf_event_ctxp
1262 */
1263 task->perf_event_ctxp = next_ctx;
1264 next->perf_event_ctxp = ctx;
1265 ctx->task = next;
1266 next_ctx->task = task;
1267 do_switch = 0;
1268
1269 perf_event_sync_stat(ctx, next_ctx);
1270 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001271 raw_spin_unlock(&next_ctx->lock);
1272 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001273 }
1274 rcu_read_unlock();
1275
1276 if (do_switch) {
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001277 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001278 cpuctx->task_ctx = NULL;
1279 }
1280}
1281
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001282static void task_ctx_sched_out(struct perf_event_context *ctx,
1283 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001284{
1285 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1286
1287 if (!cpuctx->task_ctx)
1288 return;
1289
1290 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1291 return;
1292
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001293 ctx_sched_out(ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001294 cpuctx->task_ctx = NULL;
1295}
1296
1297/*
1298 * Called with IRQs disabled
1299 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001300static void __perf_event_task_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001301{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001302 task_ctx_sched_out(ctx, EVENT_ALL);
1303}
1304
1305/*
1306 * Called with IRQs disabled
1307 */
1308static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1309 enum event_type_t event_type)
1310{
1311 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001312}
1313
1314static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001315ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001316 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001317{
1318 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001319
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001320 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1321 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001322 continue;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001323 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001324 continue;
1325
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001326 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001327 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001328
1329 /*
1330 * If this pinned group hasn't been scheduled,
1331 * put it in error state.
1332 */
1333 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1334 update_group_times(event);
1335 event->state = PERF_EVENT_STATE_ERROR;
1336 }
1337 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001338}
1339
1340static void
1341ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001342 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001343{
1344 struct perf_event *event;
1345 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001346
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001347 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1348 /* Ignore events in OFF or ERROR state */
1349 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001350 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001351 /*
1352 * Listen to the 'cpu' scheduling filter constraint
1353 * of events:
1354 */
Peter Zijlstra6e377382010-02-11 13:21:58 +01001355 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001356 continue;
1357
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001358 if (group_can_go_on(event, cpuctx, can_add_hw))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001359 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001360 can_add_hw = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001361 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001362}
1363
1364static void
1365ctx_sched_in(struct perf_event_context *ctx,
1366 struct perf_cpu_context *cpuctx,
1367 enum event_type_t event_type)
1368{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001369 raw_spin_lock(&ctx->lock);
1370 ctx->is_active = 1;
1371 if (likely(!ctx->nr_events))
1372 goto out;
1373
1374 ctx->timestamp = perf_clock();
1375
1376 perf_disable();
1377
1378 /*
1379 * First go through the list and put on any pinned groups
1380 * in order to give them the best chance of going on.
1381 */
1382 if (event_type & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001383 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001384
1385 /* Then walk through the lower prio flexible groups */
1386 if (event_type & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001387 ctx_flexible_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001388
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001389 perf_enable();
1390 out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001391 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001392}
1393
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001394static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1395 enum event_type_t event_type)
1396{
1397 struct perf_event_context *ctx = &cpuctx->ctx;
1398
1399 ctx_sched_in(ctx, cpuctx, event_type);
1400}
1401
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001402static void task_ctx_sched_in(struct task_struct *task,
1403 enum event_type_t event_type)
1404{
1405 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1406 struct perf_event_context *ctx = task->perf_event_ctxp;
1407
1408 if (likely(!ctx))
1409 return;
1410 if (cpuctx->task_ctx == ctx)
1411 return;
1412 ctx_sched_in(ctx, cpuctx, event_type);
1413 cpuctx->task_ctx = ctx;
1414}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001415/*
1416 * Called from scheduler to add the events of the current task
1417 * with interrupts disabled.
1418 *
1419 * We restore the event value and then enable it.
1420 *
1421 * This does not protect us against NMI, but enable()
1422 * sets the enabled bit in the control field of event _before_
1423 * accessing the event control register. If a NMI hits, then it will
1424 * keep the event running.
1425 */
Peter Zijlstra49f47432009-12-27 11:51:52 +01001426void perf_event_task_sched_in(struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001427{
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001428 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1429 struct perf_event_context *ctx = task->perf_event_ctxp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001430
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001431 if (likely(!ctx))
1432 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001433
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001434 if (cpuctx->task_ctx == ctx)
1435 return;
1436
eranian@google.com9b33fa62010-03-10 22:26:05 -08001437 perf_disable();
1438
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001439 /*
1440 * We want to keep the following priority order:
1441 * cpu pinned (that don't need to move), task pinned,
1442 * cpu flexible, task flexible.
1443 */
1444 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1445
1446 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1447 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1448 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1449
1450 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08001451
1452 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001453}
1454
1455#define MAX_INTERRUPTS (~0ULL)
1456
1457static void perf_log_throttle(struct perf_event *event, int enable);
1458
Peter Zijlstraabd50712010-01-26 18:50:16 +01001459static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1460{
1461 u64 frequency = event->attr.sample_freq;
1462 u64 sec = NSEC_PER_SEC;
1463 u64 divisor, dividend;
1464
1465 int count_fls, nsec_fls, frequency_fls, sec_fls;
1466
1467 count_fls = fls64(count);
1468 nsec_fls = fls64(nsec);
1469 frequency_fls = fls64(frequency);
1470 sec_fls = 30;
1471
1472 /*
1473 * We got @count in @nsec, with a target of sample_freq HZ
1474 * the target period becomes:
1475 *
1476 * @count * 10^9
1477 * period = -------------------
1478 * @nsec * sample_freq
1479 *
1480 */
1481
1482 /*
1483 * Reduce accuracy by one bit such that @a and @b converge
1484 * to a similar magnitude.
1485 */
1486#define REDUCE_FLS(a, b) \
1487do { \
1488 if (a##_fls > b##_fls) { \
1489 a >>= 1; \
1490 a##_fls--; \
1491 } else { \
1492 b >>= 1; \
1493 b##_fls--; \
1494 } \
1495} while (0)
1496
1497 /*
1498 * Reduce accuracy until either term fits in a u64, then proceed with
1499 * the other, so that finally we can do a u64/u64 division.
1500 */
1501 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1502 REDUCE_FLS(nsec, frequency);
1503 REDUCE_FLS(sec, count);
1504 }
1505
1506 if (count_fls + sec_fls > 64) {
1507 divisor = nsec * frequency;
1508
1509 while (count_fls + sec_fls > 64) {
1510 REDUCE_FLS(count, sec);
1511 divisor >>= 1;
1512 }
1513
1514 dividend = count * sec;
1515 } else {
1516 dividend = count * sec;
1517
1518 while (nsec_fls + frequency_fls > 64) {
1519 REDUCE_FLS(nsec, frequency);
1520 dividend >>= 1;
1521 }
1522
1523 divisor = nsec * frequency;
1524 }
1525
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02001526 if (!divisor)
1527 return dividend;
1528
Peter Zijlstraabd50712010-01-26 18:50:16 +01001529 return div64_u64(dividend, divisor);
1530}
1531
Stephane Eraniand76a0812010-02-08 17:06:01 +02001532static void perf_event_stop(struct perf_event *event)
1533{
1534 if (!event->pmu->stop)
1535 return event->pmu->disable(event);
1536
1537 return event->pmu->stop(event);
1538}
1539
1540static int perf_event_start(struct perf_event *event)
1541{
1542 if (!event->pmu->start)
1543 return event->pmu->enable(event);
1544
1545 return event->pmu->start(event);
1546}
1547
Peter Zijlstraabd50712010-01-26 18:50:16 +01001548static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001549{
1550 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02001551 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001552 s64 delta;
1553
Peter Zijlstraabd50712010-01-26 18:50:16 +01001554 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001555
1556 delta = (s64)(period - hwc->sample_period);
1557 delta = (delta + 7) / 8; /* low pass filter */
1558
1559 sample_period = hwc->sample_period + delta;
1560
1561 if (!sample_period)
1562 sample_period = 1;
1563
1564 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001565
Peter Zijlstrae7850592010-05-21 14:43:08 +02001566 if (local64_read(&hwc->period_left) > 8*sample_period) {
Peter Zijlstraabd50712010-01-26 18:50:16 +01001567 perf_disable();
Stephane Eraniand76a0812010-02-08 17:06:01 +02001568 perf_event_stop(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001569 local64_set(&hwc->period_left, 0);
Stephane Eraniand76a0812010-02-08 17:06:01 +02001570 perf_event_start(event);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001571 perf_enable();
1572 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001573}
1574
1575static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
1576{
1577 struct perf_event *event;
1578 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001579 u64 interrupts, now;
1580 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001581
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001582 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11001583 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001584 if (event->state != PERF_EVENT_STATE_ACTIVE)
1585 continue;
1586
Peter Zijlstra5d27c232009-12-17 13:16:32 +01001587 if (event->cpu != -1 && event->cpu != smp_processor_id())
1588 continue;
1589
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001590 hwc = &event->hw;
1591
1592 interrupts = hwc->interrupts;
1593 hwc->interrupts = 0;
1594
1595 /*
1596 * unthrottle events on the tick
1597 */
1598 if (interrupts == MAX_INTERRUPTS) {
1599 perf_log_throttle(event, 1);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001600 perf_disable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001601 event->pmu->unthrottle(event);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001602 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001603 }
1604
1605 if (!event->attr.freq || !event->attr.sample_freq)
1606 continue;
1607
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001608 perf_disable();
Peter Zijlstraabd50712010-01-26 18:50:16 +01001609 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001610 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001611 delta = now - hwc->freq_count_stamp;
1612 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001613
Peter Zijlstraabd50712010-01-26 18:50:16 +01001614 if (delta > 0)
1615 perf_adjust_period(event, TICK_NSEC, delta);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001616 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001617 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001618 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001619}
1620
1621/*
1622 * Round-robin a context's events:
1623 */
1624static void rotate_ctx(struct perf_event_context *ctx)
1625{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001626 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001627
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001628 /* Rotate the first entry last of non-pinned groups */
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001629 list_rotate_left(&ctx->flexible_groups);
1630
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001631 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001632}
1633
Peter Zijlstra49f47432009-12-27 11:51:52 +01001634void perf_event_task_tick(struct task_struct *curr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001635{
1636 struct perf_cpu_context *cpuctx;
1637 struct perf_event_context *ctx;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001638 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001639
1640 if (!atomic_read(&nr_events))
1641 return;
1642
Peter Zijlstra49f47432009-12-27 11:51:52 +01001643 cpuctx = &__get_cpu_var(perf_cpu_context);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001644 if (cpuctx->ctx.nr_events &&
1645 cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1646 rotate = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001647
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001648 ctx = curr->perf_event_ctxp;
1649 if (ctx && ctx->nr_events && ctx->nr_events != ctx->nr_active)
1650 rotate = 1;
Peter Zijlstra9717e6c2010-01-28 13:57:44 +01001651
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001652 perf_ctx_adjust_freq(&cpuctx->ctx);
1653 if (ctx)
1654 perf_ctx_adjust_freq(ctx);
1655
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001656 if (!rotate)
1657 return;
1658
1659 perf_disable();
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001660 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001661 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001662 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001663
1664 rotate_ctx(&cpuctx->ctx);
1665 if (ctx)
1666 rotate_ctx(ctx);
1667
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001668 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001669 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001670 task_ctx_sched_in(curr, EVENT_FLEXIBLE);
Peter Zijlstra9717e6c2010-01-28 13:57:44 +01001671 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001672}
1673
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001674static int event_enable_on_exec(struct perf_event *event,
1675 struct perf_event_context *ctx)
1676{
1677 if (!event->attr.enable_on_exec)
1678 return 0;
1679
1680 event->attr.enable_on_exec = 0;
1681 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1682 return 0;
1683
1684 __perf_event_mark_enabled(event, ctx);
1685
1686 return 1;
1687}
1688
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001689/*
1690 * Enable all of a task's events that have been marked enable-on-exec.
1691 * This expects task == current.
1692 */
1693static void perf_event_enable_on_exec(struct task_struct *task)
1694{
1695 struct perf_event_context *ctx;
1696 struct perf_event *event;
1697 unsigned long flags;
1698 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001699 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001700
1701 local_irq_save(flags);
1702 ctx = task->perf_event_ctxp;
1703 if (!ctx || !ctx->nr_events)
1704 goto out;
1705
1706 __perf_event_task_sched_out(ctx);
1707
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001708 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001709
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001710 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1711 ret = event_enable_on_exec(event, ctx);
1712 if (ret)
1713 enabled = 1;
1714 }
1715
1716 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1717 ret = event_enable_on_exec(event, ctx);
1718 if (ret)
1719 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001720 }
1721
1722 /*
1723 * Unclone this context if we enabled any event.
1724 */
1725 if (enabled)
1726 unclone_ctx(ctx);
1727
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001728 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001729
Peter Zijlstra49f47432009-12-27 11:51:52 +01001730 perf_event_task_sched_in(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001731 out:
1732 local_irq_restore(flags);
1733}
1734
1735/*
1736 * Cross CPU call to read the hardware event
1737 */
1738static void __perf_event_read(void *info)
1739{
1740 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1741 struct perf_event *event = info;
1742 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001743
1744 /*
1745 * If this is a task context, we need to check whether it is
1746 * the current task context of this cpu. If not it has been
1747 * scheduled out before the smp call arrived. In that case
1748 * event->count would have been updated to a recent sample
1749 * when the event was scheduled out.
1750 */
1751 if (ctx->task && cpuctx->task_ctx != ctx)
1752 return;
1753
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001754 raw_spin_lock(&ctx->lock);
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001755 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001756 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001757 raw_spin_unlock(&ctx->lock);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001758
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001759 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001760}
1761
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001762static inline u64 perf_event_count(struct perf_event *event)
1763{
Peter Zijlstrae7850592010-05-21 14:43:08 +02001764 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001765}
1766
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001767static u64 perf_event_read(struct perf_event *event)
1768{
1769 /*
1770 * If event is enabled and currently active on a CPU, update the
1771 * value in the event structure:
1772 */
1773 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1774 smp_call_function_single(event->oncpu,
1775 __perf_event_read, event, 1);
1776 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001777 struct perf_event_context *ctx = event->ctx;
1778 unsigned long flags;
1779
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001780 raw_spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001781 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001782 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001783 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001784 }
1785
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001786 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001787}
1788
1789/*
1790 * Initialize the perf_event context in a task_struct:
1791 */
1792static void
1793__perf_event_init_context(struct perf_event_context *ctx,
1794 struct task_struct *task)
1795{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001796 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001797 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001798 INIT_LIST_HEAD(&ctx->pinned_groups);
1799 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001800 INIT_LIST_HEAD(&ctx->event_list);
1801 atomic_set(&ctx->refcount, 1);
1802 ctx->task = task;
1803}
1804
1805static struct perf_event_context *find_get_context(pid_t pid, int cpu)
1806{
1807 struct perf_event_context *ctx;
1808 struct perf_cpu_context *cpuctx;
1809 struct task_struct *task;
1810 unsigned long flags;
1811 int err;
1812
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001813 if (pid == -1 && cpu != -1) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001814 /* Must be root to operate on a CPU event: */
1815 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1816 return ERR_PTR(-EACCES);
1817
Paul Mackerras0f624e72009-12-15 19:40:32 +11001818 if (cpu < 0 || cpu >= nr_cpumask_bits)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001819 return ERR_PTR(-EINVAL);
1820
1821 /*
1822 * We could be clever and allow to attach a event to an
1823 * offline CPU and activate it when the CPU comes up, but
1824 * that's for later.
1825 */
Rusty Russellf6325e32009-12-17 11:43:08 -06001826 if (!cpu_online(cpu))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001827 return ERR_PTR(-ENODEV);
1828
1829 cpuctx = &per_cpu(perf_cpu_context, cpu);
1830 ctx = &cpuctx->ctx;
1831 get_ctx(ctx);
1832
1833 return ctx;
1834 }
1835
1836 rcu_read_lock();
1837 if (!pid)
1838 task = current;
1839 else
1840 task = find_task_by_vpid(pid);
1841 if (task)
1842 get_task_struct(task);
1843 rcu_read_unlock();
1844
1845 if (!task)
1846 return ERR_PTR(-ESRCH);
1847
1848 /*
1849 * Can't attach events to a dying task.
1850 */
1851 err = -ESRCH;
1852 if (task->flags & PF_EXITING)
1853 goto errout;
1854
1855 /* Reuse ptrace permission checks for now. */
1856 err = -EACCES;
1857 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1858 goto errout;
1859
1860 retry:
1861 ctx = perf_lock_task_context(task, &flags);
1862 if (ctx) {
1863 unclone_ctx(ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001864 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001865 }
1866
1867 if (!ctx) {
Xiao Guangrongaa5452d2009-12-09 11:28:13 +08001868 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001869 err = -ENOMEM;
1870 if (!ctx)
1871 goto errout;
1872 __perf_event_init_context(ctx, task);
1873 get_ctx(ctx);
1874 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
1875 /*
1876 * We raced with some other task; use
1877 * the context they set.
1878 */
1879 kfree(ctx);
1880 goto retry;
1881 }
1882 get_task_struct(task);
1883 }
1884
1885 put_task_struct(task);
1886 return ctx;
1887
1888 errout:
1889 put_task_struct(task);
1890 return ERR_PTR(err);
1891}
1892
Li Zefan6fb29152009-10-15 11:21:42 +08001893static void perf_event_free_filter(struct perf_event *event);
1894
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001895static void free_event_rcu(struct rcu_head *head)
1896{
1897 struct perf_event *event;
1898
1899 event = container_of(head, struct perf_event, rcu_head);
1900 if (event->ns)
1901 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08001902 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001903 kfree(event);
1904}
1905
1906static void perf_pending_sync(struct perf_event *event);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02001907static void perf_buffer_put(struct perf_buffer *buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001908
1909static void free_event(struct perf_event *event)
1910{
1911 perf_pending_sync(event);
1912
1913 if (!event->parent) {
1914 atomic_dec(&nr_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01001915 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001916 atomic_dec(&nr_mmap_events);
1917 if (event->attr.comm)
1918 atomic_dec(&nr_comm_events);
1919 if (event->attr.task)
1920 atomic_dec(&nr_task_events);
1921 }
1922
Peter Zijlstraca5135e2010-05-28 19:33:23 +02001923 if (event->buffer) {
1924 perf_buffer_put(event->buffer);
1925 event->buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001926 }
1927
1928 if (event->destroy)
1929 event->destroy(event);
1930
1931 put_ctx(event->ctx);
1932 call_rcu(&event->rcu_head, free_event_rcu);
1933}
1934
Arjan van de Venfb0459d2009-09-25 12:25:56 +02001935int perf_event_release_kernel(struct perf_event *event)
1936{
1937 struct perf_event_context *ctx = event->ctx;
1938
Peter Zijlstra050735b2010-05-11 11:51:53 +02001939 /*
1940 * Remove from the PMU, can't get re-enabled since we got
1941 * here because the last ref went.
1942 */
1943 perf_event_disable(event);
1944
Arjan van de Venfb0459d2009-09-25 12:25:56 +02001945 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02001946 /*
1947 * There are two ways this annotation is useful:
1948 *
1949 * 1) there is a lock recursion from perf_event_exit_task
1950 * see the comment there.
1951 *
1952 * 2) there is a lock-inversion with mmap_sem through
1953 * perf_event_read_group(), which takes faults while
1954 * holding ctx->mutex, however this is called after
1955 * the last filedesc died, so there is no possibility
1956 * to trigger the AB-BA case.
1957 */
1958 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02001959 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001960 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02001961 list_del_event(event, ctx);
Peter Zijlstra050735b2010-05-11 11:51:53 +02001962 raw_spin_unlock_irq(&ctx->lock);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02001963 mutex_unlock(&ctx->mutex);
1964
1965 mutex_lock(&event->owner->perf_event_mutex);
1966 list_del_init(&event->owner_entry);
1967 mutex_unlock(&event->owner->perf_event_mutex);
1968 put_task_struct(event->owner);
1969
1970 free_event(event);
1971
1972 return 0;
1973}
1974EXPORT_SYMBOL_GPL(perf_event_release_kernel);
1975
Peter Zijlstraa66a3052009-11-23 11:37:23 +01001976/*
1977 * Called when the last reference to the file is gone.
1978 */
1979static int perf_release(struct inode *inode, struct file *file)
1980{
1981 struct perf_event *event = file->private_data;
1982
1983 file->private_data = NULL;
1984
1985 return perf_event_release_kernel(event);
1986}
1987
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001988static int perf_event_read_size(struct perf_event *event)
1989{
1990 int entry = sizeof(u64); /* value */
1991 int size = 0;
1992 int nr = 1;
1993
1994 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1995 size += sizeof(u64);
1996
1997 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1998 size += sizeof(u64);
1999
2000 if (event->attr.read_format & PERF_FORMAT_ID)
2001 entry += sizeof(u64);
2002
2003 if (event->attr.read_format & PERF_FORMAT_GROUP) {
2004 nr += event->group_leader->nr_siblings;
2005 size += sizeof(u64);
2006 }
2007
2008 size += entry * nr;
2009
2010 return size;
2011}
2012
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002013u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002014{
2015 struct perf_event *child;
2016 u64 total = 0;
2017
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002018 *enabled = 0;
2019 *running = 0;
2020
Peter Zijlstra6f105812009-11-20 22:19:56 +01002021 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002022 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002023 *enabled += event->total_time_enabled +
2024 atomic64_read(&event->child_total_time_enabled);
2025 *running += event->total_time_running +
2026 atomic64_read(&event->child_total_time_running);
2027
2028 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002029 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002030 *enabled += child->total_time_enabled;
2031 *running += child->total_time_running;
2032 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002033 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002034
2035 return total;
2036}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002037EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002038
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002039static int perf_event_read_group(struct perf_event *event,
2040 u64 read_format, char __user *buf)
2041{
2042 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01002043 int n = 0, size = 0, ret = -EFAULT;
2044 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002045 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002046 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002047
Peter Zijlstra6f105812009-11-20 22:19:56 +01002048 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002049 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002050
2051 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002052 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2053 values[n++] = enabled;
2054 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2055 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002056 values[n++] = count;
2057 if (read_format & PERF_FORMAT_ID)
2058 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002059
2060 size = n * sizeof(u64);
2061
2062 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01002063 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002064
Peter Zijlstra6f105812009-11-20 22:19:56 +01002065 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002066
2067 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01002068 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002069
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002070 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01002071 if (read_format & PERF_FORMAT_ID)
2072 values[n++] = primary_event_id(sub);
2073
2074 size = n * sizeof(u64);
2075
Stephane Eranian184d3da2009-11-23 21:40:49 -08002076 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01002077 ret = -EFAULT;
2078 goto unlock;
2079 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01002080
2081 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002082 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002083unlock:
2084 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002085
Peter Zijlstraabf48682009-11-20 22:19:49 +01002086 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002087}
2088
2089static int perf_event_read_one(struct perf_event *event,
2090 u64 read_format, char __user *buf)
2091{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002092 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002093 u64 values[4];
2094 int n = 0;
2095
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002096 values[n++] = perf_event_read_value(event, &enabled, &running);
2097 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2098 values[n++] = enabled;
2099 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2100 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002101 if (read_format & PERF_FORMAT_ID)
2102 values[n++] = primary_event_id(event);
2103
2104 if (copy_to_user(buf, values, n * sizeof(u64)))
2105 return -EFAULT;
2106
2107 return n * sizeof(u64);
2108}
2109
2110/*
2111 * Read the performance event - simple non blocking version for now
2112 */
2113static ssize_t
2114perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2115{
2116 u64 read_format = event->attr.read_format;
2117 int ret;
2118
2119 /*
2120 * Return end-of-file for a read on a event that is in
2121 * error state (i.e. because it was pinned but it couldn't be
2122 * scheduled on to the CPU at some point).
2123 */
2124 if (event->state == PERF_EVENT_STATE_ERROR)
2125 return 0;
2126
2127 if (count < perf_event_read_size(event))
2128 return -ENOSPC;
2129
2130 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002131 if (read_format & PERF_FORMAT_GROUP)
2132 ret = perf_event_read_group(event, read_format, buf);
2133 else
2134 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002135
2136 return ret;
2137}
2138
2139static ssize_t
2140perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2141{
2142 struct perf_event *event = file->private_data;
2143
2144 return perf_read_hw(event, buf, count);
2145}
2146
2147static unsigned int perf_poll(struct file *file, poll_table *wait)
2148{
2149 struct perf_event *event = file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002150 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002151 unsigned int events = POLL_HUP;
2152
2153 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002154 buffer = rcu_dereference(event->buffer);
2155 if (buffer)
2156 events = atomic_xchg(&buffer->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002157 rcu_read_unlock();
2158
2159 poll_wait(file, &event->waitq, wait);
2160
2161 return events;
2162}
2163
2164static void perf_event_reset(struct perf_event *event)
2165{
2166 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002167 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002168 perf_event_update_userpage(event);
2169}
2170
2171/*
2172 * Holding the top-level event's child_mutex means that any
2173 * descendant process that has inherited this event will block
2174 * in sync_child_event if it goes to exit, thus satisfying the
2175 * task existence requirements of perf_event_enable/disable.
2176 */
2177static void perf_event_for_each_child(struct perf_event *event,
2178 void (*func)(struct perf_event *))
2179{
2180 struct perf_event *child;
2181
2182 WARN_ON_ONCE(event->ctx->parent_ctx);
2183 mutex_lock(&event->child_mutex);
2184 func(event);
2185 list_for_each_entry(child, &event->child_list, child_list)
2186 func(child);
2187 mutex_unlock(&event->child_mutex);
2188}
2189
2190static void perf_event_for_each(struct perf_event *event,
2191 void (*func)(struct perf_event *))
2192{
2193 struct perf_event_context *ctx = event->ctx;
2194 struct perf_event *sibling;
2195
2196 WARN_ON_ONCE(ctx->parent_ctx);
2197 mutex_lock(&ctx->mutex);
2198 event = event->group_leader;
2199
2200 perf_event_for_each_child(event, func);
2201 func(event);
2202 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2203 perf_event_for_each_child(event, func);
2204 mutex_unlock(&ctx->mutex);
2205}
2206
2207static int perf_event_period(struct perf_event *event, u64 __user *arg)
2208{
2209 struct perf_event_context *ctx = event->ctx;
2210 unsigned long size;
2211 int ret = 0;
2212 u64 value;
2213
2214 if (!event->attr.sample_period)
2215 return -EINVAL;
2216
2217 size = copy_from_user(&value, arg, sizeof(value));
2218 if (size != sizeof(value))
2219 return -EFAULT;
2220
2221 if (!value)
2222 return -EINVAL;
2223
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002224 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002225 if (event->attr.freq) {
2226 if (value > sysctl_perf_event_sample_rate) {
2227 ret = -EINVAL;
2228 goto unlock;
2229 }
2230
2231 event->attr.sample_freq = value;
2232 } else {
2233 event->attr.sample_period = value;
2234 event->hw.sample_period = value;
2235 }
2236unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002237 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002238
2239 return ret;
2240}
2241
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002242static const struct file_operations perf_fops;
2243
2244static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2245{
2246 struct file *file;
2247
2248 file = fget_light(fd, fput_needed);
2249 if (!file)
2250 return ERR_PTR(-EBADF);
2251
2252 if (file->f_op != &perf_fops) {
2253 fput_light(file, *fput_needed);
2254 *fput_needed = 0;
2255 return ERR_PTR(-EBADF);
2256 }
2257
2258 return file->private_data;
2259}
2260
2261static int perf_event_set_output(struct perf_event *event,
2262 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08002263static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002264
2265static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2266{
2267 struct perf_event *event = file->private_data;
2268 void (*func)(struct perf_event *);
2269 u32 flags = arg;
2270
2271 switch (cmd) {
2272 case PERF_EVENT_IOC_ENABLE:
2273 func = perf_event_enable;
2274 break;
2275 case PERF_EVENT_IOC_DISABLE:
2276 func = perf_event_disable;
2277 break;
2278 case PERF_EVENT_IOC_RESET:
2279 func = perf_event_reset;
2280 break;
2281
2282 case PERF_EVENT_IOC_REFRESH:
2283 return perf_event_refresh(event, arg);
2284
2285 case PERF_EVENT_IOC_PERIOD:
2286 return perf_event_period(event, (u64 __user *)arg);
2287
2288 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002289 {
2290 struct perf_event *output_event = NULL;
2291 int fput_needed = 0;
2292 int ret;
2293
2294 if (arg != -1) {
2295 output_event = perf_fget_light(arg, &fput_needed);
2296 if (IS_ERR(output_event))
2297 return PTR_ERR(output_event);
2298 }
2299
2300 ret = perf_event_set_output(event, output_event);
2301 if (output_event)
2302 fput_light(output_event->filp, fput_needed);
2303
2304 return ret;
2305 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002306
Li Zefan6fb29152009-10-15 11:21:42 +08002307 case PERF_EVENT_IOC_SET_FILTER:
2308 return perf_event_set_filter(event, (void __user *)arg);
2309
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002310 default:
2311 return -ENOTTY;
2312 }
2313
2314 if (flags & PERF_IOC_FLAG_GROUP)
2315 perf_event_for_each(event, func);
2316 else
2317 perf_event_for_each_child(event, func);
2318
2319 return 0;
2320}
2321
2322int perf_event_task_enable(void)
2323{
2324 struct perf_event *event;
2325
2326 mutex_lock(&current->perf_event_mutex);
2327 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2328 perf_event_for_each_child(event, perf_event_enable);
2329 mutex_unlock(&current->perf_event_mutex);
2330
2331 return 0;
2332}
2333
2334int perf_event_task_disable(void)
2335{
2336 struct perf_event *event;
2337
2338 mutex_lock(&current->perf_event_mutex);
2339 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2340 perf_event_for_each_child(event, perf_event_disable);
2341 mutex_unlock(&current->perf_event_mutex);
2342
2343 return 0;
2344}
2345
2346#ifndef PERF_EVENT_INDEX_OFFSET
2347# define PERF_EVENT_INDEX_OFFSET 0
2348#endif
2349
2350static int perf_event_index(struct perf_event *event)
2351{
2352 if (event->state != PERF_EVENT_STATE_ACTIVE)
2353 return 0;
2354
2355 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2356}
2357
2358/*
2359 * Callers need to ensure there can be no nesting of this function, otherwise
2360 * the seqlock logic goes bad. We can not serialize this because the arch
2361 * code calls this from NMI context.
2362 */
2363void perf_event_update_userpage(struct perf_event *event)
2364{
2365 struct perf_event_mmap_page *userpg;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002366 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002367
2368 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002369 buffer = rcu_dereference(event->buffer);
2370 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002371 goto unlock;
2372
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002373 userpg = buffer->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002374
2375 /*
2376 * Disable preemption so as to not let the corresponding user-space
2377 * spin too long if we get preempted.
2378 */
2379 preempt_disable();
2380 ++userpg->lock;
2381 barrier();
2382 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002383 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002384 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02002385 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002386
2387 userpg->time_enabled = event->total_time_enabled +
2388 atomic64_read(&event->child_total_time_enabled);
2389
2390 userpg->time_running = event->total_time_running +
2391 atomic64_read(&event->child_total_time_running);
2392
2393 barrier();
2394 ++userpg->lock;
2395 preempt_enable();
2396unlock:
2397 rcu_read_unlock();
2398}
2399
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002400static unsigned long perf_data_size(struct perf_buffer *buffer);
2401
2402static void
2403perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2404{
2405 long max_size = perf_data_size(buffer);
2406
2407 if (watermark)
2408 buffer->watermark = min(max_size, watermark);
2409
2410 if (!buffer->watermark)
2411 buffer->watermark = max_size / 2;
2412
2413 if (flags & PERF_BUFFER_WRITABLE)
2414 buffer->writable = 1;
2415
2416 atomic_set(&buffer->refcount, 1);
2417}
2418
Peter Zijlstra906010b2009-09-21 16:08:49 +02002419#ifndef CONFIG_PERF_USE_VMALLOC
2420
2421/*
2422 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2423 */
2424
2425static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002426perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002427{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002428 if (pgoff > buffer->nr_pages)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002429 return NULL;
2430
2431 if (pgoff == 0)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002432 return virt_to_page(buffer->user_page);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002433
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002434 return virt_to_page(buffer->data_pages[pgoff - 1]);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002435}
2436
Peter Zijlstraa19d35c2010-05-17 18:48:00 +02002437static void *perf_mmap_alloc_page(int cpu)
2438{
2439 struct page *page;
2440 int node;
2441
2442 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2443 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2444 if (!page)
2445 return NULL;
2446
2447 return page_address(page);
2448}
2449
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002450static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002451perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002452{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002453 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002454 unsigned long size;
2455 int i;
2456
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002457 size = sizeof(struct perf_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002458 size += nr_pages * sizeof(void *);
2459
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002460 buffer = kzalloc(size, GFP_KERNEL);
2461 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002462 goto fail;
2463
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002464 buffer->user_page = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002465 if (!buffer->user_page)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002466 goto fail_user_page;
2467
2468 for (i = 0; i < nr_pages; i++) {
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002469 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002470 if (!buffer->data_pages[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002471 goto fail_data_pages;
2472 }
2473
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002474 buffer->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002475
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002476 perf_buffer_init(buffer, watermark, flags);
2477
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002478 return buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002479
2480fail_data_pages:
2481 for (i--; i >= 0; i--)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002482 free_page((unsigned long)buffer->data_pages[i]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002483
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002484 free_page((unsigned long)buffer->user_page);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002485
2486fail_user_page:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002487 kfree(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002488
2489fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02002490 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002491}
2492
2493static void perf_mmap_free_page(unsigned long addr)
2494{
2495 struct page *page = virt_to_page((void *)addr);
2496
2497 page->mapping = NULL;
2498 __free_page(page);
2499}
2500
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002501static void perf_buffer_free(struct perf_buffer *buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002502{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002503 int i;
2504
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002505 perf_mmap_free_page((unsigned long)buffer->user_page);
2506 for (i = 0; i < buffer->nr_pages; i++)
2507 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2508 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002509}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002510
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002511static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002512{
2513 return 0;
2514}
2515
Peter Zijlstra906010b2009-09-21 16:08:49 +02002516#else
2517
2518/*
2519 * Back perf_mmap() with vmalloc memory.
2520 *
2521 * Required for architectures that have d-cache aliasing issues.
2522 */
2523
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002524static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002525{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002526 return buffer->page_order;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002527}
2528
Peter Zijlstra906010b2009-09-21 16:08:49 +02002529static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002530perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002531{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002532 if (pgoff > (1UL << page_order(buffer)))
Peter Zijlstra906010b2009-09-21 16:08:49 +02002533 return NULL;
2534
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002535 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002536}
2537
2538static void perf_mmap_unmark_page(void *addr)
2539{
2540 struct page *page = vmalloc_to_page(addr);
2541
2542 page->mapping = NULL;
2543}
2544
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002545static void perf_buffer_free_work(struct work_struct *work)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002546{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002547 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002548 void *base;
2549 int i, nr;
2550
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002551 buffer = container_of(work, struct perf_buffer, work);
2552 nr = 1 << page_order(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002553
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002554 base = buffer->user_page;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002555 for (i = 0; i < nr + 1; i++)
2556 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2557
2558 vfree(base);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002559 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002560}
2561
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002562static void perf_buffer_free(struct perf_buffer *buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002563{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002564 schedule_work(&buffer->work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002565}
2566
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002567static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002568perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002569{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002570 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002571 unsigned long size;
2572 void *all_buf;
2573
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002574 size = sizeof(struct perf_buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002575 size += sizeof(void *);
2576
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002577 buffer = kzalloc(size, GFP_KERNEL);
2578 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002579 goto fail;
2580
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002581 INIT_WORK(&buffer->work, perf_buffer_free_work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002582
2583 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2584 if (!all_buf)
2585 goto fail_all_buf;
2586
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002587 buffer->user_page = all_buf;
2588 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2589 buffer->page_order = ilog2(nr_pages);
2590 buffer->nr_pages = 1;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002591
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002592 perf_buffer_init(buffer, watermark, flags);
2593
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002594 return buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002595
2596fail_all_buf:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002597 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002598
2599fail:
2600 return NULL;
2601}
2602
2603#endif
2604
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002605static unsigned long perf_data_size(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002606{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002607 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002608}
2609
Peter Zijlstra906010b2009-09-21 16:08:49 +02002610static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2611{
2612 struct perf_event *event = vma->vm_file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002613 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002614 int ret = VM_FAULT_SIGBUS;
2615
2616 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2617 if (vmf->pgoff == 0)
2618 ret = 0;
2619 return ret;
2620 }
2621
2622 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002623 buffer = rcu_dereference(event->buffer);
2624 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002625 goto unlock;
2626
2627 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2628 goto unlock;
2629
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002630 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002631 if (!vmf->page)
2632 goto unlock;
2633
2634 get_page(vmf->page);
2635 vmf->page->mapping = vma->vm_file->f_mapping;
2636 vmf->page->index = vmf->pgoff;
2637
2638 ret = 0;
2639unlock:
2640 rcu_read_unlock();
2641
2642 return ret;
2643}
2644
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002645static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002646{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002647 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002648
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002649 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
2650 perf_buffer_free(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002651}
2652
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002653static struct perf_buffer *perf_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002654{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002655 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002656
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002657 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002658 buffer = rcu_dereference(event->buffer);
2659 if (buffer) {
2660 if (!atomic_inc_not_zero(&buffer->refcount))
2661 buffer = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002662 }
2663 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002664
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002665 return buffer;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002666}
2667
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002668static void perf_buffer_put(struct perf_buffer *buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002669{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002670 if (!atomic_dec_and_test(&buffer->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002671 return;
2672
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002673 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002674}
2675
2676static void perf_mmap_open(struct vm_area_struct *vma)
2677{
2678 struct perf_event *event = vma->vm_file->private_data;
2679
2680 atomic_inc(&event->mmap_count);
2681}
2682
2683static void perf_mmap_close(struct vm_area_struct *vma)
2684{
2685 struct perf_event *event = vma->vm_file->private_data;
2686
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002687 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002688 unsigned long size = perf_data_size(event->buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002689 struct user_struct *user = event->mmap_user;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002690 struct perf_buffer *buffer = event->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002691
Peter Zijlstra906010b2009-09-21 16:08:49 +02002692 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002693 vma->vm_mm->locked_vm -= event->mmap_locked;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002694 rcu_assign_pointer(event->buffer, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002695 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002696
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002697 perf_buffer_put(buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002698 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002699 }
2700}
2701
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002702static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002703 .open = perf_mmap_open,
2704 .close = perf_mmap_close,
2705 .fault = perf_mmap_fault,
2706 .page_mkwrite = perf_mmap_fault,
2707};
2708
2709static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2710{
2711 struct perf_event *event = file->private_data;
2712 unsigned long user_locked, user_lock_limit;
2713 struct user_struct *user = current_user();
2714 unsigned long locked, lock_limit;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002715 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002716 unsigned long vma_size;
2717 unsigned long nr_pages;
2718 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002719 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002720
Peter Zijlstrac7920612010-05-18 10:33:24 +02002721 /*
2722 * Don't allow mmap() of inherited per-task counters. This would
2723 * create a performance issue due to all children writing to the
2724 * same buffer.
2725 */
2726 if (event->cpu == -1 && event->attr.inherit)
2727 return -EINVAL;
2728
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002729 if (!(vma->vm_flags & VM_SHARED))
2730 return -EINVAL;
2731
2732 vma_size = vma->vm_end - vma->vm_start;
2733 nr_pages = (vma_size / PAGE_SIZE) - 1;
2734
2735 /*
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002736 * If we have buffer pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002737 * can do bitmasks instead of modulo.
2738 */
2739 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2740 return -EINVAL;
2741
2742 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2743 return -EINVAL;
2744
2745 if (vma->vm_pgoff != 0)
2746 return -EINVAL;
2747
2748 WARN_ON_ONCE(event->ctx->parent_ctx);
2749 mutex_lock(&event->mmap_mutex);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002750 if (event->buffer) {
2751 if (event->buffer->nr_pages == nr_pages)
2752 atomic_inc(&event->buffer->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002753 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002754 ret = -EINVAL;
2755 goto unlock;
2756 }
2757
2758 user_extra = nr_pages + 1;
2759 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2760
2761 /*
2762 * Increase the limit linearly with more CPUs:
2763 */
2764 user_lock_limit *= num_online_cpus();
2765
2766 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2767
2768 extra = 0;
2769 if (user_locked > user_lock_limit)
2770 extra = user_locked - user_lock_limit;
2771
Jiri Slaby78d7d402010-03-05 13:42:54 -08002772 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002773 lock_limit >>= PAGE_SHIFT;
2774 locked = vma->vm_mm->locked_vm + extra;
2775
2776 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2777 !capable(CAP_IPC_LOCK)) {
2778 ret = -EPERM;
2779 goto unlock;
2780 }
2781
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002782 WARN_ON(event->buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002783
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002784 if (vma->vm_flags & VM_WRITE)
2785 flags |= PERF_BUFFER_WRITABLE;
2786
2787 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
2788 event->cpu, flags);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002789 if (!buffer) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002790 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002791 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002792 }
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002793 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002794
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002795 atomic_long_add(user_extra, &user->locked_vm);
2796 event->mmap_locked = extra;
2797 event->mmap_user = get_current_user();
2798 vma->vm_mm->locked_vm += event->mmap_locked;
2799
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002800unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002801 if (!ret)
2802 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002803 mutex_unlock(&event->mmap_mutex);
2804
2805 vma->vm_flags |= VM_RESERVED;
2806 vma->vm_ops = &perf_mmap_vmops;
2807
2808 return ret;
2809}
2810
2811static int perf_fasync(int fd, struct file *filp, int on)
2812{
2813 struct inode *inode = filp->f_path.dentry->d_inode;
2814 struct perf_event *event = filp->private_data;
2815 int retval;
2816
2817 mutex_lock(&inode->i_mutex);
2818 retval = fasync_helper(fd, filp, on, &event->fasync);
2819 mutex_unlock(&inode->i_mutex);
2820
2821 if (retval < 0)
2822 return retval;
2823
2824 return 0;
2825}
2826
2827static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01002828 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002829 .release = perf_release,
2830 .read = perf_read,
2831 .poll = perf_poll,
2832 .unlocked_ioctl = perf_ioctl,
2833 .compat_ioctl = perf_ioctl,
2834 .mmap = perf_mmap,
2835 .fasync = perf_fasync,
2836};
2837
2838/*
2839 * Perf event wakeup
2840 *
2841 * If there's data, ensure we set the poll() state and publish everything
2842 * to user-space before waking everybody up.
2843 */
2844
2845void perf_event_wakeup(struct perf_event *event)
2846{
2847 wake_up_all(&event->waitq);
2848
2849 if (event->pending_kill) {
2850 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
2851 event->pending_kill = 0;
2852 }
2853}
2854
2855/*
2856 * Pending wakeups
2857 *
2858 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2859 *
2860 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2861 * single linked list and use cmpxchg() to add entries lockless.
2862 */
2863
2864static void perf_pending_event(struct perf_pending_entry *entry)
2865{
2866 struct perf_event *event = container_of(entry,
2867 struct perf_event, pending);
2868
2869 if (event->pending_disable) {
2870 event->pending_disable = 0;
2871 __perf_event_disable(event);
2872 }
2873
2874 if (event->pending_wakeup) {
2875 event->pending_wakeup = 0;
2876 perf_event_wakeup(event);
2877 }
2878}
2879
2880#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2881
2882static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
2883 PENDING_TAIL,
2884};
2885
2886static void perf_pending_queue(struct perf_pending_entry *entry,
2887 void (*func)(struct perf_pending_entry *))
2888{
2889 struct perf_pending_entry **head;
2890
2891 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
2892 return;
2893
2894 entry->func = func;
2895
2896 head = &get_cpu_var(perf_pending_head);
2897
2898 do {
2899 entry->next = *head;
2900 } while (cmpxchg(head, entry->next, entry) != entry->next);
2901
2902 set_perf_event_pending();
2903
2904 put_cpu_var(perf_pending_head);
2905}
2906
2907static int __perf_pending_run(void)
2908{
2909 struct perf_pending_entry *list;
2910 int nr = 0;
2911
2912 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2913 while (list != PENDING_TAIL) {
2914 void (*func)(struct perf_pending_entry *);
2915 struct perf_pending_entry *entry = list;
2916
2917 list = list->next;
2918
2919 func = entry->func;
2920 entry->next = NULL;
2921 /*
2922 * Ensure we observe the unqueue before we issue the wakeup,
2923 * so that we won't be waiting forever.
2924 * -- see perf_not_pending().
2925 */
2926 smp_wmb();
2927
2928 func(entry);
2929 nr++;
2930 }
2931
2932 return nr;
2933}
2934
2935static inline int perf_not_pending(struct perf_event *event)
2936{
2937 /*
2938 * If we flush on whatever cpu we run, there is a chance we don't
2939 * need to wait.
2940 */
2941 get_cpu();
2942 __perf_pending_run();
2943 put_cpu();
2944
2945 /*
2946 * Ensure we see the proper queue state before going to sleep
2947 * so that we do not miss the wakeup. -- see perf_pending_handle()
2948 */
2949 smp_rmb();
2950 return event->pending.next == NULL;
2951}
2952
2953static void perf_pending_sync(struct perf_event *event)
2954{
2955 wait_event(event->waitq, perf_not_pending(event));
2956}
2957
2958void perf_event_do_pending(void)
2959{
2960 __perf_pending_run();
2961}
2962
2963/*
2964 * Callchain support -- arch specific
2965 */
2966
2967__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2968{
2969 return NULL;
2970}
2971
Frederic Weisbecker5331d7b2010-03-04 21:15:56 +01002972
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002973/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08002974 * We assume there is only KVM supporting the callbacks.
2975 * Later on, we might change it to a list if there is
2976 * another virtualization implementation supporting the callbacks.
2977 */
2978struct perf_guest_info_callbacks *perf_guest_cbs;
2979
2980int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
2981{
2982 perf_guest_cbs = cbs;
2983 return 0;
2984}
2985EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
2986
2987int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
2988{
2989 perf_guest_cbs = NULL;
2990 return 0;
2991}
2992EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
2993
2994/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002995 * Output
2996 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002997static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002998 unsigned long offset, unsigned long head)
2999{
3000 unsigned long mask;
3001
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003002 if (!buffer->writable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003003 return true;
3004
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003005 mask = perf_data_size(buffer) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003006
3007 offset = (offset - tail) & mask;
3008 head = (head - tail) & mask;
3009
3010 if ((int)(head - offset) < 0)
3011 return false;
3012
3013 return true;
3014}
3015
3016static void perf_output_wakeup(struct perf_output_handle *handle)
3017{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003018 atomic_set(&handle->buffer->poll, POLL_IN);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003019
3020 if (handle->nmi) {
3021 handle->event->pending_wakeup = 1;
3022 perf_pending_queue(&handle->event->pending,
3023 perf_pending_event);
3024 } else
3025 perf_event_wakeup(handle->event);
3026}
3027
3028/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003029 * We need to ensure a later event_id doesn't publish a head when a former
Peter Zijlstraef607772010-05-18 10:50:41 +02003030 * event isn't done writing. However since we need to deal with NMIs we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003031 * cannot fully serialize things.
3032 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003033 * We only publish the head (and generate a wakeup) when the outer-most
Peter Zijlstraef607772010-05-18 10:50:41 +02003034 * event completes.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003035 */
Peter Zijlstraef607772010-05-18 10:50:41 +02003036static void perf_output_get_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003037{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003038 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003039
Peter Zijlstraef607772010-05-18 10:50:41 +02003040 preempt_disable();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003041 local_inc(&buffer->nest);
3042 handle->wakeup = local_read(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003043}
3044
Peter Zijlstraef607772010-05-18 10:50:41 +02003045static void perf_output_put_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003046{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003047 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003048 unsigned long head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003049
3050again:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003051 head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003052
3053 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003054 * IRQ/NMI can happen here, which means we can miss a head update.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003055 */
3056
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003057 if (!local_dec_and_test(&buffer->nest))
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003058 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003059
3060 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003061 * Publish the known good head. Rely on the full barrier implied
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003062 * by atomic_dec_and_test() order the buffer->head read and this
Peter Zijlstraef607772010-05-18 10:50:41 +02003063 * write.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003064 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003065 buffer->user_page->data_head = head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003066
Peter Zijlstraef607772010-05-18 10:50:41 +02003067 /*
3068 * Now check if we missed an update, rely on the (compiler)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003069 * barrier in atomic_dec_and_test() to re-read buffer->head.
Peter Zijlstraef607772010-05-18 10:50:41 +02003070 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003071 if (unlikely(head != local_read(&buffer->head))) {
3072 local_inc(&buffer->nest);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003073 goto again;
3074 }
3075
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003076 if (handle->wakeup != local_read(&buffer->wakeup))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003077 perf_output_wakeup(handle);
Peter Zijlstraef607772010-05-18 10:50:41 +02003078
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003079 out:
Peter Zijlstraef607772010-05-18 10:50:41 +02003080 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003081}
3082
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003083__always_inline void perf_output_copy(struct perf_output_handle *handle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003084 const void *buf, unsigned int len)
3085{
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003086 do {
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003087 unsigned long size = min_t(unsigned long, handle->size, len);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003088
3089 memcpy(handle->addr, buf, size);
3090
3091 len -= size;
3092 handle->addr += size;
Frederic Weisbecker74048f82010-05-27 21:34:58 +02003093 buf += size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003094 handle->size -= size;
3095 if (!handle->size) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003096 struct perf_buffer *buffer = handle->buffer;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003097
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003098 handle->page++;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003099 handle->page &= buffer->nr_pages - 1;
3100 handle->addr = buffer->data_pages[handle->page];
3101 handle->size = PAGE_SIZE << page_order(buffer);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003102 }
3103 } while (len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003104}
3105
3106int perf_output_begin(struct perf_output_handle *handle,
3107 struct perf_event *event, unsigned int size,
3108 int nmi, int sample)
3109{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003110 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003111 unsigned long tail, offset, head;
3112 int have_lost;
3113 struct {
3114 struct perf_event_header header;
3115 u64 id;
3116 u64 lost;
3117 } lost_event;
3118
3119 rcu_read_lock();
3120 /*
3121 * For inherited events we send all the output towards the parent.
3122 */
3123 if (event->parent)
3124 event = event->parent;
3125
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003126 buffer = rcu_dereference(event->buffer);
3127 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003128 goto out;
3129
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003130 handle->buffer = buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003131 handle->event = event;
3132 handle->nmi = nmi;
3133 handle->sample = sample;
3134
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003135 if (!buffer->nr_pages)
Stephane Eranian00d1d0b2010-05-17 12:46:01 +02003136 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003137
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003138 have_lost = local_read(&buffer->lost);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003139 if (have_lost)
3140 size += sizeof(lost_event);
3141
Peter Zijlstraef607772010-05-18 10:50:41 +02003142 perf_output_get_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003143
3144 do {
3145 /*
3146 * Userspace could choose to issue a mb() before updating the
3147 * tail pointer. So that all reads will be completed before the
3148 * write is issued.
3149 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003150 tail = ACCESS_ONCE(buffer->user_page->data_tail);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003151 smp_rmb();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003152 offset = head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003153 head += size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003154 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003155 goto fail;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003156 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003157
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003158 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3159 local_add(buffer->watermark, &buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003160
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003161 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3162 handle->page &= buffer->nr_pages - 1;
3163 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3164 handle->addr = buffer->data_pages[handle->page];
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003165 handle->addr += handle->size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003166 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003167
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003168 if (have_lost) {
3169 lost_event.header.type = PERF_RECORD_LOST;
3170 lost_event.header.misc = 0;
3171 lost_event.header.size = sizeof(lost_event);
3172 lost_event.id = event->id;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003173 lost_event.lost = local_xchg(&buffer->lost, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003174
3175 perf_output_put(handle, lost_event);
3176 }
3177
3178 return 0;
3179
3180fail:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003181 local_inc(&buffer->lost);
Peter Zijlstraef607772010-05-18 10:50:41 +02003182 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003183out:
3184 rcu_read_unlock();
3185
3186 return -ENOSPC;
3187}
3188
3189void perf_output_end(struct perf_output_handle *handle)
3190{
3191 struct perf_event *event = handle->event;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003192 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003193
3194 int wakeup_events = event->attr.wakeup_events;
3195
3196 if (handle->sample && wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003197 int events = local_inc_return(&buffer->events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003198 if (events >= wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003199 local_sub(wakeup_events, &buffer->events);
3200 local_inc(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003201 }
3202 }
3203
Peter Zijlstraef607772010-05-18 10:50:41 +02003204 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003205 rcu_read_unlock();
3206}
3207
3208static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3209{
3210 /*
3211 * only top level events have the pid namespace they were created in
3212 */
3213 if (event->parent)
3214 event = event->parent;
3215
3216 return task_tgid_nr_ns(p, event->ns);
3217}
3218
3219static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3220{
3221 /*
3222 * only top level events have the pid namespace they were created in
3223 */
3224 if (event->parent)
3225 event = event->parent;
3226
3227 return task_pid_nr_ns(p, event->ns);
3228}
3229
3230static void perf_output_read_one(struct perf_output_handle *handle,
3231 struct perf_event *event)
3232{
3233 u64 read_format = event->attr.read_format;
3234 u64 values[4];
3235 int n = 0;
3236
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003237 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003238 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3239 values[n++] = event->total_time_enabled +
3240 atomic64_read(&event->child_total_time_enabled);
3241 }
3242 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3243 values[n++] = event->total_time_running +
3244 atomic64_read(&event->child_total_time_running);
3245 }
3246 if (read_format & PERF_FORMAT_ID)
3247 values[n++] = primary_event_id(event);
3248
3249 perf_output_copy(handle, values, n * sizeof(u64));
3250}
3251
3252/*
3253 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3254 */
3255static void perf_output_read_group(struct perf_output_handle *handle,
3256 struct perf_event *event)
3257{
3258 struct perf_event *leader = event->group_leader, *sub;
3259 u64 read_format = event->attr.read_format;
3260 u64 values[5];
3261 int n = 0;
3262
3263 values[n++] = 1 + leader->nr_siblings;
3264
3265 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3266 values[n++] = leader->total_time_enabled;
3267
3268 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3269 values[n++] = leader->total_time_running;
3270
3271 if (leader != event)
3272 leader->pmu->read(leader);
3273
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003274 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003275 if (read_format & PERF_FORMAT_ID)
3276 values[n++] = primary_event_id(leader);
3277
3278 perf_output_copy(handle, values, n * sizeof(u64));
3279
3280 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3281 n = 0;
3282
3283 if (sub != event)
3284 sub->pmu->read(sub);
3285
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003286 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003287 if (read_format & PERF_FORMAT_ID)
3288 values[n++] = primary_event_id(sub);
3289
3290 perf_output_copy(handle, values, n * sizeof(u64));
3291 }
3292}
3293
3294static void perf_output_read(struct perf_output_handle *handle,
3295 struct perf_event *event)
3296{
3297 if (event->attr.read_format & PERF_FORMAT_GROUP)
3298 perf_output_read_group(handle, event);
3299 else
3300 perf_output_read_one(handle, event);
3301}
3302
3303void perf_output_sample(struct perf_output_handle *handle,
3304 struct perf_event_header *header,
3305 struct perf_sample_data *data,
3306 struct perf_event *event)
3307{
3308 u64 sample_type = data->type;
3309
3310 perf_output_put(handle, *header);
3311
3312 if (sample_type & PERF_SAMPLE_IP)
3313 perf_output_put(handle, data->ip);
3314
3315 if (sample_type & PERF_SAMPLE_TID)
3316 perf_output_put(handle, data->tid_entry);
3317
3318 if (sample_type & PERF_SAMPLE_TIME)
3319 perf_output_put(handle, data->time);
3320
3321 if (sample_type & PERF_SAMPLE_ADDR)
3322 perf_output_put(handle, data->addr);
3323
3324 if (sample_type & PERF_SAMPLE_ID)
3325 perf_output_put(handle, data->id);
3326
3327 if (sample_type & PERF_SAMPLE_STREAM_ID)
3328 perf_output_put(handle, data->stream_id);
3329
3330 if (sample_type & PERF_SAMPLE_CPU)
3331 perf_output_put(handle, data->cpu_entry);
3332
3333 if (sample_type & PERF_SAMPLE_PERIOD)
3334 perf_output_put(handle, data->period);
3335
3336 if (sample_type & PERF_SAMPLE_READ)
3337 perf_output_read(handle, event);
3338
3339 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3340 if (data->callchain) {
3341 int size = 1;
3342
3343 if (data->callchain)
3344 size += data->callchain->nr;
3345
3346 size *= sizeof(u64);
3347
3348 perf_output_copy(handle, data->callchain, size);
3349 } else {
3350 u64 nr = 0;
3351 perf_output_put(handle, nr);
3352 }
3353 }
3354
3355 if (sample_type & PERF_SAMPLE_RAW) {
3356 if (data->raw) {
3357 perf_output_put(handle, data->raw->size);
3358 perf_output_copy(handle, data->raw->data,
3359 data->raw->size);
3360 } else {
3361 struct {
3362 u32 size;
3363 u32 data;
3364 } raw = {
3365 .size = sizeof(u32),
3366 .data = 0,
3367 };
3368 perf_output_put(handle, raw);
3369 }
3370 }
3371}
3372
3373void perf_prepare_sample(struct perf_event_header *header,
3374 struct perf_sample_data *data,
3375 struct perf_event *event,
3376 struct pt_regs *regs)
3377{
3378 u64 sample_type = event->attr.sample_type;
3379
3380 data->type = sample_type;
3381
3382 header->type = PERF_RECORD_SAMPLE;
3383 header->size = sizeof(*header);
3384
3385 header->misc = 0;
3386 header->misc |= perf_misc_flags(regs);
3387
3388 if (sample_type & PERF_SAMPLE_IP) {
3389 data->ip = perf_instruction_pointer(regs);
3390
3391 header->size += sizeof(data->ip);
3392 }
3393
3394 if (sample_type & PERF_SAMPLE_TID) {
3395 /* namespace issues */
3396 data->tid_entry.pid = perf_event_pid(event, current);
3397 data->tid_entry.tid = perf_event_tid(event, current);
3398
3399 header->size += sizeof(data->tid_entry);
3400 }
3401
3402 if (sample_type & PERF_SAMPLE_TIME) {
3403 data->time = perf_clock();
3404
3405 header->size += sizeof(data->time);
3406 }
3407
3408 if (sample_type & PERF_SAMPLE_ADDR)
3409 header->size += sizeof(data->addr);
3410
3411 if (sample_type & PERF_SAMPLE_ID) {
3412 data->id = primary_event_id(event);
3413
3414 header->size += sizeof(data->id);
3415 }
3416
3417 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3418 data->stream_id = event->id;
3419
3420 header->size += sizeof(data->stream_id);
3421 }
3422
3423 if (sample_type & PERF_SAMPLE_CPU) {
3424 data->cpu_entry.cpu = raw_smp_processor_id();
3425 data->cpu_entry.reserved = 0;
3426
3427 header->size += sizeof(data->cpu_entry);
3428 }
3429
3430 if (sample_type & PERF_SAMPLE_PERIOD)
3431 header->size += sizeof(data->period);
3432
3433 if (sample_type & PERF_SAMPLE_READ)
3434 header->size += perf_event_read_size(event);
3435
3436 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3437 int size = 1;
3438
3439 data->callchain = perf_callchain(regs);
3440
3441 if (data->callchain)
3442 size += data->callchain->nr;
3443
3444 header->size += size * sizeof(u64);
3445 }
3446
3447 if (sample_type & PERF_SAMPLE_RAW) {
3448 int size = sizeof(u32);
3449
3450 if (data->raw)
3451 size += data->raw->size;
3452 else
3453 size += sizeof(u32);
3454
3455 WARN_ON_ONCE(size & (sizeof(u64)-1));
3456 header->size += size;
3457 }
3458}
3459
3460static void perf_event_output(struct perf_event *event, int nmi,
3461 struct perf_sample_data *data,
3462 struct pt_regs *regs)
3463{
3464 struct perf_output_handle handle;
3465 struct perf_event_header header;
3466
3467 perf_prepare_sample(&header, data, event, regs);
3468
3469 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3470 return;
3471
3472 perf_output_sample(&handle, &header, data, event);
3473
3474 perf_output_end(&handle);
3475}
3476
3477/*
3478 * read event_id
3479 */
3480
3481struct perf_read_event {
3482 struct perf_event_header header;
3483
3484 u32 pid;
3485 u32 tid;
3486};
3487
3488static void
3489perf_event_read_event(struct perf_event *event,
3490 struct task_struct *task)
3491{
3492 struct perf_output_handle handle;
3493 struct perf_read_event read_event = {
3494 .header = {
3495 .type = PERF_RECORD_READ,
3496 .misc = 0,
3497 .size = sizeof(read_event) + perf_event_read_size(event),
3498 },
3499 .pid = perf_event_pid(event, task),
3500 .tid = perf_event_tid(event, task),
3501 };
3502 int ret;
3503
3504 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3505 if (ret)
3506 return;
3507
3508 perf_output_put(&handle, read_event);
3509 perf_output_read(&handle, event);
3510
3511 perf_output_end(&handle);
3512}
3513
3514/*
3515 * task tracking -- fork/exit
3516 *
Eric B Munson3af9e852010-05-18 15:30:49 +01003517 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003518 */
3519
3520struct perf_task_event {
3521 struct task_struct *task;
3522 struct perf_event_context *task_ctx;
3523
3524 struct {
3525 struct perf_event_header header;
3526
3527 u32 pid;
3528 u32 ppid;
3529 u32 tid;
3530 u32 ptid;
3531 u64 time;
3532 } event_id;
3533};
3534
3535static void perf_event_task_output(struct perf_event *event,
3536 struct perf_task_event *task_event)
3537{
3538 struct perf_output_handle handle;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003539 struct task_struct *task = task_event->task;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01003540 int size, ret;
3541
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003542 size = task_event->event_id.header.size;
3543 ret = perf_output_begin(&handle, event, size, 0, 0);
3544
Peter Zijlstraef607772010-05-18 10:50:41 +02003545 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003546 return;
3547
3548 task_event->event_id.pid = perf_event_pid(event, task);
3549 task_event->event_id.ppid = perf_event_pid(event, current);
3550
3551 task_event->event_id.tid = perf_event_tid(event, task);
3552 task_event->event_id.ptid = perf_event_tid(event, current);
3553
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003554 perf_output_put(&handle, task_event->event_id);
3555
3556 perf_output_end(&handle);
3557}
3558
3559static int perf_event_task_match(struct perf_event *event)
3560{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003561 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003562 return 0;
3563
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003564 if (event->cpu != -1 && event->cpu != smp_processor_id())
3565 return 0;
3566
Eric B Munson3af9e852010-05-18 15:30:49 +01003567 if (event->attr.comm || event->attr.mmap ||
3568 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003569 return 1;
3570
3571 return 0;
3572}
3573
3574static void perf_event_task_ctx(struct perf_event_context *ctx,
3575 struct perf_task_event *task_event)
3576{
3577 struct perf_event *event;
3578
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003579 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3580 if (perf_event_task_match(event))
3581 perf_event_task_output(event, task_event);
3582 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003583}
3584
3585static void perf_event_task_event(struct perf_task_event *task_event)
3586{
3587 struct perf_cpu_context *cpuctx;
3588 struct perf_event_context *ctx = task_event->task_ctx;
3589
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01003590 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003591 cpuctx = &get_cpu_var(perf_cpu_context);
3592 perf_event_task_ctx(&cpuctx->ctx, task_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003593 if (!ctx)
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003594 ctx = rcu_dereference(current->perf_event_ctxp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003595 if (ctx)
3596 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003597 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003598 rcu_read_unlock();
3599}
3600
3601static void perf_event_task(struct task_struct *task,
3602 struct perf_event_context *task_ctx,
3603 int new)
3604{
3605 struct perf_task_event task_event;
3606
3607 if (!atomic_read(&nr_comm_events) &&
3608 !atomic_read(&nr_mmap_events) &&
3609 !atomic_read(&nr_task_events))
3610 return;
3611
3612 task_event = (struct perf_task_event){
3613 .task = task,
3614 .task_ctx = task_ctx,
3615 .event_id = {
3616 .header = {
3617 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3618 .misc = 0,
3619 .size = sizeof(task_event.event_id),
3620 },
3621 /* .pid */
3622 /* .ppid */
3623 /* .tid */
3624 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003625 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003626 },
3627 };
3628
3629 perf_event_task_event(&task_event);
3630}
3631
3632void perf_event_fork(struct task_struct *task)
3633{
3634 perf_event_task(task, NULL, 1);
3635}
3636
3637/*
3638 * comm tracking
3639 */
3640
3641struct perf_comm_event {
3642 struct task_struct *task;
3643 char *comm;
3644 int comm_size;
3645
3646 struct {
3647 struct perf_event_header header;
3648
3649 u32 pid;
3650 u32 tid;
3651 } event_id;
3652};
3653
3654static void perf_event_comm_output(struct perf_event *event,
3655 struct perf_comm_event *comm_event)
3656{
3657 struct perf_output_handle handle;
3658 int size = comm_event->event_id.header.size;
3659 int ret = perf_output_begin(&handle, event, size, 0, 0);
3660
3661 if (ret)
3662 return;
3663
3664 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3665 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3666
3667 perf_output_put(&handle, comm_event->event_id);
3668 perf_output_copy(&handle, comm_event->comm,
3669 comm_event->comm_size);
3670 perf_output_end(&handle);
3671}
3672
3673static int perf_event_comm_match(struct perf_event *event)
3674{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003675 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003676 return 0;
3677
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003678 if (event->cpu != -1 && event->cpu != smp_processor_id())
3679 return 0;
3680
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003681 if (event->attr.comm)
3682 return 1;
3683
3684 return 0;
3685}
3686
3687static void perf_event_comm_ctx(struct perf_event_context *ctx,
3688 struct perf_comm_event *comm_event)
3689{
3690 struct perf_event *event;
3691
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003692 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3693 if (perf_event_comm_match(event))
3694 perf_event_comm_output(event, comm_event);
3695 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003696}
3697
3698static void perf_event_comm_event(struct perf_comm_event *comm_event)
3699{
3700 struct perf_cpu_context *cpuctx;
3701 struct perf_event_context *ctx;
3702 unsigned int size;
3703 char comm[TASK_COMM_LEN];
3704
3705 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01003706 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003707 size = ALIGN(strlen(comm)+1, sizeof(u64));
3708
3709 comm_event->comm = comm;
3710 comm_event->comm_size = size;
3711
3712 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3713
Peter Zijlstraf6595f32009-11-20 22:19:47 +01003714 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003715 cpuctx = &get_cpu_var(perf_cpu_context);
3716 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003717 ctx = rcu_dereference(current->perf_event_ctxp);
3718 if (ctx)
3719 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003720 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003721 rcu_read_unlock();
3722}
3723
3724void perf_event_comm(struct task_struct *task)
3725{
3726 struct perf_comm_event comm_event;
3727
3728 if (task->perf_event_ctxp)
3729 perf_event_enable_on_exec(task);
3730
3731 if (!atomic_read(&nr_comm_events))
3732 return;
3733
3734 comm_event = (struct perf_comm_event){
3735 .task = task,
3736 /* .comm */
3737 /* .comm_size */
3738 .event_id = {
3739 .header = {
3740 .type = PERF_RECORD_COMM,
3741 .misc = 0,
3742 /* .size */
3743 },
3744 /* .pid */
3745 /* .tid */
3746 },
3747 };
3748
3749 perf_event_comm_event(&comm_event);
3750}
3751
3752/*
3753 * mmap tracking
3754 */
3755
3756struct perf_mmap_event {
3757 struct vm_area_struct *vma;
3758
3759 const char *file_name;
3760 int file_size;
3761
3762 struct {
3763 struct perf_event_header header;
3764
3765 u32 pid;
3766 u32 tid;
3767 u64 start;
3768 u64 len;
3769 u64 pgoff;
3770 } event_id;
3771};
3772
3773static void perf_event_mmap_output(struct perf_event *event,
3774 struct perf_mmap_event *mmap_event)
3775{
3776 struct perf_output_handle handle;
3777 int size = mmap_event->event_id.header.size;
3778 int ret = perf_output_begin(&handle, event, size, 0, 0);
3779
3780 if (ret)
3781 return;
3782
3783 mmap_event->event_id.pid = perf_event_pid(event, current);
3784 mmap_event->event_id.tid = perf_event_tid(event, current);
3785
3786 perf_output_put(&handle, mmap_event->event_id);
3787 perf_output_copy(&handle, mmap_event->file_name,
3788 mmap_event->file_size);
3789 perf_output_end(&handle);
3790}
3791
3792static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01003793 struct perf_mmap_event *mmap_event,
3794 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003795{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003796 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003797 return 0;
3798
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003799 if (event->cpu != -1 && event->cpu != smp_processor_id())
3800 return 0;
3801
Eric B Munson3af9e852010-05-18 15:30:49 +01003802 if ((!executable && event->attr.mmap_data) ||
3803 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003804 return 1;
3805
3806 return 0;
3807}
3808
3809static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01003810 struct perf_mmap_event *mmap_event,
3811 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003812{
3813 struct perf_event *event;
3814
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003815 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01003816 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003817 perf_event_mmap_output(event, mmap_event);
3818 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003819}
3820
3821static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
3822{
3823 struct perf_cpu_context *cpuctx;
3824 struct perf_event_context *ctx;
3825 struct vm_area_struct *vma = mmap_event->vma;
3826 struct file *file = vma->vm_file;
3827 unsigned int size;
3828 char tmp[16];
3829 char *buf = NULL;
3830 const char *name;
3831
3832 memset(tmp, 0, sizeof(tmp));
3833
3834 if (file) {
3835 /*
3836 * d_path works from the end of the buffer backwards, so we
3837 * need to add enough zero bytes after the string to handle
3838 * the 64bit alignment we do later.
3839 */
3840 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
3841 if (!buf) {
3842 name = strncpy(tmp, "//enomem", sizeof(tmp));
3843 goto got_name;
3844 }
3845 name = d_path(&file->f_path, buf, PATH_MAX);
3846 if (IS_ERR(name)) {
3847 name = strncpy(tmp, "//toolong", sizeof(tmp));
3848 goto got_name;
3849 }
3850 } else {
3851 if (arch_vma_name(mmap_event->vma)) {
3852 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3853 sizeof(tmp));
3854 goto got_name;
3855 }
3856
3857 if (!vma->vm_mm) {
3858 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3859 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01003860 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
3861 vma->vm_end >= vma->vm_mm->brk) {
3862 name = strncpy(tmp, "[heap]", sizeof(tmp));
3863 goto got_name;
3864 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
3865 vma->vm_end >= vma->vm_mm->start_stack) {
3866 name = strncpy(tmp, "[stack]", sizeof(tmp));
3867 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003868 }
3869
3870 name = strncpy(tmp, "//anon", sizeof(tmp));
3871 goto got_name;
3872 }
3873
3874got_name:
3875 size = ALIGN(strlen(name)+1, sizeof(u64));
3876
3877 mmap_event->file_name = name;
3878 mmap_event->file_size = size;
3879
3880 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
3881
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01003882 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003883 cpuctx = &get_cpu_var(perf_cpu_context);
Eric B Munson3af9e852010-05-18 15:30:49 +01003884 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event, vma->vm_flags & VM_EXEC);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003885 ctx = rcu_dereference(current->perf_event_ctxp);
3886 if (ctx)
Eric B Munson3af9e852010-05-18 15:30:49 +01003887 perf_event_mmap_ctx(ctx, mmap_event, vma->vm_flags & VM_EXEC);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003888 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003889 rcu_read_unlock();
3890
3891 kfree(buf);
3892}
3893
Eric B Munson3af9e852010-05-18 15:30:49 +01003894void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003895{
3896 struct perf_mmap_event mmap_event;
3897
3898 if (!atomic_read(&nr_mmap_events))
3899 return;
3900
3901 mmap_event = (struct perf_mmap_event){
3902 .vma = vma,
3903 /* .file_name */
3904 /* .file_size */
3905 .event_id = {
3906 .header = {
3907 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003908 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003909 /* .size */
3910 },
3911 /* .pid */
3912 /* .tid */
3913 .start = vma->vm_start,
3914 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01003915 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003916 },
3917 };
3918
3919 perf_event_mmap_event(&mmap_event);
3920}
3921
3922/*
3923 * IRQ throttle logging
3924 */
3925
3926static void perf_log_throttle(struct perf_event *event, int enable)
3927{
3928 struct perf_output_handle handle;
3929 int ret;
3930
3931 struct {
3932 struct perf_event_header header;
3933 u64 time;
3934 u64 id;
3935 u64 stream_id;
3936 } throttle_event = {
3937 .header = {
3938 .type = PERF_RECORD_THROTTLE,
3939 .misc = 0,
3940 .size = sizeof(throttle_event),
3941 },
3942 .time = perf_clock(),
3943 .id = primary_event_id(event),
3944 .stream_id = event->id,
3945 };
3946
3947 if (enable)
3948 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
3949
3950 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
3951 if (ret)
3952 return;
3953
3954 perf_output_put(&handle, throttle_event);
3955 perf_output_end(&handle);
3956}
3957
3958/*
3959 * Generic event overflow handling, sampling.
3960 */
3961
3962static int __perf_event_overflow(struct perf_event *event, int nmi,
3963 int throttle, struct perf_sample_data *data,
3964 struct pt_regs *regs)
3965{
3966 int events = atomic_read(&event->event_limit);
3967 struct hw_perf_event *hwc = &event->hw;
3968 int ret = 0;
3969
3970 throttle = (throttle && event->pmu->unthrottle != NULL);
3971
3972 if (!throttle) {
3973 hwc->interrupts++;
3974 } else {
3975 if (hwc->interrupts != MAX_INTERRUPTS) {
3976 hwc->interrupts++;
3977 if (HZ * hwc->interrupts >
3978 (u64)sysctl_perf_event_sample_rate) {
3979 hwc->interrupts = MAX_INTERRUPTS;
3980 perf_log_throttle(event, 0);
3981 ret = 1;
3982 }
3983 } else {
3984 /*
3985 * Keep re-disabling events even though on the previous
3986 * pass we disabled it - just in case we raced with a
3987 * sched-in and the event got enabled again:
3988 */
3989 ret = 1;
3990 }
3991 }
3992
3993 if (event->attr.freq) {
3994 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01003995 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003996
Peter Zijlstraabd50712010-01-26 18:50:16 +01003997 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003998
Peter Zijlstraabd50712010-01-26 18:50:16 +01003999 if (delta > 0 && delta < 2*TICK_NSEC)
4000 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004001 }
4002
4003 /*
4004 * XXX event_limit might not quite work as expected on inherited
4005 * events
4006 */
4007
4008 event->pending_kill = POLL_IN;
4009 if (events && atomic_dec_and_test(&event->event_limit)) {
4010 ret = 1;
4011 event->pending_kill = POLL_HUP;
4012 if (nmi) {
4013 event->pending_disable = 1;
4014 perf_pending_queue(&event->pending,
4015 perf_pending_event);
4016 } else
4017 perf_event_disable(event);
4018 }
4019
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004020 if (event->overflow_handler)
4021 event->overflow_handler(event, nmi, data, regs);
4022 else
4023 perf_event_output(event, nmi, data, regs);
4024
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004025 return ret;
4026}
4027
4028int perf_event_overflow(struct perf_event *event, int nmi,
4029 struct perf_sample_data *data,
4030 struct pt_regs *regs)
4031{
4032 return __perf_event_overflow(event, nmi, 1, data, regs);
4033}
4034
4035/*
4036 * Generic software event infrastructure
4037 */
4038
4039/*
4040 * We directly increment event->count and keep a second value in
4041 * event->hw.period_left to count intervals. This period event
4042 * is kept in the range [-sample_period, 0] so that we can use the
4043 * sign as trigger.
4044 */
4045
4046static u64 perf_swevent_set_period(struct perf_event *event)
4047{
4048 struct hw_perf_event *hwc = &event->hw;
4049 u64 period = hwc->last_period;
4050 u64 nr, offset;
4051 s64 old, val;
4052
4053 hwc->last_period = hwc->sample_period;
4054
4055again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004056 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004057 if (val < 0)
4058 return 0;
4059
4060 nr = div64_u64(period + val, period);
4061 offset = nr * period;
4062 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004063 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004064 goto again;
4065
4066 return nr;
4067}
4068
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004069static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004070 int nmi, struct perf_sample_data *data,
4071 struct pt_regs *regs)
4072{
4073 struct hw_perf_event *hwc = &event->hw;
4074 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004075
4076 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004077 if (!overflow)
4078 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004079
4080 if (hwc->interrupts == MAX_INTERRUPTS)
4081 return;
4082
4083 for (; overflow; overflow--) {
4084 if (__perf_event_overflow(event, nmi, throttle,
4085 data, regs)) {
4086 /*
4087 * We inhibit the overflow from happening when
4088 * hwc->interrupts == MAX_INTERRUPTS.
4089 */
4090 break;
4091 }
4092 throttle = 1;
4093 }
4094}
4095
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004096static void perf_swevent_add(struct perf_event *event, u64 nr,
4097 int nmi, struct perf_sample_data *data,
4098 struct pt_regs *regs)
4099{
4100 struct hw_perf_event *hwc = &event->hw;
4101
Peter Zijlstrae7850592010-05-21 14:43:08 +02004102 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004103
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004104 if (!regs)
4105 return;
4106
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004107 if (!hwc->sample_period)
4108 return;
4109
4110 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4111 return perf_swevent_overflow(event, 1, nmi, data, regs);
4112
Peter Zijlstrae7850592010-05-21 14:43:08 +02004113 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004114 return;
4115
4116 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004117}
4118
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004119static int perf_exclude_event(struct perf_event *event,
4120 struct pt_regs *regs)
4121{
4122 if (regs) {
4123 if (event->attr.exclude_user && user_mode(regs))
4124 return 1;
4125
4126 if (event->attr.exclude_kernel && !user_mode(regs))
4127 return 1;
4128 }
4129
4130 return 0;
4131}
4132
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004133static int perf_swevent_match(struct perf_event *event,
4134 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004135 u32 event_id,
4136 struct perf_sample_data *data,
4137 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004138{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004139 if (event->attr.type != type)
4140 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004141
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004142 if (event->attr.config != event_id)
4143 return 0;
4144
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004145 if (perf_exclude_event(event, regs))
4146 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004147
4148 return 1;
4149}
4150
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004151static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004152{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004153 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004154
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004155 return hash_64(val, SWEVENT_HLIST_BITS);
4156}
4157
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004158static inline struct hlist_head *
4159__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004160{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004161 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004162
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004163 return &hlist->heads[hash];
4164}
4165
4166/* For the read side: events when they trigger */
4167static inline struct hlist_head *
4168find_swevent_head_rcu(struct perf_cpu_context *ctx, u64 type, u32 event_id)
4169{
4170 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004171
4172 hlist = rcu_dereference(ctx->swevent_hlist);
4173 if (!hlist)
4174 return NULL;
4175
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004176 return __find_swevent_head(hlist, type, event_id);
4177}
4178
4179/* For the event head insertion and removal in the hlist */
4180static inline struct hlist_head *
4181find_swevent_head(struct perf_cpu_context *ctx, struct perf_event *event)
4182{
4183 struct swevent_hlist *hlist;
4184 u32 event_id = event->attr.config;
4185 u64 type = event->attr.type;
4186
4187 /*
4188 * Event scheduling is always serialized against hlist allocation
4189 * and release. Which makes the protected version suitable here.
4190 * The context lock guarantees that.
4191 */
4192 hlist = rcu_dereference_protected(ctx->swevent_hlist,
4193 lockdep_is_held(&event->ctx->lock));
4194 if (!hlist)
4195 return NULL;
4196
4197 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004198}
4199
4200static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4201 u64 nr, int nmi,
4202 struct perf_sample_data *data,
4203 struct pt_regs *regs)
4204{
4205 struct perf_cpu_context *cpuctx;
4206 struct perf_event *event;
4207 struct hlist_node *node;
4208 struct hlist_head *head;
4209
4210 cpuctx = &__get_cpu_var(perf_cpu_context);
4211
4212 rcu_read_lock();
4213
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004214 head = find_swevent_head_rcu(cpuctx, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004215
4216 if (!head)
4217 goto end;
4218
4219 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004220 if (perf_swevent_match(event, type, event_id, data, regs))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004221 perf_swevent_add(event, nr, nmi, data, regs);
4222 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004223end:
4224 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004225}
4226
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004227int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004228{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004229 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004230 int rctx;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004231
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004232 if (in_nmi())
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004233 rctx = 3;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004234 else if (in_irq())
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004235 rctx = 2;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004236 else if (in_softirq())
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004237 rctx = 1;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004238 else
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004239 rctx = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004240
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004241 if (cpuctx->recursion[rctx])
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004242 return -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004243
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004244 cpuctx->recursion[rctx]++;
4245 barrier();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004246
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004247 return rctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004248}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004249EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004250
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004251void inline perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004252{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004253 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4254 barrier();
Frederic Weisbeckerfe612672009-11-24 20:38:22 +01004255 cpuctx->recursion[rctx]--;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004256}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004257
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004258void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4259 struct pt_regs *regs, u64 addr)
4260{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004261 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004262 int rctx;
4263
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004264 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004265 rctx = perf_swevent_get_recursion_context();
4266 if (rctx < 0)
4267 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004268
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004269 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004270
4271 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004272
4273 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004274 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004275}
4276
4277static void perf_swevent_read(struct perf_event *event)
4278{
4279}
4280
4281static int perf_swevent_enable(struct perf_event *event)
4282{
4283 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004284 struct perf_cpu_context *cpuctx;
4285 struct hlist_head *head;
4286
4287 cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004288
4289 if (hwc->sample_period) {
4290 hwc->last_period = hwc->sample_period;
4291 perf_swevent_set_period(event);
4292 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004293
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004294 head = find_swevent_head(cpuctx, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004295 if (WARN_ON_ONCE(!head))
4296 return -EINVAL;
4297
4298 hlist_add_head_rcu(&event->hlist_entry, head);
4299
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004300 return 0;
4301}
4302
4303static void perf_swevent_disable(struct perf_event *event)
4304{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004305 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004306}
4307
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004308static void perf_swevent_void(struct perf_event *event)
4309{
4310}
4311
4312static int perf_swevent_int(struct perf_event *event)
4313{
4314 return 0;
4315}
4316
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004317static const struct pmu perf_ops_generic = {
4318 .enable = perf_swevent_enable,
4319 .disable = perf_swevent_disable,
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004320 .start = perf_swevent_int,
4321 .stop = perf_swevent_void,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004322 .read = perf_swevent_read,
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004323 .unthrottle = perf_swevent_void, /* hwc->interrupts already reset */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004324};
4325
4326/*
4327 * hrtimer based swevent callback
4328 */
4329
4330static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
4331{
4332 enum hrtimer_restart ret = HRTIMER_RESTART;
4333 struct perf_sample_data data;
4334 struct pt_regs *regs;
4335 struct perf_event *event;
4336 u64 period;
4337
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004338 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004339 event->pmu->read(event);
4340
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004341 perf_sample_data_init(&data, 0);
Xiao Guangrong59d069e2009-12-01 17:30:08 +08004342 data.period = event->hw.last_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004343 regs = get_irq_regs();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004344
Frederic Weisbeckerdf8290b2010-04-09 00:28:14 +02004345 if (regs && !perf_exclude_event(event, regs)) {
Soeren Sandmann54f44072009-10-22 18:34:08 +02004346 if (!(event->attr.exclude_idle && current->pid == 0))
4347 if (perf_event_overflow(event, 0, &data, regs))
4348 ret = HRTIMER_NORESTART;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004349 }
4350
4351 period = max_t(u64, 10000, event->hw.sample_period);
4352 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4353
4354 return ret;
4355}
4356
Soeren Sandmann721a6692009-09-15 14:33:08 +02004357static void perf_swevent_start_hrtimer(struct perf_event *event)
4358{
4359 struct hw_perf_event *hwc = &event->hw;
4360
4361 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4362 hwc->hrtimer.function = perf_swevent_hrtimer;
4363 if (hwc->sample_period) {
4364 u64 period;
4365
4366 if (hwc->remaining) {
4367 if (hwc->remaining < 0)
4368 period = 10000;
4369 else
4370 period = hwc->remaining;
4371 hwc->remaining = 0;
4372 } else {
4373 period = max_t(u64, 10000, hwc->sample_period);
4374 }
4375 __hrtimer_start_range_ns(&hwc->hrtimer,
4376 ns_to_ktime(period), 0,
4377 HRTIMER_MODE_REL, 0);
4378 }
4379}
4380
4381static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4382{
4383 struct hw_perf_event *hwc = &event->hw;
4384
4385 if (hwc->sample_period) {
4386 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4387 hwc->remaining = ktime_to_ns(remaining);
4388
4389 hrtimer_cancel(&hwc->hrtimer);
4390 }
4391}
4392
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004393/*
4394 * Software event: cpu wall time clock
4395 */
4396
4397static void cpu_clock_perf_event_update(struct perf_event *event)
4398{
4399 int cpu = raw_smp_processor_id();
4400 s64 prev;
4401 u64 now;
4402
4403 now = cpu_clock(cpu);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004404 prev = local64_xchg(&event->hw.prev_count, now);
4405 local64_add(now - prev, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004406}
4407
4408static int cpu_clock_perf_event_enable(struct perf_event *event)
4409{
4410 struct hw_perf_event *hwc = &event->hw;
4411 int cpu = raw_smp_processor_id();
4412
Peter Zijlstrae7850592010-05-21 14:43:08 +02004413 local64_set(&hwc->prev_count, cpu_clock(cpu));
Soeren Sandmann721a6692009-09-15 14:33:08 +02004414 perf_swevent_start_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004415
4416 return 0;
4417}
4418
4419static void cpu_clock_perf_event_disable(struct perf_event *event)
4420{
Soeren Sandmann721a6692009-09-15 14:33:08 +02004421 perf_swevent_cancel_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004422 cpu_clock_perf_event_update(event);
4423}
4424
4425static void cpu_clock_perf_event_read(struct perf_event *event)
4426{
4427 cpu_clock_perf_event_update(event);
4428}
4429
4430static const struct pmu perf_ops_cpu_clock = {
4431 .enable = cpu_clock_perf_event_enable,
4432 .disable = cpu_clock_perf_event_disable,
4433 .read = cpu_clock_perf_event_read,
4434};
4435
4436/*
4437 * Software event: task time clock
4438 */
4439
4440static void task_clock_perf_event_update(struct perf_event *event, u64 now)
4441{
4442 u64 prev;
4443 s64 delta;
4444
Peter Zijlstrae7850592010-05-21 14:43:08 +02004445 prev = local64_xchg(&event->hw.prev_count, now);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004446 delta = now - prev;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004447 local64_add(delta, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004448}
4449
4450static int task_clock_perf_event_enable(struct perf_event *event)
4451{
4452 struct hw_perf_event *hwc = &event->hw;
4453 u64 now;
4454
4455 now = event->ctx->time;
4456
Peter Zijlstrae7850592010-05-21 14:43:08 +02004457 local64_set(&hwc->prev_count, now);
Soeren Sandmann721a6692009-09-15 14:33:08 +02004458
4459 perf_swevent_start_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004460
4461 return 0;
4462}
4463
4464static void task_clock_perf_event_disable(struct perf_event *event)
4465{
Soeren Sandmann721a6692009-09-15 14:33:08 +02004466 perf_swevent_cancel_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004467 task_clock_perf_event_update(event, event->ctx->time);
4468
4469}
4470
4471static void task_clock_perf_event_read(struct perf_event *event)
4472{
4473 u64 time;
4474
4475 if (!in_nmi()) {
4476 update_context_time(event->ctx);
4477 time = event->ctx->time;
4478 } else {
4479 u64 now = perf_clock();
4480 u64 delta = now - event->ctx->timestamp;
4481 time = event->ctx->time + delta;
4482 }
4483
4484 task_clock_perf_event_update(event, time);
4485}
4486
4487static const struct pmu perf_ops_task_clock = {
4488 .enable = task_clock_perf_event_enable,
4489 .disable = task_clock_perf_event_disable,
4490 .read = task_clock_perf_event_read,
4491};
4492
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004493/* Deref the hlist from the update side */
4494static inline struct swevent_hlist *
4495swevent_hlist_deref(struct perf_cpu_context *cpuctx)
4496{
4497 return rcu_dereference_protected(cpuctx->swevent_hlist,
4498 lockdep_is_held(&cpuctx->hlist_mutex));
4499}
4500
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004501static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4502{
4503 struct swevent_hlist *hlist;
4504
4505 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4506 kfree(hlist);
4507}
4508
4509static void swevent_hlist_release(struct perf_cpu_context *cpuctx)
4510{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004511 struct swevent_hlist *hlist = swevent_hlist_deref(cpuctx);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004512
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004513 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004514 return;
4515
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004516 rcu_assign_pointer(cpuctx->swevent_hlist, NULL);
4517 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4518}
4519
4520static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4521{
4522 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4523
4524 mutex_lock(&cpuctx->hlist_mutex);
4525
4526 if (!--cpuctx->hlist_refcount)
4527 swevent_hlist_release(cpuctx);
4528
4529 mutex_unlock(&cpuctx->hlist_mutex);
4530}
4531
4532static void swevent_hlist_put(struct perf_event *event)
4533{
4534 int cpu;
4535
4536 if (event->cpu != -1) {
4537 swevent_hlist_put_cpu(event, event->cpu);
4538 return;
4539 }
4540
4541 for_each_possible_cpu(cpu)
4542 swevent_hlist_put_cpu(event, cpu);
4543}
4544
4545static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4546{
4547 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4548 int err = 0;
4549
4550 mutex_lock(&cpuctx->hlist_mutex);
4551
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004552 if (!swevent_hlist_deref(cpuctx) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004553 struct swevent_hlist *hlist;
4554
4555 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4556 if (!hlist) {
4557 err = -ENOMEM;
4558 goto exit;
4559 }
4560 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
4561 }
4562 cpuctx->hlist_refcount++;
4563 exit:
4564 mutex_unlock(&cpuctx->hlist_mutex);
4565
4566 return err;
4567}
4568
4569static int swevent_hlist_get(struct perf_event *event)
4570{
4571 int err;
4572 int cpu, failed_cpu;
4573
4574 if (event->cpu != -1)
4575 return swevent_hlist_get_cpu(event, event->cpu);
4576
4577 get_online_cpus();
4578 for_each_possible_cpu(cpu) {
4579 err = swevent_hlist_get_cpu(event, cpu);
4580 if (err) {
4581 failed_cpu = cpu;
4582 goto fail;
4583 }
4584 }
4585 put_online_cpus();
4586
4587 return 0;
4588 fail:
4589 for_each_possible_cpu(cpu) {
4590 if (cpu == failed_cpu)
4591 break;
4592 swevent_hlist_put_cpu(event, cpu);
4593 }
4594
4595 put_online_cpus();
4596 return err;
4597}
4598
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004599#ifdef CONFIG_EVENT_TRACING
4600
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004601static const struct pmu perf_ops_tracepoint = {
4602 .enable = perf_trace_enable,
4603 .disable = perf_trace_disable,
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004604 .start = perf_swevent_int,
4605 .stop = perf_swevent_void,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004606 .read = perf_swevent_read,
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004607 .unthrottle = perf_swevent_void,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004608};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004609
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004610static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004611 struct perf_sample_data *data)
4612{
4613 void *record = data->raw->data;
4614
4615 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4616 return 1;
4617 return 0;
4618}
4619
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004620static int perf_tp_event_match(struct perf_event *event,
4621 struct perf_sample_data *data,
4622 struct pt_regs *regs)
4623{
Peter Zijlstra580d6072010-05-20 20:54:31 +02004624 /*
4625 * All tracepoints are from kernel-space.
4626 */
4627 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004628 return 0;
4629
4630 if (!perf_tp_filter_match(event, data))
4631 return 0;
4632
4633 return 1;
4634}
4635
4636void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004637 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004638{
4639 struct perf_sample_data data;
4640 struct perf_event *event;
4641 struct hlist_node *node;
4642
4643 struct perf_raw_record raw = {
4644 .size = entry_size,
4645 .data = record,
4646 };
4647
4648 perf_sample_data_init(&data, addr);
4649 data.raw = &raw;
4650
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004651 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4652 if (perf_tp_event_match(event, &data, regs))
4653 perf_swevent_add(event, count, 1, &data, regs);
4654 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004655
4656 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004657}
4658EXPORT_SYMBOL_GPL(perf_tp_event);
4659
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004660static void tp_perf_event_destroy(struct perf_event *event)
4661{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004662 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004663}
4664
4665static const struct pmu *tp_perf_event_init(struct perf_event *event)
4666{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004667 int err;
4668
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004669 /*
4670 * Raw tracepoint data is a severe data leak, only allow root to
4671 * have these.
4672 */
4673 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4674 perf_paranoid_tracepoint_raw() &&
4675 !capable(CAP_SYS_ADMIN))
4676 return ERR_PTR(-EPERM);
4677
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004678 err = perf_trace_init(event);
4679 if (err)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004680 return NULL;
4681
4682 event->destroy = tp_perf_event_destroy;
4683
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004684 return &perf_ops_tracepoint;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004685}
Li Zefan6fb29152009-10-15 11:21:42 +08004686
4687static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4688{
4689 char *filter_str;
4690 int ret;
4691
4692 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4693 return -EINVAL;
4694
4695 filter_str = strndup_user(arg, PAGE_SIZE);
4696 if (IS_ERR(filter_str))
4697 return PTR_ERR(filter_str);
4698
4699 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4700
4701 kfree(filter_str);
4702 return ret;
4703}
4704
4705static void perf_event_free_filter(struct perf_event *event)
4706{
4707 ftrace_profile_free_filter(event);
4708}
4709
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004710#else
Li Zefan6fb29152009-10-15 11:21:42 +08004711
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004712static const struct pmu *tp_perf_event_init(struct perf_event *event)
4713{
4714 return NULL;
4715}
Li Zefan6fb29152009-10-15 11:21:42 +08004716
4717static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4718{
4719 return -ENOENT;
4720}
4721
4722static void perf_event_free_filter(struct perf_event *event)
4723{
4724}
4725
Li Zefan07b139c2009-12-21 14:27:35 +08004726#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004727
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004728#ifdef CONFIG_HAVE_HW_BREAKPOINT
4729static void bp_perf_event_destroy(struct perf_event *event)
4730{
4731 release_bp_slot(event);
4732}
4733
4734static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4735{
4736 int err;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004737
4738 err = register_perf_hw_breakpoint(bp);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004739 if (err)
4740 return ERR_PTR(err);
4741
4742 bp->destroy = bp_perf_event_destroy;
4743
4744 return &perf_ops_bp;
4745}
4746
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004747void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004748{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004749 struct perf_sample_data sample;
4750 struct pt_regs *regs = data;
4751
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004752 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004753
4754 if (!perf_exclude_event(bp, regs))
4755 perf_swevent_add(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004756}
4757#else
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004758static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4759{
4760 return NULL;
4761}
4762
4763void perf_bp_event(struct perf_event *bp, void *regs)
4764{
4765}
4766#endif
4767
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004768atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4769
4770static void sw_perf_event_destroy(struct perf_event *event)
4771{
4772 u64 event_id = event->attr.config;
4773
4774 WARN_ON(event->parent);
4775
4776 atomic_dec(&perf_swevent_enabled[event_id]);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004777 swevent_hlist_put(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004778}
4779
4780static const struct pmu *sw_perf_event_init(struct perf_event *event)
4781{
4782 const struct pmu *pmu = NULL;
4783 u64 event_id = event->attr.config;
4784
4785 /*
4786 * Software events (currently) can't in general distinguish
4787 * between user, kernel and hypervisor events.
4788 * However, context switches and cpu migrations are considered
4789 * to be kernel events, and page faults are never hypervisor
4790 * events.
4791 */
4792 switch (event_id) {
4793 case PERF_COUNT_SW_CPU_CLOCK:
4794 pmu = &perf_ops_cpu_clock;
4795
4796 break;
4797 case PERF_COUNT_SW_TASK_CLOCK:
4798 /*
4799 * If the user instantiates this as a per-cpu event,
4800 * use the cpu_clock event instead.
4801 */
4802 if (event->ctx->task)
4803 pmu = &perf_ops_task_clock;
4804 else
4805 pmu = &perf_ops_cpu_clock;
4806
4807 break;
4808 case PERF_COUNT_SW_PAGE_FAULTS:
4809 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
4810 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
4811 case PERF_COUNT_SW_CONTEXT_SWITCHES:
4812 case PERF_COUNT_SW_CPU_MIGRATIONS:
Anton Blanchardf7d79862009-10-18 01:09:29 +00004813 case PERF_COUNT_SW_ALIGNMENT_FAULTS:
4814 case PERF_COUNT_SW_EMULATION_FAULTS:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004815 if (!event->parent) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004816 int err;
4817
4818 err = swevent_hlist_get(event);
4819 if (err)
4820 return ERR_PTR(err);
4821
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004822 atomic_inc(&perf_swevent_enabled[event_id]);
4823 event->destroy = sw_perf_event_destroy;
4824 }
4825 pmu = &perf_ops_generic;
4826 break;
4827 }
4828
4829 return pmu;
4830}
4831
4832/*
4833 * Allocate and initialize a event structure
4834 */
4835static struct perf_event *
4836perf_event_alloc(struct perf_event_attr *attr,
4837 int cpu,
4838 struct perf_event_context *ctx,
4839 struct perf_event *group_leader,
4840 struct perf_event *parent_event,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004841 perf_overflow_handler_t overflow_handler,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004842 gfp_t gfpflags)
4843{
4844 const struct pmu *pmu;
4845 struct perf_event *event;
4846 struct hw_perf_event *hwc;
4847 long err;
4848
4849 event = kzalloc(sizeof(*event), gfpflags);
4850 if (!event)
4851 return ERR_PTR(-ENOMEM);
4852
4853 /*
4854 * Single events are their own group leaders, with an
4855 * empty sibling list:
4856 */
4857 if (!group_leader)
4858 group_leader = event;
4859
4860 mutex_init(&event->child_mutex);
4861 INIT_LIST_HEAD(&event->child_list);
4862
4863 INIT_LIST_HEAD(&event->group_entry);
4864 INIT_LIST_HEAD(&event->event_entry);
4865 INIT_LIST_HEAD(&event->sibling_list);
4866 init_waitqueue_head(&event->waitq);
4867
4868 mutex_init(&event->mmap_mutex);
4869
4870 event->cpu = cpu;
4871 event->attr = *attr;
4872 event->group_leader = group_leader;
4873 event->pmu = NULL;
4874 event->ctx = ctx;
4875 event->oncpu = -1;
4876
4877 event->parent = parent_event;
4878
4879 event->ns = get_pid_ns(current->nsproxy->pid_ns);
4880 event->id = atomic64_inc_return(&perf_event_id);
4881
4882 event->state = PERF_EVENT_STATE_INACTIVE;
4883
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004884 if (!overflow_handler && parent_event)
4885 overflow_handler = parent_event->overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02004886
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004887 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02004888
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004889 if (attr->disabled)
4890 event->state = PERF_EVENT_STATE_OFF;
4891
4892 pmu = NULL;
4893
4894 hwc = &event->hw;
4895 hwc->sample_period = attr->sample_period;
4896 if (attr->freq && attr->sample_freq)
4897 hwc->sample_period = 1;
4898 hwc->last_period = hwc->sample_period;
4899
Peter Zijlstrae7850592010-05-21 14:43:08 +02004900 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004901
4902 /*
4903 * we currently do not support PERF_FORMAT_GROUP on inherited events
4904 */
4905 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
4906 goto done;
4907
4908 switch (attr->type) {
4909 case PERF_TYPE_RAW:
4910 case PERF_TYPE_HARDWARE:
4911 case PERF_TYPE_HW_CACHE:
4912 pmu = hw_perf_event_init(event);
4913 break;
4914
4915 case PERF_TYPE_SOFTWARE:
4916 pmu = sw_perf_event_init(event);
4917 break;
4918
4919 case PERF_TYPE_TRACEPOINT:
4920 pmu = tp_perf_event_init(event);
4921 break;
4922
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004923 case PERF_TYPE_BREAKPOINT:
4924 pmu = bp_perf_event_init(event);
4925 break;
4926
4927
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004928 default:
4929 break;
4930 }
4931done:
4932 err = 0;
4933 if (!pmu)
4934 err = -EINVAL;
4935 else if (IS_ERR(pmu))
4936 err = PTR_ERR(pmu);
4937
4938 if (err) {
4939 if (event->ns)
4940 put_pid_ns(event->ns);
4941 kfree(event);
4942 return ERR_PTR(err);
4943 }
4944
4945 event->pmu = pmu;
4946
4947 if (!event->parent) {
4948 atomic_inc(&nr_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01004949 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004950 atomic_inc(&nr_mmap_events);
4951 if (event->attr.comm)
4952 atomic_inc(&nr_comm_events);
4953 if (event->attr.task)
4954 atomic_inc(&nr_task_events);
4955 }
4956
4957 return event;
4958}
4959
4960static int perf_copy_attr(struct perf_event_attr __user *uattr,
4961 struct perf_event_attr *attr)
4962{
4963 u32 size;
4964 int ret;
4965
4966 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
4967 return -EFAULT;
4968
4969 /*
4970 * zero the full structure, so that a short copy will be nice.
4971 */
4972 memset(attr, 0, sizeof(*attr));
4973
4974 ret = get_user(size, &uattr->size);
4975 if (ret)
4976 return ret;
4977
4978 if (size > PAGE_SIZE) /* silly large */
4979 goto err_size;
4980
4981 if (!size) /* abi compat */
4982 size = PERF_ATTR_SIZE_VER0;
4983
4984 if (size < PERF_ATTR_SIZE_VER0)
4985 goto err_size;
4986
4987 /*
4988 * If we're handed a bigger struct than we know of,
4989 * ensure all the unknown bits are 0 - i.e. new
4990 * user-space does not rely on any kernel feature
4991 * extensions we dont know about yet.
4992 */
4993 if (size > sizeof(*attr)) {
4994 unsigned char __user *addr;
4995 unsigned char __user *end;
4996 unsigned char val;
4997
4998 addr = (void __user *)uattr + sizeof(*attr);
4999 end = (void __user *)uattr + size;
5000
5001 for (; addr < end; addr++) {
5002 ret = get_user(val, addr);
5003 if (ret)
5004 return ret;
5005 if (val)
5006 goto err_size;
5007 }
5008 size = sizeof(*attr);
5009 }
5010
5011 ret = copy_from_user(attr, uattr, size);
5012 if (ret)
5013 return -EFAULT;
5014
5015 /*
5016 * If the type exists, the corresponding creation will verify
5017 * the attr->config.
5018 */
5019 if (attr->type >= PERF_TYPE_MAX)
5020 return -EINVAL;
5021
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05305022 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005023 return -EINVAL;
5024
5025 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5026 return -EINVAL;
5027
5028 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5029 return -EINVAL;
5030
5031out:
5032 return ret;
5033
5034err_size:
5035 put_user(sizeof(*attr), &uattr->size);
5036 ret = -E2BIG;
5037 goto out;
5038}
5039
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005040static int
5041perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005042{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005043 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005044 int ret = -EINVAL;
5045
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005046 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005047 goto set;
5048
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005049 /* don't allow circular references */
5050 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005051 goto out;
5052
Peter Zijlstra0f139302010-05-20 14:35:15 +02005053 /*
5054 * Don't allow cross-cpu buffers
5055 */
5056 if (output_event->cpu != event->cpu)
5057 goto out;
5058
5059 /*
5060 * If its not a per-cpu buffer, it must be the same task.
5061 */
5062 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5063 goto out;
5064
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005065set:
5066 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005067 /* Can't redirect output if we've got an active mmap() */
5068 if (atomic_read(&event->mmap_count))
5069 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005070
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005071 if (output_event) {
5072 /* get the buffer we want to redirect to */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005073 buffer = perf_buffer_get(output_event);
5074 if (!buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005075 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005076 }
5077
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005078 old_buffer = event->buffer;
5079 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005080 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005081unlock:
5082 mutex_unlock(&event->mmap_mutex);
5083
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005084 if (old_buffer)
5085 perf_buffer_put(old_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005086out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005087 return ret;
5088}
5089
5090/**
5091 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5092 *
5093 * @attr_uptr: event_id type attributes for monitoring/sampling
5094 * @pid: target pid
5095 * @cpu: target cpu
5096 * @group_fd: group leader event fd
5097 */
5098SYSCALL_DEFINE5(perf_event_open,
5099 struct perf_event_attr __user *, attr_uptr,
5100 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5101{
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005102 struct perf_event *event, *group_leader = NULL, *output_event = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005103 struct perf_event_attr attr;
5104 struct perf_event_context *ctx;
5105 struct file *event_file = NULL;
5106 struct file *group_file = NULL;
Al Viroea635c62010-05-26 17:40:29 -04005107 int event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005108 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005109 int err;
5110
5111 /* for future expandability... */
5112 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5113 return -EINVAL;
5114
5115 err = perf_copy_attr(attr_uptr, &attr);
5116 if (err)
5117 return err;
5118
5119 if (!attr.exclude_kernel) {
5120 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5121 return -EACCES;
5122 }
5123
5124 if (attr.freq) {
5125 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5126 return -EINVAL;
5127 }
5128
Al Viroea635c62010-05-26 17:40:29 -04005129 event_fd = get_unused_fd_flags(O_RDWR);
5130 if (event_fd < 0)
5131 return event_fd;
5132
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005133 /*
5134 * Get the target context (task or percpu):
5135 */
5136 ctx = find_get_context(pid, cpu);
Al Viroea635c62010-05-26 17:40:29 -04005137 if (IS_ERR(ctx)) {
5138 err = PTR_ERR(ctx);
5139 goto err_fd;
5140 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005141
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005142 if (group_fd != -1) {
5143 group_leader = perf_fget_light(group_fd, &fput_needed);
5144 if (IS_ERR(group_leader)) {
5145 err = PTR_ERR(group_leader);
5146 goto err_put_context;
5147 }
5148 group_file = group_leader->filp;
5149 if (flags & PERF_FLAG_FD_OUTPUT)
5150 output_event = group_leader;
5151 if (flags & PERF_FLAG_FD_NO_GROUP)
5152 group_leader = NULL;
5153 }
5154
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005155 /*
5156 * Look up the group leader (we will attach this event to it):
5157 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005158 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005159 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005160
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005161 /*
5162 * Do not allow a recursive hierarchy (this new sibling
5163 * becoming part of another group-sibling):
5164 */
5165 if (group_leader->group_leader != group_leader)
5166 goto err_put_context;
5167 /*
5168 * Do not allow to attach to a group in a different
5169 * task or CPU context:
5170 */
5171 if (group_leader->ctx != ctx)
5172 goto err_put_context;
5173 /*
5174 * Only a group leader can be exclusive or pinned
5175 */
5176 if (attr.exclusive || attr.pinned)
5177 goto err_put_context;
5178 }
5179
5180 event = perf_event_alloc(&attr, cpu, ctx, group_leader,
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005181 NULL, NULL, GFP_KERNEL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005182 if (IS_ERR(event)) {
5183 err = PTR_ERR(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005184 goto err_put_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005185 }
5186
5187 if (output_event) {
5188 err = perf_event_set_output(event, output_event);
5189 if (err)
5190 goto err_free_put_context;
5191 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005192
Al Viroea635c62010-05-26 17:40:29 -04005193 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5194 if (IS_ERR(event_file)) {
5195 err = PTR_ERR(event_file);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005196 goto err_free_put_context;
Al Viroea635c62010-05-26 17:40:29 -04005197 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005198
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005199 event->filp = event_file;
5200 WARN_ON_ONCE(ctx->parent_ctx);
5201 mutex_lock(&ctx->mutex);
5202 perf_install_in_context(ctx, event, cpu);
5203 ++ctx->generation;
5204 mutex_unlock(&ctx->mutex);
5205
5206 event->owner = current;
5207 get_task_struct(current);
5208 mutex_lock(&current->perf_event_mutex);
5209 list_add_tail(&event->owner_entry, &current->perf_event_list);
5210 mutex_unlock(&current->perf_event_mutex);
5211
Peter Zijlstra8a495422010-05-27 15:47:49 +02005212 /*
5213 * Drop the reference on the group_event after placing the
5214 * new event on the sibling_list. This ensures destruction
5215 * of the group leader will find the pointer to itself in
5216 * perf_group_detach().
5217 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005218 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04005219 fd_install(event_fd, event_file);
5220 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005221
Al Viroea635c62010-05-26 17:40:29 -04005222err_free_put_context:
5223 free_event(event);
5224err_put_context:
5225 fput_light(group_file, fput_needed);
5226 put_ctx(ctx);
5227err_fd:
5228 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005229 return err;
5230}
5231
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005232/**
5233 * perf_event_create_kernel_counter
5234 *
5235 * @attr: attributes of the counter to create
5236 * @cpu: cpu in which the counter is bound
5237 * @pid: task to profile
5238 */
5239struct perf_event *
5240perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005241 pid_t pid,
5242 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005243{
5244 struct perf_event *event;
5245 struct perf_event_context *ctx;
5246 int err;
5247
5248 /*
5249 * Get the target context (task or percpu):
5250 */
5251
5252 ctx = find_get_context(pid, cpu);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005253 if (IS_ERR(ctx)) {
5254 err = PTR_ERR(ctx);
5255 goto err_exit;
5256 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005257
5258 event = perf_event_alloc(attr, cpu, ctx, NULL,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005259 NULL, overflow_handler, GFP_KERNEL);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005260 if (IS_ERR(event)) {
5261 err = PTR_ERR(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005262 goto err_put_context;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005263 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005264
5265 event->filp = NULL;
5266 WARN_ON_ONCE(ctx->parent_ctx);
5267 mutex_lock(&ctx->mutex);
5268 perf_install_in_context(ctx, event, cpu);
5269 ++ctx->generation;
5270 mutex_unlock(&ctx->mutex);
5271
5272 event->owner = current;
5273 get_task_struct(current);
5274 mutex_lock(&current->perf_event_mutex);
5275 list_add_tail(&event->owner_entry, &current->perf_event_list);
5276 mutex_unlock(&current->perf_event_mutex);
5277
5278 return event;
5279
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005280 err_put_context:
5281 put_ctx(ctx);
5282 err_exit:
5283 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005284}
5285EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5286
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005287/*
5288 * inherit a event from parent task to child task:
5289 */
5290static struct perf_event *
5291inherit_event(struct perf_event *parent_event,
5292 struct task_struct *parent,
5293 struct perf_event_context *parent_ctx,
5294 struct task_struct *child,
5295 struct perf_event *group_leader,
5296 struct perf_event_context *child_ctx)
5297{
5298 struct perf_event *child_event;
5299
5300 /*
5301 * Instead of creating recursive hierarchies of events,
5302 * we link inherited events back to the original parent,
5303 * which has a filp for sure, which we use as the reference
5304 * count:
5305 */
5306 if (parent_event->parent)
5307 parent_event = parent_event->parent;
5308
5309 child_event = perf_event_alloc(&parent_event->attr,
5310 parent_event->cpu, child_ctx,
5311 group_leader, parent_event,
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005312 NULL, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005313 if (IS_ERR(child_event))
5314 return child_event;
5315 get_ctx(child_ctx);
5316
5317 /*
5318 * Make the child state follow the state of the parent event,
5319 * not its attr.disabled bit. We hold the parent's mutex,
5320 * so we won't race with perf_event_{en, dis}able_family.
5321 */
5322 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
5323 child_event->state = PERF_EVENT_STATE_INACTIVE;
5324 else
5325 child_event->state = PERF_EVENT_STATE_OFF;
5326
Peter Zijlstra75c9f322010-01-29 09:04:26 +01005327 if (parent_event->attr.freq) {
5328 u64 sample_period = parent_event->hw.sample_period;
5329 struct hw_perf_event *hwc = &child_event->hw;
5330
5331 hwc->sample_period = sample_period;
5332 hwc->last_period = sample_period;
5333
Peter Zijlstrae7850592010-05-21 14:43:08 +02005334 local64_set(&hwc->period_left, sample_period);
Peter Zijlstra75c9f322010-01-29 09:04:26 +01005335 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005336
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005337 child_event->overflow_handler = parent_event->overflow_handler;
5338
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005339 /*
5340 * Link it up in the child's context:
5341 */
5342 add_event_to_ctx(child_event, child_ctx);
5343
5344 /*
5345 * Get a reference to the parent filp - we will fput it
5346 * when the child event exits. This is safe to do because
5347 * we are in the parent and we know that the filp still
5348 * exists and has a nonzero count:
5349 */
5350 atomic_long_inc(&parent_event->filp->f_count);
5351
5352 /*
5353 * Link this into the parent event's child list
5354 */
5355 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5356 mutex_lock(&parent_event->child_mutex);
5357 list_add_tail(&child_event->child_list, &parent_event->child_list);
5358 mutex_unlock(&parent_event->child_mutex);
5359
5360 return child_event;
5361}
5362
5363static int inherit_group(struct perf_event *parent_event,
5364 struct task_struct *parent,
5365 struct perf_event_context *parent_ctx,
5366 struct task_struct *child,
5367 struct perf_event_context *child_ctx)
5368{
5369 struct perf_event *leader;
5370 struct perf_event *sub;
5371 struct perf_event *child_ctr;
5372
5373 leader = inherit_event(parent_event, parent, parent_ctx,
5374 child, NULL, child_ctx);
5375 if (IS_ERR(leader))
5376 return PTR_ERR(leader);
5377 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
5378 child_ctr = inherit_event(sub, parent, parent_ctx,
5379 child, leader, child_ctx);
5380 if (IS_ERR(child_ctr))
5381 return PTR_ERR(child_ctr);
5382 }
5383 return 0;
5384}
5385
5386static void sync_child_event(struct perf_event *child_event,
5387 struct task_struct *child)
5388{
5389 struct perf_event *parent_event = child_event->parent;
5390 u64 child_val;
5391
5392 if (child_event->attr.inherit_stat)
5393 perf_event_read_event(child_event, child);
5394
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005395 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005396
5397 /*
5398 * Add back the child's count to the parent's count:
5399 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02005400 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005401 atomic64_add(child_event->total_time_enabled,
5402 &parent_event->child_total_time_enabled);
5403 atomic64_add(child_event->total_time_running,
5404 &parent_event->child_total_time_running);
5405
5406 /*
5407 * Remove this event from the parent's list
5408 */
5409 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5410 mutex_lock(&parent_event->child_mutex);
5411 list_del_init(&child_event->child_list);
5412 mutex_unlock(&parent_event->child_mutex);
5413
5414 /*
5415 * Release the parent event, if this was the last
5416 * reference to it.
5417 */
5418 fput(parent_event->filp);
5419}
5420
5421static void
5422__perf_event_exit_task(struct perf_event *child_event,
5423 struct perf_event_context *child_ctx,
5424 struct task_struct *child)
5425{
5426 struct perf_event *parent_event;
5427
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005428 perf_event_remove_from_context(child_event);
5429
5430 parent_event = child_event->parent;
5431 /*
5432 * It can happen that parent exits first, and has events
5433 * that are still around due to the child reference. These
5434 * events need to be zapped - but otherwise linger.
5435 */
5436 if (parent_event) {
5437 sync_child_event(child_event, child);
5438 free_event(child_event);
5439 }
5440}
5441
5442/*
5443 * When a child task exits, feed back event values to parent events.
5444 */
5445void perf_event_exit_task(struct task_struct *child)
5446{
5447 struct perf_event *child_event, *tmp;
5448 struct perf_event_context *child_ctx;
5449 unsigned long flags;
5450
5451 if (likely(!child->perf_event_ctxp)) {
5452 perf_event_task(child, NULL, 0);
5453 return;
5454 }
5455
5456 local_irq_save(flags);
5457 /*
5458 * We can't reschedule here because interrupts are disabled,
5459 * and either child is current or it is a task that can't be
5460 * scheduled, so we are now safe from rescheduling changing
5461 * our context.
5462 */
5463 child_ctx = child->perf_event_ctxp;
5464 __perf_event_task_sched_out(child_ctx);
5465
5466 /*
5467 * Take the context lock here so that if find_get_context is
5468 * reading child->perf_event_ctxp, we wait until it has
5469 * incremented the context's refcount before we do put_ctx below.
5470 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01005471 raw_spin_lock(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005472 child->perf_event_ctxp = NULL;
5473 /*
5474 * If this context is a clone; unclone it so it can't get
5475 * swapped to another process while we're removing all
5476 * the events from it.
5477 */
5478 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01005479 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01005480 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005481
5482 /*
5483 * Report the task dead after unscheduling the events so that we
5484 * won't get any samples after PERF_RECORD_EXIT. We can however still
5485 * get a few PERF_RECORD_READ events.
5486 */
5487 perf_event_task(child, child_ctx, 0);
5488
5489 /*
5490 * We can recurse on the same lock type through:
5491 *
5492 * __perf_event_exit_task()
5493 * sync_child_event()
5494 * fput(parent_event->filp)
5495 * perf_release()
5496 * mutex_lock(&ctx->mutex)
5497 *
5498 * But since its the parent context it won't be the same instance.
5499 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02005500 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005501
5502again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005503 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5504 group_entry)
5505 __perf_event_exit_task(child_event, child_ctx, child);
5506
5507 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005508 group_entry)
5509 __perf_event_exit_task(child_event, child_ctx, child);
5510
5511 /*
5512 * If the last event was a group event, it will have appended all
5513 * its siblings to the list, but we obtained 'tmp' before that which
5514 * will still point to the list head terminating the iteration.
5515 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005516 if (!list_empty(&child_ctx->pinned_groups) ||
5517 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005518 goto again;
5519
5520 mutex_unlock(&child_ctx->mutex);
5521
5522 put_ctx(child_ctx);
5523}
5524
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005525static void perf_free_event(struct perf_event *event,
5526 struct perf_event_context *ctx)
5527{
5528 struct perf_event *parent = event->parent;
5529
5530 if (WARN_ON_ONCE(!parent))
5531 return;
5532
5533 mutex_lock(&parent->child_mutex);
5534 list_del_init(&event->child_list);
5535 mutex_unlock(&parent->child_mutex);
5536
5537 fput(parent->filp);
5538
Peter Zijlstra8a495422010-05-27 15:47:49 +02005539 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005540 list_del_event(event, ctx);
5541 free_event(event);
5542}
5543
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005544/*
5545 * free an unexposed, unused context as created by inheritance by
5546 * init_task below, used by fork() in case of fail.
5547 */
5548void perf_event_free_task(struct task_struct *task)
5549{
5550 struct perf_event_context *ctx = task->perf_event_ctxp;
5551 struct perf_event *event, *tmp;
5552
5553 if (!ctx)
5554 return;
5555
5556 mutex_lock(&ctx->mutex);
5557again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005558 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5559 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005560
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005561 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5562 group_entry)
5563 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005564
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005565 if (!list_empty(&ctx->pinned_groups) ||
5566 !list_empty(&ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005567 goto again;
5568
5569 mutex_unlock(&ctx->mutex);
5570
5571 put_ctx(ctx);
5572}
5573
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005574static int
5575inherit_task_group(struct perf_event *event, struct task_struct *parent,
5576 struct perf_event_context *parent_ctx,
5577 struct task_struct *child,
5578 int *inherited_all)
5579{
5580 int ret;
5581 struct perf_event_context *child_ctx = child->perf_event_ctxp;
5582
5583 if (!event->attr.inherit) {
5584 *inherited_all = 0;
5585 return 0;
5586 }
5587
5588 if (!child_ctx) {
5589 /*
5590 * This is executed from the parent task context, so
5591 * inherit events that have been marked for cloning.
5592 * First allocate and initialize a context for the
5593 * child.
5594 */
5595
5596 child_ctx = kzalloc(sizeof(struct perf_event_context),
5597 GFP_KERNEL);
5598 if (!child_ctx)
5599 return -ENOMEM;
5600
5601 __perf_event_init_context(child_ctx, child);
5602 child->perf_event_ctxp = child_ctx;
5603 get_task_struct(child);
5604 }
5605
5606 ret = inherit_group(event, parent, parent_ctx,
5607 child, child_ctx);
5608
5609 if (ret)
5610 *inherited_all = 0;
5611
5612 return ret;
5613}
5614
5615
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005616/*
5617 * Initialize the perf_event context in task_struct
5618 */
5619int perf_event_init_task(struct task_struct *child)
5620{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005621 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005622 struct perf_event_context *cloned_ctx;
5623 struct perf_event *event;
5624 struct task_struct *parent = current;
5625 int inherited_all = 1;
5626 int ret = 0;
5627
5628 child->perf_event_ctxp = NULL;
5629
5630 mutex_init(&child->perf_event_mutex);
5631 INIT_LIST_HEAD(&child->perf_event_list);
5632
5633 if (likely(!parent->perf_event_ctxp))
5634 return 0;
5635
5636 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005637 * If the parent's context is a clone, pin it so it won't get
5638 * swapped under us.
5639 */
5640 parent_ctx = perf_pin_task_context(parent);
5641
5642 /*
5643 * No need to check if parent_ctx != NULL here; since we saw
5644 * it non-NULL earlier, the only reason for it to become NULL
5645 * is if we exit, and since we're currently in the middle of
5646 * a fork we can't be exiting at the same time.
5647 */
5648
5649 /*
5650 * Lock the parent list. No need to lock the child - not PID
5651 * hashed yet and not running, so nobody can access it.
5652 */
5653 mutex_lock(&parent_ctx->mutex);
5654
5655 /*
5656 * We dont have to disable NMIs - we are only looking at
5657 * the list, not manipulating it:
5658 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005659 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
5660 ret = inherit_task_group(event, parent, parent_ctx, child,
5661 &inherited_all);
5662 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005663 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005664 }
5665
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005666 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
5667 ret = inherit_task_group(event, parent, parent_ctx, child,
5668 &inherited_all);
5669 if (ret)
5670 break;
5671 }
5672
5673 child_ctx = child->perf_event_ctxp;
5674
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01005675 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005676 /*
5677 * Mark the child context as a clone of the parent
5678 * context, or of whatever the parent is a clone of.
5679 * Note that if the parent is a clone, it could get
5680 * uncloned at any point, but that doesn't matter
5681 * because the list of events and the generation
5682 * count can't have changed since we took the mutex.
5683 */
5684 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
5685 if (cloned_ctx) {
5686 child_ctx->parent_ctx = cloned_ctx;
5687 child_ctx->parent_gen = parent_ctx->parent_gen;
5688 } else {
5689 child_ctx->parent_ctx = parent_ctx;
5690 child_ctx->parent_gen = parent_ctx->generation;
5691 }
5692 get_ctx(child_ctx->parent_ctx);
5693 }
5694
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005695 mutex_unlock(&parent_ctx->mutex);
5696
5697 perf_unpin_context(parent_ctx);
5698
5699 return ret;
5700}
5701
Paul Mackerras220b1402010-03-10 20:45:52 +11005702static void __init perf_event_init_all_cpus(void)
5703{
5704 int cpu;
5705 struct perf_cpu_context *cpuctx;
5706
5707 for_each_possible_cpu(cpu) {
5708 cpuctx = &per_cpu(perf_cpu_context, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005709 mutex_init(&cpuctx->hlist_mutex);
Paul Mackerras220b1402010-03-10 20:45:52 +11005710 __perf_event_init_context(&cpuctx->ctx, NULL);
5711 }
5712}
5713
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005714static void __cpuinit perf_event_init_cpu(int cpu)
5715{
5716 struct perf_cpu_context *cpuctx;
5717
5718 cpuctx = &per_cpu(perf_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005719
5720 spin_lock(&perf_resource_lock);
5721 cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
5722 spin_unlock(&perf_resource_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005723
5724 mutex_lock(&cpuctx->hlist_mutex);
5725 if (cpuctx->hlist_refcount > 0) {
5726 struct swevent_hlist *hlist;
5727
5728 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5729 WARN_ON_ONCE(!hlist);
5730 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
5731 }
5732 mutex_unlock(&cpuctx->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005733}
5734
5735#ifdef CONFIG_HOTPLUG_CPU
5736static void __perf_event_exit_cpu(void *info)
5737{
5738 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
5739 struct perf_event_context *ctx = &cpuctx->ctx;
5740 struct perf_event *event, *tmp;
5741
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005742 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5743 __perf_event_remove_from_context(event);
5744 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005745 __perf_event_remove_from_context(event);
5746}
5747static void perf_event_exit_cpu(int cpu)
5748{
5749 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
5750 struct perf_event_context *ctx = &cpuctx->ctx;
5751
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005752 mutex_lock(&cpuctx->hlist_mutex);
5753 swevent_hlist_release(cpuctx);
5754 mutex_unlock(&cpuctx->hlist_mutex);
5755
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005756 mutex_lock(&ctx->mutex);
5757 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
5758 mutex_unlock(&ctx->mutex);
5759}
5760#else
5761static inline void perf_event_exit_cpu(int cpu) { }
5762#endif
5763
5764static int __cpuinit
5765perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
5766{
5767 unsigned int cpu = (long)hcpu;
5768
Peter Zijlstra5e116372010-06-11 13:35:08 +02005769 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005770
5771 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02005772 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005773 perf_event_init_cpu(cpu);
5774 break;
5775
Peter Zijlstra5e116372010-06-11 13:35:08 +02005776 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005777 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005778 perf_event_exit_cpu(cpu);
5779 break;
5780
5781 default:
5782 break;
5783 }
5784
5785 return NOTIFY_OK;
5786}
5787
5788/*
5789 * This has to have a higher priority than migration_notifier in sched.c.
5790 */
5791static struct notifier_block __cpuinitdata perf_cpu_nb = {
5792 .notifier_call = perf_cpu_notify,
5793 .priority = 20,
5794};
5795
5796void __init perf_event_init(void)
5797{
Paul Mackerras220b1402010-03-10 20:45:52 +11005798 perf_event_init_all_cpus();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005799 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
5800 (void *)(long)smp_processor_id());
5801 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
5802 (void *)(long)smp_processor_id());
5803 register_cpu_notifier(&perf_cpu_nb);
5804}
5805
Andi Kleenc9be0a32010-01-05 12:47:58 +01005806static ssize_t perf_show_reserve_percpu(struct sysdev_class *class,
5807 struct sysdev_class_attribute *attr,
5808 char *buf)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005809{
5810 return sprintf(buf, "%d\n", perf_reserved_percpu);
5811}
5812
5813static ssize_t
5814perf_set_reserve_percpu(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +01005815 struct sysdev_class_attribute *attr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005816 const char *buf,
5817 size_t count)
5818{
5819 struct perf_cpu_context *cpuctx;
5820 unsigned long val;
5821 int err, cpu, mpt;
5822
5823 err = strict_strtoul(buf, 10, &val);
5824 if (err)
5825 return err;
5826 if (val > perf_max_events)
5827 return -EINVAL;
5828
5829 spin_lock(&perf_resource_lock);
5830 perf_reserved_percpu = val;
5831 for_each_online_cpu(cpu) {
5832 cpuctx = &per_cpu(perf_cpu_context, cpu);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01005833 raw_spin_lock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005834 mpt = min(perf_max_events - cpuctx->ctx.nr_events,
5835 perf_max_events - perf_reserved_percpu);
5836 cpuctx->max_pertask = mpt;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01005837 raw_spin_unlock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005838 }
5839 spin_unlock(&perf_resource_lock);
5840
5841 return count;
5842}
5843
Andi Kleenc9be0a32010-01-05 12:47:58 +01005844static ssize_t perf_show_overcommit(struct sysdev_class *class,
5845 struct sysdev_class_attribute *attr,
5846 char *buf)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005847{
5848 return sprintf(buf, "%d\n", perf_overcommit);
5849}
5850
5851static ssize_t
Andi Kleenc9be0a32010-01-05 12:47:58 +01005852perf_set_overcommit(struct sysdev_class *class,
5853 struct sysdev_class_attribute *attr,
5854 const char *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005855{
5856 unsigned long val;
5857 int err;
5858
5859 err = strict_strtoul(buf, 10, &val);
5860 if (err)
5861 return err;
5862 if (val > 1)
5863 return -EINVAL;
5864
5865 spin_lock(&perf_resource_lock);
5866 perf_overcommit = val;
5867 spin_unlock(&perf_resource_lock);
5868
5869 return count;
5870}
5871
5872static SYSDEV_CLASS_ATTR(
5873 reserve_percpu,
5874 0644,
5875 perf_show_reserve_percpu,
5876 perf_set_reserve_percpu
5877 );
5878
5879static SYSDEV_CLASS_ATTR(
5880 overcommit,
5881 0644,
5882 perf_show_overcommit,
5883 perf_set_overcommit
5884 );
5885
5886static struct attribute *perfclass_attrs[] = {
5887 &attr_reserve_percpu.attr,
5888 &attr_overcommit.attr,
5889 NULL
5890};
5891
5892static struct attribute_group perfclass_attr_group = {
5893 .attrs = perfclass_attrs,
5894 .name = "perf_events",
5895};
5896
5897static int __init perf_event_sysfs_init(void)
5898{
5899 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
5900 &perfclass_attr_group);
5901}
5902device_initcall(perf_event_sysfs_init);