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>
43 #include <linux/mman.h>
47 #include <asm/irq_regs.h>
49 struct remote_function_call
{
50 struct task_struct
*p
;
51 int (*func
)(void *info
);
56 static void remote_function(void *data
)
58 struct remote_function_call
*tfc
= data
;
59 struct task_struct
*p
= tfc
->p
;
63 if (task_cpu(p
) != smp_processor_id() || !task_curr(p
))
67 tfc
->ret
= tfc
->func(tfc
->info
);
71 * task_function_call - call a function on the cpu on which a task runs
72 * @p: the task to evaluate
73 * @func: the function to be called
74 * @info: the function call argument
76 * Calls the function @func when the task is currently running. This might
77 * be on the current CPU, which just calls the function directly
79 * returns: @func return value, or
80 * -ESRCH - when the process isn't running
81 * -EAGAIN - when the process moved away
84 task_function_call(struct task_struct
*p
, int (*func
) (void *info
), void *info
)
86 struct remote_function_call data
= {
90 .ret
= -ESRCH
, /* No such (running) process */
94 smp_call_function_single(task_cpu(p
), remote_function
, &data
, 1);
100 * cpu_function_call - call a function on the cpu
101 * @func: the function to be called
102 * @info: the function call argument
104 * Calls the function @func on the remote cpu.
106 * returns: @func return value or -ENXIO when the cpu is offline
108 static int cpu_function_call(int cpu
, int (*func
) (void *info
), void *info
)
110 struct remote_function_call data
= {
114 .ret
= -ENXIO
, /* No such CPU */
117 smp_call_function_single(cpu
, remote_function
, &data
, 1);
122 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
123 PERF_FLAG_FD_OUTPUT |\
124 PERF_FLAG_PID_CGROUP |\
125 PERF_FLAG_FD_CLOEXEC)
128 * branch priv levels that need permission checks
130 #define PERF_SAMPLE_BRANCH_PERM_PLM \
131 (PERF_SAMPLE_BRANCH_KERNEL |\
132 PERF_SAMPLE_BRANCH_HV)
135 EVENT_FLEXIBLE
= 0x1,
137 EVENT_ALL
= EVENT_FLEXIBLE
| EVENT_PINNED
,
141 * perf_sched_events : >0 events exist
142 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
144 struct static_key_deferred perf_sched_events __read_mostly
;
145 static DEFINE_PER_CPU(atomic_t
, perf_cgroup_events
);
146 static DEFINE_PER_CPU(atomic_t
, perf_branch_stack_events
);
148 static atomic_t nr_mmap_events __read_mostly
;
149 static atomic_t nr_comm_events __read_mostly
;
150 static atomic_t nr_task_events __read_mostly
;
151 static atomic_t nr_freq_events __read_mostly
;
153 static LIST_HEAD(pmus
);
154 static DEFINE_MUTEX(pmus_lock
);
155 static struct srcu_struct pmus_srcu
;
158 * perf event paranoia level:
159 * -1 - not paranoid at all
160 * 0 - disallow raw tracepoint access for unpriv
161 * 1 - disallow cpu events for unpriv
162 * 2 - disallow kernel profiling for unpriv
164 int sysctl_perf_event_paranoid __read_mostly
= 1;
166 /* Minimum for 512 kiB + 1 user control page */
167 int sysctl_perf_event_mlock __read_mostly
= 512 + (PAGE_SIZE
/ 1024); /* 'free' kiB per user */
170 * max perf event sample rate
172 #define DEFAULT_MAX_SAMPLE_RATE 100000
173 #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
174 #define DEFAULT_CPU_TIME_MAX_PERCENT 25
176 int sysctl_perf_event_sample_rate __read_mostly
= DEFAULT_MAX_SAMPLE_RATE
;
178 static int max_samples_per_tick __read_mostly
= DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE
, HZ
);
179 static int perf_sample_period_ns __read_mostly
= DEFAULT_SAMPLE_PERIOD_NS
;
181 static int perf_sample_allowed_ns __read_mostly
=
182 DEFAULT_SAMPLE_PERIOD_NS
* DEFAULT_CPU_TIME_MAX_PERCENT
/ 100;
184 void update_perf_cpu_limits(void)
186 u64 tmp
= perf_sample_period_ns
;
188 tmp
*= sysctl_perf_cpu_time_max_percent
;
190 ACCESS_ONCE(perf_sample_allowed_ns
) = tmp
;
193 static int perf_rotate_context(struct perf_cpu_context
*cpuctx
);
195 int perf_proc_update_handler(struct ctl_table
*table
, int write
,
196 void __user
*buffer
, size_t *lenp
,
199 int ret
= proc_dointvec_minmax(table
, write
, buffer
, lenp
, ppos
);
204 max_samples_per_tick
= DIV_ROUND_UP(sysctl_perf_event_sample_rate
, HZ
);
205 perf_sample_period_ns
= NSEC_PER_SEC
/ sysctl_perf_event_sample_rate
;
206 update_perf_cpu_limits();
211 int sysctl_perf_cpu_time_max_percent __read_mostly
= DEFAULT_CPU_TIME_MAX_PERCENT
;
213 int perf_cpu_time_max_percent_handler(struct ctl_table
*table
, int write
,
214 void __user
*buffer
, size_t *lenp
,
217 int ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
222 update_perf_cpu_limits();
228 * perf samples are done in some very critical code paths (NMIs).
229 * If they take too much CPU time, the system can lock up and not
230 * get any real work done. This will drop the sample rate when
231 * we detect that events are taking too long.
233 #define NR_ACCUMULATED_SAMPLES 128
234 static DEFINE_PER_CPU(u64
, running_sample_length
);
236 static void perf_duration_warn(struct irq_work
*w
)
238 u64 allowed_ns
= ACCESS_ONCE(perf_sample_allowed_ns
);
239 u64 avg_local_sample_len
;
240 u64 local_samples_len
;
242 local_samples_len
= __get_cpu_var(running_sample_length
);
243 avg_local_sample_len
= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
245 printk_ratelimited(KERN_WARNING
246 "perf interrupt took too long (%lld > %lld), lowering "
247 "kernel.perf_event_max_sample_rate to %d\n",
248 avg_local_sample_len
, allowed_ns
>> 1,
249 sysctl_perf_event_sample_rate
);
252 static DEFINE_IRQ_WORK(perf_duration_work
, perf_duration_warn
);
254 void perf_sample_event_took(u64 sample_len_ns
)
256 u64 allowed_ns
= ACCESS_ONCE(perf_sample_allowed_ns
);
257 u64 avg_local_sample_len
;
258 u64 local_samples_len
;
263 /* decay the counter by 1 average sample */
264 local_samples_len
= __get_cpu_var(running_sample_length
);
265 local_samples_len
-= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
266 local_samples_len
+= sample_len_ns
;
267 __get_cpu_var(running_sample_length
) = local_samples_len
;
270 * note: this will be biased artifically low until we have
271 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
272 * from having to maintain a count.
274 avg_local_sample_len
= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
276 if (avg_local_sample_len
<= allowed_ns
)
279 if (max_samples_per_tick
<= 1)
282 max_samples_per_tick
= DIV_ROUND_UP(max_samples_per_tick
, 2);
283 sysctl_perf_event_sample_rate
= max_samples_per_tick
* HZ
;
284 perf_sample_period_ns
= NSEC_PER_SEC
/ sysctl_perf_event_sample_rate
;
286 update_perf_cpu_limits();
288 if (!irq_work_queue(&perf_duration_work
)) {
289 early_printk("perf interrupt took too long (%lld > %lld), lowering "
290 "kernel.perf_event_max_sample_rate to %d\n",
291 avg_local_sample_len
, allowed_ns
>> 1,
292 sysctl_perf_event_sample_rate
);
296 static atomic64_t perf_event_id
;
298 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
299 enum event_type_t event_type
);
301 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
302 enum event_type_t event_type
,
303 struct task_struct
*task
);
305 static void update_context_time(struct perf_event_context
*ctx
);
306 static u64
perf_event_time(struct perf_event
*event
);
308 void __weak
perf_event_print_debug(void) { }
310 extern __weak
const char *perf_pmu_name(void)
315 static inline u64
perf_clock(void)
317 return local_clock();
320 static inline struct perf_cpu_context
*
321 __get_cpu_context(struct perf_event_context
*ctx
)
323 return this_cpu_ptr(ctx
->pmu
->pmu_cpu_context
);
326 static void perf_ctx_lock(struct perf_cpu_context
*cpuctx
,
327 struct perf_event_context
*ctx
)
329 raw_spin_lock(&cpuctx
->ctx
.lock
);
331 raw_spin_lock(&ctx
->lock
);
334 static void perf_ctx_unlock(struct perf_cpu_context
*cpuctx
,
335 struct perf_event_context
*ctx
)
338 raw_spin_unlock(&ctx
->lock
);
339 raw_spin_unlock(&cpuctx
->ctx
.lock
);
342 #ifdef CONFIG_CGROUP_PERF
345 * perf_cgroup_info keeps track of time_enabled for a cgroup.
346 * This is a per-cpu dynamically allocated data structure.
348 struct perf_cgroup_info
{
354 struct cgroup_subsys_state css
;
355 struct perf_cgroup_info __percpu
*info
;
359 * Must ensure cgroup is pinned (css_get) before calling
360 * this function. In other words, we cannot call this function
361 * if there is no cgroup event for the current CPU context.
363 static inline struct perf_cgroup
*
364 perf_cgroup_from_task(struct task_struct
*task
)
366 return container_of(task_css(task
, perf_event_cgrp_id
),
367 struct perf_cgroup
, css
);
371 perf_cgroup_match(struct perf_event
*event
)
373 struct perf_event_context
*ctx
= event
->ctx
;
374 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
376 /* @event doesn't care about cgroup */
380 /* wants specific cgroup scope but @cpuctx isn't associated with any */
385 * Cgroup scoping is recursive. An event enabled for a cgroup is
386 * also enabled for all its descendant cgroups. If @cpuctx's
387 * cgroup is a descendant of @event's (the test covers identity
388 * case), it's a match.
390 return cgroup_is_descendant(cpuctx
->cgrp
->css
.cgroup
,
391 event
->cgrp
->css
.cgroup
);
394 static inline void perf_put_cgroup(struct perf_event
*event
)
396 css_put(&event
->cgrp
->css
);
399 static inline void perf_detach_cgroup(struct perf_event
*event
)
401 perf_put_cgroup(event
);
405 static inline int is_cgroup_event(struct perf_event
*event
)
407 return event
->cgrp
!= NULL
;
410 static inline u64
perf_cgroup_event_time(struct perf_event
*event
)
412 struct perf_cgroup_info
*t
;
414 t
= per_cpu_ptr(event
->cgrp
->info
, event
->cpu
);
418 static inline void __update_cgrp_time(struct perf_cgroup
*cgrp
)
420 struct perf_cgroup_info
*info
;
425 info
= this_cpu_ptr(cgrp
->info
);
427 info
->time
+= now
- info
->timestamp
;
428 info
->timestamp
= now
;
431 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context
*cpuctx
)
433 struct perf_cgroup
*cgrp_out
= cpuctx
->cgrp
;
435 __update_cgrp_time(cgrp_out
);
438 static inline void update_cgrp_time_from_event(struct perf_event
*event
)
440 struct perf_cgroup
*cgrp
;
443 * ensure we access cgroup data only when needed and
444 * when we know the cgroup is pinned (css_get)
446 if (!is_cgroup_event(event
))
449 cgrp
= perf_cgroup_from_task(current
);
451 * Do not update time when cgroup is not active
453 if (cgrp
== event
->cgrp
)
454 __update_cgrp_time(event
->cgrp
);
458 perf_cgroup_set_timestamp(struct task_struct
*task
,
459 struct perf_event_context
*ctx
)
461 struct perf_cgroup
*cgrp
;
462 struct perf_cgroup_info
*info
;
465 * ctx->lock held by caller
466 * ensure we do not access cgroup data
467 * unless we have the cgroup pinned (css_get)
469 if (!task
|| !ctx
->nr_cgroups
)
472 cgrp
= perf_cgroup_from_task(task
);
473 info
= this_cpu_ptr(cgrp
->info
);
474 info
->timestamp
= ctx
->timestamp
;
477 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
478 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
481 * reschedule events based on the cgroup constraint of task.
483 * mode SWOUT : schedule out everything
484 * mode SWIN : schedule in based on cgroup for next
486 void perf_cgroup_switch(struct task_struct
*task
, int mode
)
488 struct perf_cpu_context
*cpuctx
;
493 * disable interrupts to avoid geting nr_cgroup
494 * changes via __perf_event_disable(). Also
497 local_irq_save(flags
);
500 * we reschedule only in the presence of cgroup
501 * constrained events.
505 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
506 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
507 if (cpuctx
->unique_pmu
!= pmu
)
508 continue; /* ensure we process each cpuctx once */
511 * perf_cgroup_events says at least one
512 * context on this CPU has cgroup events.
514 * ctx->nr_cgroups reports the number of cgroup
515 * events for a context.
517 if (cpuctx
->ctx
.nr_cgroups
> 0) {
518 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
519 perf_pmu_disable(cpuctx
->ctx
.pmu
);
521 if (mode
& PERF_CGROUP_SWOUT
) {
522 cpu_ctx_sched_out(cpuctx
, EVENT_ALL
);
524 * must not be done before ctxswout due
525 * to event_filter_match() in event_sched_out()
530 if (mode
& PERF_CGROUP_SWIN
) {
531 WARN_ON_ONCE(cpuctx
->cgrp
);
533 * set cgrp before ctxsw in to allow
534 * event_filter_match() to not have to pass
537 cpuctx
->cgrp
= perf_cgroup_from_task(task
);
538 cpu_ctx_sched_in(cpuctx
, EVENT_ALL
, task
);
540 perf_pmu_enable(cpuctx
->ctx
.pmu
);
541 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
547 local_irq_restore(flags
);
550 static inline void perf_cgroup_sched_out(struct task_struct
*task
,
551 struct task_struct
*next
)
553 struct perf_cgroup
*cgrp1
;
554 struct perf_cgroup
*cgrp2
= NULL
;
557 * we come here when we know perf_cgroup_events > 0
559 cgrp1
= perf_cgroup_from_task(task
);
562 * next is NULL when called from perf_event_enable_on_exec()
563 * that will systematically cause a cgroup_switch()
566 cgrp2
= perf_cgroup_from_task(next
);
569 * only schedule out current cgroup events if we know
570 * that we are switching to a different cgroup. Otherwise,
571 * do no touch the cgroup events.
574 perf_cgroup_switch(task
, PERF_CGROUP_SWOUT
);
577 static inline void perf_cgroup_sched_in(struct task_struct
*prev
,
578 struct task_struct
*task
)
580 struct perf_cgroup
*cgrp1
;
581 struct perf_cgroup
*cgrp2
= NULL
;
584 * we come here when we know perf_cgroup_events > 0
586 cgrp1
= perf_cgroup_from_task(task
);
588 /* prev can never be NULL */
589 cgrp2
= perf_cgroup_from_task(prev
);
592 * only need to schedule in cgroup events if we are changing
593 * cgroup during ctxsw. Cgroup events were not scheduled
594 * out of ctxsw out if that was not the case.
597 perf_cgroup_switch(task
, PERF_CGROUP_SWIN
);
600 static inline int perf_cgroup_connect(int fd
, struct perf_event
*event
,
601 struct perf_event_attr
*attr
,
602 struct perf_event
*group_leader
)
604 struct perf_cgroup
*cgrp
;
605 struct cgroup_subsys_state
*css
;
606 struct fd f
= fdget(fd
);
612 css
= css_tryget_online_from_dir(f
.file
->f_dentry
,
613 &perf_event_cgrp_subsys
);
619 cgrp
= container_of(css
, struct perf_cgroup
, css
);
623 * all events in a group must monitor
624 * the same cgroup because a task belongs
625 * to only one perf cgroup at a time
627 if (group_leader
&& group_leader
->cgrp
!= cgrp
) {
628 perf_detach_cgroup(event
);
637 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
639 struct perf_cgroup_info
*t
;
640 t
= per_cpu_ptr(event
->cgrp
->info
, event
->cpu
);
641 event
->shadow_ctx_time
= now
- t
->timestamp
;
645 perf_cgroup_defer_enabled(struct perf_event
*event
)
648 * when the current task's perf cgroup does not match
649 * the event's, we need to remember to call the
650 * perf_mark_enable() function the first time a task with
651 * a matching perf cgroup is scheduled in.
653 if (is_cgroup_event(event
) && !perf_cgroup_match(event
))
654 event
->cgrp_defer_enabled
= 1;
658 perf_cgroup_mark_enabled(struct perf_event
*event
,
659 struct perf_event_context
*ctx
)
661 struct perf_event
*sub
;
662 u64 tstamp
= perf_event_time(event
);
664 if (!event
->cgrp_defer_enabled
)
667 event
->cgrp_defer_enabled
= 0;
669 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
670 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
671 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
) {
672 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
673 sub
->cgrp_defer_enabled
= 0;
677 #else /* !CONFIG_CGROUP_PERF */
680 perf_cgroup_match(struct perf_event
*event
)
685 static inline void perf_detach_cgroup(struct perf_event
*event
)
688 static inline int is_cgroup_event(struct perf_event
*event
)
693 static inline u64
perf_cgroup_event_cgrp_time(struct perf_event
*event
)
698 static inline void update_cgrp_time_from_event(struct perf_event
*event
)
702 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context
*cpuctx
)
706 static inline void perf_cgroup_sched_out(struct task_struct
*task
,
707 struct task_struct
*next
)
711 static inline void perf_cgroup_sched_in(struct task_struct
*prev
,
712 struct task_struct
*task
)
716 static inline int perf_cgroup_connect(pid_t pid
, struct perf_event
*event
,
717 struct perf_event_attr
*attr
,
718 struct perf_event
*group_leader
)
724 perf_cgroup_set_timestamp(struct task_struct
*task
,
725 struct perf_event_context
*ctx
)
730 perf_cgroup_switch(struct task_struct
*task
, struct task_struct
*next
)
735 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
739 static inline u64
perf_cgroup_event_time(struct perf_event
*event
)
745 perf_cgroup_defer_enabled(struct perf_event
*event
)
750 perf_cgroup_mark_enabled(struct perf_event
*event
,
751 struct perf_event_context
*ctx
)
757 * set default to be dependent on timer tick just
760 #define PERF_CPU_HRTIMER (1000 / HZ)
762 * function must be called with interrupts disbled
764 static enum hrtimer_restart
perf_cpu_hrtimer_handler(struct hrtimer
*hr
)
766 struct perf_cpu_context
*cpuctx
;
767 enum hrtimer_restart ret
= HRTIMER_NORESTART
;
770 WARN_ON(!irqs_disabled());
772 cpuctx
= container_of(hr
, struct perf_cpu_context
, hrtimer
);
774 rotations
= perf_rotate_context(cpuctx
);
777 * arm timer if needed
780 hrtimer_forward_now(hr
, cpuctx
->hrtimer_interval
);
781 ret
= HRTIMER_RESTART
;
787 /* CPU is going down */
788 void perf_cpu_hrtimer_cancel(int cpu
)
790 struct perf_cpu_context
*cpuctx
;
794 if (WARN_ON(cpu
!= smp_processor_id()))
797 local_irq_save(flags
);
801 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
802 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
804 if (pmu
->task_ctx_nr
== perf_sw_context
)
807 hrtimer_cancel(&cpuctx
->hrtimer
);
812 local_irq_restore(flags
);
815 static void __perf_cpu_hrtimer_init(struct perf_cpu_context
*cpuctx
, int cpu
)
817 struct hrtimer
*hr
= &cpuctx
->hrtimer
;
818 struct pmu
*pmu
= cpuctx
->ctx
.pmu
;
821 /* no multiplexing needed for SW PMU */
822 if (pmu
->task_ctx_nr
== perf_sw_context
)
826 * check default is sane, if not set then force to
827 * default interval (1/tick)
829 timer
= pmu
->hrtimer_interval_ms
;
831 timer
= pmu
->hrtimer_interval_ms
= PERF_CPU_HRTIMER
;
833 cpuctx
->hrtimer_interval
= ns_to_ktime(NSEC_PER_MSEC
* timer
);
835 hrtimer_init(hr
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL_PINNED
);
836 hr
->function
= perf_cpu_hrtimer_handler
;
839 static void perf_cpu_hrtimer_restart(struct perf_cpu_context
*cpuctx
)
841 struct hrtimer
*hr
= &cpuctx
->hrtimer
;
842 struct pmu
*pmu
= cpuctx
->ctx
.pmu
;
845 if (pmu
->task_ctx_nr
== perf_sw_context
)
848 if (hrtimer_active(hr
))
851 if (!hrtimer_callback_running(hr
))
852 __hrtimer_start_range_ns(hr
, cpuctx
->hrtimer_interval
,
853 0, HRTIMER_MODE_REL_PINNED
, 0);
856 void perf_pmu_disable(struct pmu
*pmu
)
858 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
860 pmu
->pmu_disable(pmu
);
863 void perf_pmu_enable(struct pmu
*pmu
)
865 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
867 pmu
->pmu_enable(pmu
);
870 static DEFINE_PER_CPU(struct list_head
, rotation_list
);
873 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
874 * because they're strictly cpu affine and rotate_start is called with IRQs
875 * disabled, while rotate_context is called from IRQ context.
877 static void perf_pmu_rotate_start(struct pmu
*pmu
)
879 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
880 struct list_head
*head
= &__get_cpu_var(rotation_list
);
882 WARN_ON(!irqs_disabled());
884 if (list_empty(&cpuctx
->rotation_list
))
885 list_add(&cpuctx
->rotation_list
, head
);
888 static void get_ctx(struct perf_event_context
*ctx
)
890 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
893 static void put_ctx(struct perf_event_context
*ctx
)
895 if (atomic_dec_and_test(&ctx
->refcount
)) {
897 put_ctx(ctx
->parent_ctx
);
899 put_task_struct(ctx
->task
);
900 kfree_rcu(ctx
, rcu_head
);
904 static void unclone_ctx(struct perf_event_context
*ctx
)
906 if (ctx
->parent_ctx
) {
907 put_ctx(ctx
->parent_ctx
);
908 ctx
->parent_ctx
= NULL
;
913 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
916 * only top level events have the pid namespace they were created in
919 event
= event
->parent
;
921 return task_tgid_nr_ns(p
, event
->ns
);
924 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
927 * only top level events have the pid namespace they were created in
930 event
= event
->parent
;
932 return task_pid_nr_ns(p
, event
->ns
);
936 * If we inherit events we want to return the parent event id
939 static u64
primary_event_id(struct perf_event
*event
)
944 id
= event
->parent
->id
;
950 * Get the perf_event_context for a task and lock it.
951 * This has to cope with with the fact that until it is locked,
952 * the context could get moved to another task.
954 static struct perf_event_context
*
955 perf_lock_task_context(struct task_struct
*task
, int ctxn
, unsigned long *flags
)
957 struct perf_event_context
*ctx
;
961 * One of the few rules of preemptible RCU is that one cannot do
962 * rcu_read_unlock() while holding a scheduler (or nested) lock when
963 * part of the read side critical section was preemptible -- see
964 * rcu_read_unlock_special().
966 * Since ctx->lock nests under rq->lock we must ensure the entire read
967 * side critical section is non-preemptible.
971 ctx
= rcu_dereference(task
->perf_event_ctxp
[ctxn
]);
974 * If this context is a clone of another, it might
975 * get swapped for another underneath us by
976 * perf_event_task_sched_out, though the
977 * rcu_read_lock() protects us from any context
978 * getting freed. Lock the context and check if it
979 * got swapped before we could get the lock, and retry
980 * if so. If we locked the right context, then it
981 * can't get swapped on us any more.
983 raw_spin_lock_irqsave(&ctx
->lock
, *flags
);
984 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
[ctxn
])) {
985 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
991 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
992 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
1002 * Get the context for a task and increment its pin_count so it
1003 * can't get swapped to another task. This also increments its
1004 * reference count so that the context can't get freed.
1006 static struct perf_event_context
*
1007 perf_pin_task_context(struct task_struct
*task
, int ctxn
)
1009 struct perf_event_context
*ctx
;
1010 unsigned long flags
;
1012 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
1015 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1020 static void perf_unpin_context(struct perf_event_context
*ctx
)
1022 unsigned long flags
;
1024 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
1026 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1030 * Update the record of the current time in a context.
1032 static void update_context_time(struct perf_event_context
*ctx
)
1034 u64 now
= perf_clock();
1036 ctx
->time
+= now
- ctx
->timestamp
;
1037 ctx
->timestamp
= now
;
1040 static u64
perf_event_time(struct perf_event
*event
)
1042 struct perf_event_context
*ctx
= event
->ctx
;
1044 if (is_cgroup_event(event
))
1045 return perf_cgroup_event_time(event
);
1047 return ctx
? ctx
->time
: 0;
1051 * Update the total_time_enabled and total_time_running fields for a event.
1052 * The caller of this function needs to hold the ctx->lock.
1054 static void update_event_times(struct perf_event
*event
)
1056 struct perf_event_context
*ctx
= event
->ctx
;
1059 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
1060 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
1063 * in cgroup mode, time_enabled represents
1064 * the time the event was enabled AND active
1065 * tasks were in the monitored cgroup. This is
1066 * independent of the activity of the context as
1067 * there may be a mix of cgroup and non-cgroup events.
1069 * That is why we treat cgroup events differently
1072 if (is_cgroup_event(event
))
1073 run_end
= perf_cgroup_event_time(event
);
1074 else if (ctx
->is_active
)
1075 run_end
= ctx
->time
;
1077 run_end
= event
->tstamp_stopped
;
1079 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
1081 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
1082 run_end
= event
->tstamp_stopped
;
1084 run_end
= perf_event_time(event
);
1086 event
->total_time_running
= run_end
- event
->tstamp_running
;
1091 * Update total_time_enabled and total_time_running for all events in a group.
1093 static void update_group_times(struct perf_event
*leader
)
1095 struct perf_event
*event
;
1097 update_event_times(leader
);
1098 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
1099 update_event_times(event
);
1102 static struct list_head
*
1103 ctx_group_list(struct perf_event
*event
, struct perf_event_context
*ctx
)
1105 if (event
->attr
.pinned
)
1106 return &ctx
->pinned_groups
;
1108 return &ctx
->flexible_groups
;
1112 * Add a event from the lists for its context.
1113 * Must be called with ctx->mutex and ctx->lock held.
1116 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
1118 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_CONTEXT
);
1119 event
->attach_state
|= PERF_ATTACH_CONTEXT
;
1122 * If we're a stand alone event or group leader, we go to the context
1123 * list, group events are kept attached to the group so that
1124 * perf_group_detach can, at all times, locate all siblings.
1126 if (event
->group_leader
== event
) {
1127 struct list_head
*list
;
1129 if (is_software_event(event
))
1130 event
->group_flags
|= PERF_GROUP_SOFTWARE
;
1132 list
= ctx_group_list(event
, ctx
);
1133 list_add_tail(&event
->group_entry
, list
);
1136 if (is_cgroup_event(event
))
1139 if (has_branch_stack(event
))
1140 ctx
->nr_branch_stack
++;
1142 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
1143 if (!ctx
->nr_events
)
1144 perf_pmu_rotate_start(ctx
->pmu
);
1146 if (event
->attr
.inherit_stat
)
1153 * Initialize event state based on the perf_event_attr::disabled.
1155 static inline void perf_event__state_init(struct perf_event
*event
)
1157 event
->state
= event
->attr
.disabled
? PERF_EVENT_STATE_OFF
:
1158 PERF_EVENT_STATE_INACTIVE
;
1162 * Called at perf_event creation and when events are attached/detached from a
1165 static void perf_event__read_size(struct perf_event
*event
)
1167 int entry
= sizeof(u64
); /* value */
1171 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1172 size
+= sizeof(u64
);
1174 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1175 size
+= sizeof(u64
);
1177 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
1178 entry
+= sizeof(u64
);
1180 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
1181 nr
+= event
->group_leader
->nr_siblings
;
1182 size
+= sizeof(u64
);
1186 event
->read_size
= size
;
1189 static void perf_event__header_size(struct perf_event
*event
)
1191 struct perf_sample_data
*data
;
1192 u64 sample_type
= event
->attr
.sample_type
;
1195 perf_event__read_size(event
);
1197 if (sample_type
& PERF_SAMPLE_IP
)
1198 size
+= sizeof(data
->ip
);
1200 if (sample_type
& PERF_SAMPLE_ADDR
)
1201 size
+= sizeof(data
->addr
);
1203 if (sample_type
& PERF_SAMPLE_PERIOD
)
1204 size
+= sizeof(data
->period
);
1206 if (sample_type
& PERF_SAMPLE_WEIGHT
)
1207 size
+= sizeof(data
->weight
);
1209 if (sample_type
& PERF_SAMPLE_READ
)
1210 size
+= event
->read_size
;
1212 if (sample_type
& PERF_SAMPLE_DATA_SRC
)
1213 size
+= sizeof(data
->data_src
.val
);
1215 if (sample_type
& PERF_SAMPLE_TRANSACTION
)
1216 size
+= sizeof(data
->txn
);
1218 event
->header_size
= size
;
1221 static void perf_event__id_header_size(struct perf_event
*event
)
1223 struct perf_sample_data
*data
;
1224 u64 sample_type
= event
->attr
.sample_type
;
1227 if (sample_type
& PERF_SAMPLE_TID
)
1228 size
+= sizeof(data
->tid_entry
);
1230 if (sample_type
& PERF_SAMPLE_TIME
)
1231 size
+= sizeof(data
->time
);
1233 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
1234 size
+= sizeof(data
->id
);
1236 if (sample_type
& PERF_SAMPLE_ID
)
1237 size
+= sizeof(data
->id
);
1239 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
1240 size
+= sizeof(data
->stream_id
);
1242 if (sample_type
& PERF_SAMPLE_CPU
)
1243 size
+= sizeof(data
->cpu_entry
);
1245 event
->id_header_size
= size
;
1248 static void perf_group_attach(struct perf_event
*event
)
1250 struct perf_event
*group_leader
= event
->group_leader
, *pos
;
1253 * We can have double attach due to group movement in perf_event_open.
1255 if (event
->attach_state
& PERF_ATTACH_GROUP
)
1258 event
->attach_state
|= PERF_ATTACH_GROUP
;
1260 if (group_leader
== event
)
1263 if (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
&&
1264 !is_software_event(event
))
1265 group_leader
->group_flags
&= ~PERF_GROUP_SOFTWARE
;
1267 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
1268 group_leader
->nr_siblings
++;
1270 perf_event__header_size(group_leader
);
1272 list_for_each_entry(pos
, &group_leader
->sibling_list
, group_entry
)
1273 perf_event__header_size(pos
);
1277 * Remove a event from the lists for its context.
1278 * Must be called with ctx->mutex and ctx->lock held.
1281 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
1283 struct perf_cpu_context
*cpuctx
;
1285 * We can have double detach due to exit/hot-unplug + close.
1287 if (!(event
->attach_state
& PERF_ATTACH_CONTEXT
))
1290 event
->attach_state
&= ~PERF_ATTACH_CONTEXT
;
1292 if (is_cgroup_event(event
)) {
1294 cpuctx
= __get_cpu_context(ctx
);
1296 * if there are no more cgroup events
1297 * then cler cgrp to avoid stale pointer
1298 * in update_cgrp_time_from_cpuctx()
1300 if (!ctx
->nr_cgroups
)
1301 cpuctx
->cgrp
= NULL
;
1304 if (has_branch_stack(event
))
1305 ctx
->nr_branch_stack
--;
1308 if (event
->attr
.inherit_stat
)
1311 list_del_rcu(&event
->event_entry
);
1313 if (event
->group_leader
== event
)
1314 list_del_init(&event
->group_entry
);
1316 update_group_times(event
);
1319 * If event was in error state, then keep it
1320 * that way, otherwise bogus counts will be
1321 * returned on read(). The only way to get out
1322 * of error state is by explicit re-enabling
1325 if (event
->state
> PERF_EVENT_STATE_OFF
)
1326 event
->state
= PERF_EVENT_STATE_OFF
;
1331 static void perf_group_detach(struct perf_event
*event
)
1333 struct perf_event
*sibling
, *tmp
;
1334 struct list_head
*list
= NULL
;
1337 * We can have double detach due to exit/hot-unplug + close.
1339 if (!(event
->attach_state
& PERF_ATTACH_GROUP
))
1342 event
->attach_state
&= ~PERF_ATTACH_GROUP
;
1345 * If this is a sibling, remove it from its group.
1347 if (event
->group_leader
!= event
) {
1348 list_del_init(&event
->group_entry
);
1349 event
->group_leader
->nr_siblings
--;
1353 if (!list_empty(&event
->group_entry
))
1354 list
= &event
->group_entry
;
1357 * If this was a group event with sibling events then
1358 * upgrade the siblings to singleton events by adding them
1359 * to whatever list we are on.
1361 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
1363 list_move_tail(&sibling
->group_entry
, list
);
1364 sibling
->group_leader
= sibling
;
1366 /* Inherit group flags from the previous leader */
1367 sibling
->group_flags
= event
->group_flags
;
1371 perf_event__header_size(event
->group_leader
);
1373 list_for_each_entry(tmp
, &event
->group_leader
->sibling_list
, group_entry
)
1374 perf_event__header_size(tmp
);
1378 event_filter_match(struct perf_event
*event
)
1380 return (event
->cpu
== -1 || event
->cpu
== smp_processor_id())
1381 && perf_cgroup_match(event
);
1385 event_sched_out(struct perf_event
*event
,
1386 struct perf_cpu_context
*cpuctx
,
1387 struct perf_event_context
*ctx
)
1389 u64 tstamp
= perf_event_time(event
);
1392 * An event which could not be activated because of
1393 * filter mismatch still needs to have its timings
1394 * maintained, otherwise bogus information is return
1395 * via read() for time_enabled, time_running:
1397 if (event
->state
== PERF_EVENT_STATE_INACTIVE
1398 && !event_filter_match(event
)) {
1399 delta
= tstamp
- event
->tstamp_stopped
;
1400 event
->tstamp_running
+= delta
;
1401 event
->tstamp_stopped
= tstamp
;
1404 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1407 perf_pmu_disable(event
->pmu
);
1409 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1410 if (event
->pending_disable
) {
1411 event
->pending_disable
= 0;
1412 event
->state
= PERF_EVENT_STATE_OFF
;
1414 event
->tstamp_stopped
= tstamp
;
1415 event
->pmu
->del(event
, 0);
1418 if (!is_software_event(event
))
1419 cpuctx
->active_oncpu
--;
1421 if (event
->attr
.freq
&& event
->attr
.sample_freq
)
1423 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
1424 cpuctx
->exclusive
= 0;
1426 perf_pmu_enable(event
->pmu
);
1430 group_sched_out(struct perf_event
*group_event
,
1431 struct perf_cpu_context
*cpuctx
,
1432 struct perf_event_context
*ctx
)
1434 struct perf_event
*event
;
1435 int state
= group_event
->state
;
1437 event_sched_out(group_event
, cpuctx
, ctx
);
1440 * Schedule out siblings (if any):
1442 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
1443 event_sched_out(event
, cpuctx
, ctx
);
1445 if (state
== PERF_EVENT_STATE_ACTIVE
&& group_event
->attr
.exclusive
)
1446 cpuctx
->exclusive
= 0;
1449 struct remove_event
{
1450 struct perf_event
*event
;
1455 * Cross CPU call to remove a performance event
1457 * We disable the event on the hardware level first. After that we
1458 * remove it from the context list.
1460 static int __perf_remove_from_context(void *info
)
1462 struct remove_event
*re
= info
;
1463 struct perf_event
*event
= re
->event
;
1464 struct perf_event_context
*ctx
= event
->ctx
;
1465 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1467 raw_spin_lock(&ctx
->lock
);
1468 event_sched_out(event
, cpuctx
, ctx
);
1469 if (re
->detach_group
)
1470 perf_group_detach(event
);
1471 list_del_event(event
, ctx
);
1472 if (!ctx
->nr_events
&& cpuctx
->task_ctx
== ctx
) {
1474 cpuctx
->task_ctx
= NULL
;
1476 raw_spin_unlock(&ctx
->lock
);
1483 * Remove the event from a task's (or a CPU's) list of events.
1485 * CPU events are removed with a smp call. For task events we only
1486 * call when the task is on a CPU.
1488 * If event->ctx is a cloned context, callers must make sure that
1489 * every task struct that event->ctx->task could possibly point to
1490 * remains valid. This is OK when called from perf_release since
1491 * that only calls us on the top-level context, which can't be a clone.
1492 * When called from perf_event_exit_task, it's OK because the
1493 * context has been detached from its task.
1495 static void perf_remove_from_context(struct perf_event
*event
, bool detach_group
)
1497 struct perf_event_context
*ctx
= event
->ctx
;
1498 struct task_struct
*task
= ctx
->task
;
1499 struct remove_event re
= {
1501 .detach_group
= detach_group
,
1504 lockdep_assert_held(&ctx
->mutex
);
1508 * Per cpu events are removed via an smp call and
1509 * the removal is always successful.
1511 cpu_function_call(event
->cpu
, __perf_remove_from_context
, &re
);
1516 if (!task_function_call(task
, __perf_remove_from_context
, &re
))
1519 raw_spin_lock_irq(&ctx
->lock
);
1521 * If we failed to find a running task, but find the context active now
1522 * that we've acquired the ctx->lock, retry.
1524 if (ctx
->is_active
) {
1525 raw_spin_unlock_irq(&ctx
->lock
);
1530 * Since the task isn't running, its safe to remove the event, us
1531 * holding the ctx->lock ensures the task won't get scheduled in.
1534 perf_group_detach(event
);
1535 list_del_event(event
, ctx
);
1536 raw_spin_unlock_irq(&ctx
->lock
);
1540 * Cross CPU call to disable a performance event
1542 int __perf_event_disable(void *info
)
1544 struct perf_event
*event
= info
;
1545 struct perf_event_context
*ctx
= event
->ctx
;
1546 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1549 * If this is a per-task event, need to check whether this
1550 * event's task is the current task on this cpu.
1552 * Can trigger due to concurrent perf_event_context_sched_out()
1553 * flipping contexts around.
1555 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
1558 raw_spin_lock(&ctx
->lock
);
1561 * If the event is on, turn it off.
1562 * If it is in error state, leave it in error state.
1564 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
) {
1565 update_context_time(ctx
);
1566 update_cgrp_time_from_event(event
);
1567 update_group_times(event
);
1568 if (event
== event
->group_leader
)
1569 group_sched_out(event
, cpuctx
, ctx
);
1571 event_sched_out(event
, cpuctx
, ctx
);
1572 event
->state
= PERF_EVENT_STATE_OFF
;
1575 raw_spin_unlock(&ctx
->lock
);
1583 * If event->ctx is a cloned context, callers must make sure that
1584 * every task struct that event->ctx->task could possibly point to
1585 * remains valid. This condition is satisifed when called through
1586 * perf_event_for_each_child or perf_event_for_each because they
1587 * hold the top-level event's child_mutex, so any descendant that
1588 * goes to exit will block in sync_child_event.
1589 * When called from perf_pending_event it's OK because event->ctx
1590 * is the current context on this CPU and preemption is disabled,
1591 * hence we can't get into perf_event_task_sched_out for this context.
1593 void perf_event_disable(struct perf_event
*event
)
1595 struct perf_event_context
*ctx
= event
->ctx
;
1596 struct task_struct
*task
= ctx
->task
;
1600 * Disable the event on the cpu that it's on
1602 cpu_function_call(event
->cpu
, __perf_event_disable
, event
);
1607 if (!task_function_call(task
, __perf_event_disable
, event
))
1610 raw_spin_lock_irq(&ctx
->lock
);
1612 * If the event is still active, we need to retry the cross-call.
1614 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
1615 raw_spin_unlock_irq(&ctx
->lock
);
1617 * Reload the task pointer, it might have been changed by
1618 * a concurrent perf_event_context_sched_out().
1625 * Since we have the lock this context can't be scheduled
1626 * in, so we can change the state safely.
1628 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1629 update_group_times(event
);
1630 event
->state
= PERF_EVENT_STATE_OFF
;
1632 raw_spin_unlock_irq(&ctx
->lock
);
1634 EXPORT_SYMBOL_GPL(perf_event_disable
);
1636 static void perf_set_shadow_time(struct perf_event
*event
,
1637 struct perf_event_context
*ctx
,
1641 * use the correct time source for the time snapshot
1643 * We could get by without this by leveraging the
1644 * fact that to get to this function, the caller
1645 * has most likely already called update_context_time()
1646 * and update_cgrp_time_xx() and thus both timestamp
1647 * are identical (or very close). Given that tstamp is,
1648 * already adjusted for cgroup, we could say that:
1649 * tstamp - ctx->timestamp
1651 * tstamp - cgrp->timestamp.
1653 * Then, in perf_output_read(), the calculation would
1654 * work with no changes because:
1655 * - event is guaranteed scheduled in
1656 * - no scheduled out in between
1657 * - thus the timestamp would be the same
1659 * But this is a bit hairy.
1661 * So instead, we have an explicit cgroup call to remain
1662 * within the time time source all along. We believe it
1663 * is cleaner and simpler to understand.
1665 if (is_cgroup_event(event
))
1666 perf_cgroup_set_shadow_time(event
, tstamp
);
1668 event
->shadow_ctx_time
= tstamp
- ctx
->timestamp
;
1671 #define MAX_INTERRUPTS (~0ULL)
1673 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1676 event_sched_in(struct perf_event
*event
,
1677 struct perf_cpu_context
*cpuctx
,
1678 struct perf_event_context
*ctx
)
1680 u64 tstamp
= perf_event_time(event
);
1683 lockdep_assert_held(&ctx
->lock
);
1685 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1688 event
->state
= PERF_EVENT_STATE_ACTIVE
;
1689 event
->oncpu
= smp_processor_id();
1692 * Unthrottle events, since we scheduled we might have missed several
1693 * ticks already, also for a heavily scheduling task there is little
1694 * guarantee it'll get a tick in a timely manner.
1696 if (unlikely(event
->hw
.interrupts
== MAX_INTERRUPTS
)) {
1697 perf_log_throttle(event
, 1);
1698 event
->hw
.interrupts
= 0;
1702 * The new state must be visible before we turn it on in the hardware:
1706 perf_pmu_disable(event
->pmu
);
1708 if (event
->pmu
->add(event
, PERF_EF_START
)) {
1709 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1715 event
->tstamp_running
+= tstamp
- event
->tstamp_stopped
;
1717 perf_set_shadow_time(event
, ctx
, tstamp
);
1719 if (!is_software_event(event
))
1720 cpuctx
->active_oncpu
++;
1722 if (event
->attr
.freq
&& event
->attr
.sample_freq
)
1725 if (event
->attr
.exclusive
)
1726 cpuctx
->exclusive
= 1;
1729 perf_pmu_enable(event
->pmu
);
1735 group_sched_in(struct perf_event
*group_event
,
1736 struct perf_cpu_context
*cpuctx
,
1737 struct perf_event_context
*ctx
)
1739 struct perf_event
*event
, *partial_group
= NULL
;
1740 struct pmu
*pmu
= ctx
->pmu
;
1741 u64 now
= ctx
->time
;
1742 bool simulate
= false;
1744 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
1747 pmu
->start_txn(pmu
);
1749 if (event_sched_in(group_event
, cpuctx
, ctx
)) {
1750 pmu
->cancel_txn(pmu
);
1751 perf_cpu_hrtimer_restart(cpuctx
);
1756 * Schedule in siblings as one group (if any):
1758 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
1759 if (event_sched_in(event
, cpuctx
, ctx
)) {
1760 partial_group
= event
;
1765 if (!pmu
->commit_txn(pmu
))
1770 * Groups can be scheduled in as one unit only, so undo any
1771 * partial group before returning:
1772 * The events up to the failed event are scheduled out normally,
1773 * tstamp_stopped will be updated.
1775 * The failed events and the remaining siblings need to have
1776 * their timings updated as if they had gone thru event_sched_in()
1777 * and event_sched_out(). This is required to get consistent timings
1778 * across the group. This also takes care of the case where the group
1779 * could never be scheduled by ensuring tstamp_stopped is set to mark
1780 * the time the event was actually stopped, such that time delta
1781 * calculation in update_event_times() is correct.
1783 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
1784 if (event
== partial_group
)
1788 event
->tstamp_running
+= now
- event
->tstamp_stopped
;
1789 event
->tstamp_stopped
= now
;
1791 event_sched_out(event
, cpuctx
, ctx
);
1794 event_sched_out(group_event
, cpuctx
, ctx
);
1796 pmu
->cancel_txn(pmu
);
1798 perf_cpu_hrtimer_restart(cpuctx
);
1804 * Work out whether we can put this event group on the CPU now.
1806 static int group_can_go_on(struct perf_event
*event
,
1807 struct perf_cpu_context
*cpuctx
,
1811 * Groups consisting entirely of software events can always go on.
1813 if (event
->group_flags
& PERF_GROUP_SOFTWARE
)
1816 * If an exclusive group is already on, no other hardware
1819 if (cpuctx
->exclusive
)
1822 * If this group is exclusive and there are already
1823 * events on the CPU, it can't go on.
1825 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
1828 * Otherwise, try to add it if all previous groups were able
1834 static void add_event_to_ctx(struct perf_event
*event
,
1835 struct perf_event_context
*ctx
)
1837 u64 tstamp
= perf_event_time(event
);
1839 list_add_event(event
, ctx
);
1840 perf_group_attach(event
);
1841 event
->tstamp_enabled
= tstamp
;
1842 event
->tstamp_running
= tstamp
;
1843 event
->tstamp_stopped
= tstamp
;
1846 static void task_ctx_sched_out(struct perf_event_context
*ctx
);
1848 ctx_sched_in(struct perf_event_context
*ctx
,
1849 struct perf_cpu_context
*cpuctx
,
1850 enum event_type_t event_type
,
1851 struct task_struct
*task
);
1853 static void perf_event_sched_in(struct perf_cpu_context
*cpuctx
,
1854 struct perf_event_context
*ctx
,
1855 struct task_struct
*task
)
1857 cpu_ctx_sched_in(cpuctx
, EVENT_PINNED
, task
);
1859 ctx_sched_in(ctx
, cpuctx
, EVENT_PINNED
, task
);
1860 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
, task
);
1862 ctx_sched_in(ctx
, cpuctx
, EVENT_FLEXIBLE
, task
);
1866 * Cross CPU call to install and enable a performance event
1868 * Must be called with ctx->mutex held
1870 static int __perf_install_in_context(void *info
)
1872 struct perf_event
*event
= info
;
1873 struct perf_event_context
*ctx
= event
->ctx
;
1874 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1875 struct perf_event_context
*task_ctx
= cpuctx
->task_ctx
;
1876 struct task_struct
*task
= current
;
1878 perf_ctx_lock(cpuctx
, task_ctx
);
1879 perf_pmu_disable(cpuctx
->ctx
.pmu
);
1882 * If there was an active task_ctx schedule it out.
1885 task_ctx_sched_out(task_ctx
);
1888 * If the context we're installing events in is not the
1889 * active task_ctx, flip them.
1891 if (ctx
->task
&& task_ctx
!= ctx
) {
1893 raw_spin_unlock(&task_ctx
->lock
);
1894 raw_spin_lock(&ctx
->lock
);
1899 cpuctx
->task_ctx
= task_ctx
;
1900 task
= task_ctx
->task
;
1903 cpu_ctx_sched_out(cpuctx
, EVENT_ALL
);
1905 update_context_time(ctx
);
1907 * update cgrp time only if current cgrp
1908 * matches event->cgrp. Must be done before
1909 * calling add_event_to_ctx()
1911 update_cgrp_time_from_event(event
);
1913 add_event_to_ctx(event
, ctx
);
1916 * Schedule everything back in
1918 perf_event_sched_in(cpuctx
, task_ctx
, task
);
1920 perf_pmu_enable(cpuctx
->ctx
.pmu
);
1921 perf_ctx_unlock(cpuctx
, task_ctx
);
1927 * Attach a performance event to a context
1929 * First we add the event to the list with the hardware enable bit
1930 * in event->hw_config cleared.
1932 * If the event is attached to a task which is on a CPU we use a smp
1933 * call to enable it in the task context. The task might have been
1934 * scheduled away, but we check this in the smp call again.
1937 perf_install_in_context(struct perf_event_context
*ctx
,
1938 struct perf_event
*event
,
1941 struct task_struct
*task
= ctx
->task
;
1943 lockdep_assert_held(&ctx
->mutex
);
1946 if (event
->cpu
!= -1)
1951 * Per cpu events are installed via an smp call and
1952 * the install is always successful.
1954 cpu_function_call(cpu
, __perf_install_in_context
, event
);
1959 if (!task_function_call(task
, __perf_install_in_context
, event
))
1962 raw_spin_lock_irq(&ctx
->lock
);
1964 * If we failed to find a running task, but find the context active now
1965 * that we've acquired the ctx->lock, retry.
1967 if (ctx
->is_active
) {
1968 raw_spin_unlock_irq(&ctx
->lock
);
1973 * Since the task isn't running, its safe to add the event, us holding
1974 * the ctx->lock ensures the task won't get scheduled in.
1976 add_event_to_ctx(event
, ctx
);
1977 raw_spin_unlock_irq(&ctx
->lock
);
1981 * Put a event into inactive state and update time fields.
1982 * Enabling the leader of a group effectively enables all
1983 * the group members that aren't explicitly disabled, so we
1984 * have to update their ->tstamp_enabled also.
1985 * Note: this works for group members as well as group leaders
1986 * since the non-leader members' sibling_lists will be empty.
1988 static void __perf_event_mark_enabled(struct perf_event
*event
)
1990 struct perf_event
*sub
;
1991 u64 tstamp
= perf_event_time(event
);
1993 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1994 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
1995 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
1996 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
)
1997 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
2002 * Cross CPU call to enable a performance event
2004 static int __perf_event_enable(void *info
)
2006 struct perf_event
*event
= info
;
2007 struct perf_event_context
*ctx
= event
->ctx
;
2008 struct perf_event
*leader
= event
->group_leader
;
2009 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
2013 * There's a time window between 'ctx->is_active' check
2014 * in perf_event_enable function and this place having:
2016 * - ctx->lock unlocked
2018 * where the task could be killed and 'ctx' deactivated
2019 * by perf_event_exit_task.
2021 if (!ctx
->is_active
)
2024 raw_spin_lock(&ctx
->lock
);
2025 update_context_time(ctx
);
2027 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
2031 * set current task's cgroup time reference point
2033 perf_cgroup_set_timestamp(current
, ctx
);
2035 __perf_event_mark_enabled(event
);
2037 if (!event_filter_match(event
)) {
2038 if (is_cgroup_event(event
))
2039 perf_cgroup_defer_enabled(event
);
2044 * If the event is in a group and isn't the group leader,
2045 * then don't put it on unless the group is on.
2047 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
)
2050 if (!group_can_go_on(event
, cpuctx
, 1)) {
2053 if (event
== leader
)
2054 err
= group_sched_in(event
, cpuctx
, ctx
);
2056 err
= event_sched_in(event
, cpuctx
, ctx
);
2061 * If this event can't go on and it's part of a
2062 * group, then the whole group has to come off.
2064 if (leader
!= event
) {
2065 group_sched_out(leader
, cpuctx
, ctx
);
2066 perf_cpu_hrtimer_restart(cpuctx
);
2068 if (leader
->attr
.pinned
) {
2069 update_group_times(leader
);
2070 leader
->state
= PERF_EVENT_STATE_ERROR
;
2075 raw_spin_unlock(&ctx
->lock
);
2083 * If event->ctx is a cloned context, callers must make sure that
2084 * every task struct that event->ctx->task could possibly point to
2085 * remains valid. This condition is satisfied when called through
2086 * perf_event_for_each_child or perf_event_for_each as described
2087 * for perf_event_disable.
2089 void perf_event_enable(struct perf_event
*event
)
2091 struct perf_event_context
*ctx
= event
->ctx
;
2092 struct task_struct
*task
= ctx
->task
;
2096 * Enable the event on the cpu that it's on
2098 cpu_function_call(event
->cpu
, __perf_event_enable
, event
);
2102 raw_spin_lock_irq(&ctx
->lock
);
2103 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
2107 * If the event is in error state, clear that first.
2108 * That way, if we see the event in error state below, we
2109 * know that it has gone back into error state, as distinct
2110 * from the task having been scheduled away before the
2111 * cross-call arrived.
2113 if (event
->state
== PERF_EVENT_STATE_ERROR
)
2114 event
->state
= PERF_EVENT_STATE_OFF
;
2117 if (!ctx
->is_active
) {
2118 __perf_event_mark_enabled(event
);
2122 raw_spin_unlock_irq(&ctx
->lock
);
2124 if (!task_function_call(task
, __perf_event_enable
, event
))
2127 raw_spin_lock_irq(&ctx
->lock
);
2130 * If the context is active and the event is still off,
2131 * we need to retry the cross-call.
2133 if (ctx
->is_active
&& event
->state
== PERF_EVENT_STATE_OFF
) {
2135 * task could have been flipped by a concurrent
2136 * perf_event_context_sched_out()
2143 raw_spin_unlock_irq(&ctx
->lock
);
2145 EXPORT_SYMBOL_GPL(perf_event_enable
);
2147 int perf_event_refresh(struct perf_event
*event
, int refresh
)
2150 * not supported on inherited events
2152 if (event
->attr
.inherit
|| !is_sampling_event(event
))
2155 atomic_add(refresh
, &event
->event_limit
);
2156 perf_event_enable(event
);
2160 EXPORT_SYMBOL_GPL(perf_event_refresh
);
2162 static void ctx_sched_out(struct perf_event_context
*ctx
,
2163 struct perf_cpu_context
*cpuctx
,
2164 enum event_type_t event_type
)
2166 struct perf_event
*event
;
2167 int is_active
= ctx
->is_active
;
2169 ctx
->is_active
&= ~event_type
;
2170 if (likely(!ctx
->nr_events
))
2173 update_context_time(ctx
);
2174 update_cgrp_time_from_cpuctx(cpuctx
);
2175 if (!ctx
->nr_active
)
2178 perf_pmu_disable(ctx
->pmu
);
2179 if ((is_active
& EVENT_PINNED
) && (event_type
& EVENT_PINNED
)) {
2180 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
)
2181 group_sched_out(event
, cpuctx
, ctx
);
2184 if ((is_active
& EVENT_FLEXIBLE
) && (event_type
& EVENT_FLEXIBLE
)) {
2185 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
)
2186 group_sched_out(event
, cpuctx
, ctx
);
2188 perf_pmu_enable(ctx
->pmu
);
2192 * Test whether two contexts are equivalent, i.e. whether they have both been
2193 * cloned from the same version of the same context.
2195 * Equivalence is measured using a generation number in the context that is
2196 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2197 * and list_del_event().
2199 static int context_equiv(struct perf_event_context
*ctx1
,
2200 struct perf_event_context
*ctx2
)
2202 /* Pinning disables the swap optimization */
2203 if (ctx1
->pin_count
|| ctx2
->pin_count
)
2206 /* If ctx1 is the parent of ctx2 */
2207 if (ctx1
== ctx2
->parent_ctx
&& ctx1
->generation
== ctx2
->parent_gen
)
2210 /* If ctx2 is the parent of ctx1 */
2211 if (ctx1
->parent_ctx
== ctx2
&& ctx1
->parent_gen
== ctx2
->generation
)
2215 * If ctx1 and ctx2 have the same parent; we flatten the parent
2216 * hierarchy, see perf_event_init_context().
2218 if (ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
&&
2219 ctx1
->parent_gen
== ctx2
->parent_gen
)
2226 static void __perf_event_sync_stat(struct perf_event
*event
,
2227 struct perf_event
*next_event
)
2231 if (!event
->attr
.inherit_stat
)
2235 * Update the event value, we cannot use perf_event_read()
2236 * because we're in the middle of a context switch and have IRQs
2237 * disabled, which upsets smp_call_function_single(), however
2238 * we know the event must be on the current CPU, therefore we
2239 * don't need to use it.
2241 switch (event
->state
) {
2242 case PERF_EVENT_STATE_ACTIVE
:
2243 event
->pmu
->read(event
);
2246 case PERF_EVENT_STATE_INACTIVE
:
2247 update_event_times(event
);
2255 * In order to keep per-task stats reliable we need to flip the event
2256 * values when we flip the contexts.
2258 value
= local64_read(&next_event
->count
);
2259 value
= local64_xchg(&event
->count
, value
);
2260 local64_set(&next_event
->count
, value
);
2262 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
2263 swap(event
->total_time_running
, next_event
->total_time_running
);
2266 * Since we swizzled the values, update the user visible data too.
2268 perf_event_update_userpage(event
);
2269 perf_event_update_userpage(next_event
);
2272 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
2273 struct perf_event_context
*next_ctx
)
2275 struct perf_event
*event
, *next_event
;
2280 update_context_time(ctx
);
2282 event
= list_first_entry(&ctx
->event_list
,
2283 struct perf_event
, event_entry
);
2285 next_event
= list_first_entry(&next_ctx
->event_list
,
2286 struct perf_event
, event_entry
);
2288 while (&event
->event_entry
!= &ctx
->event_list
&&
2289 &next_event
->event_entry
!= &next_ctx
->event_list
) {
2291 __perf_event_sync_stat(event
, next_event
);
2293 event
= list_next_entry(event
, event_entry
);
2294 next_event
= list_next_entry(next_event
, event_entry
);
2298 static void perf_event_context_sched_out(struct task_struct
*task
, int ctxn
,
2299 struct task_struct
*next
)
2301 struct perf_event_context
*ctx
= task
->perf_event_ctxp
[ctxn
];
2302 struct perf_event_context
*next_ctx
;
2303 struct perf_event_context
*parent
, *next_parent
;
2304 struct perf_cpu_context
*cpuctx
;
2310 cpuctx
= __get_cpu_context(ctx
);
2311 if (!cpuctx
->task_ctx
)
2315 next_ctx
= next
->perf_event_ctxp
[ctxn
];
2319 parent
= rcu_dereference(ctx
->parent_ctx
);
2320 next_parent
= rcu_dereference(next_ctx
->parent_ctx
);
2322 /* If neither context have a parent context; they cannot be clones. */
2323 if (!parent
&& !next_parent
)
2326 if (next_parent
== ctx
|| next_ctx
== parent
|| next_parent
== parent
) {
2328 * Looks like the two contexts are clones, so we might be
2329 * able to optimize the context switch. We lock both
2330 * contexts and check that they are clones under the
2331 * lock (including re-checking that neither has been
2332 * uncloned in the meantime). It doesn't matter which
2333 * order we take the locks because no other cpu could
2334 * be trying to lock both of these tasks.
2336 raw_spin_lock(&ctx
->lock
);
2337 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
2338 if (context_equiv(ctx
, next_ctx
)) {
2340 * XXX do we need a memory barrier of sorts
2341 * wrt to rcu_dereference() of perf_event_ctxp
2343 task
->perf_event_ctxp
[ctxn
] = next_ctx
;
2344 next
->perf_event_ctxp
[ctxn
] = ctx
;
2346 next_ctx
->task
= task
;
2349 perf_event_sync_stat(ctx
, next_ctx
);
2351 raw_spin_unlock(&next_ctx
->lock
);
2352 raw_spin_unlock(&ctx
->lock
);
2358 raw_spin_lock(&ctx
->lock
);
2359 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
2360 cpuctx
->task_ctx
= NULL
;
2361 raw_spin_unlock(&ctx
->lock
);
2365 #define for_each_task_context_nr(ctxn) \
2366 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2369 * Called from scheduler to remove the events of the current task,
2370 * with interrupts disabled.
2372 * We stop each event and update the event value in event->count.
2374 * This does not protect us against NMI, but disable()
2375 * sets the disabled bit in the control field of event _before_
2376 * accessing the event control register. If a NMI hits, then it will
2377 * not restart the event.
2379 void __perf_event_task_sched_out(struct task_struct
*task
,
2380 struct task_struct
*next
)
2384 for_each_task_context_nr(ctxn
)
2385 perf_event_context_sched_out(task
, ctxn
, next
);
2388 * if cgroup events exist on this CPU, then we need
2389 * to check if we have to switch out PMU state.
2390 * cgroup event are system-wide mode only
2392 if (atomic_read(&__get_cpu_var(perf_cgroup_events
)))
2393 perf_cgroup_sched_out(task
, next
);
2396 static void task_ctx_sched_out(struct perf_event_context
*ctx
)
2398 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
2400 if (!cpuctx
->task_ctx
)
2403 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
2406 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
2407 cpuctx
->task_ctx
= NULL
;
2411 * Called with IRQs disabled
2413 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
2414 enum event_type_t event_type
)
2416 ctx_sched_out(&cpuctx
->ctx
, cpuctx
, event_type
);
2420 ctx_pinned_sched_in(struct perf_event_context
*ctx
,
2421 struct perf_cpu_context
*cpuctx
)
2423 struct perf_event
*event
;
2425 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
2426 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2428 if (!event_filter_match(event
))
2431 /* may need to reset tstamp_enabled */
2432 if (is_cgroup_event(event
))
2433 perf_cgroup_mark_enabled(event
, ctx
);
2435 if (group_can_go_on(event
, cpuctx
, 1))
2436 group_sched_in(event
, cpuctx
, ctx
);
2439 * If this pinned group hasn't been scheduled,
2440 * put it in error state.
2442 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
2443 update_group_times(event
);
2444 event
->state
= PERF_EVENT_STATE_ERROR
;
2450 ctx_flexible_sched_in(struct perf_event_context
*ctx
,
2451 struct perf_cpu_context
*cpuctx
)
2453 struct perf_event
*event
;
2456 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
2457 /* Ignore events in OFF or ERROR state */
2458 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2461 * Listen to the 'cpu' scheduling filter constraint
2464 if (!event_filter_match(event
))
2467 /* may need to reset tstamp_enabled */
2468 if (is_cgroup_event(event
))
2469 perf_cgroup_mark_enabled(event
, ctx
);
2471 if (group_can_go_on(event
, cpuctx
, can_add_hw
)) {
2472 if (group_sched_in(event
, cpuctx
, ctx
))
2479 ctx_sched_in(struct perf_event_context
*ctx
,
2480 struct perf_cpu_context
*cpuctx
,
2481 enum event_type_t event_type
,
2482 struct task_struct
*task
)
2485 int is_active
= ctx
->is_active
;
2487 ctx
->is_active
|= event_type
;
2488 if (likely(!ctx
->nr_events
))
2492 ctx
->timestamp
= now
;
2493 perf_cgroup_set_timestamp(task
, ctx
);
2495 * First go through the list and put on any pinned groups
2496 * in order to give them the best chance of going on.
2498 if (!(is_active
& EVENT_PINNED
) && (event_type
& EVENT_PINNED
))
2499 ctx_pinned_sched_in(ctx
, cpuctx
);
2501 /* Then walk through the lower prio flexible groups */
2502 if (!(is_active
& EVENT_FLEXIBLE
) && (event_type
& EVENT_FLEXIBLE
))
2503 ctx_flexible_sched_in(ctx
, cpuctx
);
2506 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
2507 enum event_type_t event_type
,
2508 struct task_struct
*task
)
2510 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
2512 ctx_sched_in(ctx
, cpuctx
, event_type
, task
);
2515 static void perf_event_context_sched_in(struct perf_event_context
*ctx
,
2516 struct task_struct
*task
)
2518 struct perf_cpu_context
*cpuctx
;
2520 cpuctx
= __get_cpu_context(ctx
);
2521 if (cpuctx
->task_ctx
== ctx
)
2524 perf_ctx_lock(cpuctx
, ctx
);
2525 perf_pmu_disable(ctx
->pmu
);
2527 * We want to keep the following priority order:
2528 * cpu pinned (that don't need to move), task pinned,
2529 * cpu flexible, task flexible.
2531 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
2534 cpuctx
->task_ctx
= ctx
;
2536 perf_event_sched_in(cpuctx
, cpuctx
->task_ctx
, task
);
2538 perf_pmu_enable(ctx
->pmu
);
2539 perf_ctx_unlock(cpuctx
, ctx
);
2542 * Since these rotations are per-cpu, we need to ensure the
2543 * cpu-context we got scheduled on is actually rotating.
2545 perf_pmu_rotate_start(ctx
->pmu
);
2549 * When sampling the branck stack in system-wide, it may be necessary
2550 * to flush the stack on context switch. This happens when the branch
2551 * stack does not tag its entries with the pid of the current task.
2552 * Otherwise it becomes impossible to associate a branch entry with a
2553 * task. This ambiguity is more likely to appear when the branch stack
2554 * supports priv level filtering and the user sets it to monitor only
2555 * at the user level (which could be a useful measurement in system-wide
2556 * mode). In that case, the risk is high of having a branch stack with
2557 * branch from multiple tasks. Flushing may mean dropping the existing
2558 * entries or stashing them somewhere in the PMU specific code layer.
2560 * This function provides the context switch callback to the lower code
2561 * layer. It is invoked ONLY when there is at least one system-wide context
2562 * with at least one active event using taken branch sampling.
2564 static void perf_branch_stack_sched_in(struct task_struct
*prev
,
2565 struct task_struct
*task
)
2567 struct perf_cpu_context
*cpuctx
;
2569 unsigned long flags
;
2571 /* no need to flush branch stack if not changing task */
2575 local_irq_save(flags
);
2579 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
2580 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
2583 * check if the context has at least one
2584 * event using PERF_SAMPLE_BRANCH_STACK
2586 if (cpuctx
->ctx
.nr_branch_stack
> 0
2587 && pmu
->flush_branch_stack
) {
2589 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
2591 perf_pmu_disable(pmu
);
2593 pmu
->flush_branch_stack();
2595 perf_pmu_enable(pmu
);
2597 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
2603 local_irq_restore(flags
);
2607 * Called from scheduler to add the events of the current task
2608 * with interrupts disabled.
2610 * We restore the event value and then enable it.
2612 * This does not protect us against NMI, but enable()
2613 * sets the enabled bit in the control field of event _before_
2614 * accessing the event control register. If a NMI hits, then it will
2615 * keep the event running.
2617 void __perf_event_task_sched_in(struct task_struct
*prev
,
2618 struct task_struct
*task
)
2620 struct perf_event_context
*ctx
;
2623 for_each_task_context_nr(ctxn
) {
2624 ctx
= task
->perf_event_ctxp
[ctxn
];
2628 perf_event_context_sched_in(ctx
, task
);
2631 * if cgroup events exist on this CPU, then we need
2632 * to check if we have to switch in PMU state.
2633 * cgroup event are system-wide mode only
2635 if (atomic_read(&__get_cpu_var(perf_cgroup_events
)))
2636 perf_cgroup_sched_in(prev
, task
);
2638 /* check for system-wide branch_stack events */
2639 if (atomic_read(&__get_cpu_var(perf_branch_stack_events
)))
2640 perf_branch_stack_sched_in(prev
, task
);
2643 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
2645 u64 frequency
= event
->attr
.sample_freq
;
2646 u64 sec
= NSEC_PER_SEC
;
2647 u64 divisor
, dividend
;
2649 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
2651 count_fls
= fls64(count
);
2652 nsec_fls
= fls64(nsec
);
2653 frequency_fls
= fls64(frequency
);
2657 * We got @count in @nsec, with a target of sample_freq HZ
2658 * the target period becomes:
2661 * period = -------------------
2662 * @nsec * sample_freq
2667 * Reduce accuracy by one bit such that @a and @b converge
2668 * to a similar magnitude.
2670 #define REDUCE_FLS(a, b) \
2672 if (a##_fls > b##_fls) { \
2682 * Reduce accuracy until either term fits in a u64, then proceed with
2683 * the other, so that finally we can do a u64/u64 division.
2685 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
2686 REDUCE_FLS(nsec
, frequency
);
2687 REDUCE_FLS(sec
, count
);
2690 if (count_fls
+ sec_fls
> 64) {
2691 divisor
= nsec
* frequency
;
2693 while (count_fls
+ sec_fls
> 64) {
2694 REDUCE_FLS(count
, sec
);
2698 dividend
= count
* sec
;
2700 dividend
= count
* sec
;
2702 while (nsec_fls
+ frequency_fls
> 64) {
2703 REDUCE_FLS(nsec
, frequency
);
2707 divisor
= nsec
* frequency
;
2713 return div64_u64(dividend
, divisor
);
2716 static DEFINE_PER_CPU(int, perf_throttled_count
);
2717 static DEFINE_PER_CPU(u64
, perf_throttled_seq
);
2719 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
, bool disable
)
2721 struct hw_perf_event
*hwc
= &event
->hw
;
2722 s64 period
, sample_period
;
2725 period
= perf_calculate_period(event
, nsec
, count
);
2727 delta
= (s64
)(period
- hwc
->sample_period
);
2728 delta
= (delta
+ 7) / 8; /* low pass filter */
2730 sample_period
= hwc
->sample_period
+ delta
;
2735 hwc
->sample_period
= sample_period
;
2737 if (local64_read(&hwc
->period_left
) > 8*sample_period
) {
2739 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
2741 local64_set(&hwc
->period_left
, 0);
2744 event
->pmu
->start(event
, PERF_EF_RELOAD
);
2749 * combine freq adjustment with unthrottling to avoid two passes over the
2750 * events. At the same time, make sure, having freq events does not change
2751 * the rate of unthrottling as that would introduce bias.
2753 static void perf_adjust_freq_unthr_context(struct perf_event_context
*ctx
,
2756 struct perf_event
*event
;
2757 struct hw_perf_event
*hwc
;
2758 u64 now
, period
= TICK_NSEC
;
2762 * only need to iterate over all events iff:
2763 * - context have events in frequency mode (needs freq adjust)
2764 * - there are events to unthrottle on this cpu
2766 if (!(ctx
->nr_freq
|| needs_unthr
))
2769 raw_spin_lock(&ctx
->lock
);
2770 perf_pmu_disable(ctx
->pmu
);
2772 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
2773 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
2776 if (!event_filter_match(event
))
2779 perf_pmu_disable(event
->pmu
);
2783 if (hwc
->interrupts
== MAX_INTERRUPTS
) {
2784 hwc
->interrupts
= 0;
2785 perf_log_throttle(event
, 1);
2786 event
->pmu
->start(event
, 0);
2789 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
2793 * stop the event and update event->count
2795 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
2797 now
= local64_read(&event
->count
);
2798 delta
= now
- hwc
->freq_count_stamp
;
2799 hwc
->freq_count_stamp
= now
;
2803 * reload only if value has changed
2804 * we have stopped the event so tell that
2805 * to perf_adjust_period() to avoid stopping it
2809 perf_adjust_period(event
, period
, delta
, false);
2811 event
->pmu
->start(event
, delta
> 0 ? PERF_EF_RELOAD
: 0);
2813 perf_pmu_enable(event
->pmu
);
2816 perf_pmu_enable(ctx
->pmu
);
2817 raw_spin_unlock(&ctx
->lock
);
2821 * Round-robin a context's events:
2823 static void rotate_ctx(struct perf_event_context
*ctx
)
2826 * Rotate the first entry last of non-pinned groups. Rotation might be
2827 * disabled by the inheritance code.
2829 if (!ctx
->rotate_disable
)
2830 list_rotate_left(&ctx
->flexible_groups
);
2834 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2835 * because they're strictly cpu affine and rotate_start is called with IRQs
2836 * disabled, while rotate_context is called from IRQ context.
2838 static int perf_rotate_context(struct perf_cpu_context
*cpuctx
)
2840 struct perf_event_context
*ctx
= NULL
;
2841 int rotate
= 0, remove
= 1;
2843 if (cpuctx
->ctx
.nr_events
) {
2845 if (cpuctx
->ctx
.nr_events
!= cpuctx
->ctx
.nr_active
)
2849 ctx
= cpuctx
->task_ctx
;
2850 if (ctx
&& ctx
->nr_events
) {
2852 if (ctx
->nr_events
!= ctx
->nr_active
)
2859 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
2860 perf_pmu_disable(cpuctx
->ctx
.pmu
);
2862 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
2864 ctx_sched_out(ctx
, cpuctx
, EVENT_FLEXIBLE
);
2866 rotate_ctx(&cpuctx
->ctx
);
2870 perf_event_sched_in(cpuctx
, ctx
, current
);
2872 perf_pmu_enable(cpuctx
->ctx
.pmu
);
2873 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
2876 list_del_init(&cpuctx
->rotation_list
);
2881 #ifdef CONFIG_NO_HZ_FULL
2882 bool perf_event_can_stop_tick(void)
2884 if (atomic_read(&nr_freq_events
) ||
2885 __this_cpu_read(perf_throttled_count
))
2892 void perf_event_task_tick(void)
2894 struct list_head
*head
= &__get_cpu_var(rotation_list
);
2895 struct perf_cpu_context
*cpuctx
, *tmp
;
2896 struct perf_event_context
*ctx
;
2899 WARN_ON(!irqs_disabled());
2901 __this_cpu_inc(perf_throttled_seq
);
2902 throttled
= __this_cpu_xchg(perf_throttled_count
, 0);
2904 list_for_each_entry_safe(cpuctx
, tmp
, head
, rotation_list
) {
2906 perf_adjust_freq_unthr_context(ctx
, throttled
);
2908 ctx
= cpuctx
->task_ctx
;
2910 perf_adjust_freq_unthr_context(ctx
, throttled
);
2914 static int event_enable_on_exec(struct perf_event
*event
,
2915 struct perf_event_context
*ctx
)
2917 if (!event
->attr
.enable_on_exec
)
2920 event
->attr
.enable_on_exec
= 0;
2921 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
2924 __perf_event_mark_enabled(event
);
2930 * Enable all of a task's events that have been marked enable-on-exec.
2931 * This expects task == current.
2933 static void perf_event_enable_on_exec(struct perf_event_context
*ctx
)
2935 struct perf_event
*event
;
2936 unsigned long flags
;
2940 local_irq_save(flags
);
2941 if (!ctx
|| !ctx
->nr_events
)
2945 * We must ctxsw out cgroup events to avoid conflict
2946 * when invoking perf_task_event_sched_in() later on
2947 * in this function. Otherwise we end up trying to
2948 * ctxswin cgroup events which are already scheduled
2951 perf_cgroup_sched_out(current
, NULL
);
2953 raw_spin_lock(&ctx
->lock
);
2954 task_ctx_sched_out(ctx
);
2956 list_for_each_entry(event
, &ctx
->event_list
, event_entry
) {
2957 ret
= event_enable_on_exec(event
, ctx
);
2963 * Unclone this context if we enabled any event.
2968 raw_spin_unlock(&ctx
->lock
);
2971 * Also calls ctxswin for cgroup events, if any:
2973 perf_event_context_sched_in(ctx
, ctx
->task
);
2975 local_irq_restore(flags
);
2978 void perf_event_exec(void)
2980 struct perf_event_context
*ctx
;
2984 for_each_task_context_nr(ctxn
) {
2985 ctx
= current
->perf_event_ctxp
[ctxn
];
2989 perf_event_enable_on_exec(ctx
);
2995 * Cross CPU call to read the hardware event
2997 static void __perf_event_read(void *info
)
2999 struct perf_event
*event
= info
;
3000 struct perf_event_context
*ctx
= event
->ctx
;
3001 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
3004 * If this is a task context, we need to check whether it is
3005 * the current task context of this cpu. If not it has been
3006 * scheduled out before the smp call arrived. In that case
3007 * event->count would have been updated to a recent sample
3008 * when the event was scheduled out.
3010 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
3013 raw_spin_lock(&ctx
->lock
);
3014 if (ctx
->is_active
) {
3015 update_context_time(ctx
);
3016 update_cgrp_time_from_event(event
);
3018 update_event_times(event
);
3019 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
3020 event
->pmu
->read(event
);
3021 raw_spin_unlock(&ctx
->lock
);
3024 static inline u64
perf_event_count(struct perf_event
*event
)
3026 return local64_read(&event
->count
) + atomic64_read(&event
->child_count
);
3029 static u64
perf_event_read(struct perf_event
*event
)
3032 * If event is enabled and currently active on a CPU, update the
3033 * value in the event structure:
3035 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
3036 smp_call_function_single(event
->oncpu
,
3037 __perf_event_read
, event
, 1);
3038 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
3039 struct perf_event_context
*ctx
= event
->ctx
;
3040 unsigned long flags
;
3042 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
3044 * may read while context is not active
3045 * (e.g., thread is blocked), in that case
3046 * we cannot update context time
3048 if (ctx
->is_active
) {
3049 update_context_time(ctx
);
3050 update_cgrp_time_from_event(event
);
3052 update_event_times(event
);
3053 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
3056 return perf_event_count(event
);
3060 * Initialize the perf_event context in a task_struct:
3062 static void __perf_event_init_context(struct perf_event_context
*ctx
)
3064 raw_spin_lock_init(&ctx
->lock
);
3065 mutex_init(&ctx
->mutex
);
3066 INIT_LIST_HEAD(&ctx
->pinned_groups
);
3067 INIT_LIST_HEAD(&ctx
->flexible_groups
);
3068 INIT_LIST_HEAD(&ctx
->event_list
);
3069 atomic_set(&ctx
->refcount
, 1);
3072 static struct perf_event_context
*
3073 alloc_perf_context(struct pmu
*pmu
, struct task_struct
*task
)
3075 struct perf_event_context
*ctx
;
3077 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
3081 __perf_event_init_context(ctx
);
3084 get_task_struct(task
);
3091 static struct task_struct
*
3092 find_lively_task_by_vpid(pid_t vpid
)
3094 struct task_struct
*task
;
3101 task
= find_task_by_vpid(vpid
);
3103 get_task_struct(task
);
3107 return ERR_PTR(-ESRCH
);
3109 /* Reuse ptrace permission checks for now. */
3111 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
3116 put_task_struct(task
);
3117 return ERR_PTR(err
);
3122 * Returns a matching context with refcount and pincount.
3124 static struct perf_event_context
*
3125 find_get_context(struct pmu
*pmu
, struct task_struct
*task
, int cpu
)
3127 struct perf_event_context
*ctx
;
3128 struct perf_cpu_context
*cpuctx
;
3129 unsigned long flags
;
3133 /* Must be root to operate on a CPU event: */
3134 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
3135 return ERR_PTR(-EACCES
);
3138 * We could be clever and allow to attach a event to an
3139 * offline CPU and activate it when the CPU comes up, but
3142 if (!cpu_online(cpu
))
3143 return ERR_PTR(-ENODEV
);
3145 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
3154 ctxn
= pmu
->task_ctx_nr
;
3159 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
3163 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
3165 ctx
= alloc_perf_context(pmu
, task
);
3171 mutex_lock(&task
->perf_event_mutex
);
3173 * If it has already passed perf_event_exit_task().
3174 * we must see PF_EXITING, it takes this mutex too.
3176 if (task
->flags
& PF_EXITING
)
3178 else if (task
->perf_event_ctxp
[ctxn
])
3183 rcu_assign_pointer(task
->perf_event_ctxp
[ctxn
], ctx
);
3185 mutex_unlock(&task
->perf_event_mutex
);
3187 if (unlikely(err
)) {
3199 return ERR_PTR(err
);
3202 static void perf_event_free_filter(struct perf_event
*event
);
3204 static void free_event_rcu(struct rcu_head
*head
)
3206 struct perf_event
*event
;
3208 event
= container_of(head
, struct perf_event
, rcu_head
);
3210 put_pid_ns(event
->ns
);
3211 perf_event_free_filter(event
);
3215 static void ring_buffer_put(struct ring_buffer
*rb
);
3216 static void ring_buffer_attach(struct perf_event
*event
,
3217 struct ring_buffer
*rb
);
3219 static void unaccount_event_cpu(struct perf_event
*event
, int cpu
)
3224 if (has_branch_stack(event
)) {
3225 if (!(event
->attach_state
& PERF_ATTACH_TASK
))
3226 atomic_dec(&per_cpu(perf_branch_stack_events
, cpu
));
3228 if (is_cgroup_event(event
))
3229 atomic_dec(&per_cpu(perf_cgroup_events
, cpu
));
3232 static void unaccount_event(struct perf_event
*event
)
3237 if (event
->attach_state
& PERF_ATTACH_TASK
)
3238 static_key_slow_dec_deferred(&perf_sched_events
);
3239 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
3240 atomic_dec(&nr_mmap_events
);
3241 if (event
->attr
.comm
)
3242 atomic_dec(&nr_comm_events
);
3243 if (event
->attr
.task
)
3244 atomic_dec(&nr_task_events
);
3245 if (event
->attr
.freq
)
3246 atomic_dec(&nr_freq_events
);
3247 if (is_cgroup_event(event
))
3248 static_key_slow_dec_deferred(&perf_sched_events
);
3249 if (has_branch_stack(event
))
3250 static_key_slow_dec_deferred(&perf_sched_events
);
3252 unaccount_event_cpu(event
, event
->cpu
);
3255 static void __free_event(struct perf_event
*event
)
3257 if (!event
->parent
) {
3258 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
)
3259 put_callchain_buffers();
3263 event
->destroy(event
);
3266 put_ctx(event
->ctx
);
3269 module_put(event
->pmu
->module
);
3271 call_rcu(&event
->rcu_head
, free_event_rcu
);
3274 static void _free_event(struct perf_event
*event
)
3276 irq_work_sync(&event
->pending
);
3278 unaccount_event(event
);
3282 * Can happen when we close an event with re-directed output.
3284 * Since we have a 0 refcount, perf_mmap_close() will skip
3285 * over us; possibly making our ring_buffer_put() the last.
3287 mutex_lock(&event
->mmap_mutex
);
3288 ring_buffer_attach(event
, NULL
);
3289 mutex_unlock(&event
->mmap_mutex
);
3292 if (is_cgroup_event(event
))
3293 perf_detach_cgroup(event
);
3295 __free_event(event
);
3299 * Used to free events which have a known refcount of 1, such as in error paths
3300 * where the event isn't exposed yet and inherited events.
3302 static void free_event(struct perf_event
*event
)
3304 if (WARN(atomic_long_cmpxchg(&event
->refcount
, 1, 0) != 1,
3305 "unexpected event refcount: %ld; ptr=%p\n",
3306 atomic_long_read(&event
->refcount
), event
)) {
3307 /* leak to avoid use-after-free */
3315 * Called when the last reference to the file is gone.
3317 static void put_event(struct perf_event
*event
)
3319 struct perf_event_context
*ctx
= event
->ctx
;
3320 struct task_struct
*owner
;
3322 if (!atomic_long_dec_and_test(&event
->refcount
))
3326 owner
= ACCESS_ONCE(event
->owner
);
3328 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3329 * !owner it means the list deletion is complete and we can indeed
3330 * free this event, otherwise we need to serialize on
3331 * owner->perf_event_mutex.
3333 smp_read_barrier_depends();
3336 * Since delayed_put_task_struct() also drops the last
3337 * task reference we can safely take a new reference
3338 * while holding the rcu_read_lock().
3340 get_task_struct(owner
);
3345 mutex_lock(&owner
->perf_event_mutex
);
3347 * We have to re-check the event->owner field, if it is cleared
3348 * we raced with perf_event_exit_task(), acquiring the mutex
3349 * ensured they're done, and we can proceed with freeing the
3353 list_del_init(&event
->owner_entry
);
3354 mutex_unlock(&owner
->perf_event_mutex
);
3355 put_task_struct(owner
);
3358 WARN_ON_ONCE(ctx
->parent_ctx
);
3360 * There are two ways this annotation is useful:
3362 * 1) there is a lock recursion from perf_event_exit_task
3363 * see the comment there.
3365 * 2) there is a lock-inversion with mmap_sem through
3366 * perf_event_read_group(), which takes faults while
3367 * holding ctx->mutex, however this is called after
3368 * the last filedesc died, so there is no possibility
3369 * to trigger the AB-BA case.
3371 mutex_lock_nested(&ctx
->mutex
, SINGLE_DEPTH_NESTING
);
3372 perf_remove_from_context(event
, true);
3373 mutex_unlock(&ctx
->mutex
);
3378 int perf_event_release_kernel(struct perf_event
*event
)
3383 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
3385 static int perf_release(struct inode
*inode
, struct file
*file
)
3387 put_event(file
->private_data
);
3391 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
3393 struct perf_event
*child
;
3399 mutex_lock(&event
->child_mutex
);
3400 total
+= perf_event_read(event
);
3401 *enabled
+= event
->total_time_enabled
+
3402 atomic64_read(&event
->child_total_time_enabled
);
3403 *running
+= event
->total_time_running
+
3404 atomic64_read(&event
->child_total_time_running
);
3406 list_for_each_entry(child
, &event
->child_list
, child_list
) {
3407 total
+= perf_event_read(child
);
3408 *enabled
+= child
->total_time_enabled
;
3409 *running
+= child
->total_time_running
;
3411 mutex_unlock(&event
->child_mutex
);
3415 EXPORT_SYMBOL_GPL(perf_event_read_value
);
3417 static int perf_event_read_group(struct perf_event
*event
,
3418 u64 read_format
, char __user
*buf
)
3420 struct perf_event
*leader
= event
->group_leader
, *sub
;
3421 int n
= 0, size
= 0, ret
= -EFAULT
;
3422 struct perf_event_context
*ctx
= leader
->ctx
;
3424 u64 count
, enabled
, running
;
3426 mutex_lock(&ctx
->mutex
);
3427 count
= perf_event_read_value(leader
, &enabled
, &running
);
3429 values
[n
++] = 1 + leader
->nr_siblings
;
3430 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3431 values
[n
++] = enabled
;
3432 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3433 values
[n
++] = running
;
3434 values
[n
++] = count
;
3435 if (read_format
& PERF_FORMAT_ID
)
3436 values
[n
++] = primary_event_id(leader
);
3438 size
= n
* sizeof(u64
);
3440 if (copy_to_user(buf
, values
, size
))
3445 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3448 values
[n
++] = perf_event_read_value(sub
, &enabled
, &running
);
3449 if (read_format
& PERF_FORMAT_ID
)
3450 values
[n
++] = primary_event_id(sub
);
3452 size
= n
* sizeof(u64
);
3454 if (copy_to_user(buf
+ ret
, values
, size
)) {
3462 mutex_unlock(&ctx
->mutex
);
3467 static int perf_event_read_one(struct perf_event
*event
,
3468 u64 read_format
, char __user
*buf
)
3470 u64 enabled
, running
;
3474 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
3475 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3476 values
[n
++] = enabled
;
3477 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3478 values
[n
++] = running
;
3479 if (read_format
& PERF_FORMAT_ID
)
3480 values
[n
++] = primary_event_id(event
);
3482 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
3485 return n
* sizeof(u64
);
3489 * Read the performance event - simple non blocking version for now
3492 perf_read_hw(struct perf_event
*event
, char __user
*buf
, size_t count
)
3494 u64 read_format
= event
->attr
.read_format
;
3498 * Return end-of-file for a read on a event that is in
3499 * error state (i.e. because it was pinned but it couldn't be
3500 * scheduled on to the CPU at some point).
3502 if (event
->state
== PERF_EVENT_STATE_ERROR
)
3505 if (count
< event
->read_size
)
3508 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3509 if (read_format
& PERF_FORMAT_GROUP
)
3510 ret
= perf_event_read_group(event
, read_format
, buf
);
3512 ret
= perf_event_read_one(event
, read_format
, buf
);
3518 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
3520 struct perf_event
*event
= file
->private_data
;
3522 return perf_read_hw(event
, buf
, count
);
3525 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
3527 struct perf_event
*event
= file
->private_data
;
3528 struct ring_buffer
*rb
;
3529 unsigned int events
= POLL_HUP
;
3532 * Pin the event->rb by taking event->mmap_mutex; otherwise
3533 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
3535 mutex_lock(&event
->mmap_mutex
);
3538 events
= atomic_xchg(&rb
->poll
, 0);
3539 mutex_unlock(&event
->mmap_mutex
);
3541 poll_wait(file
, &event
->waitq
, wait
);
3546 static void perf_event_reset(struct perf_event
*event
)
3548 (void)perf_event_read(event
);
3549 local64_set(&event
->count
, 0);
3550 perf_event_update_userpage(event
);
3554 * Holding the top-level event's child_mutex means that any
3555 * descendant process that has inherited this event will block
3556 * in sync_child_event if it goes to exit, thus satisfying the
3557 * task existence requirements of perf_event_enable/disable.
3559 static void perf_event_for_each_child(struct perf_event
*event
,
3560 void (*func
)(struct perf_event
*))
3562 struct perf_event
*child
;
3564 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3565 mutex_lock(&event
->child_mutex
);
3567 list_for_each_entry(child
, &event
->child_list
, child_list
)
3569 mutex_unlock(&event
->child_mutex
);
3572 static void perf_event_for_each(struct perf_event
*event
,
3573 void (*func
)(struct perf_event
*))
3575 struct perf_event_context
*ctx
= event
->ctx
;
3576 struct perf_event
*sibling
;
3578 WARN_ON_ONCE(ctx
->parent_ctx
);
3579 mutex_lock(&ctx
->mutex
);
3580 event
= event
->group_leader
;
3582 perf_event_for_each_child(event
, func
);
3583 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
3584 perf_event_for_each_child(sibling
, func
);
3585 mutex_unlock(&ctx
->mutex
);
3588 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
3590 struct perf_event_context
*ctx
= event
->ctx
;
3591 int ret
= 0, active
;
3594 if (!is_sampling_event(event
))
3597 if (copy_from_user(&value
, arg
, sizeof(value
)))
3603 raw_spin_lock_irq(&ctx
->lock
);
3604 if (event
->attr
.freq
) {
3605 if (value
> sysctl_perf_event_sample_rate
) {
3610 event
->attr
.sample_freq
= value
;
3612 event
->attr
.sample_period
= value
;
3613 event
->hw
.sample_period
= value
;
3616 active
= (event
->state
== PERF_EVENT_STATE_ACTIVE
);
3618 perf_pmu_disable(ctx
->pmu
);
3619 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
3622 local64_set(&event
->hw
.period_left
, 0);
3625 event
->pmu
->start(event
, PERF_EF_RELOAD
);
3626 perf_pmu_enable(ctx
->pmu
);
3630 raw_spin_unlock_irq(&ctx
->lock
);
3635 static const struct file_operations perf_fops
;
3637 static inline int perf_fget_light(int fd
, struct fd
*p
)
3639 struct fd f
= fdget(fd
);
3643 if (f
.file
->f_op
!= &perf_fops
) {
3651 static int perf_event_set_output(struct perf_event
*event
,
3652 struct perf_event
*output_event
);
3653 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
3655 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
3657 struct perf_event
*event
= file
->private_data
;
3658 void (*func
)(struct perf_event
*);
3662 case PERF_EVENT_IOC_ENABLE
:
3663 func
= perf_event_enable
;
3665 case PERF_EVENT_IOC_DISABLE
:
3666 func
= perf_event_disable
;
3668 case PERF_EVENT_IOC_RESET
:
3669 func
= perf_event_reset
;
3672 case PERF_EVENT_IOC_REFRESH
:
3673 return perf_event_refresh(event
, arg
);
3675 case PERF_EVENT_IOC_PERIOD
:
3676 return perf_event_period(event
, (u64 __user
*)arg
);
3678 case PERF_EVENT_IOC_ID
:
3680 u64 id
= primary_event_id(event
);
3682 if (copy_to_user((void __user
*)arg
, &id
, sizeof(id
)))
3687 case PERF_EVENT_IOC_SET_OUTPUT
:
3691 struct perf_event
*output_event
;
3693 ret
= perf_fget_light(arg
, &output
);
3696 output_event
= output
.file
->private_data
;
3697 ret
= perf_event_set_output(event
, output_event
);
3700 ret
= perf_event_set_output(event
, NULL
);
3705 case PERF_EVENT_IOC_SET_FILTER
:
3706 return perf_event_set_filter(event
, (void __user
*)arg
);
3712 if (flags
& PERF_IOC_FLAG_GROUP
)
3713 perf_event_for_each(event
, func
);
3715 perf_event_for_each_child(event
, func
);
3720 int perf_event_task_enable(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_enable
);
3727 mutex_unlock(¤t
->perf_event_mutex
);
3732 int perf_event_task_disable(void)
3734 struct perf_event
*event
;
3736 mutex_lock(¤t
->perf_event_mutex
);
3737 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
3738 perf_event_for_each_child(event
, perf_event_disable
);
3739 mutex_unlock(¤t
->perf_event_mutex
);
3744 static int perf_event_index(struct perf_event
*event
)
3746 if (event
->hw
.state
& PERF_HES_STOPPED
)
3749 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
3752 return event
->pmu
->event_idx(event
);
3755 static void calc_timer_values(struct perf_event
*event
,
3762 *now
= perf_clock();
3763 ctx_time
= event
->shadow_ctx_time
+ *now
;
3764 *enabled
= ctx_time
- event
->tstamp_enabled
;
3765 *running
= ctx_time
- event
->tstamp_running
;
3768 static void perf_event_init_userpage(struct perf_event
*event
)
3770 struct perf_event_mmap_page
*userpg
;
3771 struct ring_buffer
*rb
;
3774 rb
= rcu_dereference(event
->rb
);
3778 userpg
= rb
->user_page
;
3780 /* Allow new userspace to detect that bit 0 is deprecated */
3781 userpg
->cap_bit0_is_deprecated
= 1;
3782 userpg
->size
= offsetof(struct perf_event_mmap_page
, __reserved
);
3788 void __weak
arch_perf_update_userpage(struct perf_event_mmap_page
*userpg
, u64 now
)
3793 * Callers need to ensure there can be no nesting of this function, otherwise
3794 * the seqlock logic goes bad. We can not serialize this because the arch
3795 * code calls this from NMI context.
3797 void perf_event_update_userpage(struct perf_event
*event
)
3799 struct perf_event_mmap_page
*userpg
;
3800 struct ring_buffer
*rb
;
3801 u64 enabled
, running
, now
;
3804 rb
= rcu_dereference(event
->rb
);
3809 * compute total_time_enabled, total_time_running
3810 * based on snapshot values taken when the event
3811 * was last scheduled in.
3813 * we cannot simply called update_context_time()
3814 * because of locking issue as we can be called in
3817 calc_timer_values(event
, &now
, &enabled
, &running
);
3819 userpg
= rb
->user_page
;
3821 * Disable preemption so as to not let the corresponding user-space
3822 * spin too long if we get preempted.
3827 userpg
->index
= perf_event_index(event
);
3828 userpg
->offset
= perf_event_count(event
);
3830 userpg
->offset
-= local64_read(&event
->hw
.prev_count
);
3832 userpg
->time_enabled
= enabled
+
3833 atomic64_read(&event
->child_total_time_enabled
);
3835 userpg
->time_running
= running
+
3836 atomic64_read(&event
->child_total_time_running
);
3838 arch_perf_update_userpage(userpg
, now
);
3847 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
3849 struct perf_event
*event
= vma
->vm_file
->private_data
;
3850 struct ring_buffer
*rb
;
3851 int ret
= VM_FAULT_SIGBUS
;
3853 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
3854 if (vmf
->pgoff
== 0)
3860 rb
= rcu_dereference(event
->rb
);
3864 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
3867 vmf
->page
= perf_mmap_to_page(rb
, vmf
->pgoff
);
3871 get_page(vmf
->page
);
3872 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
3873 vmf
->page
->index
= vmf
->pgoff
;
3882 static void ring_buffer_attach(struct perf_event
*event
,
3883 struct ring_buffer
*rb
)
3885 struct ring_buffer
*old_rb
= NULL
;
3886 unsigned long flags
;
3890 * Should be impossible, we set this when removing
3891 * event->rb_entry and wait/clear when adding event->rb_entry.
3893 WARN_ON_ONCE(event
->rcu_pending
);
3896 event
->rcu_batches
= get_state_synchronize_rcu();
3897 event
->rcu_pending
= 1;
3899 spin_lock_irqsave(&old_rb
->event_lock
, flags
);
3900 list_del_rcu(&event
->rb_entry
);
3901 spin_unlock_irqrestore(&old_rb
->event_lock
, flags
);
3904 if (event
->rcu_pending
&& rb
) {
3905 cond_synchronize_rcu(event
->rcu_batches
);
3906 event
->rcu_pending
= 0;
3910 spin_lock_irqsave(&rb
->event_lock
, flags
);
3911 list_add_rcu(&event
->rb_entry
, &rb
->event_list
);
3912 spin_unlock_irqrestore(&rb
->event_lock
, flags
);
3915 rcu_assign_pointer(event
->rb
, rb
);
3918 ring_buffer_put(old_rb
);
3920 * Since we detached before setting the new rb, so that we
3921 * could attach the new rb, we could have missed a wakeup.
3924 wake_up_all(&event
->waitq
);
3928 static void ring_buffer_wakeup(struct perf_event
*event
)
3930 struct ring_buffer
*rb
;
3933 rb
= rcu_dereference(event
->rb
);
3935 list_for_each_entry_rcu(event
, &rb
->event_list
, rb_entry
)
3936 wake_up_all(&event
->waitq
);
3941 static void rb_free_rcu(struct rcu_head
*rcu_head
)
3943 struct ring_buffer
*rb
;
3945 rb
= container_of(rcu_head
, struct ring_buffer
, rcu_head
);
3949 static struct ring_buffer
*ring_buffer_get(struct perf_event
*event
)
3951 struct ring_buffer
*rb
;
3954 rb
= rcu_dereference(event
->rb
);
3956 if (!atomic_inc_not_zero(&rb
->refcount
))
3964 static void ring_buffer_put(struct ring_buffer
*rb
)
3966 if (!atomic_dec_and_test(&rb
->refcount
))
3969 WARN_ON_ONCE(!list_empty(&rb
->event_list
));
3971 call_rcu(&rb
->rcu_head
, rb_free_rcu
);
3974 static void perf_mmap_open(struct vm_area_struct
*vma
)
3976 struct perf_event
*event
= vma
->vm_file
->private_data
;
3978 atomic_inc(&event
->mmap_count
);
3979 atomic_inc(&event
->rb
->mmap_count
);
3983 * A buffer can be mmap()ed multiple times; either directly through the same
3984 * event, or through other events by use of perf_event_set_output().
3986 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3987 * the buffer here, where we still have a VM context. This means we need
3988 * to detach all events redirecting to us.
3990 static void perf_mmap_close(struct vm_area_struct
*vma
)
3992 struct perf_event
*event
= vma
->vm_file
->private_data
;
3994 struct ring_buffer
*rb
= ring_buffer_get(event
);
3995 struct user_struct
*mmap_user
= rb
->mmap_user
;
3996 int mmap_locked
= rb
->mmap_locked
;
3997 unsigned long size
= perf_data_size(rb
);
3999 atomic_dec(&rb
->mmap_count
);
4001 if (!atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
))
4004 ring_buffer_attach(event
, NULL
);
4005 mutex_unlock(&event
->mmap_mutex
);
4007 /* If there's still other mmap()s of this buffer, we're done. */
4008 if (atomic_read(&rb
->mmap_count
))
4012 * No other mmap()s, detach from all other events that might redirect
4013 * into the now unreachable buffer. Somewhat complicated by the
4014 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4018 list_for_each_entry_rcu(event
, &rb
->event_list
, rb_entry
) {
4019 if (!atomic_long_inc_not_zero(&event
->refcount
)) {
4021 * This event is en-route to free_event() which will
4022 * detach it and remove it from the list.
4028 mutex_lock(&event
->mmap_mutex
);
4030 * Check we didn't race with perf_event_set_output() which can
4031 * swizzle the rb from under us while we were waiting to
4032 * acquire mmap_mutex.
4034 * If we find a different rb; ignore this event, a next
4035 * iteration will no longer find it on the list. We have to
4036 * still restart the iteration to make sure we're not now
4037 * iterating the wrong list.
4039 if (event
->rb
== rb
)
4040 ring_buffer_attach(event
, NULL
);
4042 mutex_unlock(&event
->mmap_mutex
);
4046 * Restart the iteration; either we're on the wrong list or
4047 * destroyed its integrity by doing a deletion.
4054 * It could be there's still a few 0-ref events on the list; they'll
4055 * get cleaned up by free_event() -- they'll also still have their
4056 * ref on the rb and will free it whenever they are done with it.
4058 * Aside from that, this buffer is 'fully' detached and unmapped,
4059 * undo the VM accounting.
4062 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &mmap_user
->locked_vm
);
4063 vma
->vm_mm
->pinned_vm
-= mmap_locked
;
4064 free_uid(mmap_user
);
4067 ring_buffer_put(rb
); /* could be last */
4070 static const struct vm_operations_struct perf_mmap_vmops
= {
4071 .open
= perf_mmap_open
,
4072 .close
= perf_mmap_close
,
4073 .fault
= perf_mmap_fault
,
4074 .page_mkwrite
= perf_mmap_fault
,
4077 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
4079 struct perf_event
*event
= file
->private_data
;
4080 unsigned long user_locked
, user_lock_limit
;
4081 struct user_struct
*user
= current_user();
4082 unsigned long locked
, lock_limit
;
4083 struct ring_buffer
*rb
;
4084 unsigned long vma_size
;
4085 unsigned long nr_pages
;
4086 long user_extra
, extra
;
4087 int ret
= 0, flags
= 0;
4090 * Don't allow mmap() of inherited per-task counters. This would
4091 * create a performance issue due to all children writing to the
4094 if (event
->cpu
== -1 && event
->attr
.inherit
)
4097 if (!(vma
->vm_flags
& VM_SHARED
))
4100 vma_size
= vma
->vm_end
- vma
->vm_start
;
4101 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
4104 * If we have rb pages ensure they're a power-of-two number, so we
4105 * can do bitmasks instead of modulo.
4107 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
4110 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
4113 if (vma
->vm_pgoff
!= 0)
4116 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
4118 mutex_lock(&event
->mmap_mutex
);
4120 if (event
->rb
->nr_pages
!= nr_pages
) {
4125 if (!atomic_inc_not_zero(&event
->rb
->mmap_count
)) {
4127 * Raced against perf_mmap_close() through
4128 * perf_event_set_output(). Try again, hope for better
4131 mutex_unlock(&event
->mmap_mutex
);
4138 user_extra
= nr_pages
+ 1;
4139 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
4142 * Increase the limit linearly with more CPUs:
4144 user_lock_limit
*= num_online_cpus();
4146 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
4149 if (user_locked
> user_lock_limit
)
4150 extra
= user_locked
- user_lock_limit
;
4152 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
4153 lock_limit
>>= PAGE_SHIFT
;
4154 locked
= vma
->vm_mm
->pinned_vm
+ extra
;
4156 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
4157 !capable(CAP_IPC_LOCK
)) {
4164 if (vma
->vm_flags
& VM_WRITE
)
4165 flags
|= RING_BUFFER_WRITABLE
;
4167 rb
= rb_alloc(nr_pages
,
4168 event
->attr
.watermark
? event
->attr
.wakeup_watermark
: 0,
4176 atomic_set(&rb
->mmap_count
, 1);
4177 rb
->mmap_locked
= extra
;
4178 rb
->mmap_user
= get_current_user();
4180 atomic_long_add(user_extra
, &user
->locked_vm
);
4181 vma
->vm_mm
->pinned_vm
+= extra
;
4183 ring_buffer_attach(event
, rb
);
4185 perf_event_init_userpage(event
);
4186 perf_event_update_userpage(event
);
4190 atomic_inc(&event
->mmap_count
);
4191 mutex_unlock(&event
->mmap_mutex
);
4194 * Since pinned accounting is per vm we cannot allow fork() to copy our
4197 vma
->vm_flags
|= VM_DONTCOPY
| VM_DONTEXPAND
| VM_DONTDUMP
;
4198 vma
->vm_ops
= &perf_mmap_vmops
;
4203 static int perf_fasync(int fd
, struct file
*filp
, int on
)
4205 struct inode
*inode
= file_inode(filp
);
4206 struct perf_event
*event
= filp
->private_data
;
4209 mutex_lock(&inode
->i_mutex
);
4210 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
4211 mutex_unlock(&inode
->i_mutex
);
4219 static const struct file_operations perf_fops
= {
4220 .llseek
= no_llseek
,
4221 .release
= perf_release
,
4224 .unlocked_ioctl
= perf_ioctl
,
4225 .compat_ioctl
= perf_ioctl
,
4227 .fasync
= perf_fasync
,
4233 * If there's data, ensure we set the poll() state and publish everything
4234 * to user-space before waking everybody up.
4237 void perf_event_wakeup(struct perf_event
*event
)
4239 ring_buffer_wakeup(event
);
4241 if (event
->pending_kill
) {
4242 kill_fasync(&event
->fasync
, SIGIO
, event
->pending_kill
);
4243 event
->pending_kill
= 0;
4247 static void perf_pending_event(struct irq_work
*entry
)
4249 struct perf_event
*event
= container_of(entry
,
4250 struct perf_event
, pending
);
4252 if (event
->pending_disable
) {
4253 event
->pending_disable
= 0;
4254 __perf_event_disable(event
);
4257 if (event
->pending_wakeup
) {
4258 event
->pending_wakeup
= 0;
4259 perf_event_wakeup(event
);
4264 * We assume there is only KVM supporting the callbacks.
4265 * Later on, we might change it to a list if there is
4266 * another virtualization implementation supporting the callbacks.
4268 struct perf_guest_info_callbacks
*perf_guest_cbs
;
4270 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
4272 perf_guest_cbs
= cbs
;
4275 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks
);
4277 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
4279 perf_guest_cbs
= NULL
;
4282 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks
);
4285 perf_output_sample_regs(struct perf_output_handle
*handle
,
4286 struct pt_regs
*regs
, u64 mask
)
4290 for_each_set_bit(bit
, (const unsigned long *) &mask
,
4291 sizeof(mask
) * BITS_PER_BYTE
) {
4294 val
= perf_reg_value(regs
, bit
);
4295 perf_output_put(handle
, val
);
4299 static void perf_sample_regs_user(struct perf_regs_user
*regs_user
,
4300 struct pt_regs
*regs
)
4302 if (!user_mode(regs
)) {
4304 regs
= task_pt_regs(current
);
4310 regs_user
->regs
= regs
;
4311 regs_user
->abi
= perf_reg_abi(current
);
4316 * Get remaining task size from user stack pointer.
4318 * It'd be better to take stack vma map and limit this more
4319 * precisly, but there's no way to get it safely under interrupt,
4320 * so using TASK_SIZE as limit.
4322 static u64
perf_ustack_task_size(struct pt_regs
*regs
)
4324 unsigned long addr
= perf_user_stack_pointer(regs
);
4326 if (!addr
|| addr
>= TASK_SIZE
)
4329 return TASK_SIZE
- addr
;
4333 perf_sample_ustack_size(u16 stack_size
, u16 header_size
,
4334 struct pt_regs
*regs
)
4338 /* No regs, no stack pointer, no dump. */
4343 * Check if we fit in with the requested stack size into the:
4345 * If we don't, we limit the size to the TASK_SIZE.
4347 * - remaining sample size
4348 * If we don't, we customize the stack size to
4349 * fit in to the remaining sample size.
4352 task_size
= min((u64
) USHRT_MAX
, perf_ustack_task_size(regs
));
4353 stack_size
= min(stack_size
, (u16
) task_size
);
4355 /* Current header size plus static size and dynamic size. */
4356 header_size
+= 2 * sizeof(u64
);
4358 /* Do we fit in with the current stack dump size? */
4359 if ((u16
) (header_size
+ stack_size
) < header_size
) {
4361 * If we overflow the maximum size for the sample,
4362 * we customize the stack dump size to fit in.
4364 stack_size
= USHRT_MAX
- header_size
- sizeof(u64
);
4365 stack_size
= round_up(stack_size
, sizeof(u64
));
4372 perf_output_sample_ustack(struct perf_output_handle
*handle
, u64 dump_size
,
4373 struct pt_regs
*regs
)
4375 /* Case of a kernel thread, nothing to dump */
4378 perf_output_put(handle
, size
);
4387 * - the size requested by user or the best one we can fit
4388 * in to the sample max size
4390 * - user stack dump data
4392 * - the actual dumped size
4396 perf_output_put(handle
, dump_size
);
4399 sp
= perf_user_stack_pointer(regs
);
4400 rem
= __output_copy_user(handle
, (void *) sp
, dump_size
);
4401 dyn_size
= dump_size
- rem
;
4403 perf_output_skip(handle
, rem
);
4406 perf_output_put(handle
, dyn_size
);
4410 static void __perf_event_header__init_id(struct perf_event_header
*header
,
4411 struct perf_sample_data
*data
,
4412 struct perf_event
*event
)
4414 u64 sample_type
= event
->attr
.sample_type
;
4416 data
->type
= sample_type
;
4417 header
->size
+= event
->id_header_size
;
4419 if (sample_type
& PERF_SAMPLE_TID
) {
4420 /* namespace issues */
4421 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
4422 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
4425 if (sample_type
& PERF_SAMPLE_TIME
)
4426 data
->time
= perf_clock();
4428 if (sample_type
& (PERF_SAMPLE_ID
| PERF_SAMPLE_IDENTIFIER
))
4429 data
->id
= primary_event_id(event
);
4431 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4432 data
->stream_id
= event
->id
;
4434 if (sample_type
& PERF_SAMPLE_CPU
) {
4435 data
->cpu_entry
.cpu
= raw_smp_processor_id();
4436 data
->cpu_entry
.reserved
= 0;
4440 void perf_event_header__init_id(struct perf_event_header
*header
,
4441 struct perf_sample_data
*data
,
4442 struct perf_event
*event
)
4444 if (event
->attr
.sample_id_all
)
4445 __perf_event_header__init_id(header
, data
, event
);
4448 static void __perf_event__output_id_sample(struct perf_output_handle
*handle
,
4449 struct perf_sample_data
*data
)
4451 u64 sample_type
= data
->type
;
4453 if (sample_type
& PERF_SAMPLE_TID
)
4454 perf_output_put(handle
, data
->tid_entry
);
4456 if (sample_type
& PERF_SAMPLE_TIME
)
4457 perf_output_put(handle
, data
->time
);
4459 if (sample_type
& PERF_SAMPLE_ID
)
4460 perf_output_put(handle
, data
->id
);
4462 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4463 perf_output_put(handle
, data
->stream_id
);
4465 if (sample_type
& PERF_SAMPLE_CPU
)
4466 perf_output_put(handle
, data
->cpu_entry
);
4468 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
4469 perf_output_put(handle
, data
->id
);
4472 void perf_event__output_id_sample(struct perf_event
*event
,
4473 struct perf_output_handle
*handle
,
4474 struct perf_sample_data
*sample
)
4476 if (event
->attr
.sample_id_all
)
4477 __perf_event__output_id_sample(handle
, sample
);
4480 static void perf_output_read_one(struct perf_output_handle
*handle
,
4481 struct perf_event
*event
,
4482 u64 enabled
, u64 running
)
4484 u64 read_format
= event
->attr
.read_format
;
4488 values
[n
++] = perf_event_count(event
);
4489 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
4490 values
[n
++] = enabled
+
4491 atomic64_read(&event
->child_total_time_enabled
);
4493 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
4494 values
[n
++] = running
+
4495 atomic64_read(&event
->child_total_time_running
);
4497 if (read_format
& PERF_FORMAT_ID
)
4498 values
[n
++] = primary_event_id(event
);
4500 __output_copy(handle
, values
, n
* sizeof(u64
));
4504 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4506 static void perf_output_read_group(struct perf_output_handle
*handle
,
4507 struct perf_event
*event
,
4508 u64 enabled
, u64 running
)
4510 struct perf_event
*leader
= event
->group_leader
, *sub
;
4511 u64 read_format
= event
->attr
.read_format
;
4515 values
[n
++] = 1 + leader
->nr_siblings
;
4517 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
4518 values
[n
++] = enabled
;
4520 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
4521 values
[n
++] = running
;
4523 if (leader
!= event
)
4524 leader
->pmu
->read(leader
);
4526 values
[n
++] = perf_event_count(leader
);
4527 if (read_format
& PERF_FORMAT_ID
)
4528 values
[n
++] = primary_event_id(leader
);
4530 __output_copy(handle
, values
, n
* sizeof(u64
));
4532 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
4535 if ((sub
!= event
) &&
4536 (sub
->state
== PERF_EVENT_STATE_ACTIVE
))
4537 sub
->pmu
->read(sub
);
4539 values
[n
++] = perf_event_count(sub
);
4540 if (read_format
& PERF_FORMAT_ID
)
4541 values
[n
++] = primary_event_id(sub
);
4543 __output_copy(handle
, values
, n
* sizeof(u64
));
4547 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4548 PERF_FORMAT_TOTAL_TIME_RUNNING)
4550 static void perf_output_read(struct perf_output_handle
*handle
,
4551 struct perf_event
*event
)
4553 u64 enabled
= 0, running
= 0, now
;
4554 u64 read_format
= event
->attr
.read_format
;
4557 * compute total_time_enabled, total_time_running
4558 * based on snapshot values taken when the event
4559 * was last scheduled in.
4561 * we cannot simply called update_context_time()
4562 * because of locking issue as we are called in
4565 if (read_format
& PERF_FORMAT_TOTAL_TIMES
)
4566 calc_timer_values(event
, &now
, &enabled
, &running
);
4568 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
4569 perf_output_read_group(handle
, event
, enabled
, running
);
4571 perf_output_read_one(handle
, event
, enabled
, running
);
4574 void perf_output_sample(struct perf_output_handle
*handle
,
4575 struct perf_event_header
*header
,
4576 struct perf_sample_data
*data
,
4577 struct perf_event
*event
)
4579 u64 sample_type
= data
->type
;
4581 perf_output_put(handle
, *header
);
4583 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
4584 perf_output_put(handle
, data
->id
);
4586 if (sample_type
& PERF_SAMPLE_IP
)
4587 perf_output_put(handle
, data
->ip
);
4589 if (sample_type
& PERF_SAMPLE_TID
)
4590 perf_output_put(handle
, data
->tid_entry
);
4592 if (sample_type
& PERF_SAMPLE_TIME
)
4593 perf_output_put(handle
, data
->time
);
4595 if (sample_type
& PERF_SAMPLE_ADDR
)
4596 perf_output_put(handle
, data
->addr
);
4598 if (sample_type
& PERF_SAMPLE_ID
)
4599 perf_output_put(handle
, data
->id
);
4601 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4602 perf_output_put(handle
, data
->stream_id
);
4604 if (sample_type
& PERF_SAMPLE_CPU
)
4605 perf_output_put(handle
, data
->cpu_entry
);
4607 if (sample_type
& PERF_SAMPLE_PERIOD
)
4608 perf_output_put(handle
, data
->period
);
4610 if (sample_type
& PERF_SAMPLE_READ
)
4611 perf_output_read(handle
, event
);
4613 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
4614 if (data
->callchain
) {
4617 if (data
->callchain
)
4618 size
+= data
->callchain
->nr
;
4620 size
*= sizeof(u64
);
4622 __output_copy(handle
, data
->callchain
, size
);
4625 perf_output_put(handle
, nr
);
4629 if (sample_type
& PERF_SAMPLE_RAW
) {
4631 perf_output_put(handle
, data
->raw
->size
);
4632 __output_copy(handle
, data
->raw
->data
,
4639 .size
= sizeof(u32
),
4642 perf_output_put(handle
, raw
);
4646 if (sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
4647 if (data
->br_stack
) {
4650 size
= data
->br_stack
->nr
4651 * sizeof(struct perf_branch_entry
);
4653 perf_output_put(handle
, data
->br_stack
->nr
);
4654 perf_output_copy(handle
, data
->br_stack
->entries
, size
);
4657 * we always store at least the value of nr
4660 perf_output_put(handle
, nr
);
4664 if (sample_type
& PERF_SAMPLE_REGS_USER
) {
4665 u64 abi
= data
->regs_user
.abi
;
4668 * If there are no regs to dump, notice it through
4669 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4671 perf_output_put(handle
, abi
);
4674 u64 mask
= event
->attr
.sample_regs_user
;
4675 perf_output_sample_regs(handle
,
4676 data
->regs_user
.regs
,
4681 if (sample_type
& PERF_SAMPLE_STACK_USER
) {
4682 perf_output_sample_ustack(handle
,
4683 data
->stack_user_size
,
4684 data
->regs_user
.regs
);
4687 if (sample_type
& PERF_SAMPLE_WEIGHT
)
4688 perf_output_put(handle
, data
->weight
);
4690 if (sample_type
& PERF_SAMPLE_DATA_SRC
)
4691 perf_output_put(handle
, data
->data_src
.val
);
4693 if (sample_type
& PERF_SAMPLE_TRANSACTION
)
4694 perf_output_put(handle
, data
->txn
);
4696 if (!event
->attr
.watermark
) {
4697 int wakeup_events
= event
->attr
.wakeup_events
;
4699 if (wakeup_events
) {
4700 struct ring_buffer
*rb
= handle
->rb
;
4701 int events
= local_inc_return(&rb
->events
);
4703 if (events
>= wakeup_events
) {
4704 local_sub(wakeup_events
, &rb
->events
);
4705 local_inc(&rb
->wakeup
);
4711 void perf_prepare_sample(struct perf_event_header
*header
,
4712 struct perf_sample_data
*data
,
4713 struct perf_event
*event
,
4714 struct pt_regs
*regs
)
4716 u64 sample_type
= event
->attr
.sample_type
;
4718 header
->type
= PERF_RECORD_SAMPLE
;
4719 header
->size
= sizeof(*header
) + event
->header_size
;
4722 header
->misc
|= perf_misc_flags(regs
);
4724 __perf_event_header__init_id(header
, data
, event
);
4726 if (sample_type
& PERF_SAMPLE_IP
)
4727 data
->ip
= perf_instruction_pointer(regs
);
4729 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
4732 data
->callchain
= perf_callchain(event
, regs
);
4734 if (data
->callchain
)
4735 size
+= data
->callchain
->nr
;
4737 header
->size
+= size
* sizeof(u64
);
4740 if (sample_type
& PERF_SAMPLE_RAW
) {
4741 int size
= sizeof(u32
);
4744 size
+= data
->raw
->size
;
4746 size
+= sizeof(u32
);
4748 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
4749 header
->size
+= size
;
4752 if (sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
4753 int size
= sizeof(u64
); /* nr */
4754 if (data
->br_stack
) {
4755 size
+= data
->br_stack
->nr
4756 * sizeof(struct perf_branch_entry
);
4758 header
->size
+= size
;
4761 if (sample_type
& PERF_SAMPLE_REGS_USER
) {
4762 /* regs dump ABI info */
4763 int size
= sizeof(u64
);
4765 perf_sample_regs_user(&data
->regs_user
, regs
);
4767 if (data
->regs_user
.regs
) {
4768 u64 mask
= event
->attr
.sample_regs_user
;
4769 size
+= hweight64(mask
) * sizeof(u64
);
4772 header
->size
+= size
;
4775 if (sample_type
& PERF_SAMPLE_STACK_USER
) {
4777 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4778 * processed as the last one or have additional check added
4779 * in case new sample type is added, because we could eat
4780 * up the rest of the sample size.
4782 struct perf_regs_user
*uregs
= &data
->regs_user
;
4783 u16 stack_size
= event
->attr
.sample_stack_user
;
4784 u16 size
= sizeof(u64
);
4787 perf_sample_regs_user(uregs
, regs
);
4789 stack_size
= perf_sample_ustack_size(stack_size
, header
->size
,
4793 * If there is something to dump, add space for the dump
4794 * itself and for the field that tells the dynamic size,
4795 * which is how many have been actually dumped.
4798 size
+= sizeof(u64
) + stack_size
;
4800 data
->stack_user_size
= stack_size
;
4801 header
->size
+= size
;
4805 static void perf_event_output(struct perf_event
*event
,
4806 struct perf_sample_data
*data
,
4807 struct pt_regs
*regs
)
4809 struct perf_output_handle handle
;
4810 struct perf_event_header header
;
4812 /* protect the callchain buffers */
4815 perf_prepare_sample(&header
, data
, event
, regs
);
4817 if (perf_output_begin(&handle
, event
, header
.size
))
4820 perf_output_sample(&handle
, &header
, data
, event
);
4822 perf_output_end(&handle
);
4832 struct perf_read_event
{
4833 struct perf_event_header header
;
4840 perf_event_read_event(struct perf_event
*event
,
4841 struct task_struct
*task
)
4843 struct perf_output_handle handle
;
4844 struct perf_sample_data sample
;
4845 struct perf_read_event read_event
= {
4847 .type
= PERF_RECORD_READ
,
4849 .size
= sizeof(read_event
) + event
->read_size
,
4851 .pid
= perf_event_pid(event
, task
),
4852 .tid
= perf_event_tid(event
, task
),
4856 perf_event_header__init_id(&read_event
.header
, &sample
, event
);
4857 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
);
4861 perf_output_put(&handle
, read_event
);
4862 perf_output_read(&handle
, event
);
4863 perf_event__output_id_sample(event
, &handle
, &sample
);
4865 perf_output_end(&handle
);
4868 typedef void (perf_event_aux_output_cb
)(struct perf_event
*event
, void *data
);
4871 perf_event_aux_ctx(struct perf_event_context
*ctx
,
4872 perf_event_aux_output_cb output
,
4875 struct perf_event
*event
;
4877 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
4878 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
4880 if (!event_filter_match(event
))
4882 output(event
, data
);
4887 perf_event_aux(perf_event_aux_output_cb output
, void *data
,
4888 struct perf_event_context
*task_ctx
)
4890 struct perf_cpu_context
*cpuctx
;
4891 struct perf_event_context
*ctx
;
4896 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
4897 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
4898 if (cpuctx
->unique_pmu
!= pmu
)
4900 perf_event_aux_ctx(&cpuctx
->ctx
, output
, data
);
4903 ctxn
= pmu
->task_ctx_nr
;
4906 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
4908 perf_event_aux_ctx(ctx
, output
, data
);
4910 put_cpu_ptr(pmu
->pmu_cpu_context
);
4915 perf_event_aux_ctx(task_ctx
, output
, data
);
4922 * task tracking -- fork/exit
4924 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
4927 struct perf_task_event
{
4928 struct task_struct
*task
;
4929 struct perf_event_context
*task_ctx
;
4932 struct perf_event_header header
;
4942 static int perf_event_task_match(struct perf_event
*event
)
4944 return event
->attr
.comm
|| event
->attr
.mmap
||
4945 event
->attr
.mmap2
|| event
->attr
.mmap_data
||
4949 static void perf_event_task_output(struct perf_event
*event
,
4952 struct perf_task_event
*task_event
= data
;
4953 struct perf_output_handle handle
;
4954 struct perf_sample_data sample
;
4955 struct task_struct
*task
= task_event
->task
;
4956 int ret
, size
= task_event
->event_id
.header
.size
;
4958 if (!perf_event_task_match(event
))
4961 perf_event_header__init_id(&task_event
->event_id
.header
, &sample
, event
);
4963 ret
= perf_output_begin(&handle
, event
,
4964 task_event
->event_id
.header
.size
);
4968 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
4969 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
4971 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
4972 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
4974 perf_output_put(&handle
, task_event
->event_id
);
4976 perf_event__output_id_sample(event
, &handle
, &sample
);
4978 perf_output_end(&handle
);
4980 task_event
->event_id
.header
.size
= size
;
4983 static void perf_event_task(struct task_struct
*task
,
4984 struct perf_event_context
*task_ctx
,
4987 struct perf_task_event task_event
;
4989 if (!atomic_read(&nr_comm_events
) &&
4990 !atomic_read(&nr_mmap_events
) &&
4991 !atomic_read(&nr_task_events
))
4994 task_event
= (struct perf_task_event
){
4996 .task_ctx
= task_ctx
,
4999 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
5001 .size
= sizeof(task_event
.event_id
),
5007 .time
= perf_clock(),
5011 perf_event_aux(perf_event_task_output
,
5016 void perf_event_fork(struct task_struct
*task
)
5018 perf_event_task(task
, NULL
, 1);
5025 struct perf_comm_event
{
5026 struct task_struct
*task
;
5031 struct perf_event_header header
;
5038 static int perf_event_comm_match(struct perf_event
*event
)
5040 return event
->attr
.comm
;
5043 static void perf_event_comm_output(struct perf_event
*event
,
5046 struct perf_comm_event
*comm_event
= data
;
5047 struct perf_output_handle handle
;
5048 struct perf_sample_data sample
;
5049 int size
= comm_event
->event_id
.header
.size
;
5052 if (!perf_event_comm_match(event
))
5055 perf_event_header__init_id(&comm_event
->event_id
.header
, &sample
, event
);
5056 ret
= perf_output_begin(&handle
, event
,
5057 comm_event
->event_id
.header
.size
);
5062 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
5063 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
5065 perf_output_put(&handle
, comm_event
->event_id
);
5066 __output_copy(&handle
, comm_event
->comm
,
5067 comm_event
->comm_size
);
5069 perf_event__output_id_sample(event
, &handle
, &sample
);
5071 perf_output_end(&handle
);
5073 comm_event
->event_id
.header
.size
= size
;
5076 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
5078 char comm
[TASK_COMM_LEN
];
5081 memset(comm
, 0, sizeof(comm
));
5082 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
5083 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
5085 comm_event
->comm
= comm
;
5086 comm_event
->comm_size
= size
;
5088 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
5090 perf_event_aux(perf_event_comm_output
,
5095 void perf_event_comm(struct task_struct
*task
, bool exec
)
5097 struct perf_comm_event comm_event
;
5099 if (!atomic_read(&nr_comm_events
))
5102 comm_event
= (struct perf_comm_event
){
5108 .type
= PERF_RECORD_COMM
,
5109 .misc
= exec
? PERF_RECORD_MISC_COMM_EXEC
: 0,
5117 perf_event_comm_event(&comm_event
);
5124 struct perf_mmap_event
{
5125 struct vm_area_struct
*vma
;
5127 const char *file_name
;
5135 struct perf_event_header header
;
5145 static int perf_event_mmap_match(struct perf_event
*event
,
5148 struct perf_mmap_event
*mmap_event
= data
;
5149 struct vm_area_struct
*vma
= mmap_event
->vma
;
5150 int executable
= vma
->vm_flags
& VM_EXEC
;
5152 return (!executable
&& event
->attr
.mmap_data
) ||
5153 (executable
&& (event
->attr
.mmap
|| event
->attr
.mmap2
));
5156 static void perf_event_mmap_output(struct perf_event
*event
,
5159 struct perf_mmap_event
*mmap_event
= data
;
5160 struct perf_output_handle handle
;
5161 struct perf_sample_data sample
;
5162 int size
= mmap_event
->event_id
.header
.size
;
5165 if (!perf_event_mmap_match(event
, data
))
5168 if (event
->attr
.mmap2
) {
5169 mmap_event
->event_id
.header
.type
= PERF_RECORD_MMAP2
;
5170 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->maj
);
5171 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->min
);
5172 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->ino
);
5173 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->ino_generation
);
5174 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->prot
);
5175 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->flags
);
5178 perf_event_header__init_id(&mmap_event
->event_id
.header
, &sample
, event
);
5179 ret
= perf_output_begin(&handle
, event
,
5180 mmap_event
->event_id
.header
.size
);
5184 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
5185 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
5187 perf_output_put(&handle
, mmap_event
->event_id
);
5189 if (event
->attr
.mmap2
) {
5190 perf_output_put(&handle
, mmap_event
->maj
);
5191 perf_output_put(&handle
, mmap_event
->min
);
5192 perf_output_put(&handle
, mmap_event
->ino
);
5193 perf_output_put(&handle
, mmap_event
->ino_generation
);
5194 perf_output_put(&handle
, mmap_event
->prot
);
5195 perf_output_put(&handle
, mmap_event
->flags
);
5198 __output_copy(&handle
, mmap_event
->file_name
,
5199 mmap_event
->file_size
);
5201 perf_event__output_id_sample(event
, &handle
, &sample
);
5203 perf_output_end(&handle
);
5205 mmap_event
->event_id
.header
.size
= size
;
5208 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
5210 struct vm_area_struct
*vma
= mmap_event
->vma
;
5211 struct file
*file
= vma
->vm_file
;
5212 int maj
= 0, min
= 0;
5213 u64 ino
= 0, gen
= 0;
5214 u32 prot
= 0, flags
= 0;
5221 struct inode
*inode
;
5224 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
5230 * d_path() works from the end of the rb backwards, so we
5231 * need to add enough zero bytes after the string to handle
5232 * the 64bit alignment we do later.
5234 name
= d_path(&file
->f_path
, buf
, PATH_MAX
- sizeof(u64
));
5239 inode
= file_inode(vma
->vm_file
);
5240 dev
= inode
->i_sb
->s_dev
;
5242 gen
= inode
->i_generation
;
5246 if (vma
->vm_flags
& VM_READ
)
5248 if (vma
->vm_flags
& VM_WRITE
)
5250 if (vma
->vm_flags
& VM_EXEC
)
5253 if (vma
->vm_flags
& VM_MAYSHARE
)
5256 flags
= MAP_PRIVATE
;
5258 if (vma
->vm_flags
& VM_DENYWRITE
)
5259 flags
|= MAP_DENYWRITE
;
5260 if (vma
->vm_flags
& VM_MAYEXEC
)
5261 flags
|= MAP_EXECUTABLE
;
5262 if (vma
->vm_flags
& VM_LOCKED
)
5263 flags
|= MAP_LOCKED
;
5264 if (vma
->vm_flags
& VM_HUGETLB
)
5265 flags
|= MAP_HUGETLB
;
5269 name
= (char *)arch_vma_name(vma
);
5273 if (vma
->vm_start
<= vma
->vm_mm
->start_brk
&&
5274 vma
->vm_end
>= vma
->vm_mm
->brk
) {
5278 if (vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
5279 vma
->vm_end
>= vma
->vm_mm
->start_stack
) {
5289 strlcpy(tmp
, name
, sizeof(tmp
));
5293 * Since our buffer works in 8 byte units we need to align our string
5294 * size to a multiple of 8. However, we must guarantee the tail end is
5295 * zero'd out to avoid leaking random bits to userspace.
5297 size
= strlen(name
)+1;
5298 while (!IS_ALIGNED(size
, sizeof(u64
)))
5299 name
[size
++] = '\0';
5301 mmap_event
->file_name
= name
;
5302 mmap_event
->file_size
= size
;
5303 mmap_event
->maj
= maj
;
5304 mmap_event
->min
= min
;
5305 mmap_event
->ino
= ino
;
5306 mmap_event
->ino_generation
= gen
;
5307 mmap_event
->prot
= prot
;
5308 mmap_event
->flags
= flags
;
5310 if (!(vma
->vm_flags
& VM_EXEC
))
5311 mmap_event
->event_id
.header
.misc
|= PERF_RECORD_MISC_MMAP_DATA
;
5313 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
5315 perf_event_aux(perf_event_mmap_output
,
5322 void perf_event_mmap(struct vm_area_struct
*vma
)
5324 struct perf_mmap_event mmap_event
;
5326 if (!atomic_read(&nr_mmap_events
))
5329 mmap_event
= (struct perf_mmap_event
){
5335 .type
= PERF_RECORD_MMAP
,
5336 .misc
= PERF_RECORD_MISC_USER
,
5341 .start
= vma
->vm_start
,
5342 .len
= vma
->vm_end
- vma
->vm_start
,
5343 .pgoff
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
,
5345 /* .maj (attr_mmap2 only) */
5346 /* .min (attr_mmap2 only) */
5347 /* .ino (attr_mmap2 only) */
5348 /* .ino_generation (attr_mmap2 only) */
5349 /* .prot (attr_mmap2 only) */
5350 /* .flags (attr_mmap2 only) */
5353 perf_event_mmap_event(&mmap_event
);
5357 * IRQ throttle logging
5360 static void perf_log_throttle(struct perf_event
*event
, int enable
)
5362 struct perf_output_handle handle
;
5363 struct perf_sample_data sample
;
5367 struct perf_event_header header
;
5371 } throttle_event
= {
5373 .type
= PERF_RECORD_THROTTLE
,
5375 .size
= sizeof(throttle_event
),
5377 .time
= perf_clock(),
5378 .id
= primary_event_id(event
),
5379 .stream_id
= event
->id
,
5383 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
5385 perf_event_header__init_id(&throttle_event
.header
, &sample
, event
);
5387 ret
= perf_output_begin(&handle
, event
,
5388 throttle_event
.header
.size
);
5392 perf_output_put(&handle
, throttle_event
);
5393 perf_event__output_id_sample(event
, &handle
, &sample
);
5394 perf_output_end(&handle
);
5398 * Generic event overflow handling, sampling.
5401 static int __perf_event_overflow(struct perf_event
*event
,
5402 int throttle
, struct perf_sample_data
*data
,
5403 struct pt_regs
*regs
)
5405 int events
= atomic_read(&event
->event_limit
);
5406 struct hw_perf_event
*hwc
= &event
->hw
;
5411 * Non-sampling counters might still use the PMI to fold short
5412 * hardware counters, ignore those.
5414 if (unlikely(!is_sampling_event(event
)))
5417 seq
= __this_cpu_read(perf_throttled_seq
);
5418 if (seq
!= hwc
->interrupts_seq
) {
5419 hwc
->interrupts_seq
= seq
;
5420 hwc
->interrupts
= 1;
5423 if (unlikely(throttle
5424 && hwc
->interrupts
>= max_samples_per_tick
)) {
5425 __this_cpu_inc(perf_throttled_count
);
5426 hwc
->interrupts
= MAX_INTERRUPTS
;
5427 perf_log_throttle(event
, 0);
5428 tick_nohz_full_kick();
5433 if (event
->attr
.freq
) {
5434 u64 now
= perf_clock();
5435 s64 delta
= now
- hwc
->freq_time_stamp
;
5437 hwc
->freq_time_stamp
= now
;
5439 if (delta
> 0 && delta
< 2*TICK_NSEC
)
5440 perf_adjust_period(event
, delta
, hwc
->last_period
, true);
5444 * XXX event_limit might not quite work as expected on inherited
5448 event
->pending_kill
= POLL_IN
;
5449 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
5451 event
->pending_kill
= POLL_HUP
;
5452 event
->pending_disable
= 1;
5453 irq_work_queue(&event
->pending
);
5456 if (event
->overflow_handler
)
5457 event
->overflow_handler(event
, data
, regs
);
5459 perf_event_output(event
, data
, regs
);
5461 if (event
->fasync
&& event
->pending_kill
) {
5462 event
->pending_wakeup
= 1;
5463 irq_work_queue(&event
->pending
);
5469 int perf_event_overflow(struct perf_event
*event
,
5470 struct perf_sample_data
*data
,
5471 struct pt_regs
*regs
)
5473 return __perf_event_overflow(event
, 1, data
, regs
);
5477 * Generic software event infrastructure
5480 struct swevent_htable
{
5481 struct swevent_hlist
*swevent_hlist
;
5482 struct mutex hlist_mutex
;
5485 /* Recursion avoidance in each contexts */
5486 int recursion
[PERF_NR_CONTEXTS
];
5488 /* Keeps track of cpu being initialized/exited */
5492 static DEFINE_PER_CPU(struct swevent_htable
, swevent_htable
);
5495 * We directly increment event->count and keep a second value in
5496 * event->hw.period_left to count intervals. This period event
5497 * is kept in the range [-sample_period, 0] so that we can use the
5501 u64
perf_swevent_set_period(struct perf_event
*event
)
5503 struct hw_perf_event
*hwc
= &event
->hw
;
5504 u64 period
= hwc
->last_period
;
5508 hwc
->last_period
= hwc
->sample_period
;
5511 old
= val
= local64_read(&hwc
->period_left
);
5515 nr
= div64_u64(period
+ val
, period
);
5516 offset
= nr
* period
;
5518 if (local64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
5524 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
5525 struct perf_sample_data
*data
,
5526 struct pt_regs
*regs
)
5528 struct hw_perf_event
*hwc
= &event
->hw
;
5532 overflow
= perf_swevent_set_period(event
);
5534 if (hwc
->interrupts
== MAX_INTERRUPTS
)
5537 for (; overflow
; overflow
--) {
5538 if (__perf_event_overflow(event
, throttle
,
5541 * We inhibit the overflow from happening when
5542 * hwc->interrupts == MAX_INTERRUPTS.
5550 static void perf_swevent_event(struct perf_event
*event
, u64 nr
,
5551 struct perf_sample_data
*data
,
5552 struct pt_regs
*regs
)
5554 struct hw_perf_event
*hwc
= &event
->hw
;
5556 local64_add(nr
, &event
->count
);
5561 if (!is_sampling_event(event
))
5564 if ((event
->attr
.sample_type
& PERF_SAMPLE_PERIOD
) && !event
->attr
.freq
) {
5566 return perf_swevent_overflow(event
, 1, data
, regs
);
5568 data
->period
= event
->hw
.last_period
;
5570 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
5571 return perf_swevent_overflow(event
, 1, data
, regs
);
5573 if (local64_add_negative(nr
, &hwc
->period_left
))
5576 perf_swevent_overflow(event
, 0, data
, regs
);
5579 static int perf_exclude_event(struct perf_event
*event
,
5580 struct pt_regs
*regs
)
5582 if (event
->hw
.state
& PERF_HES_STOPPED
)
5586 if (event
->attr
.exclude_user
&& user_mode(regs
))
5589 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
5596 static int perf_swevent_match(struct perf_event
*event
,
5597 enum perf_type_id type
,
5599 struct perf_sample_data
*data
,
5600 struct pt_regs
*regs
)
5602 if (event
->attr
.type
!= type
)
5605 if (event
->attr
.config
!= event_id
)
5608 if (perf_exclude_event(event
, regs
))
5614 static inline u64
swevent_hash(u64 type
, u32 event_id
)
5616 u64 val
= event_id
| (type
<< 32);
5618 return hash_64(val
, SWEVENT_HLIST_BITS
);
5621 static inline struct hlist_head
*
5622 __find_swevent_head(struct swevent_hlist
*hlist
, u64 type
, u32 event_id
)
5624 u64 hash
= swevent_hash(type
, event_id
);
5626 return &hlist
->heads
[hash
];
5629 /* For the read side: events when they trigger */
5630 static inline struct hlist_head
*
5631 find_swevent_head_rcu(struct swevent_htable
*swhash
, u64 type
, u32 event_id
)
5633 struct swevent_hlist
*hlist
;
5635 hlist
= rcu_dereference(swhash
->swevent_hlist
);
5639 return __find_swevent_head(hlist
, type
, event_id
);
5642 /* For the event head insertion and removal in the hlist */
5643 static inline struct hlist_head
*
5644 find_swevent_head(struct swevent_htable
*swhash
, struct perf_event
*event
)
5646 struct swevent_hlist
*hlist
;
5647 u32 event_id
= event
->attr
.config
;
5648 u64 type
= event
->attr
.type
;
5651 * Event scheduling is always serialized against hlist allocation
5652 * and release. Which makes the protected version suitable here.
5653 * The context lock guarantees that.
5655 hlist
= rcu_dereference_protected(swhash
->swevent_hlist
,
5656 lockdep_is_held(&event
->ctx
->lock
));
5660 return __find_swevent_head(hlist
, type
, event_id
);
5663 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
5665 struct perf_sample_data
*data
,
5666 struct pt_regs
*regs
)
5668 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5669 struct perf_event
*event
;
5670 struct hlist_head
*head
;
5673 head
= find_swevent_head_rcu(swhash
, type
, event_id
);
5677 hlist_for_each_entry_rcu(event
, head
, hlist_entry
) {
5678 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
5679 perf_swevent_event(event
, nr
, data
, regs
);
5685 int perf_swevent_get_recursion_context(void)
5687 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5689 return get_recursion_context(swhash
->recursion
);
5691 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
5693 inline void perf_swevent_put_recursion_context(int rctx
)
5695 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5697 put_recursion_context(swhash
->recursion
, rctx
);
5700 void __perf_sw_event(u32 event_id
, u64 nr
, struct pt_regs
*regs
, u64 addr
)
5702 struct perf_sample_data data
;
5705 preempt_disable_notrace();
5706 rctx
= perf_swevent_get_recursion_context();
5710 perf_sample_data_init(&data
, addr
, 0);
5712 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, &data
, regs
);
5714 perf_swevent_put_recursion_context(rctx
);
5715 preempt_enable_notrace();
5718 static void perf_swevent_read(struct perf_event
*event
)
5722 static int perf_swevent_add(struct perf_event
*event
, int flags
)
5724 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5725 struct hw_perf_event
*hwc
= &event
->hw
;
5726 struct hlist_head
*head
;
5728 if (is_sampling_event(event
)) {
5729 hwc
->last_period
= hwc
->sample_period
;
5730 perf_swevent_set_period(event
);
5733 hwc
->state
= !(flags
& PERF_EF_START
);
5735 head
= find_swevent_head(swhash
, event
);
5738 * We can race with cpu hotplug code. Do not
5739 * WARN if the cpu just got unplugged.
5741 WARN_ON_ONCE(swhash
->online
);
5745 hlist_add_head_rcu(&event
->hlist_entry
, head
);
5750 static void perf_swevent_del(struct perf_event
*event
, int flags
)
5752 hlist_del_rcu(&event
->hlist_entry
);
5755 static void perf_swevent_start(struct perf_event
*event
, int flags
)
5757 event
->hw
.state
= 0;
5760 static void perf_swevent_stop(struct perf_event
*event
, int flags
)
5762 event
->hw
.state
= PERF_HES_STOPPED
;
5765 /* Deref the hlist from the update side */
5766 static inline struct swevent_hlist
*
5767 swevent_hlist_deref(struct swevent_htable
*swhash
)
5769 return rcu_dereference_protected(swhash
->swevent_hlist
,
5770 lockdep_is_held(&swhash
->hlist_mutex
));
5773 static void swevent_hlist_release(struct swevent_htable
*swhash
)
5775 struct swevent_hlist
*hlist
= swevent_hlist_deref(swhash
);
5780 rcu_assign_pointer(swhash
->swevent_hlist
, NULL
);
5781 kfree_rcu(hlist
, rcu_head
);
5784 static void swevent_hlist_put_cpu(struct perf_event
*event
, int cpu
)
5786 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
5788 mutex_lock(&swhash
->hlist_mutex
);
5790 if (!--swhash
->hlist_refcount
)
5791 swevent_hlist_release(swhash
);
5793 mutex_unlock(&swhash
->hlist_mutex
);
5796 static void swevent_hlist_put(struct perf_event
*event
)
5800 for_each_possible_cpu(cpu
)
5801 swevent_hlist_put_cpu(event
, cpu
);
5804 static int swevent_hlist_get_cpu(struct perf_event
*event
, int cpu
)
5806 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
5809 mutex_lock(&swhash
->hlist_mutex
);
5811 if (!swevent_hlist_deref(swhash
) && cpu_online(cpu
)) {
5812 struct swevent_hlist
*hlist
;
5814 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
5819 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
5821 swhash
->hlist_refcount
++;
5823 mutex_unlock(&swhash
->hlist_mutex
);
5828 static int swevent_hlist_get(struct perf_event
*event
)
5831 int cpu
, failed_cpu
;
5834 for_each_possible_cpu(cpu
) {
5835 err
= swevent_hlist_get_cpu(event
, cpu
);
5845 for_each_possible_cpu(cpu
) {
5846 if (cpu
== failed_cpu
)
5848 swevent_hlist_put_cpu(event
, cpu
);
5855 struct static_key perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
5857 static void sw_perf_event_destroy(struct perf_event
*event
)
5859 u64 event_id
= event
->attr
.config
;
5861 WARN_ON(event
->parent
);
5863 static_key_slow_dec(&perf_swevent_enabled
[event_id
]);
5864 swevent_hlist_put(event
);
5867 static int perf_swevent_init(struct perf_event
*event
)
5869 u64 event_id
= event
->attr
.config
;
5871 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
5875 * no branch sampling for software events
5877 if (has_branch_stack(event
))
5881 case PERF_COUNT_SW_CPU_CLOCK
:
5882 case PERF_COUNT_SW_TASK_CLOCK
:
5889 if (event_id
>= PERF_COUNT_SW_MAX
)
5892 if (!event
->parent
) {
5895 err
= swevent_hlist_get(event
);
5899 static_key_slow_inc(&perf_swevent_enabled
[event_id
]);
5900 event
->destroy
= sw_perf_event_destroy
;
5906 static int perf_swevent_event_idx(struct perf_event
*event
)
5911 static struct pmu perf_swevent
= {
5912 .task_ctx_nr
= perf_sw_context
,
5914 .event_init
= perf_swevent_init
,
5915 .add
= perf_swevent_add
,
5916 .del
= perf_swevent_del
,
5917 .start
= perf_swevent_start
,
5918 .stop
= perf_swevent_stop
,
5919 .read
= perf_swevent_read
,
5921 .event_idx
= perf_swevent_event_idx
,
5924 #ifdef CONFIG_EVENT_TRACING
5926 static int perf_tp_filter_match(struct perf_event
*event
,
5927 struct perf_sample_data
*data
)
5929 void *record
= data
->raw
->data
;
5931 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
5936 static int perf_tp_event_match(struct perf_event
*event
,
5937 struct perf_sample_data
*data
,
5938 struct pt_regs
*regs
)
5940 if (event
->hw
.state
& PERF_HES_STOPPED
)
5943 * All tracepoints are from kernel-space.
5945 if (event
->attr
.exclude_kernel
)
5948 if (!perf_tp_filter_match(event
, data
))
5954 void perf_tp_event(u64 addr
, u64 count
, void *record
, int entry_size
,
5955 struct pt_regs
*regs
, struct hlist_head
*head
, int rctx
,
5956 struct task_struct
*task
)
5958 struct perf_sample_data data
;
5959 struct perf_event
*event
;
5961 struct perf_raw_record raw
= {
5966 perf_sample_data_init(&data
, addr
, 0);
5969 hlist_for_each_entry_rcu(event
, head
, hlist_entry
) {
5970 if (perf_tp_event_match(event
, &data
, regs
))
5971 perf_swevent_event(event
, count
, &data
, regs
);
5975 * If we got specified a target task, also iterate its context and
5976 * deliver this event there too.
5978 if (task
&& task
!= current
) {
5979 struct perf_event_context
*ctx
;
5980 struct trace_entry
*entry
= record
;
5983 ctx
= rcu_dereference(task
->perf_event_ctxp
[perf_sw_context
]);
5987 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
5988 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
5990 if (event
->attr
.config
!= entry
->type
)
5992 if (perf_tp_event_match(event
, &data
, regs
))
5993 perf_swevent_event(event
, count
, &data
, regs
);
5999 perf_swevent_put_recursion_context(rctx
);
6001 EXPORT_SYMBOL_GPL(perf_tp_event
);
6003 static void tp_perf_event_destroy(struct perf_event
*event
)
6005 perf_trace_destroy(event
);
6008 static int perf_tp_event_init(struct perf_event
*event
)
6012 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
6016 * no branch sampling for tracepoint events
6018 if (has_branch_stack(event
))
6021 err
= perf_trace_init(event
);
6025 event
->destroy
= tp_perf_event_destroy
;
6030 static struct pmu perf_tracepoint
= {
6031 .task_ctx_nr
= perf_sw_context
,
6033 .event_init
= perf_tp_event_init
,
6034 .add
= perf_trace_add
,
6035 .del
= perf_trace_del
,
6036 .start
= perf_swevent_start
,
6037 .stop
= perf_swevent_stop
,
6038 .read
= perf_swevent_read
,
6040 .event_idx
= perf_swevent_event_idx
,
6043 static inline void perf_tp_register(void)
6045 perf_pmu_register(&perf_tracepoint
, "tracepoint", PERF_TYPE_TRACEPOINT
);
6048 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
6053 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
6056 filter_str
= strndup_user(arg
, PAGE_SIZE
);
6057 if (IS_ERR(filter_str
))
6058 return PTR_ERR(filter_str
);
6060 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
, filter_str
);
6066 static void perf_event_free_filter(struct perf_event
*event
)
6068 ftrace_profile_free_filter(event
);
6073 static inline void perf_tp_register(void)
6077 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
6082 static void perf_event_free_filter(struct perf_event
*event
)
6086 #endif /* CONFIG_EVENT_TRACING */
6088 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6089 void perf_bp_event(struct perf_event
*bp
, void *data
)
6091 struct perf_sample_data sample
;
6092 struct pt_regs
*regs
= data
;
6094 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
, 0);
6096 if (!bp
->hw
.state
&& !perf_exclude_event(bp
, regs
))
6097 perf_swevent_event(bp
, 1, &sample
, regs
);
6102 * hrtimer based swevent callback
6105 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
6107 enum hrtimer_restart ret
= HRTIMER_RESTART
;
6108 struct perf_sample_data data
;
6109 struct pt_regs
*regs
;
6110 struct perf_event
*event
;
6113 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
6115 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
6116 return HRTIMER_NORESTART
;
6118 event
->pmu
->read(event
);
6120 perf_sample_data_init(&data
, 0, event
->hw
.last_period
);
6121 regs
= get_irq_regs();
6123 if (regs
&& !perf_exclude_event(event
, regs
)) {
6124 if (!(event
->attr
.exclude_idle
&& is_idle_task(current
)))
6125 if (__perf_event_overflow(event
, 1, &data
, regs
))
6126 ret
= HRTIMER_NORESTART
;
6129 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
6130 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
6135 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
6137 struct hw_perf_event
*hwc
= &event
->hw
;
6140 if (!is_sampling_event(event
))
6143 period
= local64_read(&hwc
->period_left
);
6148 local64_set(&hwc
->period_left
, 0);
6150 period
= max_t(u64
, 10000, hwc
->sample_period
);
6152 __hrtimer_start_range_ns(&hwc
->hrtimer
,
6153 ns_to_ktime(period
), 0,
6154 HRTIMER_MODE_REL_PINNED
, 0);
6157 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
6159 struct hw_perf_event
*hwc
= &event
->hw
;
6161 if (is_sampling_event(event
)) {
6162 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
6163 local64_set(&hwc
->period_left
, ktime_to_ns(remaining
));
6165 hrtimer_cancel(&hwc
->hrtimer
);
6169 static void perf_swevent_init_hrtimer(struct perf_event
*event
)
6171 struct hw_perf_event
*hwc
= &event
->hw
;
6173 if (!is_sampling_event(event
))
6176 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
6177 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
6180 * Since hrtimers have a fixed rate, we can do a static freq->period
6181 * mapping and avoid the whole period adjust feedback stuff.
6183 if (event
->attr
.freq
) {
6184 long freq
= event
->attr
.sample_freq
;
6186 event
->attr
.sample_period
= NSEC_PER_SEC
/ freq
;
6187 hwc
->sample_period
= event
->attr
.sample_period
;
6188 local64_set(&hwc
->period_left
, hwc
->sample_period
);
6189 hwc
->last_period
= hwc
->sample_period
;
6190 event
->attr
.freq
= 0;
6195 * Software event: cpu wall time clock
6198 static void cpu_clock_event_update(struct perf_event
*event
)
6203 now
= local_clock();
6204 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
6205 local64_add(now
- prev
, &event
->count
);
6208 static void cpu_clock_event_start(struct perf_event
*event
, int flags
)
6210 local64_set(&event
->hw
.prev_count
, local_clock());
6211 perf_swevent_start_hrtimer(event
);
6214 static void cpu_clock_event_stop(struct perf_event
*event
, int flags
)
6216 perf_swevent_cancel_hrtimer(event
);
6217 cpu_clock_event_update(event
);
6220 static int cpu_clock_event_add(struct perf_event
*event
, int flags
)
6222 if (flags
& PERF_EF_START
)
6223 cpu_clock_event_start(event
, flags
);
6228 static void cpu_clock_event_del(struct perf_event
*event
, int flags
)
6230 cpu_clock_event_stop(event
, flags
);
6233 static void cpu_clock_event_read(struct perf_event
*event
)
6235 cpu_clock_event_update(event
);
6238 static int cpu_clock_event_init(struct perf_event
*event
)
6240 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
6243 if (event
->attr
.config
!= PERF_COUNT_SW_CPU_CLOCK
)
6247 * no branch sampling for software events
6249 if (has_branch_stack(event
))
6252 perf_swevent_init_hrtimer(event
);
6257 static struct pmu perf_cpu_clock
= {
6258 .task_ctx_nr
= perf_sw_context
,
6260 .event_init
= cpu_clock_event_init
,
6261 .add
= cpu_clock_event_add
,
6262 .del
= cpu_clock_event_del
,
6263 .start
= cpu_clock_event_start
,
6264 .stop
= cpu_clock_event_stop
,
6265 .read
= cpu_clock_event_read
,
6267 .event_idx
= perf_swevent_event_idx
,
6271 * Software event: task time clock
6274 static void task_clock_event_update(struct perf_event
*event
, u64 now
)
6279 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
6281 local64_add(delta
, &event
->count
);
6284 static void task_clock_event_start(struct perf_event
*event
, int flags
)
6286 local64_set(&event
->hw
.prev_count
, event
->ctx
->time
);
6287 perf_swevent_start_hrtimer(event
);
6290 static void task_clock_event_stop(struct perf_event
*event
, int flags
)
6292 perf_swevent_cancel_hrtimer(event
);
6293 task_clock_event_update(event
, event
->ctx
->time
);
6296 static int task_clock_event_add(struct perf_event
*event
, int flags
)
6298 if (flags
& PERF_EF_START
)
6299 task_clock_event_start(event
, flags
);
6304 static void task_clock_event_del(struct perf_event
*event
, int flags
)
6306 task_clock_event_stop(event
, PERF_EF_UPDATE
);
6309 static void task_clock_event_read(struct perf_event
*event
)
6311 u64 now
= perf_clock();
6312 u64 delta
= now
- event
->ctx
->timestamp
;
6313 u64 time
= event
->ctx
->time
+ delta
;
6315 task_clock_event_update(event
, time
);
6318 static int task_clock_event_init(struct perf_event
*event
)
6320 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
6323 if (event
->attr
.config
!= PERF_COUNT_SW_TASK_CLOCK
)
6327 * no branch sampling for software events
6329 if (has_branch_stack(event
))
6332 perf_swevent_init_hrtimer(event
);
6337 static struct pmu perf_task_clock
= {
6338 .task_ctx_nr
= perf_sw_context
,
6340 .event_init
= task_clock_event_init
,
6341 .add
= task_clock_event_add
,
6342 .del
= task_clock_event_del
,
6343 .start
= task_clock_event_start
,
6344 .stop
= task_clock_event_stop
,
6345 .read
= task_clock_event_read
,
6347 .event_idx
= perf_swevent_event_idx
,
6350 static void perf_pmu_nop_void(struct pmu
*pmu
)
6354 static int perf_pmu_nop_int(struct pmu
*pmu
)
6359 static void perf_pmu_start_txn(struct pmu
*pmu
)
6361 perf_pmu_disable(pmu
);
6364 static int perf_pmu_commit_txn(struct pmu
*pmu
)
6366 perf_pmu_enable(pmu
);
6370 static void perf_pmu_cancel_txn(struct pmu
*pmu
)
6372 perf_pmu_enable(pmu
);
6375 static int perf_event_idx_default(struct perf_event
*event
)
6377 return event
->hw
.idx
+ 1;
6381 * Ensures all contexts with the same task_ctx_nr have the same
6382 * pmu_cpu_context too.
6384 static struct perf_cpu_context __percpu
*find_pmu_context(int ctxn
)
6391 list_for_each_entry(pmu
, &pmus
, entry
) {
6392 if (pmu
->task_ctx_nr
== ctxn
)
6393 return pmu
->pmu_cpu_context
;
6399 static void update_pmu_context(struct pmu
*pmu
, struct pmu
*old_pmu
)
6403 for_each_possible_cpu(cpu
) {
6404 struct perf_cpu_context
*cpuctx
;
6406 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
6408 if (cpuctx
->unique_pmu
== old_pmu
)
6409 cpuctx
->unique_pmu
= pmu
;
6413 static void free_pmu_context(struct pmu
*pmu
)
6417 mutex_lock(&pmus_lock
);
6419 * Like a real lame refcount.
6421 list_for_each_entry(i
, &pmus
, entry
) {
6422 if (i
->pmu_cpu_context
== pmu
->pmu_cpu_context
) {
6423 update_pmu_context(i
, pmu
);
6428 free_percpu(pmu
->pmu_cpu_context
);
6430 mutex_unlock(&pmus_lock
);
6432 static struct idr pmu_idr
;
6435 type_show(struct device
*dev
, struct device_attribute
*attr
, char *page
)
6437 struct pmu
*pmu
= dev_get_drvdata(dev
);
6439 return snprintf(page
, PAGE_SIZE
-1, "%d\n", pmu
->type
);
6441 static DEVICE_ATTR_RO(type
);
6444 perf_event_mux_interval_ms_show(struct device
*dev
,
6445 struct device_attribute
*attr
,
6448 struct pmu
*pmu
= dev_get_drvdata(dev
);
6450 return snprintf(page
, PAGE_SIZE
-1, "%d\n", pmu
->hrtimer_interval_ms
);
6454 perf_event_mux_interval_ms_store(struct device
*dev
,
6455 struct device_attribute
*attr
,
6456 const char *buf
, size_t count
)
6458 struct pmu
*pmu
= dev_get_drvdata(dev
);
6459 int timer
, cpu
, ret
;
6461 ret
= kstrtoint(buf
, 0, &timer
);
6468 /* same value, noting to do */
6469 if (timer
== pmu
->hrtimer_interval_ms
)
6472 pmu
->hrtimer_interval_ms
= timer
;
6474 /* update all cpuctx for this PMU */
6475 for_each_possible_cpu(cpu
) {
6476 struct perf_cpu_context
*cpuctx
;
6477 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
6478 cpuctx
->hrtimer_interval
= ns_to_ktime(NSEC_PER_MSEC
* timer
);
6480 if (hrtimer_active(&cpuctx
->hrtimer
))
6481 hrtimer_forward_now(&cpuctx
->hrtimer
, cpuctx
->hrtimer_interval
);
6486 static DEVICE_ATTR_RW(perf_event_mux_interval_ms
);
6488 static struct attribute
*pmu_dev_attrs
[] = {
6489 &dev_attr_type
.attr
,
6490 &dev_attr_perf_event_mux_interval_ms
.attr
,
6493 ATTRIBUTE_GROUPS(pmu_dev
);
6495 static int pmu_bus_running
;
6496 static struct bus_type pmu_bus
= {
6497 .name
= "event_source",
6498 .dev_groups
= pmu_dev_groups
,
6501 static void pmu_dev_release(struct device
*dev
)
6506 static int pmu_dev_alloc(struct pmu
*pmu
)
6510 pmu
->dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
6514 pmu
->dev
->groups
= pmu
->attr_groups
;
6515 device_initialize(pmu
->dev
);
6516 ret
= dev_set_name(pmu
->dev
, "%s", pmu
->name
);
6520 dev_set_drvdata(pmu
->dev
, pmu
);
6521 pmu
->dev
->bus
= &pmu_bus
;
6522 pmu
->dev
->release
= pmu_dev_release
;
6523 ret
= device_add(pmu
->dev
);
6531 put_device(pmu
->dev
);
6535 static struct lock_class_key cpuctx_mutex
;
6536 static struct lock_class_key cpuctx_lock
;
6538 int perf_pmu_register(struct pmu
*pmu
, const char *name
, int type
)
6542 mutex_lock(&pmus_lock
);
6544 pmu
->pmu_disable_count
= alloc_percpu(int);
6545 if (!pmu
->pmu_disable_count
)
6554 type
= idr_alloc(&pmu_idr
, pmu
, PERF_TYPE_MAX
, 0, GFP_KERNEL
);
6562 if (pmu_bus_running
) {
6563 ret
= pmu_dev_alloc(pmu
);
6569 pmu
->pmu_cpu_context
= find_pmu_context(pmu
->task_ctx_nr
);
6570 if (pmu
->pmu_cpu_context
)
6571 goto got_cpu_context
;
6574 pmu
->pmu_cpu_context
= alloc_percpu(struct perf_cpu_context
);
6575 if (!pmu
->pmu_cpu_context
)
6578 for_each_possible_cpu(cpu
) {
6579 struct perf_cpu_context
*cpuctx
;
6581 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
6582 __perf_event_init_context(&cpuctx
->ctx
);
6583 lockdep_set_class(&cpuctx
->ctx
.mutex
, &cpuctx_mutex
);
6584 lockdep_set_class(&cpuctx
->ctx
.lock
, &cpuctx_lock
);
6585 cpuctx
->ctx
.type
= cpu_context
;
6586 cpuctx
->ctx
.pmu
= pmu
;
6588 __perf_cpu_hrtimer_init(cpuctx
, cpu
);
6590 INIT_LIST_HEAD(&cpuctx
->rotation_list
);
6591 cpuctx
->unique_pmu
= pmu
;
6595 if (!pmu
->start_txn
) {
6596 if (pmu
->pmu_enable
) {
6598 * If we have pmu_enable/pmu_disable calls, install
6599 * transaction stubs that use that to try and batch
6600 * hardware accesses.
6602 pmu
->start_txn
= perf_pmu_start_txn
;
6603 pmu
->commit_txn
= perf_pmu_commit_txn
;
6604 pmu
->cancel_txn
= perf_pmu_cancel_txn
;
6606 pmu
->start_txn
= perf_pmu_nop_void
;
6607 pmu
->commit_txn
= perf_pmu_nop_int
;
6608 pmu
->cancel_txn
= perf_pmu_nop_void
;
6612 if (!pmu
->pmu_enable
) {
6613 pmu
->pmu_enable
= perf_pmu_nop_void
;
6614 pmu
->pmu_disable
= perf_pmu_nop_void
;
6617 if (!pmu
->event_idx
)
6618 pmu
->event_idx
= perf_event_idx_default
;
6620 list_add_rcu(&pmu
->entry
, &pmus
);
6623 mutex_unlock(&pmus_lock
);
6628 device_del(pmu
->dev
);
6629 put_device(pmu
->dev
);
6632 if (pmu
->type
>= PERF_TYPE_MAX
)
6633 idr_remove(&pmu_idr
, pmu
->type
);
6636 free_percpu(pmu
->pmu_disable_count
);
6639 EXPORT_SYMBOL_GPL(perf_pmu_register
);
6641 void perf_pmu_unregister(struct pmu
*pmu
)
6643 mutex_lock(&pmus_lock
);
6644 list_del_rcu(&pmu
->entry
);
6645 mutex_unlock(&pmus_lock
);
6648 * We dereference the pmu list under both SRCU and regular RCU, so
6649 * synchronize against both of those.
6651 synchronize_srcu(&pmus_srcu
);
6654 free_percpu(pmu
->pmu_disable_count
);
6655 if (pmu
->type
>= PERF_TYPE_MAX
)
6656 idr_remove(&pmu_idr
, pmu
->type
);
6657 device_del(pmu
->dev
);
6658 put_device(pmu
->dev
);
6659 free_pmu_context(pmu
);
6661 EXPORT_SYMBOL_GPL(perf_pmu_unregister
);
6663 struct pmu
*perf_init_event(struct perf_event
*event
)
6665 struct pmu
*pmu
= NULL
;
6669 idx
= srcu_read_lock(&pmus_srcu
);
6672 pmu
= idr_find(&pmu_idr
, event
->attr
.type
);
6675 if (!try_module_get(pmu
->module
)) {
6676 pmu
= ERR_PTR(-ENODEV
);
6680 ret
= pmu
->event_init(event
);
6686 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
6687 if (!try_module_get(pmu
->module
)) {
6688 pmu
= ERR_PTR(-ENODEV
);
6692 ret
= pmu
->event_init(event
);
6696 if (ret
!= -ENOENT
) {
6701 pmu
= ERR_PTR(-ENOENT
);
6703 srcu_read_unlock(&pmus_srcu
, idx
);
6708 static void account_event_cpu(struct perf_event
*event
, int cpu
)
6713 if (has_branch_stack(event
)) {
6714 if (!(event
->attach_state
& PERF_ATTACH_TASK
))
6715 atomic_inc(&per_cpu(perf_branch_stack_events
, cpu
));
6717 if (is_cgroup_event(event
))
6718 atomic_inc(&per_cpu(perf_cgroup_events
, cpu
));
6721 static void account_event(struct perf_event
*event
)
6726 if (event
->attach_state
& PERF_ATTACH_TASK
)
6727 static_key_slow_inc(&perf_sched_events
.key
);
6728 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
6729 atomic_inc(&nr_mmap_events
);
6730 if (event
->attr
.comm
)
6731 atomic_inc(&nr_comm_events
);
6732 if (event
->attr
.task
)
6733 atomic_inc(&nr_task_events
);
6734 if (event
->attr
.freq
) {
6735 if (atomic_inc_return(&nr_freq_events
) == 1)
6736 tick_nohz_full_kick_all();
6738 if (has_branch_stack(event
))
6739 static_key_slow_inc(&perf_sched_events
.key
);
6740 if (is_cgroup_event(event
))
6741 static_key_slow_inc(&perf_sched_events
.key
);
6743 account_event_cpu(event
, event
->cpu
);
6747 * Allocate and initialize a event structure
6749 static struct perf_event
*
6750 perf_event_alloc(struct perf_event_attr
*attr
, int cpu
,
6751 struct task_struct
*task
,
6752 struct perf_event
*group_leader
,
6753 struct perf_event
*parent_event
,
6754 perf_overflow_handler_t overflow_handler
,
6758 struct perf_event
*event
;
6759 struct hw_perf_event
*hwc
;
6762 if ((unsigned)cpu
>= nr_cpu_ids
) {
6763 if (!task
|| cpu
!= -1)
6764 return ERR_PTR(-EINVAL
);
6767 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
6769 return ERR_PTR(-ENOMEM
);
6772 * Single events are their own group leaders, with an
6773 * empty sibling list:
6776 group_leader
= event
;
6778 mutex_init(&event
->child_mutex
);
6779 INIT_LIST_HEAD(&event
->child_list
);
6781 INIT_LIST_HEAD(&event
->group_entry
);
6782 INIT_LIST_HEAD(&event
->event_entry
);
6783 INIT_LIST_HEAD(&event
->sibling_list
);
6784 INIT_LIST_HEAD(&event
->rb_entry
);
6785 INIT_LIST_HEAD(&event
->active_entry
);
6786 INIT_HLIST_NODE(&event
->hlist_entry
);
6789 init_waitqueue_head(&event
->waitq
);
6790 init_irq_work(&event
->pending
, perf_pending_event
);
6792 mutex_init(&event
->mmap_mutex
);
6794 atomic_long_set(&event
->refcount
, 1);
6796 event
->attr
= *attr
;
6797 event
->group_leader
= group_leader
;
6801 event
->parent
= parent_event
;
6803 event
->ns
= get_pid_ns(task_active_pid_ns(current
));
6804 event
->id
= atomic64_inc_return(&perf_event_id
);
6806 event
->state
= PERF_EVENT_STATE_INACTIVE
;
6809 event
->attach_state
= PERF_ATTACH_TASK
;
6811 if (attr
->type
== PERF_TYPE_TRACEPOINT
)
6812 event
->hw
.tp_target
= task
;
6813 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6815 * hw_breakpoint is a bit difficult here..
6817 else if (attr
->type
== PERF_TYPE_BREAKPOINT
)
6818 event
->hw
.bp_target
= task
;
6822 if (!overflow_handler
&& parent_event
) {
6823 overflow_handler
= parent_event
->overflow_handler
;
6824 context
= parent_event
->overflow_handler_context
;
6827 event
->overflow_handler
= overflow_handler
;
6828 event
->overflow_handler_context
= context
;
6830 perf_event__state_init(event
);
6835 hwc
->sample_period
= attr
->sample_period
;
6836 if (attr
->freq
&& attr
->sample_freq
)
6837 hwc
->sample_period
= 1;
6838 hwc
->last_period
= hwc
->sample_period
;
6840 local64_set(&hwc
->period_left
, hwc
->sample_period
);
6843 * we currently do not support PERF_FORMAT_GROUP on inherited events
6845 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
6848 pmu
= perf_init_event(event
);
6851 else if (IS_ERR(pmu
)) {
6856 if (!event
->parent
) {
6857 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
6858 err
= get_callchain_buffers();
6868 event
->destroy(event
);
6869 module_put(pmu
->module
);
6872 put_pid_ns(event
->ns
);
6875 return ERR_PTR(err
);
6878 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
6879 struct perf_event_attr
*attr
)
6884 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
6888 * zero the full structure, so that a short copy will be nice.
6890 memset(attr
, 0, sizeof(*attr
));
6892 ret
= get_user(size
, &uattr
->size
);
6896 if (size
> PAGE_SIZE
) /* silly large */
6899 if (!size
) /* abi compat */
6900 size
= PERF_ATTR_SIZE_VER0
;
6902 if (size
< PERF_ATTR_SIZE_VER0
)
6906 * If we're handed a bigger struct than we know of,
6907 * ensure all the unknown bits are 0 - i.e. new
6908 * user-space does not rely on any kernel feature
6909 * extensions we dont know about yet.
6911 if (size
> sizeof(*attr
)) {
6912 unsigned char __user
*addr
;
6913 unsigned char __user
*end
;
6916 addr
= (void __user
*)uattr
+ sizeof(*attr
);
6917 end
= (void __user
*)uattr
+ size
;
6919 for (; addr
< end
; addr
++) {
6920 ret
= get_user(val
, addr
);
6926 size
= sizeof(*attr
);
6929 ret
= copy_from_user(attr
, uattr
, size
);
6933 if (attr
->__reserved_1
)
6936 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
6939 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
6942 if (attr
->sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
6943 u64 mask
= attr
->branch_sample_type
;
6945 /* only using defined bits */
6946 if (mask
& ~(PERF_SAMPLE_BRANCH_MAX
-1))
6949 /* at least one branch bit must be set */
6950 if (!(mask
& ~PERF_SAMPLE_BRANCH_PLM_ALL
))
6953 /* propagate priv level, when not set for branch */
6954 if (!(mask
& PERF_SAMPLE_BRANCH_PLM_ALL
)) {
6956 /* exclude_kernel checked on syscall entry */
6957 if (!attr
->exclude_kernel
)
6958 mask
|= PERF_SAMPLE_BRANCH_KERNEL
;
6960 if (!attr
->exclude_user
)
6961 mask
|= PERF_SAMPLE_BRANCH_USER
;
6963 if (!attr
->exclude_hv
)
6964 mask
|= PERF_SAMPLE_BRANCH_HV
;
6966 * adjust user setting (for HW filter setup)
6968 attr
->branch_sample_type
= mask
;
6970 /* privileged levels capture (kernel, hv): check permissions */
6971 if ((mask
& PERF_SAMPLE_BRANCH_PERM_PLM
)
6972 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
6976 if (attr
->sample_type
& PERF_SAMPLE_REGS_USER
) {
6977 ret
= perf_reg_validate(attr
->sample_regs_user
);
6982 if (attr
->sample_type
& PERF_SAMPLE_STACK_USER
) {
6983 if (!arch_perf_have_user_stack_dump())
6987 * We have __u32 type for the size, but so far
6988 * we can only use __u16 as maximum due to the
6989 * __u16 sample size limit.
6991 if (attr
->sample_stack_user
>= USHRT_MAX
)
6993 else if (!IS_ALIGNED(attr
->sample_stack_user
, sizeof(u64
)))
7001 put_user(sizeof(*attr
), &uattr
->size
);
7007 perf_event_set_output(struct perf_event
*event
, struct perf_event
*output_event
)
7009 struct ring_buffer
*rb
= NULL
;
7015 /* don't allow circular references */
7016 if (event
== output_event
)
7020 * Don't allow cross-cpu buffers
7022 if (output_event
->cpu
!= event
->cpu
)
7026 * If its not a per-cpu rb, it must be the same task.
7028 if (output_event
->cpu
== -1 && output_event
->ctx
!= event
->ctx
)
7032 mutex_lock(&event
->mmap_mutex
);
7033 /* Can't redirect output if we've got an active mmap() */
7034 if (atomic_read(&event
->mmap_count
))
7038 /* get the rb we want to redirect to */
7039 rb
= ring_buffer_get(output_event
);
7044 ring_buffer_attach(event
, rb
);
7048 mutex_unlock(&event
->mmap_mutex
);
7055 * sys_perf_event_open - open a performance event, associate it to a task/cpu
7057 * @attr_uptr: event_id type attributes for monitoring/sampling
7060 * @group_fd: group leader event fd
7062 SYSCALL_DEFINE5(perf_event_open
,
7063 struct perf_event_attr __user
*, attr_uptr
,
7064 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
7066 struct perf_event
*group_leader
= NULL
, *output_event
= NULL
;
7067 struct perf_event
*event
, *sibling
;
7068 struct perf_event_attr attr
;
7069 struct perf_event_context
*ctx
;
7070 struct file
*event_file
= NULL
;
7071 struct fd group
= {NULL
, 0};
7072 struct task_struct
*task
= NULL
;
7077 int f_flags
= O_RDWR
;
7079 /* for future expandability... */
7080 if (flags
& ~PERF_FLAG_ALL
)
7083 err
= perf_copy_attr(attr_uptr
, &attr
);
7087 if (!attr
.exclude_kernel
) {
7088 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
7093 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
7096 if (attr
.sample_period
& (1ULL << 63))
7101 * In cgroup mode, the pid argument is used to pass the fd
7102 * opened to the cgroup directory in cgroupfs. The cpu argument
7103 * designates the cpu on which to monitor threads from that
7106 if ((flags
& PERF_FLAG_PID_CGROUP
) && (pid
== -1 || cpu
== -1))
7109 if (flags
& PERF_FLAG_FD_CLOEXEC
)
7110 f_flags
|= O_CLOEXEC
;
7112 event_fd
= get_unused_fd_flags(f_flags
);
7116 if (group_fd
!= -1) {
7117 err
= perf_fget_light(group_fd
, &group
);
7120 group_leader
= group
.file
->private_data
;
7121 if (flags
& PERF_FLAG_FD_OUTPUT
)
7122 output_event
= group_leader
;
7123 if (flags
& PERF_FLAG_FD_NO_GROUP
)
7124 group_leader
= NULL
;
7127 if (pid
!= -1 && !(flags
& PERF_FLAG_PID_CGROUP
)) {
7128 task
= find_lively_task_by_vpid(pid
);
7130 err
= PTR_ERR(task
);
7135 if (task
&& group_leader
&&
7136 group_leader
->attr
.inherit
!= attr
.inherit
) {
7143 event
= perf_event_alloc(&attr
, cpu
, task
, group_leader
, NULL
,
7145 if (IS_ERR(event
)) {
7146 err
= PTR_ERR(event
);
7150 if (flags
& PERF_FLAG_PID_CGROUP
) {
7151 err
= perf_cgroup_connect(pid
, event
, &attr
, group_leader
);
7153 __free_event(event
);
7158 if (is_sampling_event(event
)) {
7159 if (event
->pmu
->capabilities
& PERF_PMU_CAP_NO_INTERRUPT
) {
7165 account_event(event
);
7168 * Special case software events and allow them to be part of
7169 * any hardware group.
7174 (is_software_event(event
) != is_software_event(group_leader
))) {
7175 if (is_software_event(event
)) {
7177 * If event and group_leader are not both a software
7178 * event, and event is, then group leader is not.
7180 * Allow the addition of software events to !software
7181 * groups, this is safe because software events never
7184 pmu
= group_leader
->pmu
;
7185 } else if (is_software_event(group_leader
) &&
7186 (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
)) {
7188 * In case the group is a pure software group, and we
7189 * try to add a hardware event, move the whole group to
7190 * the hardware context.
7197 * Get the target context (task or percpu):
7199 ctx
= find_get_context(pmu
, task
, event
->cpu
);
7206 put_task_struct(task
);
7211 * Look up the group leader (we will attach this event to it):
7217 * Do not allow a recursive hierarchy (this new sibling
7218 * becoming part of another group-sibling):
7220 if (group_leader
->group_leader
!= group_leader
)
7223 * Do not allow to attach to a group in a different
7224 * task or CPU context:
7227 if (group_leader
->ctx
->type
!= ctx
->type
)
7230 if (group_leader
->ctx
!= ctx
)
7235 * Only a group leader can be exclusive or pinned
7237 if (attr
.exclusive
|| attr
.pinned
)
7242 err
= perf_event_set_output(event
, output_event
);
7247 event_file
= anon_inode_getfile("[perf_event]", &perf_fops
, event
,
7249 if (IS_ERR(event_file
)) {
7250 err
= PTR_ERR(event_file
);
7255 struct perf_event_context
*gctx
= group_leader
->ctx
;
7257 mutex_lock(&gctx
->mutex
);
7258 perf_remove_from_context(group_leader
, false);
7261 * Removing from the context ends up with disabled
7262 * event. What we want here is event in the initial
7263 * startup state, ready to be add into new context.
7265 perf_event__state_init(group_leader
);
7266 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
7268 perf_remove_from_context(sibling
, false);
7269 perf_event__state_init(sibling
);
7272 mutex_unlock(&gctx
->mutex
);
7276 WARN_ON_ONCE(ctx
->parent_ctx
);
7277 mutex_lock(&ctx
->mutex
);
7281 perf_install_in_context(ctx
, group_leader
, event
->cpu
);
7283 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
7285 perf_install_in_context(ctx
, sibling
, event
->cpu
);
7290 perf_install_in_context(ctx
, event
, event
->cpu
);
7291 perf_unpin_context(ctx
);
7292 mutex_unlock(&ctx
->mutex
);
7296 event
->owner
= current
;
7298 mutex_lock(¤t
->perf_event_mutex
);
7299 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
7300 mutex_unlock(¤t
->perf_event_mutex
);
7303 * Precalculate sample_data sizes
7305 perf_event__header_size(event
);
7306 perf_event__id_header_size(event
);
7309 * Drop the reference on the group_event after placing the
7310 * new event on the sibling_list. This ensures destruction
7311 * of the group leader will find the pointer to itself in
7312 * perf_group_detach().
7315 fd_install(event_fd
, event_file
);
7319 perf_unpin_context(ctx
);
7327 put_task_struct(task
);
7331 put_unused_fd(event_fd
);
7336 * perf_event_create_kernel_counter
7338 * @attr: attributes of the counter to create
7339 * @cpu: cpu in which the counter is bound
7340 * @task: task to profile (NULL for percpu)
7343 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
7344 struct task_struct
*task
,
7345 perf_overflow_handler_t overflow_handler
,
7348 struct perf_event_context
*ctx
;
7349 struct perf_event
*event
;
7353 * Get the target context (task or percpu):
7356 event
= perf_event_alloc(attr
, cpu
, task
, NULL
, NULL
,
7357 overflow_handler
, context
);
7358 if (IS_ERR(event
)) {
7359 err
= PTR_ERR(event
);
7363 account_event(event
);
7365 ctx
= find_get_context(event
->pmu
, task
, cpu
);
7371 WARN_ON_ONCE(ctx
->parent_ctx
);
7372 mutex_lock(&ctx
->mutex
);
7373 perf_install_in_context(ctx
, event
, cpu
);
7374 perf_unpin_context(ctx
);
7375 mutex_unlock(&ctx
->mutex
);
7382 return ERR_PTR(err
);
7384 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
7386 void perf_pmu_migrate_context(struct pmu
*pmu
, int src_cpu
, int dst_cpu
)
7388 struct perf_event_context
*src_ctx
;
7389 struct perf_event_context
*dst_ctx
;
7390 struct perf_event
*event
, *tmp
;
7393 src_ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, src_cpu
)->ctx
;
7394 dst_ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, dst_cpu
)->ctx
;
7396 mutex_lock(&src_ctx
->mutex
);
7397 list_for_each_entry_safe(event
, tmp
, &src_ctx
->event_list
,
7399 perf_remove_from_context(event
, false);
7400 unaccount_event_cpu(event
, src_cpu
);
7402 list_add(&event
->migrate_entry
, &events
);
7404 mutex_unlock(&src_ctx
->mutex
);
7408 mutex_lock(&dst_ctx
->mutex
);
7409 list_for_each_entry_safe(event
, tmp
, &events
, migrate_entry
) {
7410 list_del(&event
->migrate_entry
);
7411 if (event
->state
>= PERF_EVENT_STATE_OFF
)
7412 event
->state
= PERF_EVENT_STATE_INACTIVE
;
7413 account_event_cpu(event
, dst_cpu
);
7414 perf_install_in_context(dst_ctx
, event
, dst_cpu
);
7417 mutex_unlock(&dst_ctx
->mutex
);
7419 EXPORT_SYMBOL_GPL(perf_pmu_migrate_context
);
7421 static void sync_child_event(struct perf_event
*child_event
,
7422 struct task_struct
*child
)
7424 struct perf_event
*parent_event
= child_event
->parent
;
7427 if (child_event
->attr
.inherit_stat
)
7428 perf_event_read_event(child_event
, child
);
7430 child_val
= perf_event_count(child_event
);
7433 * Add back the child's count to the parent's count:
7435 atomic64_add(child_val
, &parent_event
->child_count
);
7436 atomic64_add(child_event
->total_time_enabled
,
7437 &parent_event
->child_total_time_enabled
);
7438 atomic64_add(child_event
->total_time_running
,
7439 &parent_event
->child_total_time_running
);
7442 * Remove this event from the parent's list
7444 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
7445 mutex_lock(&parent_event
->child_mutex
);
7446 list_del_init(&child_event
->child_list
);
7447 mutex_unlock(&parent_event
->child_mutex
);
7450 * Release the parent event, if this was the last
7453 put_event(parent_event
);
7457 __perf_event_exit_task(struct perf_event
*child_event
,
7458 struct perf_event_context
*child_ctx
,
7459 struct task_struct
*child
)
7461 perf_remove_from_context(child_event
, true);
7464 * It can happen that the parent exits first, and has events
7465 * that are still around due to the child reference. These
7466 * events need to be zapped.
7468 if (child_event
->parent
) {
7469 sync_child_event(child_event
, child
);
7470 free_event(child_event
);
7474 static void perf_event_exit_task_context(struct task_struct
*child
, int ctxn
)
7476 struct perf_event
*child_event
, *next
;
7477 struct perf_event_context
*child_ctx
;
7478 unsigned long flags
;
7480 if (likely(!child
->perf_event_ctxp
[ctxn
])) {
7481 perf_event_task(child
, NULL
, 0);
7485 local_irq_save(flags
);
7487 * We can't reschedule here because interrupts are disabled,
7488 * and either child is current or it is a task that can't be
7489 * scheduled, so we are now safe from rescheduling changing
7492 child_ctx
= rcu_dereference_raw(child
->perf_event_ctxp
[ctxn
]);
7495 * Take the context lock here so that if find_get_context is
7496 * reading child->perf_event_ctxp, we wait until it has
7497 * incremented the context's refcount before we do put_ctx below.
7499 raw_spin_lock(&child_ctx
->lock
);
7500 task_ctx_sched_out(child_ctx
);
7501 child
->perf_event_ctxp
[ctxn
] = NULL
;
7503 * If this context is a clone; unclone it so it can't get
7504 * swapped to another process while we're removing all
7505 * the events from it.
7507 unclone_ctx(child_ctx
);
7508 update_context_time(child_ctx
);
7509 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
7512 * Report the task dead after unscheduling the events so that we
7513 * won't get any samples after PERF_RECORD_EXIT. We can however still
7514 * get a few PERF_RECORD_READ events.
7516 perf_event_task(child
, child_ctx
, 0);
7519 * We can recurse on the same lock type through:
7521 * __perf_event_exit_task()
7522 * sync_child_event()
7524 * mutex_lock(&ctx->mutex)
7526 * But since its the parent context it won't be the same instance.
7528 mutex_lock(&child_ctx
->mutex
);
7530 list_for_each_entry_safe(child_event
, next
, &child_ctx
->event_list
, event_entry
)
7531 __perf_event_exit_task(child_event
, child_ctx
, child
);
7533 mutex_unlock(&child_ctx
->mutex
);
7539 * When a child task exits, feed back event values to parent events.
7541 void perf_event_exit_task(struct task_struct
*child
)
7543 struct perf_event
*event
, *tmp
;
7546 mutex_lock(&child
->perf_event_mutex
);
7547 list_for_each_entry_safe(event
, tmp
, &child
->perf_event_list
,
7549 list_del_init(&event
->owner_entry
);
7552 * Ensure the list deletion is visible before we clear
7553 * the owner, closes a race against perf_release() where
7554 * we need to serialize on the owner->perf_event_mutex.
7557 event
->owner
= NULL
;
7559 mutex_unlock(&child
->perf_event_mutex
);
7561 for_each_task_context_nr(ctxn
)
7562 perf_event_exit_task_context(child
, ctxn
);
7565 static void perf_free_event(struct perf_event
*event
,
7566 struct perf_event_context
*ctx
)
7568 struct perf_event
*parent
= event
->parent
;
7570 if (WARN_ON_ONCE(!parent
))
7573 mutex_lock(&parent
->child_mutex
);
7574 list_del_init(&event
->child_list
);
7575 mutex_unlock(&parent
->child_mutex
);
7579 perf_group_detach(event
);
7580 list_del_event(event
, ctx
);
7585 * free an unexposed, unused context as created by inheritance by
7586 * perf_event_init_task below, used by fork() in case of fail.
7588 void perf_event_free_task(struct task_struct
*task
)
7590 struct perf_event_context
*ctx
;
7591 struct perf_event
*event
, *tmp
;
7594 for_each_task_context_nr(ctxn
) {
7595 ctx
= task
->perf_event_ctxp
[ctxn
];
7599 mutex_lock(&ctx
->mutex
);
7601 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
,
7603 perf_free_event(event
, ctx
);
7605 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
,
7607 perf_free_event(event
, ctx
);
7609 if (!list_empty(&ctx
->pinned_groups
) ||
7610 !list_empty(&ctx
->flexible_groups
))
7613 mutex_unlock(&ctx
->mutex
);
7619 void perf_event_delayed_put(struct task_struct
*task
)
7623 for_each_task_context_nr(ctxn
)
7624 WARN_ON_ONCE(task
->perf_event_ctxp
[ctxn
]);
7628 * inherit a event from parent task to child task:
7630 static struct perf_event
*
7631 inherit_event(struct perf_event
*parent_event
,
7632 struct task_struct
*parent
,
7633 struct perf_event_context
*parent_ctx
,
7634 struct task_struct
*child
,
7635 struct perf_event
*group_leader
,
7636 struct perf_event_context
*child_ctx
)
7638 struct perf_event
*child_event
;
7639 unsigned long flags
;
7642 * Instead of creating recursive hierarchies of events,
7643 * we link inherited events back to the original parent,
7644 * which has a filp for sure, which we use as the reference
7647 if (parent_event
->parent
)
7648 parent_event
= parent_event
->parent
;
7650 child_event
= perf_event_alloc(&parent_event
->attr
,
7653 group_leader
, parent_event
,
7655 if (IS_ERR(child_event
))
7658 if (!atomic_long_inc_not_zero(&parent_event
->refcount
)) {
7659 free_event(child_event
);
7666 * Make the child state follow the state of the parent event,
7667 * not its attr.disabled bit. We hold the parent's mutex,
7668 * so we won't race with perf_event_{en, dis}able_family.
7670 if (parent_event
->state
>= PERF_EVENT_STATE_INACTIVE
)
7671 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
7673 child_event
->state
= PERF_EVENT_STATE_OFF
;
7675 if (parent_event
->attr
.freq
) {
7676 u64 sample_period
= parent_event
->hw
.sample_period
;
7677 struct hw_perf_event
*hwc
= &child_event
->hw
;
7679 hwc
->sample_period
= sample_period
;
7680 hwc
->last_period
= sample_period
;
7682 local64_set(&hwc
->period_left
, sample_period
);
7685 child_event
->ctx
= child_ctx
;
7686 child_event
->overflow_handler
= parent_event
->overflow_handler
;
7687 child_event
->overflow_handler_context
7688 = parent_event
->overflow_handler_context
;
7691 * Precalculate sample_data sizes
7693 perf_event__header_size(child_event
);
7694 perf_event__id_header_size(child_event
);
7697 * Link it up in the child's context:
7699 raw_spin_lock_irqsave(&child_ctx
->lock
, flags
);
7700 add_event_to_ctx(child_event
, child_ctx
);
7701 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
7704 * Link this into the parent event's child list
7706 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
7707 mutex_lock(&parent_event
->child_mutex
);
7708 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
7709 mutex_unlock(&parent_event
->child_mutex
);
7714 static int inherit_group(struct perf_event
*parent_event
,
7715 struct task_struct
*parent
,
7716 struct perf_event_context
*parent_ctx
,
7717 struct task_struct
*child
,
7718 struct perf_event_context
*child_ctx
)
7720 struct perf_event
*leader
;
7721 struct perf_event
*sub
;
7722 struct perf_event
*child_ctr
;
7724 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
7725 child
, NULL
, child_ctx
);
7727 return PTR_ERR(leader
);
7728 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
7729 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
7730 child
, leader
, child_ctx
);
7731 if (IS_ERR(child_ctr
))
7732 return PTR_ERR(child_ctr
);
7738 inherit_task_group(struct perf_event
*event
, struct task_struct
*parent
,
7739 struct perf_event_context
*parent_ctx
,
7740 struct task_struct
*child
, int ctxn
,
7744 struct perf_event_context
*child_ctx
;
7746 if (!event
->attr
.inherit
) {
7751 child_ctx
= child
->perf_event_ctxp
[ctxn
];
7754 * This is executed from the parent task context, so
7755 * inherit events that have been marked for cloning.
7756 * First allocate and initialize a context for the
7760 child_ctx
= alloc_perf_context(parent_ctx
->pmu
, child
);
7764 child
->perf_event_ctxp
[ctxn
] = child_ctx
;
7767 ret
= inherit_group(event
, parent
, parent_ctx
,
7777 * Initialize the perf_event context in task_struct
7779 int perf_event_init_context(struct task_struct
*child
, int ctxn
)
7781 struct perf_event_context
*child_ctx
, *parent_ctx
;
7782 struct perf_event_context
*cloned_ctx
;
7783 struct perf_event
*event
;
7784 struct task_struct
*parent
= current
;
7785 int inherited_all
= 1;
7786 unsigned long flags
;
7789 if (likely(!parent
->perf_event_ctxp
[ctxn
]))
7793 * If the parent's context is a clone, pin it so it won't get
7796 parent_ctx
= perf_pin_task_context(parent
, ctxn
);
7801 * No need to check if parent_ctx != NULL here; since we saw
7802 * it non-NULL earlier, the only reason for it to become NULL
7803 * is if we exit, and since we're currently in the middle of
7804 * a fork we can't be exiting at the same time.
7808 * Lock the parent list. No need to lock the child - not PID
7809 * hashed yet and not running, so nobody can access it.
7811 mutex_lock(&parent_ctx
->mutex
);
7814 * We dont have to disable NMIs - we are only looking at
7815 * the list, not manipulating it:
7817 list_for_each_entry(event
, &parent_ctx
->pinned_groups
, group_entry
) {
7818 ret
= inherit_task_group(event
, parent
, parent_ctx
,
7819 child
, ctxn
, &inherited_all
);
7825 * We can't hold ctx->lock when iterating the ->flexible_group list due
7826 * to allocations, but we need to prevent rotation because
7827 * rotate_ctx() will change the list from interrupt context.
7829 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
7830 parent_ctx
->rotate_disable
= 1;
7831 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
7833 list_for_each_entry(event
, &parent_ctx
->flexible_groups
, group_entry
) {
7834 ret
= inherit_task_group(event
, parent
, parent_ctx
,
7835 child
, ctxn
, &inherited_all
);
7840 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
7841 parent_ctx
->rotate_disable
= 0;
7843 child_ctx
= child
->perf_event_ctxp
[ctxn
];
7845 if (child_ctx
&& inherited_all
) {
7847 * Mark the child context as a clone of the parent
7848 * context, or of whatever the parent is a clone of.
7850 * Note that if the parent is a clone, the holding of
7851 * parent_ctx->lock avoids it from being uncloned.
7853 cloned_ctx
= parent_ctx
->parent_ctx
;
7855 child_ctx
->parent_ctx
= cloned_ctx
;
7856 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
7858 child_ctx
->parent_ctx
= parent_ctx
;
7859 child_ctx
->parent_gen
= parent_ctx
->generation
;
7861 get_ctx(child_ctx
->parent_ctx
);
7864 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
7865 mutex_unlock(&parent_ctx
->mutex
);
7867 perf_unpin_context(parent_ctx
);
7868 put_ctx(parent_ctx
);
7874 * Initialize the perf_event context in task_struct
7876 int perf_event_init_task(struct task_struct
*child
)
7880 memset(child
->perf_event_ctxp
, 0, sizeof(child
->perf_event_ctxp
));
7881 mutex_init(&child
->perf_event_mutex
);
7882 INIT_LIST_HEAD(&child
->perf_event_list
);
7884 for_each_task_context_nr(ctxn
) {
7885 ret
= perf_event_init_context(child
, ctxn
);
7893 static void __init
perf_event_init_all_cpus(void)
7895 struct swevent_htable
*swhash
;
7898 for_each_possible_cpu(cpu
) {
7899 swhash
= &per_cpu(swevent_htable
, cpu
);
7900 mutex_init(&swhash
->hlist_mutex
);
7901 INIT_LIST_HEAD(&per_cpu(rotation_list
, cpu
));
7905 static void perf_event_init_cpu(int cpu
)
7907 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
7909 mutex_lock(&swhash
->hlist_mutex
);
7910 swhash
->online
= true;
7911 if (swhash
->hlist_refcount
> 0) {
7912 struct swevent_hlist
*hlist
;
7914 hlist
= kzalloc_node(sizeof(*hlist
), GFP_KERNEL
, cpu_to_node(cpu
));
7916 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
7918 mutex_unlock(&swhash
->hlist_mutex
);
7921 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7922 static void perf_pmu_rotate_stop(struct pmu
*pmu
)
7924 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
7926 WARN_ON(!irqs_disabled());
7928 list_del_init(&cpuctx
->rotation_list
);
7931 static void __perf_event_exit_context(void *__info
)
7933 struct remove_event re
= { .detach_group
= false };
7934 struct perf_event_context
*ctx
= __info
;
7936 perf_pmu_rotate_stop(ctx
->pmu
);
7939 list_for_each_entry_rcu(re
.event
, &ctx
->event_list
, event_entry
)
7940 __perf_remove_from_context(&re
);
7944 static void perf_event_exit_cpu_context(int cpu
)
7946 struct perf_event_context
*ctx
;
7950 idx
= srcu_read_lock(&pmus_srcu
);
7951 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
7952 ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
)->ctx
;
7954 mutex_lock(&ctx
->mutex
);
7955 smp_call_function_single(cpu
, __perf_event_exit_context
, ctx
, 1);
7956 mutex_unlock(&ctx
->mutex
);
7958 srcu_read_unlock(&pmus_srcu
, idx
);
7961 static void perf_event_exit_cpu(int cpu
)
7963 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
7965 perf_event_exit_cpu_context(cpu
);
7967 mutex_lock(&swhash
->hlist_mutex
);
7968 swhash
->online
= false;
7969 swevent_hlist_release(swhash
);
7970 mutex_unlock(&swhash
->hlist_mutex
);
7973 static inline void perf_event_exit_cpu(int cpu
) { }
7977 perf_reboot(struct notifier_block
*notifier
, unsigned long val
, void *v
)
7981 for_each_online_cpu(cpu
)
7982 perf_event_exit_cpu(cpu
);
7988 * Run the perf reboot notifier at the very last possible moment so that
7989 * the generic watchdog code runs as long as possible.
7991 static struct notifier_block perf_reboot_notifier
= {
7992 .notifier_call
= perf_reboot
,
7993 .priority
= INT_MIN
,
7997 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
7999 unsigned int cpu
= (long)hcpu
;
8001 switch (action
& ~CPU_TASKS_FROZEN
) {
8003 case CPU_UP_PREPARE
:
8004 case CPU_DOWN_FAILED
:
8005 perf_event_init_cpu(cpu
);
8008 case CPU_UP_CANCELED
:
8009 case CPU_DOWN_PREPARE
:
8010 perf_event_exit_cpu(cpu
);
8019 void __init
perf_event_init(void)
8025 perf_event_init_all_cpus();
8026 init_srcu_struct(&pmus_srcu
);
8027 perf_pmu_register(&perf_swevent
, "software", PERF_TYPE_SOFTWARE
);
8028 perf_pmu_register(&perf_cpu_clock
, NULL
, -1);
8029 perf_pmu_register(&perf_task_clock
, NULL
, -1);
8031 perf_cpu_notifier(perf_cpu_notify
);
8032 register_reboot_notifier(&perf_reboot_notifier
);
8034 ret
= init_hw_breakpoint();
8035 WARN(ret
, "hw_breakpoint initialization failed with: %d", ret
);
8037 /* do not patch jump label more than once per second */
8038 jump_label_rate_limit(&perf_sched_events
, HZ
);
8041 * Build time assertion that we keep the data_head at the intended
8042 * location. IOW, validation we got the __reserved[] size right.
8044 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page
, data_head
))
8048 static int __init
perf_event_sysfs_init(void)
8053 mutex_lock(&pmus_lock
);
8055 ret
= bus_register(&pmu_bus
);
8059 list_for_each_entry(pmu
, &pmus
, entry
) {
8060 if (!pmu
->name
|| pmu
->type
< 0)
8063 ret
= pmu_dev_alloc(pmu
);
8064 WARN(ret
, "Failed to register pmu: %s, reason %d\n", pmu
->name
, ret
);
8066 pmu_bus_running
= 1;
8070 mutex_unlock(&pmus_lock
);
8074 device_initcall(perf_event_sysfs_init
);
8076 #ifdef CONFIG_CGROUP_PERF
8077 static struct cgroup_subsys_state
*
8078 perf_cgroup_css_alloc(struct cgroup_subsys_state
*parent_css
)
8080 struct perf_cgroup
*jc
;
8082 jc
= kzalloc(sizeof(*jc
), GFP_KERNEL
);
8084 return ERR_PTR(-ENOMEM
);
8086 jc
->info
= alloc_percpu(struct perf_cgroup_info
);
8089 return ERR_PTR(-ENOMEM
);
8095 static void perf_cgroup_css_free(struct cgroup_subsys_state
*css
)
8097 struct perf_cgroup
*jc
= container_of(css
, struct perf_cgroup
, css
);
8099 free_percpu(jc
->info
);
8103 static int __perf_cgroup_move(void *info
)
8105 struct task_struct
*task
= info
;
8106 perf_cgroup_switch(task
, PERF_CGROUP_SWOUT
| PERF_CGROUP_SWIN
);
8110 static void perf_cgroup_attach(struct cgroup_subsys_state
*css
,
8111 struct cgroup_taskset
*tset
)
8113 struct task_struct
*task
;
8115 cgroup_taskset_for_each(task
, tset
)
8116 task_function_call(task
, __perf_cgroup_move
, task
);
8119 static void perf_cgroup_exit(struct cgroup_subsys_state
*css
,
8120 struct cgroup_subsys_state
*old_css
,
8121 struct task_struct
*task
)
8124 * cgroup_exit() is called in the copy_process() failure path.
8125 * Ignore this case since the task hasn't ran yet, this avoids
8126 * trying to poke a half freed task state from generic code.
8128 if (!(task
->flags
& PF_EXITING
))
8131 task_function_call(task
, __perf_cgroup_move
, task
);
8134 struct cgroup_subsys perf_event_cgrp_subsys
= {
8135 .css_alloc
= perf_cgroup_css_alloc
,
8136 .css_free
= perf_cgroup_css_free
,
8137 .exit
= perf_cgroup_exit
,
8138 .attach
= perf_cgroup_attach
,
8140 #endif /* CONFIG_CGROUP_PERF */