2 * Performance events core code:
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011 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/export.h>
29 #include <linux/vmalloc.h>
30 #include <linux/hardirq.h>
31 #include <linux/rculist.h>
32 #include <linux/uaccess.h>
33 #include <linux/syscalls.h>
34 #include <linux/anon_inodes.h>
35 #include <linux/kernel_stat.h>
36 #include <linux/perf_event.h>
37 #include <linux/ftrace_event.h>
38 #include <linux/hw_breakpoint.h>
42 #include <asm/irq_regs.h>
44 struct remote_function_call
{
45 struct task_struct
*p
;
46 int (*func
)(void *info
);
51 static void remote_function(void *data
)
53 struct remote_function_call
*tfc
= data
;
54 struct task_struct
*p
= tfc
->p
;
58 if (task_cpu(p
) != smp_processor_id() || !task_curr(p
))
62 tfc
->ret
= tfc
->func(tfc
->info
);
66 * task_function_call - call a function on the cpu on which a task runs
67 * @p: the task to evaluate
68 * @func: the function to be called
69 * @info: the function call argument
71 * Calls the function @func when the task is currently running. This might
72 * be on the current CPU, which just calls the function directly
74 * returns: @func return value, or
75 * -ESRCH - when the process isn't running
76 * -EAGAIN - when the process moved away
79 task_function_call(struct task_struct
*p
, int (*func
) (void *info
), void *info
)
81 struct remote_function_call data
= {
85 .ret
= -ESRCH
, /* No such (running) process */
89 smp_call_function_single(task_cpu(p
), remote_function
, &data
, 1);
95 * cpu_function_call - call a function on the cpu
96 * @func: the function to be called
97 * @info: the function call argument
99 * Calls the function @func on the remote cpu.
101 * returns: @func return value or -ENXIO when the cpu is offline
103 static int cpu_function_call(int cpu
, int (*func
) (void *info
), void *info
)
105 struct remote_function_call data
= {
109 .ret
= -ENXIO
, /* No such CPU */
112 smp_call_function_single(cpu
, remote_function
, &data
, 1);
117 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
118 PERF_FLAG_FD_OUTPUT |\
119 PERF_FLAG_PID_CGROUP)
122 EVENT_FLEXIBLE
= 0x1,
124 EVENT_ALL
= EVENT_FLEXIBLE
| EVENT_PINNED
,
128 * perf_sched_events : >0 events exist
129 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
131 struct jump_label_key perf_sched_events __read_mostly
;
132 static DEFINE_PER_CPU(atomic_t
, perf_cgroup_events
);
134 static atomic_t nr_mmap_events __read_mostly
;
135 static atomic_t nr_comm_events __read_mostly
;
136 static atomic_t nr_task_events __read_mostly
;
138 static LIST_HEAD(pmus
);
139 static DEFINE_MUTEX(pmus_lock
);
140 static struct srcu_struct pmus_srcu
;
143 * perf event paranoia level:
144 * -1 - not paranoid at all
145 * 0 - disallow raw tracepoint access for unpriv
146 * 1 - disallow cpu events for unpriv
147 * 2 - disallow kernel profiling for unpriv
149 int sysctl_perf_event_paranoid __read_mostly
= 1;
151 /* Minimum for 512 kiB + 1 user control page */
152 int sysctl_perf_event_mlock __read_mostly
= 512 + (PAGE_SIZE
/ 1024); /* 'free' kiB per user */
155 * max perf event sample rate
157 #define DEFAULT_MAX_SAMPLE_RATE 100000
158 int sysctl_perf_event_sample_rate __read_mostly
= DEFAULT_MAX_SAMPLE_RATE
;
159 static int max_samples_per_tick __read_mostly
=
160 DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE
, HZ
);
162 int perf_proc_update_handler(struct ctl_table
*table
, int write
,
163 void __user
*buffer
, size_t *lenp
,
166 int ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
171 max_samples_per_tick
= DIV_ROUND_UP(sysctl_perf_event_sample_rate
, HZ
);
176 static atomic64_t perf_event_id
;
178 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
179 enum event_type_t event_type
);
181 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
182 enum event_type_t event_type
,
183 struct task_struct
*task
);
185 static void update_context_time(struct perf_event_context
*ctx
);
186 static u64
perf_event_time(struct perf_event
*event
);
188 void __weak
perf_event_print_debug(void) { }
190 extern __weak
const char *perf_pmu_name(void)
195 static inline u64
perf_clock(void)
197 return local_clock();
200 static inline struct perf_cpu_context
*
201 __get_cpu_context(struct perf_event_context
*ctx
)
203 return this_cpu_ptr(ctx
->pmu
->pmu_cpu_context
);
206 static void perf_ctx_lock(struct perf_cpu_context
*cpuctx
,
207 struct perf_event_context
*ctx
)
209 raw_spin_lock(&cpuctx
->ctx
.lock
);
211 raw_spin_lock(&ctx
->lock
);
214 static void perf_ctx_unlock(struct perf_cpu_context
*cpuctx
,
215 struct perf_event_context
*ctx
)
218 raw_spin_unlock(&ctx
->lock
);
219 raw_spin_unlock(&cpuctx
->ctx
.lock
);
222 #ifdef CONFIG_CGROUP_PERF
225 * Must ensure cgroup is pinned (css_get) before calling
226 * this function. In other words, we cannot call this function
227 * if there is no cgroup event for the current CPU context.
229 static inline struct perf_cgroup
*
230 perf_cgroup_from_task(struct task_struct
*task
)
232 return container_of(task_subsys_state(task
, perf_subsys_id
),
233 struct perf_cgroup
, css
);
237 perf_cgroup_match(struct perf_event
*event
)
239 struct perf_event_context
*ctx
= event
->ctx
;
240 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
242 return !event
->cgrp
|| event
->cgrp
== cpuctx
->cgrp
;
245 static inline void perf_get_cgroup(struct perf_event
*event
)
247 css_get(&event
->cgrp
->css
);
250 static inline void perf_put_cgroup(struct perf_event
*event
)
252 css_put(&event
->cgrp
->css
);
255 static inline void perf_detach_cgroup(struct perf_event
*event
)
257 perf_put_cgroup(event
);
261 static inline int is_cgroup_event(struct perf_event
*event
)
263 return event
->cgrp
!= NULL
;
266 static inline u64
perf_cgroup_event_time(struct perf_event
*event
)
268 struct perf_cgroup_info
*t
;
270 t
= per_cpu_ptr(event
->cgrp
->info
, event
->cpu
);
274 static inline void __update_cgrp_time(struct perf_cgroup
*cgrp
)
276 struct perf_cgroup_info
*info
;
281 info
= this_cpu_ptr(cgrp
->info
);
283 info
->time
+= now
- info
->timestamp
;
284 info
->timestamp
= now
;
287 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context
*cpuctx
)
289 struct perf_cgroup
*cgrp_out
= cpuctx
->cgrp
;
291 __update_cgrp_time(cgrp_out
);
294 static inline void update_cgrp_time_from_event(struct perf_event
*event
)
296 struct perf_cgroup
*cgrp
;
299 * ensure we access cgroup data only when needed and
300 * when we know the cgroup is pinned (css_get)
302 if (!is_cgroup_event(event
))
305 cgrp
= perf_cgroup_from_task(current
);
307 * Do not update time when cgroup is not active
309 if (cgrp
== event
->cgrp
)
310 __update_cgrp_time(event
->cgrp
);
314 perf_cgroup_set_timestamp(struct task_struct
*task
,
315 struct perf_event_context
*ctx
)
317 struct perf_cgroup
*cgrp
;
318 struct perf_cgroup_info
*info
;
321 * ctx->lock held by caller
322 * ensure we do not access cgroup data
323 * unless we have the cgroup pinned (css_get)
325 if (!task
|| !ctx
->nr_cgroups
)
328 cgrp
= perf_cgroup_from_task(task
);
329 info
= this_cpu_ptr(cgrp
->info
);
330 info
->timestamp
= ctx
->timestamp
;
333 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
334 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
337 * reschedule events based on the cgroup constraint of task.
339 * mode SWOUT : schedule out everything
340 * mode SWIN : schedule in based on cgroup for next
342 void perf_cgroup_switch(struct task_struct
*task
, int mode
)
344 struct perf_cpu_context
*cpuctx
;
349 * disable interrupts to avoid geting nr_cgroup
350 * changes via __perf_event_disable(). Also
353 local_irq_save(flags
);
356 * we reschedule only in the presence of cgroup
357 * constrained events.
361 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
362 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
365 * perf_cgroup_events says at least one
366 * context on this CPU has cgroup events.
368 * ctx->nr_cgroups reports the number of cgroup
369 * events for a context.
371 if (cpuctx
->ctx
.nr_cgroups
> 0) {
372 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
373 perf_pmu_disable(cpuctx
->ctx
.pmu
);
375 if (mode
& PERF_CGROUP_SWOUT
) {
376 cpu_ctx_sched_out(cpuctx
, EVENT_ALL
);
378 * must not be done before ctxswout due
379 * to event_filter_match() in event_sched_out()
384 if (mode
& PERF_CGROUP_SWIN
) {
385 WARN_ON_ONCE(cpuctx
->cgrp
);
386 /* set cgrp before ctxsw in to
387 * allow event_filter_match() to not
388 * have to pass task around
390 cpuctx
->cgrp
= perf_cgroup_from_task(task
);
391 cpu_ctx_sched_in(cpuctx
, EVENT_ALL
, task
);
393 perf_pmu_enable(cpuctx
->ctx
.pmu
);
394 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
400 local_irq_restore(flags
);
403 static inline void perf_cgroup_sched_out(struct task_struct
*task
)
405 perf_cgroup_switch(task
, PERF_CGROUP_SWOUT
);
408 static inline void perf_cgroup_sched_in(struct task_struct
*task
)
410 perf_cgroup_switch(task
, PERF_CGROUP_SWIN
);
413 static inline int perf_cgroup_connect(int fd
, struct perf_event
*event
,
414 struct perf_event_attr
*attr
,
415 struct perf_event
*group_leader
)
417 struct perf_cgroup
*cgrp
;
418 struct cgroup_subsys_state
*css
;
420 int ret
= 0, fput_needed
;
422 file
= fget_light(fd
, &fput_needed
);
426 css
= cgroup_css_from_dir(file
, perf_subsys_id
);
432 cgrp
= container_of(css
, struct perf_cgroup
, css
);
435 /* must be done before we fput() the file */
436 perf_get_cgroup(event
);
439 * all events in a group must monitor
440 * the same cgroup because a task belongs
441 * to only one perf cgroup at a time
443 if (group_leader
&& group_leader
->cgrp
!= cgrp
) {
444 perf_detach_cgroup(event
);
448 fput_light(file
, fput_needed
);
453 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
455 struct perf_cgroup_info
*t
;
456 t
= per_cpu_ptr(event
->cgrp
->info
, event
->cpu
);
457 event
->shadow_ctx_time
= now
- t
->timestamp
;
461 perf_cgroup_defer_enabled(struct perf_event
*event
)
464 * when the current task's perf cgroup does not match
465 * the event's, we need to remember to call the
466 * perf_mark_enable() function the first time a task with
467 * a matching perf cgroup is scheduled in.
469 if (is_cgroup_event(event
) && !perf_cgroup_match(event
))
470 event
->cgrp_defer_enabled
= 1;
474 perf_cgroup_mark_enabled(struct perf_event
*event
,
475 struct perf_event_context
*ctx
)
477 struct perf_event
*sub
;
478 u64 tstamp
= perf_event_time(event
);
480 if (!event
->cgrp_defer_enabled
)
483 event
->cgrp_defer_enabled
= 0;
485 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
486 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
487 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
) {
488 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
489 sub
->cgrp_defer_enabled
= 0;
493 #else /* !CONFIG_CGROUP_PERF */
496 perf_cgroup_match(struct perf_event
*event
)
501 static inline void perf_detach_cgroup(struct perf_event
*event
)
504 static inline int is_cgroup_event(struct perf_event
*event
)
509 static inline u64
perf_cgroup_event_cgrp_time(struct perf_event
*event
)
514 static inline void update_cgrp_time_from_event(struct perf_event
*event
)
518 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context
*cpuctx
)
522 static inline void perf_cgroup_sched_out(struct task_struct
*task
)
526 static inline void perf_cgroup_sched_in(struct task_struct
*task
)
530 static inline int perf_cgroup_connect(pid_t pid
, struct perf_event
*event
,
531 struct perf_event_attr
*attr
,
532 struct perf_event
*group_leader
)
538 perf_cgroup_set_timestamp(struct task_struct
*task
,
539 struct perf_event_context
*ctx
)
544 perf_cgroup_switch(struct task_struct
*task
, struct task_struct
*next
)
549 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
553 static inline u64
perf_cgroup_event_time(struct perf_event
*event
)
559 perf_cgroup_defer_enabled(struct perf_event
*event
)
564 perf_cgroup_mark_enabled(struct perf_event
*event
,
565 struct perf_event_context
*ctx
)
570 void perf_pmu_disable(struct pmu
*pmu
)
572 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
574 pmu
->pmu_disable(pmu
);
577 void perf_pmu_enable(struct pmu
*pmu
)
579 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
581 pmu
->pmu_enable(pmu
);
584 static DEFINE_PER_CPU(struct list_head
, rotation_list
);
587 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
588 * because they're strictly cpu affine and rotate_start is called with IRQs
589 * disabled, while rotate_context is called from IRQ context.
591 static void perf_pmu_rotate_start(struct pmu
*pmu
)
593 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
594 struct list_head
*head
= &__get_cpu_var(rotation_list
);
596 WARN_ON(!irqs_disabled());
598 if (list_empty(&cpuctx
->rotation_list
))
599 list_add(&cpuctx
->rotation_list
, head
);
602 static void get_ctx(struct perf_event_context
*ctx
)
604 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
607 static void put_ctx(struct perf_event_context
*ctx
)
609 if (atomic_dec_and_test(&ctx
->refcount
)) {
611 put_ctx(ctx
->parent_ctx
);
613 put_task_struct(ctx
->task
);
614 kfree_rcu(ctx
, rcu_head
);
618 static void unclone_ctx(struct perf_event_context
*ctx
)
620 if (ctx
->parent_ctx
) {
621 put_ctx(ctx
->parent_ctx
);
622 ctx
->parent_ctx
= NULL
;
626 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
629 * only top level events have the pid namespace they were created in
632 event
= event
->parent
;
634 return task_tgid_nr_ns(p
, event
->ns
);
637 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
640 * only top level events have the pid namespace they were created in
643 event
= event
->parent
;
645 return task_pid_nr_ns(p
, event
->ns
);
649 * If we inherit events we want to return the parent event id
652 static u64
primary_event_id(struct perf_event
*event
)
657 id
= event
->parent
->id
;
663 * Get the perf_event_context for a task and lock it.
664 * This has to cope with with the fact that until it is locked,
665 * the context could get moved to another task.
667 static struct perf_event_context
*
668 perf_lock_task_context(struct task_struct
*task
, int ctxn
, unsigned long *flags
)
670 struct perf_event_context
*ctx
;
674 ctx
= rcu_dereference(task
->perf_event_ctxp
[ctxn
]);
677 * If this context is a clone of another, it might
678 * get swapped for another underneath us by
679 * perf_event_task_sched_out, though the
680 * rcu_read_lock() protects us from any context
681 * getting freed. Lock the context and check if it
682 * got swapped before we could get the lock, and retry
683 * if so. If we locked the right context, then it
684 * can't get swapped on us any more.
686 raw_spin_lock_irqsave(&ctx
->lock
, *flags
);
687 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
[ctxn
])) {
688 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
692 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
693 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
702 * Get the context for a task and increment its pin_count so it
703 * can't get swapped to another task. This also increments its
704 * reference count so that the context can't get freed.
706 static struct perf_event_context
*
707 perf_pin_task_context(struct task_struct
*task
, int ctxn
)
709 struct perf_event_context
*ctx
;
712 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
715 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
720 static void perf_unpin_context(struct perf_event_context
*ctx
)
724 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
726 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
730 * Update the record of the current time in a context.
732 static void update_context_time(struct perf_event_context
*ctx
)
734 u64 now
= perf_clock();
736 ctx
->time
+= now
- ctx
->timestamp
;
737 ctx
->timestamp
= now
;
740 static u64
perf_event_time(struct perf_event
*event
)
742 struct perf_event_context
*ctx
= event
->ctx
;
744 if (is_cgroup_event(event
))
745 return perf_cgroup_event_time(event
);
747 return ctx
? ctx
->time
: 0;
751 * Update the total_time_enabled and total_time_running fields for a event.
752 * The caller of this function needs to hold the ctx->lock.
754 static void update_event_times(struct perf_event
*event
)
756 struct perf_event_context
*ctx
= event
->ctx
;
759 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
760 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
763 * in cgroup mode, time_enabled represents
764 * the time the event was enabled AND active
765 * tasks were in the monitored cgroup. This is
766 * independent of the activity of the context as
767 * there may be a mix of cgroup and non-cgroup events.
769 * That is why we treat cgroup events differently
772 if (is_cgroup_event(event
))
773 run_end
= perf_event_time(event
);
774 else if (ctx
->is_active
)
777 run_end
= event
->tstamp_stopped
;
779 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
781 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
782 run_end
= event
->tstamp_stopped
;
784 run_end
= perf_event_time(event
);
786 event
->total_time_running
= run_end
- event
->tstamp_running
;
791 * Update total_time_enabled and total_time_running for all events in a group.
793 static void update_group_times(struct perf_event
*leader
)
795 struct perf_event
*event
;
797 update_event_times(leader
);
798 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
799 update_event_times(event
);
802 static struct list_head
*
803 ctx_group_list(struct perf_event
*event
, struct perf_event_context
*ctx
)
805 if (event
->attr
.pinned
)
806 return &ctx
->pinned_groups
;
808 return &ctx
->flexible_groups
;
812 * Add a event from the lists for its context.
813 * Must be called with ctx->mutex and ctx->lock held.
816 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
818 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_CONTEXT
);
819 event
->attach_state
|= PERF_ATTACH_CONTEXT
;
822 * If we're a stand alone event or group leader, we go to the context
823 * list, group events are kept attached to the group so that
824 * perf_group_detach can, at all times, locate all siblings.
826 if (event
->group_leader
== event
) {
827 struct list_head
*list
;
829 if (is_software_event(event
))
830 event
->group_flags
|= PERF_GROUP_SOFTWARE
;
832 list
= ctx_group_list(event
, ctx
);
833 list_add_tail(&event
->group_entry
, list
);
836 if (is_cgroup_event(event
))
839 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
841 perf_pmu_rotate_start(ctx
->pmu
);
843 if (event
->attr
.inherit_stat
)
848 * Called at perf_event creation and when events are attached/detached from a
851 static void perf_event__read_size(struct perf_event
*event
)
853 int entry
= sizeof(u64
); /* value */
857 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
860 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
863 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
864 entry
+= sizeof(u64
);
866 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
867 nr
+= event
->group_leader
->nr_siblings
;
872 event
->read_size
= size
;
875 static void perf_event__header_size(struct perf_event
*event
)
877 struct perf_sample_data
*data
;
878 u64 sample_type
= event
->attr
.sample_type
;
881 perf_event__read_size(event
);
883 if (sample_type
& PERF_SAMPLE_IP
)
884 size
+= sizeof(data
->ip
);
886 if (sample_type
& PERF_SAMPLE_ADDR
)
887 size
+= sizeof(data
->addr
);
889 if (sample_type
& PERF_SAMPLE_PERIOD
)
890 size
+= sizeof(data
->period
);
892 if (sample_type
& PERF_SAMPLE_READ
)
893 size
+= event
->read_size
;
895 event
->header_size
= size
;
898 static void perf_event__id_header_size(struct perf_event
*event
)
900 struct perf_sample_data
*data
;
901 u64 sample_type
= event
->attr
.sample_type
;
904 if (sample_type
& PERF_SAMPLE_TID
)
905 size
+= sizeof(data
->tid_entry
);
907 if (sample_type
& PERF_SAMPLE_TIME
)
908 size
+= sizeof(data
->time
);
910 if (sample_type
& PERF_SAMPLE_ID
)
911 size
+= sizeof(data
->id
);
913 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
914 size
+= sizeof(data
->stream_id
);
916 if (sample_type
& PERF_SAMPLE_CPU
)
917 size
+= sizeof(data
->cpu_entry
);
919 event
->id_header_size
= size
;
922 static void perf_group_attach(struct perf_event
*event
)
924 struct perf_event
*group_leader
= event
->group_leader
, *pos
;
927 * We can have double attach due to group movement in perf_event_open.
929 if (event
->attach_state
& PERF_ATTACH_GROUP
)
932 event
->attach_state
|= PERF_ATTACH_GROUP
;
934 if (group_leader
== event
)
937 if (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
&&
938 !is_software_event(event
))
939 group_leader
->group_flags
&= ~PERF_GROUP_SOFTWARE
;
941 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
942 group_leader
->nr_siblings
++;
944 perf_event__header_size(group_leader
);
946 list_for_each_entry(pos
, &group_leader
->sibling_list
, group_entry
)
947 perf_event__header_size(pos
);
951 * Remove a event from the lists for its context.
952 * Must be called with ctx->mutex and ctx->lock held.
955 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
957 struct perf_cpu_context
*cpuctx
;
959 * We can have double detach due to exit/hot-unplug + close.
961 if (!(event
->attach_state
& PERF_ATTACH_CONTEXT
))
964 event
->attach_state
&= ~PERF_ATTACH_CONTEXT
;
966 if (is_cgroup_event(event
)) {
968 cpuctx
= __get_cpu_context(ctx
);
970 * if there are no more cgroup events
971 * then cler cgrp to avoid stale pointer
972 * in update_cgrp_time_from_cpuctx()
974 if (!ctx
->nr_cgroups
)
979 if (event
->attr
.inherit_stat
)
982 list_del_rcu(&event
->event_entry
);
984 if (event
->group_leader
== event
)
985 list_del_init(&event
->group_entry
);
987 update_group_times(event
);
990 * If event was in error state, then keep it
991 * that way, otherwise bogus counts will be
992 * returned on read(). The only way to get out
993 * of error state is by explicit re-enabling
996 if (event
->state
> PERF_EVENT_STATE_OFF
)
997 event
->state
= PERF_EVENT_STATE_OFF
;
1000 static void perf_group_detach(struct perf_event
*event
)
1002 struct perf_event
*sibling
, *tmp
;
1003 struct list_head
*list
= NULL
;
1006 * We can have double detach due to exit/hot-unplug + close.
1008 if (!(event
->attach_state
& PERF_ATTACH_GROUP
))
1011 event
->attach_state
&= ~PERF_ATTACH_GROUP
;
1014 * If this is a sibling, remove it from its group.
1016 if (event
->group_leader
!= event
) {
1017 list_del_init(&event
->group_entry
);
1018 event
->group_leader
->nr_siblings
--;
1022 if (!list_empty(&event
->group_entry
))
1023 list
= &event
->group_entry
;
1026 * If this was a group event with sibling events then
1027 * upgrade the siblings to singleton events by adding them
1028 * to whatever list we are on.
1030 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
1032 list_move_tail(&sibling
->group_entry
, list
);
1033 sibling
->group_leader
= sibling
;
1035 /* Inherit group flags from the previous leader */
1036 sibling
->group_flags
= event
->group_flags
;
1040 perf_event__header_size(event
->group_leader
);
1042 list_for_each_entry(tmp
, &event
->group_leader
->sibling_list
, group_entry
)
1043 perf_event__header_size(tmp
);
1047 event_filter_match(struct perf_event
*event
)
1049 return (event
->cpu
== -1 || event
->cpu
== smp_processor_id())
1050 && perf_cgroup_match(event
);
1054 event_sched_out(struct perf_event
*event
,
1055 struct perf_cpu_context
*cpuctx
,
1056 struct perf_event_context
*ctx
)
1058 u64 tstamp
= perf_event_time(event
);
1061 * An event which could not be activated because of
1062 * filter mismatch still needs to have its timings
1063 * maintained, otherwise bogus information is return
1064 * via read() for time_enabled, time_running:
1066 if (event
->state
== PERF_EVENT_STATE_INACTIVE
1067 && !event_filter_match(event
)) {
1068 delta
= tstamp
- event
->tstamp_stopped
;
1069 event
->tstamp_running
+= delta
;
1070 event
->tstamp_stopped
= tstamp
;
1073 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1076 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1077 if (event
->pending_disable
) {
1078 event
->pending_disable
= 0;
1079 event
->state
= PERF_EVENT_STATE_OFF
;
1081 event
->tstamp_stopped
= tstamp
;
1082 event
->pmu
->del(event
, 0);
1085 if (!is_software_event(event
))
1086 cpuctx
->active_oncpu
--;
1088 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
1089 cpuctx
->exclusive
= 0;
1093 group_sched_out(struct perf_event
*group_event
,
1094 struct perf_cpu_context
*cpuctx
,
1095 struct perf_event_context
*ctx
)
1097 struct perf_event
*event
;
1098 int state
= group_event
->state
;
1100 event_sched_out(group_event
, cpuctx
, ctx
);
1103 * Schedule out siblings (if any):
1105 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
1106 event_sched_out(event
, cpuctx
, ctx
);
1108 if (state
== PERF_EVENT_STATE_ACTIVE
&& group_event
->attr
.exclusive
)
1109 cpuctx
->exclusive
= 0;
1113 * Cross CPU call to remove a performance event
1115 * We disable the event on the hardware level first. After that we
1116 * remove it from the context list.
1118 static int __perf_remove_from_context(void *info
)
1120 struct perf_event
*event
= info
;
1121 struct perf_event_context
*ctx
= event
->ctx
;
1122 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1124 raw_spin_lock(&ctx
->lock
);
1125 event_sched_out(event
, cpuctx
, ctx
);
1126 list_del_event(event
, ctx
);
1127 if (!ctx
->nr_events
&& cpuctx
->task_ctx
== ctx
) {
1129 cpuctx
->task_ctx
= NULL
;
1131 raw_spin_unlock(&ctx
->lock
);
1138 * Remove the event from a task's (or a CPU's) list of events.
1140 * CPU events are removed with a smp call. For task events we only
1141 * call when the task is on a CPU.
1143 * If event->ctx is a cloned context, callers must make sure that
1144 * every task struct that event->ctx->task could possibly point to
1145 * remains valid. This is OK when called from perf_release since
1146 * that only calls us on the top-level context, which can't be a clone.
1147 * When called from perf_event_exit_task, it's OK because the
1148 * context has been detached from its task.
1150 static void perf_remove_from_context(struct perf_event
*event
)
1152 struct perf_event_context
*ctx
= event
->ctx
;
1153 struct task_struct
*task
= ctx
->task
;
1155 lockdep_assert_held(&ctx
->mutex
);
1159 * Per cpu events are removed via an smp call and
1160 * the removal is always successful.
1162 cpu_function_call(event
->cpu
, __perf_remove_from_context
, event
);
1167 if (!task_function_call(task
, __perf_remove_from_context
, event
))
1170 raw_spin_lock_irq(&ctx
->lock
);
1172 * If we failed to find a running task, but find the context active now
1173 * that we've acquired the ctx->lock, retry.
1175 if (ctx
->is_active
) {
1176 raw_spin_unlock_irq(&ctx
->lock
);
1181 * Since the task isn't running, its safe to remove the event, us
1182 * holding the ctx->lock ensures the task won't get scheduled in.
1184 list_del_event(event
, ctx
);
1185 raw_spin_unlock_irq(&ctx
->lock
);
1189 * Cross CPU call to disable a performance event
1191 static int __perf_event_disable(void *info
)
1193 struct perf_event
*event
= info
;
1194 struct perf_event_context
*ctx
= event
->ctx
;
1195 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1198 * If this is a per-task event, need to check whether this
1199 * event's task is the current task on this cpu.
1201 * Can trigger due to concurrent perf_event_context_sched_out()
1202 * flipping contexts around.
1204 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
1207 raw_spin_lock(&ctx
->lock
);
1210 * If the event is on, turn it off.
1211 * If it is in error state, leave it in error state.
1213 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
) {
1214 update_context_time(ctx
);
1215 update_cgrp_time_from_event(event
);
1216 update_group_times(event
);
1217 if (event
== event
->group_leader
)
1218 group_sched_out(event
, cpuctx
, ctx
);
1220 event_sched_out(event
, cpuctx
, ctx
);
1221 event
->state
= PERF_EVENT_STATE_OFF
;
1224 raw_spin_unlock(&ctx
->lock
);
1232 * If event->ctx is a cloned context, callers must make sure that
1233 * every task struct that event->ctx->task could possibly point to
1234 * remains valid. This condition is satisifed when called through
1235 * perf_event_for_each_child or perf_event_for_each because they
1236 * hold the top-level event's child_mutex, so any descendant that
1237 * goes to exit will block in sync_child_event.
1238 * When called from perf_pending_event it's OK because event->ctx
1239 * is the current context on this CPU and preemption is disabled,
1240 * hence we can't get into perf_event_task_sched_out for this context.
1242 void perf_event_disable(struct perf_event
*event
)
1244 struct perf_event_context
*ctx
= event
->ctx
;
1245 struct task_struct
*task
= ctx
->task
;
1249 * Disable the event on the cpu that it's on
1251 cpu_function_call(event
->cpu
, __perf_event_disable
, event
);
1256 if (!task_function_call(task
, __perf_event_disable
, event
))
1259 raw_spin_lock_irq(&ctx
->lock
);
1261 * If the event is still active, we need to retry the cross-call.
1263 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
1264 raw_spin_unlock_irq(&ctx
->lock
);
1266 * Reload the task pointer, it might have been changed by
1267 * a concurrent perf_event_context_sched_out().
1274 * Since we have the lock this context can't be scheduled
1275 * in, so we can change the state safely.
1277 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1278 update_group_times(event
);
1279 event
->state
= PERF_EVENT_STATE_OFF
;
1281 raw_spin_unlock_irq(&ctx
->lock
);
1284 static void perf_set_shadow_time(struct perf_event
*event
,
1285 struct perf_event_context
*ctx
,
1289 * use the correct time source for the time snapshot
1291 * We could get by without this by leveraging the
1292 * fact that to get to this function, the caller
1293 * has most likely already called update_context_time()
1294 * and update_cgrp_time_xx() and thus both timestamp
1295 * are identical (or very close). Given that tstamp is,
1296 * already adjusted for cgroup, we could say that:
1297 * tstamp - ctx->timestamp
1299 * tstamp - cgrp->timestamp.
1301 * Then, in perf_output_read(), the calculation would
1302 * work with no changes because:
1303 * - event is guaranteed scheduled in
1304 * - no scheduled out in between
1305 * - thus the timestamp would be the same
1307 * But this is a bit hairy.
1309 * So instead, we have an explicit cgroup call to remain
1310 * within the time time source all along. We believe it
1311 * is cleaner and simpler to understand.
1313 if (is_cgroup_event(event
))
1314 perf_cgroup_set_shadow_time(event
, tstamp
);
1316 event
->shadow_ctx_time
= tstamp
- ctx
->timestamp
;
1319 #define MAX_INTERRUPTS (~0ULL)
1321 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1324 event_sched_in(struct perf_event
*event
,
1325 struct perf_cpu_context
*cpuctx
,
1326 struct perf_event_context
*ctx
)
1328 u64 tstamp
= perf_event_time(event
);
1330 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1333 event
->state
= PERF_EVENT_STATE_ACTIVE
;
1334 event
->oncpu
= smp_processor_id();
1337 * Unthrottle events, since we scheduled we might have missed several
1338 * ticks already, also for a heavily scheduling task there is little
1339 * guarantee it'll get a tick in a timely manner.
1341 if (unlikely(event
->hw
.interrupts
== MAX_INTERRUPTS
)) {
1342 perf_log_throttle(event
, 1);
1343 event
->hw
.interrupts
= 0;
1347 * The new state must be visible before we turn it on in the hardware:
1351 if (event
->pmu
->add(event
, PERF_EF_START
)) {
1352 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1357 event
->tstamp_running
+= tstamp
- event
->tstamp_stopped
;
1359 perf_set_shadow_time(event
, ctx
, tstamp
);
1361 if (!is_software_event(event
))
1362 cpuctx
->active_oncpu
++;
1365 if (event
->attr
.exclusive
)
1366 cpuctx
->exclusive
= 1;
1372 group_sched_in(struct perf_event
*group_event
,
1373 struct perf_cpu_context
*cpuctx
,
1374 struct perf_event_context
*ctx
)
1376 struct perf_event
*event
, *partial_group
= NULL
;
1377 struct pmu
*pmu
= group_event
->pmu
;
1378 u64 now
= ctx
->time
;
1379 bool simulate
= false;
1381 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
1384 pmu
->start_txn(pmu
);
1386 if (event_sched_in(group_event
, cpuctx
, ctx
)) {
1387 pmu
->cancel_txn(pmu
);
1392 * Schedule in siblings as one group (if any):
1394 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
1395 if (event_sched_in(event
, cpuctx
, ctx
)) {
1396 partial_group
= event
;
1401 if (!pmu
->commit_txn(pmu
))
1406 * Groups can be scheduled in as one unit only, so undo any
1407 * partial group before returning:
1408 * The events up to the failed event are scheduled out normally,
1409 * tstamp_stopped will be updated.
1411 * The failed events and the remaining siblings need to have
1412 * their timings updated as if they had gone thru event_sched_in()
1413 * and event_sched_out(). This is required to get consistent timings
1414 * across the group. This also takes care of the case where the group
1415 * could never be scheduled by ensuring tstamp_stopped is set to mark
1416 * the time the event was actually stopped, such that time delta
1417 * calculation in update_event_times() is correct.
1419 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
1420 if (event
== partial_group
)
1424 event
->tstamp_running
+= now
- event
->tstamp_stopped
;
1425 event
->tstamp_stopped
= now
;
1427 event_sched_out(event
, cpuctx
, ctx
);
1430 event_sched_out(group_event
, cpuctx
, ctx
);
1432 pmu
->cancel_txn(pmu
);
1438 * Work out whether we can put this event group on the CPU now.
1440 static int group_can_go_on(struct perf_event
*event
,
1441 struct perf_cpu_context
*cpuctx
,
1445 * Groups consisting entirely of software events can always go on.
1447 if (event
->group_flags
& PERF_GROUP_SOFTWARE
)
1450 * If an exclusive group is already on, no other hardware
1453 if (cpuctx
->exclusive
)
1456 * If this group is exclusive and there are already
1457 * events on the CPU, it can't go on.
1459 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
1462 * Otherwise, try to add it if all previous groups were able
1468 static void add_event_to_ctx(struct perf_event
*event
,
1469 struct perf_event_context
*ctx
)
1471 u64 tstamp
= perf_event_time(event
);
1473 list_add_event(event
, ctx
);
1474 perf_group_attach(event
);
1475 event
->tstamp_enabled
= tstamp
;
1476 event
->tstamp_running
= tstamp
;
1477 event
->tstamp_stopped
= tstamp
;
1480 static void task_ctx_sched_out(struct perf_event_context
*ctx
);
1482 ctx_sched_in(struct perf_event_context
*ctx
,
1483 struct perf_cpu_context
*cpuctx
,
1484 enum event_type_t event_type
,
1485 struct task_struct
*task
);
1487 static void perf_event_sched_in(struct perf_cpu_context
*cpuctx
,
1488 struct perf_event_context
*ctx
,
1489 struct task_struct
*task
)
1491 cpu_ctx_sched_in(cpuctx
, EVENT_PINNED
, task
);
1493 ctx_sched_in(ctx
, cpuctx
, EVENT_PINNED
, task
);
1494 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
, task
);
1496 ctx_sched_in(ctx
, cpuctx
, EVENT_FLEXIBLE
, task
);
1500 * Cross CPU call to install and enable a performance event
1502 * Must be called with ctx->mutex held
1504 static int __perf_install_in_context(void *info
)
1506 struct perf_event
*event
= info
;
1507 struct perf_event_context
*ctx
= event
->ctx
;
1508 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1509 struct perf_event_context
*task_ctx
= cpuctx
->task_ctx
;
1510 struct task_struct
*task
= current
;
1512 perf_ctx_lock(cpuctx
, task_ctx
);
1513 perf_pmu_disable(cpuctx
->ctx
.pmu
);
1516 * If there was an active task_ctx schedule it out.
1519 task_ctx_sched_out(task_ctx
);
1522 * If the context we're installing events in is not the
1523 * active task_ctx, flip them.
1525 if (ctx
->task
&& task_ctx
!= ctx
) {
1527 raw_spin_unlock(&task_ctx
->lock
);
1528 raw_spin_lock(&ctx
->lock
);
1533 cpuctx
->task_ctx
= task_ctx
;
1534 task
= task_ctx
->task
;
1537 cpu_ctx_sched_out(cpuctx
, EVENT_ALL
);
1539 update_context_time(ctx
);
1541 * update cgrp time only if current cgrp
1542 * matches event->cgrp. Must be done before
1543 * calling add_event_to_ctx()
1545 update_cgrp_time_from_event(event
);
1547 add_event_to_ctx(event
, ctx
);
1550 * Schedule everything back in
1552 perf_event_sched_in(cpuctx
, task_ctx
, task
);
1554 perf_pmu_enable(cpuctx
->ctx
.pmu
);
1555 perf_ctx_unlock(cpuctx
, task_ctx
);
1561 * Attach a performance event to a context
1563 * First we add the event to the list with the hardware enable bit
1564 * in event->hw_config cleared.
1566 * If the event is attached to a task which is on a CPU we use a smp
1567 * call to enable it in the task context. The task might have been
1568 * scheduled away, but we check this in the smp call again.
1571 perf_install_in_context(struct perf_event_context
*ctx
,
1572 struct perf_event
*event
,
1575 struct task_struct
*task
= ctx
->task
;
1577 lockdep_assert_held(&ctx
->mutex
);
1583 * Per cpu events are installed via an smp call and
1584 * the install is always successful.
1586 cpu_function_call(cpu
, __perf_install_in_context
, event
);
1591 if (!task_function_call(task
, __perf_install_in_context
, event
))
1594 raw_spin_lock_irq(&ctx
->lock
);
1596 * If we failed to find a running task, but find the context active now
1597 * that we've acquired the ctx->lock, retry.
1599 if (ctx
->is_active
) {
1600 raw_spin_unlock_irq(&ctx
->lock
);
1605 * Since the task isn't running, its safe to add the event, us holding
1606 * the ctx->lock ensures the task won't get scheduled in.
1608 add_event_to_ctx(event
, ctx
);
1609 raw_spin_unlock_irq(&ctx
->lock
);
1613 * Put a event into inactive state and update time fields.
1614 * Enabling the leader of a group effectively enables all
1615 * the group members that aren't explicitly disabled, so we
1616 * have to update their ->tstamp_enabled also.
1617 * Note: this works for group members as well as group leaders
1618 * since the non-leader members' sibling_lists will be empty.
1620 static void __perf_event_mark_enabled(struct perf_event
*event
,
1621 struct perf_event_context
*ctx
)
1623 struct perf_event
*sub
;
1624 u64 tstamp
= perf_event_time(event
);
1626 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1627 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
1628 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
1629 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
)
1630 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
1635 * Cross CPU call to enable a performance event
1637 static int __perf_event_enable(void *info
)
1639 struct perf_event
*event
= info
;
1640 struct perf_event_context
*ctx
= event
->ctx
;
1641 struct perf_event
*leader
= event
->group_leader
;
1642 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1645 if (WARN_ON_ONCE(!ctx
->is_active
))
1648 raw_spin_lock(&ctx
->lock
);
1649 update_context_time(ctx
);
1651 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1655 * set current task's cgroup time reference point
1657 perf_cgroup_set_timestamp(current
, ctx
);
1659 __perf_event_mark_enabled(event
, ctx
);
1661 if (!event_filter_match(event
)) {
1662 if (is_cgroup_event(event
))
1663 perf_cgroup_defer_enabled(event
);
1668 * If the event is in a group and isn't the group leader,
1669 * then don't put it on unless the group is on.
1671 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
)
1674 if (!group_can_go_on(event
, cpuctx
, 1)) {
1677 if (event
== leader
)
1678 err
= group_sched_in(event
, cpuctx
, ctx
);
1680 err
= event_sched_in(event
, cpuctx
, ctx
);
1685 * If this event can't go on and it's part of a
1686 * group, then the whole group has to come off.
1688 if (leader
!= event
)
1689 group_sched_out(leader
, cpuctx
, ctx
);
1690 if (leader
->attr
.pinned
) {
1691 update_group_times(leader
);
1692 leader
->state
= PERF_EVENT_STATE_ERROR
;
1697 raw_spin_unlock(&ctx
->lock
);
1705 * If event->ctx is a cloned context, callers must make sure that
1706 * every task struct that event->ctx->task could possibly point to
1707 * remains valid. This condition is satisfied when called through
1708 * perf_event_for_each_child or perf_event_for_each as described
1709 * for perf_event_disable.
1711 void perf_event_enable(struct perf_event
*event
)
1713 struct perf_event_context
*ctx
= event
->ctx
;
1714 struct task_struct
*task
= ctx
->task
;
1718 * Enable the event on the cpu that it's on
1720 cpu_function_call(event
->cpu
, __perf_event_enable
, event
);
1724 raw_spin_lock_irq(&ctx
->lock
);
1725 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1729 * If the event is in error state, clear that first.
1730 * That way, if we see the event in error state below, we
1731 * know that it has gone back into error state, as distinct
1732 * from the task having been scheduled away before the
1733 * cross-call arrived.
1735 if (event
->state
== PERF_EVENT_STATE_ERROR
)
1736 event
->state
= PERF_EVENT_STATE_OFF
;
1739 if (!ctx
->is_active
) {
1740 __perf_event_mark_enabled(event
, ctx
);
1744 raw_spin_unlock_irq(&ctx
->lock
);
1746 if (!task_function_call(task
, __perf_event_enable
, event
))
1749 raw_spin_lock_irq(&ctx
->lock
);
1752 * If the context is active and the event is still off,
1753 * we need to retry the cross-call.
1755 if (ctx
->is_active
&& event
->state
== PERF_EVENT_STATE_OFF
) {
1757 * task could have been flipped by a concurrent
1758 * perf_event_context_sched_out()
1765 raw_spin_unlock_irq(&ctx
->lock
);
1768 int perf_event_refresh(struct perf_event
*event
, int refresh
)
1771 * not supported on inherited events
1773 if (event
->attr
.inherit
|| !is_sampling_event(event
))
1776 atomic_add(refresh
, &event
->event_limit
);
1777 perf_event_enable(event
);
1781 EXPORT_SYMBOL_GPL(perf_event_refresh
);
1783 static void ctx_sched_out(struct perf_event_context
*ctx
,
1784 struct perf_cpu_context
*cpuctx
,
1785 enum event_type_t event_type
)
1787 struct perf_event
*event
;
1788 int is_active
= ctx
->is_active
;
1790 ctx
->is_active
&= ~event_type
;
1791 if (likely(!ctx
->nr_events
))
1794 update_context_time(ctx
);
1795 update_cgrp_time_from_cpuctx(cpuctx
);
1796 if (!ctx
->nr_active
)
1799 perf_pmu_disable(ctx
->pmu
);
1800 if ((is_active
& EVENT_PINNED
) && (event_type
& EVENT_PINNED
)) {
1801 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
)
1802 group_sched_out(event
, cpuctx
, ctx
);
1805 if ((is_active
& EVENT_FLEXIBLE
) && (event_type
& EVENT_FLEXIBLE
)) {
1806 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
)
1807 group_sched_out(event
, cpuctx
, ctx
);
1809 perf_pmu_enable(ctx
->pmu
);
1813 * Test whether two contexts are equivalent, i.e. whether they
1814 * have both been cloned from the same version of the same context
1815 * and they both have the same number of enabled events.
1816 * If the number of enabled events is the same, then the set
1817 * of enabled events should be the same, because these are both
1818 * inherited contexts, therefore we can't access individual events
1819 * in them directly with an fd; we can only enable/disable all
1820 * events via prctl, or enable/disable all events in a family
1821 * via ioctl, which will have the same effect on both contexts.
1823 static int context_equiv(struct perf_event_context
*ctx1
,
1824 struct perf_event_context
*ctx2
)
1826 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
1827 && ctx1
->parent_gen
== ctx2
->parent_gen
1828 && !ctx1
->pin_count
&& !ctx2
->pin_count
;
1831 static void __perf_event_sync_stat(struct perf_event
*event
,
1832 struct perf_event
*next_event
)
1836 if (!event
->attr
.inherit_stat
)
1840 * Update the event value, we cannot use perf_event_read()
1841 * because we're in the middle of a context switch and have IRQs
1842 * disabled, which upsets smp_call_function_single(), however
1843 * we know the event must be on the current CPU, therefore we
1844 * don't need to use it.
1846 switch (event
->state
) {
1847 case PERF_EVENT_STATE_ACTIVE
:
1848 event
->pmu
->read(event
);
1851 case PERF_EVENT_STATE_INACTIVE
:
1852 update_event_times(event
);
1860 * In order to keep per-task stats reliable we need to flip the event
1861 * values when we flip the contexts.
1863 value
= local64_read(&next_event
->count
);
1864 value
= local64_xchg(&event
->count
, value
);
1865 local64_set(&next_event
->count
, value
);
1867 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
1868 swap(event
->total_time_running
, next_event
->total_time_running
);
1871 * Since we swizzled the values, update the user visible data too.
1873 perf_event_update_userpage(event
);
1874 perf_event_update_userpage(next_event
);
1877 #define list_next_entry(pos, member) \
1878 list_entry(pos->member.next, typeof(*pos), member)
1880 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
1881 struct perf_event_context
*next_ctx
)
1883 struct perf_event
*event
, *next_event
;
1888 update_context_time(ctx
);
1890 event
= list_first_entry(&ctx
->event_list
,
1891 struct perf_event
, event_entry
);
1893 next_event
= list_first_entry(&next_ctx
->event_list
,
1894 struct perf_event
, event_entry
);
1896 while (&event
->event_entry
!= &ctx
->event_list
&&
1897 &next_event
->event_entry
!= &next_ctx
->event_list
) {
1899 __perf_event_sync_stat(event
, next_event
);
1901 event
= list_next_entry(event
, event_entry
);
1902 next_event
= list_next_entry(next_event
, event_entry
);
1906 static void perf_event_context_sched_out(struct task_struct
*task
, int ctxn
,
1907 struct task_struct
*next
)
1909 struct perf_event_context
*ctx
= task
->perf_event_ctxp
[ctxn
];
1910 struct perf_event_context
*next_ctx
;
1911 struct perf_event_context
*parent
;
1912 struct perf_cpu_context
*cpuctx
;
1918 cpuctx
= __get_cpu_context(ctx
);
1919 if (!cpuctx
->task_ctx
)
1923 parent
= rcu_dereference(ctx
->parent_ctx
);
1924 next_ctx
= next
->perf_event_ctxp
[ctxn
];
1925 if (parent
&& next_ctx
&&
1926 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
1928 * Looks like the two contexts are clones, so we might be
1929 * able to optimize the context switch. We lock both
1930 * contexts and check that they are clones under the
1931 * lock (including re-checking that neither has been
1932 * uncloned in the meantime). It doesn't matter which
1933 * order we take the locks because no other cpu could
1934 * be trying to lock both of these tasks.
1936 raw_spin_lock(&ctx
->lock
);
1937 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
1938 if (context_equiv(ctx
, next_ctx
)) {
1940 * XXX do we need a memory barrier of sorts
1941 * wrt to rcu_dereference() of perf_event_ctxp
1943 task
->perf_event_ctxp
[ctxn
] = next_ctx
;
1944 next
->perf_event_ctxp
[ctxn
] = ctx
;
1946 next_ctx
->task
= task
;
1949 perf_event_sync_stat(ctx
, next_ctx
);
1951 raw_spin_unlock(&next_ctx
->lock
);
1952 raw_spin_unlock(&ctx
->lock
);
1957 raw_spin_lock(&ctx
->lock
);
1958 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
1959 cpuctx
->task_ctx
= NULL
;
1960 raw_spin_unlock(&ctx
->lock
);
1964 #define for_each_task_context_nr(ctxn) \
1965 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1968 * Called from scheduler to remove the events of the current task,
1969 * with interrupts disabled.
1971 * We stop each event and update the event value in event->count.
1973 * This does not protect us against NMI, but disable()
1974 * sets the disabled bit in the control field of event _before_
1975 * accessing the event control register. If a NMI hits, then it will
1976 * not restart the event.
1978 void __perf_event_task_sched_out(struct task_struct
*task
,
1979 struct task_struct
*next
)
1983 for_each_task_context_nr(ctxn
)
1984 perf_event_context_sched_out(task
, ctxn
, next
);
1987 * if cgroup events exist on this CPU, then we need
1988 * to check if we have to switch out PMU state.
1989 * cgroup event are system-wide mode only
1991 if (atomic_read(&__get_cpu_var(perf_cgroup_events
)))
1992 perf_cgroup_sched_out(task
);
1995 static void task_ctx_sched_out(struct perf_event_context
*ctx
)
1997 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1999 if (!cpuctx
->task_ctx
)
2002 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
2005 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
2006 cpuctx
->task_ctx
= NULL
;
2010 * Called with IRQs disabled
2012 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
2013 enum event_type_t event_type
)
2015 ctx_sched_out(&cpuctx
->ctx
, cpuctx
, event_type
);
2019 ctx_pinned_sched_in(struct perf_event_context
*ctx
,
2020 struct perf_cpu_context
*cpuctx
)
2022 struct perf_event
*event
;
2024 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
2025 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2027 if (!event_filter_match(event
))
2030 /* may need to reset tstamp_enabled */
2031 if (is_cgroup_event(event
))
2032 perf_cgroup_mark_enabled(event
, ctx
);
2034 if (group_can_go_on(event
, cpuctx
, 1))
2035 group_sched_in(event
, cpuctx
, ctx
);
2038 * If this pinned group hasn't been scheduled,
2039 * put it in error state.
2041 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
2042 update_group_times(event
);
2043 event
->state
= PERF_EVENT_STATE_ERROR
;
2049 ctx_flexible_sched_in(struct perf_event_context
*ctx
,
2050 struct perf_cpu_context
*cpuctx
)
2052 struct perf_event
*event
;
2055 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
2056 /* Ignore events in OFF or ERROR state */
2057 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2060 * Listen to the 'cpu' scheduling filter constraint
2063 if (!event_filter_match(event
))
2066 /* may need to reset tstamp_enabled */
2067 if (is_cgroup_event(event
))
2068 perf_cgroup_mark_enabled(event
, ctx
);
2070 if (group_can_go_on(event
, cpuctx
, can_add_hw
)) {
2071 if (group_sched_in(event
, cpuctx
, ctx
))
2078 ctx_sched_in(struct perf_event_context
*ctx
,
2079 struct perf_cpu_context
*cpuctx
,
2080 enum event_type_t event_type
,
2081 struct task_struct
*task
)
2084 int is_active
= ctx
->is_active
;
2086 ctx
->is_active
|= event_type
;
2087 if (likely(!ctx
->nr_events
))
2091 ctx
->timestamp
= now
;
2092 perf_cgroup_set_timestamp(task
, ctx
);
2094 * First go through the list and put on any pinned groups
2095 * in order to give them the best chance of going on.
2097 if (!(is_active
& EVENT_PINNED
) && (event_type
& EVENT_PINNED
))
2098 ctx_pinned_sched_in(ctx
, cpuctx
);
2100 /* Then walk through the lower prio flexible groups */
2101 if (!(is_active
& EVENT_FLEXIBLE
) && (event_type
& EVENT_FLEXIBLE
))
2102 ctx_flexible_sched_in(ctx
, cpuctx
);
2105 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
2106 enum event_type_t event_type
,
2107 struct task_struct
*task
)
2109 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
2111 ctx_sched_in(ctx
, cpuctx
, event_type
, task
);
2114 static void perf_event_context_sched_in(struct perf_event_context
*ctx
,
2115 struct task_struct
*task
)
2117 struct perf_cpu_context
*cpuctx
;
2119 cpuctx
= __get_cpu_context(ctx
);
2120 if (cpuctx
->task_ctx
== ctx
)
2123 perf_ctx_lock(cpuctx
, ctx
);
2124 perf_pmu_disable(ctx
->pmu
);
2126 * We want to keep the following priority order:
2127 * cpu pinned (that don't need to move), task pinned,
2128 * cpu flexible, task flexible.
2130 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
2132 perf_event_sched_in(cpuctx
, ctx
, task
);
2134 cpuctx
->task_ctx
= ctx
;
2136 perf_pmu_enable(ctx
->pmu
);
2137 perf_ctx_unlock(cpuctx
, ctx
);
2140 * Since these rotations are per-cpu, we need to ensure the
2141 * cpu-context we got scheduled on is actually rotating.
2143 perf_pmu_rotate_start(ctx
->pmu
);
2147 * Called from scheduler to add the events of the current task
2148 * with interrupts disabled.
2150 * We restore the event value and then enable it.
2152 * This does not protect us against NMI, but enable()
2153 * sets the enabled bit in the control field of event _before_
2154 * accessing the event control register. If a NMI hits, then it will
2155 * keep the event running.
2157 void __perf_event_task_sched_in(struct task_struct
*task
)
2159 struct perf_event_context
*ctx
;
2162 for_each_task_context_nr(ctxn
) {
2163 ctx
= task
->perf_event_ctxp
[ctxn
];
2167 perf_event_context_sched_in(ctx
, task
);
2170 * if cgroup events exist on this CPU, then we need
2171 * to check if we have to switch in PMU state.
2172 * cgroup event are system-wide mode only
2174 if (atomic_read(&__get_cpu_var(perf_cgroup_events
)))
2175 perf_cgroup_sched_in(task
);
2178 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
2180 u64 frequency
= event
->attr
.sample_freq
;
2181 u64 sec
= NSEC_PER_SEC
;
2182 u64 divisor
, dividend
;
2184 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
2186 count_fls
= fls64(count
);
2187 nsec_fls
= fls64(nsec
);
2188 frequency_fls
= fls64(frequency
);
2192 * We got @count in @nsec, with a target of sample_freq HZ
2193 * the target period becomes:
2196 * period = -------------------
2197 * @nsec * sample_freq
2202 * Reduce accuracy by one bit such that @a and @b converge
2203 * to a similar magnitude.
2205 #define REDUCE_FLS(a, b) \
2207 if (a##_fls > b##_fls) { \
2217 * Reduce accuracy until either term fits in a u64, then proceed with
2218 * the other, so that finally we can do a u64/u64 division.
2220 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
2221 REDUCE_FLS(nsec
, frequency
);
2222 REDUCE_FLS(sec
, count
);
2225 if (count_fls
+ sec_fls
> 64) {
2226 divisor
= nsec
* frequency
;
2228 while (count_fls
+ sec_fls
> 64) {
2229 REDUCE_FLS(count
, sec
);
2233 dividend
= count
* sec
;
2235 dividend
= count
* sec
;
2237 while (nsec_fls
+ frequency_fls
> 64) {
2238 REDUCE_FLS(nsec
, frequency
);
2242 divisor
= nsec
* frequency
;
2248 return div64_u64(dividend
, divisor
);
2251 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
)
2253 struct hw_perf_event
*hwc
= &event
->hw
;
2254 s64 period
, sample_period
;
2257 period
= perf_calculate_period(event
, nsec
, count
);
2259 delta
= (s64
)(period
- hwc
->sample_period
);
2260 delta
= (delta
+ 7) / 8; /* low pass filter */
2262 sample_period
= hwc
->sample_period
+ delta
;
2267 hwc
->sample_period
= sample_period
;
2269 if (local64_read(&hwc
->period_left
) > 8*sample_period
) {
2270 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
2271 local64_set(&hwc
->period_left
, 0);
2272 event
->pmu
->start(event
, PERF_EF_RELOAD
);
2276 static void perf_ctx_adjust_freq(struct perf_event_context
*ctx
, u64 period
)
2278 struct perf_event
*event
;
2279 struct hw_perf_event
*hwc
;
2280 u64 interrupts
, now
;
2283 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
2284 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
2287 if (!event_filter_match(event
))
2292 interrupts
= hwc
->interrupts
;
2293 hwc
->interrupts
= 0;
2296 * unthrottle events on the tick
2298 if (interrupts
== MAX_INTERRUPTS
) {
2299 perf_log_throttle(event
, 1);
2300 event
->pmu
->start(event
, 0);
2303 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
2306 event
->pmu
->read(event
);
2307 now
= local64_read(&event
->count
);
2308 delta
= now
- hwc
->freq_count_stamp
;
2309 hwc
->freq_count_stamp
= now
;
2312 perf_adjust_period(event
, period
, delta
);
2317 * Round-robin a context's events:
2319 static void rotate_ctx(struct perf_event_context
*ctx
)
2322 * Rotate the first entry last of non-pinned groups. Rotation might be
2323 * disabled by the inheritance code.
2325 if (!ctx
->rotate_disable
)
2326 list_rotate_left(&ctx
->flexible_groups
);
2330 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2331 * because they're strictly cpu affine and rotate_start is called with IRQs
2332 * disabled, while rotate_context is called from IRQ context.
2334 static void perf_rotate_context(struct perf_cpu_context
*cpuctx
)
2336 u64 interval
= (u64
)cpuctx
->jiffies_interval
* TICK_NSEC
;
2337 struct perf_event_context
*ctx
= NULL
;
2338 int rotate
= 0, remove
= 1;
2340 if (cpuctx
->ctx
.nr_events
) {
2342 if (cpuctx
->ctx
.nr_events
!= cpuctx
->ctx
.nr_active
)
2346 ctx
= cpuctx
->task_ctx
;
2347 if (ctx
&& ctx
->nr_events
) {
2349 if (ctx
->nr_events
!= ctx
->nr_active
)
2353 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
2354 perf_pmu_disable(cpuctx
->ctx
.pmu
);
2355 perf_ctx_adjust_freq(&cpuctx
->ctx
, interval
);
2357 perf_ctx_adjust_freq(ctx
, interval
);
2362 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
2364 ctx_sched_out(ctx
, cpuctx
, EVENT_FLEXIBLE
);
2366 rotate_ctx(&cpuctx
->ctx
);
2370 perf_event_sched_in(cpuctx
, ctx
, current
);
2374 list_del_init(&cpuctx
->rotation_list
);
2376 perf_pmu_enable(cpuctx
->ctx
.pmu
);
2377 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
2380 void perf_event_task_tick(void)
2382 struct list_head
*head
= &__get_cpu_var(rotation_list
);
2383 struct perf_cpu_context
*cpuctx
, *tmp
;
2385 WARN_ON(!irqs_disabled());
2387 list_for_each_entry_safe(cpuctx
, tmp
, head
, rotation_list
) {
2388 if (cpuctx
->jiffies_interval
== 1 ||
2389 !(jiffies
% cpuctx
->jiffies_interval
))
2390 perf_rotate_context(cpuctx
);
2394 static int event_enable_on_exec(struct perf_event
*event
,
2395 struct perf_event_context
*ctx
)
2397 if (!event
->attr
.enable_on_exec
)
2400 event
->attr
.enable_on_exec
= 0;
2401 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
2404 __perf_event_mark_enabled(event
, ctx
);
2410 * Enable all of a task's events that have been marked enable-on-exec.
2411 * This expects task == current.
2413 static void perf_event_enable_on_exec(struct perf_event_context
*ctx
)
2415 struct perf_event
*event
;
2416 unsigned long flags
;
2420 local_irq_save(flags
);
2421 if (!ctx
|| !ctx
->nr_events
)
2425 * We must ctxsw out cgroup events to avoid conflict
2426 * when invoking perf_task_event_sched_in() later on
2427 * in this function. Otherwise we end up trying to
2428 * ctxswin cgroup events which are already scheduled
2431 perf_cgroup_sched_out(current
);
2433 raw_spin_lock(&ctx
->lock
);
2434 task_ctx_sched_out(ctx
);
2436 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
2437 ret
= event_enable_on_exec(event
, ctx
);
2442 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
2443 ret
= event_enable_on_exec(event
, ctx
);
2449 * Unclone this context if we enabled any event.
2454 raw_spin_unlock(&ctx
->lock
);
2457 * Also calls ctxswin for cgroup events, if any:
2459 perf_event_context_sched_in(ctx
, ctx
->task
);
2461 local_irq_restore(flags
);
2465 * Cross CPU call to read the hardware event
2467 static void __perf_event_read(void *info
)
2469 struct perf_event
*event
= info
;
2470 struct perf_event_context
*ctx
= event
->ctx
;
2471 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
2474 * If this is a task context, we need to check whether it is
2475 * the current task context of this cpu. If not it has been
2476 * scheduled out before the smp call arrived. In that case
2477 * event->count would have been updated to a recent sample
2478 * when the event was scheduled out.
2480 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
2483 raw_spin_lock(&ctx
->lock
);
2484 if (ctx
->is_active
) {
2485 update_context_time(ctx
);
2486 update_cgrp_time_from_event(event
);
2488 update_event_times(event
);
2489 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
2490 event
->pmu
->read(event
);
2491 raw_spin_unlock(&ctx
->lock
);
2494 static inline u64
perf_event_count(struct perf_event
*event
)
2496 return local64_read(&event
->count
) + atomic64_read(&event
->child_count
);
2499 static u64
perf_event_read(struct perf_event
*event
)
2502 * If event is enabled and currently active on a CPU, update the
2503 * value in the event structure:
2505 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
2506 smp_call_function_single(event
->oncpu
,
2507 __perf_event_read
, event
, 1);
2508 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
2509 struct perf_event_context
*ctx
= event
->ctx
;
2510 unsigned long flags
;
2512 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
2514 * may read while context is not active
2515 * (e.g., thread is blocked), in that case
2516 * we cannot update context time
2518 if (ctx
->is_active
) {
2519 update_context_time(ctx
);
2520 update_cgrp_time_from_event(event
);
2522 update_event_times(event
);
2523 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
2526 return perf_event_count(event
);
2533 struct callchain_cpus_entries
{
2534 struct rcu_head rcu_head
;
2535 struct perf_callchain_entry
*cpu_entries
[0];
2538 static DEFINE_PER_CPU(int, callchain_recursion
[PERF_NR_CONTEXTS
]);
2539 static atomic_t nr_callchain_events
;
2540 static DEFINE_MUTEX(callchain_mutex
);
2541 struct callchain_cpus_entries
*callchain_cpus_entries
;
2544 __weak
void perf_callchain_kernel(struct perf_callchain_entry
*entry
,
2545 struct pt_regs
*regs
)
2549 __weak
void perf_callchain_user(struct perf_callchain_entry
*entry
,
2550 struct pt_regs
*regs
)
2554 static void release_callchain_buffers_rcu(struct rcu_head
*head
)
2556 struct callchain_cpus_entries
*entries
;
2559 entries
= container_of(head
, struct callchain_cpus_entries
, rcu_head
);
2561 for_each_possible_cpu(cpu
)
2562 kfree(entries
->cpu_entries
[cpu
]);
2567 static void release_callchain_buffers(void)
2569 struct callchain_cpus_entries
*entries
;
2571 entries
= callchain_cpus_entries
;
2572 rcu_assign_pointer(callchain_cpus_entries
, NULL
);
2573 call_rcu(&entries
->rcu_head
, release_callchain_buffers_rcu
);
2576 static int alloc_callchain_buffers(void)
2580 struct callchain_cpus_entries
*entries
;
2583 * We can't use the percpu allocation API for data that can be
2584 * accessed from NMI. Use a temporary manual per cpu allocation
2585 * until that gets sorted out.
2587 size
= offsetof(struct callchain_cpus_entries
, cpu_entries
[nr_cpu_ids
]);
2589 entries
= kzalloc(size
, GFP_KERNEL
);
2593 size
= sizeof(struct perf_callchain_entry
) * PERF_NR_CONTEXTS
;
2595 for_each_possible_cpu(cpu
) {
2596 entries
->cpu_entries
[cpu
] = kmalloc_node(size
, GFP_KERNEL
,
2598 if (!entries
->cpu_entries
[cpu
])
2602 rcu_assign_pointer(callchain_cpus_entries
, entries
);
2607 for_each_possible_cpu(cpu
)
2608 kfree(entries
->cpu_entries
[cpu
]);
2614 static int get_callchain_buffers(void)
2619 mutex_lock(&callchain_mutex
);
2621 count
= atomic_inc_return(&nr_callchain_events
);
2622 if (WARN_ON_ONCE(count
< 1)) {
2628 /* If the allocation failed, give up */
2629 if (!callchain_cpus_entries
)
2634 err
= alloc_callchain_buffers();
2636 release_callchain_buffers();
2638 mutex_unlock(&callchain_mutex
);
2643 static void put_callchain_buffers(void)
2645 if (atomic_dec_and_mutex_lock(&nr_callchain_events
, &callchain_mutex
)) {
2646 release_callchain_buffers();
2647 mutex_unlock(&callchain_mutex
);
2651 static int get_recursion_context(int *recursion
)
2659 else if (in_softirq())
2664 if (recursion
[rctx
])
2673 static inline void put_recursion_context(int *recursion
, int rctx
)
2679 static struct perf_callchain_entry
*get_callchain_entry(int *rctx
)
2682 struct callchain_cpus_entries
*entries
;
2684 *rctx
= get_recursion_context(__get_cpu_var(callchain_recursion
));
2688 entries
= rcu_dereference(callchain_cpus_entries
);
2692 cpu
= smp_processor_id();
2694 return &entries
->cpu_entries
[cpu
][*rctx
];
2698 put_callchain_entry(int rctx
)
2700 put_recursion_context(__get_cpu_var(callchain_recursion
), rctx
);
2703 static struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
2706 struct perf_callchain_entry
*entry
;
2709 entry
= get_callchain_entry(&rctx
);
2718 if (!user_mode(regs
)) {
2719 perf_callchain_store(entry
, PERF_CONTEXT_KERNEL
);
2720 perf_callchain_kernel(entry
, regs
);
2722 regs
= task_pt_regs(current
);
2728 perf_callchain_store(entry
, PERF_CONTEXT_USER
);
2729 perf_callchain_user(entry
, regs
);
2733 put_callchain_entry(rctx
);
2739 * Initialize the perf_event context in a task_struct:
2741 static void __perf_event_init_context(struct perf_event_context
*ctx
)
2743 raw_spin_lock_init(&ctx
->lock
);
2744 mutex_init(&ctx
->mutex
);
2745 INIT_LIST_HEAD(&ctx
->pinned_groups
);
2746 INIT_LIST_HEAD(&ctx
->flexible_groups
);
2747 INIT_LIST_HEAD(&ctx
->event_list
);
2748 atomic_set(&ctx
->refcount
, 1);
2751 static struct perf_event_context
*
2752 alloc_perf_context(struct pmu
*pmu
, struct task_struct
*task
)
2754 struct perf_event_context
*ctx
;
2756 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
2760 __perf_event_init_context(ctx
);
2763 get_task_struct(task
);
2770 static struct task_struct
*
2771 find_lively_task_by_vpid(pid_t vpid
)
2773 struct task_struct
*task
;
2780 task
= find_task_by_vpid(vpid
);
2782 get_task_struct(task
);
2786 return ERR_PTR(-ESRCH
);
2788 /* Reuse ptrace permission checks for now. */
2790 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
2795 put_task_struct(task
);
2796 return ERR_PTR(err
);
2801 * Returns a matching context with refcount and pincount.
2803 static struct perf_event_context
*
2804 find_get_context(struct pmu
*pmu
, struct task_struct
*task
, int cpu
)
2806 struct perf_event_context
*ctx
;
2807 struct perf_cpu_context
*cpuctx
;
2808 unsigned long flags
;
2812 /* Must be root to operate on a CPU event: */
2813 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
2814 return ERR_PTR(-EACCES
);
2817 * We could be clever and allow to attach a event to an
2818 * offline CPU and activate it when the CPU comes up, but
2821 if (!cpu_online(cpu
))
2822 return ERR_PTR(-ENODEV
);
2824 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
2833 ctxn
= pmu
->task_ctx_nr
;
2838 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
2842 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
2844 ctx
= alloc_perf_context(pmu
, task
);
2850 mutex_lock(&task
->perf_event_mutex
);
2852 * If it has already passed perf_event_exit_task().
2853 * we must see PF_EXITING, it takes this mutex too.
2855 if (task
->flags
& PF_EXITING
)
2857 else if (task
->perf_event_ctxp
[ctxn
])
2862 rcu_assign_pointer(task
->perf_event_ctxp
[ctxn
], ctx
);
2864 mutex_unlock(&task
->perf_event_mutex
);
2866 if (unlikely(err
)) {
2878 return ERR_PTR(err
);
2881 static void perf_event_free_filter(struct perf_event
*event
);
2883 static void free_event_rcu(struct rcu_head
*head
)
2885 struct perf_event
*event
;
2887 event
= container_of(head
, struct perf_event
, rcu_head
);
2889 put_pid_ns(event
->ns
);
2890 perf_event_free_filter(event
);
2894 static void ring_buffer_put(struct ring_buffer
*rb
);
2896 static void free_event(struct perf_event
*event
)
2898 irq_work_sync(&event
->pending
);
2900 if (!event
->parent
) {
2901 if (event
->attach_state
& PERF_ATTACH_TASK
)
2902 jump_label_dec(&perf_sched_events
);
2903 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
2904 atomic_dec(&nr_mmap_events
);
2905 if (event
->attr
.comm
)
2906 atomic_dec(&nr_comm_events
);
2907 if (event
->attr
.task
)
2908 atomic_dec(&nr_task_events
);
2909 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
)
2910 put_callchain_buffers();
2911 if (is_cgroup_event(event
)) {
2912 atomic_dec(&per_cpu(perf_cgroup_events
, event
->cpu
));
2913 jump_label_dec(&perf_sched_events
);
2918 ring_buffer_put(event
->rb
);
2922 if (is_cgroup_event(event
))
2923 perf_detach_cgroup(event
);
2926 event
->destroy(event
);
2929 put_ctx(event
->ctx
);
2931 call_rcu(&event
->rcu_head
, free_event_rcu
);
2934 int perf_event_release_kernel(struct perf_event
*event
)
2936 struct perf_event_context
*ctx
= event
->ctx
;
2938 WARN_ON_ONCE(ctx
->parent_ctx
);
2940 * There are two ways this annotation is useful:
2942 * 1) there is a lock recursion from perf_event_exit_task
2943 * see the comment there.
2945 * 2) there is a lock-inversion with mmap_sem through
2946 * perf_event_read_group(), which takes faults while
2947 * holding ctx->mutex, however this is called after
2948 * the last filedesc died, so there is no possibility
2949 * to trigger the AB-BA case.
2951 mutex_lock_nested(&ctx
->mutex
, SINGLE_DEPTH_NESTING
);
2952 raw_spin_lock_irq(&ctx
->lock
);
2953 perf_group_detach(event
);
2954 raw_spin_unlock_irq(&ctx
->lock
);
2955 perf_remove_from_context(event
);
2956 mutex_unlock(&ctx
->mutex
);
2962 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
2965 * Called when the last reference to the file is gone.
2967 static int perf_release(struct inode
*inode
, struct file
*file
)
2969 struct perf_event
*event
= file
->private_data
;
2970 struct task_struct
*owner
;
2972 file
->private_data
= NULL
;
2975 owner
= ACCESS_ONCE(event
->owner
);
2977 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2978 * !owner it means the list deletion is complete and we can indeed
2979 * free this event, otherwise we need to serialize on
2980 * owner->perf_event_mutex.
2982 smp_read_barrier_depends();
2985 * Since delayed_put_task_struct() also drops the last
2986 * task reference we can safely take a new reference
2987 * while holding the rcu_read_lock().
2989 get_task_struct(owner
);
2994 mutex_lock(&owner
->perf_event_mutex
);
2996 * We have to re-check the event->owner field, if it is cleared
2997 * we raced with perf_event_exit_task(), acquiring the mutex
2998 * ensured they're done, and we can proceed with freeing the
3002 list_del_init(&event
->owner_entry
);
3003 mutex_unlock(&owner
->perf_event_mutex
);
3004 put_task_struct(owner
);
3007 return perf_event_release_kernel(event
);
3010 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
3012 struct perf_event
*child
;
3018 mutex_lock(&event
->child_mutex
);
3019 total
+= perf_event_read(event
);
3020 *enabled
+= event
->total_time_enabled
+
3021 atomic64_read(&event
->child_total_time_enabled
);
3022 *running
+= event
->total_time_running
+
3023 atomic64_read(&event
->child_total_time_running
);
3025 list_for_each_entry(child
, &event
->child_list
, child_list
) {
3026 total
+= perf_event_read(child
);
3027 *enabled
+= child
->total_time_enabled
;
3028 *running
+= child
->total_time_running
;
3030 mutex_unlock(&event
->child_mutex
);
3034 EXPORT_SYMBOL_GPL(perf_event_read_value
);
3036 static int perf_event_read_group(struct perf_event
*event
,
3037 u64 read_format
, char __user
*buf
)
3039 struct perf_event
*leader
= event
->group_leader
, *sub
;
3040 int n
= 0, size
= 0, ret
= -EFAULT
;
3041 struct perf_event_context
*ctx
= leader
->ctx
;
3043 u64 count
, enabled
, running
;
3045 mutex_lock(&ctx
->mutex
);
3046 count
= perf_event_read_value(leader
, &enabled
, &running
);
3048 values
[n
++] = 1 + leader
->nr_siblings
;
3049 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3050 values
[n
++] = enabled
;
3051 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3052 values
[n
++] = running
;
3053 values
[n
++] = count
;
3054 if (read_format
& PERF_FORMAT_ID
)
3055 values
[n
++] = primary_event_id(leader
);
3057 size
= n
* sizeof(u64
);
3059 if (copy_to_user(buf
, values
, size
))
3064 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3067 values
[n
++] = perf_event_read_value(sub
, &enabled
, &running
);
3068 if (read_format
& PERF_FORMAT_ID
)
3069 values
[n
++] = primary_event_id(sub
);
3071 size
= n
* sizeof(u64
);
3073 if (copy_to_user(buf
+ ret
, values
, size
)) {
3081 mutex_unlock(&ctx
->mutex
);
3086 static int perf_event_read_one(struct perf_event
*event
,
3087 u64 read_format
, char __user
*buf
)
3089 u64 enabled
, running
;
3093 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
3094 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3095 values
[n
++] = enabled
;
3096 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3097 values
[n
++] = running
;
3098 if (read_format
& PERF_FORMAT_ID
)
3099 values
[n
++] = primary_event_id(event
);
3101 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
3104 return n
* sizeof(u64
);
3108 * Read the performance event - simple non blocking version for now
3111 perf_read_hw(struct perf_event
*event
, char __user
*buf
, size_t count
)
3113 u64 read_format
= event
->attr
.read_format
;
3117 * Return end-of-file for a read on a event that is in
3118 * error state (i.e. because it was pinned but it couldn't be
3119 * scheduled on to the CPU at some point).
3121 if (event
->state
== PERF_EVENT_STATE_ERROR
)
3124 if (count
< event
->read_size
)
3127 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3128 if (read_format
& PERF_FORMAT_GROUP
)
3129 ret
= perf_event_read_group(event
, read_format
, buf
);
3131 ret
= perf_event_read_one(event
, read_format
, buf
);
3137 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
3139 struct perf_event
*event
= file
->private_data
;
3141 return perf_read_hw(event
, buf
, count
);
3144 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
3146 struct perf_event
*event
= file
->private_data
;
3147 struct ring_buffer
*rb
;
3148 unsigned int events
= POLL_HUP
;
3151 rb
= rcu_dereference(event
->rb
);
3153 events
= atomic_xchg(&rb
->poll
, 0);
3156 poll_wait(file
, &event
->waitq
, wait
);
3161 static void perf_event_reset(struct perf_event
*event
)
3163 (void)perf_event_read(event
);
3164 local64_set(&event
->count
, 0);
3165 perf_event_update_userpage(event
);
3169 * Holding the top-level event's child_mutex means that any
3170 * descendant process that has inherited this event will block
3171 * in sync_child_event if it goes to exit, thus satisfying the
3172 * task existence requirements of perf_event_enable/disable.
3174 static void perf_event_for_each_child(struct perf_event
*event
,
3175 void (*func
)(struct perf_event
*))
3177 struct perf_event
*child
;
3179 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3180 mutex_lock(&event
->child_mutex
);
3182 list_for_each_entry(child
, &event
->child_list
, child_list
)
3184 mutex_unlock(&event
->child_mutex
);
3187 static void perf_event_for_each(struct perf_event
*event
,
3188 void (*func
)(struct perf_event
*))
3190 struct perf_event_context
*ctx
= event
->ctx
;
3191 struct perf_event
*sibling
;
3193 WARN_ON_ONCE(ctx
->parent_ctx
);
3194 mutex_lock(&ctx
->mutex
);
3195 event
= event
->group_leader
;
3197 perf_event_for_each_child(event
, func
);
3199 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
3200 perf_event_for_each_child(event
, func
);
3201 mutex_unlock(&ctx
->mutex
);
3204 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
3206 struct perf_event_context
*ctx
= event
->ctx
;
3210 if (!is_sampling_event(event
))
3213 if (copy_from_user(&value
, arg
, sizeof(value
)))
3219 raw_spin_lock_irq(&ctx
->lock
);
3220 if (event
->attr
.freq
) {
3221 if (value
> sysctl_perf_event_sample_rate
) {
3226 event
->attr
.sample_freq
= value
;
3228 event
->attr
.sample_period
= value
;
3229 event
->hw
.sample_period
= value
;
3232 raw_spin_unlock_irq(&ctx
->lock
);
3237 static const struct file_operations perf_fops
;
3239 static struct perf_event
*perf_fget_light(int fd
, int *fput_needed
)
3243 file
= fget_light(fd
, fput_needed
);
3245 return ERR_PTR(-EBADF
);
3247 if (file
->f_op
!= &perf_fops
) {
3248 fput_light(file
, *fput_needed
);
3250 return ERR_PTR(-EBADF
);
3253 return file
->private_data
;
3256 static int perf_event_set_output(struct perf_event
*event
,
3257 struct perf_event
*output_event
);
3258 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
3260 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
3262 struct perf_event
*event
= file
->private_data
;
3263 void (*func
)(struct perf_event
*);
3267 case PERF_EVENT_IOC_ENABLE
:
3268 func
= perf_event_enable
;
3270 case PERF_EVENT_IOC_DISABLE
:
3271 func
= perf_event_disable
;
3273 case PERF_EVENT_IOC_RESET
:
3274 func
= perf_event_reset
;
3277 case PERF_EVENT_IOC_REFRESH
:
3278 return perf_event_refresh(event
, arg
);
3280 case PERF_EVENT_IOC_PERIOD
:
3281 return perf_event_period(event
, (u64 __user
*)arg
);
3283 case PERF_EVENT_IOC_SET_OUTPUT
:
3285 struct perf_event
*output_event
= NULL
;
3286 int fput_needed
= 0;
3290 output_event
= perf_fget_light(arg
, &fput_needed
);
3291 if (IS_ERR(output_event
))
3292 return PTR_ERR(output_event
);
3295 ret
= perf_event_set_output(event
, output_event
);
3297 fput_light(output_event
->filp
, fput_needed
);
3302 case PERF_EVENT_IOC_SET_FILTER
:
3303 return perf_event_set_filter(event
, (void __user
*)arg
);
3309 if (flags
& PERF_IOC_FLAG_GROUP
)
3310 perf_event_for_each(event
, func
);
3312 perf_event_for_each_child(event
, func
);
3317 int perf_event_task_enable(void)
3319 struct perf_event
*event
;
3321 mutex_lock(¤t
->perf_event_mutex
);
3322 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
3323 perf_event_for_each_child(event
, perf_event_enable
);
3324 mutex_unlock(¤t
->perf_event_mutex
);
3329 int perf_event_task_disable(void)
3331 struct perf_event
*event
;
3333 mutex_lock(¤t
->perf_event_mutex
);
3334 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
3335 perf_event_for_each_child(event
, perf_event_disable
);
3336 mutex_unlock(¤t
->perf_event_mutex
);
3341 #ifndef PERF_EVENT_INDEX_OFFSET
3342 # define PERF_EVENT_INDEX_OFFSET 0
3345 static int perf_event_index(struct perf_event
*event
)
3347 if (event
->hw
.state
& PERF_HES_STOPPED
)
3350 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
3353 return event
->hw
.idx
+ 1 - PERF_EVENT_INDEX_OFFSET
;
3356 static void calc_timer_values(struct perf_event
*event
,
3363 ctx_time
= event
->shadow_ctx_time
+ now
;
3364 *enabled
= ctx_time
- event
->tstamp_enabled
;
3365 *running
= ctx_time
- event
->tstamp_running
;
3369 * Callers need to ensure there can be no nesting of this function, otherwise
3370 * the seqlock logic goes bad. We can not serialize this because the arch
3371 * code calls this from NMI context.
3373 void perf_event_update_userpage(struct perf_event
*event
)
3375 struct perf_event_mmap_page
*userpg
;
3376 struct ring_buffer
*rb
;
3377 u64 enabled
, running
;
3381 * compute total_time_enabled, total_time_running
3382 * based on snapshot values taken when the event
3383 * was last scheduled in.
3385 * we cannot simply called update_context_time()
3386 * because of locking issue as we can be called in
3389 calc_timer_values(event
, &enabled
, &running
);
3390 rb
= rcu_dereference(event
->rb
);
3394 userpg
= rb
->user_page
;
3397 * Disable preemption so as to not let the corresponding user-space
3398 * spin too long if we get preempted.
3403 userpg
->index
= perf_event_index(event
);
3404 userpg
->offset
= perf_event_count(event
);
3405 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
3406 userpg
->offset
-= local64_read(&event
->hw
.prev_count
);
3408 userpg
->time_enabled
= enabled
+
3409 atomic64_read(&event
->child_total_time_enabled
);
3411 userpg
->time_running
= running
+
3412 atomic64_read(&event
->child_total_time_running
);
3421 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
3423 struct perf_event
*event
= vma
->vm_file
->private_data
;
3424 struct ring_buffer
*rb
;
3425 int ret
= VM_FAULT_SIGBUS
;
3427 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
3428 if (vmf
->pgoff
== 0)
3434 rb
= rcu_dereference(event
->rb
);
3438 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
3441 vmf
->page
= perf_mmap_to_page(rb
, vmf
->pgoff
);
3445 get_page(vmf
->page
);
3446 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
3447 vmf
->page
->index
= vmf
->pgoff
;
3456 static void rb_free_rcu(struct rcu_head
*rcu_head
)
3458 struct ring_buffer
*rb
;
3460 rb
= container_of(rcu_head
, struct ring_buffer
, rcu_head
);
3464 static struct ring_buffer
*ring_buffer_get(struct perf_event
*event
)
3466 struct ring_buffer
*rb
;
3469 rb
= rcu_dereference(event
->rb
);
3471 if (!atomic_inc_not_zero(&rb
->refcount
))
3479 static void ring_buffer_put(struct ring_buffer
*rb
)
3481 if (!atomic_dec_and_test(&rb
->refcount
))
3484 call_rcu(&rb
->rcu_head
, rb_free_rcu
);
3487 static void perf_mmap_open(struct vm_area_struct
*vma
)
3489 struct perf_event
*event
= vma
->vm_file
->private_data
;
3491 atomic_inc(&event
->mmap_count
);
3494 static void perf_mmap_close(struct vm_area_struct
*vma
)
3496 struct perf_event
*event
= vma
->vm_file
->private_data
;
3498 if (atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
)) {
3499 unsigned long size
= perf_data_size(event
->rb
);
3500 struct user_struct
*user
= event
->mmap_user
;
3501 struct ring_buffer
*rb
= event
->rb
;
3503 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &user
->locked_vm
);
3504 vma
->vm_mm
->pinned_vm
-= event
->mmap_locked
;
3505 rcu_assign_pointer(event
->rb
, NULL
);
3506 mutex_unlock(&event
->mmap_mutex
);
3508 ring_buffer_put(rb
);
3513 static const struct vm_operations_struct perf_mmap_vmops
= {
3514 .open
= perf_mmap_open
,
3515 .close
= perf_mmap_close
,
3516 .fault
= perf_mmap_fault
,
3517 .page_mkwrite
= perf_mmap_fault
,
3520 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
3522 struct perf_event
*event
= file
->private_data
;
3523 unsigned long user_locked
, user_lock_limit
;
3524 struct user_struct
*user
= current_user();
3525 unsigned long locked
, lock_limit
;
3526 struct ring_buffer
*rb
;
3527 unsigned long vma_size
;
3528 unsigned long nr_pages
;
3529 long user_extra
, extra
;
3530 int ret
= 0, flags
= 0;
3533 * Don't allow mmap() of inherited per-task counters. This would
3534 * create a performance issue due to all children writing to the
3537 if (event
->cpu
== -1 && event
->attr
.inherit
)
3540 if (!(vma
->vm_flags
& VM_SHARED
))
3543 vma_size
= vma
->vm_end
- vma
->vm_start
;
3544 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
3547 * If we have rb pages ensure they're a power-of-two number, so we
3548 * can do bitmasks instead of modulo.
3550 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
3553 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
3556 if (vma
->vm_pgoff
!= 0)
3559 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3560 mutex_lock(&event
->mmap_mutex
);
3562 if (event
->rb
->nr_pages
== nr_pages
)
3563 atomic_inc(&event
->rb
->refcount
);
3569 user_extra
= nr_pages
+ 1;
3570 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
3573 * Increase the limit linearly with more CPUs:
3575 user_lock_limit
*= num_online_cpus();
3577 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
3580 if (user_locked
> user_lock_limit
)
3581 extra
= user_locked
- user_lock_limit
;
3583 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
3584 lock_limit
>>= PAGE_SHIFT
;
3585 locked
= vma
->vm_mm
->pinned_vm
+ extra
;
3587 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
3588 !capable(CAP_IPC_LOCK
)) {
3595 if (vma
->vm_flags
& VM_WRITE
)
3596 flags
|= RING_BUFFER_WRITABLE
;
3598 rb
= rb_alloc(nr_pages
,
3599 event
->attr
.watermark
? event
->attr
.wakeup_watermark
: 0,
3606 rcu_assign_pointer(event
->rb
, rb
);
3608 atomic_long_add(user_extra
, &user
->locked_vm
);
3609 event
->mmap_locked
= extra
;
3610 event
->mmap_user
= get_current_user();
3611 vma
->vm_mm
->pinned_vm
+= event
->mmap_locked
;
3615 atomic_inc(&event
->mmap_count
);
3616 mutex_unlock(&event
->mmap_mutex
);
3618 vma
->vm_flags
|= VM_RESERVED
;
3619 vma
->vm_ops
= &perf_mmap_vmops
;
3624 static int perf_fasync(int fd
, struct file
*filp
, int on
)
3626 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
3627 struct perf_event
*event
= filp
->private_data
;
3630 mutex_lock(&inode
->i_mutex
);
3631 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
3632 mutex_unlock(&inode
->i_mutex
);
3640 static const struct file_operations perf_fops
= {
3641 .llseek
= no_llseek
,
3642 .release
= perf_release
,
3645 .unlocked_ioctl
= perf_ioctl
,
3646 .compat_ioctl
= perf_ioctl
,
3648 .fasync
= perf_fasync
,
3654 * If there's data, ensure we set the poll() state and publish everything
3655 * to user-space before waking everybody up.
3658 void perf_event_wakeup(struct perf_event
*event
)
3660 wake_up_all(&event
->waitq
);
3662 if (event
->pending_kill
) {
3663 kill_fasync(&event
->fasync
, SIGIO
, event
->pending_kill
);
3664 event
->pending_kill
= 0;
3668 static void perf_pending_event(struct irq_work
*entry
)
3670 struct perf_event
*event
= container_of(entry
,
3671 struct perf_event
, pending
);
3673 if (event
->pending_disable
) {
3674 event
->pending_disable
= 0;
3675 __perf_event_disable(event
);
3678 if (event
->pending_wakeup
) {
3679 event
->pending_wakeup
= 0;
3680 perf_event_wakeup(event
);
3685 * We assume there is only KVM supporting the callbacks.
3686 * Later on, we might change it to a list if there is
3687 * another virtualization implementation supporting the callbacks.
3689 struct perf_guest_info_callbacks
*perf_guest_cbs
;
3691 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
3693 perf_guest_cbs
= cbs
;
3696 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks
);
3698 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
3700 perf_guest_cbs
= NULL
;
3703 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks
);
3705 static void __perf_event_header__init_id(struct perf_event_header
*header
,
3706 struct perf_sample_data
*data
,
3707 struct perf_event
*event
)
3709 u64 sample_type
= event
->attr
.sample_type
;
3711 data
->type
= sample_type
;
3712 header
->size
+= event
->id_header_size
;
3714 if (sample_type
& PERF_SAMPLE_TID
) {
3715 /* namespace issues */
3716 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
3717 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
3720 if (sample_type
& PERF_SAMPLE_TIME
)
3721 data
->time
= perf_clock();
3723 if (sample_type
& PERF_SAMPLE_ID
)
3724 data
->id
= primary_event_id(event
);
3726 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
3727 data
->stream_id
= event
->id
;
3729 if (sample_type
& PERF_SAMPLE_CPU
) {
3730 data
->cpu_entry
.cpu
= raw_smp_processor_id();
3731 data
->cpu_entry
.reserved
= 0;
3735 void perf_event_header__init_id(struct perf_event_header
*header
,
3736 struct perf_sample_data
*data
,
3737 struct perf_event
*event
)
3739 if (event
->attr
.sample_id_all
)
3740 __perf_event_header__init_id(header
, data
, event
);
3743 static void __perf_event__output_id_sample(struct perf_output_handle
*handle
,
3744 struct perf_sample_data
*data
)
3746 u64 sample_type
= data
->type
;
3748 if (sample_type
& PERF_SAMPLE_TID
)
3749 perf_output_put(handle
, data
->tid_entry
);
3751 if (sample_type
& PERF_SAMPLE_TIME
)
3752 perf_output_put(handle
, data
->time
);
3754 if (sample_type
& PERF_SAMPLE_ID
)
3755 perf_output_put(handle
, data
->id
);
3757 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
3758 perf_output_put(handle
, data
->stream_id
);
3760 if (sample_type
& PERF_SAMPLE_CPU
)
3761 perf_output_put(handle
, data
->cpu_entry
);
3764 void perf_event__output_id_sample(struct perf_event
*event
,
3765 struct perf_output_handle
*handle
,
3766 struct perf_sample_data
*sample
)
3768 if (event
->attr
.sample_id_all
)
3769 __perf_event__output_id_sample(handle
, sample
);
3772 static void perf_output_read_one(struct perf_output_handle
*handle
,
3773 struct perf_event
*event
,
3774 u64 enabled
, u64 running
)
3776 u64 read_format
= event
->attr
.read_format
;
3780 values
[n
++] = perf_event_count(event
);
3781 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
3782 values
[n
++] = enabled
+
3783 atomic64_read(&event
->child_total_time_enabled
);
3785 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
3786 values
[n
++] = running
+
3787 atomic64_read(&event
->child_total_time_running
);
3789 if (read_format
& PERF_FORMAT_ID
)
3790 values
[n
++] = primary_event_id(event
);
3792 __output_copy(handle
, values
, n
* sizeof(u64
));
3796 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3798 static void perf_output_read_group(struct perf_output_handle
*handle
,
3799 struct perf_event
*event
,
3800 u64 enabled
, u64 running
)
3802 struct perf_event
*leader
= event
->group_leader
, *sub
;
3803 u64 read_format
= event
->attr
.read_format
;
3807 values
[n
++] = 1 + leader
->nr_siblings
;
3809 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3810 values
[n
++] = enabled
;
3812 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3813 values
[n
++] = running
;
3815 if (leader
!= event
)
3816 leader
->pmu
->read(leader
);
3818 values
[n
++] = perf_event_count(leader
);
3819 if (read_format
& PERF_FORMAT_ID
)
3820 values
[n
++] = primary_event_id(leader
);
3822 __output_copy(handle
, values
, n
* sizeof(u64
));
3824 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3828 sub
->pmu
->read(sub
);
3830 values
[n
++] = perf_event_count(sub
);
3831 if (read_format
& PERF_FORMAT_ID
)
3832 values
[n
++] = primary_event_id(sub
);
3834 __output_copy(handle
, values
, n
* sizeof(u64
));
3838 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
3839 PERF_FORMAT_TOTAL_TIME_RUNNING)
3841 static void perf_output_read(struct perf_output_handle
*handle
,
3842 struct perf_event
*event
)
3844 u64 enabled
= 0, running
= 0;
3845 u64 read_format
= event
->attr
.read_format
;
3848 * compute total_time_enabled, total_time_running
3849 * based on snapshot values taken when the event
3850 * was last scheduled in.
3852 * we cannot simply called update_context_time()
3853 * because of locking issue as we are called in
3856 if (read_format
& PERF_FORMAT_TOTAL_TIMES
)
3857 calc_timer_values(event
, &enabled
, &running
);
3859 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
3860 perf_output_read_group(handle
, event
, enabled
, running
);
3862 perf_output_read_one(handle
, event
, enabled
, running
);
3865 void perf_output_sample(struct perf_output_handle
*handle
,
3866 struct perf_event_header
*header
,
3867 struct perf_sample_data
*data
,
3868 struct perf_event
*event
)
3870 u64 sample_type
= data
->type
;
3872 perf_output_put(handle
, *header
);
3874 if (sample_type
& PERF_SAMPLE_IP
)
3875 perf_output_put(handle
, data
->ip
);
3877 if (sample_type
& PERF_SAMPLE_TID
)
3878 perf_output_put(handle
, data
->tid_entry
);
3880 if (sample_type
& PERF_SAMPLE_TIME
)
3881 perf_output_put(handle
, data
->time
);
3883 if (sample_type
& PERF_SAMPLE_ADDR
)
3884 perf_output_put(handle
, data
->addr
);
3886 if (sample_type
& PERF_SAMPLE_ID
)
3887 perf_output_put(handle
, data
->id
);
3889 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
3890 perf_output_put(handle
, data
->stream_id
);
3892 if (sample_type
& PERF_SAMPLE_CPU
)
3893 perf_output_put(handle
, data
->cpu_entry
);
3895 if (sample_type
& PERF_SAMPLE_PERIOD
)
3896 perf_output_put(handle
, data
->period
);
3898 if (sample_type
& PERF_SAMPLE_READ
)
3899 perf_output_read(handle
, event
);
3901 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3902 if (data
->callchain
) {
3905 if (data
->callchain
)
3906 size
+= data
->callchain
->nr
;
3908 size
*= sizeof(u64
);
3910 __output_copy(handle
, data
->callchain
, size
);
3913 perf_output_put(handle
, nr
);
3917 if (sample_type
& PERF_SAMPLE_RAW
) {
3919 perf_output_put(handle
, data
->raw
->size
);
3920 __output_copy(handle
, data
->raw
->data
,
3927 .size
= sizeof(u32
),
3930 perf_output_put(handle
, raw
);
3934 if (!event
->attr
.watermark
) {
3935 int wakeup_events
= event
->attr
.wakeup_events
;
3937 if (wakeup_events
) {
3938 struct ring_buffer
*rb
= handle
->rb
;
3939 int events
= local_inc_return(&rb
->events
);
3941 if (events
>= wakeup_events
) {
3942 local_sub(wakeup_events
, &rb
->events
);
3943 local_inc(&rb
->wakeup
);
3949 void perf_prepare_sample(struct perf_event_header
*header
,
3950 struct perf_sample_data
*data
,
3951 struct perf_event
*event
,
3952 struct pt_regs
*regs
)
3954 u64 sample_type
= event
->attr
.sample_type
;
3956 header
->type
= PERF_RECORD_SAMPLE
;
3957 header
->size
= sizeof(*header
) + event
->header_size
;
3960 header
->misc
|= perf_misc_flags(regs
);
3962 __perf_event_header__init_id(header
, data
, event
);
3964 if (sample_type
& PERF_SAMPLE_IP
)
3965 data
->ip
= perf_instruction_pointer(regs
);
3967 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3970 data
->callchain
= perf_callchain(regs
);
3972 if (data
->callchain
)
3973 size
+= data
->callchain
->nr
;
3975 header
->size
+= size
* sizeof(u64
);
3978 if (sample_type
& PERF_SAMPLE_RAW
) {
3979 int size
= sizeof(u32
);
3982 size
+= data
->raw
->size
;
3984 size
+= sizeof(u32
);
3986 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
3987 header
->size
+= size
;
3991 static void perf_event_output(struct perf_event
*event
,
3992 struct perf_sample_data
*data
,
3993 struct pt_regs
*regs
)
3995 struct perf_output_handle handle
;
3996 struct perf_event_header header
;
3998 /* protect the callchain buffers */
4001 perf_prepare_sample(&header
, data
, event
, regs
);
4003 if (perf_output_begin(&handle
, event
, header
.size
))
4006 perf_output_sample(&handle
, &header
, data
, event
);
4008 perf_output_end(&handle
);
4018 struct perf_read_event
{
4019 struct perf_event_header header
;
4026 perf_event_read_event(struct perf_event
*event
,
4027 struct task_struct
*task
)
4029 struct perf_output_handle handle
;
4030 struct perf_sample_data sample
;
4031 struct perf_read_event read_event
= {
4033 .type
= PERF_RECORD_READ
,
4035 .size
= sizeof(read_event
) + event
->read_size
,
4037 .pid
= perf_event_pid(event
, task
),
4038 .tid
= perf_event_tid(event
, task
),
4042 perf_event_header__init_id(&read_event
.header
, &sample
, event
);
4043 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
);
4047 perf_output_put(&handle
, read_event
);
4048 perf_output_read(&handle
, event
);
4049 perf_event__output_id_sample(event
, &handle
, &sample
);
4051 perf_output_end(&handle
);
4055 * task tracking -- fork/exit
4057 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4060 struct perf_task_event
{
4061 struct task_struct
*task
;
4062 struct perf_event_context
*task_ctx
;
4065 struct perf_event_header header
;
4075 static void perf_event_task_output(struct perf_event
*event
,
4076 struct perf_task_event
*task_event
)
4078 struct perf_output_handle handle
;
4079 struct perf_sample_data sample
;
4080 struct task_struct
*task
= task_event
->task
;
4081 int ret
, size
= task_event
->event_id
.header
.size
;
4083 perf_event_header__init_id(&task_event
->event_id
.header
, &sample
, event
);
4085 ret
= perf_output_begin(&handle
, event
,
4086 task_event
->event_id
.header
.size
);
4090 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
4091 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
4093 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
4094 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
4096 perf_output_put(&handle
, task_event
->event_id
);
4098 perf_event__output_id_sample(event
, &handle
, &sample
);
4100 perf_output_end(&handle
);
4102 task_event
->event_id
.header
.size
= size
;
4105 static int perf_event_task_match(struct perf_event
*event
)
4107 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
4110 if (!event_filter_match(event
))
4113 if (event
->attr
.comm
|| event
->attr
.mmap
||
4114 event
->attr
.mmap_data
|| event
->attr
.task
)
4120 static void perf_event_task_ctx(struct perf_event_context
*ctx
,
4121 struct perf_task_event
*task_event
)
4123 struct perf_event
*event
;
4125 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
4126 if (perf_event_task_match(event
))
4127 perf_event_task_output(event
, task_event
);
4131 static void perf_event_task_event(struct perf_task_event
*task_event
)
4133 struct perf_cpu_context
*cpuctx
;
4134 struct perf_event_context
*ctx
;
4139 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
4140 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
4141 if (cpuctx
->active_pmu
!= pmu
)
4143 perf_event_task_ctx(&cpuctx
->ctx
, task_event
);
4145 ctx
= task_event
->task_ctx
;
4147 ctxn
= pmu
->task_ctx_nr
;
4150 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
4153 perf_event_task_ctx(ctx
, task_event
);
4155 put_cpu_ptr(pmu
->pmu_cpu_context
);
4160 static void perf_event_task(struct task_struct
*task
,
4161 struct perf_event_context
*task_ctx
,
4164 struct perf_task_event task_event
;
4166 if (!atomic_read(&nr_comm_events
) &&
4167 !atomic_read(&nr_mmap_events
) &&
4168 !atomic_read(&nr_task_events
))
4171 task_event
= (struct perf_task_event
){
4173 .task_ctx
= task_ctx
,
4176 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
4178 .size
= sizeof(task_event
.event_id
),
4184 .time
= perf_clock(),
4188 perf_event_task_event(&task_event
);
4191 void perf_event_fork(struct task_struct
*task
)
4193 perf_event_task(task
, NULL
, 1);
4200 struct perf_comm_event
{
4201 struct task_struct
*task
;
4206 struct perf_event_header header
;
4213 static void perf_event_comm_output(struct perf_event
*event
,
4214 struct perf_comm_event
*comm_event
)
4216 struct perf_output_handle handle
;
4217 struct perf_sample_data sample
;
4218 int size
= comm_event
->event_id
.header
.size
;
4221 perf_event_header__init_id(&comm_event
->event_id
.header
, &sample
, event
);
4222 ret
= perf_output_begin(&handle
, event
,
4223 comm_event
->event_id
.header
.size
);
4228 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
4229 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
4231 perf_output_put(&handle
, comm_event
->event_id
);
4232 __output_copy(&handle
, comm_event
->comm
,
4233 comm_event
->comm_size
);
4235 perf_event__output_id_sample(event
, &handle
, &sample
);
4237 perf_output_end(&handle
);
4239 comm_event
->event_id
.header
.size
= size
;
4242 static int perf_event_comm_match(struct perf_event
*event
)
4244 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
4247 if (!event_filter_match(event
))
4250 if (event
->attr
.comm
)
4256 static void perf_event_comm_ctx(struct perf_event_context
*ctx
,
4257 struct perf_comm_event
*comm_event
)
4259 struct perf_event
*event
;
4261 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
4262 if (perf_event_comm_match(event
))
4263 perf_event_comm_output(event
, comm_event
);
4267 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
4269 struct perf_cpu_context
*cpuctx
;
4270 struct perf_event_context
*ctx
;
4271 char comm
[TASK_COMM_LEN
];
4276 memset(comm
, 0, sizeof(comm
));
4277 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
4278 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
4280 comm_event
->comm
= comm
;
4281 comm_event
->comm_size
= size
;
4283 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
4285 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
4286 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
4287 if (cpuctx
->active_pmu
!= pmu
)
4289 perf_event_comm_ctx(&cpuctx
->ctx
, comm_event
);
4291 ctxn
= pmu
->task_ctx_nr
;
4295 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
4297 perf_event_comm_ctx(ctx
, comm_event
);
4299 put_cpu_ptr(pmu
->pmu_cpu_context
);
4304 void perf_event_comm(struct task_struct
*task
)
4306 struct perf_comm_event comm_event
;
4307 struct perf_event_context
*ctx
;
4310 for_each_task_context_nr(ctxn
) {
4311 ctx
= task
->perf_event_ctxp
[ctxn
];
4315 perf_event_enable_on_exec(ctx
);
4318 if (!atomic_read(&nr_comm_events
))
4321 comm_event
= (struct perf_comm_event
){
4327 .type
= PERF_RECORD_COMM
,
4336 perf_event_comm_event(&comm_event
);
4343 struct perf_mmap_event
{
4344 struct vm_area_struct
*vma
;
4346 const char *file_name
;
4350 struct perf_event_header header
;
4360 static void perf_event_mmap_output(struct perf_event
*event
,
4361 struct perf_mmap_event
*mmap_event
)
4363 struct perf_output_handle handle
;
4364 struct perf_sample_data sample
;
4365 int size
= mmap_event
->event_id
.header
.size
;
4368 perf_event_header__init_id(&mmap_event
->event_id
.header
, &sample
, event
);
4369 ret
= perf_output_begin(&handle
, event
,
4370 mmap_event
->event_id
.header
.size
);
4374 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
4375 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
4377 perf_output_put(&handle
, mmap_event
->event_id
);
4378 __output_copy(&handle
, mmap_event
->file_name
,
4379 mmap_event
->file_size
);
4381 perf_event__output_id_sample(event
, &handle
, &sample
);
4383 perf_output_end(&handle
);
4385 mmap_event
->event_id
.header
.size
= size
;
4388 static int perf_event_mmap_match(struct perf_event
*event
,
4389 struct perf_mmap_event
*mmap_event
,
4392 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
4395 if (!event_filter_match(event
))
4398 if ((!executable
&& event
->attr
.mmap_data
) ||
4399 (executable
&& event
->attr
.mmap
))
4405 static void perf_event_mmap_ctx(struct perf_event_context
*ctx
,
4406 struct perf_mmap_event
*mmap_event
,
4409 struct perf_event
*event
;
4411 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
4412 if (perf_event_mmap_match(event
, mmap_event
, executable
))
4413 perf_event_mmap_output(event
, mmap_event
);
4417 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
4419 struct perf_cpu_context
*cpuctx
;
4420 struct perf_event_context
*ctx
;
4421 struct vm_area_struct
*vma
= mmap_event
->vma
;
4422 struct file
*file
= vma
->vm_file
;
4430 memset(tmp
, 0, sizeof(tmp
));
4434 * d_path works from the end of the rb backwards, so we
4435 * need to add enough zero bytes after the string to handle
4436 * the 64bit alignment we do later.
4438 buf
= kzalloc(PATH_MAX
+ sizeof(u64
), GFP_KERNEL
);
4440 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
4443 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
4445 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
4449 if (arch_vma_name(mmap_event
->vma
)) {
4450 name
= strncpy(tmp
, arch_vma_name(mmap_event
->vma
),
4456 name
= strncpy(tmp
, "[vdso]", sizeof(tmp
));
4458 } else if (vma
->vm_start
<= vma
->vm_mm
->start_brk
&&
4459 vma
->vm_end
>= vma
->vm_mm
->brk
) {
4460 name
= strncpy(tmp
, "[heap]", sizeof(tmp
));
4462 } else if (vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
4463 vma
->vm_end
>= vma
->vm_mm
->start_stack
) {
4464 name
= strncpy(tmp
, "[stack]", sizeof(tmp
));
4468 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
4473 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
4475 mmap_event
->file_name
= name
;
4476 mmap_event
->file_size
= size
;
4478 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
4481 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
4482 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
4483 if (cpuctx
->active_pmu
!= pmu
)
4485 perf_event_mmap_ctx(&cpuctx
->ctx
, mmap_event
,
4486 vma
->vm_flags
& VM_EXEC
);
4488 ctxn
= pmu
->task_ctx_nr
;
4492 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
4494 perf_event_mmap_ctx(ctx
, mmap_event
,
4495 vma
->vm_flags
& VM_EXEC
);
4498 put_cpu_ptr(pmu
->pmu_cpu_context
);
4505 void perf_event_mmap(struct vm_area_struct
*vma
)
4507 struct perf_mmap_event mmap_event
;
4509 if (!atomic_read(&nr_mmap_events
))
4512 mmap_event
= (struct perf_mmap_event
){
4518 .type
= PERF_RECORD_MMAP
,
4519 .misc
= PERF_RECORD_MISC_USER
,
4524 .start
= vma
->vm_start
,
4525 .len
= vma
->vm_end
- vma
->vm_start
,
4526 .pgoff
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
,
4530 perf_event_mmap_event(&mmap_event
);
4534 * IRQ throttle logging
4537 static void perf_log_throttle(struct perf_event
*event
, int enable
)
4539 struct perf_output_handle handle
;
4540 struct perf_sample_data sample
;
4544 struct perf_event_header header
;
4548 } throttle_event
= {
4550 .type
= PERF_RECORD_THROTTLE
,
4552 .size
= sizeof(throttle_event
),
4554 .time
= perf_clock(),
4555 .id
= primary_event_id(event
),
4556 .stream_id
= event
->id
,
4560 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
4562 perf_event_header__init_id(&throttle_event
.header
, &sample
, event
);
4564 ret
= perf_output_begin(&handle
, event
,
4565 throttle_event
.header
.size
);
4569 perf_output_put(&handle
, throttle_event
);
4570 perf_event__output_id_sample(event
, &handle
, &sample
);
4571 perf_output_end(&handle
);
4575 * Generic event overflow handling, sampling.
4578 static int __perf_event_overflow(struct perf_event
*event
,
4579 int throttle
, struct perf_sample_data
*data
,
4580 struct pt_regs
*regs
)
4582 int events
= atomic_read(&event
->event_limit
);
4583 struct hw_perf_event
*hwc
= &event
->hw
;
4587 * Non-sampling counters might still use the PMI to fold short
4588 * hardware counters, ignore those.
4590 if (unlikely(!is_sampling_event(event
)))
4593 if (unlikely(hwc
->interrupts
>= max_samples_per_tick
)) {
4595 hwc
->interrupts
= MAX_INTERRUPTS
;
4596 perf_log_throttle(event
, 0);
4602 if (event
->attr
.freq
) {
4603 u64 now
= perf_clock();
4604 s64 delta
= now
- hwc
->freq_time_stamp
;
4606 hwc
->freq_time_stamp
= now
;
4608 if (delta
> 0 && delta
< 2*TICK_NSEC
)
4609 perf_adjust_period(event
, delta
, hwc
->last_period
);
4613 * XXX event_limit might not quite work as expected on inherited
4617 event
->pending_kill
= POLL_IN
;
4618 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
4620 event
->pending_kill
= POLL_HUP
;
4621 event
->pending_disable
= 1;
4622 irq_work_queue(&event
->pending
);
4625 if (event
->overflow_handler
)
4626 event
->overflow_handler(event
, data
, regs
);
4628 perf_event_output(event
, data
, regs
);
4630 if (event
->fasync
&& event
->pending_kill
) {
4631 event
->pending_wakeup
= 1;
4632 irq_work_queue(&event
->pending
);
4638 int perf_event_overflow(struct perf_event
*event
,
4639 struct perf_sample_data
*data
,
4640 struct pt_regs
*regs
)
4642 return __perf_event_overflow(event
, 1, data
, regs
);
4646 * Generic software event infrastructure
4649 struct swevent_htable
{
4650 struct swevent_hlist
*swevent_hlist
;
4651 struct mutex hlist_mutex
;
4654 /* Recursion avoidance in each contexts */
4655 int recursion
[PERF_NR_CONTEXTS
];
4658 static DEFINE_PER_CPU(struct swevent_htable
, swevent_htable
);
4661 * We directly increment event->count and keep a second value in
4662 * event->hw.period_left to count intervals. This period event
4663 * is kept in the range [-sample_period, 0] so that we can use the
4667 static u64
perf_swevent_set_period(struct perf_event
*event
)
4669 struct hw_perf_event
*hwc
= &event
->hw
;
4670 u64 period
= hwc
->last_period
;
4674 hwc
->last_period
= hwc
->sample_period
;
4677 old
= val
= local64_read(&hwc
->period_left
);
4681 nr
= div64_u64(period
+ val
, period
);
4682 offset
= nr
* period
;
4684 if (local64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
4690 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
4691 struct perf_sample_data
*data
,
4692 struct pt_regs
*regs
)
4694 struct hw_perf_event
*hwc
= &event
->hw
;
4697 data
->period
= event
->hw
.last_period
;
4699 overflow
= perf_swevent_set_period(event
);
4701 if (hwc
->interrupts
== MAX_INTERRUPTS
)
4704 for (; overflow
; overflow
--) {
4705 if (__perf_event_overflow(event
, throttle
,
4708 * We inhibit the overflow from happening when
4709 * hwc->interrupts == MAX_INTERRUPTS.
4717 static void perf_swevent_event(struct perf_event
*event
, u64 nr
,
4718 struct perf_sample_data
*data
,
4719 struct pt_regs
*regs
)
4721 struct hw_perf_event
*hwc
= &event
->hw
;
4723 local64_add(nr
, &event
->count
);
4728 if (!is_sampling_event(event
))
4731 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
4732 return perf_swevent_overflow(event
, 1, data
, regs
);
4734 if (local64_add_negative(nr
, &hwc
->period_left
))
4737 perf_swevent_overflow(event
, 0, data
, regs
);
4740 static int perf_exclude_event(struct perf_event
*event
,
4741 struct pt_regs
*regs
)
4743 if (event
->hw
.state
& PERF_HES_STOPPED
)
4747 if (event
->attr
.exclude_user
&& user_mode(regs
))
4750 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
4757 static int perf_swevent_match(struct perf_event
*event
,
4758 enum perf_type_id type
,
4760 struct perf_sample_data
*data
,
4761 struct pt_regs
*regs
)
4763 if (event
->attr
.type
!= type
)
4766 if (event
->attr
.config
!= event_id
)
4769 if (perf_exclude_event(event
, regs
))
4775 static inline u64
swevent_hash(u64 type
, u32 event_id
)
4777 u64 val
= event_id
| (type
<< 32);
4779 return hash_64(val
, SWEVENT_HLIST_BITS
);
4782 static inline struct hlist_head
*
4783 __find_swevent_head(struct swevent_hlist
*hlist
, u64 type
, u32 event_id
)
4785 u64 hash
= swevent_hash(type
, event_id
);
4787 return &hlist
->heads
[hash
];
4790 /* For the read side: events when they trigger */
4791 static inline struct hlist_head
*
4792 find_swevent_head_rcu(struct swevent_htable
*swhash
, u64 type
, u32 event_id
)
4794 struct swevent_hlist
*hlist
;
4796 hlist
= rcu_dereference(swhash
->swevent_hlist
);
4800 return __find_swevent_head(hlist
, type
, event_id
);
4803 /* For the event head insertion and removal in the hlist */
4804 static inline struct hlist_head
*
4805 find_swevent_head(struct swevent_htable
*swhash
, struct perf_event
*event
)
4807 struct swevent_hlist
*hlist
;
4808 u32 event_id
= event
->attr
.config
;
4809 u64 type
= event
->attr
.type
;
4812 * Event scheduling is always serialized against hlist allocation
4813 * and release. Which makes the protected version suitable here.
4814 * The context lock guarantees that.
4816 hlist
= rcu_dereference_protected(swhash
->swevent_hlist
,
4817 lockdep_is_held(&event
->ctx
->lock
));
4821 return __find_swevent_head(hlist
, type
, event_id
);
4824 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
4826 struct perf_sample_data
*data
,
4827 struct pt_regs
*regs
)
4829 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
4830 struct perf_event
*event
;
4831 struct hlist_node
*node
;
4832 struct hlist_head
*head
;
4835 head
= find_swevent_head_rcu(swhash
, type
, event_id
);
4839 hlist_for_each_entry_rcu(event
, node
, head
, hlist_entry
) {
4840 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
4841 perf_swevent_event(event
, nr
, data
, regs
);
4847 int perf_swevent_get_recursion_context(void)
4849 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
4851 return get_recursion_context(swhash
->recursion
);
4853 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
4855 inline void perf_swevent_put_recursion_context(int rctx
)
4857 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
4859 put_recursion_context(swhash
->recursion
, rctx
);
4862 void __perf_sw_event(u32 event_id
, u64 nr
, struct pt_regs
*regs
, u64 addr
)
4864 struct perf_sample_data data
;
4867 preempt_disable_notrace();
4868 rctx
= perf_swevent_get_recursion_context();
4872 perf_sample_data_init(&data
, addr
);
4874 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, &data
, regs
);
4876 perf_swevent_put_recursion_context(rctx
);
4877 preempt_enable_notrace();
4880 static void perf_swevent_read(struct perf_event
*event
)
4884 static int perf_swevent_add(struct perf_event
*event
, int flags
)
4886 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
4887 struct hw_perf_event
*hwc
= &event
->hw
;
4888 struct hlist_head
*head
;
4890 if (is_sampling_event(event
)) {
4891 hwc
->last_period
= hwc
->sample_period
;
4892 perf_swevent_set_period(event
);
4895 hwc
->state
= !(flags
& PERF_EF_START
);
4897 head
= find_swevent_head(swhash
, event
);
4898 if (WARN_ON_ONCE(!head
))
4901 hlist_add_head_rcu(&event
->hlist_entry
, head
);
4906 static void perf_swevent_del(struct perf_event
*event
, int flags
)
4908 hlist_del_rcu(&event
->hlist_entry
);
4911 static void perf_swevent_start(struct perf_event
*event
, int flags
)
4913 event
->hw
.state
= 0;
4916 static void perf_swevent_stop(struct perf_event
*event
, int flags
)
4918 event
->hw
.state
= PERF_HES_STOPPED
;
4921 /* Deref the hlist from the update side */
4922 static inline struct swevent_hlist
*
4923 swevent_hlist_deref(struct swevent_htable
*swhash
)
4925 return rcu_dereference_protected(swhash
->swevent_hlist
,
4926 lockdep_is_held(&swhash
->hlist_mutex
));
4929 static void swevent_hlist_release(struct swevent_htable
*swhash
)
4931 struct swevent_hlist
*hlist
= swevent_hlist_deref(swhash
);
4936 rcu_assign_pointer(swhash
->swevent_hlist
, NULL
);
4937 kfree_rcu(hlist
, rcu_head
);
4940 static void swevent_hlist_put_cpu(struct perf_event
*event
, int cpu
)
4942 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
4944 mutex_lock(&swhash
->hlist_mutex
);
4946 if (!--swhash
->hlist_refcount
)
4947 swevent_hlist_release(swhash
);
4949 mutex_unlock(&swhash
->hlist_mutex
);
4952 static void swevent_hlist_put(struct perf_event
*event
)
4956 if (event
->cpu
!= -1) {
4957 swevent_hlist_put_cpu(event
, event
->cpu
);
4961 for_each_possible_cpu(cpu
)
4962 swevent_hlist_put_cpu(event
, cpu
);
4965 static int swevent_hlist_get_cpu(struct perf_event
*event
, int cpu
)
4967 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
4970 mutex_lock(&swhash
->hlist_mutex
);
4972 if (!swevent_hlist_deref(swhash
) && cpu_online(cpu
)) {
4973 struct swevent_hlist
*hlist
;
4975 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
4980 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
4982 swhash
->hlist_refcount
++;
4984 mutex_unlock(&swhash
->hlist_mutex
);
4989 static int swevent_hlist_get(struct perf_event
*event
)
4992 int cpu
, failed_cpu
;
4994 if (event
->cpu
!= -1)
4995 return swevent_hlist_get_cpu(event
, event
->cpu
);
4998 for_each_possible_cpu(cpu
) {
4999 err
= swevent_hlist_get_cpu(event
, cpu
);
5009 for_each_possible_cpu(cpu
) {
5010 if (cpu
== failed_cpu
)
5012 swevent_hlist_put_cpu(event
, cpu
);
5019 struct jump_label_key perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
5021 static void sw_perf_event_destroy(struct perf_event
*event
)
5023 u64 event_id
= event
->attr
.config
;
5025 WARN_ON(event
->parent
);
5027 jump_label_dec(&perf_swevent_enabled
[event_id
]);
5028 swevent_hlist_put(event
);
5031 static int perf_swevent_init(struct perf_event
*event
)
5033 int event_id
= event
->attr
.config
;
5035 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
5039 case PERF_COUNT_SW_CPU_CLOCK
:
5040 case PERF_COUNT_SW_TASK_CLOCK
:
5047 if (event_id
>= PERF_COUNT_SW_MAX
)
5050 if (!event
->parent
) {
5053 err
= swevent_hlist_get(event
);
5057 jump_label_inc(&perf_swevent_enabled
[event_id
]);
5058 event
->destroy
= sw_perf_event_destroy
;
5064 static struct pmu perf_swevent
= {
5065 .task_ctx_nr
= perf_sw_context
,
5067 .event_init
= perf_swevent_init
,
5068 .add
= perf_swevent_add
,
5069 .del
= perf_swevent_del
,
5070 .start
= perf_swevent_start
,
5071 .stop
= perf_swevent_stop
,
5072 .read
= perf_swevent_read
,
5075 #ifdef CONFIG_EVENT_TRACING
5077 static int perf_tp_filter_match(struct perf_event
*event
,
5078 struct perf_sample_data
*data
)
5080 void *record
= data
->raw
->data
;
5082 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
5087 static int perf_tp_event_match(struct perf_event
*event
,
5088 struct perf_sample_data
*data
,
5089 struct pt_regs
*regs
)
5091 if (event
->hw
.state
& PERF_HES_STOPPED
)
5094 * All tracepoints are from kernel-space.
5096 if (event
->attr
.exclude_kernel
)
5099 if (!perf_tp_filter_match(event
, data
))
5105 void perf_tp_event(u64 addr
, u64 count
, void *record
, int entry_size
,
5106 struct pt_regs
*regs
, struct hlist_head
*head
, int rctx
)
5108 struct perf_sample_data data
;
5109 struct perf_event
*event
;
5110 struct hlist_node
*node
;
5112 struct perf_raw_record raw
= {
5117 perf_sample_data_init(&data
, addr
);
5120 hlist_for_each_entry_rcu(event
, node
, head
, hlist_entry
) {
5121 if (perf_tp_event_match(event
, &data
, regs
))
5122 perf_swevent_event(event
, count
, &data
, regs
);
5125 perf_swevent_put_recursion_context(rctx
);
5127 EXPORT_SYMBOL_GPL(perf_tp_event
);
5129 static void tp_perf_event_destroy(struct perf_event
*event
)
5131 perf_trace_destroy(event
);
5134 static int perf_tp_event_init(struct perf_event
*event
)
5138 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
5141 err
= perf_trace_init(event
);
5145 event
->destroy
= tp_perf_event_destroy
;
5150 static struct pmu perf_tracepoint
= {
5151 .task_ctx_nr
= perf_sw_context
,
5153 .event_init
= perf_tp_event_init
,
5154 .add
= perf_trace_add
,
5155 .del
= perf_trace_del
,
5156 .start
= perf_swevent_start
,
5157 .stop
= perf_swevent_stop
,
5158 .read
= perf_swevent_read
,
5161 static inline void perf_tp_register(void)
5163 perf_pmu_register(&perf_tracepoint
, "tracepoint", PERF_TYPE_TRACEPOINT
);
5166 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
5171 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
5174 filter_str
= strndup_user(arg
, PAGE_SIZE
);
5175 if (IS_ERR(filter_str
))
5176 return PTR_ERR(filter_str
);
5178 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
, filter_str
);
5184 static void perf_event_free_filter(struct perf_event
*event
)
5186 ftrace_profile_free_filter(event
);
5191 static inline void perf_tp_register(void)
5195 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
5200 static void perf_event_free_filter(struct perf_event
*event
)
5204 #endif /* CONFIG_EVENT_TRACING */
5206 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5207 void perf_bp_event(struct perf_event
*bp
, void *data
)
5209 struct perf_sample_data sample
;
5210 struct pt_regs
*regs
= data
;
5212 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
);
5214 if (!bp
->hw
.state
&& !perf_exclude_event(bp
, regs
))
5215 perf_swevent_event(bp
, 1, &sample
, regs
);
5220 * hrtimer based swevent callback
5223 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
5225 enum hrtimer_restart ret
= HRTIMER_RESTART
;
5226 struct perf_sample_data data
;
5227 struct pt_regs
*regs
;
5228 struct perf_event
*event
;
5231 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
5233 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
5234 return HRTIMER_NORESTART
;
5236 event
->pmu
->read(event
);
5238 perf_sample_data_init(&data
, 0);
5239 data
.period
= event
->hw
.last_period
;
5240 regs
= get_irq_regs();
5242 if (regs
&& !perf_exclude_event(event
, regs
)) {
5243 if (!(event
->attr
.exclude_idle
&& current
->pid
== 0))
5244 if (perf_event_overflow(event
, &data
, regs
))
5245 ret
= HRTIMER_NORESTART
;
5248 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
5249 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
5254 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
5256 struct hw_perf_event
*hwc
= &event
->hw
;
5259 if (!is_sampling_event(event
))
5262 period
= local64_read(&hwc
->period_left
);
5267 local64_set(&hwc
->period_left
, 0);
5269 period
= max_t(u64
, 10000, hwc
->sample_period
);
5271 __hrtimer_start_range_ns(&hwc
->hrtimer
,
5272 ns_to_ktime(period
), 0,
5273 HRTIMER_MODE_REL_PINNED
, 0);
5276 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
5278 struct hw_perf_event
*hwc
= &event
->hw
;
5280 if (is_sampling_event(event
)) {
5281 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
5282 local64_set(&hwc
->period_left
, ktime_to_ns(remaining
));
5284 hrtimer_cancel(&hwc
->hrtimer
);
5288 static void perf_swevent_init_hrtimer(struct perf_event
*event
)
5290 struct hw_perf_event
*hwc
= &event
->hw
;
5292 if (!is_sampling_event(event
))
5295 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
5296 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
5299 * Since hrtimers have a fixed rate, we can do a static freq->period
5300 * mapping and avoid the whole period adjust feedback stuff.
5302 if (event
->attr
.freq
) {
5303 long freq
= event
->attr
.sample_freq
;
5305 event
->attr
.sample_period
= NSEC_PER_SEC
/ freq
;
5306 hwc
->sample_period
= event
->attr
.sample_period
;
5307 local64_set(&hwc
->period_left
, hwc
->sample_period
);
5308 event
->attr
.freq
= 0;
5313 * Software event: cpu wall time clock
5316 static void cpu_clock_event_update(struct perf_event
*event
)
5321 now
= local_clock();
5322 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
5323 local64_add(now
- prev
, &event
->count
);
5326 static void cpu_clock_event_start(struct perf_event
*event
, int flags
)
5328 local64_set(&event
->hw
.prev_count
, local_clock());
5329 perf_swevent_start_hrtimer(event
);
5332 static void cpu_clock_event_stop(struct perf_event
*event
, int flags
)
5334 perf_swevent_cancel_hrtimer(event
);
5335 cpu_clock_event_update(event
);
5338 static int cpu_clock_event_add(struct perf_event
*event
, int flags
)
5340 if (flags
& PERF_EF_START
)
5341 cpu_clock_event_start(event
, flags
);
5346 static void cpu_clock_event_del(struct perf_event
*event
, int flags
)
5348 cpu_clock_event_stop(event
, flags
);
5351 static void cpu_clock_event_read(struct perf_event
*event
)
5353 cpu_clock_event_update(event
);
5356 static int cpu_clock_event_init(struct perf_event
*event
)
5358 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
5361 if (event
->attr
.config
!= PERF_COUNT_SW_CPU_CLOCK
)
5364 perf_swevent_init_hrtimer(event
);
5369 static struct pmu perf_cpu_clock
= {
5370 .task_ctx_nr
= perf_sw_context
,
5372 .event_init
= cpu_clock_event_init
,
5373 .add
= cpu_clock_event_add
,
5374 .del
= cpu_clock_event_del
,
5375 .start
= cpu_clock_event_start
,
5376 .stop
= cpu_clock_event_stop
,
5377 .read
= cpu_clock_event_read
,
5381 * Software event: task time clock
5384 static void task_clock_event_update(struct perf_event
*event
, u64 now
)
5389 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
5391 local64_add(delta
, &event
->count
);
5394 static void task_clock_event_start(struct perf_event
*event
, int flags
)
5396 local64_set(&event
->hw
.prev_count
, event
->ctx
->time
);
5397 perf_swevent_start_hrtimer(event
);
5400 static void task_clock_event_stop(struct perf_event
*event
, int flags
)
5402 perf_swevent_cancel_hrtimer(event
);
5403 task_clock_event_update(event
, event
->ctx
->time
);
5406 static int task_clock_event_add(struct perf_event
*event
, int flags
)
5408 if (flags
& PERF_EF_START
)
5409 task_clock_event_start(event
, flags
);
5414 static void task_clock_event_del(struct perf_event
*event
, int flags
)
5416 task_clock_event_stop(event
, PERF_EF_UPDATE
);
5419 static void task_clock_event_read(struct perf_event
*event
)
5421 u64 now
= perf_clock();
5422 u64 delta
= now
- event
->ctx
->timestamp
;
5423 u64 time
= event
->ctx
->time
+ delta
;
5425 task_clock_event_update(event
, time
);
5428 static int task_clock_event_init(struct perf_event
*event
)
5430 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
5433 if (event
->attr
.config
!= PERF_COUNT_SW_TASK_CLOCK
)
5436 perf_swevent_init_hrtimer(event
);
5441 static struct pmu perf_task_clock
= {
5442 .task_ctx_nr
= perf_sw_context
,
5444 .event_init
= task_clock_event_init
,
5445 .add
= task_clock_event_add
,
5446 .del
= task_clock_event_del
,
5447 .start
= task_clock_event_start
,
5448 .stop
= task_clock_event_stop
,
5449 .read
= task_clock_event_read
,
5452 static void perf_pmu_nop_void(struct pmu
*pmu
)
5456 static int perf_pmu_nop_int(struct pmu
*pmu
)
5461 static void perf_pmu_start_txn(struct pmu
*pmu
)
5463 perf_pmu_disable(pmu
);
5466 static int perf_pmu_commit_txn(struct pmu
*pmu
)
5468 perf_pmu_enable(pmu
);
5472 static void perf_pmu_cancel_txn(struct pmu
*pmu
)
5474 perf_pmu_enable(pmu
);
5478 * Ensures all contexts with the same task_ctx_nr have the same
5479 * pmu_cpu_context too.
5481 static void *find_pmu_context(int ctxn
)
5488 list_for_each_entry(pmu
, &pmus
, entry
) {
5489 if (pmu
->task_ctx_nr
== ctxn
)
5490 return pmu
->pmu_cpu_context
;
5496 static void update_pmu_context(struct pmu
*pmu
, struct pmu
*old_pmu
)
5500 for_each_possible_cpu(cpu
) {
5501 struct perf_cpu_context
*cpuctx
;
5503 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
5505 if (cpuctx
->active_pmu
== old_pmu
)
5506 cpuctx
->active_pmu
= pmu
;
5510 static void free_pmu_context(struct pmu
*pmu
)
5514 mutex_lock(&pmus_lock
);
5516 * Like a real lame refcount.
5518 list_for_each_entry(i
, &pmus
, entry
) {
5519 if (i
->pmu_cpu_context
== pmu
->pmu_cpu_context
) {
5520 update_pmu_context(i
, pmu
);
5525 free_percpu(pmu
->pmu_cpu_context
);
5527 mutex_unlock(&pmus_lock
);
5529 static struct idr pmu_idr
;
5532 type_show(struct device
*dev
, struct device_attribute
*attr
, char *page
)
5534 struct pmu
*pmu
= dev_get_drvdata(dev
);
5536 return snprintf(page
, PAGE_SIZE
-1, "%d\n", pmu
->type
);
5539 static struct device_attribute pmu_dev_attrs
[] = {
5544 static int pmu_bus_running
;
5545 static struct bus_type pmu_bus
= {
5546 .name
= "event_source",
5547 .dev_attrs
= pmu_dev_attrs
,
5550 static void pmu_dev_release(struct device
*dev
)
5555 static int pmu_dev_alloc(struct pmu
*pmu
)
5559 pmu
->dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
5563 device_initialize(pmu
->dev
);
5564 ret
= dev_set_name(pmu
->dev
, "%s", pmu
->name
);
5568 dev_set_drvdata(pmu
->dev
, pmu
);
5569 pmu
->dev
->bus
= &pmu_bus
;
5570 pmu
->dev
->release
= pmu_dev_release
;
5571 ret
= device_add(pmu
->dev
);
5579 put_device(pmu
->dev
);
5583 static struct lock_class_key cpuctx_mutex
;
5584 static struct lock_class_key cpuctx_lock
;
5586 int perf_pmu_register(struct pmu
*pmu
, char *name
, int type
)
5590 mutex_lock(&pmus_lock
);
5592 pmu
->pmu_disable_count
= alloc_percpu(int);
5593 if (!pmu
->pmu_disable_count
)
5602 int err
= idr_pre_get(&pmu_idr
, GFP_KERNEL
);
5606 err
= idr_get_new_above(&pmu_idr
, pmu
, PERF_TYPE_MAX
, &type
);
5614 if (pmu_bus_running
) {
5615 ret
= pmu_dev_alloc(pmu
);
5621 pmu
->pmu_cpu_context
= find_pmu_context(pmu
->task_ctx_nr
);
5622 if (pmu
->pmu_cpu_context
)
5623 goto got_cpu_context
;
5625 pmu
->pmu_cpu_context
= alloc_percpu(struct perf_cpu_context
);
5626 if (!pmu
->pmu_cpu_context
)
5629 for_each_possible_cpu(cpu
) {
5630 struct perf_cpu_context
*cpuctx
;
5632 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
5633 __perf_event_init_context(&cpuctx
->ctx
);
5634 lockdep_set_class(&cpuctx
->ctx
.mutex
, &cpuctx_mutex
);
5635 lockdep_set_class(&cpuctx
->ctx
.lock
, &cpuctx_lock
);
5636 cpuctx
->ctx
.type
= cpu_context
;
5637 cpuctx
->ctx
.pmu
= pmu
;
5638 cpuctx
->jiffies_interval
= 1;
5639 INIT_LIST_HEAD(&cpuctx
->rotation_list
);
5640 cpuctx
->active_pmu
= pmu
;
5644 if (!pmu
->start_txn
) {
5645 if (pmu
->pmu_enable
) {
5647 * If we have pmu_enable/pmu_disable calls, install
5648 * transaction stubs that use that to try and batch
5649 * hardware accesses.
5651 pmu
->start_txn
= perf_pmu_start_txn
;
5652 pmu
->commit_txn
= perf_pmu_commit_txn
;
5653 pmu
->cancel_txn
= perf_pmu_cancel_txn
;
5655 pmu
->start_txn
= perf_pmu_nop_void
;
5656 pmu
->commit_txn
= perf_pmu_nop_int
;
5657 pmu
->cancel_txn
= perf_pmu_nop_void
;
5661 if (!pmu
->pmu_enable
) {
5662 pmu
->pmu_enable
= perf_pmu_nop_void
;
5663 pmu
->pmu_disable
= perf_pmu_nop_void
;
5666 list_add_rcu(&pmu
->entry
, &pmus
);
5669 mutex_unlock(&pmus_lock
);
5674 device_del(pmu
->dev
);
5675 put_device(pmu
->dev
);
5678 if (pmu
->type
>= PERF_TYPE_MAX
)
5679 idr_remove(&pmu_idr
, pmu
->type
);
5682 free_percpu(pmu
->pmu_disable_count
);
5686 void perf_pmu_unregister(struct pmu
*pmu
)
5688 mutex_lock(&pmus_lock
);
5689 list_del_rcu(&pmu
->entry
);
5690 mutex_unlock(&pmus_lock
);
5693 * We dereference the pmu list under both SRCU and regular RCU, so
5694 * synchronize against both of those.
5696 synchronize_srcu(&pmus_srcu
);
5699 free_percpu(pmu
->pmu_disable_count
);
5700 if (pmu
->type
>= PERF_TYPE_MAX
)
5701 idr_remove(&pmu_idr
, pmu
->type
);
5702 device_del(pmu
->dev
);
5703 put_device(pmu
->dev
);
5704 free_pmu_context(pmu
);
5707 struct pmu
*perf_init_event(struct perf_event
*event
)
5709 struct pmu
*pmu
= NULL
;
5713 idx
= srcu_read_lock(&pmus_srcu
);
5716 pmu
= idr_find(&pmu_idr
, event
->attr
.type
);
5719 ret
= pmu
->event_init(event
);
5725 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
5726 ret
= pmu
->event_init(event
);
5730 if (ret
!= -ENOENT
) {
5735 pmu
= ERR_PTR(-ENOENT
);
5737 srcu_read_unlock(&pmus_srcu
, idx
);
5743 * Allocate and initialize a event structure
5745 static struct perf_event
*
5746 perf_event_alloc(struct perf_event_attr
*attr
, int cpu
,
5747 struct task_struct
*task
,
5748 struct perf_event
*group_leader
,
5749 struct perf_event
*parent_event
,
5750 perf_overflow_handler_t overflow_handler
,
5754 struct perf_event
*event
;
5755 struct hw_perf_event
*hwc
;
5758 if ((unsigned)cpu
>= nr_cpu_ids
) {
5759 if (!task
|| cpu
!= -1)
5760 return ERR_PTR(-EINVAL
);
5763 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
5765 return ERR_PTR(-ENOMEM
);
5768 * Single events are their own group leaders, with an
5769 * empty sibling list:
5772 group_leader
= event
;
5774 mutex_init(&event
->child_mutex
);
5775 INIT_LIST_HEAD(&event
->child_list
);
5777 INIT_LIST_HEAD(&event
->group_entry
);
5778 INIT_LIST_HEAD(&event
->event_entry
);
5779 INIT_LIST_HEAD(&event
->sibling_list
);
5780 init_waitqueue_head(&event
->waitq
);
5781 init_irq_work(&event
->pending
, perf_pending_event
);
5783 mutex_init(&event
->mmap_mutex
);
5786 event
->attr
= *attr
;
5787 event
->group_leader
= group_leader
;
5791 event
->parent
= parent_event
;
5793 event
->ns
= get_pid_ns(current
->nsproxy
->pid_ns
);
5794 event
->id
= atomic64_inc_return(&perf_event_id
);
5796 event
->state
= PERF_EVENT_STATE_INACTIVE
;
5799 event
->attach_state
= PERF_ATTACH_TASK
;
5800 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5802 * hw_breakpoint is a bit difficult here..
5804 if (attr
->type
== PERF_TYPE_BREAKPOINT
)
5805 event
->hw
.bp_target
= task
;
5809 if (!overflow_handler
&& parent_event
) {
5810 overflow_handler
= parent_event
->overflow_handler
;
5811 context
= parent_event
->overflow_handler_context
;
5814 event
->overflow_handler
= overflow_handler
;
5815 event
->overflow_handler_context
= context
;
5818 event
->state
= PERF_EVENT_STATE_OFF
;
5823 hwc
->sample_period
= attr
->sample_period
;
5824 if (attr
->freq
&& attr
->sample_freq
)
5825 hwc
->sample_period
= 1;
5826 hwc
->last_period
= hwc
->sample_period
;
5828 local64_set(&hwc
->period_left
, hwc
->sample_period
);
5831 * we currently do not support PERF_FORMAT_GROUP on inherited events
5833 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
5836 pmu
= perf_init_event(event
);
5842 else if (IS_ERR(pmu
))
5847 put_pid_ns(event
->ns
);
5849 return ERR_PTR(err
);
5854 if (!event
->parent
) {
5855 if (event
->attach_state
& PERF_ATTACH_TASK
)
5856 jump_label_inc(&perf_sched_events
);
5857 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
5858 atomic_inc(&nr_mmap_events
);
5859 if (event
->attr
.comm
)
5860 atomic_inc(&nr_comm_events
);
5861 if (event
->attr
.task
)
5862 atomic_inc(&nr_task_events
);
5863 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
5864 err
= get_callchain_buffers();
5867 return ERR_PTR(err
);
5875 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
5876 struct perf_event_attr
*attr
)
5881 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
5885 * zero the full structure, so that a short copy will be nice.
5887 memset(attr
, 0, sizeof(*attr
));
5889 ret
= get_user(size
, &uattr
->size
);
5893 if (size
> PAGE_SIZE
) /* silly large */
5896 if (!size
) /* abi compat */
5897 size
= PERF_ATTR_SIZE_VER0
;
5899 if (size
< PERF_ATTR_SIZE_VER0
)
5903 * If we're handed a bigger struct than we know of,
5904 * ensure all the unknown bits are 0 - i.e. new
5905 * user-space does not rely on any kernel feature
5906 * extensions we dont know about yet.
5908 if (size
> sizeof(*attr
)) {
5909 unsigned char __user
*addr
;
5910 unsigned char __user
*end
;
5913 addr
= (void __user
*)uattr
+ sizeof(*attr
);
5914 end
= (void __user
*)uattr
+ size
;
5916 for (; addr
< end
; addr
++) {
5917 ret
= get_user(val
, addr
);
5923 size
= sizeof(*attr
);
5926 ret
= copy_from_user(attr
, uattr
, size
);
5930 if (attr
->__reserved_1
)
5933 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
5936 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
5943 put_user(sizeof(*attr
), &uattr
->size
);
5949 perf_event_set_output(struct perf_event
*event
, struct perf_event
*output_event
)
5951 struct ring_buffer
*rb
= NULL
, *old_rb
= NULL
;
5957 /* don't allow circular references */
5958 if (event
== output_event
)
5962 * Don't allow cross-cpu buffers
5964 if (output_event
->cpu
!= event
->cpu
)
5968 * If its not a per-cpu rb, it must be the same task.
5970 if (output_event
->cpu
== -1 && output_event
->ctx
!= event
->ctx
)
5974 mutex_lock(&event
->mmap_mutex
);
5975 /* Can't redirect output if we've got an active mmap() */
5976 if (atomic_read(&event
->mmap_count
))
5980 /* get the rb we want to redirect to */
5981 rb
= ring_buffer_get(output_event
);
5987 rcu_assign_pointer(event
->rb
, rb
);
5990 mutex_unlock(&event
->mmap_mutex
);
5993 ring_buffer_put(old_rb
);
5999 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6001 * @attr_uptr: event_id type attributes for monitoring/sampling
6004 * @group_fd: group leader event fd
6006 SYSCALL_DEFINE5(perf_event_open
,
6007 struct perf_event_attr __user
*, attr_uptr
,
6008 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
6010 struct perf_event
*group_leader
= NULL
, *output_event
= NULL
;
6011 struct perf_event
*event
, *sibling
;
6012 struct perf_event_attr attr
;
6013 struct perf_event_context
*ctx
;
6014 struct file
*event_file
= NULL
;
6015 struct file
*group_file
= NULL
;
6016 struct task_struct
*task
= NULL
;
6020 int fput_needed
= 0;
6023 /* for future expandability... */
6024 if (flags
& ~PERF_FLAG_ALL
)
6027 err
= perf_copy_attr(attr_uptr
, &attr
);
6031 if (!attr
.exclude_kernel
) {
6032 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
6037 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
6042 * In cgroup mode, the pid argument is used to pass the fd
6043 * opened to the cgroup directory in cgroupfs. The cpu argument
6044 * designates the cpu on which to monitor threads from that
6047 if ((flags
& PERF_FLAG_PID_CGROUP
) && (pid
== -1 || cpu
== -1))
6050 event_fd
= get_unused_fd_flags(O_RDWR
);
6054 if (group_fd
!= -1) {
6055 group_leader
= perf_fget_light(group_fd
, &fput_needed
);
6056 if (IS_ERR(group_leader
)) {
6057 err
= PTR_ERR(group_leader
);
6060 group_file
= group_leader
->filp
;
6061 if (flags
& PERF_FLAG_FD_OUTPUT
)
6062 output_event
= group_leader
;
6063 if (flags
& PERF_FLAG_FD_NO_GROUP
)
6064 group_leader
= NULL
;
6067 if (pid
!= -1 && !(flags
& PERF_FLAG_PID_CGROUP
)) {
6068 task
= find_lively_task_by_vpid(pid
);
6070 err
= PTR_ERR(task
);
6075 event
= perf_event_alloc(&attr
, cpu
, task
, group_leader
, NULL
,
6077 if (IS_ERR(event
)) {
6078 err
= PTR_ERR(event
);
6082 if (flags
& PERF_FLAG_PID_CGROUP
) {
6083 err
= perf_cgroup_connect(pid
, event
, &attr
, group_leader
);
6088 * - that has cgroup constraint on event->cpu
6089 * - that may need work on context switch
6091 atomic_inc(&per_cpu(perf_cgroup_events
, event
->cpu
));
6092 jump_label_inc(&perf_sched_events
);
6096 * Special case software events and allow them to be part of
6097 * any hardware group.
6102 (is_software_event(event
) != is_software_event(group_leader
))) {
6103 if (is_software_event(event
)) {
6105 * If event and group_leader are not both a software
6106 * event, and event is, then group leader is not.
6108 * Allow the addition of software events to !software
6109 * groups, this is safe because software events never
6112 pmu
= group_leader
->pmu
;
6113 } else if (is_software_event(group_leader
) &&
6114 (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
)) {
6116 * In case the group is a pure software group, and we
6117 * try to add a hardware event, move the whole group to
6118 * the hardware context.
6125 * Get the target context (task or percpu):
6127 ctx
= find_get_context(pmu
, task
, cpu
);
6134 put_task_struct(task
);
6139 * Look up the group leader (we will attach this event to it):
6145 * Do not allow a recursive hierarchy (this new sibling
6146 * becoming part of another group-sibling):
6148 if (group_leader
->group_leader
!= group_leader
)
6151 * Do not allow to attach to a group in a different
6152 * task or CPU context:
6155 if (group_leader
->ctx
->type
!= ctx
->type
)
6158 if (group_leader
->ctx
!= ctx
)
6163 * Only a group leader can be exclusive or pinned
6165 if (attr
.exclusive
|| attr
.pinned
)
6170 err
= perf_event_set_output(event
, output_event
);
6175 event_file
= anon_inode_getfile("[perf_event]", &perf_fops
, event
, O_RDWR
);
6176 if (IS_ERR(event_file
)) {
6177 err
= PTR_ERR(event_file
);
6182 struct perf_event_context
*gctx
= group_leader
->ctx
;
6184 mutex_lock(&gctx
->mutex
);
6185 perf_remove_from_context(group_leader
);
6186 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
6188 perf_remove_from_context(sibling
);
6191 mutex_unlock(&gctx
->mutex
);
6195 event
->filp
= event_file
;
6196 WARN_ON_ONCE(ctx
->parent_ctx
);
6197 mutex_lock(&ctx
->mutex
);
6200 perf_install_in_context(ctx
, group_leader
, cpu
);
6202 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
6204 perf_install_in_context(ctx
, sibling
, cpu
);
6209 perf_install_in_context(ctx
, event
, cpu
);
6211 perf_unpin_context(ctx
);
6212 mutex_unlock(&ctx
->mutex
);
6214 event
->owner
= current
;
6216 mutex_lock(¤t
->perf_event_mutex
);
6217 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
6218 mutex_unlock(¤t
->perf_event_mutex
);
6221 * Precalculate sample_data sizes
6223 perf_event__header_size(event
);
6224 perf_event__id_header_size(event
);
6227 * Drop the reference on the group_event after placing the
6228 * new event on the sibling_list. This ensures destruction
6229 * of the group leader will find the pointer to itself in
6230 * perf_group_detach().
6232 fput_light(group_file
, fput_needed
);
6233 fd_install(event_fd
, event_file
);
6237 perf_unpin_context(ctx
);
6243 put_task_struct(task
);
6245 fput_light(group_file
, fput_needed
);
6247 put_unused_fd(event_fd
);
6252 * perf_event_create_kernel_counter
6254 * @attr: attributes of the counter to create
6255 * @cpu: cpu in which the counter is bound
6256 * @task: task to profile (NULL for percpu)
6259 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
6260 struct task_struct
*task
,
6261 perf_overflow_handler_t overflow_handler
,
6264 struct perf_event_context
*ctx
;
6265 struct perf_event
*event
;
6269 * Get the target context (task or percpu):
6272 event
= perf_event_alloc(attr
, cpu
, task
, NULL
, NULL
,
6273 overflow_handler
, context
);
6274 if (IS_ERR(event
)) {
6275 err
= PTR_ERR(event
);
6279 ctx
= find_get_context(event
->pmu
, task
, cpu
);
6286 WARN_ON_ONCE(ctx
->parent_ctx
);
6287 mutex_lock(&ctx
->mutex
);
6288 perf_install_in_context(ctx
, event
, cpu
);
6290 perf_unpin_context(ctx
);
6291 mutex_unlock(&ctx
->mutex
);
6298 return ERR_PTR(err
);
6300 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
6302 static void sync_child_event(struct perf_event
*child_event
,
6303 struct task_struct
*child
)
6305 struct perf_event
*parent_event
= child_event
->parent
;
6308 if (child_event
->attr
.inherit_stat
)
6309 perf_event_read_event(child_event
, child
);
6311 child_val
= perf_event_count(child_event
);
6314 * Add back the child's count to the parent's count:
6316 atomic64_add(child_val
, &parent_event
->child_count
);
6317 atomic64_add(child_event
->total_time_enabled
,
6318 &parent_event
->child_total_time_enabled
);
6319 atomic64_add(child_event
->total_time_running
,
6320 &parent_event
->child_total_time_running
);
6323 * Remove this event from the parent's list
6325 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
6326 mutex_lock(&parent_event
->child_mutex
);
6327 list_del_init(&child_event
->child_list
);
6328 mutex_unlock(&parent_event
->child_mutex
);
6331 * Release the parent event, if this was the last
6334 fput(parent_event
->filp
);
6338 __perf_event_exit_task(struct perf_event
*child_event
,
6339 struct perf_event_context
*child_ctx
,
6340 struct task_struct
*child
)
6342 if (child_event
->parent
) {
6343 raw_spin_lock_irq(&child_ctx
->lock
);
6344 perf_group_detach(child_event
);
6345 raw_spin_unlock_irq(&child_ctx
->lock
);
6348 perf_remove_from_context(child_event
);
6351 * It can happen that the parent exits first, and has events
6352 * that are still around due to the child reference. These
6353 * events need to be zapped.
6355 if (child_event
->parent
) {
6356 sync_child_event(child_event
, child
);
6357 free_event(child_event
);
6361 static void perf_event_exit_task_context(struct task_struct
*child
, int ctxn
)
6363 struct perf_event
*child_event
, *tmp
;
6364 struct perf_event_context
*child_ctx
;
6365 unsigned long flags
;
6367 if (likely(!child
->perf_event_ctxp
[ctxn
])) {
6368 perf_event_task(child
, NULL
, 0);
6372 local_irq_save(flags
);
6374 * We can't reschedule here because interrupts are disabled,
6375 * and either child is current or it is a task that can't be
6376 * scheduled, so we are now safe from rescheduling changing
6379 child_ctx
= rcu_dereference_raw(child
->perf_event_ctxp
[ctxn
]);
6382 * Take the context lock here so that if find_get_context is
6383 * reading child->perf_event_ctxp, we wait until it has
6384 * incremented the context's refcount before we do put_ctx below.
6386 raw_spin_lock(&child_ctx
->lock
);
6387 task_ctx_sched_out(child_ctx
);
6388 child
->perf_event_ctxp
[ctxn
] = NULL
;
6390 * If this context is a clone; unclone it so it can't get
6391 * swapped to another process while we're removing all
6392 * the events from it.
6394 unclone_ctx(child_ctx
);
6395 update_context_time(child_ctx
);
6396 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
6399 * Report the task dead after unscheduling the events so that we
6400 * won't get any samples after PERF_RECORD_EXIT. We can however still
6401 * get a few PERF_RECORD_READ events.
6403 perf_event_task(child
, child_ctx
, 0);
6406 * We can recurse on the same lock type through:
6408 * __perf_event_exit_task()
6409 * sync_child_event()
6410 * fput(parent_event->filp)
6412 * mutex_lock(&ctx->mutex)
6414 * But since its the parent context it won't be the same instance.
6416 mutex_lock(&child_ctx
->mutex
);
6419 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->pinned_groups
,
6421 __perf_event_exit_task(child_event
, child_ctx
, child
);
6423 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->flexible_groups
,
6425 __perf_event_exit_task(child_event
, child_ctx
, child
);
6428 * If the last event was a group event, it will have appended all
6429 * its siblings to the list, but we obtained 'tmp' before that which
6430 * will still point to the list head terminating the iteration.
6432 if (!list_empty(&child_ctx
->pinned_groups
) ||
6433 !list_empty(&child_ctx
->flexible_groups
))
6436 mutex_unlock(&child_ctx
->mutex
);
6442 * When a child task exits, feed back event values to parent events.
6444 void perf_event_exit_task(struct task_struct
*child
)
6446 struct perf_event
*event
, *tmp
;
6449 mutex_lock(&child
->perf_event_mutex
);
6450 list_for_each_entry_safe(event
, tmp
, &child
->perf_event_list
,
6452 list_del_init(&event
->owner_entry
);
6455 * Ensure the list deletion is visible before we clear
6456 * the owner, closes a race against perf_release() where
6457 * we need to serialize on the owner->perf_event_mutex.
6460 event
->owner
= NULL
;
6462 mutex_unlock(&child
->perf_event_mutex
);
6464 for_each_task_context_nr(ctxn
)
6465 perf_event_exit_task_context(child
, ctxn
);
6468 static void perf_free_event(struct perf_event
*event
,
6469 struct perf_event_context
*ctx
)
6471 struct perf_event
*parent
= event
->parent
;
6473 if (WARN_ON_ONCE(!parent
))
6476 mutex_lock(&parent
->child_mutex
);
6477 list_del_init(&event
->child_list
);
6478 mutex_unlock(&parent
->child_mutex
);
6482 perf_group_detach(event
);
6483 list_del_event(event
, ctx
);
6488 * free an unexposed, unused context as created by inheritance by
6489 * perf_event_init_task below, used by fork() in case of fail.
6491 void perf_event_free_task(struct task_struct
*task
)
6493 struct perf_event_context
*ctx
;
6494 struct perf_event
*event
, *tmp
;
6497 for_each_task_context_nr(ctxn
) {
6498 ctx
= task
->perf_event_ctxp
[ctxn
];
6502 mutex_lock(&ctx
->mutex
);
6504 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
,
6506 perf_free_event(event
, ctx
);
6508 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
,
6510 perf_free_event(event
, ctx
);
6512 if (!list_empty(&ctx
->pinned_groups
) ||
6513 !list_empty(&ctx
->flexible_groups
))
6516 mutex_unlock(&ctx
->mutex
);
6522 void perf_event_delayed_put(struct task_struct
*task
)
6526 for_each_task_context_nr(ctxn
)
6527 WARN_ON_ONCE(task
->perf_event_ctxp
[ctxn
]);
6531 * inherit a event from parent task to child task:
6533 static struct perf_event
*
6534 inherit_event(struct perf_event
*parent_event
,
6535 struct task_struct
*parent
,
6536 struct perf_event_context
*parent_ctx
,
6537 struct task_struct
*child
,
6538 struct perf_event
*group_leader
,
6539 struct perf_event_context
*child_ctx
)
6541 struct perf_event
*child_event
;
6542 unsigned long flags
;
6545 * Instead of creating recursive hierarchies of events,
6546 * we link inherited events back to the original parent,
6547 * which has a filp for sure, which we use as the reference
6550 if (parent_event
->parent
)
6551 parent_event
= parent_event
->parent
;
6553 child_event
= perf_event_alloc(&parent_event
->attr
,
6556 group_leader
, parent_event
,
6558 if (IS_ERR(child_event
))
6563 * Make the child state follow the state of the parent event,
6564 * not its attr.disabled bit. We hold the parent's mutex,
6565 * so we won't race with perf_event_{en, dis}able_family.
6567 if (parent_event
->state
>= PERF_EVENT_STATE_INACTIVE
)
6568 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
6570 child_event
->state
= PERF_EVENT_STATE_OFF
;
6572 if (parent_event
->attr
.freq
) {
6573 u64 sample_period
= parent_event
->hw
.sample_period
;
6574 struct hw_perf_event
*hwc
= &child_event
->hw
;
6576 hwc
->sample_period
= sample_period
;
6577 hwc
->last_period
= sample_period
;
6579 local64_set(&hwc
->period_left
, sample_period
);
6582 child_event
->ctx
= child_ctx
;
6583 child_event
->overflow_handler
= parent_event
->overflow_handler
;
6584 child_event
->overflow_handler_context
6585 = parent_event
->overflow_handler_context
;
6588 * Precalculate sample_data sizes
6590 perf_event__header_size(child_event
);
6591 perf_event__id_header_size(child_event
);
6594 * Link it up in the child's context:
6596 raw_spin_lock_irqsave(&child_ctx
->lock
, flags
);
6597 add_event_to_ctx(child_event
, child_ctx
);
6598 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
6601 * Get a reference to the parent filp - we will fput it
6602 * when the child event exits. This is safe to do because
6603 * we are in the parent and we know that the filp still
6604 * exists and has a nonzero count:
6606 atomic_long_inc(&parent_event
->filp
->f_count
);
6609 * Link this into the parent event's child list
6611 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
6612 mutex_lock(&parent_event
->child_mutex
);
6613 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
6614 mutex_unlock(&parent_event
->child_mutex
);
6619 static int inherit_group(struct perf_event
*parent_event
,
6620 struct task_struct
*parent
,
6621 struct perf_event_context
*parent_ctx
,
6622 struct task_struct
*child
,
6623 struct perf_event_context
*child_ctx
)
6625 struct perf_event
*leader
;
6626 struct perf_event
*sub
;
6627 struct perf_event
*child_ctr
;
6629 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
6630 child
, NULL
, child_ctx
);
6632 return PTR_ERR(leader
);
6633 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
6634 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
6635 child
, leader
, child_ctx
);
6636 if (IS_ERR(child_ctr
))
6637 return PTR_ERR(child_ctr
);
6643 inherit_task_group(struct perf_event
*event
, struct task_struct
*parent
,
6644 struct perf_event_context
*parent_ctx
,
6645 struct task_struct
*child
, int ctxn
,
6649 struct perf_event_context
*child_ctx
;
6651 if (!event
->attr
.inherit
) {
6656 child_ctx
= child
->perf_event_ctxp
[ctxn
];
6659 * This is executed from the parent task context, so
6660 * inherit events that have been marked for cloning.
6661 * First allocate and initialize a context for the
6665 child_ctx
= alloc_perf_context(event
->pmu
, child
);
6669 child
->perf_event_ctxp
[ctxn
] = child_ctx
;
6672 ret
= inherit_group(event
, parent
, parent_ctx
,
6682 * Initialize the perf_event context in task_struct
6684 int perf_event_init_context(struct task_struct
*child
, int ctxn
)
6686 struct perf_event_context
*child_ctx
, *parent_ctx
;
6687 struct perf_event_context
*cloned_ctx
;
6688 struct perf_event
*event
;
6689 struct task_struct
*parent
= current
;
6690 int inherited_all
= 1;
6691 unsigned long flags
;
6694 if (likely(!parent
->perf_event_ctxp
[ctxn
]))
6698 * If the parent's context is a clone, pin it so it won't get
6701 parent_ctx
= perf_pin_task_context(parent
, ctxn
);
6704 * No need to check if parent_ctx != NULL here; since we saw
6705 * it non-NULL earlier, the only reason for it to become NULL
6706 * is if we exit, and since we're currently in the middle of
6707 * a fork we can't be exiting at the same time.
6711 * Lock the parent list. No need to lock the child - not PID
6712 * hashed yet and not running, so nobody can access it.
6714 mutex_lock(&parent_ctx
->mutex
);
6717 * We dont have to disable NMIs - we are only looking at
6718 * the list, not manipulating it:
6720 list_for_each_entry(event
, &parent_ctx
->pinned_groups
, group_entry
) {
6721 ret
= inherit_task_group(event
, parent
, parent_ctx
,
6722 child
, ctxn
, &inherited_all
);
6728 * We can't hold ctx->lock when iterating the ->flexible_group list due
6729 * to allocations, but we need to prevent rotation because
6730 * rotate_ctx() will change the list from interrupt context.
6732 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
6733 parent_ctx
->rotate_disable
= 1;
6734 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
6736 list_for_each_entry(event
, &parent_ctx
->flexible_groups
, group_entry
) {
6737 ret
= inherit_task_group(event
, parent
, parent_ctx
,
6738 child
, ctxn
, &inherited_all
);
6743 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
6744 parent_ctx
->rotate_disable
= 0;
6746 child_ctx
= child
->perf_event_ctxp
[ctxn
];
6748 if (child_ctx
&& inherited_all
) {
6750 * Mark the child context as a clone of the parent
6751 * context, or of whatever the parent is a clone of.
6753 * Note that if the parent is a clone, the holding of
6754 * parent_ctx->lock avoids it from being uncloned.
6756 cloned_ctx
= parent_ctx
->parent_ctx
;
6758 child_ctx
->parent_ctx
= cloned_ctx
;
6759 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
6761 child_ctx
->parent_ctx
= parent_ctx
;
6762 child_ctx
->parent_gen
= parent_ctx
->generation
;
6764 get_ctx(child_ctx
->parent_ctx
);
6767 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
6768 mutex_unlock(&parent_ctx
->mutex
);
6770 perf_unpin_context(parent_ctx
);
6771 put_ctx(parent_ctx
);
6777 * Initialize the perf_event context in task_struct
6779 int perf_event_init_task(struct task_struct
*child
)
6783 memset(child
->perf_event_ctxp
, 0, sizeof(child
->perf_event_ctxp
));
6784 mutex_init(&child
->perf_event_mutex
);
6785 INIT_LIST_HEAD(&child
->perf_event_list
);
6787 for_each_task_context_nr(ctxn
) {
6788 ret
= perf_event_init_context(child
, ctxn
);
6796 static void __init
perf_event_init_all_cpus(void)
6798 struct swevent_htable
*swhash
;
6801 for_each_possible_cpu(cpu
) {
6802 swhash
= &per_cpu(swevent_htable
, cpu
);
6803 mutex_init(&swhash
->hlist_mutex
);
6804 INIT_LIST_HEAD(&per_cpu(rotation_list
, cpu
));
6808 static void __cpuinit
perf_event_init_cpu(int cpu
)
6810 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
6812 mutex_lock(&swhash
->hlist_mutex
);
6813 if (swhash
->hlist_refcount
> 0) {
6814 struct swevent_hlist
*hlist
;
6816 hlist
= kzalloc_node(sizeof(*hlist
), GFP_KERNEL
, cpu_to_node(cpu
));
6818 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
6820 mutex_unlock(&swhash
->hlist_mutex
);
6823 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
6824 static void perf_pmu_rotate_stop(struct pmu
*pmu
)
6826 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
6828 WARN_ON(!irqs_disabled());
6830 list_del_init(&cpuctx
->rotation_list
);
6833 static void __perf_event_exit_context(void *__info
)
6835 struct perf_event_context
*ctx
= __info
;
6836 struct perf_event
*event
, *tmp
;
6838 perf_pmu_rotate_stop(ctx
->pmu
);
6840 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
, group_entry
)
6841 __perf_remove_from_context(event
);
6842 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
, group_entry
)
6843 __perf_remove_from_context(event
);
6846 static void perf_event_exit_cpu_context(int cpu
)
6848 struct perf_event_context
*ctx
;
6852 idx
= srcu_read_lock(&pmus_srcu
);
6853 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
6854 ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
)->ctx
;
6856 mutex_lock(&ctx
->mutex
);
6857 smp_call_function_single(cpu
, __perf_event_exit_context
, ctx
, 1);
6858 mutex_unlock(&ctx
->mutex
);
6860 srcu_read_unlock(&pmus_srcu
, idx
);
6863 static void perf_event_exit_cpu(int cpu
)
6865 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
6867 mutex_lock(&swhash
->hlist_mutex
);
6868 swevent_hlist_release(swhash
);
6869 mutex_unlock(&swhash
->hlist_mutex
);
6871 perf_event_exit_cpu_context(cpu
);
6874 static inline void perf_event_exit_cpu(int cpu
) { }
6878 perf_reboot(struct notifier_block
*notifier
, unsigned long val
, void *v
)
6882 for_each_online_cpu(cpu
)
6883 perf_event_exit_cpu(cpu
);
6889 * Run the perf reboot notifier at the very last possible moment so that
6890 * the generic watchdog code runs as long as possible.
6892 static struct notifier_block perf_reboot_notifier
= {
6893 .notifier_call
= perf_reboot
,
6894 .priority
= INT_MIN
,
6897 static int __cpuinit
6898 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
6900 unsigned int cpu
= (long)hcpu
;
6902 switch (action
& ~CPU_TASKS_FROZEN
) {
6904 case CPU_UP_PREPARE
:
6905 case CPU_DOWN_FAILED
:
6906 perf_event_init_cpu(cpu
);
6909 case CPU_UP_CANCELED
:
6910 case CPU_DOWN_PREPARE
:
6911 perf_event_exit_cpu(cpu
);
6921 void __init
perf_event_init(void)
6927 perf_event_init_all_cpus();
6928 init_srcu_struct(&pmus_srcu
);
6929 perf_pmu_register(&perf_swevent
, "software", PERF_TYPE_SOFTWARE
);
6930 perf_pmu_register(&perf_cpu_clock
, NULL
, -1);
6931 perf_pmu_register(&perf_task_clock
, NULL
, -1);
6933 perf_cpu_notifier(perf_cpu_notify
);
6934 register_reboot_notifier(&perf_reboot_notifier
);
6936 ret
= init_hw_breakpoint();
6937 WARN(ret
, "hw_breakpoint initialization failed with: %d", ret
);
6940 static int __init
perf_event_sysfs_init(void)
6945 mutex_lock(&pmus_lock
);
6947 ret
= bus_register(&pmu_bus
);
6951 list_for_each_entry(pmu
, &pmus
, entry
) {
6952 if (!pmu
->name
|| pmu
->type
< 0)
6955 ret
= pmu_dev_alloc(pmu
);
6956 WARN(ret
, "Failed to register pmu: %s, reason %d\n", pmu
->name
, ret
);
6958 pmu_bus_running
= 1;
6962 mutex_unlock(&pmus_lock
);
6966 device_initcall(perf_event_sysfs_init
);
6968 #ifdef CONFIG_CGROUP_PERF
6969 static struct cgroup_subsys_state
*perf_cgroup_create(
6970 struct cgroup_subsys
*ss
, struct cgroup
*cont
)
6972 struct perf_cgroup
*jc
;
6974 jc
= kzalloc(sizeof(*jc
), GFP_KERNEL
);
6976 return ERR_PTR(-ENOMEM
);
6978 jc
->info
= alloc_percpu(struct perf_cgroup_info
);
6981 return ERR_PTR(-ENOMEM
);
6987 static void perf_cgroup_destroy(struct cgroup_subsys
*ss
,
6988 struct cgroup
*cont
)
6990 struct perf_cgroup
*jc
;
6991 jc
= container_of(cgroup_subsys_state(cont
, perf_subsys_id
),
6992 struct perf_cgroup
, css
);
6993 free_percpu(jc
->info
);
6997 static int __perf_cgroup_move(void *info
)
6999 struct task_struct
*task
= info
;
7000 perf_cgroup_switch(task
, PERF_CGROUP_SWOUT
| PERF_CGROUP_SWIN
);
7005 perf_cgroup_attach_task(struct cgroup
*cgrp
, struct task_struct
*task
)
7007 task_function_call(task
, __perf_cgroup_move
, task
);
7010 static void perf_cgroup_exit(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
,
7011 struct cgroup
*old_cgrp
, struct task_struct
*task
)
7014 * cgroup_exit() is called in the copy_process() failure path.
7015 * Ignore this case since the task hasn't ran yet, this avoids
7016 * trying to poke a half freed task state from generic code.
7018 if (!(task
->flags
& PF_EXITING
))
7021 perf_cgroup_attach_task(cgrp
, task
);
7024 struct cgroup_subsys perf_subsys
= {
7025 .name
= "perf_event",
7026 .subsys_id
= perf_subsys_id
,
7027 .create
= perf_cgroup_create
,
7028 .destroy
= perf_cgroup_destroy
,
7029 .exit
= perf_cgroup_exit
,
7030 .attach_task
= perf_cgroup_attach_task
,
7032 #endif /* CONFIG_CGROUP_PERF */