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/idr.h>
17 #include <linux/file.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/hash.h>
21 #include <linux/sysfs.h>
22 #include <linux/dcache.h>
23 #include <linux/percpu.h>
24 #include <linux/ptrace.h>
25 #include <linux/reboot.h>
26 #include <linux/vmstat.h>
27 #include <linux/device.h>
28 #include <linux/vmalloc.h>
29 #include <linux/hardirq.h>
30 #include <linux/rculist.h>
31 #include <linux/uaccess.h>
32 #include <linux/syscalls.h>
33 #include <linux/anon_inodes.h>
34 #include <linux/kernel_stat.h>
35 #include <linux/perf_event.h>
36 #include <linux/ftrace_event.h>
37 #include <linux/hw_breakpoint.h>
39 #include <asm/irq_regs.h>
44 EVENT_ALL
= EVENT_FLEXIBLE
| EVENT_PINNED
,
47 atomic_t perf_task_events __read_mostly
;
48 static atomic_t nr_mmap_events __read_mostly
;
49 static atomic_t nr_comm_events __read_mostly
;
50 static atomic_t nr_task_events __read_mostly
;
52 static LIST_HEAD(pmus
);
53 static DEFINE_MUTEX(pmus_lock
);
54 static struct srcu_struct pmus_srcu
;
57 * perf event paranoia level:
58 * -1 - not paranoid at all
59 * 0 - disallow raw tracepoint access for unpriv
60 * 1 - disallow cpu events for unpriv
61 * 2 - disallow kernel profiling for unpriv
63 int sysctl_perf_event_paranoid __read_mostly
= 1;
65 int sysctl_perf_event_mlock __read_mostly
= 512; /* 'free' kb per user */
68 * max perf event sample rate
70 int sysctl_perf_event_sample_rate __read_mostly
= 100000;
72 static atomic64_t perf_event_id
;
74 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
75 enum event_type_t event_type
);
77 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
78 enum event_type_t event_type
);
80 void __weak
perf_event_print_debug(void) { }
82 extern __weak
const char *perf_pmu_name(void)
87 static inline u64
perf_clock(void)
92 void perf_pmu_disable(struct pmu
*pmu
)
94 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
96 pmu
->pmu_disable(pmu
);
99 void perf_pmu_enable(struct pmu
*pmu
)
101 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
103 pmu
->pmu_enable(pmu
);
106 static DEFINE_PER_CPU(struct list_head
, rotation_list
);
109 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
110 * because they're strictly cpu affine and rotate_start is called with IRQs
111 * disabled, while rotate_context is called from IRQ context.
113 static void perf_pmu_rotate_start(struct pmu
*pmu
)
115 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
116 struct list_head
*head
= &__get_cpu_var(rotation_list
);
118 WARN_ON(!irqs_disabled());
120 if (list_empty(&cpuctx
->rotation_list
))
121 list_add(&cpuctx
->rotation_list
, head
);
124 static void get_ctx(struct perf_event_context
*ctx
)
126 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
129 static void free_ctx(struct rcu_head
*head
)
131 struct perf_event_context
*ctx
;
133 ctx
= container_of(head
, struct perf_event_context
, rcu_head
);
137 static void put_ctx(struct perf_event_context
*ctx
)
139 if (atomic_dec_and_test(&ctx
->refcount
)) {
141 put_ctx(ctx
->parent_ctx
);
143 put_task_struct(ctx
->task
);
144 call_rcu(&ctx
->rcu_head
, free_ctx
);
148 static void unclone_ctx(struct perf_event_context
*ctx
)
150 if (ctx
->parent_ctx
) {
151 put_ctx(ctx
->parent_ctx
);
152 ctx
->parent_ctx
= NULL
;
156 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
159 * only top level events have the pid namespace they were created in
162 event
= event
->parent
;
164 return task_tgid_nr_ns(p
, event
->ns
);
167 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
170 * only top level events have the pid namespace they were created in
173 event
= event
->parent
;
175 return task_pid_nr_ns(p
, event
->ns
);
179 * If we inherit events we want to return the parent event id
182 static u64
primary_event_id(struct perf_event
*event
)
187 id
= event
->parent
->id
;
193 * Get the perf_event_context for a task and lock it.
194 * This has to cope with with the fact that until it is locked,
195 * the context could get moved to another task.
197 static struct perf_event_context
*
198 perf_lock_task_context(struct task_struct
*task
, int ctxn
, unsigned long *flags
)
200 struct perf_event_context
*ctx
;
204 ctx
= rcu_dereference(task
->perf_event_ctxp
[ctxn
]);
207 * If this context is a clone of another, it might
208 * get swapped for another underneath us by
209 * perf_event_task_sched_out, though the
210 * rcu_read_lock() protects us from any context
211 * getting freed. Lock the context and check if it
212 * got swapped before we could get the lock, and retry
213 * if so. If we locked the right context, then it
214 * can't get swapped on us any more.
216 raw_spin_lock_irqsave(&ctx
->lock
, *flags
);
217 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
[ctxn
])) {
218 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
222 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
223 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
232 * Get the context for a task and increment its pin_count so it
233 * can't get swapped to another task. This also increments its
234 * reference count so that the context can't get freed.
236 static struct perf_event_context
*
237 perf_pin_task_context(struct task_struct
*task
, int ctxn
)
239 struct perf_event_context
*ctx
;
242 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
245 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
250 static void perf_unpin_context(struct perf_event_context
*ctx
)
254 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
256 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
261 * Update the record of the current time in a context.
263 static void update_context_time(struct perf_event_context
*ctx
)
265 u64 now
= perf_clock();
267 ctx
->time
+= now
- ctx
->timestamp
;
268 ctx
->timestamp
= now
;
272 * Update the total_time_enabled and total_time_running fields for a event.
274 static void update_event_times(struct perf_event
*event
)
276 struct perf_event_context
*ctx
= event
->ctx
;
279 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
280 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
286 run_end
= event
->tstamp_stopped
;
288 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
290 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
291 run_end
= event
->tstamp_stopped
;
295 event
->total_time_running
= run_end
- event
->tstamp_running
;
299 * Update total_time_enabled and total_time_running for all events in a group.
301 static void update_group_times(struct perf_event
*leader
)
303 struct perf_event
*event
;
305 update_event_times(leader
);
306 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
307 update_event_times(event
);
310 static struct list_head
*
311 ctx_group_list(struct perf_event
*event
, struct perf_event_context
*ctx
)
313 if (event
->attr
.pinned
)
314 return &ctx
->pinned_groups
;
316 return &ctx
->flexible_groups
;
320 * Add a event from the lists for its context.
321 * Must be called with ctx->mutex and ctx->lock held.
324 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
326 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_CONTEXT
);
327 event
->attach_state
|= PERF_ATTACH_CONTEXT
;
330 * If we're a stand alone event or group leader, we go to the context
331 * list, group events are kept attached to the group so that
332 * perf_group_detach can, at all times, locate all siblings.
334 if (event
->group_leader
== event
) {
335 struct list_head
*list
;
337 if (is_software_event(event
))
338 event
->group_flags
|= PERF_GROUP_SOFTWARE
;
340 list
= ctx_group_list(event
, ctx
);
341 list_add_tail(&event
->group_entry
, list
);
344 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
346 perf_pmu_rotate_start(ctx
->pmu
);
348 if (event
->attr
.inherit_stat
)
353 * Called at perf_event creation and when events are attached/detached from a
356 static void perf_event__read_size(struct perf_event
*event
)
358 int entry
= sizeof(u64
); /* value */
362 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
365 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
368 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
369 entry
+= sizeof(u64
);
371 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
372 nr
+= event
->group_leader
->nr_siblings
;
377 event
->read_size
= size
;
380 static void perf_event__header_size(struct perf_event
*event
)
382 struct perf_sample_data
*data
;
383 u64 sample_type
= event
->attr
.sample_type
;
386 perf_event__read_size(event
);
388 if (sample_type
& PERF_SAMPLE_IP
)
389 size
+= sizeof(data
->ip
);
391 if (sample_type
& PERF_SAMPLE_ADDR
)
392 size
+= sizeof(data
->addr
);
394 if (sample_type
& PERF_SAMPLE_PERIOD
)
395 size
+= sizeof(data
->period
);
397 if (sample_type
& PERF_SAMPLE_READ
)
398 size
+= event
->read_size
;
400 event
->header_size
= size
;
403 static void perf_event__id_header_size(struct perf_event
*event
)
405 struct perf_sample_data
*data
;
406 u64 sample_type
= event
->attr
.sample_type
;
409 if (sample_type
& PERF_SAMPLE_TID
)
410 size
+= sizeof(data
->tid_entry
);
412 if (sample_type
& PERF_SAMPLE_TIME
)
413 size
+= sizeof(data
->time
);
415 if (sample_type
& PERF_SAMPLE_ID
)
416 size
+= sizeof(data
->id
);
418 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
419 size
+= sizeof(data
->stream_id
);
421 if (sample_type
& PERF_SAMPLE_CPU
)
422 size
+= sizeof(data
->cpu_entry
);
424 event
->id_header_size
= size
;
427 static void perf_group_attach(struct perf_event
*event
)
429 struct perf_event
*group_leader
= event
->group_leader
, *pos
;
432 * We can have double attach due to group movement in perf_event_open.
434 if (event
->attach_state
& PERF_ATTACH_GROUP
)
437 event
->attach_state
|= PERF_ATTACH_GROUP
;
439 if (group_leader
== event
)
442 if (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
&&
443 !is_software_event(event
))
444 group_leader
->group_flags
&= ~PERF_GROUP_SOFTWARE
;
446 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
447 group_leader
->nr_siblings
++;
449 perf_event__header_size(group_leader
);
451 list_for_each_entry(pos
, &group_leader
->sibling_list
, group_entry
)
452 perf_event__header_size(pos
);
456 * Remove a event from the lists for its context.
457 * Must be called with ctx->mutex and ctx->lock held.
460 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
463 * We can have double detach due to exit/hot-unplug + close.
465 if (!(event
->attach_state
& PERF_ATTACH_CONTEXT
))
468 event
->attach_state
&= ~PERF_ATTACH_CONTEXT
;
471 if (event
->attr
.inherit_stat
)
474 list_del_rcu(&event
->event_entry
);
476 if (event
->group_leader
== event
)
477 list_del_init(&event
->group_entry
);
479 update_group_times(event
);
482 * If event was in error state, then keep it
483 * that way, otherwise bogus counts will be
484 * returned on read(). The only way to get out
485 * of error state is by explicit re-enabling
488 if (event
->state
> PERF_EVENT_STATE_OFF
)
489 event
->state
= PERF_EVENT_STATE_OFF
;
492 static void perf_group_detach(struct perf_event
*event
)
494 struct perf_event
*sibling
, *tmp
;
495 struct list_head
*list
= NULL
;
498 * We can have double detach due to exit/hot-unplug + close.
500 if (!(event
->attach_state
& PERF_ATTACH_GROUP
))
503 event
->attach_state
&= ~PERF_ATTACH_GROUP
;
506 * If this is a sibling, remove it from its group.
508 if (event
->group_leader
!= event
) {
509 list_del_init(&event
->group_entry
);
510 event
->group_leader
->nr_siblings
--;
514 if (!list_empty(&event
->group_entry
))
515 list
= &event
->group_entry
;
518 * If this was a group event with sibling events then
519 * upgrade the siblings to singleton events by adding them
520 * to whatever list we are on.
522 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
524 list_move_tail(&sibling
->group_entry
, list
);
525 sibling
->group_leader
= sibling
;
527 /* Inherit group flags from the previous leader */
528 sibling
->group_flags
= event
->group_flags
;
532 perf_event__header_size(event
->group_leader
);
534 list_for_each_entry(tmp
, &event
->group_leader
->sibling_list
, group_entry
)
535 perf_event__header_size(tmp
);
539 event_filter_match(struct perf_event
*event
)
541 return event
->cpu
== -1 || event
->cpu
== smp_processor_id();
545 event_sched_out(struct perf_event
*event
,
546 struct perf_cpu_context
*cpuctx
,
547 struct perf_event_context
*ctx
)
551 * An event which could not be activated because of
552 * filter mismatch still needs to have its timings
553 * maintained, otherwise bogus information is return
554 * via read() for time_enabled, time_running:
556 if (event
->state
== PERF_EVENT_STATE_INACTIVE
557 && !event_filter_match(event
)) {
558 delta
= ctx
->time
- event
->tstamp_stopped
;
559 event
->tstamp_running
+= delta
;
560 event
->tstamp_stopped
= ctx
->time
;
563 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
566 event
->state
= PERF_EVENT_STATE_INACTIVE
;
567 if (event
->pending_disable
) {
568 event
->pending_disable
= 0;
569 event
->state
= PERF_EVENT_STATE_OFF
;
571 event
->tstamp_stopped
= ctx
->time
;
572 event
->pmu
->del(event
, 0);
575 if (!is_software_event(event
))
576 cpuctx
->active_oncpu
--;
578 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
579 cpuctx
->exclusive
= 0;
583 group_sched_out(struct perf_event
*group_event
,
584 struct perf_cpu_context
*cpuctx
,
585 struct perf_event_context
*ctx
)
587 struct perf_event
*event
;
588 int state
= group_event
->state
;
590 event_sched_out(group_event
, cpuctx
, ctx
);
593 * Schedule out siblings (if any):
595 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
596 event_sched_out(event
, cpuctx
, ctx
);
598 if (state
== PERF_EVENT_STATE_ACTIVE
&& group_event
->attr
.exclusive
)
599 cpuctx
->exclusive
= 0;
602 static inline struct perf_cpu_context
*
603 __get_cpu_context(struct perf_event_context
*ctx
)
605 return this_cpu_ptr(ctx
->pmu
->pmu_cpu_context
);
609 * Cross CPU call to remove a performance event
611 * We disable the event on the hardware level first. After that we
612 * remove it from the context list.
614 static void __perf_event_remove_from_context(void *info
)
616 struct perf_event
*event
= info
;
617 struct perf_event_context
*ctx
= event
->ctx
;
618 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
621 * If this is a task context, we need to check whether it is
622 * the current task context of this cpu. If not it has been
623 * scheduled out before the smp call arrived.
625 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
628 raw_spin_lock(&ctx
->lock
);
630 event_sched_out(event
, cpuctx
, ctx
);
632 list_del_event(event
, ctx
);
634 raw_spin_unlock(&ctx
->lock
);
639 * Remove the event from a task's (or a CPU's) list of events.
641 * Must be called with ctx->mutex held.
643 * CPU events are removed with a smp call. For task events we only
644 * call when the task is on a CPU.
646 * If event->ctx is a cloned context, callers must make sure that
647 * every task struct that event->ctx->task could possibly point to
648 * remains valid. This is OK when called from perf_release since
649 * that only calls us on the top-level context, which can't be a clone.
650 * When called from perf_event_exit_task, it's OK because the
651 * context has been detached from its task.
653 static void perf_event_remove_from_context(struct perf_event
*event
)
655 struct perf_event_context
*ctx
= event
->ctx
;
656 struct task_struct
*task
= ctx
->task
;
660 * Per cpu events are removed via an smp call and
661 * the removal is always successful.
663 smp_call_function_single(event
->cpu
,
664 __perf_event_remove_from_context
,
670 task_oncpu_function_call(task
, __perf_event_remove_from_context
,
673 raw_spin_lock_irq(&ctx
->lock
);
675 * If the context is active we need to retry the smp call.
677 if (ctx
->nr_active
&& !list_empty(&event
->group_entry
)) {
678 raw_spin_unlock_irq(&ctx
->lock
);
683 * The lock prevents that this context is scheduled in so we
684 * can remove the event safely, if the call above did not
687 if (!list_empty(&event
->group_entry
))
688 list_del_event(event
, ctx
);
689 raw_spin_unlock_irq(&ctx
->lock
);
693 * Cross CPU call to disable a performance event
695 static void __perf_event_disable(void *info
)
697 struct perf_event
*event
= info
;
698 struct perf_event_context
*ctx
= event
->ctx
;
699 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
702 * If this is a per-task event, need to check whether this
703 * event's task is the current task on this cpu.
705 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
708 raw_spin_lock(&ctx
->lock
);
711 * If the event is on, turn it off.
712 * If it is in error state, leave it in error state.
714 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
) {
715 update_context_time(ctx
);
716 update_group_times(event
);
717 if (event
== event
->group_leader
)
718 group_sched_out(event
, cpuctx
, ctx
);
720 event_sched_out(event
, cpuctx
, ctx
);
721 event
->state
= PERF_EVENT_STATE_OFF
;
724 raw_spin_unlock(&ctx
->lock
);
730 * If event->ctx is a cloned context, callers must make sure that
731 * every task struct that event->ctx->task could possibly point to
732 * remains valid. This condition is satisifed when called through
733 * perf_event_for_each_child or perf_event_for_each because they
734 * hold the top-level event's child_mutex, so any descendant that
735 * goes to exit will block in sync_child_event.
736 * When called from perf_pending_event it's OK because event->ctx
737 * is the current context on this CPU and preemption is disabled,
738 * hence we can't get into perf_event_task_sched_out for this context.
740 void perf_event_disable(struct perf_event
*event
)
742 struct perf_event_context
*ctx
= event
->ctx
;
743 struct task_struct
*task
= ctx
->task
;
747 * Disable the event on the cpu that it's on
749 smp_call_function_single(event
->cpu
, __perf_event_disable
,
755 task_oncpu_function_call(task
, __perf_event_disable
, event
);
757 raw_spin_lock_irq(&ctx
->lock
);
759 * If the event is still active, we need to retry the cross-call.
761 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
762 raw_spin_unlock_irq(&ctx
->lock
);
767 * Since we have the lock this context can't be scheduled
768 * in, so we can change the state safely.
770 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
771 update_group_times(event
);
772 event
->state
= PERF_EVENT_STATE_OFF
;
775 raw_spin_unlock_irq(&ctx
->lock
);
779 event_sched_in(struct perf_event
*event
,
780 struct perf_cpu_context
*cpuctx
,
781 struct perf_event_context
*ctx
)
783 if (event
->state
<= PERF_EVENT_STATE_OFF
)
786 event
->state
= PERF_EVENT_STATE_ACTIVE
;
787 event
->oncpu
= smp_processor_id();
789 * The new state must be visible before we turn it on in the hardware:
793 if (event
->pmu
->add(event
, PERF_EF_START
)) {
794 event
->state
= PERF_EVENT_STATE_INACTIVE
;
799 event
->tstamp_running
+= ctx
->time
- event
->tstamp_stopped
;
801 event
->shadow_ctx_time
= ctx
->time
- ctx
->timestamp
;
803 if (!is_software_event(event
))
804 cpuctx
->active_oncpu
++;
807 if (event
->attr
.exclusive
)
808 cpuctx
->exclusive
= 1;
814 group_sched_in(struct perf_event
*group_event
,
815 struct perf_cpu_context
*cpuctx
,
816 struct perf_event_context
*ctx
)
818 struct perf_event
*event
, *partial_group
= NULL
;
819 struct pmu
*pmu
= group_event
->pmu
;
821 bool simulate
= false;
823 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
828 if (event_sched_in(group_event
, cpuctx
, ctx
)) {
829 pmu
->cancel_txn(pmu
);
834 * Schedule in siblings as one group (if any):
836 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
837 if (event_sched_in(event
, cpuctx
, ctx
)) {
838 partial_group
= event
;
843 if (!pmu
->commit_txn(pmu
))
848 * Groups can be scheduled in as one unit only, so undo any
849 * partial group before returning:
850 * The events up to the failed event are scheduled out normally,
851 * tstamp_stopped will be updated.
853 * The failed events and the remaining siblings need to have
854 * their timings updated as if they had gone thru event_sched_in()
855 * and event_sched_out(). This is required to get consistent timings
856 * across the group. This also takes care of the case where the group
857 * could never be scheduled by ensuring tstamp_stopped is set to mark
858 * the time the event was actually stopped, such that time delta
859 * calculation in update_event_times() is correct.
861 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
862 if (event
== partial_group
)
866 event
->tstamp_running
+= now
- event
->tstamp_stopped
;
867 event
->tstamp_stopped
= now
;
869 event_sched_out(event
, cpuctx
, ctx
);
872 event_sched_out(group_event
, cpuctx
, ctx
);
874 pmu
->cancel_txn(pmu
);
880 * Work out whether we can put this event group on the CPU now.
882 static int group_can_go_on(struct perf_event
*event
,
883 struct perf_cpu_context
*cpuctx
,
887 * Groups consisting entirely of software events can always go on.
889 if (event
->group_flags
& PERF_GROUP_SOFTWARE
)
892 * If an exclusive group is already on, no other hardware
895 if (cpuctx
->exclusive
)
898 * If this group is exclusive and there are already
899 * events on the CPU, it can't go on.
901 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
904 * Otherwise, try to add it if all previous groups were able
910 static void add_event_to_ctx(struct perf_event
*event
,
911 struct perf_event_context
*ctx
)
913 list_add_event(event
, ctx
);
914 perf_group_attach(event
);
915 event
->tstamp_enabled
= ctx
->time
;
916 event
->tstamp_running
= ctx
->time
;
917 event
->tstamp_stopped
= ctx
->time
;
921 * Cross CPU call to install and enable a performance event
923 * Must be called with ctx->mutex held
925 static void __perf_install_in_context(void *info
)
927 struct perf_event
*event
= info
;
928 struct perf_event_context
*ctx
= event
->ctx
;
929 struct perf_event
*leader
= event
->group_leader
;
930 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
934 * If this is a task context, we need to check whether it is
935 * the current task context of this cpu. If not it has been
936 * scheduled out before the smp call arrived.
937 * Or possibly this is the right context but it isn't
938 * on this cpu because it had no events.
940 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
941 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
943 cpuctx
->task_ctx
= ctx
;
946 raw_spin_lock(&ctx
->lock
);
948 update_context_time(ctx
);
950 add_event_to_ctx(event
, ctx
);
952 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
956 * Don't put the event on if it is disabled or if
957 * it is in a group and the group isn't on.
959 if (event
->state
!= PERF_EVENT_STATE_INACTIVE
||
960 (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
))
964 * An exclusive event can't go on if there are already active
965 * hardware events, and no hardware event can go on if there
966 * is already an exclusive event on.
968 if (!group_can_go_on(event
, cpuctx
, 1))
971 err
= event_sched_in(event
, cpuctx
, ctx
);
975 * This event couldn't go on. If it is in a group
976 * then we have to pull the whole group off.
977 * If the event group is pinned then put it in error state.
980 group_sched_out(leader
, cpuctx
, ctx
);
981 if (leader
->attr
.pinned
) {
982 update_group_times(leader
);
983 leader
->state
= PERF_EVENT_STATE_ERROR
;
988 raw_spin_unlock(&ctx
->lock
);
992 * Attach a performance event to a context
994 * First we add the event to the list with the hardware enable bit
995 * in event->hw_config cleared.
997 * If the event is attached to a task which is on a CPU we use a smp
998 * call to enable it in the task context. The task might have been
999 * scheduled away, but we check this in the smp call again.
1001 * Must be called with ctx->mutex held.
1004 perf_install_in_context(struct perf_event_context
*ctx
,
1005 struct perf_event
*event
,
1008 struct task_struct
*task
= ctx
->task
;
1014 * Per cpu events are installed via an smp call and
1015 * the install is always successful.
1017 smp_call_function_single(cpu
, __perf_install_in_context
,
1023 task_oncpu_function_call(task
, __perf_install_in_context
,
1026 raw_spin_lock_irq(&ctx
->lock
);
1028 * we need to retry the smp call.
1030 if (ctx
->is_active
&& list_empty(&event
->group_entry
)) {
1031 raw_spin_unlock_irq(&ctx
->lock
);
1036 * The lock prevents that this context is scheduled in so we
1037 * can add the event safely, if it the call above did not
1040 if (list_empty(&event
->group_entry
))
1041 add_event_to_ctx(event
, ctx
);
1042 raw_spin_unlock_irq(&ctx
->lock
);
1046 * Put a event into inactive state and update time fields.
1047 * Enabling the leader of a group effectively enables all
1048 * the group members that aren't explicitly disabled, so we
1049 * have to update their ->tstamp_enabled also.
1050 * Note: this works for group members as well as group leaders
1051 * since the non-leader members' sibling_lists will be empty.
1053 static void __perf_event_mark_enabled(struct perf_event
*event
,
1054 struct perf_event_context
*ctx
)
1056 struct perf_event
*sub
;
1058 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1059 event
->tstamp_enabled
= ctx
->time
- event
->total_time_enabled
;
1060 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
1061 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
) {
1062 sub
->tstamp_enabled
=
1063 ctx
->time
- sub
->total_time_enabled
;
1069 * Cross CPU call to enable a performance event
1071 static void __perf_event_enable(void *info
)
1073 struct perf_event
*event
= info
;
1074 struct perf_event_context
*ctx
= event
->ctx
;
1075 struct perf_event
*leader
= event
->group_leader
;
1076 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1080 * If this is a per-task event, need to check whether this
1081 * event's task is the current task on this cpu.
1083 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
1084 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
1086 cpuctx
->task_ctx
= ctx
;
1089 raw_spin_lock(&ctx
->lock
);
1091 update_context_time(ctx
);
1093 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1095 __perf_event_mark_enabled(event
, ctx
);
1097 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1101 * If the event is in a group and isn't the group leader,
1102 * then don't put it on unless the group is on.
1104 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
)
1107 if (!group_can_go_on(event
, cpuctx
, 1)) {
1110 if (event
== leader
)
1111 err
= group_sched_in(event
, cpuctx
, ctx
);
1113 err
= event_sched_in(event
, cpuctx
, ctx
);
1118 * If this event can't go on and it's part of a
1119 * group, then the whole group has to come off.
1121 if (leader
!= event
)
1122 group_sched_out(leader
, cpuctx
, ctx
);
1123 if (leader
->attr
.pinned
) {
1124 update_group_times(leader
);
1125 leader
->state
= PERF_EVENT_STATE_ERROR
;
1130 raw_spin_unlock(&ctx
->lock
);
1136 * If event->ctx is a cloned context, callers must make sure that
1137 * every task struct that event->ctx->task could possibly point to
1138 * remains valid. This condition is satisfied when called through
1139 * perf_event_for_each_child or perf_event_for_each as described
1140 * for perf_event_disable.
1142 void perf_event_enable(struct perf_event
*event
)
1144 struct perf_event_context
*ctx
= event
->ctx
;
1145 struct task_struct
*task
= ctx
->task
;
1149 * Enable the event on the cpu that it's on
1151 smp_call_function_single(event
->cpu
, __perf_event_enable
,
1156 raw_spin_lock_irq(&ctx
->lock
);
1157 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1161 * If the event is in error state, clear that first.
1162 * That way, if we see the event in error state below, we
1163 * know that it has gone back into error state, as distinct
1164 * from the task having been scheduled away before the
1165 * cross-call arrived.
1167 if (event
->state
== PERF_EVENT_STATE_ERROR
)
1168 event
->state
= PERF_EVENT_STATE_OFF
;
1171 raw_spin_unlock_irq(&ctx
->lock
);
1172 task_oncpu_function_call(task
, __perf_event_enable
, event
);
1174 raw_spin_lock_irq(&ctx
->lock
);
1177 * If the context is active and the event is still off,
1178 * we need to retry the cross-call.
1180 if (ctx
->is_active
&& event
->state
== PERF_EVENT_STATE_OFF
)
1184 * Since we have the lock this context can't be scheduled
1185 * in, so we can change the state safely.
1187 if (event
->state
== PERF_EVENT_STATE_OFF
)
1188 __perf_event_mark_enabled(event
, ctx
);
1191 raw_spin_unlock_irq(&ctx
->lock
);
1194 static int perf_event_refresh(struct perf_event
*event
, int refresh
)
1197 * not supported on inherited events
1199 if (event
->attr
.inherit
|| !is_sampling_event(event
))
1202 atomic_add(refresh
, &event
->event_limit
);
1203 perf_event_enable(event
);
1208 static void ctx_sched_out(struct perf_event_context
*ctx
,
1209 struct perf_cpu_context
*cpuctx
,
1210 enum event_type_t event_type
)
1212 struct perf_event
*event
;
1214 raw_spin_lock(&ctx
->lock
);
1215 perf_pmu_disable(ctx
->pmu
);
1217 if (likely(!ctx
->nr_events
))
1219 update_context_time(ctx
);
1221 if (!ctx
->nr_active
)
1224 if (event_type
& EVENT_PINNED
) {
1225 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
)
1226 group_sched_out(event
, cpuctx
, ctx
);
1229 if (event_type
& EVENT_FLEXIBLE
) {
1230 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
)
1231 group_sched_out(event
, cpuctx
, ctx
);
1234 perf_pmu_enable(ctx
->pmu
);
1235 raw_spin_unlock(&ctx
->lock
);
1239 * Test whether two contexts are equivalent, i.e. whether they
1240 * have both been cloned from the same version of the same context
1241 * and they both have the same number of enabled events.
1242 * If the number of enabled events is the same, then the set
1243 * of enabled events should be the same, because these are both
1244 * inherited contexts, therefore we can't access individual events
1245 * in them directly with an fd; we can only enable/disable all
1246 * events via prctl, or enable/disable all events in a family
1247 * via ioctl, which will have the same effect on both contexts.
1249 static int context_equiv(struct perf_event_context
*ctx1
,
1250 struct perf_event_context
*ctx2
)
1252 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
1253 && ctx1
->parent_gen
== ctx2
->parent_gen
1254 && !ctx1
->pin_count
&& !ctx2
->pin_count
;
1257 static void __perf_event_sync_stat(struct perf_event
*event
,
1258 struct perf_event
*next_event
)
1262 if (!event
->attr
.inherit_stat
)
1266 * Update the event value, we cannot use perf_event_read()
1267 * because we're in the middle of a context switch and have IRQs
1268 * disabled, which upsets smp_call_function_single(), however
1269 * we know the event must be on the current CPU, therefore we
1270 * don't need to use it.
1272 switch (event
->state
) {
1273 case PERF_EVENT_STATE_ACTIVE
:
1274 event
->pmu
->read(event
);
1277 case PERF_EVENT_STATE_INACTIVE
:
1278 update_event_times(event
);
1286 * In order to keep per-task stats reliable we need to flip the event
1287 * values when we flip the contexts.
1289 value
= local64_read(&next_event
->count
);
1290 value
= local64_xchg(&event
->count
, value
);
1291 local64_set(&next_event
->count
, value
);
1293 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
1294 swap(event
->total_time_running
, next_event
->total_time_running
);
1297 * Since we swizzled the values, update the user visible data too.
1299 perf_event_update_userpage(event
);
1300 perf_event_update_userpage(next_event
);
1303 #define list_next_entry(pos, member) \
1304 list_entry(pos->member.next, typeof(*pos), member)
1306 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
1307 struct perf_event_context
*next_ctx
)
1309 struct perf_event
*event
, *next_event
;
1314 update_context_time(ctx
);
1316 event
= list_first_entry(&ctx
->event_list
,
1317 struct perf_event
, event_entry
);
1319 next_event
= list_first_entry(&next_ctx
->event_list
,
1320 struct perf_event
, event_entry
);
1322 while (&event
->event_entry
!= &ctx
->event_list
&&
1323 &next_event
->event_entry
!= &next_ctx
->event_list
) {
1325 __perf_event_sync_stat(event
, next_event
);
1327 event
= list_next_entry(event
, event_entry
);
1328 next_event
= list_next_entry(next_event
, event_entry
);
1332 void perf_event_context_sched_out(struct task_struct
*task
, int ctxn
,
1333 struct task_struct
*next
)
1335 struct perf_event_context
*ctx
= task
->perf_event_ctxp
[ctxn
];
1336 struct perf_event_context
*next_ctx
;
1337 struct perf_event_context
*parent
;
1338 struct perf_cpu_context
*cpuctx
;
1344 cpuctx
= __get_cpu_context(ctx
);
1345 if (!cpuctx
->task_ctx
)
1349 parent
= rcu_dereference(ctx
->parent_ctx
);
1350 next_ctx
= next
->perf_event_ctxp
[ctxn
];
1351 if (parent
&& next_ctx
&&
1352 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
1354 * Looks like the two contexts are clones, so we might be
1355 * able to optimize the context switch. We lock both
1356 * contexts and check that they are clones under the
1357 * lock (including re-checking that neither has been
1358 * uncloned in the meantime). It doesn't matter which
1359 * order we take the locks because no other cpu could
1360 * be trying to lock both of these tasks.
1362 raw_spin_lock(&ctx
->lock
);
1363 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
1364 if (context_equiv(ctx
, next_ctx
)) {
1366 * XXX do we need a memory barrier of sorts
1367 * wrt to rcu_dereference() of perf_event_ctxp
1369 task
->perf_event_ctxp
[ctxn
] = next_ctx
;
1370 next
->perf_event_ctxp
[ctxn
] = ctx
;
1372 next_ctx
->task
= task
;
1375 perf_event_sync_stat(ctx
, next_ctx
);
1377 raw_spin_unlock(&next_ctx
->lock
);
1378 raw_spin_unlock(&ctx
->lock
);
1383 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
1384 cpuctx
->task_ctx
= NULL
;
1388 #define for_each_task_context_nr(ctxn) \
1389 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1392 * Called from scheduler to remove the events of the current task,
1393 * with interrupts disabled.
1395 * We stop each event and update the event value in event->count.
1397 * This does not protect us against NMI, but disable()
1398 * sets the disabled bit in the control field of event _before_
1399 * accessing the event control register. If a NMI hits, then it will
1400 * not restart the event.
1402 void __perf_event_task_sched_out(struct task_struct
*task
,
1403 struct task_struct
*next
)
1407 for_each_task_context_nr(ctxn
)
1408 perf_event_context_sched_out(task
, ctxn
, next
);
1411 static void task_ctx_sched_out(struct perf_event_context
*ctx
,
1412 enum event_type_t event_type
)
1414 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1416 if (!cpuctx
->task_ctx
)
1419 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
1422 ctx_sched_out(ctx
, cpuctx
, event_type
);
1423 cpuctx
->task_ctx
= NULL
;
1427 * Called with IRQs disabled
1429 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
1430 enum event_type_t event_type
)
1432 ctx_sched_out(&cpuctx
->ctx
, cpuctx
, event_type
);
1436 ctx_pinned_sched_in(struct perf_event_context
*ctx
,
1437 struct perf_cpu_context
*cpuctx
)
1439 struct perf_event
*event
;
1441 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
1442 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1444 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1447 if (group_can_go_on(event
, cpuctx
, 1))
1448 group_sched_in(event
, cpuctx
, ctx
);
1451 * If this pinned group hasn't been scheduled,
1452 * put it in error state.
1454 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1455 update_group_times(event
);
1456 event
->state
= PERF_EVENT_STATE_ERROR
;
1462 ctx_flexible_sched_in(struct perf_event_context
*ctx
,
1463 struct perf_cpu_context
*cpuctx
)
1465 struct perf_event
*event
;
1468 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
1469 /* Ignore events in OFF or ERROR state */
1470 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1473 * Listen to the 'cpu' scheduling filter constraint
1476 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1479 if (group_can_go_on(event
, cpuctx
, can_add_hw
)) {
1480 if (group_sched_in(event
, cpuctx
, ctx
))
1487 ctx_sched_in(struct perf_event_context
*ctx
,
1488 struct perf_cpu_context
*cpuctx
,
1489 enum event_type_t event_type
)
1491 raw_spin_lock(&ctx
->lock
);
1493 if (likely(!ctx
->nr_events
))
1496 ctx
->timestamp
= perf_clock();
1499 * First go through the list and put on any pinned groups
1500 * in order to give them the best chance of going on.
1502 if (event_type
& EVENT_PINNED
)
1503 ctx_pinned_sched_in(ctx
, cpuctx
);
1505 /* Then walk through the lower prio flexible groups */
1506 if (event_type
& EVENT_FLEXIBLE
)
1507 ctx_flexible_sched_in(ctx
, cpuctx
);
1510 raw_spin_unlock(&ctx
->lock
);
1513 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
1514 enum event_type_t event_type
)
1516 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
1518 ctx_sched_in(ctx
, cpuctx
, event_type
);
1521 static void task_ctx_sched_in(struct perf_event_context
*ctx
,
1522 enum event_type_t event_type
)
1524 struct perf_cpu_context
*cpuctx
;
1526 cpuctx
= __get_cpu_context(ctx
);
1527 if (cpuctx
->task_ctx
== ctx
)
1530 ctx_sched_in(ctx
, cpuctx
, event_type
);
1531 cpuctx
->task_ctx
= ctx
;
1534 void perf_event_context_sched_in(struct perf_event_context
*ctx
)
1536 struct perf_cpu_context
*cpuctx
;
1538 cpuctx
= __get_cpu_context(ctx
);
1539 if (cpuctx
->task_ctx
== ctx
)
1542 perf_pmu_disable(ctx
->pmu
);
1544 * We want to keep the following priority order:
1545 * cpu pinned (that don't need to move), task pinned,
1546 * cpu flexible, task flexible.
1548 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
1550 ctx_sched_in(ctx
, cpuctx
, EVENT_PINNED
);
1551 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
);
1552 ctx_sched_in(ctx
, cpuctx
, EVENT_FLEXIBLE
);
1554 cpuctx
->task_ctx
= ctx
;
1557 * Since these rotations are per-cpu, we need to ensure the
1558 * cpu-context we got scheduled on is actually rotating.
1560 perf_pmu_rotate_start(ctx
->pmu
);
1561 perf_pmu_enable(ctx
->pmu
);
1565 * Called from scheduler to add the events of the current task
1566 * with interrupts disabled.
1568 * We restore the event value and then enable it.
1570 * This does not protect us against NMI, but enable()
1571 * sets the enabled bit in the control field of event _before_
1572 * accessing the event control register. If a NMI hits, then it will
1573 * keep the event running.
1575 void __perf_event_task_sched_in(struct task_struct
*task
)
1577 struct perf_event_context
*ctx
;
1580 for_each_task_context_nr(ctxn
) {
1581 ctx
= task
->perf_event_ctxp
[ctxn
];
1585 perf_event_context_sched_in(ctx
);
1589 #define MAX_INTERRUPTS (~0ULL)
1591 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1593 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1595 u64 frequency
= event
->attr
.sample_freq
;
1596 u64 sec
= NSEC_PER_SEC
;
1597 u64 divisor
, dividend
;
1599 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
1601 count_fls
= fls64(count
);
1602 nsec_fls
= fls64(nsec
);
1603 frequency_fls
= fls64(frequency
);
1607 * We got @count in @nsec, with a target of sample_freq HZ
1608 * the target period becomes:
1611 * period = -------------------
1612 * @nsec * sample_freq
1617 * Reduce accuracy by one bit such that @a and @b converge
1618 * to a similar magnitude.
1620 #define REDUCE_FLS(a, b) \
1622 if (a##_fls > b##_fls) { \
1632 * Reduce accuracy until either term fits in a u64, then proceed with
1633 * the other, so that finally we can do a u64/u64 division.
1635 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
1636 REDUCE_FLS(nsec
, frequency
);
1637 REDUCE_FLS(sec
, count
);
1640 if (count_fls
+ sec_fls
> 64) {
1641 divisor
= nsec
* frequency
;
1643 while (count_fls
+ sec_fls
> 64) {
1644 REDUCE_FLS(count
, sec
);
1648 dividend
= count
* sec
;
1650 dividend
= count
* sec
;
1652 while (nsec_fls
+ frequency_fls
> 64) {
1653 REDUCE_FLS(nsec
, frequency
);
1657 divisor
= nsec
* frequency
;
1663 return div64_u64(dividend
, divisor
);
1666 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1668 struct hw_perf_event
*hwc
= &event
->hw
;
1669 s64 period
, sample_period
;
1672 period
= perf_calculate_period(event
, nsec
, count
);
1674 delta
= (s64
)(period
- hwc
->sample_period
);
1675 delta
= (delta
+ 7) / 8; /* low pass filter */
1677 sample_period
= hwc
->sample_period
+ delta
;
1682 hwc
->sample_period
= sample_period
;
1684 if (local64_read(&hwc
->period_left
) > 8*sample_period
) {
1685 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
1686 local64_set(&hwc
->period_left
, 0);
1687 event
->pmu
->start(event
, PERF_EF_RELOAD
);
1691 static void perf_ctx_adjust_freq(struct perf_event_context
*ctx
, u64 period
)
1693 struct perf_event
*event
;
1694 struct hw_perf_event
*hwc
;
1695 u64 interrupts
, now
;
1698 raw_spin_lock(&ctx
->lock
);
1699 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
1700 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1703 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1708 interrupts
= hwc
->interrupts
;
1709 hwc
->interrupts
= 0;
1712 * unthrottle events on the tick
1714 if (interrupts
== MAX_INTERRUPTS
) {
1715 perf_log_throttle(event
, 1);
1716 event
->pmu
->start(event
, 0);
1719 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
1722 event
->pmu
->read(event
);
1723 now
= local64_read(&event
->count
);
1724 delta
= now
- hwc
->freq_count_stamp
;
1725 hwc
->freq_count_stamp
= now
;
1728 perf_adjust_period(event
, period
, delta
);
1730 raw_spin_unlock(&ctx
->lock
);
1734 * Round-robin a context's events:
1736 static void rotate_ctx(struct perf_event_context
*ctx
)
1738 raw_spin_lock(&ctx
->lock
);
1741 * Rotate the first entry last of non-pinned groups. Rotation might be
1742 * disabled by the inheritance code.
1744 if (!ctx
->rotate_disable
)
1745 list_rotate_left(&ctx
->flexible_groups
);
1747 raw_spin_unlock(&ctx
->lock
);
1751 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
1752 * because they're strictly cpu affine and rotate_start is called with IRQs
1753 * disabled, while rotate_context is called from IRQ context.
1755 static void perf_rotate_context(struct perf_cpu_context
*cpuctx
)
1757 u64 interval
= (u64
)cpuctx
->jiffies_interval
* TICK_NSEC
;
1758 struct perf_event_context
*ctx
= NULL
;
1759 int rotate
= 0, remove
= 1;
1761 if (cpuctx
->ctx
.nr_events
) {
1763 if (cpuctx
->ctx
.nr_events
!= cpuctx
->ctx
.nr_active
)
1767 ctx
= cpuctx
->task_ctx
;
1768 if (ctx
&& ctx
->nr_events
) {
1770 if (ctx
->nr_events
!= ctx
->nr_active
)
1774 perf_pmu_disable(cpuctx
->ctx
.pmu
);
1775 perf_ctx_adjust_freq(&cpuctx
->ctx
, interval
);
1777 perf_ctx_adjust_freq(ctx
, interval
);
1782 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
1784 task_ctx_sched_out(ctx
, EVENT_FLEXIBLE
);
1786 rotate_ctx(&cpuctx
->ctx
);
1790 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
);
1792 task_ctx_sched_in(ctx
, EVENT_FLEXIBLE
);
1796 list_del_init(&cpuctx
->rotation_list
);
1798 perf_pmu_enable(cpuctx
->ctx
.pmu
);
1801 void perf_event_task_tick(void)
1803 struct list_head
*head
= &__get_cpu_var(rotation_list
);
1804 struct perf_cpu_context
*cpuctx
, *tmp
;
1806 WARN_ON(!irqs_disabled());
1808 list_for_each_entry_safe(cpuctx
, tmp
, head
, rotation_list
) {
1809 if (cpuctx
->jiffies_interval
== 1 ||
1810 !(jiffies
% cpuctx
->jiffies_interval
))
1811 perf_rotate_context(cpuctx
);
1815 static int event_enable_on_exec(struct perf_event
*event
,
1816 struct perf_event_context
*ctx
)
1818 if (!event
->attr
.enable_on_exec
)
1821 event
->attr
.enable_on_exec
= 0;
1822 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1825 __perf_event_mark_enabled(event
, ctx
);
1831 * Enable all of a task's events that have been marked enable-on-exec.
1832 * This expects task == current.
1834 static void perf_event_enable_on_exec(struct perf_event_context
*ctx
)
1836 struct perf_event
*event
;
1837 unsigned long flags
;
1841 local_irq_save(flags
);
1842 if (!ctx
|| !ctx
->nr_events
)
1845 task_ctx_sched_out(ctx
, EVENT_ALL
);
1847 raw_spin_lock(&ctx
->lock
);
1849 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
1850 ret
= event_enable_on_exec(event
, ctx
);
1855 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
1856 ret
= event_enable_on_exec(event
, ctx
);
1862 * Unclone this context if we enabled any event.
1867 raw_spin_unlock(&ctx
->lock
);
1869 perf_event_context_sched_in(ctx
);
1871 local_irq_restore(flags
);
1875 * Cross CPU call to read the hardware event
1877 static void __perf_event_read(void *info
)
1879 struct perf_event
*event
= info
;
1880 struct perf_event_context
*ctx
= event
->ctx
;
1881 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1884 * If this is a task context, we need to check whether it is
1885 * the current task context of this cpu. If not it has been
1886 * scheduled out before the smp call arrived. In that case
1887 * event->count would have been updated to a recent sample
1888 * when the event was scheduled out.
1890 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
1893 raw_spin_lock(&ctx
->lock
);
1894 update_context_time(ctx
);
1895 update_event_times(event
);
1896 raw_spin_unlock(&ctx
->lock
);
1898 event
->pmu
->read(event
);
1901 static inline u64
perf_event_count(struct perf_event
*event
)
1903 return local64_read(&event
->count
) + atomic64_read(&event
->child_count
);
1906 static u64
perf_event_read(struct perf_event
*event
)
1909 * If event is enabled and currently active on a CPU, update the
1910 * value in the event structure:
1912 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
1913 smp_call_function_single(event
->oncpu
,
1914 __perf_event_read
, event
, 1);
1915 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1916 struct perf_event_context
*ctx
= event
->ctx
;
1917 unsigned long flags
;
1919 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
1921 * may read while context is not active
1922 * (e.g., thread is blocked), in that case
1923 * we cannot update context time
1926 update_context_time(ctx
);
1927 update_event_times(event
);
1928 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1931 return perf_event_count(event
);
1938 struct callchain_cpus_entries
{
1939 struct rcu_head rcu_head
;
1940 struct perf_callchain_entry
*cpu_entries
[0];
1943 static DEFINE_PER_CPU(int, callchain_recursion
[PERF_NR_CONTEXTS
]);
1944 static atomic_t nr_callchain_events
;
1945 static DEFINE_MUTEX(callchain_mutex
);
1946 struct callchain_cpus_entries
*callchain_cpus_entries
;
1949 __weak
void perf_callchain_kernel(struct perf_callchain_entry
*entry
,
1950 struct pt_regs
*regs
)
1954 __weak
void perf_callchain_user(struct perf_callchain_entry
*entry
,
1955 struct pt_regs
*regs
)
1959 static void release_callchain_buffers_rcu(struct rcu_head
*head
)
1961 struct callchain_cpus_entries
*entries
;
1964 entries
= container_of(head
, struct callchain_cpus_entries
, rcu_head
);
1966 for_each_possible_cpu(cpu
)
1967 kfree(entries
->cpu_entries
[cpu
]);
1972 static void release_callchain_buffers(void)
1974 struct callchain_cpus_entries
*entries
;
1976 entries
= callchain_cpus_entries
;
1977 rcu_assign_pointer(callchain_cpus_entries
, NULL
);
1978 call_rcu(&entries
->rcu_head
, release_callchain_buffers_rcu
);
1981 static int alloc_callchain_buffers(void)
1985 struct callchain_cpus_entries
*entries
;
1988 * We can't use the percpu allocation API for data that can be
1989 * accessed from NMI. Use a temporary manual per cpu allocation
1990 * until that gets sorted out.
1992 size
= sizeof(*entries
) + sizeof(struct perf_callchain_entry
*) *
1993 num_possible_cpus();
1995 entries
= kzalloc(size
, GFP_KERNEL
);
1999 size
= sizeof(struct perf_callchain_entry
) * PERF_NR_CONTEXTS
;
2001 for_each_possible_cpu(cpu
) {
2002 entries
->cpu_entries
[cpu
] = kmalloc_node(size
, GFP_KERNEL
,
2004 if (!entries
->cpu_entries
[cpu
])
2008 rcu_assign_pointer(callchain_cpus_entries
, entries
);
2013 for_each_possible_cpu(cpu
)
2014 kfree(entries
->cpu_entries
[cpu
]);
2020 static int get_callchain_buffers(void)
2025 mutex_lock(&callchain_mutex
);
2027 count
= atomic_inc_return(&nr_callchain_events
);
2028 if (WARN_ON_ONCE(count
< 1)) {
2034 /* If the allocation failed, give up */
2035 if (!callchain_cpus_entries
)
2040 err
= alloc_callchain_buffers();
2042 release_callchain_buffers();
2044 mutex_unlock(&callchain_mutex
);
2049 static void put_callchain_buffers(void)
2051 if (atomic_dec_and_mutex_lock(&nr_callchain_events
, &callchain_mutex
)) {
2052 release_callchain_buffers();
2053 mutex_unlock(&callchain_mutex
);
2057 static int get_recursion_context(int *recursion
)
2065 else if (in_softirq())
2070 if (recursion
[rctx
])
2079 static inline void put_recursion_context(int *recursion
, int rctx
)
2085 static struct perf_callchain_entry
*get_callchain_entry(int *rctx
)
2088 struct callchain_cpus_entries
*entries
;
2090 *rctx
= get_recursion_context(__get_cpu_var(callchain_recursion
));
2094 entries
= rcu_dereference(callchain_cpus_entries
);
2098 cpu
= smp_processor_id();
2100 return &entries
->cpu_entries
[cpu
][*rctx
];
2104 put_callchain_entry(int rctx
)
2106 put_recursion_context(__get_cpu_var(callchain_recursion
), rctx
);
2109 static struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
2112 struct perf_callchain_entry
*entry
;
2115 entry
= get_callchain_entry(&rctx
);
2124 if (!user_mode(regs
)) {
2125 perf_callchain_store(entry
, PERF_CONTEXT_KERNEL
);
2126 perf_callchain_kernel(entry
, regs
);
2128 regs
= task_pt_regs(current
);
2134 perf_callchain_store(entry
, PERF_CONTEXT_USER
);
2135 perf_callchain_user(entry
, regs
);
2139 put_callchain_entry(rctx
);
2145 * Initialize the perf_event context in a task_struct:
2147 static void __perf_event_init_context(struct perf_event_context
*ctx
)
2149 raw_spin_lock_init(&ctx
->lock
);
2150 mutex_init(&ctx
->mutex
);
2151 INIT_LIST_HEAD(&ctx
->pinned_groups
);
2152 INIT_LIST_HEAD(&ctx
->flexible_groups
);
2153 INIT_LIST_HEAD(&ctx
->event_list
);
2154 atomic_set(&ctx
->refcount
, 1);
2157 static struct perf_event_context
*
2158 alloc_perf_context(struct pmu
*pmu
, struct task_struct
*task
)
2160 struct perf_event_context
*ctx
;
2162 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
2166 __perf_event_init_context(ctx
);
2169 get_task_struct(task
);
2176 static struct task_struct
*
2177 find_lively_task_by_vpid(pid_t vpid
)
2179 struct task_struct
*task
;
2186 task
= find_task_by_vpid(vpid
);
2188 get_task_struct(task
);
2192 return ERR_PTR(-ESRCH
);
2195 * Can't attach events to a dying task.
2198 if (task
->flags
& PF_EXITING
)
2201 /* Reuse ptrace permission checks for now. */
2203 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
2208 put_task_struct(task
);
2209 return ERR_PTR(err
);
2213 static struct perf_event_context
*
2214 find_get_context(struct pmu
*pmu
, struct task_struct
*task
, int cpu
)
2216 struct perf_event_context
*ctx
;
2217 struct perf_cpu_context
*cpuctx
;
2218 unsigned long flags
;
2221 if (!task
&& cpu
!= -1) {
2222 /* Must be root to operate on a CPU event: */
2223 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
2224 return ERR_PTR(-EACCES
);
2226 if (cpu
< 0 || cpu
>= nr_cpumask_bits
)
2227 return ERR_PTR(-EINVAL
);
2230 * We could be clever and allow to attach a event to an
2231 * offline CPU and activate it when the CPU comes up, but
2234 if (!cpu_online(cpu
))
2235 return ERR_PTR(-ENODEV
);
2237 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
2245 ctxn
= pmu
->task_ctx_nr
;
2250 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
2253 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
2257 ctx
= alloc_perf_context(pmu
, task
);
2264 if (cmpxchg(&task
->perf_event_ctxp
[ctxn
], NULL
, ctx
)) {
2266 * We raced with some other task; use
2267 * the context they set.
2269 put_task_struct(task
);
2278 return ERR_PTR(err
);
2281 static void perf_event_free_filter(struct perf_event
*event
);
2283 static void free_event_rcu(struct rcu_head
*head
)
2285 struct perf_event
*event
;
2287 event
= container_of(head
, struct perf_event
, rcu_head
);
2289 put_pid_ns(event
->ns
);
2290 perf_event_free_filter(event
);
2294 static void perf_buffer_put(struct perf_buffer
*buffer
);
2296 static void free_event(struct perf_event
*event
)
2298 irq_work_sync(&event
->pending
);
2300 if (!event
->parent
) {
2301 if (event
->attach_state
& PERF_ATTACH_TASK
)
2302 jump_label_dec(&perf_task_events
);
2303 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
2304 atomic_dec(&nr_mmap_events
);
2305 if (event
->attr
.comm
)
2306 atomic_dec(&nr_comm_events
);
2307 if (event
->attr
.task
)
2308 atomic_dec(&nr_task_events
);
2309 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
)
2310 put_callchain_buffers();
2313 if (event
->buffer
) {
2314 perf_buffer_put(event
->buffer
);
2315 event
->buffer
= NULL
;
2319 event
->destroy(event
);
2322 put_ctx(event
->ctx
);
2324 call_rcu(&event
->rcu_head
, free_event_rcu
);
2327 int perf_event_release_kernel(struct perf_event
*event
)
2329 struct perf_event_context
*ctx
= event
->ctx
;
2332 * Remove from the PMU, can't get re-enabled since we got
2333 * here because the last ref went.
2335 perf_event_disable(event
);
2337 WARN_ON_ONCE(ctx
->parent_ctx
);
2339 * There are two ways this annotation is useful:
2341 * 1) there is a lock recursion from perf_event_exit_task
2342 * see the comment there.
2344 * 2) there is a lock-inversion with mmap_sem through
2345 * perf_event_read_group(), which takes faults while
2346 * holding ctx->mutex, however this is called after
2347 * the last filedesc died, so there is no possibility
2348 * to trigger the AB-BA case.
2350 mutex_lock_nested(&ctx
->mutex
, SINGLE_DEPTH_NESTING
);
2351 raw_spin_lock_irq(&ctx
->lock
);
2352 perf_group_detach(event
);
2353 list_del_event(event
, ctx
);
2354 raw_spin_unlock_irq(&ctx
->lock
);
2355 mutex_unlock(&ctx
->mutex
);
2361 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
2364 * Called when the last reference to the file is gone.
2366 static int perf_release(struct inode
*inode
, struct file
*file
)
2368 struct perf_event
*event
= file
->private_data
;
2369 struct task_struct
*owner
;
2371 file
->private_data
= NULL
;
2374 owner
= ACCESS_ONCE(event
->owner
);
2376 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2377 * !owner it means the list deletion is complete and we can indeed
2378 * free this event, otherwise we need to serialize on
2379 * owner->perf_event_mutex.
2381 smp_read_barrier_depends();
2384 * Since delayed_put_task_struct() also drops the last
2385 * task reference we can safely take a new reference
2386 * while holding the rcu_read_lock().
2388 get_task_struct(owner
);
2393 mutex_lock(&owner
->perf_event_mutex
);
2395 * We have to re-check the event->owner field, if it is cleared
2396 * we raced with perf_event_exit_task(), acquiring the mutex
2397 * ensured they're done, and we can proceed with freeing the
2401 list_del_init(&event
->owner_entry
);
2402 mutex_unlock(&owner
->perf_event_mutex
);
2403 put_task_struct(owner
);
2406 return perf_event_release_kernel(event
);
2409 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
2411 struct perf_event
*child
;
2417 mutex_lock(&event
->child_mutex
);
2418 total
+= perf_event_read(event
);
2419 *enabled
+= event
->total_time_enabled
+
2420 atomic64_read(&event
->child_total_time_enabled
);
2421 *running
+= event
->total_time_running
+
2422 atomic64_read(&event
->child_total_time_running
);
2424 list_for_each_entry(child
, &event
->child_list
, child_list
) {
2425 total
+= perf_event_read(child
);
2426 *enabled
+= child
->total_time_enabled
;
2427 *running
+= child
->total_time_running
;
2429 mutex_unlock(&event
->child_mutex
);
2433 EXPORT_SYMBOL_GPL(perf_event_read_value
);
2435 static int perf_event_read_group(struct perf_event
*event
,
2436 u64 read_format
, char __user
*buf
)
2438 struct perf_event
*leader
= event
->group_leader
, *sub
;
2439 int n
= 0, size
= 0, ret
= -EFAULT
;
2440 struct perf_event_context
*ctx
= leader
->ctx
;
2442 u64 count
, enabled
, running
;
2444 mutex_lock(&ctx
->mutex
);
2445 count
= perf_event_read_value(leader
, &enabled
, &running
);
2447 values
[n
++] = 1 + leader
->nr_siblings
;
2448 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2449 values
[n
++] = enabled
;
2450 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2451 values
[n
++] = running
;
2452 values
[n
++] = count
;
2453 if (read_format
& PERF_FORMAT_ID
)
2454 values
[n
++] = primary_event_id(leader
);
2456 size
= n
* sizeof(u64
);
2458 if (copy_to_user(buf
, values
, size
))
2463 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
2466 values
[n
++] = perf_event_read_value(sub
, &enabled
, &running
);
2467 if (read_format
& PERF_FORMAT_ID
)
2468 values
[n
++] = primary_event_id(sub
);
2470 size
= n
* sizeof(u64
);
2472 if (copy_to_user(buf
+ ret
, values
, size
)) {
2480 mutex_unlock(&ctx
->mutex
);
2485 static int perf_event_read_one(struct perf_event
*event
,
2486 u64 read_format
, char __user
*buf
)
2488 u64 enabled
, running
;
2492 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
2493 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2494 values
[n
++] = enabled
;
2495 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2496 values
[n
++] = running
;
2497 if (read_format
& PERF_FORMAT_ID
)
2498 values
[n
++] = primary_event_id(event
);
2500 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
2503 return n
* sizeof(u64
);
2507 * Read the performance event - simple non blocking version for now
2510 perf_read_hw(struct perf_event
*event
, char __user
*buf
, size_t count
)
2512 u64 read_format
= event
->attr
.read_format
;
2516 * Return end-of-file for a read on a event that is in
2517 * error state (i.e. because it was pinned but it couldn't be
2518 * scheduled on to the CPU at some point).
2520 if (event
->state
== PERF_EVENT_STATE_ERROR
)
2523 if (count
< event
->read_size
)
2526 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2527 if (read_format
& PERF_FORMAT_GROUP
)
2528 ret
= perf_event_read_group(event
, read_format
, buf
);
2530 ret
= perf_event_read_one(event
, read_format
, buf
);
2536 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
2538 struct perf_event
*event
= file
->private_data
;
2540 return perf_read_hw(event
, buf
, count
);
2543 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
2545 struct perf_event
*event
= file
->private_data
;
2546 struct perf_buffer
*buffer
;
2547 unsigned int events
= POLL_HUP
;
2550 buffer
= rcu_dereference(event
->buffer
);
2552 events
= atomic_xchg(&buffer
->poll
, 0);
2555 poll_wait(file
, &event
->waitq
, wait
);
2560 static void perf_event_reset(struct perf_event
*event
)
2562 (void)perf_event_read(event
);
2563 local64_set(&event
->count
, 0);
2564 perf_event_update_userpage(event
);
2568 * Holding the top-level event's child_mutex means that any
2569 * descendant process that has inherited this event will block
2570 * in sync_child_event if it goes to exit, thus satisfying the
2571 * task existence requirements of perf_event_enable/disable.
2573 static void perf_event_for_each_child(struct perf_event
*event
,
2574 void (*func
)(struct perf_event
*))
2576 struct perf_event
*child
;
2578 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2579 mutex_lock(&event
->child_mutex
);
2581 list_for_each_entry(child
, &event
->child_list
, child_list
)
2583 mutex_unlock(&event
->child_mutex
);
2586 static void perf_event_for_each(struct perf_event
*event
,
2587 void (*func
)(struct perf_event
*))
2589 struct perf_event_context
*ctx
= event
->ctx
;
2590 struct perf_event
*sibling
;
2592 WARN_ON_ONCE(ctx
->parent_ctx
);
2593 mutex_lock(&ctx
->mutex
);
2594 event
= event
->group_leader
;
2596 perf_event_for_each_child(event
, func
);
2598 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
2599 perf_event_for_each_child(event
, func
);
2600 mutex_unlock(&ctx
->mutex
);
2603 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
2605 struct perf_event_context
*ctx
= event
->ctx
;
2609 if (!is_sampling_event(event
))
2612 if (copy_from_user(&value
, arg
, sizeof(value
)))
2618 raw_spin_lock_irq(&ctx
->lock
);
2619 if (event
->attr
.freq
) {
2620 if (value
> sysctl_perf_event_sample_rate
) {
2625 event
->attr
.sample_freq
= value
;
2627 event
->attr
.sample_period
= value
;
2628 event
->hw
.sample_period
= value
;
2631 raw_spin_unlock_irq(&ctx
->lock
);
2636 static const struct file_operations perf_fops
;
2638 static struct perf_event
*perf_fget_light(int fd
, int *fput_needed
)
2642 file
= fget_light(fd
, fput_needed
);
2644 return ERR_PTR(-EBADF
);
2646 if (file
->f_op
!= &perf_fops
) {
2647 fput_light(file
, *fput_needed
);
2649 return ERR_PTR(-EBADF
);
2652 return file
->private_data
;
2655 static int perf_event_set_output(struct perf_event
*event
,
2656 struct perf_event
*output_event
);
2657 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
2659 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2661 struct perf_event
*event
= file
->private_data
;
2662 void (*func
)(struct perf_event
*);
2666 case PERF_EVENT_IOC_ENABLE
:
2667 func
= perf_event_enable
;
2669 case PERF_EVENT_IOC_DISABLE
:
2670 func
= perf_event_disable
;
2672 case PERF_EVENT_IOC_RESET
:
2673 func
= perf_event_reset
;
2676 case PERF_EVENT_IOC_REFRESH
:
2677 return perf_event_refresh(event
, arg
);
2679 case PERF_EVENT_IOC_PERIOD
:
2680 return perf_event_period(event
, (u64 __user
*)arg
);
2682 case PERF_EVENT_IOC_SET_OUTPUT
:
2684 struct perf_event
*output_event
= NULL
;
2685 int fput_needed
= 0;
2689 output_event
= perf_fget_light(arg
, &fput_needed
);
2690 if (IS_ERR(output_event
))
2691 return PTR_ERR(output_event
);
2694 ret
= perf_event_set_output(event
, output_event
);
2696 fput_light(output_event
->filp
, fput_needed
);
2701 case PERF_EVENT_IOC_SET_FILTER
:
2702 return perf_event_set_filter(event
, (void __user
*)arg
);
2708 if (flags
& PERF_IOC_FLAG_GROUP
)
2709 perf_event_for_each(event
, func
);
2711 perf_event_for_each_child(event
, func
);
2716 int perf_event_task_enable(void)
2718 struct perf_event
*event
;
2720 mutex_lock(¤t
->perf_event_mutex
);
2721 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2722 perf_event_for_each_child(event
, perf_event_enable
);
2723 mutex_unlock(¤t
->perf_event_mutex
);
2728 int perf_event_task_disable(void)
2730 struct perf_event
*event
;
2732 mutex_lock(¤t
->perf_event_mutex
);
2733 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2734 perf_event_for_each_child(event
, perf_event_disable
);
2735 mutex_unlock(¤t
->perf_event_mutex
);
2740 #ifndef PERF_EVENT_INDEX_OFFSET
2741 # define PERF_EVENT_INDEX_OFFSET 0
2744 static int perf_event_index(struct perf_event
*event
)
2746 if (event
->hw
.state
& PERF_HES_STOPPED
)
2749 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
2752 return event
->hw
.idx
+ 1 - PERF_EVENT_INDEX_OFFSET
;
2756 * Callers need to ensure there can be no nesting of this function, otherwise
2757 * the seqlock logic goes bad. We can not serialize this because the arch
2758 * code calls this from NMI context.
2760 void perf_event_update_userpage(struct perf_event
*event
)
2762 struct perf_event_mmap_page
*userpg
;
2763 struct perf_buffer
*buffer
;
2766 buffer
= rcu_dereference(event
->buffer
);
2770 userpg
= buffer
->user_page
;
2773 * Disable preemption so as to not let the corresponding user-space
2774 * spin too long if we get preempted.
2779 userpg
->index
= perf_event_index(event
);
2780 userpg
->offset
= perf_event_count(event
);
2781 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
2782 userpg
->offset
-= local64_read(&event
->hw
.prev_count
);
2784 userpg
->time_enabled
= event
->total_time_enabled
+
2785 atomic64_read(&event
->child_total_time_enabled
);
2787 userpg
->time_running
= event
->total_time_running
+
2788 atomic64_read(&event
->child_total_time_running
);
2797 static unsigned long perf_data_size(struct perf_buffer
*buffer
);
2800 perf_buffer_init(struct perf_buffer
*buffer
, long watermark
, int flags
)
2802 long max_size
= perf_data_size(buffer
);
2805 buffer
->watermark
= min(max_size
, watermark
);
2807 if (!buffer
->watermark
)
2808 buffer
->watermark
= max_size
/ 2;
2810 if (flags
& PERF_BUFFER_WRITABLE
)
2811 buffer
->writable
= 1;
2813 atomic_set(&buffer
->refcount
, 1);
2816 #ifndef CONFIG_PERF_USE_VMALLOC
2819 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2822 static struct page
*
2823 perf_mmap_to_page(struct perf_buffer
*buffer
, unsigned long pgoff
)
2825 if (pgoff
> buffer
->nr_pages
)
2829 return virt_to_page(buffer
->user_page
);
2831 return virt_to_page(buffer
->data_pages
[pgoff
- 1]);
2834 static void *perf_mmap_alloc_page(int cpu
)
2839 node
= (cpu
== -1) ? cpu
: cpu_to_node(cpu
);
2840 page
= alloc_pages_node(node
, GFP_KERNEL
| __GFP_ZERO
, 0);
2844 return page_address(page
);
2847 static struct perf_buffer
*
2848 perf_buffer_alloc(int nr_pages
, long watermark
, int cpu
, int flags
)
2850 struct perf_buffer
*buffer
;
2854 size
= sizeof(struct perf_buffer
);
2855 size
+= nr_pages
* sizeof(void *);
2857 buffer
= kzalloc(size
, GFP_KERNEL
);
2861 buffer
->user_page
= perf_mmap_alloc_page(cpu
);
2862 if (!buffer
->user_page
)
2863 goto fail_user_page
;
2865 for (i
= 0; i
< nr_pages
; i
++) {
2866 buffer
->data_pages
[i
] = perf_mmap_alloc_page(cpu
);
2867 if (!buffer
->data_pages
[i
])
2868 goto fail_data_pages
;
2871 buffer
->nr_pages
= nr_pages
;
2873 perf_buffer_init(buffer
, watermark
, flags
);
2878 for (i
--; i
>= 0; i
--)
2879 free_page((unsigned long)buffer
->data_pages
[i
]);
2881 free_page((unsigned long)buffer
->user_page
);
2890 static void perf_mmap_free_page(unsigned long addr
)
2892 struct page
*page
= virt_to_page((void *)addr
);
2894 page
->mapping
= NULL
;
2898 static void perf_buffer_free(struct perf_buffer
*buffer
)
2902 perf_mmap_free_page((unsigned long)buffer
->user_page
);
2903 for (i
= 0; i
< buffer
->nr_pages
; i
++)
2904 perf_mmap_free_page((unsigned long)buffer
->data_pages
[i
]);
2908 static inline int page_order(struct perf_buffer
*buffer
)
2916 * Back perf_mmap() with vmalloc memory.
2918 * Required for architectures that have d-cache aliasing issues.
2921 static inline int page_order(struct perf_buffer
*buffer
)
2923 return buffer
->page_order
;
2926 static struct page
*
2927 perf_mmap_to_page(struct perf_buffer
*buffer
, unsigned long pgoff
)
2929 if (pgoff
> (1UL << page_order(buffer
)))
2932 return vmalloc_to_page((void *)buffer
->user_page
+ pgoff
* PAGE_SIZE
);
2935 static void perf_mmap_unmark_page(void *addr
)
2937 struct page
*page
= vmalloc_to_page(addr
);
2939 page
->mapping
= NULL
;
2942 static void perf_buffer_free_work(struct work_struct
*work
)
2944 struct perf_buffer
*buffer
;
2948 buffer
= container_of(work
, struct perf_buffer
, work
);
2949 nr
= 1 << page_order(buffer
);
2951 base
= buffer
->user_page
;
2952 for (i
= 0; i
< nr
+ 1; i
++)
2953 perf_mmap_unmark_page(base
+ (i
* PAGE_SIZE
));
2959 static void perf_buffer_free(struct perf_buffer
*buffer
)
2961 schedule_work(&buffer
->work
);
2964 static struct perf_buffer
*
2965 perf_buffer_alloc(int nr_pages
, long watermark
, int cpu
, int flags
)
2967 struct perf_buffer
*buffer
;
2971 size
= sizeof(struct perf_buffer
);
2972 size
+= sizeof(void *);
2974 buffer
= kzalloc(size
, GFP_KERNEL
);
2978 INIT_WORK(&buffer
->work
, perf_buffer_free_work
);
2980 all_buf
= vmalloc_user((nr_pages
+ 1) * PAGE_SIZE
);
2984 buffer
->user_page
= all_buf
;
2985 buffer
->data_pages
[0] = all_buf
+ PAGE_SIZE
;
2986 buffer
->page_order
= ilog2(nr_pages
);
2987 buffer
->nr_pages
= 1;
2989 perf_buffer_init(buffer
, watermark
, flags
);
3002 static unsigned long perf_data_size(struct perf_buffer
*buffer
)
3004 return buffer
->nr_pages
<< (PAGE_SHIFT
+ page_order(buffer
));
3007 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
3009 struct perf_event
*event
= vma
->vm_file
->private_data
;
3010 struct perf_buffer
*buffer
;
3011 int ret
= VM_FAULT_SIGBUS
;
3013 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
3014 if (vmf
->pgoff
== 0)
3020 buffer
= rcu_dereference(event
->buffer
);
3024 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
3027 vmf
->page
= perf_mmap_to_page(buffer
, vmf
->pgoff
);
3031 get_page(vmf
->page
);
3032 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
3033 vmf
->page
->index
= vmf
->pgoff
;
3042 static void perf_buffer_free_rcu(struct rcu_head
*rcu_head
)
3044 struct perf_buffer
*buffer
;
3046 buffer
= container_of(rcu_head
, struct perf_buffer
, rcu_head
);
3047 perf_buffer_free(buffer
);
3050 static struct perf_buffer
*perf_buffer_get(struct perf_event
*event
)
3052 struct perf_buffer
*buffer
;
3055 buffer
= rcu_dereference(event
->buffer
);
3057 if (!atomic_inc_not_zero(&buffer
->refcount
))
3065 static void perf_buffer_put(struct perf_buffer
*buffer
)
3067 if (!atomic_dec_and_test(&buffer
->refcount
))
3070 call_rcu(&buffer
->rcu_head
, perf_buffer_free_rcu
);
3073 static void perf_mmap_open(struct vm_area_struct
*vma
)
3075 struct perf_event
*event
= vma
->vm_file
->private_data
;
3077 atomic_inc(&event
->mmap_count
);
3080 static void perf_mmap_close(struct vm_area_struct
*vma
)
3082 struct perf_event
*event
= vma
->vm_file
->private_data
;
3084 if (atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
)) {
3085 unsigned long size
= perf_data_size(event
->buffer
);
3086 struct user_struct
*user
= event
->mmap_user
;
3087 struct perf_buffer
*buffer
= event
->buffer
;
3089 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &user
->locked_vm
);
3090 vma
->vm_mm
->locked_vm
-= event
->mmap_locked
;
3091 rcu_assign_pointer(event
->buffer
, NULL
);
3092 mutex_unlock(&event
->mmap_mutex
);
3094 perf_buffer_put(buffer
);
3099 static const struct vm_operations_struct perf_mmap_vmops
= {
3100 .open
= perf_mmap_open
,
3101 .close
= perf_mmap_close
,
3102 .fault
= perf_mmap_fault
,
3103 .page_mkwrite
= perf_mmap_fault
,
3106 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
3108 struct perf_event
*event
= file
->private_data
;
3109 unsigned long user_locked
, user_lock_limit
;
3110 struct user_struct
*user
= current_user();
3111 unsigned long locked
, lock_limit
;
3112 struct perf_buffer
*buffer
;
3113 unsigned long vma_size
;
3114 unsigned long nr_pages
;
3115 long user_extra
, extra
;
3116 int ret
= 0, flags
= 0;
3119 * Don't allow mmap() of inherited per-task counters. This would
3120 * create a performance issue due to all children writing to the
3123 if (event
->cpu
== -1 && event
->attr
.inherit
)
3126 if (!(vma
->vm_flags
& VM_SHARED
))
3129 vma_size
= vma
->vm_end
- vma
->vm_start
;
3130 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
3133 * If we have buffer pages ensure they're a power-of-two number, so we
3134 * can do bitmasks instead of modulo.
3136 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
3139 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
3142 if (vma
->vm_pgoff
!= 0)
3145 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3146 mutex_lock(&event
->mmap_mutex
);
3147 if (event
->buffer
) {
3148 if (event
->buffer
->nr_pages
== nr_pages
)
3149 atomic_inc(&event
->buffer
->refcount
);
3155 user_extra
= nr_pages
+ 1;
3156 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
3159 * Increase the limit linearly with more CPUs:
3161 user_lock_limit
*= num_online_cpus();
3163 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
3166 if (user_locked
> user_lock_limit
)
3167 extra
= user_locked
- user_lock_limit
;
3169 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
3170 lock_limit
>>= PAGE_SHIFT
;
3171 locked
= vma
->vm_mm
->locked_vm
+ extra
;
3173 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
3174 !capable(CAP_IPC_LOCK
)) {
3179 WARN_ON(event
->buffer
);
3181 if (vma
->vm_flags
& VM_WRITE
)
3182 flags
|= PERF_BUFFER_WRITABLE
;
3184 buffer
= perf_buffer_alloc(nr_pages
, event
->attr
.wakeup_watermark
,
3190 rcu_assign_pointer(event
->buffer
, buffer
);
3192 atomic_long_add(user_extra
, &user
->locked_vm
);
3193 event
->mmap_locked
= extra
;
3194 event
->mmap_user
= get_current_user();
3195 vma
->vm_mm
->locked_vm
+= event
->mmap_locked
;
3199 atomic_inc(&event
->mmap_count
);
3200 mutex_unlock(&event
->mmap_mutex
);
3202 vma
->vm_flags
|= VM_RESERVED
;
3203 vma
->vm_ops
= &perf_mmap_vmops
;
3208 static int perf_fasync(int fd
, struct file
*filp
, int on
)
3210 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
3211 struct perf_event
*event
= filp
->private_data
;
3214 mutex_lock(&inode
->i_mutex
);
3215 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
3216 mutex_unlock(&inode
->i_mutex
);
3224 static const struct file_operations perf_fops
= {
3225 .llseek
= no_llseek
,
3226 .release
= perf_release
,
3229 .unlocked_ioctl
= perf_ioctl
,
3230 .compat_ioctl
= perf_ioctl
,
3232 .fasync
= perf_fasync
,
3238 * If there's data, ensure we set the poll() state and publish everything
3239 * to user-space before waking everybody up.
3242 void perf_event_wakeup(struct perf_event
*event
)
3244 wake_up_all(&event
->waitq
);
3246 if (event
->pending_kill
) {
3247 kill_fasync(&event
->fasync
, SIGIO
, event
->pending_kill
);
3248 event
->pending_kill
= 0;
3252 static void perf_pending_event(struct irq_work
*entry
)
3254 struct perf_event
*event
= container_of(entry
,
3255 struct perf_event
, pending
);
3257 if (event
->pending_disable
) {
3258 event
->pending_disable
= 0;
3259 __perf_event_disable(event
);
3262 if (event
->pending_wakeup
) {
3263 event
->pending_wakeup
= 0;
3264 perf_event_wakeup(event
);
3269 * We assume there is only KVM supporting the callbacks.
3270 * Later on, we might change it to a list if there is
3271 * another virtualization implementation supporting the callbacks.
3273 struct perf_guest_info_callbacks
*perf_guest_cbs
;
3275 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
3277 perf_guest_cbs
= cbs
;
3280 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks
);
3282 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
3284 perf_guest_cbs
= NULL
;
3287 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks
);
3292 static bool perf_output_space(struct perf_buffer
*buffer
, unsigned long tail
,
3293 unsigned long offset
, unsigned long head
)
3297 if (!buffer
->writable
)
3300 mask
= perf_data_size(buffer
) - 1;
3302 offset
= (offset
- tail
) & mask
;
3303 head
= (head
- tail
) & mask
;
3305 if ((int)(head
- offset
) < 0)
3311 static void perf_output_wakeup(struct perf_output_handle
*handle
)
3313 atomic_set(&handle
->buffer
->poll
, POLL_IN
);
3316 handle
->event
->pending_wakeup
= 1;
3317 irq_work_queue(&handle
->event
->pending
);
3319 perf_event_wakeup(handle
->event
);
3323 * We need to ensure a later event_id doesn't publish a head when a former
3324 * event isn't done writing. However since we need to deal with NMIs we
3325 * cannot fully serialize things.
3327 * We only publish the head (and generate a wakeup) when the outer-most
3330 static void perf_output_get_handle(struct perf_output_handle
*handle
)
3332 struct perf_buffer
*buffer
= handle
->buffer
;
3335 local_inc(&buffer
->nest
);
3336 handle
->wakeup
= local_read(&buffer
->wakeup
);
3339 static void perf_output_put_handle(struct perf_output_handle
*handle
)
3341 struct perf_buffer
*buffer
= handle
->buffer
;
3345 head
= local_read(&buffer
->head
);
3348 * IRQ/NMI can happen here, which means we can miss a head update.
3351 if (!local_dec_and_test(&buffer
->nest
))
3355 * Publish the known good head. Rely on the full barrier implied
3356 * by atomic_dec_and_test() order the buffer->head read and this
3359 buffer
->user_page
->data_head
= head
;
3362 * Now check if we missed an update, rely on the (compiler)
3363 * barrier in atomic_dec_and_test() to re-read buffer->head.
3365 if (unlikely(head
!= local_read(&buffer
->head
))) {
3366 local_inc(&buffer
->nest
);
3370 if (handle
->wakeup
!= local_read(&buffer
->wakeup
))
3371 perf_output_wakeup(handle
);
3377 __always_inline
void perf_output_copy(struct perf_output_handle
*handle
,
3378 const void *buf
, unsigned int len
)
3381 unsigned long size
= min_t(unsigned long, handle
->size
, len
);
3383 memcpy(handle
->addr
, buf
, size
);
3386 handle
->addr
+= size
;
3388 handle
->size
-= size
;
3389 if (!handle
->size
) {
3390 struct perf_buffer
*buffer
= handle
->buffer
;
3393 handle
->page
&= buffer
->nr_pages
- 1;
3394 handle
->addr
= buffer
->data_pages
[handle
->page
];
3395 handle
->size
= PAGE_SIZE
<< page_order(buffer
);
3400 static void __perf_event_header__init_id(struct perf_event_header
*header
,
3401 struct perf_sample_data
*data
,
3402 struct perf_event
*event
)
3404 u64 sample_type
= event
->attr
.sample_type
;
3406 data
->type
= sample_type
;
3407 header
->size
+= event
->id_header_size
;
3409 if (sample_type
& PERF_SAMPLE_TID
) {
3410 /* namespace issues */
3411 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
3412 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
3415 if (sample_type
& PERF_SAMPLE_TIME
)
3416 data
->time
= perf_clock();
3418 if (sample_type
& PERF_SAMPLE_ID
)
3419 data
->id
= primary_event_id(event
);
3421 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
3422 data
->stream_id
= event
->id
;
3424 if (sample_type
& PERF_SAMPLE_CPU
) {
3425 data
->cpu_entry
.cpu
= raw_smp_processor_id();
3426 data
->cpu_entry
.reserved
= 0;
3430 static void perf_event_header__init_id(struct perf_event_header
*header
,
3431 struct perf_sample_data
*data
,
3432 struct perf_event
*event
)
3434 if (event
->attr
.sample_id_all
)
3435 __perf_event_header__init_id(header
, data
, event
);
3438 static void __perf_event__output_id_sample(struct perf_output_handle
*handle
,
3439 struct perf_sample_data
*data
)
3441 u64 sample_type
= data
->type
;
3443 if (sample_type
& PERF_SAMPLE_TID
)
3444 perf_output_put(handle
, data
->tid_entry
);
3446 if (sample_type
& PERF_SAMPLE_TIME
)
3447 perf_output_put(handle
, data
->time
);
3449 if (sample_type
& PERF_SAMPLE_ID
)
3450 perf_output_put(handle
, data
->id
);
3452 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
3453 perf_output_put(handle
, data
->stream_id
);
3455 if (sample_type
& PERF_SAMPLE_CPU
)
3456 perf_output_put(handle
, data
->cpu_entry
);
3459 static void perf_event__output_id_sample(struct perf_event
*event
,
3460 struct perf_output_handle
*handle
,
3461 struct perf_sample_data
*sample
)
3463 if (event
->attr
.sample_id_all
)
3464 __perf_event__output_id_sample(handle
, sample
);
3467 int perf_output_begin(struct perf_output_handle
*handle
,
3468 struct perf_event
*event
, unsigned int size
,
3469 int nmi
, int sample
)
3471 struct perf_buffer
*buffer
;
3472 unsigned long tail
, offset
, head
;
3474 struct perf_sample_data sample_data
;
3476 struct perf_event_header header
;
3483 * For inherited events we send all the output towards the parent.
3486 event
= event
->parent
;
3488 buffer
= rcu_dereference(event
->buffer
);
3492 handle
->buffer
= buffer
;
3493 handle
->event
= event
;
3495 handle
->sample
= sample
;
3497 if (!buffer
->nr_pages
)
3500 have_lost
= local_read(&buffer
->lost
);
3502 lost_event
.header
.size
= sizeof(lost_event
);
3503 perf_event_header__init_id(&lost_event
.header
, &sample_data
,
3505 size
+= lost_event
.header
.size
;
3508 perf_output_get_handle(handle
);
3512 * Userspace could choose to issue a mb() before updating the
3513 * tail pointer. So that all reads will be completed before the
3516 tail
= ACCESS_ONCE(buffer
->user_page
->data_tail
);
3518 offset
= head
= local_read(&buffer
->head
);
3520 if (unlikely(!perf_output_space(buffer
, tail
, offset
, head
)))
3522 } while (local_cmpxchg(&buffer
->head
, offset
, head
) != offset
);
3524 if (head
- local_read(&buffer
->wakeup
) > buffer
->watermark
)
3525 local_add(buffer
->watermark
, &buffer
->wakeup
);
3527 handle
->page
= offset
>> (PAGE_SHIFT
+ page_order(buffer
));
3528 handle
->page
&= buffer
->nr_pages
- 1;
3529 handle
->size
= offset
& ((PAGE_SIZE
<< page_order(buffer
)) - 1);
3530 handle
->addr
= buffer
->data_pages
[handle
->page
];
3531 handle
->addr
+= handle
->size
;
3532 handle
->size
= (PAGE_SIZE
<< page_order(buffer
)) - handle
->size
;
3535 lost_event
.header
.type
= PERF_RECORD_LOST
;
3536 lost_event
.header
.misc
= 0;
3537 lost_event
.id
= event
->id
;
3538 lost_event
.lost
= local_xchg(&buffer
->lost
, 0);
3540 perf_output_put(handle
, lost_event
);
3541 perf_event__output_id_sample(event
, handle
, &sample_data
);
3547 local_inc(&buffer
->lost
);
3548 perf_output_put_handle(handle
);
3555 void perf_output_end(struct perf_output_handle
*handle
)
3557 struct perf_event
*event
= handle
->event
;
3558 struct perf_buffer
*buffer
= handle
->buffer
;
3560 int wakeup_events
= event
->attr
.wakeup_events
;
3562 if (handle
->sample
&& wakeup_events
) {
3563 int events
= local_inc_return(&buffer
->events
);
3564 if (events
>= wakeup_events
) {
3565 local_sub(wakeup_events
, &buffer
->events
);
3566 local_inc(&buffer
->wakeup
);
3570 perf_output_put_handle(handle
);
3574 static void perf_output_read_one(struct perf_output_handle
*handle
,
3575 struct perf_event
*event
,
3576 u64 enabled
, u64 running
)
3578 u64 read_format
= event
->attr
.read_format
;
3582 values
[n
++] = perf_event_count(event
);
3583 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
3584 values
[n
++] = enabled
+
3585 atomic64_read(&event
->child_total_time_enabled
);
3587 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
3588 values
[n
++] = running
+
3589 atomic64_read(&event
->child_total_time_running
);
3591 if (read_format
& PERF_FORMAT_ID
)
3592 values
[n
++] = primary_event_id(event
);
3594 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3598 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3600 static void perf_output_read_group(struct perf_output_handle
*handle
,
3601 struct perf_event
*event
,
3602 u64 enabled
, u64 running
)
3604 struct perf_event
*leader
= event
->group_leader
, *sub
;
3605 u64 read_format
= event
->attr
.read_format
;
3609 values
[n
++] = 1 + leader
->nr_siblings
;
3611 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3612 values
[n
++] = enabled
;
3614 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3615 values
[n
++] = running
;
3617 if (leader
!= event
)
3618 leader
->pmu
->read(leader
);
3620 values
[n
++] = perf_event_count(leader
);
3621 if (read_format
& PERF_FORMAT_ID
)
3622 values
[n
++] = primary_event_id(leader
);
3624 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3626 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3630 sub
->pmu
->read(sub
);
3632 values
[n
++] = perf_event_count(sub
);
3633 if (read_format
& PERF_FORMAT_ID
)
3634 values
[n
++] = primary_event_id(sub
);
3636 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3640 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
3641 PERF_FORMAT_TOTAL_TIME_RUNNING)
3643 static void perf_output_read(struct perf_output_handle
*handle
,
3644 struct perf_event
*event
)
3646 u64 enabled
= 0, running
= 0, now
, ctx_time
;
3647 u64 read_format
= event
->attr
.read_format
;
3650 * compute total_time_enabled, total_time_running
3651 * based on snapshot values taken when the event
3652 * was last scheduled in.
3654 * we cannot simply called update_context_time()
3655 * because of locking issue as we are called in
3658 if (read_format
& PERF_FORMAT_TOTAL_TIMES
) {
3660 ctx_time
= event
->shadow_ctx_time
+ now
;
3661 enabled
= ctx_time
- event
->tstamp_enabled
;
3662 running
= ctx_time
- event
->tstamp_running
;
3665 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
3666 perf_output_read_group(handle
, event
, enabled
, running
);
3668 perf_output_read_one(handle
, event
, enabled
, running
);
3671 void perf_output_sample(struct perf_output_handle
*handle
,
3672 struct perf_event_header
*header
,
3673 struct perf_sample_data
*data
,
3674 struct perf_event
*event
)
3676 u64 sample_type
= data
->type
;
3678 perf_output_put(handle
, *header
);
3680 if (sample_type
& PERF_SAMPLE_IP
)
3681 perf_output_put(handle
, data
->ip
);
3683 if (sample_type
& PERF_SAMPLE_TID
)
3684 perf_output_put(handle
, data
->tid_entry
);
3686 if (sample_type
& PERF_SAMPLE_TIME
)
3687 perf_output_put(handle
, data
->time
);
3689 if (sample_type
& PERF_SAMPLE_ADDR
)
3690 perf_output_put(handle
, data
->addr
);
3692 if (sample_type
& PERF_SAMPLE_ID
)
3693 perf_output_put(handle
, data
->id
);
3695 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
3696 perf_output_put(handle
, data
->stream_id
);
3698 if (sample_type
& PERF_SAMPLE_CPU
)
3699 perf_output_put(handle
, data
->cpu_entry
);
3701 if (sample_type
& PERF_SAMPLE_PERIOD
)
3702 perf_output_put(handle
, data
->period
);
3704 if (sample_type
& PERF_SAMPLE_READ
)
3705 perf_output_read(handle
, event
);
3707 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3708 if (data
->callchain
) {
3711 if (data
->callchain
)
3712 size
+= data
->callchain
->nr
;
3714 size
*= sizeof(u64
);
3716 perf_output_copy(handle
, data
->callchain
, size
);
3719 perf_output_put(handle
, nr
);
3723 if (sample_type
& PERF_SAMPLE_RAW
) {
3725 perf_output_put(handle
, data
->raw
->size
);
3726 perf_output_copy(handle
, data
->raw
->data
,
3733 .size
= sizeof(u32
),
3736 perf_output_put(handle
, raw
);
3741 void perf_prepare_sample(struct perf_event_header
*header
,
3742 struct perf_sample_data
*data
,
3743 struct perf_event
*event
,
3744 struct pt_regs
*regs
)
3746 u64 sample_type
= event
->attr
.sample_type
;
3748 header
->type
= PERF_RECORD_SAMPLE
;
3749 header
->size
= sizeof(*header
) + event
->header_size
;
3752 header
->misc
|= perf_misc_flags(regs
);
3754 __perf_event_header__init_id(header
, data
, event
);
3756 if (sample_type
& PERF_SAMPLE_IP
)
3757 data
->ip
= perf_instruction_pointer(regs
);
3759 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3762 data
->callchain
= perf_callchain(regs
);
3764 if (data
->callchain
)
3765 size
+= data
->callchain
->nr
;
3767 header
->size
+= size
* sizeof(u64
);
3770 if (sample_type
& PERF_SAMPLE_RAW
) {
3771 int size
= sizeof(u32
);
3774 size
+= data
->raw
->size
;
3776 size
+= sizeof(u32
);
3778 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
3779 header
->size
+= size
;
3783 static void perf_event_output(struct perf_event
*event
, int nmi
,
3784 struct perf_sample_data
*data
,
3785 struct pt_regs
*regs
)
3787 struct perf_output_handle handle
;
3788 struct perf_event_header header
;
3790 /* protect the callchain buffers */
3793 perf_prepare_sample(&header
, data
, event
, regs
);
3795 if (perf_output_begin(&handle
, event
, header
.size
, nmi
, 1))
3798 perf_output_sample(&handle
, &header
, data
, event
);
3800 perf_output_end(&handle
);
3810 struct perf_read_event
{
3811 struct perf_event_header header
;
3818 perf_event_read_event(struct perf_event
*event
,
3819 struct task_struct
*task
)
3821 struct perf_output_handle handle
;
3822 struct perf_sample_data sample
;
3823 struct perf_read_event read_event
= {
3825 .type
= PERF_RECORD_READ
,
3827 .size
= sizeof(read_event
) + event
->read_size
,
3829 .pid
= perf_event_pid(event
, task
),
3830 .tid
= perf_event_tid(event
, task
),
3834 perf_event_header__init_id(&read_event
.header
, &sample
, event
);
3835 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
, 0, 0);
3839 perf_output_put(&handle
, read_event
);
3840 perf_output_read(&handle
, event
);
3841 perf_event__output_id_sample(event
, &handle
, &sample
);
3843 perf_output_end(&handle
);
3847 * task tracking -- fork/exit
3849 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
3852 struct perf_task_event
{
3853 struct task_struct
*task
;
3854 struct perf_event_context
*task_ctx
;
3857 struct perf_event_header header
;
3867 static void perf_event_task_output(struct perf_event
*event
,
3868 struct perf_task_event
*task_event
)
3870 struct perf_output_handle handle
;
3871 struct perf_sample_data sample
;
3872 struct task_struct
*task
= task_event
->task
;
3873 int ret
, size
= task_event
->event_id
.header
.size
;
3875 perf_event_header__init_id(&task_event
->event_id
.header
, &sample
, event
);
3877 ret
= perf_output_begin(&handle
, event
,
3878 task_event
->event_id
.header
.size
, 0, 0);
3882 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
3883 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
3885 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
3886 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
3888 perf_output_put(&handle
, task_event
->event_id
);
3890 perf_event__output_id_sample(event
, &handle
, &sample
);
3892 perf_output_end(&handle
);
3894 task_event
->event_id
.header
.size
= size
;
3897 static int perf_event_task_match(struct perf_event
*event
)
3899 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3902 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3905 if (event
->attr
.comm
|| event
->attr
.mmap
||
3906 event
->attr
.mmap_data
|| event
->attr
.task
)
3912 static void perf_event_task_ctx(struct perf_event_context
*ctx
,
3913 struct perf_task_event
*task_event
)
3915 struct perf_event
*event
;
3917 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3918 if (perf_event_task_match(event
))
3919 perf_event_task_output(event
, task_event
);
3923 static void perf_event_task_event(struct perf_task_event
*task_event
)
3925 struct perf_cpu_context
*cpuctx
;
3926 struct perf_event_context
*ctx
;
3931 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
3932 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
3933 if (cpuctx
->active_pmu
!= pmu
)
3935 perf_event_task_ctx(&cpuctx
->ctx
, task_event
);
3937 ctx
= task_event
->task_ctx
;
3939 ctxn
= pmu
->task_ctx_nr
;
3942 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
3945 perf_event_task_ctx(ctx
, task_event
);
3947 put_cpu_ptr(pmu
->pmu_cpu_context
);
3952 static void perf_event_task(struct task_struct
*task
,
3953 struct perf_event_context
*task_ctx
,
3956 struct perf_task_event task_event
;
3958 if (!atomic_read(&nr_comm_events
) &&
3959 !atomic_read(&nr_mmap_events
) &&
3960 !atomic_read(&nr_task_events
))
3963 task_event
= (struct perf_task_event
){
3965 .task_ctx
= task_ctx
,
3968 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
3970 .size
= sizeof(task_event
.event_id
),
3976 .time
= perf_clock(),
3980 perf_event_task_event(&task_event
);
3983 void perf_event_fork(struct task_struct
*task
)
3985 perf_event_task(task
, NULL
, 1);
3992 struct perf_comm_event
{
3993 struct task_struct
*task
;
3998 struct perf_event_header header
;
4005 static void perf_event_comm_output(struct perf_event
*event
,
4006 struct perf_comm_event
*comm_event
)
4008 struct perf_output_handle handle
;
4009 struct perf_sample_data sample
;
4010 int size
= comm_event
->event_id
.header
.size
;
4013 perf_event_header__init_id(&comm_event
->event_id
.header
, &sample
, event
);
4014 ret
= perf_output_begin(&handle
, event
,
4015 comm_event
->event_id
.header
.size
, 0, 0);
4020 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
4021 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
4023 perf_output_put(&handle
, comm_event
->event_id
);
4024 perf_output_copy(&handle
, comm_event
->comm
,
4025 comm_event
->comm_size
);
4027 perf_event__output_id_sample(event
, &handle
, &sample
);
4029 perf_output_end(&handle
);
4031 comm_event
->event_id
.header
.size
= size
;
4034 static int perf_event_comm_match(struct perf_event
*event
)
4036 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
4039 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
4042 if (event
->attr
.comm
)
4048 static void perf_event_comm_ctx(struct perf_event_context
*ctx
,
4049 struct perf_comm_event
*comm_event
)
4051 struct perf_event
*event
;
4053 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
4054 if (perf_event_comm_match(event
))
4055 perf_event_comm_output(event
, comm_event
);
4059 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
4061 struct perf_cpu_context
*cpuctx
;
4062 struct perf_event_context
*ctx
;
4063 char comm
[TASK_COMM_LEN
];
4068 memset(comm
, 0, sizeof(comm
));
4069 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
4070 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
4072 comm_event
->comm
= comm
;
4073 comm_event
->comm_size
= size
;
4075 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
4077 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
4078 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
4079 if (cpuctx
->active_pmu
!= pmu
)
4081 perf_event_comm_ctx(&cpuctx
->ctx
, comm_event
);
4083 ctxn
= pmu
->task_ctx_nr
;
4087 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
4089 perf_event_comm_ctx(ctx
, comm_event
);
4091 put_cpu_ptr(pmu
->pmu_cpu_context
);
4096 void perf_event_comm(struct task_struct
*task
)
4098 struct perf_comm_event comm_event
;
4099 struct perf_event_context
*ctx
;
4102 for_each_task_context_nr(ctxn
) {
4103 ctx
= task
->perf_event_ctxp
[ctxn
];
4107 perf_event_enable_on_exec(ctx
);
4110 if (!atomic_read(&nr_comm_events
))
4113 comm_event
= (struct perf_comm_event
){
4119 .type
= PERF_RECORD_COMM
,
4128 perf_event_comm_event(&comm_event
);
4135 struct perf_mmap_event
{
4136 struct vm_area_struct
*vma
;
4138 const char *file_name
;
4142 struct perf_event_header header
;
4152 static void perf_event_mmap_output(struct perf_event
*event
,
4153 struct perf_mmap_event
*mmap_event
)
4155 struct perf_output_handle handle
;
4156 struct perf_sample_data sample
;
4157 int size
= mmap_event
->event_id
.header
.size
;
4160 perf_event_header__init_id(&mmap_event
->event_id
.header
, &sample
, event
);
4161 ret
= perf_output_begin(&handle
, event
,
4162 mmap_event
->event_id
.header
.size
, 0, 0);
4166 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
4167 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
4169 perf_output_put(&handle
, mmap_event
->event_id
);
4170 perf_output_copy(&handle
, mmap_event
->file_name
,
4171 mmap_event
->file_size
);
4173 perf_event__output_id_sample(event
, &handle
, &sample
);
4175 perf_output_end(&handle
);
4177 mmap_event
->event_id
.header
.size
= size
;
4180 static int perf_event_mmap_match(struct perf_event
*event
,
4181 struct perf_mmap_event
*mmap_event
,
4184 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
4187 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
4190 if ((!executable
&& event
->attr
.mmap_data
) ||
4191 (executable
&& event
->attr
.mmap
))
4197 static void perf_event_mmap_ctx(struct perf_event_context
*ctx
,
4198 struct perf_mmap_event
*mmap_event
,
4201 struct perf_event
*event
;
4203 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
4204 if (perf_event_mmap_match(event
, mmap_event
, executable
))
4205 perf_event_mmap_output(event
, mmap_event
);
4209 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
4211 struct perf_cpu_context
*cpuctx
;
4212 struct perf_event_context
*ctx
;
4213 struct vm_area_struct
*vma
= mmap_event
->vma
;
4214 struct file
*file
= vma
->vm_file
;
4222 memset(tmp
, 0, sizeof(tmp
));
4226 * d_path works from the end of the buffer backwards, so we
4227 * need to add enough zero bytes after the string to handle
4228 * the 64bit alignment we do later.
4230 buf
= kzalloc(PATH_MAX
+ sizeof(u64
), GFP_KERNEL
);
4232 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
4235 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
4237 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
4241 if (arch_vma_name(mmap_event
->vma
)) {
4242 name
= strncpy(tmp
, arch_vma_name(mmap_event
->vma
),
4248 name
= strncpy(tmp
, "[vdso]", sizeof(tmp
));
4250 } else if (vma
->vm_start
<= vma
->vm_mm
->start_brk
&&
4251 vma
->vm_end
>= vma
->vm_mm
->brk
) {
4252 name
= strncpy(tmp
, "[heap]", sizeof(tmp
));
4254 } else if (vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
4255 vma
->vm_end
>= vma
->vm_mm
->start_stack
) {
4256 name
= strncpy(tmp
, "[stack]", sizeof(tmp
));
4260 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
4265 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
4267 mmap_event
->file_name
= name
;
4268 mmap_event
->file_size
= size
;
4270 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
4273 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
4274 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
4275 if (cpuctx
->active_pmu
!= pmu
)
4277 perf_event_mmap_ctx(&cpuctx
->ctx
, mmap_event
,
4278 vma
->vm_flags
& VM_EXEC
);
4280 ctxn
= pmu
->task_ctx_nr
;
4284 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
4286 perf_event_mmap_ctx(ctx
, mmap_event
,
4287 vma
->vm_flags
& VM_EXEC
);
4290 put_cpu_ptr(pmu
->pmu_cpu_context
);
4297 void perf_event_mmap(struct vm_area_struct
*vma
)
4299 struct perf_mmap_event mmap_event
;
4301 if (!atomic_read(&nr_mmap_events
))
4304 mmap_event
= (struct perf_mmap_event
){
4310 .type
= PERF_RECORD_MMAP
,
4311 .misc
= PERF_RECORD_MISC_USER
,
4316 .start
= vma
->vm_start
,
4317 .len
= vma
->vm_end
- vma
->vm_start
,
4318 .pgoff
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
,
4322 perf_event_mmap_event(&mmap_event
);
4326 * IRQ throttle logging
4329 static void perf_log_throttle(struct perf_event
*event
, int enable
)
4331 struct perf_output_handle handle
;
4332 struct perf_sample_data sample
;
4336 struct perf_event_header header
;
4340 } throttle_event
= {
4342 .type
= PERF_RECORD_THROTTLE
,
4344 .size
= sizeof(throttle_event
),
4346 .time
= perf_clock(),
4347 .id
= primary_event_id(event
),
4348 .stream_id
= event
->id
,
4352 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
4354 perf_event_header__init_id(&throttle_event
.header
, &sample
, event
);
4356 ret
= perf_output_begin(&handle
, event
,
4357 throttle_event
.header
.size
, 1, 0);
4361 perf_output_put(&handle
, throttle_event
);
4362 perf_event__output_id_sample(event
, &handle
, &sample
);
4363 perf_output_end(&handle
);
4367 * Generic event overflow handling, sampling.
4370 static int __perf_event_overflow(struct perf_event
*event
, int nmi
,
4371 int throttle
, struct perf_sample_data
*data
,
4372 struct pt_regs
*regs
)
4374 int events
= atomic_read(&event
->event_limit
);
4375 struct hw_perf_event
*hwc
= &event
->hw
;
4379 * Non-sampling counters might still use the PMI to fold short
4380 * hardware counters, ignore those.
4382 if (unlikely(!is_sampling_event(event
)))
4388 if (hwc
->interrupts
!= MAX_INTERRUPTS
) {
4390 if (HZ
* hwc
->interrupts
>
4391 (u64
)sysctl_perf_event_sample_rate
) {
4392 hwc
->interrupts
= MAX_INTERRUPTS
;
4393 perf_log_throttle(event
, 0);
4398 * Keep re-disabling events even though on the previous
4399 * pass we disabled it - just in case we raced with a
4400 * sched-in and the event got enabled again:
4406 if (event
->attr
.freq
) {
4407 u64 now
= perf_clock();
4408 s64 delta
= now
- hwc
->freq_time_stamp
;
4410 hwc
->freq_time_stamp
= now
;
4412 if (delta
> 0 && delta
< 2*TICK_NSEC
)
4413 perf_adjust_period(event
, delta
, hwc
->last_period
);
4417 * XXX event_limit might not quite work as expected on inherited
4421 event
->pending_kill
= POLL_IN
;
4422 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
4424 event
->pending_kill
= POLL_HUP
;
4426 event
->pending_disable
= 1;
4427 irq_work_queue(&event
->pending
);
4429 perf_event_disable(event
);
4432 if (event
->overflow_handler
)
4433 event
->overflow_handler(event
, nmi
, data
, regs
);
4435 perf_event_output(event
, nmi
, data
, regs
);
4440 int perf_event_overflow(struct perf_event
*event
, int nmi
,
4441 struct perf_sample_data
*data
,
4442 struct pt_regs
*regs
)
4444 return __perf_event_overflow(event
, nmi
, 1, data
, regs
);
4448 * Generic software event infrastructure
4451 struct swevent_htable
{
4452 struct swevent_hlist
*swevent_hlist
;
4453 struct mutex hlist_mutex
;
4456 /* Recursion avoidance in each contexts */
4457 int recursion
[PERF_NR_CONTEXTS
];
4460 static DEFINE_PER_CPU(struct swevent_htable
, swevent_htable
);
4463 * We directly increment event->count and keep a second value in
4464 * event->hw.period_left to count intervals. This period event
4465 * is kept in the range [-sample_period, 0] so that we can use the
4469 static u64
perf_swevent_set_period(struct perf_event
*event
)
4471 struct hw_perf_event
*hwc
= &event
->hw
;
4472 u64 period
= hwc
->last_period
;
4476 hwc
->last_period
= hwc
->sample_period
;
4479 old
= val
= local64_read(&hwc
->period_left
);
4483 nr
= div64_u64(period
+ val
, period
);
4484 offset
= nr
* period
;
4486 if (local64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
4492 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
4493 int nmi
, struct perf_sample_data
*data
,
4494 struct pt_regs
*regs
)
4496 struct hw_perf_event
*hwc
= &event
->hw
;
4499 data
->period
= event
->hw
.last_period
;
4501 overflow
= perf_swevent_set_period(event
);
4503 if (hwc
->interrupts
== MAX_INTERRUPTS
)
4506 for (; overflow
; overflow
--) {
4507 if (__perf_event_overflow(event
, nmi
, throttle
,
4510 * We inhibit the overflow from happening when
4511 * hwc->interrupts == MAX_INTERRUPTS.
4519 static void perf_swevent_event(struct perf_event
*event
, u64 nr
,
4520 int nmi
, struct perf_sample_data
*data
,
4521 struct pt_regs
*regs
)
4523 struct hw_perf_event
*hwc
= &event
->hw
;
4525 local64_add(nr
, &event
->count
);
4530 if (!is_sampling_event(event
))
4533 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
4534 return perf_swevent_overflow(event
, 1, nmi
, data
, regs
);
4536 if (local64_add_negative(nr
, &hwc
->period_left
))
4539 perf_swevent_overflow(event
, 0, nmi
, data
, regs
);
4542 static int perf_exclude_event(struct perf_event
*event
,
4543 struct pt_regs
*regs
)
4545 if (event
->hw
.state
& PERF_HES_STOPPED
)
4549 if (event
->attr
.exclude_user
&& user_mode(regs
))
4552 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
4559 static int perf_swevent_match(struct perf_event
*event
,
4560 enum perf_type_id type
,
4562 struct perf_sample_data
*data
,
4563 struct pt_regs
*regs
)
4565 if (event
->attr
.type
!= type
)
4568 if (event
->attr
.config
!= event_id
)
4571 if (perf_exclude_event(event
, regs
))
4577 static inline u64
swevent_hash(u64 type
, u32 event_id
)
4579 u64 val
= event_id
| (type
<< 32);
4581 return hash_64(val
, SWEVENT_HLIST_BITS
);
4584 static inline struct hlist_head
*
4585 __find_swevent_head(struct swevent_hlist
*hlist
, u64 type
, u32 event_id
)
4587 u64 hash
= swevent_hash(type
, event_id
);
4589 return &hlist
->heads
[hash
];
4592 /* For the read side: events when they trigger */
4593 static inline struct hlist_head
*
4594 find_swevent_head_rcu(struct swevent_htable
*swhash
, u64 type
, u32 event_id
)
4596 struct swevent_hlist
*hlist
;
4598 hlist
= rcu_dereference(swhash
->swevent_hlist
);
4602 return __find_swevent_head(hlist
, type
, event_id
);
4605 /* For the event head insertion and removal in the hlist */
4606 static inline struct hlist_head
*
4607 find_swevent_head(struct swevent_htable
*swhash
, struct perf_event
*event
)
4609 struct swevent_hlist
*hlist
;
4610 u32 event_id
= event
->attr
.config
;
4611 u64 type
= event
->attr
.type
;
4614 * Event scheduling is always serialized against hlist allocation
4615 * and release. Which makes the protected version suitable here.
4616 * The context lock guarantees that.
4618 hlist
= rcu_dereference_protected(swhash
->swevent_hlist
,
4619 lockdep_is_held(&event
->ctx
->lock
));
4623 return __find_swevent_head(hlist
, type
, event_id
);
4626 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
4628 struct perf_sample_data
*data
,
4629 struct pt_regs
*regs
)
4631 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
4632 struct perf_event
*event
;
4633 struct hlist_node
*node
;
4634 struct hlist_head
*head
;
4637 head
= find_swevent_head_rcu(swhash
, type
, event_id
);
4641 hlist_for_each_entry_rcu(event
, node
, head
, hlist_entry
) {
4642 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
4643 perf_swevent_event(event
, nr
, nmi
, data
, regs
);
4649 int perf_swevent_get_recursion_context(void)
4651 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
4653 return get_recursion_context(swhash
->recursion
);
4655 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
4657 void inline perf_swevent_put_recursion_context(int rctx
)
4659 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
4661 put_recursion_context(swhash
->recursion
, rctx
);
4664 void __perf_sw_event(u32 event_id
, u64 nr
, int nmi
,
4665 struct pt_regs
*regs
, u64 addr
)
4667 struct perf_sample_data data
;
4670 preempt_disable_notrace();
4671 rctx
= perf_swevent_get_recursion_context();
4675 perf_sample_data_init(&data
, addr
);
4677 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, nmi
, &data
, regs
);
4679 perf_swevent_put_recursion_context(rctx
);
4680 preempt_enable_notrace();
4683 static void perf_swevent_read(struct perf_event
*event
)
4687 static int perf_swevent_add(struct perf_event
*event
, int flags
)
4689 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
4690 struct hw_perf_event
*hwc
= &event
->hw
;
4691 struct hlist_head
*head
;
4693 if (is_sampling_event(event
)) {
4694 hwc
->last_period
= hwc
->sample_period
;
4695 perf_swevent_set_period(event
);
4698 hwc
->state
= !(flags
& PERF_EF_START
);
4700 head
= find_swevent_head(swhash
, event
);
4701 if (WARN_ON_ONCE(!head
))
4704 hlist_add_head_rcu(&event
->hlist_entry
, head
);
4709 static void perf_swevent_del(struct perf_event
*event
, int flags
)
4711 hlist_del_rcu(&event
->hlist_entry
);
4714 static void perf_swevent_start(struct perf_event
*event
, int flags
)
4716 event
->hw
.state
= 0;
4719 static void perf_swevent_stop(struct perf_event
*event
, int flags
)
4721 event
->hw
.state
= PERF_HES_STOPPED
;
4724 /* Deref the hlist from the update side */
4725 static inline struct swevent_hlist
*
4726 swevent_hlist_deref(struct swevent_htable
*swhash
)
4728 return rcu_dereference_protected(swhash
->swevent_hlist
,
4729 lockdep_is_held(&swhash
->hlist_mutex
));
4732 static void swevent_hlist_release_rcu(struct rcu_head
*rcu_head
)
4734 struct swevent_hlist
*hlist
;
4736 hlist
= container_of(rcu_head
, struct swevent_hlist
, rcu_head
);
4740 static void swevent_hlist_release(struct swevent_htable
*swhash
)
4742 struct swevent_hlist
*hlist
= swevent_hlist_deref(swhash
);
4747 rcu_assign_pointer(swhash
->swevent_hlist
, NULL
);
4748 call_rcu(&hlist
->rcu_head
, swevent_hlist_release_rcu
);
4751 static void swevent_hlist_put_cpu(struct perf_event
*event
, int cpu
)
4753 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
4755 mutex_lock(&swhash
->hlist_mutex
);
4757 if (!--swhash
->hlist_refcount
)
4758 swevent_hlist_release(swhash
);
4760 mutex_unlock(&swhash
->hlist_mutex
);
4763 static void swevent_hlist_put(struct perf_event
*event
)
4767 if (event
->cpu
!= -1) {
4768 swevent_hlist_put_cpu(event
, event
->cpu
);
4772 for_each_possible_cpu(cpu
)
4773 swevent_hlist_put_cpu(event
, cpu
);
4776 static int swevent_hlist_get_cpu(struct perf_event
*event
, int cpu
)
4778 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
4781 mutex_lock(&swhash
->hlist_mutex
);
4783 if (!swevent_hlist_deref(swhash
) && cpu_online(cpu
)) {
4784 struct swevent_hlist
*hlist
;
4786 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
4791 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
4793 swhash
->hlist_refcount
++;
4795 mutex_unlock(&swhash
->hlist_mutex
);
4800 static int swevent_hlist_get(struct perf_event
*event
)
4803 int cpu
, failed_cpu
;
4805 if (event
->cpu
!= -1)
4806 return swevent_hlist_get_cpu(event
, event
->cpu
);
4809 for_each_possible_cpu(cpu
) {
4810 err
= swevent_hlist_get_cpu(event
, cpu
);
4820 for_each_possible_cpu(cpu
) {
4821 if (cpu
== failed_cpu
)
4823 swevent_hlist_put_cpu(event
, cpu
);
4830 atomic_t perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
4832 static void sw_perf_event_destroy(struct perf_event
*event
)
4834 u64 event_id
= event
->attr
.config
;
4836 WARN_ON(event
->parent
);
4838 jump_label_dec(&perf_swevent_enabled
[event_id
]);
4839 swevent_hlist_put(event
);
4842 static int perf_swevent_init(struct perf_event
*event
)
4844 int event_id
= event
->attr
.config
;
4846 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
4850 case PERF_COUNT_SW_CPU_CLOCK
:
4851 case PERF_COUNT_SW_TASK_CLOCK
:
4858 if (event_id
>= PERF_COUNT_SW_MAX
)
4861 if (!event
->parent
) {
4864 err
= swevent_hlist_get(event
);
4868 jump_label_inc(&perf_swevent_enabled
[event_id
]);
4869 event
->destroy
= sw_perf_event_destroy
;
4875 static struct pmu perf_swevent
= {
4876 .task_ctx_nr
= perf_sw_context
,
4878 .event_init
= perf_swevent_init
,
4879 .add
= perf_swevent_add
,
4880 .del
= perf_swevent_del
,
4881 .start
= perf_swevent_start
,
4882 .stop
= perf_swevent_stop
,
4883 .read
= perf_swevent_read
,
4886 #ifdef CONFIG_EVENT_TRACING
4888 static int perf_tp_filter_match(struct perf_event
*event
,
4889 struct perf_sample_data
*data
)
4891 void *record
= data
->raw
->data
;
4893 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
4898 static int perf_tp_event_match(struct perf_event
*event
,
4899 struct perf_sample_data
*data
,
4900 struct pt_regs
*regs
)
4903 * All tracepoints are from kernel-space.
4905 if (event
->attr
.exclude_kernel
)
4908 if (!perf_tp_filter_match(event
, data
))
4914 void perf_tp_event(u64 addr
, u64 count
, void *record
, int entry_size
,
4915 struct pt_regs
*regs
, struct hlist_head
*head
, int rctx
)
4917 struct perf_sample_data data
;
4918 struct perf_event
*event
;
4919 struct hlist_node
*node
;
4921 struct perf_raw_record raw
= {
4926 perf_sample_data_init(&data
, addr
);
4929 hlist_for_each_entry_rcu(event
, node
, head
, hlist_entry
) {
4930 if (perf_tp_event_match(event
, &data
, regs
))
4931 perf_swevent_event(event
, count
, 1, &data
, regs
);
4934 perf_swevent_put_recursion_context(rctx
);
4936 EXPORT_SYMBOL_GPL(perf_tp_event
);
4938 static void tp_perf_event_destroy(struct perf_event
*event
)
4940 perf_trace_destroy(event
);
4943 static int perf_tp_event_init(struct perf_event
*event
)
4947 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
4950 err
= perf_trace_init(event
);
4954 event
->destroy
= tp_perf_event_destroy
;
4959 static struct pmu perf_tracepoint
= {
4960 .task_ctx_nr
= perf_sw_context
,
4962 .event_init
= perf_tp_event_init
,
4963 .add
= perf_trace_add
,
4964 .del
= perf_trace_del
,
4965 .start
= perf_swevent_start
,
4966 .stop
= perf_swevent_stop
,
4967 .read
= perf_swevent_read
,
4970 static inline void perf_tp_register(void)
4972 perf_pmu_register(&perf_tracepoint
, "tracepoint", PERF_TYPE_TRACEPOINT
);
4975 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
4980 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
4983 filter_str
= strndup_user(arg
, PAGE_SIZE
);
4984 if (IS_ERR(filter_str
))
4985 return PTR_ERR(filter_str
);
4987 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
, filter_str
);
4993 static void perf_event_free_filter(struct perf_event
*event
)
4995 ftrace_profile_free_filter(event
);
5000 static inline void perf_tp_register(void)
5004 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
5009 static void perf_event_free_filter(struct perf_event
*event
)
5013 #endif /* CONFIG_EVENT_TRACING */
5015 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5016 void perf_bp_event(struct perf_event
*bp
, void *data
)
5018 struct perf_sample_data sample
;
5019 struct pt_regs
*regs
= data
;
5021 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
);
5023 if (!bp
->hw
.state
&& !perf_exclude_event(bp
, regs
))
5024 perf_swevent_event(bp
, 1, 1, &sample
, regs
);
5029 * hrtimer based swevent callback
5032 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
5034 enum hrtimer_restart ret
= HRTIMER_RESTART
;
5035 struct perf_sample_data data
;
5036 struct pt_regs
*regs
;
5037 struct perf_event
*event
;
5040 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
5041 event
->pmu
->read(event
);
5043 perf_sample_data_init(&data
, 0);
5044 data
.period
= event
->hw
.last_period
;
5045 regs
= get_irq_regs();
5047 if (regs
&& !perf_exclude_event(event
, regs
)) {
5048 if (!(event
->attr
.exclude_idle
&& current
->pid
== 0))
5049 if (perf_event_overflow(event
, 0, &data
, regs
))
5050 ret
= HRTIMER_NORESTART
;
5053 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
5054 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
5059 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
5061 struct hw_perf_event
*hwc
= &event
->hw
;
5064 if (!is_sampling_event(event
))
5067 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
5068 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
5070 period
= local64_read(&hwc
->period_left
);
5075 local64_set(&hwc
->period_left
, 0);
5077 period
= max_t(u64
, 10000, hwc
->sample_period
);
5079 __hrtimer_start_range_ns(&hwc
->hrtimer
,
5080 ns_to_ktime(period
), 0,
5081 HRTIMER_MODE_REL_PINNED
, 0);
5084 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
5086 struct hw_perf_event
*hwc
= &event
->hw
;
5088 if (is_sampling_event(event
)) {
5089 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
5090 local64_set(&hwc
->period_left
, ktime_to_ns(remaining
));
5092 hrtimer_cancel(&hwc
->hrtimer
);
5097 * Software event: cpu wall time clock
5100 static void cpu_clock_event_update(struct perf_event
*event
)
5105 now
= local_clock();
5106 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
5107 local64_add(now
- prev
, &event
->count
);
5110 static void cpu_clock_event_start(struct perf_event
*event
, int flags
)
5112 local64_set(&event
->hw
.prev_count
, local_clock());
5113 perf_swevent_start_hrtimer(event
);
5116 static void cpu_clock_event_stop(struct perf_event
*event
, int flags
)
5118 perf_swevent_cancel_hrtimer(event
);
5119 cpu_clock_event_update(event
);
5122 static int cpu_clock_event_add(struct perf_event
*event
, int flags
)
5124 if (flags
& PERF_EF_START
)
5125 cpu_clock_event_start(event
, flags
);
5130 static void cpu_clock_event_del(struct perf_event
*event
, int flags
)
5132 cpu_clock_event_stop(event
, flags
);
5135 static void cpu_clock_event_read(struct perf_event
*event
)
5137 cpu_clock_event_update(event
);
5140 static int cpu_clock_event_init(struct perf_event
*event
)
5142 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
5145 if (event
->attr
.config
!= PERF_COUNT_SW_CPU_CLOCK
)
5151 static struct pmu perf_cpu_clock
= {
5152 .task_ctx_nr
= perf_sw_context
,
5154 .event_init
= cpu_clock_event_init
,
5155 .add
= cpu_clock_event_add
,
5156 .del
= cpu_clock_event_del
,
5157 .start
= cpu_clock_event_start
,
5158 .stop
= cpu_clock_event_stop
,
5159 .read
= cpu_clock_event_read
,
5163 * Software event: task time clock
5166 static void task_clock_event_update(struct perf_event
*event
, u64 now
)
5171 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
5173 local64_add(delta
, &event
->count
);
5176 static void task_clock_event_start(struct perf_event
*event
, int flags
)
5178 local64_set(&event
->hw
.prev_count
, event
->ctx
->time
);
5179 perf_swevent_start_hrtimer(event
);
5182 static void task_clock_event_stop(struct perf_event
*event
, int flags
)
5184 perf_swevent_cancel_hrtimer(event
);
5185 task_clock_event_update(event
, event
->ctx
->time
);
5188 static int task_clock_event_add(struct perf_event
*event
, int flags
)
5190 if (flags
& PERF_EF_START
)
5191 task_clock_event_start(event
, flags
);
5196 static void task_clock_event_del(struct perf_event
*event
, int flags
)
5198 task_clock_event_stop(event
, PERF_EF_UPDATE
);
5201 static void task_clock_event_read(struct perf_event
*event
)
5206 update_context_time(event
->ctx
);
5207 time
= event
->ctx
->time
;
5209 u64 now
= perf_clock();
5210 u64 delta
= now
- event
->ctx
->timestamp
;
5211 time
= event
->ctx
->time
+ delta
;
5214 task_clock_event_update(event
, time
);
5217 static int task_clock_event_init(struct perf_event
*event
)
5219 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
5222 if (event
->attr
.config
!= PERF_COUNT_SW_TASK_CLOCK
)
5228 static struct pmu perf_task_clock
= {
5229 .task_ctx_nr
= perf_sw_context
,
5231 .event_init
= task_clock_event_init
,
5232 .add
= task_clock_event_add
,
5233 .del
= task_clock_event_del
,
5234 .start
= task_clock_event_start
,
5235 .stop
= task_clock_event_stop
,
5236 .read
= task_clock_event_read
,
5239 static void perf_pmu_nop_void(struct pmu
*pmu
)
5243 static int perf_pmu_nop_int(struct pmu
*pmu
)
5248 static void perf_pmu_start_txn(struct pmu
*pmu
)
5250 perf_pmu_disable(pmu
);
5253 static int perf_pmu_commit_txn(struct pmu
*pmu
)
5255 perf_pmu_enable(pmu
);
5259 static void perf_pmu_cancel_txn(struct pmu
*pmu
)
5261 perf_pmu_enable(pmu
);
5265 * Ensures all contexts with the same task_ctx_nr have the same
5266 * pmu_cpu_context too.
5268 static void *find_pmu_context(int ctxn
)
5275 list_for_each_entry(pmu
, &pmus
, entry
) {
5276 if (pmu
->task_ctx_nr
== ctxn
)
5277 return pmu
->pmu_cpu_context
;
5283 static void update_pmu_context(struct pmu
*pmu
, struct pmu
*old_pmu
)
5287 for_each_possible_cpu(cpu
) {
5288 struct perf_cpu_context
*cpuctx
;
5290 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
5292 if (cpuctx
->active_pmu
== old_pmu
)
5293 cpuctx
->active_pmu
= pmu
;
5297 static void free_pmu_context(struct pmu
*pmu
)
5301 mutex_lock(&pmus_lock
);
5303 * Like a real lame refcount.
5305 list_for_each_entry(i
, &pmus
, entry
) {
5306 if (i
->pmu_cpu_context
== pmu
->pmu_cpu_context
) {
5307 update_pmu_context(i
, pmu
);
5312 free_percpu(pmu
->pmu_cpu_context
);
5314 mutex_unlock(&pmus_lock
);
5316 static struct idr pmu_idr
;
5319 type_show(struct device
*dev
, struct device_attribute
*attr
, char *page
)
5321 struct pmu
*pmu
= dev_get_drvdata(dev
);
5323 return snprintf(page
, PAGE_SIZE
-1, "%d\n", pmu
->type
);
5326 static struct device_attribute pmu_dev_attrs
[] = {
5331 static int pmu_bus_running
;
5332 static struct bus_type pmu_bus
= {
5333 .name
= "event_source",
5334 .dev_attrs
= pmu_dev_attrs
,
5337 static void pmu_dev_release(struct device
*dev
)
5342 static int pmu_dev_alloc(struct pmu
*pmu
)
5346 pmu
->dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
5350 device_initialize(pmu
->dev
);
5351 ret
= dev_set_name(pmu
->dev
, "%s", pmu
->name
);
5355 dev_set_drvdata(pmu
->dev
, pmu
);
5356 pmu
->dev
->bus
= &pmu_bus
;
5357 pmu
->dev
->release
= pmu_dev_release
;
5358 ret
= device_add(pmu
->dev
);
5366 put_device(pmu
->dev
);
5370 int perf_pmu_register(struct pmu
*pmu
, char *name
, int type
)
5374 mutex_lock(&pmus_lock
);
5376 pmu
->pmu_disable_count
= alloc_percpu(int);
5377 if (!pmu
->pmu_disable_count
)
5386 int err
= idr_pre_get(&pmu_idr
, GFP_KERNEL
);
5390 err
= idr_get_new_above(&pmu_idr
, pmu
, PERF_TYPE_MAX
, &type
);
5398 if (pmu_bus_running
) {
5399 ret
= pmu_dev_alloc(pmu
);
5405 pmu
->pmu_cpu_context
= find_pmu_context(pmu
->task_ctx_nr
);
5406 if (pmu
->pmu_cpu_context
)
5407 goto got_cpu_context
;
5409 pmu
->pmu_cpu_context
= alloc_percpu(struct perf_cpu_context
);
5410 if (!pmu
->pmu_cpu_context
)
5413 for_each_possible_cpu(cpu
) {
5414 struct perf_cpu_context
*cpuctx
;
5416 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
5417 __perf_event_init_context(&cpuctx
->ctx
);
5418 cpuctx
->ctx
.type
= cpu_context
;
5419 cpuctx
->ctx
.pmu
= pmu
;
5420 cpuctx
->jiffies_interval
= 1;
5421 INIT_LIST_HEAD(&cpuctx
->rotation_list
);
5422 cpuctx
->active_pmu
= pmu
;
5426 if (!pmu
->start_txn
) {
5427 if (pmu
->pmu_enable
) {
5429 * If we have pmu_enable/pmu_disable calls, install
5430 * transaction stubs that use that to try and batch
5431 * hardware accesses.
5433 pmu
->start_txn
= perf_pmu_start_txn
;
5434 pmu
->commit_txn
= perf_pmu_commit_txn
;
5435 pmu
->cancel_txn
= perf_pmu_cancel_txn
;
5437 pmu
->start_txn
= perf_pmu_nop_void
;
5438 pmu
->commit_txn
= perf_pmu_nop_int
;
5439 pmu
->cancel_txn
= perf_pmu_nop_void
;
5443 if (!pmu
->pmu_enable
) {
5444 pmu
->pmu_enable
= perf_pmu_nop_void
;
5445 pmu
->pmu_disable
= perf_pmu_nop_void
;
5448 list_add_rcu(&pmu
->entry
, &pmus
);
5451 mutex_unlock(&pmus_lock
);
5456 device_del(pmu
->dev
);
5457 put_device(pmu
->dev
);
5460 if (pmu
->type
>= PERF_TYPE_MAX
)
5461 idr_remove(&pmu_idr
, pmu
->type
);
5464 free_percpu(pmu
->pmu_disable_count
);
5468 void perf_pmu_unregister(struct pmu
*pmu
)
5470 mutex_lock(&pmus_lock
);
5471 list_del_rcu(&pmu
->entry
);
5472 mutex_unlock(&pmus_lock
);
5475 * We dereference the pmu list under both SRCU and regular RCU, so
5476 * synchronize against both of those.
5478 synchronize_srcu(&pmus_srcu
);
5481 free_percpu(pmu
->pmu_disable_count
);
5482 if (pmu
->type
>= PERF_TYPE_MAX
)
5483 idr_remove(&pmu_idr
, pmu
->type
);
5484 device_del(pmu
->dev
);
5485 put_device(pmu
->dev
);
5486 free_pmu_context(pmu
);
5489 struct pmu
*perf_init_event(struct perf_event
*event
)
5491 struct pmu
*pmu
= NULL
;
5494 idx
= srcu_read_lock(&pmus_srcu
);
5497 pmu
= idr_find(&pmu_idr
, event
->attr
.type
);
5502 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
5503 int ret
= pmu
->event_init(event
);
5507 if (ret
!= -ENOENT
) {
5512 pmu
= ERR_PTR(-ENOENT
);
5514 srcu_read_unlock(&pmus_srcu
, idx
);
5520 * Allocate and initialize a event structure
5522 static struct perf_event
*
5523 perf_event_alloc(struct perf_event_attr
*attr
, int cpu
,
5524 struct task_struct
*task
,
5525 struct perf_event
*group_leader
,
5526 struct perf_event
*parent_event
,
5527 perf_overflow_handler_t overflow_handler
)
5530 struct perf_event
*event
;
5531 struct hw_perf_event
*hwc
;
5534 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
5536 return ERR_PTR(-ENOMEM
);
5539 * Single events are their own group leaders, with an
5540 * empty sibling list:
5543 group_leader
= event
;
5545 mutex_init(&event
->child_mutex
);
5546 INIT_LIST_HEAD(&event
->child_list
);
5548 INIT_LIST_HEAD(&event
->group_entry
);
5549 INIT_LIST_HEAD(&event
->event_entry
);
5550 INIT_LIST_HEAD(&event
->sibling_list
);
5551 init_waitqueue_head(&event
->waitq
);
5552 init_irq_work(&event
->pending
, perf_pending_event
);
5554 mutex_init(&event
->mmap_mutex
);
5557 event
->attr
= *attr
;
5558 event
->group_leader
= group_leader
;
5562 event
->parent
= parent_event
;
5564 event
->ns
= get_pid_ns(current
->nsproxy
->pid_ns
);
5565 event
->id
= atomic64_inc_return(&perf_event_id
);
5567 event
->state
= PERF_EVENT_STATE_INACTIVE
;
5570 event
->attach_state
= PERF_ATTACH_TASK
;
5571 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5573 * hw_breakpoint is a bit difficult here..
5575 if (attr
->type
== PERF_TYPE_BREAKPOINT
)
5576 event
->hw
.bp_target
= task
;
5580 if (!overflow_handler
&& parent_event
)
5581 overflow_handler
= parent_event
->overflow_handler
;
5583 event
->overflow_handler
= overflow_handler
;
5586 event
->state
= PERF_EVENT_STATE_OFF
;
5591 hwc
->sample_period
= attr
->sample_period
;
5592 if (attr
->freq
&& attr
->sample_freq
)
5593 hwc
->sample_period
= 1;
5594 hwc
->last_period
= hwc
->sample_period
;
5596 local64_set(&hwc
->period_left
, hwc
->sample_period
);
5599 * we currently do not support PERF_FORMAT_GROUP on inherited events
5601 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
5604 pmu
= perf_init_event(event
);
5610 else if (IS_ERR(pmu
))
5615 put_pid_ns(event
->ns
);
5617 return ERR_PTR(err
);
5622 if (!event
->parent
) {
5623 if (event
->attach_state
& PERF_ATTACH_TASK
)
5624 jump_label_inc(&perf_task_events
);
5625 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
5626 atomic_inc(&nr_mmap_events
);
5627 if (event
->attr
.comm
)
5628 atomic_inc(&nr_comm_events
);
5629 if (event
->attr
.task
)
5630 atomic_inc(&nr_task_events
);
5631 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
5632 err
= get_callchain_buffers();
5635 return ERR_PTR(err
);
5643 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
5644 struct perf_event_attr
*attr
)
5649 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
5653 * zero the full structure, so that a short copy will be nice.
5655 memset(attr
, 0, sizeof(*attr
));
5657 ret
= get_user(size
, &uattr
->size
);
5661 if (size
> PAGE_SIZE
) /* silly large */
5664 if (!size
) /* abi compat */
5665 size
= PERF_ATTR_SIZE_VER0
;
5667 if (size
< PERF_ATTR_SIZE_VER0
)
5671 * If we're handed a bigger struct than we know of,
5672 * ensure all the unknown bits are 0 - i.e. new
5673 * user-space does not rely on any kernel feature
5674 * extensions we dont know about yet.
5676 if (size
> sizeof(*attr
)) {
5677 unsigned char __user
*addr
;
5678 unsigned char __user
*end
;
5681 addr
= (void __user
*)uattr
+ sizeof(*attr
);
5682 end
= (void __user
*)uattr
+ size
;
5684 for (; addr
< end
; addr
++) {
5685 ret
= get_user(val
, addr
);
5691 size
= sizeof(*attr
);
5694 ret
= copy_from_user(attr
, uattr
, size
);
5699 * If the type exists, the corresponding creation will verify
5702 if (attr
->type
>= PERF_TYPE_MAX
)
5705 if (attr
->__reserved_1
)
5708 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
5711 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
5718 put_user(sizeof(*attr
), &uattr
->size
);
5724 perf_event_set_output(struct perf_event
*event
, struct perf_event
*output_event
)
5726 struct perf_buffer
*buffer
= NULL
, *old_buffer
= NULL
;
5732 /* don't allow circular references */
5733 if (event
== output_event
)
5737 * Don't allow cross-cpu buffers
5739 if (output_event
->cpu
!= event
->cpu
)
5743 * If its not a per-cpu buffer, it must be the same task.
5745 if (output_event
->cpu
== -1 && output_event
->ctx
!= event
->ctx
)
5749 mutex_lock(&event
->mmap_mutex
);
5750 /* Can't redirect output if we've got an active mmap() */
5751 if (atomic_read(&event
->mmap_count
))
5755 /* get the buffer we want to redirect to */
5756 buffer
= perf_buffer_get(output_event
);
5761 old_buffer
= event
->buffer
;
5762 rcu_assign_pointer(event
->buffer
, buffer
);
5765 mutex_unlock(&event
->mmap_mutex
);
5768 perf_buffer_put(old_buffer
);
5774 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5776 * @attr_uptr: event_id type attributes for monitoring/sampling
5779 * @group_fd: group leader event fd
5781 SYSCALL_DEFINE5(perf_event_open
,
5782 struct perf_event_attr __user
*, attr_uptr
,
5783 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
5785 struct perf_event
*group_leader
= NULL
, *output_event
= NULL
;
5786 struct perf_event
*event
, *sibling
;
5787 struct perf_event_attr attr
;
5788 struct perf_event_context
*ctx
;
5789 struct file
*event_file
= NULL
;
5790 struct file
*group_file
= NULL
;
5791 struct task_struct
*task
= NULL
;
5795 int fput_needed
= 0;
5798 /* for future expandability... */
5799 if (flags
& ~(PERF_FLAG_FD_NO_GROUP
| PERF_FLAG_FD_OUTPUT
))
5802 err
= perf_copy_attr(attr_uptr
, &attr
);
5806 if (!attr
.exclude_kernel
) {
5807 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
5812 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
5816 event_fd
= get_unused_fd_flags(O_RDWR
);
5820 if (group_fd
!= -1) {
5821 group_leader
= perf_fget_light(group_fd
, &fput_needed
);
5822 if (IS_ERR(group_leader
)) {
5823 err
= PTR_ERR(group_leader
);
5826 group_file
= group_leader
->filp
;
5827 if (flags
& PERF_FLAG_FD_OUTPUT
)
5828 output_event
= group_leader
;
5829 if (flags
& PERF_FLAG_FD_NO_GROUP
)
5830 group_leader
= NULL
;
5834 task
= find_lively_task_by_vpid(pid
);
5836 err
= PTR_ERR(task
);
5841 event
= perf_event_alloc(&attr
, cpu
, task
, group_leader
, NULL
, NULL
);
5842 if (IS_ERR(event
)) {
5843 err
= PTR_ERR(event
);
5848 * Special case software events and allow them to be part of
5849 * any hardware group.
5854 (is_software_event(event
) != is_software_event(group_leader
))) {
5855 if (is_software_event(event
)) {
5857 * If event and group_leader are not both a software
5858 * event, and event is, then group leader is not.
5860 * Allow the addition of software events to !software
5861 * groups, this is safe because software events never
5864 pmu
= group_leader
->pmu
;
5865 } else if (is_software_event(group_leader
) &&
5866 (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
)) {
5868 * In case the group is a pure software group, and we
5869 * try to add a hardware event, move the whole group to
5870 * the hardware context.
5877 * Get the target context (task or percpu):
5879 ctx
= find_get_context(pmu
, task
, cpu
);
5886 * Look up the group leader (we will attach this event to it):
5892 * Do not allow a recursive hierarchy (this new sibling
5893 * becoming part of another group-sibling):
5895 if (group_leader
->group_leader
!= group_leader
)
5898 * Do not allow to attach to a group in a different
5899 * task or CPU context:
5902 if (group_leader
->ctx
->type
!= ctx
->type
)
5905 if (group_leader
->ctx
!= ctx
)
5910 * Only a group leader can be exclusive or pinned
5912 if (attr
.exclusive
|| attr
.pinned
)
5917 err
= perf_event_set_output(event
, output_event
);
5922 event_file
= anon_inode_getfile("[perf_event]", &perf_fops
, event
, O_RDWR
);
5923 if (IS_ERR(event_file
)) {
5924 err
= PTR_ERR(event_file
);
5929 struct perf_event_context
*gctx
= group_leader
->ctx
;
5931 mutex_lock(&gctx
->mutex
);
5932 perf_event_remove_from_context(group_leader
);
5933 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
5935 perf_event_remove_from_context(sibling
);
5938 mutex_unlock(&gctx
->mutex
);
5942 event
->filp
= event_file
;
5943 WARN_ON_ONCE(ctx
->parent_ctx
);
5944 mutex_lock(&ctx
->mutex
);
5947 perf_install_in_context(ctx
, group_leader
, cpu
);
5949 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
5951 perf_install_in_context(ctx
, sibling
, cpu
);
5956 perf_install_in_context(ctx
, event
, cpu
);
5958 mutex_unlock(&ctx
->mutex
);
5960 event
->owner
= current
;
5962 mutex_lock(¤t
->perf_event_mutex
);
5963 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
5964 mutex_unlock(¤t
->perf_event_mutex
);
5967 * Precalculate sample_data sizes
5969 perf_event__header_size(event
);
5970 perf_event__id_header_size(event
);
5973 * Drop the reference on the group_event after placing the
5974 * new event on the sibling_list. This ensures destruction
5975 * of the group leader will find the pointer to itself in
5976 * perf_group_detach().
5978 fput_light(group_file
, fput_needed
);
5979 fd_install(event_fd
, event_file
);
5988 put_task_struct(task
);
5990 fput_light(group_file
, fput_needed
);
5992 put_unused_fd(event_fd
);
5997 * perf_event_create_kernel_counter
5999 * @attr: attributes of the counter to create
6000 * @cpu: cpu in which the counter is bound
6001 * @task: task to profile (NULL for percpu)
6004 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
6005 struct task_struct
*task
,
6006 perf_overflow_handler_t overflow_handler
)
6008 struct perf_event_context
*ctx
;
6009 struct perf_event
*event
;
6013 * Get the target context (task or percpu):
6016 event
= perf_event_alloc(attr
, cpu
, task
, NULL
, NULL
, overflow_handler
);
6017 if (IS_ERR(event
)) {
6018 err
= PTR_ERR(event
);
6022 ctx
= find_get_context(event
->pmu
, task
, cpu
);
6029 WARN_ON_ONCE(ctx
->parent_ctx
);
6030 mutex_lock(&ctx
->mutex
);
6031 perf_install_in_context(ctx
, event
, cpu
);
6033 mutex_unlock(&ctx
->mutex
);
6040 return ERR_PTR(err
);
6042 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
6044 static void sync_child_event(struct perf_event
*child_event
,
6045 struct task_struct
*child
)
6047 struct perf_event
*parent_event
= child_event
->parent
;
6050 if (child_event
->attr
.inherit_stat
)
6051 perf_event_read_event(child_event
, child
);
6053 child_val
= perf_event_count(child_event
);
6056 * Add back the child's count to the parent's count:
6058 atomic64_add(child_val
, &parent_event
->child_count
);
6059 atomic64_add(child_event
->total_time_enabled
,
6060 &parent_event
->child_total_time_enabled
);
6061 atomic64_add(child_event
->total_time_running
,
6062 &parent_event
->child_total_time_running
);
6065 * Remove this event from the parent's list
6067 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
6068 mutex_lock(&parent_event
->child_mutex
);
6069 list_del_init(&child_event
->child_list
);
6070 mutex_unlock(&parent_event
->child_mutex
);
6073 * Release the parent event, if this was the last
6076 fput(parent_event
->filp
);
6080 __perf_event_exit_task(struct perf_event
*child_event
,
6081 struct perf_event_context
*child_ctx
,
6082 struct task_struct
*child
)
6084 struct perf_event
*parent_event
;
6086 perf_event_remove_from_context(child_event
);
6088 parent_event
= child_event
->parent
;
6090 * It can happen that parent exits first, and has events
6091 * that are still around due to the child reference. These
6092 * events need to be zapped - but otherwise linger.
6095 sync_child_event(child_event
, child
);
6096 free_event(child_event
);
6100 static void perf_event_exit_task_context(struct task_struct
*child
, int ctxn
)
6102 struct perf_event
*child_event
, *tmp
;
6103 struct perf_event_context
*child_ctx
;
6104 unsigned long flags
;
6106 if (likely(!child
->perf_event_ctxp
[ctxn
])) {
6107 perf_event_task(child
, NULL
, 0);
6111 local_irq_save(flags
);
6113 * We can't reschedule here because interrupts are disabled,
6114 * and either child is current or it is a task that can't be
6115 * scheduled, so we are now safe from rescheduling changing
6118 child_ctx
= child
->perf_event_ctxp
[ctxn
];
6119 task_ctx_sched_out(child_ctx
, EVENT_ALL
);
6122 * Take the context lock here so that if find_get_context is
6123 * reading child->perf_event_ctxp, we wait until it has
6124 * incremented the context's refcount before we do put_ctx below.
6126 raw_spin_lock(&child_ctx
->lock
);
6127 child
->perf_event_ctxp
[ctxn
] = NULL
;
6129 * If this context is a clone; unclone it so it can't get
6130 * swapped to another process while we're removing all
6131 * the events from it.
6133 unclone_ctx(child_ctx
);
6134 update_context_time(child_ctx
);
6135 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
6138 * Report the task dead after unscheduling the events so that we
6139 * won't get any samples after PERF_RECORD_EXIT. We can however still
6140 * get a few PERF_RECORD_READ events.
6142 perf_event_task(child
, child_ctx
, 0);
6145 * We can recurse on the same lock type through:
6147 * __perf_event_exit_task()
6148 * sync_child_event()
6149 * fput(parent_event->filp)
6151 * mutex_lock(&ctx->mutex)
6153 * But since its the parent context it won't be the same instance.
6155 mutex_lock(&child_ctx
->mutex
);
6158 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->pinned_groups
,
6160 __perf_event_exit_task(child_event
, child_ctx
, child
);
6162 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->flexible_groups
,
6164 __perf_event_exit_task(child_event
, child_ctx
, child
);
6167 * If the last event was a group event, it will have appended all
6168 * its siblings to the list, but we obtained 'tmp' before that which
6169 * will still point to the list head terminating the iteration.
6171 if (!list_empty(&child_ctx
->pinned_groups
) ||
6172 !list_empty(&child_ctx
->flexible_groups
))
6175 mutex_unlock(&child_ctx
->mutex
);
6181 * When a child task exits, feed back event values to parent events.
6183 void perf_event_exit_task(struct task_struct
*child
)
6185 struct perf_event
*event
, *tmp
;
6188 mutex_lock(&child
->perf_event_mutex
);
6189 list_for_each_entry_safe(event
, tmp
, &child
->perf_event_list
,
6191 list_del_init(&event
->owner_entry
);
6194 * Ensure the list deletion is visible before we clear
6195 * the owner, closes a race against perf_release() where
6196 * we need to serialize on the owner->perf_event_mutex.
6199 event
->owner
= NULL
;
6201 mutex_unlock(&child
->perf_event_mutex
);
6203 for_each_task_context_nr(ctxn
)
6204 perf_event_exit_task_context(child
, ctxn
);
6207 static void perf_free_event(struct perf_event
*event
,
6208 struct perf_event_context
*ctx
)
6210 struct perf_event
*parent
= event
->parent
;
6212 if (WARN_ON_ONCE(!parent
))
6215 mutex_lock(&parent
->child_mutex
);
6216 list_del_init(&event
->child_list
);
6217 mutex_unlock(&parent
->child_mutex
);
6221 perf_group_detach(event
);
6222 list_del_event(event
, ctx
);
6227 * free an unexposed, unused context as created by inheritance by
6228 * perf_event_init_task below, used by fork() in case of fail.
6230 void perf_event_free_task(struct task_struct
*task
)
6232 struct perf_event_context
*ctx
;
6233 struct perf_event
*event
, *tmp
;
6236 for_each_task_context_nr(ctxn
) {
6237 ctx
= task
->perf_event_ctxp
[ctxn
];
6241 mutex_lock(&ctx
->mutex
);
6243 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
,
6245 perf_free_event(event
, ctx
);
6247 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
,
6249 perf_free_event(event
, ctx
);
6251 if (!list_empty(&ctx
->pinned_groups
) ||
6252 !list_empty(&ctx
->flexible_groups
))
6255 mutex_unlock(&ctx
->mutex
);
6261 void perf_event_delayed_put(struct task_struct
*task
)
6265 for_each_task_context_nr(ctxn
)
6266 WARN_ON_ONCE(task
->perf_event_ctxp
[ctxn
]);
6270 * inherit a event from parent task to child task:
6272 static struct perf_event
*
6273 inherit_event(struct perf_event
*parent_event
,
6274 struct task_struct
*parent
,
6275 struct perf_event_context
*parent_ctx
,
6276 struct task_struct
*child
,
6277 struct perf_event
*group_leader
,
6278 struct perf_event_context
*child_ctx
)
6280 struct perf_event
*child_event
;
6281 unsigned long flags
;
6284 * Instead of creating recursive hierarchies of events,
6285 * we link inherited events back to the original parent,
6286 * which has a filp for sure, which we use as the reference
6289 if (parent_event
->parent
)
6290 parent_event
= parent_event
->parent
;
6292 child_event
= perf_event_alloc(&parent_event
->attr
,
6295 group_leader
, parent_event
,
6297 if (IS_ERR(child_event
))
6302 * Make the child state follow the state of the parent event,
6303 * not its attr.disabled bit. We hold the parent's mutex,
6304 * so we won't race with perf_event_{en, dis}able_family.
6306 if (parent_event
->state
>= PERF_EVENT_STATE_INACTIVE
)
6307 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
6309 child_event
->state
= PERF_EVENT_STATE_OFF
;
6311 if (parent_event
->attr
.freq
) {
6312 u64 sample_period
= parent_event
->hw
.sample_period
;
6313 struct hw_perf_event
*hwc
= &child_event
->hw
;
6315 hwc
->sample_period
= sample_period
;
6316 hwc
->last_period
= sample_period
;
6318 local64_set(&hwc
->period_left
, sample_period
);
6321 child_event
->ctx
= child_ctx
;
6322 child_event
->overflow_handler
= parent_event
->overflow_handler
;
6325 * Precalculate sample_data sizes
6327 perf_event__header_size(child_event
);
6328 perf_event__id_header_size(child_event
);
6331 * Link it up in the child's context:
6333 raw_spin_lock_irqsave(&child_ctx
->lock
, flags
);
6334 add_event_to_ctx(child_event
, child_ctx
);
6335 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
6338 * Get a reference to the parent filp - we will fput it
6339 * when the child event exits. This is safe to do because
6340 * we are in the parent and we know that the filp still
6341 * exists and has a nonzero count:
6343 atomic_long_inc(&parent_event
->filp
->f_count
);
6346 * Link this into the parent event's child list
6348 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
6349 mutex_lock(&parent_event
->child_mutex
);
6350 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
6351 mutex_unlock(&parent_event
->child_mutex
);
6356 static int inherit_group(struct perf_event
*parent_event
,
6357 struct task_struct
*parent
,
6358 struct perf_event_context
*parent_ctx
,
6359 struct task_struct
*child
,
6360 struct perf_event_context
*child_ctx
)
6362 struct perf_event
*leader
;
6363 struct perf_event
*sub
;
6364 struct perf_event
*child_ctr
;
6366 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
6367 child
, NULL
, child_ctx
);
6369 return PTR_ERR(leader
);
6370 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
6371 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
6372 child
, leader
, child_ctx
);
6373 if (IS_ERR(child_ctr
))
6374 return PTR_ERR(child_ctr
);
6380 inherit_task_group(struct perf_event
*event
, struct task_struct
*parent
,
6381 struct perf_event_context
*parent_ctx
,
6382 struct task_struct
*child
, int ctxn
,
6386 struct perf_event_context
*child_ctx
;
6388 if (!event
->attr
.inherit
) {
6393 child_ctx
= child
->perf_event_ctxp
[ctxn
];
6396 * This is executed from the parent task context, so
6397 * inherit events that have been marked for cloning.
6398 * First allocate and initialize a context for the
6402 child_ctx
= alloc_perf_context(event
->pmu
, child
);
6406 child
->perf_event_ctxp
[ctxn
] = child_ctx
;
6409 ret
= inherit_group(event
, parent
, parent_ctx
,
6419 * Initialize the perf_event context in task_struct
6421 int perf_event_init_context(struct task_struct
*child
, int ctxn
)
6423 struct perf_event_context
*child_ctx
, *parent_ctx
;
6424 struct perf_event_context
*cloned_ctx
;
6425 struct perf_event
*event
;
6426 struct task_struct
*parent
= current
;
6427 int inherited_all
= 1;
6428 unsigned long flags
;
6431 child
->perf_event_ctxp
[ctxn
] = NULL
;
6433 mutex_init(&child
->perf_event_mutex
);
6434 INIT_LIST_HEAD(&child
->perf_event_list
);
6436 if (likely(!parent
->perf_event_ctxp
[ctxn
]))
6440 * If the parent's context is a clone, pin it so it won't get
6443 parent_ctx
= perf_pin_task_context(parent
, ctxn
);
6446 * No need to check if parent_ctx != NULL here; since we saw
6447 * it non-NULL earlier, the only reason for it to become NULL
6448 * is if we exit, and since we're currently in the middle of
6449 * a fork we can't be exiting at the same time.
6453 * Lock the parent list. No need to lock the child - not PID
6454 * hashed yet and not running, so nobody can access it.
6456 mutex_lock(&parent_ctx
->mutex
);
6459 * We dont have to disable NMIs - we are only looking at
6460 * the list, not manipulating it:
6462 list_for_each_entry(event
, &parent_ctx
->pinned_groups
, group_entry
) {
6463 ret
= inherit_task_group(event
, parent
, parent_ctx
,
6464 child
, ctxn
, &inherited_all
);
6470 * We can't hold ctx->lock when iterating the ->flexible_group list due
6471 * to allocations, but we need to prevent rotation because
6472 * rotate_ctx() will change the list from interrupt context.
6474 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
6475 parent_ctx
->rotate_disable
= 1;
6476 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
6478 list_for_each_entry(event
, &parent_ctx
->flexible_groups
, group_entry
) {
6479 ret
= inherit_task_group(event
, parent
, parent_ctx
,
6480 child
, ctxn
, &inherited_all
);
6485 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
6486 parent_ctx
->rotate_disable
= 0;
6487 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
6489 child_ctx
= child
->perf_event_ctxp
[ctxn
];
6491 if (child_ctx
&& inherited_all
) {
6493 * Mark the child context as a clone of the parent
6494 * context, or of whatever the parent is a clone of.
6495 * Note that if the parent is a clone, it could get
6496 * uncloned at any point, but that doesn't matter
6497 * because the list of events and the generation
6498 * count can't have changed since we took the mutex.
6500 cloned_ctx
= rcu_dereference(parent_ctx
->parent_ctx
);
6502 child_ctx
->parent_ctx
= cloned_ctx
;
6503 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
6505 child_ctx
->parent_ctx
= parent_ctx
;
6506 child_ctx
->parent_gen
= parent_ctx
->generation
;
6508 get_ctx(child_ctx
->parent_ctx
);
6511 mutex_unlock(&parent_ctx
->mutex
);
6513 perf_unpin_context(parent_ctx
);
6519 * Initialize the perf_event context in task_struct
6521 int perf_event_init_task(struct task_struct
*child
)
6525 for_each_task_context_nr(ctxn
) {
6526 ret
= perf_event_init_context(child
, ctxn
);
6534 static void __init
perf_event_init_all_cpus(void)
6536 struct swevent_htable
*swhash
;
6539 for_each_possible_cpu(cpu
) {
6540 swhash
= &per_cpu(swevent_htable
, cpu
);
6541 mutex_init(&swhash
->hlist_mutex
);
6542 INIT_LIST_HEAD(&per_cpu(rotation_list
, cpu
));
6546 static void __cpuinit
perf_event_init_cpu(int cpu
)
6548 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
6550 mutex_lock(&swhash
->hlist_mutex
);
6551 if (swhash
->hlist_refcount
> 0) {
6552 struct swevent_hlist
*hlist
;
6554 hlist
= kzalloc_node(sizeof(*hlist
), GFP_KERNEL
, cpu_to_node(cpu
));
6556 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
6558 mutex_unlock(&swhash
->hlist_mutex
);
6561 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
6562 static void perf_pmu_rotate_stop(struct pmu
*pmu
)
6564 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
6566 WARN_ON(!irqs_disabled());
6568 list_del_init(&cpuctx
->rotation_list
);
6571 static void __perf_event_exit_context(void *__info
)
6573 struct perf_event_context
*ctx
= __info
;
6574 struct perf_event
*event
, *tmp
;
6576 perf_pmu_rotate_stop(ctx
->pmu
);
6578 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
, group_entry
)
6579 __perf_event_remove_from_context(event
);
6580 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
, group_entry
)
6581 __perf_event_remove_from_context(event
);
6584 static void perf_event_exit_cpu_context(int cpu
)
6586 struct perf_event_context
*ctx
;
6590 idx
= srcu_read_lock(&pmus_srcu
);
6591 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
6592 ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
)->ctx
;
6594 mutex_lock(&ctx
->mutex
);
6595 smp_call_function_single(cpu
, __perf_event_exit_context
, ctx
, 1);
6596 mutex_unlock(&ctx
->mutex
);
6598 srcu_read_unlock(&pmus_srcu
, idx
);
6601 static void perf_event_exit_cpu(int cpu
)
6603 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
6605 mutex_lock(&swhash
->hlist_mutex
);
6606 swevent_hlist_release(swhash
);
6607 mutex_unlock(&swhash
->hlist_mutex
);
6609 perf_event_exit_cpu_context(cpu
);
6612 static inline void perf_event_exit_cpu(int cpu
) { }
6616 perf_reboot(struct notifier_block
*notifier
, unsigned long val
, void *v
)
6620 for_each_online_cpu(cpu
)
6621 perf_event_exit_cpu(cpu
);
6627 * Run the perf reboot notifier at the very last possible moment so that
6628 * the generic watchdog code runs as long as possible.
6630 static struct notifier_block perf_reboot_notifier
= {
6631 .notifier_call
= perf_reboot
,
6632 .priority
= INT_MIN
,
6635 static int __cpuinit
6636 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
6638 unsigned int cpu
= (long)hcpu
;
6640 switch (action
& ~CPU_TASKS_FROZEN
) {
6642 case CPU_UP_PREPARE
:
6643 case CPU_DOWN_FAILED
:
6644 perf_event_init_cpu(cpu
);
6647 case CPU_UP_CANCELED
:
6648 case CPU_DOWN_PREPARE
:
6649 perf_event_exit_cpu(cpu
);
6659 void __init
perf_event_init(void)
6665 perf_event_init_all_cpus();
6666 init_srcu_struct(&pmus_srcu
);
6667 perf_pmu_register(&perf_swevent
, "software", PERF_TYPE_SOFTWARE
);
6668 perf_pmu_register(&perf_cpu_clock
, NULL
, -1);
6669 perf_pmu_register(&perf_task_clock
, NULL
, -1);
6671 perf_cpu_notifier(perf_cpu_notify
);
6672 register_reboot_notifier(&perf_reboot_notifier
);
6674 ret
= init_hw_breakpoint();
6675 WARN(ret
, "hw_breakpoint initialization failed with: %d", ret
);
6678 static int __init
perf_event_sysfs_init(void)
6683 mutex_lock(&pmus_lock
);
6685 ret
= bus_register(&pmu_bus
);
6689 list_for_each_entry(pmu
, &pmus
, entry
) {
6690 if (!pmu
->name
|| pmu
->type
< 0)
6693 ret
= pmu_dev_alloc(pmu
);
6694 WARN(ret
, "Failed to register pmu: %s, reason %d\n", pmu
->name
, ret
);
6696 pmu_bus_running
= 1;
6700 mutex_unlock(&pmus_lock
);
6704 device_initcall(perf_event_sysfs_init
);