blob: a9047463fd83e26cf0e7756ce6a447d8dec5fb2c [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
88static DEFINE_PER_CPU(int, perf_disable_count);
89
Ingo Molnarcdd6c482009-09-21 12:02:48 +020090void perf_disable(void)
91{
Peter Zijlstra32975a42010-03-06 19:49:19 +010092 if (!__get_cpu_var(perf_disable_count)++)
93 hw_perf_disable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +020094}
95
96void perf_enable(void)
97{
Peter Zijlstra32975a42010-03-06 19:49:19 +010098 if (!--__get_cpu_var(perf_disable_count))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020099 hw_perf_enable();
100}
101
102static void get_ctx(struct perf_event_context *ctx)
103{
104 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
105}
106
107static void free_ctx(struct rcu_head *head)
108{
109 struct perf_event_context *ctx;
110
111 ctx = container_of(head, struct perf_event_context, rcu_head);
112 kfree(ctx);
113}
114
115static void put_ctx(struct perf_event_context *ctx)
116{
117 if (atomic_dec_and_test(&ctx->refcount)) {
118 if (ctx->parent_ctx)
119 put_ctx(ctx->parent_ctx);
120 if (ctx->task)
121 put_task_struct(ctx->task);
122 call_rcu(&ctx->rcu_head, free_ctx);
123 }
124}
125
126static void unclone_ctx(struct perf_event_context *ctx)
127{
128 if (ctx->parent_ctx) {
129 put_ctx(ctx->parent_ctx);
130 ctx->parent_ctx = NULL;
131 }
132}
133
134/*
135 * If we inherit events we want to return the parent event id
136 * to userspace.
137 */
138static u64 primary_event_id(struct perf_event *event)
139{
140 u64 id = event->id;
141
142 if (event->parent)
143 id = event->parent->id;
144
145 return id;
146}
147
148/*
149 * Get the perf_event_context for a task and lock it.
150 * This has to cope with with the fact that until it is locked,
151 * the context could get moved to another task.
152 */
153static struct perf_event_context *
154perf_lock_task_context(struct task_struct *task, unsigned long *flags)
155{
156 struct perf_event_context *ctx;
157
158 rcu_read_lock();
159 retry:
160 ctx = rcu_dereference(task->perf_event_ctxp);
161 if (ctx) {
162 /*
163 * If this context is a clone of another, it might
164 * get swapped for another underneath us by
165 * perf_event_task_sched_out, though the
166 * rcu_read_lock() protects us from any context
167 * getting freed. Lock the context and check if it
168 * got swapped before we could get the lock, and retry
169 * if so. If we locked the right context, then it
170 * can't get swapped on us any more.
171 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100172 raw_spin_lock_irqsave(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200173 if (ctx != rcu_dereference(task->perf_event_ctxp)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100174 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200175 goto retry;
176 }
177
178 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100179 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200180 ctx = NULL;
181 }
182 }
183 rcu_read_unlock();
184 return ctx;
185}
186
187/*
188 * Get the context for a task and increment its pin_count so it
189 * can't get swapped to another task. This also increments its
190 * reference count so that the context can't get freed.
191 */
192static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
193{
194 struct perf_event_context *ctx;
195 unsigned long flags;
196
197 ctx = perf_lock_task_context(task, &flags);
198 if (ctx) {
199 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100200 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200201 }
202 return ctx;
203}
204
205static void perf_unpin_context(struct perf_event_context *ctx)
206{
207 unsigned long flags;
208
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100209 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200210 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100211 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200212 put_ctx(ctx);
213}
214
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100215static inline u64 perf_clock(void)
216{
Peter Zijlstra24691ea2010-02-26 16:36:23 +0100217 return cpu_clock(raw_smp_processor_id());
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100218}
219
220/*
221 * Update the record of the current time in a context.
222 */
223static void update_context_time(struct perf_event_context *ctx)
224{
225 u64 now = perf_clock();
226
227 ctx->time += now - ctx->timestamp;
228 ctx->timestamp = now;
229}
230
231/*
232 * Update the total_time_enabled and total_time_running fields for a event.
233 */
234static void update_event_times(struct perf_event *event)
235{
236 struct perf_event_context *ctx = event->ctx;
237 u64 run_end;
238
239 if (event->state < PERF_EVENT_STATE_INACTIVE ||
240 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
241 return;
242
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100243 if (ctx->is_active)
244 run_end = ctx->time;
245 else
246 run_end = event->tstamp_stopped;
247
248 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100249
250 if (event->state == PERF_EVENT_STATE_INACTIVE)
251 run_end = event->tstamp_stopped;
252 else
253 run_end = ctx->time;
254
255 event->total_time_running = run_end - event->tstamp_running;
256}
257
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100258static struct list_head *
259ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
260{
261 if (event->attr.pinned)
262 return &ctx->pinned_groups;
263 else
264 return &ctx->flexible_groups;
265}
266
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200267/*
268 * Add a event from the lists for its context.
269 * Must be called with ctx->mutex and ctx->lock held.
270 */
271static void
272list_add_event(struct perf_event *event, struct perf_event_context *ctx)
273{
274 struct perf_event *group_leader = event->group_leader;
275
276 /*
277 * Depending on whether it is a standalone or sibling event,
278 * add it straight to the context's event list, or to the group
279 * leader's sibling list:
280 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100281 if (group_leader == event) {
282 struct list_head *list;
283
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100284 if (is_software_event(event))
285 event->group_flags |= PERF_GROUP_SOFTWARE;
286
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100287 list = ctx_group_list(event, ctx);
288 list_add_tail(&event->group_entry, list);
289 } else {
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100290 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
291 !is_software_event(event))
292 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
293
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200294 list_add_tail(&event->group_entry, &group_leader->sibling_list);
295 group_leader->nr_siblings++;
296 }
297
298 list_add_rcu(&event->event_entry, &ctx->event_list);
299 ctx->nr_events++;
300 if (event->attr.inherit_stat)
301 ctx->nr_stat++;
302}
303
304/*
305 * Remove a event from the lists for its context.
306 * Must be called with ctx->mutex and ctx->lock held.
307 */
308static void
309list_del_event(struct perf_event *event, struct perf_event_context *ctx)
310{
311 struct perf_event *sibling, *tmp;
312
313 if (list_empty(&event->group_entry))
314 return;
315 ctx->nr_events--;
316 if (event->attr.inherit_stat)
317 ctx->nr_stat--;
318
319 list_del_init(&event->group_entry);
320 list_del_rcu(&event->event_entry);
321
322 if (event->group_leader != event)
323 event->group_leader->nr_siblings--;
324
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100325 update_event_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800326
327 /*
328 * If event was in error state, then keep it
329 * that way, otherwise bogus counts will be
330 * returned on read(). The only way to get out
331 * of error state is by explicit re-enabling
332 * of the event
333 */
334 if (event->state > PERF_EVENT_STATE_OFF)
335 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra2e2af502009-11-23 11:37:25 +0100336
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200337 /*
338 * If this was a group event with sibling events then
339 * upgrade the siblings to singleton events by adding them
340 * to the context list directly:
341 */
342 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100343 struct list_head *list;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200344
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100345 list = ctx_group_list(event, ctx);
346 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200347 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100348
349 /* Inherit group flags from the previous leader */
350 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200351 }
352}
353
354static void
355event_sched_out(struct perf_event *event,
356 struct perf_cpu_context *cpuctx,
357 struct perf_event_context *ctx)
358{
359 if (event->state != PERF_EVENT_STATE_ACTIVE)
360 return;
361
362 event->state = PERF_EVENT_STATE_INACTIVE;
363 if (event->pending_disable) {
364 event->pending_disable = 0;
365 event->state = PERF_EVENT_STATE_OFF;
366 }
367 event->tstamp_stopped = ctx->time;
368 event->pmu->disable(event);
369 event->oncpu = -1;
370
371 if (!is_software_event(event))
372 cpuctx->active_oncpu--;
373 ctx->nr_active--;
374 if (event->attr.exclusive || !cpuctx->active_oncpu)
375 cpuctx->exclusive = 0;
376}
377
378static void
379group_sched_out(struct perf_event *group_event,
380 struct perf_cpu_context *cpuctx,
381 struct perf_event_context *ctx)
382{
383 struct perf_event *event;
384
385 if (group_event->state != PERF_EVENT_STATE_ACTIVE)
386 return;
387
388 event_sched_out(group_event, cpuctx, ctx);
389
390 /*
391 * Schedule out siblings (if any):
392 */
393 list_for_each_entry(event, &group_event->sibling_list, group_entry)
394 event_sched_out(event, cpuctx, ctx);
395
396 if (group_event->attr.exclusive)
397 cpuctx->exclusive = 0;
398}
399
400/*
401 * Cross CPU call to remove a performance event
402 *
403 * We disable the event on the hardware level first. After that we
404 * remove it from the context list.
405 */
406static void __perf_event_remove_from_context(void *info)
407{
408 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
409 struct perf_event *event = info;
410 struct perf_event_context *ctx = event->ctx;
411
412 /*
413 * If this is a task context, we need to check whether it is
414 * the current task context of this cpu. If not it has been
415 * scheduled out before the smp call arrived.
416 */
417 if (ctx->task && cpuctx->task_ctx != ctx)
418 return;
419
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100420 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200421 /*
422 * Protect the list operation against NMI by disabling the
423 * events on a global level.
424 */
425 perf_disable();
426
427 event_sched_out(event, cpuctx, ctx);
428
429 list_del_event(event, ctx);
430
431 if (!ctx->task) {
432 /*
433 * Allow more per task events with respect to the
434 * reservation:
435 */
436 cpuctx->max_pertask =
437 min(perf_max_events - ctx->nr_events,
438 perf_max_events - perf_reserved_percpu);
439 }
440
441 perf_enable();
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100442 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200443}
444
445
446/*
447 * Remove the event from a task's (or a CPU's) list of events.
448 *
449 * Must be called with ctx->mutex held.
450 *
451 * CPU events are removed with a smp call. For task events we only
452 * call when the task is on a CPU.
453 *
454 * If event->ctx is a cloned context, callers must make sure that
455 * every task struct that event->ctx->task could possibly point to
456 * remains valid. This is OK when called from perf_release since
457 * that only calls us on the top-level context, which can't be a clone.
458 * When called from perf_event_exit_task, it's OK because the
459 * context has been detached from its task.
460 */
461static void perf_event_remove_from_context(struct perf_event *event)
462{
463 struct perf_event_context *ctx = event->ctx;
464 struct task_struct *task = ctx->task;
465
466 if (!task) {
467 /*
468 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200469 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200470 */
471 smp_call_function_single(event->cpu,
472 __perf_event_remove_from_context,
473 event, 1);
474 return;
475 }
476
477retry:
478 task_oncpu_function_call(task, __perf_event_remove_from_context,
479 event);
480
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100481 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200482 /*
483 * If the context is active we need to retry the smp call.
484 */
485 if (ctx->nr_active && !list_empty(&event->group_entry)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100486 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200487 goto retry;
488 }
489
490 /*
491 * The lock prevents that this context is scheduled in so we
492 * can remove the event safely, if the call above did not
493 * succeed.
494 */
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +0100495 if (!list_empty(&event->group_entry))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200496 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100497 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200498}
499
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200500/*
501 * Update total_time_enabled and total_time_running for all events in a group.
502 */
503static void update_group_times(struct perf_event *leader)
504{
505 struct perf_event *event;
506
507 update_event_times(leader);
508 list_for_each_entry(event, &leader->sibling_list, group_entry)
509 update_event_times(event);
510}
511
512/*
513 * Cross CPU call to disable a performance event
514 */
515static void __perf_event_disable(void *info)
516{
517 struct perf_event *event = info;
518 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
519 struct perf_event_context *ctx = event->ctx;
520
521 /*
522 * If this is a per-task event, need to check whether this
523 * event's task is the current task on this cpu.
524 */
525 if (ctx->task && cpuctx->task_ctx != ctx)
526 return;
527
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100528 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200529
530 /*
531 * If the event is on, turn it off.
532 * If it is in error state, leave it in error state.
533 */
534 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
535 update_context_time(ctx);
536 update_group_times(event);
537 if (event == event->group_leader)
538 group_sched_out(event, cpuctx, ctx);
539 else
540 event_sched_out(event, cpuctx, ctx);
541 event->state = PERF_EVENT_STATE_OFF;
542 }
543
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100544 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200545}
546
547/*
548 * Disable a event.
549 *
550 * If event->ctx is a cloned context, callers must make sure that
551 * every task struct that event->ctx->task could possibly point to
552 * remains valid. This condition is satisifed when called through
553 * perf_event_for_each_child or perf_event_for_each because they
554 * hold the top-level event's child_mutex, so any descendant that
555 * goes to exit will block in sync_child_event.
556 * When called from perf_pending_event it's OK because event->ctx
557 * is the current context on this CPU and preemption is disabled,
558 * hence we can't get into perf_event_task_sched_out for this context.
559 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100560void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200561{
562 struct perf_event_context *ctx = event->ctx;
563 struct task_struct *task = ctx->task;
564
565 if (!task) {
566 /*
567 * Disable the event on the cpu that it's on
568 */
569 smp_call_function_single(event->cpu, __perf_event_disable,
570 event, 1);
571 return;
572 }
573
574 retry:
575 task_oncpu_function_call(task, __perf_event_disable, event);
576
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100577 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200578 /*
579 * If the event is still active, we need to retry the cross-call.
580 */
581 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100582 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200583 goto retry;
584 }
585
586 /*
587 * Since we have the lock this context can't be scheduled
588 * in, so we can change the state safely.
589 */
590 if (event->state == PERF_EVENT_STATE_INACTIVE) {
591 update_group_times(event);
592 event->state = PERF_EVENT_STATE_OFF;
593 }
594
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100595 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200596}
597
598static int
599event_sched_in(struct perf_event *event,
600 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100601 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200602{
603 if (event->state <= PERF_EVENT_STATE_OFF)
604 return 0;
605
606 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +0100607 event->oncpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200608 /*
609 * The new state must be visible before we turn it on in the hardware:
610 */
611 smp_wmb();
612
613 if (event->pmu->enable(event)) {
614 event->state = PERF_EVENT_STATE_INACTIVE;
615 event->oncpu = -1;
616 return -EAGAIN;
617 }
618
619 event->tstamp_running += ctx->time - event->tstamp_stopped;
620
621 if (!is_software_event(event))
622 cpuctx->active_oncpu++;
623 ctx->nr_active++;
624
625 if (event->attr.exclusive)
626 cpuctx->exclusive = 1;
627
628 return 0;
629}
630
631static int
632group_sched_in(struct perf_event *group_event,
633 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100634 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200635{
Lin Ming6bde9b62010-04-23 13:56:00 +0800636 struct perf_event *event, *partial_group = NULL;
637 const struct pmu *pmu = group_event->pmu;
638 bool txn = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200639 int ret;
640
641 if (group_event->state == PERF_EVENT_STATE_OFF)
642 return 0;
643
Lin Ming6bde9b62010-04-23 13:56:00 +0800644 /* Check if group transaction availabe */
645 if (pmu->start_txn)
646 txn = true;
647
648 if (txn)
649 pmu->start_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200650
Peter Zijlstra6e377382010-02-11 13:21:58 +0100651 if (event_sched_in(group_event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200652 return -EAGAIN;
653
654 /*
655 * Schedule in siblings as one group (if any):
656 */
657 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Peter Zijlstra6e377382010-02-11 13:21:58 +0100658 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200659 partial_group = event;
660 goto group_error;
661 }
662 }
663
Paul Mackerras6e851582010-05-08 20:58:00 +1000664 if (!txn)
665 return 0;
Lin Ming6bde9b62010-04-23 13:56:00 +0800666
Paul Mackerras6e851582010-05-08 20:58:00 +1000667 ret = pmu->commit_txn(pmu);
668 if (!ret) {
669 pmu->cancel_txn(pmu);
670 return 0;
Lin Ming6bde9b62010-04-23 13:56:00 +0800671 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200672
673group_error:
Lin Ming6bde9b62010-04-23 13:56:00 +0800674 if (txn)
675 pmu->cancel_txn(pmu);
676
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200677 /*
678 * Groups can be scheduled in as one unit only, so undo any
679 * partial group before returning:
680 */
681 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
682 if (event == partial_group)
683 break;
684 event_sched_out(event, cpuctx, ctx);
685 }
686 event_sched_out(group_event, cpuctx, ctx);
687
688 return -EAGAIN;
689}
690
691/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200692 * Work out whether we can put this event group on the CPU now.
693 */
694static int group_can_go_on(struct perf_event *event,
695 struct perf_cpu_context *cpuctx,
696 int can_add_hw)
697{
698 /*
699 * Groups consisting entirely of software events can always go on.
700 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100701 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200702 return 1;
703 /*
704 * If an exclusive group is already on, no other hardware
705 * events can go on.
706 */
707 if (cpuctx->exclusive)
708 return 0;
709 /*
710 * If this group is exclusive and there are already
711 * events on the CPU, it can't go on.
712 */
713 if (event->attr.exclusive && cpuctx->active_oncpu)
714 return 0;
715 /*
716 * Otherwise, try to add it if all previous groups were able
717 * to go on.
718 */
719 return can_add_hw;
720}
721
722static void add_event_to_ctx(struct perf_event *event,
723 struct perf_event_context *ctx)
724{
725 list_add_event(event, ctx);
726 event->tstamp_enabled = ctx->time;
727 event->tstamp_running = ctx->time;
728 event->tstamp_stopped = ctx->time;
729}
730
731/*
732 * Cross CPU call to install and enable a performance event
733 *
734 * Must be called with ctx->mutex held
735 */
736static void __perf_install_in_context(void *info)
737{
738 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
739 struct perf_event *event = info;
740 struct perf_event_context *ctx = event->ctx;
741 struct perf_event *leader = event->group_leader;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200742 int err;
743
744 /*
745 * If this is a task context, we need to check whether it is
746 * the current task context of this cpu. If not it has been
747 * scheduled out before the smp call arrived.
748 * Or possibly this is the right context but it isn't
749 * on this cpu because it had no events.
750 */
751 if (ctx->task && cpuctx->task_ctx != ctx) {
752 if (cpuctx->task_ctx || ctx->task != current)
753 return;
754 cpuctx->task_ctx = ctx;
755 }
756
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100757 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200758 ctx->is_active = 1;
759 update_context_time(ctx);
760
761 /*
762 * Protect the list operation against NMI by disabling the
763 * events on a global level. NOP for non NMI based events.
764 */
765 perf_disable();
766
767 add_event_to_ctx(event, ctx);
768
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100769 if (event->cpu != -1 && event->cpu != smp_processor_id())
770 goto unlock;
771
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200772 /*
773 * Don't put the event on if it is disabled or if
774 * it is in a group and the group isn't on.
775 */
776 if (event->state != PERF_EVENT_STATE_INACTIVE ||
777 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
778 goto unlock;
779
780 /*
781 * An exclusive event can't go on if there are already active
782 * hardware events, and no hardware event can go on if there
783 * is already an exclusive event on.
784 */
785 if (!group_can_go_on(event, cpuctx, 1))
786 err = -EEXIST;
787 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100788 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200789
790 if (err) {
791 /*
792 * This event couldn't go on. If it is in a group
793 * then we have to pull the whole group off.
794 * If the event group is pinned then put it in error state.
795 */
796 if (leader != event)
797 group_sched_out(leader, cpuctx, ctx);
798 if (leader->attr.pinned) {
799 update_group_times(leader);
800 leader->state = PERF_EVENT_STATE_ERROR;
801 }
802 }
803
804 if (!err && !ctx->task && cpuctx->max_pertask)
805 cpuctx->max_pertask--;
806
807 unlock:
808 perf_enable();
809
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100810 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200811}
812
813/*
814 * Attach a performance event to a context
815 *
816 * First we add the event to the list with the hardware enable bit
817 * in event->hw_config cleared.
818 *
819 * If the event is attached to a task which is on a CPU we use a smp
820 * call to enable it in the task context. The task might have been
821 * scheduled away, but we check this in the smp call again.
822 *
823 * Must be called with ctx->mutex held.
824 */
825static void
826perf_install_in_context(struct perf_event_context *ctx,
827 struct perf_event *event,
828 int cpu)
829{
830 struct task_struct *task = ctx->task;
831
832 if (!task) {
833 /*
834 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200835 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200836 */
837 smp_call_function_single(cpu, __perf_install_in_context,
838 event, 1);
839 return;
840 }
841
842retry:
843 task_oncpu_function_call(task, __perf_install_in_context,
844 event);
845
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100846 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200847 /*
848 * we need to retry the smp call.
849 */
850 if (ctx->is_active && list_empty(&event->group_entry)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100851 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200852 goto retry;
853 }
854
855 /*
856 * The lock prevents that this context is scheduled in so we
857 * can add the event safely, if it the call above did not
858 * succeed.
859 */
860 if (list_empty(&event->group_entry))
861 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100862 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200863}
864
865/*
866 * Put a event into inactive state and update time fields.
867 * Enabling the leader of a group effectively enables all
868 * the group members that aren't explicitly disabled, so we
869 * have to update their ->tstamp_enabled also.
870 * Note: this works for group members as well as group leaders
871 * since the non-leader members' sibling_lists will be empty.
872 */
873static void __perf_event_mark_enabled(struct perf_event *event,
874 struct perf_event_context *ctx)
875{
876 struct perf_event *sub;
877
878 event->state = PERF_EVENT_STATE_INACTIVE;
879 event->tstamp_enabled = ctx->time - event->total_time_enabled;
880 list_for_each_entry(sub, &event->sibling_list, group_entry)
881 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
882 sub->tstamp_enabled =
883 ctx->time - sub->total_time_enabled;
884}
885
886/*
887 * Cross CPU call to enable a performance event
888 */
889static void __perf_event_enable(void *info)
890{
891 struct perf_event *event = info;
892 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
893 struct perf_event_context *ctx = event->ctx;
894 struct perf_event *leader = event->group_leader;
895 int err;
896
897 /*
898 * If this is a per-task event, need to check whether this
899 * event's task is the current task on this cpu.
900 */
901 if (ctx->task && cpuctx->task_ctx != ctx) {
902 if (cpuctx->task_ctx || ctx->task != current)
903 return;
904 cpuctx->task_ctx = ctx;
905 }
906
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100907 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200908 ctx->is_active = 1;
909 update_context_time(ctx);
910
911 if (event->state >= PERF_EVENT_STATE_INACTIVE)
912 goto unlock;
913 __perf_event_mark_enabled(event, ctx);
914
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100915 if (event->cpu != -1 && event->cpu != smp_processor_id())
916 goto unlock;
917
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200918 /*
919 * If the event is in a group and isn't the group leader,
920 * then don't put it on unless the group is on.
921 */
922 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
923 goto unlock;
924
925 if (!group_can_go_on(event, cpuctx, 1)) {
926 err = -EEXIST;
927 } else {
928 perf_disable();
929 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +0100930 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200931 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100932 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200933 perf_enable();
934 }
935
936 if (err) {
937 /*
938 * If this event can't go on and it's part of a
939 * group, then the whole group has to come off.
940 */
941 if (leader != event)
942 group_sched_out(leader, cpuctx, ctx);
943 if (leader->attr.pinned) {
944 update_group_times(leader);
945 leader->state = PERF_EVENT_STATE_ERROR;
946 }
947 }
948
949 unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100950 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200951}
952
953/*
954 * Enable a event.
955 *
956 * If event->ctx is a cloned context, callers must make sure that
957 * every task struct that event->ctx->task could possibly point to
958 * remains valid. This condition is satisfied when called through
959 * perf_event_for_each_child or perf_event_for_each as described
960 * for perf_event_disable.
961 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100962void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200963{
964 struct perf_event_context *ctx = event->ctx;
965 struct task_struct *task = ctx->task;
966
967 if (!task) {
968 /*
969 * Enable the event on the cpu that it's on
970 */
971 smp_call_function_single(event->cpu, __perf_event_enable,
972 event, 1);
973 return;
974 }
975
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100976 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200977 if (event->state >= PERF_EVENT_STATE_INACTIVE)
978 goto out;
979
980 /*
981 * If the event is in error state, clear that first.
982 * That way, if we see the event in error state below, we
983 * know that it has gone back into error state, as distinct
984 * from the task having been scheduled away before the
985 * cross-call arrived.
986 */
987 if (event->state == PERF_EVENT_STATE_ERROR)
988 event->state = PERF_EVENT_STATE_OFF;
989
990 retry:
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100991 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200992 task_oncpu_function_call(task, __perf_event_enable, event);
993
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100994 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200995
996 /*
997 * If the context is active and the event is still off,
998 * we need to retry the cross-call.
999 */
1000 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1001 goto retry;
1002
1003 /*
1004 * Since we have the lock this context can't be scheduled
1005 * in, so we can change the state safely.
1006 */
1007 if (event->state == PERF_EVENT_STATE_OFF)
1008 __perf_event_mark_enabled(event, ctx);
1009
1010 out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001011 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001012}
1013
1014static int perf_event_refresh(struct perf_event *event, int refresh)
1015{
1016 /*
1017 * not supported on inherited events
1018 */
1019 if (event->attr.inherit)
1020 return -EINVAL;
1021
1022 atomic_add(refresh, &event->event_limit);
1023 perf_event_enable(event);
1024
1025 return 0;
1026}
1027
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001028enum event_type_t {
1029 EVENT_FLEXIBLE = 0x1,
1030 EVENT_PINNED = 0x2,
1031 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1032};
1033
1034static void ctx_sched_out(struct perf_event_context *ctx,
1035 struct perf_cpu_context *cpuctx,
1036 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001037{
1038 struct perf_event *event;
1039
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001040 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001041 ctx->is_active = 0;
1042 if (likely(!ctx->nr_events))
1043 goto out;
1044 update_context_time(ctx);
1045
1046 perf_disable();
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001047 if (!ctx->nr_active)
1048 goto out_enable;
1049
1050 if (event_type & EVENT_PINNED)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001051 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1052 group_sched_out(event, cpuctx, ctx);
1053
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001054 if (event_type & EVENT_FLEXIBLE)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001055 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001056 group_sched_out(event, cpuctx, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001057
1058 out_enable:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001059 perf_enable();
1060 out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001061 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001062}
1063
1064/*
1065 * Test whether two contexts are equivalent, i.e. whether they
1066 * have both been cloned from the same version of the same context
1067 * and they both have the same number of enabled events.
1068 * If the number of enabled events is the same, then the set
1069 * of enabled events should be the same, because these are both
1070 * inherited contexts, therefore we can't access individual events
1071 * in them directly with an fd; we can only enable/disable all
1072 * events via prctl, or enable/disable all events in a family
1073 * via ioctl, which will have the same effect on both contexts.
1074 */
1075static int context_equiv(struct perf_event_context *ctx1,
1076 struct perf_event_context *ctx2)
1077{
1078 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1079 && ctx1->parent_gen == ctx2->parent_gen
1080 && !ctx1->pin_count && !ctx2->pin_count;
1081}
1082
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001083static void __perf_event_sync_stat(struct perf_event *event,
1084 struct perf_event *next_event)
1085{
1086 u64 value;
1087
1088 if (!event->attr.inherit_stat)
1089 return;
1090
1091 /*
1092 * Update the event value, we cannot use perf_event_read()
1093 * because we're in the middle of a context switch and have IRQs
1094 * disabled, which upsets smp_call_function_single(), however
1095 * we know the event must be on the current CPU, therefore we
1096 * don't need to use it.
1097 */
1098 switch (event->state) {
1099 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001100 event->pmu->read(event);
1101 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001102
1103 case PERF_EVENT_STATE_INACTIVE:
1104 update_event_times(event);
1105 break;
1106
1107 default:
1108 break;
1109 }
1110
1111 /*
1112 * In order to keep per-task stats reliable we need to flip the event
1113 * values when we flip the contexts.
1114 */
1115 value = atomic64_read(&next_event->count);
1116 value = atomic64_xchg(&event->count, value);
1117 atomic64_set(&next_event->count, value);
1118
1119 swap(event->total_time_enabled, next_event->total_time_enabled);
1120 swap(event->total_time_running, next_event->total_time_running);
1121
1122 /*
1123 * Since we swizzled the values, update the user visible data too.
1124 */
1125 perf_event_update_userpage(event);
1126 perf_event_update_userpage(next_event);
1127}
1128
1129#define list_next_entry(pos, member) \
1130 list_entry(pos->member.next, typeof(*pos), member)
1131
1132static void perf_event_sync_stat(struct perf_event_context *ctx,
1133 struct perf_event_context *next_ctx)
1134{
1135 struct perf_event *event, *next_event;
1136
1137 if (!ctx->nr_stat)
1138 return;
1139
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001140 update_context_time(ctx);
1141
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001142 event = list_first_entry(&ctx->event_list,
1143 struct perf_event, event_entry);
1144
1145 next_event = list_first_entry(&next_ctx->event_list,
1146 struct perf_event, event_entry);
1147
1148 while (&event->event_entry != &ctx->event_list &&
1149 &next_event->event_entry != &next_ctx->event_list) {
1150
1151 __perf_event_sync_stat(event, next_event);
1152
1153 event = list_next_entry(event, event_entry);
1154 next_event = list_next_entry(next_event, event_entry);
1155 }
1156}
1157
1158/*
1159 * Called from scheduler to remove the events of the current task,
1160 * with interrupts disabled.
1161 *
1162 * We stop each event and update the event value in event->count.
1163 *
1164 * This does not protect us against NMI, but disable()
1165 * sets the disabled bit in the control field of event _before_
1166 * accessing the event control register. If a NMI hits, then it will
1167 * not restart the event.
1168 */
1169void perf_event_task_sched_out(struct task_struct *task,
Peter Zijlstra49f47432009-12-27 11:51:52 +01001170 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001171{
Peter Zijlstra49f47432009-12-27 11:51:52 +01001172 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001173 struct perf_event_context *ctx = task->perf_event_ctxp;
1174 struct perf_event_context *next_ctx;
1175 struct perf_event_context *parent;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001176 int do_switch = 1;
1177
Frederic Weisbeckere49a5bd2010-03-22 19:40:03 +01001178 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001179
1180 if (likely(!ctx || !cpuctx->task_ctx))
1181 return;
1182
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001183 rcu_read_lock();
1184 parent = rcu_dereference(ctx->parent_ctx);
1185 next_ctx = next->perf_event_ctxp;
1186 if (parent && next_ctx &&
1187 rcu_dereference(next_ctx->parent_ctx) == parent) {
1188 /*
1189 * Looks like the two contexts are clones, so we might be
1190 * able to optimize the context switch. We lock both
1191 * contexts and check that they are clones under the
1192 * lock (including re-checking that neither has been
1193 * uncloned in the meantime). It doesn't matter which
1194 * order we take the locks because no other cpu could
1195 * be trying to lock both of these tasks.
1196 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001197 raw_spin_lock(&ctx->lock);
1198 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001199 if (context_equiv(ctx, next_ctx)) {
1200 /*
1201 * XXX do we need a memory barrier of sorts
1202 * wrt to rcu_dereference() of perf_event_ctxp
1203 */
1204 task->perf_event_ctxp = next_ctx;
1205 next->perf_event_ctxp = ctx;
1206 ctx->task = next;
1207 next_ctx->task = task;
1208 do_switch = 0;
1209
1210 perf_event_sync_stat(ctx, next_ctx);
1211 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001212 raw_spin_unlock(&next_ctx->lock);
1213 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001214 }
1215 rcu_read_unlock();
1216
1217 if (do_switch) {
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001218 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001219 cpuctx->task_ctx = NULL;
1220 }
1221}
1222
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001223static void task_ctx_sched_out(struct perf_event_context *ctx,
1224 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001225{
1226 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1227
1228 if (!cpuctx->task_ctx)
1229 return;
1230
1231 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1232 return;
1233
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001234 ctx_sched_out(ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001235 cpuctx->task_ctx = NULL;
1236}
1237
1238/*
1239 * Called with IRQs disabled
1240 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001241static void __perf_event_task_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001242{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001243 task_ctx_sched_out(ctx, EVENT_ALL);
1244}
1245
1246/*
1247 * Called with IRQs disabled
1248 */
1249static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1250 enum event_type_t event_type)
1251{
1252 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001253}
1254
1255static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001256ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001257 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001258{
1259 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001260
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001261 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1262 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001263 continue;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001264 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001265 continue;
1266
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001267 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001268 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001269
1270 /*
1271 * If this pinned group hasn't been scheduled,
1272 * put it in error state.
1273 */
1274 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1275 update_group_times(event);
1276 event->state = PERF_EVENT_STATE_ERROR;
1277 }
1278 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001279}
1280
1281static void
1282ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001283 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001284{
1285 struct perf_event *event;
1286 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001287
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001288 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1289 /* Ignore events in OFF or ERROR state */
1290 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001291 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001292 /*
1293 * Listen to the 'cpu' scheduling filter constraint
1294 * of events:
1295 */
Peter Zijlstra6e377382010-02-11 13:21:58 +01001296 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001297 continue;
1298
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001299 if (group_can_go_on(event, cpuctx, can_add_hw))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001300 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001301 can_add_hw = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001302 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001303}
1304
1305static void
1306ctx_sched_in(struct perf_event_context *ctx,
1307 struct perf_cpu_context *cpuctx,
1308 enum event_type_t event_type)
1309{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001310 raw_spin_lock(&ctx->lock);
1311 ctx->is_active = 1;
1312 if (likely(!ctx->nr_events))
1313 goto out;
1314
1315 ctx->timestamp = perf_clock();
1316
1317 perf_disable();
1318
1319 /*
1320 * First go through the list and put on any pinned groups
1321 * in order to give them the best chance of going on.
1322 */
1323 if (event_type & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001324 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001325
1326 /* Then walk through the lower prio flexible groups */
1327 if (event_type & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001328 ctx_flexible_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001329
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001330 perf_enable();
1331 out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001332 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001333}
1334
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001335static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1336 enum event_type_t event_type)
1337{
1338 struct perf_event_context *ctx = &cpuctx->ctx;
1339
1340 ctx_sched_in(ctx, cpuctx, event_type);
1341}
1342
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001343static void task_ctx_sched_in(struct task_struct *task,
1344 enum event_type_t event_type)
1345{
1346 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1347 struct perf_event_context *ctx = task->perf_event_ctxp;
1348
1349 if (likely(!ctx))
1350 return;
1351 if (cpuctx->task_ctx == ctx)
1352 return;
1353 ctx_sched_in(ctx, cpuctx, event_type);
1354 cpuctx->task_ctx = ctx;
1355}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001356/*
1357 * Called from scheduler to add the events of the current task
1358 * with interrupts disabled.
1359 *
1360 * We restore the event value and then enable it.
1361 *
1362 * This does not protect us against NMI, but enable()
1363 * sets the enabled bit in the control field of event _before_
1364 * accessing the event control register. If a NMI hits, then it will
1365 * keep the event running.
1366 */
Peter Zijlstra49f47432009-12-27 11:51:52 +01001367void perf_event_task_sched_in(struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001368{
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001369 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1370 struct perf_event_context *ctx = task->perf_event_ctxp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001371
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001372 if (likely(!ctx))
1373 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001374
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001375 if (cpuctx->task_ctx == ctx)
1376 return;
1377
eranian@google.com9b33fa62010-03-10 22:26:05 -08001378 perf_disable();
1379
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001380 /*
1381 * We want to keep the following priority order:
1382 * cpu pinned (that don't need to move), task pinned,
1383 * cpu flexible, task flexible.
1384 */
1385 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1386
1387 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1388 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1389 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1390
1391 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08001392
1393 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001394}
1395
1396#define MAX_INTERRUPTS (~0ULL)
1397
1398static void perf_log_throttle(struct perf_event *event, int enable);
1399
Peter Zijlstraabd50712010-01-26 18:50:16 +01001400static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1401{
1402 u64 frequency = event->attr.sample_freq;
1403 u64 sec = NSEC_PER_SEC;
1404 u64 divisor, dividend;
1405
1406 int count_fls, nsec_fls, frequency_fls, sec_fls;
1407
1408 count_fls = fls64(count);
1409 nsec_fls = fls64(nsec);
1410 frequency_fls = fls64(frequency);
1411 sec_fls = 30;
1412
1413 /*
1414 * We got @count in @nsec, with a target of sample_freq HZ
1415 * the target period becomes:
1416 *
1417 * @count * 10^9
1418 * period = -------------------
1419 * @nsec * sample_freq
1420 *
1421 */
1422
1423 /*
1424 * Reduce accuracy by one bit such that @a and @b converge
1425 * to a similar magnitude.
1426 */
1427#define REDUCE_FLS(a, b) \
1428do { \
1429 if (a##_fls > b##_fls) { \
1430 a >>= 1; \
1431 a##_fls--; \
1432 } else { \
1433 b >>= 1; \
1434 b##_fls--; \
1435 } \
1436} while (0)
1437
1438 /*
1439 * Reduce accuracy until either term fits in a u64, then proceed with
1440 * the other, so that finally we can do a u64/u64 division.
1441 */
1442 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1443 REDUCE_FLS(nsec, frequency);
1444 REDUCE_FLS(sec, count);
1445 }
1446
1447 if (count_fls + sec_fls > 64) {
1448 divisor = nsec * frequency;
1449
1450 while (count_fls + sec_fls > 64) {
1451 REDUCE_FLS(count, sec);
1452 divisor >>= 1;
1453 }
1454
1455 dividend = count * sec;
1456 } else {
1457 dividend = count * sec;
1458
1459 while (nsec_fls + frequency_fls > 64) {
1460 REDUCE_FLS(nsec, frequency);
1461 dividend >>= 1;
1462 }
1463
1464 divisor = nsec * frequency;
1465 }
1466
1467 return div64_u64(dividend, divisor);
1468}
1469
Stephane Eraniand76a0812010-02-08 17:06:01 +02001470static void perf_event_stop(struct perf_event *event)
1471{
1472 if (!event->pmu->stop)
1473 return event->pmu->disable(event);
1474
1475 return event->pmu->stop(event);
1476}
1477
1478static int perf_event_start(struct perf_event *event)
1479{
1480 if (!event->pmu->start)
1481 return event->pmu->enable(event);
1482
1483 return event->pmu->start(event);
1484}
1485
Peter Zijlstraabd50712010-01-26 18:50:16 +01001486static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001487{
1488 struct hw_perf_event *hwc = &event->hw;
1489 u64 period, sample_period;
1490 s64 delta;
1491
Peter Zijlstraabd50712010-01-26 18:50:16 +01001492 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001493
1494 delta = (s64)(period - hwc->sample_period);
1495 delta = (delta + 7) / 8; /* low pass filter */
1496
1497 sample_period = hwc->sample_period + delta;
1498
1499 if (!sample_period)
1500 sample_period = 1;
1501
1502 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001503
1504 if (atomic64_read(&hwc->period_left) > 8*sample_period) {
1505 perf_disable();
Stephane Eraniand76a0812010-02-08 17:06:01 +02001506 perf_event_stop(event);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001507 atomic64_set(&hwc->period_left, 0);
Stephane Eraniand76a0812010-02-08 17:06:01 +02001508 perf_event_start(event);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001509 perf_enable();
1510 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001511}
1512
1513static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
1514{
1515 struct perf_event *event;
1516 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001517 u64 interrupts, now;
1518 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001519
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001520 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11001521 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001522 if (event->state != PERF_EVENT_STATE_ACTIVE)
1523 continue;
1524
Peter Zijlstra5d27c232009-12-17 13:16:32 +01001525 if (event->cpu != -1 && event->cpu != smp_processor_id())
1526 continue;
1527
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001528 hwc = &event->hw;
1529
1530 interrupts = hwc->interrupts;
1531 hwc->interrupts = 0;
1532
1533 /*
1534 * unthrottle events on the tick
1535 */
1536 if (interrupts == MAX_INTERRUPTS) {
1537 perf_log_throttle(event, 1);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001538 perf_disable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001539 event->pmu->unthrottle(event);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001540 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001541 }
1542
1543 if (!event->attr.freq || !event->attr.sample_freq)
1544 continue;
1545
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001546 perf_disable();
Peter Zijlstraabd50712010-01-26 18:50:16 +01001547 event->pmu->read(event);
1548 now = atomic64_read(&event->count);
1549 delta = now - hwc->freq_count_stamp;
1550 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001551
Peter Zijlstraabd50712010-01-26 18:50:16 +01001552 if (delta > 0)
1553 perf_adjust_period(event, TICK_NSEC, delta);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001554 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001555 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001556 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001557}
1558
1559/*
1560 * Round-robin a context's events:
1561 */
1562static void rotate_ctx(struct perf_event_context *ctx)
1563{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001564 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001565
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001566 /* Rotate the first entry last of non-pinned groups */
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001567 list_rotate_left(&ctx->flexible_groups);
1568
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001569 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001570}
1571
Peter Zijlstra49f47432009-12-27 11:51:52 +01001572void perf_event_task_tick(struct task_struct *curr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001573{
1574 struct perf_cpu_context *cpuctx;
1575 struct perf_event_context *ctx;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001576 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001577
1578 if (!atomic_read(&nr_events))
1579 return;
1580
Peter Zijlstra49f47432009-12-27 11:51:52 +01001581 cpuctx = &__get_cpu_var(perf_cpu_context);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001582 if (cpuctx->ctx.nr_events &&
1583 cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1584 rotate = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001585
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001586 ctx = curr->perf_event_ctxp;
1587 if (ctx && ctx->nr_events && ctx->nr_events != ctx->nr_active)
1588 rotate = 1;
Peter Zijlstra9717e6c2010-01-28 13:57:44 +01001589
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001590 perf_ctx_adjust_freq(&cpuctx->ctx);
1591 if (ctx)
1592 perf_ctx_adjust_freq(ctx);
1593
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001594 if (!rotate)
1595 return;
1596
1597 perf_disable();
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001598 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001599 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001600 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001601
1602 rotate_ctx(&cpuctx->ctx);
1603 if (ctx)
1604 rotate_ctx(ctx);
1605
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001606 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001607 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001608 task_ctx_sched_in(curr, EVENT_FLEXIBLE);
Peter Zijlstra9717e6c2010-01-28 13:57:44 +01001609 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001610}
1611
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001612static int event_enable_on_exec(struct perf_event *event,
1613 struct perf_event_context *ctx)
1614{
1615 if (!event->attr.enable_on_exec)
1616 return 0;
1617
1618 event->attr.enable_on_exec = 0;
1619 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1620 return 0;
1621
1622 __perf_event_mark_enabled(event, ctx);
1623
1624 return 1;
1625}
1626
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001627/*
1628 * Enable all of a task's events that have been marked enable-on-exec.
1629 * This expects task == current.
1630 */
1631static void perf_event_enable_on_exec(struct task_struct *task)
1632{
1633 struct perf_event_context *ctx;
1634 struct perf_event *event;
1635 unsigned long flags;
1636 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001637 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001638
1639 local_irq_save(flags);
1640 ctx = task->perf_event_ctxp;
1641 if (!ctx || !ctx->nr_events)
1642 goto out;
1643
1644 __perf_event_task_sched_out(ctx);
1645
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001646 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001647
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001648 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1649 ret = event_enable_on_exec(event, ctx);
1650 if (ret)
1651 enabled = 1;
1652 }
1653
1654 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1655 ret = event_enable_on_exec(event, ctx);
1656 if (ret)
1657 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001658 }
1659
1660 /*
1661 * Unclone this context if we enabled any event.
1662 */
1663 if (enabled)
1664 unclone_ctx(ctx);
1665
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001666 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001667
Peter Zijlstra49f47432009-12-27 11:51:52 +01001668 perf_event_task_sched_in(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001669 out:
1670 local_irq_restore(flags);
1671}
1672
1673/*
1674 * Cross CPU call to read the hardware event
1675 */
1676static void __perf_event_read(void *info)
1677{
1678 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1679 struct perf_event *event = info;
1680 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001681
1682 /*
1683 * If this is a task context, we need to check whether it is
1684 * the current task context of this cpu. If not it has been
1685 * scheduled out before the smp call arrived. In that case
1686 * event->count would have been updated to a recent sample
1687 * when the event was scheduled out.
1688 */
1689 if (ctx->task && cpuctx->task_ctx != ctx)
1690 return;
1691
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001692 raw_spin_lock(&ctx->lock);
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001693 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001694 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001695 raw_spin_unlock(&ctx->lock);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001696
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001697 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001698}
1699
1700static u64 perf_event_read(struct perf_event *event)
1701{
1702 /*
1703 * If event is enabled and currently active on a CPU, update the
1704 * value in the event structure:
1705 */
1706 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1707 smp_call_function_single(event->oncpu,
1708 __perf_event_read, event, 1);
1709 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001710 struct perf_event_context *ctx = event->ctx;
1711 unsigned long flags;
1712
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001713 raw_spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001714 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001715 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001716 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001717 }
1718
1719 return atomic64_read(&event->count);
1720}
1721
1722/*
1723 * Initialize the perf_event context in a task_struct:
1724 */
1725static void
1726__perf_event_init_context(struct perf_event_context *ctx,
1727 struct task_struct *task)
1728{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001729 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001730 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001731 INIT_LIST_HEAD(&ctx->pinned_groups);
1732 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001733 INIT_LIST_HEAD(&ctx->event_list);
1734 atomic_set(&ctx->refcount, 1);
1735 ctx->task = task;
1736}
1737
1738static struct perf_event_context *find_get_context(pid_t pid, int cpu)
1739{
1740 struct perf_event_context *ctx;
1741 struct perf_cpu_context *cpuctx;
1742 struct task_struct *task;
1743 unsigned long flags;
1744 int err;
1745
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001746 if (pid == -1 && cpu != -1) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001747 /* Must be root to operate on a CPU event: */
1748 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1749 return ERR_PTR(-EACCES);
1750
Paul Mackerras0f624e72009-12-15 19:40:32 +11001751 if (cpu < 0 || cpu >= nr_cpumask_bits)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001752 return ERR_PTR(-EINVAL);
1753
1754 /*
1755 * We could be clever and allow to attach a event to an
1756 * offline CPU and activate it when the CPU comes up, but
1757 * that's for later.
1758 */
Rusty Russellf6325e32009-12-17 11:43:08 -06001759 if (!cpu_online(cpu))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001760 return ERR_PTR(-ENODEV);
1761
1762 cpuctx = &per_cpu(perf_cpu_context, cpu);
1763 ctx = &cpuctx->ctx;
1764 get_ctx(ctx);
1765
1766 return ctx;
1767 }
1768
1769 rcu_read_lock();
1770 if (!pid)
1771 task = current;
1772 else
1773 task = find_task_by_vpid(pid);
1774 if (task)
1775 get_task_struct(task);
1776 rcu_read_unlock();
1777
1778 if (!task)
1779 return ERR_PTR(-ESRCH);
1780
1781 /*
1782 * Can't attach events to a dying task.
1783 */
1784 err = -ESRCH;
1785 if (task->flags & PF_EXITING)
1786 goto errout;
1787
1788 /* Reuse ptrace permission checks for now. */
1789 err = -EACCES;
1790 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1791 goto errout;
1792
1793 retry:
1794 ctx = perf_lock_task_context(task, &flags);
1795 if (ctx) {
1796 unclone_ctx(ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001797 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001798 }
1799
1800 if (!ctx) {
Xiao Guangrongaa5452d2009-12-09 11:28:13 +08001801 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001802 err = -ENOMEM;
1803 if (!ctx)
1804 goto errout;
1805 __perf_event_init_context(ctx, task);
1806 get_ctx(ctx);
1807 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
1808 /*
1809 * We raced with some other task; use
1810 * the context they set.
1811 */
1812 kfree(ctx);
1813 goto retry;
1814 }
1815 get_task_struct(task);
1816 }
1817
1818 put_task_struct(task);
1819 return ctx;
1820
1821 errout:
1822 put_task_struct(task);
1823 return ERR_PTR(err);
1824}
1825
Li Zefan6fb29152009-10-15 11:21:42 +08001826static void perf_event_free_filter(struct perf_event *event);
1827
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001828static void free_event_rcu(struct rcu_head *head)
1829{
1830 struct perf_event *event;
1831
1832 event = container_of(head, struct perf_event, rcu_head);
1833 if (event->ns)
1834 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08001835 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001836 kfree(event);
1837}
1838
1839static void perf_pending_sync(struct perf_event *event);
1840
1841static void free_event(struct perf_event *event)
1842{
1843 perf_pending_sync(event);
1844
1845 if (!event->parent) {
1846 atomic_dec(&nr_events);
1847 if (event->attr.mmap)
1848 atomic_dec(&nr_mmap_events);
1849 if (event->attr.comm)
1850 atomic_dec(&nr_comm_events);
1851 if (event->attr.task)
1852 atomic_dec(&nr_task_events);
1853 }
1854
1855 if (event->output) {
1856 fput(event->output->filp);
1857 event->output = NULL;
1858 }
1859
1860 if (event->destroy)
1861 event->destroy(event);
1862
1863 put_ctx(event->ctx);
1864 call_rcu(&event->rcu_head, free_event_rcu);
1865}
1866
Arjan van de Venfb0459d2009-09-25 12:25:56 +02001867int perf_event_release_kernel(struct perf_event *event)
1868{
1869 struct perf_event_context *ctx = event->ctx;
1870
1871 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02001872 /*
1873 * There are two ways this annotation is useful:
1874 *
1875 * 1) there is a lock recursion from perf_event_exit_task
1876 * see the comment there.
1877 *
1878 * 2) there is a lock-inversion with mmap_sem through
1879 * perf_event_read_group(), which takes faults while
1880 * holding ctx->mutex, however this is called after
1881 * the last filedesc died, so there is no possibility
1882 * to trigger the AB-BA case.
1883 */
1884 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02001885 perf_event_remove_from_context(event);
1886 mutex_unlock(&ctx->mutex);
1887
1888 mutex_lock(&event->owner->perf_event_mutex);
1889 list_del_init(&event->owner_entry);
1890 mutex_unlock(&event->owner->perf_event_mutex);
1891 put_task_struct(event->owner);
1892
1893 free_event(event);
1894
1895 return 0;
1896}
1897EXPORT_SYMBOL_GPL(perf_event_release_kernel);
1898
Peter Zijlstraa66a3052009-11-23 11:37:23 +01001899/*
1900 * Called when the last reference to the file is gone.
1901 */
1902static int perf_release(struct inode *inode, struct file *file)
1903{
1904 struct perf_event *event = file->private_data;
1905
1906 file->private_data = NULL;
1907
1908 return perf_event_release_kernel(event);
1909}
1910
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001911static int perf_event_read_size(struct perf_event *event)
1912{
1913 int entry = sizeof(u64); /* value */
1914 int size = 0;
1915 int nr = 1;
1916
1917 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1918 size += sizeof(u64);
1919
1920 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1921 size += sizeof(u64);
1922
1923 if (event->attr.read_format & PERF_FORMAT_ID)
1924 entry += sizeof(u64);
1925
1926 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1927 nr += event->group_leader->nr_siblings;
1928 size += sizeof(u64);
1929 }
1930
1931 size += entry * nr;
1932
1933 return size;
1934}
1935
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001936u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001937{
1938 struct perf_event *child;
1939 u64 total = 0;
1940
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001941 *enabled = 0;
1942 *running = 0;
1943
Peter Zijlstra6f105812009-11-20 22:19:56 +01001944 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001945 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001946 *enabled += event->total_time_enabled +
1947 atomic64_read(&event->child_total_time_enabled);
1948 *running += event->total_time_running +
1949 atomic64_read(&event->child_total_time_running);
1950
1951 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001952 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001953 *enabled += child->total_time_enabled;
1954 *running += child->total_time_running;
1955 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01001956 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001957
1958 return total;
1959}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02001960EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001961
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001962static int perf_event_read_group(struct perf_event *event,
1963 u64 read_format, char __user *buf)
1964{
1965 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01001966 int n = 0, size = 0, ret = -EFAULT;
1967 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01001968 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001969 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01001970
Peter Zijlstra6f105812009-11-20 22:19:56 +01001971 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001972 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001973
1974 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001975 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1976 values[n++] = enabled;
1977 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1978 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01001979 values[n++] = count;
1980 if (read_format & PERF_FORMAT_ID)
1981 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001982
1983 size = n * sizeof(u64);
1984
1985 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01001986 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001987
Peter Zijlstra6f105812009-11-20 22:19:56 +01001988 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001989
1990 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01001991 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001992
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001993 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01001994 if (read_format & PERF_FORMAT_ID)
1995 values[n++] = primary_event_id(sub);
1996
1997 size = n * sizeof(u64);
1998
Stephane Eranian184d3da2009-11-23 21:40:49 -08001999 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01002000 ret = -EFAULT;
2001 goto unlock;
2002 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01002003
2004 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002005 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002006unlock:
2007 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002008
Peter Zijlstraabf48682009-11-20 22:19:49 +01002009 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002010}
2011
2012static int perf_event_read_one(struct perf_event *event,
2013 u64 read_format, char __user *buf)
2014{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002015 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002016 u64 values[4];
2017 int n = 0;
2018
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002019 values[n++] = perf_event_read_value(event, &enabled, &running);
2020 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2021 values[n++] = enabled;
2022 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2023 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002024 if (read_format & PERF_FORMAT_ID)
2025 values[n++] = primary_event_id(event);
2026
2027 if (copy_to_user(buf, values, n * sizeof(u64)))
2028 return -EFAULT;
2029
2030 return n * sizeof(u64);
2031}
2032
2033/*
2034 * Read the performance event - simple non blocking version for now
2035 */
2036static ssize_t
2037perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2038{
2039 u64 read_format = event->attr.read_format;
2040 int ret;
2041
2042 /*
2043 * Return end-of-file for a read on a event that is in
2044 * error state (i.e. because it was pinned but it couldn't be
2045 * scheduled on to the CPU at some point).
2046 */
2047 if (event->state == PERF_EVENT_STATE_ERROR)
2048 return 0;
2049
2050 if (count < perf_event_read_size(event))
2051 return -ENOSPC;
2052
2053 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002054 if (read_format & PERF_FORMAT_GROUP)
2055 ret = perf_event_read_group(event, read_format, buf);
2056 else
2057 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002058
2059 return ret;
2060}
2061
2062static ssize_t
2063perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2064{
2065 struct perf_event *event = file->private_data;
2066
2067 return perf_read_hw(event, buf, count);
2068}
2069
2070static unsigned int perf_poll(struct file *file, poll_table *wait)
2071{
2072 struct perf_event *event = file->private_data;
2073 struct perf_mmap_data *data;
2074 unsigned int events = POLL_HUP;
2075
2076 rcu_read_lock();
2077 data = rcu_dereference(event->data);
2078 if (data)
2079 events = atomic_xchg(&data->poll, 0);
2080 rcu_read_unlock();
2081
2082 poll_wait(file, &event->waitq, wait);
2083
2084 return events;
2085}
2086
2087static void perf_event_reset(struct perf_event *event)
2088{
2089 (void)perf_event_read(event);
2090 atomic64_set(&event->count, 0);
2091 perf_event_update_userpage(event);
2092}
2093
2094/*
2095 * Holding the top-level event's child_mutex means that any
2096 * descendant process that has inherited this event will block
2097 * in sync_child_event if it goes to exit, thus satisfying the
2098 * task existence requirements of perf_event_enable/disable.
2099 */
2100static void perf_event_for_each_child(struct perf_event *event,
2101 void (*func)(struct perf_event *))
2102{
2103 struct perf_event *child;
2104
2105 WARN_ON_ONCE(event->ctx->parent_ctx);
2106 mutex_lock(&event->child_mutex);
2107 func(event);
2108 list_for_each_entry(child, &event->child_list, child_list)
2109 func(child);
2110 mutex_unlock(&event->child_mutex);
2111}
2112
2113static void perf_event_for_each(struct perf_event *event,
2114 void (*func)(struct perf_event *))
2115{
2116 struct perf_event_context *ctx = event->ctx;
2117 struct perf_event *sibling;
2118
2119 WARN_ON_ONCE(ctx->parent_ctx);
2120 mutex_lock(&ctx->mutex);
2121 event = event->group_leader;
2122
2123 perf_event_for_each_child(event, func);
2124 func(event);
2125 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2126 perf_event_for_each_child(event, func);
2127 mutex_unlock(&ctx->mutex);
2128}
2129
2130static int perf_event_period(struct perf_event *event, u64 __user *arg)
2131{
2132 struct perf_event_context *ctx = event->ctx;
2133 unsigned long size;
2134 int ret = 0;
2135 u64 value;
2136
2137 if (!event->attr.sample_period)
2138 return -EINVAL;
2139
2140 size = copy_from_user(&value, arg, sizeof(value));
2141 if (size != sizeof(value))
2142 return -EFAULT;
2143
2144 if (!value)
2145 return -EINVAL;
2146
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002147 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002148 if (event->attr.freq) {
2149 if (value > sysctl_perf_event_sample_rate) {
2150 ret = -EINVAL;
2151 goto unlock;
2152 }
2153
2154 event->attr.sample_freq = value;
2155 } else {
2156 event->attr.sample_period = value;
2157 event->hw.sample_period = value;
2158 }
2159unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002160 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002161
2162 return ret;
2163}
2164
Li Zefan6fb29152009-10-15 11:21:42 +08002165static int perf_event_set_output(struct perf_event *event, int output_fd);
2166static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002167
2168static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2169{
2170 struct perf_event *event = file->private_data;
2171 void (*func)(struct perf_event *);
2172 u32 flags = arg;
2173
2174 switch (cmd) {
2175 case PERF_EVENT_IOC_ENABLE:
2176 func = perf_event_enable;
2177 break;
2178 case PERF_EVENT_IOC_DISABLE:
2179 func = perf_event_disable;
2180 break;
2181 case PERF_EVENT_IOC_RESET:
2182 func = perf_event_reset;
2183 break;
2184
2185 case PERF_EVENT_IOC_REFRESH:
2186 return perf_event_refresh(event, arg);
2187
2188 case PERF_EVENT_IOC_PERIOD:
2189 return perf_event_period(event, (u64 __user *)arg);
2190
2191 case PERF_EVENT_IOC_SET_OUTPUT:
2192 return perf_event_set_output(event, arg);
2193
Li Zefan6fb29152009-10-15 11:21:42 +08002194 case PERF_EVENT_IOC_SET_FILTER:
2195 return perf_event_set_filter(event, (void __user *)arg);
2196
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002197 default:
2198 return -ENOTTY;
2199 }
2200
2201 if (flags & PERF_IOC_FLAG_GROUP)
2202 perf_event_for_each(event, func);
2203 else
2204 perf_event_for_each_child(event, func);
2205
2206 return 0;
2207}
2208
2209int perf_event_task_enable(void)
2210{
2211 struct perf_event *event;
2212
2213 mutex_lock(&current->perf_event_mutex);
2214 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2215 perf_event_for_each_child(event, perf_event_enable);
2216 mutex_unlock(&current->perf_event_mutex);
2217
2218 return 0;
2219}
2220
2221int perf_event_task_disable(void)
2222{
2223 struct perf_event *event;
2224
2225 mutex_lock(&current->perf_event_mutex);
2226 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2227 perf_event_for_each_child(event, perf_event_disable);
2228 mutex_unlock(&current->perf_event_mutex);
2229
2230 return 0;
2231}
2232
2233#ifndef PERF_EVENT_INDEX_OFFSET
2234# define PERF_EVENT_INDEX_OFFSET 0
2235#endif
2236
2237static int perf_event_index(struct perf_event *event)
2238{
2239 if (event->state != PERF_EVENT_STATE_ACTIVE)
2240 return 0;
2241
2242 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2243}
2244
2245/*
2246 * Callers need to ensure there can be no nesting of this function, otherwise
2247 * the seqlock logic goes bad. We can not serialize this because the arch
2248 * code calls this from NMI context.
2249 */
2250void perf_event_update_userpage(struct perf_event *event)
2251{
2252 struct perf_event_mmap_page *userpg;
2253 struct perf_mmap_data *data;
2254
2255 rcu_read_lock();
2256 data = rcu_dereference(event->data);
2257 if (!data)
2258 goto unlock;
2259
2260 userpg = data->user_page;
2261
2262 /*
2263 * Disable preemption so as to not let the corresponding user-space
2264 * spin too long if we get preempted.
2265 */
2266 preempt_disable();
2267 ++userpg->lock;
2268 barrier();
2269 userpg->index = perf_event_index(event);
2270 userpg->offset = atomic64_read(&event->count);
2271 if (event->state == PERF_EVENT_STATE_ACTIVE)
2272 userpg->offset -= atomic64_read(&event->hw.prev_count);
2273
2274 userpg->time_enabled = event->total_time_enabled +
2275 atomic64_read(&event->child_total_time_enabled);
2276
2277 userpg->time_running = event->total_time_running +
2278 atomic64_read(&event->child_total_time_running);
2279
2280 barrier();
2281 ++userpg->lock;
2282 preempt_enable();
2283unlock:
2284 rcu_read_unlock();
2285}
2286
Peter Zijlstra906010b2009-09-21 16:08:49 +02002287static unsigned long perf_data_size(struct perf_mmap_data *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002288{
Peter Zijlstra906010b2009-09-21 16:08:49 +02002289 return data->nr_pages << (PAGE_SHIFT + data->data_order);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002290}
2291
Peter Zijlstra906010b2009-09-21 16:08:49 +02002292#ifndef CONFIG_PERF_USE_VMALLOC
2293
2294/*
2295 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2296 */
2297
2298static struct page *
2299perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2300{
2301 if (pgoff > data->nr_pages)
2302 return NULL;
2303
2304 if (pgoff == 0)
2305 return virt_to_page(data->user_page);
2306
2307 return virt_to_page(data->data_pages[pgoff - 1]);
2308}
2309
2310static struct perf_mmap_data *
2311perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002312{
2313 struct perf_mmap_data *data;
2314 unsigned long size;
2315 int i;
2316
2317 WARN_ON(atomic_read(&event->mmap_count));
2318
2319 size = sizeof(struct perf_mmap_data);
2320 size += nr_pages * sizeof(void *);
2321
2322 data = kzalloc(size, GFP_KERNEL);
2323 if (!data)
2324 goto fail;
2325
2326 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
2327 if (!data->user_page)
2328 goto fail_user_page;
2329
2330 for (i = 0; i < nr_pages; i++) {
2331 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
2332 if (!data->data_pages[i])
2333 goto fail_data_pages;
2334 }
2335
Peter Zijlstra906010b2009-09-21 16:08:49 +02002336 data->data_order = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002337 data->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002338
Peter Zijlstra906010b2009-09-21 16:08:49 +02002339 return data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002340
2341fail_data_pages:
2342 for (i--; i >= 0; i--)
2343 free_page((unsigned long)data->data_pages[i]);
2344
2345 free_page((unsigned long)data->user_page);
2346
2347fail_user_page:
2348 kfree(data);
2349
2350fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02002351 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002352}
2353
2354static void perf_mmap_free_page(unsigned long addr)
2355{
2356 struct page *page = virt_to_page((void *)addr);
2357
2358 page->mapping = NULL;
2359 __free_page(page);
2360}
2361
Peter Zijlstra906010b2009-09-21 16:08:49 +02002362static void perf_mmap_data_free(struct perf_mmap_data *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002363{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002364 int i;
2365
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002366 perf_mmap_free_page((unsigned long)data->user_page);
2367 for (i = 0; i < data->nr_pages; i++)
2368 perf_mmap_free_page((unsigned long)data->data_pages[i]);
Kristian Høgsbergec70ccd2009-12-01 15:05:01 -05002369 kfree(data);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002370}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002371
Peter Zijlstra906010b2009-09-21 16:08:49 +02002372#else
2373
2374/*
2375 * Back perf_mmap() with vmalloc memory.
2376 *
2377 * Required for architectures that have d-cache aliasing issues.
2378 */
2379
2380static struct page *
2381perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2382{
2383 if (pgoff > (1UL << data->data_order))
2384 return NULL;
2385
2386 return vmalloc_to_page((void *)data->user_page + pgoff * PAGE_SIZE);
2387}
2388
2389static void perf_mmap_unmark_page(void *addr)
2390{
2391 struct page *page = vmalloc_to_page(addr);
2392
2393 page->mapping = NULL;
2394}
2395
2396static void perf_mmap_data_free_work(struct work_struct *work)
2397{
2398 struct perf_mmap_data *data;
2399 void *base;
2400 int i, nr;
2401
2402 data = container_of(work, struct perf_mmap_data, work);
2403 nr = 1 << data->data_order;
2404
2405 base = data->user_page;
2406 for (i = 0; i < nr + 1; i++)
2407 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2408
2409 vfree(base);
Kristian Høgsbergec70ccd2009-12-01 15:05:01 -05002410 kfree(data);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002411}
2412
2413static void perf_mmap_data_free(struct perf_mmap_data *data)
2414{
2415 schedule_work(&data->work);
2416}
2417
2418static struct perf_mmap_data *
2419perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
2420{
2421 struct perf_mmap_data *data;
2422 unsigned long size;
2423 void *all_buf;
2424
2425 WARN_ON(atomic_read(&event->mmap_count));
2426
2427 size = sizeof(struct perf_mmap_data);
2428 size += sizeof(void *);
2429
2430 data = kzalloc(size, GFP_KERNEL);
2431 if (!data)
2432 goto fail;
2433
2434 INIT_WORK(&data->work, perf_mmap_data_free_work);
2435
2436 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2437 if (!all_buf)
2438 goto fail_all_buf;
2439
2440 data->user_page = all_buf;
2441 data->data_pages[0] = all_buf + PAGE_SIZE;
2442 data->data_order = ilog2(nr_pages);
2443 data->nr_pages = 1;
2444
2445 return data;
2446
2447fail_all_buf:
2448 kfree(data);
2449
2450fail:
2451 return NULL;
2452}
2453
2454#endif
2455
2456static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2457{
2458 struct perf_event *event = vma->vm_file->private_data;
2459 struct perf_mmap_data *data;
2460 int ret = VM_FAULT_SIGBUS;
2461
2462 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2463 if (vmf->pgoff == 0)
2464 ret = 0;
2465 return ret;
2466 }
2467
2468 rcu_read_lock();
2469 data = rcu_dereference(event->data);
2470 if (!data)
2471 goto unlock;
2472
2473 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2474 goto unlock;
2475
2476 vmf->page = perf_mmap_to_page(data, vmf->pgoff);
2477 if (!vmf->page)
2478 goto unlock;
2479
2480 get_page(vmf->page);
2481 vmf->page->mapping = vma->vm_file->f_mapping;
2482 vmf->page->index = vmf->pgoff;
2483
2484 ret = 0;
2485unlock:
2486 rcu_read_unlock();
2487
2488 return ret;
2489}
2490
2491static void
2492perf_mmap_data_init(struct perf_event *event, struct perf_mmap_data *data)
2493{
2494 long max_size = perf_data_size(data);
2495
2496 atomic_set(&data->lock, -1);
2497
2498 if (event->attr.watermark) {
2499 data->watermark = min_t(long, max_size,
2500 event->attr.wakeup_watermark);
2501 }
2502
2503 if (!data->watermark)
Stephane Eranian8904b182009-11-20 22:19:57 +01002504 data->watermark = max_size / 2;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002505
2506
2507 rcu_assign_pointer(event->data, data);
2508}
2509
2510static void perf_mmap_data_free_rcu(struct rcu_head *rcu_head)
2511{
2512 struct perf_mmap_data *data;
2513
2514 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2515 perf_mmap_data_free(data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002516}
2517
Peter Zijlstra906010b2009-09-21 16:08:49 +02002518static void perf_mmap_data_release(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002519{
2520 struct perf_mmap_data *data = event->data;
2521
2522 WARN_ON(atomic_read(&event->mmap_count));
2523
2524 rcu_assign_pointer(event->data, NULL);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002525 call_rcu(&data->rcu_head, perf_mmap_data_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002526}
2527
2528static void perf_mmap_open(struct vm_area_struct *vma)
2529{
2530 struct perf_event *event = vma->vm_file->private_data;
2531
2532 atomic_inc(&event->mmap_count);
2533}
2534
2535static void perf_mmap_close(struct vm_area_struct *vma)
2536{
2537 struct perf_event *event = vma->vm_file->private_data;
2538
2539 WARN_ON_ONCE(event->ctx->parent_ctx);
2540 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstra906010b2009-09-21 16:08:49 +02002541 unsigned long size = perf_data_size(event->data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002542 struct user_struct *user = current_user();
2543
Peter Zijlstra906010b2009-09-21 16:08:49 +02002544 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002545 vma->vm_mm->locked_vm -= event->data->nr_locked;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002546 perf_mmap_data_release(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002547 mutex_unlock(&event->mmap_mutex);
2548 }
2549}
2550
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002551static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002552 .open = perf_mmap_open,
2553 .close = perf_mmap_close,
2554 .fault = perf_mmap_fault,
2555 .page_mkwrite = perf_mmap_fault,
2556};
2557
2558static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2559{
2560 struct perf_event *event = file->private_data;
2561 unsigned long user_locked, user_lock_limit;
2562 struct user_struct *user = current_user();
2563 unsigned long locked, lock_limit;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002564 struct perf_mmap_data *data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002565 unsigned long vma_size;
2566 unsigned long nr_pages;
2567 long user_extra, extra;
2568 int ret = 0;
2569
2570 if (!(vma->vm_flags & VM_SHARED))
2571 return -EINVAL;
2572
2573 vma_size = vma->vm_end - vma->vm_start;
2574 nr_pages = (vma_size / PAGE_SIZE) - 1;
2575
2576 /*
2577 * If we have data pages ensure they're a power-of-two number, so we
2578 * can do bitmasks instead of modulo.
2579 */
2580 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2581 return -EINVAL;
2582
2583 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2584 return -EINVAL;
2585
2586 if (vma->vm_pgoff != 0)
2587 return -EINVAL;
2588
2589 WARN_ON_ONCE(event->ctx->parent_ctx);
2590 mutex_lock(&event->mmap_mutex);
2591 if (event->output) {
2592 ret = -EINVAL;
2593 goto unlock;
2594 }
2595
2596 if (atomic_inc_not_zero(&event->mmap_count)) {
2597 if (nr_pages != event->data->nr_pages)
2598 ret = -EINVAL;
2599 goto unlock;
2600 }
2601
2602 user_extra = nr_pages + 1;
2603 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2604
2605 /*
2606 * Increase the limit linearly with more CPUs:
2607 */
2608 user_lock_limit *= num_online_cpus();
2609
2610 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2611
2612 extra = 0;
2613 if (user_locked > user_lock_limit)
2614 extra = user_locked - user_lock_limit;
2615
Jiri Slaby78d7d402010-03-05 13:42:54 -08002616 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002617 lock_limit >>= PAGE_SHIFT;
2618 locked = vma->vm_mm->locked_vm + extra;
2619
2620 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2621 !capable(CAP_IPC_LOCK)) {
2622 ret = -EPERM;
2623 goto unlock;
2624 }
2625
2626 WARN_ON(event->data);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002627
2628 data = perf_mmap_data_alloc(event, nr_pages);
2629 ret = -ENOMEM;
2630 if (!data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002631 goto unlock;
2632
Peter Zijlstra906010b2009-09-21 16:08:49 +02002633 ret = 0;
2634 perf_mmap_data_init(event, data);
2635
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002636 atomic_set(&event->mmap_count, 1);
2637 atomic_long_add(user_extra, &user->locked_vm);
2638 vma->vm_mm->locked_vm += extra;
2639 event->data->nr_locked = extra;
2640 if (vma->vm_flags & VM_WRITE)
2641 event->data->writable = 1;
2642
2643unlock:
2644 mutex_unlock(&event->mmap_mutex);
2645
2646 vma->vm_flags |= VM_RESERVED;
2647 vma->vm_ops = &perf_mmap_vmops;
2648
2649 return ret;
2650}
2651
2652static int perf_fasync(int fd, struct file *filp, int on)
2653{
2654 struct inode *inode = filp->f_path.dentry->d_inode;
2655 struct perf_event *event = filp->private_data;
2656 int retval;
2657
2658 mutex_lock(&inode->i_mutex);
2659 retval = fasync_helper(fd, filp, on, &event->fasync);
2660 mutex_unlock(&inode->i_mutex);
2661
2662 if (retval < 0)
2663 return retval;
2664
2665 return 0;
2666}
2667
2668static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01002669 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002670 .release = perf_release,
2671 .read = perf_read,
2672 .poll = perf_poll,
2673 .unlocked_ioctl = perf_ioctl,
2674 .compat_ioctl = perf_ioctl,
2675 .mmap = perf_mmap,
2676 .fasync = perf_fasync,
2677};
2678
2679/*
2680 * Perf event wakeup
2681 *
2682 * If there's data, ensure we set the poll() state and publish everything
2683 * to user-space before waking everybody up.
2684 */
2685
2686void perf_event_wakeup(struct perf_event *event)
2687{
2688 wake_up_all(&event->waitq);
2689
2690 if (event->pending_kill) {
2691 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
2692 event->pending_kill = 0;
2693 }
2694}
2695
2696/*
2697 * Pending wakeups
2698 *
2699 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2700 *
2701 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2702 * single linked list and use cmpxchg() to add entries lockless.
2703 */
2704
2705static void perf_pending_event(struct perf_pending_entry *entry)
2706{
2707 struct perf_event *event = container_of(entry,
2708 struct perf_event, pending);
2709
2710 if (event->pending_disable) {
2711 event->pending_disable = 0;
2712 __perf_event_disable(event);
2713 }
2714
2715 if (event->pending_wakeup) {
2716 event->pending_wakeup = 0;
2717 perf_event_wakeup(event);
2718 }
2719}
2720
2721#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2722
2723static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
2724 PENDING_TAIL,
2725};
2726
2727static void perf_pending_queue(struct perf_pending_entry *entry,
2728 void (*func)(struct perf_pending_entry *))
2729{
2730 struct perf_pending_entry **head;
2731
2732 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
2733 return;
2734
2735 entry->func = func;
2736
2737 head = &get_cpu_var(perf_pending_head);
2738
2739 do {
2740 entry->next = *head;
2741 } while (cmpxchg(head, entry->next, entry) != entry->next);
2742
2743 set_perf_event_pending();
2744
2745 put_cpu_var(perf_pending_head);
2746}
2747
2748static int __perf_pending_run(void)
2749{
2750 struct perf_pending_entry *list;
2751 int nr = 0;
2752
2753 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2754 while (list != PENDING_TAIL) {
2755 void (*func)(struct perf_pending_entry *);
2756 struct perf_pending_entry *entry = list;
2757
2758 list = list->next;
2759
2760 func = entry->func;
2761 entry->next = NULL;
2762 /*
2763 * Ensure we observe the unqueue before we issue the wakeup,
2764 * so that we won't be waiting forever.
2765 * -- see perf_not_pending().
2766 */
2767 smp_wmb();
2768
2769 func(entry);
2770 nr++;
2771 }
2772
2773 return nr;
2774}
2775
2776static inline int perf_not_pending(struct perf_event *event)
2777{
2778 /*
2779 * If we flush on whatever cpu we run, there is a chance we don't
2780 * need to wait.
2781 */
2782 get_cpu();
2783 __perf_pending_run();
2784 put_cpu();
2785
2786 /*
2787 * Ensure we see the proper queue state before going to sleep
2788 * so that we do not miss the wakeup. -- see perf_pending_handle()
2789 */
2790 smp_rmb();
2791 return event->pending.next == NULL;
2792}
2793
2794static void perf_pending_sync(struct perf_event *event)
2795{
2796 wait_event(event->waitq, perf_not_pending(event));
2797}
2798
2799void perf_event_do_pending(void)
2800{
2801 __perf_pending_run();
2802}
2803
2804/*
2805 * Callchain support -- arch specific
2806 */
2807
2808__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2809{
2810 return NULL;
2811}
2812
Frederic Weisbecker5331d7b2010-03-04 21:15:56 +01002813__weak
2814void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip, int skip)
2815{
2816}
Frederic Weisbecker26d80aa2010-04-03 12:22:05 +02002817
Frederic Weisbecker5331d7b2010-03-04 21:15:56 +01002818
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002819/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08002820 * We assume there is only KVM supporting the callbacks.
2821 * Later on, we might change it to a list if there is
2822 * another virtualization implementation supporting the callbacks.
2823 */
2824struct perf_guest_info_callbacks *perf_guest_cbs;
2825
2826int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
2827{
2828 perf_guest_cbs = cbs;
2829 return 0;
2830}
2831EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
2832
2833int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
2834{
2835 perf_guest_cbs = NULL;
2836 return 0;
2837}
2838EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
2839
2840/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002841 * Output
2842 */
2843static bool perf_output_space(struct perf_mmap_data *data, unsigned long tail,
2844 unsigned long offset, unsigned long head)
2845{
2846 unsigned long mask;
2847
2848 if (!data->writable)
2849 return true;
2850
Peter Zijlstra906010b2009-09-21 16:08:49 +02002851 mask = perf_data_size(data) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002852
2853 offset = (offset - tail) & mask;
2854 head = (head - tail) & mask;
2855
2856 if ((int)(head - offset) < 0)
2857 return false;
2858
2859 return true;
2860}
2861
2862static void perf_output_wakeup(struct perf_output_handle *handle)
2863{
2864 atomic_set(&handle->data->poll, POLL_IN);
2865
2866 if (handle->nmi) {
2867 handle->event->pending_wakeup = 1;
2868 perf_pending_queue(&handle->event->pending,
2869 perf_pending_event);
2870 } else
2871 perf_event_wakeup(handle->event);
2872}
2873
2874/*
2875 * Curious locking construct.
2876 *
2877 * We need to ensure a later event_id doesn't publish a head when a former
2878 * event_id isn't done writing. However since we need to deal with NMIs we
2879 * cannot fully serialize things.
2880 *
2881 * What we do is serialize between CPUs so we only have to deal with NMI
2882 * nesting on a single CPU.
2883 *
2884 * We only publish the head (and generate a wakeup) when the outer-most
2885 * event_id completes.
2886 */
2887static void perf_output_lock(struct perf_output_handle *handle)
2888{
2889 struct perf_mmap_data *data = handle->data;
Peter Zijlstra559fdc32009-11-16 12:45:14 +01002890 int cur, cpu = get_cpu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002891
2892 handle->locked = 0;
2893
Peter Zijlstra559fdc32009-11-16 12:45:14 +01002894 for (;;) {
2895 cur = atomic_cmpxchg(&data->lock, -1, cpu);
2896 if (cur == -1) {
2897 handle->locked = 1;
2898 break;
2899 }
2900 if (cur == cpu)
2901 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002902
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002903 cpu_relax();
Peter Zijlstra559fdc32009-11-16 12:45:14 +01002904 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002905}
2906
2907static void perf_output_unlock(struct perf_output_handle *handle)
2908{
2909 struct perf_mmap_data *data = handle->data;
2910 unsigned long head;
2911 int cpu;
2912
2913 data->done_head = data->head;
2914
2915 if (!handle->locked)
2916 goto out;
2917
2918again:
2919 /*
2920 * The xchg implies a full barrier that ensures all writes are done
2921 * before we publish the new head, matched by a rmb() in userspace when
2922 * reading this position.
2923 */
2924 while ((head = atomic_long_xchg(&data->done_head, 0)))
2925 data->user_page->data_head = head;
2926
2927 /*
2928 * NMI can happen here, which means we can miss a done_head update.
2929 */
2930
2931 cpu = atomic_xchg(&data->lock, -1);
2932 WARN_ON_ONCE(cpu != smp_processor_id());
2933
2934 /*
2935 * Therefore we have to validate we did not indeed do so.
2936 */
2937 if (unlikely(atomic_long_read(&data->done_head))) {
2938 /*
2939 * Since we had it locked, we can lock it again.
2940 */
2941 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2942 cpu_relax();
2943
2944 goto again;
2945 }
2946
2947 if (atomic_xchg(&data->wakeup, 0))
2948 perf_output_wakeup(handle);
2949out:
Peter Zijlstra559fdc32009-11-16 12:45:14 +01002950 put_cpu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002951}
2952
2953void perf_output_copy(struct perf_output_handle *handle,
2954 const void *buf, unsigned int len)
2955{
2956 unsigned int pages_mask;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002957 unsigned long offset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002958 unsigned int size;
2959 void **pages;
2960
2961 offset = handle->offset;
2962 pages_mask = handle->data->nr_pages - 1;
2963 pages = handle->data->data_pages;
2964
2965 do {
Peter Zijlstra906010b2009-09-21 16:08:49 +02002966 unsigned long page_offset;
2967 unsigned long page_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002968 int nr;
2969
2970 nr = (offset >> PAGE_SHIFT) & pages_mask;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002971 page_size = 1UL << (handle->data->data_order + PAGE_SHIFT);
2972 page_offset = offset & (page_size - 1);
2973 size = min_t(unsigned int, page_size - page_offset, len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002974
2975 memcpy(pages[nr] + page_offset, buf, size);
2976
2977 len -= size;
2978 buf += size;
2979 offset += size;
2980 } while (len);
2981
2982 handle->offset = offset;
2983
2984 /*
2985 * Check we didn't copy past our reservation window, taking the
2986 * possible unsigned int wrap into account.
2987 */
2988 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
2989}
2990
2991int perf_output_begin(struct perf_output_handle *handle,
2992 struct perf_event *event, unsigned int size,
2993 int nmi, int sample)
2994{
2995 struct perf_event *output_event;
2996 struct perf_mmap_data *data;
2997 unsigned long tail, offset, head;
2998 int have_lost;
2999 struct {
3000 struct perf_event_header header;
3001 u64 id;
3002 u64 lost;
3003 } lost_event;
3004
3005 rcu_read_lock();
3006 /*
3007 * For inherited events we send all the output towards the parent.
3008 */
3009 if (event->parent)
3010 event = event->parent;
3011
3012 output_event = rcu_dereference(event->output);
3013 if (output_event)
3014 event = output_event;
3015
3016 data = rcu_dereference(event->data);
3017 if (!data)
3018 goto out;
3019
3020 handle->data = data;
3021 handle->event = event;
3022 handle->nmi = nmi;
3023 handle->sample = sample;
3024
3025 if (!data->nr_pages)
3026 goto fail;
3027
3028 have_lost = atomic_read(&data->lost);
3029 if (have_lost)
3030 size += sizeof(lost_event);
3031
3032 perf_output_lock(handle);
3033
3034 do {
3035 /*
3036 * Userspace could choose to issue a mb() before updating the
3037 * tail pointer. So that all reads will be completed before the
3038 * write is issued.
3039 */
3040 tail = ACCESS_ONCE(data->user_page->data_tail);
3041 smp_rmb();
3042 offset = head = atomic_long_read(&data->head);
3043 head += size;
3044 if (unlikely(!perf_output_space(data, tail, offset, head)))
3045 goto fail;
3046 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
3047
3048 handle->offset = offset;
3049 handle->head = head;
3050
3051 if (head - tail > data->watermark)
3052 atomic_set(&data->wakeup, 1);
3053
3054 if (have_lost) {
3055 lost_event.header.type = PERF_RECORD_LOST;
3056 lost_event.header.misc = 0;
3057 lost_event.header.size = sizeof(lost_event);
3058 lost_event.id = event->id;
3059 lost_event.lost = atomic_xchg(&data->lost, 0);
3060
3061 perf_output_put(handle, lost_event);
3062 }
3063
3064 return 0;
3065
3066fail:
3067 atomic_inc(&data->lost);
3068 perf_output_unlock(handle);
3069out:
3070 rcu_read_unlock();
3071
3072 return -ENOSPC;
3073}
3074
3075void perf_output_end(struct perf_output_handle *handle)
3076{
3077 struct perf_event *event = handle->event;
3078 struct perf_mmap_data *data = handle->data;
3079
3080 int wakeup_events = event->attr.wakeup_events;
3081
3082 if (handle->sample && wakeup_events) {
3083 int events = atomic_inc_return(&data->events);
3084 if (events >= wakeup_events) {
3085 atomic_sub(wakeup_events, &data->events);
3086 atomic_set(&data->wakeup, 1);
3087 }
3088 }
3089
3090 perf_output_unlock(handle);
3091 rcu_read_unlock();
3092}
3093
3094static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3095{
3096 /*
3097 * only top level events have the pid namespace they were created in
3098 */
3099 if (event->parent)
3100 event = event->parent;
3101
3102 return task_tgid_nr_ns(p, event->ns);
3103}
3104
3105static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3106{
3107 /*
3108 * only top level events have the pid namespace they were created in
3109 */
3110 if (event->parent)
3111 event = event->parent;
3112
3113 return task_pid_nr_ns(p, event->ns);
3114}
3115
3116static void perf_output_read_one(struct perf_output_handle *handle,
3117 struct perf_event *event)
3118{
3119 u64 read_format = event->attr.read_format;
3120 u64 values[4];
3121 int n = 0;
3122
3123 values[n++] = atomic64_read(&event->count);
3124 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3125 values[n++] = event->total_time_enabled +
3126 atomic64_read(&event->child_total_time_enabled);
3127 }
3128 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3129 values[n++] = event->total_time_running +
3130 atomic64_read(&event->child_total_time_running);
3131 }
3132 if (read_format & PERF_FORMAT_ID)
3133 values[n++] = primary_event_id(event);
3134
3135 perf_output_copy(handle, values, n * sizeof(u64));
3136}
3137
3138/*
3139 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3140 */
3141static void perf_output_read_group(struct perf_output_handle *handle,
3142 struct perf_event *event)
3143{
3144 struct perf_event *leader = event->group_leader, *sub;
3145 u64 read_format = event->attr.read_format;
3146 u64 values[5];
3147 int n = 0;
3148
3149 values[n++] = 1 + leader->nr_siblings;
3150
3151 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3152 values[n++] = leader->total_time_enabled;
3153
3154 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3155 values[n++] = leader->total_time_running;
3156
3157 if (leader != event)
3158 leader->pmu->read(leader);
3159
3160 values[n++] = atomic64_read(&leader->count);
3161 if (read_format & PERF_FORMAT_ID)
3162 values[n++] = primary_event_id(leader);
3163
3164 perf_output_copy(handle, values, n * sizeof(u64));
3165
3166 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3167 n = 0;
3168
3169 if (sub != event)
3170 sub->pmu->read(sub);
3171
3172 values[n++] = atomic64_read(&sub->count);
3173 if (read_format & PERF_FORMAT_ID)
3174 values[n++] = primary_event_id(sub);
3175
3176 perf_output_copy(handle, values, n * sizeof(u64));
3177 }
3178}
3179
3180static void perf_output_read(struct perf_output_handle *handle,
3181 struct perf_event *event)
3182{
3183 if (event->attr.read_format & PERF_FORMAT_GROUP)
3184 perf_output_read_group(handle, event);
3185 else
3186 perf_output_read_one(handle, event);
3187}
3188
3189void perf_output_sample(struct perf_output_handle *handle,
3190 struct perf_event_header *header,
3191 struct perf_sample_data *data,
3192 struct perf_event *event)
3193{
3194 u64 sample_type = data->type;
3195
3196 perf_output_put(handle, *header);
3197
3198 if (sample_type & PERF_SAMPLE_IP)
3199 perf_output_put(handle, data->ip);
3200
3201 if (sample_type & PERF_SAMPLE_TID)
3202 perf_output_put(handle, data->tid_entry);
3203
3204 if (sample_type & PERF_SAMPLE_TIME)
3205 perf_output_put(handle, data->time);
3206
3207 if (sample_type & PERF_SAMPLE_ADDR)
3208 perf_output_put(handle, data->addr);
3209
3210 if (sample_type & PERF_SAMPLE_ID)
3211 perf_output_put(handle, data->id);
3212
3213 if (sample_type & PERF_SAMPLE_STREAM_ID)
3214 perf_output_put(handle, data->stream_id);
3215
3216 if (sample_type & PERF_SAMPLE_CPU)
3217 perf_output_put(handle, data->cpu_entry);
3218
3219 if (sample_type & PERF_SAMPLE_PERIOD)
3220 perf_output_put(handle, data->period);
3221
3222 if (sample_type & PERF_SAMPLE_READ)
3223 perf_output_read(handle, event);
3224
3225 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3226 if (data->callchain) {
3227 int size = 1;
3228
3229 if (data->callchain)
3230 size += data->callchain->nr;
3231
3232 size *= sizeof(u64);
3233
3234 perf_output_copy(handle, data->callchain, size);
3235 } else {
3236 u64 nr = 0;
3237 perf_output_put(handle, nr);
3238 }
3239 }
3240
3241 if (sample_type & PERF_SAMPLE_RAW) {
3242 if (data->raw) {
3243 perf_output_put(handle, data->raw->size);
3244 perf_output_copy(handle, data->raw->data,
3245 data->raw->size);
3246 } else {
3247 struct {
3248 u32 size;
3249 u32 data;
3250 } raw = {
3251 .size = sizeof(u32),
3252 .data = 0,
3253 };
3254 perf_output_put(handle, raw);
3255 }
3256 }
3257}
3258
3259void perf_prepare_sample(struct perf_event_header *header,
3260 struct perf_sample_data *data,
3261 struct perf_event *event,
3262 struct pt_regs *regs)
3263{
3264 u64 sample_type = event->attr.sample_type;
3265
3266 data->type = sample_type;
3267
3268 header->type = PERF_RECORD_SAMPLE;
3269 header->size = sizeof(*header);
3270
3271 header->misc = 0;
3272 header->misc |= perf_misc_flags(regs);
3273
3274 if (sample_type & PERF_SAMPLE_IP) {
3275 data->ip = perf_instruction_pointer(regs);
3276
3277 header->size += sizeof(data->ip);
3278 }
3279
3280 if (sample_type & PERF_SAMPLE_TID) {
3281 /* namespace issues */
3282 data->tid_entry.pid = perf_event_pid(event, current);
3283 data->tid_entry.tid = perf_event_tid(event, current);
3284
3285 header->size += sizeof(data->tid_entry);
3286 }
3287
3288 if (sample_type & PERF_SAMPLE_TIME) {
3289 data->time = perf_clock();
3290
3291 header->size += sizeof(data->time);
3292 }
3293
3294 if (sample_type & PERF_SAMPLE_ADDR)
3295 header->size += sizeof(data->addr);
3296
3297 if (sample_type & PERF_SAMPLE_ID) {
3298 data->id = primary_event_id(event);
3299
3300 header->size += sizeof(data->id);
3301 }
3302
3303 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3304 data->stream_id = event->id;
3305
3306 header->size += sizeof(data->stream_id);
3307 }
3308
3309 if (sample_type & PERF_SAMPLE_CPU) {
3310 data->cpu_entry.cpu = raw_smp_processor_id();
3311 data->cpu_entry.reserved = 0;
3312
3313 header->size += sizeof(data->cpu_entry);
3314 }
3315
3316 if (sample_type & PERF_SAMPLE_PERIOD)
3317 header->size += sizeof(data->period);
3318
3319 if (sample_type & PERF_SAMPLE_READ)
3320 header->size += perf_event_read_size(event);
3321
3322 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3323 int size = 1;
3324
3325 data->callchain = perf_callchain(regs);
3326
3327 if (data->callchain)
3328 size += data->callchain->nr;
3329
3330 header->size += size * sizeof(u64);
3331 }
3332
3333 if (sample_type & PERF_SAMPLE_RAW) {
3334 int size = sizeof(u32);
3335
3336 if (data->raw)
3337 size += data->raw->size;
3338 else
3339 size += sizeof(u32);
3340
3341 WARN_ON_ONCE(size & (sizeof(u64)-1));
3342 header->size += size;
3343 }
3344}
3345
3346static void perf_event_output(struct perf_event *event, int nmi,
3347 struct perf_sample_data *data,
3348 struct pt_regs *regs)
3349{
3350 struct perf_output_handle handle;
3351 struct perf_event_header header;
3352
3353 perf_prepare_sample(&header, data, event, regs);
3354
3355 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3356 return;
3357
3358 perf_output_sample(&handle, &header, data, event);
3359
3360 perf_output_end(&handle);
3361}
3362
3363/*
3364 * read event_id
3365 */
3366
3367struct perf_read_event {
3368 struct perf_event_header header;
3369
3370 u32 pid;
3371 u32 tid;
3372};
3373
3374static void
3375perf_event_read_event(struct perf_event *event,
3376 struct task_struct *task)
3377{
3378 struct perf_output_handle handle;
3379 struct perf_read_event read_event = {
3380 .header = {
3381 .type = PERF_RECORD_READ,
3382 .misc = 0,
3383 .size = sizeof(read_event) + perf_event_read_size(event),
3384 },
3385 .pid = perf_event_pid(event, task),
3386 .tid = perf_event_tid(event, task),
3387 };
3388 int ret;
3389
3390 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3391 if (ret)
3392 return;
3393
3394 perf_output_put(&handle, read_event);
3395 perf_output_read(&handle, event);
3396
3397 perf_output_end(&handle);
3398}
3399
3400/*
3401 * task tracking -- fork/exit
3402 *
3403 * enabled by: attr.comm | attr.mmap | attr.task
3404 */
3405
3406struct perf_task_event {
3407 struct task_struct *task;
3408 struct perf_event_context *task_ctx;
3409
3410 struct {
3411 struct perf_event_header header;
3412
3413 u32 pid;
3414 u32 ppid;
3415 u32 tid;
3416 u32 ptid;
3417 u64 time;
3418 } event_id;
3419};
3420
3421static void perf_event_task_output(struct perf_event *event,
3422 struct perf_task_event *task_event)
3423{
3424 struct perf_output_handle handle;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003425 struct task_struct *task = task_event->task;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01003426 unsigned long flags;
3427 int size, ret;
3428
3429 /*
3430 * If this CPU attempts to acquire an rq lock held by a CPU spinning
3431 * in perf_output_lock() from interrupt context, it's game over.
3432 */
3433 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003434
3435 size = task_event->event_id.header.size;
3436 ret = perf_output_begin(&handle, event, size, 0, 0);
3437
Mike Galbraith8bb39f92010-03-26 11:11:33 +01003438 if (ret) {
3439 local_irq_restore(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003440 return;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01003441 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003442
3443 task_event->event_id.pid = perf_event_pid(event, task);
3444 task_event->event_id.ppid = perf_event_pid(event, current);
3445
3446 task_event->event_id.tid = perf_event_tid(event, task);
3447 task_event->event_id.ptid = perf_event_tid(event, current);
3448
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003449 perf_output_put(&handle, task_event->event_id);
3450
3451 perf_output_end(&handle);
Mike Galbraith8bb39f92010-03-26 11:11:33 +01003452 local_irq_restore(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003453}
3454
3455static int perf_event_task_match(struct perf_event *event)
3456{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003457 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003458 return 0;
3459
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003460 if (event->cpu != -1 && event->cpu != smp_processor_id())
3461 return 0;
3462
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003463 if (event->attr.comm || event->attr.mmap || event->attr.task)
3464 return 1;
3465
3466 return 0;
3467}
3468
3469static void perf_event_task_ctx(struct perf_event_context *ctx,
3470 struct perf_task_event *task_event)
3471{
3472 struct perf_event *event;
3473
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003474 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3475 if (perf_event_task_match(event))
3476 perf_event_task_output(event, task_event);
3477 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003478}
3479
3480static void perf_event_task_event(struct perf_task_event *task_event)
3481{
3482 struct perf_cpu_context *cpuctx;
3483 struct perf_event_context *ctx = task_event->task_ctx;
3484
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01003485 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003486 cpuctx = &get_cpu_var(perf_cpu_context);
3487 perf_event_task_ctx(&cpuctx->ctx, task_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003488 if (!ctx)
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003489 ctx = rcu_dereference(current->perf_event_ctxp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003490 if (ctx)
3491 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003492 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003493 rcu_read_unlock();
3494}
3495
3496static void perf_event_task(struct task_struct *task,
3497 struct perf_event_context *task_ctx,
3498 int new)
3499{
3500 struct perf_task_event task_event;
3501
3502 if (!atomic_read(&nr_comm_events) &&
3503 !atomic_read(&nr_mmap_events) &&
3504 !atomic_read(&nr_task_events))
3505 return;
3506
3507 task_event = (struct perf_task_event){
3508 .task = task,
3509 .task_ctx = task_ctx,
3510 .event_id = {
3511 .header = {
3512 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3513 .misc = 0,
3514 .size = sizeof(task_event.event_id),
3515 },
3516 /* .pid */
3517 /* .ppid */
3518 /* .tid */
3519 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003520 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003521 },
3522 };
3523
3524 perf_event_task_event(&task_event);
3525}
3526
3527void perf_event_fork(struct task_struct *task)
3528{
3529 perf_event_task(task, NULL, 1);
3530}
3531
3532/*
3533 * comm tracking
3534 */
3535
3536struct perf_comm_event {
3537 struct task_struct *task;
3538 char *comm;
3539 int comm_size;
3540
3541 struct {
3542 struct perf_event_header header;
3543
3544 u32 pid;
3545 u32 tid;
3546 } event_id;
3547};
3548
3549static void perf_event_comm_output(struct perf_event *event,
3550 struct perf_comm_event *comm_event)
3551{
3552 struct perf_output_handle handle;
3553 int size = comm_event->event_id.header.size;
3554 int ret = perf_output_begin(&handle, event, size, 0, 0);
3555
3556 if (ret)
3557 return;
3558
3559 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3560 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3561
3562 perf_output_put(&handle, comm_event->event_id);
3563 perf_output_copy(&handle, comm_event->comm,
3564 comm_event->comm_size);
3565 perf_output_end(&handle);
3566}
3567
3568static int perf_event_comm_match(struct perf_event *event)
3569{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003570 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003571 return 0;
3572
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003573 if (event->cpu != -1 && event->cpu != smp_processor_id())
3574 return 0;
3575
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003576 if (event->attr.comm)
3577 return 1;
3578
3579 return 0;
3580}
3581
3582static void perf_event_comm_ctx(struct perf_event_context *ctx,
3583 struct perf_comm_event *comm_event)
3584{
3585 struct perf_event *event;
3586
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003587 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3588 if (perf_event_comm_match(event))
3589 perf_event_comm_output(event, comm_event);
3590 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003591}
3592
3593static void perf_event_comm_event(struct perf_comm_event *comm_event)
3594{
3595 struct perf_cpu_context *cpuctx;
3596 struct perf_event_context *ctx;
3597 unsigned int size;
3598 char comm[TASK_COMM_LEN];
3599
3600 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01003601 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003602 size = ALIGN(strlen(comm)+1, sizeof(u64));
3603
3604 comm_event->comm = comm;
3605 comm_event->comm_size = size;
3606
3607 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3608
Peter Zijlstraf6595f32009-11-20 22:19:47 +01003609 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003610 cpuctx = &get_cpu_var(perf_cpu_context);
3611 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003612 ctx = rcu_dereference(current->perf_event_ctxp);
3613 if (ctx)
3614 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003615 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003616 rcu_read_unlock();
3617}
3618
3619void perf_event_comm(struct task_struct *task)
3620{
3621 struct perf_comm_event comm_event;
3622
3623 if (task->perf_event_ctxp)
3624 perf_event_enable_on_exec(task);
3625
3626 if (!atomic_read(&nr_comm_events))
3627 return;
3628
3629 comm_event = (struct perf_comm_event){
3630 .task = task,
3631 /* .comm */
3632 /* .comm_size */
3633 .event_id = {
3634 .header = {
3635 .type = PERF_RECORD_COMM,
3636 .misc = 0,
3637 /* .size */
3638 },
3639 /* .pid */
3640 /* .tid */
3641 },
3642 };
3643
3644 perf_event_comm_event(&comm_event);
3645}
3646
3647/*
3648 * mmap tracking
3649 */
3650
3651struct perf_mmap_event {
3652 struct vm_area_struct *vma;
3653
3654 const char *file_name;
3655 int file_size;
3656
3657 struct {
3658 struct perf_event_header header;
3659
3660 u32 pid;
3661 u32 tid;
3662 u64 start;
3663 u64 len;
3664 u64 pgoff;
3665 } event_id;
3666};
3667
3668static void perf_event_mmap_output(struct perf_event *event,
3669 struct perf_mmap_event *mmap_event)
3670{
3671 struct perf_output_handle handle;
3672 int size = mmap_event->event_id.header.size;
3673 int ret = perf_output_begin(&handle, event, size, 0, 0);
3674
3675 if (ret)
3676 return;
3677
3678 mmap_event->event_id.pid = perf_event_pid(event, current);
3679 mmap_event->event_id.tid = perf_event_tid(event, current);
3680
3681 perf_output_put(&handle, mmap_event->event_id);
3682 perf_output_copy(&handle, mmap_event->file_name,
3683 mmap_event->file_size);
3684 perf_output_end(&handle);
3685}
3686
3687static int perf_event_mmap_match(struct perf_event *event,
3688 struct perf_mmap_event *mmap_event)
3689{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003690 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003691 return 0;
3692
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003693 if (event->cpu != -1 && event->cpu != smp_processor_id())
3694 return 0;
3695
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003696 if (event->attr.mmap)
3697 return 1;
3698
3699 return 0;
3700}
3701
3702static void perf_event_mmap_ctx(struct perf_event_context *ctx,
3703 struct perf_mmap_event *mmap_event)
3704{
3705 struct perf_event *event;
3706
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003707 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3708 if (perf_event_mmap_match(event, mmap_event))
3709 perf_event_mmap_output(event, mmap_event);
3710 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003711}
3712
3713static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
3714{
3715 struct perf_cpu_context *cpuctx;
3716 struct perf_event_context *ctx;
3717 struct vm_area_struct *vma = mmap_event->vma;
3718 struct file *file = vma->vm_file;
3719 unsigned int size;
3720 char tmp[16];
3721 char *buf = NULL;
3722 const char *name;
3723
3724 memset(tmp, 0, sizeof(tmp));
3725
3726 if (file) {
3727 /*
3728 * d_path works from the end of the buffer backwards, so we
3729 * need to add enough zero bytes after the string to handle
3730 * the 64bit alignment we do later.
3731 */
3732 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
3733 if (!buf) {
3734 name = strncpy(tmp, "//enomem", sizeof(tmp));
3735 goto got_name;
3736 }
3737 name = d_path(&file->f_path, buf, PATH_MAX);
3738 if (IS_ERR(name)) {
3739 name = strncpy(tmp, "//toolong", sizeof(tmp));
3740 goto got_name;
3741 }
3742 } else {
3743 if (arch_vma_name(mmap_event->vma)) {
3744 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3745 sizeof(tmp));
3746 goto got_name;
3747 }
3748
3749 if (!vma->vm_mm) {
3750 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3751 goto got_name;
3752 }
3753
3754 name = strncpy(tmp, "//anon", sizeof(tmp));
3755 goto got_name;
3756 }
3757
3758got_name:
3759 size = ALIGN(strlen(name)+1, sizeof(u64));
3760
3761 mmap_event->file_name = name;
3762 mmap_event->file_size = size;
3763
3764 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
3765
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01003766 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003767 cpuctx = &get_cpu_var(perf_cpu_context);
3768 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003769 ctx = rcu_dereference(current->perf_event_ctxp);
3770 if (ctx)
3771 perf_event_mmap_ctx(ctx, mmap_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003772 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003773 rcu_read_unlock();
3774
3775 kfree(buf);
3776}
3777
3778void __perf_event_mmap(struct vm_area_struct *vma)
3779{
3780 struct perf_mmap_event mmap_event;
3781
3782 if (!atomic_read(&nr_mmap_events))
3783 return;
3784
3785 mmap_event = (struct perf_mmap_event){
3786 .vma = vma,
3787 /* .file_name */
3788 /* .file_size */
3789 .event_id = {
3790 .header = {
3791 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003792 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003793 /* .size */
3794 },
3795 /* .pid */
3796 /* .tid */
3797 .start = vma->vm_start,
3798 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01003799 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003800 },
3801 };
3802
3803 perf_event_mmap_event(&mmap_event);
3804}
3805
3806/*
3807 * IRQ throttle logging
3808 */
3809
3810static void perf_log_throttle(struct perf_event *event, int enable)
3811{
3812 struct perf_output_handle handle;
3813 int ret;
3814
3815 struct {
3816 struct perf_event_header header;
3817 u64 time;
3818 u64 id;
3819 u64 stream_id;
3820 } throttle_event = {
3821 .header = {
3822 .type = PERF_RECORD_THROTTLE,
3823 .misc = 0,
3824 .size = sizeof(throttle_event),
3825 },
3826 .time = perf_clock(),
3827 .id = primary_event_id(event),
3828 .stream_id = event->id,
3829 };
3830
3831 if (enable)
3832 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
3833
3834 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
3835 if (ret)
3836 return;
3837
3838 perf_output_put(&handle, throttle_event);
3839 perf_output_end(&handle);
3840}
3841
3842/*
3843 * Generic event overflow handling, sampling.
3844 */
3845
3846static int __perf_event_overflow(struct perf_event *event, int nmi,
3847 int throttle, struct perf_sample_data *data,
3848 struct pt_regs *regs)
3849{
3850 int events = atomic_read(&event->event_limit);
3851 struct hw_perf_event *hwc = &event->hw;
3852 int ret = 0;
3853
3854 throttle = (throttle && event->pmu->unthrottle != NULL);
3855
3856 if (!throttle) {
3857 hwc->interrupts++;
3858 } else {
3859 if (hwc->interrupts != MAX_INTERRUPTS) {
3860 hwc->interrupts++;
3861 if (HZ * hwc->interrupts >
3862 (u64)sysctl_perf_event_sample_rate) {
3863 hwc->interrupts = MAX_INTERRUPTS;
3864 perf_log_throttle(event, 0);
3865 ret = 1;
3866 }
3867 } else {
3868 /*
3869 * Keep re-disabling events even though on the previous
3870 * pass we disabled it - just in case we raced with a
3871 * sched-in and the event got enabled again:
3872 */
3873 ret = 1;
3874 }
3875 }
3876
3877 if (event->attr.freq) {
3878 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01003879 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003880
Peter Zijlstraabd50712010-01-26 18:50:16 +01003881 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003882
Peter Zijlstraabd50712010-01-26 18:50:16 +01003883 if (delta > 0 && delta < 2*TICK_NSEC)
3884 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003885 }
3886
3887 /*
3888 * XXX event_limit might not quite work as expected on inherited
3889 * events
3890 */
3891
3892 event->pending_kill = POLL_IN;
3893 if (events && atomic_dec_and_test(&event->event_limit)) {
3894 ret = 1;
3895 event->pending_kill = POLL_HUP;
3896 if (nmi) {
3897 event->pending_disable = 1;
3898 perf_pending_queue(&event->pending,
3899 perf_pending_event);
3900 } else
3901 perf_event_disable(event);
3902 }
3903
Peter Zijlstra453f19e2009-11-20 22:19:43 +01003904 if (event->overflow_handler)
3905 event->overflow_handler(event, nmi, data, regs);
3906 else
3907 perf_event_output(event, nmi, data, regs);
3908
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003909 return ret;
3910}
3911
3912int perf_event_overflow(struct perf_event *event, int nmi,
3913 struct perf_sample_data *data,
3914 struct pt_regs *regs)
3915{
3916 return __perf_event_overflow(event, nmi, 1, data, regs);
3917}
3918
3919/*
3920 * Generic software event infrastructure
3921 */
3922
3923/*
3924 * We directly increment event->count and keep a second value in
3925 * event->hw.period_left to count intervals. This period event
3926 * is kept in the range [-sample_period, 0] so that we can use the
3927 * sign as trigger.
3928 */
3929
3930static u64 perf_swevent_set_period(struct perf_event *event)
3931{
3932 struct hw_perf_event *hwc = &event->hw;
3933 u64 period = hwc->last_period;
3934 u64 nr, offset;
3935 s64 old, val;
3936
3937 hwc->last_period = hwc->sample_period;
3938
3939again:
3940 old = val = atomic64_read(&hwc->period_left);
3941 if (val < 0)
3942 return 0;
3943
3944 nr = div64_u64(period + val, period);
3945 offset = nr * period;
3946 val -= offset;
3947 if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
3948 goto again;
3949
3950 return nr;
3951}
3952
Peter Zijlstra0cff7842009-11-20 22:19:44 +01003953static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003954 int nmi, struct perf_sample_data *data,
3955 struct pt_regs *regs)
3956{
3957 struct hw_perf_event *hwc = &event->hw;
3958 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003959
3960 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01003961 if (!overflow)
3962 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003963
3964 if (hwc->interrupts == MAX_INTERRUPTS)
3965 return;
3966
3967 for (; overflow; overflow--) {
3968 if (__perf_event_overflow(event, nmi, throttle,
3969 data, regs)) {
3970 /*
3971 * We inhibit the overflow from happening when
3972 * hwc->interrupts == MAX_INTERRUPTS.
3973 */
3974 break;
3975 }
3976 throttle = 1;
3977 }
3978}
3979
3980static void perf_swevent_unthrottle(struct perf_event *event)
3981{
3982 /*
3983 * Nothing to do, we already reset hwc->interrupts.
3984 */
3985}
3986
3987static void perf_swevent_add(struct perf_event *event, u64 nr,
3988 int nmi, struct perf_sample_data *data,
3989 struct pt_regs *regs)
3990{
3991 struct hw_perf_event *hwc = &event->hw;
3992
3993 atomic64_add(nr, &event->count);
3994
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003995 if (!regs)
3996 return;
3997
Peter Zijlstra0cff7842009-11-20 22:19:44 +01003998 if (!hwc->sample_period)
3999 return;
4000
4001 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4002 return perf_swevent_overflow(event, 1, nmi, data, regs);
4003
4004 if (atomic64_add_negative(nr, &hwc->period_left))
4005 return;
4006
4007 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004008}
4009
Li Zefan6fb29152009-10-15 11:21:42 +08004010static int perf_tp_event_match(struct perf_event *event,
4011 struct perf_sample_data *data);
4012
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004013static int perf_exclude_event(struct perf_event *event,
4014 struct pt_regs *regs)
4015{
4016 if (regs) {
4017 if (event->attr.exclude_user && user_mode(regs))
4018 return 1;
4019
4020 if (event->attr.exclude_kernel && !user_mode(regs))
4021 return 1;
4022 }
4023
4024 return 0;
4025}
4026
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004027static int perf_swevent_match(struct perf_event *event,
4028 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004029 u32 event_id,
4030 struct perf_sample_data *data,
4031 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004032{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004033 if (event->attr.type != type)
4034 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004035
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004036 if (event->attr.config != event_id)
4037 return 0;
4038
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004039 if (perf_exclude_event(event, regs))
4040 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004041
Li Zefan6fb29152009-10-15 11:21:42 +08004042 if (event->attr.type == PERF_TYPE_TRACEPOINT &&
4043 !perf_tp_event_match(event, data))
4044 return 0;
4045
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004046 return 1;
4047}
4048
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004049static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004050{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004051 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004052
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004053 return hash_64(val, SWEVENT_HLIST_BITS);
4054}
4055
4056static struct hlist_head *
4057find_swevent_head(struct perf_cpu_context *ctx, u64 type, u32 event_id)
4058{
4059 u64 hash;
4060 struct swevent_hlist *hlist;
4061
4062 hash = swevent_hash(type, event_id);
4063
4064 hlist = rcu_dereference(ctx->swevent_hlist);
4065 if (!hlist)
4066 return NULL;
4067
4068 return &hlist->heads[hash];
4069}
4070
4071static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4072 u64 nr, int nmi,
4073 struct perf_sample_data *data,
4074 struct pt_regs *regs)
4075{
4076 struct perf_cpu_context *cpuctx;
4077 struct perf_event *event;
4078 struct hlist_node *node;
4079 struct hlist_head *head;
4080
4081 cpuctx = &__get_cpu_var(perf_cpu_context);
4082
4083 rcu_read_lock();
4084
4085 head = find_swevent_head(cpuctx, type, event_id);
4086
4087 if (!head)
4088 goto end;
4089
4090 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004091 if (perf_swevent_match(event, type, event_id, data, regs))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004092 perf_swevent_add(event, nr, nmi, data, regs);
4093 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004094end:
4095 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004096}
4097
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004098int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004099{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004100 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
4101 int rctx;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004102
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004103 if (in_nmi())
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004104 rctx = 3;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004105 else if (in_irq())
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004106 rctx = 2;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004107 else if (in_softirq())
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004108 rctx = 1;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004109 else
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004110 rctx = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004111
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004112 if (cpuctx->recursion[rctx]) {
4113 put_cpu_var(perf_cpu_context);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004114 return -1;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004115 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004116
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004117 cpuctx->recursion[rctx]++;
4118 barrier();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004119
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004120 return rctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004121}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004122EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004123
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004124void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004125{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004126 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4127 barrier();
Frederic Weisbeckerfe612672009-11-24 20:38:22 +01004128 cpuctx->recursion[rctx]--;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004129 put_cpu_var(perf_cpu_context);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004130}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004131EXPORT_SYMBOL_GPL(perf_swevent_put_recursion_context);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004132
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004133
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004134void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4135 struct pt_regs *regs, u64 addr)
4136{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004137 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004138 int rctx;
4139
4140 rctx = perf_swevent_get_recursion_context();
4141 if (rctx < 0)
4142 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004143
Peter Zijlstradc1d628a2010-03-03 15:55:04 +01004144 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004145
4146 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004147
4148 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004149}
4150
4151static void perf_swevent_read(struct perf_event *event)
4152{
4153}
4154
4155static int perf_swevent_enable(struct perf_event *event)
4156{
4157 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004158 struct perf_cpu_context *cpuctx;
4159 struct hlist_head *head;
4160
4161 cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004162
4163 if (hwc->sample_period) {
4164 hwc->last_period = hwc->sample_period;
4165 perf_swevent_set_period(event);
4166 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004167
4168 head = find_swevent_head(cpuctx, event->attr.type, event->attr.config);
4169 if (WARN_ON_ONCE(!head))
4170 return -EINVAL;
4171
4172 hlist_add_head_rcu(&event->hlist_entry, head);
4173
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004174 return 0;
4175}
4176
4177static void perf_swevent_disable(struct perf_event *event)
4178{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004179 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004180}
4181
4182static const struct pmu perf_ops_generic = {
4183 .enable = perf_swevent_enable,
4184 .disable = perf_swevent_disable,
4185 .read = perf_swevent_read,
4186 .unthrottle = perf_swevent_unthrottle,
4187};
4188
4189/*
4190 * hrtimer based swevent callback
4191 */
4192
4193static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
4194{
4195 enum hrtimer_restart ret = HRTIMER_RESTART;
4196 struct perf_sample_data data;
4197 struct pt_regs *regs;
4198 struct perf_event *event;
4199 u64 period;
4200
Peter Zijlstradc1d628a2010-03-03 15:55:04 +01004201 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004202 event->pmu->read(event);
4203
Peter Zijlstradc1d628a2010-03-03 15:55:04 +01004204 perf_sample_data_init(&data, 0);
Xiao Guangrong59d069e2009-12-01 17:30:08 +08004205 data.period = event->hw.last_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004206 regs = get_irq_regs();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004207
Frederic Weisbeckerdf8290b2010-04-09 00:28:14 +02004208 if (regs && !perf_exclude_event(event, regs)) {
Soeren Sandmann54f44072009-10-22 18:34:08 +02004209 if (!(event->attr.exclude_idle && current->pid == 0))
4210 if (perf_event_overflow(event, 0, &data, regs))
4211 ret = HRTIMER_NORESTART;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004212 }
4213
4214 period = max_t(u64, 10000, event->hw.sample_period);
4215 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4216
4217 return ret;
4218}
4219
Soeren Sandmann721a6692009-09-15 14:33:08 +02004220static void perf_swevent_start_hrtimer(struct perf_event *event)
4221{
4222 struct hw_perf_event *hwc = &event->hw;
4223
4224 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4225 hwc->hrtimer.function = perf_swevent_hrtimer;
4226 if (hwc->sample_period) {
4227 u64 period;
4228
4229 if (hwc->remaining) {
4230 if (hwc->remaining < 0)
4231 period = 10000;
4232 else
4233 period = hwc->remaining;
4234 hwc->remaining = 0;
4235 } else {
4236 period = max_t(u64, 10000, hwc->sample_period);
4237 }
4238 __hrtimer_start_range_ns(&hwc->hrtimer,
4239 ns_to_ktime(period), 0,
4240 HRTIMER_MODE_REL, 0);
4241 }
4242}
4243
4244static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4245{
4246 struct hw_perf_event *hwc = &event->hw;
4247
4248 if (hwc->sample_period) {
4249 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4250 hwc->remaining = ktime_to_ns(remaining);
4251
4252 hrtimer_cancel(&hwc->hrtimer);
4253 }
4254}
4255
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004256/*
4257 * Software event: cpu wall time clock
4258 */
4259
4260static void cpu_clock_perf_event_update(struct perf_event *event)
4261{
4262 int cpu = raw_smp_processor_id();
4263 s64 prev;
4264 u64 now;
4265
4266 now = cpu_clock(cpu);
Xiao Guangrongec89a06f2009-12-09 11:30:36 +08004267 prev = atomic64_xchg(&event->hw.prev_count, now);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004268 atomic64_add(now - prev, &event->count);
4269}
4270
4271static int cpu_clock_perf_event_enable(struct perf_event *event)
4272{
4273 struct hw_perf_event *hwc = &event->hw;
4274 int cpu = raw_smp_processor_id();
4275
4276 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Soeren Sandmann721a6692009-09-15 14:33:08 +02004277 perf_swevent_start_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004278
4279 return 0;
4280}
4281
4282static void cpu_clock_perf_event_disable(struct perf_event *event)
4283{
Soeren Sandmann721a6692009-09-15 14:33:08 +02004284 perf_swevent_cancel_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004285 cpu_clock_perf_event_update(event);
4286}
4287
4288static void cpu_clock_perf_event_read(struct perf_event *event)
4289{
4290 cpu_clock_perf_event_update(event);
4291}
4292
4293static const struct pmu perf_ops_cpu_clock = {
4294 .enable = cpu_clock_perf_event_enable,
4295 .disable = cpu_clock_perf_event_disable,
4296 .read = cpu_clock_perf_event_read,
4297};
4298
4299/*
4300 * Software event: task time clock
4301 */
4302
4303static void task_clock_perf_event_update(struct perf_event *event, u64 now)
4304{
4305 u64 prev;
4306 s64 delta;
4307
4308 prev = atomic64_xchg(&event->hw.prev_count, now);
4309 delta = now - prev;
4310 atomic64_add(delta, &event->count);
4311}
4312
4313static int task_clock_perf_event_enable(struct perf_event *event)
4314{
4315 struct hw_perf_event *hwc = &event->hw;
4316 u64 now;
4317
4318 now = event->ctx->time;
4319
4320 atomic64_set(&hwc->prev_count, now);
Soeren Sandmann721a6692009-09-15 14:33:08 +02004321
4322 perf_swevent_start_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004323
4324 return 0;
4325}
4326
4327static void task_clock_perf_event_disable(struct perf_event *event)
4328{
Soeren Sandmann721a6692009-09-15 14:33:08 +02004329 perf_swevent_cancel_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004330 task_clock_perf_event_update(event, event->ctx->time);
4331
4332}
4333
4334static void task_clock_perf_event_read(struct perf_event *event)
4335{
4336 u64 time;
4337
4338 if (!in_nmi()) {
4339 update_context_time(event->ctx);
4340 time = event->ctx->time;
4341 } else {
4342 u64 now = perf_clock();
4343 u64 delta = now - event->ctx->timestamp;
4344 time = event->ctx->time + delta;
4345 }
4346
4347 task_clock_perf_event_update(event, time);
4348}
4349
4350static const struct pmu perf_ops_task_clock = {
4351 .enable = task_clock_perf_event_enable,
4352 .disable = task_clock_perf_event_disable,
4353 .read = task_clock_perf_event_read,
4354};
4355
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004356static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4357{
4358 struct swevent_hlist *hlist;
4359
4360 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4361 kfree(hlist);
4362}
4363
4364static void swevent_hlist_release(struct perf_cpu_context *cpuctx)
4365{
4366 struct swevent_hlist *hlist;
4367
4368 if (!cpuctx->swevent_hlist)
4369 return;
4370
4371 hlist = cpuctx->swevent_hlist;
4372 rcu_assign_pointer(cpuctx->swevent_hlist, NULL);
4373 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4374}
4375
4376static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4377{
4378 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4379
4380 mutex_lock(&cpuctx->hlist_mutex);
4381
4382 if (!--cpuctx->hlist_refcount)
4383 swevent_hlist_release(cpuctx);
4384
4385 mutex_unlock(&cpuctx->hlist_mutex);
4386}
4387
4388static void swevent_hlist_put(struct perf_event *event)
4389{
4390 int cpu;
4391
4392 if (event->cpu != -1) {
4393 swevent_hlist_put_cpu(event, event->cpu);
4394 return;
4395 }
4396
4397 for_each_possible_cpu(cpu)
4398 swevent_hlist_put_cpu(event, cpu);
4399}
4400
4401static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4402{
4403 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4404 int err = 0;
4405
4406 mutex_lock(&cpuctx->hlist_mutex);
4407
4408 if (!cpuctx->swevent_hlist && cpu_online(cpu)) {
4409 struct swevent_hlist *hlist;
4410
4411 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4412 if (!hlist) {
4413 err = -ENOMEM;
4414 goto exit;
4415 }
4416 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
4417 }
4418 cpuctx->hlist_refcount++;
4419 exit:
4420 mutex_unlock(&cpuctx->hlist_mutex);
4421
4422 return err;
4423}
4424
4425static int swevent_hlist_get(struct perf_event *event)
4426{
4427 int err;
4428 int cpu, failed_cpu;
4429
4430 if (event->cpu != -1)
4431 return swevent_hlist_get_cpu(event, event->cpu);
4432
4433 get_online_cpus();
4434 for_each_possible_cpu(cpu) {
4435 err = swevent_hlist_get_cpu(event, cpu);
4436 if (err) {
4437 failed_cpu = cpu;
4438 goto fail;
4439 }
4440 }
4441 put_online_cpus();
4442
4443 return 0;
4444 fail:
4445 for_each_possible_cpu(cpu) {
4446 if (cpu == failed_cpu)
4447 break;
4448 swevent_hlist_put_cpu(event, cpu);
4449 }
4450
4451 put_online_cpus();
4452 return err;
4453}
4454
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004455#ifdef CONFIG_EVENT_TRACING
4456
4457void perf_tp_event(int event_id, u64 addr, u64 count, void *record,
4458 int entry_size, struct pt_regs *regs)
4459{
4460 struct perf_sample_data data;
4461 struct perf_raw_record raw = {
4462 .size = entry_size,
4463 .data = record,
4464 };
4465
4466 perf_sample_data_init(&data, addr);
4467 data.raw = &raw;
4468
4469 /* Trace events already protected against recursion */
4470 do_perf_sw_event(PERF_TYPE_TRACEPOINT, event_id, count, 1,
4471 &data, regs);
4472}
4473EXPORT_SYMBOL_GPL(perf_tp_event);
4474
4475static int perf_tp_event_match(struct perf_event *event,
4476 struct perf_sample_data *data)
4477{
4478 void *record = data->raw->data;
4479
4480 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4481 return 1;
4482 return 0;
4483}
4484
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004485static void tp_perf_event_destroy(struct perf_event *event)
4486{
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01004487 perf_trace_disable(event->attr.config);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004488 swevent_hlist_put(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004489}
4490
4491static const struct pmu *tp_perf_event_init(struct perf_event *event)
4492{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004493 int err;
4494
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004495 /*
4496 * Raw tracepoint data is a severe data leak, only allow root to
4497 * have these.
4498 */
4499 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4500 perf_paranoid_tracepoint_raw() &&
4501 !capable(CAP_SYS_ADMIN))
4502 return ERR_PTR(-EPERM);
4503
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01004504 if (perf_trace_enable(event->attr.config))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004505 return NULL;
4506
4507 event->destroy = tp_perf_event_destroy;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004508 err = swevent_hlist_get(event);
4509 if (err) {
4510 perf_trace_disable(event->attr.config);
4511 return ERR_PTR(err);
4512 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004513
4514 return &perf_ops_generic;
4515}
Li Zefan6fb29152009-10-15 11:21:42 +08004516
4517static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4518{
4519 char *filter_str;
4520 int ret;
4521
4522 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4523 return -EINVAL;
4524
4525 filter_str = strndup_user(arg, PAGE_SIZE);
4526 if (IS_ERR(filter_str))
4527 return PTR_ERR(filter_str);
4528
4529 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4530
4531 kfree(filter_str);
4532 return ret;
4533}
4534
4535static void perf_event_free_filter(struct perf_event *event)
4536{
4537 ftrace_profile_free_filter(event);
4538}
4539
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004540#else
Li Zefan6fb29152009-10-15 11:21:42 +08004541
4542static int perf_tp_event_match(struct perf_event *event,
4543 struct perf_sample_data *data)
4544{
4545 return 1;
4546}
4547
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004548static const struct pmu *tp_perf_event_init(struct perf_event *event)
4549{
4550 return NULL;
4551}
Li Zefan6fb29152009-10-15 11:21:42 +08004552
4553static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4554{
4555 return -ENOENT;
4556}
4557
4558static void perf_event_free_filter(struct perf_event *event)
4559{
4560}
4561
Li Zefan07b139c2009-12-21 14:27:35 +08004562#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004563
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004564#ifdef CONFIG_HAVE_HW_BREAKPOINT
4565static void bp_perf_event_destroy(struct perf_event *event)
4566{
4567 release_bp_slot(event);
4568}
4569
4570static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4571{
4572 int err;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004573
4574 err = register_perf_hw_breakpoint(bp);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004575 if (err)
4576 return ERR_PTR(err);
4577
4578 bp->destroy = bp_perf_event_destroy;
4579
4580 return &perf_ops_bp;
4581}
4582
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004583void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004584{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004585 struct perf_sample_data sample;
4586 struct pt_regs *regs = data;
4587
Peter Zijlstradc1d628a2010-03-03 15:55:04 +01004588 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004589
4590 if (!perf_exclude_event(bp, regs))
4591 perf_swevent_add(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004592}
4593#else
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004594static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4595{
4596 return NULL;
4597}
4598
4599void perf_bp_event(struct perf_event *bp, void *regs)
4600{
4601}
4602#endif
4603
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004604atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4605
4606static void sw_perf_event_destroy(struct perf_event *event)
4607{
4608 u64 event_id = event->attr.config;
4609
4610 WARN_ON(event->parent);
4611
4612 atomic_dec(&perf_swevent_enabled[event_id]);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004613 swevent_hlist_put(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004614}
4615
4616static const struct pmu *sw_perf_event_init(struct perf_event *event)
4617{
4618 const struct pmu *pmu = NULL;
4619 u64 event_id = event->attr.config;
4620
4621 /*
4622 * Software events (currently) can't in general distinguish
4623 * between user, kernel and hypervisor events.
4624 * However, context switches and cpu migrations are considered
4625 * to be kernel events, and page faults are never hypervisor
4626 * events.
4627 */
4628 switch (event_id) {
4629 case PERF_COUNT_SW_CPU_CLOCK:
4630 pmu = &perf_ops_cpu_clock;
4631
4632 break;
4633 case PERF_COUNT_SW_TASK_CLOCK:
4634 /*
4635 * If the user instantiates this as a per-cpu event,
4636 * use the cpu_clock event instead.
4637 */
4638 if (event->ctx->task)
4639 pmu = &perf_ops_task_clock;
4640 else
4641 pmu = &perf_ops_cpu_clock;
4642
4643 break;
4644 case PERF_COUNT_SW_PAGE_FAULTS:
4645 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
4646 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
4647 case PERF_COUNT_SW_CONTEXT_SWITCHES:
4648 case PERF_COUNT_SW_CPU_MIGRATIONS:
Anton Blanchardf7d79862009-10-18 01:09:29 +00004649 case PERF_COUNT_SW_ALIGNMENT_FAULTS:
4650 case PERF_COUNT_SW_EMULATION_FAULTS:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004651 if (!event->parent) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004652 int err;
4653
4654 err = swevent_hlist_get(event);
4655 if (err)
4656 return ERR_PTR(err);
4657
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004658 atomic_inc(&perf_swevent_enabled[event_id]);
4659 event->destroy = sw_perf_event_destroy;
4660 }
4661 pmu = &perf_ops_generic;
4662 break;
4663 }
4664
4665 return pmu;
4666}
4667
4668/*
4669 * Allocate and initialize a event structure
4670 */
4671static struct perf_event *
4672perf_event_alloc(struct perf_event_attr *attr,
4673 int cpu,
4674 struct perf_event_context *ctx,
4675 struct perf_event *group_leader,
4676 struct perf_event *parent_event,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004677 perf_overflow_handler_t overflow_handler,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004678 gfp_t gfpflags)
4679{
4680 const struct pmu *pmu;
4681 struct perf_event *event;
4682 struct hw_perf_event *hwc;
4683 long err;
4684
4685 event = kzalloc(sizeof(*event), gfpflags);
4686 if (!event)
4687 return ERR_PTR(-ENOMEM);
4688
4689 /*
4690 * Single events are their own group leaders, with an
4691 * empty sibling list:
4692 */
4693 if (!group_leader)
4694 group_leader = event;
4695
4696 mutex_init(&event->child_mutex);
4697 INIT_LIST_HEAD(&event->child_list);
4698
4699 INIT_LIST_HEAD(&event->group_entry);
4700 INIT_LIST_HEAD(&event->event_entry);
4701 INIT_LIST_HEAD(&event->sibling_list);
4702 init_waitqueue_head(&event->waitq);
4703
4704 mutex_init(&event->mmap_mutex);
4705
4706 event->cpu = cpu;
4707 event->attr = *attr;
4708 event->group_leader = group_leader;
4709 event->pmu = NULL;
4710 event->ctx = ctx;
4711 event->oncpu = -1;
4712
4713 event->parent = parent_event;
4714
4715 event->ns = get_pid_ns(current->nsproxy->pid_ns);
4716 event->id = atomic64_inc_return(&perf_event_id);
4717
4718 event->state = PERF_EVENT_STATE_INACTIVE;
4719
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004720 if (!overflow_handler && parent_event)
4721 overflow_handler = parent_event->overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02004722
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004723 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02004724
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004725 if (attr->disabled)
4726 event->state = PERF_EVENT_STATE_OFF;
4727
4728 pmu = NULL;
4729
4730 hwc = &event->hw;
4731 hwc->sample_period = attr->sample_period;
4732 if (attr->freq && attr->sample_freq)
4733 hwc->sample_period = 1;
4734 hwc->last_period = hwc->sample_period;
4735
4736 atomic64_set(&hwc->period_left, hwc->sample_period);
4737
4738 /*
4739 * we currently do not support PERF_FORMAT_GROUP on inherited events
4740 */
4741 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
4742 goto done;
4743
4744 switch (attr->type) {
4745 case PERF_TYPE_RAW:
4746 case PERF_TYPE_HARDWARE:
4747 case PERF_TYPE_HW_CACHE:
4748 pmu = hw_perf_event_init(event);
4749 break;
4750
4751 case PERF_TYPE_SOFTWARE:
4752 pmu = sw_perf_event_init(event);
4753 break;
4754
4755 case PERF_TYPE_TRACEPOINT:
4756 pmu = tp_perf_event_init(event);
4757 break;
4758
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004759 case PERF_TYPE_BREAKPOINT:
4760 pmu = bp_perf_event_init(event);
4761 break;
4762
4763
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004764 default:
4765 break;
4766 }
4767done:
4768 err = 0;
4769 if (!pmu)
4770 err = -EINVAL;
4771 else if (IS_ERR(pmu))
4772 err = PTR_ERR(pmu);
4773
4774 if (err) {
4775 if (event->ns)
4776 put_pid_ns(event->ns);
4777 kfree(event);
4778 return ERR_PTR(err);
4779 }
4780
4781 event->pmu = pmu;
4782
4783 if (!event->parent) {
4784 atomic_inc(&nr_events);
4785 if (event->attr.mmap)
4786 atomic_inc(&nr_mmap_events);
4787 if (event->attr.comm)
4788 atomic_inc(&nr_comm_events);
4789 if (event->attr.task)
4790 atomic_inc(&nr_task_events);
4791 }
4792
4793 return event;
4794}
4795
4796static int perf_copy_attr(struct perf_event_attr __user *uattr,
4797 struct perf_event_attr *attr)
4798{
4799 u32 size;
4800 int ret;
4801
4802 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
4803 return -EFAULT;
4804
4805 /*
4806 * zero the full structure, so that a short copy will be nice.
4807 */
4808 memset(attr, 0, sizeof(*attr));
4809
4810 ret = get_user(size, &uattr->size);
4811 if (ret)
4812 return ret;
4813
4814 if (size > PAGE_SIZE) /* silly large */
4815 goto err_size;
4816
4817 if (!size) /* abi compat */
4818 size = PERF_ATTR_SIZE_VER0;
4819
4820 if (size < PERF_ATTR_SIZE_VER0)
4821 goto err_size;
4822
4823 /*
4824 * If we're handed a bigger struct than we know of,
4825 * ensure all the unknown bits are 0 - i.e. new
4826 * user-space does not rely on any kernel feature
4827 * extensions we dont know about yet.
4828 */
4829 if (size > sizeof(*attr)) {
4830 unsigned char __user *addr;
4831 unsigned char __user *end;
4832 unsigned char val;
4833
4834 addr = (void __user *)uattr + sizeof(*attr);
4835 end = (void __user *)uattr + size;
4836
4837 for (; addr < end; addr++) {
4838 ret = get_user(val, addr);
4839 if (ret)
4840 return ret;
4841 if (val)
4842 goto err_size;
4843 }
4844 size = sizeof(*attr);
4845 }
4846
4847 ret = copy_from_user(attr, uattr, size);
4848 if (ret)
4849 return -EFAULT;
4850
4851 /*
4852 * If the type exists, the corresponding creation will verify
4853 * the attr->config.
4854 */
4855 if (attr->type >= PERF_TYPE_MAX)
4856 return -EINVAL;
4857
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05304858 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004859 return -EINVAL;
4860
4861 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
4862 return -EINVAL;
4863
4864 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
4865 return -EINVAL;
4866
4867out:
4868 return ret;
4869
4870err_size:
4871 put_user(sizeof(*attr), &uattr->size);
4872 ret = -E2BIG;
4873 goto out;
4874}
4875
Li Zefan6fb29152009-10-15 11:21:42 +08004876static int perf_event_set_output(struct perf_event *event, int output_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004877{
4878 struct perf_event *output_event = NULL;
4879 struct file *output_file = NULL;
4880 struct perf_event *old_output;
4881 int fput_needed = 0;
4882 int ret = -EINVAL;
4883
4884 if (!output_fd)
4885 goto set;
4886
4887 output_file = fget_light(output_fd, &fput_needed);
4888 if (!output_file)
4889 return -EBADF;
4890
4891 if (output_file->f_op != &perf_fops)
4892 goto out;
4893
4894 output_event = output_file->private_data;
4895
4896 /* Don't chain output fds */
4897 if (output_event->output)
4898 goto out;
4899
4900 /* Don't set an output fd when we already have an output channel */
4901 if (event->data)
4902 goto out;
4903
4904 atomic_long_inc(&output_file->f_count);
4905
4906set:
4907 mutex_lock(&event->mmap_mutex);
4908 old_output = event->output;
4909 rcu_assign_pointer(event->output, output_event);
4910 mutex_unlock(&event->mmap_mutex);
4911
4912 if (old_output) {
4913 /*
4914 * we need to make sure no existing perf_output_*()
4915 * is still referencing this event.
4916 */
4917 synchronize_rcu();
4918 fput(old_output->filp);
4919 }
4920
4921 ret = 0;
4922out:
4923 fput_light(output_file, fput_needed);
4924 return ret;
4925}
4926
4927/**
4928 * sys_perf_event_open - open a performance event, associate it to a task/cpu
4929 *
4930 * @attr_uptr: event_id type attributes for monitoring/sampling
4931 * @pid: target pid
4932 * @cpu: target cpu
4933 * @group_fd: group leader event fd
4934 */
4935SYSCALL_DEFINE5(perf_event_open,
4936 struct perf_event_attr __user *, attr_uptr,
4937 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
4938{
4939 struct perf_event *event, *group_leader;
4940 struct perf_event_attr attr;
4941 struct perf_event_context *ctx;
4942 struct file *event_file = NULL;
4943 struct file *group_file = NULL;
4944 int fput_needed = 0;
4945 int fput_needed2 = 0;
4946 int err;
4947
4948 /* for future expandability... */
4949 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
4950 return -EINVAL;
4951
4952 err = perf_copy_attr(attr_uptr, &attr);
4953 if (err)
4954 return err;
4955
4956 if (!attr.exclude_kernel) {
4957 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
4958 return -EACCES;
4959 }
4960
4961 if (attr.freq) {
4962 if (attr.sample_freq > sysctl_perf_event_sample_rate)
4963 return -EINVAL;
4964 }
4965
4966 /*
4967 * Get the target context (task or percpu):
4968 */
4969 ctx = find_get_context(pid, cpu);
4970 if (IS_ERR(ctx))
4971 return PTR_ERR(ctx);
4972
4973 /*
4974 * Look up the group leader (we will attach this event to it):
4975 */
4976 group_leader = NULL;
4977 if (group_fd != -1 && !(flags & PERF_FLAG_FD_NO_GROUP)) {
4978 err = -EINVAL;
4979 group_file = fget_light(group_fd, &fput_needed);
4980 if (!group_file)
4981 goto err_put_context;
4982 if (group_file->f_op != &perf_fops)
4983 goto err_put_context;
4984
4985 group_leader = group_file->private_data;
4986 /*
4987 * Do not allow a recursive hierarchy (this new sibling
4988 * becoming part of another group-sibling):
4989 */
4990 if (group_leader->group_leader != group_leader)
4991 goto err_put_context;
4992 /*
4993 * Do not allow to attach to a group in a different
4994 * task or CPU context:
4995 */
4996 if (group_leader->ctx != ctx)
4997 goto err_put_context;
4998 /*
4999 * Only a group leader can be exclusive or pinned
5000 */
5001 if (attr.exclusive || attr.pinned)
5002 goto err_put_context;
5003 }
5004
5005 event = perf_event_alloc(&attr, cpu, ctx, group_leader,
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005006 NULL, NULL, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005007 err = PTR_ERR(event);
5008 if (IS_ERR(event))
5009 goto err_put_context;
5010
Roland Dreier628ff7c2009-12-18 09:41:24 -08005011 err = anon_inode_getfd("[perf_event]", &perf_fops, event, O_RDWR);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005012 if (err < 0)
5013 goto err_free_put_context;
5014
5015 event_file = fget_light(err, &fput_needed2);
5016 if (!event_file)
5017 goto err_free_put_context;
5018
5019 if (flags & PERF_FLAG_FD_OUTPUT) {
5020 err = perf_event_set_output(event, group_fd);
5021 if (err)
5022 goto err_fput_free_put_context;
5023 }
5024
5025 event->filp = event_file;
5026 WARN_ON_ONCE(ctx->parent_ctx);
5027 mutex_lock(&ctx->mutex);
5028 perf_install_in_context(ctx, event, cpu);
5029 ++ctx->generation;
5030 mutex_unlock(&ctx->mutex);
5031
5032 event->owner = current;
5033 get_task_struct(current);
5034 mutex_lock(&current->perf_event_mutex);
5035 list_add_tail(&event->owner_entry, &current->perf_event_list);
5036 mutex_unlock(&current->perf_event_mutex);
5037
5038err_fput_free_put_context:
5039 fput_light(event_file, fput_needed2);
5040
5041err_free_put_context:
5042 if (err < 0)
Tejun Heo048c8522010-05-01 10:11:35 +02005043 free_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005044
5045err_put_context:
5046 if (err < 0)
5047 put_ctx(ctx);
5048
5049 fput_light(group_file, fput_needed);
5050
5051 return err;
5052}
5053
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005054/**
5055 * perf_event_create_kernel_counter
5056 *
5057 * @attr: attributes of the counter to create
5058 * @cpu: cpu in which the counter is bound
5059 * @pid: task to profile
5060 */
5061struct perf_event *
5062perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005063 pid_t pid,
5064 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005065{
5066 struct perf_event *event;
5067 struct perf_event_context *ctx;
5068 int err;
5069
5070 /*
5071 * Get the target context (task or percpu):
5072 */
5073
5074 ctx = find_get_context(pid, cpu);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005075 if (IS_ERR(ctx)) {
5076 err = PTR_ERR(ctx);
5077 goto err_exit;
5078 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005079
5080 event = perf_event_alloc(attr, cpu, ctx, NULL,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005081 NULL, overflow_handler, GFP_KERNEL);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005082 if (IS_ERR(event)) {
5083 err = PTR_ERR(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005084 goto err_put_context;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005085 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005086
5087 event->filp = NULL;
5088 WARN_ON_ONCE(ctx->parent_ctx);
5089 mutex_lock(&ctx->mutex);
5090 perf_install_in_context(ctx, event, cpu);
5091 ++ctx->generation;
5092 mutex_unlock(&ctx->mutex);
5093
5094 event->owner = current;
5095 get_task_struct(current);
5096 mutex_lock(&current->perf_event_mutex);
5097 list_add_tail(&event->owner_entry, &current->perf_event_list);
5098 mutex_unlock(&current->perf_event_mutex);
5099
5100 return event;
5101
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005102 err_put_context:
5103 put_ctx(ctx);
5104 err_exit:
5105 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005106}
5107EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5108
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005109/*
5110 * inherit a event from parent task to child task:
5111 */
5112static struct perf_event *
5113inherit_event(struct perf_event *parent_event,
5114 struct task_struct *parent,
5115 struct perf_event_context *parent_ctx,
5116 struct task_struct *child,
5117 struct perf_event *group_leader,
5118 struct perf_event_context *child_ctx)
5119{
5120 struct perf_event *child_event;
5121
5122 /*
5123 * Instead of creating recursive hierarchies of events,
5124 * we link inherited events back to the original parent,
5125 * which has a filp for sure, which we use as the reference
5126 * count:
5127 */
5128 if (parent_event->parent)
5129 parent_event = parent_event->parent;
5130
5131 child_event = perf_event_alloc(&parent_event->attr,
5132 parent_event->cpu, child_ctx,
5133 group_leader, parent_event,
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005134 NULL, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005135 if (IS_ERR(child_event))
5136 return child_event;
5137 get_ctx(child_ctx);
5138
5139 /*
5140 * Make the child state follow the state of the parent event,
5141 * not its attr.disabled bit. We hold the parent's mutex,
5142 * so we won't race with perf_event_{en, dis}able_family.
5143 */
5144 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
5145 child_event->state = PERF_EVENT_STATE_INACTIVE;
5146 else
5147 child_event->state = PERF_EVENT_STATE_OFF;
5148
Peter Zijlstra75c9f322010-01-29 09:04:26 +01005149 if (parent_event->attr.freq) {
5150 u64 sample_period = parent_event->hw.sample_period;
5151 struct hw_perf_event *hwc = &child_event->hw;
5152
5153 hwc->sample_period = sample_period;
5154 hwc->last_period = sample_period;
5155
5156 atomic64_set(&hwc->period_left, sample_period);
5157 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005158
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005159 child_event->overflow_handler = parent_event->overflow_handler;
5160
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005161 /*
5162 * Link it up in the child's context:
5163 */
5164 add_event_to_ctx(child_event, child_ctx);
5165
5166 /*
5167 * Get a reference to the parent filp - we will fput it
5168 * when the child event exits. This is safe to do because
5169 * we are in the parent and we know that the filp still
5170 * exists and has a nonzero count:
5171 */
5172 atomic_long_inc(&parent_event->filp->f_count);
5173
5174 /*
5175 * Link this into the parent event's child list
5176 */
5177 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5178 mutex_lock(&parent_event->child_mutex);
5179 list_add_tail(&child_event->child_list, &parent_event->child_list);
5180 mutex_unlock(&parent_event->child_mutex);
5181
5182 return child_event;
5183}
5184
5185static int inherit_group(struct perf_event *parent_event,
5186 struct task_struct *parent,
5187 struct perf_event_context *parent_ctx,
5188 struct task_struct *child,
5189 struct perf_event_context *child_ctx)
5190{
5191 struct perf_event *leader;
5192 struct perf_event *sub;
5193 struct perf_event *child_ctr;
5194
5195 leader = inherit_event(parent_event, parent, parent_ctx,
5196 child, NULL, child_ctx);
5197 if (IS_ERR(leader))
5198 return PTR_ERR(leader);
5199 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
5200 child_ctr = inherit_event(sub, parent, parent_ctx,
5201 child, leader, child_ctx);
5202 if (IS_ERR(child_ctr))
5203 return PTR_ERR(child_ctr);
5204 }
5205 return 0;
5206}
5207
5208static void sync_child_event(struct perf_event *child_event,
5209 struct task_struct *child)
5210{
5211 struct perf_event *parent_event = child_event->parent;
5212 u64 child_val;
5213
5214 if (child_event->attr.inherit_stat)
5215 perf_event_read_event(child_event, child);
5216
5217 child_val = atomic64_read(&child_event->count);
5218
5219 /*
5220 * Add back the child's count to the parent's count:
5221 */
5222 atomic64_add(child_val, &parent_event->count);
5223 atomic64_add(child_event->total_time_enabled,
5224 &parent_event->child_total_time_enabled);
5225 atomic64_add(child_event->total_time_running,
5226 &parent_event->child_total_time_running);
5227
5228 /*
5229 * Remove this event from the parent's list
5230 */
5231 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5232 mutex_lock(&parent_event->child_mutex);
5233 list_del_init(&child_event->child_list);
5234 mutex_unlock(&parent_event->child_mutex);
5235
5236 /*
5237 * Release the parent event, if this was the last
5238 * reference to it.
5239 */
5240 fput(parent_event->filp);
5241}
5242
5243static void
5244__perf_event_exit_task(struct perf_event *child_event,
5245 struct perf_event_context *child_ctx,
5246 struct task_struct *child)
5247{
5248 struct perf_event *parent_event;
5249
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005250 perf_event_remove_from_context(child_event);
5251
5252 parent_event = child_event->parent;
5253 /*
5254 * It can happen that parent exits first, and has events
5255 * that are still around due to the child reference. These
5256 * events need to be zapped - but otherwise linger.
5257 */
5258 if (parent_event) {
5259 sync_child_event(child_event, child);
5260 free_event(child_event);
5261 }
5262}
5263
5264/*
5265 * When a child task exits, feed back event values to parent events.
5266 */
5267void perf_event_exit_task(struct task_struct *child)
5268{
5269 struct perf_event *child_event, *tmp;
5270 struct perf_event_context *child_ctx;
5271 unsigned long flags;
5272
5273 if (likely(!child->perf_event_ctxp)) {
5274 perf_event_task(child, NULL, 0);
5275 return;
5276 }
5277
5278 local_irq_save(flags);
5279 /*
5280 * We can't reschedule here because interrupts are disabled,
5281 * and either child is current or it is a task that can't be
5282 * scheduled, so we are now safe from rescheduling changing
5283 * our context.
5284 */
5285 child_ctx = child->perf_event_ctxp;
5286 __perf_event_task_sched_out(child_ctx);
5287
5288 /*
5289 * Take the context lock here so that if find_get_context is
5290 * reading child->perf_event_ctxp, we wait until it has
5291 * incremented the context's refcount before we do put_ctx below.
5292 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01005293 raw_spin_lock(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005294 child->perf_event_ctxp = NULL;
5295 /*
5296 * If this context is a clone; unclone it so it can't get
5297 * swapped to another process while we're removing all
5298 * the events from it.
5299 */
5300 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01005301 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01005302 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005303
5304 /*
5305 * Report the task dead after unscheduling the events so that we
5306 * won't get any samples after PERF_RECORD_EXIT. We can however still
5307 * get a few PERF_RECORD_READ events.
5308 */
5309 perf_event_task(child, child_ctx, 0);
5310
5311 /*
5312 * We can recurse on the same lock type through:
5313 *
5314 * __perf_event_exit_task()
5315 * sync_child_event()
5316 * fput(parent_event->filp)
5317 * perf_release()
5318 * mutex_lock(&ctx->mutex)
5319 *
5320 * But since its the parent context it won't be the same instance.
5321 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02005322 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005323
5324again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005325 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5326 group_entry)
5327 __perf_event_exit_task(child_event, child_ctx, child);
5328
5329 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005330 group_entry)
5331 __perf_event_exit_task(child_event, child_ctx, child);
5332
5333 /*
5334 * If the last event was a group event, it will have appended all
5335 * its siblings to the list, but we obtained 'tmp' before that which
5336 * will still point to the list head terminating the iteration.
5337 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005338 if (!list_empty(&child_ctx->pinned_groups) ||
5339 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005340 goto again;
5341
5342 mutex_unlock(&child_ctx->mutex);
5343
5344 put_ctx(child_ctx);
5345}
5346
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005347static void perf_free_event(struct perf_event *event,
5348 struct perf_event_context *ctx)
5349{
5350 struct perf_event *parent = event->parent;
5351
5352 if (WARN_ON_ONCE(!parent))
5353 return;
5354
5355 mutex_lock(&parent->child_mutex);
5356 list_del_init(&event->child_list);
5357 mutex_unlock(&parent->child_mutex);
5358
5359 fput(parent->filp);
5360
5361 list_del_event(event, ctx);
5362 free_event(event);
5363}
5364
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005365/*
5366 * free an unexposed, unused context as created by inheritance by
5367 * init_task below, used by fork() in case of fail.
5368 */
5369void perf_event_free_task(struct task_struct *task)
5370{
5371 struct perf_event_context *ctx = task->perf_event_ctxp;
5372 struct perf_event *event, *tmp;
5373
5374 if (!ctx)
5375 return;
5376
5377 mutex_lock(&ctx->mutex);
5378again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005379 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5380 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005381
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005382 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5383 group_entry)
5384 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005385
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005386 if (!list_empty(&ctx->pinned_groups) ||
5387 !list_empty(&ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005388 goto again;
5389
5390 mutex_unlock(&ctx->mutex);
5391
5392 put_ctx(ctx);
5393}
5394
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005395static int
5396inherit_task_group(struct perf_event *event, struct task_struct *parent,
5397 struct perf_event_context *parent_ctx,
5398 struct task_struct *child,
5399 int *inherited_all)
5400{
5401 int ret;
5402 struct perf_event_context *child_ctx = child->perf_event_ctxp;
5403
5404 if (!event->attr.inherit) {
5405 *inherited_all = 0;
5406 return 0;
5407 }
5408
5409 if (!child_ctx) {
5410 /*
5411 * This is executed from the parent task context, so
5412 * inherit events that have been marked for cloning.
5413 * First allocate and initialize a context for the
5414 * child.
5415 */
5416
5417 child_ctx = kzalloc(sizeof(struct perf_event_context),
5418 GFP_KERNEL);
5419 if (!child_ctx)
5420 return -ENOMEM;
5421
5422 __perf_event_init_context(child_ctx, child);
5423 child->perf_event_ctxp = child_ctx;
5424 get_task_struct(child);
5425 }
5426
5427 ret = inherit_group(event, parent, parent_ctx,
5428 child, child_ctx);
5429
5430 if (ret)
5431 *inherited_all = 0;
5432
5433 return ret;
5434}
5435
5436
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005437/*
5438 * Initialize the perf_event context in task_struct
5439 */
5440int perf_event_init_task(struct task_struct *child)
5441{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005442 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005443 struct perf_event_context *cloned_ctx;
5444 struct perf_event *event;
5445 struct task_struct *parent = current;
5446 int inherited_all = 1;
5447 int ret = 0;
5448
5449 child->perf_event_ctxp = NULL;
5450
5451 mutex_init(&child->perf_event_mutex);
5452 INIT_LIST_HEAD(&child->perf_event_list);
5453
5454 if (likely(!parent->perf_event_ctxp))
5455 return 0;
5456
5457 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005458 * If the parent's context is a clone, pin it so it won't get
5459 * swapped under us.
5460 */
5461 parent_ctx = perf_pin_task_context(parent);
5462
5463 /*
5464 * No need to check if parent_ctx != NULL here; since we saw
5465 * it non-NULL earlier, the only reason for it to become NULL
5466 * is if we exit, and since we're currently in the middle of
5467 * a fork we can't be exiting at the same time.
5468 */
5469
5470 /*
5471 * Lock the parent list. No need to lock the child - not PID
5472 * hashed yet and not running, so nobody can access it.
5473 */
5474 mutex_lock(&parent_ctx->mutex);
5475
5476 /*
5477 * We dont have to disable NMIs - we are only looking at
5478 * the list, not manipulating it:
5479 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005480 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
5481 ret = inherit_task_group(event, parent, parent_ctx, child,
5482 &inherited_all);
5483 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005484 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005485 }
5486
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005487 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
5488 ret = inherit_task_group(event, parent, parent_ctx, child,
5489 &inherited_all);
5490 if (ret)
5491 break;
5492 }
5493
5494 child_ctx = child->perf_event_ctxp;
5495
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01005496 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005497 /*
5498 * Mark the child context as a clone of the parent
5499 * context, or of whatever the parent is a clone of.
5500 * Note that if the parent is a clone, it could get
5501 * uncloned at any point, but that doesn't matter
5502 * because the list of events and the generation
5503 * count can't have changed since we took the mutex.
5504 */
5505 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
5506 if (cloned_ctx) {
5507 child_ctx->parent_ctx = cloned_ctx;
5508 child_ctx->parent_gen = parent_ctx->parent_gen;
5509 } else {
5510 child_ctx->parent_ctx = parent_ctx;
5511 child_ctx->parent_gen = parent_ctx->generation;
5512 }
5513 get_ctx(child_ctx->parent_ctx);
5514 }
5515
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005516 mutex_unlock(&parent_ctx->mutex);
5517
5518 perf_unpin_context(parent_ctx);
5519
5520 return ret;
5521}
5522
Paul Mackerras220b1402010-03-10 20:45:52 +11005523static void __init perf_event_init_all_cpus(void)
5524{
5525 int cpu;
5526 struct perf_cpu_context *cpuctx;
5527
5528 for_each_possible_cpu(cpu) {
5529 cpuctx = &per_cpu(perf_cpu_context, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005530 mutex_init(&cpuctx->hlist_mutex);
Paul Mackerras220b1402010-03-10 20:45:52 +11005531 __perf_event_init_context(&cpuctx->ctx, NULL);
5532 }
5533}
5534
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005535static void __cpuinit perf_event_init_cpu(int cpu)
5536{
5537 struct perf_cpu_context *cpuctx;
5538
5539 cpuctx = &per_cpu(perf_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005540
5541 spin_lock(&perf_resource_lock);
5542 cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
5543 spin_unlock(&perf_resource_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005544
5545 mutex_lock(&cpuctx->hlist_mutex);
5546 if (cpuctx->hlist_refcount > 0) {
5547 struct swevent_hlist *hlist;
5548
5549 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5550 WARN_ON_ONCE(!hlist);
5551 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
5552 }
5553 mutex_unlock(&cpuctx->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005554}
5555
5556#ifdef CONFIG_HOTPLUG_CPU
5557static void __perf_event_exit_cpu(void *info)
5558{
5559 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
5560 struct perf_event_context *ctx = &cpuctx->ctx;
5561 struct perf_event *event, *tmp;
5562
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005563 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5564 __perf_event_remove_from_context(event);
5565 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005566 __perf_event_remove_from_context(event);
5567}
5568static void perf_event_exit_cpu(int cpu)
5569{
5570 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
5571 struct perf_event_context *ctx = &cpuctx->ctx;
5572
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005573 mutex_lock(&cpuctx->hlist_mutex);
5574 swevent_hlist_release(cpuctx);
5575 mutex_unlock(&cpuctx->hlist_mutex);
5576
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005577 mutex_lock(&ctx->mutex);
5578 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
5579 mutex_unlock(&ctx->mutex);
5580}
5581#else
5582static inline void perf_event_exit_cpu(int cpu) { }
5583#endif
5584
5585static int __cpuinit
5586perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
5587{
5588 unsigned int cpu = (long)hcpu;
5589
5590 switch (action) {
5591
5592 case CPU_UP_PREPARE:
5593 case CPU_UP_PREPARE_FROZEN:
5594 perf_event_init_cpu(cpu);
5595 break;
5596
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005597 case CPU_DOWN_PREPARE:
5598 case CPU_DOWN_PREPARE_FROZEN:
5599 perf_event_exit_cpu(cpu);
5600 break;
5601
5602 default:
5603 break;
5604 }
5605
5606 return NOTIFY_OK;
5607}
5608
5609/*
5610 * This has to have a higher priority than migration_notifier in sched.c.
5611 */
5612static struct notifier_block __cpuinitdata perf_cpu_nb = {
5613 .notifier_call = perf_cpu_notify,
5614 .priority = 20,
5615};
5616
5617void __init perf_event_init(void)
5618{
Paul Mackerras220b1402010-03-10 20:45:52 +11005619 perf_event_init_all_cpus();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005620 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
5621 (void *)(long)smp_processor_id());
5622 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
5623 (void *)(long)smp_processor_id());
5624 register_cpu_notifier(&perf_cpu_nb);
5625}
5626
Andi Kleenc9be0a32010-01-05 12:47:58 +01005627static ssize_t perf_show_reserve_percpu(struct sysdev_class *class,
5628 struct sysdev_class_attribute *attr,
5629 char *buf)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005630{
5631 return sprintf(buf, "%d\n", perf_reserved_percpu);
5632}
5633
5634static ssize_t
5635perf_set_reserve_percpu(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +01005636 struct sysdev_class_attribute *attr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005637 const char *buf,
5638 size_t count)
5639{
5640 struct perf_cpu_context *cpuctx;
5641 unsigned long val;
5642 int err, cpu, mpt;
5643
5644 err = strict_strtoul(buf, 10, &val);
5645 if (err)
5646 return err;
5647 if (val > perf_max_events)
5648 return -EINVAL;
5649
5650 spin_lock(&perf_resource_lock);
5651 perf_reserved_percpu = val;
5652 for_each_online_cpu(cpu) {
5653 cpuctx = &per_cpu(perf_cpu_context, cpu);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01005654 raw_spin_lock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005655 mpt = min(perf_max_events - cpuctx->ctx.nr_events,
5656 perf_max_events - perf_reserved_percpu);
5657 cpuctx->max_pertask = mpt;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01005658 raw_spin_unlock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005659 }
5660 spin_unlock(&perf_resource_lock);
5661
5662 return count;
5663}
5664
Andi Kleenc9be0a32010-01-05 12:47:58 +01005665static ssize_t perf_show_overcommit(struct sysdev_class *class,
5666 struct sysdev_class_attribute *attr,
5667 char *buf)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005668{
5669 return sprintf(buf, "%d\n", perf_overcommit);
5670}
5671
5672static ssize_t
Andi Kleenc9be0a32010-01-05 12:47:58 +01005673perf_set_overcommit(struct sysdev_class *class,
5674 struct sysdev_class_attribute *attr,
5675 const char *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005676{
5677 unsigned long val;
5678 int err;
5679
5680 err = strict_strtoul(buf, 10, &val);
5681 if (err)
5682 return err;
5683 if (val > 1)
5684 return -EINVAL;
5685
5686 spin_lock(&perf_resource_lock);
5687 perf_overcommit = val;
5688 spin_unlock(&perf_resource_lock);
5689
5690 return count;
5691}
5692
5693static SYSDEV_CLASS_ATTR(
5694 reserve_percpu,
5695 0644,
5696 perf_show_reserve_percpu,
5697 perf_set_reserve_percpu
5698 );
5699
5700static SYSDEV_CLASS_ATTR(
5701 overcommit,
5702 0644,
5703 perf_show_overcommit,
5704 perf_set_overcommit
5705 );
5706
5707static struct attribute *perfclass_attrs[] = {
5708 &attr_reserve_percpu.attr,
5709 &attr_overcommit.attr,
5710 NULL
5711};
5712
5713static struct attribute_group perfclass_attr_group = {
5714 .attrs = perfclass_attrs,
5715 .name = "perf_events",
5716};
5717
5718static int __init perf_event_sysfs_init(void)
5719{
5720 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
5721 &perfclass_attr_group);
5722}
5723device_initcall(perf_event_sysfs_init);