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>
50 #include <asm/irq_regs.h>
52 typedef int (*remote_function_f
)(void *);
54 struct remote_function_call
{
55 struct task_struct
*p
;
56 remote_function_f func
;
61 static void remote_function(void *data
)
63 struct remote_function_call
*tfc
= data
;
64 struct task_struct
*p
= tfc
->p
;
68 if (task_cpu(p
) != smp_processor_id())
72 * Now that we're on right CPU with IRQs disabled, we can test
73 * if we hit the right task without races.
76 tfc
->ret
= -ESRCH
; /* No such (running) process */
81 tfc
->ret
= tfc
->func(tfc
->info
);
85 * task_function_call - call a function on the cpu on which a task runs
86 * @p: the task to evaluate
87 * @func: the function to be called
88 * @info: the function call argument
90 * Calls the function @func when the task is currently running. This might
91 * be on the current CPU, which just calls the function directly
93 * returns: @func return value, or
94 * -ESRCH - when the process isn't running
95 * -EAGAIN - when the process moved away
98 task_function_call(struct task_struct
*p
, remote_function_f func
, void *info
)
100 struct remote_function_call data
= {
109 ret
= smp_call_function_single(task_cpu(p
), remote_function
, &data
, 1);
112 } while (ret
== -EAGAIN
);
118 * cpu_function_call - call a function on the cpu
119 * @func: the function to be called
120 * @info: the function call argument
122 * Calls the function @func on the remote cpu.
124 * returns: @func return value or -ENXIO when the cpu is offline
126 static int cpu_function_call(int cpu
, remote_function_f func
, void *info
)
128 struct remote_function_call data
= {
132 .ret
= -ENXIO
, /* No such CPU */
135 smp_call_function_single(cpu
, remote_function
, &data
, 1);
140 static inline struct perf_cpu_context
*
141 __get_cpu_context(struct perf_event_context
*ctx
)
143 return this_cpu_ptr(ctx
->pmu
->pmu_cpu_context
);
146 static void perf_ctx_lock(struct perf_cpu_context
*cpuctx
,
147 struct perf_event_context
*ctx
)
149 raw_spin_lock(&cpuctx
->ctx
.lock
);
151 raw_spin_lock(&ctx
->lock
);
154 static void perf_ctx_unlock(struct perf_cpu_context
*cpuctx
,
155 struct perf_event_context
*ctx
)
158 raw_spin_unlock(&ctx
->lock
);
159 raw_spin_unlock(&cpuctx
->ctx
.lock
);
162 #define TASK_TOMBSTONE ((void *)-1L)
164 static bool is_kernel_event(struct perf_event
*event
)
166 return READ_ONCE(event
->owner
) == TASK_TOMBSTONE
;
170 * On task ctx scheduling...
172 * When !ctx->nr_events a task context will not be scheduled. This means
173 * we can disable the scheduler hooks (for performance) without leaving
174 * pending task ctx state.
176 * This however results in two special cases:
178 * - removing the last event from a task ctx; this is relatively straight
179 * forward and is done in __perf_remove_from_context.
181 * - adding the first event to a task ctx; this is tricky because we cannot
182 * rely on ctx->is_active and therefore cannot use event_function_call().
183 * See perf_install_in_context().
185 * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set.
188 typedef void (*event_f
)(struct perf_event
*, struct perf_cpu_context
*,
189 struct perf_event_context
*, void *);
191 struct event_function_struct
{
192 struct perf_event
*event
;
197 static int event_function(void *info
)
199 struct event_function_struct
*efs
= info
;
200 struct perf_event
*event
= efs
->event
;
201 struct perf_event_context
*ctx
= event
->ctx
;
202 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
203 struct perf_event_context
*task_ctx
= cpuctx
->task_ctx
;
206 WARN_ON_ONCE(!irqs_disabled());
208 perf_ctx_lock(cpuctx
, task_ctx
);
210 * Since we do the IPI call without holding ctx->lock things can have
211 * changed, double check we hit the task we set out to hit.
214 if (ctx
->task
!= current
) {
220 * We only use event_function_call() on established contexts,
221 * and event_function() is only ever called when active (or
222 * rather, we'll have bailed in task_function_call() or the
223 * above ctx->task != current test), therefore we must have
224 * ctx->is_active here.
226 WARN_ON_ONCE(!ctx
->is_active
);
228 * And since we have ctx->is_active, cpuctx->task_ctx must
231 WARN_ON_ONCE(task_ctx
!= ctx
);
233 WARN_ON_ONCE(&cpuctx
->ctx
!= ctx
);
236 efs
->func(event
, cpuctx
, ctx
, efs
->data
);
238 perf_ctx_unlock(cpuctx
, task_ctx
);
243 static void event_function_local(struct perf_event
*event
, event_f func
, void *data
)
245 struct event_function_struct efs
= {
251 int ret
= event_function(&efs
);
255 static void event_function_call(struct perf_event
*event
, event_f func
, void *data
)
257 struct perf_event_context
*ctx
= event
->ctx
;
258 struct task_struct
*task
= READ_ONCE(ctx
->task
); /* verified in event_function */
259 struct event_function_struct efs
= {
265 if (!event
->parent
) {
267 * If this is a !child event, we must hold ctx::mutex to
268 * stabilize the the event->ctx relation. See
269 * perf_event_ctx_lock().
271 lockdep_assert_held(&ctx
->mutex
);
275 cpu_function_call(event
->cpu
, event_function
, &efs
);
279 if (task
== TASK_TOMBSTONE
)
283 if (!task_function_call(task
, event_function
, &efs
))
286 raw_spin_lock_irq(&ctx
->lock
);
288 * Reload the task pointer, it might have been changed by
289 * a concurrent perf_event_context_sched_out().
292 if (task
== TASK_TOMBSTONE
) {
293 raw_spin_unlock_irq(&ctx
->lock
);
296 if (ctx
->is_active
) {
297 raw_spin_unlock_irq(&ctx
->lock
);
300 func(event
, NULL
, ctx
, data
);
301 raw_spin_unlock_irq(&ctx
->lock
);
304 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
305 PERF_FLAG_FD_OUTPUT |\
306 PERF_FLAG_PID_CGROUP |\
307 PERF_FLAG_FD_CLOEXEC)
310 * branch priv levels that need permission checks
312 #define PERF_SAMPLE_BRANCH_PERM_PLM \
313 (PERF_SAMPLE_BRANCH_KERNEL |\
314 PERF_SAMPLE_BRANCH_HV)
317 EVENT_FLEXIBLE
= 0x1,
320 EVENT_ALL
= EVENT_FLEXIBLE
| EVENT_PINNED
,
324 * perf_sched_events : >0 events exist
325 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
328 static void perf_sched_delayed(struct work_struct
*work
);
329 DEFINE_STATIC_KEY_FALSE(perf_sched_events
);
330 static DECLARE_DELAYED_WORK(perf_sched_work
, perf_sched_delayed
);
331 static DEFINE_MUTEX(perf_sched_mutex
);
332 static atomic_t perf_sched_count
;
334 static DEFINE_PER_CPU(atomic_t
, perf_cgroup_events
);
335 static DEFINE_PER_CPU(int, perf_sched_cb_usages
);
337 static atomic_t nr_mmap_events __read_mostly
;
338 static atomic_t nr_comm_events __read_mostly
;
339 static atomic_t nr_task_events __read_mostly
;
340 static atomic_t nr_freq_events __read_mostly
;
341 static atomic_t nr_switch_events __read_mostly
;
343 static LIST_HEAD(pmus
);
344 static DEFINE_MUTEX(pmus_lock
);
345 static struct srcu_struct pmus_srcu
;
348 * perf event paranoia level:
349 * -1 - not paranoid at all
350 * 0 - disallow raw tracepoint access for unpriv
351 * 1 - disallow cpu events for unpriv
352 * 2 - disallow kernel profiling for unpriv
354 int sysctl_perf_event_paranoid __read_mostly
= 1;
356 /* Minimum for 512 kiB + 1 user control page */
357 int sysctl_perf_event_mlock __read_mostly
= 512 + (PAGE_SIZE
/ 1024); /* 'free' kiB per user */
360 * max perf event sample rate
362 #define DEFAULT_MAX_SAMPLE_RATE 100000
363 #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
364 #define DEFAULT_CPU_TIME_MAX_PERCENT 25
366 int sysctl_perf_event_sample_rate __read_mostly
= DEFAULT_MAX_SAMPLE_RATE
;
368 static int max_samples_per_tick __read_mostly
= DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE
, HZ
);
369 static int perf_sample_period_ns __read_mostly
= DEFAULT_SAMPLE_PERIOD_NS
;
371 static int perf_sample_allowed_ns __read_mostly
=
372 DEFAULT_SAMPLE_PERIOD_NS
* DEFAULT_CPU_TIME_MAX_PERCENT
/ 100;
374 static void update_perf_cpu_limits(void)
376 u64 tmp
= perf_sample_period_ns
;
378 tmp
*= sysctl_perf_cpu_time_max_percent
;
380 ACCESS_ONCE(perf_sample_allowed_ns
) = tmp
;
383 static int perf_rotate_context(struct perf_cpu_context
*cpuctx
);
385 int perf_proc_update_handler(struct ctl_table
*table
, int write
,
386 void __user
*buffer
, size_t *lenp
,
389 int ret
= proc_dointvec_minmax(table
, write
, buffer
, lenp
, ppos
);
394 max_samples_per_tick
= DIV_ROUND_UP(sysctl_perf_event_sample_rate
, HZ
);
395 perf_sample_period_ns
= NSEC_PER_SEC
/ sysctl_perf_event_sample_rate
;
396 update_perf_cpu_limits();
401 int sysctl_perf_cpu_time_max_percent __read_mostly
= DEFAULT_CPU_TIME_MAX_PERCENT
;
403 int perf_cpu_time_max_percent_handler(struct ctl_table
*table
, int write
,
404 void __user
*buffer
, size_t *lenp
,
407 int ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
412 update_perf_cpu_limits();
418 * perf samples are done in some very critical code paths (NMIs).
419 * If they take too much CPU time, the system can lock up and not
420 * get any real work done. This will drop the sample rate when
421 * we detect that events are taking too long.
423 #define NR_ACCUMULATED_SAMPLES 128
424 static DEFINE_PER_CPU(u64
, running_sample_length
);
426 static void perf_duration_warn(struct irq_work
*w
)
428 u64 allowed_ns
= ACCESS_ONCE(perf_sample_allowed_ns
);
429 u64 avg_local_sample_len
;
430 u64 local_samples_len
;
432 local_samples_len
= __this_cpu_read(running_sample_length
);
433 avg_local_sample_len
= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
435 printk_ratelimited(KERN_WARNING
436 "perf interrupt took too long (%lld > %lld), lowering "
437 "kernel.perf_event_max_sample_rate to %d\n",
438 avg_local_sample_len
, allowed_ns
>> 1,
439 sysctl_perf_event_sample_rate
);
442 static DEFINE_IRQ_WORK(perf_duration_work
, perf_duration_warn
);
444 void perf_sample_event_took(u64 sample_len_ns
)
446 u64 allowed_ns
= ACCESS_ONCE(perf_sample_allowed_ns
);
447 u64 avg_local_sample_len
;
448 u64 local_samples_len
;
453 /* decay the counter by 1 average sample */
454 local_samples_len
= __this_cpu_read(running_sample_length
);
455 local_samples_len
-= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
456 local_samples_len
+= sample_len_ns
;
457 __this_cpu_write(running_sample_length
, local_samples_len
);
460 * note: this will be biased artifically low until we have
461 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
462 * from having to maintain a count.
464 avg_local_sample_len
= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
466 if (avg_local_sample_len
<= allowed_ns
)
469 if (max_samples_per_tick
<= 1)
472 max_samples_per_tick
= DIV_ROUND_UP(max_samples_per_tick
, 2);
473 sysctl_perf_event_sample_rate
= max_samples_per_tick
* HZ
;
474 perf_sample_period_ns
= NSEC_PER_SEC
/ sysctl_perf_event_sample_rate
;
476 update_perf_cpu_limits();
478 if (!irq_work_queue(&perf_duration_work
)) {
479 early_printk("perf interrupt took too long (%lld > %lld), lowering "
480 "kernel.perf_event_max_sample_rate to %d\n",
481 avg_local_sample_len
, allowed_ns
>> 1,
482 sysctl_perf_event_sample_rate
);
486 static atomic64_t perf_event_id
;
488 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
489 enum event_type_t event_type
);
491 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
492 enum event_type_t event_type
,
493 struct task_struct
*task
);
495 static void update_context_time(struct perf_event_context
*ctx
);
496 static u64
perf_event_time(struct perf_event
*event
);
498 void __weak
perf_event_print_debug(void) { }
500 extern __weak
const char *perf_pmu_name(void)
505 static inline u64
perf_clock(void)
507 return local_clock();
510 static inline u64
perf_event_clock(struct perf_event
*event
)
512 return event
->clock();
515 #ifdef CONFIG_CGROUP_PERF
518 perf_cgroup_match(struct perf_event
*event
)
520 struct perf_event_context
*ctx
= event
->ctx
;
521 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
523 /* @event doesn't care about cgroup */
527 /* wants specific cgroup scope but @cpuctx isn't associated with any */
532 * Cgroup scoping is recursive. An event enabled for a cgroup is
533 * also enabled for all its descendant cgroups. If @cpuctx's
534 * cgroup is a descendant of @event's (the test covers identity
535 * case), it's a match.
537 return cgroup_is_descendant(cpuctx
->cgrp
->css
.cgroup
,
538 event
->cgrp
->css
.cgroup
);
541 static inline void perf_detach_cgroup(struct perf_event
*event
)
543 css_put(&event
->cgrp
->css
);
547 static inline int is_cgroup_event(struct perf_event
*event
)
549 return event
->cgrp
!= NULL
;
552 static inline u64
perf_cgroup_event_time(struct perf_event
*event
)
554 struct perf_cgroup_info
*t
;
556 t
= per_cpu_ptr(event
->cgrp
->info
, event
->cpu
);
560 static inline void __update_cgrp_time(struct perf_cgroup
*cgrp
)
562 struct perf_cgroup_info
*info
;
567 info
= this_cpu_ptr(cgrp
->info
);
569 info
->time
+= now
- info
->timestamp
;
570 info
->timestamp
= now
;
573 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context
*cpuctx
)
575 struct perf_cgroup
*cgrp_out
= cpuctx
->cgrp
;
577 __update_cgrp_time(cgrp_out
);
580 static inline void update_cgrp_time_from_event(struct perf_event
*event
)
582 struct perf_cgroup
*cgrp
;
585 * ensure we access cgroup data only when needed and
586 * when we know the cgroup is pinned (css_get)
588 if (!is_cgroup_event(event
))
591 cgrp
= perf_cgroup_from_task(current
, event
->ctx
);
593 * Do not update time when cgroup is not active
595 if (cgrp
== event
->cgrp
)
596 __update_cgrp_time(event
->cgrp
);
600 perf_cgroup_set_timestamp(struct task_struct
*task
,
601 struct perf_event_context
*ctx
)
603 struct perf_cgroup
*cgrp
;
604 struct perf_cgroup_info
*info
;
607 * ctx->lock held by caller
608 * ensure we do not access cgroup data
609 * unless we have the cgroup pinned (css_get)
611 if (!task
|| !ctx
->nr_cgroups
)
614 cgrp
= perf_cgroup_from_task(task
, ctx
);
615 info
= this_cpu_ptr(cgrp
->info
);
616 info
->timestamp
= ctx
->timestamp
;
619 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
620 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
623 * reschedule events based on the cgroup constraint of task.
625 * mode SWOUT : schedule out everything
626 * mode SWIN : schedule in based on cgroup for next
628 static void perf_cgroup_switch(struct task_struct
*task
, int mode
)
630 struct perf_cpu_context
*cpuctx
;
635 * disable interrupts to avoid geting nr_cgroup
636 * changes via __perf_event_disable(). Also
639 local_irq_save(flags
);
642 * we reschedule only in the presence of cgroup
643 * constrained events.
646 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
647 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
648 if (cpuctx
->unique_pmu
!= pmu
)
649 continue; /* ensure we process each cpuctx once */
652 * perf_cgroup_events says at least one
653 * context on this CPU has cgroup events.
655 * ctx->nr_cgroups reports the number of cgroup
656 * events for a context.
658 if (cpuctx
->ctx
.nr_cgroups
> 0) {
659 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
660 perf_pmu_disable(cpuctx
->ctx
.pmu
);
662 if (mode
& PERF_CGROUP_SWOUT
) {
663 cpu_ctx_sched_out(cpuctx
, EVENT_ALL
);
665 * must not be done before ctxswout due
666 * to event_filter_match() in event_sched_out()
671 if (mode
& PERF_CGROUP_SWIN
) {
672 WARN_ON_ONCE(cpuctx
->cgrp
);
674 * set cgrp before ctxsw in to allow
675 * event_filter_match() to not have to pass
677 * we pass the cpuctx->ctx to perf_cgroup_from_task()
678 * because cgorup events are only per-cpu
680 cpuctx
->cgrp
= perf_cgroup_from_task(task
, &cpuctx
->ctx
);
681 cpu_ctx_sched_in(cpuctx
, EVENT_ALL
, task
);
683 perf_pmu_enable(cpuctx
->ctx
.pmu
);
684 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
688 local_irq_restore(flags
);
691 static inline void perf_cgroup_sched_out(struct task_struct
*task
,
692 struct task_struct
*next
)
694 struct perf_cgroup
*cgrp1
;
695 struct perf_cgroup
*cgrp2
= NULL
;
699 * we come here when we know perf_cgroup_events > 0
700 * we do not need to pass the ctx here because we know
701 * we are holding the rcu lock
703 cgrp1
= perf_cgroup_from_task(task
, NULL
);
704 cgrp2
= perf_cgroup_from_task(next
, NULL
);
707 * only schedule out current cgroup events if we know
708 * that we are switching to a different cgroup. Otherwise,
709 * do no touch the cgroup events.
712 perf_cgroup_switch(task
, PERF_CGROUP_SWOUT
);
717 static inline void perf_cgroup_sched_in(struct task_struct
*prev
,
718 struct task_struct
*task
)
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(prev
, NULL
);
733 * only need to schedule in cgroup events if we are changing
734 * cgroup during ctxsw. Cgroup events were not scheduled
735 * out of ctxsw out if that was not the case.
738 perf_cgroup_switch(task
, PERF_CGROUP_SWIN
);
743 static inline int perf_cgroup_connect(int fd
, struct perf_event
*event
,
744 struct perf_event_attr
*attr
,
745 struct perf_event
*group_leader
)
747 struct perf_cgroup
*cgrp
;
748 struct cgroup_subsys_state
*css
;
749 struct fd f
= fdget(fd
);
755 css
= css_tryget_online_from_dir(f
.file
->f_path
.dentry
,
756 &perf_event_cgrp_subsys
);
762 cgrp
= container_of(css
, struct perf_cgroup
, css
);
766 * all events in a group must monitor
767 * the same cgroup because a task belongs
768 * to only one perf cgroup at a time
770 if (group_leader
&& group_leader
->cgrp
!= cgrp
) {
771 perf_detach_cgroup(event
);
780 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
782 struct perf_cgroup_info
*t
;
783 t
= per_cpu_ptr(event
->cgrp
->info
, event
->cpu
);
784 event
->shadow_ctx_time
= now
- t
->timestamp
;
788 perf_cgroup_defer_enabled(struct perf_event
*event
)
791 * when the current task's perf cgroup does not match
792 * the event's, we need to remember to call the
793 * perf_mark_enable() function the first time a task with
794 * a matching perf cgroup is scheduled in.
796 if (is_cgroup_event(event
) && !perf_cgroup_match(event
))
797 event
->cgrp_defer_enabled
= 1;
801 perf_cgroup_mark_enabled(struct perf_event
*event
,
802 struct perf_event_context
*ctx
)
804 struct perf_event
*sub
;
805 u64 tstamp
= perf_event_time(event
);
807 if (!event
->cgrp_defer_enabled
)
810 event
->cgrp_defer_enabled
= 0;
812 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
813 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
814 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
) {
815 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
816 sub
->cgrp_defer_enabled
= 0;
820 #else /* !CONFIG_CGROUP_PERF */
823 perf_cgroup_match(struct perf_event
*event
)
828 static inline void perf_detach_cgroup(struct perf_event
*event
)
831 static inline int is_cgroup_event(struct perf_event
*event
)
836 static inline u64
perf_cgroup_event_cgrp_time(struct perf_event
*event
)
841 static inline void update_cgrp_time_from_event(struct perf_event
*event
)
845 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context
*cpuctx
)
849 static inline void perf_cgroup_sched_out(struct task_struct
*task
,
850 struct task_struct
*next
)
854 static inline void perf_cgroup_sched_in(struct task_struct
*prev
,
855 struct task_struct
*task
)
859 static inline int perf_cgroup_connect(pid_t pid
, struct perf_event
*event
,
860 struct perf_event_attr
*attr
,
861 struct perf_event
*group_leader
)
867 perf_cgroup_set_timestamp(struct task_struct
*task
,
868 struct perf_event_context
*ctx
)
873 perf_cgroup_switch(struct task_struct
*task
, struct task_struct
*next
)
878 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
882 static inline u64
perf_cgroup_event_time(struct perf_event
*event
)
888 perf_cgroup_defer_enabled(struct perf_event
*event
)
893 perf_cgroup_mark_enabled(struct perf_event
*event
,
894 struct perf_event_context
*ctx
)
900 * set default to be dependent on timer tick just
903 #define PERF_CPU_HRTIMER (1000 / HZ)
905 * function must be called with interrupts disbled
907 static enum hrtimer_restart
perf_mux_hrtimer_handler(struct hrtimer
*hr
)
909 struct perf_cpu_context
*cpuctx
;
912 WARN_ON(!irqs_disabled());
914 cpuctx
= container_of(hr
, struct perf_cpu_context
, hrtimer
);
915 rotations
= perf_rotate_context(cpuctx
);
917 raw_spin_lock(&cpuctx
->hrtimer_lock
);
919 hrtimer_forward_now(hr
, cpuctx
->hrtimer_interval
);
921 cpuctx
->hrtimer_active
= 0;
922 raw_spin_unlock(&cpuctx
->hrtimer_lock
);
924 return rotations
? HRTIMER_RESTART
: HRTIMER_NORESTART
;
927 static void __perf_mux_hrtimer_init(struct perf_cpu_context
*cpuctx
, int cpu
)
929 struct hrtimer
*timer
= &cpuctx
->hrtimer
;
930 struct pmu
*pmu
= cpuctx
->ctx
.pmu
;
933 /* no multiplexing needed for SW PMU */
934 if (pmu
->task_ctx_nr
== perf_sw_context
)
938 * check default is sane, if not set then force to
939 * default interval (1/tick)
941 interval
= pmu
->hrtimer_interval_ms
;
943 interval
= pmu
->hrtimer_interval_ms
= PERF_CPU_HRTIMER
;
945 cpuctx
->hrtimer_interval
= ns_to_ktime(NSEC_PER_MSEC
* interval
);
947 raw_spin_lock_init(&cpuctx
->hrtimer_lock
);
948 hrtimer_init(timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS_PINNED
);
949 timer
->function
= perf_mux_hrtimer_handler
;
952 static int perf_mux_hrtimer_restart(struct perf_cpu_context
*cpuctx
)
954 struct hrtimer
*timer
= &cpuctx
->hrtimer
;
955 struct pmu
*pmu
= cpuctx
->ctx
.pmu
;
959 if (pmu
->task_ctx_nr
== perf_sw_context
)
962 raw_spin_lock_irqsave(&cpuctx
->hrtimer_lock
, flags
);
963 if (!cpuctx
->hrtimer_active
) {
964 cpuctx
->hrtimer_active
= 1;
965 hrtimer_forward_now(timer
, cpuctx
->hrtimer_interval
);
966 hrtimer_start_expires(timer
, HRTIMER_MODE_ABS_PINNED
);
968 raw_spin_unlock_irqrestore(&cpuctx
->hrtimer_lock
, flags
);
973 void perf_pmu_disable(struct pmu
*pmu
)
975 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
977 pmu
->pmu_disable(pmu
);
980 void perf_pmu_enable(struct pmu
*pmu
)
982 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
984 pmu
->pmu_enable(pmu
);
987 static DEFINE_PER_CPU(struct list_head
, active_ctx_list
);
990 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
991 * perf_event_task_tick() are fully serialized because they're strictly cpu
992 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
993 * disabled, while perf_event_task_tick is called from IRQ context.
995 static void perf_event_ctx_activate(struct perf_event_context
*ctx
)
997 struct list_head
*head
= this_cpu_ptr(&active_ctx_list
);
999 WARN_ON(!irqs_disabled());
1001 WARN_ON(!list_empty(&ctx
->active_ctx_list
));
1003 list_add(&ctx
->active_ctx_list
, head
);
1006 static void perf_event_ctx_deactivate(struct perf_event_context
*ctx
)
1008 WARN_ON(!irqs_disabled());
1010 WARN_ON(list_empty(&ctx
->active_ctx_list
));
1012 list_del_init(&ctx
->active_ctx_list
);
1015 static void get_ctx(struct perf_event_context
*ctx
)
1017 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
1020 static void free_ctx(struct rcu_head
*head
)
1022 struct perf_event_context
*ctx
;
1024 ctx
= container_of(head
, struct perf_event_context
, rcu_head
);
1025 kfree(ctx
->task_ctx_data
);
1029 static void put_ctx(struct perf_event_context
*ctx
)
1031 if (atomic_dec_and_test(&ctx
->refcount
)) {
1032 if (ctx
->parent_ctx
)
1033 put_ctx(ctx
->parent_ctx
);
1034 if (ctx
->task
&& ctx
->task
!= TASK_TOMBSTONE
)
1035 put_task_struct(ctx
->task
);
1036 call_rcu(&ctx
->rcu_head
, free_ctx
);
1041 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1042 * perf_pmu_migrate_context() we need some magic.
1044 * Those places that change perf_event::ctx will hold both
1045 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1047 * Lock ordering is by mutex address. There are two other sites where
1048 * perf_event_context::mutex nests and those are:
1050 * - perf_event_exit_task_context() [ child , 0 ]
1051 * perf_event_exit_event()
1052 * put_event() [ parent, 1 ]
1054 * - perf_event_init_context() [ parent, 0 ]
1055 * inherit_task_group()
1058 * perf_event_alloc()
1060 * perf_try_init_event() [ child , 1 ]
1062 * While it appears there is an obvious deadlock here -- the parent and child
1063 * nesting levels are inverted between the two. This is in fact safe because
1064 * life-time rules separate them. That is an exiting task cannot fork, and a
1065 * spawning task cannot (yet) exit.
1067 * But remember that that these are parent<->child context relations, and
1068 * migration does not affect children, therefore these two orderings should not
1071 * The change in perf_event::ctx does not affect children (as claimed above)
1072 * because the sys_perf_event_open() case will install a new event and break
1073 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1074 * concerned with cpuctx and that doesn't have children.
1076 * The places that change perf_event::ctx will issue:
1078 * perf_remove_from_context();
1079 * synchronize_rcu();
1080 * perf_install_in_context();
1082 * to affect the change. The remove_from_context() + synchronize_rcu() should
1083 * quiesce the event, after which we can install it in the new location. This
1084 * means that only external vectors (perf_fops, prctl) can perturb the event
1085 * while in transit. Therefore all such accessors should also acquire
1086 * perf_event_context::mutex to serialize against this.
1088 * However; because event->ctx can change while we're waiting to acquire
1089 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1093 * task_struct::perf_event_mutex
1094 * perf_event_context::mutex
1095 * perf_event::child_mutex;
1096 * perf_event_context::lock
1097 * perf_event::mmap_mutex
1100 static struct perf_event_context
*
1101 perf_event_ctx_lock_nested(struct perf_event
*event
, int nesting
)
1103 struct perf_event_context
*ctx
;
1107 ctx
= ACCESS_ONCE(event
->ctx
);
1108 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
1114 mutex_lock_nested(&ctx
->mutex
, nesting
);
1115 if (event
->ctx
!= ctx
) {
1116 mutex_unlock(&ctx
->mutex
);
1124 static inline struct perf_event_context
*
1125 perf_event_ctx_lock(struct perf_event
*event
)
1127 return perf_event_ctx_lock_nested(event
, 0);
1130 static void perf_event_ctx_unlock(struct perf_event
*event
,
1131 struct perf_event_context
*ctx
)
1133 mutex_unlock(&ctx
->mutex
);
1138 * This must be done under the ctx->lock, such as to serialize against
1139 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1140 * calling scheduler related locks and ctx->lock nests inside those.
1142 static __must_check
struct perf_event_context
*
1143 unclone_ctx(struct perf_event_context
*ctx
)
1145 struct perf_event_context
*parent_ctx
= ctx
->parent_ctx
;
1147 lockdep_assert_held(&ctx
->lock
);
1150 ctx
->parent_ctx
= NULL
;
1156 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
1159 * only top level events have the pid namespace they were created in
1162 event
= event
->parent
;
1164 return task_tgid_nr_ns(p
, event
->ns
);
1167 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
1170 * only top level events have the pid namespace they were created in
1173 event
= event
->parent
;
1175 return task_pid_nr_ns(p
, event
->ns
);
1179 * If we inherit events we want to return the parent event id
1182 static u64
primary_event_id(struct perf_event
*event
)
1187 id
= event
->parent
->id
;
1193 * Get the perf_event_context for a task and lock it.
1195 * This has to cope with with the fact that until it is locked,
1196 * the context could get moved to another task.
1198 static struct perf_event_context
*
1199 perf_lock_task_context(struct task_struct
*task
, int ctxn
, unsigned long *flags
)
1201 struct perf_event_context
*ctx
;
1205 * One of the few rules of preemptible RCU is that one cannot do
1206 * rcu_read_unlock() while holding a scheduler (or nested) lock when
1207 * part of the read side critical section was irqs-enabled -- see
1208 * rcu_read_unlock_special().
1210 * Since ctx->lock nests under rq->lock we must ensure the entire read
1211 * side critical section has interrupts disabled.
1213 local_irq_save(*flags
);
1215 ctx
= rcu_dereference(task
->perf_event_ctxp
[ctxn
]);
1218 * If this context is a clone of another, it might
1219 * get swapped for another underneath us by
1220 * perf_event_task_sched_out, though the
1221 * rcu_read_lock() protects us from any context
1222 * getting freed. Lock the context and check if it
1223 * got swapped before we could get the lock, and retry
1224 * if so. If we locked the right context, then it
1225 * can't get swapped on us any more.
1227 raw_spin_lock(&ctx
->lock
);
1228 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
[ctxn
])) {
1229 raw_spin_unlock(&ctx
->lock
);
1231 local_irq_restore(*flags
);
1235 if (ctx
->task
== TASK_TOMBSTONE
||
1236 !atomic_inc_not_zero(&ctx
->refcount
)) {
1237 raw_spin_unlock(&ctx
->lock
);
1240 WARN_ON_ONCE(ctx
->task
!= task
);
1245 local_irq_restore(*flags
);
1250 * Get the context for a task and increment its pin_count so it
1251 * can't get swapped to another task. This also increments its
1252 * reference count so that the context can't get freed.
1254 static struct perf_event_context
*
1255 perf_pin_task_context(struct task_struct
*task
, int ctxn
)
1257 struct perf_event_context
*ctx
;
1258 unsigned long flags
;
1260 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
1263 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1268 static void perf_unpin_context(struct perf_event_context
*ctx
)
1270 unsigned long flags
;
1272 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
1274 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1278 * Update the record of the current time in a context.
1280 static void update_context_time(struct perf_event_context
*ctx
)
1282 u64 now
= perf_clock();
1284 ctx
->time
+= now
- ctx
->timestamp
;
1285 ctx
->timestamp
= now
;
1288 static u64
perf_event_time(struct perf_event
*event
)
1290 struct perf_event_context
*ctx
= event
->ctx
;
1292 if (is_cgroup_event(event
))
1293 return perf_cgroup_event_time(event
);
1295 return ctx
? ctx
->time
: 0;
1299 * Update the total_time_enabled and total_time_running fields for a event.
1301 static void update_event_times(struct perf_event
*event
)
1303 struct perf_event_context
*ctx
= event
->ctx
;
1306 lockdep_assert_held(&ctx
->lock
);
1308 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
1309 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
1313 * in cgroup mode, time_enabled represents
1314 * the time the event was enabled AND active
1315 * tasks were in the monitored cgroup. This is
1316 * independent of the activity of the context as
1317 * there may be a mix of cgroup and non-cgroup events.
1319 * That is why we treat cgroup events differently
1322 if (is_cgroup_event(event
))
1323 run_end
= perf_cgroup_event_time(event
);
1324 else if (ctx
->is_active
)
1325 run_end
= ctx
->time
;
1327 run_end
= event
->tstamp_stopped
;
1329 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
1331 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
1332 run_end
= event
->tstamp_stopped
;
1334 run_end
= perf_event_time(event
);
1336 event
->total_time_running
= run_end
- event
->tstamp_running
;
1341 * Update total_time_enabled and total_time_running for all events in a group.
1343 static void update_group_times(struct perf_event
*leader
)
1345 struct perf_event
*event
;
1347 update_event_times(leader
);
1348 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
1349 update_event_times(event
);
1352 static struct list_head
*
1353 ctx_group_list(struct perf_event
*event
, struct perf_event_context
*ctx
)
1355 if (event
->attr
.pinned
)
1356 return &ctx
->pinned_groups
;
1358 return &ctx
->flexible_groups
;
1362 * Add a event from the lists for its context.
1363 * Must be called with ctx->mutex and ctx->lock held.
1366 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
1368 lockdep_assert_held(&ctx
->lock
);
1370 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_CONTEXT
);
1371 event
->attach_state
|= PERF_ATTACH_CONTEXT
;
1374 * If we're a stand alone event or group leader, we go to the context
1375 * list, group events are kept attached to the group so that
1376 * perf_group_detach can, at all times, locate all siblings.
1378 if (event
->group_leader
== event
) {
1379 struct list_head
*list
;
1381 if (is_software_event(event
))
1382 event
->group_flags
|= PERF_GROUP_SOFTWARE
;
1384 list
= ctx_group_list(event
, ctx
);
1385 list_add_tail(&event
->group_entry
, list
);
1388 if (is_cgroup_event(event
))
1391 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
1393 if (event
->attr
.inherit_stat
)
1400 * Initialize event state based on the perf_event_attr::disabled.
1402 static inline void perf_event__state_init(struct perf_event
*event
)
1404 event
->state
= event
->attr
.disabled
? PERF_EVENT_STATE_OFF
:
1405 PERF_EVENT_STATE_INACTIVE
;
1408 static void __perf_event_read_size(struct perf_event
*event
, int nr_siblings
)
1410 int entry
= sizeof(u64
); /* value */
1414 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1415 size
+= sizeof(u64
);
1417 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1418 size
+= sizeof(u64
);
1420 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
1421 entry
+= sizeof(u64
);
1423 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
1425 size
+= sizeof(u64
);
1429 event
->read_size
= size
;
1432 static void __perf_event_header_size(struct perf_event
*event
, u64 sample_type
)
1434 struct perf_sample_data
*data
;
1437 if (sample_type
& PERF_SAMPLE_IP
)
1438 size
+= sizeof(data
->ip
);
1440 if (sample_type
& PERF_SAMPLE_ADDR
)
1441 size
+= sizeof(data
->addr
);
1443 if (sample_type
& PERF_SAMPLE_PERIOD
)
1444 size
+= sizeof(data
->period
);
1446 if (sample_type
& PERF_SAMPLE_WEIGHT
)
1447 size
+= sizeof(data
->weight
);
1449 if (sample_type
& PERF_SAMPLE_READ
)
1450 size
+= event
->read_size
;
1452 if (sample_type
& PERF_SAMPLE_DATA_SRC
)
1453 size
+= sizeof(data
->data_src
.val
);
1455 if (sample_type
& PERF_SAMPLE_TRANSACTION
)
1456 size
+= sizeof(data
->txn
);
1458 event
->header_size
= size
;
1462 * Called at perf_event creation and when events are attached/detached from a
1465 static void perf_event__header_size(struct perf_event
*event
)
1467 __perf_event_read_size(event
,
1468 event
->group_leader
->nr_siblings
);
1469 __perf_event_header_size(event
, event
->attr
.sample_type
);
1472 static void perf_event__id_header_size(struct perf_event
*event
)
1474 struct perf_sample_data
*data
;
1475 u64 sample_type
= event
->attr
.sample_type
;
1478 if (sample_type
& PERF_SAMPLE_TID
)
1479 size
+= sizeof(data
->tid_entry
);
1481 if (sample_type
& PERF_SAMPLE_TIME
)
1482 size
+= sizeof(data
->time
);
1484 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
1485 size
+= sizeof(data
->id
);
1487 if (sample_type
& PERF_SAMPLE_ID
)
1488 size
+= sizeof(data
->id
);
1490 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
1491 size
+= sizeof(data
->stream_id
);
1493 if (sample_type
& PERF_SAMPLE_CPU
)
1494 size
+= sizeof(data
->cpu_entry
);
1496 event
->id_header_size
= size
;
1499 static bool perf_event_validate_size(struct perf_event
*event
)
1502 * The values computed here will be over-written when we actually
1505 __perf_event_read_size(event
, event
->group_leader
->nr_siblings
+ 1);
1506 __perf_event_header_size(event
, event
->attr
.sample_type
& ~PERF_SAMPLE_READ
);
1507 perf_event__id_header_size(event
);
1510 * Sum the lot; should not exceed the 64k limit we have on records.
1511 * Conservative limit to allow for callchains and other variable fields.
1513 if (event
->read_size
+ event
->header_size
+
1514 event
->id_header_size
+ sizeof(struct perf_event_header
) >= 16*1024)
1520 static void perf_group_attach(struct perf_event
*event
)
1522 struct perf_event
*group_leader
= event
->group_leader
, *pos
;
1525 * We can have double attach due to group movement in perf_event_open.
1527 if (event
->attach_state
& PERF_ATTACH_GROUP
)
1530 event
->attach_state
|= PERF_ATTACH_GROUP
;
1532 if (group_leader
== event
)
1535 WARN_ON_ONCE(group_leader
->ctx
!= event
->ctx
);
1537 if (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
&&
1538 !is_software_event(event
))
1539 group_leader
->group_flags
&= ~PERF_GROUP_SOFTWARE
;
1541 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
1542 group_leader
->nr_siblings
++;
1544 perf_event__header_size(group_leader
);
1546 list_for_each_entry(pos
, &group_leader
->sibling_list
, group_entry
)
1547 perf_event__header_size(pos
);
1551 * Remove a event from the lists for its context.
1552 * Must be called with ctx->mutex and ctx->lock held.
1555 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
1557 struct perf_cpu_context
*cpuctx
;
1559 WARN_ON_ONCE(event
->ctx
!= ctx
);
1560 lockdep_assert_held(&ctx
->lock
);
1563 * We can have double detach due to exit/hot-unplug + close.
1565 if (!(event
->attach_state
& PERF_ATTACH_CONTEXT
))
1568 event
->attach_state
&= ~PERF_ATTACH_CONTEXT
;
1570 if (is_cgroup_event(event
)) {
1573 * Because cgroup events are always per-cpu events, this will
1574 * always be called from the right CPU.
1576 cpuctx
= __get_cpu_context(ctx
);
1578 * If there are no more cgroup events then clear cgrp to avoid
1579 * stale pointer in update_cgrp_time_from_cpuctx().
1581 if (!ctx
->nr_cgroups
)
1582 cpuctx
->cgrp
= NULL
;
1586 if (event
->attr
.inherit_stat
)
1589 list_del_rcu(&event
->event_entry
);
1591 if (event
->group_leader
== event
)
1592 list_del_init(&event
->group_entry
);
1594 update_group_times(event
);
1597 * If event was in error state, then keep it
1598 * that way, otherwise bogus counts will be
1599 * returned on read(). The only way to get out
1600 * of error state is by explicit re-enabling
1603 if (event
->state
> PERF_EVENT_STATE_OFF
)
1604 event
->state
= PERF_EVENT_STATE_OFF
;
1609 static void perf_group_detach(struct perf_event
*event
)
1611 struct perf_event
*sibling
, *tmp
;
1612 struct list_head
*list
= NULL
;
1615 * We can have double detach due to exit/hot-unplug + close.
1617 if (!(event
->attach_state
& PERF_ATTACH_GROUP
))
1620 event
->attach_state
&= ~PERF_ATTACH_GROUP
;
1623 * If this is a sibling, remove it from its group.
1625 if (event
->group_leader
!= event
) {
1626 list_del_init(&event
->group_entry
);
1627 event
->group_leader
->nr_siblings
--;
1631 if (!list_empty(&event
->group_entry
))
1632 list
= &event
->group_entry
;
1635 * If this was a group event with sibling events then
1636 * upgrade the siblings to singleton events by adding them
1637 * to whatever list we are on.
1639 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
1641 list_move_tail(&sibling
->group_entry
, list
);
1642 sibling
->group_leader
= sibling
;
1644 /* Inherit group flags from the previous leader */
1645 sibling
->group_flags
= event
->group_flags
;
1647 WARN_ON_ONCE(sibling
->ctx
!= event
->ctx
);
1651 perf_event__header_size(event
->group_leader
);
1653 list_for_each_entry(tmp
, &event
->group_leader
->sibling_list
, group_entry
)
1654 perf_event__header_size(tmp
);
1657 static bool is_orphaned_event(struct perf_event
*event
)
1659 return event
->state
== PERF_EVENT_STATE_DEAD
;
1662 static inline int pmu_filter_match(struct perf_event
*event
)
1664 struct pmu
*pmu
= event
->pmu
;
1665 return pmu
->filter_match
? pmu
->filter_match(event
) : 1;
1669 event_filter_match(struct perf_event
*event
)
1671 return (event
->cpu
== -1 || event
->cpu
== smp_processor_id())
1672 && perf_cgroup_match(event
) && pmu_filter_match(event
);
1676 event_sched_out(struct perf_event
*event
,
1677 struct perf_cpu_context
*cpuctx
,
1678 struct perf_event_context
*ctx
)
1680 u64 tstamp
= perf_event_time(event
);
1683 WARN_ON_ONCE(event
->ctx
!= ctx
);
1684 lockdep_assert_held(&ctx
->lock
);
1687 * An event which could not be activated because of
1688 * filter mismatch still needs to have its timings
1689 * maintained, otherwise bogus information is return
1690 * via read() for time_enabled, time_running:
1692 if (event
->state
== PERF_EVENT_STATE_INACTIVE
1693 && !event_filter_match(event
)) {
1694 delta
= tstamp
- event
->tstamp_stopped
;
1695 event
->tstamp_running
+= delta
;
1696 event
->tstamp_stopped
= tstamp
;
1699 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1702 perf_pmu_disable(event
->pmu
);
1704 event
->tstamp_stopped
= tstamp
;
1705 event
->pmu
->del(event
, 0);
1707 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1708 if (event
->pending_disable
) {
1709 event
->pending_disable
= 0;
1710 event
->state
= PERF_EVENT_STATE_OFF
;
1713 if (!is_software_event(event
))
1714 cpuctx
->active_oncpu
--;
1715 if (!--ctx
->nr_active
)
1716 perf_event_ctx_deactivate(ctx
);
1717 if (event
->attr
.freq
&& event
->attr
.sample_freq
)
1719 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
1720 cpuctx
->exclusive
= 0;
1722 perf_pmu_enable(event
->pmu
);
1726 group_sched_out(struct perf_event
*group_event
,
1727 struct perf_cpu_context
*cpuctx
,
1728 struct perf_event_context
*ctx
)
1730 struct perf_event
*event
;
1731 int state
= group_event
->state
;
1733 event_sched_out(group_event
, cpuctx
, ctx
);
1736 * Schedule out siblings (if any):
1738 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
1739 event_sched_out(event
, cpuctx
, ctx
);
1741 if (state
== PERF_EVENT_STATE_ACTIVE
&& group_event
->attr
.exclusive
)
1742 cpuctx
->exclusive
= 0;
1745 #define DETACH_GROUP 0x01UL
1748 * Cross CPU call to remove a performance event
1750 * We disable the event on the hardware level first. After that we
1751 * remove it from the context list.
1754 __perf_remove_from_context(struct perf_event
*event
,
1755 struct perf_cpu_context
*cpuctx
,
1756 struct perf_event_context
*ctx
,
1759 unsigned long flags
= (unsigned long)info
;
1761 event_sched_out(event
, cpuctx
, ctx
);
1762 if (flags
& DETACH_GROUP
)
1763 perf_group_detach(event
);
1764 list_del_event(event
, ctx
);
1766 if (!ctx
->nr_events
&& ctx
->is_active
) {
1769 WARN_ON_ONCE(cpuctx
->task_ctx
!= ctx
);
1770 cpuctx
->task_ctx
= NULL
;
1776 * Remove the event from a task's (or a CPU's) list of events.
1778 * If event->ctx is a cloned context, callers must make sure that
1779 * every task struct that event->ctx->task could possibly point to
1780 * remains valid. This is OK when called from perf_release since
1781 * that only calls us on the top-level context, which can't be a clone.
1782 * When called from perf_event_exit_task, it's OK because the
1783 * context has been detached from its task.
1785 static void perf_remove_from_context(struct perf_event
*event
, unsigned long flags
)
1787 lockdep_assert_held(&event
->ctx
->mutex
);
1789 event_function_call(event
, __perf_remove_from_context
, (void *)flags
);
1793 * Cross CPU call to disable a performance event
1795 static void __perf_event_disable(struct perf_event
*event
,
1796 struct perf_cpu_context
*cpuctx
,
1797 struct perf_event_context
*ctx
,
1800 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
1803 update_context_time(ctx
);
1804 update_cgrp_time_from_event(event
);
1805 update_group_times(event
);
1806 if (event
== event
->group_leader
)
1807 group_sched_out(event
, cpuctx
, ctx
);
1809 event_sched_out(event
, cpuctx
, ctx
);
1810 event
->state
= PERF_EVENT_STATE_OFF
;
1816 * If event->ctx is a cloned context, callers must make sure that
1817 * every task struct that event->ctx->task could possibly point to
1818 * remains valid. This condition is satisifed when called through
1819 * perf_event_for_each_child or perf_event_for_each because they
1820 * hold the top-level event's child_mutex, so any descendant that
1821 * goes to exit will block in perf_event_exit_event().
1823 * When called from perf_pending_event it's OK because event->ctx
1824 * is the current context on this CPU and preemption is disabled,
1825 * hence we can't get into perf_event_task_sched_out for this context.
1827 static void _perf_event_disable(struct perf_event
*event
)
1829 struct perf_event_context
*ctx
= event
->ctx
;
1831 raw_spin_lock_irq(&ctx
->lock
);
1832 if (event
->state
<= PERF_EVENT_STATE_OFF
) {
1833 raw_spin_unlock_irq(&ctx
->lock
);
1836 raw_spin_unlock_irq(&ctx
->lock
);
1838 event_function_call(event
, __perf_event_disable
, NULL
);
1841 void perf_event_disable_local(struct perf_event
*event
)
1843 event_function_local(event
, __perf_event_disable
, NULL
);
1847 * Strictly speaking kernel users cannot create groups and therefore this
1848 * interface does not need the perf_event_ctx_lock() magic.
1850 void perf_event_disable(struct perf_event
*event
)
1852 struct perf_event_context
*ctx
;
1854 ctx
= perf_event_ctx_lock(event
);
1855 _perf_event_disable(event
);
1856 perf_event_ctx_unlock(event
, ctx
);
1858 EXPORT_SYMBOL_GPL(perf_event_disable
);
1860 static void perf_set_shadow_time(struct perf_event
*event
,
1861 struct perf_event_context
*ctx
,
1865 * use the correct time source for the time snapshot
1867 * We could get by without this by leveraging the
1868 * fact that to get to this function, the caller
1869 * has most likely already called update_context_time()
1870 * and update_cgrp_time_xx() and thus both timestamp
1871 * are identical (or very close). Given that tstamp is,
1872 * already adjusted for cgroup, we could say that:
1873 * tstamp - ctx->timestamp
1875 * tstamp - cgrp->timestamp.
1877 * Then, in perf_output_read(), the calculation would
1878 * work with no changes because:
1879 * - event is guaranteed scheduled in
1880 * - no scheduled out in between
1881 * - thus the timestamp would be the same
1883 * But this is a bit hairy.
1885 * So instead, we have an explicit cgroup call to remain
1886 * within the time time source all along. We believe it
1887 * is cleaner and simpler to understand.
1889 if (is_cgroup_event(event
))
1890 perf_cgroup_set_shadow_time(event
, tstamp
);
1892 event
->shadow_ctx_time
= tstamp
- ctx
->timestamp
;
1895 #define MAX_INTERRUPTS (~0ULL)
1897 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1898 static void perf_log_itrace_start(struct perf_event
*event
);
1901 event_sched_in(struct perf_event
*event
,
1902 struct perf_cpu_context
*cpuctx
,
1903 struct perf_event_context
*ctx
)
1905 u64 tstamp
= perf_event_time(event
);
1908 lockdep_assert_held(&ctx
->lock
);
1910 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1913 event
->state
= PERF_EVENT_STATE_ACTIVE
;
1914 event
->oncpu
= smp_processor_id();
1917 * Unthrottle events, since we scheduled we might have missed several
1918 * ticks already, also for a heavily scheduling task there is little
1919 * guarantee it'll get a tick in a timely manner.
1921 if (unlikely(event
->hw
.interrupts
== MAX_INTERRUPTS
)) {
1922 perf_log_throttle(event
, 1);
1923 event
->hw
.interrupts
= 0;
1927 * The new state must be visible before we turn it on in the hardware:
1931 perf_pmu_disable(event
->pmu
);
1933 perf_set_shadow_time(event
, ctx
, tstamp
);
1935 perf_log_itrace_start(event
);
1937 if (event
->pmu
->add(event
, PERF_EF_START
)) {
1938 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1944 event
->tstamp_running
+= tstamp
- event
->tstamp_stopped
;
1946 if (!is_software_event(event
))
1947 cpuctx
->active_oncpu
++;
1948 if (!ctx
->nr_active
++)
1949 perf_event_ctx_activate(ctx
);
1950 if (event
->attr
.freq
&& event
->attr
.sample_freq
)
1953 if (event
->attr
.exclusive
)
1954 cpuctx
->exclusive
= 1;
1957 perf_pmu_enable(event
->pmu
);
1963 group_sched_in(struct perf_event
*group_event
,
1964 struct perf_cpu_context
*cpuctx
,
1965 struct perf_event_context
*ctx
)
1967 struct perf_event
*event
, *partial_group
= NULL
;
1968 struct pmu
*pmu
= ctx
->pmu
;
1969 u64 now
= ctx
->time
;
1970 bool simulate
= false;
1972 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
1975 pmu
->start_txn(pmu
, PERF_PMU_TXN_ADD
);
1977 if (event_sched_in(group_event
, cpuctx
, ctx
)) {
1978 pmu
->cancel_txn(pmu
);
1979 perf_mux_hrtimer_restart(cpuctx
);
1984 * Schedule in siblings as one group (if any):
1986 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
1987 if (event_sched_in(event
, cpuctx
, ctx
)) {
1988 partial_group
= event
;
1993 if (!pmu
->commit_txn(pmu
))
1998 * Groups can be scheduled in as one unit only, so undo any
1999 * partial group before returning:
2000 * The events up to the failed event are scheduled out normally,
2001 * tstamp_stopped will be updated.
2003 * The failed events and the remaining siblings need to have
2004 * their timings updated as if they had gone thru event_sched_in()
2005 * and event_sched_out(). This is required to get consistent timings
2006 * across the group. This also takes care of the case where the group
2007 * could never be scheduled by ensuring tstamp_stopped is set to mark
2008 * the time the event was actually stopped, such that time delta
2009 * calculation in update_event_times() is correct.
2011 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
2012 if (event
== partial_group
)
2016 event
->tstamp_running
+= now
- event
->tstamp_stopped
;
2017 event
->tstamp_stopped
= now
;
2019 event_sched_out(event
, cpuctx
, ctx
);
2022 event_sched_out(group_event
, cpuctx
, ctx
);
2024 pmu
->cancel_txn(pmu
);
2026 perf_mux_hrtimer_restart(cpuctx
);
2032 * Work out whether we can put this event group on the CPU now.
2034 static int group_can_go_on(struct perf_event
*event
,
2035 struct perf_cpu_context
*cpuctx
,
2039 * Groups consisting entirely of software events can always go on.
2041 if (event
->group_flags
& PERF_GROUP_SOFTWARE
)
2044 * If an exclusive group is already on, no other hardware
2047 if (cpuctx
->exclusive
)
2050 * If this group is exclusive and there are already
2051 * events on the CPU, it can't go on.
2053 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
2056 * Otherwise, try to add it if all previous groups were able
2062 static void add_event_to_ctx(struct perf_event
*event
,
2063 struct perf_event_context
*ctx
)
2065 u64 tstamp
= perf_event_time(event
);
2067 list_add_event(event
, ctx
);
2068 perf_group_attach(event
);
2069 event
->tstamp_enabled
= tstamp
;
2070 event
->tstamp_running
= tstamp
;
2071 event
->tstamp_stopped
= tstamp
;
2074 static void ctx_sched_out(struct perf_event_context
*ctx
,
2075 struct perf_cpu_context
*cpuctx
,
2076 enum event_type_t event_type
);
2078 ctx_sched_in(struct perf_event_context
*ctx
,
2079 struct perf_cpu_context
*cpuctx
,
2080 enum event_type_t event_type
,
2081 struct task_struct
*task
);
2083 static void task_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
2084 struct perf_event_context
*ctx
)
2086 if (!cpuctx
->task_ctx
)
2089 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
2092 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
2095 static void perf_event_sched_in(struct perf_cpu_context
*cpuctx
,
2096 struct perf_event_context
*ctx
,
2097 struct task_struct
*task
)
2099 cpu_ctx_sched_in(cpuctx
, EVENT_PINNED
, task
);
2101 ctx_sched_in(ctx
, cpuctx
, EVENT_PINNED
, task
);
2102 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
, task
);
2104 ctx_sched_in(ctx
, cpuctx
, EVENT_FLEXIBLE
, task
);
2107 static void ctx_resched(struct perf_cpu_context
*cpuctx
,
2108 struct perf_event_context
*task_ctx
)
2110 perf_pmu_disable(cpuctx
->ctx
.pmu
);
2112 task_ctx_sched_out(cpuctx
, task_ctx
);
2113 cpu_ctx_sched_out(cpuctx
, EVENT_ALL
);
2114 perf_event_sched_in(cpuctx
, task_ctx
, current
);
2115 perf_pmu_enable(cpuctx
->ctx
.pmu
);
2119 * Cross CPU call to install and enable a performance event
2121 * Very similar to remote_function() + event_function() but cannot assume that
2122 * things like ctx->is_active and cpuctx->task_ctx are set.
2124 static int __perf_install_in_context(void *info
)
2126 struct perf_event
*event
= info
;
2127 struct perf_event_context
*ctx
= event
->ctx
;
2128 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
2129 struct perf_event_context
*task_ctx
= cpuctx
->task_ctx
;
2130 bool activate
= true;
2133 raw_spin_lock(&cpuctx
->ctx
.lock
);
2135 raw_spin_lock(&ctx
->lock
);
2138 /* If we're on the wrong CPU, try again */
2139 if (task_cpu(ctx
->task
) != smp_processor_id()) {
2145 * If we're on the right CPU, see if the task we target is
2146 * current, if not we don't have to activate the ctx, a future
2147 * context switch will do that for us.
2149 if (ctx
->task
!= current
)
2152 WARN_ON_ONCE(cpuctx
->task_ctx
&& cpuctx
->task_ctx
!= ctx
);
2154 } else if (task_ctx
) {
2155 raw_spin_lock(&task_ctx
->lock
);
2159 ctx_sched_out(ctx
, cpuctx
, EVENT_TIME
);
2160 add_event_to_ctx(event
, ctx
);
2161 ctx_resched(cpuctx
, task_ctx
);
2163 add_event_to_ctx(event
, ctx
);
2167 perf_ctx_unlock(cpuctx
, task_ctx
);
2173 * Attach a performance event to a context.
2175 * Very similar to event_function_call, see comment there.
2178 perf_install_in_context(struct perf_event_context
*ctx
,
2179 struct perf_event
*event
,
2182 struct task_struct
*task
= READ_ONCE(ctx
->task
);
2184 lockdep_assert_held(&ctx
->mutex
);
2187 if (event
->cpu
!= -1)
2191 cpu_function_call(cpu
, __perf_install_in_context
, event
);
2196 * Should not happen, we validate the ctx is still alive before calling.
2198 if (WARN_ON_ONCE(task
== TASK_TOMBSTONE
))
2202 * Installing events is tricky because we cannot rely on ctx->is_active
2203 * to be set in case this is the nr_events 0 -> 1 transition.
2207 * Cannot use task_function_call() because we need to run on the task's
2208 * CPU regardless of whether its current or not.
2210 if (!cpu_function_call(task_cpu(task
), __perf_install_in_context
, event
))
2213 raw_spin_lock_irq(&ctx
->lock
);
2215 if (WARN_ON_ONCE(task
== TASK_TOMBSTONE
)) {
2217 * Cannot happen because we already checked above (which also
2218 * cannot happen), and we hold ctx->mutex, which serializes us
2219 * against perf_event_exit_task_context().
2221 raw_spin_unlock_irq(&ctx
->lock
);
2224 raw_spin_unlock_irq(&ctx
->lock
);
2226 * Since !ctx->is_active doesn't mean anything, we must IPI
2233 * Put a event into inactive state and update time fields.
2234 * Enabling the leader of a group effectively enables all
2235 * the group members that aren't explicitly disabled, so we
2236 * have to update their ->tstamp_enabled also.
2237 * Note: this works for group members as well as group leaders
2238 * since the non-leader members' sibling_lists will be empty.
2240 static void __perf_event_mark_enabled(struct perf_event
*event
)
2242 struct perf_event
*sub
;
2243 u64 tstamp
= perf_event_time(event
);
2245 event
->state
= PERF_EVENT_STATE_INACTIVE
;
2246 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
2247 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
2248 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
)
2249 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
2254 * Cross CPU call to enable a performance event
2256 static void __perf_event_enable(struct perf_event
*event
,
2257 struct perf_cpu_context
*cpuctx
,
2258 struct perf_event_context
*ctx
,
2261 struct perf_event
*leader
= event
->group_leader
;
2262 struct perf_event_context
*task_ctx
;
2264 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
||
2265 event
->state
<= PERF_EVENT_STATE_ERROR
)
2269 ctx_sched_out(ctx
, cpuctx
, EVENT_TIME
);
2271 __perf_event_mark_enabled(event
);
2273 if (!ctx
->is_active
)
2276 if (!event_filter_match(event
)) {
2277 if (is_cgroup_event(event
))
2278 perf_cgroup_defer_enabled(event
);
2279 ctx_sched_in(ctx
, cpuctx
, EVENT_TIME
, current
);
2284 * If the event is in a group and isn't the group leader,
2285 * then don't put it on unless the group is on.
2287 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
) {
2288 ctx_sched_in(ctx
, cpuctx
, EVENT_TIME
, current
);
2292 task_ctx
= cpuctx
->task_ctx
;
2294 WARN_ON_ONCE(task_ctx
!= ctx
);
2296 ctx_resched(cpuctx
, task_ctx
);
2302 * If event->ctx is a cloned context, callers must make sure that
2303 * every task struct that event->ctx->task could possibly point to
2304 * remains valid. This condition is satisfied when called through
2305 * perf_event_for_each_child or perf_event_for_each as described
2306 * for perf_event_disable.
2308 static void _perf_event_enable(struct perf_event
*event
)
2310 struct perf_event_context
*ctx
= event
->ctx
;
2312 raw_spin_lock_irq(&ctx
->lock
);
2313 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
||
2314 event
->state
< PERF_EVENT_STATE_ERROR
) {
2315 raw_spin_unlock_irq(&ctx
->lock
);
2320 * If the event is in error state, clear that first.
2322 * That way, if we see the event in error state below, we know that it
2323 * has gone back into error state, as distinct from the task having
2324 * been scheduled away before the cross-call arrived.
2326 if (event
->state
== PERF_EVENT_STATE_ERROR
)
2327 event
->state
= PERF_EVENT_STATE_OFF
;
2328 raw_spin_unlock_irq(&ctx
->lock
);
2330 event_function_call(event
, __perf_event_enable
, NULL
);
2334 * See perf_event_disable();
2336 void perf_event_enable(struct perf_event
*event
)
2338 struct perf_event_context
*ctx
;
2340 ctx
= perf_event_ctx_lock(event
);
2341 _perf_event_enable(event
);
2342 perf_event_ctx_unlock(event
, ctx
);
2344 EXPORT_SYMBOL_GPL(perf_event_enable
);
2346 static int _perf_event_refresh(struct perf_event
*event
, int refresh
)
2349 * not supported on inherited events
2351 if (event
->attr
.inherit
|| !is_sampling_event(event
))
2354 atomic_add(refresh
, &event
->event_limit
);
2355 _perf_event_enable(event
);
2361 * See perf_event_disable()
2363 int perf_event_refresh(struct perf_event
*event
, int refresh
)
2365 struct perf_event_context
*ctx
;
2368 ctx
= perf_event_ctx_lock(event
);
2369 ret
= _perf_event_refresh(event
, refresh
);
2370 perf_event_ctx_unlock(event
, ctx
);
2374 EXPORT_SYMBOL_GPL(perf_event_refresh
);
2376 static void ctx_sched_out(struct perf_event_context
*ctx
,
2377 struct perf_cpu_context
*cpuctx
,
2378 enum event_type_t event_type
)
2380 int is_active
= ctx
->is_active
;
2381 struct perf_event
*event
;
2383 lockdep_assert_held(&ctx
->lock
);
2385 if (likely(!ctx
->nr_events
)) {
2387 * See __perf_remove_from_context().
2389 WARN_ON_ONCE(ctx
->is_active
);
2391 WARN_ON_ONCE(cpuctx
->task_ctx
);
2395 ctx
->is_active
&= ~event_type
;
2396 if (!(ctx
->is_active
& EVENT_ALL
))
2400 WARN_ON_ONCE(cpuctx
->task_ctx
!= ctx
);
2401 if (!ctx
->is_active
)
2402 cpuctx
->task_ctx
= NULL
;
2405 is_active
^= ctx
->is_active
; /* changed bits */
2407 if (is_active
& EVENT_TIME
) {
2408 /* update (and stop) ctx time */
2409 update_context_time(ctx
);
2410 update_cgrp_time_from_cpuctx(cpuctx
);
2413 if (!ctx
->nr_active
|| !(is_active
& EVENT_ALL
))
2416 perf_pmu_disable(ctx
->pmu
);
2417 if (is_active
& EVENT_PINNED
) {
2418 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
)
2419 group_sched_out(event
, cpuctx
, ctx
);
2422 if (is_active
& EVENT_FLEXIBLE
) {
2423 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
)
2424 group_sched_out(event
, cpuctx
, ctx
);
2426 perf_pmu_enable(ctx
->pmu
);
2430 * Test whether two contexts are equivalent, i.e. whether they have both been
2431 * cloned from the same version of the same context.
2433 * Equivalence is measured using a generation number in the context that is
2434 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2435 * and list_del_event().
2437 static int context_equiv(struct perf_event_context
*ctx1
,
2438 struct perf_event_context
*ctx2
)
2440 lockdep_assert_held(&ctx1
->lock
);
2441 lockdep_assert_held(&ctx2
->lock
);
2443 /* Pinning disables the swap optimization */
2444 if (ctx1
->pin_count
|| ctx2
->pin_count
)
2447 /* If ctx1 is the parent of ctx2 */
2448 if (ctx1
== ctx2
->parent_ctx
&& ctx1
->generation
== ctx2
->parent_gen
)
2451 /* If ctx2 is the parent of ctx1 */
2452 if (ctx1
->parent_ctx
== ctx2
&& ctx1
->parent_gen
== ctx2
->generation
)
2456 * If ctx1 and ctx2 have the same parent; we flatten the parent
2457 * hierarchy, see perf_event_init_context().
2459 if (ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
&&
2460 ctx1
->parent_gen
== ctx2
->parent_gen
)
2467 static void __perf_event_sync_stat(struct perf_event
*event
,
2468 struct perf_event
*next_event
)
2472 if (!event
->attr
.inherit_stat
)
2476 * Update the event value, we cannot use perf_event_read()
2477 * because we're in the middle of a context switch and have IRQs
2478 * disabled, which upsets smp_call_function_single(), however
2479 * we know the event must be on the current CPU, therefore we
2480 * don't need to use it.
2482 switch (event
->state
) {
2483 case PERF_EVENT_STATE_ACTIVE
:
2484 event
->pmu
->read(event
);
2487 case PERF_EVENT_STATE_INACTIVE
:
2488 update_event_times(event
);
2496 * In order to keep per-task stats reliable we need to flip the event
2497 * values when we flip the contexts.
2499 value
= local64_read(&next_event
->count
);
2500 value
= local64_xchg(&event
->count
, value
);
2501 local64_set(&next_event
->count
, value
);
2503 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
2504 swap(event
->total_time_running
, next_event
->total_time_running
);
2507 * Since we swizzled the values, update the user visible data too.
2509 perf_event_update_userpage(event
);
2510 perf_event_update_userpage(next_event
);
2513 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
2514 struct perf_event_context
*next_ctx
)
2516 struct perf_event
*event
, *next_event
;
2521 update_context_time(ctx
);
2523 event
= list_first_entry(&ctx
->event_list
,
2524 struct perf_event
, event_entry
);
2526 next_event
= list_first_entry(&next_ctx
->event_list
,
2527 struct perf_event
, event_entry
);
2529 while (&event
->event_entry
!= &ctx
->event_list
&&
2530 &next_event
->event_entry
!= &next_ctx
->event_list
) {
2532 __perf_event_sync_stat(event
, next_event
);
2534 event
= list_next_entry(event
, event_entry
);
2535 next_event
= list_next_entry(next_event
, event_entry
);
2539 static void perf_event_context_sched_out(struct task_struct
*task
, int ctxn
,
2540 struct task_struct
*next
)
2542 struct perf_event_context
*ctx
= task
->perf_event_ctxp
[ctxn
];
2543 struct perf_event_context
*next_ctx
;
2544 struct perf_event_context
*parent
, *next_parent
;
2545 struct perf_cpu_context
*cpuctx
;
2551 cpuctx
= __get_cpu_context(ctx
);
2552 if (!cpuctx
->task_ctx
)
2556 next_ctx
= next
->perf_event_ctxp
[ctxn
];
2560 parent
= rcu_dereference(ctx
->parent_ctx
);
2561 next_parent
= rcu_dereference(next_ctx
->parent_ctx
);
2563 /* If neither context have a parent context; they cannot be clones. */
2564 if (!parent
&& !next_parent
)
2567 if (next_parent
== ctx
|| next_ctx
== parent
|| next_parent
== parent
) {
2569 * Looks like the two contexts are clones, so we might be
2570 * able to optimize the context switch. We lock both
2571 * contexts and check that they are clones under the
2572 * lock (including re-checking that neither has been
2573 * uncloned in the meantime). It doesn't matter which
2574 * order we take the locks because no other cpu could
2575 * be trying to lock both of these tasks.
2577 raw_spin_lock(&ctx
->lock
);
2578 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
2579 if (context_equiv(ctx
, next_ctx
)) {
2580 WRITE_ONCE(ctx
->task
, next
);
2581 WRITE_ONCE(next_ctx
->task
, task
);
2583 swap(ctx
->task_ctx_data
, next_ctx
->task_ctx_data
);
2586 * RCU_INIT_POINTER here is safe because we've not
2587 * modified the ctx and the above modification of
2588 * ctx->task and ctx->task_ctx_data are immaterial
2589 * since those values are always verified under
2590 * ctx->lock which we're now holding.
2592 RCU_INIT_POINTER(task
->perf_event_ctxp
[ctxn
], next_ctx
);
2593 RCU_INIT_POINTER(next
->perf_event_ctxp
[ctxn
], ctx
);
2597 perf_event_sync_stat(ctx
, next_ctx
);
2599 raw_spin_unlock(&next_ctx
->lock
);
2600 raw_spin_unlock(&ctx
->lock
);
2606 raw_spin_lock(&ctx
->lock
);
2607 task_ctx_sched_out(cpuctx
, ctx
);
2608 raw_spin_unlock(&ctx
->lock
);
2612 void perf_sched_cb_dec(struct pmu
*pmu
)
2614 this_cpu_dec(perf_sched_cb_usages
);
2617 void perf_sched_cb_inc(struct pmu
*pmu
)
2619 this_cpu_inc(perf_sched_cb_usages
);
2623 * This function provides the context switch callback to the lower code
2624 * layer. It is invoked ONLY when the context switch callback is enabled.
2626 static void perf_pmu_sched_task(struct task_struct
*prev
,
2627 struct task_struct
*next
,
2630 struct perf_cpu_context
*cpuctx
;
2632 unsigned long flags
;
2637 local_irq_save(flags
);
2641 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
2642 if (pmu
->sched_task
) {
2643 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
2645 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
2647 perf_pmu_disable(pmu
);
2649 pmu
->sched_task(cpuctx
->task_ctx
, sched_in
);
2651 perf_pmu_enable(pmu
);
2653 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
2659 local_irq_restore(flags
);
2662 static void perf_event_switch(struct task_struct
*task
,
2663 struct task_struct
*next_prev
, bool sched_in
);
2665 #define for_each_task_context_nr(ctxn) \
2666 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2669 * Called from scheduler to remove the events of the current task,
2670 * with interrupts disabled.
2672 * We stop each event and update the event value in event->count.
2674 * This does not protect us against NMI, but disable()
2675 * sets the disabled bit in the control field of event _before_
2676 * accessing the event control register. If a NMI hits, then it will
2677 * not restart the event.
2679 void __perf_event_task_sched_out(struct task_struct
*task
,
2680 struct task_struct
*next
)
2684 if (__this_cpu_read(perf_sched_cb_usages
))
2685 perf_pmu_sched_task(task
, next
, false);
2687 if (atomic_read(&nr_switch_events
))
2688 perf_event_switch(task
, next
, false);
2690 for_each_task_context_nr(ctxn
)
2691 perf_event_context_sched_out(task
, ctxn
, next
);
2694 * if cgroup events exist on this CPU, then we need
2695 * to check if we have to switch out PMU state.
2696 * cgroup event are system-wide mode only
2698 if (atomic_read(this_cpu_ptr(&perf_cgroup_events
)))
2699 perf_cgroup_sched_out(task
, next
);
2703 * Called with IRQs disabled
2705 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
2706 enum event_type_t event_type
)
2708 ctx_sched_out(&cpuctx
->ctx
, cpuctx
, event_type
);
2712 ctx_pinned_sched_in(struct perf_event_context
*ctx
,
2713 struct perf_cpu_context
*cpuctx
)
2715 struct perf_event
*event
;
2717 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
2718 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2720 if (!event_filter_match(event
))
2723 /* may need to reset tstamp_enabled */
2724 if (is_cgroup_event(event
))
2725 perf_cgroup_mark_enabled(event
, ctx
);
2727 if (group_can_go_on(event
, cpuctx
, 1))
2728 group_sched_in(event
, cpuctx
, ctx
);
2731 * If this pinned group hasn't been scheduled,
2732 * put it in error state.
2734 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
2735 update_group_times(event
);
2736 event
->state
= PERF_EVENT_STATE_ERROR
;
2742 ctx_flexible_sched_in(struct perf_event_context
*ctx
,
2743 struct perf_cpu_context
*cpuctx
)
2745 struct perf_event
*event
;
2748 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
2749 /* Ignore events in OFF or ERROR state */
2750 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2753 * Listen to the 'cpu' scheduling filter constraint
2756 if (!event_filter_match(event
))
2759 /* may need to reset tstamp_enabled */
2760 if (is_cgroup_event(event
))
2761 perf_cgroup_mark_enabled(event
, ctx
);
2763 if (group_can_go_on(event
, cpuctx
, can_add_hw
)) {
2764 if (group_sched_in(event
, cpuctx
, ctx
))
2771 ctx_sched_in(struct perf_event_context
*ctx
,
2772 struct perf_cpu_context
*cpuctx
,
2773 enum event_type_t event_type
,
2774 struct task_struct
*task
)
2776 int is_active
= ctx
->is_active
;
2779 lockdep_assert_held(&ctx
->lock
);
2781 if (likely(!ctx
->nr_events
))
2784 ctx
->is_active
|= (event_type
| EVENT_TIME
);
2787 cpuctx
->task_ctx
= ctx
;
2789 WARN_ON_ONCE(cpuctx
->task_ctx
!= ctx
);
2792 is_active
^= ctx
->is_active
; /* changed bits */
2794 if (is_active
& EVENT_TIME
) {
2795 /* start ctx time */
2797 ctx
->timestamp
= now
;
2798 perf_cgroup_set_timestamp(task
, ctx
);
2802 * First go through the list and put on any pinned groups
2803 * in order to give them the best chance of going on.
2805 if (is_active
& EVENT_PINNED
)
2806 ctx_pinned_sched_in(ctx
, cpuctx
);
2808 /* Then walk through the lower prio flexible groups */
2809 if (is_active
& EVENT_FLEXIBLE
)
2810 ctx_flexible_sched_in(ctx
, cpuctx
);
2813 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
2814 enum event_type_t event_type
,
2815 struct task_struct
*task
)
2817 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
2819 ctx_sched_in(ctx
, cpuctx
, event_type
, task
);
2822 static void perf_event_context_sched_in(struct perf_event_context
*ctx
,
2823 struct task_struct
*task
)
2825 struct perf_cpu_context
*cpuctx
;
2827 cpuctx
= __get_cpu_context(ctx
);
2828 if (cpuctx
->task_ctx
== ctx
)
2831 perf_ctx_lock(cpuctx
, ctx
);
2832 perf_pmu_disable(ctx
->pmu
);
2834 * We want to keep the following priority order:
2835 * cpu pinned (that don't need to move), task pinned,
2836 * cpu flexible, task flexible.
2838 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
2839 perf_event_sched_in(cpuctx
, ctx
, task
);
2840 perf_pmu_enable(ctx
->pmu
);
2841 perf_ctx_unlock(cpuctx
, ctx
);
2845 * Called from scheduler to add the events of the current task
2846 * with interrupts disabled.
2848 * We restore the event value and then enable it.
2850 * This does not protect us against NMI, but enable()
2851 * sets the enabled bit in the control field of event _before_
2852 * accessing the event control register. If a NMI hits, then it will
2853 * keep the event running.
2855 void __perf_event_task_sched_in(struct task_struct
*prev
,
2856 struct task_struct
*task
)
2858 struct perf_event_context
*ctx
;
2862 * If cgroup events exist on this CPU, then we need to check if we have
2863 * to switch in PMU state; cgroup event are system-wide mode only.
2865 * Since cgroup events are CPU events, we must schedule these in before
2866 * we schedule in the task events.
2868 if (atomic_read(this_cpu_ptr(&perf_cgroup_events
)))
2869 perf_cgroup_sched_in(prev
, task
);
2871 for_each_task_context_nr(ctxn
) {
2872 ctx
= task
->perf_event_ctxp
[ctxn
];
2876 perf_event_context_sched_in(ctx
, task
);
2879 if (atomic_read(&nr_switch_events
))
2880 perf_event_switch(task
, prev
, true);
2882 if (__this_cpu_read(perf_sched_cb_usages
))
2883 perf_pmu_sched_task(prev
, task
, true);
2886 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
2888 u64 frequency
= event
->attr
.sample_freq
;
2889 u64 sec
= NSEC_PER_SEC
;
2890 u64 divisor
, dividend
;
2892 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
2894 count_fls
= fls64(count
);
2895 nsec_fls
= fls64(nsec
);
2896 frequency_fls
= fls64(frequency
);
2900 * We got @count in @nsec, with a target of sample_freq HZ
2901 * the target period becomes:
2904 * period = -------------------
2905 * @nsec * sample_freq
2910 * Reduce accuracy by one bit such that @a and @b converge
2911 * to a similar magnitude.
2913 #define REDUCE_FLS(a, b) \
2915 if (a##_fls > b##_fls) { \
2925 * Reduce accuracy until either term fits in a u64, then proceed with
2926 * the other, so that finally we can do a u64/u64 division.
2928 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
2929 REDUCE_FLS(nsec
, frequency
);
2930 REDUCE_FLS(sec
, count
);
2933 if (count_fls
+ sec_fls
> 64) {
2934 divisor
= nsec
* frequency
;
2936 while (count_fls
+ sec_fls
> 64) {
2937 REDUCE_FLS(count
, sec
);
2941 dividend
= count
* sec
;
2943 dividend
= count
* sec
;
2945 while (nsec_fls
+ frequency_fls
> 64) {
2946 REDUCE_FLS(nsec
, frequency
);
2950 divisor
= nsec
* frequency
;
2956 return div64_u64(dividend
, divisor
);
2959 static DEFINE_PER_CPU(int, perf_throttled_count
);
2960 static DEFINE_PER_CPU(u64
, perf_throttled_seq
);
2962 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
, bool disable
)
2964 struct hw_perf_event
*hwc
= &event
->hw
;
2965 s64 period
, sample_period
;
2968 period
= perf_calculate_period(event
, nsec
, count
);
2970 delta
= (s64
)(period
- hwc
->sample_period
);
2971 delta
= (delta
+ 7) / 8; /* low pass filter */
2973 sample_period
= hwc
->sample_period
+ delta
;
2978 hwc
->sample_period
= sample_period
;
2980 if (local64_read(&hwc
->period_left
) > 8*sample_period
) {
2982 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
2984 local64_set(&hwc
->period_left
, 0);
2987 event
->pmu
->start(event
, PERF_EF_RELOAD
);
2992 * combine freq adjustment with unthrottling to avoid two passes over the
2993 * events. At the same time, make sure, having freq events does not change
2994 * the rate of unthrottling as that would introduce bias.
2996 static void perf_adjust_freq_unthr_context(struct perf_event_context
*ctx
,
2999 struct perf_event
*event
;
3000 struct hw_perf_event
*hwc
;
3001 u64 now
, period
= TICK_NSEC
;
3005 * only need to iterate over all events iff:
3006 * - context have events in frequency mode (needs freq adjust)
3007 * - there are events to unthrottle on this cpu
3009 if (!(ctx
->nr_freq
|| needs_unthr
))
3012 raw_spin_lock(&ctx
->lock
);
3013 perf_pmu_disable(ctx
->pmu
);
3015 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3016 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
3019 if (!event_filter_match(event
))
3022 perf_pmu_disable(event
->pmu
);
3026 if (hwc
->interrupts
== MAX_INTERRUPTS
) {
3027 hwc
->interrupts
= 0;
3028 perf_log_throttle(event
, 1);
3029 event
->pmu
->start(event
, 0);
3032 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
3036 * stop the event and update event->count
3038 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
3040 now
= local64_read(&event
->count
);
3041 delta
= now
- hwc
->freq_count_stamp
;
3042 hwc
->freq_count_stamp
= now
;
3046 * reload only if value has changed
3047 * we have stopped the event so tell that
3048 * to perf_adjust_period() to avoid stopping it
3052 perf_adjust_period(event
, period
, delta
, false);
3054 event
->pmu
->start(event
, delta
> 0 ? PERF_EF_RELOAD
: 0);
3056 perf_pmu_enable(event
->pmu
);
3059 perf_pmu_enable(ctx
->pmu
);
3060 raw_spin_unlock(&ctx
->lock
);
3064 * Round-robin a context's events:
3066 static void rotate_ctx(struct perf_event_context
*ctx
)
3069 * Rotate the first entry last of non-pinned groups. Rotation might be
3070 * disabled by the inheritance code.
3072 if (!ctx
->rotate_disable
)
3073 list_rotate_left(&ctx
->flexible_groups
);
3076 static int perf_rotate_context(struct perf_cpu_context
*cpuctx
)
3078 struct perf_event_context
*ctx
= NULL
;
3081 if (cpuctx
->ctx
.nr_events
) {
3082 if (cpuctx
->ctx
.nr_events
!= cpuctx
->ctx
.nr_active
)
3086 ctx
= cpuctx
->task_ctx
;
3087 if (ctx
&& ctx
->nr_events
) {
3088 if (ctx
->nr_events
!= ctx
->nr_active
)
3095 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
3096 perf_pmu_disable(cpuctx
->ctx
.pmu
);
3098 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
3100 ctx_sched_out(ctx
, cpuctx
, EVENT_FLEXIBLE
);
3102 rotate_ctx(&cpuctx
->ctx
);
3106 perf_event_sched_in(cpuctx
, ctx
, current
);
3108 perf_pmu_enable(cpuctx
->ctx
.pmu
);
3109 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
3115 void perf_event_task_tick(void)
3117 struct list_head
*head
= this_cpu_ptr(&active_ctx_list
);
3118 struct perf_event_context
*ctx
, *tmp
;
3121 WARN_ON(!irqs_disabled());
3123 __this_cpu_inc(perf_throttled_seq
);
3124 throttled
= __this_cpu_xchg(perf_throttled_count
, 0);
3125 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS
);
3127 list_for_each_entry_safe(ctx
, tmp
, head
, active_ctx_list
)
3128 perf_adjust_freq_unthr_context(ctx
, throttled
);
3131 static int event_enable_on_exec(struct perf_event
*event
,
3132 struct perf_event_context
*ctx
)
3134 if (!event
->attr
.enable_on_exec
)
3137 event
->attr
.enable_on_exec
= 0;
3138 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
3141 __perf_event_mark_enabled(event
);
3147 * Enable all of a task's events that have been marked enable-on-exec.
3148 * This expects task == current.
3150 static void perf_event_enable_on_exec(int ctxn
)
3152 struct perf_event_context
*ctx
, *clone_ctx
= NULL
;
3153 struct perf_cpu_context
*cpuctx
;
3154 struct perf_event
*event
;
3155 unsigned long flags
;
3158 local_irq_save(flags
);
3159 ctx
= current
->perf_event_ctxp
[ctxn
];
3160 if (!ctx
|| !ctx
->nr_events
)
3163 cpuctx
= __get_cpu_context(ctx
);
3164 perf_ctx_lock(cpuctx
, ctx
);
3165 ctx_sched_out(ctx
, cpuctx
, EVENT_TIME
);
3166 list_for_each_entry(event
, &ctx
->event_list
, event_entry
)
3167 enabled
|= event_enable_on_exec(event
, ctx
);
3170 * Unclone and reschedule this context if we enabled any event.
3173 clone_ctx
= unclone_ctx(ctx
);
3174 ctx_resched(cpuctx
, ctx
);
3176 perf_ctx_unlock(cpuctx
, ctx
);
3179 local_irq_restore(flags
);
3185 void perf_event_exec(void)
3190 for_each_task_context_nr(ctxn
)
3191 perf_event_enable_on_exec(ctxn
);
3195 struct perf_read_data
{
3196 struct perf_event
*event
;
3202 * Cross CPU call to read the hardware event
3204 static void __perf_event_read(void *info
)
3206 struct perf_read_data
*data
= info
;
3207 struct perf_event
*sub
, *event
= data
->event
;
3208 struct perf_event_context
*ctx
= event
->ctx
;
3209 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
3210 struct pmu
*pmu
= event
->pmu
;
3213 * If this is a task context, we need to check whether it is
3214 * the current task context of this cpu. If not it has been
3215 * scheduled out before the smp call arrived. In that case
3216 * event->count would have been updated to a recent sample
3217 * when the event was scheduled out.
3219 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
3222 raw_spin_lock(&ctx
->lock
);
3223 if (ctx
->is_active
) {
3224 update_context_time(ctx
);
3225 update_cgrp_time_from_event(event
);
3228 update_event_times(event
);
3229 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
3238 pmu
->start_txn(pmu
, PERF_PMU_TXN_READ
);
3242 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
3243 update_event_times(sub
);
3244 if (sub
->state
== PERF_EVENT_STATE_ACTIVE
) {
3246 * Use sibling's PMU rather than @event's since
3247 * sibling could be on different (eg: software) PMU.
3249 sub
->pmu
->read(sub
);
3253 data
->ret
= pmu
->commit_txn(pmu
);
3256 raw_spin_unlock(&ctx
->lock
);
3259 static inline u64
perf_event_count(struct perf_event
*event
)
3261 if (event
->pmu
->count
)
3262 return event
->pmu
->count(event
);
3264 return __perf_event_count(event
);
3268 * NMI-safe method to read a local event, that is an event that
3270 * - either for the current task, or for this CPU
3271 * - does not have inherit set, for inherited task events
3272 * will not be local and we cannot read them atomically
3273 * - must not have a pmu::count method
3275 u64
perf_event_read_local(struct perf_event
*event
)
3277 unsigned long flags
;
3281 * Disabling interrupts avoids all counter scheduling (context
3282 * switches, timer based rotation and IPIs).
3284 local_irq_save(flags
);
3286 /* If this is a per-task event, it must be for current */
3287 WARN_ON_ONCE((event
->attach_state
& PERF_ATTACH_TASK
) &&
3288 event
->hw
.target
!= current
);
3290 /* If this is a per-CPU event, it must be for this CPU */
3291 WARN_ON_ONCE(!(event
->attach_state
& PERF_ATTACH_TASK
) &&
3292 event
->cpu
!= smp_processor_id());
3295 * It must not be an event with inherit set, we cannot read
3296 * all child counters from atomic context.
3298 WARN_ON_ONCE(event
->attr
.inherit
);
3301 * It must not have a pmu::count method, those are not
3304 WARN_ON_ONCE(event
->pmu
->count
);
3307 * If the event is currently on this CPU, its either a per-task event,
3308 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3311 if (event
->oncpu
== smp_processor_id())
3312 event
->pmu
->read(event
);
3314 val
= local64_read(&event
->count
);
3315 local_irq_restore(flags
);
3320 static int perf_event_read(struct perf_event
*event
, bool group
)
3325 * If event is enabled and currently active on a CPU, update the
3326 * value in the event structure:
3328 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
3329 struct perf_read_data data
= {
3334 smp_call_function_single(event
->oncpu
,
3335 __perf_event_read
, &data
, 1);
3337 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
3338 struct perf_event_context
*ctx
= event
->ctx
;
3339 unsigned long flags
;
3341 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
3343 * may read while context is not active
3344 * (e.g., thread is blocked), in that case
3345 * we cannot update context time
3347 if (ctx
->is_active
) {
3348 update_context_time(ctx
);
3349 update_cgrp_time_from_event(event
);
3352 update_group_times(event
);
3354 update_event_times(event
);
3355 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
3362 * Initialize the perf_event context in a task_struct:
3364 static void __perf_event_init_context(struct perf_event_context
*ctx
)
3366 raw_spin_lock_init(&ctx
->lock
);
3367 mutex_init(&ctx
->mutex
);
3368 INIT_LIST_HEAD(&ctx
->active_ctx_list
);
3369 INIT_LIST_HEAD(&ctx
->pinned_groups
);
3370 INIT_LIST_HEAD(&ctx
->flexible_groups
);
3371 INIT_LIST_HEAD(&ctx
->event_list
);
3372 atomic_set(&ctx
->refcount
, 1);
3375 static struct perf_event_context
*
3376 alloc_perf_context(struct pmu
*pmu
, struct task_struct
*task
)
3378 struct perf_event_context
*ctx
;
3380 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
3384 __perf_event_init_context(ctx
);
3387 get_task_struct(task
);
3394 static struct task_struct
*
3395 find_lively_task_by_vpid(pid_t vpid
)
3397 struct task_struct
*task
;
3404 task
= find_task_by_vpid(vpid
);
3406 get_task_struct(task
);
3410 return ERR_PTR(-ESRCH
);
3412 /* Reuse ptrace permission checks for now. */
3414 if (!ptrace_may_access(task
, PTRACE_MODE_READ_REALCREDS
))
3419 put_task_struct(task
);
3420 return ERR_PTR(err
);
3425 * Returns a matching context with refcount and pincount.
3427 static struct perf_event_context
*
3428 find_get_context(struct pmu
*pmu
, struct task_struct
*task
,
3429 struct perf_event
*event
)
3431 struct perf_event_context
*ctx
, *clone_ctx
= NULL
;
3432 struct perf_cpu_context
*cpuctx
;
3433 void *task_ctx_data
= NULL
;
3434 unsigned long flags
;
3436 int cpu
= event
->cpu
;
3439 /* Must be root to operate on a CPU event: */
3440 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
3441 return ERR_PTR(-EACCES
);
3444 * We could be clever and allow to attach a event to an
3445 * offline CPU and activate it when the CPU comes up, but
3448 if (!cpu_online(cpu
))
3449 return ERR_PTR(-ENODEV
);
3451 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
3460 ctxn
= pmu
->task_ctx_nr
;
3464 if (event
->attach_state
& PERF_ATTACH_TASK_DATA
) {
3465 task_ctx_data
= kzalloc(pmu
->task_ctx_size
, GFP_KERNEL
);
3466 if (!task_ctx_data
) {
3473 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
3475 clone_ctx
= unclone_ctx(ctx
);
3478 if (task_ctx_data
&& !ctx
->task_ctx_data
) {
3479 ctx
->task_ctx_data
= task_ctx_data
;
3480 task_ctx_data
= NULL
;
3482 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
3487 ctx
= alloc_perf_context(pmu
, task
);
3492 if (task_ctx_data
) {
3493 ctx
->task_ctx_data
= task_ctx_data
;
3494 task_ctx_data
= NULL
;
3498 mutex_lock(&task
->perf_event_mutex
);
3500 * If it has already passed perf_event_exit_task().
3501 * we must see PF_EXITING, it takes this mutex too.
3503 if (task
->flags
& PF_EXITING
)
3505 else if (task
->perf_event_ctxp
[ctxn
])
3510 rcu_assign_pointer(task
->perf_event_ctxp
[ctxn
], ctx
);
3512 mutex_unlock(&task
->perf_event_mutex
);
3514 if (unlikely(err
)) {
3523 kfree(task_ctx_data
);
3527 kfree(task_ctx_data
);
3528 return ERR_PTR(err
);
3531 static void perf_event_free_filter(struct perf_event
*event
);
3532 static void perf_event_free_bpf_prog(struct perf_event
*event
);
3534 static void free_event_rcu(struct rcu_head
*head
)
3536 struct perf_event
*event
;
3538 event
= container_of(head
, struct perf_event
, rcu_head
);
3540 put_pid_ns(event
->ns
);
3541 perf_event_free_filter(event
);
3545 static void ring_buffer_attach(struct perf_event
*event
,
3546 struct ring_buffer
*rb
);
3548 static void unaccount_event_cpu(struct perf_event
*event
, int cpu
)
3553 if (is_cgroup_event(event
))
3554 atomic_dec(&per_cpu(perf_cgroup_events
, cpu
));
3557 #ifdef CONFIG_NO_HZ_FULL
3558 static DEFINE_SPINLOCK(nr_freq_lock
);
3561 static void unaccount_freq_event_nohz(void)
3563 #ifdef CONFIG_NO_HZ_FULL
3564 spin_lock(&nr_freq_lock
);
3565 if (atomic_dec_and_test(&nr_freq_events
))
3566 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS
);
3567 spin_unlock(&nr_freq_lock
);
3571 static void unaccount_freq_event(void)
3573 if (tick_nohz_full_enabled())
3574 unaccount_freq_event_nohz();
3576 atomic_dec(&nr_freq_events
);
3579 static void unaccount_event(struct perf_event
*event
)
3586 if (event
->attach_state
& PERF_ATTACH_TASK
)
3588 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
3589 atomic_dec(&nr_mmap_events
);
3590 if (event
->attr
.comm
)
3591 atomic_dec(&nr_comm_events
);
3592 if (event
->attr
.task
)
3593 atomic_dec(&nr_task_events
);
3594 if (event
->attr
.freq
)
3595 unaccount_freq_event();
3596 if (event
->attr
.context_switch
) {
3598 atomic_dec(&nr_switch_events
);
3600 if (is_cgroup_event(event
))
3602 if (has_branch_stack(event
))
3606 if (!atomic_add_unless(&perf_sched_count
, -1, 1))
3607 schedule_delayed_work(&perf_sched_work
, HZ
);
3610 unaccount_event_cpu(event
, event
->cpu
);
3613 static void perf_sched_delayed(struct work_struct
*work
)
3615 mutex_lock(&perf_sched_mutex
);
3616 if (atomic_dec_and_test(&perf_sched_count
))
3617 static_branch_disable(&perf_sched_events
);
3618 mutex_unlock(&perf_sched_mutex
);
3622 * The following implement mutual exclusion of events on "exclusive" pmus
3623 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3624 * at a time, so we disallow creating events that might conflict, namely:
3626 * 1) cpu-wide events in the presence of per-task events,
3627 * 2) per-task events in the presence of cpu-wide events,
3628 * 3) two matching events on the same context.
3630 * The former two cases are handled in the allocation path (perf_event_alloc(),
3631 * _free_event()), the latter -- before the first perf_install_in_context().
3633 static int exclusive_event_init(struct perf_event
*event
)
3635 struct pmu
*pmu
= event
->pmu
;
3637 if (!(pmu
->capabilities
& PERF_PMU_CAP_EXCLUSIVE
))
3641 * Prevent co-existence of per-task and cpu-wide events on the
3642 * same exclusive pmu.
3644 * Negative pmu::exclusive_cnt means there are cpu-wide
3645 * events on this "exclusive" pmu, positive means there are
3648 * Since this is called in perf_event_alloc() path, event::ctx
3649 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
3650 * to mean "per-task event", because unlike other attach states it
3651 * never gets cleared.
3653 if (event
->attach_state
& PERF_ATTACH_TASK
) {
3654 if (!atomic_inc_unless_negative(&pmu
->exclusive_cnt
))
3657 if (!atomic_dec_unless_positive(&pmu
->exclusive_cnt
))
3664 static void exclusive_event_destroy(struct perf_event
*event
)
3666 struct pmu
*pmu
= event
->pmu
;
3668 if (!(pmu
->capabilities
& PERF_PMU_CAP_EXCLUSIVE
))
3671 /* see comment in exclusive_event_init() */
3672 if (event
->attach_state
& PERF_ATTACH_TASK
)
3673 atomic_dec(&pmu
->exclusive_cnt
);
3675 atomic_inc(&pmu
->exclusive_cnt
);
3678 static bool exclusive_event_match(struct perf_event
*e1
, struct perf_event
*e2
)
3680 if ((e1
->pmu
->capabilities
& PERF_PMU_CAP_EXCLUSIVE
) &&
3681 (e1
->cpu
== e2
->cpu
||
3688 /* Called under the same ctx::mutex as perf_install_in_context() */
3689 static bool exclusive_event_installable(struct perf_event
*event
,
3690 struct perf_event_context
*ctx
)
3692 struct perf_event
*iter_event
;
3693 struct pmu
*pmu
= event
->pmu
;
3695 if (!(pmu
->capabilities
& PERF_PMU_CAP_EXCLUSIVE
))
3698 list_for_each_entry(iter_event
, &ctx
->event_list
, event_entry
) {
3699 if (exclusive_event_match(iter_event
, event
))
3706 static void _free_event(struct perf_event
*event
)
3708 irq_work_sync(&event
->pending
);
3710 unaccount_event(event
);
3714 * Can happen when we close an event with re-directed output.
3716 * Since we have a 0 refcount, perf_mmap_close() will skip
3717 * over us; possibly making our ring_buffer_put() the last.
3719 mutex_lock(&event
->mmap_mutex
);
3720 ring_buffer_attach(event
, NULL
);
3721 mutex_unlock(&event
->mmap_mutex
);
3724 if (is_cgroup_event(event
))
3725 perf_detach_cgroup(event
);
3727 if (!event
->parent
) {
3728 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
)
3729 put_callchain_buffers();
3732 perf_event_free_bpf_prog(event
);
3735 event
->destroy(event
);
3738 put_ctx(event
->ctx
);
3741 exclusive_event_destroy(event
);
3742 module_put(event
->pmu
->module
);
3745 call_rcu(&event
->rcu_head
, free_event_rcu
);
3749 * Used to free events which have a known refcount of 1, such as in error paths
3750 * where the event isn't exposed yet and inherited events.
3752 static void free_event(struct perf_event
*event
)
3754 if (WARN(atomic_long_cmpxchg(&event
->refcount
, 1, 0) != 1,
3755 "unexpected event refcount: %ld; ptr=%p\n",
3756 atomic_long_read(&event
->refcount
), event
)) {
3757 /* leak to avoid use-after-free */
3765 * Remove user event from the owner task.
3767 static void perf_remove_from_owner(struct perf_event
*event
)
3769 struct task_struct
*owner
;
3773 * Matches the smp_store_release() in perf_event_exit_task(). If we
3774 * observe !owner it means the list deletion is complete and we can
3775 * indeed free this event, otherwise we need to serialize on
3776 * owner->perf_event_mutex.
3778 owner
= lockless_dereference(event
->owner
);
3781 * Since delayed_put_task_struct() also drops the last
3782 * task reference we can safely take a new reference
3783 * while holding the rcu_read_lock().
3785 get_task_struct(owner
);
3791 * If we're here through perf_event_exit_task() we're already
3792 * holding ctx->mutex which would be an inversion wrt. the
3793 * normal lock order.
3795 * However we can safely take this lock because its the child
3798 mutex_lock_nested(&owner
->perf_event_mutex
, SINGLE_DEPTH_NESTING
);
3801 * We have to re-check the event->owner field, if it is cleared
3802 * we raced with perf_event_exit_task(), acquiring the mutex
3803 * ensured they're done, and we can proceed with freeing the
3807 list_del_init(&event
->owner_entry
);
3808 smp_store_release(&event
->owner
, NULL
);
3810 mutex_unlock(&owner
->perf_event_mutex
);
3811 put_task_struct(owner
);
3815 static void put_event(struct perf_event
*event
)
3817 if (!atomic_long_dec_and_test(&event
->refcount
))
3824 * Kill an event dead; while event:refcount will preserve the event
3825 * object, it will not preserve its functionality. Once the last 'user'
3826 * gives up the object, we'll destroy the thing.
3828 int perf_event_release_kernel(struct perf_event
*event
)
3830 struct perf_event_context
*ctx
= event
->ctx
;
3831 struct perf_event
*child
, *tmp
;
3834 * If we got here through err_file: fput(event_file); we will not have
3835 * attached to a context yet.
3838 WARN_ON_ONCE(event
->attach_state
&
3839 (PERF_ATTACH_CONTEXT
|PERF_ATTACH_GROUP
));
3843 if (!is_kernel_event(event
))
3844 perf_remove_from_owner(event
);
3846 ctx
= perf_event_ctx_lock(event
);
3847 WARN_ON_ONCE(ctx
->parent_ctx
);
3848 perf_remove_from_context(event
, DETACH_GROUP
);
3850 raw_spin_lock_irq(&ctx
->lock
);
3852 * Mark this even as STATE_DEAD, there is no external reference to it
3855 * Anybody acquiring event->child_mutex after the below loop _must_
3856 * also see this, most importantly inherit_event() which will avoid
3857 * placing more children on the list.
3859 * Thus this guarantees that we will in fact observe and kill _ALL_
3862 event
->state
= PERF_EVENT_STATE_DEAD
;
3863 raw_spin_unlock_irq(&ctx
->lock
);
3865 perf_event_ctx_unlock(event
, ctx
);
3868 mutex_lock(&event
->child_mutex
);
3869 list_for_each_entry(child
, &event
->child_list
, child_list
) {
3872 * Cannot change, child events are not migrated, see the
3873 * comment with perf_event_ctx_lock_nested().
3875 ctx
= lockless_dereference(child
->ctx
);
3877 * Since child_mutex nests inside ctx::mutex, we must jump
3878 * through hoops. We start by grabbing a reference on the ctx.
3880 * Since the event cannot get freed while we hold the
3881 * child_mutex, the context must also exist and have a !0
3887 * Now that we have a ctx ref, we can drop child_mutex, and
3888 * acquire ctx::mutex without fear of it going away. Then we
3889 * can re-acquire child_mutex.
3891 mutex_unlock(&event
->child_mutex
);
3892 mutex_lock(&ctx
->mutex
);
3893 mutex_lock(&event
->child_mutex
);
3896 * Now that we hold ctx::mutex and child_mutex, revalidate our
3897 * state, if child is still the first entry, it didn't get freed
3898 * and we can continue doing so.
3900 tmp
= list_first_entry_or_null(&event
->child_list
,
3901 struct perf_event
, child_list
);
3903 perf_remove_from_context(child
, DETACH_GROUP
);
3904 list_del(&child
->child_list
);
3907 * This matches the refcount bump in inherit_event();
3908 * this can't be the last reference.
3913 mutex_unlock(&event
->child_mutex
);
3914 mutex_unlock(&ctx
->mutex
);
3918 mutex_unlock(&event
->child_mutex
);
3921 put_event(event
); /* Must be the 'last' reference */
3924 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
3927 * Called when the last reference to the file is gone.
3929 static int perf_release(struct inode
*inode
, struct file
*file
)
3931 perf_event_release_kernel(file
->private_data
);
3935 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
3937 struct perf_event
*child
;
3943 mutex_lock(&event
->child_mutex
);
3945 (void)perf_event_read(event
, false);
3946 total
+= perf_event_count(event
);
3948 *enabled
+= event
->total_time_enabled
+
3949 atomic64_read(&event
->child_total_time_enabled
);
3950 *running
+= event
->total_time_running
+
3951 atomic64_read(&event
->child_total_time_running
);
3953 list_for_each_entry(child
, &event
->child_list
, child_list
) {
3954 (void)perf_event_read(child
, false);
3955 total
+= perf_event_count(child
);
3956 *enabled
+= child
->total_time_enabled
;
3957 *running
+= child
->total_time_running
;
3959 mutex_unlock(&event
->child_mutex
);
3963 EXPORT_SYMBOL_GPL(perf_event_read_value
);
3965 static int __perf_read_group_add(struct perf_event
*leader
,
3966 u64 read_format
, u64
*values
)
3968 struct perf_event
*sub
;
3969 int n
= 1; /* skip @nr */
3972 ret
= perf_event_read(leader
, true);
3977 * Since we co-schedule groups, {enabled,running} times of siblings
3978 * will be identical to those of the leader, so we only publish one
3981 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
3982 values
[n
++] += leader
->total_time_enabled
+
3983 atomic64_read(&leader
->child_total_time_enabled
);
3986 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
3987 values
[n
++] += leader
->total_time_running
+
3988 atomic64_read(&leader
->child_total_time_running
);
3992 * Write {count,id} tuples for every sibling.
3994 values
[n
++] += perf_event_count(leader
);
3995 if (read_format
& PERF_FORMAT_ID
)
3996 values
[n
++] = primary_event_id(leader
);
3998 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3999 values
[n
++] += perf_event_count(sub
);
4000 if (read_format
& PERF_FORMAT_ID
)
4001 values
[n
++] = primary_event_id(sub
);
4007 static int perf_read_group(struct perf_event
*event
,
4008 u64 read_format
, char __user
*buf
)
4010 struct perf_event
*leader
= event
->group_leader
, *child
;
4011 struct perf_event_context
*ctx
= leader
->ctx
;
4015 lockdep_assert_held(&ctx
->mutex
);
4017 values
= kzalloc(event
->read_size
, GFP_KERNEL
);
4021 values
[0] = 1 + leader
->nr_siblings
;
4024 * By locking the child_mutex of the leader we effectively
4025 * lock the child list of all siblings.. XXX explain how.
4027 mutex_lock(&leader
->child_mutex
);
4029 ret
= __perf_read_group_add(leader
, read_format
, values
);
4033 list_for_each_entry(child
, &leader
->child_list
, child_list
) {
4034 ret
= __perf_read_group_add(child
, read_format
, values
);
4039 mutex_unlock(&leader
->child_mutex
);
4041 ret
= event
->read_size
;
4042 if (copy_to_user(buf
, values
, event
->read_size
))
4047 mutex_unlock(&leader
->child_mutex
);
4053 static int perf_read_one(struct perf_event
*event
,
4054 u64 read_format
, char __user
*buf
)
4056 u64 enabled
, running
;
4060 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
4061 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
4062 values
[n
++] = enabled
;
4063 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
4064 values
[n
++] = running
;
4065 if (read_format
& PERF_FORMAT_ID
)
4066 values
[n
++] = primary_event_id(event
);
4068 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
4071 return n
* sizeof(u64
);
4074 static bool is_event_hup(struct perf_event
*event
)
4078 if (event
->state
> PERF_EVENT_STATE_EXIT
)
4081 mutex_lock(&event
->child_mutex
);
4082 no_children
= list_empty(&event
->child_list
);
4083 mutex_unlock(&event
->child_mutex
);
4088 * Read the performance event - simple non blocking version for now
4091 __perf_read(struct perf_event
*event
, char __user
*buf
, size_t count
)
4093 u64 read_format
= event
->attr
.read_format
;
4097 * Return end-of-file for a read on a event that is in
4098 * error state (i.e. because it was pinned but it couldn't be
4099 * scheduled on to the CPU at some point).
4101 if (event
->state
== PERF_EVENT_STATE_ERROR
)
4104 if (count
< event
->read_size
)
4107 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
4108 if (read_format
& PERF_FORMAT_GROUP
)
4109 ret
= perf_read_group(event
, read_format
, buf
);
4111 ret
= perf_read_one(event
, read_format
, buf
);
4117 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
4119 struct perf_event
*event
= file
->private_data
;
4120 struct perf_event_context
*ctx
;
4123 ctx
= perf_event_ctx_lock(event
);
4124 ret
= __perf_read(event
, buf
, count
);
4125 perf_event_ctx_unlock(event
, ctx
);
4130 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
4132 struct perf_event
*event
= file
->private_data
;
4133 struct ring_buffer
*rb
;
4134 unsigned int events
= POLLHUP
;
4136 poll_wait(file
, &event
->waitq
, wait
);
4138 if (is_event_hup(event
))
4142 * Pin the event->rb by taking event->mmap_mutex; otherwise
4143 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
4145 mutex_lock(&event
->mmap_mutex
);
4148 events
= atomic_xchg(&rb
->poll
, 0);
4149 mutex_unlock(&event
->mmap_mutex
);
4153 static void _perf_event_reset(struct perf_event
*event
)
4155 (void)perf_event_read(event
, false);
4156 local64_set(&event
->count
, 0);
4157 perf_event_update_userpage(event
);
4161 * Holding the top-level event's child_mutex means that any
4162 * descendant process that has inherited this event will block
4163 * in perf_event_exit_event() if it goes to exit, thus satisfying the
4164 * task existence requirements of perf_event_enable/disable.
4166 static void perf_event_for_each_child(struct perf_event
*event
,
4167 void (*func
)(struct perf_event
*))
4169 struct perf_event
*child
;
4171 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
4173 mutex_lock(&event
->child_mutex
);
4175 list_for_each_entry(child
, &event
->child_list
, child_list
)
4177 mutex_unlock(&event
->child_mutex
);
4180 static void perf_event_for_each(struct perf_event
*event
,
4181 void (*func
)(struct perf_event
*))
4183 struct perf_event_context
*ctx
= event
->ctx
;
4184 struct perf_event
*sibling
;
4186 lockdep_assert_held(&ctx
->mutex
);
4188 event
= event
->group_leader
;
4190 perf_event_for_each_child(event
, func
);
4191 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
4192 perf_event_for_each_child(sibling
, func
);
4195 static void __perf_event_period(struct perf_event
*event
,
4196 struct perf_cpu_context
*cpuctx
,
4197 struct perf_event_context
*ctx
,
4200 u64 value
= *((u64
*)info
);
4203 if (event
->attr
.freq
) {
4204 event
->attr
.sample_freq
= value
;
4206 event
->attr
.sample_period
= value
;
4207 event
->hw
.sample_period
= value
;
4210 active
= (event
->state
== PERF_EVENT_STATE_ACTIVE
);
4212 perf_pmu_disable(ctx
->pmu
);
4213 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
4216 local64_set(&event
->hw
.period_left
, 0);
4219 event
->pmu
->start(event
, PERF_EF_RELOAD
);
4220 perf_pmu_enable(ctx
->pmu
);
4224 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
4228 if (!is_sampling_event(event
))
4231 if (copy_from_user(&value
, arg
, sizeof(value
)))
4237 if (event
->attr
.freq
&& value
> sysctl_perf_event_sample_rate
)
4240 event_function_call(event
, __perf_event_period
, &value
);
4245 static const struct file_operations perf_fops
;
4247 static inline int perf_fget_light(int fd
, struct fd
*p
)
4249 struct fd f
= fdget(fd
);
4253 if (f
.file
->f_op
!= &perf_fops
) {
4261 static int perf_event_set_output(struct perf_event
*event
,
4262 struct perf_event
*output_event
);
4263 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
4264 static int perf_event_set_bpf_prog(struct perf_event
*event
, u32 prog_fd
);
4266 static long _perf_ioctl(struct perf_event
*event
, unsigned int cmd
, unsigned long arg
)
4268 void (*func
)(struct perf_event
*);
4272 case PERF_EVENT_IOC_ENABLE
:
4273 func
= _perf_event_enable
;
4275 case PERF_EVENT_IOC_DISABLE
:
4276 func
= _perf_event_disable
;
4278 case PERF_EVENT_IOC_RESET
:
4279 func
= _perf_event_reset
;
4282 case PERF_EVENT_IOC_REFRESH
:
4283 return _perf_event_refresh(event
, arg
);
4285 case PERF_EVENT_IOC_PERIOD
:
4286 return perf_event_period(event
, (u64 __user
*)arg
);
4288 case PERF_EVENT_IOC_ID
:
4290 u64 id
= primary_event_id(event
);
4292 if (copy_to_user((void __user
*)arg
, &id
, sizeof(id
)))
4297 case PERF_EVENT_IOC_SET_OUTPUT
:
4301 struct perf_event
*output_event
;
4303 ret
= perf_fget_light(arg
, &output
);
4306 output_event
= output
.file
->private_data
;
4307 ret
= perf_event_set_output(event
, output_event
);
4310 ret
= perf_event_set_output(event
, NULL
);
4315 case PERF_EVENT_IOC_SET_FILTER
:
4316 return perf_event_set_filter(event
, (void __user
*)arg
);
4318 case PERF_EVENT_IOC_SET_BPF
:
4319 return perf_event_set_bpf_prog(event
, arg
);
4325 if (flags
& PERF_IOC_FLAG_GROUP
)
4326 perf_event_for_each(event
, func
);
4328 perf_event_for_each_child(event
, func
);
4333 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
4335 struct perf_event
*event
= file
->private_data
;
4336 struct perf_event_context
*ctx
;
4339 ctx
= perf_event_ctx_lock(event
);
4340 ret
= _perf_ioctl(event
, cmd
, arg
);
4341 perf_event_ctx_unlock(event
, ctx
);
4346 #ifdef CONFIG_COMPAT
4347 static long perf_compat_ioctl(struct file
*file
, unsigned int cmd
,
4350 switch (_IOC_NR(cmd
)) {
4351 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER
):
4352 case _IOC_NR(PERF_EVENT_IOC_ID
):
4353 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4354 if (_IOC_SIZE(cmd
) == sizeof(compat_uptr_t
)) {
4355 cmd
&= ~IOCSIZE_MASK
;
4356 cmd
|= sizeof(void *) << IOCSIZE_SHIFT
;
4360 return perf_ioctl(file
, cmd
, arg
);
4363 # define perf_compat_ioctl NULL
4366 int perf_event_task_enable(void)
4368 struct perf_event_context
*ctx
;
4369 struct perf_event
*event
;
4371 mutex_lock(¤t
->perf_event_mutex
);
4372 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
) {
4373 ctx
= perf_event_ctx_lock(event
);
4374 perf_event_for_each_child(event
, _perf_event_enable
);
4375 perf_event_ctx_unlock(event
, ctx
);
4377 mutex_unlock(¤t
->perf_event_mutex
);
4382 int perf_event_task_disable(void)
4384 struct perf_event_context
*ctx
;
4385 struct perf_event
*event
;
4387 mutex_lock(¤t
->perf_event_mutex
);
4388 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
) {
4389 ctx
= perf_event_ctx_lock(event
);
4390 perf_event_for_each_child(event
, _perf_event_disable
);
4391 perf_event_ctx_unlock(event
, ctx
);
4393 mutex_unlock(¤t
->perf_event_mutex
);
4398 static int perf_event_index(struct perf_event
*event
)
4400 if (event
->hw
.state
& PERF_HES_STOPPED
)
4403 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
4406 return event
->pmu
->event_idx(event
);
4409 static void calc_timer_values(struct perf_event
*event
,
4416 *now
= perf_clock();
4417 ctx_time
= event
->shadow_ctx_time
+ *now
;
4418 *enabled
= ctx_time
- event
->tstamp_enabled
;
4419 *running
= ctx_time
- event
->tstamp_running
;
4422 static void perf_event_init_userpage(struct perf_event
*event
)
4424 struct perf_event_mmap_page
*userpg
;
4425 struct ring_buffer
*rb
;
4428 rb
= rcu_dereference(event
->rb
);
4432 userpg
= rb
->user_page
;
4434 /* Allow new userspace to detect that bit 0 is deprecated */
4435 userpg
->cap_bit0_is_deprecated
= 1;
4436 userpg
->size
= offsetof(struct perf_event_mmap_page
, __reserved
);
4437 userpg
->data_offset
= PAGE_SIZE
;
4438 userpg
->data_size
= perf_data_size(rb
);
4444 void __weak
arch_perf_update_userpage(
4445 struct perf_event
*event
, struct perf_event_mmap_page
*userpg
, u64 now
)
4450 * Callers need to ensure there can be no nesting of this function, otherwise
4451 * the seqlock logic goes bad. We can not serialize this because the arch
4452 * code calls this from NMI context.
4454 void perf_event_update_userpage(struct perf_event
*event
)
4456 struct perf_event_mmap_page
*userpg
;
4457 struct ring_buffer
*rb
;
4458 u64 enabled
, running
, now
;
4461 rb
= rcu_dereference(event
->rb
);
4466 * compute total_time_enabled, total_time_running
4467 * based on snapshot values taken when the event
4468 * was last scheduled in.
4470 * we cannot simply called update_context_time()
4471 * because of locking issue as we can be called in
4474 calc_timer_values(event
, &now
, &enabled
, &running
);
4476 userpg
= rb
->user_page
;
4478 * Disable preemption so as to not let the corresponding user-space
4479 * spin too long if we get preempted.
4484 userpg
->index
= perf_event_index(event
);
4485 userpg
->offset
= perf_event_count(event
);
4487 userpg
->offset
-= local64_read(&event
->hw
.prev_count
);
4489 userpg
->time_enabled
= enabled
+
4490 atomic64_read(&event
->child_total_time_enabled
);
4492 userpg
->time_running
= running
+
4493 atomic64_read(&event
->child_total_time_running
);
4495 arch_perf_update_userpage(event
, userpg
, now
);
4504 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
4506 struct perf_event
*event
= vma
->vm_file
->private_data
;
4507 struct ring_buffer
*rb
;
4508 int ret
= VM_FAULT_SIGBUS
;
4510 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
4511 if (vmf
->pgoff
== 0)
4517 rb
= rcu_dereference(event
->rb
);
4521 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
4524 vmf
->page
= perf_mmap_to_page(rb
, vmf
->pgoff
);
4528 get_page(vmf
->page
);
4529 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
4530 vmf
->page
->index
= vmf
->pgoff
;
4539 static void ring_buffer_attach(struct perf_event
*event
,
4540 struct ring_buffer
*rb
)
4542 struct ring_buffer
*old_rb
= NULL
;
4543 unsigned long flags
;
4547 * Should be impossible, we set this when removing
4548 * event->rb_entry and wait/clear when adding event->rb_entry.
4550 WARN_ON_ONCE(event
->rcu_pending
);
4553 spin_lock_irqsave(&old_rb
->event_lock
, flags
);
4554 list_del_rcu(&event
->rb_entry
);
4555 spin_unlock_irqrestore(&old_rb
->event_lock
, flags
);
4557 event
->rcu_batches
= get_state_synchronize_rcu();
4558 event
->rcu_pending
= 1;
4562 if (event
->rcu_pending
) {
4563 cond_synchronize_rcu(event
->rcu_batches
);
4564 event
->rcu_pending
= 0;
4567 spin_lock_irqsave(&rb
->event_lock
, flags
);
4568 list_add_rcu(&event
->rb_entry
, &rb
->event_list
);
4569 spin_unlock_irqrestore(&rb
->event_lock
, flags
);
4572 rcu_assign_pointer(event
->rb
, rb
);
4575 ring_buffer_put(old_rb
);
4577 * Since we detached before setting the new rb, so that we
4578 * could attach the new rb, we could have missed a wakeup.
4581 wake_up_all(&event
->waitq
);
4585 static void ring_buffer_wakeup(struct perf_event
*event
)
4587 struct ring_buffer
*rb
;
4590 rb
= rcu_dereference(event
->rb
);
4592 list_for_each_entry_rcu(event
, &rb
->event_list
, rb_entry
)
4593 wake_up_all(&event
->waitq
);
4598 struct ring_buffer
*ring_buffer_get(struct perf_event
*event
)
4600 struct ring_buffer
*rb
;
4603 rb
= rcu_dereference(event
->rb
);
4605 if (!atomic_inc_not_zero(&rb
->refcount
))
4613 void ring_buffer_put(struct ring_buffer
*rb
)
4615 if (!atomic_dec_and_test(&rb
->refcount
))
4618 WARN_ON_ONCE(!list_empty(&rb
->event_list
));
4620 call_rcu(&rb
->rcu_head
, rb_free_rcu
);
4623 static void perf_mmap_open(struct vm_area_struct
*vma
)
4625 struct perf_event
*event
= vma
->vm_file
->private_data
;
4627 atomic_inc(&event
->mmap_count
);
4628 atomic_inc(&event
->rb
->mmap_count
);
4631 atomic_inc(&event
->rb
->aux_mmap_count
);
4633 if (event
->pmu
->event_mapped
)
4634 event
->pmu
->event_mapped(event
);
4638 * A buffer can be mmap()ed multiple times; either directly through the same
4639 * event, or through other events by use of perf_event_set_output().
4641 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4642 * the buffer here, where we still have a VM context. This means we need
4643 * to detach all events redirecting to us.
4645 static void perf_mmap_close(struct vm_area_struct
*vma
)
4647 struct perf_event
*event
= vma
->vm_file
->private_data
;
4649 struct ring_buffer
*rb
= ring_buffer_get(event
);
4650 struct user_struct
*mmap_user
= rb
->mmap_user
;
4651 int mmap_locked
= rb
->mmap_locked
;
4652 unsigned long size
= perf_data_size(rb
);
4654 if (event
->pmu
->event_unmapped
)
4655 event
->pmu
->event_unmapped(event
);
4658 * rb->aux_mmap_count will always drop before rb->mmap_count and
4659 * event->mmap_count, so it is ok to use event->mmap_mutex to
4660 * serialize with perf_mmap here.
4662 if (rb_has_aux(rb
) && vma
->vm_pgoff
== rb
->aux_pgoff
&&
4663 atomic_dec_and_mutex_lock(&rb
->aux_mmap_count
, &event
->mmap_mutex
)) {
4664 atomic_long_sub(rb
->aux_nr_pages
, &mmap_user
->locked_vm
);
4665 vma
->vm_mm
->pinned_vm
-= rb
->aux_mmap_locked
;
4668 mutex_unlock(&event
->mmap_mutex
);
4671 atomic_dec(&rb
->mmap_count
);
4673 if (!atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
))
4676 ring_buffer_attach(event
, NULL
);
4677 mutex_unlock(&event
->mmap_mutex
);
4679 /* If there's still other mmap()s of this buffer, we're done. */
4680 if (atomic_read(&rb
->mmap_count
))
4684 * No other mmap()s, detach from all other events that might redirect
4685 * into the now unreachable buffer. Somewhat complicated by the
4686 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4690 list_for_each_entry_rcu(event
, &rb
->event_list
, rb_entry
) {
4691 if (!atomic_long_inc_not_zero(&event
->refcount
)) {
4693 * This event is en-route to free_event() which will
4694 * detach it and remove it from the list.
4700 mutex_lock(&event
->mmap_mutex
);
4702 * Check we didn't race with perf_event_set_output() which can
4703 * swizzle the rb from under us while we were waiting to
4704 * acquire mmap_mutex.
4706 * If we find a different rb; ignore this event, a next
4707 * iteration will no longer find it on the list. We have to
4708 * still restart the iteration to make sure we're not now
4709 * iterating the wrong list.
4711 if (event
->rb
== rb
)
4712 ring_buffer_attach(event
, NULL
);
4714 mutex_unlock(&event
->mmap_mutex
);
4718 * Restart the iteration; either we're on the wrong list or
4719 * destroyed its integrity by doing a deletion.
4726 * It could be there's still a few 0-ref events on the list; they'll
4727 * get cleaned up by free_event() -- they'll also still have their
4728 * ref on the rb and will free it whenever they are done with it.
4730 * Aside from that, this buffer is 'fully' detached and unmapped,
4731 * undo the VM accounting.
4734 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &mmap_user
->locked_vm
);
4735 vma
->vm_mm
->pinned_vm
-= mmap_locked
;
4736 free_uid(mmap_user
);
4739 ring_buffer_put(rb
); /* could be last */
4742 static const struct vm_operations_struct perf_mmap_vmops
= {
4743 .open
= perf_mmap_open
,
4744 .close
= perf_mmap_close
, /* non mergable */
4745 .fault
= perf_mmap_fault
,
4746 .page_mkwrite
= perf_mmap_fault
,
4749 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
4751 struct perf_event
*event
= file
->private_data
;
4752 unsigned long user_locked
, user_lock_limit
;
4753 struct user_struct
*user
= current_user();
4754 unsigned long locked
, lock_limit
;
4755 struct ring_buffer
*rb
= NULL
;
4756 unsigned long vma_size
;
4757 unsigned long nr_pages
;
4758 long user_extra
= 0, extra
= 0;
4759 int ret
= 0, flags
= 0;
4762 * Don't allow mmap() of inherited per-task counters. This would
4763 * create a performance issue due to all children writing to the
4766 if (event
->cpu
== -1 && event
->attr
.inherit
)
4769 if (!(vma
->vm_flags
& VM_SHARED
))
4772 vma_size
= vma
->vm_end
- vma
->vm_start
;
4774 if (vma
->vm_pgoff
== 0) {
4775 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
4778 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
4779 * mapped, all subsequent mappings should have the same size
4780 * and offset. Must be above the normal perf buffer.
4782 u64 aux_offset
, aux_size
;
4787 nr_pages
= vma_size
/ PAGE_SIZE
;
4789 mutex_lock(&event
->mmap_mutex
);
4796 aux_offset
= ACCESS_ONCE(rb
->user_page
->aux_offset
);
4797 aux_size
= ACCESS_ONCE(rb
->user_page
->aux_size
);
4799 if (aux_offset
< perf_data_size(rb
) + PAGE_SIZE
)
4802 if (aux_offset
!= vma
->vm_pgoff
<< PAGE_SHIFT
)
4805 /* already mapped with a different offset */
4806 if (rb_has_aux(rb
) && rb
->aux_pgoff
!= vma
->vm_pgoff
)
4809 if (aux_size
!= vma_size
|| aux_size
!= nr_pages
* PAGE_SIZE
)
4812 /* already mapped with a different size */
4813 if (rb_has_aux(rb
) && rb
->aux_nr_pages
!= nr_pages
)
4816 if (!is_power_of_2(nr_pages
))
4819 if (!atomic_inc_not_zero(&rb
->mmap_count
))
4822 if (rb_has_aux(rb
)) {
4823 atomic_inc(&rb
->aux_mmap_count
);
4828 atomic_set(&rb
->aux_mmap_count
, 1);
4829 user_extra
= nr_pages
;
4835 * If we have rb pages ensure they're a power-of-two number, so we
4836 * can do bitmasks instead of modulo.
4838 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
4841 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
4844 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
4846 mutex_lock(&event
->mmap_mutex
);
4848 if (event
->rb
->nr_pages
!= nr_pages
) {
4853 if (!atomic_inc_not_zero(&event
->rb
->mmap_count
)) {
4855 * Raced against perf_mmap_close() through
4856 * perf_event_set_output(). Try again, hope for better
4859 mutex_unlock(&event
->mmap_mutex
);
4866 user_extra
= nr_pages
+ 1;
4869 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
4872 * Increase the limit linearly with more CPUs:
4874 user_lock_limit
*= num_online_cpus();
4876 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
4878 if (user_locked
> user_lock_limit
)
4879 extra
= user_locked
- user_lock_limit
;
4881 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
4882 lock_limit
>>= PAGE_SHIFT
;
4883 locked
= vma
->vm_mm
->pinned_vm
+ extra
;
4885 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
4886 !capable(CAP_IPC_LOCK
)) {
4891 WARN_ON(!rb
&& event
->rb
);
4893 if (vma
->vm_flags
& VM_WRITE
)
4894 flags
|= RING_BUFFER_WRITABLE
;
4897 rb
= rb_alloc(nr_pages
,
4898 event
->attr
.watermark
? event
->attr
.wakeup_watermark
: 0,
4906 atomic_set(&rb
->mmap_count
, 1);
4907 rb
->mmap_user
= get_current_user();
4908 rb
->mmap_locked
= extra
;
4910 ring_buffer_attach(event
, rb
);
4912 perf_event_init_userpage(event
);
4913 perf_event_update_userpage(event
);
4915 ret
= rb_alloc_aux(rb
, event
, vma
->vm_pgoff
, nr_pages
,
4916 event
->attr
.aux_watermark
, flags
);
4918 rb
->aux_mmap_locked
= extra
;
4923 atomic_long_add(user_extra
, &user
->locked_vm
);
4924 vma
->vm_mm
->pinned_vm
+= extra
;
4926 atomic_inc(&event
->mmap_count
);
4928 atomic_dec(&rb
->mmap_count
);
4931 mutex_unlock(&event
->mmap_mutex
);
4934 * Since pinned accounting is per vm we cannot allow fork() to copy our
4937 vma
->vm_flags
|= VM_DONTCOPY
| VM_DONTEXPAND
| VM_DONTDUMP
;
4938 vma
->vm_ops
= &perf_mmap_vmops
;
4940 if (event
->pmu
->event_mapped
)
4941 event
->pmu
->event_mapped(event
);
4946 static int perf_fasync(int fd
, struct file
*filp
, int on
)
4948 struct inode
*inode
= file_inode(filp
);
4949 struct perf_event
*event
= filp
->private_data
;
4953 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
4954 inode_unlock(inode
);
4962 static const struct file_operations perf_fops
= {
4963 .llseek
= no_llseek
,
4964 .release
= perf_release
,
4967 .unlocked_ioctl
= perf_ioctl
,
4968 .compat_ioctl
= perf_compat_ioctl
,
4970 .fasync
= perf_fasync
,
4976 * If there's data, ensure we set the poll() state and publish everything
4977 * to user-space before waking everybody up.
4980 static inline struct fasync_struct
**perf_event_fasync(struct perf_event
*event
)
4982 /* only the parent has fasync state */
4984 event
= event
->parent
;
4985 return &event
->fasync
;
4988 void perf_event_wakeup(struct perf_event
*event
)
4990 ring_buffer_wakeup(event
);
4992 if (event
->pending_kill
) {
4993 kill_fasync(perf_event_fasync(event
), SIGIO
, event
->pending_kill
);
4994 event
->pending_kill
= 0;
4998 static void perf_pending_event(struct irq_work
*entry
)
5000 struct perf_event
*event
= container_of(entry
,
5001 struct perf_event
, pending
);
5004 rctx
= perf_swevent_get_recursion_context();
5006 * If we 'fail' here, that's OK, it means recursion is already disabled
5007 * and we won't recurse 'further'.
5010 if (event
->pending_disable
) {
5011 event
->pending_disable
= 0;
5012 perf_event_disable_local(event
);
5015 if (event
->pending_wakeup
) {
5016 event
->pending_wakeup
= 0;
5017 perf_event_wakeup(event
);
5021 perf_swevent_put_recursion_context(rctx
);
5025 * We assume there is only KVM supporting the callbacks.
5026 * Later on, we might change it to a list if there is
5027 * another virtualization implementation supporting the callbacks.
5029 struct perf_guest_info_callbacks
*perf_guest_cbs
;
5031 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
5033 perf_guest_cbs
= cbs
;
5036 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks
);
5038 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
5040 perf_guest_cbs
= NULL
;
5043 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks
);
5046 perf_output_sample_regs(struct perf_output_handle
*handle
,
5047 struct pt_regs
*regs
, u64 mask
)
5051 for_each_set_bit(bit
, (const unsigned long *) &mask
,
5052 sizeof(mask
) * BITS_PER_BYTE
) {
5055 val
= perf_reg_value(regs
, bit
);
5056 perf_output_put(handle
, val
);
5060 static void perf_sample_regs_user(struct perf_regs
*regs_user
,
5061 struct pt_regs
*regs
,
5062 struct pt_regs
*regs_user_copy
)
5064 if (user_mode(regs
)) {
5065 regs_user
->abi
= perf_reg_abi(current
);
5066 regs_user
->regs
= regs
;
5067 } else if (current
->mm
) {
5068 perf_get_regs_user(regs_user
, regs
, regs_user_copy
);
5070 regs_user
->abi
= PERF_SAMPLE_REGS_ABI_NONE
;
5071 regs_user
->regs
= NULL
;
5075 static void perf_sample_regs_intr(struct perf_regs
*regs_intr
,
5076 struct pt_regs
*regs
)
5078 regs_intr
->regs
= regs
;
5079 regs_intr
->abi
= perf_reg_abi(current
);
5084 * Get remaining task size from user stack pointer.
5086 * It'd be better to take stack vma map and limit this more
5087 * precisly, but there's no way to get it safely under interrupt,
5088 * so using TASK_SIZE as limit.
5090 static u64
perf_ustack_task_size(struct pt_regs
*regs
)
5092 unsigned long addr
= perf_user_stack_pointer(regs
);
5094 if (!addr
|| addr
>= TASK_SIZE
)
5097 return TASK_SIZE
- addr
;
5101 perf_sample_ustack_size(u16 stack_size
, u16 header_size
,
5102 struct pt_regs
*regs
)
5106 /* No regs, no stack pointer, no dump. */
5111 * Check if we fit in with the requested stack size into the:
5113 * If we don't, we limit the size to the TASK_SIZE.
5115 * - remaining sample size
5116 * If we don't, we customize the stack size to
5117 * fit in to the remaining sample size.
5120 task_size
= min((u64
) USHRT_MAX
, perf_ustack_task_size(regs
));
5121 stack_size
= min(stack_size
, (u16
) task_size
);
5123 /* Current header size plus static size and dynamic size. */
5124 header_size
+= 2 * sizeof(u64
);
5126 /* Do we fit in with the current stack dump size? */
5127 if ((u16
) (header_size
+ stack_size
) < header_size
) {
5129 * If we overflow the maximum size for the sample,
5130 * we customize the stack dump size to fit in.
5132 stack_size
= USHRT_MAX
- header_size
- sizeof(u64
);
5133 stack_size
= round_up(stack_size
, sizeof(u64
));
5140 perf_output_sample_ustack(struct perf_output_handle
*handle
, u64 dump_size
,
5141 struct pt_regs
*regs
)
5143 /* Case of a kernel thread, nothing to dump */
5146 perf_output_put(handle
, size
);
5155 * - the size requested by user or the best one we can fit
5156 * in to the sample max size
5158 * - user stack dump data
5160 * - the actual dumped size
5164 perf_output_put(handle
, dump_size
);
5167 sp
= perf_user_stack_pointer(regs
);
5168 rem
= __output_copy_user(handle
, (void *) sp
, dump_size
);
5169 dyn_size
= dump_size
- rem
;
5171 perf_output_skip(handle
, rem
);
5174 perf_output_put(handle
, dyn_size
);
5178 static void __perf_event_header__init_id(struct perf_event_header
*header
,
5179 struct perf_sample_data
*data
,
5180 struct perf_event
*event
)
5182 u64 sample_type
= event
->attr
.sample_type
;
5184 data
->type
= sample_type
;
5185 header
->size
+= event
->id_header_size
;
5187 if (sample_type
& PERF_SAMPLE_TID
) {
5188 /* namespace issues */
5189 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
5190 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
5193 if (sample_type
& PERF_SAMPLE_TIME
)
5194 data
->time
= perf_event_clock(event
);
5196 if (sample_type
& (PERF_SAMPLE_ID
| PERF_SAMPLE_IDENTIFIER
))
5197 data
->id
= primary_event_id(event
);
5199 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
5200 data
->stream_id
= event
->id
;
5202 if (sample_type
& PERF_SAMPLE_CPU
) {
5203 data
->cpu_entry
.cpu
= raw_smp_processor_id();
5204 data
->cpu_entry
.reserved
= 0;
5208 void perf_event_header__init_id(struct perf_event_header
*header
,
5209 struct perf_sample_data
*data
,
5210 struct perf_event
*event
)
5212 if (event
->attr
.sample_id_all
)
5213 __perf_event_header__init_id(header
, data
, event
);
5216 static void __perf_event__output_id_sample(struct perf_output_handle
*handle
,
5217 struct perf_sample_data
*data
)
5219 u64 sample_type
= data
->type
;
5221 if (sample_type
& PERF_SAMPLE_TID
)
5222 perf_output_put(handle
, data
->tid_entry
);
5224 if (sample_type
& PERF_SAMPLE_TIME
)
5225 perf_output_put(handle
, data
->time
);
5227 if (sample_type
& PERF_SAMPLE_ID
)
5228 perf_output_put(handle
, data
->id
);
5230 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
5231 perf_output_put(handle
, data
->stream_id
);
5233 if (sample_type
& PERF_SAMPLE_CPU
)
5234 perf_output_put(handle
, data
->cpu_entry
);
5236 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
5237 perf_output_put(handle
, data
->id
);
5240 void perf_event__output_id_sample(struct perf_event
*event
,
5241 struct perf_output_handle
*handle
,
5242 struct perf_sample_data
*sample
)
5244 if (event
->attr
.sample_id_all
)
5245 __perf_event__output_id_sample(handle
, sample
);
5248 static void perf_output_read_one(struct perf_output_handle
*handle
,
5249 struct perf_event
*event
,
5250 u64 enabled
, u64 running
)
5252 u64 read_format
= event
->attr
.read_format
;
5256 values
[n
++] = perf_event_count(event
);
5257 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
5258 values
[n
++] = enabled
+
5259 atomic64_read(&event
->child_total_time_enabled
);
5261 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
5262 values
[n
++] = running
+
5263 atomic64_read(&event
->child_total_time_running
);
5265 if (read_format
& PERF_FORMAT_ID
)
5266 values
[n
++] = primary_event_id(event
);
5268 __output_copy(handle
, values
, n
* sizeof(u64
));
5272 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
5274 static void perf_output_read_group(struct perf_output_handle
*handle
,
5275 struct perf_event
*event
,
5276 u64 enabled
, u64 running
)
5278 struct perf_event
*leader
= event
->group_leader
, *sub
;
5279 u64 read_format
= event
->attr
.read_format
;
5283 values
[n
++] = 1 + leader
->nr_siblings
;
5285 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
5286 values
[n
++] = enabled
;
5288 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
5289 values
[n
++] = running
;
5291 if (leader
!= event
)
5292 leader
->pmu
->read(leader
);
5294 values
[n
++] = perf_event_count(leader
);
5295 if (read_format
& PERF_FORMAT_ID
)
5296 values
[n
++] = primary_event_id(leader
);
5298 __output_copy(handle
, values
, n
* sizeof(u64
));
5300 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
5303 if ((sub
!= event
) &&
5304 (sub
->state
== PERF_EVENT_STATE_ACTIVE
))
5305 sub
->pmu
->read(sub
);
5307 values
[n
++] = perf_event_count(sub
);
5308 if (read_format
& PERF_FORMAT_ID
)
5309 values
[n
++] = primary_event_id(sub
);
5311 __output_copy(handle
, values
, n
* sizeof(u64
));
5315 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5316 PERF_FORMAT_TOTAL_TIME_RUNNING)
5318 static void perf_output_read(struct perf_output_handle
*handle
,
5319 struct perf_event
*event
)
5321 u64 enabled
= 0, running
= 0, now
;
5322 u64 read_format
= event
->attr
.read_format
;
5325 * compute total_time_enabled, total_time_running
5326 * based on snapshot values taken when the event
5327 * was last scheduled in.
5329 * we cannot simply called update_context_time()
5330 * because of locking issue as we are called in
5333 if (read_format
& PERF_FORMAT_TOTAL_TIMES
)
5334 calc_timer_values(event
, &now
, &enabled
, &running
);
5336 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
5337 perf_output_read_group(handle
, event
, enabled
, running
);
5339 perf_output_read_one(handle
, event
, enabled
, running
);
5342 void perf_output_sample(struct perf_output_handle
*handle
,
5343 struct perf_event_header
*header
,
5344 struct perf_sample_data
*data
,
5345 struct perf_event
*event
)
5347 u64 sample_type
= data
->type
;
5349 perf_output_put(handle
, *header
);
5351 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
5352 perf_output_put(handle
, data
->id
);
5354 if (sample_type
& PERF_SAMPLE_IP
)
5355 perf_output_put(handle
, data
->ip
);
5357 if (sample_type
& PERF_SAMPLE_TID
)
5358 perf_output_put(handle
, data
->tid_entry
);
5360 if (sample_type
& PERF_SAMPLE_TIME
)
5361 perf_output_put(handle
, data
->time
);
5363 if (sample_type
& PERF_SAMPLE_ADDR
)
5364 perf_output_put(handle
, data
->addr
);
5366 if (sample_type
& PERF_SAMPLE_ID
)
5367 perf_output_put(handle
, data
->id
);
5369 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
5370 perf_output_put(handle
, data
->stream_id
);
5372 if (sample_type
& PERF_SAMPLE_CPU
)
5373 perf_output_put(handle
, data
->cpu_entry
);
5375 if (sample_type
& PERF_SAMPLE_PERIOD
)
5376 perf_output_put(handle
, data
->period
);
5378 if (sample_type
& PERF_SAMPLE_READ
)
5379 perf_output_read(handle
, event
);
5381 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
5382 if (data
->callchain
) {
5385 if (data
->callchain
)
5386 size
+= data
->callchain
->nr
;
5388 size
*= sizeof(u64
);
5390 __output_copy(handle
, data
->callchain
, size
);
5393 perf_output_put(handle
, nr
);
5397 if (sample_type
& PERF_SAMPLE_RAW
) {
5399 u32 raw_size
= data
->raw
->size
;
5400 u32 real_size
= round_up(raw_size
+ sizeof(u32
),
5401 sizeof(u64
)) - sizeof(u32
);
5404 perf_output_put(handle
, real_size
);
5405 __output_copy(handle
, data
->raw
->data
, raw_size
);
5406 if (real_size
- raw_size
)
5407 __output_copy(handle
, &zero
, real_size
- raw_size
);
5413 .size
= sizeof(u32
),
5416 perf_output_put(handle
, raw
);
5420 if (sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
5421 if (data
->br_stack
) {
5424 size
= data
->br_stack
->nr
5425 * sizeof(struct perf_branch_entry
);
5427 perf_output_put(handle
, data
->br_stack
->nr
);
5428 perf_output_copy(handle
, data
->br_stack
->entries
, size
);
5431 * we always store at least the value of nr
5434 perf_output_put(handle
, nr
);
5438 if (sample_type
& PERF_SAMPLE_REGS_USER
) {
5439 u64 abi
= data
->regs_user
.abi
;
5442 * If there are no regs to dump, notice it through
5443 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5445 perf_output_put(handle
, abi
);
5448 u64 mask
= event
->attr
.sample_regs_user
;
5449 perf_output_sample_regs(handle
,
5450 data
->regs_user
.regs
,
5455 if (sample_type
& PERF_SAMPLE_STACK_USER
) {
5456 perf_output_sample_ustack(handle
,
5457 data
->stack_user_size
,
5458 data
->regs_user
.regs
);
5461 if (sample_type
& PERF_SAMPLE_WEIGHT
)
5462 perf_output_put(handle
, data
->weight
);
5464 if (sample_type
& PERF_SAMPLE_DATA_SRC
)
5465 perf_output_put(handle
, data
->data_src
.val
);
5467 if (sample_type
& PERF_SAMPLE_TRANSACTION
)
5468 perf_output_put(handle
, data
->txn
);
5470 if (sample_type
& PERF_SAMPLE_REGS_INTR
) {
5471 u64 abi
= data
->regs_intr
.abi
;
5473 * If there are no regs to dump, notice it through
5474 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5476 perf_output_put(handle
, abi
);
5479 u64 mask
= event
->attr
.sample_regs_intr
;
5481 perf_output_sample_regs(handle
,
5482 data
->regs_intr
.regs
,
5487 if (!event
->attr
.watermark
) {
5488 int wakeup_events
= event
->attr
.wakeup_events
;
5490 if (wakeup_events
) {
5491 struct ring_buffer
*rb
= handle
->rb
;
5492 int events
= local_inc_return(&rb
->events
);
5494 if (events
>= wakeup_events
) {
5495 local_sub(wakeup_events
, &rb
->events
);
5496 local_inc(&rb
->wakeup
);
5502 void perf_prepare_sample(struct perf_event_header
*header
,
5503 struct perf_sample_data
*data
,
5504 struct perf_event
*event
,
5505 struct pt_regs
*regs
)
5507 u64 sample_type
= event
->attr
.sample_type
;
5509 header
->type
= PERF_RECORD_SAMPLE
;
5510 header
->size
= sizeof(*header
) + event
->header_size
;
5513 header
->misc
|= perf_misc_flags(regs
);
5515 __perf_event_header__init_id(header
, data
, event
);
5517 if (sample_type
& PERF_SAMPLE_IP
)
5518 data
->ip
= perf_instruction_pointer(regs
);
5520 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
5523 data
->callchain
= perf_callchain(event
, regs
);
5525 if (data
->callchain
)
5526 size
+= data
->callchain
->nr
;
5528 header
->size
+= size
* sizeof(u64
);
5531 if (sample_type
& PERF_SAMPLE_RAW
) {
5532 int size
= sizeof(u32
);
5535 size
+= data
->raw
->size
;
5537 size
+= sizeof(u32
);
5539 header
->size
+= round_up(size
, sizeof(u64
));
5542 if (sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
5543 int size
= sizeof(u64
); /* nr */
5544 if (data
->br_stack
) {
5545 size
+= data
->br_stack
->nr
5546 * sizeof(struct perf_branch_entry
);
5548 header
->size
+= size
;
5551 if (sample_type
& (PERF_SAMPLE_REGS_USER
| PERF_SAMPLE_STACK_USER
))
5552 perf_sample_regs_user(&data
->regs_user
, regs
,
5553 &data
->regs_user_copy
);
5555 if (sample_type
& PERF_SAMPLE_REGS_USER
) {
5556 /* regs dump ABI info */
5557 int size
= sizeof(u64
);
5559 if (data
->regs_user
.regs
) {
5560 u64 mask
= event
->attr
.sample_regs_user
;
5561 size
+= hweight64(mask
) * sizeof(u64
);
5564 header
->size
+= size
;
5567 if (sample_type
& PERF_SAMPLE_STACK_USER
) {
5569 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5570 * processed as the last one or have additional check added
5571 * in case new sample type is added, because we could eat
5572 * up the rest of the sample size.
5574 u16 stack_size
= event
->attr
.sample_stack_user
;
5575 u16 size
= sizeof(u64
);
5577 stack_size
= perf_sample_ustack_size(stack_size
, header
->size
,
5578 data
->regs_user
.regs
);
5581 * If there is something to dump, add space for the dump
5582 * itself and for the field that tells the dynamic size,
5583 * which is how many have been actually dumped.
5586 size
+= sizeof(u64
) + stack_size
;
5588 data
->stack_user_size
= stack_size
;
5589 header
->size
+= size
;
5592 if (sample_type
& PERF_SAMPLE_REGS_INTR
) {
5593 /* regs dump ABI info */
5594 int size
= sizeof(u64
);
5596 perf_sample_regs_intr(&data
->regs_intr
, regs
);
5598 if (data
->regs_intr
.regs
) {
5599 u64 mask
= event
->attr
.sample_regs_intr
;
5601 size
+= hweight64(mask
) * sizeof(u64
);
5604 header
->size
+= size
;
5608 void perf_event_output(struct perf_event
*event
,
5609 struct perf_sample_data
*data
,
5610 struct pt_regs
*regs
)
5612 struct perf_output_handle handle
;
5613 struct perf_event_header header
;
5615 /* protect the callchain buffers */
5618 perf_prepare_sample(&header
, data
, event
, regs
);
5620 if (perf_output_begin(&handle
, event
, header
.size
))
5623 perf_output_sample(&handle
, &header
, data
, event
);
5625 perf_output_end(&handle
);
5635 struct perf_read_event
{
5636 struct perf_event_header header
;
5643 perf_event_read_event(struct perf_event
*event
,
5644 struct task_struct
*task
)
5646 struct perf_output_handle handle
;
5647 struct perf_sample_data sample
;
5648 struct perf_read_event read_event
= {
5650 .type
= PERF_RECORD_READ
,
5652 .size
= sizeof(read_event
) + event
->read_size
,
5654 .pid
= perf_event_pid(event
, task
),
5655 .tid
= perf_event_tid(event
, task
),
5659 perf_event_header__init_id(&read_event
.header
, &sample
, event
);
5660 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
);
5664 perf_output_put(&handle
, read_event
);
5665 perf_output_read(&handle
, event
);
5666 perf_event__output_id_sample(event
, &handle
, &sample
);
5668 perf_output_end(&handle
);
5671 typedef void (perf_event_aux_output_cb
)(struct perf_event
*event
, void *data
);
5674 perf_event_aux_ctx(struct perf_event_context
*ctx
,
5675 perf_event_aux_output_cb output
,
5678 struct perf_event
*event
;
5680 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
5681 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
5683 if (!event_filter_match(event
))
5685 output(event
, data
);
5690 perf_event_aux_task_ctx(perf_event_aux_output_cb output
, void *data
,
5691 struct perf_event_context
*task_ctx
)
5695 perf_event_aux_ctx(task_ctx
, output
, data
);
5701 perf_event_aux(perf_event_aux_output_cb output
, void *data
,
5702 struct perf_event_context
*task_ctx
)
5704 struct perf_cpu_context
*cpuctx
;
5705 struct perf_event_context
*ctx
;
5710 * If we have task_ctx != NULL we only notify
5711 * the task context itself. The task_ctx is set
5712 * only for EXIT events before releasing task
5716 perf_event_aux_task_ctx(output
, data
, task_ctx
);
5721 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
5722 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
5723 if (cpuctx
->unique_pmu
!= pmu
)
5725 perf_event_aux_ctx(&cpuctx
->ctx
, output
, data
);
5726 ctxn
= pmu
->task_ctx_nr
;
5729 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
5731 perf_event_aux_ctx(ctx
, output
, data
);
5733 put_cpu_ptr(pmu
->pmu_cpu_context
);
5739 * task tracking -- fork/exit
5741 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
5744 struct perf_task_event
{
5745 struct task_struct
*task
;
5746 struct perf_event_context
*task_ctx
;
5749 struct perf_event_header header
;
5759 static int perf_event_task_match(struct perf_event
*event
)
5761 return event
->attr
.comm
|| event
->attr
.mmap
||
5762 event
->attr
.mmap2
|| event
->attr
.mmap_data
||
5766 static void perf_event_task_output(struct perf_event
*event
,
5769 struct perf_task_event
*task_event
= data
;
5770 struct perf_output_handle handle
;
5771 struct perf_sample_data sample
;
5772 struct task_struct
*task
= task_event
->task
;
5773 int ret
, size
= task_event
->event_id
.header
.size
;
5775 if (!perf_event_task_match(event
))
5778 perf_event_header__init_id(&task_event
->event_id
.header
, &sample
, event
);
5780 ret
= perf_output_begin(&handle
, event
,
5781 task_event
->event_id
.header
.size
);
5785 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
5786 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
5788 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
5789 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
5791 task_event
->event_id
.time
= perf_event_clock(event
);
5793 perf_output_put(&handle
, task_event
->event_id
);
5795 perf_event__output_id_sample(event
, &handle
, &sample
);
5797 perf_output_end(&handle
);
5799 task_event
->event_id
.header
.size
= size
;
5802 static void perf_event_task(struct task_struct
*task
,
5803 struct perf_event_context
*task_ctx
,
5806 struct perf_task_event task_event
;
5808 if (!atomic_read(&nr_comm_events
) &&
5809 !atomic_read(&nr_mmap_events
) &&
5810 !atomic_read(&nr_task_events
))
5813 task_event
= (struct perf_task_event
){
5815 .task_ctx
= task_ctx
,
5818 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
5820 .size
= sizeof(task_event
.event_id
),
5830 perf_event_aux(perf_event_task_output
,
5835 void perf_event_fork(struct task_struct
*task
)
5837 perf_event_task(task
, NULL
, 1);
5844 struct perf_comm_event
{
5845 struct task_struct
*task
;
5850 struct perf_event_header header
;
5857 static int perf_event_comm_match(struct perf_event
*event
)
5859 return event
->attr
.comm
;
5862 static void perf_event_comm_output(struct perf_event
*event
,
5865 struct perf_comm_event
*comm_event
= data
;
5866 struct perf_output_handle handle
;
5867 struct perf_sample_data sample
;
5868 int size
= comm_event
->event_id
.header
.size
;
5871 if (!perf_event_comm_match(event
))
5874 perf_event_header__init_id(&comm_event
->event_id
.header
, &sample
, event
);
5875 ret
= perf_output_begin(&handle
, event
,
5876 comm_event
->event_id
.header
.size
);
5881 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
5882 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
5884 perf_output_put(&handle
, comm_event
->event_id
);
5885 __output_copy(&handle
, comm_event
->comm
,
5886 comm_event
->comm_size
);
5888 perf_event__output_id_sample(event
, &handle
, &sample
);
5890 perf_output_end(&handle
);
5892 comm_event
->event_id
.header
.size
= size
;
5895 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
5897 char comm
[TASK_COMM_LEN
];
5900 memset(comm
, 0, sizeof(comm
));
5901 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
5902 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
5904 comm_event
->comm
= comm
;
5905 comm_event
->comm_size
= size
;
5907 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
5909 perf_event_aux(perf_event_comm_output
,
5914 void perf_event_comm(struct task_struct
*task
, bool exec
)
5916 struct perf_comm_event comm_event
;
5918 if (!atomic_read(&nr_comm_events
))
5921 comm_event
= (struct perf_comm_event
){
5927 .type
= PERF_RECORD_COMM
,
5928 .misc
= exec
? PERF_RECORD_MISC_COMM_EXEC
: 0,
5936 perf_event_comm_event(&comm_event
);
5943 struct perf_mmap_event
{
5944 struct vm_area_struct
*vma
;
5946 const char *file_name
;
5954 struct perf_event_header header
;
5964 static int perf_event_mmap_match(struct perf_event
*event
,
5967 struct perf_mmap_event
*mmap_event
= data
;
5968 struct vm_area_struct
*vma
= mmap_event
->vma
;
5969 int executable
= vma
->vm_flags
& VM_EXEC
;
5971 return (!executable
&& event
->attr
.mmap_data
) ||
5972 (executable
&& (event
->attr
.mmap
|| event
->attr
.mmap2
));
5975 static void perf_event_mmap_output(struct perf_event
*event
,
5978 struct perf_mmap_event
*mmap_event
= data
;
5979 struct perf_output_handle handle
;
5980 struct perf_sample_data sample
;
5981 int size
= mmap_event
->event_id
.header
.size
;
5984 if (!perf_event_mmap_match(event
, data
))
5987 if (event
->attr
.mmap2
) {
5988 mmap_event
->event_id
.header
.type
= PERF_RECORD_MMAP2
;
5989 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->maj
);
5990 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->min
);
5991 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->ino
);
5992 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->ino_generation
);
5993 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->prot
);
5994 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->flags
);
5997 perf_event_header__init_id(&mmap_event
->event_id
.header
, &sample
, event
);
5998 ret
= perf_output_begin(&handle
, event
,
5999 mmap_event
->event_id
.header
.size
);
6003 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
6004 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
6006 perf_output_put(&handle
, mmap_event
->event_id
);
6008 if (event
->attr
.mmap2
) {
6009 perf_output_put(&handle
, mmap_event
->maj
);
6010 perf_output_put(&handle
, mmap_event
->min
);
6011 perf_output_put(&handle
, mmap_event
->ino
);
6012 perf_output_put(&handle
, mmap_event
->ino_generation
);
6013 perf_output_put(&handle
, mmap_event
->prot
);
6014 perf_output_put(&handle
, mmap_event
->flags
);
6017 __output_copy(&handle
, mmap_event
->file_name
,
6018 mmap_event
->file_size
);
6020 perf_event__output_id_sample(event
, &handle
, &sample
);
6022 perf_output_end(&handle
);
6024 mmap_event
->event_id
.header
.size
= size
;
6027 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
6029 struct vm_area_struct
*vma
= mmap_event
->vma
;
6030 struct file
*file
= vma
->vm_file
;
6031 int maj
= 0, min
= 0;
6032 u64 ino
= 0, gen
= 0;
6033 u32 prot
= 0, flags
= 0;
6040 struct inode
*inode
;
6043 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
6049 * d_path() works from the end of the rb backwards, so we
6050 * need to add enough zero bytes after the string to handle
6051 * the 64bit alignment we do later.
6053 name
= file_path(file
, buf
, PATH_MAX
- sizeof(u64
));
6058 inode
= file_inode(vma
->vm_file
);
6059 dev
= inode
->i_sb
->s_dev
;
6061 gen
= inode
->i_generation
;
6065 if (vma
->vm_flags
& VM_READ
)
6067 if (vma
->vm_flags
& VM_WRITE
)
6069 if (vma
->vm_flags
& VM_EXEC
)
6072 if (vma
->vm_flags
& VM_MAYSHARE
)
6075 flags
= MAP_PRIVATE
;
6077 if (vma
->vm_flags
& VM_DENYWRITE
)
6078 flags
|= MAP_DENYWRITE
;
6079 if (vma
->vm_flags
& VM_MAYEXEC
)
6080 flags
|= MAP_EXECUTABLE
;
6081 if (vma
->vm_flags
& VM_LOCKED
)
6082 flags
|= MAP_LOCKED
;
6083 if (vma
->vm_flags
& VM_HUGETLB
)
6084 flags
|= MAP_HUGETLB
;
6088 if (vma
->vm_ops
&& vma
->vm_ops
->name
) {
6089 name
= (char *) vma
->vm_ops
->name(vma
);
6094 name
= (char *)arch_vma_name(vma
);
6098 if (vma
->vm_start
<= vma
->vm_mm
->start_brk
&&
6099 vma
->vm_end
>= vma
->vm_mm
->brk
) {
6103 if (vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
6104 vma
->vm_end
>= vma
->vm_mm
->start_stack
) {
6114 strlcpy(tmp
, name
, sizeof(tmp
));
6118 * Since our buffer works in 8 byte units we need to align our string
6119 * size to a multiple of 8. However, we must guarantee the tail end is
6120 * zero'd out to avoid leaking random bits to userspace.
6122 size
= strlen(name
)+1;
6123 while (!IS_ALIGNED(size
, sizeof(u64
)))
6124 name
[size
++] = '\0';
6126 mmap_event
->file_name
= name
;
6127 mmap_event
->file_size
= size
;
6128 mmap_event
->maj
= maj
;
6129 mmap_event
->min
= min
;
6130 mmap_event
->ino
= ino
;
6131 mmap_event
->ino_generation
= gen
;
6132 mmap_event
->prot
= prot
;
6133 mmap_event
->flags
= flags
;
6135 if (!(vma
->vm_flags
& VM_EXEC
))
6136 mmap_event
->event_id
.header
.misc
|= PERF_RECORD_MISC_MMAP_DATA
;
6138 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
6140 perf_event_aux(perf_event_mmap_output
,
6147 void perf_event_mmap(struct vm_area_struct
*vma
)
6149 struct perf_mmap_event mmap_event
;
6151 if (!atomic_read(&nr_mmap_events
))
6154 mmap_event
= (struct perf_mmap_event
){
6160 .type
= PERF_RECORD_MMAP
,
6161 .misc
= PERF_RECORD_MISC_USER
,
6166 .start
= vma
->vm_start
,
6167 .len
= vma
->vm_end
- vma
->vm_start
,
6168 .pgoff
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
,
6170 /* .maj (attr_mmap2 only) */
6171 /* .min (attr_mmap2 only) */
6172 /* .ino (attr_mmap2 only) */
6173 /* .ino_generation (attr_mmap2 only) */
6174 /* .prot (attr_mmap2 only) */
6175 /* .flags (attr_mmap2 only) */
6178 perf_event_mmap_event(&mmap_event
);
6181 void perf_event_aux_event(struct perf_event
*event
, unsigned long head
,
6182 unsigned long size
, u64 flags
)
6184 struct perf_output_handle handle
;
6185 struct perf_sample_data sample
;
6186 struct perf_aux_event
{
6187 struct perf_event_header header
;
6193 .type
= PERF_RECORD_AUX
,
6195 .size
= sizeof(rec
),
6203 perf_event_header__init_id(&rec
.header
, &sample
, event
);
6204 ret
= perf_output_begin(&handle
, event
, rec
.header
.size
);
6209 perf_output_put(&handle
, rec
);
6210 perf_event__output_id_sample(event
, &handle
, &sample
);
6212 perf_output_end(&handle
);
6216 * Lost/dropped samples logging
6218 void perf_log_lost_samples(struct perf_event
*event
, u64 lost
)
6220 struct perf_output_handle handle
;
6221 struct perf_sample_data sample
;
6225 struct perf_event_header header
;
6227 } lost_samples_event
= {
6229 .type
= PERF_RECORD_LOST_SAMPLES
,
6231 .size
= sizeof(lost_samples_event
),
6236 perf_event_header__init_id(&lost_samples_event
.header
, &sample
, event
);
6238 ret
= perf_output_begin(&handle
, event
,
6239 lost_samples_event
.header
.size
);
6243 perf_output_put(&handle
, lost_samples_event
);
6244 perf_event__output_id_sample(event
, &handle
, &sample
);
6245 perf_output_end(&handle
);
6249 * context_switch tracking
6252 struct perf_switch_event
{
6253 struct task_struct
*task
;
6254 struct task_struct
*next_prev
;
6257 struct perf_event_header header
;
6263 static int perf_event_switch_match(struct perf_event
*event
)
6265 return event
->attr
.context_switch
;
6268 static void perf_event_switch_output(struct perf_event
*event
, void *data
)
6270 struct perf_switch_event
*se
= data
;
6271 struct perf_output_handle handle
;
6272 struct perf_sample_data sample
;
6275 if (!perf_event_switch_match(event
))
6278 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6279 if (event
->ctx
->task
) {
6280 se
->event_id
.header
.type
= PERF_RECORD_SWITCH
;
6281 se
->event_id
.header
.size
= sizeof(se
->event_id
.header
);
6283 se
->event_id
.header
.type
= PERF_RECORD_SWITCH_CPU_WIDE
;
6284 se
->event_id
.header
.size
= sizeof(se
->event_id
);
6285 se
->event_id
.next_prev_pid
=
6286 perf_event_pid(event
, se
->next_prev
);
6287 se
->event_id
.next_prev_tid
=
6288 perf_event_tid(event
, se
->next_prev
);
6291 perf_event_header__init_id(&se
->event_id
.header
, &sample
, event
);
6293 ret
= perf_output_begin(&handle
, event
, se
->event_id
.header
.size
);
6297 if (event
->ctx
->task
)
6298 perf_output_put(&handle
, se
->event_id
.header
);
6300 perf_output_put(&handle
, se
->event_id
);
6302 perf_event__output_id_sample(event
, &handle
, &sample
);
6304 perf_output_end(&handle
);
6307 static void perf_event_switch(struct task_struct
*task
,
6308 struct task_struct
*next_prev
, bool sched_in
)
6310 struct perf_switch_event switch_event
;
6312 /* N.B. caller checks nr_switch_events != 0 */
6314 switch_event
= (struct perf_switch_event
){
6316 .next_prev
= next_prev
,
6320 .misc
= sched_in
? 0 : PERF_RECORD_MISC_SWITCH_OUT
,
6323 /* .next_prev_pid */
6324 /* .next_prev_tid */
6328 perf_event_aux(perf_event_switch_output
,
6334 * IRQ throttle logging
6337 static void perf_log_throttle(struct perf_event
*event
, int enable
)
6339 struct perf_output_handle handle
;
6340 struct perf_sample_data sample
;
6344 struct perf_event_header header
;
6348 } throttle_event
= {
6350 .type
= PERF_RECORD_THROTTLE
,
6352 .size
= sizeof(throttle_event
),
6354 .time
= perf_event_clock(event
),
6355 .id
= primary_event_id(event
),
6356 .stream_id
= event
->id
,
6360 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
6362 perf_event_header__init_id(&throttle_event
.header
, &sample
, event
);
6364 ret
= perf_output_begin(&handle
, event
,
6365 throttle_event
.header
.size
);
6369 perf_output_put(&handle
, throttle_event
);
6370 perf_event__output_id_sample(event
, &handle
, &sample
);
6371 perf_output_end(&handle
);
6374 static void perf_log_itrace_start(struct perf_event
*event
)
6376 struct perf_output_handle handle
;
6377 struct perf_sample_data sample
;
6378 struct perf_aux_event
{
6379 struct perf_event_header header
;
6386 event
= event
->parent
;
6388 if (!(event
->pmu
->capabilities
& PERF_PMU_CAP_ITRACE
) ||
6389 event
->hw
.itrace_started
)
6392 rec
.header
.type
= PERF_RECORD_ITRACE_START
;
6393 rec
.header
.misc
= 0;
6394 rec
.header
.size
= sizeof(rec
);
6395 rec
.pid
= perf_event_pid(event
, current
);
6396 rec
.tid
= perf_event_tid(event
, current
);
6398 perf_event_header__init_id(&rec
.header
, &sample
, event
);
6399 ret
= perf_output_begin(&handle
, event
, rec
.header
.size
);
6404 perf_output_put(&handle
, rec
);
6405 perf_event__output_id_sample(event
, &handle
, &sample
);
6407 perf_output_end(&handle
);
6411 * Generic event overflow handling, sampling.
6414 static int __perf_event_overflow(struct perf_event
*event
,
6415 int throttle
, struct perf_sample_data
*data
,
6416 struct pt_regs
*regs
)
6418 int events
= atomic_read(&event
->event_limit
);
6419 struct hw_perf_event
*hwc
= &event
->hw
;
6424 * Non-sampling counters might still use the PMI to fold short
6425 * hardware counters, ignore those.
6427 if (unlikely(!is_sampling_event(event
)))
6430 seq
= __this_cpu_read(perf_throttled_seq
);
6431 if (seq
!= hwc
->interrupts_seq
) {
6432 hwc
->interrupts_seq
= seq
;
6433 hwc
->interrupts
= 1;
6436 if (unlikely(throttle
6437 && hwc
->interrupts
>= max_samples_per_tick
)) {
6438 __this_cpu_inc(perf_throttled_count
);
6439 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS
);
6440 hwc
->interrupts
= MAX_INTERRUPTS
;
6441 perf_log_throttle(event
, 0);
6446 if (event
->attr
.freq
) {
6447 u64 now
= perf_clock();
6448 s64 delta
= now
- hwc
->freq_time_stamp
;
6450 hwc
->freq_time_stamp
= now
;
6452 if (delta
> 0 && delta
< 2*TICK_NSEC
)
6453 perf_adjust_period(event
, delta
, hwc
->last_period
, true);
6457 * XXX event_limit might not quite work as expected on inherited
6461 event
->pending_kill
= POLL_IN
;
6462 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
6464 event
->pending_kill
= POLL_HUP
;
6465 event
->pending_disable
= 1;
6466 irq_work_queue(&event
->pending
);
6469 if (event
->overflow_handler
)
6470 event
->overflow_handler(event
, data
, regs
);
6472 perf_event_output(event
, data
, regs
);
6474 if (*perf_event_fasync(event
) && event
->pending_kill
) {
6475 event
->pending_wakeup
= 1;
6476 irq_work_queue(&event
->pending
);
6482 int perf_event_overflow(struct perf_event
*event
,
6483 struct perf_sample_data
*data
,
6484 struct pt_regs
*regs
)
6486 return __perf_event_overflow(event
, 1, data
, regs
);
6490 * Generic software event infrastructure
6493 struct swevent_htable
{
6494 struct swevent_hlist
*swevent_hlist
;
6495 struct mutex hlist_mutex
;
6498 /* Recursion avoidance in each contexts */
6499 int recursion
[PERF_NR_CONTEXTS
];
6502 static DEFINE_PER_CPU(struct swevent_htable
, swevent_htable
);
6505 * We directly increment event->count and keep a second value in
6506 * event->hw.period_left to count intervals. This period event
6507 * is kept in the range [-sample_period, 0] so that we can use the
6511 u64
perf_swevent_set_period(struct perf_event
*event
)
6513 struct hw_perf_event
*hwc
= &event
->hw
;
6514 u64 period
= hwc
->last_period
;
6518 hwc
->last_period
= hwc
->sample_period
;
6521 old
= val
= local64_read(&hwc
->period_left
);
6525 nr
= div64_u64(period
+ val
, period
);
6526 offset
= nr
* period
;
6528 if (local64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
6534 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
6535 struct perf_sample_data
*data
,
6536 struct pt_regs
*regs
)
6538 struct hw_perf_event
*hwc
= &event
->hw
;
6542 overflow
= perf_swevent_set_period(event
);
6544 if (hwc
->interrupts
== MAX_INTERRUPTS
)
6547 for (; overflow
; overflow
--) {
6548 if (__perf_event_overflow(event
, throttle
,
6551 * We inhibit the overflow from happening when
6552 * hwc->interrupts == MAX_INTERRUPTS.
6560 static void perf_swevent_event(struct perf_event
*event
, u64 nr
,
6561 struct perf_sample_data
*data
,
6562 struct pt_regs
*regs
)
6564 struct hw_perf_event
*hwc
= &event
->hw
;
6566 local64_add(nr
, &event
->count
);
6571 if (!is_sampling_event(event
))
6574 if ((event
->attr
.sample_type
& PERF_SAMPLE_PERIOD
) && !event
->attr
.freq
) {
6576 return perf_swevent_overflow(event
, 1, data
, regs
);
6578 data
->period
= event
->hw
.last_period
;
6580 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
6581 return perf_swevent_overflow(event
, 1, data
, regs
);
6583 if (local64_add_negative(nr
, &hwc
->period_left
))
6586 perf_swevent_overflow(event
, 0, data
, regs
);
6589 static int perf_exclude_event(struct perf_event
*event
,
6590 struct pt_regs
*regs
)
6592 if (event
->hw
.state
& PERF_HES_STOPPED
)
6596 if (event
->attr
.exclude_user
&& user_mode(regs
))
6599 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
6606 static int perf_swevent_match(struct perf_event
*event
,
6607 enum perf_type_id type
,
6609 struct perf_sample_data
*data
,
6610 struct pt_regs
*regs
)
6612 if (event
->attr
.type
!= type
)
6615 if (event
->attr
.config
!= event_id
)
6618 if (perf_exclude_event(event
, regs
))
6624 static inline u64
swevent_hash(u64 type
, u32 event_id
)
6626 u64 val
= event_id
| (type
<< 32);
6628 return hash_64(val
, SWEVENT_HLIST_BITS
);
6631 static inline struct hlist_head
*
6632 __find_swevent_head(struct swevent_hlist
*hlist
, u64 type
, u32 event_id
)
6634 u64 hash
= swevent_hash(type
, event_id
);
6636 return &hlist
->heads
[hash
];
6639 /* For the read side: events when they trigger */
6640 static inline struct hlist_head
*
6641 find_swevent_head_rcu(struct swevent_htable
*swhash
, u64 type
, u32 event_id
)
6643 struct swevent_hlist
*hlist
;
6645 hlist
= rcu_dereference(swhash
->swevent_hlist
);
6649 return __find_swevent_head(hlist
, type
, event_id
);
6652 /* For the event head insertion and removal in the hlist */
6653 static inline struct hlist_head
*
6654 find_swevent_head(struct swevent_htable
*swhash
, struct perf_event
*event
)
6656 struct swevent_hlist
*hlist
;
6657 u32 event_id
= event
->attr
.config
;
6658 u64 type
= event
->attr
.type
;
6661 * Event scheduling is always serialized against hlist allocation
6662 * and release. Which makes the protected version suitable here.
6663 * The context lock guarantees that.
6665 hlist
= rcu_dereference_protected(swhash
->swevent_hlist
,
6666 lockdep_is_held(&event
->ctx
->lock
));
6670 return __find_swevent_head(hlist
, type
, event_id
);
6673 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
6675 struct perf_sample_data
*data
,
6676 struct pt_regs
*regs
)
6678 struct swevent_htable
*swhash
= this_cpu_ptr(&swevent_htable
);
6679 struct perf_event
*event
;
6680 struct hlist_head
*head
;
6683 head
= find_swevent_head_rcu(swhash
, type
, event_id
);
6687 hlist_for_each_entry_rcu(event
, head
, hlist_entry
) {
6688 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
6689 perf_swevent_event(event
, nr
, data
, regs
);
6695 DEFINE_PER_CPU(struct pt_regs
, __perf_regs
[4]);
6697 int perf_swevent_get_recursion_context(void)
6699 struct swevent_htable
*swhash
= this_cpu_ptr(&swevent_htable
);
6701 return get_recursion_context(swhash
->recursion
);
6703 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
6705 inline void perf_swevent_put_recursion_context(int rctx
)
6707 struct swevent_htable
*swhash
= this_cpu_ptr(&swevent_htable
);
6709 put_recursion_context(swhash
->recursion
, rctx
);
6712 void ___perf_sw_event(u32 event_id
, u64 nr
, struct pt_regs
*regs
, u64 addr
)
6714 struct perf_sample_data data
;
6716 if (WARN_ON_ONCE(!regs
))
6719 perf_sample_data_init(&data
, addr
, 0);
6720 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, &data
, regs
);
6723 void __perf_sw_event(u32 event_id
, u64 nr
, struct pt_regs
*regs
, u64 addr
)
6727 preempt_disable_notrace();
6728 rctx
= perf_swevent_get_recursion_context();
6729 if (unlikely(rctx
< 0))
6732 ___perf_sw_event(event_id
, nr
, regs
, addr
);
6734 perf_swevent_put_recursion_context(rctx
);
6736 preempt_enable_notrace();
6739 static void perf_swevent_read(struct perf_event
*event
)
6743 static int perf_swevent_add(struct perf_event
*event
, int flags
)
6745 struct swevent_htable
*swhash
= this_cpu_ptr(&swevent_htable
);
6746 struct hw_perf_event
*hwc
= &event
->hw
;
6747 struct hlist_head
*head
;
6749 if (is_sampling_event(event
)) {
6750 hwc
->last_period
= hwc
->sample_period
;
6751 perf_swevent_set_period(event
);
6754 hwc
->state
= !(flags
& PERF_EF_START
);
6756 head
= find_swevent_head(swhash
, event
);
6757 if (WARN_ON_ONCE(!head
))
6760 hlist_add_head_rcu(&event
->hlist_entry
, head
);
6761 perf_event_update_userpage(event
);
6766 static void perf_swevent_del(struct perf_event
*event
, int flags
)
6768 hlist_del_rcu(&event
->hlist_entry
);
6771 static void perf_swevent_start(struct perf_event
*event
, int flags
)
6773 event
->hw
.state
= 0;
6776 static void perf_swevent_stop(struct perf_event
*event
, int flags
)
6778 event
->hw
.state
= PERF_HES_STOPPED
;
6781 /* Deref the hlist from the update side */
6782 static inline struct swevent_hlist
*
6783 swevent_hlist_deref(struct swevent_htable
*swhash
)
6785 return rcu_dereference_protected(swhash
->swevent_hlist
,
6786 lockdep_is_held(&swhash
->hlist_mutex
));
6789 static void swevent_hlist_release(struct swevent_htable
*swhash
)
6791 struct swevent_hlist
*hlist
= swevent_hlist_deref(swhash
);
6796 RCU_INIT_POINTER(swhash
->swevent_hlist
, NULL
);
6797 kfree_rcu(hlist
, rcu_head
);
6800 static void swevent_hlist_put_cpu(int cpu
)
6802 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
6804 mutex_lock(&swhash
->hlist_mutex
);
6806 if (!--swhash
->hlist_refcount
)
6807 swevent_hlist_release(swhash
);
6809 mutex_unlock(&swhash
->hlist_mutex
);
6812 static void swevent_hlist_put(void)
6816 for_each_possible_cpu(cpu
)
6817 swevent_hlist_put_cpu(cpu
);
6820 static int swevent_hlist_get_cpu(int cpu
)
6822 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
6825 mutex_lock(&swhash
->hlist_mutex
);
6826 if (!swevent_hlist_deref(swhash
) && cpu_online(cpu
)) {
6827 struct swevent_hlist
*hlist
;
6829 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
6834 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
6836 swhash
->hlist_refcount
++;
6838 mutex_unlock(&swhash
->hlist_mutex
);
6843 static int swevent_hlist_get(void)
6845 int err
, cpu
, failed_cpu
;
6848 for_each_possible_cpu(cpu
) {
6849 err
= swevent_hlist_get_cpu(cpu
);
6859 for_each_possible_cpu(cpu
) {
6860 if (cpu
== failed_cpu
)
6862 swevent_hlist_put_cpu(cpu
);
6869 struct static_key perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
6871 static void sw_perf_event_destroy(struct perf_event
*event
)
6873 u64 event_id
= event
->attr
.config
;
6875 WARN_ON(event
->parent
);
6877 static_key_slow_dec(&perf_swevent_enabled
[event_id
]);
6878 swevent_hlist_put();
6881 static int perf_swevent_init(struct perf_event
*event
)
6883 u64 event_id
= event
->attr
.config
;
6885 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
6889 * no branch sampling for software events
6891 if (has_branch_stack(event
))
6895 case PERF_COUNT_SW_CPU_CLOCK
:
6896 case PERF_COUNT_SW_TASK_CLOCK
:
6903 if (event_id
>= PERF_COUNT_SW_MAX
)
6906 if (!event
->parent
) {
6909 err
= swevent_hlist_get();
6913 static_key_slow_inc(&perf_swevent_enabled
[event_id
]);
6914 event
->destroy
= sw_perf_event_destroy
;
6920 static struct pmu perf_swevent
= {
6921 .task_ctx_nr
= perf_sw_context
,
6923 .capabilities
= PERF_PMU_CAP_NO_NMI
,
6925 .event_init
= perf_swevent_init
,
6926 .add
= perf_swevent_add
,
6927 .del
= perf_swevent_del
,
6928 .start
= perf_swevent_start
,
6929 .stop
= perf_swevent_stop
,
6930 .read
= perf_swevent_read
,
6933 #ifdef CONFIG_EVENT_TRACING
6935 static int perf_tp_filter_match(struct perf_event
*event
,
6936 struct perf_sample_data
*data
)
6938 void *record
= data
->raw
->data
;
6940 /* only top level events have filters set */
6942 event
= event
->parent
;
6944 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
6949 static int perf_tp_event_match(struct perf_event
*event
,
6950 struct perf_sample_data
*data
,
6951 struct pt_regs
*regs
)
6953 if (event
->hw
.state
& PERF_HES_STOPPED
)
6956 * All tracepoints are from kernel-space.
6958 if (event
->attr
.exclude_kernel
)
6961 if (!perf_tp_filter_match(event
, data
))
6967 void perf_tp_event(u64 addr
, u64 count
, void *record
, int entry_size
,
6968 struct pt_regs
*regs
, struct hlist_head
*head
, int rctx
,
6969 struct task_struct
*task
)
6971 struct perf_sample_data data
;
6972 struct perf_event
*event
;
6974 struct perf_raw_record raw
= {
6979 perf_sample_data_init(&data
, addr
, 0);
6982 hlist_for_each_entry_rcu(event
, head
, hlist_entry
) {
6983 if (perf_tp_event_match(event
, &data
, regs
))
6984 perf_swevent_event(event
, count
, &data
, regs
);
6988 * If we got specified a target task, also iterate its context and
6989 * deliver this event there too.
6991 if (task
&& task
!= current
) {
6992 struct perf_event_context
*ctx
;
6993 struct trace_entry
*entry
= record
;
6996 ctx
= rcu_dereference(task
->perf_event_ctxp
[perf_sw_context
]);
7000 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
7001 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
7003 if (event
->attr
.config
!= entry
->type
)
7005 if (perf_tp_event_match(event
, &data
, regs
))
7006 perf_swevent_event(event
, count
, &data
, regs
);
7012 perf_swevent_put_recursion_context(rctx
);
7014 EXPORT_SYMBOL_GPL(perf_tp_event
);
7016 static void tp_perf_event_destroy(struct perf_event
*event
)
7018 perf_trace_destroy(event
);
7021 static int perf_tp_event_init(struct perf_event
*event
)
7025 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
7029 * no branch sampling for tracepoint events
7031 if (has_branch_stack(event
))
7034 err
= perf_trace_init(event
);
7038 event
->destroy
= tp_perf_event_destroy
;
7043 static struct pmu perf_tracepoint
= {
7044 .task_ctx_nr
= perf_sw_context
,
7046 .event_init
= perf_tp_event_init
,
7047 .add
= perf_trace_add
,
7048 .del
= perf_trace_del
,
7049 .start
= perf_swevent_start
,
7050 .stop
= perf_swevent_stop
,
7051 .read
= perf_swevent_read
,
7054 static inline void perf_tp_register(void)
7056 perf_pmu_register(&perf_tracepoint
, "tracepoint", PERF_TYPE_TRACEPOINT
);
7059 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
7064 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
7067 filter_str
= strndup_user(arg
, PAGE_SIZE
);
7068 if (IS_ERR(filter_str
))
7069 return PTR_ERR(filter_str
);
7071 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
, filter_str
);
7077 static void perf_event_free_filter(struct perf_event
*event
)
7079 ftrace_profile_free_filter(event
);
7082 static int perf_event_set_bpf_prog(struct perf_event
*event
, u32 prog_fd
)
7084 struct bpf_prog
*prog
;
7086 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
7089 if (event
->tp_event
->prog
)
7092 if (!(event
->tp_event
->flags
& TRACE_EVENT_FL_UKPROBE
))
7093 /* bpf programs can only be attached to u/kprobes */
7096 prog
= bpf_prog_get(prog_fd
);
7098 return PTR_ERR(prog
);
7100 if (prog
->type
!= BPF_PROG_TYPE_KPROBE
) {
7101 /* valid fd, but invalid bpf program type */
7106 event
->tp_event
->prog
= prog
;
7111 static void perf_event_free_bpf_prog(struct perf_event
*event
)
7113 struct bpf_prog
*prog
;
7115 if (!event
->tp_event
)
7118 prog
= event
->tp_event
->prog
;
7120 event
->tp_event
->prog
= NULL
;
7127 static inline void perf_tp_register(void)
7131 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
7136 static void perf_event_free_filter(struct perf_event
*event
)
7140 static int perf_event_set_bpf_prog(struct perf_event
*event
, u32 prog_fd
)
7145 static void perf_event_free_bpf_prog(struct perf_event
*event
)
7148 #endif /* CONFIG_EVENT_TRACING */
7150 #ifdef CONFIG_HAVE_HW_BREAKPOINT
7151 void perf_bp_event(struct perf_event
*bp
, void *data
)
7153 struct perf_sample_data sample
;
7154 struct pt_regs
*regs
= data
;
7156 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
, 0);
7158 if (!bp
->hw
.state
&& !perf_exclude_event(bp
, regs
))
7159 perf_swevent_event(bp
, 1, &sample
, regs
);
7164 * hrtimer based swevent callback
7167 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
7169 enum hrtimer_restart ret
= HRTIMER_RESTART
;
7170 struct perf_sample_data data
;
7171 struct pt_regs
*regs
;
7172 struct perf_event
*event
;
7175 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
7177 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
7178 return HRTIMER_NORESTART
;
7180 event
->pmu
->read(event
);
7182 perf_sample_data_init(&data
, 0, event
->hw
.last_period
);
7183 regs
= get_irq_regs();
7185 if (regs
&& !perf_exclude_event(event
, regs
)) {
7186 if (!(event
->attr
.exclude_idle
&& is_idle_task(current
)))
7187 if (__perf_event_overflow(event
, 1, &data
, regs
))
7188 ret
= HRTIMER_NORESTART
;
7191 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
7192 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
7197 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
7199 struct hw_perf_event
*hwc
= &event
->hw
;
7202 if (!is_sampling_event(event
))
7205 period
= local64_read(&hwc
->period_left
);
7210 local64_set(&hwc
->period_left
, 0);
7212 period
= max_t(u64
, 10000, hwc
->sample_period
);
7214 hrtimer_start(&hwc
->hrtimer
, ns_to_ktime(period
),
7215 HRTIMER_MODE_REL_PINNED
);
7218 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
7220 struct hw_perf_event
*hwc
= &event
->hw
;
7222 if (is_sampling_event(event
)) {
7223 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
7224 local64_set(&hwc
->period_left
, ktime_to_ns(remaining
));
7226 hrtimer_cancel(&hwc
->hrtimer
);
7230 static void perf_swevent_init_hrtimer(struct perf_event
*event
)
7232 struct hw_perf_event
*hwc
= &event
->hw
;
7234 if (!is_sampling_event(event
))
7237 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
7238 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
7241 * Since hrtimers have a fixed rate, we can do a static freq->period
7242 * mapping and avoid the whole period adjust feedback stuff.
7244 if (event
->attr
.freq
) {
7245 long freq
= event
->attr
.sample_freq
;
7247 event
->attr
.sample_period
= NSEC_PER_SEC
/ freq
;
7248 hwc
->sample_period
= event
->attr
.sample_period
;
7249 local64_set(&hwc
->period_left
, hwc
->sample_period
);
7250 hwc
->last_period
= hwc
->sample_period
;
7251 event
->attr
.freq
= 0;
7256 * Software event: cpu wall time clock
7259 static void cpu_clock_event_update(struct perf_event
*event
)
7264 now
= local_clock();
7265 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
7266 local64_add(now
- prev
, &event
->count
);
7269 static void cpu_clock_event_start(struct perf_event
*event
, int flags
)
7271 local64_set(&event
->hw
.prev_count
, local_clock());
7272 perf_swevent_start_hrtimer(event
);
7275 static void cpu_clock_event_stop(struct perf_event
*event
, int flags
)
7277 perf_swevent_cancel_hrtimer(event
);
7278 cpu_clock_event_update(event
);
7281 static int cpu_clock_event_add(struct perf_event
*event
, int flags
)
7283 if (flags
& PERF_EF_START
)
7284 cpu_clock_event_start(event
, flags
);
7285 perf_event_update_userpage(event
);
7290 static void cpu_clock_event_del(struct perf_event
*event
, int flags
)
7292 cpu_clock_event_stop(event
, flags
);
7295 static void cpu_clock_event_read(struct perf_event
*event
)
7297 cpu_clock_event_update(event
);
7300 static int cpu_clock_event_init(struct perf_event
*event
)
7302 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
7305 if (event
->attr
.config
!= PERF_COUNT_SW_CPU_CLOCK
)
7309 * no branch sampling for software events
7311 if (has_branch_stack(event
))
7314 perf_swevent_init_hrtimer(event
);
7319 static struct pmu perf_cpu_clock
= {
7320 .task_ctx_nr
= perf_sw_context
,
7322 .capabilities
= PERF_PMU_CAP_NO_NMI
,
7324 .event_init
= cpu_clock_event_init
,
7325 .add
= cpu_clock_event_add
,
7326 .del
= cpu_clock_event_del
,
7327 .start
= cpu_clock_event_start
,
7328 .stop
= cpu_clock_event_stop
,
7329 .read
= cpu_clock_event_read
,
7333 * Software event: task time clock
7336 static void task_clock_event_update(struct perf_event
*event
, u64 now
)
7341 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
7343 local64_add(delta
, &event
->count
);
7346 static void task_clock_event_start(struct perf_event
*event
, int flags
)
7348 local64_set(&event
->hw
.prev_count
, event
->ctx
->time
);
7349 perf_swevent_start_hrtimer(event
);
7352 static void task_clock_event_stop(struct perf_event
*event
, int flags
)
7354 perf_swevent_cancel_hrtimer(event
);
7355 task_clock_event_update(event
, event
->ctx
->time
);
7358 static int task_clock_event_add(struct perf_event
*event
, int flags
)
7360 if (flags
& PERF_EF_START
)
7361 task_clock_event_start(event
, flags
);
7362 perf_event_update_userpage(event
);
7367 static void task_clock_event_del(struct perf_event
*event
, int flags
)
7369 task_clock_event_stop(event
, PERF_EF_UPDATE
);
7372 static void task_clock_event_read(struct perf_event
*event
)
7374 u64 now
= perf_clock();
7375 u64 delta
= now
- event
->ctx
->timestamp
;
7376 u64 time
= event
->ctx
->time
+ delta
;
7378 task_clock_event_update(event
, time
);
7381 static int task_clock_event_init(struct perf_event
*event
)
7383 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
7386 if (event
->attr
.config
!= PERF_COUNT_SW_TASK_CLOCK
)
7390 * no branch sampling for software events
7392 if (has_branch_stack(event
))
7395 perf_swevent_init_hrtimer(event
);
7400 static struct pmu perf_task_clock
= {
7401 .task_ctx_nr
= perf_sw_context
,
7403 .capabilities
= PERF_PMU_CAP_NO_NMI
,
7405 .event_init
= task_clock_event_init
,
7406 .add
= task_clock_event_add
,
7407 .del
= task_clock_event_del
,
7408 .start
= task_clock_event_start
,
7409 .stop
= task_clock_event_stop
,
7410 .read
= task_clock_event_read
,
7413 static void perf_pmu_nop_void(struct pmu
*pmu
)
7417 static void perf_pmu_nop_txn(struct pmu
*pmu
, unsigned int flags
)
7421 static int perf_pmu_nop_int(struct pmu
*pmu
)
7426 static DEFINE_PER_CPU(unsigned int, nop_txn_flags
);
7428 static void perf_pmu_start_txn(struct pmu
*pmu
, unsigned int flags
)
7430 __this_cpu_write(nop_txn_flags
, flags
);
7432 if (flags
& ~PERF_PMU_TXN_ADD
)
7435 perf_pmu_disable(pmu
);
7438 static int perf_pmu_commit_txn(struct pmu
*pmu
)
7440 unsigned int flags
= __this_cpu_read(nop_txn_flags
);
7442 __this_cpu_write(nop_txn_flags
, 0);
7444 if (flags
& ~PERF_PMU_TXN_ADD
)
7447 perf_pmu_enable(pmu
);
7451 static void perf_pmu_cancel_txn(struct pmu
*pmu
)
7453 unsigned int flags
= __this_cpu_read(nop_txn_flags
);
7455 __this_cpu_write(nop_txn_flags
, 0);
7457 if (flags
& ~PERF_PMU_TXN_ADD
)
7460 perf_pmu_enable(pmu
);
7463 static int perf_event_idx_default(struct perf_event
*event
)
7469 * Ensures all contexts with the same task_ctx_nr have the same
7470 * pmu_cpu_context too.
7472 static struct perf_cpu_context __percpu
*find_pmu_context(int ctxn
)
7479 list_for_each_entry(pmu
, &pmus
, entry
) {
7480 if (pmu
->task_ctx_nr
== ctxn
)
7481 return pmu
->pmu_cpu_context
;
7487 static void update_pmu_context(struct pmu
*pmu
, struct pmu
*old_pmu
)
7491 for_each_possible_cpu(cpu
) {
7492 struct perf_cpu_context
*cpuctx
;
7494 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
7496 if (cpuctx
->unique_pmu
== old_pmu
)
7497 cpuctx
->unique_pmu
= pmu
;
7501 static void free_pmu_context(struct pmu
*pmu
)
7505 mutex_lock(&pmus_lock
);
7507 * Like a real lame refcount.
7509 list_for_each_entry(i
, &pmus
, entry
) {
7510 if (i
->pmu_cpu_context
== pmu
->pmu_cpu_context
) {
7511 update_pmu_context(i
, pmu
);
7516 free_percpu(pmu
->pmu_cpu_context
);
7518 mutex_unlock(&pmus_lock
);
7520 static struct idr pmu_idr
;
7523 type_show(struct device
*dev
, struct device_attribute
*attr
, char *page
)
7525 struct pmu
*pmu
= dev_get_drvdata(dev
);
7527 return snprintf(page
, PAGE_SIZE
-1, "%d\n", pmu
->type
);
7529 static DEVICE_ATTR_RO(type
);
7532 perf_event_mux_interval_ms_show(struct device
*dev
,
7533 struct device_attribute
*attr
,
7536 struct pmu
*pmu
= dev_get_drvdata(dev
);
7538 return snprintf(page
, PAGE_SIZE
-1, "%d\n", pmu
->hrtimer_interval_ms
);
7541 static DEFINE_MUTEX(mux_interval_mutex
);
7544 perf_event_mux_interval_ms_store(struct device
*dev
,
7545 struct device_attribute
*attr
,
7546 const char *buf
, size_t count
)
7548 struct pmu
*pmu
= dev_get_drvdata(dev
);
7549 int timer
, cpu
, ret
;
7551 ret
= kstrtoint(buf
, 0, &timer
);
7558 /* same value, noting to do */
7559 if (timer
== pmu
->hrtimer_interval_ms
)
7562 mutex_lock(&mux_interval_mutex
);
7563 pmu
->hrtimer_interval_ms
= timer
;
7565 /* update all cpuctx for this PMU */
7567 for_each_online_cpu(cpu
) {
7568 struct perf_cpu_context
*cpuctx
;
7569 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
7570 cpuctx
->hrtimer_interval
= ns_to_ktime(NSEC_PER_MSEC
* timer
);
7572 cpu_function_call(cpu
,
7573 (remote_function_f
)perf_mux_hrtimer_restart
, cpuctx
);
7576 mutex_unlock(&mux_interval_mutex
);
7580 static DEVICE_ATTR_RW(perf_event_mux_interval_ms
);
7582 static struct attribute
*pmu_dev_attrs
[] = {
7583 &dev_attr_type
.attr
,
7584 &dev_attr_perf_event_mux_interval_ms
.attr
,
7587 ATTRIBUTE_GROUPS(pmu_dev
);
7589 static int pmu_bus_running
;
7590 static struct bus_type pmu_bus
= {
7591 .name
= "event_source",
7592 .dev_groups
= pmu_dev_groups
,
7595 static void pmu_dev_release(struct device
*dev
)
7600 static int pmu_dev_alloc(struct pmu
*pmu
)
7604 pmu
->dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
7608 pmu
->dev
->groups
= pmu
->attr_groups
;
7609 device_initialize(pmu
->dev
);
7610 ret
= dev_set_name(pmu
->dev
, "%s", pmu
->name
);
7614 dev_set_drvdata(pmu
->dev
, pmu
);
7615 pmu
->dev
->bus
= &pmu_bus
;
7616 pmu
->dev
->release
= pmu_dev_release
;
7617 ret
= device_add(pmu
->dev
);
7625 put_device(pmu
->dev
);
7629 static struct lock_class_key cpuctx_mutex
;
7630 static struct lock_class_key cpuctx_lock
;
7632 int perf_pmu_register(struct pmu
*pmu
, const char *name
, int type
)
7636 mutex_lock(&pmus_lock
);
7638 pmu
->pmu_disable_count
= alloc_percpu(int);
7639 if (!pmu
->pmu_disable_count
)
7648 type
= idr_alloc(&pmu_idr
, pmu
, PERF_TYPE_MAX
, 0, GFP_KERNEL
);
7656 if (pmu_bus_running
) {
7657 ret
= pmu_dev_alloc(pmu
);
7663 pmu
->pmu_cpu_context
= find_pmu_context(pmu
->task_ctx_nr
);
7664 if (pmu
->pmu_cpu_context
)
7665 goto got_cpu_context
;
7668 pmu
->pmu_cpu_context
= alloc_percpu(struct perf_cpu_context
);
7669 if (!pmu
->pmu_cpu_context
)
7672 for_each_possible_cpu(cpu
) {
7673 struct perf_cpu_context
*cpuctx
;
7675 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
7676 __perf_event_init_context(&cpuctx
->ctx
);
7677 lockdep_set_class(&cpuctx
->ctx
.mutex
, &cpuctx_mutex
);
7678 lockdep_set_class(&cpuctx
->ctx
.lock
, &cpuctx_lock
);
7679 cpuctx
->ctx
.pmu
= pmu
;
7681 __perf_mux_hrtimer_init(cpuctx
, cpu
);
7683 cpuctx
->unique_pmu
= pmu
;
7687 if (!pmu
->start_txn
) {
7688 if (pmu
->pmu_enable
) {
7690 * If we have pmu_enable/pmu_disable calls, install
7691 * transaction stubs that use that to try and batch
7692 * hardware accesses.
7694 pmu
->start_txn
= perf_pmu_start_txn
;
7695 pmu
->commit_txn
= perf_pmu_commit_txn
;
7696 pmu
->cancel_txn
= perf_pmu_cancel_txn
;
7698 pmu
->start_txn
= perf_pmu_nop_txn
;
7699 pmu
->commit_txn
= perf_pmu_nop_int
;
7700 pmu
->cancel_txn
= perf_pmu_nop_void
;
7704 if (!pmu
->pmu_enable
) {
7705 pmu
->pmu_enable
= perf_pmu_nop_void
;
7706 pmu
->pmu_disable
= perf_pmu_nop_void
;
7709 if (!pmu
->event_idx
)
7710 pmu
->event_idx
= perf_event_idx_default
;
7712 list_add_rcu(&pmu
->entry
, &pmus
);
7713 atomic_set(&pmu
->exclusive_cnt
, 0);
7716 mutex_unlock(&pmus_lock
);
7721 device_del(pmu
->dev
);
7722 put_device(pmu
->dev
);
7725 if (pmu
->type
>= PERF_TYPE_MAX
)
7726 idr_remove(&pmu_idr
, pmu
->type
);
7729 free_percpu(pmu
->pmu_disable_count
);
7732 EXPORT_SYMBOL_GPL(perf_pmu_register
);
7734 void perf_pmu_unregister(struct pmu
*pmu
)
7736 mutex_lock(&pmus_lock
);
7737 list_del_rcu(&pmu
->entry
);
7738 mutex_unlock(&pmus_lock
);
7741 * We dereference the pmu list under both SRCU and regular RCU, so
7742 * synchronize against both of those.
7744 synchronize_srcu(&pmus_srcu
);
7747 free_percpu(pmu
->pmu_disable_count
);
7748 if (pmu
->type
>= PERF_TYPE_MAX
)
7749 idr_remove(&pmu_idr
, pmu
->type
);
7750 device_del(pmu
->dev
);
7751 put_device(pmu
->dev
);
7752 free_pmu_context(pmu
);
7754 EXPORT_SYMBOL_GPL(perf_pmu_unregister
);
7756 static int perf_try_init_event(struct pmu
*pmu
, struct perf_event
*event
)
7758 struct perf_event_context
*ctx
= NULL
;
7761 if (!try_module_get(pmu
->module
))
7764 if (event
->group_leader
!= event
) {
7766 * This ctx->mutex can nest when we're called through
7767 * inheritance. See the perf_event_ctx_lock_nested() comment.
7769 ctx
= perf_event_ctx_lock_nested(event
->group_leader
,
7770 SINGLE_DEPTH_NESTING
);
7775 ret
= pmu
->event_init(event
);
7778 perf_event_ctx_unlock(event
->group_leader
, ctx
);
7781 module_put(pmu
->module
);
7786 static struct pmu
*perf_init_event(struct perf_event
*event
)
7788 struct pmu
*pmu
= NULL
;
7792 idx
= srcu_read_lock(&pmus_srcu
);
7795 pmu
= idr_find(&pmu_idr
, event
->attr
.type
);
7798 ret
= perf_try_init_event(pmu
, event
);
7804 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
7805 ret
= perf_try_init_event(pmu
, event
);
7809 if (ret
!= -ENOENT
) {
7814 pmu
= ERR_PTR(-ENOENT
);
7816 srcu_read_unlock(&pmus_srcu
, idx
);
7821 static void account_event_cpu(struct perf_event
*event
, int cpu
)
7826 if (is_cgroup_event(event
))
7827 atomic_inc(&per_cpu(perf_cgroup_events
, cpu
));
7830 /* Freq events need the tick to stay alive (see perf_event_task_tick). */
7831 static void account_freq_event_nohz(void)
7833 #ifdef CONFIG_NO_HZ_FULL
7834 /* Lock so we don't race with concurrent unaccount */
7835 spin_lock(&nr_freq_lock
);
7836 if (atomic_inc_return(&nr_freq_events
) == 1)
7837 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS
);
7838 spin_unlock(&nr_freq_lock
);
7842 static void account_freq_event(void)
7844 if (tick_nohz_full_enabled())
7845 account_freq_event_nohz();
7847 atomic_inc(&nr_freq_events
);
7851 static void account_event(struct perf_event
*event
)
7858 if (event
->attach_state
& PERF_ATTACH_TASK
)
7860 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
7861 atomic_inc(&nr_mmap_events
);
7862 if (event
->attr
.comm
)
7863 atomic_inc(&nr_comm_events
);
7864 if (event
->attr
.task
)
7865 atomic_inc(&nr_task_events
);
7866 if (event
->attr
.freq
)
7867 account_freq_event();
7868 if (event
->attr
.context_switch
) {
7869 atomic_inc(&nr_switch_events
);
7872 if (has_branch_stack(event
))
7874 if (is_cgroup_event(event
))
7878 if (atomic_inc_not_zero(&perf_sched_count
))
7881 mutex_lock(&perf_sched_mutex
);
7882 if (!atomic_read(&perf_sched_count
)) {
7883 static_branch_enable(&perf_sched_events
);
7885 * Guarantee that all CPUs observe they key change and
7886 * call the perf scheduling hooks before proceeding to
7887 * install events that need them.
7889 synchronize_sched();
7892 * Now that we have waited for the sync_sched(), allow further
7893 * increments to by-pass the mutex.
7895 atomic_inc(&perf_sched_count
);
7896 mutex_unlock(&perf_sched_mutex
);
7900 account_event_cpu(event
, event
->cpu
);
7904 * Allocate and initialize a event structure
7906 static struct perf_event
*
7907 perf_event_alloc(struct perf_event_attr
*attr
, int cpu
,
7908 struct task_struct
*task
,
7909 struct perf_event
*group_leader
,
7910 struct perf_event
*parent_event
,
7911 perf_overflow_handler_t overflow_handler
,
7912 void *context
, int cgroup_fd
)
7915 struct perf_event
*event
;
7916 struct hw_perf_event
*hwc
;
7919 if ((unsigned)cpu
>= nr_cpu_ids
) {
7920 if (!task
|| cpu
!= -1)
7921 return ERR_PTR(-EINVAL
);
7924 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
7926 return ERR_PTR(-ENOMEM
);
7929 * Single events are their own group leaders, with an
7930 * empty sibling list:
7933 group_leader
= event
;
7935 mutex_init(&event
->child_mutex
);
7936 INIT_LIST_HEAD(&event
->child_list
);
7938 INIT_LIST_HEAD(&event
->group_entry
);
7939 INIT_LIST_HEAD(&event
->event_entry
);
7940 INIT_LIST_HEAD(&event
->sibling_list
);
7941 INIT_LIST_HEAD(&event
->rb_entry
);
7942 INIT_LIST_HEAD(&event
->active_entry
);
7943 INIT_HLIST_NODE(&event
->hlist_entry
);
7946 init_waitqueue_head(&event
->waitq
);
7947 init_irq_work(&event
->pending
, perf_pending_event
);
7949 mutex_init(&event
->mmap_mutex
);
7951 atomic_long_set(&event
->refcount
, 1);
7953 event
->attr
= *attr
;
7954 event
->group_leader
= group_leader
;
7958 event
->parent
= parent_event
;
7960 event
->ns
= get_pid_ns(task_active_pid_ns(current
));
7961 event
->id
= atomic64_inc_return(&perf_event_id
);
7963 event
->state
= PERF_EVENT_STATE_INACTIVE
;
7966 event
->attach_state
= PERF_ATTACH_TASK
;
7968 * XXX pmu::event_init needs to know what task to account to
7969 * and we cannot use the ctx information because we need the
7970 * pmu before we get a ctx.
7972 event
->hw
.target
= task
;
7975 event
->clock
= &local_clock
;
7977 event
->clock
= parent_event
->clock
;
7979 if (!overflow_handler
&& parent_event
) {
7980 overflow_handler
= parent_event
->overflow_handler
;
7981 context
= parent_event
->overflow_handler_context
;
7984 event
->overflow_handler
= overflow_handler
;
7985 event
->overflow_handler_context
= context
;
7987 perf_event__state_init(event
);
7992 hwc
->sample_period
= attr
->sample_period
;
7993 if (attr
->freq
&& attr
->sample_freq
)
7994 hwc
->sample_period
= 1;
7995 hwc
->last_period
= hwc
->sample_period
;
7997 local64_set(&hwc
->period_left
, hwc
->sample_period
);
8000 * we currently do not support PERF_FORMAT_GROUP on inherited events
8002 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
8005 if (!has_branch_stack(event
))
8006 event
->attr
.branch_sample_type
= 0;
8008 if (cgroup_fd
!= -1) {
8009 err
= perf_cgroup_connect(cgroup_fd
, event
, attr
, group_leader
);
8014 pmu
= perf_init_event(event
);
8017 else if (IS_ERR(pmu
)) {
8022 err
= exclusive_event_init(event
);
8026 if (!event
->parent
) {
8027 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
8028 err
= get_callchain_buffers();
8034 /* symmetric to unaccount_event() in _free_event() */
8035 account_event(event
);
8040 exclusive_event_destroy(event
);
8044 event
->destroy(event
);
8045 module_put(pmu
->module
);
8047 if (is_cgroup_event(event
))
8048 perf_detach_cgroup(event
);
8050 put_pid_ns(event
->ns
);
8053 return ERR_PTR(err
);
8056 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
8057 struct perf_event_attr
*attr
)
8062 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
8066 * zero the full structure, so that a short copy will be nice.
8068 memset(attr
, 0, sizeof(*attr
));
8070 ret
= get_user(size
, &uattr
->size
);
8074 if (size
> PAGE_SIZE
) /* silly large */
8077 if (!size
) /* abi compat */
8078 size
= PERF_ATTR_SIZE_VER0
;
8080 if (size
< PERF_ATTR_SIZE_VER0
)
8084 * If we're handed a bigger struct than we know of,
8085 * ensure all the unknown bits are 0 - i.e. new
8086 * user-space does not rely on any kernel feature
8087 * extensions we dont know about yet.
8089 if (size
> sizeof(*attr
)) {
8090 unsigned char __user
*addr
;
8091 unsigned char __user
*end
;
8094 addr
= (void __user
*)uattr
+ sizeof(*attr
);
8095 end
= (void __user
*)uattr
+ size
;
8097 for (; addr
< end
; addr
++) {
8098 ret
= get_user(val
, addr
);
8104 size
= sizeof(*attr
);
8107 ret
= copy_from_user(attr
, uattr
, size
);
8111 if (attr
->__reserved_1
)
8114 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
8117 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
8120 if (attr
->sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
8121 u64 mask
= attr
->branch_sample_type
;
8123 /* only using defined bits */
8124 if (mask
& ~(PERF_SAMPLE_BRANCH_MAX
-1))
8127 /* at least one branch bit must be set */
8128 if (!(mask
& ~PERF_SAMPLE_BRANCH_PLM_ALL
))
8131 /* propagate priv level, when not set for branch */
8132 if (!(mask
& PERF_SAMPLE_BRANCH_PLM_ALL
)) {
8134 /* exclude_kernel checked on syscall entry */
8135 if (!attr
->exclude_kernel
)
8136 mask
|= PERF_SAMPLE_BRANCH_KERNEL
;
8138 if (!attr
->exclude_user
)
8139 mask
|= PERF_SAMPLE_BRANCH_USER
;
8141 if (!attr
->exclude_hv
)
8142 mask
|= PERF_SAMPLE_BRANCH_HV
;
8144 * adjust user setting (for HW filter setup)
8146 attr
->branch_sample_type
= mask
;
8148 /* privileged levels capture (kernel, hv): check permissions */
8149 if ((mask
& PERF_SAMPLE_BRANCH_PERM_PLM
)
8150 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
8154 if (attr
->sample_type
& PERF_SAMPLE_REGS_USER
) {
8155 ret
= perf_reg_validate(attr
->sample_regs_user
);
8160 if (attr
->sample_type
& PERF_SAMPLE_STACK_USER
) {
8161 if (!arch_perf_have_user_stack_dump())
8165 * We have __u32 type for the size, but so far
8166 * we can only use __u16 as maximum due to the
8167 * __u16 sample size limit.
8169 if (attr
->sample_stack_user
>= USHRT_MAX
)
8171 else if (!IS_ALIGNED(attr
->sample_stack_user
, sizeof(u64
)))
8175 if (attr
->sample_type
& PERF_SAMPLE_REGS_INTR
)
8176 ret
= perf_reg_validate(attr
->sample_regs_intr
);
8181 put_user(sizeof(*attr
), &uattr
->size
);
8187 perf_event_set_output(struct perf_event
*event
, struct perf_event
*output_event
)
8189 struct ring_buffer
*rb
= NULL
;
8195 /* don't allow circular references */
8196 if (event
== output_event
)
8200 * Don't allow cross-cpu buffers
8202 if (output_event
->cpu
!= event
->cpu
)
8206 * If its not a per-cpu rb, it must be the same task.
8208 if (output_event
->cpu
== -1 && output_event
->ctx
!= event
->ctx
)
8212 * Mixing clocks in the same buffer is trouble you don't need.
8214 if (output_event
->clock
!= event
->clock
)
8218 * If both events generate aux data, they must be on the same PMU
8220 if (has_aux(event
) && has_aux(output_event
) &&
8221 event
->pmu
!= output_event
->pmu
)
8225 mutex_lock(&event
->mmap_mutex
);
8226 /* Can't redirect output if we've got an active mmap() */
8227 if (atomic_read(&event
->mmap_count
))
8231 /* get the rb we want to redirect to */
8232 rb
= ring_buffer_get(output_event
);
8237 ring_buffer_attach(event
, rb
);
8241 mutex_unlock(&event
->mmap_mutex
);
8247 static void mutex_lock_double(struct mutex
*a
, struct mutex
*b
)
8253 mutex_lock_nested(b
, SINGLE_DEPTH_NESTING
);
8256 static int perf_event_set_clock(struct perf_event
*event
, clockid_t clk_id
)
8258 bool nmi_safe
= false;
8261 case CLOCK_MONOTONIC
:
8262 event
->clock
= &ktime_get_mono_fast_ns
;
8266 case CLOCK_MONOTONIC_RAW
:
8267 event
->clock
= &ktime_get_raw_fast_ns
;
8271 case CLOCK_REALTIME
:
8272 event
->clock
= &ktime_get_real_ns
;
8275 case CLOCK_BOOTTIME
:
8276 event
->clock
= &ktime_get_boot_ns
;
8280 event
->clock
= &ktime_get_tai_ns
;
8287 if (!nmi_safe
&& !(event
->pmu
->capabilities
& PERF_PMU_CAP_NO_NMI
))
8294 * sys_perf_event_open - open a performance event, associate it to a task/cpu
8296 * @attr_uptr: event_id type attributes for monitoring/sampling
8299 * @group_fd: group leader event fd
8301 SYSCALL_DEFINE5(perf_event_open
,
8302 struct perf_event_attr __user
*, attr_uptr
,
8303 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
8305 struct perf_event
*group_leader
= NULL
, *output_event
= NULL
;
8306 struct perf_event
*event
, *sibling
;
8307 struct perf_event_attr attr
;
8308 struct perf_event_context
*ctx
, *uninitialized_var(gctx
);
8309 struct file
*event_file
= NULL
;
8310 struct fd group
= {NULL
, 0};
8311 struct task_struct
*task
= NULL
;
8316 int f_flags
= O_RDWR
;
8319 /* for future expandability... */
8320 if (flags
& ~PERF_FLAG_ALL
)
8323 err
= perf_copy_attr(attr_uptr
, &attr
);
8327 if (!attr
.exclude_kernel
) {
8328 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
8333 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
8336 if (attr
.sample_period
& (1ULL << 63))
8341 * In cgroup mode, the pid argument is used to pass the fd
8342 * opened to the cgroup directory in cgroupfs. The cpu argument
8343 * designates the cpu on which to monitor threads from that
8346 if ((flags
& PERF_FLAG_PID_CGROUP
) && (pid
== -1 || cpu
== -1))
8349 if (flags
& PERF_FLAG_FD_CLOEXEC
)
8350 f_flags
|= O_CLOEXEC
;
8352 event_fd
= get_unused_fd_flags(f_flags
);
8356 if (group_fd
!= -1) {
8357 err
= perf_fget_light(group_fd
, &group
);
8360 group_leader
= group
.file
->private_data
;
8361 if (flags
& PERF_FLAG_FD_OUTPUT
)
8362 output_event
= group_leader
;
8363 if (flags
& PERF_FLAG_FD_NO_GROUP
)
8364 group_leader
= NULL
;
8367 if (pid
!= -1 && !(flags
& PERF_FLAG_PID_CGROUP
)) {
8368 task
= find_lively_task_by_vpid(pid
);
8370 err
= PTR_ERR(task
);
8375 if (task
&& group_leader
&&
8376 group_leader
->attr
.inherit
!= attr
.inherit
) {
8383 if (flags
& PERF_FLAG_PID_CGROUP
)
8386 event
= perf_event_alloc(&attr
, cpu
, task
, group_leader
, NULL
,
8387 NULL
, NULL
, cgroup_fd
);
8388 if (IS_ERR(event
)) {
8389 err
= PTR_ERR(event
);
8393 if (is_sampling_event(event
)) {
8394 if (event
->pmu
->capabilities
& PERF_PMU_CAP_NO_INTERRUPT
) {
8401 * Special case software events and allow them to be part of
8402 * any hardware group.
8406 if (attr
.use_clockid
) {
8407 err
= perf_event_set_clock(event
, attr
.clockid
);
8413 (is_software_event(event
) != is_software_event(group_leader
))) {
8414 if (is_software_event(event
)) {
8416 * If event and group_leader are not both a software
8417 * event, and event is, then group leader is not.
8419 * Allow the addition of software events to !software
8420 * groups, this is safe because software events never
8423 pmu
= group_leader
->pmu
;
8424 } else if (is_software_event(group_leader
) &&
8425 (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
)) {
8427 * In case the group is a pure software group, and we
8428 * try to add a hardware event, move the whole group to
8429 * the hardware context.
8436 * Get the target context (task or percpu):
8438 ctx
= find_get_context(pmu
, task
, event
);
8444 if ((pmu
->capabilities
& PERF_PMU_CAP_EXCLUSIVE
) && group_leader
) {
8450 put_task_struct(task
);
8455 * Look up the group leader (we will attach this event to it):
8461 * Do not allow a recursive hierarchy (this new sibling
8462 * becoming part of another group-sibling):
8464 if (group_leader
->group_leader
!= group_leader
)
8467 /* All events in a group should have the same clock */
8468 if (group_leader
->clock
!= event
->clock
)
8472 * Do not allow to attach to a group in a different
8473 * task or CPU context:
8477 * Make sure we're both on the same task, or both
8480 if (group_leader
->ctx
->task
!= ctx
->task
)
8484 * Make sure we're both events for the same CPU;
8485 * grouping events for different CPUs is broken; since
8486 * you can never concurrently schedule them anyhow.
8488 if (group_leader
->cpu
!= event
->cpu
)
8491 if (group_leader
->ctx
!= ctx
)
8496 * Only a group leader can be exclusive or pinned
8498 if (attr
.exclusive
|| attr
.pinned
)
8503 err
= perf_event_set_output(event
, output_event
);
8508 event_file
= anon_inode_getfile("[perf_event]", &perf_fops
, event
,
8510 if (IS_ERR(event_file
)) {
8511 err
= PTR_ERR(event_file
);
8516 gctx
= group_leader
->ctx
;
8517 mutex_lock_double(&gctx
->mutex
, &ctx
->mutex
);
8518 if (gctx
->task
== TASK_TOMBSTONE
) {
8523 mutex_lock(&ctx
->mutex
);
8526 if (ctx
->task
== TASK_TOMBSTONE
) {
8531 if (!perf_event_validate_size(event
)) {
8537 * Must be under the same ctx::mutex as perf_install_in_context(),
8538 * because we need to serialize with concurrent event creation.
8540 if (!exclusive_event_installable(event
, ctx
)) {
8541 /* exclusive and group stuff are assumed mutually exclusive */
8542 WARN_ON_ONCE(move_group
);
8548 WARN_ON_ONCE(ctx
->parent_ctx
);
8552 * See perf_event_ctx_lock() for comments on the details
8553 * of swizzling perf_event::ctx.
8555 perf_remove_from_context(group_leader
, 0);
8557 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
8559 perf_remove_from_context(sibling
, 0);
8564 * Wait for everybody to stop referencing the events through
8565 * the old lists, before installing it on new lists.
8570 * Install the group siblings before the group leader.
8572 * Because a group leader will try and install the entire group
8573 * (through the sibling list, which is still in-tact), we can
8574 * end up with siblings installed in the wrong context.
8576 * By installing siblings first we NO-OP because they're not
8577 * reachable through the group lists.
8579 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
8581 perf_event__state_init(sibling
);
8582 perf_install_in_context(ctx
, sibling
, sibling
->cpu
);
8587 * Removing from the context ends up with disabled
8588 * event. What we want here is event in the initial
8589 * startup state, ready to be add into new context.
8591 perf_event__state_init(group_leader
);
8592 perf_install_in_context(ctx
, group_leader
, group_leader
->cpu
);
8596 * Now that all events are installed in @ctx, nothing
8597 * references @gctx anymore, so drop the last reference we have
8604 * Precalculate sample_data sizes; do while holding ctx::mutex such
8605 * that we're serialized against further additions and before
8606 * perf_install_in_context() which is the point the event is active and
8607 * can use these values.
8609 perf_event__header_size(event
);
8610 perf_event__id_header_size(event
);
8612 event
->owner
= current
;
8614 perf_install_in_context(ctx
, event
, event
->cpu
);
8615 perf_unpin_context(ctx
);
8618 mutex_unlock(&gctx
->mutex
);
8619 mutex_unlock(&ctx
->mutex
);
8623 mutex_lock(¤t
->perf_event_mutex
);
8624 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
8625 mutex_unlock(¤t
->perf_event_mutex
);
8628 * Drop the reference on the group_event after placing the
8629 * new event on the sibling_list. This ensures destruction
8630 * of the group leader will find the pointer to itself in
8631 * perf_group_detach().
8634 fd_install(event_fd
, event_file
);
8639 mutex_unlock(&gctx
->mutex
);
8640 mutex_unlock(&ctx
->mutex
);
8644 perf_unpin_context(ctx
);
8648 * If event_file is set, the fput() above will have called ->release()
8649 * and that will take care of freeing the event.
8657 put_task_struct(task
);
8661 put_unused_fd(event_fd
);
8666 * perf_event_create_kernel_counter
8668 * @attr: attributes of the counter to create
8669 * @cpu: cpu in which the counter is bound
8670 * @task: task to profile (NULL for percpu)
8673 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
8674 struct task_struct
*task
,
8675 perf_overflow_handler_t overflow_handler
,
8678 struct perf_event_context
*ctx
;
8679 struct perf_event
*event
;
8683 * Get the target context (task or percpu):
8686 event
= perf_event_alloc(attr
, cpu
, task
, NULL
, NULL
,
8687 overflow_handler
, context
, -1);
8688 if (IS_ERR(event
)) {
8689 err
= PTR_ERR(event
);
8693 /* Mark owner so we could distinguish it from user events. */
8694 event
->owner
= TASK_TOMBSTONE
;
8696 ctx
= find_get_context(event
->pmu
, task
, event
);
8702 WARN_ON_ONCE(ctx
->parent_ctx
);
8703 mutex_lock(&ctx
->mutex
);
8704 if (ctx
->task
== TASK_TOMBSTONE
) {
8709 if (!exclusive_event_installable(event
, ctx
)) {
8714 perf_install_in_context(ctx
, event
, cpu
);
8715 perf_unpin_context(ctx
);
8716 mutex_unlock(&ctx
->mutex
);
8721 mutex_unlock(&ctx
->mutex
);
8722 perf_unpin_context(ctx
);
8727 return ERR_PTR(err
);
8729 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
8731 void perf_pmu_migrate_context(struct pmu
*pmu
, int src_cpu
, int dst_cpu
)
8733 struct perf_event_context
*src_ctx
;
8734 struct perf_event_context
*dst_ctx
;
8735 struct perf_event
*event
, *tmp
;
8738 src_ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, src_cpu
)->ctx
;
8739 dst_ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, dst_cpu
)->ctx
;
8742 * See perf_event_ctx_lock() for comments on the details
8743 * of swizzling perf_event::ctx.
8745 mutex_lock_double(&src_ctx
->mutex
, &dst_ctx
->mutex
);
8746 list_for_each_entry_safe(event
, tmp
, &src_ctx
->event_list
,
8748 perf_remove_from_context(event
, 0);
8749 unaccount_event_cpu(event
, src_cpu
);
8751 list_add(&event
->migrate_entry
, &events
);
8755 * Wait for the events to quiesce before re-instating them.
8760 * Re-instate events in 2 passes.
8762 * Skip over group leaders and only install siblings on this first
8763 * pass, siblings will not get enabled without a leader, however a
8764 * leader will enable its siblings, even if those are still on the old
8767 list_for_each_entry_safe(event
, tmp
, &events
, migrate_entry
) {
8768 if (event
->group_leader
== event
)
8771 list_del(&event
->migrate_entry
);
8772 if (event
->state
>= PERF_EVENT_STATE_OFF
)
8773 event
->state
= PERF_EVENT_STATE_INACTIVE
;
8774 account_event_cpu(event
, dst_cpu
);
8775 perf_install_in_context(dst_ctx
, event
, dst_cpu
);
8780 * Once all the siblings are setup properly, install the group leaders
8783 list_for_each_entry_safe(event
, tmp
, &events
, migrate_entry
) {
8784 list_del(&event
->migrate_entry
);
8785 if (event
->state
>= PERF_EVENT_STATE_OFF
)
8786 event
->state
= PERF_EVENT_STATE_INACTIVE
;
8787 account_event_cpu(event
, dst_cpu
);
8788 perf_install_in_context(dst_ctx
, event
, dst_cpu
);
8791 mutex_unlock(&dst_ctx
->mutex
);
8792 mutex_unlock(&src_ctx
->mutex
);
8794 EXPORT_SYMBOL_GPL(perf_pmu_migrate_context
);
8796 static void sync_child_event(struct perf_event
*child_event
,
8797 struct task_struct
*child
)
8799 struct perf_event
*parent_event
= child_event
->parent
;
8802 if (child_event
->attr
.inherit_stat
)
8803 perf_event_read_event(child_event
, child
);
8805 child_val
= perf_event_count(child_event
);
8808 * Add back the child's count to the parent's count:
8810 atomic64_add(child_val
, &parent_event
->child_count
);
8811 atomic64_add(child_event
->total_time_enabled
,
8812 &parent_event
->child_total_time_enabled
);
8813 atomic64_add(child_event
->total_time_running
,
8814 &parent_event
->child_total_time_running
);
8818 perf_event_exit_event(struct perf_event
*child_event
,
8819 struct perf_event_context
*child_ctx
,
8820 struct task_struct
*child
)
8822 struct perf_event
*parent_event
= child_event
->parent
;
8825 * Do not destroy the 'original' grouping; because of the context
8826 * switch optimization the original events could've ended up in a
8827 * random child task.
8829 * If we were to destroy the original group, all group related
8830 * operations would cease to function properly after this random
8833 * Do destroy all inherited groups, we don't care about those
8834 * and being thorough is better.
8836 raw_spin_lock_irq(&child_ctx
->lock
);
8837 WARN_ON_ONCE(child_ctx
->is_active
);
8840 perf_group_detach(child_event
);
8841 list_del_event(child_event
, child_ctx
);
8842 child_event
->state
= PERF_EVENT_STATE_EXIT
; /* is_event_hup() */
8843 raw_spin_unlock_irq(&child_ctx
->lock
);
8846 * Parent events are governed by their filedesc, retain them.
8848 if (!parent_event
) {
8849 perf_event_wakeup(child_event
);
8853 * Child events can be cleaned up.
8856 sync_child_event(child_event
, child
);
8859 * Remove this event from the parent's list
8861 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
8862 mutex_lock(&parent_event
->child_mutex
);
8863 list_del_init(&child_event
->child_list
);
8864 mutex_unlock(&parent_event
->child_mutex
);
8867 * Kick perf_poll() for is_event_hup().
8869 perf_event_wakeup(parent_event
);
8870 free_event(child_event
);
8871 put_event(parent_event
);
8874 static void perf_event_exit_task_context(struct task_struct
*child
, int ctxn
)
8876 struct perf_event_context
*child_ctx
, *clone_ctx
= NULL
;
8877 struct perf_event
*child_event
, *next
;
8879 WARN_ON_ONCE(child
!= current
);
8881 child_ctx
= perf_pin_task_context(child
, ctxn
);
8886 * In order to reduce the amount of tricky in ctx tear-down, we hold
8887 * ctx::mutex over the entire thing. This serializes against almost
8888 * everything that wants to access the ctx.
8890 * The exception is sys_perf_event_open() /
8891 * perf_event_create_kernel_count() which does find_get_context()
8892 * without ctx::mutex (it cannot because of the move_group double mutex
8893 * lock thing). See the comments in perf_install_in_context().
8895 mutex_lock(&child_ctx
->mutex
);
8898 * In a single ctx::lock section, de-schedule the events and detach the
8899 * context from the task such that we cannot ever get it scheduled back
8902 raw_spin_lock_irq(&child_ctx
->lock
);
8903 task_ctx_sched_out(__get_cpu_context(child_ctx
), child_ctx
);
8906 * Now that the context is inactive, destroy the task <-> ctx relation
8907 * and mark the context dead.
8909 RCU_INIT_POINTER(child
->perf_event_ctxp
[ctxn
], NULL
);
8910 put_ctx(child_ctx
); /* cannot be last */
8911 WRITE_ONCE(child_ctx
->task
, TASK_TOMBSTONE
);
8912 put_task_struct(current
); /* cannot be last */
8914 clone_ctx
= unclone_ctx(child_ctx
);
8915 raw_spin_unlock_irq(&child_ctx
->lock
);
8921 * Report the task dead after unscheduling the events so that we
8922 * won't get any samples after PERF_RECORD_EXIT. We can however still
8923 * get a few PERF_RECORD_READ events.
8925 perf_event_task(child
, child_ctx
, 0);
8927 list_for_each_entry_safe(child_event
, next
, &child_ctx
->event_list
, event_entry
)
8928 perf_event_exit_event(child_event
, child_ctx
, child
);
8930 mutex_unlock(&child_ctx
->mutex
);
8936 * When a child task exits, feed back event values to parent events.
8938 void perf_event_exit_task(struct task_struct
*child
)
8940 struct perf_event
*event
, *tmp
;
8943 mutex_lock(&child
->perf_event_mutex
);
8944 list_for_each_entry_safe(event
, tmp
, &child
->perf_event_list
,
8946 list_del_init(&event
->owner_entry
);
8949 * Ensure the list deletion is visible before we clear
8950 * the owner, closes a race against perf_release() where
8951 * we need to serialize on the owner->perf_event_mutex.
8953 smp_store_release(&event
->owner
, NULL
);
8955 mutex_unlock(&child
->perf_event_mutex
);
8957 for_each_task_context_nr(ctxn
)
8958 perf_event_exit_task_context(child
, ctxn
);
8961 * The perf_event_exit_task_context calls perf_event_task
8962 * with child's task_ctx, which generates EXIT events for
8963 * child contexts and sets child->perf_event_ctxp[] to NULL.
8964 * At this point we need to send EXIT events to cpu contexts.
8966 perf_event_task(child
, NULL
, 0);
8969 static void perf_free_event(struct perf_event
*event
,
8970 struct perf_event_context
*ctx
)
8972 struct perf_event
*parent
= event
->parent
;
8974 if (WARN_ON_ONCE(!parent
))
8977 mutex_lock(&parent
->child_mutex
);
8978 list_del_init(&event
->child_list
);
8979 mutex_unlock(&parent
->child_mutex
);
8983 raw_spin_lock_irq(&ctx
->lock
);
8984 perf_group_detach(event
);
8985 list_del_event(event
, ctx
);
8986 raw_spin_unlock_irq(&ctx
->lock
);
8991 * Free an unexposed, unused context as created by inheritance by
8992 * perf_event_init_task below, used by fork() in case of fail.
8994 * Not all locks are strictly required, but take them anyway to be nice and
8995 * help out with the lockdep assertions.
8997 void perf_event_free_task(struct task_struct
*task
)
8999 struct perf_event_context
*ctx
;
9000 struct perf_event
*event
, *tmp
;
9003 for_each_task_context_nr(ctxn
) {
9004 ctx
= task
->perf_event_ctxp
[ctxn
];
9008 mutex_lock(&ctx
->mutex
);
9010 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
,
9012 perf_free_event(event
, ctx
);
9014 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
,
9016 perf_free_event(event
, ctx
);
9018 if (!list_empty(&ctx
->pinned_groups
) ||
9019 !list_empty(&ctx
->flexible_groups
))
9022 mutex_unlock(&ctx
->mutex
);
9028 void perf_event_delayed_put(struct task_struct
*task
)
9032 for_each_task_context_nr(ctxn
)
9033 WARN_ON_ONCE(task
->perf_event_ctxp
[ctxn
]);
9036 struct file
*perf_event_get(unsigned int fd
)
9040 file
= fget_raw(fd
);
9042 return ERR_PTR(-EBADF
);
9044 if (file
->f_op
!= &perf_fops
) {
9046 return ERR_PTR(-EBADF
);
9052 const struct perf_event_attr
*perf_event_attrs(struct perf_event
*event
)
9055 return ERR_PTR(-EINVAL
);
9057 return &event
->attr
;
9061 * inherit a event from parent task to child task:
9063 static struct perf_event
*
9064 inherit_event(struct perf_event
*parent_event
,
9065 struct task_struct
*parent
,
9066 struct perf_event_context
*parent_ctx
,
9067 struct task_struct
*child
,
9068 struct perf_event
*group_leader
,
9069 struct perf_event_context
*child_ctx
)
9071 enum perf_event_active_state parent_state
= parent_event
->state
;
9072 struct perf_event
*child_event
;
9073 unsigned long flags
;
9076 * Instead of creating recursive hierarchies of events,
9077 * we link inherited events back to the original parent,
9078 * which has a filp for sure, which we use as the reference
9081 if (parent_event
->parent
)
9082 parent_event
= parent_event
->parent
;
9084 child_event
= perf_event_alloc(&parent_event
->attr
,
9087 group_leader
, parent_event
,
9089 if (IS_ERR(child_event
))
9093 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
9094 * must be under the same lock in order to serialize against
9095 * perf_event_release_kernel(), such that either we must observe
9096 * is_orphaned_event() or they will observe us on the child_list.
9098 mutex_lock(&parent_event
->child_mutex
);
9099 if (is_orphaned_event(parent_event
) ||
9100 !atomic_long_inc_not_zero(&parent_event
->refcount
)) {
9101 mutex_unlock(&parent_event
->child_mutex
);
9102 free_event(child_event
);
9109 * Make the child state follow the state of the parent event,
9110 * not its attr.disabled bit. We hold the parent's mutex,
9111 * so we won't race with perf_event_{en, dis}able_family.
9113 if (parent_state
>= PERF_EVENT_STATE_INACTIVE
)
9114 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
9116 child_event
->state
= PERF_EVENT_STATE_OFF
;
9118 if (parent_event
->attr
.freq
) {
9119 u64 sample_period
= parent_event
->hw
.sample_period
;
9120 struct hw_perf_event
*hwc
= &child_event
->hw
;
9122 hwc
->sample_period
= sample_period
;
9123 hwc
->last_period
= sample_period
;
9125 local64_set(&hwc
->period_left
, sample_period
);
9128 child_event
->ctx
= child_ctx
;
9129 child_event
->overflow_handler
= parent_event
->overflow_handler
;
9130 child_event
->overflow_handler_context
9131 = parent_event
->overflow_handler_context
;
9134 * Precalculate sample_data sizes
9136 perf_event__header_size(child_event
);
9137 perf_event__id_header_size(child_event
);
9140 * Link it up in the child's context:
9142 raw_spin_lock_irqsave(&child_ctx
->lock
, flags
);
9143 add_event_to_ctx(child_event
, child_ctx
);
9144 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
9147 * Link this into the parent event's child list
9149 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
9150 mutex_unlock(&parent_event
->child_mutex
);
9155 static int inherit_group(struct perf_event
*parent_event
,
9156 struct task_struct
*parent
,
9157 struct perf_event_context
*parent_ctx
,
9158 struct task_struct
*child
,
9159 struct perf_event_context
*child_ctx
)
9161 struct perf_event
*leader
;
9162 struct perf_event
*sub
;
9163 struct perf_event
*child_ctr
;
9165 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
9166 child
, NULL
, child_ctx
);
9168 return PTR_ERR(leader
);
9169 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
9170 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
9171 child
, leader
, child_ctx
);
9172 if (IS_ERR(child_ctr
))
9173 return PTR_ERR(child_ctr
);
9179 inherit_task_group(struct perf_event
*event
, struct task_struct
*parent
,
9180 struct perf_event_context
*parent_ctx
,
9181 struct task_struct
*child
, int ctxn
,
9185 struct perf_event_context
*child_ctx
;
9187 if (!event
->attr
.inherit
) {
9192 child_ctx
= child
->perf_event_ctxp
[ctxn
];
9195 * This is executed from the parent task context, so
9196 * inherit events that have been marked for cloning.
9197 * First allocate and initialize a context for the
9201 child_ctx
= alloc_perf_context(parent_ctx
->pmu
, child
);
9205 child
->perf_event_ctxp
[ctxn
] = child_ctx
;
9208 ret
= inherit_group(event
, parent
, parent_ctx
,
9218 * Initialize the perf_event context in task_struct
9220 static int perf_event_init_context(struct task_struct
*child
, int ctxn
)
9222 struct perf_event_context
*child_ctx
, *parent_ctx
;
9223 struct perf_event_context
*cloned_ctx
;
9224 struct perf_event
*event
;
9225 struct task_struct
*parent
= current
;
9226 int inherited_all
= 1;
9227 unsigned long flags
;
9230 if (likely(!parent
->perf_event_ctxp
[ctxn
]))
9234 * If the parent's context is a clone, pin it so it won't get
9237 parent_ctx
= perf_pin_task_context(parent
, ctxn
);
9242 * No need to check if parent_ctx != NULL here; since we saw
9243 * it non-NULL earlier, the only reason for it to become NULL
9244 * is if we exit, and since we're currently in the middle of
9245 * a fork we can't be exiting at the same time.
9249 * Lock the parent list. No need to lock the child - not PID
9250 * hashed yet and not running, so nobody can access it.
9252 mutex_lock(&parent_ctx
->mutex
);
9255 * We dont have to disable NMIs - we are only looking at
9256 * the list, not manipulating it:
9258 list_for_each_entry(event
, &parent_ctx
->pinned_groups
, group_entry
) {
9259 ret
= inherit_task_group(event
, parent
, parent_ctx
,
9260 child
, ctxn
, &inherited_all
);
9266 * We can't hold ctx->lock when iterating the ->flexible_group list due
9267 * to allocations, but we need to prevent rotation because
9268 * rotate_ctx() will change the list from interrupt context.
9270 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
9271 parent_ctx
->rotate_disable
= 1;
9272 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
9274 list_for_each_entry(event
, &parent_ctx
->flexible_groups
, group_entry
) {
9275 ret
= inherit_task_group(event
, parent
, parent_ctx
,
9276 child
, ctxn
, &inherited_all
);
9281 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
9282 parent_ctx
->rotate_disable
= 0;
9284 child_ctx
= child
->perf_event_ctxp
[ctxn
];
9286 if (child_ctx
&& inherited_all
) {
9288 * Mark the child context as a clone of the parent
9289 * context, or of whatever the parent is a clone of.
9291 * Note that if the parent is a clone, the holding of
9292 * parent_ctx->lock avoids it from being uncloned.
9294 cloned_ctx
= parent_ctx
->parent_ctx
;
9296 child_ctx
->parent_ctx
= cloned_ctx
;
9297 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
9299 child_ctx
->parent_ctx
= parent_ctx
;
9300 child_ctx
->parent_gen
= parent_ctx
->generation
;
9302 get_ctx(child_ctx
->parent_ctx
);
9305 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
9306 mutex_unlock(&parent_ctx
->mutex
);
9308 perf_unpin_context(parent_ctx
);
9309 put_ctx(parent_ctx
);
9315 * Initialize the perf_event context in task_struct
9317 int perf_event_init_task(struct task_struct
*child
)
9321 memset(child
->perf_event_ctxp
, 0, sizeof(child
->perf_event_ctxp
));
9322 mutex_init(&child
->perf_event_mutex
);
9323 INIT_LIST_HEAD(&child
->perf_event_list
);
9325 for_each_task_context_nr(ctxn
) {
9326 ret
= perf_event_init_context(child
, ctxn
);
9328 perf_event_free_task(child
);
9336 static void __init
perf_event_init_all_cpus(void)
9338 struct swevent_htable
*swhash
;
9341 for_each_possible_cpu(cpu
) {
9342 swhash
= &per_cpu(swevent_htable
, cpu
);
9343 mutex_init(&swhash
->hlist_mutex
);
9344 INIT_LIST_HEAD(&per_cpu(active_ctx_list
, cpu
));
9348 static void perf_event_init_cpu(int cpu
)
9350 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
9352 mutex_lock(&swhash
->hlist_mutex
);
9353 if (swhash
->hlist_refcount
> 0 && !swevent_hlist_deref(swhash
)) {
9354 struct swevent_hlist
*hlist
;
9356 hlist
= kzalloc_node(sizeof(*hlist
), GFP_KERNEL
, cpu_to_node(cpu
));
9358 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
9360 mutex_unlock(&swhash
->hlist_mutex
);
9363 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
9364 static void __perf_event_exit_context(void *__info
)
9366 struct perf_event_context
*ctx
= __info
;
9367 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
9368 struct perf_event
*event
;
9370 raw_spin_lock(&ctx
->lock
);
9371 list_for_each_entry(event
, &ctx
->event_list
, event_entry
)
9372 __perf_remove_from_context(event
, cpuctx
, ctx
, (void *)DETACH_GROUP
);
9373 raw_spin_unlock(&ctx
->lock
);
9376 static void perf_event_exit_cpu_context(int cpu
)
9378 struct perf_event_context
*ctx
;
9382 idx
= srcu_read_lock(&pmus_srcu
);
9383 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
9384 ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
)->ctx
;
9386 mutex_lock(&ctx
->mutex
);
9387 smp_call_function_single(cpu
, __perf_event_exit_context
, ctx
, 1);
9388 mutex_unlock(&ctx
->mutex
);
9390 srcu_read_unlock(&pmus_srcu
, idx
);
9393 static void perf_event_exit_cpu(int cpu
)
9395 perf_event_exit_cpu_context(cpu
);
9398 static inline void perf_event_exit_cpu(int cpu
) { }
9402 perf_reboot(struct notifier_block
*notifier
, unsigned long val
, void *v
)
9406 for_each_online_cpu(cpu
)
9407 perf_event_exit_cpu(cpu
);
9413 * Run the perf reboot notifier at the very last possible moment so that
9414 * the generic watchdog code runs as long as possible.
9416 static struct notifier_block perf_reboot_notifier
= {
9417 .notifier_call
= perf_reboot
,
9418 .priority
= INT_MIN
,
9422 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
9424 unsigned int cpu
= (long)hcpu
;
9426 switch (action
& ~CPU_TASKS_FROZEN
) {
9428 case CPU_UP_PREPARE
:
9429 perf_event_init_cpu(cpu
);
9432 case CPU_DOWN_PREPARE
:
9433 perf_event_exit_cpu(cpu
);
9442 void __init
perf_event_init(void)
9448 perf_event_init_all_cpus();
9449 init_srcu_struct(&pmus_srcu
);
9450 perf_pmu_register(&perf_swevent
, "software", PERF_TYPE_SOFTWARE
);
9451 perf_pmu_register(&perf_cpu_clock
, NULL
, -1);
9452 perf_pmu_register(&perf_task_clock
, NULL
, -1);
9454 perf_cpu_notifier(perf_cpu_notify
);
9455 register_reboot_notifier(&perf_reboot_notifier
);
9457 ret
= init_hw_breakpoint();
9458 WARN(ret
, "hw_breakpoint initialization failed with: %d", ret
);
9461 * Build time assertion that we keep the data_head at the intended
9462 * location. IOW, validation we got the __reserved[] size right.
9464 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page
, data_head
))
9468 ssize_t
perf_event_sysfs_show(struct device
*dev
, struct device_attribute
*attr
,
9471 struct perf_pmu_events_attr
*pmu_attr
=
9472 container_of(attr
, struct perf_pmu_events_attr
, attr
);
9474 if (pmu_attr
->event_str
)
9475 return sprintf(page
, "%s\n", pmu_attr
->event_str
);
9479 EXPORT_SYMBOL_GPL(perf_event_sysfs_show
);
9481 static int __init
perf_event_sysfs_init(void)
9486 mutex_lock(&pmus_lock
);
9488 ret
= bus_register(&pmu_bus
);
9492 list_for_each_entry(pmu
, &pmus
, entry
) {
9493 if (!pmu
->name
|| pmu
->type
< 0)
9496 ret
= pmu_dev_alloc(pmu
);
9497 WARN(ret
, "Failed to register pmu: %s, reason %d\n", pmu
->name
, ret
);
9499 pmu_bus_running
= 1;
9503 mutex_unlock(&pmus_lock
);
9507 device_initcall(perf_event_sysfs_init
);
9509 #ifdef CONFIG_CGROUP_PERF
9510 static struct cgroup_subsys_state
*
9511 perf_cgroup_css_alloc(struct cgroup_subsys_state
*parent_css
)
9513 struct perf_cgroup
*jc
;
9515 jc
= kzalloc(sizeof(*jc
), GFP_KERNEL
);
9517 return ERR_PTR(-ENOMEM
);
9519 jc
->info
= alloc_percpu(struct perf_cgroup_info
);
9522 return ERR_PTR(-ENOMEM
);
9528 static void perf_cgroup_css_free(struct cgroup_subsys_state
*css
)
9530 struct perf_cgroup
*jc
= container_of(css
, struct perf_cgroup
, css
);
9532 free_percpu(jc
->info
);
9536 static int __perf_cgroup_move(void *info
)
9538 struct task_struct
*task
= info
;
9540 perf_cgroup_switch(task
, PERF_CGROUP_SWOUT
| PERF_CGROUP_SWIN
);
9545 static void perf_cgroup_attach(struct cgroup_taskset
*tset
)
9547 struct task_struct
*task
;
9548 struct cgroup_subsys_state
*css
;
9550 cgroup_taskset_for_each(task
, css
, tset
)
9551 task_function_call(task
, __perf_cgroup_move
, task
);
9554 struct cgroup_subsys perf_event_cgrp_subsys
= {
9555 .css_alloc
= perf_cgroup_css_alloc
,
9556 .css_free
= perf_cgroup_css_free
,
9557 .attach
= perf_cgroup_attach
,
9559 #endif /* CONFIG_CGROUP_PERF */