2 * Performance events core code:
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/idr.h>
17 #include <linux/file.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/hash.h>
21 #include <linux/tick.h>
22 #include <linux/sysfs.h>
23 #include <linux/dcache.h>
24 #include <linux/percpu.h>
25 #include <linux/ptrace.h>
26 #include <linux/reboot.h>
27 #include <linux/vmstat.h>
28 #include <linux/device.h>
29 #include <linux/export.h>
30 #include <linux/vmalloc.h>
31 #include <linux/hardirq.h>
32 #include <linux/rculist.h>
33 #include <linux/uaccess.h>
34 #include <linux/syscalls.h>
35 #include <linux/anon_inodes.h>
36 #include <linux/kernel_stat.h>
37 #include <linux/perf_event.h>
38 #include <linux/ftrace_event.h>
39 #include <linux/hw_breakpoint.h>
40 #include <linux/mm_types.h>
41 #include <linux/cgroup.h>
42 #include <linux/module.h>
46 #include <asm/irq_regs.h>
48 struct remote_function_call
{
49 struct task_struct
*p
;
50 int (*func
)(void *info
);
55 static void remote_function(void *data
)
57 struct remote_function_call
*tfc
= data
;
58 struct task_struct
*p
= tfc
->p
;
62 if (task_cpu(p
) != smp_processor_id() || !task_curr(p
))
66 tfc
->ret
= tfc
->func(tfc
->info
);
70 * task_function_call - call a function on the cpu on which a task runs
71 * @p: the task to evaluate
72 * @func: the function to be called
73 * @info: the function call argument
75 * Calls the function @func when the task is currently running. This might
76 * be on the current CPU, which just calls the function directly
78 * returns: @func return value, or
79 * -ESRCH - when the process isn't running
80 * -EAGAIN - when the process moved away
83 task_function_call(struct task_struct
*p
, int (*func
) (void *info
), void *info
)
85 struct remote_function_call data
= {
89 .ret
= -ESRCH
, /* No such (running) process */
93 smp_call_function_single(task_cpu(p
), remote_function
, &data
, 1);
99 * cpu_function_call - call a function on the cpu
100 * @func: the function to be called
101 * @info: the function call argument
103 * Calls the function @func on the remote cpu.
105 * returns: @func return value or -ENXIO when the cpu is offline
107 static int cpu_function_call(int cpu
, int (*func
) (void *info
), void *info
)
109 struct remote_function_call data
= {
113 .ret
= -ENXIO
, /* No such CPU */
116 smp_call_function_single(cpu
, remote_function
, &data
, 1);
121 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
122 PERF_FLAG_FD_OUTPUT |\
123 PERF_FLAG_PID_CGROUP |\
124 PERF_FLAG_FD_CLOEXEC)
127 * branch priv levels that need permission checks
129 #define PERF_SAMPLE_BRANCH_PERM_PLM \
130 (PERF_SAMPLE_BRANCH_KERNEL |\
131 PERF_SAMPLE_BRANCH_HV)
134 EVENT_FLEXIBLE
= 0x1,
136 EVENT_ALL
= EVENT_FLEXIBLE
| EVENT_PINNED
,
140 * perf_sched_events : >0 events exist
141 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
143 struct static_key_deferred perf_sched_events __read_mostly
;
144 static DEFINE_PER_CPU(atomic_t
, perf_cgroup_events
);
145 static DEFINE_PER_CPU(atomic_t
, perf_branch_stack_events
);
147 static atomic_t nr_mmap_events __read_mostly
;
148 static atomic_t nr_comm_events __read_mostly
;
149 static atomic_t nr_task_events __read_mostly
;
150 static atomic_t nr_freq_events __read_mostly
;
152 static LIST_HEAD(pmus
);
153 static DEFINE_MUTEX(pmus_lock
);
154 static struct srcu_struct pmus_srcu
;
157 * perf event paranoia level:
158 * -1 - not paranoid at all
159 * 0 - disallow raw tracepoint access for unpriv
160 * 1 - disallow cpu events for unpriv
161 * 2 - disallow kernel profiling for unpriv
163 int sysctl_perf_event_paranoid __read_mostly
= 1;
165 /* Minimum for 512 kiB + 1 user control page */
166 int sysctl_perf_event_mlock __read_mostly
= 512 + (PAGE_SIZE
/ 1024); /* 'free' kiB per user */
169 * max perf event sample rate
171 #define DEFAULT_MAX_SAMPLE_RATE 100000
172 #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
173 #define DEFAULT_CPU_TIME_MAX_PERCENT 25
175 int sysctl_perf_event_sample_rate __read_mostly
= DEFAULT_MAX_SAMPLE_RATE
;
177 static int max_samples_per_tick __read_mostly
= DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE
, HZ
);
178 static int perf_sample_period_ns __read_mostly
= DEFAULT_SAMPLE_PERIOD_NS
;
180 static int perf_sample_allowed_ns __read_mostly
=
181 DEFAULT_SAMPLE_PERIOD_NS
* DEFAULT_CPU_TIME_MAX_PERCENT
/ 100;
183 void update_perf_cpu_limits(void)
185 u64 tmp
= perf_sample_period_ns
;
187 tmp
*= sysctl_perf_cpu_time_max_percent
;
189 ACCESS_ONCE(perf_sample_allowed_ns
) = tmp
;
192 static int perf_rotate_context(struct perf_cpu_context
*cpuctx
);
194 int perf_proc_update_handler(struct ctl_table
*table
, int write
,
195 void __user
*buffer
, size_t *lenp
,
198 int ret
= proc_dointvec_minmax(table
, write
, buffer
, lenp
, ppos
);
203 max_samples_per_tick
= DIV_ROUND_UP(sysctl_perf_event_sample_rate
, HZ
);
204 perf_sample_period_ns
= NSEC_PER_SEC
/ sysctl_perf_event_sample_rate
;
205 update_perf_cpu_limits();
210 int sysctl_perf_cpu_time_max_percent __read_mostly
= DEFAULT_CPU_TIME_MAX_PERCENT
;
212 int perf_cpu_time_max_percent_handler(struct ctl_table
*table
, int write
,
213 void __user
*buffer
, size_t *lenp
,
216 int ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
221 update_perf_cpu_limits();
227 * perf samples are done in some very critical code paths (NMIs).
228 * If they take too much CPU time, the system can lock up and not
229 * get any real work done. This will drop the sample rate when
230 * we detect that events are taking too long.
232 #define NR_ACCUMULATED_SAMPLES 128
233 static DEFINE_PER_CPU(u64
, running_sample_length
);
235 static void perf_duration_warn(struct irq_work
*w
)
237 u64 allowed_ns
= ACCESS_ONCE(perf_sample_allowed_ns
);
238 u64 avg_local_sample_len
;
239 u64 local_samples_len
;
241 local_samples_len
= __get_cpu_var(running_sample_length
);
242 avg_local_sample_len
= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
244 printk_ratelimited(KERN_WARNING
245 "perf interrupt took too long (%lld > %lld), lowering "
246 "kernel.perf_event_max_sample_rate to %d\n",
247 avg_local_sample_len
, allowed_ns
>> 1,
248 sysctl_perf_event_sample_rate
);
251 static DEFINE_IRQ_WORK(perf_duration_work
, perf_duration_warn
);
253 void perf_sample_event_took(u64 sample_len_ns
)
255 u64 allowed_ns
= ACCESS_ONCE(perf_sample_allowed_ns
);
256 u64 avg_local_sample_len
;
257 u64 local_samples_len
;
262 /* decay the counter by 1 average sample */
263 local_samples_len
= __get_cpu_var(running_sample_length
);
264 local_samples_len
-= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
265 local_samples_len
+= sample_len_ns
;
266 __get_cpu_var(running_sample_length
) = local_samples_len
;
269 * note: this will be biased artifically low until we have
270 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
271 * from having to maintain a count.
273 avg_local_sample_len
= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
275 if (avg_local_sample_len
<= allowed_ns
)
278 if (max_samples_per_tick
<= 1)
281 max_samples_per_tick
= DIV_ROUND_UP(max_samples_per_tick
, 2);
282 sysctl_perf_event_sample_rate
= max_samples_per_tick
* HZ
;
283 perf_sample_period_ns
= NSEC_PER_SEC
/ sysctl_perf_event_sample_rate
;
285 update_perf_cpu_limits();
287 if (!irq_work_queue(&perf_duration_work
)) {
288 early_printk("perf interrupt took too long (%lld > %lld), lowering "
289 "kernel.perf_event_max_sample_rate to %d\n",
290 avg_local_sample_len
, allowed_ns
>> 1,
291 sysctl_perf_event_sample_rate
);
295 static atomic64_t perf_event_id
;
297 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
298 enum event_type_t event_type
);
300 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
301 enum event_type_t event_type
,
302 struct task_struct
*task
);
304 static void update_context_time(struct perf_event_context
*ctx
);
305 static u64
perf_event_time(struct perf_event
*event
);
307 void __weak
perf_event_print_debug(void) { }
309 extern __weak
const char *perf_pmu_name(void)
314 static inline u64
perf_clock(void)
316 return local_clock();
319 static inline struct perf_cpu_context
*
320 __get_cpu_context(struct perf_event_context
*ctx
)
322 return this_cpu_ptr(ctx
->pmu
->pmu_cpu_context
);
325 static void perf_ctx_lock(struct perf_cpu_context
*cpuctx
,
326 struct perf_event_context
*ctx
)
328 raw_spin_lock(&cpuctx
->ctx
.lock
);
330 raw_spin_lock(&ctx
->lock
);
333 static void perf_ctx_unlock(struct perf_cpu_context
*cpuctx
,
334 struct perf_event_context
*ctx
)
337 raw_spin_unlock(&ctx
->lock
);
338 raw_spin_unlock(&cpuctx
->ctx
.lock
);
341 #ifdef CONFIG_CGROUP_PERF
344 * perf_cgroup_info keeps track of time_enabled for a cgroup.
345 * This is a per-cpu dynamically allocated data structure.
347 struct perf_cgroup_info
{
353 struct cgroup_subsys_state css
;
354 struct perf_cgroup_info __percpu
*info
;
358 * Must ensure cgroup is pinned (css_get) before calling
359 * this function. In other words, we cannot call this function
360 * if there is no cgroup event for the current CPU context.
362 static inline struct perf_cgroup
*
363 perf_cgroup_from_task(struct task_struct
*task
)
365 return container_of(task_css(task
, perf_event_cgrp_id
),
366 struct perf_cgroup
, css
);
370 perf_cgroup_match(struct perf_event
*event
)
372 struct perf_event_context
*ctx
= event
->ctx
;
373 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
375 /* @event doesn't care about cgroup */
379 /* wants specific cgroup scope but @cpuctx isn't associated with any */
384 * Cgroup scoping is recursive. An event enabled for a cgroup is
385 * also enabled for all its descendant cgroups. If @cpuctx's
386 * cgroup is a descendant of @event's (the test covers identity
387 * case), it's a match.
389 return cgroup_is_descendant(cpuctx
->cgrp
->css
.cgroup
,
390 event
->cgrp
->css
.cgroup
);
393 static inline void perf_put_cgroup(struct perf_event
*event
)
395 css_put(&event
->cgrp
->css
);
398 static inline void perf_detach_cgroup(struct perf_event
*event
)
400 perf_put_cgroup(event
);
404 static inline int is_cgroup_event(struct perf_event
*event
)
406 return event
->cgrp
!= NULL
;
409 static inline u64
perf_cgroup_event_time(struct perf_event
*event
)
411 struct perf_cgroup_info
*t
;
413 t
= per_cpu_ptr(event
->cgrp
->info
, event
->cpu
);
417 static inline void __update_cgrp_time(struct perf_cgroup
*cgrp
)
419 struct perf_cgroup_info
*info
;
424 info
= this_cpu_ptr(cgrp
->info
);
426 info
->time
+= now
- info
->timestamp
;
427 info
->timestamp
= now
;
430 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context
*cpuctx
)
432 struct perf_cgroup
*cgrp_out
= cpuctx
->cgrp
;
434 __update_cgrp_time(cgrp_out
);
437 static inline void update_cgrp_time_from_event(struct perf_event
*event
)
439 struct perf_cgroup
*cgrp
;
442 * ensure we access cgroup data only when needed and
443 * when we know the cgroup is pinned (css_get)
445 if (!is_cgroup_event(event
))
448 cgrp
= perf_cgroup_from_task(current
);
450 * Do not update time when cgroup is not active
452 if (cgrp
== event
->cgrp
)
453 __update_cgrp_time(event
->cgrp
);
457 perf_cgroup_set_timestamp(struct task_struct
*task
,
458 struct perf_event_context
*ctx
)
460 struct perf_cgroup
*cgrp
;
461 struct perf_cgroup_info
*info
;
464 * ctx->lock held by caller
465 * ensure we do not access cgroup data
466 * unless we have the cgroup pinned (css_get)
468 if (!task
|| !ctx
->nr_cgroups
)
471 cgrp
= perf_cgroup_from_task(task
);
472 info
= this_cpu_ptr(cgrp
->info
);
473 info
->timestamp
= ctx
->timestamp
;
476 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
477 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
480 * reschedule events based on the cgroup constraint of task.
482 * mode SWOUT : schedule out everything
483 * mode SWIN : schedule in based on cgroup for next
485 void perf_cgroup_switch(struct task_struct
*task
, int mode
)
487 struct perf_cpu_context
*cpuctx
;
492 * disable interrupts to avoid geting nr_cgroup
493 * changes via __perf_event_disable(). Also
496 local_irq_save(flags
);
499 * we reschedule only in the presence of cgroup
500 * constrained events.
504 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
505 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
506 if (cpuctx
->unique_pmu
!= pmu
)
507 continue; /* ensure we process each cpuctx once */
510 * perf_cgroup_events says at least one
511 * context on this CPU has cgroup events.
513 * ctx->nr_cgroups reports the number of cgroup
514 * events for a context.
516 if (cpuctx
->ctx
.nr_cgroups
> 0) {
517 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
518 perf_pmu_disable(cpuctx
->ctx
.pmu
);
520 if (mode
& PERF_CGROUP_SWOUT
) {
521 cpu_ctx_sched_out(cpuctx
, EVENT_ALL
);
523 * must not be done before ctxswout due
524 * to event_filter_match() in event_sched_out()
529 if (mode
& PERF_CGROUP_SWIN
) {
530 WARN_ON_ONCE(cpuctx
->cgrp
);
532 * set cgrp before ctxsw in to allow
533 * event_filter_match() to not have to pass
536 cpuctx
->cgrp
= perf_cgroup_from_task(task
);
537 cpu_ctx_sched_in(cpuctx
, EVENT_ALL
, task
);
539 perf_pmu_enable(cpuctx
->ctx
.pmu
);
540 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
546 local_irq_restore(flags
);
549 static inline void perf_cgroup_sched_out(struct task_struct
*task
,
550 struct task_struct
*next
)
552 struct perf_cgroup
*cgrp1
;
553 struct perf_cgroup
*cgrp2
= NULL
;
556 * we come here when we know perf_cgroup_events > 0
558 cgrp1
= perf_cgroup_from_task(task
);
561 * next is NULL when called from perf_event_enable_on_exec()
562 * that will systematically cause a cgroup_switch()
565 cgrp2
= perf_cgroup_from_task(next
);
568 * only schedule out current cgroup events if we know
569 * that we are switching to a different cgroup. Otherwise,
570 * do no touch the cgroup events.
573 perf_cgroup_switch(task
, PERF_CGROUP_SWOUT
);
576 static inline void perf_cgroup_sched_in(struct task_struct
*prev
,
577 struct task_struct
*task
)
579 struct perf_cgroup
*cgrp1
;
580 struct perf_cgroup
*cgrp2
= NULL
;
583 * we come here when we know perf_cgroup_events > 0
585 cgrp1
= perf_cgroup_from_task(task
);
587 /* prev can never be NULL */
588 cgrp2
= perf_cgroup_from_task(prev
);
591 * only need to schedule in cgroup events if we are changing
592 * cgroup during ctxsw. Cgroup events were not scheduled
593 * out of ctxsw out if that was not the case.
596 perf_cgroup_switch(task
, PERF_CGROUP_SWIN
);
599 static inline int perf_cgroup_connect(int fd
, struct perf_event
*event
,
600 struct perf_event_attr
*attr
,
601 struct perf_event
*group_leader
)
603 struct perf_cgroup
*cgrp
;
604 struct cgroup_subsys_state
*css
;
605 struct fd f
= fdget(fd
);
611 css
= css_tryget_from_dir(f
.file
->f_dentry
, &perf_event_cgrp_subsys
);
617 cgrp
= container_of(css
, struct perf_cgroup
, css
);
621 * all events in a group must monitor
622 * the same cgroup because a task belongs
623 * to only one perf cgroup at a time
625 if (group_leader
&& group_leader
->cgrp
!= cgrp
) {
626 perf_detach_cgroup(event
);
635 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
637 struct perf_cgroup_info
*t
;
638 t
= per_cpu_ptr(event
->cgrp
->info
, event
->cpu
);
639 event
->shadow_ctx_time
= now
- t
->timestamp
;
643 perf_cgroup_defer_enabled(struct perf_event
*event
)
646 * when the current task's perf cgroup does not match
647 * the event's, we need to remember to call the
648 * perf_mark_enable() function the first time a task with
649 * a matching perf cgroup is scheduled in.
651 if (is_cgroup_event(event
) && !perf_cgroup_match(event
))
652 event
->cgrp_defer_enabled
= 1;
656 perf_cgroup_mark_enabled(struct perf_event
*event
,
657 struct perf_event_context
*ctx
)
659 struct perf_event
*sub
;
660 u64 tstamp
= perf_event_time(event
);
662 if (!event
->cgrp_defer_enabled
)
665 event
->cgrp_defer_enabled
= 0;
667 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
668 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
669 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
) {
670 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
671 sub
->cgrp_defer_enabled
= 0;
675 #else /* !CONFIG_CGROUP_PERF */
678 perf_cgroup_match(struct perf_event
*event
)
683 static inline void perf_detach_cgroup(struct perf_event
*event
)
686 static inline int is_cgroup_event(struct perf_event
*event
)
691 static inline u64
perf_cgroup_event_cgrp_time(struct perf_event
*event
)
696 static inline void update_cgrp_time_from_event(struct perf_event
*event
)
700 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context
*cpuctx
)
704 static inline void perf_cgroup_sched_out(struct task_struct
*task
,
705 struct task_struct
*next
)
709 static inline void perf_cgroup_sched_in(struct task_struct
*prev
,
710 struct task_struct
*task
)
714 static inline int perf_cgroup_connect(pid_t pid
, struct perf_event
*event
,
715 struct perf_event_attr
*attr
,
716 struct perf_event
*group_leader
)
722 perf_cgroup_set_timestamp(struct task_struct
*task
,
723 struct perf_event_context
*ctx
)
728 perf_cgroup_switch(struct task_struct
*task
, struct task_struct
*next
)
733 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
737 static inline u64
perf_cgroup_event_time(struct perf_event
*event
)
743 perf_cgroup_defer_enabled(struct perf_event
*event
)
748 perf_cgroup_mark_enabled(struct perf_event
*event
,
749 struct perf_event_context
*ctx
)
755 * set default to be dependent on timer tick just
758 #define PERF_CPU_HRTIMER (1000 / HZ)
760 * function must be called with interrupts disbled
762 static enum hrtimer_restart
perf_cpu_hrtimer_handler(struct hrtimer
*hr
)
764 struct perf_cpu_context
*cpuctx
;
765 enum hrtimer_restart ret
= HRTIMER_NORESTART
;
768 WARN_ON(!irqs_disabled());
770 cpuctx
= container_of(hr
, struct perf_cpu_context
, hrtimer
);
772 rotations
= perf_rotate_context(cpuctx
);
775 * arm timer if needed
778 hrtimer_forward_now(hr
, cpuctx
->hrtimer_interval
);
779 ret
= HRTIMER_RESTART
;
785 /* CPU is going down */
786 void perf_cpu_hrtimer_cancel(int cpu
)
788 struct perf_cpu_context
*cpuctx
;
792 if (WARN_ON(cpu
!= smp_processor_id()))
795 local_irq_save(flags
);
799 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
800 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
802 if (pmu
->task_ctx_nr
== perf_sw_context
)
805 hrtimer_cancel(&cpuctx
->hrtimer
);
810 local_irq_restore(flags
);
813 static void __perf_cpu_hrtimer_init(struct perf_cpu_context
*cpuctx
, int cpu
)
815 struct hrtimer
*hr
= &cpuctx
->hrtimer
;
816 struct pmu
*pmu
= cpuctx
->ctx
.pmu
;
819 /* no multiplexing needed for SW PMU */
820 if (pmu
->task_ctx_nr
== perf_sw_context
)
824 * check default is sane, if not set then force to
825 * default interval (1/tick)
827 timer
= pmu
->hrtimer_interval_ms
;
829 timer
= pmu
->hrtimer_interval_ms
= PERF_CPU_HRTIMER
;
831 cpuctx
->hrtimer_interval
= ns_to_ktime(NSEC_PER_MSEC
* timer
);
833 hrtimer_init(hr
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL_PINNED
);
834 hr
->function
= perf_cpu_hrtimer_handler
;
837 static void perf_cpu_hrtimer_restart(struct perf_cpu_context
*cpuctx
)
839 struct hrtimer
*hr
= &cpuctx
->hrtimer
;
840 struct pmu
*pmu
= cpuctx
->ctx
.pmu
;
843 if (pmu
->task_ctx_nr
== perf_sw_context
)
846 if (hrtimer_active(hr
))
849 if (!hrtimer_callback_running(hr
))
850 __hrtimer_start_range_ns(hr
, cpuctx
->hrtimer_interval
,
851 0, HRTIMER_MODE_REL_PINNED
, 0);
854 void perf_pmu_disable(struct pmu
*pmu
)
856 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
858 pmu
->pmu_disable(pmu
);
861 void perf_pmu_enable(struct pmu
*pmu
)
863 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
865 pmu
->pmu_enable(pmu
);
868 static DEFINE_PER_CPU(struct list_head
, rotation_list
);
871 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
872 * because they're strictly cpu affine and rotate_start is called with IRQs
873 * disabled, while rotate_context is called from IRQ context.
875 static void perf_pmu_rotate_start(struct pmu
*pmu
)
877 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
878 struct list_head
*head
= &__get_cpu_var(rotation_list
);
880 WARN_ON(!irqs_disabled());
882 if (list_empty(&cpuctx
->rotation_list
))
883 list_add(&cpuctx
->rotation_list
, head
);
886 static void get_ctx(struct perf_event_context
*ctx
)
888 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
891 static void put_ctx(struct perf_event_context
*ctx
)
893 if (atomic_dec_and_test(&ctx
->refcount
)) {
895 put_ctx(ctx
->parent_ctx
);
897 put_task_struct(ctx
->task
);
898 kfree_rcu(ctx
, rcu_head
);
902 static void unclone_ctx(struct perf_event_context
*ctx
)
904 if (ctx
->parent_ctx
) {
905 put_ctx(ctx
->parent_ctx
);
906 ctx
->parent_ctx
= NULL
;
911 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
914 * only top level events have the pid namespace they were created in
917 event
= event
->parent
;
919 return task_tgid_nr_ns(p
, event
->ns
);
922 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
925 * only top level events have the pid namespace they were created in
928 event
= event
->parent
;
930 return task_pid_nr_ns(p
, event
->ns
);
934 * If we inherit events we want to return the parent event id
937 static u64
primary_event_id(struct perf_event
*event
)
942 id
= event
->parent
->id
;
948 * Get the perf_event_context for a task and lock it.
949 * This has to cope with with the fact that until it is locked,
950 * the context could get moved to another task.
952 static struct perf_event_context
*
953 perf_lock_task_context(struct task_struct
*task
, int ctxn
, unsigned long *flags
)
955 struct perf_event_context
*ctx
;
959 * One of the few rules of preemptible RCU is that one cannot do
960 * rcu_read_unlock() while holding a scheduler (or nested) lock when
961 * part of the read side critical section was preemptible -- see
962 * rcu_read_unlock_special().
964 * Since ctx->lock nests under rq->lock we must ensure the entire read
965 * side critical section is non-preemptible.
969 ctx
= rcu_dereference(task
->perf_event_ctxp
[ctxn
]);
972 * If this context is a clone of another, it might
973 * get swapped for another underneath us by
974 * perf_event_task_sched_out, though the
975 * rcu_read_lock() protects us from any context
976 * getting freed. Lock the context and check if it
977 * got swapped before we could get the lock, and retry
978 * if so. If we locked the right context, then it
979 * can't get swapped on us any more.
981 raw_spin_lock_irqsave(&ctx
->lock
, *flags
);
982 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
[ctxn
])) {
983 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
989 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
990 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
1000 * Get the context for a task and increment its pin_count so it
1001 * can't get swapped to another task. This also increments its
1002 * reference count so that the context can't get freed.
1004 static struct perf_event_context
*
1005 perf_pin_task_context(struct task_struct
*task
, int ctxn
)
1007 struct perf_event_context
*ctx
;
1008 unsigned long flags
;
1010 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
1013 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1018 static void perf_unpin_context(struct perf_event_context
*ctx
)
1020 unsigned long flags
;
1022 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
1024 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1028 * Update the record of the current time in a context.
1030 static void update_context_time(struct perf_event_context
*ctx
)
1032 u64 now
= perf_clock();
1034 ctx
->time
+= now
- ctx
->timestamp
;
1035 ctx
->timestamp
= now
;
1038 static u64
perf_event_time(struct perf_event
*event
)
1040 struct perf_event_context
*ctx
= event
->ctx
;
1042 if (is_cgroup_event(event
))
1043 return perf_cgroup_event_time(event
);
1045 return ctx
? ctx
->time
: 0;
1049 * Update the total_time_enabled and total_time_running fields for a event.
1050 * The caller of this function needs to hold the ctx->lock.
1052 static void update_event_times(struct perf_event
*event
)
1054 struct perf_event_context
*ctx
= event
->ctx
;
1057 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
1058 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
1061 * in cgroup mode, time_enabled represents
1062 * the time the event was enabled AND active
1063 * tasks were in the monitored cgroup. This is
1064 * independent of the activity of the context as
1065 * there may be a mix of cgroup and non-cgroup events.
1067 * That is why we treat cgroup events differently
1070 if (is_cgroup_event(event
))
1071 run_end
= perf_cgroup_event_time(event
);
1072 else if (ctx
->is_active
)
1073 run_end
= ctx
->time
;
1075 run_end
= event
->tstamp_stopped
;
1077 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
1079 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
1080 run_end
= event
->tstamp_stopped
;
1082 run_end
= perf_event_time(event
);
1084 event
->total_time_running
= run_end
- event
->tstamp_running
;
1089 * Update total_time_enabled and total_time_running for all events in a group.
1091 static void update_group_times(struct perf_event
*leader
)
1093 struct perf_event
*event
;
1095 update_event_times(leader
);
1096 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
1097 update_event_times(event
);
1100 static struct list_head
*
1101 ctx_group_list(struct perf_event
*event
, struct perf_event_context
*ctx
)
1103 if (event
->attr
.pinned
)
1104 return &ctx
->pinned_groups
;
1106 return &ctx
->flexible_groups
;
1110 * Add a event from the lists for its context.
1111 * Must be called with ctx->mutex and ctx->lock held.
1114 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
1116 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_CONTEXT
);
1117 event
->attach_state
|= PERF_ATTACH_CONTEXT
;
1120 * If we're a stand alone event or group leader, we go to the context
1121 * list, group events are kept attached to the group so that
1122 * perf_group_detach can, at all times, locate all siblings.
1124 if (event
->group_leader
== event
) {
1125 struct list_head
*list
;
1127 if (is_software_event(event
))
1128 event
->group_flags
|= PERF_GROUP_SOFTWARE
;
1130 list
= ctx_group_list(event
, ctx
);
1131 list_add_tail(&event
->group_entry
, list
);
1134 if (is_cgroup_event(event
))
1137 if (has_branch_stack(event
))
1138 ctx
->nr_branch_stack
++;
1140 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
1141 if (!ctx
->nr_events
)
1142 perf_pmu_rotate_start(ctx
->pmu
);
1144 if (event
->attr
.inherit_stat
)
1151 * Initialize event state based on the perf_event_attr::disabled.
1153 static inline void perf_event__state_init(struct perf_event
*event
)
1155 event
->state
= event
->attr
.disabled
? PERF_EVENT_STATE_OFF
:
1156 PERF_EVENT_STATE_INACTIVE
;
1160 * Called at perf_event creation and when events are attached/detached from a
1163 static void perf_event__read_size(struct perf_event
*event
)
1165 int entry
= sizeof(u64
); /* value */
1169 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1170 size
+= sizeof(u64
);
1172 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1173 size
+= sizeof(u64
);
1175 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
1176 entry
+= sizeof(u64
);
1178 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
1179 nr
+= event
->group_leader
->nr_siblings
;
1180 size
+= sizeof(u64
);
1184 event
->read_size
= size
;
1187 static void perf_event__header_size(struct perf_event
*event
)
1189 struct perf_sample_data
*data
;
1190 u64 sample_type
= event
->attr
.sample_type
;
1193 perf_event__read_size(event
);
1195 if (sample_type
& PERF_SAMPLE_IP
)
1196 size
+= sizeof(data
->ip
);
1198 if (sample_type
& PERF_SAMPLE_ADDR
)
1199 size
+= sizeof(data
->addr
);
1201 if (sample_type
& PERF_SAMPLE_PERIOD
)
1202 size
+= sizeof(data
->period
);
1204 if (sample_type
& PERF_SAMPLE_WEIGHT
)
1205 size
+= sizeof(data
->weight
);
1207 if (sample_type
& PERF_SAMPLE_READ
)
1208 size
+= event
->read_size
;
1210 if (sample_type
& PERF_SAMPLE_DATA_SRC
)
1211 size
+= sizeof(data
->data_src
.val
);
1213 if (sample_type
& PERF_SAMPLE_TRANSACTION
)
1214 size
+= sizeof(data
->txn
);
1216 event
->header_size
= size
;
1219 static void perf_event__id_header_size(struct perf_event
*event
)
1221 struct perf_sample_data
*data
;
1222 u64 sample_type
= event
->attr
.sample_type
;
1225 if (sample_type
& PERF_SAMPLE_TID
)
1226 size
+= sizeof(data
->tid_entry
);
1228 if (sample_type
& PERF_SAMPLE_TIME
)
1229 size
+= sizeof(data
->time
);
1231 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
1232 size
+= sizeof(data
->id
);
1234 if (sample_type
& PERF_SAMPLE_ID
)
1235 size
+= sizeof(data
->id
);
1237 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
1238 size
+= sizeof(data
->stream_id
);
1240 if (sample_type
& PERF_SAMPLE_CPU
)
1241 size
+= sizeof(data
->cpu_entry
);
1243 event
->id_header_size
= size
;
1246 static void perf_group_attach(struct perf_event
*event
)
1248 struct perf_event
*group_leader
= event
->group_leader
, *pos
;
1251 * We can have double attach due to group movement in perf_event_open.
1253 if (event
->attach_state
& PERF_ATTACH_GROUP
)
1256 event
->attach_state
|= PERF_ATTACH_GROUP
;
1258 if (group_leader
== event
)
1261 if (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
&&
1262 !is_software_event(event
))
1263 group_leader
->group_flags
&= ~PERF_GROUP_SOFTWARE
;
1265 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
1266 group_leader
->nr_siblings
++;
1268 perf_event__header_size(group_leader
);
1270 list_for_each_entry(pos
, &group_leader
->sibling_list
, group_entry
)
1271 perf_event__header_size(pos
);
1275 * Remove a event from the lists for its context.
1276 * Must be called with ctx->mutex and ctx->lock held.
1279 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
1281 struct perf_cpu_context
*cpuctx
;
1283 * We can have double detach due to exit/hot-unplug + close.
1285 if (!(event
->attach_state
& PERF_ATTACH_CONTEXT
))
1288 event
->attach_state
&= ~PERF_ATTACH_CONTEXT
;
1290 if (is_cgroup_event(event
)) {
1292 cpuctx
= __get_cpu_context(ctx
);
1294 * if there are no more cgroup events
1295 * then cler cgrp to avoid stale pointer
1296 * in update_cgrp_time_from_cpuctx()
1298 if (!ctx
->nr_cgroups
)
1299 cpuctx
->cgrp
= NULL
;
1302 if (has_branch_stack(event
))
1303 ctx
->nr_branch_stack
--;
1306 if (event
->attr
.inherit_stat
)
1309 list_del_rcu(&event
->event_entry
);
1311 if (event
->group_leader
== event
)
1312 list_del_init(&event
->group_entry
);
1314 update_group_times(event
);
1317 * If event was in error state, then keep it
1318 * that way, otherwise bogus counts will be
1319 * returned on read(). The only way to get out
1320 * of error state is by explicit re-enabling
1323 if (event
->state
> PERF_EVENT_STATE_OFF
)
1324 event
->state
= PERF_EVENT_STATE_OFF
;
1329 static void perf_group_detach(struct perf_event
*event
)
1331 struct perf_event
*sibling
, *tmp
;
1332 struct list_head
*list
= NULL
;
1335 * We can have double detach due to exit/hot-unplug + close.
1337 if (!(event
->attach_state
& PERF_ATTACH_GROUP
))
1340 event
->attach_state
&= ~PERF_ATTACH_GROUP
;
1343 * If this is a sibling, remove it from its group.
1345 if (event
->group_leader
!= event
) {
1346 list_del_init(&event
->group_entry
);
1347 event
->group_leader
->nr_siblings
--;
1351 if (!list_empty(&event
->group_entry
))
1352 list
= &event
->group_entry
;
1355 * If this was a group event with sibling events then
1356 * upgrade the siblings to singleton events by adding them
1357 * to whatever list we are on.
1359 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
1361 list_move_tail(&sibling
->group_entry
, list
);
1362 sibling
->group_leader
= sibling
;
1364 /* Inherit group flags from the previous leader */
1365 sibling
->group_flags
= event
->group_flags
;
1369 perf_event__header_size(event
->group_leader
);
1371 list_for_each_entry(tmp
, &event
->group_leader
->sibling_list
, group_entry
)
1372 perf_event__header_size(tmp
);
1376 event_filter_match(struct perf_event
*event
)
1378 return (event
->cpu
== -1 || event
->cpu
== smp_processor_id())
1379 && perf_cgroup_match(event
);
1383 event_sched_out(struct perf_event
*event
,
1384 struct perf_cpu_context
*cpuctx
,
1385 struct perf_event_context
*ctx
)
1387 u64 tstamp
= perf_event_time(event
);
1390 * An event which could not be activated because of
1391 * filter mismatch still needs to have its timings
1392 * maintained, otherwise bogus information is return
1393 * via read() for time_enabled, time_running:
1395 if (event
->state
== PERF_EVENT_STATE_INACTIVE
1396 && !event_filter_match(event
)) {
1397 delta
= tstamp
- event
->tstamp_stopped
;
1398 event
->tstamp_running
+= delta
;
1399 event
->tstamp_stopped
= tstamp
;
1402 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1405 perf_pmu_disable(event
->pmu
);
1407 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1408 if (event
->pending_disable
) {
1409 event
->pending_disable
= 0;
1410 event
->state
= PERF_EVENT_STATE_OFF
;
1412 event
->tstamp_stopped
= tstamp
;
1413 event
->pmu
->del(event
, 0);
1416 if (!is_software_event(event
))
1417 cpuctx
->active_oncpu
--;
1419 if (event
->attr
.freq
&& event
->attr
.sample_freq
)
1421 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
1422 cpuctx
->exclusive
= 0;
1424 perf_pmu_enable(event
->pmu
);
1428 group_sched_out(struct perf_event
*group_event
,
1429 struct perf_cpu_context
*cpuctx
,
1430 struct perf_event_context
*ctx
)
1432 struct perf_event
*event
;
1433 int state
= group_event
->state
;
1435 event_sched_out(group_event
, cpuctx
, ctx
);
1438 * Schedule out siblings (if any):
1440 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
1441 event_sched_out(event
, cpuctx
, ctx
);
1443 if (state
== PERF_EVENT_STATE_ACTIVE
&& group_event
->attr
.exclusive
)
1444 cpuctx
->exclusive
= 0;
1447 struct remove_event
{
1448 struct perf_event
*event
;
1453 * Cross CPU call to remove a performance event
1455 * We disable the event on the hardware level first. After that we
1456 * remove it from the context list.
1458 static int __perf_remove_from_context(void *info
)
1460 struct remove_event
*re
= info
;
1461 struct perf_event
*event
= re
->event
;
1462 struct perf_event_context
*ctx
= event
->ctx
;
1463 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1465 raw_spin_lock(&ctx
->lock
);
1466 event_sched_out(event
, cpuctx
, ctx
);
1467 if (re
->detach_group
)
1468 perf_group_detach(event
);
1469 list_del_event(event
, ctx
);
1470 if (!ctx
->nr_events
&& cpuctx
->task_ctx
== ctx
) {
1472 cpuctx
->task_ctx
= NULL
;
1474 raw_spin_unlock(&ctx
->lock
);
1481 * Remove the event from a task's (or a CPU's) list of events.
1483 * CPU events are removed with a smp call. For task events we only
1484 * call when the task is on a CPU.
1486 * If event->ctx is a cloned context, callers must make sure that
1487 * every task struct that event->ctx->task could possibly point to
1488 * remains valid. This is OK when called from perf_release since
1489 * that only calls us on the top-level context, which can't be a clone.
1490 * When called from perf_event_exit_task, it's OK because the
1491 * context has been detached from its task.
1493 static void perf_remove_from_context(struct perf_event
*event
, bool detach_group
)
1495 struct perf_event_context
*ctx
= event
->ctx
;
1496 struct task_struct
*task
= ctx
->task
;
1497 struct remove_event re
= {
1499 .detach_group
= detach_group
,
1502 lockdep_assert_held(&ctx
->mutex
);
1506 * Per cpu events are removed via an smp call and
1507 * the removal is always successful.
1509 cpu_function_call(event
->cpu
, __perf_remove_from_context
, &re
);
1514 if (!task_function_call(task
, __perf_remove_from_context
, &re
))
1517 raw_spin_lock_irq(&ctx
->lock
);
1519 * If we failed to find a running task, but find the context active now
1520 * that we've acquired the ctx->lock, retry.
1522 if (ctx
->is_active
) {
1523 raw_spin_unlock_irq(&ctx
->lock
);
1528 * Since the task isn't running, its safe to remove the event, us
1529 * holding the ctx->lock ensures the task won't get scheduled in.
1532 perf_group_detach(event
);
1533 list_del_event(event
, ctx
);
1534 raw_spin_unlock_irq(&ctx
->lock
);
1538 * Cross CPU call to disable a performance event
1540 int __perf_event_disable(void *info
)
1542 struct perf_event
*event
= info
;
1543 struct perf_event_context
*ctx
= event
->ctx
;
1544 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1547 * If this is a per-task event, need to check whether this
1548 * event's task is the current task on this cpu.
1550 * Can trigger due to concurrent perf_event_context_sched_out()
1551 * flipping contexts around.
1553 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
1556 raw_spin_lock(&ctx
->lock
);
1559 * If the event is on, turn it off.
1560 * If it is in error state, leave it in error state.
1562 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
) {
1563 update_context_time(ctx
);
1564 update_cgrp_time_from_event(event
);
1565 update_group_times(event
);
1566 if (event
== event
->group_leader
)
1567 group_sched_out(event
, cpuctx
, ctx
);
1569 event_sched_out(event
, cpuctx
, ctx
);
1570 event
->state
= PERF_EVENT_STATE_OFF
;
1573 raw_spin_unlock(&ctx
->lock
);
1581 * If event->ctx is a cloned context, callers must make sure that
1582 * every task struct that event->ctx->task could possibly point to
1583 * remains valid. This condition is satisifed when called through
1584 * perf_event_for_each_child or perf_event_for_each because they
1585 * hold the top-level event's child_mutex, so any descendant that
1586 * goes to exit will block in sync_child_event.
1587 * When called from perf_pending_event it's OK because event->ctx
1588 * is the current context on this CPU and preemption is disabled,
1589 * hence we can't get into perf_event_task_sched_out for this context.
1591 void perf_event_disable(struct perf_event
*event
)
1593 struct perf_event_context
*ctx
= event
->ctx
;
1594 struct task_struct
*task
= ctx
->task
;
1598 * Disable the event on the cpu that it's on
1600 cpu_function_call(event
->cpu
, __perf_event_disable
, event
);
1605 if (!task_function_call(task
, __perf_event_disable
, event
))
1608 raw_spin_lock_irq(&ctx
->lock
);
1610 * If the event is still active, we need to retry the cross-call.
1612 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
1613 raw_spin_unlock_irq(&ctx
->lock
);
1615 * Reload the task pointer, it might have been changed by
1616 * a concurrent perf_event_context_sched_out().
1623 * Since we have the lock this context can't be scheduled
1624 * in, so we can change the state safely.
1626 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1627 update_group_times(event
);
1628 event
->state
= PERF_EVENT_STATE_OFF
;
1630 raw_spin_unlock_irq(&ctx
->lock
);
1632 EXPORT_SYMBOL_GPL(perf_event_disable
);
1634 static void perf_set_shadow_time(struct perf_event
*event
,
1635 struct perf_event_context
*ctx
,
1639 * use the correct time source for the time snapshot
1641 * We could get by without this by leveraging the
1642 * fact that to get to this function, the caller
1643 * has most likely already called update_context_time()
1644 * and update_cgrp_time_xx() and thus both timestamp
1645 * are identical (or very close). Given that tstamp is,
1646 * already adjusted for cgroup, we could say that:
1647 * tstamp - ctx->timestamp
1649 * tstamp - cgrp->timestamp.
1651 * Then, in perf_output_read(), the calculation would
1652 * work with no changes because:
1653 * - event is guaranteed scheduled in
1654 * - no scheduled out in between
1655 * - thus the timestamp would be the same
1657 * But this is a bit hairy.
1659 * So instead, we have an explicit cgroup call to remain
1660 * within the time time source all along. We believe it
1661 * is cleaner and simpler to understand.
1663 if (is_cgroup_event(event
))
1664 perf_cgroup_set_shadow_time(event
, tstamp
);
1666 event
->shadow_ctx_time
= tstamp
- ctx
->timestamp
;
1669 #define MAX_INTERRUPTS (~0ULL)
1671 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1674 event_sched_in(struct perf_event
*event
,
1675 struct perf_cpu_context
*cpuctx
,
1676 struct perf_event_context
*ctx
)
1678 u64 tstamp
= perf_event_time(event
);
1681 lockdep_assert_held(&ctx
->lock
);
1683 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1686 event
->state
= PERF_EVENT_STATE_ACTIVE
;
1687 event
->oncpu
= smp_processor_id();
1690 * Unthrottle events, since we scheduled we might have missed several
1691 * ticks already, also for a heavily scheduling task there is little
1692 * guarantee it'll get a tick in a timely manner.
1694 if (unlikely(event
->hw
.interrupts
== MAX_INTERRUPTS
)) {
1695 perf_log_throttle(event
, 1);
1696 event
->hw
.interrupts
= 0;
1700 * The new state must be visible before we turn it on in the hardware:
1704 perf_pmu_disable(event
->pmu
);
1706 if (event
->pmu
->add(event
, PERF_EF_START
)) {
1707 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1713 event
->tstamp_running
+= tstamp
- event
->tstamp_stopped
;
1715 perf_set_shadow_time(event
, ctx
, tstamp
);
1717 if (!is_software_event(event
))
1718 cpuctx
->active_oncpu
++;
1720 if (event
->attr
.freq
&& event
->attr
.sample_freq
)
1723 if (event
->attr
.exclusive
)
1724 cpuctx
->exclusive
= 1;
1727 perf_pmu_enable(event
->pmu
);
1733 group_sched_in(struct perf_event
*group_event
,
1734 struct perf_cpu_context
*cpuctx
,
1735 struct perf_event_context
*ctx
)
1737 struct perf_event
*event
, *partial_group
= NULL
;
1738 struct pmu
*pmu
= ctx
->pmu
;
1739 u64 now
= ctx
->time
;
1740 bool simulate
= false;
1742 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
1745 pmu
->start_txn(pmu
);
1747 if (event_sched_in(group_event
, cpuctx
, ctx
)) {
1748 pmu
->cancel_txn(pmu
);
1749 perf_cpu_hrtimer_restart(cpuctx
);
1754 * Schedule in siblings as one group (if any):
1756 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
1757 if (event_sched_in(event
, cpuctx
, ctx
)) {
1758 partial_group
= event
;
1763 if (!pmu
->commit_txn(pmu
))
1768 * Groups can be scheduled in as one unit only, so undo any
1769 * partial group before returning:
1770 * The events up to the failed event are scheduled out normally,
1771 * tstamp_stopped will be updated.
1773 * The failed events and the remaining siblings need to have
1774 * their timings updated as if they had gone thru event_sched_in()
1775 * and event_sched_out(). This is required to get consistent timings
1776 * across the group. This also takes care of the case where the group
1777 * could never be scheduled by ensuring tstamp_stopped is set to mark
1778 * the time the event was actually stopped, such that time delta
1779 * calculation in update_event_times() is correct.
1781 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
1782 if (event
== partial_group
)
1786 event
->tstamp_running
+= now
- event
->tstamp_stopped
;
1787 event
->tstamp_stopped
= now
;
1789 event_sched_out(event
, cpuctx
, ctx
);
1792 event_sched_out(group_event
, cpuctx
, ctx
);
1794 pmu
->cancel_txn(pmu
);
1796 perf_cpu_hrtimer_restart(cpuctx
);
1802 * Work out whether we can put this event group on the CPU now.
1804 static int group_can_go_on(struct perf_event
*event
,
1805 struct perf_cpu_context
*cpuctx
,
1809 * Groups consisting entirely of software events can always go on.
1811 if (event
->group_flags
& PERF_GROUP_SOFTWARE
)
1814 * If an exclusive group is already on, no other hardware
1817 if (cpuctx
->exclusive
)
1820 * If this group is exclusive and there are already
1821 * events on the CPU, it can't go on.
1823 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
1826 * Otherwise, try to add it if all previous groups were able
1832 static void add_event_to_ctx(struct perf_event
*event
,
1833 struct perf_event_context
*ctx
)
1835 u64 tstamp
= perf_event_time(event
);
1837 list_add_event(event
, ctx
);
1838 perf_group_attach(event
);
1839 event
->tstamp_enabled
= tstamp
;
1840 event
->tstamp_running
= tstamp
;
1841 event
->tstamp_stopped
= tstamp
;
1844 static void task_ctx_sched_out(struct perf_event_context
*ctx
);
1846 ctx_sched_in(struct perf_event_context
*ctx
,
1847 struct perf_cpu_context
*cpuctx
,
1848 enum event_type_t event_type
,
1849 struct task_struct
*task
);
1851 static void perf_event_sched_in(struct perf_cpu_context
*cpuctx
,
1852 struct perf_event_context
*ctx
,
1853 struct task_struct
*task
)
1855 cpu_ctx_sched_in(cpuctx
, EVENT_PINNED
, task
);
1857 ctx_sched_in(ctx
, cpuctx
, EVENT_PINNED
, task
);
1858 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
, task
);
1860 ctx_sched_in(ctx
, cpuctx
, EVENT_FLEXIBLE
, task
);
1864 * Cross CPU call to install and enable a performance event
1866 * Must be called with ctx->mutex held
1868 static int __perf_install_in_context(void *info
)
1870 struct perf_event
*event
= info
;
1871 struct perf_event_context
*ctx
= event
->ctx
;
1872 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1873 struct perf_event_context
*task_ctx
= cpuctx
->task_ctx
;
1874 struct task_struct
*task
= current
;
1876 perf_ctx_lock(cpuctx
, task_ctx
);
1877 perf_pmu_disable(cpuctx
->ctx
.pmu
);
1880 * If there was an active task_ctx schedule it out.
1883 task_ctx_sched_out(task_ctx
);
1886 * If the context we're installing events in is not the
1887 * active task_ctx, flip them.
1889 if (ctx
->task
&& task_ctx
!= ctx
) {
1891 raw_spin_unlock(&task_ctx
->lock
);
1892 raw_spin_lock(&ctx
->lock
);
1897 cpuctx
->task_ctx
= task_ctx
;
1898 task
= task_ctx
->task
;
1901 cpu_ctx_sched_out(cpuctx
, EVENT_ALL
);
1903 update_context_time(ctx
);
1905 * update cgrp time only if current cgrp
1906 * matches event->cgrp. Must be done before
1907 * calling add_event_to_ctx()
1909 update_cgrp_time_from_event(event
);
1911 add_event_to_ctx(event
, ctx
);
1914 * Schedule everything back in
1916 perf_event_sched_in(cpuctx
, task_ctx
, task
);
1918 perf_pmu_enable(cpuctx
->ctx
.pmu
);
1919 perf_ctx_unlock(cpuctx
, task_ctx
);
1925 * Attach a performance event to a context
1927 * First we add the event to the list with the hardware enable bit
1928 * in event->hw_config cleared.
1930 * If the event is attached to a task which is on a CPU we use a smp
1931 * call to enable it in the task context. The task might have been
1932 * scheduled away, but we check this in the smp call again.
1935 perf_install_in_context(struct perf_event_context
*ctx
,
1936 struct perf_event
*event
,
1939 struct task_struct
*task
= ctx
->task
;
1941 lockdep_assert_held(&ctx
->mutex
);
1944 if (event
->cpu
!= -1)
1949 * Per cpu events are installed via an smp call and
1950 * the install is always successful.
1952 cpu_function_call(cpu
, __perf_install_in_context
, event
);
1957 if (!task_function_call(task
, __perf_install_in_context
, event
))
1960 raw_spin_lock_irq(&ctx
->lock
);
1962 * If we failed to find a running task, but find the context active now
1963 * that we've acquired the ctx->lock, retry.
1965 if (ctx
->is_active
) {
1966 raw_spin_unlock_irq(&ctx
->lock
);
1971 * Since the task isn't running, its safe to add the event, us holding
1972 * the ctx->lock ensures the task won't get scheduled in.
1974 add_event_to_ctx(event
, ctx
);
1975 raw_spin_unlock_irq(&ctx
->lock
);
1979 * Put a event into inactive state and update time fields.
1980 * Enabling the leader of a group effectively enables all
1981 * the group members that aren't explicitly disabled, so we
1982 * have to update their ->tstamp_enabled also.
1983 * Note: this works for group members as well as group leaders
1984 * since the non-leader members' sibling_lists will be empty.
1986 static void __perf_event_mark_enabled(struct perf_event
*event
)
1988 struct perf_event
*sub
;
1989 u64 tstamp
= perf_event_time(event
);
1991 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1992 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
1993 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
1994 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
)
1995 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
2000 * Cross CPU call to enable a performance event
2002 static int __perf_event_enable(void *info
)
2004 struct perf_event
*event
= info
;
2005 struct perf_event_context
*ctx
= event
->ctx
;
2006 struct perf_event
*leader
= event
->group_leader
;
2007 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
2011 * There's a time window between 'ctx->is_active' check
2012 * in perf_event_enable function and this place having:
2014 * - ctx->lock unlocked
2016 * where the task could be killed and 'ctx' deactivated
2017 * by perf_event_exit_task.
2019 if (!ctx
->is_active
)
2022 raw_spin_lock(&ctx
->lock
);
2023 update_context_time(ctx
);
2025 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
2029 * set current task's cgroup time reference point
2031 perf_cgroup_set_timestamp(current
, ctx
);
2033 __perf_event_mark_enabled(event
);
2035 if (!event_filter_match(event
)) {
2036 if (is_cgroup_event(event
))
2037 perf_cgroup_defer_enabled(event
);
2042 * If the event is in a group and isn't the group leader,
2043 * then don't put it on unless the group is on.
2045 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
)
2048 if (!group_can_go_on(event
, cpuctx
, 1)) {
2051 if (event
== leader
)
2052 err
= group_sched_in(event
, cpuctx
, ctx
);
2054 err
= event_sched_in(event
, cpuctx
, ctx
);
2059 * If this event can't go on and it's part of a
2060 * group, then the whole group has to come off.
2062 if (leader
!= event
) {
2063 group_sched_out(leader
, cpuctx
, ctx
);
2064 perf_cpu_hrtimer_restart(cpuctx
);
2066 if (leader
->attr
.pinned
) {
2067 update_group_times(leader
);
2068 leader
->state
= PERF_EVENT_STATE_ERROR
;
2073 raw_spin_unlock(&ctx
->lock
);
2081 * If event->ctx is a cloned context, callers must make sure that
2082 * every task struct that event->ctx->task could possibly point to
2083 * remains valid. This condition is satisfied when called through
2084 * perf_event_for_each_child or perf_event_for_each as described
2085 * for perf_event_disable.
2087 void perf_event_enable(struct perf_event
*event
)
2089 struct perf_event_context
*ctx
= event
->ctx
;
2090 struct task_struct
*task
= ctx
->task
;
2094 * Enable the event on the cpu that it's on
2096 cpu_function_call(event
->cpu
, __perf_event_enable
, event
);
2100 raw_spin_lock_irq(&ctx
->lock
);
2101 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
2105 * If the event is in error state, clear that first.
2106 * That way, if we see the event in error state below, we
2107 * know that it has gone back into error state, as distinct
2108 * from the task having been scheduled away before the
2109 * cross-call arrived.
2111 if (event
->state
== PERF_EVENT_STATE_ERROR
)
2112 event
->state
= PERF_EVENT_STATE_OFF
;
2115 if (!ctx
->is_active
) {
2116 __perf_event_mark_enabled(event
);
2120 raw_spin_unlock_irq(&ctx
->lock
);
2122 if (!task_function_call(task
, __perf_event_enable
, event
))
2125 raw_spin_lock_irq(&ctx
->lock
);
2128 * If the context is active and the event is still off,
2129 * we need to retry the cross-call.
2131 if (ctx
->is_active
&& event
->state
== PERF_EVENT_STATE_OFF
) {
2133 * task could have been flipped by a concurrent
2134 * perf_event_context_sched_out()
2141 raw_spin_unlock_irq(&ctx
->lock
);
2143 EXPORT_SYMBOL_GPL(perf_event_enable
);
2145 int perf_event_refresh(struct perf_event
*event
, int refresh
)
2148 * not supported on inherited events
2150 if (event
->attr
.inherit
|| !is_sampling_event(event
))
2153 atomic_add(refresh
, &event
->event_limit
);
2154 perf_event_enable(event
);
2158 EXPORT_SYMBOL_GPL(perf_event_refresh
);
2160 static void ctx_sched_out(struct perf_event_context
*ctx
,
2161 struct perf_cpu_context
*cpuctx
,
2162 enum event_type_t event_type
)
2164 struct perf_event
*event
;
2165 int is_active
= ctx
->is_active
;
2167 ctx
->is_active
&= ~event_type
;
2168 if (likely(!ctx
->nr_events
))
2171 update_context_time(ctx
);
2172 update_cgrp_time_from_cpuctx(cpuctx
);
2173 if (!ctx
->nr_active
)
2176 perf_pmu_disable(ctx
->pmu
);
2177 if ((is_active
& EVENT_PINNED
) && (event_type
& EVENT_PINNED
)) {
2178 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
)
2179 group_sched_out(event
, cpuctx
, ctx
);
2182 if ((is_active
& EVENT_FLEXIBLE
) && (event_type
& EVENT_FLEXIBLE
)) {
2183 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
)
2184 group_sched_out(event
, cpuctx
, ctx
);
2186 perf_pmu_enable(ctx
->pmu
);
2190 * Test whether two contexts are equivalent, i.e. whether they have both been
2191 * cloned from the same version of the same context.
2193 * Equivalence is measured using a generation number in the context that is
2194 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2195 * and list_del_event().
2197 static int context_equiv(struct perf_event_context
*ctx1
,
2198 struct perf_event_context
*ctx2
)
2200 /* Pinning disables the swap optimization */
2201 if (ctx1
->pin_count
|| ctx2
->pin_count
)
2204 /* If ctx1 is the parent of ctx2 */
2205 if (ctx1
== ctx2
->parent_ctx
&& ctx1
->generation
== ctx2
->parent_gen
)
2208 /* If ctx2 is the parent of ctx1 */
2209 if (ctx1
->parent_ctx
== ctx2
&& ctx1
->parent_gen
== ctx2
->generation
)
2213 * If ctx1 and ctx2 have the same parent; we flatten the parent
2214 * hierarchy, see perf_event_init_context().
2216 if (ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
&&
2217 ctx1
->parent_gen
== ctx2
->parent_gen
)
2224 static void __perf_event_sync_stat(struct perf_event
*event
,
2225 struct perf_event
*next_event
)
2229 if (!event
->attr
.inherit_stat
)
2233 * Update the event value, we cannot use perf_event_read()
2234 * because we're in the middle of a context switch and have IRQs
2235 * disabled, which upsets smp_call_function_single(), however
2236 * we know the event must be on the current CPU, therefore we
2237 * don't need to use it.
2239 switch (event
->state
) {
2240 case PERF_EVENT_STATE_ACTIVE
:
2241 event
->pmu
->read(event
);
2244 case PERF_EVENT_STATE_INACTIVE
:
2245 update_event_times(event
);
2253 * In order to keep per-task stats reliable we need to flip the event
2254 * values when we flip the contexts.
2256 value
= local64_read(&next_event
->count
);
2257 value
= local64_xchg(&event
->count
, value
);
2258 local64_set(&next_event
->count
, value
);
2260 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
2261 swap(event
->total_time_running
, next_event
->total_time_running
);
2264 * Since we swizzled the values, update the user visible data too.
2266 perf_event_update_userpage(event
);
2267 perf_event_update_userpage(next_event
);
2270 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
2271 struct perf_event_context
*next_ctx
)
2273 struct perf_event
*event
, *next_event
;
2278 update_context_time(ctx
);
2280 event
= list_first_entry(&ctx
->event_list
,
2281 struct perf_event
, event_entry
);
2283 next_event
= list_first_entry(&next_ctx
->event_list
,
2284 struct perf_event
, event_entry
);
2286 while (&event
->event_entry
!= &ctx
->event_list
&&
2287 &next_event
->event_entry
!= &next_ctx
->event_list
) {
2289 __perf_event_sync_stat(event
, next_event
);
2291 event
= list_next_entry(event
, event_entry
);
2292 next_event
= list_next_entry(next_event
, event_entry
);
2296 static void perf_event_context_sched_out(struct task_struct
*task
, int ctxn
,
2297 struct task_struct
*next
)
2299 struct perf_event_context
*ctx
= task
->perf_event_ctxp
[ctxn
];
2300 struct perf_event_context
*next_ctx
;
2301 struct perf_event_context
*parent
, *next_parent
;
2302 struct perf_cpu_context
*cpuctx
;
2308 cpuctx
= __get_cpu_context(ctx
);
2309 if (!cpuctx
->task_ctx
)
2313 next_ctx
= next
->perf_event_ctxp
[ctxn
];
2317 parent
= rcu_dereference(ctx
->parent_ctx
);
2318 next_parent
= rcu_dereference(next_ctx
->parent_ctx
);
2320 /* If neither context have a parent context; they cannot be clones. */
2321 if (!parent
&& !next_parent
)
2324 if (next_parent
== ctx
|| next_ctx
== parent
|| next_parent
== parent
) {
2326 * Looks like the two contexts are clones, so we might be
2327 * able to optimize the context switch. We lock both
2328 * contexts and check that they are clones under the
2329 * lock (including re-checking that neither has been
2330 * uncloned in the meantime). It doesn't matter which
2331 * order we take the locks because no other cpu could
2332 * be trying to lock both of these tasks.
2334 raw_spin_lock(&ctx
->lock
);
2335 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
2336 if (context_equiv(ctx
, next_ctx
)) {
2338 * XXX do we need a memory barrier of sorts
2339 * wrt to rcu_dereference() of perf_event_ctxp
2341 task
->perf_event_ctxp
[ctxn
] = next_ctx
;
2342 next
->perf_event_ctxp
[ctxn
] = ctx
;
2344 next_ctx
->task
= task
;
2347 perf_event_sync_stat(ctx
, next_ctx
);
2349 raw_spin_unlock(&next_ctx
->lock
);
2350 raw_spin_unlock(&ctx
->lock
);
2356 raw_spin_lock(&ctx
->lock
);
2357 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
2358 cpuctx
->task_ctx
= NULL
;
2359 raw_spin_unlock(&ctx
->lock
);
2363 #define for_each_task_context_nr(ctxn) \
2364 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2367 * Called from scheduler to remove the events of the current task,
2368 * with interrupts disabled.
2370 * We stop each event and update the event value in event->count.
2372 * This does not protect us against NMI, but disable()
2373 * sets the disabled bit in the control field of event _before_
2374 * accessing the event control register. If a NMI hits, then it will
2375 * not restart the event.
2377 void __perf_event_task_sched_out(struct task_struct
*task
,
2378 struct task_struct
*next
)
2382 for_each_task_context_nr(ctxn
)
2383 perf_event_context_sched_out(task
, ctxn
, next
);
2386 * if cgroup events exist on this CPU, then we need
2387 * to check if we have to switch out PMU state.
2388 * cgroup event are system-wide mode only
2390 if (atomic_read(&__get_cpu_var(perf_cgroup_events
)))
2391 perf_cgroup_sched_out(task
, next
);
2394 static void task_ctx_sched_out(struct perf_event_context
*ctx
)
2396 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
2398 if (!cpuctx
->task_ctx
)
2401 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
2404 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
2405 cpuctx
->task_ctx
= NULL
;
2409 * Called with IRQs disabled
2411 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
2412 enum event_type_t event_type
)
2414 ctx_sched_out(&cpuctx
->ctx
, cpuctx
, event_type
);
2418 ctx_pinned_sched_in(struct perf_event_context
*ctx
,
2419 struct perf_cpu_context
*cpuctx
)
2421 struct perf_event
*event
;
2423 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
2424 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2426 if (!event_filter_match(event
))
2429 /* may need to reset tstamp_enabled */
2430 if (is_cgroup_event(event
))
2431 perf_cgroup_mark_enabled(event
, ctx
);
2433 if (group_can_go_on(event
, cpuctx
, 1))
2434 group_sched_in(event
, cpuctx
, ctx
);
2437 * If this pinned group hasn't been scheduled,
2438 * put it in error state.
2440 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
2441 update_group_times(event
);
2442 event
->state
= PERF_EVENT_STATE_ERROR
;
2448 ctx_flexible_sched_in(struct perf_event_context
*ctx
,
2449 struct perf_cpu_context
*cpuctx
)
2451 struct perf_event
*event
;
2454 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
2455 /* Ignore events in OFF or ERROR state */
2456 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2459 * Listen to the 'cpu' scheduling filter constraint
2462 if (!event_filter_match(event
))
2465 /* may need to reset tstamp_enabled */
2466 if (is_cgroup_event(event
))
2467 perf_cgroup_mark_enabled(event
, ctx
);
2469 if (group_can_go_on(event
, cpuctx
, can_add_hw
)) {
2470 if (group_sched_in(event
, cpuctx
, ctx
))
2477 ctx_sched_in(struct perf_event_context
*ctx
,
2478 struct perf_cpu_context
*cpuctx
,
2479 enum event_type_t event_type
,
2480 struct task_struct
*task
)
2483 int is_active
= ctx
->is_active
;
2485 ctx
->is_active
|= event_type
;
2486 if (likely(!ctx
->nr_events
))
2490 ctx
->timestamp
= now
;
2491 perf_cgroup_set_timestamp(task
, ctx
);
2493 * First go through the list and put on any pinned groups
2494 * in order to give them the best chance of going on.
2496 if (!(is_active
& EVENT_PINNED
) && (event_type
& EVENT_PINNED
))
2497 ctx_pinned_sched_in(ctx
, cpuctx
);
2499 /* Then walk through the lower prio flexible groups */
2500 if (!(is_active
& EVENT_FLEXIBLE
) && (event_type
& EVENT_FLEXIBLE
))
2501 ctx_flexible_sched_in(ctx
, cpuctx
);
2504 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
2505 enum event_type_t event_type
,
2506 struct task_struct
*task
)
2508 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
2510 ctx_sched_in(ctx
, cpuctx
, event_type
, task
);
2513 static void perf_event_context_sched_in(struct perf_event_context
*ctx
,
2514 struct task_struct
*task
)
2516 struct perf_cpu_context
*cpuctx
;
2518 cpuctx
= __get_cpu_context(ctx
);
2519 if (cpuctx
->task_ctx
== ctx
)
2522 perf_ctx_lock(cpuctx
, ctx
);
2523 perf_pmu_disable(ctx
->pmu
);
2525 * We want to keep the following priority order:
2526 * cpu pinned (that don't need to move), task pinned,
2527 * cpu flexible, task flexible.
2529 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
2532 cpuctx
->task_ctx
= ctx
;
2534 perf_event_sched_in(cpuctx
, cpuctx
->task_ctx
, task
);
2536 perf_pmu_enable(ctx
->pmu
);
2537 perf_ctx_unlock(cpuctx
, ctx
);
2540 * Since these rotations are per-cpu, we need to ensure the
2541 * cpu-context we got scheduled on is actually rotating.
2543 perf_pmu_rotate_start(ctx
->pmu
);
2547 * When sampling the branck stack in system-wide, it may be necessary
2548 * to flush the stack on context switch. This happens when the branch
2549 * stack does not tag its entries with the pid of the current task.
2550 * Otherwise it becomes impossible to associate a branch entry with a
2551 * task. This ambiguity is more likely to appear when the branch stack
2552 * supports priv level filtering and the user sets it to monitor only
2553 * at the user level (which could be a useful measurement in system-wide
2554 * mode). In that case, the risk is high of having a branch stack with
2555 * branch from multiple tasks. Flushing may mean dropping the existing
2556 * entries or stashing them somewhere in the PMU specific code layer.
2558 * This function provides the context switch callback to the lower code
2559 * layer. It is invoked ONLY when there is at least one system-wide context
2560 * with at least one active event using taken branch sampling.
2562 static void perf_branch_stack_sched_in(struct task_struct
*prev
,
2563 struct task_struct
*task
)
2565 struct perf_cpu_context
*cpuctx
;
2567 unsigned long flags
;
2569 /* no need to flush branch stack if not changing task */
2573 local_irq_save(flags
);
2577 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
2578 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
2581 * check if the context has at least one
2582 * event using PERF_SAMPLE_BRANCH_STACK
2584 if (cpuctx
->ctx
.nr_branch_stack
> 0
2585 && pmu
->flush_branch_stack
) {
2587 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
2589 perf_pmu_disable(pmu
);
2591 pmu
->flush_branch_stack();
2593 perf_pmu_enable(pmu
);
2595 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
2601 local_irq_restore(flags
);
2605 * Called from scheduler to add the events of the current task
2606 * with interrupts disabled.
2608 * We restore the event value and then enable it.
2610 * This does not protect us against NMI, but enable()
2611 * sets the enabled bit in the control field of event _before_
2612 * accessing the event control register. If a NMI hits, then it will
2613 * keep the event running.
2615 void __perf_event_task_sched_in(struct task_struct
*prev
,
2616 struct task_struct
*task
)
2618 struct perf_event_context
*ctx
;
2621 for_each_task_context_nr(ctxn
) {
2622 ctx
= task
->perf_event_ctxp
[ctxn
];
2626 perf_event_context_sched_in(ctx
, task
);
2629 * if cgroup events exist on this CPU, then we need
2630 * to check if we have to switch in PMU state.
2631 * cgroup event are system-wide mode only
2633 if (atomic_read(&__get_cpu_var(perf_cgroup_events
)))
2634 perf_cgroup_sched_in(prev
, task
);
2636 /* check for system-wide branch_stack events */
2637 if (atomic_read(&__get_cpu_var(perf_branch_stack_events
)))
2638 perf_branch_stack_sched_in(prev
, task
);
2641 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
2643 u64 frequency
= event
->attr
.sample_freq
;
2644 u64 sec
= NSEC_PER_SEC
;
2645 u64 divisor
, dividend
;
2647 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
2649 count_fls
= fls64(count
);
2650 nsec_fls
= fls64(nsec
);
2651 frequency_fls
= fls64(frequency
);
2655 * We got @count in @nsec, with a target of sample_freq HZ
2656 * the target period becomes:
2659 * period = -------------------
2660 * @nsec * sample_freq
2665 * Reduce accuracy by one bit such that @a and @b converge
2666 * to a similar magnitude.
2668 #define REDUCE_FLS(a, b) \
2670 if (a##_fls > b##_fls) { \
2680 * Reduce accuracy until either term fits in a u64, then proceed with
2681 * the other, so that finally we can do a u64/u64 division.
2683 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
2684 REDUCE_FLS(nsec
, frequency
);
2685 REDUCE_FLS(sec
, count
);
2688 if (count_fls
+ sec_fls
> 64) {
2689 divisor
= nsec
* frequency
;
2691 while (count_fls
+ sec_fls
> 64) {
2692 REDUCE_FLS(count
, sec
);
2696 dividend
= count
* sec
;
2698 dividend
= count
* sec
;
2700 while (nsec_fls
+ frequency_fls
> 64) {
2701 REDUCE_FLS(nsec
, frequency
);
2705 divisor
= nsec
* frequency
;
2711 return div64_u64(dividend
, divisor
);
2714 static DEFINE_PER_CPU(int, perf_throttled_count
);
2715 static DEFINE_PER_CPU(u64
, perf_throttled_seq
);
2717 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
, bool disable
)
2719 struct hw_perf_event
*hwc
= &event
->hw
;
2720 s64 period
, sample_period
;
2723 period
= perf_calculate_period(event
, nsec
, count
);
2725 delta
= (s64
)(period
- hwc
->sample_period
);
2726 delta
= (delta
+ 7) / 8; /* low pass filter */
2728 sample_period
= hwc
->sample_period
+ delta
;
2733 hwc
->sample_period
= sample_period
;
2735 if (local64_read(&hwc
->period_left
) > 8*sample_period
) {
2737 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
2739 local64_set(&hwc
->period_left
, 0);
2742 event
->pmu
->start(event
, PERF_EF_RELOAD
);
2747 * combine freq adjustment with unthrottling to avoid two passes over the
2748 * events. At the same time, make sure, having freq events does not change
2749 * the rate of unthrottling as that would introduce bias.
2751 static void perf_adjust_freq_unthr_context(struct perf_event_context
*ctx
,
2754 struct perf_event
*event
;
2755 struct hw_perf_event
*hwc
;
2756 u64 now
, period
= TICK_NSEC
;
2760 * only need to iterate over all events iff:
2761 * - context have events in frequency mode (needs freq adjust)
2762 * - there are events to unthrottle on this cpu
2764 if (!(ctx
->nr_freq
|| needs_unthr
))
2767 raw_spin_lock(&ctx
->lock
);
2768 perf_pmu_disable(ctx
->pmu
);
2770 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
2771 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
2774 if (!event_filter_match(event
))
2777 perf_pmu_disable(event
->pmu
);
2781 if (hwc
->interrupts
== MAX_INTERRUPTS
) {
2782 hwc
->interrupts
= 0;
2783 perf_log_throttle(event
, 1);
2784 event
->pmu
->start(event
, 0);
2787 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
2791 * stop the event and update event->count
2793 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
2795 now
= local64_read(&event
->count
);
2796 delta
= now
- hwc
->freq_count_stamp
;
2797 hwc
->freq_count_stamp
= now
;
2801 * reload only if value has changed
2802 * we have stopped the event so tell that
2803 * to perf_adjust_period() to avoid stopping it
2807 perf_adjust_period(event
, period
, delta
, false);
2809 event
->pmu
->start(event
, delta
> 0 ? PERF_EF_RELOAD
: 0);
2811 perf_pmu_enable(event
->pmu
);
2814 perf_pmu_enable(ctx
->pmu
);
2815 raw_spin_unlock(&ctx
->lock
);
2819 * Round-robin a context's events:
2821 static void rotate_ctx(struct perf_event_context
*ctx
)
2824 * Rotate the first entry last of non-pinned groups. Rotation might be
2825 * disabled by the inheritance code.
2827 if (!ctx
->rotate_disable
)
2828 list_rotate_left(&ctx
->flexible_groups
);
2832 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2833 * because they're strictly cpu affine and rotate_start is called with IRQs
2834 * disabled, while rotate_context is called from IRQ context.
2836 static int perf_rotate_context(struct perf_cpu_context
*cpuctx
)
2838 struct perf_event_context
*ctx
= NULL
;
2839 int rotate
= 0, remove
= 1;
2841 if (cpuctx
->ctx
.nr_events
) {
2843 if (cpuctx
->ctx
.nr_events
!= cpuctx
->ctx
.nr_active
)
2847 ctx
= cpuctx
->task_ctx
;
2848 if (ctx
&& ctx
->nr_events
) {
2850 if (ctx
->nr_events
!= ctx
->nr_active
)
2857 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
2858 perf_pmu_disable(cpuctx
->ctx
.pmu
);
2860 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
2862 ctx_sched_out(ctx
, cpuctx
, EVENT_FLEXIBLE
);
2864 rotate_ctx(&cpuctx
->ctx
);
2868 perf_event_sched_in(cpuctx
, ctx
, current
);
2870 perf_pmu_enable(cpuctx
->ctx
.pmu
);
2871 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
2874 list_del_init(&cpuctx
->rotation_list
);
2879 #ifdef CONFIG_NO_HZ_FULL
2880 bool perf_event_can_stop_tick(void)
2882 if (atomic_read(&nr_freq_events
) ||
2883 __this_cpu_read(perf_throttled_count
))
2890 void perf_event_task_tick(void)
2892 struct list_head
*head
= &__get_cpu_var(rotation_list
);
2893 struct perf_cpu_context
*cpuctx
, *tmp
;
2894 struct perf_event_context
*ctx
;
2897 WARN_ON(!irqs_disabled());
2899 __this_cpu_inc(perf_throttled_seq
);
2900 throttled
= __this_cpu_xchg(perf_throttled_count
, 0);
2902 list_for_each_entry_safe(cpuctx
, tmp
, head
, rotation_list
) {
2904 perf_adjust_freq_unthr_context(ctx
, throttled
);
2906 ctx
= cpuctx
->task_ctx
;
2908 perf_adjust_freq_unthr_context(ctx
, throttled
);
2912 static int event_enable_on_exec(struct perf_event
*event
,
2913 struct perf_event_context
*ctx
)
2915 if (!event
->attr
.enable_on_exec
)
2918 event
->attr
.enable_on_exec
= 0;
2919 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
2922 __perf_event_mark_enabled(event
);
2928 * Enable all of a task's events that have been marked enable-on-exec.
2929 * This expects task == current.
2931 static void perf_event_enable_on_exec(struct perf_event_context
*ctx
)
2933 struct perf_event
*event
;
2934 unsigned long flags
;
2938 local_irq_save(flags
);
2939 if (!ctx
|| !ctx
->nr_events
)
2943 * We must ctxsw out cgroup events to avoid conflict
2944 * when invoking perf_task_event_sched_in() later on
2945 * in this function. Otherwise we end up trying to
2946 * ctxswin cgroup events which are already scheduled
2949 perf_cgroup_sched_out(current
, NULL
);
2951 raw_spin_lock(&ctx
->lock
);
2952 task_ctx_sched_out(ctx
);
2954 list_for_each_entry(event
, &ctx
->event_list
, event_entry
) {
2955 ret
= event_enable_on_exec(event
, ctx
);
2961 * Unclone this context if we enabled any event.
2966 raw_spin_unlock(&ctx
->lock
);
2969 * Also calls ctxswin for cgroup events, if any:
2971 perf_event_context_sched_in(ctx
, ctx
->task
);
2973 local_irq_restore(flags
);
2977 * Cross CPU call to read the hardware event
2979 static void __perf_event_read(void *info
)
2981 struct perf_event
*event
= info
;
2982 struct perf_event_context
*ctx
= event
->ctx
;
2983 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
2986 * If this is a task context, we need to check whether it is
2987 * the current task context of this cpu. If not it has been
2988 * scheduled out before the smp call arrived. In that case
2989 * event->count would have been updated to a recent sample
2990 * when the event was scheduled out.
2992 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
2995 raw_spin_lock(&ctx
->lock
);
2996 if (ctx
->is_active
) {
2997 update_context_time(ctx
);
2998 update_cgrp_time_from_event(event
);
3000 update_event_times(event
);
3001 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
3002 event
->pmu
->read(event
);
3003 raw_spin_unlock(&ctx
->lock
);
3006 static inline u64
perf_event_count(struct perf_event
*event
)
3008 return local64_read(&event
->count
) + atomic64_read(&event
->child_count
);
3011 static u64
perf_event_read(struct perf_event
*event
)
3014 * If event is enabled and currently active on a CPU, update the
3015 * value in the event structure:
3017 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
3018 smp_call_function_single(event
->oncpu
,
3019 __perf_event_read
, event
, 1);
3020 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
3021 struct perf_event_context
*ctx
= event
->ctx
;
3022 unsigned long flags
;
3024 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
3026 * may read while context is not active
3027 * (e.g., thread is blocked), in that case
3028 * we cannot update context time
3030 if (ctx
->is_active
) {
3031 update_context_time(ctx
);
3032 update_cgrp_time_from_event(event
);
3034 update_event_times(event
);
3035 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
3038 return perf_event_count(event
);
3042 * Initialize the perf_event context in a task_struct:
3044 static void __perf_event_init_context(struct perf_event_context
*ctx
)
3046 raw_spin_lock_init(&ctx
->lock
);
3047 mutex_init(&ctx
->mutex
);
3048 INIT_LIST_HEAD(&ctx
->pinned_groups
);
3049 INIT_LIST_HEAD(&ctx
->flexible_groups
);
3050 INIT_LIST_HEAD(&ctx
->event_list
);
3051 atomic_set(&ctx
->refcount
, 1);
3054 static struct perf_event_context
*
3055 alloc_perf_context(struct pmu
*pmu
, struct task_struct
*task
)
3057 struct perf_event_context
*ctx
;
3059 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
3063 __perf_event_init_context(ctx
);
3066 get_task_struct(task
);
3073 static struct task_struct
*
3074 find_lively_task_by_vpid(pid_t vpid
)
3076 struct task_struct
*task
;
3083 task
= find_task_by_vpid(vpid
);
3085 get_task_struct(task
);
3089 return ERR_PTR(-ESRCH
);
3091 /* Reuse ptrace permission checks for now. */
3093 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
3098 put_task_struct(task
);
3099 return ERR_PTR(err
);
3104 * Returns a matching context with refcount and pincount.
3106 static struct perf_event_context
*
3107 find_get_context(struct pmu
*pmu
, struct task_struct
*task
, int cpu
)
3109 struct perf_event_context
*ctx
;
3110 struct perf_cpu_context
*cpuctx
;
3111 unsigned long flags
;
3115 /* Must be root to operate on a CPU event: */
3116 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
3117 return ERR_PTR(-EACCES
);
3120 * We could be clever and allow to attach a event to an
3121 * offline CPU and activate it when the CPU comes up, but
3124 if (!cpu_online(cpu
))
3125 return ERR_PTR(-ENODEV
);
3127 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
3136 ctxn
= pmu
->task_ctx_nr
;
3141 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
3145 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
3147 ctx
= alloc_perf_context(pmu
, task
);
3153 mutex_lock(&task
->perf_event_mutex
);
3155 * If it has already passed perf_event_exit_task().
3156 * we must see PF_EXITING, it takes this mutex too.
3158 if (task
->flags
& PF_EXITING
)
3160 else if (task
->perf_event_ctxp
[ctxn
])
3165 rcu_assign_pointer(task
->perf_event_ctxp
[ctxn
], ctx
);
3167 mutex_unlock(&task
->perf_event_mutex
);
3169 if (unlikely(err
)) {
3181 return ERR_PTR(err
);
3184 static void perf_event_free_filter(struct perf_event
*event
);
3186 static void free_event_rcu(struct rcu_head
*head
)
3188 struct perf_event
*event
;
3190 event
= container_of(head
, struct perf_event
, rcu_head
);
3192 put_pid_ns(event
->ns
);
3193 perf_event_free_filter(event
);
3197 static void ring_buffer_put(struct ring_buffer
*rb
);
3198 static void ring_buffer_detach(struct perf_event
*event
, struct ring_buffer
*rb
);
3200 static void unaccount_event_cpu(struct perf_event
*event
, int cpu
)
3205 if (has_branch_stack(event
)) {
3206 if (!(event
->attach_state
& PERF_ATTACH_TASK
))
3207 atomic_dec(&per_cpu(perf_branch_stack_events
, cpu
));
3209 if (is_cgroup_event(event
))
3210 atomic_dec(&per_cpu(perf_cgroup_events
, cpu
));
3213 static void unaccount_event(struct perf_event
*event
)
3218 if (event
->attach_state
& PERF_ATTACH_TASK
)
3219 static_key_slow_dec_deferred(&perf_sched_events
);
3220 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
3221 atomic_dec(&nr_mmap_events
);
3222 if (event
->attr
.comm
)
3223 atomic_dec(&nr_comm_events
);
3224 if (event
->attr
.task
)
3225 atomic_dec(&nr_task_events
);
3226 if (event
->attr
.freq
)
3227 atomic_dec(&nr_freq_events
);
3228 if (is_cgroup_event(event
))
3229 static_key_slow_dec_deferred(&perf_sched_events
);
3230 if (has_branch_stack(event
))
3231 static_key_slow_dec_deferred(&perf_sched_events
);
3233 unaccount_event_cpu(event
, event
->cpu
);
3236 static void __free_event(struct perf_event
*event
)
3238 if (!event
->parent
) {
3239 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
)
3240 put_callchain_buffers();
3244 event
->destroy(event
);
3247 put_ctx(event
->ctx
);
3250 module_put(event
->pmu
->module
);
3252 call_rcu(&event
->rcu_head
, free_event_rcu
);
3255 static void _free_event(struct perf_event
*event
)
3257 irq_work_sync(&event
->pending
);
3259 unaccount_event(event
);
3262 struct ring_buffer
*rb
;
3265 * Can happen when we close an event with re-directed output.
3267 * Since we have a 0 refcount, perf_mmap_close() will skip
3268 * over us; possibly making our ring_buffer_put() the last.
3270 mutex_lock(&event
->mmap_mutex
);
3273 rcu_assign_pointer(event
->rb
, NULL
);
3274 ring_buffer_detach(event
, rb
);
3275 ring_buffer_put(rb
); /* could be last */
3277 mutex_unlock(&event
->mmap_mutex
);
3280 if (is_cgroup_event(event
))
3281 perf_detach_cgroup(event
);
3283 __free_event(event
);
3287 * Used to free events which have a known refcount of 1, such as in error paths
3288 * where the event isn't exposed yet and inherited events.
3290 static void free_event(struct perf_event
*event
)
3292 if (WARN(atomic_long_cmpxchg(&event
->refcount
, 1, 0) != 1,
3293 "unexpected event refcount: %ld; ptr=%p\n",
3294 atomic_long_read(&event
->refcount
), event
)) {
3295 /* leak to avoid use-after-free */
3303 * Called when the last reference to the file is gone.
3305 static void put_event(struct perf_event
*event
)
3307 struct perf_event_context
*ctx
= event
->ctx
;
3308 struct task_struct
*owner
;
3310 if (!atomic_long_dec_and_test(&event
->refcount
))
3314 owner
= ACCESS_ONCE(event
->owner
);
3316 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3317 * !owner it means the list deletion is complete and we can indeed
3318 * free this event, otherwise we need to serialize on
3319 * owner->perf_event_mutex.
3321 smp_read_barrier_depends();
3324 * Since delayed_put_task_struct() also drops the last
3325 * task reference we can safely take a new reference
3326 * while holding the rcu_read_lock().
3328 get_task_struct(owner
);
3333 mutex_lock(&owner
->perf_event_mutex
);
3335 * We have to re-check the event->owner field, if it is cleared
3336 * we raced with perf_event_exit_task(), acquiring the mutex
3337 * ensured they're done, and we can proceed with freeing the
3341 list_del_init(&event
->owner_entry
);
3342 mutex_unlock(&owner
->perf_event_mutex
);
3343 put_task_struct(owner
);
3346 WARN_ON_ONCE(ctx
->parent_ctx
);
3348 * There are two ways this annotation is useful:
3350 * 1) there is a lock recursion from perf_event_exit_task
3351 * see the comment there.
3353 * 2) there is a lock-inversion with mmap_sem through
3354 * perf_event_read_group(), which takes faults while
3355 * holding ctx->mutex, however this is called after
3356 * the last filedesc died, so there is no possibility
3357 * to trigger the AB-BA case.
3359 mutex_lock_nested(&ctx
->mutex
, SINGLE_DEPTH_NESTING
);
3360 perf_remove_from_context(event
, true);
3361 mutex_unlock(&ctx
->mutex
);
3366 int perf_event_release_kernel(struct perf_event
*event
)
3371 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
3373 static int perf_release(struct inode
*inode
, struct file
*file
)
3375 put_event(file
->private_data
);
3379 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
3381 struct perf_event
*child
;
3387 mutex_lock(&event
->child_mutex
);
3388 total
+= perf_event_read(event
);
3389 *enabled
+= event
->total_time_enabled
+
3390 atomic64_read(&event
->child_total_time_enabled
);
3391 *running
+= event
->total_time_running
+
3392 atomic64_read(&event
->child_total_time_running
);
3394 list_for_each_entry(child
, &event
->child_list
, child_list
) {
3395 total
+= perf_event_read(child
);
3396 *enabled
+= child
->total_time_enabled
;
3397 *running
+= child
->total_time_running
;
3399 mutex_unlock(&event
->child_mutex
);
3403 EXPORT_SYMBOL_GPL(perf_event_read_value
);
3405 static int perf_event_read_group(struct perf_event
*event
,
3406 u64 read_format
, char __user
*buf
)
3408 struct perf_event
*leader
= event
->group_leader
, *sub
;
3409 int n
= 0, size
= 0, ret
= -EFAULT
;
3410 struct perf_event_context
*ctx
= leader
->ctx
;
3412 u64 count
, enabled
, running
;
3414 mutex_lock(&ctx
->mutex
);
3415 count
= perf_event_read_value(leader
, &enabled
, &running
);
3417 values
[n
++] = 1 + leader
->nr_siblings
;
3418 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3419 values
[n
++] = enabled
;
3420 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3421 values
[n
++] = running
;
3422 values
[n
++] = count
;
3423 if (read_format
& PERF_FORMAT_ID
)
3424 values
[n
++] = primary_event_id(leader
);
3426 size
= n
* sizeof(u64
);
3428 if (copy_to_user(buf
, values
, size
))
3433 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3436 values
[n
++] = perf_event_read_value(sub
, &enabled
, &running
);
3437 if (read_format
& PERF_FORMAT_ID
)
3438 values
[n
++] = primary_event_id(sub
);
3440 size
= n
* sizeof(u64
);
3442 if (copy_to_user(buf
+ ret
, values
, size
)) {
3450 mutex_unlock(&ctx
->mutex
);
3455 static int perf_event_read_one(struct perf_event
*event
,
3456 u64 read_format
, char __user
*buf
)
3458 u64 enabled
, running
;
3462 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
3463 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3464 values
[n
++] = enabled
;
3465 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3466 values
[n
++] = running
;
3467 if (read_format
& PERF_FORMAT_ID
)
3468 values
[n
++] = primary_event_id(event
);
3470 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
3473 return n
* sizeof(u64
);
3477 * Read the performance event - simple non blocking version for now
3480 perf_read_hw(struct perf_event
*event
, char __user
*buf
, size_t count
)
3482 u64 read_format
= event
->attr
.read_format
;
3486 * Return end-of-file for a read on a event that is in
3487 * error state (i.e. because it was pinned but it couldn't be
3488 * scheduled on to the CPU at some point).
3490 if (event
->state
== PERF_EVENT_STATE_ERROR
)
3493 if (count
< event
->read_size
)
3496 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3497 if (read_format
& PERF_FORMAT_GROUP
)
3498 ret
= perf_event_read_group(event
, read_format
, buf
);
3500 ret
= perf_event_read_one(event
, read_format
, buf
);
3506 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
3508 struct perf_event
*event
= file
->private_data
;
3510 return perf_read_hw(event
, buf
, count
);
3513 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
3515 struct perf_event
*event
= file
->private_data
;
3516 struct ring_buffer
*rb
;
3517 unsigned int events
= POLL_HUP
;
3520 * Pin the event->rb by taking event->mmap_mutex; otherwise
3521 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
3523 mutex_lock(&event
->mmap_mutex
);
3526 events
= atomic_xchg(&rb
->poll
, 0);
3527 mutex_unlock(&event
->mmap_mutex
);
3529 poll_wait(file
, &event
->waitq
, wait
);
3534 static void perf_event_reset(struct perf_event
*event
)
3536 (void)perf_event_read(event
);
3537 local64_set(&event
->count
, 0);
3538 perf_event_update_userpage(event
);
3542 * Holding the top-level event's child_mutex means that any
3543 * descendant process that has inherited this event will block
3544 * in sync_child_event if it goes to exit, thus satisfying the
3545 * task existence requirements of perf_event_enable/disable.
3547 static void perf_event_for_each_child(struct perf_event
*event
,
3548 void (*func
)(struct perf_event
*))
3550 struct perf_event
*child
;
3552 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3553 mutex_lock(&event
->child_mutex
);
3555 list_for_each_entry(child
, &event
->child_list
, child_list
)
3557 mutex_unlock(&event
->child_mutex
);
3560 static void perf_event_for_each(struct perf_event
*event
,
3561 void (*func
)(struct perf_event
*))
3563 struct perf_event_context
*ctx
= event
->ctx
;
3564 struct perf_event
*sibling
;
3566 WARN_ON_ONCE(ctx
->parent_ctx
);
3567 mutex_lock(&ctx
->mutex
);
3568 event
= event
->group_leader
;
3570 perf_event_for_each_child(event
, func
);
3571 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
3572 perf_event_for_each_child(sibling
, func
);
3573 mutex_unlock(&ctx
->mutex
);
3576 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
3578 struct perf_event_context
*ctx
= event
->ctx
;
3579 int ret
= 0, active
;
3582 if (!is_sampling_event(event
))
3585 if (copy_from_user(&value
, arg
, sizeof(value
)))
3591 raw_spin_lock_irq(&ctx
->lock
);
3592 if (event
->attr
.freq
) {
3593 if (value
> sysctl_perf_event_sample_rate
) {
3598 event
->attr
.sample_freq
= value
;
3600 event
->attr
.sample_period
= value
;
3601 event
->hw
.sample_period
= value
;
3604 active
= (event
->state
== PERF_EVENT_STATE_ACTIVE
);
3606 perf_pmu_disable(ctx
->pmu
);
3607 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
3610 local64_set(&event
->hw
.period_left
, 0);
3613 event
->pmu
->start(event
, PERF_EF_RELOAD
);
3614 perf_pmu_enable(ctx
->pmu
);
3618 raw_spin_unlock_irq(&ctx
->lock
);
3623 static const struct file_operations perf_fops
;
3625 static inline int perf_fget_light(int fd
, struct fd
*p
)
3627 struct fd f
= fdget(fd
);
3631 if (f
.file
->f_op
!= &perf_fops
) {
3639 static int perf_event_set_output(struct perf_event
*event
,
3640 struct perf_event
*output_event
);
3641 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
3643 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
3645 struct perf_event
*event
= file
->private_data
;
3646 void (*func
)(struct perf_event
*);
3650 case PERF_EVENT_IOC_ENABLE
:
3651 func
= perf_event_enable
;
3653 case PERF_EVENT_IOC_DISABLE
:
3654 func
= perf_event_disable
;
3656 case PERF_EVENT_IOC_RESET
:
3657 func
= perf_event_reset
;
3660 case PERF_EVENT_IOC_REFRESH
:
3661 return perf_event_refresh(event
, arg
);
3663 case PERF_EVENT_IOC_PERIOD
:
3664 return perf_event_period(event
, (u64 __user
*)arg
);
3666 case PERF_EVENT_IOC_ID
:
3668 u64 id
= primary_event_id(event
);
3670 if (copy_to_user((void __user
*)arg
, &id
, sizeof(id
)))
3675 case PERF_EVENT_IOC_SET_OUTPUT
:
3679 struct perf_event
*output_event
;
3681 ret
= perf_fget_light(arg
, &output
);
3684 output_event
= output
.file
->private_data
;
3685 ret
= perf_event_set_output(event
, output_event
);
3688 ret
= perf_event_set_output(event
, NULL
);
3693 case PERF_EVENT_IOC_SET_FILTER
:
3694 return perf_event_set_filter(event
, (void __user
*)arg
);
3700 if (flags
& PERF_IOC_FLAG_GROUP
)
3701 perf_event_for_each(event
, func
);
3703 perf_event_for_each_child(event
, func
);
3708 int perf_event_task_enable(void)
3710 struct perf_event
*event
;
3712 mutex_lock(¤t
->perf_event_mutex
);
3713 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
3714 perf_event_for_each_child(event
, perf_event_enable
);
3715 mutex_unlock(¤t
->perf_event_mutex
);
3720 int perf_event_task_disable(void)
3722 struct perf_event
*event
;
3724 mutex_lock(¤t
->perf_event_mutex
);
3725 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
3726 perf_event_for_each_child(event
, perf_event_disable
);
3727 mutex_unlock(¤t
->perf_event_mutex
);
3732 static int perf_event_index(struct perf_event
*event
)
3734 if (event
->hw
.state
& PERF_HES_STOPPED
)
3737 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
3740 return event
->pmu
->event_idx(event
);
3743 static void calc_timer_values(struct perf_event
*event
,
3750 *now
= perf_clock();
3751 ctx_time
= event
->shadow_ctx_time
+ *now
;
3752 *enabled
= ctx_time
- event
->tstamp_enabled
;
3753 *running
= ctx_time
- event
->tstamp_running
;
3756 static void perf_event_init_userpage(struct perf_event
*event
)
3758 struct perf_event_mmap_page
*userpg
;
3759 struct ring_buffer
*rb
;
3762 rb
= rcu_dereference(event
->rb
);
3766 userpg
= rb
->user_page
;
3768 /* Allow new userspace to detect that bit 0 is deprecated */
3769 userpg
->cap_bit0_is_deprecated
= 1;
3770 userpg
->size
= offsetof(struct perf_event_mmap_page
, __reserved
);
3776 void __weak
arch_perf_update_userpage(struct perf_event_mmap_page
*userpg
, u64 now
)
3781 * Callers need to ensure there can be no nesting of this function, otherwise
3782 * the seqlock logic goes bad. We can not serialize this because the arch
3783 * code calls this from NMI context.
3785 void perf_event_update_userpage(struct perf_event
*event
)
3787 struct perf_event_mmap_page
*userpg
;
3788 struct ring_buffer
*rb
;
3789 u64 enabled
, running
, now
;
3792 rb
= rcu_dereference(event
->rb
);
3797 * compute total_time_enabled, total_time_running
3798 * based on snapshot values taken when the event
3799 * was last scheduled in.
3801 * we cannot simply called update_context_time()
3802 * because of locking issue as we can be called in
3805 calc_timer_values(event
, &now
, &enabled
, &running
);
3807 userpg
= rb
->user_page
;
3809 * Disable preemption so as to not let the corresponding user-space
3810 * spin too long if we get preempted.
3815 userpg
->index
= perf_event_index(event
);
3816 userpg
->offset
= perf_event_count(event
);
3818 userpg
->offset
-= local64_read(&event
->hw
.prev_count
);
3820 userpg
->time_enabled
= enabled
+
3821 atomic64_read(&event
->child_total_time_enabled
);
3823 userpg
->time_running
= running
+
3824 atomic64_read(&event
->child_total_time_running
);
3826 arch_perf_update_userpage(userpg
, now
);
3835 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
3837 struct perf_event
*event
= vma
->vm_file
->private_data
;
3838 struct ring_buffer
*rb
;
3839 int ret
= VM_FAULT_SIGBUS
;
3841 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
3842 if (vmf
->pgoff
== 0)
3848 rb
= rcu_dereference(event
->rb
);
3852 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
3855 vmf
->page
= perf_mmap_to_page(rb
, vmf
->pgoff
);
3859 get_page(vmf
->page
);
3860 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
3861 vmf
->page
->index
= vmf
->pgoff
;
3870 static void ring_buffer_attach(struct perf_event
*event
,
3871 struct ring_buffer
*rb
)
3873 unsigned long flags
;
3875 if (!list_empty(&event
->rb_entry
))
3878 spin_lock_irqsave(&rb
->event_lock
, flags
);
3879 if (list_empty(&event
->rb_entry
))
3880 list_add(&event
->rb_entry
, &rb
->event_list
);
3881 spin_unlock_irqrestore(&rb
->event_lock
, flags
);
3884 static void ring_buffer_detach(struct perf_event
*event
, struct ring_buffer
*rb
)
3886 unsigned long flags
;
3888 if (list_empty(&event
->rb_entry
))
3891 spin_lock_irqsave(&rb
->event_lock
, flags
);
3892 list_del_init(&event
->rb_entry
);
3893 wake_up_all(&event
->waitq
);
3894 spin_unlock_irqrestore(&rb
->event_lock
, flags
);
3897 static void ring_buffer_wakeup(struct perf_event
*event
)
3899 struct ring_buffer
*rb
;
3902 rb
= rcu_dereference(event
->rb
);
3904 list_for_each_entry_rcu(event
, &rb
->event_list
, rb_entry
)
3905 wake_up_all(&event
->waitq
);
3910 static void rb_free_rcu(struct rcu_head
*rcu_head
)
3912 struct ring_buffer
*rb
;
3914 rb
= container_of(rcu_head
, struct ring_buffer
, rcu_head
);
3918 static struct ring_buffer
*ring_buffer_get(struct perf_event
*event
)
3920 struct ring_buffer
*rb
;
3923 rb
= rcu_dereference(event
->rb
);
3925 if (!atomic_inc_not_zero(&rb
->refcount
))
3933 static void ring_buffer_put(struct ring_buffer
*rb
)
3935 if (!atomic_dec_and_test(&rb
->refcount
))
3938 WARN_ON_ONCE(!list_empty(&rb
->event_list
));
3940 call_rcu(&rb
->rcu_head
, rb_free_rcu
);
3943 static void perf_mmap_open(struct vm_area_struct
*vma
)
3945 struct perf_event
*event
= vma
->vm_file
->private_data
;
3947 atomic_inc(&event
->mmap_count
);
3948 atomic_inc(&event
->rb
->mmap_count
);
3952 * A buffer can be mmap()ed multiple times; either directly through the same
3953 * event, or through other events by use of perf_event_set_output().
3955 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3956 * the buffer here, where we still have a VM context. This means we need
3957 * to detach all events redirecting to us.
3959 static void perf_mmap_close(struct vm_area_struct
*vma
)
3961 struct perf_event
*event
= vma
->vm_file
->private_data
;
3963 struct ring_buffer
*rb
= event
->rb
;
3964 struct user_struct
*mmap_user
= rb
->mmap_user
;
3965 int mmap_locked
= rb
->mmap_locked
;
3966 unsigned long size
= perf_data_size(rb
);
3968 atomic_dec(&rb
->mmap_count
);
3970 if (!atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
))
3973 /* Detach current event from the buffer. */
3974 rcu_assign_pointer(event
->rb
, NULL
);
3975 ring_buffer_detach(event
, rb
);
3976 mutex_unlock(&event
->mmap_mutex
);
3978 /* If there's still other mmap()s of this buffer, we're done. */
3979 if (atomic_read(&rb
->mmap_count
)) {
3980 ring_buffer_put(rb
); /* can't be last */
3985 * No other mmap()s, detach from all other events that might redirect
3986 * into the now unreachable buffer. Somewhat complicated by the
3987 * fact that rb::event_lock otherwise nests inside mmap_mutex.
3991 list_for_each_entry_rcu(event
, &rb
->event_list
, rb_entry
) {
3992 if (!atomic_long_inc_not_zero(&event
->refcount
)) {
3994 * This event is en-route to free_event() which will
3995 * detach it and remove it from the list.
4001 mutex_lock(&event
->mmap_mutex
);
4003 * Check we didn't race with perf_event_set_output() which can
4004 * swizzle the rb from under us while we were waiting to
4005 * acquire mmap_mutex.
4007 * If we find a different rb; ignore this event, a next
4008 * iteration will no longer find it on the list. We have to
4009 * still restart the iteration to make sure we're not now
4010 * iterating the wrong list.
4012 if (event
->rb
== rb
) {
4013 rcu_assign_pointer(event
->rb
, NULL
);
4014 ring_buffer_detach(event
, rb
);
4015 ring_buffer_put(rb
); /* can't be last, we still have one */
4017 mutex_unlock(&event
->mmap_mutex
);
4021 * Restart the iteration; either we're on the wrong list or
4022 * destroyed its integrity by doing a deletion.
4029 * It could be there's still a few 0-ref events on the list; they'll
4030 * get cleaned up by free_event() -- they'll also still have their
4031 * ref on the rb and will free it whenever they are done with it.
4033 * Aside from that, this buffer is 'fully' detached and unmapped,
4034 * undo the VM accounting.
4037 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &mmap_user
->locked_vm
);
4038 vma
->vm_mm
->pinned_vm
-= mmap_locked
;
4039 free_uid(mmap_user
);
4041 ring_buffer_put(rb
); /* could be last */
4044 static const struct vm_operations_struct perf_mmap_vmops
= {
4045 .open
= perf_mmap_open
,
4046 .close
= perf_mmap_close
,
4047 .fault
= perf_mmap_fault
,
4048 .page_mkwrite
= perf_mmap_fault
,
4051 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
4053 struct perf_event
*event
= file
->private_data
;
4054 unsigned long user_locked
, user_lock_limit
;
4055 struct user_struct
*user
= current_user();
4056 unsigned long locked
, lock_limit
;
4057 struct ring_buffer
*rb
;
4058 unsigned long vma_size
;
4059 unsigned long nr_pages
;
4060 long user_extra
, extra
;
4061 int ret
= 0, flags
= 0;
4064 * Don't allow mmap() of inherited per-task counters. This would
4065 * create a performance issue due to all children writing to the
4068 if (event
->cpu
== -1 && event
->attr
.inherit
)
4071 if (!(vma
->vm_flags
& VM_SHARED
))
4074 vma_size
= vma
->vm_end
- vma
->vm_start
;
4075 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
4078 * If we have rb pages ensure they're a power-of-two number, so we
4079 * can do bitmasks instead of modulo.
4081 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
4084 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
4087 if (vma
->vm_pgoff
!= 0)
4090 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
4092 mutex_lock(&event
->mmap_mutex
);
4094 if (event
->rb
->nr_pages
!= nr_pages
) {
4099 if (!atomic_inc_not_zero(&event
->rb
->mmap_count
)) {
4101 * Raced against perf_mmap_close() through
4102 * perf_event_set_output(). Try again, hope for better
4105 mutex_unlock(&event
->mmap_mutex
);
4112 user_extra
= nr_pages
+ 1;
4113 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
4116 * Increase the limit linearly with more CPUs:
4118 user_lock_limit
*= num_online_cpus();
4120 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
4123 if (user_locked
> user_lock_limit
)
4124 extra
= user_locked
- user_lock_limit
;
4126 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
4127 lock_limit
>>= PAGE_SHIFT
;
4128 locked
= vma
->vm_mm
->pinned_vm
+ extra
;
4130 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
4131 !capable(CAP_IPC_LOCK
)) {
4138 if (vma
->vm_flags
& VM_WRITE
)
4139 flags
|= RING_BUFFER_WRITABLE
;
4141 rb
= rb_alloc(nr_pages
,
4142 event
->attr
.watermark
? event
->attr
.wakeup_watermark
: 0,
4150 atomic_set(&rb
->mmap_count
, 1);
4151 rb
->mmap_locked
= extra
;
4152 rb
->mmap_user
= get_current_user();
4154 atomic_long_add(user_extra
, &user
->locked_vm
);
4155 vma
->vm_mm
->pinned_vm
+= extra
;
4157 ring_buffer_attach(event
, rb
);
4158 rcu_assign_pointer(event
->rb
, rb
);
4160 perf_event_init_userpage(event
);
4161 perf_event_update_userpage(event
);
4165 atomic_inc(&event
->mmap_count
);
4166 mutex_unlock(&event
->mmap_mutex
);
4169 * Since pinned accounting is per vm we cannot allow fork() to copy our
4172 vma
->vm_flags
|= VM_DONTCOPY
| VM_DONTEXPAND
| VM_DONTDUMP
;
4173 vma
->vm_ops
= &perf_mmap_vmops
;
4178 static int perf_fasync(int fd
, struct file
*filp
, int on
)
4180 struct inode
*inode
= file_inode(filp
);
4181 struct perf_event
*event
= filp
->private_data
;
4184 mutex_lock(&inode
->i_mutex
);
4185 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
4186 mutex_unlock(&inode
->i_mutex
);
4194 static const struct file_operations perf_fops
= {
4195 .llseek
= no_llseek
,
4196 .release
= perf_release
,
4199 .unlocked_ioctl
= perf_ioctl
,
4200 .compat_ioctl
= perf_ioctl
,
4202 .fasync
= perf_fasync
,
4208 * If there's data, ensure we set the poll() state and publish everything
4209 * to user-space before waking everybody up.
4212 void perf_event_wakeup(struct perf_event
*event
)
4214 ring_buffer_wakeup(event
);
4216 if (event
->pending_kill
) {
4217 kill_fasync(&event
->fasync
, SIGIO
, event
->pending_kill
);
4218 event
->pending_kill
= 0;
4222 static void perf_pending_event(struct irq_work
*entry
)
4224 struct perf_event
*event
= container_of(entry
,
4225 struct perf_event
, pending
);
4227 if (event
->pending_disable
) {
4228 event
->pending_disable
= 0;
4229 __perf_event_disable(event
);
4232 if (event
->pending_wakeup
) {
4233 event
->pending_wakeup
= 0;
4234 perf_event_wakeup(event
);
4239 * We assume there is only KVM supporting the callbacks.
4240 * Later on, we might change it to a list if there is
4241 * another virtualization implementation supporting the callbacks.
4243 struct perf_guest_info_callbacks
*perf_guest_cbs
;
4245 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
4247 perf_guest_cbs
= cbs
;
4250 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks
);
4252 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
4254 perf_guest_cbs
= NULL
;
4257 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks
);
4260 perf_output_sample_regs(struct perf_output_handle
*handle
,
4261 struct pt_regs
*regs
, u64 mask
)
4265 for_each_set_bit(bit
, (const unsigned long *) &mask
,
4266 sizeof(mask
) * BITS_PER_BYTE
) {
4269 val
= perf_reg_value(regs
, bit
);
4270 perf_output_put(handle
, val
);
4274 static void perf_sample_regs_user(struct perf_regs_user
*regs_user
,
4275 struct pt_regs
*regs
)
4277 if (!user_mode(regs
)) {
4279 regs
= task_pt_regs(current
);
4285 regs_user
->regs
= regs
;
4286 regs_user
->abi
= perf_reg_abi(current
);
4291 * Get remaining task size from user stack pointer.
4293 * It'd be better to take stack vma map and limit this more
4294 * precisly, but there's no way to get it safely under interrupt,
4295 * so using TASK_SIZE as limit.
4297 static u64
perf_ustack_task_size(struct pt_regs
*regs
)
4299 unsigned long addr
= perf_user_stack_pointer(regs
);
4301 if (!addr
|| addr
>= TASK_SIZE
)
4304 return TASK_SIZE
- addr
;
4308 perf_sample_ustack_size(u16 stack_size
, u16 header_size
,
4309 struct pt_regs
*regs
)
4313 /* No regs, no stack pointer, no dump. */
4318 * Check if we fit in with the requested stack size into the:
4320 * If we don't, we limit the size to the TASK_SIZE.
4322 * - remaining sample size
4323 * If we don't, we customize the stack size to
4324 * fit in to the remaining sample size.
4327 task_size
= min((u64
) USHRT_MAX
, perf_ustack_task_size(regs
));
4328 stack_size
= min(stack_size
, (u16
) task_size
);
4330 /* Current header size plus static size and dynamic size. */
4331 header_size
+= 2 * sizeof(u64
);
4333 /* Do we fit in with the current stack dump size? */
4334 if ((u16
) (header_size
+ stack_size
) < header_size
) {
4336 * If we overflow the maximum size for the sample,
4337 * we customize the stack dump size to fit in.
4339 stack_size
= USHRT_MAX
- header_size
- sizeof(u64
);
4340 stack_size
= round_up(stack_size
, sizeof(u64
));
4347 perf_output_sample_ustack(struct perf_output_handle
*handle
, u64 dump_size
,
4348 struct pt_regs
*regs
)
4350 /* Case of a kernel thread, nothing to dump */
4353 perf_output_put(handle
, size
);
4362 * - the size requested by user or the best one we can fit
4363 * in to the sample max size
4365 * - user stack dump data
4367 * - the actual dumped size
4371 perf_output_put(handle
, dump_size
);
4374 sp
= perf_user_stack_pointer(regs
);
4375 rem
= __output_copy_user(handle
, (void *) sp
, dump_size
);
4376 dyn_size
= dump_size
- rem
;
4378 perf_output_skip(handle
, rem
);
4381 perf_output_put(handle
, dyn_size
);
4385 static void __perf_event_header__init_id(struct perf_event_header
*header
,
4386 struct perf_sample_data
*data
,
4387 struct perf_event
*event
)
4389 u64 sample_type
= event
->attr
.sample_type
;
4391 data
->type
= sample_type
;
4392 header
->size
+= event
->id_header_size
;
4394 if (sample_type
& PERF_SAMPLE_TID
) {
4395 /* namespace issues */
4396 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
4397 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
4400 if (sample_type
& PERF_SAMPLE_TIME
)
4401 data
->time
= perf_clock();
4403 if (sample_type
& (PERF_SAMPLE_ID
| PERF_SAMPLE_IDENTIFIER
))
4404 data
->id
= primary_event_id(event
);
4406 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4407 data
->stream_id
= event
->id
;
4409 if (sample_type
& PERF_SAMPLE_CPU
) {
4410 data
->cpu_entry
.cpu
= raw_smp_processor_id();
4411 data
->cpu_entry
.reserved
= 0;
4415 void perf_event_header__init_id(struct perf_event_header
*header
,
4416 struct perf_sample_data
*data
,
4417 struct perf_event
*event
)
4419 if (event
->attr
.sample_id_all
)
4420 __perf_event_header__init_id(header
, data
, event
);
4423 static void __perf_event__output_id_sample(struct perf_output_handle
*handle
,
4424 struct perf_sample_data
*data
)
4426 u64 sample_type
= data
->type
;
4428 if (sample_type
& PERF_SAMPLE_TID
)
4429 perf_output_put(handle
, data
->tid_entry
);
4431 if (sample_type
& PERF_SAMPLE_TIME
)
4432 perf_output_put(handle
, data
->time
);
4434 if (sample_type
& PERF_SAMPLE_ID
)
4435 perf_output_put(handle
, data
->id
);
4437 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4438 perf_output_put(handle
, data
->stream_id
);
4440 if (sample_type
& PERF_SAMPLE_CPU
)
4441 perf_output_put(handle
, data
->cpu_entry
);
4443 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
4444 perf_output_put(handle
, data
->id
);
4447 void perf_event__output_id_sample(struct perf_event
*event
,
4448 struct perf_output_handle
*handle
,
4449 struct perf_sample_data
*sample
)
4451 if (event
->attr
.sample_id_all
)
4452 __perf_event__output_id_sample(handle
, sample
);
4455 static void perf_output_read_one(struct perf_output_handle
*handle
,
4456 struct perf_event
*event
,
4457 u64 enabled
, u64 running
)
4459 u64 read_format
= event
->attr
.read_format
;
4463 values
[n
++] = perf_event_count(event
);
4464 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
4465 values
[n
++] = enabled
+
4466 atomic64_read(&event
->child_total_time_enabled
);
4468 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
4469 values
[n
++] = running
+
4470 atomic64_read(&event
->child_total_time_running
);
4472 if (read_format
& PERF_FORMAT_ID
)
4473 values
[n
++] = primary_event_id(event
);
4475 __output_copy(handle
, values
, n
* sizeof(u64
));
4479 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4481 static void perf_output_read_group(struct perf_output_handle
*handle
,
4482 struct perf_event
*event
,
4483 u64 enabled
, u64 running
)
4485 struct perf_event
*leader
= event
->group_leader
, *sub
;
4486 u64 read_format
= event
->attr
.read_format
;
4490 values
[n
++] = 1 + leader
->nr_siblings
;
4492 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
4493 values
[n
++] = enabled
;
4495 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
4496 values
[n
++] = running
;
4498 if (leader
!= event
)
4499 leader
->pmu
->read(leader
);
4501 values
[n
++] = perf_event_count(leader
);
4502 if (read_format
& PERF_FORMAT_ID
)
4503 values
[n
++] = primary_event_id(leader
);
4505 __output_copy(handle
, values
, n
* sizeof(u64
));
4507 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
4510 if ((sub
!= event
) &&
4511 (sub
->state
== PERF_EVENT_STATE_ACTIVE
))
4512 sub
->pmu
->read(sub
);
4514 values
[n
++] = perf_event_count(sub
);
4515 if (read_format
& PERF_FORMAT_ID
)
4516 values
[n
++] = primary_event_id(sub
);
4518 __output_copy(handle
, values
, n
* sizeof(u64
));
4522 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4523 PERF_FORMAT_TOTAL_TIME_RUNNING)
4525 static void perf_output_read(struct perf_output_handle
*handle
,
4526 struct perf_event
*event
)
4528 u64 enabled
= 0, running
= 0, now
;
4529 u64 read_format
= event
->attr
.read_format
;
4532 * compute total_time_enabled, total_time_running
4533 * based on snapshot values taken when the event
4534 * was last scheduled in.
4536 * we cannot simply called update_context_time()
4537 * because of locking issue as we are called in
4540 if (read_format
& PERF_FORMAT_TOTAL_TIMES
)
4541 calc_timer_values(event
, &now
, &enabled
, &running
);
4543 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
4544 perf_output_read_group(handle
, event
, enabled
, running
);
4546 perf_output_read_one(handle
, event
, enabled
, running
);
4549 void perf_output_sample(struct perf_output_handle
*handle
,
4550 struct perf_event_header
*header
,
4551 struct perf_sample_data
*data
,
4552 struct perf_event
*event
)
4554 u64 sample_type
= data
->type
;
4556 perf_output_put(handle
, *header
);
4558 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
4559 perf_output_put(handle
, data
->id
);
4561 if (sample_type
& PERF_SAMPLE_IP
)
4562 perf_output_put(handle
, data
->ip
);
4564 if (sample_type
& PERF_SAMPLE_TID
)
4565 perf_output_put(handle
, data
->tid_entry
);
4567 if (sample_type
& PERF_SAMPLE_TIME
)
4568 perf_output_put(handle
, data
->time
);
4570 if (sample_type
& PERF_SAMPLE_ADDR
)
4571 perf_output_put(handle
, data
->addr
);
4573 if (sample_type
& PERF_SAMPLE_ID
)
4574 perf_output_put(handle
, data
->id
);
4576 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4577 perf_output_put(handle
, data
->stream_id
);
4579 if (sample_type
& PERF_SAMPLE_CPU
)
4580 perf_output_put(handle
, data
->cpu_entry
);
4582 if (sample_type
& PERF_SAMPLE_PERIOD
)
4583 perf_output_put(handle
, data
->period
);
4585 if (sample_type
& PERF_SAMPLE_READ
)
4586 perf_output_read(handle
, event
);
4588 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
4589 if (data
->callchain
) {
4592 if (data
->callchain
)
4593 size
+= data
->callchain
->nr
;
4595 size
*= sizeof(u64
);
4597 __output_copy(handle
, data
->callchain
, size
);
4600 perf_output_put(handle
, nr
);
4604 if (sample_type
& PERF_SAMPLE_RAW
) {
4606 perf_output_put(handle
, data
->raw
->size
);
4607 __output_copy(handle
, data
->raw
->data
,
4614 .size
= sizeof(u32
),
4617 perf_output_put(handle
, raw
);
4621 if (sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
4622 if (data
->br_stack
) {
4625 size
= data
->br_stack
->nr
4626 * sizeof(struct perf_branch_entry
);
4628 perf_output_put(handle
, data
->br_stack
->nr
);
4629 perf_output_copy(handle
, data
->br_stack
->entries
, size
);
4632 * we always store at least the value of nr
4635 perf_output_put(handle
, nr
);
4639 if (sample_type
& PERF_SAMPLE_REGS_USER
) {
4640 u64 abi
= data
->regs_user
.abi
;
4643 * If there are no regs to dump, notice it through
4644 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4646 perf_output_put(handle
, abi
);
4649 u64 mask
= event
->attr
.sample_regs_user
;
4650 perf_output_sample_regs(handle
,
4651 data
->regs_user
.regs
,
4656 if (sample_type
& PERF_SAMPLE_STACK_USER
) {
4657 perf_output_sample_ustack(handle
,
4658 data
->stack_user_size
,
4659 data
->regs_user
.regs
);
4662 if (sample_type
& PERF_SAMPLE_WEIGHT
)
4663 perf_output_put(handle
, data
->weight
);
4665 if (sample_type
& PERF_SAMPLE_DATA_SRC
)
4666 perf_output_put(handle
, data
->data_src
.val
);
4668 if (sample_type
& PERF_SAMPLE_TRANSACTION
)
4669 perf_output_put(handle
, data
->txn
);
4671 if (!event
->attr
.watermark
) {
4672 int wakeup_events
= event
->attr
.wakeup_events
;
4674 if (wakeup_events
) {
4675 struct ring_buffer
*rb
= handle
->rb
;
4676 int events
= local_inc_return(&rb
->events
);
4678 if (events
>= wakeup_events
) {
4679 local_sub(wakeup_events
, &rb
->events
);
4680 local_inc(&rb
->wakeup
);
4686 void perf_prepare_sample(struct perf_event_header
*header
,
4687 struct perf_sample_data
*data
,
4688 struct perf_event
*event
,
4689 struct pt_regs
*regs
)
4691 u64 sample_type
= event
->attr
.sample_type
;
4693 header
->type
= PERF_RECORD_SAMPLE
;
4694 header
->size
= sizeof(*header
) + event
->header_size
;
4697 header
->misc
|= perf_misc_flags(regs
);
4699 __perf_event_header__init_id(header
, data
, event
);
4701 if (sample_type
& PERF_SAMPLE_IP
)
4702 data
->ip
= perf_instruction_pointer(regs
);
4704 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
4707 data
->callchain
= perf_callchain(event
, regs
);
4709 if (data
->callchain
)
4710 size
+= data
->callchain
->nr
;
4712 header
->size
+= size
* sizeof(u64
);
4715 if (sample_type
& PERF_SAMPLE_RAW
) {
4716 int size
= sizeof(u32
);
4719 size
+= data
->raw
->size
;
4721 size
+= sizeof(u32
);
4723 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
4724 header
->size
+= size
;
4727 if (sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
4728 int size
= sizeof(u64
); /* nr */
4729 if (data
->br_stack
) {
4730 size
+= data
->br_stack
->nr
4731 * sizeof(struct perf_branch_entry
);
4733 header
->size
+= size
;
4736 if (sample_type
& PERF_SAMPLE_REGS_USER
) {
4737 /* regs dump ABI info */
4738 int size
= sizeof(u64
);
4740 perf_sample_regs_user(&data
->regs_user
, regs
);
4742 if (data
->regs_user
.regs
) {
4743 u64 mask
= event
->attr
.sample_regs_user
;
4744 size
+= hweight64(mask
) * sizeof(u64
);
4747 header
->size
+= size
;
4750 if (sample_type
& PERF_SAMPLE_STACK_USER
) {
4752 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4753 * processed as the last one or have additional check added
4754 * in case new sample type is added, because we could eat
4755 * up the rest of the sample size.
4757 struct perf_regs_user
*uregs
= &data
->regs_user
;
4758 u16 stack_size
= event
->attr
.sample_stack_user
;
4759 u16 size
= sizeof(u64
);
4762 perf_sample_regs_user(uregs
, regs
);
4764 stack_size
= perf_sample_ustack_size(stack_size
, header
->size
,
4768 * If there is something to dump, add space for the dump
4769 * itself and for the field that tells the dynamic size,
4770 * which is how many have been actually dumped.
4773 size
+= sizeof(u64
) + stack_size
;
4775 data
->stack_user_size
= stack_size
;
4776 header
->size
+= size
;
4780 static void perf_event_output(struct perf_event
*event
,
4781 struct perf_sample_data
*data
,
4782 struct pt_regs
*regs
)
4784 struct perf_output_handle handle
;
4785 struct perf_event_header header
;
4787 /* protect the callchain buffers */
4790 perf_prepare_sample(&header
, data
, event
, regs
);
4792 if (perf_output_begin(&handle
, event
, header
.size
))
4795 perf_output_sample(&handle
, &header
, data
, event
);
4797 perf_output_end(&handle
);
4807 struct perf_read_event
{
4808 struct perf_event_header header
;
4815 perf_event_read_event(struct perf_event
*event
,
4816 struct task_struct
*task
)
4818 struct perf_output_handle handle
;
4819 struct perf_sample_data sample
;
4820 struct perf_read_event read_event
= {
4822 .type
= PERF_RECORD_READ
,
4824 .size
= sizeof(read_event
) + event
->read_size
,
4826 .pid
= perf_event_pid(event
, task
),
4827 .tid
= perf_event_tid(event
, task
),
4831 perf_event_header__init_id(&read_event
.header
, &sample
, event
);
4832 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
);
4836 perf_output_put(&handle
, read_event
);
4837 perf_output_read(&handle
, event
);
4838 perf_event__output_id_sample(event
, &handle
, &sample
);
4840 perf_output_end(&handle
);
4843 typedef void (perf_event_aux_output_cb
)(struct perf_event
*event
, void *data
);
4846 perf_event_aux_ctx(struct perf_event_context
*ctx
,
4847 perf_event_aux_output_cb output
,
4850 struct perf_event
*event
;
4852 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
4853 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
4855 if (!event_filter_match(event
))
4857 output(event
, data
);
4862 perf_event_aux(perf_event_aux_output_cb output
, void *data
,
4863 struct perf_event_context
*task_ctx
)
4865 struct perf_cpu_context
*cpuctx
;
4866 struct perf_event_context
*ctx
;
4871 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
4872 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
4873 if (cpuctx
->unique_pmu
!= pmu
)
4875 perf_event_aux_ctx(&cpuctx
->ctx
, output
, data
);
4878 ctxn
= pmu
->task_ctx_nr
;
4881 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
4883 perf_event_aux_ctx(ctx
, output
, data
);
4885 put_cpu_ptr(pmu
->pmu_cpu_context
);
4890 perf_event_aux_ctx(task_ctx
, output
, data
);
4897 * task tracking -- fork/exit
4899 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
4902 struct perf_task_event
{
4903 struct task_struct
*task
;
4904 struct perf_event_context
*task_ctx
;
4907 struct perf_event_header header
;
4917 static int perf_event_task_match(struct perf_event
*event
)
4919 return event
->attr
.comm
|| event
->attr
.mmap
||
4920 event
->attr
.mmap2
|| event
->attr
.mmap_data
||
4924 static void perf_event_task_output(struct perf_event
*event
,
4927 struct perf_task_event
*task_event
= data
;
4928 struct perf_output_handle handle
;
4929 struct perf_sample_data sample
;
4930 struct task_struct
*task
= task_event
->task
;
4931 int ret
, size
= task_event
->event_id
.header
.size
;
4933 if (!perf_event_task_match(event
))
4936 perf_event_header__init_id(&task_event
->event_id
.header
, &sample
, event
);
4938 ret
= perf_output_begin(&handle
, event
,
4939 task_event
->event_id
.header
.size
);
4943 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
4944 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
4946 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
4947 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
4949 perf_output_put(&handle
, task_event
->event_id
);
4951 perf_event__output_id_sample(event
, &handle
, &sample
);
4953 perf_output_end(&handle
);
4955 task_event
->event_id
.header
.size
= size
;
4958 static void perf_event_task(struct task_struct
*task
,
4959 struct perf_event_context
*task_ctx
,
4962 struct perf_task_event task_event
;
4964 if (!atomic_read(&nr_comm_events
) &&
4965 !atomic_read(&nr_mmap_events
) &&
4966 !atomic_read(&nr_task_events
))
4969 task_event
= (struct perf_task_event
){
4971 .task_ctx
= task_ctx
,
4974 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
4976 .size
= sizeof(task_event
.event_id
),
4982 .time
= perf_clock(),
4986 perf_event_aux(perf_event_task_output
,
4991 void perf_event_fork(struct task_struct
*task
)
4993 perf_event_task(task
, NULL
, 1);
5000 struct perf_comm_event
{
5001 struct task_struct
*task
;
5006 struct perf_event_header header
;
5013 static int perf_event_comm_match(struct perf_event
*event
)
5015 return event
->attr
.comm
;
5018 static void perf_event_comm_output(struct perf_event
*event
,
5021 struct perf_comm_event
*comm_event
= data
;
5022 struct perf_output_handle handle
;
5023 struct perf_sample_data sample
;
5024 int size
= comm_event
->event_id
.header
.size
;
5027 if (!perf_event_comm_match(event
))
5030 perf_event_header__init_id(&comm_event
->event_id
.header
, &sample
, event
);
5031 ret
= perf_output_begin(&handle
, event
,
5032 comm_event
->event_id
.header
.size
);
5037 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
5038 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
5040 perf_output_put(&handle
, comm_event
->event_id
);
5041 __output_copy(&handle
, comm_event
->comm
,
5042 comm_event
->comm_size
);
5044 perf_event__output_id_sample(event
, &handle
, &sample
);
5046 perf_output_end(&handle
);
5048 comm_event
->event_id
.header
.size
= size
;
5051 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
5053 char comm
[TASK_COMM_LEN
];
5056 memset(comm
, 0, sizeof(comm
));
5057 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
5058 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
5060 comm_event
->comm
= comm
;
5061 comm_event
->comm_size
= size
;
5063 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
5065 perf_event_aux(perf_event_comm_output
,
5070 void perf_event_comm(struct task_struct
*task
)
5072 struct perf_comm_event comm_event
;
5073 struct perf_event_context
*ctx
;
5077 for_each_task_context_nr(ctxn
) {
5078 ctx
= task
->perf_event_ctxp
[ctxn
];
5082 perf_event_enable_on_exec(ctx
);
5086 if (!atomic_read(&nr_comm_events
))
5089 comm_event
= (struct perf_comm_event
){
5095 .type
= PERF_RECORD_COMM
,
5104 perf_event_comm_event(&comm_event
);
5111 struct perf_mmap_event
{
5112 struct vm_area_struct
*vma
;
5114 const char *file_name
;
5121 struct perf_event_header header
;
5131 static int perf_event_mmap_match(struct perf_event
*event
,
5134 struct perf_mmap_event
*mmap_event
= data
;
5135 struct vm_area_struct
*vma
= mmap_event
->vma
;
5136 int executable
= vma
->vm_flags
& VM_EXEC
;
5138 return (!executable
&& event
->attr
.mmap_data
) ||
5139 (executable
&& (event
->attr
.mmap
|| event
->attr
.mmap2
));
5142 static void perf_event_mmap_output(struct perf_event
*event
,
5145 struct perf_mmap_event
*mmap_event
= data
;
5146 struct perf_output_handle handle
;
5147 struct perf_sample_data sample
;
5148 int size
= mmap_event
->event_id
.header
.size
;
5151 if (!perf_event_mmap_match(event
, data
))
5154 if (event
->attr
.mmap2
) {
5155 mmap_event
->event_id
.header
.type
= PERF_RECORD_MMAP2
;
5156 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->maj
);
5157 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->min
);
5158 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->ino
);
5159 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->ino_generation
);
5162 perf_event_header__init_id(&mmap_event
->event_id
.header
, &sample
, event
);
5163 ret
= perf_output_begin(&handle
, event
,
5164 mmap_event
->event_id
.header
.size
);
5168 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
5169 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
5171 perf_output_put(&handle
, mmap_event
->event_id
);
5173 if (event
->attr
.mmap2
) {
5174 perf_output_put(&handle
, mmap_event
->maj
);
5175 perf_output_put(&handle
, mmap_event
->min
);
5176 perf_output_put(&handle
, mmap_event
->ino
);
5177 perf_output_put(&handle
, mmap_event
->ino_generation
);
5180 __output_copy(&handle
, mmap_event
->file_name
,
5181 mmap_event
->file_size
);
5183 perf_event__output_id_sample(event
, &handle
, &sample
);
5185 perf_output_end(&handle
);
5187 mmap_event
->event_id
.header
.size
= size
;
5190 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
5192 struct vm_area_struct
*vma
= mmap_event
->vma
;
5193 struct file
*file
= vma
->vm_file
;
5194 int maj
= 0, min
= 0;
5195 u64 ino
= 0, gen
= 0;
5202 struct inode
*inode
;
5205 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
5211 * d_path() works from the end of the rb backwards, so we
5212 * need to add enough zero bytes after the string to handle
5213 * the 64bit alignment we do later.
5215 name
= d_path(&file
->f_path
, buf
, PATH_MAX
- sizeof(u64
));
5220 inode
= file_inode(vma
->vm_file
);
5221 dev
= inode
->i_sb
->s_dev
;
5223 gen
= inode
->i_generation
;
5228 name
= (char *)arch_vma_name(vma
);
5232 if (vma
->vm_start
<= vma
->vm_mm
->start_brk
&&
5233 vma
->vm_end
>= vma
->vm_mm
->brk
) {
5237 if (vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
5238 vma
->vm_end
>= vma
->vm_mm
->start_stack
) {
5248 strlcpy(tmp
, name
, sizeof(tmp
));
5252 * Since our buffer works in 8 byte units we need to align our string
5253 * size to a multiple of 8. However, we must guarantee the tail end is
5254 * zero'd out to avoid leaking random bits to userspace.
5256 size
= strlen(name
)+1;
5257 while (!IS_ALIGNED(size
, sizeof(u64
)))
5258 name
[size
++] = '\0';
5260 mmap_event
->file_name
= name
;
5261 mmap_event
->file_size
= size
;
5262 mmap_event
->maj
= maj
;
5263 mmap_event
->min
= min
;
5264 mmap_event
->ino
= ino
;
5265 mmap_event
->ino_generation
= gen
;
5267 if (!(vma
->vm_flags
& VM_EXEC
))
5268 mmap_event
->event_id
.header
.misc
|= PERF_RECORD_MISC_MMAP_DATA
;
5270 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
5272 perf_event_aux(perf_event_mmap_output
,
5279 void perf_event_mmap(struct vm_area_struct
*vma
)
5281 struct perf_mmap_event mmap_event
;
5283 if (!atomic_read(&nr_mmap_events
))
5286 mmap_event
= (struct perf_mmap_event
){
5292 .type
= PERF_RECORD_MMAP
,
5293 .misc
= PERF_RECORD_MISC_USER
,
5298 .start
= vma
->vm_start
,
5299 .len
= vma
->vm_end
- vma
->vm_start
,
5300 .pgoff
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
,
5302 /* .maj (attr_mmap2 only) */
5303 /* .min (attr_mmap2 only) */
5304 /* .ino (attr_mmap2 only) */
5305 /* .ino_generation (attr_mmap2 only) */
5308 perf_event_mmap_event(&mmap_event
);
5312 * IRQ throttle logging
5315 static void perf_log_throttle(struct perf_event
*event
, int enable
)
5317 struct perf_output_handle handle
;
5318 struct perf_sample_data sample
;
5322 struct perf_event_header header
;
5326 } throttle_event
= {
5328 .type
= PERF_RECORD_THROTTLE
,
5330 .size
= sizeof(throttle_event
),
5332 .time
= perf_clock(),
5333 .id
= primary_event_id(event
),
5334 .stream_id
= event
->id
,
5338 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
5340 perf_event_header__init_id(&throttle_event
.header
, &sample
, event
);
5342 ret
= perf_output_begin(&handle
, event
,
5343 throttle_event
.header
.size
);
5347 perf_output_put(&handle
, throttle_event
);
5348 perf_event__output_id_sample(event
, &handle
, &sample
);
5349 perf_output_end(&handle
);
5353 * Generic event overflow handling, sampling.
5356 static int __perf_event_overflow(struct perf_event
*event
,
5357 int throttle
, struct perf_sample_data
*data
,
5358 struct pt_regs
*regs
)
5360 int events
= atomic_read(&event
->event_limit
);
5361 struct hw_perf_event
*hwc
= &event
->hw
;
5366 * Non-sampling counters might still use the PMI to fold short
5367 * hardware counters, ignore those.
5369 if (unlikely(!is_sampling_event(event
)))
5372 seq
= __this_cpu_read(perf_throttled_seq
);
5373 if (seq
!= hwc
->interrupts_seq
) {
5374 hwc
->interrupts_seq
= seq
;
5375 hwc
->interrupts
= 1;
5378 if (unlikely(throttle
5379 && hwc
->interrupts
>= max_samples_per_tick
)) {
5380 __this_cpu_inc(perf_throttled_count
);
5381 hwc
->interrupts
= MAX_INTERRUPTS
;
5382 perf_log_throttle(event
, 0);
5383 tick_nohz_full_kick();
5388 if (event
->attr
.freq
) {
5389 u64 now
= perf_clock();
5390 s64 delta
= now
- hwc
->freq_time_stamp
;
5392 hwc
->freq_time_stamp
= now
;
5394 if (delta
> 0 && delta
< 2*TICK_NSEC
)
5395 perf_adjust_period(event
, delta
, hwc
->last_period
, true);
5399 * XXX event_limit might not quite work as expected on inherited
5403 event
->pending_kill
= POLL_IN
;
5404 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
5406 event
->pending_kill
= POLL_HUP
;
5407 event
->pending_disable
= 1;
5408 irq_work_queue(&event
->pending
);
5411 if (event
->overflow_handler
)
5412 event
->overflow_handler(event
, data
, regs
);
5414 perf_event_output(event
, data
, regs
);
5416 if (event
->fasync
&& event
->pending_kill
) {
5417 event
->pending_wakeup
= 1;
5418 irq_work_queue(&event
->pending
);
5424 int perf_event_overflow(struct perf_event
*event
,
5425 struct perf_sample_data
*data
,
5426 struct pt_regs
*regs
)
5428 return __perf_event_overflow(event
, 1, data
, regs
);
5432 * Generic software event infrastructure
5435 struct swevent_htable
{
5436 struct swevent_hlist
*swevent_hlist
;
5437 struct mutex hlist_mutex
;
5440 /* Recursion avoidance in each contexts */
5441 int recursion
[PERF_NR_CONTEXTS
];
5444 static DEFINE_PER_CPU(struct swevent_htable
, swevent_htable
);
5447 * We directly increment event->count and keep a second value in
5448 * event->hw.period_left to count intervals. This period event
5449 * is kept in the range [-sample_period, 0] so that we can use the
5453 u64
perf_swevent_set_period(struct perf_event
*event
)
5455 struct hw_perf_event
*hwc
= &event
->hw
;
5456 u64 period
= hwc
->last_period
;
5460 hwc
->last_period
= hwc
->sample_period
;
5463 old
= val
= local64_read(&hwc
->period_left
);
5467 nr
= div64_u64(period
+ val
, period
);
5468 offset
= nr
* period
;
5470 if (local64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
5476 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
5477 struct perf_sample_data
*data
,
5478 struct pt_regs
*regs
)
5480 struct hw_perf_event
*hwc
= &event
->hw
;
5484 overflow
= perf_swevent_set_period(event
);
5486 if (hwc
->interrupts
== MAX_INTERRUPTS
)
5489 for (; overflow
; overflow
--) {
5490 if (__perf_event_overflow(event
, throttle
,
5493 * We inhibit the overflow from happening when
5494 * hwc->interrupts == MAX_INTERRUPTS.
5502 static void perf_swevent_event(struct perf_event
*event
, u64 nr
,
5503 struct perf_sample_data
*data
,
5504 struct pt_regs
*regs
)
5506 struct hw_perf_event
*hwc
= &event
->hw
;
5508 local64_add(nr
, &event
->count
);
5513 if (!is_sampling_event(event
))
5516 if ((event
->attr
.sample_type
& PERF_SAMPLE_PERIOD
) && !event
->attr
.freq
) {
5518 return perf_swevent_overflow(event
, 1, data
, regs
);
5520 data
->period
= event
->hw
.last_period
;
5522 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
5523 return perf_swevent_overflow(event
, 1, data
, regs
);
5525 if (local64_add_negative(nr
, &hwc
->period_left
))
5528 perf_swevent_overflow(event
, 0, data
, regs
);
5531 static int perf_exclude_event(struct perf_event
*event
,
5532 struct pt_regs
*regs
)
5534 if (event
->hw
.state
& PERF_HES_STOPPED
)
5538 if (event
->attr
.exclude_user
&& user_mode(regs
))
5541 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
5548 static int perf_swevent_match(struct perf_event
*event
,
5549 enum perf_type_id type
,
5551 struct perf_sample_data
*data
,
5552 struct pt_regs
*regs
)
5554 if (event
->attr
.type
!= type
)
5557 if (event
->attr
.config
!= event_id
)
5560 if (perf_exclude_event(event
, regs
))
5566 static inline u64
swevent_hash(u64 type
, u32 event_id
)
5568 u64 val
= event_id
| (type
<< 32);
5570 return hash_64(val
, SWEVENT_HLIST_BITS
);
5573 static inline struct hlist_head
*
5574 __find_swevent_head(struct swevent_hlist
*hlist
, u64 type
, u32 event_id
)
5576 u64 hash
= swevent_hash(type
, event_id
);
5578 return &hlist
->heads
[hash
];
5581 /* For the read side: events when they trigger */
5582 static inline struct hlist_head
*
5583 find_swevent_head_rcu(struct swevent_htable
*swhash
, u64 type
, u32 event_id
)
5585 struct swevent_hlist
*hlist
;
5587 hlist
= rcu_dereference(swhash
->swevent_hlist
);
5591 return __find_swevent_head(hlist
, type
, event_id
);
5594 /* For the event head insertion and removal in the hlist */
5595 static inline struct hlist_head
*
5596 find_swevent_head(struct swevent_htable
*swhash
, struct perf_event
*event
)
5598 struct swevent_hlist
*hlist
;
5599 u32 event_id
= event
->attr
.config
;
5600 u64 type
= event
->attr
.type
;
5603 * Event scheduling is always serialized against hlist allocation
5604 * and release. Which makes the protected version suitable here.
5605 * The context lock guarantees that.
5607 hlist
= rcu_dereference_protected(swhash
->swevent_hlist
,
5608 lockdep_is_held(&event
->ctx
->lock
));
5612 return __find_swevent_head(hlist
, type
, event_id
);
5615 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
5617 struct perf_sample_data
*data
,
5618 struct pt_regs
*regs
)
5620 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5621 struct perf_event
*event
;
5622 struct hlist_head
*head
;
5625 head
= find_swevent_head_rcu(swhash
, type
, event_id
);
5629 hlist_for_each_entry_rcu(event
, head
, hlist_entry
) {
5630 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
5631 perf_swevent_event(event
, nr
, data
, regs
);
5637 int perf_swevent_get_recursion_context(void)
5639 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5641 return get_recursion_context(swhash
->recursion
);
5643 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
5645 inline void perf_swevent_put_recursion_context(int rctx
)
5647 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5649 put_recursion_context(swhash
->recursion
, rctx
);
5652 void __perf_sw_event(u32 event_id
, u64 nr
, struct pt_regs
*regs
, u64 addr
)
5654 struct perf_sample_data data
;
5657 preempt_disable_notrace();
5658 rctx
= perf_swevent_get_recursion_context();
5662 perf_sample_data_init(&data
, addr
, 0);
5664 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, &data
, regs
);
5666 perf_swevent_put_recursion_context(rctx
);
5667 preempt_enable_notrace();
5670 static void perf_swevent_read(struct perf_event
*event
)
5674 static int perf_swevent_add(struct perf_event
*event
, int flags
)
5676 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5677 struct hw_perf_event
*hwc
= &event
->hw
;
5678 struct hlist_head
*head
;
5680 if (is_sampling_event(event
)) {
5681 hwc
->last_period
= hwc
->sample_period
;
5682 perf_swevent_set_period(event
);
5685 hwc
->state
= !(flags
& PERF_EF_START
);
5687 head
= find_swevent_head(swhash
, event
);
5688 if (WARN_ON_ONCE(!head
))
5691 hlist_add_head_rcu(&event
->hlist_entry
, head
);
5696 static void perf_swevent_del(struct perf_event
*event
, int flags
)
5698 hlist_del_rcu(&event
->hlist_entry
);
5701 static void perf_swevent_start(struct perf_event
*event
, int flags
)
5703 event
->hw
.state
= 0;
5706 static void perf_swevent_stop(struct perf_event
*event
, int flags
)
5708 event
->hw
.state
= PERF_HES_STOPPED
;
5711 /* Deref the hlist from the update side */
5712 static inline struct swevent_hlist
*
5713 swevent_hlist_deref(struct swevent_htable
*swhash
)
5715 return rcu_dereference_protected(swhash
->swevent_hlist
,
5716 lockdep_is_held(&swhash
->hlist_mutex
));
5719 static void swevent_hlist_release(struct swevent_htable
*swhash
)
5721 struct swevent_hlist
*hlist
= swevent_hlist_deref(swhash
);
5726 rcu_assign_pointer(swhash
->swevent_hlist
, NULL
);
5727 kfree_rcu(hlist
, rcu_head
);
5730 static void swevent_hlist_put_cpu(struct perf_event
*event
, int cpu
)
5732 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
5734 mutex_lock(&swhash
->hlist_mutex
);
5736 if (!--swhash
->hlist_refcount
)
5737 swevent_hlist_release(swhash
);
5739 mutex_unlock(&swhash
->hlist_mutex
);
5742 static void swevent_hlist_put(struct perf_event
*event
)
5746 for_each_possible_cpu(cpu
)
5747 swevent_hlist_put_cpu(event
, cpu
);
5750 static int swevent_hlist_get_cpu(struct perf_event
*event
, int cpu
)
5752 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
5755 mutex_lock(&swhash
->hlist_mutex
);
5757 if (!swevent_hlist_deref(swhash
) && cpu_online(cpu
)) {
5758 struct swevent_hlist
*hlist
;
5760 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
5765 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
5767 swhash
->hlist_refcount
++;
5769 mutex_unlock(&swhash
->hlist_mutex
);
5774 static int swevent_hlist_get(struct perf_event
*event
)
5777 int cpu
, failed_cpu
;
5780 for_each_possible_cpu(cpu
) {
5781 err
= swevent_hlist_get_cpu(event
, cpu
);
5791 for_each_possible_cpu(cpu
) {
5792 if (cpu
== failed_cpu
)
5794 swevent_hlist_put_cpu(event
, cpu
);
5801 struct static_key perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
5803 static void sw_perf_event_destroy(struct perf_event
*event
)
5805 u64 event_id
= event
->attr
.config
;
5807 WARN_ON(event
->parent
);
5809 static_key_slow_dec(&perf_swevent_enabled
[event_id
]);
5810 swevent_hlist_put(event
);
5813 static int perf_swevent_init(struct perf_event
*event
)
5815 u64 event_id
= event
->attr
.config
;
5817 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
5821 * no branch sampling for software events
5823 if (has_branch_stack(event
))
5827 case PERF_COUNT_SW_CPU_CLOCK
:
5828 case PERF_COUNT_SW_TASK_CLOCK
:
5835 if (event_id
>= PERF_COUNT_SW_MAX
)
5838 if (!event
->parent
) {
5841 err
= swevent_hlist_get(event
);
5845 static_key_slow_inc(&perf_swevent_enabled
[event_id
]);
5846 event
->destroy
= sw_perf_event_destroy
;
5852 static int perf_swevent_event_idx(struct perf_event
*event
)
5857 static struct pmu perf_swevent
= {
5858 .task_ctx_nr
= perf_sw_context
,
5860 .event_init
= perf_swevent_init
,
5861 .add
= perf_swevent_add
,
5862 .del
= perf_swevent_del
,
5863 .start
= perf_swevent_start
,
5864 .stop
= perf_swevent_stop
,
5865 .read
= perf_swevent_read
,
5867 .event_idx
= perf_swevent_event_idx
,
5870 #ifdef CONFIG_EVENT_TRACING
5872 static int perf_tp_filter_match(struct perf_event
*event
,
5873 struct perf_sample_data
*data
)
5875 void *record
= data
->raw
->data
;
5877 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
5882 static int perf_tp_event_match(struct perf_event
*event
,
5883 struct perf_sample_data
*data
,
5884 struct pt_regs
*regs
)
5886 if (event
->hw
.state
& PERF_HES_STOPPED
)
5889 * All tracepoints are from kernel-space.
5891 if (event
->attr
.exclude_kernel
)
5894 if (!perf_tp_filter_match(event
, data
))
5900 void perf_tp_event(u64 addr
, u64 count
, void *record
, int entry_size
,
5901 struct pt_regs
*regs
, struct hlist_head
*head
, int rctx
,
5902 struct task_struct
*task
)
5904 struct perf_sample_data data
;
5905 struct perf_event
*event
;
5907 struct perf_raw_record raw
= {
5912 perf_sample_data_init(&data
, addr
, 0);
5915 hlist_for_each_entry_rcu(event
, head
, hlist_entry
) {
5916 if (perf_tp_event_match(event
, &data
, regs
))
5917 perf_swevent_event(event
, count
, &data
, regs
);
5921 * If we got specified a target task, also iterate its context and
5922 * deliver this event there too.
5924 if (task
&& task
!= current
) {
5925 struct perf_event_context
*ctx
;
5926 struct trace_entry
*entry
= record
;
5929 ctx
= rcu_dereference(task
->perf_event_ctxp
[perf_sw_context
]);
5933 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
5934 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
5936 if (event
->attr
.config
!= entry
->type
)
5938 if (perf_tp_event_match(event
, &data
, regs
))
5939 perf_swevent_event(event
, count
, &data
, regs
);
5945 perf_swevent_put_recursion_context(rctx
);
5947 EXPORT_SYMBOL_GPL(perf_tp_event
);
5949 static void tp_perf_event_destroy(struct perf_event
*event
)
5951 perf_trace_destroy(event
);
5954 static int perf_tp_event_init(struct perf_event
*event
)
5958 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
5962 * no branch sampling for tracepoint events
5964 if (has_branch_stack(event
))
5967 err
= perf_trace_init(event
);
5971 event
->destroy
= tp_perf_event_destroy
;
5976 static struct pmu perf_tracepoint
= {
5977 .task_ctx_nr
= perf_sw_context
,
5979 .event_init
= perf_tp_event_init
,
5980 .add
= perf_trace_add
,
5981 .del
= perf_trace_del
,
5982 .start
= perf_swevent_start
,
5983 .stop
= perf_swevent_stop
,
5984 .read
= perf_swevent_read
,
5986 .event_idx
= perf_swevent_event_idx
,
5989 static inline void perf_tp_register(void)
5991 perf_pmu_register(&perf_tracepoint
, "tracepoint", PERF_TYPE_TRACEPOINT
);
5994 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
5999 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
6002 filter_str
= strndup_user(arg
, PAGE_SIZE
);
6003 if (IS_ERR(filter_str
))
6004 return PTR_ERR(filter_str
);
6006 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
, filter_str
);
6012 static void perf_event_free_filter(struct perf_event
*event
)
6014 ftrace_profile_free_filter(event
);
6019 static inline void perf_tp_register(void)
6023 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
6028 static void perf_event_free_filter(struct perf_event
*event
)
6032 #endif /* CONFIG_EVENT_TRACING */
6034 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6035 void perf_bp_event(struct perf_event
*bp
, void *data
)
6037 struct perf_sample_data sample
;
6038 struct pt_regs
*regs
= data
;
6040 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
, 0);
6042 if (!bp
->hw
.state
&& !perf_exclude_event(bp
, regs
))
6043 perf_swevent_event(bp
, 1, &sample
, regs
);
6048 * hrtimer based swevent callback
6051 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
6053 enum hrtimer_restart ret
= HRTIMER_RESTART
;
6054 struct perf_sample_data data
;
6055 struct pt_regs
*regs
;
6056 struct perf_event
*event
;
6059 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
6061 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
6062 return HRTIMER_NORESTART
;
6064 event
->pmu
->read(event
);
6066 perf_sample_data_init(&data
, 0, event
->hw
.last_period
);
6067 regs
= get_irq_regs();
6069 if (regs
&& !perf_exclude_event(event
, regs
)) {
6070 if (!(event
->attr
.exclude_idle
&& is_idle_task(current
)))
6071 if (__perf_event_overflow(event
, 1, &data
, regs
))
6072 ret
= HRTIMER_NORESTART
;
6075 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
6076 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
6081 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
6083 struct hw_perf_event
*hwc
= &event
->hw
;
6086 if (!is_sampling_event(event
))
6089 period
= local64_read(&hwc
->period_left
);
6094 local64_set(&hwc
->period_left
, 0);
6096 period
= max_t(u64
, 10000, hwc
->sample_period
);
6098 __hrtimer_start_range_ns(&hwc
->hrtimer
,
6099 ns_to_ktime(period
), 0,
6100 HRTIMER_MODE_REL_PINNED
, 0);
6103 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
6105 struct hw_perf_event
*hwc
= &event
->hw
;
6107 if (is_sampling_event(event
)) {
6108 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
6109 local64_set(&hwc
->period_left
, ktime_to_ns(remaining
));
6111 hrtimer_cancel(&hwc
->hrtimer
);
6115 static void perf_swevent_init_hrtimer(struct perf_event
*event
)
6117 struct hw_perf_event
*hwc
= &event
->hw
;
6119 if (!is_sampling_event(event
))
6122 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
6123 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
6126 * Since hrtimers have a fixed rate, we can do a static freq->period
6127 * mapping and avoid the whole period adjust feedback stuff.
6129 if (event
->attr
.freq
) {
6130 long freq
= event
->attr
.sample_freq
;
6132 event
->attr
.sample_period
= NSEC_PER_SEC
/ freq
;
6133 hwc
->sample_period
= event
->attr
.sample_period
;
6134 local64_set(&hwc
->period_left
, hwc
->sample_period
);
6135 hwc
->last_period
= hwc
->sample_period
;
6136 event
->attr
.freq
= 0;
6141 * Software event: cpu wall time clock
6144 static void cpu_clock_event_update(struct perf_event
*event
)
6149 now
= local_clock();
6150 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
6151 local64_add(now
- prev
, &event
->count
);
6154 static void cpu_clock_event_start(struct perf_event
*event
, int flags
)
6156 local64_set(&event
->hw
.prev_count
, local_clock());
6157 perf_swevent_start_hrtimer(event
);
6160 static void cpu_clock_event_stop(struct perf_event
*event
, int flags
)
6162 perf_swevent_cancel_hrtimer(event
);
6163 cpu_clock_event_update(event
);
6166 static int cpu_clock_event_add(struct perf_event
*event
, int flags
)
6168 if (flags
& PERF_EF_START
)
6169 cpu_clock_event_start(event
, flags
);
6174 static void cpu_clock_event_del(struct perf_event
*event
, int flags
)
6176 cpu_clock_event_stop(event
, flags
);
6179 static void cpu_clock_event_read(struct perf_event
*event
)
6181 cpu_clock_event_update(event
);
6184 static int cpu_clock_event_init(struct perf_event
*event
)
6186 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
6189 if (event
->attr
.config
!= PERF_COUNT_SW_CPU_CLOCK
)
6193 * no branch sampling for software events
6195 if (has_branch_stack(event
))
6198 perf_swevent_init_hrtimer(event
);
6203 static struct pmu perf_cpu_clock
= {
6204 .task_ctx_nr
= perf_sw_context
,
6206 .event_init
= cpu_clock_event_init
,
6207 .add
= cpu_clock_event_add
,
6208 .del
= cpu_clock_event_del
,
6209 .start
= cpu_clock_event_start
,
6210 .stop
= cpu_clock_event_stop
,
6211 .read
= cpu_clock_event_read
,
6213 .event_idx
= perf_swevent_event_idx
,
6217 * Software event: task time clock
6220 static void task_clock_event_update(struct perf_event
*event
, u64 now
)
6225 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
6227 local64_add(delta
, &event
->count
);
6230 static void task_clock_event_start(struct perf_event
*event
, int flags
)
6232 local64_set(&event
->hw
.prev_count
, event
->ctx
->time
);
6233 perf_swevent_start_hrtimer(event
);
6236 static void task_clock_event_stop(struct perf_event
*event
, int flags
)
6238 perf_swevent_cancel_hrtimer(event
);
6239 task_clock_event_update(event
, event
->ctx
->time
);
6242 static int task_clock_event_add(struct perf_event
*event
, int flags
)
6244 if (flags
& PERF_EF_START
)
6245 task_clock_event_start(event
, flags
);
6250 static void task_clock_event_del(struct perf_event
*event
, int flags
)
6252 task_clock_event_stop(event
, PERF_EF_UPDATE
);
6255 static void task_clock_event_read(struct perf_event
*event
)
6257 u64 now
= perf_clock();
6258 u64 delta
= now
- event
->ctx
->timestamp
;
6259 u64 time
= event
->ctx
->time
+ delta
;
6261 task_clock_event_update(event
, time
);
6264 static int task_clock_event_init(struct perf_event
*event
)
6266 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
6269 if (event
->attr
.config
!= PERF_COUNT_SW_TASK_CLOCK
)
6273 * no branch sampling for software events
6275 if (has_branch_stack(event
))
6278 perf_swevent_init_hrtimer(event
);
6283 static struct pmu perf_task_clock
= {
6284 .task_ctx_nr
= perf_sw_context
,
6286 .event_init
= task_clock_event_init
,
6287 .add
= task_clock_event_add
,
6288 .del
= task_clock_event_del
,
6289 .start
= task_clock_event_start
,
6290 .stop
= task_clock_event_stop
,
6291 .read
= task_clock_event_read
,
6293 .event_idx
= perf_swevent_event_idx
,
6296 static void perf_pmu_nop_void(struct pmu
*pmu
)
6300 static int perf_pmu_nop_int(struct pmu
*pmu
)
6305 static void perf_pmu_start_txn(struct pmu
*pmu
)
6307 perf_pmu_disable(pmu
);
6310 static int perf_pmu_commit_txn(struct pmu
*pmu
)
6312 perf_pmu_enable(pmu
);
6316 static void perf_pmu_cancel_txn(struct pmu
*pmu
)
6318 perf_pmu_enable(pmu
);
6321 static int perf_event_idx_default(struct perf_event
*event
)
6323 return event
->hw
.idx
+ 1;
6327 * Ensures all contexts with the same task_ctx_nr have the same
6328 * pmu_cpu_context too.
6330 static struct perf_cpu_context __percpu
*find_pmu_context(int ctxn
)
6337 list_for_each_entry(pmu
, &pmus
, entry
) {
6338 if (pmu
->task_ctx_nr
== ctxn
)
6339 return pmu
->pmu_cpu_context
;
6345 static void update_pmu_context(struct pmu
*pmu
, struct pmu
*old_pmu
)
6349 for_each_possible_cpu(cpu
) {
6350 struct perf_cpu_context
*cpuctx
;
6352 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
6354 if (cpuctx
->unique_pmu
== old_pmu
)
6355 cpuctx
->unique_pmu
= pmu
;
6359 static void free_pmu_context(struct pmu
*pmu
)
6363 mutex_lock(&pmus_lock
);
6365 * Like a real lame refcount.
6367 list_for_each_entry(i
, &pmus
, entry
) {
6368 if (i
->pmu_cpu_context
== pmu
->pmu_cpu_context
) {
6369 update_pmu_context(i
, pmu
);
6374 free_percpu(pmu
->pmu_cpu_context
);
6376 mutex_unlock(&pmus_lock
);
6378 static struct idr pmu_idr
;
6381 type_show(struct device
*dev
, struct device_attribute
*attr
, char *page
)
6383 struct pmu
*pmu
= dev_get_drvdata(dev
);
6385 return snprintf(page
, PAGE_SIZE
-1, "%d\n", pmu
->type
);
6387 static DEVICE_ATTR_RO(type
);
6390 perf_event_mux_interval_ms_show(struct device
*dev
,
6391 struct device_attribute
*attr
,
6394 struct pmu
*pmu
= dev_get_drvdata(dev
);
6396 return snprintf(page
, PAGE_SIZE
-1, "%d\n", pmu
->hrtimer_interval_ms
);
6400 perf_event_mux_interval_ms_store(struct device
*dev
,
6401 struct device_attribute
*attr
,
6402 const char *buf
, size_t count
)
6404 struct pmu
*pmu
= dev_get_drvdata(dev
);
6405 int timer
, cpu
, ret
;
6407 ret
= kstrtoint(buf
, 0, &timer
);
6414 /* same value, noting to do */
6415 if (timer
== pmu
->hrtimer_interval_ms
)
6418 pmu
->hrtimer_interval_ms
= timer
;
6420 /* update all cpuctx for this PMU */
6421 for_each_possible_cpu(cpu
) {
6422 struct perf_cpu_context
*cpuctx
;
6423 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
6424 cpuctx
->hrtimer_interval
= ns_to_ktime(NSEC_PER_MSEC
* timer
);
6426 if (hrtimer_active(&cpuctx
->hrtimer
))
6427 hrtimer_forward_now(&cpuctx
->hrtimer
, cpuctx
->hrtimer_interval
);
6432 static DEVICE_ATTR_RW(perf_event_mux_interval_ms
);
6434 static struct attribute
*pmu_dev_attrs
[] = {
6435 &dev_attr_type
.attr
,
6436 &dev_attr_perf_event_mux_interval_ms
.attr
,
6439 ATTRIBUTE_GROUPS(pmu_dev
);
6441 static int pmu_bus_running
;
6442 static struct bus_type pmu_bus
= {
6443 .name
= "event_source",
6444 .dev_groups
= pmu_dev_groups
,
6447 static void pmu_dev_release(struct device
*dev
)
6452 static int pmu_dev_alloc(struct pmu
*pmu
)
6456 pmu
->dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
6460 pmu
->dev
->groups
= pmu
->attr_groups
;
6461 device_initialize(pmu
->dev
);
6462 ret
= dev_set_name(pmu
->dev
, "%s", pmu
->name
);
6466 dev_set_drvdata(pmu
->dev
, pmu
);
6467 pmu
->dev
->bus
= &pmu_bus
;
6468 pmu
->dev
->release
= pmu_dev_release
;
6469 ret
= device_add(pmu
->dev
);
6477 put_device(pmu
->dev
);
6481 static struct lock_class_key cpuctx_mutex
;
6482 static struct lock_class_key cpuctx_lock
;
6484 int perf_pmu_register(struct pmu
*pmu
, const char *name
, int type
)
6488 mutex_lock(&pmus_lock
);
6490 pmu
->pmu_disable_count
= alloc_percpu(int);
6491 if (!pmu
->pmu_disable_count
)
6500 type
= idr_alloc(&pmu_idr
, pmu
, PERF_TYPE_MAX
, 0, GFP_KERNEL
);
6508 if (pmu_bus_running
) {
6509 ret
= pmu_dev_alloc(pmu
);
6515 pmu
->pmu_cpu_context
= find_pmu_context(pmu
->task_ctx_nr
);
6516 if (pmu
->pmu_cpu_context
)
6517 goto got_cpu_context
;
6520 pmu
->pmu_cpu_context
= alloc_percpu(struct perf_cpu_context
);
6521 if (!pmu
->pmu_cpu_context
)
6524 for_each_possible_cpu(cpu
) {
6525 struct perf_cpu_context
*cpuctx
;
6527 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
6528 __perf_event_init_context(&cpuctx
->ctx
);
6529 lockdep_set_class(&cpuctx
->ctx
.mutex
, &cpuctx_mutex
);
6530 lockdep_set_class(&cpuctx
->ctx
.lock
, &cpuctx_lock
);
6531 cpuctx
->ctx
.type
= cpu_context
;
6532 cpuctx
->ctx
.pmu
= pmu
;
6534 __perf_cpu_hrtimer_init(cpuctx
, cpu
);
6536 INIT_LIST_HEAD(&cpuctx
->rotation_list
);
6537 cpuctx
->unique_pmu
= pmu
;
6541 if (!pmu
->start_txn
) {
6542 if (pmu
->pmu_enable
) {
6544 * If we have pmu_enable/pmu_disable calls, install
6545 * transaction stubs that use that to try and batch
6546 * hardware accesses.
6548 pmu
->start_txn
= perf_pmu_start_txn
;
6549 pmu
->commit_txn
= perf_pmu_commit_txn
;
6550 pmu
->cancel_txn
= perf_pmu_cancel_txn
;
6552 pmu
->start_txn
= perf_pmu_nop_void
;
6553 pmu
->commit_txn
= perf_pmu_nop_int
;
6554 pmu
->cancel_txn
= perf_pmu_nop_void
;
6558 if (!pmu
->pmu_enable
) {
6559 pmu
->pmu_enable
= perf_pmu_nop_void
;
6560 pmu
->pmu_disable
= perf_pmu_nop_void
;
6563 if (!pmu
->event_idx
)
6564 pmu
->event_idx
= perf_event_idx_default
;
6566 list_add_rcu(&pmu
->entry
, &pmus
);
6569 mutex_unlock(&pmus_lock
);
6574 device_del(pmu
->dev
);
6575 put_device(pmu
->dev
);
6578 if (pmu
->type
>= PERF_TYPE_MAX
)
6579 idr_remove(&pmu_idr
, pmu
->type
);
6582 free_percpu(pmu
->pmu_disable_count
);
6585 EXPORT_SYMBOL_GPL(perf_pmu_register
);
6587 void perf_pmu_unregister(struct pmu
*pmu
)
6589 mutex_lock(&pmus_lock
);
6590 list_del_rcu(&pmu
->entry
);
6591 mutex_unlock(&pmus_lock
);
6594 * We dereference the pmu list under both SRCU and regular RCU, so
6595 * synchronize against both of those.
6597 synchronize_srcu(&pmus_srcu
);
6600 free_percpu(pmu
->pmu_disable_count
);
6601 if (pmu
->type
>= PERF_TYPE_MAX
)
6602 idr_remove(&pmu_idr
, pmu
->type
);
6603 device_del(pmu
->dev
);
6604 put_device(pmu
->dev
);
6605 free_pmu_context(pmu
);
6607 EXPORT_SYMBOL_GPL(perf_pmu_unregister
);
6609 struct pmu
*perf_init_event(struct perf_event
*event
)
6611 struct pmu
*pmu
= NULL
;
6615 idx
= srcu_read_lock(&pmus_srcu
);
6618 pmu
= idr_find(&pmu_idr
, event
->attr
.type
);
6621 if (!try_module_get(pmu
->module
)) {
6622 pmu
= ERR_PTR(-ENODEV
);
6626 ret
= pmu
->event_init(event
);
6632 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
6633 if (!try_module_get(pmu
->module
)) {
6634 pmu
= ERR_PTR(-ENODEV
);
6638 ret
= pmu
->event_init(event
);
6642 if (ret
!= -ENOENT
) {
6647 pmu
= ERR_PTR(-ENOENT
);
6649 srcu_read_unlock(&pmus_srcu
, idx
);
6654 static void account_event_cpu(struct perf_event
*event
, int cpu
)
6659 if (has_branch_stack(event
)) {
6660 if (!(event
->attach_state
& PERF_ATTACH_TASK
))
6661 atomic_inc(&per_cpu(perf_branch_stack_events
, cpu
));
6663 if (is_cgroup_event(event
))
6664 atomic_inc(&per_cpu(perf_cgroup_events
, cpu
));
6667 static void account_event(struct perf_event
*event
)
6672 if (event
->attach_state
& PERF_ATTACH_TASK
)
6673 static_key_slow_inc(&perf_sched_events
.key
);
6674 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
6675 atomic_inc(&nr_mmap_events
);
6676 if (event
->attr
.comm
)
6677 atomic_inc(&nr_comm_events
);
6678 if (event
->attr
.task
)
6679 atomic_inc(&nr_task_events
);
6680 if (event
->attr
.freq
) {
6681 if (atomic_inc_return(&nr_freq_events
) == 1)
6682 tick_nohz_full_kick_all();
6684 if (has_branch_stack(event
))
6685 static_key_slow_inc(&perf_sched_events
.key
);
6686 if (is_cgroup_event(event
))
6687 static_key_slow_inc(&perf_sched_events
.key
);
6689 account_event_cpu(event
, event
->cpu
);
6693 * Allocate and initialize a event structure
6695 static struct perf_event
*
6696 perf_event_alloc(struct perf_event_attr
*attr
, int cpu
,
6697 struct task_struct
*task
,
6698 struct perf_event
*group_leader
,
6699 struct perf_event
*parent_event
,
6700 perf_overflow_handler_t overflow_handler
,
6704 struct perf_event
*event
;
6705 struct hw_perf_event
*hwc
;
6708 if ((unsigned)cpu
>= nr_cpu_ids
) {
6709 if (!task
|| cpu
!= -1)
6710 return ERR_PTR(-EINVAL
);
6713 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
6715 return ERR_PTR(-ENOMEM
);
6718 * Single events are their own group leaders, with an
6719 * empty sibling list:
6722 group_leader
= event
;
6724 mutex_init(&event
->child_mutex
);
6725 INIT_LIST_HEAD(&event
->child_list
);
6727 INIT_LIST_HEAD(&event
->group_entry
);
6728 INIT_LIST_HEAD(&event
->event_entry
);
6729 INIT_LIST_HEAD(&event
->sibling_list
);
6730 INIT_LIST_HEAD(&event
->rb_entry
);
6731 INIT_LIST_HEAD(&event
->active_entry
);
6732 INIT_HLIST_NODE(&event
->hlist_entry
);
6735 init_waitqueue_head(&event
->waitq
);
6736 init_irq_work(&event
->pending
, perf_pending_event
);
6738 mutex_init(&event
->mmap_mutex
);
6740 atomic_long_set(&event
->refcount
, 1);
6742 event
->attr
= *attr
;
6743 event
->group_leader
= group_leader
;
6747 event
->parent
= parent_event
;
6749 event
->ns
= get_pid_ns(task_active_pid_ns(current
));
6750 event
->id
= atomic64_inc_return(&perf_event_id
);
6752 event
->state
= PERF_EVENT_STATE_INACTIVE
;
6755 event
->attach_state
= PERF_ATTACH_TASK
;
6757 if (attr
->type
== PERF_TYPE_TRACEPOINT
)
6758 event
->hw
.tp_target
= task
;
6759 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6761 * hw_breakpoint is a bit difficult here..
6763 else if (attr
->type
== PERF_TYPE_BREAKPOINT
)
6764 event
->hw
.bp_target
= task
;
6768 if (!overflow_handler
&& parent_event
) {
6769 overflow_handler
= parent_event
->overflow_handler
;
6770 context
= parent_event
->overflow_handler_context
;
6773 event
->overflow_handler
= overflow_handler
;
6774 event
->overflow_handler_context
= context
;
6776 perf_event__state_init(event
);
6781 hwc
->sample_period
= attr
->sample_period
;
6782 if (attr
->freq
&& attr
->sample_freq
)
6783 hwc
->sample_period
= 1;
6784 hwc
->last_period
= hwc
->sample_period
;
6786 local64_set(&hwc
->period_left
, hwc
->sample_period
);
6789 * we currently do not support PERF_FORMAT_GROUP on inherited events
6791 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
6794 pmu
= perf_init_event(event
);
6797 else if (IS_ERR(pmu
)) {
6802 if (!event
->parent
) {
6803 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
6804 err
= get_callchain_buffers();
6814 event
->destroy(event
);
6815 module_put(pmu
->module
);
6818 put_pid_ns(event
->ns
);
6821 return ERR_PTR(err
);
6824 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
6825 struct perf_event_attr
*attr
)
6830 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
6834 * zero the full structure, so that a short copy will be nice.
6836 memset(attr
, 0, sizeof(*attr
));
6838 ret
= get_user(size
, &uattr
->size
);
6842 if (size
> PAGE_SIZE
) /* silly large */
6845 if (!size
) /* abi compat */
6846 size
= PERF_ATTR_SIZE_VER0
;
6848 if (size
< PERF_ATTR_SIZE_VER0
)
6852 * If we're handed a bigger struct than we know of,
6853 * ensure all the unknown bits are 0 - i.e. new
6854 * user-space does not rely on any kernel feature
6855 * extensions we dont know about yet.
6857 if (size
> sizeof(*attr
)) {
6858 unsigned char __user
*addr
;
6859 unsigned char __user
*end
;
6862 addr
= (void __user
*)uattr
+ sizeof(*attr
);
6863 end
= (void __user
*)uattr
+ size
;
6865 for (; addr
< end
; addr
++) {
6866 ret
= get_user(val
, addr
);
6872 size
= sizeof(*attr
);
6875 ret
= copy_from_user(attr
, uattr
, size
);
6879 /* disabled for now */
6883 if (attr
->__reserved_1
)
6886 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
6889 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
6892 if (attr
->sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
6893 u64 mask
= attr
->branch_sample_type
;
6895 /* only using defined bits */
6896 if (mask
& ~(PERF_SAMPLE_BRANCH_MAX
-1))
6899 /* at least one branch bit must be set */
6900 if (!(mask
& ~PERF_SAMPLE_BRANCH_PLM_ALL
))
6903 /* propagate priv level, when not set for branch */
6904 if (!(mask
& PERF_SAMPLE_BRANCH_PLM_ALL
)) {
6906 /* exclude_kernel checked on syscall entry */
6907 if (!attr
->exclude_kernel
)
6908 mask
|= PERF_SAMPLE_BRANCH_KERNEL
;
6910 if (!attr
->exclude_user
)
6911 mask
|= PERF_SAMPLE_BRANCH_USER
;
6913 if (!attr
->exclude_hv
)
6914 mask
|= PERF_SAMPLE_BRANCH_HV
;
6916 * adjust user setting (for HW filter setup)
6918 attr
->branch_sample_type
= mask
;
6920 /* privileged levels capture (kernel, hv): check permissions */
6921 if ((mask
& PERF_SAMPLE_BRANCH_PERM_PLM
)
6922 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
6926 if (attr
->sample_type
& PERF_SAMPLE_REGS_USER
) {
6927 ret
= perf_reg_validate(attr
->sample_regs_user
);
6932 if (attr
->sample_type
& PERF_SAMPLE_STACK_USER
) {
6933 if (!arch_perf_have_user_stack_dump())
6937 * We have __u32 type for the size, but so far
6938 * we can only use __u16 as maximum due to the
6939 * __u16 sample size limit.
6941 if (attr
->sample_stack_user
>= USHRT_MAX
)
6943 else if (!IS_ALIGNED(attr
->sample_stack_user
, sizeof(u64
)))
6951 put_user(sizeof(*attr
), &uattr
->size
);
6957 perf_event_set_output(struct perf_event
*event
, struct perf_event
*output_event
)
6959 struct ring_buffer
*rb
= NULL
, *old_rb
= NULL
;
6965 /* don't allow circular references */
6966 if (event
== output_event
)
6970 * Don't allow cross-cpu buffers
6972 if (output_event
->cpu
!= event
->cpu
)
6976 * If its not a per-cpu rb, it must be the same task.
6978 if (output_event
->cpu
== -1 && output_event
->ctx
!= event
->ctx
)
6982 mutex_lock(&event
->mmap_mutex
);
6983 /* Can't redirect output if we've got an active mmap() */
6984 if (atomic_read(&event
->mmap_count
))
6990 /* get the rb we want to redirect to */
6991 rb
= ring_buffer_get(output_event
);
6997 ring_buffer_detach(event
, old_rb
);
7000 ring_buffer_attach(event
, rb
);
7002 rcu_assign_pointer(event
->rb
, rb
);
7005 ring_buffer_put(old_rb
);
7007 * Since we detached before setting the new rb, so that we
7008 * could attach the new rb, we could have missed a wakeup.
7011 wake_up_all(&event
->waitq
);
7016 mutex_unlock(&event
->mmap_mutex
);
7023 * sys_perf_event_open - open a performance event, associate it to a task/cpu
7025 * @attr_uptr: event_id type attributes for monitoring/sampling
7028 * @group_fd: group leader event fd
7030 SYSCALL_DEFINE5(perf_event_open
,
7031 struct perf_event_attr __user
*, attr_uptr
,
7032 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
7034 struct perf_event
*group_leader
= NULL
, *output_event
= NULL
;
7035 struct perf_event
*event
, *sibling
;
7036 struct perf_event_attr attr
;
7037 struct perf_event_context
*ctx
;
7038 struct file
*event_file
= NULL
;
7039 struct fd group
= {NULL
, 0};
7040 struct task_struct
*task
= NULL
;
7045 int f_flags
= O_RDWR
;
7047 /* for future expandability... */
7048 if (flags
& ~PERF_FLAG_ALL
)
7051 err
= perf_copy_attr(attr_uptr
, &attr
);
7055 if (!attr
.exclude_kernel
) {
7056 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
7061 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
7066 * In cgroup mode, the pid argument is used to pass the fd
7067 * opened to the cgroup directory in cgroupfs. The cpu argument
7068 * designates the cpu on which to monitor threads from that
7071 if ((flags
& PERF_FLAG_PID_CGROUP
) && (pid
== -1 || cpu
== -1))
7074 if (flags
& PERF_FLAG_FD_CLOEXEC
)
7075 f_flags
|= O_CLOEXEC
;
7077 event_fd
= get_unused_fd_flags(f_flags
);
7081 if (group_fd
!= -1) {
7082 err
= perf_fget_light(group_fd
, &group
);
7085 group_leader
= group
.file
->private_data
;
7086 if (flags
& PERF_FLAG_FD_OUTPUT
)
7087 output_event
= group_leader
;
7088 if (flags
& PERF_FLAG_FD_NO_GROUP
)
7089 group_leader
= NULL
;
7092 if (pid
!= -1 && !(flags
& PERF_FLAG_PID_CGROUP
)) {
7093 task
= find_lively_task_by_vpid(pid
);
7095 err
= PTR_ERR(task
);
7100 if (task
&& group_leader
&&
7101 group_leader
->attr
.inherit
!= attr
.inherit
) {
7108 event
= perf_event_alloc(&attr
, cpu
, task
, group_leader
, NULL
,
7110 if (IS_ERR(event
)) {
7111 err
= PTR_ERR(event
);
7115 if (flags
& PERF_FLAG_PID_CGROUP
) {
7116 err
= perf_cgroup_connect(pid
, event
, &attr
, group_leader
);
7118 __free_event(event
);
7123 if (is_sampling_event(event
)) {
7124 if (event
->pmu
->capabilities
& PERF_PMU_CAP_NO_INTERRUPT
) {
7130 account_event(event
);
7133 * Special case software events and allow them to be part of
7134 * any hardware group.
7139 (is_software_event(event
) != is_software_event(group_leader
))) {
7140 if (is_software_event(event
)) {
7142 * If event and group_leader are not both a software
7143 * event, and event is, then group leader is not.
7145 * Allow the addition of software events to !software
7146 * groups, this is safe because software events never
7149 pmu
= group_leader
->pmu
;
7150 } else if (is_software_event(group_leader
) &&
7151 (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
)) {
7153 * In case the group is a pure software group, and we
7154 * try to add a hardware event, move the whole group to
7155 * the hardware context.
7162 * Get the target context (task or percpu):
7164 ctx
= find_get_context(pmu
, task
, event
->cpu
);
7171 put_task_struct(task
);
7176 * Look up the group leader (we will attach this event to it):
7182 * Do not allow a recursive hierarchy (this new sibling
7183 * becoming part of another group-sibling):
7185 if (group_leader
->group_leader
!= group_leader
)
7188 * Do not allow to attach to a group in a different
7189 * task or CPU context:
7192 if (group_leader
->ctx
->type
!= ctx
->type
)
7195 if (group_leader
->ctx
!= ctx
)
7200 * Only a group leader can be exclusive or pinned
7202 if (attr
.exclusive
|| attr
.pinned
)
7207 err
= perf_event_set_output(event
, output_event
);
7212 event_file
= anon_inode_getfile("[perf_event]", &perf_fops
, event
,
7214 if (IS_ERR(event_file
)) {
7215 err
= PTR_ERR(event_file
);
7220 struct perf_event_context
*gctx
= group_leader
->ctx
;
7222 mutex_lock(&gctx
->mutex
);
7223 perf_remove_from_context(group_leader
, false);
7226 * Removing from the context ends up with disabled
7227 * event. What we want here is event in the initial
7228 * startup state, ready to be add into new context.
7230 perf_event__state_init(group_leader
);
7231 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
7233 perf_remove_from_context(sibling
, false);
7234 perf_event__state_init(sibling
);
7237 mutex_unlock(&gctx
->mutex
);
7241 WARN_ON_ONCE(ctx
->parent_ctx
);
7242 mutex_lock(&ctx
->mutex
);
7246 perf_install_in_context(ctx
, group_leader
, event
->cpu
);
7248 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
7250 perf_install_in_context(ctx
, sibling
, event
->cpu
);
7255 perf_install_in_context(ctx
, event
, event
->cpu
);
7256 perf_unpin_context(ctx
);
7257 mutex_unlock(&ctx
->mutex
);
7261 event
->owner
= current
;
7263 mutex_lock(¤t
->perf_event_mutex
);
7264 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
7265 mutex_unlock(¤t
->perf_event_mutex
);
7268 * Precalculate sample_data sizes
7270 perf_event__header_size(event
);
7271 perf_event__id_header_size(event
);
7274 * Drop the reference on the group_event after placing the
7275 * new event on the sibling_list. This ensures destruction
7276 * of the group leader will find the pointer to itself in
7277 * perf_group_detach().
7280 fd_install(event_fd
, event_file
);
7284 perf_unpin_context(ctx
);
7292 put_task_struct(task
);
7296 put_unused_fd(event_fd
);
7301 * perf_event_create_kernel_counter
7303 * @attr: attributes of the counter to create
7304 * @cpu: cpu in which the counter is bound
7305 * @task: task to profile (NULL for percpu)
7308 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
7309 struct task_struct
*task
,
7310 perf_overflow_handler_t overflow_handler
,
7313 struct perf_event_context
*ctx
;
7314 struct perf_event
*event
;
7318 * Get the target context (task or percpu):
7321 event
= perf_event_alloc(attr
, cpu
, task
, NULL
, NULL
,
7322 overflow_handler
, context
);
7323 if (IS_ERR(event
)) {
7324 err
= PTR_ERR(event
);
7328 account_event(event
);
7330 ctx
= find_get_context(event
->pmu
, task
, cpu
);
7336 WARN_ON_ONCE(ctx
->parent_ctx
);
7337 mutex_lock(&ctx
->mutex
);
7338 perf_install_in_context(ctx
, event
, cpu
);
7339 perf_unpin_context(ctx
);
7340 mutex_unlock(&ctx
->mutex
);
7347 return ERR_PTR(err
);
7349 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
7351 void perf_pmu_migrate_context(struct pmu
*pmu
, int src_cpu
, int dst_cpu
)
7353 struct perf_event_context
*src_ctx
;
7354 struct perf_event_context
*dst_ctx
;
7355 struct perf_event
*event
, *tmp
;
7358 src_ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, src_cpu
)->ctx
;
7359 dst_ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, dst_cpu
)->ctx
;
7361 mutex_lock(&src_ctx
->mutex
);
7362 list_for_each_entry_safe(event
, tmp
, &src_ctx
->event_list
,
7364 perf_remove_from_context(event
, false);
7365 unaccount_event_cpu(event
, src_cpu
);
7367 list_add(&event
->migrate_entry
, &events
);
7369 mutex_unlock(&src_ctx
->mutex
);
7373 mutex_lock(&dst_ctx
->mutex
);
7374 list_for_each_entry_safe(event
, tmp
, &events
, migrate_entry
) {
7375 list_del(&event
->migrate_entry
);
7376 if (event
->state
>= PERF_EVENT_STATE_OFF
)
7377 event
->state
= PERF_EVENT_STATE_INACTIVE
;
7378 account_event_cpu(event
, dst_cpu
);
7379 perf_install_in_context(dst_ctx
, event
, dst_cpu
);
7382 mutex_unlock(&dst_ctx
->mutex
);
7384 EXPORT_SYMBOL_GPL(perf_pmu_migrate_context
);
7386 static void sync_child_event(struct perf_event
*child_event
,
7387 struct task_struct
*child
)
7389 struct perf_event
*parent_event
= child_event
->parent
;
7392 if (child_event
->attr
.inherit_stat
)
7393 perf_event_read_event(child_event
, child
);
7395 child_val
= perf_event_count(child_event
);
7398 * Add back the child's count to the parent's count:
7400 atomic64_add(child_val
, &parent_event
->child_count
);
7401 atomic64_add(child_event
->total_time_enabled
,
7402 &parent_event
->child_total_time_enabled
);
7403 atomic64_add(child_event
->total_time_running
,
7404 &parent_event
->child_total_time_running
);
7407 * Remove this event from the parent's list
7409 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
7410 mutex_lock(&parent_event
->child_mutex
);
7411 list_del_init(&child_event
->child_list
);
7412 mutex_unlock(&parent_event
->child_mutex
);
7415 * Release the parent event, if this was the last
7418 put_event(parent_event
);
7422 __perf_event_exit_task(struct perf_event
*child_event
,
7423 struct perf_event_context
*child_ctx
,
7424 struct task_struct
*child
)
7426 perf_remove_from_context(child_event
, true);
7429 * It can happen that the parent exits first, and has events
7430 * that are still around due to the child reference. These
7431 * events need to be zapped.
7433 if (child_event
->parent
) {
7434 sync_child_event(child_event
, child
);
7435 free_event(child_event
);
7439 static void perf_event_exit_task_context(struct task_struct
*child
, int ctxn
)
7441 struct perf_event
*child_event
, *next
;
7442 struct perf_event_context
*child_ctx
;
7443 unsigned long flags
;
7445 if (likely(!child
->perf_event_ctxp
[ctxn
])) {
7446 perf_event_task(child
, NULL
, 0);
7450 local_irq_save(flags
);
7452 * We can't reschedule here because interrupts are disabled,
7453 * and either child is current or it is a task that can't be
7454 * scheduled, so we are now safe from rescheduling changing
7457 child_ctx
= rcu_dereference_raw(child
->perf_event_ctxp
[ctxn
]);
7460 * Take the context lock here so that if find_get_context is
7461 * reading child->perf_event_ctxp, we wait until it has
7462 * incremented the context's refcount before we do put_ctx below.
7464 raw_spin_lock(&child_ctx
->lock
);
7465 task_ctx_sched_out(child_ctx
);
7466 child
->perf_event_ctxp
[ctxn
] = NULL
;
7468 * If this context is a clone; unclone it so it can't get
7469 * swapped to another process while we're removing all
7470 * the events from it.
7472 unclone_ctx(child_ctx
);
7473 update_context_time(child_ctx
);
7474 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
7477 * Report the task dead after unscheduling the events so that we
7478 * won't get any samples after PERF_RECORD_EXIT. We can however still
7479 * get a few PERF_RECORD_READ events.
7481 perf_event_task(child
, child_ctx
, 0);
7484 * We can recurse on the same lock type through:
7486 * __perf_event_exit_task()
7487 * sync_child_event()
7489 * mutex_lock(&ctx->mutex)
7491 * But since its the parent context it won't be the same instance.
7493 mutex_lock(&child_ctx
->mutex
);
7495 list_for_each_entry_safe(child_event
, next
, &child_ctx
->event_list
, event_entry
)
7496 __perf_event_exit_task(child_event
, child_ctx
, child
);
7498 mutex_unlock(&child_ctx
->mutex
);
7504 * When a child task exits, feed back event values to parent events.
7506 void perf_event_exit_task(struct task_struct
*child
)
7508 struct perf_event
*event
, *tmp
;
7511 mutex_lock(&child
->perf_event_mutex
);
7512 list_for_each_entry_safe(event
, tmp
, &child
->perf_event_list
,
7514 list_del_init(&event
->owner_entry
);
7517 * Ensure the list deletion is visible before we clear
7518 * the owner, closes a race against perf_release() where
7519 * we need to serialize on the owner->perf_event_mutex.
7522 event
->owner
= NULL
;
7524 mutex_unlock(&child
->perf_event_mutex
);
7526 for_each_task_context_nr(ctxn
)
7527 perf_event_exit_task_context(child
, ctxn
);
7530 static void perf_free_event(struct perf_event
*event
,
7531 struct perf_event_context
*ctx
)
7533 struct perf_event
*parent
= event
->parent
;
7535 if (WARN_ON_ONCE(!parent
))
7538 mutex_lock(&parent
->child_mutex
);
7539 list_del_init(&event
->child_list
);
7540 mutex_unlock(&parent
->child_mutex
);
7544 perf_group_detach(event
);
7545 list_del_event(event
, ctx
);
7550 * free an unexposed, unused context as created by inheritance by
7551 * perf_event_init_task below, used by fork() in case of fail.
7553 void perf_event_free_task(struct task_struct
*task
)
7555 struct perf_event_context
*ctx
;
7556 struct perf_event
*event
, *tmp
;
7559 for_each_task_context_nr(ctxn
) {
7560 ctx
= task
->perf_event_ctxp
[ctxn
];
7564 mutex_lock(&ctx
->mutex
);
7566 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
,
7568 perf_free_event(event
, ctx
);
7570 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
,
7572 perf_free_event(event
, ctx
);
7574 if (!list_empty(&ctx
->pinned_groups
) ||
7575 !list_empty(&ctx
->flexible_groups
))
7578 mutex_unlock(&ctx
->mutex
);
7584 void perf_event_delayed_put(struct task_struct
*task
)
7588 for_each_task_context_nr(ctxn
)
7589 WARN_ON_ONCE(task
->perf_event_ctxp
[ctxn
]);
7593 * inherit a event from parent task to child task:
7595 static struct perf_event
*
7596 inherit_event(struct perf_event
*parent_event
,
7597 struct task_struct
*parent
,
7598 struct perf_event_context
*parent_ctx
,
7599 struct task_struct
*child
,
7600 struct perf_event
*group_leader
,
7601 struct perf_event_context
*child_ctx
)
7603 struct perf_event
*child_event
;
7604 unsigned long flags
;
7607 * Instead of creating recursive hierarchies of events,
7608 * we link inherited events back to the original parent,
7609 * which has a filp for sure, which we use as the reference
7612 if (parent_event
->parent
)
7613 parent_event
= parent_event
->parent
;
7615 child_event
= perf_event_alloc(&parent_event
->attr
,
7618 group_leader
, parent_event
,
7620 if (IS_ERR(child_event
))
7623 if (!atomic_long_inc_not_zero(&parent_event
->refcount
)) {
7624 free_event(child_event
);
7631 * Make the child state follow the state of the parent event,
7632 * not its attr.disabled bit. We hold the parent's mutex,
7633 * so we won't race with perf_event_{en, dis}able_family.
7635 if (parent_event
->state
>= PERF_EVENT_STATE_INACTIVE
)
7636 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
7638 child_event
->state
= PERF_EVENT_STATE_OFF
;
7640 if (parent_event
->attr
.freq
) {
7641 u64 sample_period
= parent_event
->hw
.sample_period
;
7642 struct hw_perf_event
*hwc
= &child_event
->hw
;
7644 hwc
->sample_period
= sample_period
;
7645 hwc
->last_period
= sample_period
;
7647 local64_set(&hwc
->period_left
, sample_period
);
7650 child_event
->ctx
= child_ctx
;
7651 child_event
->overflow_handler
= parent_event
->overflow_handler
;
7652 child_event
->overflow_handler_context
7653 = parent_event
->overflow_handler_context
;
7656 * Precalculate sample_data sizes
7658 perf_event__header_size(child_event
);
7659 perf_event__id_header_size(child_event
);
7662 * Link it up in the child's context:
7664 raw_spin_lock_irqsave(&child_ctx
->lock
, flags
);
7665 add_event_to_ctx(child_event
, child_ctx
);
7666 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
7669 * Link this into the parent event's child list
7671 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
7672 mutex_lock(&parent_event
->child_mutex
);
7673 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
7674 mutex_unlock(&parent_event
->child_mutex
);
7679 static int inherit_group(struct perf_event
*parent_event
,
7680 struct task_struct
*parent
,
7681 struct perf_event_context
*parent_ctx
,
7682 struct task_struct
*child
,
7683 struct perf_event_context
*child_ctx
)
7685 struct perf_event
*leader
;
7686 struct perf_event
*sub
;
7687 struct perf_event
*child_ctr
;
7689 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
7690 child
, NULL
, child_ctx
);
7692 return PTR_ERR(leader
);
7693 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
7694 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
7695 child
, leader
, child_ctx
);
7696 if (IS_ERR(child_ctr
))
7697 return PTR_ERR(child_ctr
);
7703 inherit_task_group(struct perf_event
*event
, struct task_struct
*parent
,
7704 struct perf_event_context
*parent_ctx
,
7705 struct task_struct
*child
, int ctxn
,
7709 struct perf_event_context
*child_ctx
;
7711 if (!event
->attr
.inherit
) {
7716 child_ctx
= child
->perf_event_ctxp
[ctxn
];
7719 * This is executed from the parent task context, so
7720 * inherit events that have been marked for cloning.
7721 * First allocate and initialize a context for the
7725 child_ctx
= alloc_perf_context(parent_ctx
->pmu
, child
);
7729 child
->perf_event_ctxp
[ctxn
] = child_ctx
;
7732 ret
= inherit_group(event
, parent
, parent_ctx
,
7742 * Initialize the perf_event context in task_struct
7744 int perf_event_init_context(struct task_struct
*child
, int ctxn
)
7746 struct perf_event_context
*child_ctx
, *parent_ctx
;
7747 struct perf_event_context
*cloned_ctx
;
7748 struct perf_event
*event
;
7749 struct task_struct
*parent
= current
;
7750 int inherited_all
= 1;
7751 unsigned long flags
;
7754 if (likely(!parent
->perf_event_ctxp
[ctxn
]))
7758 * If the parent's context is a clone, pin it so it won't get
7761 parent_ctx
= perf_pin_task_context(parent
, ctxn
);
7766 * No need to check if parent_ctx != NULL here; since we saw
7767 * it non-NULL earlier, the only reason for it to become NULL
7768 * is if we exit, and since we're currently in the middle of
7769 * a fork we can't be exiting at the same time.
7773 * Lock the parent list. No need to lock the child - not PID
7774 * hashed yet and not running, so nobody can access it.
7776 mutex_lock(&parent_ctx
->mutex
);
7779 * We dont have to disable NMIs - we are only looking at
7780 * the list, not manipulating it:
7782 list_for_each_entry(event
, &parent_ctx
->pinned_groups
, group_entry
) {
7783 ret
= inherit_task_group(event
, parent
, parent_ctx
,
7784 child
, ctxn
, &inherited_all
);
7790 * We can't hold ctx->lock when iterating the ->flexible_group list due
7791 * to allocations, but we need to prevent rotation because
7792 * rotate_ctx() will change the list from interrupt context.
7794 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
7795 parent_ctx
->rotate_disable
= 1;
7796 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
7798 list_for_each_entry(event
, &parent_ctx
->flexible_groups
, group_entry
) {
7799 ret
= inherit_task_group(event
, parent
, parent_ctx
,
7800 child
, ctxn
, &inherited_all
);
7805 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
7806 parent_ctx
->rotate_disable
= 0;
7808 child_ctx
= child
->perf_event_ctxp
[ctxn
];
7810 if (child_ctx
&& inherited_all
) {
7812 * Mark the child context as a clone of the parent
7813 * context, or of whatever the parent is a clone of.
7815 * Note that if the parent is a clone, the holding of
7816 * parent_ctx->lock avoids it from being uncloned.
7818 cloned_ctx
= parent_ctx
->parent_ctx
;
7820 child_ctx
->parent_ctx
= cloned_ctx
;
7821 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
7823 child_ctx
->parent_ctx
= parent_ctx
;
7824 child_ctx
->parent_gen
= parent_ctx
->generation
;
7826 get_ctx(child_ctx
->parent_ctx
);
7829 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
7830 mutex_unlock(&parent_ctx
->mutex
);
7832 perf_unpin_context(parent_ctx
);
7833 put_ctx(parent_ctx
);
7839 * Initialize the perf_event context in task_struct
7841 int perf_event_init_task(struct task_struct
*child
)
7845 memset(child
->perf_event_ctxp
, 0, sizeof(child
->perf_event_ctxp
));
7846 mutex_init(&child
->perf_event_mutex
);
7847 INIT_LIST_HEAD(&child
->perf_event_list
);
7849 for_each_task_context_nr(ctxn
) {
7850 ret
= perf_event_init_context(child
, ctxn
);
7858 static void __init
perf_event_init_all_cpus(void)
7860 struct swevent_htable
*swhash
;
7863 for_each_possible_cpu(cpu
) {
7864 swhash
= &per_cpu(swevent_htable
, cpu
);
7865 mutex_init(&swhash
->hlist_mutex
);
7866 INIT_LIST_HEAD(&per_cpu(rotation_list
, cpu
));
7870 static void perf_event_init_cpu(int cpu
)
7872 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
7874 mutex_lock(&swhash
->hlist_mutex
);
7875 if (swhash
->hlist_refcount
> 0) {
7876 struct swevent_hlist
*hlist
;
7878 hlist
= kzalloc_node(sizeof(*hlist
), GFP_KERNEL
, cpu_to_node(cpu
));
7880 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
7882 mutex_unlock(&swhash
->hlist_mutex
);
7885 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7886 static void perf_pmu_rotate_stop(struct pmu
*pmu
)
7888 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
7890 WARN_ON(!irqs_disabled());
7892 list_del_init(&cpuctx
->rotation_list
);
7895 static void __perf_event_exit_context(void *__info
)
7897 struct remove_event re
= { .detach_group
= false };
7898 struct perf_event_context
*ctx
= __info
;
7900 perf_pmu_rotate_stop(ctx
->pmu
);
7903 list_for_each_entry_rcu(re
.event
, &ctx
->event_list
, event_entry
)
7904 __perf_remove_from_context(&re
);
7908 static void perf_event_exit_cpu_context(int cpu
)
7910 struct perf_event_context
*ctx
;
7914 idx
= srcu_read_lock(&pmus_srcu
);
7915 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
7916 ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
)->ctx
;
7918 mutex_lock(&ctx
->mutex
);
7919 smp_call_function_single(cpu
, __perf_event_exit_context
, ctx
, 1);
7920 mutex_unlock(&ctx
->mutex
);
7922 srcu_read_unlock(&pmus_srcu
, idx
);
7925 static void perf_event_exit_cpu(int cpu
)
7927 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
7929 perf_event_exit_cpu_context(cpu
);
7931 mutex_lock(&swhash
->hlist_mutex
);
7932 swevent_hlist_release(swhash
);
7933 mutex_unlock(&swhash
->hlist_mutex
);
7936 static inline void perf_event_exit_cpu(int cpu
) { }
7940 perf_reboot(struct notifier_block
*notifier
, unsigned long val
, void *v
)
7944 for_each_online_cpu(cpu
)
7945 perf_event_exit_cpu(cpu
);
7951 * Run the perf reboot notifier at the very last possible moment so that
7952 * the generic watchdog code runs as long as possible.
7954 static struct notifier_block perf_reboot_notifier
= {
7955 .notifier_call
= perf_reboot
,
7956 .priority
= INT_MIN
,
7960 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
7962 unsigned int cpu
= (long)hcpu
;
7964 switch (action
& ~CPU_TASKS_FROZEN
) {
7966 case CPU_UP_PREPARE
:
7967 case CPU_DOWN_FAILED
:
7968 perf_event_init_cpu(cpu
);
7971 case CPU_UP_CANCELED
:
7972 case CPU_DOWN_PREPARE
:
7973 perf_event_exit_cpu(cpu
);
7982 void __init
perf_event_init(void)
7988 perf_event_init_all_cpus();
7989 init_srcu_struct(&pmus_srcu
);
7990 perf_pmu_register(&perf_swevent
, "software", PERF_TYPE_SOFTWARE
);
7991 perf_pmu_register(&perf_cpu_clock
, NULL
, -1);
7992 perf_pmu_register(&perf_task_clock
, NULL
, -1);
7994 perf_cpu_notifier(perf_cpu_notify
);
7995 register_reboot_notifier(&perf_reboot_notifier
);
7997 ret
= init_hw_breakpoint();
7998 WARN(ret
, "hw_breakpoint initialization failed with: %d", ret
);
8000 /* do not patch jump label more than once per second */
8001 jump_label_rate_limit(&perf_sched_events
, HZ
);
8004 * Build time assertion that we keep the data_head at the intended
8005 * location. IOW, validation we got the __reserved[] size right.
8007 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page
, data_head
))
8011 static int __init
perf_event_sysfs_init(void)
8016 mutex_lock(&pmus_lock
);
8018 ret
= bus_register(&pmu_bus
);
8022 list_for_each_entry(pmu
, &pmus
, entry
) {
8023 if (!pmu
->name
|| pmu
->type
< 0)
8026 ret
= pmu_dev_alloc(pmu
);
8027 WARN(ret
, "Failed to register pmu: %s, reason %d\n", pmu
->name
, ret
);
8029 pmu_bus_running
= 1;
8033 mutex_unlock(&pmus_lock
);
8037 device_initcall(perf_event_sysfs_init
);
8039 #ifdef CONFIG_CGROUP_PERF
8040 static struct cgroup_subsys_state
*
8041 perf_cgroup_css_alloc(struct cgroup_subsys_state
*parent_css
)
8043 struct perf_cgroup
*jc
;
8045 jc
= kzalloc(sizeof(*jc
), GFP_KERNEL
);
8047 return ERR_PTR(-ENOMEM
);
8049 jc
->info
= alloc_percpu(struct perf_cgroup_info
);
8052 return ERR_PTR(-ENOMEM
);
8058 static void perf_cgroup_css_free(struct cgroup_subsys_state
*css
)
8060 struct perf_cgroup
*jc
= container_of(css
, struct perf_cgroup
, css
);
8062 free_percpu(jc
->info
);
8066 static int __perf_cgroup_move(void *info
)
8068 struct task_struct
*task
= info
;
8069 perf_cgroup_switch(task
, PERF_CGROUP_SWOUT
| PERF_CGROUP_SWIN
);
8073 static void perf_cgroup_attach(struct cgroup_subsys_state
*css
,
8074 struct cgroup_taskset
*tset
)
8076 struct task_struct
*task
;
8078 cgroup_taskset_for_each(task
, tset
)
8079 task_function_call(task
, __perf_cgroup_move
, task
);
8082 static void perf_cgroup_exit(struct cgroup_subsys_state
*css
,
8083 struct cgroup_subsys_state
*old_css
,
8084 struct task_struct
*task
)
8087 * cgroup_exit() is called in the copy_process() failure path.
8088 * Ignore this case since the task hasn't ran yet, this avoids
8089 * trying to poke a half freed task state from generic code.
8091 if (!(task
->flags
& PF_EXITING
))
8094 task_function_call(task
, __perf_cgroup_move
, task
);
8097 struct cgroup_subsys perf_event_cgrp_subsys
= {
8098 .css_alloc
= perf_cgroup_css_alloc
,
8099 .css_free
= perf_cgroup_css_free
,
8100 .exit
= perf_cgroup_exit
,
8101 .attach
= perf_cgroup_attach
,
8103 #endif /* CONFIG_CGROUP_PERF */