2 * Performance events core code:
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/idr.h>
17 #include <linux/file.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/hash.h>
21 #include <linux/tick.h>
22 #include <linux/sysfs.h>
23 #include <linux/dcache.h>
24 #include <linux/percpu.h>
25 #include <linux/ptrace.h>
26 #include <linux/reboot.h>
27 #include <linux/vmstat.h>
28 #include <linux/device.h>
29 #include <linux/export.h>
30 #include <linux/vmalloc.h>
31 #include <linux/hardirq.h>
32 #include <linux/rculist.h>
33 #include <linux/uaccess.h>
34 #include <linux/syscalls.h>
35 #include <linux/anon_inodes.h>
36 #include <linux/kernel_stat.h>
37 #include <linux/perf_event.h>
38 #include <linux/ftrace_event.h>
39 #include <linux/hw_breakpoint.h>
40 #include <linux/mm_types.h>
41 #include <linux/cgroup.h>
45 #include <asm/irq_regs.h>
47 struct remote_function_call
{
48 struct task_struct
*p
;
49 int (*func
)(void *info
);
54 static void remote_function(void *data
)
56 struct remote_function_call
*tfc
= data
;
57 struct task_struct
*p
= tfc
->p
;
61 if (task_cpu(p
) != smp_processor_id() || !task_curr(p
))
65 tfc
->ret
= tfc
->func(tfc
->info
);
69 * task_function_call - call a function on the cpu on which a task runs
70 * @p: the task to evaluate
71 * @func: the function to be called
72 * @info: the function call argument
74 * Calls the function @func when the task is currently running. This might
75 * be on the current CPU, which just calls the function directly
77 * returns: @func return value, or
78 * -ESRCH - when the process isn't running
79 * -EAGAIN - when the process moved away
82 task_function_call(struct task_struct
*p
, int (*func
) (void *info
), void *info
)
84 struct remote_function_call data
= {
88 .ret
= -ESRCH
, /* No such (running) process */
92 smp_call_function_single(task_cpu(p
), remote_function
, &data
, 1);
98 * cpu_function_call - call a function on the cpu
99 * @func: the function to be called
100 * @info: the function call argument
102 * Calls the function @func on the remote cpu.
104 * returns: @func return value or -ENXIO when the cpu is offline
106 static int cpu_function_call(int cpu
, int (*func
) (void *info
), void *info
)
108 struct remote_function_call data
= {
112 .ret
= -ENXIO
, /* No such CPU */
115 smp_call_function_single(cpu
, remote_function
, &data
, 1);
120 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
121 PERF_FLAG_FD_OUTPUT |\
122 PERF_FLAG_PID_CGROUP |\
123 PERF_FLAG_FD_CLOEXEC)
126 * branch priv levels that need permission checks
128 #define PERF_SAMPLE_BRANCH_PERM_PLM \
129 (PERF_SAMPLE_BRANCH_KERNEL |\
130 PERF_SAMPLE_BRANCH_HV)
133 EVENT_FLEXIBLE
= 0x1,
135 EVENT_ALL
= EVENT_FLEXIBLE
| EVENT_PINNED
,
139 * perf_sched_events : >0 events exist
140 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
142 struct static_key_deferred perf_sched_events __read_mostly
;
143 static DEFINE_PER_CPU(atomic_t
, perf_cgroup_events
);
144 static DEFINE_PER_CPU(atomic_t
, perf_branch_stack_events
);
146 static atomic_t nr_mmap_events __read_mostly
;
147 static atomic_t nr_comm_events __read_mostly
;
148 static atomic_t nr_task_events __read_mostly
;
149 static atomic_t nr_freq_events __read_mostly
;
151 static LIST_HEAD(pmus
);
152 static DEFINE_MUTEX(pmus_lock
);
153 static struct srcu_struct pmus_srcu
;
156 * perf event paranoia level:
157 * -1 - not paranoid at all
158 * 0 - disallow raw tracepoint access for unpriv
159 * 1 - disallow cpu events for unpriv
160 * 2 - disallow kernel profiling for unpriv
162 int sysctl_perf_event_paranoid __read_mostly
= 1;
164 /* Minimum for 512 kiB + 1 user control page */
165 int sysctl_perf_event_mlock __read_mostly
= 512 + (PAGE_SIZE
/ 1024); /* 'free' kiB per user */
168 * max perf event sample rate
170 #define DEFAULT_MAX_SAMPLE_RATE 100000
171 #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
172 #define DEFAULT_CPU_TIME_MAX_PERCENT 25
174 int sysctl_perf_event_sample_rate __read_mostly
= DEFAULT_MAX_SAMPLE_RATE
;
176 static int max_samples_per_tick __read_mostly
= DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE
, HZ
);
177 static int perf_sample_period_ns __read_mostly
= DEFAULT_SAMPLE_PERIOD_NS
;
179 static int perf_sample_allowed_ns __read_mostly
=
180 DEFAULT_SAMPLE_PERIOD_NS
* DEFAULT_CPU_TIME_MAX_PERCENT
/ 100;
182 void update_perf_cpu_limits(void)
184 u64 tmp
= perf_sample_period_ns
;
186 tmp
*= sysctl_perf_cpu_time_max_percent
;
188 ACCESS_ONCE(perf_sample_allowed_ns
) = tmp
;
191 static int perf_rotate_context(struct perf_cpu_context
*cpuctx
);
193 int perf_proc_update_handler(struct ctl_table
*table
, int write
,
194 void __user
*buffer
, size_t *lenp
,
197 int ret
= proc_dointvec_minmax(table
, write
, buffer
, lenp
, ppos
);
202 max_samples_per_tick
= DIV_ROUND_UP(sysctl_perf_event_sample_rate
, HZ
);
203 perf_sample_period_ns
= NSEC_PER_SEC
/ sysctl_perf_event_sample_rate
;
204 update_perf_cpu_limits();
209 int sysctl_perf_cpu_time_max_percent __read_mostly
= DEFAULT_CPU_TIME_MAX_PERCENT
;
211 int perf_cpu_time_max_percent_handler(struct ctl_table
*table
, int write
,
212 void __user
*buffer
, size_t *lenp
,
215 int ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
220 update_perf_cpu_limits();
226 * perf samples are done in some very critical code paths (NMIs).
227 * If they take too much CPU time, the system can lock up and not
228 * get any real work done. This will drop the sample rate when
229 * we detect that events are taking too long.
231 #define NR_ACCUMULATED_SAMPLES 128
232 static DEFINE_PER_CPU(u64
, running_sample_length
);
234 void perf_sample_event_took(u64 sample_len_ns
)
236 u64 avg_local_sample_len
;
237 u64 local_samples_len
;
238 u64 allowed_ns
= ACCESS_ONCE(perf_sample_allowed_ns
);
243 /* decay the counter by 1 average sample */
244 local_samples_len
= __get_cpu_var(running_sample_length
);
245 local_samples_len
-= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
246 local_samples_len
+= sample_len_ns
;
247 __get_cpu_var(running_sample_length
) = local_samples_len
;
250 * note: this will be biased artifically low until we have
251 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
252 * from having to maintain a count.
254 avg_local_sample_len
= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
256 if (avg_local_sample_len
<= allowed_ns
)
259 if (max_samples_per_tick
<= 1)
262 max_samples_per_tick
= DIV_ROUND_UP(max_samples_per_tick
, 2);
263 sysctl_perf_event_sample_rate
= max_samples_per_tick
* HZ
;
264 perf_sample_period_ns
= NSEC_PER_SEC
/ sysctl_perf_event_sample_rate
;
266 printk_ratelimited(KERN_WARNING
267 "perf samples too long (%lld > %lld), lowering "
268 "kernel.perf_event_max_sample_rate to %d\n",
269 avg_local_sample_len
, allowed_ns
,
270 sysctl_perf_event_sample_rate
);
272 update_perf_cpu_limits();
275 static atomic64_t perf_event_id
;
277 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
278 enum event_type_t event_type
);
280 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
281 enum event_type_t event_type
,
282 struct task_struct
*task
);
284 static void update_context_time(struct perf_event_context
*ctx
);
285 static u64
perf_event_time(struct perf_event
*event
);
287 void __weak
perf_event_print_debug(void) { }
289 extern __weak
const char *perf_pmu_name(void)
294 static inline u64
perf_clock(void)
296 return local_clock();
299 static inline struct perf_cpu_context
*
300 __get_cpu_context(struct perf_event_context
*ctx
)
302 return this_cpu_ptr(ctx
->pmu
->pmu_cpu_context
);
305 static void perf_ctx_lock(struct perf_cpu_context
*cpuctx
,
306 struct perf_event_context
*ctx
)
308 raw_spin_lock(&cpuctx
->ctx
.lock
);
310 raw_spin_lock(&ctx
->lock
);
313 static void perf_ctx_unlock(struct perf_cpu_context
*cpuctx
,
314 struct perf_event_context
*ctx
)
317 raw_spin_unlock(&ctx
->lock
);
318 raw_spin_unlock(&cpuctx
->ctx
.lock
);
321 #ifdef CONFIG_CGROUP_PERF
324 * perf_cgroup_info keeps track of time_enabled for a cgroup.
325 * This is a per-cpu dynamically allocated data structure.
327 struct perf_cgroup_info
{
333 struct cgroup_subsys_state css
;
334 struct perf_cgroup_info __percpu
*info
;
338 * Must ensure cgroup is pinned (css_get) before calling
339 * this function. In other words, we cannot call this function
340 * if there is no cgroup event for the current CPU context.
342 static inline struct perf_cgroup
*
343 perf_cgroup_from_task(struct task_struct
*task
)
345 return container_of(task_css(task
, perf_event_cgrp_id
),
346 struct perf_cgroup
, css
);
350 perf_cgroup_match(struct perf_event
*event
)
352 struct perf_event_context
*ctx
= event
->ctx
;
353 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
355 /* @event doesn't care about cgroup */
359 /* wants specific cgroup scope but @cpuctx isn't associated with any */
364 * Cgroup scoping is recursive. An event enabled for a cgroup is
365 * also enabled for all its descendant cgroups. If @cpuctx's
366 * cgroup is a descendant of @event's (the test covers identity
367 * case), it's a match.
369 return cgroup_is_descendant(cpuctx
->cgrp
->css
.cgroup
,
370 event
->cgrp
->css
.cgroup
);
373 static inline void perf_put_cgroup(struct perf_event
*event
)
375 css_put(&event
->cgrp
->css
);
378 static inline void perf_detach_cgroup(struct perf_event
*event
)
380 perf_put_cgroup(event
);
384 static inline int is_cgroup_event(struct perf_event
*event
)
386 return event
->cgrp
!= NULL
;
389 static inline u64
perf_cgroup_event_time(struct perf_event
*event
)
391 struct perf_cgroup_info
*t
;
393 t
= per_cpu_ptr(event
->cgrp
->info
, event
->cpu
);
397 static inline void __update_cgrp_time(struct perf_cgroup
*cgrp
)
399 struct perf_cgroup_info
*info
;
404 info
= this_cpu_ptr(cgrp
->info
);
406 info
->time
+= now
- info
->timestamp
;
407 info
->timestamp
= now
;
410 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context
*cpuctx
)
412 struct perf_cgroup
*cgrp_out
= cpuctx
->cgrp
;
414 __update_cgrp_time(cgrp_out
);
417 static inline void update_cgrp_time_from_event(struct perf_event
*event
)
419 struct perf_cgroup
*cgrp
;
422 * ensure we access cgroup data only when needed and
423 * when we know the cgroup is pinned (css_get)
425 if (!is_cgroup_event(event
))
428 cgrp
= perf_cgroup_from_task(current
);
430 * Do not update time when cgroup is not active
432 if (cgrp
== event
->cgrp
)
433 __update_cgrp_time(event
->cgrp
);
437 perf_cgroup_set_timestamp(struct task_struct
*task
,
438 struct perf_event_context
*ctx
)
440 struct perf_cgroup
*cgrp
;
441 struct perf_cgroup_info
*info
;
444 * ctx->lock held by caller
445 * ensure we do not access cgroup data
446 * unless we have the cgroup pinned (css_get)
448 if (!task
|| !ctx
->nr_cgroups
)
451 cgrp
= perf_cgroup_from_task(task
);
452 info
= this_cpu_ptr(cgrp
->info
);
453 info
->timestamp
= ctx
->timestamp
;
456 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
457 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
460 * reschedule events based on the cgroup constraint of task.
462 * mode SWOUT : schedule out everything
463 * mode SWIN : schedule in based on cgroup for next
465 void perf_cgroup_switch(struct task_struct
*task
, int mode
)
467 struct perf_cpu_context
*cpuctx
;
472 * disable interrupts to avoid geting nr_cgroup
473 * changes via __perf_event_disable(). Also
476 local_irq_save(flags
);
479 * we reschedule only in the presence of cgroup
480 * constrained events.
484 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
485 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
486 if (cpuctx
->unique_pmu
!= pmu
)
487 continue; /* ensure we process each cpuctx once */
490 * perf_cgroup_events says at least one
491 * context on this CPU has cgroup events.
493 * ctx->nr_cgroups reports the number of cgroup
494 * events for a context.
496 if (cpuctx
->ctx
.nr_cgroups
> 0) {
497 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
498 perf_pmu_disable(cpuctx
->ctx
.pmu
);
500 if (mode
& PERF_CGROUP_SWOUT
) {
501 cpu_ctx_sched_out(cpuctx
, EVENT_ALL
);
503 * must not be done before ctxswout due
504 * to event_filter_match() in event_sched_out()
509 if (mode
& PERF_CGROUP_SWIN
) {
510 WARN_ON_ONCE(cpuctx
->cgrp
);
512 * set cgrp before ctxsw in to allow
513 * event_filter_match() to not have to pass
516 cpuctx
->cgrp
= perf_cgroup_from_task(task
);
517 cpu_ctx_sched_in(cpuctx
, EVENT_ALL
, task
);
519 perf_pmu_enable(cpuctx
->ctx
.pmu
);
520 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
526 local_irq_restore(flags
);
529 static inline void perf_cgroup_sched_out(struct task_struct
*task
,
530 struct task_struct
*next
)
532 struct perf_cgroup
*cgrp1
;
533 struct perf_cgroup
*cgrp2
= NULL
;
536 * we come here when we know perf_cgroup_events > 0
538 cgrp1
= perf_cgroup_from_task(task
);
541 * next is NULL when called from perf_event_enable_on_exec()
542 * that will systematically cause a cgroup_switch()
545 cgrp2
= perf_cgroup_from_task(next
);
548 * only schedule out current cgroup events if we know
549 * that we are switching to a different cgroup. Otherwise,
550 * do no touch the cgroup events.
553 perf_cgroup_switch(task
, PERF_CGROUP_SWOUT
);
556 static inline void perf_cgroup_sched_in(struct task_struct
*prev
,
557 struct task_struct
*task
)
559 struct perf_cgroup
*cgrp1
;
560 struct perf_cgroup
*cgrp2
= NULL
;
563 * we come here when we know perf_cgroup_events > 0
565 cgrp1
= perf_cgroup_from_task(task
);
567 /* prev can never be NULL */
568 cgrp2
= perf_cgroup_from_task(prev
);
571 * only need to schedule in cgroup events if we are changing
572 * cgroup during ctxsw. Cgroup events were not scheduled
573 * out of ctxsw out if that was not the case.
576 perf_cgroup_switch(task
, PERF_CGROUP_SWIN
);
579 static inline int perf_cgroup_connect(int fd
, struct perf_event
*event
,
580 struct perf_event_attr
*attr
,
581 struct perf_event
*group_leader
)
583 struct perf_cgroup
*cgrp
;
584 struct cgroup_subsys_state
*css
;
585 struct fd f
= fdget(fd
);
591 css
= css_tryget_from_dir(f
.file
->f_dentry
, &perf_event_cgrp_subsys
);
597 cgrp
= container_of(css
, struct perf_cgroup
, css
);
601 * all events in a group must monitor
602 * the same cgroup because a task belongs
603 * to only one perf cgroup at a time
605 if (group_leader
&& group_leader
->cgrp
!= cgrp
) {
606 perf_detach_cgroup(event
);
615 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
617 struct perf_cgroup_info
*t
;
618 t
= per_cpu_ptr(event
->cgrp
->info
, event
->cpu
);
619 event
->shadow_ctx_time
= now
- t
->timestamp
;
623 perf_cgroup_defer_enabled(struct perf_event
*event
)
626 * when the current task's perf cgroup does not match
627 * the event's, we need to remember to call the
628 * perf_mark_enable() function the first time a task with
629 * a matching perf cgroup is scheduled in.
631 if (is_cgroup_event(event
) && !perf_cgroup_match(event
))
632 event
->cgrp_defer_enabled
= 1;
636 perf_cgroup_mark_enabled(struct perf_event
*event
,
637 struct perf_event_context
*ctx
)
639 struct perf_event
*sub
;
640 u64 tstamp
= perf_event_time(event
);
642 if (!event
->cgrp_defer_enabled
)
645 event
->cgrp_defer_enabled
= 0;
647 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
648 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
649 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
) {
650 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
651 sub
->cgrp_defer_enabled
= 0;
655 #else /* !CONFIG_CGROUP_PERF */
658 perf_cgroup_match(struct perf_event
*event
)
663 static inline void perf_detach_cgroup(struct perf_event
*event
)
666 static inline int is_cgroup_event(struct perf_event
*event
)
671 static inline u64
perf_cgroup_event_cgrp_time(struct perf_event
*event
)
676 static inline void update_cgrp_time_from_event(struct perf_event
*event
)
680 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context
*cpuctx
)
684 static inline void perf_cgroup_sched_out(struct task_struct
*task
,
685 struct task_struct
*next
)
689 static inline void perf_cgroup_sched_in(struct task_struct
*prev
,
690 struct task_struct
*task
)
694 static inline int perf_cgroup_connect(pid_t pid
, struct perf_event
*event
,
695 struct perf_event_attr
*attr
,
696 struct perf_event
*group_leader
)
702 perf_cgroup_set_timestamp(struct task_struct
*task
,
703 struct perf_event_context
*ctx
)
708 perf_cgroup_switch(struct task_struct
*task
, struct task_struct
*next
)
713 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
717 static inline u64
perf_cgroup_event_time(struct perf_event
*event
)
723 perf_cgroup_defer_enabled(struct perf_event
*event
)
728 perf_cgroup_mark_enabled(struct perf_event
*event
,
729 struct perf_event_context
*ctx
)
735 * set default to be dependent on timer tick just
738 #define PERF_CPU_HRTIMER (1000 / HZ)
740 * function must be called with interrupts disbled
742 static enum hrtimer_restart
perf_cpu_hrtimer_handler(struct hrtimer
*hr
)
744 struct perf_cpu_context
*cpuctx
;
745 enum hrtimer_restart ret
= HRTIMER_NORESTART
;
748 WARN_ON(!irqs_disabled());
750 cpuctx
= container_of(hr
, struct perf_cpu_context
, hrtimer
);
752 rotations
= perf_rotate_context(cpuctx
);
755 * arm timer if needed
758 hrtimer_forward_now(hr
, cpuctx
->hrtimer_interval
);
759 ret
= HRTIMER_RESTART
;
765 /* CPU is going down */
766 void perf_cpu_hrtimer_cancel(int cpu
)
768 struct perf_cpu_context
*cpuctx
;
772 if (WARN_ON(cpu
!= smp_processor_id()))
775 local_irq_save(flags
);
779 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
780 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
782 if (pmu
->task_ctx_nr
== perf_sw_context
)
785 hrtimer_cancel(&cpuctx
->hrtimer
);
790 local_irq_restore(flags
);
793 static void __perf_cpu_hrtimer_init(struct perf_cpu_context
*cpuctx
, int cpu
)
795 struct hrtimer
*hr
= &cpuctx
->hrtimer
;
796 struct pmu
*pmu
= cpuctx
->ctx
.pmu
;
799 /* no multiplexing needed for SW PMU */
800 if (pmu
->task_ctx_nr
== perf_sw_context
)
804 * check default is sane, if not set then force to
805 * default interval (1/tick)
807 timer
= pmu
->hrtimer_interval_ms
;
809 timer
= pmu
->hrtimer_interval_ms
= PERF_CPU_HRTIMER
;
811 cpuctx
->hrtimer_interval
= ns_to_ktime(NSEC_PER_MSEC
* timer
);
813 hrtimer_init(hr
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL_PINNED
);
814 hr
->function
= perf_cpu_hrtimer_handler
;
817 static void perf_cpu_hrtimer_restart(struct perf_cpu_context
*cpuctx
)
819 struct hrtimer
*hr
= &cpuctx
->hrtimer
;
820 struct pmu
*pmu
= cpuctx
->ctx
.pmu
;
823 if (pmu
->task_ctx_nr
== perf_sw_context
)
826 if (hrtimer_active(hr
))
829 if (!hrtimer_callback_running(hr
))
830 __hrtimer_start_range_ns(hr
, cpuctx
->hrtimer_interval
,
831 0, HRTIMER_MODE_REL_PINNED
, 0);
834 void perf_pmu_disable(struct pmu
*pmu
)
836 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
838 pmu
->pmu_disable(pmu
);
841 void perf_pmu_enable(struct pmu
*pmu
)
843 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
845 pmu
->pmu_enable(pmu
);
848 static DEFINE_PER_CPU(struct list_head
, rotation_list
);
851 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
852 * because they're strictly cpu affine and rotate_start is called with IRQs
853 * disabled, while rotate_context is called from IRQ context.
855 static void perf_pmu_rotate_start(struct pmu
*pmu
)
857 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
858 struct list_head
*head
= &__get_cpu_var(rotation_list
);
860 WARN_ON(!irqs_disabled());
862 if (list_empty(&cpuctx
->rotation_list
))
863 list_add(&cpuctx
->rotation_list
, head
);
866 static void get_ctx(struct perf_event_context
*ctx
)
868 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
871 static void put_ctx(struct perf_event_context
*ctx
)
873 if (atomic_dec_and_test(&ctx
->refcount
)) {
875 put_ctx(ctx
->parent_ctx
);
877 put_task_struct(ctx
->task
);
878 kfree_rcu(ctx
, rcu_head
);
882 static void unclone_ctx(struct perf_event_context
*ctx
)
884 if (ctx
->parent_ctx
) {
885 put_ctx(ctx
->parent_ctx
);
886 ctx
->parent_ctx
= NULL
;
891 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
894 * only top level events have the pid namespace they were created in
897 event
= event
->parent
;
899 return task_tgid_nr_ns(p
, event
->ns
);
902 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
905 * only top level events have the pid namespace they were created in
908 event
= event
->parent
;
910 return task_pid_nr_ns(p
, event
->ns
);
914 * If we inherit events we want to return the parent event id
917 static u64
primary_event_id(struct perf_event
*event
)
922 id
= event
->parent
->id
;
928 * Get the perf_event_context for a task and lock it.
929 * This has to cope with with the fact that until it is locked,
930 * the context could get moved to another task.
932 static struct perf_event_context
*
933 perf_lock_task_context(struct task_struct
*task
, int ctxn
, unsigned long *flags
)
935 struct perf_event_context
*ctx
;
939 * One of the few rules of preemptible RCU is that one cannot do
940 * rcu_read_unlock() while holding a scheduler (or nested) lock when
941 * part of the read side critical section was preemptible -- see
942 * rcu_read_unlock_special().
944 * Since ctx->lock nests under rq->lock we must ensure the entire read
945 * side critical section is non-preemptible.
949 ctx
= rcu_dereference(task
->perf_event_ctxp
[ctxn
]);
952 * If this context is a clone of another, it might
953 * get swapped for another underneath us by
954 * perf_event_task_sched_out, though the
955 * rcu_read_lock() protects us from any context
956 * getting freed. Lock the context and check if it
957 * got swapped before we could get the lock, and retry
958 * if so. If we locked the right context, then it
959 * can't get swapped on us any more.
961 raw_spin_lock_irqsave(&ctx
->lock
, *flags
);
962 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
[ctxn
])) {
963 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
969 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
970 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
980 * Get the context for a task and increment its pin_count so it
981 * can't get swapped to another task. This also increments its
982 * reference count so that the context can't get freed.
984 static struct perf_event_context
*
985 perf_pin_task_context(struct task_struct
*task
, int ctxn
)
987 struct perf_event_context
*ctx
;
990 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
993 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
998 static void perf_unpin_context(struct perf_event_context
*ctx
)
1000 unsigned long flags
;
1002 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
1004 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1008 * Update the record of the current time in a context.
1010 static void update_context_time(struct perf_event_context
*ctx
)
1012 u64 now
= perf_clock();
1014 ctx
->time
+= now
- ctx
->timestamp
;
1015 ctx
->timestamp
= now
;
1018 static u64
perf_event_time(struct perf_event
*event
)
1020 struct perf_event_context
*ctx
= event
->ctx
;
1022 if (is_cgroup_event(event
))
1023 return perf_cgroup_event_time(event
);
1025 return ctx
? ctx
->time
: 0;
1029 * Update the total_time_enabled and total_time_running fields for a event.
1030 * The caller of this function needs to hold the ctx->lock.
1032 static void update_event_times(struct perf_event
*event
)
1034 struct perf_event_context
*ctx
= event
->ctx
;
1037 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
1038 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
1041 * in cgroup mode, time_enabled represents
1042 * the time the event was enabled AND active
1043 * tasks were in the monitored cgroup. This is
1044 * independent of the activity of the context as
1045 * there may be a mix of cgroup and non-cgroup events.
1047 * That is why we treat cgroup events differently
1050 if (is_cgroup_event(event
))
1051 run_end
= perf_cgroup_event_time(event
);
1052 else if (ctx
->is_active
)
1053 run_end
= ctx
->time
;
1055 run_end
= event
->tstamp_stopped
;
1057 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
1059 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
1060 run_end
= event
->tstamp_stopped
;
1062 run_end
= perf_event_time(event
);
1064 event
->total_time_running
= run_end
- event
->tstamp_running
;
1069 * Update total_time_enabled and total_time_running for all events in a group.
1071 static void update_group_times(struct perf_event
*leader
)
1073 struct perf_event
*event
;
1075 update_event_times(leader
);
1076 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
1077 update_event_times(event
);
1080 static struct list_head
*
1081 ctx_group_list(struct perf_event
*event
, struct perf_event_context
*ctx
)
1083 if (event
->attr
.pinned
)
1084 return &ctx
->pinned_groups
;
1086 return &ctx
->flexible_groups
;
1090 * Add a event from the lists for its context.
1091 * Must be called with ctx->mutex and ctx->lock held.
1094 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
1096 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_CONTEXT
);
1097 event
->attach_state
|= PERF_ATTACH_CONTEXT
;
1100 * If we're a stand alone event or group leader, we go to the context
1101 * list, group events are kept attached to the group so that
1102 * perf_group_detach can, at all times, locate all siblings.
1104 if (event
->group_leader
== event
) {
1105 struct list_head
*list
;
1107 if (is_software_event(event
))
1108 event
->group_flags
|= PERF_GROUP_SOFTWARE
;
1110 list
= ctx_group_list(event
, ctx
);
1111 list_add_tail(&event
->group_entry
, list
);
1114 if (is_cgroup_event(event
))
1117 if (has_branch_stack(event
))
1118 ctx
->nr_branch_stack
++;
1120 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
1121 if (!ctx
->nr_events
)
1122 perf_pmu_rotate_start(ctx
->pmu
);
1124 if (event
->attr
.inherit_stat
)
1131 * Initialize event state based on the perf_event_attr::disabled.
1133 static inline void perf_event__state_init(struct perf_event
*event
)
1135 event
->state
= event
->attr
.disabled
? PERF_EVENT_STATE_OFF
:
1136 PERF_EVENT_STATE_INACTIVE
;
1140 * Called at perf_event creation and when events are attached/detached from a
1143 static void perf_event__read_size(struct perf_event
*event
)
1145 int entry
= sizeof(u64
); /* value */
1149 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1150 size
+= sizeof(u64
);
1152 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1153 size
+= sizeof(u64
);
1155 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
1156 entry
+= sizeof(u64
);
1158 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
1159 nr
+= event
->group_leader
->nr_siblings
;
1160 size
+= sizeof(u64
);
1164 event
->read_size
= size
;
1167 static void perf_event__header_size(struct perf_event
*event
)
1169 struct perf_sample_data
*data
;
1170 u64 sample_type
= event
->attr
.sample_type
;
1173 perf_event__read_size(event
);
1175 if (sample_type
& PERF_SAMPLE_IP
)
1176 size
+= sizeof(data
->ip
);
1178 if (sample_type
& PERF_SAMPLE_ADDR
)
1179 size
+= sizeof(data
->addr
);
1181 if (sample_type
& PERF_SAMPLE_PERIOD
)
1182 size
+= sizeof(data
->period
);
1184 if (sample_type
& PERF_SAMPLE_WEIGHT
)
1185 size
+= sizeof(data
->weight
);
1187 if (sample_type
& PERF_SAMPLE_READ
)
1188 size
+= event
->read_size
;
1190 if (sample_type
& PERF_SAMPLE_DATA_SRC
)
1191 size
+= sizeof(data
->data_src
.val
);
1193 if (sample_type
& PERF_SAMPLE_TRANSACTION
)
1194 size
+= sizeof(data
->txn
);
1196 event
->header_size
= size
;
1199 static void perf_event__id_header_size(struct perf_event
*event
)
1201 struct perf_sample_data
*data
;
1202 u64 sample_type
= event
->attr
.sample_type
;
1205 if (sample_type
& PERF_SAMPLE_TID
)
1206 size
+= sizeof(data
->tid_entry
);
1208 if (sample_type
& PERF_SAMPLE_TIME
)
1209 size
+= sizeof(data
->time
);
1211 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
1212 size
+= sizeof(data
->id
);
1214 if (sample_type
& PERF_SAMPLE_ID
)
1215 size
+= sizeof(data
->id
);
1217 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
1218 size
+= sizeof(data
->stream_id
);
1220 if (sample_type
& PERF_SAMPLE_CPU
)
1221 size
+= sizeof(data
->cpu_entry
);
1223 event
->id_header_size
= size
;
1226 static void perf_group_attach(struct perf_event
*event
)
1228 struct perf_event
*group_leader
= event
->group_leader
, *pos
;
1231 * We can have double attach due to group movement in perf_event_open.
1233 if (event
->attach_state
& PERF_ATTACH_GROUP
)
1236 event
->attach_state
|= PERF_ATTACH_GROUP
;
1238 if (group_leader
== event
)
1241 if (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
&&
1242 !is_software_event(event
))
1243 group_leader
->group_flags
&= ~PERF_GROUP_SOFTWARE
;
1245 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
1246 group_leader
->nr_siblings
++;
1248 perf_event__header_size(group_leader
);
1250 list_for_each_entry(pos
, &group_leader
->sibling_list
, group_entry
)
1251 perf_event__header_size(pos
);
1255 * Remove a event from the lists for its context.
1256 * Must be called with ctx->mutex and ctx->lock held.
1259 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
1261 struct perf_cpu_context
*cpuctx
;
1263 * We can have double detach due to exit/hot-unplug + close.
1265 if (!(event
->attach_state
& PERF_ATTACH_CONTEXT
))
1268 event
->attach_state
&= ~PERF_ATTACH_CONTEXT
;
1270 if (is_cgroup_event(event
)) {
1272 cpuctx
= __get_cpu_context(ctx
);
1274 * if there are no more cgroup events
1275 * then cler cgrp to avoid stale pointer
1276 * in update_cgrp_time_from_cpuctx()
1278 if (!ctx
->nr_cgroups
)
1279 cpuctx
->cgrp
= NULL
;
1282 if (has_branch_stack(event
))
1283 ctx
->nr_branch_stack
--;
1286 if (event
->attr
.inherit_stat
)
1289 list_del_rcu(&event
->event_entry
);
1291 if (event
->group_leader
== event
)
1292 list_del_init(&event
->group_entry
);
1294 update_group_times(event
);
1297 * If event was in error state, then keep it
1298 * that way, otherwise bogus counts will be
1299 * returned on read(). The only way to get out
1300 * of error state is by explicit re-enabling
1303 if (event
->state
> PERF_EVENT_STATE_OFF
)
1304 event
->state
= PERF_EVENT_STATE_OFF
;
1309 static void perf_group_detach(struct perf_event
*event
)
1311 struct perf_event
*sibling
, *tmp
;
1312 struct list_head
*list
= NULL
;
1315 * We can have double detach due to exit/hot-unplug + close.
1317 if (!(event
->attach_state
& PERF_ATTACH_GROUP
))
1320 event
->attach_state
&= ~PERF_ATTACH_GROUP
;
1323 * If this is a sibling, remove it from its group.
1325 if (event
->group_leader
!= event
) {
1326 list_del_init(&event
->group_entry
);
1327 event
->group_leader
->nr_siblings
--;
1331 if (!list_empty(&event
->group_entry
))
1332 list
= &event
->group_entry
;
1335 * If this was a group event with sibling events then
1336 * upgrade the siblings to singleton events by adding them
1337 * to whatever list we are on.
1339 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
1341 list_move_tail(&sibling
->group_entry
, list
);
1342 sibling
->group_leader
= sibling
;
1344 /* Inherit group flags from the previous leader */
1345 sibling
->group_flags
= event
->group_flags
;
1349 perf_event__header_size(event
->group_leader
);
1351 list_for_each_entry(tmp
, &event
->group_leader
->sibling_list
, group_entry
)
1352 perf_event__header_size(tmp
);
1356 event_filter_match(struct perf_event
*event
)
1358 return (event
->cpu
== -1 || event
->cpu
== smp_processor_id())
1359 && perf_cgroup_match(event
);
1363 event_sched_out(struct perf_event
*event
,
1364 struct perf_cpu_context
*cpuctx
,
1365 struct perf_event_context
*ctx
)
1367 u64 tstamp
= perf_event_time(event
);
1370 * An event which could not be activated because of
1371 * filter mismatch still needs to have its timings
1372 * maintained, otherwise bogus information is return
1373 * via read() for time_enabled, time_running:
1375 if (event
->state
== PERF_EVENT_STATE_INACTIVE
1376 && !event_filter_match(event
)) {
1377 delta
= tstamp
- event
->tstamp_stopped
;
1378 event
->tstamp_running
+= delta
;
1379 event
->tstamp_stopped
= tstamp
;
1382 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1385 perf_pmu_disable(event
->pmu
);
1387 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1388 if (event
->pending_disable
) {
1389 event
->pending_disable
= 0;
1390 event
->state
= PERF_EVENT_STATE_OFF
;
1392 event
->tstamp_stopped
= tstamp
;
1393 event
->pmu
->del(event
, 0);
1396 if (!is_software_event(event
))
1397 cpuctx
->active_oncpu
--;
1399 if (event
->attr
.freq
&& event
->attr
.sample_freq
)
1401 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
1402 cpuctx
->exclusive
= 0;
1404 perf_pmu_enable(event
->pmu
);
1408 group_sched_out(struct perf_event
*group_event
,
1409 struct perf_cpu_context
*cpuctx
,
1410 struct perf_event_context
*ctx
)
1412 struct perf_event
*event
;
1413 int state
= group_event
->state
;
1415 event_sched_out(group_event
, cpuctx
, ctx
);
1418 * Schedule out siblings (if any):
1420 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
1421 event_sched_out(event
, cpuctx
, ctx
);
1423 if (state
== PERF_EVENT_STATE_ACTIVE
&& group_event
->attr
.exclusive
)
1424 cpuctx
->exclusive
= 0;
1428 * Cross CPU call to remove a performance event
1430 * We disable the event on the hardware level first. After that we
1431 * remove it from the context list.
1433 static int __perf_remove_from_context(void *info
)
1435 struct perf_event
*event
= info
;
1436 struct perf_event_context
*ctx
= event
->ctx
;
1437 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1439 raw_spin_lock(&ctx
->lock
);
1440 event_sched_out(event
, cpuctx
, ctx
);
1441 list_del_event(event
, ctx
);
1442 if (!ctx
->nr_events
&& cpuctx
->task_ctx
== ctx
) {
1444 cpuctx
->task_ctx
= NULL
;
1446 raw_spin_unlock(&ctx
->lock
);
1453 * Remove the event from a task's (or a CPU's) list of events.
1455 * CPU events are removed with a smp call. For task events we only
1456 * call when the task is on a CPU.
1458 * If event->ctx is a cloned context, callers must make sure that
1459 * every task struct that event->ctx->task could possibly point to
1460 * remains valid. This is OK when called from perf_release since
1461 * that only calls us on the top-level context, which can't be a clone.
1462 * When called from perf_event_exit_task, it's OK because the
1463 * context has been detached from its task.
1465 static void perf_remove_from_context(struct perf_event
*event
)
1467 struct perf_event_context
*ctx
= event
->ctx
;
1468 struct task_struct
*task
= ctx
->task
;
1470 lockdep_assert_held(&ctx
->mutex
);
1474 * Per cpu events are removed via an smp call and
1475 * the removal is always successful.
1477 cpu_function_call(event
->cpu
, __perf_remove_from_context
, event
);
1482 if (!task_function_call(task
, __perf_remove_from_context
, event
))
1485 raw_spin_lock_irq(&ctx
->lock
);
1487 * If we failed to find a running task, but find the context active now
1488 * that we've acquired the ctx->lock, retry.
1490 if (ctx
->is_active
) {
1491 raw_spin_unlock_irq(&ctx
->lock
);
1496 * Since the task isn't running, its safe to remove the event, us
1497 * holding the ctx->lock ensures the task won't get scheduled in.
1499 list_del_event(event
, ctx
);
1500 raw_spin_unlock_irq(&ctx
->lock
);
1504 * Cross CPU call to disable a performance event
1506 int __perf_event_disable(void *info
)
1508 struct perf_event
*event
= info
;
1509 struct perf_event_context
*ctx
= event
->ctx
;
1510 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1513 * If this is a per-task event, need to check whether this
1514 * event's task is the current task on this cpu.
1516 * Can trigger due to concurrent perf_event_context_sched_out()
1517 * flipping contexts around.
1519 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
1522 raw_spin_lock(&ctx
->lock
);
1525 * If the event is on, turn it off.
1526 * If it is in error state, leave it in error state.
1528 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
) {
1529 update_context_time(ctx
);
1530 update_cgrp_time_from_event(event
);
1531 update_group_times(event
);
1532 if (event
== event
->group_leader
)
1533 group_sched_out(event
, cpuctx
, ctx
);
1535 event_sched_out(event
, cpuctx
, ctx
);
1536 event
->state
= PERF_EVENT_STATE_OFF
;
1539 raw_spin_unlock(&ctx
->lock
);
1547 * If event->ctx is a cloned context, callers must make sure that
1548 * every task struct that event->ctx->task could possibly point to
1549 * remains valid. This condition is satisifed when called through
1550 * perf_event_for_each_child or perf_event_for_each because they
1551 * hold the top-level event's child_mutex, so any descendant that
1552 * goes to exit will block in sync_child_event.
1553 * When called from perf_pending_event it's OK because event->ctx
1554 * is the current context on this CPU and preemption is disabled,
1555 * hence we can't get into perf_event_task_sched_out for this context.
1557 void perf_event_disable(struct perf_event
*event
)
1559 struct perf_event_context
*ctx
= event
->ctx
;
1560 struct task_struct
*task
= ctx
->task
;
1564 * Disable the event on the cpu that it's on
1566 cpu_function_call(event
->cpu
, __perf_event_disable
, event
);
1571 if (!task_function_call(task
, __perf_event_disable
, event
))
1574 raw_spin_lock_irq(&ctx
->lock
);
1576 * If the event is still active, we need to retry the cross-call.
1578 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
1579 raw_spin_unlock_irq(&ctx
->lock
);
1581 * Reload the task pointer, it might have been changed by
1582 * a concurrent perf_event_context_sched_out().
1589 * Since we have the lock this context can't be scheduled
1590 * in, so we can change the state safely.
1592 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1593 update_group_times(event
);
1594 event
->state
= PERF_EVENT_STATE_OFF
;
1596 raw_spin_unlock_irq(&ctx
->lock
);
1598 EXPORT_SYMBOL_GPL(perf_event_disable
);
1600 static void perf_set_shadow_time(struct perf_event
*event
,
1601 struct perf_event_context
*ctx
,
1605 * use the correct time source for the time snapshot
1607 * We could get by without this by leveraging the
1608 * fact that to get to this function, the caller
1609 * has most likely already called update_context_time()
1610 * and update_cgrp_time_xx() and thus both timestamp
1611 * are identical (or very close). Given that tstamp is,
1612 * already adjusted for cgroup, we could say that:
1613 * tstamp - ctx->timestamp
1615 * tstamp - cgrp->timestamp.
1617 * Then, in perf_output_read(), the calculation would
1618 * work with no changes because:
1619 * - event is guaranteed scheduled in
1620 * - no scheduled out in between
1621 * - thus the timestamp would be the same
1623 * But this is a bit hairy.
1625 * So instead, we have an explicit cgroup call to remain
1626 * within the time time source all along. We believe it
1627 * is cleaner and simpler to understand.
1629 if (is_cgroup_event(event
))
1630 perf_cgroup_set_shadow_time(event
, tstamp
);
1632 event
->shadow_ctx_time
= tstamp
- ctx
->timestamp
;
1635 #define MAX_INTERRUPTS (~0ULL)
1637 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1640 event_sched_in(struct perf_event
*event
,
1641 struct perf_cpu_context
*cpuctx
,
1642 struct perf_event_context
*ctx
)
1644 u64 tstamp
= perf_event_time(event
);
1647 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1650 event
->state
= PERF_EVENT_STATE_ACTIVE
;
1651 event
->oncpu
= smp_processor_id();
1654 * Unthrottle events, since we scheduled we might have missed several
1655 * ticks already, also for a heavily scheduling task there is little
1656 * guarantee it'll get a tick in a timely manner.
1658 if (unlikely(event
->hw
.interrupts
== MAX_INTERRUPTS
)) {
1659 perf_log_throttle(event
, 1);
1660 event
->hw
.interrupts
= 0;
1664 * The new state must be visible before we turn it on in the hardware:
1668 perf_pmu_disable(event
->pmu
);
1670 if (event
->pmu
->add(event
, PERF_EF_START
)) {
1671 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1677 event
->tstamp_running
+= tstamp
- event
->tstamp_stopped
;
1679 perf_set_shadow_time(event
, ctx
, tstamp
);
1681 if (!is_software_event(event
))
1682 cpuctx
->active_oncpu
++;
1684 if (event
->attr
.freq
&& event
->attr
.sample_freq
)
1687 if (event
->attr
.exclusive
)
1688 cpuctx
->exclusive
= 1;
1691 perf_pmu_enable(event
->pmu
);
1697 group_sched_in(struct perf_event
*group_event
,
1698 struct perf_cpu_context
*cpuctx
,
1699 struct perf_event_context
*ctx
)
1701 struct perf_event
*event
, *partial_group
= NULL
;
1702 struct pmu
*pmu
= group_event
->pmu
;
1703 u64 now
= ctx
->time
;
1704 bool simulate
= false;
1706 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
1709 pmu
->start_txn(pmu
);
1711 if (event_sched_in(group_event
, cpuctx
, ctx
)) {
1712 pmu
->cancel_txn(pmu
);
1713 perf_cpu_hrtimer_restart(cpuctx
);
1718 * Schedule in siblings as one group (if any):
1720 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
1721 if (event_sched_in(event
, cpuctx
, ctx
)) {
1722 partial_group
= event
;
1727 if (!pmu
->commit_txn(pmu
))
1732 * Groups can be scheduled in as one unit only, so undo any
1733 * partial group before returning:
1734 * The events up to the failed event are scheduled out normally,
1735 * tstamp_stopped will be updated.
1737 * The failed events and the remaining siblings need to have
1738 * their timings updated as if they had gone thru event_sched_in()
1739 * and event_sched_out(). This is required to get consistent timings
1740 * across the group. This also takes care of the case where the group
1741 * could never be scheduled by ensuring tstamp_stopped is set to mark
1742 * the time the event was actually stopped, such that time delta
1743 * calculation in update_event_times() is correct.
1745 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
1746 if (event
== partial_group
)
1750 event
->tstamp_running
+= now
- event
->tstamp_stopped
;
1751 event
->tstamp_stopped
= now
;
1753 event_sched_out(event
, cpuctx
, ctx
);
1756 event_sched_out(group_event
, cpuctx
, ctx
);
1758 pmu
->cancel_txn(pmu
);
1760 perf_cpu_hrtimer_restart(cpuctx
);
1766 * Work out whether we can put this event group on the CPU now.
1768 static int group_can_go_on(struct perf_event
*event
,
1769 struct perf_cpu_context
*cpuctx
,
1773 * Groups consisting entirely of software events can always go on.
1775 if (event
->group_flags
& PERF_GROUP_SOFTWARE
)
1778 * If an exclusive group is already on, no other hardware
1781 if (cpuctx
->exclusive
)
1784 * If this group is exclusive and there are already
1785 * events on the CPU, it can't go on.
1787 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
1790 * Otherwise, try to add it if all previous groups were able
1796 static void add_event_to_ctx(struct perf_event
*event
,
1797 struct perf_event_context
*ctx
)
1799 u64 tstamp
= perf_event_time(event
);
1801 list_add_event(event
, ctx
);
1802 perf_group_attach(event
);
1803 event
->tstamp_enabled
= tstamp
;
1804 event
->tstamp_running
= tstamp
;
1805 event
->tstamp_stopped
= tstamp
;
1808 static void task_ctx_sched_out(struct perf_event_context
*ctx
);
1810 ctx_sched_in(struct perf_event_context
*ctx
,
1811 struct perf_cpu_context
*cpuctx
,
1812 enum event_type_t event_type
,
1813 struct task_struct
*task
);
1815 static void perf_event_sched_in(struct perf_cpu_context
*cpuctx
,
1816 struct perf_event_context
*ctx
,
1817 struct task_struct
*task
)
1819 cpu_ctx_sched_in(cpuctx
, EVENT_PINNED
, task
);
1821 ctx_sched_in(ctx
, cpuctx
, EVENT_PINNED
, task
);
1822 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
, task
);
1824 ctx_sched_in(ctx
, cpuctx
, EVENT_FLEXIBLE
, task
);
1828 * Cross CPU call to install and enable a performance event
1830 * Must be called with ctx->mutex held
1832 static int __perf_install_in_context(void *info
)
1834 struct perf_event
*event
= info
;
1835 struct perf_event_context
*ctx
= event
->ctx
;
1836 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1837 struct perf_event_context
*task_ctx
= cpuctx
->task_ctx
;
1838 struct task_struct
*task
= current
;
1840 perf_ctx_lock(cpuctx
, task_ctx
);
1841 perf_pmu_disable(cpuctx
->ctx
.pmu
);
1844 * If there was an active task_ctx schedule it out.
1847 task_ctx_sched_out(task_ctx
);
1850 * If the context we're installing events in is not the
1851 * active task_ctx, flip them.
1853 if (ctx
->task
&& task_ctx
!= ctx
) {
1855 raw_spin_unlock(&task_ctx
->lock
);
1856 raw_spin_lock(&ctx
->lock
);
1861 cpuctx
->task_ctx
= task_ctx
;
1862 task
= task_ctx
->task
;
1865 cpu_ctx_sched_out(cpuctx
, EVENT_ALL
);
1867 update_context_time(ctx
);
1869 * update cgrp time only if current cgrp
1870 * matches event->cgrp. Must be done before
1871 * calling add_event_to_ctx()
1873 update_cgrp_time_from_event(event
);
1875 add_event_to_ctx(event
, ctx
);
1878 * Schedule everything back in
1880 perf_event_sched_in(cpuctx
, task_ctx
, task
);
1882 perf_pmu_enable(cpuctx
->ctx
.pmu
);
1883 perf_ctx_unlock(cpuctx
, task_ctx
);
1889 * Attach a performance event to a context
1891 * First we add the event to the list with the hardware enable bit
1892 * in event->hw_config cleared.
1894 * If the event is attached to a task which is on a CPU we use a smp
1895 * call to enable it in the task context. The task might have been
1896 * scheduled away, but we check this in the smp call again.
1899 perf_install_in_context(struct perf_event_context
*ctx
,
1900 struct perf_event
*event
,
1903 struct task_struct
*task
= ctx
->task
;
1905 lockdep_assert_held(&ctx
->mutex
);
1908 if (event
->cpu
!= -1)
1913 * Per cpu events are installed via an smp call and
1914 * the install is always successful.
1916 cpu_function_call(cpu
, __perf_install_in_context
, event
);
1921 if (!task_function_call(task
, __perf_install_in_context
, event
))
1924 raw_spin_lock_irq(&ctx
->lock
);
1926 * If we failed to find a running task, but find the context active now
1927 * that we've acquired the ctx->lock, retry.
1929 if (ctx
->is_active
) {
1930 raw_spin_unlock_irq(&ctx
->lock
);
1935 * Since the task isn't running, its safe to add the event, us holding
1936 * the ctx->lock ensures the task won't get scheduled in.
1938 add_event_to_ctx(event
, ctx
);
1939 raw_spin_unlock_irq(&ctx
->lock
);
1943 * Put a event into inactive state and update time fields.
1944 * Enabling the leader of a group effectively enables all
1945 * the group members that aren't explicitly disabled, so we
1946 * have to update their ->tstamp_enabled also.
1947 * Note: this works for group members as well as group leaders
1948 * since the non-leader members' sibling_lists will be empty.
1950 static void __perf_event_mark_enabled(struct perf_event
*event
)
1952 struct perf_event
*sub
;
1953 u64 tstamp
= perf_event_time(event
);
1955 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1956 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
1957 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
1958 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
)
1959 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
1964 * Cross CPU call to enable a performance event
1966 static int __perf_event_enable(void *info
)
1968 struct perf_event
*event
= info
;
1969 struct perf_event_context
*ctx
= event
->ctx
;
1970 struct perf_event
*leader
= event
->group_leader
;
1971 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1975 * There's a time window between 'ctx->is_active' check
1976 * in perf_event_enable function and this place having:
1978 * - ctx->lock unlocked
1980 * where the task could be killed and 'ctx' deactivated
1981 * by perf_event_exit_task.
1983 if (!ctx
->is_active
)
1986 raw_spin_lock(&ctx
->lock
);
1987 update_context_time(ctx
);
1989 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1993 * set current task's cgroup time reference point
1995 perf_cgroup_set_timestamp(current
, ctx
);
1997 __perf_event_mark_enabled(event
);
1999 if (!event_filter_match(event
)) {
2000 if (is_cgroup_event(event
))
2001 perf_cgroup_defer_enabled(event
);
2006 * If the event is in a group and isn't the group leader,
2007 * then don't put it on unless the group is on.
2009 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
)
2012 if (!group_can_go_on(event
, cpuctx
, 1)) {
2015 if (event
== leader
)
2016 err
= group_sched_in(event
, cpuctx
, ctx
);
2018 err
= event_sched_in(event
, cpuctx
, ctx
);
2023 * If this event can't go on and it's part of a
2024 * group, then the whole group has to come off.
2026 if (leader
!= event
) {
2027 group_sched_out(leader
, cpuctx
, ctx
);
2028 perf_cpu_hrtimer_restart(cpuctx
);
2030 if (leader
->attr
.pinned
) {
2031 update_group_times(leader
);
2032 leader
->state
= PERF_EVENT_STATE_ERROR
;
2037 raw_spin_unlock(&ctx
->lock
);
2045 * If event->ctx is a cloned context, callers must make sure that
2046 * every task struct that event->ctx->task could possibly point to
2047 * remains valid. This condition is satisfied when called through
2048 * perf_event_for_each_child or perf_event_for_each as described
2049 * for perf_event_disable.
2051 void perf_event_enable(struct perf_event
*event
)
2053 struct perf_event_context
*ctx
= event
->ctx
;
2054 struct task_struct
*task
= ctx
->task
;
2058 * Enable the event on the cpu that it's on
2060 cpu_function_call(event
->cpu
, __perf_event_enable
, event
);
2064 raw_spin_lock_irq(&ctx
->lock
);
2065 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
2069 * If the event is in error state, clear that first.
2070 * That way, if we see the event in error state below, we
2071 * know that it has gone back into error state, as distinct
2072 * from the task having been scheduled away before the
2073 * cross-call arrived.
2075 if (event
->state
== PERF_EVENT_STATE_ERROR
)
2076 event
->state
= PERF_EVENT_STATE_OFF
;
2079 if (!ctx
->is_active
) {
2080 __perf_event_mark_enabled(event
);
2084 raw_spin_unlock_irq(&ctx
->lock
);
2086 if (!task_function_call(task
, __perf_event_enable
, event
))
2089 raw_spin_lock_irq(&ctx
->lock
);
2092 * If the context is active and the event is still off,
2093 * we need to retry the cross-call.
2095 if (ctx
->is_active
&& event
->state
== PERF_EVENT_STATE_OFF
) {
2097 * task could have been flipped by a concurrent
2098 * perf_event_context_sched_out()
2105 raw_spin_unlock_irq(&ctx
->lock
);
2107 EXPORT_SYMBOL_GPL(perf_event_enable
);
2109 int perf_event_refresh(struct perf_event
*event
, int refresh
)
2112 * not supported on inherited events
2114 if (event
->attr
.inherit
|| !is_sampling_event(event
))
2117 atomic_add(refresh
, &event
->event_limit
);
2118 perf_event_enable(event
);
2122 EXPORT_SYMBOL_GPL(perf_event_refresh
);
2124 static void ctx_sched_out(struct perf_event_context
*ctx
,
2125 struct perf_cpu_context
*cpuctx
,
2126 enum event_type_t event_type
)
2128 struct perf_event
*event
;
2129 int is_active
= ctx
->is_active
;
2131 ctx
->is_active
&= ~event_type
;
2132 if (likely(!ctx
->nr_events
))
2135 update_context_time(ctx
);
2136 update_cgrp_time_from_cpuctx(cpuctx
);
2137 if (!ctx
->nr_active
)
2140 perf_pmu_disable(ctx
->pmu
);
2141 if ((is_active
& EVENT_PINNED
) && (event_type
& EVENT_PINNED
)) {
2142 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
)
2143 group_sched_out(event
, cpuctx
, ctx
);
2146 if ((is_active
& EVENT_FLEXIBLE
) && (event_type
& EVENT_FLEXIBLE
)) {
2147 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
)
2148 group_sched_out(event
, cpuctx
, ctx
);
2150 perf_pmu_enable(ctx
->pmu
);
2154 * Test whether two contexts are equivalent, i.e. whether they have both been
2155 * cloned from the same version of the same context.
2157 * Equivalence is measured using a generation number in the context that is
2158 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2159 * and list_del_event().
2161 static int context_equiv(struct perf_event_context
*ctx1
,
2162 struct perf_event_context
*ctx2
)
2164 /* Pinning disables the swap optimization */
2165 if (ctx1
->pin_count
|| ctx2
->pin_count
)
2168 /* If ctx1 is the parent of ctx2 */
2169 if (ctx1
== ctx2
->parent_ctx
&& ctx1
->generation
== ctx2
->parent_gen
)
2172 /* If ctx2 is the parent of ctx1 */
2173 if (ctx1
->parent_ctx
== ctx2
&& ctx1
->parent_gen
== ctx2
->generation
)
2177 * If ctx1 and ctx2 have the same parent; we flatten the parent
2178 * hierarchy, see perf_event_init_context().
2180 if (ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
&&
2181 ctx1
->parent_gen
== ctx2
->parent_gen
)
2188 static void __perf_event_sync_stat(struct perf_event
*event
,
2189 struct perf_event
*next_event
)
2193 if (!event
->attr
.inherit_stat
)
2197 * Update the event value, we cannot use perf_event_read()
2198 * because we're in the middle of a context switch and have IRQs
2199 * disabled, which upsets smp_call_function_single(), however
2200 * we know the event must be on the current CPU, therefore we
2201 * don't need to use it.
2203 switch (event
->state
) {
2204 case PERF_EVENT_STATE_ACTIVE
:
2205 event
->pmu
->read(event
);
2208 case PERF_EVENT_STATE_INACTIVE
:
2209 update_event_times(event
);
2217 * In order to keep per-task stats reliable we need to flip the event
2218 * values when we flip the contexts.
2220 value
= local64_read(&next_event
->count
);
2221 value
= local64_xchg(&event
->count
, value
);
2222 local64_set(&next_event
->count
, value
);
2224 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
2225 swap(event
->total_time_running
, next_event
->total_time_running
);
2228 * Since we swizzled the values, update the user visible data too.
2230 perf_event_update_userpage(event
);
2231 perf_event_update_userpage(next_event
);
2234 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
2235 struct perf_event_context
*next_ctx
)
2237 struct perf_event
*event
, *next_event
;
2242 update_context_time(ctx
);
2244 event
= list_first_entry(&ctx
->event_list
,
2245 struct perf_event
, event_entry
);
2247 next_event
= list_first_entry(&next_ctx
->event_list
,
2248 struct perf_event
, event_entry
);
2250 while (&event
->event_entry
!= &ctx
->event_list
&&
2251 &next_event
->event_entry
!= &next_ctx
->event_list
) {
2253 __perf_event_sync_stat(event
, next_event
);
2255 event
= list_next_entry(event
, event_entry
);
2256 next_event
= list_next_entry(next_event
, event_entry
);
2260 static void perf_event_context_sched_out(struct task_struct
*task
, int ctxn
,
2261 struct task_struct
*next
)
2263 struct perf_event_context
*ctx
= task
->perf_event_ctxp
[ctxn
];
2264 struct perf_event_context
*next_ctx
;
2265 struct perf_event_context
*parent
, *next_parent
;
2266 struct perf_cpu_context
*cpuctx
;
2272 cpuctx
= __get_cpu_context(ctx
);
2273 if (!cpuctx
->task_ctx
)
2277 next_ctx
= next
->perf_event_ctxp
[ctxn
];
2281 parent
= rcu_dereference(ctx
->parent_ctx
);
2282 next_parent
= rcu_dereference(next_ctx
->parent_ctx
);
2284 /* If neither context have a parent context; they cannot be clones. */
2285 if (!parent
&& !next_parent
)
2288 if (next_parent
== ctx
|| next_ctx
== parent
|| next_parent
== parent
) {
2290 * Looks like the two contexts are clones, so we might be
2291 * able to optimize the context switch. We lock both
2292 * contexts and check that they are clones under the
2293 * lock (including re-checking that neither has been
2294 * uncloned in the meantime). It doesn't matter which
2295 * order we take the locks because no other cpu could
2296 * be trying to lock both of these tasks.
2298 raw_spin_lock(&ctx
->lock
);
2299 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
2300 if (context_equiv(ctx
, next_ctx
)) {
2302 * XXX do we need a memory barrier of sorts
2303 * wrt to rcu_dereference() of perf_event_ctxp
2305 task
->perf_event_ctxp
[ctxn
] = next_ctx
;
2306 next
->perf_event_ctxp
[ctxn
] = ctx
;
2308 next_ctx
->task
= task
;
2311 perf_event_sync_stat(ctx
, next_ctx
);
2313 raw_spin_unlock(&next_ctx
->lock
);
2314 raw_spin_unlock(&ctx
->lock
);
2320 raw_spin_lock(&ctx
->lock
);
2321 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
2322 cpuctx
->task_ctx
= NULL
;
2323 raw_spin_unlock(&ctx
->lock
);
2327 #define for_each_task_context_nr(ctxn) \
2328 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2331 * Called from scheduler to remove the events of the current task,
2332 * with interrupts disabled.
2334 * We stop each event and update the event value in event->count.
2336 * This does not protect us against NMI, but disable()
2337 * sets the disabled bit in the control field of event _before_
2338 * accessing the event control register. If a NMI hits, then it will
2339 * not restart the event.
2341 void __perf_event_task_sched_out(struct task_struct
*task
,
2342 struct task_struct
*next
)
2346 for_each_task_context_nr(ctxn
)
2347 perf_event_context_sched_out(task
, ctxn
, next
);
2350 * if cgroup events exist on this CPU, then we need
2351 * to check if we have to switch out PMU state.
2352 * cgroup event are system-wide mode only
2354 if (atomic_read(&__get_cpu_var(perf_cgroup_events
)))
2355 perf_cgroup_sched_out(task
, next
);
2358 static void task_ctx_sched_out(struct perf_event_context
*ctx
)
2360 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
2362 if (!cpuctx
->task_ctx
)
2365 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
2368 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
2369 cpuctx
->task_ctx
= NULL
;
2373 * Called with IRQs disabled
2375 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
2376 enum event_type_t event_type
)
2378 ctx_sched_out(&cpuctx
->ctx
, cpuctx
, event_type
);
2382 ctx_pinned_sched_in(struct perf_event_context
*ctx
,
2383 struct perf_cpu_context
*cpuctx
)
2385 struct perf_event
*event
;
2387 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
2388 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2390 if (!event_filter_match(event
))
2393 /* may need to reset tstamp_enabled */
2394 if (is_cgroup_event(event
))
2395 perf_cgroup_mark_enabled(event
, ctx
);
2397 if (group_can_go_on(event
, cpuctx
, 1))
2398 group_sched_in(event
, cpuctx
, ctx
);
2401 * If this pinned group hasn't been scheduled,
2402 * put it in error state.
2404 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
2405 update_group_times(event
);
2406 event
->state
= PERF_EVENT_STATE_ERROR
;
2412 ctx_flexible_sched_in(struct perf_event_context
*ctx
,
2413 struct perf_cpu_context
*cpuctx
)
2415 struct perf_event
*event
;
2418 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
2419 /* Ignore events in OFF or ERROR state */
2420 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2423 * Listen to the 'cpu' scheduling filter constraint
2426 if (!event_filter_match(event
))
2429 /* may need to reset tstamp_enabled */
2430 if (is_cgroup_event(event
))
2431 perf_cgroup_mark_enabled(event
, ctx
);
2433 if (group_can_go_on(event
, cpuctx
, can_add_hw
)) {
2434 if (group_sched_in(event
, cpuctx
, ctx
))
2441 ctx_sched_in(struct perf_event_context
*ctx
,
2442 struct perf_cpu_context
*cpuctx
,
2443 enum event_type_t event_type
,
2444 struct task_struct
*task
)
2447 int is_active
= ctx
->is_active
;
2449 ctx
->is_active
|= event_type
;
2450 if (likely(!ctx
->nr_events
))
2454 ctx
->timestamp
= now
;
2455 perf_cgroup_set_timestamp(task
, ctx
);
2457 * First go through the list and put on any pinned groups
2458 * in order to give them the best chance of going on.
2460 if (!(is_active
& EVENT_PINNED
) && (event_type
& EVENT_PINNED
))
2461 ctx_pinned_sched_in(ctx
, cpuctx
);
2463 /* Then walk through the lower prio flexible groups */
2464 if (!(is_active
& EVENT_FLEXIBLE
) && (event_type
& EVENT_FLEXIBLE
))
2465 ctx_flexible_sched_in(ctx
, cpuctx
);
2468 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
2469 enum event_type_t event_type
,
2470 struct task_struct
*task
)
2472 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
2474 ctx_sched_in(ctx
, cpuctx
, event_type
, task
);
2477 static void perf_event_context_sched_in(struct perf_event_context
*ctx
,
2478 struct task_struct
*task
)
2480 struct perf_cpu_context
*cpuctx
;
2482 cpuctx
= __get_cpu_context(ctx
);
2483 if (cpuctx
->task_ctx
== ctx
)
2486 perf_ctx_lock(cpuctx
, ctx
);
2487 perf_pmu_disable(ctx
->pmu
);
2489 * We want to keep the following priority order:
2490 * cpu pinned (that don't need to move), task pinned,
2491 * cpu flexible, task flexible.
2493 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
2496 cpuctx
->task_ctx
= ctx
;
2498 perf_event_sched_in(cpuctx
, cpuctx
->task_ctx
, task
);
2500 perf_pmu_enable(ctx
->pmu
);
2501 perf_ctx_unlock(cpuctx
, ctx
);
2504 * Since these rotations are per-cpu, we need to ensure the
2505 * cpu-context we got scheduled on is actually rotating.
2507 perf_pmu_rotate_start(ctx
->pmu
);
2511 * When sampling the branck stack in system-wide, it may be necessary
2512 * to flush the stack on context switch. This happens when the branch
2513 * stack does not tag its entries with the pid of the current task.
2514 * Otherwise it becomes impossible to associate a branch entry with a
2515 * task. This ambiguity is more likely to appear when the branch stack
2516 * supports priv level filtering and the user sets it to monitor only
2517 * at the user level (which could be a useful measurement in system-wide
2518 * mode). In that case, the risk is high of having a branch stack with
2519 * branch from multiple tasks. Flushing may mean dropping the existing
2520 * entries or stashing them somewhere in the PMU specific code layer.
2522 * This function provides the context switch callback to the lower code
2523 * layer. It is invoked ONLY when there is at least one system-wide context
2524 * with at least one active event using taken branch sampling.
2526 static void perf_branch_stack_sched_in(struct task_struct
*prev
,
2527 struct task_struct
*task
)
2529 struct perf_cpu_context
*cpuctx
;
2531 unsigned long flags
;
2533 /* no need to flush branch stack if not changing task */
2537 local_irq_save(flags
);
2541 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
2542 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
2545 * check if the context has at least one
2546 * event using PERF_SAMPLE_BRANCH_STACK
2548 if (cpuctx
->ctx
.nr_branch_stack
> 0
2549 && pmu
->flush_branch_stack
) {
2551 pmu
= cpuctx
->ctx
.pmu
;
2553 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
2555 perf_pmu_disable(pmu
);
2557 pmu
->flush_branch_stack();
2559 perf_pmu_enable(pmu
);
2561 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
2567 local_irq_restore(flags
);
2571 * Called from scheduler to add the events of the current task
2572 * with interrupts disabled.
2574 * We restore the event value and then enable it.
2576 * This does not protect us against NMI, but enable()
2577 * sets the enabled bit in the control field of event _before_
2578 * accessing the event control register. If a NMI hits, then it will
2579 * keep the event running.
2581 void __perf_event_task_sched_in(struct task_struct
*prev
,
2582 struct task_struct
*task
)
2584 struct perf_event_context
*ctx
;
2587 for_each_task_context_nr(ctxn
) {
2588 ctx
= task
->perf_event_ctxp
[ctxn
];
2592 perf_event_context_sched_in(ctx
, task
);
2595 * if cgroup events exist on this CPU, then we need
2596 * to check if we have to switch in PMU state.
2597 * cgroup event are system-wide mode only
2599 if (atomic_read(&__get_cpu_var(perf_cgroup_events
)))
2600 perf_cgroup_sched_in(prev
, task
);
2602 /* check for system-wide branch_stack events */
2603 if (atomic_read(&__get_cpu_var(perf_branch_stack_events
)))
2604 perf_branch_stack_sched_in(prev
, task
);
2607 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
2609 u64 frequency
= event
->attr
.sample_freq
;
2610 u64 sec
= NSEC_PER_SEC
;
2611 u64 divisor
, dividend
;
2613 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
2615 count_fls
= fls64(count
);
2616 nsec_fls
= fls64(nsec
);
2617 frequency_fls
= fls64(frequency
);
2621 * We got @count in @nsec, with a target of sample_freq HZ
2622 * the target period becomes:
2625 * period = -------------------
2626 * @nsec * sample_freq
2631 * Reduce accuracy by one bit such that @a and @b converge
2632 * to a similar magnitude.
2634 #define REDUCE_FLS(a, b) \
2636 if (a##_fls > b##_fls) { \
2646 * Reduce accuracy until either term fits in a u64, then proceed with
2647 * the other, so that finally we can do a u64/u64 division.
2649 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
2650 REDUCE_FLS(nsec
, frequency
);
2651 REDUCE_FLS(sec
, count
);
2654 if (count_fls
+ sec_fls
> 64) {
2655 divisor
= nsec
* frequency
;
2657 while (count_fls
+ sec_fls
> 64) {
2658 REDUCE_FLS(count
, sec
);
2662 dividend
= count
* sec
;
2664 dividend
= count
* sec
;
2666 while (nsec_fls
+ frequency_fls
> 64) {
2667 REDUCE_FLS(nsec
, frequency
);
2671 divisor
= nsec
* frequency
;
2677 return div64_u64(dividend
, divisor
);
2680 static DEFINE_PER_CPU(int, perf_throttled_count
);
2681 static DEFINE_PER_CPU(u64
, perf_throttled_seq
);
2683 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
, bool disable
)
2685 struct hw_perf_event
*hwc
= &event
->hw
;
2686 s64 period
, sample_period
;
2689 period
= perf_calculate_period(event
, nsec
, count
);
2691 delta
= (s64
)(period
- hwc
->sample_period
);
2692 delta
= (delta
+ 7) / 8; /* low pass filter */
2694 sample_period
= hwc
->sample_period
+ delta
;
2699 hwc
->sample_period
= sample_period
;
2701 if (local64_read(&hwc
->period_left
) > 8*sample_period
) {
2703 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
2705 local64_set(&hwc
->period_left
, 0);
2708 event
->pmu
->start(event
, PERF_EF_RELOAD
);
2713 * combine freq adjustment with unthrottling to avoid two passes over the
2714 * events. At the same time, make sure, having freq events does not change
2715 * the rate of unthrottling as that would introduce bias.
2717 static void perf_adjust_freq_unthr_context(struct perf_event_context
*ctx
,
2720 struct perf_event
*event
;
2721 struct hw_perf_event
*hwc
;
2722 u64 now
, period
= TICK_NSEC
;
2726 * only need to iterate over all events iff:
2727 * - context have events in frequency mode (needs freq adjust)
2728 * - there are events to unthrottle on this cpu
2730 if (!(ctx
->nr_freq
|| needs_unthr
))
2733 raw_spin_lock(&ctx
->lock
);
2734 perf_pmu_disable(ctx
->pmu
);
2736 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
2737 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
2740 if (!event_filter_match(event
))
2743 perf_pmu_disable(event
->pmu
);
2747 if (hwc
->interrupts
== MAX_INTERRUPTS
) {
2748 hwc
->interrupts
= 0;
2749 perf_log_throttle(event
, 1);
2750 event
->pmu
->start(event
, 0);
2753 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
2757 * stop the event and update event->count
2759 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
2761 now
= local64_read(&event
->count
);
2762 delta
= now
- hwc
->freq_count_stamp
;
2763 hwc
->freq_count_stamp
= now
;
2767 * reload only if value has changed
2768 * we have stopped the event so tell that
2769 * to perf_adjust_period() to avoid stopping it
2773 perf_adjust_period(event
, period
, delta
, false);
2775 event
->pmu
->start(event
, delta
> 0 ? PERF_EF_RELOAD
: 0);
2777 perf_pmu_enable(event
->pmu
);
2780 perf_pmu_enable(ctx
->pmu
);
2781 raw_spin_unlock(&ctx
->lock
);
2785 * Round-robin a context's events:
2787 static void rotate_ctx(struct perf_event_context
*ctx
)
2790 * Rotate the first entry last of non-pinned groups. Rotation might be
2791 * disabled by the inheritance code.
2793 if (!ctx
->rotate_disable
)
2794 list_rotate_left(&ctx
->flexible_groups
);
2798 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2799 * because they're strictly cpu affine and rotate_start is called with IRQs
2800 * disabled, while rotate_context is called from IRQ context.
2802 static int perf_rotate_context(struct perf_cpu_context
*cpuctx
)
2804 struct perf_event_context
*ctx
= NULL
;
2805 int rotate
= 0, remove
= 1;
2807 if (cpuctx
->ctx
.nr_events
) {
2809 if (cpuctx
->ctx
.nr_events
!= cpuctx
->ctx
.nr_active
)
2813 ctx
= cpuctx
->task_ctx
;
2814 if (ctx
&& ctx
->nr_events
) {
2816 if (ctx
->nr_events
!= ctx
->nr_active
)
2823 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
2824 perf_pmu_disable(cpuctx
->ctx
.pmu
);
2826 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
2828 ctx_sched_out(ctx
, cpuctx
, EVENT_FLEXIBLE
);
2830 rotate_ctx(&cpuctx
->ctx
);
2834 perf_event_sched_in(cpuctx
, ctx
, current
);
2836 perf_pmu_enable(cpuctx
->ctx
.pmu
);
2837 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
2840 list_del_init(&cpuctx
->rotation_list
);
2845 #ifdef CONFIG_NO_HZ_FULL
2846 bool perf_event_can_stop_tick(void)
2848 if (atomic_read(&nr_freq_events
) ||
2849 __this_cpu_read(perf_throttled_count
))
2856 void perf_event_task_tick(void)
2858 struct list_head
*head
= &__get_cpu_var(rotation_list
);
2859 struct perf_cpu_context
*cpuctx
, *tmp
;
2860 struct perf_event_context
*ctx
;
2863 WARN_ON(!irqs_disabled());
2865 __this_cpu_inc(perf_throttled_seq
);
2866 throttled
= __this_cpu_xchg(perf_throttled_count
, 0);
2868 list_for_each_entry_safe(cpuctx
, tmp
, head
, rotation_list
) {
2870 perf_adjust_freq_unthr_context(ctx
, throttled
);
2872 ctx
= cpuctx
->task_ctx
;
2874 perf_adjust_freq_unthr_context(ctx
, throttled
);
2878 static int event_enable_on_exec(struct perf_event
*event
,
2879 struct perf_event_context
*ctx
)
2881 if (!event
->attr
.enable_on_exec
)
2884 event
->attr
.enable_on_exec
= 0;
2885 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
2888 __perf_event_mark_enabled(event
);
2894 * Enable all of a task's events that have been marked enable-on-exec.
2895 * This expects task == current.
2897 static void perf_event_enable_on_exec(struct perf_event_context
*ctx
)
2899 struct perf_event
*event
;
2900 unsigned long flags
;
2904 local_irq_save(flags
);
2905 if (!ctx
|| !ctx
->nr_events
)
2909 * We must ctxsw out cgroup events to avoid conflict
2910 * when invoking perf_task_event_sched_in() later on
2911 * in this function. Otherwise we end up trying to
2912 * ctxswin cgroup events which are already scheduled
2915 perf_cgroup_sched_out(current
, NULL
);
2917 raw_spin_lock(&ctx
->lock
);
2918 task_ctx_sched_out(ctx
);
2920 list_for_each_entry(event
, &ctx
->event_list
, event_entry
) {
2921 ret
= event_enable_on_exec(event
, ctx
);
2927 * Unclone this context if we enabled any event.
2932 raw_spin_unlock(&ctx
->lock
);
2935 * Also calls ctxswin for cgroup events, if any:
2937 perf_event_context_sched_in(ctx
, ctx
->task
);
2939 local_irq_restore(flags
);
2943 * Cross CPU call to read the hardware event
2945 static void __perf_event_read(void *info
)
2947 struct perf_event
*event
= info
;
2948 struct perf_event_context
*ctx
= event
->ctx
;
2949 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
2952 * If this is a task context, we need to check whether it is
2953 * the current task context of this cpu. If not it has been
2954 * scheduled out before the smp call arrived. In that case
2955 * event->count would have been updated to a recent sample
2956 * when the event was scheduled out.
2958 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
2961 raw_spin_lock(&ctx
->lock
);
2962 if (ctx
->is_active
) {
2963 update_context_time(ctx
);
2964 update_cgrp_time_from_event(event
);
2966 update_event_times(event
);
2967 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
2968 event
->pmu
->read(event
);
2969 raw_spin_unlock(&ctx
->lock
);
2972 static inline u64
perf_event_count(struct perf_event
*event
)
2974 return local64_read(&event
->count
) + atomic64_read(&event
->child_count
);
2977 static u64
perf_event_read(struct perf_event
*event
)
2980 * If event is enabled and currently active on a CPU, update the
2981 * value in the event structure:
2983 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
2984 smp_call_function_single(event
->oncpu
,
2985 __perf_event_read
, event
, 1);
2986 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
2987 struct perf_event_context
*ctx
= event
->ctx
;
2988 unsigned long flags
;
2990 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
2992 * may read while context is not active
2993 * (e.g., thread is blocked), in that case
2994 * we cannot update context time
2996 if (ctx
->is_active
) {
2997 update_context_time(ctx
);
2998 update_cgrp_time_from_event(event
);
3000 update_event_times(event
);
3001 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
3004 return perf_event_count(event
);
3008 * Initialize the perf_event context in a task_struct:
3010 static void __perf_event_init_context(struct perf_event_context
*ctx
)
3012 raw_spin_lock_init(&ctx
->lock
);
3013 mutex_init(&ctx
->mutex
);
3014 INIT_LIST_HEAD(&ctx
->pinned_groups
);
3015 INIT_LIST_HEAD(&ctx
->flexible_groups
);
3016 INIT_LIST_HEAD(&ctx
->event_list
);
3017 atomic_set(&ctx
->refcount
, 1);
3020 static struct perf_event_context
*
3021 alloc_perf_context(struct pmu
*pmu
, struct task_struct
*task
)
3023 struct perf_event_context
*ctx
;
3025 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
3029 __perf_event_init_context(ctx
);
3032 get_task_struct(task
);
3039 static struct task_struct
*
3040 find_lively_task_by_vpid(pid_t vpid
)
3042 struct task_struct
*task
;
3049 task
= find_task_by_vpid(vpid
);
3051 get_task_struct(task
);
3055 return ERR_PTR(-ESRCH
);
3057 /* Reuse ptrace permission checks for now. */
3059 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
3064 put_task_struct(task
);
3065 return ERR_PTR(err
);
3070 * Returns a matching context with refcount and pincount.
3072 static struct perf_event_context
*
3073 find_get_context(struct pmu
*pmu
, struct task_struct
*task
, int cpu
)
3075 struct perf_event_context
*ctx
;
3076 struct perf_cpu_context
*cpuctx
;
3077 unsigned long flags
;
3081 /* Must be root to operate on a CPU event: */
3082 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
3083 return ERR_PTR(-EACCES
);
3086 * We could be clever and allow to attach a event to an
3087 * offline CPU and activate it when the CPU comes up, but
3090 if (!cpu_online(cpu
))
3091 return ERR_PTR(-ENODEV
);
3093 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
3102 ctxn
= pmu
->task_ctx_nr
;
3107 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
3111 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
3113 ctx
= alloc_perf_context(pmu
, task
);
3119 mutex_lock(&task
->perf_event_mutex
);
3121 * If it has already passed perf_event_exit_task().
3122 * we must see PF_EXITING, it takes this mutex too.
3124 if (task
->flags
& PF_EXITING
)
3126 else if (task
->perf_event_ctxp
[ctxn
])
3131 rcu_assign_pointer(task
->perf_event_ctxp
[ctxn
], ctx
);
3133 mutex_unlock(&task
->perf_event_mutex
);
3135 if (unlikely(err
)) {
3147 return ERR_PTR(err
);
3150 static void perf_event_free_filter(struct perf_event
*event
);
3152 static void free_event_rcu(struct rcu_head
*head
)
3154 struct perf_event
*event
;
3156 event
= container_of(head
, struct perf_event
, rcu_head
);
3158 put_pid_ns(event
->ns
);
3159 perf_event_free_filter(event
);
3163 static void ring_buffer_put(struct ring_buffer
*rb
);
3164 static void ring_buffer_detach(struct perf_event
*event
, struct ring_buffer
*rb
);
3166 static void unaccount_event_cpu(struct perf_event
*event
, int cpu
)
3171 if (has_branch_stack(event
)) {
3172 if (!(event
->attach_state
& PERF_ATTACH_TASK
))
3173 atomic_dec(&per_cpu(perf_branch_stack_events
, cpu
));
3175 if (is_cgroup_event(event
))
3176 atomic_dec(&per_cpu(perf_cgroup_events
, cpu
));
3179 static void unaccount_event(struct perf_event
*event
)
3184 if (event
->attach_state
& PERF_ATTACH_TASK
)
3185 static_key_slow_dec_deferred(&perf_sched_events
);
3186 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
3187 atomic_dec(&nr_mmap_events
);
3188 if (event
->attr
.comm
)
3189 atomic_dec(&nr_comm_events
);
3190 if (event
->attr
.task
)
3191 atomic_dec(&nr_task_events
);
3192 if (event
->attr
.freq
)
3193 atomic_dec(&nr_freq_events
);
3194 if (is_cgroup_event(event
))
3195 static_key_slow_dec_deferred(&perf_sched_events
);
3196 if (has_branch_stack(event
))
3197 static_key_slow_dec_deferred(&perf_sched_events
);
3199 unaccount_event_cpu(event
, event
->cpu
);
3202 static void __free_event(struct perf_event
*event
)
3204 if (!event
->parent
) {
3205 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
)
3206 put_callchain_buffers();
3210 event
->destroy(event
);
3213 put_ctx(event
->ctx
);
3215 call_rcu(&event
->rcu_head
, free_event_rcu
);
3217 static void free_event(struct perf_event
*event
)
3219 irq_work_sync(&event
->pending
);
3221 unaccount_event(event
);
3224 struct ring_buffer
*rb
;
3227 * Can happen when we close an event with re-directed output.
3229 * Since we have a 0 refcount, perf_mmap_close() will skip
3230 * over us; possibly making our ring_buffer_put() the last.
3232 mutex_lock(&event
->mmap_mutex
);
3235 rcu_assign_pointer(event
->rb
, NULL
);
3236 ring_buffer_detach(event
, rb
);
3237 ring_buffer_put(rb
); /* could be last */
3239 mutex_unlock(&event
->mmap_mutex
);
3242 if (is_cgroup_event(event
))
3243 perf_detach_cgroup(event
);
3246 __free_event(event
);
3249 int perf_event_release_kernel(struct perf_event
*event
)
3251 struct perf_event_context
*ctx
= event
->ctx
;
3253 WARN_ON_ONCE(ctx
->parent_ctx
);
3255 * There are two ways this annotation is useful:
3257 * 1) there is a lock recursion from perf_event_exit_task
3258 * see the comment there.
3260 * 2) there is a lock-inversion with mmap_sem through
3261 * perf_event_read_group(), which takes faults while
3262 * holding ctx->mutex, however this is called after
3263 * the last filedesc died, so there is no possibility
3264 * to trigger the AB-BA case.
3266 mutex_lock_nested(&ctx
->mutex
, SINGLE_DEPTH_NESTING
);
3267 raw_spin_lock_irq(&ctx
->lock
);
3268 perf_group_detach(event
);
3269 raw_spin_unlock_irq(&ctx
->lock
);
3270 perf_remove_from_context(event
);
3271 mutex_unlock(&ctx
->mutex
);
3277 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
3280 * Called when the last reference to the file is gone.
3282 static void put_event(struct perf_event
*event
)
3284 struct task_struct
*owner
;
3286 if (!atomic_long_dec_and_test(&event
->refcount
))
3290 owner
= ACCESS_ONCE(event
->owner
);
3292 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3293 * !owner it means the list deletion is complete and we can indeed
3294 * free this event, otherwise we need to serialize on
3295 * owner->perf_event_mutex.
3297 smp_read_barrier_depends();
3300 * Since delayed_put_task_struct() also drops the last
3301 * task reference we can safely take a new reference
3302 * while holding the rcu_read_lock().
3304 get_task_struct(owner
);
3309 mutex_lock(&owner
->perf_event_mutex
);
3311 * We have to re-check the event->owner field, if it is cleared
3312 * we raced with perf_event_exit_task(), acquiring the mutex
3313 * ensured they're done, and we can proceed with freeing the
3317 list_del_init(&event
->owner_entry
);
3318 mutex_unlock(&owner
->perf_event_mutex
);
3319 put_task_struct(owner
);
3322 perf_event_release_kernel(event
);
3325 static int perf_release(struct inode
*inode
, struct file
*file
)
3327 put_event(file
->private_data
);
3331 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
3333 struct perf_event
*child
;
3339 mutex_lock(&event
->child_mutex
);
3340 total
+= perf_event_read(event
);
3341 *enabled
+= event
->total_time_enabled
+
3342 atomic64_read(&event
->child_total_time_enabled
);
3343 *running
+= event
->total_time_running
+
3344 atomic64_read(&event
->child_total_time_running
);
3346 list_for_each_entry(child
, &event
->child_list
, child_list
) {
3347 total
+= perf_event_read(child
);
3348 *enabled
+= child
->total_time_enabled
;
3349 *running
+= child
->total_time_running
;
3351 mutex_unlock(&event
->child_mutex
);
3355 EXPORT_SYMBOL_GPL(perf_event_read_value
);
3357 static int perf_event_read_group(struct perf_event
*event
,
3358 u64 read_format
, char __user
*buf
)
3360 struct perf_event
*leader
= event
->group_leader
, *sub
;
3361 int n
= 0, size
= 0, ret
= -EFAULT
;
3362 struct perf_event_context
*ctx
= leader
->ctx
;
3364 u64 count
, enabled
, running
;
3366 mutex_lock(&ctx
->mutex
);
3367 count
= perf_event_read_value(leader
, &enabled
, &running
);
3369 values
[n
++] = 1 + leader
->nr_siblings
;
3370 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3371 values
[n
++] = enabled
;
3372 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3373 values
[n
++] = running
;
3374 values
[n
++] = count
;
3375 if (read_format
& PERF_FORMAT_ID
)
3376 values
[n
++] = primary_event_id(leader
);
3378 size
= n
* sizeof(u64
);
3380 if (copy_to_user(buf
, values
, size
))
3385 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3388 values
[n
++] = perf_event_read_value(sub
, &enabled
, &running
);
3389 if (read_format
& PERF_FORMAT_ID
)
3390 values
[n
++] = primary_event_id(sub
);
3392 size
= n
* sizeof(u64
);
3394 if (copy_to_user(buf
+ ret
, values
, size
)) {
3402 mutex_unlock(&ctx
->mutex
);
3407 static int perf_event_read_one(struct perf_event
*event
,
3408 u64 read_format
, char __user
*buf
)
3410 u64 enabled
, running
;
3414 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
3415 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3416 values
[n
++] = enabled
;
3417 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3418 values
[n
++] = running
;
3419 if (read_format
& PERF_FORMAT_ID
)
3420 values
[n
++] = primary_event_id(event
);
3422 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
3425 return n
* sizeof(u64
);
3429 * Read the performance event - simple non blocking version for now
3432 perf_read_hw(struct perf_event
*event
, char __user
*buf
, size_t count
)
3434 u64 read_format
= event
->attr
.read_format
;
3438 * Return end-of-file for a read on a event that is in
3439 * error state (i.e. because it was pinned but it couldn't be
3440 * scheduled on to the CPU at some point).
3442 if (event
->state
== PERF_EVENT_STATE_ERROR
)
3445 if (count
< event
->read_size
)
3448 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3449 if (read_format
& PERF_FORMAT_GROUP
)
3450 ret
= perf_event_read_group(event
, read_format
, buf
);
3452 ret
= perf_event_read_one(event
, read_format
, buf
);
3458 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
3460 struct perf_event
*event
= file
->private_data
;
3462 return perf_read_hw(event
, buf
, count
);
3465 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
3467 struct perf_event
*event
= file
->private_data
;
3468 struct ring_buffer
*rb
;
3469 unsigned int events
= POLL_HUP
;
3472 * Pin the event->rb by taking event->mmap_mutex; otherwise
3473 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
3475 mutex_lock(&event
->mmap_mutex
);
3478 events
= atomic_xchg(&rb
->poll
, 0);
3479 mutex_unlock(&event
->mmap_mutex
);
3481 poll_wait(file
, &event
->waitq
, wait
);
3486 static void perf_event_reset(struct perf_event
*event
)
3488 (void)perf_event_read(event
);
3489 local64_set(&event
->count
, 0);
3490 perf_event_update_userpage(event
);
3494 * Holding the top-level event's child_mutex means that any
3495 * descendant process that has inherited this event will block
3496 * in sync_child_event if it goes to exit, thus satisfying the
3497 * task existence requirements of perf_event_enable/disable.
3499 static void perf_event_for_each_child(struct perf_event
*event
,
3500 void (*func
)(struct perf_event
*))
3502 struct perf_event
*child
;
3504 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3505 mutex_lock(&event
->child_mutex
);
3507 list_for_each_entry(child
, &event
->child_list
, child_list
)
3509 mutex_unlock(&event
->child_mutex
);
3512 static void perf_event_for_each(struct perf_event
*event
,
3513 void (*func
)(struct perf_event
*))
3515 struct perf_event_context
*ctx
= event
->ctx
;
3516 struct perf_event
*sibling
;
3518 WARN_ON_ONCE(ctx
->parent_ctx
);
3519 mutex_lock(&ctx
->mutex
);
3520 event
= event
->group_leader
;
3522 perf_event_for_each_child(event
, func
);
3523 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
3524 perf_event_for_each_child(sibling
, func
);
3525 mutex_unlock(&ctx
->mutex
);
3528 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
3530 struct perf_event_context
*ctx
= event
->ctx
;
3531 int ret
= 0, active
;
3534 if (!is_sampling_event(event
))
3537 if (copy_from_user(&value
, arg
, sizeof(value
)))
3543 raw_spin_lock_irq(&ctx
->lock
);
3544 if (event
->attr
.freq
) {
3545 if (value
> sysctl_perf_event_sample_rate
) {
3550 event
->attr
.sample_freq
= value
;
3552 event
->attr
.sample_period
= value
;
3553 event
->hw
.sample_period
= value
;
3556 active
= (event
->state
== PERF_EVENT_STATE_ACTIVE
);
3558 perf_pmu_disable(ctx
->pmu
);
3559 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
3562 local64_set(&event
->hw
.period_left
, 0);
3565 event
->pmu
->start(event
, PERF_EF_RELOAD
);
3566 perf_pmu_enable(ctx
->pmu
);
3570 raw_spin_unlock_irq(&ctx
->lock
);
3575 static const struct file_operations perf_fops
;
3577 static inline int perf_fget_light(int fd
, struct fd
*p
)
3579 struct fd f
= fdget(fd
);
3583 if (f
.file
->f_op
!= &perf_fops
) {
3591 static int perf_event_set_output(struct perf_event
*event
,
3592 struct perf_event
*output_event
);
3593 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
3595 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
3597 struct perf_event
*event
= file
->private_data
;
3598 void (*func
)(struct perf_event
*);
3602 case PERF_EVENT_IOC_ENABLE
:
3603 func
= perf_event_enable
;
3605 case PERF_EVENT_IOC_DISABLE
:
3606 func
= perf_event_disable
;
3608 case PERF_EVENT_IOC_RESET
:
3609 func
= perf_event_reset
;
3612 case PERF_EVENT_IOC_REFRESH
:
3613 return perf_event_refresh(event
, arg
);
3615 case PERF_EVENT_IOC_PERIOD
:
3616 return perf_event_period(event
, (u64 __user
*)arg
);
3618 case PERF_EVENT_IOC_ID
:
3620 u64 id
= primary_event_id(event
);
3622 if (copy_to_user((void __user
*)arg
, &id
, sizeof(id
)))
3627 case PERF_EVENT_IOC_SET_OUTPUT
:
3631 struct perf_event
*output_event
;
3633 ret
= perf_fget_light(arg
, &output
);
3636 output_event
= output
.file
->private_data
;
3637 ret
= perf_event_set_output(event
, output_event
);
3640 ret
= perf_event_set_output(event
, NULL
);
3645 case PERF_EVENT_IOC_SET_FILTER
:
3646 return perf_event_set_filter(event
, (void __user
*)arg
);
3652 if (flags
& PERF_IOC_FLAG_GROUP
)
3653 perf_event_for_each(event
, func
);
3655 perf_event_for_each_child(event
, func
);
3660 int perf_event_task_enable(void)
3662 struct perf_event
*event
;
3664 mutex_lock(¤t
->perf_event_mutex
);
3665 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
3666 perf_event_for_each_child(event
, perf_event_enable
);
3667 mutex_unlock(¤t
->perf_event_mutex
);
3672 int perf_event_task_disable(void)
3674 struct perf_event
*event
;
3676 mutex_lock(¤t
->perf_event_mutex
);
3677 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
3678 perf_event_for_each_child(event
, perf_event_disable
);
3679 mutex_unlock(¤t
->perf_event_mutex
);
3684 static int perf_event_index(struct perf_event
*event
)
3686 if (event
->hw
.state
& PERF_HES_STOPPED
)
3689 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
3692 return event
->pmu
->event_idx(event
);
3695 static void calc_timer_values(struct perf_event
*event
,
3702 *now
= perf_clock();
3703 ctx_time
= event
->shadow_ctx_time
+ *now
;
3704 *enabled
= ctx_time
- event
->tstamp_enabled
;
3705 *running
= ctx_time
- event
->tstamp_running
;
3708 static void perf_event_init_userpage(struct perf_event
*event
)
3710 struct perf_event_mmap_page
*userpg
;
3711 struct ring_buffer
*rb
;
3714 rb
= rcu_dereference(event
->rb
);
3718 userpg
= rb
->user_page
;
3720 /* Allow new userspace to detect that bit 0 is deprecated */
3721 userpg
->cap_bit0_is_deprecated
= 1;
3722 userpg
->size
= offsetof(struct perf_event_mmap_page
, __reserved
);
3728 void __weak
arch_perf_update_userpage(struct perf_event_mmap_page
*userpg
, u64 now
)
3733 * Callers need to ensure there can be no nesting of this function, otherwise
3734 * the seqlock logic goes bad. We can not serialize this because the arch
3735 * code calls this from NMI context.
3737 void perf_event_update_userpage(struct perf_event
*event
)
3739 struct perf_event_mmap_page
*userpg
;
3740 struct ring_buffer
*rb
;
3741 u64 enabled
, running
, now
;
3744 rb
= rcu_dereference(event
->rb
);
3749 * compute total_time_enabled, total_time_running
3750 * based on snapshot values taken when the event
3751 * was last scheduled in.
3753 * we cannot simply called update_context_time()
3754 * because of locking issue as we can be called in
3757 calc_timer_values(event
, &now
, &enabled
, &running
);
3759 userpg
= rb
->user_page
;
3761 * Disable preemption so as to not let the corresponding user-space
3762 * spin too long if we get preempted.
3767 userpg
->index
= perf_event_index(event
);
3768 userpg
->offset
= perf_event_count(event
);
3770 userpg
->offset
-= local64_read(&event
->hw
.prev_count
);
3772 userpg
->time_enabled
= enabled
+
3773 atomic64_read(&event
->child_total_time_enabled
);
3775 userpg
->time_running
= running
+
3776 atomic64_read(&event
->child_total_time_running
);
3778 arch_perf_update_userpage(userpg
, now
);
3787 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
3789 struct perf_event
*event
= vma
->vm_file
->private_data
;
3790 struct ring_buffer
*rb
;
3791 int ret
= VM_FAULT_SIGBUS
;
3793 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
3794 if (vmf
->pgoff
== 0)
3800 rb
= rcu_dereference(event
->rb
);
3804 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
3807 vmf
->page
= perf_mmap_to_page(rb
, vmf
->pgoff
);
3811 get_page(vmf
->page
);
3812 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
3813 vmf
->page
->index
= vmf
->pgoff
;
3822 static void ring_buffer_attach(struct perf_event
*event
,
3823 struct ring_buffer
*rb
)
3825 unsigned long flags
;
3827 if (!list_empty(&event
->rb_entry
))
3830 spin_lock_irqsave(&rb
->event_lock
, flags
);
3831 if (list_empty(&event
->rb_entry
))
3832 list_add(&event
->rb_entry
, &rb
->event_list
);
3833 spin_unlock_irqrestore(&rb
->event_lock
, flags
);
3836 static void ring_buffer_detach(struct perf_event
*event
, struct ring_buffer
*rb
)
3838 unsigned long flags
;
3840 if (list_empty(&event
->rb_entry
))
3843 spin_lock_irqsave(&rb
->event_lock
, flags
);
3844 list_del_init(&event
->rb_entry
);
3845 wake_up_all(&event
->waitq
);
3846 spin_unlock_irqrestore(&rb
->event_lock
, flags
);
3849 static void ring_buffer_wakeup(struct perf_event
*event
)
3851 struct ring_buffer
*rb
;
3854 rb
= rcu_dereference(event
->rb
);
3856 list_for_each_entry_rcu(event
, &rb
->event_list
, rb_entry
)
3857 wake_up_all(&event
->waitq
);
3862 static void rb_free_rcu(struct rcu_head
*rcu_head
)
3864 struct ring_buffer
*rb
;
3866 rb
= container_of(rcu_head
, struct ring_buffer
, rcu_head
);
3870 static struct ring_buffer
*ring_buffer_get(struct perf_event
*event
)
3872 struct ring_buffer
*rb
;
3875 rb
= rcu_dereference(event
->rb
);
3877 if (!atomic_inc_not_zero(&rb
->refcount
))
3885 static void ring_buffer_put(struct ring_buffer
*rb
)
3887 if (!atomic_dec_and_test(&rb
->refcount
))
3890 WARN_ON_ONCE(!list_empty(&rb
->event_list
));
3892 call_rcu(&rb
->rcu_head
, rb_free_rcu
);
3895 static void perf_mmap_open(struct vm_area_struct
*vma
)
3897 struct perf_event
*event
= vma
->vm_file
->private_data
;
3899 atomic_inc(&event
->mmap_count
);
3900 atomic_inc(&event
->rb
->mmap_count
);
3904 * A buffer can be mmap()ed multiple times; either directly through the same
3905 * event, or through other events by use of perf_event_set_output().
3907 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3908 * the buffer here, where we still have a VM context. This means we need
3909 * to detach all events redirecting to us.
3911 static void perf_mmap_close(struct vm_area_struct
*vma
)
3913 struct perf_event
*event
= vma
->vm_file
->private_data
;
3915 struct ring_buffer
*rb
= event
->rb
;
3916 struct user_struct
*mmap_user
= rb
->mmap_user
;
3917 int mmap_locked
= rb
->mmap_locked
;
3918 unsigned long size
= perf_data_size(rb
);
3920 atomic_dec(&rb
->mmap_count
);
3922 if (!atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
))
3925 /* Detach current event from the buffer. */
3926 rcu_assign_pointer(event
->rb
, NULL
);
3927 ring_buffer_detach(event
, rb
);
3928 mutex_unlock(&event
->mmap_mutex
);
3930 /* If there's still other mmap()s of this buffer, we're done. */
3931 if (atomic_read(&rb
->mmap_count
)) {
3932 ring_buffer_put(rb
); /* can't be last */
3937 * No other mmap()s, detach from all other events that might redirect
3938 * into the now unreachable buffer. Somewhat complicated by the
3939 * fact that rb::event_lock otherwise nests inside mmap_mutex.
3943 list_for_each_entry_rcu(event
, &rb
->event_list
, rb_entry
) {
3944 if (!atomic_long_inc_not_zero(&event
->refcount
)) {
3946 * This event is en-route to free_event() which will
3947 * detach it and remove it from the list.
3953 mutex_lock(&event
->mmap_mutex
);
3955 * Check we didn't race with perf_event_set_output() which can
3956 * swizzle the rb from under us while we were waiting to
3957 * acquire mmap_mutex.
3959 * If we find a different rb; ignore this event, a next
3960 * iteration will no longer find it on the list. We have to
3961 * still restart the iteration to make sure we're not now
3962 * iterating the wrong list.
3964 if (event
->rb
== rb
) {
3965 rcu_assign_pointer(event
->rb
, NULL
);
3966 ring_buffer_detach(event
, rb
);
3967 ring_buffer_put(rb
); /* can't be last, we still have one */
3969 mutex_unlock(&event
->mmap_mutex
);
3973 * Restart the iteration; either we're on the wrong list or
3974 * destroyed its integrity by doing a deletion.
3981 * It could be there's still a few 0-ref events on the list; they'll
3982 * get cleaned up by free_event() -- they'll also still have their
3983 * ref on the rb and will free it whenever they are done with it.
3985 * Aside from that, this buffer is 'fully' detached and unmapped,
3986 * undo the VM accounting.
3989 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &mmap_user
->locked_vm
);
3990 vma
->vm_mm
->pinned_vm
-= mmap_locked
;
3991 free_uid(mmap_user
);
3993 ring_buffer_put(rb
); /* could be last */
3996 static const struct vm_operations_struct perf_mmap_vmops
= {
3997 .open
= perf_mmap_open
,
3998 .close
= perf_mmap_close
,
3999 .fault
= perf_mmap_fault
,
4000 .page_mkwrite
= perf_mmap_fault
,
4003 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
4005 struct perf_event
*event
= file
->private_data
;
4006 unsigned long user_locked
, user_lock_limit
;
4007 struct user_struct
*user
= current_user();
4008 unsigned long locked
, lock_limit
;
4009 struct ring_buffer
*rb
;
4010 unsigned long vma_size
;
4011 unsigned long nr_pages
;
4012 long user_extra
, extra
;
4013 int ret
= 0, flags
= 0;
4016 * Don't allow mmap() of inherited per-task counters. This would
4017 * create a performance issue due to all children writing to the
4020 if (event
->cpu
== -1 && event
->attr
.inherit
)
4023 if (!(vma
->vm_flags
& VM_SHARED
))
4026 vma_size
= vma
->vm_end
- vma
->vm_start
;
4027 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
4030 * If we have rb pages ensure they're a power-of-two number, so we
4031 * can do bitmasks instead of modulo.
4033 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
4036 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
4039 if (vma
->vm_pgoff
!= 0)
4042 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
4044 mutex_lock(&event
->mmap_mutex
);
4046 if (event
->rb
->nr_pages
!= nr_pages
) {
4051 if (!atomic_inc_not_zero(&event
->rb
->mmap_count
)) {
4053 * Raced against perf_mmap_close() through
4054 * perf_event_set_output(). Try again, hope for better
4057 mutex_unlock(&event
->mmap_mutex
);
4064 user_extra
= nr_pages
+ 1;
4065 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
4068 * Increase the limit linearly with more CPUs:
4070 user_lock_limit
*= num_online_cpus();
4072 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
4075 if (user_locked
> user_lock_limit
)
4076 extra
= user_locked
- user_lock_limit
;
4078 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
4079 lock_limit
>>= PAGE_SHIFT
;
4080 locked
= vma
->vm_mm
->pinned_vm
+ extra
;
4082 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
4083 !capable(CAP_IPC_LOCK
)) {
4090 if (vma
->vm_flags
& VM_WRITE
)
4091 flags
|= RING_BUFFER_WRITABLE
;
4093 rb
= rb_alloc(nr_pages
,
4094 event
->attr
.watermark
? event
->attr
.wakeup_watermark
: 0,
4102 atomic_set(&rb
->mmap_count
, 1);
4103 rb
->mmap_locked
= extra
;
4104 rb
->mmap_user
= get_current_user();
4106 atomic_long_add(user_extra
, &user
->locked_vm
);
4107 vma
->vm_mm
->pinned_vm
+= extra
;
4109 ring_buffer_attach(event
, rb
);
4110 rcu_assign_pointer(event
->rb
, rb
);
4112 perf_event_init_userpage(event
);
4113 perf_event_update_userpage(event
);
4117 atomic_inc(&event
->mmap_count
);
4118 mutex_unlock(&event
->mmap_mutex
);
4121 * Since pinned accounting is per vm we cannot allow fork() to copy our
4124 vma
->vm_flags
|= VM_DONTCOPY
| VM_DONTEXPAND
| VM_DONTDUMP
;
4125 vma
->vm_ops
= &perf_mmap_vmops
;
4130 static int perf_fasync(int fd
, struct file
*filp
, int on
)
4132 struct inode
*inode
= file_inode(filp
);
4133 struct perf_event
*event
= filp
->private_data
;
4136 mutex_lock(&inode
->i_mutex
);
4137 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
4138 mutex_unlock(&inode
->i_mutex
);
4146 static const struct file_operations perf_fops
= {
4147 .llseek
= no_llseek
,
4148 .release
= perf_release
,
4151 .unlocked_ioctl
= perf_ioctl
,
4152 .compat_ioctl
= perf_ioctl
,
4154 .fasync
= perf_fasync
,
4160 * If there's data, ensure we set the poll() state and publish everything
4161 * to user-space before waking everybody up.
4164 void perf_event_wakeup(struct perf_event
*event
)
4166 ring_buffer_wakeup(event
);
4168 if (event
->pending_kill
) {
4169 kill_fasync(&event
->fasync
, SIGIO
, event
->pending_kill
);
4170 event
->pending_kill
= 0;
4174 static void perf_pending_event(struct irq_work
*entry
)
4176 struct perf_event
*event
= container_of(entry
,
4177 struct perf_event
, pending
);
4179 if (event
->pending_disable
) {
4180 event
->pending_disable
= 0;
4181 __perf_event_disable(event
);
4184 if (event
->pending_wakeup
) {
4185 event
->pending_wakeup
= 0;
4186 perf_event_wakeup(event
);
4191 * We assume there is only KVM supporting the callbacks.
4192 * Later on, we might change it to a list if there is
4193 * another virtualization implementation supporting the callbacks.
4195 struct perf_guest_info_callbacks
*perf_guest_cbs
;
4197 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
4199 perf_guest_cbs
= cbs
;
4202 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks
);
4204 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
4206 perf_guest_cbs
= NULL
;
4209 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks
);
4212 perf_output_sample_regs(struct perf_output_handle
*handle
,
4213 struct pt_regs
*regs
, u64 mask
)
4217 for_each_set_bit(bit
, (const unsigned long *) &mask
,
4218 sizeof(mask
) * BITS_PER_BYTE
) {
4221 val
= perf_reg_value(regs
, bit
);
4222 perf_output_put(handle
, val
);
4226 static void perf_sample_regs_user(struct perf_regs_user
*regs_user
,
4227 struct pt_regs
*regs
)
4229 if (!user_mode(regs
)) {
4231 regs
= task_pt_regs(current
);
4237 regs_user
->regs
= regs
;
4238 regs_user
->abi
= perf_reg_abi(current
);
4243 * Get remaining task size from user stack pointer.
4245 * It'd be better to take stack vma map and limit this more
4246 * precisly, but there's no way to get it safely under interrupt,
4247 * so using TASK_SIZE as limit.
4249 static u64
perf_ustack_task_size(struct pt_regs
*regs
)
4251 unsigned long addr
= perf_user_stack_pointer(regs
);
4253 if (!addr
|| addr
>= TASK_SIZE
)
4256 return TASK_SIZE
- addr
;
4260 perf_sample_ustack_size(u16 stack_size
, u16 header_size
,
4261 struct pt_regs
*regs
)
4265 /* No regs, no stack pointer, no dump. */
4270 * Check if we fit in with the requested stack size into the:
4272 * If we don't, we limit the size to the TASK_SIZE.
4274 * - remaining sample size
4275 * If we don't, we customize the stack size to
4276 * fit in to the remaining sample size.
4279 task_size
= min((u64
) USHRT_MAX
, perf_ustack_task_size(regs
));
4280 stack_size
= min(stack_size
, (u16
) task_size
);
4282 /* Current header size plus static size and dynamic size. */
4283 header_size
+= 2 * sizeof(u64
);
4285 /* Do we fit in with the current stack dump size? */
4286 if ((u16
) (header_size
+ stack_size
) < header_size
) {
4288 * If we overflow the maximum size for the sample,
4289 * we customize the stack dump size to fit in.
4291 stack_size
= USHRT_MAX
- header_size
- sizeof(u64
);
4292 stack_size
= round_up(stack_size
, sizeof(u64
));
4299 perf_output_sample_ustack(struct perf_output_handle
*handle
, u64 dump_size
,
4300 struct pt_regs
*regs
)
4302 /* Case of a kernel thread, nothing to dump */
4305 perf_output_put(handle
, size
);
4314 * - the size requested by user or the best one we can fit
4315 * in to the sample max size
4317 * - user stack dump data
4319 * - the actual dumped size
4323 perf_output_put(handle
, dump_size
);
4326 sp
= perf_user_stack_pointer(regs
);
4327 rem
= __output_copy_user(handle
, (void *) sp
, dump_size
);
4328 dyn_size
= dump_size
- rem
;
4330 perf_output_skip(handle
, rem
);
4333 perf_output_put(handle
, dyn_size
);
4337 static void __perf_event_header__init_id(struct perf_event_header
*header
,
4338 struct perf_sample_data
*data
,
4339 struct perf_event
*event
)
4341 u64 sample_type
= event
->attr
.sample_type
;
4343 data
->type
= sample_type
;
4344 header
->size
+= event
->id_header_size
;
4346 if (sample_type
& PERF_SAMPLE_TID
) {
4347 /* namespace issues */
4348 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
4349 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
4352 if (sample_type
& PERF_SAMPLE_TIME
)
4353 data
->time
= perf_clock();
4355 if (sample_type
& (PERF_SAMPLE_ID
| PERF_SAMPLE_IDENTIFIER
))
4356 data
->id
= primary_event_id(event
);
4358 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4359 data
->stream_id
= event
->id
;
4361 if (sample_type
& PERF_SAMPLE_CPU
) {
4362 data
->cpu_entry
.cpu
= raw_smp_processor_id();
4363 data
->cpu_entry
.reserved
= 0;
4367 void perf_event_header__init_id(struct perf_event_header
*header
,
4368 struct perf_sample_data
*data
,
4369 struct perf_event
*event
)
4371 if (event
->attr
.sample_id_all
)
4372 __perf_event_header__init_id(header
, data
, event
);
4375 static void __perf_event__output_id_sample(struct perf_output_handle
*handle
,
4376 struct perf_sample_data
*data
)
4378 u64 sample_type
= data
->type
;
4380 if (sample_type
& PERF_SAMPLE_TID
)
4381 perf_output_put(handle
, data
->tid_entry
);
4383 if (sample_type
& PERF_SAMPLE_TIME
)
4384 perf_output_put(handle
, data
->time
);
4386 if (sample_type
& PERF_SAMPLE_ID
)
4387 perf_output_put(handle
, data
->id
);
4389 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4390 perf_output_put(handle
, data
->stream_id
);
4392 if (sample_type
& PERF_SAMPLE_CPU
)
4393 perf_output_put(handle
, data
->cpu_entry
);
4395 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
4396 perf_output_put(handle
, data
->id
);
4399 void perf_event__output_id_sample(struct perf_event
*event
,
4400 struct perf_output_handle
*handle
,
4401 struct perf_sample_data
*sample
)
4403 if (event
->attr
.sample_id_all
)
4404 __perf_event__output_id_sample(handle
, sample
);
4407 static void perf_output_read_one(struct perf_output_handle
*handle
,
4408 struct perf_event
*event
,
4409 u64 enabled
, u64 running
)
4411 u64 read_format
= event
->attr
.read_format
;
4415 values
[n
++] = perf_event_count(event
);
4416 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
4417 values
[n
++] = enabled
+
4418 atomic64_read(&event
->child_total_time_enabled
);
4420 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
4421 values
[n
++] = running
+
4422 atomic64_read(&event
->child_total_time_running
);
4424 if (read_format
& PERF_FORMAT_ID
)
4425 values
[n
++] = primary_event_id(event
);
4427 __output_copy(handle
, values
, n
* sizeof(u64
));
4431 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4433 static void perf_output_read_group(struct perf_output_handle
*handle
,
4434 struct perf_event
*event
,
4435 u64 enabled
, u64 running
)
4437 struct perf_event
*leader
= event
->group_leader
, *sub
;
4438 u64 read_format
= event
->attr
.read_format
;
4442 values
[n
++] = 1 + leader
->nr_siblings
;
4444 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
4445 values
[n
++] = enabled
;
4447 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
4448 values
[n
++] = running
;
4450 if (leader
!= event
)
4451 leader
->pmu
->read(leader
);
4453 values
[n
++] = perf_event_count(leader
);
4454 if (read_format
& PERF_FORMAT_ID
)
4455 values
[n
++] = primary_event_id(leader
);
4457 __output_copy(handle
, values
, n
* sizeof(u64
));
4459 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
4462 if ((sub
!= event
) &&
4463 (sub
->state
== PERF_EVENT_STATE_ACTIVE
))
4464 sub
->pmu
->read(sub
);
4466 values
[n
++] = perf_event_count(sub
);
4467 if (read_format
& PERF_FORMAT_ID
)
4468 values
[n
++] = primary_event_id(sub
);
4470 __output_copy(handle
, values
, n
* sizeof(u64
));
4474 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4475 PERF_FORMAT_TOTAL_TIME_RUNNING)
4477 static void perf_output_read(struct perf_output_handle
*handle
,
4478 struct perf_event
*event
)
4480 u64 enabled
= 0, running
= 0, now
;
4481 u64 read_format
= event
->attr
.read_format
;
4484 * compute total_time_enabled, total_time_running
4485 * based on snapshot values taken when the event
4486 * was last scheduled in.
4488 * we cannot simply called update_context_time()
4489 * because of locking issue as we are called in
4492 if (read_format
& PERF_FORMAT_TOTAL_TIMES
)
4493 calc_timer_values(event
, &now
, &enabled
, &running
);
4495 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
4496 perf_output_read_group(handle
, event
, enabled
, running
);
4498 perf_output_read_one(handle
, event
, enabled
, running
);
4501 void perf_output_sample(struct perf_output_handle
*handle
,
4502 struct perf_event_header
*header
,
4503 struct perf_sample_data
*data
,
4504 struct perf_event
*event
)
4506 u64 sample_type
= data
->type
;
4508 perf_output_put(handle
, *header
);
4510 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
4511 perf_output_put(handle
, data
->id
);
4513 if (sample_type
& PERF_SAMPLE_IP
)
4514 perf_output_put(handle
, data
->ip
);
4516 if (sample_type
& PERF_SAMPLE_TID
)
4517 perf_output_put(handle
, data
->tid_entry
);
4519 if (sample_type
& PERF_SAMPLE_TIME
)
4520 perf_output_put(handle
, data
->time
);
4522 if (sample_type
& PERF_SAMPLE_ADDR
)
4523 perf_output_put(handle
, data
->addr
);
4525 if (sample_type
& PERF_SAMPLE_ID
)
4526 perf_output_put(handle
, data
->id
);
4528 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4529 perf_output_put(handle
, data
->stream_id
);
4531 if (sample_type
& PERF_SAMPLE_CPU
)
4532 perf_output_put(handle
, data
->cpu_entry
);
4534 if (sample_type
& PERF_SAMPLE_PERIOD
)
4535 perf_output_put(handle
, data
->period
);
4537 if (sample_type
& PERF_SAMPLE_READ
)
4538 perf_output_read(handle
, event
);
4540 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
4541 if (data
->callchain
) {
4544 if (data
->callchain
)
4545 size
+= data
->callchain
->nr
;
4547 size
*= sizeof(u64
);
4549 __output_copy(handle
, data
->callchain
, size
);
4552 perf_output_put(handle
, nr
);
4556 if (sample_type
& PERF_SAMPLE_RAW
) {
4558 perf_output_put(handle
, data
->raw
->size
);
4559 __output_copy(handle
, data
->raw
->data
,
4566 .size
= sizeof(u32
),
4569 perf_output_put(handle
, raw
);
4573 if (sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
4574 if (data
->br_stack
) {
4577 size
= data
->br_stack
->nr
4578 * sizeof(struct perf_branch_entry
);
4580 perf_output_put(handle
, data
->br_stack
->nr
);
4581 perf_output_copy(handle
, data
->br_stack
->entries
, size
);
4584 * we always store at least the value of nr
4587 perf_output_put(handle
, nr
);
4591 if (sample_type
& PERF_SAMPLE_REGS_USER
) {
4592 u64 abi
= data
->regs_user
.abi
;
4595 * If there are no regs to dump, notice it through
4596 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4598 perf_output_put(handle
, abi
);
4601 u64 mask
= event
->attr
.sample_regs_user
;
4602 perf_output_sample_regs(handle
,
4603 data
->regs_user
.regs
,
4608 if (sample_type
& PERF_SAMPLE_STACK_USER
) {
4609 perf_output_sample_ustack(handle
,
4610 data
->stack_user_size
,
4611 data
->regs_user
.regs
);
4614 if (sample_type
& PERF_SAMPLE_WEIGHT
)
4615 perf_output_put(handle
, data
->weight
);
4617 if (sample_type
& PERF_SAMPLE_DATA_SRC
)
4618 perf_output_put(handle
, data
->data_src
.val
);
4620 if (sample_type
& PERF_SAMPLE_TRANSACTION
)
4621 perf_output_put(handle
, data
->txn
);
4623 if (!event
->attr
.watermark
) {
4624 int wakeup_events
= event
->attr
.wakeup_events
;
4626 if (wakeup_events
) {
4627 struct ring_buffer
*rb
= handle
->rb
;
4628 int events
= local_inc_return(&rb
->events
);
4630 if (events
>= wakeup_events
) {
4631 local_sub(wakeup_events
, &rb
->events
);
4632 local_inc(&rb
->wakeup
);
4638 void perf_prepare_sample(struct perf_event_header
*header
,
4639 struct perf_sample_data
*data
,
4640 struct perf_event
*event
,
4641 struct pt_regs
*regs
)
4643 u64 sample_type
= event
->attr
.sample_type
;
4645 header
->type
= PERF_RECORD_SAMPLE
;
4646 header
->size
= sizeof(*header
) + event
->header_size
;
4649 header
->misc
|= perf_misc_flags(regs
);
4651 __perf_event_header__init_id(header
, data
, event
);
4653 if (sample_type
& PERF_SAMPLE_IP
)
4654 data
->ip
= perf_instruction_pointer(regs
);
4656 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
4659 data
->callchain
= perf_callchain(event
, regs
);
4661 if (data
->callchain
)
4662 size
+= data
->callchain
->nr
;
4664 header
->size
+= size
* sizeof(u64
);
4667 if (sample_type
& PERF_SAMPLE_RAW
) {
4668 int size
= sizeof(u32
);
4671 size
+= data
->raw
->size
;
4673 size
+= sizeof(u32
);
4675 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
4676 header
->size
+= size
;
4679 if (sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
4680 int size
= sizeof(u64
); /* nr */
4681 if (data
->br_stack
) {
4682 size
+= data
->br_stack
->nr
4683 * sizeof(struct perf_branch_entry
);
4685 header
->size
+= size
;
4688 if (sample_type
& PERF_SAMPLE_REGS_USER
) {
4689 /* regs dump ABI info */
4690 int size
= sizeof(u64
);
4692 perf_sample_regs_user(&data
->regs_user
, regs
);
4694 if (data
->regs_user
.regs
) {
4695 u64 mask
= event
->attr
.sample_regs_user
;
4696 size
+= hweight64(mask
) * sizeof(u64
);
4699 header
->size
+= size
;
4702 if (sample_type
& PERF_SAMPLE_STACK_USER
) {
4704 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4705 * processed as the last one or have additional check added
4706 * in case new sample type is added, because we could eat
4707 * up the rest of the sample size.
4709 struct perf_regs_user
*uregs
= &data
->regs_user
;
4710 u16 stack_size
= event
->attr
.sample_stack_user
;
4711 u16 size
= sizeof(u64
);
4714 perf_sample_regs_user(uregs
, regs
);
4716 stack_size
= perf_sample_ustack_size(stack_size
, header
->size
,
4720 * If there is something to dump, add space for the dump
4721 * itself and for the field that tells the dynamic size,
4722 * which is how many have been actually dumped.
4725 size
+= sizeof(u64
) + stack_size
;
4727 data
->stack_user_size
= stack_size
;
4728 header
->size
+= size
;
4732 static void perf_event_output(struct perf_event
*event
,
4733 struct perf_sample_data
*data
,
4734 struct pt_regs
*regs
)
4736 struct perf_output_handle handle
;
4737 struct perf_event_header header
;
4739 /* protect the callchain buffers */
4742 perf_prepare_sample(&header
, data
, event
, regs
);
4744 if (perf_output_begin(&handle
, event
, header
.size
))
4747 perf_output_sample(&handle
, &header
, data
, event
);
4749 perf_output_end(&handle
);
4759 struct perf_read_event
{
4760 struct perf_event_header header
;
4767 perf_event_read_event(struct perf_event
*event
,
4768 struct task_struct
*task
)
4770 struct perf_output_handle handle
;
4771 struct perf_sample_data sample
;
4772 struct perf_read_event read_event
= {
4774 .type
= PERF_RECORD_READ
,
4776 .size
= sizeof(read_event
) + event
->read_size
,
4778 .pid
= perf_event_pid(event
, task
),
4779 .tid
= perf_event_tid(event
, task
),
4783 perf_event_header__init_id(&read_event
.header
, &sample
, event
);
4784 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
);
4788 perf_output_put(&handle
, read_event
);
4789 perf_output_read(&handle
, event
);
4790 perf_event__output_id_sample(event
, &handle
, &sample
);
4792 perf_output_end(&handle
);
4795 typedef void (perf_event_aux_output_cb
)(struct perf_event
*event
, void *data
);
4798 perf_event_aux_ctx(struct perf_event_context
*ctx
,
4799 perf_event_aux_output_cb output
,
4802 struct perf_event
*event
;
4804 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
4805 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
4807 if (!event_filter_match(event
))
4809 output(event
, data
);
4814 perf_event_aux(perf_event_aux_output_cb output
, void *data
,
4815 struct perf_event_context
*task_ctx
)
4817 struct perf_cpu_context
*cpuctx
;
4818 struct perf_event_context
*ctx
;
4823 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
4824 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
4825 if (cpuctx
->unique_pmu
!= pmu
)
4827 perf_event_aux_ctx(&cpuctx
->ctx
, output
, data
);
4830 ctxn
= pmu
->task_ctx_nr
;
4833 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
4835 perf_event_aux_ctx(ctx
, output
, data
);
4837 put_cpu_ptr(pmu
->pmu_cpu_context
);
4842 perf_event_aux_ctx(task_ctx
, output
, data
);
4849 * task tracking -- fork/exit
4851 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
4854 struct perf_task_event
{
4855 struct task_struct
*task
;
4856 struct perf_event_context
*task_ctx
;
4859 struct perf_event_header header
;
4869 static int perf_event_task_match(struct perf_event
*event
)
4871 return event
->attr
.comm
|| event
->attr
.mmap
||
4872 event
->attr
.mmap2
|| event
->attr
.mmap_data
||
4876 static void perf_event_task_output(struct perf_event
*event
,
4879 struct perf_task_event
*task_event
= data
;
4880 struct perf_output_handle handle
;
4881 struct perf_sample_data sample
;
4882 struct task_struct
*task
= task_event
->task
;
4883 int ret
, size
= task_event
->event_id
.header
.size
;
4885 if (!perf_event_task_match(event
))
4888 perf_event_header__init_id(&task_event
->event_id
.header
, &sample
, event
);
4890 ret
= perf_output_begin(&handle
, event
,
4891 task_event
->event_id
.header
.size
);
4895 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
4896 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
4898 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
4899 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
4901 perf_output_put(&handle
, task_event
->event_id
);
4903 perf_event__output_id_sample(event
, &handle
, &sample
);
4905 perf_output_end(&handle
);
4907 task_event
->event_id
.header
.size
= size
;
4910 static void perf_event_task(struct task_struct
*task
,
4911 struct perf_event_context
*task_ctx
,
4914 struct perf_task_event task_event
;
4916 if (!atomic_read(&nr_comm_events
) &&
4917 !atomic_read(&nr_mmap_events
) &&
4918 !atomic_read(&nr_task_events
))
4921 task_event
= (struct perf_task_event
){
4923 .task_ctx
= task_ctx
,
4926 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
4928 .size
= sizeof(task_event
.event_id
),
4934 .time
= perf_clock(),
4938 perf_event_aux(perf_event_task_output
,
4943 void perf_event_fork(struct task_struct
*task
)
4945 perf_event_task(task
, NULL
, 1);
4952 struct perf_comm_event
{
4953 struct task_struct
*task
;
4958 struct perf_event_header header
;
4965 static int perf_event_comm_match(struct perf_event
*event
)
4967 return event
->attr
.comm
;
4970 static void perf_event_comm_output(struct perf_event
*event
,
4973 struct perf_comm_event
*comm_event
= data
;
4974 struct perf_output_handle handle
;
4975 struct perf_sample_data sample
;
4976 int size
= comm_event
->event_id
.header
.size
;
4979 if (!perf_event_comm_match(event
))
4982 perf_event_header__init_id(&comm_event
->event_id
.header
, &sample
, event
);
4983 ret
= perf_output_begin(&handle
, event
,
4984 comm_event
->event_id
.header
.size
);
4989 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
4990 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
4992 perf_output_put(&handle
, comm_event
->event_id
);
4993 __output_copy(&handle
, comm_event
->comm
,
4994 comm_event
->comm_size
);
4996 perf_event__output_id_sample(event
, &handle
, &sample
);
4998 perf_output_end(&handle
);
5000 comm_event
->event_id
.header
.size
= size
;
5003 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
5005 char comm
[TASK_COMM_LEN
];
5008 memset(comm
, 0, sizeof(comm
));
5009 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
5010 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
5012 comm_event
->comm
= comm
;
5013 comm_event
->comm_size
= size
;
5015 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
5017 perf_event_aux(perf_event_comm_output
,
5022 void perf_event_comm(struct task_struct
*task
)
5024 struct perf_comm_event comm_event
;
5025 struct perf_event_context
*ctx
;
5029 for_each_task_context_nr(ctxn
) {
5030 ctx
= task
->perf_event_ctxp
[ctxn
];
5034 perf_event_enable_on_exec(ctx
);
5038 if (!atomic_read(&nr_comm_events
))
5041 comm_event
= (struct perf_comm_event
){
5047 .type
= PERF_RECORD_COMM
,
5056 perf_event_comm_event(&comm_event
);
5063 struct perf_mmap_event
{
5064 struct vm_area_struct
*vma
;
5066 const char *file_name
;
5073 struct perf_event_header header
;
5083 static int perf_event_mmap_match(struct perf_event
*event
,
5086 struct perf_mmap_event
*mmap_event
= data
;
5087 struct vm_area_struct
*vma
= mmap_event
->vma
;
5088 int executable
= vma
->vm_flags
& VM_EXEC
;
5090 return (!executable
&& event
->attr
.mmap_data
) ||
5091 (executable
&& (event
->attr
.mmap
|| event
->attr
.mmap2
));
5094 static void perf_event_mmap_output(struct perf_event
*event
,
5097 struct perf_mmap_event
*mmap_event
= data
;
5098 struct perf_output_handle handle
;
5099 struct perf_sample_data sample
;
5100 int size
= mmap_event
->event_id
.header
.size
;
5103 if (!perf_event_mmap_match(event
, data
))
5106 if (event
->attr
.mmap2
) {
5107 mmap_event
->event_id
.header
.type
= PERF_RECORD_MMAP2
;
5108 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->maj
);
5109 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->min
);
5110 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->ino
);
5111 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->ino_generation
);
5114 perf_event_header__init_id(&mmap_event
->event_id
.header
, &sample
, event
);
5115 ret
= perf_output_begin(&handle
, event
,
5116 mmap_event
->event_id
.header
.size
);
5120 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
5121 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
5123 perf_output_put(&handle
, mmap_event
->event_id
);
5125 if (event
->attr
.mmap2
) {
5126 perf_output_put(&handle
, mmap_event
->maj
);
5127 perf_output_put(&handle
, mmap_event
->min
);
5128 perf_output_put(&handle
, mmap_event
->ino
);
5129 perf_output_put(&handle
, mmap_event
->ino_generation
);
5132 __output_copy(&handle
, mmap_event
->file_name
,
5133 mmap_event
->file_size
);
5135 perf_event__output_id_sample(event
, &handle
, &sample
);
5137 perf_output_end(&handle
);
5139 mmap_event
->event_id
.header
.size
= size
;
5142 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
5144 struct vm_area_struct
*vma
= mmap_event
->vma
;
5145 struct file
*file
= vma
->vm_file
;
5146 int maj
= 0, min
= 0;
5147 u64 ino
= 0, gen
= 0;
5154 struct inode
*inode
;
5157 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
5163 * d_path() works from the end of the rb backwards, so we
5164 * need to add enough zero bytes after the string to handle
5165 * the 64bit alignment we do later.
5167 name
= d_path(&file
->f_path
, buf
, PATH_MAX
- sizeof(u64
));
5172 inode
= file_inode(vma
->vm_file
);
5173 dev
= inode
->i_sb
->s_dev
;
5175 gen
= inode
->i_generation
;
5180 name
= (char *)arch_vma_name(vma
);
5184 if (vma
->vm_start
<= vma
->vm_mm
->start_brk
&&
5185 vma
->vm_end
>= vma
->vm_mm
->brk
) {
5189 if (vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
5190 vma
->vm_end
>= vma
->vm_mm
->start_stack
) {
5200 strlcpy(tmp
, name
, sizeof(tmp
));
5204 * Since our buffer works in 8 byte units we need to align our string
5205 * size to a multiple of 8. However, we must guarantee the tail end is
5206 * zero'd out to avoid leaking random bits to userspace.
5208 size
= strlen(name
)+1;
5209 while (!IS_ALIGNED(size
, sizeof(u64
)))
5210 name
[size
++] = '\0';
5212 mmap_event
->file_name
= name
;
5213 mmap_event
->file_size
= size
;
5214 mmap_event
->maj
= maj
;
5215 mmap_event
->min
= min
;
5216 mmap_event
->ino
= ino
;
5217 mmap_event
->ino_generation
= gen
;
5219 if (!(vma
->vm_flags
& VM_EXEC
))
5220 mmap_event
->event_id
.header
.misc
|= PERF_RECORD_MISC_MMAP_DATA
;
5222 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
5224 perf_event_aux(perf_event_mmap_output
,
5231 void perf_event_mmap(struct vm_area_struct
*vma
)
5233 struct perf_mmap_event mmap_event
;
5235 if (!atomic_read(&nr_mmap_events
))
5238 mmap_event
= (struct perf_mmap_event
){
5244 .type
= PERF_RECORD_MMAP
,
5245 .misc
= PERF_RECORD_MISC_USER
,
5250 .start
= vma
->vm_start
,
5251 .len
= vma
->vm_end
- vma
->vm_start
,
5252 .pgoff
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
,
5254 /* .maj (attr_mmap2 only) */
5255 /* .min (attr_mmap2 only) */
5256 /* .ino (attr_mmap2 only) */
5257 /* .ino_generation (attr_mmap2 only) */
5260 perf_event_mmap_event(&mmap_event
);
5264 * IRQ throttle logging
5267 static void perf_log_throttle(struct perf_event
*event
, int enable
)
5269 struct perf_output_handle handle
;
5270 struct perf_sample_data sample
;
5274 struct perf_event_header header
;
5278 } throttle_event
= {
5280 .type
= PERF_RECORD_THROTTLE
,
5282 .size
= sizeof(throttle_event
),
5284 .time
= perf_clock(),
5285 .id
= primary_event_id(event
),
5286 .stream_id
= event
->id
,
5290 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
5292 perf_event_header__init_id(&throttle_event
.header
, &sample
, event
);
5294 ret
= perf_output_begin(&handle
, event
,
5295 throttle_event
.header
.size
);
5299 perf_output_put(&handle
, throttle_event
);
5300 perf_event__output_id_sample(event
, &handle
, &sample
);
5301 perf_output_end(&handle
);
5305 * Generic event overflow handling, sampling.
5308 static int __perf_event_overflow(struct perf_event
*event
,
5309 int throttle
, struct perf_sample_data
*data
,
5310 struct pt_regs
*regs
)
5312 int events
= atomic_read(&event
->event_limit
);
5313 struct hw_perf_event
*hwc
= &event
->hw
;
5318 * Non-sampling counters might still use the PMI to fold short
5319 * hardware counters, ignore those.
5321 if (unlikely(!is_sampling_event(event
)))
5324 seq
= __this_cpu_read(perf_throttled_seq
);
5325 if (seq
!= hwc
->interrupts_seq
) {
5326 hwc
->interrupts_seq
= seq
;
5327 hwc
->interrupts
= 1;
5330 if (unlikely(throttle
5331 && hwc
->interrupts
>= max_samples_per_tick
)) {
5332 __this_cpu_inc(perf_throttled_count
);
5333 hwc
->interrupts
= MAX_INTERRUPTS
;
5334 perf_log_throttle(event
, 0);
5335 tick_nohz_full_kick();
5340 if (event
->attr
.freq
) {
5341 u64 now
= perf_clock();
5342 s64 delta
= now
- hwc
->freq_time_stamp
;
5344 hwc
->freq_time_stamp
= now
;
5346 if (delta
> 0 && delta
< 2*TICK_NSEC
)
5347 perf_adjust_period(event
, delta
, hwc
->last_period
, true);
5351 * XXX event_limit might not quite work as expected on inherited
5355 event
->pending_kill
= POLL_IN
;
5356 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
5358 event
->pending_kill
= POLL_HUP
;
5359 event
->pending_disable
= 1;
5360 irq_work_queue(&event
->pending
);
5363 if (event
->overflow_handler
)
5364 event
->overflow_handler(event
, data
, regs
);
5366 perf_event_output(event
, data
, regs
);
5368 if (event
->fasync
&& event
->pending_kill
) {
5369 event
->pending_wakeup
= 1;
5370 irq_work_queue(&event
->pending
);
5376 int perf_event_overflow(struct perf_event
*event
,
5377 struct perf_sample_data
*data
,
5378 struct pt_regs
*regs
)
5380 return __perf_event_overflow(event
, 1, data
, regs
);
5384 * Generic software event infrastructure
5387 struct swevent_htable
{
5388 struct swevent_hlist
*swevent_hlist
;
5389 struct mutex hlist_mutex
;
5392 /* Recursion avoidance in each contexts */
5393 int recursion
[PERF_NR_CONTEXTS
];
5396 static DEFINE_PER_CPU(struct swevent_htable
, swevent_htable
);
5399 * We directly increment event->count and keep a second value in
5400 * event->hw.period_left to count intervals. This period event
5401 * is kept in the range [-sample_period, 0] so that we can use the
5405 u64
perf_swevent_set_period(struct perf_event
*event
)
5407 struct hw_perf_event
*hwc
= &event
->hw
;
5408 u64 period
= hwc
->last_period
;
5412 hwc
->last_period
= hwc
->sample_period
;
5415 old
= val
= local64_read(&hwc
->period_left
);
5419 nr
= div64_u64(period
+ val
, period
);
5420 offset
= nr
* period
;
5422 if (local64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
5428 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
5429 struct perf_sample_data
*data
,
5430 struct pt_regs
*regs
)
5432 struct hw_perf_event
*hwc
= &event
->hw
;
5436 overflow
= perf_swevent_set_period(event
);
5438 if (hwc
->interrupts
== MAX_INTERRUPTS
)
5441 for (; overflow
; overflow
--) {
5442 if (__perf_event_overflow(event
, throttle
,
5445 * We inhibit the overflow from happening when
5446 * hwc->interrupts == MAX_INTERRUPTS.
5454 static void perf_swevent_event(struct perf_event
*event
, u64 nr
,
5455 struct perf_sample_data
*data
,
5456 struct pt_regs
*regs
)
5458 struct hw_perf_event
*hwc
= &event
->hw
;
5460 local64_add(nr
, &event
->count
);
5465 if (!is_sampling_event(event
))
5468 if ((event
->attr
.sample_type
& PERF_SAMPLE_PERIOD
) && !event
->attr
.freq
) {
5470 return perf_swevent_overflow(event
, 1, data
, regs
);
5472 data
->period
= event
->hw
.last_period
;
5474 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
5475 return perf_swevent_overflow(event
, 1, data
, regs
);
5477 if (local64_add_negative(nr
, &hwc
->period_left
))
5480 perf_swevent_overflow(event
, 0, data
, regs
);
5483 static int perf_exclude_event(struct perf_event
*event
,
5484 struct pt_regs
*regs
)
5486 if (event
->hw
.state
& PERF_HES_STOPPED
)
5490 if (event
->attr
.exclude_user
&& user_mode(regs
))
5493 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
5500 static int perf_swevent_match(struct perf_event
*event
,
5501 enum perf_type_id type
,
5503 struct perf_sample_data
*data
,
5504 struct pt_regs
*regs
)
5506 if (event
->attr
.type
!= type
)
5509 if (event
->attr
.config
!= event_id
)
5512 if (perf_exclude_event(event
, regs
))
5518 static inline u64
swevent_hash(u64 type
, u32 event_id
)
5520 u64 val
= event_id
| (type
<< 32);
5522 return hash_64(val
, SWEVENT_HLIST_BITS
);
5525 static inline struct hlist_head
*
5526 __find_swevent_head(struct swevent_hlist
*hlist
, u64 type
, u32 event_id
)
5528 u64 hash
= swevent_hash(type
, event_id
);
5530 return &hlist
->heads
[hash
];
5533 /* For the read side: events when they trigger */
5534 static inline struct hlist_head
*
5535 find_swevent_head_rcu(struct swevent_htable
*swhash
, u64 type
, u32 event_id
)
5537 struct swevent_hlist
*hlist
;
5539 hlist
= rcu_dereference(swhash
->swevent_hlist
);
5543 return __find_swevent_head(hlist
, type
, event_id
);
5546 /* For the event head insertion and removal in the hlist */
5547 static inline struct hlist_head
*
5548 find_swevent_head(struct swevent_htable
*swhash
, struct perf_event
*event
)
5550 struct swevent_hlist
*hlist
;
5551 u32 event_id
= event
->attr
.config
;
5552 u64 type
= event
->attr
.type
;
5555 * Event scheduling is always serialized against hlist allocation
5556 * and release. Which makes the protected version suitable here.
5557 * The context lock guarantees that.
5559 hlist
= rcu_dereference_protected(swhash
->swevent_hlist
,
5560 lockdep_is_held(&event
->ctx
->lock
));
5564 return __find_swevent_head(hlist
, type
, event_id
);
5567 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
5569 struct perf_sample_data
*data
,
5570 struct pt_regs
*regs
)
5572 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5573 struct perf_event
*event
;
5574 struct hlist_head
*head
;
5577 head
= find_swevent_head_rcu(swhash
, type
, event_id
);
5581 hlist_for_each_entry_rcu(event
, head
, hlist_entry
) {
5582 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
5583 perf_swevent_event(event
, nr
, data
, regs
);
5589 int perf_swevent_get_recursion_context(void)
5591 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5593 return get_recursion_context(swhash
->recursion
);
5595 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
5597 inline void perf_swevent_put_recursion_context(int rctx
)
5599 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5601 put_recursion_context(swhash
->recursion
, rctx
);
5604 void __perf_sw_event(u32 event_id
, u64 nr
, struct pt_regs
*regs
, u64 addr
)
5606 struct perf_sample_data data
;
5609 preempt_disable_notrace();
5610 rctx
= perf_swevent_get_recursion_context();
5614 perf_sample_data_init(&data
, addr
, 0);
5616 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, &data
, regs
);
5618 perf_swevent_put_recursion_context(rctx
);
5619 preempt_enable_notrace();
5622 static void perf_swevent_read(struct perf_event
*event
)
5626 static int perf_swevent_add(struct perf_event
*event
, int flags
)
5628 struct swevent_htable
*swhash
= &__get_cpu_var(swevent_htable
);
5629 struct hw_perf_event
*hwc
= &event
->hw
;
5630 struct hlist_head
*head
;
5632 if (is_sampling_event(event
)) {
5633 hwc
->last_period
= hwc
->sample_period
;
5634 perf_swevent_set_period(event
);
5637 hwc
->state
= !(flags
& PERF_EF_START
);
5639 head
= find_swevent_head(swhash
, event
);
5640 if (WARN_ON_ONCE(!head
))
5643 hlist_add_head_rcu(&event
->hlist_entry
, head
);
5648 static void perf_swevent_del(struct perf_event
*event
, int flags
)
5650 hlist_del_rcu(&event
->hlist_entry
);
5653 static void perf_swevent_start(struct perf_event
*event
, int flags
)
5655 event
->hw
.state
= 0;
5658 static void perf_swevent_stop(struct perf_event
*event
, int flags
)
5660 event
->hw
.state
= PERF_HES_STOPPED
;
5663 /* Deref the hlist from the update side */
5664 static inline struct swevent_hlist
*
5665 swevent_hlist_deref(struct swevent_htable
*swhash
)
5667 return rcu_dereference_protected(swhash
->swevent_hlist
,
5668 lockdep_is_held(&swhash
->hlist_mutex
));
5671 static void swevent_hlist_release(struct swevent_htable
*swhash
)
5673 struct swevent_hlist
*hlist
= swevent_hlist_deref(swhash
);
5678 rcu_assign_pointer(swhash
->swevent_hlist
, NULL
);
5679 kfree_rcu(hlist
, rcu_head
);
5682 static void swevent_hlist_put_cpu(struct perf_event
*event
, int cpu
)
5684 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
5686 mutex_lock(&swhash
->hlist_mutex
);
5688 if (!--swhash
->hlist_refcount
)
5689 swevent_hlist_release(swhash
);
5691 mutex_unlock(&swhash
->hlist_mutex
);
5694 static void swevent_hlist_put(struct perf_event
*event
)
5698 for_each_possible_cpu(cpu
)
5699 swevent_hlist_put_cpu(event
, cpu
);
5702 static int swevent_hlist_get_cpu(struct perf_event
*event
, int cpu
)
5704 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
5707 mutex_lock(&swhash
->hlist_mutex
);
5709 if (!swevent_hlist_deref(swhash
) && cpu_online(cpu
)) {
5710 struct swevent_hlist
*hlist
;
5712 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
5717 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
5719 swhash
->hlist_refcount
++;
5721 mutex_unlock(&swhash
->hlist_mutex
);
5726 static int swevent_hlist_get(struct perf_event
*event
)
5729 int cpu
, failed_cpu
;
5732 for_each_possible_cpu(cpu
) {
5733 err
= swevent_hlist_get_cpu(event
, cpu
);
5743 for_each_possible_cpu(cpu
) {
5744 if (cpu
== failed_cpu
)
5746 swevent_hlist_put_cpu(event
, cpu
);
5753 struct static_key perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
5755 static void sw_perf_event_destroy(struct perf_event
*event
)
5757 u64 event_id
= event
->attr
.config
;
5759 WARN_ON(event
->parent
);
5761 static_key_slow_dec(&perf_swevent_enabled
[event_id
]);
5762 swevent_hlist_put(event
);
5765 static int perf_swevent_init(struct perf_event
*event
)
5767 u64 event_id
= event
->attr
.config
;
5769 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
5773 * no branch sampling for software events
5775 if (has_branch_stack(event
))
5779 case PERF_COUNT_SW_CPU_CLOCK
:
5780 case PERF_COUNT_SW_TASK_CLOCK
:
5787 if (event_id
>= PERF_COUNT_SW_MAX
)
5790 if (!event
->parent
) {
5793 err
= swevent_hlist_get(event
);
5797 static_key_slow_inc(&perf_swevent_enabled
[event_id
]);
5798 event
->destroy
= sw_perf_event_destroy
;
5804 static int perf_swevent_event_idx(struct perf_event
*event
)
5809 static struct pmu perf_swevent
= {
5810 .task_ctx_nr
= perf_sw_context
,
5812 .event_init
= perf_swevent_init
,
5813 .add
= perf_swevent_add
,
5814 .del
= perf_swevent_del
,
5815 .start
= perf_swevent_start
,
5816 .stop
= perf_swevent_stop
,
5817 .read
= perf_swevent_read
,
5819 .event_idx
= perf_swevent_event_idx
,
5822 #ifdef CONFIG_EVENT_TRACING
5824 static int perf_tp_filter_match(struct perf_event
*event
,
5825 struct perf_sample_data
*data
)
5827 void *record
= data
->raw
->data
;
5829 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
5834 static int perf_tp_event_match(struct perf_event
*event
,
5835 struct perf_sample_data
*data
,
5836 struct pt_regs
*regs
)
5838 if (event
->hw
.state
& PERF_HES_STOPPED
)
5841 * All tracepoints are from kernel-space.
5843 if (event
->attr
.exclude_kernel
)
5846 if (!perf_tp_filter_match(event
, data
))
5852 void perf_tp_event(u64 addr
, u64 count
, void *record
, int entry_size
,
5853 struct pt_regs
*regs
, struct hlist_head
*head
, int rctx
,
5854 struct task_struct
*task
)
5856 struct perf_sample_data data
;
5857 struct perf_event
*event
;
5859 struct perf_raw_record raw
= {
5864 perf_sample_data_init(&data
, addr
, 0);
5867 hlist_for_each_entry_rcu(event
, head
, hlist_entry
) {
5868 if (perf_tp_event_match(event
, &data
, regs
))
5869 perf_swevent_event(event
, count
, &data
, regs
);
5873 * If we got specified a target task, also iterate its context and
5874 * deliver this event there too.
5876 if (task
&& task
!= current
) {
5877 struct perf_event_context
*ctx
;
5878 struct trace_entry
*entry
= record
;
5881 ctx
= rcu_dereference(task
->perf_event_ctxp
[perf_sw_context
]);
5885 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
5886 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
5888 if (event
->attr
.config
!= entry
->type
)
5890 if (perf_tp_event_match(event
, &data
, regs
))
5891 perf_swevent_event(event
, count
, &data
, regs
);
5897 perf_swevent_put_recursion_context(rctx
);
5899 EXPORT_SYMBOL_GPL(perf_tp_event
);
5901 static void tp_perf_event_destroy(struct perf_event
*event
)
5903 perf_trace_destroy(event
);
5906 static int perf_tp_event_init(struct perf_event
*event
)
5910 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
5914 * no branch sampling for tracepoint events
5916 if (has_branch_stack(event
))
5919 err
= perf_trace_init(event
);
5923 event
->destroy
= tp_perf_event_destroy
;
5928 static struct pmu perf_tracepoint
= {
5929 .task_ctx_nr
= perf_sw_context
,
5931 .event_init
= perf_tp_event_init
,
5932 .add
= perf_trace_add
,
5933 .del
= perf_trace_del
,
5934 .start
= perf_swevent_start
,
5935 .stop
= perf_swevent_stop
,
5936 .read
= perf_swevent_read
,
5938 .event_idx
= perf_swevent_event_idx
,
5941 static inline void perf_tp_register(void)
5943 perf_pmu_register(&perf_tracepoint
, "tracepoint", PERF_TYPE_TRACEPOINT
);
5946 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
5951 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
5954 filter_str
= strndup_user(arg
, PAGE_SIZE
);
5955 if (IS_ERR(filter_str
))
5956 return PTR_ERR(filter_str
);
5958 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
, filter_str
);
5964 static void perf_event_free_filter(struct perf_event
*event
)
5966 ftrace_profile_free_filter(event
);
5971 static inline void perf_tp_register(void)
5975 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
5980 static void perf_event_free_filter(struct perf_event
*event
)
5984 #endif /* CONFIG_EVENT_TRACING */
5986 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5987 void perf_bp_event(struct perf_event
*bp
, void *data
)
5989 struct perf_sample_data sample
;
5990 struct pt_regs
*regs
= data
;
5992 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
, 0);
5994 if (!bp
->hw
.state
&& !perf_exclude_event(bp
, regs
))
5995 perf_swevent_event(bp
, 1, &sample
, regs
);
6000 * hrtimer based swevent callback
6003 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
6005 enum hrtimer_restart ret
= HRTIMER_RESTART
;
6006 struct perf_sample_data data
;
6007 struct pt_regs
*regs
;
6008 struct perf_event
*event
;
6011 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
6013 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
6014 return HRTIMER_NORESTART
;
6016 event
->pmu
->read(event
);
6018 perf_sample_data_init(&data
, 0, event
->hw
.last_period
);
6019 regs
= get_irq_regs();
6021 if (regs
&& !perf_exclude_event(event
, regs
)) {
6022 if (!(event
->attr
.exclude_idle
&& is_idle_task(current
)))
6023 if (__perf_event_overflow(event
, 1, &data
, regs
))
6024 ret
= HRTIMER_NORESTART
;
6027 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
6028 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
6033 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
6035 struct hw_perf_event
*hwc
= &event
->hw
;
6038 if (!is_sampling_event(event
))
6041 period
= local64_read(&hwc
->period_left
);
6046 local64_set(&hwc
->period_left
, 0);
6048 period
= max_t(u64
, 10000, hwc
->sample_period
);
6050 __hrtimer_start_range_ns(&hwc
->hrtimer
,
6051 ns_to_ktime(period
), 0,
6052 HRTIMER_MODE_REL_PINNED
, 0);
6055 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
6057 struct hw_perf_event
*hwc
= &event
->hw
;
6059 if (is_sampling_event(event
)) {
6060 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
6061 local64_set(&hwc
->period_left
, ktime_to_ns(remaining
));
6063 hrtimer_cancel(&hwc
->hrtimer
);
6067 static void perf_swevent_init_hrtimer(struct perf_event
*event
)
6069 struct hw_perf_event
*hwc
= &event
->hw
;
6071 if (!is_sampling_event(event
))
6074 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
6075 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
6078 * Since hrtimers have a fixed rate, we can do a static freq->period
6079 * mapping and avoid the whole period adjust feedback stuff.
6081 if (event
->attr
.freq
) {
6082 long freq
= event
->attr
.sample_freq
;
6084 event
->attr
.sample_period
= NSEC_PER_SEC
/ freq
;
6085 hwc
->sample_period
= event
->attr
.sample_period
;
6086 local64_set(&hwc
->period_left
, hwc
->sample_period
);
6087 hwc
->last_period
= hwc
->sample_period
;
6088 event
->attr
.freq
= 0;
6093 * Software event: cpu wall time clock
6096 static void cpu_clock_event_update(struct perf_event
*event
)
6101 now
= local_clock();
6102 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
6103 local64_add(now
- prev
, &event
->count
);
6106 static void cpu_clock_event_start(struct perf_event
*event
, int flags
)
6108 local64_set(&event
->hw
.prev_count
, local_clock());
6109 perf_swevent_start_hrtimer(event
);
6112 static void cpu_clock_event_stop(struct perf_event
*event
, int flags
)
6114 perf_swevent_cancel_hrtimer(event
);
6115 cpu_clock_event_update(event
);
6118 static int cpu_clock_event_add(struct perf_event
*event
, int flags
)
6120 if (flags
& PERF_EF_START
)
6121 cpu_clock_event_start(event
, flags
);
6126 static void cpu_clock_event_del(struct perf_event
*event
, int flags
)
6128 cpu_clock_event_stop(event
, flags
);
6131 static void cpu_clock_event_read(struct perf_event
*event
)
6133 cpu_clock_event_update(event
);
6136 static int cpu_clock_event_init(struct perf_event
*event
)
6138 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
6141 if (event
->attr
.config
!= PERF_COUNT_SW_CPU_CLOCK
)
6145 * no branch sampling for software events
6147 if (has_branch_stack(event
))
6150 perf_swevent_init_hrtimer(event
);
6155 static struct pmu perf_cpu_clock
= {
6156 .task_ctx_nr
= perf_sw_context
,
6158 .event_init
= cpu_clock_event_init
,
6159 .add
= cpu_clock_event_add
,
6160 .del
= cpu_clock_event_del
,
6161 .start
= cpu_clock_event_start
,
6162 .stop
= cpu_clock_event_stop
,
6163 .read
= cpu_clock_event_read
,
6165 .event_idx
= perf_swevent_event_idx
,
6169 * Software event: task time clock
6172 static void task_clock_event_update(struct perf_event
*event
, u64 now
)
6177 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
6179 local64_add(delta
, &event
->count
);
6182 static void task_clock_event_start(struct perf_event
*event
, int flags
)
6184 local64_set(&event
->hw
.prev_count
, event
->ctx
->time
);
6185 perf_swevent_start_hrtimer(event
);
6188 static void task_clock_event_stop(struct perf_event
*event
, int flags
)
6190 perf_swevent_cancel_hrtimer(event
);
6191 task_clock_event_update(event
, event
->ctx
->time
);
6194 static int task_clock_event_add(struct perf_event
*event
, int flags
)
6196 if (flags
& PERF_EF_START
)
6197 task_clock_event_start(event
, flags
);
6202 static void task_clock_event_del(struct perf_event
*event
, int flags
)
6204 task_clock_event_stop(event
, PERF_EF_UPDATE
);
6207 static void task_clock_event_read(struct perf_event
*event
)
6209 u64 now
= perf_clock();
6210 u64 delta
= now
- event
->ctx
->timestamp
;
6211 u64 time
= event
->ctx
->time
+ delta
;
6213 task_clock_event_update(event
, time
);
6216 static int task_clock_event_init(struct perf_event
*event
)
6218 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
6221 if (event
->attr
.config
!= PERF_COUNT_SW_TASK_CLOCK
)
6225 * no branch sampling for software events
6227 if (has_branch_stack(event
))
6230 perf_swevent_init_hrtimer(event
);
6235 static struct pmu perf_task_clock
= {
6236 .task_ctx_nr
= perf_sw_context
,
6238 .event_init
= task_clock_event_init
,
6239 .add
= task_clock_event_add
,
6240 .del
= task_clock_event_del
,
6241 .start
= task_clock_event_start
,
6242 .stop
= task_clock_event_stop
,
6243 .read
= task_clock_event_read
,
6245 .event_idx
= perf_swevent_event_idx
,
6248 static void perf_pmu_nop_void(struct pmu
*pmu
)
6252 static int perf_pmu_nop_int(struct pmu
*pmu
)
6257 static void perf_pmu_start_txn(struct pmu
*pmu
)
6259 perf_pmu_disable(pmu
);
6262 static int perf_pmu_commit_txn(struct pmu
*pmu
)
6264 perf_pmu_enable(pmu
);
6268 static void perf_pmu_cancel_txn(struct pmu
*pmu
)
6270 perf_pmu_enable(pmu
);
6273 static int perf_event_idx_default(struct perf_event
*event
)
6275 return event
->hw
.idx
+ 1;
6279 * Ensures all contexts with the same task_ctx_nr have the same
6280 * pmu_cpu_context too.
6282 static void *find_pmu_context(int ctxn
)
6289 list_for_each_entry(pmu
, &pmus
, entry
) {
6290 if (pmu
->task_ctx_nr
== ctxn
)
6291 return pmu
->pmu_cpu_context
;
6297 static void update_pmu_context(struct pmu
*pmu
, struct pmu
*old_pmu
)
6301 for_each_possible_cpu(cpu
) {
6302 struct perf_cpu_context
*cpuctx
;
6304 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
6306 if (cpuctx
->unique_pmu
== old_pmu
)
6307 cpuctx
->unique_pmu
= pmu
;
6311 static void free_pmu_context(struct pmu
*pmu
)
6315 mutex_lock(&pmus_lock
);
6317 * Like a real lame refcount.
6319 list_for_each_entry(i
, &pmus
, entry
) {
6320 if (i
->pmu_cpu_context
== pmu
->pmu_cpu_context
) {
6321 update_pmu_context(i
, pmu
);
6326 free_percpu(pmu
->pmu_cpu_context
);
6328 mutex_unlock(&pmus_lock
);
6330 static struct idr pmu_idr
;
6333 type_show(struct device
*dev
, struct device_attribute
*attr
, char *page
)
6335 struct pmu
*pmu
= dev_get_drvdata(dev
);
6337 return snprintf(page
, PAGE_SIZE
-1, "%d\n", pmu
->type
);
6339 static DEVICE_ATTR_RO(type
);
6342 perf_event_mux_interval_ms_show(struct device
*dev
,
6343 struct device_attribute
*attr
,
6346 struct pmu
*pmu
= dev_get_drvdata(dev
);
6348 return snprintf(page
, PAGE_SIZE
-1, "%d\n", pmu
->hrtimer_interval_ms
);
6352 perf_event_mux_interval_ms_store(struct device
*dev
,
6353 struct device_attribute
*attr
,
6354 const char *buf
, size_t count
)
6356 struct pmu
*pmu
= dev_get_drvdata(dev
);
6357 int timer
, cpu
, ret
;
6359 ret
= kstrtoint(buf
, 0, &timer
);
6366 /* same value, noting to do */
6367 if (timer
== pmu
->hrtimer_interval_ms
)
6370 pmu
->hrtimer_interval_ms
= timer
;
6372 /* update all cpuctx for this PMU */
6373 for_each_possible_cpu(cpu
) {
6374 struct perf_cpu_context
*cpuctx
;
6375 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
6376 cpuctx
->hrtimer_interval
= ns_to_ktime(NSEC_PER_MSEC
* timer
);
6378 if (hrtimer_active(&cpuctx
->hrtimer
))
6379 hrtimer_forward_now(&cpuctx
->hrtimer
, cpuctx
->hrtimer_interval
);
6384 static DEVICE_ATTR_RW(perf_event_mux_interval_ms
);
6386 static struct attribute
*pmu_dev_attrs
[] = {
6387 &dev_attr_type
.attr
,
6388 &dev_attr_perf_event_mux_interval_ms
.attr
,
6391 ATTRIBUTE_GROUPS(pmu_dev
);
6393 static int pmu_bus_running
;
6394 static struct bus_type pmu_bus
= {
6395 .name
= "event_source",
6396 .dev_groups
= pmu_dev_groups
,
6399 static void pmu_dev_release(struct device
*dev
)
6404 static int pmu_dev_alloc(struct pmu
*pmu
)
6408 pmu
->dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
6412 pmu
->dev
->groups
= pmu
->attr_groups
;
6413 device_initialize(pmu
->dev
);
6414 ret
= dev_set_name(pmu
->dev
, "%s", pmu
->name
);
6418 dev_set_drvdata(pmu
->dev
, pmu
);
6419 pmu
->dev
->bus
= &pmu_bus
;
6420 pmu
->dev
->release
= pmu_dev_release
;
6421 ret
= device_add(pmu
->dev
);
6429 put_device(pmu
->dev
);
6433 static struct lock_class_key cpuctx_mutex
;
6434 static struct lock_class_key cpuctx_lock
;
6436 int perf_pmu_register(struct pmu
*pmu
, const char *name
, int type
)
6440 mutex_lock(&pmus_lock
);
6442 pmu
->pmu_disable_count
= alloc_percpu(int);
6443 if (!pmu
->pmu_disable_count
)
6452 type
= idr_alloc(&pmu_idr
, pmu
, PERF_TYPE_MAX
, 0, GFP_KERNEL
);
6460 if (pmu_bus_running
) {
6461 ret
= pmu_dev_alloc(pmu
);
6467 pmu
->pmu_cpu_context
= find_pmu_context(pmu
->task_ctx_nr
);
6468 if (pmu
->pmu_cpu_context
)
6469 goto got_cpu_context
;
6472 pmu
->pmu_cpu_context
= alloc_percpu(struct perf_cpu_context
);
6473 if (!pmu
->pmu_cpu_context
)
6476 for_each_possible_cpu(cpu
) {
6477 struct perf_cpu_context
*cpuctx
;
6479 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
6480 __perf_event_init_context(&cpuctx
->ctx
);
6481 lockdep_set_class(&cpuctx
->ctx
.mutex
, &cpuctx_mutex
);
6482 lockdep_set_class(&cpuctx
->ctx
.lock
, &cpuctx_lock
);
6483 cpuctx
->ctx
.type
= cpu_context
;
6484 cpuctx
->ctx
.pmu
= pmu
;
6486 __perf_cpu_hrtimer_init(cpuctx
, cpu
);
6488 INIT_LIST_HEAD(&cpuctx
->rotation_list
);
6489 cpuctx
->unique_pmu
= pmu
;
6493 if (!pmu
->start_txn
) {
6494 if (pmu
->pmu_enable
) {
6496 * If we have pmu_enable/pmu_disable calls, install
6497 * transaction stubs that use that to try and batch
6498 * hardware accesses.
6500 pmu
->start_txn
= perf_pmu_start_txn
;
6501 pmu
->commit_txn
= perf_pmu_commit_txn
;
6502 pmu
->cancel_txn
= perf_pmu_cancel_txn
;
6504 pmu
->start_txn
= perf_pmu_nop_void
;
6505 pmu
->commit_txn
= perf_pmu_nop_int
;
6506 pmu
->cancel_txn
= perf_pmu_nop_void
;
6510 if (!pmu
->pmu_enable
) {
6511 pmu
->pmu_enable
= perf_pmu_nop_void
;
6512 pmu
->pmu_disable
= perf_pmu_nop_void
;
6515 if (!pmu
->event_idx
)
6516 pmu
->event_idx
= perf_event_idx_default
;
6518 list_add_rcu(&pmu
->entry
, &pmus
);
6521 mutex_unlock(&pmus_lock
);
6526 device_del(pmu
->dev
);
6527 put_device(pmu
->dev
);
6530 if (pmu
->type
>= PERF_TYPE_MAX
)
6531 idr_remove(&pmu_idr
, pmu
->type
);
6534 free_percpu(pmu
->pmu_disable_count
);
6538 void perf_pmu_unregister(struct pmu
*pmu
)
6540 mutex_lock(&pmus_lock
);
6541 list_del_rcu(&pmu
->entry
);
6542 mutex_unlock(&pmus_lock
);
6545 * We dereference the pmu list under both SRCU and regular RCU, so
6546 * synchronize against both of those.
6548 synchronize_srcu(&pmus_srcu
);
6551 free_percpu(pmu
->pmu_disable_count
);
6552 if (pmu
->type
>= PERF_TYPE_MAX
)
6553 idr_remove(&pmu_idr
, pmu
->type
);
6554 device_del(pmu
->dev
);
6555 put_device(pmu
->dev
);
6556 free_pmu_context(pmu
);
6559 struct pmu
*perf_init_event(struct perf_event
*event
)
6561 struct pmu
*pmu
= NULL
;
6565 idx
= srcu_read_lock(&pmus_srcu
);
6568 pmu
= idr_find(&pmu_idr
, event
->attr
.type
);
6572 ret
= pmu
->event_init(event
);
6578 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
6580 ret
= pmu
->event_init(event
);
6584 if (ret
!= -ENOENT
) {
6589 pmu
= ERR_PTR(-ENOENT
);
6591 srcu_read_unlock(&pmus_srcu
, idx
);
6596 static void account_event_cpu(struct perf_event
*event
, int cpu
)
6601 if (has_branch_stack(event
)) {
6602 if (!(event
->attach_state
& PERF_ATTACH_TASK
))
6603 atomic_inc(&per_cpu(perf_branch_stack_events
, cpu
));
6605 if (is_cgroup_event(event
))
6606 atomic_inc(&per_cpu(perf_cgroup_events
, cpu
));
6609 static void account_event(struct perf_event
*event
)
6614 if (event
->attach_state
& PERF_ATTACH_TASK
)
6615 static_key_slow_inc(&perf_sched_events
.key
);
6616 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
6617 atomic_inc(&nr_mmap_events
);
6618 if (event
->attr
.comm
)
6619 atomic_inc(&nr_comm_events
);
6620 if (event
->attr
.task
)
6621 atomic_inc(&nr_task_events
);
6622 if (event
->attr
.freq
) {
6623 if (atomic_inc_return(&nr_freq_events
) == 1)
6624 tick_nohz_full_kick_all();
6626 if (has_branch_stack(event
))
6627 static_key_slow_inc(&perf_sched_events
.key
);
6628 if (is_cgroup_event(event
))
6629 static_key_slow_inc(&perf_sched_events
.key
);
6631 account_event_cpu(event
, event
->cpu
);
6635 * Allocate and initialize a event structure
6637 static struct perf_event
*
6638 perf_event_alloc(struct perf_event_attr
*attr
, int cpu
,
6639 struct task_struct
*task
,
6640 struct perf_event
*group_leader
,
6641 struct perf_event
*parent_event
,
6642 perf_overflow_handler_t overflow_handler
,
6646 struct perf_event
*event
;
6647 struct hw_perf_event
*hwc
;
6650 if ((unsigned)cpu
>= nr_cpu_ids
) {
6651 if (!task
|| cpu
!= -1)
6652 return ERR_PTR(-EINVAL
);
6655 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
6657 return ERR_PTR(-ENOMEM
);
6660 * Single events are their own group leaders, with an
6661 * empty sibling list:
6664 group_leader
= event
;
6666 mutex_init(&event
->child_mutex
);
6667 INIT_LIST_HEAD(&event
->child_list
);
6669 INIT_LIST_HEAD(&event
->group_entry
);
6670 INIT_LIST_HEAD(&event
->event_entry
);
6671 INIT_LIST_HEAD(&event
->sibling_list
);
6672 INIT_LIST_HEAD(&event
->rb_entry
);
6673 INIT_LIST_HEAD(&event
->active_entry
);
6674 INIT_HLIST_NODE(&event
->hlist_entry
);
6677 init_waitqueue_head(&event
->waitq
);
6678 init_irq_work(&event
->pending
, perf_pending_event
);
6680 mutex_init(&event
->mmap_mutex
);
6682 atomic_long_set(&event
->refcount
, 1);
6684 event
->attr
= *attr
;
6685 event
->group_leader
= group_leader
;
6689 event
->parent
= parent_event
;
6691 event
->ns
= get_pid_ns(task_active_pid_ns(current
));
6692 event
->id
= atomic64_inc_return(&perf_event_id
);
6694 event
->state
= PERF_EVENT_STATE_INACTIVE
;
6697 event
->attach_state
= PERF_ATTACH_TASK
;
6699 if (attr
->type
== PERF_TYPE_TRACEPOINT
)
6700 event
->hw
.tp_target
= task
;
6701 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6703 * hw_breakpoint is a bit difficult here..
6705 else if (attr
->type
== PERF_TYPE_BREAKPOINT
)
6706 event
->hw
.bp_target
= task
;
6710 if (!overflow_handler
&& parent_event
) {
6711 overflow_handler
= parent_event
->overflow_handler
;
6712 context
= parent_event
->overflow_handler_context
;
6715 event
->overflow_handler
= overflow_handler
;
6716 event
->overflow_handler_context
= context
;
6718 perf_event__state_init(event
);
6723 hwc
->sample_period
= attr
->sample_period
;
6724 if (attr
->freq
&& attr
->sample_freq
)
6725 hwc
->sample_period
= 1;
6726 hwc
->last_period
= hwc
->sample_period
;
6728 local64_set(&hwc
->period_left
, hwc
->sample_period
);
6731 * we currently do not support PERF_FORMAT_GROUP on inherited events
6733 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
6736 pmu
= perf_init_event(event
);
6739 else if (IS_ERR(pmu
)) {
6744 if (!event
->parent
) {
6745 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
6746 err
= get_callchain_buffers();
6756 event
->destroy(event
);
6759 put_pid_ns(event
->ns
);
6762 return ERR_PTR(err
);
6765 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
6766 struct perf_event_attr
*attr
)
6771 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
6775 * zero the full structure, so that a short copy will be nice.
6777 memset(attr
, 0, sizeof(*attr
));
6779 ret
= get_user(size
, &uattr
->size
);
6783 if (size
> PAGE_SIZE
) /* silly large */
6786 if (!size
) /* abi compat */
6787 size
= PERF_ATTR_SIZE_VER0
;
6789 if (size
< PERF_ATTR_SIZE_VER0
)
6793 * If we're handed a bigger struct than we know of,
6794 * ensure all the unknown bits are 0 - i.e. new
6795 * user-space does not rely on any kernel feature
6796 * extensions we dont know about yet.
6798 if (size
> sizeof(*attr
)) {
6799 unsigned char __user
*addr
;
6800 unsigned char __user
*end
;
6803 addr
= (void __user
*)uattr
+ sizeof(*attr
);
6804 end
= (void __user
*)uattr
+ size
;
6806 for (; addr
< end
; addr
++) {
6807 ret
= get_user(val
, addr
);
6813 size
= sizeof(*attr
);
6816 ret
= copy_from_user(attr
, uattr
, size
);
6820 /* disabled for now */
6824 if (attr
->__reserved_1
)
6827 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
6830 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
6833 if (attr
->sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
6834 u64 mask
= attr
->branch_sample_type
;
6836 /* only using defined bits */
6837 if (mask
& ~(PERF_SAMPLE_BRANCH_MAX
-1))
6840 /* at least one branch bit must be set */
6841 if (!(mask
& ~PERF_SAMPLE_BRANCH_PLM_ALL
))
6844 /* propagate priv level, when not set for branch */
6845 if (!(mask
& PERF_SAMPLE_BRANCH_PLM_ALL
)) {
6847 /* exclude_kernel checked on syscall entry */
6848 if (!attr
->exclude_kernel
)
6849 mask
|= PERF_SAMPLE_BRANCH_KERNEL
;
6851 if (!attr
->exclude_user
)
6852 mask
|= PERF_SAMPLE_BRANCH_USER
;
6854 if (!attr
->exclude_hv
)
6855 mask
|= PERF_SAMPLE_BRANCH_HV
;
6857 * adjust user setting (for HW filter setup)
6859 attr
->branch_sample_type
= mask
;
6861 /* privileged levels capture (kernel, hv): check permissions */
6862 if ((mask
& PERF_SAMPLE_BRANCH_PERM_PLM
)
6863 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
6867 if (attr
->sample_type
& PERF_SAMPLE_REGS_USER
) {
6868 ret
= perf_reg_validate(attr
->sample_regs_user
);
6873 if (attr
->sample_type
& PERF_SAMPLE_STACK_USER
) {
6874 if (!arch_perf_have_user_stack_dump())
6878 * We have __u32 type for the size, but so far
6879 * we can only use __u16 as maximum due to the
6880 * __u16 sample size limit.
6882 if (attr
->sample_stack_user
>= USHRT_MAX
)
6884 else if (!IS_ALIGNED(attr
->sample_stack_user
, sizeof(u64
)))
6892 put_user(sizeof(*attr
), &uattr
->size
);
6898 perf_event_set_output(struct perf_event
*event
, struct perf_event
*output_event
)
6900 struct ring_buffer
*rb
= NULL
, *old_rb
= NULL
;
6906 /* don't allow circular references */
6907 if (event
== output_event
)
6911 * Don't allow cross-cpu buffers
6913 if (output_event
->cpu
!= event
->cpu
)
6917 * If its not a per-cpu rb, it must be the same task.
6919 if (output_event
->cpu
== -1 && output_event
->ctx
!= event
->ctx
)
6923 mutex_lock(&event
->mmap_mutex
);
6924 /* Can't redirect output if we've got an active mmap() */
6925 if (atomic_read(&event
->mmap_count
))
6931 /* get the rb we want to redirect to */
6932 rb
= ring_buffer_get(output_event
);
6938 ring_buffer_detach(event
, old_rb
);
6941 ring_buffer_attach(event
, rb
);
6943 rcu_assign_pointer(event
->rb
, rb
);
6946 ring_buffer_put(old_rb
);
6948 * Since we detached before setting the new rb, so that we
6949 * could attach the new rb, we could have missed a wakeup.
6952 wake_up_all(&event
->waitq
);
6957 mutex_unlock(&event
->mmap_mutex
);
6964 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6966 * @attr_uptr: event_id type attributes for monitoring/sampling
6969 * @group_fd: group leader event fd
6971 SYSCALL_DEFINE5(perf_event_open
,
6972 struct perf_event_attr __user
*, attr_uptr
,
6973 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
6975 struct perf_event
*group_leader
= NULL
, *output_event
= NULL
;
6976 struct perf_event
*event
, *sibling
;
6977 struct perf_event_attr attr
;
6978 struct perf_event_context
*ctx
;
6979 struct file
*event_file
= NULL
;
6980 struct fd group
= {NULL
, 0};
6981 struct task_struct
*task
= NULL
;
6986 int f_flags
= O_RDWR
;
6988 /* for future expandability... */
6989 if (flags
& ~PERF_FLAG_ALL
)
6992 err
= perf_copy_attr(attr_uptr
, &attr
);
6996 if (!attr
.exclude_kernel
) {
6997 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
7002 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
7007 * In cgroup mode, the pid argument is used to pass the fd
7008 * opened to the cgroup directory in cgroupfs. The cpu argument
7009 * designates the cpu on which to monitor threads from that
7012 if ((flags
& PERF_FLAG_PID_CGROUP
) && (pid
== -1 || cpu
== -1))
7015 if (flags
& PERF_FLAG_FD_CLOEXEC
)
7016 f_flags
|= O_CLOEXEC
;
7018 event_fd
= get_unused_fd_flags(f_flags
);
7022 if (group_fd
!= -1) {
7023 err
= perf_fget_light(group_fd
, &group
);
7026 group_leader
= group
.file
->private_data
;
7027 if (flags
& PERF_FLAG_FD_OUTPUT
)
7028 output_event
= group_leader
;
7029 if (flags
& PERF_FLAG_FD_NO_GROUP
)
7030 group_leader
= NULL
;
7033 if (pid
!= -1 && !(flags
& PERF_FLAG_PID_CGROUP
)) {
7034 task
= find_lively_task_by_vpid(pid
);
7036 err
= PTR_ERR(task
);
7043 event
= perf_event_alloc(&attr
, cpu
, task
, group_leader
, NULL
,
7045 if (IS_ERR(event
)) {
7046 err
= PTR_ERR(event
);
7050 if (flags
& PERF_FLAG_PID_CGROUP
) {
7051 err
= perf_cgroup_connect(pid
, event
, &attr
, group_leader
);
7053 __free_event(event
);
7058 account_event(event
);
7061 * Special case software events and allow them to be part of
7062 * any hardware group.
7067 (is_software_event(event
) != is_software_event(group_leader
))) {
7068 if (is_software_event(event
)) {
7070 * If event and group_leader are not both a software
7071 * event, and event is, then group leader is not.
7073 * Allow the addition of software events to !software
7074 * groups, this is safe because software events never
7077 pmu
= group_leader
->pmu
;
7078 } else if (is_software_event(group_leader
) &&
7079 (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
)) {
7081 * In case the group is a pure software group, and we
7082 * try to add a hardware event, move the whole group to
7083 * the hardware context.
7090 * Get the target context (task or percpu):
7092 ctx
= find_get_context(pmu
, task
, event
->cpu
);
7099 put_task_struct(task
);
7104 * Look up the group leader (we will attach this event to it):
7110 * Do not allow a recursive hierarchy (this new sibling
7111 * becoming part of another group-sibling):
7113 if (group_leader
->group_leader
!= group_leader
)
7116 * Do not allow to attach to a group in a different
7117 * task or CPU context:
7120 if (group_leader
->ctx
->type
!= ctx
->type
)
7123 if (group_leader
->ctx
!= ctx
)
7128 * Only a group leader can be exclusive or pinned
7130 if (attr
.exclusive
|| attr
.pinned
)
7135 err
= perf_event_set_output(event
, output_event
);
7140 event_file
= anon_inode_getfile("[perf_event]", &perf_fops
, event
,
7142 if (IS_ERR(event_file
)) {
7143 err
= PTR_ERR(event_file
);
7148 struct perf_event_context
*gctx
= group_leader
->ctx
;
7150 mutex_lock(&gctx
->mutex
);
7151 perf_remove_from_context(group_leader
);
7154 * Removing from the context ends up with disabled
7155 * event. What we want here is event in the initial
7156 * startup state, ready to be add into new context.
7158 perf_event__state_init(group_leader
);
7159 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
7161 perf_remove_from_context(sibling
);
7162 perf_event__state_init(sibling
);
7165 mutex_unlock(&gctx
->mutex
);
7169 WARN_ON_ONCE(ctx
->parent_ctx
);
7170 mutex_lock(&ctx
->mutex
);
7174 perf_install_in_context(ctx
, group_leader
, event
->cpu
);
7176 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
7178 perf_install_in_context(ctx
, sibling
, event
->cpu
);
7183 perf_install_in_context(ctx
, event
, event
->cpu
);
7184 perf_unpin_context(ctx
);
7185 mutex_unlock(&ctx
->mutex
);
7189 event
->owner
= current
;
7191 mutex_lock(¤t
->perf_event_mutex
);
7192 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
7193 mutex_unlock(¤t
->perf_event_mutex
);
7196 * Precalculate sample_data sizes
7198 perf_event__header_size(event
);
7199 perf_event__id_header_size(event
);
7202 * Drop the reference on the group_event after placing the
7203 * new event on the sibling_list. This ensures destruction
7204 * of the group leader will find the pointer to itself in
7205 * perf_group_detach().
7208 fd_install(event_fd
, event_file
);
7212 perf_unpin_context(ctx
);
7219 put_task_struct(task
);
7223 put_unused_fd(event_fd
);
7228 * perf_event_create_kernel_counter
7230 * @attr: attributes of the counter to create
7231 * @cpu: cpu in which the counter is bound
7232 * @task: task to profile (NULL for percpu)
7235 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
7236 struct task_struct
*task
,
7237 perf_overflow_handler_t overflow_handler
,
7240 struct perf_event_context
*ctx
;
7241 struct perf_event
*event
;
7245 * Get the target context (task or percpu):
7248 event
= perf_event_alloc(attr
, cpu
, task
, NULL
, NULL
,
7249 overflow_handler
, context
);
7250 if (IS_ERR(event
)) {
7251 err
= PTR_ERR(event
);
7255 account_event(event
);
7257 ctx
= find_get_context(event
->pmu
, task
, cpu
);
7263 WARN_ON_ONCE(ctx
->parent_ctx
);
7264 mutex_lock(&ctx
->mutex
);
7265 perf_install_in_context(ctx
, event
, cpu
);
7266 perf_unpin_context(ctx
);
7267 mutex_unlock(&ctx
->mutex
);
7274 return ERR_PTR(err
);
7276 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
7278 void perf_pmu_migrate_context(struct pmu
*pmu
, int src_cpu
, int dst_cpu
)
7280 struct perf_event_context
*src_ctx
;
7281 struct perf_event_context
*dst_ctx
;
7282 struct perf_event
*event
, *tmp
;
7285 src_ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, src_cpu
)->ctx
;
7286 dst_ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, dst_cpu
)->ctx
;
7288 mutex_lock(&src_ctx
->mutex
);
7289 list_for_each_entry_safe(event
, tmp
, &src_ctx
->event_list
,
7291 perf_remove_from_context(event
);
7292 unaccount_event_cpu(event
, src_cpu
);
7294 list_add(&event
->migrate_entry
, &events
);
7296 mutex_unlock(&src_ctx
->mutex
);
7300 mutex_lock(&dst_ctx
->mutex
);
7301 list_for_each_entry_safe(event
, tmp
, &events
, migrate_entry
) {
7302 list_del(&event
->migrate_entry
);
7303 if (event
->state
>= PERF_EVENT_STATE_OFF
)
7304 event
->state
= PERF_EVENT_STATE_INACTIVE
;
7305 account_event_cpu(event
, dst_cpu
);
7306 perf_install_in_context(dst_ctx
, event
, dst_cpu
);
7309 mutex_unlock(&dst_ctx
->mutex
);
7311 EXPORT_SYMBOL_GPL(perf_pmu_migrate_context
);
7313 static void sync_child_event(struct perf_event
*child_event
,
7314 struct task_struct
*child
)
7316 struct perf_event
*parent_event
= child_event
->parent
;
7319 if (child_event
->attr
.inherit_stat
)
7320 perf_event_read_event(child_event
, child
);
7322 child_val
= perf_event_count(child_event
);
7325 * Add back the child's count to the parent's count:
7327 atomic64_add(child_val
, &parent_event
->child_count
);
7328 atomic64_add(child_event
->total_time_enabled
,
7329 &parent_event
->child_total_time_enabled
);
7330 atomic64_add(child_event
->total_time_running
,
7331 &parent_event
->child_total_time_running
);
7334 * Remove this event from the parent's list
7336 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
7337 mutex_lock(&parent_event
->child_mutex
);
7338 list_del_init(&child_event
->child_list
);
7339 mutex_unlock(&parent_event
->child_mutex
);
7342 * Release the parent event, if this was the last
7345 put_event(parent_event
);
7349 __perf_event_exit_task(struct perf_event
*child_event
,
7350 struct perf_event_context
*child_ctx
,
7351 struct task_struct
*child
)
7353 if (child_event
->parent
) {
7354 raw_spin_lock_irq(&child_ctx
->lock
);
7355 perf_group_detach(child_event
);
7356 raw_spin_unlock_irq(&child_ctx
->lock
);
7359 perf_remove_from_context(child_event
);
7362 * It can happen that the parent exits first, and has events
7363 * that are still around due to the child reference. These
7364 * events need to be zapped.
7366 if (child_event
->parent
) {
7367 sync_child_event(child_event
, child
);
7368 free_event(child_event
);
7372 static void perf_event_exit_task_context(struct task_struct
*child
, int ctxn
)
7374 struct perf_event
*child_event
, *tmp
;
7375 struct perf_event_context
*child_ctx
;
7376 unsigned long flags
;
7378 if (likely(!child
->perf_event_ctxp
[ctxn
])) {
7379 perf_event_task(child
, NULL
, 0);
7383 local_irq_save(flags
);
7385 * We can't reschedule here because interrupts are disabled,
7386 * and either child is current or it is a task that can't be
7387 * scheduled, so we are now safe from rescheduling changing
7390 child_ctx
= rcu_dereference_raw(child
->perf_event_ctxp
[ctxn
]);
7393 * Take the context lock here so that if find_get_context is
7394 * reading child->perf_event_ctxp, we wait until it has
7395 * incremented the context's refcount before we do put_ctx below.
7397 raw_spin_lock(&child_ctx
->lock
);
7398 task_ctx_sched_out(child_ctx
);
7399 child
->perf_event_ctxp
[ctxn
] = NULL
;
7401 * If this context is a clone; unclone it so it can't get
7402 * swapped to another process while we're removing all
7403 * the events from it.
7405 unclone_ctx(child_ctx
);
7406 update_context_time(child_ctx
);
7407 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
7410 * Report the task dead after unscheduling the events so that we
7411 * won't get any samples after PERF_RECORD_EXIT. We can however still
7412 * get a few PERF_RECORD_READ events.
7414 perf_event_task(child
, child_ctx
, 0);
7417 * We can recurse on the same lock type through:
7419 * __perf_event_exit_task()
7420 * sync_child_event()
7422 * mutex_lock(&ctx->mutex)
7424 * But since its the parent context it won't be the same instance.
7426 mutex_lock(&child_ctx
->mutex
);
7429 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->pinned_groups
,
7431 __perf_event_exit_task(child_event
, child_ctx
, child
);
7433 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->flexible_groups
,
7435 __perf_event_exit_task(child_event
, child_ctx
, child
);
7438 * If the last event was a group event, it will have appended all
7439 * its siblings to the list, but we obtained 'tmp' before that which
7440 * will still point to the list head terminating the iteration.
7442 if (!list_empty(&child_ctx
->pinned_groups
) ||
7443 !list_empty(&child_ctx
->flexible_groups
))
7446 mutex_unlock(&child_ctx
->mutex
);
7452 * When a child task exits, feed back event values to parent events.
7454 void perf_event_exit_task(struct task_struct
*child
)
7456 struct perf_event
*event
, *tmp
;
7459 mutex_lock(&child
->perf_event_mutex
);
7460 list_for_each_entry_safe(event
, tmp
, &child
->perf_event_list
,
7462 list_del_init(&event
->owner_entry
);
7465 * Ensure the list deletion is visible before we clear
7466 * the owner, closes a race against perf_release() where
7467 * we need to serialize on the owner->perf_event_mutex.
7470 event
->owner
= NULL
;
7472 mutex_unlock(&child
->perf_event_mutex
);
7474 for_each_task_context_nr(ctxn
)
7475 perf_event_exit_task_context(child
, ctxn
);
7478 static void perf_free_event(struct perf_event
*event
,
7479 struct perf_event_context
*ctx
)
7481 struct perf_event
*parent
= event
->parent
;
7483 if (WARN_ON_ONCE(!parent
))
7486 mutex_lock(&parent
->child_mutex
);
7487 list_del_init(&event
->child_list
);
7488 mutex_unlock(&parent
->child_mutex
);
7492 perf_group_detach(event
);
7493 list_del_event(event
, ctx
);
7498 * free an unexposed, unused context as created by inheritance by
7499 * perf_event_init_task below, used by fork() in case of fail.
7501 void perf_event_free_task(struct task_struct
*task
)
7503 struct perf_event_context
*ctx
;
7504 struct perf_event
*event
, *tmp
;
7507 for_each_task_context_nr(ctxn
) {
7508 ctx
= task
->perf_event_ctxp
[ctxn
];
7512 mutex_lock(&ctx
->mutex
);
7514 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
,
7516 perf_free_event(event
, ctx
);
7518 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
,
7520 perf_free_event(event
, ctx
);
7522 if (!list_empty(&ctx
->pinned_groups
) ||
7523 !list_empty(&ctx
->flexible_groups
))
7526 mutex_unlock(&ctx
->mutex
);
7532 void perf_event_delayed_put(struct task_struct
*task
)
7536 for_each_task_context_nr(ctxn
)
7537 WARN_ON_ONCE(task
->perf_event_ctxp
[ctxn
]);
7541 * inherit a event from parent task to child task:
7543 static struct perf_event
*
7544 inherit_event(struct perf_event
*parent_event
,
7545 struct task_struct
*parent
,
7546 struct perf_event_context
*parent_ctx
,
7547 struct task_struct
*child
,
7548 struct perf_event
*group_leader
,
7549 struct perf_event_context
*child_ctx
)
7551 struct perf_event
*child_event
;
7552 unsigned long flags
;
7555 * Instead of creating recursive hierarchies of events,
7556 * we link inherited events back to the original parent,
7557 * which has a filp for sure, which we use as the reference
7560 if (parent_event
->parent
)
7561 parent_event
= parent_event
->parent
;
7563 child_event
= perf_event_alloc(&parent_event
->attr
,
7566 group_leader
, parent_event
,
7568 if (IS_ERR(child_event
))
7571 if (!atomic_long_inc_not_zero(&parent_event
->refcount
)) {
7572 free_event(child_event
);
7579 * Make the child state follow the state of the parent event,
7580 * not its attr.disabled bit. We hold the parent's mutex,
7581 * so we won't race with perf_event_{en, dis}able_family.
7583 if (parent_event
->state
>= PERF_EVENT_STATE_INACTIVE
)
7584 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
7586 child_event
->state
= PERF_EVENT_STATE_OFF
;
7588 if (parent_event
->attr
.freq
) {
7589 u64 sample_period
= parent_event
->hw
.sample_period
;
7590 struct hw_perf_event
*hwc
= &child_event
->hw
;
7592 hwc
->sample_period
= sample_period
;
7593 hwc
->last_period
= sample_period
;
7595 local64_set(&hwc
->period_left
, sample_period
);
7598 child_event
->ctx
= child_ctx
;
7599 child_event
->overflow_handler
= parent_event
->overflow_handler
;
7600 child_event
->overflow_handler_context
7601 = parent_event
->overflow_handler_context
;
7604 * Precalculate sample_data sizes
7606 perf_event__header_size(child_event
);
7607 perf_event__id_header_size(child_event
);
7610 * Link it up in the child's context:
7612 raw_spin_lock_irqsave(&child_ctx
->lock
, flags
);
7613 add_event_to_ctx(child_event
, child_ctx
);
7614 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
7617 * Link this into the parent event's child list
7619 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
7620 mutex_lock(&parent_event
->child_mutex
);
7621 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
7622 mutex_unlock(&parent_event
->child_mutex
);
7627 static int inherit_group(struct perf_event
*parent_event
,
7628 struct task_struct
*parent
,
7629 struct perf_event_context
*parent_ctx
,
7630 struct task_struct
*child
,
7631 struct perf_event_context
*child_ctx
)
7633 struct perf_event
*leader
;
7634 struct perf_event
*sub
;
7635 struct perf_event
*child_ctr
;
7637 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
7638 child
, NULL
, child_ctx
);
7640 return PTR_ERR(leader
);
7641 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
7642 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
7643 child
, leader
, child_ctx
);
7644 if (IS_ERR(child_ctr
))
7645 return PTR_ERR(child_ctr
);
7651 inherit_task_group(struct perf_event
*event
, struct task_struct
*parent
,
7652 struct perf_event_context
*parent_ctx
,
7653 struct task_struct
*child
, int ctxn
,
7657 struct perf_event_context
*child_ctx
;
7659 if (!event
->attr
.inherit
) {
7664 child_ctx
= child
->perf_event_ctxp
[ctxn
];
7667 * This is executed from the parent task context, so
7668 * inherit events that have been marked for cloning.
7669 * First allocate and initialize a context for the
7673 child_ctx
= alloc_perf_context(parent_ctx
->pmu
, child
);
7677 child
->perf_event_ctxp
[ctxn
] = child_ctx
;
7680 ret
= inherit_group(event
, parent
, parent_ctx
,
7690 * Initialize the perf_event context in task_struct
7692 int perf_event_init_context(struct task_struct
*child
, int ctxn
)
7694 struct perf_event_context
*child_ctx
, *parent_ctx
;
7695 struct perf_event_context
*cloned_ctx
;
7696 struct perf_event
*event
;
7697 struct task_struct
*parent
= current
;
7698 int inherited_all
= 1;
7699 unsigned long flags
;
7702 if (likely(!parent
->perf_event_ctxp
[ctxn
]))
7706 * If the parent's context is a clone, pin it so it won't get
7709 parent_ctx
= perf_pin_task_context(parent
, ctxn
);
7712 * No need to check if parent_ctx != NULL here; since we saw
7713 * it non-NULL earlier, the only reason for it to become NULL
7714 * is if we exit, and since we're currently in the middle of
7715 * a fork we can't be exiting at the same time.
7719 * Lock the parent list. No need to lock the child - not PID
7720 * hashed yet and not running, so nobody can access it.
7722 mutex_lock(&parent_ctx
->mutex
);
7725 * We dont have to disable NMIs - we are only looking at
7726 * the list, not manipulating it:
7728 list_for_each_entry(event
, &parent_ctx
->pinned_groups
, group_entry
) {
7729 ret
= inherit_task_group(event
, parent
, parent_ctx
,
7730 child
, ctxn
, &inherited_all
);
7736 * We can't hold ctx->lock when iterating the ->flexible_group list due
7737 * to allocations, but we need to prevent rotation because
7738 * rotate_ctx() will change the list from interrupt context.
7740 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
7741 parent_ctx
->rotate_disable
= 1;
7742 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
7744 list_for_each_entry(event
, &parent_ctx
->flexible_groups
, group_entry
) {
7745 ret
= inherit_task_group(event
, parent
, parent_ctx
,
7746 child
, ctxn
, &inherited_all
);
7751 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
7752 parent_ctx
->rotate_disable
= 0;
7754 child_ctx
= child
->perf_event_ctxp
[ctxn
];
7756 if (child_ctx
&& inherited_all
) {
7758 * Mark the child context as a clone of the parent
7759 * context, or of whatever the parent is a clone of.
7761 * Note that if the parent is a clone, the holding of
7762 * parent_ctx->lock avoids it from being uncloned.
7764 cloned_ctx
= parent_ctx
->parent_ctx
;
7766 child_ctx
->parent_ctx
= cloned_ctx
;
7767 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
7769 child_ctx
->parent_ctx
= parent_ctx
;
7770 child_ctx
->parent_gen
= parent_ctx
->generation
;
7772 get_ctx(child_ctx
->parent_ctx
);
7775 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
7776 mutex_unlock(&parent_ctx
->mutex
);
7778 perf_unpin_context(parent_ctx
);
7779 put_ctx(parent_ctx
);
7785 * Initialize the perf_event context in task_struct
7787 int perf_event_init_task(struct task_struct
*child
)
7791 memset(child
->perf_event_ctxp
, 0, sizeof(child
->perf_event_ctxp
));
7792 mutex_init(&child
->perf_event_mutex
);
7793 INIT_LIST_HEAD(&child
->perf_event_list
);
7795 for_each_task_context_nr(ctxn
) {
7796 ret
= perf_event_init_context(child
, ctxn
);
7804 static void __init
perf_event_init_all_cpus(void)
7806 struct swevent_htable
*swhash
;
7809 for_each_possible_cpu(cpu
) {
7810 swhash
= &per_cpu(swevent_htable
, cpu
);
7811 mutex_init(&swhash
->hlist_mutex
);
7812 INIT_LIST_HEAD(&per_cpu(rotation_list
, cpu
));
7816 static void perf_event_init_cpu(int cpu
)
7818 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
7820 mutex_lock(&swhash
->hlist_mutex
);
7821 if (swhash
->hlist_refcount
> 0) {
7822 struct swevent_hlist
*hlist
;
7824 hlist
= kzalloc_node(sizeof(*hlist
), GFP_KERNEL
, cpu_to_node(cpu
));
7826 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
7828 mutex_unlock(&swhash
->hlist_mutex
);
7831 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7832 static void perf_pmu_rotate_stop(struct pmu
*pmu
)
7834 struct perf_cpu_context
*cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
7836 WARN_ON(!irqs_disabled());
7838 list_del_init(&cpuctx
->rotation_list
);
7841 static void __perf_event_exit_context(void *__info
)
7843 struct perf_event_context
*ctx
= __info
;
7844 struct perf_event
*event
, *tmp
;
7846 perf_pmu_rotate_stop(ctx
->pmu
);
7848 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
, group_entry
)
7849 __perf_remove_from_context(event
);
7850 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
, group_entry
)
7851 __perf_remove_from_context(event
);
7854 static void perf_event_exit_cpu_context(int cpu
)
7856 struct perf_event_context
*ctx
;
7860 idx
= srcu_read_lock(&pmus_srcu
);
7861 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
7862 ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
)->ctx
;
7864 mutex_lock(&ctx
->mutex
);
7865 smp_call_function_single(cpu
, __perf_event_exit_context
, ctx
, 1);
7866 mutex_unlock(&ctx
->mutex
);
7868 srcu_read_unlock(&pmus_srcu
, idx
);
7871 static void perf_event_exit_cpu(int cpu
)
7873 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
7875 mutex_lock(&swhash
->hlist_mutex
);
7876 swevent_hlist_release(swhash
);
7877 mutex_unlock(&swhash
->hlist_mutex
);
7879 perf_event_exit_cpu_context(cpu
);
7882 static inline void perf_event_exit_cpu(int cpu
) { }
7886 perf_reboot(struct notifier_block
*notifier
, unsigned long val
, void *v
)
7890 for_each_online_cpu(cpu
)
7891 perf_event_exit_cpu(cpu
);
7897 * Run the perf reboot notifier at the very last possible moment so that
7898 * the generic watchdog code runs as long as possible.
7900 static struct notifier_block perf_reboot_notifier
= {
7901 .notifier_call
= perf_reboot
,
7902 .priority
= INT_MIN
,
7906 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
7908 unsigned int cpu
= (long)hcpu
;
7910 switch (action
& ~CPU_TASKS_FROZEN
) {
7912 case CPU_UP_PREPARE
:
7913 case CPU_DOWN_FAILED
:
7914 perf_event_init_cpu(cpu
);
7917 case CPU_UP_CANCELED
:
7918 case CPU_DOWN_PREPARE
:
7919 perf_event_exit_cpu(cpu
);
7928 void __init
perf_event_init(void)
7934 perf_event_init_all_cpus();
7935 init_srcu_struct(&pmus_srcu
);
7936 perf_pmu_register(&perf_swevent
, "software", PERF_TYPE_SOFTWARE
);
7937 perf_pmu_register(&perf_cpu_clock
, NULL
, -1);
7938 perf_pmu_register(&perf_task_clock
, NULL
, -1);
7940 perf_cpu_notifier(perf_cpu_notify
);
7941 register_reboot_notifier(&perf_reboot_notifier
);
7943 ret
= init_hw_breakpoint();
7944 WARN(ret
, "hw_breakpoint initialization failed with: %d", ret
);
7946 /* do not patch jump label more than once per second */
7947 jump_label_rate_limit(&perf_sched_events
, HZ
);
7950 * Build time assertion that we keep the data_head at the intended
7951 * location. IOW, validation we got the __reserved[] size right.
7953 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page
, data_head
))
7957 static int __init
perf_event_sysfs_init(void)
7962 mutex_lock(&pmus_lock
);
7964 ret
= bus_register(&pmu_bus
);
7968 list_for_each_entry(pmu
, &pmus
, entry
) {
7969 if (!pmu
->name
|| pmu
->type
< 0)
7972 ret
= pmu_dev_alloc(pmu
);
7973 WARN(ret
, "Failed to register pmu: %s, reason %d\n", pmu
->name
, ret
);
7975 pmu_bus_running
= 1;
7979 mutex_unlock(&pmus_lock
);
7983 device_initcall(perf_event_sysfs_init
);
7985 #ifdef CONFIG_CGROUP_PERF
7986 static struct cgroup_subsys_state
*
7987 perf_cgroup_css_alloc(struct cgroup_subsys_state
*parent_css
)
7989 struct perf_cgroup
*jc
;
7991 jc
= kzalloc(sizeof(*jc
), GFP_KERNEL
);
7993 return ERR_PTR(-ENOMEM
);
7995 jc
->info
= alloc_percpu(struct perf_cgroup_info
);
7998 return ERR_PTR(-ENOMEM
);
8004 static void perf_cgroup_css_free(struct cgroup_subsys_state
*css
)
8006 struct perf_cgroup
*jc
= container_of(css
, struct perf_cgroup
, css
);
8008 free_percpu(jc
->info
);
8012 static int __perf_cgroup_move(void *info
)
8014 struct task_struct
*task
= info
;
8015 perf_cgroup_switch(task
, PERF_CGROUP_SWOUT
| PERF_CGROUP_SWIN
);
8019 static void perf_cgroup_attach(struct cgroup_subsys_state
*css
,
8020 struct cgroup_taskset
*tset
)
8022 struct task_struct
*task
;
8024 cgroup_taskset_for_each(task
, css
, tset
)
8025 task_function_call(task
, __perf_cgroup_move
, task
);
8028 static void perf_cgroup_exit(struct cgroup_subsys_state
*css
,
8029 struct cgroup_subsys_state
*old_css
,
8030 struct task_struct
*task
)
8033 * cgroup_exit() is called in the copy_process() failure path.
8034 * Ignore this case since the task hasn't ran yet, this avoids
8035 * trying to poke a half freed task state from generic code.
8037 if (!(task
->flags
& PF_EXITING
))
8040 task_function_call(task
, __perf_cgroup_move
, task
);
8043 struct cgroup_subsys perf_event_cgrp_subsys
= {
8044 .css_alloc
= perf_cgroup_css_alloc
,
8045 .css_free
= perf_cgroup_css_free
,
8046 .exit
= perf_cgroup_exit
,
8047 .attach
= perf_cgroup_attach
,
8049 #endif /* CONFIG_CGROUP_PERF */