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>
45 #include <asm/irq_regs.h>
47 struct remote_function_call
{
48 struct task_struct
*p
;
49 int (*func
)(void *info
);
54 static void remote_function(void *data
)
56 struct remote_function_call
*tfc
= data
;
57 struct task_struct
*p
= tfc
->p
;
61 if (task_cpu(p
) != smp_processor_id() || !task_curr(p
))
65 tfc
->ret
= tfc
->func(tfc
->info
);
69 * task_function_call - call a function on the cpu on which a task runs
70 * @p: the task to evaluate
71 * @func: the function to be called
72 * @info: the function call argument
74 * Calls the function @func when the task is currently running. This might
75 * be on the current CPU, which just calls the function directly
77 * returns: @func return value, or
78 * -ESRCH - when the process isn't running
79 * -EAGAIN - when the process moved away
82 task_function_call(struct task_struct
*p
, int (*func
) (void *info
), void *info
)
84 struct remote_function_call data
= {
88 .ret
= -ESRCH
, /* No such (running) process */
92 smp_call_function_single(task_cpu(p
), remote_function
, &data
, 1);
98 * cpu_function_call - call a function on the cpu
99 * @func: the function to be called
100 * @info: the function call argument
102 * Calls the function @func on the remote cpu.
104 * returns: @func return value or -ENXIO when the cpu is offline
106 static int cpu_function_call(int cpu
, int (*func
) (void *info
), void *info
)
108 struct remote_function_call data
= {
112 .ret
= -ENXIO
, /* No such CPU */
115 smp_call_function_single(cpu
, remote_function
, &data
, 1);
120 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
121 PERF_FLAG_FD_OUTPUT |\
122 PERF_FLAG_PID_CGROUP)
125 * branch priv levels that need permission checks
127 #define PERF_SAMPLE_BRANCH_PERM_PLM \
128 (PERF_SAMPLE_BRANCH_KERNEL |\
129 PERF_SAMPLE_BRANCH_HV)
132 EVENT_FLEXIBLE
= 0x1,
134 EVENT_ALL
= EVENT_FLEXIBLE
| EVENT_PINNED
,
138 * perf_sched_events : >0 events exist
139 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
141 struct static_key_deferred perf_sched_events __read_mostly
;
142 static DEFINE_PER_CPU(atomic_t
, perf_cgroup_events
);
143 static DEFINE_PER_CPU(atomic_t
, perf_branch_stack_events
);
145 static atomic_t nr_mmap_events __read_mostly
;
146 static atomic_t nr_comm_events __read_mostly
;
147 static atomic_t nr_task_events __read_mostly
;
149 static LIST_HEAD(pmus
);
150 static DEFINE_MUTEX(pmus_lock
);
151 static struct srcu_struct pmus_srcu
;
154 * perf event paranoia level:
155 * -1 - not paranoid at all
156 * 0 - disallow raw tracepoint access for unpriv
157 * 1 - disallow cpu events for unpriv
158 * 2 - disallow kernel profiling for unpriv
160 int sysctl_perf_event_paranoid __read_mostly
= 1;
162 /* Minimum for 512 kiB + 1 user control page */
163 int sysctl_perf_event_mlock __read_mostly
= 512 + (PAGE_SIZE
/ 1024); /* 'free' kiB per user */
166 * max perf event sample rate
168 #define DEFAULT_MAX_SAMPLE_RATE 100000
169 #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
170 #define DEFAULT_CPU_TIME_MAX_PERCENT 25
172 int sysctl_perf_event_sample_rate __read_mostly
= DEFAULT_MAX_SAMPLE_RATE
;
174 static int max_samples_per_tick __read_mostly
= DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE
, HZ
);
175 static int perf_sample_period_ns __read_mostly
= DEFAULT_SAMPLE_PERIOD_NS
;
177 static atomic_t perf_sample_allowed_ns __read_mostly
=
178 ATOMIC_INIT( DEFAULT_SAMPLE_PERIOD_NS
* DEFAULT_CPU_TIME_MAX_PERCENT
/ 100);
180 void update_perf_cpu_limits(void)
182 u64 tmp
= perf_sample_period_ns
;
184 tmp
*= sysctl_perf_cpu_time_max_percent
;
186 atomic_set(&perf_sample_allowed_ns
, tmp
);
189 int perf_proc_update_handler(struct ctl_table
*table
, int write
,
190 void __user
*buffer
, size_t *lenp
,
193 int ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
198 max_samples_per_tick
= DIV_ROUND_UP(sysctl_perf_event_sample_rate
, HZ
);
199 perf_sample_period_ns
= NSEC_PER_SEC
/ sysctl_perf_event_sample_rate
;
200 update_perf_cpu_limits();
205 int sysctl_perf_cpu_time_max_percent __read_mostly
= DEFAULT_CPU_TIME_MAX_PERCENT
;
207 int perf_cpu_time_max_percent_handler(struct ctl_table
*table
, int write
,
208 void __user
*buffer
, size_t *lenp
,
211 int ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
216 update_perf_cpu_limits();
222 * perf samples are done in some very critical code paths (NMIs).
223 * If they take too much CPU time, the system can lock up and not
224 * get any real work done. This will drop the sample rate when
225 * we detect that events are taking too long.
227 #define NR_ACCUMULATED_SAMPLES 128
228 DEFINE_PER_CPU(u64
, running_sample_length
);
230 void perf_sample_event_took(u64 sample_len_ns
)
232 u64 avg_local_sample_len
;
233 u64 local_samples_len
;
235 if (atomic_read(&perf_sample_allowed_ns
) == 0)
238 /* decay the counter by 1 average sample */
239 local_samples_len
= __get_cpu_var(running_sample_length
);
240 local_samples_len
-= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
241 local_samples_len
+= sample_len_ns
;
242 __get_cpu_var(running_sample_length
) = local_samples_len
;
245 * note: this will be biased artifically low until we have
246 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
247 * from having to maintain a count.
249 avg_local_sample_len
= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
251 if (avg_local_sample_len
<= atomic_read(&perf_sample_allowed_ns
))
254 if (max_samples_per_tick
<= 1)
257 max_samples_per_tick
= DIV_ROUND_UP(max_samples_per_tick
, 2);
258 sysctl_perf_event_sample_rate
= max_samples_per_tick
* HZ
;
259 perf_sample_period_ns
= NSEC_PER_SEC
/ sysctl_perf_event_sample_rate
;
261 printk_ratelimited(KERN_WARNING
262 "perf samples too long (%lld > %d), lowering "
263 "kernel.perf_event_max_sample_rate to %d\n",
264 avg_local_sample_len
,
265 atomic_read(&perf_sample_allowed_ns
),
266 sysctl_perf_event_sample_rate
);
268 update_perf_cpu_limits();
271 static atomic64_t perf_event_id
;
273 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
274 enum event_type_t event_type
);
276 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
277 enum event_type_t event_type
,
278 struct task_struct
*task
);
280 static void update_context_time(struct perf_event_context
*ctx
);
281 static u64
perf_event_time(struct perf_event
*event
);
283 void __weak
perf_event_print_debug(void) { }
285 extern __weak
const char *perf_pmu_name(void)
290 static inline u64
perf_clock(void)
292 return local_clock();
295 static inline struct perf_cpu_context
*
296 __get_cpu_context(struct perf_event_context
*ctx
)
298 return this_cpu_ptr(ctx
->pmu
->pmu_cpu_context
);
301 static void perf_ctx_lock(struct perf_cpu_context
*cpuctx
,
302 struct perf_event_context
*ctx
)
304 raw_spin_lock(&cpuctx
->ctx
.lock
);
306 raw_spin_lock(&ctx
->lock
);
309 static void perf_ctx_unlock(struct perf_cpu_context
*cpuctx
,
310 struct perf_event_context
*ctx
)
313 raw_spin_unlock(&ctx
->lock
);
314 raw_spin_unlock(&cpuctx
->ctx
.lock
);
317 #ifdef CONFIG_CGROUP_PERF
320 * perf_cgroup_info keeps track of time_enabled for a cgroup.
321 * This is a per-cpu dynamically allocated data structure.
323 struct perf_cgroup_info
{
329 struct cgroup_subsys_state css
;
330 struct perf_cgroup_info __percpu
*info
;
334 * Must ensure cgroup is pinned (css_get) before calling
335 * this function. In other words, we cannot call this function
336 * if there is no cgroup event for the current CPU context.
338 static inline struct perf_cgroup
*
339 perf_cgroup_from_task(struct task_struct
*task
)
341 return container_of(task_subsys_state(task
, perf_subsys_id
),
342 struct perf_cgroup
, css
);
346 perf_cgroup_match(struct perf_event
*event
)
348 struct perf_event_context
*ctx
= event
->ctx
;
349 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
351 /* @event doesn't care about cgroup */
355 /* wants specific cgroup scope but @cpuctx isn't associated with any */
360 * Cgroup scoping is recursive. An event enabled for a cgroup is
361 * also enabled for all its descendant cgroups. If @cpuctx's
362 * cgroup is a descendant of @event's (the test covers identity
363 * case), it's a match.
365 return cgroup_is_descendant(cpuctx
->cgrp
->css
.cgroup
,
366 event
->cgrp
->css
.cgroup
);
369 static inline bool perf_tryget_cgroup(struct perf_event
*event
)
371 return css_tryget(&event
->cgrp
->css
);
374 static inline void perf_put_cgroup(struct perf_event
*event
)
376 css_put(&event
->cgrp
->css
);
379 static inline void perf_detach_cgroup(struct perf_event
*event
)
381 perf_put_cgroup(event
);
385 static inline int is_cgroup_event(struct perf_event
*event
)
387 return event
->cgrp
!= NULL
;
390 static inline u64
perf_cgroup_event_time(struct perf_event
*event
)
392 struct perf_cgroup_info
*t
;
394 t
= per_cpu_ptr(event
->cgrp
->info
, event
->cpu
);
398 static inline void __update_cgrp_time(struct perf_cgroup
*cgrp
)
400 struct perf_cgroup_info
*info
;
405 info
= this_cpu_ptr(cgrp
->info
);
407 info
->time
+= now
- info
->timestamp
;
408 info
->timestamp
= now
;
411 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context
*cpuctx
)
413 struct perf_cgroup
*cgrp_out
= cpuctx
->cgrp
;
415 __update_cgrp_time(cgrp_out
);
418 static inline void update_cgrp_time_from_event(struct perf_event
*event
)
420 struct perf_cgroup
*cgrp
;
423 * ensure we access cgroup data only when needed and
424 * when we know the cgroup is pinned (css_get)
426 if (!is_cgroup_event(event
))
429 cgrp
= perf_cgroup_from_task(current
);
431 * Do not update time when cgroup is not active
433 if (cgrp
== event
->cgrp
)
434 __update_cgrp_time(event
->cgrp
);
438 perf_cgroup_set_timestamp(struct task_struct
*task
,
439 struct perf_event_context
*ctx
)
441 struct perf_cgroup
*cgrp
;
442 struct perf_cgroup_info
*info
;
445 * ctx->lock held by caller
446 * ensure we do not access cgroup data
447 * unless we have the cgroup pinned (css_get)
449 if (!task
|| !ctx
->nr_cgroups
)
452 cgrp
= perf_cgroup_from_task(task
);
453 info
= this_cpu_ptr(cgrp
->info
);
454 info
->timestamp
= ctx
->timestamp
;
457 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
458 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
461 * reschedule events based on the cgroup constraint of task.
463 * mode SWOUT : schedule out everything
464 * mode SWIN : schedule in based on cgroup for next
466 void perf_cgroup_switch(struct task_struct
*task
, int mode
)
468 struct perf_cpu_context
*cpuctx
;
473 * disable interrupts to avoid geting nr_cgroup
474 * changes via __perf_event_disable(). Also
477 local_irq_save(flags
);
480 * we reschedule only in the presence of cgroup
481 * constrained events.
485 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
486 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
487 if (cpuctx
->unique_pmu
!= pmu
)
488 continue; /* ensure we process each cpuctx once */
491 * perf_cgroup_events says at least one
492 * context on this CPU has cgroup events.
494 * ctx->nr_cgroups reports the number of cgroup
495 * events for a context.
497 if (cpuctx
->ctx
.nr_cgroups
> 0) {
498 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
499 perf_pmu_disable(cpuctx
->ctx
.pmu
);
501 if (mode
& PERF_CGROUP_SWOUT
) {
502 cpu_ctx_sched_out(cpuctx
, EVENT_ALL
);
504 * must not be done before ctxswout due
505 * to event_filter_match() in event_sched_out()
510 if (mode
& PERF_CGROUP_SWIN
) {
511 WARN_ON_ONCE(cpuctx
->cgrp
);
513 * set cgrp before ctxsw in to allow
514 * event_filter_match() to not have to pass
517 cpuctx
->cgrp
= perf_cgroup_from_task(task
);
518 cpu_ctx_sched_in(cpuctx
, EVENT_ALL
, task
);
520 perf_pmu_enable(cpuctx
->ctx
.pmu
);
521 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
527 local_irq_restore(flags
);
530 static inline void perf_cgroup_sched_out(struct task_struct
*task
,
531 struct task_struct
*next
)
533 struct perf_cgroup
*cgrp1
;
534 struct perf_cgroup
*cgrp2
= NULL
;
537 * we come here when we know perf_cgroup_events > 0
539 cgrp1
= perf_cgroup_from_task(task
);
542 * next is NULL when called from perf_event_enable_on_exec()
543 * that will systematically cause a cgroup_switch()
546 cgrp2
= perf_cgroup_from_task(next
);
549 * only schedule out current cgroup events if we know
550 * that we are switching to a different cgroup. Otherwise,
551 * do no touch the cgroup events.
554 perf_cgroup_switch(task
, PERF_CGROUP_SWOUT
);
557 static inline void perf_cgroup_sched_in(struct task_struct
*prev
,
558 struct task_struct
*task
)
560 struct perf_cgroup
*cgrp1
;
561 struct perf_cgroup
*cgrp2
= NULL
;
564 * we come here when we know perf_cgroup_events > 0
566 cgrp1
= perf_cgroup_from_task(task
);
568 /* prev can never be NULL */
569 cgrp2
= perf_cgroup_from_task(prev
);
572 * only need to schedule in cgroup events if we are changing
573 * cgroup during ctxsw. Cgroup events were not scheduled
574 * out of ctxsw out if that was not the case.
577 perf_cgroup_switch(task
, PERF_CGROUP_SWIN
);
580 static inline int perf_cgroup_connect(int fd
, struct perf_event
*event
,
581 struct perf_event_attr
*attr
,
582 struct perf_event
*group_leader
)
584 struct perf_cgroup
*cgrp
;
585 struct cgroup_subsys_state
*css
;
586 struct fd f
= fdget(fd
);
592 css
= cgroup_css_from_dir(f
.file
, perf_subsys_id
);
598 cgrp
= container_of(css
, struct perf_cgroup
, css
);
601 /* must be done before we fput() the file */
602 if (!perf_tryget_cgroup(event
)) {
609 * all events in a group must monitor
610 * the same cgroup because a task belongs
611 * to only one perf cgroup at a time
613 if (group_leader
&& group_leader
->cgrp
!= cgrp
) {
614 perf_detach_cgroup(event
);
623 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
625 struct perf_cgroup_info
*t
;
626 t
= per_cpu_ptr(event
->cgrp
->info
, event
->cpu
);
627 event
->shadow_ctx_time
= now
- t
->timestamp
;
631 perf_cgroup_defer_enabled(struct perf_event
*event
)
634 * when the current task's perf cgroup does not match
635 * the event's, we need to remember to call the
636 * perf_mark_enable() function the first time a task with
637 * a matching perf cgroup is scheduled in.
639 if (is_cgroup_event(event
) && !perf_cgroup_match(event
))
640 event
->cgrp_defer_enabled
= 1;
644 perf_cgroup_mark_enabled(struct perf_event
*event
,
645 struct perf_event_context
*ctx
)
647 struct perf_event
*sub
;
648 u64 tstamp
= perf_event_time(event
);
650 if (!event
->cgrp_defer_enabled
)
653 event
->cgrp_defer_enabled
= 0;
655 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
656 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
657 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
) {
658 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
659 sub
->cgrp_defer_enabled
= 0;
663 #else /* !CONFIG_CGROUP_PERF */
666 perf_cgroup_match(struct perf_event
*event
)
671 static inline void perf_detach_cgroup(struct perf_event
*event
)
674 static inline int is_cgroup_event(struct perf_event
*event
)
679 static inline u64
perf_cgroup_event_cgrp_time(struct perf_event
*event
)
684 static inline void update_cgrp_time_from_event(struct perf_event
*event
)
688 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context
*cpuctx
)
692 static inline void perf_cgroup_sched_out(struct task_struct
*task
,
693 struct task_struct
*next
)
697 static inline void perf_cgroup_sched_in(struct task_struct
*prev
,
698 struct task_struct
*task
)
702 static inline int perf_cgroup_connect(pid_t pid
, struct perf_event
*event
,
703 struct perf_event_attr
*attr
,
704 struct perf_event
*group_leader
)
710 perf_cgroup_set_timestamp(struct task_struct
*task
,
711 struct perf_event_context
*ctx
)
716 perf_cgroup_switch(struct task_struct
*task
, struct task_struct
*next
)
721 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
725 static inline u64
perf_cgroup_event_time(struct perf_event
*event
)
731 perf_cgroup_defer_enabled(struct perf_event
*event
)
736 perf_cgroup_mark_enabled(struct perf_event
*event
,
737 struct perf_event_context
*ctx
)
742 void perf_pmu_disable(struct pmu
*pmu
)
744 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
746 pmu
->pmu_disable(pmu
);
749 void perf_pmu_enable(struct pmu
*pmu
)
751 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
753 pmu
->pmu_enable(pmu
);
756 static DEFINE_PER_CPU(struct list_head
, rotation_list
);
759 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
760 * because they're strictly cpu affine and rotate_start is called with IRQs
761 * disabled, while rotate_context is called from IRQ context.
763 static void perf_pmu_rotate_start(struct pmu
*pmu
)
765 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
766 struct list_head
*head
= &__get_cpu_var(rotation_list
);
768 WARN_ON(!irqs_disabled());
770 if (list_empty(&cpuctx
->rotation_list
)) {
771 int was_empty
= list_empty(head
);
772 list_add(&cpuctx
->rotation_list
, head
);
774 tick_nohz_full_kick();
778 static void get_ctx(struct perf_event_context
*ctx
)
780 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
783 static void put_ctx(struct perf_event_context
*ctx
)
785 if (atomic_dec_and_test(&ctx
->refcount
)) {
787 put_ctx(ctx
->parent_ctx
);
789 put_task_struct(ctx
->task
);
790 kfree_rcu(ctx
, rcu_head
);
794 static void unclone_ctx(struct perf_event_context
*ctx
)
796 if (ctx
->parent_ctx
) {
797 put_ctx(ctx
->parent_ctx
);
798 ctx
->parent_ctx
= NULL
;
802 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
805 * only top level events have the pid namespace they were created in
808 event
= event
->parent
;
810 return task_tgid_nr_ns(p
, event
->ns
);
813 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
816 * only top level events have the pid namespace they were created in
819 event
= event
->parent
;
821 return task_pid_nr_ns(p
, event
->ns
);
825 * If we inherit events we want to return the parent event id
828 static u64
primary_event_id(struct perf_event
*event
)
833 id
= event
->parent
->id
;
839 * Get the perf_event_context for a task and lock it.
840 * This has to cope with with the fact that until it is locked,
841 * the context could get moved to another task.
843 static struct perf_event_context
*
844 perf_lock_task_context(struct task_struct
*task
, int ctxn
, unsigned long *flags
)
846 struct perf_event_context
*ctx
;
850 * One of the few rules of preemptible RCU is that one cannot do
851 * rcu_read_unlock() while holding a scheduler (or nested) lock when
852 * part of the read side critical section was preemptible -- see
853 * rcu_read_unlock_special().
855 * Since ctx->lock nests under rq->lock we must ensure the entire read
856 * side critical section is non-preemptible.
860 ctx
= rcu_dereference(task
->perf_event_ctxp
[ctxn
]);
863 * If this context is a clone of another, it might
864 * get swapped for another underneath us by
865 * perf_event_task_sched_out, though the
866 * rcu_read_lock() protects us from any context
867 * getting freed. Lock the context and check if it
868 * got swapped before we could get the lock, and retry
869 * if so. If we locked the right context, then it
870 * can't get swapped on us any more.
872 raw_spin_lock_irqsave(&ctx
->lock
, *flags
);
873 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
[ctxn
])) {
874 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
880 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
881 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
891 * Get the context for a task and increment its pin_count so it
892 * can't get swapped to another task. This also increments its
893 * reference count so that the context can't get freed.
895 static struct perf_event_context
*
896 perf_pin_task_context(struct task_struct
*task
, int ctxn
)
898 struct perf_event_context
*ctx
;
901 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
904 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
909 static void perf_unpin_context(struct perf_event_context
*ctx
)
913 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
915 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
919 * Update the record of the current time in a context.
921 static void update_context_time(struct perf_event_context
*ctx
)
923 u64 now
= perf_clock();
925 ctx
->time
+= now
- ctx
->timestamp
;
926 ctx
->timestamp
= now
;
929 static u64
perf_event_time(struct perf_event
*event
)
931 struct perf_event_context
*ctx
= event
->ctx
;
933 if (is_cgroup_event(event
))
934 return perf_cgroup_event_time(event
);
936 return ctx
? ctx
->time
: 0;
940 * Update the total_time_enabled and total_time_running fields for a event.
941 * The caller of this function needs to hold the ctx->lock.
943 static void update_event_times(struct perf_event
*event
)
945 struct perf_event_context
*ctx
= event
->ctx
;
948 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
949 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
952 * in cgroup mode, time_enabled represents
953 * the time the event was enabled AND active
954 * tasks were in the monitored cgroup. This is
955 * independent of the activity of the context as
956 * there may be a mix of cgroup and non-cgroup events.
958 * That is why we treat cgroup events differently
961 if (is_cgroup_event(event
))
962 run_end
= perf_cgroup_event_time(event
);
963 else if (ctx
->is_active
)
966 run_end
= event
->tstamp_stopped
;
968 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
970 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
971 run_end
= event
->tstamp_stopped
;
973 run_end
= perf_event_time(event
);
975 event
->total_time_running
= run_end
- event
->tstamp_running
;
980 * Update total_time_enabled and total_time_running for all events in a group.
982 static void update_group_times(struct perf_event
*leader
)
984 struct perf_event
*event
;
986 update_event_times(leader
);
987 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
988 update_event_times(event
);
991 static struct list_head
*
992 ctx_group_list(struct perf_event
*event
, struct perf_event_context
*ctx
)
994 if (event
->attr
.pinned
)
995 return &ctx
->pinned_groups
;
997 return &ctx
->flexible_groups
;
1001 * Add a event from the lists for its context.
1002 * Must be called with ctx->mutex and ctx->lock held.
1005 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
1007 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_CONTEXT
);
1008 event
->attach_state
|= PERF_ATTACH_CONTEXT
;
1011 * If we're a stand alone event or group leader, we go to the context
1012 * list, group events are kept attached to the group so that
1013 * perf_group_detach can, at all times, locate all siblings.
1015 if (event
->group_leader
== event
) {
1016 struct list_head
*list
;
1018 if (is_software_event(event
))
1019 event
->group_flags
|= PERF_GROUP_SOFTWARE
;
1021 list
= ctx_group_list(event
, ctx
);
1022 list_add_tail(&event
->group_entry
, list
);
1025 if (is_cgroup_event(event
))
1028 if (has_branch_stack(event
))
1029 ctx
->nr_branch_stack
++;
1031 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
1032 if (!ctx
->nr_events
)
1033 perf_pmu_rotate_start(ctx
->pmu
);
1035 if (event
->attr
.inherit_stat
)
1040 * Initialize event state based on the perf_event_attr::disabled.
1042 static inline void perf_event__state_init(struct perf_event
*event
)
1044 event
->state
= event
->attr
.disabled
? PERF_EVENT_STATE_OFF
:
1045 PERF_EVENT_STATE_INACTIVE
;
1049 * Called at perf_event creation and when events are attached/detached from a
1052 static void perf_event__read_size(struct perf_event
*event
)
1054 int entry
= sizeof(u64
); /* value */
1058 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1059 size
+= sizeof(u64
);
1061 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1062 size
+= sizeof(u64
);
1064 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
1065 entry
+= sizeof(u64
);
1067 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
1068 nr
+= event
->group_leader
->nr_siblings
;
1069 size
+= sizeof(u64
);
1073 event
->read_size
= size
;
1076 static void perf_event__header_size(struct perf_event
*event
)
1078 struct perf_sample_data
*data
;
1079 u64 sample_type
= event
->attr
.sample_type
;
1082 perf_event__read_size(event
);
1084 if (sample_type
& PERF_SAMPLE_IP
)
1085 size
+= sizeof(data
->ip
);
1087 if (sample_type
& PERF_SAMPLE_ADDR
)
1088 size
+= sizeof(data
->addr
);
1090 if (sample_type
& PERF_SAMPLE_PERIOD
)
1091 size
+= sizeof(data
->period
);
1093 if (sample_type
& PERF_SAMPLE_WEIGHT
)
1094 size
+= sizeof(data
->weight
);
1096 if (sample_type
& PERF_SAMPLE_READ
)
1097 size
+= event
->read_size
;
1099 if (sample_type
& PERF_SAMPLE_DATA_SRC
)
1100 size
+= sizeof(data
->data_src
.val
);
1102 event
->header_size
= size
;
1105 static void perf_event__id_header_size(struct perf_event
*event
)
1107 struct perf_sample_data
*data
;
1108 u64 sample_type
= event
->attr
.sample_type
;
1111 if (sample_type
& PERF_SAMPLE_TID
)
1112 size
+= sizeof(data
->tid_entry
);
1114 if (sample_type
& PERF_SAMPLE_TIME
)
1115 size
+= sizeof(data
->time
);
1117 if (sample_type
& PERF_SAMPLE_ID
)
1118 size
+= sizeof(data
->id
);
1120 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
1121 size
+= sizeof(data
->stream_id
);
1123 if (sample_type
& PERF_SAMPLE_CPU
)
1124 size
+= sizeof(data
->cpu_entry
);
1126 event
->id_header_size
= size
;
1129 static void perf_group_attach(struct perf_event
*event
)
1131 struct perf_event
*group_leader
= event
->group_leader
, *pos
;
1134 * We can have double attach due to group movement in perf_event_open.
1136 if (event
->attach_state
& PERF_ATTACH_GROUP
)
1139 event
->attach_state
|= PERF_ATTACH_GROUP
;
1141 if (group_leader
== event
)
1144 if (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
&&
1145 !is_software_event(event
))
1146 group_leader
->group_flags
&= ~PERF_GROUP_SOFTWARE
;
1148 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
1149 group_leader
->nr_siblings
++;
1151 perf_event__header_size(group_leader
);
1153 list_for_each_entry(pos
, &group_leader
->sibling_list
, group_entry
)
1154 perf_event__header_size(pos
);
1158 * Remove a event from the lists for its context.
1159 * Must be called with ctx->mutex and ctx->lock held.
1162 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
1164 struct perf_cpu_context
*cpuctx
;
1166 * We can have double detach due to exit/hot-unplug + close.
1168 if (!(event
->attach_state
& PERF_ATTACH_CONTEXT
))
1171 event
->attach_state
&= ~PERF_ATTACH_CONTEXT
;
1173 if (is_cgroup_event(event
)) {
1175 cpuctx
= __get_cpu_context(ctx
);
1177 * if there are no more cgroup events
1178 * then cler cgrp to avoid stale pointer
1179 * in update_cgrp_time_from_cpuctx()
1181 if (!ctx
->nr_cgroups
)
1182 cpuctx
->cgrp
= NULL
;
1185 if (has_branch_stack(event
))
1186 ctx
->nr_branch_stack
--;
1189 if (event
->attr
.inherit_stat
)
1192 list_del_rcu(&event
->event_entry
);
1194 if (event
->group_leader
== event
)
1195 list_del_init(&event
->group_entry
);
1197 update_group_times(event
);
1200 * If event was in error state, then keep it
1201 * that way, otherwise bogus counts will be
1202 * returned on read(). The only way to get out
1203 * of error state is by explicit re-enabling
1206 if (event
->state
> PERF_EVENT_STATE_OFF
)
1207 event
->state
= PERF_EVENT_STATE_OFF
;
1210 static void perf_group_detach(struct perf_event
*event
)
1212 struct perf_event
*sibling
, *tmp
;
1213 struct list_head
*list
= NULL
;
1216 * We can have double detach due to exit/hot-unplug + close.
1218 if (!(event
->attach_state
& PERF_ATTACH_GROUP
))
1221 event
->attach_state
&= ~PERF_ATTACH_GROUP
;
1224 * If this is a sibling, remove it from its group.
1226 if (event
->group_leader
!= event
) {
1227 list_del_init(&event
->group_entry
);
1228 event
->group_leader
->nr_siblings
--;
1232 if (!list_empty(&event
->group_entry
))
1233 list
= &event
->group_entry
;
1236 * If this was a group event with sibling events then
1237 * upgrade the siblings to singleton events by adding them
1238 * to whatever list we are on.
1240 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
1242 list_move_tail(&sibling
->group_entry
, list
);
1243 sibling
->group_leader
= sibling
;
1245 /* Inherit group flags from the previous leader */
1246 sibling
->group_flags
= event
->group_flags
;
1250 perf_event__header_size(event
->group_leader
);
1252 list_for_each_entry(tmp
, &event
->group_leader
->sibling_list
, group_entry
)
1253 perf_event__header_size(tmp
);
1257 event_filter_match(struct perf_event
*event
)
1259 return (event
->cpu
== -1 || event
->cpu
== smp_processor_id())
1260 && perf_cgroup_match(event
);
1264 event_sched_out(struct perf_event
*event
,
1265 struct perf_cpu_context
*cpuctx
,
1266 struct perf_event_context
*ctx
)
1268 u64 tstamp
= perf_event_time(event
);
1271 * An event which could not be activated because of
1272 * filter mismatch still needs to have its timings
1273 * maintained, otherwise bogus information is return
1274 * via read() for time_enabled, time_running:
1276 if (event
->state
== PERF_EVENT_STATE_INACTIVE
1277 && !event_filter_match(event
)) {
1278 delta
= tstamp
- event
->tstamp_stopped
;
1279 event
->tstamp_running
+= delta
;
1280 event
->tstamp_stopped
= tstamp
;
1283 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1286 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1287 if (event
->pending_disable
) {
1288 event
->pending_disable
= 0;
1289 event
->state
= PERF_EVENT_STATE_OFF
;
1291 event
->tstamp_stopped
= tstamp
;
1292 event
->pmu
->del(event
, 0);
1295 if (!is_software_event(event
))
1296 cpuctx
->active_oncpu
--;
1298 if (event
->attr
.freq
&& event
->attr
.sample_freq
)
1300 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
1301 cpuctx
->exclusive
= 0;
1305 group_sched_out(struct perf_event
*group_event
,
1306 struct perf_cpu_context
*cpuctx
,
1307 struct perf_event_context
*ctx
)
1309 struct perf_event
*event
;
1310 int state
= group_event
->state
;
1312 event_sched_out(group_event
, cpuctx
, ctx
);
1315 * Schedule out siblings (if any):
1317 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
1318 event_sched_out(event
, cpuctx
, ctx
);
1320 if (state
== PERF_EVENT_STATE_ACTIVE
&& group_event
->attr
.exclusive
)
1321 cpuctx
->exclusive
= 0;
1324 struct remove_event
{
1325 struct perf_event
*event
;
1330 * Cross CPU call to remove a performance event
1332 * We disable the event on the hardware level first. After that we
1333 * remove it from the context list.
1335 static int __perf_remove_from_context(void *info
)
1337 struct remove_event
*re
= info
;
1338 struct perf_event
*event
= re
->event
;
1339 struct perf_event_context
*ctx
= event
->ctx
;
1340 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1342 raw_spin_lock(&ctx
->lock
);
1343 event_sched_out(event
, cpuctx
, ctx
);
1344 if (re
->detach_group
)
1345 perf_group_detach(event
);
1346 list_del_event(event
, ctx
);
1347 if (!ctx
->nr_events
&& cpuctx
->task_ctx
== ctx
) {
1349 cpuctx
->task_ctx
= NULL
;
1351 raw_spin_unlock(&ctx
->lock
);
1358 * Remove the event from a task's (or a CPU's) list of events.
1360 * CPU events are removed with a smp call. For task events we only
1361 * call when the task is on a CPU.
1363 * If event->ctx is a cloned context, callers must make sure that
1364 * every task struct that event->ctx->task could possibly point to
1365 * remains valid. This is OK when called from perf_release since
1366 * that only calls us on the top-level context, which can't be a clone.
1367 * When called from perf_event_exit_task, it's OK because the
1368 * context has been detached from its task.
1370 static void perf_remove_from_context(struct perf_event
*event
, bool detach_group
)
1372 struct perf_event_context
*ctx
= event
->ctx
;
1373 struct task_struct
*task
= ctx
->task
;
1374 struct remove_event re
= {
1376 .detach_group
= detach_group
,
1379 lockdep_assert_held(&ctx
->mutex
);
1383 * Per cpu events are removed via an smp call and
1384 * the removal is always successful.
1386 cpu_function_call(event
->cpu
, __perf_remove_from_context
, &re
);
1391 if (!task_function_call(task
, __perf_remove_from_context
, &re
))
1394 raw_spin_lock_irq(&ctx
->lock
);
1396 * If we failed to find a running task, but find the context active now
1397 * that we've acquired the ctx->lock, retry.
1399 if (ctx
->is_active
) {
1400 raw_spin_unlock_irq(&ctx
->lock
);
1405 * Since the task isn't running, its safe to remove the event, us
1406 * holding the ctx->lock ensures the task won't get scheduled in.
1409 perf_group_detach(event
);
1410 list_del_event(event
, ctx
);
1411 raw_spin_unlock_irq(&ctx
->lock
);
1415 * Cross CPU call to disable a performance event
1417 int __perf_event_disable(void *info
)
1419 struct perf_event
*event
= info
;
1420 struct perf_event_context
*ctx
= event
->ctx
;
1421 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1424 * If this is a per-task event, need to check whether this
1425 * event's task is the current task on this cpu.
1427 * Can trigger due to concurrent perf_event_context_sched_out()
1428 * flipping contexts around.
1430 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
1433 raw_spin_lock(&ctx
->lock
);
1436 * If the event is on, turn it off.
1437 * If it is in error state, leave it in error state.
1439 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
) {
1440 update_context_time(ctx
);
1441 update_cgrp_time_from_event(event
);
1442 update_group_times(event
);
1443 if (event
== event
->group_leader
)
1444 group_sched_out(event
, cpuctx
, ctx
);
1446 event_sched_out(event
, cpuctx
, ctx
);
1447 event
->state
= PERF_EVENT_STATE_OFF
;
1450 raw_spin_unlock(&ctx
->lock
);
1458 * If event->ctx is a cloned context, callers must make sure that
1459 * every task struct that event->ctx->task could possibly point to
1460 * remains valid. This condition is satisifed when called through
1461 * perf_event_for_each_child or perf_event_for_each because they
1462 * hold the top-level event's child_mutex, so any descendant that
1463 * goes to exit will block in sync_child_event.
1464 * When called from perf_pending_event it's OK because event->ctx
1465 * is the current context on this CPU and preemption is disabled,
1466 * hence we can't get into perf_event_task_sched_out for this context.
1468 void perf_event_disable(struct perf_event
*event
)
1470 struct perf_event_context
*ctx
= event
->ctx
;
1471 struct task_struct
*task
= ctx
->task
;
1475 * Disable the event on the cpu that it's on
1477 cpu_function_call(event
->cpu
, __perf_event_disable
, event
);
1482 if (!task_function_call(task
, __perf_event_disable
, event
))
1485 raw_spin_lock_irq(&ctx
->lock
);
1487 * If the event is still active, we need to retry the cross-call.
1489 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
1490 raw_spin_unlock_irq(&ctx
->lock
);
1492 * Reload the task pointer, it might have been changed by
1493 * a concurrent perf_event_context_sched_out().
1500 * Since we have the lock this context can't be scheduled
1501 * in, so we can change the state safely.
1503 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1504 update_group_times(event
);
1505 event
->state
= PERF_EVENT_STATE_OFF
;
1507 raw_spin_unlock_irq(&ctx
->lock
);
1509 EXPORT_SYMBOL_GPL(perf_event_disable
);
1511 static void perf_set_shadow_time(struct perf_event
*event
,
1512 struct perf_event_context
*ctx
,
1516 * use the correct time source for the time snapshot
1518 * We could get by without this by leveraging the
1519 * fact that to get to this function, the caller
1520 * has most likely already called update_context_time()
1521 * and update_cgrp_time_xx() and thus both timestamp
1522 * are identical (or very close). Given that tstamp is,
1523 * already adjusted for cgroup, we could say that:
1524 * tstamp - ctx->timestamp
1526 * tstamp - cgrp->timestamp.
1528 * Then, in perf_output_read(), the calculation would
1529 * work with no changes because:
1530 * - event is guaranteed scheduled in
1531 * - no scheduled out in between
1532 * - thus the timestamp would be the same
1534 * But this is a bit hairy.
1536 * So instead, we have an explicit cgroup call to remain
1537 * within the time time source all along. We believe it
1538 * is cleaner and simpler to understand.
1540 if (is_cgroup_event(event
))
1541 perf_cgroup_set_shadow_time(event
, tstamp
);
1543 event
->shadow_ctx_time
= tstamp
- ctx
->timestamp
;
1546 #define MAX_INTERRUPTS (~0ULL)
1548 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1551 event_sched_in(struct perf_event
*event
,
1552 struct perf_cpu_context
*cpuctx
,
1553 struct perf_event_context
*ctx
)
1555 u64 tstamp
= perf_event_time(event
);
1557 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1560 event
->state
= PERF_EVENT_STATE_ACTIVE
;
1561 event
->oncpu
= smp_processor_id();
1564 * Unthrottle events, since we scheduled we might have missed several
1565 * ticks already, also for a heavily scheduling task there is little
1566 * guarantee it'll get a tick in a timely manner.
1568 if (unlikely(event
->hw
.interrupts
== MAX_INTERRUPTS
)) {
1569 perf_log_throttle(event
, 1);
1570 event
->hw
.interrupts
= 0;
1574 * The new state must be visible before we turn it on in the hardware:
1578 if (event
->pmu
->add(event
, PERF_EF_START
)) {
1579 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1584 event
->tstamp_running
+= tstamp
- event
->tstamp_stopped
;
1586 perf_set_shadow_time(event
, ctx
, tstamp
);
1588 if (!is_software_event(event
))
1589 cpuctx
->active_oncpu
++;
1591 if (event
->attr
.freq
&& event
->attr
.sample_freq
)
1594 if (event
->attr
.exclusive
)
1595 cpuctx
->exclusive
= 1;
1601 group_sched_in(struct perf_event
*group_event
,
1602 struct perf_cpu_context
*cpuctx
,
1603 struct perf_event_context
*ctx
)
1605 struct perf_event
*event
, *partial_group
= NULL
;
1606 struct pmu
*pmu
= group_event
->pmu
;
1607 u64 now
= ctx
->time
;
1608 bool simulate
= false;
1610 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
1613 pmu
->start_txn(pmu
);
1615 if (event_sched_in(group_event
, cpuctx
, ctx
)) {
1616 pmu
->cancel_txn(pmu
);
1621 * Schedule in siblings as one group (if any):
1623 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
1624 if (event_sched_in(event
, cpuctx
, ctx
)) {
1625 partial_group
= event
;
1630 if (!pmu
->commit_txn(pmu
))
1635 * Groups can be scheduled in as one unit only, so undo any
1636 * partial group before returning:
1637 * The events up to the failed event are scheduled out normally,
1638 * tstamp_stopped will be updated.
1640 * The failed events and the remaining siblings need to have
1641 * their timings updated as if they had gone thru event_sched_in()
1642 * and event_sched_out(). This is required to get consistent timings
1643 * across the group. This also takes care of the case where the group
1644 * could never be scheduled by ensuring tstamp_stopped is set to mark
1645 * the time the event was actually stopped, such that time delta
1646 * calculation in update_event_times() is correct.
1648 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
1649 if (event
== partial_group
)
1653 event
->tstamp_running
+= now
- event
->tstamp_stopped
;
1654 event
->tstamp_stopped
= now
;
1656 event_sched_out(event
, cpuctx
, ctx
);
1659 event_sched_out(group_event
, cpuctx
, ctx
);
1661 pmu
->cancel_txn(pmu
);
1667 * Work out whether we can put this event group on the CPU now.
1669 static int group_can_go_on(struct perf_event
*event
,
1670 struct perf_cpu_context
*cpuctx
,
1674 * Groups consisting entirely of software events can always go on.
1676 if (event
->group_flags
& PERF_GROUP_SOFTWARE
)
1679 * If an exclusive group is already on, no other hardware
1682 if (cpuctx
->exclusive
)
1685 * If this group is exclusive and there are already
1686 * events on the CPU, it can't go on.
1688 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
1691 * Otherwise, try to add it if all previous groups were able
1697 static void add_event_to_ctx(struct perf_event
*event
,
1698 struct perf_event_context
*ctx
)
1700 u64 tstamp
= perf_event_time(event
);
1702 list_add_event(event
, ctx
);
1703 perf_group_attach(event
);
1704 event
->tstamp_enabled
= tstamp
;
1705 event
->tstamp_running
= tstamp
;
1706 event
->tstamp_stopped
= tstamp
;
1709 static void task_ctx_sched_out(struct perf_event_context
*ctx
);
1711 ctx_sched_in(struct perf_event_context
*ctx
,
1712 struct perf_cpu_context
*cpuctx
,
1713 enum event_type_t event_type
,
1714 struct task_struct
*task
);
1716 static void perf_event_sched_in(struct perf_cpu_context
*cpuctx
,
1717 struct perf_event_context
*ctx
,
1718 struct task_struct
*task
)
1720 cpu_ctx_sched_in(cpuctx
, EVENT_PINNED
, task
);
1722 ctx_sched_in(ctx
, cpuctx
, EVENT_PINNED
, task
);
1723 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
, task
);
1725 ctx_sched_in(ctx
, cpuctx
, EVENT_FLEXIBLE
, task
);
1729 * Cross CPU call to install and enable a performance event
1731 * Must be called with ctx->mutex held
1733 static int __perf_install_in_context(void *info
)
1735 struct perf_event
*event
= info
;
1736 struct perf_event_context
*ctx
= event
->ctx
;
1737 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1738 struct perf_event_context
*task_ctx
= cpuctx
->task_ctx
;
1739 struct task_struct
*task
= current
;
1741 perf_ctx_lock(cpuctx
, task_ctx
);
1742 perf_pmu_disable(cpuctx
->ctx
.pmu
);
1745 * If there was an active task_ctx schedule it out.
1748 task_ctx_sched_out(task_ctx
);
1751 * If the context we're installing events in is not the
1752 * active task_ctx, flip them.
1754 if (ctx
->task
&& task_ctx
!= ctx
) {
1756 raw_spin_unlock(&task_ctx
->lock
);
1757 raw_spin_lock(&ctx
->lock
);
1762 cpuctx
->task_ctx
= task_ctx
;
1763 task
= task_ctx
->task
;
1766 cpu_ctx_sched_out(cpuctx
, EVENT_ALL
);
1768 update_context_time(ctx
);
1770 * update cgrp time only if current cgrp
1771 * matches event->cgrp. Must be done before
1772 * calling add_event_to_ctx()
1774 update_cgrp_time_from_event(event
);
1776 add_event_to_ctx(event
, ctx
);
1779 * Schedule everything back in
1781 perf_event_sched_in(cpuctx
, task_ctx
, task
);
1783 perf_pmu_enable(cpuctx
->ctx
.pmu
);
1784 perf_ctx_unlock(cpuctx
, task_ctx
);
1790 * Attach a performance event to a context
1792 * First we add the event to the list with the hardware enable bit
1793 * in event->hw_config cleared.
1795 * If the event is attached to a task which is on a CPU we use a smp
1796 * call to enable it in the task context. The task might have been
1797 * scheduled away, but we check this in the smp call again.
1800 perf_install_in_context(struct perf_event_context
*ctx
,
1801 struct perf_event
*event
,
1804 struct task_struct
*task
= ctx
->task
;
1806 lockdep_assert_held(&ctx
->mutex
);
1809 if (event
->cpu
!= -1)
1814 * Per cpu events are installed via an smp call and
1815 * the install is always successful.
1817 cpu_function_call(cpu
, __perf_install_in_context
, event
);
1822 if (!task_function_call(task
, __perf_install_in_context
, event
))
1825 raw_spin_lock_irq(&ctx
->lock
);
1827 * If we failed to find a running task, but find the context active now
1828 * that we've acquired the ctx->lock, retry.
1830 if (ctx
->is_active
) {
1831 raw_spin_unlock_irq(&ctx
->lock
);
1836 * Since the task isn't running, its safe to add the event, us holding
1837 * the ctx->lock ensures the task won't get scheduled in.
1839 add_event_to_ctx(event
, ctx
);
1840 raw_spin_unlock_irq(&ctx
->lock
);
1844 * Put a event into inactive state and update time fields.
1845 * Enabling the leader of a group effectively enables all
1846 * the group members that aren't explicitly disabled, so we
1847 * have to update their ->tstamp_enabled also.
1848 * Note: this works for group members as well as group leaders
1849 * since the non-leader members' sibling_lists will be empty.
1851 static void __perf_event_mark_enabled(struct perf_event
*event
)
1853 struct perf_event
*sub
;
1854 u64 tstamp
= perf_event_time(event
);
1856 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1857 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
1858 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
1859 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
)
1860 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
1865 * Cross CPU call to enable a performance event
1867 static int __perf_event_enable(void *info
)
1869 struct perf_event
*event
= info
;
1870 struct perf_event_context
*ctx
= event
->ctx
;
1871 struct perf_event
*leader
= event
->group_leader
;
1872 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1876 * There's a time window between 'ctx->is_active' check
1877 * in perf_event_enable function and this place having:
1879 * - ctx->lock unlocked
1881 * where the task could be killed and 'ctx' deactivated
1882 * by perf_event_exit_task.
1884 if (!ctx
->is_active
)
1887 raw_spin_lock(&ctx
->lock
);
1888 update_context_time(ctx
);
1890 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1894 * set current task's cgroup time reference point
1896 perf_cgroup_set_timestamp(current
, ctx
);
1898 __perf_event_mark_enabled(event
);
1900 if (!event_filter_match(event
)) {
1901 if (is_cgroup_event(event
))
1902 perf_cgroup_defer_enabled(event
);
1907 * If the event is in a group and isn't the group leader,
1908 * then don't put it on unless the group is on.
1910 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
)
1913 if (!group_can_go_on(event
, cpuctx
, 1)) {
1916 if (event
== leader
)
1917 err
= group_sched_in(event
, cpuctx
, ctx
);
1919 err
= event_sched_in(event
, cpuctx
, ctx
);
1924 * If this event can't go on and it's part of a
1925 * group, then the whole group has to come off.
1927 if (leader
!= event
)
1928 group_sched_out(leader
, cpuctx
, ctx
);
1929 if (leader
->attr
.pinned
) {
1930 update_group_times(leader
);
1931 leader
->state
= PERF_EVENT_STATE_ERROR
;
1936 raw_spin_unlock(&ctx
->lock
);
1944 * If event->ctx is a cloned context, callers must make sure that
1945 * every task struct that event->ctx->task could possibly point to
1946 * remains valid. This condition is satisfied when called through
1947 * perf_event_for_each_child or perf_event_for_each as described
1948 * for perf_event_disable.
1950 void perf_event_enable(struct perf_event
*event
)
1952 struct perf_event_context
*ctx
= event
->ctx
;
1953 struct task_struct
*task
= ctx
->task
;
1957 * Enable the event on the cpu that it's on
1959 cpu_function_call(event
->cpu
, __perf_event_enable
, event
);
1963 raw_spin_lock_irq(&ctx
->lock
);
1964 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1968 * If the event is in error state, clear that first.
1969 * That way, if we see the event in error state below, we
1970 * know that it has gone back into error state, as distinct
1971 * from the task having been scheduled away before the
1972 * cross-call arrived.
1974 if (event
->state
== PERF_EVENT_STATE_ERROR
)
1975 event
->state
= PERF_EVENT_STATE_OFF
;
1978 if (!ctx
->is_active
) {
1979 __perf_event_mark_enabled(event
);
1983 raw_spin_unlock_irq(&ctx
->lock
);
1985 if (!task_function_call(task
, __perf_event_enable
, event
))
1988 raw_spin_lock_irq(&ctx
->lock
);
1991 * If the context is active and the event is still off,
1992 * we need to retry the cross-call.
1994 if (ctx
->is_active
&& event
->state
== PERF_EVENT_STATE_OFF
) {
1996 * task could have been flipped by a concurrent
1997 * perf_event_context_sched_out()
2004 raw_spin_unlock_irq(&ctx
->lock
);
2006 EXPORT_SYMBOL_GPL(perf_event_enable
);
2008 int perf_event_refresh(struct perf_event
*event
, int refresh
)
2011 * not supported on inherited events
2013 if (event
->attr
.inherit
|| !is_sampling_event(event
))
2016 atomic_add(refresh
, &event
->event_limit
);
2017 perf_event_enable(event
);
2021 EXPORT_SYMBOL_GPL(perf_event_refresh
);
2023 static void ctx_sched_out(struct perf_event_context
*ctx
,
2024 struct perf_cpu_context
*cpuctx
,
2025 enum event_type_t event_type
)
2027 struct perf_event
*event
;
2028 int is_active
= ctx
->is_active
;
2030 ctx
->is_active
&= ~event_type
;
2031 if (likely(!ctx
->nr_events
))
2034 update_context_time(ctx
);
2035 update_cgrp_time_from_cpuctx(cpuctx
);
2036 if (!ctx
->nr_active
)
2039 perf_pmu_disable(ctx
->pmu
);
2040 if ((is_active
& EVENT_PINNED
) && (event_type
& EVENT_PINNED
)) {
2041 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
)
2042 group_sched_out(event
, cpuctx
, ctx
);
2045 if ((is_active
& EVENT_FLEXIBLE
) && (event_type
& EVENT_FLEXIBLE
)) {
2046 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
)
2047 group_sched_out(event
, cpuctx
, ctx
);
2049 perf_pmu_enable(ctx
->pmu
);
2053 * Test whether two contexts are equivalent, i.e. whether they
2054 * have both been cloned from the same version of the same context
2055 * and they both have the same number of enabled events.
2056 * If the number of enabled events is the same, then the set
2057 * of enabled events should be the same, because these are both
2058 * inherited contexts, therefore we can't access individual events
2059 * in them directly with an fd; we can only enable/disable all
2060 * events via prctl, or enable/disable all events in a family
2061 * via ioctl, which will have the same effect on both contexts.
2063 static int context_equiv(struct perf_event_context
*ctx1
,
2064 struct perf_event_context
*ctx2
)
2066 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
2067 && ctx1
->parent_gen
== ctx2
->parent_gen
2068 && !ctx1
->pin_count
&& !ctx2
->pin_count
;
2071 static void __perf_event_sync_stat(struct perf_event
*event
,
2072 struct perf_event
*next_event
)
2076 if (!event
->attr
.inherit_stat
)
2080 * Update the event value, we cannot use perf_event_read()
2081 * because we're in the middle of a context switch and have IRQs
2082 * disabled, which upsets smp_call_function_single(), however
2083 * we know the event must be on the current CPU, therefore we
2084 * don't need to use it.
2086 switch (event
->state
) {
2087 case PERF_EVENT_STATE_ACTIVE
:
2088 event
->pmu
->read(event
);
2091 case PERF_EVENT_STATE_INACTIVE
:
2092 update_event_times(event
);
2100 * In order to keep per-task stats reliable we need to flip the event
2101 * values when we flip the contexts.
2103 value
= local64_read(&next_event
->count
);
2104 value
= local64_xchg(&event
->count
, value
);
2105 local64_set(&next_event
->count
, value
);
2107 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
2108 swap(event
->total_time_running
, next_event
->total_time_running
);
2111 * Since we swizzled the values, update the user visible data too.
2113 perf_event_update_userpage(event
);
2114 perf_event_update_userpage(next_event
);
2117 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
2118 struct perf_event_context
*next_ctx
)
2120 struct perf_event
*event
, *next_event
;
2125 update_context_time(ctx
);
2127 event
= list_first_entry(&ctx
->event_list
,
2128 struct perf_event
, event_entry
);
2130 next_event
= list_first_entry(&next_ctx
->event_list
,
2131 struct perf_event
, event_entry
);
2133 while (&event
->event_entry
!= &ctx
->event_list
&&
2134 &next_event
->event_entry
!= &next_ctx
->event_list
) {
2136 __perf_event_sync_stat(event
, next_event
);
2138 event
= list_next_entry(event
, event_entry
);
2139 next_event
= list_next_entry(next_event
, event_entry
);
2143 static void perf_event_context_sched_out(struct task_struct
*task
, int ctxn
,
2144 struct task_struct
*next
)
2146 struct perf_event_context
*ctx
= task
->perf_event_ctxp
[ctxn
];
2147 struct perf_event_context
*next_ctx
;
2148 struct perf_event_context
*parent
;
2149 struct perf_cpu_context
*cpuctx
;
2155 cpuctx
= __get_cpu_context(ctx
);
2156 if (!cpuctx
->task_ctx
)
2160 parent
= rcu_dereference(ctx
->parent_ctx
);
2161 next_ctx
= next
->perf_event_ctxp
[ctxn
];
2162 if (parent
&& next_ctx
&&
2163 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
2165 * Looks like the two contexts are clones, so we might be
2166 * able to optimize the context switch. We lock both
2167 * contexts and check that they are clones under the
2168 * lock (including re-checking that neither has been
2169 * uncloned in the meantime). It doesn't matter which
2170 * order we take the locks because no other cpu could
2171 * be trying to lock both of these tasks.
2173 raw_spin_lock(&ctx
->lock
);
2174 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
2175 if (context_equiv(ctx
, next_ctx
)) {
2177 * XXX do we need a memory barrier of sorts
2178 * wrt to rcu_dereference() of perf_event_ctxp
2180 task
->perf_event_ctxp
[ctxn
] = next_ctx
;
2181 next
->perf_event_ctxp
[ctxn
] = ctx
;
2183 next_ctx
->task
= task
;
2186 perf_event_sync_stat(ctx
, next_ctx
);
2188 raw_spin_unlock(&next_ctx
->lock
);
2189 raw_spin_unlock(&ctx
->lock
);
2194 raw_spin_lock(&ctx
->lock
);
2195 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
2196 cpuctx
->task_ctx
= NULL
;
2197 raw_spin_unlock(&ctx
->lock
);
2201 #define for_each_task_context_nr(ctxn) \
2202 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2205 * Called from scheduler to remove the events of the current task,
2206 * with interrupts disabled.
2208 * We stop each event and update the event value in event->count.
2210 * This does not protect us against NMI, but disable()
2211 * sets the disabled bit in the control field of event _before_
2212 * accessing the event control register. If a NMI hits, then it will
2213 * not restart the event.
2215 void __perf_event_task_sched_out(struct task_struct
*task
,
2216 struct task_struct
*next
)
2220 for_each_task_context_nr(ctxn
)
2221 perf_event_context_sched_out(task
, ctxn
, next
);
2224 * if cgroup events exist on this CPU, then we need
2225 * to check if we have to switch out PMU state.
2226 * cgroup event are system-wide mode only
2228 if (atomic_read(&__get_cpu_var(perf_cgroup_events
)))
2229 perf_cgroup_sched_out(task
, next
);
2232 static void task_ctx_sched_out(struct perf_event_context
*ctx
)
2234 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
2236 if (!cpuctx
->task_ctx
)
2239 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
2242 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
2243 cpuctx
->task_ctx
= NULL
;
2247 * Called with IRQs disabled
2249 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
2250 enum event_type_t event_type
)
2252 ctx_sched_out(&cpuctx
->ctx
, cpuctx
, event_type
);
2256 ctx_pinned_sched_in(struct perf_event_context
*ctx
,
2257 struct perf_cpu_context
*cpuctx
)
2259 struct perf_event
*event
;
2261 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
2262 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2264 if (!event_filter_match(event
))
2267 /* may need to reset tstamp_enabled */
2268 if (is_cgroup_event(event
))
2269 perf_cgroup_mark_enabled(event
, ctx
);
2271 if (group_can_go_on(event
, cpuctx
, 1))
2272 group_sched_in(event
, cpuctx
, ctx
);
2275 * If this pinned group hasn't been scheduled,
2276 * put it in error state.
2278 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
2279 update_group_times(event
);
2280 event
->state
= PERF_EVENT_STATE_ERROR
;
2286 ctx_flexible_sched_in(struct perf_event_context
*ctx
,
2287 struct perf_cpu_context
*cpuctx
)
2289 struct perf_event
*event
;
2292 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
2293 /* Ignore events in OFF or ERROR state */
2294 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2297 * Listen to the 'cpu' scheduling filter constraint
2300 if (!event_filter_match(event
))
2303 /* may need to reset tstamp_enabled */
2304 if (is_cgroup_event(event
))
2305 perf_cgroup_mark_enabled(event
, ctx
);
2307 if (group_can_go_on(event
, cpuctx
, can_add_hw
)) {
2308 if (group_sched_in(event
, cpuctx
, ctx
))
2315 ctx_sched_in(struct perf_event_context
*ctx
,
2316 struct perf_cpu_context
*cpuctx
,
2317 enum event_type_t event_type
,
2318 struct task_struct
*task
)
2321 int is_active
= ctx
->is_active
;
2323 ctx
->is_active
|= event_type
;
2324 if (likely(!ctx
->nr_events
))
2328 ctx
->timestamp
= now
;
2329 perf_cgroup_set_timestamp(task
, ctx
);
2331 * First go through the list and put on any pinned groups
2332 * in order to give them the best chance of going on.
2334 if (!(is_active
& EVENT_PINNED
) && (event_type
& EVENT_PINNED
))
2335 ctx_pinned_sched_in(ctx
, cpuctx
);
2337 /* Then walk through the lower prio flexible groups */
2338 if (!(is_active
& EVENT_FLEXIBLE
) && (event_type
& EVENT_FLEXIBLE
))
2339 ctx_flexible_sched_in(ctx
, cpuctx
);
2342 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
2343 enum event_type_t event_type
,
2344 struct task_struct
*task
)
2346 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
2348 ctx_sched_in(ctx
, cpuctx
, event_type
, task
);
2351 static void perf_event_context_sched_in(struct perf_event_context
*ctx
,
2352 struct task_struct
*task
)
2354 struct perf_cpu_context
*cpuctx
;
2356 cpuctx
= __get_cpu_context(ctx
);
2357 if (cpuctx
->task_ctx
== ctx
)
2360 perf_ctx_lock(cpuctx
, ctx
);
2361 perf_pmu_disable(ctx
->pmu
);
2363 * We want to keep the following priority order:
2364 * cpu pinned (that don't need to move), task pinned,
2365 * cpu flexible, task flexible.
2367 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
2370 cpuctx
->task_ctx
= ctx
;
2372 perf_event_sched_in(cpuctx
, cpuctx
->task_ctx
, task
);
2374 perf_pmu_enable(ctx
->pmu
);
2375 perf_ctx_unlock(cpuctx
, ctx
);
2378 * Since these rotations are per-cpu, we need to ensure the
2379 * cpu-context we got scheduled on is actually rotating.
2381 perf_pmu_rotate_start(ctx
->pmu
);
2385 * When sampling the branck stack in system-wide, it may be necessary
2386 * to flush the stack on context switch. This happens when the branch
2387 * stack does not tag its entries with the pid of the current task.
2388 * Otherwise it becomes impossible to associate a branch entry with a
2389 * task. This ambiguity is more likely to appear when the branch stack
2390 * supports priv level filtering and the user sets it to monitor only
2391 * at the user level (which could be a useful measurement in system-wide
2392 * mode). In that case, the risk is high of having a branch stack with
2393 * branch from multiple tasks. Flushing may mean dropping the existing
2394 * entries or stashing them somewhere in the PMU specific code layer.
2396 * This function provides the context switch callback to the lower code
2397 * layer. It is invoked ONLY when there is at least one system-wide context
2398 * with at least one active event using taken branch sampling.
2400 static void perf_branch_stack_sched_in(struct task_struct
*prev
,
2401 struct task_struct
*task
)
2403 struct perf_cpu_context
*cpuctx
;
2405 unsigned long flags
;
2407 /* no need to flush branch stack if not changing task */
2411 local_irq_save(flags
);
2415 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
2416 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
2419 * check if the context has at least one
2420 * event using PERF_SAMPLE_BRANCH_STACK
2422 if (cpuctx
->ctx
.nr_branch_stack
> 0
2423 && pmu
->flush_branch_stack
) {
2425 pmu
= cpuctx
->ctx
.pmu
;
2427 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
2429 perf_pmu_disable(pmu
);
2431 pmu
->flush_branch_stack();
2433 perf_pmu_enable(pmu
);
2435 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
2441 local_irq_restore(flags
);
2445 * Called from scheduler to add the events of the current task
2446 * with interrupts disabled.
2448 * We restore the event value and then enable it.
2450 * This does not protect us against NMI, but enable()
2451 * sets the enabled bit in the control field of event _before_
2452 * accessing the event control register. If a NMI hits, then it will
2453 * keep the event running.
2455 void __perf_event_task_sched_in(struct task_struct
*prev
,
2456 struct task_struct
*task
)
2458 struct perf_event_context
*ctx
;
2461 for_each_task_context_nr(ctxn
) {
2462 ctx
= task
->perf_event_ctxp
[ctxn
];
2466 perf_event_context_sched_in(ctx
, task
);
2469 * if cgroup events exist on this CPU, then we need
2470 * to check if we have to switch in PMU state.
2471 * cgroup event are system-wide mode only
2473 if (atomic_read(&__get_cpu_var(perf_cgroup_events
)))
2474 perf_cgroup_sched_in(prev
, task
);
2476 /* check for system-wide branch_stack events */
2477 if (atomic_read(&__get_cpu_var(perf_branch_stack_events
)))
2478 perf_branch_stack_sched_in(prev
, task
);
2481 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
2483 u64 frequency
= event
->attr
.sample_freq
;
2484 u64 sec
= NSEC_PER_SEC
;
2485 u64 divisor
, dividend
;
2487 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
2489 count_fls
= fls64(count
);
2490 nsec_fls
= fls64(nsec
);
2491 frequency_fls
= fls64(frequency
);
2495 * We got @count in @nsec, with a target of sample_freq HZ
2496 * the target period becomes:
2499 * period = -------------------
2500 * @nsec * sample_freq
2505 * Reduce accuracy by one bit such that @a and @b converge
2506 * to a similar magnitude.
2508 #define REDUCE_FLS(a, b) \
2510 if (a##_fls > b##_fls) { \
2520 * Reduce accuracy until either term fits in a u64, then proceed with
2521 * the other, so that finally we can do a u64/u64 division.
2523 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
2524 REDUCE_FLS(nsec
, frequency
);
2525 REDUCE_FLS(sec
, count
);
2528 if (count_fls
+ sec_fls
> 64) {
2529 divisor
= nsec
* frequency
;
2531 while (count_fls
+ sec_fls
> 64) {
2532 REDUCE_FLS(count
, sec
);
2536 dividend
= count
* sec
;
2538 dividend
= count
* sec
;
2540 while (nsec_fls
+ frequency_fls
> 64) {
2541 REDUCE_FLS(nsec
, frequency
);
2545 divisor
= nsec
* frequency
;
2551 return div64_u64(dividend
, divisor
);
2554 static DEFINE_PER_CPU(int, perf_throttled_count
);
2555 static DEFINE_PER_CPU(u64
, perf_throttled_seq
);
2557 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
, bool disable
)
2559 struct hw_perf_event
*hwc
= &event
->hw
;
2560 s64 period
, sample_period
;
2563 period
= perf_calculate_period(event
, nsec
, count
);
2565 delta
= (s64
)(period
- hwc
->sample_period
);
2566 delta
= (delta
+ 7) / 8; /* low pass filter */
2568 sample_period
= hwc
->sample_period
+ delta
;
2573 hwc
->sample_period
= sample_period
;
2575 if (local64_read(&hwc
->period_left
) > 8*sample_period
) {
2577 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
2579 local64_set(&hwc
->period_left
, 0);
2582 event
->pmu
->start(event
, PERF_EF_RELOAD
);
2587 * combine freq adjustment with unthrottling to avoid two passes over the
2588 * events. At the same time, make sure, having freq events does not change
2589 * the rate of unthrottling as that would introduce bias.
2591 static void perf_adjust_freq_unthr_context(struct perf_event_context
*ctx
,
2594 struct perf_event
*event
;
2595 struct hw_perf_event
*hwc
;
2596 u64 now
, period
= TICK_NSEC
;
2600 * only need to iterate over all events iff:
2601 * - context have events in frequency mode (needs freq adjust)
2602 * - there are events to unthrottle on this cpu
2604 if (!(ctx
->nr_freq
|| needs_unthr
))
2607 raw_spin_lock(&ctx
->lock
);
2608 perf_pmu_disable(ctx
->pmu
);
2610 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
2611 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
2614 if (!event_filter_match(event
))
2619 if (needs_unthr
&& hwc
->interrupts
== MAX_INTERRUPTS
) {
2620 hwc
->interrupts
= 0;
2621 perf_log_throttle(event
, 1);
2622 event
->pmu
->start(event
, 0);
2625 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
2629 * stop the event and update event->count
2631 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
2633 now
= local64_read(&event
->count
);
2634 delta
= now
- hwc
->freq_count_stamp
;
2635 hwc
->freq_count_stamp
= now
;
2639 * reload only if value has changed
2640 * we have stopped the event so tell that
2641 * to perf_adjust_period() to avoid stopping it
2645 perf_adjust_period(event
, period
, delta
, false);
2647 event
->pmu
->start(event
, delta
> 0 ? PERF_EF_RELOAD
: 0);
2650 perf_pmu_enable(ctx
->pmu
);
2651 raw_spin_unlock(&ctx
->lock
);
2655 * Round-robin a context's events:
2657 static void rotate_ctx(struct perf_event_context
*ctx
)
2660 * Rotate the first entry last of non-pinned groups. Rotation might be
2661 * disabled by the inheritance code.
2663 if (!ctx
->rotate_disable
)
2664 list_rotate_left(&ctx
->flexible_groups
);
2668 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2669 * because they're strictly cpu affine and rotate_start is called with IRQs
2670 * disabled, while rotate_context is called from IRQ context.
2672 static void perf_rotate_context(struct perf_cpu_context
*cpuctx
)
2674 struct perf_event_context
*ctx
= NULL
;
2675 int rotate
= 0, remove
= 1;
2677 if (cpuctx
->ctx
.nr_events
) {
2679 if (cpuctx
->ctx
.nr_events
!= cpuctx
->ctx
.nr_active
)
2683 ctx
= cpuctx
->task_ctx
;
2684 if (ctx
&& ctx
->nr_events
) {
2686 if (ctx
->nr_events
!= ctx
->nr_active
)
2693 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
2694 perf_pmu_disable(cpuctx
->ctx
.pmu
);
2696 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
2698 ctx_sched_out(ctx
, cpuctx
, EVENT_FLEXIBLE
);
2700 rotate_ctx(&cpuctx
->ctx
);
2704 perf_event_sched_in(cpuctx
, ctx
, current
);
2706 perf_pmu_enable(cpuctx
->ctx
.pmu
);
2707 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
2710 list_del_init(&cpuctx
->rotation_list
);
2713 #ifdef CONFIG_NO_HZ_FULL
2714 bool perf_event_can_stop_tick(void)
2716 if (list_empty(&__get_cpu_var(rotation_list
)))
2723 void perf_event_task_tick(void)
2725 struct list_head
*head
= &__get_cpu_var(rotation_list
);
2726 struct perf_cpu_context
*cpuctx
, *tmp
;
2727 struct perf_event_context
*ctx
;
2730 WARN_ON(!irqs_disabled());
2732 __this_cpu_inc(perf_throttled_seq
);
2733 throttled
= __this_cpu_xchg(perf_throttled_count
, 0);
2735 list_for_each_entry_safe(cpuctx
, tmp
, head
, rotation_list
) {
2737 perf_adjust_freq_unthr_context(ctx
, throttled
);
2739 ctx
= cpuctx
->task_ctx
;
2741 perf_adjust_freq_unthr_context(ctx
, throttled
);
2743 if (cpuctx
->jiffies_interval
== 1 ||
2744 !(jiffies
% cpuctx
->jiffies_interval
))
2745 perf_rotate_context(cpuctx
);
2749 static int event_enable_on_exec(struct perf_event
*event
,
2750 struct perf_event_context
*ctx
)
2752 if (!event
->attr
.enable_on_exec
)
2755 event
->attr
.enable_on_exec
= 0;
2756 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
2759 __perf_event_mark_enabled(event
);
2765 * Enable all of a task's events that have been marked enable-on-exec.
2766 * This expects task == current.
2768 static void perf_event_enable_on_exec(struct perf_event_context
*ctx
)
2770 struct perf_event
*event
;
2771 unsigned long flags
;
2775 local_irq_save(flags
);
2776 if (!ctx
|| !ctx
->nr_events
)
2780 * We must ctxsw out cgroup events to avoid conflict
2781 * when invoking perf_task_event_sched_in() later on
2782 * in this function. Otherwise we end up trying to
2783 * ctxswin cgroup events which are already scheduled
2786 perf_cgroup_sched_out(current
, NULL
);
2788 raw_spin_lock(&ctx
->lock
);
2789 task_ctx_sched_out(ctx
);
2791 list_for_each_entry(event
, &ctx
->event_list
, event_entry
) {
2792 ret
= event_enable_on_exec(event
, ctx
);
2798 * Unclone this context if we enabled any event.
2803 raw_spin_unlock(&ctx
->lock
);
2806 * Also calls ctxswin for cgroup events, if any:
2808 perf_event_context_sched_in(ctx
, ctx
->task
);
2810 local_irq_restore(flags
);
2814 * Cross CPU call to read the hardware event
2816 static void __perf_event_read(void *info
)
2818 struct perf_event
*event
= info
;
2819 struct perf_event_context
*ctx
= event
->ctx
;
2820 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
2823 * If this is a task context, we need to check whether it is
2824 * the current task context of this cpu. If not it has been
2825 * scheduled out before the smp call arrived. In that case
2826 * event->count would have been updated to a recent sample
2827 * when the event was scheduled out.
2829 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
2832 raw_spin_lock(&ctx
->lock
);
2833 if (ctx
->is_active
) {
2834 update_context_time(ctx
);
2835 update_cgrp_time_from_event(event
);
2837 update_event_times(event
);
2838 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
2839 event
->pmu
->read(event
);
2840 raw_spin_unlock(&ctx
->lock
);
2843 static inline u64
perf_event_count(struct perf_event
*event
)
2845 return local64_read(&event
->count
) + atomic64_read(&event
->child_count
);
2848 static u64
perf_event_read(struct perf_event
*event
)
2851 * If event is enabled and currently active on a CPU, update the
2852 * value in the event structure:
2854 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
2855 smp_call_function_single(event
->oncpu
,
2856 __perf_event_read
, event
, 1);
2857 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
2858 struct perf_event_context
*ctx
= event
->ctx
;
2859 unsigned long flags
;
2861 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
2863 * may read while context is not active
2864 * (e.g., thread is blocked), in that case
2865 * we cannot update context time
2867 if (ctx
->is_active
) {
2868 update_context_time(ctx
);
2869 update_cgrp_time_from_event(event
);
2871 update_event_times(event
);
2872 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
2875 return perf_event_count(event
);
2879 * Initialize the perf_event context in a task_struct:
2881 static void __perf_event_init_context(struct perf_event_context
*ctx
)
2883 raw_spin_lock_init(&ctx
->lock
);
2884 mutex_init(&ctx
->mutex
);
2885 INIT_LIST_HEAD(&ctx
->pinned_groups
);
2886 INIT_LIST_HEAD(&ctx
->flexible_groups
);
2887 INIT_LIST_HEAD(&ctx
->event_list
);
2888 atomic_set(&ctx
->refcount
, 1);
2891 static struct perf_event_context
*
2892 alloc_perf_context(struct pmu
*pmu
, struct task_struct
*task
)
2894 struct perf_event_context
*ctx
;
2896 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
2900 __perf_event_init_context(ctx
);
2903 get_task_struct(task
);
2910 static struct task_struct
*
2911 find_lively_task_by_vpid(pid_t vpid
)
2913 struct task_struct
*task
;
2920 task
= find_task_by_vpid(vpid
);
2922 get_task_struct(task
);
2926 return ERR_PTR(-ESRCH
);
2928 /* Reuse ptrace permission checks for now. */
2930 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
2935 put_task_struct(task
);
2936 return ERR_PTR(err
);
2941 * Returns a matching context with refcount and pincount.
2943 static struct perf_event_context
*
2944 find_get_context(struct pmu
*pmu
, struct task_struct
*task
, int cpu
)
2946 struct perf_event_context
*ctx
;
2947 struct perf_cpu_context
*cpuctx
;
2948 unsigned long flags
;
2952 /* Must be root to operate on a CPU event: */
2953 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
2954 return ERR_PTR(-EACCES
);
2957 * We could be clever and allow to attach a event to an
2958 * offline CPU and activate it when the CPU comes up, but
2961 if (!cpu_online(cpu
))
2962 return ERR_PTR(-ENODEV
);
2964 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
2973 ctxn
= pmu
->task_ctx_nr
;
2978 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
2982 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
2984 ctx
= alloc_perf_context(pmu
, task
);
2990 mutex_lock(&task
->perf_event_mutex
);
2992 * If it has already passed perf_event_exit_task().
2993 * we must see PF_EXITING, it takes this mutex too.
2995 if (task
->flags
& PF_EXITING
)
2997 else if (task
->perf_event_ctxp
[ctxn
])
3002 rcu_assign_pointer(task
->perf_event_ctxp
[ctxn
], ctx
);
3004 mutex_unlock(&task
->perf_event_mutex
);
3006 if (unlikely(err
)) {
3018 return ERR_PTR(err
);
3021 static void perf_event_free_filter(struct perf_event
*event
);
3023 static void free_event_rcu(struct rcu_head
*head
)
3025 struct perf_event
*event
;
3027 event
= container_of(head
, struct perf_event
, rcu_head
);
3029 put_pid_ns(event
->ns
);
3030 perf_event_free_filter(event
);
3034 static void ring_buffer_put(struct ring_buffer
*rb
);
3035 static void ring_buffer_detach(struct perf_event
*event
, struct ring_buffer
*rb
);
3037 static void free_event(struct perf_event
*event
)
3039 irq_work_sync(&event
->pending
);
3041 if (!event
->parent
) {
3042 if (event
->attach_state
& PERF_ATTACH_TASK
)
3043 static_key_slow_dec_deferred(&perf_sched_events
);
3044 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
3045 atomic_dec(&nr_mmap_events
);
3046 if (event
->attr
.comm
)
3047 atomic_dec(&nr_comm_events
);
3048 if (event
->attr
.task
)
3049 atomic_dec(&nr_task_events
);
3050 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
)
3051 put_callchain_buffers();
3052 if (is_cgroup_event(event
)) {
3053 atomic_dec(&per_cpu(perf_cgroup_events
, event
->cpu
));
3054 static_key_slow_dec_deferred(&perf_sched_events
);
3057 if (has_branch_stack(event
)) {
3058 static_key_slow_dec_deferred(&perf_sched_events
);
3059 /* is system-wide event */
3060 if (!(event
->attach_state
& PERF_ATTACH_TASK
)) {
3061 atomic_dec(&per_cpu(perf_branch_stack_events
,
3068 struct ring_buffer
*rb
;
3071 * Can happen when we close an event with re-directed output.
3073 * Since we have a 0 refcount, perf_mmap_close() will skip
3074 * over us; possibly making our ring_buffer_put() the last.
3076 mutex_lock(&event
->mmap_mutex
);
3079 rcu_assign_pointer(event
->rb
, NULL
);
3080 ring_buffer_detach(event
, rb
);
3081 ring_buffer_put(rb
); /* could be last */
3083 mutex_unlock(&event
->mmap_mutex
);
3086 if (is_cgroup_event(event
))
3087 perf_detach_cgroup(event
);
3090 event
->destroy(event
);
3093 put_ctx(event
->ctx
);
3095 call_rcu(&event
->rcu_head
, free_event_rcu
);
3098 int perf_event_release_kernel(struct perf_event
*event
)
3100 struct perf_event_context
*ctx
= event
->ctx
;
3102 WARN_ON_ONCE(ctx
->parent_ctx
);
3104 * There are two ways this annotation is useful:
3106 * 1) there is a lock recursion from perf_event_exit_task
3107 * see the comment there.
3109 * 2) there is a lock-inversion with mmap_sem through
3110 * perf_event_read_group(), which takes faults while
3111 * holding ctx->mutex, however this is called after
3112 * the last filedesc died, so there is no possibility
3113 * to trigger the AB-BA case.
3115 mutex_lock_nested(&ctx
->mutex
, SINGLE_DEPTH_NESTING
);
3116 perf_remove_from_context(event
, true);
3117 mutex_unlock(&ctx
->mutex
);
3123 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
3126 * Called when the last reference to the file is gone.
3128 static void put_event(struct perf_event
*event
)
3130 struct task_struct
*owner
;
3132 if (!atomic_long_dec_and_test(&event
->refcount
))
3136 owner
= ACCESS_ONCE(event
->owner
);
3138 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3139 * !owner it means the list deletion is complete and we can indeed
3140 * free this event, otherwise we need to serialize on
3141 * owner->perf_event_mutex.
3143 smp_read_barrier_depends();
3146 * Since delayed_put_task_struct() also drops the last
3147 * task reference we can safely take a new reference
3148 * while holding the rcu_read_lock().
3150 get_task_struct(owner
);
3155 mutex_lock(&owner
->perf_event_mutex
);
3157 * We have to re-check the event->owner field, if it is cleared
3158 * we raced with perf_event_exit_task(), acquiring the mutex
3159 * ensured they're done, and we can proceed with freeing the
3163 list_del_init(&event
->owner_entry
);
3164 mutex_unlock(&owner
->perf_event_mutex
);
3165 put_task_struct(owner
);
3168 perf_event_release_kernel(event
);
3171 static int perf_release(struct inode
*inode
, struct file
*file
)
3173 put_event(file
->private_data
);
3177 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
3179 struct perf_event
*child
;
3185 mutex_lock(&event
->child_mutex
);
3186 total
+= perf_event_read(event
);
3187 *enabled
+= event
->total_time_enabled
+
3188 atomic64_read(&event
->child_total_time_enabled
);
3189 *running
+= event
->total_time_running
+
3190 atomic64_read(&event
->child_total_time_running
);
3192 list_for_each_entry(child
, &event
->child_list
, child_list
) {
3193 total
+= perf_event_read(child
);
3194 *enabled
+= child
->total_time_enabled
;
3195 *running
+= child
->total_time_running
;
3197 mutex_unlock(&event
->child_mutex
);
3201 EXPORT_SYMBOL_GPL(perf_event_read_value
);
3203 static int perf_event_read_group(struct perf_event
*event
,
3204 u64 read_format
, char __user
*buf
)
3206 struct perf_event
*leader
= event
->group_leader
, *sub
;
3207 int n
= 0, size
= 0, ret
= -EFAULT
;
3208 struct perf_event_context
*ctx
= leader
->ctx
;
3210 u64 count
, enabled
, running
;
3212 mutex_lock(&ctx
->mutex
);
3213 count
= perf_event_read_value(leader
, &enabled
, &running
);
3215 values
[n
++] = 1 + leader
->nr_siblings
;
3216 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3217 values
[n
++] = enabled
;
3218 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3219 values
[n
++] = running
;
3220 values
[n
++] = count
;
3221 if (read_format
& PERF_FORMAT_ID
)
3222 values
[n
++] = primary_event_id(leader
);
3224 size
= n
* sizeof(u64
);
3226 if (copy_to_user(buf
, values
, size
))
3231 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3234 values
[n
++] = perf_event_read_value(sub
, &enabled
, &running
);
3235 if (read_format
& PERF_FORMAT_ID
)
3236 values
[n
++] = primary_event_id(sub
);
3238 size
= n
* sizeof(u64
);
3240 if (copy_to_user(buf
+ ret
, values
, size
)) {
3248 mutex_unlock(&ctx
->mutex
);
3253 static int perf_event_read_one(struct perf_event
*event
,
3254 u64 read_format
, char __user
*buf
)
3256 u64 enabled
, running
;
3260 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
3261 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3262 values
[n
++] = enabled
;
3263 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3264 values
[n
++] = running
;
3265 if (read_format
& PERF_FORMAT_ID
)
3266 values
[n
++] = primary_event_id(event
);
3268 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
3271 return n
* sizeof(u64
);
3275 * Read the performance event - simple non blocking version for now
3278 perf_read_hw(struct perf_event
*event
, char __user
*buf
, size_t count
)
3280 u64 read_format
= event
->attr
.read_format
;
3284 * Return end-of-file for a read on a event that is in
3285 * error state (i.e. because it was pinned but it couldn't be
3286 * scheduled on to the CPU at some point).
3288 if (event
->state
== PERF_EVENT_STATE_ERROR
)
3291 if (count
< event
->read_size
)
3294 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3295 if (read_format
& PERF_FORMAT_GROUP
)
3296 ret
= perf_event_read_group(event
, read_format
, buf
);
3298 ret
= perf_event_read_one(event
, read_format
, buf
);
3304 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
3306 struct perf_event
*event
= file
->private_data
;
3308 return perf_read_hw(event
, buf
, count
);
3311 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
3313 struct perf_event
*event
= file
->private_data
;
3314 struct ring_buffer
*rb
;
3315 unsigned int events
= POLL_HUP
;
3318 * Pin the event->rb by taking event->mmap_mutex; otherwise
3319 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
3321 mutex_lock(&event
->mmap_mutex
);
3324 events
= atomic_xchg(&rb
->poll
, 0);
3325 mutex_unlock(&event
->mmap_mutex
);
3327 poll_wait(file
, &event
->waitq
, wait
);
3332 static void perf_event_reset(struct perf_event
*event
)
3334 (void)perf_event_read(event
);
3335 local64_set(&event
->count
, 0);
3336 perf_event_update_userpage(event
);
3340 * Holding the top-level event's child_mutex means that any
3341 * descendant process that has inherited this event will block
3342 * in sync_child_event if it goes to exit, thus satisfying the
3343 * task existence requirements of perf_event_enable/disable.
3345 static void perf_event_for_each_child(struct perf_event
*event
,
3346 void (*func
)(struct perf_event
*))
3348 struct perf_event
*child
;
3350 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3351 mutex_lock(&event
->child_mutex
);
3353 list_for_each_entry(child
, &event
->child_list
, child_list
)
3355 mutex_unlock(&event
->child_mutex
);
3358 static void perf_event_for_each(struct perf_event
*event
,
3359 void (*func
)(struct perf_event
*))
3361 struct perf_event_context
*ctx
= event
->ctx
;
3362 struct perf_event
*sibling
;
3364 WARN_ON_ONCE(ctx
->parent_ctx
);
3365 mutex_lock(&ctx
->mutex
);
3366 event
= event
->group_leader
;
3368 perf_event_for_each_child(event
, func
);
3369 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
3370 perf_event_for_each_child(sibling
, func
);
3371 mutex_unlock(&ctx
->mutex
);
3374 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
3376 struct perf_event_context
*ctx
= event
->ctx
;
3380 if (!is_sampling_event(event
))
3383 if (copy_from_user(&value
, arg
, sizeof(value
)))
3389 raw_spin_lock_irq(&ctx
->lock
);
3390 if (event
->attr
.freq
) {
3391 if (value
> sysctl_perf_event_sample_rate
) {
3396 event
->attr
.sample_freq
= value
;
3398 event
->attr
.sample_period
= value
;
3399 event
->hw
.sample_period
= value
;
3402 raw_spin_unlock_irq(&ctx
->lock
);
3407 static const struct file_operations perf_fops
;
3409 static inline int perf_fget_light(int fd
, struct fd
*p
)
3411 struct fd f
= fdget(fd
);
3415 if (f
.file
->f_op
!= &perf_fops
) {
3423 static int perf_event_set_output(struct perf_event
*event
,
3424 struct perf_event
*output_event
);
3425 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
3427 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
3429 struct perf_event
*event
= file
->private_data
;
3430 void (*func
)(struct perf_event
*);
3434 case PERF_EVENT_IOC_ENABLE
:
3435 func
= perf_event_enable
;
3437 case PERF_EVENT_IOC_DISABLE
:
3438 func
= perf_event_disable
;
3440 case PERF_EVENT_IOC_RESET
:
3441 func
= perf_event_reset
;
3444 case PERF_EVENT_IOC_REFRESH
:
3445 return perf_event_refresh(event
, arg
);
3447 case PERF_EVENT_IOC_PERIOD
:
3448 return perf_event_period(event
, (u64 __user
*)arg
);
3450 case PERF_EVENT_IOC_SET_OUTPUT
:
3454 struct perf_event
*output_event
;
3456 ret
= perf_fget_light(arg
, &output
);
3459 output_event
= output
.file
->private_data
;
3460 ret
= perf_event_set_output(event
, output_event
);
3463 ret
= perf_event_set_output(event
, NULL
);
3468 case PERF_EVENT_IOC_SET_FILTER
:
3469 return perf_event_set_filter(event
, (void __user
*)arg
);
3475 if (flags
& PERF_IOC_FLAG_GROUP
)
3476 perf_event_for_each(event
, func
);
3478 perf_event_for_each_child(event
, func
);
3483 int perf_event_task_enable(void)
3485 struct perf_event
*event
;
3487 mutex_lock(¤t
->perf_event_mutex
);
3488 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
3489 perf_event_for_each_child(event
, perf_event_enable
);
3490 mutex_unlock(¤t
->perf_event_mutex
);
3495 int perf_event_task_disable(void)
3497 struct perf_event
*event
;
3499 mutex_lock(¤t
->perf_event_mutex
);
3500 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
3501 perf_event_for_each_child(event
, perf_event_disable
);
3502 mutex_unlock(¤t
->perf_event_mutex
);
3507 static int perf_event_index(struct perf_event
*event
)
3509 if (event
->hw
.state
& PERF_HES_STOPPED
)
3512 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
3515 return event
->pmu
->event_idx(event
);
3518 static void calc_timer_values(struct perf_event
*event
,
3525 *now
= perf_clock();
3526 ctx_time
= event
->shadow_ctx_time
+ *now
;
3527 *enabled
= ctx_time
- event
->tstamp_enabled
;
3528 *running
= ctx_time
- event
->tstamp_running
;
3531 void __weak
arch_perf_update_userpage(struct perf_event_mmap_page
*userpg
, u64 now
)
3536 * Callers need to ensure there can be no nesting of this function, otherwise
3537 * the seqlock logic goes bad. We can not serialize this because the arch
3538 * code calls this from NMI context.
3540 void perf_event_update_userpage(struct perf_event
*event
)
3542 struct perf_event_mmap_page
*userpg
;
3543 struct ring_buffer
*rb
;
3544 u64 enabled
, running
, now
;
3548 * compute total_time_enabled, total_time_running
3549 * based on snapshot values taken when the event
3550 * was last scheduled in.
3552 * we cannot simply called update_context_time()
3553 * because of locking issue as we can be called in
3556 calc_timer_values(event
, &now
, &enabled
, &running
);
3557 rb
= rcu_dereference(event
->rb
);
3561 userpg
= rb
->user_page
;
3564 * Disable preemption so as to not let the corresponding user-space
3565 * spin too long if we get preempted.
3570 userpg
->index
= perf_event_index(event
);
3571 userpg
->offset
= perf_event_count(event
);
3573 userpg
->offset
-= local64_read(&event
->hw
.prev_count
);
3575 userpg
->time_enabled
= enabled
+
3576 atomic64_read(&event
->child_total_time_enabled
);
3578 userpg
->time_running
= running
+
3579 atomic64_read(&event
->child_total_time_running
);
3581 arch_perf_update_userpage(userpg
, now
);
3590 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
3592 struct perf_event
*event
= vma
->vm_file
->private_data
;
3593 struct ring_buffer
*rb
;
3594 int ret
= VM_FAULT_SIGBUS
;
3596 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
3597 if (vmf
->pgoff
== 0)
3603 rb
= rcu_dereference(event
->rb
);
3607 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
3610 vmf
->page
= perf_mmap_to_page(rb
, vmf
->pgoff
);
3614 get_page(vmf
->page
);
3615 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
3616 vmf
->page
->index
= vmf
->pgoff
;
3625 static void ring_buffer_attach(struct perf_event
*event
,
3626 struct ring_buffer
*rb
)
3628 unsigned long flags
;
3630 if (!list_empty(&event
->rb_entry
))
3633 spin_lock_irqsave(&rb
->event_lock
, flags
);
3634 if (list_empty(&event
->rb_entry
))
3635 list_add(&event
->rb_entry
, &rb
->event_list
);
3636 spin_unlock_irqrestore(&rb
->event_lock
, flags
);
3639 static void ring_buffer_detach(struct perf_event
*event
, struct ring_buffer
*rb
)
3641 unsigned long flags
;
3643 if (list_empty(&event
->rb_entry
))
3646 spin_lock_irqsave(&rb
->event_lock
, flags
);
3647 list_del_init(&event
->rb_entry
);
3648 wake_up_all(&event
->waitq
);
3649 spin_unlock_irqrestore(&rb
->event_lock
, flags
);
3652 static void ring_buffer_wakeup(struct perf_event
*event
)
3654 struct ring_buffer
*rb
;
3657 rb
= rcu_dereference(event
->rb
);
3659 list_for_each_entry_rcu(event
, &rb
->event_list
, rb_entry
)
3660 wake_up_all(&event
->waitq
);
3665 static void rb_free_rcu(struct rcu_head
*rcu_head
)
3667 struct ring_buffer
*rb
;
3669 rb
= container_of(rcu_head
, struct ring_buffer
, rcu_head
);
3673 static struct ring_buffer
*ring_buffer_get(struct perf_event
*event
)
3675 struct ring_buffer
*rb
;
3678 rb
= rcu_dereference(event
->rb
);
3680 if (!atomic_inc_not_zero(&rb
->refcount
))
3688 static void ring_buffer_put(struct ring_buffer
*rb
)
3690 if (!atomic_dec_and_test(&rb
->refcount
))
3693 WARN_ON_ONCE(!list_empty(&rb
->event_list
));
3695 call_rcu(&rb
->rcu_head
, rb_free_rcu
);
3698 static void perf_mmap_open(struct vm_area_struct
*vma
)
3700 struct perf_event
*event
= vma
->vm_file
->private_data
;
3702 atomic_inc(&event
->mmap_count
);
3703 atomic_inc(&event
->rb
->mmap_count
);
3707 * A buffer can be mmap()ed multiple times; either directly through the same
3708 * event, or through other events by use of perf_event_set_output().
3710 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3711 * the buffer here, where we still have a VM context. This means we need
3712 * to detach all events redirecting to us.
3714 static void perf_mmap_close(struct vm_area_struct
*vma
)
3716 struct perf_event
*event
= vma
->vm_file
->private_data
;
3718 struct ring_buffer
*rb
= event
->rb
;
3719 struct user_struct
*mmap_user
= rb
->mmap_user
;
3720 int mmap_locked
= rb
->mmap_locked
;
3721 unsigned long size
= perf_data_size(rb
);
3723 atomic_dec(&rb
->mmap_count
);
3725 if (!atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
))
3728 /* Detach current event from the buffer. */
3729 rcu_assign_pointer(event
->rb
, NULL
);
3730 ring_buffer_detach(event
, rb
);
3731 mutex_unlock(&event
->mmap_mutex
);
3733 /* If there's still other mmap()s of this buffer, we're done. */
3734 if (atomic_read(&rb
->mmap_count
)) {
3735 ring_buffer_put(rb
); /* can't be last */
3740 * No other mmap()s, detach from all other events that might redirect
3741 * into the now unreachable buffer. Somewhat complicated by the
3742 * fact that rb::event_lock otherwise nests inside mmap_mutex.
3746 list_for_each_entry_rcu(event
, &rb
->event_list
, rb_entry
) {
3747 if (!atomic_long_inc_not_zero(&event
->refcount
)) {
3749 * This event is en-route to free_event() which will
3750 * detach it and remove it from the list.
3756 mutex_lock(&event
->mmap_mutex
);
3758 * Check we didn't race with perf_event_set_output() which can
3759 * swizzle the rb from under us while we were waiting to
3760 * acquire mmap_mutex.
3762 * If we find a different rb; ignore this event, a next
3763 * iteration will no longer find it on the list. We have to
3764 * still restart the iteration to make sure we're not now
3765 * iterating the wrong list.
3767 if (event
->rb
== rb
) {
3768 rcu_assign_pointer(event
->rb
, NULL
);
3769 ring_buffer_detach(event
, rb
);
3770 ring_buffer_put(rb
); /* can't be last, we still have one */
3772 mutex_unlock(&event
->mmap_mutex
);
3776 * Restart the iteration; either we're on the wrong list or
3777 * destroyed its integrity by doing a deletion.
3784 * It could be there's still a few 0-ref events on the list; they'll
3785 * get cleaned up by free_event() -- they'll also still have their
3786 * ref on the rb and will free it whenever they are done with it.
3788 * Aside from that, this buffer is 'fully' detached and unmapped,
3789 * undo the VM accounting.
3792 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &mmap_user
->locked_vm
);
3793 vma
->vm_mm
->pinned_vm
-= mmap_locked
;
3794 free_uid(mmap_user
);
3796 ring_buffer_put(rb
); /* could be last */
3799 static const struct vm_operations_struct perf_mmap_vmops
= {
3800 .open
= perf_mmap_open
,
3801 .close
= perf_mmap_close
,
3802 .fault
= perf_mmap_fault
,
3803 .page_mkwrite
= perf_mmap_fault
,
3806 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
3808 struct perf_event
*event
= file
->private_data
;
3809 unsigned long user_locked
, user_lock_limit
;
3810 struct user_struct
*user
= current_user();
3811 unsigned long locked
, lock_limit
;
3812 struct ring_buffer
*rb
;
3813 unsigned long vma_size
;
3814 unsigned long nr_pages
;
3815 long user_extra
, extra
;
3816 int ret
= 0, flags
= 0;
3819 * Don't allow mmap() of inherited per-task counters. This would
3820 * create a performance issue due to all children writing to the
3823 if (event
->cpu
== -1 && event
->attr
.inherit
)
3826 if (!(vma
->vm_flags
& VM_SHARED
))
3829 vma_size
= vma
->vm_end
- vma
->vm_start
;
3830 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
3833 * If we have rb pages ensure they're a power-of-two number, so we
3834 * can do bitmasks instead of modulo.
3836 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
3839 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
3842 if (vma
->vm_pgoff
!= 0)
3845 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3847 mutex_lock(&event
->mmap_mutex
);
3849 if (event
->rb
->nr_pages
!= nr_pages
) {
3854 if (!atomic_inc_not_zero(&event
->rb
->mmap_count
)) {
3856 * Raced against perf_mmap_close() through
3857 * perf_event_set_output(). Try again, hope for better
3860 mutex_unlock(&event
->mmap_mutex
);
3867 user_extra
= nr_pages
+ 1;
3868 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
3871 * Increase the limit linearly with more CPUs:
3873 user_lock_limit
*= num_online_cpus();
3875 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
3878 if (user_locked
> user_lock_limit
)
3879 extra
= user_locked
- user_lock_limit
;
3881 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
3882 lock_limit
>>= PAGE_SHIFT
;
3883 locked
= vma
->vm_mm
->pinned_vm
+ extra
;
3885 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
3886 !capable(CAP_IPC_LOCK
)) {
3893 if (vma
->vm_flags
& VM_WRITE
)
3894 flags
|= RING_BUFFER_WRITABLE
;
3896 rb
= rb_alloc(nr_pages
,
3897 event
->attr
.watermark
? event
->attr
.wakeup_watermark
: 0,
3905 atomic_set(&rb
->mmap_count
, 1);
3906 rb
->mmap_locked
= extra
;
3907 rb
->mmap_user
= get_current_user();
3909 atomic_long_add(user_extra
, &user
->locked_vm
);
3910 vma
->vm_mm
->pinned_vm
+= extra
;
3912 ring_buffer_attach(event
, rb
);
3913 rcu_assign_pointer(event
->rb
, rb
);
3915 perf_event_update_userpage(event
);
3919 atomic_inc(&event
->mmap_count
);
3920 mutex_unlock(&event
->mmap_mutex
);
3923 * Since pinned accounting is per vm we cannot allow fork() to copy our
3926 vma
->vm_flags
|= VM_DONTCOPY
| VM_DONTEXPAND
| VM_DONTDUMP
;
3927 vma
->vm_ops
= &perf_mmap_vmops
;
3932 static int perf_fasync(int fd
, struct file
*filp
, int on
)
3934 struct inode
*inode
= file_inode(filp
);
3935 struct perf_event
*event
= filp
->private_data
;
3938 mutex_lock(&inode
->i_mutex
);
3939 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
3940 mutex_unlock(&inode
->i_mutex
);
3948 static const struct file_operations perf_fops
= {
3949 .llseek
= no_llseek
,
3950 .release
= perf_release
,
3953 .unlocked_ioctl
= perf_ioctl
,
3954 .compat_ioctl
= perf_ioctl
,
3956 .fasync
= perf_fasync
,
3962 * If there's data, ensure we set the poll() state and publish everything
3963 * to user-space before waking everybody up.
3966 void perf_event_wakeup(struct perf_event
*event
)
3968 ring_buffer_wakeup(event
);
3970 if (event
->pending_kill
) {
3971 kill_fasync(&event
->fasync
, SIGIO
, event
->pending_kill
);
3972 event
->pending_kill
= 0;
3976 static void perf_pending_event(struct irq_work
*entry
)
3978 struct perf_event
*event
= container_of(entry
,
3979 struct perf_event
, pending
);
3981 if (event
->pending_disable
) {
3982 event
->pending_disable
= 0;
3983 __perf_event_disable(event
);
3986 if (event
->pending_wakeup
) {
3987 event
->pending_wakeup
= 0;
3988 perf_event_wakeup(event
);
3993 * We assume there is only KVM supporting the callbacks.
3994 * Later on, we might change it to a list if there is
3995 * another virtualization implementation supporting the callbacks.
3997 struct perf_guest_info_callbacks
*perf_guest_cbs
;
3999 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
4001 perf_guest_cbs
= cbs
;
4004 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks
);
4006 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
4008 perf_guest_cbs
= NULL
;
4011 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks
);
4014 perf_output_sample_regs(struct perf_output_handle
*handle
,
4015 struct pt_regs
*regs
, u64 mask
)
4019 for_each_set_bit(bit
, (const unsigned long *) &mask
,
4020 sizeof(mask
) * BITS_PER_BYTE
) {
4023 val
= perf_reg_value(regs
, bit
);
4024 perf_output_put(handle
, val
);
4028 static void perf_sample_regs_user(struct perf_regs_user
*regs_user
,
4029 struct pt_regs
*regs
)
4031 if (!user_mode(regs
)) {
4033 regs
= task_pt_regs(current
);
4039 regs_user
->regs
= regs
;
4040 regs_user
->abi
= perf_reg_abi(current
);
4045 * Get remaining task size from user stack pointer.
4047 * It'd be better to take stack vma map and limit this more
4048 * precisly, but there's no way to get it safely under interrupt,
4049 * so using TASK_SIZE as limit.
4051 static u64
perf_ustack_task_size(struct pt_regs
*regs
)
4053 unsigned long addr
= perf_user_stack_pointer(regs
);
4055 if (!addr
|| addr
>= TASK_SIZE
)
4058 return TASK_SIZE
- addr
;
4062 perf_sample_ustack_size(u16 stack_size
, u16 header_size
,
4063 struct pt_regs
*regs
)
4067 /* No regs, no stack pointer, no dump. */
4072 * Check if we fit in with the requested stack size into the:
4074 * If we don't, we limit the size to the TASK_SIZE.
4076 * - remaining sample size
4077 * If we don't, we customize the stack size to
4078 * fit in to the remaining sample size.
4081 task_size
= min((u64
) USHRT_MAX
, perf_ustack_task_size(regs
));
4082 stack_size
= min(stack_size
, (u16
) task_size
);
4084 /* Current header size plus static size and dynamic size. */
4085 header_size
+= 2 * sizeof(u64
);
4087 /* Do we fit in with the current stack dump size? */
4088 if ((u16
) (header_size
+ stack_size
) < header_size
) {
4090 * If we overflow the maximum size for the sample,
4091 * we customize the stack dump size to fit in.
4093 stack_size
= USHRT_MAX
- header_size
- sizeof(u64
);
4094 stack_size
= round_up(stack_size
, sizeof(u64
));
4101 perf_output_sample_ustack(struct perf_output_handle
*handle
, u64 dump_size
,
4102 struct pt_regs
*regs
)
4104 /* Case of a kernel thread, nothing to dump */
4107 perf_output_put(handle
, size
);
4116 * - the size requested by user or the best one we can fit
4117 * in to the sample max size
4119 * - user stack dump data
4121 * - the actual dumped size
4125 perf_output_put(handle
, dump_size
);
4128 sp
= perf_user_stack_pointer(regs
);
4129 rem
= __output_copy_user(handle
, (void *) sp
, dump_size
);
4130 dyn_size
= dump_size
- rem
;
4132 perf_output_skip(handle
, rem
);
4135 perf_output_put(handle
, dyn_size
);
4139 static void __perf_event_header__init_id(struct perf_event_header
*header
,
4140 struct perf_sample_data
*data
,
4141 struct perf_event
*event
)
4143 u64 sample_type
= event
->attr
.sample_type
;
4145 data
->type
= sample_type
;
4146 header
->size
+= event
->id_header_size
;
4148 if (sample_type
& PERF_SAMPLE_TID
) {
4149 /* namespace issues */
4150 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
4151 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
4154 if (sample_type
& PERF_SAMPLE_TIME
)
4155 data
->time
= perf_clock();
4157 if (sample_type
& PERF_SAMPLE_ID
)
4158 data
->id
= primary_event_id(event
);
4160 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4161 data
->stream_id
= event
->id
;
4163 if (sample_type
& PERF_SAMPLE_CPU
) {
4164 data
->cpu_entry
.cpu
= raw_smp_processor_id();
4165 data
->cpu_entry
.reserved
= 0;
4169 void perf_event_header__init_id(struct perf_event_header
*header
,
4170 struct perf_sample_data
*data
,
4171 struct perf_event
*event
)
4173 if (event
->attr
.sample_id_all
)
4174 __perf_event_header__init_id(header
, data
, event
);
4177 static void __perf_event__output_id_sample(struct perf_output_handle
*handle
,
4178 struct perf_sample_data
*data
)
4180 u64 sample_type
= data
->type
;
4182 if (sample_type
& PERF_SAMPLE_TID
)
4183 perf_output_put(handle
, data
->tid_entry
);
4185 if (sample_type
& PERF_SAMPLE_TIME
)
4186 perf_output_put(handle
, data
->time
);
4188 if (sample_type
& PERF_SAMPLE_ID
)
4189 perf_output_put(handle
, data
->id
);
4191 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4192 perf_output_put(handle
, data
->stream_id
);
4194 if (sample_type
& PERF_SAMPLE_CPU
)
4195 perf_output_put(handle
, data
->cpu_entry
);
4198 void perf_event__output_id_sample(struct perf_event
*event
,
4199 struct perf_output_handle
*handle
,
4200 struct perf_sample_data
*sample
)
4202 if (event
->attr
.sample_id_all
)
4203 __perf_event__output_id_sample(handle
, sample
);
4206 static void perf_output_read_one(struct perf_output_handle
*handle
,
4207 struct perf_event
*event
,
4208 u64 enabled
, u64 running
)
4210 u64 read_format
= event
->attr
.read_format
;
4214 values
[n
++] = perf_event_count(event
);
4215 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
4216 values
[n
++] = enabled
+
4217 atomic64_read(&event
->child_total_time_enabled
);
4219 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
4220 values
[n
++] = running
+
4221 atomic64_read(&event
->child_total_time_running
);
4223 if (read_format
& PERF_FORMAT_ID
)
4224 values
[n
++] = primary_event_id(event
);
4226 __output_copy(handle
, values
, n
* sizeof(u64
));
4230 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4232 static void perf_output_read_group(struct perf_output_handle
*handle
,
4233 struct perf_event
*event
,
4234 u64 enabled
, u64 running
)
4236 struct perf_event
*leader
= event
->group_leader
, *sub
;
4237 u64 read_format
= event
->attr
.read_format
;
4241 values
[n
++] = 1 + leader
->nr_siblings
;
4243 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
4244 values
[n
++] = enabled
;
4246 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
4247 values
[n
++] = running
;
4249 if (leader
!= event
)
4250 leader
->pmu
->read(leader
);
4252 values
[n
++] = perf_event_count(leader
);
4253 if (read_format
& PERF_FORMAT_ID
)
4254 values
[n
++] = primary_event_id(leader
);
4256 __output_copy(handle
, values
, n
* sizeof(u64
));
4258 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
4262 sub
->pmu
->read(sub
);
4264 values
[n
++] = perf_event_count(sub
);
4265 if (read_format
& PERF_FORMAT_ID
)
4266 values
[n
++] = primary_event_id(sub
);
4268 __output_copy(handle
, values
, n
* sizeof(u64
));
4272 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4273 PERF_FORMAT_TOTAL_TIME_RUNNING)
4275 static void perf_output_read(struct perf_output_handle
*handle
,
4276 struct perf_event
*event
)
4278 u64 enabled
= 0, running
= 0, now
;
4279 u64 read_format
= event
->attr
.read_format
;
4282 * compute total_time_enabled, total_time_running
4283 * based on snapshot values taken when the event
4284 * was last scheduled in.
4286 * we cannot simply called update_context_time()
4287 * because of locking issue as we are called in
4290 if (read_format
& PERF_FORMAT_TOTAL_TIMES
)
4291 calc_timer_values(event
, &now
, &enabled
, &running
);
4293 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
4294 perf_output_read_group(handle
, event
, enabled
, running
);
4296 perf_output_read_one(handle
, event
, enabled
, running
);
4299 void perf_output_sample(struct perf_output_handle
*handle
,
4300 struct perf_event_header
*header
,
4301 struct perf_sample_data
*data
,
4302 struct perf_event
*event
)
4304 u64 sample_type
= data
->type
;
4306 perf_output_put(handle
, *header
);
4308 if (sample_type
& PERF_SAMPLE_IP
)
4309 perf_output_put(handle
, data
->ip
);
4311 if (sample_type
& PERF_SAMPLE_TID
)
4312 perf_output_put(handle
, data
->tid_entry
);
4314 if (sample_type
& PERF_SAMPLE_TIME
)
4315 perf_output_put(handle
, data
->time
);
4317 if (sample_type
& PERF_SAMPLE_ADDR
)
4318 perf_output_put(handle
, data
->addr
);
4320 if (sample_type
& PERF_SAMPLE_ID
)
4321 perf_output_put(handle
, data
->id
);
4323 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4324 perf_output_put(handle
, data
->stream_id
);
4326 if (sample_type
& PERF_SAMPLE_CPU
)
4327 perf_output_put(handle
, data
->cpu_entry
);
4329 if (sample_type
& PERF_SAMPLE_PERIOD
)
4330 perf_output_put(handle
, data
->period
);
4332 if (sample_type
& PERF_SAMPLE_READ
)
4333 perf_output_read(handle
, event
);
4335 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
4336 if (data
->callchain
) {
4339 if (data
->callchain
)
4340 size
+= data
->callchain
->nr
;
4342 size
*= sizeof(u64
);
4344 __output_copy(handle
, data
->callchain
, size
);
4347 perf_output_put(handle
, nr
);
4351 if (sample_type
& PERF_SAMPLE_RAW
) {
4353 perf_output_put(handle
, data
->raw
->size
);
4354 __output_copy(handle
, data
->raw
->data
,
4361 .size
= sizeof(u32
),
4364 perf_output_put(handle
, raw
);
4368 if (!event
->attr
.watermark
) {
4369 int wakeup_events
= event
->attr
.wakeup_events
;
4371 if (wakeup_events
) {
4372 struct ring_buffer
*rb
= handle
->rb
;
4373 int events
= local_inc_return(&rb
->events
);
4375 if (events
>= wakeup_events
) {
4376 local_sub(wakeup_events
, &rb
->events
);
4377 local_inc(&rb
->wakeup
);
4382 if (sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
4383 if (data
->br_stack
) {
4386 size
= data
->br_stack
->nr
4387 * sizeof(struct perf_branch_entry
);
4389 perf_output_put(handle
, data
->br_stack
->nr
);
4390 perf_output_copy(handle
, data
->br_stack
->entries
, size
);
4393 * we always store at least the value of nr
4396 perf_output_put(handle
, nr
);
4400 if (sample_type
& PERF_SAMPLE_REGS_USER
) {
4401 u64 abi
= data
->regs_user
.abi
;
4404 * If there are no regs to dump, notice it through
4405 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4407 perf_output_put(handle
, abi
);
4410 u64 mask
= event
->attr
.sample_regs_user
;
4411 perf_output_sample_regs(handle
,
4412 data
->regs_user
.regs
,
4417 if (sample_type
& PERF_SAMPLE_STACK_USER
)
4418 perf_output_sample_ustack(handle
,
4419 data
->stack_user_size
,
4420 data
->regs_user
.regs
);
4422 if (sample_type
& PERF_SAMPLE_WEIGHT
)
4423 perf_output_put(handle
, data
->weight
);
4425 if (sample_type
& PERF_SAMPLE_DATA_SRC
)
4426 perf_output_put(handle
, data
->data_src
.val
);
4429 void perf_prepare_sample(struct perf_event_header
*header
,
4430 struct perf_sample_data
*data
,
4431 struct perf_event
*event
,
4432 struct pt_regs
*regs
)
4434 u64 sample_type
= event
->attr
.sample_type
;
4436 header
->type
= PERF_RECORD_SAMPLE
;
4437 header
->size
= sizeof(*header
) + event
->header_size
;
4440 header
->misc
|= perf_misc_flags(regs
);
4442 __perf_event_header__init_id(header
, data
, event
);
4444 if (sample_type
& PERF_SAMPLE_IP
)
4445 data
->ip
= perf_instruction_pointer(regs
);
4447 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
4450 data
->callchain
= perf_callchain(event
, regs
);
4452 if (data
->callchain
)
4453 size
+= data
->callchain
->nr
;
4455 header
->size
+= size
* sizeof(u64
);
4458 if (sample_type
& PERF_SAMPLE_RAW
) {
4459 int size
= sizeof(u32
);
4462 size
+= data
->raw
->size
;
4464 size
+= sizeof(u32
);
4466 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
4467 header
->size
+= size
;
4470 if (sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
4471 int size
= sizeof(u64
); /* nr */
4472 if (data
->br_stack
) {
4473 size
+= data
->br_stack
->nr
4474 * sizeof(struct perf_branch_entry
);
4476 header
->size
+= size
;
4479 if (sample_type
& PERF_SAMPLE_REGS_USER
) {
4480 /* regs dump ABI info */
4481 int size
= sizeof(u64
);
4483 perf_sample_regs_user(&data
->regs_user
, regs
);
4485 if (data
->regs_user
.regs
) {
4486 u64 mask
= event
->attr
.sample_regs_user
;
4487 size
+= hweight64(mask
) * sizeof(u64
);
4490 header
->size
+= size
;
4493 if (sample_type
& PERF_SAMPLE_STACK_USER
) {
4495 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4496 * processed as the last one or have additional check added
4497 * in case new sample type is added, because we could eat
4498 * up the rest of the sample size.
4500 struct perf_regs_user
*uregs
= &data
->regs_user
;
4501 u16 stack_size
= event
->attr
.sample_stack_user
;
4502 u16 size
= sizeof(u64
);
4505 perf_sample_regs_user(uregs
, regs
);
4507 stack_size
= perf_sample_ustack_size(stack_size
, header
->size
,
4511 * If there is something to dump, add space for the dump
4512 * itself and for the field that tells the dynamic size,
4513 * which is how many have been actually dumped.
4516 size
+= sizeof(u64
) + stack_size
;
4518 data
->stack_user_size
= stack_size
;
4519 header
->size
+= size
;
4523 static void perf_event_output(struct perf_event
*event
,
4524 struct perf_sample_data
*data
,
4525 struct pt_regs
*regs
)
4527 struct perf_output_handle handle
;
4528 struct perf_event_header header
;
4530 /* protect the callchain buffers */
4533 perf_prepare_sample(&header
, data
, event
, regs
);
4535 if (perf_output_begin(&handle
, event
, header
.size
))
4538 perf_output_sample(&handle
, &header
, data
, event
);
4540 perf_output_end(&handle
);
4550 struct perf_read_event
{
4551 struct perf_event_header header
;
4558 perf_event_read_event(struct perf_event
*event
,
4559 struct task_struct
*task
)
4561 struct perf_output_handle handle
;
4562 struct perf_sample_data sample
;
4563 struct perf_read_event read_event
= {
4565 .type
= PERF_RECORD_READ
,
4567 .size
= sizeof(read_event
) + event
->read_size
,
4569 .pid
= perf_event_pid(event
, task
),
4570 .tid
= perf_event_tid(event
, task
),
4574 perf_event_header__init_id(&read_event
.header
, &sample
, event
);
4575 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
);
4579 perf_output_put(&handle
, read_event
);
4580 perf_output_read(&handle
, event
);
4581 perf_event__output_id_sample(event
, &handle
, &sample
);
4583 perf_output_end(&handle
);
4586 typedef int (perf_event_aux_match_cb
)(struct perf_event
*event
, void *data
);
4587 typedef void (perf_event_aux_output_cb
)(struct perf_event
*event
, void *data
);
4590 perf_event_aux_ctx(struct perf_event_context
*ctx
,
4591 perf_event_aux_match_cb match
,
4592 perf_event_aux_output_cb output
,
4595 struct perf_event
*event
;
4597 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
4598 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
4600 if (!event_filter_match(event
))
4602 if (match(event
, data
))
4603 output(event
, data
);
4608 perf_event_aux(perf_event_aux_match_cb match
,
4609 perf_event_aux_output_cb output
,
4611 struct perf_event_context
*task_ctx
)
4613 struct perf_cpu_context
*cpuctx
;
4614 struct perf_event_context
*ctx
;
4619 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
4620 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
4621 if (cpuctx
->unique_pmu
!= pmu
)
4623 perf_event_aux_ctx(&cpuctx
->ctx
, match
, output
, data
);
4626 ctxn
= pmu
->task_ctx_nr
;
4629 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
4631 perf_event_aux_ctx(ctx
, match
, output
, data
);
4633 put_cpu_ptr(pmu
->pmu_cpu_context
);
4638 perf_event_aux_ctx(task_ctx
, match
, output
, data
);
4645 * task tracking -- fork/exit
4647 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4650 struct perf_task_event
{
4651 struct task_struct
*task
;
4652 struct perf_event_context
*task_ctx
;
4655 struct perf_event_header header
;
4665 static void perf_event_task_output(struct perf_event
*event
,
4668 struct perf_task_event
*task_event
= data
;
4669 struct perf_output_handle handle
;
4670 struct perf_sample_data sample
;
4671 struct task_struct
*task
= task_event
->task
;
4672 int ret
, size
= task_event
->event_id
.header
.size
;
4674 perf_event_header__init_id(&task_event
->event_id
.header
, &sample
, event
);
4676 ret
= perf_output_begin(&handle
, event
,
4677 task_event
->event_id
.header
.size
);
4681 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
4682 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
4684 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
4685 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
4687 perf_output_put(&handle
, task_event
->event_id
);
4689 perf_event__output_id_sample(event
, &handle
, &sample
);
4691 perf_output_end(&handle
);
4693 task_event
->event_id
.header
.size
= size
;
4696 static int perf_event_task_match(struct perf_event
*event
,
4697 void *data __maybe_unused
)
4699 return event
->attr
.comm
|| event
->attr
.mmap
||
4700 event
->attr
.mmap_data
|| event
->attr
.task
;
4703 static void perf_event_task(struct task_struct
*task
,
4704 struct perf_event_context
*task_ctx
,
4707 struct perf_task_event task_event
;
4709 if (!atomic_read(&nr_comm_events
) &&
4710 !atomic_read(&nr_mmap_events
) &&
4711 !atomic_read(&nr_task_events
))
4714 task_event
= (struct perf_task_event
){
4716 .task_ctx
= task_ctx
,
4719 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
4721 .size
= sizeof(task_event
.event_id
),
4727 .time
= perf_clock(),
4731 perf_event_aux(perf_event_task_match
,
4732 perf_event_task_output
,
4737 void perf_event_fork(struct task_struct
*task
)
4739 perf_event_task(task
, NULL
, 1);
4746 struct perf_comm_event
{
4747 struct task_struct
*task
;
4752 struct perf_event_header header
;
4759 static void perf_event_comm_output(struct perf_event
*event
,
4762 struct perf_comm_event
*comm_event
= data
;
4763 struct perf_output_handle handle
;
4764 struct perf_sample_data sample
;
4765 int size
= comm_event
->event_id
.header
.size
;
4768 perf_event_header__init_id(&comm_event
->event_id
.header
, &sample
, event
);
4769 ret
= perf_output_begin(&handle
, event
,
4770 comm_event
->event_id
.header
.size
);
4775 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
4776 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
4778 perf_output_put(&handle
, comm_event
->event_id
);
4779 __output_copy(&handle
, comm_event
->comm
,
4780 comm_event
->comm_size
);
4782 perf_event__output_id_sample(event
, &handle
, &sample
);
4784 perf_output_end(&handle
);
4786 comm_event
->event_id
.header
.size
= size
;
4789 static int perf_event_comm_match(struct perf_event
*event
,
4790 void *data __maybe_unused
)
4792 return event
->attr
.comm
;
4795 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
4797 char comm
[TASK_COMM_LEN
];
4800 memset(comm
, 0, sizeof(comm
));
4801 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
4802 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
4804 comm_event
->comm
= comm
;
4805 comm_event
->comm_size
= size
;
4807 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
4809 perf_event_aux(perf_event_comm_match
,
4810 perf_event_comm_output
,
4815 void perf_event_comm(struct task_struct
*task
)
4817 struct perf_comm_event comm_event
;
4818 struct perf_event_context
*ctx
;
4822 for_each_task_context_nr(ctxn
) {
4823 ctx
= task
->perf_event_ctxp
[ctxn
];
4827 perf_event_enable_on_exec(ctx
);
4831 if (!atomic_read(&nr_comm_events
))
4834 comm_event
= (struct perf_comm_event
){
4840 .type
= PERF_RECORD_COMM
,
4849 perf_event_comm_event(&comm_event
);
4856 struct perf_mmap_event
{
4857 struct vm_area_struct
*vma
;
4859 const char *file_name
;
4863 struct perf_event_header header
;
4873 static void perf_event_mmap_output(struct perf_event
*event
,
4876 struct perf_mmap_event
*mmap_event
= data
;
4877 struct perf_output_handle handle
;
4878 struct perf_sample_data sample
;
4879 int size
= mmap_event
->event_id
.header
.size
;
4882 perf_event_header__init_id(&mmap_event
->event_id
.header
, &sample
, event
);
4883 ret
= perf_output_begin(&handle
, event
,
4884 mmap_event
->event_id
.header
.size
);
4888 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
4889 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
4891 perf_output_put(&handle
, mmap_event
->event_id
);
4892 __output_copy(&handle
, mmap_event
->file_name
,
4893 mmap_event
->file_size
);
4895 perf_event__output_id_sample(event
, &handle
, &sample
);
4897 perf_output_end(&handle
);
4899 mmap_event
->event_id
.header
.size
= size
;
4902 static int perf_event_mmap_match(struct perf_event
*event
,
4905 struct perf_mmap_event
*mmap_event
= data
;
4906 struct vm_area_struct
*vma
= mmap_event
->vma
;
4907 int executable
= vma
->vm_flags
& VM_EXEC
;
4909 return (!executable
&& event
->attr
.mmap_data
) ||
4910 (executable
&& event
->attr
.mmap
);
4913 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
4915 struct vm_area_struct
*vma
= mmap_event
->vma
;
4916 struct file
*file
= vma
->vm_file
;
4922 memset(tmp
, 0, sizeof(tmp
));
4926 * d_path works from the end of the rb backwards, so we
4927 * need to add enough zero bytes after the string to handle
4928 * the 64bit alignment we do later.
4930 buf
= kzalloc(PATH_MAX
+ sizeof(u64
), GFP_KERNEL
);
4932 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
4935 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
4937 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
4941 if (arch_vma_name(mmap_event
->vma
)) {
4942 name
= strncpy(tmp
, arch_vma_name(mmap_event
->vma
),
4944 tmp
[sizeof(tmp
) - 1] = '\0';
4949 name
= strncpy(tmp
, "[vdso]", sizeof(tmp
));
4951 } else if (vma
->vm_start
<= vma
->vm_mm
->start_brk
&&
4952 vma
->vm_end
>= vma
->vm_mm
->brk
) {
4953 name
= strncpy(tmp
, "[heap]", sizeof(tmp
));
4955 } else if (vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
4956 vma
->vm_end
>= vma
->vm_mm
->start_stack
) {
4957 name
= strncpy(tmp
, "[stack]", sizeof(tmp
));
4961 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
4966 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
4968 mmap_event
->file_name
= name
;
4969 mmap_event
->file_size
= size
;
4971 if (!(vma
->vm_flags
& VM_EXEC
))
4972 mmap_event
->event_id
.header
.misc
|= PERF_RECORD_MISC_MMAP_DATA
;
4974 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
4976 perf_event_aux(perf_event_mmap_match
,
4977 perf_event_mmap_output
,
4984 void perf_event_mmap(struct vm_area_struct
*vma
)
4986 struct perf_mmap_event mmap_event
;
4988 if (!atomic_read(&nr_mmap_events
))
4991 mmap_event
= (struct perf_mmap_event
){
4997 .type
= PERF_RECORD_MMAP
,
4998 .misc
= PERF_RECORD_MISC_USER
,
5003 .start
= vma
->vm_start
,
5004 .len
= vma
->vm_end
- vma
->vm_start
,
5005 .pgoff
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
,
5009 perf_event_mmap_event(&mmap_event
);
5013 * IRQ throttle logging
5016 static void perf_log_throttle(struct perf_event
*event
, int enable
)
5018 struct perf_output_handle handle
;
5019 struct perf_sample_data sample
;
5023 struct perf_event_header header
;
5027 } throttle_event
= {
5029 .type
= PERF_RECORD_THROTTLE
,
5031 .size
= sizeof(throttle_event
),
5033 .time
= perf_clock(),
5034 .id
= primary_event_id(event
),
5035 .stream_id
= event
->id
,
5039 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
5041 perf_event_header__init_id(&throttle_event
.header
, &sample
, event
);
5043 ret
= perf_output_begin(&handle
, event
,
5044 throttle_event
.header
.size
);
5048 perf_output_put(&handle
, throttle_event
);
5049 perf_event__output_id_sample(event
, &handle
, &sample
);
5050 perf_output_end(&handle
);
5054 * Generic event overflow handling, sampling.
5057 static int __perf_event_overflow(struct perf_event
*event
,
5058 int throttle
, struct perf_sample_data
*data
,
5059 struct pt_regs
*regs
)
5061 int events
= atomic_read(&event
->event_limit
);
5062 struct hw_perf_event
*hwc
= &event
->hw
;
5067 * Non-sampling counters might still use the PMI to fold short
5068 * hardware counters, ignore those.
5070 if (unlikely(!is_sampling_event(event
)))
5073 seq
= __this_cpu_read(perf_throttled_seq
);
5074 if (seq
!= hwc
->interrupts_seq
) {
5075 hwc
->interrupts_seq
= seq
;
5076 hwc
->interrupts
= 1;
5079 if (unlikely(throttle
5080 && hwc
->interrupts
>= max_samples_per_tick
)) {
5081 __this_cpu_inc(perf_throttled_count
);
5082 hwc
->interrupts
= MAX_INTERRUPTS
;
5083 perf_log_throttle(event
, 0);
5088 if (event
->attr
.freq
) {
5089 u64 now
= perf_clock();
5090 s64 delta
= now
- hwc
->freq_time_stamp
;
5092 hwc
->freq_time_stamp
= now
;
5094 if (delta
> 0 && delta
< 2*TICK_NSEC
)
5095 perf_adjust_period(event
, delta
, hwc
->last_period
, true);
5099 * XXX event_limit might not quite work as expected on inherited
5103 event
->pending_kill
= POLL_IN
;
5104 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
5106 event
->pending_kill
= POLL_HUP
;
5107 event
->pending_disable
= 1;
5108 irq_work_queue(&event
->pending
);
5111 if (event
->overflow_handler
)
5112 event
->overflow_handler(event
, data
, regs
);
5114 perf_event_output(event
, data
, regs
);
5116 if (event
->fasync
&& event
->pending_kill
) {
5117 event
->pending_wakeup
= 1;
5118 irq_work_queue(&event
->pending
);
5124 int perf_event_overflow(struct perf_event
*event
,
5125 struct perf_sample_data
*data
,
5126 struct pt_regs
*regs
)
5128 return __perf_event_overflow(event
, 1, data
, regs
);
5132 * Generic software event infrastructure
5135 struct swevent_htable
{
5136 struct swevent_hlist
*swevent_hlist
;
5137 struct mutex hlist_mutex
;
5140 /* Recursion avoidance in each contexts */
5141 int recursion
[PERF_NR_CONTEXTS
];
5143 /* Keeps track of cpu being initialized/exited */
5147 static DEFINE_PER_CPU(struct swevent_htable
, swevent_htable
);
5150 * We directly increment event->count and keep a second value in
5151 * event->hw.period_left to count intervals. This period event
5152 * is kept in the range [-sample_period, 0] so that we can use the
5156 static u64
perf_swevent_set_period(struct perf_event
*event
)
5158 struct hw_perf_event
*hwc
= &event
->hw
;
5159 u64 period
= hwc
->last_period
;
5163 hwc
->last_period
= hwc
->sample_period
;
5166 old
= val
= local64_read(&hwc
->period_left
);
5170 nr
= div64_u64(period
+ val
, period
);
5171 offset
= nr
* period
;
5173 if (local64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
5179 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
5180 struct perf_sample_data
*data
,
5181 struct pt_regs
*regs
)
5183 struct hw_perf_event
*hwc
= &event
->hw
;
5187 overflow
= perf_swevent_set_period(event
);
5189 if (hwc
->interrupts
== MAX_INTERRUPTS
)
5192 for (; overflow
; overflow
--) {
5193 if (__perf_event_overflow(event
, throttle
,
5196 * We inhibit the overflow from happening when
5197 * hwc->interrupts == MAX_INTERRUPTS.
5205 static void perf_swevent_event(struct perf_event
*event
, u64 nr
,
5206 struct perf_sample_data
*data
,
5207 struct pt_regs
*regs
)
5209 struct hw_perf_event
*hwc
= &event
->hw
;
5211 local64_add(nr
, &event
->count
);
5216 if (!is_sampling_event(event
))
5219 if ((event
->attr
.sample_type
& PERF_SAMPLE_PERIOD
) && !event
->attr
.freq
) {
5221 return perf_swevent_overflow(event
, 1, data
, regs
);
5223 data
->period
= event
->hw
.last_period
;
5225 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
5226 return perf_swevent_overflow(event
, 1, data
, regs
);
5228 if (local64_add_negative(nr
, &hwc
->period_left
))
5231 perf_swevent_overflow(event
, 0, data
, regs
);
5234 static int perf_exclude_event(struct perf_event
*event
,
5235 struct pt_regs
*regs
)
5237 if (event
->hw
.state
& PERF_HES_STOPPED
)
5241 if (event
->attr
.exclude_user
&& user_mode(regs
))
5244 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
5251 static int perf_swevent_match(struct perf_event
*event
,
5252 enum perf_type_id type
,
5254 struct perf_sample_data
*data
,
5255 struct pt_regs
*regs
)
5257 if (event
->attr
.type
!= type
)
5260 if (event
->attr
.config
!= event_id
)
5263 if (perf_exclude_event(event
, regs
))
5269 static inline u64
swevent_hash(u64 type
, u32 event_id
)
5271 u64 val
= event_id
| (type
<< 32);
5273 return hash_64(val
, SWEVENT_HLIST_BITS
);
5276 static inline struct hlist_head
*
5277 __find_swevent_head(struct swevent_hlist
*hlist
, u64 type
, u32 event_id
)
5279 u64 hash
= swevent_hash(type
, event_id
);
5281 return &hlist
->heads
[hash
];
5284 /* For the read side: events when they trigger */
5285 static inline struct hlist_head
*
5286 find_swevent_head_rcu(struct swevent_htable
*swhash
, u64 type
, u32 event_id
)
5288 struct swevent_hlist
*hlist
;
5290 hlist
= rcu_dereference(swhash
->swevent_hlist
);
5294 return __find_swevent_head(hlist
, type
, event_id
);
5297 /* For the event head insertion and removal in the hlist */
5298 static inline struct hlist_head
*
5299 find_swevent_head(struct swevent_htable
*swhash
, struct perf_event
*event
)
5301 struct swevent_hlist
*hlist
;
5302 u32 event_id
= event
->attr
.config
;
5303 u64 type
= event
->attr
.type
;
5306 * Event scheduling is always serialized against hlist allocation
5307 * and release. Which makes the protected version suitable here.
5308 * The context lock guarantees that.
5310 hlist
= rcu_dereference_protected(swhash
->swevent_hlist
,
5311 lockdep_is_held(&event
->ctx
->lock
));
5315 return __find_swevent_head(hlist
, type
, event_id
);
5318 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
5320 struct perf_sample_data
*data
,
5321 struct pt_regs
*regs
)
5323 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5324 struct perf_event
*event
;
5325 struct hlist_head
*head
;
5328 head
= find_swevent_head_rcu(swhash
, type
, event_id
);
5332 hlist_for_each_entry_rcu(event
, head
, hlist_entry
) {
5333 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
5334 perf_swevent_event(event
, nr
, data
, regs
);
5340 int perf_swevent_get_recursion_context(void)
5342 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5344 return get_recursion_context(swhash
->recursion
);
5346 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
5348 inline void perf_swevent_put_recursion_context(int rctx
)
5350 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5352 put_recursion_context(swhash
->recursion
, rctx
);
5355 void __perf_sw_event(u32 event_id
, u64 nr
, struct pt_regs
*regs
, u64 addr
)
5357 struct perf_sample_data data
;
5360 preempt_disable_notrace();
5361 rctx
= perf_swevent_get_recursion_context();
5365 perf_sample_data_init(&data
, addr
, 0);
5367 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, &data
, regs
);
5369 perf_swevent_put_recursion_context(rctx
);
5370 preempt_enable_notrace();
5373 static void perf_swevent_read(struct perf_event
*event
)
5377 static int perf_swevent_add(struct perf_event
*event
, int flags
)
5379 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5380 struct hw_perf_event
*hwc
= &event
->hw
;
5381 struct hlist_head
*head
;
5383 if (is_sampling_event(event
)) {
5384 hwc
->last_period
= hwc
->sample_period
;
5385 perf_swevent_set_period(event
);
5388 hwc
->state
= !(flags
& PERF_EF_START
);
5390 head
= find_swevent_head(swhash
, event
);
5393 * We can race with cpu hotplug code. Do not
5394 * WARN if the cpu just got unplugged.
5396 WARN_ON_ONCE(swhash
->online
);
5400 hlist_add_head_rcu(&event
->hlist_entry
, head
);
5405 static void perf_swevent_del(struct perf_event
*event
, int flags
)
5407 hlist_del_rcu(&event
->hlist_entry
);
5410 static void perf_swevent_start(struct perf_event
*event
, int flags
)
5412 event
->hw
.state
= 0;
5415 static void perf_swevent_stop(struct perf_event
*event
, int flags
)
5417 event
->hw
.state
= PERF_HES_STOPPED
;
5420 /* Deref the hlist from the update side */
5421 static inline struct swevent_hlist
*
5422 swevent_hlist_deref(struct swevent_htable
*swhash
)
5424 return rcu_dereference_protected(swhash
->swevent_hlist
,
5425 lockdep_is_held(&swhash
->hlist_mutex
));
5428 static void swevent_hlist_release(struct swevent_htable
*swhash
)
5430 struct swevent_hlist
*hlist
= swevent_hlist_deref(swhash
);
5435 rcu_assign_pointer(swhash
->swevent_hlist
, NULL
);
5436 kfree_rcu(hlist
, rcu_head
);
5439 static void swevent_hlist_put_cpu(struct perf_event
*event
, int cpu
)
5441 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
5443 mutex_lock(&swhash
->hlist_mutex
);
5445 if (!--swhash
->hlist_refcount
)
5446 swevent_hlist_release(swhash
);
5448 mutex_unlock(&swhash
->hlist_mutex
);
5451 static void swevent_hlist_put(struct perf_event
*event
)
5455 if (event
->cpu
!= -1) {
5456 swevent_hlist_put_cpu(event
, event
->cpu
);
5460 for_each_possible_cpu(cpu
)
5461 swevent_hlist_put_cpu(event
, cpu
);
5464 static int swevent_hlist_get_cpu(struct perf_event
*event
, int cpu
)
5466 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
5469 mutex_lock(&swhash
->hlist_mutex
);
5471 if (!swevent_hlist_deref(swhash
) && cpu_online(cpu
)) {
5472 struct swevent_hlist
*hlist
;
5474 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
5479 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
5481 swhash
->hlist_refcount
++;
5483 mutex_unlock(&swhash
->hlist_mutex
);
5488 static int swevent_hlist_get(struct perf_event
*event
)
5491 int cpu
, failed_cpu
;
5493 if (event
->cpu
!= -1)
5494 return swevent_hlist_get_cpu(event
, event
->cpu
);
5497 for_each_possible_cpu(cpu
) {
5498 err
= swevent_hlist_get_cpu(event
, cpu
);
5508 for_each_possible_cpu(cpu
) {
5509 if (cpu
== failed_cpu
)
5511 swevent_hlist_put_cpu(event
, cpu
);
5518 struct static_key perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
5520 static void sw_perf_event_destroy(struct perf_event
*event
)
5522 u64 event_id
= event
->attr
.config
;
5524 WARN_ON(event
->parent
);
5526 static_key_slow_dec(&perf_swevent_enabled
[event_id
]);
5527 swevent_hlist_put(event
);
5530 static int perf_swevent_init(struct perf_event
*event
)
5532 u64 event_id
= event
->attr
.config
;
5534 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
5538 * no branch sampling for software events
5540 if (has_branch_stack(event
))
5544 case PERF_COUNT_SW_CPU_CLOCK
:
5545 case PERF_COUNT_SW_TASK_CLOCK
:
5552 if (event_id
>= PERF_COUNT_SW_MAX
)
5555 if (!event
->parent
) {
5558 err
= swevent_hlist_get(event
);
5562 static_key_slow_inc(&perf_swevent_enabled
[event_id
]);
5563 event
->destroy
= sw_perf_event_destroy
;
5569 static int perf_swevent_event_idx(struct perf_event
*event
)
5574 static struct pmu perf_swevent
= {
5575 .task_ctx_nr
= perf_sw_context
,
5577 .event_init
= perf_swevent_init
,
5578 .add
= perf_swevent_add
,
5579 .del
= perf_swevent_del
,
5580 .start
= perf_swevent_start
,
5581 .stop
= perf_swevent_stop
,
5582 .read
= perf_swevent_read
,
5584 .event_idx
= perf_swevent_event_idx
,
5587 #ifdef CONFIG_EVENT_TRACING
5589 static int perf_tp_filter_match(struct perf_event
*event
,
5590 struct perf_sample_data
*data
)
5592 void *record
= data
->raw
->data
;
5594 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
5599 static int perf_tp_event_match(struct perf_event
*event
,
5600 struct perf_sample_data
*data
,
5601 struct pt_regs
*regs
)
5603 if (event
->hw
.state
& PERF_HES_STOPPED
)
5606 * All tracepoints are from kernel-space.
5608 if (event
->attr
.exclude_kernel
)
5611 if (!perf_tp_filter_match(event
, data
))
5617 void perf_tp_event(u64 addr
, u64 count
, void *record
, int entry_size
,
5618 struct pt_regs
*regs
, struct hlist_head
*head
, int rctx
,
5619 struct task_struct
*task
)
5621 struct perf_sample_data data
;
5622 struct perf_event
*event
;
5624 struct perf_raw_record raw
= {
5629 perf_sample_data_init(&data
, addr
, 0);
5632 hlist_for_each_entry_rcu(event
, head
, hlist_entry
) {
5633 if (perf_tp_event_match(event
, &data
, regs
))
5634 perf_swevent_event(event
, count
, &data
, regs
);
5638 * If we got specified a target task, also iterate its context and
5639 * deliver this event there too.
5641 if (task
&& task
!= current
) {
5642 struct perf_event_context
*ctx
;
5643 struct trace_entry
*entry
= record
;
5646 ctx
= rcu_dereference(task
->perf_event_ctxp
[perf_sw_context
]);
5650 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
5651 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
5653 if (event
->attr
.config
!= entry
->type
)
5655 if (perf_tp_event_match(event
, &data
, regs
))
5656 perf_swevent_event(event
, count
, &data
, regs
);
5662 perf_swevent_put_recursion_context(rctx
);
5664 EXPORT_SYMBOL_GPL(perf_tp_event
);
5666 static void tp_perf_event_destroy(struct perf_event
*event
)
5668 perf_trace_destroy(event
);
5671 static int perf_tp_event_init(struct perf_event
*event
)
5675 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
5679 * no branch sampling for tracepoint events
5681 if (has_branch_stack(event
))
5684 err
= perf_trace_init(event
);
5688 event
->destroy
= tp_perf_event_destroy
;
5693 static struct pmu perf_tracepoint
= {
5694 .task_ctx_nr
= perf_sw_context
,
5696 .event_init
= perf_tp_event_init
,
5697 .add
= perf_trace_add
,
5698 .del
= perf_trace_del
,
5699 .start
= perf_swevent_start
,
5700 .stop
= perf_swevent_stop
,
5701 .read
= perf_swevent_read
,
5703 .event_idx
= perf_swevent_event_idx
,
5706 static inline void perf_tp_register(void)
5708 perf_pmu_register(&perf_tracepoint
, "tracepoint", PERF_TYPE_TRACEPOINT
);
5711 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
5716 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
5719 filter_str
= strndup_user(arg
, PAGE_SIZE
);
5720 if (IS_ERR(filter_str
))
5721 return PTR_ERR(filter_str
);
5723 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
, filter_str
);
5729 static void perf_event_free_filter(struct perf_event
*event
)
5731 ftrace_profile_free_filter(event
);
5736 static inline void perf_tp_register(void)
5740 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
5745 static void perf_event_free_filter(struct perf_event
*event
)
5749 #endif /* CONFIG_EVENT_TRACING */
5751 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5752 void perf_bp_event(struct perf_event
*bp
, void *data
)
5754 struct perf_sample_data sample
;
5755 struct pt_regs
*regs
= data
;
5757 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
, 0);
5759 if (!bp
->hw
.state
&& !perf_exclude_event(bp
, regs
))
5760 perf_swevent_event(bp
, 1, &sample
, regs
);
5765 * hrtimer based swevent callback
5768 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
5770 enum hrtimer_restart ret
= HRTIMER_RESTART
;
5771 struct perf_sample_data data
;
5772 struct pt_regs
*regs
;
5773 struct perf_event
*event
;
5776 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
5778 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
5779 return HRTIMER_NORESTART
;
5781 event
->pmu
->read(event
);
5783 perf_sample_data_init(&data
, 0, event
->hw
.last_period
);
5784 regs
= get_irq_regs();
5786 if (regs
&& !perf_exclude_event(event
, regs
)) {
5787 if (!(event
->attr
.exclude_idle
&& is_idle_task(current
)))
5788 if (__perf_event_overflow(event
, 1, &data
, regs
))
5789 ret
= HRTIMER_NORESTART
;
5792 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
5793 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
5798 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
5800 struct hw_perf_event
*hwc
= &event
->hw
;
5803 if (!is_sampling_event(event
))
5806 period
= local64_read(&hwc
->period_left
);
5811 local64_set(&hwc
->period_left
, 0);
5813 period
= max_t(u64
, 10000, hwc
->sample_period
);
5815 __hrtimer_start_range_ns(&hwc
->hrtimer
,
5816 ns_to_ktime(period
), 0,
5817 HRTIMER_MODE_REL_PINNED
, 0);
5820 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
5822 struct hw_perf_event
*hwc
= &event
->hw
;
5824 if (is_sampling_event(event
)) {
5825 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
5826 local64_set(&hwc
->period_left
, ktime_to_ns(remaining
));
5828 hrtimer_cancel(&hwc
->hrtimer
);
5832 static void perf_swevent_init_hrtimer(struct perf_event
*event
)
5834 struct hw_perf_event
*hwc
= &event
->hw
;
5836 if (!is_sampling_event(event
))
5839 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
5840 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
5843 * Since hrtimers have a fixed rate, we can do a static freq->period
5844 * mapping and avoid the whole period adjust feedback stuff.
5846 if (event
->attr
.freq
) {
5847 long freq
= event
->attr
.sample_freq
;
5849 event
->attr
.sample_period
= NSEC_PER_SEC
/ freq
;
5850 hwc
->sample_period
= event
->attr
.sample_period
;
5851 local64_set(&hwc
->period_left
, hwc
->sample_period
);
5852 hwc
->last_period
= hwc
->sample_period
;
5853 event
->attr
.freq
= 0;
5858 * Software event: cpu wall time clock
5861 static void cpu_clock_event_update(struct perf_event
*event
)
5866 now
= local_clock();
5867 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
5868 local64_add(now
- prev
, &event
->count
);
5871 static void cpu_clock_event_start(struct perf_event
*event
, int flags
)
5873 local64_set(&event
->hw
.prev_count
, local_clock());
5874 perf_swevent_start_hrtimer(event
);
5877 static void cpu_clock_event_stop(struct perf_event
*event
, int flags
)
5879 perf_swevent_cancel_hrtimer(event
);
5880 cpu_clock_event_update(event
);
5883 static int cpu_clock_event_add(struct perf_event
*event
, int flags
)
5885 if (flags
& PERF_EF_START
)
5886 cpu_clock_event_start(event
, flags
);
5891 static void cpu_clock_event_del(struct perf_event
*event
, int flags
)
5893 cpu_clock_event_stop(event
, flags
);
5896 static void cpu_clock_event_read(struct perf_event
*event
)
5898 cpu_clock_event_update(event
);
5901 static int cpu_clock_event_init(struct perf_event
*event
)
5903 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
5906 if (event
->attr
.config
!= PERF_COUNT_SW_CPU_CLOCK
)
5910 * no branch sampling for software events
5912 if (has_branch_stack(event
))
5915 perf_swevent_init_hrtimer(event
);
5920 static struct pmu perf_cpu_clock
= {
5921 .task_ctx_nr
= perf_sw_context
,
5923 .event_init
= cpu_clock_event_init
,
5924 .add
= cpu_clock_event_add
,
5925 .del
= cpu_clock_event_del
,
5926 .start
= cpu_clock_event_start
,
5927 .stop
= cpu_clock_event_stop
,
5928 .read
= cpu_clock_event_read
,
5930 .event_idx
= perf_swevent_event_idx
,
5934 * Software event: task time clock
5937 static void task_clock_event_update(struct perf_event
*event
, u64 now
)
5942 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
5944 local64_add(delta
, &event
->count
);
5947 static void task_clock_event_start(struct perf_event
*event
, int flags
)
5949 local64_set(&event
->hw
.prev_count
, event
->ctx
->time
);
5950 perf_swevent_start_hrtimer(event
);
5953 static void task_clock_event_stop(struct perf_event
*event
, int flags
)
5955 perf_swevent_cancel_hrtimer(event
);
5956 task_clock_event_update(event
, event
->ctx
->time
);
5959 static int task_clock_event_add(struct perf_event
*event
, int flags
)
5961 if (flags
& PERF_EF_START
)
5962 task_clock_event_start(event
, flags
);
5967 static void task_clock_event_del(struct perf_event
*event
, int flags
)
5969 task_clock_event_stop(event
, PERF_EF_UPDATE
);
5972 static void task_clock_event_read(struct perf_event
*event
)
5974 u64 now
= perf_clock();
5975 u64 delta
= now
- event
->ctx
->timestamp
;
5976 u64 time
= event
->ctx
->time
+ delta
;
5978 task_clock_event_update(event
, time
);
5981 static int task_clock_event_init(struct perf_event
*event
)
5983 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
5986 if (event
->attr
.config
!= PERF_COUNT_SW_TASK_CLOCK
)
5990 * no branch sampling for software events
5992 if (has_branch_stack(event
))
5995 perf_swevent_init_hrtimer(event
);
6000 static struct pmu perf_task_clock
= {
6001 .task_ctx_nr
= perf_sw_context
,
6003 .event_init
= task_clock_event_init
,
6004 .add
= task_clock_event_add
,
6005 .del
= task_clock_event_del
,
6006 .start
= task_clock_event_start
,
6007 .stop
= task_clock_event_stop
,
6008 .read
= task_clock_event_read
,
6010 .event_idx
= perf_swevent_event_idx
,
6013 static void perf_pmu_nop_void(struct pmu
*pmu
)
6017 static int perf_pmu_nop_int(struct pmu
*pmu
)
6022 static void perf_pmu_start_txn(struct pmu
*pmu
)
6024 perf_pmu_disable(pmu
);
6027 static int perf_pmu_commit_txn(struct pmu
*pmu
)
6029 perf_pmu_enable(pmu
);
6033 static void perf_pmu_cancel_txn(struct pmu
*pmu
)
6035 perf_pmu_enable(pmu
);
6038 static int perf_event_idx_default(struct perf_event
*event
)
6040 return event
->hw
.idx
+ 1;
6044 * Ensures all contexts with the same task_ctx_nr have the same
6045 * pmu_cpu_context too.
6047 static void *find_pmu_context(int ctxn
)
6054 list_for_each_entry(pmu
, &pmus
, entry
) {
6055 if (pmu
->task_ctx_nr
== ctxn
)
6056 return pmu
->pmu_cpu_context
;
6062 static void update_pmu_context(struct pmu
*pmu
, struct pmu
*old_pmu
)
6066 for_each_possible_cpu(cpu
) {
6067 struct perf_cpu_context
*cpuctx
;
6069 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
6071 if (cpuctx
->unique_pmu
== old_pmu
)
6072 cpuctx
->unique_pmu
= pmu
;
6076 static void free_pmu_context(struct pmu
*pmu
)
6080 mutex_lock(&pmus_lock
);
6082 * Like a real lame refcount.
6084 list_for_each_entry(i
, &pmus
, entry
) {
6085 if (i
->pmu_cpu_context
== pmu
->pmu_cpu_context
) {
6086 update_pmu_context(i
, pmu
);
6091 free_percpu(pmu
->pmu_cpu_context
);
6093 mutex_unlock(&pmus_lock
);
6095 static struct idr pmu_idr
;
6098 type_show(struct device
*dev
, struct device_attribute
*attr
, char *page
)
6100 struct pmu
*pmu
= dev_get_drvdata(dev
);
6102 return snprintf(page
, PAGE_SIZE
-1, "%d\n", pmu
->type
);
6105 static struct device_attribute pmu_dev_attrs
[] = {
6110 static int pmu_bus_running
;
6111 static struct bus_type pmu_bus
= {
6112 .name
= "event_source",
6113 .dev_attrs
= pmu_dev_attrs
,
6116 static void pmu_dev_release(struct device
*dev
)
6121 static int pmu_dev_alloc(struct pmu
*pmu
)
6125 pmu
->dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
6129 pmu
->dev
->groups
= pmu
->attr_groups
;
6130 device_initialize(pmu
->dev
);
6131 ret
= dev_set_name(pmu
->dev
, "%s", pmu
->name
);
6135 dev_set_drvdata(pmu
->dev
, pmu
);
6136 pmu
->dev
->bus
= &pmu_bus
;
6137 pmu
->dev
->release
= pmu_dev_release
;
6138 ret
= device_add(pmu
->dev
);
6146 put_device(pmu
->dev
);
6150 static struct lock_class_key cpuctx_mutex
;
6151 static struct lock_class_key cpuctx_lock
;
6153 int perf_pmu_register(struct pmu
*pmu
, char *name
, int type
)
6157 mutex_lock(&pmus_lock
);
6159 pmu
->pmu_disable_count
= alloc_percpu(int);
6160 if (!pmu
->pmu_disable_count
)
6169 type
= idr_alloc(&pmu_idr
, pmu
, PERF_TYPE_MAX
, 0, GFP_KERNEL
);
6177 if (pmu_bus_running
) {
6178 ret
= pmu_dev_alloc(pmu
);
6184 pmu
->pmu_cpu_context
= find_pmu_context(pmu
->task_ctx_nr
);
6185 if (pmu
->pmu_cpu_context
)
6186 goto got_cpu_context
;
6189 pmu
->pmu_cpu_context
= alloc_percpu(struct perf_cpu_context
);
6190 if (!pmu
->pmu_cpu_context
)
6193 for_each_possible_cpu(cpu
) {
6194 struct perf_cpu_context
*cpuctx
;
6196 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
6197 __perf_event_init_context(&cpuctx
->ctx
);
6198 lockdep_set_class(&cpuctx
->ctx
.mutex
, &cpuctx_mutex
);
6199 lockdep_set_class(&cpuctx
->ctx
.lock
, &cpuctx_lock
);
6200 cpuctx
->ctx
.type
= cpu_context
;
6201 cpuctx
->ctx
.pmu
= pmu
;
6202 cpuctx
->jiffies_interval
= 1;
6203 INIT_LIST_HEAD(&cpuctx
->rotation_list
);
6204 cpuctx
->unique_pmu
= pmu
;
6208 if (!pmu
->start_txn
) {
6209 if (pmu
->pmu_enable
) {
6211 * If we have pmu_enable/pmu_disable calls, install
6212 * transaction stubs that use that to try and batch
6213 * hardware accesses.
6215 pmu
->start_txn
= perf_pmu_start_txn
;
6216 pmu
->commit_txn
= perf_pmu_commit_txn
;
6217 pmu
->cancel_txn
= perf_pmu_cancel_txn
;
6219 pmu
->start_txn
= perf_pmu_nop_void
;
6220 pmu
->commit_txn
= perf_pmu_nop_int
;
6221 pmu
->cancel_txn
= perf_pmu_nop_void
;
6225 if (!pmu
->pmu_enable
) {
6226 pmu
->pmu_enable
= perf_pmu_nop_void
;
6227 pmu
->pmu_disable
= perf_pmu_nop_void
;
6230 if (!pmu
->event_idx
)
6231 pmu
->event_idx
= perf_event_idx_default
;
6233 list_add_rcu(&pmu
->entry
, &pmus
);
6236 mutex_unlock(&pmus_lock
);
6241 device_del(pmu
->dev
);
6242 put_device(pmu
->dev
);
6245 if (pmu
->type
>= PERF_TYPE_MAX
)
6246 idr_remove(&pmu_idr
, pmu
->type
);
6249 free_percpu(pmu
->pmu_disable_count
);
6253 void perf_pmu_unregister(struct pmu
*pmu
)
6255 mutex_lock(&pmus_lock
);
6256 list_del_rcu(&pmu
->entry
);
6257 mutex_unlock(&pmus_lock
);
6260 * We dereference the pmu list under both SRCU and regular RCU, so
6261 * synchronize against both of those.
6263 synchronize_srcu(&pmus_srcu
);
6266 free_percpu(pmu
->pmu_disable_count
);
6267 if (pmu
->type
>= PERF_TYPE_MAX
)
6268 idr_remove(&pmu_idr
, pmu
->type
);
6269 device_del(pmu
->dev
);
6270 put_device(pmu
->dev
);
6271 free_pmu_context(pmu
);
6274 struct pmu
*perf_init_event(struct perf_event
*event
)
6276 struct pmu
*pmu
= NULL
;
6280 idx
= srcu_read_lock(&pmus_srcu
);
6283 pmu
= idr_find(&pmu_idr
, event
->attr
.type
);
6287 ret
= pmu
->event_init(event
);
6293 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
6295 ret
= pmu
->event_init(event
);
6299 if (ret
!= -ENOENT
) {
6304 pmu
= ERR_PTR(-ENOENT
);
6306 srcu_read_unlock(&pmus_srcu
, idx
);
6312 * Allocate and initialize a event structure
6314 static struct perf_event
*
6315 perf_event_alloc(struct perf_event_attr
*attr
, int cpu
,
6316 struct task_struct
*task
,
6317 struct perf_event
*group_leader
,
6318 struct perf_event
*parent_event
,
6319 perf_overflow_handler_t overflow_handler
,
6323 struct perf_event
*event
;
6324 struct hw_perf_event
*hwc
;
6327 if ((unsigned)cpu
>= nr_cpu_ids
) {
6328 if (!task
|| cpu
!= -1)
6329 return ERR_PTR(-EINVAL
);
6332 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
6334 return ERR_PTR(-ENOMEM
);
6337 * Single events are their own group leaders, with an
6338 * empty sibling list:
6341 group_leader
= event
;
6343 mutex_init(&event
->child_mutex
);
6344 INIT_LIST_HEAD(&event
->child_list
);
6346 INIT_LIST_HEAD(&event
->group_entry
);
6347 INIT_LIST_HEAD(&event
->event_entry
);
6348 INIT_LIST_HEAD(&event
->sibling_list
);
6349 INIT_LIST_HEAD(&event
->rb_entry
);
6351 init_waitqueue_head(&event
->waitq
);
6352 init_irq_work(&event
->pending
, perf_pending_event
);
6354 mutex_init(&event
->mmap_mutex
);
6356 atomic_long_set(&event
->refcount
, 1);
6358 event
->attr
= *attr
;
6359 event
->group_leader
= group_leader
;
6363 event
->parent
= parent_event
;
6365 event
->ns
= get_pid_ns(task_active_pid_ns(current
));
6366 event
->id
= atomic64_inc_return(&perf_event_id
);
6368 event
->state
= PERF_EVENT_STATE_INACTIVE
;
6371 event
->attach_state
= PERF_ATTACH_TASK
;
6373 if (attr
->type
== PERF_TYPE_TRACEPOINT
)
6374 event
->hw
.tp_target
= task
;
6375 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6377 * hw_breakpoint is a bit difficult here..
6379 else if (attr
->type
== PERF_TYPE_BREAKPOINT
)
6380 event
->hw
.bp_target
= task
;
6384 if (!overflow_handler
&& parent_event
) {
6385 overflow_handler
= parent_event
->overflow_handler
;
6386 context
= parent_event
->overflow_handler_context
;
6389 event
->overflow_handler
= overflow_handler
;
6390 event
->overflow_handler_context
= context
;
6392 perf_event__state_init(event
);
6397 hwc
->sample_period
= attr
->sample_period
;
6398 if (attr
->freq
&& attr
->sample_freq
)
6399 hwc
->sample_period
= 1;
6400 hwc
->last_period
= hwc
->sample_period
;
6402 local64_set(&hwc
->period_left
, hwc
->sample_period
);
6405 * we currently do not support PERF_FORMAT_GROUP on inherited events
6407 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
6410 pmu
= perf_init_event(event
);
6416 else if (IS_ERR(pmu
))
6421 put_pid_ns(event
->ns
);
6423 return ERR_PTR(err
);
6426 if (!event
->parent
) {
6427 if (event
->attach_state
& PERF_ATTACH_TASK
)
6428 static_key_slow_inc(&perf_sched_events
.key
);
6429 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
6430 atomic_inc(&nr_mmap_events
);
6431 if (event
->attr
.comm
)
6432 atomic_inc(&nr_comm_events
);
6433 if (event
->attr
.task
)
6434 atomic_inc(&nr_task_events
);
6435 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
6436 err
= get_callchain_buffers();
6439 return ERR_PTR(err
);
6442 if (has_branch_stack(event
)) {
6443 static_key_slow_inc(&perf_sched_events
.key
);
6444 if (!(event
->attach_state
& PERF_ATTACH_TASK
))
6445 atomic_inc(&per_cpu(perf_branch_stack_events
,
6453 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
6454 struct perf_event_attr
*attr
)
6459 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
6463 * zero the full structure, so that a short copy will be nice.
6465 memset(attr
, 0, sizeof(*attr
));
6467 ret
= get_user(size
, &uattr
->size
);
6471 if (size
> PAGE_SIZE
) /* silly large */
6474 if (!size
) /* abi compat */
6475 size
= PERF_ATTR_SIZE_VER0
;
6477 if (size
< PERF_ATTR_SIZE_VER0
)
6481 * If we're handed a bigger struct than we know of,
6482 * ensure all the unknown bits are 0 - i.e. new
6483 * user-space does not rely on any kernel feature
6484 * extensions we dont know about yet.
6486 if (size
> sizeof(*attr
)) {
6487 unsigned char __user
*addr
;
6488 unsigned char __user
*end
;
6491 addr
= (void __user
*)uattr
+ sizeof(*attr
);
6492 end
= (void __user
*)uattr
+ size
;
6494 for (; addr
< end
; addr
++) {
6495 ret
= get_user(val
, addr
);
6501 size
= sizeof(*attr
);
6504 ret
= copy_from_user(attr
, uattr
, size
);
6508 if (attr
->__reserved_1
)
6511 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
6514 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
6517 if (attr
->sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
6518 u64 mask
= attr
->branch_sample_type
;
6520 /* only using defined bits */
6521 if (mask
& ~(PERF_SAMPLE_BRANCH_MAX
-1))
6524 /* at least one branch bit must be set */
6525 if (!(mask
& ~PERF_SAMPLE_BRANCH_PLM_ALL
))
6528 /* kernel level capture: check permissions */
6529 if ((mask
& PERF_SAMPLE_BRANCH_PERM_PLM
)
6530 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
6533 /* propagate priv level, when not set for branch */
6534 if (!(mask
& PERF_SAMPLE_BRANCH_PLM_ALL
)) {
6536 /* exclude_kernel checked on syscall entry */
6537 if (!attr
->exclude_kernel
)
6538 mask
|= PERF_SAMPLE_BRANCH_KERNEL
;
6540 if (!attr
->exclude_user
)
6541 mask
|= PERF_SAMPLE_BRANCH_USER
;
6543 if (!attr
->exclude_hv
)
6544 mask
|= PERF_SAMPLE_BRANCH_HV
;
6546 * adjust user setting (for HW filter setup)
6548 attr
->branch_sample_type
= mask
;
6552 if (attr
->sample_type
& PERF_SAMPLE_REGS_USER
) {
6553 ret
= perf_reg_validate(attr
->sample_regs_user
);
6558 if (attr
->sample_type
& PERF_SAMPLE_STACK_USER
) {
6559 if (!arch_perf_have_user_stack_dump())
6563 * We have __u32 type for the size, but so far
6564 * we can only use __u16 as maximum due to the
6565 * __u16 sample size limit.
6567 if (attr
->sample_stack_user
>= USHRT_MAX
)
6569 else if (!IS_ALIGNED(attr
->sample_stack_user
, sizeof(u64
)))
6577 put_user(sizeof(*attr
), &uattr
->size
);
6583 perf_event_set_output(struct perf_event
*event
, struct perf_event
*output_event
)
6585 struct ring_buffer
*rb
= NULL
, *old_rb
= NULL
;
6591 /* don't allow circular references */
6592 if (event
== output_event
)
6596 * Don't allow cross-cpu buffers
6598 if (output_event
->cpu
!= event
->cpu
)
6602 * If its not a per-cpu rb, it must be the same task.
6604 if (output_event
->cpu
== -1 && output_event
->ctx
!= event
->ctx
)
6608 mutex_lock(&event
->mmap_mutex
);
6609 /* Can't redirect output if we've got an active mmap() */
6610 if (atomic_read(&event
->mmap_count
))
6616 /* get the rb we want to redirect to */
6617 rb
= ring_buffer_get(output_event
);
6623 ring_buffer_detach(event
, old_rb
);
6626 ring_buffer_attach(event
, rb
);
6628 rcu_assign_pointer(event
->rb
, rb
);
6631 ring_buffer_put(old_rb
);
6633 * Since we detached before setting the new rb, so that we
6634 * could attach the new rb, we could have missed a wakeup.
6637 wake_up_all(&event
->waitq
);
6642 mutex_unlock(&event
->mmap_mutex
);
6649 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6651 * @attr_uptr: event_id type attributes for monitoring/sampling
6654 * @group_fd: group leader event fd
6656 SYSCALL_DEFINE5(perf_event_open
,
6657 struct perf_event_attr __user
*, attr_uptr
,
6658 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
6660 struct perf_event
*group_leader
= NULL
, *output_event
= NULL
;
6661 struct perf_event
*event
, *sibling
;
6662 struct perf_event_attr attr
;
6663 struct perf_event_context
*ctx
;
6664 struct file
*event_file
= NULL
;
6665 struct fd group
= {NULL
, 0};
6666 struct task_struct
*task
= NULL
;
6672 /* for future expandability... */
6673 if (flags
& ~PERF_FLAG_ALL
)
6676 err
= perf_copy_attr(attr_uptr
, &attr
);
6680 if (!attr
.exclude_kernel
) {
6681 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
6686 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
6689 if (attr
.sample_period
& (1ULL << 63))
6694 * In cgroup mode, the pid argument is used to pass the fd
6695 * opened to the cgroup directory in cgroupfs. The cpu argument
6696 * designates the cpu on which to monitor threads from that
6699 if ((flags
& PERF_FLAG_PID_CGROUP
) && (pid
== -1 || cpu
== -1))
6702 event_fd
= get_unused_fd();
6706 if (group_fd
!= -1) {
6707 err
= perf_fget_light(group_fd
, &group
);
6710 group_leader
= group
.file
->private_data
;
6711 if (flags
& PERF_FLAG_FD_OUTPUT
)
6712 output_event
= group_leader
;
6713 if (flags
& PERF_FLAG_FD_NO_GROUP
)
6714 group_leader
= NULL
;
6717 if (pid
!= -1 && !(flags
& PERF_FLAG_PID_CGROUP
)) {
6718 task
= find_lively_task_by_vpid(pid
);
6720 err
= PTR_ERR(task
);
6727 event
= perf_event_alloc(&attr
, cpu
, task
, group_leader
, NULL
,
6729 if (IS_ERR(event
)) {
6730 err
= PTR_ERR(event
);
6734 if (flags
& PERF_FLAG_PID_CGROUP
) {
6735 err
= perf_cgroup_connect(pid
, event
, &attr
, group_leader
);
6740 * - that has cgroup constraint on event->cpu
6741 * - that may need work on context switch
6743 atomic_inc(&per_cpu(perf_cgroup_events
, event
->cpu
));
6744 static_key_slow_inc(&perf_sched_events
.key
);
6748 * Special case software events and allow them to be part of
6749 * any hardware group.
6754 (is_software_event(event
) != is_software_event(group_leader
))) {
6755 if (is_software_event(event
)) {
6757 * If event and group_leader are not both a software
6758 * event, and event is, then group leader is not.
6760 * Allow the addition of software events to !software
6761 * groups, this is safe because software events never
6764 pmu
= group_leader
->pmu
;
6765 } else if (is_software_event(group_leader
) &&
6766 (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
)) {
6768 * In case the group is a pure software group, and we
6769 * try to add a hardware event, move the whole group to
6770 * the hardware context.
6777 * Get the target context (task or percpu):
6779 ctx
= find_get_context(pmu
, task
, event
->cpu
);
6786 put_task_struct(task
);
6791 * Look up the group leader (we will attach this event to it):
6797 * Do not allow a recursive hierarchy (this new sibling
6798 * becoming part of another group-sibling):
6800 if (group_leader
->group_leader
!= group_leader
)
6803 * Do not allow to attach to a group in a different
6804 * task or CPU context:
6807 if (group_leader
->ctx
->type
!= ctx
->type
)
6810 if (group_leader
->ctx
!= ctx
)
6815 * Only a group leader can be exclusive or pinned
6817 if (attr
.exclusive
|| attr
.pinned
)
6822 err
= perf_event_set_output(event
, output_event
);
6827 event_file
= anon_inode_getfile("[perf_event]", &perf_fops
, event
, O_RDWR
);
6828 if (IS_ERR(event_file
)) {
6829 err
= PTR_ERR(event_file
);
6834 struct perf_event_context
*gctx
= group_leader
->ctx
;
6836 mutex_lock(&gctx
->mutex
);
6837 perf_remove_from_context(group_leader
, false);
6840 * Removing from the context ends up with disabled
6841 * event. What we want here is event in the initial
6842 * startup state, ready to be add into new context.
6844 perf_event__state_init(group_leader
);
6845 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
6847 perf_remove_from_context(sibling
, false);
6848 perf_event__state_init(sibling
);
6851 mutex_unlock(&gctx
->mutex
);
6855 WARN_ON_ONCE(ctx
->parent_ctx
);
6856 mutex_lock(&ctx
->mutex
);
6860 perf_install_in_context(ctx
, group_leader
, event
->cpu
);
6862 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
6864 perf_install_in_context(ctx
, sibling
, event
->cpu
);
6869 perf_install_in_context(ctx
, event
, event
->cpu
);
6871 perf_unpin_context(ctx
);
6872 mutex_unlock(&ctx
->mutex
);
6876 event
->owner
= current
;
6878 mutex_lock(¤t
->perf_event_mutex
);
6879 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
6880 mutex_unlock(¤t
->perf_event_mutex
);
6883 * Precalculate sample_data sizes
6885 perf_event__header_size(event
);
6886 perf_event__id_header_size(event
);
6889 * Drop the reference on the group_event after placing the
6890 * new event on the sibling_list. This ensures destruction
6891 * of the group leader will find the pointer to itself in
6892 * perf_group_detach().
6895 fd_install(event_fd
, event_file
);
6899 perf_unpin_context(ctx
);
6906 put_task_struct(task
);
6910 put_unused_fd(event_fd
);
6915 * perf_event_create_kernel_counter
6917 * @attr: attributes of the counter to create
6918 * @cpu: cpu in which the counter is bound
6919 * @task: task to profile (NULL for percpu)
6922 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
6923 struct task_struct
*task
,
6924 perf_overflow_handler_t overflow_handler
,
6927 struct perf_event_context
*ctx
;
6928 struct perf_event
*event
;
6932 * Get the target context (task or percpu):
6935 event
= perf_event_alloc(attr
, cpu
, task
, NULL
, NULL
,
6936 overflow_handler
, context
);
6937 if (IS_ERR(event
)) {
6938 err
= PTR_ERR(event
);
6942 ctx
= find_get_context(event
->pmu
, task
, cpu
);
6948 WARN_ON_ONCE(ctx
->parent_ctx
);
6949 mutex_lock(&ctx
->mutex
);
6950 perf_install_in_context(ctx
, event
, cpu
);
6952 perf_unpin_context(ctx
);
6953 mutex_unlock(&ctx
->mutex
);
6960 return ERR_PTR(err
);
6962 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
6964 void perf_pmu_migrate_context(struct pmu
*pmu
, int src_cpu
, int dst_cpu
)
6966 struct perf_event_context
*src_ctx
;
6967 struct perf_event_context
*dst_ctx
;
6968 struct perf_event
*event
, *tmp
;
6971 src_ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, src_cpu
)->ctx
;
6972 dst_ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, dst_cpu
)->ctx
;
6974 mutex_lock(&src_ctx
->mutex
);
6975 list_for_each_entry_safe(event
, tmp
, &src_ctx
->event_list
,
6977 perf_remove_from_context(event
, false);
6979 list_add(&event
->event_entry
, &events
);
6981 mutex_unlock(&src_ctx
->mutex
);
6985 mutex_lock(&dst_ctx
->mutex
);
6986 list_for_each_entry_safe(event
, tmp
, &events
, event_entry
) {
6987 list_del(&event
->event_entry
);
6988 if (event
->state
>= PERF_EVENT_STATE_OFF
)
6989 event
->state
= PERF_EVENT_STATE_INACTIVE
;
6990 perf_install_in_context(dst_ctx
, event
, dst_cpu
);
6993 mutex_unlock(&dst_ctx
->mutex
);
6995 EXPORT_SYMBOL_GPL(perf_pmu_migrate_context
);
6997 static void sync_child_event(struct perf_event
*child_event
,
6998 struct task_struct
*child
)
7000 struct perf_event
*parent_event
= child_event
->parent
;
7003 if (child_event
->attr
.inherit_stat
)
7004 perf_event_read_event(child_event
, child
);
7006 child_val
= perf_event_count(child_event
);
7009 * Add back the child's count to the parent's count:
7011 atomic64_add(child_val
, &parent_event
->child_count
);
7012 atomic64_add(child_event
->total_time_enabled
,
7013 &parent_event
->child_total_time_enabled
);
7014 atomic64_add(child_event
->total_time_running
,
7015 &parent_event
->child_total_time_running
);
7018 * Remove this event from the parent's list
7020 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
7021 mutex_lock(&parent_event
->child_mutex
);
7022 list_del_init(&child_event
->child_list
);
7023 mutex_unlock(&parent_event
->child_mutex
);
7026 * Release the parent event, if this was the last
7029 put_event(parent_event
);
7033 __perf_event_exit_task(struct perf_event
*child_event
,
7034 struct perf_event_context
*child_ctx
,
7035 struct task_struct
*child
)
7037 perf_remove_from_context(child_event
, !!child_event
->parent
);
7040 * It can happen that the parent exits first, and has events
7041 * that are still around due to the child reference. These
7042 * events need to be zapped.
7044 if (child_event
->parent
) {
7045 sync_child_event(child_event
, child
);
7046 free_event(child_event
);
7050 static void perf_event_exit_task_context(struct task_struct
*child
, int ctxn
)
7052 struct perf_event
*child_event
, *tmp
;
7053 struct perf_event_context
*child_ctx
;
7054 unsigned long flags
;
7056 if (likely(!child
->perf_event_ctxp
[ctxn
])) {
7057 perf_event_task(child
, NULL
, 0);
7061 local_irq_save(flags
);
7063 * We can't reschedule here because interrupts are disabled,
7064 * and either child is current or it is a task that can't be
7065 * scheduled, so we are now safe from rescheduling changing
7068 child_ctx
= rcu_dereference_raw(child
->perf_event_ctxp
[ctxn
]);
7071 * Take the context lock here so that if find_get_context is
7072 * reading child->perf_event_ctxp, we wait until it has
7073 * incremented the context's refcount before we do put_ctx below.
7075 raw_spin_lock(&child_ctx
->lock
);
7076 task_ctx_sched_out(child_ctx
);
7077 child
->perf_event_ctxp
[ctxn
] = NULL
;
7079 * If this context is a clone; unclone it so it can't get
7080 * swapped to another process while we're removing all
7081 * the events from it.
7083 unclone_ctx(child_ctx
);
7084 update_context_time(child_ctx
);
7085 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
7088 * Report the task dead after unscheduling the events so that we
7089 * won't get any samples after PERF_RECORD_EXIT. We can however still
7090 * get a few PERF_RECORD_READ events.
7092 perf_event_task(child
, child_ctx
, 0);
7095 * We can recurse on the same lock type through:
7097 * __perf_event_exit_task()
7098 * sync_child_event()
7100 * mutex_lock(&ctx->mutex)
7102 * But since its the parent context it won't be the same instance.
7104 mutex_lock(&child_ctx
->mutex
);
7107 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->pinned_groups
,
7109 __perf_event_exit_task(child_event
, child_ctx
, child
);
7111 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->flexible_groups
,
7113 __perf_event_exit_task(child_event
, child_ctx
, child
);
7116 * If the last event was a group event, it will have appended all
7117 * its siblings to the list, but we obtained 'tmp' before that which
7118 * will still point to the list head terminating the iteration.
7120 if (!list_empty(&child_ctx
->pinned_groups
) ||
7121 !list_empty(&child_ctx
->flexible_groups
))
7124 mutex_unlock(&child_ctx
->mutex
);
7130 * When a child task exits, feed back event values to parent events.
7132 void perf_event_exit_task(struct task_struct
*child
)
7134 struct perf_event
*event
, *tmp
;
7137 mutex_lock(&child
->perf_event_mutex
);
7138 list_for_each_entry_safe(event
, tmp
, &child
->perf_event_list
,
7140 list_del_init(&event
->owner_entry
);
7143 * Ensure the list deletion is visible before we clear
7144 * the owner, closes a race against perf_release() where
7145 * we need to serialize on the owner->perf_event_mutex.
7148 event
->owner
= NULL
;
7150 mutex_unlock(&child
->perf_event_mutex
);
7152 for_each_task_context_nr(ctxn
)
7153 perf_event_exit_task_context(child
, ctxn
);
7156 static void perf_free_event(struct perf_event
*event
,
7157 struct perf_event_context
*ctx
)
7159 struct perf_event
*parent
= event
->parent
;
7161 if (WARN_ON_ONCE(!parent
))
7164 mutex_lock(&parent
->child_mutex
);
7165 list_del_init(&event
->child_list
);
7166 mutex_unlock(&parent
->child_mutex
);
7170 perf_group_detach(event
);
7171 list_del_event(event
, ctx
);
7176 * free an unexposed, unused context as created by inheritance by
7177 * perf_event_init_task below, used by fork() in case of fail.
7179 void perf_event_free_task(struct task_struct
*task
)
7181 struct perf_event_context
*ctx
;
7182 struct perf_event
*event
, *tmp
;
7185 for_each_task_context_nr(ctxn
) {
7186 ctx
= task
->perf_event_ctxp
[ctxn
];
7190 mutex_lock(&ctx
->mutex
);
7192 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
,
7194 perf_free_event(event
, ctx
);
7196 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
,
7198 perf_free_event(event
, ctx
);
7200 if (!list_empty(&ctx
->pinned_groups
) ||
7201 !list_empty(&ctx
->flexible_groups
))
7204 mutex_unlock(&ctx
->mutex
);
7210 void perf_event_delayed_put(struct task_struct
*task
)
7214 for_each_task_context_nr(ctxn
)
7215 WARN_ON_ONCE(task
->perf_event_ctxp
[ctxn
]);
7219 * inherit a event from parent task to child task:
7221 static struct perf_event
*
7222 inherit_event(struct perf_event
*parent_event
,
7223 struct task_struct
*parent
,
7224 struct perf_event_context
*parent_ctx
,
7225 struct task_struct
*child
,
7226 struct perf_event
*group_leader
,
7227 struct perf_event_context
*child_ctx
)
7229 struct perf_event
*child_event
;
7230 unsigned long flags
;
7233 * Instead of creating recursive hierarchies of events,
7234 * we link inherited events back to the original parent,
7235 * which has a filp for sure, which we use as the reference
7238 if (parent_event
->parent
)
7239 parent_event
= parent_event
->parent
;
7241 child_event
= perf_event_alloc(&parent_event
->attr
,
7244 group_leader
, parent_event
,
7246 if (IS_ERR(child_event
))
7249 if (!atomic_long_inc_not_zero(&parent_event
->refcount
)) {
7250 free_event(child_event
);
7257 * Make the child state follow the state of the parent event,
7258 * not its attr.disabled bit. We hold the parent's mutex,
7259 * so we won't race with perf_event_{en, dis}able_family.
7261 if (parent_event
->state
>= PERF_EVENT_STATE_INACTIVE
)
7262 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
7264 child_event
->state
= PERF_EVENT_STATE_OFF
;
7266 if (parent_event
->attr
.freq
) {
7267 u64 sample_period
= parent_event
->hw
.sample_period
;
7268 struct hw_perf_event
*hwc
= &child_event
->hw
;
7270 hwc
->sample_period
= sample_period
;
7271 hwc
->last_period
= sample_period
;
7273 local64_set(&hwc
->period_left
, sample_period
);
7276 child_event
->ctx
= child_ctx
;
7277 child_event
->overflow_handler
= parent_event
->overflow_handler
;
7278 child_event
->overflow_handler_context
7279 = parent_event
->overflow_handler_context
;
7282 * Precalculate sample_data sizes
7284 perf_event__header_size(child_event
);
7285 perf_event__id_header_size(child_event
);
7288 * Link it up in the child's context:
7290 raw_spin_lock_irqsave(&child_ctx
->lock
, flags
);
7291 add_event_to_ctx(child_event
, child_ctx
);
7292 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
7295 * Link this into the parent event's child list
7297 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
7298 mutex_lock(&parent_event
->child_mutex
);
7299 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
7300 mutex_unlock(&parent_event
->child_mutex
);
7305 static int inherit_group(struct perf_event
*parent_event
,
7306 struct task_struct
*parent
,
7307 struct perf_event_context
*parent_ctx
,
7308 struct task_struct
*child
,
7309 struct perf_event_context
*child_ctx
)
7311 struct perf_event
*leader
;
7312 struct perf_event
*sub
;
7313 struct perf_event
*child_ctr
;
7315 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
7316 child
, NULL
, child_ctx
);
7318 return PTR_ERR(leader
);
7319 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
7320 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
7321 child
, leader
, child_ctx
);
7322 if (IS_ERR(child_ctr
))
7323 return PTR_ERR(child_ctr
);
7329 inherit_task_group(struct perf_event
*event
, struct task_struct
*parent
,
7330 struct perf_event_context
*parent_ctx
,
7331 struct task_struct
*child
, int ctxn
,
7335 struct perf_event_context
*child_ctx
;
7337 if (!event
->attr
.inherit
) {
7342 child_ctx
= child
->perf_event_ctxp
[ctxn
];
7345 * This is executed from the parent task context, so
7346 * inherit events that have been marked for cloning.
7347 * First allocate and initialize a context for the
7351 child_ctx
= alloc_perf_context(parent_ctx
->pmu
, child
);
7355 child
->perf_event_ctxp
[ctxn
] = child_ctx
;
7358 ret
= inherit_group(event
, parent
, parent_ctx
,
7368 * Initialize the perf_event context in task_struct
7370 int perf_event_init_context(struct task_struct
*child
, int ctxn
)
7372 struct perf_event_context
*child_ctx
, *parent_ctx
;
7373 struct perf_event_context
*cloned_ctx
;
7374 struct perf_event
*event
;
7375 struct task_struct
*parent
= current
;
7376 int inherited_all
= 1;
7377 unsigned long flags
;
7380 if (likely(!parent
->perf_event_ctxp
[ctxn
]))
7384 * If the parent's context is a clone, pin it so it won't get
7387 parent_ctx
= perf_pin_task_context(parent
, ctxn
);
7390 * No need to check if parent_ctx != NULL here; since we saw
7391 * it non-NULL earlier, the only reason for it to become NULL
7392 * is if we exit, and since we're currently in the middle of
7393 * a fork we can't be exiting at the same time.
7397 * Lock the parent list. No need to lock the child - not PID
7398 * hashed yet and not running, so nobody can access it.
7400 mutex_lock(&parent_ctx
->mutex
);
7403 * We dont have to disable NMIs - we are only looking at
7404 * the list, not manipulating it:
7406 list_for_each_entry(event
, &parent_ctx
->pinned_groups
, group_entry
) {
7407 ret
= inherit_task_group(event
, parent
, parent_ctx
,
7408 child
, ctxn
, &inherited_all
);
7414 * We can't hold ctx->lock when iterating the ->flexible_group list due
7415 * to allocations, but we need to prevent rotation because
7416 * rotate_ctx() will change the list from interrupt context.
7418 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
7419 parent_ctx
->rotate_disable
= 1;
7420 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
7422 list_for_each_entry(event
, &parent_ctx
->flexible_groups
, group_entry
) {
7423 ret
= inherit_task_group(event
, parent
, parent_ctx
,
7424 child
, ctxn
, &inherited_all
);
7429 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
7430 parent_ctx
->rotate_disable
= 0;
7432 child_ctx
= child
->perf_event_ctxp
[ctxn
];
7434 if (child_ctx
&& inherited_all
) {
7436 * Mark the child context as a clone of the parent
7437 * context, or of whatever the parent is a clone of.
7439 * Note that if the parent is a clone, the holding of
7440 * parent_ctx->lock avoids it from being uncloned.
7442 cloned_ctx
= parent_ctx
->parent_ctx
;
7444 child_ctx
->parent_ctx
= cloned_ctx
;
7445 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
7447 child_ctx
->parent_ctx
= parent_ctx
;
7448 child_ctx
->parent_gen
= parent_ctx
->generation
;
7450 get_ctx(child_ctx
->parent_ctx
);
7453 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
7454 mutex_unlock(&parent_ctx
->mutex
);
7456 perf_unpin_context(parent_ctx
);
7457 put_ctx(parent_ctx
);
7463 * Initialize the perf_event context in task_struct
7465 int perf_event_init_task(struct task_struct
*child
)
7469 memset(child
->perf_event_ctxp
, 0, sizeof(child
->perf_event_ctxp
));
7470 mutex_init(&child
->perf_event_mutex
);
7471 INIT_LIST_HEAD(&child
->perf_event_list
);
7473 for_each_task_context_nr(ctxn
) {
7474 ret
= perf_event_init_context(child
, ctxn
);
7482 static void __init
perf_event_init_all_cpus(void)
7484 struct swevent_htable
*swhash
;
7487 for_each_possible_cpu(cpu
) {
7488 swhash
= &per_cpu(swevent_htable
, cpu
);
7489 mutex_init(&swhash
->hlist_mutex
);
7490 INIT_LIST_HEAD(&per_cpu(rotation_list
, cpu
));
7494 static void __cpuinit
perf_event_init_cpu(int cpu
)
7496 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
7498 mutex_lock(&swhash
->hlist_mutex
);
7499 swhash
->online
= true;
7500 if (swhash
->hlist_refcount
> 0) {
7501 struct swevent_hlist
*hlist
;
7503 hlist
= kzalloc_node(sizeof(*hlist
), GFP_KERNEL
, cpu_to_node(cpu
));
7505 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
7507 mutex_unlock(&swhash
->hlist_mutex
);
7510 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7511 static void perf_pmu_rotate_stop(struct pmu
*pmu
)
7513 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
7515 WARN_ON(!irqs_disabled());
7517 list_del_init(&cpuctx
->rotation_list
);
7520 static void __perf_event_exit_context(void *__info
)
7522 struct remove_event re
= { .detach_group
= false };
7523 struct perf_event_context
*ctx
= __info
;
7525 perf_pmu_rotate_stop(ctx
->pmu
);
7528 list_for_each_entry_rcu(re
.event
, &ctx
->event_list
, event_entry
)
7529 __perf_remove_from_context(&re
);
7533 static void perf_event_exit_cpu_context(int cpu
)
7535 struct perf_event_context
*ctx
;
7539 idx
= srcu_read_lock(&pmus_srcu
);
7540 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
7541 ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
)->ctx
;
7543 mutex_lock(&ctx
->mutex
);
7544 smp_call_function_single(cpu
, __perf_event_exit_context
, ctx
, 1);
7545 mutex_unlock(&ctx
->mutex
);
7547 srcu_read_unlock(&pmus_srcu
, idx
);
7550 static void perf_event_exit_cpu(int cpu
)
7552 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
7554 perf_event_exit_cpu_context(cpu
);
7556 mutex_lock(&swhash
->hlist_mutex
);
7557 swhash
->online
= false;
7558 swevent_hlist_release(swhash
);
7559 mutex_unlock(&swhash
->hlist_mutex
);
7562 static inline void perf_event_exit_cpu(int cpu
) { }
7566 perf_reboot(struct notifier_block
*notifier
, unsigned long val
, void *v
)
7570 for_each_online_cpu(cpu
)
7571 perf_event_exit_cpu(cpu
);
7577 * Run the perf reboot notifier at the very last possible moment so that
7578 * the generic watchdog code runs as long as possible.
7580 static struct notifier_block perf_reboot_notifier
= {
7581 .notifier_call
= perf_reboot
,
7582 .priority
= INT_MIN
,
7585 static int __cpuinit
7586 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
7588 unsigned int cpu
= (long)hcpu
;
7590 switch (action
& ~CPU_TASKS_FROZEN
) {
7592 case CPU_UP_PREPARE
:
7593 case CPU_DOWN_FAILED
:
7594 perf_event_init_cpu(cpu
);
7597 case CPU_UP_CANCELED
:
7598 case CPU_DOWN_PREPARE
:
7599 perf_event_exit_cpu(cpu
);
7609 void __init
perf_event_init(void)
7615 perf_event_init_all_cpus();
7616 init_srcu_struct(&pmus_srcu
);
7617 perf_pmu_register(&perf_swevent
, "software", PERF_TYPE_SOFTWARE
);
7618 perf_pmu_register(&perf_cpu_clock
, NULL
, -1);
7619 perf_pmu_register(&perf_task_clock
, NULL
, -1);
7621 perf_cpu_notifier(perf_cpu_notify
);
7622 register_reboot_notifier(&perf_reboot_notifier
);
7624 ret
= init_hw_breakpoint();
7625 WARN(ret
, "hw_breakpoint initialization failed with: %d", ret
);
7627 /* do not patch jump label more than once per second */
7628 jump_label_rate_limit(&perf_sched_events
, HZ
);
7631 * Build time assertion that we keep the data_head at the intended
7632 * location. IOW, validation we got the __reserved[] size right.
7634 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page
, data_head
))
7638 static int __init
perf_event_sysfs_init(void)
7643 mutex_lock(&pmus_lock
);
7645 ret
= bus_register(&pmu_bus
);
7649 list_for_each_entry(pmu
, &pmus
, entry
) {
7650 if (!pmu
->name
|| pmu
->type
< 0)
7653 ret
= pmu_dev_alloc(pmu
);
7654 WARN(ret
, "Failed to register pmu: %s, reason %d\n", pmu
->name
, ret
);
7656 pmu_bus_running
= 1;
7660 mutex_unlock(&pmus_lock
);
7664 device_initcall(perf_event_sysfs_init
);
7666 #ifdef CONFIG_CGROUP_PERF
7667 static struct cgroup_subsys_state
*perf_cgroup_css_alloc(struct cgroup
*cont
)
7669 struct perf_cgroup
*jc
;
7671 jc
= kzalloc(sizeof(*jc
), GFP_KERNEL
);
7673 return ERR_PTR(-ENOMEM
);
7675 jc
->info
= alloc_percpu(struct perf_cgroup_info
);
7678 return ERR_PTR(-ENOMEM
);
7684 static void perf_cgroup_css_free(struct cgroup
*cont
)
7686 struct perf_cgroup
*jc
;
7687 jc
= container_of(cgroup_subsys_state(cont
, perf_subsys_id
),
7688 struct perf_cgroup
, css
);
7689 free_percpu(jc
->info
);
7693 static int __perf_cgroup_move(void *info
)
7695 struct task_struct
*task
= info
;
7696 perf_cgroup_switch(task
, PERF_CGROUP_SWOUT
| PERF_CGROUP_SWIN
);
7700 static void perf_cgroup_attach(struct cgroup
*cgrp
, struct cgroup_taskset
*tset
)
7702 struct task_struct
*task
;
7704 cgroup_taskset_for_each(task
, cgrp
, tset
)
7705 task_function_call(task
, __perf_cgroup_move
, task
);
7708 static void perf_cgroup_exit(struct cgroup
*cgrp
, struct cgroup
*old_cgrp
,
7709 struct task_struct
*task
)
7712 * cgroup_exit() is called in the copy_process() failure path.
7713 * Ignore this case since the task hasn't ran yet, this avoids
7714 * trying to poke a half freed task state from generic code.
7716 if (!(task
->flags
& PF_EXITING
))
7719 task_function_call(task
, __perf_cgroup_move
, task
);
7722 struct cgroup_subsys perf_subsys
= {
7723 .name
= "perf_event",
7724 .subsys_id
= perf_subsys_id
,
7725 .css_alloc
= perf_cgroup_css_alloc
,
7726 .css_free
= perf_cgroup_css_free
,
7727 .exit
= perf_cgroup_exit
,
7728 .attach
= perf_cgroup_attach
,
7730 #endif /* CONFIG_CGROUP_PERF */