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 int sysctl_perf_event_mlock __read_mostly
= 512; /* 'free' kb per user */
64 * max perf event sample rate
66 int sysctl_perf_event_sample_rate __read_mostly
= 100000;
68 static atomic64_t perf_event_id
;
71 * Lock for (sysadmin-configurable) event reservations:
73 static DEFINE_SPINLOCK(perf_resource_lock
);
76 * Architecture provided APIs - weak aliases:
78 extern __weak
const struct pmu
*hw_perf_event_init(struct perf_event
*event
)
83 void __weak
hw_perf_disable(void) { barrier(); }
84 void __weak
hw_perf_enable(void) { barrier(); }
86 void __weak
perf_event_print_debug(void) { }
88 extern __weak
const char *perf_pmu_name(void)
93 static DEFINE_PER_CPU(int, perf_disable_count
);
95 void perf_disable(void)
97 if (!__get_cpu_var(perf_disable_count
)++)
101 void perf_enable(void)
103 if (!--__get_cpu_var(perf_disable_count
))
107 static void get_ctx(struct perf_event_context
*ctx
)
109 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
112 static void free_ctx(struct rcu_head
*head
)
114 struct perf_event_context
*ctx
;
116 ctx
= container_of(head
, struct perf_event_context
, rcu_head
);
120 static void put_ctx(struct perf_event_context
*ctx
)
122 if (atomic_dec_and_test(&ctx
->refcount
)) {
124 put_ctx(ctx
->parent_ctx
);
126 put_task_struct(ctx
->task
);
127 call_rcu(&ctx
->rcu_head
, free_ctx
);
131 static void unclone_ctx(struct perf_event_context
*ctx
)
133 if (ctx
->parent_ctx
) {
134 put_ctx(ctx
->parent_ctx
);
135 ctx
->parent_ctx
= NULL
;
140 * If we inherit events we want to return the parent event id
143 static u64
primary_event_id(struct perf_event
*event
)
148 id
= event
->parent
->id
;
154 * Get the perf_event_context for a task and lock it.
155 * This has to cope with with the fact that until it is locked,
156 * the context could get moved to another task.
158 static struct perf_event_context
*
159 perf_lock_task_context(struct task_struct
*task
, unsigned long *flags
)
161 struct perf_event_context
*ctx
;
165 ctx
= rcu_dereference(task
->perf_event_ctxp
);
168 * If this context is a clone of another, it might
169 * get swapped for another underneath us by
170 * perf_event_task_sched_out, though the
171 * rcu_read_lock() protects us from any context
172 * getting freed. Lock the context and check if it
173 * got swapped before we could get the lock, and retry
174 * if so. If we locked the right context, then it
175 * can't get swapped on us any more.
177 raw_spin_lock_irqsave(&ctx
->lock
, *flags
);
178 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
)) {
179 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
183 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
184 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
193 * Get the context for a task and increment its pin_count so it
194 * can't get swapped to another task. This also increments its
195 * reference count so that the context can't get freed.
197 static struct perf_event_context
*perf_pin_task_context(struct task_struct
*task
)
199 struct perf_event_context
*ctx
;
202 ctx
= perf_lock_task_context(task
, &flags
);
205 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
210 static void perf_unpin_context(struct perf_event_context
*ctx
)
214 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
216 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
220 static inline u64
perf_clock(void)
222 return local_clock();
226 * Update the record of the current time in a context.
228 static void update_context_time(struct perf_event_context
*ctx
)
230 u64 now
= perf_clock();
232 ctx
->time
+= now
- ctx
->timestamp
;
233 ctx
->timestamp
= now
;
237 * Update the total_time_enabled and total_time_running fields for a event.
239 static void update_event_times(struct perf_event
*event
)
241 struct perf_event_context
*ctx
= event
->ctx
;
244 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
245 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
251 run_end
= event
->tstamp_stopped
;
253 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
255 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
256 run_end
= event
->tstamp_stopped
;
260 event
->total_time_running
= run_end
- event
->tstamp_running
;
264 * Update total_time_enabled and total_time_running for all events in a group.
266 static void update_group_times(struct perf_event
*leader
)
268 struct perf_event
*event
;
270 update_event_times(leader
);
271 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
272 update_event_times(event
);
275 static struct list_head
*
276 ctx_group_list(struct perf_event
*event
, struct perf_event_context
*ctx
)
278 if (event
->attr
.pinned
)
279 return &ctx
->pinned_groups
;
281 return &ctx
->flexible_groups
;
285 * Add a event from the lists for its context.
286 * Must be called with ctx->mutex and ctx->lock held.
289 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
291 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_CONTEXT
);
292 event
->attach_state
|= PERF_ATTACH_CONTEXT
;
295 * If we're a stand alone event or group leader, we go to the context
296 * list, group events are kept attached to the group so that
297 * perf_group_detach can, at all times, locate all siblings.
299 if (event
->group_leader
== event
) {
300 struct list_head
*list
;
302 if (is_software_event(event
))
303 event
->group_flags
|= PERF_GROUP_SOFTWARE
;
305 list
= ctx_group_list(event
, ctx
);
306 list_add_tail(&event
->group_entry
, list
);
309 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
311 if (event
->attr
.inherit_stat
)
315 static void perf_group_attach(struct perf_event
*event
)
317 struct perf_event
*group_leader
= event
->group_leader
;
319 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_GROUP
);
320 event
->attach_state
|= PERF_ATTACH_GROUP
;
322 if (group_leader
== event
)
325 if (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
&&
326 !is_software_event(event
))
327 group_leader
->group_flags
&= ~PERF_GROUP_SOFTWARE
;
329 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
330 group_leader
->nr_siblings
++;
334 * Remove a event from the lists for its context.
335 * Must be called with ctx->mutex and ctx->lock held.
338 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
341 * We can have double detach due to exit/hot-unplug + close.
343 if (!(event
->attach_state
& PERF_ATTACH_CONTEXT
))
346 event
->attach_state
&= ~PERF_ATTACH_CONTEXT
;
349 if (event
->attr
.inherit_stat
)
352 list_del_rcu(&event
->event_entry
);
354 if (event
->group_leader
== event
)
355 list_del_init(&event
->group_entry
);
357 update_group_times(event
);
360 * If event was in error state, then keep it
361 * that way, otherwise bogus counts will be
362 * returned on read(). The only way to get out
363 * of error state is by explicit re-enabling
366 if (event
->state
> PERF_EVENT_STATE_OFF
)
367 event
->state
= PERF_EVENT_STATE_OFF
;
370 static void perf_group_detach(struct perf_event
*event
)
372 struct perf_event
*sibling
, *tmp
;
373 struct list_head
*list
= NULL
;
376 * We can have double detach due to exit/hot-unplug + close.
378 if (!(event
->attach_state
& PERF_ATTACH_GROUP
))
381 event
->attach_state
&= ~PERF_ATTACH_GROUP
;
384 * If this is a sibling, remove it from its group.
386 if (event
->group_leader
!= event
) {
387 list_del_init(&event
->group_entry
);
388 event
->group_leader
->nr_siblings
--;
392 if (!list_empty(&event
->group_entry
))
393 list
= &event
->group_entry
;
396 * If this was a group event with sibling events then
397 * upgrade the siblings to singleton events by adding them
398 * to whatever list we are on.
400 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
402 list_move_tail(&sibling
->group_entry
, list
);
403 sibling
->group_leader
= sibling
;
405 /* Inherit group flags from the previous leader */
406 sibling
->group_flags
= event
->group_flags
;
411 event_filter_match(struct perf_event
*event
)
413 return event
->cpu
== -1 || event
->cpu
== smp_processor_id();
417 event_sched_out(struct perf_event
*event
,
418 struct perf_cpu_context
*cpuctx
,
419 struct perf_event_context
*ctx
)
423 * An event which could not be activated because of
424 * filter mismatch still needs to have its timings
425 * maintained, otherwise bogus information is return
426 * via read() for time_enabled, time_running:
428 if (event
->state
== PERF_EVENT_STATE_INACTIVE
429 && !event_filter_match(event
)) {
430 delta
= ctx
->time
- event
->tstamp_stopped
;
431 event
->tstamp_running
+= delta
;
432 event
->tstamp_stopped
= ctx
->time
;
435 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
438 event
->state
= PERF_EVENT_STATE_INACTIVE
;
439 if (event
->pending_disable
) {
440 event
->pending_disable
= 0;
441 event
->state
= PERF_EVENT_STATE_OFF
;
443 event
->tstamp_stopped
= ctx
->time
;
444 event
->pmu
->disable(event
);
447 if (!is_software_event(event
))
448 cpuctx
->active_oncpu
--;
450 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
451 cpuctx
->exclusive
= 0;
455 group_sched_out(struct perf_event
*group_event
,
456 struct perf_cpu_context
*cpuctx
,
457 struct perf_event_context
*ctx
)
459 struct perf_event
*event
;
460 int state
= group_event
->state
;
462 event_sched_out(group_event
, cpuctx
, ctx
);
465 * Schedule out siblings (if any):
467 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
468 event_sched_out(event
, cpuctx
, ctx
);
470 if (state
== PERF_EVENT_STATE_ACTIVE
&& group_event
->attr
.exclusive
)
471 cpuctx
->exclusive
= 0;
475 * Cross CPU call to remove a performance event
477 * We disable the event on the hardware level first. After that we
478 * remove it from the context list.
480 static void __perf_event_remove_from_context(void *info
)
482 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
483 struct perf_event
*event
= info
;
484 struct perf_event_context
*ctx
= event
->ctx
;
487 * If this is a task context, we need to check whether it is
488 * the current task context of this cpu. If not it has been
489 * scheduled out before the smp call arrived.
491 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
494 raw_spin_lock(&ctx
->lock
);
496 * Protect the list operation against NMI by disabling the
497 * events on a global level.
501 event_sched_out(event
, cpuctx
, ctx
);
503 list_del_event(event
, ctx
);
507 * Allow more per task events with respect to the
510 cpuctx
->max_pertask
=
511 min(perf_max_events
- ctx
->nr_events
,
512 perf_max_events
- perf_reserved_percpu
);
516 raw_spin_unlock(&ctx
->lock
);
521 * Remove the event from a task's (or a CPU's) list of events.
523 * Must be called with ctx->mutex held.
525 * CPU events are removed with a smp call. For task events we only
526 * call when the task is on a CPU.
528 * If event->ctx is a cloned context, callers must make sure that
529 * every task struct that event->ctx->task could possibly point to
530 * remains valid. This is OK when called from perf_release since
531 * that only calls us on the top-level context, which can't be a clone.
532 * When called from perf_event_exit_task, it's OK because the
533 * context has been detached from its task.
535 static void perf_event_remove_from_context(struct perf_event
*event
)
537 struct perf_event_context
*ctx
= event
->ctx
;
538 struct task_struct
*task
= ctx
->task
;
542 * Per cpu events are removed via an smp call and
543 * the removal is always successful.
545 smp_call_function_single(event
->cpu
,
546 __perf_event_remove_from_context
,
552 task_oncpu_function_call(task
, __perf_event_remove_from_context
,
555 raw_spin_lock_irq(&ctx
->lock
);
557 * If the context is active we need to retry the smp call.
559 if (ctx
->nr_active
&& !list_empty(&event
->group_entry
)) {
560 raw_spin_unlock_irq(&ctx
->lock
);
565 * The lock prevents that this context is scheduled in so we
566 * can remove the event safely, if the call above did not
569 if (!list_empty(&event
->group_entry
))
570 list_del_event(event
, ctx
);
571 raw_spin_unlock_irq(&ctx
->lock
);
575 * Cross CPU call to disable a performance event
577 static void __perf_event_disable(void *info
)
579 struct perf_event
*event
= info
;
580 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
581 struct perf_event_context
*ctx
= event
->ctx
;
584 * If this is a per-task event, need to check whether this
585 * event's task is the current task on this cpu.
587 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
590 raw_spin_lock(&ctx
->lock
);
593 * If the event is on, turn it off.
594 * If it is in error state, leave it in error state.
596 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
) {
597 update_context_time(ctx
);
598 update_group_times(event
);
599 if (event
== event
->group_leader
)
600 group_sched_out(event
, cpuctx
, ctx
);
602 event_sched_out(event
, cpuctx
, ctx
);
603 event
->state
= PERF_EVENT_STATE_OFF
;
606 raw_spin_unlock(&ctx
->lock
);
612 * If event->ctx is a cloned context, callers must make sure that
613 * every task struct that event->ctx->task could possibly point to
614 * remains valid. This condition is satisifed when called through
615 * perf_event_for_each_child or perf_event_for_each because they
616 * hold the top-level event's child_mutex, so any descendant that
617 * goes to exit will block in sync_child_event.
618 * When called from perf_pending_event it's OK because event->ctx
619 * is the current context on this CPU and preemption is disabled,
620 * hence we can't get into perf_event_task_sched_out for this context.
622 void perf_event_disable(struct perf_event
*event
)
624 struct perf_event_context
*ctx
= event
->ctx
;
625 struct task_struct
*task
= ctx
->task
;
629 * Disable the event on the cpu that it's on
631 smp_call_function_single(event
->cpu
, __perf_event_disable
,
637 task_oncpu_function_call(task
, __perf_event_disable
, event
);
639 raw_spin_lock_irq(&ctx
->lock
);
641 * If the event is still active, we need to retry the cross-call.
643 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
644 raw_spin_unlock_irq(&ctx
->lock
);
649 * Since we have the lock this context can't be scheduled
650 * in, so we can change the state safely.
652 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
653 update_group_times(event
);
654 event
->state
= PERF_EVENT_STATE_OFF
;
657 raw_spin_unlock_irq(&ctx
->lock
);
661 event_sched_in(struct perf_event
*event
,
662 struct perf_cpu_context
*cpuctx
,
663 struct perf_event_context
*ctx
)
665 if (event
->state
<= PERF_EVENT_STATE_OFF
)
668 event
->state
= PERF_EVENT_STATE_ACTIVE
;
669 event
->oncpu
= smp_processor_id();
671 * The new state must be visible before we turn it on in the hardware:
675 if (event
->pmu
->enable(event
)) {
676 event
->state
= PERF_EVENT_STATE_INACTIVE
;
681 event
->tstamp_running
+= ctx
->time
- event
->tstamp_stopped
;
683 if (!is_software_event(event
))
684 cpuctx
->active_oncpu
++;
687 if (event
->attr
.exclusive
)
688 cpuctx
->exclusive
= 1;
694 group_sched_in(struct perf_event
*group_event
,
695 struct perf_cpu_context
*cpuctx
,
696 struct perf_event_context
*ctx
)
698 struct perf_event
*event
, *partial_group
= NULL
;
699 const struct pmu
*pmu
= group_event
->pmu
;
702 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
705 /* Check if group transaction availabe */
712 if (event_sched_in(group_event
, cpuctx
, ctx
)) {
714 pmu
->cancel_txn(pmu
);
719 * Schedule in siblings as one group (if any):
721 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
722 if (event_sched_in(event
, cpuctx
, ctx
)) {
723 partial_group
= event
;
728 if (!txn
|| !pmu
->commit_txn(pmu
))
733 * Groups can be scheduled in as one unit only, so undo any
734 * partial group before returning:
736 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
737 if (event
== partial_group
)
739 event_sched_out(event
, cpuctx
, ctx
);
741 event_sched_out(group_event
, cpuctx
, ctx
);
744 pmu
->cancel_txn(pmu
);
750 * Work out whether we can put this event group on the CPU now.
752 static int group_can_go_on(struct perf_event
*event
,
753 struct perf_cpu_context
*cpuctx
,
757 * Groups consisting entirely of software events can always go on.
759 if (event
->group_flags
& PERF_GROUP_SOFTWARE
)
762 * If an exclusive group is already on, no other hardware
765 if (cpuctx
->exclusive
)
768 * If this group is exclusive and there are already
769 * events on the CPU, it can't go on.
771 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
774 * Otherwise, try to add it if all previous groups were able
780 static void add_event_to_ctx(struct perf_event
*event
,
781 struct perf_event_context
*ctx
)
783 list_add_event(event
, ctx
);
784 perf_group_attach(event
);
785 event
->tstamp_enabled
= ctx
->time
;
786 event
->tstamp_running
= ctx
->time
;
787 event
->tstamp_stopped
= ctx
->time
;
791 * Cross CPU call to install and enable a performance event
793 * Must be called with ctx->mutex held
795 static void __perf_install_in_context(void *info
)
797 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
798 struct perf_event
*event
= info
;
799 struct perf_event_context
*ctx
= event
->ctx
;
800 struct perf_event
*leader
= event
->group_leader
;
804 * If this is a task context, we need to check whether it is
805 * the current task context of this cpu. If not it has been
806 * scheduled out before the smp call arrived.
807 * Or possibly this is the right context but it isn't
808 * on this cpu because it had no events.
810 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
811 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
813 cpuctx
->task_ctx
= ctx
;
816 raw_spin_lock(&ctx
->lock
);
818 update_context_time(ctx
);
821 * Protect the list operation against NMI by disabling the
822 * events on a global level. NOP for non NMI based events.
826 add_event_to_ctx(event
, ctx
);
828 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
832 * Don't put the event on if it is disabled or if
833 * it is in a group and the group isn't on.
835 if (event
->state
!= PERF_EVENT_STATE_INACTIVE
||
836 (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
))
840 * An exclusive event can't go on if there are already active
841 * hardware events, and no hardware event can go on if there
842 * is already an exclusive event on.
844 if (!group_can_go_on(event
, cpuctx
, 1))
847 err
= event_sched_in(event
, cpuctx
, ctx
);
851 * This event couldn't go on. If it is in a group
852 * then we have to pull the whole group off.
853 * If the event group is pinned then put it in error state.
856 group_sched_out(leader
, cpuctx
, ctx
);
857 if (leader
->attr
.pinned
) {
858 update_group_times(leader
);
859 leader
->state
= PERF_EVENT_STATE_ERROR
;
863 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
864 cpuctx
->max_pertask
--;
869 raw_spin_unlock(&ctx
->lock
);
873 * Attach a performance event to a context
875 * First we add the event to the list with the hardware enable bit
876 * in event->hw_config cleared.
878 * If the event is attached to a task which is on a CPU we use a smp
879 * call to enable it in the task context. The task might have been
880 * scheduled away, but we check this in the smp call again.
882 * Must be called with ctx->mutex held.
885 perf_install_in_context(struct perf_event_context
*ctx
,
886 struct perf_event
*event
,
889 struct task_struct
*task
= ctx
->task
;
893 * Per cpu events are installed via an smp call and
894 * the install is always successful.
896 smp_call_function_single(cpu
, __perf_install_in_context
,
902 task_oncpu_function_call(task
, __perf_install_in_context
,
905 raw_spin_lock_irq(&ctx
->lock
);
907 * we need to retry the smp call.
909 if (ctx
->is_active
&& list_empty(&event
->group_entry
)) {
910 raw_spin_unlock_irq(&ctx
->lock
);
915 * The lock prevents that this context is scheduled in so we
916 * can add the event safely, if it the call above did not
919 if (list_empty(&event
->group_entry
))
920 add_event_to_ctx(event
, ctx
);
921 raw_spin_unlock_irq(&ctx
->lock
);
925 * Put a event into inactive state and update time fields.
926 * Enabling the leader of a group effectively enables all
927 * the group members that aren't explicitly disabled, so we
928 * have to update their ->tstamp_enabled also.
929 * Note: this works for group members as well as group leaders
930 * since the non-leader members' sibling_lists will be empty.
932 static void __perf_event_mark_enabled(struct perf_event
*event
,
933 struct perf_event_context
*ctx
)
935 struct perf_event
*sub
;
937 event
->state
= PERF_EVENT_STATE_INACTIVE
;
938 event
->tstamp_enabled
= ctx
->time
- event
->total_time_enabled
;
939 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
)
940 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
)
941 sub
->tstamp_enabled
=
942 ctx
->time
- sub
->total_time_enabled
;
946 * Cross CPU call to enable a performance event
948 static void __perf_event_enable(void *info
)
950 struct perf_event
*event
= info
;
951 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
952 struct perf_event_context
*ctx
= event
->ctx
;
953 struct perf_event
*leader
= event
->group_leader
;
957 * If this is a per-task event, need to check whether this
958 * event's task is the current task on this cpu.
960 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
961 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
963 cpuctx
->task_ctx
= ctx
;
966 raw_spin_lock(&ctx
->lock
);
968 update_context_time(ctx
);
970 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
972 __perf_event_mark_enabled(event
, ctx
);
974 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
978 * If the event is in a group and isn't the group leader,
979 * then don't put it on unless the group is on.
981 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
)
984 if (!group_can_go_on(event
, cpuctx
, 1)) {
989 err
= group_sched_in(event
, cpuctx
, ctx
);
991 err
= event_sched_in(event
, cpuctx
, ctx
);
997 * If this event can't go on and it's part of a
998 * group, then the whole group has to come off.
1000 if (leader
!= event
)
1001 group_sched_out(leader
, cpuctx
, ctx
);
1002 if (leader
->attr
.pinned
) {
1003 update_group_times(leader
);
1004 leader
->state
= PERF_EVENT_STATE_ERROR
;
1009 raw_spin_unlock(&ctx
->lock
);
1015 * If event->ctx is a cloned context, callers must make sure that
1016 * every task struct that event->ctx->task could possibly point to
1017 * remains valid. This condition is satisfied when called through
1018 * perf_event_for_each_child or perf_event_for_each as described
1019 * for perf_event_disable.
1021 void perf_event_enable(struct perf_event
*event
)
1023 struct perf_event_context
*ctx
= event
->ctx
;
1024 struct task_struct
*task
= ctx
->task
;
1028 * Enable the event on the cpu that it's on
1030 smp_call_function_single(event
->cpu
, __perf_event_enable
,
1035 raw_spin_lock_irq(&ctx
->lock
);
1036 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1040 * If the event is in error state, clear that first.
1041 * That way, if we see the event in error state below, we
1042 * know that it has gone back into error state, as distinct
1043 * from the task having been scheduled away before the
1044 * cross-call arrived.
1046 if (event
->state
== PERF_EVENT_STATE_ERROR
)
1047 event
->state
= PERF_EVENT_STATE_OFF
;
1050 raw_spin_unlock_irq(&ctx
->lock
);
1051 task_oncpu_function_call(task
, __perf_event_enable
, event
);
1053 raw_spin_lock_irq(&ctx
->lock
);
1056 * If the context is active and the event is still off,
1057 * we need to retry the cross-call.
1059 if (ctx
->is_active
&& event
->state
== PERF_EVENT_STATE_OFF
)
1063 * Since we have the lock this context can't be scheduled
1064 * in, so we can change the state safely.
1066 if (event
->state
== PERF_EVENT_STATE_OFF
)
1067 __perf_event_mark_enabled(event
, ctx
);
1070 raw_spin_unlock_irq(&ctx
->lock
);
1073 static int perf_event_refresh(struct perf_event
*event
, int refresh
)
1076 * not supported on inherited events
1078 if (event
->attr
.inherit
)
1081 atomic_add(refresh
, &event
->event_limit
);
1082 perf_event_enable(event
);
1088 EVENT_FLEXIBLE
= 0x1,
1090 EVENT_ALL
= EVENT_FLEXIBLE
| EVENT_PINNED
,
1093 static void ctx_sched_out(struct perf_event_context
*ctx
,
1094 struct perf_cpu_context
*cpuctx
,
1095 enum event_type_t event_type
)
1097 struct perf_event
*event
;
1099 raw_spin_lock(&ctx
->lock
);
1101 if (likely(!ctx
->nr_events
))
1103 update_context_time(ctx
);
1106 if (!ctx
->nr_active
)
1109 if (event_type
& EVENT_PINNED
)
1110 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
)
1111 group_sched_out(event
, cpuctx
, ctx
);
1113 if (event_type
& EVENT_FLEXIBLE
)
1114 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
)
1115 group_sched_out(event
, cpuctx
, ctx
);
1120 raw_spin_unlock(&ctx
->lock
);
1124 * Test whether two contexts are equivalent, i.e. whether they
1125 * have both been cloned from the same version of the same context
1126 * and they both have the same number of enabled events.
1127 * If the number of enabled events is the same, then the set
1128 * of enabled events should be the same, because these are both
1129 * inherited contexts, therefore we can't access individual events
1130 * in them directly with an fd; we can only enable/disable all
1131 * events via prctl, or enable/disable all events in a family
1132 * via ioctl, which will have the same effect on both contexts.
1134 static int context_equiv(struct perf_event_context
*ctx1
,
1135 struct perf_event_context
*ctx2
)
1137 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
1138 && ctx1
->parent_gen
== ctx2
->parent_gen
1139 && !ctx1
->pin_count
&& !ctx2
->pin_count
;
1142 static void __perf_event_sync_stat(struct perf_event
*event
,
1143 struct perf_event
*next_event
)
1147 if (!event
->attr
.inherit_stat
)
1151 * Update the event value, we cannot use perf_event_read()
1152 * because we're in the middle of a context switch and have IRQs
1153 * disabled, which upsets smp_call_function_single(), however
1154 * we know the event must be on the current CPU, therefore we
1155 * don't need to use it.
1157 switch (event
->state
) {
1158 case PERF_EVENT_STATE_ACTIVE
:
1159 event
->pmu
->read(event
);
1162 case PERF_EVENT_STATE_INACTIVE
:
1163 update_event_times(event
);
1171 * In order to keep per-task stats reliable we need to flip the event
1172 * values when we flip the contexts.
1174 value
= local64_read(&next_event
->count
);
1175 value
= local64_xchg(&event
->count
, value
);
1176 local64_set(&next_event
->count
, value
);
1178 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
1179 swap(event
->total_time_running
, next_event
->total_time_running
);
1182 * Since we swizzled the values, update the user visible data too.
1184 perf_event_update_userpage(event
);
1185 perf_event_update_userpage(next_event
);
1188 #define list_next_entry(pos, member) \
1189 list_entry(pos->member.next, typeof(*pos), member)
1191 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
1192 struct perf_event_context
*next_ctx
)
1194 struct perf_event
*event
, *next_event
;
1199 update_context_time(ctx
);
1201 event
= list_first_entry(&ctx
->event_list
,
1202 struct perf_event
, event_entry
);
1204 next_event
= list_first_entry(&next_ctx
->event_list
,
1205 struct perf_event
, event_entry
);
1207 while (&event
->event_entry
!= &ctx
->event_list
&&
1208 &next_event
->event_entry
!= &next_ctx
->event_list
) {
1210 __perf_event_sync_stat(event
, next_event
);
1212 event
= list_next_entry(event
, event_entry
);
1213 next_event
= list_next_entry(next_event
, event_entry
);
1218 * Called from scheduler to remove the events of the current task,
1219 * with interrupts disabled.
1221 * We stop each event and update the event value in event->count.
1223 * This does not protect us against NMI, but disable()
1224 * sets the disabled bit in the control field of event _before_
1225 * accessing the event control register. If a NMI hits, then it will
1226 * not restart the event.
1228 void perf_event_task_sched_out(struct task_struct
*task
,
1229 struct task_struct
*next
)
1231 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1232 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
1233 struct perf_event_context
*next_ctx
;
1234 struct perf_event_context
*parent
;
1237 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES
, 1, 1, NULL
, 0);
1239 if (likely(!ctx
|| !cpuctx
->task_ctx
))
1243 parent
= rcu_dereference(ctx
->parent_ctx
);
1244 next_ctx
= next
->perf_event_ctxp
;
1245 if (parent
&& next_ctx
&&
1246 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
1248 * Looks like the two contexts are clones, so we might be
1249 * able to optimize the context switch. We lock both
1250 * contexts and check that they are clones under the
1251 * lock (including re-checking that neither has been
1252 * uncloned in the meantime). It doesn't matter which
1253 * order we take the locks because no other cpu could
1254 * be trying to lock both of these tasks.
1256 raw_spin_lock(&ctx
->lock
);
1257 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
1258 if (context_equiv(ctx
, next_ctx
)) {
1260 * XXX do we need a memory barrier of sorts
1261 * wrt to rcu_dereference() of perf_event_ctxp
1263 task
->perf_event_ctxp
= next_ctx
;
1264 next
->perf_event_ctxp
= ctx
;
1266 next_ctx
->task
= task
;
1269 perf_event_sync_stat(ctx
, next_ctx
);
1271 raw_spin_unlock(&next_ctx
->lock
);
1272 raw_spin_unlock(&ctx
->lock
);
1277 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
1278 cpuctx
->task_ctx
= NULL
;
1282 static void task_ctx_sched_out(struct perf_event_context
*ctx
,
1283 enum event_type_t event_type
)
1285 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1287 if (!cpuctx
->task_ctx
)
1290 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
1293 ctx_sched_out(ctx
, cpuctx
, event_type
);
1294 cpuctx
->task_ctx
= NULL
;
1298 * Called with IRQs disabled
1300 static void __perf_event_task_sched_out(struct perf_event_context
*ctx
)
1302 task_ctx_sched_out(ctx
, EVENT_ALL
);
1306 * Called with IRQs disabled
1308 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
1309 enum event_type_t event_type
)
1311 ctx_sched_out(&cpuctx
->ctx
, cpuctx
, event_type
);
1315 ctx_pinned_sched_in(struct perf_event_context
*ctx
,
1316 struct perf_cpu_context
*cpuctx
)
1318 struct perf_event
*event
;
1320 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
1321 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1323 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1326 if (group_can_go_on(event
, cpuctx
, 1))
1327 group_sched_in(event
, cpuctx
, ctx
);
1330 * If this pinned group hasn't been scheduled,
1331 * put it in error state.
1333 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1334 update_group_times(event
);
1335 event
->state
= PERF_EVENT_STATE_ERROR
;
1341 ctx_flexible_sched_in(struct perf_event_context
*ctx
,
1342 struct perf_cpu_context
*cpuctx
)
1344 struct perf_event
*event
;
1347 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
1348 /* Ignore events in OFF or ERROR state */
1349 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1352 * Listen to the 'cpu' scheduling filter constraint
1355 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1358 if (group_can_go_on(event
, cpuctx
, can_add_hw
))
1359 if (group_sched_in(event
, cpuctx
, ctx
))
1365 ctx_sched_in(struct perf_event_context
*ctx
,
1366 struct perf_cpu_context
*cpuctx
,
1367 enum event_type_t event_type
)
1369 raw_spin_lock(&ctx
->lock
);
1371 if (likely(!ctx
->nr_events
))
1374 ctx
->timestamp
= perf_clock();
1379 * First go through the list and put on any pinned groups
1380 * in order to give them the best chance of going on.
1382 if (event_type
& EVENT_PINNED
)
1383 ctx_pinned_sched_in(ctx
, cpuctx
);
1385 /* Then walk through the lower prio flexible groups */
1386 if (event_type
& EVENT_FLEXIBLE
)
1387 ctx_flexible_sched_in(ctx
, cpuctx
);
1391 raw_spin_unlock(&ctx
->lock
);
1394 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
1395 enum event_type_t event_type
)
1397 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
1399 ctx_sched_in(ctx
, cpuctx
, event_type
);
1402 static void task_ctx_sched_in(struct task_struct
*task
,
1403 enum event_type_t event_type
)
1405 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1406 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
1410 if (cpuctx
->task_ctx
== ctx
)
1412 ctx_sched_in(ctx
, cpuctx
, event_type
);
1413 cpuctx
->task_ctx
= ctx
;
1416 * Called from scheduler to add the events of the current task
1417 * with interrupts disabled.
1419 * We restore the event value and then enable it.
1421 * This does not protect us against NMI, but enable()
1422 * sets the enabled bit in the control field of event _before_
1423 * accessing the event control register. If a NMI hits, then it will
1424 * keep the event running.
1426 void perf_event_task_sched_in(struct task_struct
*task
)
1428 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1429 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
1434 if (cpuctx
->task_ctx
== ctx
)
1440 * We want to keep the following priority order:
1441 * cpu pinned (that don't need to move), task pinned,
1442 * cpu flexible, task flexible.
1444 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
1446 ctx_sched_in(ctx
, cpuctx
, EVENT_PINNED
);
1447 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
);
1448 ctx_sched_in(ctx
, cpuctx
, EVENT_FLEXIBLE
);
1450 cpuctx
->task_ctx
= ctx
;
1455 #define MAX_INTERRUPTS (~0ULL)
1457 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1459 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1461 u64 frequency
= event
->attr
.sample_freq
;
1462 u64 sec
= NSEC_PER_SEC
;
1463 u64 divisor
, dividend
;
1465 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
1467 count_fls
= fls64(count
);
1468 nsec_fls
= fls64(nsec
);
1469 frequency_fls
= fls64(frequency
);
1473 * We got @count in @nsec, with a target of sample_freq HZ
1474 * the target period becomes:
1477 * period = -------------------
1478 * @nsec * sample_freq
1483 * Reduce accuracy by one bit such that @a and @b converge
1484 * to a similar magnitude.
1486 #define REDUCE_FLS(a, b) \
1488 if (a##_fls > b##_fls) { \
1498 * Reduce accuracy until either term fits in a u64, then proceed with
1499 * the other, so that finally we can do a u64/u64 division.
1501 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
1502 REDUCE_FLS(nsec
, frequency
);
1503 REDUCE_FLS(sec
, count
);
1506 if (count_fls
+ sec_fls
> 64) {
1507 divisor
= nsec
* frequency
;
1509 while (count_fls
+ sec_fls
> 64) {
1510 REDUCE_FLS(count
, sec
);
1514 dividend
= count
* sec
;
1516 dividend
= count
* sec
;
1518 while (nsec_fls
+ frequency_fls
> 64) {
1519 REDUCE_FLS(nsec
, frequency
);
1523 divisor
= nsec
* frequency
;
1529 return div64_u64(dividend
, divisor
);
1532 static void perf_event_stop(struct perf_event
*event
)
1534 if (!event
->pmu
->stop
)
1535 return event
->pmu
->disable(event
);
1537 return event
->pmu
->stop(event
);
1540 static int perf_event_start(struct perf_event
*event
)
1542 if (!event
->pmu
->start
)
1543 return event
->pmu
->enable(event
);
1545 return event
->pmu
->start(event
);
1548 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1550 struct hw_perf_event
*hwc
= &event
->hw
;
1551 s64 period
, sample_period
;
1554 period
= perf_calculate_period(event
, nsec
, count
);
1556 delta
= (s64
)(period
- hwc
->sample_period
);
1557 delta
= (delta
+ 7) / 8; /* low pass filter */
1559 sample_period
= hwc
->sample_period
+ delta
;
1564 hwc
->sample_period
= sample_period
;
1566 if (local64_read(&hwc
->period_left
) > 8*sample_period
) {
1568 perf_event_stop(event
);
1569 local64_set(&hwc
->period_left
, 0);
1570 perf_event_start(event
);
1575 static void perf_ctx_adjust_freq(struct perf_event_context
*ctx
)
1577 struct perf_event
*event
;
1578 struct hw_perf_event
*hwc
;
1579 u64 interrupts
, now
;
1582 raw_spin_lock(&ctx
->lock
);
1583 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
1584 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1587 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1592 interrupts
= hwc
->interrupts
;
1593 hwc
->interrupts
= 0;
1596 * unthrottle events on the tick
1598 if (interrupts
== MAX_INTERRUPTS
) {
1599 perf_log_throttle(event
, 1);
1601 event
->pmu
->unthrottle(event
);
1605 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
1609 event
->pmu
->read(event
);
1610 now
= local64_read(&event
->count
);
1611 delta
= now
- hwc
->freq_count_stamp
;
1612 hwc
->freq_count_stamp
= now
;
1615 perf_adjust_period(event
, TICK_NSEC
, delta
);
1618 raw_spin_unlock(&ctx
->lock
);
1622 * Round-robin a context's events:
1624 static void rotate_ctx(struct perf_event_context
*ctx
)
1626 raw_spin_lock(&ctx
->lock
);
1628 /* Rotate the first entry last of non-pinned groups */
1629 list_rotate_left(&ctx
->flexible_groups
);
1631 raw_spin_unlock(&ctx
->lock
);
1634 void perf_event_task_tick(struct task_struct
*curr
)
1636 struct perf_cpu_context
*cpuctx
;
1637 struct perf_event_context
*ctx
;
1640 if (!atomic_read(&nr_events
))
1643 cpuctx
= &__get_cpu_var(perf_cpu_context
);
1644 if (cpuctx
->ctx
.nr_events
&&
1645 cpuctx
->ctx
.nr_events
!= cpuctx
->ctx
.nr_active
)
1648 ctx
= curr
->perf_event_ctxp
;
1649 if (ctx
&& ctx
->nr_events
&& ctx
->nr_events
!= ctx
->nr_active
)
1652 perf_ctx_adjust_freq(&cpuctx
->ctx
);
1654 perf_ctx_adjust_freq(ctx
);
1660 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
1662 task_ctx_sched_out(ctx
, EVENT_FLEXIBLE
);
1664 rotate_ctx(&cpuctx
->ctx
);
1668 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
);
1670 task_ctx_sched_in(curr
, EVENT_FLEXIBLE
);
1674 static int event_enable_on_exec(struct perf_event
*event
,
1675 struct perf_event_context
*ctx
)
1677 if (!event
->attr
.enable_on_exec
)
1680 event
->attr
.enable_on_exec
= 0;
1681 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1684 __perf_event_mark_enabled(event
, ctx
);
1690 * Enable all of a task's events that have been marked enable-on-exec.
1691 * This expects task == current.
1693 static void perf_event_enable_on_exec(struct task_struct
*task
)
1695 struct perf_event_context
*ctx
;
1696 struct perf_event
*event
;
1697 unsigned long flags
;
1701 local_irq_save(flags
);
1702 ctx
= task
->perf_event_ctxp
;
1703 if (!ctx
|| !ctx
->nr_events
)
1706 __perf_event_task_sched_out(ctx
);
1708 raw_spin_lock(&ctx
->lock
);
1710 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
1711 ret
= event_enable_on_exec(event
, ctx
);
1716 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
1717 ret
= event_enable_on_exec(event
, ctx
);
1723 * Unclone this context if we enabled any event.
1728 raw_spin_unlock(&ctx
->lock
);
1730 perf_event_task_sched_in(task
);
1732 local_irq_restore(flags
);
1736 * Cross CPU call to read the hardware event
1738 static void __perf_event_read(void *info
)
1740 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1741 struct perf_event
*event
= info
;
1742 struct perf_event_context
*ctx
= event
->ctx
;
1745 * If this is a task context, we need to check whether it is
1746 * the current task context of this cpu. If not it has been
1747 * scheduled out before the smp call arrived. In that case
1748 * event->count would have been updated to a recent sample
1749 * when the event was scheduled out.
1751 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
1754 raw_spin_lock(&ctx
->lock
);
1755 update_context_time(ctx
);
1756 update_event_times(event
);
1757 raw_spin_unlock(&ctx
->lock
);
1759 event
->pmu
->read(event
);
1762 static inline u64
perf_event_count(struct perf_event
*event
)
1764 return local64_read(&event
->count
) + atomic64_read(&event
->child_count
);
1767 static u64
perf_event_read(struct perf_event
*event
)
1770 * If event is enabled and currently active on a CPU, update the
1771 * value in the event structure:
1773 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
1774 smp_call_function_single(event
->oncpu
,
1775 __perf_event_read
, event
, 1);
1776 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1777 struct perf_event_context
*ctx
= event
->ctx
;
1778 unsigned long flags
;
1780 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
1781 update_context_time(ctx
);
1782 update_event_times(event
);
1783 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1786 return perf_event_count(event
);
1790 * Initialize the perf_event context in a task_struct:
1793 __perf_event_init_context(struct perf_event_context
*ctx
,
1794 struct task_struct
*task
)
1796 raw_spin_lock_init(&ctx
->lock
);
1797 mutex_init(&ctx
->mutex
);
1798 INIT_LIST_HEAD(&ctx
->pinned_groups
);
1799 INIT_LIST_HEAD(&ctx
->flexible_groups
);
1800 INIT_LIST_HEAD(&ctx
->event_list
);
1801 atomic_set(&ctx
->refcount
, 1);
1805 static struct perf_event_context
*find_get_context(pid_t pid
, int cpu
)
1807 struct perf_event_context
*ctx
;
1808 struct perf_cpu_context
*cpuctx
;
1809 struct task_struct
*task
;
1810 unsigned long flags
;
1813 if (pid
== -1 && cpu
!= -1) {
1814 /* Must be root to operate on a CPU event: */
1815 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
1816 return ERR_PTR(-EACCES
);
1818 if (cpu
< 0 || cpu
>= nr_cpumask_bits
)
1819 return ERR_PTR(-EINVAL
);
1822 * We could be clever and allow to attach a event to an
1823 * offline CPU and activate it when the CPU comes up, but
1826 if (!cpu_online(cpu
))
1827 return ERR_PTR(-ENODEV
);
1829 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1840 task
= find_task_by_vpid(pid
);
1842 get_task_struct(task
);
1846 return ERR_PTR(-ESRCH
);
1849 * Can't attach events to a dying task.
1852 if (task
->flags
& PF_EXITING
)
1855 /* Reuse ptrace permission checks for now. */
1857 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1861 ctx
= perf_lock_task_context(task
, &flags
);
1864 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1868 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
1872 __perf_event_init_context(ctx
, task
);
1874 if (cmpxchg(&task
->perf_event_ctxp
, NULL
, ctx
)) {
1876 * We raced with some other task; use
1877 * the context they set.
1882 get_task_struct(task
);
1885 put_task_struct(task
);
1889 put_task_struct(task
);
1890 return ERR_PTR(err
);
1893 static void perf_event_free_filter(struct perf_event
*event
);
1895 static void free_event_rcu(struct rcu_head
*head
)
1897 struct perf_event
*event
;
1899 event
= container_of(head
, struct perf_event
, rcu_head
);
1901 put_pid_ns(event
->ns
);
1902 perf_event_free_filter(event
);
1906 static void perf_pending_sync(struct perf_event
*event
);
1907 static void perf_buffer_put(struct perf_buffer
*buffer
);
1909 static void free_event(struct perf_event
*event
)
1911 perf_pending_sync(event
);
1913 if (!event
->parent
) {
1914 atomic_dec(&nr_events
);
1915 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
1916 atomic_dec(&nr_mmap_events
);
1917 if (event
->attr
.comm
)
1918 atomic_dec(&nr_comm_events
);
1919 if (event
->attr
.task
)
1920 atomic_dec(&nr_task_events
);
1923 if (event
->buffer
) {
1924 perf_buffer_put(event
->buffer
);
1925 event
->buffer
= NULL
;
1929 event
->destroy(event
);
1931 put_ctx(event
->ctx
);
1932 call_rcu(&event
->rcu_head
, free_event_rcu
);
1935 int perf_event_release_kernel(struct perf_event
*event
)
1937 struct perf_event_context
*ctx
= event
->ctx
;
1940 * Remove from the PMU, can't get re-enabled since we got
1941 * here because the last ref went.
1943 perf_event_disable(event
);
1945 WARN_ON_ONCE(ctx
->parent_ctx
);
1947 * There are two ways this annotation is useful:
1949 * 1) there is a lock recursion from perf_event_exit_task
1950 * see the comment there.
1952 * 2) there is a lock-inversion with mmap_sem through
1953 * perf_event_read_group(), which takes faults while
1954 * holding ctx->mutex, however this is called after
1955 * the last filedesc died, so there is no possibility
1956 * to trigger the AB-BA case.
1958 mutex_lock_nested(&ctx
->mutex
, SINGLE_DEPTH_NESTING
);
1959 raw_spin_lock_irq(&ctx
->lock
);
1960 perf_group_detach(event
);
1961 list_del_event(event
, ctx
);
1962 raw_spin_unlock_irq(&ctx
->lock
);
1963 mutex_unlock(&ctx
->mutex
);
1965 mutex_lock(&event
->owner
->perf_event_mutex
);
1966 list_del_init(&event
->owner_entry
);
1967 mutex_unlock(&event
->owner
->perf_event_mutex
);
1968 put_task_struct(event
->owner
);
1974 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
1977 * Called when the last reference to the file is gone.
1979 static int perf_release(struct inode
*inode
, struct file
*file
)
1981 struct perf_event
*event
= file
->private_data
;
1983 file
->private_data
= NULL
;
1985 return perf_event_release_kernel(event
);
1988 static int perf_event_read_size(struct perf_event
*event
)
1990 int entry
= sizeof(u64
); /* value */
1994 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1995 size
+= sizeof(u64
);
1997 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1998 size
+= sizeof(u64
);
2000 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
2001 entry
+= sizeof(u64
);
2003 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
2004 nr
+= event
->group_leader
->nr_siblings
;
2005 size
+= sizeof(u64
);
2013 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
2015 struct perf_event
*child
;
2021 mutex_lock(&event
->child_mutex
);
2022 total
+= perf_event_read(event
);
2023 *enabled
+= event
->total_time_enabled
+
2024 atomic64_read(&event
->child_total_time_enabled
);
2025 *running
+= event
->total_time_running
+
2026 atomic64_read(&event
->child_total_time_running
);
2028 list_for_each_entry(child
, &event
->child_list
, child_list
) {
2029 total
+= perf_event_read(child
);
2030 *enabled
+= child
->total_time_enabled
;
2031 *running
+= child
->total_time_running
;
2033 mutex_unlock(&event
->child_mutex
);
2037 EXPORT_SYMBOL_GPL(perf_event_read_value
);
2039 static int perf_event_read_group(struct perf_event
*event
,
2040 u64 read_format
, char __user
*buf
)
2042 struct perf_event
*leader
= event
->group_leader
, *sub
;
2043 int n
= 0, size
= 0, ret
= -EFAULT
;
2044 struct perf_event_context
*ctx
= leader
->ctx
;
2046 u64 count
, enabled
, running
;
2048 mutex_lock(&ctx
->mutex
);
2049 count
= perf_event_read_value(leader
, &enabled
, &running
);
2051 values
[n
++] = 1 + leader
->nr_siblings
;
2052 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2053 values
[n
++] = enabled
;
2054 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2055 values
[n
++] = running
;
2056 values
[n
++] = count
;
2057 if (read_format
& PERF_FORMAT_ID
)
2058 values
[n
++] = primary_event_id(leader
);
2060 size
= n
* sizeof(u64
);
2062 if (copy_to_user(buf
, values
, size
))
2067 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
2070 values
[n
++] = perf_event_read_value(sub
, &enabled
, &running
);
2071 if (read_format
& PERF_FORMAT_ID
)
2072 values
[n
++] = primary_event_id(sub
);
2074 size
= n
* sizeof(u64
);
2076 if (copy_to_user(buf
+ ret
, values
, size
)) {
2084 mutex_unlock(&ctx
->mutex
);
2089 static int perf_event_read_one(struct perf_event
*event
,
2090 u64 read_format
, char __user
*buf
)
2092 u64 enabled
, running
;
2096 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
2097 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2098 values
[n
++] = enabled
;
2099 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2100 values
[n
++] = running
;
2101 if (read_format
& PERF_FORMAT_ID
)
2102 values
[n
++] = primary_event_id(event
);
2104 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
2107 return n
* sizeof(u64
);
2111 * Read the performance event - simple non blocking version for now
2114 perf_read_hw(struct perf_event
*event
, char __user
*buf
, size_t count
)
2116 u64 read_format
= event
->attr
.read_format
;
2120 * Return end-of-file for a read on a event that is in
2121 * error state (i.e. because it was pinned but it couldn't be
2122 * scheduled on to the CPU at some point).
2124 if (event
->state
== PERF_EVENT_STATE_ERROR
)
2127 if (count
< perf_event_read_size(event
))
2130 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2131 if (read_format
& PERF_FORMAT_GROUP
)
2132 ret
= perf_event_read_group(event
, read_format
, buf
);
2134 ret
= perf_event_read_one(event
, read_format
, buf
);
2140 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
2142 struct perf_event
*event
= file
->private_data
;
2144 return perf_read_hw(event
, buf
, count
);
2147 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
2149 struct perf_event
*event
= file
->private_data
;
2150 struct perf_buffer
*buffer
;
2151 unsigned int events
= POLL_HUP
;
2154 buffer
= rcu_dereference(event
->buffer
);
2156 events
= atomic_xchg(&buffer
->poll
, 0);
2159 poll_wait(file
, &event
->waitq
, wait
);
2164 static void perf_event_reset(struct perf_event
*event
)
2166 (void)perf_event_read(event
);
2167 local64_set(&event
->count
, 0);
2168 perf_event_update_userpage(event
);
2172 * Holding the top-level event's child_mutex means that any
2173 * descendant process that has inherited this event will block
2174 * in sync_child_event if it goes to exit, thus satisfying the
2175 * task existence requirements of perf_event_enable/disable.
2177 static void perf_event_for_each_child(struct perf_event
*event
,
2178 void (*func
)(struct perf_event
*))
2180 struct perf_event
*child
;
2182 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2183 mutex_lock(&event
->child_mutex
);
2185 list_for_each_entry(child
, &event
->child_list
, child_list
)
2187 mutex_unlock(&event
->child_mutex
);
2190 static void perf_event_for_each(struct perf_event
*event
,
2191 void (*func
)(struct perf_event
*))
2193 struct perf_event_context
*ctx
= event
->ctx
;
2194 struct perf_event
*sibling
;
2196 WARN_ON_ONCE(ctx
->parent_ctx
);
2197 mutex_lock(&ctx
->mutex
);
2198 event
= event
->group_leader
;
2200 perf_event_for_each_child(event
, func
);
2202 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
2203 perf_event_for_each_child(event
, func
);
2204 mutex_unlock(&ctx
->mutex
);
2207 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
2209 struct perf_event_context
*ctx
= event
->ctx
;
2214 if (!event
->attr
.sample_period
)
2217 size
= copy_from_user(&value
, arg
, sizeof(value
));
2218 if (size
!= sizeof(value
))
2224 raw_spin_lock_irq(&ctx
->lock
);
2225 if (event
->attr
.freq
) {
2226 if (value
> sysctl_perf_event_sample_rate
) {
2231 event
->attr
.sample_freq
= value
;
2233 event
->attr
.sample_period
= value
;
2234 event
->hw
.sample_period
= value
;
2237 raw_spin_unlock_irq(&ctx
->lock
);
2242 static const struct file_operations perf_fops
;
2244 static struct perf_event
*perf_fget_light(int fd
, int *fput_needed
)
2248 file
= fget_light(fd
, fput_needed
);
2250 return ERR_PTR(-EBADF
);
2252 if (file
->f_op
!= &perf_fops
) {
2253 fput_light(file
, *fput_needed
);
2255 return ERR_PTR(-EBADF
);
2258 return file
->private_data
;
2261 static int perf_event_set_output(struct perf_event
*event
,
2262 struct perf_event
*output_event
);
2263 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
2265 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2267 struct perf_event
*event
= file
->private_data
;
2268 void (*func
)(struct perf_event
*);
2272 case PERF_EVENT_IOC_ENABLE
:
2273 func
= perf_event_enable
;
2275 case PERF_EVENT_IOC_DISABLE
:
2276 func
= perf_event_disable
;
2278 case PERF_EVENT_IOC_RESET
:
2279 func
= perf_event_reset
;
2282 case PERF_EVENT_IOC_REFRESH
:
2283 return perf_event_refresh(event
, arg
);
2285 case PERF_EVENT_IOC_PERIOD
:
2286 return perf_event_period(event
, (u64 __user
*)arg
);
2288 case PERF_EVENT_IOC_SET_OUTPUT
:
2290 struct perf_event
*output_event
= NULL
;
2291 int fput_needed
= 0;
2295 output_event
= perf_fget_light(arg
, &fput_needed
);
2296 if (IS_ERR(output_event
))
2297 return PTR_ERR(output_event
);
2300 ret
= perf_event_set_output(event
, output_event
);
2302 fput_light(output_event
->filp
, fput_needed
);
2307 case PERF_EVENT_IOC_SET_FILTER
:
2308 return perf_event_set_filter(event
, (void __user
*)arg
);
2314 if (flags
& PERF_IOC_FLAG_GROUP
)
2315 perf_event_for_each(event
, func
);
2317 perf_event_for_each_child(event
, func
);
2322 int perf_event_task_enable(void)
2324 struct perf_event
*event
;
2326 mutex_lock(¤t
->perf_event_mutex
);
2327 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2328 perf_event_for_each_child(event
, perf_event_enable
);
2329 mutex_unlock(¤t
->perf_event_mutex
);
2334 int perf_event_task_disable(void)
2336 struct perf_event
*event
;
2338 mutex_lock(¤t
->perf_event_mutex
);
2339 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2340 perf_event_for_each_child(event
, perf_event_disable
);
2341 mutex_unlock(¤t
->perf_event_mutex
);
2346 #ifndef PERF_EVENT_INDEX_OFFSET
2347 # define PERF_EVENT_INDEX_OFFSET 0
2350 static int perf_event_index(struct perf_event
*event
)
2352 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
2355 return event
->hw
.idx
+ 1 - PERF_EVENT_INDEX_OFFSET
;
2359 * Callers need to ensure there can be no nesting of this function, otherwise
2360 * the seqlock logic goes bad. We can not serialize this because the arch
2361 * code calls this from NMI context.
2363 void perf_event_update_userpage(struct perf_event
*event
)
2365 struct perf_event_mmap_page
*userpg
;
2366 struct perf_buffer
*buffer
;
2369 buffer
= rcu_dereference(event
->buffer
);
2373 userpg
= buffer
->user_page
;
2376 * Disable preemption so as to not let the corresponding user-space
2377 * spin too long if we get preempted.
2382 userpg
->index
= perf_event_index(event
);
2383 userpg
->offset
= perf_event_count(event
);
2384 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
2385 userpg
->offset
-= local64_read(&event
->hw
.prev_count
);
2387 userpg
->time_enabled
= event
->total_time_enabled
+
2388 atomic64_read(&event
->child_total_time_enabled
);
2390 userpg
->time_running
= event
->total_time_running
+
2391 atomic64_read(&event
->child_total_time_running
);
2400 static unsigned long perf_data_size(struct perf_buffer
*buffer
);
2403 perf_buffer_init(struct perf_buffer
*buffer
, long watermark
, int flags
)
2405 long max_size
= perf_data_size(buffer
);
2408 buffer
->watermark
= min(max_size
, watermark
);
2410 if (!buffer
->watermark
)
2411 buffer
->watermark
= max_size
/ 2;
2413 if (flags
& PERF_BUFFER_WRITABLE
)
2414 buffer
->writable
= 1;
2416 atomic_set(&buffer
->refcount
, 1);
2419 #ifndef CONFIG_PERF_USE_VMALLOC
2422 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2425 static struct page
*
2426 perf_mmap_to_page(struct perf_buffer
*buffer
, unsigned long pgoff
)
2428 if (pgoff
> buffer
->nr_pages
)
2432 return virt_to_page(buffer
->user_page
);
2434 return virt_to_page(buffer
->data_pages
[pgoff
- 1]);
2437 static void *perf_mmap_alloc_page(int cpu
)
2442 node
= (cpu
== -1) ? cpu
: cpu_to_node(cpu
);
2443 page
= alloc_pages_node(node
, GFP_KERNEL
| __GFP_ZERO
, 0);
2447 return page_address(page
);
2450 static struct perf_buffer
*
2451 perf_buffer_alloc(int nr_pages
, long watermark
, int cpu
, int flags
)
2453 struct perf_buffer
*buffer
;
2457 size
= sizeof(struct perf_buffer
);
2458 size
+= nr_pages
* sizeof(void *);
2460 buffer
= kzalloc(size
, GFP_KERNEL
);
2464 buffer
->user_page
= perf_mmap_alloc_page(cpu
);
2465 if (!buffer
->user_page
)
2466 goto fail_user_page
;
2468 for (i
= 0; i
< nr_pages
; i
++) {
2469 buffer
->data_pages
[i
] = perf_mmap_alloc_page(cpu
);
2470 if (!buffer
->data_pages
[i
])
2471 goto fail_data_pages
;
2474 buffer
->nr_pages
= nr_pages
;
2476 perf_buffer_init(buffer
, watermark
, flags
);
2481 for (i
--; i
>= 0; i
--)
2482 free_page((unsigned long)buffer
->data_pages
[i
]);
2484 free_page((unsigned long)buffer
->user_page
);
2493 static void perf_mmap_free_page(unsigned long addr
)
2495 struct page
*page
= virt_to_page((void *)addr
);
2497 page
->mapping
= NULL
;
2501 static void perf_buffer_free(struct perf_buffer
*buffer
)
2505 perf_mmap_free_page((unsigned long)buffer
->user_page
);
2506 for (i
= 0; i
< buffer
->nr_pages
; i
++)
2507 perf_mmap_free_page((unsigned long)buffer
->data_pages
[i
]);
2511 static inline int page_order(struct perf_buffer
*buffer
)
2519 * Back perf_mmap() with vmalloc memory.
2521 * Required for architectures that have d-cache aliasing issues.
2524 static inline int page_order(struct perf_buffer
*buffer
)
2526 return buffer
->page_order
;
2529 static struct page
*
2530 perf_mmap_to_page(struct perf_buffer
*buffer
, unsigned long pgoff
)
2532 if (pgoff
> (1UL << page_order(buffer
)))
2535 return vmalloc_to_page((void *)buffer
->user_page
+ pgoff
* PAGE_SIZE
);
2538 static void perf_mmap_unmark_page(void *addr
)
2540 struct page
*page
= vmalloc_to_page(addr
);
2542 page
->mapping
= NULL
;
2545 static void perf_buffer_free_work(struct work_struct
*work
)
2547 struct perf_buffer
*buffer
;
2551 buffer
= container_of(work
, struct perf_buffer
, work
);
2552 nr
= 1 << page_order(buffer
);
2554 base
= buffer
->user_page
;
2555 for (i
= 0; i
< nr
+ 1; i
++)
2556 perf_mmap_unmark_page(base
+ (i
* PAGE_SIZE
));
2562 static void perf_buffer_free(struct perf_buffer
*buffer
)
2564 schedule_work(&buffer
->work
);
2567 static struct perf_buffer
*
2568 perf_buffer_alloc(int nr_pages
, long watermark
, int cpu
, int flags
)
2570 struct perf_buffer
*buffer
;
2574 size
= sizeof(struct perf_buffer
);
2575 size
+= sizeof(void *);
2577 buffer
= kzalloc(size
, GFP_KERNEL
);
2581 INIT_WORK(&buffer
->work
, perf_buffer_free_work
);
2583 all_buf
= vmalloc_user((nr_pages
+ 1) * PAGE_SIZE
);
2587 buffer
->user_page
= all_buf
;
2588 buffer
->data_pages
[0] = all_buf
+ PAGE_SIZE
;
2589 buffer
->page_order
= ilog2(nr_pages
);
2590 buffer
->nr_pages
= 1;
2592 perf_buffer_init(buffer
, watermark
, flags
);
2605 static unsigned long perf_data_size(struct perf_buffer
*buffer
)
2607 return buffer
->nr_pages
<< (PAGE_SHIFT
+ page_order(buffer
));
2610 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2612 struct perf_event
*event
= vma
->vm_file
->private_data
;
2613 struct perf_buffer
*buffer
;
2614 int ret
= VM_FAULT_SIGBUS
;
2616 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
2617 if (vmf
->pgoff
== 0)
2623 buffer
= rcu_dereference(event
->buffer
);
2627 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
2630 vmf
->page
= perf_mmap_to_page(buffer
, vmf
->pgoff
);
2634 get_page(vmf
->page
);
2635 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
2636 vmf
->page
->index
= vmf
->pgoff
;
2645 static void perf_buffer_free_rcu(struct rcu_head
*rcu_head
)
2647 struct perf_buffer
*buffer
;
2649 buffer
= container_of(rcu_head
, struct perf_buffer
, rcu_head
);
2650 perf_buffer_free(buffer
);
2653 static struct perf_buffer
*perf_buffer_get(struct perf_event
*event
)
2655 struct perf_buffer
*buffer
;
2658 buffer
= rcu_dereference(event
->buffer
);
2660 if (!atomic_inc_not_zero(&buffer
->refcount
))
2668 static void perf_buffer_put(struct perf_buffer
*buffer
)
2670 if (!atomic_dec_and_test(&buffer
->refcount
))
2673 call_rcu(&buffer
->rcu_head
, perf_buffer_free_rcu
);
2676 static void perf_mmap_open(struct vm_area_struct
*vma
)
2678 struct perf_event
*event
= vma
->vm_file
->private_data
;
2680 atomic_inc(&event
->mmap_count
);
2683 static void perf_mmap_close(struct vm_area_struct
*vma
)
2685 struct perf_event
*event
= vma
->vm_file
->private_data
;
2687 if (atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
)) {
2688 unsigned long size
= perf_data_size(event
->buffer
);
2689 struct user_struct
*user
= event
->mmap_user
;
2690 struct perf_buffer
*buffer
= event
->buffer
;
2692 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &user
->locked_vm
);
2693 vma
->vm_mm
->locked_vm
-= event
->mmap_locked
;
2694 rcu_assign_pointer(event
->buffer
, NULL
);
2695 mutex_unlock(&event
->mmap_mutex
);
2697 perf_buffer_put(buffer
);
2702 static const struct vm_operations_struct perf_mmap_vmops
= {
2703 .open
= perf_mmap_open
,
2704 .close
= perf_mmap_close
,
2705 .fault
= perf_mmap_fault
,
2706 .page_mkwrite
= perf_mmap_fault
,
2709 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2711 struct perf_event
*event
= file
->private_data
;
2712 unsigned long user_locked
, user_lock_limit
;
2713 struct user_struct
*user
= current_user();
2714 unsigned long locked
, lock_limit
;
2715 struct perf_buffer
*buffer
;
2716 unsigned long vma_size
;
2717 unsigned long nr_pages
;
2718 long user_extra
, extra
;
2719 int ret
= 0, flags
= 0;
2722 * Don't allow mmap() of inherited per-task counters. This would
2723 * create a performance issue due to all children writing to the
2726 if (event
->cpu
== -1 && event
->attr
.inherit
)
2729 if (!(vma
->vm_flags
& VM_SHARED
))
2732 vma_size
= vma
->vm_end
- vma
->vm_start
;
2733 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
2736 * If we have buffer pages ensure they're a power-of-two number, so we
2737 * can do bitmasks instead of modulo.
2739 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
2742 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
2745 if (vma
->vm_pgoff
!= 0)
2748 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2749 mutex_lock(&event
->mmap_mutex
);
2750 if (event
->buffer
) {
2751 if (event
->buffer
->nr_pages
== nr_pages
)
2752 atomic_inc(&event
->buffer
->refcount
);
2758 user_extra
= nr_pages
+ 1;
2759 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
2762 * Increase the limit linearly with more CPUs:
2764 user_lock_limit
*= num_online_cpus();
2766 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
2769 if (user_locked
> user_lock_limit
)
2770 extra
= user_locked
- user_lock_limit
;
2772 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
2773 lock_limit
>>= PAGE_SHIFT
;
2774 locked
= vma
->vm_mm
->locked_vm
+ extra
;
2776 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
2777 !capable(CAP_IPC_LOCK
)) {
2782 WARN_ON(event
->buffer
);
2784 if (vma
->vm_flags
& VM_WRITE
)
2785 flags
|= PERF_BUFFER_WRITABLE
;
2787 buffer
= perf_buffer_alloc(nr_pages
, event
->attr
.wakeup_watermark
,
2793 rcu_assign_pointer(event
->buffer
, buffer
);
2795 atomic_long_add(user_extra
, &user
->locked_vm
);
2796 event
->mmap_locked
= extra
;
2797 event
->mmap_user
= get_current_user();
2798 vma
->vm_mm
->locked_vm
+= event
->mmap_locked
;
2802 atomic_inc(&event
->mmap_count
);
2803 mutex_unlock(&event
->mmap_mutex
);
2805 vma
->vm_flags
|= VM_RESERVED
;
2806 vma
->vm_ops
= &perf_mmap_vmops
;
2811 static int perf_fasync(int fd
, struct file
*filp
, int on
)
2813 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
2814 struct perf_event
*event
= filp
->private_data
;
2817 mutex_lock(&inode
->i_mutex
);
2818 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
2819 mutex_unlock(&inode
->i_mutex
);
2827 static const struct file_operations perf_fops
= {
2828 .llseek
= no_llseek
,
2829 .release
= perf_release
,
2832 .unlocked_ioctl
= perf_ioctl
,
2833 .compat_ioctl
= perf_ioctl
,
2835 .fasync
= perf_fasync
,
2841 * If there's data, ensure we set the poll() state and publish everything
2842 * to user-space before waking everybody up.
2845 void perf_event_wakeup(struct perf_event
*event
)
2847 wake_up_all(&event
->waitq
);
2849 if (event
->pending_kill
) {
2850 kill_fasync(&event
->fasync
, SIGIO
, event
->pending_kill
);
2851 event
->pending_kill
= 0;
2858 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2860 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2861 * single linked list and use cmpxchg() to add entries lockless.
2864 static void perf_pending_event(struct perf_pending_entry
*entry
)
2866 struct perf_event
*event
= container_of(entry
,
2867 struct perf_event
, pending
);
2869 if (event
->pending_disable
) {
2870 event
->pending_disable
= 0;
2871 __perf_event_disable(event
);
2874 if (event
->pending_wakeup
) {
2875 event
->pending_wakeup
= 0;
2876 perf_event_wakeup(event
);
2880 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2882 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
2886 static void perf_pending_queue(struct perf_pending_entry
*entry
,
2887 void (*func
)(struct perf_pending_entry
*))
2889 struct perf_pending_entry
**head
;
2891 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
2896 head
= &get_cpu_var(perf_pending_head
);
2899 entry
->next
= *head
;
2900 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
2902 set_perf_event_pending();
2904 put_cpu_var(perf_pending_head
);
2907 static int __perf_pending_run(void)
2909 struct perf_pending_entry
*list
;
2912 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
2913 while (list
!= PENDING_TAIL
) {
2914 void (*func
)(struct perf_pending_entry
*);
2915 struct perf_pending_entry
*entry
= list
;
2922 * Ensure we observe the unqueue before we issue the wakeup,
2923 * so that we won't be waiting forever.
2924 * -- see perf_not_pending().
2935 static inline int perf_not_pending(struct perf_event
*event
)
2938 * If we flush on whatever cpu we run, there is a chance we don't
2942 __perf_pending_run();
2946 * Ensure we see the proper queue state before going to sleep
2947 * so that we do not miss the wakeup. -- see perf_pending_handle()
2950 return event
->pending
.next
== NULL
;
2953 static void perf_pending_sync(struct perf_event
*event
)
2955 wait_event(event
->waitq
, perf_not_pending(event
));
2958 void perf_event_do_pending(void)
2960 __perf_pending_run();
2964 * Callchain support -- arch specific
2967 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
2974 * We assume there is only KVM supporting the callbacks.
2975 * Later on, we might change it to a list if there is
2976 * another virtualization implementation supporting the callbacks.
2978 struct perf_guest_info_callbacks
*perf_guest_cbs
;
2980 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
2982 perf_guest_cbs
= cbs
;
2985 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks
);
2987 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
2989 perf_guest_cbs
= NULL
;
2992 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks
);
2997 static bool perf_output_space(struct perf_buffer
*buffer
, unsigned long tail
,
2998 unsigned long offset
, unsigned long head
)
3002 if (!buffer
->writable
)
3005 mask
= perf_data_size(buffer
) - 1;
3007 offset
= (offset
- tail
) & mask
;
3008 head
= (head
- tail
) & mask
;
3010 if ((int)(head
- offset
) < 0)
3016 static void perf_output_wakeup(struct perf_output_handle
*handle
)
3018 atomic_set(&handle
->buffer
->poll
, POLL_IN
);
3021 handle
->event
->pending_wakeup
= 1;
3022 perf_pending_queue(&handle
->event
->pending
,
3023 perf_pending_event
);
3025 perf_event_wakeup(handle
->event
);
3029 * We need to ensure a later event_id doesn't publish a head when a former
3030 * event isn't done writing. However since we need to deal with NMIs we
3031 * cannot fully serialize things.
3033 * We only publish the head (and generate a wakeup) when the outer-most
3036 static void perf_output_get_handle(struct perf_output_handle
*handle
)
3038 struct perf_buffer
*buffer
= handle
->buffer
;
3041 local_inc(&buffer
->nest
);
3042 handle
->wakeup
= local_read(&buffer
->wakeup
);
3045 static void perf_output_put_handle(struct perf_output_handle
*handle
)
3047 struct perf_buffer
*buffer
= handle
->buffer
;
3051 head
= local_read(&buffer
->head
);
3054 * IRQ/NMI can happen here, which means we can miss a head update.
3057 if (!local_dec_and_test(&buffer
->nest
))
3061 * Publish the known good head. Rely on the full barrier implied
3062 * by atomic_dec_and_test() order the buffer->head read and this
3065 buffer
->user_page
->data_head
= head
;
3068 * Now check if we missed an update, rely on the (compiler)
3069 * barrier in atomic_dec_and_test() to re-read buffer->head.
3071 if (unlikely(head
!= local_read(&buffer
->head
))) {
3072 local_inc(&buffer
->nest
);
3076 if (handle
->wakeup
!= local_read(&buffer
->wakeup
))
3077 perf_output_wakeup(handle
);
3083 __always_inline
void perf_output_copy(struct perf_output_handle
*handle
,
3084 const void *buf
, unsigned int len
)
3087 unsigned long size
= min_t(unsigned long, handle
->size
, len
);
3089 memcpy(handle
->addr
, buf
, size
);
3092 handle
->addr
+= size
;
3094 handle
->size
-= size
;
3095 if (!handle
->size
) {
3096 struct perf_buffer
*buffer
= handle
->buffer
;
3099 handle
->page
&= buffer
->nr_pages
- 1;
3100 handle
->addr
= buffer
->data_pages
[handle
->page
];
3101 handle
->size
= PAGE_SIZE
<< page_order(buffer
);
3106 int perf_output_begin(struct perf_output_handle
*handle
,
3107 struct perf_event
*event
, unsigned int size
,
3108 int nmi
, int sample
)
3110 struct perf_buffer
*buffer
;
3111 unsigned long tail
, offset
, head
;
3114 struct perf_event_header header
;
3121 * For inherited events we send all the output towards the parent.
3124 event
= event
->parent
;
3126 buffer
= rcu_dereference(event
->buffer
);
3130 handle
->buffer
= buffer
;
3131 handle
->event
= event
;
3133 handle
->sample
= sample
;
3135 if (!buffer
->nr_pages
)
3138 have_lost
= local_read(&buffer
->lost
);
3140 size
+= sizeof(lost_event
);
3142 perf_output_get_handle(handle
);
3146 * Userspace could choose to issue a mb() before updating the
3147 * tail pointer. So that all reads will be completed before the
3150 tail
= ACCESS_ONCE(buffer
->user_page
->data_tail
);
3152 offset
= head
= local_read(&buffer
->head
);
3154 if (unlikely(!perf_output_space(buffer
, tail
, offset
, head
)))
3156 } while (local_cmpxchg(&buffer
->head
, offset
, head
) != offset
);
3158 if (head
- local_read(&buffer
->wakeup
) > buffer
->watermark
)
3159 local_add(buffer
->watermark
, &buffer
->wakeup
);
3161 handle
->page
= offset
>> (PAGE_SHIFT
+ page_order(buffer
));
3162 handle
->page
&= buffer
->nr_pages
- 1;
3163 handle
->size
= offset
& ((PAGE_SIZE
<< page_order(buffer
)) - 1);
3164 handle
->addr
= buffer
->data_pages
[handle
->page
];
3165 handle
->addr
+= handle
->size
;
3166 handle
->size
= (PAGE_SIZE
<< page_order(buffer
)) - handle
->size
;
3169 lost_event
.header
.type
= PERF_RECORD_LOST
;
3170 lost_event
.header
.misc
= 0;
3171 lost_event
.header
.size
= sizeof(lost_event
);
3172 lost_event
.id
= event
->id
;
3173 lost_event
.lost
= local_xchg(&buffer
->lost
, 0);
3175 perf_output_put(handle
, lost_event
);
3181 local_inc(&buffer
->lost
);
3182 perf_output_put_handle(handle
);
3189 void perf_output_end(struct perf_output_handle
*handle
)
3191 struct perf_event
*event
= handle
->event
;
3192 struct perf_buffer
*buffer
= handle
->buffer
;
3194 int wakeup_events
= event
->attr
.wakeup_events
;
3196 if (handle
->sample
&& wakeup_events
) {
3197 int events
= local_inc_return(&buffer
->events
);
3198 if (events
>= wakeup_events
) {
3199 local_sub(wakeup_events
, &buffer
->events
);
3200 local_inc(&buffer
->wakeup
);
3204 perf_output_put_handle(handle
);
3208 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
3211 * only top level events have the pid namespace they were created in
3214 event
= event
->parent
;
3216 return task_tgid_nr_ns(p
, event
->ns
);
3219 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
3222 * only top level events have the pid namespace they were created in
3225 event
= event
->parent
;
3227 return task_pid_nr_ns(p
, event
->ns
);
3230 static void perf_output_read_one(struct perf_output_handle
*handle
,
3231 struct perf_event
*event
)
3233 u64 read_format
= event
->attr
.read_format
;
3237 values
[n
++] = perf_event_count(event
);
3238 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
3239 values
[n
++] = event
->total_time_enabled
+
3240 atomic64_read(&event
->child_total_time_enabled
);
3242 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
3243 values
[n
++] = event
->total_time_running
+
3244 atomic64_read(&event
->child_total_time_running
);
3246 if (read_format
& PERF_FORMAT_ID
)
3247 values
[n
++] = primary_event_id(event
);
3249 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3253 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3255 static void perf_output_read_group(struct perf_output_handle
*handle
,
3256 struct perf_event
*event
)
3258 struct perf_event
*leader
= event
->group_leader
, *sub
;
3259 u64 read_format
= event
->attr
.read_format
;
3263 values
[n
++] = 1 + leader
->nr_siblings
;
3265 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3266 values
[n
++] = leader
->total_time_enabled
;
3268 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3269 values
[n
++] = leader
->total_time_running
;
3271 if (leader
!= event
)
3272 leader
->pmu
->read(leader
);
3274 values
[n
++] = perf_event_count(leader
);
3275 if (read_format
& PERF_FORMAT_ID
)
3276 values
[n
++] = primary_event_id(leader
);
3278 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3280 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3284 sub
->pmu
->read(sub
);
3286 values
[n
++] = perf_event_count(sub
);
3287 if (read_format
& PERF_FORMAT_ID
)
3288 values
[n
++] = primary_event_id(sub
);
3290 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3294 static void perf_output_read(struct perf_output_handle
*handle
,
3295 struct perf_event
*event
)
3297 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
3298 perf_output_read_group(handle
, event
);
3300 perf_output_read_one(handle
, event
);
3303 void perf_output_sample(struct perf_output_handle
*handle
,
3304 struct perf_event_header
*header
,
3305 struct perf_sample_data
*data
,
3306 struct perf_event
*event
)
3308 u64 sample_type
= data
->type
;
3310 perf_output_put(handle
, *header
);
3312 if (sample_type
& PERF_SAMPLE_IP
)
3313 perf_output_put(handle
, data
->ip
);
3315 if (sample_type
& PERF_SAMPLE_TID
)
3316 perf_output_put(handle
, data
->tid_entry
);
3318 if (sample_type
& PERF_SAMPLE_TIME
)
3319 perf_output_put(handle
, data
->time
);
3321 if (sample_type
& PERF_SAMPLE_ADDR
)
3322 perf_output_put(handle
, data
->addr
);
3324 if (sample_type
& PERF_SAMPLE_ID
)
3325 perf_output_put(handle
, data
->id
);
3327 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
3328 perf_output_put(handle
, data
->stream_id
);
3330 if (sample_type
& PERF_SAMPLE_CPU
)
3331 perf_output_put(handle
, data
->cpu_entry
);
3333 if (sample_type
& PERF_SAMPLE_PERIOD
)
3334 perf_output_put(handle
, data
->period
);
3336 if (sample_type
& PERF_SAMPLE_READ
)
3337 perf_output_read(handle
, event
);
3339 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3340 if (data
->callchain
) {
3343 if (data
->callchain
)
3344 size
+= data
->callchain
->nr
;
3346 size
*= sizeof(u64
);
3348 perf_output_copy(handle
, data
->callchain
, size
);
3351 perf_output_put(handle
, nr
);
3355 if (sample_type
& PERF_SAMPLE_RAW
) {
3357 perf_output_put(handle
, data
->raw
->size
);
3358 perf_output_copy(handle
, data
->raw
->data
,
3365 .size
= sizeof(u32
),
3368 perf_output_put(handle
, raw
);
3373 void perf_prepare_sample(struct perf_event_header
*header
,
3374 struct perf_sample_data
*data
,
3375 struct perf_event
*event
,
3376 struct pt_regs
*regs
)
3378 u64 sample_type
= event
->attr
.sample_type
;
3380 data
->type
= sample_type
;
3382 header
->type
= PERF_RECORD_SAMPLE
;
3383 header
->size
= sizeof(*header
);
3386 header
->misc
|= perf_misc_flags(regs
);
3388 if (sample_type
& PERF_SAMPLE_IP
) {
3389 data
->ip
= perf_instruction_pointer(regs
);
3391 header
->size
+= sizeof(data
->ip
);
3394 if (sample_type
& PERF_SAMPLE_TID
) {
3395 /* namespace issues */
3396 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
3397 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
3399 header
->size
+= sizeof(data
->tid_entry
);
3402 if (sample_type
& PERF_SAMPLE_TIME
) {
3403 data
->time
= perf_clock();
3405 header
->size
+= sizeof(data
->time
);
3408 if (sample_type
& PERF_SAMPLE_ADDR
)
3409 header
->size
+= sizeof(data
->addr
);
3411 if (sample_type
& PERF_SAMPLE_ID
) {
3412 data
->id
= primary_event_id(event
);
3414 header
->size
+= sizeof(data
->id
);
3417 if (sample_type
& PERF_SAMPLE_STREAM_ID
) {
3418 data
->stream_id
= event
->id
;
3420 header
->size
+= sizeof(data
->stream_id
);
3423 if (sample_type
& PERF_SAMPLE_CPU
) {
3424 data
->cpu_entry
.cpu
= raw_smp_processor_id();
3425 data
->cpu_entry
.reserved
= 0;
3427 header
->size
+= sizeof(data
->cpu_entry
);
3430 if (sample_type
& PERF_SAMPLE_PERIOD
)
3431 header
->size
+= sizeof(data
->period
);
3433 if (sample_type
& PERF_SAMPLE_READ
)
3434 header
->size
+= perf_event_read_size(event
);
3436 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3439 data
->callchain
= perf_callchain(regs
);
3441 if (data
->callchain
)
3442 size
+= data
->callchain
->nr
;
3444 header
->size
+= size
* sizeof(u64
);
3447 if (sample_type
& PERF_SAMPLE_RAW
) {
3448 int size
= sizeof(u32
);
3451 size
+= data
->raw
->size
;
3453 size
+= sizeof(u32
);
3455 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
3456 header
->size
+= size
;
3460 static void perf_event_output(struct perf_event
*event
, int nmi
,
3461 struct perf_sample_data
*data
,
3462 struct pt_regs
*regs
)
3464 struct perf_output_handle handle
;
3465 struct perf_event_header header
;
3467 perf_prepare_sample(&header
, data
, event
, regs
);
3469 if (perf_output_begin(&handle
, event
, header
.size
, nmi
, 1))
3472 perf_output_sample(&handle
, &header
, data
, event
);
3474 perf_output_end(&handle
);
3481 struct perf_read_event
{
3482 struct perf_event_header header
;
3489 perf_event_read_event(struct perf_event
*event
,
3490 struct task_struct
*task
)
3492 struct perf_output_handle handle
;
3493 struct perf_read_event read_event
= {
3495 .type
= PERF_RECORD_READ
,
3497 .size
= sizeof(read_event
) + perf_event_read_size(event
),
3499 .pid
= perf_event_pid(event
, task
),
3500 .tid
= perf_event_tid(event
, task
),
3504 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
, 0, 0);
3508 perf_output_put(&handle
, read_event
);
3509 perf_output_read(&handle
, event
);
3511 perf_output_end(&handle
);
3515 * task tracking -- fork/exit
3517 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
3520 struct perf_task_event
{
3521 struct task_struct
*task
;
3522 struct perf_event_context
*task_ctx
;
3525 struct perf_event_header header
;
3535 static void perf_event_task_output(struct perf_event
*event
,
3536 struct perf_task_event
*task_event
)
3538 struct perf_output_handle handle
;
3539 struct task_struct
*task
= task_event
->task
;
3542 size
= task_event
->event_id
.header
.size
;
3543 ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3548 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
3549 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
3551 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
3552 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
3554 perf_output_put(&handle
, task_event
->event_id
);
3556 perf_output_end(&handle
);
3559 static int perf_event_task_match(struct perf_event
*event
)
3561 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3564 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3567 if (event
->attr
.comm
|| event
->attr
.mmap
||
3568 event
->attr
.mmap_data
|| event
->attr
.task
)
3574 static void perf_event_task_ctx(struct perf_event_context
*ctx
,
3575 struct perf_task_event
*task_event
)
3577 struct perf_event
*event
;
3579 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3580 if (perf_event_task_match(event
))
3581 perf_event_task_output(event
, task_event
);
3585 static void perf_event_task_event(struct perf_task_event
*task_event
)
3587 struct perf_cpu_context
*cpuctx
;
3588 struct perf_event_context
*ctx
= task_event
->task_ctx
;
3591 cpuctx
= &get_cpu_var(perf_cpu_context
);
3592 perf_event_task_ctx(&cpuctx
->ctx
, task_event
);
3594 ctx
= rcu_dereference(current
->perf_event_ctxp
);
3596 perf_event_task_ctx(ctx
, task_event
);
3597 put_cpu_var(perf_cpu_context
);
3601 static void perf_event_task(struct task_struct
*task
,
3602 struct perf_event_context
*task_ctx
,
3605 struct perf_task_event task_event
;
3607 if (!atomic_read(&nr_comm_events
) &&
3608 !atomic_read(&nr_mmap_events
) &&
3609 !atomic_read(&nr_task_events
))
3612 task_event
= (struct perf_task_event
){
3614 .task_ctx
= task_ctx
,
3617 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
3619 .size
= sizeof(task_event
.event_id
),
3625 .time
= perf_clock(),
3629 perf_event_task_event(&task_event
);
3632 void perf_event_fork(struct task_struct
*task
)
3634 perf_event_task(task
, NULL
, 1);
3641 struct perf_comm_event
{
3642 struct task_struct
*task
;
3647 struct perf_event_header header
;
3654 static void perf_event_comm_output(struct perf_event
*event
,
3655 struct perf_comm_event
*comm_event
)
3657 struct perf_output_handle handle
;
3658 int size
= comm_event
->event_id
.header
.size
;
3659 int ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3664 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
3665 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
3667 perf_output_put(&handle
, comm_event
->event_id
);
3668 perf_output_copy(&handle
, comm_event
->comm
,
3669 comm_event
->comm_size
);
3670 perf_output_end(&handle
);
3673 static int perf_event_comm_match(struct perf_event
*event
)
3675 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3678 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3681 if (event
->attr
.comm
)
3687 static void perf_event_comm_ctx(struct perf_event_context
*ctx
,
3688 struct perf_comm_event
*comm_event
)
3690 struct perf_event
*event
;
3692 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3693 if (perf_event_comm_match(event
))
3694 perf_event_comm_output(event
, comm_event
);
3698 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
3700 struct perf_cpu_context
*cpuctx
;
3701 struct perf_event_context
*ctx
;
3703 char comm
[TASK_COMM_LEN
];
3705 memset(comm
, 0, sizeof(comm
));
3706 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
3707 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
3709 comm_event
->comm
= comm
;
3710 comm_event
->comm_size
= size
;
3712 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
3715 cpuctx
= &get_cpu_var(perf_cpu_context
);
3716 perf_event_comm_ctx(&cpuctx
->ctx
, comm_event
);
3717 ctx
= rcu_dereference(current
->perf_event_ctxp
);
3719 perf_event_comm_ctx(ctx
, comm_event
);
3720 put_cpu_var(perf_cpu_context
);
3724 void perf_event_comm(struct task_struct
*task
)
3726 struct perf_comm_event comm_event
;
3728 if (task
->perf_event_ctxp
)
3729 perf_event_enable_on_exec(task
);
3731 if (!atomic_read(&nr_comm_events
))
3734 comm_event
= (struct perf_comm_event
){
3740 .type
= PERF_RECORD_COMM
,
3749 perf_event_comm_event(&comm_event
);
3756 struct perf_mmap_event
{
3757 struct vm_area_struct
*vma
;
3759 const char *file_name
;
3763 struct perf_event_header header
;
3773 static void perf_event_mmap_output(struct perf_event
*event
,
3774 struct perf_mmap_event
*mmap_event
)
3776 struct perf_output_handle handle
;
3777 int size
= mmap_event
->event_id
.header
.size
;
3778 int ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3783 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
3784 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
3786 perf_output_put(&handle
, mmap_event
->event_id
);
3787 perf_output_copy(&handle
, mmap_event
->file_name
,
3788 mmap_event
->file_size
);
3789 perf_output_end(&handle
);
3792 static int perf_event_mmap_match(struct perf_event
*event
,
3793 struct perf_mmap_event
*mmap_event
,
3796 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3799 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3802 if ((!executable
&& event
->attr
.mmap_data
) ||
3803 (executable
&& event
->attr
.mmap
))
3809 static void perf_event_mmap_ctx(struct perf_event_context
*ctx
,
3810 struct perf_mmap_event
*mmap_event
,
3813 struct perf_event
*event
;
3815 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3816 if (perf_event_mmap_match(event
, mmap_event
, executable
))
3817 perf_event_mmap_output(event
, mmap_event
);
3821 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
3823 struct perf_cpu_context
*cpuctx
;
3824 struct perf_event_context
*ctx
;
3825 struct vm_area_struct
*vma
= mmap_event
->vma
;
3826 struct file
*file
= vma
->vm_file
;
3832 memset(tmp
, 0, sizeof(tmp
));
3836 * d_path works from the end of the buffer backwards, so we
3837 * need to add enough zero bytes after the string to handle
3838 * the 64bit alignment we do later.
3840 buf
= kzalloc(PATH_MAX
+ sizeof(u64
), GFP_KERNEL
);
3842 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
3845 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
3847 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
3851 if (arch_vma_name(mmap_event
->vma
)) {
3852 name
= strncpy(tmp
, arch_vma_name(mmap_event
->vma
),
3858 name
= strncpy(tmp
, "[vdso]", sizeof(tmp
));
3860 } else if (vma
->vm_start
<= vma
->vm_mm
->start_brk
&&
3861 vma
->vm_end
>= vma
->vm_mm
->brk
) {
3862 name
= strncpy(tmp
, "[heap]", sizeof(tmp
));
3864 } else if (vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
3865 vma
->vm_end
>= vma
->vm_mm
->start_stack
) {
3866 name
= strncpy(tmp
, "[stack]", sizeof(tmp
));
3870 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
3875 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
3877 mmap_event
->file_name
= name
;
3878 mmap_event
->file_size
= size
;
3880 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
3883 cpuctx
= &get_cpu_var(perf_cpu_context
);
3884 perf_event_mmap_ctx(&cpuctx
->ctx
, mmap_event
, vma
->vm_flags
& VM_EXEC
);
3885 ctx
= rcu_dereference(current
->perf_event_ctxp
);
3887 perf_event_mmap_ctx(ctx
, mmap_event
, vma
->vm_flags
& VM_EXEC
);
3888 put_cpu_var(perf_cpu_context
);
3894 void perf_event_mmap(struct vm_area_struct
*vma
)
3896 struct perf_mmap_event mmap_event
;
3898 if (!atomic_read(&nr_mmap_events
))
3901 mmap_event
= (struct perf_mmap_event
){
3907 .type
= PERF_RECORD_MMAP
,
3908 .misc
= PERF_RECORD_MISC_USER
,
3913 .start
= vma
->vm_start
,
3914 .len
= vma
->vm_end
- vma
->vm_start
,
3915 .pgoff
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
,
3919 perf_event_mmap_event(&mmap_event
);
3923 * IRQ throttle logging
3926 static void perf_log_throttle(struct perf_event
*event
, int enable
)
3928 struct perf_output_handle handle
;
3932 struct perf_event_header header
;
3936 } throttle_event
= {
3938 .type
= PERF_RECORD_THROTTLE
,
3940 .size
= sizeof(throttle_event
),
3942 .time
= perf_clock(),
3943 .id
= primary_event_id(event
),
3944 .stream_id
= event
->id
,
3948 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
3950 ret
= perf_output_begin(&handle
, event
, sizeof(throttle_event
), 1, 0);
3954 perf_output_put(&handle
, throttle_event
);
3955 perf_output_end(&handle
);
3959 * Generic event overflow handling, sampling.
3962 static int __perf_event_overflow(struct perf_event
*event
, int nmi
,
3963 int throttle
, struct perf_sample_data
*data
,
3964 struct pt_regs
*regs
)
3966 int events
= atomic_read(&event
->event_limit
);
3967 struct hw_perf_event
*hwc
= &event
->hw
;
3970 throttle
= (throttle
&& event
->pmu
->unthrottle
!= NULL
);
3975 if (hwc
->interrupts
!= MAX_INTERRUPTS
) {
3977 if (HZ
* hwc
->interrupts
>
3978 (u64
)sysctl_perf_event_sample_rate
) {
3979 hwc
->interrupts
= MAX_INTERRUPTS
;
3980 perf_log_throttle(event
, 0);
3985 * Keep re-disabling events even though on the previous
3986 * pass we disabled it - just in case we raced with a
3987 * sched-in and the event got enabled again:
3993 if (event
->attr
.freq
) {
3994 u64 now
= perf_clock();
3995 s64 delta
= now
- hwc
->freq_time_stamp
;
3997 hwc
->freq_time_stamp
= now
;
3999 if (delta
> 0 && delta
< 2*TICK_NSEC
)
4000 perf_adjust_period(event
, delta
, hwc
->last_period
);
4004 * XXX event_limit might not quite work as expected on inherited
4008 event
->pending_kill
= POLL_IN
;
4009 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
4011 event
->pending_kill
= POLL_HUP
;
4013 event
->pending_disable
= 1;
4014 perf_pending_queue(&event
->pending
,
4015 perf_pending_event
);
4017 perf_event_disable(event
);
4020 if (event
->overflow_handler
)
4021 event
->overflow_handler(event
, nmi
, data
, regs
);
4023 perf_event_output(event
, nmi
, data
, regs
);
4028 int perf_event_overflow(struct perf_event
*event
, int nmi
,
4029 struct perf_sample_data
*data
,
4030 struct pt_regs
*regs
)
4032 return __perf_event_overflow(event
, nmi
, 1, data
, regs
);
4036 * Generic software event infrastructure
4040 * We directly increment event->count and keep a second value in
4041 * event->hw.period_left to count intervals. This period event
4042 * is kept in the range [-sample_period, 0] so that we can use the
4046 static u64
perf_swevent_set_period(struct perf_event
*event
)
4048 struct hw_perf_event
*hwc
= &event
->hw
;
4049 u64 period
= hwc
->last_period
;
4053 hwc
->last_period
= hwc
->sample_period
;
4056 old
= val
= local64_read(&hwc
->period_left
);
4060 nr
= div64_u64(period
+ val
, period
);
4061 offset
= nr
* period
;
4063 if (local64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
4069 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
4070 int nmi
, struct perf_sample_data
*data
,
4071 struct pt_regs
*regs
)
4073 struct hw_perf_event
*hwc
= &event
->hw
;
4076 data
->period
= event
->hw
.last_period
;
4078 overflow
= perf_swevent_set_period(event
);
4080 if (hwc
->interrupts
== MAX_INTERRUPTS
)
4083 for (; overflow
; overflow
--) {
4084 if (__perf_event_overflow(event
, nmi
, throttle
,
4087 * We inhibit the overflow from happening when
4088 * hwc->interrupts == MAX_INTERRUPTS.
4096 static void perf_swevent_add(struct perf_event
*event
, u64 nr
,
4097 int nmi
, struct perf_sample_data
*data
,
4098 struct pt_regs
*regs
)
4100 struct hw_perf_event
*hwc
= &event
->hw
;
4102 local64_add(nr
, &event
->count
);
4107 if (!hwc
->sample_period
)
4110 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
4111 return perf_swevent_overflow(event
, 1, nmi
, data
, regs
);
4113 if (local64_add_negative(nr
, &hwc
->period_left
))
4116 perf_swevent_overflow(event
, 0, nmi
, data
, regs
);
4119 static int perf_exclude_event(struct perf_event
*event
,
4120 struct pt_regs
*regs
)
4123 if (event
->attr
.exclude_user
&& user_mode(regs
))
4126 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
4133 static int perf_swevent_match(struct perf_event
*event
,
4134 enum perf_type_id type
,
4136 struct perf_sample_data
*data
,
4137 struct pt_regs
*regs
)
4139 if (event
->attr
.type
!= type
)
4142 if (event
->attr
.config
!= event_id
)
4145 if (perf_exclude_event(event
, regs
))
4151 static inline u64
swevent_hash(u64 type
, u32 event_id
)
4153 u64 val
= event_id
| (type
<< 32);
4155 return hash_64(val
, SWEVENT_HLIST_BITS
);
4158 static inline struct hlist_head
*
4159 __find_swevent_head(struct swevent_hlist
*hlist
, u64 type
, u32 event_id
)
4161 u64 hash
= swevent_hash(type
, event_id
);
4163 return &hlist
->heads
[hash
];
4166 /* For the read side: events when they trigger */
4167 static inline struct hlist_head
*
4168 find_swevent_head_rcu(struct perf_cpu_context
*ctx
, u64 type
, u32 event_id
)
4170 struct swevent_hlist
*hlist
;
4172 hlist
= rcu_dereference(ctx
->swevent_hlist
);
4176 return __find_swevent_head(hlist
, type
, event_id
);
4179 /* For the event head insertion and removal in the hlist */
4180 static inline struct hlist_head
*
4181 find_swevent_head(struct perf_cpu_context
*ctx
, struct perf_event
*event
)
4183 struct swevent_hlist
*hlist
;
4184 u32 event_id
= event
->attr
.config
;
4185 u64 type
= event
->attr
.type
;
4188 * Event scheduling is always serialized against hlist allocation
4189 * and release. Which makes the protected version suitable here.
4190 * The context lock guarantees that.
4192 hlist
= rcu_dereference_protected(ctx
->swevent_hlist
,
4193 lockdep_is_held(&event
->ctx
->lock
));
4197 return __find_swevent_head(hlist
, type
, event_id
);
4200 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
4202 struct perf_sample_data
*data
,
4203 struct pt_regs
*regs
)
4205 struct perf_cpu_context
*cpuctx
;
4206 struct perf_event
*event
;
4207 struct hlist_node
*node
;
4208 struct hlist_head
*head
;
4210 cpuctx
= &__get_cpu_var(perf_cpu_context
);
4214 head
= find_swevent_head_rcu(cpuctx
, type
, event_id
);
4219 hlist_for_each_entry_rcu(event
, node
, head
, hlist_entry
) {
4220 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
4221 perf_swevent_add(event
, nr
, nmi
, data
, regs
);
4227 int perf_swevent_get_recursion_context(void)
4229 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
4236 else if (in_softirq())
4241 if (cpuctx
->recursion
[rctx
])
4244 cpuctx
->recursion
[rctx
]++;
4249 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
4251 void inline perf_swevent_put_recursion_context(int rctx
)
4253 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
4255 cpuctx
->recursion
[rctx
]--;
4258 void __perf_sw_event(u32 event_id
, u64 nr
, int nmi
,
4259 struct pt_regs
*regs
, u64 addr
)
4261 struct perf_sample_data data
;
4264 preempt_disable_notrace();
4265 rctx
= perf_swevent_get_recursion_context();
4269 perf_sample_data_init(&data
, addr
);
4271 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, nmi
, &data
, regs
);
4273 perf_swevent_put_recursion_context(rctx
);
4274 preempt_enable_notrace();
4277 static void perf_swevent_read(struct perf_event
*event
)
4281 static int perf_swevent_enable(struct perf_event
*event
)
4283 struct hw_perf_event
*hwc
= &event
->hw
;
4284 struct perf_cpu_context
*cpuctx
;
4285 struct hlist_head
*head
;
4287 cpuctx
= &__get_cpu_var(perf_cpu_context
);
4289 if (hwc
->sample_period
) {
4290 hwc
->last_period
= hwc
->sample_period
;
4291 perf_swevent_set_period(event
);
4294 head
= find_swevent_head(cpuctx
, event
);
4295 if (WARN_ON_ONCE(!head
))
4298 hlist_add_head_rcu(&event
->hlist_entry
, head
);
4303 static void perf_swevent_disable(struct perf_event
*event
)
4305 hlist_del_rcu(&event
->hlist_entry
);
4308 static void perf_swevent_void(struct perf_event
*event
)
4312 static int perf_swevent_int(struct perf_event
*event
)
4317 static const struct pmu perf_ops_generic
= {
4318 .enable
= perf_swevent_enable
,
4319 .disable
= perf_swevent_disable
,
4320 .start
= perf_swevent_int
,
4321 .stop
= perf_swevent_void
,
4322 .read
= perf_swevent_read
,
4323 .unthrottle
= perf_swevent_void
, /* hwc->interrupts already reset */
4327 * hrtimer based swevent callback
4330 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
4332 enum hrtimer_restart ret
= HRTIMER_RESTART
;
4333 struct perf_sample_data data
;
4334 struct pt_regs
*regs
;
4335 struct perf_event
*event
;
4338 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
4339 event
->pmu
->read(event
);
4341 perf_sample_data_init(&data
, 0);
4342 data
.period
= event
->hw
.last_period
;
4343 regs
= get_irq_regs();
4345 if (regs
&& !perf_exclude_event(event
, regs
)) {
4346 if (!(event
->attr
.exclude_idle
&& current
->pid
== 0))
4347 if (perf_event_overflow(event
, 0, &data
, regs
))
4348 ret
= HRTIMER_NORESTART
;
4351 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
4352 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
4357 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
4359 struct hw_perf_event
*hwc
= &event
->hw
;
4361 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
4362 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
4363 if (hwc
->sample_period
) {
4366 if (hwc
->remaining
) {
4367 if (hwc
->remaining
< 0)
4370 period
= hwc
->remaining
;
4373 period
= max_t(u64
, 10000, hwc
->sample_period
);
4375 __hrtimer_start_range_ns(&hwc
->hrtimer
,
4376 ns_to_ktime(period
), 0,
4377 HRTIMER_MODE_REL
, 0);
4381 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
4383 struct hw_perf_event
*hwc
= &event
->hw
;
4385 if (hwc
->sample_period
) {
4386 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
4387 hwc
->remaining
= ktime_to_ns(remaining
);
4389 hrtimer_cancel(&hwc
->hrtimer
);
4394 * Software event: cpu wall time clock
4397 static void cpu_clock_perf_event_update(struct perf_event
*event
)
4399 int cpu
= raw_smp_processor_id();
4403 now
= cpu_clock(cpu
);
4404 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
4405 local64_add(now
- prev
, &event
->count
);
4408 static int cpu_clock_perf_event_enable(struct perf_event
*event
)
4410 struct hw_perf_event
*hwc
= &event
->hw
;
4411 int cpu
= raw_smp_processor_id();
4413 local64_set(&hwc
->prev_count
, cpu_clock(cpu
));
4414 perf_swevent_start_hrtimer(event
);
4419 static void cpu_clock_perf_event_disable(struct perf_event
*event
)
4421 perf_swevent_cancel_hrtimer(event
);
4422 cpu_clock_perf_event_update(event
);
4425 static void cpu_clock_perf_event_read(struct perf_event
*event
)
4427 cpu_clock_perf_event_update(event
);
4430 static const struct pmu perf_ops_cpu_clock
= {
4431 .enable
= cpu_clock_perf_event_enable
,
4432 .disable
= cpu_clock_perf_event_disable
,
4433 .read
= cpu_clock_perf_event_read
,
4437 * Software event: task time clock
4440 static void task_clock_perf_event_update(struct perf_event
*event
, u64 now
)
4445 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
4447 local64_add(delta
, &event
->count
);
4450 static int task_clock_perf_event_enable(struct perf_event
*event
)
4452 struct hw_perf_event
*hwc
= &event
->hw
;
4455 now
= event
->ctx
->time
;
4457 local64_set(&hwc
->prev_count
, now
);
4459 perf_swevent_start_hrtimer(event
);
4464 static void task_clock_perf_event_disable(struct perf_event
*event
)
4466 perf_swevent_cancel_hrtimer(event
);
4467 task_clock_perf_event_update(event
, event
->ctx
->time
);
4471 static void task_clock_perf_event_read(struct perf_event
*event
)
4476 update_context_time(event
->ctx
);
4477 time
= event
->ctx
->time
;
4479 u64 now
= perf_clock();
4480 u64 delta
= now
- event
->ctx
->timestamp
;
4481 time
= event
->ctx
->time
+ delta
;
4484 task_clock_perf_event_update(event
, time
);
4487 static const struct pmu perf_ops_task_clock
= {
4488 .enable
= task_clock_perf_event_enable
,
4489 .disable
= task_clock_perf_event_disable
,
4490 .read
= task_clock_perf_event_read
,
4493 /* Deref the hlist from the update side */
4494 static inline struct swevent_hlist
*
4495 swevent_hlist_deref(struct perf_cpu_context
*cpuctx
)
4497 return rcu_dereference_protected(cpuctx
->swevent_hlist
,
4498 lockdep_is_held(&cpuctx
->hlist_mutex
));
4501 static void swevent_hlist_release_rcu(struct rcu_head
*rcu_head
)
4503 struct swevent_hlist
*hlist
;
4505 hlist
= container_of(rcu_head
, struct swevent_hlist
, rcu_head
);
4509 static void swevent_hlist_release(struct perf_cpu_context
*cpuctx
)
4511 struct swevent_hlist
*hlist
= swevent_hlist_deref(cpuctx
);
4516 rcu_assign_pointer(cpuctx
->swevent_hlist
, NULL
);
4517 call_rcu(&hlist
->rcu_head
, swevent_hlist_release_rcu
);
4520 static void swevent_hlist_put_cpu(struct perf_event
*event
, int cpu
)
4522 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4524 mutex_lock(&cpuctx
->hlist_mutex
);
4526 if (!--cpuctx
->hlist_refcount
)
4527 swevent_hlist_release(cpuctx
);
4529 mutex_unlock(&cpuctx
->hlist_mutex
);
4532 static void swevent_hlist_put(struct perf_event
*event
)
4536 if (event
->cpu
!= -1) {
4537 swevent_hlist_put_cpu(event
, event
->cpu
);
4541 for_each_possible_cpu(cpu
)
4542 swevent_hlist_put_cpu(event
, cpu
);
4545 static int swevent_hlist_get_cpu(struct perf_event
*event
, int cpu
)
4547 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4550 mutex_lock(&cpuctx
->hlist_mutex
);
4552 if (!swevent_hlist_deref(cpuctx
) && cpu_online(cpu
)) {
4553 struct swevent_hlist
*hlist
;
4555 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
4560 rcu_assign_pointer(cpuctx
->swevent_hlist
, hlist
);
4562 cpuctx
->hlist_refcount
++;
4564 mutex_unlock(&cpuctx
->hlist_mutex
);
4569 static int swevent_hlist_get(struct perf_event
*event
)
4572 int cpu
, failed_cpu
;
4574 if (event
->cpu
!= -1)
4575 return swevent_hlist_get_cpu(event
, event
->cpu
);
4578 for_each_possible_cpu(cpu
) {
4579 err
= swevent_hlist_get_cpu(event
, cpu
);
4589 for_each_possible_cpu(cpu
) {
4590 if (cpu
== failed_cpu
)
4592 swevent_hlist_put_cpu(event
, cpu
);
4599 #ifdef CONFIG_EVENT_TRACING
4601 static const struct pmu perf_ops_tracepoint
= {
4602 .enable
= perf_trace_enable
,
4603 .disable
= perf_trace_disable
,
4604 .start
= perf_swevent_int
,
4605 .stop
= perf_swevent_void
,
4606 .read
= perf_swevent_read
,
4607 .unthrottle
= perf_swevent_void
,
4610 static int perf_tp_filter_match(struct perf_event
*event
,
4611 struct perf_sample_data
*data
)
4613 void *record
= data
->raw
->data
;
4615 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
4620 static int perf_tp_event_match(struct perf_event
*event
,
4621 struct perf_sample_data
*data
,
4622 struct pt_regs
*regs
)
4625 * All tracepoints are from kernel-space.
4627 if (event
->attr
.exclude_kernel
)
4630 if (!perf_tp_filter_match(event
, data
))
4636 void perf_tp_event(u64 addr
, u64 count
, void *record
, int entry_size
,
4637 struct pt_regs
*regs
, struct hlist_head
*head
, int rctx
)
4639 struct perf_sample_data data
;
4640 struct perf_event
*event
;
4641 struct hlist_node
*node
;
4643 struct perf_raw_record raw
= {
4648 perf_sample_data_init(&data
, addr
);
4651 hlist_for_each_entry_rcu(event
, node
, head
, hlist_entry
) {
4652 if (perf_tp_event_match(event
, &data
, regs
))
4653 perf_swevent_add(event
, count
, 1, &data
, regs
);
4656 perf_swevent_put_recursion_context(rctx
);
4658 EXPORT_SYMBOL_GPL(perf_tp_event
);
4660 static void tp_perf_event_destroy(struct perf_event
*event
)
4662 perf_trace_destroy(event
);
4665 static const struct pmu
*tp_perf_event_init(struct perf_event
*event
)
4670 * Raw tracepoint data is a severe data leak, only allow root to
4673 if ((event
->attr
.sample_type
& PERF_SAMPLE_RAW
) &&
4674 perf_paranoid_tracepoint_raw() &&
4675 !capable(CAP_SYS_ADMIN
))
4676 return ERR_PTR(-EPERM
);
4678 err
= perf_trace_init(event
);
4682 event
->destroy
= tp_perf_event_destroy
;
4684 return &perf_ops_tracepoint
;
4687 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
4692 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
4695 filter_str
= strndup_user(arg
, PAGE_SIZE
);
4696 if (IS_ERR(filter_str
))
4697 return PTR_ERR(filter_str
);
4699 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
, filter_str
);
4705 static void perf_event_free_filter(struct perf_event
*event
)
4707 ftrace_profile_free_filter(event
);
4712 static const struct pmu
*tp_perf_event_init(struct perf_event
*event
)
4717 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
4722 static void perf_event_free_filter(struct perf_event
*event
)
4726 #endif /* CONFIG_EVENT_TRACING */
4728 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4729 static void bp_perf_event_destroy(struct perf_event
*event
)
4731 release_bp_slot(event
);
4734 static const struct pmu
*bp_perf_event_init(struct perf_event
*bp
)
4738 err
= register_perf_hw_breakpoint(bp
);
4740 return ERR_PTR(err
);
4742 bp
->destroy
= bp_perf_event_destroy
;
4744 return &perf_ops_bp
;
4747 void perf_bp_event(struct perf_event
*bp
, void *data
)
4749 struct perf_sample_data sample
;
4750 struct pt_regs
*regs
= data
;
4752 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
);
4754 if (!perf_exclude_event(bp
, regs
))
4755 perf_swevent_add(bp
, 1, 1, &sample
, regs
);
4758 static const struct pmu
*bp_perf_event_init(struct perf_event
*bp
)
4763 void perf_bp_event(struct perf_event
*bp
, void *regs
)
4768 atomic_t perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
4770 static void sw_perf_event_destroy(struct perf_event
*event
)
4772 u64 event_id
= event
->attr
.config
;
4774 WARN_ON(event
->parent
);
4776 atomic_dec(&perf_swevent_enabled
[event_id
]);
4777 swevent_hlist_put(event
);
4780 static const struct pmu
*sw_perf_event_init(struct perf_event
*event
)
4782 const struct pmu
*pmu
= NULL
;
4783 u64 event_id
= event
->attr
.config
;
4786 * Software events (currently) can't in general distinguish
4787 * between user, kernel and hypervisor events.
4788 * However, context switches and cpu migrations are considered
4789 * to be kernel events, and page faults are never hypervisor
4793 case PERF_COUNT_SW_CPU_CLOCK
:
4794 pmu
= &perf_ops_cpu_clock
;
4797 case PERF_COUNT_SW_TASK_CLOCK
:
4799 * If the user instantiates this as a per-cpu event,
4800 * use the cpu_clock event instead.
4802 if (event
->ctx
->task
)
4803 pmu
= &perf_ops_task_clock
;
4805 pmu
= &perf_ops_cpu_clock
;
4808 case PERF_COUNT_SW_PAGE_FAULTS
:
4809 case PERF_COUNT_SW_PAGE_FAULTS_MIN
:
4810 case PERF_COUNT_SW_PAGE_FAULTS_MAJ
:
4811 case PERF_COUNT_SW_CONTEXT_SWITCHES
:
4812 case PERF_COUNT_SW_CPU_MIGRATIONS
:
4813 case PERF_COUNT_SW_ALIGNMENT_FAULTS
:
4814 case PERF_COUNT_SW_EMULATION_FAULTS
:
4815 if (!event
->parent
) {
4818 err
= swevent_hlist_get(event
);
4820 return ERR_PTR(err
);
4822 atomic_inc(&perf_swevent_enabled
[event_id
]);
4823 event
->destroy
= sw_perf_event_destroy
;
4825 pmu
= &perf_ops_generic
;
4833 * Allocate and initialize a event structure
4835 static struct perf_event
*
4836 perf_event_alloc(struct perf_event_attr
*attr
,
4838 struct perf_event_context
*ctx
,
4839 struct perf_event
*group_leader
,
4840 struct perf_event
*parent_event
,
4841 perf_overflow_handler_t overflow_handler
,
4844 const struct pmu
*pmu
;
4845 struct perf_event
*event
;
4846 struct hw_perf_event
*hwc
;
4849 event
= kzalloc(sizeof(*event
), gfpflags
);
4851 return ERR_PTR(-ENOMEM
);
4854 * Single events are their own group leaders, with an
4855 * empty sibling list:
4858 group_leader
= event
;
4860 mutex_init(&event
->child_mutex
);
4861 INIT_LIST_HEAD(&event
->child_list
);
4863 INIT_LIST_HEAD(&event
->group_entry
);
4864 INIT_LIST_HEAD(&event
->event_entry
);
4865 INIT_LIST_HEAD(&event
->sibling_list
);
4866 init_waitqueue_head(&event
->waitq
);
4868 mutex_init(&event
->mmap_mutex
);
4871 event
->attr
= *attr
;
4872 event
->group_leader
= group_leader
;
4877 event
->parent
= parent_event
;
4879 event
->ns
= get_pid_ns(current
->nsproxy
->pid_ns
);
4880 event
->id
= atomic64_inc_return(&perf_event_id
);
4882 event
->state
= PERF_EVENT_STATE_INACTIVE
;
4884 if (!overflow_handler
&& parent_event
)
4885 overflow_handler
= parent_event
->overflow_handler
;
4887 event
->overflow_handler
= overflow_handler
;
4890 event
->state
= PERF_EVENT_STATE_OFF
;
4895 hwc
->sample_period
= attr
->sample_period
;
4896 if (attr
->freq
&& attr
->sample_freq
)
4897 hwc
->sample_period
= 1;
4898 hwc
->last_period
= hwc
->sample_period
;
4900 local64_set(&hwc
->period_left
, hwc
->sample_period
);
4903 * we currently do not support PERF_FORMAT_GROUP on inherited events
4905 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
4908 switch (attr
->type
) {
4910 case PERF_TYPE_HARDWARE
:
4911 case PERF_TYPE_HW_CACHE
:
4912 pmu
= hw_perf_event_init(event
);
4915 case PERF_TYPE_SOFTWARE
:
4916 pmu
= sw_perf_event_init(event
);
4919 case PERF_TYPE_TRACEPOINT
:
4920 pmu
= tp_perf_event_init(event
);
4923 case PERF_TYPE_BREAKPOINT
:
4924 pmu
= bp_perf_event_init(event
);
4935 else if (IS_ERR(pmu
))
4940 put_pid_ns(event
->ns
);
4942 return ERR_PTR(err
);
4947 if (!event
->parent
) {
4948 atomic_inc(&nr_events
);
4949 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
4950 atomic_inc(&nr_mmap_events
);
4951 if (event
->attr
.comm
)
4952 atomic_inc(&nr_comm_events
);
4953 if (event
->attr
.task
)
4954 atomic_inc(&nr_task_events
);
4960 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
4961 struct perf_event_attr
*attr
)
4966 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
4970 * zero the full structure, so that a short copy will be nice.
4972 memset(attr
, 0, sizeof(*attr
));
4974 ret
= get_user(size
, &uattr
->size
);
4978 if (size
> PAGE_SIZE
) /* silly large */
4981 if (!size
) /* abi compat */
4982 size
= PERF_ATTR_SIZE_VER0
;
4984 if (size
< PERF_ATTR_SIZE_VER0
)
4988 * If we're handed a bigger struct than we know of,
4989 * ensure all the unknown bits are 0 - i.e. new
4990 * user-space does not rely on any kernel feature
4991 * extensions we dont know about yet.
4993 if (size
> sizeof(*attr
)) {
4994 unsigned char __user
*addr
;
4995 unsigned char __user
*end
;
4998 addr
= (void __user
*)uattr
+ sizeof(*attr
);
4999 end
= (void __user
*)uattr
+ size
;
5001 for (; addr
< end
; addr
++) {
5002 ret
= get_user(val
, addr
);
5008 size
= sizeof(*attr
);
5011 ret
= copy_from_user(attr
, uattr
, size
);
5016 * If the type exists, the corresponding creation will verify
5019 if (attr
->type
>= PERF_TYPE_MAX
)
5022 if (attr
->__reserved_1
)
5025 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
5028 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
5035 put_user(sizeof(*attr
), &uattr
->size
);
5041 perf_event_set_output(struct perf_event
*event
, struct perf_event
*output_event
)
5043 struct perf_buffer
*buffer
= NULL
, *old_buffer
= NULL
;
5049 /* don't allow circular references */
5050 if (event
== output_event
)
5054 * Don't allow cross-cpu buffers
5056 if (output_event
->cpu
!= event
->cpu
)
5060 * If its not a per-cpu buffer, it must be the same task.
5062 if (output_event
->cpu
== -1 && output_event
->ctx
!= event
->ctx
)
5066 mutex_lock(&event
->mmap_mutex
);
5067 /* Can't redirect output if we've got an active mmap() */
5068 if (atomic_read(&event
->mmap_count
))
5072 /* get the buffer we want to redirect to */
5073 buffer
= perf_buffer_get(output_event
);
5078 old_buffer
= event
->buffer
;
5079 rcu_assign_pointer(event
->buffer
, buffer
);
5082 mutex_unlock(&event
->mmap_mutex
);
5085 perf_buffer_put(old_buffer
);
5091 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5093 * @attr_uptr: event_id type attributes for monitoring/sampling
5096 * @group_fd: group leader event fd
5098 SYSCALL_DEFINE5(perf_event_open
,
5099 struct perf_event_attr __user
*, attr_uptr
,
5100 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
5102 struct perf_event
*event
, *group_leader
= NULL
, *output_event
= NULL
;
5103 struct perf_event_attr attr
;
5104 struct perf_event_context
*ctx
;
5105 struct file
*event_file
= NULL
;
5106 struct file
*group_file
= NULL
;
5108 int fput_needed
= 0;
5111 /* for future expandability... */
5112 if (flags
& ~(PERF_FLAG_FD_NO_GROUP
| PERF_FLAG_FD_OUTPUT
))
5115 err
= perf_copy_attr(attr_uptr
, &attr
);
5119 if (!attr
.exclude_kernel
) {
5120 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
5125 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
5129 event_fd
= get_unused_fd_flags(O_RDWR
);
5134 * Get the target context (task or percpu):
5136 ctx
= find_get_context(pid
, cpu
);
5142 if (group_fd
!= -1) {
5143 group_leader
= perf_fget_light(group_fd
, &fput_needed
);
5144 if (IS_ERR(group_leader
)) {
5145 err
= PTR_ERR(group_leader
);
5146 goto err_put_context
;
5148 group_file
= group_leader
->filp
;
5149 if (flags
& PERF_FLAG_FD_OUTPUT
)
5150 output_event
= group_leader
;
5151 if (flags
& PERF_FLAG_FD_NO_GROUP
)
5152 group_leader
= NULL
;
5156 * Look up the group leader (we will attach this event to it):
5162 * Do not allow a recursive hierarchy (this new sibling
5163 * becoming part of another group-sibling):
5165 if (group_leader
->group_leader
!= group_leader
)
5166 goto err_put_context
;
5168 * Do not allow to attach to a group in a different
5169 * task or CPU context:
5171 if (group_leader
->ctx
!= ctx
)
5172 goto err_put_context
;
5174 * Only a group leader can be exclusive or pinned
5176 if (attr
.exclusive
|| attr
.pinned
)
5177 goto err_put_context
;
5180 event
= perf_event_alloc(&attr
, cpu
, ctx
, group_leader
,
5181 NULL
, NULL
, GFP_KERNEL
);
5182 if (IS_ERR(event
)) {
5183 err
= PTR_ERR(event
);
5184 goto err_put_context
;
5188 err
= perf_event_set_output(event
, output_event
);
5190 goto err_free_put_context
;
5193 event_file
= anon_inode_getfile("[perf_event]", &perf_fops
, event
, O_RDWR
);
5194 if (IS_ERR(event_file
)) {
5195 err
= PTR_ERR(event_file
);
5196 goto err_free_put_context
;
5199 event
->filp
= event_file
;
5200 WARN_ON_ONCE(ctx
->parent_ctx
);
5201 mutex_lock(&ctx
->mutex
);
5202 perf_install_in_context(ctx
, event
, cpu
);
5204 mutex_unlock(&ctx
->mutex
);
5206 event
->owner
= current
;
5207 get_task_struct(current
);
5208 mutex_lock(¤t
->perf_event_mutex
);
5209 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
5210 mutex_unlock(¤t
->perf_event_mutex
);
5213 * Drop the reference on the group_event after placing the
5214 * new event on the sibling_list. This ensures destruction
5215 * of the group leader will find the pointer to itself in
5216 * perf_group_detach().
5218 fput_light(group_file
, fput_needed
);
5219 fd_install(event_fd
, event_file
);
5222 err_free_put_context
:
5225 fput_light(group_file
, fput_needed
);
5228 put_unused_fd(event_fd
);
5233 * perf_event_create_kernel_counter
5235 * @attr: attributes of the counter to create
5236 * @cpu: cpu in which the counter is bound
5237 * @pid: task to profile
5240 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
5242 perf_overflow_handler_t overflow_handler
)
5244 struct perf_event
*event
;
5245 struct perf_event_context
*ctx
;
5249 * Get the target context (task or percpu):
5252 ctx
= find_get_context(pid
, cpu
);
5258 event
= perf_event_alloc(attr
, cpu
, ctx
, NULL
,
5259 NULL
, overflow_handler
, GFP_KERNEL
);
5260 if (IS_ERR(event
)) {
5261 err
= PTR_ERR(event
);
5262 goto err_put_context
;
5266 WARN_ON_ONCE(ctx
->parent_ctx
);
5267 mutex_lock(&ctx
->mutex
);
5268 perf_install_in_context(ctx
, event
, cpu
);
5270 mutex_unlock(&ctx
->mutex
);
5272 event
->owner
= current
;
5273 get_task_struct(current
);
5274 mutex_lock(¤t
->perf_event_mutex
);
5275 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
5276 mutex_unlock(¤t
->perf_event_mutex
);
5283 return ERR_PTR(err
);
5285 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
5288 * inherit a event from parent task to child task:
5290 static struct perf_event
*
5291 inherit_event(struct perf_event
*parent_event
,
5292 struct task_struct
*parent
,
5293 struct perf_event_context
*parent_ctx
,
5294 struct task_struct
*child
,
5295 struct perf_event
*group_leader
,
5296 struct perf_event_context
*child_ctx
)
5298 struct perf_event
*child_event
;
5301 * Instead of creating recursive hierarchies of events,
5302 * we link inherited events back to the original parent,
5303 * which has a filp for sure, which we use as the reference
5306 if (parent_event
->parent
)
5307 parent_event
= parent_event
->parent
;
5309 child_event
= perf_event_alloc(&parent_event
->attr
,
5310 parent_event
->cpu
, child_ctx
,
5311 group_leader
, parent_event
,
5313 if (IS_ERR(child_event
))
5318 * Make the child state follow the state of the parent event,
5319 * not its attr.disabled bit. We hold the parent's mutex,
5320 * so we won't race with perf_event_{en, dis}able_family.
5322 if (parent_event
->state
>= PERF_EVENT_STATE_INACTIVE
)
5323 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
5325 child_event
->state
= PERF_EVENT_STATE_OFF
;
5327 if (parent_event
->attr
.freq
) {
5328 u64 sample_period
= parent_event
->hw
.sample_period
;
5329 struct hw_perf_event
*hwc
= &child_event
->hw
;
5331 hwc
->sample_period
= sample_period
;
5332 hwc
->last_period
= sample_period
;
5334 local64_set(&hwc
->period_left
, sample_period
);
5337 child_event
->overflow_handler
= parent_event
->overflow_handler
;
5340 * Link it up in the child's context:
5342 add_event_to_ctx(child_event
, child_ctx
);
5345 * Get a reference to the parent filp - we will fput it
5346 * when the child event exits. This is safe to do because
5347 * we are in the parent and we know that the filp still
5348 * exists and has a nonzero count:
5350 atomic_long_inc(&parent_event
->filp
->f_count
);
5353 * Link this into the parent event's child list
5355 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
5356 mutex_lock(&parent_event
->child_mutex
);
5357 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
5358 mutex_unlock(&parent_event
->child_mutex
);
5363 static int inherit_group(struct perf_event
*parent_event
,
5364 struct task_struct
*parent
,
5365 struct perf_event_context
*parent_ctx
,
5366 struct task_struct
*child
,
5367 struct perf_event_context
*child_ctx
)
5369 struct perf_event
*leader
;
5370 struct perf_event
*sub
;
5371 struct perf_event
*child_ctr
;
5373 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
5374 child
, NULL
, child_ctx
);
5376 return PTR_ERR(leader
);
5377 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
5378 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
5379 child
, leader
, child_ctx
);
5380 if (IS_ERR(child_ctr
))
5381 return PTR_ERR(child_ctr
);
5386 static void sync_child_event(struct perf_event
*child_event
,
5387 struct task_struct
*child
)
5389 struct perf_event
*parent_event
= child_event
->parent
;
5392 if (child_event
->attr
.inherit_stat
)
5393 perf_event_read_event(child_event
, child
);
5395 child_val
= perf_event_count(child_event
);
5398 * Add back the child's count to the parent's count:
5400 atomic64_add(child_val
, &parent_event
->child_count
);
5401 atomic64_add(child_event
->total_time_enabled
,
5402 &parent_event
->child_total_time_enabled
);
5403 atomic64_add(child_event
->total_time_running
,
5404 &parent_event
->child_total_time_running
);
5407 * Remove this event from the parent's list
5409 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
5410 mutex_lock(&parent_event
->child_mutex
);
5411 list_del_init(&child_event
->child_list
);
5412 mutex_unlock(&parent_event
->child_mutex
);
5415 * Release the parent event, if this was the last
5418 fput(parent_event
->filp
);
5422 __perf_event_exit_task(struct perf_event
*child_event
,
5423 struct perf_event_context
*child_ctx
,
5424 struct task_struct
*child
)
5426 struct perf_event
*parent_event
;
5428 perf_event_remove_from_context(child_event
);
5430 parent_event
= child_event
->parent
;
5432 * It can happen that parent exits first, and has events
5433 * that are still around due to the child reference. These
5434 * events need to be zapped - but otherwise linger.
5437 sync_child_event(child_event
, child
);
5438 free_event(child_event
);
5443 * When a child task exits, feed back event values to parent events.
5445 void perf_event_exit_task(struct task_struct
*child
)
5447 struct perf_event
*child_event
, *tmp
;
5448 struct perf_event_context
*child_ctx
;
5449 unsigned long flags
;
5451 if (likely(!child
->perf_event_ctxp
)) {
5452 perf_event_task(child
, NULL
, 0);
5456 local_irq_save(flags
);
5458 * We can't reschedule here because interrupts are disabled,
5459 * and either child is current or it is a task that can't be
5460 * scheduled, so we are now safe from rescheduling changing
5463 child_ctx
= child
->perf_event_ctxp
;
5464 __perf_event_task_sched_out(child_ctx
);
5467 * Take the context lock here so that if find_get_context is
5468 * reading child->perf_event_ctxp, we wait until it has
5469 * incremented the context's refcount before we do put_ctx below.
5471 raw_spin_lock(&child_ctx
->lock
);
5472 child
->perf_event_ctxp
= NULL
;
5474 * If this context is a clone; unclone it so it can't get
5475 * swapped to another process while we're removing all
5476 * the events from it.
5478 unclone_ctx(child_ctx
);
5479 update_context_time(child_ctx
);
5480 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
5483 * Report the task dead after unscheduling the events so that we
5484 * won't get any samples after PERF_RECORD_EXIT. We can however still
5485 * get a few PERF_RECORD_READ events.
5487 perf_event_task(child
, child_ctx
, 0);
5490 * We can recurse on the same lock type through:
5492 * __perf_event_exit_task()
5493 * sync_child_event()
5494 * fput(parent_event->filp)
5496 * mutex_lock(&ctx->mutex)
5498 * But since its the parent context it won't be the same instance.
5500 mutex_lock(&child_ctx
->mutex
);
5503 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->pinned_groups
,
5505 __perf_event_exit_task(child_event
, child_ctx
, child
);
5507 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->flexible_groups
,
5509 __perf_event_exit_task(child_event
, child_ctx
, child
);
5512 * If the last event was a group event, it will have appended all
5513 * its siblings to the list, but we obtained 'tmp' before that which
5514 * will still point to the list head terminating the iteration.
5516 if (!list_empty(&child_ctx
->pinned_groups
) ||
5517 !list_empty(&child_ctx
->flexible_groups
))
5520 mutex_unlock(&child_ctx
->mutex
);
5525 static void perf_free_event(struct perf_event
*event
,
5526 struct perf_event_context
*ctx
)
5528 struct perf_event
*parent
= event
->parent
;
5530 if (WARN_ON_ONCE(!parent
))
5533 mutex_lock(&parent
->child_mutex
);
5534 list_del_init(&event
->child_list
);
5535 mutex_unlock(&parent
->child_mutex
);
5539 perf_group_detach(event
);
5540 list_del_event(event
, ctx
);
5545 * free an unexposed, unused context as created by inheritance by
5546 * init_task below, used by fork() in case of fail.
5548 void perf_event_free_task(struct task_struct
*task
)
5550 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
5551 struct perf_event
*event
, *tmp
;
5556 mutex_lock(&ctx
->mutex
);
5558 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
, group_entry
)
5559 perf_free_event(event
, ctx
);
5561 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
,
5563 perf_free_event(event
, ctx
);
5565 if (!list_empty(&ctx
->pinned_groups
) ||
5566 !list_empty(&ctx
->flexible_groups
))
5569 mutex_unlock(&ctx
->mutex
);
5575 inherit_task_group(struct perf_event
*event
, struct task_struct
*parent
,
5576 struct perf_event_context
*parent_ctx
,
5577 struct task_struct
*child
,
5581 struct perf_event_context
*child_ctx
= child
->perf_event_ctxp
;
5583 if (!event
->attr
.inherit
) {
5590 * This is executed from the parent task context, so
5591 * inherit events that have been marked for cloning.
5592 * First allocate and initialize a context for the
5596 child_ctx
= kzalloc(sizeof(struct perf_event_context
),
5601 __perf_event_init_context(child_ctx
, child
);
5602 child
->perf_event_ctxp
= child_ctx
;
5603 get_task_struct(child
);
5606 ret
= inherit_group(event
, parent
, parent_ctx
,
5617 * Initialize the perf_event context in task_struct
5619 int perf_event_init_task(struct task_struct
*child
)
5621 struct perf_event_context
*child_ctx
, *parent_ctx
;
5622 struct perf_event_context
*cloned_ctx
;
5623 struct perf_event
*event
;
5624 struct task_struct
*parent
= current
;
5625 int inherited_all
= 1;
5628 child
->perf_event_ctxp
= NULL
;
5630 mutex_init(&child
->perf_event_mutex
);
5631 INIT_LIST_HEAD(&child
->perf_event_list
);
5633 if (likely(!parent
->perf_event_ctxp
))
5637 * If the parent's context is a clone, pin it so it won't get
5640 parent_ctx
= perf_pin_task_context(parent
);
5643 * No need to check if parent_ctx != NULL here; since we saw
5644 * it non-NULL earlier, the only reason for it to become NULL
5645 * is if we exit, and since we're currently in the middle of
5646 * a fork we can't be exiting at the same time.
5650 * Lock the parent list. No need to lock the child - not PID
5651 * hashed yet and not running, so nobody can access it.
5653 mutex_lock(&parent_ctx
->mutex
);
5656 * We dont have to disable NMIs - we are only looking at
5657 * the list, not manipulating it:
5659 list_for_each_entry(event
, &parent_ctx
->pinned_groups
, group_entry
) {
5660 ret
= inherit_task_group(event
, parent
, parent_ctx
, child
,
5666 list_for_each_entry(event
, &parent_ctx
->flexible_groups
, group_entry
) {
5667 ret
= inherit_task_group(event
, parent
, parent_ctx
, child
,
5673 child_ctx
= child
->perf_event_ctxp
;
5675 if (child_ctx
&& inherited_all
) {
5677 * Mark the child context as a clone of the parent
5678 * context, or of whatever the parent is a clone of.
5679 * Note that if the parent is a clone, it could get
5680 * uncloned at any point, but that doesn't matter
5681 * because the list of events and the generation
5682 * count can't have changed since we took the mutex.
5684 cloned_ctx
= rcu_dereference(parent_ctx
->parent_ctx
);
5686 child_ctx
->parent_ctx
= cloned_ctx
;
5687 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
5689 child_ctx
->parent_ctx
= parent_ctx
;
5690 child_ctx
->parent_gen
= parent_ctx
->generation
;
5692 get_ctx(child_ctx
->parent_ctx
);
5695 mutex_unlock(&parent_ctx
->mutex
);
5697 perf_unpin_context(parent_ctx
);
5702 static void __init
perf_event_init_all_cpus(void)
5705 struct perf_cpu_context
*cpuctx
;
5707 for_each_possible_cpu(cpu
) {
5708 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5709 mutex_init(&cpuctx
->hlist_mutex
);
5710 __perf_event_init_context(&cpuctx
->ctx
, NULL
);
5714 static void __cpuinit
perf_event_init_cpu(int cpu
)
5716 struct perf_cpu_context
*cpuctx
;
5718 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5720 spin_lock(&perf_resource_lock
);
5721 cpuctx
->max_pertask
= perf_max_events
- perf_reserved_percpu
;
5722 spin_unlock(&perf_resource_lock
);
5724 mutex_lock(&cpuctx
->hlist_mutex
);
5725 if (cpuctx
->hlist_refcount
> 0) {
5726 struct swevent_hlist
*hlist
;
5728 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
5729 WARN_ON_ONCE(!hlist
);
5730 rcu_assign_pointer(cpuctx
->swevent_hlist
, hlist
);
5732 mutex_unlock(&cpuctx
->hlist_mutex
);
5735 #ifdef CONFIG_HOTPLUG_CPU
5736 static void __perf_event_exit_cpu(void *info
)
5738 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
5739 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
5740 struct perf_event
*event
, *tmp
;
5742 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
, group_entry
)
5743 __perf_event_remove_from_context(event
);
5744 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
, group_entry
)
5745 __perf_event_remove_from_context(event
);
5747 static void perf_event_exit_cpu(int cpu
)
5749 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5750 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
5752 mutex_lock(&cpuctx
->hlist_mutex
);
5753 swevent_hlist_release(cpuctx
);
5754 mutex_unlock(&cpuctx
->hlist_mutex
);
5756 mutex_lock(&ctx
->mutex
);
5757 smp_call_function_single(cpu
, __perf_event_exit_cpu
, NULL
, 1);
5758 mutex_unlock(&ctx
->mutex
);
5761 static inline void perf_event_exit_cpu(int cpu
) { }
5764 static int __cpuinit
5765 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
5767 unsigned int cpu
= (long)hcpu
;
5769 switch (action
& ~CPU_TASKS_FROZEN
) {
5771 case CPU_UP_PREPARE
:
5772 case CPU_DOWN_FAILED
:
5773 perf_event_init_cpu(cpu
);
5776 case CPU_UP_CANCELED
:
5777 case CPU_DOWN_PREPARE
:
5778 perf_event_exit_cpu(cpu
);
5789 * This has to have a higher priority than migration_notifier in sched.c.
5791 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
5792 .notifier_call
= perf_cpu_notify
,
5796 void __init
perf_event_init(void)
5798 perf_event_init_all_cpus();
5799 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
5800 (void *)(long)smp_processor_id());
5801 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_ONLINE
,
5802 (void *)(long)smp_processor_id());
5803 register_cpu_notifier(&perf_cpu_nb
);
5806 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class,
5807 struct sysdev_class_attribute
*attr
,
5810 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
5814 perf_set_reserve_percpu(struct sysdev_class
*class,
5815 struct sysdev_class_attribute
*attr
,
5819 struct perf_cpu_context
*cpuctx
;
5823 err
= strict_strtoul(buf
, 10, &val
);
5826 if (val
> perf_max_events
)
5829 spin_lock(&perf_resource_lock
);
5830 perf_reserved_percpu
= val
;
5831 for_each_online_cpu(cpu
) {
5832 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5833 raw_spin_lock_irq(&cpuctx
->ctx
.lock
);
5834 mpt
= min(perf_max_events
- cpuctx
->ctx
.nr_events
,
5835 perf_max_events
- perf_reserved_percpu
);
5836 cpuctx
->max_pertask
= mpt
;
5837 raw_spin_unlock_irq(&cpuctx
->ctx
.lock
);
5839 spin_unlock(&perf_resource_lock
);
5844 static ssize_t
perf_show_overcommit(struct sysdev_class
*class,
5845 struct sysdev_class_attribute
*attr
,
5848 return sprintf(buf
, "%d\n", perf_overcommit
);
5852 perf_set_overcommit(struct sysdev_class
*class,
5853 struct sysdev_class_attribute
*attr
,
5854 const char *buf
, size_t count
)
5859 err
= strict_strtoul(buf
, 10, &val
);
5865 spin_lock(&perf_resource_lock
);
5866 perf_overcommit
= val
;
5867 spin_unlock(&perf_resource_lock
);
5872 static SYSDEV_CLASS_ATTR(
5875 perf_show_reserve_percpu
,
5876 perf_set_reserve_percpu
5879 static SYSDEV_CLASS_ATTR(
5882 perf_show_overcommit
,
5886 static struct attribute
*perfclass_attrs
[] = {
5887 &attr_reserve_percpu
.attr
,
5888 &attr_overcommit
.attr
,
5892 static struct attribute_group perfclass_attr_group
= {
5893 .attrs
= perfclass_attrs
,
5894 .name
= "perf_events",
5897 static int __init
perf_event_sysfs_init(void)
5899 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
5900 &perfclass_attr_group
);
5902 device_initcall(perf_event_sysfs_init
);