2 * Performance events core code:
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>
9 * For licensing details see kernel-base/COPYING
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/file.h>
17 #include <linux/poll.h>
18 #include <linux/sysfs.h>
19 #include <linux/dcache.h>
20 #include <linux/percpu.h>
21 #include <linux/ptrace.h>
22 #include <linux/vmstat.h>
23 #include <linux/vmalloc.h>
24 #include <linux/hardirq.h>
25 #include <linux/rculist.h>
26 #include <linux/uaccess.h>
27 #include <linux/syscalls.h>
28 #include <linux/anon_inodes.h>
29 #include <linux/kernel_stat.h>
30 #include <linux/perf_event.h>
31 #include <linux/ftrace_event.h>
32 #include <linux/hw_breakpoint.h>
34 #include <asm/irq_regs.h>
37 * Each CPU has a list of per CPU events:
39 static DEFINE_PER_CPU(struct perf_cpu_context
, perf_cpu_context
);
41 int perf_max_events __read_mostly
= 1;
42 static int perf_reserved_percpu __read_mostly
;
43 static int perf_overcommit __read_mostly
= 1;
45 static atomic_t nr_events __read_mostly
;
46 static atomic_t nr_mmap_events __read_mostly
;
47 static atomic_t nr_comm_events __read_mostly
;
48 static atomic_t nr_task_events __read_mostly
;
51 * perf event paranoia level:
52 * -1 - not paranoid at all
53 * 0 - disallow raw tracepoint access for unpriv
54 * 1 - disallow cpu events for unpriv
55 * 2 - disallow kernel profiling for unpriv
57 int sysctl_perf_event_paranoid __read_mostly
= 1;
59 static inline bool perf_paranoid_tracepoint_raw(void)
61 return sysctl_perf_event_paranoid
> -1;
64 static inline bool perf_paranoid_cpu(void)
66 return sysctl_perf_event_paranoid
> 0;
69 static inline bool perf_paranoid_kernel(void)
71 return sysctl_perf_event_paranoid
> 1;
74 /* Minimum for 128 pages + 1 for the user control page */
75 int sysctl_perf_event_mlock __read_mostly
= 516; /* 'free' kb per user */
78 * max perf event sample rate
80 int sysctl_perf_event_sample_rate __read_mostly
= 100000;
82 static atomic64_t perf_event_id
;
85 * Lock for (sysadmin-configurable) event reservations:
87 static DEFINE_SPINLOCK(perf_resource_lock
);
90 * Architecture provided APIs - weak aliases:
92 extern __weak
const struct pmu
*hw_perf_event_init(struct perf_event
*event
)
97 void __weak
hw_perf_disable(void) { barrier(); }
98 void __weak
hw_perf_enable(void) { barrier(); }
100 void __weak
hw_perf_event_setup(int cpu
) { barrier(); }
101 void __weak
hw_perf_event_setup_online(int cpu
) { barrier(); }
104 hw_perf_group_sched_in(struct perf_event
*group_leader
,
105 struct perf_cpu_context
*cpuctx
,
106 struct perf_event_context
*ctx
, int cpu
)
111 void __weak
perf_event_print_debug(void) { }
113 static DEFINE_PER_CPU(int, perf_disable_count
);
115 void __perf_disable(void)
117 __get_cpu_var(perf_disable_count
)++;
120 bool __perf_enable(void)
122 return !--__get_cpu_var(perf_disable_count
);
125 void perf_disable(void)
131 void perf_enable(void)
137 static void get_ctx(struct perf_event_context
*ctx
)
139 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
142 static void free_ctx(struct rcu_head
*head
)
144 struct perf_event_context
*ctx
;
146 ctx
= container_of(head
, struct perf_event_context
, rcu_head
);
150 static void put_ctx(struct perf_event_context
*ctx
)
152 if (atomic_dec_and_test(&ctx
->refcount
)) {
154 put_ctx(ctx
->parent_ctx
);
156 put_task_struct(ctx
->task
);
157 call_rcu(&ctx
->rcu_head
, free_ctx
);
161 static void unclone_ctx(struct perf_event_context
*ctx
)
163 if (ctx
->parent_ctx
) {
164 put_ctx(ctx
->parent_ctx
);
165 ctx
->parent_ctx
= NULL
;
170 * If we inherit events we want to return the parent event id
173 static u64
primary_event_id(struct perf_event
*event
)
178 id
= event
->parent
->id
;
184 * Get the perf_event_context for a task and lock it.
185 * This has to cope with with the fact that until it is locked,
186 * the context could get moved to another task.
188 static struct perf_event_context
*
189 perf_lock_task_context(struct task_struct
*task
, unsigned long *flags
)
191 struct perf_event_context
*ctx
;
195 ctx
= rcu_dereference(task
->perf_event_ctxp
);
198 * If this context is a clone of another, it might
199 * get swapped for another underneath us by
200 * perf_event_task_sched_out, though the
201 * rcu_read_lock() protects us from any context
202 * getting freed. Lock the context and check if it
203 * got swapped before we could get the lock, and retry
204 * if so. If we locked the right context, then it
205 * can't get swapped on us any more.
207 raw_spin_lock_irqsave(&ctx
->lock
, *flags
);
208 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
)) {
209 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
213 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
214 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
223 * Get the context for a task and increment its pin_count so it
224 * can't get swapped to another task. This also increments its
225 * reference count so that the context can't get freed.
227 static struct perf_event_context
*perf_pin_task_context(struct task_struct
*task
)
229 struct perf_event_context
*ctx
;
232 ctx
= perf_lock_task_context(task
, &flags
);
235 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
240 static void perf_unpin_context(struct perf_event_context
*ctx
)
244 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
246 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
250 static inline u64
perf_clock(void)
252 return cpu_clock(raw_smp_processor_id());
256 * Update the record of the current time in a context.
258 static void update_context_time(struct perf_event_context
*ctx
)
260 u64 now
= perf_clock();
262 ctx
->time
+= now
- ctx
->timestamp
;
263 ctx
->timestamp
= now
;
267 * Update the total_time_enabled and total_time_running fields for a event.
269 static void update_event_times(struct perf_event
*event
)
271 struct perf_event_context
*ctx
= event
->ctx
;
274 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
275 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
281 run_end
= event
->tstamp_stopped
;
283 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
285 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
286 run_end
= event
->tstamp_stopped
;
290 event
->total_time_running
= run_end
- event
->tstamp_running
;
294 * Add a event from the lists for its context.
295 * Must be called with ctx->mutex and ctx->lock held.
298 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
300 struct perf_event
*group_leader
= event
->group_leader
;
303 * Depending on whether it is a standalone or sibling event,
304 * add it straight to the context's event list, or to the group
305 * leader's sibling list:
307 if (group_leader
== event
)
308 list_add_tail(&event
->group_entry
, &ctx
->group_list
);
310 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
311 group_leader
->nr_siblings
++;
314 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
316 if (event
->attr
.inherit_stat
)
321 * Remove a event from the lists for its context.
322 * Must be called with ctx->mutex and ctx->lock held.
325 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
327 struct perf_event
*sibling
, *tmp
;
329 if (list_empty(&event
->group_entry
))
332 if (event
->attr
.inherit_stat
)
335 list_del_init(&event
->group_entry
);
336 list_del_rcu(&event
->event_entry
);
338 if (event
->group_leader
!= event
)
339 event
->group_leader
->nr_siblings
--;
341 update_event_times(event
);
344 * If event was in error state, then keep it
345 * that way, otherwise bogus counts will be
346 * returned on read(). The only way to get out
347 * of error state is by explicit re-enabling
350 if (event
->state
> PERF_EVENT_STATE_OFF
)
351 event
->state
= PERF_EVENT_STATE_OFF
;
354 * If this was a group event with sibling events then
355 * upgrade the siblings to singleton events by adding them
356 * to the context list directly:
358 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
360 list_move_tail(&sibling
->group_entry
, &ctx
->group_list
);
361 sibling
->group_leader
= sibling
;
366 event_sched_out(struct perf_event
*event
,
367 struct perf_cpu_context
*cpuctx
,
368 struct perf_event_context
*ctx
)
370 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
373 event
->state
= PERF_EVENT_STATE_INACTIVE
;
374 if (event
->pending_disable
) {
375 event
->pending_disable
= 0;
376 event
->state
= PERF_EVENT_STATE_OFF
;
378 event
->tstamp_stopped
= ctx
->time
;
379 event
->pmu
->disable(event
);
382 if (!is_software_event(event
))
383 cpuctx
->active_oncpu
--;
385 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
386 cpuctx
->exclusive
= 0;
390 group_sched_out(struct perf_event
*group_event
,
391 struct perf_cpu_context
*cpuctx
,
392 struct perf_event_context
*ctx
)
394 struct perf_event
*event
;
396 if (group_event
->state
!= PERF_EVENT_STATE_ACTIVE
)
399 event_sched_out(group_event
, cpuctx
, ctx
);
402 * Schedule out siblings (if any):
404 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
405 event_sched_out(event
, cpuctx
, ctx
);
407 if (group_event
->attr
.exclusive
)
408 cpuctx
->exclusive
= 0;
412 * Cross CPU call to remove a performance event
414 * We disable the event on the hardware level first. After that we
415 * remove it from the context list.
417 static void __perf_event_remove_from_context(void *info
)
419 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
420 struct perf_event
*event
= info
;
421 struct perf_event_context
*ctx
= event
->ctx
;
424 * If this is a task context, we need to check whether it is
425 * the current task context of this cpu. If not it has been
426 * scheduled out before the smp call arrived.
428 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
431 raw_spin_lock(&ctx
->lock
);
433 * Protect the list operation against NMI by disabling the
434 * events on a global level.
438 event_sched_out(event
, cpuctx
, ctx
);
440 list_del_event(event
, ctx
);
444 * Allow more per task events with respect to the
447 cpuctx
->max_pertask
=
448 min(perf_max_events
- ctx
->nr_events
,
449 perf_max_events
- perf_reserved_percpu
);
453 raw_spin_unlock(&ctx
->lock
);
458 * Remove the event from a task's (or a CPU's) list of events.
460 * Must be called with ctx->mutex held.
462 * CPU events are removed with a smp call. For task events we only
463 * call when the task is on a CPU.
465 * If event->ctx is a cloned context, callers must make sure that
466 * every task struct that event->ctx->task could possibly point to
467 * remains valid. This is OK when called from perf_release since
468 * that only calls us on the top-level context, which can't be a clone.
469 * When called from perf_event_exit_task, it's OK because the
470 * context has been detached from its task.
472 static void perf_event_remove_from_context(struct perf_event
*event
)
474 struct perf_event_context
*ctx
= event
->ctx
;
475 struct task_struct
*task
= ctx
->task
;
479 * Per cpu events are removed via an smp call and
480 * the removal is always successful.
482 smp_call_function_single(event
->cpu
,
483 __perf_event_remove_from_context
,
489 task_oncpu_function_call(task
, __perf_event_remove_from_context
,
492 raw_spin_lock_irq(&ctx
->lock
);
494 * If the context is active we need to retry the smp call.
496 if (ctx
->nr_active
&& !list_empty(&event
->group_entry
)) {
497 raw_spin_unlock_irq(&ctx
->lock
);
502 * The lock prevents that this context is scheduled in so we
503 * can remove the event safely, if the call above did not
506 if (!list_empty(&event
->group_entry
))
507 list_del_event(event
, ctx
);
508 raw_spin_unlock_irq(&ctx
->lock
);
512 * Update total_time_enabled and total_time_running for all events in a group.
514 static void update_group_times(struct perf_event
*leader
)
516 struct perf_event
*event
;
518 update_event_times(leader
);
519 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
520 update_event_times(event
);
524 * Cross CPU call to disable a performance event
526 static void __perf_event_disable(void *info
)
528 struct perf_event
*event
= info
;
529 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
530 struct perf_event_context
*ctx
= event
->ctx
;
533 * If this is a per-task event, need to check whether this
534 * event's task is the current task on this cpu.
536 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
539 raw_spin_lock(&ctx
->lock
);
542 * If the event is on, turn it off.
543 * If it is in error state, leave it in error state.
545 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
) {
546 update_context_time(ctx
);
547 update_group_times(event
);
548 if (event
== event
->group_leader
)
549 group_sched_out(event
, cpuctx
, ctx
);
551 event_sched_out(event
, cpuctx
, ctx
);
552 event
->state
= PERF_EVENT_STATE_OFF
;
555 raw_spin_unlock(&ctx
->lock
);
561 * If event->ctx is a cloned context, callers must make sure that
562 * every task struct that event->ctx->task could possibly point to
563 * remains valid. This condition is satisifed when called through
564 * perf_event_for_each_child or perf_event_for_each because they
565 * hold the top-level event's child_mutex, so any descendant that
566 * goes to exit will block in sync_child_event.
567 * When called from perf_pending_event it's OK because event->ctx
568 * is the current context on this CPU and preemption is disabled,
569 * hence we can't get into perf_event_task_sched_out for this context.
571 void perf_event_disable(struct perf_event
*event
)
573 struct perf_event_context
*ctx
= event
->ctx
;
574 struct task_struct
*task
= ctx
->task
;
578 * Disable the event on the cpu that it's on
580 smp_call_function_single(event
->cpu
, __perf_event_disable
,
586 task_oncpu_function_call(task
, __perf_event_disable
, event
);
588 raw_spin_lock_irq(&ctx
->lock
);
590 * If the event is still active, we need to retry the cross-call.
592 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
593 raw_spin_unlock_irq(&ctx
->lock
);
598 * Since we have the lock this context can't be scheduled
599 * in, so we can change the state safely.
601 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
602 update_group_times(event
);
603 event
->state
= PERF_EVENT_STATE_OFF
;
606 raw_spin_unlock_irq(&ctx
->lock
);
610 event_sched_in(struct perf_event
*event
,
611 struct perf_cpu_context
*cpuctx
,
612 struct perf_event_context
*ctx
,
615 if (event
->state
<= PERF_EVENT_STATE_OFF
)
618 event
->state
= PERF_EVENT_STATE_ACTIVE
;
619 event
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
621 * The new state must be visible before we turn it on in the hardware:
625 if (event
->pmu
->enable(event
)) {
626 event
->state
= PERF_EVENT_STATE_INACTIVE
;
631 event
->tstamp_running
+= ctx
->time
- event
->tstamp_stopped
;
633 if (!is_software_event(event
))
634 cpuctx
->active_oncpu
++;
637 if (event
->attr
.exclusive
)
638 cpuctx
->exclusive
= 1;
644 group_sched_in(struct perf_event
*group_event
,
645 struct perf_cpu_context
*cpuctx
,
646 struct perf_event_context
*ctx
,
649 struct perf_event
*event
, *partial_group
;
652 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
655 ret
= hw_perf_group_sched_in(group_event
, cpuctx
, ctx
, cpu
);
657 return ret
< 0 ? ret
: 0;
659 if (event_sched_in(group_event
, cpuctx
, ctx
, cpu
))
663 * Schedule in siblings as one group (if any):
665 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
666 if (event_sched_in(event
, cpuctx
, ctx
, cpu
)) {
667 partial_group
= event
;
676 * Groups can be scheduled in as one unit only, so undo any
677 * partial group before returning:
679 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
680 if (event
== partial_group
)
682 event_sched_out(event
, cpuctx
, ctx
);
684 event_sched_out(group_event
, cpuctx
, ctx
);
690 * Return 1 for a group consisting entirely of software events,
691 * 0 if the group contains any hardware events.
693 static int is_software_only_group(struct perf_event
*leader
)
695 struct perf_event
*event
;
697 if (!is_software_event(leader
))
700 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
701 if (!is_software_event(event
))
708 * Work out whether we can put this event group on the CPU now.
710 static int group_can_go_on(struct perf_event
*event
,
711 struct perf_cpu_context
*cpuctx
,
715 * Groups consisting entirely of software events can always go on.
717 if (is_software_only_group(event
))
720 * If an exclusive group is already on, no other hardware
723 if (cpuctx
->exclusive
)
726 * If this group is exclusive and there are already
727 * events on the CPU, it can't go on.
729 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
732 * Otherwise, try to add it if all previous groups were able
738 static void add_event_to_ctx(struct perf_event
*event
,
739 struct perf_event_context
*ctx
)
741 list_add_event(event
, ctx
);
742 event
->tstamp_enabled
= ctx
->time
;
743 event
->tstamp_running
= ctx
->time
;
744 event
->tstamp_stopped
= ctx
->time
;
748 * Cross CPU call to install and enable a performance event
750 * Must be called with ctx->mutex held
752 static void __perf_install_in_context(void *info
)
754 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
755 struct perf_event
*event
= info
;
756 struct perf_event_context
*ctx
= event
->ctx
;
757 struct perf_event
*leader
= event
->group_leader
;
758 int cpu
= smp_processor_id();
762 * If this is a task context, we need to check whether it is
763 * the current task context of this cpu. If not it has been
764 * scheduled out before the smp call arrived.
765 * Or possibly this is the right context but it isn't
766 * on this cpu because it had no events.
768 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
769 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
771 cpuctx
->task_ctx
= ctx
;
774 raw_spin_lock(&ctx
->lock
);
776 update_context_time(ctx
);
779 * Protect the list operation against NMI by disabling the
780 * events on a global level. NOP for non NMI based events.
784 add_event_to_ctx(event
, ctx
);
786 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
790 * Don't put the event on if it is disabled or if
791 * it is in a group and the group isn't on.
793 if (event
->state
!= PERF_EVENT_STATE_INACTIVE
||
794 (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
))
798 * An exclusive event can't go on if there are already active
799 * hardware events, and no hardware event can go on if there
800 * is already an exclusive event on.
802 if (!group_can_go_on(event
, cpuctx
, 1))
805 err
= event_sched_in(event
, cpuctx
, ctx
, cpu
);
809 * This event couldn't go on. If it is in a group
810 * then we have to pull the whole group off.
811 * If the event group is pinned then put it in error state.
814 group_sched_out(leader
, cpuctx
, ctx
);
815 if (leader
->attr
.pinned
) {
816 update_group_times(leader
);
817 leader
->state
= PERF_EVENT_STATE_ERROR
;
821 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
822 cpuctx
->max_pertask
--;
827 raw_spin_unlock(&ctx
->lock
);
831 * Attach a performance event to a context
833 * First we add the event to the list with the hardware enable bit
834 * in event->hw_config cleared.
836 * If the event is attached to a task which is on a CPU we use a smp
837 * call to enable it in the task context. The task might have been
838 * scheduled away, but we check this in the smp call again.
840 * Must be called with ctx->mutex held.
843 perf_install_in_context(struct perf_event_context
*ctx
,
844 struct perf_event
*event
,
847 struct task_struct
*task
= ctx
->task
;
851 * Per cpu events are installed via an smp call and
852 * the install is always successful.
854 smp_call_function_single(cpu
, __perf_install_in_context
,
860 task_oncpu_function_call(task
, __perf_install_in_context
,
863 raw_spin_lock_irq(&ctx
->lock
);
865 * we need to retry the smp call.
867 if (ctx
->is_active
&& list_empty(&event
->group_entry
)) {
868 raw_spin_unlock_irq(&ctx
->lock
);
873 * The lock prevents that this context is scheduled in so we
874 * can add the event safely, if it the call above did not
877 if (list_empty(&event
->group_entry
))
878 add_event_to_ctx(event
, ctx
);
879 raw_spin_unlock_irq(&ctx
->lock
);
883 * Put a event into inactive state and update time fields.
884 * Enabling the leader of a group effectively enables all
885 * the group members that aren't explicitly disabled, so we
886 * have to update their ->tstamp_enabled also.
887 * Note: this works for group members as well as group leaders
888 * since the non-leader members' sibling_lists will be empty.
890 static void __perf_event_mark_enabled(struct perf_event
*event
,
891 struct perf_event_context
*ctx
)
893 struct perf_event
*sub
;
895 event
->state
= PERF_EVENT_STATE_INACTIVE
;
896 event
->tstamp_enabled
= ctx
->time
- event
->total_time_enabled
;
897 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
)
898 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
)
899 sub
->tstamp_enabled
=
900 ctx
->time
- sub
->total_time_enabled
;
904 * Cross CPU call to enable a performance event
906 static void __perf_event_enable(void *info
)
908 struct perf_event
*event
= info
;
909 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
910 struct perf_event_context
*ctx
= event
->ctx
;
911 struct perf_event
*leader
= event
->group_leader
;
915 * If this is a per-task event, need to check whether this
916 * event's task is the current task on this cpu.
918 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
919 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
921 cpuctx
->task_ctx
= ctx
;
924 raw_spin_lock(&ctx
->lock
);
926 update_context_time(ctx
);
928 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
930 __perf_event_mark_enabled(event
, ctx
);
932 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
936 * If the event is in a group and isn't the group leader,
937 * then don't put it on unless the group is on.
939 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
)
942 if (!group_can_go_on(event
, cpuctx
, 1)) {
947 err
= group_sched_in(event
, cpuctx
, ctx
,
950 err
= event_sched_in(event
, cpuctx
, ctx
,
957 * If this event can't go on and it's part of a
958 * group, then the whole group has to come off.
961 group_sched_out(leader
, cpuctx
, ctx
);
962 if (leader
->attr
.pinned
) {
963 update_group_times(leader
);
964 leader
->state
= PERF_EVENT_STATE_ERROR
;
969 raw_spin_unlock(&ctx
->lock
);
975 * If event->ctx is a cloned context, callers must make sure that
976 * every task struct that event->ctx->task could possibly point to
977 * remains valid. This condition is satisfied when called through
978 * perf_event_for_each_child or perf_event_for_each as described
979 * for perf_event_disable.
981 void perf_event_enable(struct perf_event
*event
)
983 struct perf_event_context
*ctx
= event
->ctx
;
984 struct task_struct
*task
= ctx
->task
;
988 * Enable the event on the cpu that it's on
990 smp_call_function_single(event
->cpu
, __perf_event_enable
,
995 raw_spin_lock_irq(&ctx
->lock
);
996 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1000 * If the event is in error state, clear that first.
1001 * That way, if we see the event in error state below, we
1002 * know that it has gone back into error state, as distinct
1003 * from the task having been scheduled away before the
1004 * cross-call arrived.
1006 if (event
->state
== PERF_EVENT_STATE_ERROR
)
1007 event
->state
= PERF_EVENT_STATE_OFF
;
1010 raw_spin_unlock_irq(&ctx
->lock
);
1011 task_oncpu_function_call(task
, __perf_event_enable
, event
);
1013 raw_spin_lock_irq(&ctx
->lock
);
1016 * If the context is active and the event is still off,
1017 * we need to retry the cross-call.
1019 if (ctx
->is_active
&& event
->state
== PERF_EVENT_STATE_OFF
)
1023 * Since we have the lock this context can't be scheduled
1024 * in, so we can change the state safely.
1026 if (event
->state
== PERF_EVENT_STATE_OFF
)
1027 __perf_event_mark_enabled(event
, ctx
);
1030 raw_spin_unlock_irq(&ctx
->lock
);
1033 static int perf_event_refresh(struct perf_event
*event
, int refresh
)
1036 * not supported on inherited events
1038 if (event
->attr
.inherit
)
1041 atomic_add(refresh
, &event
->event_limit
);
1042 perf_event_enable(event
);
1047 void __perf_event_sched_out(struct perf_event_context
*ctx
,
1048 struct perf_cpu_context
*cpuctx
)
1050 struct perf_event
*event
;
1052 raw_spin_lock(&ctx
->lock
);
1054 if (likely(!ctx
->nr_events
))
1056 update_context_time(ctx
);
1059 if (ctx
->nr_active
) {
1060 list_for_each_entry(event
, &ctx
->group_list
, group_entry
)
1061 group_sched_out(event
, cpuctx
, ctx
);
1065 raw_spin_unlock(&ctx
->lock
);
1069 * Test whether two contexts are equivalent, i.e. whether they
1070 * have both been cloned from the same version of the same context
1071 * and they both have the same number of enabled events.
1072 * If the number of enabled events is the same, then the set
1073 * of enabled events should be the same, because these are both
1074 * inherited contexts, therefore we can't access individual events
1075 * in them directly with an fd; we can only enable/disable all
1076 * events via prctl, or enable/disable all events in a family
1077 * via ioctl, which will have the same effect on both contexts.
1079 static int context_equiv(struct perf_event_context
*ctx1
,
1080 struct perf_event_context
*ctx2
)
1082 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
1083 && ctx1
->parent_gen
== ctx2
->parent_gen
1084 && !ctx1
->pin_count
&& !ctx2
->pin_count
;
1087 static void __perf_event_sync_stat(struct perf_event
*event
,
1088 struct perf_event
*next_event
)
1092 if (!event
->attr
.inherit_stat
)
1096 * Update the event value, we cannot use perf_event_read()
1097 * because we're in the middle of a context switch and have IRQs
1098 * disabled, which upsets smp_call_function_single(), however
1099 * we know the event must be on the current CPU, therefore we
1100 * don't need to use it.
1102 switch (event
->state
) {
1103 case PERF_EVENT_STATE_ACTIVE
:
1104 event
->pmu
->read(event
);
1107 case PERF_EVENT_STATE_INACTIVE
:
1108 update_event_times(event
);
1116 * In order to keep per-task stats reliable we need to flip the event
1117 * values when we flip the contexts.
1119 value
= atomic64_read(&next_event
->count
);
1120 value
= atomic64_xchg(&event
->count
, value
);
1121 atomic64_set(&next_event
->count
, value
);
1123 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
1124 swap(event
->total_time_running
, next_event
->total_time_running
);
1127 * Since we swizzled the values, update the user visible data too.
1129 perf_event_update_userpage(event
);
1130 perf_event_update_userpage(next_event
);
1133 #define list_next_entry(pos, member) \
1134 list_entry(pos->member.next, typeof(*pos), member)
1136 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
1137 struct perf_event_context
*next_ctx
)
1139 struct perf_event
*event
, *next_event
;
1144 update_context_time(ctx
);
1146 event
= list_first_entry(&ctx
->event_list
,
1147 struct perf_event
, event_entry
);
1149 next_event
= list_first_entry(&next_ctx
->event_list
,
1150 struct perf_event
, event_entry
);
1152 while (&event
->event_entry
!= &ctx
->event_list
&&
1153 &next_event
->event_entry
!= &next_ctx
->event_list
) {
1155 __perf_event_sync_stat(event
, next_event
);
1157 event
= list_next_entry(event
, event_entry
);
1158 next_event
= list_next_entry(next_event
, event_entry
);
1163 * Called from scheduler to remove the events of the current task,
1164 * with interrupts disabled.
1166 * We stop each event and update the event value in event->count.
1168 * This does not protect us against NMI, but disable()
1169 * sets the disabled bit in the control field of event _before_
1170 * accessing the event control register. If a NMI hits, then it will
1171 * not restart the event.
1173 void perf_event_task_sched_out(struct task_struct
*task
,
1174 struct task_struct
*next
, int cpu
)
1176 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1177 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
1178 struct perf_event_context
*next_ctx
;
1179 struct perf_event_context
*parent
;
1180 struct pt_regs
*regs
;
1183 regs
= task_pt_regs(task
);
1184 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES
, 1, 1, regs
, 0);
1186 if (likely(!ctx
|| !cpuctx
->task_ctx
))
1190 parent
= rcu_dereference(ctx
->parent_ctx
);
1191 next_ctx
= next
->perf_event_ctxp
;
1192 if (parent
&& next_ctx
&&
1193 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
1195 * Looks like the two contexts are clones, so we might be
1196 * able to optimize the context switch. We lock both
1197 * contexts and check that they are clones under the
1198 * lock (including re-checking that neither has been
1199 * uncloned in the meantime). It doesn't matter which
1200 * order we take the locks because no other cpu could
1201 * be trying to lock both of these tasks.
1203 raw_spin_lock(&ctx
->lock
);
1204 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
1205 if (context_equiv(ctx
, next_ctx
)) {
1207 * XXX do we need a memory barrier of sorts
1208 * wrt to rcu_dereference() of perf_event_ctxp
1210 task
->perf_event_ctxp
= next_ctx
;
1211 next
->perf_event_ctxp
= ctx
;
1213 next_ctx
->task
= task
;
1216 perf_event_sync_stat(ctx
, next_ctx
);
1218 raw_spin_unlock(&next_ctx
->lock
);
1219 raw_spin_unlock(&ctx
->lock
);
1224 __perf_event_sched_out(ctx
, cpuctx
);
1225 cpuctx
->task_ctx
= NULL
;
1230 * Called with IRQs disabled
1232 static void __perf_event_task_sched_out(struct perf_event_context
*ctx
)
1234 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1236 if (!cpuctx
->task_ctx
)
1239 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
1242 __perf_event_sched_out(ctx
, cpuctx
);
1243 cpuctx
->task_ctx
= NULL
;
1247 * Called with IRQs disabled
1249 static void perf_event_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
1251 __perf_event_sched_out(&cpuctx
->ctx
, cpuctx
);
1255 __perf_event_sched_in(struct perf_event_context
*ctx
,
1256 struct perf_cpu_context
*cpuctx
, int cpu
)
1258 struct perf_event
*event
;
1261 raw_spin_lock(&ctx
->lock
);
1263 if (likely(!ctx
->nr_events
))
1266 ctx
->timestamp
= perf_clock();
1271 * First go through the list and put on any pinned groups
1272 * in order to give them the best chance of going on.
1274 list_for_each_entry(event
, &ctx
->group_list
, group_entry
) {
1275 if (event
->state
<= PERF_EVENT_STATE_OFF
||
1276 !event
->attr
.pinned
)
1278 if (event
->cpu
!= -1 && event
->cpu
!= cpu
)
1281 if (group_can_go_on(event
, cpuctx
, 1))
1282 group_sched_in(event
, cpuctx
, ctx
, cpu
);
1285 * If this pinned group hasn't been scheduled,
1286 * put it in error state.
1288 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1289 update_group_times(event
);
1290 event
->state
= PERF_EVENT_STATE_ERROR
;
1294 list_for_each_entry(event
, &ctx
->group_list
, group_entry
) {
1296 * Ignore events in OFF or ERROR state, and
1297 * ignore pinned events since we did them already.
1299 if (event
->state
<= PERF_EVENT_STATE_OFF
||
1304 * Listen to the 'cpu' scheduling filter constraint
1307 if (event
->cpu
!= -1 && event
->cpu
!= cpu
)
1310 if (group_can_go_on(event
, cpuctx
, can_add_hw
))
1311 if (group_sched_in(event
, cpuctx
, ctx
, cpu
))
1316 raw_spin_unlock(&ctx
->lock
);
1320 * Called from scheduler to add the events of the current task
1321 * with interrupts disabled.
1323 * We restore the event value and then enable it.
1325 * This does not protect us against NMI, but enable()
1326 * sets the enabled bit in the control field of event _before_
1327 * accessing the event control register. If a NMI hits, then it will
1328 * keep the event running.
1330 void perf_event_task_sched_in(struct task_struct
*task
, int cpu
)
1332 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1333 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
1337 if (cpuctx
->task_ctx
== ctx
)
1339 __perf_event_sched_in(ctx
, cpuctx
, cpu
);
1340 cpuctx
->task_ctx
= ctx
;
1343 static void perf_event_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
1345 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
1347 __perf_event_sched_in(ctx
, cpuctx
, cpu
);
1350 #define MAX_INTERRUPTS (~0ULL)
1352 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1354 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1356 u64 frequency
= event
->attr
.sample_freq
;
1357 u64 sec
= NSEC_PER_SEC
;
1358 u64 divisor
, dividend
;
1360 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
1362 count_fls
= fls64(count
);
1363 nsec_fls
= fls64(nsec
);
1364 frequency_fls
= fls64(frequency
);
1368 * We got @count in @nsec, with a target of sample_freq HZ
1369 * the target period becomes:
1372 * period = -------------------
1373 * @nsec * sample_freq
1378 * Reduce accuracy by one bit such that @a and @b converge
1379 * to a similar magnitude.
1381 #define REDUCE_FLS(a, b) \
1383 if (a##_fls > b##_fls) { \
1393 * Reduce accuracy until either term fits in a u64, then proceed with
1394 * the other, so that finally we can do a u64/u64 division.
1396 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
1397 REDUCE_FLS(nsec
, frequency
);
1398 REDUCE_FLS(sec
, count
);
1401 if (count_fls
+ sec_fls
> 64) {
1402 divisor
= nsec
* frequency
;
1404 while (count_fls
+ sec_fls
> 64) {
1405 REDUCE_FLS(count
, sec
);
1409 dividend
= count
* sec
;
1411 dividend
= count
* sec
;
1413 while (nsec_fls
+ frequency_fls
> 64) {
1414 REDUCE_FLS(nsec
, frequency
);
1418 divisor
= nsec
* frequency
;
1424 return div64_u64(dividend
, divisor
);
1427 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1429 struct hw_perf_event
*hwc
= &event
->hw
;
1430 s64 period
, sample_period
;
1433 period
= perf_calculate_period(event
, nsec
, count
);
1435 delta
= (s64
)(period
- hwc
->sample_period
);
1436 delta
= (delta
+ 7) / 8; /* low pass filter */
1438 sample_period
= hwc
->sample_period
+ delta
;
1443 hwc
->sample_period
= sample_period
;
1445 if (atomic64_read(&hwc
->period_left
) > 8*sample_period
) {
1447 event
->pmu
->disable(event
);
1448 atomic64_set(&hwc
->period_left
, 0);
1449 event
->pmu
->enable(event
);
1454 static void perf_ctx_adjust_freq(struct perf_event_context
*ctx
)
1456 struct perf_event
*event
;
1457 struct hw_perf_event
*hwc
;
1458 u64 interrupts
, now
;
1461 raw_spin_lock(&ctx
->lock
);
1462 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
1463 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1466 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1471 interrupts
= hwc
->interrupts
;
1472 hwc
->interrupts
= 0;
1475 * unthrottle events on the tick
1477 if (interrupts
== MAX_INTERRUPTS
) {
1478 perf_log_throttle(event
, 1);
1479 event
->pmu
->unthrottle(event
);
1482 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
1485 event
->pmu
->read(event
);
1486 now
= atomic64_read(&event
->count
);
1487 delta
= now
- hwc
->freq_count_stamp
;
1488 hwc
->freq_count_stamp
= now
;
1491 perf_adjust_period(event
, TICK_NSEC
, delta
);
1493 raw_spin_unlock(&ctx
->lock
);
1497 * Round-robin a context's events:
1499 static void rotate_ctx(struct perf_event_context
*ctx
)
1501 struct perf_event
*event
;
1503 if (!ctx
->nr_events
)
1506 raw_spin_lock(&ctx
->lock
);
1508 * Rotate the first entry last (works just fine for group events too):
1511 list_for_each_entry(event
, &ctx
->group_list
, group_entry
) {
1512 list_move_tail(&event
->group_entry
, &ctx
->group_list
);
1517 raw_spin_unlock(&ctx
->lock
);
1520 void perf_event_task_tick(struct task_struct
*curr
, int cpu
)
1522 struct perf_cpu_context
*cpuctx
;
1523 struct perf_event_context
*ctx
;
1525 if (!atomic_read(&nr_events
))
1528 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1529 ctx
= curr
->perf_event_ctxp
;
1531 perf_ctx_adjust_freq(&cpuctx
->ctx
);
1533 perf_ctx_adjust_freq(ctx
);
1535 perf_event_cpu_sched_out(cpuctx
);
1537 __perf_event_task_sched_out(ctx
);
1539 rotate_ctx(&cpuctx
->ctx
);
1543 perf_event_cpu_sched_in(cpuctx
, cpu
);
1545 perf_event_task_sched_in(curr
, cpu
);
1549 * Enable all of a task's events that have been marked enable-on-exec.
1550 * This expects task == current.
1552 static void perf_event_enable_on_exec(struct task_struct
*task
)
1554 struct perf_event_context
*ctx
;
1555 struct perf_event
*event
;
1556 unsigned long flags
;
1559 local_irq_save(flags
);
1560 ctx
= task
->perf_event_ctxp
;
1561 if (!ctx
|| !ctx
->nr_events
)
1564 __perf_event_task_sched_out(ctx
);
1566 raw_spin_lock(&ctx
->lock
);
1568 list_for_each_entry(event
, &ctx
->group_list
, group_entry
) {
1569 if (!event
->attr
.enable_on_exec
)
1571 event
->attr
.enable_on_exec
= 0;
1572 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1574 __perf_event_mark_enabled(event
, ctx
);
1579 * Unclone this context if we enabled any event.
1584 raw_spin_unlock(&ctx
->lock
);
1586 perf_event_task_sched_in(task
, smp_processor_id());
1588 local_irq_restore(flags
);
1592 * Cross CPU call to read the hardware event
1594 static void __perf_event_read(void *info
)
1596 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1597 struct perf_event
*event
= info
;
1598 struct perf_event_context
*ctx
= event
->ctx
;
1601 * If this is a task context, we need to check whether it is
1602 * the current task context of this cpu. If not it has been
1603 * scheduled out before the smp call arrived. In that case
1604 * event->count would have been updated to a recent sample
1605 * when the event was scheduled out.
1607 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
1610 raw_spin_lock(&ctx
->lock
);
1611 update_context_time(ctx
);
1612 update_event_times(event
);
1613 raw_spin_unlock(&ctx
->lock
);
1615 event
->pmu
->read(event
);
1618 static u64
perf_event_read(struct perf_event
*event
)
1621 * If event is enabled and currently active on a CPU, update the
1622 * value in the event structure:
1624 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
1625 smp_call_function_single(event
->oncpu
,
1626 __perf_event_read
, event
, 1);
1627 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1628 struct perf_event_context
*ctx
= event
->ctx
;
1629 unsigned long flags
;
1631 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
1632 update_context_time(ctx
);
1633 update_event_times(event
);
1634 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1637 return atomic64_read(&event
->count
);
1641 * Initialize the perf_event context in a task_struct:
1644 __perf_event_init_context(struct perf_event_context
*ctx
,
1645 struct task_struct
*task
)
1647 raw_spin_lock_init(&ctx
->lock
);
1648 mutex_init(&ctx
->mutex
);
1649 INIT_LIST_HEAD(&ctx
->group_list
);
1650 INIT_LIST_HEAD(&ctx
->event_list
);
1651 atomic_set(&ctx
->refcount
, 1);
1655 static struct perf_event_context
*find_get_context(pid_t pid
, int cpu
)
1657 struct perf_event_context
*ctx
;
1658 struct perf_cpu_context
*cpuctx
;
1659 struct task_struct
*task
;
1660 unsigned long flags
;
1663 if (pid
== -1 && cpu
!= -1) {
1664 /* Must be root to operate on a CPU event: */
1665 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
1666 return ERR_PTR(-EACCES
);
1668 if (cpu
< 0 || cpu
>= nr_cpumask_bits
)
1669 return ERR_PTR(-EINVAL
);
1672 * We could be clever and allow to attach a event to an
1673 * offline CPU and activate it when the CPU comes up, but
1676 if (!cpu_online(cpu
))
1677 return ERR_PTR(-ENODEV
);
1679 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1690 task
= find_task_by_vpid(pid
);
1692 get_task_struct(task
);
1696 return ERR_PTR(-ESRCH
);
1699 * Can't attach events to a dying task.
1702 if (task
->flags
& PF_EXITING
)
1705 /* Reuse ptrace permission checks for now. */
1707 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1711 ctx
= perf_lock_task_context(task
, &flags
);
1714 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1718 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
1722 __perf_event_init_context(ctx
, task
);
1724 if (cmpxchg(&task
->perf_event_ctxp
, NULL
, ctx
)) {
1726 * We raced with some other task; use
1727 * the context they set.
1732 get_task_struct(task
);
1735 put_task_struct(task
);
1739 put_task_struct(task
);
1740 return ERR_PTR(err
);
1743 static void perf_event_free_filter(struct perf_event
*event
);
1745 static void free_event_rcu(struct rcu_head
*head
)
1747 struct perf_event
*event
;
1749 event
= container_of(head
, struct perf_event
, rcu_head
);
1751 put_pid_ns(event
->ns
);
1752 perf_event_free_filter(event
);
1756 static void perf_pending_sync(struct perf_event
*event
);
1758 static void free_event(struct perf_event
*event
)
1760 perf_pending_sync(event
);
1762 if (!event
->parent
) {
1763 atomic_dec(&nr_events
);
1764 if (event
->attr
.mmap
)
1765 atomic_dec(&nr_mmap_events
);
1766 if (event
->attr
.comm
)
1767 atomic_dec(&nr_comm_events
);
1768 if (event
->attr
.task
)
1769 atomic_dec(&nr_task_events
);
1772 if (event
->output
) {
1773 fput(event
->output
->filp
);
1774 event
->output
= NULL
;
1778 event
->destroy(event
);
1780 put_ctx(event
->ctx
);
1781 call_rcu(&event
->rcu_head
, free_event_rcu
);
1784 int perf_event_release_kernel(struct perf_event
*event
)
1786 struct perf_event_context
*ctx
= event
->ctx
;
1788 WARN_ON_ONCE(ctx
->parent_ctx
);
1789 mutex_lock(&ctx
->mutex
);
1790 perf_event_remove_from_context(event
);
1791 mutex_unlock(&ctx
->mutex
);
1793 mutex_lock(&event
->owner
->perf_event_mutex
);
1794 list_del_init(&event
->owner_entry
);
1795 mutex_unlock(&event
->owner
->perf_event_mutex
);
1796 put_task_struct(event
->owner
);
1802 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
1805 * Called when the last reference to the file is gone.
1807 static int perf_release(struct inode
*inode
, struct file
*file
)
1809 struct perf_event
*event
= file
->private_data
;
1811 file
->private_data
= NULL
;
1813 return perf_event_release_kernel(event
);
1816 static int perf_event_read_size(struct perf_event
*event
)
1818 int entry
= sizeof(u64
); /* value */
1822 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1823 size
+= sizeof(u64
);
1825 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1826 size
+= sizeof(u64
);
1828 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
1829 entry
+= sizeof(u64
);
1831 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
1832 nr
+= event
->group_leader
->nr_siblings
;
1833 size
+= sizeof(u64
);
1841 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
1843 struct perf_event
*child
;
1849 mutex_lock(&event
->child_mutex
);
1850 total
+= perf_event_read(event
);
1851 *enabled
+= event
->total_time_enabled
+
1852 atomic64_read(&event
->child_total_time_enabled
);
1853 *running
+= event
->total_time_running
+
1854 atomic64_read(&event
->child_total_time_running
);
1856 list_for_each_entry(child
, &event
->child_list
, child_list
) {
1857 total
+= perf_event_read(child
);
1858 *enabled
+= child
->total_time_enabled
;
1859 *running
+= child
->total_time_running
;
1861 mutex_unlock(&event
->child_mutex
);
1865 EXPORT_SYMBOL_GPL(perf_event_read_value
);
1867 static int perf_event_read_group(struct perf_event
*event
,
1868 u64 read_format
, char __user
*buf
)
1870 struct perf_event
*leader
= event
->group_leader
, *sub
;
1871 int n
= 0, size
= 0, ret
= -EFAULT
;
1872 struct perf_event_context
*ctx
= leader
->ctx
;
1874 u64 count
, enabled
, running
;
1876 mutex_lock(&ctx
->mutex
);
1877 count
= perf_event_read_value(leader
, &enabled
, &running
);
1879 values
[n
++] = 1 + leader
->nr_siblings
;
1880 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1881 values
[n
++] = enabled
;
1882 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1883 values
[n
++] = running
;
1884 values
[n
++] = count
;
1885 if (read_format
& PERF_FORMAT_ID
)
1886 values
[n
++] = primary_event_id(leader
);
1888 size
= n
* sizeof(u64
);
1890 if (copy_to_user(buf
, values
, size
))
1895 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
1898 values
[n
++] = perf_event_read_value(sub
, &enabled
, &running
);
1899 if (read_format
& PERF_FORMAT_ID
)
1900 values
[n
++] = primary_event_id(sub
);
1902 size
= n
* sizeof(u64
);
1904 if (copy_to_user(buf
+ ret
, values
, size
)) {
1912 mutex_unlock(&ctx
->mutex
);
1917 static int perf_event_read_one(struct perf_event
*event
,
1918 u64 read_format
, char __user
*buf
)
1920 u64 enabled
, running
;
1924 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
1925 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1926 values
[n
++] = enabled
;
1927 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1928 values
[n
++] = running
;
1929 if (read_format
& PERF_FORMAT_ID
)
1930 values
[n
++] = primary_event_id(event
);
1932 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
1935 return n
* sizeof(u64
);
1939 * Read the performance event - simple non blocking version for now
1942 perf_read_hw(struct perf_event
*event
, char __user
*buf
, size_t count
)
1944 u64 read_format
= event
->attr
.read_format
;
1948 * Return end-of-file for a read on a event that is in
1949 * error state (i.e. because it was pinned but it couldn't be
1950 * scheduled on to the CPU at some point).
1952 if (event
->state
== PERF_EVENT_STATE_ERROR
)
1955 if (count
< perf_event_read_size(event
))
1958 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
1959 if (read_format
& PERF_FORMAT_GROUP
)
1960 ret
= perf_event_read_group(event
, read_format
, buf
);
1962 ret
= perf_event_read_one(event
, read_format
, buf
);
1968 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1970 struct perf_event
*event
= file
->private_data
;
1972 return perf_read_hw(event
, buf
, count
);
1975 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1977 struct perf_event
*event
= file
->private_data
;
1978 struct perf_mmap_data
*data
;
1979 unsigned int events
= POLL_HUP
;
1982 data
= rcu_dereference(event
->data
);
1984 events
= atomic_xchg(&data
->poll
, 0);
1987 poll_wait(file
, &event
->waitq
, wait
);
1992 static void perf_event_reset(struct perf_event
*event
)
1994 (void)perf_event_read(event
);
1995 atomic64_set(&event
->count
, 0);
1996 perf_event_update_userpage(event
);
2000 * Holding the top-level event's child_mutex means that any
2001 * descendant process that has inherited this event will block
2002 * in sync_child_event if it goes to exit, thus satisfying the
2003 * task existence requirements of perf_event_enable/disable.
2005 static void perf_event_for_each_child(struct perf_event
*event
,
2006 void (*func
)(struct perf_event
*))
2008 struct perf_event
*child
;
2010 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2011 mutex_lock(&event
->child_mutex
);
2013 list_for_each_entry(child
, &event
->child_list
, child_list
)
2015 mutex_unlock(&event
->child_mutex
);
2018 static void perf_event_for_each(struct perf_event
*event
,
2019 void (*func
)(struct perf_event
*))
2021 struct perf_event_context
*ctx
= event
->ctx
;
2022 struct perf_event
*sibling
;
2024 WARN_ON_ONCE(ctx
->parent_ctx
);
2025 mutex_lock(&ctx
->mutex
);
2026 event
= event
->group_leader
;
2028 perf_event_for_each_child(event
, func
);
2030 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
2031 perf_event_for_each_child(event
, func
);
2032 mutex_unlock(&ctx
->mutex
);
2035 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
2037 struct perf_event_context
*ctx
= event
->ctx
;
2042 if (!event
->attr
.sample_period
)
2045 size
= copy_from_user(&value
, arg
, sizeof(value
));
2046 if (size
!= sizeof(value
))
2052 raw_spin_lock_irq(&ctx
->lock
);
2053 if (event
->attr
.freq
) {
2054 if (value
> sysctl_perf_event_sample_rate
) {
2059 event
->attr
.sample_freq
= value
;
2061 event
->attr
.sample_period
= value
;
2062 event
->hw
.sample_period
= value
;
2065 raw_spin_unlock_irq(&ctx
->lock
);
2070 static int perf_event_set_output(struct perf_event
*event
, int output_fd
);
2071 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
2073 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2075 struct perf_event
*event
= file
->private_data
;
2076 void (*func
)(struct perf_event
*);
2080 case PERF_EVENT_IOC_ENABLE
:
2081 func
= perf_event_enable
;
2083 case PERF_EVENT_IOC_DISABLE
:
2084 func
= perf_event_disable
;
2086 case PERF_EVENT_IOC_RESET
:
2087 func
= perf_event_reset
;
2090 case PERF_EVENT_IOC_REFRESH
:
2091 return perf_event_refresh(event
, arg
);
2093 case PERF_EVENT_IOC_PERIOD
:
2094 return perf_event_period(event
, (u64 __user
*)arg
);
2096 case PERF_EVENT_IOC_SET_OUTPUT
:
2097 return perf_event_set_output(event
, arg
);
2099 case PERF_EVENT_IOC_SET_FILTER
:
2100 return perf_event_set_filter(event
, (void __user
*)arg
);
2106 if (flags
& PERF_IOC_FLAG_GROUP
)
2107 perf_event_for_each(event
, func
);
2109 perf_event_for_each_child(event
, func
);
2114 int perf_event_task_enable(void)
2116 struct perf_event
*event
;
2118 mutex_lock(¤t
->perf_event_mutex
);
2119 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2120 perf_event_for_each_child(event
, perf_event_enable
);
2121 mutex_unlock(¤t
->perf_event_mutex
);
2126 int perf_event_task_disable(void)
2128 struct perf_event
*event
;
2130 mutex_lock(¤t
->perf_event_mutex
);
2131 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2132 perf_event_for_each_child(event
, perf_event_disable
);
2133 mutex_unlock(¤t
->perf_event_mutex
);
2138 #ifndef PERF_EVENT_INDEX_OFFSET
2139 # define PERF_EVENT_INDEX_OFFSET 0
2142 static int perf_event_index(struct perf_event
*event
)
2144 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
2147 return event
->hw
.idx
+ 1 - PERF_EVENT_INDEX_OFFSET
;
2151 * Callers need to ensure there can be no nesting of this function, otherwise
2152 * the seqlock logic goes bad. We can not serialize this because the arch
2153 * code calls this from NMI context.
2155 void perf_event_update_userpage(struct perf_event
*event
)
2157 struct perf_event_mmap_page
*userpg
;
2158 struct perf_mmap_data
*data
;
2161 data
= rcu_dereference(event
->data
);
2165 userpg
= data
->user_page
;
2168 * Disable preemption so as to not let the corresponding user-space
2169 * spin too long if we get preempted.
2174 userpg
->index
= perf_event_index(event
);
2175 userpg
->offset
= atomic64_read(&event
->count
);
2176 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
2177 userpg
->offset
-= atomic64_read(&event
->hw
.prev_count
);
2179 userpg
->time_enabled
= event
->total_time_enabled
+
2180 atomic64_read(&event
->child_total_time_enabled
);
2182 userpg
->time_running
= event
->total_time_running
+
2183 atomic64_read(&event
->child_total_time_running
);
2192 static unsigned long perf_data_size(struct perf_mmap_data
*data
)
2194 return data
->nr_pages
<< (PAGE_SHIFT
+ data
->data_order
);
2197 #ifndef CONFIG_PERF_USE_VMALLOC
2200 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2203 static struct page
*
2204 perf_mmap_to_page(struct perf_mmap_data
*data
, unsigned long pgoff
)
2206 if (pgoff
> data
->nr_pages
)
2210 return virt_to_page(data
->user_page
);
2212 return virt_to_page(data
->data_pages
[pgoff
- 1]);
2215 static struct perf_mmap_data
*
2216 perf_mmap_data_alloc(struct perf_event
*event
, int nr_pages
)
2218 struct perf_mmap_data
*data
;
2222 WARN_ON(atomic_read(&event
->mmap_count
));
2224 size
= sizeof(struct perf_mmap_data
);
2225 size
+= nr_pages
* sizeof(void *);
2227 data
= kzalloc(size
, GFP_KERNEL
);
2231 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
2232 if (!data
->user_page
)
2233 goto fail_user_page
;
2235 for (i
= 0; i
< nr_pages
; i
++) {
2236 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
2237 if (!data
->data_pages
[i
])
2238 goto fail_data_pages
;
2241 data
->data_order
= 0;
2242 data
->nr_pages
= nr_pages
;
2247 for (i
--; i
>= 0; i
--)
2248 free_page((unsigned long)data
->data_pages
[i
]);
2250 free_page((unsigned long)data
->user_page
);
2259 static void perf_mmap_free_page(unsigned long addr
)
2261 struct page
*page
= virt_to_page((void *)addr
);
2263 page
->mapping
= NULL
;
2267 static void perf_mmap_data_free(struct perf_mmap_data
*data
)
2271 perf_mmap_free_page((unsigned long)data
->user_page
);
2272 for (i
= 0; i
< data
->nr_pages
; i
++)
2273 perf_mmap_free_page((unsigned long)data
->data_pages
[i
]);
2280 * Back perf_mmap() with vmalloc memory.
2282 * Required for architectures that have d-cache aliasing issues.
2285 static struct page
*
2286 perf_mmap_to_page(struct perf_mmap_data
*data
, unsigned long pgoff
)
2288 if (pgoff
> (1UL << data
->data_order
))
2291 return vmalloc_to_page((void *)data
->user_page
+ pgoff
* PAGE_SIZE
);
2294 static void perf_mmap_unmark_page(void *addr
)
2296 struct page
*page
= vmalloc_to_page(addr
);
2298 page
->mapping
= NULL
;
2301 static void perf_mmap_data_free_work(struct work_struct
*work
)
2303 struct perf_mmap_data
*data
;
2307 data
= container_of(work
, struct perf_mmap_data
, work
);
2308 nr
= 1 << data
->data_order
;
2310 base
= data
->user_page
;
2311 for (i
= 0; i
< nr
+ 1; i
++)
2312 perf_mmap_unmark_page(base
+ (i
* PAGE_SIZE
));
2318 static void perf_mmap_data_free(struct perf_mmap_data
*data
)
2320 schedule_work(&data
->work
);
2323 static struct perf_mmap_data
*
2324 perf_mmap_data_alloc(struct perf_event
*event
, int nr_pages
)
2326 struct perf_mmap_data
*data
;
2330 WARN_ON(atomic_read(&event
->mmap_count
));
2332 size
= sizeof(struct perf_mmap_data
);
2333 size
+= sizeof(void *);
2335 data
= kzalloc(size
, GFP_KERNEL
);
2339 INIT_WORK(&data
->work
, perf_mmap_data_free_work
);
2341 all_buf
= vmalloc_user((nr_pages
+ 1) * PAGE_SIZE
);
2345 data
->user_page
= all_buf
;
2346 data
->data_pages
[0] = all_buf
+ PAGE_SIZE
;
2347 data
->data_order
= ilog2(nr_pages
);
2361 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2363 struct perf_event
*event
= vma
->vm_file
->private_data
;
2364 struct perf_mmap_data
*data
;
2365 int ret
= VM_FAULT_SIGBUS
;
2367 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
2368 if (vmf
->pgoff
== 0)
2374 data
= rcu_dereference(event
->data
);
2378 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
2381 vmf
->page
= perf_mmap_to_page(data
, vmf
->pgoff
);
2385 get_page(vmf
->page
);
2386 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
2387 vmf
->page
->index
= vmf
->pgoff
;
2397 perf_mmap_data_init(struct perf_event
*event
, struct perf_mmap_data
*data
)
2399 long max_size
= perf_data_size(data
);
2401 atomic_set(&data
->lock
, -1);
2403 if (event
->attr
.watermark
) {
2404 data
->watermark
= min_t(long, max_size
,
2405 event
->attr
.wakeup_watermark
);
2408 if (!data
->watermark
)
2409 data
->watermark
= max_size
/ 2;
2412 rcu_assign_pointer(event
->data
, data
);
2415 static void perf_mmap_data_free_rcu(struct rcu_head
*rcu_head
)
2417 struct perf_mmap_data
*data
;
2419 data
= container_of(rcu_head
, struct perf_mmap_data
, rcu_head
);
2420 perf_mmap_data_free(data
);
2423 static void perf_mmap_data_release(struct perf_event
*event
)
2425 struct perf_mmap_data
*data
= event
->data
;
2427 WARN_ON(atomic_read(&event
->mmap_count
));
2429 rcu_assign_pointer(event
->data
, NULL
);
2430 call_rcu(&data
->rcu_head
, perf_mmap_data_free_rcu
);
2433 static void perf_mmap_open(struct vm_area_struct
*vma
)
2435 struct perf_event
*event
= vma
->vm_file
->private_data
;
2437 atomic_inc(&event
->mmap_count
);
2440 static void perf_mmap_close(struct vm_area_struct
*vma
)
2442 struct perf_event
*event
= vma
->vm_file
->private_data
;
2444 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2445 if (atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
)) {
2446 unsigned long size
= perf_data_size(event
->data
);
2447 struct user_struct
*user
= current_user();
2449 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &user
->locked_vm
);
2450 vma
->vm_mm
->locked_vm
-= event
->data
->nr_locked
;
2451 perf_mmap_data_release(event
);
2452 mutex_unlock(&event
->mmap_mutex
);
2456 static const struct vm_operations_struct perf_mmap_vmops
= {
2457 .open
= perf_mmap_open
,
2458 .close
= perf_mmap_close
,
2459 .fault
= perf_mmap_fault
,
2460 .page_mkwrite
= perf_mmap_fault
,
2463 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2465 struct perf_event
*event
= file
->private_data
;
2466 unsigned long user_locked
, user_lock_limit
;
2467 struct user_struct
*user
= current_user();
2468 unsigned long locked
, lock_limit
;
2469 struct perf_mmap_data
*data
;
2470 unsigned long vma_size
;
2471 unsigned long nr_pages
;
2472 long user_extra
, extra
;
2475 if (!(vma
->vm_flags
& VM_SHARED
))
2478 vma_size
= vma
->vm_end
- vma
->vm_start
;
2479 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
2482 * If we have data pages ensure they're a power-of-two number, so we
2483 * can do bitmasks instead of modulo.
2485 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
2488 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
2491 if (vma
->vm_pgoff
!= 0)
2494 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2495 mutex_lock(&event
->mmap_mutex
);
2496 if (event
->output
) {
2501 if (atomic_inc_not_zero(&event
->mmap_count
)) {
2502 if (nr_pages
!= event
->data
->nr_pages
)
2507 user_extra
= nr_pages
+ 1;
2508 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
2511 * Increase the limit linearly with more CPUs:
2513 user_lock_limit
*= num_online_cpus();
2515 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
2518 if (user_locked
> user_lock_limit
)
2519 extra
= user_locked
- user_lock_limit
;
2521 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
2522 lock_limit
>>= PAGE_SHIFT
;
2523 locked
= vma
->vm_mm
->locked_vm
+ extra
;
2525 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
2526 !capable(CAP_IPC_LOCK
)) {
2531 WARN_ON(event
->data
);
2533 data
= perf_mmap_data_alloc(event
, nr_pages
);
2539 perf_mmap_data_init(event
, data
);
2541 atomic_set(&event
->mmap_count
, 1);
2542 atomic_long_add(user_extra
, &user
->locked_vm
);
2543 vma
->vm_mm
->locked_vm
+= extra
;
2544 event
->data
->nr_locked
= extra
;
2545 if (vma
->vm_flags
& VM_WRITE
)
2546 event
->data
->writable
= 1;
2549 mutex_unlock(&event
->mmap_mutex
);
2551 vma
->vm_flags
|= VM_RESERVED
;
2552 vma
->vm_ops
= &perf_mmap_vmops
;
2557 static int perf_fasync(int fd
, struct file
*filp
, int on
)
2559 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
2560 struct perf_event
*event
= filp
->private_data
;
2563 mutex_lock(&inode
->i_mutex
);
2564 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
2565 mutex_unlock(&inode
->i_mutex
);
2573 static const struct file_operations perf_fops
= {
2574 .release
= perf_release
,
2577 .unlocked_ioctl
= perf_ioctl
,
2578 .compat_ioctl
= perf_ioctl
,
2580 .fasync
= perf_fasync
,
2586 * If there's data, ensure we set the poll() state and publish everything
2587 * to user-space before waking everybody up.
2590 void perf_event_wakeup(struct perf_event
*event
)
2592 wake_up_all(&event
->waitq
);
2594 if (event
->pending_kill
) {
2595 kill_fasync(&event
->fasync
, SIGIO
, event
->pending_kill
);
2596 event
->pending_kill
= 0;
2603 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2605 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2606 * single linked list and use cmpxchg() to add entries lockless.
2609 static void perf_pending_event(struct perf_pending_entry
*entry
)
2611 struct perf_event
*event
= container_of(entry
,
2612 struct perf_event
, pending
);
2614 if (event
->pending_disable
) {
2615 event
->pending_disable
= 0;
2616 __perf_event_disable(event
);
2619 if (event
->pending_wakeup
) {
2620 event
->pending_wakeup
= 0;
2621 perf_event_wakeup(event
);
2625 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2627 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
2631 static void perf_pending_queue(struct perf_pending_entry
*entry
,
2632 void (*func
)(struct perf_pending_entry
*))
2634 struct perf_pending_entry
**head
;
2636 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
2641 head
= &get_cpu_var(perf_pending_head
);
2644 entry
->next
= *head
;
2645 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
2647 set_perf_event_pending();
2649 put_cpu_var(perf_pending_head
);
2652 static int __perf_pending_run(void)
2654 struct perf_pending_entry
*list
;
2657 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
2658 while (list
!= PENDING_TAIL
) {
2659 void (*func
)(struct perf_pending_entry
*);
2660 struct perf_pending_entry
*entry
= list
;
2667 * Ensure we observe the unqueue before we issue the wakeup,
2668 * so that we won't be waiting forever.
2669 * -- see perf_not_pending().
2680 static inline int perf_not_pending(struct perf_event
*event
)
2683 * If we flush on whatever cpu we run, there is a chance we don't
2687 __perf_pending_run();
2691 * Ensure we see the proper queue state before going to sleep
2692 * so that we do not miss the wakeup. -- see perf_pending_handle()
2695 return event
->pending
.next
== NULL
;
2698 static void perf_pending_sync(struct perf_event
*event
)
2700 wait_event(event
->waitq
, perf_not_pending(event
));
2703 void perf_event_do_pending(void)
2705 __perf_pending_run();
2709 * Callchain support -- arch specific
2712 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
2720 static bool perf_output_space(struct perf_mmap_data
*data
, unsigned long tail
,
2721 unsigned long offset
, unsigned long head
)
2725 if (!data
->writable
)
2728 mask
= perf_data_size(data
) - 1;
2730 offset
= (offset
- tail
) & mask
;
2731 head
= (head
- tail
) & mask
;
2733 if ((int)(head
- offset
) < 0)
2739 static void perf_output_wakeup(struct perf_output_handle
*handle
)
2741 atomic_set(&handle
->data
->poll
, POLL_IN
);
2744 handle
->event
->pending_wakeup
= 1;
2745 perf_pending_queue(&handle
->event
->pending
,
2746 perf_pending_event
);
2748 perf_event_wakeup(handle
->event
);
2752 * Curious locking construct.
2754 * We need to ensure a later event_id doesn't publish a head when a former
2755 * event_id isn't done writing. However since we need to deal with NMIs we
2756 * cannot fully serialize things.
2758 * What we do is serialize between CPUs so we only have to deal with NMI
2759 * nesting on a single CPU.
2761 * We only publish the head (and generate a wakeup) when the outer-most
2762 * event_id completes.
2764 static void perf_output_lock(struct perf_output_handle
*handle
)
2766 struct perf_mmap_data
*data
= handle
->data
;
2767 int cur
, cpu
= get_cpu();
2772 cur
= atomic_cmpxchg(&data
->lock
, -1, cpu
);
2784 static void perf_output_unlock(struct perf_output_handle
*handle
)
2786 struct perf_mmap_data
*data
= handle
->data
;
2790 data
->done_head
= data
->head
;
2792 if (!handle
->locked
)
2797 * The xchg implies a full barrier that ensures all writes are done
2798 * before we publish the new head, matched by a rmb() in userspace when
2799 * reading this position.
2801 while ((head
= atomic_long_xchg(&data
->done_head
, 0)))
2802 data
->user_page
->data_head
= head
;
2805 * NMI can happen here, which means we can miss a done_head update.
2808 cpu
= atomic_xchg(&data
->lock
, -1);
2809 WARN_ON_ONCE(cpu
!= smp_processor_id());
2812 * Therefore we have to validate we did not indeed do so.
2814 if (unlikely(atomic_long_read(&data
->done_head
))) {
2816 * Since we had it locked, we can lock it again.
2818 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2824 if (atomic_xchg(&data
->wakeup
, 0))
2825 perf_output_wakeup(handle
);
2830 void perf_output_copy(struct perf_output_handle
*handle
,
2831 const void *buf
, unsigned int len
)
2833 unsigned int pages_mask
;
2834 unsigned long offset
;
2838 offset
= handle
->offset
;
2839 pages_mask
= handle
->data
->nr_pages
- 1;
2840 pages
= handle
->data
->data_pages
;
2843 unsigned long page_offset
;
2844 unsigned long page_size
;
2847 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
2848 page_size
= 1UL << (handle
->data
->data_order
+ PAGE_SHIFT
);
2849 page_offset
= offset
& (page_size
- 1);
2850 size
= min_t(unsigned int, page_size
- page_offset
, len
);
2852 memcpy(pages
[nr
] + page_offset
, buf
, size
);
2859 handle
->offset
= offset
;
2862 * Check we didn't copy past our reservation window, taking the
2863 * possible unsigned int wrap into account.
2865 WARN_ON_ONCE(((long)(handle
->head
- handle
->offset
)) < 0);
2868 int perf_output_begin(struct perf_output_handle
*handle
,
2869 struct perf_event
*event
, unsigned int size
,
2870 int nmi
, int sample
)
2872 struct perf_event
*output_event
;
2873 struct perf_mmap_data
*data
;
2874 unsigned long tail
, offset
, head
;
2877 struct perf_event_header header
;
2884 * For inherited events we send all the output towards the parent.
2887 event
= event
->parent
;
2889 output_event
= rcu_dereference(event
->output
);
2891 event
= output_event
;
2893 data
= rcu_dereference(event
->data
);
2897 handle
->data
= data
;
2898 handle
->event
= event
;
2900 handle
->sample
= sample
;
2902 if (!data
->nr_pages
)
2905 have_lost
= atomic_read(&data
->lost
);
2907 size
+= sizeof(lost_event
);
2909 perf_output_lock(handle
);
2913 * Userspace could choose to issue a mb() before updating the
2914 * tail pointer. So that all reads will be completed before the
2917 tail
= ACCESS_ONCE(data
->user_page
->data_tail
);
2919 offset
= head
= atomic_long_read(&data
->head
);
2921 if (unlikely(!perf_output_space(data
, tail
, offset
, head
)))
2923 } while (atomic_long_cmpxchg(&data
->head
, offset
, head
) != offset
);
2925 handle
->offset
= offset
;
2926 handle
->head
= head
;
2928 if (head
- tail
> data
->watermark
)
2929 atomic_set(&data
->wakeup
, 1);
2932 lost_event
.header
.type
= PERF_RECORD_LOST
;
2933 lost_event
.header
.misc
= 0;
2934 lost_event
.header
.size
= sizeof(lost_event
);
2935 lost_event
.id
= event
->id
;
2936 lost_event
.lost
= atomic_xchg(&data
->lost
, 0);
2938 perf_output_put(handle
, lost_event
);
2944 atomic_inc(&data
->lost
);
2945 perf_output_unlock(handle
);
2952 void perf_output_end(struct perf_output_handle
*handle
)
2954 struct perf_event
*event
= handle
->event
;
2955 struct perf_mmap_data
*data
= handle
->data
;
2957 int wakeup_events
= event
->attr
.wakeup_events
;
2959 if (handle
->sample
&& wakeup_events
) {
2960 int events
= atomic_inc_return(&data
->events
);
2961 if (events
>= wakeup_events
) {
2962 atomic_sub(wakeup_events
, &data
->events
);
2963 atomic_set(&data
->wakeup
, 1);
2967 perf_output_unlock(handle
);
2971 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
2974 * only top level events have the pid namespace they were created in
2977 event
= event
->parent
;
2979 return task_tgid_nr_ns(p
, event
->ns
);
2982 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
2985 * only top level events have the pid namespace they were created in
2988 event
= event
->parent
;
2990 return task_pid_nr_ns(p
, event
->ns
);
2993 static void perf_output_read_one(struct perf_output_handle
*handle
,
2994 struct perf_event
*event
)
2996 u64 read_format
= event
->attr
.read_format
;
3000 values
[n
++] = atomic64_read(&event
->count
);
3001 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
3002 values
[n
++] = event
->total_time_enabled
+
3003 atomic64_read(&event
->child_total_time_enabled
);
3005 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
3006 values
[n
++] = event
->total_time_running
+
3007 atomic64_read(&event
->child_total_time_running
);
3009 if (read_format
& PERF_FORMAT_ID
)
3010 values
[n
++] = primary_event_id(event
);
3012 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3016 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3018 static void perf_output_read_group(struct perf_output_handle
*handle
,
3019 struct perf_event
*event
)
3021 struct perf_event
*leader
= event
->group_leader
, *sub
;
3022 u64 read_format
= event
->attr
.read_format
;
3026 values
[n
++] = 1 + leader
->nr_siblings
;
3028 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3029 values
[n
++] = leader
->total_time_enabled
;
3031 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3032 values
[n
++] = leader
->total_time_running
;
3034 if (leader
!= event
)
3035 leader
->pmu
->read(leader
);
3037 values
[n
++] = atomic64_read(&leader
->count
);
3038 if (read_format
& PERF_FORMAT_ID
)
3039 values
[n
++] = primary_event_id(leader
);
3041 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3043 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3047 sub
->pmu
->read(sub
);
3049 values
[n
++] = atomic64_read(&sub
->count
);
3050 if (read_format
& PERF_FORMAT_ID
)
3051 values
[n
++] = primary_event_id(sub
);
3053 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3057 static void perf_output_read(struct perf_output_handle
*handle
,
3058 struct perf_event
*event
)
3060 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
3061 perf_output_read_group(handle
, event
);
3063 perf_output_read_one(handle
, event
);
3066 void perf_output_sample(struct perf_output_handle
*handle
,
3067 struct perf_event_header
*header
,
3068 struct perf_sample_data
*data
,
3069 struct perf_event
*event
)
3071 u64 sample_type
= data
->type
;
3073 perf_output_put(handle
, *header
);
3075 if (sample_type
& PERF_SAMPLE_IP
)
3076 perf_output_put(handle
, data
->ip
);
3078 if (sample_type
& PERF_SAMPLE_TID
)
3079 perf_output_put(handle
, data
->tid_entry
);
3081 if (sample_type
& PERF_SAMPLE_TIME
)
3082 perf_output_put(handle
, data
->time
);
3084 if (sample_type
& PERF_SAMPLE_ADDR
)
3085 perf_output_put(handle
, data
->addr
);
3087 if (sample_type
& PERF_SAMPLE_ID
)
3088 perf_output_put(handle
, data
->id
);
3090 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
3091 perf_output_put(handle
, data
->stream_id
);
3093 if (sample_type
& PERF_SAMPLE_CPU
)
3094 perf_output_put(handle
, data
->cpu_entry
);
3096 if (sample_type
& PERF_SAMPLE_PERIOD
)
3097 perf_output_put(handle
, data
->period
);
3099 if (sample_type
& PERF_SAMPLE_READ
)
3100 perf_output_read(handle
, event
);
3102 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3103 if (data
->callchain
) {
3106 if (data
->callchain
)
3107 size
+= data
->callchain
->nr
;
3109 size
*= sizeof(u64
);
3111 perf_output_copy(handle
, data
->callchain
, size
);
3114 perf_output_put(handle
, nr
);
3118 if (sample_type
& PERF_SAMPLE_RAW
) {
3120 perf_output_put(handle
, data
->raw
->size
);
3121 perf_output_copy(handle
, data
->raw
->data
,
3128 .size
= sizeof(u32
),
3131 perf_output_put(handle
, raw
);
3136 void perf_prepare_sample(struct perf_event_header
*header
,
3137 struct perf_sample_data
*data
,
3138 struct perf_event
*event
,
3139 struct pt_regs
*regs
)
3141 u64 sample_type
= event
->attr
.sample_type
;
3143 data
->type
= sample_type
;
3145 header
->type
= PERF_RECORD_SAMPLE
;
3146 header
->size
= sizeof(*header
);
3149 header
->misc
|= perf_misc_flags(regs
);
3151 if (sample_type
& PERF_SAMPLE_IP
) {
3152 data
->ip
= perf_instruction_pointer(regs
);
3154 header
->size
+= sizeof(data
->ip
);
3157 if (sample_type
& PERF_SAMPLE_TID
) {
3158 /* namespace issues */
3159 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
3160 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
3162 header
->size
+= sizeof(data
->tid_entry
);
3165 if (sample_type
& PERF_SAMPLE_TIME
) {
3166 data
->time
= perf_clock();
3168 header
->size
+= sizeof(data
->time
);
3171 if (sample_type
& PERF_SAMPLE_ADDR
)
3172 header
->size
+= sizeof(data
->addr
);
3174 if (sample_type
& PERF_SAMPLE_ID
) {
3175 data
->id
= primary_event_id(event
);
3177 header
->size
+= sizeof(data
->id
);
3180 if (sample_type
& PERF_SAMPLE_STREAM_ID
) {
3181 data
->stream_id
= event
->id
;
3183 header
->size
+= sizeof(data
->stream_id
);
3186 if (sample_type
& PERF_SAMPLE_CPU
) {
3187 data
->cpu_entry
.cpu
= raw_smp_processor_id();
3188 data
->cpu_entry
.reserved
= 0;
3190 header
->size
+= sizeof(data
->cpu_entry
);
3193 if (sample_type
& PERF_SAMPLE_PERIOD
)
3194 header
->size
+= sizeof(data
->period
);
3196 if (sample_type
& PERF_SAMPLE_READ
)
3197 header
->size
+= perf_event_read_size(event
);
3199 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3202 data
->callchain
= perf_callchain(regs
);
3204 if (data
->callchain
)
3205 size
+= data
->callchain
->nr
;
3207 header
->size
+= size
* sizeof(u64
);
3210 if (sample_type
& PERF_SAMPLE_RAW
) {
3211 int size
= sizeof(u32
);
3214 size
+= data
->raw
->size
;
3216 size
+= sizeof(u32
);
3218 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
3219 header
->size
+= size
;
3223 static void perf_event_output(struct perf_event
*event
, int nmi
,
3224 struct perf_sample_data
*data
,
3225 struct pt_regs
*regs
)
3227 struct perf_output_handle handle
;
3228 struct perf_event_header header
;
3230 perf_prepare_sample(&header
, data
, event
, regs
);
3232 if (perf_output_begin(&handle
, event
, header
.size
, nmi
, 1))
3235 perf_output_sample(&handle
, &header
, data
, event
);
3237 perf_output_end(&handle
);
3244 struct perf_read_event
{
3245 struct perf_event_header header
;
3252 perf_event_read_event(struct perf_event
*event
,
3253 struct task_struct
*task
)
3255 struct perf_output_handle handle
;
3256 struct perf_read_event read_event
= {
3258 .type
= PERF_RECORD_READ
,
3260 .size
= sizeof(read_event
) + perf_event_read_size(event
),
3262 .pid
= perf_event_pid(event
, task
),
3263 .tid
= perf_event_tid(event
, task
),
3267 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
, 0, 0);
3271 perf_output_put(&handle
, read_event
);
3272 perf_output_read(&handle
, event
);
3274 perf_output_end(&handle
);
3278 * task tracking -- fork/exit
3280 * enabled by: attr.comm | attr.mmap | attr.task
3283 struct perf_task_event
{
3284 struct task_struct
*task
;
3285 struct perf_event_context
*task_ctx
;
3288 struct perf_event_header header
;
3298 static void perf_event_task_output(struct perf_event
*event
,
3299 struct perf_task_event
*task_event
)
3301 struct perf_output_handle handle
;
3303 struct task_struct
*task
= task_event
->task
;
3306 size
= task_event
->event_id
.header
.size
;
3307 ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3312 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
3313 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
3315 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
3316 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
3318 perf_output_put(&handle
, task_event
->event_id
);
3320 perf_output_end(&handle
);
3323 static int perf_event_task_match(struct perf_event
*event
)
3325 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3328 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3331 if (event
->attr
.comm
|| event
->attr
.mmap
|| event
->attr
.task
)
3337 static void perf_event_task_ctx(struct perf_event_context
*ctx
,
3338 struct perf_task_event
*task_event
)
3340 struct perf_event
*event
;
3342 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3343 if (perf_event_task_match(event
))
3344 perf_event_task_output(event
, task_event
);
3348 static void perf_event_task_event(struct perf_task_event
*task_event
)
3350 struct perf_cpu_context
*cpuctx
;
3351 struct perf_event_context
*ctx
= task_event
->task_ctx
;
3354 cpuctx
= &get_cpu_var(perf_cpu_context
);
3355 perf_event_task_ctx(&cpuctx
->ctx
, task_event
);
3357 ctx
= rcu_dereference(current
->perf_event_ctxp
);
3359 perf_event_task_ctx(ctx
, task_event
);
3360 put_cpu_var(perf_cpu_context
);
3364 static void perf_event_task(struct task_struct
*task
,
3365 struct perf_event_context
*task_ctx
,
3368 struct perf_task_event task_event
;
3370 if (!atomic_read(&nr_comm_events
) &&
3371 !atomic_read(&nr_mmap_events
) &&
3372 !atomic_read(&nr_task_events
))
3375 task_event
= (struct perf_task_event
){
3377 .task_ctx
= task_ctx
,
3380 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
3382 .size
= sizeof(task_event
.event_id
),
3388 .time
= perf_clock(),
3392 perf_event_task_event(&task_event
);
3395 void perf_event_fork(struct task_struct
*task
)
3397 perf_event_task(task
, NULL
, 1);
3404 struct perf_comm_event
{
3405 struct task_struct
*task
;
3410 struct perf_event_header header
;
3417 static void perf_event_comm_output(struct perf_event
*event
,
3418 struct perf_comm_event
*comm_event
)
3420 struct perf_output_handle handle
;
3421 int size
= comm_event
->event_id
.header
.size
;
3422 int ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3427 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
3428 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
3430 perf_output_put(&handle
, comm_event
->event_id
);
3431 perf_output_copy(&handle
, comm_event
->comm
,
3432 comm_event
->comm_size
);
3433 perf_output_end(&handle
);
3436 static int perf_event_comm_match(struct perf_event
*event
)
3438 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3441 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3444 if (event
->attr
.comm
)
3450 static void perf_event_comm_ctx(struct perf_event_context
*ctx
,
3451 struct perf_comm_event
*comm_event
)
3453 struct perf_event
*event
;
3455 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3456 if (perf_event_comm_match(event
))
3457 perf_event_comm_output(event
, comm_event
);
3461 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
3463 struct perf_cpu_context
*cpuctx
;
3464 struct perf_event_context
*ctx
;
3466 char comm
[TASK_COMM_LEN
];
3468 memset(comm
, 0, sizeof(comm
));
3469 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
3470 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
3472 comm_event
->comm
= comm
;
3473 comm_event
->comm_size
= size
;
3475 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
3478 cpuctx
= &get_cpu_var(perf_cpu_context
);
3479 perf_event_comm_ctx(&cpuctx
->ctx
, comm_event
);
3480 ctx
= rcu_dereference(current
->perf_event_ctxp
);
3482 perf_event_comm_ctx(ctx
, comm_event
);
3483 put_cpu_var(perf_cpu_context
);
3487 void perf_event_comm(struct task_struct
*task
)
3489 struct perf_comm_event comm_event
;
3491 if (task
->perf_event_ctxp
)
3492 perf_event_enable_on_exec(task
);
3494 if (!atomic_read(&nr_comm_events
))
3497 comm_event
= (struct perf_comm_event
){
3503 .type
= PERF_RECORD_COMM
,
3512 perf_event_comm_event(&comm_event
);
3519 struct perf_mmap_event
{
3520 struct vm_area_struct
*vma
;
3522 const char *file_name
;
3526 struct perf_event_header header
;
3536 static void perf_event_mmap_output(struct perf_event
*event
,
3537 struct perf_mmap_event
*mmap_event
)
3539 struct perf_output_handle handle
;
3540 int size
= mmap_event
->event_id
.header
.size
;
3541 int ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3546 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
3547 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
3549 perf_output_put(&handle
, mmap_event
->event_id
);
3550 perf_output_copy(&handle
, mmap_event
->file_name
,
3551 mmap_event
->file_size
);
3552 perf_output_end(&handle
);
3555 static int perf_event_mmap_match(struct perf_event
*event
,
3556 struct perf_mmap_event
*mmap_event
)
3558 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3561 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3564 if (event
->attr
.mmap
)
3570 static void perf_event_mmap_ctx(struct perf_event_context
*ctx
,
3571 struct perf_mmap_event
*mmap_event
)
3573 struct perf_event
*event
;
3575 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3576 if (perf_event_mmap_match(event
, mmap_event
))
3577 perf_event_mmap_output(event
, mmap_event
);
3581 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
3583 struct perf_cpu_context
*cpuctx
;
3584 struct perf_event_context
*ctx
;
3585 struct vm_area_struct
*vma
= mmap_event
->vma
;
3586 struct file
*file
= vma
->vm_file
;
3592 memset(tmp
, 0, sizeof(tmp
));
3596 * d_path works from the end of the buffer backwards, so we
3597 * need to add enough zero bytes after the string to handle
3598 * the 64bit alignment we do later.
3600 buf
= kzalloc(PATH_MAX
+ sizeof(u64
), GFP_KERNEL
);
3602 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
3605 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
3607 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
3611 if (arch_vma_name(mmap_event
->vma
)) {
3612 name
= strncpy(tmp
, arch_vma_name(mmap_event
->vma
),
3618 name
= strncpy(tmp
, "[vdso]", sizeof(tmp
));
3622 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
3627 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
3629 mmap_event
->file_name
= name
;
3630 mmap_event
->file_size
= size
;
3632 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
3635 cpuctx
= &get_cpu_var(perf_cpu_context
);
3636 perf_event_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
3637 ctx
= rcu_dereference(current
->perf_event_ctxp
);
3639 perf_event_mmap_ctx(ctx
, mmap_event
);
3640 put_cpu_var(perf_cpu_context
);
3646 void __perf_event_mmap(struct vm_area_struct
*vma
)
3648 struct perf_mmap_event mmap_event
;
3650 if (!atomic_read(&nr_mmap_events
))
3653 mmap_event
= (struct perf_mmap_event
){
3659 .type
= PERF_RECORD_MMAP
,
3665 .start
= vma
->vm_start
,
3666 .len
= vma
->vm_end
- vma
->vm_start
,
3667 .pgoff
= vma
->vm_pgoff
,
3671 perf_event_mmap_event(&mmap_event
);
3675 * IRQ throttle logging
3678 static void perf_log_throttle(struct perf_event
*event
, int enable
)
3680 struct perf_output_handle handle
;
3684 struct perf_event_header header
;
3688 } throttle_event
= {
3690 .type
= PERF_RECORD_THROTTLE
,
3692 .size
= sizeof(throttle_event
),
3694 .time
= perf_clock(),
3695 .id
= primary_event_id(event
),
3696 .stream_id
= event
->id
,
3700 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
3702 ret
= perf_output_begin(&handle
, event
, sizeof(throttle_event
), 1, 0);
3706 perf_output_put(&handle
, throttle_event
);
3707 perf_output_end(&handle
);
3711 * Generic event overflow handling, sampling.
3714 static int __perf_event_overflow(struct perf_event
*event
, int nmi
,
3715 int throttle
, struct perf_sample_data
*data
,
3716 struct pt_regs
*regs
)
3718 int events
= atomic_read(&event
->event_limit
);
3719 struct hw_perf_event
*hwc
= &event
->hw
;
3722 throttle
= (throttle
&& event
->pmu
->unthrottle
!= NULL
);
3727 if (hwc
->interrupts
!= MAX_INTERRUPTS
) {
3729 if (HZ
* hwc
->interrupts
>
3730 (u64
)sysctl_perf_event_sample_rate
) {
3731 hwc
->interrupts
= MAX_INTERRUPTS
;
3732 perf_log_throttle(event
, 0);
3737 * Keep re-disabling events even though on the previous
3738 * pass we disabled it - just in case we raced with a
3739 * sched-in and the event got enabled again:
3745 if (event
->attr
.freq
) {
3746 u64 now
= perf_clock();
3747 s64 delta
= now
- hwc
->freq_time_stamp
;
3749 hwc
->freq_time_stamp
= now
;
3751 if (delta
> 0 && delta
< 2*TICK_NSEC
)
3752 perf_adjust_period(event
, delta
, hwc
->last_period
);
3756 * XXX event_limit might not quite work as expected on inherited
3760 event
->pending_kill
= POLL_IN
;
3761 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
3763 event
->pending_kill
= POLL_HUP
;
3765 event
->pending_disable
= 1;
3766 perf_pending_queue(&event
->pending
,
3767 perf_pending_event
);
3769 perf_event_disable(event
);
3772 if (event
->overflow_handler
)
3773 event
->overflow_handler(event
, nmi
, data
, regs
);
3775 perf_event_output(event
, nmi
, data
, regs
);
3780 int perf_event_overflow(struct perf_event
*event
, int nmi
,
3781 struct perf_sample_data
*data
,
3782 struct pt_regs
*regs
)
3784 return __perf_event_overflow(event
, nmi
, 1, data
, regs
);
3788 * Generic software event infrastructure
3792 * We directly increment event->count and keep a second value in
3793 * event->hw.period_left to count intervals. This period event
3794 * is kept in the range [-sample_period, 0] so that we can use the
3798 static u64
perf_swevent_set_period(struct perf_event
*event
)
3800 struct hw_perf_event
*hwc
= &event
->hw
;
3801 u64 period
= hwc
->last_period
;
3805 hwc
->last_period
= hwc
->sample_period
;
3808 old
= val
= atomic64_read(&hwc
->period_left
);
3812 nr
= div64_u64(period
+ val
, period
);
3813 offset
= nr
* period
;
3815 if (atomic64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
3821 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
3822 int nmi
, struct perf_sample_data
*data
,
3823 struct pt_regs
*regs
)
3825 struct hw_perf_event
*hwc
= &event
->hw
;
3828 data
->period
= event
->hw
.last_period
;
3830 overflow
= perf_swevent_set_period(event
);
3832 if (hwc
->interrupts
== MAX_INTERRUPTS
)
3835 for (; overflow
; overflow
--) {
3836 if (__perf_event_overflow(event
, nmi
, throttle
,
3839 * We inhibit the overflow from happening when
3840 * hwc->interrupts == MAX_INTERRUPTS.
3848 static void perf_swevent_unthrottle(struct perf_event
*event
)
3851 * Nothing to do, we already reset hwc->interrupts.
3855 static void perf_swevent_add(struct perf_event
*event
, u64 nr
,
3856 int nmi
, struct perf_sample_data
*data
,
3857 struct pt_regs
*regs
)
3859 struct hw_perf_event
*hwc
= &event
->hw
;
3861 atomic64_add(nr
, &event
->count
);
3866 if (!hwc
->sample_period
)
3869 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
3870 return perf_swevent_overflow(event
, 1, nmi
, data
, regs
);
3872 if (atomic64_add_negative(nr
, &hwc
->period_left
))
3875 perf_swevent_overflow(event
, 0, nmi
, data
, regs
);
3878 static int perf_swevent_is_counting(struct perf_event
*event
)
3881 * The event is active, we're good!
3883 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
3887 * The event is off/error, not counting.
3889 if (event
->state
!= PERF_EVENT_STATE_INACTIVE
)
3893 * The event is inactive, if the context is active
3894 * we're part of a group that didn't make it on the 'pmu',
3897 if (event
->ctx
->is_active
)
3901 * We're inactive and the context is too, this means the
3902 * task is scheduled out, we're counting events that happen
3903 * to us, like migration events.
3908 static int perf_tp_event_match(struct perf_event
*event
,
3909 struct perf_sample_data
*data
);
3911 static int perf_exclude_event(struct perf_event
*event
,
3912 struct pt_regs
*regs
)
3915 if (event
->attr
.exclude_user
&& user_mode(regs
))
3918 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
3925 static int perf_swevent_match(struct perf_event
*event
,
3926 enum perf_type_id type
,
3928 struct perf_sample_data
*data
,
3929 struct pt_regs
*regs
)
3931 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3934 if (!perf_swevent_is_counting(event
))
3937 if (event
->attr
.type
!= type
)
3940 if (event
->attr
.config
!= event_id
)
3943 if (perf_exclude_event(event
, regs
))
3946 if (event
->attr
.type
== PERF_TYPE_TRACEPOINT
&&
3947 !perf_tp_event_match(event
, data
))
3953 static void perf_swevent_ctx_event(struct perf_event_context
*ctx
,
3954 enum perf_type_id type
,
3955 u32 event_id
, u64 nr
, int nmi
,
3956 struct perf_sample_data
*data
,
3957 struct pt_regs
*regs
)
3959 struct perf_event
*event
;
3961 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3962 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
3963 perf_swevent_add(event
, nr
, nmi
, data
, regs
);
3967 int perf_swevent_get_recursion_context(void)
3969 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
3976 else if (in_softirq())
3981 if (cpuctx
->recursion
[rctx
]) {
3982 put_cpu_var(perf_cpu_context
);
3986 cpuctx
->recursion
[rctx
]++;
3991 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
3993 void perf_swevent_put_recursion_context(int rctx
)
3995 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
3997 cpuctx
->recursion
[rctx
]--;
3998 put_cpu_var(perf_cpu_context
);
4000 EXPORT_SYMBOL_GPL(perf_swevent_put_recursion_context
);
4002 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
4004 struct perf_sample_data
*data
,
4005 struct pt_regs
*regs
)
4007 struct perf_cpu_context
*cpuctx
;
4008 struct perf_event_context
*ctx
;
4010 cpuctx
= &__get_cpu_var(perf_cpu_context
);
4012 perf_swevent_ctx_event(&cpuctx
->ctx
, type
, event_id
,
4013 nr
, nmi
, data
, regs
);
4015 * doesn't really matter which of the child contexts the
4016 * events ends up in.
4018 ctx
= rcu_dereference(current
->perf_event_ctxp
);
4020 perf_swevent_ctx_event(ctx
, type
, event_id
, nr
, nmi
, data
, regs
);
4024 void __perf_sw_event(u32 event_id
, u64 nr
, int nmi
,
4025 struct pt_regs
*regs
, u64 addr
)
4027 struct perf_sample_data data
;
4030 rctx
= perf_swevent_get_recursion_context();
4034 perf_sample_data_init(&data
, addr
);
4036 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, nmi
, &data
, regs
);
4038 perf_swevent_put_recursion_context(rctx
);
4041 static void perf_swevent_read(struct perf_event
*event
)
4045 static int perf_swevent_enable(struct perf_event
*event
)
4047 struct hw_perf_event
*hwc
= &event
->hw
;
4049 if (hwc
->sample_period
) {
4050 hwc
->last_period
= hwc
->sample_period
;
4051 perf_swevent_set_period(event
);
4056 static void perf_swevent_disable(struct perf_event
*event
)
4060 static const struct pmu perf_ops_generic
= {
4061 .enable
= perf_swevent_enable
,
4062 .disable
= perf_swevent_disable
,
4063 .read
= perf_swevent_read
,
4064 .unthrottle
= perf_swevent_unthrottle
,
4068 * hrtimer based swevent callback
4071 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
4073 enum hrtimer_restart ret
= HRTIMER_RESTART
;
4074 struct perf_sample_data data
;
4075 struct pt_regs
*regs
;
4076 struct perf_event
*event
;
4079 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
4080 event
->pmu
->read(event
);
4082 perf_sample_data_init(&data
, 0);
4083 data
.period
= event
->hw
.last_period
;
4084 regs
= get_irq_regs();
4086 * In case we exclude kernel IPs or are somehow not in interrupt
4087 * context, provide the next best thing, the user IP.
4089 if ((event
->attr
.exclude_kernel
|| !regs
) &&
4090 !event
->attr
.exclude_user
)
4091 regs
= task_pt_regs(current
);
4094 if (!(event
->attr
.exclude_idle
&& current
->pid
== 0))
4095 if (perf_event_overflow(event
, 0, &data
, regs
))
4096 ret
= HRTIMER_NORESTART
;
4099 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
4100 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
4105 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
4107 struct hw_perf_event
*hwc
= &event
->hw
;
4109 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
4110 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
4111 if (hwc
->sample_period
) {
4114 if (hwc
->remaining
) {
4115 if (hwc
->remaining
< 0)
4118 period
= hwc
->remaining
;
4121 period
= max_t(u64
, 10000, hwc
->sample_period
);
4123 __hrtimer_start_range_ns(&hwc
->hrtimer
,
4124 ns_to_ktime(period
), 0,
4125 HRTIMER_MODE_REL
, 0);
4129 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
4131 struct hw_perf_event
*hwc
= &event
->hw
;
4133 if (hwc
->sample_period
) {
4134 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
4135 hwc
->remaining
= ktime_to_ns(remaining
);
4137 hrtimer_cancel(&hwc
->hrtimer
);
4142 * Software event: cpu wall time clock
4145 static void cpu_clock_perf_event_update(struct perf_event
*event
)
4147 int cpu
= raw_smp_processor_id();
4151 now
= cpu_clock(cpu
);
4152 prev
= atomic64_xchg(&event
->hw
.prev_count
, now
);
4153 atomic64_add(now
- prev
, &event
->count
);
4156 static int cpu_clock_perf_event_enable(struct perf_event
*event
)
4158 struct hw_perf_event
*hwc
= &event
->hw
;
4159 int cpu
= raw_smp_processor_id();
4161 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
4162 perf_swevent_start_hrtimer(event
);
4167 static void cpu_clock_perf_event_disable(struct perf_event
*event
)
4169 perf_swevent_cancel_hrtimer(event
);
4170 cpu_clock_perf_event_update(event
);
4173 static void cpu_clock_perf_event_read(struct perf_event
*event
)
4175 cpu_clock_perf_event_update(event
);
4178 static const struct pmu perf_ops_cpu_clock
= {
4179 .enable
= cpu_clock_perf_event_enable
,
4180 .disable
= cpu_clock_perf_event_disable
,
4181 .read
= cpu_clock_perf_event_read
,
4185 * Software event: task time clock
4188 static void task_clock_perf_event_update(struct perf_event
*event
, u64 now
)
4193 prev
= atomic64_xchg(&event
->hw
.prev_count
, now
);
4195 atomic64_add(delta
, &event
->count
);
4198 static int task_clock_perf_event_enable(struct perf_event
*event
)
4200 struct hw_perf_event
*hwc
= &event
->hw
;
4203 now
= event
->ctx
->time
;
4205 atomic64_set(&hwc
->prev_count
, now
);
4207 perf_swevent_start_hrtimer(event
);
4212 static void task_clock_perf_event_disable(struct perf_event
*event
)
4214 perf_swevent_cancel_hrtimer(event
);
4215 task_clock_perf_event_update(event
, event
->ctx
->time
);
4219 static void task_clock_perf_event_read(struct perf_event
*event
)
4224 update_context_time(event
->ctx
);
4225 time
= event
->ctx
->time
;
4227 u64 now
= perf_clock();
4228 u64 delta
= now
- event
->ctx
->timestamp
;
4229 time
= event
->ctx
->time
+ delta
;
4232 task_clock_perf_event_update(event
, time
);
4235 static const struct pmu perf_ops_task_clock
= {
4236 .enable
= task_clock_perf_event_enable
,
4237 .disable
= task_clock_perf_event_disable
,
4238 .read
= task_clock_perf_event_read
,
4241 #ifdef CONFIG_EVENT_PROFILE
4243 void perf_tp_event(int event_id
, u64 addr
, u64 count
, void *record
,
4246 struct pt_regs
*regs
= get_irq_regs();
4247 struct perf_sample_data data
;
4248 struct perf_raw_record raw
= {
4253 perf_sample_data_init(&data
, addr
);
4257 regs
= task_pt_regs(current
);
4259 /* Trace events already protected against recursion */
4260 do_perf_sw_event(PERF_TYPE_TRACEPOINT
, event_id
, count
, 1,
4263 EXPORT_SYMBOL_GPL(perf_tp_event
);
4265 static int perf_tp_event_match(struct perf_event
*event
,
4266 struct perf_sample_data
*data
)
4268 void *record
= data
->raw
->data
;
4270 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
4275 static void tp_perf_event_destroy(struct perf_event
*event
)
4277 ftrace_profile_disable(event
->attr
.config
);
4280 static const struct pmu
*tp_perf_event_init(struct perf_event
*event
)
4283 * Raw tracepoint data is a severe data leak, only allow root to
4286 if ((event
->attr
.sample_type
& PERF_SAMPLE_RAW
) &&
4287 perf_paranoid_tracepoint_raw() &&
4288 !capable(CAP_SYS_ADMIN
))
4289 return ERR_PTR(-EPERM
);
4291 if (ftrace_profile_enable(event
->attr
.config
))
4294 event
->destroy
= tp_perf_event_destroy
;
4296 return &perf_ops_generic
;
4299 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
4304 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
4307 filter_str
= strndup_user(arg
, PAGE_SIZE
);
4308 if (IS_ERR(filter_str
))
4309 return PTR_ERR(filter_str
);
4311 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
, filter_str
);
4317 static void perf_event_free_filter(struct perf_event
*event
)
4319 ftrace_profile_free_filter(event
);
4324 static int perf_tp_event_match(struct perf_event
*event
,
4325 struct perf_sample_data
*data
)
4330 static const struct pmu
*tp_perf_event_init(struct perf_event
*event
)
4335 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
4340 static void perf_event_free_filter(struct perf_event
*event
)
4344 #endif /* CONFIG_EVENT_PROFILE */
4346 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4347 static void bp_perf_event_destroy(struct perf_event
*event
)
4349 release_bp_slot(event
);
4352 static const struct pmu
*bp_perf_event_init(struct perf_event
*bp
)
4356 err
= register_perf_hw_breakpoint(bp
);
4358 return ERR_PTR(err
);
4360 bp
->destroy
= bp_perf_event_destroy
;
4362 return &perf_ops_bp
;
4365 void perf_bp_event(struct perf_event
*bp
, void *data
)
4367 struct perf_sample_data sample
;
4368 struct pt_regs
*regs
= data
;
4370 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
);
4372 if (!perf_exclude_event(bp
, regs
))
4373 perf_swevent_add(bp
, 1, 1, &sample
, regs
);
4376 static const struct pmu
*bp_perf_event_init(struct perf_event
*bp
)
4381 void perf_bp_event(struct perf_event
*bp
, void *regs
)
4386 atomic_t perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
4388 static void sw_perf_event_destroy(struct perf_event
*event
)
4390 u64 event_id
= event
->attr
.config
;
4392 WARN_ON(event
->parent
);
4394 atomic_dec(&perf_swevent_enabled
[event_id
]);
4397 static const struct pmu
*sw_perf_event_init(struct perf_event
*event
)
4399 const struct pmu
*pmu
= NULL
;
4400 u64 event_id
= event
->attr
.config
;
4403 * Software events (currently) can't in general distinguish
4404 * between user, kernel and hypervisor events.
4405 * However, context switches and cpu migrations are considered
4406 * to be kernel events, and page faults are never hypervisor
4410 case PERF_COUNT_SW_CPU_CLOCK
:
4411 pmu
= &perf_ops_cpu_clock
;
4414 case PERF_COUNT_SW_TASK_CLOCK
:
4416 * If the user instantiates this as a per-cpu event,
4417 * use the cpu_clock event instead.
4419 if (event
->ctx
->task
)
4420 pmu
= &perf_ops_task_clock
;
4422 pmu
= &perf_ops_cpu_clock
;
4425 case PERF_COUNT_SW_PAGE_FAULTS
:
4426 case PERF_COUNT_SW_PAGE_FAULTS_MIN
:
4427 case PERF_COUNT_SW_PAGE_FAULTS_MAJ
:
4428 case PERF_COUNT_SW_CONTEXT_SWITCHES
:
4429 case PERF_COUNT_SW_CPU_MIGRATIONS
:
4430 case PERF_COUNT_SW_ALIGNMENT_FAULTS
:
4431 case PERF_COUNT_SW_EMULATION_FAULTS
:
4432 if (!event
->parent
) {
4433 atomic_inc(&perf_swevent_enabled
[event_id
]);
4434 event
->destroy
= sw_perf_event_destroy
;
4436 pmu
= &perf_ops_generic
;
4444 * Allocate and initialize a event structure
4446 static struct perf_event
*
4447 perf_event_alloc(struct perf_event_attr
*attr
,
4449 struct perf_event_context
*ctx
,
4450 struct perf_event
*group_leader
,
4451 struct perf_event
*parent_event
,
4452 perf_overflow_handler_t overflow_handler
,
4455 const struct pmu
*pmu
;
4456 struct perf_event
*event
;
4457 struct hw_perf_event
*hwc
;
4460 event
= kzalloc(sizeof(*event
), gfpflags
);
4462 return ERR_PTR(-ENOMEM
);
4465 * Single events are their own group leaders, with an
4466 * empty sibling list:
4469 group_leader
= event
;
4471 mutex_init(&event
->child_mutex
);
4472 INIT_LIST_HEAD(&event
->child_list
);
4474 INIT_LIST_HEAD(&event
->group_entry
);
4475 INIT_LIST_HEAD(&event
->event_entry
);
4476 INIT_LIST_HEAD(&event
->sibling_list
);
4477 init_waitqueue_head(&event
->waitq
);
4479 mutex_init(&event
->mmap_mutex
);
4482 event
->attr
= *attr
;
4483 event
->group_leader
= group_leader
;
4488 event
->parent
= parent_event
;
4490 event
->ns
= get_pid_ns(current
->nsproxy
->pid_ns
);
4491 event
->id
= atomic64_inc_return(&perf_event_id
);
4493 event
->state
= PERF_EVENT_STATE_INACTIVE
;
4495 if (!overflow_handler
&& parent_event
)
4496 overflow_handler
= parent_event
->overflow_handler
;
4498 event
->overflow_handler
= overflow_handler
;
4501 event
->state
= PERF_EVENT_STATE_OFF
;
4506 hwc
->sample_period
= attr
->sample_period
;
4507 if (attr
->freq
&& attr
->sample_freq
)
4508 hwc
->sample_period
= 1;
4509 hwc
->last_period
= hwc
->sample_period
;
4511 atomic64_set(&hwc
->period_left
, hwc
->sample_period
);
4514 * we currently do not support PERF_FORMAT_GROUP on inherited events
4516 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
4519 switch (attr
->type
) {
4521 case PERF_TYPE_HARDWARE
:
4522 case PERF_TYPE_HW_CACHE
:
4523 pmu
= hw_perf_event_init(event
);
4526 case PERF_TYPE_SOFTWARE
:
4527 pmu
= sw_perf_event_init(event
);
4530 case PERF_TYPE_TRACEPOINT
:
4531 pmu
= tp_perf_event_init(event
);
4534 case PERF_TYPE_BREAKPOINT
:
4535 pmu
= bp_perf_event_init(event
);
4546 else if (IS_ERR(pmu
))
4551 put_pid_ns(event
->ns
);
4553 return ERR_PTR(err
);
4558 if (!event
->parent
) {
4559 atomic_inc(&nr_events
);
4560 if (event
->attr
.mmap
)
4561 atomic_inc(&nr_mmap_events
);
4562 if (event
->attr
.comm
)
4563 atomic_inc(&nr_comm_events
);
4564 if (event
->attr
.task
)
4565 atomic_inc(&nr_task_events
);
4571 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
4572 struct perf_event_attr
*attr
)
4577 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
4581 * zero the full structure, so that a short copy will be nice.
4583 memset(attr
, 0, sizeof(*attr
));
4585 ret
= get_user(size
, &uattr
->size
);
4589 if (size
> PAGE_SIZE
) /* silly large */
4592 if (!size
) /* abi compat */
4593 size
= PERF_ATTR_SIZE_VER0
;
4595 if (size
< PERF_ATTR_SIZE_VER0
)
4599 * If we're handed a bigger struct than we know of,
4600 * ensure all the unknown bits are 0 - i.e. new
4601 * user-space does not rely on any kernel feature
4602 * extensions we dont know about yet.
4604 if (size
> sizeof(*attr
)) {
4605 unsigned char __user
*addr
;
4606 unsigned char __user
*end
;
4609 addr
= (void __user
*)uattr
+ sizeof(*attr
);
4610 end
= (void __user
*)uattr
+ size
;
4612 for (; addr
< end
; addr
++) {
4613 ret
= get_user(val
, addr
);
4619 size
= sizeof(*attr
);
4622 ret
= copy_from_user(attr
, uattr
, size
);
4627 * If the type exists, the corresponding creation will verify
4630 if (attr
->type
>= PERF_TYPE_MAX
)
4633 if (attr
->__reserved_1
)
4636 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
4639 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
4646 put_user(sizeof(*attr
), &uattr
->size
);
4651 static int perf_event_set_output(struct perf_event
*event
, int output_fd
)
4653 struct perf_event
*output_event
= NULL
;
4654 struct file
*output_file
= NULL
;
4655 struct perf_event
*old_output
;
4656 int fput_needed
= 0;
4662 output_file
= fget_light(output_fd
, &fput_needed
);
4666 if (output_file
->f_op
!= &perf_fops
)
4669 output_event
= output_file
->private_data
;
4671 /* Don't chain output fds */
4672 if (output_event
->output
)
4675 /* Don't set an output fd when we already have an output channel */
4679 atomic_long_inc(&output_file
->f_count
);
4682 mutex_lock(&event
->mmap_mutex
);
4683 old_output
= event
->output
;
4684 rcu_assign_pointer(event
->output
, output_event
);
4685 mutex_unlock(&event
->mmap_mutex
);
4689 * we need to make sure no existing perf_output_*()
4690 * is still referencing this event.
4693 fput(old_output
->filp
);
4698 fput_light(output_file
, fput_needed
);
4703 * sys_perf_event_open - open a performance event, associate it to a task/cpu
4705 * @attr_uptr: event_id type attributes for monitoring/sampling
4708 * @group_fd: group leader event fd
4710 SYSCALL_DEFINE5(perf_event_open
,
4711 struct perf_event_attr __user
*, attr_uptr
,
4712 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
4714 struct perf_event
*event
, *group_leader
;
4715 struct perf_event_attr attr
;
4716 struct perf_event_context
*ctx
;
4717 struct file
*event_file
= NULL
;
4718 struct file
*group_file
= NULL
;
4720 int fput_needed
= 0;
4723 /* for future expandability... */
4724 if (flags
& ~(PERF_FLAG_FD_NO_GROUP
| PERF_FLAG_FD_OUTPUT
))
4727 err
= perf_copy_attr(attr_uptr
, &attr
);
4731 if (!attr
.exclude_kernel
) {
4732 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
4737 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
4741 event_fd
= get_unused_fd_flags(O_RDWR
);
4746 * Get the target context (task or percpu):
4748 ctx
= find_get_context(pid
, cpu
);
4755 * Look up the group leader (we will attach this event to it):
4757 group_leader
= NULL
;
4758 if (group_fd
!= -1 && !(flags
& PERF_FLAG_FD_NO_GROUP
)) {
4760 group_file
= fget_light(group_fd
, &fput_needed
);
4762 goto err_put_context
;
4763 if (group_file
->f_op
!= &perf_fops
)
4764 goto err_put_context
;
4766 group_leader
= group_file
->private_data
;
4768 * Do not allow a recursive hierarchy (this new sibling
4769 * becoming part of another group-sibling):
4771 if (group_leader
->group_leader
!= group_leader
)
4772 goto err_put_context
;
4774 * Do not allow to attach to a group in a different
4775 * task or CPU context:
4777 if (group_leader
->ctx
!= ctx
)
4778 goto err_put_context
;
4780 * Only a group leader can be exclusive or pinned
4782 if (attr
.exclusive
|| attr
.pinned
)
4783 goto err_put_context
;
4786 event
= perf_event_alloc(&attr
, cpu
, ctx
, group_leader
,
4787 NULL
, NULL
, GFP_KERNEL
);
4788 err
= PTR_ERR(event
);
4790 goto err_put_context
;
4792 event_file
= anon_inode_getfile("[perf_event]", &perf_fops
, event
, O_RDWR
);
4793 if (IS_ERR(event_file
)) {
4794 err
= PTR_ERR(event_file
);
4795 goto err_free_put_context
;
4798 if (flags
& PERF_FLAG_FD_OUTPUT
) {
4799 err
= perf_event_set_output(event
, group_fd
);
4801 goto err_fput_free_put_context
;
4804 event
->filp
= event_file
;
4805 WARN_ON_ONCE(ctx
->parent_ctx
);
4806 mutex_lock(&ctx
->mutex
);
4807 perf_install_in_context(ctx
, event
, cpu
);
4809 mutex_unlock(&ctx
->mutex
);
4811 event
->owner
= current
;
4812 get_task_struct(current
);
4813 mutex_lock(¤t
->perf_event_mutex
);
4814 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
4815 mutex_unlock(¤t
->perf_event_mutex
);
4817 fput_light(group_file
, fput_needed
);
4818 fd_install(event_fd
, event_file
);
4821 err_fput_free_put_context
:
4823 err_free_put_context
:
4826 fput_light(group_file
, fput_needed
);
4829 put_unused_fd(event_fd
);
4834 * perf_event_create_kernel_counter
4836 * @attr: attributes of the counter to create
4837 * @cpu: cpu in which the counter is bound
4838 * @pid: task to profile
4841 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
4843 perf_overflow_handler_t overflow_handler
)
4845 struct perf_event
*event
;
4846 struct perf_event_context
*ctx
;
4850 * Get the target context (task or percpu):
4853 ctx
= find_get_context(pid
, cpu
);
4859 event
= perf_event_alloc(attr
, cpu
, ctx
, NULL
,
4860 NULL
, overflow_handler
, GFP_KERNEL
);
4861 if (IS_ERR(event
)) {
4862 err
= PTR_ERR(event
);
4863 goto err_put_context
;
4867 WARN_ON_ONCE(ctx
->parent_ctx
);
4868 mutex_lock(&ctx
->mutex
);
4869 perf_install_in_context(ctx
, event
, cpu
);
4871 mutex_unlock(&ctx
->mutex
);
4873 event
->owner
= current
;
4874 get_task_struct(current
);
4875 mutex_lock(¤t
->perf_event_mutex
);
4876 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
4877 mutex_unlock(¤t
->perf_event_mutex
);
4884 return ERR_PTR(err
);
4886 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
4889 * inherit a event from parent task to child task:
4891 static struct perf_event
*
4892 inherit_event(struct perf_event
*parent_event
,
4893 struct task_struct
*parent
,
4894 struct perf_event_context
*parent_ctx
,
4895 struct task_struct
*child
,
4896 struct perf_event
*group_leader
,
4897 struct perf_event_context
*child_ctx
)
4899 struct perf_event
*child_event
;
4902 * Instead of creating recursive hierarchies of events,
4903 * we link inherited events back to the original parent,
4904 * which has a filp for sure, which we use as the reference
4907 if (parent_event
->parent
)
4908 parent_event
= parent_event
->parent
;
4910 child_event
= perf_event_alloc(&parent_event
->attr
,
4911 parent_event
->cpu
, child_ctx
,
4912 group_leader
, parent_event
,
4914 if (IS_ERR(child_event
))
4919 * Make the child state follow the state of the parent event,
4920 * not its attr.disabled bit. We hold the parent's mutex,
4921 * so we won't race with perf_event_{en, dis}able_family.
4923 if (parent_event
->state
>= PERF_EVENT_STATE_INACTIVE
)
4924 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
4926 child_event
->state
= PERF_EVENT_STATE_OFF
;
4928 if (parent_event
->attr
.freq
)
4929 child_event
->hw
.sample_period
= parent_event
->hw
.sample_period
;
4931 child_event
->overflow_handler
= parent_event
->overflow_handler
;
4934 * Link it up in the child's context:
4936 add_event_to_ctx(child_event
, child_ctx
);
4939 * Get a reference to the parent filp - we will fput it
4940 * when the child event exits. This is safe to do because
4941 * we are in the parent and we know that the filp still
4942 * exists and has a nonzero count:
4944 atomic_long_inc(&parent_event
->filp
->f_count
);
4947 * Link this into the parent event's child list
4949 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
4950 mutex_lock(&parent_event
->child_mutex
);
4951 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
4952 mutex_unlock(&parent_event
->child_mutex
);
4957 static int inherit_group(struct perf_event
*parent_event
,
4958 struct task_struct
*parent
,
4959 struct perf_event_context
*parent_ctx
,
4960 struct task_struct
*child
,
4961 struct perf_event_context
*child_ctx
)
4963 struct perf_event
*leader
;
4964 struct perf_event
*sub
;
4965 struct perf_event
*child_ctr
;
4967 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
4968 child
, NULL
, child_ctx
);
4970 return PTR_ERR(leader
);
4971 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
4972 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
4973 child
, leader
, child_ctx
);
4974 if (IS_ERR(child_ctr
))
4975 return PTR_ERR(child_ctr
);
4980 static void sync_child_event(struct perf_event
*child_event
,
4981 struct task_struct
*child
)
4983 struct perf_event
*parent_event
= child_event
->parent
;
4986 if (child_event
->attr
.inherit_stat
)
4987 perf_event_read_event(child_event
, child
);
4989 child_val
= atomic64_read(&child_event
->count
);
4992 * Add back the child's count to the parent's count:
4994 atomic64_add(child_val
, &parent_event
->count
);
4995 atomic64_add(child_event
->total_time_enabled
,
4996 &parent_event
->child_total_time_enabled
);
4997 atomic64_add(child_event
->total_time_running
,
4998 &parent_event
->child_total_time_running
);
5001 * Remove this event from the parent's list
5003 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
5004 mutex_lock(&parent_event
->child_mutex
);
5005 list_del_init(&child_event
->child_list
);
5006 mutex_unlock(&parent_event
->child_mutex
);
5009 * Release the parent event, if this was the last
5012 fput(parent_event
->filp
);
5016 __perf_event_exit_task(struct perf_event
*child_event
,
5017 struct perf_event_context
*child_ctx
,
5018 struct task_struct
*child
)
5020 struct perf_event
*parent_event
;
5022 perf_event_remove_from_context(child_event
);
5024 parent_event
= child_event
->parent
;
5026 * It can happen that parent exits first, and has events
5027 * that are still around due to the child reference. These
5028 * events need to be zapped - but otherwise linger.
5031 sync_child_event(child_event
, child
);
5032 free_event(child_event
);
5037 * When a child task exits, feed back event values to parent events.
5039 void perf_event_exit_task(struct task_struct
*child
)
5041 struct perf_event
*child_event
, *tmp
;
5042 struct perf_event_context
*child_ctx
;
5043 unsigned long flags
;
5045 if (likely(!child
->perf_event_ctxp
)) {
5046 perf_event_task(child
, NULL
, 0);
5050 local_irq_save(flags
);
5052 * We can't reschedule here because interrupts are disabled,
5053 * and either child is current or it is a task that can't be
5054 * scheduled, so we are now safe from rescheduling changing
5057 child_ctx
= child
->perf_event_ctxp
;
5058 __perf_event_task_sched_out(child_ctx
);
5061 * Take the context lock here so that if find_get_context is
5062 * reading child->perf_event_ctxp, we wait until it has
5063 * incremented the context's refcount before we do put_ctx below.
5065 raw_spin_lock(&child_ctx
->lock
);
5066 child
->perf_event_ctxp
= NULL
;
5068 * If this context is a clone; unclone it so it can't get
5069 * swapped to another process while we're removing all
5070 * the events from it.
5072 unclone_ctx(child_ctx
);
5073 update_context_time(child_ctx
);
5074 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
5077 * Report the task dead after unscheduling the events so that we
5078 * won't get any samples after PERF_RECORD_EXIT. We can however still
5079 * get a few PERF_RECORD_READ events.
5081 perf_event_task(child
, child_ctx
, 0);
5084 * We can recurse on the same lock type through:
5086 * __perf_event_exit_task()
5087 * sync_child_event()
5088 * fput(parent_event->filp)
5090 * mutex_lock(&ctx->mutex)
5092 * But since its the parent context it won't be the same instance.
5094 mutex_lock_nested(&child_ctx
->mutex
, SINGLE_DEPTH_NESTING
);
5097 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->group_list
,
5099 __perf_event_exit_task(child_event
, child_ctx
, child
);
5102 * If the last event was a group event, it will have appended all
5103 * its siblings to the list, but we obtained 'tmp' before that which
5104 * will still point to the list head terminating the iteration.
5106 if (!list_empty(&child_ctx
->group_list
))
5109 mutex_unlock(&child_ctx
->mutex
);
5115 * free an unexposed, unused context as created by inheritance by
5116 * init_task below, used by fork() in case of fail.
5118 void perf_event_free_task(struct task_struct
*task
)
5120 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
5121 struct perf_event
*event
, *tmp
;
5126 mutex_lock(&ctx
->mutex
);
5128 list_for_each_entry_safe(event
, tmp
, &ctx
->group_list
, group_entry
) {
5129 struct perf_event
*parent
= event
->parent
;
5131 if (WARN_ON_ONCE(!parent
))
5134 mutex_lock(&parent
->child_mutex
);
5135 list_del_init(&event
->child_list
);
5136 mutex_unlock(&parent
->child_mutex
);
5140 list_del_event(event
, ctx
);
5144 if (!list_empty(&ctx
->group_list
))
5147 mutex_unlock(&ctx
->mutex
);
5153 * Initialize the perf_event context in task_struct
5155 int perf_event_init_task(struct task_struct
*child
)
5157 struct perf_event_context
*child_ctx
= NULL
, *parent_ctx
;
5158 struct perf_event_context
*cloned_ctx
;
5159 struct perf_event
*event
;
5160 struct task_struct
*parent
= current
;
5161 int inherited_all
= 1;
5164 child
->perf_event_ctxp
= NULL
;
5166 mutex_init(&child
->perf_event_mutex
);
5167 INIT_LIST_HEAD(&child
->perf_event_list
);
5169 if (likely(!parent
->perf_event_ctxp
))
5173 * If the parent's context is a clone, pin it so it won't get
5176 parent_ctx
= perf_pin_task_context(parent
);
5179 * No need to check if parent_ctx != NULL here; since we saw
5180 * it non-NULL earlier, the only reason for it to become NULL
5181 * is if we exit, and since we're currently in the middle of
5182 * a fork we can't be exiting at the same time.
5186 * Lock the parent list. No need to lock the child - not PID
5187 * hashed yet and not running, so nobody can access it.
5189 mutex_lock(&parent_ctx
->mutex
);
5192 * We dont have to disable NMIs - we are only looking at
5193 * the list, not manipulating it:
5195 list_for_each_entry(event
, &parent_ctx
->group_list
, group_entry
) {
5197 if (!event
->attr
.inherit
) {
5202 if (!child
->perf_event_ctxp
) {
5204 * This is executed from the parent task context, so
5205 * inherit events that have been marked for cloning.
5206 * First allocate and initialize a context for the
5210 child_ctx
= kzalloc(sizeof(struct perf_event_context
),
5217 __perf_event_init_context(child_ctx
, child
);
5218 child
->perf_event_ctxp
= child_ctx
;
5219 get_task_struct(child
);
5222 ret
= inherit_group(event
, parent
, parent_ctx
,
5230 if (child_ctx
&& inherited_all
) {
5232 * Mark the child context as a clone of the parent
5233 * context, or of whatever the parent is a clone of.
5234 * Note that if the parent is a clone, it could get
5235 * uncloned at any point, but that doesn't matter
5236 * because the list of events and the generation
5237 * count can't have changed since we took the mutex.
5239 cloned_ctx
= rcu_dereference(parent_ctx
->parent_ctx
);
5241 child_ctx
->parent_ctx
= cloned_ctx
;
5242 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
5244 child_ctx
->parent_ctx
= parent_ctx
;
5245 child_ctx
->parent_gen
= parent_ctx
->generation
;
5247 get_ctx(child_ctx
->parent_ctx
);
5250 mutex_unlock(&parent_ctx
->mutex
);
5252 perf_unpin_context(parent_ctx
);
5257 static void __init
perf_event_init_all_cpus(void)
5260 struct perf_cpu_context
*cpuctx
;
5262 for_each_possible_cpu(cpu
) {
5263 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5264 __perf_event_init_context(&cpuctx
->ctx
, NULL
);
5268 static void __cpuinit
perf_event_init_cpu(int cpu
)
5270 struct perf_cpu_context
*cpuctx
;
5272 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5274 spin_lock(&perf_resource_lock
);
5275 cpuctx
->max_pertask
= perf_max_events
- perf_reserved_percpu
;
5276 spin_unlock(&perf_resource_lock
);
5278 hw_perf_event_setup(cpu
);
5281 #ifdef CONFIG_HOTPLUG_CPU
5282 static void __perf_event_exit_cpu(void *info
)
5284 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
5285 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
5286 struct perf_event
*event
, *tmp
;
5288 list_for_each_entry_safe(event
, tmp
, &ctx
->group_list
, group_entry
)
5289 __perf_event_remove_from_context(event
);
5291 static void perf_event_exit_cpu(int cpu
)
5293 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5294 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
5296 mutex_lock(&ctx
->mutex
);
5297 smp_call_function_single(cpu
, __perf_event_exit_cpu
, NULL
, 1);
5298 mutex_unlock(&ctx
->mutex
);
5301 static inline void perf_event_exit_cpu(int cpu
) { }
5304 static int __cpuinit
5305 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
5307 unsigned int cpu
= (long)hcpu
;
5311 case CPU_UP_PREPARE
:
5312 case CPU_UP_PREPARE_FROZEN
:
5313 perf_event_init_cpu(cpu
);
5317 case CPU_ONLINE_FROZEN
:
5318 hw_perf_event_setup_online(cpu
);
5321 case CPU_DOWN_PREPARE
:
5322 case CPU_DOWN_PREPARE_FROZEN
:
5323 perf_event_exit_cpu(cpu
);
5334 * This has to have a higher priority than migration_notifier in sched.c.
5336 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
5337 .notifier_call
= perf_cpu_notify
,
5341 void __init
perf_event_init(void)
5343 perf_event_init_all_cpus();
5344 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
5345 (void *)(long)smp_processor_id());
5346 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_ONLINE
,
5347 (void *)(long)smp_processor_id());
5348 register_cpu_notifier(&perf_cpu_nb
);
5351 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
5353 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
5357 perf_set_reserve_percpu(struct sysdev_class
*class,
5361 struct perf_cpu_context
*cpuctx
;
5365 err
= strict_strtoul(buf
, 10, &val
);
5368 if (val
> perf_max_events
)
5371 spin_lock(&perf_resource_lock
);
5372 perf_reserved_percpu
= val
;
5373 for_each_online_cpu(cpu
) {
5374 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5375 raw_spin_lock_irq(&cpuctx
->ctx
.lock
);
5376 mpt
= min(perf_max_events
- cpuctx
->ctx
.nr_events
,
5377 perf_max_events
- perf_reserved_percpu
);
5378 cpuctx
->max_pertask
= mpt
;
5379 raw_spin_unlock_irq(&cpuctx
->ctx
.lock
);
5381 spin_unlock(&perf_resource_lock
);
5386 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
5388 return sprintf(buf
, "%d\n", perf_overcommit
);
5392 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
5397 err
= strict_strtoul(buf
, 10, &val
);
5403 spin_lock(&perf_resource_lock
);
5404 perf_overcommit
= val
;
5405 spin_unlock(&perf_resource_lock
);
5410 static SYSDEV_CLASS_ATTR(
5413 perf_show_reserve_percpu
,
5414 perf_set_reserve_percpu
5417 static SYSDEV_CLASS_ATTR(
5420 perf_show_overcommit
,
5424 static struct attribute
*perfclass_attrs
[] = {
5425 &attr_reserve_percpu
.attr
,
5426 &attr_overcommit
.attr
,
5430 static struct attribute_group perfclass_attr_group
= {
5431 .attrs
= perfclass_attrs
,
5432 .name
= "perf_events",
5435 static int __init
perf_event_sysfs_init(void)
5437 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
5438 &perfclass_attr_group
);
5440 device_initcall(perf_event_sysfs_init
);