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/tick.h>
22 #include <linux/sysfs.h>
23 #include <linux/dcache.h>
24 #include <linux/percpu.h>
25 #include <linux/ptrace.h>
26 #include <linux/reboot.h>
27 #include <linux/vmstat.h>
28 #include <linux/device.h>
29 #include <linux/export.h>
30 #include <linux/vmalloc.h>
31 #include <linux/hardirq.h>
32 #include <linux/rculist.h>
33 #include <linux/uaccess.h>
34 #include <linux/syscalls.h>
35 #include <linux/anon_inodes.h>
36 #include <linux/kernel_stat.h>
37 #include <linux/perf_event.h>
38 #include <linux/ftrace_event.h>
39 #include <linux/hw_breakpoint.h>
40 #include <linux/mm_types.h>
41 #include <linux/cgroup.h>
42 #include <linux/compat.h>
46 #include <asm/irq_regs.h>
48 struct remote_function_call
{
49 struct task_struct
*p
;
50 int (*func
)(void *info
);
55 static void remote_function(void *data
)
57 struct remote_function_call
*tfc
= data
;
58 struct task_struct
*p
= tfc
->p
;
62 if (task_cpu(p
) != smp_processor_id() || !task_curr(p
))
66 tfc
->ret
= tfc
->func(tfc
->info
);
70 * task_function_call - call a function on the cpu on which a task runs
71 * @p: the task to evaluate
72 * @func: the function to be called
73 * @info: the function call argument
75 * Calls the function @func when the task is currently running. This might
76 * be on the current CPU, which just calls the function directly
78 * returns: @func return value, or
79 * -ESRCH - when the process isn't running
80 * -EAGAIN - when the process moved away
83 task_function_call(struct task_struct
*p
, int (*func
) (void *info
), void *info
)
85 struct remote_function_call data
= {
89 .ret
= -ESRCH
, /* No such (running) process */
93 smp_call_function_single(task_cpu(p
), remote_function
, &data
, 1);
99 * cpu_function_call - call a function on the cpu
100 * @func: the function to be called
101 * @info: the function call argument
103 * Calls the function @func on the remote cpu.
105 * returns: @func return value or -ENXIO when the cpu is offline
107 static int cpu_function_call(int cpu
, int (*func
) (void *info
), void *info
)
109 struct remote_function_call data
= {
113 .ret
= -ENXIO
, /* No such CPU */
116 smp_call_function_single(cpu
, remote_function
, &data
, 1);
121 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
122 PERF_FLAG_FD_OUTPUT |\
123 PERF_FLAG_PID_CGROUP)
126 * branch priv levels that need permission checks
128 #define PERF_SAMPLE_BRANCH_PERM_PLM \
129 (PERF_SAMPLE_BRANCH_KERNEL |\
130 PERF_SAMPLE_BRANCH_HV)
133 EVENT_FLEXIBLE
= 0x1,
135 EVENT_ALL
= EVENT_FLEXIBLE
| EVENT_PINNED
,
139 * perf_sched_events : >0 events exist
140 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
142 struct static_key_deferred perf_sched_events __read_mostly
;
143 static DEFINE_PER_CPU(atomic_t
, perf_cgroup_events
);
144 static DEFINE_PER_CPU(atomic_t
, perf_branch_stack_events
);
146 static atomic_t nr_mmap_events __read_mostly
;
147 static atomic_t nr_comm_events __read_mostly
;
148 static atomic_t nr_task_events __read_mostly
;
150 static LIST_HEAD(pmus
);
151 static DEFINE_MUTEX(pmus_lock
);
152 static struct srcu_struct pmus_srcu
;
155 * perf event paranoia level:
156 * -1 - not paranoid at all
157 * 0 - disallow raw tracepoint access for unpriv
158 * 1 - disallow cpu events for unpriv
159 * 2 - disallow kernel profiling for unpriv
161 int sysctl_perf_event_paranoid __read_mostly
= 1;
163 /* Minimum for 512 kiB + 1 user control page */
164 int sysctl_perf_event_mlock __read_mostly
= 512 + (PAGE_SIZE
/ 1024); /* 'free' kiB per user */
167 * max perf event sample rate
169 #define DEFAULT_MAX_SAMPLE_RATE 100000
170 #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
171 #define DEFAULT_CPU_TIME_MAX_PERCENT 25
173 int sysctl_perf_event_sample_rate __read_mostly
= DEFAULT_MAX_SAMPLE_RATE
;
175 static int max_samples_per_tick __read_mostly
= DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE
, HZ
);
176 static int perf_sample_period_ns __read_mostly
= DEFAULT_SAMPLE_PERIOD_NS
;
178 static atomic_t perf_sample_allowed_ns __read_mostly
=
179 ATOMIC_INIT( DEFAULT_SAMPLE_PERIOD_NS
* DEFAULT_CPU_TIME_MAX_PERCENT
/ 100);
181 void update_perf_cpu_limits(void)
183 u64 tmp
= perf_sample_period_ns
;
185 tmp
*= sysctl_perf_cpu_time_max_percent
;
187 atomic_set(&perf_sample_allowed_ns
, tmp
);
190 int perf_proc_update_handler(struct ctl_table
*table
, int write
,
191 void __user
*buffer
, size_t *lenp
,
194 int ret
= proc_dointvec_minmax(table
, write
, buffer
, lenp
, ppos
);
199 max_samples_per_tick
= DIV_ROUND_UP(sysctl_perf_event_sample_rate
, HZ
);
200 perf_sample_period_ns
= NSEC_PER_SEC
/ sysctl_perf_event_sample_rate
;
201 update_perf_cpu_limits();
206 int sysctl_perf_cpu_time_max_percent __read_mostly
= DEFAULT_CPU_TIME_MAX_PERCENT
;
208 int perf_cpu_time_max_percent_handler(struct ctl_table
*table
, int write
,
209 void __user
*buffer
, size_t *lenp
,
212 int ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
217 update_perf_cpu_limits();
223 * perf samples are done in some very critical code paths (NMIs).
224 * If they take too much CPU time, the system can lock up and not
225 * get any real work done. This will drop the sample rate when
226 * we detect that events are taking too long.
228 #define NR_ACCUMULATED_SAMPLES 128
229 DEFINE_PER_CPU(u64
, running_sample_length
);
231 void perf_sample_event_took(u64 sample_len_ns
)
233 u64 avg_local_sample_len
;
234 u64 local_samples_len
;
236 if (atomic_read(&perf_sample_allowed_ns
) == 0)
239 /* decay the counter by 1 average sample */
240 local_samples_len
= __get_cpu_var(running_sample_length
);
241 local_samples_len
-= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
242 local_samples_len
+= sample_len_ns
;
243 __get_cpu_var(running_sample_length
) = local_samples_len
;
246 * note: this will be biased artifically low until we have
247 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
248 * from having to maintain a count.
250 avg_local_sample_len
= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
252 if (avg_local_sample_len
<= atomic_read(&perf_sample_allowed_ns
))
255 if (max_samples_per_tick
<= 1)
258 max_samples_per_tick
= DIV_ROUND_UP(max_samples_per_tick
, 2);
259 sysctl_perf_event_sample_rate
= max_samples_per_tick
* HZ
;
260 perf_sample_period_ns
= NSEC_PER_SEC
/ sysctl_perf_event_sample_rate
;
262 printk_ratelimited(KERN_WARNING
263 "perf samples too long (%lld > %d), lowering "
264 "kernel.perf_event_max_sample_rate to %d\n",
265 avg_local_sample_len
,
266 atomic_read(&perf_sample_allowed_ns
),
267 sysctl_perf_event_sample_rate
);
269 update_perf_cpu_limits();
272 static atomic64_t perf_event_id
;
274 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
275 enum event_type_t event_type
);
277 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
278 enum event_type_t event_type
,
279 struct task_struct
*task
);
281 static void update_context_time(struct perf_event_context
*ctx
);
282 static u64
perf_event_time(struct perf_event
*event
);
284 void __weak
perf_event_print_debug(void) { }
286 extern __weak
const char *perf_pmu_name(void)
291 static inline u64
perf_clock(void)
293 return local_clock();
296 static inline struct perf_cpu_context
*
297 __get_cpu_context(struct perf_event_context
*ctx
)
299 return this_cpu_ptr(ctx
->pmu
->pmu_cpu_context
);
302 static void perf_ctx_lock(struct perf_cpu_context
*cpuctx
,
303 struct perf_event_context
*ctx
)
305 raw_spin_lock(&cpuctx
->ctx
.lock
);
307 raw_spin_lock(&ctx
->lock
);
310 static void perf_ctx_unlock(struct perf_cpu_context
*cpuctx
,
311 struct perf_event_context
*ctx
)
314 raw_spin_unlock(&ctx
->lock
);
315 raw_spin_unlock(&cpuctx
->ctx
.lock
);
318 #ifdef CONFIG_CGROUP_PERF
321 * perf_cgroup_info keeps track of time_enabled for a cgroup.
322 * This is a per-cpu dynamically allocated data structure.
324 struct perf_cgroup_info
{
330 struct cgroup_subsys_state css
;
331 struct perf_cgroup_info __percpu
*info
;
335 * Must ensure cgroup is pinned (css_get) before calling
336 * this function. In other words, we cannot call this function
337 * if there is no cgroup event for the current CPU context.
339 static inline struct perf_cgroup
*
340 perf_cgroup_from_task(struct task_struct
*task
)
342 return container_of(task_subsys_state(task
, perf_subsys_id
),
343 struct perf_cgroup
, css
);
347 perf_cgroup_match(struct perf_event
*event
)
349 struct perf_event_context
*ctx
= event
->ctx
;
350 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
352 /* @event doesn't care about cgroup */
356 /* wants specific cgroup scope but @cpuctx isn't associated with any */
361 * Cgroup scoping is recursive. An event enabled for a cgroup is
362 * also enabled for all its descendant cgroups. If @cpuctx's
363 * cgroup is a descendant of @event's (the test covers identity
364 * case), it's a match.
366 return cgroup_is_descendant(cpuctx
->cgrp
->css
.cgroup
,
367 event
->cgrp
->css
.cgroup
);
370 static inline bool perf_tryget_cgroup(struct perf_event
*event
)
372 return css_tryget(&event
->cgrp
->css
);
375 static inline void perf_put_cgroup(struct perf_event
*event
)
377 css_put(&event
->cgrp
->css
);
380 static inline void perf_detach_cgroup(struct perf_event
*event
)
382 perf_put_cgroup(event
);
386 static inline int is_cgroup_event(struct perf_event
*event
)
388 return event
->cgrp
!= NULL
;
391 static inline u64
perf_cgroup_event_time(struct perf_event
*event
)
393 struct perf_cgroup_info
*t
;
395 t
= per_cpu_ptr(event
->cgrp
->info
, event
->cpu
);
399 static inline void __update_cgrp_time(struct perf_cgroup
*cgrp
)
401 struct perf_cgroup_info
*info
;
406 info
= this_cpu_ptr(cgrp
->info
);
408 info
->time
+= now
- info
->timestamp
;
409 info
->timestamp
= now
;
412 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context
*cpuctx
)
414 struct perf_cgroup
*cgrp_out
= cpuctx
->cgrp
;
416 __update_cgrp_time(cgrp_out
);
419 static inline void update_cgrp_time_from_event(struct perf_event
*event
)
421 struct perf_cgroup
*cgrp
;
424 * ensure we access cgroup data only when needed and
425 * when we know the cgroup is pinned (css_get)
427 if (!is_cgroup_event(event
))
430 cgrp
= perf_cgroup_from_task(current
);
432 * Do not update time when cgroup is not active
434 if (cgrp
== event
->cgrp
)
435 __update_cgrp_time(event
->cgrp
);
439 perf_cgroup_set_timestamp(struct task_struct
*task
,
440 struct perf_event_context
*ctx
)
442 struct perf_cgroup
*cgrp
;
443 struct perf_cgroup_info
*info
;
446 * ctx->lock held by caller
447 * ensure we do not access cgroup data
448 * unless we have the cgroup pinned (css_get)
450 if (!task
|| !ctx
->nr_cgroups
)
453 cgrp
= perf_cgroup_from_task(task
);
454 info
= this_cpu_ptr(cgrp
->info
);
455 info
->timestamp
= ctx
->timestamp
;
458 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
459 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
462 * reschedule events based on the cgroup constraint of task.
464 * mode SWOUT : schedule out everything
465 * mode SWIN : schedule in based on cgroup for next
467 void perf_cgroup_switch(struct task_struct
*task
, int mode
)
469 struct perf_cpu_context
*cpuctx
;
474 * disable interrupts to avoid geting nr_cgroup
475 * changes via __perf_event_disable(). Also
478 local_irq_save(flags
);
481 * we reschedule only in the presence of cgroup
482 * constrained events.
486 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
487 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
488 if (cpuctx
->unique_pmu
!= pmu
)
489 continue; /* ensure we process each cpuctx once */
492 * perf_cgroup_events says at least one
493 * context on this CPU has cgroup events.
495 * ctx->nr_cgroups reports the number of cgroup
496 * events for a context.
498 if (cpuctx
->ctx
.nr_cgroups
> 0) {
499 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
500 perf_pmu_disable(cpuctx
->ctx
.pmu
);
502 if (mode
& PERF_CGROUP_SWOUT
) {
503 cpu_ctx_sched_out(cpuctx
, EVENT_ALL
);
505 * must not be done before ctxswout due
506 * to event_filter_match() in event_sched_out()
511 if (mode
& PERF_CGROUP_SWIN
) {
512 WARN_ON_ONCE(cpuctx
->cgrp
);
514 * set cgrp before ctxsw in to allow
515 * event_filter_match() to not have to pass
518 cpuctx
->cgrp
= perf_cgroup_from_task(task
);
519 cpu_ctx_sched_in(cpuctx
, EVENT_ALL
, task
);
521 perf_pmu_enable(cpuctx
->ctx
.pmu
);
522 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
528 local_irq_restore(flags
);
531 static inline void perf_cgroup_sched_out(struct task_struct
*task
,
532 struct task_struct
*next
)
534 struct perf_cgroup
*cgrp1
;
535 struct perf_cgroup
*cgrp2
= NULL
;
538 * we come here when we know perf_cgroup_events > 0
540 cgrp1
= perf_cgroup_from_task(task
);
543 * next is NULL when called from perf_event_enable_on_exec()
544 * that will systematically cause a cgroup_switch()
547 cgrp2
= perf_cgroup_from_task(next
);
550 * only schedule out current cgroup events if we know
551 * that we are switching to a different cgroup. Otherwise,
552 * do no touch the cgroup events.
555 perf_cgroup_switch(task
, PERF_CGROUP_SWOUT
);
558 static inline void perf_cgroup_sched_in(struct task_struct
*prev
,
559 struct task_struct
*task
)
561 struct perf_cgroup
*cgrp1
;
562 struct perf_cgroup
*cgrp2
= NULL
;
565 * we come here when we know perf_cgroup_events > 0
567 cgrp1
= perf_cgroup_from_task(task
);
569 /* prev can never be NULL */
570 cgrp2
= perf_cgroup_from_task(prev
);
573 * only need to schedule in cgroup events if we are changing
574 * cgroup during ctxsw. Cgroup events were not scheduled
575 * out of ctxsw out if that was not the case.
578 perf_cgroup_switch(task
, PERF_CGROUP_SWIN
);
581 static inline int perf_cgroup_connect(int fd
, struct perf_event
*event
,
582 struct perf_event_attr
*attr
,
583 struct perf_event
*group_leader
)
585 struct perf_cgroup
*cgrp
;
586 struct cgroup_subsys_state
*css
;
587 struct fd f
= fdget(fd
);
593 css
= cgroup_css_from_dir(f
.file
, perf_subsys_id
);
599 cgrp
= container_of(css
, struct perf_cgroup
, css
);
602 /* must be done before we fput() the file */
603 if (!perf_tryget_cgroup(event
)) {
610 * all events in a group must monitor
611 * the same cgroup because a task belongs
612 * to only one perf cgroup at a time
614 if (group_leader
&& group_leader
->cgrp
!= cgrp
) {
615 perf_detach_cgroup(event
);
624 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
626 struct perf_cgroup_info
*t
;
627 t
= per_cpu_ptr(event
->cgrp
->info
, event
->cpu
);
628 event
->shadow_ctx_time
= now
- t
->timestamp
;
632 perf_cgroup_defer_enabled(struct perf_event
*event
)
635 * when the current task's perf cgroup does not match
636 * the event's, we need to remember to call the
637 * perf_mark_enable() function the first time a task with
638 * a matching perf cgroup is scheduled in.
640 if (is_cgroup_event(event
) && !perf_cgroup_match(event
))
641 event
->cgrp_defer_enabled
= 1;
645 perf_cgroup_mark_enabled(struct perf_event
*event
,
646 struct perf_event_context
*ctx
)
648 struct perf_event
*sub
;
649 u64 tstamp
= perf_event_time(event
);
651 if (!event
->cgrp_defer_enabled
)
654 event
->cgrp_defer_enabled
= 0;
656 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
657 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
658 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
) {
659 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
660 sub
->cgrp_defer_enabled
= 0;
664 #else /* !CONFIG_CGROUP_PERF */
667 perf_cgroup_match(struct perf_event
*event
)
672 static inline void perf_detach_cgroup(struct perf_event
*event
)
675 static inline int is_cgroup_event(struct perf_event
*event
)
680 static inline u64
perf_cgroup_event_cgrp_time(struct perf_event
*event
)
685 static inline void update_cgrp_time_from_event(struct perf_event
*event
)
689 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context
*cpuctx
)
693 static inline void perf_cgroup_sched_out(struct task_struct
*task
,
694 struct task_struct
*next
)
698 static inline void perf_cgroup_sched_in(struct task_struct
*prev
,
699 struct task_struct
*task
)
703 static inline int perf_cgroup_connect(pid_t pid
, struct perf_event
*event
,
704 struct perf_event_attr
*attr
,
705 struct perf_event
*group_leader
)
711 perf_cgroup_set_timestamp(struct task_struct
*task
,
712 struct perf_event_context
*ctx
)
717 perf_cgroup_switch(struct task_struct
*task
, struct task_struct
*next
)
722 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
726 static inline u64
perf_cgroup_event_time(struct perf_event
*event
)
732 perf_cgroup_defer_enabled(struct perf_event
*event
)
737 perf_cgroup_mark_enabled(struct perf_event
*event
,
738 struct perf_event_context
*ctx
)
743 void perf_pmu_disable(struct pmu
*pmu
)
745 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
747 pmu
->pmu_disable(pmu
);
750 void perf_pmu_enable(struct pmu
*pmu
)
752 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
754 pmu
->pmu_enable(pmu
);
757 static DEFINE_PER_CPU(struct list_head
, rotation_list
);
760 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
761 * because they're strictly cpu affine and rotate_start is called with IRQs
762 * disabled, while rotate_context is called from IRQ context.
764 static void perf_pmu_rotate_start(struct pmu
*pmu
)
766 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
767 struct list_head
*head
= &__get_cpu_var(rotation_list
);
769 WARN_ON(!irqs_disabled());
771 if (list_empty(&cpuctx
->rotation_list
)) {
772 int was_empty
= list_empty(head
);
773 list_add(&cpuctx
->rotation_list
, head
);
775 tick_nohz_full_kick();
779 static void get_ctx(struct perf_event_context
*ctx
)
781 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
784 static void put_ctx(struct perf_event_context
*ctx
)
786 if (atomic_dec_and_test(&ctx
->refcount
)) {
788 put_ctx(ctx
->parent_ctx
);
790 put_task_struct(ctx
->task
);
791 kfree_rcu(ctx
, rcu_head
);
795 static void unclone_ctx(struct perf_event_context
*ctx
)
797 if (ctx
->parent_ctx
) {
798 put_ctx(ctx
->parent_ctx
);
799 ctx
->parent_ctx
= NULL
;
803 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
806 * only top level events have the pid namespace they were created in
809 event
= event
->parent
;
811 return task_tgid_nr_ns(p
, event
->ns
);
814 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
817 * only top level events have the pid namespace they were created in
820 event
= event
->parent
;
822 return task_pid_nr_ns(p
, event
->ns
);
826 * If we inherit events we want to return the parent event id
829 static u64
primary_event_id(struct perf_event
*event
)
834 id
= event
->parent
->id
;
840 * Get the perf_event_context for a task and lock it.
841 * This has to cope with with the fact that until it is locked,
842 * the context could get moved to another task.
844 static struct perf_event_context
*
845 perf_lock_task_context(struct task_struct
*task
, int ctxn
, unsigned long *flags
)
847 struct perf_event_context
*ctx
;
851 * One of the few rules of preemptible RCU is that one cannot do
852 * rcu_read_unlock() while holding a scheduler (or nested) lock when
853 * part of the read side critical section was preemptible -- see
854 * rcu_read_unlock_special().
856 * Since ctx->lock nests under rq->lock we must ensure the entire read
857 * side critical section is non-preemptible.
861 ctx
= rcu_dereference(task
->perf_event_ctxp
[ctxn
]);
864 * If this context is a clone of another, it might
865 * get swapped for another underneath us by
866 * perf_event_task_sched_out, though the
867 * rcu_read_lock() protects us from any context
868 * getting freed. Lock the context and check if it
869 * got swapped before we could get the lock, and retry
870 * if so. If we locked the right context, then it
871 * can't get swapped on us any more.
873 raw_spin_lock_irqsave(&ctx
->lock
, *flags
);
874 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
[ctxn
])) {
875 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
881 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
882 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
892 * Get the context for a task and increment its pin_count so it
893 * can't get swapped to another task. This also increments its
894 * reference count so that the context can't get freed.
896 static struct perf_event_context
*
897 perf_pin_task_context(struct task_struct
*task
, int ctxn
)
899 struct perf_event_context
*ctx
;
902 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
905 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
910 static void perf_unpin_context(struct perf_event_context
*ctx
)
914 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
916 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
920 * Update the record of the current time in a context.
922 static void update_context_time(struct perf_event_context
*ctx
)
924 u64 now
= perf_clock();
926 ctx
->time
+= now
- ctx
->timestamp
;
927 ctx
->timestamp
= now
;
930 static u64
perf_event_time(struct perf_event
*event
)
932 struct perf_event_context
*ctx
= event
->ctx
;
934 if (is_cgroup_event(event
))
935 return perf_cgroup_event_time(event
);
937 return ctx
? ctx
->time
: 0;
941 * Update the total_time_enabled and total_time_running fields for a event.
942 * The caller of this function needs to hold the ctx->lock.
944 static void update_event_times(struct perf_event
*event
)
946 struct perf_event_context
*ctx
= event
->ctx
;
949 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
950 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
953 * in cgroup mode, time_enabled represents
954 * the time the event was enabled AND active
955 * tasks were in the monitored cgroup. This is
956 * independent of the activity of the context as
957 * there may be a mix of cgroup and non-cgroup events.
959 * That is why we treat cgroup events differently
962 if (is_cgroup_event(event
))
963 run_end
= perf_cgroup_event_time(event
);
964 else if (ctx
->is_active
)
967 run_end
= event
->tstamp_stopped
;
969 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
971 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
972 run_end
= event
->tstamp_stopped
;
974 run_end
= perf_event_time(event
);
976 event
->total_time_running
= run_end
- event
->tstamp_running
;
981 * Update total_time_enabled and total_time_running for all events in a group.
983 static void update_group_times(struct perf_event
*leader
)
985 struct perf_event
*event
;
987 update_event_times(leader
);
988 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
989 update_event_times(event
);
992 static struct list_head
*
993 ctx_group_list(struct perf_event
*event
, struct perf_event_context
*ctx
)
995 if (event
->attr
.pinned
)
996 return &ctx
->pinned_groups
;
998 return &ctx
->flexible_groups
;
1002 * Add a event from the lists for its context.
1003 * Must be called with ctx->mutex and ctx->lock held.
1006 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
1008 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_CONTEXT
);
1009 event
->attach_state
|= PERF_ATTACH_CONTEXT
;
1012 * If we're a stand alone event or group leader, we go to the context
1013 * list, group events are kept attached to the group so that
1014 * perf_group_detach can, at all times, locate all siblings.
1016 if (event
->group_leader
== event
) {
1017 struct list_head
*list
;
1019 if (is_software_event(event
))
1020 event
->group_flags
|= PERF_GROUP_SOFTWARE
;
1022 list
= ctx_group_list(event
, ctx
);
1023 list_add_tail(&event
->group_entry
, list
);
1026 if (is_cgroup_event(event
))
1029 if (has_branch_stack(event
))
1030 ctx
->nr_branch_stack
++;
1032 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
1033 if (!ctx
->nr_events
)
1034 perf_pmu_rotate_start(ctx
->pmu
);
1036 if (event
->attr
.inherit_stat
)
1041 * Initialize event state based on the perf_event_attr::disabled.
1043 static inline void perf_event__state_init(struct perf_event
*event
)
1045 event
->state
= event
->attr
.disabled
? PERF_EVENT_STATE_OFF
:
1046 PERF_EVENT_STATE_INACTIVE
;
1050 * Called at perf_event creation and when events are attached/detached from a
1053 static void perf_event__read_size(struct perf_event
*event
)
1055 int entry
= sizeof(u64
); /* value */
1059 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1060 size
+= sizeof(u64
);
1062 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1063 size
+= sizeof(u64
);
1065 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
1066 entry
+= sizeof(u64
);
1068 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
1069 nr
+= event
->group_leader
->nr_siblings
;
1070 size
+= sizeof(u64
);
1074 event
->read_size
= size
;
1077 static void perf_event__header_size(struct perf_event
*event
)
1079 struct perf_sample_data
*data
;
1080 u64 sample_type
= event
->attr
.sample_type
;
1083 perf_event__read_size(event
);
1085 if (sample_type
& PERF_SAMPLE_IP
)
1086 size
+= sizeof(data
->ip
);
1088 if (sample_type
& PERF_SAMPLE_ADDR
)
1089 size
+= sizeof(data
->addr
);
1091 if (sample_type
& PERF_SAMPLE_PERIOD
)
1092 size
+= sizeof(data
->period
);
1094 if (sample_type
& PERF_SAMPLE_WEIGHT
)
1095 size
+= sizeof(data
->weight
);
1097 if (sample_type
& PERF_SAMPLE_READ
)
1098 size
+= event
->read_size
;
1100 if (sample_type
& PERF_SAMPLE_DATA_SRC
)
1101 size
+= sizeof(data
->data_src
.val
);
1103 event
->header_size
= size
;
1106 static void perf_event__id_header_size(struct perf_event
*event
)
1108 struct perf_sample_data
*data
;
1109 u64 sample_type
= event
->attr
.sample_type
;
1112 if (sample_type
& PERF_SAMPLE_TID
)
1113 size
+= sizeof(data
->tid_entry
);
1115 if (sample_type
& PERF_SAMPLE_TIME
)
1116 size
+= sizeof(data
->time
);
1118 if (sample_type
& PERF_SAMPLE_ID
)
1119 size
+= sizeof(data
->id
);
1121 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
1122 size
+= sizeof(data
->stream_id
);
1124 if (sample_type
& PERF_SAMPLE_CPU
)
1125 size
+= sizeof(data
->cpu_entry
);
1127 event
->id_header_size
= size
;
1130 static void perf_group_attach(struct perf_event
*event
)
1132 struct perf_event
*group_leader
= event
->group_leader
, *pos
;
1135 * We can have double attach due to group movement in perf_event_open.
1137 if (event
->attach_state
& PERF_ATTACH_GROUP
)
1140 event
->attach_state
|= PERF_ATTACH_GROUP
;
1142 if (group_leader
== event
)
1145 if (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
&&
1146 !is_software_event(event
))
1147 group_leader
->group_flags
&= ~PERF_GROUP_SOFTWARE
;
1149 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
1150 group_leader
->nr_siblings
++;
1152 perf_event__header_size(group_leader
);
1154 list_for_each_entry(pos
, &group_leader
->sibling_list
, group_entry
)
1155 perf_event__header_size(pos
);
1159 * Remove a event from the lists for its context.
1160 * Must be called with ctx->mutex and ctx->lock held.
1163 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
1165 struct perf_cpu_context
*cpuctx
;
1167 * We can have double detach due to exit/hot-unplug + close.
1169 if (!(event
->attach_state
& PERF_ATTACH_CONTEXT
))
1172 event
->attach_state
&= ~PERF_ATTACH_CONTEXT
;
1174 if (is_cgroup_event(event
)) {
1176 cpuctx
= __get_cpu_context(ctx
);
1178 * if there are no more cgroup events
1179 * then cler cgrp to avoid stale pointer
1180 * in update_cgrp_time_from_cpuctx()
1182 if (!ctx
->nr_cgroups
)
1183 cpuctx
->cgrp
= NULL
;
1186 if (has_branch_stack(event
))
1187 ctx
->nr_branch_stack
--;
1190 if (event
->attr
.inherit_stat
)
1193 list_del_rcu(&event
->event_entry
);
1195 if (event
->group_leader
== event
)
1196 list_del_init(&event
->group_entry
);
1198 update_group_times(event
);
1201 * If event was in error state, then keep it
1202 * that way, otherwise bogus counts will be
1203 * returned on read(). The only way to get out
1204 * of error state is by explicit re-enabling
1207 if (event
->state
> PERF_EVENT_STATE_OFF
)
1208 event
->state
= PERF_EVENT_STATE_OFF
;
1211 static void perf_group_detach(struct perf_event
*event
)
1213 struct perf_event
*sibling
, *tmp
;
1214 struct list_head
*list
= NULL
;
1217 * We can have double detach due to exit/hot-unplug + close.
1219 if (!(event
->attach_state
& PERF_ATTACH_GROUP
))
1222 event
->attach_state
&= ~PERF_ATTACH_GROUP
;
1225 * If this is a sibling, remove it from its group.
1227 if (event
->group_leader
!= event
) {
1228 list_del_init(&event
->group_entry
);
1229 event
->group_leader
->nr_siblings
--;
1233 if (!list_empty(&event
->group_entry
))
1234 list
= &event
->group_entry
;
1237 * If this was a group event with sibling events then
1238 * upgrade the siblings to singleton events by adding them
1239 * to whatever list we are on.
1240 * If this isn't on a list, make sure we still remove the sibling's
1241 * group_entry from this sibling_list; otherwise, when that sibling
1242 * is later deallocated, it will try to remove itself from this
1243 * sibling_list, which may well have been deallocated already,
1244 * resulting in a use-after-free.
1246 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
1248 list_move_tail(&sibling
->group_entry
, list
);
1250 list_del_init(&sibling
->group_entry
);
1251 sibling
->group_leader
= sibling
;
1253 /* Inherit group flags from the previous leader */
1254 sibling
->group_flags
= event
->group_flags
;
1258 perf_event__header_size(event
->group_leader
);
1260 list_for_each_entry(tmp
, &event
->group_leader
->sibling_list
, group_entry
)
1261 perf_event__header_size(tmp
);
1265 event_filter_match(struct perf_event
*event
)
1267 return (event
->cpu
== -1 || event
->cpu
== smp_processor_id())
1268 && perf_cgroup_match(event
);
1272 event_sched_out(struct perf_event
*event
,
1273 struct perf_cpu_context
*cpuctx
,
1274 struct perf_event_context
*ctx
)
1276 u64 tstamp
= perf_event_time(event
);
1279 * An event which could not be activated because of
1280 * filter mismatch still needs to have its timings
1281 * maintained, otherwise bogus information is return
1282 * via read() for time_enabled, time_running:
1284 if (event
->state
== PERF_EVENT_STATE_INACTIVE
1285 && !event_filter_match(event
)) {
1286 delta
= tstamp
- event
->tstamp_stopped
;
1287 event
->tstamp_running
+= delta
;
1288 event
->tstamp_stopped
= tstamp
;
1291 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1294 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1295 if (event
->pending_disable
) {
1296 event
->pending_disable
= 0;
1297 event
->state
= PERF_EVENT_STATE_OFF
;
1299 event
->tstamp_stopped
= tstamp
;
1300 event
->pmu
->del(event
, 0);
1303 if (!is_software_event(event
))
1304 cpuctx
->active_oncpu
--;
1306 if (event
->attr
.freq
&& event
->attr
.sample_freq
)
1308 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
1309 cpuctx
->exclusive
= 0;
1313 group_sched_out(struct perf_event
*group_event
,
1314 struct perf_cpu_context
*cpuctx
,
1315 struct perf_event_context
*ctx
)
1317 struct perf_event
*event
;
1318 int state
= group_event
->state
;
1320 event_sched_out(group_event
, cpuctx
, ctx
);
1323 * Schedule out siblings (if any):
1325 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
1326 event_sched_out(event
, cpuctx
, ctx
);
1328 if (state
== PERF_EVENT_STATE_ACTIVE
&& group_event
->attr
.exclusive
)
1329 cpuctx
->exclusive
= 0;
1332 struct remove_event
{
1333 struct perf_event
*event
;
1338 * Cross CPU call to remove a performance event
1340 * We disable the event on the hardware level first. After that we
1341 * remove it from the context list.
1343 static int __perf_remove_from_context(void *info
)
1345 struct remove_event
*re
= info
;
1346 struct perf_event
*event
= re
->event
;
1347 struct perf_event_context
*ctx
= event
->ctx
;
1348 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1350 raw_spin_lock(&ctx
->lock
);
1351 event_sched_out(event
, cpuctx
, ctx
);
1352 if (re
->detach_group
)
1353 perf_group_detach(event
);
1354 list_del_event(event
, ctx
);
1355 if (!ctx
->nr_events
&& cpuctx
->task_ctx
== ctx
) {
1357 cpuctx
->task_ctx
= NULL
;
1359 raw_spin_unlock(&ctx
->lock
);
1366 * Remove the event from a task's (or a CPU's) list of events.
1368 * CPU events are removed with a smp call. For task events we only
1369 * call when the task is on a CPU.
1371 * If event->ctx is a cloned context, callers must make sure that
1372 * every task struct that event->ctx->task could possibly point to
1373 * remains valid. This is OK when called from perf_release since
1374 * that only calls us on the top-level context, which can't be a clone.
1375 * When called from perf_event_exit_task, it's OK because the
1376 * context has been detached from its task.
1378 static void perf_remove_from_context(struct perf_event
*event
, bool detach_group
)
1380 struct perf_event_context
*ctx
= event
->ctx
;
1381 struct task_struct
*task
= ctx
->task
;
1382 struct remove_event re
= {
1384 .detach_group
= detach_group
,
1387 lockdep_assert_held(&ctx
->mutex
);
1391 * Per cpu events are removed via an smp call and
1392 * the removal is always successful.
1394 cpu_function_call(event
->cpu
, __perf_remove_from_context
, &re
);
1399 if (!task_function_call(task
, __perf_remove_from_context
, &re
))
1402 raw_spin_lock_irq(&ctx
->lock
);
1404 * If we failed to find a running task, but find the context active now
1405 * that we've acquired the ctx->lock, retry.
1407 if (ctx
->is_active
) {
1408 raw_spin_unlock_irq(&ctx
->lock
);
1410 * Reload the task pointer, it might have been changed by
1411 * a concurrent perf_event_context_sched_out().
1418 * Since the task isn't running, its safe to remove the event, us
1419 * holding the ctx->lock ensures the task won't get scheduled in.
1422 perf_group_detach(event
);
1423 list_del_event(event
, ctx
);
1424 raw_spin_unlock_irq(&ctx
->lock
);
1428 * Cross CPU call to disable a performance event
1430 int __perf_event_disable(void *info
)
1432 struct perf_event
*event
= info
;
1433 struct perf_event_context
*ctx
= event
->ctx
;
1434 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1437 * If this is a per-task event, need to check whether this
1438 * event's task is the current task on this cpu.
1440 * Can trigger due to concurrent perf_event_context_sched_out()
1441 * flipping contexts around.
1443 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
1446 raw_spin_lock(&ctx
->lock
);
1449 * If the event is on, turn it off.
1450 * If it is in error state, leave it in error state.
1452 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
) {
1453 update_context_time(ctx
);
1454 update_cgrp_time_from_event(event
);
1455 update_group_times(event
);
1456 if (event
== event
->group_leader
)
1457 group_sched_out(event
, cpuctx
, ctx
);
1459 event_sched_out(event
, cpuctx
, ctx
);
1460 event
->state
= PERF_EVENT_STATE_OFF
;
1463 raw_spin_unlock(&ctx
->lock
);
1471 * If event->ctx is a cloned context, callers must make sure that
1472 * every task struct that event->ctx->task could possibly point to
1473 * remains valid. This condition is satisifed when called through
1474 * perf_event_for_each_child or perf_event_for_each because they
1475 * hold the top-level event's child_mutex, so any descendant that
1476 * goes to exit will block in sync_child_event.
1477 * When called from perf_pending_event it's OK because event->ctx
1478 * is the current context on this CPU and preemption is disabled,
1479 * hence we can't get into perf_event_task_sched_out for this context.
1481 void perf_event_disable(struct perf_event
*event
)
1483 struct perf_event_context
*ctx
= event
->ctx
;
1484 struct task_struct
*task
= ctx
->task
;
1488 * Disable the event on the cpu that it's on
1490 cpu_function_call(event
->cpu
, __perf_event_disable
, event
);
1495 if (!task_function_call(task
, __perf_event_disable
, event
))
1498 raw_spin_lock_irq(&ctx
->lock
);
1500 * If the event is still active, we need to retry the cross-call.
1502 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
1503 raw_spin_unlock_irq(&ctx
->lock
);
1505 * Reload the task pointer, it might have been changed by
1506 * a concurrent perf_event_context_sched_out().
1513 * Since we have the lock this context can't be scheduled
1514 * in, so we can change the state safely.
1516 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1517 update_group_times(event
);
1518 event
->state
= PERF_EVENT_STATE_OFF
;
1520 raw_spin_unlock_irq(&ctx
->lock
);
1522 EXPORT_SYMBOL_GPL(perf_event_disable
);
1524 static void perf_set_shadow_time(struct perf_event
*event
,
1525 struct perf_event_context
*ctx
,
1529 * use the correct time source for the time snapshot
1531 * We could get by without this by leveraging the
1532 * fact that to get to this function, the caller
1533 * has most likely already called update_context_time()
1534 * and update_cgrp_time_xx() and thus both timestamp
1535 * are identical (or very close). Given that tstamp is,
1536 * already adjusted for cgroup, we could say that:
1537 * tstamp - ctx->timestamp
1539 * tstamp - cgrp->timestamp.
1541 * Then, in perf_output_read(), the calculation would
1542 * work with no changes because:
1543 * - event is guaranteed scheduled in
1544 * - no scheduled out in between
1545 * - thus the timestamp would be the same
1547 * But this is a bit hairy.
1549 * So instead, we have an explicit cgroup call to remain
1550 * within the time time source all along. We believe it
1551 * is cleaner and simpler to understand.
1553 if (is_cgroup_event(event
))
1554 perf_cgroup_set_shadow_time(event
, tstamp
);
1556 event
->shadow_ctx_time
= tstamp
- ctx
->timestamp
;
1559 #define MAX_INTERRUPTS (~0ULL)
1561 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1564 event_sched_in(struct perf_event
*event
,
1565 struct perf_cpu_context
*cpuctx
,
1566 struct perf_event_context
*ctx
)
1568 u64 tstamp
= perf_event_time(event
);
1570 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1573 event
->state
= PERF_EVENT_STATE_ACTIVE
;
1574 event
->oncpu
= smp_processor_id();
1577 * Unthrottle events, since we scheduled we might have missed several
1578 * ticks already, also for a heavily scheduling task there is little
1579 * guarantee it'll get a tick in a timely manner.
1581 if (unlikely(event
->hw
.interrupts
== MAX_INTERRUPTS
)) {
1582 perf_log_throttle(event
, 1);
1583 event
->hw
.interrupts
= 0;
1587 * The new state must be visible before we turn it on in the hardware:
1591 if (event
->pmu
->add(event
, PERF_EF_START
)) {
1592 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1597 event
->tstamp_running
+= tstamp
- event
->tstamp_stopped
;
1599 perf_set_shadow_time(event
, ctx
, tstamp
);
1601 if (!is_software_event(event
))
1602 cpuctx
->active_oncpu
++;
1604 if (event
->attr
.freq
&& event
->attr
.sample_freq
)
1607 if (event
->attr
.exclusive
)
1608 cpuctx
->exclusive
= 1;
1614 group_sched_in(struct perf_event
*group_event
,
1615 struct perf_cpu_context
*cpuctx
,
1616 struct perf_event_context
*ctx
)
1618 struct perf_event
*event
, *partial_group
= NULL
;
1619 struct pmu
*pmu
= group_event
->pmu
;
1620 u64 now
= ctx
->time
;
1621 bool simulate
= false;
1623 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
1626 pmu
->start_txn(pmu
);
1628 if (event_sched_in(group_event
, cpuctx
, ctx
)) {
1629 pmu
->cancel_txn(pmu
);
1634 * Schedule in siblings as one group (if any):
1636 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
1637 if (event_sched_in(event
, cpuctx
, ctx
)) {
1638 partial_group
= event
;
1643 if (!pmu
->commit_txn(pmu
))
1648 * Groups can be scheduled in as one unit only, so undo any
1649 * partial group before returning:
1650 * The events up to the failed event are scheduled out normally,
1651 * tstamp_stopped will be updated.
1653 * The failed events and the remaining siblings need to have
1654 * their timings updated as if they had gone thru event_sched_in()
1655 * and event_sched_out(). This is required to get consistent timings
1656 * across the group. This also takes care of the case where the group
1657 * could never be scheduled by ensuring tstamp_stopped is set to mark
1658 * the time the event was actually stopped, such that time delta
1659 * calculation in update_event_times() is correct.
1661 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
1662 if (event
== partial_group
)
1666 event
->tstamp_running
+= now
- event
->tstamp_stopped
;
1667 event
->tstamp_stopped
= now
;
1669 event_sched_out(event
, cpuctx
, ctx
);
1672 event_sched_out(group_event
, cpuctx
, ctx
);
1674 pmu
->cancel_txn(pmu
);
1680 * Work out whether we can put this event group on the CPU now.
1682 static int group_can_go_on(struct perf_event
*event
,
1683 struct perf_cpu_context
*cpuctx
,
1687 * Groups consisting entirely of software events can always go on.
1689 if (event
->group_flags
& PERF_GROUP_SOFTWARE
)
1692 * If an exclusive group is already on, no other hardware
1695 if (cpuctx
->exclusive
)
1698 * If this group is exclusive and there are already
1699 * events on the CPU, it can't go on.
1701 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
1704 * Otherwise, try to add it if all previous groups were able
1710 static void add_event_to_ctx(struct perf_event
*event
,
1711 struct perf_event_context
*ctx
)
1713 u64 tstamp
= perf_event_time(event
);
1715 list_add_event(event
, ctx
);
1716 perf_group_attach(event
);
1717 event
->tstamp_enabled
= tstamp
;
1718 event
->tstamp_running
= tstamp
;
1719 event
->tstamp_stopped
= tstamp
;
1722 static void task_ctx_sched_out(struct perf_event_context
*ctx
);
1724 ctx_sched_in(struct perf_event_context
*ctx
,
1725 struct perf_cpu_context
*cpuctx
,
1726 enum event_type_t event_type
,
1727 struct task_struct
*task
);
1729 static void perf_event_sched_in(struct perf_cpu_context
*cpuctx
,
1730 struct perf_event_context
*ctx
,
1731 struct task_struct
*task
)
1733 cpu_ctx_sched_in(cpuctx
, EVENT_PINNED
, task
);
1735 ctx_sched_in(ctx
, cpuctx
, EVENT_PINNED
, task
);
1736 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
, task
);
1738 ctx_sched_in(ctx
, cpuctx
, EVENT_FLEXIBLE
, task
);
1742 * Cross CPU call to install and enable a performance event
1744 * Must be called with ctx->mutex held
1746 static int __perf_install_in_context(void *info
)
1748 struct perf_event
*event
= info
;
1749 struct perf_event_context
*ctx
= event
->ctx
;
1750 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1751 struct perf_event_context
*task_ctx
= cpuctx
->task_ctx
;
1752 struct task_struct
*task
= current
;
1754 perf_ctx_lock(cpuctx
, task_ctx
);
1755 perf_pmu_disable(cpuctx
->ctx
.pmu
);
1758 * If there was an active task_ctx schedule it out.
1761 task_ctx_sched_out(task_ctx
);
1764 * If the context we're installing events in is not the
1765 * active task_ctx, flip them.
1767 if (ctx
->task
&& task_ctx
!= ctx
) {
1769 raw_spin_unlock(&task_ctx
->lock
);
1770 raw_spin_lock(&ctx
->lock
);
1775 cpuctx
->task_ctx
= task_ctx
;
1776 task
= task_ctx
->task
;
1779 cpu_ctx_sched_out(cpuctx
, EVENT_ALL
);
1781 update_context_time(ctx
);
1783 * update cgrp time only if current cgrp
1784 * matches event->cgrp. Must be done before
1785 * calling add_event_to_ctx()
1787 update_cgrp_time_from_event(event
);
1789 add_event_to_ctx(event
, ctx
);
1792 * Schedule everything back in
1794 perf_event_sched_in(cpuctx
, task_ctx
, task
);
1796 perf_pmu_enable(cpuctx
->ctx
.pmu
);
1797 perf_ctx_unlock(cpuctx
, task_ctx
);
1803 * Attach a performance event to a context
1805 * First we add the event to the list with the hardware enable bit
1806 * in event->hw_config cleared.
1808 * If the event is attached to a task which is on a CPU we use a smp
1809 * call to enable it in the task context. The task might have been
1810 * scheduled away, but we check this in the smp call again.
1813 perf_install_in_context(struct perf_event_context
*ctx
,
1814 struct perf_event
*event
,
1817 struct task_struct
*task
= ctx
->task
;
1819 lockdep_assert_held(&ctx
->mutex
);
1822 if (event
->cpu
!= -1)
1827 * Per cpu events are installed via an smp call and
1828 * the install is always successful.
1830 cpu_function_call(cpu
, __perf_install_in_context
, event
);
1835 if (!task_function_call(task
, __perf_install_in_context
, event
))
1838 raw_spin_lock_irq(&ctx
->lock
);
1840 * If we failed to find a running task, but find the context active now
1841 * that we've acquired the ctx->lock, retry.
1843 if (ctx
->is_active
) {
1844 raw_spin_unlock_irq(&ctx
->lock
);
1846 * Reload the task pointer, it might have been changed by
1847 * a concurrent perf_event_context_sched_out().
1854 * Since the task isn't running, its safe to add the event, us holding
1855 * the ctx->lock ensures the task won't get scheduled in.
1857 add_event_to_ctx(event
, ctx
);
1858 raw_spin_unlock_irq(&ctx
->lock
);
1862 * Put a event into inactive state and update time fields.
1863 * Enabling the leader of a group effectively enables all
1864 * the group members that aren't explicitly disabled, so we
1865 * have to update their ->tstamp_enabled also.
1866 * Note: this works for group members as well as group leaders
1867 * since the non-leader members' sibling_lists will be empty.
1869 static void __perf_event_mark_enabled(struct perf_event
*event
)
1871 struct perf_event
*sub
;
1872 u64 tstamp
= perf_event_time(event
);
1874 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1875 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
1876 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
1877 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
)
1878 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
1883 * Cross CPU call to enable a performance event
1885 static int __perf_event_enable(void *info
)
1887 struct perf_event
*event
= info
;
1888 struct perf_event_context
*ctx
= event
->ctx
;
1889 struct perf_event
*leader
= event
->group_leader
;
1890 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1894 * There's a time window between 'ctx->is_active' check
1895 * in perf_event_enable function and this place having:
1897 * - ctx->lock unlocked
1899 * where the task could be killed and 'ctx' deactivated
1900 * by perf_event_exit_task.
1902 if (!ctx
->is_active
)
1905 raw_spin_lock(&ctx
->lock
);
1906 update_context_time(ctx
);
1908 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1912 * set current task's cgroup time reference point
1914 perf_cgroup_set_timestamp(current
, ctx
);
1916 __perf_event_mark_enabled(event
);
1918 if (!event_filter_match(event
)) {
1919 if (is_cgroup_event(event
))
1920 perf_cgroup_defer_enabled(event
);
1925 * If the event is in a group and isn't the group leader,
1926 * then don't put it on unless the group is on.
1928 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
)
1931 if (!group_can_go_on(event
, cpuctx
, 1)) {
1934 if (event
== leader
)
1935 err
= group_sched_in(event
, cpuctx
, ctx
);
1937 err
= event_sched_in(event
, cpuctx
, ctx
);
1942 * If this event can't go on and it's part of a
1943 * group, then the whole group has to come off.
1945 if (leader
!= event
)
1946 group_sched_out(leader
, cpuctx
, ctx
);
1947 if (leader
->attr
.pinned
) {
1948 update_group_times(leader
);
1949 leader
->state
= PERF_EVENT_STATE_ERROR
;
1954 raw_spin_unlock(&ctx
->lock
);
1962 * If event->ctx is a cloned context, callers must make sure that
1963 * every task struct that event->ctx->task could possibly point to
1964 * remains valid. This condition is satisfied when called through
1965 * perf_event_for_each_child or perf_event_for_each as described
1966 * for perf_event_disable.
1968 void perf_event_enable(struct perf_event
*event
)
1970 struct perf_event_context
*ctx
= event
->ctx
;
1971 struct task_struct
*task
= ctx
->task
;
1975 * Enable the event on the cpu that it's on
1977 cpu_function_call(event
->cpu
, __perf_event_enable
, event
);
1981 raw_spin_lock_irq(&ctx
->lock
);
1982 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1986 * If the event is in error state, clear that first.
1987 * That way, if we see the event in error state below, we
1988 * know that it has gone back into error state, as distinct
1989 * from the task having been scheduled away before the
1990 * cross-call arrived.
1992 if (event
->state
== PERF_EVENT_STATE_ERROR
)
1993 event
->state
= PERF_EVENT_STATE_OFF
;
1996 if (!ctx
->is_active
) {
1997 __perf_event_mark_enabled(event
);
2001 raw_spin_unlock_irq(&ctx
->lock
);
2003 if (!task_function_call(task
, __perf_event_enable
, event
))
2006 raw_spin_lock_irq(&ctx
->lock
);
2009 * If the context is active and the event is still off,
2010 * we need to retry the cross-call.
2012 if (ctx
->is_active
&& event
->state
== PERF_EVENT_STATE_OFF
) {
2014 * task could have been flipped by a concurrent
2015 * perf_event_context_sched_out()
2022 raw_spin_unlock_irq(&ctx
->lock
);
2024 EXPORT_SYMBOL_GPL(perf_event_enable
);
2026 int perf_event_refresh(struct perf_event
*event
, int refresh
)
2029 * not supported on inherited events
2031 if (event
->attr
.inherit
|| !is_sampling_event(event
))
2034 atomic_add(refresh
, &event
->event_limit
);
2035 perf_event_enable(event
);
2039 EXPORT_SYMBOL_GPL(perf_event_refresh
);
2041 static void ctx_sched_out(struct perf_event_context
*ctx
,
2042 struct perf_cpu_context
*cpuctx
,
2043 enum event_type_t event_type
)
2045 struct perf_event
*event
;
2046 int is_active
= ctx
->is_active
;
2048 ctx
->is_active
&= ~event_type
;
2049 if (likely(!ctx
->nr_events
))
2052 update_context_time(ctx
);
2053 update_cgrp_time_from_cpuctx(cpuctx
);
2054 if (!ctx
->nr_active
)
2057 perf_pmu_disable(ctx
->pmu
);
2058 if ((is_active
& EVENT_PINNED
) && (event_type
& EVENT_PINNED
)) {
2059 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
)
2060 group_sched_out(event
, cpuctx
, ctx
);
2063 if ((is_active
& EVENT_FLEXIBLE
) && (event_type
& EVENT_FLEXIBLE
)) {
2064 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
)
2065 group_sched_out(event
, cpuctx
, ctx
);
2067 perf_pmu_enable(ctx
->pmu
);
2071 * Test whether two contexts are equivalent, i.e. whether they
2072 * have both been cloned from the same version of the same context
2073 * and they both have the same number of enabled events.
2074 * If the number of enabled events is the same, then the set
2075 * of enabled events should be the same, because these are both
2076 * inherited contexts, therefore we can't access individual events
2077 * in them directly with an fd; we can only enable/disable all
2078 * events via prctl, or enable/disable all events in a family
2079 * via ioctl, which will have the same effect on both contexts.
2081 static int context_equiv(struct perf_event_context
*ctx1
,
2082 struct perf_event_context
*ctx2
)
2084 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
2085 && ctx1
->parent_gen
== ctx2
->parent_gen
2086 && !ctx1
->pin_count
&& !ctx2
->pin_count
;
2089 static void __perf_event_sync_stat(struct perf_event
*event
,
2090 struct perf_event
*next_event
)
2094 if (!event
->attr
.inherit_stat
)
2098 * Update the event value, we cannot use perf_event_read()
2099 * because we're in the middle of a context switch and have IRQs
2100 * disabled, which upsets smp_call_function_single(), however
2101 * we know the event must be on the current CPU, therefore we
2102 * don't need to use it.
2104 switch (event
->state
) {
2105 case PERF_EVENT_STATE_ACTIVE
:
2106 event
->pmu
->read(event
);
2109 case PERF_EVENT_STATE_INACTIVE
:
2110 update_event_times(event
);
2118 * In order to keep per-task stats reliable we need to flip the event
2119 * values when we flip the contexts.
2121 value
= local64_read(&next_event
->count
);
2122 value
= local64_xchg(&event
->count
, value
);
2123 local64_set(&next_event
->count
, value
);
2125 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
2126 swap(event
->total_time_running
, next_event
->total_time_running
);
2129 * Since we swizzled the values, update the user visible data too.
2131 perf_event_update_userpage(event
);
2132 perf_event_update_userpage(next_event
);
2135 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
2136 struct perf_event_context
*next_ctx
)
2138 struct perf_event
*event
, *next_event
;
2143 update_context_time(ctx
);
2145 event
= list_first_entry(&ctx
->event_list
,
2146 struct perf_event
, event_entry
);
2148 next_event
= list_first_entry(&next_ctx
->event_list
,
2149 struct perf_event
, event_entry
);
2151 while (&event
->event_entry
!= &ctx
->event_list
&&
2152 &next_event
->event_entry
!= &next_ctx
->event_list
) {
2154 __perf_event_sync_stat(event
, next_event
);
2156 event
= list_next_entry(event
, event_entry
);
2157 next_event
= list_next_entry(next_event
, event_entry
);
2161 static void perf_event_context_sched_out(struct task_struct
*task
, int ctxn
,
2162 struct task_struct
*next
)
2164 struct perf_event_context
*ctx
= task
->perf_event_ctxp
[ctxn
];
2165 struct perf_event_context
*next_ctx
;
2166 struct perf_event_context
*parent
;
2167 struct perf_cpu_context
*cpuctx
;
2173 cpuctx
= __get_cpu_context(ctx
);
2174 if (!cpuctx
->task_ctx
)
2178 parent
= rcu_dereference(ctx
->parent_ctx
);
2179 next_ctx
= next
->perf_event_ctxp
[ctxn
];
2180 if (parent
&& next_ctx
&&
2181 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
2183 * Looks like the two contexts are clones, so we might be
2184 * able to optimize the context switch. We lock both
2185 * contexts and check that they are clones under the
2186 * lock (including re-checking that neither has been
2187 * uncloned in the meantime). It doesn't matter which
2188 * order we take the locks because no other cpu could
2189 * be trying to lock both of these tasks.
2191 raw_spin_lock(&ctx
->lock
);
2192 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
2193 if (context_equiv(ctx
, next_ctx
)) {
2195 * XXX do we need a memory barrier of sorts
2196 * wrt to rcu_dereference() of perf_event_ctxp
2198 task
->perf_event_ctxp
[ctxn
] = next_ctx
;
2199 next
->perf_event_ctxp
[ctxn
] = ctx
;
2201 next_ctx
->task
= task
;
2204 perf_event_sync_stat(ctx
, next_ctx
);
2206 raw_spin_unlock(&next_ctx
->lock
);
2207 raw_spin_unlock(&ctx
->lock
);
2212 raw_spin_lock(&ctx
->lock
);
2213 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
2214 cpuctx
->task_ctx
= NULL
;
2215 raw_spin_unlock(&ctx
->lock
);
2219 #define for_each_task_context_nr(ctxn) \
2220 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2223 * Called from scheduler to remove the events of the current task,
2224 * with interrupts disabled.
2226 * We stop each event and update the event value in event->count.
2228 * This does not protect us against NMI, but disable()
2229 * sets the disabled bit in the control field of event _before_
2230 * accessing the event control register. If a NMI hits, then it will
2231 * not restart the event.
2233 void __perf_event_task_sched_out(struct task_struct
*task
,
2234 struct task_struct
*next
)
2238 for_each_task_context_nr(ctxn
)
2239 perf_event_context_sched_out(task
, ctxn
, next
);
2242 * if cgroup events exist on this CPU, then we need
2243 * to check if we have to switch out PMU state.
2244 * cgroup event are system-wide mode only
2246 if (atomic_read(&__get_cpu_var(perf_cgroup_events
)))
2247 perf_cgroup_sched_out(task
, next
);
2250 static void task_ctx_sched_out(struct perf_event_context
*ctx
)
2252 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
2254 if (!cpuctx
->task_ctx
)
2257 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
2260 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
2261 cpuctx
->task_ctx
= NULL
;
2265 * Called with IRQs disabled
2267 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
2268 enum event_type_t event_type
)
2270 ctx_sched_out(&cpuctx
->ctx
, cpuctx
, event_type
);
2274 ctx_pinned_sched_in(struct perf_event_context
*ctx
,
2275 struct perf_cpu_context
*cpuctx
)
2277 struct perf_event
*event
;
2279 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
2280 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2282 if (!event_filter_match(event
))
2285 /* may need to reset tstamp_enabled */
2286 if (is_cgroup_event(event
))
2287 perf_cgroup_mark_enabled(event
, ctx
);
2289 if (group_can_go_on(event
, cpuctx
, 1))
2290 group_sched_in(event
, cpuctx
, ctx
);
2293 * If this pinned group hasn't been scheduled,
2294 * put it in error state.
2296 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
2297 update_group_times(event
);
2298 event
->state
= PERF_EVENT_STATE_ERROR
;
2304 ctx_flexible_sched_in(struct perf_event_context
*ctx
,
2305 struct perf_cpu_context
*cpuctx
)
2307 struct perf_event
*event
;
2310 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
2311 /* Ignore events in OFF or ERROR state */
2312 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2315 * Listen to the 'cpu' scheduling filter constraint
2318 if (!event_filter_match(event
))
2321 /* may need to reset tstamp_enabled */
2322 if (is_cgroup_event(event
))
2323 perf_cgroup_mark_enabled(event
, ctx
);
2325 if (group_can_go_on(event
, cpuctx
, can_add_hw
)) {
2326 if (group_sched_in(event
, cpuctx
, ctx
))
2333 ctx_sched_in(struct perf_event_context
*ctx
,
2334 struct perf_cpu_context
*cpuctx
,
2335 enum event_type_t event_type
,
2336 struct task_struct
*task
)
2339 int is_active
= ctx
->is_active
;
2341 ctx
->is_active
|= event_type
;
2342 if (likely(!ctx
->nr_events
))
2346 ctx
->timestamp
= now
;
2347 perf_cgroup_set_timestamp(task
, ctx
);
2349 * First go through the list and put on any pinned groups
2350 * in order to give them the best chance of going on.
2352 if (!(is_active
& EVENT_PINNED
) && (event_type
& EVENT_PINNED
))
2353 ctx_pinned_sched_in(ctx
, cpuctx
);
2355 /* Then walk through the lower prio flexible groups */
2356 if (!(is_active
& EVENT_FLEXIBLE
) && (event_type
& EVENT_FLEXIBLE
))
2357 ctx_flexible_sched_in(ctx
, cpuctx
);
2360 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
2361 enum event_type_t event_type
,
2362 struct task_struct
*task
)
2364 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
2366 ctx_sched_in(ctx
, cpuctx
, event_type
, task
);
2369 static void perf_event_context_sched_in(struct perf_event_context
*ctx
,
2370 struct task_struct
*task
)
2372 struct perf_cpu_context
*cpuctx
;
2374 cpuctx
= __get_cpu_context(ctx
);
2375 if (cpuctx
->task_ctx
== ctx
)
2378 perf_ctx_lock(cpuctx
, ctx
);
2379 perf_pmu_disable(ctx
->pmu
);
2381 * We want to keep the following priority order:
2382 * cpu pinned (that don't need to move), task pinned,
2383 * cpu flexible, task flexible.
2385 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
2388 cpuctx
->task_ctx
= ctx
;
2390 perf_event_sched_in(cpuctx
, cpuctx
->task_ctx
, task
);
2392 perf_pmu_enable(ctx
->pmu
);
2393 perf_ctx_unlock(cpuctx
, ctx
);
2396 * Since these rotations are per-cpu, we need to ensure the
2397 * cpu-context we got scheduled on is actually rotating.
2399 perf_pmu_rotate_start(ctx
->pmu
);
2403 * When sampling the branck stack in system-wide, it may be necessary
2404 * to flush the stack on context switch. This happens when the branch
2405 * stack does not tag its entries with the pid of the current task.
2406 * Otherwise it becomes impossible to associate a branch entry with a
2407 * task. This ambiguity is more likely to appear when the branch stack
2408 * supports priv level filtering and the user sets it to monitor only
2409 * at the user level (which could be a useful measurement in system-wide
2410 * mode). In that case, the risk is high of having a branch stack with
2411 * branch from multiple tasks. Flushing may mean dropping the existing
2412 * entries or stashing them somewhere in the PMU specific code layer.
2414 * This function provides the context switch callback to the lower code
2415 * layer. It is invoked ONLY when there is at least one system-wide context
2416 * with at least one active event using taken branch sampling.
2418 static void perf_branch_stack_sched_in(struct task_struct
*prev
,
2419 struct task_struct
*task
)
2421 struct perf_cpu_context
*cpuctx
;
2423 unsigned long flags
;
2425 /* no need to flush branch stack if not changing task */
2429 local_irq_save(flags
);
2433 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
2434 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
2437 * check if the context has at least one
2438 * event using PERF_SAMPLE_BRANCH_STACK
2440 if (cpuctx
->ctx
.nr_branch_stack
> 0
2441 && pmu
->flush_branch_stack
) {
2443 pmu
= cpuctx
->ctx
.pmu
;
2445 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
2447 perf_pmu_disable(pmu
);
2449 pmu
->flush_branch_stack();
2451 perf_pmu_enable(pmu
);
2453 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
2459 local_irq_restore(flags
);
2463 * Called from scheduler to add the events of the current task
2464 * with interrupts disabled.
2466 * We restore the event value and then enable it.
2468 * This does not protect us against NMI, but enable()
2469 * sets the enabled bit in the control field of event _before_
2470 * accessing the event control register. If a NMI hits, then it will
2471 * keep the event running.
2473 void __perf_event_task_sched_in(struct task_struct
*prev
,
2474 struct task_struct
*task
)
2476 struct perf_event_context
*ctx
;
2479 for_each_task_context_nr(ctxn
) {
2480 ctx
= task
->perf_event_ctxp
[ctxn
];
2484 perf_event_context_sched_in(ctx
, task
);
2487 * if cgroup events exist on this CPU, then we need
2488 * to check if we have to switch in PMU state.
2489 * cgroup event are system-wide mode only
2491 if (atomic_read(&__get_cpu_var(perf_cgroup_events
)))
2492 perf_cgroup_sched_in(prev
, task
);
2494 /* check for system-wide branch_stack events */
2495 if (atomic_read(&__get_cpu_var(perf_branch_stack_events
)))
2496 perf_branch_stack_sched_in(prev
, task
);
2499 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
2501 u64 frequency
= event
->attr
.sample_freq
;
2502 u64 sec
= NSEC_PER_SEC
;
2503 u64 divisor
, dividend
;
2505 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
2507 count_fls
= fls64(count
);
2508 nsec_fls
= fls64(nsec
);
2509 frequency_fls
= fls64(frequency
);
2513 * We got @count in @nsec, with a target of sample_freq HZ
2514 * the target period becomes:
2517 * period = -------------------
2518 * @nsec * sample_freq
2523 * Reduce accuracy by one bit such that @a and @b converge
2524 * to a similar magnitude.
2526 #define REDUCE_FLS(a, b) \
2528 if (a##_fls > b##_fls) { \
2538 * Reduce accuracy until either term fits in a u64, then proceed with
2539 * the other, so that finally we can do a u64/u64 division.
2541 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
2542 REDUCE_FLS(nsec
, frequency
);
2543 REDUCE_FLS(sec
, count
);
2546 if (count_fls
+ sec_fls
> 64) {
2547 divisor
= nsec
* frequency
;
2549 while (count_fls
+ sec_fls
> 64) {
2550 REDUCE_FLS(count
, sec
);
2554 dividend
= count
* sec
;
2556 dividend
= count
* sec
;
2558 while (nsec_fls
+ frequency_fls
> 64) {
2559 REDUCE_FLS(nsec
, frequency
);
2563 divisor
= nsec
* frequency
;
2569 return div64_u64(dividend
, divisor
);
2572 static DEFINE_PER_CPU(int, perf_throttled_count
);
2573 static DEFINE_PER_CPU(u64
, perf_throttled_seq
);
2575 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
, bool disable
)
2577 struct hw_perf_event
*hwc
= &event
->hw
;
2578 s64 period
, sample_period
;
2581 period
= perf_calculate_period(event
, nsec
, count
);
2583 delta
= (s64
)(period
- hwc
->sample_period
);
2584 delta
= (delta
+ 7) / 8; /* low pass filter */
2586 sample_period
= hwc
->sample_period
+ delta
;
2591 hwc
->sample_period
= sample_period
;
2593 if (local64_read(&hwc
->period_left
) > 8*sample_period
) {
2595 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
2597 local64_set(&hwc
->period_left
, 0);
2600 event
->pmu
->start(event
, PERF_EF_RELOAD
);
2605 * combine freq adjustment with unthrottling to avoid two passes over the
2606 * events. At the same time, make sure, having freq events does not change
2607 * the rate of unthrottling as that would introduce bias.
2609 static void perf_adjust_freq_unthr_context(struct perf_event_context
*ctx
,
2612 struct perf_event
*event
;
2613 struct hw_perf_event
*hwc
;
2614 u64 now
, period
= TICK_NSEC
;
2618 * only need to iterate over all events iff:
2619 * - context have events in frequency mode (needs freq adjust)
2620 * - there are events to unthrottle on this cpu
2622 if (!(ctx
->nr_freq
|| needs_unthr
))
2625 raw_spin_lock(&ctx
->lock
);
2626 perf_pmu_disable(ctx
->pmu
);
2628 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
2629 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
2632 if (!event_filter_match(event
))
2637 if (needs_unthr
&& hwc
->interrupts
== MAX_INTERRUPTS
) {
2638 hwc
->interrupts
= 0;
2639 perf_log_throttle(event
, 1);
2640 event
->pmu
->start(event
, 0);
2643 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
2647 * stop the event and update event->count
2649 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
2651 now
= local64_read(&event
->count
);
2652 delta
= now
- hwc
->freq_count_stamp
;
2653 hwc
->freq_count_stamp
= now
;
2657 * reload only if value has changed
2658 * we have stopped the event so tell that
2659 * to perf_adjust_period() to avoid stopping it
2663 perf_adjust_period(event
, period
, delta
, false);
2665 event
->pmu
->start(event
, delta
> 0 ? PERF_EF_RELOAD
: 0);
2668 perf_pmu_enable(ctx
->pmu
);
2669 raw_spin_unlock(&ctx
->lock
);
2673 * Round-robin a context's events:
2675 static void rotate_ctx(struct perf_event_context
*ctx
)
2678 * Rotate the first entry last of non-pinned groups. Rotation might be
2679 * disabled by the inheritance code.
2681 if (!ctx
->rotate_disable
)
2682 list_rotate_left(&ctx
->flexible_groups
);
2686 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2687 * because they're strictly cpu affine and rotate_start is called with IRQs
2688 * disabled, while rotate_context is called from IRQ context.
2690 static void perf_rotate_context(struct perf_cpu_context
*cpuctx
)
2692 struct perf_event_context
*ctx
= NULL
;
2693 int rotate
= 0, remove
= 1;
2695 if (cpuctx
->ctx
.nr_events
) {
2697 if (cpuctx
->ctx
.nr_events
!= cpuctx
->ctx
.nr_active
)
2701 ctx
= cpuctx
->task_ctx
;
2702 if (ctx
&& ctx
->nr_events
) {
2704 if (ctx
->nr_events
!= ctx
->nr_active
)
2711 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
2712 perf_pmu_disable(cpuctx
->ctx
.pmu
);
2714 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
2716 ctx_sched_out(ctx
, cpuctx
, EVENT_FLEXIBLE
);
2718 rotate_ctx(&cpuctx
->ctx
);
2722 perf_event_sched_in(cpuctx
, ctx
, current
);
2724 perf_pmu_enable(cpuctx
->ctx
.pmu
);
2725 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
2728 list_del_init(&cpuctx
->rotation_list
);
2731 #ifdef CONFIG_NO_HZ_FULL
2732 bool perf_event_can_stop_tick(void)
2734 if (list_empty(&__get_cpu_var(rotation_list
)))
2741 void perf_event_task_tick(void)
2743 struct list_head
*head
= &__get_cpu_var(rotation_list
);
2744 struct perf_cpu_context
*cpuctx
, *tmp
;
2745 struct perf_event_context
*ctx
;
2748 WARN_ON(!irqs_disabled());
2750 __this_cpu_inc(perf_throttled_seq
);
2751 throttled
= __this_cpu_xchg(perf_throttled_count
, 0);
2753 list_for_each_entry_safe(cpuctx
, tmp
, head
, rotation_list
) {
2755 perf_adjust_freq_unthr_context(ctx
, throttled
);
2757 ctx
= cpuctx
->task_ctx
;
2759 perf_adjust_freq_unthr_context(ctx
, throttled
);
2761 if (cpuctx
->jiffies_interval
== 1 ||
2762 !(jiffies
% cpuctx
->jiffies_interval
))
2763 perf_rotate_context(cpuctx
);
2767 static int event_enable_on_exec(struct perf_event
*event
,
2768 struct perf_event_context
*ctx
)
2770 if (!event
->attr
.enable_on_exec
)
2773 event
->attr
.enable_on_exec
= 0;
2774 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
2777 __perf_event_mark_enabled(event
);
2783 * Enable all of a task's events that have been marked enable-on-exec.
2784 * This expects task == current.
2786 static void perf_event_enable_on_exec(struct perf_event_context
*ctx
)
2788 struct perf_event
*event
;
2789 unsigned long flags
;
2793 local_irq_save(flags
);
2794 if (!ctx
|| !ctx
->nr_events
)
2798 * We must ctxsw out cgroup events to avoid conflict
2799 * when invoking perf_task_event_sched_in() later on
2800 * in this function. Otherwise we end up trying to
2801 * ctxswin cgroup events which are already scheduled
2804 perf_cgroup_sched_out(current
, NULL
);
2806 raw_spin_lock(&ctx
->lock
);
2807 task_ctx_sched_out(ctx
);
2809 list_for_each_entry(event
, &ctx
->event_list
, event_entry
) {
2810 ret
= event_enable_on_exec(event
, ctx
);
2816 * Unclone this context if we enabled any event.
2821 raw_spin_unlock(&ctx
->lock
);
2824 * Also calls ctxswin for cgroup events, if any:
2826 perf_event_context_sched_in(ctx
, ctx
->task
);
2828 local_irq_restore(flags
);
2832 * Cross CPU call to read the hardware event
2834 static void __perf_event_read(void *info
)
2836 struct perf_event
*event
= info
;
2837 struct perf_event_context
*ctx
= event
->ctx
;
2838 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
2841 * If this is a task context, we need to check whether it is
2842 * the current task context of this cpu. If not it has been
2843 * scheduled out before the smp call arrived. In that case
2844 * event->count would have been updated to a recent sample
2845 * when the event was scheduled out.
2847 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
2850 raw_spin_lock(&ctx
->lock
);
2851 if (ctx
->is_active
) {
2852 update_context_time(ctx
);
2853 update_cgrp_time_from_event(event
);
2855 update_event_times(event
);
2856 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
2857 event
->pmu
->read(event
);
2858 raw_spin_unlock(&ctx
->lock
);
2861 static inline u64
perf_event_count(struct perf_event
*event
)
2863 return local64_read(&event
->count
) + atomic64_read(&event
->child_count
);
2866 static u64
perf_event_read(struct perf_event
*event
)
2869 * If event is enabled and currently active on a CPU, update the
2870 * value in the event structure:
2872 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
2873 smp_call_function_single(event
->oncpu
,
2874 __perf_event_read
, event
, 1);
2875 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
2876 struct perf_event_context
*ctx
= event
->ctx
;
2877 unsigned long flags
;
2879 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
2881 * may read while context is not active
2882 * (e.g., thread is blocked), in that case
2883 * we cannot update context time
2885 if (ctx
->is_active
) {
2886 update_context_time(ctx
);
2887 update_cgrp_time_from_event(event
);
2889 update_event_times(event
);
2890 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
2893 return perf_event_count(event
);
2897 * Initialize the perf_event context in a task_struct:
2899 static void __perf_event_init_context(struct perf_event_context
*ctx
)
2901 raw_spin_lock_init(&ctx
->lock
);
2902 mutex_init(&ctx
->mutex
);
2903 INIT_LIST_HEAD(&ctx
->pinned_groups
);
2904 INIT_LIST_HEAD(&ctx
->flexible_groups
);
2905 INIT_LIST_HEAD(&ctx
->event_list
);
2906 atomic_set(&ctx
->refcount
, 1);
2909 static struct perf_event_context
*
2910 alloc_perf_context(struct pmu
*pmu
, struct task_struct
*task
)
2912 struct perf_event_context
*ctx
;
2914 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
2918 __perf_event_init_context(ctx
);
2921 get_task_struct(task
);
2928 static struct task_struct
*
2929 find_lively_task_by_vpid(pid_t vpid
)
2931 struct task_struct
*task
;
2938 task
= find_task_by_vpid(vpid
);
2940 get_task_struct(task
);
2944 return ERR_PTR(-ESRCH
);
2946 /* Reuse ptrace permission checks for now. */
2948 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
2953 put_task_struct(task
);
2954 return ERR_PTR(err
);
2959 * Returns a matching context with refcount and pincount.
2961 static struct perf_event_context
*
2962 find_get_context(struct pmu
*pmu
, struct task_struct
*task
, int cpu
)
2964 struct perf_event_context
*ctx
;
2965 struct perf_cpu_context
*cpuctx
;
2966 unsigned long flags
;
2970 /* Must be root to operate on a CPU event: */
2971 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
2972 return ERR_PTR(-EACCES
);
2975 * We could be clever and allow to attach a event to an
2976 * offline CPU and activate it when the CPU comes up, but
2979 if (!cpu_online(cpu
))
2980 return ERR_PTR(-ENODEV
);
2982 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
2991 ctxn
= pmu
->task_ctx_nr
;
2996 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
3000 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
3002 ctx
= alloc_perf_context(pmu
, task
);
3008 mutex_lock(&task
->perf_event_mutex
);
3010 * If it has already passed perf_event_exit_task().
3011 * we must see PF_EXITING, it takes this mutex too.
3013 if (task
->flags
& PF_EXITING
)
3015 else if (task
->perf_event_ctxp
[ctxn
])
3020 rcu_assign_pointer(task
->perf_event_ctxp
[ctxn
], ctx
);
3022 mutex_unlock(&task
->perf_event_mutex
);
3024 if (unlikely(err
)) {
3036 return ERR_PTR(err
);
3039 static void perf_event_free_filter(struct perf_event
*event
);
3041 static void free_event_rcu(struct rcu_head
*head
)
3043 struct perf_event
*event
;
3045 event
= container_of(head
, struct perf_event
, rcu_head
);
3047 put_pid_ns(event
->ns
);
3048 perf_event_free_filter(event
);
3052 static void ring_buffer_put(struct ring_buffer
*rb
);
3053 static void ring_buffer_detach(struct perf_event
*event
, struct ring_buffer
*rb
);
3055 static void free_event(struct perf_event
*event
)
3057 irq_work_sync(&event
->pending
);
3059 if (!event
->parent
) {
3060 if (event
->attach_state
& PERF_ATTACH_TASK
)
3061 static_key_slow_dec_deferred(&perf_sched_events
);
3062 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
3063 atomic_dec(&nr_mmap_events
);
3064 if (event
->attr
.comm
)
3065 atomic_dec(&nr_comm_events
);
3066 if (event
->attr
.task
)
3067 atomic_dec(&nr_task_events
);
3068 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
)
3069 put_callchain_buffers();
3070 if (is_cgroup_event(event
)) {
3071 atomic_dec(&per_cpu(perf_cgroup_events
, event
->cpu
));
3072 static_key_slow_dec_deferred(&perf_sched_events
);
3075 if (has_branch_stack(event
)) {
3076 static_key_slow_dec_deferred(&perf_sched_events
);
3077 /* is system-wide event */
3078 if (!(event
->attach_state
& PERF_ATTACH_TASK
)) {
3079 atomic_dec(&per_cpu(perf_branch_stack_events
,
3086 struct ring_buffer
*rb
;
3089 * Can happen when we close an event with re-directed output.
3091 * Since we have a 0 refcount, perf_mmap_close() will skip
3092 * over us; possibly making our ring_buffer_put() the last.
3094 mutex_lock(&event
->mmap_mutex
);
3097 rcu_assign_pointer(event
->rb
, NULL
);
3098 ring_buffer_detach(event
, rb
);
3099 ring_buffer_put(rb
); /* could be last */
3101 mutex_unlock(&event
->mmap_mutex
);
3104 if (is_cgroup_event(event
))
3105 perf_detach_cgroup(event
);
3108 event
->destroy(event
);
3111 put_ctx(event
->ctx
);
3113 call_rcu(&event
->rcu_head
, free_event_rcu
);
3116 int perf_event_release_kernel(struct perf_event
*event
)
3118 struct perf_event_context
*ctx
= event
->ctx
;
3120 WARN_ON_ONCE(ctx
->parent_ctx
);
3122 * There are two ways this annotation is useful:
3124 * 1) there is a lock recursion from perf_event_exit_task
3125 * see the comment there.
3127 * 2) there is a lock-inversion with mmap_sem through
3128 * perf_event_read_group(), which takes faults while
3129 * holding ctx->mutex, however this is called after
3130 * the last filedesc died, so there is no possibility
3131 * to trigger the AB-BA case.
3133 mutex_lock_nested(&ctx
->mutex
, SINGLE_DEPTH_NESTING
);
3134 perf_remove_from_context(event
, true);
3135 mutex_unlock(&ctx
->mutex
);
3141 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
3144 * Called when the last reference to the file is gone.
3146 static void put_event(struct perf_event
*event
)
3148 struct task_struct
*owner
;
3150 if (!atomic_long_dec_and_test(&event
->refcount
))
3154 owner
= ACCESS_ONCE(event
->owner
);
3156 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3157 * !owner it means the list deletion is complete and we can indeed
3158 * free this event, otherwise we need to serialize on
3159 * owner->perf_event_mutex.
3161 smp_read_barrier_depends();
3164 * Since delayed_put_task_struct() also drops the last
3165 * task reference we can safely take a new reference
3166 * while holding the rcu_read_lock().
3168 get_task_struct(owner
);
3173 mutex_lock(&owner
->perf_event_mutex
);
3175 * We have to re-check the event->owner field, if it is cleared
3176 * we raced with perf_event_exit_task(), acquiring the mutex
3177 * ensured they're done, and we can proceed with freeing the
3181 list_del_init(&event
->owner_entry
);
3182 mutex_unlock(&owner
->perf_event_mutex
);
3183 put_task_struct(owner
);
3186 perf_event_release_kernel(event
);
3189 static int perf_release(struct inode
*inode
, struct file
*file
)
3191 put_event(file
->private_data
);
3195 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
3197 struct perf_event
*child
;
3203 mutex_lock(&event
->child_mutex
);
3204 total
+= perf_event_read(event
);
3205 *enabled
+= event
->total_time_enabled
+
3206 atomic64_read(&event
->child_total_time_enabled
);
3207 *running
+= event
->total_time_running
+
3208 atomic64_read(&event
->child_total_time_running
);
3210 list_for_each_entry(child
, &event
->child_list
, child_list
) {
3211 total
+= perf_event_read(child
);
3212 *enabled
+= child
->total_time_enabled
;
3213 *running
+= child
->total_time_running
;
3215 mutex_unlock(&event
->child_mutex
);
3219 EXPORT_SYMBOL_GPL(perf_event_read_value
);
3221 static int perf_event_read_group(struct perf_event
*event
,
3222 u64 read_format
, char __user
*buf
)
3224 struct perf_event
*leader
= event
->group_leader
, *sub
;
3225 int n
= 0, size
= 0, ret
= -EFAULT
;
3226 struct perf_event_context
*ctx
= leader
->ctx
;
3228 u64 count
, enabled
, running
;
3230 mutex_lock(&ctx
->mutex
);
3231 count
= perf_event_read_value(leader
, &enabled
, &running
);
3233 values
[n
++] = 1 + leader
->nr_siblings
;
3234 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3235 values
[n
++] = enabled
;
3236 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3237 values
[n
++] = running
;
3238 values
[n
++] = count
;
3239 if (read_format
& PERF_FORMAT_ID
)
3240 values
[n
++] = primary_event_id(leader
);
3242 size
= n
* sizeof(u64
);
3244 if (copy_to_user(buf
, values
, size
))
3249 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3252 values
[n
++] = perf_event_read_value(sub
, &enabled
, &running
);
3253 if (read_format
& PERF_FORMAT_ID
)
3254 values
[n
++] = primary_event_id(sub
);
3256 size
= n
* sizeof(u64
);
3258 if (copy_to_user(buf
+ ret
, values
, size
)) {
3266 mutex_unlock(&ctx
->mutex
);
3271 static int perf_event_read_one(struct perf_event
*event
,
3272 u64 read_format
, char __user
*buf
)
3274 u64 enabled
, running
;
3278 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
3279 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3280 values
[n
++] = enabled
;
3281 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3282 values
[n
++] = running
;
3283 if (read_format
& PERF_FORMAT_ID
)
3284 values
[n
++] = primary_event_id(event
);
3286 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
3289 return n
* sizeof(u64
);
3293 * Read the performance event - simple non blocking version for now
3296 perf_read_hw(struct perf_event
*event
, char __user
*buf
, size_t count
)
3298 u64 read_format
= event
->attr
.read_format
;
3302 * Return end-of-file for a read on a event that is in
3303 * error state (i.e. because it was pinned but it couldn't be
3304 * scheduled on to the CPU at some point).
3306 if (event
->state
== PERF_EVENT_STATE_ERROR
)
3309 if (count
< event
->read_size
)
3312 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3313 if (read_format
& PERF_FORMAT_GROUP
)
3314 ret
= perf_event_read_group(event
, read_format
, buf
);
3316 ret
= perf_event_read_one(event
, read_format
, buf
);
3322 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
3324 struct perf_event
*event
= file
->private_data
;
3326 return perf_read_hw(event
, buf
, count
);
3329 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
3331 struct perf_event
*event
= file
->private_data
;
3332 struct ring_buffer
*rb
;
3333 unsigned int events
= POLL_HUP
;
3336 * Pin the event->rb by taking event->mmap_mutex; otherwise
3337 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
3339 mutex_lock(&event
->mmap_mutex
);
3342 events
= atomic_xchg(&rb
->poll
, 0);
3343 mutex_unlock(&event
->mmap_mutex
);
3345 poll_wait(file
, &event
->waitq
, wait
);
3350 static void perf_event_reset(struct perf_event
*event
)
3352 (void)perf_event_read(event
);
3353 local64_set(&event
->count
, 0);
3354 perf_event_update_userpage(event
);
3358 * Holding the top-level event's child_mutex means that any
3359 * descendant process that has inherited this event will block
3360 * in sync_child_event if it goes to exit, thus satisfying the
3361 * task existence requirements of perf_event_enable/disable.
3363 static void perf_event_for_each_child(struct perf_event
*event
,
3364 void (*func
)(struct perf_event
*))
3366 struct perf_event
*child
;
3368 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3369 mutex_lock(&event
->child_mutex
);
3371 list_for_each_entry(child
, &event
->child_list
, child_list
)
3373 mutex_unlock(&event
->child_mutex
);
3376 static void perf_event_for_each(struct perf_event
*event
,
3377 void (*func
)(struct perf_event
*))
3379 struct perf_event_context
*ctx
= event
->ctx
;
3380 struct perf_event
*sibling
;
3382 WARN_ON_ONCE(ctx
->parent_ctx
);
3383 mutex_lock(&ctx
->mutex
);
3384 event
= event
->group_leader
;
3386 perf_event_for_each_child(event
, func
);
3387 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
3388 perf_event_for_each_child(sibling
, func
);
3389 mutex_unlock(&ctx
->mutex
);
3392 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
3394 struct perf_event_context
*ctx
= event
->ctx
;
3398 if (!is_sampling_event(event
))
3401 if (copy_from_user(&value
, arg
, sizeof(value
)))
3407 raw_spin_lock_irq(&ctx
->lock
);
3408 if (event
->attr
.freq
) {
3409 if (value
> sysctl_perf_event_sample_rate
) {
3414 event
->attr
.sample_freq
= value
;
3416 event
->attr
.sample_period
= value
;
3417 event
->hw
.sample_period
= value
;
3420 raw_spin_unlock_irq(&ctx
->lock
);
3425 static const struct file_operations perf_fops
;
3427 static inline int perf_fget_light(int fd
, struct fd
*p
)
3429 struct fd f
= fdget(fd
);
3433 if (f
.file
->f_op
!= &perf_fops
) {
3441 static int perf_event_set_output(struct perf_event
*event
,
3442 struct perf_event
*output_event
);
3443 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
3445 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
3447 struct perf_event
*event
= file
->private_data
;
3448 void (*func
)(struct perf_event
*);
3452 case PERF_EVENT_IOC_ENABLE
:
3453 func
= perf_event_enable
;
3455 case PERF_EVENT_IOC_DISABLE
:
3456 func
= perf_event_disable
;
3458 case PERF_EVENT_IOC_RESET
:
3459 func
= perf_event_reset
;
3462 case PERF_EVENT_IOC_REFRESH
:
3463 return perf_event_refresh(event
, arg
);
3465 case PERF_EVENT_IOC_PERIOD
:
3466 return perf_event_period(event
, (u64 __user
*)arg
);
3468 case PERF_EVENT_IOC_SET_OUTPUT
:
3472 struct perf_event
*output_event
;
3474 ret
= perf_fget_light(arg
, &output
);
3477 output_event
= output
.file
->private_data
;
3478 ret
= perf_event_set_output(event
, output_event
);
3481 ret
= perf_event_set_output(event
, NULL
);
3486 case PERF_EVENT_IOC_SET_FILTER
:
3487 return perf_event_set_filter(event
, (void __user
*)arg
);
3493 if (flags
& PERF_IOC_FLAG_GROUP
)
3494 perf_event_for_each(event
, func
);
3496 perf_event_for_each_child(event
, func
);
3501 #ifdef CONFIG_COMPAT
3502 static long perf_compat_ioctl(struct file
*file
, unsigned int cmd
,
3505 switch (_IOC_NR(cmd
)) {
3506 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER
):
3507 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
3508 if (_IOC_SIZE(cmd
) == sizeof(compat_uptr_t
)) {
3509 cmd
&= ~IOCSIZE_MASK
;
3510 cmd
|= sizeof(void *) << IOCSIZE_SHIFT
;
3514 return perf_ioctl(file
, cmd
, arg
);
3517 # define perf_compat_ioctl NULL
3520 int perf_event_task_enable(void)
3522 struct perf_event
*event
;
3524 mutex_lock(¤t
->perf_event_mutex
);
3525 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
3526 perf_event_for_each_child(event
, perf_event_enable
);
3527 mutex_unlock(¤t
->perf_event_mutex
);
3532 int perf_event_task_disable(void)
3534 struct perf_event
*event
;
3536 mutex_lock(¤t
->perf_event_mutex
);
3537 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
3538 perf_event_for_each_child(event
, perf_event_disable
);
3539 mutex_unlock(¤t
->perf_event_mutex
);
3544 static int perf_event_index(struct perf_event
*event
)
3546 if (event
->hw
.state
& PERF_HES_STOPPED
)
3549 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
3552 return event
->pmu
->event_idx(event
);
3555 static void calc_timer_values(struct perf_event
*event
,
3562 *now
= perf_clock();
3563 ctx_time
= event
->shadow_ctx_time
+ *now
;
3564 *enabled
= ctx_time
- event
->tstamp_enabled
;
3565 *running
= ctx_time
- event
->tstamp_running
;
3568 void __weak
arch_perf_update_userpage(struct perf_event_mmap_page
*userpg
, u64 now
)
3573 * Callers need to ensure there can be no nesting of this function, otherwise
3574 * the seqlock logic goes bad. We can not serialize this because the arch
3575 * code calls this from NMI context.
3577 void perf_event_update_userpage(struct perf_event
*event
)
3579 struct perf_event_mmap_page
*userpg
;
3580 struct ring_buffer
*rb
;
3581 u64 enabled
, running
, now
;
3585 * compute total_time_enabled, total_time_running
3586 * based on snapshot values taken when the event
3587 * was last scheduled in.
3589 * we cannot simply called update_context_time()
3590 * because of locking issue as we can be called in
3593 calc_timer_values(event
, &now
, &enabled
, &running
);
3594 rb
= rcu_dereference(event
->rb
);
3598 userpg
= rb
->user_page
;
3601 * Disable preemption so as to not let the corresponding user-space
3602 * spin too long if we get preempted.
3607 userpg
->index
= perf_event_index(event
);
3608 userpg
->offset
= perf_event_count(event
);
3610 userpg
->offset
-= local64_read(&event
->hw
.prev_count
);
3612 userpg
->time_enabled
= enabled
+
3613 atomic64_read(&event
->child_total_time_enabled
);
3615 userpg
->time_running
= running
+
3616 atomic64_read(&event
->child_total_time_running
);
3618 arch_perf_update_userpage(userpg
, now
);
3627 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
3629 struct perf_event
*event
= vma
->vm_file
->private_data
;
3630 struct ring_buffer
*rb
;
3631 int ret
= VM_FAULT_SIGBUS
;
3633 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
3634 if (vmf
->pgoff
== 0)
3640 rb
= rcu_dereference(event
->rb
);
3644 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
3647 vmf
->page
= perf_mmap_to_page(rb
, vmf
->pgoff
);
3651 get_page(vmf
->page
);
3652 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
3653 vmf
->page
->index
= vmf
->pgoff
;
3662 static void ring_buffer_attach(struct perf_event
*event
,
3663 struct ring_buffer
*rb
)
3665 unsigned long flags
;
3667 if (!list_empty(&event
->rb_entry
))
3670 spin_lock_irqsave(&rb
->event_lock
, flags
);
3671 if (list_empty(&event
->rb_entry
))
3672 list_add(&event
->rb_entry
, &rb
->event_list
);
3673 spin_unlock_irqrestore(&rb
->event_lock
, flags
);
3676 static void ring_buffer_detach(struct perf_event
*event
, struct ring_buffer
*rb
)
3678 unsigned long flags
;
3680 if (list_empty(&event
->rb_entry
))
3683 spin_lock_irqsave(&rb
->event_lock
, flags
);
3684 list_del_init(&event
->rb_entry
);
3685 wake_up_all(&event
->waitq
);
3686 spin_unlock_irqrestore(&rb
->event_lock
, flags
);
3689 static void ring_buffer_wakeup(struct perf_event
*event
)
3691 struct ring_buffer
*rb
;
3694 rb
= rcu_dereference(event
->rb
);
3696 list_for_each_entry_rcu(event
, &rb
->event_list
, rb_entry
)
3697 wake_up_all(&event
->waitq
);
3702 static void rb_free_rcu(struct rcu_head
*rcu_head
)
3704 struct ring_buffer
*rb
;
3706 rb
= container_of(rcu_head
, struct ring_buffer
, rcu_head
);
3710 static struct ring_buffer
*ring_buffer_get(struct perf_event
*event
)
3712 struct ring_buffer
*rb
;
3715 rb
= rcu_dereference(event
->rb
);
3717 if (!atomic_inc_not_zero(&rb
->refcount
))
3725 static void ring_buffer_put(struct ring_buffer
*rb
)
3727 if (!atomic_dec_and_test(&rb
->refcount
))
3730 WARN_ON_ONCE(!list_empty(&rb
->event_list
));
3732 call_rcu(&rb
->rcu_head
, rb_free_rcu
);
3735 static void perf_mmap_open(struct vm_area_struct
*vma
)
3737 struct perf_event
*event
= vma
->vm_file
->private_data
;
3739 atomic_inc(&event
->mmap_count
);
3740 atomic_inc(&event
->rb
->mmap_count
);
3744 * A buffer can be mmap()ed multiple times; either directly through the same
3745 * event, or through other events by use of perf_event_set_output().
3747 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3748 * the buffer here, where we still have a VM context. This means we need
3749 * to detach all events redirecting to us.
3751 static void perf_mmap_close(struct vm_area_struct
*vma
)
3753 struct perf_event
*event
= vma
->vm_file
->private_data
;
3755 struct ring_buffer
*rb
= event
->rb
;
3756 struct user_struct
*mmap_user
= rb
->mmap_user
;
3757 int mmap_locked
= rb
->mmap_locked
;
3758 unsigned long size
= perf_data_size(rb
);
3760 atomic_dec(&rb
->mmap_count
);
3762 if (!atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
))
3765 /* Detach current event from the buffer. */
3766 rcu_assign_pointer(event
->rb
, NULL
);
3767 ring_buffer_detach(event
, rb
);
3768 mutex_unlock(&event
->mmap_mutex
);
3770 /* If there's still other mmap()s of this buffer, we're done. */
3771 if (atomic_read(&rb
->mmap_count
)) {
3772 ring_buffer_put(rb
); /* can't be last */
3777 * No other mmap()s, detach from all other events that might redirect
3778 * into the now unreachable buffer. Somewhat complicated by the
3779 * fact that rb::event_lock otherwise nests inside mmap_mutex.
3783 list_for_each_entry_rcu(event
, &rb
->event_list
, rb_entry
) {
3784 if (!atomic_long_inc_not_zero(&event
->refcount
)) {
3786 * This event is en-route to free_event() which will
3787 * detach it and remove it from the list.
3793 mutex_lock(&event
->mmap_mutex
);
3795 * Check we didn't race with perf_event_set_output() which can
3796 * swizzle the rb from under us while we were waiting to
3797 * acquire mmap_mutex.
3799 * If we find a different rb; ignore this event, a next
3800 * iteration will no longer find it on the list. We have to
3801 * still restart the iteration to make sure we're not now
3802 * iterating the wrong list.
3804 if (event
->rb
== rb
) {
3805 rcu_assign_pointer(event
->rb
, NULL
);
3806 ring_buffer_detach(event
, rb
);
3807 ring_buffer_put(rb
); /* can't be last, we still have one */
3809 mutex_unlock(&event
->mmap_mutex
);
3813 * Restart the iteration; either we're on the wrong list or
3814 * destroyed its integrity by doing a deletion.
3821 * It could be there's still a few 0-ref events on the list; they'll
3822 * get cleaned up by free_event() -- they'll also still have their
3823 * ref on the rb and will free it whenever they are done with it.
3825 * Aside from that, this buffer is 'fully' detached and unmapped,
3826 * undo the VM accounting.
3829 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &mmap_user
->locked_vm
);
3830 vma
->vm_mm
->pinned_vm
-= mmap_locked
;
3831 free_uid(mmap_user
);
3833 ring_buffer_put(rb
); /* could be last */
3836 static const struct vm_operations_struct perf_mmap_vmops
= {
3837 .open
= perf_mmap_open
,
3838 .close
= perf_mmap_close
,
3839 .fault
= perf_mmap_fault
,
3840 .page_mkwrite
= perf_mmap_fault
,
3843 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
3845 struct perf_event
*event
= file
->private_data
;
3846 unsigned long user_locked
, user_lock_limit
;
3847 struct user_struct
*user
= current_user();
3848 unsigned long locked
, lock_limit
;
3849 struct ring_buffer
*rb
;
3850 unsigned long vma_size
;
3851 unsigned long nr_pages
;
3852 long user_extra
, extra
;
3853 int ret
= 0, flags
= 0;
3856 * Don't allow mmap() of inherited per-task counters. This would
3857 * create a performance issue due to all children writing to the
3860 if (event
->cpu
== -1 && event
->attr
.inherit
)
3863 if (!(vma
->vm_flags
& VM_SHARED
))
3866 vma_size
= vma
->vm_end
- vma
->vm_start
;
3867 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
3870 * If we have rb pages ensure they're a power-of-two number, so we
3871 * can do bitmasks instead of modulo.
3873 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
3876 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
3879 if (vma
->vm_pgoff
!= 0)
3882 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3884 mutex_lock(&event
->mmap_mutex
);
3886 if (event
->rb
->nr_pages
!= nr_pages
) {
3891 if (!atomic_inc_not_zero(&event
->rb
->mmap_count
)) {
3893 * Raced against perf_mmap_close() through
3894 * perf_event_set_output(). Try again, hope for better
3897 mutex_unlock(&event
->mmap_mutex
);
3904 user_extra
= nr_pages
+ 1;
3905 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
3908 * Increase the limit linearly with more CPUs:
3910 user_lock_limit
*= num_online_cpus();
3912 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
3915 if (user_locked
> user_lock_limit
)
3916 extra
= user_locked
- user_lock_limit
;
3918 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
3919 lock_limit
>>= PAGE_SHIFT
;
3920 locked
= vma
->vm_mm
->pinned_vm
+ extra
;
3922 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
3923 !capable(CAP_IPC_LOCK
)) {
3930 if (vma
->vm_flags
& VM_WRITE
)
3931 flags
|= RING_BUFFER_WRITABLE
;
3933 rb
= rb_alloc(nr_pages
,
3934 event
->attr
.watermark
? event
->attr
.wakeup_watermark
: 0,
3942 atomic_set(&rb
->mmap_count
, 1);
3943 rb
->mmap_locked
= extra
;
3944 rb
->mmap_user
= get_current_user();
3946 atomic_long_add(user_extra
, &user
->locked_vm
);
3947 vma
->vm_mm
->pinned_vm
+= extra
;
3949 ring_buffer_attach(event
, rb
);
3950 rcu_assign_pointer(event
->rb
, rb
);
3952 perf_event_update_userpage(event
);
3956 atomic_inc(&event
->mmap_count
);
3957 mutex_unlock(&event
->mmap_mutex
);
3960 * Since pinned accounting is per vm we cannot allow fork() to copy our
3963 vma
->vm_flags
|= VM_DONTCOPY
| VM_DONTEXPAND
| VM_DONTDUMP
;
3964 vma
->vm_ops
= &perf_mmap_vmops
;
3969 static int perf_fasync(int fd
, struct file
*filp
, int on
)
3971 struct inode
*inode
= file_inode(filp
);
3972 struct perf_event
*event
= filp
->private_data
;
3975 mutex_lock(&inode
->i_mutex
);
3976 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
3977 mutex_unlock(&inode
->i_mutex
);
3985 static const struct file_operations perf_fops
= {
3986 .llseek
= no_llseek
,
3987 .release
= perf_release
,
3990 .unlocked_ioctl
= perf_ioctl
,
3991 .compat_ioctl
= perf_compat_ioctl
,
3993 .fasync
= perf_fasync
,
3999 * If there's data, ensure we set the poll() state and publish everything
4000 * to user-space before waking everybody up.
4003 void perf_event_wakeup(struct perf_event
*event
)
4005 ring_buffer_wakeup(event
);
4007 if (event
->pending_kill
) {
4008 kill_fasync(&event
->fasync
, SIGIO
, event
->pending_kill
);
4009 event
->pending_kill
= 0;
4013 static void perf_pending_event(struct irq_work
*entry
)
4015 struct perf_event
*event
= container_of(entry
,
4016 struct perf_event
, pending
);
4018 if (event
->pending_disable
) {
4019 event
->pending_disable
= 0;
4020 __perf_event_disable(event
);
4023 if (event
->pending_wakeup
) {
4024 event
->pending_wakeup
= 0;
4025 perf_event_wakeup(event
);
4030 * We assume there is only KVM supporting the callbacks.
4031 * Later on, we might change it to a list if there is
4032 * another virtualization implementation supporting the callbacks.
4034 struct perf_guest_info_callbacks
*perf_guest_cbs
;
4036 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
4038 perf_guest_cbs
= cbs
;
4041 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks
);
4043 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
4045 perf_guest_cbs
= NULL
;
4048 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks
);
4051 perf_output_sample_regs(struct perf_output_handle
*handle
,
4052 struct pt_regs
*regs
, u64 mask
)
4056 for_each_set_bit(bit
, (const unsigned long *) &mask
,
4057 sizeof(mask
) * BITS_PER_BYTE
) {
4060 val
= perf_reg_value(regs
, bit
);
4061 perf_output_put(handle
, val
);
4065 static void perf_sample_regs_user(struct perf_regs_user
*regs_user
,
4066 struct pt_regs
*regs
)
4068 if (!user_mode(regs
)) {
4070 regs
= task_pt_regs(current
);
4076 regs_user
->regs
= regs
;
4077 regs_user
->abi
= perf_reg_abi(current
);
4082 * Get remaining task size from user stack pointer.
4084 * It'd be better to take stack vma map and limit this more
4085 * precisly, but there's no way to get it safely under interrupt,
4086 * so using TASK_SIZE as limit.
4088 static u64
perf_ustack_task_size(struct pt_regs
*regs
)
4090 unsigned long addr
= perf_user_stack_pointer(regs
);
4092 if (!addr
|| addr
>= TASK_SIZE
)
4095 return TASK_SIZE
- addr
;
4099 perf_sample_ustack_size(u16 stack_size
, u16 header_size
,
4100 struct pt_regs
*regs
)
4104 /* No regs, no stack pointer, no dump. */
4109 * Check if we fit in with the requested stack size into the:
4111 * If we don't, we limit the size to the TASK_SIZE.
4113 * - remaining sample size
4114 * If we don't, we customize the stack size to
4115 * fit in to the remaining sample size.
4118 task_size
= min((u64
) USHRT_MAX
, perf_ustack_task_size(regs
));
4119 stack_size
= min(stack_size
, (u16
) task_size
);
4121 /* Current header size plus static size and dynamic size. */
4122 header_size
+= 2 * sizeof(u64
);
4124 /* Do we fit in with the current stack dump size? */
4125 if ((u16
) (header_size
+ stack_size
) < header_size
) {
4127 * If we overflow the maximum size for the sample,
4128 * we customize the stack dump size to fit in.
4130 stack_size
= USHRT_MAX
- header_size
- sizeof(u64
);
4131 stack_size
= round_up(stack_size
, sizeof(u64
));
4138 perf_output_sample_ustack(struct perf_output_handle
*handle
, u64 dump_size
,
4139 struct pt_regs
*regs
)
4141 /* Case of a kernel thread, nothing to dump */
4144 perf_output_put(handle
, size
);
4153 * - the size requested by user or the best one we can fit
4154 * in to the sample max size
4156 * - user stack dump data
4158 * - the actual dumped size
4162 perf_output_put(handle
, dump_size
);
4165 sp
= perf_user_stack_pointer(regs
);
4166 rem
= __output_copy_user(handle
, (void *) sp
, dump_size
);
4167 dyn_size
= dump_size
- rem
;
4169 perf_output_skip(handle
, rem
);
4172 perf_output_put(handle
, dyn_size
);
4176 static void __perf_event_header__init_id(struct perf_event_header
*header
,
4177 struct perf_sample_data
*data
,
4178 struct perf_event
*event
)
4180 u64 sample_type
= event
->attr
.sample_type
;
4182 data
->type
= sample_type
;
4183 header
->size
+= event
->id_header_size
;
4185 if (sample_type
& PERF_SAMPLE_TID
) {
4186 /* namespace issues */
4187 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
4188 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
4191 if (sample_type
& PERF_SAMPLE_TIME
)
4192 data
->time
= perf_clock();
4194 if (sample_type
& PERF_SAMPLE_ID
)
4195 data
->id
= primary_event_id(event
);
4197 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4198 data
->stream_id
= event
->id
;
4200 if (sample_type
& PERF_SAMPLE_CPU
) {
4201 data
->cpu_entry
.cpu
= raw_smp_processor_id();
4202 data
->cpu_entry
.reserved
= 0;
4206 void perf_event_header__init_id(struct perf_event_header
*header
,
4207 struct perf_sample_data
*data
,
4208 struct perf_event
*event
)
4210 if (event
->attr
.sample_id_all
)
4211 __perf_event_header__init_id(header
, data
, event
);
4214 static void __perf_event__output_id_sample(struct perf_output_handle
*handle
,
4215 struct perf_sample_data
*data
)
4217 u64 sample_type
= data
->type
;
4219 if (sample_type
& PERF_SAMPLE_TID
)
4220 perf_output_put(handle
, data
->tid_entry
);
4222 if (sample_type
& PERF_SAMPLE_TIME
)
4223 perf_output_put(handle
, data
->time
);
4225 if (sample_type
& PERF_SAMPLE_ID
)
4226 perf_output_put(handle
, data
->id
);
4228 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4229 perf_output_put(handle
, data
->stream_id
);
4231 if (sample_type
& PERF_SAMPLE_CPU
)
4232 perf_output_put(handle
, data
->cpu_entry
);
4235 void perf_event__output_id_sample(struct perf_event
*event
,
4236 struct perf_output_handle
*handle
,
4237 struct perf_sample_data
*sample
)
4239 if (event
->attr
.sample_id_all
)
4240 __perf_event__output_id_sample(handle
, sample
);
4243 static void perf_output_read_one(struct perf_output_handle
*handle
,
4244 struct perf_event
*event
,
4245 u64 enabled
, u64 running
)
4247 u64 read_format
= event
->attr
.read_format
;
4251 values
[n
++] = perf_event_count(event
);
4252 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
4253 values
[n
++] = enabled
+
4254 atomic64_read(&event
->child_total_time_enabled
);
4256 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
4257 values
[n
++] = running
+
4258 atomic64_read(&event
->child_total_time_running
);
4260 if (read_format
& PERF_FORMAT_ID
)
4261 values
[n
++] = primary_event_id(event
);
4263 __output_copy(handle
, values
, n
* sizeof(u64
));
4267 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4269 static void perf_output_read_group(struct perf_output_handle
*handle
,
4270 struct perf_event
*event
,
4271 u64 enabled
, u64 running
)
4273 struct perf_event
*leader
= event
->group_leader
, *sub
;
4274 u64 read_format
= event
->attr
.read_format
;
4278 values
[n
++] = 1 + leader
->nr_siblings
;
4280 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
4281 values
[n
++] = enabled
;
4283 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
4284 values
[n
++] = running
;
4286 if (leader
!= event
)
4287 leader
->pmu
->read(leader
);
4289 values
[n
++] = perf_event_count(leader
);
4290 if (read_format
& PERF_FORMAT_ID
)
4291 values
[n
++] = primary_event_id(leader
);
4293 __output_copy(handle
, values
, n
* sizeof(u64
));
4295 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
4299 sub
->pmu
->read(sub
);
4301 values
[n
++] = perf_event_count(sub
);
4302 if (read_format
& PERF_FORMAT_ID
)
4303 values
[n
++] = primary_event_id(sub
);
4305 __output_copy(handle
, values
, n
* sizeof(u64
));
4309 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4310 PERF_FORMAT_TOTAL_TIME_RUNNING)
4312 static void perf_output_read(struct perf_output_handle
*handle
,
4313 struct perf_event
*event
)
4315 u64 enabled
= 0, running
= 0, now
;
4316 u64 read_format
= event
->attr
.read_format
;
4319 * compute total_time_enabled, total_time_running
4320 * based on snapshot values taken when the event
4321 * was last scheduled in.
4323 * we cannot simply called update_context_time()
4324 * because of locking issue as we are called in
4327 if (read_format
& PERF_FORMAT_TOTAL_TIMES
)
4328 calc_timer_values(event
, &now
, &enabled
, &running
);
4330 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
4331 perf_output_read_group(handle
, event
, enabled
, running
);
4333 perf_output_read_one(handle
, event
, enabled
, running
);
4336 void perf_output_sample(struct perf_output_handle
*handle
,
4337 struct perf_event_header
*header
,
4338 struct perf_sample_data
*data
,
4339 struct perf_event
*event
)
4341 u64 sample_type
= data
->type
;
4343 perf_output_put(handle
, *header
);
4345 if (sample_type
& PERF_SAMPLE_IP
)
4346 perf_output_put(handle
, data
->ip
);
4348 if (sample_type
& PERF_SAMPLE_TID
)
4349 perf_output_put(handle
, data
->tid_entry
);
4351 if (sample_type
& PERF_SAMPLE_TIME
)
4352 perf_output_put(handle
, data
->time
);
4354 if (sample_type
& PERF_SAMPLE_ADDR
)
4355 perf_output_put(handle
, data
->addr
);
4357 if (sample_type
& PERF_SAMPLE_ID
)
4358 perf_output_put(handle
, data
->id
);
4360 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4361 perf_output_put(handle
, data
->stream_id
);
4363 if (sample_type
& PERF_SAMPLE_CPU
)
4364 perf_output_put(handle
, data
->cpu_entry
);
4366 if (sample_type
& PERF_SAMPLE_PERIOD
)
4367 perf_output_put(handle
, data
->period
);
4369 if (sample_type
& PERF_SAMPLE_READ
)
4370 perf_output_read(handle
, event
);
4372 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
4373 if (data
->callchain
) {
4376 if (data
->callchain
)
4377 size
+= data
->callchain
->nr
;
4379 size
*= sizeof(u64
);
4381 __output_copy(handle
, data
->callchain
, size
);
4384 perf_output_put(handle
, nr
);
4388 if (sample_type
& PERF_SAMPLE_RAW
) {
4390 perf_output_put(handle
, data
->raw
->size
);
4391 __output_copy(handle
, data
->raw
->data
,
4398 .size
= sizeof(u32
),
4401 perf_output_put(handle
, raw
);
4405 if (!event
->attr
.watermark
) {
4406 int wakeup_events
= event
->attr
.wakeup_events
;
4408 if (wakeup_events
) {
4409 struct ring_buffer
*rb
= handle
->rb
;
4410 int events
= local_inc_return(&rb
->events
);
4412 if (events
>= wakeup_events
) {
4413 local_sub(wakeup_events
, &rb
->events
);
4414 local_inc(&rb
->wakeup
);
4419 if (sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
4420 if (data
->br_stack
) {
4423 size
= data
->br_stack
->nr
4424 * sizeof(struct perf_branch_entry
);
4426 perf_output_put(handle
, data
->br_stack
->nr
);
4427 perf_output_copy(handle
, data
->br_stack
->entries
, size
);
4430 * we always store at least the value of nr
4433 perf_output_put(handle
, nr
);
4437 if (sample_type
& PERF_SAMPLE_REGS_USER
) {
4438 u64 abi
= data
->regs_user
.abi
;
4441 * If there are no regs to dump, notice it through
4442 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4444 perf_output_put(handle
, abi
);
4447 u64 mask
= event
->attr
.sample_regs_user
;
4448 perf_output_sample_regs(handle
,
4449 data
->regs_user
.regs
,
4454 if (sample_type
& PERF_SAMPLE_STACK_USER
)
4455 perf_output_sample_ustack(handle
,
4456 data
->stack_user_size
,
4457 data
->regs_user
.regs
);
4459 if (sample_type
& PERF_SAMPLE_WEIGHT
)
4460 perf_output_put(handle
, data
->weight
);
4462 if (sample_type
& PERF_SAMPLE_DATA_SRC
)
4463 perf_output_put(handle
, data
->data_src
.val
);
4466 void perf_prepare_sample(struct perf_event_header
*header
,
4467 struct perf_sample_data
*data
,
4468 struct perf_event
*event
,
4469 struct pt_regs
*regs
)
4471 u64 sample_type
= event
->attr
.sample_type
;
4473 header
->type
= PERF_RECORD_SAMPLE
;
4474 header
->size
= sizeof(*header
) + event
->header_size
;
4477 header
->misc
|= perf_misc_flags(regs
);
4479 __perf_event_header__init_id(header
, data
, event
);
4481 if (sample_type
& PERF_SAMPLE_IP
)
4482 data
->ip
= perf_instruction_pointer(regs
);
4484 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
4487 data
->callchain
= perf_callchain(event
, regs
);
4489 if (data
->callchain
)
4490 size
+= data
->callchain
->nr
;
4492 header
->size
+= size
* sizeof(u64
);
4495 if (sample_type
& PERF_SAMPLE_RAW
) {
4496 int size
= sizeof(u32
);
4499 size
+= data
->raw
->size
;
4501 size
+= sizeof(u32
);
4503 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
4504 header
->size
+= size
;
4507 if (sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
4508 int size
= sizeof(u64
); /* nr */
4509 if (data
->br_stack
) {
4510 size
+= data
->br_stack
->nr
4511 * sizeof(struct perf_branch_entry
);
4513 header
->size
+= size
;
4516 if (sample_type
& PERF_SAMPLE_REGS_USER
) {
4517 /* regs dump ABI info */
4518 int size
= sizeof(u64
);
4520 perf_sample_regs_user(&data
->regs_user
, regs
);
4522 if (data
->regs_user
.regs
) {
4523 u64 mask
= event
->attr
.sample_regs_user
;
4524 size
+= hweight64(mask
) * sizeof(u64
);
4527 header
->size
+= size
;
4530 if (sample_type
& PERF_SAMPLE_STACK_USER
) {
4532 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4533 * processed as the last one or have additional check added
4534 * in case new sample type is added, because we could eat
4535 * up the rest of the sample size.
4537 struct perf_regs_user
*uregs
= &data
->regs_user
;
4538 u16 stack_size
= event
->attr
.sample_stack_user
;
4539 u16 size
= sizeof(u64
);
4542 perf_sample_regs_user(uregs
, regs
);
4544 stack_size
= perf_sample_ustack_size(stack_size
, header
->size
,
4548 * If there is something to dump, add space for the dump
4549 * itself and for the field that tells the dynamic size,
4550 * which is how many have been actually dumped.
4553 size
+= sizeof(u64
) + stack_size
;
4555 data
->stack_user_size
= stack_size
;
4556 header
->size
+= size
;
4560 static void perf_event_output(struct perf_event
*event
,
4561 struct perf_sample_data
*data
,
4562 struct pt_regs
*regs
)
4564 struct perf_output_handle handle
;
4565 struct perf_event_header header
;
4567 /* protect the callchain buffers */
4570 perf_prepare_sample(&header
, data
, event
, regs
);
4572 if (perf_output_begin(&handle
, event
, header
.size
))
4575 perf_output_sample(&handle
, &header
, data
, event
);
4577 perf_output_end(&handle
);
4587 struct perf_read_event
{
4588 struct perf_event_header header
;
4595 perf_event_read_event(struct perf_event
*event
,
4596 struct task_struct
*task
)
4598 struct perf_output_handle handle
;
4599 struct perf_sample_data sample
;
4600 struct perf_read_event read_event
= {
4602 .type
= PERF_RECORD_READ
,
4604 .size
= sizeof(read_event
) + event
->read_size
,
4606 .pid
= perf_event_pid(event
, task
),
4607 .tid
= perf_event_tid(event
, task
),
4611 perf_event_header__init_id(&read_event
.header
, &sample
, event
);
4612 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
);
4616 perf_output_put(&handle
, read_event
);
4617 perf_output_read(&handle
, event
);
4618 perf_event__output_id_sample(event
, &handle
, &sample
);
4620 perf_output_end(&handle
);
4623 typedef int (perf_event_aux_match_cb
)(struct perf_event
*event
, void *data
);
4624 typedef void (perf_event_aux_output_cb
)(struct perf_event
*event
, void *data
);
4627 perf_event_aux_ctx(struct perf_event_context
*ctx
,
4628 perf_event_aux_match_cb match
,
4629 perf_event_aux_output_cb output
,
4632 struct perf_event
*event
;
4634 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
4635 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
4637 if (!event_filter_match(event
))
4639 if (match(event
, data
))
4640 output(event
, data
);
4645 perf_event_aux(perf_event_aux_match_cb match
,
4646 perf_event_aux_output_cb output
,
4648 struct perf_event_context
*task_ctx
)
4650 struct perf_cpu_context
*cpuctx
;
4651 struct perf_event_context
*ctx
;
4656 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
4657 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
4658 if (cpuctx
->unique_pmu
!= pmu
)
4660 perf_event_aux_ctx(&cpuctx
->ctx
, match
, output
, data
);
4663 ctxn
= pmu
->task_ctx_nr
;
4666 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
4668 perf_event_aux_ctx(ctx
, match
, output
, data
);
4670 put_cpu_ptr(pmu
->pmu_cpu_context
);
4675 perf_event_aux_ctx(task_ctx
, match
, output
, data
);
4682 * task tracking -- fork/exit
4684 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4687 struct perf_task_event
{
4688 struct task_struct
*task
;
4689 struct perf_event_context
*task_ctx
;
4692 struct perf_event_header header
;
4702 static void perf_event_task_output(struct perf_event
*event
,
4705 struct perf_task_event
*task_event
= data
;
4706 struct perf_output_handle handle
;
4707 struct perf_sample_data sample
;
4708 struct task_struct
*task
= task_event
->task
;
4709 int ret
, size
= task_event
->event_id
.header
.size
;
4711 perf_event_header__init_id(&task_event
->event_id
.header
, &sample
, event
);
4713 ret
= perf_output_begin(&handle
, event
,
4714 task_event
->event_id
.header
.size
);
4718 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
4719 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
4721 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
4722 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
4724 perf_output_put(&handle
, task_event
->event_id
);
4726 perf_event__output_id_sample(event
, &handle
, &sample
);
4728 perf_output_end(&handle
);
4730 task_event
->event_id
.header
.size
= size
;
4733 static int perf_event_task_match(struct perf_event
*event
,
4734 void *data __maybe_unused
)
4736 return event
->attr
.comm
|| event
->attr
.mmap
||
4737 event
->attr
.mmap_data
|| event
->attr
.task
;
4740 static void perf_event_task(struct task_struct
*task
,
4741 struct perf_event_context
*task_ctx
,
4744 struct perf_task_event task_event
;
4746 if (!atomic_read(&nr_comm_events
) &&
4747 !atomic_read(&nr_mmap_events
) &&
4748 !atomic_read(&nr_task_events
))
4751 task_event
= (struct perf_task_event
){
4753 .task_ctx
= task_ctx
,
4756 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
4758 .size
= sizeof(task_event
.event_id
),
4764 .time
= perf_clock(),
4768 perf_event_aux(perf_event_task_match
,
4769 perf_event_task_output
,
4774 void perf_event_fork(struct task_struct
*task
)
4776 perf_event_task(task
, NULL
, 1);
4783 struct perf_comm_event
{
4784 struct task_struct
*task
;
4789 struct perf_event_header header
;
4796 static void perf_event_comm_output(struct perf_event
*event
,
4799 struct perf_comm_event
*comm_event
= data
;
4800 struct perf_output_handle handle
;
4801 struct perf_sample_data sample
;
4802 int size
= comm_event
->event_id
.header
.size
;
4805 perf_event_header__init_id(&comm_event
->event_id
.header
, &sample
, event
);
4806 ret
= perf_output_begin(&handle
, event
,
4807 comm_event
->event_id
.header
.size
);
4812 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
4813 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
4815 perf_output_put(&handle
, comm_event
->event_id
);
4816 __output_copy(&handle
, comm_event
->comm
,
4817 comm_event
->comm_size
);
4819 perf_event__output_id_sample(event
, &handle
, &sample
);
4821 perf_output_end(&handle
);
4823 comm_event
->event_id
.header
.size
= size
;
4826 static int perf_event_comm_match(struct perf_event
*event
,
4827 void *data __maybe_unused
)
4829 return event
->attr
.comm
;
4832 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
4834 char comm
[TASK_COMM_LEN
];
4837 memset(comm
, 0, sizeof(comm
));
4838 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
4839 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
4841 comm_event
->comm
= comm
;
4842 comm_event
->comm_size
= size
;
4844 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
4846 perf_event_aux(perf_event_comm_match
,
4847 perf_event_comm_output
,
4852 void perf_event_comm(struct task_struct
*task
)
4854 struct perf_comm_event comm_event
;
4855 struct perf_event_context
*ctx
;
4859 for_each_task_context_nr(ctxn
) {
4860 ctx
= task
->perf_event_ctxp
[ctxn
];
4864 perf_event_enable_on_exec(ctx
);
4868 if (!atomic_read(&nr_comm_events
))
4871 comm_event
= (struct perf_comm_event
){
4877 .type
= PERF_RECORD_COMM
,
4886 perf_event_comm_event(&comm_event
);
4893 struct perf_mmap_event
{
4894 struct vm_area_struct
*vma
;
4896 const char *file_name
;
4900 struct perf_event_header header
;
4910 static void perf_event_mmap_output(struct perf_event
*event
,
4913 struct perf_mmap_event
*mmap_event
= data
;
4914 struct perf_output_handle handle
;
4915 struct perf_sample_data sample
;
4916 int size
= mmap_event
->event_id
.header
.size
;
4919 perf_event_header__init_id(&mmap_event
->event_id
.header
, &sample
, event
);
4920 ret
= perf_output_begin(&handle
, event
,
4921 mmap_event
->event_id
.header
.size
);
4925 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
4926 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
4928 perf_output_put(&handle
, mmap_event
->event_id
);
4929 __output_copy(&handle
, mmap_event
->file_name
,
4930 mmap_event
->file_size
);
4932 perf_event__output_id_sample(event
, &handle
, &sample
);
4934 perf_output_end(&handle
);
4936 mmap_event
->event_id
.header
.size
= size
;
4939 static int perf_event_mmap_match(struct perf_event
*event
,
4942 struct perf_mmap_event
*mmap_event
= data
;
4943 struct vm_area_struct
*vma
= mmap_event
->vma
;
4944 int executable
= vma
->vm_flags
& VM_EXEC
;
4946 return (!executable
&& event
->attr
.mmap_data
) ||
4947 (executable
&& event
->attr
.mmap
);
4950 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
4952 struct vm_area_struct
*vma
= mmap_event
->vma
;
4953 struct file
*file
= vma
->vm_file
;
4959 memset(tmp
, 0, sizeof(tmp
));
4963 * d_path works from the end of the rb backwards, so we
4964 * need to add enough zero bytes after the string to handle
4965 * the 64bit alignment we do later.
4967 buf
= kzalloc(PATH_MAX
+ sizeof(u64
), GFP_KERNEL
);
4969 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
4972 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
4974 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
4978 if (arch_vma_name(mmap_event
->vma
)) {
4979 name
= strncpy(tmp
, arch_vma_name(mmap_event
->vma
),
4981 tmp
[sizeof(tmp
) - 1] = '\0';
4986 name
= strncpy(tmp
, "[vdso]", sizeof(tmp
));
4988 } else if (vma
->vm_start
<= vma
->vm_mm
->start_brk
&&
4989 vma
->vm_end
>= vma
->vm_mm
->brk
) {
4990 name
= strncpy(tmp
, "[heap]", sizeof(tmp
));
4992 } else if (vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
4993 vma
->vm_end
>= vma
->vm_mm
->start_stack
) {
4994 name
= strncpy(tmp
, "[stack]", sizeof(tmp
));
4998 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
5003 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
5005 mmap_event
->file_name
= name
;
5006 mmap_event
->file_size
= size
;
5008 if (!(vma
->vm_flags
& VM_EXEC
))
5009 mmap_event
->event_id
.header
.misc
|= PERF_RECORD_MISC_MMAP_DATA
;
5011 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
5013 perf_event_aux(perf_event_mmap_match
,
5014 perf_event_mmap_output
,
5021 void perf_event_mmap(struct vm_area_struct
*vma
)
5023 struct perf_mmap_event mmap_event
;
5025 if (!atomic_read(&nr_mmap_events
))
5028 mmap_event
= (struct perf_mmap_event
){
5034 .type
= PERF_RECORD_MMAP
,
5035 .misc
= PERF_RECORD_MISC_USER
,
5040 .start
= vma
->vm_start
,
5041 .len
= vma
->vm_end
- vma
->vm_start
,
5042 .pgoff
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
,
5046 perf_event_mmap_event(&mmap_event
);
5050 * IRQ throttle logging
5053 static void perf_log_throttle(struct perf_event
*event
, int enable
)
5055 struct perf_output_handle handle
;
5056 struct perf_sample_data sample
;
5060 struct perf_event_header header
;
5064 } throttle_event
= {
5066 .type
= PERF_RECORD_THROTTLE
,
5068 .size
= sizeof(throttle_event
),
5070 .time
= perf_clock(),
5071 .id
= primary_event_id(event
),
5072 .stream_id
= event
->id
,
5076 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
5078 perf_event_header__init_id(&throttle_event
.header
, &sample
, event
);
5080 ret
= perf_output_begin(&handle
, event
,
5081 throttle_event
.header
.size
);
5085 perf_output_put(&handle
, throttle_event
);
5086 perf_event__output_id_sample(event
, &handle
, &sample
);
5087 perf_output_end(&handle
);
5091 * Generic event overflow handling, sampling.
5094 static int __perf_event_overflow(struct perf_event
*event
,
5095 int throttle
, struct perf_sample_data
*data
,
5096 struct pt_regs
*regs
)
5098 int events
= atomic_read(&event
->event_limit
);
5099 struct hw_perf_event
*hwc
= &event
->hw
;
5104 * Non-sampling counters might still use the PMI to fold short
5105 * hardware counters, ignore those.
5107 if (unlikely(!is_sampling_event(event
)))
5110 seq
= __this_cpu_read(perf_throttled_seq
);
5111 if (seq
!= hwc
->interrupts_seq
) {
5112 hwc
->interrupts_seq
= seq
;
5113 hwc
->interrupts
= 1;
5116 if (unlikely(throttle
5117 && hwc
->interrupts
>= max_samples_per_tick
)) {
5118 __this_cpu_inc(perf_throttled_count
);
5119 hwc
->interrupts
= MAX_INTERRUPTS
;
5120 perf_log_throttle(event
, 0);
5125 if (event
->attr
.freq
) {
5126 u64 now
= perf_clock();
5127 s64 delta
= now
- hwc
->freq_time_stamp
;
5129 hwc
->freq_time_stamp
= now
;
5131 if (delta
> 0 && delta
< 2*TICK_NSEC
)
5132 perf_adjust_period(event
, delta
, hwc
->last_period
, true);
5136 * XXX event_limit might not quite work as expected on inherited
5140 event
->pending_kill
= POLL_IN
;
5141 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
5143 event
->pending_kill
= POLL_HUP
;
5144 event
->pending_disable
= 1;
5145 irq_work_queue(&event
->pending
);
5148 if (event
->overflow_handler
)
5149 event
->overflow_handler(event
, data
, regs
);
5151 perf_event_output(event
, data
, regs
);
5153 if (event
->fasync
&& event
->pending_kill
) {
5154 event
->pending_wakeup
= 1;
5155 irq_work_queue(&event
->pending
);
5161 int perf_event_overflow(struct perf_event
*event
,
5162 struct perf_sample_data
*data
,
5163 struct pt_regs
*regs
)
5165 return __perf_event_overflow(event
, 1, data
, regs
);
5169 * Generic software event infrastructure
5172 struct swevent_htable
{
5173 struct swevent_hlist
*swevent_hlist
;
5174 struct mutex hlist_mutex
;
5177 /* Recursion avoidance in each contexts */
5178 int recursion
[PERF_NR_CONTEXTS
];
5181 static DEFINE_PER_CPU(struct swevent_htable
, swevent_htable
);
5184 * We directly increment event->count and keep a second value in
5185 * event->hw.period_left to count intervals. This period event
5186 * is kept in the range [-sample_period, 0] so that we can use the
5190 static u64
perf_swevent_set_period(struct perf_event
*event
)
5192 struct hw_perf_event
*hwc
= &event
->hw
;
5193 u64 period
= hwc
->last_period
;
5197 hwc
->last_period
= hwc
->sample_period
;
5200 old
= val
= local64_read(&hwc
->period_left
);
5204 nr
= div64_u64(period
+ val
, period
);
5205 offset
= nr
* period
;
5207 if (local64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
5213 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
5214 struct perf_sample_data
*data
,
5215 struct pt_regs
*regs
)
5217 struct hw_perf_event
*hwc
= &event
->hw
;
5221 overflow
= perf_swevent_set_period(event
);
5223 if (hwc
->interrupts
== MAX_INTERRUPTS
)
5226 for (; overflow
; overflow
--) {
5227 if (__perf_event_overflow(event
, throttle
,
5230 * We inhibit the overflow from happening when
5231 * hwc->interrupts == MAX_INTERRUPTS.
5239 static void perf_swevent_event(struct perf_event
*event
, u64 nr
,
5240 struct perf_sample_data
*data
,
5241 struct pt_regs
*regs
)
5243 struct hw_perf_event
*hwc
= &event
->hw
;
5245 local64_add(nr
, &event
->count
);
5250 if (!is_sampling_event(event
))
5253 if ((event
->attr
.sample_type
& PERF_SAMPLE_PERIOD
) && !event
->attr
.freq
) {
5255 return perf_swevent_overflow(event
, 1, data
, regs
);
5257 data
->period
= event
->hw
.last_period
;
5259 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
5260 return perf_swevent_overflow(event
, 1, data
, regs
);
5262 if (local64_add_negative(nr
, &hwc
->period_left
))
5265 perf_swevent_overflow(event
, 0, data
, regs
);
5268 static int perf_exclude_event(struct perf_event
*event
,
5269 struct pt_regs
*regs
)
5271 if (event
->hw
.state
& PERF_HES_STOPPED
)
5275 if (event
->attr
.exclude_user
&& user_mode(regs
))
5278 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
5285 static int perf_swevent_match(struct perf_event
*event
,
5286 enum perf_type_id type
,
5288 struct perf_sample_data
*data
,
5289 struct pt_regs
*regs
)
5291 if (event
->attr
.type
!= type
)
5294 if (event
->attr
.config
!= event_id
)
5297 if (perf_exclude_event(event
, regs
))
5303 static inline u64
swevent_hash(u64 type
, u32 event_id
)
5305 u64 val
= event_id
| (type
<< 32);
5307 return hash_64(val
, SWEVENT_HLIST_BITS
);
5310 static inline struct hlist_head
*
5311 __find_swevent_head(struct swevent_hlist
*hlist
, u64 type
, u32 event_id
)
5313 u64 hash
= swevent_hash(type
, event_id
);
5315 return &hlist
->heads
[hash
];
5318 /* For the read side: events when they trigger */
5319 static inline struct hlist_head
*
5320 find_swevent_head_rcu(struct swevent_htable
*swhash
, u64 type
, u32 event_id
)
5322 struct swevent_hlist
*hlist
;
5324 hlist
= rcu_dereference(swhash
->swevent_hlist
);
5328 return __find_swevent_head(hlist
, type
, event_id
);
5331 /* For the event head insertion and removal in the hlist */
5332 static inline struct hlist_head
*
5333 find_swevent_head(struct swevent_htable
*swhash
, struct perf_event
*event
)
5335 struct swevent_hlist
*hlist
;
5336 u32 event_id
= event
->attr
.config
;
5337 u64 type
= event
->attr
.type
;
5340 * Event scheduling is always serialized against hlist allocation
5341 * and release. Which makes the protected version suitable here.
5342 * The context lock guarantees that.
5344 hlist
= rcu_dereference_protected(swhash
->swevent_hlist
,
5345 lockdep_is_held(&event
->ctx
->lock
));
5349 return __find_swevent_head(hlist
, type
, event_id
);
5352 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
5354 struct perf_sample_data
*data
,
5355 struct pt_regs
*regs
)
5357 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5358 struct perf_event
*event
;
5359 struct hlist_head
*head
;
5362 head
= find_swevent_head_rcu(swhash
, type
, event_id
);
5366 hlist_for_each_entry_rcu(event
, head
, hlist_entry
) {
5367 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
5368 perf_swevent_event(event
, nr
, data
, regs
);
5374 int perf_swevent_get_recursion_context(void)
5376 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5378 return get_recursion_context(swhash
->recursion
);
5380 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
5382 inline void perf_swevent_put_recursion_context(int rctx
)
5384 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5386 put_recursion_context(swhash
->recursion
, rctx
);
5389 void __perf_sw_event(u32 event_id
, u64 nr
, struct pt_regs
*regs
, u64 addr
)
5391 struct perf_sample_data data
;
5394 preempt_disable_notrace();
5395 rctx
= perf_swevent_get_recursion_context();
5399 perf_sample_data_init(&data
, addr
, 0);
5401 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, &data
, regs
);
5403 perf_swevent_put_recursion_context(rctx
);
5404 preempt_enable_notrace();
5407 static void perf_swevent_read(struct perf_event
*event
)
5411 static int perf_swevent_add(struct perf_event
*event
, int flags
)
5413 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5414 struct hw_perf_event
*hwc
= &event
->hw
;
5415 struct hlist_head
*head
;
5417 if (is_sampling_event(event
)) {
5418 hwc
->last_period
= hwc
->sample_period
;
5419 perf_swevent_set_period(event
);
5422 hwc
->state
= !(flags
& PERF_EF_START
);
5424 head
= find_swevent_head(swhash
, event
);
5425 if (WARN_ON_ONCE(!head
))
5428 hlist_add_head_rcu(&event
->hlist_entry
, head
);
5433 static void perf_swevent_del(struct perf_event
*event
, int flags
)
5435 hlist_del_rcu(&event
->hlist_entry
);
5438 static void perf_swevent_start(struct perf_event
*event
, int flags
)
5440 event
->hw
.state
= 0;
5443 static void perf_swevent_stop(struct perf_event
*event
, int flags
)
5445 event
->hw
.state
= PERF_HES_STOPPED
;
5448 /* Deref the hlist from the update side */
5449 static inline struct swevent_hlist
*
5450 swevent_hlist_deref(struct swevent_htable
*swhash
)
5452 return rcu_dereference_protected(swhash
->swevent_hlist
,
5453 lockdep_is_held(&swhash
->hlist_mutex
));
5456 static void swevent_hlist_release(struct swevent_htable
*swhash
)
5458 struct swevent_hlist
*hlist
= swevent_hlist_deref(swhash
);
5463 rcu_assign_pointer(swhash
->swevent_hlist
, NULL
);
5464 kfree_rcu(hlist
, rcu_head
);
5467 static void swevent_hlist_put_cpu(struct perf_event
*event
, int cpu
)
5469 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
5471 mutex_lock(&swhash
->hlist_mutex
);
5473 if (!--swhash
->hlist_refcount
)
5474 swevent_hlist_release(swhash
);
5476 mutex_unlock(&swhash
->hlist_mutex
);
5479 static void swevent_hlist_put(struct perf_event
*event
)
5483 if (event
->cpu
!= -1) {
5484 swevent_hlist_put_cpu(event
, event
->cpu
);
5488 for_each_possible_cpu(cpu
)
5489 swevent_hlist_put_cpu(event
, cpu
);
5492 static int swevent_hlist_get_cpu(struct perf_event
*event
, int cpu
)
5494 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
5497 mutex_lock(&swhash
->hlist_mutex
);
5498 if (!swevent_hlist_deref(swhash
) && cpu_online(cpu
)) {
5499 struct swevent_hlist
*hlist
;
5501 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
5506 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
5508 swhash
->hlist_refcount
++;
5510 mutex_unlock(&swhash
->hlist_mutex
);
5515 static int swevent_hlist_get(struct perf_event
*event
)
5518 int cpu
, failed_cpu
;
5520 if (event
->cpu
!= -1)
5521 return swevent_hlist_get_cpu(event
, event
->cpu
);
5524 for_each_possible_cpu(cpu
) {
5525 err
= swevent_hlist_get_cpu(event
, cpu
);
5535 for_each_possible_cpu(cpu
) {
5536 if (cpu
== failed_cpu
)
5538 swevent_hlist_put_cpu(event
, cpu
);
5545 struct static_key perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
5547 static void sw_perf_event_destroy(struct perf_event
*event
)
5549 u64 event_id
= event
->attr
.config
;
5551 WARN_ON(event
->parent
);
5553 static_key_slow_dec(&perf_swevent_enabled
[event_id
]);
5554 swevent_hlist_put(event
);
5557 static int perf_swevent_init(struct perf_event
*event
)
5559 u64 event_id
= event
->attr
.config
;
5561 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
5565 * no branch sampling for software events
5567 if (has_branch_stack(event
))
5571 case PERF_COUNT_SW_CPU_CLOCK
:
5572 case PERF_COUNT_SW_TASK_CLOCK
:
5579 if (event_id
>= PERF_COUNT_SW_MAX
)
5582 if (!event
->parent
) {
5585 err
= swevent_hlist_get(event
);
5589 static_key_slow_inc(&perf_swevent_enabled
[event_id
]);
5590 event
->destroy
= sw_perf_event_destroy
;
5596 static int perf_swevent_event_idx(struct perf_event
*event
)
5601 static struct pmu perf_swevent
= {
5602 .task_ctx_nr
= perf_sw_context
,
5604 .event_init
= perf_swevent_init
,
5605 .add
= perf_swevent_add
,
5606 .del
= perf_swevent_del
,
5607 .start
= perf_swevent_start
,
5608 .stop
= perf_swevent_stop
,
5609 .read
= perf_swevent_read
,
5611 .event_idx
= perf_swevent_event_idx
,
5614 #ifdef CONFIG_EVENT_TRACING
5616 static int perf_tp_filter_match(struct perf_event
*event
,
5617 struct perf_sample_data
*data
)
5619 void *record
= data
->raw
->data
;
5621 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
5626 static int perf_tp_event_match(struct perf_event
*event
,
5627 struct perf_sample_data
*data
,
5628 struct pt_regs
*regs
)
5630 if (event
->hw
.state
& PERF_HES_STOPPED
)
5633 * All tracepoints are from kernel-space.
5635 if (event
->attr
.exclude_kernel
)
5638 if (!perf_tp_filter_match(event
, data
))
5644 void perf_tp_event(u64 addr
, u64 count
, void *record
, int entry_size
,
5645 struct pt_regs
*regs
, struct hlist_head
*head
, int rctx
,
5646 struct task_struct
*task
)
5648 struct perf_sample_data data
;
5649 struct perf_event
*event
;
5651 struct perf_raw_record raw
= {
5656 perf_sample_data_init(&data
, addr
, 0);
5659 hlist_for_each_entry_rcu(event
, head
, hlist_entry
) {
5660 if (perf_tp_event_match(event
, &data
, regs
))
5661 perf_swevent_event(event
, count
, &data
, regs
);
5665 * If we got specified a target task, also iterate its context and
5666 * deliver this event there too.
5668 if (task
&& task
!= current
) {
5669 struct perf_event_context
*ctx
;
5670 struct trace_entry
*entry
= record
;
5673 ctx
= rcu_dereference(task
->perf_event_ctxp
[perf_sw_context
]);
5677 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
5678 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
5680 if (event
->attr
.config
!= entry
->type
)
5682 if (perf_tp_event_match(event
, &data
, regs
))
5683 perf_swevent_event(event
, count
, &data
, regs
);
5689 perf_swevent_put_recursion_context(rctx
);
5691 EXPORT_SYMBOL_GPL(perf_tp_event
);
5693 static void tp_perf_event_destroy(struct perf_event
*event
)
5695 perf_trace_destroy(event
);
5698 static int perf_tp_event_init(struct perf_event
*event
)
5702 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
5706 * no branch sampling for tracepoint events
5708 if (has_branch_stack(event
))
5711 err
= perf_trace_init(event
);
5715 event
->destroy
= tp_perf_event_destroy
;
5720 static struct pmu perf_tracepoint
= {
5721 .task_ctx_nr
= perf_sw_context
,
5723 .event_init
= perf_tp_event_init
,
5724 .add
= perf_trace_add
,
5725 .del
= perf_trace_del
,
5726 .start
= perf_swevent_start
,
5727 .stop
= perf_swevent_stop
,
5728 .read
= perf_swevent_read
,
5730 .event_idx
= perf_swevent_event_idx
,
5733 static inline void perf_tp_register(void)
5735 perf_pmu_register(&perf_tracepoint
, "tracepoint", PERF_TYPE_TRACEPOINT
);
5738 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
5743 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
5746 filter_str
= strndup_user(arg
, PAGE_SIZE
);
5747 if (IS_ERR(filter_str
))
5748 return PTR_ERR(filter_str
);
5750 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
, filter_str
);
5756 static void perf_event_free_filter(struct perf_event
*event
)
5758 ftrace_profile_free_filter(event
);
5763 static inline void perf_tp_register(void)
5767 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
5772 static void perf_event_free_filter(struct perf_event
*event
)
5776 #endif /* CONFIG_EVENT_TRACING */
5778 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5779 void perf_bp_event(struct perf_event
*bp
, void *data
)
5781 struct perf_sample_data sample
;
5782 struct pt_regs
*regs
= data
;
5784 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
, 0);
5786 if (!bp
->hw
.state
&& !perf_exclude_event(bp
, regs
))
5787 perf_swevent_event(bp
, 1, &sample
, regs
);
5792 * hrtimer based swevent callback
5795 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
5797 enum hrtimer_restart ret
= HRTIMER_RESTART
;
5798 struct perf_sample_data data
;
5799 struct pt_regs
*regs
;
5800 struct perf_event
*event
;
5803 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
5805 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
5806 return HRTIMER_NORESTART
;
5808 event
->pmu
->read(event
);
5810 perf_sample_data_init(&data
, 0, event
->hw
.last_period
);
5811 regs
= get_irq_regs();
5813 if (regs
&& !perf_exclude_event(event
, regs
)) {
5814 if (!(event
->attr
.exclude_idle
&& is_idle_task(current
)))
5815 if (__perf_event_overflow(event
, 1, &data
, regs
))
5816 ret
= HRTIMER_NORESTART
;
5819 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
5820 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
5825 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
5827 struct hw_perf_event
*hwc
= &event
->hw
;
5830 if (!is_sampling_event(event
))
5833 period
= local64_read(&hwc
->period_left
);
5838 local64_set(&hwc
->period_left
, 0);
5840 period
= max_t(u64
, 10000, hwc
->sample_period
);
5842 __hrtimer_start_range_ns(&hwc
->hrtimer
,
5843 ns_to_ktime(period
), 0,
5844 HRTIMER_MODE_REL_PINNED
, 0);
5847 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
5849 struct hw_perf_event
*hwc
= &event
->hw
;
5851 if (is_sampling_event(event
)) {
5852 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
5853 local64_set(&hwc
->period_left
, ktime_to_ns(remaining
));
5855 hrtimer_cancel(&hwc
->hrtimer
);
5859 static void perf_swevent_init_hrtimer(struct perf_event
*event
)
5861 struct hw_perf_event
*hwc
= &event
->hw
;
5863 if (!is_sampling_event(event
))
5866 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
5867 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
5870 * Since hrtimers have a fixed rate, we can do a static freq->period
5871 * mapping and avoid the whole period adjust feedback stuff.
5873 if (event
->attr
.freq
) {
5874 long freq
= event
->attr
.sample_freq
;
5876 event
->attr
.sample_period
= NSEC_PER_SEC
/ freq
;
5877 hwc
->sample_period
= event
->attr
.sample_period
;
5878 local64_set(&hwc
->period_left
, hwc
->sample_period
);
5879 hwc
->last_period
= hwc
->sample_period
;
5880 event
->attr
.freq
= 0;
5885 * Software event: cpu wall time clock
5888 static void cpu_clock_event_update(struct perf_event
*event
)
5893 now
= local_clock();
5894 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
5895 local64_add(now
- prev
, &event
->count
);
5898 static void cpu_clock_event_start(struct perf_event
*event
, int flags
)
5900 local64_set(&event
->hw
.prev_count
, local_clock());
5901 perf_swevent_start_hrtimer(event
);
5904 static void cpu_clock_event_stop(struct perf_event
*event
, int flags
)
5906 perf_swevent_cancel_hrtimer(event
);
5907 cpu_clock_event_update(event
);
5910 static int cpu_clock_event_add(struct perf_event
*event
, int flags
)
5912 if (flags
& PERF_EF_START
)
5913 cpu_clock_event_start(event
, flags
);
5918 static void cpu_clock_event_del(struct perf_event
*event
, int flags
)
5920 cpu_clock_event_stop(event
, flags
);
5923 static void cpu_clock_event_read(struct perf_event
*event
)
5925 cpu_clock_event_update(event
);
5928 static int cpu_clock_event_init(struct perf_event
*event
)
5930 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
5933 if (event
->attr
.config
!= PERF_COUNT_SW_CPU_CLOCK
)
5937 * no branch sampling for software events
5939 if (has_branch_stack(event
))
5942 perf_swevent_init_hrtimer(event
);
5947 static struct pmu perf_cpu_clock
= {
5948 .task_ctx_nr
= perf_sw_context
,
5950 .event_init
= cpu_clock_event_init
,
5951 .add
= cpu_clock_event_add
,
5952 .del
= cpu_clock_event_del
,
5953 .start
= cpu_clock_event_start
,
5954 .stop
= cpu_clock_event_stop
,
5955 .read
= cpu_clock_event_read
,
5957 .event_idx
= perf_swevent_event_idx
,
5961 * Software event: task time clock
5964 static void task_clock_event_update(struct perf_event
*event
, u64 now
)
5969 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
5971 local64_add(delta
, &event
->count
);
5974 static void task_clock_event_start(struct perf_event
*event
, int flags
)
5976 local64_set(&event
->hw
.prev_count
, event
->ctx
->time
);
5977 perf_swevent_start_hrtimer(event
);
5980 static void task_clock_event_stop(struct perf_event
*event
, int flags
)
5982 perf_swevent_cancel_hrtimer(event
);
5983 task_clock_event_update(event
, event
->ctx
->time
);
5986 static int task_clock_event_add(struct perf_event
*event
, int flags
)
5988 if (flags
& PERF_EF_START
)
5989 task_clock_event_start(event
, flags
);
5994 static void task_clock_event_del(struct perf_event
*event
, int flags
)
5996 task_clock_event_stop(event
, PERF_EF_UPDATE
);
5999 static void task_clock_event_read(struct perf_event
*event
)
6001 u64 now
= perf_clock();
6002 u64 delta
= now
- event
->ctx
->timestamp
;
6003 u64 time
= event
->ctx
->time
+ delta
;
6005 task_clock_event_update(event
, time
);
6008 static int task_clock_event_init(struct perf_event
*event
)
6010 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
6013 if (event
->attr
.config
!= PERF_COUNT_SW_TASK_CLOCK
)
6017 * no branch sampling for software events
6019 if (has_branch_stack(event
))
6022 perf_swevent_init_hrtimer(event
);
6027 static struct pmu perf_task_clock
= {
6028 .task_ctx_nr
= perf_sw_context
,
6030 .event_init
= task_clock_event_init
,
6031 .add
= task_clock_event_add
,
6032 .del
= task_clock_event_del
,
6033 .start
= task_clock_event_start
,
6034 .stop
= task_clock_event_stop
,
6035 .read
= task_clock_event_read
,
6037 .event_idx
= perf_swevent_event_idx
,
6040 static void perf_pmu_nop_void(struct pmu
*pmu
)
6044 static int perf_pmu_nop_int(struct pmu
*pmu
)
6049 static void perf_pmu_start_txn(struct pmu
*pmu
)
6051 perf_pmu_disable(pmu
);
6054 static int perf_pmu_commit_txn(struct pmu
*pmu
)
6056 perf_pmu_enable(pmu
);
6060 static void perf_pmu_cancel_txn(struct pmu
*pmu
)
6062 perf_pmu_enable(pmu
);
6065 static int perf_event_idx_default(struct perf_event
*event
)
6067 return event
->hw
.idx
+ 1;
6071 * Ensures all contexts with the same task_ctx_nr have the same
6072 * pmu_cpu_context too.
6074 static void *find_pmu_context(int ctxn
)
6081 list_for_each_entry(pmu
, &pmus
, entry
) {
6082 if (pmu
->task_ctx_nr
== ctxn
)
6083 return pmu
->pmu_cpu_context
;
6089 static void update_pmu_context(struct pmu
*pmu
, struct pmu
*old_pmu
)
6093 for_each_possible_cpu(cpu
) {
6094 struct perf_cpu_context
*cpuctx
;
6096 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
6098 if (cpuctx
->unique_pmu
== old_pmu
)
6099 cpuctx
->unique_pmu
= pmu
;
6103 static void free_pmu_context(struct pmu
*pmu
)
6107 mutex_lock(&pmus_lock
);
6109 * Like a real lame refcount.
6111 list_for_each_entry(i
, &pmus
, entry
) {
6112 if (i
->pmu_cpu_context
== pmu
->pmu_cpu_context
) {
6113 update_pmu_context(i
, pmu
);
6118 free_percpu(pmu
->pmu_cpu_context
);
6120 mutex_unlock(&pmus_lock
);
6122 static struct idr pmu_idr
;
6125 type_show(struct device
*dev
, struct device_attribute
*attr
, char *page
)
6127 struct pmu
*pmu
= dev_get_drvdata(dev
);
6129 return snprintf(page
, PAGE_SIZE
-1, "%d\n", pmu
->type
);
6132 static struct device_attribute pmu_dev_attrs
[] = {
6137 static int pmu_bus_running
;
6138 static struct bus_type pmu_bus
= {
6139 .name
= "event_source",
6140 .dev_attrs
= pmu_dev_attrs
,
6143 static void pmu_dev_release(struct device
*dev
)
6148 static int pmu_dev_alloc(struct pmu
*pmu
)
6152 pmu
->dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
6156 pmu
->dev
->groups
= pmu
->attr_groups
;
6157 device_initialize(pmu
->dev
);
6158 ret
= dev_set_name(pmu
->dev
, "%s", pmu
->name
);
6162 dev_set_drvdata(pmu
->dev
, pmu
);
6163 pmu
->dev
->bus
= &pmu_bus
;
6164 pmu
->dev
->release
= pmu_dev_release
;
6165 ret
= device_add(pmu
->dev
);
6173 put_device(pmu
->dev
);
6177 static struct lock_class_key cpuctx_mutex
;
6178 static struct lock_class_key cpuctx_lock
;
6180 int perf_pmu_register(struct pmu
*pmu
, char *name
, int type
)
6184 mutex_lock(&pmus_lock
);
6186 pmu
->pmu_disable_count
= alloc_percpu(int);
6187 if (!pmu
->pmu_disable_count
)
6196 type
= idr_alloc(&pmu_idr
, pmu
, PERF_TYPE_MAX
, 0, GFP_KERNEL
);
6204 if (pmu_bus_running
) {
6205 ret
= pmu_dev_alloc(pmu
);
6211 pmu
->pmu_cpu_context
= find_pmu_context(pmu
->task_ctx_nr
);
6212 if (pmu
->pmu_cpu_context
)
6213 goto got_cpu_context
;
6216 pmu
->pmu_cpu_context
= alloc_percpu(struct perf_cpu_context
);
6217 if (!pmu
->pmu_cpu_context
)
6220 for_each_possible_cpu(cpu
) {
6221 struct perf_cpu_context
*cpuctx
;
6223 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
6224 __perf_event_init_context(&cpuctx
->ctx
);
6225 lockdep_set_class(&cpuctx
->ctx
.mutex
, &cpuctx_mutex
);
6226 lockdep_set_class(&cpuctx
->ctx
.lock
, &cpuctx_lock
);
6227 cpuctx
->ctx
.type
= cpu_context
;
6228 cpuctx
->ctx
.pmu
= pmu
;
6229 cpuctx
->jiffies_interval
= 1;
6230 INIT_LIST_HEAD(&cpuctx
->rotation_list
);
6231 cpuctx
->unique_pmu
= pmu
;
6235 if (!pmu
->start_txn
) {
6236 if (pmu
->pmu_enable
) {
6238 * If we have pmu_enable/pmu_disable calls, install
6239 * transaction stubs that use that to try and batch
6240 * hardware accesses.
6242 pmu
->start_txn
= perf_pmu_start_txn
;
6243 pmu
->commit_txn
= perf_pmu_commit_txn
;
6244 pmu
->cancel_txn
= perf_pmu_cancel_txn
;
6246 pmu
->start_txn
= perf_pmu_nop_void
;
6247 pmu
->commit_txn
= perf_pmu_nop_int
;
6248 pmu
->cancel_txn
= perf_pmu_nop_void
;
6252 if (!pmu
->pmu_enable
) {
6253 pmu
->pmu_enable
= perf_pmu_nop_void
;
6254 pmu
->pmu_disable
= perf_pmu_nop_void
;
6257 if (!pmu
->event_idx
)
6258 pmu
->event_idx
= perf_event_idx_default
;
6260 list_add_rcu(&pmu
->entry
, &pmus
);
6263 mutex_unlock(&pmus_lock
);
6268 device_del(pmu
->dev
);
6269 put_device(pmu
->dev
);
6272 if (pmu
->type
>= PERF_TYPE_MAX
)
6273 idr_remove(&pmu_idr
, pmu
->type
);
6276 free_percpu(pmu
->pmu_disable_count
);
6280 void perf_pmu_unregister(struct pmu
*pmu
)
6282 mutex_lock(&pmus_lock
);
6283 list_del_rcu(&pmu
->entry
);
6284 mutex_unlock(&pmus_lock
);
6287 * We dereference the pmu list under both SRCU and regular RCU, so
6288 * synchronize against both of those.
6290 synchronize_srcu(&pmus_srcu
);
6293 free_percpu(pmu
->pmu_disable_count
);
6294 if (pmu
->type
>= PERF_TYPE_MAX
)
6295 idr_remove(&pmu_idr
, pmu
->type
);
6296 device_del(pmu
->dev
);
6297 put_device(pmu
->dev
);
6298 free_pmu_context(pmu
);
6301 struct pmu
*perf_init_event(struct perf_event
*event
)
6303 struct pmu
*pmu
= NULL
;
6307 idx
= srcu_read_lock(&pmus_srcu
);
6310 pmu
= idr_find(&pmu_idr
, event
->attr
.type
);
6314 ret
= pmu
->event_init(event
);
6320 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
6322 ret
= pmu
->event_init(event
);
6326 if (ret
!= -ENOENT
) {
6331 pmu
= ERR_PTR(-ENOENT
);
6333 srcu_read_unlock(&pmus_srcu
, idx
);
6339 * Allocate and initialize a event structure
6341 static struct perf_event
*
6342 perf_event_alloc(struct perf_event_attr
*attr
, int cpu
,
6343 struct task_struct
*task
,
6344 struct perf_event
*group_leader
,
6345 struct perf_event
*parent_event
,
6346 perf_overflow_handler_t overflow_handler
,
6350 struct perf_event
*event
;
6351 struct hw_perf_event
*hwc
;
6354 if ((unsigned)cpu
>= nr_cpu_ids
) {
6355 if (!task
|| cpu
!= -1)
6356 return ERR_PTR(-EINVAL
);
6359 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
6361 return ERR_PTR(-ENOMEM
);
6364 * Single events are their own group leaders, with an
6365 * empty sibling list:
6368 group_leader
= event
;
6370 mutex_init(&event
->child_mutex
);
6371 INIT_LIST_HEAD(&event
->child_list
);
6373 INIT_LIST_HEAD(&event
->group_entry
);
6374 INIT_LIST_HEAD(&event
->event_entry
);
6375 INIT_LIST_HEAD(&event
->sibling_list
);
6376 INIT_LIST_HEAD(&event
->rb_entry
);
6378 init_waitqueue_head(&event
->waitq
);
6379 init_irq_work(&event
->pending
, perf_pending_event
);
6381 mutex_init(&event
->mmap_mutex
);
6383 atomic_long_set(&event
->refcount
, 1);
6385 event
->attr
= *attr
;
6386 event
->group_leader
= group_leader
;
6390 event
->parent
= parent_event
;
6392 event
->ns
= get_pid_ns(task_active_pid_ns(current
));
6393 event
->id
= atomic64_inc_return(&perf_event_id
);
6395 event
->state
= PERF_EVENT_STATE_INACTIVE
;
6398 event
->attach_state
= PERF_ATTACH_TASK
;
6400 if (attr
->type
== PERF_TYPE_TRACEPOINT
)
6401 event
->hw
.tp_target
= task
;
6402 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6404 * hw_breakpoint is a bit difficult here..
6406 else if (attr
->type
== PERF_TYPE_BREAKPOINT
)
6407 event
->hw
.bp_target
= task
;
6411 if (!overflow_handler
&& parent_event
) {
6412 overflow_handler
= parent_event
->overflow_handler
;
6413 context
= parent_event
->overflow_handler_context
;
6416 event
->overflow_handler
= overflow_handler
;
6417 event
->overflow_handler_context
= context
;
6419 perf_event__state_init(event
);
6424 hwc
->sample_period
= attr
->sample_period
;
6425 if (attr
->freq
&& attr
->sample_freq
)
6426 hwc
->sample_period
= 1;
6427 hwc
->last_period
= hwc
->sample_period
;
6429 local64_set(&hwc
->period_left
, hwc
->sample_period
);
6432 * we currently do not support PERF_FORMAT_GROUP on inherited events
6434 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
6437 pmu
= perf_init_event(event
);
6443 else if (IS_ERR(pmu
))
6448 put_pid_ns(event
->ns
);
6450 return ERR_PTR(err
);
6453 if (!event
->parent
) {
6454 if (event
->attach_state
& PERF_ATTACH_TASK
)
6455 static_key_slow_inc(&perf_sched_events
.key
);
6456 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
6457 atomic_inc(&nr_mmap_events
);
6458 if (event
->attr
.comm
)
6459 atomic_inc(&nr_comm_events
);
6460 if (event
->attr
.task
)
6461 atomic_inc(&nr_task_events
);
6462 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
6463 err
= get_callchain_buffers();
6466 return ERR_PTR(err
);
6469 if (has_branch_stack(event
)) {
6470 static_key_slow_inc(&perf_sched_events
.key
);
6471 if (!(event
->attach_state
& PERF_ATTACH_TASK
))
6472 atomic_inc(&per_cpu(perf_branch_stack_events
,
6480 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
6481 struct perf_event_attr
*attr
)
6486 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
6490 * zero the full structure, so that a short copy will be nice.
6492 memset(attr
, 0, sizeof(*attr
));
6494 ret
= get_user(size
, &uattr
->size
);
6498 if (size
> PAGE_SIZE
) /* silly large */
6501 if (!size
) /* abi compat */
6502 size
= PERF_ATTR_SIZE_VER0
;
6504 if (size
< PERF_ATTR_SIZE_VER0
)
6508 * If we're handed a bigger struct than we know of,
6509 * ensure all the unknown bits are 0 - i.e. new
6510 * user-space does not rely on any kernel feature
6511 * extensions we dont know about yet.
6513 if (size
> sizeof(*attr
)) {
6514 unsigned char __user
*addr
;
6515 unsigned char __user
*end
;
6518 addr
= (void __user
*)uattr
+ sizeof(*attr
);
6519 end
= (void __user
*)uattr
+ size
;
6521 for (; addr
< end
; addr
++) {
6522 ret
= get_user(val
, addr
);
6528 size
= sizeof(*attr
);
6531 ret
= copy_from_user(attr
, uattr
, size
);
6535 if (attr
->__reserved_1
)
6538 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
6541 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
6544 if (attr
->sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
6545 u64 mask
= attr
->branch_sample_type
;
6547 /* only using defined bits */
6548 if (mask
& ~(PERF_SAMPLE_BRANCH_MAX
-1))
6551 /* at least one branch bit must be set */
6552 if (!(mask
& ~PERF_SAMPLE_BRANCH_PLM_ALL
))
6555 /* kernel level capture: check permissions */
6556 if ((mask
& PERF_SAMPLE_BRANCH_PERM_PLM
)
6557 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
6560 /* propagate priv level, when not set for branch */
6561 if (!(mask
& PERF_SAMPLE_BRANCH_PLM_ALL
)) {
6563 /* exclude_kernel checked on syscall entry */
6564 if (!attr
->exclude_kernel
)
6565 mask
|= PERF_SAMPLE_BRANCH_KERNEL
;
6567 if (!attr
->exclude_user
)
6568 mask
|= PERF_SAMPLE_BRANCH_USER
;
6570 if (!attr
->exclude_hv
)
6571 mask
|= PERF_SAMPLE_BRANCH_HV
;
6573 * adjust user setting (for HW filter setup)
6575 attr
->branch_sample_type
= mask
;
6579 if (attr
->sample_type
& PERF_SAMPLE_REGS_USER
) {
6580 ret
= perf_reg_validate(attr
->sample_regs_user
);
6585 if (attr
->sample_type
& PERF_SAMPLE_STACK_USER
) {
6586 if (!arch_perf_have_user_stack_dump())
6590 * We have __u32 type for the size, but so far
6591 * we can only use __u16 as maximum due to the
6592 * __u16 sample size limit.
6594 if (attr
->sample_stack_user
>= USHRT_MAX
)
6596 else if (!IS_ALIGNED(attr
->sample_stack_user
, sizeof(u64
)))
6604 put_user(sizeof(*attr
), &uattr
->size
);
6610 perf_event_set_output(struct perf_event
*event
, struct perf_event
*output_event
)
6612 struct ring_buffer
*rb
= NULL
, *old_rb
= NULL
;
6618 /* don't allow circular references */
6619 if (event
== output_event
)
6623 * Don't allow cross-cpu buffers
6625 if (output_event
->cpu
!= event
->cpu
)
6629 * If its not a per-cpu rb, it must be the same task.
6631 if (output_event
->cpu
== -1 && output_event
->ctx
!= event
->ctx
)
6635 mutex_lock(&event
->mmap_mutex
);
6636 /* Can't redirect output if we've got an active mmap() */
6637 if (atomic_read(&event
->mmap_count
))
6643 /* get the rb we want to redirect to */
6644 rb
= ring_buffer_get(output_event
);
6650 ring_buffer_detach(event
, old_rb
);
6653 ring_buffer_attach(event
, rb
);
6655 rcu_assign_pointer(event
->rb
, rb
);
6658 ring_buffer_put(old_rb
);
6660 * Since we detached before setting the new rb, so that we
6661 * could attach the new rb, we could have missed a wakeup.
6664 wake_up_all(&event
->waitq
);
6669 mutex_unlock(&event
->mmap_mutex
);
6676 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6678 * @attr_uptr: event_id type attributes for monitoring/sampling
6681 * @group_fd: group leader event fd
6683 SYSCALL_DEFINE5(perf_event_open
,
6684 struct perf_event_attr __user
*, attr_uptr
,
6685 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
6687 struct perf_event
*group_leader
= NULL
, *output_event
= NULL
;
6688 struct perf_event
*event
, *sibling
;
6689 struct perf_event_attr attr
;
6690 struct perf_event_context
*ctx
;
6691 struct file
*event_file
= NULL
;
6692 struct fd group
= {NULL
, 0};
6693 struct task_struct
*task
= NULL
;
6699 /* for future expandability... */
6700 if (flags
& ~PERF_FLAG_ALL
)
6703 err
= perf_copy_attr(attr_uptr
, &attr
);
6707 if (!attr
.exclude_kernel
) {
6708 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
6713 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
6716 if (attr
.sample_period
& (1ULL << 63))
6721 * In cgroup mode, the pid argument is used to pass the fd
6722 * opened to the cgroup directory in cgroupfs. The cpu argument
6723 * designates the cpu on which to monitor threads from that
6726 if ((flags
& PERF_FLAG_PID_CGROUP
) && (pid
== -1 || cpu
== -1))
6729 event_fd
= get_unused_fd();
6733 if (group_fd
!= -1) {
6734 err
= perf_fget_light(group_fd
, &group
);
6737 group_leader
= group
.file
->private_data
;
6738 if (flags
& PERF_FLAG_FD_OUTPUT
)
6739 output_event
= group_leader
;
6740 if (flags
& PERF_FLAG_FD_NO_GROUP
)
6741 group_leader
= NULL
;
6744 if (pid
!= -1 && !(flags
& PERF_FLAG_PID_CGROUP
)) {
6745 task
= find_lively_task_by_vpid(pid
);
6747 err
= PTR_ERR(task
);
6754 event
= perf_event_alloc(&attr
, cpu
, task
, group_leader
, NULL
,
6756 if (IS_ERR(event
)) {
6757 err
= PTR_ERR(event
);
6761 if (flags
& PERF_FLAG_PID_CGROUP
) {
6762 err
= perf_cgroup_connect(pid
, event
, &attr
, group_leader
);
6767 * - that has cgroup constraint on event->cpu
6768 * - that may need work on context switch
6770 atomic_inc(&per_cpu(perf_cgroup_events
, event
->cpu
));
6771 static_key_slow_inc(&perf_sched_events
.key
);
6775 * Special case software events and allow them to be part of
6776 * any hardware group.
6781 (is_software_event(event
) != is_software_event(group_leader
))) {
6782 if (is_software_event(event
)) {
6784 * If event and group_leader are not both a software
6785 * event, and event is, then group leader is not.
6787 * Allow the addition of software events to !software
6788 * groups, this is safe because software events never
6791 pmu
= group_leader
->pmu
;
6792 } else if (is_software_event(group_leader
) &&
6793 (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
)) {
6795 * In case the group is a pure software group, and we
6796 * try to add a hardware event, move the whole group to
6797 * the hardware context.
6804 * Get the target context (task or percpu):
6806 ctx
= find_get_context(pmu
, task
, event
->cpu
);
6813 put_task_struct(task
);
6818 * Look up the group leader (we will attach this event to it):
6824 * Do not allow a recursive hierarchy (this new sibling
6825 * becoming part of another group-sibling):
6827 if (group_leader
->group_leader
!= group_leader
)
6830 * Do not allow to attach to a group in a different
6831 * task or CPU context:
6834 if (group_leader
->ctx
->type
!= ctx
->type
)
6837 if (group_leader
->ctx
!= ctx
)
6842 * Only a group leader can be exclusive or pinned
6844 if (attr
.exclusive
|| attr
.pinned
)
6849 err
= perf_event_set_output(event
, output_event
);
6854 event_file
= anon_inode_getfile("[perf_event]", &perf_fops
, event
, O_RDWR
);
6855 if (IS_ERR(event_file
)) {
6856 err
= PTR_ERR(event_file
);
6861 struct perf_event_context
*gctx
= group_leader
->ctx
;
6863 mutex_lock(&gctx
->mutex
);
6864 perf_remove_from_context(group_leader
, false);
6867 * Removing from the context ends up with disabled
6868 * event. What we want here is event in the initial
6869 * startup state, ready to be add into new context.
6871 perf_event__state_init(group_leader
);
6872 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
6874 perf_remove_from_context(sibling
, false);
6875 perf_event__state_init(sibling
);
6878 mutex_unlock(&gctx
->mutex
);
6882 WARN_ON_ONCE(ctx
->parent_ctx
);
6883 mutex_lock(&ctx
->mutex
);
6887 perf_install_in_context(ctx
, group_leader
, group_leader
->cpu
);
6889 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
6891 perf_install_in_context(ctx
, sibling
, sibling
->cpu
);
6896 perf_install_in_context(ctx
, event
, event
->cpu
);
6898 perf_unpin_context(ctx
);
6899 mutex_unlock(&ctx
->mutex
);
6903 event
->owner
= current
;
6905 mutex_lock(¤t
->perf_event_mutex
);
6906 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
6907 mutex_unlock(¤t
->perf_event_mutex
);
6910 * Precalculate sample_data sizes
6912 perf_event__header_size(event
);
6913 perf_event__id_header_size(event
);
6916 * Drop the reference on the group_event after placing the
6917 * new event on the sibling_list. This ensures destruction
6918 * of the group leader will find the pointer to itself in
6919 * perf_group_detach().
6922 fd_install(event_fd
, event_file
);
6926 perf_unpin_context(ctx
);
6933 put_task_struct(task
);
6937 put_unused_fd(event_fd
);
6942 * perf_event_create_kernel_counter
6944 * @attr: attributes of the counter to create
6945 * @cpu: cpu in which the counter is bound
6946 * @task: task to profile (NULL for percpu)
6949 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
6950 struct task_struct
*task
,
6951 perf_overflow_handler_t overflow_handler
,
6954 struct perf_event_context
*ctx
;
6955 struct perf_event
*event
;
6959 * Get the target context (task or percpu):
6962 event
= perf_event_alloc(attr
, cpu
, task
, NULL
, NULL
,
6963 overflow_handler
, context
);
6964 if (IS_ERR(event
)) {
6965 err
= PTR_ERR(event
);
6969 ctx
= find_get_context(event
->pmu
, task
, cpu
);
6975 WARN_ON_ONCE(ctx
->parent_ctx
);
6976 mutex_lock(&ctx
->mutex
);
6977 perf_install_in_context(ctx
, event
, cpu
);
6979 perf_unpin_context(ctx
);
6980 mutex_unlock(&ctx
->mutex
);
6987 return ERR_PTR(err
);
6989 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
6991 void perf_pmu_migrate_context(struct pmu
*pmu
, int src_cpu
, int dst_cpu
)
6993 struct perf_event_context
*src_ctx
;
6994 struct perf_event_context
*dst_ctx
;
6995 struct perf_event
*event
, *tmp
;
6998 src_ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, src_cpu
)->ctx
;
6999 dst_ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, dst_cpu
)->ctx
;
7001 mutex_lock(&src_ctx
->mutex
);
7002 list_for_each_entry_safe(event
, tmp
, &src_ctx
->event_list
,
7004 perf_remove_from_context(event
, false);
7006 list_add(&event
->event_entry
, &events
);
7008 mutex_unlock(&src_ctx
->mutex
);
7012 mutex_lock(&dst_ctx
->mutex
);
7013 list_for_each_entry_safe(event
, tmp
, &events
, event_entry
) {
7014 list_del(&event
->event_entry
);
7015 if (event
->state
>= PERF_EVENT_STATE_OFF
)
7016 event
->state
= PERF_EVENT_STATE_INACTIVE
;
7017 perf_install_in_context(dst_ctx
, event
, dst_cpu
);
7020 mutex_unlock(&dst_ctx
->mutex
);
7022 EXPORT_SYMBOL_GPL(perf_pmu_migrate_context
);
7024 static void sync_child_event(struct perf_event
*child_event
,
7025 struct task_struct
*child
)
7027 struct perf_event
*parent_event
= child_event
->parent
;
7030 if (child_event
->attr
.inherit_stat
)
7031 perf_event_read_event(child_event
, child
);
7033 child_val
= perf_event_count(child_event
);
7036 * Add back the child's count to the parent's count:
7038 atomic64_add(child_val
, &parent_event
->child_count
);
7039 atomic64_add(child_event
->total_time_enabled
,
7040 &parent_event
->child_total_time_enabled
);
7041 atomic64_add(child_event
->total_time_running
,
7042 &parent_event
->child_total_time_running
);
7045 * Remove this event from the parent's list
7047 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
7048 mutex_lock(&parent_event
->child_mutex
);
7049 list_del_init(&child_event
->child_list
);
7050 mutex_unlock(&parent_event
->child_mutex
);
7053 * Release the parent event, if this was the last
7056 put_event(parent_event
);
7060 __perf_event_exit_task(struct perf_event
*child_event
,
7061 struct perf_event_context
*child_ctx
,
7062 struct task_struct
*child
)
7064 perf_remove_from_context(child_event
, !!child_event
->parent
);
7067 * It can happen that the parent exits first, and has events
7068 * that are still around due to the child reference. These
7069 * events need to be zapped.
7071 if (child_event
->parent
) {
7072 sync_child_event(child_event
, child
);
7073 free_event(child_event
);
7077 static void perf_event_exit_task_context(struct task_struct
*child
, int ctxn
)
7079 struct perf_event
*child_event
, *tmp
;
7080 struct perf_event_context
*child_ctx
;
7081 unsigned long flags
;
7083 if (likely(!child
->perf_event_ctxp
[ctxn
])) {
7084 perf_event_task(child
, NULL
, 0);
7088 local_irq_save(flags
);
7090 * We can't reschedule here because interrupts are disabled,
7091 * and either child is current or it is a task that can't be
7092 * scheduled, so we are now safe from rescheduling changing
7095 child_ctx
= rcu_dereference_raw(child
->perf_event_ctxp
[ctxn
]);
7098 * Take the context lock here so that if find_get_context is
7099 * reading child->perf_event_ctxp, we wait until it has
7100 * incremented the context's refcount before we do put_ctx below.
7102 raw_spin_lock(&child_ctx
->lock
);
7103 task_ctx_sched_out(child_ctx
);
7104 child
->perf_event_ctxp
[ctxn
] = NULL
;
7106 * If this context is a clone; unclone it so it can't get
7107 * swapped to another process while we're removing all
7108 * the events from it.
7110 unclone_ctx(child_ctx
);
7111 update_context_time(child_ctx
);
7112 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
7115 * Report the task dead after unscheduling the events so that we
7116 * won't get any samples after PERF_RECORD_EXIT. We can however still
7117 * get a few PERF_RECORD_READ events.
7119 perf_event_task(child
, child_ctx
, 0);
7122 * We can recurse on the same lock type through:
7124 * __perf_event_exit_task()
7125 * sync_child_event()
7127 * mutex_lock(&ctx->mutex)
7129 * But since its the parent context it won't be the same instance.
7131 mutex_lock(&child_ctx
->mutex
);
7134 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->pinned_groups
,
7136 __perf_event_exit_task(child_event
, child_ctx
, child
);
7138 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->flexible_groups
,
7140 __perf_event_exit_task(child_event
, child_ctx
, child
);
7143 * If the last event was a group event, it will have appended all
7144 * its siblings to the list, but we obtained 'tmp' before that which
7145 * will still point to the list head terminating the iteration.
7147 if (!list_empty(&child_ctx
->pinned_groups
) ||
7148 !list_empty(&child_ctx
->flexible_groups
))
7151 mutex_unlock(&child_ctx
->mutex
);
7157 * When a child task exits, feed back event values to parent events.
7159 void perf_event_exit_task(struct task_struct
*child
)
7161 struct perf_event
*event
, *tmp
;
7164 mutex_lock(&child
->perf_event_mutex
);
7165 list_for_each_entry_safe(event
, tmp
, &child
->perf_event_list
,
7167 list_del_init(&event
->owner_entry
);
7170 * Ensure the list deletion is visible before we clear
7171 * the owner, closes a race against perf_release() where
7172 * we need to serialize on the owner->perf_event_mutex.
7175 event
->owner
= NULL
;
7177 mutex_unlock(&child
->perf_event_mutex
);
7179 for_each_task_context_nr(ctxn
)
7180 perf_event_exit_task_context(child
, ctxn
);
7183 static void perf_free_event(struct perf_event
*event
,
7184 struct perf_event_context
*ctx
)
7186 struct perf_event
*parent
= event
->parent
;
7188 if (WARN_ON_ONCE(!parent
))
7191 mutex_lock(&parent
->child_mutex
);
7192 list_del_init(&event
->child_list
);
7193 mutex_unlock(&parent
->child_mutex
);
7197 perf_group_detach(event
);
7198 list_del_event(event
, ctx
);
7203 * free an unexposed, unused context as created by inheritance by
7204 * perf_event_init_task below, used by fork() in case of fail.
7206 void perf_event_free_task(struct task_struct
*task
)
7208 struct perf_event_context
*ctx
;
7209 struct perf_event
*event
, *tmp
;
7212 for_each_task_context_nr(ctxn
) {
7213 ctx
= task
->perf_event_ctxp
[ctxn
];
7217 mutex_lock(&ctx
->mutex
);
7219 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
,
7221 perf_free_event(event
, ctx
);
7223 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
,
7225 perf_free_event(event
, ctx
);
7227 if (!list_empty(&ctx
->pinned_groups
) ||
7228 !list_empty(&ctx
->flexible_groups
))
7231 mutex_unlock(&ctx
->mutex
);
7237 void perf_event_delayed_put(struct task_struct
*task
)
7241 for_each_task_context_nr(ctxn
)
7242 WARN_ON_ONCE(task
->perf_event_ctxp
[ctxn
]);
7246 * inherit a event from parent task to child task:
7248 static struct perf_event
*
7249 inherit_event(struct perf_event
*parent_event
,
7250 struct task_struct
*parent
,
7251 struct perf_event_context
*parent_ctx
,
7252 struct task_struct
*child
,
7253 struct perf_event
*group_leader
,
7254 struct perf_event_context
*child_ctx
)
7256 struct perf_event
*child_event
;
7257 unsigned long flags
;
7260 * Instead of creating recursive hierarchies of events,
7261 * we link inherited events back to the original parent,
7262 * which has a filp for sure, which we use as the reference
7265 if (parent_event
->parent
)
7266 parent_event
= parent_event
->parent
;
7268 child_event
= perf_event_alloc(&parent_event
->attr
,
7271 group_leader
, parent_event
,
7273 if (IS_ERR(child_event
))
7276 if (!atomic_long_inc_not_zero(&parent_event
->refcount
)) {
7277 free_event(child_event
);
7284 * Make the child state follow the state of the parent event,
7285 * not its attr.disabled bit. We hold the parent's mutex,
7286 * so we won't race with perf_event_{en, dis}able_family.
7288 if (parent_event
->state
>= PERF_EVENT_STATE_INACTIVE
)
7289 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
7291 child_event
->state
= PERF_EVENT_STATE_OFF
;
7293 if (parent_event
->attr
.freq
) {
7294 u64 sample_period
= parent_event
->hw
.sample_period
;
7295 struct hw_perf_event
*hwc
= &child_event
->hw
;
7297 hwc
->sample_period
= sample_period
;
7298 hwc
->last_period
= sample_period
;
7300 local64_set(&hwc
->period_left
, sample_period
);
7303 child_event
->ctx
= child_ctx
;
7304 child_event
->overflow_handler
= parent_event
->overflow_handler
;
7305 child_event
->overflow_handler_context
7306 = parent_event
->overflow_handler_context
;
7309 * Precalculate sample_data sizes
7311 perf_event__header_size(child_event
);
7312 perf_event__id_header_size(child_event
);
7315 * Link it up in the child's context:
7317 raw_spin_lock_irqsave(&child_ctx
->lock
, flags
);
7318 add_event_to_ctx(child_event
, child_ctx
);
7319 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
7322 * Link this into the parent event's child list
7324 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
7325 mutex_lock(&parent_event
->child_mutex
);
7326 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
7327 mutex_unlock(&parent_event
->child_mutex
);
7332 static int inherit_group(struct perf_event
*parent_event
,
7333 struct task_struct
*parent
,
7334 struct perf_event_context
*parent_ctx
,
7335 struct task_struct
*child
,
7336 struct perf_event_context
*child_ctx
)
7338 struct perf_event
*leader
;
7339 struct perf_event
*sub
;
7340 struct perf_event
*child_ctr
;
7342 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
7343 child
, NULL
, child_ctx
);
7345 return PTR_ERR(leader
);
7346 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
7347 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
7348 child
, leader
, child_ctx
);
7349 if (IS_ERR(child_ctr
))
7350 return PTR_ERR(child_ctr
);
7356 inherit_task_group(struct perf_event
*event
, struct task_struct
*parent
,
7357 struct perf_event_context
*parent_ctx
,
7358 struct task_struct
*child
, int ctxn
,
7362 struct perf_event_context
*child_ctx
;
7364 if (!event
->attr
.inherit
) {
7369 child_ctx
= child
->perf_event_ctxp
[ctxn
];
7372 * This is executed from the parent task context, so
7373 * inherit events that have been marked for cloning.
7374 * First allocate and initialize a context for the
7378 child_ctx
= alloc_perf_context(parent_ctx
->pmu
, child
);
7382 child
->perf_event_ctxp
[ctxn
] = child_ctx
;
7385 ret
= inherit_group(event
, parent
, parent_ctx
,
7395 * Initialize the perf_event context in task_struct
7397 int perf_event_init_context(struct task_struct
*child
, int ctxn
)
7399 struct perf_event_context
*child_ctx
, *parent_ctx
;
7400 struct perf_event_context
*cloned_ctx
;
7401 struct perf_event
*event
;
7402 struct task_struct
*parent
= current
;
7403 int inherited_all
= 1;
7404 unsigned long flags
;
7407 if (likely(!parent
->perf_event_ctxp
[ctxn
]))
7411 * If the parent's context is a clone, pin it so it won't get
7414 parent_ctx
= perf_pin_task_context(parent
, ctxn
);
7417 * No need to check if parent_ctx != NULL here; since we saw
7418 * it non-NULL earlier, the only reason for it to become NULL
7419 * is if we exit, and since we're currently in the middle of
7420 * a fork we can't be exiting at the same time.
7424 * Lock the parent list. No need to lock the child - not PID
7425 * hashed yet and not running, so nobody can access it.
7427 mutex_lock(&parent_ctx
->mutex
);
7430 * We dont have to disable NMIs - we are only looking at
7431 * the list, not manipulating it:
7433 list_for_each_entry(event
, &parent_ctx
->pinned_groups
, group_entry
) {
7434 ret
= inherit_task_group(event
, parent
, parent_ctx
,
7435 child
, ctxn
, &inherited_all
);
7441 * We can't hold ctx->lock when iterating the ->flexible_group list due
7442 * to allocations, but we need to prevent rotation because
7443 * rotate_ctx() will change the list from interrupt context.
7445 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
7446 parent_ctx
->rotate_disable
= 1;
7447 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
7449 list_for_each_entry(event
, &parent_ctx
->flexible_groups
, group_entry
) {
7450 ret
= inherit_task_group(event
, parent
, parent_ctx
,
7451 child
, ctxn
, &inherited_all
);
7456 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
7457 parent_ctx
->rotate_disable
= 0;
7459 child_ctx
= child
->perf_event_ctxp
[ctxn
];
7461 if (child_ctx
&& inherited_all
) {
7463 * Mark the child context as a clone of the parent
7464 * context, or of whatever the parent is a clone of.
7466 * Note that if the parent is a clone, the holding of
7467 * parent_ctx->lock avoids it from being uncloned.
7469 cloned_ctx
= parent_ctx
->parent_ctx
;
7471 child_ctx
->parent_ctx
= cloned_ctx
;
7472 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
7474 child_ctx
->parent_ctx
= parent_ctx
;
7475 child_ctx
->parent_gen
= parent_ctx
->generation
;
7477 get_ctx(child_ctx
->parent_ctx
);
7480 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
7481 mutex_unlock(&parent_ctx
->mutex
);
7483 perf_unpin_context(parent_ctx
);
7484 put_ctx(parent_ctx
);
7490 * Initialize the perf_event context in task_struct
7492 int perf_event_init_task(struct task_struct
*child
)
7496 memset(child
->perf_event_ctxp
, 0, sizeof(child
->perf_event_ctxp
));
7497 mutex_init(&child
->perf_event_mutex
);
7498 INIT_LIST_HEAD(&child
->perf_event_list
);
7500 for_each_task_context_nr(ctxn
) {
7501 ret
= perf_event_init_context(child
, ctxn
);
7503 perf_event_free_task(child
);
7511 static void __init
perf_event_init_all_cpus(void)
7513 struct swevent_htable
*swhash
;
7516 for_each_possible_cpu(cpu
) {
7517 swhash
= &per_cpu(swevent_htable
, cpu
);
7518 mutex_init(&swhash
->hlist_mutex
);
7519 INIT_LIST_HEAD(&per_cpu(rotation_list
, cpu
));
7523 static void __cpuinit
perf_event_init_cpu(int cpu
)
7525 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
7527 mutex_lock(&swhash
->hlist_mutex
);
7528 if (swhash
->hlist_refcount
> 0) {
7529 struct swevent_hlist
*hlist
;
7531 hlist
= kzalloc_node(sizeof(*hlist
), GFP_KERNEL
, cpu_to_node(cpu
));
7533 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
7535 mutex_unlock(&swhash
->hlist_mutex
);
7538 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7539 static void perf_pmu_rotate_stop(struct pmu
*pmu
)
7541 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
7543 WARN_ON(!irqs_disabled());
7545 list_del_init(&cpuctx
->rotation_list
);
7548 static void __perf_event_exit_context(void *__info
)
7550 struct remove_event re
= { .detach_group
= false };
7551 struct perf_event_context
*ctx
= __info
;
7553 perf_pmu_rotate_stop(ctx
->pmu
);
7556 list_for_each_entry_rcu(re
.event
, &ctx
->event_list
, event_entry
)
7557 __perf_remove_from_context(&re
);
7561 static void perf_event_exit_cpu_context(int cpu
)
7563 struct perf_event_context
*ctx
;
7567 idx
= srcu_read_lock(&pmus_srcu
);
7568 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
7569 ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
)->ctx
;
7571 mutex_lock(&ctx
->mutex
);
7572 smp_call_function_single(cpu
, __perf_event_exit_context
, ctx
, 1);
7573 mutex_unlock(&ctx
->mutex
);
7575 srcu_read_unlock(&pmus_srcu
, idx
);
7578 static void perf_event_exit_cpu(int cpu
)
7580 perf_event_exit_cpu_context(cpu
);
7583 static inline void perf_event_exit_cpu(int cpu
) { }
7587 perf_reboot(struct notifier_block
*notifier
, unsigned long val
, void *v
)
7591 for_each_online_cpu(cpu
)
7592 perf_event_exit_cpu(cpu
);
7598 * Run the perf reboot notifier at the very last possible moment so that
7599 * the generic watchdog code runs as long as possible.
7601 static struct notifier_block perf_reboot_notifier
= {
7602 .notifier_call
= perf_reboot
,
7603 .priority
= INT_MIN
,
7606 static int __cpuinit
7607 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
7609 unsigned int cpu
= (long)hcpu
;
7611 switch (action
& ~CPU_TASKS_FROZEN
) {
7613 case CPU_UP_PREPARE
:
7614 case CPU_DOWN_FAILED
:
7615 perf_event_init_cpu(cpu
);
7618 case CPU_UP_CANCELED
:
7619 case CPU_DOWN_PREPARE
:
7620 perf_event_exit_cpu(cpu
);
7630 void __init
perf_event_init(void)
7636 perf_event_init_all_cpus();
7637 init_srcu_struct(&pmus_srcu
);
7638 perf_pmu_register(&perf_swevent
, "software", PERF_TYPE_SOFTWARE
);
7639 perf_pmu_register(&perf_cpu_clock
, NULL
, -1);
7640 perf_pmu_register(&perf_task_clock
, NULL
, -1);
7642 perf_cpu_notifier(perf_cpu_notify
);
7643 register_reboot_notifier(&perf_reboot_notifier
);
7645 ret
= init_hw_breakpoint();
7646 WARN(ret
, "hw_breakpoint initialization failed with: %d", ret
);
7648 /* do not patch jump label more than once per second */
7649 jump_label_rate_limit(&perf_sched_events
, HZ
);
7652 * Build time assertion that we keep the data_head at the intended
7653 * location. IOW, validation we got the __reserved[] size right.
7655 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page
, data_head
))
7659 static int __init
perf_event_sysfs_init(void)
7664 mutex_lock(&pmus_lock
);
7666 ret
= bus_register(&pmu_bus
);
7670 list_for_each_entry(pmu
, &pmus
, entry
) {
7671 if (!pmu
->name
|| pmu
->type
< 0)
7674 ret
= pmu_dev_alloc(pmu
);
7675 WARN(ret
, "Failed to register pmu: %s, reason %d\n", pmu
->name
, ret
);
7677 pmu_bus_running
= 1;
7681 mutex_unlock(&pmus_lock
);
7685 device_initcall(perf_event_sysfs_init
);
7687 #ifdef CONFIG_CGROUP_PERF
7688 static struct cgroup_subsys_state
*perf_cgroup_css_alloc(struct cgroup
*cont
)
7690 struct perf_cgroup
*jc
;
7692 jc
= kzalloc(sizeof(*jc
), GFP_KERNEL
);
7694 return ERR_PTR(-ENOMEM
);
7696 jc
->info
= alloc_percpu(struct perf_cgroup_info
);
7699 return ERR_PTR(-ENOMEM
);
7705 static void perf_cgroup_css_free(struct cgroup
*cont
)
7707 struct perf_cgroup
*jc
;
7708 jc
= container_of(cgroup_subsys_state(cont
, perf_subsys_id
),
7709 struct perf_cgroup
, css
);
7710 free_percpu(jc
->info
);
7714 static int __perf_cgroup_move(void *info
)
7716 struct task_struct
*task
= info
;
7717 perf_cgroup_switch(task
, PERF_CGROUP_SWOUT
| PERF_CGROUP_SWIN
);
7721 static void perf_cgroup_attach(struct cgroup
*cgrp
, struct cgroup_taskset
*tset
)
7723 struct task_struct
*task
;
7725 cgroup_taskset_for_each(task
, cgrp
, tset
)
7726 task_function_call(task
, __perf_cgroup_move
, task
);
7729 static void perf_cgroup_exit(struct cgroup
*cgrp
, struct cgroup
*old_cgrp
,
7730 struct task_struct
*task
)
7733 * cgroup_exit() is called in the copy_process() failure path.
7734 * Ignore this case since the task hasn't ran yet, this avoids
7735 * trying to poke a half freed task state from generic code.
7737 if (!(task
->flags
& PF_EXITING
))
7740 task_function_call(task
, __perf_cgroup_move
, task
);
7743 struct cgroup_subsys perf_subsys
= {
7744 .name
= "perf_event",
7745 .subsys_id
= perf_subsys_id
,
7746 .css_alloc
= perf_cgroup_css_alloc
,
7747 .css_free
= perf_cgroup_css_free
,
7748 .exit
= perf_cgroup_exit
,
7749 .attach
= perf_cgroup_attach
,
7751 #endif /* CONFIG_CGROUP_PERF */