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/slab.h>
19 #include <linux/hash.h>
20 #include <linux/sysfs.h>
21 #include <linux/dcache.h>
22 #include <linux/percpu.h>
23 #include <linux/ptrace.h>
24 #include <linux/vmstat.h>
25 #include <linux/vmalloc.h>
26 #include <linux/hardirq.h>
27 #include <linux/rculist.h>
28 #include <linux/uaccess.h>
29 #include <linux/syscalls.h>
30 #include <linux/anon_inodes.h>
31 #include <linux/kernel_stat.h>
32 #include <linux/perf_event.h>
33 #include <linux/ftrace_event.h>
34 #include <linux/hw_breakpoint.h>
36 #include <asm/irq_regs.h>
39 * Each CPU has a list of per CPU events:
41 static DEFINE_PER_CPU(struct perf_cpu_context
, perf_cpu_context
);
43 int perf_max_events __read_mostly
= 1;
44 static int perf_reserved_percpu __read_mostly
;
45 static int perf_overcommit __read_mostly
= 1;
47 static atomic_t nr_events __read_mostly
;
48 static atomic_t nr_mmap_events __read_mostly
;
49 static atomic_t nr_comm_events __read_mostly
;
50 static atomic_t nr_task_events __read_mostly
;
53 * perf event paranoia level:
54 * -1 - not paranoid at all
55 * 0 - disallow raw tracepoint access for unpriv
56 * 1 - disallow cpu events for unpriv
57 * 2 - disallow kernel profiling for unpriv
59 int sysctl_perf_event_paranoid __read_mostly
= 1;
61 /* Minimum for 128 pages + 1 for the user control page */
62 int sysctl_perf_event_mlock __read_mostly
= 516; /* 'free' kb per user */
65 * max perf event sample rate
67 int sysctl_perf_event_sample_rate __read_mostly
= 100000;
69 static atomic64_t perf_event_id
;
72 * Lock for (sysadmin-configurable) event reservations:
74 static DEFINE_SPINLOCK(perf_resource_lock
);
77 * Architecture provided APIs - weak aliases:
79 extern __weak
const struct pmu
*hw_perf_event_init(struct perf_event
*event
)
84 void __weak
hw_perf_disable(void) { barrier(); }
85 void __weak
hw_perf_enable(void) { barrier(); }
87 void __weak
perf_event_print_debug(void) { }
89 static DEFINE_PER_CPU(int, perf_disable_count
);
91 void perf_disable(void)
93 if (!__get_cpu_var(perf_disable_count
)++)
97 void perf_enable(void)
99 if (!--__get_cpu_var(perf_disable_count
))
103 static void get_ctx(struct perf_event_context
*ctx
)
105 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
108 static void free_ctx(struct rcu_head
*head
)
110 struct perf_event_context
*ctx
;
112 ctx
= container_of(head
, struct perf_event_context
, rcu_head
);
116 static void put_ctx(struct perf_event_context
*ctx
)
118 if (atomic_dec_and_test(&ctx
->refcount
)) {
120 put_ctx(ctx
->parent_ctx
);
122 put_task_struct(ctx
->task
);
123 call_rcu(&ctx
->rcu_head
, free_ctx
);
127 static void unclone_ctx(struct perf_event_context
*ctx
)
129 if (ctx
->parent_ctx
) {
130 put_ctx(ctx
->parent_ctx
);
131 ctx
->parent_ctx
= NULL
;
136 * If we inherit events we want to return the parent event id
139 static u64
primary_event_id(struct perf_event
*event
)
144 id
= event
->parent
->id
;
150 * Get the perf_event_context for a task and lock it.
151 * This has to cope with with the fact that until it is locked,
152 * the context could get moved to another task.
154 static struct perf_event_context
*
155 perf_lock_task_context(struct task_struct
*task
, unsigned long *flags
)
157 struct perf_event_context
*ctx
;
161 ctx
= rcu_dereference(task
->perf_event_ctxp
);
164 * If this context is a clone of another, it might
165 * get swapped for another underneath us by
166 * perf_event_task_sched_out, though the
167 * rcu_read_lock() protects us from any context
168 * getting freed. Lock the context and check if it
169 * got swapped before we could get the lock, and retry
170 * if so. If we locked the right context, then it
171 * can't get swapped on us any more.
173 raw_spin_lock_irqsave(&ctx
->lock
, *flags
);
174 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
)) {
175 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
179 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
180 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
189 * Get the context for a task and increment its pin_count so it
190 * can't get swapped to another task. This also increments its
191 * reference count so that the context can't get freed.
193 static struct perf_event_context
*perf_pin_task_context(struct task_struct
*task
)
195 struct perf_event_context
*ctx
;
198 ctx
= perf_lock_task_context(task
, &flags
);
201 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
206 static void perf_unpin_context(struct perf_event_context
*ctx
)
210 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
212 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
216 static inline u64
perf_clock(void)
218 return cpu_clock(raw_smp_processor_id());
222 * Update the record of the current time in a context.
224 static void update_context_time(struct perf_event_context
*ctx
)
226 u64 now
= perf_clock();
228 ctx
->time
+= now
- ctx
->timestamp
;
229 ctx
->timestamp
= now
;
233 * Update the total_time_enabled and total_time_running fields for a event.
235 static void update_event_times(struct perf_event
*event
)
237 struct perf_event_context
*ctx
= event
->ctx
;
240 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
241 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
247 run_end
= event
->tstamp_stopped
;
249 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
251 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
252 run_end
= event
->tstamp_stopped
;
256 event
->total_time_running
= run_end
- event
->tstamp_running
;
260 * Update total_time_enabled and total_time_running for all events in a group.
262 static void update_group_times(struct perf_event
*leader
)
264 struct perf_event
*event
;
266 update_event_times(leader
);
267 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
268 update_event_times(event
);
271 static struct list_head
*
272 ctx_group_list(struct perf_event
*event
, struct perf_event_context
*ctx
)
274 if (event
->attr
.pinned
)
275 return &ctx
->pinned_groups
;
277 return &ctx
->flexible_groups
;
281 * Add a event from the lists for its context.
282 * Must be called with ctx->mutex and ctx->lock held.
285 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
287 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_CONTEXT
);
288 event
->attach_state
|= PERF_ATTACH_CONTEXT
;
291 * If we're a stand alone event or group leader, we go to the context
292 * list, group events are kept attached to the group so that
293 * perf_group_detach can, at all times, locate all siblings.
295 if (event
->group_leader
== event
) {
296 struct list_head
*list
;
298 if (is_software_event(event
))
299 event
->group_flags
|= PERF_GROUP_SOFTWARE
;
301 list
= ctx_group_list(event
, ctx
);
302 list_add_tail(&event
->group_entry
, list
);
305 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
307 if (event
->attr
.inherit_stat
)
311 static void perf_group_attach(struct perf_event
*event
)
313 struct perf_event
*group_leader
= event
->group_leader
;
315 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_GROUP
);
316 event
->attach_state
|= PERF_ATTACH_GROUP
;
318 if (group_leader
== event
)
321 if (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
&&
322 !is_software_event(event
))
323 group_leader
->group_flags
&= ~PERF_GROUP_SOFTWARE
;
325 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
326 group_leader
->nr_siblings
++;
330 * Remove a event from the lists for its context.
331 * Must be called with ctx->mutex and ctx->lock held.
334 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
337 * We can have double detach due to exit/hot-unplug + close.
339 if (!(event
->attach_state
& PERF_ATTACH_CONTEXT
))
342 event
->attach_state
&= ~PERF_ATTACH_CONTEXT
;
345 if (event
->attr
.inherit_stat
)
348 list_del_rcu(&event
->event_entry
);
350 if (event
->group_leader
== event
)
351 list_del_init(&event
->group_entry
);
353 update_group_times(event
);
356 * If event was in error state, then keep it
357 * that way, otherwise bogus counts will be
358 * returned on read(). The only way to get out
359 * of error state is by explicit re-enabling
362 if (event
->state
> PERF_EVENT_STATE_OFF
)
363 event
->state
= PERF_EVENT_STATE_OFF
;
366 static void perf_group_detach(struct perf_event
*event
)
368 struct perf_event
*sibling
, *tmp
;
369 struct list_head
*list
= NULL
;
372 * We can have double detach due to exit/hot-unplug + close.
374 if (!(event
->attach_state
& PERF_ATTACH_GROUP
))
377 event
->attach_state
&= ~PERF_ATTACH_GROUP
;
380 * If this is a sibling, remove it from its group.
382 if (event
->group_leader
!= event
) {
383 list_del_init(&event
->group_entry
);
384 event
->group_leader
->nr_siblings
--;
388 if (!list_empty(&event
->group_entry
))
389 list
= &event
->group_entry
;
392 * If this was a group event with sibling events then
393 * upgrade the siblings to singleton events by adding them
394 * to whatever list we are on.
396 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
398 list_move_tail(&sibling
->group_entry
, list
);
399 sibling
->group_leader
= sibling
;
401 /* Inherit group flags from the previous leader */
402 sibling
->group_flags
= event
->group_flags
;
407 event_sched_out(struct perf_event
*event
,
408 struct perf_cpu_context
*cpuctx
,
409 struct perf_event_context
*ctx
)
411 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
414 event
->state
= PERF_EVENT_STATE_INACTIVE
;
415 if (event
->pending_disable
) {
416 event
->pending_disable
= 0;
417 event
->state
= PERF_EVENT_STATE_OFF
;
419 event
->tstamp_stopped
= ctx
->time
;
420 event
->pmu
->disable(event
);
423 if (!is_software_event(event
))
424 cpuctx
->active_oncpu
--;
426 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
427 cpuctx
->exclusive
= 0;
431 group_sched_out(struct perf_event
*group_event
,
432 struct perf_cpu_context
*cpuctx
,
433 struct perf_event_context
*ctx
)
435 struct perf_event
*event
;
437 if (group_event
->state
!= PERF_EVENT_STATE_ACTIVE
)
440 event_sched_out(group_event
, cpuctx
, ctx
);
443 * Schedule out siblings (if any):
445 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
446 event_sched_out(event
, cpuctx
, ctx
);
448 if (group_event
->attr
.exclusive
)
449 cpuctx
->exclusive
= 0;
453 * Cross CPU call to remove a performance event
455 * We disable the event on the hardware level first. After that we
456 * remove it from the context list.
458 static void __perf_event_remove_from_context(void *info
)
460 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
461 struct perf_event
*event
= info
;
462 struct perf_event_context
*ctx
= event
->ctx
;
465 * If this is a task context, we need to check whether it is
466 * the current task context of this cpu. If not it has been
467 * scheduled out before the smp call arrived.
469 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
472 raw_spin_lock(&ctx
->lock
);
474 * Protect the list operation against NMI by disabling the
475 * events on a global level.
479 event_sched_out(event
, cpuctx
, ctx
);
481 list_del_event(event
, ctx
);
485 * Allow more per task events with respect to the
488 cpuctx
->max_pertask
=
489 min(perf_max_events
- ctx
->nr_events
,
490 perf_max_events
- perf_reserved_percpu
);
494 raw_spin_unlock(&ctx
->lock
);
499 * Remove the event from a task's (or a CPU's) list of events.
501 * Must be called with ctx->mutex held.
503 * CPU events are removed with a smp call. For task events we only
504 * call when the task is on a CPU.
506 * If event->ctx is a cloned context, callers must make sure that
507 * every task struct that event->ctx->task could possibly point to
508 * remains valid. This is OK when called from perf_release since
509 * that only calls us on the top-level context, which can't be a clone.
510 * When called from perf_event_exit_task, it's OK because the
511 * context has been detached from its task.
513 static void perf_event_remove_from_context(struct perf_event
*event
)
515 struct perf_event_context
*ctx
= event
->ctx
;
516 struct task_struct
*task
= ctx
->task
;
520 * Per cpu events are removed via an smp call and
521 * the removal is always successful.
523 smp_call_function_single(event
->cpu
,
524 __perf_event_remove_from_context
,
530 task_oncpu_function_call(task
, __perf_event_remove_from_context
,
533 raw_spin_lock_irq(&ctx
->lock
);
535 * If the context is active we need to retry the smp call.
537 if (ctx
->nr_active
&& !list_empty(&event
->group_entry
)) {
538 raw_spin_unlock_irq(&ctx
->lock
);
543 * The lock prevents that this context is scheduled in so we
544 * can remove the event safely, if the call above did not
547 if (!list_empty(&event
->group_entry
))
548 list_del_event(event
, ctx
);
549 raw_spin_unlock_irq(&ctx
->lock
);
553 * Cross CPU call to disable a performance event
555 static void __perf_event_disable(void *info
)
557 struct perf_event
*event
= info
;
558 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
559 struct perf_event_context
*ctx
= event
->ctx
;
562 * If this is a per-task event, need to check whether this
563 * event's task is the current task on this cpu.
565 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
568 raw_spin_lock(&ctx
->lock
);
571 * If the event is on, turn it off.
572 * If it is in error state, leave it in error state.
574 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
) {
575 update_context_time(ctx
);
576 update_group_times(event
);
577 if (event
== event
->group_leader
)
578 group_sched_out(event
, cpuctx
, ctx
);
580 event_sched_out(event
, cpuctx
, ctx
);
581 event
->state
= PERF_EVENT_STATE_OFF
;
584 raw_spin_unlock(&ctx
->lock
);
590 * If event->ctx is a cloned context, callers must make sure that
591 * every task struct that event->ctx->task could possibly point to
592 * remains valid. This condition is satisifed when called through
593 * perf_event_for_each_child or perf_event_for_each because they
594 * hold the top-level event's child_mutex, so any descendant that
595 * goes to exit will block in sync_child_event.
596 * When called from perf_pending_event it's OK because event->ctx
597 * is the current context on this CPU and preemption is disabled,
598 * hence we can't get into perf_event_task_sched_out for this context.
600 void perf_event_disable(struct perf_event
*event
)
602 struct perf_event_context
*ctx
= event
->ctx
;
603 struct task_struct
*task
= ctx
->task
;
607 * Disable the event on the cpu that it's on
609 smp_call_function_single(event
->cpu
, __perf_event_disable
,
615 task_oncpu_function_call(task
, __perf_event_disable
, event
);
617 raw_spin_lock_irq(&ctx
->lock
);
619 * If the event is still active, we need to retry the cross-call.
621 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
622 raw_spin_unlock_irq(&ctx
->lock
);
627 * Since we have the lock this context can't be scheduled
628 * in, so we can change the state safely.
630 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
631 update_group_times(event
);
632 event
->state
= PERF_EVENT_STATE_OFF
;
635 raw_spin_unlock_irq(&ctx
->lock
);
639 event_sched_in(struct perf_event
*event
,
640 struct perf_cpu_context
*cpuctx
,
641 struct perf_event_context
*ctx
)
643 if (event
->state
<= PERF_EVENT_STATE_OFF
)
646 event
->state
= PERF_EVENT_STATE_ACTIVE
;
647 event
->oncpu
= smp_processor_id();
649 * The new state must be visible before we turn it on in the hardware:
653 if (event
->pmu
->enable(event
)) {
654 event
->state
= PERF_EVENT_STATE_INACTIVE
;
659 event
->tstamp_running
+= ctx
->time
- event
->tstamp_stopped
;
661 if (!is_software_event(event
))
662 cpuctx
->active_oncpu
++;
665 if (event
->attr
.exclusive
)
666 cpuctx
->exclusive
= 1;
672 group_sched_in(struct perf_event
*group_event
,
673 struct perf_cpu_context
*cpuctx
,
674 struct perf_event_context
*ctx
)
676 struct perf_event
*event
, *partial_group
= NULL
;
677 const struct pmu
*pmu
= group_event
->pmu
;
681 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
684 /* Check if group transaction availabe */
691 if (event_sched_in(group_event
, cpuctx
, ctx
)) {
693 pmu
->cancel_txn(pmu
);
698 * Schedule in siblings as one group (if any):
700 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
701 if (event_sched_in(event
, cpuctx
, ctx
)) {
702 partial_group
= event
;
710 ret
= pmu
->commit_txn(pmu
);
712 pmu
->cancel_txn(pmu
);
718 * Groups can be scheduled in as one unit only, so undo any
719 * partial group before returning:
721 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
722 if (event
== partial_group
)
724 event_sched_out(event
, cpuctx
, ctx
);
726 event_sched_out(group_event
, cpuctx
, ctx
);
729 pmu
->cancel_txn(pmu
);
735 * Work out whether we can put this event group on the CPU now.
737 static int group_can_go_on(struct perf_event
*event
,
738 struct perf_cpu_context
*cpuctx
,
742 * Groups consisting entirely of software events can always go on.
744 if (event
->group_flags
& PERF_GROUP_SOFTWARE
)
747 * If an exclusive group is already on, no other hardware
750 if (cpuctx
->exclusive
)
753 * If this group is exclusive and there are already
754 * events on the CPU, it can't go on.
756 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
759 * Otherwise, try to add it if all previous groups were able
765 static void add_event_to_ctx(struct perf_event
*event
,
766 struct perf_event_context
*ctx
)
768 list_add_event(event
, ctx
);
769 perf_group_attach(event
);
770 event
->tstamp_enabled
= ctx
->time
;
771 event
->tstamp_running
= ctx
->time
;
772 event
->tstamp_stopped
= ctx
->time
;
776 * Cross CPU call to install and enable a performance event
778 * Must be called with ctx->mutex held
780 static void __perf_install_in_context(void *info
)
782 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
783 struct perf_event
*event
= info
;
784 struct perf_event_context
*ctx
= event
->ctx
;
785 struct perf_event
*leader
= event
->group_leader
;
789 * If this is a task context, we need to check whether it is
790 * the current task context of this cpu. If not it has been
791 * scheduled out before the smp call arrived.
792 * Or possibly this is the right context but it isn't
793 * on this cpu because it had no events.
795 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
796 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
798 cpuctx
->task_ctx
= ctx
;
801 raw_spin_lock(&ctx
->lock
);
803 update_context_time(ctx
);
806 * Protect the list operation against NMI by disabling the
807 * events on a global level. NOP for non NMI based events.
811 add_event_to_ctx(event
, ctx
);
813 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
817 * Don't put the event on if it is disabled or if
818 * it is in a group and the group isn't on.
820 if (event
->state
!= PERF_EVENT_STATE_INACTIVE
||
821 (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
))
825 * An exclusive event can't go on if there are already active
826 * hardware events, and no hardware event can go on if there
827 * is already an exclusive event on.
829 if (!group_can_go_on(event
, cpuctx
, 1))
832 err
= event_sched_in(event
, cpuctx
, ctx
);
836 * This event couldn't go on. If it is in a group
837 * then we have to pull the whole group off.
838 * If the event group is pinned then put it in error state.
841 group_sched_out(leader
, cpuctx
, ctx
);
842 if (leader
->attr
.pinned
) {
843 update_group_times(leader
);
844 leader
->state
= PERF_EVENT_STATE_ERROR
;
848 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
849 cpuctx
->max_pertask
--;
854 raw_spin_unlock(&ctx
->lock
);
858 * Attach a performance event to a context
860 * First we add the event to the list with the hardware enable bit
861 * in event->hw_config cleared.
863 * If the event is attached to a task which is on a CPU we use a smp
864 * call to enable it in the task context. The task might have been
865 * scheduled away, but we check this in the smp call again.
867 * Must be called with ctx->mutex held.
870 perf_install_in_context(struct perf_event_context
*ctx
,
871 struct perf_event
*event
,
874 struct task_struct
*task
= ctx
->task
;
878 * Per cpu events are installed via an smp call and
879 * the install is always successful.
881 smp_call_function_single(cpu
, __perf_install_in_context
,
887 task_oncpu_function_call(task
, __perf_install_in_context
,
890 raw_spin_lock_irq(&ctx
->lock
);
892 * we need to retry the smp call.
894 if (ctx
->is_active
&& list_empty(&event
->group_entry
)) {
895 raw_spin_unlock_irq(&ctx
->lock
);
900 * The lock prevents that this context is scheduled in so we
901 * can add the event safely, if it the call above did not
904 if (list_empty(&event
->group_entry
))
905 add_event_to_ctx(event
, ctx
);
906 raw_spin_unlock_irq(&ctx
->lock
);
910 * Put a event into inactive state and update time fields.
911 * Enabling the leader of a group effectively enables all
912 * the group members that aren't explicitly disabled, so we
913 * have to update their ->tstamp_enabled also.
914 * Note: this works for group members as well as group leaders
915 * since the non-leader members' sibling_lists will be empty.
917 static void __perf_event_mark_enabled(struct perf_event
*event
,
918 struct perf_event_context
*ctx
)
920 struct perf_event
*sub
;
922 event
->state
= PERF_EVENT_STATE_INACTIVE
;
923 event
->tstamp_enabled
= ctx
->time
- event
->total_time_enabled
;
924 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
)
925 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
)
926 sub
->tstamp_enabled
=
927 ctx
->time
- sub
->total_time_enabled
;
931 * Cross CPU call to enable a performance event
933 static void __perf_event_enable(void *info
)
935 struct perf_event
*event
= info
;
936 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
937 struct perf_event_context
*ctx
= event
->ctx
;
938 struct perf_event
*leader
= event
->group_leader
;
942 * If this is a per-task event, need to check whether this
943 * event's task is the current task on this cpu.
945 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
946 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
948 cpuctx
->task_ctx
= ctx
;
951 raw_spin_lock(&ctx
->lock
);
953 update_context_time(ctx
);
955 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
957 __perf_event_mark_enabled(event
, ctx
);
959 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
963 * If the event is in a group and isn't the group leader,
964 * then don't put it on unless the group is on.
966 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
)
969 if (!group_can_go_on(event
, cpuctx
, 1)) {
974 err
= group_sched_in(event
, cpuctx
, ctx
);
976 err
= event_sched_in(event
, cpuctx
, ctx
);
982 * If this event can't go on and it's part of a
983 * group, then the whole group has to come off.
986 group_sched_out(leader
, cpuctx
, ctx
);
987 if (leader
->attr
.pinned
) {
988 update_group_times(leader
);
989 leader
->state
= PERF_EVENT_STATE_ERROR
;
994 raw_spin_unlock(&ctx
->lock
);
1000 * If event->ctx is a cloned context, callers must make sure that
1001 * every task struct that event->ctx->task could possibly point to
1002 * remains valid. This condition is satisfied when called through
1003 * perf_event_for_each_child or perf_event_for_each as described
1004 * for perf_event_disable.
1006 void perf_event_enable(struct perf_event
*event
)
1008 struct perf_event_context
*ctx
= event
->ctx
;
1009 struct task_struct
*task
= ctx
->task
;
1013 * Enable the event on the cpu that it's on
1015 smp_call_function_single(event
->cpu
, __perf_event_enable
,
1020 raw_spin_lock_irq(&ctx
->lock
);
1021 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1025 * If the event is in error state, clear that first.
1026 * That way, if we see the event in error state below, we
1027 * know that it has gone back into error state, as distinct
1028 * from the task having been scheduled away before the
1029 * cross-call arrived.
1031 if (event
->state
== PERF_EVENT_STATE_ERROR
)
1032 event
->state
= PERF_EVENT_STATE_OFF
;
1035 raw_spin_unlock_irq(&ctx
->lock
);
1036 task_oncpu_function_call(task
, __perf_event_enable
, event
);
1038 raw_spin_lock_irq(&ctx
->lock
);
1041 * If the context is active and the event is still off,
1042 * we need to retry the cross-call.
1044 if (ctx
->is_active
&& event
->state
== PERF_EVENT_STATE_OFF
)
1048 * Since we have the lock this context can't be scheduled
1049 * in, so we can change the state safely.
1051 if (event
->state
== PERF_EVENT_STATE_OFF
)
1052 __perf_event_mark_enabled(event
, ctx
);
1055 raw_spin_unlock_irq(&ctx
->lock
);
1058 static int perf_event_refresh(struct perf_event
*event
, int refresh
)
1061 * not supported on inherited events
1063 if (event
->attr
.inherit
)
1066 atomic_add(refresh
, &event
->event_limit
);
1067 perf_event_enable(event
);
1073 EVENT_FLEXIBLE
= 0x1,
1075 EVENT_ALL
= EVENT_FLEXIBLE
| EVENT_PINNED
,
1078 static void ctx_sched_out(struct perf_event_context
*ctx
,
1079 struct perf_cpu_context
*cpuctx
,
1080 enum event_type_t event_type
)
1082 struct perf_event
*event
;
1084 raw_spin_lock(&ctx
->lock
);
1086 if (likely(!ctx
->nr_events
))
1088 update_context_time(ctx
);
1091 if (!ctx
->nr_active
)
1094 if (event_type
& EVENT_PINNED
)
1095 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
)
1096 group_sched_out(event
, cpuctx
, ctx
);
1098 if (event_type
& EVENT_FLEXIBLE
)
1099 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
)
1100 group_sched_out(event
, cpuctx
, ctx
);
1105 raw_spin_unlock(&ctx
->lock
);
1109 * Test whether two contexts are equivalent, i.e. whether they
1110 * have both been cloned from the same version of the same context
1111 * and they both have the same number of enabled events.
1112 * If the number of enabled events is the same, then the set
1113 * of enabled events should be the same, because these are both
1114 * inherited contexts, therefore we can't access individual events
1115 * in them directly with an fd; we can only enable/disable all
1116 * events via prctl, or enable/disable all events in a family
1117 * via ioctl, which will have the same effect on both contexts.
1119 static int context_equiv(struct perf_event_context
*ctx1
,
1120 struct perf_event_context
*ctx2
)
1122 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
1123 && ctx1
->parent_gen
== ctx2
->parent_gen
1124 && !ctx1
->pin_count
&& !ctx2
->pin_count
;
1127 static void __perf_event_sync_stat(struct perf_event
*event
,
1128 struct perf_event
*next_event
)
1132 if (!event
->attr
.inherit_stat
)
1136 * Update the event value, we cannot use perf_event_read()
1137 * because we're in the middle of a context switch and have IRQs
1138 * disabled, which upsets smp_call_function_single(), however
1139 * we know the event must be on the current CPU, therefore we
1140 * don't need to use it.
1142 switch (event
->state
) {
1143 case PERF_EVENT_STATE_ACTIVE
:
1144 event
->pmu
->read(event
);
1147 case PERF_EVENT_STATE_INACTIVE
:
1148 update_event_times(event
);
1156 * In order to keep per-task stats reliable we need to flip the event
1157 * values when we flip the contexts.
1159 value
= atomic64_read(&next_event
->count
);
1160 value
= atomic64_xchg(&event
->count
, value
);
1161 atomic64_set(&next_event
->count
, value
);
1163 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
1164 swap(event
->total_time_running
, next_event
->total_time_running
);
1167 * Since we swizzled the values, update the user visible data too.
1169 perf_event_update_userpage(event
);
1170 perf_event_update_userpage(next_event
);
1173 #define list_next_entry(pos, member) \
1174 list_entry(pos->member.next, typeof(*pos), member)
1176 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
1177 struct perf_event_context
*next_ctx
)
1179 struct perf_event
*event
, *next_event
;
1184 update_context_time(ctx
);
1186 event
= list_first_entry(&ctx
->event_list
,
1187 struct perf_event
, event_entry
);
1189 next_event
= list_first_entry(&next_ctx
->event_list
,
1190 struct perf_event
, event_entry
);
1192 while (&event
->event_entry
!= &ctx
->event_list
&&
1193 &next_event
->event_entry
!= &next_ctx
->event_list
) {
1195 __perf_event_sync_stat(event
, next_event
);
1197 event
= list_next_entry(event
, event_entry
);
1198 next_event
= list_next_entry(next_event
, event_entry
);
1203 * Called from scheduler to remove the events of the current task,
1204 * with interrupts disabled.
1206 * We stop each event and update the event value in event->count.
1208 * This does not protect us against NMI, but disable()
1209 * sets the disabled bit in the control field of event _before_
1210 * accessing the event control register. If a NMI hits, then it will
1211 * not restart the event.
1213 void perf_event_task_sched_out(struct task_struct
*task
,
1214 struct task_struct
*next
)
1216 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1217 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
1218 struct perf_event_context
*next_ctx
;
1219 struct perf_event_context
*parent
;
1222 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES
, 1, 1, NULL
, 0);
1224 if (likely(!ctx
|| !cpuctx
->task_ctx
))
1228 parent
= rcu_dereference(ctx
->parent_ctx
);
1229 next_ctx
= next
->perf_event_ctxp
;
1230 if (parent
&& next_ctx
&&
1231 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
1233 * Looks like the two contexts are clones, so we might be
1234 * able to optimize the context switch. We lock both
1235 * contexts and check that they are clones under the
1236 * lock (including re-checking that neither has been
1237 * uncloned in the meantime). It doesn't matter which
1238 * order we take the locks because no other cpu could
1239 * be trying to lock both of these tasks.
1241 raw_spin_lock(&ctx
->lock
);
1242 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
1243 if (context_equiv(ctx
, next_ctx
)) {
1245 * XXX do we need a memory barrier of sorts
1246 * wrt to rcu_dereference() of perf_event_ctxp
1248 task
->perf_event_ctxp
= next_ctx
;
1249 next
->perf_event_ctxp
= ctx
;
1251 next_ctx
->task
= task
;
1254 perf_event_sync_stat(ctx
, next_ctx
);
1256 raw_spin_unlock(&next_ctx
->lock
);
1257 raw_spin_unlock(&ctx
->lock
);
1262 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
1263 cpuctx
->task_ctx
= NULL
;
1267 static void task_ctx_sched_out(struct perf_event_context
*ctx
,
1268 enum event_type_t event_type
)
1270 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1272 if (!cpuctx
->task_ctx
)
1275 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
1278 ctx_sched_out(ctx
, cpuctx
, event_type
);
1279 cpuctx
->task_ctx
= NULL
;
1283 * Called with IRQs disabled
1285 static void __perf_event_task_sched_out(struct perf_event_context
*ctx
)
1287 task_ctx_sched_out(ctx
, EVENT_ALL
);
1291 * Called with IRQs disabled
1293 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
1294 enum event_type_t event_type
)
1296 ctx_sched_out(&cpuctx
->ctx
, cpuctx
, event_type
);
1300 ctx_pinned_sched_in(struct perf_event_context
*ctx
,
1301 struct perf_cpu_context
*cpuctx
)
1303 struct perf_event
*event
;
1305 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
1306 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1308 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1311 if (group_can_go_on(event
, cpuctx
, 1))
1312 group_sched_in(event
, cpuctx
, ctx
);
1315 * If this pinned group hasn't been scheduled,
1316 * put it in error state.
1318 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1319 update_group_times(event
);
1320 event
->state
= PERF_EVENT_STATE_ERROR
;
1326 ctx_flexible_sched_in(struct perf_event_context
*ctx
,
1327 struct perf_cpu_context
*cpuctx
)
1329 struct perf_event
*event
;
1332 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
1333 /* Ignore events in OFF or ERROR state */
1334 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1337 * Listen to the 'cpu' scheduling filter constraint
1340 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1343 if (group_can_go_on(event
, cpuctx
, can_add_hw
))
1344 if (group_sched_in(event
, cpuctx
, ctx
))
1350 ctx_sched_in(struct perf_event_context
*ctx
,
1351 struct perf_cpu_context
*cpuctx
,
1352 enum event_type_t event_type
)
1354 raw_spin_lock(&ctx
->lock
);
1356 if (likely(!ctx
->nr_events
))
1359 ctx
->timestamp
= perf_clock();
1364 * First go through the list and put on any pinned groups
1365 * in order to give them the best chance of going on.
1367 if (event_type
& EVENT_PINNED
)
1368 ctx_pinned_sched_in(ctx
, cpuctx
);
1370 /* Then walk through the lower prio flexible groups */
1371 if (event_type
& EVENT_FLEXIBLE
)
1372 ctx_flexible_sched_in(ctx
, cpuctx
);
1376 raw_spin_unlock(&ctx
->lock
);
1379 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
1380 enum event_type_t event_type
)
1382 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
1384 ctx_sched_in(ctx
, cpuctx
, event_type
);
1387 static void task_ctx_sched_in(struct task_struct
*task
,
1388 enum event_type_t event_type
)
1390 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1391 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
1395 if (cpuctx
->task_ctx
== ctx
)
1397 ctx_sched_in(ctx
, cpuctx
, event_type
);
1398 cpuctx
->task_ctx
= ctx
;
1401 * Called from scheduler to add the events of the current task
1402 * with interrupts disabled.
1404 * We restore the event value and then enable it.
1406 * This does not protect us against NMI, but enable()
1407 * sets the enabled bit in the control field of event _before_
1408 * accessing the event control register. If a NMI hits, then it will
1409 * keep the event running.
1411 void perf_event_task_sched_in(struct task_struct
*task
)
1413 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1414 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
1419 if (cpuctx
->task_ctx
== ctx
)
1425 * We want to keep the following priority order:
1426 * cpu pinned (that don't need to move), task pinned,
1427 * cpu flexible, task flexible.
1429 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
1431 ctx_sched_in(ctx
, cpuctx
, EVENT_PINNED
);
1432 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
);
1433 ctx_sched_in(ctx
, cpuctx
, EVENT_FLEXIBLE
);
1435 cpuctx
->task_ctx
= ctx
;
1440 #define MAX_INTERRUPTS (~0ULL)
1442 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1444 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1446 u64 frequency
= event
->attr
.sample_freq
;
1447 u64 sec
= NSEC_PER_SEC
;
1448 u64 divisor
, dividend
;
1450 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
1452 count_fls
= fls64(count
);
1453 nsec_fls
= fls64(nsec
);
1454 frequency_fls
= fls64(frequency
);
1458 * We got @count in @nsec, with a target of sample_freq HZ
1459 * the target period becomes:
1462 * period = -------------------
1463 * @nsec * sample_freq
1468 * Reduce accuracy by one bit such that @a and @b converge
1469 * to a similar magnitude.
1471 #define REDUCE_FLS(a, b) \
1473 if (a##_fls > b##_fls) { \
1483 * Reduce accuracy until either term fits in a u64, then proceed with
1484 * the other, so that finally we can do a u64/u64 division.
1486 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
1487 REDUCE_FLS(nsec
, frequency
);
1488 REDUCE_FLS(sec
, count
);
1491 if (count_fls
+ sec_fls
> 64) {
1492 divisor
= nsec
* frequency
;
1494 while (count_fls
+ sec_fls
> 64) {
1495 REDUCE_FLS(count
, sec
);
1499 dividend
= count
* sec
;
1501 dividend
= count
* sec
;
1503 while (nsec_fls
+ frequency_fls
> 64) {
1504 REDUCE_FLS(nsec
, frequency
);
1508 divisor
= nsec
* frequency
;
1514 return div64_u64(dividend
, divisor
);
1517 static void perf_event_stop(struct perf_event
*event
)
1519 if (!event
->pmu
->stop
)
1520 return event
->pmu
->disable(event
);
1522 return event
->pmu
->stop(event
);
1525 static int perf_event_start(struct perf_event
*event
)
1527 if (!event
->pmu
->start
)
1528 return event
->pmu
->enable(event
);
1530 return event
->pmu
->start(event
);
1533 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1535 struct hw_perf_event
*hwc
= &event
->hw
;
1536 s64 period
, sample_period
;
1539 period
= perf_calculate_period(event
, nsec
, count
);
1541 delta
= (s64
)(period
- hwc
->sample_period
);
1542 delta
= (delta
+ 7) / 8; /* low pass filter */
1544 sample_period
= hwc
->sample_period
+ delta
;
1549 hwc
->sample_period
= sample_period
;
1551 if (atomic64_read(&hwc
->period_left
) > 8*sample_period
) {
1553 perf_event_stop(event
);
1554 atomic64_set(&hwc
->period_left
, 0);
1555 perf_event_start(event
);
1560 static void perf_ctx_adjust_freq(struct perf_event_context
*ctx
)
1562 struct perf_event
*event
;
1563 struct hw_perf_event
*hwc
;
1564 u64 interrupts
, now
;
1567 raw_spin_lock(&ctx
->lock
);
1568 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
1569 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1572 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1577 interrupts
= hwc
->interrupts
;
1578 hwc
->interrupts
= 0;
1581 * unthrottle events on the tick
1583 if (interrupts
== MAX_INTERRUPTS
) {
1584 perf_log_throttle(event
, 1);
1586 event
->pmu
->unthrottle(event
);
1590 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
1594 event
->pmu
->read(event
);
1595 now
= atomic64_read(&event
->count
);
1596 delta
= now
- hwc
->freq_count_stamp
;
1597 hwc
->freq_count_stamp
= now
;
1600 perf_adjust_period(event
, TICK_NSEC
, delta
);
1603 raw_spin_unlock(&ctx
->lock
);
1607 * Round-robin a context's events:
1609 static void rotate_ctx(struct perf_event_context
*ctx
)
1611 raw_spin_lock(&ctx
->lock
);
1614 * Rotate the first entry last of non-pinned groups. Rotation might be
1615 * disabled by the inheritance code.
1617 if (!ctx
->rotate_disable
)
1618 list_rotate_left(&ctx
->flexible_groups
);
1620 raw_spin_unlock(&ctx
->lock
);
1623 void perf_event_task_tick(struct task_struct
*curr
)
1625 struct perf_cpu_context
*cpuctx
;
1626 struct perf_event_context
*ctx
;
1629 if (!atomic_read(&nr_events
))
1632 cpuctx
= &__get_cpu_var(perf_cpu_context
);
1633 if (cpuctx
->ctx
.nr_events
&&
1634 cpuctx
->ctx
.nr_events
!= cpuctx
->ctx
.nr_active
)
1637 ctx
= curr
->perf_event_ctxp
;
1638 if (ctx
&& ctx
->nr_events
&& ctx
->nr_events
!= ctx
->nr_active
)
1641 perf_ctx_adjust_freq(&cpuctx
->ctx
);
1643 perf_ctx_adjust_freq(ctx
);
1649 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
1651 task_ctx_sched_out(ctx
, EVENT_FLEXIBLE
);
1653 rotate_ctx(&cpuctx
->ctx
);
1657 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
);
1659 task_ctx_sched_in(curr
, EVENT_FLEXIBLE
);
1663 static int event_enable_on_exec(struct perf_event
*event
,
1664 struct perf_event_context
*ctx
)
1666 if (!event
->attr
.enable_on_exec
)
1669 event
->attr
.enable_on_exec
= 0;
1670 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1673 __perf_event_mark_enabled(event
, ctx
);
1679 * Enable all of a task's events that have been marked enable-on-exec.
1680 * This expects task == current.
1682 static void perf_event_enable_on_exec(struct task_struct
*task
)
1684 struct perf_event_context
*ctx
;
1685 struct perf_event
*event
;
1686 unsigned long flags
;
1690 local_irq_save(flags
);
1691 ctx
= task
->perf_event_ctxp
;
1692 if (!ctx
|| !ctx
->nr_events
)
1695 __perf_event_task_sched_out(ctx
);
1697 raw_spin_lock(&ctx
->lock
);
1699 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
1700 ret
= event_enable_on_exec(event
, ctx
);
1705 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
1706 ret
= event_enable_on_exec(event
, ctx
);
1712 * Unclone this context if we enabled any event.
1717 raw_spin_unlock(&ctx
->lock
);
1719 perf_event_task_sched_in(task
);
1721 local_irq_restore(flags
);
1725 * Cross CPU call to read the hardware event
1727 static void __perf_event_read(void *info
)
1729 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1730 struct perf_event
*event
= info
;
1731 struct perf_event_context
*ctx
= event
->ctx
;
1734 * If this is a task context, we need to check whether it is
1735 * the current task context of this cpu. If not it has been
1736 * scheduled out before the smp call arrived. In that case
1737 * event->count would have been updated to a recent sample
1738 * when the event was scheduled out.
1740 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
1743 raw_spin_lock(&ctx
->lock
);
1744 update_context_time(ctx
);
1745 update_event_times(event
);
1746 raw_spin_unlock(&ctx
->lock
);
1748 event
->pmu
->read(event
);
1751 static u64
perf_event_read(struct perf_event
*event
)
1754 * If event is enabled and currently active on a CPU, update the
1755 * value in the event structure:
1757 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
1758 smp_call_function_single(event
->oncpu
,
1759 __perf_event_read
, event
, 1);
1760 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1761 struct perf_event_context
*ctx
= event
->ctx
;
1762 unsigned long flags
;
1764 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
1766 * may read while context is not active
1767 * (e.g., thread is blocked), in that case
1768 * we cannot update context time
1771 update_context_time(ctx
);
1772 update_event_times(event
);
1773 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1776 return atomic64_read(&event
->count
);
1780 * Initialize the perf_event context in a task_struct:
1783 __perf_event_init_context(struct perf_event_context
*ctx
,
1784 struct task_struct
*task
)
1786 raw_spin_lock_init(&ctx
->lock
);
1787 mutex_init(&ctx
->mutex
);
1788 INIT_LIST_HEAD(&ctx
->pinned_groups
);
1789 INIT_LIST_HEAD(&ctx
->flexible_groups
);
1790 INIT_LIST_HEAD(&ctx
->event_list
);
1791 atomic_set(&ctx
->refcount
, 1);
1795 static struct perf_event_context
*find_get_context(pid_t pid
, int cpu
)
1797 struct perf_event_context
*ctx
;
1798 struct perf_cpu_context
*cpuctx
;
1799 struct task_struct
*task
;
1800 unsigned long flags
;
1803 if (pid
== -1 && cpu
!= -1) {
1804 /* Must be root to operate on a CPU event: */
1805 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
1806 return ERR_PTR(-EACCES
);
1808 if (cpu
< 0 || cpu
>= nr_cpumask_bits
)
1809 return ERR_PTR(-EINVAL
);
1812 * We could be clever and allow to attach a event to an
1813 * offline CPU and activate it when the CPU comes up, but
1816 if (!cpu_online(cpu
))
1817 return ERR_PTR(-ENODEV
);
1819 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1830 task
= find_task_by_vpid(pid
);
1832 get_task_struct(task
);
1836 return ERR_PTR(-ESRCH
);
1839 * Can't attach events to a dying task.
1842 if (task
->flags
& PF_EXITING
)
1845 /* Reuse ptrace permission checks for now. */
1847 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1851 ctx
= perf_lock_task_context(task
, &flags
);
1854 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1858 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
1862 __perf_event_init_context(ctx
, task
);
1864 if (cmpxchg(&task
->perf_event_ctxp
, NULL
, ctx
)) {
1866 * We raced with some other task; use
1867 * the context they set.
1872 get_task_struct(task
);
1875 put_task_struct(task
);
1879 put_task_struct(task
);
1880 return ERR_PTR(err
);
1883 static void perf_event_free_filter(struct perf_event
*event
);
1885 static void free_event_rcu(struct rcu_head
*head
)
1887 struct perf_event
*event
;
1889 event
= container_of(head
, struct perf_event
, rcu_head
);
1891 put_pid_ns(event
->ns
);
1892 perf_event_free_filter(event
);
1896 static void perf_pending_sync(struct perf_event
*event
);
1897 static void perf_mmap_data_put(struct perf_mmap_data
*data
);
1899 static void free_event(struct perf_event
*event
)
1901 perf_pending_sync(event
);
1903 if (!event
->parent
) {
1904 atomic_dec(&nr_events
);
1905 if (event
->attr
.mmap
)
1906 atomic_dec(&nr_mmap_events
);
1907 if (event
->attr
.comm
)
1908 atomic_dec(&nr_comm_events
);
1909 if (event
->attr
.task
)
1910 atomic_dec(&nr_task_events
);
1914 perf_mmap_data_put(event
->data
);
1919 event
->destroy(event
);
1921 put_ctx(event
->ctx
);
1922 call_rcu(&event
->rcu_head
, free_event_rcu
);
1925 int perf_event_release_kernel(struct perf_event
*event
)
1927 struct perf_event_context
*ctx
= event
->ctx
;
1930 * Remove from the PMU, can't get re-enabled since we got
1931 * here because the last ref went.
1933 perf_event_disable(event
);
1935 WARN_ON_ONCE(ctx
->parent_ctx
);
1937 * There are two ways this annotation is useful:
1939 * 1) there is a lock recursion from perf_event_exit_task
1940 * see the comment there.
1942 * 2) there is a lock-inversion with mmap_sem through
1943 * perf_event_read_group(), which takes faults while
1944 * holding ctx->mutex, however this is called after
1945 * the last filedesc died, so there is no possibility
1946 * to trigger the AB-BA case.
1948 mutex_lock_nested(&ctx
->mutex
, SINGLE_DEPTH_NESTING
);
1949 raw_spin_lock_irq(&ctx
->lock
);
1950 perf_group_detach(event
);
1951 list_del_event(event
, ctx
);
1952 raw_spin_unlock_irq(&ctx
->lock
);
1953 mutex_unlock(&ctx
->mutex
);
1955 mutex_lock(&event
->owner
->perf_event_mutex
);
1956 list_del_init(&event
->owner_entry
);
1957 mutex_unlock(&event
->owner
->perf_event_mutex
);
1958 put_task_struct(event
->owner
);
1964 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
1967 * Called when the last reference to the file is gone.
1969 static int perf_release(struct inode
*inode
, struct file
*file
)
1971 struct perf_event
*event
= file
->private_data
;
1973 file
->private_data
= NULL
;
1975 return perf_event_release_kernel(event
);
1978 static int perf_event_read_size(struct perf_event
*event
)
1980 int entry
= sizeof(u64
); /* value */
1984 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1985 size
+= sizeof(u64
);
1987 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1988 size
+= sizeof(u64
);
1990 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
1991 entry
+= sizeof(u64
);
1993 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
1994 nr
+= event
->group_leader
->nr_siblings
;
1995 size
+= sizeof(u64
);
2003 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
2005 struct perf_event
*child
;
2011 mutex_lock(&event
->child_mutex
);
2012 total
+= perf_event_read(event
);
2013 *enabled
+= event
->total_time_enabled
+
2014 atomic64_read(&event
->child_total_time_enabled
);
2015 *running
+= event
->total_time_running
+
2016 atomic64_read(&event
->child_total_time_running
);
2018 list_for_each_entry(child
, &event
->child_list
, child_list
) {
2019 total
+= perf_event_read(child
);
2020 *enabled
+= child
->total_time_enabled
;
2021 *running
+= child
->total_time_running
;
2023 mutex_unlock(&event
->child_mutex
);
2027 EXPORT_SYMBOL_GPL(perf_event_read_value
);
2029 static int perf_event_read_group(struct perf_event
*event
,
2030 u64 read_format
, char __user
*buf
)
2032 struct perf_event
*leader
= event
->group_leader
, *sub
;
2033 int n
= 0, size
= 0, ret
= -EFAULT
;
2034 struct perf_event_context
*ctx
= leader
->ctx
;
2036 u64 count
, enabled
, running
;
2038 mutex_lock(&ctx
->mutex
);
2039 count
= perf_event_read_value(leader
, &enabled
, &running
);
2041 values
[n
++] = 1 + leader
->nr_siblings
;
2042 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2043 values
[n
++] = enabled
;
2044 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2045 values
[n
++] = running
;
2046 values
[n
++] = count
;
2047 if (read_format
& PERF_FORMAT_ID
)
2048 values
[n
++] = primary_event_id(leader
);
2050 size
= n
* sizeof(u64
);
2052 if (copy_to_user(buf
, values
, size
))
2057 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
2060 values
[n
++] = perf_event_read_value(sub
, &enabled
, &running
);
2061 if (read_format
& PERF_FORMAT_ID
)
2062 values
[n
++] = primary_event_id(sub
);
2064 size
= n
* sizeof(u64
);
2066 if (copy_to_user(buf
+ ret
, values
, size
)) {
2074 mutex_unlock(&ctx
->mutex
);
2079 static int perf_event_read_one(struct perf_event
*event
,
2080 u64 read_format
, char __user
*buf
)
2082 u64 enabled
, running
;
2086 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
2087 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2088 values
[n
++] = enabled
;
2089 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2090 values
[n
++] = running
;
2091 if (read_format
& PERF_FORMAT_ID
)
2092 values
[n
++] = primary_event_id(event
);
2094 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
2097 return n
* sizeof(u64
);
2101 * Read the performance event - simple non blocking version for now
2104 perf_read_hw(struct perf_event
*event
, char __user
*buf
, size_t count
)
2106 u64 read_format
= event
->attr
.read_format
;
2110 * Return end-of-file for a read on a event that is in
2111 * error state (i.e. because it was pinned but it couldn't be
2112 * scheduled on to the CPU at some point).
2114 if (event
->state
== PERF_EVENT_STATE_ERROR
)
2117 if (count
< perf_event_read_size(event
))
2120 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2121 if (read_format
& PERF_FORMAT_GROUP
)
2122 ret
= perf_event_read_group(event
, read_format
, buf
);
2124 ret
= perf_event_read_one(event
, read_format
, buf
);
2130 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
2132 struct perf_event
*event
= file
->private_data
;
2134 return perf_read_hw(event
, buf
, count
);
2137 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
2139 struct perf_event
*event
= file
->private_data
;
2140 struct perf_mmap_data
*data
;
2141 unsigned int events
= POLL_HUP
;
2144 data
= rcu_dereference(event
->data
);
2146 events
= atomic_xchg(&data
->poll
, 0);
2149 poll_wait(file
, &event
->waitq
, wait
);
2154 static void perf_event_reset(struct perf_event
*event
)
2156 (void)perf_event_read(event
);
2157 atomic64_set(&event
->count
, 0);
2158 perf_event_update_userpage(event
);
2162 * Holding the top-level event's child_mutex means that any
2163 * descendant process that has inherited this event will block
2164 * in sync_child_event if it goes to exit, thus satisfying the
2165 * task existence requirements of perf_event_enable/disable.
2167 static void perf_event_for_each_child(struct perf_event
*event
,
2168 void (*func
)(struct perf_event
*))
2170 struct perf_event
*child
;
2172 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2173 mutex_lock(&event
->child_mutex
);
2175 list_for_each_entry(child
, &event
->child_list
, child_list
)
2177 mutex_unlock(&event
->child_mutex
);
2180 static void perf_event_for_each(struct perf_event
*event
,
2181 void (*func
)(struct perf_event
*))
2183 struct perf_event_context
*ctx
= event
->ctx
;
2184 struct perf_event
*sibling
;
2186 WARN_ON_ONCE(ctx
->parent_ctx
);
2187 mutex_lock(&ctx
->mutex
);
2188 event
= event
->group_leader
;
2190 perf_event_for_each_child(event
, func
);
2192 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
2193 perf_event_for_each_child(event
, func
);
2194 mutex_unlock(&ctx
->mutex
);
2197 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
2199 struct perf_event_context
*ctx
= event
->ctx
;
2204 if (!event
->attr
.sample_period
)
2207 size
= copy_from_user(&value
, arg
, sizeof(value
));
2208 if (size
!= sizeof(value
))
2214 raw_spin_lock_irq(&ctx
->lock
);
2215 if (event
->attr
.freq
) {
2216 if (value
> sysctl_perf_event_sample_rate
) {
2221 event
->attr
.sample_freq
= value
;
2223 event
->attr
.sample_period
= value
;
2224 event
->hw
.sample_period
= value
;
2227 raw_spin_unlock_irq(&ctx
->lock
);
2232 static const struct file_operations perf_fops
;
2234 static struct perf_event
*perf_fget_light(int fd
, int *fput_needed
)
2238 file
= fget_light(fd
, fput_needed
);
2240 return ERR_PTR(-EBADF
);
2242 if (file
->f_op
!= &perf_fops
) {
2243 fput_light(file
, *fput_needed
);
2245 return ERR_PTR(-EBADF
);
2248 return file
->private_data
;
2251 static int perf_event_set_output(struct perf_event
*event
,
2252 struct perf_event
*output_event
);
2253 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
2255 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2257 struct perf_event
*event
= file
->private_data
;
2258 void (*func
)(struct perf_event
*);
2262 case PERF_EVENT_IOC_ENABLE
:
2263 func
= perf_event_enable
;
2265 case PERF_EVENT_IOC_DISABLE
:
2266 func
= perf_event_disable
;
2268 case PERF_EVENT_IOC_RESET
:
2269 func
= perf_event_reset
;
2272 case PERF_EVENT_IOC_REFRESH
:
2273 return perf_event_refresh(event
, arg
);
2275 case PERF_EVENT_IOC_PERIOD
:
2276 return perf_event_period(event
, (u64 __user
*)arg
);
2278 case PERF_EVENT_IOC_SET_OUTPUT
:
2280 struct perf_event
*output_event
= NULL
;
2281 int fput_needed
= 0;
2285 output_event
= perf_fget_light(arg
, &fput_needed
);
2286 if (IS_ERR(output_event
))
2287 return PTR_ERR(output_event
);
2290 ret
= perf_event_set_output(event
, output_event
);
2292 fput_light(output_event
->filp
, fput_needed
);
2297 case PERF_EVENT_IOC_SET_FILTER
:
2298 return perf_event_set_filter(event
, (void __user
*)arg
);
2304 if (flags
& PERF_IOC_FLAG_GROUP
)
2305 perf_event_for_each(event
, func
);
2307 perf_event_for_each_child(event
, func
);
2312 int perf_event_task_enable(void)
2314 struct perf_event
*event
;
2316 mutex_lock(¤t
->perf_event_mutex
);
2317 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2318 perf_event_for_each_child(event
, perf_event_enable
);
2319 mutex_unlock(¤t
->perf_event_mutex
);
2324 int perf_event_task_disable(void)
2326 struct perf_event
*event
;
2328 mutex_lock(¤t
->perf_event_mutex
);
2329 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2330 perf_event_for_each_child(event
, perf_event_disable
);
2331 mutex_unlock(¤t
->perf_event_mutex
);
2336 #ifndef PERF_EVENT_INDEX_OFFSET
2337 # define PERF_EVENT_INDEX_OFFSET 0
2340 static int perf_event_index(struct perf_event
*event
)
2342 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
2345 return event
->hw
.idx
+ 1 - PERF_EVENT_INDEX_OFFSET
;
2349 * Callers need to ensure there can be no nesting of this function, otherwise
2350 * the seqlock logic goes bad. We can not serialize this because the arch
2351 * code calls this from NMI context.
2353 void perf_event_update_userpage(struct perf_event
*event
)
2355 struct perf_event_mmap_page
*userpg
;
2356 struct perf_mmap_data
*data
;
2359 data
= rcu_dereference(event
->data
);
2363 userpg
= data
->user_page
;
2366 * Disable preemption so as to not let the corresponding user-space
2367 * spin too long if we get preempted.
2372 userpg
->index
= perf_event_index(event
);
2373 userpg
->offset
= atomic64_read(&event
->count
);
2374 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
2375 userpg
->offset
-= atomic64_read(&event
->hw
.prev_count
);
2377 userpg
->time_enabled
= event
->total_time_enabled
+
2378 atomic64_read(&event
->child_total_time_enabled
);
2380 userpg
->time_running
= event
->total_time_running
+
2381 atomic64_read(&event
->child_total_time_running
);
2390 #ifndef CONFIG_PERF_USE_VMALLOC
2393 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2396 static struct page
*
2397 perf_mmap_to_page(struct perf_mmap_data
*data
, unsigned long pgoff
)
2399 if (pgoff
> data
->nr_pages
)
2403 return virt_to_page(data
->user_page
);
2405 return virt_to_page(data
->data_pages
[pgoff
- 1]);
2408 static void *perf_mmap_alloc_page(int cpu
)
2413 node
= (cpu
== -1) ? cpu
: cpu_to_node(cpu
);
2414 page
= alloc_pages_node(node
, GFP_KERNEL
| __GFP_ZERO
, 0);
2418 return page_address(page
);
2421 static struct perf_mmap_data
*
2422 perf_mmap_data_alloc(struct perf_event
*event
, int nr_pages
)
2424 struct perf_mmap_data
*data
;
2428 size
= sizeof(struct perf_mmap_data
);
2429 size
+= nr_pages
* sizeof(void *);
2431 data
= kzalloc(size
, GFP_KERNEL
);
2435 data
->user_page
= perf_mmap_alloc_page(event
->cpu
);
2436 if (!data
->user_page
)
2437 goto fail_user_page
;
2439 for (i
= 0; i
< nr_pages
; i
++) {
2440 data
->data_pages
[i
] = perf_mmap_alloc_page(event
->cpu
);
2441 if (!data
->data_pages
[i
])
2442 goto fail_data_pages
;
2445 data
->nr_pages
= nr_pages
;
2450 for (i
--; i
>= 0; i
--)
2451 free_page((unsigned long)data
->data_pages
[i
]);
2453 free_page((unsigned long)data
->user_page
);
2462 static void perf_mmap_free_page(unsigned long addr
)
2464 struct page
*page
= virt_to_page((void *)addr
);
2466 page
->mapping
= NULL
;
2470 static void perf_mmap_data_free(struct perf_mmap_data
*data
)
2474 perf_mmap_free_page((unsigned long)data
->user_page
);
2475 for (i
= 0; i
< data
->nr_pages
; i
++)
2476 perf_mmap_free_page((unsigned long)data
->data_pages
[i
]);
2480 static inline int page_order(struct perf_mmap_data
*data
)
2488 * Back perf_mmap() with vmalloc memory.
2490 * Required for architectures that have d-cache aliasing issues.
2493 static inline int page_order(struct perf_mmap_data
*data
)
2495 return data
->page_order
;
2498 static struct page
*
2499 perf_mmap_to_page(struct perf_mmap_data
*data
, unsigned long pgoff
)
2501 if (pgoff
> (1UL << page_order(data
)))
2504 return vmalloc_to_page((void *)data
->user_page
+ pgoff
* PAGE_SIZE
);
2507 static void perf_mmap_unmark_page(void *addr
)
2509 struct page
*page
= vmalloc_to_page(addr
);
2511 page
->mapping
= NULL
;
2514 static void perf_mmap_data_free_work(struct work_struct
*work
)
2516 struct perf_mmap_data
*data
;
2520 data
= container_of(work
, struct perf_mmap_data
, work
);
2521 nr
= 1 << page_order(data
);
2523 base
= data
->user_page
;
2524 for (i
= 0; i
< nr
+ 1; i
++)
2525 perf_mmap_unmark_page(base
+ (i
* PAGE_SIZE
));
2531 static void perf_mmap_data_free(struct perf_mmap_data
*data
)
2533 schedule_work(&data
->work
);
2536 static struct perf_mmap_data
*
2537 perf_mmap_data_alloc(struct perf_event
*event
, int nr_pages
)
2539 struct perf_mmap_data
*data
;
2543 size
= sizeof(struct perf_mmap_data
);
2544 size
+= sizeof(void *);
2546 data
= kzalloc(size
, GFP_KERNEL
);
2550 INIT_WORK(&data
->work
, perf_mmap_data_free_work
);
2552 all_buf
= vmalloc_user((nr_pages
+ 1) * PAGE_SIZE
);
2556 data
->user_page
= all_buf
;
2557 data
->data_pages
[0] = all_buf
+ PAGE_SIZE
;
2558 data
->page_order
= ilog2(nr_pages
);
2572 static unsigned long perf_data_size(struct perf_mmap_data
*data
)
2574 return data
->nr_pages
<< (PAGE_SHIFT
+ page_order(data
));
2577 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2579 struct perf_event
*event
= vma
->vm_file
->private_data
;
2580 struct perf_mmap_data
*data
;
2581 int ret
= VM_FAULT_SIGBUS
;
2583 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
2584 if (vmf
->pgoff
== 0)
2590 data
= rcu_dereference(event
->data
);
2594 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
2597 vmf
->page
= perf_mmap_to_page(data
, vmf
->pgoff
);
2601 get_page(vmf
->page
);
2602 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
2603 vmf
->page
->index
= vmf
->pgoff
;
2613 perf_mmap_data_init(struct perf_event
*event
, struct perf_mmap_data
*data
)
2615 long max_size
= perf_data_size(data
);
2617 if (event
->attr
.watermark
) {
2618 data
->watermark
= min_t(long, max_size
,
2619 event
->attr
.wakeup_watermark
);
2622 if (!data
->watermark
)
2623 data
->watermark
= max_size
/ 2;
2625 atomic_set(&data
->refcount
, 1);
2626 rcu_assign_pointer(event
->data
, data
);
2629 static void perf_mmap_data_free_rcu(struct rcu_head
*rcu_head
)
2631 struct perf_mmap_data
*data
;
2633 data
= container_of(rcu_head
, struct perf_mmap_data
, rcu_head
);
2634 perf_mmap_data_free(data
);
2637 static struct perf_mmap_data
*perf_mmap_data_get(struct perf_event
*event
)
2639 struct perf_mmap_data
*data
;
2642 data
= rcu_dereference(event
->data
);
2644 if (!atomic_inc_not_zero(&data
->refcount
))
2652 static void perf_mmap_data_put(struct perf_mmap_data
*data
)
2654 if (!atomic_dec_and_test(&data
->refcount
))
2657 call_rcu(&data
->rcu_head
, perf_mmap_data_free_rcu
);
2660 static void perf_mmap_open(struct vm_area_struct
*vma
)
2662 struct perf_event
*event
= vma
->vm_file
->private_data
;
2664 atomic_inc(&event
->mmap_count
);
2667 static void perf_mmap_close(struct vm_area_struct
*vma
)
2669 struct perf_event
*event
= vma
->vm_file
->private_data
;
2671 if (atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
)) {
2672 unsigned long size
= perf_data_size(event
->data
);
2673 struct user_struct
*user
= event
->mmap_user
;
2674 struct perf_mmap_data
*data
= event
->data
;
2676 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &user
->locked_vm
);
2677 vma
->vm_mm
->locked_vm
-= event
->mmap_locked
;
2678 rcu_assign_pointer(event
->data
, NULL
);
2679 mutex_unlock(&event
->mmap_mutex
);
2681 perf_mmap_data_put(data
);
2686 static const struct vm_operations_struct perf_mmap_vmops
= {
2687 .open
= perf_mmap_open
,
2688 .close
= perf_mmap_close
,
2689 .fault
= perf_mmap_fault
,
2690 .page_mkwrite
= perf_mmap_fault
,
2693 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2695 struct perf_event
*event
= file
->private_data
;
2696 unsigned long user_locked
, user_lock_limit
;
2697 struct user_struct
*user
= current_user();
2698 unsigned long locked
, lock_limit
;
2699 struct perf_mmap_data
*data
;
2700 unsigned long vma_size
;
2701 unsigned long nr_pages
;
2702 long user_extra
, extra
;
2706 * Don't allow mmap() of inherited per-task counters. This would
2707 * create a performance issue due to all children writing to the
2710 if (event
->cpu
== -1 && event
->attr
.inherit
)
2713 if (!(vma
->vm_flags
& VM_SHARED
))
2716 vma_size
= vma
->vm_end
- vma
->vm_start
;
2717 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
2720 * If we have data pages ensure they're a power-of-two number, so we
2721 * can do bitmasks instead of modulo.
2723 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
2726 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
2729 if (vma
->vm_pgoff
!= 0)
2732 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2733 mutex_lock(&event
->mmap_mutex
);
2735 if (event
->data
->nr_pages
== nr_pages
)
2736 atomic_inc(&event
->data
->refcount
);
2742 user_extra
= nr_pages
+ 1;
2743 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
2746 * Increase the limit linearly with more CPUs:
2748 user_lock_limit
*= num_online_cpus();
2750 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
2753 if (user_locked
> user_lock_limit
)
2754 extra
= user_locked
- user_lock_limit
;
2756 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
2757 lock_limit
>>= PAGE_SHIFT
;
2758 locked
= vma
->vm_mm
->locked_vm
+ extra
;
2760 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
2761 !capable(CAP_IPC_LOCK
)) {
2766 WARN_ON(event
->data
);
2768 data
= perf_mmap_data_alloc(event
, nr_pages
);
2774 perf_mmap_data_init(event
, data
);
2775 if (vma
->vm_flags
& VM_WRITE
)
2776 event
->data
->writable
= 1;
2778 atomic_long_add(user_extra
, &user
->locked_vm
);
2779 event
->mmap_locked
= extra
;
2780 event
->mmap_user
= get_current_user();
2781 vma
->vm_mm
->locked_vm
+= event
->mmap_locked
;
2785 atomic_inc(&event
->mmap_count
);
2786 mutex_unlock(&event
->mmap_mutex
);
2788 vma
->vm_flags
|= VM_RESERVED
;
2789 vma
->vm_ops
= &perf_mmap_vmops
;
2794 static int perf_fasync(int fd
, struct file
*filp
, int on
)
2796 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
2797 struct perf_event
*event
= filp
->private_data
;
2800 mutex_lock(&inode
->i_mutex
);
2801 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
2802 mutex_unlock(&inode
->i_mutex
);
2810 static const struct file_operations perf_fops
= {
2811 .llseek
= no_llseek
,
2812 .release
= perf_release
,
2815 .unlocked_ioctl
= perf_ioctl
,
2816 .compat_ioctl
= perf_ioctl
,
2818 .fasync
= perf_fasync
,
2824 * If there's data, ensure we set the poll() state and publish everything
2825 * to user-space before waking everybody up.
2828 void perf_event_wakeup(struct perf_event
*event
)
2830 wake_up_all(&event
->waitq
);
2832 if (event
->pending_kill
) {
2833 kill_fasync(&event
->fasync
, SIGIO
, event
->pending_kill
);
2834 event
->pending_kill
= 0;
2841 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2843 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2844 * single linked list and use cmpxchg() to add entries lockless.
2847 static void perf_pending_event(struct perf_pending_entry
*entry
)
2849 struct perf_event
*event
= container_of(entry
,
2850 struct perf_event
, pending
);
2852 if (event
->pending_disable
) {
2853 event
->pending_disable
= 0;
2854 __perf_event_disable(event
);
2857 if (event
->pending_wakeup
) {
2858 event
->pending_wakeup
= 0;
2859 perf_event_wakeup(event
);
2863 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2865 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
2869 static void perf_pending_queue(struct perf_pending_entry
*entry
,
2870 void (*func
)(struct perf_pending_entry
*))
2872 struct perf_pending_entry
**head
;
2874 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
2879 head
= &get_cpu_var(perf_pending_head
);
2882 entry
->next
= *head
;
2883 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
2885 set_perf_event_pending();
2887 put_cpu_var(perf_pending_head
);
2890 static int __perf_pending_run(void)
2892 struct perf_pending_entry
*list
;
2895 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
2896 while (list
!= PENDING_TAIL
) {
2897 void (*func
)(struct perf_pending_entry
*);
2898 struct perf_pending_entry
*entry
= list
;
2905 * Ensure we observe the unqueue before we issue the wakeup,
2906 * so that we won't be waiting forever.
2907 * -- see perf_not_pending().
2918 static inline int perf_not_pending(struct perf_event
*event
)
2921 * If we flush on whatever cpu we run, there is a chance we don't
2925 __perf_pending_run();
2929 * Ensure we see the proper queue state before going to sleep
2930 * so that we do not miss the wakeup. -- see perf_pending_handle()
2933 return event
->pending
.next
== NULL
;
2936 static void perf_pending_sync(struct perf_event
*event
)
2938 wait_event(event
->waitq
, perf_not_pending(event
));
2941 void perf_event_do_pending(void)
2943 __perf_pending_run();
2947 * Callchain support -- arch specific
2950 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
2956 void perf_arch_fetch_caller_regs(struct pt_regs
*regs
, unsigned long ip
, int skip
)
2962 * We assume there is only KVM supporting the callbacks.
2963 * Later on, we might change it to a list if there is
2964 * another virtualization implementation supporting the callbacks.
2966 struct perf_guest_info_callbacks
*perf_guest_cbs
;
2968 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
2970 perf_guest_cbs
= cbs
;
2973 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks
);
2975 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
2977 perf_guest_cbs
= NULL
;
2980 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks
);
2985 static bool perf_output_space(struct perf_mmap_data
*data
, unsigned long tail
,
2986 unsigned long offset
, unsigned long head
)
2990 if (!data
->writable
)
2993 mask
= perf_data_size(data
) - 1;
2995 offset
= (offset
- tail
) & mask
;
2996 head
= (head
- tail
) & mask
;
2998 if ((int)(head
- offset
) < 0)
3004 static void perf_output_wakeup(struct perf_output_handle
*handle
)
3006 atomic_set(&handle
->data
->poll
, POLL_IN
);
3009 handle
->event
->pending_wakeup
= 1;
3010 perf_pending_queue(&handle
->event
->pending
,
3011 perf_pending_event
);
3013 perf_event_wakeup(handle
->event
);
3017 * We need to ensure a later event_id doesn't publish a head when a former
3018 * event isn't done writing. However since we need to deal with NMIs we
3019 * cannot fully serialize things.
3021 * We only publish the head (and generate a wakeup) when the outer-most
3024 static void perf_output_get_handle(struct perf_output_handle
*handle
)
3026 struct perf_mmap_data
*data
= handle
->data
;
3029 local_inc(&data
->nest
);
3030 handle
->wakeup
= local_read(&data
->wakeup
);
3033 static void perf_output_put_handle(struct perf_output_handle
*handle
)
3035 struct perf_mmap_data
*data
= handle
->data
;
3039 head
= local_read(&data
->head
);
3042 * IRQ/NMI can happen here, which means we can miss a head update.
3045 if (!local_dec_and_test(&data
->nest
))
3049 * Publish the known good head. Rely on the full barrier implied
3050 * by atomic_dec_and_test() order the data->head read and this
3053 data
->user_page
->data_head
= head
;
3056 * Now check if we missed an update, rely on the (compiler)
3057 * barrier in atomic_dec_and_test() to re-read data->head.
3059 if (unlikely(head
!= local_read(&data
->head
))) {
3060 local_inc(&data
->nest
);
3064 if (handle
->wakeup
!= local_read(&data
->wakeup
))
3065 perf_output_wakeup(handle
);
3071 __always_inline
void perf_output_copy(struct perf_output_handle
*handle
,
3072 const void *buf
, unsigned int len
)
3075 unsigned long size
= min_t(unsigned long, handle
->size
, len
);
3077 memcpy(handle
->addr
, buf
, size
);
3080 handle
->addr
+= size
;
3082 handle
->size
-= size
;
3083 if (!handle
->size
) {
3084 struct perf_mmap_data
*data
= handle
->data
;
3087 handle
->page
&= data
->nr_pages
- 1;
3088 handle
->addr
= data
->data_pages
[handle
->page
];
3089 handle
->size
= PAGE_SIZE
<< page_order(data
);
3094 int perf_output_begin(struct perf_output_handle
*handle
,
3095 struct perf_event
*event
, unsigned int size
,
3096 int nmi
, int sample
)
3098 struct perf_mmap_data
*data
;
3099 unsigned long tail
, offset
, head
;
3102 struct perf_event_header header
;
3109 * For inherited events we send all the output towards the parent.
3112 event
= event
->parent
;
3114 data
= rcu_dereference(event
->data
);
3118 handle
->data
= data
;
3119 handle
->event
= event
;
3121 handle
->sample
= sample
;
3123 if (!data
->nr_pages
)
3126 have_lost
= local_read(&data
->lost
);
3128 size
+= sizeof(lost_event
);
3130 perf_output_get_handle(handle
);
3134 * Userspace could choose to issue a mb() before updating the
3135 * tail pointer. So that all reads will be completed before the
3138 tail
= ACCESS_ONCE(data
->user_page
->data_tail
);
3140 offset
= head
= local_read(&data
->head
);
3142 if (unlikely(!perf_output_space(data
, tail
, offset
, head
)))
3144 } while (local_cmpxchg(&data
->head
, offset
, head
) != offset
);
3146 if (head
- local_read(&data
->wakeup
) > data
->watermark
)
3147 local_add(data
->watermark
, &data
->wakeup
);
3149 handle
->page
= offset
>> (PAGE_SHIFT
+ page_order(data
));
3150 handle
->page
&= data
->nr_pages
- 1;
3151 handle
->size
= offset
& ((PAGE_SIZE
<< page_order(data
)) - 1);
3152 handle
->addr
= data
->data_pages
[handle
->page
];
3153 handle
->addr
+= handle
->size
;
3154 handle
->size
= (PAGE_SIZE
<< page_order(data
)) - handle
->size
;
3157 lost_event
.header
.type
= PERF_RECORD_LOST
;
3158 lost_event
.header
.misc
= 0;
3159 lost_event
.header
.size
= sizeof(lost_event
);
3160 lost_event
.id
= event
->id
;
3161 lost_event
.lost
= local_xchg(&data
->lost
, 0);
3163 perf_output_put(handle
, lost_event
);
3169 local_inc(&data
->lost
);
3170 perf_output_put_handle(handle
);
3177 void perf_output_end(struct perf_output_handle
*handle
)
3179 struct perf_event
*event
= handle
->event
;
3180 struct perf_mmap_data
*data
= handle
->data
;
3182 int wakeup_events
= event
->attr
.wakeup_events
;
3184 if (handle
->sample
&& wakeup_events
) {
3185 int events
= local_inc_return(&data
->events
);
3186 if (events
>= wakeup_events
) {
3187 local_sub(wakeup_events
, &data
->events
);
3188 local_inc(&data
->wakeup
);
3192 perf_output_put_handle(handle
);
3196 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
3199 * only top level events have the pid namespace they were created in
3202 event
= event
->parent
;
3204 return task_tgid_nr_ns(p
, event
->ns
);
3207 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
3210 * only top level events have the pid namespace they were created in
3213 event
= event
->parent
;
3215 return task_pid_nr_ns(p
, event
->ns
);
3218 static void perf_output_read_one(struct perf_output_handle
*handle
,
3219 struct perf_event
*event
)
3221 u64 read_format
= event
->attr
.read_format
;
3225 values
[n
++] = atomic64_read(&event
->count
);
3226 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
3227 values
[n
++] = event
->total_time_enabled
+
3228 atomic64_read(&event
->child_total_time_enabled
);
3230 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
3231 values
[n
++] = event
->total_time_running
+
3232 atomic64_read(&event
->child_total_time_running
);
3234 if (read_format
& PERF_FORMAT_ID
)
3235 values
[n
++] = primary_event_id(event
);
3237 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3241 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3243 static void perf_output_read_group(struct perf_output_handle
*handle
,
3244 struct perf_event
*event
)
3246 struct perf_event
*leader
= event
->group_leader
, *sub
;
3247 u64 read_format
= event
->attr
.read_format
;
3251 values
[n
++] = 1 + leader
->nr_siblings
;
3253 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3254 values
[n
++] = leader
->total_time_enabled
;
3256 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3257 values
[n
++] = leader
->total_time_running
;
3259 if (leader
!= event
)
3260 leader
->pmu
->read(leader
);
3262 values
[n
++] = atomic64_read(&leader
->count
);
3263 if (read_format
& PERF_FORMAT_ID
)
3264 values
[n
++] = primary_event_id(leader
);
3266 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3268 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3272 sub
->pmu
->read(sub
);
3274 values
[n
++] = atomic64_read(&sub
->count
);
3275 if (read_format
& PERF_FORMAT_ID
)
3276 values
[n
++] = primary_event_id(sub
);
3278 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3282 static void perf_output_read(struct perf_output_handle
*handle
,
3283 struct perf_event
*event
)
3285 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
3286 perf_output_read_group(handle
, event
);
3288 perf_output_read_one(handle
, event
);
3291 void perf_output_sample(struct perf_output_handle
*handle
,
3292 struct perf_event_header
*header
,
3293 struct perf_sample_data
*data
,
3294 struct perf_event
*event
)
3296 u64 sample_type
= data
->type
;
3298 perf_output_put(handle
, *header
);
3300 if (sample_type
& PERF_SAMPLE_IP
)
3301 perf_output_put(handle
, data
->ip
);
3303 if (sample_type
& PERF_SAMPLE_TID
)
3304 perf_output_put(handle
, data
->tid_entry
);
3306 if (sample_type
& PERF_SAMPLE_TIME
)
3307 perf_output_put(handle
, data
->time
);
3309 if (sample_type
& PERF_SAMPLE_ADDR
)
3310 perf_output_put(handle
, data
->addr
);
3312 if (sample_type
& PERF_SAMPLE_ID
)
3313 perf_output_put(handle
, data
->id
);
3315 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
3316 perf_output_put(handle
, data
->stream_id
);
3318 if (sample_type
& PERF_SAMPLE_CPU
)
3319 perf_output_put(handle
, data
->cpu_entry
);
3321 if (sample_type
& PERF_SAMPLE_PERIOD
)
3322 perf_output_put(handle
, data
->period
);
3324 if (sample_type
& PERF_SAMPLE_READ
)
3325 perf_output_read(handle
, event
);
3327 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3328 if (data
->callchain
) {
3331 if (data
->callchain
)
3332 size
+= data
->callchain
->nr
;
3334 size
*= sizeof(u64
);
3336 perf_output_copy(handle
, data
->callchain
, size
);
3339 perf_output_put(handle
, nr
);
3343 if (sample_type
& PERF_SAMPLE_RAW
) {
3345 perf_output_put(handle
, data
->raw
->size
);
3346 perf_output_copy(handle
, data
->raw
->data
,
3353 .size
= sizeof(u32
),
3356 perf_output_put(handle
, raw
);
3361 void perf_prepare_sample(struct perf_event_header
*header
,
3362 struct perf_sample_data
*data
,
3363 struct perf_event
*event
,
3364 struct pt_regs
*regs
)
3366 u64 sample_type
= event
->attr
.sample_type
;
3368 data
->type
= sample_type
;
3370 header
->type
= PERF_RECORD_SAMPLE
;
3371 header
->size
= sizeof(*header
);
3374 header
->misc
|= perf_misc_flags(regs
);
3376 if (sample_type
& PERF_SAMPLE_IP
) {
3377 data
->ip
= perf_instruction_pointer(regs
);
3379 header
->size
+= sizeof(data
->ip
);
3382 if (sample_type
& PERF_SAMPLE_TID
) {
3383 /* namespace issues */
3384 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
3385 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
3387 header
->size
+= sizeof(data
->tid_entry
);
3390 if (sample_type
& PERF_SAMPLE_TIME
) {
3391 data
->time
= perf_clock();
3393 header
->size
+= sizeof(data
->time
);
3396 if (sample_type
& PERF_SAMPLE_ADDR
)
3397 header
->size
+= sizeof(data
->addr
);
3399 if (sample_type
& PERF_SAMPLE_ID
) {
3400 data
->id
= primary_event_id(event
);
3402 header
->size
+= sizeof(data
->id
);
3405 if (sample_type
& PERF_SAMPLE_STREAM_ID
) {
3406 data
->stream_id
= event
->id
;
3408 header
->size
+= sizeof(data
->stream_id
);
3411 if (sample_type
& PERF_SAMPLE_CPU
) {
3412 data
->cpu_entry
.cpu
= raw_smp_processor_id();
3413 data
->cpu_entry
.reserved
= 0;
3415 header
->size
+= sizeof(data
->cpu_entry
);
3418 if (sample_type
& PERF_SAMPLE_PERIOD
)
3419 header
->size
+= sizeof(data
->period
);
3421 if (sample_type
& PERF_SAMPLE_READ
)
3422 header
->size
+= perf_event_read_size(event
);
3424 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3427 data
->callchain
= perf_callchain(regs
);
3429 if (data
->callchain
)
3430 size
+= data
->callchain
->nr
;
3432 header
->size
+= size
* sizeof(u64
);
3435 if (sample_type
& PERF_SAMPLE_RAW
) {
3436 int size
= sizeof(u32
);
3439 size
+= data
->raw
->size
;
3441 size
+= sizeof(u32
);
3443 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
3444 header
->size
+= size
;
3448 static void perf_event_output(struct perf_event
*event
, int nmi
,
3449 struct perf_sample_data
*data
,
3450 struct pt_regs
*regs
)
3452 struct perf_output_handle handle
;
3453 struct perf_event_header header
;
3455 perf_prepare_sample(&header
, data
, event
, regs
);
3457 if (perf_output_begin(&handle
, event
, header
.size
, nmi
, 1))
3460 perf_output_sample(&handle
, &header
, data
, event
);
3462 perf_output_end(&handle
);
3469 struct perf_read_event
{
3470 struct perf_event_header header
;
3477 perf_event_read_event(struct perf_event
*event
,
3478 struct task_struct
*task
)
3480 struct perf_output_handle handle
;
3481 struct perf_read_event read_event
= {
3483 .type
= PERF_RECORD_READ
,
3485 .size
= sizeof(read_event
) + perf_event_read_size(event
),
3487 .pid
= perf_event_pid(event
, task
),
3488 .tid
= perf_event_tid(event
, task
),
3492 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
, 0, 0);
3496 perf_output_put(&handle
, read_event
);
3497 perf_output_read(&handle
, event
);
3499 perf_output_end(&handle
);
3503 * task tracking -- fork/exit
3505 * enabled by: attr.comm | attr.mmap | attr.task
3508 struct perf_task_event
{
3509 struct task_struct
*task
;
3510 struct perf_event_context
*task_ctx
;
3513 struct perf_event_header header
;
3523 static void perf_event_task_output(struct perf_event
*event
,
3524 struct perf_task_event
*task_event
)
3526 struct perf_output_handle handle
;
3527 struct task_struct
*task
= task_event
->task
;
3530 size
= task_event
->event_id
.header
.size
;
3531 ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3536 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
3537 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
3539 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
3540 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
3542 perf_output_put(&handle
, task_event
->event_id
);
3544 perf_output_end(&handle
);
3547 static int perf_event_task_match(struct perf_event
*event
)
3549 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3552 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3555 if (event
->attr
.comm
|| event
->attr
.mmap
|| event
->attr
.task
)
3561 static void perf_event_task_ctx(struct perf_event_context
*ctx
,
3562 struct perf_task_event
*task_event
)
3564 struct perf_event
*event
;
3566 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3567 if (perf_event_task_match(event
))
3568 perf_event_task_output(event
, task_event
);
3572 static void perf_event_task_event(struct perf_task_event
*task_event
)
3574 struct perf_cpu_context
*cpuctx
;
3575 struct perf_event_context
*ctx
= task_event
->task_ctx
;
3578 cpuctx
= &get_cpu_var(perf_cpu_context
);
3579 perf_event_task_ctx(&cpuctx
->ctx
, task_event
);
3581 ctx
= rcu_dereference(current
->perf_event_ctxp
);
3583 perf_event_task_ctx(ctx
, task_event
);
3584 put_cpu_var(perf_cpu_context
);
3588 static void perf_event_task(struct task_struct
*task
,
3589 struct perf_event_context
*task_ctx
,
3592 struct perf_task_event task_event
;
3594 if (!atomic_read(&nr_comm_events
) &&
3595 !atomic_read(&nr_mmap_events
) &&
3596 !atomic_read(&nr_task_events
))
3599 task_event
= (struct perf_task_event
){
3601 .task_ctx
= task_ctx
,
3604 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
3606 .size
= sizeof(task_event
.event_id
),
3612 .time
= perf_clock(),
3616 perf_event_task_event(&task_event
);
3619 void perf_event_fork(struct task_struct
*task
)
3621 perf_event_task(task
, NULL
, 1);
3628 struct perf_comm_event
{
3629 struct task_struct
*task
;
3634 struct perf_event_header header
;
3641 static void perf_event_comm_output(struct perf_event
*event
,
3642 struct perf_comm_event
*comm_event
)
3644 struct perf_output_handle handle
;
3645 int size
= comm_event
->event_id
.header
.size
;
3646 int ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3651 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
3652 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
3654 perf_output_put(&handle
, comm_event
->event_id
);
3655 perf_output_copy(&handle
, comm_event
->comm
,
3656 comm_event
->comm_size
);
3657 perf_output_end(&handle
);
3660 static int perf_event_comm_match(struct perf_event
*event
)
3662 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3665 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3668 if (event
->attr
.comm
)
3674 static void perf_event_comm_ctx(struct perf_event_context
*ctx
,
3675 struct perf_comm_event
*comm_event
)
3677 struct perf_event
*event
;
3679 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3680 if (perf_event_comm_match(event
))
3681 perf_event_comm_output(event
, comm_event
);
3685 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
3687 struct perf_cpu_context
*cpuctx
;
3688 struct perf_event_context
*ctx
;
3690 char comm
[TASK_COMM_LEN
];
3692 memset(comm
, 0, sizeof(comm
));
3693 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
3694 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
3696 comm_event
->comm
= comm
;
3697 comm_event
->comm_size
= size
;
3699 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
3702 cpuctx
= &get_cpu_var(perf_cpu_context
);
3703 perf_event_comm_ctx(&cpuctx
->ctx
, comm_event
);
3704 ctx
= rcu_dereference(current
->perf_event_ctxp
);
3706 perf_event_comm_ctx(ctx
, comm_event
);
3707 put_cpu_var(perf_cpu_context
);
3711 void perf_event_comm(struct task_struct
*task
)
3713 struct perf_comm_event comm_event
;
3715 if (task
->perf_event_ctxp
)
3716 perf_event_enable_on_exec(task
);
3718 if (!atomic_read(&nr_comm_events
))
3721 comm_event
= (struct perf_comm_event
){
3727 .type
= PERF_RECORD_COMM
,
3736 perf_event_comm_event(&comm_event
);
3743 struct perf_mmap_event
{
3744 struct vm_area_struct
*vma
;
3746 const char *file_name
;
3750 struct perf_event_header header
;
3760 static void perf_event_mmap_output(struct perf_event
*event
,
3761 struct perf_mmap_event
*mmap_event
)
3763 struct perf_output_handle handle
;
3764 int size
= mmap_event
->event_id
.header
.size
;
3765 int ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3770 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
3771 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
3773 perf_output_put(&handle
, mmap_event
->event_id
);
3774 perf_output_copy(&handle
, mmap_event
->file_name
,
3775 mmap_event
->file_size
);
3776 perf_output_end(&handle
);
3779 static int perf_event_mmap_match(struct perf_event
*event
,
3780 struct perf_mmap_event
*mmap_event
)
3782 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3785 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3788 if (event
->attr
.mmap
)
3794 static void perf_event_mmap_ctx(struct perf_event_context
*ctx
,
3795 struct perf_mmap_event
*mmap_event
)
3797 struct perf_event
*event
;
3799 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3800 if (perf_event_mmap_match(event
, mmap_event
))
3801 perf_event_mmap_output(event
, mmap_event
);
3805 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
3807 struct perf_cpu_context
*cpuctx
;
3808 struct perf_event_context
*ctx
;
3809 struct vm_area_struct
*vma
= mmap_event
->vma
;
3810 struct file
*file
= vma
->vm_file
;
3816 memset(tmp
, 0, sizeof(tmp
));
3820 * d_path works from the end of the buffer backwards, so we
3821 * need to add enough zero bytes after the string to handle
3822 * the 64bit alignment we do later.
3824 buf
= kzalloc(PATH_MAX
+ sizeof(u64
), GFP_KERNEL
);
3826 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
3829 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
3831 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
3835 if (arch_vma_name(mmap_event
->vma
)) {
3836 name
= strncpy(tmp
, arch_vma_name(mmap_event
->vma
),
3842 name
= strncpy(tmp
, "[vdso]", sizeof(tmp
));
3846 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
3851 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
3853 mmap_event
->file_name
= name
;
3854 mmap_event
->file_size
= size
;
3856 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
3859 cpuctx
= &get_cpu_var(perf_cpu_context
);
3860 perf_event_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
3861 ctx
= rcu_dereference(current
->perf_event_ctxp
);
3863 perf_event_mmap_ctx(ctx
, mmap_event
);
3864 put_cpu_var(perf_cpu_context
);
3870 void __perf_event_mmap(struct vm_area_struct
*vma
)
3872 struct perf_mmap_event mmap_event
;
3874 if (!atomic_read(&nr_mmap_events
))
3877 mmap_event
= (struct perf_mmap_event
){
3883 .type
= PERF_RECORD_MMAP
,
3884 .misc
= PERF_RECORD_MISC_USER
,
3889 .start
= vma
->vm_start
,
3890 .len
= vma
->vm_end
- vma
->vm_start
,
3891 .pgoff
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
,
3895 perf_event_mmap_event(&mmap_event
);
3899 * IRQ throttle logging
3902 static void perf_log_throttle(struct perf_event
*event
, int enable
)
3904 struct perf_output_handle handle
;
3908 struct perf_event_header header
;
3912 } throttle_event
= {
3914 .type
= PERF_RECORD_THROTTLE
,
3916 .size
= sizeof(throttle_event
),
3918 .time
= perf_clock(),
3919 .id
= primary_event_id(event
),
3920 .stream_id
= event
->id
,
3924 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
3926 ret
= perf_output_begin(&handle
, event
, sizeof(throttle_event
), 1, 0);
3930 perf_output_put(&handle
, throttle_event
);
3931 perf_output_end(&handle
);
3935 * Generic event overflow handling, sampling.
3938 static int __perf_event_overflow(struct perf_event
*event
, int nmi
,
3939 int throttle
, struct perf_sample_data
*data
,
3940 struct pt_regs
*regs
)
3942 int events
= atomic_read(&event
->event_limit
);
3943 struct hw_perf_event
*hwc
= &event
->hw
;
3946 throttle
= (throttle
&& event
->pmu
->unthrottle
!= NULL
);
3951 if (hwc
->interrupts
!= MAX_INTERRUPTS
) {
3953 if (HZ
* hwc
->interrupts
>
3954 (u64
)sysctl_perf_event_sample_rate
) {
3955 hwc
->interrupts
= MAX_INTERRUPTS
;
3956 perf_log_throttle(event
, 0);
3961 * Keep re-disabling events even though on the previous
3962 * pass we disabled it - just in case we raced with a
3963 * sched-in and the event got enabled again:
3969 if (event
->attr
.freq
) {
3970 u64 now
= perf_clock();
3971 s64 delta
= now
- hwc
->freq_time_stamp
;
3973 hwc
->freq_time_stamp
= now
;
3975 if (delta
> 0 && delta
< 2*TICK_NSEC
)
3976 perf_adjust_period(event
, delta
, hwc
->last_period
);
3980 * XXX event_limit might not quite work as expected on inherited
3984 event
->pending_kill
= POLL_IN
;
3985 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
3987 event
->pending_kill
= POLL_HUP
;
3989 event
->pending_disable
= 1;
3990 perf_pending_queue(&event
->pending
,
3991 perf_pending_event
);
3993 perf_event_disable(event
);
3996 if (event
->overflow_handler
)
3997 event
->overflow_handler(event
, nmi
, data
, regs
);
3999 perf_event_output(event
, nmi
, data
, regs
);
4004 int perf_event_overflow(struct perf_event
*event
, int nmi
,
4005 struct perf_sample_data
*data
,
4006 struct pt_regs
*regs
)
4008 return __perf_event_overflow(event
, nmi
, 1, data
, regs
);
4012 * Generic software event infrastructure
4016 * We directly increment event->count and keep a second value in
4017 * event->hw.period_left to count intervals. This period event
4018 * is kept in the range [-sample_period, 0] so that we can use the
4022 static u64
perf_swevent_set_period(struct perf_event
*event
)
4024 struct hw_perf_event
*hwc
= &event
->hw
;
4025 u64 period
= hwc
->last_period
;
4029 hwc
->last_period
= hwc
->sample_period
;
4032 old
= val
= atomic64_read(&hwc
->period_left
);
4036 nr
= div64_u64(period
+ val
, period
);
4037 offset
= nr
* period
;
4039 if (atomic64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
4045 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
4046 int nmi
, struct perf_sample_data
*data
,
4047 struct pt_regs
*regs
)
4049 struct hw_perf_event
*hwc
= &event
->hw
;
4052 data
->period
= event
->hw
.last_period
;
4054 overflow
= perf_swevent_set_period(event
);
4056 if (hwc
->interrupts
== MAX_INTERRUPTS
)
4059 for (; overflow
; overflow
--) {
4060 if (__perf_event_overflow(event
, nmi
, throttle
,
4063 * We inhibit the overflow from happening when
4064 * hwc->interrupts == MAX_INTERRUPTS.
4072 static void perf_swevent_add(struct perf_event
*event
, u64 nr
,
4073 int nmi
, struct perf_sample_data
*data
,
4074 struct pt_regs
*regs
)
4076 struct hw_perf_event
*hwc
= &event
->hw
;
4078 atomic64_add(nr
, &event
->count
);
4083 if (!hwc
->sample_period
)
4086 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
4087 return perf_swevent_overflow(event
, 1, nmi
, data
, regs
);
4089 if (atomic64_add_negative(nr
, &hwc
->period_left
))
4092 perf_swevent_overflow(event
, 0, nmi
, data
, regs
);
4095 static int perf_exclude_event(struct perf_event
*event
,
4096 struct pt_regs
*regs
)
4099 if (event
->attr
.exclude_user
&& user_mode(regs
))
4102 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
4109 static int perf_swevent_match(struct perf_event
*event
,
4110 enum perf_type_id type
,
4112 struct perf_sample_data
*data
,
4113 struct pt_regs
*regs
)
4115 if (event
->attr
.type
!= type
)
4118 if (event
->attr
.config
!= event_id
)
4121 if (perf_exclude_event(event
, regs
))
4127 static inline u64
swevent_hash(u64 type
, u32 event_id
)
4129 u64 val
= event_id
| (type
<< 32);
4131 return hash_64(val
, SWEVENT_HLIST_BITS
);
4134 static inline struct hlist_head
*
4135 __find_swevent_head(struct swevent_hlist
*hlist
, u64 type
, u32 event_id
)
4137 u64 hash
= swevent_hash(type
, event_id
);
4139 return &hlist
->heads
[hash
];
4142 /* For the read side: events when they trigger */
4143 static inline struct hlist_head
*
4144 find_swevent_head_rcu(struct perf_cpu_context
*ctx
, u64 type
, u32 event_id
)
4146 struct swevent_hlist
*hlist
;
4148 hlist
= rcu_dereference(ctx
->swevent_hlist
);
4152 return __find_swevent_head(hlist
, type
, event_id
);
4155 /* For the event head insertion and removal in the hlist */
4156 static inline struct hlist_head
*
4157 find_swevent_head(struct perf_cpu_context
*ctx
, struct perf_event
*event
)
4159 struct swevent_hlist
*hlist
;
4160 u32 event_id
= event
->attr
.config
;
4161 u64 type
= event
->attr
.type
;
4164 * Event scheduling is always serialized against hlist allocation
4165 * and release. Which makes the protected version suitable here.
4166 * The context lock guarantees that.
4168 hlist
= rcu_dereference_protected(ctx
->swevent_hlist
,
4169 lockdep_is_held(&event
->ctx
->lock
));
4173 return __find_swevent_head(hlist
, type
, event_id
);
4176 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
4178 struct perf_sample_data
*data
,
4179 struct pt_regs
*regs
)
4181 struct perf_cpu_context
*cpuctx
;
4182 struct perf_event
*event
;
4183 struct hlist_node
*node
;
4184 struct hlist_head
*head
;
4186 cpuctx
= &__get_cpu_var(perf_cpu_context
);
4190 head
= find_swevent_head_rcu(cpuctx
, type
, event_id
);
4195 hlist_for_each_entry_rcu(event
, node
, head
, hlist_entry
) {
4196 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
4197 perf_swevent_add(event
, nr
, nmi
, data
, regs
);
4203 int perf_swevent_get_recursion_context(void)
4205 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
4212 else if (in_softirq())
4217 if (cpuctx
->recursion
[rctx
])
4220 cpuctx
->recursion
[rctx
]++;
4225 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
4227 void perf_swevent_put_recursion_context(int rctx
)
4229 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
4231 cpuctx
->recursion
[rctx
]--;
4233 EXPORT_SYMBOL_GPL(perf_swevent_put_recursion_context
);
4236 void __perf_sw_event(u32 event_id
, u64 nr
, int nmi
,
4237 struct pt_regs
*regs
, u64 addr
)
4239 struct perf_sample_data data
;
4242 preempt_disable_notrace();
4243 rctx
= perf_swevent_get_recursion_context();
4247 perf_sample_data_init(&data
, addr
);
4249 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, nmi
, &data
, regs
);
4251 perf_swevent_put_recursion_context(rctx
);
4252 preempt_enable_notrace();
4255 static void perf_swevent_read(struct perf_event
*event
)
4259 static int perf_swevent_enable(struct perf_event
*event
)
4261 struct hw_perf_event
*hwc
= &event
->hw
;
4262 struct perf_cpu_context
*cpuctx
;
4263 struct hlist_head
*head
;
4265 cpuctx
= &__get_cpu_var(perf_cpu_context
);
4267 if (hwc
->sample_period
) {
4268 hwc
->last_period
= hwc
->sample_period
;
4269 perf_swevent_set_period(event
);
4272 head
= find_swevent_head(cpuctx
, event
);
4273 if (WARN_ON_ONCE(!head
))
4276 hlist_add_head_rcu(&event
->hlist_entry
, head
);
4281 static void perf_swevent_disable(struct perf_event
*event
)
4283 hlist_del_rcu(&event
->hlist_entry
);
4286 static void perf_swevent_void(struct perf_event
*event
)
4290 static int perf_swevent_int(struct perf_event
*event
)
4295 static const struct pmu perf_ops_generic
= {
4296 .enable
= perf_swevent_enable
,
4297 .disable
= perf_swevent_disable
,
4298 .start
= perf_swevent_int
,
4299 .stop
= perf_swevent_void
,
4300 .read
= perf_swevent_read
,
4301 .unthrottle
= perf_swevent_void
, /* hwc->interrupts already reset */
4305 * hrtimer based swevent callback
4308 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
4310 enum hrtimer_restart ret
= HRTIMER_RESTART
;
4311 struct perf_sample_data data
;
4312 struct pt_regs
*regs
;
4313 struct perf_event
*event
;
4316 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
4317 event
->pmu
->read(event
);
4319 perf_sample_data_init(&data
, 0);
4320 data
.period
= event
->hw
.last_period
;
4321 regs
= get_irq_regs();
4323 if (regs
&& !perf_exclude_event(event
, regs
)) {
4324 if (!(event
->attr
.exclude_idle
&& current
->pid
== 0))
4325 if (perf_event_overflow(event
, 0, &data
, regs
))
4326 ret
= HRTIMER_NORESTART
;
4329 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
4330 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
4335 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
4337 struct hw_perf_event
*hwc
= &event
->hw
;
4339 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
4340 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
4341 if (hwc
->sample_period
) {
4344 if (hwc
->remaining
) {
4345 if (hwc
->remaining
< 0)
4348 period
= hwc
->remaining
;
4351 period
= max_t(u64
, 10000, hwc
->sample_period
);
4353 __hrtimer_start_range_ns(&hwc
->hrtimer
,
4354 ns_to_ktime(period
), 0,
4355 HRTIMER_MODE_REL
, 0);
4359 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
4361 struct hw_perf_event
*hwc
= &event
->hw
;
4363 if (hwc
->sample_period
) {
4364 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
4365 hwc
->remaining
= ktime_to_ns(remaining
);
4367 hrtimer_cancel(&hwc
->hrtimer
);
4372 * Software event: cpu wall time clock
4375 static void cpu_clock_perf_event_update(struct perf_event
*event
)
4377 int cpu
= raw_smp_processor_id();
4381 now
= cpu_clock(cpu
);
4382 prev
= atomic64_xchg(&event
->hw
.prev_count
, now
);
4383 atomic64_add(now
- prev
, &event
->count
);
4386 static int cpu_clock_perf_event_enable(struct perf_event
*event
)
4388 struct hw_perf_event
*hwc
= &event
->hw
;
4389 int cpu
= raw_smp_processor_id();
4391 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
4392 perf_swevent_start_hrtimer(event
);
4397 static void cpu_clock_perf_event_disable(struct perf_event
*event
)
4399 perf_swevent_cancel_hrtimer(event
);
4400 cpu_clock_perf_event_update(event
);
4403 static void cpu_clock_perf_event_read(struct perf_event
*event
)
4405 cpu_clock_perf_event_update(event
);
4408 static const struct pmu perf_ops_cpu_clock
= {
4409 .enable
= cpu_clock_perf_event_enable
,
4410 .disable
= cpu_clock_perf_event_disable
,
4411 .read
= cpu_clock_perf_event_read
,
4415 * Software event: task time clock
4418 static void task_clock_perf_event_update(struct perf_event
*event
, u64 now
)
4423 prev
= atomic64_xchg(&event
->hw
.prev_count
, now
);
4425 atomic64_add(delta
, &event
->count
);
4428 static int task_clock_perf_event_enable(struct perf_event
*event
)
4430 struct hw_perf_event
*hwc
= &event
->hw
;
4433 now
= event
->ctx
->time
;
4435 atomic64_set(&hwc
->prev_count
, now
);
4437 perf_swevent_start_hrtimer(event
);
4442 static void task_clock_perf_event_disable(struct perf_event
*event
)
4444 perf_swevent_cancel_hrtimer(event
);
4445 task_clock_perf_event_update(event
, event
->ctx
->time
);
4449 static void task_clock_perf_event_read(struct perf_event
*event
)
4454 update_context_time(event
->ctx
);
4455 time
= event
->ctx
->time
;
4457 u64 now
= perf_clock();
4458 u64 delta
= now
- event
->ctx
->timestamp
;
4459 time
= event
->ctx
->time
+ delta
;
4462 task_clock_perf_event_update(event
, time
);
4465 static const struct pmu perf_ops_task_clock
= {
4466 .enable
= task_clock_perf_event_enable
,
4467 .disable
= task_clock_perf_event_disable
,
4468 .read
= task_clock_perf_event_read
,
4471 /* Deref the hlist from the update side */
4472 static inline struct swevent_hlist
*
4473 swevent_hlist_deref(struct perf_cpu_context
*cpuctx
)
4475 return rcu_dereference_protected(cpuctx
->swevent_hlist
,
4476 lockdep_is_held(&cpuctx
->hlist_mutex
));
4479 static void swevent_hlist_release_rcu(struct rcu_head
*rcu_head
)
4481 struct swevent_hlist
*hlist
;
4483 hlist
= container_of(rcu_head
, struct swevent_hlist
, rcu_head
);
4487 static void swevent_hlist_release(struct perf_cpu_context
*cpuctx
)
4489 struct swevent_hlist
*hlist
= swevent_hlist_deref(cpuctx
);
4494 rcu_assign_pointer(cpuctx
->swevent_hlist
, NULL
);
4495 call_rcu(&hlist
->rcu_head
, swevent_hlist_release_rcu
);
4498 static void swevent_hlist_put_cpu(struct perf_event
*event
, int cpu
)
4500 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4502 mutex_lock(&cpuctx
->hlist_mutex
);
4504 if (!--cpuctx
->hlist_refcount
)
4505 swevent_hlist_release(cpuctx
);
4507 mutex_unlock(&cpuctx
->hlist_mutex
);
4510 static void swevent_hlist_put(struct perf_event
*event
)
4514 if (event
->cpu
!= -1) {
4515 swevent_hlist_put_cpu(event
, event
->cpu
);
4519 for_each_possible_cpu(cpu
)
4520 swevent_hlist_put_cpu(event
, cpu
);
4523 static int swevent_hlist_get_cpu(struct perf_event
*event
, int cpu
)
4525 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4528 mutex_lock(&cpuctx
->hlist_mutex
);
4530 if (!swevent_hlist_deref(cpuctx
) && cpu_online(cpu
)) {
4531 struct swevent_hlist
*hlist
;
4533 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
4538 rcu_assign_pointer(cpuctx
->swevent_hlist
, hlist
);
4540 cpuctx
->hlist_refcount
++;
4542 mutex_unlock(&cpuctx
->hlist_mutex
);
4547 static int swevent_hlist_get(struct perf_event
*event
)
4550 int cpu
, failed_cpu
;
4552 if (event
->cpu
!= -1)
4553 return swevent_hlist_get_cpu(event
, event
->cpu
);
4556 for_each_possible_cpu(cpu
) {
4557 err
= swevent_hlist_get_cpu(event
, cpu
);
4567 for_each_possible_cpu(cpu
) {
4568 if (cpu
== failed_cpu
)
4570 swevent_hlist_put_cpu(event
, cpu
);
4577 #ifdef CONFIG_EVENT_TRACING
4579 static const struct pmu perf_ops_tracepoint
= {
4580 .enable
= perf_trace_enable
,
4581 .disable
= perf_trace_disable
,
4582 .start
= perf_swevent_int
,
4583 .stop
= perf_swevent_void
,
4584 .read
= perf_swevent_read
,
4585 .unthrottle
= perf_swevent_void
,
4588 static int perf_tp_filter_match(struct perf_event
*event
,
4589 struct perf_sample_data
*data
)
4591 void *record
= data
->raw
->data
;
4593 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
4598 static int perf_tp_event_match(struct perf_event
*event
,
4599 struct perf_sample_data
*data
,
4600 struct pt_regs
*regs
)
4603 * All tracepoints are from kernel-space.
4605 if (event
->attr
.exclude_kernel
)
4608 if (!perf_tp_filter_match(event
, data
))
4614 void perf_tp_event(u64 addr
, u64 count
, void *record
, int entry_size
,
4615 struct pt_regs
*regs
, struct hlist_head
*head
)
4617 struct perf_sample_data data
;
4618 struct perf_event
*event
;
4619 struct hlist_node
*node
;
4621 struct perf_raw_record raw
= {
4626 perf_sample_data_init(&data
, addr
);
4630 hlist_for_each_entry_rcu(event
, node
, head
, hlist_entry
) {
4631 if (perf_tp_event_match(event
, &data
, regs
))
4632 perf_swevent_add(event
, count
, 1, &data
, regs
);
4636 EXPORT_SYMBOL_GPL(perf_tp_event
);
4638 static void tp_perf_event_destroy(struct perf_event
*event
)
4640 perf_trace_destroy(event
);
4643 static const struct pmu
*tp_perf_event_init(struct perf_event
*event
)
4648 * Raw tracepoint data is a severe data leak, only allow root to
4651 if ((event
->attr
.sample_type
& PERF_SAMPLE_RAW
) &&
4652 perf_paranoid_tracepoint_raw() &&
4653 !capable(CAP_SYS_ADMIN
))
4654 return ERR_PTR(-EPERM
);
4656 err
= perf_trace_init(event
);
4660 event
->destroy
= tp_perf_event_destroy
;
4662 return &perf_ops_tracepoint
;
4665 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
4670 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
4673 filter_str
= strndup_user(arg
, PAGE_SIZE
);
4674 if (IS_ERR(filter_str
))
4675 return PTR_ERR(filter_str
);
4677 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
, filter_str
);
4683 static void perf_event_free_filter(struct perf_event
*event
)
4685 ftrace_profile_free_filter(event
);
4690 static const struct pmu
*tp_perf_event_init(struct perf_event
*event
)
4695 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
4700 static void perf_event_free_filter(struct perf_event
*event
)
4704 #endif /* CONFIG_EVENT_TRACING */
4706 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4707 static void bp_perf_event_destroy(struct perf_event
*event
)
4709 release_bp_slot(event
);
4712 static const struct pmu
*bp_perf_event_init(struct perf_event
*bp
)
4716 err
= register_perf_hw_breakpoint(bp
);
4718 return ERR_PTR(err
);
4720 bp
->destroy
= bp_perf_event_destroy
;
4722 return &perf_ops_bp
;
4725 void perf_bp_event(struct perf_event
*bp
, void *data
)
4727 struct perf_sample_data sample
;
4728 struct pt_regs
*regs
= data
;
4730 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
);
4732 if (!perf_exclude_event(bp
, regs
))
4733 perf_swevent_add(bp
, 1, 1, &sample
, regs
);
4736 static const struct pmu
*bp_perf_event_init(struct perf_event
*bp
)
4741 void perf_bp_event(struct perf_event
*bp
, void *regs
)
4746 atomic_t perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
4748 static void sw_perf_event_destroy(struct perf_event
*event
)
4750 u64 event_id
= event
->attr
.config
;
4752 WARN_ON(event
->parent
);
4754 atomic_dec(&perf_swevent_enabled
[event_id
]);
4755 swevent_hlist_put(event
);
4758 static const struct pmu
*sw_perf_event_init(struct perf_event
*event
)
4760 const struct pmu
*pmu
= NULL
;
4761 u64 event_id
= event
->attr
.config
;
4764 * Software events (currently) can't in general distinguish
4765 * between user, kernel and hypervisor events.
4766 * However, context switches and cpu migrations are considered
4767 * to be kernel events, and page faults are never hypervisor
4771 case PERF_COUNT_SW_CPU_CLOCK
:
4772 pmu
= &perf_ops_cpu_clock
;
4775 case PERF_COUNT_SW_TASK_CLOCK
:
4777 * If the user instantiates this as a per-cpu event,
4778 * use the cpu_clock event instead.
4780 if (event
->ctx
->task
)
4781 pmu
= &perf_ops_task_clock
;
4783 pmu
= &perf_ops_cpu_clock
;
4786 case PERF_COUNT_SW_PAGE_FAULTS
:
4787 case PERF_COUNT_SW_PAGE_FAULTS_MIN
:
4788 case PERF_COUNT_SW_PAGE_FAULTS_MAJ
:
4789 case PERF_COUNT_SW_CONTEXT_SWITCHES
:
4790 case PERF_COUNT_SW_CPU_MIGRATIONS
:
4791 case PERF_COUNT_SW_ALIGNMENT_FAULTS
:
4792 case PERF_COUNT_SW_EMULATION_FAULTS
:
4793 if (!event
->parent
) {
4796 err
= swevent_hlist_get(event
);
4798 return ERR_PTR(err
);
4800 atomic_inc(&perf_swevent_enabled
[event_id
]);
4801 event
->destroy
= sw_perf_event_destroy
;
4803 pmu
= &perf_ops_generic
;
4811 * Allocate and initialize a event structure
4813 static struct perf_event
*
4814 perf_event_alloc(struct perf_event_attr
*attr
,
4816 struct perf_event_context
*ctx
,
4817 struct perf_event
*group_leader
,
4818 struct perf_event
*parent_event
,
4819 perf_overflow_handler_t overflow_handler
,
4822 const struct pmu
*pmu
;
4823 struct perf_event
*event
;
4824 struct hw_perf_event
*hwc
;
4827 event
= kzalloc(sizeof(*event
), gfpflags
);
4829 return ERR_PTR(-ENOMEM
);
4832 * Single events are their own group leaders, with an
4833 * empty sibling list:
4836 group_leader
= event
;
4838 mutex_init(&event
->child_mutex
);
4839 INIT_LIST_HEAD(&event
->child_list
);
4841 INIT_LIST_HEAD(&event
->group_entry
);
4842 INIT_LIST_HEAD(&event
->event_entry
);
4843 INIT_LIST_HEAD(&event
->sibling_list
);
4844 init_waitqueue_head(&event
->waitq
);
4846 mutex_init(&event
->mmap_mutex
);
4849 event
->attr
= *attr
;
4850 event
->group_leader
= group_leader
;
4855 event
->parent
= parent_event
;
4857 event
->ns
= get_pid_ns(current
->nsproxy
->pid_ns
);
4858 event
->id
= atomic64_inc_return(&perf_event_id
);
4860 event
->state
= PERF_EVENT_STATE_INACTIVE
;
4862 if (!overflow_handler
&& parent_event
)
4863 overflow_handler
= parent_event
->overflow_handler
;
4865 event
->overflow_handler
= overflow_handler
;
4868 event
->state
= PERF_EVENT_STATE_OFF
;
4873 hwc
->sample_period
= attr
->sample_period
;
4874 if (attr
->freq
&& attr
->sample_freq
)
4875 hwc
->sample_period
= 1;
4876 hwc
->last_period
= hwc
->sample_period
;
4878 atomic64_set(&hwc
->period_left
, hwc
->sample_period
);
4881 * we currently do not support PERF_FORMAT_GROUP on inherited events
4883 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
4886 switch (attr
->type
) {
4888 case PERF_TYPE_HARDWARE
:
4889 case PERF_TYPE_HW_CACHE
:
4890 pmu
= hw_perf_event_init(event
);
4893 case PERF_TYPE_SOFTWARE
:
4894 pmu
= sw_perf_event_init(event
);
4897 case PERF_TYPE_TRACEPOINT
:
4898 pmu
= tp_perf_event_init(event
);
4901 case PERF_TYPE_BREAKPOINT
:
4902 pmu
= bp_perf_event_init(event
);
4913 else if (IS_ERR(pmu
))
4918 put_pid_ns(event
->ns
);
4920 return ERR_PTR(err
);
4925 if (!event
->parent
) {
4926 atomic_inc(&nr_events
);
4927 if (event
->attr
.mmap
)
4928 atomic_inc(&nr_mmap_events
);
4929 if (event
->attr
.comm
)
4930 atomic_inc(&nr_comm_events
);
4931 if (event
->attr
.task
)
4932 atomic_inc(&nr_task_events
);
4938 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
4939 struct perf_event_attr
*attr
)
4944 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
4948 * zero the full structure, so that a short copy will be nice.
4950 memset(attr
, 0, sizeof(*attr
));
4952 ret
= get_user(size
, &uattr
->size
);
4956 if (size
> PAGE_SIZE
) /* silly large */
4959 if (!size
) /* abi compat */
4960 size
= PERF_ATTR_SIZE_VER0
;
4962 if (size
< PERF_ATTR_SIZE_VER0
)
4966 * If we're handed a bigger struct than we know of,
4967 * ensure all the unknown bits are 0 - i.e. new
4968 * user-space does not rely on any kernel feature
4969 * extensions we dont know about yet.
4971 if (size
> sizeof(*attr
)) {
4972 unsigned char __user
*addr
;
4973 unsigned char __user
*end
;
4976 addr
= (void __user
*)uattr
+ sizeof(*attr
);
4977 end
= (void __user
*)uattr
+ size
;
4979 for (; addr
< end
; addr
++) {
4980 ret
= get_user(val
, addr
);
4986 size
= sizeof(*attr
);
4989 ret
= copy_from_user(attr
, uattr
, size
);
4994 * If the type exists, the corresponding creation will verify
4997 if (attr
->type
>= PERF_TYPE_MAX
)
5000 if (attr
->__reserved_1
)
5003 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
5006 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
5013 put_user(sizeof(*attr
), &uattr
->size
);
5019 perf_event_set_output(struct perf_event
*event
, struct perf_event
*output_event
)
5021 struct perf_mmap_data
*data
= NULL
, *old_data
= NULL
;
5027 /* don't allow circular references */
5028 if (event
== output_event
)
5032 * Don't allow cross-cpu buffers
5034 if (output_event
->cpu
!= event
->cpu
)
5038 * If its not a per-cpu buffer, it must be the same task.
5040 if (output_event
->cpu
== -1 && output_event
->ctx
!= event
->ctx
)
5044 mutex_lock(&event
->mmap_mutex
);
5045 /* Can't redirect output if we've got an active mmap() */
5046 if (atomic_read(&event
->mmap_count
))
5050 /* get the buffer we want to redirect to */
5051 data
= perf_mmap_data_get(output_event
);
5056 old_data
= event
->data
;
5057 rcu_assign_pointer(event
->data
, data
);
5060 mutex_unlock(&event
->mmap_mutex
);
5063 perf_mmap_data_put(old_data
);
5069 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5071 * @attr_uptr: event_id type attributes for monitoring/sampling
5074 * @group_fd: group leader event fd
5076 SYSCALL_DEFINE5(perf_event_open
,
5077 struct perf_event_attr __user
*, attr_uptr
,
5078 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
5080 struct perf_event
*event
, *group_leader
= NULL
, *output_event
= NULL
;
5081 struct perf_event_attr attr
;
5082 struct perf_event_context
*ctx
;
5083 struct file
*event_file
= NULL
;
5084 struct file
*group_file
= NULL
;
5086 int fput_needed
= 0;
5089 /* for future expandability... */
5090 if (flags
& ~(PERF_FLAG_FD_NO_GROUP
| PERF_FLAG_FD_OUTPUT
))
5093 err
= perf_copy_attr(attr_uptr
, &attr
);
5097 if (!attr
.exclude_kernel
) {
5098 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
5103 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
5107 event_fd
= get_unused_fd_flags(O_RDWR
);
5112 * Get the target context (task or percpu):
5114 ctx
= find_get_context(pid
, cpu
);
5120 if (group_fd
!= -1) {
5121 group_leader
= perf_fget_light(group_fd
, &fput_needed
);
5122 if (IS_ERR(group_leader
)) {
5123 err
= PTR_ERR(group_leader
);
5124 goto err_put_context
;
5126 group_file
= group_leader
->filp
;
5127 if (flags
& PERF_FLAG_FD_OUTPUT
)
5128 output_event
= group_leader
;
5129 if (flags
& PERF_FLAG_FD_NO_GROUP
)
5130 group_leader
= NULL
;
5134 * Look up the group leader (we will attach this event to it):
5140 * Do not allow a recursive hierarchy (this new sibling
5141 * becoming part of another group-sibling):
5143 if (group_leader
->group_leader
!= group_leader
)
5144 goto err_put_context
;
5146 * Do not allow to attach to a group in a different
5147 * task or CPU context:
5149 if (group_leader
->ctx
!= ctx
)
5150 goto err_put_context
;
5152 * Only a group leader can be exclusive or pinned
5154 if (attr
.exclusive
|| attr
.pinned
)
5155 goto err_put_context
;
5158 event
= perf_event_alloc(&attr
, cpu
, ctx
, group_leader
,
5159 NULL
, NULL
, GFP_KERNEL
);
5160 if (IS_ERR(event
)) {
5161 err
= PTR_ERR(event
);
5162 goto err_put_context
;
5166 err
= perf_event_set_output(event
, output_event
);
5168 goto err_free_put_context
;
5171 event_file
= anon_inode_getfile("[perf_event]", &perf_fops
, event
, O_RDWR
);
5172 if (IS_ERR(event_file
)) {
5173 err
= PTR_ERR(event_file
);
5174 goto err_free_put_context
;
5177 event
->filp
= event_file
;
5178 WARN_ON_ONCE(ctx
->parent_ctx
);
5179 mutex_lock(&ctx
->mutex
);
5180 perf_install_in_context(ctx
, event
, cpu
);
5182 mutex_unlock(&ctx
->mutex
);
5184 event
->owner
= current
;
5185 get_task_struct(current
);
5186 mutex_lock(¤t
->perf_event_mutex
);
5187 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
5188 mutex_unlock(¤t
->perf_event_mutex
);
5191 * Drop the reference on the group_event after placing the
5192 * new event on the sibling_list. This ensures destruction
5193 * of the group leader will find the pointer to itself in
5194 * perf_group_detach().
5196 fput_light(group_file
, fput_needed
);
5197 fd_install(event_fd
, event_file
);
5200 err_free_put_context
:
5203 fput_light(group_file
, fput_needed
);
5206 put_unused_fd(event_fd
);
5211 * perf_event_create_kernel_counter
5213 * @attr: attributes of the counter to create
5214 * @cpu: cpu in which the counter is bound
5215 * @pid: task to profile
5218 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
5220 perf_overflow_handler_t overflow_handler
)
5222 struct perf_event
*event
;
5223 struct perf_event_context
*ctx
;
5227 * Get the target context (task or percpu):
5230 ctx
= find_get_context(pid
, cpu
);
5236 event
= perf_event_alloc(attr
, cpu
, ctx
, NULL
,
5237 NULL
, overflow_handler
, GFP_KERNEL
);
5238 if (IS_ERR(event
)) {
5239 err
= PTR_ERR(event
);
5240 goto err_put_context
;
5244 WARN_ON_ONCE(ctx
->parent_ctx
);
5245 mutex_lock(&ctx
->mutex
);
5246 perf_install_in_context(ctx
, event
, cpu
);
5248 mutex_unlock(&ctx
->mutex
);
5250 event
->owner
= current
;
5251 get_task_struct(current
);
5252 mutex_lock(¤t
->perf_event_mutex
);
5253 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
5254 mutex_unlock(¤t
->perf_event_mutex
);
5261 return ERR_PTR(err
);
5263 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
5266 * inherit a event from parent task to child task:
5268 static struct perf_event
*
5269 inherit_event(struct perf_event
*parent_event
,
5270 struct task_struct
*parent
,
5271 struct perf_event_context
*parent_ctx
,
5272 struct task_struct
*child
,
5273 struct perf_event
*group_leader
,
5274 struct perf_event_context
*child_ctx
)
5276 struct perf_event
*child_event
;
5279 * Instead of creating recursive hierarchies of events,
5280 * we link inherited events back to the original parent,
5281 * which has a filp for sure, which we use as the reference
5284 if (parent_event
->parent
)
5285 parent_event
= parent_event
->parent
;
5287 child_event
= perf_event_alloc(&parent_event
->attr
,
5288 parent_event
->cpu
, child_ctx
,
5289 group_leader
, parent_event
,
5291 if (IS_ERR(child_event
))
5296 * Make the child state follow the state of the parent event,
5297 * not its attr.disabled bit. We hold the parent's mutex,
5298 * so we won't race with perf_event_{en, dis}able_family.
5300 if (parent_event
->state
>= PERF_EVENT_STATE_INACTIVE
)
5301 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
5303 child_event
->state
= PERF_EVENT_STATE_OFF
;
5305 if (parent_event
->attr
.freq
) {
5306 u64 sample_period
= parent_event
->hw
.sample_period
;
5307 struct hw_perf_event
*hwc
= &child_event
->hw
;
5309 hwc
->sample_period
= sample_period
;
5310 hwc
->last_period
= sample_period
;
5312 atomic64_set(&hwc
->period_left
, sample_period
);
5315 child_event
->overflow_handler
= parent_event
->overflow_handler
;
5318 * Link it up in the child's context:
5320 add_event_to_ctx(child_event
, child_ctx
);
5323 * Get a reference to the parent filp - we will fput it
5324 * when the child event exits. This is safe to do because
5325 * we are in the parent and we know that the filp still
5326 * exists and has a nonzero count:
5328 atomic_long_inc(&parent_event
->filp
->f_count
);
5331 * Link this into the parent event's child list
5333 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
5334 mutex_lock(&parent_event
->child_mutex
);
5335 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
5336 mutex_unlock(&parent_event
->child_mutex
);
5341 static int inherit_group(struct perf_event
*parent_event
,
5342 struct task_struct
*parent
,
5343 struct perf_event_context
*parent_ctx
,
5344 struct task_struct
*child
,
5345 struct perf_event_context
*child_ctx
)
5347 struct perf_event
*leader
;
5348 struct perf_event
*sub
;
5349 struct perf_event
*child_ctr
;
5351 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
5352 child
, NULL
, child_ctx
);
5354 return PTR_ERR(leader
);
5355 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
5356 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
5357 child
, leader
, child_ctx
);
5358 if (IS_ERR(child_ctr
))
5359 return PTR_ERR(child_ctr
);
5364 static void sync_child_event(struct perf_event
*child_event
,
5365 struct task_struct
*child
)
5367 struct perf_event
*parent_event
= child_event
->parent
;
5370 if (child_event
->attr
.inherit_stat
)
5371 perf_event_read_event(child_event
, child
);
5373 child_val
= atomic64_read(&child_event
->count
);
5376 * Add back the child's count to the parent's count:
5378 atomic64_add(child_val
, &parent_event
->count
);
5379 atomic64_add(child_event
->total_time_enabled
,
5380 &parent_event
->child_total_time_enabled
);
5381 atomic64_add(child_event
->total_time_running
,
5382 &parent_event
->child_total_time_running
);
5385 * Remove this event from the parent's list
5387 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
5388 mutex_lock(&parent_event
->child_mutex
);
5389 list_del_init(&child_event
->child_list
);
5390 mutex_unlock(&parent_event
->child_mutex
);
5393 * Release the parent event, if this was the last
5396 fput(parent_event
->filp
);
5400 __perf_event_exit_task(struct perf_event
*child_event
,
5401 struct perf_event_context
*child_ctx
,
5402 struct task_struct
*child
)
5404 if (child_event
->parent
) {
5405 raw_spin_lock_irq(&child_ctx
->lock
);
5406 perf_group_detach(child_event
);
5407 raw_spin_unlock_irq(&child_ctx
->lock
);
5410 perf_event_remove_from_context(child_event
);
5413 * It can happen that the parent exits first, and has events
5414 * that are still around due to the child reference. These
5415 * events need to be zapped.
5417 if (child_event
->parent
) {
5418 sync_child_event(child_event
, child
);
5419 free_event(child_event
);
5424 * When a child task exits, feed back event values to parent events.
5426 void perf_event_exit_task(struct task_struct
*child
)
5428 struct perf_event
*child_event
, *tmp
;
5429 struct perf_event_context
*child_ctx
;
5430 unsigned long flags
;
5432 if (likely(!child
->perf_event_ctxp
)) {
5433 perf_event_task(child
, NULL
, 0);
5437 local_irq_save(flags
);
5439 * We can't reschedule here because interrupts are disabled,
5440 * and either child is current or it is a task that can't be
5441 * scheduled, so we are now safe from rescheduling changing
5444 child_ctx
= child
->perf_event_ctxp
;
5445 __perf_event_task_sched_out(child_ctx
);
5448 * Take the context lock here so that if find_get_context is
5449 * reading child->perf_event_ctxp, we wait until it has
5450 * incremented the context's refcount before we do put_ctx below.
5452 raw_spin_lock(&child_ctx
->lock
);
5453 child
->perf_event_ctxp
= NULL
;
5455 * If this context is a clone; unclone it so it can't get
5456 * swapped to another process while we're removing all
5457 * the events from it.
5459 unclone_ctx(child_ctx
);
5460 update_context_time(child_ctx
);
5461 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
5464 * Report the task dead after unscheduling the events so that we
5465 * won't get any samples after PERF_RECORD_EXIT. We can however still
5466 * get a few PERF_RECORD_READ events.
5468 perf_event_task(child
, child_ctx
, 0);
5471 * We can recurse on the same lock type through:
5473 * __perf_event_exit_task()
5474 * sync_child_event()
5475 * fput(parent_event->filp)
5477 * mutex_lock(&ctx->mutex)
5479 * But since its the parent context it won't be the same instance.
5481 mutex_lock(&child_ctx
->mutex
);
5484 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->pinned_groups
,
5486 __perf_event_exit_task(child_event
, child_ctx
, child
);
5488 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->flexible_groups
,
5490 __perf_event_exit_task(child_event
, child_ctx
, child
);
5493 * If the last event was a group event, it will have appended all
5494 * its siblings to the list, but we obtained 'tmp' before that which
5495 * will still point to the list head terminating the iteration.
5497 if (!list_empty(&child_ctx
->pinned_groups
) ||
5498 !list_empty(&child_ctx
->flexible_groups
))
5501 mutex_unlock(&child_ctx
->mutex
);
5506 static void perf_free_event(struct perf_event
*event
,
5507 struct perf_event_context
*ctx
)
5509 struct perf_event
*parent
= event
->parent
;
5511 if (WARN_ON_ONCE(!parent
))
5514 mutex_lock(&parent
->child_mutex
);
5515 list_del_init(&event
->child_list
);
5516 mutex_unlock(&parent
->child_mutex
);
5520 perf_group_detach(event
);
5521 list_del_event(event
, ctx
);
5526 * free an unexposed, unused context as created by inheritance by
5527 * init_task below, used by fork() in case of fail.
5529 void perf_event_free_task(struct task_struct
*task
)
5531 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
5532 struct perf_event
*event
, *tmp
;
5537 mutex_lock(&ctx
->mutex
);
5539 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
, group_entry
)
5540 perf_free_event(event
, ctx
);
5542 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
,
5544 perf_free_event(event
, ctx
);
5546 if (!list_empty(&ctx
->pinned_groups
) ||
5547 !list_empty(&ctx
->flexible_groups
))
5550 mutex_unlock(&ctx
->mutex
);
5556 inherit_task_group(struct perf_event
*event
, struct task_struct
*parent
,
5557 struct perf_event_context
*parent_ctx
,
5558 struct task_struct
*child
,
5562 struct perf_event_context
*child_ctx
= child
->perf_event_ctxp
;
5564 if (!event
->attr
.inherit
) {
5571 * This is executed from the parent task context, so
5572 * inherit events that have been marked for cloning.
5573 * First allocate and initialize a context for the
5577 child_ctx
= kzalloc(sizeof(struct perf_event_context
),
5582 __perf_event_init_context(child_ctx
, child
);
5583 child
->perf_event_ctxp
= child_ctx
;
5584 get_task_struct(child
);
5587 ret
= inherit_group(event
, parent
, parent_ctx
,
5598 * Initialize the perf_event context in task_struct
5600 int perf_event_init_task(struct task_struct
*child
)
5602 struct perf_event_context
*child_ctx
, *parent_ctx
;
5603 struct perf_event_context
*cloned_ctx
;
5604 struct perf_event
*event
;
5605 struct task_struct
*parent
= current
;
5606 int inherited_all
= 1;
5607 unsigned long flags
;
5610 child
->perf_event_ctxp
= NULL
;
5612 mutex_init(&child
->perf_event_mutex
);
5613 INIT_LIST_HEAD(&child
->perf_event_list
);
5615 if (likely(!parent
->perf_event_ctxp
))
5619 * If the parent's context is a clone, pin it so it won't get
5622 parent_ctx
= perf_pin_task_context(parent
);
5625 * No need to check if parent_ctx != NULL here; since we saw
5626 * it non-NULL earlier, the only reason for it to become NULL
5627 * is if we exit, and since we're currently in the middle of
5628 * a fork we can't be exiting at the same time.
5632 * Lock the parent list. No need to lock the child - not PID
5633 * hashed yet and not running, so nobody can access it.
5635 mutex_lock(&parent_ctx
->mutex
);
5638 * We dont have to disable NMIs - we are only looking at
5639 * the list, not manipulating it:
5641 list_for_each_entry(event
, &parent_ctx
->pinned_groups
, group_entry
) {
5642 ret
= inherit_task_group(event
, parent
, parent_ctx
, child
,
5649 * We can't hold ctx->lock when iterating the ->flexible_group list due
5650 * to allocations, but we need to prevent rotation because
5651 * rotate_ctx() will change the list from interrupt context.
5653 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
5654 parent_ctx
->rotate_disable
= 1;
5655 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
5657 list_for_each_entry(event
, &parent_ctx
->flexible_groups
, group_entry
) {
5658 ret
= inherit_task_group(event
, parent
, parent_ctx
, child
,
5664 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
5665 parent_ctx
->rotate_disable
= 0;
5666 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
5668 child_ctx
= child
->perf_event_ctxp
;
5670 if (child_ctx
&& inherited_all
) {
5672 * Mark the child context as a clone of the parent
5673 * context, or of whatever the parent is a clone of.
5674 * Note that if the parent is a clone, it could get
5675 * uncloned at any point, but that doesn't matter
5676 * because the list of events and the generation
5677 * count can't have changed since we took the mutex.
5679 cloned_ctx
= rcu_dereference(parent_ctx
->parent_ctx
);
5681 child_ctx
->parent_ctx
= cloned_ctx
;
5682 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
5684 child_ctx
->parent_ctx
= parent_ctx
;
5685 child_ctx
->parent_gen
= parent_ctx
->generation
;
5687 get_ctx(child_ctx
->parent_ctx
);
5690 mutex_unlock(&parent_ctx
->mutex
);
5692 perf_unpin_context(parent_ctx
);
5697 static void __init
perf_event_init_all_cpus(void)
5700 struct perf_cpu_context
*cpuctx
;
5702 for_each_possible_cpu(cpu
) {
5703 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5704 mutex_init(&cpuctx
->hlist_mutex
);
5705 __perf_event_init_context(&cpuctx
->ctx
, NULL
);
5709 static void __cpuinit
perf_event_init_cpu(int cpu
)
5711 struct perf_cpu_context
*cpuctx
;
5713 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5715 spin_lock(&perf_resource_lock
);
5716 cpuctx
->max_pertask
= perf_max_events
- perf_reserved_percpu
;
5717 spin_unlock(&perf_resource_lock
);
5719 mutex_lock(&cpuctx
->hlist_mutex
);
5720 if (cpuctx
->hlist_refcount
> 0) {
5721 struct swevent_hlist
*hlist
;
5723 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
5724 WARN_ON_ONCE(!hlist
);
5725 rcu_assign_pointer(cpuctx
->swevent_hlist
, hlist
);
5727 mutex_unlock(&cpuctx
->hlist_mutex
);
5730 #ifdef CONFIG_HOTPLUG_CPU
5731 static void __perf_event_exit_cpu(void *info
)
5733 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
5734 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
5735 struct perf_event
*event
, *tmp
;
5737 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
, group_entry
)
5738 __perf_event_remove_from_context(event
);
5739 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
, group_entry
)
5740 __perf_event_remove_from_context(event
);
5742 static void perf_event_exit_cpu(int cpu
)
5744 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5745 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
5747 mutex_lock(&cpuctx
->hlist_mutex
);
5748 swevent_hlist_release(cpuctx
);
5749 mutex_unlock(&cpuctx
->hlist_mutex
);
5751 mutex_lock(&ctx
->mutex
);
5752 smp_call_function_single(cpu
, __perf_event_exit_cpu
, NULL
, 1);
5753 mutex_unlock(&ctx
->mutex
);
5756 static inline void perf_event_exit_cpu(int cpu
) { }
5759 static int __cpuinit
5760 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
5762 unsigned int cpu
= (long)hcpu
;
5766 case CPU_UP_PREPARE
:
5767 case CPU_UP_PREPARE_FROZEN
:
5768 perf_event_init_cpu(cpu
);
5771 case CPU_DOWN_PREPARE
:
5772 case CPU_DOWN_PREPARE_FROZEN
:
5773 perf_event_exit_cpu(cpu
);
5784 * This has to have a higher priority than migration_notifier in sched.c.
5786 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
5787 .notifier_call
= perf_cpu_notify
,
5791 void __init
perf_event_init(void)
5793 perf_event_init_all_cpus();
5794 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
5795 (void *)(long)smp_processor_id());
5796 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_ONLINE
,
5797 (void *)(long)smp_processor_id());
5798 register_cpu_notifier(&perf_cpu_nb
);
5801 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class,
5802 struct sysdev_class_attribute
*attr
,
5805 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
5809 perf_set_reserve_percpu(struct sysdev_class
*class,
5810 struct sysdev_class_attribute
*attr
,
5814 struct perf_cpu_context
*cpuctx
;
5818 err
= strict_strtoul(buf
, 10, &val
);
5821 if (val
> perf_max_events
)
5824 spin_lock(&perf_resource_lock
);
5825 perf_reserved_percpu
= val
;
5826 for_each_online_cpu(cpu
) {
5827 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5828 raw_spin_lock_irq(&cpuctx
->ctx
.lock
);
5829 mpt
= min(perf_max_events
- cpuctx
->ctx
.nr_events
,
5830 perf_max_events
- perf_reserved_percpu
);
5831 cpuctx
->max_pertask
= mpt
;
5832 raw_spin_unlock_irq(&cpuctx
->ctx
.lock
);
5834 spin_unlock(&perf_resource_lock
);
5839 static ssize_t
perf_show_overcommit(struct sysdev_class
*class,
5840 struct sysdev_class_attribute
*attr
,
5843 return sprintf(buf
, "%d\n", perf_overcommit
);
5847 perf_set_overcommit(struct sysdev_class
*class,
5848 struct sysdev_class_attribute
*attr
,
5849 const char *buf
, size_t count
)
5854 err
= strict_strtoul(buf
, 10, &val
);
5860 spin_lock(&perf_resource_lock
);
5861 perf_overcommit
= val
;
5862 spin_unlock(&perf_resource_lock
);
5867 static SYSDEV_CLASS_ATTR(
5870 perf_show_reserve_percpu
,
5871 perf_set_reserve_percpu
5874 static SYSDEV_CLASS_ATTR(
5877 perf_show_overcommit
,
5881 static struct attribute
*perfclass_attrs
[] = {
5882 &attr_reserve_percpu
.attr
,
5883 &attr_overcommit
.attr
,
5887 static struct attribute_group perfclass_attr_group
= {
5888 .attrs
= perfclass_attrs
,
5889 .name
= "perf_events",
5892 static int __init
perf_event_sysfs_init(void)
5894 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
5895 &perfclass_attr_group
);
5897 device_initcall(perf_event_sysfs_init
);