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 static DEFINE_PER_CPU(int, perf_disable_count
);
90 void perf_disable(void)
92 if (!__get_cpu_var(perf_disable_count
)++)
96 void perf_enable(void)
98 if (!--__get_cpu_var(perf_disable_count
))
102 static void get_ctx(struct perf_event_context
*ctx
)
104 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
107 static void free_ctx(struct rcu_head
*head
)
109 struct perf_event_context
*ctx
;
111 ctx
= container_of(head
, struct perf_event_context
, rcu_head
);
115 static void put_ctx(struct perf_event_context
*ctx
)
117 if (atomic_dec_and_test(&ctx
->refcount
)) {
119 put_ctx(ctx
->parent_ctx
);
121 put_task_struct(ctx
->task
);
122 call_rcu(&ctx
->rcu_head
, free_ctx
);
126 static void unclone_ctx(struct perf_event_context
*ctx
)
128 if (ctx
->parent_ctx
) {
129 put_ctx(ctx
->parent_ctx
);
130 ctx
->parent_ctx
= NULL
;
135 * If we inherit events we want to return the parent event id
138 static u64
primary_event_id(struct perf_event
*event
)
143 id
= event
->parent
->id
;
149 * Get the perf_event_context for a task and lock it.
150 * This has to cope with with the fact that until it is locked,
151 * the context could get moved to another task.
153 static struct perf_event_context
*
154 perf_lock_task_context(struct task_struct
*task
, unsigned long *flags
)
156 struct perf_event_context
*ctx
;
160 ctx
= rcu_dereference(task
->perf_event_ctxp
);
163 * If this context is a clone of another, it might
164 * get swapped for another underneath us by
165 * perf_event_task_sched_out, though the
166 * rcu_read_lock() protects us from any context
167 * getting freed. Lock the context and check if it
168 * got swapped before we could get the lock, and retry
169 * if so. If we locked the right context, then it
170 * can't get swapped on us any more.
172 raw_spin_lock_irqsave(&ctx
->lock
, *flags
);
173 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
)) {
174 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
178 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
179 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
188 * Get the context for a task and increment its pin_count so it
189 * can't get swapped to another task. This also increments its
190 * reference count so that the context can't get freed.
192 static struct perf_event_context
*perf_pin_task_context(struct task_struct
*task
)
194 struct perf_event_context
*ctx
;
197 ctx
= perf_lock_task_context(task
, &flags
);
200 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
205 static void perf_unpin_context(struct perf_event_context
*ctx
)
209 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
211 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
215 static inline u64
perf_clock(void)
217 return cpu_clock(raw_smp_processor_id());
221 * Update the record of the current time in a context.
223 static void update_context_time(struct perf_event_context
*ctx
)
225 u64 now
= perf_clock();
227 ctx
->time
+= now
- ctx
->timestamp
;
228 ctx
->timestamp
= now
;
232 * Update the total_time_enabled and total_time_running fields for a event.
234 static void update_event_times(struct perf_event
*event
)
236 struct perf_event_context
*ctx
= event
->ctx
;
239 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
240 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
246 run_end
= event
->tstamp_stopped
;
248 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
250 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
251 run_end
= event
->tstamp_stopped
;
255 event
->total_time_running
= run_end
- event
->tstamp_running
;
259 * Update total_time_enabled and total_time_running for all events in a group.
261 static void update_group_times(struct perf_event
*leader
)
263 struct perf_event
*event
;
265 update_event_times(leader
);
266 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
267 update_event_times(event
);
270 static struct list_head
*
271 ctx_group_list(struct perf_event
*event
, struct perf_event_context
*ctx
)
273 if (event
->attr
.pinned
)
274 return &ctx
->pinned_groups
;
276 return &ctx
->flexible_groups
;
280 * Add a event from the lists for its context.
281 * Must be called with ctx->mutex and ctx->lock held.
284 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
286 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_CONTEXT
);
287 event
->attach_state
|= PERF_ATTACH_CONTEXT
;
290 * If we're a stand alone event or group leader, we go to the context
291 * list, group events are kept attached to the group so that
292 * perf_group_detach can, at all times, locate all siblings.
294 if (event
->group_leader
== event
) {
295 struct list_head
*list
;
297 if (is_software_event(event
))
298 event
->group_flags
|= PERF_GROUP_SOFTWARE
;
300 list
= ctx_group_list(event
, ctx
);
301 list_add_tail(&event
->group_entry
, list
);
304 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
306 if (event
->attr
.inherit_stat
)
310 static void perf_group_attach(struct perf_event
*event
)
312 struct perf_event
*group_leader
= event
->group_leader
;
314 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_GROUP
);
315 event
->attach_state
|= PERF_ATTACH_GROUP
;
317 if (group_leader
== event
)
320 if (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
&&
321 !is_software_event(event
))
322 group_leader
->group_flags
&= ~PERF_GROUP_SOFTWARE
;
324 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
325 group_leader
->nr_siblings
++;
329 * Remove a event from the lists for its context.
330 * Must be called with ctx->mutex and ctx->lock held.
333 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
336 * We can have double detach due to exit/hot-unplug + close.
338 if (!(event
->attach_state
& PERF_ATTACH_CONTEXT
))
341 event
->attach_state
&= ~PERF_ATTACH_CONTEXT
;
344 if (event
->attr
.inherit_stat
)
347 list_del_rcu(&event
->event_entry
);
349 if (event
->group_leader
== event
)
350 list_del_init(&event
->group_entry
);
352 update_group_times(event
);
355 * If event was in error state, then keep it
356 * that way, otherwise bogus counts will be
357 * returned on read(). The only way to get out
358 * of error state is by explicit re-enabling
361 if (event
->state
> PERF_EVENT_STATE_OFF
)
362 event
->state
= PERF_EVENT_STATE_OFF
;
365 static void perf_group_detach(struct perf_event
*event
)
367 struct perf_event
*sibling
, *tmp
;
368 struct list_head
*list
= NULL
;
371 * We can have double detach due to exit/hot-unplug + close.
373 if (!(event
->attach_state
& PERF_ATTACH_GROUP
))
376 event
->attach_state
&= ~PERF_ATTACH_GROUP
;
379 * If this is a sibling, remove it from its group.
381 if (event
->group_leader
!= event
) {
382 list_del_init(&event
->group_entry
);
383 event
->group_leader
->nr_siblings
--;
387 if (!list_empty(&event
->group_entry
))
388 list
= &event
->group_entry
;
391 * If this was a group event with sibling events then
392 * upgrade the siblings to singleton events by adding them
393 * to whatever list we are on.
395 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
397 list_move_tail(&sibling
->group_entry
, list
);
398 sibling
->group_leader
= sibling
;
400 /* Inherit group flags from the previous leader */
401 sibling
->group_flags
= event
->group_flags
;
406 event_sched_out(struct perf_event
*event
,
407 struct perf_cpu_context
*cpuctx
,
408 struct perf_event_context
*ctx
)
410 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
413 event
->state
= PERF_EVENT_STATE_INACTIVE
;
414 if (event
->pending_disable
) {
415 event
->pending_disable
= 0;
416 event
->state
= PERF_EVENT_STATE_OFF
;
418 event
->tstamp_stopped
= ctx
->time
;
419 event
->pmu
->disable(event
);
422 if (!is_software_event(event
))
423 cpuctx
->active_oncpu
--;
425 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
426 cpuctx
->exclusive
= 0;
430 group_sched_out(struct perf_event
*group_event
,
431 struct perf_cpu_context
*cpuctx
,
432 struct perf_event_context
*ctx
)
434 struct perf_event
*event
;
436 if (group_event
->state
!= PERF_EVENT_STATE_ACTIVE
)
439 event_sched_out(group_event
, cpuctx
, ctx
);
442 * Schedule out siblings (if any):
444 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
445 event_sched_out(event
, cpuctx
, ctx
);
447 if (group_event
->attr
.exclusive
)
448 cpuctx
->exclusive
= 0;
452 * Cross CPU call to remove a performance event
454 * We disable the event on the hardware level first. After that we
455 * remove it from the context list.
457 static void __perf_event_remove_from_context(void *info
)
459 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
460 struct perf_event
*event
= info
;
461 struct perf_event_context
*ctx
= event
->ctx
;
464 * If this is a task context, we need to check whether it is
465 * the current task context of this cpu. If not it has been
466 * scheduled out before the smp call arrived.
468 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
471 raw_spin_lock(&ctx
->lock
);
473 * Protect the list operation against NMI by disabling the
474 * events on a global level.
478 event_sched_out(event
, cpuctx
, ctx
);
480 list_del_event(event
, ctx
);
484 * Allow more per task events with respect to the
487 cpuctx
->max_pertask
=
488 min(perf_max_events
- ctx
->nr_events
,
489 perf_max_events
- perf_reserved_percpu
);
493 raw_spin_unlock(&ctx
->lock
);
498 * Remove the event from a task's (or a CPU's) list of events.
500 * Must be called with ctx->mutex held.
502 * CPU events are removed with a smp call. For task events we only
503 * call when the task is on a CPU.
505 * If event->ctx is a cloned context, callers must make sure that
506 * every task struct that event->ctx->task could possibly point to
507 * remains valid. This is OK when called from perf_release since
508 * that only calls us on the top-level context, which can't be a clone.
509 * When called from perf_event_exit_task, it's OK because the
510 * context has been detached from its task.
512 static void perf_event_remove_from_context(struct perf_event
*event
)
514 struct perf_event_context
*ctx
= event
->ctx
;
515 struct task_struct
*task
= ctx
->task
;
519 * Per cpu events are removed via an smp call and
520 * the removal is always successful.
522 smp_call_function_single(event
->cpu
,
523 __perf_event_remove_from_context
,
529 task_oncpu_function_call(task
, __perf_event_remove_from_context
,
532 raw_spin_lock_irq(&ctx
->lock
);
534 * If the context is active we need to retry the smp call.
536 if (ctx
->nr_active
&& !list_empty(&event
->group_entry
)) {
537 raw_spin_unlock_irq(&ctx
->lock
);
542 * The lock prevents that this context is scheduled in so we
543 * can remove the event safely, if the call above did not
546 if (!list_empty(&event
->group_entry
))
547 list_del_event(event
, ctx
);
548 raw_spin_unlock_irq(&ctx
->lock
);
552 * Cross CPU call to disable a performance event
554 static void __perf_event_disable(void *info
)
556 struct perf_event
*event
= info
;
557 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
558 struct perf_event_context
*ctx
= event
->ctx
;
561 * If this is a per-task event, need to check whether this
562 * event's task is the current task on this cpu.
564 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
567 raw_spin_lock(&ctx
->lock
);
570 * If the event is on, turn it off.
571 * If it is in error state, leave it in error state.
573 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
) {
574 update_context_time(ctx
);
575 update_group_times(event
);
576 if (event
== event
->group_leader
)
577 group_sched_out(event
, cpuctx
, ctx
);
579 event_sched_out(event
, cpuctx
, ctx
);
580 event
->state
= PERF_EVENT_STATE_OFF
;
583 raw_spin_unlock(&ctx
->lock
);
589 * If event->ctx is a cloned context, callers must make sure that
590 * every task struct that event->ctx->task could possibly point to
591 * remains valid. This condition is satisifed when called through
592 * perf_event_for_each_child or perf_event_for_each because they
593 * hold the top-level event's child_mutex, so any descendant that
594 * goes to exit will block in sync_child_event.
595 * When called from perf_pending_event it's OK because event->ctx
596 * is the current context on this CPU and preemption is disabled,
597 * hence we can't get into perf_event_task_sched_out for this context.
599 void perf_event_disable(struct perf_event
*event
)
601 struct perf_event_context
*ctx
= event
->ctx
;
602 struct task_struct
*task
= ctx
->task
;
606 * Disable the event on the cpu that it's on
608 smp_call_function_single(event
->cpu
, __perf_event_disable
,
614 task_oncpu_function_call(task
, __perf_event_disable
, event
);
616 raw_spin_lock_irq(&ctx
->lock
);
618 * If the event is still active, we need to retry the cross-call.
620 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
621 raw_spin_unlock_irq(&ctx
->lock
);
626 * Since we have the lock this context can't be scheduled
627 * in, so we can change the state safely.
629 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
630 update_group_times(event
);
631 event
->state
= PERF_EVENT_STATE_OFF
;
634 raw_spin_unlock_irq(&ctx
->lock
);
638 event_sched_in(struct perf_event
*event
,
639 struct perf_cpu_context
*cpuctx
,
640 struct perf_event_context
*ctx
)
642 if (event
->state
<= PERF_EVENT_STATE_OFF
)
645 event
->state
= PERF_EVENT_STATE_ACTIVE
;
646 event
->oncpu
= smp_processor_id();
648 * The new state must be visible before we turn it on in the hardware:
652 if (event
->pmu
->enable(event
)) {
653 event
->state
= PERF_EVENT_STATE_INACTIVE
;
658 event
->tstamp_running
+= ctx
->time
- event
->tstamp_stopped
;
660 if (!is_software_event(event
))
661 cpuctx
->active_oncpu
++;
664 if (event
->attr
.exclusive
)
665 cpuctx
->exclusive
= 1;
671 group_sched_in(struct perf_event
*group_event
,
672 struct perf_cpu_context
*cpuctx
,
673 struct perf_event_context
*ctx
)
675 struct perf_event
*event
, *partial_group
= NULL
;
676 const struct pmu
*pmu
= group_event
->pmu
;
680 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
683 /* Check if group transaction availabe */
690 if (event_sched_in(group_event
, cpuctx
, ctx
)) {
692 pmu
->cancel_txn(pmu
);
697 * Schedule in siblings as one group (if any):
699 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
700 if (event_sched_in(event
, cpuctx
, ctx
)) {
701 partial_group
= event
;
709 ret
= pmu
->commit_txn(pmu
);
711 pmu
->cancel_txn(pmu
);
717 * Groups can be scheduled in as one unit only, so undo any
718 * partial group before returning:
720 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
721 if (event
== partial_group
)
723 event_sched_out(event
, cpuctx
, ctx
);
725 event_sched_out(group_event
, cpuctx
, ctx
);
728 pmu
->cancel_txn(pmu
);
734 * Work out whether we can put this event group on the CPU now.
736 static int group_can_go_on(struct perf_event
*event
,
737 struct perf_cpu_context
*cpuctx
,
741 * Groups consisting entirely of software events can always go on.
743 if (event
->group_flags
& PERF_GROUP_SOFTWARE
)
746 * If an exclusive group is already on, no other hardware
749 if (cpuctx
->exclusive
)
752 * If this group is exclusive and there are already
753 * events on the CPU, it can't go on.
755 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
758 * Otherwise, try to add it if all previous groups were able
764 static void add_event_to_ctx(struct perf_event
*event
,
765 struct perf_event_context
*ctx
)
767 list_add_event(event
, ctx
);
768 perf_group_attach(event
);
769 event
->tstamp_enabled
= ctx
->time
;
770 event
->tstamp_running
= ctx
->time
;
771 event
->tstamp_stopped
= ctx
->time
;
775 * Cross CPU call to install and enable a performance event
777 * Must be called with ctx->mutex held
779 static void __perf_install_in_context(void *info
)
781 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
782 struct perf_event
*event
= info
;
783 struct perf_event_context
*ctx
= event
->ctx
;
784 struct perf_event
*leader
= event
->group_leader
;
788 * If this is a task context, we need to check whether it is
789 * the current task context of this cpu. If not it has been
790 * scheduled out before the smp call arrived.
791 * Or possibly this is the right context but it isn't
792 * on this cpu because it had no events.
794 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
795 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
797 cpuctx
->task_ctx
= ctx
;
800 raw_spin_lock(&ctx
->lock
);
802 update_context_time(ctx
);
805 * Protect the list operation against NMI by disabling the
806 * events on a global level. NOP for non NMI based events.
810 add_event_to_ctx(event
, ctx
);
812 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
816 * Don't put the event on if it is disabled or if
817 * it is in a group and the group isn't on.
819 if (event
->state
!= PERF_EVENT_STATE_INACTIVE
||
820 (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
))
824 * An exclusive event can't go on if there are already active
825 * hardware events, and no hardware event can go on if there
826 * is already an exclusive event on.
828 if (!group_can_go_on(event
, cpuctx
, 1))
831 err
= event_sched_in(event
, cpuctx
, ctx
);
835 * This event couldn't go on. If it is in a group
836 * then we have to pull the whole group off.
837 * If the event group is pinned then put it in error state.
840 group_sched_out(leader
, cpuctx
, ctx
);
841 if (leader
->attr
.pinned
) {
842 update_group_times(leader
);
843 leader
->state
= PERF_EVENT_STATE_ERROR
;
847 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
848 cpuctx
->max_pertask
--;
853 raw_spin_unlock(&ctx
->lock
);
857 * Attach a performance event to a context
859 * First we add the event to the list with the hardware enable bit
860 * in event->hw_config cleared.
862 * If the event is attached to a task which is on a CPU we use a smp
863 * call to enable it in the task context. The task might have been
864 * scheduled away, but we check this in the smp call again.
866 * Must be called with ctx->mutex held.
869 perf_install_in_context(struct perf_event_context
*ctx
,
870 struct perf_event
*event
,
873 struct task_struct
*task
= ctx
->task
;
877 * Per cpu events are installed via an smp call and
878 * the install is always successful.
880 smp_call_function_single(cpu
, __perf_install_in_context
,
886 task_oncpu_function_call(task
, __perf_install_in_context
,
889 raw_spin_lock_irq(&ctx
->lock
);
891 * we need to retry the smp call.
893 if (ctx
->is_active
&& list_empty(&event
->group_entry
)) {
894 raw_spin_unlock_irq(&ctx
->lock
);
899 * The lock prevents that this context is scheduled in so we
900 * can add the event safely, if it the call above did not
903 if (list_empty(&event
->group_entry
))
904 add_event_to_ctx(event
, ctx
);
905 raw_spin_unlock_irq(&ctx
->lock
);
909 * Put a event into inactive state and update time fields.
910 * Enabling the leader of a group effectively enables all
911 * the group members that aren't explicitly disabled, so we
912 * have to update their ->tstamp_enabled also.
913 * Note: this works for group members as well as group leaders
914 * since the non-leader members' sibling_lists will be empty.
916 static void __perf_event_mark_enabled(struct perf_event
*event
,
917 struct perf_event_context
*ctx
)
919 struct perf_event
*sub
;
921 event
->state
= PERF_EVENT_STATE_INACTIVE
;
922 event
->tstamp_enabled
= ctx
->time
- event
->total_time_enabled
;
923 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
)
924 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
)
925 sub
->tstamp_enabled
=
926 ctx
->time
- sub
->total_time_enabled
;
930 * Cross CPU call to enable a performance event
932 static void __perf_event_enable(void *info
)
934 struct perf_event
*event
= info
;
935 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
936 struct perf_event_context
*ctx
= event
->ctx
;
937 struct perf_event
*leader
= event
->group_leader
;
941 * If this is a per-task event, need to check whether this
942 * event's task is the current task on this cpu.
944 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
945 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
947 cpuctx
->task_ctx
= ctx
;
950 raw_spin_lock(&ctx
->lock
);
952 update_context_time(ctx
);
954 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
956 __perf_event_mark_enabled(event
, ctx
);
958 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
962 * If the event is in a group and isn't the group leader,
963 * then don't put it on unless the group is on.
965 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
)
968 if (!group_can_go_on(event
, cpuctx
, 1)) {
973 err
= group_sched_in(event
, cpuctx
, ctx
);
975 err
= event_sched_in(event
, cpuctx
, ctx
);
981 * If this event can't go on and it's part of a
982 * group, then the whole group has to come off.
985 group_sched_out(leader
, cpuctx
, ctx
);
986 if (leader
->attr
.pinned
) {
987 update_group_times(leader
);
988 leader
->state
= PERF_EVENT_STATE_ERROR
;
993 raw_spin_unlock(&ctx
->lock
);
999 * If event->ctx is a cloned context, callers must make sure that
1000 * every task struct that event->ctx->task could possibly point to
1001 * remains valid. This condition is satisfied when called through
1002 * perf_event_for_each_child or perf_event_for_each as described
1003 * for perf_event_disable.
1005 void perf_event_enable(struct perf_event
*event
)
1007 struct perf_event_context
*ctx
= event
->ctx
;
1008 struct task_struct
*task
= ctx
->task
;
1012 * Enable the event on the cpu that it's on
1014 smp_call_function_single(event
->cpu
, __perf_event_enable
,
1019 raw_spin_lock_irq(&ctx
->lock
);
1020 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1024 * If the event is in error state, clear that first.
1025 * That way, if we see the event in error state below, we
1026 * know that it has gone back into error state, as distinct
1027 * from the task having been scheduled away before the
1028 * cross-call arrived.
1030 if (event
->state
== PERF_EVENT_STATE_ERROR
)
1031 event
->state
= PERF_EVENT_STATE_OFF
;
1034 raw_spin_unlock_irq(&ctx
->lock
);
1035 task_oncpu_function_call(task
, __perf_event_enable
, event
);
1037 raw_spin_lock_irq(&ctx
->lock
);
1040 * If the context is active and the event is still off,
1041 * we need to retry the cross-call.
1043 if (ctx
->is_active
&& event
->state
== PERF_EVENT_STATE_OFF
)
1047 * Since we have the lock this context can't be scheduled
1048 * in, so we can change the state safely.
1050 if (event
->state
== PERF_EVENT_STATE_OFF
)
1051 __perf_event_mark_enabled(event
, ctx
);
1054 raw_spin_unlock_irq(&ctx
->lock
);
1057 static int perf_event_refresh(struct perf_event
*event
, int refresh
)
1060 * not supported on inherited events
1062 if (event
->attr
.inherit
)
1065 atomic_add(refresh
, &event
->event_limit
);
1066 perf_event_enable(event
);
1072 EVENT_FLEXIBLE
= 0x1,
1074 EVENT_ALL
= EVENT_FLEXIBLE
| EVENT_PINNED
,
1077 static void ctx_sched_out(struct perf_event_context
*ctx
,
1078 struct perf_cpu_context
*cpuctx
,
1079 enum event_type_t event_type
)
1081 struct perf_event
*event
;
1083 raw_spin_lock(&ctx
->lock
);
1085 if (likely(!ctx
->nr_events
))
1087 update_context_time(ctx
);
1090 if (!ctx
->nr_active
)
1093 if (event_type
& EVENT_PINNED
)
1094 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
)
1095 group_sched_out(event
, cpuctx
, ctx
);
1097 if (event_type
& EVENT_FLEXIBLE
)
1098 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
)
1099 group_sched_out(event
, cpuctx
, ctx
);
1104 raw_spin_unlock(&ctx
->lock
);
1108 * Test whether two contexts are equivalent, i.e. whether they
1109 * have both been cloned from the same version of the same context
1110 * and they both have the same number of enabled events.
1111 * If the number of enabled events is the same, then the set
1112 * of enabled events should be the same, because these are both
1113 * inherited contexts, therefore we can't access individual events
1114 * in them directly with an fd; we can only enable/disable all
1115 * events via prctl, or enable/disable all events in a family
1116 * via ioctl, which will have the same effect on both contexts.
1118 static int context_equiv(struct perf_event_context
*ctx1
,
1119 struct perf_event_context
*ctx2
)
1121 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
1122 && ctx1
->parent_gen
== ctx2
->parent_gen
1123 && !ctx1
->pin_count
&& !ctx2
->pin_count
;
1126 static void __perf_event_sync_stat(struct perf_event
*event
,
1127 struct perf_event
*next_event
)
1131 if (!event
->attr
.inherit_stat
)
1135 * Update the event value, we cannot use perf_event_read()
1136 * because we're in the middle of a context switch and have IRQs
1137 * disabled, which upsets smp_call_function_single(), however
1138 * we know the event must be on the current CPU, therefore we
1139 * don't need to use it.
1141 switch (event
->state
) {
1142 case PERF_EVENT_STATE_ACTIVE
:
1143 event
->pmu
->read(event
);
1146 case PERF_EVENT_STATE_INACTIVE
:
1147 update_event_times(event
);
1155 * In order to keep per-task stats reliable we need to flip the event
1156 * values when we flip the contexts.
1158 value
= atomic64_read(&next_event
->count
);
1159 value
= atomic64_xchg(&event
->count
, value
);
1160 atomic64_set(&next_event
->count
, value
);
1162 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
1163 swap(event
->total_time_running
, next_event
->total_time_running
);
1166 * Since we swizzled the values, update the user visible data too.
1168 perf_event_update_userpage(event
);
1169 perf_event_update_userpage(next_event
);
1172 #define list_next_entry(pos, member) \
1173 list_entry(pos->member.next, typeof(*pos), member)
1175 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
1176 struct perf_event_context
*next_ctx
)
1178 struct perf_event
*event
, *next_event
;
1183 update_context_time(ctx
);
1185 event
= list_first_entry(&ctx
->event_list
,
1186 struct perf_event
, event_entry
);
1188 next_event
= list_first_entry(&next_ctx
->event_list
,
1189 struct perf_event
, event_entry
);
1191 while (&event
->event_entry
!= &ctx
->event_list
&&
1192 &next_event
->event_entry
!= &next_ctx
->event_list
) {
1194 __perf_event_sync_stat(event
, next_event
);
1196 event
= list_next_entry(event
, event_entry
);
1197 next_event
= list_next_entry(next_event
, event_entry
);
1202 * Called from scheduler to remove the events of the current task,
1203 * with interrupts disabled.
1205 * We stop each event and update the event value in event->count.
1207 * This does not protect us against NMI, but disable()
1208 * sets the disabled bit in the control field of event _before_
1209 * accessing the event control register. If a NMI hits, then it will
1210 * not restart the event.
1212 void perf_event_task_sched_out(struct task_struct
*task
,
1213 struct task_struct
*next
)
1215 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1216 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
1217 struct perf_event_context
*next_ctx
;
1218 struct perf_event_context
*parent
;
1221 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES
, 1, 1, NULL
, 0);
1223 if (likely(!ctx
|| !cpuctx
->task_ctx
))
1227 parent
= rcu_dereference(ctx
->parent_ctx
);
1228 next_ctx
= next
->perf_event_ctxp
;
1229 if (parent
&& next_ctx
&&
1230 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
1232 * Looks like the two contexts are clones, so we might be
1233 * able to optimize the context switch. We lock both
1234 * contexts and check that they are clones under the
1235 * lock (including re-checking that neither has been
1236 * uncloned in the meantime). It doesn't matter which
1237 * order we take the locks because no other cpu could
1238 * be trying to lock both of these tasks.
1240 raw_spin_lock(&ctx
->lock
);
1241 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
1242 if (context_equiv(ctx
, next_ctx
)) {
1244 * XXX do we need a memory barrier of sorts
1245 * wrt to rcu_dereference() of perf_event_ctxp
1247 task
->perf_event_ctxp
= next_ctx
;
1248 next
->perf_event_ctxp
= ctx
;
1250 next_ctx
->task
= task
;
1253 perf_event_sync_stat(ctx
, next_ctx
);
1255 raw_spin_unlock(&next_ctx
->lock
);
1256 raw_spin_unlock(&ctx
->lock
);
1261 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
1262 cpuctx
->task_ctx
= NULL
;
1266 static void task_ctx_sched_out(struct perf_event_context
*ctx
,
1267 enum event_type_t event_type
)
1269 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1271 if (!cpuctx
->task_ctx
)
1274 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
1277 ctx_sched_out(ctx
, cpuctx
, event_type
);
1278 cpuctx
->task_ctx
= NULL
;
1282 * Called with IRQs disabled
1284 static void __perf_event_task_sched_out(struct perf_event_context
*ctx
)
1286 task_ctx_sched_out(ctx
, EVENT_ALL
);
1290 * Called with IRQs disabled
1292 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
1293 enum event_type_t event_type
)
1295 ctx_sched_out(&cpuctx
->ctx
, cpuctx
, event_type
);
1299 ctx_pinned_sched_in(struct perf_event_context
*ctx
,
1300 struct perf_cpu_context
*cpuctx
)
1302 struct perf_event
*event
;
1304 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
1305 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1307 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1310 if (group_can_go_on(event
, cpuctx
, 1))
1311 group_sched_in(event
, cpuctx
, ctx
);
1314 * If this pinned group hasn't been scheduled,
1315 * put it in error state.
1317 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1318 update_group_times(event
);
1319 event
->state
= PERF_EVENT_STATE_ERROR
;
1325 ctx_flexible_sched_in(struct perf_event_context
*ctx
,
1326 struct perf_cpu_context
*cpuctx
)
1328 struct perf_event
*event
;
1331 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
1332 /* Ignore events in OFF or ERROR state */
1333 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1336 * Listen to the 'cpu' scheduling filter constraint
1339 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1342 if (group_can_go_on(event
, cpuctx
, can_add_hw
))
1343 if (group_sched_in(event
, cpuctx
, ctx
))
1349 ctx_sched_in(struct perf_event_context
*ctx
,
1350 struct perf_cpu_context
*cpuctx
,
1351 enum event_type_t event_type
)
1353 raw_spin_lock(&ctx
->lock
);
1355 if (likely(!ctx
->nr_events
))
1358 ctx
->timestamp
= perf_clock();
1363 * First go through the list and put on any pinned groups
1364 * in order to give them the best chance of going on.
1366 if (event_type
& EVENT_PINNED
)
1367 ctx_pinned_sched_in(ctx
, cpuctx
);
1369 /* Then walk through the lower prio flexible groups */
1370 if (event_type
& EVENT_FLEXIBLE
)
1371 ctx_flexible_sched_in(ctx
, cpuctx
);
1375 raw_spin_unlock(&ctx
->lock
);
1378 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
1379 enum event_type_t event_type
)
1381 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
1383 ctx_sched_in(ctx
, cpuctx
, event_type
);
1386 static void task_ctx_sched_in(struct task_struct
*task
,
1387 enum event_type_t event_type
)
1389 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1390 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
1394 if (cpuctx
->task_ctx
== ctx
)
1396 ctx_sched_in(ctx
, cpuctx
, event_type
);
1397 cpuctx
->task_ctx
= ctx
;
1400 * Called from scheduler to add the events of the current task
1401 * with interrupts disabled.
1403 * We restore the event value and then enable it.
1405 * This does not protect us against NMI, but enable()
1406 * sets the enabled bit in the control field of event _before_
1407 * accessing the event control register. If a NMI hits, then it will
1408 * keep the event running.
1410 void perf_event_task_sched_in(struct task_struct
*task
)
1412 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1413 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
1418 if (cpuctx
->task_ctx
== ctx
)
1424 * We want to keep the following priority order:
1425 * cpu pinned (that don't need to move), task pinned,
1426 * cpu flexible, task flexible.
1428 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
1430 ctx_sched_in(ctx
, cpuctx
, EVENT_PINNED
);
1431 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
);
1432 ctx_sched_in(ctx
, cpuctx
, EVENT_FLEXIBLE
);
1434 cpuctx
->task_ctx
= ctx
;
1439 #define MAX_INTERRUPTS (~0ULL)
1441 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1443 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1445 u64 frequency
= event
->attr
.sample_freq
;
1446 u64 sec
= NSEC_PER_SEC
;
1447 u64 divisor
, dividend
;
1449 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
1451 count_fls
= fls64(count
);
1452 nsec_fls
= fls64(nsec
);
1453 frequency_fls
= fls64(frequency
);
1457 * We got @count in @nsec, with a target of sample_freq HZ
1458 * the target period becomes:
1461 * period = -------------------
1462 * @nsec * sample_freq
1467 * Reduce accuracy by one bit such that @a and @b converge
1468 * to a similar magnitude.
1470 #define REDUCE_FLS(a, b) \
1472 if (a##_fls > b##_fls) { \
1482 * Reduce accuracy until either term fits in a u64, then proceed with
1483 * the other, so that finally we can do a u64/u64 division.
1485 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
1486 REDUCE_FLS(nsec
, frequency
);
1487 REDUCE_FLS(sec
, count
);
1490 if (count_fls
+ sec_fls
> 64) {
1491 divisor
= nsec
* frequency
;
1493 while (count_fls
+ sec_fls
> 64) {
1494 REDUCE_FLS(count
, sec
);
1498 dividend
= count
* sec
;
1500 dividend
= count
* sec
;
1502 while (nsec_fls
+ frequency_fls
> 64) {
1503 REDUCE_FLS(nsec
, frequency
);
1507 divisor
= nsec
* frequency
;
1513 return div64_u64(dividend
, divisor
);
1516 static void perf_event_stop(struct perf_event
*event
)
1518 if (!event
->pmu
->stop
)
1519 return event
->pmu
->disable(event
);
1521 return event
->pmu
->stop(event
);
1524 static int perf_event_start(struct perf_event
*event
)
1526 if (!event
->pmu
->start
)
1527 return event
->pmu
->enable(event
);
1529 return event
->pmu
->start(event
);
1532 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1534 struct hw_perf_event
*hwc
= &event
->hw
;
1535 s64 period
, sample_period
;
1538 period
= perf_calculate_period(event
, nsec
, count
);
1540 delta
= (s64
)(period
- hwc
->sample_period
);
1541 delta
= (delta
+ 7) / 8; /* low pass filter */
1543 sample_period
= hwc
->sample_period
+ delta
;
1548 hwc
->sample_period
= sample_period
;
1550 if (atomic64_read(&hwc
->period_left
) > 8*sample_period
) {
1552 perf_event_stop(event
);
1553 atomic64_set(&hwc
->period_left
, 0);
1554 perf_event_start(event
);
1559 static void perf_ctx_adjust_freq(struct perf_event_context
*ctx
)
1561 struct perf_event
*event
;
1562 struct hw_perf_event
*hwc
;
1563 u64 interrupts
, now
;
1566 raw_spin_lock(&ctx
->lock
);
1567 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
1568 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1571 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1576 interrupts
= hwc
->interrupts
;
1577 hwc
->interrupts
= 0;
1580 * unthrottle events on the tick
1582 if (interrupts
== MAX_INTERRUPTS
) {
1583 perf_log_throttle(event
, 1);
1585 event
->pmu
->unthrottle(event
);
1589 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
1593 event
->pmu
->read(event
);
1594 now
= atomic64_read(&event
->count
);
1595 delta
= now
- hwc
->freq_count_stamp
;
1596 hwc
->freq_count_stamp
= now
;
1599 perf_adjust_period(event
, TICK_NSEC
, delta
);
1602 raw_spin_unlock(&ctx
->lock
);
1606 * Round-robin a context's events:
1608 static void rotate_ctx(struct perf_event_context
*ctx
)
1610 raw_spin_lock(&ctx
->lock
);
1613 * Rotate the first entry last of non-pinned groups. Rotation might be
1614 * disabled by the inheritance code.
1616 if (!ctx
->rotate_disable
)
1617 list_rotate_left(&ctx
->flexible_groups
);
1619 raw_spin_unlock(&ctx
->lock
);
1622 void perf_event_task_tick(struct task_struct
*curr
)
1624 struct perf_cpu_context
*cpuctx
;
1625 struct perf_event_context
*ctx
;
1628 if (!atomic_read(&nr_events
))
1631 cpuctx
= &__get_cpu_var(perf_cpu_context
);
1632 if (cpuctx
->ctx
.nr_events
&&
1633 cpuctx
->ctx
.nr_events
!= cpuctx
->ctx
.nr_active
)
1636 ctx
= curr
->perf_event_ctxp
;
1637 if (ctx
&& ctx
->nr_events
&& ctx
->nr_events
!= ctx
->nr_active
)
1640 perf_ctx_adjust_freq(&cpuctx
->ctx
);
1642 perf_ctx_adjust_freq(ctx
);
1648 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
1650 task_ctx_sched_out(ctx
, EVENT_FLEXIBLE
);
1652 rotate_ctx(&cpuctx
->ctx
);
1656 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
);
1658 task_ctx_sched_in(curr
, EVENT_FLEXIBLE
);
1662 static int event_enable_on_exec(struct perf_event
*event
,
1663 struct perf_event_context
*ctx
)
1665 if (!event
->attr
.enable_on_exec
)
1668 event
->attr
.enable_on_exec
= 0;
1669 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1672 __perf_event_mark_enabled(event
, ctx
);
1678 * Enable all of a task's events that have been marked enable-on-exec.
1679 * This expects task == current.
1681 static void perf_event_enable_on_exec(struct task_struct
*task
)
1683 struct perf_event_context
*ctx
;
1684 struct perf_event
*event
;
1685 unsigned long flags
;
1689 local_irq_save(flags
);
1690 ctx
= task
->perf_event_ctxp
;
1691 if (!ctx
|| !ctx
->nr_events
)
1694 __perf_event_task_sched_out(ctx
);
1696 raw_spin_lock(&ctx
->lock
);
1698 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
1699 ret
= event_enable_on_exec(event
, ctx
);
1704 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
1705 ret
= event_enable_on_exec(event
, ctx
);
1711 * Unclone this context if we enabled any event.
1716 raw_spin_unlock(&ctx
->lock
);
1718 perf_event_task_sched_in(task
);
1720 local_irq_restore(flags
);
1724 * Cross CPU call to read the hardware event
1726 static void __perf_event_read(void *info
)
1728 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1729 struct perf_event
*event
= info
;
1730 struct perf_event_context
*ctx
= event
->ctx
;
1733 * If this is a task context, we need to check whether it is
1734 * the current task context of this cpu. If not it has been
1735 * scheduled out before the smp call arrived. In that case
1736 * event->count would have been updated to a recent sample
1737 * when the event was scheduled out.
1739 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
1742 raw_spin_lock(&ctx
->lock
);
1743 update_context_time(ctx
);
1744 update_event_times(event
);
1745 raw_spin_unlock(&ctx
->lock
);
1747 event
->pmu
->read(event
);
1750 static u64
perf_event_read(struct perf_event
*event
)
1753 * If event is enabled and currently active on a CPU, update the
1754 * value in the event structure:
1756 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
1757 smp_call_function_single(event
->oncpu
,
1758 __perf_event_read
, event
, 1);
1759 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1760 struct perf_event_context
*ctx
= event
->ctx
;
1761 unsigned long flags
;
1763 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
1765 * may read while context is not active
1766 * (e.g., thread is blocked), in that case
1767 * we cannot update context time
1770 update_context_time(ctx
);
1771 update_event_times(event
);
1772 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1775 return atomic64_read(&event
->count
);
1779 * Initialize the perf_event context in a task_struct:
1782 __perf_event_init_context(struct perf_event_context
*ctx
,
1783 struct task_struct
*task
)
1785 raw_spin_lock_init(&ctx
->lock
);
1786 mutex_init(&ctx
->mutex
);
1787 INIT_LIST_HEAD(&ctx
->pinned_groups
);
1788 INIT_LIST_HEAD(&ctx
->flexible_groups
);
1789 INIT_LIST_HEAD(&ctx
->event_list
);
1790 atomic_set(&ctx
->refcount
, 1);
1794 static struct perf_event_context
*find_get_context(pid_t pid
, int cpu
)
1796 struct perf_event_context
*ctx
;
1797 struct perf_cpu_context
*cpuctx
;
1798 struct task_struct
*task
;
1799 unsigned long flags
;
1802 if (pid
== -1 && cpu
!= -1) {
1803 /* Must be root to operate on a CPU event: */
1804 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
1805 return ERR_PTR(-EACCES
);
1807 if (cpu
< 0 || cpu
>= nr_cpumask_bits
)
1808 return ERR_PTR(-EINVAL
);
1811 * We could be clever and allow to attach a event to an
1812 * offline CPU and activate it when the CPU comes up, but
1815 if (!cpu_online(cpu
))
1816 return ERR_PTR(-ENODEV
);
1818 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1829 task
= find_task_by_vpid(pid
);
1831 get_task_struct(task
);
1835 return ERR_PTR(-ESRCH
);
1838 * Can't attach events to a dying task.
1841 if (task
->flags
& PF_EXITING
)
1844 /* Reuse ptrace permission checks for now. */
1846 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1850 ctx
= perf_lock_task_context(task
, &flags
);
1853 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1857 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
1861 __perf_event_init_context(ctx
, task
);
1863 if (cmpxchg(&task
->perf_event_ctxp
, NULL
, ctx
)) {
1865 * We raced with some other task; use
1866 * the context they set.
1871 get_task_struct(task
);
1874 put_task_struct(task
);
1878 put_task_struct(task
);
1879 return ERR_PTR(err
);
1882 static void perf_event_free_filter(struct perf_event
*event
);
1884 static void free_event_rcu(struct rcu_head
*head
)
1886 struct perf_event
*event
;
1888 event
= container_of(head
, struct perf_event
, rcu_head
);
1890 put_pid_ns(event
->ns
);
1891 perf_event_free_filter(event
);
1895 static void perf_pending_sync(struct perf_event
*event
);
1896 static void perf_mmap_data_put(struct perf_mmap_data
*data
);
1898 static void free_event(struct perf_event
*event
)
1900 perf_pending_sync(event
);
1902 if (!event
->parent
) {
1903 atomic_dec(&nr_events
);
1904 if (event
->attr
.mmap
)
1905 atomic_dec(&nr_mmap_events
);
1906 if (event
->attr
.comm
)
1907 atomic_dec(&nr_comm_events
);
1908 if (event
->attr
.task
)
1909 atomic_dec(&nr_task_events
);
1913 perf_mmap_data_put(event
->data
);
1918 event
->destroy(event
);
1920 put_ctx(event
->ctx
);
1921 call_rcu(&event
->rcu_head
, free_event_rcu
);
1924 int perf_event_release_kernel(struct perf_event
*event
)
1926 struct perf_event_context
*ctx
= event
->ctx
;
1929 * Remove from the PMU, can't get re-enabled since we got
1930 * here because the last ref went.
1932 perf_event_disable(event
);
1934 WARN_ON_ONCE(ctx
->parent_ctx
);
1936 * There are two ways this annotation is useful:
1938 * 1) there is a lock recursion from perf_event_exit_task
1939 * see the comment there.
1941 * 2) there is a lock-inversion with mmap_sem through
1942 * perf_event_read_group(), which takes faults while
1943 * holding ctx->mutex, however this is called after
1944 * the last filedesc died, so there is no possibility
1945 * to trigger the AB-BA case.
1947 mutex_lock_nested(&ctx
->mutex
, SINGLE_DEPTH_NESTING
);
1948 raw_spin_lock_irq(&ctx
->lock
);
1949 perf_group_detach(event
);
1950 list_del_event(event
, ctx
);
1951 raw_spin_unlock_irq(&ctx
->lock
);
1952 mutex_unlock(&ctx
->mutex
);
1954 mutex_lock(&event
->owner
->perf_event_mutex
);
1955 list_del_init(&event
->owner_entry
);
1956 mutex_unlock(&event
->owner
->perf_event_mutex
);
1957 put_task_struct(event
->owner
);
1963 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
1966 * Called when the last reference to the file is gone.
1968 static int perf_release(struct inode
*inode
, struct file
*file
)
1970 struct perf_event
*event
= file
->private_data
;
1972 file
->private_data
= NULL
;
1974 return perf_event_release_kernel(event
);
1977 static int perf_event_read_size(struct perf_event
*event
)
1979 int entry
= sizeof(u64
); /* value */
1983 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1984 size
+= sizeof(u64
);
1986 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1987 size
+= sizeof(u64
);
1989 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
1990 entry
+= sizeof(u64
);
1992 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
1993 nr
+= event
->group_leader
->nr_siblings
;
1994 size
+= sizeof(u64
);
2002 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
2004 struct perf_event
*child
;
2010 mutex_lock(&event
->child_mutex
);
2011 total
+= perf_event_read(event
);
2012 *enabled
+= event
->total_time_enabled
+
2013 atomic64_read(&event
->child_total_time_enabled
);
2014 *running
+= event
->total_time_running
+
2015 atomic64_read(&event
->child_total_time_running
);
2017 list_for_each_entry(child
, &event
->child_list
, child_list
) {
2018 total
+= perf_event_read(child
);
2019 *enabled
+= child
->total_time_enabled
;
2020 *running
+= child
->total_time_running
;
2022 mutex_unlock(&event
->child_mutex
);
2026 EXPORT_SYMBOL_GPL(perf_event_read_value
);
2028 static int perf_event_read_group(struct perf_event
*event
,
2029 u64 read_format
, char __user
*buf
)
2031 struct perf_event
*leader
= event
->group_leader
, *sub
;
2032 int n
= 0, size
= 0, ret
= -EFAULT
;
2033 struct perf_event_context
*ctx
= leader
->ctx
;
2035 u64 count
, enabled
, running
;
2037 mutex_lock(&ctx
->mutex
);
2038 count
= perf_event_read_value(leader
, &enabled
, &running
);
2040 values
[n
++] = 1 + leader
->nr_siblings
;
2041 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2042 values
[n
++] = enabled
;
2043 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2044 values
[n
++] = running
;
2045 values
[n
++] = count
;
2046 if (read_format
& PERF_FORMAT_ID
)
2047 values
[n
++] = primary_event_id(leader
);
2049 size
= n
* sizeof(u64
);
2051 if (copy_to_user(buf
, values
, size
))
2056 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
2059 values
[n
++] = perf_event_read_value(sub
, &enabled
, &running
);
2060 if (read_format
& PERF_FORMAT_ID
)
2061 values
[n
++] = primary_event_id(sub
);
2063 size
= n
* sizeof(u64
);
2065 if (copy_to_user(buf
+ ret
, values
, size
)) {
2073 mutex_unlock(&ctx
->mutex
);
2078 static int perf_event_read_one(struct perf_event
*event
,
2079 u64 read_format
, char __user
*buf
)
2081 u64 enabled
, running
;
2085 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
2086 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2087 values
[n
++] = enabled
;
2088 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2089 values
[n
++] = running
;
2090 if (read_format
& PERF_FORMAT_ID
)
2091 values
[n
++] = primary_event_id(event
);
2093 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
2096 return n
* sizeof(u64
);
2100 * Read the performance event - simple non blocking version for now
2103 perf_read_hw(struct perf_event
*event
, char __user
*buf
, size_t count
)
2105 u64 read_format
= event
->attr
.read_format
;
2109 * Return end-of-file for a read on a event that is in
2110 * error state (i.e. because it was pinned but it couldn't be
2111 * scheduled on to the CPU at some point).
2113 if (event
->state
== PERF_EVENT_STATE_ERROR
)
2116 if (count
< perf_event_read_size(event
))
2119 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2120 if (read_format
& PERF_FORMAT_GROUP
)
2121 ret
= perf_event_read_group(event
, read_format
, buf
);
2123 ret
= perf_event_read_one(event
, read_format
, buf
);
2129 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
2131 struct perf_event
*event
= file
->private_data
;
2133 return perf_read_hw(event
, buf
, count
);
2136 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
2138 struct perf_event
*event
= file
->private_data
;
2139 struct perf_mmap_data
*data
;
2140 unsigned int events
= POLL_HUP
;
2143 data
= rcu_dereference(event
->data
);
2145 events
= atomic_xchg(&data
->poll
, 0);
2148 poll_wait(file
, &event
->waitq
, wait
);
2153 static void perf_event_reset(struct perf_event
*event
)
2155 (void)perf_event_read(event
);
2156 atomic64_set(&event
->count
, 0);
2157 perf_event_update_userpage(event
);
2161 * Holding the top-level event's child_mutex means that any
2162 * descendant process that has inherited this event will block
2163 * in sync_child_event if it goes to exit, thus satisfying the
2164 * task existence requirements of perf_event_enable/disable.
2166 static void perf_event_for_each_child(struct perf_event
*event
,
2167 void (*func
)(struct perf_event
*))
2169 struct perf_event
*child
;
2171 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2172 mutex_lock(&event
->child_mutex
);
2174 list_for_each_entry(child
, &event
->child_list
, child_list
)
2176 mutex_unlock(&event
->child_mutex
);
2179 static void perf_event_for_each(struct perf_event
*event
,
2180 void (*func
)(struct perf_event
*))
2182 struct perf_event_context
*ctx
= event
->ctx
;
2183 struct perf_event
*sibling
;
2185 WARN_ON_ONCE(ctx
->parent_ctx
);
2186 mutex_lock(&ctx
->mutex
);
2187 event
= event
->group_leader
;
2189 perf_event_for_each_child(event
, func
);
2191 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
2192 perf_event_for_each_child(event
, func
);
2193 mutex_unlock(&ctx
->mutex
);
2196 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
2198 struct perf_event_context
*ctx
= event
->ctx
;
2203 if (!event
->attr
.sample_period
)
2206 size
= copy_from_user(&value
, arg
, sizeof(value
));
2207 if (size
!= sizeof(value
))
2213 raw_spin_lock_irq(&ctx
->lock
);
2214 if (event
->attr
.freq
) {
2215 if (value
> sysctl_perf_event_sample_rate
) {
2220 event
->attr
.sample_freq
= value
;
2222 event
->attr
.sample_period
= value
;
2223 event
->hw
.sample_period
= value
;
2226 raw_spin_unlock_irq(&ctx
->lock
);
2231 static const struct file_operations perf_fops
;
2233 static struct perf_event
*perf_fget_light(int fd
, int *fput_needed
)
2237 file
= fget_light(fd
, fput_needed
);
2239 return ERR_PTR(-EBADF
);
2241 if (file
->f_op
!= &perf_fops
) {
2242 fput_light(file
, *fput_needed
);
2244 return ERR_PTR(-EBADF
);
2247 return file
->private_data
;
2250 static int perf_event_set_output(struct perf_event
*event
,
2251 struct perf_event
*output_event
);
2252 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
2254 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2256 struct perf_event
*event
= file
->private_data
;
2257 void (*func
)(struct perf_event
*);
2261 case PERF_EVENT_IOC_ENABLE
:
2262 func
= perf_event_enable
;
2264 case PERF_EVENT_IOC_DISABLE
:
2265 func
= perf_event_disable
;
2267 case PERF_EVENT_IOC_RESET
:
2268 func
= perf_event_reset
;
2271 case PERF_EVENT_IOC_REFRESH
:
2272 return perf_event_refresh(event
, arg
);
2274 case PERF_EVENT_IOC_PERIOD
:
2275 return perf_event_period(event
, (u64 __user
*)arg
);
2277 case PERF_EVENT_IOC_SET_OUTPUT
:
2279 struct perf_event
*output_event
= NULL
;
2280 int fput_needed
= 0;
2284 output_event
= perf_fget_light(arg
, &fput_needed
);
2285 if (IS_ERR(output_event
))
2286 return PTR_ERR(output_event
);
2289 ret
= perf_event_set_output(event
, output_event
);
2291 fput_light(output_event
->filp
, fput_needed
);
2296 case PERF_EVENT_IOC_SET_FILTER
:
2297 return perf_event_set_filter(event
, (void __user
*)arg
);
2303 if (flags
& PERF_IOC_FLAG_GROUP
)
2304 perf_event_for_each(event
, func
);
2306 perf_event_for_each_child(event
, func
);
2311 int perf_event_task_enable(void)
2313 struct perf_event
*event
;
2315 mutex_lock(¤t
->perf_event_mutex
);
2316 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2317 perf_event_for_each_child(event
, perf_event_enable
);
2318 mutex_unlock(¤t
->perf_event_mutex
);
2323 int perf_event_task_disable(void)
2325 struct perf_event
*event
;
2327 mutex_lock(¤t
->perf_event_mutex
);
2328 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2329 perf_event_for_each_child(event
, perf_event_disable
);
2330 mutex_unlock(¤t
->perf_event_mutex
);
2335 #ifndef PERF_EVENT_INDEX_OFFSET
2336 # define PERF_EVENT_INDEX_OFFSET 0
2339 static int perf_event_index(struct perf_event
*event
)
2341 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
2344 return event
->hw
.idx
+ 1 - PERF_EVENT_INDEX_OFFSET
;
2348 * Callers need to ensure there can be no nesting of this function, otherwise
2349 * the seqlock logic goes bad. We can not serialize this because the arch
2350 * code calls this from NMI context.
2352 void perf_event_update_userpage(struct perf_event
*event
)
2354 struct perf_event_mmap_page
*userpg
;
2355 struct perf_mmap_data
*data
;
2358 data
= rcu_dereference(event
->data
);
2362 userpg
= data
->user_page
;
2365 * Disable preemption so as to not let the corresponding user-space
2366 * spin too long if we get preempted.
2371 userpg
->index
= perf_event_index(event
);
2372 userpg
->offset
= atomic64_read(&event
->count
);
2373 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
2374 userpg
->offset
-= atomic64_read(&event
->hw
.prev_count
);
2376 userpg
->time_enabled
= event
->total_time_enabled
+
2377 atomic64_read(&event
->child_total_time_enabled
);
2379 userpg
->time_running
= event
->total_time_running
+
2380 atomic64_read(&event
->child_total_time_running
);
2389 #ifndef CONFIG_PERF_USE_VMALLOC
2392 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2395 static struct page
*
2396 perf_mmap_to_page(struct perf_mmap_data
*data
, unsigned long pgoff
)
2398 if (pgoff
> data
->nr_pages
)
2402 return virt_to_page(data
->user_page
);
2404 return virt_to_page(data
->data_pages
[pgoff
- 1]);
2407 static void *perf_mmap_alloc_page(int cpu
)
2412 node
= (cpu
== -1) ? cpu
: cpu_to_node(cpu
);
2413 page
= alloc_pages_node(node
, GFP_KERNEL
| __GFP_ZERO
, 0);
2417 return page_address(page
);
2420 static struct perf_mmap_data
*
2421 perf_mmap_data_alloc(struct perf_event
*event
, int nr_pages
)
2423 struct perf_mmap_data
*data
;
2427 size
= sizeof(struct perf_mmap_data
);
2428 size
+= nr_pages
* sizeof(void *);
2430 data
= kzalloc(size
, GFP_KERNEL
);
2434 data
->user_page
= perf_mmap_alloc_page(event
->cpu
);
2435 if (!data
->user_page
)
2436 goto fail_user_page
;
2438 for (i
= 0; i
< nr_pages
; i
++) {
2439 data
->data_pages
[i
] = perf_mmap_alloc_page(event
->cpu
);
2440 if (!data
->data_pages
[i
])
2441 goto fail_data_pages
;
2444 data
->nr_pages
= nr_pages
;
2449 for (i
--; i
>= 0; i
--)
2450 free_page((unsigned long)data
->data_pages
[i
]);
2452 free_page((unsigned long)data
->user_page
);
2461 static void perf_mmap_free_page(unsigned long addr
)
2463 struct page
*page
= virt_to_page((void *)addr
);
2465 page
->mapping
= NULL
;
2469 static void perf_mmap_data_free(struct perf_mmap_data
*data
)
2473 perf_mmap_free_page((unsigned long)data
->user_page
);
2474 for (i
= 0; i
< data
->nr_pages
; i
++)
2475 perf_mmap_free_page((unsigned long)data
->data_pages
[i
]);
2479 static inline int page_order(struct perf_mmap_data
*data
)
2487 * Back perf_mmap() with vmalloc memory.
2489 * Required for architectures that have d-cache aliasing issues.
2492 static inline int page_order(struct perf_mmap_data
*data
)
2494 return data
->page_order
;
2497 static struct page
*
2498 perf_mmap_to_page(struct perf_mmap_data
*data
, unsigned long pgoff
)
2500 if (pgoff
> (1UL << page_order(data
)))
2503 return vmalloc_to_page((void *)data
->user_page
+ pgoff
* PAGE_SIZE
);
2506 static void perf_mmap_unmark_page(void *addr
)
2508 struct page
*page
= vmalloc_to_page(addr
);
2510 page
->mapping
= NULL
;
2513 static void perf_mmap_data_free_work(struct work_struct
*work
)
2515 struct perf_mmap_data
*data
;
2519 data
= container_of(work
, struct perf_mmap_data
, work
);
2520 nr
= 1 << page_order(data
);
2522 base
= data
->user_page
;
2523 for (i
= 0; i
< nr
+ 1; i
++)
2524 perf_mmap_unmark_page(base
+ (i
* PAGE_SIZE
));
2530 static void perf_mmap_data_free(struct perf_mmap_data
*data
)
2532 schedule_work(&data
->work
);
2535 static struct perf_mmap_data
*
2536 perf_mmap_data_alloc(struct perf_event
*event
, int nr_pages
)
2538 struct perf_mmap_data
*data
;
2542 size
= sizeof(struct perf_mmap_data
);
2543 size
+= sizeof(void *);
2545 data
= kzalloc(size
, GFP_KERNEL
);
2549 INIT_WORK(&data
->work
, perf_mmap_data_free_work
);
2551 all_buf
= vmalloc_user((nr_pages
+ 1) * PAGE_SIZE
);
2555 data
->user_page
= all_buf
;
2556 data
->data_pages
[0] = all_buf
+ PAGE_SIZE
;
2557 data
->page_order
= ilog2(nr_pages
);
2571 static unsigned long perf_data_size(struct perf_mmap_data
*data
)
2573 return data
->nr_pages
<< (PAGE_SHIFT
+ page_order(data
));
2576 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2578 struct perf_event
*event
= vma
->vm_file
->private_data
;
2579 struct perf_mmap_data
*data
;
2580 int ret
= VM_FAULT_SIGBUS
;
2582 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
2583 if (vmf
->pgoff
== 0)
2589 data
= rcu_dereference(event
->data
);
2593 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
2596 vmf
->page
= perf_mmap_to_page(data
, vmf
->pgoff
);
2600 get_page(vmf
->page
);
2601 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
2602 vmf
->page
->index
= vmf
->pgoff
;
2612 perf_mmap_data_init(struct perf_event
*event
, struct perf_mmap_data
*data
)
2614 long max_size
= perf_data_size(data
);
2616 if (event
->attr
.watermark
) {
2617 data
->watermark
= min_t(long, max_size
,
2618 event
->attr
.wakeup_watermark
);
2621 if (!data
->watermark
)
2622 data
->watermark
= max_size
/ 2;
2624 atomic_set(&data
->refcount
, 1);
2625 rcu_assign_pointer(event
->data
, data
);
2628 static void perf_mmap_data_free_rcu(struct rcu_head
*rcu_head
)
2630 struct perf_mmap_data
*data
;
2632 data
= container_of(rcu_head
, struct perf_mmap_data
, rcu_head
);
2633 perf_mmap_data_free(data
);
2636 static struct perf_mmap_data
*perf_mmap_data_get(struct perf_event
*event
)
2638 struct perf_mmap_data
*data
;
2641 data
= rcu_dereference(event
->data
);
2643 if (!atomic_inc_not_zero(&data
->refcount
))
2651 static void perf_mmap_data_put(struct perf_mmap_data
*data
)
2653 if (!atomic_dec_and_test(&data
->refcount
))
2656 call_rcu(&data
->rcu_head
, perf_mmap_data_free_rcu
);
2659 static void perf_mmap_open(struct vm_area_struct
*vma
)
2661 struct perf_event
*event
= vma
->vm_file
->private_data
;
2663 atomic_inc(&event
->mmap_count
);
2666 static void perf_mmap_close(struct vm_area_struct
*vma
)
2668 struct perf_event
*event
= vma
->vm_file
->private_data
;
2670 if (atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
)) {
2671 unsigned long size
= perf_data_size(event
->data
);
2672 struct user_struct
*user
= event
->mmap_user
;
2673 struct perf_mmap_data
*data
= event
->data
;
2675 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &user
->locked_vm
);
2676 vma
->vm_mm
->locked_vm
-= event
->mmap_locked
;
2677 rcu_assign_pointer(event
->data
, NULL
);
2678 mutex_unlock(&event
->mmap_mutex
);
2680 perf_mmap_data_put(data
);
2685 static const struct vm_operations_struct perf_mmap_vmops
= {
2686 .open
= perf_mmap_open
,
2687 .close
= perf_mmap_close
,
2688 .fault
= perf_mmap_fault
,
2689 .page_mkwrite
= perf_mmap_fault
,
2692 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2694 struct perf_event
*event
= file
->private_data
;
2695 unsigned long user_locked
, user_lock_limit
;
2696 struct user_struct
*user
= current_user();
2697 unsigned long locked
, lock_limit
;
2698 struct perf_mmap_data
*data
;
2699 unsigned long vma_size
;
2700 unsigned long nr_pages
;
2701 long user_extra
, extra
;
2705 * Don't allow mmap() of inherited per-task counters. This would
2706 * create a performance issue due to all children writing to the
2709 if (event
->cpu
== -1 && event
->attr
.inherit
)
2712 if (!(vma
->vm_flags
& VM_SHARED
))
2715 vma_size
= vma
->vm_end
- vma
->vm_start
;
2716 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
2719 * If we have data pages ensure they're a power-of-two number, so we
2720 * can do bitmasks instead of modulo.
2722 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
2725 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
2728 if (vma
->vm_pgoff
!= 0)
2731 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2732 mutex_lock(&event
->mmap_mutex
);
2734 if (event
->data
->nr_pages
== nr_pages
)
2735 atomic_inc(&event
->data
->refcount
);
2741 user_extra
= nr_pages
+ 1;
2742 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
2745 * Increase the limit linearly with more CPUs:
2747 user_lock_limit
*= num_online_cpus();
2749 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
2752 if (user_locked
> user_lock_limit
)
2753 extra
= user_locked
- user_lock_limit
;
2755 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
2756 lock_limit
>>= PAGE_SHIFT
;
2757 locked
= vma
->vm_mm
->locked_vm
+ extra
;
2759 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
2760 !capable(CAP_IPC_LOCK
)) {
2765 WARN_ON(event
->data
);
2767 data
= perf_mmap_data_alloc(event
, nr_pages
);
2773 perf_mmap_data_init(event
, data
);
2774 if (vma
->vm_flags
& VM_WRITE
)
2775 event
->data
->writable
= 1;
2777 atomic_long_add(user_extra
, &user
->locked_vm
);
2778 event
->mmap_locked
= extra
;
2779 event
->mmap_user
= get_current_user();
2780 vma
->vm_mm
->locked_vm
+= event
->mmap_locked
;
2784 atomic_inc(&event
->mmap_count
);
2785 mutex_unlock(&event
->mmap_mutex
);
2787 vma
->vm_flags
|= VM_RESERVED
;
2788 vma
->vm_ops
= &perf_mmap_vmops
;
2793 static int perf_fasync(int fd
, struct file
*filp
, int on
)
2795 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
2796 struct perf_event
*event
= filp
->private_data
;
2799 mutex_lock(&inode
->i_mutex
);
2800 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
2801 mutex_unlock(&inode
->i_mutex
);
2809 static const struct file_operations perf_fops
= {
2810 .llseek
= no_llseek
,
2811 .release
= perf_release
,
2814 .unlocked_ioctl
= perf_ioctl
,
2815 .compat_ioctl
= perf_ioctl
,
2817 .fasync
= perf_fasync
,
2823 * If there's data, ensure we set the poll() state and publish everything
2824 * to user-space before waking everybody up.
2827 void perf_event_wakeup(struct perf_event
*event
)
2829 wake_up_all(&event
->waitq
);
2831 if (event
->pending_kill
) {
2832 kill_fasync(&event
->fasync
, SIGIO
, event
->pending_kill
);
2833 event
->pending_kill
= 0;
2840 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2842 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2843 * single linked list and use cmpxchg() to add entries lockless.
2846 static void perf_pending_event(struct perf_pending_entry
*entry
)
2848 struct perf_event
*event
= container_of(entry
,
2849 struct perf_event
, pending
);
2851 if (event
->pending_disable
) {
2852 event
->pending_disable
= 0;
2853 __perf_event_disable(event
);
2856 if (event
->pending_wakeup
) {
2857 event
->pending_wakeup
= 0;
2858 perf_event_wakeup(event
);
2862 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2864 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
2868 static void perf_pending_queue(struct perf_pending_entry
*entry
,
2869 void (*func
)(struct perf_pending_entry
*))
2871 struct perf_pending_entry
**head
;
2873 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
2878 head
= &get_cpu_var(perf_pending_head
);
2881 entry
->next
= *head
;
2882 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
2884 set_perf_event_pending();
2886 put_cpu_var(perf_pending_head
);
2889 static int __perf_pending_run(void)
2891 struct perf_pending_entry
*list
;
2894 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
2895 while (list
!= PENDING_TAIL
) {
2896 void (*func
)(struct perf_pending_entry
*);
2897 struct perf_pending_entry
*entry
= list
;
2904 * Ensure we observe the unqueue before we issue the wakeup,
2905 * so that we won't be waiting forever.
2906 * -- see perf_not_pending().
2917 static inline int perf_not_pending(struct perf_event
*event
)
2920 * If we flush on whatever cpu we run, there is a chance we don't
2924 __perf_pending_run();
2928 * Ensure we see the proper queue state before going to sleep
2929 * so that we do not miss the wakeup. -- see perf_pending_handle()
2932 return event
->pending
.next
== NULL
;
2935 static void perf_pending_sync(struct perf_event
*event
)
2937 wait_event(event
->waitq
, perf_not_pending(event
));
2940 void perf_event_do_pending(void)
2942 __perf_pending_run();
2946 * Callchain support -- arch specific
2949 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
2955 void perf_arch_fetch_caller_regs(struct pt_regs
*regs
, unsigned long ip
, int skip
)
2961 * We assume there is only KVM supporting the callbacks.
2962 * Later on, we might change it to a list if there is
2963 * another virtualization implementation supporting the callbacks.
2965 struct perf_guest_info_callbacks
*perf_guest_cbs
;
2967 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
2969 perf_guest_cbs
= cbs
;
2972 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks
);
2974 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
2976 perf_guest_cbs
= NULL
;
2979 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks
);
2984 static bool perf_output_space(struct perf_mmap_data
*data
, unsigned long tail
,
2985 unsigned long offset
, unsigned long head
)
2989 if (!data
->writable
)
2992 mask
= perf_data_size(data
) - 1;
2994 offset
= (offset
- tail
) & mask
;
2995 head
= (head
- tail
) & mask
;
2997 if ((int)(head
- offset
) < 0)
3003 static void perf_output_wakeup(struct perf_output_handle
*handle
)
3005 atomic_set(&handle
->data
->poll
, POLL_IN
);
3008 handle
->event
->pending_wakeup
= 1;
3009 perf_pending_queue(&handle
->event
->pending
,
3010 perf_pending_event
);
3012 perf_event_wakeup(handle
->event
);
3016 * We need to ensure a later event_id doesn't publish a head when a former
3017 * event isn't done writing. However since we need to deal with NMIs we
3018 * cannot fully serialize things.
3020 * We only publish the head (and generate a wakeup) when the outer-most
3023 static void perf_output_get_handle(struct perf_output_handle
*handle
)
3025 struct perf_mmap_data
*data
= handle
->data
;
3028 local_inc(&data
->nest
);
3029 handle
->wakeup
= local_read(&data
->wakeup
);
3032 static void perf_output_put_handle(struct perf_output_handle
*handle
)
3034 struct perf_mmap_data
*data
= handle
->data
;
3038 head
= local_read(&data
->head
);
3041 * IRQ/NMI can happen here, which means we can miss a head update.
3044 if (!local_dec_and_test(&data
->nest
))
3048 * Publish the known good head. Rely on the full barrier implied
3049 * by atomic_dec_and_test() order the data->head read and this
3052 data
->user_page
->data_head
= head
;
3055 * Now check if we missed an update, rely on the (compiler)
3056 * barrier in atomic_dec_and_test() to re-read data->head.
3058 if (unlikely(head
!= local_read(&data
->head
))) {
3059 local_inc(&data
->nest
);
3063 if (handle
->wakeup
!= local_read(&data
->wakeup
))
3064 perf_output_wakeup(handle
);
3070 __always_inline
void perf_output_copy(struct perf_output_handle
*handle
,
3071 const void *buf
, unsigned int len
)
3074 unsigned long size
= min_t(unsigned long, handle
->size
, len
);
3076 memcpy(handle
->addr
, buf
, size
);
3079 handle
->addr
+= size
;
3081 handle
->size
-= size
;
3082 if (!handle
->size
) {
3083 struct perf_mmap_data
*data
= handle
->data
;
3086 handle
->page
&= data
->nr_pages
- 1;
3087 handle
->addr
= data
->data_pages
[handle
->page
];
3088 handle
->size
= PAGE_SIZE
<< page_order(data
);
3093 int perf_output_begin(struct perf_output_handle
*handle
,
3094 struct perf_event
*event
, unsigned int size
,
3095 int nmi
, int sample
)
3097 struct perf_mmap_data
*data
;
3098 unsigned long tail
, offset
, head
;
3101 struct perf_event_header header
;
3108 * For inherited events we send all the output towards the parent.
3111 event
= event
->parent
;
3113 data
= rcu_dereference(event
->data
);
3117 handle
->data
= data
;
3118 handle
->event
= event
;
3120 handle
->sample
= sample
;
3122 if (!data
->nr_pages
)
3125 have_lost
= local_read(&data
->lost
);
3127 size
+= sizeof(lost_event
);
3129 perf_output_get_handle(handle
);
3133 * Userspace could choose to issue a mb() before updating the
3134 * tail pointer. So that all reads will be completed before the
3137 tail
= ACCESS_ONCE(data
->user_page
->data_tail
);
3139 offset
= head
= local_read(&data
->head
);
3141 if (unlikely(!perf_output_space(data
, tail
, offset
, head
)))
3143 } while (local_cmpxchg(&data
->head
, offset
, head
) != offset
);
3145 if (head
- local_read(&data
->wakeup
) > data
->watermark
)
3146 local_add(data
->watermark
, &data
->wakeup
);
3148 handle
->page
= offset
>> (PAGE_SHIFT
+ page_order(data
));
3149 handle
->page
&= data
->nr_pages
- 1;
3150 handle
->size
= offset
& ((PAGE_SIZE
<< page_order(data
)) - 1);
3151 handle
->addr
= data
->data_pages
[handle
->page
];
3152 handle
->addr
+= handle
->size
;
3153 handle
->size
= (PAGE_SIZE
<< page_order(data
)) - handle
->size
;
3156 lost_event
.header
.type
= PERF_RECORD_LOST
;
3157 lost_event
.header
.misc
= 0;
3158 lost_event
.header
.size
= sizeof(lost_event
);
3159 lost_event
.id
= event
->id
;
3160 lost_event
.lost
= local_xchg(&data
->lost
, 0);
3162 perf_output_put(handle
, lost_event
);
3168 local_inc(&data
->lost
);
3169 perf_output_put_handle(handle
);
3176 void perf_output_end(struct perf_output_handle
*handle
)
3178 struct perf_event
*event
= handle
->event
;
3179 struct perf_mmap_data
*data
= handle
->data
;
3181 int wakeup_events
= event
->attr
.wakeup_events
;
3183 if (handle
->sample
&& wakeup_events
) {
3184 int events
= local_inc_return(&data
->events
);
3185 if (events
>= wakeup_events
) {
3186 local_sub(wakeup_events
, &data
->events
);
3187 local_inc(&data
->wakeup
);
3191 perf_output_put_handle(handle
);
3195 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
3198 * only top level events have the pid namespace they were created in
3201 event
= event
->parent
;
3203 return task_tgid_nr_ns(p
, event
->ns
);
3206 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
3209 * only top level events have the pid namespace they were created in
3212 event
= event
->parent
;
3214 return task_pid_nr_ns(p
, event
->ns
);
3217 static void perf_output_read_one(struct perf_output_handle
*handle
,
3218 struct perf_event
*event
)
3220 u64 read_format
= event
->attr
.read_format
;
3224 values
[n
++] = atomic64_read(&event
->count
);
3225 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
3226 values
[n
++] = event
->total_time_enabled
+
3227 atomic64_read(&event
->child_total_time_enabled
);
3229 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
3230 values
[n
++] = event
->total_time_running
+
3231 atomic64_read(&event
->child_total_time_running
);
3233 if (read_format
& PERF_FORMAT_ID
)
3234 values
[n
++] = primary_event_id(event
);
3236 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3240 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3242 static void perf_output_read_group(struct perf_output_handle
*handle
,
3243 struct perf_event
*event
)
3245 struct perf_event
*leader
= event
->group_leader
, *sub
;
3246 u64 read_format
= event
->attr
.read_format
;
3250 values
[n
++] = 1 + leader
->nr_siblings
;
3252 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3253 values
[n
++] = leader
->total_time_enabled
;
3255 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3256 values
[n
++] = leader
->total_time_running
;
3258 if (leader
!= event
)
3259 leader
->pmu
->read(leader
);
3261 values
[n
++] = atomic64_read(&leader
->count
);
3262 if (read_format
& PERF_FORMAT_ID
)
3263 values
[n
++] = primary_event_id(leader
);
3265 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3267 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3271 sub
->pmu
->read(sub
);
3273 values
[n
++] = atomic64_read(&sub
->count
);
3274 if (read_format
& PERF_FORMAT_ID
)
3275 values
[n
++] = primary_event_id(sub
);
3277 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3281 static void perf_output_read(struct perf_output_handle
*handle
,
3282 struct perf_event
*event
)
3284 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
3285 perf_output_read_group(handle
, event
);
3287 perf_output_read_one(handle
, event
);
3290 void perf_output_sample(struct perf_output_handle
*handle
,
3291 struct perf_event_header
*header
,
3292 struct perf_sample_data
*data
,
3293 struct perf_event
*event
)
3295 u64 sample_type
= data
->type
;
3297 perf_output_put(handle
, *header
);
3299 if (sample_type
& PERF_SAMPLE_IP
)
3300 perf_output_put(handle
, data
->ip
);
3302 if (sample_type
& PERF_SAMPLE_TID
)
3303 perf_output_put(handle
, data
->tid_entry
);
3305 if (sample_type
& PERF_SAMPLE_TIME
)
3306 perf_output_put(handle
, data
->time
);
3308 if (sample_type
& PERF_SAMPLE_ADDR
)
3309 perf_output_put(handle
, data
->addr
);
3311 if (sample_type
& PERF_SAMPLE_ID
)
3312 perf_output_put(handle
, data
->id
);
3314 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
3315 perf_output_put(handle
, data
->stream_id
);
3317 if (sample_type
& PERF_SAMPLE_CPU
)
3318 perf_output_put(handle
, data
->cpu_entry
);
3320 if (sample_type
& PERF_SAMPLE_PERIOD
)
3321 perf_output_put(handle
, data
->period
);
3323 if (sample_type
& PERF_SAMPLE_READ
)
3324 perf_output_read(handle
, event
);
3326 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3327 if (data
->callchain
) {
3330 if (data
->callchain
)
3331 size
+= data
->callchain
->nr
;
3333 size
*= sizeof(u64
);
3335 perf_output_copy(handle
, data
->callchain
, size
);
3338 perf_output_put(handle
, nr
);
3342 if (sample_type
& PERF_SAMPLE_RAW
) {
3344 perf_output_put(handle
, data
->raw
->size
);
3345 perf_output_copy(handle
, data
->raw
->data
,
3352 .size
= sizeof(u32
),
3355 perf_output_put(handle
, raw
);
3360 void perf_prepare_sample(struct perf_event_header
*header
,
3361 struct perf_sample_data
*data
,
3362 struct perf_event
*event
,
3363 struct pt_regs
*regs
)
3365 u64 sample_type
= event
->attr
.sample_type
;
3367 data
->type
= sample_type
;
3369 header
->type
= PERF_RECORD_SAMPLE
;
3370 header
->size
= sizeof(*header
);
3373 header
->misc
|= perf_misc_flags(regs
);
3375 if (sample_type
& PERF_SAMPLE_IP
) {
3376 data
->ip
= perf_instruction_pointer(regs
);
3378 header
->size
+= sizeof(data
->ip
);
3381 if (sample_type
& PERF_SAMPLE_TID
) {
3382 /* namespace issues */
3383 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
3384 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
3386 header
->size
+= sizeof(data
->tid_entry
);
3389 if (sample_type
& PERF_SAMPLE_TIME
) {
3390 data
->time
= perf_clock();
3392 header
->size
+= sizeof(data
->time
);
3395 if (sample_type
& PERF_SAMPLE_ADDR
)
3396 header
->size
+= sizeof(data
->addr
);
3398 if (sample_type
& PERF_SAMPLE_ID
) {
3399 data
->id
= primary_event_id(event
);
3401 header
->size
+= sizeof(data
->id
);
3404 if (sample_type
& PERF_SAMPLE_STREAM_ID
) {
3405 data
->stream_id
= event
->id
;
3407 header
->size
+= sizeof(data
->stream_id
);
3410 if (sample_type
& PERF_SAMPLE_CPU
) {
3411 data
->cpu_entry
.cpu
= raw_smp_processor_id();
3412 data
->cpu_entry
.reserved
= 0;
3414 header
->size
+= sizeof(data
->cpu_entry
);
3417 if (sample_type
& PERF_SAMPLE_PERIOD
)
3418 header
->size
+= sizeof(data
->period
);
3420 if (sample_type
& PERF_SAMPLE_READ
)
3421 header
->size
+= perf_event_read_size(event
);
3423 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3426 data
->callchain
= perf_callchain(regs
);
3428 if (data
->callchain
)
3429 size
+= data
->callchain
->nr
;
3431 header
->size
+= size
* sizeof(u64
);
3434 if (sample_type
& PERF_SAMPLE_RAW
) {
3435 int size
= sizeof(u32
);
3438 size
+= data
->raw
->size
;
3440 size
+= sizeof(u32
);
3442 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
3443 header
->size
+= size
;
3447 static void perf_event_output(struct perf_event
*event
, int nmi
,
3448 struct perf_sample_data
*data
,
3449 struct pt_regs
*regs
)
3451 struct perf_output_handle handle
;
3452 struct perf_event_header header
;
3454 perf_prepare_sample(&header
, data
, event
, regs
);
3456 if (perf_output_begin(&handle
, event
, header
.size
, nmi
, 1))
3459 perf_output_sample(&handle
, &header
, data
, event
);
3461 perf_output_end(&handle
);
3468 struct perf_read_event
{
3469 struct perf_event_header header
;
3476 perf_event_read_event(struct perf_event
*event
,
3477 struct task_struct
*task
)
3479 struct perf_output_handle handle
;
3480 struct perf_read_event read_event
= {
3482 .type
= PERF_RECORD_READ
,
3484 .size
= sizeof(read_event
) + perf_event_read_size(event
),
3486 .pid
= perf_event_pid(event
, task
),
3487 .tid
= perf_event_tid(event
, task
),
3491 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
, 0, 0);
3495 perf_output_put(&handle
, read_event
);
3496 perf_output_read(&handle
, event
);
3498 perf_output_end(&handle
);
3502 * task tracking -- fork/exit
3504 * enabled by: attr.comm | attr.mmap | attr.task
3507 struct perf_task_event
{
3508 struct task_struct
*task
;
3509 struct perf_event_context
*task_ctx
;
3512 struct perf_event_header header
;
3522 static void perf_event_task_output(struct perf_event
*event
,
3523 struct perf_task_event
*task_event
)
3525 struct perf_output_handle handle
;
3526 struct task_struct
*task
= task_event
->task
;
3529 size
= task_event
->event_id
.header
.size
;
3530 ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3535 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
3536 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
3538 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
3539 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
3541 perf_output_put(&handle
, task_event
->event_id
);
3543 perf_output_end(&handle
);
3546 static int perf_event_task_match(struct perf_event
*event
)
3548 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3551 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3554 if (event
->attr
.comm
|| event
->attr
.mmap
|| event
->attr
.task
)
3560 static void perf_event_task_ctx(struct perf_event_context
*ctx
,
3561 struct perf_task_event
*task_event
)
3563 struct perf_event
*event
;
3565 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3566 if (perf_event_task_match(event
))
3567 perf_event_task_output(event
, task_event
);
3571 static void perf_event_task_event(struct perf_task_event
*task_event
)
3573 struct perf_cpu_context
*cpuctx
;
3574 struct perf_event_context
*ctx
= task_event
->task_ctx
;
3577 cpuctx
= &get_cpu_var(perf_cpu_context
);
3578 perf_event_task_ctx(&cpuctx
->ctx
, task_event
);
3580 ctx
= rcu_dereference(current
->perf_event_ctxp
);
3582 perf_event_task_ctx(ctx
, task_event
);
3583 put_cpu_var(perf_cpu_context
);
3587 static void perf_event_task(struct task_struct
*task
,
3588 struct perf_event_context
*task_ctx
,
3591 struct perf_task_event task_event
;
3593 if (!atomic_read(&nr_comm_events
) &&
3594 !atomic_read(&nr_mmap_events
) &&
3595 !atomic_read(&nr_task_events
))
3598 task_event
= (struct perf_task_event
){
3600 .task_ctx
= task_ctx
,
3603 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
3605 .size
= sizeof(task_event
.event_id
),
3611 .time
= perf_clock(),
3615 perf_event_task_event(&task_event
);
3618 void perf_event_fork(struct task_struct
*task
)
3620 perf_event_task(task
, NULL
, 1);
3627 struct perf_comm_event
{
3628 struct task_struct
*task
;
3633 struct perf_event_header header
;
3640 static void perf_event_comm_output(struct perf_event
*event
,
3641 struct perf_comm_event
*comm_event
)
3643 struct perf_output_handle handle
;
3644 int size
= comm_event
->event_id
.header
.size
;
3645 int ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3650 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
3651 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
3653 perf_output_put(&handle
, comm_event
->event_id
);
3654 perf_output_copy(&handle
, comm_event
->comm
,
3655 comm_event
->comm_size
);
3656 perf_output_end(&handle
);
3659 static int perf_event_comm_match(struct perf_event
*event
)
3661 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3664 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3667 if (event
->attr
.comm
)
3673 static void perf_event_comm_ctx(struct perf_event_context
*ctx
,
3674 struct perf_comm_event
*comm_event
)
3676 struct perf_event
*event
;
3678 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3679 if (perf_event_comm_match(event
))
3680 perf_event_comm_output(event
, comm_event
);
3684 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
3686 struct perf_cpu_context
*cpuctx
;
3687 struct perf_event_context
*ctx
;
3689 char comm
[TASK_COMM_LEN
];
3691 memset(comm
, 0, sizeof(comm
));
3692 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
3693 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
3695 comm_event
->comm
= comm
;
3696 comm_event
->comm_size
= size
;
3698 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
3701 cpuctx
= &get_cpu_var(perf_cpu_context
);
3702 perf_event_comm_ctx(&cpuctx
->ctx
, comm_event
);
3703 ctx
= rcu_dereference(current
->perf_event_ctxp
);
3705 perf_event_comm_ctx(ctx
, comm_event
);
3706 put_cpu_var(perf_cpu_context
);
3710 void perf_event_comm(struct task_struct
*task
)
3712 struct perf_comm_event comm_event
;
3714 if (task
->perf_event_ctxp
)
3715 perf_event_enable_on_exec(task
);
3717 if (!atomic_read(&nr_comm_events
))
3720 comm_event
= (struct perf_comm_event
){
3726 .type
= PERF_RECORD_COMM
,
3735 perf_event_comm_event(&comm_event
);
3742 struct perf_mmap_event
{
3743 struct vm_area_struct
*vma
;
3745 const char *file_name
;
3749 struct perf_event_header header
;
3759 static void perf_event_mmap_output(struct perf_event
*event
,
3760 struct perf_mmap_event
*mmap_event
)
3762 struct perf_output_handle handle
;
3763 int size
= mmap_event
->event_id
.header
.size
;
3764 int ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3769 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
3770 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
3772 perf_output_put(&handle
, mmap_event
->event_id
);
3773 perf_output_copy(&handle
, mmap_event
->file_name
,
3774 mmap_event
->file_size
);
3775 perf_output_end(&handle
);
3778 static int perf_event_mmap_match(struct perf_event
*event
,
3779 struct perf_mmap_event
*mmap_event
)
3781 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3784 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3787 if (event
->attr
.mmap
)
3793 static void perf_event_mmap_ctx(struct perf_event_context
*ctx
,
3794 struct perf_mmap_event
*mmap_event
)
3796 struct perf_event
*event
;
3798 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3799 if (perf_event_mmap_match(event
, mmap_event
))
3800 perf_event_mmap_output(event
, mmap_event
);
3804 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
3806 struct perf_cpu_context
*cpuctx
;
3807 struct perf_event_context
*ctx
;
3808 struct vm_area_struct
*vma
= mmap_event
->vma
;
3809 struct file
*file
= vma
->vm_file
;
3815 memset(tmp
, 0, sizeof(tmp
));
3819 * d_path works from the end of the buffer backwards, so we
3820 * need to add enough zero bytes after the string to handle
3821 * the 64bit alignment we do later.
3823 buf
= kzalloc(PATH_MAX
+ sizeof(u64
), GFP_KERNEL
);
3825 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
3828 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
3830 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
3834 if (arch_vma_name(mmap_event
->vma
)) {
3835 name
= strncpy(tmp
, arch_vma_name(mmap_event
->vma
),
3841 name
= strncpy(tmp
, "[vdso]", sizeof(tmp
));
3845 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
3850 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
3852 mmap_event
->file_name
= name
;
3853 mmap_event
->file_size
= size
;
3855 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
3858 cpuctx
= &get_cpu_var(perf_cpu_context
);
3859 perf_event_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
3860 ctx
= rcu_dereference(current
->perf_event_ctxp
);
3862 perf_event_mmap_ctx(ctx
, mmap_event
);
3863 put_cpu_var(perf_cpu_context
);
3869 void __perf_event_mmap(struct vm_area_struct
*vma
)
3871 struct perf_mmap_event mmap_event
;
3873 if (!atomic_read(&nr_mmap_events
))
3876 mmap_event
= (struct perf_mmap_event
){
3882 .type
= PERF_RECORD_MMAP
,
3883 .misc
= PERF_RECORD_MISC_USER
,
3888 .start
= vma
->vm_start
,
3889 .len
= vma
->vm_end
- vma
->vm_start
,
3890 .pgoff
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
,
3894 perf_event_mmap_event(&mmap_event
);
3898 * IRQ throttle logging
3901 static void perf_log_throttle(struct perf_event
*event
, int enable
)
3903 struct perf_output_handle handle
;
3907 struct perf_event_header header
;
3911 } throttle_event
= {
3913 .type
= PERF_RECORD_THROTTLE
,
3915 .size
= sizeof(throttle_event
),
3917 .time
= perf_clock(),
3918 .id
= primary_event_id(event
),
3919 .stream_id
= event
->id
,
3923 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
3925 ret
= perf_output_begin(&handle
, event
, sizeof(throttle_event
), 1, 0);
3929 perf_output_put(&handle
, throttle_event
);
3930 perf_output_end(&handle
);
3934 * Generic event overflow handling, sampling.
3937 static int __perf_event_overflow(struct perf_event
*event
, int nmi
,
3938 int throttle
, struct perf_sample_data
*data
,
3939 struct pt_regs
*regs
)
3941 int events
= atomic_read(&event
->event_limit
);
3942 struct hw_perf_event
*hwc
= &event
->hw
;
3945 throttle
= (throttle
&& event
->pmu
->unthrottle
!= NULL
);
3950 if (hwc
->interrupts
!= MAX_INTERRUPTS
) {
3952 if (HZ
* hwc
->interrupts
>
3953 (u64
)sysctl_perf_event_sample_rate
) {
3954 hwc
->interrupts
= MAX_INTERRUPTS
;
3955 perf_log_throttle(event
, 0);
3960 * Keep re-disabling events even though on the previous
3961 * pass we disabled it - just in case we raced with a
3962 * sched-in and the event got enabled again:
3968 if (event
->attr
.freq
) {
3969 u64 now
= perf_clock();
3970 s64 delta
= now
- hwc
->freq_time_stamp
;
3972 hwc
->freq_time_stamp
= now
;
3974 if (delta
> 0 && delta
< 2*TICK_NSEC
)
3975 perf_adjust_period(event
, delta
, hwc
->last_period
);
3979 * XXX event_limit might not quite work as expected on inherited
3983 event
->pending_kill
= POLL_IN
;
3984 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
3986 event
->pending_kill
= POLL_HUP
;
3988 event
->pending_disable
= 1;
3989 perf_pending_queue(&event
->pending
,
3990 perf_pending_event
);
3992 perf_event_disable(event
);
3995 if (event
->overflow_handler
)
3996 event
->overflow_handler(event
, nmi
, data
, regs
);
3998 perf_event_output(event
, nmi
, data
, regs
);
4003 int perf_event_overflow(struct perf_event
*event
, int nmi
,
4004 struct perf_sample_data
*data
,
4005 struct pt_regs
*regs
)
4007 return __perf_event_overflow(event
, nmi
, 1, data
, regs
);
4011 * Generic software event infrastructure
4015 * We directly increment event->count and keep a second value in
4016 * event->hw.period_left to count intervals. This period event
4017 * is kept in the range [-sample_period, 0] so that we can use the
4021 static u64
perf_swevent_set_period(struct perf_event
*event
)
4023 struct hw_perf_event
*hwc
= &event
->hw
;
4024 u64 period
= hwc
->last_period
;
4028 hwc
->last_period
= hwc
->sample_period
;
4031 old
= val
= atomic64_read(&hwc
->period_left
);
4035 nr
= div64_u64(period
+ val
, period
);
4036 offset
= nr
* period
;
4038 if (atomic64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
4044 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
4045 int nmi
, struct perf_sample_data
*data
,
4046 struct pt_regs
*regs
)
4048 struct hw_perf_event
*hwc
= &event
->hw
;
4051 data
->period
= event
->hw
.last_period
;
4053 overflow
= perf_swevent_set_period(event
);
4055 if (hwc
->interrupts
== MAX_INTERRUPTS
)
4058 for (; overflow
; overflow
--) {
4059 if (__perf_event_overflow(event
, nmi
, throttle
,
4062 * We inhibit the overflow from happening when
4063 * hwc->interrupts == MAX_INTERRUPTS.
4071 static void perf_swevent_add(struct perf_event
*event
, u64 nr
,
4072 int nmi
, struct perf_sample_data
*data
,
4073 struct pt_regs
*regs
)
4075 struct hw_perf_event
*hwc
= &event
->hw
;
4077 atomic64_add(nr
, &event
->count
);
4082 if (!hwc
->sample_period
)
4085 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
4086 return perf_swevent_overflow(event
, 1, nmi
, data
, regs
);
4088 if (atomic64_add_negative(nr
, &hwc
->period_left
))
4091 perf_swevent_overflow(event
, 0, nmi
, data
, regs
);
4094 static int perf_exclude_event(struct perf_event
*event
,
4095 struct pt_regs
*regs
)
4098 if (event
->attr
.exclude_user
&& user_mode(regs
))
4101 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
4108 static int perf_swevent_match(struct perf_event
*event
,
4109 enum perf_type_id type
,
4111 struct perf_sample_data
*data
,
4112 struct pt_regs
*regs
)
4114 if (event
->attr
.type
!= type
)
4117 if (event
->attr
.config
!= event_id
)
4120 if (perf_exclude_event(event
, regs
))
4126 static inline u64
swevent_hash(u64 type
, u32 event_id
)
4128 u64 val
= event_id
| (type
<< 32);
4130 return hash_64(val
, SWEVENT_HLIST_BITS
);
4133 static inline struct hlist_head
*
4134 __find_swevent_head(struct swevent_hlist
*hlist
, u64 type
, u32 event_id
)
4136 u64 hash
= swevent_hash(type
, event_id
);
4138 return &hlist
->heads
[hash
];
4141 /* For the read side: events when they trigger */
4142 static inline struct hlist_head
*
4143 find_swevent_head_rcu(struct perf_cpu_context
*ctx
, u64 type
, u32 event_id
)
4145 struct swevent_hlist
*hlist
;
4147 hlist
= rcu_dereference(ctx
->swevent_hlist
);
4151 return __find_swevent_head(hlist
, type
, event_id
);
4154 /* For the event head insertion and removal in the hlist */
4155 static inline struct hlist_head
*
4156 find_swevent_head(struct perf_cpu_context
*ctx
, struct perf_event
*event
)
4158 struct swevent_hlist
*hlist
;
4159 u32 event_id
= event
->attr
.config
;
4160 u64 type
= event
->attr
.type
;
4163 * Event scheduling is always serialized against hlist allocation
4164 * and release. Which makes the protected version suitable here.
4165 * The context lock guarantees that.
4167 hlist
= rcu_dereference_protected(ctx
->swevent_hlist
,
4168 lockdep_is_held(&event
->ctx
->lock
));
4172 return __find_swevent_head(hlist
, type
, event_id
);
4175 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
4177 struct perf_sample_data
*data
,
4178 struct pt_regs
*regs
)
4180 struct perf_cpu_context
*cpuctx
;
4181 struct perf_event
*event
;
4182 struct hlist_node
*node
;
4183 struct hlist_head
*head
;
4185 cpuctx
= &__get_cpu_var(perf_cpu_context
);
4189 head
= find_swevent_head_rcu(cpuctx
, type
, event_id
);
4194 hlist_for_each_entry_rcu(event
, node
, head
, hlist_entry
) {
4195 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
4196 perf_swevent_add(event
, nr
, nmi
, data
, regs
);
4202 int perf_swevent_get_recursion_context(void)
4204 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
4211 else if (in_softirq())
4216 if (cpuctx
->recursion
[rctx
])
4219 cpuctx
->recursion
[rctx
]++;
4224 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
4226 void perf_swevent_put_recursion_context(int rctx
)
4228 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
4230 cpuctx
->recursion
[rctx
]--;
4232 EXPORT_SYMBOL_GPL(perf_swevent_put_recursion_context
);
4235 void __perf_sw_event(u32 event_id
, u64 nr
, int nmi
,
4236 struct pt_regs
*regs
, u64 addr
)
4238 struct perf_sample_data data
;
4241 preempt_disable_notrace();
4242 rctx
= perf_swevent_get_recursion_context();
4246 perf_sample_data_init(&data
, addr
);
4248 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, nmi
, &data
, regs
);
4250 perf_swevent_put_recursion_context(rctx
);
4251 preempt_enable_notrace();
4254 static void perf_swevent_read(struct perf_event
*event
)
4258 static int perf_swevent_enable(struct perf_event
*event
)
4260 struct hw_perf_event
*hwc
= &event
->hw
;
4261 struct perf_cpu_context
*cpuctx
;
4262 struct hlist_head
*head
;
4264 cpuctx
= &__get_cpu_var(perf_cpu_context
);
4266 if (hwc
->sample_period
) {
4267 hwc
->last_period
= hwc
->sample_period
;
4268 perf_swevent_set_period(event
);
4271 head
= find_swevent_head(cpuctx
, event
);
4272 if (WARN_ON_ONCE(!head
))
4275 hlist_add_head_rcu(&event
->hlist_entry
, head
);
4280 static void perf_swevent_disable(struct perf_event
*event
)
4282 hlist_del_rcu(&event
->hlist_entry
);
4285 static void perf_swevent_void(struct perf_event
*event
)
4289 static int perf_swevent_int(struct perf_event
*event
)
4294 static const struct pmu perf_ops_generic
= {
4295 .enable
= perf_swevent_enable
,
4296 .disable
= perf_swevent_disable
,
4297 .start
= perf_swevent_int
,
4298 .stop
= perf_swevent_void
,
4299 .read
= perf_swevent_read
,
4300 .unthrottle
= perf_swevent_void
, /* hwc->interrupts already reset */
4304 * hrtimer based swevent callback
4307 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
4309 enum hrtimer_restart ret
= HRTIMER_RESTART
;
4310 struct perf_sample_data data
;
4311 struct pt_regs
*regs
;
4312 struct perf_event
*event
;
4315 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
4316 event
->pmu
->read(event
);
4318 perf_sample_data_init(&data
, 0);
4319 data
.period
= event
->hw
.last_period
;
4320 regs
= get_irq_regs();
4322 if (regs
&& !perf_exclude_event(event
, regs
)) {
4323 if (!(event
->attr
.exclude_idle
&& current
->pid
== 0))
4324 if (perf_event_overflow(event
, 0, &data
, regs
))
4325 ret
= HRTIMER_NORESTART
;
4328 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
4329 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
4334 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
4336 struct hw_perf_event
*hwc
= &event
->hw
;
4338 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
4339 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
4340 if (hwc
->sample_period
) {
4343 if (hwc
->remaining
) {
4344 if (hwc
->remaining
< 0)
4347 period
= hwc
->remaining
;
4350 period
= max_t(u64
, 10000, hwc
->sample_period
);
4352 __hrtimer_start_range_ns(&hwc
->hrtimer
,
4353 ns_to_ktime(period
), 0,
4354 HRTIMER_MODE_REL
, 0);
4358 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
4360 struct hw_perf_event
*hwc
= &event
->hw
;
4362 if (hwc
->sample_period
) {
4363 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
4364 hwc
->remaining
= ktime_to_ns(remaining
);
4366 hrtimer_cancel(&hwc
->hrtimer
);
4371 * Software event: cpu wall time clock
4374 static void cpu_clock_perf_event_update(struct perf_event
*event
)
4376 int cpu
= raw_smp_processor_id();
4380 now
= cpu_clock(cpu
);
4381 prev
= atomic64_xchg(&event
->hw
.prev_count
, now
);
4382 atomic64_add(now
- prev
, &event
->count
);
4385 static int cpu_clock_perf_event_enable(struct perf_event
*event
)
4387 struct hw_perf_event
*hwc
= &event
->hw
;
4388 int cpu
= raw_smp_processor_id();
4390 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
4391 perf_swevent_start_hrtimer(event
);
4396 static void cpu_clock_perf_event_disable(struct perf_event
*event
)
4398 perf_swevent_cancel_hrtimer(event
);
4399 cpu_clock_perf_event_update(event
);
4402 static void cpu_clock_perf_event_read(struct perf_event
*event
)
4404 cpu_clock_perf_event_update(event
);
4407 static const struct pmu perf_ops_cpu_clock
= {
4408 .enable
= cpu_clock_perf_event_enable
,
4409 .disable
= cpu_clock_perf_event_disable
,
4410 .read
= cpu_clock_perf_event_read
,
4414 * Software event: task time clock
4417 static void task_clock_perf_event_update(struct perf_event
*event
, u64 now
)
4422 prev
= atomic64_xchg(&event
->hw
.prev_count
, now
);
4424 atomic64_add(delta
, &event
->count
);
4427 static int task_clock_perf_event_enable(struct perf_event
*event
)
4429 struct hw_perf_event
*hwc
= &event
->hw
;
4432 now
= event
->ctx
->time
;
4434 atomic64_set(&hwc
->prev_count
, now
);
4436 perf_swevent_start_hrtimer(event
);
4441 static void task_clock_perf_event_disable(struct perf_event
*event
)
4443 perf_swevent_cancel_hrtimer(event
);
4444 task_clock_perf_event_update(event
, event
->ctx
->time
);
4448 static void task_clock_perf_event_read(struct perf_event
*event
)
4453 update_context_time(event
->ctx
);
4454 time
= event
->ctx
->time
;
4456 u64 now
= perf_clock();
4457 u64 delta
= now
- event
->ctx
->timestamp
;
4458 time
= event
->ctx
->time
+ delta
;
4461 task_clock_perf_event_update(event
, time
);
4464 static const struct pmu perf_ops_task_clock
= {
4465 .enable
= task_clock_perf_event_enable
,
4466 .disable
= task_clock_perf_event_disable
,
4467 .read
= task_clock_perf_event_read
,
4470 /* Deref the hlist from the update side */
4471 static inline struct swevent_hlist
*
4472 swevent_hlist_deref(struct perf_cpu_context
*cpuctx
)
4474 return rcu_dereference_protected(cpuctx
->swevent_hlist
,
4475 lockdep_is_held(&cpuctx
->hlist_mutex
));
4478 static void swevent_hlist_release_rcu(struct rcu_head
*rcu_head
)
4480 struct swevent_hlist
*hlist
;
4482 hlist
= container_of(rcu_head
, struct swevent_hlist
, rcu_head
);
4486 static void swevent_hlist_release(struct perf_cpu_context
*cpuctx
)
4488 struct swevent_hlist
*hlist
= swevent_hlist_deref(cpuctx
);
4493 rcu_assign_pointer(cpuctx
->swevent_hlist
, NULL
);
4494 call_rcu(&hlist
->rcu_head
, swevent_hlist_release_rcu
);
4497 static void swevent_hlist_put_cpu(struct perf_event
*event
, int cpu
)
4499 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4501 mutex_lock(&cpuctx
->hlist_mutex
);
4503 if (!--cpuctx
->hlist_refcount
)
4504 swevent_hlist_release(cpuctx
);
4506 mutex_unlock(&cpuctx
->hlist_mutex
);
4509 static void swevent_hlist_put(struct perf_event
*event
)
4513 if (event
->cpu
!= -1) {
4514 swevent_hlist_put_cpu(event
, event
->cpu
);
4518 for_each_possible_cpu(cpu
)
4519 swevent_hlist_put_cpu(event
, cpu
);
4522 static int swevent_hlist_get_cpu(struct perf_event
*event
, int cpu
)
4524 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4527 mutex_lock(&cpuctx
->hlist_mutex
);
4529 if (!swevent_hlist_deref(cpuctx
) && cpu_online(cpu
)) {
4530 struct swevent_hlist
*hlist
;
4532 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
4537 rcu_assign_pointer(cpuctx
->swevent_hlist
, hlist
);
4539 cpuctx
->hlist_refcount
++;
4541 mutex_unlock(&cpuctx
->hlist_mutex
);
4546 static int swevent_hlist_get(struct perf_event
*event
)
4549 int cpu
, failed_cpu
;
4551 if (event
->cpu
!= -1)
4552 return swevent_hlist_get_cpu(event
, event
->cpu
);
4555 for_each_possible_cpu(cpu
) {
4556 err
= swevent_hlist_get_cpu(event
, cpu
);
4566 for_each_possible_cpu(cpu
) {
4567 if (cpu
== failed_cpu
)
4569 swevent_hlist_put_cpu(event
, cpu
);
4576 #ifdef CONFIG_EVENT_TRACING
4578 static const struct pmu perf_ops_tracepoint
= {
4579 .enable
= perf_trace_enable
,
4580 .disable
= perf_trace_disable
,
4581 .start
= perf_swevent_int
,
4582 .stop
= perf_swevent_void
,
4583 .read
= perf_swevent_read
,
4584 .unthrottle
= perf_swevent_void
,
4587 static int perf_tp_filter_match(struct perf_event
*event
,
4588 struct perf_sample_data
*data
)
4590 void *record
= data
->raw
->data
;
4592 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
4597 static int perf_tp_event_match(struct perf_event
*event
,
4598 struct perf_sample_data
*data
,
4599 struct pt_regs
*regs
)
4602 * All tracepoints are from kernel-space.
4604 if (event
->attr
.exclude_kernel
)
4607 if (!perf_tp_filter_match(event
, data
))
4613 void perf_tp_event(u64 addr
, u64 count
, void *record
, int entry_size
,
4614 struct pt_regs
*regs
, struct hlist_head
*head
)
4616 struct perf_sample_data data
;
4617 struct perf_event
*event
;
4618 struct hlist_node
*node
;
4620 struct perf_raw_record raw
= {
4625 perf_sample_data_init(&data
, addr
);
4629 hlist_for_each_entry_rcu(event
, node
, head
, hlist_entry
) {
4630 if (perf_tp_event_match(event
, &data
, regs
))
4631 perf_swevent_add(event
, count
, 1, &data
, regs
);
4635 EXPORT_SYMBOL_GPL(perf_tp_event
);
4637 static void tp_perf_event_destroy(struct perf_event
*event
)
4639 perf_trace_destroy(event
);
4642 static const struct pmu
*tp_perf_event_init(struct perf_event
*event
)
4647 * Raw tracepoint data is a severe data leak, only allow root to
4650 if ((event
->attr
.sample_type
& PERF_SAMPLE_RAW
) &&
4651 perf_paranoid_tracepoint_raw() &&
4652 !capable(CAP_SYS_ADMIN
))
4653 return ERR_PTR(-EPERM
);
4655 err
= perf_trace_init(event
);
4659 event
->destroy
= tp_perf_event_destroy
;
4661 return &perf_ops_tracepoint
;
4664 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
4669 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
4672 filter_str
= strndup_user(arg
, PAGE_SIZE
);
4673 if (IS_ERR(filter_str
))
4674 return PTR_ERR(filter_str
);
4676 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
, filter_str
);
4682 static void perf_event_free_filter(struct perf_event
*event
)
4684 ftrace_profile_free_filter(event
);
4689 static const struct pmu
*tp_perf_event_init(struct perf_event
*event
)
4694 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
4699 static void perf_event_free_filter(struct perf_event
*event
)
4703 #endif /* CONFIG_EVENT_TRACING */
4705 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4706 static void bp_perf_event_destroy(struct perf_event
*event
)
4708 release_bp_slot(event
);
4711 static const struct pmu
*bp_perf_event_init(struct perf_event
*bp
)
4715 err
= register_perf_hw_breakpoint(bp
);
4717 return ERR_PTR(err
);
4719 bp
->destroy
= bp_perf_event_destroy
;
4721 return &perf_ops_bp
;
4724 void perf_bp_event(struct perf_event
*bp
, void *data
)
4726 struct perf_sample_data sample
;
4727 struct pt_regs
*regs
= data
;
4729 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
);
4731 if (!perf_exclude_event(bp
, regs
))
4732 perf_swevent_add(bp
, 1, 1, &sample
, regs
);
4735 static const struct pmu
*bp_perf_event_init(struct perf_event
*bp
)
4740 void perf_bp_event(struct perf_event
*bp
, void *regs
)
4745 atomic_t perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
4747 static void sw_perf_event_destroy(struct perf_event
*event
)
4749 u64 event_id
= event
->attr
.config
;
4751 WARN_ON(event
->parent
);
4753 atomic_dec(&perf_swevent_enabled
[event_id
]);
4754 swevent_hlist_put(event
);
4757 static const struct pmu
*sw_perf_event_init(struct perf_event
*event
)
4759 const struct pmu
*pmu
= NULL
;
4760 u64 event_id
= event
->attr
.config
;
4763 * Software events (currently) can't in general distinguish
4764 * between user, kernel and hypervisor events.
4765 * However, context switches and cpu migrations are considered
4766 * to be kernel events, and page faults are never hypervisor
4770 case PERF_COUNT_SW_CPU_CLOCK
:
4771 pmu
= &perf_ops_cpu_clock
;
4774 case PERF_COUNT_SW_TASK_CLOCK
:
4776 * If the user instantiates this as a per-cpu event,
4777 * use the cpu_clock event instead.
4779 if (event
->ctx
->task
)
4780 pmu
= &perf_ops_task_clock
;
4782 pmu
= &perf_ops_cpu_clock
;
4785 case PERF_COUNT_SW_PAGE_FAULTS
:
4786 case PERF_COUNT_SW_PAGE_FAULTS_MIN
:
4787 case PERF_COUNT_SW_PAGE_FAULTS_MAJ
:
4788 case PERF_COUNT_SW_CONTEXT_SWITCHES
:
4789 case PERF_COUNT_SW_CPU_MIGRATIONS
:
4790 case PERF_COUNT_SW_ALIGNMENT_FAULTS
:
4791 case PERF_COUNT_SW_EMULATION_FAULTS
:
4792 if (!event
->parent
) {
4795 err
= swevent_hlist_get(event
);
4797 return ERR_PTR(err
);
4799 atomic_inc(&perf_swevent_enabled
[event_id
]);
4800 event
->destroy
= sw_perf_event_destroy
;
4802 pmu
= &perf_ops_generic
;
4810 * Allocate and initialize a event structure
4812 static struct perf_event
*
4813 perf_event_alloc(struct perf_event_attr
*attr
,
4815 struct perf_event_context
*ctx
,
4816 struct perf_event
*group_leader
,
4817 struct perf_event
*parent_event
,
4818 perf_overflow_handler_t overflow_handler
,
4821 const struct pmu
*pmu
;
4822 struct perf_event
*event
;
4823 struct hw_perf_event
*hwc
;
4826 event
= kzalloc(sizeof(*event
), gfpflags
);
4828 return ERR_PTR(-ENOMEM
);
4831 * Single events are their own group leaders, with an
4832 * empty sibling list:
4835 group_leader
= event
;
4837 mutex_init(&event
->child_mutex
);
4838 INIT_LIST_HEAD(&event
->child_list
);
4840 INIT_LIST_HEAD(&event
->group_entry
);
4841 INIT_LIST_HEAD(&event
->event_entry
);
4842 INIT_LIST_HEAD(&event
->sibling_list
);
4843 init_waitqueue_head(&event
->waitq
);
4845 mutex_init(&event
->mmap_mutex
);
4848 event
->attr
= *attr
;
4849 event
->group_leader
= group_leader
;
4854 event
->parent
= parent_event
;
4856 event
->ns
= get_pid_ns(current
->nsproxy
->pid_ns
);
4857 event
->id
= atomic64_inc_return(&perf_event_id
);
4859 event
->state
= PERF_EVENT_STATE_INACTIVE
;
4861 if (!overflow_handler
&& parent_event
)
4862 overflow_handler
= parent_event
->overflow_handler
;
4864 event
->overflow_handler
= overflow_handler
;
4867 event
->state
= PERF_EVENT_STATE_OFF
;
4872 hwc
->sample_period
= attr
->sample_period
;
4873 if (attr
->freq
&& attr
->sample_freq
)
4874 hwc
->sample_period
= 1;
4875 hwc
->last_period
= hwc
->sample_period
;
4877 atomic64_set(&hwc
->period_left
, hwc
->sample_period
);
4880 * we currently do not support PERF_FORMAT_GROUP on inherited events
4882 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
4885 switch (attr
->type
) {
4887 case PERF_TYPE_HARDWARE
:
4888 case PERF_TYPE_HW_CACHE
:
4889 pmu
= hw_perf_event_init(event
);
4892 case PERF_TYPE_SOFTWARE
:
4893 pmu
= sw_perf_event_init(event
);
4896 case PERF_TYPE_TRACEPOINT
:
4897 pmu
= tp_perf_event_init(event
);
4900 case PERF_TYPE_BREAKPOINT
:
4901 pmu
= bp_perf_event_init(event
);
4912 else if (IS_ERR(pmu
))
4917 put_pid_ns(event
->ns
);
4919 return ERR_PTR(err
);
4924 if (!event
->parent
) {
4925 atomic_inc(&nr_events
);
4926 if (event
->attr
.mmap
)
4927 atomic_inc(&nr_mmap_events
);
4928 if (event
->attr
.comm
)
4929 atomic_inc(&nr_comm_events
);
4930 if (event
->attr
.task
)
4931 atomic_inc(&nr_task_events
);
4937 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
4938 struct perf_event_attr
*attr
)
4943 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
4947 * zero the full structure, so that a short copy will be nice.
4949 memset(attr
, 0, sizeof(*attr
));
4951 ret
= get_user(size
, &uattr
->size
);
4955 if (size
> PAGE_SIZE
) /* silly large */
4958 if (!size
) /* abi compat */
4959 size
= PERF_ATTR_SIZE_VER0
;
4961 if (size
< PERF_ATTR_SIZE_VER0
)
4965 * If we're handed a bigger struct than we know of,
4966 * ensure all the unknown bits are 0 - i.e. new
4967 * user-space does not rely on any kernel feature
4968 * extensions we dont know about yet.
4970 if (size
> sizeof(*attr
)) {
4971 unsigned char __user
*addr
;
4972 unsigned char __user
*end
;
4975 addr
= (void __user
*)uattr
+ sizeof(*attr
);
4976 end
= (void __user
*)uattr
+ size
;
4978 for (; addr
< end
; addr
++) {
4979 ret
= get_user(val
, addr
);
4985 size
= sizeof(*attr
);
4988 ret
= copy_from_user(attr
, uattr
, size
);
4993 * If the type exists, the corresponding creation will verify
4996 if (attr
->type
>= PERF_TYPE_MAX
)
4999 if (attr
->__reserved_1
)
5002 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
5005 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
5012 put_user(sizeof(*attr
), &uattr
->size
);
5018 perf_event_set_output(struct perf_event
*event
, struct perf_event
*output_event
)
5020 struct perf_mmap_data
*data
= NULL
, *old_data
= NULL
;
5026 /* don't allow circular references */
5027 if (event
== output_event
)
5031 * Don't allow cross-cpu buffers
5033 if (output_event
->cpu
!= event
->cpu
)
5037 * If its not a per-cpu buffer, it must be the same task.
5039 if (output_event
->cpu
== -1 && output_event
->ctx
!= event
->ctx
)
5043 mutex_lock(&event
->mmap_mutex
);
5044 /* Can't redirect output if we've got an active mmap() */
5045 if (atomic_read(&event
->mmap_count
))
5049 /* get the buffer we want to redirect to */
5050 data
= perf_mmap_data_get(output_event
);
5055 old_data
= event
->data
;
5056 rcu_assign_pointer(event
->data
, data
);
5059 mutex_unlock(&event
->mmap_mutex
);
5062 perf_mmap_data_put(old_data
);
5068 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5070 * @attr_uptr: event_id type attributes for monitoring/sampling
5073 * @group_fd: group leader event fd
5075 SYSCALL_DEFINE5(perf_event_open
,
5076 struct perf_event_attr __user
*, attr_uptr
,
5077 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
5079 struct perf_event
*event
, *group_leader
= NULL
, *output_event
= NULL
;
5080 struct perf_event_attr attr
;
5081 struct perf_event_context
*ctx
;
5082 struct file
*event_file
= NULL
;
5083 struct file
*group_file
= NULL
;
5085 int fput_needed
= 0;
5088 /* for future expandability... */
5089 if (flags
& ~(PERF_FLAG_FD_NO_GROUP
| PERF_FLAG_FD_OUTPUT
))
5092 err
= perf_copy_attr(attr_uptr
, &attr
);
5096 if (!attr
.exclude_kernel
) {
5097 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
5102 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
5106 event_fd
= get_unused_fd_flags(O_RDWR
);
5111 * Get the target context (task or percpu):
5113 ctx
= find_get_context(pid
, cpu
);
5119 if (group_fd
!= -1) {
5120 group_leader
= perf_fget_light(group_fd
, &fput_needed
);
5121 if (IS_ERR(group_leader
)) {
5122 err
= PTR_ERR(group_leader
);
5123 goto err_put_context
;
5125 group_file
= group_leader
->filp
;
5126 if (flags
& PERF_FLAG_FD_OUTPUT
)
5127 output_event
= group_leader
;
5128 if (flags
& PERF_FLAG_FD_NO_GROUP
)
5129 group_leader
= NULL
;
5133 * Look up the group leader (we will attach this event to it):
5139 * Do not allow a recursive hierarchy (this new sibling
5140 * becoming part of another group-sibling):
5142 if (group_leader
->group_leader
!= group_leader
)
5143 goto err_put_context
;
5145 * Do not allow to attach to a group in a different
5146 * task or CPU context:
5148 if (group_leader
->ctx
!= ctx
)
5149 goto err_put_context
;
5151 * Only a group leader can be exclusive or pinned
5153 if (attr
.exclusive
|| attr
.pinned
)
5154 goto err_put_context
;
5157 event
= perf_event_alloc(&attr
, cpu
, ctx
, group_leader
,
5158 NULL
, NULL
, GFP_KERNEL
);
5159 if (IS_ERR(event
)) {
5160 err
= PTR_ERR(event
);
5161 goto err_put_context
;
5165 err
= perf_event_set_output(event
, output_event
);
5167 goto err_free_put_context
;
5170 event_file
= anon_inode_getfile("[perf_event]", &perf_fops
, event
, O_RDWR
);
5171 if (IS_ERR(event_file
)) {
5172 err
= PTR_ERR(event_file
);
5173 goto err_free_put_context
;
5176 event
->filp
= event_file
;
5177 WARN_ON_ONCE(ctx
->parent_ctx
);
5178 mutex_lock(&ctx
->mutex
);
5179 perf_install_in_context(ctx
, event
, cpu
);
5181 mutex_unlock(&ctx
->mutex
);
5183 event
->owner
= current
;
5184 get_task_struct(current
);
5185 mutex_lock(¤t
->perf_event_mutex
);
5186 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
5187 mutex_unlock(¤t
->perf_event_mutex
);
5190 * Drop the reference on the group_event after placing the
5191 * new event on the sibling_list. This ensures destruction
5192 * of the group leader will find the pointer to itself in
5193 * perf_group_detach().
5195 fput_light(group_file
, fput_needed
);
5196 fd_install(event_fd
, event_file
);
5199 err_free_put_context
:
5202 fput_light(group_file
, fput_needed
);
5205 put_unused_fd(event_fd
);
5210 * perf_event_create_kernel_counter
5212 * @attr: attributes of the counter to create
5213 * @cpu: cpu in which the counter is bound
5214 * @pid: task to profile
5217 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
5219 perf_overflow_handler_t overflow_handler
)
5221 struct perf_event
*event
;
5222 struct perf_event_context
*ctx
;
5226 * Get the target context (task or percpu):
5229 ctx
= find_get_context(pid
, cpu
);
5235 event
= perf_event_alloc(attr
, cpu
, ctx
, NULL
,
5236 NULL
, overflow_handler
, GFP_KERNEL
);
5237 if (IS_ERR(event
)) {
5238 err
= PTR_ERR(event
);
5239 goto err_put_context
;
5243 WARN_ON_ONCE(ctx
->parent_ctx
);
5244 mutex_lock(&ctx
->mutex
);
5245 perf_install_in_context(ctx
, event
, cpu
);
5247 mutex_unlock(&ctx
->mutex
);
5249 event
->owner
= current
;
5250 get_task_struct(current
);
5251 mutex_lock(¤t
->perf_event_mutex
);
5252 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
5253 mutex_unlock(¤t
->perf_event_mutex
);
5260 return ERR_PTR(err
);
5262 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
5265 * inherit a event from parent task to child task:
5267 static struct perf_event
*
5268 inherit_event(struct perf_event
*parent_event
,
5269 struct task_struct
*parent
,
5270 struct perf_event_context
*parent_ctx
,
5271 struct task_struct
*child
,
5272 struct perf_event
*group_leader
,
5273 struct perf_event_context
*child_ctx
)
5275 struct perf_event
*child_event
;
5278 * Instead of creating recursive hierarchies of events,
5279 * we link inherited events back to the original parent,
5280 * which has a filp for sure, which we use as the reference
5283 if (parent_event
->parent
)
5284 parent_event
= parent_event
->parent
;
5286 child_event
= perf_event_alloc(&parent_event
->attr
,
5287 parent_event
->cpu
, child_ctx
,
5288 group_leader
, parent_event
,
5290 if (IS_ERR(child_event
))
5295 * Make the child state follow the state of the parent event,
5296 * not its attr.disabled bit. We hold the parent's mutex,
5297 * so we won't race with perf_event_{en, dis}able_family.
5299 if (parent_event
->state
>= PERF_EVENT_STATE_INACTIVE
)
5300 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
5302 child_event
->state
= PERF_EVENT_STATE_OFF
;
5304 if (parent_event
->attr
.freq
) {
5305 u64 sample_period
= parent_event
->hw
.sample_period
;
5306 struct hw_perf_event
*hwc
= &child_event
->hw
;
5308 hwc
->sample_period
= sample_period
;
5309 hwc
->last_period
= sample_period
;
5311 atomic64_set(&hwc
->period_left
, sample_period
);
5314 child_event
->overflow_handler
= parent_event
->overflow_handler
;
5317 * Link it up in the child's context:
5319 add_event_to_ctx(child_event
, child_ctx
);
5322 * Get a reference to the parent filp - we will fput it
5323 * when the child event exits. This is safe to do because
5324 * we are in the parent and we know that the filp still
5325 * exists and has a nonzero count:
5327 atomic_long_inc(&parent_event
->filp
->f_count
);
5330 * Link this into the parent event's child list
5332 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
5333 mutex_lock(&parent_event
->child_mutex
);
5334 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
5335 mutex_unlock(&parent_event
->child_mutex
);
5340 static int inherit_group(struct perf_event
*parent_event
,
5341 struct task_struct
*parent
,
5342 struct perf_event_context
*parent_ctx
,
5343 struct task_struct
*child
,
5344 struct perf_event_context
*child_ctx
)
5346 struct perf_event
*leader
;
5347 struct perf_event
*sub
;
5348 struct perf_event
*child_ctr
;
5350 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
5351 child
, NULL
, child_ctx
);
5353 return PTR_ERR(leader
);
5354 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
5355 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
5356 child
, leader
, child_ctx
);
5357 if (IS_ERR(child_ctr
))
5358 return PTR_ERR(child_ctr
);
5363 static void sync_child_event(struct perf_event
*child_event
,
5364 struct task_struct
*child
)
5366 struct perf_event
*parent_event
= child_event
->parent
;
5369 if (child_event
->attr
.inherit_stat
)
5370 perf_event_read_event(child_event
, child
);
5372 child_val
= atomic64_read(&child_event
->count
);
5375 * Add back the child's count to the parent's count:
5377 atomic64_add(child_val
, &parent_event
->count
);
5378 atomic64_add(child_event
->total_time_enabled
,
5379 &parent_event
->child_total_time_enabled
);
5380 atomic64_add(child_event
->total_time_running
,
5381 &parent_event
->child_total_time_running
);
5384 * Remove this event from the parent's list
5386 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
5387 mutex_lock(&parent_event
->child_mutex
);
5388 list_del_init(&child_event
->child_list
);
5389 mutex_unlock(&parent_event
->child_mutex
);
5392 * Release the parent event, if this was the last
5395 fput(parent_event
->filp
);
5399 __perf_event_exit_task(struct perf_event
*child_event
,
5400 struct perf_event_context
*child_ctx
,
5401 struct task_struct
*child
)
5403 struct perf_event
*parent_event
;
5405 perf_event_remove_from_context(child_event
);
5407 parent_event
= child_event
->parent
;
5409 * It can happen that parent exits first, and has events
5410 * that are still around due to the child reference. These
5411 * events need to be zapped - but otherwise linger.
5414 sync_child_event(child_event
, child
);
5415 free_event(child_event
);
5420 * When a child task exits, feed back event values to parent events.
5422 void perf_event_exit_task(struct task_struct
*child
)
5424 struct perf_event
*child_event
, *tmp
;
5425 struct perf_event_context
*child_ctx
;
5426 unsigned long flags
;
5428 if (likely(!child
->perf_event_ctxp
)) {
5429 perf_event_task(child
, NULL
, 0);
5433 local_irq_save(flags
);
5435 * We can't reschedule here because interrupts are disabled,
5436 * and either child is current or it is a task that can't be
5437 * scheduled, so we are now safe from rescheduling changing
5440 child_ctx
= child
->perf_event_ctxp
;
5441 __perf_event_task_sched_out(child_ctx
);
5444 * Take the context lock here so that if find_get_context is
5445 * reading child->perf_event_ctxp, we wait until it has
5446 * incremented the context's refcount before we do put_ctx below.
5448 raw_spin_lock(&child_ctx
->lock
);
5449 child
->perf_event_ctxp
= NULL
;
5451 * If this context is a clone; unclone it so it can't get
5452 * swapped to another process while we're removing all
5453 * the events from it.
5455 unclone_ctx(child_ctx
);
5456 update_context_time(child_ctx
);
5457 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
5460 * Report the task dead after unscheduling the events so that we
5461 * won't get any samples after PERF_RECORD_EXIT. We can however still
5462 * get a few PERF_RECORD_READ events.
5464 perf_event_task(child
, child_ctx
, 0);
5467 * We can recurse on the same lock type through:
5469 * __perf_event_exit_task()
5470 * sync_child_event()
5471 * fput(parent_event->filp)
5473 * mutex_lock(&ctx->mutex)
5475 * But since its the parent context it won't be the same instance.
5477 mutex_lock(&child_ctx
->mutex
);
5480 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->pinned_groups
,
5482 __perf_event_exit_task(child_event
, child_ctx
, child
);
5484 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->flexible_groups
,
5486 __perf_event_exit_task(child_event
, child_ctx
, child
);
5489 * If the last event was a group event, it will have appended all
5490 * its siblings to the list, but we obtained 'tmp' before that which
5491 * will still point to the list head terminating the iteration.
5493 if (!list_empty(&child_ctx
->pinned_groups
) ||
5494 !list_empty(&child_ctx
->flexible_groups
))
5497 mutex_unlock(&child_ctx
->mutex
);
5502 static void perf_free_event(struct perf_event
*event
,
5503 struct perf_event_context
*ctx
)
5505 struct perf_event
*parent
= event
->parent
;
5507 if (WARN_ON_ONCE(!parent
))
5510 mutex_lock(&parent
->child_mutex
);
5511 list_del_init(&event
->child_list
);
5512 mutex_unlock(&parent
->child_mutex
);
5516 perf_group_detach(event
);
5517 list_del_event(event
, ctx
);
5522 * free an unexposed, unused context as created by inheritance by
5523 * init_task below, used by fork() in case of fail.
5525 void perf_event_free_task(struct task_struct
*task
)
5527 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
5528 struct perf_event
*event
, *tmp
;
5533 mutex_lock(&ctx
->mutex
);
5535 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
, group_entry
)
5536 perf_free_event(event
, ctx
);
5538 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
,
5540 perf_free_event(event
, ctx
);
5542 if (!list_empty(&ctx
->pinned_groups
) ||
5543 !list_empty(&ctx
->flexible_groups
))
5546 mutex_unlock(&ctx
->mutex
);
5552 inherit_task_group(struct perf_event
*event
, struct task_struct
*parent
,
5553 struct perf_event_context
*parent_ctx
,
5554 struct task_struct
*child
,
5558 struct perf_event_context
*child_ctx
= child
->perf_event_ctxp
;
5560 if (!event
->attr
.inherit
) {
5567 * This is executed from the parent task context, so
5568 * inherit events that have been marked for cloning.
5569 * First allocate and initialize a context for the
5573 child_ctx
= kzalloc(sizeof(struct perf_event_context
),
5578 __perf_event_init_context(child_ctx
, child
);
5579 child
->perf_event_ctxp
= child_ctx
;
5580 get_task_struct(child
);
5583 ret
= inherit_group(event
, parent
, parent_ctx
,
5594 * Initialize the perf_event context in task_struct
5596 int perf_event_init_task(struct task_struct
*child
)
5598 struct perf_event_context
*child_ctx
, *parent_ctx
;
5599 struct perf_event_context
*cloned_ctx
;
5600 struct perf_event
*event
;
5601 struct task_struct
*parent
= current
;
5602 int inherited_all
= 1;
5603 unsigned long flags
;
5606 child
->perf_event_ctxp
= NULL
;
5608 mutex_init(&child
->perf_event_mutex
);
5609 INIT_LIST_HEAD(&child
->perf_event_list
);
5611 if (likely(!parent
->perf_event_ctxp
))
5615 * If the parent's context is a clone, pin it so it won't get
5618 parent_ctx
= perf_pin_task_context(parent
);
5621 * No need to check if parent_ctx != NULL here; since we saw
5622 * it non-NULL earlier, the only reason for it to become NULL
5623 * is if we exit, and since we're currently in the middle of
5624 * a fork we can't be exiting at the same time.
5628 * Lock the parent list. No need to lock the child - not PID
5629 * hashed yet and not running, so nobody can access it.
5631 mutex_lock(&parent_ctx
->mutex
);
5634 * We dont have to disable NMIs - we are only looking at
5635 * the list, not manipulating it:
5637 list_for_each_entry(event
, &parent_ctx
->pinned_groups
, group_entry
) {
5638 ret
= inherit_task_group(event
, parent
, parent_ctx
, child
,
5645 * We can't hold ctx->lock when iterating the ->flexible_group list due
5646 * to allocations, but we need to prevent rotation because
5647 * rotate_ctx() will change the list from interrupt context.
5649 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
5650 parent_ctx
->rotate_disable
= 1;
5651 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
5653 list_for_each_entry(event
, &parent_ctx
->flexible_groups
, group_entry
) {
5654 ret
= inherit_task_group(event
, parent
, parent_ctx
, child
,
5660 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
5661 parent_ctx
->rotate_disable
= 0;
5662 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
5664 child_ctx
= child
->perf_event_ctxp
;
5666 if (child_ctx
&& inherited_all
) {
5668 * Mark the child context as a clone of the parent
5669 * context, or of whatever the parent is a clone of.
5670 * Note that if the parent is a clone, it could get
5671 * uncloned at any point, but that doesn't matter
5672 * because the list of events and the generation
5673 * count can't have changed since we took the mutex.
5675 cloned_ctx
= rcu_dereference(parent_ctx
->parent_ctx
);
5677 child_ctx
->parent_ctx
= cloned_ctx
;
5678 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
5680 child_ctx
->parent_ctx
= parent_ctx
;
5681 child_ctx
->parent_gen
= parent_ctx
->generation
;
5683 get_ctx(child_ctx
->parent_ctx
);
5686 mutex_unlock(&parent_ctx
->mutex
);
5688 perf_unpin_context(parent_ctx
);
5693 static void __init
perf_event_init_all_cpus(void)
5696 struct perf_cpu_context
*cpuctx
;
5698 for_each_possible_cpu(cpu
) {
5699 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5700 mutex_init(&cpuctx
->hlist_mutex
);
5701 __perf_event_init_context(&cpuctx
->ctx
, NULL
);
5705 static void __cpuinit
perf_event_init_cpu(int cpu
)
5707 struct perf_cpu_context
*cpuctx
;
5709 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5711 spin_lock(&perf_resource_lock
);
5712 cpuctx
->max_pertask
= perf_max_events
- perf_reserved_percpu
;
5713 spin_unlock(&perf_resource_lock
);
5715 mutex_lock(&cpuctx
->hlist_mutex
);
5716 if (cpuctx
->hlist_refcount
> 0) {
5717 struct swevent_hlist
*hlist
;
5719 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
5720 WARN_ON_ONCE(!hlist
);
5721 rcu_assign_pointer(cpuctx
->swevent_hlist
, hlist
);
5723 mutex_unlock(&cpuctx
->hlist_mutex
);
5726 #ifdef CONFIG_HOTPLUG_CPU
5727 static void __perf_event_exit_cpu(void *info
)
5729 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
5730 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
5731 struct perf_event
*event
, *tmp
;
5733 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
, group_entry
)
5734 __perf_event_remove_from_context(event
);
5735 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
, group_entry
)
5736 __perf_event_remove_from_context(event
);
5738 static void perf_event_exit_cpu(int cpu
)
5740 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5741 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
5743 mutex_lock(&cpuctx
->hlist_mutex
);
5744 swevent_hlist_release(cpuctx
);
5745 mutex_unlock(&cpuctx
->hlist_mutex
);
5747 mutex_lock(&ctx
->mutex
);
5748 smp_call_function_single(cpu
, __perf_event_exit_cpu
, NULL
, 1);
5749 mutex_unlock(&ctx
->mutex
);
5752 static inline void perf_event_exit_cpu(int cpu
) { }
5755 static int __cpuinit
5756 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
5758 unsigned int cpu
= (long)hcpu
;
5762 case CPU_UP_PREPARE
:
5763 case CPU_UP_PREPARE_FROZEN
:
5764 perf_event_init_cpu(cpu
);
5767 case CPU_DOWN_PREPARE
:
5768 case CPU_DOWN_PREPARE_FROZEN
:
5769 perf_event_exit_cpu(cpu
);
5780 * This has to have a higher priority than migration_notifier in sched.c.
5782 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
5783 .notifier_call
= perf_cpu_notify
,
5787 void __init
perf_event_init(void)
5789 perf_event_init_all_cpus();
5790 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
5791 (void *)(long)smp_processor_id());
5792 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_ONLINE
,
5793 (void *)(long)smp_processor_id());
5794 register_cpu_notifier(&perf_cpu_nb
);
5797 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class,
5798 struct sysdev_class_attribute
*attr
,
5801 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
5805 perf_set_reserve_percpu(struct sysdev_class
*class,
5806 struct sysdev_class_attribute
*attr
,
5810 struct perf_cpu_context
*cpuctx
;
5814 err
= strict_strtoul(buf
, 10, &val
);
5817 if (val
> perf_max_events
)
5820 spin_lock(&perf_resource_lock
);
5821 perf_reserved_percpu
= val
;
5822 for_each_online_cpu(cpu
) {
5823 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5824 raw_spin_lock_irq(&cpuctx
->ctx
.lock
);
5825 mpt
= min(perf_max_events
- cpuctx
->ctx
.nr_events
,
5826 perf_max_events
- perf_reserved_percpu
);
5827 cpuctx
->max_pertask
= mpt
;
5828 raw_spin_unlock_irq(&cpuctx
->ctx
.lock
);
5830 spin_unlock(&perf_resource_lock
);
5835 static ssize_t
perf_show_overcommit(struct sysdev_class
*class,
5836 struct sysdev_class_attribute
*attr
,
5839 return sprintf(buf
, "%d\n", perf_overcommit
);
5843 perf_set_overcommit(struct sysdev_class
*class,
5844 struct sysdev_class_attribute
*attr
,
5845 const char *buf
, size_t count
)
5850 err
= strict_strtoul(buf
, 10, &val
);
5856 spin_lock(&perf_resource_lock
);
5857 perf_overcommit
= val
;
5858 spin_unlock(&perf_resource_lock
);
5863 static SYSDEV_CLASS_ATTR(
5866 perf_show_reserve_percpu
,
5867 perf_set_reserve_percpu
5870 static SYSDEV_CLASS_ATTR(
5873 perf_show_overcommit
,
5877 static struct attribute
*perfclass_attrs
[] = {
5878 &attr_reserve_percpu
.attr
,
5879 &attr_overcommit
.attr
,
5883 static struct attribute_group perfclass_attr_group
= {
5884 .attrs
= perfclass_attrs
,
5885 .name
= "perf_events",
5888 static int __init
perf_event_sysfs_init(void)
5890 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
5891 &perfclass_attr_group
);
5893 device_initcall(perf_event_sysfs_init
);