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
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/cgroup.h>
38 #include <linux/perf_event.h>
39 #include <linux/trace_events.h>
40 #include <linux/hw_breakpoint.h>
41 #include <linux/mm_types.h>
42 #include <linux/module.h>
43 #include <linux/mman.h>
44 #include <linux/compat.h>
45 #include <linux/bpf.h>
46 #include <linux/filter.h>
47 #include <linux/namei.h>
48 #include <linux/parser.h>
52 #include <asm/irq_regs.h>
54 typedef int (*remote_function_f
)(void *);
56 struct remote_function_call
{
57 struct task_struct
*p
;
58 remote_function_f func
;
63 static void remote_function(void *data
)
65 struct remote_function_call
*tfc
= data
;
66 struct task_struct
*p
= tfc
->p
;
70 if (task_cpu(p
) != smp_processor_id())
74 * Now that we're on right CPU with IRQs disabled, we can test
75 * if we hit the right task without races.
78 tfc
->ret
= -ESRCH
; /* No such (running) process */
83 tfc
->ret
= tfc
->func(tfc
->info
);
87 * task_function_call - call a function on the cpu on which a task runs
88 * @p: the task to evaluate
89 * @func: the function to be called
90 * @info: the function call argument
92 * Calls the function @func when the task is currently running. This might
93 * be on the current CPU, which just calls the function directly
95 * returns: @func return value, or
96 * -ESRCH - when the process isn't running
97 * -EAGAIN - when the process moved away
100 task_function_call(struct task_struct
*p
, remote_function_f func
, void *info
)
102 struct remote_function_call data
= {
111 ret
= smp_call_function_single(task_cpu(p
), remote_function
, &data
, 1);
114 } while (ret
== -EAGAIN
);
120 * cpu_function_call - call a function on the cpu
121 * @func: the function to be called
122 * @info: the function call argument
124 * Calls the function @func on the remote cpu.
126 * returns: @func return value or -ENXIO when the cpu is offline
128 static int cpu_function_call(int cpu
, remote_function_f func
, void *info
)
130 struct remote_function_call data
= {
134 .ret
= -ENXIO
, /* No such CPU */
137 smp_call_function_single(cpu
, remote_function
, &data
, 1);
142 static inline struct perf_cpu_context
*
143 __get_cpu_context(struct perf_event_context
*ctx
)
145 return this_cpu_ptr(ctx
->pmu
->pmu_cpu_context
);
148 static void perf_ctx_lock(struct perf_cpu_context
*cpuctx
,
149 struct perf_event_context
*ctx
)
151 raw_spin_lock(&cpuctx
->ctx
.lock
);
153 raw_spin_lock(&ctx
->lock
);
156 static void perf_ctx_unlock(struct perf_cpu_context
*cpuctx
,
157 struct perf_event_context
*ctx
)
160 raw_spin_unlock(&ctx
->lock
);
161 raw_spin_unlock(&cpuctx
->ctx
.lock
);
164 #define TASK_TOMBSTONE ((void *)-1L)
166 static bool is_kernel_event(struct perf_event
*event
)
168 return READ_ONCE(event
->owner
) == TASK_TOMBSTONE
;
172 * On task ctx scheduling...
174 * When !ctx->nr_events a task context will not be scheduled. This means
175 * we can disable the scheduler hooks (for performance) without leaving
176 * pending task ctx state.
178 * This however results in two special cases:
180 * - removing the last event from a task ctx; this is relatively straight
181 * forward and is done in __perf_remove_from_context.
183 * - adding the first event to a task ctx; this is tricky because we cannot
184 * rely on ctx->is_active and therefore cannot use event_function_call().
185 * See perf_install_in_context().
187 * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set.
190 typedef void (*event_f
)(struct perf_event
*, struct perf_cpu_context
*,
191 struct perf_event_context
*, void *);
193 struct event_function_struct
{
194 struct perf_event
*event
;
199 static int event_function(void *info
)
201 struct event_function_struct
*efs
= info
;
202 struct perf_event
*event
= efs
->event
;
203 struct perf_event_context
*ctx
= event
->ctx
;
204 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
205 struct perf_event_context
*task_ctx
= cpuctx
->task_ctx
;
208 WARN_ON_ONCE(!irqs_disabled());
210 perf_ctx_lock(cpuctx
, task_ctx
);
212 * Since we do the IPI call without holding ctx->lock things can have
213 * changed, double check we hit the task we set out to hit.
216 if (ctx
->task
!= current
) {
222 * We only use event_function_call() on established contexts,
223 * and event_function() is only ever called when active (or
224 * rather, we'll have bailed in task_function_call() or the
225 * above ctx->task != current test), therefore we must have
226 * ctx->is_active here.
228 WARN_ON_ONCE(!ctx
->is_active
);
230 * And since we have ctx->is_active, cpuctx->task_ctx must
233 WARN_ON_ONCE(task_ctx
!= ctx
);
235 WARN_ON_ONCE(&cpuctx
->ctx
!= ctx
);
238 efs
->func(event
, cpuctx
, ctx
, efs
->data
);
240 perf_ctx_unlock(cpuctx
, task_ctx
);
245 static void event_function_local(struct perf_event
*event
, event_f func
, void *data
)
247 struct event_function_struct efs
= {
253 int ret
= event_function(&efs
);
257 static void event_function_call(struct perf_event
*event
, event_f func
, void *data
)
259 struct perf_event_context
*ctx
= event
->ctx
;
260 struct task_struct
*task
= READ_ONCE(ctx
->task
); /* verified in event_function */
261 struct event_function_struct efs
= {
267 if (!event
->parent
) {
269 * If this is a !child event, we must hold ctx::mutex to
270 * stabilize the the event->ctx relation. See
271 * perf_event_ctx_lock().
273 lockdep_assert_held(&ctx
->mutex
);
277 cpu_function_call(event
->cpu
, event_function
, &efs
);
281 if (task
== TASK_TOMBSTONE
)
285 if (!task_function_call(task
, event_function
, &efs
))
288 raw_spin_lock_irq(&ctx
->lock
);
290 * Reload the task pointer, it might have been changed by
291 * a concurrent perf_event_context_sched_out().
294 if (task
== TASK_TOMBSTONE
) {
295 raw_spin_unlock_irq(&ctx
->lock
);
298 if (ctx
->is_active
) {
299 raw_spin_unlock_irq(&ctx
->lock
);
302 func(event
, NULL
, ctx
, data
);
303 raw_spin_unlock_irq(&ctx
->lock
);
306 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
307 PERF_FLAG_FD_OUTPUT |\
308 PERF_FLAG_PID_CGROUP |\
309 PERF_FLAG_FD_CLOEXEC)
312 * branch priv levels that need permission checks
314 #define PERF_SAMPLE_BRANCH_PERM_PLM \
315 (PERF_SAMPLE_BRANCH_KERNEL |\
316 PERF_SAMPLE_BRANCH_HV)
319 EVENT_FLEXIBLE
= 0x1,
322 EVENT_ALL
= EVENT_FLEXIBLE
| EVENT_PINNED
,
326 * perf_sched_events : >0 events exist
327 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
330 static void perf_sched_delayed(struct work_struct
*work
);
331 DEFINE_STATIC_KEY_FALSE(perf_sched_events
);
332 static DECLARE_DELAYED_WORK(perf_sched_work
, perf_sched_delayed
);
333 static DEFINE_MUTEX(perf_sched_mutex
);
334 static atomic_t perf_sched_count
;
336 static DEFINE_PER_CPU(atomic_t
, perf_cgroup_events
);
337 static DEFINE_PER_CPU(int, perf_sched_cb_usages
);
338 static DEFINE_PER_CPU(struct pmu_event_list
, pmu_sb_events
);
340 static atomic_t nr_mmap_events __read_mostly
;
341 static atomic_t nr_comm_events __read_mostly
;
342 static atomic_t nr_task_events __read_mostly
;
343 static atomic_t nr_freq_events __read_mostly
;
344 static atomic_t nr_switch_events __read_mostly
;
346 static LIST_HEAD(pmus
);
347 static DEFINE_MUTEX(pmus_lock
);
348 static struct srcu_struct pmus_srcu
;
351 * perf event paranoia level:
352 * -1 - not paranoid at all
353 * 0 - disallow raw tracepoint access for unpriv
354 * 1 - disallow cpu events for unpriv
355 * 2 - disallow kernel profiling for unpriv
357 int sysctl_perf_event_paranoid __read_mostly
= 2;
359 /* Minimum for 512 kiB + 1 user control page */
360 int sysctl_perf_event_mlock __read_mostly
= 512 + (PAGE_SIZE
/ 1024); /* 'free' kiB per user */
363 * max perf event sample rate
365 #define DEFAULT_MAX_SAMPLE_RATE 100000
366 #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
367 #define DEFAULT_CPU_TIME_MAX_PERCENT 25
369 int sysctl_perf_event_sample_rate __read_mostly
= DEFAULT_MAX_SAMPLE_RATE
;
371 static int max_samples_per_tick __read_mostly
= DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE
, HZ
);
372 static int perf_sample_period_ns __read_mostly
= DEFAULT_SAMPLE_PERIOD_NS
;
374 static int perf_sample_allowed_ns __read_mostly
=
375 DEFAULT_SAMPLE_PERIOD_NS
* DEFAULT_CPU_TIME_MAX_PERCENT
/ 100;
377 static void update_perf_cpu_limits(void)
379 u64 tmp
= perf_sample_period_ns
;
381 tmp
*= sysctl_perf_cpu_time_max_percent
;
382 tmp
= div_u64(tmp
, 100);
386 WRITE_ONCE(perf_sample_allowed_ns
, tmp
);
389 static int perf_rotate_context(struct perf_cpu_context
*cpuctx
);
391 int perf_proc_update_handler(struct ctl_table
*table
, int write
,
392 void __user
*buffer
, size_t *lenp
,
395 int ret
= proc_dointvec_minmax(table
, write
, buffer
, lenp
, ppos
);
401 * If throttling is disabled don't allow the write:
403 if (sysctl_perf_cpu_time_max_percent
== 100 ||
404 sysctl_perf_cpu_time_max_percent
== 0)
407 max_samples_per_tick
= DIV_ROUND_UP(sysctl_perf_event_sample_rate
, HZ
);
408 perf_sample_period_ns
= NSEC_PER_SEC
/ sysctl_perf_event_sample_rate
;
409 update_perf_cpu_limits();
414 int sysctl_perf_cpu_time_max_percent __read_mostly
= DEFAULT_CPU_TIME_MAX_PERCENT
;
416 int perf_cpu_time_max_percent_handler(struct ctl_table
*table
, int write
,
417 void __user
*buffer
, size_t *lenp
,
420 int ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
425 if (sysctl_perf_cpu_time_max_percent
== 100 ||
426 sysctl_perf_cpu_time_max_percent
== 0) {
428 "perf: Dynamic interrupt throttling disabled, can hang your system!\n");
429 WRITE_ONCE(perf_sample_allowed_ns
, 0);
431 update_perf_cpu_limits();
438 * perf samples are done in some very critical code paths (NMIs).
439 * If they take too much CPU time, the system can lock up and not
440 * get any real work done. This will drop the sample rate when
441 * we detect that events are taking too long.
443 #define NR_ACCUMULATED_SAMPLES 128
444 static DEFINE_PER_CPU(u64
, running_sample_length
);
446 static u64 __report_avg
;
447 static u64 __report_allowed
;
449 static void perf_duration_warn(struct irq_work
*w
)
451 printk_ratelimited(KERN_WARNING
452 "perf: interrupt took too long (%lld > %lld), lowering "
453 "kernel.perf_event_max_sample_rate to %d\n",
454 __report_avg
, __report_allowed
,
455 sysctl_perf_event_sample_rate
);
458 static DEFINE_IRQ_WORK(perf_duration_work
, perf_duration_warn
);
460 void perf_sample_event_took(u64 sample_len_ns
)
462 u64 max_len
= READ_ONCE(perf_sample_allowed_ns
);
470 /* Decay the counter by 1 average sample. */
471 running_len
= __this_cpu_read(running_sample_length
);
472 running_len
-= running_len
/NR_ACCUMULATED_SAMPLES
;
473 running_len
+= sample_len_ns
;
474 __this_cpu_write(running_sample_length
, running_len
);
477 * Note: this will be biased artifically low until we have
478 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
479 * from having to maintain a count.
481 avg_len
= running_len
/NR_ACCUMULATED_SAMPLES
;
482 if (avg_len
<= max_len
)
485 __report_avg
= avg_len
;
486 __report_allowed
= max_len
;
489 * Compute a throttle threshold 25% below the current duration.
491 avg_len
+= avg_len
/ 4;
492 max
= (TICK_NSEC
/ 100) * sysctl_perf_cpu_time_max_percent
;
498 WRITE_ONCE(perf_sample_allowed_ns
, avg_len
);
499 WRITE_ONCE(max_samples_per_tick
, max
);
501 sysctl_perf_event_sample_rate
= max
* HZ
;
502 perf_sample_period_ns
= NSEC_PER_SEC
/ sysctl_perf_event_sample_rate
;
504 if (!irq_work_queue(&perf_duration_work
)) {
505 early_printk("perf: interrupt took too long (%lld > %lld), lowering "
506 "kernel.perf_event_max_sample_rate to %d\n",
507 __report_avg
, __report_allowed
,
508 sysctl_perf_event_sample_rate
);
512 static atomic64_t perf_event_id
;
514 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
515 enum event_type_t event_type
);
517 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
518 enum event_type_t event_type
,
519 struct task_struct
*task
);
521 static void update_context_time(struct perf_event_context
*ctx
);
522 static u64
perf_event_time(struct perf_event
*event
);
524 void __weak
perf_event_print_debug(void) { }
526 extern __weak
const char *perf_pmu_name(void)
531 static inline u64
perf_clock(void)
533 return local_clock();
536 static inline u64
perf_event_clock(struct perf_event
*event
)
538 return event
->clock();
541 #ifdef CONFIG_CGROUP_PERF
544 perf_cgroup_match(struct perf_event
*event
)
546 struct perf_event_context
*ctx
= event
->ctx
;
547 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
549 /* @event doesn't care about cgroup */
553 /* wants specific cgroup scope but @cpuctx isn't associated with any */
558 * Cgroup scoping is recursive. An event enabled for a cgroup is
559 * also enabled for all its descendant cgroups. If @cpuctx's
560 * cgroup is a descendant of @event's (the test covers identity
561 * case), it's a match.
563 return cgroup_is_descendant(cpuctx
->cgrp
->css
.cgroup
,
564 event
->cgrp
->css
.cgroup
);
567 static inline void perf_detach_cgroup(struct perf_event
*event
)
569 css_put(&event
->cgrp
->css
);
573 static inline int is_cgroup_event(struct perf_event
*event
)
575 return event
->cgrp
!= NULL
;
578 static inline u64
perf_cgroup_event_time(struct perf_event
*event
)
580 struct perf_cgroup_info
*t
;
582 t
= per_cpu_ptr(event
->cgrp
->info
, event
->cpu
);
586 static inline void __update_cgrp_time(struct perf_cgroup
*cgrp
)
588 struct perf_cgroup_info
*info
;
593 info
= this_cpu_ptr(cgrp
->info
);
595 info
->time
+= now
- info
->timestamp
;
596 info
->timestamp
= now
;
599 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context
*cpuctx
)
601 struct perf_cgroup
*cgrp_out
= cpuctx
->cgrp
;
603 __update_cgrp_time(cgrp_out
);
606 static inline void update_cgrp_time_from_event(struct perf_event
*event
)
608 struct perf_cgroup
*cgrp
;
611 * ensure we access cgroup data only when needed and
612 * when we know the cgroup is pinned (css_get)
614 if (!is_cgroup_event(event
))
617 cgrp
= perf_cgroup_from_task(current
, event
->ctx
);
619 * Do not update time when cgroup is not active
621 if (cgrp
== event
->cgrp
)
622 __update_cgrp_time(event
->cgrp
);
626 perf_cgroup_set_timestamp(struct task_struct
*task
,
627 struct perf_event_context
*ctx
)
629 struct perf_cgroup
*cgrp
;
630 struct perf_cgroup_info
*info
;
633 * ctx->lock held by caller
634 * ensure we do not access cgroup data
635 * unless we have the cgroup pinned (css_get)
637 if (!task
|| !ctx
->nr_cgroups
)
640 cgrp
= perf_cgroup_from_task(task
, ctx
);
641 info
= this_cpu_ptr(cgrp
->info
);
642 info
->timestamp
= ctx
->timestamp
;
645 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
646 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
649 * reschedule events based on the cgroup constraint of task.
651 * mode SWOUT : schedule out everything
652 * mode SWIN : schedule in based on cgroup for next
654 static void perf_cgroup_switch(struct task_struct
*task
, int mode
)
656 struct perf_cpu_context
*cpuctx
;
661 * disable interrupts to avoid geting nr_cgroup
662 * changes via __perf_event_disable(). Also
665 local_irq_save(flags
);
668 * we reschedule only in the presence of cgroup
669 * constrained events.
672 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
673 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
674 if (cpuctx
->unique_pmu
!= pmu
)
675 continue; /* ensure we process each cpuctx once */
678 * perf_cgroup_events says at least one
679 * context on this CPU has cgroup events.
681 * ctx->nr_cgroups reports the number of cgroup
682 * events for a context.
684 if (cpuctx
->ctx
.nr_cgroups
> 0) {
685 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
686 perf_pmu_disable(cpuctx
->ctx
.pmu
);
688 if (mode
& PERF_CGROUP_SWOUT
) {
689 cpu_ctx_sched_out(cpuctx
, EVENT_ALL
);
691 * must not be done before ctxswout due
692 * to event_filter_match() in event_sched_out()
697 if (mode
& PERF_CGROUP_SWIN
) {
698 WARN_ON_ONCE(cpuctx
->cgrp
);
700 * set cgrp before ctxsw in to allow
701 * event_filter_match() to not have to pass
703 * we pass the cpuctx->ctx to perf_cgroup_from_task()
704 * because cgorup events are only per-cpu
706 cpuctx
->cgrp
= perf_cgroup_from_task(task
, &cpuctx
->ctx
);
707 cpu_ctx_sched_in(cpuctx
, EVENT_ALL
, task
);
709 perf_pmu_enable(cpuctx
->ctx
.pmu
);
710 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
714 local_irq_restore(flags
);
717 static inline void perf_cgroup_sched_out(struct task_struct
*task
,
718 struct task_struct
*next
)
720 struct perf_cgroup
*cgrp1
;
721 struct perf_cgroup
*cgrp2
= NULL
;
725 * we come here when we know perf_cgroup_events > 0
726 * we do not need to pass the ctx here because we know
727 * we are holding the rcu lock
729 cgrp1
= perf_cgroup_from_task(task
, NULL
);
730 cgrp2
= perf_cgroup_from_task(next
, NULL
);
733 * only schedule out current cgroup events if we know
734 * that we are switching to a different cgroup. Otherwise,
735 * do no touch the cgroup events.
738 perf_cgroup_switch(task
, PERF_CGROUP_SWOUT
);
743 static inline void perf_cgroup_sched_in(struct task_struct
*prev
,
744 struct task_struct
*task
)
746 struct perf_cgroup
*cgrp1
;
747 struct perf_cgroup
*cgrp2
= NULL
;
751 * we come here when we know perf_cgroup_events > 0
752 * we do not need to pass the ctx here because we know
753 * we are holding the rcu lock
755 cgrp1
= perf_cgroup_from_task(task
, NULL
);
756 cgrp2
= perf_cgroup_from_task(prev
, NULL
);
759 * only need to schedule in cgroup events if we are changing
760 * cgroup during ctxsw. Cgroup events were not scheduled
761 * out of ctxsw out if that was not the case.
764 perf_cgroup_switch(task
, PERF_CGROUP_SWIN
);
769 static inline int perf_cgroup_connect(int fd
, struct perf_event
*event
,
770 struct perf_event_attr
*attr
,
771 struct perf_event
*group_leader
)
773 struct perf_cgroup
*cgrp
;
774 struct cgroup_subsys_state
*css
;
775 struct fd f
= fdget(fd
);
781 css
= css_tryget_online_from_dir(f
.file
->f_path
.dentry
,
782 &perf_event_cgrp_subsys
);
788 cgrp
= container_of(css
, struct perf_cgroup
, css
);
792 * all events in a group must monitor
793 * the same cgroup because a task belongs
794 * to only one perf cgroup at a time
796 if (group_leader
&& group_leader
->cgrp
!= cgrp
) {
797 perf_detach_cgroup(event
);
806 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
808 struct perf_cgroup_info
*t
;
809 t
= per_cpu_ptr(event
->cgrp
->info
, event
->cpu
);
810 event
->shadow_ctx_time
= now
- t
->timestamp
;
814 perf_cgroup_defer_enabled(struct perf_event
*event
)
817 * when the current task's perf cgroup does not match
818 * the event's, we need to remember to call the
819 * perf_mark_enable() function the first time a task with
820 * a matching perf cgroup is scheduled in.
822 if (is_cgroup_event(event
) && !perf_cgroup_match(event
))
823 event
->cgrp_defer_enabled
= 1;
827 perf_cgroup_mark_enabled(struct perf_event
*event
,
828 struct perf_event_context
*ctx
)
830 struct perf_event
*sub
;
831 u64 tstamp
= perf_event_time(event
);
833 if (!event
->cgrp_defer_enabled
)
836 event
->cgrp_defer_enabled
= 0;
838 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
839 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
840 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
) {
841 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
842 sub
->cgrp_defer_enabled
= 0;
846 #else /* !CONFIG_CGROUP_PERF */
849 perf_cgroup_match(struct perf_event
*event
)
854 static inline void perf_detach_cgroup(struct perf_event
*event
)
857 static inline int is_cgroup_event(struct perf_event
*event
)
862 static inline u64
perf_cgroup_event_cgrp_time(struct perf_event
*event
)
867 static inline void update_cgrp_time_from_event(struct perf_event
*event
)
871 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context
*cpuctx
)
875 static inline void perf_cgroup_sched_out(struct task_struct
*task
,
876 struct task_struct
*next
)
880 static inline void perf_cgroup_sched_in(struct task_struct
*prev
,
881 struct task_struct
*task
)
885 static inline int perf_cgroup_connect(pid_t pid
, struct perf_event
*event
,
886 struct perf_event_attr
*attr
,
887 struct perf_event
*group_leader
)
893 perf_cgroup_set_timestamp(struct task_struct
*task
,
894 struct perf_event_context
*ctx
)
899 perf_cgroup_switch(struct task_struct
*task
, struct task_struct
*next
)
904 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
908 static inline u64
perf_cgroup_event_time(struct perf_event
*event
)
914 perf_cgroup_defer_enabled(struct perf_event
*event
)
919 perf_cgroup_mark_enabled(struct perf_event
*event
,
920 struct perf_event_context
*ctx
)
926 * set default to be dependent on timer tick just
929 #define PERF_CPU_HRTIMER (1000 / HZ)
931 * function must be called with interrupts disbled
933 static enum hrtimer_restart
perf_mux_hrtimer_handler(struct hrtimer
*hr
)
935 struct perf_cpu_context
*cpuctx
;
938 WARN_ON(!irqs_disabled());
940 cpuctx
= container_of(hr
, struct perf_cpu_context
, hrtimer
);
941 rotations
= perf_rotate_context(cpuctx
);
943 raw_spin_lock(&cpuctx
->hrtimer_lock
);
945 hrtimer_forward_now(hr
, cpuctx
->hrtimer_interval
);
947 cpuctx
->hrtimer_active
= 0;
948 raw_spin_unlock(&cpuctx
->hrtimer_lock
);
950 return rotations
? HRTIMER_RESTART
: HRTIMER_NORESTART
;
953 static void __perf_mux_hrtimer_init(struct perf_cpu_context
*cpuctx
, int cpu
)
955 struct hrtimer
*timer
= &cpuctx
->hrtimer
;
956 struct pmu
*pmu
= cpuctx
->ctx
.pmu
;
959 /* no multiplexing needed for SW PMU */
960 if (pmu
->task_ctx_nr
== perf_sw_context
)
964 * check default is sane, if not set then force to
965 * default interval (1/tick)
967 interval
= pmu
->hrtimer_interval_ms
;
969 interval
= pmu
->hrtimer_interval_ms
= PERF_CPU_HRTIMER
;
971 cpuctx
->hrtimer_interval
= ns_to_ktime(NSEC_PER_MSEC
* interval
);
973 raw_spin_lock_init(&cpuctx
->hrtimer_lock
);
974 hrtimer_init(timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS_PINNED
);
975 timer
->function
= perf_mux_hrtimer_handler
;
978 static int perf_mux_hrtimer_restart(struct perf_cpu_context
*cpuctx
)
980 struct hrtimer
*timer
= &cpuctx
->hrtimer
;
981 struct pmu
*pmu
= cpuctx
->ctx
.pmu
;
985 if (pmu
->task_ctx_nr
== perf_sw_context
)
988 raw_spin_lock_irqsave(&cpuctx
->hrtimer_lock
, flags
);
989 if (!cpuctx
->hrtimer_active
) {
990 cpuctx
->hrtimer_active
= 1;
991 hrtimer_forward_now(timer
, cpuctx
->hrtimer_interval
);
992 hrtimer_start_expires(timer
, HRTIMER_MODE_ABS_PINNED
);
994 raw_spin_unlock_irqrestore(&cpuctx
->hrtimer_lock
, flags
);
999 void perf_pmu_disable(struct pmu
*pmu
)
1001 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
1003 pmu
->pmu_disable(pmu
);
1006 void perf_pmu_enable(struct pmu
*pmu
)
1008 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
1010 pmu
->pmu_enable(pmu
);
1013 static DEFINE_PER_CPU(struct list_head
, active_ctx_list
);
1016 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
1017 * perf_event_task_tick() are fully serialized because they're strictly cpu
1018 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
1019 * disabled, while perf_event_task_tick is called from IRQ context.
1021 static void perf_event_ctx_activate(struct perf_event_context
*ctx
)
1023 struct list_head
*head
= this_cpu_ptr(&active_ctx_list
);
1025 WARN_ON(!irqs_disabled());
1027 WARN_ON(!list_empty(&ctx
->active_ctx_list
));
1029 list_add(&ctx
->active_ctx_list
, head
);
1032 static void perf_event_ctx_deactivate(struct perf_event_context
*ctx
)
1034 WARN_ON(!irqs_disabled());
1036 WARN_ON(list_empty(&ctx
->active_ctx_list
));
1038 list_del_init(&ctx
->active_ctx_list
);
1041 static void get_ctx(struct perf_event_context
*ctx
)
1043 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
1046 static void free_ctx(struct rcu_head
*head
)
1048 struct perf_event_context
*ctx
;
1050 ctx
= container_of(head
, struct perf_event_context
, rcu_head
);
1051 kfree(ctx
->task_ctx_data
);
1055 static void put_ctx(struct perf_event_context
*ctx
)
1057 if (atomic_dec_and_test(&ctx
->refcount
)) {
1058 if (ctx
->parent_ctx
)
1059 put_ctx(ctx
->parent_ctx
);
1060 if (ctx
->task
&& ctx
->task
!= TASK_TOMBSTONE
)
1061 put_task_struct(ctx
->task
);
1062 call_rcu(&ctx
->rcu_head
, free_ctx
);
1067 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1068 * perf_pmu_migrate_context() we need some magic.
1070 * Those places that change perf_event::ctx will hold both
1071 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1073 * Lock ordering is by mutex address. There are two other sites where
1074 * perf_event_context::mutex nests and those are:
1076 * - perf_event_exit_task_context() [ child , 0 ]
1077 * perf_event_exit_event()
1078 * put_event() [ parent, 1 ]
1080 * - perf_event_init_context() [ parent, 0 ]
1081 * inherit_task_group()
1084 * perf_event_alloc()
1086 * perf_try_init_event() [ child , 1 ]
1088 * While it appears there is an obvious deadlock here -- the parent and child
1089 * nesting levels are inverted between the two. This is in fact safe because
1090 * life-time rules separate them. That is an exiting task cannot fork, and a
1091 * spawning task cannot (yet) exit.
1093 * But remember that that these are parent<->child context relations, and
1094 * migration does not affect children, therefore these two orderings should not
1097 * The change in perf_event::ctx does not affect children (as claimed above)
1098 * because the sys_perf_event_open() case will install a new event and break
1099 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1100 * concerned with cpuctx and that doesn't have children.
1102 * The places that change perf_event::ctx will issue:
1104 * perf_remove_from_context();
1105 * synchronize_rcu();
1106 * perf_install_in_context();
1108 * to affect the change. The remove_from_context() + synchronize_rcu() should
1109 * quiesce the event, after which we can install it in the new location. This
1110 * means that only external vectors (perf_fops, prctl) can perturb the event
1111 * while in transit. Therefore all such accessors should also acquire
1112 * perf_event_context::mutex to serialize against this.
1114 * However; because event->ctx can change while we're waiting to acquire
1115 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1120 * task_struct::perf_event_mutex
1121 * perf_event_context::mutex
1122 * perf_event::child_mutex;
1123 * perf_event_context::lock
1124 * perf_event::mmap_mutex
1127 static struct perf_event_context
*
1128 perf_event_ctx_lock_nested(struct perf_event
*event
, int nesting
)
1130 struct perf_event_context
*ctx
;
1134 ctx
= ACCESS_ONCE(event
->ctx
);
1135 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
1141 mutex_lock_nested(&ctx
->mutex
, nesting
);
1142 if (event
->ctx
!= ctx
) {
1143 mutex_unlock(&ctx
->mutex
);
1151 static inline struct perf_event_context
*
1152 perf_event_ctx_lock(struct perf_event
*event
)
1154 return perf_event_ctx_lock_nested(event
, 0);
1157 static void perf_event_ctx_unlock(struct perf_event
*event
,
1158 struct perf_event_context
*ctx
)
1160 mutex_unlock(&ctx
->mutex
);
1165 * This must be done under the ctx->lock, such as to serialize against
1166 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1167 * calling scheduler related locks and ctx->lock nests inside those.
1169 static __must_check
struct perf_event_context
*
1170 unclone_ctx(struct perf_event_context
*ctx
)
1172 struct perf_event_context
*parent_ctx
= ctx
->parent_ctx
;
1174 lockdep_assert_held(&ctx
->lock
);
1177 ctx
->parent_ctx
= NULL
;
1183 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
1186 * only top level events have the pid namespace they were created in
1189 event
= event
->parent
;
1191 return task_tgid_nr_ns(p
, event
->ns
);
1194 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
1197 * only top level events have the pid namespace they were created in
1200 event
= event
->parent
;
1202 return task_pid_nr_ns(p
, event
->ns
);
1206 * If we inherit events we want to return the parent event id
1209 static u64
primary_event_id(struct perf_event
*event
)
1214 id
= event
->parent
->id
;
1220 * Get the perf_event_context for a task and lock it.
1222 * This has to cope with with the fact that until it is locked,
1223 * the context could get moved to another task.
1225 static struct perf_event_context
*
1226 perf_lock_task_context(struct task_struct
*task
, int ctxn
, unsigned long *flags
)
1228 struct perf_event_context
*ctx
;
1232 * One of the few rules of preemptible RCU is that one cannot do
1233 * rcu_read_unlock() while holding a scheduler (or nested) lock when
1234 * part of the read side critical section was irqs-enabled -- see
1235 * rcu_read_unlock_special().
1237 * Since ctx->lock nests under rq->lock we must ensure the entire read
1238 * side critical section has interrupts disabled.
1240 local_irq_save(*flags
);
1242 ctx
= rcu_dereference(task
->perf_event_ctxp
[ctxn
]);
1245 * If this context is a clone of another, it might
1246 * get swapped for another underneath us by
1247 * perf_event_task_sched_out, though the
1248 * rcu_read_lock() protects us from any context
1249 * getting freed. Lock the context and check if it
1250 * got swapped before we could get the lock, and retry
1251 * if so. If we locked the right context, then it
1252 * can't get swapped on us any more.
1254 raw_spin_lock(&ctx
->lock
);
1255 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
[ctxn
])) {
1256 raw_spin_unlock(&ctx
->lock
);
1258 local_irq_restore(*flags
);
1262 if (ctx
->task
== TASK_TOMBSTONE
||
1263 !atomic_inc_not_zero(&ctx
->refcount
)) {
1264 raw_spin_unlock(&ctx
->lock
);
1267 WARN_ON_ONCE(ctx
->task
!= task
);
1272 local_irq_restore(*flags
);
1277 * Get the context for a task and increment its pin_count so it
1278 * can't get swapped to another task. This also increments its
1279 * reference count so that the context can't get freed.
1281 static struct perf_event_context
*
1282 perf_pin_task_context(struct task_struct
*task
, int ctxn
)
1284 struct perf_event_context
*ctx
;
1285 unsigned long flags
;
1287 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
1290 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1295 static void perf_unpin_context(struct perf_event_context
*ctx
)
1297 unsigned long flags
;
1299 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
1301 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1305 * Update the record of the current time in a context.
1307 static void update_context_time(struct perf_event_context
*ctx
)
1309 u64 now
= perf_clock();
1311 ctx
->time
+= now
- ctx
->timestamp
;
1312 ctx
->timestamp
= now
;
1315 static u64
perf_event_time(struct perf_event
*event
)
1317 struct perf_event_context
*ctx
= event
->ctx
;
1319 if (is_cgroup_event(event
))
1320 return perf_cgroup_event_time(event
);
1322 return ctx
? ctx
->time
: 0;
1326 * Update the total_time_enabled and total_time_running fields for a event.
1328 static void update_event_times(struct perf_event
*event
)
1330 struct perf_event_context
*ctx
= event
->ctx
;
1333 lockdep_assert_held(&ctx
->lock
);
1335 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
1336 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
1340 * in cgroup mode, time_enabled represents
1341 * the time the event was enabled AND active
1342 * tasks were in the monitored cgroup. This is
1343 * independent of the activity of the context as
1344 * there may be a mix of cgroup and non-cgroup events.
1346 * That is why we treat cgroup events differently
1349 if (is_cgroup_event(event
))
1350 run_end
= perf_cgroup_event_time(event
);
1351 else if (ctx
->is_active
)
1352 run_end
= ctx
->time
;
1354 run_end
= event
->tstamp_stopped
;
1356 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
1358 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
1359 run_end
= event
->tstamp_stopped
;
1361 run_end
= perf_event_time(event
);
1363 event
->total_time_running
= run_end
- event
->tstamp_running
;
1368 * Update total_time_enabled and total_time_running for all events in a group.
1370 static void update_group_times(struct perf_event
*leader
)
1372 struct perf_event
*event
;
1374 update_event_times(leader
);
1375 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
1376 update_event_times(event
);
1379 static struct list_head
*
1380 ctx_group_list(struct perf_event
*event
, struct perf_event_context
*ctx
)
1382 if (event
->attr
.pinned
)
1383 return &ctx
->pinned_groups
;
1385 return &ctx
->flexible_groups
;
1389 * Add a event from the lists for its context.
1390 * Must be called with ctx->mutex and ctx->lock held.
1393 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
1395 lockdep_assert_held(&ctx
->lock
);
1397 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_CONTEXT
);
1398 event
->attach_state
|= PERF_ATTACH_CONTEXT
;
1401 * If we're a stand alone event or group leader, we go to the context
1402 * list, group events are kept attached to the group so that
1403 * perf_group_detach can, at all times, locate all siblings.
1405 if (event
->group_leader
== event
) {
1406 struct list_head
*list
;
1408 if (is_software_event(event
))
1409 event
->group_flags
|= PERF_GROUP_SOFTWARE
;
1411 list
= ctx_group_list(event
, ctx
);
1412 list_add_tail(&event
->group_entry
, list
);
1415 if (is_cgroup_event(event
))
1418 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
1420 if (event
->attr
.inherit_stat
)
1427 * Initialize event state based on the perf_event_attr::disabled.
1429 static inline void perf_event__state_init(struct perf_event
*event
)
1431 event
->state
= event
->attr
.disabled
? PERF_EVENT_STATE_OFF
:
1432 PERF_EVENT_STATE_INACTIVE
;
1435 static void __perf_event_read_size(struct perf_event
*event
, int nr_siblings
)
1437 int entry
= sizeof(u64
); /* value */
1441 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1442 size
+= sizeof(u64
);
1444 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1445 size
+= sizeof(u64
);
1447 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
1448 entry
+= sizeof(u64
);
1450 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
1452 size
+= sizeof(u64
);
1456 event
->read_size
= size
;
1459 static void __perf_event_header_size(struct perf_event
*event
, u64 sample_type
)
1461 struct perf_sample_data
*data
;
1464 if (sample_type
& PERF_SAMPLE_IP
)
1465 size
+= sizeof(data
->ip
);
1467 if (sample_type
& PERF_SAMPLE_ADDR
)
1468 size
+= sizeof(data
->addr
);
1470 if (sample_type
& PERF_SAMPLE_PERIOD
)
1471 size
+= sizeof(data
->period
);
1473 if (sample_type
& PERF_SAMPLE_WEIGHT
)
1474 size
+= sizeof(data
->weight
);
1476 if (sample_type
& PERF_SAMPLE_READ
)
1477 size
+= event
->read_size
;
1479 if (sample_type
& PERF_SAMPLE_DATA_SRC
)
1480 size
+= sizeof(data
->data_src
.val
);
1482 if (sample_type
& PERF_SAMPLE_TRANSACTION
)
1483 size
+= sizeof(data
->txn
);
1485 event
->header_size
= size
;
1489 * Called at perf_event creation and when events are attached/detached from a
1492 static void perf_event__header_size(struct perf_event
*event
)
1494 __perf_event_read_size(event
,
1495 event
->group_leader
->nr_siblings
);
1496 __perf_event_header_size(event
, event
->attr
.sample_type
);
1499 static void perf_event__id_header_size(struct perf_event
*event
)
1501 struct perf_sample_data
*data
;
1502 u64 sample_type
= event
->attr
.sample_type
;
1505 if (sample_type
& PERF_SAMPLE_TID
)
1506 size
+= sizeof(data
->tid_entry
);
1508 if (sample_type
& PERF_SAMPLE_TIME
)
1509 size
+= sizeof(data
->time
);
1511 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
1512 size
+= sizeof(data
->id
);
1514 if (sample_type
& PERF_SAMPLE_ID
)
1515 size
+= sizeof(data
->id
);
1517 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
1518 size
+= sizeof(data
->stream_id
);
1520 if (sample_type
& PERF_SAMPLE_CPU
)
1521 size
+= sizeof(data
->cpu_entry
);
1523 event
->id_header_size
= size
;
1526 static bool perf_event_validate_size(struct perf_event
*event
)
1529 * The values computed here will be over-written when we actually
1532 __perf_event_read_size(event
, event
->group_leader
->nr_siblings
+ 1);
1533 __perf_event_header_size(event
, event
->attr
.sample_type
& ~PERF_SAMPLE_READ
);
1534 perf_event__id_header_size(event
);
1537 * Sum the lot; should not exceed the 64k limit we have on records.
1538 * Conservative limit to allow for callchains and other variable fields.
1540 if (event
->read_size
+ event
->header_size
+
1541 event
->id_header_size
+ sizeof(struct perf_event_header
) >= 16*1024)
1547 static void perf_group_attach(struct perf_event
*event
)
1549 struct perf_event
*group_leader
= event
->group_leader
, *pos
;
1552 * We can have double attach due to group movement in perf_event_open.
1554 if (event
->attach_state
& PERF_ATTACH_GROUP
)
1557 event
->attach_state
|= PERF_ATTACH_GROUP
;
1559 if (group_leader
== event
)
1562 WARN_ON_ONCE(group_leader
->ctx
!= event
->ctx
);
1564 if (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
&&
1565 !is_software_event(event
))
1566 group_leader
->group_flags
&= ~PERF_GROUP_SOFTWARE
;
1568 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
1569 group_leader
->nr_siblings
++;
1571 perf_event__header_size(group_leader
);
1573 list_for_each_entry(pos
, &group_leader
->sibling_list
, group_entry
)
1574 perf_event__header_size(pos
);
1578 * Remove a event from the lists for its context.
1579 * Must be called with ctx->mutex and ctx->lock held.
1582 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
1584 struct perf_cpu_context
*cpuctx
;
1586 WARN_ON_ONCE(event
->ctx
!= ctx
);
1587 lockdep_assert_held(&ctx
->lock
);
1590 * We can have double detach due to exit/hot-unplug + close.
1592 if (!(event
->attach_state
& PERF_ATTACH_CONTEXT
))
1595 event
->attach_state
&= ~PERF_ATTACH_CONTEXT
;
1597 if (is_cgroup_event(event
)) {
1600 * Because cgroup events are always per-cpu events, this will
1601 * always be called from the right CPU.
1603 cpuctx
= __get_cpu_context(ctx
);
1605 * If there are no more cgroup events then clear cgrp to avoid
1606 * stale pointer in update_cgrp_time_from_cpuctx().
1608 if (!ctx
->nr_cgroups
)
1609 cpuctx
->cgrp
= NULL
;
1613 if (event
->attr
.inherit_stat
)
1616 list_del_rcu(&event
->event_entry
);
1618 if (event
->group_leader
== event
)
1619 list_del_init(&event
->group_entry
);
1621 update_group_times(event
);
1624 * If event was in error state, then keep it
1625 * that way, otherwise bogus counts will be
1626 * returned on read(). The only way to get out
1627 * of error state is by explicit re-enabling
1630 if (event
->state
> PERF_EVENT_STATE_OFF
)
1631 event
->state
= PERF_EVENT_STATE_OFF
;
1636 static void perf_group_detach(struct perf_event
*event
)
1638 struct perf_event
*sibling
, *tmp
;
1639 struct list_head
*list
= NULL
;
1642 * We can have double detach due to exit/hot-unplug + close.
1644 if (!(event
->attach_state
& PERF_ATTACH_GROUP
))
1647 event
->attach_state
&= ~PERF_ATTACH_GROUP
;
1650 * If this is a sibling, remove it from its group.
1652 if (event
->group_leader
!= event
) {
1653 list_del_init(&event
->group_entry
);
1654 event
->group_leader
->nr_siblings
--;
1658 if (!list_empty(&event
->group_entry
))
1659 list
= &event
->group_entry
;
1662 * If this was a group event with sibling events then
1663 * upgrade the siblings to singleton events by adding them
1664 * to whatever list we are on.
1666 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
1668 list_move_tail(&sibling
->group_entry
, list
);
1669 sibling
->group_leader
= sibling
;
1671 /* Inherit group flags from the previous leader */
1672 sibling
->group_flags
= event
->group_flags
;
1674 WARN_ON_ONCE(sibling
->ctx
!= event
->ctx
);
1678 perf_event__header_size(event
->group_leader
);
1680 list_for_each_entry(tmp
, &event
->group_leader
->sibling_list
, group_entry
)
1681 perf_event__header_size(tmp
);
1684 static bool is_orphaned_event(struct perf_event
*event
)
1686 return event
->state
== PERF_EVENT_STATE_DEAD
;
1689 static inline int __pmu_filter_match(struct perf_event
*event
)
1691 struct pmu
*pmu
= event
->pmu
;
1692 return pmu
->filter_match
? pmu
->filter_match(event
) : 1;
1696 * Check whether we should attempt to schedule an event group based on
1697 * PMU-specific filtering. An event group can consist of HW and SW events,
1698 * potentially with a SW leader, so we must check all the filters, to
1699 * determine whether a group is schedulable:
1701 static inline int pmu_filter_match(struct perf_event
*event
)
1703 struct perf_event
*child
;
1705 if (!__pmu_filter_match(event
))
1708 list_for_each_entry(child
, &event
->sibling_list
, group_entry
) {
1709 if (!__pmu_filter_match(child
))
1717 event_filter_match(struct perf_event
*event
)
1719 return (event
->cpu
== -1 || event
->cpu
== smp_processor_id())
1720 && perf_cgroup_match(event
) && pmu_filter_match(event
);
1724 event_sched_out(struct perf_event
*event
,
1725 struct perf_cpu_context
*cpuctx
,
1726 struct perf_event_context
*ctx
)
1728 u64 tstamp
= perf_event_time(event
);
1731 WARN_ON_ONCE(event
->ctx
!= ctx
);
1732 lockdep_assert_held(&ctx
->lock
);
1735 * An event which could not be activated because of
1736 * filter mismatch still needs to have its timings
1737 * maintained, otherwise bogus information is return
1738 * via read() for time_enabled, time_running:
1740 if (event
->state
== PERF_EVENT_STATE_INACTIVE
1741 && !event_filter_match(event
)) {
1742 delta
= tstamp
- event
->tstamp_stopped
;
1743 event
->tstamp_running
+= delta
;
1744 event
->tstamp_stopped
= tstamp
;
1747 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1750 perf_pmu_disable(event
->pmu
);
1752 event
->tstamp_stopped
= tstamp
;
1753 event
->pmu
->del(event
, 0);
1755 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1756 if (event
->pending_disable
) {
1757 event
->pending_disable
= 0;
1758 event
->state
= PERF_EVENT_STATE_OFF
;
1761 if (!is_software_event(event
))
1762 cpuctx
->active_oncpu
--;
1763 if (!--ctx
->nr_active
)
1764 perf_event_ctx_deactivate(ctx
);
1765 if (event
->attr
.freq
&& event
->attr
.sample_freq
)
1767 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
1768 cpuctx
->exclusive
= 0;
1770 perf_pmu_enable(event
->pmu
);
1774 group_sched_out(struct perf_event
*group_event
,
1775 struct perf_cpu_context
*cpuctx
,
1776 struct perf_event_context
*ctx
)
1778 struct perf_event
*event
;
1779 int state
= group_event
->state
;
1781 event_sched_out(group_event
, cpuctx
, ctx
);
1784 * Schedule out siblings (if any):
1786 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
1787 event_sched_out(event
, cpuctx
, ctx
);
1789 if (state
== PERF_EVENT_STATE_ACTIVE
&& group_event
->attr
.exclusive
)
1790 cpuctx
->exclusive
= 0;
1793 #define DETACH_GROUP 0x01UL
1796 * Cross CPU call to remove a performance event
1798 * We disable the event on the hardware level first. After that we
1799 * remove it from the context list.
1802 __perf_remove_from_context(struct perf_event
*event
,
1803 struct perf_cpu_context
*cpuctx
,
1804 struct perf_event_context
*ctx
,
1807 unsigned long flags
= (unsigned long)info
;
1809 event_sched_out(event
, cpuctx
, ctx
);
1810 if (flags
& DETACH_GROUP
)
1811 perf_group_detach(event
);
1812 list_del_event(event
, ctx
);
1814 if (!ctx
->nr_events
&& ctx
->is_active
) {
1817 WARN_ON_ONCE(cpuctx
->task_ctx
!= ctx
);
1818 cpuctx
->task_ctx
= NULL
;
1824 * Remove the event from a task's (or a CPU's) list of events.
1826 * If event->ctx is a cloned context, callers must make sure that
1827 * every task struct that event->ctx->task could possibly point to
1828 * remains valid. This is OK when called from perf_release since
1829 * that only calls us on the top-level context, which can't be a clone.
1830 * When called from perf_event_exit_task, it's OK because the
1831 * context has been detached from its task.
1833 static void perf_remove_from_context(struct perf_event
*event
, unsigned long flags
)
1835 lockdep_assert_held(&event
->ctx
->mutex
);
1837 event_function_call(event
, __perf_remove_from_context
, (void *)flags
);
1841 * Cross CPU call to disable a performance event
1843 static void __perf_event_disable(struct perf_event
*event
,
1844 struct perf_cpu_context
*cpuctx
,
1845 struct perf_event_context
*ctx
,
1848 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
1851 update_context_time(ctx
);
1852 update_cgrp_time_from_event(event
);
1853 update_group_times(event
);
1854 if (event
== event
->group_leader
)
1855 group_sched_out(event
, cpuctx
, ctx
);
1857 event_sched_out(event
, cpuctx
, ctx
);
1858 event
->state
= PERF_EVENT_STATE_OFF
;
1864 * If event->ctx is a cloned context, callers must make sure that
1865 * every task struct that event->ctx->task could possibly point to
1866 * remains valid. This condition is satisifed when called through
1867 * perf_event_for_each_child or perf_event_for_each because they
1868 * hold the top-level event's child_mutex, so any descendant that
1869 * goes to exit will block in perf_event_exit_event().
1871 * When called from perf_pending_event it's OK because event->ctx
1872 * is the current context on this CPU and preemption is disabled,
1873 * hence we can't get into perf_event_task_sched_out for this context.
1875 static void _perf_event_disable(struct perf_event
*event
)
1877 struct perf_event_context
*ctx
= event
->ctx
;
1879 raw_spin_lock_irq(&ctx
->lock
);
1880 if (event
->state
<= PERF_EVENT_STATE_OFF
) {
1881 raw_spin_unlock_irq(&ctx
->lock
);
1884 raw_spin_unlock_irq(&ctx
->lock
);
1886 event_function_call(event
, __perf_event_disable
, NULL
);
1889 void perf_event_disable_local(struct perf_event
*event
)
1891 event_function_local(event
, __perf_event_disable
, NULL
);
1895 * Strictly speaking kernel users cannot create groups and therefore this
1896 * interface does not need the perf_event_ctx_lock() magic.
1898 void perf_event_disable(struct perf_event
*event
)
1900 struct perf_event_context
*ctx
;
1902 ctx
= perf_event_ctx_lock(event
);
1903 _perf_event_disable(event
);
1904 perf_event_ctx_unlock(event
, ctx
);
1906 EXPORT_SYMBOL_GPL(perf_event_disable
);
1908 static void perf_set_shadow_time(struct perf_event
*event
,
1909 struct perf_event_context
*ctx
,
1913 * use the correct time source for the time snapshot
1915 * We could get by without this by leveraging the
1916 * fact that to get to this function, the caller
1917 * has most likely already called update_context_time()
1918 * and update_cgrp_time_xx() and thus both timestamp
1919 * are identical (or very close). Given that tstamp is,
1920 * already adjusted for cgroup, we could say that:
1921 * tstamp - ctx->timestamp
1923 * tstamp - cgrp->timestamp.
1925 * Then, in perf_output_read(), the calculation would
1926 * work with no changes because:
1927 * - event is guaranteed scheduled in
1928 * - no scheduled out in between
1929 * - thus the timestamp would be the same
1931 * But this is a bit hairy.
1933 * So instead, we have an explicit cgroup call to remain
1934 * within the time time source all along. We believe it
1935 * is cleaner and simpler to understand.
1937 if (is_cgroup_event(event
))
1938 perf_cgroup_set_shadow_time(event
, tstamp
);
1940 event
->shadow_ctx_time
= tstamp
- ctx
->timestamp
;
1943 #define MAX_INTERRUPTS (~0ULL)
1945 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1946 static void perf_log_itrace_start(struct perf_event
*event
);
1949 event_sched_in(struct perf_event
*event
,
1950 struct perf_cpu_context
*cpuctx
,
1951 struct perf_event_context
*ctx
)
1953 u64 tstamp
= perf_event_time(event
);
1956 lockdep_assert_held(&ctx
->lock
);
1958 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1961 WRITE_ONCE(event
->oncpu
, smp_processor_id());
1963 * Order event::oncpu write to happen before the ACTIVE state
1967 WRITE_ONCE(event
->state
, PERF_EVENT_STATE_ACTIVE
);
1970 * Unthrottle events, since we scheduled we might have missed several
1971 * ticks already, also for a heavily scheduling task there is little
1972 * guarantee it'll get a tick in a timely manner.
1974 if (unlikely(event
->hw
.interrupts
== MAX_INTERRUPTS
)) {
1975 perf_log_throttle(event
, 1);
1976 event
->hw
.interrupts
= 0;
1980 * The new state must be visible before we turn it on in the hardware:
1984 perf_pmu_disable(event
->pmu
);
1986 perf_set_shadow_time(event
, ctx
, tstamp
);
1988 perf_log_itrace_start(event
);
1990 if (event
->pmu
->add(event
, PERF_EF_START
)) {
1991 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1997 event
->tstamp_running
+= tstamp
- event
->tstamp_stopped
;
1999 if (!is_software_event(event
))
2000 cpuctx
->active_oncpu
++;
2001 if (!ctx
->nr_active
++)
2002 perf_event_ctx_activate(ctx
);
2003 if (event
->attr
.freq
&& event
->attr
.sample_freq
)
2006 if (event
->attr
.exclusive
)
2007 cpuctx
->exclusive
= 1;
2010 perf_pmu_enable(event
->pmu
);
2016 group_sched_in(struct perf_event
*group_event
,
2017 struct perf_cpu_context
*cpuctx
,
2018 struct perf_event_context
*ctx
)
2020 struct perf_event
*event
, *partial_group
= NULL
;
2021 struct pmu
*pmu
= ctx
->pmu
;
2022 u64 now
= ctx
->time
;
2023 bool simulate
= false;
2025 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
2028 pmu
->start_txn(pmu
, PERF_PMU_TXN_ADD
);
2030 if (event_sched_in(group_event
, cpuctx
, ctx
)) {
2031 pmu
->cancel_txn(pmu
);
2032 perf_mux_hrtimer_restart(cpuctx
);
2037 * Schedule in siblings as one group (if any):
2039 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
2040 if (event_sched_in(event
, cpuctx
, ctx
)) {
2041 partial_group
= event
;
2046 if (!pmu
->commit_txn(pmu
))
2051 * Groups can be scheduled in as one unit only, so undo any
2052 * partial group before returning:
2053 * The events up to the failed event are scheduled out normally,
2054 * tstamp_stopped will be updated.
2056 * The failed events and the remaining siblings need to have
2057 * their timings updated as if they had gone thru event_sched_in()
2058 * and event_sched_out(). This is required to get consistent timings
2059 * across the group. This also takes care of the case where the group
2060 * could never be scheduled by ensuring tstamp_stopped is set to mark
2061 * the time the event was actually stopped, such that time delta
2062 * calculation in update_event_times() is correct.
2064 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
2065 if (event
== partial_group
)
2069 event
->tstamp_running
+= now
- event
->tstamp_stopped
;
2070 event
->tstamp_stopped
= now
;
2072 event_sched_out(event
, cpuctx
, ctx
);
2075 event_sched_out(group_event
, cpuctx
, ctx
);
2077 pmu
->cancel_txn(pmu
);
2079 perf_mux_hrtimer_restart(cpuctx
);
2085 * Work out whether we can put this event group on the CPU now.
2087 static int group_can_go_on(struct perf_event
*event
,
2088 struct perf_cpu_context
*cpuctx
,
2092 * Groups consisting entirely of software events can always go on.
2094 if (event
->group_flags
& PERF_GROUP_SOFTWARE
)
2097 * If an exclusive group is already on, no other hardware
2100 if (cpuctx
->exclusive
)
2103 * If this group is exclusive and there are already
2104 * events on the CPU, it can't go on.
2106 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
2109 * Otherwise, try to add it if all previous groups were able
2115 static void add_event_to_ctx(struct perf_event
*event
,
2116 struct perf_event_context
*ctx
)
2118 u64 tstamp
= perf_event_time(event
);
2120 list_add_event(event
, ctx
);
2121 perf_group_attach(event
);
2122 event
->tstamp_enabled
= tstamp
;
2123 event
->tstamp_running
= tstamp
;
2124 event
->tstamp_stopped
= tstamp
;
2127 static void ctx_sched_out(struct perf_event_context
*ctx
,
2128 struct perf_cpu_context
*cpuctx
,
2129 enum event_type_t event_type
);
2131 ctx_sched_in(struct perf_event_context
*ctx
,
2132 struct perf_cpu_context
*cpuctx
,
2133 enum event_type_t event_type
,
2134 struct task_struct
*task
);
2136 static void task_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
2137 struct perf_event_context
*ctx
)
2139 if (!cpuctx
->task_ctx
)
2142 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
2145 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
2148 static void perf_event_sched_in(struct perf_cpu_context
*cpuctx
,
2149 struct perf_event_context
*ctx
,
2150 struct task_struct
*task
)
2152 cpu_ctx_sched_in(cpuctx
, EVENT_PINNED
, task
);
2154 ctx_sched_in(ctx
, cpuctx
, EVENT_PINNED
, task
);
2155 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
, task
);
2157 ctx_sched_in(ctx
, cpuctx
, EVENT_FLEXIBLE
, task
);
2160 static void ctx_resched(struct perf_cpu_context
*cpuctx
,
2161 struct perf_event_context
*task_ctx
)
2163 perf_pmu_disable(cpuctx
->ctx
.pmu
);
2165 task_ctx_sched_out(cpuctx
, task_ctx
);
2166 cpu_ctx_sched_out(cpuctx
, EVENT_ALL
);
2167 perf_event_sched_in(cpuctx
, task_ctx
, current
);
2168 perf_pmu_enable(cpuctx
->ctx
.pmu
);
2172 * Cross CPU call to install and enable a performance event
2174 * Very similar to remote_function() + event_function() but cannot assume that
2175 * things like ctx->is_active and cpuctx->task_ctx are set.
2177 static int __perf_install_in_context(void *info
)
2179 struct perf_event
*event
= info
;
2180 struct perf_event_context
*ctx
= event
->ctx
;
2181 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
2182 struct perf_event_context
*task_ctx
= cpuctx
->task_ctx
;
2183 bool activate
= true;
2186 raw_spin_lock(&cpuctx
->ctx
.lock
);
2188 raw_spin_lock(&ctx
->lock
);
2191 /* If we're on the wrong CPU, try again */
2192 if (task_cpu(ctx
->task
) != smp_processor_id()) {
2198 * If we're on the right CPU, see if the task we target is
2199 * current, if not we don't have to activate the ctx, a future
2200 * context switch will do that for us.
2202 if (ctx
->task
!= current
)
2205 WARN_ON_ONCE(cpuctx
->task_ctx
&& cpuctx
->task_ctx
!= ctx
);
2207 } else if (task_ctx
) {
2208 raw_spin_lock(&task_ctx
->lock
);
2212 ctx_sched_out(ctx
, cpuctx
, EVENT_TIME
);
2213 add_event_to_ctx(event
, ctx
);
2214 ctx_resched(cpuctx
, task_ctx
);
2216 add_event_to_ctx(event
, ctx
);
2220 perf_ctx_unlock(cpuctx
, task_ctx
);
2226 * Attach a performance event to a context.
2228 * Very similar to event_function_call, see comment there.
2231 perf_install_in_context(struct perf_event_context
*ctx
,
2232 struct perf_event
*event
,
2235 struct task_struct
*task
= READ_ONCE(ctx
->task
);
2237 lockdep_assert_held(&ctx
->mutex
);
2240 if (event
->cpu
!= -1)
2244 cpu_function_call(cpu
, __perf_install_in_context
, event
);
2249 * Should not happen, we validate the ctx is still alive before calling.
2251 if (WARN_ON_ONCE(task
== TASK_TOMBSTONE
))
2255 * Installing events is tricky because we cannot rely on ctx->is_active
2256 * to be set in case this is the nr_events 0 -> 1 transition.
2260 * Cannot use task_function_call() because we need to run on the task's
2261 * CPU regardless of whether its current or not.
2263 if (!cpu_function_call(task_cpu(task
), __perf_install_in_context
, event
))
2266 raw_spin_lock_irq(&ctx
->lock
);
2268 if (WARN_ON_ONCE(task
== TASK_TOMBSTONE
)) {
2270 * Cannot happen because we already checked above (which also
2271 * cannot happen), and we hold ctx->mutex, which serializes us
2272 * against perf_event_exit_task_context().
2274 raw_spin_unlock_irq(&ctx
->lock
);
2277 raw_spin_unlock_irq(&ctx
->lock
);
2279 * Since !ctx->is_active doesn't mean anything, we must IPI
2286 * Put a event into inactive state and update time fields.
2287 * Enabling the leader of a group effectively enables all
2288 * the group members that aren't explicitly disabled, so we
2289 * have to update their ->tstamp_enabled also.
2290 * Note: this works for group members as well as group leaders
2291 * since the non-leader members' sibling_lists will be empty.
2293 static void __perf_event_mark_enabled(struct perf_event
*event
)
2295 struct perf_event
*sub
;
2296 u64 tstamp
= perf_event_time(event
);
2298 event
->state
= PERF_EVENT_STATE_INACTIVE
;
2299 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
2300 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
2301 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
)
2302 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
2307 * Cross CPU call to enable a performance event
2309 static void __perf_event_enable(struct perf_event
*event
,
2310 struct perf_cpu_context
*cpuctx
,
2311 struct perf_event_context
*ctx
,
2314 struct perf_event
*leader
= event
->group_leader
;
2315 struct perf_event_context
*task_ctx
;
2317 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
||
2318 event
->state
<= PERF_EVENT_STATE_ERROR
)
2322 ctx_sched_out(ctx
, cpuctx
, EVENT_TIME
);
2324 __perf_event_mark_enabled(event
);
2326 if (!ctx
->is_active
)
2329 if (!event_filter_match(event
)) {
2330 if (is_cgroup_event(event
))
2331 perf_cgroup_defer_enabled(event
);
2332 ctx_sched_in(ctx
, cpuctx
, EVENT_TIME
, current
);
2337 * If the event is in a group and isn't the group leader,
2338 * then don't put it on unless the group is on.
2340 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
) {
2341 ctx_sched_in(ctx
, cpuctx
, EVENT_TIME
, current
);
2345 task_ctx
= cpuctx
->task_ctx
;
2347 WARN_ON_ONCE(task_ctx
!= ctx
);
2349 ctx_resched(cpuctx
, task_ctx
);
2355 * If event->ctx is a cloned context, callers must make sure that
2356 * every task struct that event->ctx->task could possibly point to
2357 * remains valid. This condition is satisfied when called through
2358 * perf_event_for_each_child or perf_event_for_each as described
2359 * for perf_event_disable.
2361 static void _perf_event_enable(struct perf_event
*event
)
2363 struct perf_event_context
*ctx
= event
->ctx
;
2365 raw_spin_lock_irq(&ctx
->lock
);
2366 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
||
2367 event
->state
< PERF_EVENT_STATE_ERROR
) {
2368 raw_spin_unlock_irq(&ctx
->lock
);
2373 * If the event is in error state, clear that first.
2375 * That way, if we see the event in error state below, we know that it
2376 * has gone back into error state, as distinct from the task having
2377 * been scheduled away before the cross-call arrived.
2379 if (event
->state
== PERF_EVENT_STATE_ERROR
)
2380 event
->state
= PERF_EVENT_STATE_OFF
;
2381 raw_spin_unlock_irq(&ctx
->lock
);
2383 event_function_call(event
, __perf_event_enable
, NULL
);
2387 * See perf_event_disable();
2389 void perf_event_enable(struct perf_event
*event
)
2391 struct perf_event_context
*ctx
;
2393 ctx
= perf_event_ctx_lock(event
);
2394 _perf_event_enable(event
);
2395 perf_event_ctx_unlock(event
, ctx
);
2397 EXPORT_SYMBOL_GPL(perf_event_enable
);
2399 struct stop_event_data
{
2400 struct perf_event
*event
;
2401 unsigned int restart
;
2404 static int __perf_event_stop(void *info
)
2406 struct stop_event_data
*sd
= info
;
2407 struct perf_event
*event
= sd
->event
;
2409 /* if it's already INACTIVE, do nothing */
2410 if (READ_ONCE(event
->state
) != PERF_EVENT_STATE_ACTIVE
)
2413 /* matches smp_wmb() in event_sched_in() */
2417 * There is a window with interrupts enabled before we get here,
2418 * so we need to check again lest we try to stop another CPU's event.
2420 if (READ_ONCE(event
->oncpu
) != smp_processor_id())
2423 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
2426 * May race with the actual stop (through perf_pmu_output_stop()),
2427 * but it is only used for events with AUX ring buffer, and such
2428 * events will refuse to restart because of rb::aux_mmap_count==0,
2429 * see comments in perf_aux_output_begin().
2431 * Since this is happening on a event-local CPU, no trace is lost
2435 event
->pmu
->start(event
, PERF_EF_START
);
2440 static int perf_event_restart(struct perf_event
*event
)
2442 struct stop_event_data sd
= {
2449 if (READ_ONCE(event
->state
) != PERF_EVENT_STATE_ACTIVE
)
2452 /* matches smp_wmb() in event_sched_in() */
2456 * We only want to restart ACTIVE events, so if the event goes
2457 * inactive here (event->oncpu==-1), there's nothing more to do;
2458 * fall through with ret==-ENXIO.
2460 ret
= cpu_function_call(READ_ONCE(event
->oncpu
),
2461 __perf_event_stop
, &sd
);
2462 } while (ret
== -EAGAIN
);
2468 * In order to contain the amount of racy and tricky in the address filter
2469 * configuration management, it is a two part process:
2471 * (p1) when userspace mappings change as a result of (1) or (2) or (3) below,
2472 * we update the addresses of corresponding vmas in
2473 * event::addr_filters_offs array and bump the event::addr_filters_gen;
2474 * (p2) when an event is scheduled in (pmu::add), it calls
2475 * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync()
2476 * if the generation has changed since the previous call.
2478 * If (p1) happens while the event is active, we restart it to force (p2).
2480 * (1) perf_addr_filters_apply(): adjusting filters' offsets based on
2481 * pre-existing mappings, called once when new filters arrive via SET_FILTER
2483 * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly
2484 * registered mapping, called for every new mmap(), with mm::mmap_sem down
2486 * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process
2489 void perf_event_addr_filters_sync(struct perf_event
*event
)
2491 struct perf_addr_filters_head
*ifh
= perf_event_addr_filters(event
);
2493 if (!has_addr_filter(event
))
2496 raw_spin_lock(&ifh
->lock
);
2497 if (event
->addr_filters_gen
!= event
->hw
.addr_filters_gen
) {
2498 event
->pmu
->addr_filters_sync(event
);
2499 event
->hw
.addr_filters_gen
= event
->addr_filters_gen
;
2501 raw_spin_unlock(&ifh
->lock
);
2503 EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync
);
2505 static int _perf_event_refresh(struct perf_event
*event
, int refresh
)
2508 * not supported on inherited events
2510 if (event
->attr
.inherit
|| !is_sampling_event(event
))
2513 atomic_add(refresh
, &event
->event_limit
);
2514 _perf_event_enable(event
);
2520 * See perf_event_disable()
2522 int perf_event_refresh(struct perf_event
*event
, int refresh
)
2524 struct perf_event_context
*ctx
;
2527 ctx
= perf_event_ctx_lock(event
);
2528 ret
= _perf_event_refresh(event
, refresh
);
2529 perf_event_ctx_unlock(event
, ctx
);
2533 EXPORT_SYMBOL_GPL(perf_event_refresh
);
2535 static void ctx_sched_out(struct perf_event_context
*ctx
,
2536 struct perf_cpu_context
*cpuctx
,
2537 enum event_type_t event_type
)
2539 int is_active
= ctx
->is_active
;
2540 struct perf_event
*event
;
2542 lockdep_assert_held(&ctx
->lock
);
2544 if (likely(!ctx
->nr_events
)) {
2546 * See __perf_remove_from_context().
2548 WARN_ON_ONCE(ctx
->is_active
);
2550 WARN_ON_ONCE(cpuctx
->task_ctx
);
2554 ctx
->is_active
&= ~event_type
;
2555 if (!(ctx
->is_active
& EVENT_ALL
))
2559 WARN_ON_ONCE(cpuctx
->task_ctx
!= ctx
);
2560 if (!ctx
->is_active
)
2561 cpuctx
->task_ctx
= NULL
;
2565 * Always update time if it was set; not only when it changes.
2566 * Otherwise we can 'forget' to update time for any but the last
2567 * context we sched out. For example:
2569 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
2570 * ctx_sched_out(.event_type = EVENT_PINNED)
2572 * would only update time for the pinned events.
2574 if (is_active
& EVENT_TIME
) {
2575 /* update (and stop) ctx time */
2576 update_context_time(ctx
);
2577 update_cgrp_time_from_cpuctx(cpuctx
);
2580 is_active
^= ctx
->is_active
; /* changed bits */
2582 if (!ctx
->nr_active
|| !(is_active
& EVENT_ALL
))
2585 perf_pmu_disable(ctx
->pmu
);
2586 if (is_active
& EVENT_PINNED
) {
2587 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
)
2588 group_sched_out(event
, cpuctx
, ctx
);
2591 if (is_active
& EVENT_FLEXIBLE
) {
2592 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
)
2593 group_sched_out(event
, cpuctx
, ctx
);
2595 perf_pmu_enable(ctx
->pmu
);
2599 * Test whether two contexts are equivalent, i.e. whether they have both been
2600 * cloned from the same version of the same context.
2602 * Equivalence is measured using a generation number in the context that is
2603 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2604 * and list_del_event().
2606 static int context_equiv(struct perf_event_context
*ctx1
,
2607 struct perf_event_context
*ctx2
)
2609 lockdep_assert_held(&ctx1
->lock
);
2610 lockdep_assert_held(&ctx2
->lock
);
2612 /* Pinning disables the swap optimization */
2613 if (ctx1
->pin_count
|| ctx2
->pin_count
)
2616 /* If ctx1 is the parent of ctx2 */
2617 if (ctx1
== ctx2
->parent_ctx
&& ctx1
->generation
== ctx2
->parent_gen
)
2620 /* If ctx2 is the parent of ctx1 */
2621 if (ctx1
->parent_ctx
== ctx2
&& ctx1
->parent_gen
== ctx2
->generation
)
2625 * If ctx1 and ctx2 have the same parent; we flatten the parent
2626 * hierarchy, see perf_event_init_context().
2628 if (ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
&&
2629 ctx1
->parent_gen
== ctx2
->parent_gen
)
2636 static void __perf_event_sync_stat(struct perf_event
*event
,
2637 struct perf_event
*next_event
)
2641 if (!event
->attr
.inherit_stat
)
2645 * Update the event value, we cannot use perf_event_read()
2646 * because we're in the middle of a context switch and have IRQs
2647 * disabled, which upsets smp_call_function_single(), however
2648 * we know the event must be on the current CPU, therefore we
2649 * don't need to use it.
2651 switch (event
->state
) {
2652 case PERF_EVENT_STATE_ACTIVE
:
2653 event
->pmu
->read(event
);
2656 case PERF_EVENT_STATE_INACTIVE
:
2657 update_event_times(event
);
2665 * In order to keep per-task stats reliable we need to flip the event
2666 * values when we flip the contexts.
2668 value
= local64_read(&next_event
->count
);
2669 value
= local64_xchg(&event
->count
, value
);
2670 local64_set(&next_event
->count
, value
);
2672 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
2673 swap(event
->total_time_running
, next_event
->total_time_running
);
2676 * Since we swizzled the values, update the user visible data too.
2678 perf_event_update_userpage(event
);
2679 perf_event_update_userpage(next_event
);
2682 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
2683 struct perf_event_context
*next_ctx
)
2685 struct perf_event
*event
, *next_event
;
2690 update_context_time(ctx
);
2692 event
= list_first_entry(&ctx
->event_list
,
2693 struct perf_event
, event_entry
);
2695 next_event
= list_first_entry(&next_ctx
->event_list
,
2696 struct perf_event
, event_entry
);
2698 while (&event
->event_entry
!= &ctx
->event_list
&&
2699 &next_event
->event_entry
!= &next_ctx
->event_list
) {
2701 __perf_event_sync_stat(event
, next_event
);
2703 event
= list_next_entry(event
, event_entry
);
2704 next_event
= list_next_entry(next_event
, event_entry
);
2708 static void perf_event_context_sched_out(struct task_struct
*task
, int ctxn
,
2709 struct task_struct
*next
)
2711 struct perf_event_context
*ctx
= task
->perf_event_ctxp
[ctxn
];
2712 struct perf_event_context
*next_ctx
;
2713 struct perf_event_context
*parent
, *next_parent
;
2714 struct perf_cpu_context
*cpuctx
;
2720 cpuctx
= __get_cpu_context(ctx
);
2721 if (!cpuctx
->task_ctx
)
2725 next_ctx
= next
->perf_event_ctxp
[ctxn
];
2729 parent
= rcu_dereference(ctx
->parent_ctx
);
2730 next_parent
= rcu_dereference(next_ctx
->parent_ctx
);
2732 /* If neither context have a parent context; they cannot be clones. */
2733 if (!parent
&& !next_parent
)
2736 if (next_parent
== ctx
|| next_ctx
== parent
|| next_parent
== parent
) {
2738 * Looks like the two contexts are clones, so we might be
2739 * able to optimize the context switch. We lock both
2740 * contexts and check that they are clones under the
2741 * lock (including re-checking that neither has been
2742 * uncloned in the meantime). It doesn't matter which
2743 * order we take the locks because no other cpu could
2744 * be trying to lock both of these tasks.
2746 raw_spin_lock(&ctx
->lock
);
2747 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
2748 if (context_equiv(ctx
, next_ctx
)) {
2749 WRITE_ONCE(ctx
->task
, next
);
2750 WRITE_ONCE(next_ctx
->task
, task
);
2752 swap(ctx
->task_ctx_data
, next_ctx
->task_ctx_data
);
2755 * RCU_INIT_POINTER here is safe because we've not
2756 * modified the ctx and the above modification of
2757 * ctx->task and ctx->task_ctx_data are immaterial
2758 * since those values are always verified under
2759 * ctx->lock which we're now holding.
2761 RCU_INIT_POINTER(task
->perf_event_ctxp
[ctxn
], next_ctx
);
2762 RCU_INIT_POINTER(next
->perf_event_ctxp
[ctxn
], ctx
);
2766 perf_event_sync_stat(ctx
, next_ctx
);
2768 raw_spin_unlock(&next_ctx
->lock
);
2769 raw_spin_unlock(&ctx
->lock
);
2775 raw_spin_lock(&ctx
->lock
);
2776 task_ctx_sched_out(cpuctx
, ctx
);
2777 raw_spin_unlock(&ctx
->lock
);
2781 void perf_sched_cb_dec(struct pmu
*pmu
)
2783 this_cpu_dec(perf_sched_cb_usages
);
2786 void perf_sched_cb_inc(struct pmu
*pmu
)
2788 this_cpu_inc(perf_sched_cb_usages
);
2792 * This function provides the context switch callback to the lower code
2793 * layer. It is invoked ONLY when the context switch callback is enabled.
2795 static void perf_pmu_sched_task(struct task_struct
*prev
,
2796 struct task_struct
*next
,
2799 struct perf_cpu_context
*cpuctx
;
2801 unsigned long flags
;
2806 local_irq_save(flags
);
2810 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
2811 if (pmu
->sched_task
) {
2812 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
2814 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
2816 perf_pmu_disable(pmu
);
2818 pmu
->sched_task(cpuctx
->task_ctx
, sched_in
);
2820 perf_pmu_enable(pmu
);
2822 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
2828 local_irq_restore(flags
);
2831 static void perf_event_switch(struct task_struct
*task
,
2832 struct task_struct
*next_prev
, bool sched_in
);
2834 #define for_each_task_context_nr(ctxn) \
2835 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2838 * Called from scheduler to remove the events of the current task,
2839 * with interrupts disabled.
2841 * We stop each event and update the event value in event->count.
2843 * This does not protect us against NMI, but disable()
2844 * sets the disabled bit in the control field of event _before_
2845 * accessing the event control register. If a NMI hits, then it will
2846 * not restart the event.
2848 void __perf_event_task_sched_out(struct task_struct
*task
,
2849 struct task_struct
*next
)
2853 if (__this_cpu_read(perf_sched_cb_usages
))
2854 perf_pmu_sched_task(task
, next
, false);
2856 if (atomic_read(&nr_switch_events
))
2857 perf_event_switch(task
, next
, false);
2859 for_each_task_context_nr(ctxn
)
2860 perf_event_context_sched_out(task
, ctxn
, next
);
2863 * if cgroup events exist on this CPU, then we need
2864 * to check if we have to switch out PMU state.
2865 * cgroup event are system-wide mode only
2867 if (atomic_read(this_cpu_ptr(&perf_cgroup_events
)))
2868 perf_cgroup_sched_out(task
, next
);
2872 * Called with IRQs disabled
2874 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
2875 enum event_type_t event_type
)
2877 ctx_sched_out(&cpuctx
->ctx
, cpuctx
, event_type
);
2881 ctx_pinned_sched_in(struct perf_event_context
*ctx
,
2882 struct perf_cpu_context
*cpuctx
)
2884 struct perf_event
*event
;
2886 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
2887 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2889 if (!event_filter_match(event
))
2892 /* may need to reset tstamp_enabled */
2893 if (is_cgroup_event(event
))
2894 perf_cgroup_mark_enabled(event
, ctx
);
2896 if (group_can_go_on(event
, cpuctx
, 1))
2897 group_sched_in(event
, cpuctx
, ctx
);
2900 * If this pinned group hasn't been scheduled,
2901 * put it in error state.
2903 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
2904 update_group_times(event
);
2905 event
->state
= PERF_EVENT_STATE_ERROR
;
2911 ctx_flexible_sched_in(struct perf_event_context
*ctx
,
2912 struct perf_cpu_context
*cpuctx
)
2914 struct perf_event
*event
;
2917 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
2918 /* Ignore events in OFF or ERROR state */
2919 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2922 * Listen to the 'cpu' scheduling filter constraint
2925 if (!event_filter_match(event
))
2928 /* may need to reset tstamp_enabled */
2929 if (is_cgroup_event(event
))
2930 perf_cgroup_mark_enabled(event
, ctx
);
2932 if (group_can_go_on(event
, cpuctx
, can_add_hw
)) {
2933 if (group_sched_in(event
, cpuctx
, ctx
))
2940 ctx_sched_in(struct perf_event_context
*ctx
,
2941 struct perf_cpu_context
*cpuctx
,
2942 enum event_type_t event_type
,
2943 struct task_struct
*task
)
2945 int is_active
= ctx
->is_active
;
2948 lockdep_assert_held(&ctx
->lock
);
2950 if (likely(!ctx
->nr_events
))
2953 ctx
->is_active
|= (event_type
| EVENT_TIME
);
2956 cpuctx
->task_ctx
= ctx
;
2958 WARN_ON_ONCE(cpuctx
->task_ctx
!= ctx
);
2961 is_active
^= ctx
->is_active
; /* changed bits */
2963 if (is_active
& EVENT_TIME
) {
2964 /* start ctx time */
2966 ctx
->timestamp
= now
;
2967 perf_cgroup_set_timestamp(task
, ctx
);
2971 * First go through the list and put on any pinned groups
2972 * in order to give them the best chance of going on.
2974 if (is_active
& EVENT_PINNED
)
2975 ctx_pinned_sched_in(ctx
, cpuctx
);
2977 /* Then walk through the lower prio flexible groups */
2978 if (is_active
& EVENT_FLEXIBLE
)
2979 ctx_flexible_sched_in(ctx
, cpuctx
);
2982 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
2983 enum event_type_t event_type
,
2984 struct task_struct
*task
)
2986 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
2988 ctx_sched_in(ctx
, cpuctx
, event_type
, task
);
2991 static void perf_event_context_sched_in(struct perf_event_context
*ctx
,
2992 struct task_struct
*task
)
2994 struct perf_cpu_context
*cpuctx
;
2996 cpuctx
= __get_cpu_context(ctx
);
2997 if (cpuctx
->task_ctx
== ctx
)
3000 perf_ctx_lock(cpuctx
, ctx
);
3001 perf_pmu_disable(ctx
->pmu
);
3003 * We want to keep the following priority order:
3004 * cpu pinned (that don't need to move), task pinned,
3005 * cpu flexible, task flexible.
3007 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
3008 perf_event_sched_in(cpuctx
, ctx
, task
);
3009 perf_pmu_enable(ctx
->pmu
);
3010 perf_ctx_unlock(cpuctx
, ctx
);
3014 * Called from scheduler to add the events of the current task
3015 * with interrupts disabled.
3017 * We restore the event value and then enable it.
3019 * This does not protect us against NMI, but enable()
3020 * sets the enabled bit in the control field of event _before_
3021 * accessing the event control register. If a NMI hits, then it will
3022 * keep the event running.
3024 void __perf_event_task_sched_in(struct task_struct
*prev
,
3025 struct task_struct
*task
)
3027 struct perf_event_context
*ctx
;
3031 * If cgroup events exist on this CPU, then we need to check if we have
3032 * to switch in PMU state; cgroup event are system-wide mode only.
3034 * Since cgroup events are CPU events, we must schedule these in before
3035 * we schedule in the task events.
3037 if (atomic_read(this_cpu_ptr(&perf_cgroup_events
)))
3038 perf_cgroup_sched_in(prev
, task
);
3040 for_each_task_context_nr(ctxn
) {
3041 ctx
= task
->perf_event_ctxp
[ctxn
];
3045 perf_event_context_sched_in(ctx
, task
);
3048 if (atomic_read(&nr_switch_events
))
3049 perf_event_switch(task
, prev
, true);
3051 if (__this_cpu_read(perf_sched_cb_usages
))
3052 perf_pmu_sched_task(prev
, task
, true);
3055 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
3057 u64 frequency
= event
->attr
.sample_freq
;
3058 u64 sec
= NSEC_PER_SEC
;
3059 u64 divisor
, dividend
;
3061 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
3063 count_fls
= fls64(count
);
3064 nsec_fls
= fls64(nsec
);
3065 frequency_fls
= fls64(frequency
);
3069 * We got @count in @nsec, with a target of sample_freq HZ
3070 * the target period becomes:
3073 * period = -------------------
3074 * @nsec * sample_freq
3079 * Reduce accuracy by one bit such that @a and @b converge
3080 * to a similar magnitude.
3082 #define REDUCE_FLS(a, b) \
3084 if (a##_fls > b##_fls) { \
3094 * Reduce accuracy until either term fits in a u64, then proceed with
3095 * the other, so that finally we can do a u64/u64 division.
3097 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
3098 REDUCE_FLS(nsec
, frequency
);
3099 REDUCE_FLS(sec
, count
);
3102 if (count_fls
+ sec_fls
> 64) {
3103 divisor
= nsec
* frequency
;
3105 while (count_fls
+ sec_fls
> 64) {
3106 REDUCE_FLS(count
, sec
);
3110 dividend
= count
* sec
;
3112 dividend
= count
* sec
;
3114 while (nsec_fls
+ frequency_fls
> 64) {
3115 REDUCE_FLS(nsec
, frequency
);
3119 divisor
= nsec
* frequency
;
3125 return div64_u64(dividend
, divisor
);
3128 static DEFINE_PER_CPU(int, perf_throttled_count
);
3129 static DEFINE_PER_CPU(u64
, perf_throttled_seq
);
3131 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
, bool disable
)
3133 struct hw_perf_event
*hwc
= &event
->hw
;
3134 s64 period
, sample_period
;
3137 period
= perf_calculate_period(event
, nsec
, count
);
3139 delta
= (s64
)(period
- hwc
->sample_period
);
3140 delta
= (delta
+ 7) / 8; /* low pass filter */
3142 sample_period
= hwc
->sample_period
+ delta
;
3147 hwc
->sample_period
= sample_period
;
3149 if (local64_read(&hwc
->period_left
) > 8*sample_period
) {
3151 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
3153 local64_set(&hwc
->period_left
, 0);
3156 event
->pmu
->start(event
, PERF_EF_RELOAD
);
3161 * combine freq adjustment with unthrottling to avoid two passes over the
3162 * events. At the same time, make sure, having freq events does not change
3163 * the rate of unthrottling as that would introduce bias.
3165 static void perf_adjust_freq_unthr_context(struct perf_event_context
*ctx
,
3168 struct perf_event
*event
;
3169 struct hw_perf_event
*hwc
;
3170 u64 now
, period
= TICK_NSEC
;
3174 * only need to iterate over all events iff:
3175 * - context have events in frequency mode (needs freq adjust)
3176 * - there are events to unthrottle on this cpu
3178 if (!(ctx
->nr_freq
|| needs_unthr
))
3181 raw_spin_lock(&ctx
->lock
);
3182 perf_pmu_disable(ctx
->pmu
);
3184 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3185 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
3188 if (!event_filter_match(event
))
3191 perf_pmu_disable(event
->pmu
);
3195 if (hwc
->interrupts
== MAX_INTERRUPTS
) {
3196 hwc
->interrupts
= 0;
3197 perf_log_throttle(event
, 1);
3198 event
->pmu
->start(event
, 0);
3201 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
3205 * stop the event and update event->count
3207 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
3209 now
= local64_read(&event
->count
);
3210 delta
= now
- hwc
->freq_count_stamp
;
3211 hwc
->freq_count_stamp
= now
;
3215 * reload only if value has changed
3216 * we have stopped the event so tell that
3217 * to perf_adjust_period() to avoid stopping it
3221 perf_adjust_period(event
, period
, delta
, false);
3223 event
->pmu
->start(event
, delta
> 0 ? PERF_EF_RELOAD
: 0);
3225 perf_pmu_enable(event
->pmu
);
3228 perf_pmu_enable(ctx
->pmu
);
3229 raw_spin_unlock(&ctx
->lock
);
3233 * Round-robin a context's events:
3235 static void rotate_ctx(struct perf_event_context
*ctx
)
3238 * Rotate the first entry last of non-pinned groups. Rotation might be
3239 * disabled by the inheritance code.
3241 if (!ctx
->rotate_disable
)
3242 list_rotate_left(&ctx
->flexible_groups
);
3245 static int perf_rotate_context(struct perf_cpu_context
*cpuctx
)
3247 struct perf_event_context
*ctx
= NULL
;
3250 if (cpuctx
->ctx
.nr_events
) {
3251 if (cpuctx
->ctx
.nr_events
!= cpuctx
->ctx
.nr_active
)
3255 ctx
= cpuctx
->task_ctx
;
3256 if (ctx
&& ctx
->nr_events
) {
3257 if (ctx
->nr_events
!= ctx
->nr_active
)
3264 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
3265 perf_pmu_disable(cpuctx
->ctx
.pmu
);
3267 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
3269 ctx_sched_out(ctx
, cpuctx
, EVENT_FLEXIBLE
);
3271 rotate_ctx(&cpuctx
->ctx
);
3275 perf_event_sched_in(cpuctx
, ctx
, current
);
3277 perf_pmu_enable(cpuctx
->ctx
.pmu
);
3278 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
3284 void perf_event_task_tick(void)
3286 struct list_head
*head
= this_cpu_ptr(&active_ctx_list
);
3287 struct perf_event_context
*ctx
, *tmp
;
3290 WARN_ON(!irqs_disabled());
3292 __this_cpu_inc(perf_throttled_seq
);
3293 throttled
= __this_cpu_xchg(perf_throttled_count
, 0);
3294 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS
);
3296 list_for_each_entry_safe(ctx
, tmp
, head
, active_ctx_list
)
3297 perf_adjust_freq_unthr_context(ctx
, throttled
);
3300 static int event_enable_on_exec(struct perf_event
*event
,
3301 struct perf_event_context
*ctx
)
3303 if (!event
->attr
.enable_on_exec
)
3306 event
->attr
.enable_on_exec
= 0;
3307 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
3310 __perf_event_mark_enabled(event
);
3316 * Enable all of a task's events that have been marked enable-on-exec.
3317 * This expects task == current.
3319 static void perf_event_enable_on_exec(int ctxn
)
3321 struct perf_event_context
*ctx
, *clone_ctx
= NULL
;
3322 struct perf_cpu_context
*cpuctx
;
3323 struct perf_event
*event
;
3324 unsigned long flags
;
3327 local_irq_save(flags
);
3328 ctx
= current
->perf_event_ctxp
[ctxn
];
3329 if (!ctx
|| !ctx
->nr_events
)
3332 cpuctx
= __get_cpu_context(ctx
);
3333 perf_ctx_lock(cpuctx
, ctx
);
3334 ctx_sched_out(ctx
, cpuctx
, EVENT_TIME
);
3335 list_for_each_entry(event
, &ctx
->event_list
, event_entry
)
3336 enabled
|= event_enable_on_exec(event
, ctx
);
3339 * Unclone and reschedule this context if we enabled any event.
3342 clone_ctx
= unclone_ctx(ctx
);
3343 ctx_resched(cpuctx
, ctx
);
3345 perf_ctx_unlock(cpuctx
, ctx
);
3348 local_irq_restore(flags
);
3354 struct perf_read_data
{
3355 struct perf_event
*event
;
3361 * Cross CPU call to read the hardware event
3363 static void __perf_event_read(void *info
)
3365 struct perf_read_data
*data
= info
;
3366 struct perf_event
*sub
, *event
= data
->event
;
3367 struct perf_event_context
*ctx
= event
->ctx
;
3368 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
3369 struct pmu
*pmu
= event
->pmu
;
3372 * If this is a task context, we need to check whether it is
3373 * the current task context of this cpu. If not it has been
3374 * scheduled out before the smp call arrived. In that case
3375 * event->count would have been updated to a recent sample
3376 * when the event was scheduled out.
3378 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
3381 raw_spin_lock(&ctx
->lock
);
3382 if (ctx
->is_active
) {
3383 update_context_time(ctx
);
3384 update_cgrp_time_from_event(event
);
3387 update_event_times(event
);
3388 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
3397 pmu
->start_txn(pmu
, PERF_PMU_TXN_READ
);
3401 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
3402 update_event_times(sub
);
3403 if (sub
->state
== PERF_EVENT_STATE_ACTIVE
) {
3405 * Use sibling's PMU rather than @event's since
3406 * sibling could be on different (eg: software) PMU.
3408 sub
->pmu
->read(sub
);
3412 data
->ret
= pmu
->commit_txn(pmu
);
3415 raw_spin_unlock(&ctx
->lock
);
3418 static inline u64
perf_event_count(struct perf_event
*event
)
3420 if (event
->pmu
->count
)
3421 return event
->pmu
->count(event
);
3423 return __perf_event_count(event
);
3427 * NMI-safe method to read a local event, that is an event that
3429 * - either for the current task, or for this CPU
3430 * - does not have inherit set, for inherited task events
3431 * will not be local and we cannot read them atomically
3432 * - must not have a pmu::count method
3434 u64
perf_event_read_local(struct perf_event
*event
)
3436 unsigned long flags
;
3440 * Disabling interrupts avoids all counter scheduling (context
3441 * switches, timer based rotation and IPIs).
3443 local_irq_save(flags
);
3445 /* If this is a per-task event, it must be for current */
3446 WARN_ON_ONCE((event
->attach_state
& PERF_ATTACH_TASK
) &&
3447 event
->hw
.target
!= current
);
3449 /* If this is a per-CPU event, it must be for this CPU */
3450 WARN_ON_ONCE(!(event
->attach_state
& PERF_ATTACH_TASK
) &&
3451 event
->cpu
!= smp_processor_id());
3454 * It must not be an event with inherit set, we cannot read
3455 * all child counters from atomic context.
3457 WARN_ON_ONCE(event
->attr
.inherit
);
3460 * It must not have a pmu::count method, those are not
3463 WARN_ON_ONCE(event
->pmu
->count
);
3466 * If the event is currently on this CPU, its either a per-task event,
3467 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3470 if (event
->oncpu
== smp_processor_id())
3471 event
->pmu
->read(event
);
3473 val
= local64_read(&event
->count
);
3474 local_irq_restore(flags
);
3479 static int perf_event_read(struct perf_event
*event
, bool group
)
3484 * If event is enabled and currently active on a CPU, update the
3485 * value in the event structure:
3487 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
3488 struct perf_read_data data
= {
3493 smp_call_function_single(event
->oncpu
,
3494 __perf_event_read
, &data
, 1);
3496 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
3497 struct perf_event_context
*ctx
= event
->ctx
;
3498 unsigned long flags
;
3500 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
3502 * may read while context is not active
3503 * (e.g., thread is blocked), in that case
3504 * we cannot update context time
3506 if (ctx
->is_active
) {
3507 update_context_time(ctx
);
3508 update_cgrp_time_from_event(event
);
3511 update_group_times(event
);
3513 update_event_times(event
);
3514 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
3521 * Initialize the perf_event context in a task_struct:
3523 static void __perf_event_init_context(struct perf_event_context
*ctx
)
3525 raw_spin_lock_init(&ctx
->lock
);
3526 mutex_init(&ctx
->mutex
);
3527 INIT_LIST_HEAD(&ctx
->active_ctx_list
);
3528 INIT_LIST_HEAD(&ctx
->pinned_groups
);
3529 INIT_LIST_HEAD(&ctx
->flexible_groups
);
3530 INIT_LIST_HEAD(&ctx
->event_list
);
3531 atomic_set(&ctx
->refcount
, 1);
3534 static struct perf_event_context
*
3535 alloc_perf_context(struct pmu
*pmu
, struct task_struct
*task
)
3537 struct perf_event_context
*ctx
;
3539 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
3543 __perf_event_init_context(ctx
);
3546 get_task_struct(task
);
3553 static struct task_struct
*
3554 find_lively_task_by_vpid(pid_t vpid
)
3556 struct task_struct
*task
;
3562 task
= find_task_by_vpid(vpid
);
3564 get_task_struct(task
);
3568 return ERR_PTR(-ESRCH
);
3574 * Returns a matching context with refcount and pincount.
3576 static struct perf_event_context
*
3577 find_get_context(struct pmu
*pmu
, struct task_struct
*task
,
3578 struct perf_event
*event
)
3580 struct perf_event_context
*ctx
, *clone_ctx
= NULL
;
3581 struct perf_cpu_context
*cpuctx
;
3582 void *task_ctx_data
= NULL
;
3583 unsigned long flags
;
3585 int cpu
= event
->cpu
;
3588 /* Must be root to operate on a CPU event: */
3589 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
3590 return ERR_PTR(-EACCES
);
3593 * We could be clever and allow to attach a event to an
3594 * offline CPU and activate it when the CPU comes up, but
3597 if (!cpu_online(cpu
))
3598 return ERR_PTR(-ENODEV
);
3600 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
3609 ctxn
= pmu
->task_ctx_nr
;
3613 if (event
->attach_state
& PERF_ATTACH_TASK_DATA
) {
3614 task_ctx_data
= kzalloc(pmu
->task_ctx_size
, GFP_KERNEL
);
3615 if (!task_ctx_data
) {
3622 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
3624 clone_ctx
= unclone_ctx(ctx
);
3627 if (task_ctx_data
&& !ctx
->task_ctx_data
) {
3628 ctx
->task_ctx_data
= task_ctx_data
;
3629 task_ctx_data
= NULL
;
3631 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
3636 ctx
= alloc_perf_context(pmu
, task
);
3641 if (task_ctx_data
) {
3642 ctx
->task_ctx_data
= task_ctx_data
;
3643 task_ctx_data
= NULL
;
3647 mutex_lock(&task
->perf_event_mutex
);
3649 * If it has already passed perf_event_exit_task().
3650 * we must see PF_EXITING, it takes this mutex too.
3652 if (task
->flags
& PF_EXITING
)
3654 else if (task
->perf_event_ctxp
[ctxn
])
3659 rcu_assign_pointer(task
->perf_event_ctxp
[ctxn
], ctx
);
3661 mutex_unlock(&task
->perf_event_mutex
);
3663 if (unlikely(err
)) {
3672 kfree(task_ctx_data
);
3676 kfree(task_ctx_data
);
3677 return ERR_PTR(err
);
3680 static void perf_event_free_filter(struct perf_event
*event
);
3681 static void perf_event_free_bpf_prog(struct perf_event
*event
);
3683 static void free_event_rcu(struct rcu_head
*head
)
3685 struct perf_event
*event
;
3687 event
= container_of(head
, struct perf_event
, rcu_head
);
3689 put_pid_ns(event
->ns
);
3690 perf_event_free_filter(event
);
3694 static void ring_buffer_attach(struct perf_event
*event
,
3695 struct ring_buffer
*rb
);
3697 static void detach_sb_event(struct perf_event
*event
)
3699 struct pmu_event_list
*pel
= per_cpu_ptr(&pmu_sb_events
, event
->cpu
);
3701 raw_spin_lock(&pel
->lock
);
3702 list_del_rcu(&event
->sb_list
);
3703 raw_spin_unlock(&pel
->lock
);
3706 static bool is_sb_event(struct perf_event
*event
)
3708 struct perf_event_attr
*attr
= &event
->attr
;
3713 if (event
->attach_state
& PERF_ATTACH_TASK
)
3716 if (attr
->mmap
|| attr
->mmap_data
|| attr
->mmap2
||
3717 attr
->comm
|| attr
->comm_exec
||
3719 attr
->context_switch
)
3724 static void unaccount_pmu_sb_event(struct perf_event
*event
)
3726 if (is_sb_event(event
))
3727 detach_sb_event(event
);
3730 static void unaccount_event_cpu(struct perf_event
*event
, int cpu
)
3735 if (is_cgroup_event(event
))
3736 atomic_dec(&per_cpu(perf_cgroup_events
, cpu
));
3739 #ifdef CONFIG_NO_HZ_FULL
3740 static DEFINE_SPINLOCK(nr_freq_lock
);
3743 static void unaccount_freq_event_nohz(void)
3745 #ifdef CONFIG_NO_HZ_FULL
3746 spin_lock(&nr_freq_lock
);
3747 if (atomic_dec_and_test(&nr_freq_events
))
3748 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS
);
3749 spin_unlock(&nr_freq_lock
);
3753 static void unaccount_freq_event(void)
3755 if (tick_nohz_full_enabled())
3756 unaccount_freq_event_nohz();
3758 atomic_dec(&nr_freq_events
);
3761 static void unaccount_event(struct perf_event
*event
)
3768 if (event
->attach_state
& PERF_ATTACH_TASK
)
3770 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
3771 atomic_dec(&nr_mmap_events
);
3772 if (event
->attr
.comm
)
3773 atomic_dec(&nr_comm_events
);
3774 if (event
->attr
.task
)
3775 atomic_dec(&nr_task_events
);
3776 if (event
->attr
.freq
)
3777 unaccount_freq_event();
3778 if (event
->attr
.context_switch
) {
3780 atomic_dec(&nr_switch_events
);
3782 if (is_cgroup_event(event
))
3784 if (has_branch_stack(event
))
3788 if (!atomic_add_unless(&perf_sched_count
, -1, 1))
3789 schedule_delayed_work(&perf_sched_work
, HZ
);
3792 unaccount_event_cpu(event
, event
->cpu
);
3794 unaccount_pmu_sb_event(event
);
3797 static void perf_sched_delayed(struct work_struct
*work
)
3799 mutex_lock(&perf_sched_mutex
);
3800 if (atomic_dec_and_test(&perf_sched_count
))
3801 static_branch_disable(&perf_sched_events
);
3802 mutex_unlock(&perf_sched_mutex
);
3806 * The following implement mutual exclusion of events on "exclusive" pmus
3807 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3808 * at a time, so we disallow creating events that might conflict, namely:
3810 * 1) cpu-wide events in the presence of per-task events,
3811 * 2) per-task events in the presence of cpu-wide events,
3812 * 3) two matching events on the same context.
3814 * The former two cases are handled in the allocation path (perf_event_alloc(),
3815 * _free_event()), the latter -- before the first perf_install_in_context().
3817 static int exclusive_event_init(struct perf_event
*event
)
3819 struct pmu
*pmu
= event
->pmu
;
3821 if (!(pmu
->capabilities
& PERF_PMU_CAP_EXCLUSIVE
))
3825 * Prevent co-existence of per-task and cpu-wide events on the
3826 * same exclusive pmu.
3828 * Negative pmu::exclusive_cnt means there are cpu-wide
3829 * events on this "exclusive" pmu, positive means there are
3832 * Since this is called in perf_event_alloc() path, event::ctx
3833 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
3834 * to mean "per-task event", because unlike other attach states it
3835 * never gets cleared.
3837 if (event
->attach_state
& PERF_ATTACH_TASK
) {
3838 if (!atomic_inc_unless_negative(&pmu
->exclusive_cnt
))
3841 if (!atomic_dec_unless_positive(&pmu
->exclusive_cnt
))
3848 static void exclusive_event_destroy(struct perf_event
*event
)
3850 struct pmu
*pmu
= event
->pmu
;
3852 if (!(pmu
->capabilities
& PERF_PMU_CAP_EXCLUSIVE
))
3855 /* see comment in exclusive_event_init() */
3856 if (event
->attach_state
& PERF_ATTACH_TASK
)
3857 atomic_dec(&pmu
->exclusive_cnt
);
3859 atomic_inc(&pmu
->exclusive_cnt
);
3862 static bool exclusive_event_match(struct perf_event
*e1
, struct perf_event
*e2
)
3864 if ((e1
->pmu
->capabilities
& PERF_PMU_CAP_EXCLUSIVE
) &&
3865 (e1
->cpu
== e2
->cpu
||
3872 /* Called under the same ctx::mutex as perf_install_in_context() */
3873 static bool exclusive_event_installable(struct perf_event
*event
,
3874 struct perf_event_context
*ctx
)
3876 struct perf_event
*iter_event
;
3877 struct pmu
*pmu
= event
->pmu
;
3879 if (!(pmu
->capabilities
& PERF_PMU_CAP_EXCLUSIVE
))
3882 list_for_each_entry(iter_event
, &ctx
->event_list
, event_entry
) {
3883 if (exclusive_event_match(iter_event
, event
))
3890 static void perf_addr_filters_splice(struct perf_event
*event
,
3891 struct list_head
*head
);
3893 static void _free_event(struct perf_event
*event
)
3895 irq_work_sync(&event
->pending
);
3897 unaccount_event(event
);
3901 * Can happen when we close an event with re-directed output.
3903 * Since we have a 0 refcount, perf_mmap_close() will skip
3904 * over us; possibly making our ring_buffer_put() the last.
3906 mutex_lock(&event
->mmap_mutex
);
3907 ring_buffer_attach(event
, NULL
);
3908 mutex_unlock(&event
->mmap_mutex
);
3911 if (is_cgroup_event(event
))
3912 perf_detach_cgroup(event
);
3914 if (!event
->parent
) {
3915 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
)
3916 put_callchain_buffers();
3919 perf_event_free_bpf_prog(event
);
3920 perf_addr_filters_splice(event
, NULL
);
3921 kfree(event
->addr_filters_offs
);
3924 event
->destroy(event
);
3927 put_ctx(event
->ctx
);
3929 exclusive_event_destroy(event
);
3930 module_put(event
->pmu
->module
);
3932 call_rcu(&event
->rcu_head
, free_event_rcu
);
3936 * Used to free events which have a known refcount of 1, such as in error paths
3937 * where the event isn't exposed yet and inherited events.
3939 static void free_event(struct perf_event
*event
)
3941 if (WARN(atomic_long_cmpxchg(&event
->refcount
, 1, 0) != 1,
3942 "unexpected event refcount: %ld; ptr=%p\n",
3943 atomic_long_read(&event
->refcount
), event
)) {
3944 /* leak to avoid use-after-free */
3952 * Remove user event from the owner task.
3954 static void perf_remove_from_owner(struct perf_event
*event
)
3956 struct task_struct
*owner
;
3960 * Matches the smp_store_release() in perf_event_exit_task(). If we
3961 * observe !owner it means the list deletion is complete and we can
3962 * indeed free this event, otherwise we need to serialize on
3963 * owner->perf_event_mutex.
3965 owner
= lockless_dereference(event
->owner
);
3968 * Since delayed_put_task_struct() also drops the last
3969 * task reference we can safely take a new reference
3970 * while holding the rcu_read_lock().
3972 get_task_struct(owner
);
3978 * If we're here through perf_event_exit_task() we're already
3979 * holding ctx->mutex which would be an inversion wrt. the
3980 * normal lock order.
3982 * However we can safely take this lock because its the child
3985 mutex_lock_nested(&owner
->perf_event_mutex
, SINGLE_DEPTH_NESTING
);
3988 * We have to re-check the event->owner field, if it is cleared
3989 * we raced with perf_event_exit_task(), acquiring the mutex
3990 * ensured they're done, and we can proceed with freeing the
3994 list_del_init(&event
->owner_entry
);
3995 smp_store_release(&event
->owner
, NULL
);
3997 mutex_unlock(&owner
->perf_event_mutex
);
3998 put_task_struct(owner
);
4002 static void put_event(struct perf_event
*event
)
4004 if (!atomic_long_dec_and_test(&event
->refcount
))
4011 * Kill an event dead; while event:refcount will preserve the event
4012 * object, it will not preserve its functionality. Once the last 'user'
4013 * gives up the object, we'll destroy the thing.
4015 int perf_event_release_kernel(struct perf_event
*event
)
4017 struct perf_event_context
*ctx
= event
->ctx
;
4018 struct perf_event
*child
, *tmp
;
4021 * If we got here through err_file: fput(event_file); we will not have
4022 * attached to a context yet.
4025 WARN_ON_ONCE(event
->attach_state
&
4026 (PERF_ATTACH_CONTEXT
|PERF_ATTACH_GROUP
));
4030 if (!is_kernel_event(event
))
4031 perf_remove_from_owner(event
);
4033 ctx
= perf_event_ctx_lock(event
);
4034 WARN_ON_ONCE(ctx
->parent_ctx
);
4035 perf_remove_from_context(event
, DETACH_GROUP
);
4037 raw_spin_lock_irq(&ctx
->lock
);
4039 * Mark this even as STATE_DEAD, there is no external reference to it
4042 * Anybody acquiring event->child_mutex after the below loop _must_
4043 * also see this, most importantly inherit_event() which will avoid
4044 * placing more children on the list.
4046 * Thus this guarantees that we will in fact observe and kill _ALL_
4049 event
->state
= PERF_EVENT_STATE_DEAD
;
4050 raw_spin_unlock_irq(&ctx
->lock
);
4052 perf_event_ctx_unlock(event
, ctx
);
4055 mutex_lock(&event
->child_mutex
);
4056 list_for_each_entry(child
, &event
->child_list
, child_list
) {
4059 * Cannot change, child events are not migrated, see the
4060 * comment with perf_event_ctx_lock_nested().
4062 ctx
= lockless_dereference(child
->ctx
);
4064 * Since child_mutex nests inside ctx::mutex, we must jump
4065 * through hoops. We start by grabbing a reference on the ctx.
4067 * Since the event cannot get freed while we hold the
4068 * child_mutex, the context must also exist and have a !0
4074 * Now that we have a ctx ref, we can drop child_mutex, and
4075 * acquire ctx::mutex without fear of it going away. Then we
4076 * can re-acquire child_mutex.
4078 mutex_unlock(&event
->child_mutex
);
4079 mutex_lock(&ctx
->mutex
);
4080 mutex_lock(&event
->child_mutex
);
4083 * Now that we hold ctx::mutex and child_mutex, revalidate our
4084 * state, if child is still the first entry, it didn't get freed
4085 * and we can continue doing so.
4087 tmp
= list_first_entry_or_null(&event
->child_list
,
4088 struct perf_event
, child_list
);
4090 perf_remove_from_context(child
, DETACH_GROUP
);
4091 list_del(&child
->child_list
);
4094 * This matches the refcount bump in inherit_event();
4095 * this can't be the last reference.
4100 mutex_unlock(&event
->child_mutex
);
4101 mutex_unlock(&ctx
->mutex
);
4105 mutex_unlock(&event
->child_mutex
);
4108 put_event(event
); /* Must be the 'last' reference */
4111 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
4114 * Called when the last reference to the file is gone.
4116 static int perf_release(struct inode
*inode
, struct file
*file
)
4118 perf_event_release_kernel(file
->private_data
);
4122 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
4124 struct perf_event
*child
;
4130 mutex_lock(&event
->child_mutex
);
4132 (void)perf_event_read(event
, false);
4133 total
+= perf_event_count(event
);
4135 *enabled
+= event
->total_time_enabled
+
4136 atomic64_read(&event
->child_total_time_enabled
);
4137 *running
+= event
->total_time_running
+
4138 atomic64_read(&event
->child_total_time_running
);
4140 list_for_each_entry(child
, &event
->child_list
, child_list
) {
4141 (void)perf_event_read(child
, false);
4142 total
+= perf_event_count(child
);
4143 *enabled
+= child
->total_time_enabled
;
4144 *running
+= child
->total_time_running
;
4146 mutex_unlock(&event
->child_mutex
);
4150 EXPORT_SYMBOL_GPL(perf_event_read_value
);
4152 static int __perf_read_group_add(struct perf_event
*leader
,
4153 u64 read_format
, u64
*values
)
4155 struct perf_event
*sub
;
4156 int n
= 1; /* skip @nr */
4159 ret
= perf_event_read(leader
, true);
4164 * Since we co-schedule groups, {enabled,running} times of siblings
4165 * will be identical to those of the leader, so we only publish one
4168 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
4169 values
[n
++] += leader
->total_time_enabled
+
4170 atomic64_read(&leader
->child_total_time_enabled
);
4173 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
4174 values
[n
++] += leader
->total_time_running
+
4175 atomic64_read(&leader
->child_total_time_running
);
4179 * Write {count,id} tuples for every sibling.
4181 values
[n
++] += perf_event_count(leader
);
4182 if (read_format
& PERF_FORMAT_ID
)
4183 values
[n
++] = primary_event_id(leader
);
4185 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
4186 values
[n
++] += perf_event_count(sub
);
4187 if (read_format
& PERF_FORMAT_ID
)
4188 values
[n
++] = primary_event_id(sub
);
4194 static int perf_read_group(struct perf_event
*event
,
4195 u64 read_format
, char __user
*buf
)
4197 struct perf_event
*leader
= event
->group_leader
, *child
;
4198 struct perf_event_context
*ctx
= leader
->ctx
;
4202 lockdep_assert_held(&ctx
->mutex
);
4204 values
= kzalloc(event
->read_size
, GFP_KERNEL
);
4208 values
[0] = 1 + leader
->nr_siblings
;
4211 * By locking the child_mutex of the leader we effectively
4212 * lock the child list of all siblings.. XXX explain how.
4214 mutex_lock(&leader
->child_mutex
);
4216 ret
= __perf_read_group_add(leader
, read_format
, values
);
4220 list_for_each_entry(child
, &leader
->child_list
, child_list
) {
4221 ret
= __perf_read_group_add(child
, read_format
, values
);
4226 mutex_unlock(&leader
->child_mutex
);
4228 ret
= event
->read_size
;
4229 if (copy_to_user(buf
, values
, event
->read_size
))
4234 mutex_unlock(&leader
->child_mutex
);
4240 static int perf_read_one(struct perf_event
*event
,
4241 u64 read_format
, char __user
*buf
)
4243 u64 enabled
, running
;
4247 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
4248 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
4249 values
[n
++] = enabled
;
4250 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
4251 values
[n
++] = running
;
4252 if (read_format
& PERF_FORMAT_ID
)
4253 values
[n
++] = primary_event_id(event
);
4255 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
4258 return n
* sizeof(u64
);
4261 static bool is_event_hup(struct perf_event
*event
)
4265 if (event
->state
> PERF_EVENT_STATE_EXIT
)
4268 mutex_lock(&event
->child_mutex
);
4269 no_children
= list_empty(&event
->child_list
);
4270 mutex_unlock(&event
->child_mutex
);
4275 * Read the performance event - simple non blocking version for now
4278 __perf_read(struct perf_event
*event
, char __user
*buf
, size_t count
)
4280 u64 read_format
= event
->attr
.read_format
;
4284 * Return end-of-file for a read on a event that is in
4285 * error state (i.e. because it was pinned but it couldn't be
4286 * scheduled on to the CPU at some point).
4288 if (event
->state
== PERF_EVENT_STATE_ERROR
)
4291 if (count
< event
->read_size
)
4294 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
4295 if (read_format
& PERF_FORMAT_GROUP
)
4296 ret
= perf_read_group(event
, read_format
, buf
);
4298 ret
= perf_read_one(event
, read_format
, buf
);
4304 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
4306 struct perf_event
*event
= file
->private_data
;
4307 struct perf_event_context
*ctx
;
4310 ctx
= perf_event_ctx_lock(event
);
4311 ret
= __perf_read(event
, buf
, count
);
4312 perf_event_ctx_unlock(event
, ctx
);
4317 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
4319 struct perf_event
*event
= file
->private_data
;
4320 struct ring_buffer
*rb
;
4321 unsigned int events
= POLLHUP
;
4323 poll_wait(file
, &event
->waitq
, wait
);
4325 if (is_event_hup(event
))
4329 * Pin the event->rb by taking event->mmap_mutex; otherwise
4330 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
4332 mutex_lock(&event
->mmap_mutex
);
4335 events
= atomic_xchg(&rb
->poll
, 0);
4336 mutex_unlock(&event
->mmap_mutex
);
4340 static void _perf_event_reset(struct perf_event
*event
)
4342 (void)perf_event_read(event
, false);
4343 local64_set(&event
->count
, 0);
4344 perf_event_update_userpage(event
);
4348 * Holding the top-level event's child_mutex means that any
4349 * descendant process that has inherited this event will block
4350 * in perf_event_exit_event() if it goes to exit, thus satisfying the
4351 * task existence requirements of perf_event_enable/disable.
4353 static void perf_event_for_each_child(struct perf_event
*event
,
4354 void (*func
)(struct perf_event
*))
4356 struct perf_event
*child
;
4358 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
4360 mutex_lock(&event
->child_mutex
);
4362 list_for_each_entry(child
, &event
->child_list
, child_list
)
4364 mutex_unlock(&event
->child_mutex
);
4367 static void perf_event_for_each(struct perf_event
*event
,
4368 void (*func
)(struct perf_event
*))
4370 struct perf_event_context
*ctx
= event
->ctx
;
4371 struct perf_event
*sibling
;
4373 lockdep_assert_held(&ctx
->mutex
);
4375 event
= event
->group_leader
;
4377 perf_event_for_each_child(event
, func
);
4378 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
4379 perf_event_for_each_child(sibling
, func
);
4382 static void __perf_event_period(struct perf_event
*event
,
4383 struct perf_cpu_context
*cpuctx
,
4384 struct perf_event_context
*ctx
,
4387 u64 value
= *((u64
*)info
);
4390 if (event
->attr
.freq
) {
4391 event
->attr
.sample_freq
= value
;
4393 event
->attr
.sample_period
= value
;
4394 event
->hw
.sample_period
= value
;
4397 active
= (event
->state
== PERF_EVENT_STATE_ACTIVE
);
4399 perf_pmu_disable(ctx
->pmu
);
4401 * We could be throttled; unthrottle now to avoid the tick
4402 * trying to unthrottle while we already re-started the event.
4404 if (event
->hw
.interrupts
== MAX_INTERRUPTS
) {
4405 event
->hw
.interrupts
= 0;
4406 perf_log_throttle(event
, 1);
4408 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
4411 local64_set(&event
->hw
.period_left
, 0);
4414 event
->pmu
->start(event
, PERF_EF_RELOAD
);
4415 perf_pmu_enable(ctx
->pmu
);
4419 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
4423 if (!is_sampling_event(event
))
4426 if (copy_from_user(&value
, arg
, sizeof(value
)))
4432 if (event
->attr
.freq
&& value
> sysctl_perf_event_sample_rate
)
4435 event_function_call(event
, __perf_event_period
, &value
);
4440 static const struct file_operations perf_fops
;
4442 static inline int perf_fget_light(int fd
, struct fd
*p
)
4444 struct fd f
= fdget(fd
);
4448 if (f
.file
->f_op
!= &perf_fops
) {
4456 static int perf_event_set_output(struct perf_event
*event
,
4457 struct perf_event
*output_event
);
4458 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
4459 static int perf_event_set_bpf_prog(struct perf_event
*event
, u32 prog_fd
);
4461 static long _perf_ioctl(struct perf_event
*event
, unsigned int cmd
, unsigned long arg
)
4463 void (*func
)(struct perf_event
*);
4467 case PERF_EVENT_IOC_ENABLE
:
4468 func
= _perf_event_enable
;
4470 case PERF_EVENT_IOC_DISABLE
:
4471 func
= _perf_event_disable
;
4473 case PERF_EVENT_IOC_RESET
:
4474 func
= _perf_event_reset
;
4477 case PERF_EVENT_IOC_REFRESH
:
4478 return _perf_event_refresh(event
, arg
);
4480 case PERF_EVENT_IOC_PERIOD
:
4481 return perf_event_period(event
, (u64 __user
*)arg
);
4483 case PERF_EVENT_IOC_ID
:
4485 u64 id
= primary_event_id(event
);
4487 if (copy_to_user((void __user
*)arg
, &id
, sizeof(id
)))
4492 case PERF_EVENT_IOC_SET_OUTPUT
:
4496 struct perf_event
*output_event
;
4498 ret
= perf_fget_light(arg
, &output
);
4501 output_event
= output
.file
->private_data
;
4502 ret
= perf_event_set_output(event
, output_event
);
4505 ret
= perf_event_set_output(event
, NULL
);
4510 case PERF_EVENT_IOC_SET_FILTER
:
4511 return perf_event_set_filter(event
, (void __user
*)arg
);
4513 case PERF_EVENT_IOC_SET_BPF
:
4514 return perf_event_set_bpf_prog(event
, arg
);
4516 case PERF_EVENT_IOC_PAUSE_OUTPUT
: {
4517 struct ring_buffer
*rb
;
4520 rb
= rcu_dereference(event
->rb
);
4521 if (!rb
|| !rb
->nr_pages
) {
4525 rb_toggle_paused(rb
, !!arg
);
4533 if (flags
& PERF_IOC_FLAG_GROUP
)
4534 perf_event_for_each(event
, func
);
4536 perf_event_for_each_child(event
, func
);
4541 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
4543 struct perf_event
*event
= file
->private_data
;
4544 struct perf_event_context
*ctx
;
4547 ctx
= perf_event_ctx_lock(event
);
4548 ret
= _perf_ioctl(event
, cmd
, arg
);
4549 perf_event_ctx_unlock(event
, ctx
);
4554 #ifdef CONFIG_COMPAT
4555 static long perf_compat_ioctl(struct file
*file
, unsigned int cmd
,
4558 switch (_IOC_NR(cmd
)) {
4559 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER
):
4560 case _IOC_NR(PERF_EVENT_IOC_ID
):
4561 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4562 if (_IOC_SIZE(cmd
) == sizeof(compat_uptr_t
)) {
4563 cmd
&= ~IOCSIZE_MASK
;
4564 cmd
|= sizeof(void *) << IOCSIZE_SHIFT
;
4568 return perf_ioctl(file
, cmd
, arg
);
4571 # define perf_compat_ioctl NULL
4574 int perf_event_task_enable(void)
4576 struct perf_event_context
*ctx
;
4577 struct perf_event
*event
;
4579 mutex_lock(¤t
->perf_event_mutex
);
4580 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
) {
4581 ctx
= perf_event_ctx_lock(event
);
4582 perf_event_for_each_child(event
, _perf_event_enable
);
4583 perf_event_ctx_unlock(event
, ctx
);
4585 mutex_unlock(¤t
->perf_event_mutex
);
4590 int perf_event_task_disable(void)
4592 struct perf_event_context
*ctx
;
4593 struct perf_event
*event
;
4595 mutex_lock(¤t
->perf_event_mutex
);
4596 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
) {
4597 ctx
= perf_event_ctx_lock(event
);
4598 perf_event_for_each_child(event
, _perf_event_disable
);
4599 perf_event_ctx_unlock(event
, ctx
);
4601 mutex_unlock(¤t
->perf_event_mutex
);
4606 static int perf_event_index(struct perf_event
*event
)
4608 if (event
->hw
.state
& PERF_HES_STOPPED
)
4611 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
4614 return event
->pmu
->event_idx(event
);
4617 static void calc_timer_values(struct perf_event
*event
,
4624 *now
= perf_clock();
4625 ctx_time
= event
->shadow_ctx_time
+ *now
;
4626 *enabled
= ctx_time
- event
->tstamp_enabled
;
4627 *running
= ctx_time
- event
->tstamp_running
;
4630 static void perf_event_init_userpage(struct perf_event
*event
)
4632 struct perf_event_mmap_page
*userpg
;
4633 struct ring_buffer
*rb
;
4636 rb
= rcu_dereference(event
->rb
);
4640 userpg
= rb
->user_page
;
4642 /* Allow new userspace to detect that bit 0 is deprecated */
4643 userpg
->cap_bit0_is_deprecated
= 1;
4644 userpg
->size
= offsetof(struct perf_event_mmap_page
, __reserved
);
4645 userpg
->data_offset
= PAGE_SIZE
;
4646 userpg
->data_size
= perf_data_size(rb
);
4652 void __weak
arch_perf_update_userpage(
4653 struct perf_event
*event
, struct perf_event_mmap_page
*userpg
, u64 now
)
4658 * Callers need to ensure there can be no nesting of this function, otherwise
4659 * the seqlock logic goes bad. We can not serialize this because the arch
4660 * code calls this from NMI context.
4662 void perf_event_update_userpage(struct perf_event
*event
)
4664 struct perf_event_mmap_page
*userpg
;
4665 struct ring_buffer
*rb
;
4666 u64 enabled
, running
, now
;
4669 rb
= rcu_dereference(event
->rb
);
4674 * compute total_time_enabled, total_time_running
4675 * based on snapshot values taken when the event
4676 * was last scheduled in.
4678 * we cannot simply called update_context_time()
4679 * because of locking issue as we can be called in
4682 calc_timer_values(event
, &now
, &enabled
, &running
);
4684 userpg
= rb
->user_page
;
4686 * Disable preemption so as to not let the corresponding user-space
4687 * spin too long if we get preempted.
4692 userpg
->index
= perf_event_index(event
);
4693 userpg
->offset
= perf_event_count(event
);
4695 userpg
->offset
-= local64_read(&event
->hw
.prev_count
);
4697 userpg
->time_enabled
= enabled
+
4698 atomic64_read(&event
->child_total_time_enabled
);
4700 userpg
->time_running
= running
+
4701 atomic64_read(&event
->child_total_time_running
);
4703 arch_perf_update_userpage(event
, userpg
, now
);
4712 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
4714 struct perf_event
*event
= vma
->vm_file
->private_data
;
4715 struct ring_buffer
*rb
;
4716 int ret
= VM_FAULT_SIGBUS
;
4718 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
4719 if (vmf
->pgoff
== 0)
4725 rb
= rcu_dereference(event
->rb
);
4729 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
4732 vmf
->page
= perf_mmap_to_page(rb
, vmf
->pgoff
);
4736 get_page(vmf
->page
);
4737 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
4738 vmf
->page
->index
= vmf
->pgoff
;
4747 static void ring_buffer_attach(struct perf_event
*event
,
4748 struct ring_buffer
*rb
)
4750 struct ring_buffer
*old_rb
= NULL
;
4751 unsigned long flags
;
4755 * Should be impossible, we set this when removing
4756 * event->rb_entry and wait/clear when adding event->rb_entry.
4758 WARN_ON_ONCE(event
->rcu_pending
);
4761 spin_lock_irqsave(&old_rb
->event_lock
, flags
);
4762 list_del_rcu(&event
->rb_entry
);
4763 spin_unlock_irqrestore(&old_rb
->event_lock
, flags
);
4765 event
->rcu_batches
= get_state_synchronize_rcu();
4766 event
->rcu_pending
= 1;
4770 if (event
->rcu_pending
) {
4771 cond_synchronize_rcu(event
->rcu_batches
);
4772 event
->rcu_pending
= 0;
4775 spin_lock_irqsave(&rb
->event_lock
, flags
);
4776 list_add_rcu(&event
->rb_entry
, &rb
->event_list
);
4777 spin_unlock_irqrestore(&rb
->event_lock
, flags
);
4780 rcu_assign_pointer(event
->rb
, rb
);
4783 ring_buffer_put(old_rb
);
4785 * Since we detached before setting the new rb, so that we
4786 * could attach the new rb, we could have missed a wakeup.
4789 wake_up_all(&event
->waitq
);
4793 static void ring_buffer_wakeup(struct perf_event
*event
)
4795 struct ring_buffer
*rb
;
4798 rb
= rcu_dereference(event
->rb
);
4800 list_for_each_entry_rcu(event
, &rb
->event_list
, rb_entry
)
4801 wake_up_all(&event
->waitq
);
4806 struct ring_buffer
*ring_buffer_get(struct perf_event
*event
)
4808 struct ring_buffer
*rb
;
4811 rb
= rcu_dereference(event
->rb
);
4813 if (!atomic_inc_not_zero(&rb
->refcount
))
4821 void ring_buffer_put(struct ring_buffer
*rb
)
4823 if (!atomic_dec_and_test(&rb
->refcount
))
4826 WARN_ON_ONCE(!list_empty(&rb
->event_list
));
4828 call_rcu(&rb
->rcu_head
, rb_free_rcu
);
4831 static void perf_mmap_open(struct vm_area_struct
*vma
)
4833 struct perf_event
*event
= vma
->vm_file
->private_data
;
4835 atomic_inc(&event
->mmap_count
);
4836 atomic_inc(&event
->rb
->mmap_count
);
4839 atomic_inc(&event
->rb
->aux_mmap_count
);
4841 if (event
->pmu
->event_mapped
)
4842 event
->pmu
->event_mapped(event
);
4845 static void perf_pmu_output_stop(struct perf_event
*event
);
4848 * A buffer can be mmap()ed multiple times; either directly through the same
4849 * event, or through other events by use of perf_event_set_output().
4851 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4852 * the buffer here, where we still have a VM context. This means we need
4853 * to detach all events redirecting to us.
4855 static void perf_mmap_close(struct vm_area_struct
*vma
)
4857 struct perf_event
*event
= vma
->vm_file
->private_data
;
4859 struct ring_buffer
*rb
= ring_buffer_get(event
);
4860 struct user_struct
*mmap_user
= rb
->mmap_user
;
4861 int mmap_locked
= rb
->mmap_locked
;
4862 unsigned long size
= perf_data_size(rb
);
4864 if (event
->pmu
->event_unmapped
)
4865 event
->pmu
->event_unmapped(event
);
4868 * rb->aux_mmap_count will always drop before rb->mmap_count and
4869 * event->mmap_count, so it is ok to use event->mmap_mutex to
4870 * serialize with perf_mmap here.
4872 if (rb_has_aux(rb
) && vma
->vm_pgoff
== rb
->aux_pgoff
&&
4873 atomic_dec_and_mutex_lock(&rb
->aux_mmap_count
, &event
->mmap_mutex
)) {
4875 * Stop all AUX events that are writing to this buffer,
4876 * so that we can free its AUX pages and corresponding PMU
4877 * data. Note that after rb::aux_mmap_count dropped to zero,
4878 * they won't start any more (see perf_aux_output_begin()).
4880 perf_pmu_output_stop(event
);
4882 /* now it's safe to free the pages */
4883 atomic_long_sub(rb
->aux_nr_pages
, &mmap_user
->locked_vm
);
4884 vma
->vm_mm
->pinned_vm
-= rb
->aux_mmap_locked
;
4886 /* this has to be the last one */
4888 WARN_ON_ONCE(atomic_read(&rb
->aux_refcount
));
4890 mutex_unlock(&event
->mmap_mutex
);
4893 atomic_dec(&rb
->mmap_count
);
4895 if (!atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
))
4898 ring_buffer_attach(event
, NULL
);
4899 mutex_unlock(&event
->mmap_mutex
);
4901 /* If there's still other mmap()s of this buffer, we're done. */
4902 if (atomic_read(&rb
->mmap_count
))
4906 * No other mmap()s, detach from all other events that might redirect
4907 * into the now unreachable buffer. Somewhat complicated by the
4908 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4912 list_for_each_entry_rcu(event
, &rb
->event_list
, rb_entry
) {
4913 if (!atomic_long_inc_not_zero(&event
->refcount
)) {
4915 * This event is en-route to free_event() which will
4916 * detach it and remove it from the list.
4922 mutex_lock(&event
->mmap_mutex
);
4924 * Check we didn't race with perf_event_set_output() which can
4925 * swizzle the rb from under us while we were waiting to
4926 * acquire mmap_mutex.
4928 * If we find a different rb; ignore this event, a next
4929 * iteration will no longer find it on the list. We have to
4930 * still restart the iteration to make sure we're not now
4931 * iterating the wrong list.
4933 if (event
->rb
== rb
)
4934 ring_buffer_attach(event
, NULL
);
4936 mutex_unlock(&event
->mmap_mutex
);
4940 * Restart the iteration; either we're on the wrong list or
4941 * destroyed its integrity by doing a deletion.
4948 * It could be there's still a few 0-ref events on the list; they'll
4949 * get cleaned up by free_event() -- they'll also still have their
4950 * ref on the rb and will free it whenever they are done with it.
4952 * Aside from that, this buffer is 'fully' detached and unmapped,
4953 * undo the VM accounting.
4956 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &mmap_user
->locked_vm
);
4957 vma
->vm_mm
->pinned_vm
-= mmap_locked
;
4958 free_uid(mmap_user
);
4961 ring_buffer_put(rb
); /* could be last */
4964 static const struct vm_operations_struct perf_mmap_vmops
= {
4965 .open
= perf_mmap_open
,
4966 .close
= perf_mmap_close
, /* non mergable */
4967 .fault
= perf_mmap_fault
,
4968 .page_mkwrite
= perf_mmap_fault
,
4971 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
4973 struct perf_event
*event
= file
->private_data
;
4974 unsigned long user_locked
, user_lock_limit
;
4975 struct user_struct
*user
= current_user();
4976 unsigned long locked
, lock_limit
;
4977 struct ring_buffer
*rb
= NULL
;
4978 unsigned long vma_size
;
4979 unsigned long nr_pages
;
4980 long user_extra
= 0, extra
= 0;
4981 int ret
= 0, flags
= 0;
4984 * Don't allow mmap() of inherited per-task counters. This would
4985 * create a performance issue due to all children writing to the
4988 if (event
->cpu
== -1 && event
->attr
.inherit
)
4991 if (!(vma
->vm_flags
& VM_SHARED
))
4994 vma_size
= vma
->vm_end
- vma
->vm_start
;
4996 if (vma
->vm_pgoff
== 0) {
4997 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
5000 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
5001 * mapped, all subsequent mappings should have the same size
5002 * and offset. Must be above the normal perf buffer.
5004 u64 aux_offset
, aux_size
;
5009 nr_pages
= vma_size
/ PAGE_SIZE
;
5011 mutex_lock(&event
->mmap_mutex
);
5018 aux_offset
= ACCESS_ONCE(rb
->user_page
->aux_offset
);
5019 aux_size
= ACCESS_ONCE(rb
->user_page
->aux_size
);
5021 if (aux_offset
< perf_data_size(rb
) + PAGE_SIZE
)
5024 if (aux_offset
!= vma
->vm_pgoff
<< PAGE_SHIFT
)
5027 /* already mapped with a different offset */
5028 if (rb_has_aux(rb
) && rb
->aux_pgoff
!= vma
->vm_pgoff
)
5031 if (aux_size
!= vma_size
|| aux_size
!= nr_pages
* PAGE_SIZE
)
5034 /* already mapped with a different size */
5035 if (rb_has_aux(rb
) && rb
->aux_nr_pages
!= nr_pages
)
5038 if (!is_power_of_2(nr_pages
))
5041 if (!atomic_inc_not_zero(&rb
->mmap_count
))
5044 if (rb_has_aux(rb
)) {
5045 atomic_inc(&rb
->aux_mmap_count
);
5050 atomic_set(&rb
->aux_mmap_count
, 1);
5051 user_extra
= nr_pages
;
5057 * If we have rb pages ensure they're a power-of-two number, so we
5058 * can do bitmasks instead of modulo.
5060 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
5063 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
5066 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
5068 mutex_lock(&event
->mmap_mutex
);
5070 if (event
->rb
->nr_pages
!= nr_pages
) {
5075 if (!atomic_inc_not_zero(&event
->rb
->mmap_count
)) {
5077 * Raced against perf_mmap_close() through
5078 * perf_event_set_output(). Try again, hope for better
5081 mutex_unlock(&event
->mmap_mutex
);
5088 user_extra
= nr_pages
+ 1;
5091 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
5094 * Increase the limit linearly with more CPUs:
5096 user_lock_limit
*= num_online_cpus();
5098 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
5100 if (user_locked
> user_lock_limit
)
5101 extra
= user_locked
- user_lock_limit
;
5103 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
5104 lock_limit
>>= PAGE_SHIFT
;
5105 locked
= vma
->vm_mm
->pinned_vm
+ extra
;
5107 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
5108 !capable(CAP_IPC_LOCK
)) {
5113 WARN_ON(!rb
&& event
->rb
);
5115 if (vma
->vm_flags
& VM_WRITE
)
5116 flags
|= RING_BUFFER_WRITABLE
;
5119 rb
= rb_alloc(nr_pages
,
5120 event
->attr
.watermark
? event
->attr
.wakeup_watermark
: 0,
5128 atomic_set(&rb
->mmap_count
, 1);
5129 rb
->mmap_user
= get_current_user();
5130 rb
->mmap_locked
= extra
;
5132 ring_buffer_attach(event
, rb
);
5134 perf_event_init_userpage(event
);
5135 perf_event_update_userpage(event
);
5137 ret
= rb_alloc_aux(rb
, event
, vma
->vm_pgoff
, nr_pages
,
5138 event
->attr
.aux_watermark
, flags
);
5140 rb
->aux_mmap_locked
= extra
;
5145 atomic_long_add(user_extra
, &user
->locked_vm
);
5146 vma
->vm_mm
->pinned_vm
+= extra
;
5148 atomic_inc(&event
->mmap_count
);
5150 atomic_dec(&rb
->mmap_count
);
5153 mutex_unlock(&event
->mmap_mutex
);
5156 * Since pinned accounting is per vm we cannot allow fork() to copy our
5159 vma
->vm_flags
|= VM_DONTCOPY
| VM_DONTEXPAND
| VM_DONTDUMP
;
5160 vma
->vm_ops
= &perf_mmap_vmops
;
5162 if (event
->pmu
->event_mapped
)
5163 event
->pmu
->event_mapped(event
);
5168 static int perf_fasync(int fd
, struct file
*filp
, int on
)
5170 struct inode
*inode
= file_inode(filp
);
5171 struct perf_event
*event
= filp
->private_data
;
5175 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
5176 inode_unlock(inode
);
5184 static const struct file_operations perf_fops
= {
5185 .llseek
= no_llseek
,
5186 .release
= perf_release
,
5189 .unlocked_ioctl
= perf_ioctl
,
5190 .compat_ioctl
= perf_compat_ioctl
,
5192 .fasync
= perf_fasync
,
5198 * If there's data, ensure we set the poll() state and publish everything
5199 * to user-space before waking everybody up.
5202 static inline struct fasync_struct
**perf_event_fasync(struct perf_event
*event
)
5204 /* only the parent has fasync state */
5206 event
= event
->parent
;
5207 return &event
->fasync
;
5210 void perf_event_wakeup(struct perf_event
*event
)
5212 ring_buffer_wakeup(event
);
5214 if (event
->pending_kill
) {
5215 kill_fasync(perf_event_fasync(event
), SIGIO
, event
->pending_kill
);
5216 event
->pending_kill
= 0;
5220 static void perf_pending_event(struct irq_work
*entry
)
5222 struct perf_event
*event
= container_of(entry
,
5223 struct perf_event
, pending
);
5226 rctx
= perf_swevent_get_recursion_context();
5228 * If we 'fail' here, that's OK, it means recursion is already disabled
5229 * and we won't recurse 'further'.
5232 if (event
->pending_disable
) {
5233 event
->pending_disable
= 0;
5234 perf_event_disable_local(event
);
5237 if (event
->pending_wakeup
) {
5238 event
->pending_wakeup
= 0;
5239 perf_event_wakeup(event
);
5243 perf_swevent_put_recursion_context(rctx
);
5247 * We assume there is only KVM supporting the callbacks.
5248 * Later on, we might change it to a list if there is
5249 * another virtualization implementation supporting the callbacks.
5251 struct perf_guest_info_callbacks
*perf_guest_cbs
;
5253 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
5255 perf_guest_cbs
= cbs
;
5258 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks
);
5260 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
5262 perf_guest_cbs
= NULL
;
5265 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks
);
5268 perf_output_sample_regs(struct perf_output_handle
*handle
,
5269 struct pt_regs
*regs
, u64 mask
)
5273 for_each_set_bit(bit
, (const unsigned long *) &mask
,
5274 sizeof(mask
) * BITS_PER_BYTE
) {
5277 val
= perf_reg_value(regs
, bit
);
5278 perf_output_put(handle
, val
);
5282 static void perf_sample_regs_user(struct perf_regs
*regs_user
,
5283 struct pt_regs
*regs
,
5284 struct pt_regs
*regs_user_copy
)
5286 if (user_mode(regs
)) {
5287 regs_user
->abi
= perf_reg_abi(current
);
5288 regs_user
->regs
= regs
;
5289 } else if (current
->mm
) {
5290 perf_get_regs_user(regs_user
, regs
, regs_user_copy
);
5292 regs_user
->abi
= PERF_SAMPLE_REGS_ABI_NONE
;
5293 regs_user
->regs
= NULL
;
5297 static void perf_sample_regs_intr(struct perf_regs
*regs_intr
,
5298 struct pt_regs
*regs
)
5300 regs_intr
->regs
= regs
;
5301 regs_intr
->abi
= perf_reg_abi(current
);
5306 * Get remaining task size from user stack pointer.
5308 * It'd be better to take stack vma map and limit this more
5309 * precisly, but there's no way to get it safely under interrupt,
5310 * so using TASK_SIZE as limit.
5312 static u64
perf_ustack_task_size(struct pt_regs
*regs
)
5314 unsigned long addr
= perf_user_stack_pointer(regs
);
5316 if (!addr
|| addr
>= TASK_SIZE
)
5319 return TASK_SIZE
- addr
;
5323 perf_sample_ustack_size(u16 stack_size
, u16 header_size
,
5324 struct pt_regs
*regs
)
5328 /* No regs, no stack pointer, no dump. */
5333 * Check if we fit in with the requested stack size into the:
5335 * If we don't, we limit the size to the TASK_SIZE.
5337 * - remaining sample size
5338 * If we don't, we customize the stack size to
5339 * fit in to the remaining sample size.
5342 task_size
= min((u64
) USHRT_MAX
, perf_ustack_task_size(regs
));
5343 stack_size
= min(stack_size
, (u16
) task_size
);
5345 /* Current header size plus static size and dynamic size. */
5346 header_size
+= 2 * sizeof(u64
);
5348 /* Do we fit in with the current stack dump size? */
5349 if ((u16
) (header_size
+ stack_size
) < header_size
) {
5351 * If we overflow the maximum size for the sample,
5352 * we customize the stack dump size to fit in.
5354 stack_size
= USHRT_MAX
- header_size
- sizeof(u64
);
5355 stack_size
= round_up(stack_size
, sizeof(u64
));
5362 perf_output_sample_ustack(struct perf_output_handle
*handle
, u64 dump_size
,
5363 struct pt_regs
*regs
)
5365 /* Case of a kernel thread, nothing to dump */
5368 perf_output_put(handle
, size
);
5377 * - the size requested by user or the best one we can fit
5378 * in to the sample max size
5380 * - user stack dump data
5382 * - the actual dumped size
5386 perf_output_put(handle
, dump_size
);
5389 sp
= perf_user_stack_pointer(regs
);
5390 rem
= __output_copy_user(handle
, (void *) sp
, dump_size
);
5391 dyn_size
= dump_size
- rem
;
5393 perf_output_skip(handle
, rem
);
5396 perf_output_put(handle
, dyn_size
);
5400 static void __perf_event_header__init_id(struct perf_event_header
*header
,
5401 struct perf_sample_data
*data
,
5402 struct perf_event
*event
)
5404 u64 sample_type
= event
->attr
.sample_type
;
5406 data
->type
= sample_type
;
5407 header
->size
+= event
->id_header_size
;
5409 if (sample_type
& PERF_SAMPLE_TID
) {
5410 /* namespace issues */
5411 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
5412 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
5415 if (sample_type
& PERF_SAMPLE_TIME
)
5416 data
->time
= perf_event_clock(event
);
5418 if (sample_type
& (PERF_SAMPLE_ID
| PERF_SAMPLE_IDENTIFIER
))
5419 data
->id
= primary_event_id(event
);
5421 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
5422 data
->stream_id
= event
->id
;
5424 if (sample_type
& PERF_SAMPLE_CPU
) {
5425 data
->cpu_entry
.cpu
= raw_smp_processor_id();
5426 data
->cpu_entry
.reserved
= 0;
5430 void perf_event_header__init_id(struct perf_event_header
*header
,
5431 struct perf_sample_data
*data
,
5432 struct perf_event
*event
)
5434 if (event
->attr
.sample_id_all
)
5435 __perf_event_header__init_id(header
, data
, event
);
5438 static void __perf_event__output_id_sample(struct perf_output_handle
*handle
,
5439 struct perf_sample_data
*data
)
5441 u64 sample_type
= data
->type
;
5443 if (sample_type
& PERF_SAMPLE_TID
)
5444 perf_output_put(handle
, data
->tid_entry
);
5446 if (sample_type
& PERF_SAMPLE_TIME
)
5447 perf_output_put(handle
, data
->time
);
5449 if (sample_type
& PERF_SAMPLE_ID
)
5450 perf_output_put(handle
, data
->id
);
5452 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
5453 perf_output_put(handle
, data
->stream_id
);
5455 if (sample_type
& PERF_SAMPLE_CPU
)
5456 perf_output_put(handle
, data
->cpu_entry
);
5458 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
5459 perf_output_put(handle
, data
->id
);
5462 void perf_event__output_id_sample(struct perf_event
*event
,
5463 struct perf_output_handle
*handle
,
5464 struct perf_sample_data
*sample
)
5466 if (event
->attr
.sample_id_all
)
5467 __perf_event__output_id_sample(handle
, sample
);
5470 static void perf_output_read_one(struct perf_output_handle
*handle
,
5471 struct perf_event
*event
,
5472 u64 enabled
, u64 running
)
5474 u64 read_format
= event
->attr
.read_format
;
5478 values
[n
++] = perf_event_count(event
);
5479 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
5480 values
[n
++] = enabled
+
5481 atomic64_read(&event
->child_total_time_enabled
);
5483 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
5484 values
[n
++] = running
+
5485 atomic64_read(&event
->child_total_time_running
);
5487 if (read_format
& PERF_FORMAT_ID
)
5488 values
[n
++] = primary_event_id(event
);
5490 __output_copy(handle
, values
, n
* sizeof(u64
));
5494 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
5496 static void perf_output_read_group(struct perf_output_handle
*handle
,
5497 struct perf_event
*event
,
5498 u64 enabled
, u64 running
)
5500 struct perf_event
*leader
= event
->group_leader
, *sub
;
5501 u64 read_format
= event
->attr
.read_format
;
5505 values
[n
++] = 1 + leader
->nr_siblings
;
5507 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
5508 values
[n
++] = enabled
;
5510 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
5511 values
[n
++] = running
;
5513 if (leader
!= event
)
5514 leader
->pmu
->read(leader
);
5516 values
[n
++] = perf_event_count(leader
);
5517 if (read_format
& PERF_FORMAT_ID
)
5518 values
[n
++] = primary_event_id(leader
);
5520 __output_copy(handle
, values
, n
* sizeof(u64
));
5522 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
5525 if ((sub
!= event
) &&
5526 (sub
->state
== PERF_EVENT_STATE_ACTIVE
))
5527 sub
->pmu
->read(sub
);
5529 values
[n
++] = perf_event_count(sub
);
5530 if (read_format
& PERF_FORMAT_ID
)
5531 values
[n
++] = primary_event_id(sub
);
5533 __output_copy(handle
, values
, n
* sizeof(u64
));
5537 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5538 PERF_FORMAT_TOTAL_TIME_RUNNING)
5540 static void perf_output_read(struct perf_output_handle
*handle
,
5541 struct perf_event
*event
)
5543 u64 enabled
= 0, running
= 0, now
;
5544 u64 read_format
= event
->attr
.read_format
;
5547 * compute total_time_enabled, total_time_running
5548 * based on snapshot values taken when the event
5549 * was last scheduled in.
5551 * we cannot simply called update_context_time()
5552 * because of locking issue as we are called in
5555 if (read_format
& PERF_FORMAT_TOTAL_TIMES
)
5556 calc_timer_values(event
, &now
, &enabled
, &running
);
5558 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
5559 perf_output_read_group(handle
, event
, enabled
, running
);
5561 perf_output_read_one(handle
, event
, enabled
, running
);
5564 void perf_output_sample(struct perf_output_handle
*handle
,
5565 struct perf_event_header
*header
,
5566 struct perf_sample_data
*data
,
5567 struct perf_event
*event
)
5569 u64 sample_type
= data
->type
;
5571 perf_output_put(handle
, *header
);
5573 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
5574 perf_output_put(handle
, data
->id
);
5576 if (sample_type
& PERF_SAMPLE_IP
)
5577 perf_output_put(handle
, data
->ip
);
5579 if (sample_type
& PERF_SAMPLE_TID
)
5580 perf_output_put(handle
, data
->tid_entry
);
5582 if (sample_type
& PERF_SAMPLE_TIME
)
5583 perf_output_put(handle
, data
->time
);
5585 if (sample_type
& PERF_SAMPLE_ADDR
)
5586 perf_output_put(handle
, data
->addr
);
5588 if (sample_type
& PERF_SAMPLE_ID
)
5589 perf_output_put(handle
, data
->id
);
5591 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
5592 perf_output_put(handle
, data
->stream_id
);
5594 if (sample_type
& PERF_SAMPLE_CPU
)
5595 perf_output_put(handle
, data
->cpu_entry
);
5597 if (sample_type
& PERF_SAMPLE_PERIOD
)
5598 perf_output_put(handle
, data
->period
);
5600 if (sample_type
& PERF_SAMPLE_READ
)
5601 perf_output_read(handle
, event
);
5603 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
5604 if (data
->callchain
) {
5607 if (data
->callchain
)
5608 size
+= data
->callchain
->nr
;
5610 size
*= sizeof(u64
);
5612 __output_copy(handle
, data
->callchain
, size
);
5615 perf_output_put(handle
, nr
);
5619 if (sample_type
& PERF_SAMPLE_RAW
) {
5621 u32 raw_size
= data
->raw
->size
;
5622 u32 real_size
= round_up(raw_size
+ sizeof(u32
),
5623 sizeof(u64
)) - sizeof(u32
);
5626 perf_output_put(handle
, real_size
);
5627 __output_copy(handle
, data
->raw
->data
, raw_size
);
5628 if (real_size
- raw_size
)
5629 __output_copy(handle
, &zero
, real_size
- raw_size
);
5635 .size
= sizeof(u32
),
5638 perf_output_put(handle
, raw
);
5642 if (sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
5643 if (data
->br_stack
) {
5646 size
= data
->br_stack
->nr
5647 * sizeof(struct perf_branch_entry
);
5649 perf_output_put(handle
, data
->br_stack
->nr
);
5650 perf_output_copy(handle
, data
->br_stack
->entries
, size
);
5653 * we always store at least the value of nr
5656 perf_output_put(handle
, nr
);
5660 if (sample_type
& PERF_SAMPLE_REGS_USER
) {
5661 u64 abi
= data
->regs_user
.abi
;
5664 * If there are no regs to dump, notice it through
5665 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5667 perf_output_put(handle
, abi
);
5670 u64 mask
= event
->attr
.sample_regs_user
;
5671 perf_output_sample_regs(handle
,
5672 data
->regs_user
.regs
,
5677 if (sample_type
& PERF_SAMPLE_STACK_USER
) {
5678 perf_output_sample_ustack(handle
,
5679 data
->stack_user_size
,
5680 data
->regs_user
.regs
);
5683 if (sample_type
& PERF_SAMPLE_WEIGHT
)
5684 perf_output_put(handle
, data
->weight
);
5686 if (sample_type
& PERF_SAMPLE_DATA_SRC
)
5687 perf_output_put(handle
, data
->data_src
.val
);
5689 if (sample_type
& PERF_SAMPLE_TRANSACTION
)
5690 perf_output_put(handle
, data
->txn
);
5692 if (sample_type
& PERF_SAMPLE_REGS_INTR
) {
5693 u64 abi
= data
->regs_intr
.abi
;
5695 * If there are no regs to dump, notice it through
5696 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5698 perf_output_put(handle
, abi
);
5701 u64 mask
= event
->attr
.sample_regs_intr
;
5703 perf_output_sample_regs(handle
,
5704 data
->regs_intr
.regs
,
5709 if (!event
->attr
.watermark
) {
5710 int wakeup_events
= event
->attr
.wakeup_events
;
5712 if (wakeup_events
) {
5713 struct ring_buffer
*rb
= handle
->rb
;
5714 int events
= local_inc_return(&rb
->events
);
5716 if (events
>= wakeup_events
) {
5717 local_sub(wakeup_events
, &rb
->events
);
5718 local_inc(&rb
->wakeup
);
5724 void perf_prepare_sample(struct perf_event_header
*header
,
5725 struct perf_sample_data
*data
,
5726 struct perf_event
*event
,
5727 struct pt_regs
*regs
)
5729 u64 sample_type
= event
->attr
.sample_type
;
5731 header
->type
= PERF_RECORD_SAMPLE
;
5732 header
->size
= sizeof(*header
) + event
->header_size
;
5735 header
->misc
|= perf_misc_flags(regs
);
5737 __perf_event_header__init_id(header
, data
, event
);
5739 if (sample_type
& PERF_SAMPLE_IP
)
5740 data
->ip
= perf_instruction_pointer(regs
);
5742 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
5745 data
->callchain
= perf_callchain(event
, regs
);
5747 if (data
->callchain
)
5748 size
+= data
->callchain
->nr
;
5750 header
->size
+= size
* sizeof(u64
);
5753 if (sample_type
& PERF_SAMPLE_RAW
) {
5754 int size
= sizeof(u32
);
5757 size
+= data
->raw
->size
;
5759 size
+= sizeof(u32
);
5761 header
->size
+= round_up(size
, sizeof(u64
));
5764 if (sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
5765 int size
= sizeof(u64
); /* nr */
5766 if (data
->br_stack
) {
5767 size
+= data
->br_stack
->nr
5768 * sizeof(struct perf_branch_entry
);
5770 header
->size
+= size
;
5773 if (sample_type
& (PERF_SAMPLE_REGS_USER
| PERF_SAMPLE_STACK_USER
))
5774 perf_sample_regs_user(&data
->regs_user
, regs
,
5775 &data
->regs_user_copy
);
5777 if (sample_type
& PERF_SAMPLE_REGS_USER
) {
5778 /* regs dump ABI info */
5779 int size
= sizeof(u64
);
5781 if (data
->regs_user
.regs
) {
5782 u64 mask
= event
->attr
.sample_regs_user
;
5783 size
+= hweight64(mask
) * sizeof(u64
);
5786 header
->size
+= size
;
5789 if (sample_type
& PERF_SAMPLE_STACK_USER
) {
5791 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5792 * processed as the last one or have additional check added
5793 * in case new sample type is added, because we could eat
5794 * up the rest of the sample size.
5796 u16 stack_size
= event
->attr
.sample_stack_user
;
5797 u16 size
= sizeof(u64
);
5799 stack_size
= perf_sample_ustack_size(stack_size
, header
->size
,
5800 data
->regs_user
.regs
);
5803 * If there is something to dump, add space for the dump
5804 * itself and for the field that tells the dynamic size,
5805 * which is how many have been actually dumped.
5808 size
+= sizeof(u64
) + stack_size
;
5810 data
->stack_user_size
= stack_size
;
5811 header
->size
+= size
;
5814 if (sample_type
& PERF_SAMPLE_REGS_INTR
) {
5815 /* regs dump ABI info */
5816 int size
= sizeof(u64
);
5818 perf_sample_regs_intr(&data
->regs_intr
, regs
);
5820 if (data
->regs_intr
.regs
) {
5821 u64 mask
= event
->attr
.sample_regs_intr
;
5823 size
+= hweight64(mask
) * sizeof(u64
);
5826 header
->size
+= size
;
5830 static void __always_inline
5831 __perf_event_output(struct perf_event
*event
,
5832 struct perf_sample_data
*data
,
5833 struct pt_regs
*regs
,
5834 int (*output_begin
)(struct perf_output_handle
*,
5835 struct perf_event
*,
5838 struct perf_output_handle handle
;
5839 struct perf_event_header header
;
5841 /* protect the callchain buffers */
5844 perf_prepare_sample(&header
, data
, event
, regs
);
5846 if (output_begin(&handle
, event
, header
.size
))
5849 perf_output_sample(&handle
, &header
, data
, event
);
5851 perf_output_end(&handle
);
5858 perf_event_output_forward(struct perf_event
*event
,
5859 struct perf_sample_data
*data
,
5860 struct pt_regs
*regs
)
5862 __perf_event_output(event
, data
, regs
, perf_output_begin_forward
);
5866 perf_event_output_backward(struct perf_event
*event
,
5867 struct perf_sample_data
*data
,
5868 struct pt_regs
*regs
)
5870 __perf_event_output(event
, data
, regs
, perf_output_begin_backward
);
5874 perf_event_output(struct perf_event
*event
,
5875 struct perf_sample_data
*data
,
5876 struct pt_regs
*regs
)
5878 __perf_event_output(event
, data
, regs
, perf_output_begin
);
5885 struct perf_read_event
{
5886 struct perf_event_header header
;
5893 perf_event_read_event(struct perf_event
*event
,
5894 struct task_struct
*task
)
5896 struct perf_output_handle handle
;
5897 struct perf_sample_data sample
;
5898 struct perf_read_event read_event
= {
5900 .type
= PERF_RECORD_READ
,
5902 .size
= sizeof(read_event
) + event
->read_size
,
5904 .pid
= perf_event_pid(event
, task
),
5905 .tid
= perf_event_tid(event
, task
),
5909 perf_event_header__init_id(&read_event
.header
, &sample
, event
);
5910 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
);
5914 perf_output_put(&handle
, read_event
);
5915 perf_output_read(&handle
, event
);
5916 perf_event__output_id_sample(event
, &handle
, &sample
);
5918 perf_output_end(&handle
);
5921 typedef void (perf_iterate_f
)(struct perf_event
*event
, void *data
);
5924 perf_iterate_ctx(struct perf_event_context
*ctx
,
5925 perf_iterate_f output
,
5926 void *data
, bool all
)
5928 struct perf_event
*event
;
5930 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
5932 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
5934 if (!event_filter_match(event
))
5938 output(event
, data
);
5942 static void perf_iterate_sb_cpu(perf_iterate_f output
, void *data
)
5944 struct pmu_event_list
*pel
= this_cpu_ptr(&pmu_sb_events
);
5945 struct perf_event
*event
;
5947 list_for_each_entry_rcu(event
, &pel
->list
, sb_list
) {
5948 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
5950 if (!event_filter_match(event
))
5952 output(event
, data
);
5957 * Iterate all events that need to receive side-band events.
5959 * For new callers; ensure that account_pmu_sb_event() includes
5960 * your event, otherwise it might not get delivered.
5963 perf_iterate_sb(perf_iterate_f output
, void *data
,
5964 struct perf_event_context
*task_ctx
)
5966 struct perf_event_context
*ctx
;
5973 * If we have task_ctx != NULL we only notify the task context itself.
5974 * The task_ctx is set only for EXIT events before releasing task
5978 perf_iterate_ctx(task_ctx
, output
, data
, false);
5982 perf_iterate_sb_cpu(output
, data
);
5984 for_each_task_context_nr(ctxn
) {
5985 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
5987 perf_iterate_ctx(ctx
, output
, data
, false);
5995 * Clear all file-based filters at exec, they'll have to be
5996 * re-instated when/if these objects are mmapped again.
5998 static void perf_event_addr_filters_exec(struct perf_event
*event
, void *data
)
6000 struct perf_addr_filters_head
*ifh
= perf_event_addr_filters(event
);
6001 struct perf_addr_filter
*filter
;
6002 unsigned int restart
= 0, count
= 0;
6003 unsigned long flags
;
6005 if (!has_addr_filter(event
))
6008 raw_spin_lock_irqsave(&ifh
->lock
, flags
);
6009 list_for_each_entry(filter
, &ifh
->list
, entry
) {
6010 if (filter
->inode
) {
6011 event
->addr_filters_offs
[count
] = 0;
6019 event
->addr_filters_gen
++;
6020 raw_spin_unlock_irqrestore(&ifh
->lock
, flags
);
6023 perf_event_restart(event
);
6026 void perf_event_exec(void)
6028 struct perf_event_context
*ctx
;
6032 for_each_task_context_nr(ctxn
) {
6033 ctx
= current
->perf_event_ctxp
[ctxn
];
6037 perf_event_enable_on_exec(ctxn
);
6039 perf_iterate_ctx(ctx
, perf_event_addr_filters_exec
, NULL
,
6045 struct remote_output
{
6046 struct ring_buffer
*rb
;
6050 static void __perf_event_output_stop(struct perf_event
*event
, void *data
)
6052 struct perf_event
*parent
= event
->parent
;
6053 struct remote_output
*ro
= data
;
6054 struct ring_buffer
*rb
= ro
->rb
;
6055 struct stop_event_data sd
= {
6059 if (!has_aux(event
))
6066 * In case of inheritance, it will be the parent that links to the
6067 * ring-buffer, but it will be the child that's actually using it:
6069 if (rcu_dereference(parent
->rb
) == rb
)
6070 ro
->err
= __perf_event_stop(&sd
);
6073 static int __perf_pmu_output_stop(void *info
)
6075 struct perf_event
*event
= info
;
6076 struct pmu
*pmu
= event
->pmu
;
6077 struct perf_cpu_context
*cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
6078 struct remote_output ro
= {
6083 perf_iterate_ctx(&cpuctx
->ctx
, __perf_event_output_stop
, &ro
, false);
6084 if (cpuctx
->task_ctx
)
6085 perf_iterate_ctx(cpuctx
->task_ctx
, __perf_event_output_stop
,
6092 static void perf_pmu_output_stop(struct perf_event
*event
)
6094 struct perf_event
*iter
;
6099 list_for_each_entry_rcu(iter
, &event
->rb
->event_list
, rb_entry
) {
6101 * For per-CPU events, we need to make sure that neither they
6102 * nor their children are running; for cpu==-1 events it's
6103 * sufficient to stop the event itself if it's active, since
6104 * it can't have children.
6108 cpu
= READ_ONCE(iter
->oncpu
);
6113 err
= cpu_function_call(cpu
, __perf_pmu_output_stop
, event
);
6114 if (err
== -EAGAIN
) {
6123 * task tracking -- fork/exit
6125 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
6128 struct perf_task_event
{
6129 struct task_struct
*task
;
6130 struct perf_event_context
*task_ctx
;
6133 struct perf_event_header header
;
6143 static int perf_event_task_match(struct perf_event
*event
)
6145 return event
->attr
.comm
|| event
->attr
.mmap
||
6146 event
->attr
.mmap2
|| event
->attr
.mmap_data
||
6150 static void perf_event_task_output(struct perf_event
*event
,
6153 struct perf_task_event
*task_event
= data
;
6154 struct perf_output_handle handle
;
6155 struct perf_sample_data sample
;
6156 struct task_struct
*task
= task_event
->task
;
6157 int ret
, size
= task_event
->event_id
.header
.size
;
6159 if (!perf_event_task_match(event
))
6162 perf_event_header__init_id(&task_event
->event_id
.header
, &sample
, event
);
6164 ret
= perf_output_begin(&handle
, event
,
6165 task_event
->event_id
.header
.size
);
6169 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
6170 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
6172 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
6173 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
6175 task_event
->event_id
.time
= perf_event_clock(event
);
6177 perf_output_put(&handle
, task_event
->event_id
);
6179 perf_event__output_id_sample(event
, &handle
, &sample
);
6181 perf_output_end(&handle
);
6183 task_event
->event_id
.header
.size
= size
;
6186 static void perf_event_task(struct task_struct
*task
,
6187 struct perf_event_context
*task_ctx
,
6190 struct perf_task_event task_event
;
6192 if (!atomic_read(&nr_comm_events
) &&
6193 !atomic_read(&nr_mmap_events
) &&
6194 !atomic_read(&nr_task_events
))
6197 task_event
= (struct perf_task_event
){
6199 .task_ctx
= task_ctx
,
6202 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
6204 .size
= sizeof(task_event
.event_id
),
6214 perf_iterate_sb(perf_event_task_output
,
6219 void perf_event_fork(struct task_struct
*task
)
6221 perf_event_task(task
, NULL
, 1);
6228 struct perf_comm_event
{
6229 struct task_struct
*task
;
6234 struct perf_event_header header
;
6241 static int perf_event_comm_match(struct perf_event
*event
)
6243 return event
->attr
.comm
;
6246 static void perf_event_comm_output(struct perf_event
*event
,
6249 struct perf_comm_event
*comm_event
= data
;
6250 struct perf_output_handle handle
;
6251 struct perf_sample_data sample
;
6252 int size
= comm_event
->event_id
.header
.size
;
6255 if (!perf_event_comm_match(event
))
6258 perf_event_header__init_id(&comm_event
->event_id
.header
, &sample
, event
);
6259 ret
= perf_output_begin(&handle
, event
,
6260 comm_event
->event_id
.header
.size
);
6265 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
6266 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
6268 perf_output_put(&handle
, comm_event
->event_id
);
6269 __output_copy(&handle
, comm_event
->comm
,
6270 comm_event
->comm_size
);
6272 perf_event__output_id_sample(event
, &handle
, &sample
);
6274 perf_output_end(&handle
);
6276 comm_event
->event_id
.header
.size
= size
;
6279 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
6281 char comm
[TASK_COMM_LEN
];
6284 memset(comm
, 0, sizeof(comm
));
6285 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
6286 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
6288 comm_event
->comm
= comm
;
6289 comm_event
->comm_size
= size
;
6291 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
6293 perf_iterate_sb(perf_event_comm_output
,
6298 void perf_event_comm(struct task_struct
*task
, bool exec
)
6300 struct perf_comm_event comm_event
;
6302 if (!atomic_read(&nr_comm_events
))
6305 comm_event
= (struct perf_comm_event
){
6311 .type
= PERF_RECORD_COMM
,
6312 .misc
= exec
? PERF_RECORD_MISC_COMM_EXEC
: 0,
6320 perf_event_comm_event(&comm_event
);
6327 struct perf_mmap_event
{
6328 struct vm_area_struct
*vma
;
6330 const char *file_name
;
6338 struct perf_event_header header
;
6348 static int perf_event_mmap_match(struct perf_event
*event
,
6351 struct perf_mmap_event
*mmap_event
= data
;
6352 struct vm_area_struct
*vma
= mmap_event
->vma
;
6353 int executable
= vma
->vm_flags
& VM_EXEC
;
6355 return (!executable
&& event
->attr
.mmap_data
) ||
6356 (executable
&& (event
->attr
.mmap
|| event
->attr
.mmap2
));
6359 static void perf_event_mmap_output(struct perf_event
*event
,
6362 struct perf_mmap_event
*mmap_event
= data
;
6363 struct perf_output_handle handle
;
6364 struct perf_sample_data sample
;
6365 int size
= mmap_event
->event_id
.header
.size
;
6368 if (!perf_event_mmap_match(event
, data
))
6371 if (event
->attr
.mmap2
) {
6372 mmap_event
->event_id
.header
.type
= PERF_RECORD_MMAP2
;
6373 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->maj
);
6374 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->min
);
6375 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->ino
);
6376 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->ino_generation
);
6377 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->prot
);
6378 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->flags
);
6381 perf_event_header__init_id(&mmap_event
->event_id
.header
, &sample
, event
);
6382 ret
= perf_output_begin(&handle
, event
,
6383 mmap_event
->event_id
.header
.size
);
6387 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
6388 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
6390 perf_output_put(&handle
, mmap_event
->event_id
);
6392 if (event
->attr
.mmap2
) {
6393 perf_output_put(&handle
, mmap_event
->maj
);
6394 perf_output_put(&handle
, mmap_event
->min
);
6395 perf_output_put(&handle
, mmap_event
->ino
);
6396 perf_output_put(&handle
, mmap_event
->ino_generation
);
6397 perf_output_put(&handle
, mmap_event
->prot
);
6398 perf_output_put(&handle
, mmap_event
->flags
);
6401 __output_copy(&handle
, mmap_event
->file_name
,
6402 mmap_event
->file_size
);
6404 perf_event__output_id_sample(event
, &handle
, &sample
);
6406 perf_output_end(&handle
);
6408 mmap_event
->event_id
.header
.size
= size
;
6411 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
6413 struct vm_area_struct
*vma
= mmap_event
->vma
;
6414 struct file
*file
= vma
->vm_file
;
6415 int maj
= 0, min
= 0;
6416 u64 ino
= 0, gen
= 0;
6417 u32 prot
= 0, flags
= 0;
6424 struct inode
*inode
;
6427 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
6433 * d_path() works from the end of the rb backwards, so we
6434 * need to add enough zero bytes after the string to handle
6435 * the 64bit alignment we do later.
6437 name
= file_path(file
, buf
, PATH_MAX
- sizeof(u64
));
6442 inode
= file_inode(vma
->vm_file
);
6443 dev
= inode
->i_sb
->s_dev
;
6445 gen
= inode
->i_generation
;
6449 if (vma
->vm_flags
& VM_READ
)
6451 if (vma
->vm_flags
& VM_WRITE
)
6453 if (vma
->vm_flags
& VM_EXEC
)
6456 if (vma
->vm_flags
& VM_MAYSHARE
)
6459 flags
= MAP_PRIVATE
;
6461 if (vma
->vm_flags
& VM_DENYWRITE
)
6462 flags
|= MAP_DENYWRITE
;
6463 if (vma
->vm_flags
& VM_MAYEXEC
)
6464 flags
|= MAP_EXECUTABLE
;
6465 if (vma
->vm_flags
& VM_LOCKED
)
6466 flags
|= MAP_LOCKED
;
6467 if (vma
->vm_flags
& VM_HUGETLB
)
6468 flags
|= MAP_HUGETLB
;
6472 if (vma
->vm_ops
&& vma
->vm_ops
->name
) {
6473 name
= (char *) vma
->vm_ops
->name(vma
);
6478 name
= (char *)arch_vma_name(vma
);
6482 if (vma
->vm_start
<= vma
->vm_mm
->start_brk
&&
6483 vma
->vm_end
>= vma
->vm_mm
->brk
) {
6487 if (vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
6488 vma
->vm_end
>= vma
->vm_mm
->start_stack
) {
6498 strlcpy(tmp
, name
, sizeof(tmp
));
6502 * Since our buffer works in 8 byte units we need to align our string
6503 * size to a multiple of 8. However, we must guarantee the tail end is
6504 * zero'd out to avoid leaking random bits to userspace.
6506 size
= strlen(name
)+1;
6507 while (!IS_ALIGNED(size
, sizeof(u64
)))
6508 name
[size
++] = '\0';
6510 mmap_event
->file_name
= name
;
6511 mmap_event
->file_size
= size
;
6512 mmap_event
->maj
= maj
;
6513 mmap_event
->min
= min
;
6514 mmap_event
->ino
= ino
;
6515 mmap_event
->ino_generation
= gen
;
6516 mmap_event
->prot
= prot
;
6517 mmap_event
->flags
= flags
;
6519 if (!(vma
->vm_flags
& VM_EXEC
))
6520 mmap_event
->event_id
.header
.misc
|= PERF_RECORD_MISC_MMAP_DATA
;
6522 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
6524 perf_iterate_sb(perf_event_mmap_output
,
6532 * Whether this @filter depends on a dynamic object which is not loaded
6533 * yet or its load addresses are not known.
6535 static bool perf_addr_filter_needs_mmap(struct perf_addr_filter
*filter
)
6537 return filter
->filter
&& filter
->inode
;
6541 * Check whether inode and address range match filter criteria.
6543 static bool perf_addr_filter_match(struct perf_addr_filter
*filter
,
6544 struct file
*file
, unsigned long offset
,
6547 if (filter
->inode
!= file
->f_inode
)
6550 if (filter
->offset
> offset
+ size
)
6553 if (filter
->offset
+ filter
->size
< offset
)
6559 static void __perf_addr_filters_adjust(struct perf_event
*event
, void *data
)
6561 struct perf_addr_filters_head
*ifh
= perf_event_addr_filters(event
);
6562 struct vm_area_struct
*vma
= data
;
6563 unsigned long off
= vma
->vm_pgoff
<< PAGE_SHIFT
, flags
;
6564 struct file
*file
= vma
->vm_file
;
6565 struct perf_addr_filter
*filter
;
6566 unsigned int restart
= 0, count
= 0;
6568 if (!has_addr_filter(event
))
6574 raw_spin_lock_irqsave(&ifh
->lock
, flags
);
6575 list_for_each_entry(filter
, &ifh
->list
, entry
) {
6576 if (perf_addr_filter_match(filter
, file
, off
,
6577 vma
->vm_end
- vma
->vm_start
)) {
6578 event
->addr_filters_offs
[count
] = vma
->vm_start
;
6586 event
->addr_filters_gen
++;
6587 raw_spin_unlock_irqrestore(&ifh
->lock
, flags
);
6590 perf_event_restart(event
);
6594 * Adjust all task's events' filters to the new vma
6596 static void perf_addr_filters_adjust(struct vm_area_struct
*vma
)
6598 struct perf_event_context
*ctx
;
6602 for_each_task_context_nr(ctxn
) {
6603 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
6607 perf_iterate_ctx(ctx
, __perf_addr_filters_adjust
, vma
, true);
6612 void perf_event_mmap(struct vm_area_struct
*vma
)
6614 struct perf_mmap_event mmap_event
;
6616 if (!atomic_read(&nr_mmap_events
))
6619 mmap_event
= (struct perf_mmap_event
){
6625 .type
= PERF_RECORD_MMAP
,
6626 .misc
= PERF_RECORD_MISC_USER
,
6631 .start
= vma
->vm_start
,
6632 .len
= vma
->vm_end
- vma
->vm_start
,
6633 .pgoff
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
,
6635 /* .maj (attr_mmap2 only) */
6636 /* .min (attr_mmap2 only) */
6637 /* .ino (attr_mmap2 only) */
6638 /* .ino_generation (attr_mmap2 only) */
6639 /* .prot (attr_mmap2 only) */
6640 /* .flags (attr_mmap2 only) */
6643 perf_addr_filters_adjust(vma
);
6644 perf_event_mmap_event(&mmap_event
);
6647 void perf_event_aux_event(struct perf_event
*event
, unsigned long head
,
6648 unsigned long size
, u64 flags
)
6650 struct perf_output_handle handle
;
6651 struct perf_sample_data sample
;
6652 struct perf_aux_event
{
6653 struct perf_event_header header
;
6659 .type
= PERF_RECORD_AUX
,
6661 .size
= sizeof(rec
),
6669 perf_event_header__init_id(&rec
.header
, &sample
, event
);
6670 ret
= perf_output_begin(&handle
, event
, rec
.header
.size
);
6675 perf_output_put(&handle
, rec
);
6676 perf_event__output_id_sample(event
, &handle
, &sample
);
6678 perf_output_end(&handle
);
6682 * Lost/dropped samples logging
6684 void perf_log_lost_samples(struct perf_event
*event
, u64 lost
)
6686 struct perf_output_handle handle
;
6687 struct perf_sample_data sample
;
6691 struct perf_event_header header
;
6693 } lost_samples_event
= {
6695 .type
= PERF_RECORD_LOST_SAMPLES
,
6697 .size
= sizeof(lost_samples_event
),
6702 perf_event_header__init_id(&lost_samples_event
.header
, &sample
, event
);
6704 ret
= perf_output_begin(&handle
, event
,
6705 lost_samples_event
.header
.size
);
6709 perf_output_put(&handle
, lost_samples_event
);
6710 perf_event__output_id_sample(event
, &handle
, &sample
);
6711 perf_output_end(&handle
);
6715 * context_switch tracking
6718 struct perf_switch_event
{
6719 struct task_struct
*task
;
6720 struct task_struct
*next_prev
;
6723 struct perf_event_header header
;
6729 static int perf_event_switch_match(struct perf_event
*event
)
6731 return event
->attr
.context_switch
;
6734 static void perf_event_switch_output(struct perf_event
*event
, void *data
)
6736 struct perf_switch_event
*se
= data
;
6737 struct perf_output_handle handle
;
6738 struct perf_sample_data sample
;
6741 if (!perf_event_switch_match(event
))
6744 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6745 if (event
->ctx
->task
) {
6746 se
->event_id
.header
.type
= PERF_RECORD_SWITCH
;
6747 se
->event_id
.header
.size
= sizeof(se
->event_id
.header
);
6749 se
->event_id
.header
.type
= PERF_RECORD_SWITCH_CPU_WIDE
;
6750 se
->event_id
.header
.size
= sizeof(se
->event_id
);
6751 se
->event_id
.next_prev_pid
=
6752 perf_event_pid(event
, se
->next_prev
);
6753 se
->event_id
.next_prev_tid
=
6754 perf_event_tid(event
, se
->next_prev
);
6757 perf_event_header__init_id(&se
->event_id
.header
, &sample
, event
);
6759 ret
= perf_output_begin(&handle
, event
, se
->event_id
.header
.size
);
6763 if (event
->ctx
->task
)
6764 perf_output_put(&handle
, se
->event_id
.header
);
6766 perf_output_put(&handle
, se
->event_id
);
6768 perf_event__output_id_sample(event
, &handle
, &sample
);
6770 perf_output_end(&handle
);
6773 static void perf_event_switch(struct task_struct
*task
,
6774 struct task_struct
*next_prev
, bool sched_in
)
6776 struct perf_switch_event switch_event
;
6778 /* N.B. caller checks nr_switch_events != 0 */
6780 switch_event
= (struct perf_switch_event
){
6782 .next_prev
= next_prev
,
6786 .misc
= sched_in
? 0 : PERF_RECORD_MISC_SWITCH_OUT
,
6789 /* .next_prev_pid */
6790 /* .next_prev_tid */
6794 perf_iterate_sb(perf_event_switch_output
,
6800 * IRQ throttle logging
6803 static void perf_log_throttle(struct perf_event
*event
, int enable
)
6805 struct perf_output_handle handle
;
6806 struct perf_sample_data sample
;
6810 struct perf_event_header header
;
6814 } throttle_event
= {
6816 .type
= PERF_RECORD_THROTTLE
,
6818 .size
= sizeof(throttle_event
),
6820 .time
= perf_event_clock(event
),
6821 .id
= primary_event_id(event
),
6822 .stream_id
= event
->id
,
6826 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
6828 perf_event_header__init_id(&throttle_event
.header
, &sample
, event
);
6830 ret
= perf_output_begin(&handle
, event
,
6831 throttle_event
.header
.size
);
6835 perf_output_put(&handle
, throttle_event
);
6836 perf_event__output_id_sample(event
, &handle
, &sample
);
6837 perf_output_end(&handle
);
6840 static void perf_log_itrace_start(struct perf_event
*event
)
6842 struct perf_output_handle handle
;
6843 struct perf_sample_data sample
;
6844 struct perf_aux_event
{
6845 struct perf_event_header header
;
6852 event
= event
->parent
;
6854 if (!(event
->pmu
->capabilities
& PERF_PMU_CAP_ITRACE
) ||
6855 event
->hw
.itrace_started
)
6858 rec
.header
.type
= PERF_RECORD_ITRACE_START
;
6859 rec
.header
.misc
= 0;
6860 rec
.header
.size
= sizeof(rec
);
6861 rec
.pid
= perf_event_pid(event
, current
);
6862 rec
.tid
= perf_event_tid(event
, current
);
6864 perf_event_header__init_id(&rec
.header
, &sample
, event
);
6865 ret
= perf_output_begin(&handle
, event
, rec
.header
.size
);
6870 perf_output_put(&handle
, rec
);
6871 perf_event__output_id_sample(event
, &handle
, &sample
);
6873 perf_output_end(&handle
);
6877 * Generic event overflow handling, sampling.
6880 static int __perf_event_overflow(struct perf_event
*event
,
6881 int throttle
, struct perf_sample_data
*data
,
6882 struct pt_regs
*regs
)
6884 int events
= atomic_read(&event
->event_limit
);
6885 struct hw_perf_event
*hwc
= &event
->hw
;
6890 * Non-sampling counters might still use the PMI to fold short
6891 * hardware counters, ignore those.
6893 if (unlikely(!is_sampling_event(event
)))
6896 seq
= __this_cpu_read(perf_throttled_seq
);
6897 if (seq
!= hwc
->interrupts_seq
) {
6898 hwc
->interrupts_seq
= seq
;
6899 hwc
->interrupts
= 1;
6902 if (unlikely(throttle
6903 && hwc
->interrupts
>= max_samples_per_tick
)) {
6904 __this_cpu_inc(perf_throttled_count
);
6905 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS
);
6906 hwc
->interrupts
= MAX_INTERRUPTS
;
6907 perf_log_throttle(event
, 0);
6912 if (event
->attr
.freq
) {
6913 u64 now
= perf_clock();
6914 s64 delta
= now
- hwc
->freq_time_stamp
;
6916 hwc
->freq_time_stamp
= now
;
6918 if (delta
> 0 && delta
< 2*TICK_NSEC
)
6919 perf_adjust_period(event
, delta
, hwc
->last_period
, true);
6923 * XXX event_limit might not quite work as expected on inherited
6927 event
->pending_kill
= POLL_IN
;
6928 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
6930 event
->pending_kill
= POLL_HUP
;
6931 event
->pending_disable
= 1;
6932 irq_work_queue(&event
->pending
);
6935 event
->overflow_handler(event
, data
, regs
);
6937 if (*perf_event_fasync(event
) && event
->pending_kill
) {
6938 event
->pending_wakeup
= 1;
6939 irq_work_queue(&event
->pending
);
6945 int perf_event_overflow(struct perf_event
*event
,
6946 struct perf_sample_data
*data
,
6947 struct pt_regs
*regs
)
6949 return __perf_event_overflow(event
, 1, data
, regs
);
6953 * Generic software event infrastructure
6956 struct swevent_htable
{
6957 struct swevent_hlist
*swevent_hlist
;
6958 struct mutex hlist_mutex
;
6961 /* Recursion avoidance in each contexts */
6962 int recursion
[PERF_NR_CONTEXTS
];
6965 static DEFINE_PER_CPU(struct swevent_htable
, swevent_htable
);
6968 * We directly increment event->count and keep a second value in
6969 * event->hw.period_left to count intervals. This period event
6970 * is kept in the range [-sample_period, 0] so that we can use the
6974 u64
perf_swevent_set_period(struct perf_event
*event
)
6976 struct hw_perf_event
*hwc
= &event
->hw
;
6977 u64 period
= hwc
->last_period
;
6981 hwc
->last_period
= hwc
->sample_period
;
6984 old
= val
= local64_read(&hwc
->period_left
);
6988 nr
= div64_u64(period
+ val
, period
);
6989 offset
= nr
* period
;
6991 if (local64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
6997 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
6998 struct perf_sample_data
*data
,
6999 struct pt_regs
*regs
)
7001 struct hw_perf_event
*hwc
= &event
->hw
;
7005 overflow
= perf_swevent_set_period(event
);
7007 if (hwc
->interrupts
== MAX_INTERRUPTS
)
7010 for (; overflow
; overflow
--) {
7011 if (__perf_event_overflow(event
, throttle
,
7014 * We inhibit the overflow from happening when
7015 * hwc->interrupts == MAX_INTERRUPTS.
7023 static void perf_swevent_event(struct perf_event
*event
, u64 nr
,
7024 struct perf_sample_data
*data
,
7025 struct pt_regs
*regs
)
7027 struct hw_perf_event
*hwc
= &event
->hw
;
7029 local64_add(nr
, &event
->count
);
7034 if (!is_sampling_event(event
))
7037 if ((event
->attr
.sample_type
& PERF_SAMPLE_PERIOD
) && !event
->attr
.freq
) {
7039 return perf_swevent_overflow(event
, 1, data
, regs
);
7041 data
->period
= event
->hw
.last_period
;
7043 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
7044 return perf_swevent_overflow(event
, 1, data
, regs
);
7046 if (local64_add_negative(nr
, &hwc
->period_left
))
7049 perf_swevent_overflow(event
, 0, data
, regs
);
7052 static int perf_exclude_event(struct perf_event
*event
,
7053 struct pt_regs
*regs
)
7055 if (event
->hw
.state
& PERF_HES_STOPPED
)
7059 if (event
->attr
.exclude_user
&& user_mode(regs
))
7062 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
7069 static int perf_swevent_match(struct perf_event
*event
,
7070 enum perf_type_id type
,
7072 struct perf_sample_data
*data
,
7073 struct pt_regs
*regs
)
7075 if (event
->attr
.type
!= type
)
7078 if (event
->attr
.config
!= event_id
)
7081 if (perf_exclude_event(event
, regs
))
7087 static inline u64
swevent_hash(u64 type
, u32 event_id
)
7089 u64 val
= event_id
| (type
<< 32);
7091 return hash_64(val
, SWEVENT_HLIST_BITS
);
7094 static inline struct hlist_head
*
7095 __find_swevent_head(struct swevent_hlist
*hlist
, u64 type
, u32 event_id
)
7097 u64 hash
= swevent_hash(type
, event_id
);
7099 return &hlist
->heads
[hash
];
7102 /* For the read side: events when they trigger */
7103 static inline struct hlist_head
*
7104 find_swevent_head_rcu(struct swevent_htable
*swhash
, u64 type
, u32 event_id
)
7106 struct swevent_hlist
*hlist
;
7108 hlist
= rcu_dereference(swhash
->swevent_hlist
);
7112 return __find_swevent_head(hlist
, type
, event_id
);
7115 /* For the event head insertion and removal in the hlist */
7116 static inline struct hlist_head
*
7117 find_swevent_head(struct swevent_htable
*swhash
, struct perf_event
*event
)
7119 struct swevent_hlist
*hlist
;
7120 u32 event_id
= event
->attr
.config
;
7121 u64 type
= event
->attr
.type
;
7124 * Event scheduling is always serialized against hlist allocation
7125 * and release. Which makes the protected version suitable here.
7126 * The context lock guarantees that.
7128 hlist
= rcu_dereference_protected(swhash
->swevent_hlist
,
7129 lockdep_is_held(&event
->ctx
->lock
));
7133 return __find_swevent_head(hlist
, type
, event_id
);
7136 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
7138 struct perf_sample_data
*data
,
7139 struct pt_regs
*regs
)
7141 struct swevent_htable
*swhash
= this_cpu_ptr(&swevent_htable
);
7142 struct perf_event
*event
;
7143 struct hlist_head
*head
;
7146 head
= find_swevent_head_rcu(swhash
, type
, event_id
);
7150 hlist_for_each_entry_rcu(event
, head
, hlist_entry
) {
7151 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
7152 perf_swevent_event(event
, nr
, data
, regs
);
7158 DEFINE_PER_CPU(struct pt_regs
, __perf_regs
[4]);
7160 int perf_swevent_get_recursion_context(void)
7162 struct swevent_htable
*swhash
= this_cpu_ptr(&swevent_htable
);
7164 return get_recursion_context(swhash
->recursion
);
7166 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
7168 void perf_swevent_put_recursion_context(int rctx
)
7170 struct swevent_htable
*swhash
= this_cpu_ptr(&swevent_htable
);
7172 put_recursion_context(swhash
->recursion
, rctx
);
7175 void ___perf_sw_event(u32 event_id
, u64 nr
, struct pt_regs
*regs
, u64 addr
)
7177 struct perf_sample_data data
;
7179 if (WARN_ON_ONCE(!regs
))
7182 perf_sample_data_init(&data
, addr
, 0);
7183 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, &data
, regs
);
7186 void __perf_sw_event(u32 event_id
, u64 nr
, struct pt_regs
*regs
, u64 addr
)
7190 preempt_disable_notrace();
7191 rctx
= perf_swevent_get_recursion_context();
7192 if (unlikely(rctx
< 0))
7195 ___perf_sw_event(event_id
, nr
, regs
, addr
);
7197 perf_swevent_put_recursion_context(rctx
);
7199 preempt_enable_notrace();
7202 static void perf_swevent_read(struct perf_event
*event
)
7206 static int perf_swevent_add(struct perf_event
*event
, int flags
)
7208 struct swevent_htable
*swhash
= this_cpu_ptr(&swevent_htable
);
7209 struct hw_perf_event
*hwc
= &event
->hw
;
7210 struct hlist_head
*head
;
7212 if (is_sampling_event(event
)) {
7213 hwc
->last_period
= hwc
->sample_period
;
7214 perf_swevent_set_period(event
);
7217 hwc
->state
= !(flags
& PERF_EF_START
);
7219 head
= find_swevent_head(swhash
, event
);
7220 if (WARN_ON_ONCE(!head
))
7223 hlist_add_head_rcu(&event
->hlist_entry
, head
);
7224 perf_event_update_userpage(event
);
7229 static void perf_swevent_del(struct perf_event
*event
, int flags
)
7231 hlist_del_rcu(&event
->hlist_entry
);
7234 static void perf_swevent_start(struct perf_event
*event
, int flags
)
7236 event
->hw
.state
= 0;
7239 static void perf_swevent_stop(struct perf_event
*event
, int flags
)
7241 event
->hw
.state
= PERF_HES_STOPPED
;
7244 /* Deref the hlist from the update side */
7245 static inline struct swevent_hlist
*
7246 swevent_hlist_deref(struct swevent_htable
*swhash
)
7248 return rcu_dereference_protected(swhash
->swevent_hlist
,
7249 lockdep_is_held(&swhash
->hlist_mutex
));
7252 static void swevent_hlist_release(struct swevent_htable
*swhash
)
7254 struct swevent_hlist
*hlist
= swevent_hlist_deref(swhash
);
7259 RCU_INIT_POINTER(swhash
->swevent_hlist
, NULL
);
7260 kfree_rcu(hlist
, rcu_head
);
7263 static void swevent_hlist_put_cpu(int cpu
)
7265 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
7267 mutex_lock(&swhash
->hlist_mutex
);
7269 if (!--swhash
->hlist_refcount
)
7270 swevent_hlist_release(swhash
);
7272 mutex_unlock(&swhash
->hlist_mutex
);
7275 static void swevent_hlist_put(void)
7279 for_each_possible_cpu(cpu
)
7280 swevent_hlist_put_cpu(cpu
);
7283 static int swevent_hlist_get_cpu(int cpu
)
7285 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
7288 mutex_lock(&swhash
->hlist_mutex
);
7289 if (!swevent_hlist_deref(swhash
) && cpu_online(cpu
)) {
7290 struct swevent_hlist
*hlist
;
7292 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
7297 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
7299 swhash
->hlist_refcount
++;
7301 mutex_unlock(&swhash
->hlist_mutex
);
7306 static int swevent_hlist_get(void)
7308 int err
, cpu
, failed_cpu
;
7311 for_each_possible_cpu(cpu
) {
7312 err
= swevent_hlist_get_cpu(cpu
);
7322 for_each_possible_cpu(cpu
) {
7323 if (cpu
== failed_cpu
)
7325 swevent_hlist_put_cpu(cpu
);
7332 struct static_key perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
7334 static void sw_perf_event_destroy(struct perf_event
*event
)
7336 u64 event_id
= event
->attr
.config
;
7338 WARN_ON(event
->parent
);
7340 static_key_slow_dec(&perf_swevent_enabled
[event_id
]);
7341 swevent_hlist_put();
7344 static int perf_swevent_init(struct perf_event
*event
)
7346 u64 event_id
= event
->attr
.config
;
7348 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
7352 * no branch sampling for software events
7354 if (has_branch_stack(event
))
7358 case PERF_COUNT_SW_CPU_CLOCK
:
7359 case PERF_COUNT_SW_TASK_CLOCK
:
7366 if (event_id
>= PERF_COUNT_SW_MAX
)
7369 if (!event
->parent
) {
7372 err
= swevent_hlist_get();
7376 static_key_slow_inc(&perf_swevent_enabled
[event_id
]);
7377 event
->destroy
= sw_perf_event_destroy
;
7383 static struct pmu perf_swevent
= {
7384 .task_ctx_nr
= perf_sw_context
,
7386 .capabilities
= PERF_PMU_CAP_NO_NMI
,
7388 .event_init
= perf_swevent_init
,
7389 .add
= perf_swevent_add
,
7390 .del
= perf_swevent_del
,
7391 .start
= perf_swevent_start
,
7392 .stop
= perf_swevent_stop
,
7393 .read
= perf_swevent_read
,
7396 #ifdef CONFIG_EVENT_TRACING
7398 static int perf_tp_filter_match(struct perf_event
*event
,
7399 struct perf_sample_data
*data
)
7401 void *record
= data
->raw
->data
;
7403 /* only top level events have filters set */
7405 event
= event
->parent
;
7407 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
7412 static int perf_tp_event_match(struct perf_event
*event
,
7413 struct perf_sample_data
*data
,
7414 struct pt_regs
*regs
)
7416 if (event
->hw
.state
& PERF_HES_STOPPED
)
7419 * All tracepoints are from kernel-space.
7421 if (event
->attr
.exclude_kernel
)
7424 if (!perf_tp_filter_match(event
, data
))
7430 void perf_trace_run_bpf_submit(void *raw_data
, int size
, int rctx
,
7431 struct trace_event_call
*call
, u64 count
,
7432 struct pt_regs
*regs
, struct hlist_head
*head
,
7433 struct task_struct
*task
)
7435 struct bpf_prog
*prog
= call
->prog
;
7438 *(struct pt_regs
**)raw_data
= regs
;
7439 if (!trace_call_bpf(prog
, raw_data
) || hlist_empty(head
)) {
7440 perf_swevent_put_recursion_context(rctx
);
7444 perf_tp_event(call
->event
.type
, count
, raw_data
, size
, regs
, head
,
7447 EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit
);
7449 void perf_tp_event(u16 event_type
, u64 count
, void *record
, int entry_size
,
7450 struct pt_regs
*regs
, struct hlist_head
*head
, int rctx
,
7451 struct task_struct
*task
)
7453 struct perf_sample_data data
;
7454 struct perf_event
*event
;
7456 struct perf_raw_record raw
= {
7461 perf_sample_data_init(&data
, 0, 0);
7464 perf_trace_buf_update(record
, event_type
);
7466 hlist_for_each_entry_rcu(event
, head
, hlist_entry
) {
7467 if (perf_tp_event_match(event
, &data
, regs
))
7468 perf_swevent_event(event
, count
, &data
, regs
);
7472 * If we got specified a target task, also iterate its context and
7473 * deliver this event there too.
7475 if (task
&& task
!= current
) {
7476 struct perf_event_context
*ctx
;
7477 struct trace_entry
*entry
= record
;
7480 ctx
= rcu_dereference(task
->perf_event_ctxp
[perf_sw_context
]);
7484 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
7485 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
7487 if (event
->attr
.config
!= entry
->type
)
7489 if (perf_tp_event_match(event
, &data
, regs
))
7490 perf_swevent_event(event
, count
, &data
, regs
);
7496 perf_swevent_put_recursion_context(rctx
);
7498 EXPORT_SYMBOL_GPL(perf_tp_event
);
7500 static void tp_perf_event_destroy(struct perf_event
*event
)
7502 perf_trace_destroy(event
);
7505 static int perf_tp_event_init(struct perf_event
*event
)
7509 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
7513 * no branch sampling for tracepoint events
7515 if (has_branch_stack(event
))
7518 err
= perf_trace_init(event
);
7522 event
->destroy
= tp_perf_event_destroy
;
7527 static struct pmu perf_tracepoint
= {
7528 .task_ctx_nr
= perf_sw_context
,
7530 .event_init
= perf_tp_event_init
,
7531 .add
= perf_trace_add
,
7532 .del
= perf_trace_del
,
7533 .start
= perf_swevent_start
,
7534 .stop
= perf_swevent_stop
,
7535 .read
= perf_swevent_read
,
7538 static inline void perf_tp_register(void)
7540 perf_pmu_register(&perf_tracepoint
, "tracepoint", PERF_TYPE_TRACEPOINT
);
7543 static void perf_event_free_filter(struct perf_event
*event
)
7545 ftrace_profile_free_filter(event
);
7548 static int perf_event_set_bpf_prog(struct perf_event
*event
, u32 prog_fd
)
7550 bool is_kprobe
, is_tracepoint
;
7551 struct bpf_prog
*prog
;
7553 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
7556 if (event
->tp_event
->prog
)
7559 is_kprobe
= event
->tp_event
->flags
& TRACE_EVENT_FL_UKPROBE
;
7560 is_tracepoint
= event
->tp_event
->flags
& TRACE_EVENT_FL_TRACEPOINT
;
7561 if (!is_kprobe
&& !is_tracepoint
)
7562 /* bpf programs can only be attached to u/kprobe or tracepoint */
7565 prog
= bpf_prog_get(prog_fd
);
7567 return PTR_ERR(prog
);
7569 if ((is_kprobe
&& prog
->type
!= BPF_PROG_TYPE_KPROBE
) ||
7570 (is_tracepoint
&& prog
->type
!= BPF_PROG_TYPE_TRACEPOINT
)) {
7571 /* valid fd, but invalid bpf program type */
7576 if (is_tracepoint
) {
7577 int off
= trace_event_get_offsets(event
->tp_event
);
7579 if (prog
->aux
->max_ctx_offset
> off
) {
7584 event
->tp_event
->prog
= prog
;
7589 static void perf_event_free_bpf_prog(struct perf_event
*event
)
7591 struct bpf_prog
*prog
;
7593 if (!event
->tp_event
)
7596 prog
= event
->tp_event
->prog
;
7598 event
->tp_event
->prog
= NULL
;
7599 bpf_prog_put_rcu(prog
);
7605 static inline void perf_tp_register(void)
7609 static void perf_event_free_filter(struct perf_event
*event
)
7613 static int perf_event_set_bpf_prog(struct perf_event
*event
, u32 prog_fd
)
7618 static void perf_event_free_bpf_prog(struct perf_event
*event
)
7621 #endif /* CONFIG_EVENT_TRACING */
7623 #ifdef CONFIG_HAVE_HW_BREAKPOINT
7624 void perf_bp_event(struct perf_event
*bp
, void *data
)
7626 struct perf_sample_data sample
;
7627 struct pt_regs
*regs
= data
;
7629 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
, 0);
7631 if (!bp
->hw
.state
&& !perf_exclude_event(bp
, regs
))
7632 perf_swevent_event(bp
, 1, &sample
, regs
);
7637 * Allocate a new address filter
7639 static struct perf_addr_filter
*
7640 perf_addr_filter_new(struct perf_event
*event
, struct list_head
*filters
)
7642 int node
= cpu_to_node(event
->cpu
== -1 ? 0 : event
->cpu
);
7643 struct perf_addr_filter
*filter
;
7645 filter
= kzalloc_node(sizeof(*filter
), GFP_KERNEL
, node
);
7649 INIT_LIST_HEAD(&filter
->entry
);
7650 list_add_tail(&filter
->entry
, filters
);
7655 static void free_filters_list(struct list_head
*filters
)
7657 struct perf_addr_filter
*filter
, *iter
;
7659 list_for_each_entry_safe(filter
, iter
, filters
, entry
) {
7661 iput(filter
->inode
);
7662 list_del(&filter
->entry
);
7668 * Free existing address filters and optionally install new ones
7670 static void perf_addr_filters_splice(struct perf_event
*event
,
7671 struct list_head
*head
)
7673 unsigned long flags
;
7676 if (!has_addr_filter(event
))
7679 /* don't bother with children, they don't have their own filters */
7683 raw_spin_lock_irqsave(&event
->addr_filters
.lock
, flags
);
7685 list_splice_init(&event
->addr_filters
.list
, &list
);
7687 list_splice(head
, &event
->addr_filters
.list
);
7689 raw_spin_unlock_irqrestore(&event
->addr_filters
.lock
, flags
);
7691 free_filters_list(&list
);
7695 * Scan through mm's vmas and see if one of them matches the
7696 * @filter; if so, adjust filter's address range.
7697 * Called with mm::mmap_sem down for reading.
7699 static unsigned long perf_addr_filter_apply(struct perf_addr_filter
*filter
,
7700 struct mm_struct
*mm
)
7702 struct vm_area_struct
*vma
;
7704 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
7705 struct file
*file
= vma
->vm_file
;
7706 unsigned long off
= vma
->vm_pgoff
<< PAGE_SHIFT
;
7707 unsigned long vma_size
= vma
->vm_end
- vma
->vm_start
;
7712 if (!perf_addr_filter_match(filter
, file
, off
, vma_size
))
7715 return vma
->vm_start
;
7722 * Update event's address range filters based on the
7723 * task's existing mappings, if any.
7725 static void perf_event_addr_filters_apply(struct perf_event
*event
)
7727 struct perf_addr_filters_head
*ifh
= perf_event_addr_filters(event
);
7728 struct task_struct
*task
= READ_ONCE(event
->ctx
->task
);
7729 struct perf_addr_filter
*filter
;
7730 struct mm_struct
*mm
= NULL
;
7731 unsigned int count
= 0;
7732 unsigned long flags
;
7735 * We may observe TASK_TOMBSTONE, which means that the event tear-down
7736 * will stop on the parent's child_mutex that our caller is also holding
7738 if (task
== TASK_TOMBSTONE
)
7741 mm
= get_task_mm(event
->ctx
->task
);
7745 down_read(&mm
->mmap_sem
);
7747 raw_spin_lock_irqsave(&ifh
->lock
, flags
);
7748 list_for_each_entry(filter
, &ifh
->list
, entry
) {
7749 event
->addr_filters_offs
[count
] = 0;
7751 if (perf_addr_filter_needs_mmap(filter
))
7752 event
->addr_filters_offs
[count
] =
7753 perf_addr_filter_apply(filter
, mm
);
7758 event
->addr_filters_gen
++;
7759 raw_spin_unlock_irqrestore(&ifh
->lock
, flags
);
7761 up_read(&mm
->mmap_sem
);
7766 perf_event_restart(event
);
7770 * Address range filtering: limiting the data to certain
7771 * instruction address ranges. Filters are ioctl()ed to us from
7772 * userspace as ascii strings.
7774 * Filter string format:
7777 * where ACTION is one of the
7778 * * "filter": limit the trace to this region
7779 * * "start": start tracing from this address
7780 * * "stop": stop tracing at this address/region;
7782 * * for kernel addresses: <start address>[/<size>]
7783 * * for object files: <start address>[/<size>]@</path/to/object/file>
7785 * if <size> is not specified, the range is treated as a single address.
7798 IF_STATE_ACTION
= 0,
7803 static const match_table_t if_tokens
= {
7804 { IF_ACT_FILTER
, "filter" },
7805 { IF_ACT_START
, "start" },
7806 { IF_ACT_STOP
, "stop" },
7807 { IF_SRC_FILE
, "%u/%u@%s" },
7808 { IF_SRC_KERNEL
, "%u/%u" },
7809 { IF_SRC_FILEADDR
, "%u@%s" },
7810 { IF_SRC_KERNELADDR
, "%u" },
7814 * Address filter string parser
7817 perf_event_parse_addr_filter(struct perf_event
*event
, char *fstr
,
7818 struct list_head
*filters
)
7820 struct perf_addr_filter
*filter
= NULL
;
7821 char *start
, *orig
, *filename
= NULL
;
7823 substring_t args
[MAX_OPT_ARGS
];
7824 int state
= IF_STATE_ACTION
, token
;
7825 unsigned int kernel
= 0;
7828 orig
= fstr
= kstrdup(fstr
, GFP_KERNEL
);
7832 while ((start
= strsep(&fstr
, " ,\n")) != NULL
) {
7838 /* filter definition begins */
7839 if (state
== IF_STATE_ACTION
) {
7840 filter
= perf_addr_filter_new(event
, filters
);
7845 token
= match_token(start
, if_tokens
, args
);
7852 if (state
!= IF_STATE_ACTION
)
7855 state
= IF_STATE_SOURCE
;
7858 case IF_SRC_KERNELADDR
:
7862 case IF_SRC_FILEADDR
:
7864 if (state
!= IF_STATE_SOURCE
)
7867 if (token
== IF_SRC_FILE
|| token
== IF_SRC_KERNEL
)
7871 ret
= kstrtoul(args
[0].from
, 0, &filter
->offset
);
7875 if (filter
->range
) {
7877 ret
= kstrtoul(args
[1].from
, 0, &filter
->size
);
7882 if (token
== IF_SRC_FILE
) {
7883 filename
= match_strdup(&args
[2]);
7890 state
= IF_STATE_END
;
7898 * Filter definition is fully parsed, validate and install it.
7899 * Make sure that it doesn't contradict itself or the event's
7902 if (state
== IF_STATE_END
) {
7903 if (kernel
&& event
->attr
.exclude_kernel
)
7910 /* look up the path and grab its inode */
7911 ret
= kern_path(filename
, LOOKUP_FOLLOW
, &path
);
7913 goto fail_free_name
;
7915 filter
->inode
= igrab(d_inode(path
.dentry
));
7921 if (!filter
->inode
||
7922 !S_ISREG(filter
->inode
->i_mode
))
7923 /* free_filters_list() will iput() */
7927 /* ready to consume more filters */
7928 state
= IF_STATE_ACTION
;
7933 if (state
!= IF_STATE_ACTION
)
7943 free_filters_list(filters
);
7950 perf_event_set_addr_filter(struct perf_event
*event
, char *filter_str
)
7956 * Since this is called in perf_ioctl() path, we're already holding
7959 lockdep_assert_held(&event
->ctx
->mutex
);
7961 if (WARN_ON_ONCE(event
->parent
))
7965 * For now, we only support filtering in per-task events; doing so
7966 * for CPU-wide events requires additional context switching trickery,
7967 * since same object code will be mapped at different virtual
7968 * addresses in different processes.
7970 if (!event
->ctx
->task
)
7973 ret
= perf_event_parse_addr_filter(event
, filter_str
, &filters
);
7977 ret
= event
->pmu
->addr_filters_validate(&filters
);
7979 free_filters_list(&filters
);
7983 /* remove existing filters, if any */
7984 perf_addr_filters_splice(event
, &filters
);
7986 /* install new filters */
7987 perf_event_for_each_child(event
, perf_event_addr_filters_apply
);
7992 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
7997 if ((event
->attr
.type
!= PERF_TYPE_TRACEPOINT
||
7998 !IS_ENABLED(CONFIG_EVENT_TRACING
)) &&
7999 !has_addr_filter(event
))
8002 filter_str
= strndup_user(arg
, PAGE_SIZE
);
8003 if (IS_ERR(filter_str
))
8004 return PTR_ERR(filter_str
);
8006 if (IS_ENABLED(CONFIG_EVENT_TRACING
) &&
8007 event
->attr
.type
== PERF_TYPE_TRACEPOINT
)
8008 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
,
8010 else if (has_addr_filter(event
))
8011 ret
= perf_event_set_addr_filter(event
, filter_str
);
8018 * hrtimer based swevent callback
8021 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
8023 enum hrtimer_restart ret
= HRTIMER_RESTART
;
8024 struct perf_sample_data data
;
8025 struct pt_regs
*regs
;
8026 struct perf_event
*event
;
8029 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
8031 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
8032 return HRTIMER_NORESTART
;
8034 event
->pmu
->read(event
);
8036 perf_sample_data_init(&data
, 0, event
->hw
.last_period
);
8037 regs
= get_irq_regs();
8039 if (regs
&& !perf_exclude_event(event
, regs
)) {
8040 if (!(event
->attr
.exclude_idle
&& is_idle_task(current
)))
8041 if (__perf_event_overflow(event
, 1, &data
, regs
))
8042 ret
= HRTIMER_NORESTART
;
8045 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
8046 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
8051 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
8053 struct hw_perf_event
*hwc
= &event
->hw
;
8056 if (!is_sampling_event(event
))
8059 period
= local64_read(&hwc
->period_left
);
8064 local64_set(&hwc
->period_left
, 0);
8066 period
= max_t(u64
, 10000, hwc
->sample_period
);
8068 hrtimer_start(&hwc
->hrtimer
, ns_to_ktime(period
),
8069 HRTIMER_MODE_REL_PINNED
);
8072 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
8074 struct hw_perf_event
*hwc
= &event
->hw
;
8076 if (is_sampling_event(event
)) {
8077 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
8078 local64_set(&hwc
->period_left
, ktime_to_ns(remaining
));
8080 hrtimer_cancel(&hwc
->hrtimer
);
8084 static void perf_swevent_init_hrtimer(struct perf_event
*event
)
8086 struct hw_perf_event
*hwc
= &event
->hw
;
8088 if (!is_sampling_event(event
))
8091 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
8092 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
8095 * Since hrtimers have a fixed rate, we can do a static freq->period
8096 * mapping and avoid the whole period adjust feedback stuff.
8098 if (event
->attr
.freq
) {
8099 long freq
= event
->attr
.sample_freq
;
8101 event
->attr
.sample_period
= NSEC_PER_SEC
/ freq
;
8102 hwc
->sample_period
= event
->attr
.sample_period
;
8103 local64_set(&hwc
->period_left
, hwc
->sample_period
);
8104 hwc
->last_period
= hwc
->sample_period
;
8105 event
->attr
.freq
= 0;
8110 * Software event: cpu wall time clock
8113 static void cpu_clock_event_update(struct perf_event
*event
)
8118 now
= local_clock();
8119 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
8120 local64_add(now
- prev
, &event
->count
);
8123 static void cpu_clock_event_start(struct perf_event
*event
, int flags
)
8125 local64_set(&event
->hw
.prev_count
, local_clock());
8126 perf_swevent_start_hrtimer(event
);
8129 static void cpu_clock_event_stop(struct perf_event
*event
, int flags
)
8131 perf_swevent_cancel_hrtimer(event
);
8132 cpu_clock_event_update(event
);
8135 static int cpu_clock_event_add(struct perf_event
*event
, int flags
)
8137 if (flags
& PERF_EF_START
)
8138 cpu_clock_event_start(event
, flags
);
8139 perf_event_update_userpage(event
);
8144 static void cpu_clock_event_del(struct perf_event
*event
, int flags
)
8146 cpu_clock_event_stop(event
, flags
);
8149 static void cpu_clock_event_read(struct perf_event
*event
)
8151 cpu_clock_event_update(event
);
8154 static int cpu_clock_event_init(struct perf_event
*event
)
8156 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
8159 if (event
->attr
.config
!= PERF_COUNT_SW_CPU_CLOCK
)
8163 * no branch sampling for software events
8165 if (has_branch_stack(event
))
8168 perf_swevent_init_hrtimer(event
);
8173 static struct pmu perf_cpu_clock
= {
8174 .task_ctx_nr
= perf_sw_context
,
8176 .capabilities
= PERF_PMU_CAP_NO_NMI
,
8178 .event_init
= cpu_clock_event_init
,
8179 .add
= cpu_clock_event_add
,
8180 .del
= cpu_clock_event_del
,
8181 .start
= cpu_clock_event_start
,
8182 .stop
= cpu_clock_event_stop
,
8183 .read
= cpu_clock_event_read
,
8187 * Software event: task time clock
8190 static void task_clock_event_update(struct perf_event
*event
, u64 now
)
8195 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
8197 local64_add(delta
, &event
->count
);
8200 static void task_clock_event_start(struct perf_event
*event
, int flags
)
8202 local64_set(&event
->hw
.prev_count
, event
->ctx
->time
);
8203 perf_swevent_start_hrtimer(event
);
8206 static void task_clock_event_stop(struct perf_event
*event
, int flags
)
8208 perf_swevent_cancel_hrtimer(event
);
8209 task_clock_event_update(event
, event
->ctx
->time
);
8212 static int task_clock_event_add(struct perf_event
*event
, int flags
)
8214 if (flags
& PERF_EF_START
)
8215 task_clock_event_start(event
, flags
);
8216 perf_event_update_userpage(event
);
8221 static void task_clock_event_del(struct perf_event
*event
, int flags
)
8223 task_clock_event_stop(event
, PERF_EF_UPDATE
);
8226 static void task_clock_event_read(struct perf_event
*event
)
8228 u64 now
= perf_clock();
8229 u64 delta
= now
- event
->ctx
->timestamp
;
8230 u64 time
= event
->ctx
->time
+ delta
;
8232 task_clock_event_update(event
, time
);
8235 static int task_clock_event_init(struct perf_event
*event
)
8237 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
8240 if (event
->attr
.config
!= PERF_COUNT_SW_TASK_CLOCK
)
8244 * no branch sampling for software events
8246 if (has_branch_stack(event
))
8249 perf_swevent_init_hrtimer(event
);
8254 static struct pmu perf_task_clock
= {
8255 .task_ctx_nr
= perf_sw_context
,
8257 .capabilities
= PERF_PMU_CAP_NO_NMI
,
8259 .event_init
= task_clock_event_init
,
8260 .add
= task_clock_event_add
,
8261 .del
= task_clock_event_del
,
8262 .start
= task_clock_event_start
,
8263 .stop
= task_clock_event_stop
,
8264 .read
= task_clock_event_read
,
8267 static void perf_pmu_nop_void(struct pmu
*pmu
)
8271 static void perf_pmu_nop_txn(struct pmu
*pmu
, unsigned int flags
)
8275 static int perf_pmu_nop_int(struct pmu
*pmu
)
8280 static DEFINE_PER_CPU(unsigned int, nop_txn_flags
);
8282 static void perf_pmu_start_txn(struct pmu
*pmu
, unsigned int flags
)
8284 __this_cpu_write(nop_txn_flags
, flags
);
8286 if (flags
& ~PERF_PMU_TXN_ADD
)
8289 perf_pmu_disable(pmu
);
8292 static int perf_pmu_commit_txn(struct pmu
*pmu
)
8294 unsigned int flags
= __this_cpu_read(nop_txn_flags
);
8296 __this_cpu_write(nop_txn_flags
, 0);
8298 if (flags
& ~PERF_PMU_TXN_ADD
)
8301 perf_pmu_enable(pmu
);
8305 static void perf_pmu_cancel_txn(struct pmu
*pmu
)
8307 unsigned int flags
= __this_cpu_read(nop_txn_flags
);
8309 __this_cpu_write(nop_txn_flags
, 0);
8311 if (flags
& ~PERF_PMU_TXN_ADD
)
8314 perf_pmu_enable(pmu
);
8317 static int perf_event_idx_default(struct perf_event
*event
)
8323 * Ensures all contexts with the same task_ctx_nr have the same
8324 * pmu_cpu_context too.
8326 static struct perf_cpu_context __percpu
*find_pmu_context(int ctxn
)
8333 list_for_each_entry(pmu
, &pmus
, entry
) {
8334 if (pmu
->task_ctx_nr
== ctxn
)
8335 return pmu
->pmu_cpu_context
;
8341 static void update_pmu_context(struct pmu
*pmu
, struct pmu
*old_pmu
)
8345 for_each_possible_cpu(cpu
) {
8346 struct perf_cpu_context
*cpuctx
;
8348 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
8350 if (cpuctx
->unique_pmu
== old_pmu
)
8351 cpuctx
->unique_pmu
= pmu
;
8355 static void free_pmu_context(struct pmu
*pmu
)
8359 mutex_lock(&pmus_lock
);
8361 * Like a real lame refcount.
8363 list_for_each_entry(i
, &pmus
, entry
) {
8364 if (i
->pmu_cpu_context
== pmu
->pmu_cpu_context
) {
8365 update_pmu_context(i
, pmu
);
8370 free_percpu(pmu
->pmu_cpu_context
);
8372 mutex_unlock(&pmus_lock
);
8376 * Let userspace know that this PMU supports address range filtering:
8378 static ssize_t
nr_addr_filters_show(struct device
*dev
,
8379 struct device_attribute
*attr
,
8382 struct pmu
*pmu
= dev_get_drvdata(dev
);
8384 return snprintf(page
, PAGE_SIZE
- 1, "%d\n", pmu
->nr_addr_filters
);
8386 DEVICE_ATTR_RO(nr_addr_filters
);
8388 static struct idr pmu_idr
;
8391 type_show(struct device
*dev
, struct device_attribute
*attr
, char *page
)
8393 struct pmu
*pmu
= dev_get_drvdata(dev
);
8395 return snprintf(page
, PAGE_SIZE
-1, "%d\n", pmu
->type
);
8397 static DEVICE_ATTR_RO(type
);
8400 perf_event_mux_interval_ms_show(struct device
*dev
,
8401 struct device_attribute
*attr
,
8404 struct pmu
*pmu
= dev_get_drvdata(dev
);
8406 return snprintf(page
, PAGE_SIZE
-1, "%d\n", pmu
->hrtimer_interval_ms
);
8409 static DEFINE_MUTEX(mux_interval_mutex
);
8412 perf_event_mux_interval_ms_store(struct device
*dev
,
8413 struct device_attribute
*attr
,
8414 const char *buf
, size_t count
)
8416 struct pmu
*pmu
= dev_get_drvdata(dev
);
8417 int timer
, cpu
, ret
;
8419 ret
= kstrtoint(buf
, 0, &timer
);
8426 /* same value, noting to do */
8427 if (timer
== pmu
->hrtimer_interval_ms
)
8430 mutex_lock(&mux_interval_mutex
);
8431 pmu
->hrtimer_interval_ms
= timer
;
8433 /* update all cpuctx for this PMU */
8435 for_each_online_cpu(cpu
) {
8436 struct perf_cpu_context
*cpuctx
;
8437 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
8438 cpuctx
->hrtimer_interval
= ns_to_ktime(NSEC_PER_MSEC
* timer
);
8440 cpu_function_call(cpu
,
8441 (remote_function_f
)perf_mux_hrtimer_restart
, cpuctx
);
8444 mutex_unlock(&mux_interval_mutex
);
8448 static DEVICE_ATTR_RW(perf_event_mux_interval_ms
);
8450 static struct attribute
*pmu_dev_attrs
[] = {
8451 &dev_attr_type
.attr
,
8452 &dev_attr_perf_event_mux_interval_ms
.attr
,
8455 ATTRIBUTE_GROUPS(pmu_dev
);
8457 static int pmu_bus_running
;
8458 static struct bus_type pmu_bus
= {
8459 .name
= "event_source",
8460 .dev_groups
= pmu_dev_groups
,
8463 static void pmu_dev_release(struct device
*dev
)
8468 static int pmu_dev_alloc(struct pmu
*pmu
)
8472 pmu
->dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
8476 pmu
->dev
->groups
= pmu
->attr_groups
;
8477 device_initialize(pmu
->dev
);
8478 ret
= dev_set_name(pmu
->dev
, "%s", pmu
->name
);
8482 dev_set_drvdata(pmu
->dev
, pmu
);
8483 pmu
->dev
->bus
= &pmu_bus
;
8484 pmu
->dev
->release
= pmu_dev_release
;
8485 ret
= device_add(pmu
->dev
);
8489 /* For PMUs with address filters, throw in an extra attribute: */
8490 if (pmu
->nr_addr_filters
)
8491 ret
= device_create_file(pmu
->dev
, &dev_attr_nr_addr_filters
);
8500 device_del(pmu
->dev
);
8503 put_device(pmu
->dev
);
8507 static struct lock_class_key cpuctx_mutex
;
8508 static struct lock_class_key cpuctx_lock
;
8510 int perf_pmu_register(struct pmu
*pmu
, const char *name
, int type
)
8514 mutex_lock(&pmus_lock
);
8516 pmu
->pmu_disable_count
= alloc_percpu(int);
8517 if (!pmu
->pmu_disable_count
)
8526 type
= idr_alloc(&pmu_idr
, pmu
, PERF_TYPE_MAX
, 0, GFP_KERNEL
);
8534 if (pmu_bus_running
) {
8535 ret
= pmu_dev_alloc(pmu
);
8541 if (pmu
->task_ctx_nr
== perf_hw_context
) {
8542 static int hw_context_taken
= 0;
8545 * Other than systems with heterogeneous CPUs, it never makes
8546 * sense for two PMUs to share perf_hw_context. PMUs which are
8547 * uncore must use perf_invalid_context.
8549 if (WARN_ON_ONCE(hw_context_taken
&&
8550 !(pmu
->capabilities
& PERF_PMU_CAP_HETEROGENEOUS_CPUS
)))
8551 pmu
->task_ctx_nr
= perf_invalid_context
;
8553 hw_context_taken
= 1;
8556 pmu
->pmu_cpu_context
= find_pmu_context(pmu
->task_ctx_nr
);
8557 if (pmu
->pmu_cpu_context
)
8558 goto got_cpu_context
;
8561 pmu
->pmu_cpu_context
= alloc_percpu(struct perf_cpu_context
);
8562 if (!pmu
->pmu_cpu_context
)
8565 for_each_possible_cpu(cpu
) {
8566 struct perf_cpu_context
*cpuctx
;
8568 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
8569 __perf_event_init_context(&cpuctx
->ctx
);
8570 lockdep_set_class(&cpuctx
->ctx
.mutex
, &cpuctx_mutex
);
8571 lockdep_set_class(&cpuctx
->ctx
.lock
, &cpuctx_lock
);
8572 cpuctx
->ctx
.pmu
= pmu
;
8574 __perf_mux_hrtimer_init(cpuctx
, cpu
);
8576 cpuctx
->unique_pmu
= pmu
;
8580 if (!pmu
->start_txn
) {
8581 if (pmu
->pmu_enable
) {
8583 * If we have pmu_enable/pmu_disable calls, install
8584 * transaction stubs that use that to try and batch
8585 * hardware accesses.
8587 pmu
->start_txn
= perf_pmu_start_txn
;
8588 pmu
->commit_txn
= perf_pmu_commit_txn
;
8589 pmu
->cancel_txn
= perf_pmu_cancel_txn
;
8591 pmu
->start_txn
= perf_pmu_nop_txn
;
8592 pmu
->commit_txn
= perf_pmu_nop_int
;
8593 pmu
->cancel_txn
= perf_pmu_nop_void
;
8597 if (!pmu
->pmu_enable
) {
8598 pmu
->pmu_enable
= perf_pmu_nop_void
;
8599 pmu
->pmu_disable
= perf_pmu_nop_void
;
8602 if (!pmu
->event_idx
)
8603 pmu
->event_idx
= perf_event_idx_default
;
8605 list_add_rcu(&pmu
->entry
, &pmus
);
8606 atomic_set(&pmu
->exclusive_cnt
, 0);
8609 mutex_unlock(&pmus_lock
);
8614 device_del(pmu
->dev
);
8615 put_device(pmu
->dev
);
8618 if (pmu
->type
>= PERF_TYPE_MAX
)
8619 idr_remove(&pmu_idr
, pmu
->type
);
8622 free_percpu(pmu
->pmu_disable_count
);
8625 EXPORT_SYMBOL_GPL(perf_pmu_register
);
8627 void perf_pmu_unregister(struct pmu
*pmu
)
8629 mutex_lock(&pmus_lock
);
8630 list_del_rcu(&pmu
->entry
);
8631 mutex_unlock(&pmus_lock
);
8634 * We dereference the pmu list under both SRCU and regular RCU, so
8635 * synchronize against both of those.
8637 synchronize_srcu(&pmus_srcu
);
8640 free_percpu(pmu
->pmu_disable_count
);
8641 if (pmu
->type
>= PERF_TYPE_MAX
)
8642 idr_remove(&pmu_idr
, pmu
->type
);
8643 if (pmu
->nr_addr_filters
)
8644 device_remove_file(pmu
->dev
, &dev_attr_nr_addr_filters
);
8645 device_del(pmu
->dev
);
8646 put_device(pmu
->dev
);
8647 free_pmu_context(pmu
);
8649 EXPORT_SYMBOL_GPL(perf_pmu_unregister
);
8651 static int perf_try_init_event(struct pmu
*pmu
, struct perf_event
*event
)
8653 struct perf_event_context
*ctx
= NULL
;
8656 if (!try_module_get(pmu
->module
))
8659 if (event
->group_leader
!= event
) {
8661 * This ctx->mutex can nest when we're called through
8662 * inheritance. See the perf_event_ctx_lock_nested() comment.
8664 ctx
= perf_event_ctx_lock_nested(event
->group_leader
,
8665 SINGLE_DEPTH_NESTING
);
8670 ret
= pmu
->event_init(event
);
8673 perf_event_ctx_unlock(event
->group_leader
, ctx
);
8676 module_put(pmu
->module
);
8681 static struct pmu
*perf_init_event(struct perf_event
*event
)
8683 struct pmu
*pmu
= NULL
;
8687 idx
= srcu_read_lock(&pmus_srcu
);
8690 pmu
= idr_find(&pmu_idr
, event
->attr
.type
);
8693 ret
= perf_try_init_event(pmu
, event
);
8699 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
8700 ret
= perf_try_init_event(pmu
, event
);
8704 if (ret
!= -ENOENT
) {
8709 pmu
= ERR_PTR(-ENOENT
);
8711 srcu_read_unlock(&pmus_srcu
, idx
);
8716 static void attach_sb_event(struct perf_event
*event
)
8718 struct pmu_event_list
*pel
= per_cpu_ptr(&pmu_sb_events
, event
->cpu
);
8720 raw_spin_lock(&pel
->lock
);
8721 list_add_rcu(&event
->sb_list
, &pel
->list
);
8722 raw_spin_unlock(&pel
->lock
);
8726 * We keep a list of all !task (and therefore per-cpu) events
8727 * that need to receive side-band records.
8729 * This avoids having to scan all the various PMU per-cpu contexts
8732 static void account_pmu_sb_event(struct perf_event
*event
)
8734 if (is_sb_event(event
))
8735 attach_sb_event(event
);
8738 static void account_event_cpu(struct perf_event
*event
, int cpu
)
8743 if (is_cgroup_event(event
))
8744 atomic_inc(&per_cpu(perf_cgroup_events
, cpu
));
8747 /* Freq events need the tick to stay alive (see perf_event_task_tick). */
8748 static void account_freq_event_nohz(void)
8750 #ifdef CONFIG_NO_HZ_FULL
8751 /* Lock so we don't race with concurrent unaccount */
8752 spin_lock(&nr_freq_lock
);
8753 if (atomic_inc_return(&nr_freq_events
) == 1)
8754 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS
);
8755 spin_unlock(&nr_freq_lock
);
8759 static void account_freq_event(void)
8761 if (tick_nohz_full_enabled())
8762 account_freq_event_nohz();
8764 atomic_inc(&nr_freq_events
);
8768 static void account_event(struct perf_event
*event
)
8775 if (event
->attach_state
& PERF_ATTACH_TASK
)
8777 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
8778 atomic_inc(&nr_mmap_events
);
8779 if (event
->attr
.comm
)
8780 atomic_inc(&nr_comm_events
);
8781 if (event
->attr
.task
)
8782 atomic_inc(&nr_task_events
);
8783 if (event
->attr
.freq
)
8784 account_freq_event();
8785 if (event
->attr
.context_switch
) {
8786 atomic_inc(&nr_switch_events
);
8789 if (has_branch_stack(event
))
8791 if (is_cgroup_event(event
))
8795 if (atomic_inc_not_zero(&perf_sched_count
))
8798 mutex_lock(&perf_sched_mutex
);
8799 if (!atomic_read(&perf_sched_count
)) {
8800 static_branch_enable(&perf_sched_events
);
8802 * Guarantee that all CPUs observe they key change and
8803 * call the perf scheduling hooks before proceeding to
8804 * install events that need them.
8806 synchronize_sched();
8809 * Now that we have waited for the sync_sched(), allow further
8810 * increments to by-pass the mutex.
8812 atomic_inc(&perf_sched_count
);
8813 mutex_unlock(&perf_sched_mutex
);
8817 account_event_cpu(event
, event
->cpu
);
8819 account_pmu_sb_event(event
);
8823 * Allocate and initialize a event structure
8825 static struct perf_event
*
8826 perf_event_alloc(struct perf_event_attr
*attr
, int cpu
,
8827 struct task_struct
*task
,
8828 struct perf_event
*group_leader
,
8829 struct perf_event
*parent_event
,
8830 perf_overflow_handler_t overflow_handler
,
8831 void *context
, int cgroup_fd
)
8834 struct perf_event
*event
;
8835 struct hw_perf_event
*hwc
;
8838 if ((unsigned)cpu
>= nr_cpu_ids
) {
8839 if (!task
|| cpu
!= -1)
8840 return ERR_PTR(-EINVAL
);
8843 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
8845 return ERR_PTR(-ENOMEM
);
8848 * Single events are their own group leaders, with an
8849 * empty sibling list:
8852 group_leader
= event
;
8854 mutex_init(&event
->child_mutex
);
8855 INIT_LIST_HEAD(&event
->child_list
);
8857 INIT_LIST_HEAD(&event
->group_entry
);
8858 INIT_LIST_HEAD(&event
->event_entry
);
8859 INIT_LIST_HEAD(&event
->sibling_list
);
8860 INIT_LIST_HEAD(&event
->rb_entry
);
8861 INIT_LIST_HEAD(&event
->active_entry
);
8862 INIT_LIST_HEAD(&event
->addr_filters
.list
);
8863 INIT_HLIST_NODE(&event
->hlist_entry
);
8866 init_waitqueue_head(&event
->waitq
);
8867 init_irq_work(&event
->pending
, perf_pending_event
);
8869 mutex_init(&event
->mmap_mutex
);
8870 raw_spin_lock_init(&event
->addr_filters
.lock
);
8872 atomic_long_set(&event
->refcount
, 1);
8874 event
->attr
= *attr
;
8875 event
->group_leader
= group_leader
;
8879 event
->parent
= parent_event
;
8881 event
->ns
= get_pid_ns(task_active_pid_ns(current
));
8882 event
->id
= atomic64_inc_return(&perf_event_id
);
8884 event
->state
= PERF_EVENT_STATE_INACTIVE
;
8887 event
->attach_state
= PERF_ATTACH_TASK
;
8889 * XXX pmu::event_init needs to know what task to account to
8890 * and we cannot use the ctx information because we need the
8891 * pmu before we get a ctx.
8893 event
->hw
.target
= task
;
8896 event
->clock
= &local_clock
;
8898 event
->clock
= parent_event
->clock
;
8900 if (!overflow_handler
&& parent_event
) {
8901 overflow_handler
= parent_event
->overflow_handler
;
8902 context
= parent_event
->overflow_handler_context
;
8905 if (overflow_handler
) {
8906 event
->overflow_handler
= overflow_handler
;
8907 event
->overflow_handler_context
= context
;
8908 } else if (is_write_backward(event
)){
8909 event
->overflow_handler
= perf_event_output_backward
;
8910 event
->overflow_handler_context
= NULL
;
8912 event
->overflow_handler
= perf_event_output_forward
;
8913 event
->overflow_handler_context
= NULL
;
8916 perf_event__state_init(event
);
8921 hwc
->sample_period
= attr
->sample_period
;
8922 if (attr
->freq
&& attr
->sample_freq
)
8923 hwc
->sample_period
= 1;
8924 hwc
->last_period
= hwc
->sample_period
;
8926 local64_set(&hwc
->period_left
, hwc
->sample_period
);
8929 * we currently do not support PERF_FORMAT_GROUP on inherited events
8931 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
8934 if (!has_branch_stack(event
))
8935 event
->attr
.branch_sample_type
= 0;
8937 if (cgroup_fd
!= -1) {
8938 err
= perf_cgroup_connect(cgroup_fd
, event
, attr
, group_leader
);
8943 pmu
= perf_init_event(event
);
8946 else if (IS_ERR(pmu
)) {
8951 err
= exclusive_event_init(event
);
8955 if (has_addr_filter(event
)) {
8956 event
->addr_filters_offs
= kcalloc(pmu
->nr_addr_filters
,
8957 sizeof(unsigned long),
8959 if (!event
->addr_filters_offs
)
8962 /* force hw sync on the address filters */
8963 event
->addr_filters_gen
= 1;
8966 if (!event
->parent
) {
8967 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
8968 err
= get_callchain_buffers(attr
->sample_max_stack
);
8970 goto err_addr_filters
;
8974 /* symmetric to unaccount_event() in _free_event() */
8975 account_event(event
);
8980 kfree(event
->addr_filters_offs
);
8983 exclusive_event_destroy(event
);
8987 event
->destroy(event
);
8988 module_put(pmu
->module
);
8990 if (is_cgroup_event(event
))
8991 perf_detach_cgroup(event
);
8993 put_pid_ns(event
->ns
);
8996 return ERR_PTR(err
);
8999 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
9000 struct perf_event_attr
*attr
)
9005 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
9009 * zero the full structure, so that a short copy will be nice.
9011 memset(attr
, 0, sizeof(*attr
));
9013 ret
= get_user(size
, &uattr
->size
);
9017 if (size
> PAGE_SIZE
) /* silly large */
9020 if (!size
) /* abi compat */
9021 size
= PERF_ATTR_SIZE_VER0
;
9023 if (size
< PERF_ATTR_SIZE_VER0
)
9027 * If we're handed a bigger struct than we know of,
9028 * ensure all the unknown bits are 0 - i.e. new
9029 * user-space does not rely on any kernel feature
9030 * extensions we dont know about yet.
9032 if (size
> sizeof(*attr
)) {
9033 unsigned char __user
*addr
;
9034 unsigned char __user
*end
;
9037 addr
= (void __user
*)uattr
+ sizeof(*attr
);
9038 end
= (void __user
*)uattr
+ size
;
9040 for (; addr
< end
; addr
++) {
9041 ret
= get_user(val
, addr
);
9047 size
= sizeof(*attr
);
9050 ret
= copy_from_user(attr
, uattr
, size
);
9054 if (attr
->__reserved_1
)
9057 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
9060 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
9063 if (attr
->sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
9064 u64 mask
= attr
->branch_sample_type
;
9066 /* only using defined bits */
9067 if (mask
& ~(PERF_SAMPLE_BRANCH_MAX
-1))
9070 /* at least one branch bit must be set */
9071 if (!(mask
& ~PERF_SAMPLE_BRANCH_PLM_ALL
))
9074 /* propagate priv level, when not set for branch */
9075 if (!(mask
& PERF_SAMPLE_BRANCH_PLM_ALL
)) {
9077 /* exclude_kernel checked on syscall entry */
9078 if (!attr
->exclude_kernel
)
9079 mask
|= PERF_SAMPLE_BRANCH_KERNEL
;
9081 if (!attr
->exclude_user
)
9082 mask
|= PERF_SAMPLE_BRANCH_USER
;
9084 if (!attr
->exclude_hv
)
9085 mask
|= PERF_SAMPLE_BRANCH_HV
;
9087 * adjust user setting (for HW filter setup)
9089 attr
->branch_sample_type
= mask
;
9091 /* privileged levels capture (kernel, hv): check permissions */
9092 if ((mask
& PERF_SAMPLE_BRANCH_PERM_PLM
)
9093 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
9097 if (attr
->sample_type
& PERF_SAMPLE_REGS_USER
) {
9098 ret
= perf_reg_validate(attr
->sample_regs_user
);
9103 if (attr
->sample_type
& PERF_SAMPLE_STACK_USER
) {
9104 if (!arch_perf_have_user_stack_dump())
9108 * We have __u32 type for the size, but so far
9109 * we can only use __u16 as maximum due to the
9110 * __u16 sample size limit.
9112 if (attr
->sample_stack_user
>= USHRT_MAX
)
9114 else if (!IS_ALIGNED(attr
->sample_stack_user
, sizeof(u64
)))
9118 if (attr
->sample_type
& PERF_SAMPLE_REGS_INTR
)
9119 ret
= perf_reg_validate(attr
->sample_regs_intr
);
9124 put_user(sizeof(*attr
), &uattr
->size
);
9130 perf_event_set_output(struct perf_event
*event
, struct perf_event
*output_event
)
9132 struct ring_buffer
*rb
= NULL
;
9138 /* don't allow circular references */
9139 if (event
== output_event
)
9143 * Don't allow cross-cpu buffers
9145 if (output_event
->cpu
!= event
->cpu
)
9149 * If its not a per-cpu rb, it must be the same task.
9151 if (output_event
->cpu
== -1 && output_event
->ctx
!= event
->ctx
)
9155 * Mixing clocks in the same buffer is trouble you don't need.
9157 if (output_event
->clock
!= event
->clock
)
9161 * Either writing ring buffer from beginning or from end.
9162 * Mixing is not allowed.
9164 if (is_write_backward(output_event
) != is_write_backward(event
))
9168 * If both events generate aux data, they must be on the same PMU
9170 if (has_aux(event
) && has_aux(output_event
) &&
9171 event
->pmu
!= output_event
->pmu
)
9175 mutex_lock(&event
->mmap_mutex
);
9176 /* Can't redirect output if we've got an active mmap() */
9177 if (atomic_read(&event
->mmap_count
))
9181 /* get the rb we want to redirect to */
9182 rb
= ring_buffer_get(output_event
);
9187 ring_buffer_attach(event
, rb
);
9191 mutex_unlock(&event
->mmap_mutex
);
9197 static void mutex_lock_double(struct mutex
*a
, struct mutex
*b
)
9203 mutex_lock_nested(b
, SINGLE_DEPTH_NESTING
);
9206 static int perf_event_set_clock(struct perf_event
*event
, clockid_t clk_id
)
9208 bool nmi_safe
= false;
9211 case CLOCK_MONOTONIC
:
9212 event
->clock
= &ktime_get_mono_fast_ns
;
9216 case CLOCK_MONOTONIC_RAW
:
9217 event
->clock
= &ktime_get_raw_fast_ns
;
9221 case CLOCK_REALTIME
:
9222 event
->clock
= &ktime_get_real_ns
;
9225 case CLOCK_BOOTTIME
:
9226 event
->clock
= &ktime_get_boot_ns
;
9230 event
->clock
= &ktime_get_tai_ns
;
9237 if (!nmi_safe
&& !(event
->pmu
->capabilities
& PERF_PMU_CAP_NO_NMI
))
9244 * sys_perf_event_open - open a performance event, associate it to a task/cpu
9246 * @attr_uptr: event_id type attributes for monitoring/sampling
9249 * @group_fd: group leader event fd
9251 SYSCALL_DEFINE5(perf_event_open
,
9252 struct perf_event_attr __user
*, attr_uptr
,
9253 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
9255 struct perf_event
*group_leader
= NULL
, *output_event
= NULL
;
9256 struct perf_event
*event
, *sibling
;
9257 struct perf_event_attr attr
;
9258 struct perf_event_context
*ctx
, *uninitialized_var(gctx
);
9259 struct file
*event_file
= NULL
;
9260 struct fd group
= {NULL
, 0};
9261 struct task_struct
*task
= NULL
;
9266 int f_flags
= O_RDWR
;
9269 /* for future expandability... */
9270 if (flags
& ~PERF_FLAG_ALL
)
9273 err
= perf_copy_attr(attr_uptr
, &attr
);
9277 if (!attr
.exclude_kernel
) {
9278 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
9283 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
9286 if (attr
.sample_period
& (1ULL << 63))
9290 if (!attr
.sample_max_stack
)
9291 attr
.sample_max_stack
= sysctl_perf_event_max_stack
;
9294 * In cgroup mode, the pid argument is used to pass the fd
9295 * opened to the cgroup directory in cgroupfs. The cpu argument
9296 * designates the cpu on which to monitor threads from that
9299 if ((flags
& PERF_FLAG_PID_CGROUP
) && (pid
== -1 || cpu
== -1))
9302 if (flags
& PERF_FLAG_FD_CLOEXEC
)
9303 f_flags
|= O_CLOEXEC
;
9305 event_fd
= get_unused_fd_flags(f_flags
);
9309 if (group_fd
!= -1) {
9310 err
= perf_fget_light(group_fd
, &group
);
9313 group_leader
= group
.file
->private_data
;
9314 if (flags
& PERF_FLAG_FD_OUTPUT
)
9315 output_event
= group_leader
;
9316 if (flags
& PERF_FLAG_FD_NO_GROUP
)
9317 group_leader
= NULL
;
9320 if (pid
!= -1 && !(flags
& PERF_FLAG_PID_CGROUP
)) {
9321 task
= find_lively_task_by_vpid(pid
);
9323 err
= PTR_ERR(task
);
9328 if (task
&& group_leader
&&
9329 group_leader
->attr
.inherit
!= attr
.inherit
) {
9337 err
= mutex_lock_interruptible(&task
->signal
->cred_guard_mutex
);
9342 * Reuse ptrace permission checks for now.
9344 * We must hold cred_guard_mutex across this and any potential
9345 * perf_install_in_context() call for this new event to
9346 * serialize against exec() altering our credentials (and the
9347 * perf_event_exit_task() that could imply).
9350 if (!ptrace_may_access(task
, PTRACE_MODE_READ_REALCREDS
))
9354 if (flags
& PERF_FLAG_PID_CGROUP
)
9357 event
= perf_event_alloc(&attr
, cpu
, task
, group_leader
, NULL
,
9358 NULL
, NULL
, cgroup_fd
);
9359 if (IS_ERR(event
)) {
9360 err
= PTR_ERR(event
);
9364 if (is_sampling_event(event
)) {
9365 if (event
->pmu
->capabilities
& PERF_PMU_CAP_NO_INTERRUPT
) {
9372 * Special case software events and allow them to be part of
9373 * any hardware group.
9377 if (attr
.use_clockid
) {
9378 err
= perf_event_set_clock(event
, attr
.clockid
);
9384 (is_software_event(event
) != is_software_event(group_leader
))) {
9385 if (is_software_event(event
)) {
9387 * If event and group_leader are not both a software
9388 * event, and event is, then group leader is not.
9390 * Allow the addition of software events to !software
9391 * groups, this is safe because software events never
9394 pmu
= group_leader
->pmu
;
9395 } else if (is_software_event(group_leader
) &&
9396 (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
)) {
9398 * In case the group is a pure software group, and we
9399 * try to add a hardware event, move the whole group to
9400 * the hardware context.
9407 * Get the target context (task or percpu):
9409 ctx
= find_get_context(pmu
, task
, event
);
9415 if ((pmu
->capabilities
& PERF_PMU_CAP_EXCLUSIVE
) && group_leader
) {
9421 * Look up the group leader (we will attach this event to it):
9427 * Do not allow a recursive hierarchy (this new sibling
9428 * becoming part of another group-sibling):
9430 if (group_leader
->group_leader
!= group_leader
)
9433 /* All events in a group should have the same clock */
9434 if (group_leader
->clock
!= event
->clock
)
9438 * Do not allow to attach to a group in a different
9439 * task or CPU context:
9443 * Make sure we're both on the same task, or both
9446 if (group_leader
->ctx
->task
!= ctx
->task
)
9450 * Make sure we're both events for the same CPU;
9451 * grouping events for different CPUs is broken; since
9452 * you can never concurrently schedule them anyhow.
9454 if (group_leader
->cpu
!= event
->cpu
)
9457 if (group_leader
->ctx
!= ctx
)
9462 * Only a group leader can be exclusive or pinned
9464 if (attr
.exclusive
|| attr
.pinned
)
9469 err
= perf_event_set_output(event
, output_event
);
9474 event_file
= anon_inode_getfile("[perf_event]", &perf_fops
, event
,
9476 if (IS_ERR(event_file
)) {
9477 err
= PTR_ERR(event_file
);
9483 gctx
= group_leader
->ctx
;
9484 mutex_lock_double(&gctx
->mutex
, &ctx
->mutex
);
9485 if (gctx
->task
== TASK_TOMBSTONE
) {
9490 mutex_lock(&ctx
->mutex
);
9493 if (ctx
->task
== TASK_TOMBSTONE
) {
9498 if (!perf_event_validate_size(event
)) {
9504 * Must be under the same ctx::mutex as perf_install_in_context(),
9505 * because we need to serialize with concurrent event creation.
9507 if (!exclusive_event_installable(event
, ctx
)) {
9508 /* exclusive and group stuff are assumed mutually exclusive */
9509 WARN_ON_ONCE(move_group
);
9515 WARN_ON_ONCE(ctx
->parent_ctx
);
9518 * This is the point on no return; we cannot fail hereafter. This is
9519 * where we start modifying current state.
9524 * See perf_event_ctx_lock() for comments on the details
9525 * of swizzling perf_event::ctx.
9527 perf_remove_from_context(group_leader
, 0);
9529 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
9531 perf_remove_from_context(sibling
, 0);
9536 * Wait for everybody to stop referencing the events through
9537 * the old lists, before installing it on new lists.
9542 * Install the group siblings before the group leader.
9544 * Because a group leader will try and install the entire group
9545 * (through the sibling list, which is still in-tact), we can
9546 * end up with siblings installed in the wrong context.
9548 * By installing siblings first we NO-OP because they're not
9549 * reachable through the group lists.
9551 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
9553 perf_event__state_init(sibling
);
9554 perf_install_in_context(ctx
, sibling
, sibling
->cpu
);
9559 * Removing from the context ends up with disabled
9560 * event. What we want here is event in the initial
9561 * startup state, ready to be add into new context.
9563 perf_event__state_init(group_leader
);
9564 perf_install_in_context(ctx
, group_leader
, group_leader
->cpu
);
9568 * Now that all events are installed in @ctx, nothing
9569 * references @gctx anymore, so drop the last reference we have
9576 * Precalculate sample_data sizes; do while holding ctx::mutex such
9577 * that we're serialized against further additions and before
9578 * perf_install_in_context() which is the point the event is active and
9579 * can use these values.
9581 perf_event__header_size(event
);
9582 perf_event__id_header_size(event
);
9584 event
->owner
= current
;
9586 perf_install_in_context(ctx
, event
, event
->cpu
);
9587 perf_unpin_context(ctx
);
9590 mutex_unlock(&gctx
->mutex
);
9591 mutex_unlock(&ctx
->mutex
);
9594 mutex_unlock(&task
->signal
->cred_guard_mutex
);
9595 put_task_struct(task
);
9600 mutex_lock(¤t
->perf_event_mutex
);
9601 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
9602 mutex_unlock(¤t
->perf_event_mutex
);
9605 * Drop the reference on the group_event after placing the
9606 * new event on the sibling_list. This ensures destruction
9607 * of the group leader will find the pointer to itself in
9608 * perf_group_detach().
9611 fd_install(event_fd
, event_file
);
9616 mutex_unlock(&gctx
->mutex
);
9617 mutex_unlock(&ctx
->mutex
);
9621 perf_unpin_context(ctx
);
9625 * If event_file is set, the fput() above will have called ->release()
9626 * and that will take care of freeing the event.
9632 mutex_unlock(&task
->signal
->cred_guard_mutex
);
9637 put_task_struct(task
);
9641 put_unused_fd(event_fd
);
9646 * perf_event_create_kernel_counter
9648 * @attr: attributes of the counter to create
9649 * @cpu: cpu in which the counter is bound
9650 * @task: task to profile (NULL for percpu)
9653 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
9654 struct task_struct
*task
,
9655 perf_overflow_handler_t overflow_handler
,
9658 struct perf_event_context
*ctx
;
9659 struct perf_event
*event
;
9663 * Get the target context (task or percpu):
9666 event
= perf_event_alloc(attr
, cpu
, task
, NULL
, NULL
,
9667 overflow_handler
, context
, -1);
9668 if (IS_ERR(event
)) {
9669 err
= PTR_ERR(event
);
9673 /* Mark owner so we could distinguish it from user events. */
9674 event
->owner
= TASK_TOMBSTONE
;
9676 ctx
= find_get_context(event
->pmu
, task
, event
);
9682 WARN_ON_ONCE(ctx
->parent_ctx
);
9683 mutex_lock(&ctx
->mutex
);
9684 if (ctx
->task
== TASK_TOMBSTONE
) {
9689 if (!exclusive_event_installable(event
, ctx
)) {
9694 perf_install_in_context(ctx
, event
, cpu
);
9695 perf_unpin_context(ctx
);
9696 mutex_unlock(&ctx
->mutex
);
9701 mutex_unlock(&ctx
->mutex
);
9702 perf_unpin_context(ctx
);
9707 return ERR_PTR(err
);
9709 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
9711 void perf_pmu_migrate_context(struct pmu
*pmu
, int src_cpu
, int dst_cpu
)
9713 struct perf_event_context
*src_ctx
;
9714 struct perf_event_context
*dst_ctx
;
9715 struct perf_event
*event
, *tmp
;
9718 src_ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, src_cpu
)->ctx
;
9719 dst_ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, dst_cpu
)->ctx
;
9722 * See perf_event_ctx_lock() for comments on the details
9723 * of swizzling perf_event::ctx.
9725 mutex_lock_double(&src_ctx
->mutex
, &dst_ctx
->mutex
);
9726 list_for_each_entry_safe(event
, tmp
, &src_ctx
->event_list
,
9728 perf_remove_from_context(event
, 0);
9729 unaccount_event_cpu(event
, src_cpu
);
9731 list_add(&event
->migrate_entry
, &events
);
9735 * Wait for the events to quiesce before re-instating them.
9740 * Re-instate events in 2 passes.
9742 * Skip over group leaders and only install siblings on this first
9743 * pass, siblings will not get enabled without a leader, however a
9744 * leader will enable its siblings, even if those are still on the old
9747 list_for_each_entry_safe(event
, tmp
, &events
, migrate_entry
) {
9748 if (event
->group_leader
== event
)
9751 list_del(&event
->migrate_entry
);
9752 if (event
->state
>= PERF_EVENT_STATE_OFF
)
9753 event
->state
= PERF_EVENT_STATE_INACTIVE
;
9754 account_event_cpu(event
, dst_cpu
);
9755 perf_install_in_context(dst_ctx
, event
, dst_cpu
);
9760 * Once all the siblings are setup properly, install the group leaders
9763 list_for_each_entry_safe(event
, tmp
, &events
, migrate_entry
) {
9764 list_del(&event
->migrate_entry
);
9765 if (event
->state
>= PERF_EVENT_STATE_OFF
)
9766 event
->state
= PERF_EVENT_STATE_INACTIVE
;
9767 account_event_cpu(event
, dst_cpu
);
9768 perf_install_in_context(dst_ctx
, event
, dst_cpu
);
9771 mutex_unlock(&dst_ctx
->mutex
);
9772 mutex_unlock(&src_ctx
->mutex
);
9774 EXPORT_SYMBOL_GPL(perf_pmu_migrate_context
);
9776 static void sync_child_event(struct perf_event
*child_event
,
9777 struct task_struct
*child
)
9779 struct perf_event
*parent_event
= child_event
->parent
;
9782 if (child_event
->attr
.inherit_stat
)
9783 perf_event_read_event(child_event
, child
);
9785 child_val
= perf_event_count(child_event
);
9788 * Add back the child's count to the parent's count:
9790 atomic64_add(child_val
, &parent_event
->child_count
);
9791 atomic64_add(child_event
->total_time_enabled
,
9792 &parent_event
->child_total_time_enabled
);
9793 atomic64_add(child_event
->total_time_running
,
9794 &parent_event
->child_total_time_running
);
9798 perf_event_exit_event(struct perf_event
*child_event
,
9799 struct perf_event_context
*child_ctx
,
9800 struct task_struct
*child
)
9802 struct perf_event
*parent_event
= child_event
->parent
;
9805 * Do not destroy the 'original' grouping; because of the context
9806 * switch optimization the original events could've ended up in a
9807 * random child task.
9809 * If we were to destroy the original group, all group related
9810 * operations would cease to function properly after this random
9813 * Do destroy all inherited groups, we don't care about those
9814 * and being thorough is better.
9816 raw_spin_lock_irq(&child_ctx
->lock
);
9817 WARN_ON_ONCE(child_ctx
->is_active
);
9820 perf_group_detach(child_event
);
9821 list_del_event(child_event
, child_ctx
);
9822 child_event
->state
= PERF_EVENT_STATE_EXIT
; /* is_event_hup() */
9823 raw_spin_unlock_irq(&child_ctx
->lock
);
9826 * Parent events are governed by their filedesc, retain them.
9828 if (!parent_event
) {
9829 perf_event_wakeup(child_event
);
9833 * Child events can be cleaned up.
9836 sync_child_event(child_event
, child
);
9839 * Remove this event from the parent's list
9841 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
9842 mutex_lock(&parent_event
->child_mutex
);
9843 list_del_init(&child_event
->child_list
);
9844 mutex_unlock(&parent_event
->child_mutex
);
9847 * Kick perf_poll() for is_event_hup().
9849 perf_event_wakeup(parent_event
);
9850 free_event(child_event
);
9851 put_event(parent_event
);
9854 static void perf_event_exit_task_context(struct task_struct
*child
, int ctxn
)
9856 struct perf_event_context
*child_ctx
, *clone_ctx
= NULL
;
9857 struct perf_event
*child_event
, *next
;
9859 WARN_ON_ONCE(child
!= current
);
9861 child_ctx
= perf_pin_task_context(child
, ctxn
);
9866 * In order to reduce the amount of tricky in ctx tear-down, we hold
9867 * ctx::mutex over the entire thing. This serializes against almost
9868 * everything that wants to access the ctx.
9870 * The exception is sys_perf_event_open() /
9871 * perf_event_create_kernel_count() which does find_get_context()
9872 * without ctx::mutex (it cannot because of the move_group double mutex
9873 * lock thing). See the comments in perf_install_in_context().
9875 mutex_lock(&child_ctx
->mutex
);
9878 * In a single ctx::lock section, de-schedule the events and detach the
9879 * context from the task such that we cannot ever get it scheduled back
9882 raw_spin_lock_irq(&child_ctx
->lock
);
9883 task_ctx_sched_out(__get_cpu_context(child_ctx
), child_ctx
);
9886 * Now that the context is inactive, destroy the task <-> ctx relation
9887 * and mark the context dead.
9889 RCU_INIT_POINTER(child
->perf_event_ctxp
[ctxn
], NULL
);
9890 put_ctx(child_ctx
); /* cannot be last */
9891 WRITE_ONCE(child_ctx
->task
, TASK_TOMBSTONE
);
9892 put_task_struct(current
); /* cannot be last */
9894 clone_ctx
= unclone_ctx(child_ctx
);
9895 raw_spin_unlock_irq(&child_ctx
->lock
);
9901 * Report the task dead after unscheduling the events so that we
9902 * won't get any samples after PERF_RECORD_EXIT. We can however still
9903 * get a few PERF_RECORD_READ events.
9905 perf_event_task(child
, child_ctx
, 0);
9907 list_for_each_entry_safe(child_event
, next
, &child_ctx
->event_list
, event_entry
)
9908 perf_event_exit_event(child_event
, child_ctx
, child
);
9910 mutex_unlock(&child_ctx
->mutex
);
9916 * When a child task exits, feed back event values to parent events.
9918 * Can be called with cred_guard_mutex held when called from
9919 * install_exec_creds().
9921 void perf_event_exit_task(struct task_struct
*child
)
9923 struct perf_event
*event
, *tmp
;
9926 mutex_lock(&child
->perf_event_mutex
);
9927 list_for_each_entry_safe(event
, tmp
, &child
->perf_event_list
,
9929 list_del_init(&event
->owner_entry
);
9932 * Ensure the list deletion is visible before we clear
9933 * the owner, closes a race against perf_release() where
9934 * we need to serialize on the owner->perf_event_mutex.
9936 smp_store_release(&event
->owner
, NULL
);
9938 mutex_unlock(&child
->perf_event_mutex
);
9940 for_each_task_context_nr(ctxn
)
9941 perf_event_exit_task_context(child
, ctxn
);
9944 * The perf_event_exit_task_context calls perf_event_task
9945 * with child's task_ctx, which generates EXIT events for
9946 * child contexts and sets child->perf_event_ctxp[] to NULL.
9947 * At this point we need to send EXIT events to cpu contexts.
9949 perf_event_task(child
, NULL
, 0);
9952 static void perf_free_event(struct perf_event
*event
,
9953 struct perf_event_context
*ctx
)
9955 struct perf_event
*parent
= event
->parent
;
9957 if (WARN_ON_ONCE(!parent
))
9960 mutex_lock(&parent
->child_mutex
);
9961 list_del_init(&event
->child_list
);
9962 mutex_unlock(&parent
->child_mutex
);
9966 raw_spin_lock_irq(&ctx
->lock
);
9967 perf_group_detach(event
);
9968 list_del_event(event
, ctx
);
9969 raw_spin_unlock_irq(&ctx
->lock
);
9974 * Free an unexposed, unused context as created by inheritance by
9975 * perf_event_init_task below, used by fork() in case of fail.
9977 * Not all locks are strictly required, but take them anyway to be nice and
9978 * help out with the lockdep assertions.
9980 void perf_event_free_task(struct task_struct
*task
)
9982 struct perf_event_context
*ctx
;
9983 struct perf_event
*event
, *tmp
;
9986 for_each_task_context_nr(ctxn
) {
9987 ctx
= task
->perf_event_ctxp
[ctxn
];
9991 mutex_lock(&ctx
->mutex
);
9993 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
,
9995 perf_free_event(event
, ctx
);
9997 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
,
9999 perf_free_event(event
, ctx
);
10001 if (!list_empty(&ctx
->pinned_groups
) ||
10002 !list_empty(&ctx
->flexible_groups
))
10005 mutex_unlock(&ctx
->mutex
);
10011 void perf_event_delayed_put(struct task_struct
*task
)
10015 for_each_task_context_nr(ctxn
)
10016 WARN_ON_ONCE(task
->perf_event_ctxp
[ctxn
]);
10019 struct file
*perf_event_get(unsigned int fd
)
10023 file
= fget_raw(fd
);
10025 return ERR_PTR(-EBADF
);
10027 if (file
->f_op
!= &perf_fops
) {
10029 return ERR_PTR(-EBADF
);
10035 const struct perf_event_attr
*perf_event_attrs(struct perf_event
*event
)
10038 return ERR_PTR(-EINVAL
);
10040 return &event
->attr
;
10044 * inherit a event from parent task to child task:
10046 static struct perf_event
*
10047 inherit_event(struct perf_event
*parent_event
,
10048 struct task_struct
*parent
,
10049 struct perf_event_context
*parent_ctx
,
10050 struct task_struct
*child
,
10051 struct perf_event
*group_leader
,
10052 struct perf_event_context
*child_ctx
)
10054 enum perf_event_active_state parent_state
= parent_event
->state
;
10055 struct perf_event
*child_event
;
10056 unsigned long flags
;
10059 * Instead of creating recursive hierarchies of events,
10060 * we link inherited events back to the original parent,
10061 * which has a filp for sure, which we use as the reference
10064 if (parent_event
->parent
)
10065 parent_event
= parent_event
->parent
;
10067 child_event
= perf_event_alloc(&parent_event
->attr
,
10070 group_leader
, parent_event
,
10072 if (IS_ERR(child_event
))
10073 return child_event
;
10076 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
10077 * must be under the same lock in order to serialize against
10078 * perf_event_release_kernel(), such that either we must observe
10079 * is_orphaned_event() or they will observe us on the child_list.
10081 mutex_lock(&parent_event
->child_mutex
);
10082 if (is_orphaned_event(parent_event
) ||
10083 !atomic_long_inc_not_zero(&parent_event
->refcount
)) {
10084 mutex_unlock(&parent_event
->child_mutex
);
10085 free_event(child_event
);
10089 get_ctx(child_ctx
);
10092 * Make the child state follow the state of the parent event,
10093 * not its attr.disabled bit. We hold the parent's mutex,
10094 * so we won't race with perf_event_{en, dis}able_family.
10096 if (parent_state
>= PERF_EVENT_STATE_INACTIVE
)
10097 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
10099 child_event
->state
= PERF_EVENT_STATE_OFF
;
10101 if (parent_event
->attr
.freq
) {
10102 u64 sample_period
= parent_event
->hw
.sample_period
;
10103 struct hw_perf_event
*hwc
= &child_event
->hw
;
10105 hwc
->sample_period
= sample_period
;
10106 hwc
->last_period
= sample_period
;
10108 local64_set(&hwc
->period_left
, sample_period
);
10111 child_event
->ctx
= child_ctx
;
10112 child_event
->overflow_handler
= parent_event
->overflow_handler
;
10113 child_event
->overflow_handler_context
10114 = parent_event
->overflow_handler_context
;
10117 * Precalculate sample_data sizes
10119 perf_event__header_size(child_event
);
10120 perf_event__id_header_size(child_event
);
10123 * Link it up in the child's context:
10125 raw_spin_lock_irqsave(&child_ctx
->lock
, flags
);
10126 add_event_to_ctx(child_event
, child_ctx
);
10127 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
10130 * Link this into the parent event's child list
10132 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
10133 mutex_unlock(&parent_event
->child_mutex
);
10135 return child_event
;
10138 static int inherit_group(struct perf_event
*parent_event
,
10139 struct task_struct
*parent
,
10140 struct perf_event_context
*parent_ctx
,
10141 struct task_struct
*child
,
10142 struct perf_event_context
*child_ctx
)
10144 struct perf_event
*leader
;
10145 struct perf_event
*sub
;
10146 struct perf_event
*child_ctr
;
10148 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
10149 child
, NULL
, child_ctx
);
10150 if (IS_ERR(leader
))
10151 return PTR_ERR(leader
);
10152 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
10153 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
10154 child
, leader
, child_ctx
);
10155 if (IS_ERR(child_ctr
))
10156 return PTR_ERR(child_ctr
);
10162 inherit_task_group(struct perf_event
*event
, struct task_struct
*parent
,
10163 struct perf_event_context
*parent_ctx
,
10164 struct task_struct
*child
, int ctxn
,
10165 int *inherited_all
)
10168 struct perf_event_context
*child_ctx
;
10170 if (!event
->attr
.inherit
) {
10171 *inherited_all
= 0;
10175 child_ctx
= child
->perf_event_ctxp
[ctxn
];
10178 * This is executed from the parent task context, so
10179 * inherit events that have been marked for cloning.
10180 * First allocate and initialize a context for the
10184 child_ctx
= alloc_perf_context(parent_ctx
->pmu
, child
);
10188 child
->perf_event_ctxp
[ctxn
] = child_ctx
;
10191 ret
= inherit_group(event
, parent
, parent_ctx
,
10195 *inherited_all
= 0;
10201 * Initialize the perf_event context in task_struct
10203 static int perf_event_init_context(struct task_struct
*child
, int ctxn
)
10205 struct perf_event_context
*child_ctx
, *parent_ctx
;
10206 struct perf_event_context
*cloned_ctx
;
10207 struct perf_event
*event
;
10208 struct task_struct
*parent
= current
;
10209 int inherited_all
= 1;
10210 unsigned long flags
;
10213 if (likely(!parent
->perf_event_ctxp
[ctxn
]))
10217 * If the parent's context is a clone, pin it so it won't get
10218 * swapped under us.
10220 parent_ctx
= perf_pin_task_context(parent
, ctxn
);
10225 * No need to check if parent_ctx != NULL here; since we saw
10226 * it non-NULL earlier, the only reason for it to become NULL
10227 * is if we exit, and since we're currently in the middle of
10228 * a fork we can't be exiting at the same time.
10232 * Lock the parent list. No need to lock the child - not PID
10233 * hashed yet and not running, so nobody can access it.
10235 mutex_lock(&parent_ctx
->mutex
);
10238 * We dont have to disable NMIs - we are only looking at
10239 * the list, not manipulating it:
10241 list_for_each_entry(event
, &parent_ctx
->pinned_groups
, group_entry
) {
10242 ret
= inherit_task_group(event
, parent
, parent_ctx
,
10243 child
, ctxn
, &inherited_all
);
10249 * We can't hold ctx->lock when iterating the ->flexible_group list due
10250 * to allocations, but we need to prevent rotation because
10251 * rotate_ctx() will change the list from interrupt context.
10253 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
10254 parent_ctx
->rotate_disable
= 1;
10255 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
10257 list_for_each_entry(event
, &parent_ctx
->flexible_groups
, group_entry
) {
10258 ret
= inherit_task_group(event
, parent
, parent_ctx
,
10259 child
, ctxn
, &inherited_all
);
10264 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
10265 parent_ctx
->rotate_disable
= 0;
10267 child_ctx
= child
->perf_event_ctxp
[ctxn
];
10269 if (child_ctx
&& inherited_all
) {
10271 * Mark the child context as a clone of the parent
10272 * context, or of whatever the parent is a clone of.
10274 * Note that if the parent is a clone, the holding of
10275 * parent_ctx->lock avoids it from being uncloned.
10277 cloned_ctx
= parent_ctx
->parent_ctx
;
10279 child_ctx
->parent_ctx
= cloned_ctx
;
10280 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
10282 child_ctx
->parent_ctx
= parent_ctx
;
10283 child_ctx
->parent_gen
= parent_ctx
->generation
;
10285 get_ctx(child_ctx
->parent_ctx
);
10288 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
10289 mutex_unlock(&parent_ctx
->mutex
);
10291 perf_unpin_context(parent_ctx
);
10292 put_ctx(parent_ctx
);
10298 * Initialize the perf_event context in task_struct
10300 int perf_event_init_task(struct task_struct
*child
)
10304 memset(child
->perf_event_ctxp
, 0, sizeof(child
->perf_event_ctxp
));
10305 mutex_init(&child
->perf_event_mutex
);
10306 INIT_LIST_HEAD(&child
->perf_event_list
);
10308 for_each_task_context_nr(ctxn
) {
10309 ret
= perf_event_init_context(child
, ctxn
);
10311 perf_event_free_task(child
);
10319 static void __init
perf_event_init_all_cpus(void)
10321 struct swevent_htable
*swhash
;
10324 for_each_possible_cpu(cpu
) {
10325 swhash
= &per_cpu(swevent_htable
, cpu
);
10326 mutex_init(&swhash
->hlist_mutex
);
10327 INIT_LIST_HEAD(&per_cpu(active_ctx_list
, cpu
));
10329 INIT_LIST_HEAD(&per_cpu(pmu_sb_events
.list
, cpu
));
10330 raw_spin_lock_init(&per_cpu(pmu_sb_events
.lock
, cpu
));
10334 static void perf_event_init_cpu(int cpu
)
10336 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
10338 mutex_lock(&swhash
->hlist_mutex
);
10339 if (swhash
->hlist_refcount
> 0 && !swevent_hlist_deref(swhash
)) {
10340 struct swevent_hlist
*hlist
;
10342 hlist
= kzalloc_node(sizeof(*hlist
), GFP_KERNEL
, cpu_to_node(cpu
));
10344 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
10346 mutex_unlock(&swhash
->hlist_mutex
);
10349 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
10350 static void __perf_event_exit_context(void *__info
)
10352 struct perf_event_context
*ctx
= __info
;
10353 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
10354 struct perf_event
*event
;
10356 raw_spin_lock(&ctx
->lock
);
10357 list_for_each_entry(event
, &ctx
->event_list
, event_entry
)
10358 __perf_remove_from_context(event
, cpuctx
, ctx
, (void *)DETACH_GROUP
);
10359 raw_spin_unlock(&ctx
->lock
);
10362 static void perf_event_exit_cpu_context(int cpu
)
10364 struct perf_event_context
*ctx
;
10368 idx
= srcu_read_lock(&pmus_srcu
);
10369 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
10370 ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
)->ctx
;
10372 mutex_lock(&ctx
->mutex
);
10373 smp_call_function_single(cpu
, __perf_event_exit_context
, ctx
, 1);
10374 mutex_unlock(&ctx
->mutex
);
10376 srcu_read_unlock(&pmus_srcu
, idx
);
10379 static void perf_event_exit_cpu(int cpu
)
10381 perf_event_exit_cpu_context(cpu
);
10384 static inline void perf_event_exit_cpu(int cpu
) { }
10388 perf_reboot(struct notifier_block
*notifier
, unsigned long val
, void *v
)
10392 for_each_online_cpu(cpu
)
10393 perf_event_exit_cpu(cpu
);
10399 * Run the perf reboot notifier at the very last possible moment so that
10400 * the generic watchdog code runs as long as possible.
10402 static struct notifier_block perf_reboot_notifier
= {
10403 .notifier_call
= perf_reboot
,
10404 .priority
= INT_MIN
,
10408 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
10410 unsigned int cpu
= (long)hcpu
;
10412 switch (action
& ~CPU_TASKS_FROZEN
) {
10414 case CPU_UP_PREPARE
:
10416 * This must be done before the CPU comes alive, because the
10417 * moment we can run tasks we can encounter (software) events.
10419 * Specifically, someone can have inherited events on kthreadd
10420 * or a pre-existing worker thread that gets re-bound.
10422 perf_event_init_cpu(cpu
);
10425 case CPU_DOWN_PREPARE
:
10427 * This must be done before the CPU dies because after that an
10428 * active event might want to IPI the CPU and that'll not work
10429 * so great for dead CPUs.
10431 * XXX smp_call_function_single() return -ENXIO without a warn
10432 * so we could possibly deal with this.
10434 * This is safe against new events arriving because
10435 * sys_perf_event_open() serializes against hotplug using
10436 * get_online_cpus().
10438 perf_event_exit_cpu(cpu
);
10447 void __init
perf_event_init(void)
10451 idr_init(&pmu_idr
);
10453 perf_event_init_all_cpus();
10454 init_srcu_struct(&pmus_srcu
);
10455 perf_pmu_register(&perf_swevent
, "software", PERF_TYPE_SOFTWARE
);
10456 perf_pmu_register(&perf_cpu_clock
, NULL
, -1);
10457 perf_pmu_register(&perf_task_clock
, NULL
, -1);
10458 perf_tp_register();
10459 perf_cpu_notifier(perf_cpu_notify
);
10460 register_reboot_notifier(&perf_reboot_notifier
);
10462 ret
= init_hw_breakpoint();
10463 WARN(ret
, "hw_breakpoint initialization failed with: %d", ret
);
10466 * Build time assertion that we keep the data_head at the intended
10467 * location. IOW, validation we got the __reserved[] size right.
10469 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page
, data_head
))
10473 ssize_t
perf_event_sysfs_show(struct device
*dev
, struct device_attribute
*attr
,
10476 struct perf_pmu_events_attr
*pmu_attr
=
10477 container_of(attr
, struct perf_pmu_events_attr
, attr
);
10479 if (pmu_attr
->event_str
)
10480 return sprintf(page
, "%s\n", pmu_attr
->event_str
);
10484 EXPORT_SYMBOL_GPL(perf_event_sysfs_show
);
10486 static int __init
perf_event_sysfs_init(void)
10491 mutex_lock(&pmus_lock
);
10493 ret
= bus_register(&pmu_bus
);
10497 list_for_each_entry(pmu
, &pmus
, entry
) {
10498 if (!pmu
->name
|| pmu
->type
< 0)
10501 ret
= pmu_dev_alloc(pmu
);
10502 WARN(ret
, "Failed to register pmu: %s, reason %d\n", pmu
->name
, ret
);
10504 pmu_bus_running
= 1;
10508 mutex_unlock(&pmus_lock
);
10512 device_initcall(perf_event_sysfs_init
);
10514 #ifdef CONFIG_CGROUP_PERF
10515 static struct cgroup_subsys_state
*
10516 perf_cgroup_css_alloc(struct cgroup_subsys_state
*parent_css
)
10518 struct perf_cgroup
*jc
;
10520 jc
= kzalloc(sizeof(*jc
), GFP_KERNEL
);
10522 return ERR_PTR(-ENOMEM
);
10524 jc
->info
= alloc_percpu(struct perf_cgroup_info
);
10527 return ERR_PTR(-ENOMEM
);
10533 static void perf_cgroup_css_free(struct cgroup_subsys_state
*css
)
10535 struct perf_cgroup
*jc
= container_of(css
, struct perf_cgroup
, css
);
10537 free_percpu(jc
->info
);
10541 static int __perf_cgroup_move(void *info
)
10543 struct task_struct
*task
= info
;
10545 perf_cgroup_switch(task
, PERF_CGROUP_SWOUT
| PERF_CGROUP_SWIN
);
10550 static void perf_cgroup_attach(struct cgroup_taskset
*tset
)
10552 struct task_struct
*task
;
10553 struct cgroup_subsys_state
*css
;
10555 cgroup_taskset_for_each(task
, css
, tset
)
10556 task_function_call(task
, __perf_cgroup_move
, task
);
10559 struct cgroup_subsys perf_event_cgrp_subsys
= {
10560 .css_alloc
= perf_cgroup_css_alloc
,
10561 .css_free
= perf_cgroup_css_free
,
10562 .attach
= perf_cgroup_attach
,
10564 #endif /* CONFIG_CGROUP_PERF */