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/reboot.h>
25 #include <linux/vmstat.h>
26 #include <linux/vmalloc.h>
27 #include <linux/hardirq.h>
28 #include <linux/rculist.h>
29 #include <linux/uaccess.h>
30 #include <linux/syscalls.h>
31 #include <linux/anon_inodes.h>
32 #include <linux/kernel_stat.h>
33 #include <linux/perf_event.h>
34 #include <linux/ftrace_event.h>
35 #include <linux/hw_breakpoint.h>
37 #include <asm/irq_regs.h>
39 atomic_t perf_task_events __read_mostly
;
40 static atomic_t nr_mmap_events __read_mostly
;
41 static atomic_t nr_comm_events __read_mostly
;
42 static atomic_t nr_task_events __read_mostly
;
44 static LIST_HEAD(pmus
);
45 static DEFINE_MUTEX(pmus_lock
);
46 static struct srcu_struct pmus_srcu
;
49 * perf event paranoia level:
50 * -1 - not paranoid at all
51 * 0 - disallow raw tracepoint access for unpriv
52 * 1 - disallow cpu events for unpriv
53 * 2 - disallow kernel profiling for unpriv
55 int sysctl_perf_event_paranoid __read_mostly
= 1;
57 int sysctl_perf_event_mlock __read_mostly
= 512; /* 'free' kb per user */
60 * max perf event sample rate
62 int sysctl_perf_event_sample_rate __read_mostly
= 100000;
64 static atomic64_t perf_event_id
;
66 void __weak
perf_event_print_debug(void) { }
68 extern __weak
const char *perf_pmu_name(void)
73 void perf_pmu_disable(struct pmu
*pmu
)
75 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
77 pmu
->pmu_disable(pmu
);
80 void perf_pmu_enable(struct pmu
*pmu
)
82 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
87 static DEFINE_PER_CPU(struct list_head
, rotation_list
);
90 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
91 * because they're strictly cpu affine and rotate_start is called with IRQs
92 * disabled, while rotate_context is called from IRQ context.
94 static void perf_pmu_rotate_start(struct pmu
*pmu
)
96 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
97 struct list_head
*head
= &__get_cpu_var(rotation_list
);
99 WARN_ON(!irqs_disabled());
101 if (list_empty(&cpuctx
->rotation_list
))
102 list_add(&cpuctx
->rotation_list
, head
);
105 static void get_ctx(struct perf_event_context
*ctx
)
107 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
110 static void free_ctx(struct rcu_head
*head
)
112 struct perf_event_context
*ctx
;
114 ctx
= container_of(head
, struct perf_event_context
, rcu_head
);
118 static void put_ctx(struct perf_event_context
*ctx
)
120 if (atomic_dec_and_test(&ctx
->refcount
)) {
122 put_ctx(ctx
->parent_ctx
);
124 put_task_struct(ctx
->task
);
125 call_rcu(&ctx
->rcu_head
, free_ctx
);
129 static void unclone_ctx(struct perf_event_context
*ctx
)
131 if (ctx
->parent_ctx
) {
132 put_ctx(ctx
->parent_ctx
);
133 ctx
->parent_ctx
= NULL
;
137 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
140 * only top level events have the pid namespace they were created in
143 event
= event
->parent
;
145 return task_tgid_nr_ns(p
, event
->ns
);
148 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
151 * only top level events have the pid namespace they were created in
154 event
= event
->parent
;
156 return task_pid_nr_ns(p
, event
->ns
);
160 * If we inherit events we want to return the parent event id
163 static u64
primary_event_id(struct perf_event
*event
)
168 id
= event
->parent
->id
;
174 * Get the perf_event_context for a task and lock it.
175 * This has to cope with with the fact that until it is locked,
176 * the context could get moved to another task.
178 static struct perf_event_context
*
179 perf_lock_task_context(struct task_struct
*task
, int ctxn
, unsigned long *flags
)
181 struct perf_event_context
*ctx
;
185 ctx
= rcu_dereference(task
->perf_event_ctxp
[ctxn
]);
188 * If this context is a clone of another, it might
189 * get swapped for another underneath us by
190 * perf_event_task_sched_out, though the
191 * rcu_read_lock() protects us from any context
192 * getting freed. Lock the context and check if it
193 * got swapped before we could get the lock, and retry
194 * if so. If we locked the right context, then it
195 * can't get swapped on us any more.
197 raw_spin_lock_irqsave(&ctx
->lock
, *flags
);
198 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
[ctxn
])) {
199 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
203 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
204 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
213 * Get the context for a task and increment its pin_count so it
214 * can't get swapped to another task. This also increments its
215 * reference count so that the context can't get freed.
217 static struct perf_event_context
*
218 perf_pin_task_context(struct task_struct
*task
, int ctxn
)
220 struct perf_event_context
*ctx
;
223 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
226 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
231 static void perf_unpin_context(struct perf_event_context
*ctx
)
235 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
237 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
241 static inline u64
perf_clock(void)
243 return local_clock();
247 * Update the record of the current time in a context.
249 static void update_context_time(struct perf_event_context
*ctx
)
251 u64 now
= perf_clock();
253 ctx
->time
+= now
- ctx
->timestamp
;
254 ctx
->timestamp
= now
;
258 * Update the total_time_enabled and total_time_running fields for a event.
260 static void update_event_times(struct perf_event
*event
)
262 struct perf_event_context
*ctx
= event
->ctx
;
265 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
266 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
272 run_end
= event
->tstamp_stopped
;
274 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
276 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
277 run_end
= event
->tstamp_stopped
;
281 event
->total_time_running
= run_end
- event
->tstamp_running
;
285 * Update total_time_enabled and total_time_running for all events in a group.
287 static void update_group_times(struct perf_event
*leader
)
289 struct perf_event
*event
;
291 update_event_times(leader
);
292 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
293 update_event_times(event
);
296 static struct list_head
*
297 ctx_group_list(struct perf_event
*event
, struct perf_event_context
*ctx
)
299 if (event
->attr
.pinned
)
300 return &ctx
->pinned_groups
;
302 return &ctx
->flexible_groups
;
306 * Add a event from the lists for its context.
307 * Must be called with ctx->mutex and ctx->lock held.
310 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
312 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_CONTEXT
);
313 event
->attach_state
|= PERF_ATTACH_CONTEXT
;
316 * If we're a stand alone event or group leader, we go to the context
317 * list, group events are kept attached to the group so that
318 * perf_group_detach can, at all times, locate all siblings.
320 if (event
->group_leader
== event
) {
321 struct list_head
*list
;
323 if (is_software_event(event
))
324 event
->group_flags
|= PERF_GROUP_SOFTWARE
;
326 list
= ctx_group_list(event
, ctx
);
327 list_add_tail(&event
->group_entry
, list
);
330 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
332 perf_pmu_rotate_start(ctx
->pmu
);
334 if (event
->attr
.inherit_stat
)
339 * Called at perf_event creation and when events are attached/detached from a
342 static void perf_event__read_size(struct perf_event
*event
)
344 int entry
= sizeof(u64
); /* value */
348 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
351 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
354 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
355 entry
+= sizeof(u64
);
357 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
358 nr
+= event
->group_leader
->nr_siblings
;
363 event
->read_size
= size
;
366 static void perf_event__header_size(struct perf_event
*event
)
368 struct perf_sample_data
*data
;
369 u64 sample_type
= event
->attr
.sample_type
;
372 perf_event__read_size(event
);
374 if (sample_type
& PERF_SAMPLE_IP
)
375 size
+= sizeof(data
->ip
);
377 if (sample_type
& PERF_SAMPLE_ADDR
)
378 size
+= sizeof(data
->addr
);
380 if (sample_type
& PERF_SAMPLE_PERIOD
)
381 size
+= sizeof(data
->period
);
383 if (sample_type
& PERF_SAMPLE_READ
)
384 size
+= event
->read_size
;
386 event
->header_size
= size
;
389 static void perf_event__id_header_size(struct perf_event
*event
)
391 struct perf_sample_data
*data
;
392 u64 sample_type
= event
->attr
.sample_type
;
395 if (sample_type
& PERF_SAMPLE_TID
)
396 size
+= sizeof(data
->tid_entry
);
398 if (sample_type
& PERF_SAMPLE_TIME
)
399 size
+= sizeof(data
->time
);
401 if (sample_type
& PERF_SAMPLE_ID
)
402 size
+= sizeof(data
->id
);
404 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
405 size
+= sizeof(data
->stream_id
);
407 if (sample_type
& PERF_SAMPLE_CPU
)
408 size
+= sizeof(data
->cpu_entry
);
410 event
->id_header_size
= size
;
413 static void perf_group_attach(struct perf_event
*event
)
415 struct perf_event
*group_leader
= event
->group_leader
, *pos
;
418 * We can have double attach due to group movement in perf_event_open.
420 if (event
->attach_state
& PERF_ATTACH_GROUP
)
423 event
->attach_state
|= PERF_ATTACH_GROUP
;
425 if (group_leader
== event
)
428 if (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
&&
429 !is_software_event(event
))
430 group_leader
->group_flags
&= ~PERF_GROUP_SOFTWARE
;
432 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
433 group_leader
->nr_siblings
++;
435 perf_event__header_size(group_leader
);
437 list_for_each_entry(pos
, &group_leader
->sibling_list
, group_entry
)
438 perf_event__header_size(pos
);
442 * Remove a event from the lists for its context.
443 * Must be called with ctx->mutex and ctx->lock held.
446 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
449 * We can have double detach due to exit/hot-unplug + close.
451 if (!(event
->attach_state
& PERF_ATTACH_CONTEXT
))
454 event
->attach_state
&= ~PERF_ATTACH_CONTEXT
;
457 if (event
->attr
.inherit_stat
)
460 list_del_rcu(&event
->event_entry
);
462 if (event
->group_leader
== event
)
463 list_del_init(&event
->group_entry
);
465 update_group_times(event
);
468 * If event was in error state, then keep it
469 * that way, otherwise bogus counts will be
470 * returned on read(). The only way to get out
471 * of error state is by explicit re-enabling
474 if (event
->state
> PERF_EVENT_STATE_OFF
)
475 event
->state
= PERF_EVENT_STATE_OFF
;
478 static void perf_group_detach(struct perf_event
*event
)
480 struct perf_event
*sibling
, *tmp
;
481 struct list_head
*list
= NULL
;
484 * We can have double detach due to exit/hot-unplug + close.
486 if (!(event
->attach_state
& PERF_ATTACH_GROUP
))
489 event
->attach_state
&= ~PERF_ATTACH_GROUP
;
492 * If this is a sibling, remove it from its group.
494 if (event
->group_leader
!= event
) {
495 list_del_init(&event
->group_entry
);
496 event
->group_leader
->nr_siblings
--;
500 if (!list_empty(&event
->group_entry
))
501 list
= &event
->group_entry
;
504 * If this was a group event with sibling events then
505 * upgrade the siblings to singleton events by adding them
506 * to whatever list we are on.
508 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
510 list_move_tail(&sibling
->group_entry
, list
);
511 sibling
->group_leader
= sibling
;
513 /* Inherit group flags from the previous leader */
514 sibling
->group_flags
= event
->group_flags
;
518 perf_event__header_size(event
->group_leader
);
520 list_for_each_entry(tmp
, &event
->group_leader
->sibling_list
, group_entry
)
521 perf_event__header_size(tmp
);
525 event_filter_match(struct perf_event
*event
)
527 return event
->cpu
== -1 || event
->cpu
== smp_processor_id();
531 event_sched_out(struct perf_event
*event
,
532 struct perf_cpu_context
*cpuctx
,
533 struct perf_event_context
*ctx
)
537 * An event which could not be activated because of
538 * filter mismatch still needs to have its timings
539 * maintained, otherwise bogus information is return
540 * via read() for time_enabled, time_running:
542 if (event
->state
== PERF_EVENT_STATE_INACTIVE
543 && !event_filter_match(event
)) {
544 delta
= ctx
->time
- event
->tstamp_stopped
;
545 event
->tstamp_running
+= delta
;
546 event
->tstamp_stopped
= ctx
->time
;
549 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
552 event
->state
= PERF_EVENT_STATE_INACTIVE
;
553 if (event
->pending_disable
) {
554 event
->pending_disable
= 0;
555 event
->state
= PERF_EVENT_STATE_OFF
;
557 event
->tstamp_stopped
= ctx
->time
;
558 event
->pmu
->del(event
, 0);
561 if (!is_software_event(event
))
562 cpuctx
->active_oncpu
--;
564 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
565 cpuctx
->exclusive
= 0;
569 group_sched_out(struct perf_event
*group_event
,
570 struct perf_cpu_context
*cpuctx
,
571 struct perf_event_context
*ctx
)
573 struct perf_event
*event
;
574 int state
= group_event
->state
;
576 event_sched_out(group_event
, cpuctx
, ctx
);
579 * Schedule out siblings (if any):
581 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
582 event_sched_out(event
, cpuctx
, ctx
);
584 if (state
== PERF_EVENT_STATE_ACTIVE
&& group_event
->attr
.exclusive
)
585 cpuctx
->exclusive
= 0;
588 static inline struct perf_cpu_context
*
589 __get_cpu_context(struct perf_event_context
*ctx
)
591 return this_cpu_ptr(ctx
->pmu
->pmu_cpu_context
);
595 * Cross CPU call to remove a performance event
597 * We disable the event on the hardware level first. After that we
598 * remove it from the context list.
600 static void __perf_event_remove_from_context(void *info
)
602 struct perf_event
*event
= info
;
603 struct perf_event_context
*ctx
= event
->ctx
;
604 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
607 * If this is a task context, we need to check whether it is
608 * the current task context of this cpu. If not it has been
609 * scheduled out before the smp call arrived.
611 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
614 raw_spin_lock(&ctx
->lock
);
616 event_sched_out(event
, cpuctx
, ctx
);
618 list_del_event(event
, ctx
);
620 raw_spin_unlock(&ctx
->lock
);
625 * Remove the event from a task's (or a CPU's) list of events.
627 * Must be called with ctx->mutex held.
629 * CPU events are removed with a smp call. For task events we only
630 * call when the task is on a CPU.
632 * If event->ctx is a cloned context, callers must make sure that
633 * every task struct that event->ctx->task could possibly point to
634 * remains valid. This is OK when called from perf_release since
635 * that only calls us on the top-level context, which can't be a clone.
636 * When called from perf_event_exit_task, it's OK because the
637 * context has been detached from its task.
639 static void perf_event_remove_from_context(struct perf_event
*event
)
641 struct perf_event_context
*ctx
= event
->ctx
;
642 struct task_struct
*task
= ctx
->task
;
646 * Per cpu events are removed via an smp call and
647 * the removal is always successful.
649 smp_call_function_single(event
->cpu
,
650 __perf_event_remove_from_context
,
656 task_oncpu_function_call(task
, __perf_event_remove_from_context
,
659 raw_spin_lock_irq(&ctx
->lock
);
661 * If the context is active we need to retry the smp call.
663 if (ctx
->nr_active
&& !list_empty(&event
->group_entry
)) {
664 raw_spin_unlock_irq(&ctx
->lock
);
669 * The lock prevents that this context is scheduled in so we
670 * can remove the event safely, if the call above did not
673 if (!list_empty(&event
->group_entry
))
674 list_del_event(event
, ctx
);
675 raw_spin_unlock_irq(&ctx
->lock
);
679 * Cross CPU call to disable a performance event
681 static void __perf_event_disable(void *info
)
683 struct perf_event
*event
= info
;
684 struct perf_event_context
*ctx
= event
->ctx
;
685 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
688 * If this is a per-task event, need to check whether this
689 * event's task is the current task on this cpu.
691 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
694 raw_spin_lock(&ctx
->lock
);
697 * If the event is on, turn it off.
698 * If it is in error state, leave it in error state.
700 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
) {
701 update_context_time(ctx
);
702 update_group_times(event
);
703 if (event
== event
->group_leader
)
704 group_sched_out(event
, cpuctx
, ctx
);
706 event_sched_out(event
, cpuctx
, ctx
);
707 event
->state
= PERF_EVENT_STATE_OFF
;
710 raw_spin_unlock(&ctx
->lock
);
716 * If event->ctx is a cloned context, callers must make sure that
717 * every task struct that event->ctx->task could possibly point to
718 * remains valid. This condition is satisifed when called through
719 * perf_event_for_each_child or perf_event_for_each because they
720 * hold the top-level event's child_mutex, so any descendant that
721 * goes to exit will block in sync_child_event.
722 * When called from perf_pending_event it's OK because event->ctx
723 * is the current context on this CPU and preemption is disabled,
724 * hence we can't get into perf_event_task_sched_out for this context.
726 void perf_event_disable(struct perf_event
*event
)
728 struct perf_event_context
*ctx
= event
->ctx
;
729 struct task_struct
*task
= ctx
->task
;
733 * Disable the event on the cpu that it's on
735 smp_call_function_single(event
->cpu
, __perf_event_disable
,
741 task_oncpu_function_call(task
, __perf_event_disable
, event
);
743 raw_spin_lock_irq(&ctx
->lock
);
745 * If the event is still active, we need to retry the cross-call.
747 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
748 raw_spin_unlock_irq(&ctx
->lock
);
753 * Since we have the lock this context can't be scheduled
754 * in, so we can change the state safely.
756 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
757 update_group_times(event
);
758 event
->state
= PERF_EVENT_STATE_OFF
;
761 raw_spin_unlock_irq(&ctx
->lock
);
765 event_sched_in(struct perf_event
*event
,
766 struct perf_cpu_context
*cpuctx
,
767 struct perf_event_context
*ctx
)
769 if (event
->state
<= PERF_EVENT_STATE_OFF
)
772 event
->state
= PERF_EVENT_STATE_ACTIVE
;
773 event
->oncpu
= smp_processor_id();
775 * The new state must be visible before we turn it on in the hardware:
779 if (event
->pmu
->add(event
, PERF_EF_START
)) {
780 event
->state
= PERF_EVENT_STATE_INACTIVE
;
785 event
->tstamp_running
+= ctx
->time
- event
->tstamp_stopped
;
787 event
->shadow_ctx_time
= ctx
->time
- ctx
->timestamp
;
789 if (!is_software_event(event
))
790 cpuctx
->active_oncpu
++;
793 if (event
->attr
.exclusive
)
794 cpuctx
->exclusive
= 1;
800 group_sched_in(struct perf_event
*group_event
,
801 struct perf_cpu_context
*cpuctx
,
802 struct perf_event_context
*ctx
)
804 struct perf_event
*event
, *partial_group
= NULL
;
805 struct pmu
*pmu
= group_event
->pmu
;
807 bool simulate
= false;
809 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
814 if (event_sched_in(group_event
, cpuctx
, ctx
)) {
815 pmu
->cancel_txn(pmu
);
820 * Schedule in siblings as one group (if any):
822 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
823 if (event_sched_in(event
, cpuctx
, ctx
)) {
824 partial_group
= event
;
829 if (!pmu
->commit_txn(pmu
))
834 * Groups can be scheduled in as one unit only, so undo any
835 * partial group before returning:
836 * The events up to the failed event are scheduled out normally,
837 * tstamp_stopped will be updated.
839 * The failed events and the remaining siblings need to have
840 * their timings updated as if they had gone thru event_sched_in()
841 * and event_sched_out(). This is required to get consistent timings
842 * across the group. This also takes care of the case where the group
843 * could never be scheduled by ensuring tstamp_stopped is set to mark
844 * the time the event was actually stopped, such that time delta
845 * calculation in update_event_times() is correct.
847 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
848 if (event
== partial_group
)
852 event
->tstamp_running
+= now
- event
->tstamp_stopped
;
853 event
->tstamp_stopped
= now
;
855 event_sched_out(event
, cpuctx
, ctx
);
858 event_sched_out(group_event
, cpuctx
, ctx
);
860 pmu
->cancel_txn(pmu
);
866 * Work out whether we can put this event group on the CPU now.
868 static int group_can_go_on(struct perf_event
*event
,
869 struct perf_cpu_context
*cpuctx
,
873 * Groups consisting entirely of software events can always go on.
875 if (event
->group_flags
& PERF_GROUP_SOFTWARE
)
878 * If an exclusive group is already on, no other hardware
881 if (cpuctx
->exclusive
)
884 * If this group is exclusive and there are already
885 * events on the CPU, it can't go on.
887 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
890 * Otherwise, try to add it if all previous groups were able
896 static void add_event_to_ctx(struct perf_event
*event
,
897 struct perf_event_context
*ctx
)
899 list_add_event(event
, ctx
);
900 perf_group_attach(event
);
901 event
->tstamp_enabled
= ctx
->time
;
902 event
->tstamp_running
= ctx
->time
;
903 event
->tstamp_stopped
= ctx
->time
;
907 * Cross CPU call to install and enable a performance event
909 * Must be called with ctx->mutex held
911 static void __perf_install_in_context(void *info
)
913 struct perf_event
*event
= info
;
914 struct perf_event_context
*ctx
= event
->ctx
;
915 struct perf_event
*leader
= event
->group_leader
;
916 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
920 * If this is a task context, we need to check whether it is
921 * the current task context of this cpu. If not it has been
922 * scheduled out before the smp call arrived.
923 * Or possibly this is the right context but it isn't
924 * on this cpu because it had no events.
926 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
927 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
929 cpuctx
->task_ctx
= ctx
;
932 raw_spin_lock(&ctx
->lock
);
934 update_context_time(ctx
);
936 add_event_to_ctx(event
, ctx
);
938 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
942 * Don't put the event on if it is disabled or if
943 * it is in a group and the group isn't on.
945 if (event
->state
!= PERF_EVENT_STATE_INACTIVE
||
946 (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
))
950 * An exclusive event can't go on if there are already active
951 * hardware events, and no hardware event can go on if there
952 * is already an exclusive event on.
954 if (!group_can_go_on(event
, cpuctx
, 1))
957 err
= event_sched_in(event
, cpuctx
, ctx
);
961 * This event couldn't go on. If it is in a group
962 * then we have to pull the whole group off.
963 * If the event group is pinned then put it in error state.
966 group_sched_out(leader
, cpuctx
, ctx
);
967 if (leader
->attr
.pinned
) {
968 update_group_times(leader
);
969 leader
->state
= PERF_EVENT_STATE_ERROR
;
974 raw_spin_unlock(&ctx
->lock
);
978 * Attach a performance event to a context
980 * First we add the event to the list with the hardware enable bit
981 * in event->hw_config cleared.
983 * If the event is attached to a task which is on a CPU we use a smp
984 * call to enable it in the task context. The task might have been
985 * scheduled away, but we check this in the smp call again.
987 * Must be called with ctx->mutex held.
990 perf_install_in_context(struct perf_event_context
*ctx
,
991 struct perf_event
*event
,
994 struct task_struct
*task
= ctx
->task
;
1000 * Per cpu events are installed via an smp call and
1001 * the install is always successful.
1003 smp_call_function_single(cpu
, __perf_install_in_context
,
1009 task_oncpu_function_call(task
, __perf_install_in_context
,
1012 raw_spin_lock_irq(&ctx
->lock
);
1014 * we need to retry the smp call.
1016 if (ctx
->is_active
&& list_empty(&event
->group_entry
)) {
1017 raw_spin_unlock_irq(&ctx
->lock
);
1022 * The lock prevents that this context is scheduled in so we
1023 * can add the event safely, if it the call above did not
1026 if (list_empty(&event
->group_entry
))
1027 add_event_to_ctx(event
, ctx
);
1028 raw_spin_unlock_irq(&ctx
->lock
);
1032 * Put a event into inactive state and update time fields.
1033 * Enabling the leader of a group effectively enables all
1034 * the group members that aren't explicitly disabled, so we
1035 * have to update their ->tstamp_enabled also.
1036 * Note: this works for group members as well as group leaders
1037 * since the non-leader members' sibling_lists will be empty.
1039 static void __perf_event_mark_enabled(struct perf_event
*event
,
1040 struct perf_event_context
*ctx
)
1042 struct perf_event
*sub
;
1044 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1045 event
->tstamp_enabled
= ctx
->time
- event
->total_time_enabled
;
1046 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
1047 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
) {
1048 sub
->tstamp_enabled
=
1049 ctx
->time
- sub
->total_time_enabled
;
1055 * Cross CPU call to enable a performance event
1057 static void __perf_event_enable(void *info
)
1059 struct perf_event
*event
= info
;
1060 struct perf_event_context
*ctx
= event
->ctx
;
1061 struct perf_event
*leader
= event
->group_leader
;
1062 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1066 * If this is a per-task event, need to check whether this
1067 * event's task is the current task on this cpu.
1069 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
1070 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
1072 cpuctx
->task_ctx
= ctx
;
1075 raw_spin_lock(&ctx
->lock
);
1077 update_context_time(ctx
);
1079 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1081 __perf_event_mark_enabled(event
, ctx
);
1083 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1087 * If the event is in a group and isn't the group leader,
1088 * then don't put it on unless the group is on.
1090 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
)
1093 if (!group_can_go_on(event
, cpuctx
, 1)) {
1096 if (event
== leader
)
1097 err
= group_sched_in(event
, cpuctx
, ctx
);
1099 err
= event_sched_in(event
, cpuctx
, ctx
);
1104 * If this event can't go on and it's part of a
1105 * group, then the whole group has to come off.
1107 if (leader
!= event
)
1108 group_sched_out(leader
, cpuctx
, ctx
);
1109 if (leader
->attr
.pinned
) {
1110 update_group_times(leader
);
1111 leader
->state
= PERF_EVENT_STATE_ERROR
;
1116 raw_spin_unlock(&ctx
->lock
);
1122 * If event->ctx is a cloned context, callers must make sure that
1123 * every task struct that event->ctx->task could possibly point to
1124 * remains valid. This condition is satisfied when called through
1125 * perf_event_for_each_child or perf_event_for_each as described
1126 * for perf_event_disable.
1128 void perf_event_enable(struct perf_event
*event
)
1130 struct perf_event_context
*ctx
= event
->ctx
;
1131 struct task_struct
*task
= ctx
->task
;
1135 * Enable the event on the cpu that it's on
1137 smp_call_function_single(event
->cpu
, __perf_event_enable
,
1142 raw_spin_lock_irq(&ctx
->lock
);
1143 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1147 * If the event is in error state, clear that first.
1148 * That way, if we see the event in error state below, we
1149 * know that it has gone back into error state, as distinct
1150 * from the task having been scheduled away before the
1151 * cross-call arrived.
1153 if (event
->state
== PERF_EVENT_STATE_ERROR
)
1154 event
->state
= PERF_EVENT_STATE_OFF
;
1157 raw_spin_unlock_irq(&ctx
->lock
);
1158 task_oncpu_function_call(task
, __perf_event_enable
, event
);
1160 raw_spin_lock_irq(&ctx
->lock
);
1163 * If the context is active and the event is still off,
1164 * we need to retry the cross-call.
1166 if (ctx
->is_active
&& event
->state
== PERF_EVENT_STATE_OFF
)
1170 * Since we have the lock this context can't be scheduled
1171 * in, so we can change the state safely.
1173 if (event
->state
== PERF_EVENT_STATE_OFF
)
1174 __perf_event_mark_enabled(event
, ctx
);
1177 raw_spin_unlock_irq(&ctx
->lock
);
1180 static int perf_event_refresh(struct perf_event
*event
, int refresh
)
1183 * not supported on inherited events
1185 if (event
->attr
.inherit
|| !is_sampling_event(event
))
1188 atomic_add(refresh
, &event
->event_limit
);
1189 perf_event_enable(event
);
1195 EVENT_FLEXIBLE
= 0x1,
1197 EVENT_ALL
= EVENT_FLEXIBLE
| EVENT_PINNED
,
1200 static void ctx_sched_out(struct perf_event_context
*ctx
,
1201 struct perf_cpu_context
*cpuctx
,
1202 enum event_type_t event_type
)
1204 struct perf_event
*event
;
1206 raw_spin_lock(&ctx
->lock
);
1207 perf_pmu_disable(ctx
->pmu
);
1209 if (likely(!ctx
->nr_events
))
1211 update_context_time(ctx
);
1213 if (!ctx
->nr_active
)
1216 if (event_type
& EVENT_PINNED
) {
1217 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
)
1218 group_sched_out(event
, cpuctx
, ctx
);
1221 if (event_type
& EVENT_FLEXIBLE
) {
1222 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
)
1223 group_sched_out(event
, cpuctx
, ctx
);
1226 perf_pmu_enable(ctx
->pmu
);
1227 raw_spin_unlock(&ctx
->lock
);
1231 * Test whether two contexts are equivalent, i.e. whether they
1232 * have both been cloned from the same version of the same context
1233 * and they both have the same number of enabled events.
1234 * If the number of enabled events is the same, then the set
1235 * of enabled events should be the same, because these are both
1236 * inherited contexts, therefore we can't access individual events
1237 * in them directly with an fd; we can only enable/disable all
1238 * events via prctl, or enable/disable all events in a family
1239 * via ioctl, which will have the same effect on both contexts.
1241 static int context_equiv(struct perf_event_context
*ctx1
,
1242 struct perf_event_context
*ctx2
)
1244 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
1245 && ctx1
->parent_gen
== ctx2
->parent_gen
1246 && !ctx1
->pin_count
&& !ctx2
->pin_count
;
1249 static void __perf_event_sync_stat(struct perf_event
*event
,
1250 struct perf_event
*next_event
)
1254 if (!event
->attr
.inherit_stat
)
1258 * Update the event value, we cannot use perf_event_read()
1259 * because we're in the middle of a context switch and have IRQs
1260 * disabled, which upsets smp_call_function_single(), however
1261 * we know the event must be on the current CPU, therefore we
1262 * don't need to use it.
1264 switch (event
->state
) {
1265 case PERF_EVENT_STATE_ACTIVE
:
1266 event
->pmu
->read(event
);
1269 case PERF_EVENT_STATE_INACTIVE
:
1270 update_event_times(event
);
1278 * In order to keep per-task stats reliable we need to flip the event
1279 * values when we flip the contexts.
1281 value
= local64_read(&next_event
->count
);
1282 value
= local64_xchg(&event
->count
, value
);
1283 local64_set(&next_event
->count
, value
);
1285 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
1286 swap(event
->total_time_running
, next_event
->total_time_running
);
1289 * Since we swizzled the values, update the user visible data too.
1291 perf_event_update_userpage(event
);
1292 perf_event_update_userpage(next_event
);
1295 #define list_next_entry(pos, member) \
1296 list_entry(pos->member.next, typeof(*pos), member)
1298 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
1299 struct perf_event_context
*next_ctx
)
1301 struct perf_event
*event
, *next_event
;
1306 update_context_time(ctx
);
1308 event
= list_first_entry(&ctx
->event_list
,
1309 struct perf_event
, event_entry
);
1311 next_event
= list_first_entry(&next_ctx
->event_list
,
1312 struct perf_event
, event_entry
);
1314 while (&event
->event_entry
!= &ctx
->event_list
&&
1315 &next_event
->event_entry
!= &next_ctx
->event_list
) {
1317 __perf_event_sync_stat(event
, next_event
);
1319 event
= list_next_entry(event
, event_entry
);
1320 next_event
= list_next_entry(next_event
, event_entry
);
1324 void perf_event_context_sched_out(struct task_struct
*task
, int ctxn
,
1325 struct task_struct
*next
)
1327 struct perf_event_context
*ctx
= task
->perf_event_ctxp
[ctxn
];
1328 struct perf_event_context
*next_ctx
;
1329 struct perf_event_context
*parent
;
1330 struct perf_cpu_context
*cpuctx
;
1336 cpuctx
= __get_cpu_context(ctx
);
1337 if (!cpuctx
->task_ctx
)
1341 parent
= rcu_dereference(ctx
->parent_ctx
);
1342 next_ctx
= next
->perf_event_ctxp
[ctxn
];
1343 if (parent
&& next_ctx
&&
1344 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
1346 * Looks like the two contexts are clones, so we might be
1347 * able to optimize the context switch. We lock both
1348 * contexts and check that they are clones under the
1349 * lock (including re-checking that neither has been
1350 * uncloned in the meantime). It doesn't matter which
1351 * order we take the locks because no other cpu could
1352 * be trying to lock both of these tasks.
1354 raw_spin_lock(&ctx
->lock
);
1355 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
1356 if (context_equiv(ctx
, next_ctx
)) {
1358 * XXX do we need a memory barrier of sorts
1359 * wrt to rcu_dereference() of perf_event_ctxp
1361 task
->perf_event_ctxp
[ctxn
] = next_ctx
;
1362 next
->perf_event_ctxp
[ctxn
] = ctx
;
1364 next_ctx
->task
= task
;
1367 perf_event_sync_stat(ctx
, next_ctx
);
1369 raw_spin_unlock(&next_ctx
->lock
);
1370 raw_spin_unlock(&ctx
->lock
);
1375 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
1376 cpuctx
->task_ctx
= NULL
;
1380 #define for_each_task_context_nr(ctxn) \
1381 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1384 * Called from scheduler to remove the events of the current task,
1385 * with interrupts disabled.
1387 * We stop each event and update the event value in event->count.
1389 * This does not protect us against NMI, but disable()
1390 * sets the disabled bit in the control field of event _before_
1391 * accessing the event control register. If a NMI hits, then it will
1392 * not restart the event.
1394 void __perf_event_task_sched_out(struct task_struct
*task
,
1395 struct task_struct
*next
)
1399 for_each_task_context_nr(ctxn
)
1400 perf_event_context_sched_out(task
, ctxn
, next
);
1403 static void task_ctx_sched_out(struct perf_event_context
*ctx
,
1404 enum event_type_t event_type
)
1406 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1408 if (!cpuctx
->task_ctx
)
1411 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
1414 ctx_sched_out(ctx
, cpuctx
, event_type
);
1415 cpuctx
->task_ctx
= NULL
;
1419 * Called with IRQs disabled
1421 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
1422 enum event_type_t event_type
)
1424 ctx_sched_out(&cpuctx
->ctx
, cpuctx
, event_type
);
1428 ctx_pinned_sched_in(struct perf_event_context
*ctx
,
1429 struct perf_cpu_context
*cpuctx
)
1431 struct perf_event
*event
;
1433 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
1434 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1436 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1439 if (group_can_go_on(event
, cpuctx
, 1))
1440 group_sched_in(event
, cpuctx
, ctx
);
1443 * If this pinned group hasn't been scheduled,
1444 * put it in error state.
1446 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1447 update_group_times(event
);
1448 event
->state
= PERF_EVENT_STATE_ERROR
;
1454 ctx_flexible_sched_in(struct perf_event_context
*ctx
,
1455 struct perf_cpu_context
*cpuctx
)
1457 struct perf_event
*event
;
1460 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
1461 /* Ignore events in OFF or ERROR state */
1462 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1465 * Listen to the 'cpu' scheduling filter constraint
1468 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1471 if (group_can_go_on(event
, cpuctx
, can_add_hw
)) {
1472 if (group_sched_in(event
, cpuctx
, ctx
))
1479 ctx_sched_in(struct perf_event_context
*ctx
,
1480 struct perf_cpu_context
*cpuctx
,
1481 enum event_type_t event_type
)
1483 raw_spin_lock(&ctx
->lock
);
1485 if (likely(!ctx
->nr_events
))
1488 ctx
->timestamp
= perf_clock();
1491 * First go through the list and put on any pinned groups
1492 * in order to give them the best chance of going on.
1494 if (event_type
& EVENT_PINNED
)
1495 ctx_pinned_sched_in(ctx
, cpuctx
);
1497 /* Then walk through the lower prio flexible groups */
1498 if (event_type
& EVENT_FLEXIBLE
)
1499 ctx_flexible_sched_in(ctx
, cpuctx
);
1502 raw_spin_unlock(&ctx
->lock
);
1505 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
1506 enum event_type_t event_type
)
1508 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
1510 ctx_sched_in(ctx
, cpuctx
, event_type
);
1513 static void task_ctx_sched_in(struct perf_event_context
*ctx
,
1514 enum event_type_t event_type
)
1516 struct perf_cpu_context
*cpuctx
;
1518 cpuctx
= __get_cpu_context(ctx
);
1519 if (cpuctx
->task_ctx
== ctx
)
1522 ctx_sched_in(ctx
, cpuctx
, event_type
);
1523 cpuctx
->task_ctx
= ctx
;
1526 void perf_event_context_sched_in(struct perf_event_context
*ctx
)
1528 struct perf_cpu_context
*cpuctx
;
1530 cpuctx
= __get_cpu_context(ctx
);
1531 if (cpuctx
->task_ctx
== ctx
)
1534 perf_pmu_disable(ctx
->pmu
);
1536 * We want to keep the following priority order:
1537 * cpu pinned (that don't need to move), task pinned,
1538 * cpu flexible, task flexible.
1540 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
1542 ctx_sched_in(ctx
, cpuctx
, EVENT_PINNED
);
1543 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
);
1544 ctx_sched_in(ctx
, cpuctx
, EVENT_FLEXIBLE
);
1546 cpuctx
->task_ctx
= ctx
;
1549 * Since these rotations are per-cpu, we need to ensure the
1550 * cpu-context we got scheduled on is actually rotating.
1552 perf_pmu_rotate_start(ctx
->pmu
);
1553 perf_pmu_enable(ctx
->pmu
);
1557 * Called from scheduler to add the events of the current task
1558 * with interrupts disabled.
1560 * We restore the event value and then enable it.
1562 * This does not protect us against NMI, but enable()
1563 * sets the enabled bit in the control field of event _before_
1564 * accessing the event control register. If a NMI hits, then it will
1565 * keep the event running.
1567 void __perf_event_task_sched_in(struct task_struct
*task
)
1569 struct perf_event_context
*ctx
;
1572 for_each_task_context_nr(ctxn
) {
1573 ctx
= task
->perf_event_ctxp
[ctxn
];
1577 perf_event_context_sched_in(ctx
);
1581 #define MAX_INTERRUPTS (~0ULL)
1583 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1585 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1587 u64 frequency
= event
->attr
.sample_freq
;
1588 u64 sec
= NSEC_PER_SEC
;
1589 u64 divisor
, dividend
;
1591 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
1593 count_fls
= fls64(count
);
1594 nsec_fls
= fls64(nsec
);
1595 frequency_fls
= fls64(frequency
);
1599 * We got @count in @nsec, with a target of sample_freq HZ
1600 * the target period becomes:
1603 * period = -------------------
1604 * @nsec * sample_freq
1609 * Reduce accuracy by one bit such that @a and @b converge
1610 * to a similar magnitude.
1612 #define REDUCE_FLS(a, b) \
1614 if (a##_fls > b##_fls) { \
1624 * Reduce accuracy until either term fits in a u64, then proceed with
1625 * the other, so that finally we can do a u64/u64 division.
1627 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
1628 REDUCE_FLS(nsec
, frequency
);
1629 REDUCE_FLS(sec
, count
);
1632 if (count_fls
+ sec_fls
> 64) {
1633 divisor
= nsec
* frequency
;
1635 while (count_fls
+ sec_fls
> 64) {
1636 REDUCE_FLS(count
, sec
);
1640 dividend
= count
* sec
;
1642 dividend
= count
* sec
;
1644 while (nsec_fls
+ frequency_fls
> 64) {
1645 REDUCE_FLS(nsec
, frequency
);
1649 divisor
= nsec
* frequency
;
1655 return div64_u64(dividend
, divisor
);
1658 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1660 struct hw_perf_event
*hwc
= &event
->hw
;
1661 s64 period
, sample_period
;
1664 period
= perf_calculate_period(event
, nsec
, count
);
1666 delta
= (s64
)(period
- hwc
->sample_period
);
1667 delta
= (delta
+ 7) / 8; /* low pass filter */
1669 sample_period
= hwc
->sample_period
+ delta
;
1674 hwc
->sample_period
= sample_period
;
1676 if (local64_read(&hwc
->period_left
) > 8*sample_period
) {
1677 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
1678 local64_set(&hwc
->period_left
, 0);
1679 event
->pmu
->start(event
, PERF_EF_RELOAD
);
1683 static void perf_ctx_adjust_freq(struct perf_event_context
*ctx
, u64 period
)
1685 struct perf_event
*event
;
1686 struct hw_perf_event
*hwc
;
1687 u64 interrupts
, now
;
1690 raw_spin_lock(&ctx
->lock
);
1691 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
1692 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1695 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1700 interrupts
= hwc
->interrupts
;
1701 hwc
->interrupts
= 0;
1704 * unthrottle events on the tick
1706 if (interrupts
== MAX_INTERRUPTS
) {
1707 perf_log_throttle(event
, 1);
1708 event
->pmu
->start(event
, 0);
1711 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
1714 event
->pmu
->read(event
);
1715 now
= local64_read(&event
->count
);
1716 delta
= now
- hwc
->freq_count_stamp
;
1717 hwc
->freq_count_stamp
= now
;
1720 perf_adjust_period(event
, period
, delta
);
1722 raw_spin_unlock(&ctx
->lock
);
1726 * Round-robin a context's events:
1728 static void rotate_ctx(struct perf_event_context
*ctx
)
1730 raw_spin_lock(&ctx
->lock
);
1733 * Rotate the first entry last of non-pinned groups. Rotation might be
1734 * disabled by the inheritance code.
1736 if (!ctx
->rotate_disable
)
1737 list_rotate_left(&ctx
->flexible_groups
);
1739 raw_spin_unlock(&ctx
->lock
);
1743 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
1744 * because they're strictly cpu affine and rotate_start is called with IRQs
1745 * disabled, while rotate_context is called from IRQ context.
1747 static void perf_rotate_context(struct perf_cpu_context
*cpuctx
)
1749 u64 interval
= (u64
)cpuctx
->jiffies_interval
* TICK_NSEC
;
1750 struct perf_event_context
*ctx
= NULL
;
1751 int rotate
= 0, remove
= 1;
1753 if (cpuctx
->ctx
.nr_events
) {
1755 if (cpuctx
->ctx
.nr_events
!= cpuctx
->ctx
.nr_active
)
1759 ctx
= cpuctx
->task_ctx
;
1760 if (ctx
&& ctx
->nr_events
) {
1762 if (ctx
->nr_events
!= ctx
->nr_active
)
1766 perf_pmu_disable(cpuctx
->ctx
.pmu
);
1767 perf_ctx_adjust_freq(&cpuctx
->ctx
, interval
);
1769 perf_ctx_adjust_freq(ctx
, interval
);
1774 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
1776 task_ctx_sched_out(ctx
, EVENT_FLEXIBLE
);
1778 rotate_ctx(&cpuctx
->ctx
);
1782 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
);
1784 task_ctx_sched_in(ctx
, EVENT_FLEXIBLE
);
1788 list_del_init(&cpuctx
->rotation_list
);
1790 perf_pmu_enable(cpuctx
->ctx
.pmu
);
1793 void perf_event_task_tick(void)
1795 struct list_head
*head
= &__get_cpu_var(rotation_list
);
1796 struct perf_cpu_context
*cpuctx
, *tmp
;
1798 WARN_ON(!irqs_disabled());
1800 list_for_each_entry_safe(cpuctx
, tmp
, head
, rotation_list
) {
1801 if (cpuctx
->jiffies_interval
== 1 ||
1802 !(jiffies
% cpuctx
->jiffies_interval
))
1803 perf_rotate_context(cpuctx
);
1807 static int event_enable_on_exec(struct perf_event
*event
,
1808 struct perf_event_context
*ctx
)
1810 if (!event
->attr
.enable_on_exec
)
1813 event
->attr
.enable_on_exec
= 0;
1814 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1817 __perf_event_mark_enabled(event
, ctx
);
1823 * Enable all of a task's events that have been marked enable-on-exec.
1824 * This expects task == current.
1826 static void perf_event_enable_on_exec(struct perf_event_context
*ctx
)
1828 struct perf_event
*event
;
1829 unsigned long flags
;
1833 local_irq_save(flags
);
1834 if (!ctx
|| !ctx
->nr_events
)
1837 task_ctx_sched_out(ctx
, EVENT_ALL
);
1839 raw_spin_lock(&ctx
->lock
);
1841 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
1842 ret
= event_enable_on_exec(event
, ctx
);
1847 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
1848 ret
= event_enable_on_exec(event
, ctx
);
1854 * Unclone this context if we enabled any event.
1859 raw_spin_unlock(&ctx
->lock
);
1861 perf_event_context_sched_in(ctx
);
1863 local_irq_restore(flags
);
1867 * Cross CPU call to read the hardware event
1869 static void __perf_event_read(void *info
)
1871 struct perf_event
*event
= info
;
1872 struct perf_event_context
*ctx
= event
->ctx
;
1873 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1876 * If this is a task context, we need to check whether it is
1877 * the current task context of this cpu. If not it has been
1878 * scheduled out before the smp call arrived. In that case
1879 * event->count would have been updated to a recent sample
1880 * when the event was scheduled out.
1882 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
1885 raw_spin_lock(&ctx
->lock
);
1886 update_context_time(ctx
);
1887 update_event_times(event
);
1888 raw_spin_unlock(&ctx
->lock
);
1890 event
->pmu
->read(event
);
1893 static inline u64
perf_event_count(struct perf_event
*event
)
1895 return local64_read(&event
->count
) + atomic64_read(&event
->child_count
);
1898 static u64
perf_event_read(struct perf_event
*event
)
1901 * If event is enabled and currently active on a CPU, update the
1902 * value in the event structure:
1904 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
1905 smp_call_function_single(event
->oncpu
,
1906 __perf_event_read
, event
, 1);
1907 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1908 struct perf_event_context
*ctx
= event
->ctx
;
1909 unsigned long flags
;
1911 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
1913 * may read while context is not active
1914 * (e.g., thread is blocked), in that case
1915 * we cannot update context time
1918 update_context_time(ctx
);
1919 update_event_times(event
);
1920 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1923 return perf_event_count(event
);
1930 struct callchain_cpus_entries
{
1931 struct rcu_head rcu_head
;
1932 struct perf_callchain_entry
*cpu_entries
[0];
1935 static DEFINE_PER_CPU(int, callchain_recursion
[PERF_NR_CONTEXTS
]);
1936 static atomic_t nr_callchain_events
;
1937 static DEFINE_MUTEX(callchain_mutex
);
1938 struct callchain_cpus_entries
*callchain_cpus_entries
;
1941 __weak
void perf_callchain_kernel(struct perf_callchain_entry
*entry
,
1942 struct pt_regs
*regs
)
1946 __weak
void perf_callchain_user(struct perf_callchain_entry
*entry
,
1947 struct pt_regs
*regs
)
1951 static void release_callchain_buffers_rcu(struct rcu_head
*head
)
1953 struct callchain_cpus_entries
*entries
;
1956 entries
= container_of(head
, struct callchain_cpus_entries
, rcu_head
);
1958 for_each_possible_cpu(cpu
)
1959 kfree(entries
->cpu_entries
[cpu
]);
1964 static void release_callchain_buffers(void)
1966 struct callchain_cpus_entries
*entries
;
1968 entries
= callchain_cpus_entries
;
1969 rcu_assign_pointer(callchain_cpus_entries
, NULL
);
1970 call_rcu(&entries
->rcu_head
, release_callchain_buffers_rcu
);
1973 static int alloc_callchain_buffers(void)
1977 struct callchain_cpus_entries
*entries
;
1980 * We can't use the percpu allocation API for data that can be
1981 * accessed from NMI. Use a temporary manual per cpu allocation
1982 * until that gets sorted out.
1984 size
= sizeof(*entries
) + sizeof(struct perf_callchain_entry
*) *
1985 num_possible_cpus();
1987 entries
= kzalloc(size
, GFP_KERNEL
);
1991 size
= sizeof(struct perf_callchain_entry
) * PERF_NR_CONTEXTS
;
1993 for_each_possible_cpu(cpu
) {
1994 entries
->cpu_entries
[cpu
] = kmalloc_node(size
, GFP_KERNEL
,
1996 if (!entries
->cpu_entries
[cpu
])
2000 rcu_assign_pointer(callchain_cpus_entries
, entries
);
2005 for_each_possible_cpu(cpu
)
2006 kfree(entries
->cpu_entries
[cpu
]);
2012 static int get_callchain_buffers(void)
2017 mutex_lock(&callchain_mutex
);
2019 count
= atomic_inc_return(&nr_callchain_events
);
2020 if (WARN_ON_ONCE(count
< 1)) {
2026 /* If the allocation failed, give up */
2027 if (!callchain_cpus_entries
)
2032 err
= alloc_callchain_buffers();
2034 release_callchain_buffers();
2036 mutex_unlock(&callchain_mutex
);
2041 static void put_callchain_buffers(void)
2043 if (atomic_dec_and_mutex_lock(&nr_callchain_events
, &callchain_mutex
)) {
2044 release_callchain_buffers();
2045 mutex_unlock(&callchain_mutex
);
2049 static int get_recursion_context(int *recursion
)
2057 else if (in_softirq())
2062 if (recursion
[rctx
])
2071 static inline void put_recursion_context(int *recursion
, int rctx
)
2077 static struct perf_callchain_entry
*get_callchain_entry(int *rctx
)
2080 struct callchain_cpus_entries
*entries
;
2082 *rctx
= get_recursion_context(__get_cpu_var(callchain_recursion
));
2086 entries
= rcu_dereference(callchain_cpus_entries
);
2090 cpu
= smp_processor_id();
2092 return &entries
->cpu_entries
[cpu
][*rctx
];
2096 put_callchain_entry(int rctx
)
2098 put_recursion_context(__get_cpu_var(callchain_recursion
), rctx
);
2101 static struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
2104 struct perf_callchain_entry
*entry
;
2107 entry
= get_callchain_entry(&rctx
);
2116 if (!user_mode(regs
)) {
2117 perf_callchain_store(entry
, PERF_CONTEXT_KERNEL
);
2118 perf_callchain_kernel(entry
, regs
);
2120 regs
= task_pt_regs(current
);
2126 perf_callchain_store(entry
, PERF_CONTEXT_USER
);
2127 perf_callchain_user(entry
, regs
);
2131 put_callchain_entry(rctx
);
2137 * Initialize the perf_event context in a task_struct:
2139 static void __perf_event_init_context(struct perf_event_context
*ctx
)
2141 raw_spin_lock_init(&ctx
->lock
);
2142 mutex_init(&ctx
->mutex
);
2143 INIT_LIST_HEAD(&ctx
->pinned_groups
);
2144 INIT_LIST_HEAD(&ctx
->flexible_groups
);
2145 INIT_LIST_HEAD(&ctx
->event_list
);
2146 atomic_set(&ctx
->refcount
, 1);
2149 static struct perf_event_context
*
2150 alloc_perf_context(struct pmu
*pmu
, struct task_struct
*task
)
2152 struct perf_event_context
*ctx
;
2154 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
2158 __perf_event_init_context(ctx
);
2161 get_task_struct(task
);
2168 static struct task_struct
*
2169 find_lively_task_by_vpid(pid_t vpid
)
2171 struct task_struct
*task
;
2178 task
= find_task_by_vpid(vpid
);
2180 get_task_struct(task
);
2184 return ERR_PTR(-ESRCH
);
2187 * Can't attach events to a dying task.
2190 if (task
->flags
& PF_EXITING
)
2193 /* Reuse ptrace permission checks for now. */
2195 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
2200 put_task_struct(task
);
2201 return ERR_PTR(err
);
2205 static struct perf_event_context
*
2206 find_get_context(struct pmu
*pmu
, struct task_struct
*task
, int cpu
)
2208 struct perf_event_context
*ctx
;
2209 struct perf_cpu_context
*cpuctx
;
2210 unsigned long flags
;
2213 if (!task
&& cpu
!= -1) {
2214 /* Must be root to operate on a CPU event: */
2215 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
2216 return ERR_PTR(-EACCES
);
2218 if (cpu
< 0 || cpu
>= nr_cpumask_bits
)
2219 return ERR_PTR(-EINVAL
);
2222 * We could be clever and allow to attach a event to an
2223 * offline CPU and activate it when the CPU comes up, but
2226 if (!cpu_online(cpu
))
2227 return ERR_PTR(-ENODEV
);
2229 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
2237 ctxn
= pmu
->task_ctx_nr
;
2242 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
2245 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
2249 ctx
= alloc_perf_context(pmu
, task
);
2256 if (cmpxchg(&task
->perf_event_ctxp
[ctxn
], NULL
, ctx
)) {
2258 * We raced with some other task; use
2259 * the context they set.
2261 put_task_struct(task
);
2270 return ERR_PTR(err
);
2273 static void perf_event_free_filter(struct perf_event
*event
);
2275 static void free_event_rcu(struct rcu_head
*head
)
2277 struct perf_event
*event
;
2279 event
= container_of(head
, struct perf_event
, rcu_head
);
2281 put_pid_ns(event
->ns
);
2282 perf_event_free_filter(event
);
2286 static void perf_buffer_put(struct perf_buffer
*buffer
);
2288 static void free_event(struct perf_event
*event
)
2290 irq_work_sync(&event
->pending
);
2292 if (!event
->parent
) {
2293 if (event
->attach_state
& PERF_ATTACH_TASK
)
2294 jump_label_dec(&perf_task_events
);
2295 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
2296 atomic_dec(&nr_mmap_events
);
2297 if (event
->attr
.comm
)
2298 atomic_dec(&nr_comm_events
);
2299 if (event
->attr
.task
)
2300 atomic_dec(&nr_task_events
);
2301 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
)
2302 put_callchain_buffers();
2305 if (event
->buffer
) {
2306 perf_buffer_put(event
->buffer
);
2307 event
->buffer
= NULL
;
2311 event
->destroy(event
);
2314 put_ctx(event
->ctx
);
2316 call_rcu(&event
->rcu_head
, free_event_rcu
);
2319 int perf_event_release_kernel(struct perf_event
*event
)
2321 struct perf_event_context
*ctx
= event
->ctx
;
2324 * Remove from the PMU, can't get re-enabled since we got
2325 * here because the last ref went.
2327 perf_event_disable(event
);
2329 WARN_ON_ONCE(ctx
->parent_ctx
);
2331 * There are two ways this annotation is useful:
2333 * 1) there is a lock recursion from perf_event_exit_task
2334 * see the comment there.
2336 * 2) there is a lock-inversion with mmap_sem through
2337 * perf_event_read_group(), which takes faults while
2338 * holding ctx->mutex, however this is called after
2339 * the last filedesc died, so there is no possibility
2340 * to trigger the AB-BA case.
2342 mutex_lock_nested(&ctx
->mutex
, SINGLE_DEPTH_NESTING
);
2343 raw_spin_lock_irq(&ctx
->lock
);
2344 perf_group_detach(event
);
2345 list_del_event(event
, ctx
);
2346 raw_spin_unlock_irq(&ctx
->lock
);
2347 mutex_unlock(&ctx
->mutex
);
2353 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
2356 * Called when the last reference to the file is gone.
2358 static int perf_release(struct inode
*inode
, struct file
*file
)
2360 struct perf_event
*event
= file
->private_data
;
2361 struct task_struct
*owner
;
2363 file
->private_data
= NULL
;
2366 owner
= ACCESS_ONCE(event
->owner
);
2368 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2369 * !owner it means the list deletion is complete and we can indeed
2370 * free this event, otherwise we need to serialize on
2371 * owner->perf_event_mutex.
2373 smp_read_barrier_depends();
2376 * Since delayed_put_task_struct() also drops the last
2377 * task reference we can safely take a new reference
2378 * while holding the rcu_read_lock().
2380 get_task_struct(owner
);
2385 mutex_lock(&owner
->perf_event_mutex
);
2387 * We have to re-check the event->owner field, if it is cleared
2388 * we raced with perf_event_exit_task(), acquiring the mutex
2389 * ensured they're done, and we can proceed with freeing the
2393 list_del_init(&event
->owner_entry
);
2394 mutex_unlock(&owner
->perf_event_mutex
);
2395 put_task_struct(owner
);
2398 return perf_event_release_kernel(event
);
2401 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
2403 struct perf_event
*child
;
2409 mutex_lock(&event
->child_mutex
);
2410 total
+= perf_event_read(event
);
2411 *enabled
+= event
->total_time_enabled
+
2412 atomic64_read(&event
->child_total_time_enabled
);
2413 *running
+= event
->total_time_running
+
2414 atomic64_read(&event
->child_total_time_running
);
2416 list_for_each_entry(child
, &event
->child_list
, child_list
) {
2417 total
+= perf_event_read(child
);
2418 *enabled
+= child
->total_time_enabled
;
2419 *running
+= child
->total_time_running
;
2421 mutex_unlock(&event
->child_mutex
);
2425 EXPORT_SYMBOL_GPL(perf_event_read_value
);
2427 static int perf_event_read_group(struct perf_event
*event
,
2428 u64 read_format
, char __user
*buf
)
2430 struct perf_event
*leader
= event
->group_leader
, *sub
;
2431 int n
= 0, size
= 0, ret
= -EFAULT
;
2432 struct perf_event_context
*ctx
= leader
->ctx
;
2434 u64 count
, enabled
, running
;
2436 mutex_lock(&ctx
->mutex
);
2437 count
= perf_event_read_value(leader
, &enabled
, &running
);
2439 values
[n
++] = 1 + leader
->nr_siblings
;
2440 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2441 values
[n
++] = enabled
;
2442 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2443 values
[n
++] = running
;
2444 values
[n
++] = count
;
2445 if (read_format
& PERF_FORMAT_ID
)
2446 values
[n
++] = primary_event_id(leader
);
2448 size
= n
* sizeof(u64
);
2450 if (copy_to_user(buf
, values
, size
))
2455 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
2458 values
[n
++] = perf_event_read_value(sub
, &enabled
, &running
);
2459 if (read_format
& PERF_FORMAT_ID
)
2460 values
[n
++] = primary_event_id(sub
);
2462 size
= n
* sizeof(u64
);
2464 if (copy_to_user(buf
+ ret
, values
, size
)) {
2472 mutex_unlock(&ctx
->mutex
);
2477 static int perf_event_read_one(struct perf_event
*event
,
2478 u64 read_format
, char __user
*buf
)
2480 u64 enabled
, running
;
2484 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
2485 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2486 values
[n
++] = enabled
;
2487 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2488 values
[n
++] = running
;
2489 if (read_format
& PERF_FORMAT_ID
)
2490 values
[n
++] = primary_event_id(event
);
2492 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
2495 return n
* sizeof(u64
);
2499 * Read the performance event - simple non blocking version for now
2502 perf_read_hw(struct perf_event
*event
, char __user
*buf
, size_t count
)
2504 u64 read_format
= event
->attr
.read_format
;
2508 * Return end-of-file for a read on a event that is in
2509 * error state (i.e. because it was pinned but it couldn't be
2510 * scheduled on to the CPU at some point).
2512 if (event
->state
== PERF_EVENT_STATE_ERROR
)
2515 if (count
< event
->read_size
)
2518 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2519 if (read_format
& PERF_FORMAT_GROUP
)
2520 ret
= perf_event_read_group(event
, read_format
, buf
);
2522 ret
= perf_event_read_one(event
, read_format
, buf
);
2528 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
2530 struct perf_event
*event
= file
->private_data
;
2532 return perf_read_hw(event
, buf
, count
);
2535 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
2537 struct perf_event
*event
= file
->private_data
;
2538 struct perf_buffer
*buffer
;
2539 unsigned int events
= POLL_HUP
;
2542 buffer
= rcu_dereference(event
->buffer
);
2544 events
= atomic_xchg(&buffer
->poll
, 0);
2547 poll_wait(file
, &event
->waitq
, wait
);
2552 static void perf_event_reset(struct perf_event
*event
)
2554 (void)perf_event_read(event
);
2555 local64_set(&event
->count
, 0);
2556 perf_event_update_userpage(event
);
2560 * Holding the top-level event's child_mutex means that any
2561 * descendant process that has inherited this event will block
2562 * in sync_child_event if it goes to exit, thus satisfying the
2563 * task existence requirements of perf_event_enable/disable.
2565 static void perf_event_for_each_child(struct perf_event
*event
,
2566 void (*func
)(struct perf_event
*))
2568 struct perf_event
*child
;
2570 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2571 mutex_lock(&event
->child_mutex
);
2573 list_for_each_entry(child
, &event
->child_list
, child_list
)
2575 mutex_unlock(&event
->child_mutex
);
2578 static void perf_event_for_each(struct perf_event
*event
,
2579 void (*func
)(struct perf_event
*))
2581 struct perf_event_context
*ctx
= event
->ctx
;
2582 struct perf_event
*sibling
;
2584 WARN_ON_ONCE(ctx
->parent_ctx
);
2585 mutex_lock(&ctx
->mutex
);
2586 event
= event
->group_leader
;
2588 perf_event_for_each_child(event
, func
);
2590 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
2591 perf_event_for_each_child(event
, func
);
2592 mutex_unlock(&ctx
->mutex
);
2595 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
2597 struct perf_event_context
*ctx
= event
->ctx
;
2601 if (!is_sampling_event(event
))
2604 if (copy_from_user(&value
, arg
, sizeof(value
)))
2610 raw_spin_lock_irq(&ctx
->lock
);
2611 if (event
->attr
.freq
) {
2612 if (value
> sysctl_perf_event_sample_rate
) {
2617 event
->attr
.sample_freq
= value
;
2619 event
->attr
.sample_period
= value
;
2620 event
->hw
.sample_period
= value
;
2623 raw_spin_unlock_irq(&ctx
->lock
);
2628 static const struct file_operations perf_fops
;
2630 static struct perf_event
*perf_fget_light(int fd
, int *fput_needed
)
2634 file
= fget_light(fd
, fput_needed
);
2636 return ERR_PTR(-EBADF
);
2638 if (file
->f_op
!= &perf_fops
) {
2639 fput_light(file
, *fput_needed
);
2641 return ERR_PTR(-EBADF
);
2644 return file
->private_data
;
2647 static int perf_event_set_output(struct perf_event
*event
,
2648 struct perf_event
*output_event
);
2649 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
2651 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2653 struct perf_event
*event
= file
->private_data
;
2654 void (*func
)(struct perf_event
*);
2658 case PERF_EVENT_IOC_ENABLE
:
2659 func
= perf_event_enable
;
2661 case PERF_EVENT_IOC_DISABLE
:
2662 func
= perf_event_disable
;
2664 case PERF_EVENT_IOC_RESET
:
2665 func
= perf_event_reset
;
2668 case PERF_EVENT_IOC_REFRESH
:
2669 return perf_event_refresh(event
, arg
);
2671 case PERF_EVENT_IOC_PERIOD
:
2672 return perf_event_period(event
, (u64 __user
*)arg
);
2674 case PERF_EVENT_IOC_SET_OUTPUT
:
2676 struct perf_event
*output_event
= NULL
;
2677 int fput_needed
= 0;
2681 output_event
= perf_fget_light(arg
, &fput_needed
);
2682 if (IS_ERR(output_event
))
2683 return PTR_ERR(output_event
);
2686 ret
= perf_event_set_output(event
, output_event
);
2688 fput_light(output_event
->filp
, fput_needed
);
2693 case PERF_EVENT_IOC_SET_FILTER
:
2694 return perf_event_set_filter(event
, (void __user
*)arg
);
2700 if (flags
& PERF_IOC_FLAG_GROUP
)
2701 perf_event_for_each(event
, func
);
2703 perf_event_for_each_child(event
, func
);
2708 int perf_event_task_enable(void)
2710 struct perf_event
*event
;
2712 mutex_lock(¤t
->perf_event_mutex
);
2713 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2714 perf_event_for_each_child(event
, perf_event_enable
);
2715 mutex_unlock(¤t
->perf_event_mutex
);
2720 int perf_event_task_disable(void)
2722 struct perf_event
*event
;
2724 mutex_lock(¤t
->perf_event_mutex
);
2725 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2726 perf_event_for_each_child(event
, perf_event_disable
);
2727 mutex_unlock(¤t
->perf_event_mutex
);
2732 #ifndef PERF_EVENT_INDEX_OFFSET
2733 # define PERF_EVENT_INDEX_OFFSET 0
2736 static int perf_event_index(struct perf_event
*event
)
2738 if (event
->hw
.state
& PERF_HES_STOPPED
)
2741 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
2744 return event
->hw
.idx
+ 1 - PERF_EVENT_INDEX_OFFSET
;
2748 * Callers need to ensure there can be no nesting of this function, otherwise
2749 * the seqlock logic goes bad. We can not serialize this because the arch
2750 * code calls this from NMI context.
2752 void perf_event_update_userpage(struct perf_event
*event
)
2754 struct perf_event_mmap_page
*userpg
;
2755 struct perf_buffer
*buffer
;
2758 buffer
= rcu_dereference(event
->buffer
);
2762 userpg
= buffer
->user_page
;
2765 * Disable preemption so as to not let the corresponding user-space
2766 * spin too long if we get preempted.
2771 userpg
->index
= perf_event_index(event
);
2772 userpg
->offset
= perf_event_count(event
);
2773 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
2774 userpg
->offset
-= local64_read(&event
->hw
.prev_count
);
2776 userpg
->time_enabled
= event
->total_time_enabled
+
2777 atomic64_read(&event
->child_total_time_enabled
);
2779 userpg
->time_running
= event
->total_time_running
+
2780 atomic64_read(&event
->child_total_time_running
);
2789 static unsigned long perf_data_size(struct perf_buffer
*buffer
);
2792 perf_buffer_init(struct perf_buffer
*buffer
, long watermark
, int flags
)
2794 long max_size
= perf_data_size(buffer
);
2797 buffer
->watermark
= min(max_size
, watermark
);
2799 if (!buffer
->watermark
)
2800 buffer
->watermark
= max_size
/ 2;
2802 if (flags
& PERF_BUFFER_WRITABLE
)
2803 buffer
->writable
= 1;
2805 atomic_set(&buffer
->refcount
, 1);
2808 #ifndef CONFIG_PERF_USE_VMALLOC
2811 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2814 static struct page
*
2815 perf_mmap_to_page(struct perf_buffer
*buffer
, unsigned long pgoff
)
2817 if (pgoff
> buffer
->nr_pages
)
2821 return virt_to_page(buffer
->user_page
);
2823 return virt_to_page(buffer
->data_pages
[pgoff
- 1]);
2826 static void *perf_mmap_alloc_page(int cpu
)
2831 node
= (cpu
== -1) ? cpu
: cpu_to_node(cpu
);
2832 page
= alloc_pages_node(node
, GFP_KERNEL
| __GFP_ZERO
, 0);
2836 return page_address(page
);
2839 static struct perf_buffer
*
2840 perf_buffer_alloc(int nr_pages
, long watermark
, int cpu
, int flags
)
2842 struct perf_buffer
*buffer
;
2846 size
= sizeof(struct perf_buffer
);
2847 size
+= nr_pages
* sizeof(void *);
2849 buffer
= kzalloc(size
, GFP_KERNEL
);
2853 buffer
->user_page
= perf_mmap_alloc_page(cpu
);
2854 if (!buffer
->user_page
)
2855 goto fail_user_page
;
2857 for (i
= 0; i
< nr_pages
; i
++) {
2858 buffer
->data_pages
[i
] = perf_mmap_alloc_page(cpu
);
2859 if (!buffer
->data_pages
[i
])
2860 goto fail_data_pages
;
2863 buffer
->nr_pages
= nr_pages
;
2865 perf_buffer_init(buffer
, watermark
, flags
);
2870 for (i
--; i
>= 0; i
--)
2871 free_page((unsigned long)buffer
->data_pages
[i
]);
2873 free_page((unsigned long)buffer
->user_page
);
2882 static void perf_mmap_free_page(unsigned long addr
)
2884 struct page
*page
= virt_to_page((void *)addr
);
2886 page
->mapping
= NULL
;
2890 static void perf_buffer_free(struct perf_buffer
*buffer
)
2894 perf_mmap_free_page((unsigned long)buffer
->user_page
);
2895 for (i
= 0; i
< buffer
->nr_pages
; i
++)
2896 perf_mmap_free_page((unsigned long)buffer
->data_pages
[i
]);
2900 static inline int page_order(struct perf_buffer
*buffer
)
2908 * Back perf_mmap() with vmalloc memory.
2910 * Required for architectures that have d-cache aliasing issues.
2913 static inline int page_order(struct perf_buffer
*buffer
)
2915 return buffer
->page_order
;
2918 static struct page
*
2919 perf_mmap_to_page(struct perf_buffer
*buffer
, unsigned long pgoff
)
2921 if (pgoff
> (1UL << page_order(buffer
)))
2924 return vmalloc_to_page((void *)buffer
->user_page
+ pgoff
* PAGE_SIZE
);
2927 static void perf_mmap_unmark_page(void *addr
)
2929 struct page
*page
= vmalloc_to_page(addr
);
2931 page
->mapping
= NULL
;
2934 static void perf_buffer_free_work(struct work_struct
*work
)
2936 struct perf_buffer
*buffer
;
2940 buffer
= container_of(work
, struct perf_buffer
, work
);
2941 nr
= 1 << page_order(buffer
);
2943 base
= buffer
->user_page
;
2944 for (i
= 0; i
< nr
+ 1; i
++)
2945 perf_mmap_unmark_page(base
+ (i
* PAGE_SIZE
));
2951 static void perf_buffer_free(struct perf_buffer
*buffer
)
2953 schedule_work(&buffer
->work
);
2956 static struct perf_buffer
*
2957 perf_buffer_alloc(int nr_pages
, long watermark
, int cpu
, int flags
)
2959 struct perf_buffer
*buffer
;
2963 size
= sizeof(struct perf_buffer
);
2964 size
+= sizeof(void *);
2966 buffer
= kzalloc(size
, GFP_KERNEL
);
2970 INIT_WORK(&buffer
->work
, perf_buffer_free_work
);
2972 all_buf
= vmalloc_user((nr_pages
+ 1) * PAGE_SIZE
);
2976 buffer
->user_page
= all_buf
;
2977 buffer
->data_pages
[0] = all_buf
+ PAGE_SIZE
;
2978 buffer
->page_order
= ilog2(nr_pages
);
2979 buffer
->nr_pages
= 1;
2981 perf_buffer_init(buffer
, watermark
, flags
);
2994 static unsigned long perf_data_size(struct perf_buffer
*buffer
)
2996 return buffer
->nr_pages
<< (PAGE_SHIFT
+ page_order(buffer
));
2999 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
3001 struct perf_event
*event
= vma
->vm_file
->private_data
;
3002 struct perf_buffer
*buffer
;
3003 int ret
= VM_FAULT_SIGBUS
;
3005 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
3006 if (vmf
->pgoff
== 0)
3012 buffer
= rcu_dereference(event
->buffer
);
3016 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
3019 vmf
->page
= perf_mmap_to_page(buffer
, vmf
->pgoff
);
3023 get_page(vmf
->page
);
3024 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
3025 vmf
->page
->index
= vmf
->pgoff
;
3034 static void perf_buffer_free_rcu(struct rcu_head
*rcu_head
)
3036 struct perf_buffer
*buffer
;
3038 buffer
= container_of(rcu_head
, struct perf_buffer
, rcu_head
);
3039 perf_buffer_free(buffer
);
3042 static struct perf_buffer
*perf_buffer_get(struct perf_event
*event
)
3044 struct perf_buffer
*buffer
;
3047 buffer
= rcu_dereference(event
->buffer
);
3049 if (!atomic_inc_not_zero(&buffer
->refcount
))
3057 static void perf_buffer_put(struct perf_buffer
*buffer
)
3059 if (!atomic_dec_and_test(&buffer
->refcount
))
3062 call_rcu(&buffer
->rcu_head
, perf_buffer_free_rcu
);
3065 static void perf_mmap_open(struct vm_area_struct
*vma
)
3067 struct perf_event
*event
= vma
->vm_file
->private_data
;
3069 atomic_inc(&event
->mmap_count
);
3072 static void perf_mmap_close(struct vm_area_struct
*vma
)
3074 struct perf_event
*event
= vma
->vm_file
->private_data
;
3076 if (atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
)) {
3077 unsigned long size
= perf_data_size(event
->buffer
);
3078 struct user_struct
*user
= event
->mmap_user
;
3079 struct perf_buffer
*buffer
= event
->buffer
;
3081 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &user
->locked_vm
);
3082 vma
->vm_mm
->locked_vm
-= event
->mmap_locked
;
3083 rcu_assign_pointer(event
->buffer
, NULL
);
3084 mutex_unlock(&event
->mmap_mutex
);
3086 perf_buffer_put(buffer
);
3091 static const struct vm_operations_struct perf_mmap_vmops
= {
3092 .open
= perf_mmap_open
,
3093 .close
= perf_mmap_close
,
3094 .fault
= perf_mmap_fault
,
3095 .page_mkwrite
= perf_mmap_fault
,
3098 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
3100 struct perf_event
*event
= file
->private_data
;
3101 unsigned long user_locked
, user_lock_limit
;
3102 struct user_struct
*user
= current_user();
3103 unsigned long locked
, lock_limit
;
3104 struct perf_buffer
*buffer
;
3105 unsigned long vma_size
;
3106 unsigned long nr_pages
;
3107 long user_extra
, extra
;
3108 int ret
= 0, flags
= 0;
3111 * Don't allow mmap() of inherited per-task counters. This would
3112 * create a performance issue due to all children writing to the
3115 if (event
->cpu
== -1 && event
->attr
.inherit
)
3118 if (!(vma
->vm_flags
& VM_SHARED
))
3121 vma_size
= vma
->vm_end
- vma
->vm_start
;
3122 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
3125 * If we have buffer pages ensure they're a power-of-two number, so we
3126 * can do bitmasks instead of modulo.
3128 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
3131 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
3134 if (vma
->vm_pgoff
!= 0)
3137 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3138 mutex_lock(&event
->mmap_mutex
);
3139 if (event
->buffer
) {
3140 if (event
->buffer
->nr_pages
== nr_pages
)
3141 atomic_inc(&event
->buffer
->refcount
);
3147 user_extra
= nr_pages
+ 1;
3148 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
3151 * Increase the limit linearly with more CPUs:
3153 user_lock_limit
*= num_online_cpus();
3155 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
3158 if (user_locked
> user_lock_limit
)
3159 extra
= user_locked
- user_lock_limit
;
3161 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
3162 lock_limit
>>= PAGE_SHIFT
;
3163 locked
= vma
->vm_mm
->locked_vm
+ extra
;
3165 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
3166 !capable(CAP_IPC_LOCK
)) {
3171 WARN_ON(event
->buffer
);
3173 if (vma
->vm_flags
& VM_WRITE
)
3174 flags
|= PERF_BUFFER_WRITABLE
;
3176 buffer
= perf_buffer_alloc(nr_pages
, event
->attr
.wakeup_watermark
,
3182 rcu_assign_pointer(event
->buffer
, buffer
);
3184 atomic_long_add(user_extra
, &user
->locked_vm
);
3185 event
->mmap_locked
= extra
;
3186 event
->mmap_user
= get_current_user();
3187 vma
->vm_mm
->locked_vm
+= event
->mmap_locked
;
3191 atomic_inc(&event
->mmap_count
);
3192 mutex_unlock(&event
->mmap_mutex
);
3194 vma
->vm_flags
|= VM_RESERVED
;
3195 vma
->vm_ops
= &perf_mmap_vmops
;
3200 static int perf_fasync(int fd
, struct file
*filp
, int on
)
3202 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
3203 struct perf_event
*event
= filp
->private_data
;
3206 mutex_lock(&inode
->i_mutex
);
3207 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
3208 mutex_unlock(&inode
->i_mutex
);
3216 static const struct file_operations perf_fops
= {
3217 .llseek
= no_llseek
,
3218 .release
= perf_release
,
3221 .unlocked_ioctl
= perf_ioctl
,
3222 .compat_ioctl
= perf_ioctl
,
3224 .fasync
= perf_fasync
,
3230 * If there's data, ensure we set the poll() state and publish everything
3231 * to user-space before waking everybody up.
3234 void perf_event_wakeup(struct perf_event
*event
)
3236 wake_up_all(&event
->waitq
);
3238 if (event
->pending_kill
) {
3239 kill_fasync(&event
->fasync
, SIGIO
, event
->pending_kill
);
3240 event
->pending_kill
= 0;
3244 static void perf_pending_event(struct irq_work
*entry
)
3246 struct perf_event
*event
= container_of(entry
,
3247 struct perf_event
, pending
);
3249 if (event
->pending_disable
) {
3250 event
->pending_disable
= 0;
3251 __perf_event_disable(event
);
3254 if (event
->pending_wakeup
) {
3255 event
->pending_wakeup
= 0;
3256 perf_event_wakeup(event
);
3261 * We assume there is only KVM supporting the callbacks.
3262 * Later on, we might change it to a list if there is
3263 * another virtualization implementation supporting the callbacks.
3265 struct perf_guest_info_callbacks
*perf_guest_cbs
;
3267 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
3269 perf_guest_cbs
= cbs
;
3272 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks
);
3274 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
3276 perf_guest_cbs
= NULL
;
3279 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks
);
3284 static bool perf_output_space(struct perf_buffer
*buffer
, unsigned long tail
,
3285 unsigned long offset
, unsigned long head
)
3289 if (!buffer
->writable
)
3292 mask
= perf_data_size(buffer
) - 1;
3294 offset
= (offset
- tail
) & mask
;
3295 head
= (head
- tail
) & mask
;
3297 if ((int)(head
- offset
) < 0)
3303 static void perf_output_wakeup(struct perf_output_handle
*handle
)
3305 atomic_set(&handle
->buffer
->poll
, POLL_IN
);
3308 handle
->event
->pending_wakeup
= 1;
3309 irq_work_queue(&handle
->event
->pending
);
3311 perf_event_wakeup(handle
->event
);
3315 * We need to ensure a later event_id doesn't publish a head when a former
3316 * event isn't done writing. However since we need to deal with NMIs we
3317 * cannot fully serialize things.
3319 * We only publish the head (and generate a wakeup) when the outer-most
3322 static void perf_output_get_handle(struct perf_output_handle
*handle
)
3324 struct perf_buffer
*buffer
= handle
->buffer
;
3327 local_inc(&buffer
->nest
);
3328 handle
->wakeup
= local_read(&buffer
->wakeup
);
3331 static void perf_output_put_handle(struct perf_output_handle
*handle
)
3333 struct perf_buffer
*buffer
= handle
->buffer
;
3337 head
= local_read(&buffer
->head
);
3340 * IRQ/NMI can happen here, which means we can miss a head update.
3343 if (!local_dec_and_test(&buffer
->nest
))
3347 * Publish the known good head. Rely on the full barrier implied
3348 * by atomic_dec_and_test() order the buffer->head read and this
3351 buffer
->user_page
->data_head
= head
;
3354 * Now check if we missed an update, rely on the (compiler)
3355 * barrier in atomic_dec_and_test() to re-read buffer->head.
3357 if (unlikely(head
!= local_read(&buffer
->head
))) {
3358 local_inc(&buffer
->nest
);
3362 if (handle
->wakeup
!= local_read(&buffer
->wakeup
))
3363 perf_output_wakeup(handle
);
3369 __always_inline
void perf_output_copy(struct perf_output_handle
*handle
,
3370 const void *buf
, unsigned int len
)
3373 unsigned long size
= min_t(unsigned long, handle
->size
, len
);
3375 memcpy(handle
->addr
, buf
, size
);
3378 handle
->addr
+= size
;
3380 handle
->size
-= size
;
3381 if (!handle
->size
) {
3382 struct perf_buffer
*buffer
= handle
->buffer
;
3385 handle
->page
&= buffer
->nr_pages
- 1;
3386 handle
->addr
= buffer
->data_pages
[handle
->page
];
3387 handle
->size
= PAGE_SIZE
<< page_order(buffer
);
3392 static void __perf_event_header__init_id(struct perf_event_header
*header
,
3393 struct perf_sample_data
*data
,
3394 struct perf_event
*event
)
3396 u64 sample_type
= event
->attr
.sample_type
;
3398 data
->type
= sample_type
;
3399 header
->size
+= event
->id_header_size
;
3401 if (sample_type
& PERF_SAMPLE_TID
) {
3402 /* namespace issues */
3403 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
3404 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
3407 if (sample_type
& PERF_SAMPLE_TIME
)
3408 data
->time
= perf_clock();
3410 if (sample_type
& PERF_SAMPLE_ID
)
3411 data
->id
= primary_event_id(event
);
3413 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
3414 data
->stream_id
= event
->id
;
3416 if (sample_type
& PERF_SAMPLE_CPU
) {
3417 data
->cpu_entry
.cpu
= raw_smp_processor_id();
3418 data
->cpu_entry
.reserved
= 0;
3422 static void perf_event_header__init_id(struct perf_event_header
*header
,
3423 struct perf_sample_data
*data
,
3424 struct perf_event
*event
)
3426 if (event
->attr
.sample_id_all
)
3427 __perf_event_header__init_id(header
, data
, event
);
3430 static void __perf_event__output_id_sample(struct perf_output_handle
*handle
,
3431 struct perf_sample_data
*data
)
3433 u64 sample_type
= data
->type
;
3435 if (sample_type
& PERF_SAMPLE_TID
)
3436 perf_output_put(handle
, data
->tid_entry
);
3438 if (sample_type
& PERF_SAMPLE_TIME
)
3439 perf_output_put(handle
, data
->time
);
3441 if (sample_type
& PERF_SAMPLE_ID
)
3442 perf_output_put(handle
, data
->id
);
3444 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
3445 perf_output_put(handle
, data
->stream_id
);
3447 if (sample_type
& PERF_SAMPLE_CPU
)
3448 perf_output_put(handle
, data
->cpu_entry
);
3451 static void perf_event__output_id_sample(struct perf_event
*event
,
3452 struct perf_output_handle
*handle
,
3453 struct perf_sample_data
*sample
)
3455 if (event
->attr
.sample_id_all
)
3456 __perf_event__output_id_sample(handle
, sample
);
3459 int perf_output_begin(struct perf_output_handle
*handle
,
3460 struct perf_event
*event
, unsigned int size
,
3461 int nmi
, int sample
)
3463 struct perf_buffer
*buffer
;
3464 unsigned long tail
, offset
, head
;
3466 struct perf_sample_data sample_data
;
3468 struct perf_event_header header
;
3475 * For inherited events we send all the output towards the parent.
3478 event
= event
->parent
;
3480 buffer
= rcu_dereference(event
->buffer
);
3484 handle
->buffer
= buffer
;
3485 handle
->event
= event
;
3487 handle
->sample
= sample
;
3489 if (!buffer
->nr_pages
)
3492 have_lost
= local_read(&buffer
->lost
);
3494 lost_event
.header
.size
= sizeof(lost_event
);
3495 perf_event_header__init_id(&lost_event
.header
, &sample_data
,
3497 size
+= lost_event
.header
.size
;
3500 perf_output_get_handle(handle
);
3504 * Userspace could choose to issue a mb() before updating the
3505 * tail pointer. So that all reads will be completed before the
3508 tail
= ACCESS_ONCE(buffer
->user_page
->data_tail
);
3510 offset
= head
= local_read(&buffer
->head
);
3512 if (unlikely(!perf_output_space(buffer
, tail
, offset
, head
)))
3514 } while (local_cmpxchg(&buffer
->head
, offset
, head
) != offset
);
3516 if (head
- local_read(&buffer
->wakeup
) > buffer
->watermark
)
3517 local_add(buffer
->watermark
, &buffer
->wakeup
);
3519 handle
->page
= offset
>> (PAGE_SHIFT
+ page_order(buffer
));
3520 handle
->page
&= buffer
->nr_pages
- 1;
3521 handle
->size
= offset
& ((PAGE_SIZE
<< page_order(buffer
)) - 1);
3522 handle
->addr
= buffer
->data_pages
[handle
->page
];
3523 handle
->addr
+= handle
->size
;
3524 handle
->size
= (PAGE_SIZE
<< page_order(buffer
)) - handle
->size
;
3527 lost_event
.header
.type
= PERF_RECORD_LOST
;
3528 lost_event
.header
.misc
= 0;
3529 lost_event
.id
= event
->id
;
3530 lost_event
.lost
= local_xchg(&buffer
->lost
, 0);
3532 perf_output_put(handle
, lost_event
);
3533 perf_event__output_id_sample(event
, handle
, &sample_data
);
3539 local_inc(&buffer
->lost
);
3540 perf_output_put_handle(handle
);
3547 void perf_output_end(struct perf_output_handle
*handle
)
3549 struct perf_event
*event
= handle
->event
;
3550 struct perf_buffer
*buffer
= handle
->buffer
;
3552 int wakeup_events
= event
->attr
.wakeup_events
;
3554 if (handle
->sample
&& wakeup_events
) {
3555 int events
= local_inc_return(&buffer
->events
);
3556 if (events
>= wakeup_events
) {
3557 local_sub(wakeup_events
, &buffer
->events
);
3558 local_inc(&buffer
->wakeup
);
3562 perf_output_put_handle(handle
);
3566 static void perf_output_read_one(struct perf_output_handle
*handle
,
3567 struct perf_event
*event
,
3568 u64 enabled
, u64 running
)
3570 u64 read_format
= event
->attr
.read_format
;
3574 values
[n
++] = perf_event_count(event
);
3575 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
3576 values
[n
++] = enabled
+
3577 atomic64_read(&event
->child_total_time_enabled
);
3579 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
3580 values
[n
++] = running
+
3581 atomic64_read(&event
->child_total_time_running
);
3583 if (read_format
& PERF_FORMAT_ID
)
3584 values
[n
++] = primary_event_id(event
);
3586 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3590 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3592 static void perf_output_read_group(struct perf_output_handle
*handle
,
3593 struct perf_event
*event
,
3594 u64 enabled
, u64 running
)
3596 struct perf_event
*leader
= event
->group_leader
, *sub
;
3597 u64 read_format
= event
->attr
.read_format
;
3601 values
[n
++] = 1 + leader
->nr_siblings
;
3603 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3604 values
[n
++] = enabled
;
3606 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3607 values
[n
++] = running
;
3609 if (leader
!= event
)
3610 leader
->pmu
->read(leader
);
3612 values
[n
++] = perf_event_count(leader
);
3613 if (read_format
& PERF_FORMAT_ID
)
3614 values
[n
++] = primary_event_id(leader
);
3616 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3618 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3622 sub
->pmu
->read(sub
);
3624 values
[n
++] = perf_event_count(sub
);
3625 if (read_format
& PERF_FORMAT_ID
)
3626 values
[n
++] = primary_event_id(sub
);
3628 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3632 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
3633 PERF_FORMAT_TOTAL_TIME_RUNNING)
3635 static void perf_output_read(struct perf_output_handle
*handle
,
3636 struct perf_event
*event
)
3638 u64 enabled
= 0, running
= 0, now
, ctx_time
;
3639 u64 read_format
= event
->attr
.read_format
;
3642 * compute total_time_enabled, total_time_running
3643 * based on snapshot values taken when the event
3644 * was last scheduled in.
3646 * we cannot simply called update_context_time()
3647 * because of locking issue as we are called in
3650 if (read_format
& PERF_FORMAT_TOTAL_TIMES
) {
3652 ctx_time
= event
->shadow_ctx_time
+ now
;
3653 enabled
= ctx_time
- event
->tstamp_enabled
;
3654 running
= ctx_time
- event
->tstamp_running
;
3657 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
3658 perf_output_read_group(handle
, event
, enabled
, running
);
3660 perf_output_read_one(handle
, event
, enabled
, running
);
3663 void perf_output_sample(struct perf_output_handle
*handle
,
3664 struct perf_event_header
*header
,
3665 struct perf_sample_data
*data
,
3666 struct perf_event
*event
)
3668 u64 sample_type
= data
->type
;
3670 perf_output_put(handle
, *header
);
3672 if (sample_type
& PERF_SAMPLE_IP
)
3673 perf_output_put(handle
, data
->ip
);
3675 if (sample_type
& PERF_SAMPLE_TID
)
3676 perf_output_put(handle
, data
->tid_entry
);
3678 if (sample_type
& PERF_SAMPLE_TIME
)
3679 perf_output_put(handle
, data
->time
);
3681 if (sample_type
& PERF_SAMPLE_ADDR
)
3682 perf_output_put(handle
, data
->addr
);
3684 if (sample_type
& PERF_SAMPLE_ID
)
3685 perf_output_put(handle
, data
->id
);
3687 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
3688 perf_output_put(handle
, data
->stream_id
);
3690 if (sample_type
& PERF_SAMPLE_CPU
)
3691 perf_output_put(handle
, data
->cpu_entry
);
3693 if (sample_type
& PERF_SAMPLE_PERIOD
)
3694 perf_output_put(handle
, data
->period
);
3696 if (sample_type
& PERF_SAMPLE_READ
)
3697 perf_output_read(handle
, event
);
3699 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3700 if (data
->callchain
) {
3703 if (data
->callchain
)
3704 size
+= data
->callchain
->nr
;
3706 size
*= sizeof(u64
);
3708 perf_output_copy(handle
, data
->callchain
, size
);
3711 perf_output_put(handle
, nr
);
3715 if (sample_type
& PERF_SAMPLE_RAW
) {
3717 perf_output_put(handle
, data
->raw
->size
);
3718 perf_output_copy(handle
, data
->raw
->data
,
3725 .size
= sizeof(u32
),
3728 perf_output_put(handle
, raw
);
3733 void perf_prepare_sample(struct perf_event_header
*header
,
3734 struct perf_sample_data
*data
,
3735 struct perf_event
*event
,
3736 struct pt_regs
*regs
)
3738 u64 sample_type
= event
->attr
.sample_type
;
3740 header
->type
= PERF_RECORD_SAMPLE
;
3741 header
->size
= sizeof(*header
) + event
->header_size
;
3744 header
->misc
|= perf_misc_flags(regs
);
3746 __perf_event_header__init_id(header
, data
, event
);
3748 if (sample_type
& PERF_SAMPLE_IP
)
3749 data
->ip
= perf_instruction_pointer(regs
);
3751 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3754 data
->callchain
= perf_callchain(regs
);
3756 if (data
->callchain
)
3757 size
+= data
->callchain
->nr
;
3759 header
->size
+= size
* sizeof(u64
);
3762 if (sample_type
& PERF_SAMPLE_RAW
) {
3763 int size
= sizeof(u32
);
3766 size
+= data
->raw
->size
;
3768 size
+= sizeof(u32
);
3770 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
3771 header
->size
+= size
;
3775 static void perf_event_output(struct perf_event
*event
, int nmi
,
3776 struct perf_sample_data
*data
,
3777 struct pt_regs
*regs
)
3779 struct perf_output_handle handle
;
3780 struct perf_event_header header
;
3782 /* protect the callchain buffers */
3785 perf_prepare_sample(&header
, data
, event
, regs
);
3787 if (perf_output_begin(&handle
, event
, header
.size
, nmi
, 1))
3790 perf_output_sample(&handle
, &header
, data
, event
);
3792 perf_output_end(&handle
);
3802 struct perf_read_event
{
3803 struct perf_event_header header
;
3810 perf_event_read_event(struct perf_event
*event
,
3811 struct task_struct
*task
)
3813 struct perf_output_handle handle
;
3814 struct perf_sample_data sample
;
3815 struct perf_read_event read_event
= {
3817 .type
= PERF_RECORD_READ
,
3819 .size
= sizeof(read_event
) + event
->read_size
,
3821 .pid
= perf_event_pid(event
, task
),
3822 .tid
= perf_event_tid(event
, task
),
3826 perf_event_header__init_id(&read_event
.header
, &sample
, event
);
3827 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
, 0, 0);
3831 perf_output_put(&handle
, read_event
);
3832 perf_output_read(&handle
, event
);
3833 perf_event__output_id_sample(event
, &handle
, &sample
);
3835 perf_output_end(&handle
);
3839 * task tracking -- fork/exit
3841 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
3844 struct perf_task_event
{
3845 struct task_struct
*task
;
3846 struct perf_event_context
*task_ctx
;
3849 struct perf_event_header header
;
3859 static void perf_event_task_output(struct perf_event
*event
,
3860 struct perf_task_event
*task_event
)
3862 struct perf_output_handle handle
;
3863 struct perf_sample_data sample
;
3864 struct task_struct
*task
= task_event
->task
;
3865 int ret
, size
= task_event
->event_id
.header
.size
;
3867 perf_event_header__init_id(&task_event
->event_id
.header
, &sample
, event
);
3869 ret
= perf_output_begin(&handle
, event
,
3870 task_event
->event_id
.header
.size
, 0, 0);
3874 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
3875 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
3877 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
3878 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
3880 perf_output_put(&handle
, task_event
->event_id
);
3882 perf_event__output_id_sample(event
, &handle
, &sample
);
3884 perf_output_end(&handle
);
3886 task_event
->event_id
.header
.size
= size
;
3889 static int perf_event_task_match(struct perf_event
*event
)
3891 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3894 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3897 if (event
->attr
.comm
|| event
->attr
.mmap
||
3898 event
->attr
.mmap_data
|| event
->attr
.task
)
3904 static void perf_event_task_ctx(struct perf_event_context
*ctx
,
3905 struct perf_task_event
*task_event
)
3907 struct perf_event
*event
;
3909 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3910 if (perf_event_task_match(event
))
3911 perf_event_task_output(event
, task_event
);
3915 static void perf_event_task_event(struct perf_task_event
*task_event
)
3917 struct perf_cpu_context
*cpuctx
;
3918 struct perf_event_context
*ctx
;
3923 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
3924 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
3925 if (cpuctx
->active_pmu
!= pmu
)
3927 perf_event_task_ctx(&cpuctx
->ctx
, task_event
);
3929 ctx
= task_event
->task_ctx
;
3931 ctxn
= pmu
->task_ctx_nr
;
3934 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
3937 perf_event_task_ctx(ctx
, task_event
);
3939 put_cpu_ptr(pmu
->pmu_cpu_context
);
3944 static void perf_event_task(struct task_struct
*task
,
3945 struct perf_event_context
*task_ctx
,
3948 struct perf_task_event task_event
;
3950 if (!atomic_read(&nr_comm_events
) &&
3951 !atomic_read(&nr_mmap_events
) &&
3952 !atomic_read(&nr_task_events
))
3955 task_event
= (struct perf_task_event
){
3957 .task_ctx
= task_ctx
,
3960 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
3962 .size
= sizeof(task_event
.event_id
),
3968 .time
= perf_clock(),
3972 perf_event_task_event(&task_event
);
3975 void perf_event_fork(struct task_struct
*task
)
3977 perf_event_task(task
, NULL
, 1);
3984 struct perf_comm_event
{
3985 struct task_struct
*task
;
3990 struct perf_event_header header
;
3997 static void perf_event_comm_output(struct perf_event
*event
,
3998 struct perf_comm_event
*comm_event
)
4000 struct perf_output_handle handle
;
4001 struct perf_sample_data sample
;
4002 int size
= comm_event
->event_id
.header
.size
;
4005 perf_event_header__init_id(&comm_event
->event_id
.header
, &sample
, event
);
4006 ret
= perf_output_begin(&handle
, event
,
4007 comm_event
->event_id
.header
.size
, 0, 0);
4012 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
4013 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
4015 perf_output_put(&handle
, comm_event
->event_id
);
4016 perf_output_copy(&handle
, comm_event
->comm
,
4017 comm_event
->comm_size
);
4019 perf_event__output_id_sample(event
, &handle
, &sample
);
4021 perf_output_end(&handle
);
4023 comm_event
->event_id
.header
.size
= size
;
4026 static int perf_event_comm_match(struct perf_event
*event
)
4028 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
4031 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
4034 if (event
->attr
.comm
)
4040 static void perf_event_comm_ctx(struct perf_event_context
*ctx
,
4041 struct perf_comm_event
*comm_event
)
4043 struct perf_event
*event
;
4045 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
4046 if (perf_event_comm_match(event
))
4047 perf_event_comm_output(event
, comm_event
);
4051 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
4053 struct perf_cpu_context
*cpuctx
;
4054 struct perf_event_context
*ctx
;
4055 char comm
[TASK_COMM_LEN
];
4060 memset(comm
, 0, sizeof(comm
));
4061 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
4062 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
4064 comm_event
->comm
= comm
;
4065 comm_event
->comm_size
= size
;
4067 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
4069 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
4070 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
4071 if (cpuctx
->active_pmu
!= pmu
)
4073 perf_event_comm_ctx(&cpuctx
->ctx
, comm_event
);
4075 ctxn
= pmu
->task_ctx_nr
;
4079 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
4081 perf_event_comm_ctx(ctx
, comm_event
);
4083 put_cpu_ptr(pmu
->pmu_cpu_context
);
4088 void perf_event_comm(struct task_struct
*task
)
4090 struct perf_comm_event comm_event
;
4091 struct perf_event_context
*ctx
;
4094 for_each_task_context_nr(ctxn
) {
4095 ctx
= task
->perf_event_ctxp
[ctxn
];
4099 perf_event_enable_on_exec(ctx
);
4102 if (!atomic_read(&nr_comm_events
))
4105 comm_event
= (struct perf_comm_event
){
4111 .type
= PERF_RECORD_COMM
,
4120 perf_event_comm_event(&comm_event
);
4127 struct perf_mmap_event
{
4128 struct vm_area_struct
*vma
;
4130 const char *file_name
;
4134 struct perf_event_header header
;
4144 static void perf_event_mmap_output(struct perf_event
*event
,
4145 struct perf_mmap_event
*mmap_event
)
4147 struct perf_output_handle handle
;
4148 struct perf_sample_data sample
;
4149 int size
= mmap_event
->event_id
.header
.size
;
4152 perf_event_header__init_id(&mmap_event
->event_id
.header
, &sample
, event
);
4153 ret
= perf_output_begin(&handle
, event
,
4154 mmap_event
->event_id
.header
.size
, 0, 0);
4158 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
4159 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
4161 perf_output_put(&handle
, mmap_event
->event_id
);
4162 perf_output_copy(&handle
, mmap_event
->file_name
,
4163 mmap_event
->file_size
);
4165 perf_event__output_id_sample(event
, &handle
, &sample
);
4167 perf_output_end(&handle
);
4169 mmap_event
->event_id
.header
.size
= size
;
4172 static int perf_event_mmap_match(struct perf_event
*event
,
4173 struct perf_mmap_event
*mmap_event
,
4176 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
4179 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
4182 if ((!executable
&& event
->attr
.mmap_data
) ||
4183 (executable
&& event
->attr
.mmap
))
4189 static void perf_event_mmap_ctx(struct perf_event_context
*ctx
,
4190 struct perf_mmap_event
*mmap_event
,
4193 struct perf_event
*event
;
4195 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
4196 if (perf_event_mmap_match(event
, mmap_event
, executable
))
4197 perf_event_mmap_output(event
, mmap_event
);
4201 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
4203 struct perf_cpu_context
*cpuctx
;
4204 struct perf_event_context
*ctx
;
4205 struct vm_area_struct
*vma
= mmap_event
->vma
;
4206 struct file
*file
= vma
->vm_file
;
4214 memset(tmp
, 0, sizeof(tmp
));
4218 * d_path works from the end of the buffer backwards, so we
4219 * need to add enough zero bytes after the string to handle
4220 * the 64bit alignment we do later.
4222 buf
= kzalloc(PATH_MAX
+ sizeof(u64
), GFP_KERNEL
);
4224 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
4227 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
4229 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
4233 if (arch_vma_name(mmap_event
->vma
)) {
4234 name
= strncpy(tmp
, arch_vma_name(mmap_event
->vma
),
4240 name
= strncpy(tmp
, "[vdso]", sizeof(tmp
));
4242 } else if (vma
->vm_start
<= vma
->vm_mm
->start_brk
&&
4243 vma
->vm_end
>= vma
->vm_mm
->brk
) {
4244 name
= strncpy(tmp
, "[heap]", sizeof(tmp
));
4246 } else if (vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
4247 vma
->vm_end
>= vma
->vm_mm
->start_stack
) {
4248 name
= strncpy(tmp
, "[stack]", sizeof(tmp
));
4252 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
4257 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
4259 mmap_event
->file_name
= name
;
4260 mmap_event
->file_size
= size
;
4262 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
4265 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
4266 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
4267 if (cpuctx
->active_pmu
!= pmu
)
4269 perf_event_mmap_ctx(&cpuctx
->ctx
, mmap_event
,
4270 vma
->vm_flags
& VM_EXEC
);
4272 ctxn
= pmu
->task_ctx_nr
;
4276 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
4278 perf_event_mmap_ctx(ctx
, mmap_event
,
4279 vma
->vm_flags
& VM_EXEC
);
4282 put_cpu_ptr(pmu
->pmu_cpu_context
);
4289 void perf_event_mmap(struct vm_area_struct
*vma
)
4291 struct perf_mmap_event mmap_event
;
4293 if (!atomic_read(&nr_mmap_events
))
4296 mmap_event
= (struct perf_mmap_event
){
4302 .type
= PERF_RECORD_MMAP
,
4303 .misc
= PERF_RECORD_MISC_USER
,
4308 .start
= vma
->vm_start
,
4309 .len
= vma
->vm_end
- vma
->vm_start
,
4310 .pgoff
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
,
4314 perf_event_mmap_event(&mmap_event
);
4318 * IRQ throttle logging
4321 static void perf_log_throttle(struct perf_event
*event
, int enable
)
4323 struct perf_output_handle handle
;
4324 struct perf_sample_data sample
;
4328 struct perf_event_header header
;
4332 } throttle_event
= {
4334 .type
= PERF_RECORD_THROTTLE
,
4336 .size
= sizeof(throttle_event
),
4338 .time
= perf_clock(),
4339 .id
= primary_event_id(event
),
4340 .stream_id
= event
->id
,
4344 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
4346 perf_event_header__init_id(&throttle_event
.header
, &sample
, event
);
4348 ret
= perf_output_begin(&handle
, event
,
4349 throttle_event
.header
.size
, 1, 0);
4353 perf_output_put(&handle
, throttle_event
);
4354 perf_event__output_id_sample(event
, &handle
, &sample
);
4355 perf_output_end(&handle
);
4359 * Generic event overflow handling, sampling.
4362 static int __perf_event_overflow(struct perf_event
*event
, int nmi
,
4363 int throttle
, struct perf_sample_data
*data
,
4364 struct pt_regs
*regs
)
4366 int events
= atomic_read(&event
->event_limit
);
4367 struct hw_perf_event
*hwc
= &event
->hw
;
4371 * Non-sampling counters might still use the PMI to fold short
4372 * hardware counters, ignore those.
4374 if (unlikely(!is_sampling_event(event
)))
4380 if (hwc
->interrupts
!= MAX_INTERRUPTS
) {
4382 if (HZ
* hwc
->interrupts
>
4383 (u64
)sysctl_perf_event_sample_rate
) {
4384 hwc
->interrupts
= MAX_INTERRUPTS
;
4385 perf_log_throttle(event
, 0);
4390 * Keep re-disabling events even though on the previous
4391 * pass we disabled it - just in case we raced with a
4392 * sched-in and the event got enabled again:
4398 if (event
->attr
.freq
) {
4399 u64 now
= perf_clock();
4400 s64 delta
= now
- hwc
->freq_time_stamp
;
4402 hwc
->freq_time_stamp
= now
;
4404 if (delta
> 0 && delta
< 2*TICK_NSEC
)
4405 perf_adjust_period(event
, delta
, hwc
->last_period
);
4409 * XXX event_limit might not quite work as expected on inherited
4413 event
->pending_kill
= POLL_IN
;
4414 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
4416 event
->pending_kill
= POLL_HUP
;
4418 event
->pending_disable
= 1;
4419 irq_work_queue(&event
->pending
);
4421 perf_event_disable(event
);
4424 if (event
->overflow_handler
)
4425 event
->overflow_handler(event
, nmi
, data
, regs
);
4427 perf_event_output(event
, nmi
, data
, regs
);
4432 int perf_event_overflow(struct perf_event
*event
, int nmi
,
4433 struct perf_sample_data
*data
,
4434 struct pt_regs
*regs
)
4436 return __perf_event_overflow(event
, nmi
, 1, data
, regs
);
4440 * Generic software event infrastructure
4443 struct swevent_htable
{
4444 struct swevent_hlist
*swevent_hlist
;
4445 struct mutex hlist_mutex
;
4448 /* Recursion avoidance in each contexts */
4449 int recursion
[PERF_NR_CONTEXTS
];
4452 static DEFINE_PER_CPU(struct swevent_htable
, swevent_htable
);
4455 * We directly increment event->count and keep a second value in
4456 * event->hw.period_left to count intervals. This period event
4457 * is kept in the range [-sample_period, 0] so that we can use the
4461 static u64
perf_swevent_set_period(struct perf_event
*event
)
4463 struct hw_perf_event
*hwc
= &event
->hw
;
4464 u64 period
= hwc
->last_period
;
4468 hwc
->last_period
= hwc
->sample_period
;
4471 old
= val
= local64_read(&hwc
->period_left
);
4475 nr
= div64_u64(period
+ val
, period
);
4476 offset
= nr
* period
;
4478 if (local64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
4484 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
4485 int nmi
, struct perf_sample_data
*data
,
4486 struct pt_regs
*regs
)
4488 struct hw_perf_event
*hwc
= &event
->hw
;
4491 data
->period
= event
->hw
.last_period
;
4493 overflow
= perf_swevent_set_period(event
);
4495 if (hwc
->interrupts
== MAX_INTERRUPTS
)
4498 for (; overflow
; overflow
--) {
4499 if (__perf_event_overflow(event
, nmi
, throttle
,
4502 * We inhibit the overflow from happening when
4503 * hwc->interrupts == MAX_INTERRUPTS.
4511 static void perf_swevent_event(struct perf_event
*event
, u64 nr
,
4512 int nmi
, struct perf_sample_data
*data
,
4513 struct pt_regs
*regs
)
4515 struct hw_perf_event
*hwc
= &event
->hw
;
4517 local64_add(nr
, &event
->count
);
4522 if (!is_sampling_event(event
))
4525 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
4526 return perf_swevent_overflow(event
, 1, nmi
, data
, regs
);
4528 if (local64_add_negative(nr
, &hwc
->period_left
))
4531 perf_swevent_overflow(event
, 0, nmi
, data
, regs
);
4534 static int perf_exclude_event(struct perf_event
*event
,
4535 struct pt_regs
*regs
)
4537 if (event
->hw
.state
& PERF_HES_STOPPED
)
4541 if (event
->attr
.exclude_user
&& user_mode(regs
))
4544 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
4551 static int perf_swevent_match(struct perf_event
*event
,
4552 enum perf_type_id type
,
4554 struct perf_sample_data
*data
,
4555 struct pt_regs
*regs
)
4557 if (event
->attr
.type
!= type
)
4560 if (event
->attr
.config
!= event_id
)
4563 if (perf_exclude_event(event
, regs
))
4569 static inline u64
swevent_hash(u64 type
, u32 event_id
)
4571 u64 val
= event_id
| (type
<< 32);
4573 return hash_64(val
, SWEVENT_HLIST_BITS
);
4576 static inline struct hlist_head
*
4577 __find_swevent_head(struct swevent_hlist
*hlist
, u64 type
, u32 event_id
)
4579 u64 hash
= swevent_hash(type
, event_id
);
4581 return &hlist
->heads
[hash
];
4584 /* For the read side: events when they trigger */
4585 static inline struct hlist_head
*
4586 find_swevent_head_rcu(struct swevent_htable
*swhash
, u64 type
, u32 event_id
)
4588 struct swevent_hlist
*hlist
;
4590 hlist
= rcu_dereference(swhash
->swevent_hlist
);
4594 return __find_swevent_head(hlist
, type
, event_id
);
4597 /* For the event head insertion and removal in the hlist */
4598 static inline struct hlist_head
*
4599 find_swevent_head(struct swevent_htable
*swhash
, struct perf_event
*event
)
4601 struct swevent_hlist
*hlist
;
4602 u32 event_id
= event
->attr
.config
;
4603 u64 type
= event
->attr
.type
;
4606 * Event scheduling is always serialized against hlist allocation
4607 * and release. Which makes the protected version suitable here.
4608 * The context lock guarantees that.
4610 hlist
= rcu_dereference_protected(swhash
->swevent_hlist
,
4611 lockdep_is_held(&event
->ctx
->lock
));
4615 return __find_swevent_head(hlist
, type
, event_id
);
4618 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
4620 struct perf_sample_data
*data
,
4621 struct pt_regs
*regs
)
4623 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
4624 struct perf_event
*event
;
4625 struct hlist_node
*node
;
4626 struct hlist_head
*head
;
4629 head
= find_swevent_head_rcu(swhash
, type
, event_id
);
4633 hlist_for_each_entry_rcu(event
, node
, head
, hlist_entry
) {
4634 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
4635 perf_swevent_event(event
, nr
, nmi
, data
, regs
);
4641 int perf_swevent_get_recursion_context(void)
4643 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
4645 return get_recursion_context(swhash
->recursion
);
4647 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
4649 void inline perf_swevent_put_recursion_context(int rctx
)
4651 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
4653 put_recursion_context(swhash
->recursion
, rctx
);
4656 void __perf_sw_event(u32 event_id
, u64 nr
, int nmi
,
4657 struct pt_regs
*regs
, u64 addr
)
4659 struct perf_sample_data data
;
4662 preempt_disable_notrace();
4663 rctx
= perf_swevent_get_recursion_context();
4667 perf_sample_data_init(&data
, addr
);
4669 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, nmi
, &data
, regs
);
4671 perf_swevent_put_recursion_context(rctx
);
4672 preempt_enable_notrace();
4675 static void perf_swevent_read(struct perf_event
*event
)
4679 static int perf_swevent_add(struct perf_event
*event
, int flags
)
4681 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
4682 struct hw_perf_event
*hwc
= &event
->hw
;
4683 struct hlist_head
*head
;
4685 if (is_sampling_event(event
)) {
4686 hwc
->last_period
= hwc
->sample_period
;
4687 perf_swevent_set_period(event
);
4690 hwc
->state
= !(flags
& PERF_EF_START
);
4692 head
= find_swevent_head(swhash
, event
);
4693 if (WARN_ON_ONCE(!head
))
4696 hlist_add_head_rcu(&event
->hlist_entry
, head
);
4701 static void perf_swevent_del(struct perf_event
*event
, int flags
)
4703 hlist_del_rcu(&event
->hlist_entry
);
4706 static void perf_swevent_start(struct perf_event
*event
, int flags
)
4708 event
->hw
.state
= 0;
4711 static void perf_swevent_stop(struct perf_event
*event
, int flags
)
4713 event
->hw
.state
= PERF_HES_STOPPED
;
4716 /* Deref the hlist from the update side */
4717 static inline struct swevent_hlist
*
4718 swevent_hlist_deref(struct swevent_htable
*swhash
)
4720 return rcu_dereference_protected(swhash
->swevent_hlist
,
4721 lockdep_is_held(&swhash
->hlist_mutex
));
4724 static void swevent_hlist_release_rcu(struct rcu_head
*rcu_head
)
4726 struct swevent_hlist
*hlist
;
4728 hlist
= container_of(rcu_head
, struct swevent_hlist
, rcu_head
);
4732 static void swevent_hlist_release(struct swevent_htable
*swhash
)
4734 struct swevent_hlist
*hlist
= swevent_hlist_deref(swhash
);
4739 rcu_assign_pointer(swhash
->swevent_hlist
, NULL
);
4740 call_rcu(&hlist
->rcu_head
, swevent_hlist_release_rcu
);
4743 static void swevent_hlist_put_cpu(struct perf_event
*event
, int cpu
)
4745 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
4747 mutex_lock(&swhash
->hlist_mutex
);
4749 if (!--swhash
->hlist_refcount
)
4750 swevent_hlist_release(swhash
);
4752 mutex_unlock(&swhash
->hlist_mutex
);
4755 static void swevent_hlist_put(struct perf_event
*event
)
4759 if (event
->cpu
!= -1) {
4760 swevent_hlist_put_cpu(event
, event
->cpu
);
4764 for_each_possible_cpu(cpu
)
4765 swevent_hlist_put_cpu(event
, cpu
);
4768 static int swevent_hlist_get_cpu(struct perf_event
*event
, int cpu
)
4770 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
4773 mutex_lock(&swhash
->hlist_mutex
);
4775 if (!swevent_hlist_deref(swhash
) && cpu_online(cpu
)) {
4776 struct swevent_hlist
*hlist
;
4778 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
4783 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
4785 swhash
->hlist_refcount
++;
4787 mutex_unlock(&swhash
->hlist_mutex
);
4792 static int swevent_hlist_get(struct perf_event
*event
)
4795 int cpu
, failed_cpu
;
4797 if (event
->cpu
!= -1)
4798 return swevent_hlist_get_cpu(event
, event
->cpu
);
4801 for_each_possible_cpu(cpu
) {
4802 err
= swevent_hlist_get_cpu(event
, cpu
);
4812 for_each_possible_cpu(cpu
) {
4813 if (cpu
== failed_cpu
)
4815 swevent_hlist_put_cpu(event
, cpu
);
4822 atomic_t perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
4824 static void sw_perf_event_destroy(struct perf_event
*event
)
4826 u64 event_id
= event
->attr
.config
;
4828 WARN_ON(event
->parent
);
4830 jump_label_dec(&perf_swevent_enabled
[event_id
]);
4831 swevent_hlist_put(event
);
4834 static int perf_swevent_init(struct perf_event
*event
)
4836 int event_id
= event
->attr
.config
;
4838 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
4842 case PERF_COUNT_SW_CPU_CLOCK
:
4843 case PERF_COUNT_SW_TASK_CLOCK
:
4850 if (event_id
>= PERF_COUNT_SW_MAX
)
4853 if (!event
->parent
) {
4856 err
= swevent_hlist_get(event
);
4860 jump_label_inc(&perf_swevent_enabled
[event_id
]);
4861 event
->destroy
= sw_perf_event_destroy
;
4867 static struct pmu perf_swevent
= {
4868 .task_ctx_nr
= perf_sw_context
,
4870 .event_init
= perf_swevent_init
,
4871 .add
= perf_swevent_add
,
4872 .del
= perf_swevent_del
,
4873 .start
= perf_swevent_start
,
4874 .stop
= perf_swevent_stop
,
4875 .read
= perf_swevent_read
,
4878 #ifdef CONFIG_EVENT_TRACING
4880 static int perf_tp_filter_match(struct perf_event
*event
,
4881 struct perf_sample_data
*data
)
4883 void *record
= data
->raw
->data
;
4885 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
4890 static int perf_tp_event_match(struct perf_event
*event
,
4891 struct perf_sample_data
*data
,
4892 struct pt_regs
*regs
)
4895 * All tracepoints are from kernel-space.
4897 if (event
->attr
.exclude_kernel
)
4900 if (!perf_tp_filter_match(event
, data
))
4906 void perf_tp_event(u64 addr
, u64 count
, void *record
, int entry_size
,
4907 struct pt_regs
*regs
, struct hlist_head
*head
, int rctx
)
4909 struct perf_sample_data data
;
4910 struct perf_event
*event
;
4911 struct hlist_node
*node
;
4913 struct perf_raw_record raw
= {
4918 perf_sample_data_init(&data
, addr
);
4921 hlist_for_each_entry_rcu(event
, node
, head
, hlist_entry
) {
4922 if (perf_tp_event_match(event
, &data
, regs
))
4923 perf_swevent_event(event
, count
, 1, &data
, regs
);
4926 perf_swevent_put_recursion_context(rctx
);
4928 EXPORT_SYMBOL_GPL(perf_tp_event
);
4930 static void tp_perf_event_destroy(struct perf_event
*event
)
4932 perf_trace_destroy(event
);
4935 static int perf_tp_event_init(struct perf_event
*event
)
4939 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
4942 err
= perf_trace_init(event
);
4946 event
->destroy
= tp_perf_event_destroy
;
4951 static struct pmu perf_tracepoint
= {
4952 .task_ctx_nr
= perf_sw_context
,
4954 .event_init
= perf_tp_event_init
,
4955 .add
= perf_trace_add
,
4956 .del
= perf_trace_del
,
4957 .start
= perf_swevent_start
,
4958 .stop
= perf_swevent_stop
,
4959 .read
= perf_swevent_read
,
4962 static inline void perf_tp_register(void)
4964 perf_pmu_register(&perf_tracepoint
);
4967 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
4972 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
4975 filter_str
= strndup_user(arg
, PAGE_SIZE
);
4976 if (IS_ERR(filter_str
))
4977 return PTR_ERR(filter_str
);
4979 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
, filter_str
);
4985 static void perf_event_free_filter(struct perf_event
*event
)
4987 ftrace_profile_free_filter(event
);
4992 static inline void perf_tp_register(void)
4996 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
5001 static void perf_event_free_filter(struct perf_event
*event
)
5005 #endif /* CONFIG_EVENT_TRACING */
5007 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5008 void perf_bp_event(struct perf_event
*bp
, void *data
)
5010 struct perf_sample_data sample
;
5011 struct pt_regs
*regs
= data
;
5013 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
);
5015 if (!bp
->hw
.state
&& !perf_exclude_event(bp
, regs
))
5016 perf_swevent_event(bp
, 1, 1, &sample
, regs
);
5021 * hrtimer based swevent callback
5024 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
5026 enum hrtimer_restart ret
= HRTIMER_RESTART
;
5027 struct perf_sample_data data
;
5028 struct pt_regs
*regs
;
5029 struct perf_event
*event
;
5032 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
5033 event
->pmu
->read(event
);
5035 perf_sample_data_init(&data
, 0);
5036 data
.period
= event
->hw
.last_period
;
5037 regs
= get_irq_regs();
5039 if (regs
&& !perf_exclude_event(event
, regs
)) {
5040 if (!(event
->attr
.exclude_idle
&& current
->pid
== 0))
5041 if (perf_event_overflow(event
, 0, &data
, regs
))
5042 ret
= HRTIMER_NORESTART
;
5045 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
5046 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
5051 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
5053 struct hw_perf_event
*hwc
= &event
->hw
;
5056 if (!is_sampling_event(event
))
5059 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
5060 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
5062 period
= local64_read(&hwc
->period_left
);
5067 local64_set(&hwc
->period_left
, 0);
5069 period
= max_t(u64
, 10000, hwc
->sample_period
);
5071 __hrtimer_start_range_ns(&hwc
->hrtimer
,
5072 ns_to_ktime(period
), 0,
5073 HRTIMER_MODE_REL_PINNED
, 0);
5076 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
5078 struct hw_perf_event
*hwc
= &event
->hw
;
5080 if (is_sampling_event(event
)) {
5081 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
5082 local64_set(&hwc
->period_left
, ktime_to_ns(remaining
));
5084 hrtimer_cancel(&hwc
->hrtimer
);
5089 * Software event: cpu wall time clock
5092 static void cpu_clock_event_update(struct perf_event
*event
)
5097 now
= local_clock();
5098 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
5099 local64_add(now
- prev
, &event
->count
);
5102 static void cpu_clock_event_start(struct perf_event
*event
, int flags
)
5104 local64_set(&event
->hw
.prev_count
, local_clock());
5105 perf_swevent_start_hrtimer(event
);
5108 static void cpu_clock_event_stop(struct perf_event
*event
, int flags
)
5110 perf_swevent_cancel_hrtimer(event
);
5111 cpu_clock_event_update(event
);
5114 static int cpu_clock_event_add(struct perf_event
*event
, int flags
)
5116 if (flags
& PERF_EF_START
)
5117 cpu_clock_event_start(event
, flags
);
5122 static void cpu_clock_event_del(struct perf_event
*event
, int flags
)
5124 cpu_clock_event_stop(event
, flags
);
5127 static void cpu_clock_event_read(struct perf_event
*event
)
5129 cpu_clock_event_update(event
);
5132 static int cpu_clock_event_init(struct perf_event
*event
)
5134 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
5137 if (event
->attr
.config
!= PERF_COUNT_SW_CPU_CLOCK
)
5143 static struct pmu perf_cpu_clock
= {
5144 .task_ctx_nr
= perf_sw_context
,
5146 .event_init
= cpu_clock_event_init
,
5147 .add
= cpu_clock_event_add
,
5148 .del
= cpu_clock_event_del
,
5149 .start
= cpu_clock_event_start
,
5150 .stop
= cpu_clock_event_stop
,
5151 .read
= cpu_clock_event_read
,
5155 * Software event: task time clock
5158 static void task_clock_event_update(struct perf_event
*event
, u64 now
)
5163 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
5165 local64_add(delta
, &event
->count
);
5168 static void task_clock_event_start(struct perf_event
*event
, int flags
)
5170 local64_set(&event
->hw
.prev_count
, event
->ctx
->time
);
5171 perf_swevent_start_hrtimer(event
);
5174 static void task_clock_event_stop(struct perf_event
*event
, int flags
)
5176 perf_swevent_cancel_hrtimer(event
);
5177 task_clock_event_update(event
, event
->ctx
->time
);
5180 static int task_clock_event_add(struct perf_event
*event
, int flags
)
5182 if (flags
& PERF_EF_START
)
5183 task_clock_event_start(event
, flags
);
5188 static void task_clock_event_del(struct perf_event
*event
, int flags
)
5190 task_clock_event_stop(event
, PERF_EF_UPDATE
);
5193 static void task_clock_event_read(struct perf_event
*event
)
5198 update_context_time(event
->ctx
);
5199 time
= event
->ctx
->time
;
5201 u64 now
= perf_clock();
5202 u64 delta
= now
- event
->ctx
->timestamp
;
5203 time
= event
->ctx
->time
+ delta
;
5206 task_clock_event_update(event
, time
);
5209 static int task_clock_event_init(struct perf_event
*event
)
5211 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
5214 if (event
->attr
.config
!= PERF_COUNT_SW_TASK_CLOCK
)
5220 static struct pmu perf_task_clock
= {
5221 .task_ctx_nr
= perf_sw_context
,
5223 .event_init
= task_clock_event_init
,
5224 .add
= task_clock_event_add
,
5225 .del
= task_clock_event_del
,
5226 .start
= task_clock_event_start
,
5227 .stop
= task_clock_event_stop
,
5228 .read
= task_clock_event_read
,
5231 static void perf_pmu_nop_void(struct pmu
*pmu
)
5235 static int perf_pmu_nop_int(struct pmu
*pmu
)
5240 static void perf_pmu_start_txn(struct pmu
*pmu
)
5242 perf_pmu_disable(pmu
);
5245 static int perf_pmu_commit_txn(struct pmu
*pmu
)
5247 perf_pmu_enable(pmu
);
5251 static void perf_pmu_cancel_txn(struct pmu
*pmu
)
5253 perf_pmu_enable(pmu
);
5257 * Ensures all contexts with the same task_ctx_nr have the same
5258 * pmu_cpu_context too.
5260 static void *find_pmu_context(int ctxn
)
5267 list_for_each_entry(pmu
, &pmus
, entry
) {
5268 if (pmu
->task_ctx_nr
== ctxn
)
5269 return pmu
->pmu_cpu_context
;
5275 static void update_pmu_context(struct pmu
*pmu
, struct pmu
*old_pmu
)
5279 for_each_possible_cpu(cpu
) {
5280 struct perf_cpu_context
*cpuctx
;
5282 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
5284 if (cpuctx
->active_pmu
== old_pmu
)
5285 cpuctx
->active_pmu
= pmu
;
5289 static void free_pmu_context(struct pmu
*pmu
)
5293 mutex_lock(&pmus_lock
);
5295 * Like a real lame refcount.
5297 list_for_each_entry(i
, &pmus
, entry
) {
5298 if (i
->pmu_cpu_context
== pmu
->pmu_cpu_context
) {
5299 update_pmu_context(i
, pmu
);
5304 free_percpu(pmu
->pmu_cpu_context
);
5306 mutex_unlock(&pmus_lock
);
5309 int perf_pmu_register(struct pmu
*pmu
)
5313 mutex_lock(&pmus_lock
);
5315 pmu
->pmu_disable_count
= alloc_percpu(int);
5316 if (!pmu
->pmu_disable_count
)
5319 pmu
->pmu_cpu_context
= find_pmu_context(pmu
->task_ctx_nr
);
5320 if (pmu
->pmu_cpu_context
)
5321 goto got_cpu_context
;
5323 pmu
->pmu_cpu_context
= alloc_percpu(struct perf_cpu_context
);
5324 if (!pmu
->pmu_cpu_context
)
5327 for_each_possible_cpu(cpu
) {
5328 struct perf_cpu_context
*cpuctx
;
5330 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
5331 __perf_event_init_context(&cpuctx
->ctx
);
5332 cpuctx
->ctx
.type
= cpu_context
;
5333 cpuctx
->ctx
.pmu
= pmu
;
5334 cpuctx
->jiffies_interval
= 1;
5335 INIT_LIST_HEAD(&cpuctx
->rotation_list
);
5336 cpuctx
->active_pmu
= pmu
;
5340 if (!pmu
->start_txn
) {
5341 if (pmu
->pmu_enable
) {
5343 * If we have pmu_enable/pmu_disable calls, install
5344 * transaction stubs that use that to try and batch
5345 * hardware accesses.
5347 pmu
->start_txn
= perf_pmu_start_txn
;
5348 pmu
->commit_txn
= perf_pmu_commit_txn
;
5349 pmu
->cancel_txn
= perf_pmu_cancel_txn
;
5351 pmu
->start_txn
= perf_pmu_nop_void
;
5352 pmu
->commit_txn
= perf_pmu_nop_int
;
5353 pmu
->cancel_txn
= perf_pmu_nop_void
;
5357 if (!pmu
->pmu_enable
) {
5358 pmu
->pmu_enable
= perf_pmu_nop_void
;
5359 pmu
->pmu_disable
= perf_pmu_nop_void
;
5362 list_add_rcu(&pmu
->entry
, &pmus
);
5365 mutex_unlock(&pmus_lock
);
5370 free_percpu(pmu
->pmu_disable_count
);
5374 void perf_pmu_unregister(struct pmu
*pmu
)
5376 mutex_lock(&pmus_lock
);
5377 list_del_rcu(&pmu
->entry
);
5378 mutex_unlock(&pmus_lock
);
5381 * We dereference the pmu list under both SRCU and regular RCU, so
5382 * synchronize against both of those.
5384 synchronize_srcu(&pmus_srcu
);
5387 free_percpu(pmu
->pmu_disable_count
);
5388 free_pmu_context(pmu
);
5391 struct pmu
*perf_init_event(struct perf_event
*event
)
5393 struct pmu
*pmu
= NULL
;
5396 idx
= srcu_read_lock(&pmus_srcu
);
5397 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
5398 int ret
= pmu
->event_init(event
);
5402 if (ret
!= -ENOENT
) {
5407 pmu
= ERR_PTR(-ENOENT
);
5409 srcu_read_unlock(&pmus_srcu
, idx
);
5415 * Allocate and initialize a event structure
5417 static struct perf_event
*
5418 perf_event_alloc(struct perf_event_attr
*attr
, int cpu
,
5419 struct task_struct
*task
,
5420 struct perf_event
*group_leader
,
5421 struct perf_event
*parent_event
,
5422 perf_overflow_handler_t overflow_handler
)
5425 struct perf_event
*event
;
5426 struct hw_perf_event
*hwc
;
5429 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
5431 return ERR_PTR(-ENOMEM
);
5434 * Single events are their own group leaders, with an
5435 * empty sibling list:
5438 group_leader
= event
;
5440 mutex_init(&event
->child_mutex
);
5441 INIT_LIST_HEAD(&event
->child_list
);
5443 INIT_LIST_HEAD(&event
->group_entry
);
5444 INIT_LIST_HEAD(&event
->event_entry
);
5445 INIT_LIST_HEAD(&event
->sibling_list
);
5446 init_waitqueue_head(&event
->waitq
);
5447 init_irq_work(&event
->pending
, perf_pending_event
);
5449 mutex_init(&event
->mmap_mutex
);
5452 event
->attr
= *attr
;
5453 event
->group_leader
= group_leader
;
5457 event
->parent
= parent_event
;
5459 event
->ns
= get_pid_ns(current
->nsproxy
->pid_ns
);
5460 event
->id
= atomic64_inc_return(&perf_event_id
);
5462 event
->state
= PERF_EVENT_STATE_INACTIVE
;
5465 event
->attach_state
= PERF_ATTACH_TASK
;
5466 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5468 * hw_breakpoint is a bit difficult here..
5470 if (attr
->type
== PERF_TYPE_BREAKPOINT
)
5471 event
->hw
.bp_target
= task
;
5475 if (!overflow_handler
&& parent_event
)
5476 overflow_handler
= parent_event
->overflow_handler
;
5478 event
->overflow_handler
= overflow_handler
;
5481 event
->state
= PERF_EVENT_STATE_OFF
;
5486 hwc
->sample_period
= attr
->sample_period
;
5487 if (attr
->freq
&& attr
->sample_freq
)
5488 hwc
->sample_period
= 1;
5489 hwc
->last_period
= hwc
->sample_period
;
5491 local64_set(&hwc
->period_left
, hwc
->sample_period
);
5494 * we currently do not support PERF_FORMAT_GROUP on inherited events
5496 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
5499 pmu
= perf_init_event(event
);
5505 else if (IS_ERR(pmu
))
5510 put_pid_ns(event
->ns
);
5512 return ERR_PTR(err
);
5517 if (!event
->parent
) {
5518 if (event
->attach_state
& PERF_ATTACH_TASK
)
5519 jump_label_inc(&perf_task_events
);
5520 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
5521 atomic_inc(&nr_mmap_events
);
5522 if (event
->attr
.comm
)
5523 atomic_inc(&nr_comm_events
);
5524 if (event
->attr
.task
)
5525 atomic_inc(&nr_task_events
);
5526 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
5527 err
= get_callchain_buffers();
5530 return ERR_PTR(err
);
5538 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
5539 struct perf_event_attr
*attr
)
5544 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
5548 * zero the full structure, so that a short copy will be nice.
5550 memset(attr
, 0, sizeof(*attr
));
5552 ret
= get_user(size
, &uattr
->size
);
5556 if (size
> PAGE_SIZE
) /* silly large */
5559 if (!size
) /* abi compat */
5560 size
= PERF_ATTR_SIZE_VER0
;
5562 if (size
< PERF_ATTR_SIZE_VER0
)
5566 * If we're handed a bigger struct than we know of,
5567 * ensure all the unknown bits are 0 - i.e. new
5568 * user-space does not rely on any kernel feature
5569 * extensions we dont know about yet.
5571 if (size
> sizeof(*attr
)) {
5572 unsigned char __user
*addr
;
5573 unsigned char __user
*end
;
5576 addr
= (void __user
*)uattr
+ sizeof(*attr
);
5577 end
= (void __user
*)uattr
+ size
;
5579 for (; addr
< end
; addr
++) {
5580 ret
= get_user(val
, addr
);
5586 size
= sizeof(*attr
);
5589 ret
= copy_from_user(attr
, uattr
, size
);
5594 * If the type exists, the corresponding creation will verify
5597 if (attr
->type
>= PERF_TYPE_MAX
)
5600 if (attr
->__reserved_1
)
5603 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
5606 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
5613 put_user(sizeof(*attr
), &uattr
->size
);
5619 perf_event_set_output(struct perf_event
*event
, struct perf_event
*output_event
)
5621 struct perf_buffer
*buffer
= NULL
, *old_buffer
= NULL
;
5627 /* don't allow circular references */
5628 if (event
== output_event
)
5632 * Don't allow cross-cpu buffers
5634 if (output_event
->cpu
!= event
->cpu
)
5638 * If its not a per-cpu buffer, it must be the same task.
5640 if (output_event
->cpu
== -1 && output_event
->ctx
!= event
->ctx
)
5644 mutex_lock(&event
->mmap_mutex
);
5645 /* Can't redirect output if we've got an active mmap() */
5646 if (atomic_read(&event
->mmap_count
))
5650 /* get the buffer we want to redirect to */
5651 buffer
= perf_buffer_get(output_event
);
5656 old_buffer
= event
->buffer
;
5657 rcu_assign_pointer(event
->buffer
, buffer
);
5660 mutex_unlock(&event
->mmap_mutex
);
5663 perf_buffer_put(old_buffer
);
5669 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5671 * @attr_uptr: event_id type attributes for monitoring/sampling
5674 * @group_fd: group leader event fd
5676 SYSCALL_DEFINE5(perf_event_open
,
5677 struct perf_event_attr __user
*, attr_uptr
,
5678 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
5680 struct perf_event
*group_leader
= NULL
, *output_event
= NULL
;
5681 struct perf_event
*event
, *sibling
;
5682 struct perf_event_attr attr
;
5683 struct perf_event_context
*ctx
;
5684 struct file
*event_file
= NULL
;
5685 struct file
*group_file
= NULL
;
5686 struct task_struct
*task
= NULL
;
5690 int fput_needed
= 0;
5693 /* for future expandability... */
5694 if (flags
& ~(PERF_FLAG_FD_NO_GROUP
| PERF_FLAG_FD_OUTPUT
))
5697 err
= perf_copy_attr(attr_uptr
, &attr
);
5701 if (!attr
.exclude_kernel
) {
5702 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
5707 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
5711 event_fd
= get_unused_fd_flags(O_RDWR
);
5715 if (group_fd
!= -1) {
5716 group_leader
= perf_fget_light(group_fd
, &fput_needed
);
5717 if (IS_ERR(group_leader
)) {
5718 err
= PTR_ERR(group_leader
);
5721 group_file
= group_leader
->filp
;
5722 if (flags
& PERF_FLAG_FD_OUTPUT
)
5723 output_event
= group_leader
;
5724 if (flags
& PERF_FLAG_FD_NO_GROUP
)
5725 group_leader
= NULL
;
5729 task
= find_lively_task_by_vpid(pid
);
5731 err
= PTR_ERR(task
);
5736 event
= perf_event_alloc(&attr
, cpu
, task
, group_leader
, NULL
, NULL
);
5737 if (IS_ERR(event
)) {
5738 err
= PTR_ERR(event
);
5743 * Special case software events and allow them to be part of
5744 * any hardware group.
5749 (is_software_event(event
) != is_software_event(group_leader
))) {
5750 if (is_software_event(event
)) {
5752 * If event and group_leader are not both a software
5753 * event, and event is, then group leader is not.
5755 * Allow the addition of software events to !software
5756 * groups, this is safe because software events never
5759 pmu
= group_leader
->pmu
;
5760 } else if (is_software_event(group_leader
) &&
5761 (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
)) {
5763 * In case the group is a pure software group, and we
5764 * try to add a hardware event, move the whole group to
5765 * the hardware context.
5772 * Get the target context (task or percpu):
5774 ctx
= find_get_context(pmu
, task
, cpu
);
5781 * Look up the group leader (we will attach this event to it):
5787 * Do not allow a recursive hierarchy (this new sibling
5788 * becoming part of another group-sibling):
5790 if (group_leader
->group_leader
!= group_leader
)
5793 * Do not allow to attach to a group in a different
5794 * task or CPU context:
5797 if (group_leader
->ctx
->type
!= ctx
->type
)
5800 if (group_leader
->ctx
!= ctx
)
5805 * Only a group leader can be exclusive or pinned
5807 if (attr
.exclusive
|| attr
.pinned
)
5812 err
= perf_event_set_output(event
, output_event
);
5817 event_file
= anon_inode_getfile("[perf_event]", &perf_fops
, event
, O_RDWR
);
5818 if (IS_ERR(event_file
)) {
5819 err
= PTR_ERR(event_file
);
5824 struct perf_event_context
*gctx
= group_leader
->ctx
;
5826 mutex_lock(&gctx
->mutex
);
5827 perf_event_remove_from_context(group_leader
);
5828 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
5830 perf_event_remove_from_context(sibling
);
5833 mutex_unlock(&gctx
->mutex
);
5837 event
->filp
= event_file
;
5838 WARN_ON_ONCE(ctx
->parent_ctx
);
5839 mutex_lock(&ctx
->mutex
);
5842 perf_install_in_context(ctx
, group_leader
, cpu
);
5844 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
5846 perf_install_in_context(ctx
, sibling
, cpu
);
5851 perf_install_in_context(ctx
, event
, cpu
);
5853 mutex_unlock(&ctx
->mutex
);
5855 event
->owner
= current
;
5857 mutex_lock(¤t
->perf_event_mutex
);
5858 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
5859 mutex_unlock(¤t
->perf_event_mutex
);
5862 * Precalculate sample_data sizes
5864 perf_event__header_size(event
);
5865 perf_event__id_header_size(event
);
5868 * Drop the reference on the group_event after placing the
5869 * new event on the sibling_list. This ensures destruction
5870 * of the group leader will find the pointer to itself in
5871 * perf_group_detach().
5873 fput_light(group_file
, fput_needed
);
5874 fd_install(event_fd
, event_file
);
5883 put_task_struct(task
);
5885 fput_light(group_file
, fput_needed
);
5887 put_unused_fd(event_fd
);
5892 * perf_event_create_kernel_counter
5894 * @attr: attributes of the counter to create
5895 * @cpu: cpu in which the counter is bound
5896 * @task: task to profile (NULL for percpu)
5899 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
5900 struct task_struct
*task
,
5901 perf_overflow_handler_t overflow_handler
)
5903 struct perf_event_context
*ctx
;
5904 struct perf_event
*event
;
5908 * Get the target context (task or percpu):
5911 event
= perf_event_alloc(attr
, cpu
, task
, NULL
, NULL
, overflow_handler
);
5912 if (IS_ERR(event
)) {
5913 err
= PTR_ERR(event
);
5917 ctx
= find_get_context(event
->pmu
, task
, cpu
);
5924 WARN_ON_ONCE(ctx
->parent_ctx
);
5925 mutex_lock(&ctx
->mutex
);
5926 perf_install_in_context(ctx
, event
, cpu
);
5928 mutex_unlock(&ctx
->mutex
);
5935 return ERR_PTR(err
);
5937 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
5939 static void sync_child_event(struct perf_event
*child_event
,
5940 struct task_struct
*child
)
5942 struct perf_event
*parent_event
= child_event
->parent
;
5945 if (child_event
->attr
.inherit_stat
)
5946 perf_event_read_event(child_event
, child
);
5948 child_val
= perf_event_count(child_event
);
5951 * Add back the child's count to the parent's count:
5953 atomic64_add(child_val
, &parent_event
->child_count
);
5954 atomic64_add(child_event
->total_time_enabled
,
5955 &parent_event
->child_total_time_enabled
);
5956 atomic64_add(child_event
->total_time_running
,
5957 &parent_event
->child_total_time_running
);
5960 * Remove this event from the parent's list
5962 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
5963 mutex_lock(&parent_event
->child_mutex
);
5964 list_del_init(&child_event
->child_list
);
5965 mutex_unlock(&parent_event
->child_mutex
);
5968 * Release the parent event, if this was the last
5971 fput(parent_event
->filp
);
5975 __perf_event_exit_task(struct perf_event
*child_event
,
5976 struct perf_event_context
*child_ctx
,
5977 struct task_struct
*child
)
5979 struct perf_event
*parent_event
;
5981 perf_event_remove_from_context(child_event
);
5983 parent_event
= child_event
->parent
;
5985 * It can happen that parent exits first, and has events
5986 * that are still around due to the child reference. These
5987 * events need to be zapped - but otherwise linger.
5990 sync_child_event(child_event
, child
);
5991 free_event(child_event
);
5995 static void perf_event_exit_task_context(struct task_struct
*child
, int ctxn
)
5997 struct perf_event
*child_event
, *tmp
;
5998 struct perf_event_context
*child_ctx
;
5999 unsigned long flags
;
6001 if (likely(!child
->perf_event_ctxp
[ctxn
])) {
6002 perf_event_task(child
, NULL
, 0);
6006 local_irq_save(flags
);
6008 * We can't reschedule here because interrupts are disabled,
6009 * and either child is current or it is a task that can't be
6010 * scheduled, so we are now safe from rescheduling changing
6013 child_ctx
= child
->perf_event_ctxp
[ctxn
];
6014 task_ctx_sched_out(child_ctx
, EVENT_ALL
);
6017 * Take the context lock here so that if find_get_context is
6018 * reading child->perf_event_ctxp, we wait until it has
6019 * incremented the context's refcount before we do put_ctx below.
6021 raw_spin_lock(&child_ctx
->lock
);
6022 child
->perf_event_ctxp
[ctxn
] = NULL
;
6024 * If this context is a clone; unclone it so it can't get
6025 * swapped to another process while we're removing all
6026 * the events from it.
6028 unclone_ctx(child_ctx
);
6029 update_context_time(child_ctx
);
6030 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
6033 * Report the task dead after unscheduling the events so that we
6034 * won't get any samples after PERF_RECORD_EXIT. We can however still
6035 * get a few PERF_RECORD_READ events.
6037 perf_event_task(child
, child_ctx
, 0);
6040 * We can recurse on the same lock type through:
6042 * __perf_event_exit_task()
6043 * sync_child_event()
6044 * fput(parent_event->filp)
6046 * mutex_lock(&ctx->mutex)
6048 * But since its the parent context it won't be the same instance.
6050 mutex_lock(&child_ctx
->mutex
);
6053 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->pinned_groups
,
6055 __perf_event_exit_task(child_event
, child_ctx
, child
);
6057 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->flexible_groups
,
6059 __perf_event_exit_task(child_event
, child_ctx
, child
);
6062 * If the last event was a group event, it will have appended all
6063 * its siblings to the list, but we obtained 'tmp' before that which
6064 * will still point to the list head terminating the iteration.
6066 if (!list_empty(&child_ctx
->pinned_groups
) ||
6067 !list_empty(&child_ctx
->flexible_groups
))
6070 mutex_unlock(&child_ctx
->mutex
);
6076 * When a child task exits, feed back event values to parent events.
6078 void perf_event_exit_task(struct task_struct
*child
)
6080 struct perf_event
*event
, *tmp
;
6083 mutex_lock(&child
->perf_event_mutex
);
6084 list_for_each_entry_safe(event
, tmp
, &child
->perf_event_list
,
6086 list_del_init(&event
->owner_entry
);
6089 * Ensure the list deletion is visible before we clear
6090 * the owner, closes a race against perf_release() where
6091 * we need to serialize on the owner->perf_event_mutex.
6094 event
->owner
= NULL
;
6096 mutex_unlock(&child
->perf_event_mutex
);
6098 for_each_task_context_nr(ctxn
)
6099 perf_event_exit_task_context(child
, ctxn
);
6102 static void perf_free_event(struct perf_event
*event
,
6103 struct perf_event_context
*ctx
)
6105 struct perf_event
*parent
= event
->parent
;
6107 if (WARN_ON_ONCE(!parent
))
6110 mutex_lock(&parent
->child_mutex
);
6111 list_del_init(&event
->child_list
);
6112 mutex_unlock(&parent
->child_mutex
);
6116 perf_group_detach(event
);
6117 list_del_event(event
, ctx
);
6122 * free an unexposed, unused context as created by inheritance by
6123 * perf_event_init_task below, used by fork() in case of fail.
6125 void perf_event_free_task(struct task_struct
*task
)
6127 struct perf_event_context
*ctx
;
6128 struct perf_event
*event
, *tmp
;
6131 for_each_task_context_nr(ctxn
) {
6132 ctx
= task
->perf_event_ctxp
[ctxn
];
6136 mutex_lock(&ctx
->mutex
);
6138 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
,
6140 perf_free_event(event
, ctx
);
6142 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
,
6144 perf_free_event(event
, ctx
);
6146 if (!list_empty(&ctx
->pinned_groups
) ||
6147 !list_empty(&ctx
->flexible_groups
))
6150 mutex_unlock(&ctx
->mutex
);
6156 void perf_event_delayed_put(struct task_struct
*task
)
6160 for_each_task_context_nr(ctxn
)
6161 WARN_ON_ONCE(task
->perf_event_ctxp
[ctxn
]);
6165 * inherit a event from parent task to child task:
6167 static struct perf_event
*
6168 inherit_event(struct perf_event
*parent_event
,
6169 struct task_struct
*parent
,
6170 struct perf_event_context
*parent_ctx
,
6171 struct task_struct
*child
,
6172 struct perf_event
*group_leader
,
6173 struct perf_event_context
*child_ctx
)
6175 struct perf_event
*child_event
;
6176 unsigned long flags
;
6179 * Instead of creating recursive hierarchies of events,
6180 * we link inherited events back to the original parent,
6181 * which has a filp for sure, which we use as the reference
6184 if (parent_event
->parent
)
6185 parent_event
= parent_event
->parent
;
6187 child_event
= perf_event_alloc(&parent_event
->attr
,
6190 group_leader
, parent_event
,
6192 if (IS_ERR(child_event
))
6197 * Make the child state follow the state of the parent event,
6198 * not its attr.disabled bit. We hold the parent's mutex,
6199 * so we won't race with perf_event_{en, dis}able_family.
6201 if (parent_event
->state
>= PERF_EVENT_STATE_INACTIVE
)
6202 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
6204 child_event
->state
= PERF_EVENT_STATE_OFF
;
6206 if (parent_event
->attr
.freq
) {
6207 u64 sample_period
= parent_event
->hw
.sample_period
;
6208 struct hw_perf_event
*hwc
= &child_event
->hw
;
6210 hwc
->sample_period
= sample_period
;
6211 hwc
->last_period
= sample_period
;
6213 local64_set(&hwc
->period_left
, sample_period
);
6216 child_event
->ctx
= child_ctx
;
6217 child_event
->overflow_handler
= parent_event
->overflow_handler
;
6220 * Precalculate sample_data sizes
6222 perf_event__header_size(child_event
);
6223 perf_event__id_header_size(child_event
);
6226 * Link it up in the child's context:
6228 raw_spin_lock_irqsave(&child_ctx
->lock
, flags
);
6229 add_event_to_ctx(child_event
, child_ctx
);
6230 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
6233 * Get a reference to the parent filp - we will fput it
6234 * when the child event exits. This is safe to do because
6235 * we are in the parent and we know that the filp still
6236 * exists and has a nonzero count:
6238 atomic_long_inc(&parent_event
->filp
->f_count
);
6241 * Link this into the parent event's child list
6243 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
6244 mutex_lock(&parent_event
->child_mutex
);
6245 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
6246 mutex_unlock(&parent_event
->child_mutex
);
6251 static int inherit_group(struct perf_event
*parent_event
,
6252 struct task_struct
*parent
,
6253 struct perf_event_context
*parent_ctx
,
6254 struct task_struct
*child
,
6255 struct perf_event_context
*child_ctx
)
6257 struct perf_event
*leader
;
6258 struct perf_event
*sub
;
6259 struct perf_event
*child_ctr
;
6261 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
6262 child
, NULL
, child_ctx
);
6264 return PTR_ERR(leader
);
6265 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
6266 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
6267 child
, leader
, child_ctx
);
6268 if (IS_ERR(child_ctr
))
6269 return PTR_ERR(child_ctr
);
6275 inherit_task_group(struct perf_event
*event
, struct task_struct
*parent
,
6276 struct perf_event_context
*parent_ctx
,
6277 struct task_struct
*child
, int ctxn
,
6281 struct perf_event_context
*child_ctx
;
6283 if (!event
->attr
.inherit
) {
6288 child_ctx
= child
->perf_event_ctxp
[ctxn
];
6291 * This is executed from the parent task context, so
6292 * inherit events that have been marked for cloning.
6293 * First allocate and initialize a context for the
6297 child_ctx
= alloc_perf_context(event
->pmu
, child
);
6301 child
->perf_event_ctxp
[ctxn
] = child_ctx
;
6304 ret
= inherit_group(event
, parent
, parent_ctx
,
6314 * Initialize the perf_event context in task_struct
6316 int perf_event_init_context(struct task_struct
*child
, int ctxn
)
6318 struct perf_event_context
*child_ctx
, *parent_ctx
;
6319 struct perf_event_context
*cloned_ctx
;
6320 struct perf_event
*event
;
6321 struct task_struct
*parent
= current
;
6322 int inherited_all
= 1;
6323 unsigned long flags
;
6326 child
->perf_event_ctxp
[ctxn
] = NULL
;
6328 mutex_init(&child
->perf_event_mutex
);
6329 INIT_LIST_HEAD(&child
->perf_event_list
);
6331 if (likely(!parent
->perf_event_ctxp
[ctxn
]))
6335 * If the parent's context is a clone, pin it so it won't get
6338 parent_ctx
= perf_pin_task_context(parent
, ctxn
);
6341 * No need to check if parent_ctx != NULL here; since we saw
6342 * it non-NULL earlier, the only reason for it to become NULL
6343 * is if we exit, and since we're currently in the middle of
6344 * a fork we can't be exiting at the same time.
6348 * Lock the parent list. No need to lock the child - not PID
6349 * hashed yet and not running, so nobody can access it.
6351 mutex_lock(&parent_ctx
->mutex
);
6354 * We dont have to disable NMIs - we are only looking at
6355 * the list, not manipulating it:
6357 list_for_each_entry(event
, &parent_ctx
->pinned_groups
, group_entry
) {
6358 ret
= inherit_task_group(event
, parent
, parent_ctx
,
6359 child
, ctxn
, &inherited_all
);
6365 * We can't hold ctx->lock when iterating the ->flexible_group list due
6366 * to allocations, but we need to prevent rotation because
6367 * rotate_ctx() will change the list from interrupt context.
6369 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
6370 parent_ctx
->rotate_disable
= 1;
6371 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
6373 list_for_each_entry(event
, &parent_ctx
->flexible_groups
, group_entry
) {
6374 ret
= inherit_task_group(event
, parent
, parent_ctx
,
6375 child
, ctxn
, &inherited_all
);
6380 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
6381 parent_ctx
->rotate_disable
= 0;
6382 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
6384 child_ctx
= child
->perf_event_ctxp
[ctxn
];
6386 if (child_ctx
&& inherited_all
) {
6388 * Mark the child context as a clone of the parent
6389 * context, or of whatever the parent is a clone of.
6390 * Note that if the parent is a clone, it could get
6391 * uncloned at any point, but that doesn't matter
6392 * because the list of events and the generation
6393 * count can't have changed since we took the mutex.
6395 cloned_ctx
= rcu_dereference(parent_ctx
->parent_ctx
);
6397 child_ctx
->parent_ctx
= cloned_ctx
;
6398 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
6400 child_ctx
->parent_ctx
= parent_ctx
;
6401 child_ctx
->parent_gen
= parent_ctx
->generation
;
6403 get_ctx(child_ctx
->parent_ctx
);
6406 mutex_unlock(&parent_ctx
->mutex
);
6408 perf_unpin_context(parent_ctx
);
6414 * Initialize the perf_event context in task_struct
6416 int perf_event_init_task(struct task_struct
*child
)
6420 for_each_task_context_nr(ctxn
) {
6421 ret
= perf_event_init_context(child
, ctxn
);
6429 static void __init
perf_event_init_all_cpus(void)
6431 struct swevent_htable
*swhash
;
6434 for_each_possible_cpu(cpu
) {
6435 swhash
= &per_cpu(swevent_htable
, cpu
);
6436 mutex_init(&swhash
->hlist_mutex
);
6437 INIT_LIST_HEAD(&per_cpu(rotation_list
, cpu
));
6441 static void __cpuinit
perf_event_init_cpu(int cpu
)
6443 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
6445 mutex_lock(&swhash
->hlist_mutex
);
6446 if (swhash
->hlist_refcount
> 0) {
6447 struct swevent_hlist
*hlist
;
6449 hlist
= kzalloc_node(sizeof(*hlist
), GFP_KERNEL
, cpu_to_node(cpu
));
6451 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
6453 mutex_unlock(&swhash
->hlist_mutex
);
6456 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
6457 static void perf_pmu_rotate_stop(struct pmu
*pmu
)
6459 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
6461 WARN_ON(!irqs_disabled());
6463 list_del_init(&cpuctx
->rotation_list
);
6466 static void __perf_event_exit_context(void *__info
)
6468 struct perf_event_context
*ctx
= __info
;
6469 struct perf_event
*event
, *tmp
;
6471 perf_pmu_rotate_stop(ctx
->pmu
);
6473 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
, group_entry
)
6474 __perf_event_remove_from_context(event
);
6475 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
, group_entry
)
6476 __perf_event_remove_from_context(event
);
6479 static void perf_event_exit_cpu_context(int cpu
)
6481 struct perf_event_context
*ctx
;
6485 idx
= srcu_read_lock(&pmus_srcu
);
6486 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
6487 ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
)->ctx
;
6489 mutex_lock(&ctx
->mutex
);
6490 smp_call_function_single(cpu
, __perf_event_exit_context
, ctx
, 1);
6491 mutex_unlock(&ctx
->mutex
);
6493 srcu_read_unlock(&pmus_srcu
, idx
);
6496 static void perf_event_exit_cpu(int cpu
)
6498 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
6500 mutex_lock(&swhash
->hlist_mutex
);
6501 swevent_hlist_release(swhash
);
6502 mutex_unlock(&swhash
->hlist_mutex
);
6504 perf_event_exit_cpu_context(cpu
);
6507 static inline void perf_event_exit_cpu(int cpu
) { }
6511 perf_reboot(struct notifier_block
*notifier
, unsigned long val
, void *v
)
6515 for_each_online_cpu(cpu
)
6516 perf_event_exit_cpu(cpu
);
6522 * Run the perf reboot notifier at the very last possible moment so that
6523 * the generic watchdog code runs as long as possible.
6525 static struct notifier_block perf_reboot_notifier
= {
6526 .notifier_call
= perf_reboot
,
6527 .priority
= INT_MIN
,
6530 static int __cpuinit
6531 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
6533 unsigned int cpu
= (long)hcpu
;
6535 switch (action
& ~CPU_TASKS_FROZEN
) {
6537 case CPU_UP_PREPARE
:
6538 case CPU_DOWN_FAILED
:
6539 perf_event_init_cpu(cpu
);
6542 case CPU_UP_CANCELED
:
6543 case CPU_DOWN_PREPARE
:
6544 perf_event_exit_cpu(cpu
);
6554 void __init
perf_event_init(void)
6558 perf_event_init_all_cpus();
6559 init_srcu_struct(&pmus_srcu
);
6560 perf_pmu_register(&perf_swevent
);
6561 perf_pmu_register(&perf_cpu_clock
);
6562 perf_pmu_register(&perf_task_clock
);
6564 perf_cpu_notifier(perf_cpu_notify
);
6565 register_reboot_notifier(&perf_reboot_notifier
);
6567 ret
= init_hw_breakpoint();
6568 WARN(ret
, "hw_breakpoint initialization failed with: %d", ret
);